xref: /titanic_52/usr/src/uts/sun4v/io/vdc.c (revision d7400d006d59b5d80be2255eb7dcf1f2f8fbf6b2)
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);
1235b98b509Sachartre static int	vdc_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1245b98b509Sachartre 		    int mod_flags, char *name, caddr_t valuep, int *lengthp);
1251ae08745Sheppo 
1261ae08745Sheppo /* setup */
1270d0c8d4bSnarayan static void	vdc_min(struct buf *bufp);
1280a55fbb7Slm66018 static int	vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen);
1298cd10891Snarayan static int	vdc_do_ldc_init(vdc_t *vdc, vdc_server_t *srvr);
1301ae08745Sheppo static int	vdc_start_ldc_connection(vdc_t *vdc);
1311ae08745Sheppo static int	vdc_create_device_nodes(vdc_t *vdc);
1324bac2208Snarayan static int	vdc_create_device_nodes_efi(vdc_t *vdc);
1334bac2208Snarayan static int	vdc_create_device_nodes_vtoc(vdc_t *vdc);
134366a92acSlm66018 static void	vdc_create_io_kstats(vdc_t *vdc);
135366a92acSlm66018 static void	vdc_create_err_kstats(vdc_t *vdc);
136366a92acSlm66018 static void	vdc_set_err_kstats(vdc_t *vdc);
137655fd6a9Sachartre static int	vdc_get_md_node(dev_info_t *dip, md_t **mdpp,
1388cd10891Snarayan 		    mde_cookie_t *vd_nodep);
1398cd10891Snarayan static int	vdc_init_ports(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_nodep);
1408cd10891Snarayan static void	vdc_fini_ports(vdc_t *vdc);
1418cd10891Snarayan static void	vdc_switch_server(vdc_t *vdcp);
1420a55fbb7Slm66018 static int	vdc_do_ldc_up(vdc_t *vdc);
1438cd10891Snarayan static void	vdc_terminate_ldc(vdc_t *vdc, vdc_server_t *srvr);
1441ae08745Sheppo static int	vdc_init_descriptor_ring(vdc_t *vdc);
1451ae08745Sheppo static void	vdc_destroy_descriptor_ring(vdc_t *vdc);
1464bac2208Snarayan static int	vdc_setup_devid(vdc_t *vdc);
147edcc0754Sachartre static void	vdc_store_label_efi(vdc_t *, efi_gpt_t *, efi_gpe_t *);
14878fcd0a1Sachartre static void	vdc_store_label_vtoc(vdc_t *, struct dk_geom *, struct vtoc *);
14978fcd0a1Sachartre static void	vdc_store_label_unk(vdc_t *vdc);
15078fcd0a1Sachartre static boolean_t vdc_is_opened(vdc_t *vdc);
1511ae08745Sheppo 
1521ae08745Sheppo /* handshake with vds */
1530a55fbb7Slm66018 static int		vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver);
1543af08d82Slm66018 static int		vdc_ver_negotiation(vdc_t *vdcp);
1551ae08745Sheppo static int		vdc_init_attr_negotiation(vdc_t *vdc);
1563af08d82Slm66018 static int		vdc_attr_negotiation(vdc_t *vdcp);
1571ae08745Sheppo static int		vdc_init_dring_negotiate(vdc_t *vdc);
1583af08d82Slm66018 static int		vdc_dring_negotiation(vdc_t *vdcp);
1593af08d82Slm66018 static int		vdc_send_rdx(vdc_t *vdcp);
1603af08d82Slm66018 static int		vdc_rdx_exchange(vdc_t *vdcp);
1610a55fbb7Slm66018 static boolean_t	vdc_is_supported_version(vio_ver_msg_t *ver_msg);
1621ae08745Sheppo 
1630a55fbb7Slm66018 /* processing incoming messages from vDisk server */
1641ae08745Sheppo static void	vdc_process_msg_thread(vdc_t *vdc);
1653af08d82Slm66018 static int	vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp);
1663af08d82Slm66018 
1670a55fbb7Slm66018 static uint_t	vdc_handle_cb(uint64_t event, caddr_t arg);
1683af08d82Slm66018 static int	vdc_process_data_msg(vdc_t *vdc, vio_msg_t *msg);
1690a55fbb7Slm66018 static int	vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg);
1700a55fbb7Slm66018 static int	vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg);
1710a55fbb7Slm66018 static int	vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *msg);
1723af08d82Slm66018 static int 	vdc_send_request(vdc_t *vdcp, int operation,
1733af08d82Slm66018 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
1743af08d82Slm66018 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
1753af08d82Slm66018 static int	vdc_map_to_shared_dring(vdc_t *vdcp, int idx);
1763af08d82Slm66018 static int 	vdc_populate_descriptor(vdc_t *vdcp, int operation,
1773af08d82Slm66018 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
1783af08d82Slm66018 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
1792f5224aeSachartre static int 	vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr,
1802f5224aeSachartre 		    size_t nbytes, int slice, diskaddr_t offset, int cb_type,
1812f5224aeSachartre 		    void *cb_arg, vio_desc_direction_t dir, boolean_t);
1823af08d82Slm66018 
1833af08d82Slm66018 static int	vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp);
1843af08d82Slm66018 static int	vdc_drain_response(vdc_t *vdcp);
1851ae08745Sheppo static int	vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx);
1863af08d82Slm66018 static int	vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep);
187e1ebb9ecSlm66018 static int	vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg);
1881ae08745Sheppo 
1891ae08745Sheppo /* dkio */
1902f5224aeSachartre static int	vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode,
1912f5224aeSachartre 		    int *rvalp);
192edcc0754Sachartre static int	vd_process_efi_ioctl(void *vdisk, int cmd, uintptr_t arg);
19378fcd0a1Sachartre static void	vdc_create_fake_geometry(vdc_t *vdc);
19478fcd0a1Sachartre static int	vdc_validate_geometry(vdc_t *vdc);
19578fcd0a1Sachartre static void	vdc_validate(vdc_t *vdc);
19678fcd0a1Sachartre static void	vdc_validate_task(void *arg);
197d10e4ef2Snarayan static int	vdc_null_copy_func(vdc_t *vdc, void *from, void *to,
198d10e4ef2Snarayan 		    int mode, int dir);
1994bac2208Snarayan static int	vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
2004bac2208Snarayan 		    int mode, int dir);
2014bac2208Snarayan static int	vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
2024bac2208Snarayan 		    int mode, int dir);
203d10e4ef2Snarayan static int	vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to,
204d10e4ef2Snarayan 		    int mode, int dir);
205d10e4ef2Snarayan static int	vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to,
206d10e4ef2Snarayan 		    int mode, int dir);
207d10e4ef2Snarayan static int	vdc_get_geom_convert(vdc_t *vdc, void *from, void *to,
208d10e4ef2Snarayan 		    int mode, int dir);
209d10e4ef2Snarayan static int	vdc_set_geom_convert(vdc_t *vdc, void *from, void *to,
210d10e4ef2Snarayan 		    int mode, int dir);
2114bac2208Snarayan static int	vdc_get_efi_convert(vdc_t *vdc, void *from, void *to,
2124bac2208Snarayan 		    int mode, int dir);
2134bac2208Snarayan static int	vdc_set_efi_convert(vdc_t *vdc, void *from, void *to,
2144bac2208Snarayan 		    int mode, int dir);
2151ae08745Sheppo 
2162f5224aeSachartre static void 	vdc_ownership_update(vdc_t *vdc, int ownership_flags);
2172f5224aeSachartre static int	vdc_access_set(vdc_t *vdc, uint64_t flags, int mode);
2182f5224aeSachartre static vdc_io_t	*vdc_failfast_io_queue(vdc_t *vdc, struct buf *buf);
2192f5224aeSachartre static int	vdc_failfast_check_resv(vdc_t *vdc);
2202f5224aeSachartre 
2211ae08745Sheppo /*
2221ae08745Sheppo  * Module variables
2231ae08745Sheppo  */
224e1ebb9ecSlm66018 
225e1ebb9ecSlm66018 /*
226e1ebb9ecSlm66018  * Tunable variables to control how long vdc waits before timing out on
227e1ebb9ecSlm66018  * various operations
228e1ebb9ecSlm66018  */
2293c96341aSnarayan static int	vdc_hshake_retries = 3;
230e1ebb9ecSlm66018 
231655fd6a9Sachartre static int	vdc_timeout = 0; /* units: seconds */
2328cd10891Snarayan static int 	vdc_ldcup_timeout = 1; /* units: seconds */
233655fd6a9Sachartre 
2343af08d82Slm66018 static uint64_t vdc_hz_min_ldc_delay;
2353af08d82Slm66018 static uint64_t vdc_min_timeout_ldc = 1 * MILLISEC;
2363af08d82Slm66018 static uint64_t vdc_hz_max_ldc_delay;
2373af08d82Slm66018 static uint64_t vdc_max_timeout_ldc = 100 * MILLISEC;
2383af08d82Slm66018 
2393af08d82Slm66018 static uint64_t vdc_ldc_read_init_delay = 1 * MILLISEC;
2403af08d82Slm66018 static uint64_t vdc_ldc_read_max_delay = 100 * MILLISEC;
241e1ebb9ecSlm66018 
242e1ebb9ecSlm66018 /* values for dumping - need to run in a tighter loop */
243e1ebb9ecSlm66018 static uint64_t	vdc_usec_timeout_dump = 100 * MILLISEC;	/* 0.1s units: ns */
244e1ebb9ecSlm66018 static int	vdc_dump_retries = 100;
245e1ebb9ecSlm66018 
2462f5224aeSachartre static uint16_t	vdc_scsi_timeout = 60;	/* 60s units: seconds  */
2472f5224aeSachartre 
2482f5224aeSachartre static uint64_t vdc_ownership_delay = 6 * MICROSEC; /* 6s units: usec */
2492f5224aeSachartre 
250e1ebb9ecSlm66018 /* Count of the number of vdc instances attached */
251e1ebb9ecSlm66018 static volatile uint32_t	vdc_instance_count = 0;
2521ae08745Sheppo 
2532f5224aeSachartre /* Tunable to log all SCSI errors */
2542f5224aeSachartre static boolean_t vdc_scsi_log_error = B_FALSE;
2552f5224aeSachartre 
2561ae08745Sheppo /* Soft state pointer */
2571ae08745Sheppo static void	*vdc_state;
2581ae08745Sheppo 
2593af08d82Slm66018 /*
2603af08d82Slm66018  * Controlling the verbosity of the error/debug messages
2613af08d82Slm66018  *
2623af08d82Slm66018  * vdc_msglevel - controls level of messages
2633af08d82Slm66018  * vdc_matchinst - 64-bit variable where each bit corresponds
2643af08d82Slm66018  *                 to the vdc instance the vdc_msglevel applies.
2653af08d82Slm66018  */
2663af08d82Slm66018 int		vdc_msglevel = 0x0;
2673af08d82Slm66018 uint64_t	vdc_matchinst = 0ull;
2681ae08745Sheppo 
2690a55fbb7Slm66018 /*
2700a55fbb7Slm66018  * Supported vDisk protocol version pairs.
2710a55fbb7Slm66018  *
2720a55fbb7Slm66018  * The first array entry is the latest and preferred version.
2730a55fbb7Slm66018  */
27417cadca8Slm66018 static const vio_ver_t	vdc_version[] = {{1, 1}};
2751ae08745Sheppo 
2761ae08745Sheppo static struct cb_ops vdc_cb_ops = {
2771ae08745Sheppo 	vdc_open,	/* cb_open */
2781ae08745Sheppo 	vdc_close,	/* cb_close */
2791ae08745Sheppo 	vdc_strategy,	/* cb_strategy */
2801ae08745Sheppo 	vdc_print,	/* cb_print */
2811ae08745Sheppo 	vdc_dump,	/* cb_dump */
2821ae08745Sheppo 	vdc_read,	/* cb_read */
2831ae08745Sheppo 	vdc_write,	/* cb_write */
2841ae08745Sheppo 	vdc_ioctl,	/* cb_ioctl */
2851ae08745Sheppo 	nodev,		/* cb_devmap */
2861ae08745Sheppo 	nodev,		/* cb_mmap */
2871ae08745Sheppo 	nodev,		/* cb_segmap */
2881ae08745Sheppo 	nochpoll,	/* cb_chpoll */
2895b98b509Sachartre 	vdc_prop_op,	/* cb_prop_op */
2901ae08745Sheppo 	NULL,		/* cb_str */
2911ae08745Sheppo 	D_MP | D_64BIT,	/* cb_flag */
2921ae08745Sheppo 	CB_REV,		/* cb_rev */
2931ae08745Sheppo 	vdc_aread,	/* cb_aread */
2941ae08745Sheppo 	vdc_awrite	/* cb_awrite */
2951ae08745Sheppo };
2961ae08745Sheppo 
2971ae08745Sheppo static struct dev_ops vdc_ops = {
2981ae08745Sheppo 	DEVO_REV,	/* devo_rev */
2991ae08745Sheppo 	0,		/* devo_refcnt */
3001ae08745Sheppo 	vdc_getinfo,	/* devo_getinfo */
3011ae08745Sheppo 	nulldev,	/* devo_identify */
3021ae08745Sheppo 	nulldev,	/* devo_probe */
3031ae08745Sheppo 	vdc_attach,	/* devo_attach */
3041ae08745Sheppo 	vdc_detach,	/* devo_detach */
3051ae08745Sheppo 	nodev,		/* devo_reset */
3061ae08745Sheppo 	&vdc_cb_ops,	/* devo_cb_ops */
3071ae08745Sheppo 	NULL,		/* devo_bus_ops */
3081ae08745Sheppo 	nulldev		/* devo_power */
3091ae08745Sheppo };
3101ae08745Sheppo 
3111ae08745Sheppo static struct modldrv modldrv = {
3121ae08745Sheppo 	&mod_driverops,
313205eeb1aSlm66018 	"virtual disk client",
3141ae08745Sheppo 	&vdc_ops,
3151ae08745Sheppo };
3161ae08745Sheppo 
3171ae08745Sheppo static struct modlinkage modlinkage = {
3181ae08745Sheppo 	MODREV_1,
3191ae08745Sheppo 	&modldrv,
3201ae08745Sheppo 	NULL
3211ae08745Sheppo };
3221ae08745Sheppo 
3231ae08745Sheppo /* -------------------------------------------------------------------------- */
3241ae08745Sheppo 
3251ae08745Sheppo /*
3261ae08745Sheppo  * Device Driver housekeeping and setup
3271ae08745Sheppo  */
3281ae08745Sheppo 
3291ae08745Sheppo int
3301ae08745Sheppo _init(void)
3311ae08745Sheppo {
3321ae08745Sheppo 	int	status;
3331ae08745Sheppo 
3341ae08745Sheppo 	if ((status = ddi_soft_state_init(&vdc_state, sizeof (vdc_t), 1)) != 0)
3351ae08745Sheppo 		return (status);
3361ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0)
3371ae08745Sheppo 		ddi_soft_state_fini(&vdc_state);
3381ae08745Sheppo 	return (status);
3391ae08745Sheppo }
3401ae08745Sheppo 
3411ae08745Sheppo int
3421ae08745Sheppo _info(struct modinfo *modinfop)
3431ae08745Sheppo {
3441ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
3451ae08745Sheppo }
3461ae08745Sheppo 
3471ae08745Sheppo int
3481ae08745Sheppo _fini(void)
3491ae08745Sheppo {
3501ae08745Sheppo 	int	status;
3511ae08745Sheppo 
3521ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
3531ae08745Sheppo 		return (status);
3541ae08745Sheppo 	ddi_soft_state_fini(&vdc_state);
3551ae08745Sheppo 	return (0);
3561ae08745Sheppo }
3571ae08745Sheppo 
3581ae08745Sheppo static int
3591ae08745Sheppo vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,  void *arg, void **resultp)
3601ae08745Sheppo {
3611ae08745Sheppo 	_NOTE(ARGUNUSED(dip))
3621ae08745Sheppo 
3630d0c8d4bSnarayan 	int	instance = VDCUNIT((dev_t)arg);
3641ae08745Sheppo 	vdc_t	*vdc = NULL;
3651ae08745Sheppo 
3661ae08745Sheppo 	switch (cmd) {
3671ae08745Sheppo 	case DDI_INFO_DEVT2DEVINFO:
3681ae08745Sheppo 		if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
3691ae08745Sheppo 			*resultp = NULL;
3701ae08745Sheppo 			return (DDI_FAILURE);
3711ae08745Sheppo 		}
3721ae08745Sheppo 		*resultp = vdc->dip;
3731ae08745Sheppo 		return (DDI_SUCCESS);
3741ae08745Sheppo 	case DDI_INFO_DEVT2INSTANCE:
3751ae08745Sheppo 		*resultp = (void *)(uintptr_t)instance;
3761ae08745Sheppo 		return (DDI_SUCCESS);
3771ae08745Sheppo 	default:
3781ae08745Sheppo 		*resultp = NULL;
3791ae08745Sheppo 		return (DDI_FAILURE);
3801ae08745Sheppo 	}
3811ae08745Sheppo }
3821ae08745Sheppo 
3831ae08745Sheppo static int
3841ae08745Sheppo vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3851ae08745Sheppo {
3862f5224aeSachartre 	kt_did_t failfast_tid, ownership_tid;
3871ae08745Sheppo 	int	instance;
3881ae08745Sheppo 	int	rv;
389*d7400d00Sachartre 	vdc_server_t *srvr;
3901ae08745Sheppo 	vdc_t	*vdc = NULL;
3911ae08745Sheppo 
3921ae08745Sheppo 	switch (cmd) {
3931ae08745Sheppo 	case DDI_DETACH:
3941ae08745Sheppo 		/* the real work happens below */
3951ae08745Sheppo 		break;
3961ae08745Sheppo 	case DDI_SUSPEND:
3971ae08745Sheppo 		/* nothing to do for this non-device */
3981ae08745Sheppo 		return (DDI_SUCCESS);
3991ae08745Sheppo 	default:
4001ae08745Sheppo 		return (DDI_FAILURE);
4011ae08745Sheppo 	}
4021ae08745Sheppo 
4031ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
4041ae08745Sheppo 	instance = ddi_get_instance(dip);
4053af08d82Slm66018 	DMSGX(1, "[%d] Entered\n", instance);
4061ae08745Sheppo 
4071ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
408e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
4091ae08745Sheppo 		return (DDI_FAILURE);
4101ae08745Sheppo 	}
4111ae08745Sheppo 
4122f5224aeSachartre 	/*
4132f5224aeSachartre 	 * This function is called when vdc is detached or if it has failed to
4142f5224aeSachartre 	 * attach. In that case, the attach may have fail before the vdisk type
4152f5224aeSachartre 	 * has been set so we can't call vdc_is_opened(). However as the attach
4162f5224aeSachartre 	 * has failed, we know that the vdisk is not opened and we can safely
4172f5224aeSachartre 	 * detach.
4182f5224aeSachartre 	 */
4192f5224aeSachartre 	if (vdc->vdisk_type != VD_DISK_TYPE_UNK && vdc_is_opened(vdc)) {
4203af08d82Slm66018 		DMSG(vdc, 0, "[%d] Cannot detach: device is open", instance);
4211ae08745Sheppo 		return (DDI_FAILURE);
4221ae08745Sheppo 	}
4231ae08745Sheppo 
42478fcd0a1Sachartre 	if (vdc->dkio_flush_pending) {
42578fcd0a1Sachartre 		DMSG(vdc, 0,
42678fcd0a1Sachartre 		    "[%d] Cannot detach: %d outstanding DKIO flushes\n",
42778fcd0a1Sachartre 		    instance, vdc->dkio_flush_pending);
42878fcd0a1Sachartre 		return (DDI_FAILURE);
42978fcd0a1Sachartre 	}
43078fcd0a1Sachartre 
43178fcd0a1Sachartre 	if (vdc->validate_pending) {
43278fcd0a1Sachartre 		DMSG(vdc, 0,
43378fcd0a1Sachartre 		    "[%d] Cannot detach: %d outstanding validate request\n",
43478fcd0a1Sachartre 		    instance, vdc->validate_pending);
43578fcd0a1Sachartre 		return (DDI_FAILURE);
43678fcd0a1Sachartre 	}
43778fcd0a1Sachartre 
4383af08d82Slm66018 	DMSG(vdc, 0, "[%d] proceeding...\n", instance);
4393af08d82Slm66018 
4402f5224aeSachartre 	/* If we took ownership, release ownership */
4412f5224aeSachartre 	mutex_enter(&vdc->ownership_lock);
4422f5224aeSachartre 	if (vdc->ownership & VDC_OWNERSHIP_GRANTED) {
4432f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_CLEAR, FKIOCTL);
4442f5224aeSachartre 		if (rv == 0) {
4452f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
4462f5224aeSachartre 		}
4472f5224aeSachartre 	}
4482f5224aeSachartre 	mutex_exit(&vdc->ownership_lock);
4492f5224aeSachartre 
4503af08d82Slm66018 	/* mark instance as detaching */
4513af08d82Slm66018 	vdc->lifecycle	= VDC_LC_DETACHING;
4521ae08745Sheppo 
4531ae08745Sheppo 	/*
454*d7400d00Sachartre 	 * Try and disable callbacks to prevent another handshake. We have to
455*d7400d00Sachartre 	 * disable callbacks for all servers.
4561ae08745Sheppo 	 */
457*d7400d00Sachartre 	for (srvr = vdc->server_list; srvr != NULL; srvr = srvr->next) {
458*d7400d00Sachartre 		rv = ldc_set_cb_mode(srvr->ldc_handle, LDC_CB_DISABLE);
459*d7400d00Sachartre 		DMSG(vdc, 0, "callback disabled (ldc=%lu, rv=%d)\n",
460*d7400d00Sachartre 		    srvr->ldc_id, rv);
4618cd10891Snarayan 	}
4621ae08745Sheppo 
4631ae08745Sheppo 	if (vdc->initialized & VDC_THREAD) {
4643af08d82Slm66018 		mutex_enter(&vdc->read_lock);
4653af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
4663af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET)) {
4673af08d82Slm66018 			vdc->read_state = VDC_READ_RESET;
4683af08d82Slm66018 			cv_signal(&vdc->read_cv);
4691ae08745Sheppo 		}
4703af08d82Slm66018 
4713af08d82Slm66018 		mutex_exit(&vdc->read_lock);
4723af08d82Slm66018 
4733af08d82Slm66018 		/* wake up any thread waiting for connection to come online */
4743af08d82Slm66018 		mutex_enter(&vdc->lock);
4753af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
4763af08d82Slm66018 			DMSG(vdc, 0,
4773af08d82Slm66018 			    "[%d] write reset - move to resetting state...\n",
4783af08d82Slm66018 			    instance);
4793af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
4803af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
4813af08d82Slm66018 		}
4823af08d82Slm66018 		mutex_exit(&vdc->lock);
4833af08d82Slm66018 
4843af08d82Slm66018 		/* now wait until state transitions to VDC_STATE_DETACH */
4853af08d82Slm66018 		thread_join(vdc->msg_proc_thr->t_did);
4863af08d82Slm66018 		ASSERT(vdc->state == VDC_STATE_DETACH);
4873af08d82Slm66018 		DMSG(vdc, 0, "[%d] Reset thread exit and join ..\n",
4883af08d82Slm66018 		    vdc->instance);
4891ae08745Sheppo 	}
4901ae08745Sheppo 
4911ae08745Sheppo 	mutex_enter(&vdc->lock);
4921ae08745Sheppo 
4931ae08745Sheppo 	if (vdc->initialized & VDC_DRING)
4941ae08745Sheppo 		vdc_destroy_descriptor_ring(vdc);
4951ae08745Sheppo 
4968cd10891Snarayan 	vdc_fini_ports(vdc);
4971ae08745Sheppo 
4982f5224aeSachartre 	if (vdc->failfast_thread) {
4992f5224aeSachartre 		failfast_tid = vdc->failfast_thread->t_did;
5002f5224aeSachartre 		vdc->failfast_interval = 0;
5012f5224aeSachartre 		cv_signal(&vdc->failfast_cv);
5022f5224aeSachartre 	} else {
5032f5224aeSachartre 		failfast_tid = 0;
5042f5224aeSachartre 	}
5052f5224aeSachartre 
5062f5224aeSachartre 	if (vdc->ownership & VDC_OWNERSHIP_WANTED) {
5072f5224aeSachartre 		ownership_tid = vdc->ownership_thread->t_did;
5082f5224aeSachartre 		vdc->ownership = VDC_OWNERSHIP_NONE;
5092f5224aeSachartre 		cv_signal(&vdc->ownership_cv);
5102f5224aeSachartre 	} else {
5112f5224aeSachartre 		ownership_tid = 0;
5122f5224aeSachartre 	}
5132f5224aeSachartre 
5141ae08745Sheppo 	mutex_exit(&vdc->lock);
5151ae08745Sheppo 
5162f5224aeSachartre 	if (failfast_tid != 0)
5172f5224aeSachartre 		thread_join(failfast_tid);
5182f5224aeSachartre 
5192f5224aeSachartre 	if (ownership_tid != 0)
5202f5224aeSachartre 		thread_join(ownership_tid);
5212f5224aeSachartre 
5225b98b509Sachartre 	if (vdc->initialized & VDC_MINOR)
5231ae08745Sheppo 		ddi_remove_minor_node(dip, NULL);
5241ae08745Sheppo 
525366a92acSlm66018 	if (vdc->io_stats) {
526366a92acSlm66018 		kstat_delete(vdc->io_stats);
527366a92acSlm66018 		vdc->io_stats = NULL;
528366a92acSlm66018 	}
529366a92acSlm66018 
530366a92acSlm66018 	if (vdc->err_stats) {
531366a92acSlm66018 		kstat_delete(vdc->err_stats);
532366a92acSlm66018 		vdc->err_stats = NULL;
533366a92acSlm66018 	}
534366a92acSlm66018 
5351ae08745Sheppo 	if (vdc->initialized & VDC_LOCKS) {
5361ae08745Sheppo 		mutex_destroy(&vdc->lock);
5373af08d82Slm66018 		mutex_destroy(&vdc->read_lock);
5382f5224aeSachartre 		mutex_destroy(&vdc->ownership_lock);
5393af08d82Slm66018 		cv_destroy(&vdc->initwait_cv);
5403af08d82Slm66018 		cv_destroy(&vdc->dring_free_cv);
5413af08d82Slm66018 		cv_destroy(&vdc->membind_cv);
5423af08d82Slm66018 		cv_destroy(&vdc->sync_pending_cv);
5433af08d82Slm66018 		cv_destroy(&vdc->sync_blocked_cv);
5443af08d82Slm66018 		cv_destroy(&vdc->read_cv);
5453af08d82Slm66018 		cv_destroy(&vdc->running_cv);
5462f5224aeSachartre 		cv_destroy(&vdc->ownership_cv);
5472f5224aeSachartre 		cv_destroy(&vdc->failfast_cv);
5482f5224aeSachartre 		cv_destroy(&vdc->failfast_io_cv);
5491ae08745Sheppo 	}
5501ae08745Sheppo 
5511ae08745Sheppo 	if (vdc->minfo)
5521ae08745Sheppo 		kmem_free(vdc->minfo, sizeof (struct dk_minfo));
5531ae08745Sheppo 
5541ae08745Sheppo 	if (vdc->cinfo)
5551ae08745Sheppo 		kmem_free(vdc->cinfo, sizeof (struct dk_cinfo));
5561ae08745Sheppo 
5571ae08745Sheppo 	if (vdc->vtoc)
5581ae08745Sheppo 		kmem_free(vdc->vtoc, sizeof (struct vtoc));
5591ae08745Sheppo 
56078fcd0a1Sachartre 	if (vdc->geom)
56178fcd0a1Sachartre 		kmem_free(vdc->geom, sizeof (struct dk_geom));
5620a55fbb7Slm66018 
5634bac2208Snarayan 	if (vdc->devid) {
5644bac2208Snarayan 		ddi_devid_unregister(dip);
5654bac2208Snarayan 		ddi_devid_free(vdc->devid);
5664bac2208Snarayan 	}
5674bac2208Snarayan 
5681ae08745Sheppo 	if (vdc->initialized & VDC_SOFT_STATE)
5691ae08745Sheppo 		ddi_soft_state_free(vdc_state, instance);
5701ae08745Sheppo 
5713af08d82Slm66018 	DMSG(vdc, 0, "[%d] End %p\n", instance, (void *)vdc);
5721ae08745Sheppo 
5731ae08745Sheppo 	return (DDI_SUCCESS);
5741ae08745Sheppo }
5751ae08745Sheppo 
5761ae08745Sheppo 
5771ae08745Sheppo static int
5781ae08745Sheppo vdc_do_attach(dev_info_t *dip)
5791ae08745Sheppo {
5801ae08745Sheppo 	int		instance;
5811ae08745Sheppo 	vdc_t		*vdc = NULL;
5821ae08745Sheppo 	int		status;
583655fd6a9Sachartre 	md_t		*mdp;
5848cd10891Snarayan 	mde_cookie_t	vd_node;
5851ae08745Sheppo 
5861ae08745Sheppo 	ASSERT(dip != NULL);
5871ae08745Sheppo 
5881ae08745Sheppo 	instance = ddi_get_instance(dip);
5891ae08745Sheppo 	if (ddi_soft_state_zalloc(vdc_state, instance) != DDI_SUCCESS) {
590e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't alloc state structure",
591e1ebb9ecSlm66018 		    instance);
5921ae08745Sheppo 		return (DDI_FAILURE);
5931ae08745Sheppo 	}
5941ae08745Sheppo 
5951ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
596e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
5971ae08745Sheppo 		return (DDI_FAILURE);
5981ae08745Sheppo 	}
5991ae08745Sheppo 
6001ae08745Sheppo 	/*
6011ae08745Sheppo 	 * We assign the value to initialized in this case to zero out the
6021ae08745Sheppo 	 * variable and then set bits in it to indicate what has been done
6031ae08745Sheppo 	 */
6041ae08745Sheppo 	vdc->initialized = VDC_SOFT_STATE;
6051ae08745Sheppo 
6063af08d82Slm66018 	vdc_hz_min_ldc_delay = drv_usectohz(vdc_min_timeout_ldc);
6073af08d82Slm66018 	vdc_hz_max_ldc_delay = drv_usectohz(vdc_max_timeout_ldc);
6081ae08745Sheppo 
6091ae08745Sheppo 	vdc->dip	= dip;
6101ae08745Sheppo 	vdc->instance	= instance;
6111ae08745Sheppo 	vdc->vdisk_type	= VD_DISK_TYPE_UNK;
6124bac2208Snarayan 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
6133af08d82Slm66018 	vdc->state	= VDC_STATE_INIT;
6143af08d82Slm66018 	vdc->lifecycle	= VDC_LC_ATTACHING;
6151ae08745Sheppo 	vdc->session_id = 0;
6161ae08745Sheppo 	vdc->block_size = DEV_BSIZE;
6178e6a2a04Slm66018 	vdc->max_xfer_sz = maxphys / DEV_BSIZE;
6181ae08745Sheppo 
61917cadca8Slm66018 	/*
62017cadca8Slm66018 	 * We assume, for now, that the vDisk server will export 'read'
62117cadca8Slm66018 	 * operations to us at a minimum (this is needed because of checks
62217cadca8Slm66018 	 * in vdc for supported operations early in the handshake process).
62317cadca8Slm66018 	 * The vDisk server will return ENOTSUP if this is not the case.
62417cadca8Slm66018 	 * The value will be overwritten during the attribute exchange with
62517cadca8Slm66018 	 * the bitmask of operations exported by server.
62617cadca8Slm66018 	 */
62717cadca8Slm66018 	vdc->operations = VD_OP_MASK_READ;
62817cadca8Slm66018 
6291ae08745Sheppo 	vdc->vtoc = NULL;
63078fcd0a1Sachartre 	vdc->geom = NULL;
6311ae08745Sheppo 	vdc->cinfo = NULL;
6321ae08745Sheppo 	vdc->minfo = NULL;
6331ae08745Sheppo 
6341ae08745Sheppo 	mutex_init(&vdc->lock, NULL, MUTEX_DRIVER, NULL);
6353af08d82Slm66018 	cv_init(&vdc->initwait_cv, NULL, CV_DRIVER, NULL);
6363af08d82Slm66018 	cv_init(&vdc->dring_free_cv, NULL, CV_DRIVER, NULL);
6373af08d82Slm66018 	cv_init(&vdc->membind_cv, NULL, CV_DRIVER, NULL);
6383af08d82Slm66018 	cv_init(&vdc->running_cv, NULL, CV_DRIVER, NULL);
6393af08d82Slm66018 
6403af08d82Slm66018 	vdc->threads_pending = 0;
6413af08d82Slm66018 	vdc->sync_op_pending = B_FALSE;
6423af08d82Slm66018 	vdc->sync_op_blocked = B_FALSE;
6433af08d82Slm66018 	cv_init(&vdc->sync_pending_cv, NULL, CV_DRIVER, NULL);
6443af08d82Slm66018 	cv_init(&vdc->sync_blocked_cv, NULL, CV_DRIVER, NULL);
6453af08d82Slm66018 
6462f5224aeSachartre 	mutex_init(&vdc->ownership_lock, NULL, MUTEX_DRIVER, NULL);
6472f5224aeSachartre 	cv_init(&vdc->ownership_cv, NULL, CV_DRIVER, NULL);
6482f5224aeSachartre 	cv_init(&vdc->failfast_cv, NULL, CV_DRIVER, NULL);
6492f5224aeSachartre 	cv_init(&vdc->failfast_io_cv, NULL, CV_DRIVER, NULL);
6502f5224aeSachartre 
6513af08d82Slm66018 	/* init blocking msg read functionality */
6523af08d82Slm66018 	mutex_init(&vdc->read_lock, NULL, MUTEX_DRIVER, NULL);
6533af08d82Slm66018 	cv_init(&vdc->read_cv, NULL, CV_DRIVER, NULL);
6543af08d82Slm66018 	vdc->read_state = VDC_READ_IDLE;
6553af08d82Slm66018 
6561ae08745Sheppo 	vdc->initialized |= VDC_LOCKS;
6571ae08745Sheppo 
658655fd6a9Sachartre 	/* get device and port MD node for this disk instance */
6598cd10891Snarayan 	if (vdc_get_md_node(dip, &mdp, &vd_node) != 0) {
660655fd6a9Sachartre 		cmn_err(CE_NOTE, "[%d] Could not get machine description node",
661655fd6a9Sachartre 		    instance);
662655fd6a9Sachartre 		return (DDI_FAILURE);
663655fd6a9Sachartre 	}
664655fd6a9Sachartre 
6658cd10891Snarayan 	if (vdc_init_ports(vdc, mdp, vd_node) != 0) {
6668cd10891Snarayan 		cmn_err(CE_NOTE, "[%d] Error initialising ports", instance);
6678cd10891Snarayan 		return (DDI_FAILURE);
668655fd6a9Sachartre 	}
669655fd6a9Sachartre 
670655fd6a9Sachartre 	(void) md_fini_handle(mdp);
671655fd6a9Sachartre 
6723af08d82Slm66018 	/* initialize the thread responsible for managing state with server */
6733af08d82Slm66018 	vdc->msg_proc_thr = thread_create(NULL, 0, vdc_process_msg_thread,
6741ae08745Sheppo 	    vdc, 0, &p0, TS_RUN, minclsyspri);
6753af08d82Slm66018 	if (vdc->msg_proc_thr == NULL) {
6761ae08745Sheppo 		cmn_err(CE_NOTE, "[%d] Failed to create msg processing thread",
6771ae08745Sheppo 		    instance);
6781ae08745Sheppo 		return (DDI_FAILURE);
6791ae08745Sheppo 	}
6803af08d82Slm66018 
6811ae08745Sheppo 	vdc->initialized |= VDC_THREAD;
6821ae08745Sheppo 
683366a92acSlm66018 	/* Create the kstats for saving the I/O statistics used by iostat(1M) */
684366a92acSlm66018 	vdc_create_io_kstats(vdc);
685366a92acSlm66018 	vdc_create_err_kstats(vdc);
686366a92acSlm66018 
687e1ebb9ecSlm66018 	atomic_inc_32(&vdc_instance_count);
6881ae08745Sheppo 
6890a55fbb7Slm66018 	/*
69078fcd0a1Sachartre 	 * Check the disk label. This will send requests and do the handshake.
69178fcd0a1Sachartre 	 * We don't really care about the disk label now. What we really need is
69278fcd0a1Sachartre 	 * the handshake do be done so that we know the type of the disk (slice
69378fcd0a1Sachartre 	 * or full disk) and the appropriate device nodes can be created.
6940a55fbb7Slm66018 	 */
69578fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
69678fcd0a1Sachartre 	vdc->vtoc = kmem_zalloc(sizeof (struct vtoc), KM_SLEEP);
69778fcd0a1Sachartre 	vdc->geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
69817cadca8Slm66018 	vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
69978fcd0a1Sachartre 
70078fcd0a1Sachartre 	mutex_enter(&vdc->lock);
70178fcd0a1Sachartre 	(void) vdc_validate_geometry(vdc);
70278fcd0a1Sachartre 	mutex_exit(&vdc->lock);
7031ae08745Sheppo 
7041ae08745Sheppo 	/*
7055b98b509Sachartre 	 * Now that we have the device info we can create the device nodes
7061ae08745Sheppo 	 */
7071ae08745Sheppo 	status = vdc_create_device_nodes(vdc);
7081ae08745Sheppo 	if (status) {
7093af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to create device nodes",
7101ae08745Sheppo 		    instance);
7113af08d82Slm66018 		goto return_status;
7121ae08745Sheppo 	}
7131ae08745Sheppo 
7144bac2208Snarayan 	/*
7154bac2208Snarayan 	 * Setup devid
7164bac2208Snarayan 	 */
7174bac2208Snarayan 	if (vdc_setup_devid(vdc)) {
7183af08d82Slm66018 		DMSG(vdc, 0, "[%d] No device id available\n", instance);
7194bac2208Snarayan 	}
7204bac2208Snarayan 
721366a92acSlm66018 	/*
722366a92acSlm66018 	 * Fill in the fields of the error statistics kstat that were not
723366a92acSlm66018 	 * available when creating the kstat
724366a92acSlm66018 	 */
725366a92acSlm66018 	vdc_set_err_kstats(vdc);
726366a92acSlm66018 
7271ae08745Sheppo 	ddi_report_dev(dip);
7283af08d82Slm66018 	vdc->lifecycle	= VDC_LC_ONLINE;
7293af08d82Slm66018 	DMSG(vdc, 0, "[%d] Attach tasks successful\n", instance);
7301ae08745Sheppo 
7313af08d82Slm66018 return_status:
7323af08d82Slm66018 	DMSG(vdc, 0, "[%d] Attach completed\n", instance);
7331ae08745Sheppo 	return (status);
7341ae08745Sheppo }
7351ae08745Sheppo 
7361ae08745Sheppo static int
7371ae08745Sheppo vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
7381ae08745Sheppo {
7391ae08745Sheppo 	int	status;
7401ae08745Sheppo 
7411ae08745Sheppo 	switch (cmd) {
7421ae08745Sheppo 	case DDI_ATTACH:
7431ae08745Sheppo 		if ((status = vdc_do_attach(dip)) != 0)
7441ae08745Sheppo 			(void) vdc_detach(dip, DDI_DETACH);
7451ae08745Sheppo 		return (status);
7461ae08745Sheppo 	case DDI_RESUME:
7471ae08745Sheppo 		/* nothing to do for this non-device */
7481ae08745Sheppo 		return (DDI_SUCCESS);
7491ae08745Sheppo 	default:
7501ae08745Sheppo 		return (DDI_FAILURE);
7511ae08745Sheppo 	}
7521ae08745Sheppo }
7531ae08745Sheppo 
7541ae08745Sheppo static int
7558cd10891Snarayan vdc_do_ldc_init(vdc_t *vdc, vdc_server_t *srvr)
7561ae08745Sheppo {
7571ae08745Sheppo 	int			status = 0;
7581ae08745Sheppo 	ldc_status_t		ldc_state;
7591ae08745Sheppo 	ldc_attr_t		ldc_attr;
7601ae08745Sheppo 
7611ae08745Sheppo 	ASSERT(vdc != NULL);
7628cd10891Snarayan 	ASSERT(srvr != NULL);
7631ae08745Sheppo 
7641ae08745Sheppo 	ldc_attr.devclass = LDC_DEV_BLK;
7651ae08745Sheppo 	ldc_attr.instance = vdc->instance;
7661ae08745Sheppo 	ldc_attr.mode = LDC_MODE_UNRELIABLE;	/* unreliable transport */
767e1ebb9ecSlm66018 	ldc_attr.mtu = VD_LDC_MTU;
7681ae08745Sheppo 
7698cd10891Snarayan 	if ((srvr->state & VDC_LDC_INIT) == 0) {
7708cd10891Snarayan 		status = ldc_init(srvr->ldc_id, &ldc_attr,
7718cd10891Snarayan 		    &srvr->ldc_handle);
7721ae08745Sheppo 		if (status != 0) {
7733af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_init(chan %ld) returned %d",
7748cd10891Snarayan 			    vdc->instance, srvr->ldc_id, status);
7751ae08745Sheppo 			return (status);
7761ae08745Sheppo 		}
7778cd10891Snarayan 		srvr->state |= VDC_LDC_INIT;
7781ae08745Sheppo 	}
7798cd10891Snarayan 	status = ldc_status(srvr->ldc_handle, &ldc_state);
7801ae08745Sheppo 	if (status != 0) {
7813af08d82Slm66018 		DMSG(vdc, 0, "[%d] Cannot discover LDC status [err=%d]",
782e1ebb9ecSlm66018 		    vdc->instance, status);
7838cd10891Snarayan 		goto init_exit;
7841ae08745Sheppo 	}
7858cd10891Snarayan 	srvr->ldc_state = ldc_state;
7861ae08745Sheppo 
7878cd10891Snarayan 	if ((srvr->state & VDC_LDC_CB) == 0) {
7888cd10891Snarayan 		status = ldc_reg_callback(srvr->ldc_handle, vdc_handle_cb,
7898cd10891Snarayan 		    (caddr_t)srvr);
7901ae08745Sheppo 		if (status != 0) {
7913af08d82Slm66018 			DMSG(vdc, 0, "[%d] LDC callback reg. failed (%d)",
792e1ebb9ecSlm66018 			    vdc->instance, status);
7938cd10891Snarayan 			goto init_exit;
7941ae08745Sheppo 		}
7958cd10891Snarayan 		srvr->state |= VDC_LDC_CB;
7961ae08745Sheppo 	}
7971ae08745Sheppo 
7981ae08745Sheppo 	/*
7991ae08745Sheppo 	 * At this stage we have initialised LDC, we will now try and open
8001ae08745Sheppo 	 * the connection.
8011ae08745Sheppo 	 */
8028cd10891Snarayan 	if (srvr->ldc_state == LDC_INIT) {
8038cd10891Snarayan 		status = ldc_open(srvr->ldc_handle);
8041ae08745Sheppo 		if (status != 0) {
8053af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_open(chan %ld) returned %d",
8068cd10891Snarayan 			    vdc->instance, srvr->ldc_id, status);
8078cd10891Snarayan 			goto init_exit;
8081ae08745Sheppo 		}
8098cd10891Snarayan 		srvr->state |= VDC_LDC_OPEN;
8108cd10891Snarayan 	}
8118cd10891Snarayan 
8128cd10891Snarayan init_exit:
8138cd10891Snarayan 	if (status) {
8148cd10891Snarayan 		vdc_terminate_ldc(vdc, srvr);
8151ae08745Sheppo 	}
8161ae08745Sheppo 
8171ae08745Sheppo 	return (status);
8181ae08745Sheppo }
8191ae08745Sheppo 
8201ae08745Sheppo static int
8211ae08745Sheppo vdc_start_ldc_connection(vdc_t *vdc)
8221ae08745Sheppo {
8231ae08745Sheppo 	int		status = 0;
8241ae08745Sheppo 
8251ae08745Sheppo 	ASSERT(vdc != NULL);
8261ae08745Sheppo 
8273af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdc->lock));
8281ae08745Sheppo 
8290a55fbb7Slm66018 	status = vdc_do_ldc_up(vdc);
8301ae08745Sheppo 
8313af08d82Slm66018 	DMSG(vdc, 0, "[%d] Finished bringing up LDC\n", vdc->instance);
8321ae08745Sheppo 
8333af08d82Slm66018 	return (status);
8343af08d82Slm66018 }
8353af08d82Slm66018 
8363af08d82Slm66018 static int
8373af08d82Slm66018 vdc_stop_ldc_connection(vdc_t *vdcp)
8383af08d82Slm66018 {
8393af08d82Slm66018 	int	status;
8403af08d82Slm66018 
8418cd10891Snarayan 	ASSERT(vdcp != NULL);
8428cd10891Snarayan 
8438cd10891Snarayan 	ASSERT(MUTEX_HELD(&vdcp->lock));
8448cd10891Snarayan 
8453af08d82Slm66018 	DMSG(vdcp, 0, ": Resetting connection to vDisk server : state %d\n",
8463af08d82Slm66018 	    vdcp->state);
8473af08d82Slm66018 
8488cd10891Snarayan 	status = ldc_down(vdcp->curr_server->ldc_handle);
8493af08d82Slm66018 	DMSG(vdcp, 0, "ldc_down() = %d\n", status);
8503af08d82Slm66018 
8513af08d82Slm66018 	vdcp->initialized &= ~VDC_HANDSHAKE;
8523af08d82Slm66018 	DMSG(vdcp, 0, "initialized=%x\n", vdcp->initialized);
8531ae08745Sheppo 
8541ae08745Sheppo 	return (status);
8551ae08745Sheppo }
8561ae08745Sheppo 
857366a92acSlm66018 static void
858366a92acSlm66018 vdc_create_io_kstats(vdc_t *vdc)
859366a92acSlm66018 {
860366a92acSlm66018 	if (vdc->io_stats != NULL) {
861366a92acSlm66018 		DMSG(vdc, 0, "[%d] I/O kstat already exists\n", vdc->instance);
862366a92acSlm66018 		return;
863366a92acSlm66018 	}
864366a92acSlm66018 
865366a92acSlm66018 	vdc->io_stats = kstat_create(VDC_DRIVER_NAME, vdc->instance, NULL,
866366a92acSlm66018 	    "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
867366a92acSlm66018 	if (vdc->io_stats != NULL) {
868366a92acSlm66018 		vdc->io_stats->ks_lock = &vdc->lock;
869366a92acSlm66018 		kstat_install(vdc->io_stats);
870366a92acSlm66018 	} else {
871366a92acSlm66018 		cmn_err(CE_NOTE, "[%d] Failed to create kstat: I/O statistics"
872366a92acSlm66018 		    " will not be gathered", vdc->instance);
873366a92acSlm66018 	}
874366a92acSlm66018 }
875366a92acSlm66018 
876366a92acSlm66018 static void
877366a92acSlm66018 vdc_create_err_kstats(vdc_t *vdc)
878366a92acSlm66018 {
879366a92acSlm66018 	vd_err_stats_t	*stp;
880366a92acSlm66018 	char	kstatmodule_err[KSTAT_STRLEN];
881366a92acSlm66018 	char	kstatname[KSTAT_STRLEN];
882366a92acSlm66018 	int	ndata = (sizeof (vd_err_stats_t) / sizeof (kstat_named_t));
883366a92acSlm66018 	int	instance = vdc->instance;
884366a92acSlm66018 
885366a92acSlm66018 	if (vdc->err_stats != NULL) {
886366a92acSlm66018 		DMSG(vdc, 0, "[%d] ERR kstat already exists\n", vdc->instance);
887366a92acSlm66018 		return;
888366a92acSlm66018 	}
889366a92acSlm66018 
890366a92acSlm66018 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
891366a92acSlm66018 	    "%serr", VDC_DRIVER_NAME);
892366a92acSlm66018 	(void) snprintf(kstatname, sizeof (kstatname),
893366a92acSlm66018 	    "%s%d,err", VDC_DRIVER_NAME, instance);
894366a92acSlm66018 
895366a92acSlm66018 	vdc->err_stats = kstat_create(kstatmodule_err, instance, kstatname,
896366a92acSlm66018 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
897366a92acSlm66018 
898366a92acSlm66018 	if (vdc->err_stats == NULL) {
899366a92acSlm66018 		cmn_err(CE_NOTE, "[%d] Failed to create kstat: Error statistics"
900366a92acSlm66018 		    " will not be gathered", instance);
901366a92acSlm66018 		return;
902366a92acSlm66018 	}
903366a92acSlm66018 
904366a92acSlm66018 	stp = (vd_err_stats_t *)vdc->err_stats->ks_data;
905366a92acSlm66018 	kstat_named_init(&stp->vd_softerrs,	"Soft Errors",
906366a92acSlm66018 	    KSTAT_DATA_UINT32);
907366a92acSlm66018 	kstat_named_init(&stp->vd_transerrs,	"Transport Errors",
908366a92acSlm66018 	    KSTAT_DATA_UINT32);
909366a92acSlm66018 	kstat_named_init(&stp->vd_protoerrs,	"Protocol Errors",
910366a92acSlm66018 	    KSTAT_DATA_UINT32);
911366a92acSlm66018 	kstat_named_init(&stp->vd_vid,		"Vendor",
912366a92acSlm66018 	    KSTAT_DATA_CHAR);
913366a92acSlm66018 	kstat_named_init(&stp->vd_pid,		"Product",
914366a92acSlm66018 	    KSTAT_DATA_CHAR);
915366a92acSlm66018 	kstat_named_init(&stp->vd_capacity,	"Size",
916366a92acSlm66018 	    KSTAT_DATA_ULONGLONG);
917366a92acSlm66018 
918366a92acSlm66018 	vdc->err_stats->ks_update  = nulldev;
919366a92acSlm66018 
920366a92acSlm66018 	kstat_install(vdc->err_stats);
921366a92acSlm66018 }
922366a92acSlm66018 
923366a92acSlm66018 static void
924366a92acSlm66018 vdc_set_err_kstats(vdc_t *vdc)
925366a92acSlm66018 {
926366a92acSlm66018 	vd_err_stats_t  *stp;
927366a92acSlm66018 
928366a92acSlm66018 	if (vdc->err_stats == NULL)
929366a92acSlm66018 		return;
930366a92acSlm66018 
931366a92acSlm66018 	mutex_enter(&vdc->lock);
932366a92acSlm66018 
933366a92acSlm66018 	stp = (vd_err_stats_t *)vdc->err_stats->ks_data;
934366a92acSlm66018 	ASSERT(stp != NULL);
935366a92acSlm66018 
936366a92acSlm66018 	stp->vd_capacity.value.ui64 = vdc->vdisk_size * vdc->block_size;
937366a92acSlm66018 	(void) strcpy(stp->vd_vid.value.c, "SUN");
938366a92acSlm66018 	(void) strcpy(stp->vd_pid.value.c, "VDSK");
939366a92acSlm66018 
940366a92acSlm66018 	mutex_exit(&vdc->lock);
941366a92acSlm66018 }
942366a92acSlm66018 
9434bac2208Snarayan static int
9444bac2208Snarayan vdc_create_device_nodes_efi(vdc_t *vdc)
9454bac2208Snarayan {
9464bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "h");
9474bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "h,raw");
9484bac2208Snarayan 
9494bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "wd", S_IFBLK,
9504bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
9514bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
9524bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd'",
9534bac2208Snarayan 		    vdc->instance);
9544bac2208Snarayan 		return (EIO);
9554bac2208Snarayan 	}
9564bac2208Snarayan 
9574bac2208Snarayan 	/* if any device node is created we set this flag */
9584bac2208Snarayan 	vdc->initialized |= VDC_MINOR;
9594bac2208Snarayan 
9604bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "wd,raw", S_IFCHR,
9614bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
9624bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
9634bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd,raw'",
9644bac2208Snarayan 		    vdc->instance);
9654bac2208Snarayan 		return (EIO);
9664bac2208Snarayan 	}
9674bac2208Snarayan 
9684bac2208Snarayan 	return (0);
9694bac2208Snarayan }
9704bac2208Snarayan 
9714bac2208Snarayan static int
9724bac2208Snarayan vdc_create_device_nodes_vtoc(vdc_t *vdc)
9734bac2208Snarayan {
9744bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "wd");
9754bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "wd,raw");
9764bac2208Snarayan 
9774bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "h", S_IFBLK,
9784bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
9794bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
9804bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h'",
9814bac2208Snarayan 		    vdc->instance);
9824bac2208Snarayan 		return (EIO);
9834bac2208Snarayan 	}
9844bac2208Snarayan 
9854bac2208Snarayan 	/* if any device node is created we set this flag */
9864bac2208Snarayan 	vdc->initialized |= VDC_MINOR;
9874bac2208Snarayan 
9884bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "h,raw", S_IFCHR,
9894bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
9904bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
9914bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h,raw'",
9924bac2208Snarayan 		    vdc->instance);
9934bac2208Snarayan 		return (EIO);
9944bac2208Snarayan 	}
9954bac2208Snarayan 
9964bac2208Snarayan 	return (0);
9974bac2208Snarayan }
9981ae08745Sheppo 
9991ae08745Sheppo /*
10001ae08745Sheppo  * Function:
10011ae08745Sheppo  *	vdc_create_device_nodes
10021ae08745Sheppo  *
10031ae08745Sheppo  * Description:
10041ae08745Sheppo  *	This function creates the block and character device nodes under
10055b98b509Sachartre  *	/devices. It is called as part of the attach(9E) of the instance
10065b98b509Sachartre  *	during the handshake with vds after vds has sent the attributes
10075b98b509Sachartre  *	to vdc.
10081ae08745Sheppo  *
10091ae08745Sheppo  *	If the device is of type VD_DISK_TYPE_SLICE then the minor node
10101ae08745Sheppo  *	of 2 is used in keeping with the Solaris convention that slice 2
10111ae08745Sheppo  *	refers to a whole disk. Slices start at 'a'
10121ae08745Sheppo  *
10131ae08745Sheppo  * Parameters:
10141ae08745Sheppo  *	vdc 		- soft state pointer
10151ae08745Sheppo  *
10161ae08745Sheppo  * Return Values
10171ae08745Sheppo  *	0		- Success
10181ae08745Sheppo  *	EIO		- Failed to create node
10191ae08745Sheppo  *	EINVAL		- Unknown type of disk exported
10201ae08745Sheppo  */
10211ae08745Sheppo static int
10221ae08745Sheppo vdc_create_device_nodes(vdc_t *vdc)
10231ae08745Sheppo {
10244bac2208Snarayan 	char		name[sizeof ("s,raw")];
10251ae08745Sheppo 	dev_info_t	*dip = NULL;
10264bac2208Snarayan 	int		instance, status;
10271ae08745Sheppo 	int		num_slices = 1;
10281ae08745Sheppo 	int		i;
10291ae08745Sheppo 
10301ae08745Sheppo 	ASSERT(vdc != NULL);
10311ae08745Sheppo 
10321ae08745Sheppo 	instance = vdc->instance;
10331ae08745Sheppo 	dip = vdc->dip;
10341ae08745Sheppo 
10351ae08745Sheppo 	switch (vdc->vdisk_type) {
10361ae08745Sheppo 	case VD_DISK_TYPE_DISK:
10371ae08745Sheppo 		num_slices = V_NUMPAR;
10381ae08745Sheppo 		break;
10391ae08745Sheppo 	case VD_DISK_TYPE_SLICE:
10401ae08745Sheppo 		num_slices = 1;
10411ae08745Sheppo 		break;
10421ae08745Sheppo 	case VD_DISK_TYPE_UNK:
10431ae08745Sheppo 	default:
10441ae08745Sheppo 		return (EINVAL);
10451ae08745Sheppo 	}
10461ae08745Sheppo 
10474bac2208Snarayan 	/*
10484bac2208Snarayan 	 * Minor nodes are different for EFI disks: EFI disks do not have
10494bac2208Snarayan 	 * a minor node 'g' for the minor number corresponding to slice
10504bac2208Snarayan 	 * VD_EFI_WD_SLICE (slice 7) instead they have a minor node 'wd'
10514bac2208Snarayan 	 * representing the whole disk.
10524bac2208Snarayan 	 */
10531ae08745Sheppo 	for (i = 0; i < num_slices; i++) {
10544bac2208Snarayan 
10554bac2208Snarayan 		if (i == VD_EFI_WD_SLICE) {
10564bac2208Snarayan 			if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
10574bac2208Snarayan 				status = vdc_create_device_nodes_efi(vdc);
10584bac2208Snarayan 			else
10594bac2208Snarayan 				status = vdc_create_device_nodes_vtoc(vdc);
10604bac2208Snarayan 			if (status != 0)
10614bac2208Snarayan 				return (status);
10624bac2208Snarayan 			continue;
10634bac2208Snarayan 		}
10644bac2208Snarayan 
10651ae08745Sheppo 		(void) snprintf(name, sizeof (name), "%c", 'a' + i);
10661ae08745Sheppo 		if (ddi_create_minor_node(dip, name, S_IFBLK,
10671ae08745Sheppo 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
1068e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add block node '%s'",
1069e1ebb9ecSlm66018 			    instance, name);
10701ae08745Sheppo 			return (EIO);
10711ae08745Sheppo 		}
10721ae08745Sheppo 
10731ae08745Sheppo 		/* if any device node is created we set this flag */
10741ae08745Sheppo 		vdc->initialized |= VDC_MINOR;
10751ae08745Sheppo 
107687a7269eSachartre 		(void) snprintf(name, sizeof (name), "%c%s", 'a' + i, ",raw");
107787a7269eSachartre 
10781ae08745Sheppo 		if (ddi_create_minor_node(dip, name, S_IFCHR,
10791ae08745Sheppo 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
1080e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add raw node '%s'",
1081e1ebb9ecSlm66018 			    instance, name);
10821ae08745Sheppo 			return (EIO);
10831ae08745Sheppo 		}
10841ae08745Sheppo 	}
10851ae08745Sheppo 
10861ae08745Sheppo 	return (0);
10871ae08745Sheppo }
10881ae08745Sheppo 
10891ae08745Sheppo /*
10905b98b509Sachartre  * Driver prop_op(9e) entry point function. Return the number of blocks for
10915b98b509Sachartre  * the partition in question or forward the request to the property facilities.
10921ae08745Sheppo  */
10931ae08745Sheppo static int
10945b98b509Sachartre vdc_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
10955b98b509Sachartre     char *name, caddr_t valuep, int *lengthp)
10961ae08745Sheppo {
10975b98b509Sachartre 	int instance = ddi_get_instance(dip);
10985b98b509Sachartre 	vdc_t *vdc;
10995b98b509Sachartre 	uint64_t nblocks;
11005b98b509Sachartre 	uint_t blksize;
11011ae08745Sheppo 
11025b98b509Sachartre 	vdc = ddi_get_soft_state(vdc_state, instance);
11031ae08745Sheppo 
11045b98b509Sachartre 	if (dev == DDI_DEV_T_ANY || vdc == NULL) {
11055b98b509Sachartre 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
11065b98b509Sachartre 		    name, valuep, lengthp));
11071ae08745Sheppo 	}
11081ae08745Sheppo 
11095b98b509Sachartre 	mutex_enter(&vdc->lock);
11105b98b509Sachartre 	(void) vdc_validate_geometry(vdc);
111178fcd0a1Sachartre 	if (vdc->vdisk_label == VD_DISK_LABEL_UNK) {
11125b98b509Sachartre 		mutex_exit(&vdc->lock);
11135b98b509Sachartre 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
11145b98b509Sachartre 		    name, valuep, lengthp));
111578fcd0a1Sachartre 	}
11165b98b509Sachartre 	nblocks = vdc->slice[VDCPART(dev)].nblocks;
11175b98b509Sachartre 	blksize = vdc->block_size;
11185b98b509Sachartre 	mutex_exit(&vdc->lock);
111978fcd0a1Sachartre 
11205b98b509Sachartre 	return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op, mod_flags,
11215b98b509Sachartre 	    name, valuep, lengthp, nblocks, blksize));
11221ae08745Sheppo }
11231ae08745Sheppo 
112478fcd0a1Sachartre /*
112578fcd0a1Sachartre  * Function:
112678fcd0a1Sachartre  *	vdc_is_opened
112778fcd0a1Sachartre  *
112878fcd0a1Sachartre  * Description:
112978fcd0a1Sachartre  *	This function checks if any slice of a given virtual disk is
113078fcd0a1Sachartre  *	currently opened.
113178fcd0a1Sachartre  *
113278fcd0a1Sachartre  * Parameters:
113378fcd0a1Sachartre  *	vdc 		- soft state pointer
113478fcd0a1Sachartre  *
113578fcd0a1Sachartre  * Return Values
113678fcd0a1Sachartre  *	B_TRUE		- at least one slice is opened.
113778fcd0a1Sachartre  *	B_FALSE		- no slice is opened.
113878fcd0a1Sachartre  */
113978fcd0a1Sachartre static boolean_t
114078fcd0a1Sachartre vdc_is_opened(vdc_t *vdc)
114178fcd0a1Sachartre {
114278fcd0a1Sachartre 	int i, nslices;
114378fcd0a1Sachartre 
114478fcd0a1Sachartre 	switch (vdc->vdisk_type) {
114578fcd0a1Sachartre 	case VD_DISK_TYPE_DISK:
114678fcd0a1Sachartre 		nslices = V_NUMPAR;
114778fcd0a1Sachartre 		break;
114878fcd0a1Sachartre 	case VD_DISK_TYPE_SLICE:
114978fcd0a1Sachartre 		nslices = 1;
115078fcd0a1Sachartre 		break;
115178fcd0a1Sachartre 	case VD_DISK_TYPE_UNK:
115278fcd0a1Sachartre 	default:
115378fcd0a1Sachartre 		ASSERT(0);
115478fcd0a1Sachartre 	}
115578fcd0a1Sachartre 
115678fcd0a1Sachartre 	/* check if there's any layered open */
115778fcd0a1Sachartre 	for (i = 0; i < nslices; i++) {
115878fcd0a1Sachartre 		if (vdc->open_lyr[i] > 0)
115978fcd0a1Sachartre 			return (B_TRUE);
116078fcd0a1Sachartre 	}
116178fcd0a1Sachartre 
116278fcd0a1Sachartre 	/* check if there is any other kind of open */
116378fcd0a1Sachartre 	for (i = 0; i < OTYPCNT; i++) {
116478fcd0a1Sachartre 		if (vdc->open[i] != 0)
116578fcd0a1Sachartre 			return (B_TRUE);
116678fcd0a1Sachartre 	}
116778fcd0a1Sachartre 
116878fcd0a1Sachartre 	return (B_FALSE);
116978fcd0a1Sachartre }
117078fcd0a1Sachartre 
117178fcd0a1Sachartre static int
117278fcd0a1Sachartre vdc_mark_opened(vdc_t *vdc, int slice, int flag, int otyp)
117378fcd0a1Sachartre {
117478fcd0a1Sachartre 	uint8_t slicemask;
117578fcd0a1Sachartre 	int i;
117678fcd0a1Sachartre 
117778fcd0a1Sachartre 	ASSERT(otyp < OTYPCNT);
117878fcd0a1Sachartre 	ASSERT(slice < V_NUMPAR);
117978fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
118078fcd0a1Sachartre 
118178fcd0a1Sachartre 	slicemask = 1 << slice;
118278fcd0a1Sachartre 
118378fcd0a1Sachartre 	/* check if slice is already exclusively opened */
118478fcd0a1Sachartre 	if (vdc->open_excl & slicemask)
118578fcd0a1Sachartre 		return (EBUSY);
118678fcd0a1Sachartre 
118778fcd0a1Sachartre 	/* if open exclusive, check if slice is already opened */
118878fcd0a1Sachartre 	if (flag & FEXCL) {
118978fcd0a1Sachartre 		if (vdc->open_lyr[slice] > 0)
119078fcd0a1Sachartre 			return (EBUSY);
119178fcd0a1Sachartre 		for (i = 0; i < OTYPCNT; i++) {
119278fcd0a1Sachartre 			if (vdc->open[i] & slicemask)
119378fcd0a1Sachartre 				return (EBUSY);
119478fcd0a1Sachartre 		}
119578fcd0a1Sachartre 		vdc->open_excl |= slicemask;
119678fcd0a1Sachartre 	}
119778fcd0a1Sachartre 
119878fcd0a1Sachartre 	/* mark slice as opened */
119978fcd0a1Sachartre 	if (otyp == OTYP_LYR) {
120078fcd0a1Sachartre 		vdc->open_lyr[slice]++;
120178fcd0a1Sachartre 	} else {
120278fcd0a1Sachartre 		vdc->open[otyp] |= slicemask;
120378fcd0a1Sachartre 	}
120478fcd0a1Sachartre 
120578fcd0a1Sachartre 	return (0);
120678fcd0a1Sachartre }
120778fcd0a1Sachartre 
120878fcd0a1Sachartre static void
120978fcd0a1Sachartre vdc_mark_closed(vdc_t *vdc, int slice, int flag, int otyp)
121078fcd0a1Sachartre {
121178fcd0a1Sachartre 	uint8_t slicemask;
121278fcd0a1Sachartre 
121378fcd0a1Sachartre 	ASSERT(otyp < OTYPCNT);
121478fcd0a1Sachartre 	ASSERT(slice < V_NUMPAR);
121578fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
121678fcd0a1Sachartre 
121778fcd0a1Sachartre 	slicemask = 1 << slice;
121878fcd0a1Sachartre 
121978fcd0a1Sachartre 	if (otyp == OTYP_LYR) {
122078fcd0a1Sachartre 		ASSERT(vdc->open_lyr[slice] > 0);
122178fcd0a1Sachartre 		vdc->open_lyr[slice]--;
122278fcd0a1Sachartre 	} else {
122378fcd0a1Sachartre 		vdc->open[otyp] &= ~slicemask;
122478fcd0a1Sachartre 	}
122578fcd0a1Sachartre 
122678fcd0a1Sachartre 	if (flag & FEXCL)
122778fcd0a1Sachartre 		vdc->open_excl &= ~slicemask;
122878fcd0a1Sachartre }
122978fcd0a1Sachartre 
12301ae08745Sheppo static int
12311ae08745Sheppo vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred)
12321ae08745Sheppo {
12331ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
12341ae08745Sheppo 
1235179e09c2Sachartre 	int	instance, nodelay;
123678fcd0a1Sachartre 	int	slice, status = 0;
12371ae08745Sheppo 	vdc_t	*vdc;
12381ae08745Sheppo 
12391ae08745Sheppo 	ASSERT(dev != NULL);
12400d0c8d4bSnarayan 	instance = VDCUNIT(*dev);
12411ae08745Sheppo 
124278fcd0a1Sachartre 	if (otyp >= OTYPCNT)
12431ae08745Sheppo 		return (EINVAL);
12441ae08745Sheppo 
12451ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1246e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
12471ae08745Sheppo 		return (ENXIO);
12481ae08745Sheppo 	}
12491ae08745Sheppo 
12503af08d82Slm66018 	DMSG(vdc, 0, "minor = %d flag = %x, otyp = %x\n",
12513af08d82Slm66018 	    getminor(*dev), flag, otyp);
12521ae08745Sheppo 
125378fcd0a1Sachartre 	slice = VDCPART(*dev);
125478fcd0a1Sachartre 
1255179e09c2Sachartre 	nodelay = flag & (FNDELAY | FNONBLOCK);
1256179e09c2Sachartre 
1257179e09c2Sachartre 	if ((flag & FWRITE) && (!nodelay) &&
1258179e09c2Sachartre 	    !(VD_OP_SUPPORTED(vdc->operations, VD_OP_BWRITE))) {
1259179e09c2Sachartre 		return (EROFS);
1260179e09c2Sachartre 	}
1261179e09c2Sachartre 
12621ae08745Sheppo 	mutex_enter(&vdc->lock);
126378fcd0a1Sachartre 
126478fcd0a1Sachartre 	status = vdc_mark_opened(vdc, slice, flag, otyp);
126578fcd0a1Sachartre 
126678fcd0a1Sachartre 	if (status != 0) {
126778fcd0a1Sachartre 		mutex_exit(&vdc->lock);
126878fcd0a1Sachartre 		return (status);
126978fcd0a1Sachartre 	}
127078fcd0a1Sachartre 
1271179e09c2Sachartre 	if (nodelay) {
127278fcd0a1Sachartre 
127378fcd0a1Sachartre 		/* don't resubmit a validate request if there's already one */
127478fcd0a1Sachartre 		if (vdc->validate_pending > 0) {
127578fcd0a1Sachartre 			mutex_exit(&vdc->lock);
127678fcd0a1Sachartre 			return (0);
127778fcd0a1Sachartre 		}
127878fcd0a1Sachartre 
127978fcd0a1Sachartre 		/* call vdc_validate() asynchronously to avoid blocking */
128078fcd0a1Sachartre 		if (taskq_dispatch(system_taskq, vdc_validate_task,
128178fcd0a1Sachartre 		    (void *)vdc, TQ_NOSLEEP) == NULL) {
128278fcd0a1Sachartre 			vdc_mark_closed(vdc, slice, flag, otyp);
128378fcd0a1Sachartre 			mutex_exit(&vdc->lock);
128478fcd0a1Sachartre 			return (ENXIO);
128578fcd0a1Sachartre 		}
128678fcd0a1Sachartre 
128778fcd0a1Sachartre 		vdc->validate_pending++;
128878fcd0a1Sachartre 		mutex_exit(&vdc->lock);
128978fcd0a1Sachartre 		return (0);
129078fcd0a1Sachartre 	}
129178fcd0a1Sachartre 
12921ae08745Sheppo 	mutex_exit(&vdc->lock);
12931ae08745Sheppo 
129478fcd0a1Sachartre 	vdc_validate(vdc);
129578fcd0a1Sachartre 
129678fcd0a1Sachartre 	mutex_enter(&vdc->lock);
129778fcd0a1Sachartre 
129878fcd0a1Sachartre 	if (vdc->vdisk_label == VD_DISK_LABEL_UNK ||
1299edcc0754Sachartre 	    vdc->slice[slice].nblocks == 0) {
130078fcd0a1Sachartre 		vdc_mark_closed(vdc, slice, flag, otyp);
130178fcd0a1Sachartre 		status = EIO;
130278fcd0a1Sachartre 	}
130378fcd0a1Sachartre 
130478fcd0a1Sachartre 	mutex_exit(&vdc->lock);
130578fcd0a1Sachartre 
130678fcd0a1Sachartre 	return (status);
13071ae08745Sheppo }
13081ae08745Sheppo 
13091ae08745Sheppo static int
13101ae08745Sheppo vdc_close(dev_t dev, int flag, int otyp, cred_t *cred)
13111ae08745Sheppo {
13121ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
13131ae08745Sheppo 
13141ae08745Sheppo 	int	instance;
131578fcd0a1Sachartre 	int	slice;
13162f5224aeSachartre 	int	rv, rval;
13171ae08745Sheppo 	vdc_t	*vdc;
13181ae08745Sheppo 
13190d0c8d4bSnarayan 	instance = VDCUNIT(dev);
13201ae08745Sheppo 
132178fcd0a1Sachartre 	if (otyp >= OTYPCNT)
13221ae08745Sheppo 		return (EINVAL);
13231ae08745Sheppo 
13241ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1325e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
13261ae08745Sheppo 		return (ENXIO);
13271ae08745Sheppo 	}
13281ae08745Sheppo 
13293af08d82Slm66018 	DMSG(vdc, 0, "[%d] flag = %x, otyp = %x\n", instance, flag, otyp);
13301ae08745Sheppo 
133178fcd0a1Sachartre 	slice = VDCPART(dev);
133278fcd0a1Sachartre 
13338259acd8Szk194757 	/*
13348259acd8Szk194757 	 * Attempt to flush the W$ on a close operation. If this is
13358259acd8Szk194757 	 * not a supported IOCTL command or the backing device is read-only
13368259acd8Szk194757 	 * do not fail the close operation.
13378259acd8Szk194757 	 */
13382f5224aeSachartre 	rv = vd_process_ioctl(dev, DKIOCFLUSHWRITECACHE, NULL, FKIOCTL, &rval);
13398259acd8Szk194757 
13408259acd8Szk194757 	if (rv != 0 && rv != ENOTSUP && rv != ENOTTY && rv != EROFS) {
13418259acd8Szk194757 		DMSG(vdc, 0, "[%d] flush failed with error %d on close\n",
13428259acd8Szk194757 		    instance, rv);
13438259acd8Szk194757 		return (EIO);
13448259acd8Szk194757 	}
13458259acd8Szk194757 
13461ae08745Sheppo 	mutex_enter(&vdc->lock);
134778fcd0a1Sachartre 	vdc_mark_closed(vdc, slice, flag, otyp);
13481ae08745Sheppo 	mutex_exit(&vdc->lock);
13491ae08745Sheppo 
13501ae08745Sheppo 	return (0);
13511ae08745Sheppo }
13521ae08745Sheppo 
13531ae08745Sheppo static int
13541ae08745Sheppo vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
13551ae08745Sheppo {
13561ae08745Sheppo 	_NOTE(ARGUNUSED(credp))
13571ae08745Sheppo 
13582f5224aeSachartre 	return (vd_process_ioctl(dev, cmd, (caddr_t)arg, mode, rvalp));
13591ae08745Sheppo }
13601ae08745Sheppo 
13611ae08745Sheppo static int
13621ae08745Sheppo vdc_print(dev_t dev, char *str)
13631ae08745Sheppo {
13640d0c8d4bSnarayan 	cmn_err(CE_NOTE, "vdc%d:  %s", VDCUNIT(dev), str);
13651ae08745Sheppo 	return (0);
13661ae08745Sheppo }
13671ae08745Sheppo 
13681ae08745Sheppo static int
13691ae08745Sheppo vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
13701ae08745Sheppo {
1371d10e4ef2Snarayan 	int	rv;
1372d10e4ef2Snarayan 	size_t	nbytes = nblk * DEV_BSIZE;
13730d0c8d4bSnarayan 	int	instance = VDCUNIT(dev);
1374d10e4ef2Snarayan 	vdc_t	*vdc = NULL;
13751ae08745Sheppo 
13761ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1377e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
13781ae08745Sheppo 		return (ENXIO);
13791ae08745Sheppo 	}
13801ae08745Sheppo 
13813af08d82Slm66018 	DMSG(vdc, 2, "[%d] dump %ld bytes at block 0x%lx : addr=0x%p\n",
13823af08d82Slm66018 	    instance, nbytes, blkno, (void *)addr);
13833af08d82Slm66018 	rv = vdc_send_request(vdc, VD_OP_BWRITE, addr, nbytes,
13840d0c8d4bSnarayan 	    VDCPART(dev), blkno, CB_STRATEGY, 0, VIO_write_dir);
13853af08d82Slm66018 	if (rv) {
13863af08d82Slm66018 		DMSG(vdc, 0, "Failed to do a disk dump (err=%d)\n", rv);
13871ae08745Sheppo 		return (rv);
13881ae08745Sheppo 	}
13891ae08745Sheppo 
13903af08d82Slm66018 	if (ddi_in_panic())
13913af08d82Slm66018 		(void) vdc_drain_response(vdc);
13923af08d82Slm66018 
13933af08d82Slm66018 	DMSG(vdc, 0, "[%d] End\n", instance);
13943af08d82Slm66018 
13953af08d82Slm66018 	return (0);
13963af08d82Slm66018 }
13973af08d82Slm66018 
13981ae08745Sheppo /* -------------------------------------------------------------------------- */
13991ae08745Sheppo 
14001ae08745Sheppo /*
14011ae08745Sheppo  * Disk access routines
14021ae08745Sheppo  *
14031ae08745Sheppo  */
14041ae08745Sheppo 
14051ae08745Sheppo /*
14061ae08745Sheppo  * vdc_strategy()
14071ae08745Sheppo  *
14081ae08745Sheppo  * Return Value:
14091ae08745Sheppo  *	0:	As per strategy(9E), the strategy() function must return 0
14101ae08745Sheppo  *		[ bioerror(9f) sets b_flags to the proper error code ]
14111ae08745Sheppo  */
14121ae08745Sheppo static int
14131ae08745Sheppo vdc_strategy(struct buf *buf)
14141ae08745Sheppo {
14151ae08745Sheppo 	int	rv = -1;
14161ae08745Sheppo 	vdc_t	*vdc = NULL;
14170d0c8d4bSnarayan 	int	instance = VDCUNIT(buf->b_edev);
14181ae08745Sheppo 	int	op = (buf->b_flags & B_READ) ? VD_OP_BREAD : VD_OP_BWRITE;
141987a7269eSachartre 	int	slice;
14201ae08745Sheppo 
14211ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1422e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
14231ae08745Sheppo 		bioerror(buf, ENXIO);
14241ae08745Sheppo 		biodone(buf);
14251ae08745Sheppo 		return (0);
14261ae08745Sheppo 	}
14271ae08745Sheppo 
14283af08d82Slm66018 	DMSG(vdc, 2, "[%d] %s %ld bytes at block %llx : b_addr=0x%p\n",
14293af08d82Slm66018 	    instance, (buf->b_flags & B_READ) ? "Read" : "Write",
14303af08d82Slm66018 	    buf->b_bcount, buf->b_lblkno, (void *)buf->b_un.b_addr);
1431d10e4ef2Snarayan 
14321ae08745Sheppo 	bp_mapin(buf);
14331ae08745Sheppo 
143487a7269eSachartre 	if ((long)buf->b_private == VD_SLICE_NONE) {
143587a7269eSachartre 		/* I/O using an absolute disk offset */
143687a7269eSachartre 		slice = VD_SLICE_NONE;
143787a7269eSachartre 	} else {
143887a7269eSachartre 		slice = VDCPART(buf->b_edev);
143987a7269eSachartre 	}
144087a7269eSachartre 
14413af08d82Slm66018 	rv = vdc_send_request(vdc, op, (caddr_t)buf->b_un.b_addr,
144287a7269eSachartre 	    buf->b_bcount, slice, buf->b_lblkno,
14433af08d82Slm66018 	    CB_STRATEGY, buf, (op == VD_OP_BREAD) ? VIO_read_dir :
14443af08d82Slm66018 	    VIO_write_dir);
14453af08d82Slm66018 
1446d10e4ef2Snarayan 	/*
1447d10e4ef2Snarayan 	 * If the request was successfully sent, the strategy call returns and
1448d10e4ef2Snarayan 	 * the ACK handler calls the bioxxx functions when the vDisk server is
1449366a92acSlm66018 	 * done otherwise we handle the error here.
1450d10e4ef2Snarayan 	 */
1451d10e4ef2Snarayan 	if (rv) {
14523af08d82Slm66018 		DMSG(vdc, 0, "Failed to read/write (err=%d)\n", rv);
14531ae08745Sheppo 		bioerror(buf, rv);
14541ae08745Sheppo 		biodone(buf);
1455d10e4ef2Snarayan 	}
1456d10e4ef2Snarayan 
14571ae08745Sheppo 	return (0);
14581ae08745Sheppo }
14591ae08745Sheppo 
14600d0c8d4bSnarayan /*
14610d0c8d4bSnarayan  * Function:
14620d0c8d4bSnarayan  *	vdc_min
14630d0c8d4bSnarayan  *
14640d0c8d4bSnarayan  * Description:
14650d0c8d4bSnarayan  *	Routine to limit the size of a data transfer. Used in
14660d0c8d4bSnarayan  *	conjunction with physio(9F).
14670d0c8d4bSnarayan  *
14680d0c8d4bSnarayan  * Arguments:
14690d0c8d4bSnarayan  *	bp - pointer to the indicated buf(9S) struct.
14700d0c8d4bSnarayan  *
14710d0c8d4bSnarayan  */
14720d0c8d4bSnarayan static void
14730d0c8d4bSnarayan vdc_min(struct buf *bufp)
14740d0c8d4bSnarayan {
14750d0c8d4bSnarayan 	vdc_t	*vdc = NULL;
14760d0c8d4bSnarayan 	int	instance = VDCUNIT(bufp->b_edev);
14770d0c8d4bSnarayan 
14780d0c8d4bSnarayan 	vdc = ddi_get_soft_state(vdc_state, instance);
14790d0c8d4bSnarayan 	VERIFY(vdc != NULL);
14800d0c8d4bSnarayan 
14810d0c8d4bSnarayan 	if (bufp->b_bcount > (vdc->max_xfer_sz * vdc->block_size)) {
14820d0c8d4bSnarayan 		bufp->b_bcount = vdc->max_xfer_sz * vdc->block_size;
14830d0c8d4bSnarayan 	}
14840d0c8d4bSnarayan }
14851ae08745Sheppo 
14861ae08745Sheppo static int
14871ae08745Sheppo vdc_read(dev_t dev, struct uio *uio, cred_t *cred)
14881ae08745Sheppo {
14891ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
14901ae08745Sheppo 
14910d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
14920d0c8d4bSnarayan 	return (physio(vdc_strategy, NULL, dev, B_READ, vdc_min, uio));
14931ae08745Sheppo }
14941ae08745Sheppo 
14951ae08745Sheppo static int
14961ae08745Sheppo vdc_write(dev_t dev, struct uio *uio, cred_t *cred)
14971ae08745Sheppo {
14981ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
14991ae08745Sheppo 
15000d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
15010d0c8d4bSnarayan 	return (physio(vdc_strategy, NULL, dev, B_WRITE, vdc_min, uio));
15021ae08745Sheppo }
15031ae08745Sheppo 
15041ae08745Sheppo static int
15051ae08745Sheppo vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred)
15061ae08745Sheppo {
15071ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
15081ae08745Sheppo 
15090d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
15100d0c8d4bSnarayan 	return (aphysio(vdc_strategy, anocancel, dev, B_READ, vdc_min, aio));
15111ae08745Sheppo }
15121ae08745Sheppo 
15131ae08745Sheppo static int
15141ae08745Sheppo vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred)
15151ae08745Sheppo {
15161ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
15171ae08745Sheppo 
15180d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
15190d0c8d4bSnarayan 	return (aphysio(vdc_strategy, anocancel, dev, B_WRITE, vdc_min, aio));
15201ae08745Sheppo }
15211ae08745Sheppo 
15221ae08745Sheppo 
15231ae08745Sheppo /* -------------------------------------------------------------------------- */
15241ae08745Sheppo 
15251ae08745Sheppo /*
15261ae08745Sheppo  * Handshake support
15271ae08745Sheppo  */
15281ae08745Sheppo 
15291ae08745Sheppo 
15300a55fbb7Slm66018 /*
15310a55fbb7Slm66018  * Function:
15320a55fbb7Slm66018  *	vdc_init_ver_negotiation()
15330a55fbb7Slm66018  *
15340a55fbb7Slm66018  * Description:
15350a55fbb7Slm66018  *
15360a55fbb7Slm66018  * Arguments:
15370a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
15380a55fbb7Slm66018  *
15390a55fbb7Slm66018  * Return Code:
15400a55fbb7Slm66018  *	0	- Success
15410a55fbb7Slm66018  */
15421ae08745Sheppo static int
15430a55fbb7Slm66018 vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver)
15441ae08745Sheppo {
15451ae08745Sheppo 	vio_ver_msg_t	pkt;
15461ae08745Sheppo 	size_t		msglen = sizeof (pkt);
15471ae08745Sheppo 	int		status = -1;
15481ae08745Sheppo 
15491ae08745Sheppo 	ASSERT(vdc != NULL);
15501ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
15511ae08745Sheppo 
15523af08d82Slm66018 	DMSG(vdc, 0, "[%d] Entered.\n", vdc->instance);
1553e1ebb9ecSlm66018 
15541ae08745Sheppo 	/*
15551ae08745Sheppo 	 * set the Session ID to a unique value
15561ae08745Sheppo 	 * (the lower 32 bits of the clock tick)
15571ae08745Sheppo 	 */
15581ae08745Sheppo 	vdc->session_id = ((uint32_t)gettick() & 0xffffffff);
15593af08d82Slm66018 	DMSG(vdc, 0, "[%d] Set SID to 0x%lx\n", vdc->instance, vdc->session_id);
15601ae08745Sheppo 
15611ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
15621ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
15631ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_VER_INFO;
15641ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
15651ae08745Sheppo 	pkt.dev_class = VDEV_DISK;
15660a55fbb7Slm66018 	pkt.ver_major = ver.major;
15670a55fbb7Slm66018 	pkt.ver_minor = ver.minor;
15681ae08745Sheppo 
15690a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
15703af08d82Slm66018 	DMSG(vdc, 0, "[%d] Ver info sent (status = %d)\n",
15713af08d82Slm66018 	    vdc->instance, status);
15721ae08745Sheppo 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
15733af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to send Ver negotiation info: "
15748cd10891Snarayan 		    "id(%lx) rv(%d) size(%ld)", vdc->instance,
15758cd10891Snarayan 		    vdc->curr_server->ldc_handle, status, msglen);
15761ae08745Sheppo 		if (msglen != sizeof (vio_ver_msg_t))
15771ae08745Sheppo 			status = ENOMSG;
15781ae08745Sheppo 	}
15791ae08745Sheppo 
15801ae08745Sheppo 	return (status);
15811ae08745Sheppo }
15821ae08745Sheppo 
15830a55fbb7Slm66018 /*
15840a55fbb7Slm66018  * Function:
15853af08d82Slm66018  *	vdc_ver_negotiation()
15863af08d82Slm66018  *
15873af08d82Slm66018  * Description:
15883af08d82Slm66018  *
15893af08d82Slm66018  * Arguments:
15903af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
15913af08d82Slm66018  *
15923af08d82Slm66018  * Return Code:
15933af08d82Slm66018  *	0	- Success
15943af08d82Slm66018  */
15953af08d82Slm66018 static int
15963af08d82Slm66018 vdc_ver_negotiation(vdc_t *vdcp)
15973af08d82Slm66018 {
15983af08d82Slm66018 	vio_msg_t vio_msg;
15993af08d82Slm66018 	int status;
16003af08d82Slm66018 
16013af08d82Slm66018 	if (status = vdc_init_ver_negotiation(vdcp, vdc_version[0]))
16023af08d82Slm66018 		return (status);
16033af08d82Slm66018 
16043af08d82Slm66018 	/* release lock and wait for response */
16053af08d82Slm66018 	mutex_exit(&vdcp->lock);
16063af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
16073af08d82Slm66018 	mutex_enter(&vdcp->lock);
16083af08d82Slm66018 	if (status) {
16093af08d82Slm66018 		DMSG(vdcp, 0,
16103af08d82Slm66018 		    "[%d] Failed waiting for Ver negotiation response, rv(%d)",
16113af08d82Slm66018 		    vdcp->instance, status);
16123af08d82Slm66018 		return (status);
16133af08d82Slm66018 	}
16143af08d82Slm66018 
16153af08d82Slm66018 	/* check type and sub_type ... */
16163af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
16173af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
16183af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid ver negotiation response\n",
16193af08d82Slm66018 		    vdcp->instance);
16203af08d82Slm66018 		return (EPROTO);
16213af08d82Slm66018 	}
16223af08d82Slm66018 
16233af08d82Slm66018 	return (vdc_handle_ver_msg(vdcp, (vio_ver_msg_t *)&vio_msg));
16243af08d82Slm66018 }
16253af08d82Slm66018 
16263af08d82Slm66018 /*
16273af08d82Slm66018  * Function:
16280a55fbb7Slm66018  *	vdc_init_attr_negotiation()
16290a55fbb7Slm66018  *
16300a55fbb7Slm66018  * Description:
16310a55fbb7Slm66018  *
16320a55fbb7Slm66018  * Arguments:
16330a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
16340a55fbb7Slm66018  *
16350a55fbb7Slm66018  * Return Code:
16360a55fbb7Slm66018  *	0	- Success
16370a55fbb7Slm66018  */
16381ae08745Sheppo static int
16391ae08745Sheppo vdc_init_attr_negotiation(vdc_t *vdc)
16401ae08745Sheppo {
16411ae08745Sheppo 	vd_attr_msg_t	pkt;
16421ae08745Sheppo 	size_t		msglen = sizeof (pkt);
16431ae08745Sheppo 	int		status;
16441ae08745Sheppo 
16451ae08745Sheppo 	ASSERT(vdc != NULL);
16461ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
16471ae08745Sheppo 
16483af08d82Slm66018 	DMSG(vdc, 0, "[%d] entered\n", vdc->instance);
16491ae08745Sheppo 
16501ae08745Sheppo 	/* fill in tag */
16511ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
16521ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
16531ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_ATTR_INFO;
16541ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
16551ae08745Sheppo 	/* fill in payload */
16561ae08745Sheppo 	pkt.max_xfer_sz = vdc->max_xfer_sz;
16571ae08745Sheppo 	pkt.vdisk_block_size = vdc->block_size;
1658f0ca1d9aSsb155480 	pkt.xfer_mode = VIO_DRING_MODE_V1_0;
16591ae08745Sheppo 	pkt.operations = 0;	/* server will set bits of valid operations */
16601ae08745Sheppo 	pkt.vdisk_type = 0;	/* server will set to valid device type */
166117cadca8Slm66018 	pkt.vdisk_media = 0;	/* server will set to valid media type */
16621ae08745Sheppo 	pkt.vdisk_size = 0;	/* server will set to valid size */
16631ae08745Sheppo 
16640a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
16653af08d82Slm66018 	DMSG(vdc, 0, "Attr info sent (status = %d)\n", status);
16661ae08745Sheppo 
16671ae08745Sheppo 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
16683af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to send Attr negotiation info: "
16698cd10891Snarayan 		    "id(%lx) rv(%d) size(%ld)", vdc->instance,
16708cd10891Snarayan 		    vdc->curr_server->ldc_handle, status, msglen);
16711ae08745Sheppo 		if (msglen != sizeof (vio_ver_msg_t))
16721ae08745Sheppo 			status = ENOMSG;
16731ae08745Sheppo 	}
16741ae08745Sheppo 
16751ae08745Sheppo 	return (status);
16761ae08745Sheppo }
16771ae08745Sheppo 
16780a55fbb7Slm66018 /*
16790a55fbb7Slm66018  * Function:
16803af08d82Slm66018  *	vdc_attr_negotiation()
16813af08d82Slm66018  *
16823af08d82Slm66018  * Description:
16833af08d82Slm66018  *
16843af08d82Slm66018  * Arguments:
16853af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
16863af08d82Slm66018  *
16873af08d82Slm66018  * Return Code:
16883af08d82Slm66018  *	0	- Success
16893af08d82Slm66018  */
16903af08d82Slm66018 static int
16913af08d82Slm66018 vdc_attr_negotiation(vdc_t *vdcp)
16923af08d82Slm66018 {
16933af08d82Slm66018 	int status;
16943af08d82Slm66018 	vio_msg_t vio_msg;
16953af08d82Slm66018 
16963af08d82Slm66018 	if (status = vdc_init_attr_negotiation(vdcp))
16973af08d82Slm66018 		return (status);
16983af08d82Slm66018 
16993af08d82Slm66018 	/* release lock and wait for response */
17003af08d82Slm66018 	mutex_exit(&vdcp->lock);
17013af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
17023af08d82Slm66018 	mutex_enter(&vdcp->lock);
17033af08d82Slm66018 	if (status) {
17043af08d82Slm66018 		DMSG(vdcp, 0,
17053af08d82Slm66018 		    "[%d] Failed waiting for Attr negotiation response, rv(%d)",
17063af08d82Slm66018 		    vdcp->instance, status);
17073af08d82Slm66018 		return (status);
17083af08d82Slm66018 	}
17093af08d82Slm66018 
17103af08d82Slm66018 	/* check type and sub_type ... */
17113af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
17123af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
17133af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid attr negotiation response\n",
17143af08d82Slm66018 		    vdcp->instance);
17153af08d82Slm66018 		return (EPROTO);
17163af08d82Slm66018 	}
17173af08d82Slm66018 
17183af08d82Slm66018 	return (vdc_handle_attr_msg(vdcp, (vd_attr_msg_t *)&vio_msg));
17193af08d82Slm66018 }
17203af08d82Slm66018 
17213af08d82Slm66018 
17223af08d82Slm66018 /*
17233af08d82Slm66018  * Function:
17240a55fbb7Slm66018  *	vdc_init_dring_negotiate()
17250a55fbb7Slm66018  *
17260a55fbb7Slm66018  * Description:
17270a55fbb7Slm66018  *
17280a55fbb7Slm66018  * Arguments:
17290a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
17300a55fbb7Slm66018  *
17310a55fbb7Slm66018  * Return Code:
17320a55fbb7Slm66018  *	0	- Success
17330a55fbb7Slm66018  */
17341ae08745Sheppo static int
17351ae08745Sheppo vdc_init_dring_negotiate(vdc_t *vdc)
17361ae08745Sheppo {
17371ae08745Sheppo 	vio_dring_reg_msg_t	pkt;
17381ae08745Sheppo 	size_t			msglen = sizeof (pkt);
17391ae08745Sheppo 	int			status = -1;
17403af08d82Slm66018 	int			retry;
17413af08d82Slm66018 	int			nretries = 10;
17421ae08745Sheppo 
17431ae08745Sheppo 	ASSERT(vdc != NULL);
17441ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
17451ae08745Sheppo 
17463af08d82Slm66018 	for (retry = 0; retry < nretries; retry++) {
17471ae08745Sheppo 		status = vdc_init_descriptor_ring(vdc);
17483af08d82Slm66018 		if (status != EAGAIN)
17493af08d82Slm66018 			break;
17503af08d82Slm66018 		drv_usecwait(vdc_min_timeout_ldc);
17513af08d82Slm66018 	}
17523af08d82Slm66018 
17531ae08745Sheppo 	if (status != 0) {
17543af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to init DRing (status = %d)\n",
17551ae08745Sheppo 		    vdc->instance, status);
17561ae08745Sheppo 		return (status);
17571ae08745Sheppo 	}
17583af08d82Slm66018 
17593af08d82Slm66018 	DMSG(vdc, 0, "[%d] Init of descriptor ring completed (status = %d)\n",
1760e1ebb9ecSlm66018 	    vdc->instance, status);
17611ae08745Sheppo 
17621ae08745Sheppo 	/* fill in tag */
17631ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
17641ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
17651ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_DRING_REG;
17661ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
17671ae08745Sheppo 	/* fill in payload */
17681ae08745Sheppo 	pkt.dring_ident = 0;
1769e1ebb9ecSlm66018 	pkt.num_descriptors = vdc->dring_len;
1770e1ebb9ecSlm66018 	pkt.descriptor_size = vdc->dring_entry_size;
17711ae08745Sheppo 	pkt.options = (VIO_TX_DRING | VIO_RX_DRING);
17721ae08745Sheppo 	pkt.ncookies = vdc->dring_cookie_count;
17731ae08745Sheppo 	pkt.cookie[0] = vdc->dring_cookie[0];	/* for now just one cookie */
17741ae08745Sheppo 
17750a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
17761ae08745Sheppo 	if (status != 0) {
17773af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to register DRing (err = %d)",
1778e1ebb9ecSlm66018 		    vdc->instance, status);
17791ae08745Sheppo 	}
17801ae08745Sheppo 
17811ae08745Sheppo 	return (status);
17821ae08745Sheppo }
17831ae08745Sheppo 
17841ae08745Sheppo 
17853af08d82Slm66018 /*
17863af08d82Slm66018  * Function:
17873af08d82Slm66018  *	vdc_dring_negotiation()
17883af08d82Slm66018  *
17893af08d82Slm66018  * Description:
17903af08d82Slm66018  *
17913af08d82Slm66018  * Arguments:
17923af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
17933af08d82Slm66018  *
17943af08d82Slm66018  * Return Code:
17953af08d82Slm66018  *	0	- Success
17963af08d82Slm66018  */
17973af08d82Slm66018 static int
17983af08d82Slm66018 vdc_dring_negotiation(vdc_t *vdcp)
17993af08d82Slm66018 {
18003af08d82Slm66018 	int status;
18013af08d82Slm66018 	vio_msg_t vio_msg;
18023af08d82Slm66018 
18033af08d82Slm66018 	if (status = vdc_init_dring_negotiate(vdcp))
18043af08d82Slm66018 		return (status);
18053af08d82Slm66018 
18063af08d82Slm66018 	/* release lock and wait for response */
18073af08d82Slm66018 	mutex_exit(&vdcp->lock);
18083af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
18093af08d82Slm66018 	mutex_enter(&vdcp->lock);
18103af08d82Slm66018 	if (status) {
18113af08d82Slm66018 		DMSG(vdcp, 0,
18123af08d82Slm66018 		    "[%d] Failed waiting for Dring negotiation response,"
18133af08d82Slm66018 		    " rv(%d)", vdcp->instance, status);
18143af08d82Slm66018 		return (status);
18153af08d82Slm66018 	}
18163af08d82Slm66018 
18173af08d82Slm66018 	/* check type and sub_type ... */
18183af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
18193af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
18203af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid Dring negotiation response\n",
18213af08d82Slm66018 		    vdcp->instance);
18223af08d82Slm66018 		return (EPROTO);
18233af08d82Slm66018 	}
18243af08d82Slm66018 
18253af08d82Slm66018 	return (vdc_handle_dring_reg_msg(vdcp,
18263af08d82Slm66018 	    (vio_dring_reg_msg_t *)&vio_msg));
18273af08d82Slm66018 }
18283af08d82Slm66018 
18293af08d82Slm66018 
18303af08d82Slm66018 /*
18313af08d82Slm66018  * Function:
18323af08d82Slm66018  *	vdc_send_rdx()
18333af08d82Slm66018  *
18343af08d82Slm66018  * Description:
18353af08d82Slm66018  *
18363af08d82Slm66018  * Arguments:
18373af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
18383af08d82Slm66018  *
18393af08d82Slm66018  * Return Code:
18403af08d82Slm66018  *	0	- Success
18413af08d82Slm66018  */
18423af08d82Slm66018 static int
18433af08d82Slm66018 vdc_send_rdx(vdc_t *vdcp)
18443af08d82Slm66018 {
18453af08d82Slm66018 	vio_msg_t	msg;
18463af08d82Slm66018 	size_t		msglen = sizeof (vio_msg_t);
18473af08d82Slm66018 	int		status;
18483af08d82Slm66018 
18493af08d82Slm66018 	/*
18503af08d82Slm66018 	 * Send an RDX message to vds to indicate we are ready
18513af08d82Slm66018 	 * to send data
18523af08d82Slm66018 	 */
18533af08d82Slm66018 	msg.tag.vio_msgtype = VIO_TYPE_CTRL;
18543af08d82Slm66018 	msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
18553af08d82Slm66018 	msg.tag.vio_subtype_env = VIO_RDX;
18563af08d82Slm66018 	msg.tag.vio_sid = vdcp->session_id;
18573af08d82Slm66018 	status = vdc_send(vdcp, (caddr_t)&msg, &msglen);
18583af08d82Slm66018 	if (status != 0) {
18593af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Failed to send RDX message (%d)",
18603af08d82Slm66018 		    vdcp->instance, status);
18613af08d82Slm66018 	}
18623af08d82Slm66018 
18633af08d82Slm66018 	return (status);
18643af08d82Slm66018 }
18653af08d82Slm66018 
18663af08d82Slm66018 /*
18673af08d82Slm66018  * Function:
18683af08d82Slm66018  *	vdc_handle_rdx()
18693af08d82Slm66018  *
18703af08d82Slm66018  * Description:
18713af08d82Slm66018  *
18723af08d82Slm66018  * Arguments:
18733af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
18743af08d82Slm66018  *	msgp	- received msg
18753af08d82Slm66018  *
18763af08d82Slm66018  * Return Code:
18773af08d82Slm66018  *	0	- Success
18783af08d82Slm66018  */
18793af08d82Slm66018 static int
18803af08d82Slm66018 vdc_handle_rdx(vdc_t *vdcp, vio_rdx_msg_t *msgp)
18813af08d82Slm66018 {
18823af08d82Slm66018 	_NOTE(ARGUNUSED(vdcp))
18833af08d82Slm66018 	_NOTE(ARGUNUSED(msgp))
18843af08d82Slm66018 
18853af08d82Slm66018 	ASSERT(msgp->tag.vio_msgtype == VIO_TYPE_CTRL);
18863af08d82Slm66018 	ASSERT(msgp->tag.vio_subtype == VIO_SUBTYPE_ACK);
18873af08d82Slm66018 	ASSERT(msgp->tag.vio_subtype_env == VIO_RDX);
18883af08d82Slm66018 
18893af08d82Slm66018 	DMSG(vdcp, 1, "[%d] Got an RDX msg", vdcp->instance);
18903af08d82Slm66018 
18913af08d82Slm66018 	return (0);
18923af08d82Slm66018 }
18933af08d82Slm66018 
18943af08d82Slm66018 /*
18953af08d82Slm66018  * Function:
18963af08d82Slm66018  *	vdc_rdx_exchange()
18973af08d82Slm66018  *
18983af08d82Slm66018  * Description:
18993af08d82Slm66018  *
19003af08d82Slm66018  * Arguments:
19013af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
19023af08d82Slm66018  *
19033af08d82Slm66018  * Return Code:
19043af08d82Slm66018  *	0	- Success
19053af08d82Slm66018  */
19063af08d82Slm66018 static int
19073af08d82Slm66018 vdc_rdx_exchange(vdc_t *vdcp)
19083af08d82Slm66018 {
19093af08d82Slm66018 	int status;
19103af08d82Slm66018 	vio_msg_t vio_msg;
19113af08d82Slm66018 
19123af08d82Slm66018 	if (status = vdc_send_rdx(vdcp))
19133af08d82Slm66018 		return (status);
19143af08d82Slm66018 
19153af08d82Slm66018 	/* release lock and wait for response */
19163af08d82Slm66018 	mutex_exit(&vdcp->lock);
19173af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
19183af08d82Slm66018 	mutex_enter(&vdcp->lock);
19193af08d82Slm66018 	if (status) {
192087a7269eSachartre 		DMSG(vdcp, 0, "[%d] Failed waiting for RDX response, rv(%d)",
192187a7269eSachartre 		    vdcp->instance, status);
19223af08d82Slm66018 		return (status);
19233af08d82Slm66018 	}
19243af08d82Slm66018 
19253af08d82Slm66018 	/* check type and sub_type ... */
19263af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
19273af08d82Slm66018 	    vio_msg.tag.vio_subtype != VIO_SUBTYPE_ACK) {
192887a7269eSachartre 		DMSG(vdcp, 0, "[%d] Invalid RDX response\n", vdcp->instance);
19293af08d82Slm66018 		return (EPROTO);
19303af08d82Slm66018 	}
19313af08d82Slm66018 
19323af08d82Slm66018 	return (vdc_handle_rdx(vdcp, (vio_rdx_msg_t *)&vio_msg));
19333af08d82Slm66018 }
19343af08d82Slm66018 
19353af08d82Slm66018 
19361ae08745Sheppo /* -------------------------------------------------------------------------- */
19371ae08745Sheppo 
19381ae08745Sheppo /*
19391ae08745Sheppo  * LDC helper routines
19401ae08745Sheppo  */
19411ae08745Sheppo 
19423af08d82Slm66018 static int
19433af08d82Slm66018 vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp)
19443af08d82Slm66018 {
19453af08d82Slm66018 	int		status;
19463af08d82Slm66018 	boolean_t	q_has_pkts = B_FALSE;
194717cadca8Slm66018 	uint64_t	delay_time;
19483af08d82Slm66018 	size_t		len;
19493af08d82Slm66018 
19503af08d82Slm66018 	mutex_enter(&vdc->read_lock);
19513af08d82Slm66018 
19523af08d82Slm66018 	if (vdc->read_state == VDC_READ_IDLE)
19533af08d82Slm66018 		vdc->read_state = VDC_READ_WAITING;
19543af08d82Slm66018 
19553af08d82Slm66018 	while (vdc->read_state != VDC_READ_PENDING) {
19563af08d82Slm66018 
19573af08d82Slm66018 		/* detect if the connection has been reset */
19583af08d82Slm66018 		if (vdc->read_state == VDC_READ_RESET) {
19593af08d82Slm66018 			status = ECONNRESET;
19603af08d82Slm66018 			goto done;
19613af08d82Slm66018 		}
19623af08d82Slm66018 
19633af08d82Slm66018 		cv_wait(&vdc->read_cv, &vdc->read_lock);
19643af08d82Slm66018 	}
19653af08d82Slm66018 
19663af08d82Slm66018 	/*
19673af08d82Slm66018 	 * Until we get a blocking ldc read we have to retry
19683af08d82Slm66018 	 * until the entire LDC message has arrived before
19693af08d82Slm66018 	 * ldc_read() will succeed. Note we also bail out if
1970eff7243fSlm66018 	 * the channel is reset or goes away.
19713af08d82Slm66018 	 */
19723af08d82Slm66018 	delay_time = vdc_ldc_read_init_delay;
19733af08d82Slm66018 loop:
19743af08d82Slm66018 	len = *nbytesp;
19758cd10891Snarayan 	status = ldc_read(vdc->curr_server->ldc_handle, (caddr_t)msgp, &len);
19763af08d82Slm66018 	switch (status) {
19773af08d82Slm66018 	case EAGAIN:
19783af08d82Slm66018 		delay_time *= 2;
19793af08d82Slm66018 		if (delay_time >= vdc_ldc_read_max_delay)
19803af08d82Slm66018 			delay_time = vdc_ldc_read_max_delay;
19813af08d82Slm66018 		delay(delay_time);
19823af08d82Slm66018 		goto loop;
19833af08d82Slm66018 
19843af08d82Slm66018 	case 0:
19853af08d82Slm66018 		if (len == 0) {
198617cadca8Slm66018 			DMSG(vdc, 1, "[%d] ldc_read returned 0 bytes with "
19873af08d82Slm66018 			    "no error!\n", vdc->instance);
19883af08d82Slm66018 			goto loop;
19893af08d82Slm66018 		}
19903af08d82Slm66018 
19913af08d82Slm66018 		*nbytesp = len;
19923af08d82Slm66018 
19933af08d82Slm66018 		/*
19943af08d82Slm66018 		 * If there are pending messages, leave the
19953af08d82Slm66018 		 * read state as pending. Otherwise, set the state
19963af08d82Slm66018 		 * back to idle.
19973af08d82Slm66018 		 */
19988cd10891Snarayan 		status = ldc_chkq(vdc->curr_server->ldc_handle, &q_has_pkts);
19993af08d82Slm66018 		if (status == 0 && !q_has_pkts)
20003af08d82Slm66018 			vdc->read_state = VDC_READ_IDLE;
20013af08d82Slm66018 
20023af08d82Slm66018 		break;
20033af08d82Slm66018 	default:
20043af08d82Slm66018 		DMSG(vdc, 0, "ldc_read returned %d\n", status);
20053af08d82Slm66018 		break;
20063af08d82Slm66018 	}
20073af08d82Slm66018 
20083af08d82Slm66018 done:
20093af08d82Slm66018 	mutex_exit(&vdc->read_lock);
20103af08d82Slm66018 
20113af08d82Slm66018 	return (status);
20123af08d82Slm66018 }
20133af08d82Slm66018 
20143af08d82Slm66018 
20153af08d82Slm66018 
20163af08d82Slm66018 #ifdef DEBUG
20173af08d82Slm66018 void
20183af08d82Slm66018 vdc_decode_tag(vdc_t *vdcp, vio_msg_t *msg)
20193af08d82Slm66018 {
20203af08d82Slm66018 	char *ms, *ss, *ses;
20213af08d82Slm66018 	switch (msg->tag.vio_msgtype) {
20223af08d82Slm66018 #define	Q(_s)	case _s : ms = #_s; break;
20233af08d82Slm66018 	Q(VIO_TYPE_CTRL)
20243af08d82Slm66018 	Q(VIO_TYPE_DATA)
20253af08d82Slm66018 	Q(VIO_TYPE_ERR)
20263af08d82Slm66018 #undef Q
20273af08d82Slm66018 	default: ms = "unknown"; break;
20283af08d82Slm66018 	}
20293af08d82Slm66018 
20303af08d82Slm66018 	switch (msg->tag.vio_subtype) {
20313af08d82Slm66018 #define	Q(_s)	case _s : ss = #_s; break;
20323af08d82Slm66018 	Q(VIO_SUBTYPE_INFO)
20333af08d82Slm66018 	Q(VIO_SUBTYPE_ACK)
20343af08d82Slm66018 	Q(VIO_SUBTYPE_NACK)
20353af08d82Slm66018 #undef Q
20363af08d82Slm66018 	default: ss = "unknown"; break;
20373af08d82Slm66018 	}
20383af08d82Slm66018 
20393af08d82Slm66018 	switch (msg->tag.vio_subtype_env) {
20403af08d82Slm66018 #define	Q(_s)	case _s : ses = #_s; break;
20413af08d82Slm66018 	Q(VIO_VER_INFO)
20423af08d82Slm66018 	Q(VIO_ATTR_INFO)
20433af08d82Slm66018 	Q(VIO_DRING_REG)
20443af08d82Slm66018 	Q(VIO_DRING_UNREG)
20453af08d82Slm66018 	Q(VIO_RDX)
20463af08d82Slm66018 	Q(VIO_PKT_DATA)
20473af08d82Slm66018 	Q(VIO_DESC_DATA)
20483af08d82Slm66018 	Q(VIO_DRING_DATA)
20493af08d82Slm66018 #undef Q
20503af08d82Slm66018 	default: ses = "unknown"; break;
20513af08d82Slm66018 	}
20523af08d82Slm66018 
20533af08d82Slm66018 	DMSG(vdcp, 3, "(%x/%x/%x) message : (%s/%s/%s)\n",
20543af08d82Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
20553af08d82Slm66018 	    msg->tag.vio_subtype_env, ms, ss, ses);
20563af08d82Slm66018 }
20573af08d82Slm66018 #endif
20583af08d82Slm66018 
20591ae08745Sheppo /*
20601ae08745Sheppo  * Function:
20611ae08745Sheppo  *	vdc_send()
20621ae08745Sheppo  *
20631ae08745Sheppo  * Description:
20641ae08745Sheppo  *	The function encapsulates the call to write a message using LDC.
20651ae08745Sheppo  *	If LDC indicates that the call failed due to the queue being full,
206617cadca8Slm66018  *	we retry the ldc_write(), otherwise we return the error returned by LDC.
20671ae08745Sheppo  *
20681ae08745Sheppo  * Arguments:
20691ae08745Sheppo  *	ldc_handle	- LDC handle for the channel this instance of vdc uses
20701ae08745Sheppo  *	pkt		- address of LDC message to be sent
20711ae08745Sheppo  *	msglen		- the size of the message being sent. When the function
20721ae08745Sheppo  *			  returns, this contains the number of bytes written.
20731ae08745Sheppo  *
20741ae08745Sheppo  * Return Code:
20751ae08745Sheppo  *	0		- Success.
20761ae08745Sheppo  *	EINVAL		- pkt or msglen were NULL
20771ae08745Sheppo  *	ECONNRESET	- The connection was not up.
20781ae08745Sheppo  *	EWOULDBLOCK	- LDC queue is full
20791ae08745Sheppo  *	xxx		- other error codes returned by ldc_write
20801ae08745Sheppo  */
20811ae08745Sheppo static int
20820a55fbb7Slm66018 vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen)
20831ae08745Sheppo {
20841ae08745Sheppo 	size_t	size = 0;
20851ae08745Sheppo 	int	status = 0;
20863af08d82Slm66018 	clock_t delay_ticks;
20871ae08745Sheppo 
20880a55fbb7Slm66018 	ASSERT(vdc != NULL);
20890a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
20901ae08745Sheppo 	ASSERT(msglen != NULL);
20911ae08745Sheppo 	ASSERT(*msglen != 0);
20921ae08745Sheppo 
20933af08d82Slm66018 #ifdef DEBUG
209417cadca8Slm66018 	vdc_decode_tag(vdc, (vio_msg_t *)(uintptr_t)pkt);
20953af08d82Slm66018 #endif
20963af08d82Slm66018 	/*
20973af08d82Slm66018 	 * Wait indefinitely to send if channel
20983af08d82Slm66018 	 * is busy, but bail out if we succeed or
20993af08d82Slm66018 	 * if the channel closes or is reset.
21003af08d82Slm66018 	 */
21013af08d82Slm66018 	delay_ticks = vdc_hz_min_ldc_delay;
21021ae08745Sheppo 	do {
21031ae08745Sheppo 		size = *msglen;
21048cd10891Snarayan 		status = ldc_write(vdc->curr_server->ldc_handle, pkt, &size);
21053af08d82Slm66018 		if (status == EWOULDBLOCK) {
21063af08d82Slm66018 			delay(delay_ticks);
21073af08d82Slm66018 			/* geometric backoff */
21083af08d82Slm66018 			delay_ticks *= 2;
21093af08d82Slm66018 			if (delay_ticks > vdc_hz_max_ldc_delay)
21103af08d82Slm66018 				delay_ticks = vdc_hz_max_ldc_delay;
21113af08d82Slm66018 		}
21123af08d82Slm66018 	} while (status == EWOULDBLOCK);
21131ae08745Sheppo 
21140a55fbb7Slm66018 	/* if LDC had serious issues --- reset vdc state */
21150a55fbb7Slm66018 	if (status == EIO || status == ECONNRESET) {
21163af08d82Slm66018 		/* LDC had serious issues --- reset vdc state */
21173af08d82Slm66018 		mutex_enter(&vdc->read_lock);
21183af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
21193af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET))
21203af08d82Slm66018 			cv_signal(&vdc->read_cv);
21213af08d82Slm66018 		vdc->read_state = VDC_READ_RESET;
21223af08d82Slm66018 		mutex_exit(&vdc->read_lock);
21233af08d82Slm66018 
21243af08d82Slm66018 		/* wake up any waiters in the reset thread */
21253af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
21263af08d82Slm66018 			DMSG(vdc, 0, "[%d] write reset - "
21273af08d82Slm66018 			    "vdc is resetting ..\n", vdc->instance);
21283af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
21293af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
21303af08d82Slm66018 		}
21313af08d82Slm66018 
21323af08d82Slm66018 		return (ECONNRESET);
21330a55fbb7Slm66018 	}
21340a55fbb7Slm66018 
21351ae08745Sheppo 	/* return the last size written */
21361ae08745Sheppo 	*msglen = size;
21371ae08745Sheppo 
21381ae08745Sheppo 	return (status);
21391ae08745Sheppo }
21401ae08745Sheppo 
21411ae08745Sheppo /*
21421ae08745Sheppo  * Function:
2143655fd6a9Sachartre  *	vdc_get_md_node
21441ae08745Sheppo  *
21451ae08745Sheppo  * Description:
21468cd10891Snarayan  *	Get the MD, the device node for the given disk instance. The
21478cd10891Snarayan  *	caller is responsible for cleaning up the reference to the
21488cd10891Snarayan  *	returned MD (mdpp) by calling md_fini_handle().
21491ae08745Sheppo  *
21501ae08745Sheppo  * Arguments:
21511ae08745Sheppo  *	dip	- dev info pointer for this instance of the device driver.
2152655fd6a9Sachartre  *	mdpp	- the returned MD.
2153655fd6a9Sachartre  *	vd_nodep - the returned device node.
21541ae08745Sheppo  *
21551ae08745Sheppo  * Return Code:
21561ae08745Sheppo  *	0	- Success.
21571ae08745Sheppo  *	ENOENT	- Expected node or property did not exist.
21581ae08745Sheppo  *	ENXIO	- Unexpected error communicating with MD framework
21591ae08745Sheppo  */
21601ae08745Sheppo static int
21618cd10891Snarayan vdc_get_md_node(dev_info_t *dip, md_t **mdpp, mde_cookie_t *vd_nodep)
21621ae08745Sheppo {
21631ae08745Sheppo 	int		status = ENOENT;
21641ae08745Sheppo 	char		*node_name = NULL;
21651ae08745Sheppo 	md_t		*mdp = NULL;
21661ae08745Sheppo 	int		num_nodes;
21671ae08745Sheppo 	int		num_vdevs;
21681ae08745Sheppo 	mde_cookie_t	rootnode;
21691ae08745Sheppo 	mde_cookie_t	*listp = NULL;
21701ae08745Sheppo 	boolean_t	found_inst = B_FALSE;
21711ae08745Sheppo 	int		listsz;
21721ae08745Sheppo 	int		idx;
21731ae08745Sheppo 	uint64_t	md_inst;
21741ae08745Sheppo 	int		obp_inst;
21751ae08745Sheppo 	int		instance = ddi_get_instance(dip);
21761ae08745Sheppo 
21771ae08745Sheppo 	/*
21781ae08745Sheppo 	 * Get the OBP instance number for comparison with the MD instance
21791ae08745Sheppo 	 *
21801ae08745Sheppo 	 * The "cfg-handle" property of a vdc node in an MD contains the MD's
21811ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
21821ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
21831ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
21841ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
21851ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance.
21861ae08745Sheppo 	 * If the "reg" property cannot be found, the device tree state is
21871ae08745Sheppo 	 * presumably so broken that there is no point in continuing.
21881ae08745Sheppo 	 */
21891ae08745Sheppo 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, OBP_REG)) {
21901ae08745Sheppo 		cmn_err(CE_WARN, "'%s' property does not exist", OBP_REG);
21911ae08745Sheppo 		return (ENOENT);
21921ae08745Sheppo 	}
21931ae08745Sheppo 	obp_inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
21941ae08745Sheppo 	    OBP_REG, -1);
21953af08d82Slm66018 	DMSGX(1, "[%d] OBP inst=%d\n", instance, obp_inst);
21961ae08745Sheppo 
21971ae08745Sheppo 	/*
2198655fd6a9Sachartre 	 * We now walk the MD nodes to find the node for this vdisk.
21991ae08745Sheppo 	 */
22001ae08745Sheppo 	if ((mdp = md_get_handle()) == NULL) {
22011ae08745Sheppo 		cmn_err(CE_WARN, "unable to init machine description");
22021ae08745Sheppo 		return (ENXIO);
22031ae08745Sheppo 	}
22041ae08745Sheppo 
22051ae08745Sheppo 	num_nodes = md_node_count(mdp);
22061ae08745Sheppo 	ASSERT(num_nodes > 0);
22071ae08745Sheppo 
22081ae08745Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
22091ae08745Sheppo 
22101ae08745Sheppo 	/* allocate memory for nodes */
22111ae08745Sheppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
22121ae08745Sheppo 
22131ae08745Sheppo 	rootnode = md_root_node(mdp);
22141ae08745Sheppo 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
22151ae08745Sheppo 
22161ae08745Sheppo 	/*
22171ae08745Sheppo 	 * Search for all the virtual devices, we will then check to see which
22181ae08745Sheppo 	 * ones are disk nodes.
22191ae08745Sheppo 	 */
22201ae08745Sheppo 	num_vdevs = md_scan_dag(mdp, rootnode,
22211ae08745Sheppo 	    md_find_name(mdp, VDC_MD_VDEV_NAME),
22221ae08745Sheppo 	    md_find_name(mdp, "fwd"), listp);
22231ae08745Sheppo 
22241ae08745Sheppo 	if (num_vdevs <= 0) {
22251ae08745Sheppo 		cmn_err(CE_NOTE, "No '%s' node found", VDC_MD_VDEV_NAME);
22261ae08745Sheppo 		status = ENOENT;
22271ae08745Sheppo 		goto done;
22281ae08745Sheppo 	}
22291ae08745Sheppo 
22303af08d82Slm66018 	DMSGX(1, "[%d] num_vdevs=%d\n", instance, num_vdevs);
22311ae08745Sheppo 	for (idx = 0; idx < num_vdevs; idx++) {
22321ae08745Sheppo 		status = md_get_prop_str(mdp, listp[idx], "name", &node_name);
22331ae08745Sheppo 		if ((status != 0) || (node_name == NULL)) {
22341ae08745Sheppo 			cmn_err(CE_NOTE, "Unable to get name of node type '%s'"
22351ae08745Sheppo 			    ": err %d", VDC_MD_VDEV_NAME, status);
22361ae08745Sheppo 			continue;
22371ae08745Sheppo 		}
22381ae08745Sheppo 
22393af08d82Slm66018 		DMSGX(1, "[%d] Found node '%s'\n", instance, node_name);
22401ae08745Sheppo 		if (strcmp(VDC_MD_DISK_NAME, node_name) == 0) {
22411ae08745Sheppo 			status = md_get_prop_val(mdp, listp[idx],
22421ae08745Sheppo 			    VDC_MD_CFG_HDL, &md_inst);
22433af08d82Slm66018 			DMSGX(1, "[%d] vdc inst in MD=%lx\n",
22443af08d82Slm66018 			    instance, md_inst);
22451ae08745Sheppo 			if ((status == 0) && (md_inst == obp_inst)) {
22461ae08745Sheppo 				found_inst = B_TRUE;
22471ae08745Sheppo 				break;
22481ae08745Sheppo 			}
22491ae08745Sheppo 		}
22501ae08745Sheppo 	}
22511ae08745Sheppo 
22520a55fbb7Slm66018 	if (!found_inst) {
22533af08d82Slm66018 		DMSGX(0, "Unable to find correct '%s' node", VDC_MD_DISK_NAME);
22541ae08745Sheppo 		status = ENOENT;
22551ae08745Sheppo 		goto done;
22561ae08745Sheppo 	}
22573af08d82Slm66018 	DMSGX(0, "[%d] MD inst=%lx\n", instance, md_inst);
22581ae08745Sheppo 
2259655fd6a9Sachartre 	*vd_nodep = listp[idx];
2260655fd6a9Sachartre 	*mdpp = mdp;
2261655fd6a9Sachartre done:
2262655fd6a9Sachartre 	kmem_free(listp, listsz);
2263655fd6a9Sachartre 	return (status);
2264655fd6a9Sachartre }
2265655fd6a9Sachartre 
2266655fd6a9Sachartre /*
2267655fd6a9Sachartre  * Function:
22688cd10891Snarayan  *	vdc_init_ports
2269655fd6a9Sachartre  *
2270655fd6a9Sachartre  * Description:
22718cd10891Snarayan  *	Initialize all the ports for this vdisk instance.
2272655fd6a9Sachartre  *
2273655fd6a9Sachartre  * Arguments:
22748cd10891Snarayan  *	vdc	- soft state pointer for this instance of the device driver.
22758cd10891Snarayan  *	mdp	- md pointer
22768cd10891Snarayan  *	vd_nodep - device md node.
2277655fd6a9Sachartre  *
2278655fd6a9Sachartre  * Return Code:
2279655fd6a9Sachartre  *	0	- Success.
2280655fd6a9Sachartre  *	ENOENT	- Expected node or property did not exist.
2281655fd6a9Sachartre  */
2282655fd6a9Sachartre static int
22838cd10891Snarayan vdc_init_ports(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_nodep)
2284655fd6a9Sachartre {
2285655fd6a9Sachartre 	int		status = 0;
22868cd10891Snarayan 	int		idx;
22878cd10891Snarayan 	int		num_nodes;
22888cd10891Snarayan 	int		num_vports;
22898cd10891Snarayan 	int		num_chans;
22908cd10891Snarayan 	int		listsz;
22918cd10891Snarayan 	mde_cookie_t	vd_port;
22928cd10891Snarayan 	mde_cookie_t	*chanp = NULL;
22938cd10891Snarayan 	mde_cookie_t	*portp = NULL;
22948cd10891Snarayan 	vdc_server_t	*srvr;
22958cd10891Snarayan 	vdc_server_t	*prev_srvr = NULL;
2296655fd6a9Sachartre 
22978cd10891Snarayan 	/*
22988cd10891Snarayan 	 * We now walk the MD nodes to find the port nodes for this vdisk.
22998cd10891Snarayan 	 */
2300655fd6a9Sachartre 	num_nodes = md_node_count(mdp);
2301655fd6a9Sachartre 	ASSERT(num_nodes > 0);
2302655fd6a9Sachartre 
2303655fd6a9Sachartre 	listsz = num_nodes * sizeof (mde_cookie_t);
2304655fd6a9Sachartre 
2305655fd6a9Sachartre 	/* allocate memory for nodes */
23068cd10891Snarayan 	portp = kmem_zalloc(listsz, KM_SLEEP);
2307655fd6a9Sachartre 	chanp = kmem_zalloc(listsz, KM_SLEEP);
2308655fd6a9Sachartre 
23098cd10891Snarayan 	num_vports = md_scan_dag(mdp, vd_nodep,
23108cd10891Snarayan 	    md_find_name(mdp, VDC_MD_PORT_NAME),
23118cd10891Snarayan 	    md_find_name(mdp, "fwd"), portp);
23128cd10891Snarayan 	if (num_vports == 0) {
23138cd10891Snarayan 		DMSGX(0, "Found no '%s' node for '%s' port\n",
23148cd10891Snarayan 		    VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME);
23158cd10891Snarayan 		status = ENOENT;
23168cd10891Snarayan 		goto done;
23178cd10891Snarayan 	}
23188cd10891Snarayan 
23198cd10891Snarayan 	DMSGX(1, "Found %d '%s' node(s) for '%s' port\n",
23208cd10891Snarayan 	    num_vports, VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME);
23218cd10891Snarayan 
23228cd10891Snarayan 	vdc->num_servers = 0;
23238cd10891Snarayan 	for (idx = 0; idx < num_vports; idx++) {
23248cd10891Snarayan 
23258cd10891Snarayan 		/* initialize this port */
23268cd10891Snarayan 		vd_port = portp[idx];
23278cd10891Snarayan 		srvr = kmem_zalloc(sizeof (vdc_server_t), KM_SLEEP);
23288cd10891Snarayan 		srvr->vdcp = vdc;
23298cd10891Snarayan 
23308cd10891Snarayan 		/* get port id */
23318cd10891Snarayan 		if (md_get_prop_val(mdp, vd_port, VDC_MD_ID, &srvr->id) != 0) {
23328cd10891Snarayan 			cmn_err(CE_NOTE, "vDisk port '%s' property not found",
23338cd10891Snarayan 			    VDC_MD_ID);
23348cd10891Snarayan 			kmem_free(srvr, sizeof (vdc_server_t));
23358cd10891Snarayan 			continue;
23368cd10891Snarayan 		}
23378cd10891Snarayan 
23388cd10891Snarayan 		/* set the connection timeout */
23398cd10891Snarayan 		if (md_get_prop_val(mdp, vd_port, VDC_MD_TIMEOUT,
23408cd10891Snarayan 		    &srvr->ctimeout) != 0) {
23418cd10891Snarayan 			srvr->ctimeout = 0;
23428cd10891Snarayan 		}
23438cd10891Snarayan 
23448cd10891Snarayan 		/* get the ldc id */
23458cd10891Snarayan 		num_chans = md_scan_dag(mdp, vd_port,
23461ae08745Sheppo 		    md_find_name(mdp, VDC_MD_CHAN_NAME),
23471ae08745Sheppo 		    md_find_name(mdp, "fwd"), chanp);
23481ae08745Sheppo 
23491ae08745Sheppo 		/* expecting at least one channel */
23501ae08745Sheppo 		if (num_chans <= 0) {
23511ae08745Sheppo 			cmn_err(CE_NOTE, "No '%s' node for '%s' port",
23521ae08745Sheppo 			    VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME);
23538cd10891Snarayan 			kmem_free(srvr, sizeof (vdc_server_t));
23548cd10891Snarayan 			continue;
23551ae08745Sheppo 		} else if (num_chans != 1) {
23568cd10891Snarayan 			DMSGX(0, "Expected 1 '%s' node for '%s' port, "
23578cd10891Snarayan 			    "found %d\n", VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME,
23588cd10891Snarayan 			    num_chans);
23591ae08745Sheppo 		}
23601ae08745Sheppo 
23611ae08745Sheppo 		/*
23621ae08745Sheppo 		 * We use the first channel found (index 0), irrespective of how
23631ae08745Sheppo 		 * many are there in total.
23641ae08745Sheppo 		 */
23658cd10891Snarayan 		if (md_get_prop_val(mdp, chanp[0], VDC_MD_ID,
23668cd10891Snarayan 		    &srvr->ldc_id) != 0) {
23678cd10891Snarayan 			cmn_err(CE_NOTE, "Channel '%s' property not found",
23688cd10891Snarayan 			    VDC_MD_ID);
23698cd10891Snarayan 			kmem_free(srvr, sizeof (vdc_server_t));
23708cd10891Snarayan 			continue;
23718cd10891Snarayan 		}
23728cd10891Snarayan 
23738cd10891Snarayan 		/*
23748cd10891Snarayan 		 * now initialise LDC channel which will be used to
23758cd10891Snarayan 		 * communicate with this server
23768cd10891Snarayan 		 */
23778cd10891Snarayan 		if (vdc_do_ldc_init(vdc, srvr) != 0) {
23788cd10891Snarayan 			kmem_free(srvr, sizeof (vdc_server_t));
23798cd10891Snarayan 			continue;
23808cd10891Snarayan 		}
23818cd10891Snarayan 
23828cd10891Snarayan 		/* add server to list */
2383*d7400d00Sachartre 		if (prev_srvr)
23848cd10891Snarayan 			prev_srvr->next = srvr;
2385*d7400d00Sachartre 		else
23868cd10891Snarayan 			vdc->server_list = srvr;
2387*d7400d00Sachartre 
23888cd10891Snarayan 		prev_srvr = srvr;
23898cd10891Snarayan 
23908cd10891Snarayan 		/* inc numbers of servers */
23918cd10891Snarayan 		vdc->num_servers++;
23928cd10891Snarayan 	}
23938cd10891Snarayan 
23948cd10891Snarayan 	/*
23958cd10891Snarayan 	 * Adjust the max number of handshake retries to match
23968cd10891Snarayan 	 * the number of vdisk servers.
23978cd10891Snarayan 	 */
23988cd10891Snarayan 	if (vdc_hshake_retries < vdc->num_servers)
23998cd10891Snarayan 		vdc_hshake_retries = vdc->num_servers;
24008cd10891Snarayan 
24018cd10891Snarayan 	/* pick first server as current server */
24028cd10891Snarayan 	if (vdc->server_list != NULL) {
24038cd10891Snarayan 		vdc->curr_server = vdc->server_list;
24048cd10891Snarayan 		status = 0;
24058cd10891Snarayan 	} else {
24061ae08745Sheppo 		status = ENOENT;
24071ae08745Sheppo 	}
24081ae08745Sheppo 
24091ae08745Sheppo done:
24101ae08745Sheppo 	kmem_free(chanp, listsz);
24118cd10891Snarayan 	kmem_free(portp, listsz);
24121ae08745Sheppo 	return (status);
24131ae08745Sheppo }
24141ae08745Sheppo 
24158cd10891Snarayan 
24168cd10891Snarayan /*
24178cd10891Snarayan  * Function:
24188cd10891Snarayan  *	vdc_do_ldc_up
24198cd10891Snarayan  *
24208cd10891Snarayan  * Description:
24218cd10891Snarayan  *	Bring the channel for the current server up.
24228cd10891Snarayan  *
24238cd10891Snarayan  * Arguments:
24248cd10891Snarayan  *	vdc	- soft state pointer for this instance of the device driver.
24258cd10891Snarayan  *
24268cd10891Snarayan  * Return Code:
24278cd10891Snarayan  *	0		- Success.
24288cd10891Snarayan  *	EINVAL		- Driver is detaching / LDC error
24298cd10891Snarayan  *	ECONNREFUSED	- Other end is not listening
24308cd10891Snarayan  */
24310a55fbb7Slm66018 static int
24320a55fbb7Slm66018 vdc_do_ldc_up(vdc_t *vdc)
24330a55fbb7Slm66018 {
24340a55fbb7Slm66018 	int		status;
24353af08d82Slm66018 	ldc_status_t	ldc_state;
24360a55fbb7Slm66018 
24378cd10891Snarayan 	ASSERT(MUTEX_HELD(&vdc->lock));
24388cd10891Snarayan 
24393af08d82Slm66018 	DMSG(vdc, 0, "[%d] Bringing up channel %lx\n",
24408cd10891Snarayan 	    vdc->instance, vdc->curr_server->ldc_id);
24413af08d82Slm66018 
24423af08d82Slm66018 	if (vdc->lifecycle == VDC_LC_DETACHING)
24433af08d82Slm66018 		return (EINVAL);
24440a55fbb7Slm66018 
24458cd10891Snarayan 	if ((status = ldc_up(vdc->curr_server->ldc_handle)) != 0) {
24460a55fbb7Slm66018 		switch (status) {
24470a55fbb7Slm66018 		case ECONNREFUSED:	/* listener not ready at other end */
24483af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_up(%lx,...) return %d\n",
24498cd10891Snarayan 			    vdc->instance, vdc->curr_server->ldc_id, status);
24500a55fbb7Slm66018 			status = 0;
24510a55fbb7Slm66018 			break;
24520a55fbb7Slm66018 		default:
24533af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to bring up LDC: "
24548cd10891Snarayan 			    "channel=%ld, err=%d", vdc->instance,
24558cd10891Snarayan 			    vdc->curr_server->ldc_id, status);
24563af08d82Slm66018 			break;
24573af08d82Slm66018 		}
24583af08d82Slm66018 	}
24593af08d82Slm66018 
24608cd10891Snarayan 	if (ldc_status(vdc->curr_server->ldc_handle, &ldc_state) == 0) {
24618cd10891Snarayan 		vdc->curr_server->ldc_state = ldc_state;
24623af08d82Slm66018 		if (ldc_state == LDC_UP) {
24633af08d82Slm66018 			DMSG(vdc, 0, "[%d] LDC channel already up\n",
24643af08d82Slm66018 			    vdc->instance);
24653af08d82Slm66018 			vdc->seq_num = 1;
24663af08d82Slm66018 			vdc->seq_num_reply = 0;
24670a55fbb7Slm66018 		}
24680a55fbb7Slm66018 	}
24690a55fbb7Slm66018 
24700a55fbb7Slm66018 	return (status);
24710a55fbb7Slm66018 }
24720a55fbb7Slm66018 
24730a55fbb7Slm66018 /*
24740a55fbb7Slm66018  * Function:
24750a55fbb7Slm66018  *	vdc_terminate_ldc()
24760a55fbb7Slm66018  *
24770a55fbb7Slm66018  * Description:
24780a55fbb7Slm66018  *
24790a55fbb7Slm66018  * Arguments:
24800a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
24818cd10891Snarayan  *	srvr	- vdc per-server info structure
24820a55fbb7Slm66018  *
24830a55fbb7Slm66018  * Return Code:
24840a55fbb7Slm66018  *	None
24850a55fbb7Slm66018  */
24861ae08745Sheppo static void
24878cd10891Snarayan vdc_terminate_ldc(vdc_t *vdc, vdc_server_t *srvr)
24881ae08745Sheppo {
24891ae08745Sheppo 	int	instance = ddi_get_instance(vdc->dip);
24901ae08745Sheppo 
24918cd10891Snarayan 	if (srvr->state & VDC_LDC_OPEN) {
24928cd10891Snarayan 		DMSG(vdc, 0, "[%d] ldc_close()\n", instance);
24938cd10891Snarayan 		(void) ldc_close(srvr->ldc_handle);
24948cd10891Snarayan 	}
24958cd10891Snarayan 	if (srvr->state & VDC_LDC_CB) {
24968cd10891Snarayan 		DMSG(vdc, 0, "[%d] ldc_unreg_callback()\n", instance);
24978cd10891Snarayan 		(void) ldc_unreg_callback(srvr->ldc_handle);
24988cd10891Snarayan 	}
24998cd10891Snarayan 	if (srvr->state & VDC_LDC_INIT) {
25008cd10891Snarayan 		DMSG(vdc, 0, "[%d] ldc_fini()\n", instance);
25018cd10891Snarayan 		(void) ldc_fini(srvr->ldc_handle);
25028cd10891Snarayan 		srvr->ldc_handle = NULL;
25038cd10891Snarayan 	}
25048cd10891Snarayan 
25058cd10891Snarayan 	srvr->state &= ~(VDC_LDC_INIT | VDC_LDC_CB | VDC_LDC_OPEN);
25068cd10891Snarayan }
25078cd10891Snarayan 
25088cd10891Snarayan /*
25098cd10891Snarayan  * Function:
25108cd10891Snarayan  *	vdc_fini_ports()
25118cd10891Snarayan  *
25128cd10891Snarayan  * Description:
25138cd10891Snarayan  *	Finalize all ports by closing the channel associated with each
25148cd10891Snarayan  *	port and also freeing the server structure.
25158cd10891Snarayan  *
25168cd10891Snarayan  * Arguments:
25178cd10891Snarayan  *	vdc	- soft state pointer for this instance of the device driver.
25188cd10891Snarayan  *
25198cd10891Snarayan  * Return Code:
25208cd10891Snarayan  *	None
25218cd10891Snarayan  */
25228cd10891Snarayan static void
25238cd10891Snarayan vdc_fini_ports(vdc_t *vdc)
25248cd10891Snarayan {
25258cd10891Snarayan 	int		instance = ddi_get_instance(vdc->dip);
25268cd10891Snarayan 	vdc_server_t	*srvr, *prev_srvr;
25278cd10891Snarayan 
25281ae08745Sheppo 	ASSERT(vdc != NULL);
25291ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
25301ae08745Sheppo 
25313af08d82Slm66018 	DMSG(vdc, 0, "[%d] initialized=%x\n", instance, vdc->initialized);
25321ae08745Sheppo 
25338cd10891Snarayan 	srvr = vdc->server_list;
25348cd10891Snarayan 
25358cd10891Snarayan 	while (srvr) {
25368cd10891Snarayan 
25378cd10891Snarayan 		vdc_terminate_ldc(vdc, srvr);
25388cd10891Snarayan 
25398cd10891Snarayan 		/* next server */
25408cd10891Snarayan 		prev_srvr = srvr;
25418cd10891Snarayan 		srvr = srvr->next;
25428cd10891Snarayan 
25438cd10891Snarayan 		/* free server */
25448cd10891Snarayan 		kmem_free(prev_srvr, sizeof (vdc_server_t));
25451ae08745Sheppo 	}
25461ae08745Sheppo 
25478cd10891Snarayan 	vdc->server_list = NULL;
25481ae08745Sheppo }
25491ae08745Sheppo 
25501ae08745Sheppo /* -------------------------------------------------------------------------- */
25511ae08745Sheppo 
25521ae08745Sheppo /*
25531ae08745Sheppo  * Descriptor Ring helper routines
25541ae08745Sheppo  */
25551ae08745Sheppo 
25560a55fbb7Slm66018 /*
25570a55fbb7Slm66018  * Function:
25580a55fbb7Slm66018  *	vdc_init_descriptor_ring()
25590a55fbb7Slm66018  *
25600a55fbb7Slm66018  * Description:
25610a55fbb7Slm66018  *
25620a55fbb7Slm66018  * Arguments:
25630a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
25640a55fbb7Slm66018  *
25650a55fbb7Slm66018  * Return Code:
25660a55fbb7Slm66018  *	0	- Success
25670a55fbb7Slm66018  */
25681ae08745Sheppo static int
25691ae08745Sheppo vdc_init_descriptor_ring(vdc_t *vdc)
25701ae08745Sheppo {
25711ae08745Sheppo 	vd_dring_entry_t	*dep = NULL;	/* DRing Entry pointer */
25720a55fbb7Slm66018 	int	status = 0;
25731ae08745Sheppo 	int	i;
25741ae08745Sheppo 
25753af08d82Slm66018 	DMSG(vdc, 0, "[%d] initialized=%x\n", vdc->instance, vdc->initialized);
25761ae08745Sheppo 
25771ae08745Sheppo 	ASSERT(vdc != NULL);
25781ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
25791ae08745Sheppo 
2580e1ebb9ecSlm66018 	/* ensure we have enough room to store max sized block */
2581e1ebb9ecSlm66018 	ASSERT(maxphys <= VD_MAX_BLOCK_SIZE);
2582e1ebb9ecSlm66018 
25830a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_INIT) == 0) {
25843af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_mem_dring_create\n", vdc->instance);
2585e1ebb9ecSlm66018 		/*
2586e1ebb9ecSlm66018 		 * Calculate the maximum block size we can transmit using one
2587e1ebb9ecSlm66018 		 * Descriptor Ring entry from the attributes returned by the
2588e1ebb9ecSlm66018 		 * vDisk server. This is subject to a minimum of 'maxphys'
2589e1ebb9ecSlm66018 		 * as we do not have the capability to split requests over
2590e1ebb9ecSlm66018 		 * multiple DRing entries.
2591e1ebb9ecSlm66018 		 */
2592e1ebb9ecSlm66018 		if ((vdc->max_xfer_sz * vdc->block_size) < maxphys) {
25933af08d82Slm66018 			DMSG(vdc, 0, "[%d] using minimum DRing size\n",
2594e1ebb9ecSlm66018 			    vdc->instance);
2595e1ebb9ecSlm66018 			vdc->dring_max_cookies = maxphys / PAGESIZE;
2596e1ebb9ecSlm66018 		} else {
2597e1ebb9ecSlm66018 			vdc->dring_max_cookies =
2598e1ebb9ecSlm66018 			    (vdc->max_xfer_sz * vdc->block_size) / PAGESIZE;
2599e1ebb9ecSlm66018 		}
2600e1ebb9ecSlm66018 		vdc->dring_entry_size = (sizeof (vd_dring_entry_t) +
2601e1ebb9ecSlm66018 		    (sizeof (ldc_mem_cookie_t) *
2602e1ebb9ecSlm66018 		    (vdc->dring_max_cookies - 1)));
2603e1ebb9ecSlm66018 		vdc->dring_len = VD_DRING_LEN;
2604e1ebb9ecSlm66018 
2605e1ebb9ecSlm66018 		status = ldc_mem_dring_create(vdc->dring_len,
26068cd10891Snarayan 		    vdc->dring_entry_size, &vdc->dring_hdl);
26078cd10891Snarayan 		if ((vdc->dring_hdl == NULL) || (status != 0)) {
26083af08d82Slm66018 			DMSG(vdc, 0, "[%d] Descriptor ring creation failed",
2609e1ebb9ecSlm66018 			    vdc->instance);
26101ae08745Sheppo 			return (status);
26111ae08745Sheppo 		}
26120a55fbb7Slm66018 		vdc->initialized |= VDC_DRING_INIT;
26130a55fbb7Slm66018 	}
26141ae08745Sheppo 
26150a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_BOUND) == 0) {
26163af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_mem_dring_bind\n", vdc->instance);
26170a55fbb7Slm66018 		vdc->dring_cookie =
26180a55fbb7Slm66018 		    kmem_zalloc(sizeof (ldc_mem_cookie_t), KM_SLEEP);
26191ae08745Sheppo 
26208cd10891Snarayan 		status = ldc_mem_dring_bind(vdc->curr_server->ldc_handle,
26218cd10891Snarayan 		    vdc->dring_hdl,
26224bac2208Snarayan 		    LDC_SHADOW_MAP|LDC_DIRECT_MAP, LDC_MEM_RW,
26230a55fbb7Slm66018 		    &vdc->dring_cookie[0],
26241ae08745Sheppo 		    &vdc->dring_cookie_count);
26251ae08745Sheppo 		if (status != 0) {
26263af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to bind descriptor ring "
26273af08d82Slm66018 			    "(%lx) to channel (%lx) status=%d\n",
26288cd10891Snarayan 			    vdc->instance, vdc->dring_hdl,
26298cd10891Snarayan 			    vdc->curr_server->ldc_handle, status);
26301ae08745Sheppo 			return (status);
26311ae08745Sheppo 		}
26321ae08745Sheppo 		ASSERT(vdc->dring_cookie_count == 1);
26331ae08745Sheppo 		vdc->initialized |= VDC_DRING_BOUND;
26340a55fbb7Slm66018 	}
26351ae08745Sheppo 
26368cd10891Snarayan 	status = ldc_mem_dring_info(vdc->dring_hdl, &vdc->dring_mem_info);
26371ae08745Sheppo 	if (status != 0) {
26383af08d82Slm66018 		DMSG(vdc, 0,
26393af08d82Slm66018 		    "[%d] Failed to get info for descriptor ring (%lx)\n",
26408cd10891Snarayan 		    vdc->instance, vdc->dring_hdl);
26411ae08745Sheppo 		return (status);
26421ae08745Sheppo 	}
26431ae08745Sheppo 
26440a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_LOCAL) == 0) {
26453af08d82Slm66018 		DMSG(vdc, 0, "[%d] local dring\n", vdc->instance);
26460a55fbb7Slm66018 
26471ae08745Sheppo 		/* Allocate the local copy of this dring */
26480a55fbb7Slm66018 		vdc->local_dring =
2649e1ebb9ecSlm66018 		    kmem_zalloc(vdc->dring_len * sizeof (vdc_local_desc_t),
26501ae08745Sheppo 		    KM_SLEEP);
26511ae08745Sheppo 		vdc->initialized |= VDC_DRING_LOCAL;
26520a55fbb7Slm66018 	}
26531ae08745Sheppo 
26541ae08745Sheppo 	/*
26550a55fbb7Slm66018 	 * Mark all DRing entries as free and initialize the private
26560a55fbb7Slm66018 	 * descriptor's memory handles. If any entry is initialized,
26570a55fbb7Slm66018 	 * we need to free it later so we set the bit in 'initialized'
26580a55fbb7Slm66018 	 * at the start.
26591ae08745Sheppo 	 */
26601ae08745Sheppo 	vdc->initialized |= VDC_DRING_ENTRY;
2661e1ebb9ecSlm66018 	for (i = 0; i < vdc->dring_len; i++) {
26621ae08745Sheppo 		dep = VDC_GET_DRING_ENTRY_PTR(vdc, i);
26631ae08745Sheppo 		dep->hdr.dstate = VIO_DESC_FREE;
26641ae08745Sheppo 
26658cd10891Snarayan 		status = ldc_mem_alloc_handle(vdc->curr_server->ldc_handle,
26661ae08745Sheppo 		    &vdc->local_dring[i].desc_mhdl);
26671ae08745Sheppo 		if (status != 0) {
26683af08d82Slm66018 			DMSG(vdc, 0, "![%d] Failed to alloc mem handle for"
26691ae08745Sheppo 			    " descriptor %d", vdc->instance, i);
26701ae08745Sheppo 			return (status);
26711ae08745Sheppo 		}
26723af08d82Slm66018 		vdc->local_dring[i].is_free = B_TRUE;
26731ae08745Sheppo 		vdc->local_dring[i].dep = dep;
26741ae08745Sheppo 	}
26751ae08745Sheppo 
26763af08d82Slm66018 	/* Initialize the starting index */
26773af08d82Slm66018 	vdc->dring_curr_idx = 0;
26781ae08745Sheppo 
26791ae08745Sheppo 	return (status);
26801ae08745Sheppo }
26811ae08745Sheppo 
26820a55fbb7Slm66018 /*
26830a55fbb7Slm66018  * Function:
26840a55fbb7Slm66018  *	vdc_destroy_descriptor_ring()
26850a55fbb7Slm66018  *
26860a55fbb7Slm66018  * Description:
26870a55fbb7Slm66018  *
26880a55fbb7Slm66018  * Arguments:
26890a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
26900a55fbb7Slm66018  *
26910a55fbb7Slm66018  * Return Code:
26920a55fbb7Slm66018  *	None
26930a55fbb7Slm66018  */
26941ae08745Sheppo static void
26951ae08745Sheppo vdc_destroy_descriptor_ring(vdc_t *vdc)
26961ae08745Sheppo {
26970a55fbb7Slm66018 	vdc_local_desc_t	*ldep = NULL;	/* Local Dring Entry Pointer */
26981ae08745Sheppo 	ldc_mem_handle_t	mhdl = NULL;
26993af08d82Slm66018 	ldc_mem_info_t		minfo;
27001ae08745Sheppo 	int			status = -1;
27011ae08745Sheppo 	int			i;	/* loop */
27021ae08745Sheppo 
27031ae08745Sheppo 	ASSERT(vdc != NULL);
27041ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
27051ae08745Sheppo 
27063af08d82Slm66018 	DMSG(vdc, 0, "[%d] Entered\n", vdc->instance);
27071ae08745Sheppo 
27081ae08745Sheppo 	if (vdc->initialized & VDC_DRING_ENTRY) {
27093af08d82Slm66018 		DMSG(vdc, 0,
27103af08d82Slm66018 		    "[%d] Removing Local DRing entries\n", vdc->instance);
2711e1ebb9ecSlm66018 		for (i = 0; i < vdc->dring_len; i++) {
27120a55fbb7Slm66018 			ldep = &vdc->local_dring[i];
27130a55fbb7Slm66018 			mhdl = ldep->desc_mhdl;
27141ae08745Sheppo 
27150a55fbb7Slm66018 			if (mhdl == NULL)
27160a55fbb7Slm66018 				continue;
27170a55fbb7Slm66018 
27183af08d82Slm66018 			if ((status = ldc_mem_info(mhdl, &minfo)) != 0) {
27193af08d82Slm66018 				DMSG(vdc, 0,
27203af08d82Slm66018 				    "ldc_mem_info returned an error: %d\n",
27213af08d82Slm66018 				    status);
27223af08d82Slm66018 
27233af08d82Slm66018 				/*
27243af08d82Slm66018 				 * This must mean that the mem handle
27253af08d82Slm66018 				 * is not valid. Clear it out so that
27263af08d82Slm66018 				 * no one tries to use it.
27273af08d82Slm66018 				 */
27283af08d82Slm66018 				ldep->desc_mhdl = NULL;
27293af08d82Slm66018 				continue;
27303af08d82Slm66018 			}
27313af08d82Slm66018 
27323af08d82Slm66018 			if (minfo.status == LDC_BOUND) {
27333af08d82Slm66018 				(void) ldc_mem_unbind_handle(mhdl);
27343af08d82Slm66018 			}
27353af08d82Slm66018 
27361ae08745Sheppo 			(void) ldc_mem_free_handle(mhdl);
27373af08d82Slm66018 
27383af08d82Slm66018 			ldep->desc_mhdl = NULL;
27391ae08745Sheppo 		}
27401ae08745Sheppo 		vdc->initialized &= ~VDC_DRING_ENTRY;
27411ae08745Sheppo 	}
27421ae08745Sheppo 
27431ae08745Sheppo 	if (vdc->initialized & VDC_DRING_LOCAL) {
27443af08d82Slm66018 		DMSG(vdc, 0, "[%d] Freeing Local DRing\n", vdc->instance);
27451ae08745Sheppo 		kmem_free(vdc->local_dring,
2746e1ebb9ecSlm66018 		    vdc->dring_len * sizeof (vdc_local_desc_t));
27471ae08745Sheppo 		vdc->initialized &= ~VDC_DRING_LOCAL;
27481ae08745Sheppo 	}
27491ae08745Sheppo 
27501ae08745Sheppo 	if (vdc->initialized & VDC_DRING_BOUND) {
27513af08d82Slm66018 		DMSG(vdc, 0, "[%d] Unbinding DRing\n", vdc->instance);
27528cd10891Snarayan 		status = ldc_mem_dring_unbind(vdc->dring_hdl);
27531ae08745Sheppo 		if (status == 0) {
27541ae08745Sheppo 			vdc->initialized &= ~VDC_DRING_BOUND;
27551ae08745Sheppo 		} else {
27563af08d82Slm66018 			DMSG(vdc, 0, "[%d] Error %d unbinding DRing %lx",
27578cd10891Snarayan 			    vdc->instance, status, vdc->dring_hdl);
27581ae08745Sheppo 		}
27593af08d82Slm66018 		kmem_free(vdc->dring_cookie, sizeof (ldc_mem_cookie_t));
27601ae08745Sheppo 	}
27611ae08745Sheppo 
27621ae08745Sheppo 	if (vdc->initialized & VDC_DRING_INIT) {
27633af08d82Slm66018 		DMSG(vdc, 0, "[%d] Destroying DRing\n", vdc->instance);
27648cd10891Snarayan 		status = ldc_mem_dring_destroy(vdc->dring_hdl);
27651ae08745Sheppo 		if (status == 0) {
27668cd10891Snarayan 			vdc->dring_hdl = NULL;
27671ae08745Sheppo 			bzero(&vdc->dring_mem_info, sizeof (ldc_mem_info_t));
27681ae08745Sheppo 			vdc->initialized &= ~VDC_DRING_INIT;
27691ae08745Sheppo 		} else {
27703af08d82Slm66018 			DMSG(vdc, 0, "[%d] Error %d destroying DRing (%lx)",
27718cd10891Snarayan 			    vdc->instance, status, vdc->dring_hdl);
27721ae08745Sheppo 		}
27731ae08745Sheppo 	}
27741ae08745Sheppo }
27751ae08745Sheppo 
27761ae08745Sheppo /*
27773af08d82Slm66018  * Function:
277890e2f9dcSlm66018  *	vdc_map_to_shared_dring()
27791ae08745Sheppo  *
27801ae08745Sheppo  * Description:
27813af08d82Slm66018  *	Copy contents of the local descriptor to the shared
27823af08d82Slm66018  *	memory descriptor.
27831ae08745Sheppo  *
27843af08d82Slm66018  * Arguments:
27853af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
27863af08d82Slm66018  *	idx	- descriptor ring index
27873af08d82Slm66018  *
27883af08d82Slm66018  * Return Code:
27893af08d82Slm66018  *	None
27901ae08745Sheppo  */
27911ae08745Sheppo static int
27923af08d82Slm66018 vdc_map_to_shared_dring(vdc_t *vdcp, int idx)
27931ae08745Sheppo {
27943af08d82Slm66018 	vdc_local_desc_t	*ldep;
27953af08d82Slm66018 	vd_dring_entry_t	*dep;
27963af08d82Slm66018 	int			rv;
27971ae08745Sheppo 
27983af08d82Slm66018 	ldep = &(vdcp->local_dring[idx]);
27991ae08745Sheppo 
28003af08d82Slm66018 	/* for now leave in the old pop_mem_hdl stuff */
28013af08d82Slm66018 	if (ldep->nbytes > 0) {
28023af08d82Slm66018 		rv = vdc_populate_mem_hdl(vdcp, ldep);
28033af08d82Slm66018 		if (rv) {
28043af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Cannot populate mem handle\n",
28053af08d82Slm66018 			    vdcp->instance);
28063af08d82Slm66018 			return (rv);
28073af08d82Slm66018 		}
28083af08d82Slm66018 	}
28091ae08745Sheppo 
28103af08d82Slm66018 	/*
28113af08d82Slm66018 	 * fill in the data details into the DRing
28123af08d82Slm66018 	 */
2813d10e4ef2Snarayan 	dep = ldep->dep;
28141ae08745Sheppo 	ASSERT(dep != NULL);
28151ae08745Sheppo 
28163af08d82Slm66018 	dep->payload.req_id = VDC_GET_NEXT_REQ_ID(vdcp);
28173af08d82Slm66018 	dep->payload.operation = ldep->operation;
28183af08d82Slm66018 	dep->payload.addr = ldep->offset;
28193af08d82Slm66018 	dep->payload.nbytes = ldep->nbytes;
2820055d7c80Scarlsonj 	dep->payload.status = (uint32_t)-1;	/* vds will set valid value */
28213af08d82Slm66018 	dep->payload.slice = ldep->slice;
28223af08d82Slm66018 	dep->hdr.dstate = VIO_DESC_READY;
28233af08d82Slm66018 	dep->hdr.ack = 1;		/* request an ACK for every message */
28241ae08745Sheppo 
28253af08d82Slm66018 	return (0);
28261ae08745Sheppo }
28271ae08745Sheppo 
28281ae08745Sheppo /*
28291ae08745Sheppo  * Function:
28303af08d82Slm66018  *	vdc_send_request
28313af08d82Slm66018  *
28323af08d82Slm66018  * Description:
28333af08d82Slm66018  *	This routine writes the data to be transmitted to vds into the
28343af08d82Slm66018  *	descriptor, notifies vds that the ring has been updated and
28353af08d82Slm66018  *	then waits for the request to be processed.
28363af08d82Slm66018  *
28373af08d82Slm66018  * Arguments:
28383af08d82Slm66018  *	vdcp	  - the soft state pointer
28393af08d82Slm66018  *	operation - operation we want vds to perform (VD_OP_XXX)
28403af08d82Slm66018  *	addr	  - address of data buf to be read/written.
28413af08d82Slm66018  *	nbytes	  - number of bytes to read/write
28423af08d82Slm66018  *	slice	  - the disk slice this request is for
28433af08d82Slm66018  *	offset	  - relative disk offset
28443af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
28453af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
28463af08d82Slm66018  *			. mode for ioctl(9e)
28473af08d82Slm66018  *			. LP64 diskaddr_t (block I/O)
28483af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
28493af08d82Slm66018  *
28503af08d82Slm66018  * Return Codes:
28513af08d82Slm66018  *	0
28523af08d82Slm66018  *	ENXIO
28533af08d82Slm66018  */
28543af08d82Slm66018 static int
28553af08d82Slm66018 vdc_send_request(vdc_t *vdcp, int operation, caddr_t addr,
28563af08d82Slm66018     size_t nbytes, int slice, diskaddr_t offset, int cb_type,
28573af08d82Slm66018     void *cb_arg, vio_desc_direction_t dir)
28583af08d82Slm66018 {
2859366a92acSlm66018 	int	rv = 0;
2860366a92acSlm66018 
28613af08d82Slm66018 	ASSERT(vdcp != NULL);
286287a7269eSachartre 	ASSERT(slice == VD_SLICE_NONE || slice < V_NUMPAR);
28633af08d82Slm66018 
28643af08d82Slm66018 	mutex_enter(&vdcp->lock);
28653af08d82Slm66018 
2866366a92acSlm66018 	/*
2867366a92acSlm66018 	 * If this is a block read/write operation we update the I/O statistics
2868366a92acSlm66018 	 * to indicate that the request is being put on the waitq to be
2869366a92acSlm66018 	 * serviced.
2870366a92acSlm66018 	 *
2871366a92acSlm66018 	 * We do it here (a common routine for both synchronous and strategy
2872366a92acSlm66018 	 * calls) for performance reasons - we are already holding vdc->lock
2873366a92acSlm66018 	 * so there is no extra locking overhead. We would have to explicitly
2874366a92acSlm66018 	 * grab the 'lock' mutex to update the stats if we were to do this
2875366a92acSlm66018 	 * higher up the stack in vdc_strategy() et. al.
2876366a92acSlm66018 	 */
2877366a92acSlm66018 	if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) {
2878366a92acSlm66018 		DTRACE_IO1(start, buf_t *, cb_arg);
287990e2f9dcSlm66018 		VD_KSTAT_WAITQ_ENTER(vdcp);
2880366a92acSlm66018 	}
2881366a92acSlm66018 
28823af08d82Slm66018 	do {
28833c96341aSnarayan 		while (vdcp->state != VDC_STATE_RUNNING) {
28843af08d82Slm66018 
28853c96341aSnarayan 			/* return error if detaching */
28863c96341aSnarayan 			if (vdcp->state == VDC_STATE_DETACH) {
2887366a92acSlm66018 				rv = ENXIO;
2888366a92acSlm66018 				goto done;
28893c96341aSnarayan 			}
2890655fd6a9Sachartre 
2891655fd6a9Sachartre 			/* fail request if connection timeout is reached */
2892655fd6a9Sachartre 			if (vdcp->ctimeout_reached) {
2893366a92acSlm66018 				rv = EIO;
2894366a92acSlm66018 				goto done;
2895655fd6a9Sachartre 			}
2896655fd6a9Sachartre 
28972f5224aeSachartre 			/*
28982f5224aeSachartre 			 * If we are panicking and the disk is not ready then
28992f5224aeSachartre 			 * we can't send any request because we can't complete
29002f5224aeSachartre 			 * the handshake now.
29012f5224aeSachartre 			 */
29022f5224aeSachartre 			if (ddi_in_panic()) {
2903366a92acSlm66018 				rv = EIO;
2904366a92acSlm66018 				goto done;
29052f5224aeSachartre 			}
29062f5224aeSachartre 
2907655fd6a9Sachartre 			cv_wait(&vdcp->running_cv, &vdcp->lock);
29083c96341aSnarayan 		}
29093c96341aSnarayan 
29103af08d82Slm66018 	} while (vdc_populate_descriptor(vdcp, operation, addr,
29113af08d82Slm66018 	    nbytes, slice, offset, cb_type, cb_arg, dir));
29123af08d82Slm66018 
2913366a92acSlm66018 done:
2914366a92acSlm66018 	/*
2915366a92acSlm66018 	 * If this is a block read/write we update the I/O statistics kstat
2916366a92acSlm66018 	 * to indicate that this request has been placed on the queue for
2917366a92acSlm66018 	 * processing (i.e sent to the vDisk server) - iostat(1M) will
2918366a92acSlm66018 	 * report the time waiting for the vDisk server under the %b column
2919366a92acSlm66018 	 * In the case of an error we simply take it off the wait queue.
2920366a92acSlm66018 	 */
2921366a92acSlm66018 	if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) {
2922366a92acSlm66018 		if (rv == 0) {
292390e2f9dcSlm66018 			VD_KSTAT_WAITQ_TO_RUNQ(vdcp);
2924366a92acSlm66018 			DTRACE_PROBE1(send, buf_t *, cb_arg);
2925366a92acSlm66018 		} else {
2926366a92acSlm66018 			VD_UPDATE_ERR_STATS(vdcp, vd_transerrs);
292790e2f9dcSlm66018 			VD_KSTAT_WAITQ_EXIT(vdcp);
2928366a92acSlm66018 			DTRACE_IO1(done, buf_t *, cb_arg);
2929366a92acSlm66018 		}
2930366a92acSlm66018 	}
2931366a92acSlm66018 
29323af08d82Slm66018 	mutex_exit(&vdcp->lock);
2933366a92acSlm66018 
2934366a92acSlm66018 	return (rv);
29353af08d82Slm66018 }
29363af08d82Slm66018 
29373af08d82Slm66018 
29383af08d82Slm66018 /*
29393af08d82Slm66018  * Function:
29401ae08745Sheppo  *	vdc_populate_descriptor
29411ae08745Sheppo  *
29421ae08745Sheppo  * Description:
29431ae08745Sheppo  *	This routine writes the data to be transmitted to vds into the
29441ae08745Sheppo  *	descriptor, notifies vds that the ring has been updated and
29451ae08745Sheppo  *	then waits for the request to be processed.
29461ae08745Sheppo  *
29471ae08745Sheppo  * Arguments:
29483af08d82Slm66018  *	vdcp	  - the soft state pointer
29491ae08745Sheppo  *	operation - operation we want vds to perform (VD_OP_XXX)
29503af08d82Slm66018  *	addr	  - address of data buf to be read/written.
29513af08d82Slm66018  *	nbytes	  - number of bytes to read/write
29523af08d82Slm66018  *	slice	  - the disk slice this request is for
29533af08d82Slm66018  *	offset	  - relative disk offset
29543af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
29553af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
29561ae08745Sheppo  *			. mode for ioctl(9e)
29571ae08745Sheppo  *			. LP64 diskaddr_t (block I/O)
29583af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
29591ae08745Sheppo  *
29601ae08745Sheppo  * Return Codes:
29611ae08745Sheppo  *	0
29621ae08745Sheppo  *	EAGAIN
296317cadca8Slm66018  *	ECONNRESET
29641ae08745Sheppo  *	ENXIO
29651ae08745Sheppo  */
29661ae08745Sheppo static int
29673af08d82Slm66018 vdc_populate_descriptor(vdc_t *vdcp, int operation, caddr_t addr,
29683af08d82Slm66018     size_t nbytes, int slice, diskaddr_t offset, int cb_type,
29693af08d82Slm66018     void *cb_arg, vio_desc_direction_t dir)
29701ae08745Sheppo {
29713af08d82Slm66018 	vdc_local_desc_t	*local_dep = NULL; /* Local Dring Pointer */
29723af08d82Slm66018 	int			idx;		/* Index of DRing entry used */
29733af08d82Slm66018 	int			next_idx;
29741ae08745Sheppo 	vio_dring_msg_t		dmsg;
29753af08d82Slm66018 	size_t			msglen;
29768e6a2a04Slm66018 	int			rv;
29771ae08745Sheppo 
29783af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdcp->lock));
29793af08d82Slm66018 	vdcp->threads_pending++;
29803af08d82Slm66018 loop:
29813af08d82Slm66018 	DMSG(vdcp, 2, ": dring_curr_idx = %d\n", vdcp->dring_curr_idx);
29821ae08745Sheppo 
29833af08d82Slm66018 	/* Get next available D-Ring entry */
29843af08d82Slm66018 	idx = vdcp->dring_curr_idx;
29853af08d82Slm66018 	local_dep = &(vdcp->local_dring[idx]);
29861ae08745Sheppo 
29873af08d82Slm66018 	if (!local_dep->is_free) {
29883af08d82Slm66018 		DMSG(vdcp, 2, "[%d]: dring full - waiting for space\n",
29893af08d82Slm66018 		    vdcp->instance);
29903af08d82Slm66018 		cv_wait(&vdcp->dring_free_cv, &vdcp->lock);
29913af08d82Slm66018 		if (vdcp->state == VDC_STATE_RUNNING ||
29923af08d82Slm66018 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
29933af08d82Slm66018 			goto loop;
29943af08d82Slm66018 		}
29953af08d82Slm66018 		vdcp->threads_pending--;
29963af08d82Slm66018 		return (ECONNRESET);
29971ae08745Sheppo 	}
29981ae08745Sheppo 
29993af08d82Slm66018 	next_idx = idx + 1;
30003af08d82Slm66018 	if (next_idx >= vdcp->dring_len)
30013af08d82Slm66018 		next_idx = 0;
30023af08d82Slm66018 	vdcp->dring_curr_idx = next_idx;
30031ae08745Sheppo 
30043af08d82Slm66018 	ASSERT(local_dep->is_free);
30051ae08745Sheppo 
30063af08d82Slm66018 	local_dep->operation = operation;
3007d10e4ef2Snarayan 	local_dep->addr = addr;
30083af08d82Slm66018 	local_dep->nbytes = nbytes;
30093af08d82Slm66018 	local_dep->slice = slice;
30103af08d82Slm66018 	local_dep->offset = offset;
30113af08d82Slm66018 	local_dep->cb_type = cb_type;
30123af08d82Slm66018 	local_dep->cb_arg = cb_arg;
30133af08d82Slm66018 	local_dep->dir = dir;
30143af08d82Slm66018 
30153af08d82Slm66018 	local_dep->is_free = B_FALSE;
30163af08d82Slm66018 
30173af08d82Slm66018 	rv = vdc_map_to_shared_dring(vdcp, idx);
30183af08d82Slm66018 	if (rv) {
30193af08d82Slm66018 		DMSG(vdcp, 0, "[%d]: cannot bind memory - waiting ..\n",
30203af08d82Slm66018 		    vdcp->instance);
30213af08d82Slm66018 		/* free the descriptor */
30223af08d82Slm66018 		local_dep->is_free = B_TRUE;
30233af08d82Slm66018 		vdcp->dring_curr_idx = idx;
30243af08d82Slm66018 		cv_wait(&vdcp->membind_cv, &vdcp->lock);
30253af08d82Slm66018 		if (vdcp->state == VDC_STATE_RUNNING ||
30263af08d82Slm66018 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
30273af08d82Slm66018 			goto loop;
30281ae08745Sheppo 		}
30293af08d82Slm66018 		vdcp->threads_pending--;
30303af08d82Slm66018 		return (ECONNRESET);
30311ae08745Sheppo 	}
30321ae08745Sheppo 
30331ae08745Sheppo 	/*
30341ae08745Sheppo 	 * Send a msg with the DRing details to vds
30351ae08745Sheppo 	 */
30361ae08745Sheppo 	VIO_INIT_DRING_DATA_TAG(dmsg);
30373af08d82Slm66018 	VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdcp);
30383af08d82Slm66018 	dmsg.dring_ident = vdcp->dring_ident;
30391ae08745Sheppo 	dmsg.start_idx = idx;
30401ae08745Sheppo 	dmsg.end_idx = idx;
30413af08d82Slm66018 	vdcp->seq_num++;
30421ae08745Sheppo 
3043366a92acSlm66018 	DTRACE_PROBE2(populate, int, vdcp->instance,
3044366a92acSlm66018 	    vdc_local_desc_t *, local_dep);
30453af08d82Slm66018 	DMSG(vdcp, 2, "ident=0x%lx, st=%u, end=%u, seq=%ld\n",
30463af08d82Slm66018 	    vdcp->dring_ident, dmsg.start_idx, dmsg.end_idx, dmsg.seq_num);
30471ae08745Sheppo 
30483af08d82Slm66018 	/*
30493af08d82Slm66018 	 * note we're still holding the lock here to
30503af08d82Slm66018 	 * make sure the message goes out in order !!!...
30513af08d82Slm66018 	 */
30523af08d82Slm66018 	msglen = sizeof (dmsg);
30533af08d82Slm66018 	rv = vdc_send(vdcp, (caddr_t)&dmsg, &msglen);
30543af08d82Slm66018 	switch (rv) {
30553af08d82Slm66018 	case ECONNRESET:
30563af08d82Slm66018 		/*
30573af08d82Slm66018 		 * vdc_send initiates the reset on failure.
30583af08d82Slm66018 		 * Since the transaction has already been put
30593af08d82Slm66018 		 * on the local dring, it will automatically get
30603af08d82Slm66018 		 * retried when the channel is reset. Given that,
30613af08d82Slm66018 		 * it is ok to just return success even though the
30623af08d82Slm66018 		 * send failed.
30633af08d82Slm66018 		 */
30643af08d82Slm66018 		rv = 0;
30653af08d82Slm66018 		break;
3066d10e4ef2Snarayan 
30673af08d82Slm66018 	case 0: /* EOK */
30683af08d82Slm66018 		DMSG(vdcp, 1, "sent via LDC: rv=%d\n", rv);
30693af08d82Slm66018 		break;
3070d10e4ef2Snarayan 
30713af08d82Slm66018 	default:
30723af08d82Slm66018 		goto cleanup_and_exit;
30733af08d82Slm66018 	}
3074e1ebb9ecSlm66018 
30753af08d82Slm66018 	vdcp->threads_pending--;
30763af08d82Slm66018 	return (rv);
30773af08d82Slm66018 
30783af08d82Slm66018 cleanup_and_exit:
30793af08d82Slm66018 	DMSG(vdcp, 0, "unexpected error, rv=%d\n", rv);
30803af08d82Slm66018 	return (ENXIO);
30811ae08745Sheppo }
30821ae08745Sheppo 
30831ae08745Sheppo /*
30843af08d82Slm66018  * Function:
30853af08d82Slm66018  *	vdc_do_sync_op
30863af08d82Slm66018  *
30873af08d82Slm66018  * Description:
30883af08d82Slm66018  * 	Wrapper around vdc_populate_descriptor that blocks until the
30893af08d82Slm66018  * 	response to the message is available.
30903af08d82Slm66018  *
30913af08d82Slm66018  * Arguments:
30923af08d82Slm66018  *	vdcp	  - the soft state pointer
30933af08d82Slm66018  *	operation - operation we want vds to perform (VD_OP_XXX)
30943af08d82Slm66018  *	addr	  - address of data buf to be read/written.
30953af08d82Slm66018  *	nbytes	  - number of bytes to read/write
30963af08d82Slm66018  *	slice	  - the disk slice this request is for
30973af08d82Slm66018  *	offset	  - relative disk offset
30983af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
30993af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
31003af08d82Slm66018  *			. mode for ioctl(9e)
31013af08d82Slm66018  *			. LP64 diskaddr_t (block I/O)
31023af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
31032f5224aeSachartre  *	rconflict - check for reservation conflict in case of failure
31042f5224aeSachartre  *
31052f5224aeSachartre  * rconflict should be set to B_TRUE by most callers. Callers invoking the
31062f5224aeSachartre  * VD_OP_SCSICMD operation can set rconflict to B_FALSE if they check the
31072f5224aeSachartre  * result of a successful operation with vd_scsi_status().
31083af08d82Slm66018  *
31093af08d82Slm66018  * Return Codes:
31103af08d82Slm66018  *	0
31113af08d82Slm66018  *	EAGAIN
31123af08d82Slm66018  *	EFAULT
31133af08d82Slm66018  *	ENXIO
31143af08d82Slm66018  *	EIO
31150a55fbb7Slm66018  */
31163af08d82Slm66018 static int
31173af08d82Slm66018 vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr, size_t nbytes,
31183af08d82Slm66018     int slice, diskaddr_t offset, int cb_type, void *cb_arg,
31192f5224aeSachartre     vio_desc_direction_t dir, boolean_t rconflict)
31203af08d82Slm66018 {
31213af08d82Slm66018 	int status;
31222f5224aeSachartre 	vdc_io_t *vio;
31232f5224aeSachartre 	boolean_t check_resv_conflict = B_FALSE;
31243af08d82Slm66018 
31253af08d82Slm66018 	ASSERT(cb_type == CB_SYNC);
31261ae08745Sheppo 
31271ae08745Sheppo 	/*
31283af08d82Slm66018 	 * Grab the lock, if blocked wait until the server
31293af08d82Slm66018 	 * response causes us to wake up again.
31303af08d82Slm66018 	 */
31313af08d82Slm66018 	mutex_enter(&vdcp->lock);
31323af08d82Slm66018 	vdcp->sync_op_cnt++;
31333af08d82Slm66018 	while (vdcp->sync_op_blocked && vdcp->state != VDC_STATE_DETACH)
31343af08d82Slm66018 		cv_wait(&vdcp->sync_blocked_cv, &vdcp->lock);
31353af08d82Slm66018 
31363af08d82Slm66018 	if (vdcp->state == VDC_STATE_DETACH) {
31373af08d82Slm66018 		cv_broadcast(&vdcp->sync_blocked_cv);
31383af08d82Slm66018 		vdcp->sync_op_cnt--;
31393af08d82Slm66018 		mutex_exit(&vdcp->lock);
31403af08d82Slm66018 		return (ENXIO);
31413af08d82Slm66018 	}
31423af08d82Slm66018 
31433af08d82Slm66018 	/* now block anyone other thread entering after us */
31443af08d82Slm66018 	vdcp->sync_op_blocked = B_TRUE;
31453af08d82Slm66018 	vdcp->sync_op_pending = B_TRUE;
31463af08d82Slm66018 	mutex_exit(&vdcp->lock);
31473af08d82Slm66018 
3148655fd6a9Sachartre 	status = vdc_send_request(vdcp, operation, addr,
31493af08d82Slm66018 	    nbytes, slice, offset, cb_type, cb_arg, dir);
31503af08d82Slm66018 
3151655fd6a9Sachartre 	mutex_enter(&vdcp->lock);
3152655fd6a9Sachartre 
3153655fd6a9Sachartre 	if (status != 0) {
3154655fd6a9Sachartre 		vdcp->sync_op_pending = B_FALSE;
3155655fd6a9Sachartre 	} else {
31563af08d82Slm66018 		/*
31573af08d82Slm66018 		 * block until our transaction completes.
31583af08d82Slm66018 		 * Also anyone else waiting also gets to go next.
31593af08d82Slm66018 		 */
31603af08d82Slm66018 		while (vdcp->sync_op_pending && vdcp->state != VDC_STATE_DETACH)
31613af08d82Slm66018 			cv_wait(&vdcp->sync_pending_cv, &vdcp->lock);
31623af08d82Slm66018 
3163655fd6a9Sachartre 		DMSG(vdcp, 2, ": operation returned %d\n",
3164655fd6a9Sachartre 		    vdcp->sync_op_status);
31653c96341aSnarayan 		if (vdcp->state == VDC_STATE_DETACH) {
31663c96341aSnarayan 			vdcp->sync_op_pending = B_FALSE;
31673af08d82Slm66018 			status = ENXIO;
31683c96341aSnarayan 		} else {
31693af08d82Slm66018 			status = vdcp->sync_op_status;
31702f5224aeSachartre 			if (status != 0 && vdcp->failfast_interval != 0) {
31712f5224aeSachartre 				/*
31722f5224aeSachartre 				 * Operation has failed and failfast is enabled.
31732f5224aeSachartre 				 * We need to check if the failure is due to a
31742f5224aeSachartre 				 * reservation conflict if this was requested.
31752f5224aeSachartre 				 */
31762f5224aeSachartre 				check_resv_conflict = rconflict;
31772f5224aeSachartre 			}
31782f5224aeSachartre 
31793c96341aSnarayan 		}
3180655fd6a9Sachartre 	}
31813c96341aSnarayan 
31823af08d82Slm66018 	vdcp->sync_op_status = 0;
31833af08d82Slm66018 	vdcp->sync_op_blocked = B_FALSE;
31843af08d82Slm66018 	vdcp->sync_op_cnt--;
31853af08d82Slm66018 
31863af08d82Slm66018 	/* signal the next waiting thread */
31873af08d82Slm66018 	cv_signal(&vdcp->sync_blocked_cv);
31882f5224aeSachartre 
31892f5224aeSachartre 	/*
31902f5224aeSachartre 	 * We have to check for reservation conflict after unblocking sync
31912f5224aeSachartre 	 * operations because some sync operations will be used to do this
31922f5224aeSachartre 	 * check.
31932f5224aeSachartre 	 */
31942f5224aeSachartre 	if (check_resv_conflict) {
31952f5224aeSachartre 		vio = vdc_failfast_io_queue(vdcp, NULL);
31962f5224aeSachartre 		while (vio->vio_qtime != 0)
31972f5224aeSachartre 			cv_wait(&vdcp->failfast_io_cv, &vdcp->lock);
31982f5224aeSachartre 		kmem_free(vio, sizeof (vdc_io_t));
31992f5224aeSachartre 	}
32002f5224aeSachartre 
32013af08d82Slm66018 	mutex_exit(&vdcp->lock);
32023af08d82Slm66018 
32033af08d82Slm66018 	return (status);
32043af08d82Slm66018 }
32053af08d82Slm66018 
32063af08d82Slm66018 
32073af08d82Slm66018 /*
32083af08d82Slm66018  * Function:
32093af08d82Slm66018  *	vdc_drain_response()
32103af08d82Slm66018  *
32113af08d82Slm66018  * Description:
32121ae08745Sheppo  * 	When a guest is panicking, the completion of requests needs to be
32131ae08745Sheppo  * 	handled differently because interrupts are disabled and vdc
32141ae08745Sheppo  * 	will not get messages. We have to poll for the messages instead.
32153af08d82Slm66018  *
3216366a92acSlm66018  *	Note: since we don't have a buf_t available we cannot implement
3217366a92acSlm66018  *	the io:::done DTrace probe in this specific case.
3218366a92acSlm66018  *
32193af08d82Slm66018  * Arguments:
32203af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
32213af08d82Slm66018  *
32223af08d82Slm66018  * Return Code:
32233af08d82Slm66018  *	0	- Success
32241ae08745Sheppo  */
32253af08d82Slm66018 static int
32263af08d82Slm66018 vdc_drain_response(vdc_t *vdc)
32273af08d82Slm66018 {
32283af08d82Slm66018 	int 			rv, idx, retries;
32293af08d82Slm66018 	size_t			msglen;
32303af08d82Slm66018 	vdc_local_desc_t 	*ldep = NULL;	/* Local Dring Entry Pointer */
32313af08d82Slm66018 	vio_dring_msg_t		dmsg;
32323af08d82Slm66018 
32333af08d82Slm66018 	mutex_enter(&vdc->lock);
32343af08d82Slm66018 
32351ae08745Sheppo 	retries = 0;
32361ae08745Sheppo 	for (;;) {
32371ae08745Sheppo 		msglen = sizeof (dmsg);
32388cd10891Snarayan 		rv = ldc_read(vdc->curr_server->ldc_handle, (caddr_t)&dmsg,
32398cd10891Snarayan 		    &msglen);
32408e6a2a04Slm66018 		if (rv) {
32418e6a2a04Slm66018 			rv = EINVAL;
32421ae08745Sheppo 			break;
32431ae08745Sheppo 		}
32441ae08745Sheppo 
32451ae08745Sheppo 		/*
32461ae08745Sheppo 		 * if there are no packets wait and check again
32471ae08745Sheppo 		 */
32488e6a2a04Slm66018 		if ((rv == 0) && (msglen == 0)) {
32491ae08745Sheppo 			if (retries++ > vdc_dump_retries) {
32508e6a2a04Slm66018 				rv = EAGAIN;
32511ae08745Sheppo 				break;
32521ae08745Sheppo 			}
32531ae08745Sheppo 
3254d10e4ef2Snarayan 			drv_usecwait(vdc_usec_timeout_dump);
32551ae08745Sheppo 			continue;
32561ae08745Sheppo 		}
32571ae08745Sheppo 
32581ae08745Sheppo 		/*
32591ae08745Sheppo 		 * Ignore all messages that are not ACKs/NACKs to
32601ae08745Sheppo 		 * DRing requests.
32611ae08745Sheppo 		 */
32621ae08745Sheppo 		if ((dmsg.tag.vio_msgtype != VIO_TYPE_DATA) ||
32631ae08745Sheppo 		    (dmsg.tag.vio_subtype_env != VIO_DRING_DATA)) {
32643af08d82Slm66018 			DMSG(vdc, 0, "discard pkt: type=%d sub=%d env=%d\n",
32651ae08745Sheppo 			    dmsg.tag.vio_msgtype,
32661ae08745Sheppo 			    dmsg.tag.vio_subtype,
32671ae08745Sheppo 			    dmsg.tag.vio_subtype_env);
32681ae08745Sheppo 			continue;
32691ae08745Sheppo 		}
32701ae08745Sheppo 
32711ae08745Sheppo 		/*
32723af08d82Slm66018 		 * set the appropriate return value for the current request.
32731ae08745Sheppo 		 */
32741ae08745Sheppo 		switch (dmsg.tag.vio_subtype) {
32751ae08745Sheppo 		case VIO_SUBTYPE_ACK:
32768e6a2a04Slm66018 			rv = 0;
32771ae08745Sheppo 			break;
32781ae08745Sheppo 		case VIO_SUBTYPE_NACK:
32798e6a2a04Slm66018 			rv = EAGAIN;
32801ae08745Sheppo 			break;
32811ae08745Sheppo 		default:
32821ae08745Sheppo 			continue;
32831ae08745Sheppo 		}
32841ae08745Sheppo 
32853af08d82Slm66018 		idx = dmsg.start_idx;
32863af08d82Slm66018 		if (idx >= vdc->dring_len) {
32873af08d82Slm66018 			DMSG(vdc, 0, "[%d] Bogus ack data : start %d\n",
3288e1ebb9ecSlm66018 			    vdc->instance, idx);
32893af08d82Slm66018 			continue;
32901ae08745Sheppo 		}
32913af08d82Slm66018 		ldep = &vdc->local_dring[idx];
32923af08d82Slm66018 		if (ldep->dep->hdr.dstate != VIO_DESC_DONE) {
32933af08d82Slm66018 			DMSG(vdc, 0, "[%d] Entry @ %d - state !DONE %d\n",
32943af08d82Slm66018 			    vdc->instance, idx, ldep->dep->hdr.dstate);
32951ae08745Sheppo 			continue;
32961ae08745Sheppo 		}
32971ae08745Sheppo 
32983af08d82Slm66018 		DMSG(vdc, 1, "[%d] Depopulating idx=%d state=%d\n",
32993af08d82Slm66018 		    vdc->instance, idx, ldep->dep->hdr.dstate);
3300366a92acSlm66018 
33013af08d82Slm66018 		rv = vdc_depopulate_descriptor(vdc, idx);
33023af08d82Slm66018 		if (rv) {
33033af08d82Slm66018 			DMSG(vdc, 0,
33043af08d82Slm66018 			    "[%d] Entry @ %d - depopulate failed ..\n",
33053af08d82Slm66018 			    vdc->instance, idx);
33061ae08745Sheppo 		}
33071ae08745Sheppo 
33083af08d82Slm66018 		/* if this is the last descriptor - break out of loop */
33093af08d82Slm66018 		if ((idx + 1) % vdc->dring_len == vdc->dring_curr_idx)
33103af08d82Slm66018 			break;
33113af08d82Slm66018 	}
33123af08d82Slm66018 
33133af08d82Slm66018 	mutex_exit(&vdc->lock);
33143af08d82Slm66018 	DMSG(vdc, 0, "End idx=%d\n", idx);
33153af08d82Slm66018 
33163af08d82Slm66018 	return (rv);
33171ae08745Sheppo }
33181ae08745Sheppo 
33191ae08745Sheppo 
33200a55fbb7Slm66018 /*
33210a55fbb7Slm66018  * Function:
33220a55fbb7Slm66018  *	vdc_depopulate_descriptor()
33230a55fbb7Slm66018  *
33240a55fbb7Slm66018  * Description:
33250a55fbb7Slm66018  *
33260a55fbb7Slm66018  * Arguments:
33270a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
33280a55fbb7Slm66018  *	idx	- Index of the Descriptor Ring entry being modified
33290a55fbb7Slm66018  *
33300a55fbb7Slm66018  * Return Code:
33310a55fbb7Slm66018  *	0	- Success
33320a55fbb7Slm66018  */
33331ae08745Sheppo static int
33341ae08745Sheppo vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx)
33351ae08745Sheppo {
33361ae08745Sheppo 	vd_dring_entry_t *dep = NULL;		/* Dring Entry Pointer */
33371ae08745Sheppo 	vdc_local_desc_t *ldep = NULL;		/* Local Dring Entry Pointer */
33381ae08745Sheppo 	int		status = ENXIO;
33398e6a2a04Slm66018 	int		rv = 0;
33401ae08745Sheppo 
33411ae08745Sheppo 	ASSERT(vdc != NULL);
3342e1ebb9ecSlm66018 	ASSERT(idx < vdc->dring_len);
33431ae08745Sheppo 	ldep = &vdc->local_dring[idx];
33441ae08745Sheppo 	ASSERT(ldep != NULL);
33453af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdc->lock));
33463af08d82Slm66018 
3347366a92acSlm66018 	DTRACE_PROBE2(depopulate, int, vdc->instance, vdc_local_desc_t *, ldep);
33483af08d82Slm66018 	DMSG(vdc, 2, ": idx = %d\n", idx);
3349366a92acSlm66018 
33501ae08745Sheppo 	dep = ldep->dep;
33511ae08745Sheppo 	ASSERT(dep != NULL);
3352e1ebb9ecSlm66018 	ASSERT((dep->hdr.dstate == VIO_DESC_DONE) ||
3353e1ebb9ecSlm66018 	    (dep->payload.status == ECANCELED));
33541ae08745Sheppo 
3355e1ebb9ecSlm66018 	VDC_MARK_DRING_ENTRY_FREE(vdc, idx);
33563af08d82Slm66018 
33573af08d82Slm66018 	ldep->is_free = B_TRUE;
33581ae08745Sheppo 	status = dep->payload.status;
3359205eeb1aSlm66018 	DMSG(vdc, 2, ": is_free = %d : status = %d\n", ldep->is_free, status);
33601ae08745Sheppo 
3361eff7243fSlm66018 	/*
3362eff7243fSlm66018 	 * If no buffers were used to transfer information to the server when
3363eff7243fSlm66018 	 * populating the descriptor then no memory handles need to be unbound
3364eff7243fSlm66018 	 * and we can return now.
3365eff7243fSlm66018 	 */
3366eff7243fSlm66018 	if (ldep->nbytes == 0) {
3367eff7243fSlm66018 		cv_signal(&vdc->dring_free_cv);
33688e6a2a04Slm66018 		return (status);
3369eff7243fSlm66018 	}
33708e6a2a04Slm66018 
33711ae08745Sheppo 	/*
33721ae08745Sheppo 	 * If the upper layer passed in a misaligned address we copied the
33731ae08745Sheppo 	 * data into an aligned buffer before sending it to LDC - we now
33741ae08745Sheppo 	 * copy it back to the original buffer.
33751ae08745Sheppo 	 */
33761ae08745Sheppo 	if (ldep->align_addr) {
33771ae08745Sheppo 		ASSERT(ldep->addr != NULL);
33781ae08745Sheppo 
33793c96341aSnarayan 		if (dep->payload.nbytes > 0)
33803c96341aSnarayan 			bcopy(ldep->align_addr, ldep->addr,
33813c96341aSnarayan 			    dep->payload.nbytes);
33821ae08745Sheppo 		kmem_free(ldep->align_addr,
33833c96341aSnarayan 		    sizeof (caddr_t) * P2ROUNDUP(ldep->nbytes, 8));
33841ae08745Sheppo 		ldep->align_addr = NULL;
33851ae08745Sheppo 	}
33861ae08745Sheppo 
33878e6a2a04Slm66018 	rv = ldc_mem_unbind_handle(ldep->desc_mhdl);
33888e6a2a04Slm66018 	if (rv != 0) {
33893af08d82Slm66018 		DMSG(vdc, 0, "?[%d] unbind mhdl 0x%lx @ idx %d failed (%d)",
33908e6a2a04Slm66018 		    vdc->instance, ldep->desc_mhdl, idx, rv);
33918e6a2a04Slm66018 		/*
33928e6a2a04Slm66018 		 * The error returned by the vDisk server is more informative
33938e6a2a04Slm66018 		 * and thus has a higher priority but if it isn't set we ensure
33948e6a2a04Slm66018 		 * that this function returns an error.
33958e6a2a04Slm66018 		 */
33968e6a2a04Slm66018 		if (status == 0)
33978e6a2a04Slm66018 			status = EINVAL;
33981ae08745Sheppo 	}
33991ae08745Sheppo 
34003af08d82Slm66018 	cv_signal(&vdc->membind_cv);
34013af08d82Slm66018 	cv_signal(&vdc->dring_free_cv);
34023af08d82Slm66018 
34031ae08745Sheppo 	return (status);
34041ae08745Sheppo }
34051ae08745Sheppo 
34060a55fbb7Slm66018 /*
34070a55fbb7Slm66018  * Function:
34080a55fbb7Slm66018  *	vdc_populate_mem_hdl()
34090a55fbb7Slm66018  *
34100a55fbb7Slm66018  * Description:
34110a55fbb7Slm66018  *
34120a55fbb7Slm66018  * Arguments:
34130a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
34140a55fbb7Slm66018  *	idx	- Index of the Descriptor Ring entry being modified
34150a55fbb7Slm66018  *	addr	- virtual address being mapped in
34160a55fbb7Slm66018  *	nybtes	- number of bytes in 'addr'
34170a55fbb7Slm66018  *	operation - the vDisk operation being performed (VD_OP_xxx)
34180a55fbb7Slm66018  *
34190a55fbb7Slm66018  * Return Code:
34200a55fbb7Slm66018  *	0	- Success
34210a55fbb7Slm66018  */
34221ae08745Sheppo static int
34233af08d82Slm66018 vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep)
34241ae08745Sheppo {
34251ae08745Sheppo 	vd_dring_entry_t	*dep = NULL;
34261ae08745Sheppo 	ldc_mem_handle_t	mhdl;
34271ae08745Sheppo 	caddr_t			vaddr;
34283af08d82Slm66018 	size_t			nbytes;
34294bac2208Snarayan 	uint8_t			perm = LDC_MEM_RW;
34304bac2208Snarayan 	uint8_t			maptype;
34311ae08745Sheppo 	int			rv = 0;
34321ae08745Sheppo 	int			i;
34331ae08745Sheppo 
34343af08d82Slm66018 	ASSERT(vdcp != NULL);
34351ae08745Sheppo 
34363af08d82Slm66018 	dep = ldep->dep;
34371ae08745Sheppo 	mhdl = ldep->desc_mhdl;
34381ae08745Sheppo 
34393af08d82Slm66018 	switch (ldep->dir) {
34403af08d82Slm66018 	case VIO_read_dir:
34411ae08745Sheppo 		perm = LDC_MEM_W;
34421ae08745Sheppo 		break;
34431ae08745Sheppo 
34443af08d82Slm66018 	case VIO_write_dir:
34451ae08745Sheppo 		perm = LDC_MEM_R;
34461ae08745Sheppo 		break;
34471ae08745Sheppo 
34483af08d82Slm66018 	case VIO_both_dir:
34491ae08745Sheppo 		perm = LDC_MEM_RW;
34501ae08745Sheppo 		break;
34511ae08745Sheppo 
34521ae08745Sheppo 	default:
34531ae08745Sheppo 		ASSERT(0);	/* catch bad programming in vdc */
34541ae08745Sheppo 	}
34551ae08745Sheppo 
34561ae08745Sheppo 	/*
34571ae08745Sheppo 	 * LDC expects any addresses passed in to be 8-byte aligned. We need
34581ae08745Sheppo 	 * to copy the contents of any misaligned buffers to a newly allocated
34591ae08745Sheppo 	 * buffer and bind it instead (and copy the the contents back to the
34601ae08745Sheppo 	 * original buffer passed in when depopulating the descriptor)
34611ae08745Sheppo 	 */
34623af08d82Slm66018 	vaddr = ldep->addr;
34633af08d82Slm66018 	nbytes = ldep->nbytes;
34643af08d82Slm66018 	if (((uint64_t)vaddr & 0x7) != 0) {
3465d10e4ef2Snarayan 		ASSERT(ldep->align_addr == NULL);
34661ae08745Sheppo 		ldep->align_addr =
34673af08d82Slm66018 		    kmem_alloc(sizeof (caddr_t) *
34683af08d82Slm66018 		    P2ROUNDUP(nbytes, 8), KM_SLEEP);
34693af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Misaligned address %p reallocating "
34703af08d82Slm66018 		    "(buf=%p nb=%ld op=%d)\n",
34713af08d82Slm66018 		    vdcp->instance, (void *)vaddr, (void *)ldep->align_addr,
34723af08d82Slm66018 		    nbytes, ldep->operation);
34733af08d82Slm66018 		if (perm != LDC_MEM_W)
34743af08d82Slm66018 			bcopy(vaddr, ldep->align_addr, nbytes);
34751ae08745Sheppo 		vaddr = ldep->align_addr;
34761ae08745Sheppo 	}
34771ae08745Sheppo 
34784bac2208Snarayan 	maptype = LDC_IO_MAP|LDC_SHADOW_MAP|LDC_DIRECT_MAP;
34791ae08745Sheppo 	rv = ldc_mem_bind_handle(mhdl, vaddr, P2ROUNDUP(nbytes, 8),
348087a7269eSachartre 	    maptype, perm, &dep->payload.cookie[0], &dep->payload.ncookies);
34813af08d82Slm66018 	DMSG(vdcp, 2, "[%d] bound mem handle; ncookies=%d\n",
34823af08d82Slm66018 	    vdcp->instance, dep->payload.ncookies);
34831ae08745Sheppo 	if (rv != 0) {
34843af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Failed to bind LDC memory handle "
34853af08d82Slm66018 		    "(mhdl=%p, buf=%p, err=%d)\n",
34863af08d82Slm66018 		    vdcp->instance, (void *)mhdl, (void *)vaddr, rv);
34871ae08745Sheppo 		if (ldep->align_addr) {
34881ae08745Sheppo 			kmem_free(ldep->align_addr,
3489d10e4ef2Snarayan 			    sizeof (caddr_t) * P2ROUNDUP(nbytes, 8));
34901ae08745Sheppo 			ldep->align_addr = NULL;
34911ae08745Sheppo 		}
34921ae08745Sheppo 		return (EAGAIN);
34931ae08745Sheppo 	}
34941ae08745Sheppo 
34951ae08745Sheppo 	/*
34961ae08745Sheppo 	 * Get the other cookies (if any).
34971ae08745Sheppo 	 */
34981ae08745Sheppo 	for (i = 1; i < dep->payload.ncookies; i++) {
34991ae08745Sheppo 		rv = ldc_mem_nextcookie(mhdl, &dep->payload.cookie[i]);
35001ae08745Sheppo 		if (rv != 0) {
35011ae08745Sheppo 			(void) ldc_mem_unbind_handle(mhdl);
35023af08d82Slm66018 			DMSG(vdcp, 0, "?[%d] Failed to get next cookie "
3503e1ebb9ecSlm66018 			    "(mhdl=%lx cnum=%d), err=%d",
35043af08d82Slm66018 			    vdcp->instance, mhdl, i, rv);
35051ae08745Sheppo 			if (ldep->align_addr) {
35061ae08745Sheppo 				kmem_free(ldep->align_addr,
35073c96341aSnarayan 				    sizeof (caddr_t) * ldep->nbytes);
35081ae08745Sheppo 				ldep->align_addr = NULL;
35091ae08745Sheppo 			}
35101ae08745Sheppo 			return (EAGAIN);
35111ae08745Sheppo 		}
35121ae08745Sheppo 	}
35131ae08745Sheppo 
35141ae08745Sheppo 	return (rv);
35151ae08745Sheppo }
35161ae08745Sheppo 
35171ae08745Sheppo /*
35181ae08745Sheppo  * Interrupt handlers for messages from LDC
35191ae08745Sheppo  */
35201ae08745Sheppo 
35210a55fbb7Slm66018 /*
35220a55fbb7Slm66018  * Function:
35230a55fbb7Slm66018  *	vdc_handle_cb()
35240a55fbb7Slm66018  *
35250a55fbb7Slm66018  * Description:
35260a55fbb7Slm66018  *
35270a55fbb7Slm66018  * Arguments:
35280a55fbb7Slm66018  *	event	- Type of event (LDC_EVT_xxx) that triggered the callback
35290a55fbb7Slm66018  *	arg	- soft state pointer for this instance of the device driver.
35300a55fbb7Slm66018  *
35310a55fbb7Slm66018  * Return Code:
35320a55fbb7Slm66018  *	0	- Success
35330a55fbb7Slm66018  */
35341ae08745Sheppo static uint_t
35351ae08745Sheppo vdc_handle_cb(uint64_t event, caddr_t arg)
35361ae08745Sheppo {
35371ae08745Sheppo 	ldc_status_t	ldc_state;
35381ae08745Sheppo 	int		rv = 0;
35398cd10891Snarayan 	vdc_server_t	*srvr = (vdc_server_t *)(void *)arg;
35408cd10891Snarayan 	vdc_t		*vdc = srvr->vdcp;
35411ae08745Sheppo 
35421ae08745Sheppo 	ASSERT(vdc != NULL);
35431ae08745Sheppo 
35443af08d82Slm66018 	DMSG(vdc, 1, "evt=%lx seqID=%ld\n", event, vdc->seq_num);
35451ae08745Sheppo 
35468cd10891Snarayan 	/* If callback is not for the current server, ignore it */
35478cd10891Snarayan 	mutex_enter(&vdc->lock);
35488cd10891Snarayan 
35498cd10891Snarayan 	if (vdc->curr_server != srvr) {
35508cd10891Snarayan 		DMSG(vdc, 0, "[%d] Ignoring event 0x%lx for port@%ld\n",
35518cd10891Snarayan 		    vdc->instance, event, srvr->id);
35528cd10891Snarayan 		mutex_exit(&vdc->lock);
35538cd10891Snarayan 		return (LDC_SUCCESS);
35548cd10891Snarayan 	}
35558cd10891Snarayan 
35561ae08745Sheppo 	/*
35571ae08745Sheppo 	 * Depending on the type of event that triggered this callback,
35583af08d82Slm66018 	 * we modify the handshake state or read the data.
35591ae08745Sheppo 	 *
35601ae08745Sheppo 	 * NOTE: not done as a switch() as event could be triggered by
35611ae08745Sheppo 	 * a state change and a read request. Also the ordering	of the
35621ae08745Sheppo 	 * check for the event types is deliberate.
35631ae08745Sheppo 	 */
35641ae08745Sheppo 	if (event & LDC_EVT_UP) {
35653af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received LDC_EVT_UP\n", vdc->instance);
35663af08d82Slm66018 
35671ae08745Sheppo 		/* get LDC state */
35688cd10891Snarayan 		rv = ldc_status(srvr->ldc_handle, &ldc_state);
35691ae08745Sheppo 		if (rv != 0) {
35703af08d82Slm66018 			DMSG(vdc, 0, "[%d] Couldn't get LDC status %d",
35711ae08745Sheppo 			    vdc->instance, rv);
35728cd10891Snarayan 			mutex_exit(&vdc->lock);
35731ae08745Sheppo 			return (LDC_SUCCESS);
35741ae08745Sheppo 		}
35758cd10891Snarayan 		if (srvr->ldc_state != LDC_UP &&
35768cd10891Snarayan 		    ldc_state == LDC_UP) {
35771ae08745Sheppo 			/*
35783af08d82Slm66018 			 * Reset the transaction sequence numbers when
35793af08d82Slm66018 			 * LDC comes up. We then kick off the handshake
35803af08d82Slm66018 			 * negotiation with the vDisk server.
35811ae08745Sheppo 			 */
35820a55fbb7Slm66018 			vdc->seq_num = 1;
35831ae08745Sheppo 			vdc->seq_num_reply = 0;
35848cd10891Snarayan 			srvr->ldc_state = ldc_state;
35853af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
35863af08d82Slm66018 		}
35871ae08745Sheppo 	}
35881ae08745Sheppo 
35891ae08745Sheppo 	if (event & LDC_EVT_READ) {
359017cadca8Slm66018 		DMSG(vdc, 1, "[%d] Received LDC_EVT_READ\n", vdc->instance);
35913af08d82Slm66018 		mutex_enter(&vdc->read_lock);
35923af08d82Slm66018 		cv_signal(&vdc->read_cv);
35933af08d82Slm66018 		vdc->read_state = VDC_READ_PENDING;
35943af08d82Slm66018 		mutex_exit(&vdc->read_lock);
35958cd10891Snarayan 		mutex_exit(&vdc->lock);
35961ae08745Sheppo 
35971ae08745Sheppo 		/* that's all we have to do - no need to handle DOWN/RESET */
35981ae08745Sheppo 		return (LDC_SUCCESS);
35991ae08745Sheppo 	}
36001ae08745Sheppo 
36013af08d82Slm66018 	if (event & (LDC_EVT_RESET|LDC_EVT_DOWN)) {
36020a55fbb7Slm66018 
36033af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received LDC RESET event\n", vdc->instance);
36043af08d82Slm66018 
36053af08d82Slm66018 		/*
36063af08d82Slm66018 		 * Need to wake up any readers so they will
36073af08d82Slm66018 		 * detect that a reset has occurred.
36083af08d82Slm66018 		 */
36093af08d82Slm66018 		mutex_enter(&vdc->read_lock);
36103af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
36113af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET))
36123af08d82Slm66018 			cv_signal(&vdc->read_cv);
36133af08d82Slm66018 		vdc->read_state = VDC_READ_RESET;
36143af08d82Slm66018 		mutex_exit(&vdc->read_lock);
36150a55fbb7Slm66018 
36163af08d82Slm66018 		/* wake up any threads waiting for connection to come up */
36173af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
36183af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
36193af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
36201ae08745Sheppo 		}
36211ae08745Sheppo 
36221ae08745Sheppo 	}
36231ae08745Sheppo 
36248cd10891Snarayan 	mutex_exit(&vdc->lock);
36258cd10891Snarayan 
36261ae08745Sheppo 	if (event & ~(LDC_EVT_UP | LDC_EVT_RESET | LDC_EVT_DOWN | LDC_EVT_READ))
36273af08d82Slm66018 		DMSG(vdc, 0, "![%d] Unexpected LDC event (%lx) received",
36281ae08745Sheppo 		    vdc->instance, event);
36291ae08745Sheppo 
36301ae08745Sheppo 	return (LDC_SUCCESS);
36311ae08745Sheppo }
36321ae08745Sheppo 
36333af08d82Slm66018 /*
36343af08d82Slm66018  * Function:
36353af08d82Slm66018  *	vdc_wait_for_response()
36363af08d82Slm66018  *
36373af08d82Slm66018  * Description:
36383af08d82Slm66018  *	Block waiting for a response from the server. If there is
36393af08d82Slm66018  *	no data the thread block on the read_cv that is signalled
36403af08d82Slm66018  *	by the callback when an EVT_READ occurs.
36413af08d82Slm66018  *
36423af08d82Slm66018  * Arguments:
36433af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
36443af08d82Slm66018  *
36453af08d82Slm66018  * Return Code:
36463af08d82Slm66018  *	0	- Success
36473af08d82Slm66018  */
36483af08d82Slm66018 static int
36493af08d82Slm66018 vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp)
36503af08d82Slm66018 {
36513af08d82Slm66018 	size_t		nbytes = sizeof (*msgp);
36523af08d82Slm66018 	int		status;
36533af08d82Slm66018 
36543af08d82Slm66018 	ASSERT(vdcp != NULL);
36553af08d82Slm66018 
36563af08d82Slm66018 	DMSG(vdcp, 1, "[%d] Entered\n", vdcp->instance);
36573af08d82Slm66018 
36583af08d82Slm66018 	status = vdc_recv(vdcp, msgp, &nbytes);
36593af08d82Slm66018 	DMSG(vdcp, 3, "vdc_read() done.. status=0x%x size=0x%x\n",
36603af08d82Slm66018 	    status, (int)nbytes);
36613af08d82Slm66018 	if (status) {
36623af08d82Slm66018 		DMSG(vdcp, 0, "?[%d] Error %d reading LDC msg\n",
36633af08d82Slm66018 		    vdcp->instance, status);
36643af08d82Slm66018 		return (status);
36653af08d82Slm66018 	}
36663af08d82Slm66018 
36673af08d82Slm66018 	if (nbytes < sizeof (vio_msg_tag_t)) {
36683af08d82Slm66018 		DMSG(vdcp, 0, "?[%d] Expect %lu bytes; recv'd %lu\n",
36693af08d82Slm66018 		    vdcp->instance, sizeof (vio_msg_tag_t), nbytes);
36703af08d82Slm66018 		return (ENOMSG);
36713af08d82Slm66018 	}
36723af08d82Slm66018 
36733af08d82Slm66018 	DMSG(vdcp, 2, "[%d] (%x/%x/%x)\n", vdcp->instance,
36743af08d82Slm66018 	    msgp->tag.vio_msgtype,
36753af08d82Slm66018 	    msgp->tag.vio_subtype,
36763af08d82Slm66018 	    msgp->tag.vio_subtype_env);
36773af08d82Slm66018 
36783af08d82Slm66018 	/*
36793af08d82Slm66018 	 * Verify the Session ID of the message
36803af08d82Slm66018 	 *
36813af08d82Slm66018 	 * Every message after the Version has been negotiated should
36823af08d82Slm66018 	 * have the correct session ID set.
36833af08d82Slm66018 	 */
36843af08d82Slm66018 	if ((msgp->tag.vio_sid != vdcp->session_id) &&
36853af08d82Slm66018 	    (msgp->tag.vio_subtype_env != VIO_VER_INFO)) {
36863af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid SID: received 0x%x, "
36873af08d82Slm66018 		    "expected 0x%lx [seq num %lx @ %d]",
36883af08d82Slm66018 		    vdcp->instance, msgp->tag.vio_sid,
36893af08d82Slm66018 		    vdcp->session_id,
36903af08d82Slm66018 		    ((vio_dring_msg_t *)msgp)->seq_num,
36913af08d82Slm66018 		    ((vio_dring_msg_t *)msgp)->start_idx);
36923af08d82Slm66018 		return (ENOMSG);
36933af08d82Slm66018 	}
36943af08d82Slm66018 	return (0);
36953af08d82Slm66018 }
36963af08d82Slm66018 
36973af08d82Slm66018 
36983af08d82Slm66018 /*
36993af08d82Slm66018  * Function:
37003af08d82Slm66018  *	vdc_resubmit_backup_dring()
37013af08d82Slm66018  *
37023af08d82Slm66018  * Description:
37033af08d82Slm66018  *	Resubmit each descriptor in the backed up dring to
37043af08d82Slm66018  * 	vDisk server. The Dring was backed up during connection
37053af08d82Slm66018  *	reset.
37063af08d82Slm66018  *
37073af08d82Slm66018  * Arguments:
37083af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
37093af08d82Slm66018  *
37103af08d82Slm66018  * Return Code:
37113af08d82Slm66018  *	0	- Success
37123af08d82Slm66018  */
37133af08d82Slm66018 static int
37143af08d82Slm66018 vdc_resubmit_backup_dring(vdc_t *vdcp)
37153af08d82Slm66018 {
371690e2f9dcSlm66018 	int		processed = 0;
37173af08d82Slm66018 	int		count;
37183af08d82Slm66018 	int		b_idx;
371990e2f9dcSlm66018 	int		rv = 0;
37203af08d82Slm66018 	int		dring_size;
372190e2f9dcSlm66018 	int		op;
37223af08d82Slm66018 	vio_msg_t	vio_msg;
37233af08d82Slm66018 	vdc_local_desc_t	*curr_ldep;
37243af08d82Slm66018 
37253af08d82Slm66018 	ASSERT(MUTEX_NOT_HELD(&vdcp->lock));
37263af08d82Slm66018 	ASSERT(vdcp->state == VDC_STATE_HANDLE_PENDING);
37273af08d82Slm66018 
3728655fd6a9Sachartre 	if (vdcp->local_dring_backup == NULL) {
3729655fd6a9Sachartre 		/* the pending requests have already been processed */
3730655fd6a9Sachartre 		return (0);
3731655fd6a9Sachartre 	}
3732655fd6a9Sachartre 
37333af08d82Slm66018 	DMSG(vdcp, 1, "restoring pending dring entries (len=%d, tail=%d)\n",
37343af08d82Slm66018 	    vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
37353af08d82Slm66018 
37363af08d82Slm66018 	/*
37373af08d82Slm66018 	 * Walk the backup copy of the local descriptor ring and
37383af08d82Slm66018 	 * resubmit all the outstanding transactions.
37393af08d82Slm66018 	 */
37403af08d82Slm66018 	b_idx = vdcp->local_dring_backup_tail;
37413af08d82Slm66018 	for (count = 0; count < vdcp->local_dring_backup_len; count++) {
37423af08d82Slm66018 
37433af08d82Slm66018 		curr_ldep = &(vdcp->local_dring_backup[b_idx]);
37443af08d82Slm66018 
3745eff7243fSlm66018 		/* only resubmit outstanding transactions */
37463af08d82Slm66018 		if (!curr_ldep->is_free) {
374790e2f9dcSlm66018 			/*
374890e2f9dcSlm66018 			 * If we are retrying a block read/write operation we
374990e2f9dcSlm66018 			 * need to update the I/O statistics to indicate that
375090e2f9dcSlm66018 			 * the request is being put back on the waitq to be
375190e2f9dcSlm66018 			 * serviced (it will have been taken off after the
375290e2f9dcSlm66018 			 * error was reported).
375390e2f9dcSlm66018 			 */
375490e2f9dcSlm66018 			mutex_enter(&vdcp->lock);
375590e2f9dcSlm66018 			op = curr_ldep->operation;
375690e2f9dcSlm66018 			if ((op == VD_OP_BREAD) || (op == VD_OP_BWRITE)) {
375790e2f9dcSlm66018 				DTRACE_IO1(start, buf_t *, curr_ldep->cb_arg);
375890e2f9dcSlm66018 				VD_KSTAT_WAITQ_ENTER(vdcp);
375990e2f9dcSlm66018 			}
37603af08d82Slm66018 
37613af08d82Slm66018 			DMSG(vdcp, 1, "resubmitting entry idx=%x\n", b_idx);
376290e2f9dcSlm66018 			rv = vdc_populate_descriptor(vdcp, op,
37633af08d82Slm66018 			    curr_ldep->addr, curr_ldep->nbytes,
37643af08d82Slm66018 			    curr_ldep->slice, curr_ldep->offset,
37653af08d82Slm66018 			    curr_ldep->cb_type, curr_ldep->cb_arg,
37663af08d82Slm66018 			    curr_ldep->dir);
376790e2f9dcSlm66018 
37683af08d82Slm66018 			if (rv) {
376990e2f9dcSlm66018 				if (op == VD_OP_BREAD || op == VD_OP_BWRITE) {
377090e2f9dcSlm66018 					VD_UPDATE_ERR_STATS(vdcp, vd_transerrs);
377190e2f9dcSlm66018 					VD_KSTAT_WAITQ_EXIT(vdcp);
377290e2f9dcSlm66018 					DTRACE_IO1(done, buf_t *,
377390e2f9dcSlm66018 					    curr_ldep->cb_arg);
377490e2f9dcSlm66018 				}
37753af08d82Slm66018 				DMSG(vdcp, 1, "[%d] cannot resubmit entry %d\n",
37763af08d82Slm66018 				    vdcp->instance, b_idx);
377790e2f9dcSlm66018 				mutex_exit(&vdcp->lock);
377890e2f9dcSlm66018 				goto done;
37793af08d82Slm66018 			}
37803af08d82Slm66018 
378190e2f9dcSlm66018 			/*
378290e2f9dcSlm66018 			 * If this is a block read/write we update the I/O
378390e2f9dcSlm66018 			 * statistics kstat to indicate that the request
378490e2f9dcSlm66018 			 * has been sent back to the vDisk server and should
378590e2f9dcSlm66018 			 * now be put on the run queue.
378690e2f9dcSlm66018 			 */
378790e2f9dcSlm66018 			if ((op == VD_OP_BREAD) || (op == VD_OP_BWRITE)) {
378890e2f9dcSlm66018 				DTRACE_PROBE1(send, buf_t *, curr_ldep->cb_arg);
378990e2f9dcSlm66018 				VD_KSTAT_WAITQ_TO_RUNQ(vdcp);
379090e2f9dcSlm66018 			}
379190e2f9dcSlm66018 			mutex_exit(&vdcp->lock);
379290e2f9dcSlm66018 
37933af08d82Slm66018 			/* Wait for the response message. */
37943af08d82Slm66018 			DMSG(vdcp, 1, "waiting for response to idx=%x\n",
37953af08d82Slm66018 			    b_idx);
379690e2f9dcSlm66018 			rv = vdc_wait_for_response(vdcp, &vio_msg);
379790e2f9dcSlm66018 			if (rv) {
379890e2f9dcSlm66018 				/*
379990e2f9dcSlm66018 				 * If this is a block read/write we update
380090e2f9dcSlm66018 				 * the I/O statistics kstat to take it
380190e2f9dcSlm66018 				 * off the run queue.
380290e2f9dcSlm66018 				 */
380390e2f9dcSlm66018 				mutex_enter(&vdcp->lock);
380490e2f9dcSlm66018 				if (op == VD_OP_BREAD || op == VD_OP_BWRITE) {
380590e2f9dcSlm66018 					VD_UPDATE_ERR_STATS(vdcp, vd_transerrs);
380690e2f9dcSlm66018 					VD_KSTAT_RUNQ_EXIT(vdcp);
380790e2f9dcSlm66018 					DTRACE_IO1(done, buf_t *,
380890e2f9dcSlm66018 					    curr_ldep->cb_arg);
380990e2f9dcSlm66018 				}
38103af08d82Slm66018 				DMSG(vdcp, 1, "[%d] wait_for_response "
38113af08d82Slm66018 				    "returned err=%d\n", vdcp->instance,
381290e2f9dcSlm66018 				    rv);
381390e2f9dcSlm66018 				mutex_exit(&vdcp->lock);
381490e2f9dcSlm66018 				goto done;
38153af08d82Slm66018 			}
38163af08d82Slm66018 
38173af08d82Slm66018 			DMSG(vdcp, 1, "processing msg for idx=%x\n", b_idx);
381890e2f9dcSlm66018 			rv = vdc_process_data_msg(vdcp, &vio_msg);
381990e2f9dcSlm66018 			if (rv) {
38203af08d82Slm66018 				DMSG(vdcp, 1, "[%d] process_data_msg "
38213af08d82Slm66018 				    "returned err=%d\n", vdcp->instance,
382290e2f9dcSlm66018 				    rv);
382390e2f9dcSlm66018 				goto done;
38243af08d82Slm66018 			}
3825630f014dSrameshc 			/*
3826630f014dSrameshc 			 * Mark this entry as free so that we will not resubmit
3827630f014dSrameshc 			 * this "done" request again, if we were to use the same
3828630f014dSrameshc 			 * backup_dring again in future. This could happen when
3829630f014dSrameshc 			 * a reset happens while processing the backup_dring.
3830630f014dSrameshc 			 */
3831630f014dSrameshc 			curr_ldep->is_free = B_TRUE;
383290e2f9dcSlm66018 			processed++;
38333af08d82Slm66018 		}
38343af08d82Slm66018 
38353af08d82Slm66018 		/* get the next element to submit */
38363af08d82Slm66018 		if (++b_idx >= vdcp->local_dring_backup_len)
38373af08d82Slm66018 			b_idx = 0;
38383af08d82Slm66018 	}
38393af08d82Slm66018 
38403af08d82Slm66018 	/* all done - now clear up pending dring copy */
38413af08d82Slm66018 	dring_size = vdcp->local_dring_backup_len *
38423af08d82Slm66018 	    sizeof (vdcp->local_dring_backup[0]);
38433af08d82Slm66018 
38443af08d82Slm66018 	(void) kmem_free(vdcp->local_dring_backup, dring_size);
38453af08d82Slm66018 
38463af08d82Slm66018 	vdcp->local_dring_backup = NULL;
38473af08d82Slm66018 
384890e2f9dcSlm66018 done:
384990e2f9dcSlm66018 	DTRACE_PROBE2(processed, int, processed, vdc_t *, vdcp);
385090e2f9dcSlm66018 
385190e2f9dcSlm66018 	return (rv);
38523af08d82Slm66018 }
38533af08d82Slm66018 
38543af08d82Slm66018 /*
38553af08d82Slm66018  * Function:
3856655fd6a9Sachartre  *	vdc_cancel_backup_dring
3857655fd6a9Sachartre  *
3858655fd6a9Sachartre  * Description:
3859655fd6a9Sachartre  *	Cancel each descriptor in the backed up dring to vDisk server.
3860655fd6a9Sachartre  *	The Dring was backed up during connection reset.
3861655fd6a9Sachartre  *
3862655fd6a9Sachartre  * Arguments:
3863655fd6a9Sachartre  *	vdcp	- soft state pointer for this instance of the device driver.
3864655fd6a9Sachartre  *
3865655fd6a9Sachartre  * Return Code:
3866655fd6a9Sachartre  *	None
3867655fd6a9Sachartre  */
3868655fd6a9Sachartre void
386990e2f9dcSlm66018 vdc_cancel_backup_dring(vdc_t *vdcp)
3870655fd6a9Sachartre {
3871655fd6a9Sachartre 	vdc_local_desc_t *ldep;
3872655fd6a9Sachartre 	struct buf 	*bufp;
3873655fd6a9Sachartre 	int		count;
3874655fd6a9Sachartre 	int		b_idx;
3875655fd6a9Sachartre 	int		dring_size;
387690e2f9dcSlm66018 	int		cancelled = 0;
3877655fd6a9Sachartre 
3878655fd6a9Sachartre 	ASSERT(MUTEX_HELD(&vdcp->lock));
3879655fd6a9Sachartre 	ASSERT(vdcp->state == VDC_STATE_INIT ||
3880655fd6a9Sachartre 	    vdcp->state == VDC_STATE_INIT_WAITING ||
3881655fd6a9Sachartre 	    vdcp->state == VDC_STATE_NEGOTIATE ||
3882655fd6a9Sachartre 	    vdcp->state == VDC_STATE_RESETTING);
3883655fd6a9Sachartre 
3884655fd6a9Sachartre 	if (vdcp->local_dring_backup == NULL) {
3885655fd6a9Sachartre 		/* the pending requests have already been processed */
3886655fd6a9Sachartre 		return;
3887655fd6a9Sachartre 	}
3888655fd6a9Sachartre 
3889655fd6a9Sachartre 	DMSG(vdcp, 1, "cancelling pending dring entries (len=%d, tail=%d)\n",
3890655fd6a9Sachartre 	    vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
3891655fd6a9Sachartre 
3892655fd6a9Sachartre 	/*
3893655fd6a9Sachartre 	 * Walk the backup copy of the local descriptor ring and
3894655fd6a9Sachartre 	 * cancel all the outstanding transactions.
3895655fd6a9Sachartre 	 */
3896655fd6a9Sachartre 	b_idx = vdcp->local_dring_backup_tail;
3897655fd6a9Sachartre 	for (count = 0; count < vdcp->local_dring_backup_len; count++) {
3898655fd6a9Sachartre 
3899655fd6a9Sachartre 		ldep = &(vdcp->local_dring_backup[b_idx]);
3900655fd6a9Sachartre 
3901655fd6a9Sachartre 		/* only cancel outstanding transactions */
3902655fd6a9Sachartre 		if (!ldep->is_free) {
3903655fd6a9Sachartre 
3904655fd6a9Sachartre 			DMSG(vdcp, 1, "cancelling entry idx=%x\n", b_idx);
390590e2f9dcSlm66018 			cancelled++;
3906655fd6a9Sachartre 
3907655fd6a9Sachartre 			/*
3908655fd6a9Sachartre 			 * All requests have already been cleared from the
3909655fd6a9Sachartre 			 * local descriptor ring and the LDC channel has been
3910655fd6a9Sachartre 			 * reset so we will never get any reply for these
3911655fd6a9Sachartre 			 * requests. Now we just have to notify threads waiting
3912655fd6a9Sachartre 			 * for replies that the request has failed.
3913655fd6a9Sachartre 			 */
3914655fd6a9Sachartre 			switch (ldep->cb_type) {
3915655fd6a9Sachartre 			case CB_SYNC:
3916655fd6a9Sachartre 				ASSERT(vdcp->sync_op_pending);
3917655fd6a9Sachartre 				vdcp->sync_op_status = EIO;
3918655fd6a9Sachartre 				vdcp->sync_op_pending = B_FALSE;
3919655fd6a9Sachartre 				cv_signal(&vdcp->sync_pending_cv);
3920655fd6a9Sachartre 				break;
3921655fd6a9Sachartre 
3922655fd6a9Sachartre 			case CB_STRATEGY:
3923655fd6a9Sachartre 				bufp = ldep->cb_arg;
3924655fd6a9Sachartre 				ASSERT(bufp != NULL);
3925655fd6a9Sachartre 				bufp->b_resid = bufp->b_bcount;
3926366a92acSlm66018 				VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
392790e2f9dcSlm66018 				VD_KSTAT_RUNQ_EXIT(vdcp);
3928366a92acSlm66018 				DTRACE_IO1(done, buf_t *, bufp);
3929655fd6a9Sachartre 				bioerror(bufp, EIO);
3930655fd6a9Sachartre 				biodone(bufp);
3931655fd6a9Sachartre 				break;
3932655fd6a9Sachartre 
3933655fd6a9Sachartre 			default:
3934655fd6a9Sachartre 				ASSERT(0);
3935655fd6a9Sachartre 			}
3936655fd6a9Sachartre 
3937655fd6a9Sachartre 		}
3938655fd6a9Sachartre 
3939655fd6a9Sachartre 		/* get the next element to cancel */
3940655fd6a9Sachartre 		if (++b_idx >= vdcp->local_dring_backup_len)
3941655fd6a9Sachartre 			b_idx = 0;
3942655fd6a9Sachartre 	}
3943655fd6a9Sachartre 
3944655fd6a9Sachartre 	/* all done - now clear up pending dring copy */
3945655fd6a9Sachartre 	dring_size = vdcp->local_dring_backup_len *
3946655fd6a9Sachartre 	    sizeof (vdcp->local_dring_backup[0]);
3947655fd6a9Sachartre 
3948655fd6a9Sachartre 	(void) kmem_free(vdcp->local_dring_backup, dring_size);
3949655fd6a9Sachartre 
3950655fd6a9Sachartre 	vdcp->local_dring_backup = NULL;
3951655fd6a9Sachartre 
395290e2f9dcSlm66018 	DTRACE_PROBE2(cancelled, int, cancelled, vdc_t *, vdcp);
3953655fd6a9Sachartre }
3954655fd6a9Sachartre 
3955655fd6a9Sachartre /*
3956655fd6a9Sachartre  * Function:
3957655fd6a9Sachartre  *	vdc_connection_timeout
3958655fd6a9Sachartre  *
3959655fd6a9Sachartre  * Description:
3960655fd6a9Sachartre  *	This function is invoked if the timeout set to establish the connection
3961655fd6a9Sachartre  *	with vds expires. This will happen if we spend too much time in the
3962655fd6a9Sachartre  *	VDC_STATE_INIT_WAITING or VDC_STATE_NEGOTIATE states. Then we will
3963655fd6a9Sachartre  *	cancel any pending request and mark them as failed.
3964655fd6a9Sachartre  *
3965655fd6a9Sachartre  *	If the timeout does not expire, it will be cancelled when we reach the
3966655fd6a9Sachartre  *	VDC_STATE_HANDLE_PENDING or VDC_STATE_RESETTING state. This function can
3967655fd6a9Sachartre  *	be invoked while we are in the VDC_STATE_HANDLE_PENDING or
3968655fd6a9Sachartre  *	VDC_STATE_RESETTING state in which case we do nothing because the
3969655fd6a9Sachartre  *	timeout is being cancelled.
3970655fd6a9Sachartre  *
3971655fd6a9Sachartre  * Arguments:
3972655fd6a9Sachartre  *	arg	- argument of the timeout function actually a soft state
3973655fd6a9Sachartre  *		  pointer for the instance of the device driver.
3974655fd6a9Sachartre  *
3975655fd6a9Sachartre  * Return Code:
3976655fd6a9Sachartre  *	None
3977655fd6a9Sachartre  */
3978655fd6a9Sachartre void
3979655fd6a9Sachartre vdc_connection_timeout(void *arg)
3980655fd6a9Sachartre {
3981655fd6a9Sachartre 	vdc_t 		*vdcp = (vdc_t *)arg;
3982655fd6a9Sachartre 
3983655fd6a9Sachartre 	mutex_enter(&vdcp->lock);
3984655fd6a9Sachartre 
3985655fd6a9Sachartre 	if (vdcp->state == VDC_STATE_HANDLE_PENDING ||
3986655fd6a9Sachartre 	    vdcp->state == VDC_STATE_DETACH) {
3987655fd6a9Sachartre 		/*
3988655fd6a9Sachartre 		 * The connection has just been re-established or
3989655fd6a9Sachartre 		 * we are detaching.
3990655fd6a9Sachartre 		 */
3991655fd6a9Sachartre 		vdcp->ctimeout_reached = B_FALSE;
3992655fd6a9Sachartre 		mutex_exit(&vdcp->lock);
3993655fd6a9Sachartre 		return;
3994655fd6a9Sachartre 	}
3995655fd6a9Sachartre 
3996655fd6a9Sachartre 	vdcp->ctimeout_reached = B_TRUE;
3997655fd6a9Sachartre 
3998655fd6a9Sachartre 	/* notify requests waiting for sending */
3999655fd6a9Sachartre 	cv_broadcast(&vdcp->running_cv);
4000655fd6a9Sachartre 
4001655fd6a9Sachartre 	/* cancel requests waiting for a result */
400290e2f9dcSlm66018 	vdc_cancel_backup_dring(vdcp);
4003655fd6a9Sachartre 
4004655fd6a9Sachartre 	mutex_exit(&vdcp->lock);
4005655fd6a9Sachartre 
4006655fd6a9Sachartre 	cmn_err(CE_NOTE, "[%d] connection to service domain timeout",
4007655fd6a9Sachartre 	    vdcp->instance);
4008655fd6a9Sachartre }
4009655fd6a9Sachartre 
4010655fd6a9Sachartre /*
4011655fd6a9Sachartre  * Function:
40123af08d82Slm66018  *	vdc_backup_local_dring()
40133af08d82Slm66018  *
40143af08d82Slm66018  * Description:
40153af08d82Slm66018  *	Backup the current dring in the event of a reset. The Dring
40163af08d82Slm66018  *	transactions will be resubmitted to the server when the
40173af08d82Slm66018  *	connection is restored.
40183af08d82Slm66018  *
40193af08d82Slm66018  * Arguments:
40203af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
40213af08d82Slm66018  *
40223af08d82Slm66018  * Return Code:
40233af08d82Slm66018  *	NONE
40243af08d82Slm66018  */
40253af08d82Slm66018 static void
40263af08d82Slm66018 vdc_backup_local_dring(vdc_t *vdcp)
40273af08d82Slm66018 {
40283af08d82Slm66018 	int dring_size;
40293af08d82Slm66018 
4030655fd6a9Sachartre 	ASSERT(MUTEX_HELD(&vdcp->lock));
40313af08d82Slm66018 	ASSERT(vdcp->state == VDC_STATE_RESETTING);
40323af08d82Slm66018 
40333af08d82Slm66018 	/*
40343af08d82Slm66018 	 * If the backup dring is stil around, it means
40353af08d82Slm66018 	 * that the last restore did not complete. However,
40363af08d82Slm66018 	 * since we never got back into the running state,
40373af08d82Slm66018 	 * the backup copy we have is still valid.
40383af08d82Slm66018 	 */
40393af08d82Slm66018 	if (vdcp->local_dring_backup != NULL) {
40403af08d82Slm66018 		DMSG(vdcp, 1, "reusing local descriptor ring backup "
40413af08d82Slm66018 		    "(len=%d, tail=%d)\n", vdcp->local_dring_backup_len,
40423af08d82Slm66018 		    vdcp->local_dring_backup_tail);
40433af08d82Slm66018 		return;
40443af08d82Slm66018 	}
40453af08d82Slm66018 
4046655fd6a9Sachartre 	/*
4047655fd6a9Sachartre 	 * The backup dring can be NULL and the local dring may not be
4048655fd6a9Sachartre 	 * initialized. This can happen if we had a reset while establishing
4049655fd6a9Sachartre 	 * a new connection but after the connection has timed out. In that
4050655fd6a9Sachartre 	 * case the backup dring is NULL because the requests have been
4051655fd6a9Sachartre 	 * cancelled and the request occured before the local dring is
4052655fd6a9Sachartre 	 * initialized.
4053655fd6a9Sachartre 	 */
4054655fd6a9Sachartre 	if (!(vdcp->initialized & VDC_DRING_LOCAL))
4055655fd6a9Sachartre 		return;
4056655fd6a9Sachartre 
40573af08d82Slm66018 	DMSG(vdcp, 1, "backing up the local descriptor ring (len=%d, "
40583af08d82Slm66018 	    "tail=%d)\n", vdcp->dring_len, vdcp->dring_curr_idx);
40593af08d82Slm66018 
40603af08d82Slm66018 	dring_size = vdcp->dring_len * sizeof (vdcp->local_dring[0]);
40613af08d82Slm66018 
40623af08d82Slm66018 	vdcp->local_dring_backup = kmem_alloc(dring_size, KM_SLEEP);
40633af08d82Slm66018 	bcopy(vdcp->local_dring, vdcp->local_dring_backup, dring_size);
40643af08d82Slm66018 
40653af08d82Slm66018 	vdcp->local_dring_backup_tail = vdcp->dring_curr_idx;
40663af08d82Slm66018 	vdcp->local_dring_backup_len = vdcp->dring_len;
40673af08d82Slm66018 }
40683af08d82Slm66018 
40698cd10891Snarayan static void
40708cd10891Snarayan vdc_switch_server(vdc_t *vdcp)
40718cd10891Snarayan {
40728cd10891Snarayan 	int		rv;
40738cd10891Snarayan 	vdc_server_t 	*curr_server, *new_server;
40748cd10891Snarayan 
40758cd10891Snarayan 	ASSERT(MUTEX_HELD(&vdcp->lock));
40768cd10891Snarayan 
40778cd10891Snarayan 	/* if there is only one server return back */
40788cd10891Snarayan 	if (vdcp->num_servers == 1) {
40798cd10891Snarayan 		return;
40808cd10891Snarayan 	}
40818cd10891Snarayan 
40828cd10891Snarayan 	/* Get current and next server */
40838cd10891Snarayan 	curr_server = vdcp->curr_server;
40848cd10891Snarayan 	new_server =
40858cd10891Snarayan 	    (curr_server->next) ? curr_server->next : vdcp->server_list;
40868cd10891Snarayan 	ASSERT(curr_server != new_server);
40878cd10891Snarayan 
40888cd10891Snarayan 	/* bring current server's channel down */
40898cd10891Snarayan 	rv = ldc_down(curr_server->ldc_handle);
40908cd10891Snarayan 	if (rv) {
40918cd10891Snarayan 		DMSG(vdcp, 0, "[%d] Cannot bring channel down, port %ld\n",
40928cd10891Snarayan 		    vdcp->instance, curr_server->id);
40938cd10891Snarayan 		return;
40948cd10891Snarayan 	}
40958cd10891Snarayan 
40968cd10891Snarayan 	/* switch the server */
40978cd10891Snarayan 	vdcp->curr_server = new_server;
40988cd10891Snarayan 
40998cd10891Snarayan 	DMSG(vdcp, 0, "[%d] Switched to next vdisk server, port@%ld, ldc@%ld\n",
41008cd10891Snarayan 	    vdcp->instance, vdcp->curr_server->id, vdcp->curr_server->ldc_id);
41018cd10891Snarayan }
41028cd10891Snarayan 
41031ae08745Sheppo /* -------------------------------------------------------------------------- */
41041ae08745Sheppo 
41051ae08745Sheppo /*
41061ae08745Sheppo  * The following functions process the incoming messages from vds
41071ae08745Sheppo  */
41081ae08745Sheppo 
41090a55fbb7Slm66018 /*
41100a55fbb7Slm66018  * Function:
41110a55fbb7Slm66018  *      vdc_process_msg_thread()
41120a55fbb7Slm66018  *
41130a55fbb7Slm66018  * Description:
41140a55fbb7Slm66018  *
41153af08d82Slm66018  *	Main VDC message processing thread. Each vDisk instance
41163af08d82Slm66018  * 	consists of a copy of this thread. This thread triggers
41173af08d82Slm66018  * 	all the handshakes and data exchange with the server. It
41183af08d82Slm66018  * 	also handles all channel resets
41193af08d82Slm66018  *
41200a55fbb7Slm66018  * Arguments:
41210a55fbb7Slm66018  *      vdc     - soft state pointer for this instance of the device driver.
41220a55fbb7Slm66018  *
41230a55fbb7Slm66018  * Return Code:
41240a55fbb7Slm66018  *      None
41250a55fbb7Slm66018  */
41261ae08745Sheppo static void
41273af08d82Slm66018 vdc_process_msg_thread(vdc_t *vdcp)
41281ae08745Sheppo {
41291ae08745Sheppo 	int		status;
4130655fd6a9Sachartre 	int		ctimeout;
4131655fd6a9Sachartre 	timeout_id_t	tmid = 0;
41328cd10891Snarayan 	clock_t		ldcup_timeout = 0;
41331ae08745Sheppo 
41343af08d82Slm66018 	mutex_enter(&vdcp->lock);
41351ae08745Sheppo 
41361ae08745Sheppo 	for (;;) {
41371ae08745Sheppo 
41383af08d82Slm66018 #define	Q(_s)	(vdcp->state == _s) ? #_s :
41393af08d82Slm66018 		DMSG(vdcp, 3, "state = %d (%s)\n", vdcp->state,
41403af08d82Slm66018 		    Q(VDC_STATE_INIT)
41413af08d82Slm66018 		    Q(VDC_STATE_INIT_WAITING)
41423af08d82Slm66018 		    Q(VDC_STATE_NEGOTIATE)
41433af08d82Slm66018 		    Q(VDC_STATE_HANDLE_PENDING)
41443af08d82Slm66018 		    Q(VDC_STATE_RUNNING)
41453af08d82Slm66018 		    Q(VDC_STATE_RESETTING)
41463af08d82Slm66018 		    Q(VDC_STATE_DETACH)
41473af08d82Slm66018 		    "UNKNOWN");
41481ae08745Sheppo 
41493af08d82Slm66018 		switch (vdcp->state) {
41503af08d82Slm66018 		case VDC_STATE_INIT:
41513af08d82Slm66018 
4152655fd6a9Sachartre 			/*
4153655fd6a9Sachartre 			 * If requested, start a timeout to check if the
4154655fd6a9Sachartre 			 * connection with vds is established in the
4155655fd6a9Sachartre 			 * specified delay. If the timeout expires, we
4156655fd6a9Sachartre 			 * will cancel any pending request.
4157655fd6a9Sachartre 			 *
4158655fd6a9Sachartre 			 * If some reset have occurred while establishing
4159655fd6a9Sachartre 			 * the connection, we already have a timeout armed
4160655fd6a9Sachartre 			 * and in that case we don't need to arm a new one.
41618cd10891Snarayan 			 *
41628cd10891Snarayan 			 * The same rule applies when there are multiple vds'.
41638cd10891Snarayan 			 * If either a connection cannot be established or
41648cd10891Snarayan 			 * the handshake times out, the connection thread will
41658cd10891Snarayan 			 * try another server. The 'ctimeout' will report
41668cd10891Snarayan 			 * back an error after it expires irrespective of
41678cd10891Snarayan 			 * whether the vdisk is trying to connect to just
41688cd10891Snarayan 			 * one or multiple servers.
4169655fd6a9Sachartre 			 */
4170655fd6a9Sachartre 			ctimeout = (vdc_timeout != 0)?
41718cd10891Snarayan 			    vdc_timeout : vdcp->curr_server->ctimeout;
4172655fd6a9Sachartre 
4173655fd6a9Sachartre 			if (ctimeout != 0 && tmid == 0) {
4174655fd6a9Sachartre 				tmid = timeout(vdc_connection_timeout, vdcp,
41758cd10891Snarayan 				    ctimeout * drv_usectohz(MICROSEC));
4176655fd6a9Sachartre 			}
4177655fd6a9Sachartre 
41788cd10891Snarayan 			/* Check if we are re-initializing repeatedly */
41798cd10891Snarayan 			if (vdcp->hshake_cnt > vdc_hshake_retries &&
4180655fd6a9Sachartre 			    vdcp->lifecycle != VDC_LC_ONLINE) {
41818cd10891Snarayan 
41828cd10891Snarayan 				DMSG(vdcp, 0, "[%d] too many handshakes,cnt=%d",
41838cd10891Snarayan 				    vdcp->instance, vdcp->hshake_cnt);
41843c96341aSnarayan 				cmn_err(CE_NOTE, "[%d] disk access failed.\n",
41853c96341aSnarayan 				    vdcp->instance);
41863af08d82Slm66018 				vdcp->state = VDC_STATE_DETACH;
41873af08d82Slm66018 				break;
41883af08d82Slm66018 			}
41893af08d82Slm66018 
41908cd10891Snarayan 			/* Switch to STATE_DETACH if drv is detaching */
41918cd10891Snarayan 			if (vdcp->lifecycle == VDC_LC_DETACHING) {
41928cd10891Snarayan 				vdcp->state = VDC_STATE_DETACH;
41938cd10891Snarayan 				break;
41948cd10891Snarayan 			}
41958cd10891Snarayan 
41968cd10891Snarayan 			/* Switch server */
41978cd10891Snarayan 			if (vdcp->hshake_cnt > 0)
41988cd10891Snarayan 				vdc_switch_server(vdcp);
41998cd10891Snarayan 			vdcp->hshake_cnt++;
42008cd10891Snarayan 
42013af08d82Slm66018 			/* Bring up connection with vds via LDC */
42023af08d82Slm66018 			status = vdc_start_ldc_connection(vdcp);
42038cd10891Snarayan 			if (status != EINVAL) {
42043af08d82Slm66018 				vdcp->state = VDC_STATE_INIT_WAITING;
42053af08d82Slm66018 			}
42063af08d82Slm66018 			break;
42073af08d82Slm66018 
42083af08d82Slm66018 		case VDC_STATE_INIT_WAITING:
42093af08d82Slm66018 
42108cd10891Snarayan 			/* if channel is UP, start negotiation */
42118cd10891Snarayan 			if (vdcp->curr_server->ldc_state == LDC_UP) {
42128cd10891Snarayan 				vdcp->state = VDC_STATE_NEGOTIATE;
42138cd10891Snarayan 				break;
42148cd10891Snarayan 			}
42158cd10891Snarayan 
42168cd10891Snarayan 			/* check if only one server exists */
42178cd10891Snarayan 			if (vdcp->num_servers == 1) {
42183af08d82Slm66018 				cv_wait(&vdcp->initwait_cv, &vdcp->lock);
42198cd10891Snarayan 			} else {
42208cd10891Snarayan 				/*
42218cd10891Snarayan 				 * wait for LDC_UP, if it times out, switch
42228cd10891Snarayan 				 * to another server.
42238cd10891Snarayan 				 */
42248cd10891Snarayan 				ldcup_timeout = ddi_get_lbolt() +
42258cd10891Snarayan 				    (vdc_ldcup_timeout *
42268cd10891Snarayan 				    drv_usectohz(MICROSEC));
42278cd10891Snarayan 				status = cv_timedwait(&vdcp->initwait_cv,
42288cd10891Snarayan 				    &vdcp->lock, ldcup_timeout);
42298cd10891Snarayan 				if (status == -1 &&
42308cd10891Snarayan 				    vdcp->state == VDC_STATE_INIT_WAITING &&
42318cd10891Snarayan 				    vdcp->curr_server->ldc_state != LDC_UP) {
42328cd10891Snarayan 					/* timed out & still waiting */
42338cd10891Snarayan 					vdcp->state = VDC_STATE_INIT;
42348cd10891Snarayan 					break;
42358cd10891Snarayan 				}
42368cd10891Snarayan 			}
42378cd10891Snarayan 
42383af08d82Slm66018 			if (vdcp->state != VDC_STATE_INIT_WAITING) {
42393af08d82Slm66018 				DMSG(vdcp, 0,
42403af08d82Slm66018 				    "state moved to %d out from under us...\n",
42413af08d82Slm66018 				    vdcp->state);
42423af08d82Slm66018 			}
42433af08d82Slm66018 			break;
42443af08d82Slm66018 
42453af08d82Slm66018 		case VDC_STATE_NEGOTIATE:
42463af08d82Slm66018 			switch (status = vdc_ver_negotiation(vdcp)) {
42473af08d82Slm66018 			case 0:
42483af08d82Slm66018 				break;
42493af08d82Slm66018 			default:
42503af08d82Slm66018 				DMSG(vdcp, 0, "ver negotiate failed (%d)..\n",
42513af08d82Slm66018 				    status);
42523af08d82Slm66018 				goto reset;
42533af08d82Slm66018 			}
42543af08d82Slm66018 
42553af08d82Slm66018 			switch (status = vdc_attr_negotiation(vdcp)) {
42563af08d82Slm66018 			case 0:
42573af08d82Slm66018 				break;
42583af08d82Slm66018 			default:
42593af08d82Slm66018 				DMSG(vdcp, 0, "attr negotiate failed (%d)..\n",
42603af08d82Slm66018 				    status);
42613af08d82Slm66018 				goto reset;
42623af08d82Slm66018 			}
42633af08d82Slm66018 
42643af08d82Slm66018 			switch (status = vdc_dring_negotiation(vdcp)) {
42653af08d82Slm66018 			case 0:
42663af08d82Slm66018 				break;
42673af08d82Slm66018 			default:
42683af08d82Slm66018 				DMSG(vdcp, 0, "dring negotiate failed (%d)..\n",
42693af08d82Slm66018 				    status);
42703af08d82Slm66018 				goto reset;
42713af08d82Slm66018 			}
42723af08d82Slm66018 
42733af08d82Slm66018 			switch (status = vdc_rdx_exchange(vdcp)) {
42743af08d82Slm66018 			case 0:
42753af08d82Slm66018 				vdcp->state = VDC_STATE_HANDLE_PENDING;
42763af08d82Slm66018 				goto done;
42773af08d82Slm66018 			default:
42783af08d82Slm66018 				DMSG(vdcp, 0, "RDX xchg failed ..(%d)\n",
42793af08d82Slm66018 				    status);
42803af08d82Slm66018 				goto reset;
42813af08d82Slm66018 			}
42823af08d82Slm66018 reset:
42833af08d82Slm66018 			DMSG(vdcp, 0, "negotiation failed: resetting (%d)\n",
42843af08d82Slm66018 			    status);
42853af08d82Slm66018 			vdcp->state = VDC_STATE_RESETTING;
4286655fd6a9Sachartre 			vdcp->self_reset = B_TRUE;
42873af08d82Slm66018 done:
42883af08d82Slm66018 			DMSG(vdcp, 0, "negotiation complete (state=0x%x)...\n",
42893af08d82Slm66018 			    vdcp->state);
42903af08d82Slm66018 			break;
42913af08d82Slm66018 
42923af08d82Slm66018 		case VDC_STATE_HANDLE_PENDING:
42933af08d82Slm66018 
4294655fd6a9Sachartre 			if (vdcp->ctimeout_reached) {
4295655fd6a9Sachartre 				/*
4296655fd6a9Sachartre 				 * The connection timeout had been reached so
4297655fd6a9Sachartre 				 * pending requests have been cancelled. Now
4298655fd6a9Sachartre 				 * that the connection is back we can reset
4299655fd6a9Sachartre 				 * the timeout.
4300655fd6a9Sachartre 				 */
4301655fd6a9Sachartre 				ASSERT(vdcp->local_dring_backup == NULL);
4302655fd6a9Sachartre 				ASSERT(tmid != 0);
4303655fd6a9Sachartre 				tmid = 0;
4304655fd6a9Sachartre 				vdcp->ctimeout_reached = B_FALSE;
4305655fd6a9Sachartre 				vdcp->state = VDC_STATE_RUNNING;
4306655fd6a9Sachartre 				DMSG(vdcp, 0, "[%d] connection to service "
4307655fd6a9Sachartre 				    "domain is up", vdcp->instance);
4308655fd6a9Sachartre 				break;
4309655fd6a9Sachartre 			}
4310655fd6a9Sachartre 
43113af08d82Slm66018 			mutex_exit(&vdcp->lock);
4312655fd6a9Sachartre 			if (tmid != 0) {
4313655fd6a9Sachartre 				(void) untimeout(tmid);
4314655fd6a9Sachartre 				tmid = 0;
4315655fd6a9Sachartre 			}
43163af08d82Slm66018 			status = vdc_resubmit_backup_dring(vdcp);
43173af08d82Slm66018 			mutex_enter(&vdcp->lock);
43183af08d82Slm66018 
43193af08d82Slm66018 			if (status)
43203af08d82Slm66018 				vdcp->state = VDC_STATE_RESETTING;
43213af08d82Slm66018 			else
43223af08d82Slm66018 				vdcp->state = VDC_STATE_RUNNING;
43233af08d82Slm66018 
43243af08d82Slm66018 			break;
43253af08d82Slm66018 
43263af08d82Slm66018 		/* enter running state */
43273af08d82Slm66018 		case VDC_STATE_RUNNING:
43283af08d82Slm66018 			/*
43293af08d82Slm66018 			 * Signal anyone waiting for the connection
43303af08d82Slm66018 			 * to come on line.
43313af08d82Slm66018 			 */
43323af08d82Slm66018 			vdcp->hshake_cnt = 0;
43333af08d82Slm66018 			cv_broadcast(&vdcp->running_cv);
43342f5224aeSachartre 
43352f5224aeSachartre 			/* failfast has to been checked after reset */
43362f5224aeSachartre 			cv_signal(&vdcp->failfast_cv);
43372f5224aeSachartre 
43382f5224aeSachartre 			/* ownership is lost during reset */
43392f5224aeSachartre 			if (vdcp->ownership & VDC_OWNERSHIP_WANTED)
43402f5224aeSachartre 				vdcp->ownership |= VDC_OWNERSHIP_RESET;
43412f5224aeSachartre 			cv_signal(&vdcp->ownership_cv);
43422f5224aeSachartre 
4343*d7400d00Sachartre 			cmn_err(CE_CONT, "?vdisk@%d is online using "
4344*d7400d00Sachartre 			    "ldc@%ld,%ld\n", vdcp->instance,
4345*d7400d00Sachartre 			    vdcp->curr_server->ldc_id, vdcp->curr_server->id);
4346*d7400d00Sachartre 
43473af08d82Slm66018 			mutex_exit(&vdcp->lock);
43483af08d82Slm66018 
43493af08d82Slm66018 			for (;;) {
43503af08d82Slm66018 				vio_msg_t msg;
43513af08d82Slm66018 				status = vdc_wait_for_response(vdcp, &msg);
43523af08d82Slm66018 				if (status) break;
43533af08d82Slm66018 
43543af08d82Slm66018 				DMSG(vdcp, 1, "[%d] new pkt(s) available\n",
43553af08d82Slm66018 				    vdcp->instance);
43563af08d82Slm66018 				status = vdc_process_data_msg(vdcp, &msg);
43571ae08745Sheppo 				if (status) {
43583af08d82Slm66018 					DMSG(vdcp, 1, "[%d] process_data_msg "
43593af08d82Slm66018 					    "returned err=%d\n", vdcp->instance,
43603af08d82Slm66018 					    status);
43611ae08745Sheppo 					break;
43621ae08745Sheppo 				}
43631ae08745Sheppo 
43643af08d82Slm66018 			}
4365e1ebb9ecSlm66018 
43663af08d82Slm66018 			mutex_enter(&vdcp->lock);
43673af08d82Slm66018 
4368*d7400d00Sachartre 			cmn_err(CE_CONT, "?vdisk@%d is offline\n",
4369*d7400d00Sachartre 			    vdcp->instance);
4370*d7400d00Sachartre 
43713af08d82Slm66018 			vdcp->state = VDC_STATE_RESETTING;
4372690555a1Sachartre 			vdcp->self_reset = B_TRUE;
43733af08d82Slm66018 			break;
43743af08d82Slm66018 
43753af08d82Slm66018 		case VDC_STATE_RESETTING:
4376655fd6a9Sachartre 			/*
4377655fd6a9Sachartre 			 * When we reach this state, we either come from the
4378655fd6a9Sachartre 			 * VDC_STATE_RUNNING state and we can have pending
4379655fd6a9Sachartre 			 * request but no timeout is armed; or we come from
4380655fd6a9Sachartre 			 * the VDC_STATE_INIT_WAITING, VDC_NEGOTIATE or
4381655fd6a9Sachartre 			 * VDC_HANDLE_PENDING state and there is no pending
4382655fd6a9Sachartre 			 * request or pending requests have already been copied
4383655fd6a9Sachartre 			 * into the backup dring. So we can safely keep the
4384655fd6a9Sachartre 			 * connection timeout armed while we are in this state.
4385655fd6a9Sachartre 			 */
4386655fd6a9Sachartre 
43873af08d82Slm66018 			DMSG(vdcp, 0, "Initiating channel reset "
43883af08d82Slm66018 			    "(pending = %d)\n", (int)vdcp->threads_pending);
43893af08d82Slm66018 
43903af08d82Slm66018 			if (vdcp->self_reset) {
43913af08d82Slm66018 				DMSG(vdcp, 0,
43923af08d82Slm66018 				    "[%d] calling stop_ldc_connection.\n",
43933af08d82Slm66018 				    vdcp->instance);
43943af08d82Slm66018 				status = vdc_stop_ldc_connection(vdcp);
43953af08d82Slm66018 				vdcp->self_reset = B_FALSE;
43961ae08745Sheppo 			}
43971ae08745Sheppo 
43981ae08745Sheppo 			/*
43993af08d82Slm66018 			 * Wait for all threads currently waiting
44003af08d82Slm66018 			 * for a free dring entry to use.
44011ae08745Sheppo 			 */
44023af08d82Slm66018 			while (vdcp->threads_pending) {
44033af08d82Slm66018 				cv_broadcast(&vdcp->membind_cv);
44043af08d82Slm66018 				cv_broadcast(&vdcp->dring_free_cv);
44053af08d82Slm66018 				mutex_exit(&vdcp->lock);
4406205eeb1aSlm66018 				/* give the waiters enough time to wake up */
4407205eeb1aSlm66018 				delay(vdc_hz_min_ldc_delay);
44083af08d82Slm66018 				mutex_enter(&vdcp->lock);
44091ae08745Sheppo 			}
44101ae08745Sheppo 
44113af08d82Slm66018 			ASSERT(vdcp->threads_pending == 0);
44121ae08745Sheppo 
44133af08d82Slm66018 			/* Sanity check that no thread is receiving */
44143af08d82Slm66018 			ASSERT(vdcp->read_state != VDC_READ_WAITING);
44150a55fbb7Slm66018 
44163af08d82Slm66018 			vdcp->read_state = VDC_READ_IDLE;
44173af08d82Slm66018 
44183af08d82Slm66018 			vdc_backup_local_dring(vdcp);
44193af08d82Slm66018 
44203af08d82Slm66018 			/* cleanup the old d-ring */
44213af08d82Slm66018 			vdc_destroy_descriptor_ring(vdcp);
44223af08d82Slm66018 
44233af08d82Slm66018 			/* go and start again */
44243af08d82Slm66018 			vdcp->state = VDC_STATE_INIT;
44253af08d82Slm66018 
44260a55fbb7Slm66018 			break;
44270a55fbb7Slm66018 
44283af08d82Slm66018 		case VDC_STATE_DETACH:
44293af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Reset thread exit cleanup ..\n",
44303af08d82Slm66018 			    vdcp->instance);
44313af08d82Slm66018 
4432655fd6a9Sachartre 			/* cancel any pending timeout */
4433655fd6a9Sachartre 			mutex_exit(&vdcp->lock);
4434655fd6a9Sachartre 			if (tmid != 0) {
4435655fd6a9Sachartre 				(void) untimeout(tmid);
4436655fd6a9Sachartre 				tmid = 0;
4437655fd6a9Sachartre 			}
4438655fd6a9Sachartre 			mutex_enter(&vdcp->lock);
4439655fd6a9Sachartre 
44403c96341aSnarayan 			/*
44413c96341aSnarayan 			 * Signal anyone waiting for connection
44423c96341aSnarayan 			 * to come online
44433c96341aSnarayan 			 */
44443c96341aSnarayan 			cv_broadcast(&vdcp->running_cv);
44453c96341aSnarayan 
44463af08d82Slm66018 			while (vdcp->sync_op_pending) {
44473af08d82Slm66018 				cv_signal(&vdcp->sync_pending_cv);
44483af08d82Slm66018 				cv_signal(&vdcp->sync_blocked_cv);
44493af08d82Slm66018 				mutex_exit(&vdcp->lock);
4450205eeb1aSlm66018 				/* give the waiters enough time to wake up */
4451205eeb1aSlm66018 				delay(vdc_hz_min_ldc_delay);
44523af08d82Slm66018 				mutex_enter(&vdcp->lock);
44530a55fbb7Slm66018 			}
44541ae08745Sheppo 
44553af08d82Slm66018 			mutex_exit(&vdcp->lock);
44563af08d82Slm66018 
44573af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Msg processing thread exiting ..\n",
44583af08d82Slm66018 			    vdcp->instance);
44593af08d82Slm66018 			thread_exit();
44603af08d82Slm66018 			break;
44613af08d82Slm66018 		}
44623af08d82Slm66018 	}
44630a55fbb7Slm66018 }
44640a55fbb7Slm66018 
44650a55fbb7Slm66018 
44660a55fbb7Slm66018 /*
44670a55fbb7Slm66018  * Function:
44680a55fbb7Slm66018  *	vdc_process_data_msg()
44690a55fbb7Slm66018  *
44700a55fbb7Slm66018  * Description:
44710a55fbb7Slm66018  *	This function is called by the message processing thread each time
44720a55fbb7Slm66018  *	a message with a msgtype of VIO_TYPE_DATA is received. It will either
44730a55fbb7Slm66018  *	be an ACK or NACK from vds[1] which vdc handles as follows.
44740a55fbb7Slm66018  *		ACK	- wake up the waiting thread
44750a55fbb7Slm66018  *		NACK	- resend any messages necessary
44760a55fbb7Slm66018  *
44770a55fbb7Slm66018  *	[1] Although the message format allows it, vds should not send a
44780a55fbb7Slm66018  *	    VIO_SUBTYPE_INFO message to vdc asking it to read data; if for
44790a55fbb7Slm66018  *	    some bizarre reason it does, vdc will reset the connection.
44800a55fbb7Slm66018  *
44810a55fbb7Slm66018  * Arguments:
44820a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
44830a55fbb7Slm66018  *	msg	- the LDC message sent by vds
44840a55fbb7Slm66018  *
44850a55fbb7Slm66018  * Return Code:
44860a55fbb7Slm66018  *	0	- Success.
44870a55fbb7Slm66018  *	> 0	- error value returned by LDC
44880a55fbb7Slm66018  */
44890a55fbb7Slm66018 static int
44903af08d82Slm66018 vdc_process_data_msg(vdc_t *vdcp, vio_msg_t *msg)
44910a55fbb7Slm66018 {
44920a55fbb7Slm66018 	int			status = 0;
44933af08d82Slm66018 	vio_dring_msg_t		*dring_msg;
4494d10e4ef2Snarayan 	vdc_local_desc_t	*ldep = NULL;
44953af08d82Slm66018 	int			start, end;
44963af08d82Slm66018 	int			idx;
449790e2f9dcSlm66018 	int			op;
44980a55fbb7Slm66018 
44993af08d82Slm66018 	dring_msg = (vio_dring_msg_t *)msg;
45000a55fbb7Slm66018 
45013af08d82Slm66018 	ASSERT(msg->tag.vio_msgtype == VIO_TYPE_DATA);
45023af08d82Slm66018 	ASSERT(vdcp != NULL);
45033af08d82Slm66018 
45043af08d82Slm66018 	mutex_enter(&vdcp->lock);
45050a55fbb7Slm66018 
45060a55fbb7Slm66018 	/*
45070a55fbb7Slm66018 	 * Check to see if the message has bogus data
45080a55fbb7Slm66018 	 */
4509e1ebb9ecSlm66018 	idx = start = dring_msg->start_idx;
45100a55fbb7Slm66018 	end = dring_msg->end_idx;
45113af08d82Slm66018 	if ((start >= vdcp->dring_len) ||
45123af08d82Slm66018 	    (end >= vdcp->dring_len) || (end < -1)) {
451390e2f9dcSlm66018 		/*
451490e2f9dcSlm66018 		 * Update the I/O statistics to indicate that an error ocurred.
451590e2f9dcSlm66018 		 * No need to update the wait/run queues as no specific read or
451690e2f9dcSlm66018 		 * write request is being completed in response to this 'msg'.
451790e2f9dcSlm66018 		 */
451890e2f9dcSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
45193af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Bogus ACK data : start %d, end %d\n",
45203af08d82Slm66018 		    vdcp->instance, start, end);
45213af08d82Slm66018 		mutex_exit(&vdcp->lock);
4522e1ebb9ecSlm66018 		return (EINVAL);
45230a55fbb7Slm66018 	}
45240a55fbb7Slm66018 
45250a55fbb7Slm66018 	/*
45260a55fbb7Slm66018 	 * Verify that the sequence number is what vdc expects.
45270a55fbb7Slm66018 	 */
45283af08d82Slm66018 	switch (vdc_verify_seq_num(vdcp, dring_msg)) {
4529e1ebb9ecSlm66018 	case VDC_SEQ_NUM_TODO:
4530e1ebb9ecSlm66018 		break;	/* keep processing this message */
4531e1ebb9ecSlm66018 	case VDC_SEQ_NUM_SKIP:
45323af08d82Slm66018 		mutex_exit(&vdcp->lock);
4533e1ebb9ecSlm66018 		return (0);
4534e1ebb9ecSlm66018 	case VDC_SEQ_NUM_INVALID:
453590e2f9dcSlm66018 		/*
453690e2f9dcSlm66018 		 * Update the I/O statistics to indicate that an error ocurred.
453790e2f9dcSlm66018 		 * No need to update the wait/run queues as no specific read or
453890e2f9dcSlm66018 		 * write request is being completed in response to this 'msg'.
453990e2f9dcSlm66018 		 */
4540366a92acSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
454190e2f9dcSlm66018 		DMSG(vdcp, 0, "[%d] invalid seqno\n", vdcp->instance);
4542366a92acSlm66018 		mutex_exit(&vdcp->lock);
45430a55fbb7Slm66018 		return (ENXIO);
45440a55fbb7Slm66018 	}
45450a55fbb7Slm66018 
45463af08d82Slm66018 	if (msg->tag.vio_subtype == VIO_SUBTYPE_NACK) {
454790e2f9dcSlm66018 		/*
454890e2f9dcSlm66018 		 * Update the I/O statistics to indicate that an error ocurred.
454990e2f9dcSlm66018 		 *
455090e2f9dcSlm66018 		 * We need to update the run queue if a read or write request
455190e2f9dcSlm66018 		 * is being NACKed - otherwise there will appear to be an
455290e2f9dcSlm66018 		 * indefinite outstanding request and statistics reported by
455390e2f9dcSlm66018 		 * iostat(1M) will be incorrect. The transaction will be
455490e2f9dcSlm66018 		 * resubmitted from the backup DRing following the reset
455590e2f9dcSlm66018 		 * and the wait/run queues will be entered again.
455690e2f9dcSlm66018 		 */
455790e2f9dcSlm66018 		ldep = &vdcp->local_dring[idx];
455890e2f9dcSlm66018 		op = ldep->operation;
455990e2f9dcSlm66018 		if ((op == VD_OP_BREAD) || (op == VD_OP_BWRITE)) {
456090e2f9dcSlm66018 			DTRACE_IO1(done, buf_t *, ldep->cb_arg);
456190e2f9dcSlm66018 			VD_KSTAT_RUNQ_EXIT(vdcp);
456290e2f9dcSlm66018 		}
4563366a92acSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
456490e2f9dcSlm66018 		VDC_DUMP_DRING_MSG(dring_msg);
456590e2f9dcSlm66018 		DMSG(vdcp, 0, "[%d] DATA NACK\n", vdcp->instance);
45663af08d82Slm66018 		mutex_exit(&vdcp->lock);
4567e1ebb9ecSlm66018 		return (EIO);
45680a55fbb7Slm66018 
45693af08d82Slm66018 	} else if (msg->tag.vio_subtype == VIO_SUBTYPE_INFO) {
457090e2f9dcSlm66018 		/*
457190e2f9dcSlm66018 		 * Update the I/O statistics to indicate that an error occurred.
457290e2f9dcSlm66018 		 * No need to update the wait/run queues as no specific read or
457390e2f9dcSlm66018 		 * write request is being completed in response to this 'msg'.
457490e2f9dcSlm66018 		 */
4575366a92acSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_protoerrs);
45763af08d82Slm66018 		mutex_exit(&vdcp->lock);
4577e1ebb9ecSlm66018 		return (EPROTO);
4578e1ebb9ecSlm66018 	}
4579e1ebb9ecSlm66018 
45803af08d82Slm66018 	DMSG(vdcp, 1, ": start %d end %d\n", start, end);
45813af08d82Slm66018 	ASSERT(start == end);
45823af08d82Slm66018 
45833af08d82Slm66018 	ldep = &vdcp->local_dring[idx];
45843af08d82Slm66018 
45853af08d82Slm66018 	DMSG(vdcp, 1, ": state 0x%x - cb_type 0x%x\n",
45863af08d82Slm66018 	    ldep->dep->hdr.dstate, ldep->cb_type);
45873af08d82Slm66018 
4588e1ebb9ecSlm66018 	if (ldep->dep->hdr.dstate == VIO_DESC_DONE) {
45893af08d82Slm66018 		struct buf *bufp;
4590e1ebb9ecSlm66018 
45913af08d82Slm66018 		switch (ldep->cb_type) {
45923af08d82Slm66018 		case CB_SYNC:
45933af08d82Slm66018 			ASSERT(vdcp->sync_op_pending);
4594d10e4ef2Snarayan 
45953af08d82Slm66018 			status = vdc_depopulate_descriptor(vdcp, idx);
45963af08d82Slm66018 			vdcp->sync_op_status = status;
45973af08d82Slm66018 			vdcp->sync_op_pending = B_FALSE;
45983af08d82Slm66018 			cv_signal(&vdcp->sync_pending_cv);
45993af08d82Slm66018 			break;
46004bac2208Snarayan 
46013af08d82Slm66018 		case CB_STRATEGY:
46023af08d82Slm66018 			bufp = ldep->cb_arg;
46033af08d82Slm66018 			ASSERT(bufp != NULL);
46043c96341aSnarayan 			bufp->b_resid =
46053c96341aSnarayan 			    bufp->b_bcount - ldep->dep->payload.nbytes;
46063af08d82Slm66018 			status = ldep->dep->payload.status; /* Future:ntoh */
46073af08d82Slm66018 			if (status != 0) {
46083af08d82Slm66018 				DMSG(vdcp, 1, "strategy status=%d\n", status);
4609366a92acSlm66018 				VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
46103af08d82Slm66018 				bioerror(bufp, status);
4611d10e4ef2Snarayan 			}
46122f5224aeSachartre 
46132f5224aeSachartre 			(void) vdc_depopulate_descriptor(vdcp, idx);
46143c96341aSnarayan 
46153c96341aSnarayan 			DMSG(vdcp, 1,
46163c96341aSnarayan 			    "strategy complete req=%ld bytes resp=%ld bytes\n",
46173c96341aSnarayan 			    bufp->b_bcount, ldep->dep->payload.nbytes);
46182f5224aeSachartre 
46192f5224aeSachartre 			if (status != 0 && vdcp->failfast_interval != 0) {
46202f5224aeSachartre 				/*
46212f5224aeSachartre 				 * The I/O has failed and failfast is enabled.
46222f5224aeSachartre 				 * We need the failfast thread to check if the
46232f5224aeSachartre 				 * failure is due to a reservation conflict.
46242f5224aeSachartre 				 */
46252f5224aeSachartre 				(void) vdc_failfast_io_queue(vdcp, bufp);
46262f5224aeSachartre 			} else {
4627366a92acSlm66018 				if (status == 0) {
462890e2f9dcSlm66018 					op = (bufp->b_flags & B_READ) ?
4629366a92acSlm66018 					    VD_OP_BREAD : VD_OP_BWRITE;
4630366a92acSlm66018 					VD_UPDATE_IO_STATS(vdcp, op,
4631366a92acSlm66018 					    ldep->dep->payload.nbytes);
4632366a92acSlm66018 				}
463390e2f9dcSlm66018 				VD_KSTAT_RUNQ_EXIT(vdcp);
4634366a92acSlm66018 				DTRACE_IO1(done, buf_t *, bufp);
46352f5224aeSachartre 				biodone(bufp);
46362f5224aeSachartre 			}
46373af08d82Slm66018 			break;
46383af08d82Slm66018 
46393af08d82Slm66018 		default:
46403af08d82Slm66018 			ASSERT(0);
46410a55fbb7Slm66018 		}
46423af08d82Slm66018 	}
46433af08d82Slm66018 
46443af08d82Slm66018 	/* let the arrival signal propogate */
46453af08d82Slm66018 	mutex_exit(&vdcp->lock);
46460a55fbb7Slm66018 
4647e1ebb9ecSlm66018 	/* probe gives the count of how many entries were processed */
4648366a92acSlm66018 	DTRACE_PROBE2(processed, int, 1, vdc_t *, vdcp);
46490a55fbb7Slm66018 
46503af08d82Slm66018 	return (0);
46510a55fbb7Slm66018 }
46520a55fbb7Slm66018 
46530a55fbb7Slm66018 
46540a55fbb7Slm66018 /*
46550a55fbb7Slm66018  * Function:
46560a55fbb7Slm66018  *	vdc_handle_ver_msg()
46570a55fbb7Slm66018  *
46580a55fbb7Slm66018  * Description:
46590a55fbb7Slm66018  *
46600a55fbb7Slm66018  * Arguments:
46610a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
46620a55fbb7Slm66018  *	ver_msg	- LDC message sent by vDisk server
46630a55fbb7Slm66018  *
46640a55fbb7Slm66018  * Return Code:
46650a55fbb7Slm66018  *	0	- Success
46660a55fbb7Slm66018  */
46670a55fbb7Slm66018 static int
46680a55fbb7Slm66018 vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg)
46690a55fbb7Slm66018 {
46700a55fbb7Slm66018 	int status = 0;
46710a55fbb7Slm66018 
46720a55fbb7Slm66018 	ASSERT(vdc != NULL);
46730a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
46740a55fbb7Slm66018 
46750a55fbb7Slm66018 	if (ver_msg->tag.vio_subtype_env != VIO_VER_INFO) {
46760a55fbb7Slm66018 		return (EPROTO);
46770a55fbb7Slm66018 	}
46780a55fbb7Slm66018 
46790a55fbb7Slm66018 	if (ver_msg->dev_class != VDEV_DISK_SERVER) {
46800a55fbb7Slm66018 		return (EINVAL);
46810a55fbb7Slm66018 	}
46820a55fbb7Slm66018 
46830a55fbb7Slm66018 	switch (ver_msg->tag.vio_subtype) {
46840a55fbb7Slm66018 	case VIO_SUBTYPE_ACK:
46850a55fbb7Slm66018 		/*
46860a55fbb7Slm66018 		 * We check to see if the version returned is indeed supported
46870a55fbb7Slm66018 		 * (The server may have also adjusted the minor number downwards
46880a55fbb7Slm66018 		 * and if so 'ver_msg' will contain the actual version agreed)
46890a55fbb7Slm66018 		 */
46900a55fbb7Slm66018 		if (vdc_is_supported_version(ver_msg)) {
46910a55fbb7Slm66018 			vdc->ver.major = ver_msg->ver_major;
46920a55fbb7Slm66018 			vdc->ver.minor = ver_msg->ver_minor;
46930a55fbb7Slm66018 			ASSERT(vdc->ver.major > 0);
46940a55fbb7Slm66018 		} else {
46950a55fbb7Slm66018 			status = EPROTO;
46960a55fbb7Slm66018 		}
46970a55fbb7Slm66018 		break;
46980a55fbb7Slm66018 
46990a55fbb7Slm66018 	case VIO_SUBTYPE_NACK:
47000a55fbb7Slm66018 		/*
47010a55fbb7Slm66018 		 * call vdc_is_supported_version() which will return the next
47020a55fbb7Slm66018 		 * supported version (if any) in 'ver_msg'
47030a55fbb7Slm66018 		 */
47040a55fbb7Slm66018 		(void) vdc_is_supported_version(ver_msg);
47050a55fbb7Slm66018 		if (ver_msg->ver_major > 0) {
47060a55fbb7Slm66018 			size_t len = sizeof (*ver_msg);
47070a55fbb7Slm66018 
47080a55fbb7Slm66018 			ASSERT(vdc->ver.major > 0);
47090a55fbb7Slm66018 
47100a55fbb7Slm66018 			/* reset the necessary fields and resend */
47110a55fbb7Slm66018 			ver_msg->tag.vio_subtype = VIO_SUBTYPE_INFO;
47120a55fbb7Slm66018 			ver_msg->dev_class = VDEV_DISK;
47130a55fbb7Slm66018 
47140a55fbb7Slm66018 			status = vdc_send(vdc, (caddr_t)ver_msg, &len);
47153af08d82Slm66018 			DMSG(vdc, 0, "[%d] Resend VER info (LDC status = %d)\n",
47160a55fbb7Slm66018 			    vdc->instance, status);
47170a55fbb7Slm66018 			if (len != sizeof (*ver_msg))
47180a55fbb7Slm66018 				status = EBADMSG;
47190a55fbb7Slm66018 		} else {
472087a7269eSachartre 			DMSG(vdc, 0, "[%d] No common version with vDisk server",
472187a7269eSachartre 			    vdc->instance);
47220a55fbb7Slm66018 			status = ENOTSUP;
47230a55fbb7Slm66018 		}
47240a55fbb7Slm66018 
47250a55fbb7Slm66018 		break;
47261ae08745Sheppo 	case VIO_SUBTYPE_INFO:
47271ae08745Sheppo 		/*
47281ae08745Sheppo 		 * Handle the case where vds starts handshake
4729eff7243fSlm66018 		 * (for now only vdc is the instigator)
47301ae08745Sheppo 		 */
47311ae08745Sheppo 		status = ENOTSUP;
47321ae08745Sheppo 		break;
47331ae08745Sheppo 
47341ae08745Sheppo 	default:
47350a55fbb7Slm66018 		status = EINVAL;
47361ae08745Sheppo 		break;
47371ae08745Sheppo 	}
47381ae08745Sheppo 
47390a55fbb7Slm66018 	return (status);
47400a55fbb7Slm66018 }
47410a55fbb7Slm66018 
47420a55fbb7Slm66018 /*
47430a55fbb7Slm66018  * Function:
47440a55fbb7Slm66018  *	vdc_handle_attr_msg()
47450a55fbb7Slm66018  *
47460a55fbb7Slm66018  * Description:
47470a55fbb7Slm66018  *
47480a55fbb7Slm66018  * Arguments:
47490a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
47500a55fbb7Slm66018  *	attr_msg	- LDC message sent by vDisk server
47510a55fbb7Slm66018  *
47520a55fbb7Slm66018  * Return Code:
47530a55fbb7Slm66018  *	0	- Success
47540a55fbb7Slm66018  */
47550a55fbb7Slm66018 static int
47560a55fbb7Slm66018 vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg)
47570a55fbb7Slm66018 {
47580a55fbb7Slm66018 	int status = 0;
47590a55fbb7Slm66018 
47600a55fbb7Slm66018 	ASSERT(vdc != NULL);
47610a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
47620a55fbb7Slm66018 
47630a55fbb7Slm66018 	if (attr_msg->tag.vio_subtype_env != VIO_ATTR_INFO) {
47640a55fbb7Slm66018 		return (EPROTO);
47650a55fbb7Slm66018 	}
47660a55fbb7Slm66018 
47670a55fbb7Slm66018 	switch (attr_msg->tag.vio_subtype) {
47681ae08745Sheppo 	case VIO_SUBTYPE_ACK:
47691ae08745Sheppo 		/*
47701ae08745Sheppo 		 * We now verify the attributes sent by vds.
47711ae08745Sheppo 		 */
477278fcd0a1Sachartre 		if (attr_msg->vdisk_size == 0) {
477378fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Invalid disk size from vds",
477478fcd0a1Sachartre 			    vdc->instance);
477578fcd0a1Sachartre 			status = EINVAL;
477678fcd0a1Sachartre 			break;
477778fcd0a1Sachartre 		}
477878fcd0a1Sachartre 
477978fcd0a1Sachartre 		if (attr_msg->max_xfer_sz == 0) {
478078fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Invalid transfer size from vds",
478178fcd0a1Sachartre 			    vdc->instance);
478278fcd0a1Sachartre 			status = EINVAL;
478378fcd0a1Sachartre 			break;
478478fcd0a1Sachartre 		}
478578fcd0a1Sachartre 
47862f5224aeSachartre 		if (attr_msg->vdisk_size == VD_SIZE_UNKNOWN) {
47872f5224aeSachartre 			DMSG(vdc, 0, "[%d] Unknown disk size from vds",
47882f5224aeSachartre 			    vdc->instance);
47892f5224aeSachartre 			attr_msg->vdisk_size = 0;
47902f5224aeSachartre 		}
47912f5224aeSachartre 
479278fcd0a1Sachartre 		/*
479378fcd0a1Sachartre 		 * If the disk size is already set check that it hasn't changed.
479478fcd0a1Sachartre 		 */
47952f5224aeSachartre 		if ((vdc->vdisk_size != 0) && (attr_msg->vdisk_size != 0) &&
479678fcd0a1Sachartre 		    (vdc->vdisk_size != attr_msg->vdisk_size)) {
479778fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Different disk size from vds "
479878fcd0a1Sachartre 			    "(old=0x%lx - new=0x%lx", vdc->instance,
479978fcd0a1Sachartre 			    vdc->vdisk_size, attr_msg->vdisk_size)
480078fcd0a1Sachartre 			status = EINVAL;
480178fcd0a1Sachartre 			break;
480278fcd0a1Sachartre 		}
480378fcd0a1Sachartre 
48041ae08745Sheppo 		vdc->vdisk_size = attr_msg->vdisk_size;
48051ae08745Sheppo 		vdc->vdisk_type = attr_msg->vdisk_type;
480617cadca8Slm66018 		vdc->operations = attr_msg->operations;
480717cadca8Slm66018 		if (vio_ver_is_supported(vdc->ver, 1, 1))
480817cadca8Slm66018 			vdc->vdisk_media = attr_msg->vdisk_media;
480917cadca8Slm66018 		else
481017cadca8Slm66018 			vdc->vdisk_media = 0;
48111ae08745Sheppo 
48123af08d82Slm66018 		DMSG(vdc, 0, "[%d] max_xfer_sz: sent %lx acked %lx\n",
4813e1ebb9ecSlm66018 		    vdc->instance, vdc->max_xfer_sz, attr_msg->max_xfer_sz);
48143af08d82Slm66018 		DMSG(vdc, 0, "[%d] vdisk_block_size: sent %lx acked %x\n",
4815e1ebb9ecSlm66018 		    vdc->instance, vdc->block_size,
4816e1ebb9ecSlm66018 		    attr_msg->vdisk_block_size);
4817e1ebb9ecSlm66018 
48181ae08745Sheppo 		/*
4819e1ebb9ecSlm66018 		 * We don't know at compile time what the vDisk server will
482017cadca8Slm66018 		 * think are good values but we apply a large (arbitrary)
4821e1ebb9ecSlm66018 		 * upper bound to prevent memory exhaustion in vdc if it was
4822e1ebb9ecSlm66018 		 * allocating a DRing based of huge values sent by the server.
4823e1ebb9ecSlm66018 		 * We probably will never exceed this except if the message
4824e1ebb9ecSlm66018 		 * was garbage.
48251ae08745Sheppo 		 */
4826e1ebb9ecSlm66018 		if ((attr_msg->max_xfer_sz * attr_msg->vdisk_block_size) <=
4827e1ebb9ecSlm66018 		    (PAGESIZE * DEV_BSIZE)) {
4828e1ebb9ecSlm66018 			vdc->max_xfer_sz = attr_msg->max_xfer_sz;
4829e1ebb9ecSlm66018 			vdc->block_size = attr_msg->vdisk_block_size;
4830e1ebb9ecSlm66018 		} else {
48313af08d82Slm66018 			DMSG(vdc, 0, "[%d] vds block transfer size too big;"
4832e1ebb9ecSlm66018 			    " using max supported by vdc", vdc->instance);
48331ae08745Sheppo 		}
48341ae08745Sheppo 
4835f0ca1d9aSsb155480 		if ((attr_msg->xfer_mode != VIO_DRING_MODE_V1_0) ||
48361ae08745Sheppo 		    (attr_msg->vdisk_size > INT64_MAX) ||
483717cadca8Slm66018 		    (attr_msg->operations == 0) ||
48381ae08745Sheppo 		    (attr_msg->vdisk_type > VD_DISK_TYPE_DISK)) {
48393af08d82Slm66018 			DMSG(vdc, 0, "[%d] Invalid attributes from vds",
4840e1ebb9ecSlm66018 			    vdc->instance);
48411ae08745Sheppo 			status = EINVAL;
48421ae08745Sheppo 			break;
48431ae08745Sheppo 		}
48441ae08745Sheppo 
484578fcd0a1Sachartre 		/*
484678fcd0a1Sachartre 		 * Now that we have received all attributes we can create a
484778fcd0a1Sachartre 		 * fake geometry for the disk.
484878fcd0a1Sachartre 		 */
484978fcd0a1Sachartre 		vdc_create_fake_geometry(vdc);
48501ae08745Sheppo 		break;
48511ae08745Sheppo 
48521ae08745Sheppo 	case VIO_SUBTYPE_NACK:
48531ae08745Sheppo 		/*
48541ae08745Sheppo 		 * vds could not handle the attributes we sent so we
48551ae08745Sheppo 		 * stop negotiating.
48561ae08745Sheppo 		 */
48571ae08745Sheppo 		status = EPROTO;
48581ae08745Sheppo 		break;
48591ae08745Sheppo 
48601ae08745Sheppo 	case VIO_SUBTYPE_INFO:
48611ae08745Sheppo 		/*
48621ae08745Sheppo 		 * Handle the case where vds starts the handshake
48631ae08745Sheppo 		 * (for now; vdc is the only supported instigatior)
48641ae08745Sheppo 		 */
48651ae08745Sheppo 		status = ENOTSUP;
48661ae08745Sheppo 		break;
48671ae08745Sheppo 
48681ae08745Sheppo 	default:
48691ae08745Sheppo 		status = ENOTSUP;
48701ae08745Sheppo 		break;
48711ae08745Sheppo 	}
48721ae08745Sheppo 
48730a55fbb7Slm66018 	return (status);
48741ae08745Sheppo }
48751ae08745Sheppo 
48760a55fbb7Slm66018 /*
48770a55fbb7Slm66018  * Function:
48780a55fbb7Slm66018  *	vdc_handle_dring_reg_msg()
48790a55fbb7Slm66018  *
48800a55fbb7Slm66018  * Description:
48810a55fbb7Slm66018  *
48820a55fbb7Slm66018  * Arguments:
48830a55fbb7Slm66018  *	vdc		- soft state pointer for this instance of the driver.
48840a55fbb7Slm66018  *	dring_msg	- LDC message sent by vDisk server
48850a55fbb7Slm66018  *
48860a55fbb7Slm66018  * Return Code:
48870a55fbb7Slm66018  *	0	- Success
48880a55fbb7Slm66018  */
48890a55fbb7Slm66018 static int
48900a55fbb7Slm66018 vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *dring_msg)
48910a55fbb7Slm66018 {
48920a55fbb7Slm66018 	int		status = 0;
48931ae08745Sheppo 
48940a55fbb7Slm66018 	ASSERT(vdc != NULL);
48950a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
48960a55fbb7Slm66018 
48970a55fbb7Slm66018 	if (dring_msg->tag.vio_subtype_env != VIO_DRING_REG) {
48980a55fbb7Slm66018 		return (EPROTO);
48990a55fbb7Slm66018 	}
49000a55fbb7Slm66018 
49010a55fbb7Slm66018 	switch (dring_msg->tag.vio_subtype) {
49020a55fbb7Slm66018 	case VIO_SUBTYPE_ACK:
49031ae08745Sheppo 		/* save the received dring_ident */
49041ae08745Sheppo 		vdc->dring_ident = dring_msg->dring_ident;
49053af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received dring ident=0x%lx\n",
4906e1ebb9ecSlm66018 		    vdc->instance, vdc->dring_ident);
49071ae08745Sheppo 		break;
49081ae08745Sheppo 
49091ae08745Sheppo 	case VIO_SUBTYPE_NACK:
49101ae08745Sheppo 		/*
49111ae08745Sheppo 		 * vds could not handle the DRing info we sent so we
49121ae08745Sheppo 		 * stop negotiating.
49131ae08745Sheppo 		 */
49143af08d82Slm66018 		DMSG(vdc, 0, "[%d] server could not register DRing\n",
49153af08d82Slm66018 		    vdc->instance);
49161ae08745Sheppo 		status = EPROTO;
49171ae08745Sheppo 		break;
49181ae08745Sheppo 
49191ae08745Sheppo 	case VIO_SUBTYPE_INFO:
49201ae08745Sheppo 		/*
49211ae08745Sheppo 		 * Handle the case where vds starts handshake
49221ae08745Sheppo 		 * (for now only vdc is the instigatior)
49231ae08745Sheppo 		 */
49241ae08745Sheppo 		status = ENOTSUP;
49251ae08745Sheppo 		break;
49261ae08745Sheppo 	default:
49271ae08745Sheppo 		status = ENOTSUP;
49281ae08745Sheppo 	}
49291ae08745Sheppo 
49301ae08745Sheppo 	return (status);
49311ae08745Sheppo }
49321ae08745Sheppo 
49331ae08745Sheppo /*
49341ae08745Sheppo  * Function:
49351ae08745Sheppo  *	vdc_verify_seq_num()
49361ae08745Sheppo  *
49371ae08745Sheppo  * Description:
4938e1ebb9ecSlm66018  *	This functions verifies that the sequence number sent back by the vDisk
4939e1ebb9ecSlm66018  *	server with the latest message is what is expected (i.e. it is greater
4940e1ebb9ecSlm66018  *	than the last seq num sent by the vDisk server and less than or equal
4941e1ebb9ecSlm66018  *	to the last seq num generated by vdc).
4942e1ebb9ecSlm66018  *
4943e1ebb9ecSlm66018  *	It then checks the request ID to see if any requests need processing
4944e1ebb9ecSlm66018  *	in the DRing.
49451ae08745Sheppo  *
49461ae08745Sheppo  * Arguments:
49471ae08745Sheppo  *	vdc		- soft state pointer for this instance of the driver.
49481ae08745Sheppo  *	dring_msg	- pointer to the LDC message sent by vds
49491ae08745Sheppo  *
49501ae08745Sheppo  * Return Code:
4951e1ebb9ecSlm66018  *	VDC_SEQ_NUM_TODO	- Message needs to be processed
4952e1ebb9ecSlm66018  *	VDC_SEQ_NUM_SKIP	- Message has already been processed
4953e1ebb9ecSlm66018  *	VDC_SEQ_NUM_INVALID	- The seq numbers are so out of sync,
4954e1ebb9ecSlm66018  *				  vdc cannot deal with them
49551ae08745Sheppo  */
4956e1ebb9ecSlm66018 static int
4957e1ebb9ecSlm66018 vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg)
49581ae08745Sheppo {
49591ae08745Sheppo 	ASSERT(vdc != NULL);
49601ae08745Sheppo 	ASSERT(dring_msg != NULL);
4961d10e4ef2Snarayan 	ASSERT(mutex_owned(&vdc->lock));
49621ae08745Sheppo 
49631ae08745Sheppo 	/*
49641ae08745Sheppo 	 * Check to see if the messages were responded to in the correct
4965e1ebb9ecSlm66018 	 * order by vds.
49661ae08745Sheppo 	 */
4967e1ebb9ecSlm66018 	if ((dring_msg->seq_num <= vdc->seq_num_reply) ||
4968e1ebb9ecSlm66018 	    (dring_msg->seq_num > vdc->seq_num)) {
49693af08d82Slm66018 		DMSG(vdc, 0, "?[%d] Bogus sequence_number %lu: "
4970e1ebb9ecSlm66018 		    "%lu > expected <= %lu (last proc req %lu sent %lu)\n",
4971e1ebb9ecSlm66018 		    vdc->instance, dring_msg->seq_num,
4972e1ebb9ecSlm66018 		    vdc->seq_num_reply, vdc->seq_num,
4973e1ebb9ecSlm66018 		    vdc->req_id_proc, vdc->req_id);
4974e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_INVALID);
49751ae08745Sheppo 	}
4976e1ebb9ecSlm66018 	vdc->seq_num_reply = dring_msg->seq_num;
49771ae08745Sheppo 
4978e1ebb9ecSlm66018 	if (vdc->req_id_proc < vdc->req_id)
4979e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_TODO);
4980e1ebb9ecSlm66018 	else
4981e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_SKIP);
49821ae08745Sheppo }
49831ae08745Sheppo 
49840a55fbb7Slm66018 
49850a55fbb7Slm66018 /*
49860a55fbb7Slm66018  * Function:
49870a55fbb7Slm66018  *	vdc_is_supported_version()
49880a55fbb7Slm66018  *
49890a55fbb7Slm66018  * Description:
49900a55fbb7Slm66018  *	This routine checks if the major/minor version numbers specified in
49910a55fbb7Slm66018  *	'ver_msg' are supported. If not it finds the next version that is
49920a55fbb7Slm66018  *	in the supported version list 'vdc_version[]' and sets the fields in
49930a55fbb7Slm66018  *	'ver_msg' to those values
49940a55fbb7Slm66018  *
49950a55fbb7Slm66018  * Arguments:
49960a55fbb7Slm66018  *	ver_msg	- LDC message sent by vDisk server
49970a55fbb7Slm66018  *
49980a55fbb7Slm66018  * Return Code:
49990a55fbb7Slm66018  *	B_TRUE	- Success
50000a55fbb7Slm66018  *	B_FALSE	- Version not supported
50010a55fbb7Slm66018  */
50020a55fbb7Slm66018 static boolean_t
50030a55fbb7Slm66018 vdc_is_supported_version(vio_ver_msg_t *ver_msg)
50040a55fbb7Slm66018 {
50050a55fbb7Slm66018 	int vdc_num_versions = sizeof (vdc_version) / sizeof (vdc_version[0]);
50060a55fbb7Slm66018 
50070a55fbb7Slm66018 	for (int i = 0; i < vdc_num_versions; i++) {
50080a55fbb7Slm66018 		ASSERT(vdc_version[i].major > 0);
50090a55fbb7Slm66018 		ASSERT((i == 0) ||
50100a55fbb7Slm66018 		    (vdc_version[i].major < vdc_version[i-1].major));
50110a55fbb7Slm66018 
50120a55fbb7Slm66018 		/*
50130a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
50140a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
50150a55fbb7Slm66018 		 * client. The server should support all minor versions lower
50160a55fbb7Slm66018 		 * than the value it sent
50170a55fbb7Slm66018 		 */
50180a55fbb7Slm66018 		if (ver_msg->ver_major == vdc_version[i].major) {
50190a55fbb7Slm66018 			if (ver_msg->ver_minor > vdc_version[i].minor) {
50203af08d82Slm66018 				DMSGX(0,
50213af08d82Slm66018 				    "Adjusting minor version from %u to %u",
50220a55fbb7Slm66018 				    ver_msg->ver_minor, vdc_version[i].minor);
50230a55fbb7Slm66018 				ver_msg->ver_minor = vdc_version[i].minor;
50240a55fbb7Slm66018 			}
50250a55fbb7Slm66018 			return (B_TRUE);
50260a55fbb7Slm66018 		}
50270a55fbb7Slm66018 
50280a55fbb7Slm66018 		/*
50290a55fbb7Slm66018 		 * If the message contains a higher major version number, set
50300a55fbb7Slm66018 		 * the message's major/minor versions to the current values
50310a55fbb7Slm66018 		 * and return false, so this message will get resent with
50320a55fbb7Slm66018 		 * these values, and the server will potentially try again
50330a55fbb7Slm66018 		 * with the same or a lower version
50340a55fbb7Slm66018 		 */
50350a55fbb7Slm66018 		if (ver_msg->ver_major > vdc_version[i].major) {
50360a55fbb7Slm66018 			ver_msg->ver_major = vdc_version[i].major;
50370a55fbb7Slm66018 			ver_msg->ver_minor = vdc_version[i].minor;
50383af08d82Slm66018 			DMSGX(0, "Suggesting major/minor (0x%x/0x%x)\n",
50390a55fbb7Slm66018 			    ver_msg->ver_major, ver_msg->ver_minor);
50400a55fbb7Slm66018 
50410a55fbb7Slm66018 			return (B_FALSE);
50420a55fbb7Slm66018 		}
50430a55fbb7Slm66018 
50440a55fbb7Slm66018 		/*
50450a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
50460a55fbb7Slm66018 		 * current major version, so continue the loop to the next
50470a55fbb7Slm66018 		 * (lower) supported version
50480a55fbb7Slm66018 		 */
50490a55fbb7Slm66018 	}
50500a55fbb7Slm66018 
50510a55fbb7Slm66018 	/*
50520a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
50530a55fbb7Slm66018 	 * message to terminate negotiation
50540a55fbb7Slm66018 	 */
50550a55fbb7Slm66018 	ver_msg->ver_major = 0;
50560a55fbb7Slm66018 	ver_msg->ver_minor = 0;
50570a55fbb7Slm66018 
50580a55fbb7Slm66018 	return (B_FALSE);
50590a55fbb7Slm66018 }
50601ae08745Sheppo /* -------------------------------------------------------------------------- */
50611ae08745Sheppo 
50621ae08745Sheppo /*
50631ae08745Sheppo  * DKIO(7) support
50641ae08745Sheppo  */
50651ae08745Sheppo 
50661ae08745Sheppo typedef struct vdc_dk_arg {
50671ae08745Sheppo 	struct dk_callback	dkc;
50681ae08745Sheppo 	int			mode;
50691ae08745Sheppo 	dev_t			dev;
50701ae08745Sheppo 	vdc_t			*vdc;
50711ae08745Sheppo } vdc_dk_arg_t;
50721ae08745Sheppo 
50731ae08745Sheppo /*
50741ae08745Sheppo  * Function:
50751ae08745Sheppo  * 	vdc_dkio_flush_cb()
50761ae08745Sheppo  *
50771ae08745Sheppo  * Description:
50781ae08745Sheppo  *	This routine is a callback for DKIOCFLUSHWRITECACHE which can be called
50791ae08745Sheppo  *	by kernel code.
50801ae08745Sheppo  *
50811ae08745Sheppo  * Arguments:
50821ae08745Sheppo  *	arg	- a pointer to a vdc_dk_arg_t structure.
50831ae08745Sheppo  */
50841ae08745Sheppo void
50851ae08745Sheppo vdc_dkio_flush_cb(void *arg)
50861ae08745Sheppo {
50871ae08745Sheppo 	struct vdc_dk_arg	*dk_arg = (struct vdc_dk_arg *)arg;
50881ae08745Sheppo 	struct dk_callback	*dkc = NULL;
50891ae08745Sheppo 	vdc_t			*vdc = NULL;
50901ae08745Sheppo 	int			rv;
50911ae08745Sheppo 
50921ae08745Sheppo 	if (dk_arg == NULL) {
50933af08d82Slm66018 		cmn_err(CE_NOTE, "?[Unk] DKIOCFLUSHWRITECACHE arg is NULL\n");
50941ae08745Sheppo 		return;
50951ae08745Sheppo 	}
50961ae08745Sheppo 	dkc = &dk_arg->dkc;
50971ae08745Sheppo 	vdc = dk_arg->vdc;
50981ae08745Sheppo 	ASSERT(vdc != NULL);
50991ae08745Sheppo 
51003af08d82Slm66018 	rv = vdc_do_sync_op(vdc, VD_OP_FLUSH, NULL, 0,
51012f5224aeSachartre 	    VDCPART(dk_arg->dev), 0, CB_SYNC, 0, VIO_both_dir, B_TRUE);
51021ae08745Sheppo 	if (rv != 0) {
51033af08d82Slm66018 		DMSG(vdc, 0, "[%d] DKIOCFLUSHWRITECACHE failed %d : model %x\n",
5104e1ebb9ecSlm66018 		    vdc->instance, rv,
51051ae08745Sheppo 		    ddi_model_convert_from(dk_arg->mode & FMODELS));
51061ae08745Sheppo 	}
51071ae08745Sheppo 
51081ae08745Sheppo 	/*
51091ae08745Sheppo 	 * Trigger the call back to notify the caller the the ioctl call has
51101ae08745Sheppo 	 * been completed.
51111ae08745Sheppo 	 */
51121ae08745Sheppo 	if ((dk_arg->mode & FKIOCTL) &&
51131ae08745Sheppo 	    (dkc != NULL) &&
51141ae08745Sheppo 	    (dkc->dkc_callback != NULL)) {
51151ae08745Sheppo 		ASSERT(dkc->dkc_cookie != NULL);
51168e6a2a04Slm66018 		(*dkc->dkc_callback)(dkc->dkc_cookie, rv);
51171ae08745Sheppo 	}
51181ae08745Sheppo 
51191ae08745Sheppo 	/* Indicate that one less DKIO write flush is outstanding */
51201ae08745Sheppo 	mutex_enter(&vdc->lock);
51211ae08745Sheppo 	vdc->dkio_flush_pending--;
51221ae08745Sheppo 	ASSERT(vdc->dkio_flush_pending >= 0);
51231ae08745Sheppo 	mutex_exit(&vdc->lock);
51248e6a2a04Slm66018 
51258e6a2a04Slm66018 	/* free the mem that was allocated when the callback was dispatched */
51268e6a2a04Slm66018 	kmem_free(arg, sizeof (vdc_dk_arg_t));
51271ae08745Sheppo }
51281ae08745Sheppo 
51291ae08745Sheppo /*
513087a7269eSachartre  * Function:
51319642afceSachartre  * 	vdc_dkio_gapart()
513287a7269eSachartre  *
513387a7269eSachartre  * Description:
513487a7269eSachartre  *	This function implements the DKIOCGAPART ioctl.
513587a7269eSachartre  *
513687a7269eSachartre  * Arguments:
513778fcd0a1Sachartre  *	vdc	- soft state pointer
513887a7269eSachartre  *	arg	- a pointer to a dk_map[NDKMAP] or dk_map32[NDKMAP] structure
513987a7269eSachartre  *	flag	- ioctl flags
514087a7269eSachartre  */
514187a7269eSachartre static int
51429642afceSachartre vdc_dkio_gapart(vdc_t *vdc, caddr_t arg, int flag)
514387a7269eSachartre {
514478fcd0a1Sachartre 	struct dk_geom *geom;
514578fcd0a1Sachartre 	struct vtoc *vtoc;
514687a7269eSachartre 	union {
514787a7269eSachartre 		struct dk_map map[NDKMAP];
514887a7269eSachartre 		struct dk_map32 map32[NDKMAP];
514987a7269eSachartre 	} data;
515087a7269eSachartre 	int i, rv, size;
515187a7269eSachartre 
515278fcd0a1Sachartre 	mutex_enter(&vdc->lock);
515387a7269eSachartre 
515478fcd0a1Sachartre 	if ((rv = vdc_validate_geometry(vdc)) != 0) {
515578fcd0a1Sachartre 		mutex_exit(&vdc->lock);
515687a7269eSachartre 		return (rv);
515778fcd0a1Sachartre 	}
515887a7269eSachartre 
515978fcd0a1Sachartre 	vtoc = vdc->vtoc;
516078fcd0a1Sachartre 	geom = vdc->geom;
516187a7269eSachartre 
516287a7269eSachartre 	if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) {
516387a7269eSachartre 
516478fcd0a1Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
516578fcd0a1Sachartre 			data.map32[i].dkl_cylno = vtoc->v_part[i].p_start /
516678fcd0a1Sachartre 			    (geom->dkg_nhead * geom->dkg_nsect);
516778fcd0a1Sachartre 			data.map32[i].dkl_nblk = vtoc->v_part[i].p_size;
516887a7269eSachartre 		}
516987a7269eSachartre 		size = NDKMAP * sizeof (struct dk_map32);
517087a7269eSachartre 
517187a7269eSachartre 	} else {
517287a7269eSachartre 
517378fcd0a1Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
517478fcd0a1Sachartre 			data.map[i].dkl_cylno = vtoc->v_part[i].p_start /
517578fcd0a1Sachartre 			    (geom->dkg_nhead * geom->dkg_nsect);
517678fcd0a1Sachartre 			data.map[i].dkl_nblk = vtoc->v_part[i].p_size;
517787a7269eSachartre 		}
517887a7269eSachartre 		size = NDKMAP * sizeof (struct dk_map);
517987a7269eSachartre 
518087a7269eSachartre 	}
518187a7269eSachartre 
518278fcd0a1Sachartre 	mutex_exit(&vdc->lock);
518378fcd0a1Sachartre 
518487a7269eSachartre 	if (ddi_copyout(&data, arg, size, flag) != 0)
518587a7269eSachartre 		return (EFAULT);
518687a7269eSachartre 
518787a7269eSachartre 	return (0);
518887a7269eSachartre }
518987a7269eSachartre 
519087a7269eSachartre /*
519187a7269eSachartre  * Function:
51929642afceSachartre  * 	vdc_dkio_partition()
51939642afceSachartre  *
51949642afceSachartre  * Description:
51959642afceSachartre  *	This function implements the DKIOCPARTITION ioctl.
51969642afceSachartre  *
51979642afceSachartre  * Arguments:
51989642afceSachartre  *	vdc	- soft state pointer
51999642afceSachartre  *	arg	- a pointer to a struct partition64 structure
52009642afceSachartre  *	flag	- ioctl flags
52019642afceSachartre  */
52029642afceSachartre static int
52039642afceSachartre vdc_dkio_partition(vdc_t *vdc, caddr_t arg, int flag)
52049642afceSachartre {
52059642afceSachartre 	struct partition64 p64;
52069642afceSachartre 	efi_gpt_t *gpt;
52079642afceSachartre 	efi_gpe_t *gpe;
52089642afceSachartre 	vd_efi_dev_t edev;
52099642afceSachartre 	uint_t partno;
52109642afceSachartre 	int rv;
52119642afceSachartre 
52129642afceSachartre 	if (ddi_copyin(arg, &p64, sizeof (struct partition64), flag)) {
52139642afceSachartre 		return (EFAULT);
52149642afceSachartre 	}
52159642afceSachartre 
52169642afceSachartre 	VD_EFI_DEV_SET(edev, vdc, vd_process_efi_ioctl);
52179642afceSachartre 
52189642afceSachartre 	if ((rv = vd_efi_alloc_and_read(&edev, &gpt, &gpe)) != 0) {
52199642afceSachartre 		return (rv);
52209642afceSachartre 	}
52219642afceSachartre 
52229642afceSachartre 	partno = p64.p_partno;
52239642afceSachartre 
52249642afceSachartre 	if (partno >= gpt->efi_gpt_NumberOfPartitionEntries) {
52259642afceSachartre 		vd_efi_free(&edev, gpt, gpe);
52269642afceSachartre 		return (ESRCH);
52279642afceSachartre 	}
52289642afceSachartre 
52299642afceSachartre 	bcopy(&gpe[partno].efi_gpe_PartitionTypeGUID, &p64.p_type,
52309642afceSachartre 	    sizeof (struct uuid));
52319642afceSachartre 	p64.p_start = gpe[partno].efi_gpe_StartingLBA;
52329642afceSachartre 	p64.p_size = gpe[partno].efi_gpe_EndingLBA - p64.p_start + 1;
52339642afceSachartre 
52349642afceSachartre 	if (ddi_copyout(&p64, arg, sizeof (struct partition64), flag)) {
52359642afceSachartre 		vd_efi_free(&edev, gpt, gpe);
52369642afceSachartre 		return (EFAULT);
52379642afceSachartre 	}
52389642afceSachartre 
52399642afceSachartre 	vd_efi_free(&edev, gpt, gpe);
52409642afceSachartre 	return (0);
52419642afceSachartre }
52429642afceSachartre 
52439642afceSachartre /*
52449642afceSachartre  * Function:
524587a7269eSachartre  * 	vdc_dioctl_rwcmd()
524687a7269eSachartre  *
524787a7269eSachartre  * Description:
524887a7269eSachartre  *	This function implements the DIOCTL_RWCMD ioctl. This ioctl is used
524987a7269eSachartre  *	for DKC_DIRECT disks to read or write at an absolute disk offset.
525087a7269eSachartre  *
525187a7269eSachartre  * Arguments:
525287a7269eSachartre  *	dev	- device
525387a7269eSachartre  *	arg	- a pointer to a dadkio_rwcmd or dadkio_rwcmd32 structure
525487a7269eSachartre  *	flag	- ioctl flags
525587a7269eSachartre  */
525687a7269eSachartre static int
525787a7269eSachartre vdc_dioctl_rwcmd(dev_t dev, caddr_t arg, int flag)
525887a7269eSachartre {
525987a7269eSachartre 	struct dadkio_rwcmd32 rwcmd32;
526087a7269eSachartre 	struct dadkio_rwcmd rwcmd;
526187a7269eSachartre 	struct iovec aiov;
526287a7269eSachartre 	struct uio auio;
526387a7269eSachartre 	int rw, status;
526487a7269eSachartre 	struct buf *buf;
526587a7269eSachartre 
526687a7269eSachartre 	if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) {
526787a7269eSachartre 		if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd32,
526887a7269eSachartre 		    sizeof (struct dadkio_rwcmd32), flag)) {
526987a7269eSachartre 			return (EFAULT);
527087a7269eSachartre 		}
527187a7269eSachartre 		rwcmd.cmd = rwcmd32.cmd;
527287a7269eSachartre 		rwcmd.flags = rwcmd32.flags;
527387a7269eSachartre 		rwcmd.blkaddr = (daddr_t)rwcmd32.blkaddr;
527487a7269eSachartre 		rwcmd.buflen = rwcmd32.buflen;
527587a7269eSachartre 		rwcmd.bufaddr = (caddr_t)(uintptr_t)rwcmd32.bufaddr;
527687a7269eSachartre 	} else {
527787a7269eSachartre 		if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd,
527887a7269eSachartre 		    sizeof (struct dadkio_rwcmd), flag)) {
527987a7269eSachartre 			return (EFAULT);
528087a7269eSachartre 		}
528187a7269eSachartre 	}
528287a7269eSachartre 
528387a7269eSachartre 	switch (rwcmd.cmd) {
528487a7269eSachartre 	case DADKIO_RWCMD_READ:
528587a7269eSachartre 		rw = B_READ;
528687a7269eSachartre 		break;
528787a7269eSachartre 	case DADKIO_RWCMD_WRITE:
528887a7269eSachartre 		rw = B_WRITE;
528987a7269eSachartre 		break;
529087a7269eSachartre 	default:
529187a7269eSachartre 		return (EINVAL);
529287a7269eSachartre 	}
529387a7269eSachartre 
529487a7269eSachartre 	bzero((caddr_t)&aiov, sizeof (struct iovec));
529587a7269eSachartre 	aiov.iov_base   = rwcmd.bufaddr;
529687a7269eSachartre 	aiov.iov_len    = rwcmd.buflen;
529787a7269eSachartre 
529887a7269eSachartre 	bzero((caddr_t)&auio, sizeof (struct uio));
529987a7269eSachartre 	auio.uio_iov    = &aiov;
530087a7269eSachartre 	auio.uio_iovcnt = 1;
530187a7269eSachartre 	auio.uio_loffset = rwcmd.blkaddr * DEV_BSIZE;
530287a7269eSachartre 	auio.uio_resid  = rwcmd.buflen;
530387a7269eSachartre 	auio.uio_segflg = flag & FKIOCTL ? UIO_SYSSPACE : UIO_USERSPACE;
530487a7269eSachartre 
530587a7269eSachartre 	buf = kmem_alloc(sizeof (buf_t), KM_SLEEP);
530687a7269eSachartre 	bioinit(buf);
530787a7269eSachartre 	/*
530887a7269eSachartre 	 * We use the private field of buf to specify that this is an
530987a7269eSachartre 	 * I/O using an absolute offset.
531087a7269eSachartre 	 */
531187a7269eSachartre 	buf->b_private = (void *)VD_SLICE_NONE;
531287a7269eSachartre 
531387a7269eSachartre 	status = physio(vdc_strategy, buf, dev, rw, vdc_min, &auio);
531487a7269eSachartre 
531587a7269eSachartre 	biofini(buf);
531687a7269eSachartre 	kmem_free(buf, sizeof (buf_t));
531787a7269eSachartre 
531887a7269eSachartre 	return (status);
531987a7269eSachartre }
532087a7269eSachartre 
532187a7269eSachartre /*
53222f5224aeSachartre  * Allocate a buffer for a VD_OP_SCSICMD operation. The size of the allocated
53232f5224aeSachartre  * buffer is returned in alloc_len.
53242f5224aeSachartre  */
53252f5224aeSachartre static vd_scsi_t *
53262f5224aeSachartre vdc_scsi_alloc(int cdb_len, int sense_len, int datain_len, int dataout_len,
53272f5224aeSachartre     int *alloc_len)
53282f5224aeSachartre {
53292f5224aeSachartre 	vd_scsi_t *vd_scsi;
53302f5224aeSachartre 	int vd_scsi_len = VD_SCSI_SIZE;
53312f5224aeSachartre 
53322f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(cdb_len, sizeof (uint64_t));
53332f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(sense_len, sizeof (uint64_t));
53342f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(datain_len, sizeof (uint64_t));
53352f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(dataout_len, sizeof (uint64_t));
53362f5224aeSachartre 
53372f5224aeSachartre 	ASSERT(vd_scsi_len % sizeof (uint64_t) == 0);
53382f5224aeSachartre 
53392f5224aeSachartre 	vd_scsi = kmem_zalloc(vd_scsi_len, KM_SLEEP);
53402f5224aeSachartre 
53412f5224aeSachartre 	vd_scsi->cdb_len = cdb_len;
53422f5224aeSachartre 	vd_scsi->sense_len = sense_len;
53432f5224aeSachartre 	vd_scsi->datain_len = datain_len;
53442f5224aeSachartre 	vd_scsi->dataout_len = dataout_len;
53452f5224aeSachartre 
53462f5224aeSachartre 	*alloc_len = vd_scsi_len;
53472f5224aeSachartre 
53482f5224aeSachartre 	return (vd_scsi);
53492f5224aeSachartre }
53502f5224aeSachartre 
53512f5224aeSachartre /*
53522f5224aeSachartre  * Convert the status of a SCSI command to a Solaris return code.
53532f5224aeSachartre  *
53542f5224aeSachartre  * Arguments:
53552f5224aeSachartre  *	vd_scsi		- The SCSI operation buffer.
53562f5224aeSachartre  *	log_error	- indicate if an error message should be logged.
53572f5224aeSachartre  *
53582f5224aeSachartre  * Note that our SCSI error messages are rather primitive for the moment
53592f5224aeSachartre  * and could be improved by decoding some data like the SCSI command and
53602f5224aeSachartre  * the sense key.
53612f5224aeSachartre  *
53622f5224aeSachartre  * Return value:
53632f5224aeSachartre  *	0		- Status is good.
53642f5224aeSachartre  *	EACCES		- Status reports a reservation conflict.
53652f5224aeSachartre  *	ENOTSUP		- Status reports a check condition and sense key
53662f5224aeSachartre  *			  reports an illegal request.
53672f5224aeSachartre  *	EIO		- Any other status.
53682f5224aeSachartre  */
53692f5224aeSachartre static int
53702f5224aeSachartre vdc_scsi_status(vdc_t *vdc, vd_scsi_t *vd_scsi, boolean_t log_error)
53712f5224aeSachartre {
53722f5224aeSachartre 	int rv;
53732f5224aeSachartre 	char path_str[MAXPATHLEN];
53742f5224aeSachartre 	char panic_str[VDC_RESV_CONFLICT_FMT_LEN + MAXPATHLEN];
53752f5224aeSachartre 	union scsi_cdb *cdb;
53762f5224aeSachartre 	struct scsi_extended_sense *sense;
53772f5224aeSachartre 
53782f5224aeSachartre 	if (vd_scsi->cmd_status == STATUS_GOOD)
53792f5224aeSachartre 		/* no error */
53802f5224aeSachartre 		return (0);
53812f5224aeSachartre 
53822f5224aeSachartre 	/* when the tunable vdc_scsi_log_error is true we log all errors */
53832f5224aeSachartre 	if (vdc_scsi_log_error)
53842f5224aeSachartre 		log_error = B_TRUE;
53852f5224aeSachartre 
53862f5224aeSachartre 	if (log_error) {
53872f5224aeSachartre 		cmn_err(CE_WARN, "%s (vdc%d):\tError for Command: 0x%x)\n",
53882f5224aeSachartre 		    ddi_pathname(vdc->dip, path_str), vdc->instance,
53892f5224aeSachartre 		    GETCMD(VD_SCSI_DATA_CDB(vd_scsi)));
53902f5224aeSachartre 	}
53912f5224aeSachartre 
53922f5224aeSachartre 	/* default returned value */
53932f5224aeSachartre 	rv = EIO;
53942f5224aeSachartre 
53952f5224aeSachartre 	switch (vd_scsi->cmd_status) {
53962f5224aeSachartre 
53972f5224aeSachartre 	case STATUS_CHECK:
53982f5224aeSachartre 	case STATUS_TERMINATED:
53992f5224aeSachartre 		if (log_error)
54002f5224aeSachartre 			cmn_err(CE_CONT, "\tCheck Condition Error\n");
54012f5224aeSachartre 
54022f5224aeSachartre 		/* check sense buffer */
54032f5224aeSachartre 		if (vd_scsi->sense_len == 0 ||
54042f5224aeSachartre 		    vd_scsi->sense_status != STATUS_GOOD) {
54052f5224aeSachartre 			if (log_error)
54062f5224aeSachartre 				cmn_err(CE_CONT, "\tNo Sense Data Available\n");
54072f5224aeSachartre 			break;
54082f5224aeSachartre 		}
54092f5224aeSachartre 
54102f5224aeSachartre 		sense = VD_SCSI_DATA_SENSE(vd_scsi);
54112f5224aeSachartre 
54122f5224aeSachartre 		if (log_error) {
54132f5224aeSachartre 			cmn_err(CE_CONT, "\tSense Key:  0x%x\n"
54142f5224aeSachartre 			    "\tASC: 0x%x, ASCQ: 0x%x\n",
54152f5224aeSachartre 			    scsi_sense_key((uint8_t *)sense),
54162f5224aeSachartre 			    scsi_sense_asc((uint8_t *)sense),
54172f5224aeSachartre 			    scsi_sense_ascq((uint8_t *)sense));
54182f5224aeSachartre 		}
54192f5224aeSachartre 
54202f5224aeSachartre 		if (scsi_sense_key((uint8_t *)sense) == KEY_ILLEGAL_REQUEST)
54212f5224aeSachartre 			rv = ENOTSUP;
54222f5224aeSachartre 		break;
54232f5224aeSachartre 
54242f5224aeSachartre 	case STATUS_BUSY:
54252f5224aeSachartre 		if (log_error)
54262f5224aeSachartre 			cmn_err(CE_NOTE, "\tDevice Busy\n");
54272f5224aeSachartre 		break;
54282f5224aeSachartre 
54292f5224aeSachartre 	case STATUS_RESERVATION_CONFLICT:
54302f5224aeSachartre 		/*
54312f5224aeSachartre 		 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then
54322f5224aeSachartre 		 * reservation conflict could be due to various reasons like
54332f5224aeSachartre 		 * incorrect keys, not registered or not reserved etc. So,
54342f5224aeSachartre 		 * we should not panic in that case.
54352f5224aeSachartre 		 */
54362f5224aeSachartre 		cdb = VD_SCSI_DATA_CDB(vd_scsi);
54372f5224aeSachartre 		if (vdc->failfast_interval != 0 &&
54382f5224aeSachartre 		    cdb->scc_cmd != SCMD_PERSISTENT_RESERVE_IN &&
54392f5224aeSachartre 		    cdb->scc_cmd != SCMD_PERSISTENT_RESERVE_OUT) {
54402f5224aeSachartre 			/* failfast is enabled so we have to panic */
54412f5224aeSachartre 			(void) snprintf(panic_str, sizeof (panic_str),
54422f5224aeSachartre 			    VDC_RESV_CONFLICT_FMT_STR "%s",
54432f5224aeSachartre 			    ddi_pathname(vdc->dip, path_str));
54442f5224aeSachartre 			panic(panic_str);
54452f5224aeSachartre 		}
54462f5224aeSachartre 		if (log_error)
54472f5224aeSachartre 			cmn_err(CE_NOTE, "\tReservation Conflict\n");
54482f5224aeSachartre 		rv = EACCES;
54492f5224aeSachartre 		break;
54502f5224aeSachartre 
54512f5224aeSachartre 	case STATUS_QFULL:
54522f5224aeSachartre 		if (log_error)
54532f5224aeSachartre 			cmn_err(CE_NOTE, "\tQueue Full\n");
54542f5224aeSachartre 		break;
54552f5224aeSachartre 
54562f5224aeSachartre 	case STATUS_MET:
54572f5224aeSachartre 	case STATUS_INTERMEDIATE:
54582f5224aeSachartre 	case STATUS_SCSI2:
54592f5224aeSachartre 	case STATUS_INTERMEDIATE_MET:
54602f5224aeSachartre 	case STATUS_ACA_ACTIVE:
54612f5224aeSachartre 		if (log_error)
54622f5224aeSachartre 			cmn_err(CE_CONT,
54632f5224aeSachartre 			    "\tUnexpected SCSI status received: 0x%x\n",
54642f5224aeSachartre 			    vd_scsi->cmd_status);
54652f5224aeSachartre 		break;
54662f5224aeSachartre 
54672f5224aeSachartre 	default:
54682f5224aeSachartre 		if (log_error)
54692f5224aeSachartre 			cmn_err(CE_CONT,
54702f5224aeSachartre 			    "\tInvalid SCSI status received: 0x%x\n",
54712f5224aeSachartre 			    vd_scsi->cmd_status);
54722f5224aeSachartre 		break;
54732f5224aeSachartre 	}
54742f5224aeSachartre 
54752f5224aeSachartre 	return (rv);
54762f5224aeSachartre }
54772f5224aeSachartre 
54782f5224aeSachartre /*
54792f5224aeSachartre  * Implemented the USCSICMD uscsi(7I) ioctl. This ioctl is converted to
54802f5224aeSachartre  * a VD_OP_SCSICMD operation which is sent to the vdisk server. If a SCSI
54812f5224aeSachartre  * reset is requested (i.e. a flag USCSI_RESET* is set) then the ioctl is
54822f5224aeSachartre  * converted to a VD_OP_RESET operation.
54832f5224aeSachartre  */
54842f5224aeSachartre static int
54852f5224aeSachartre vdc_uscsi_cmd(vdc_t *vdc, caddr_t arg, int mode)
54862f5224aeSachartre {
54872f5224aeSachartre 	struct uscsi_cmd 	uscsi;
54882f5224aeSachartre 	struct uscsi_cmd32	uscsi32;
54892f5224aeSachartre 	vd_scsi_t 		*vd_scsi;
54902f5224aeSachartre 	int 			vd_scsi_len;
54912f5224aeSachartre 	union scsi_cdb		*cdb;
54922f5224aeSachartre 	struct scsi_extended_sense *sense;
54932f5224aeSachartre 	char 			*datain, *dataout;
54942f5224aeSachartre 	size_t			cdb_len, datain_len, dataout_len, sense_len;
54952f5224aeSachartre 	int 			rv;
54962f5224aeSachartre 
54972f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
54982f5224aeSachartre 		if (ddi_copyin(arg, &uscsi32, sizeof (struct uscsi_cmd32),
54992f5224aeSachartre 		    mode) != 0)
55002f5224aeSachartre 			return (EFAULT);
55012f5224aeSachartre 		uscsi_cmd32touscsi_cmd((&uscsi32), (&uscsi));
55022f5224aeSachartre 	} else {
55032f5224aeSachartre 		if (ddi_copyin(arg, &uscsi, sizeof (struct uscsi_cmd),
55042f5224aeSachartre 		    mode) != 0)
55052f5224aeSachartre 			return (EFAULT);
55062f5224aeSachartre 	}
55072f5224aeSachartre 
55082f5224aeSachartre 	/* a uscsi reset is converted to a VD_OP_RESET operation */
55092f5224aeSachartre 	if (uscsi.uscsi_flags & (USCSI_RESET | USCSI_RESET_LUN |
55102f5224aeSachartre 	    USCSI_RESET_ALL)) {
55112f5224aeSachartre 		rv = vdc_do_sync_op(vdc, VD_OP_RESET, NULL, 0, 0, 0, CB_SYNC,
55122f5224aeSachartre 		    (void *)(uint64_t)mode, VIO_both_dir, B_TRUE);
55132f5224aeSachartre 		return (rv);
55142f5224aeSachartre 	}
55152f5224aeSachartre 
55162f5224aeSachartre 	/* cdb buffer length */
55172f5224aeSachartre 	cdb_len = uscsi.uscsi_cdblen;
55182f5224aeSachartre 
55192f5224aeSachartre 	/* data in and out buffers length */
55202f5224aeSachartre 	if (uscsi.uscsi_flags & USCSI_READ) {
55212f5224aeSachartre 		datain_len = uscsi.uscsi_buflen;
55222f5224aeSachartre 		dataout_len = 0;
55232f5224aeSachartre 	} else {
55242f5224aeSachartre 		datain_len = 0;
55252f5224aeSachartre 		dataout_len = uscsi.uscsi_buflen;
55262f5224aeSachartre 	}
55272f5224aeSachartre 
55282f5224aeSachartre 	/* sense buffer length */
55292f5224aeSachartre 	if (uscsi.uscsi_flags & USCSI_RQENABLE)
55302f5224aeSachartre 		sense_len = uscsi.uscsi_rqlen;
55312f5224aeSachartre 	else
55322f5224aeSachartre 		sense_len = 0;
55332f5224aeSachartre 
55342f5224aeSachartre 	/* allocate buffer for the VD_SCSICMD_OP operation */
55352f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
55362f5224aeSachartre 	    &vd_scsi_len);
55372f5224aeSachartre 
55382f5224aeSachartre 	/*
55392f5224aeSachartre 	 * The documentation of USCSI_ISOLATE and USCSI_DIAGNOSE is very vague,
55402f5224aeSachartre 	 * but basically they prevent a SCSI command from being retried in case
55412f5224aeSachartre 	 * of an error.
55422f5224aeSachartre 	 */
55432f5224aeSachartre 	if ((uscsi.uscsi_flags & USCSI_ISOLATE) ||
55442f5224aeSachartre 	    (uscsi.uscsi_flags & USCSI_DIAGNOSE))
55452f5224aeSachartre 		vd_scsi->options |= VD_SCSI_OPT_NORETRY;
55462f5224aeSachartre 
55472f5224aeSachartre 	/* set task attribute */
55482f5224aeSachartre 	if (uscsi.uscsi_flags & USCSI_NOTAG) {
55492f5224aeSachartre 		vd_scsi->task_attribute = 0;
55502f5224aeSachartre 	} else {
55512f5224aeSachartre 		if (uscsi.uscsi_flags & USCSI_HEAD)
55522f5224aeSachartre 			vd_scsi->task_attribute = VD_SCSI_TASK_ACA;
55532f5224aeSachartre 		else if (uscsi.uscsi_flags & USCSI_HTAG)
55542f5224aeSachartre 			vd_scsi->task_attribute = VD_SCSI_TASK_HQUEUE;
55552f5224aeSachartre 		else if (uscsi.uscsi_flags & USCSI_OTAG)
55562f5224aeSachartre 			vd_scsi->task_attribute = VD_SCSI_TASK_ORDERED;
55572f5224aeSachartre 		else
55582f5224aeSachartre 			vd_scsi->task_attribute = 0;
55592f5224aeSachartre 	}
55602f5224aeSachartre 
55612f5224aeSachartre 	/* set timeout */
55622f5224aeSachartre 	vd_scsi->timeout = uscsi.uscsi_timeout;
55632f5224aeSachartre 
55642f5224aeSachartre 	/* copy-in cdb data */
55652f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
55662f5224aeSachartre 	if (ddi_copyin(uscsi.uscsi_cdb, cdb, cdb_len, mode) != 0) {
55672f5224aeSachartre 		rv = EFAULT;
55682f5224aeSachartre 		goto done;
55692f5224aeSachartre 	}
55702f5224aeSachartre 
55712f5224aeSachartre 	/* keep a pointer to the sense buffer */
55722f5224aeSachartre 	sense = VD_SCSI_DATA_SENSE(vd_scsi);
55732f5224aeSachartre 
55742f5224aeSachartre 	/* keep a pointer to the data-in buffer */
55752f5224aeSachartre 	datain = (char *)VD_SCSI_DATA_IN(vd_scsi);
55762f5224aeSachartre 
55772f5224aeSachartre 	/* copy-in request data to the data-out buffer */
55782f5224aeSachartre 	dataout = (char *)VD_SCSI_DATA_OUT(vd_scsi);
55792f5224aeSachartre 	if (!(uscsi.uscsi_flags & USCSI_READ)) {
55802f5224aeSachartre 		if (ddi_copyin(uscsi.uscsi_bufaddr, dataout, dataout_len,
55812f5224aeSachartre 		    mode)) {
55822f5224aeSachartre 			rv = EFAULT;
55832f5224aeSachartre 			goto done;
55842f5224aeSachartre 		}
55852f5224aeSachartre 	}
55862f5224aeSachartre 
55872f5224aeSachartre 	/* submit the request */
55882f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
55892f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
55902f5224aeSachartre 
55912f5224aeSachartre 	if (rv != 0)
55922f5224aeSachartre 		goto done;
55932f5224aeSachartre 
55942f5224aeSachartre 	/* update scsi status */
55952f5224aeSachartre 	uscsi.uscsi_status = vd_scsi->cmd_status;
55962f5224aeSachartre 
55972f5224aeSachartre 	/* update sense data */
55982f5224aeSachartre 	if ((uscsi.uscsi_flags & USCSI_RQENABLE) &&
55992f5224aeSachartre 	    (uscsi.uscsi_status == STATUS_CHECK ||
56002f5224aeSachartre 	    uscsi.uscsi_status == STATUS_TERMINATED)) {
56012f5224aeSachartre 
56022f5224aeSachartre 		uscsi.uscsi_rqstatus = vd_scsi->sense_status;
56032f5224aeSachartre 
56042f5224aeSachartre 		if (uscsi.uscsi_rqstatus == STATUS_GOOD) {
56052f5224aeSachartre 			uscsi.uscsi_rqresid = uscsi.uscsi_rqlen -
56062f5224aeSachartre 			    vd_scsi->sense_len;
56072f5224aeSachartre 			if (ddi_copyout(sense, uscsi.uscsi_rqbuf,
56082f5224aeSachartre 			    vd_scsi->sense_len, mode) != 0) {
56092f5224aeSachartre 				rv = EFAULT;
56102f5224aeSachartre 				goto done;
56112f5224aeSachartre 			}
56122f5224aeSachartre 		}
56132f5224aeSachartre 	}
56142f5224aeSachartre 
56152f5224aeSachartre 	/* update request data */
56162f5224aeSachartre 	if (uscsi.uscsi_status == STATUS_GOOD) {
56172f5224aeSachartre 		if (uscsi.uscsi_flags & USCSI_READ) {
56182f5224aeSachartre 			uscsi.uscsi_resid = uscsi.uscsi_buflen -
56192f5224aeSachartre 			    vd_scsi->datain_len;
56202f5224aeSachartre 			if (ddi_copyout(datain, uscsi.uscsi_bufaddr,
56212f5224aeSachartre 			    vd_scsi->datain_len, mode) != 0) {
56222f5224aeSachartre 				rv = EFAULT;
56232f5224aeSachartre 				goto done;
56242f5224aeSachartre 			}
56252f5224aeSachartre 		} else {
56262f5224aeSachartre 			uscsi.uscsi_resid = uscsi.uscsi_buflen -
56272f5224aeSachartre 			    vd_scsi->dataout_len;
56282f5224aeSachartre 		}
56292f5224aeSachartre 	}
56302f5224aeSachartre 
56312f5224aeSachartre 	/* copy-out result */
56322f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
56332f5224aeSachartre 		uscsi_cmdtouscsi_cmd32((&uscsi), (&uscsi32));
56342f5224aeSachartre 		if (ddi_copyout(&uscsi32, arg, sizeof (struct uscsi_cmd32),
56352f5224aeSachartre 		    mode) != 0) {
56362f5224aeSachartre 			rv = EFAULT;
56372f5224aeSachartre 			goto done;
56382f5224aeSachartre 		}
56392f5224aeSachartre 	} else {
56402f5224aeSachartre 		if (ddi_copyout(&uscsi, arg, sizeof (struct uscsi_cmd),
56412f5224aeSachartre 		    mode) != 0) {
56422f5224aeSachartre 			rv = EFAULT;
56432f5224aeSachartre 			goto done;
56442f5224aeSachartre 		}
56452f5224aeSachartre 	}
56462f5224aeSachartre 
56472f5224aeSachartre 	/* get the return code from the SCSI command status */
56482f5224aeSachartre 	rv = vdc_scsi_status(vdc, vd_scsi,
56492f5224aeSachartre 	    !(uscsi.uscsi_flags & USCSI_SILENT));
56502f5224aeSachartre 
56512f5224aeSachartre done:
56522f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
56532f5224aeSachartre 	return (rv);
56542f5224aeSachartre }
56552f5224aeSachartre 
56562f5224aeSachartre /*
56572f5224aeSachartre  * Create a VD_OP_SCSICMD buffer for a SCSI PERSISTENT IN command.
56582f5224aeSachartre  *
56592f5224aeSachartre  * Arguments:
56602f5224aeSachartre  *	cmd		- SCSI PERSISTENT IN command
56612f5224aeSachartre  *	len		- length of the SCSI input buffer
56622f5224aeSachartre  *	vd_scsi_len	- return the length of the allocated buffer
56632f5224aeSachartre  *
56642f5224aeSachartre  * Returned Value:
56652f5224aeSachartre  *	a pointer to the allocated VD_OP_SCSICMD buffer.
56662f5224aeSachartre  */
56672f5224aeSachartre static vd_scsi_t *
56682f5224aeSachartre vdc_scsi_alloc_persistent_in(uchar_t cmd, int len, int *vd_scsi_len)
56692f5224aeSachartre {
56702f5224aeSachartre 	int cdb_len, sense_len, datain_len, dataout_len;
56712f5224aeSachartre 	vd_scsi_t *vd_scsi;
56722f5224aeSachartre 	union scsi_cdb *cdb;
56732f5224aeSachartre 
56742f5224aeSachartre 	cdb_len = CDB_GROUP1;
56752f5224aeSachartre 	sense_len = sizeof (struct scsi_extended_sense);
56762f5224aeSachartre 	datain_len = len;
56772f5224aeSachartre 	dataout_len = 0;
56782f5224aeSachartre 
56792f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
56802f5224aeSachartre 	    vd_scsi_len);
56812f5224aeSachartre 
56822f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
56832f5224aeSachartre 
56842f5224aeSachartre 	/* set cdb */
56852f5224aeSachartre 	cdb->scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
56862f5224aeSachartre 	cdb->cdb_opaque[1] = cmd;
56872f5224aeSachartre 	FORMG1COUNT(cdb, datain_len);
56882f5224aeSachartre 
56892f5224aeSachartre 	vd_scsi->timeout = vdc_scsi_timeout;
56902f5224aeSachartre 
56912f5224aeSachartre 	return (vd_scsi);
56922f5224aeSachartre }
56932f5224aeSachartre 
56942f5224aeSachartre /*
56952f5224aeSachartre  * Create a VD_OP_SCSICMD buffer for a SCSI PERSISTENT OUT command.
56962f5224aeSachartre  *
56972f5224aeSachartre  * Arguments:
56982f5224aeSachartre  *	cmd		- SCSI PERSISTENT OUT command
56992f5224aeSachartre  *	len		- length of the SCSI output buffer
57002f5224aeSachartre  *	vd_scsi_len	- return the length of the allocated buffer
57012f5224aeSachartre  *
57022f5224aeSachartre  * Returned Code:
57032f5224aeSachartre  *	a pointer to the allocated VD_OP_SCSICMD buffer.
57042f5224aeSachartre  */
57052f5224aeSachartre static vd_scsi_t *
57062f5224aeSachartre vdc_scsi_alloc_persistent_out(uchar_t cmd, int len, int *vd_scsi_len)
57072f5224aeSachartre {
57082f5224aeSachartre 	int cdb_len, sense_len, datain_len, dataout_len;
57092f5224aeSachartre 	vd_scsi_t *vd_scsi;
57102f5224aeSachartre 	union scsi_cdb *cdb;
57112f5224aeSachartre 
57122f5224aeSachartre 	cdb_len = CDB_GROUP1;
57132f5224aeSachartre 	sense_len = sizeof (struct scsi_extended_sense);
57142f5224aeSachartre 	datain_len = 0;
57152f5224aeSachartre 	dataout_len = len;
57162f5224aeSachartre 
57172f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
57182f5224aeSachartre 	    vd_scsi_len);
57192f5224aeSachartre 
57202f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
57212f5224aeSachartre 
57222f5224aeSachartre 	/* set cdb */
57232f5224aeSachartre 	cdb->scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
57242f5224aeSachartre 	cdb->cdb_opaque[1] = cmd;
57252f5224aeSachartre 	FORMG1COUNT(cdb, dataout_len);
57262f5224aeSachartre 
57272f5224aeSachartre 	vd_scsi->timeout = vdc_scsi_timeout;
57282f5224aeSachartre 
57292f5224aeSachartre 	return (vd_scsi);
57302f5224aeSachartre }
57312f5224aeSachartre 
57322f5224aeSachartre /*
57332f5224aeSachartre  * Implement the MHIOCGRP_INKEYS mhd(7i) ioctl. The ioctl is converted
57342f5224aeSachartre  * to a SCSI PERSISTENT IN READ KEYS command which is sent to the vdisk
57352f5224aeSachartre  * server with a VD_OP_SCSICMD operation.
57362f5224aeSachartre  */
57372f5224aeSachartre static int
57382f5224aeSachartre vdc_mhd_inkeys(vdc_t *vdc, caddr_t arg, int mode)
57392f5224aeSachartre {
57402f5224aeSachartre 	vd_scsi_t *vd_scsi;
57412f5224aeSachartre 	mhioc_inkeys_t inkeys;
57422f5224aeSachartre 	mhioc_key_list_t klist;
57432f5224aeSachartre 	struct mhioc_inkeys32 inkeys32;
57442f5224aeSachartre 	struct mhioc_key_list32 klist32;
57452f5224aeSachartre 	sd_prin_readkeys_t *scsi_keys;
57462f5224aeSachartre 	void *user_keys;
57472f5224aeSachartre 	int vd_scsi_len;
57482f5224aeSachartre 	int listsize, listlen, rv;
57492f5224aeSachartre 
57502f5224aeSachartre 	/* copyin arguments */
57512f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
57522f5224aeSachartre 		rv = ddi_copyin(arg, &inkeys32, sizeof (inkeys32), mode);
57532f5224aeSachartre 		if (rv != 0)
57542f5224aeSachartre 			return (EFAULT);
57552f5224aeSachartre 
57562f5224aeSachartre 		rv = ddi_copyin((caddr_t)(uintptr_t)inkeys32.li, &klist32,
57572f5224aeSachartre 		    sizeof (klist32), mode);
57582f5224aeSachartre 		if (rv != 0)
57592f5224aeSachartre 			return (EFAULT);
57602f5224aeSachartre 
57612f5224aeSachartre 		listsize = klist32.listsize;
57622f5224aeSachartre 	} else {
57632f5224aeSachartre 		rv = ddi_copyin(arg, &inkeys, sizeof (inkeys), mode);
57642f5224aeSachartre 		if (rv != 0)
57652f5224aeSachartre 			return (EFAULT);
57662f5224aeSachartre 
57672f5224aeSachartre 		rv = ddi_copyin(inkeys.li, &klist, sizeof (klist), mode);
57682f5224aeSachartre 		if (rv != 0)
57692f5224aeSachartre 			return (EFAULT);
57702f5224aeSachartre 
57712f5224aeSachartre 		listsize = klist.listsize;
57722f5224aeSachartre 	}
57732f5224aeSachartre 
57742f5224aeSachartre 	/* build SCSI VD_OP request */
57752f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_in(SD_READ_KEYS,
57762f5224aeSachartre 	    sizeof (sd_prin_readkeys_t) - sizeof (caddr_t) +
57772f5224aeSachartre 	    (sizeof (mhioc_resv_key_t) * listsize), &vd_scsi_len);
57782f5224aeSachartre 
57792f5224aeSachartre 	scsi_keys = (sd_prin_readkeys_t *)VD_SCSI_DATA_IN(vd_scsi);
57802f5224aeSachartre 
57812f5224aeSachartre 	/* submit the request */
57822f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
57832f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
57842f5224aeSachartre 
57852f5224aeSachartre 	if (rv != 0)
57862f5224aeSachartre 		goto done;
57872f5224aeSachartre 
57882f5224aeSachartre 	listlen = scsi_keys->len / MHIOC_RESV_KEY_SIZE;
57892f5224aeSachartre 
57902f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
57912f5224aeSachartre 		inkeys32.generation = scsi_keys->generation;
57922f5224aeSachartre 		rv = ddi_copyout(&inkeys32, arg, sizeof (inkeys32), mode);
57932f5224aeSachartre 		if (rv != 0) {
57942f5224aeSachartre 			rv = EFAULT;
57952f5224aeSachartre 			goto done;
57962f5224aeSachartre 		}
57972f5224aeSachartre 
57982f5224aeSachartre 		klist32.listlen = listlen;
57992f5224aeSachartre 		rv = ddi_copyout(&klist32, (caddr_t)(uintptr_t)inkeys32.li,
58002f5224aeSachartre 		    sizeof (klist32), mode);
58012f5224aeSachartre 		if (rv != 0) {
58022f5224aeSachartre 			rv = EFAULT;
58032f5224aeSachartre 			goto done;
58042f5224aeSachartre 		}
58052f5224aeSachartre 
58062f5224aeSachartre 		user_keys = (caddr_t)(uintptr_t)klist32.list;
58072f5224aeSachartre 	} else {
58082f5224aeSachartre 		inkeys.generation = scsi_keys->generation;
58092f5224aeSachartre 		rv = ddi_copyout(&inkeys, arg, sizeof (inkeys), mode);
58102f5224aeSachartre 		if (rv != 0) {
58112f5224aeSachartre 			rv = EFAULT;
58122f5224aeSachartre 			goto done;
58132f5224aeSachartre 		}
58142f5224aeSachartre 
58152f5224aeSachartre 		klist.listlen = listlen;
58162f5224aeSachartre 		rv = ddi_copyout(&klist, inkeys.li, sizeof (klist), mode);
58172f5224aeSachartre 		if (rv != 0) {
58182f5224aeSachartre 			rv = EFAULT;
58192f5224aeSachartre 			goto done;
58202f5224aeSachartre 		}
58212f5224aeSachartre 
58222f5224aeSachartre 		user_keys = klist.list;
58232f5224aeSachartre 	}
58242f5224aeSachartre 
58252f5224aeSachartre 	/* copy out keys */
58262f5224aeSachartre 	if (listlen > 0 && listsize > 0) {
58272f5224aeSachartre 		if (listsize < listlen)
58282f5224aeSachartre 			listlen = listsize;
58292f5224aeSachartre 		rv = ddi_copyout(&scsi_keys->keylist, user_keys,
58302f5224aeSachartre 		    listlen * MHIOC_RESV_KEY_SIZE, mode);
58312f5224aeSachartre 		if (rv != 0)
58322f5224aeSachartre 			rv = EFAULT;
58332f5224aeSachartre 	}
58342f5224aeSachartre 
58352f5224aeSachartre 	if (rv == 0)
58362f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
58372f5224aeSachartre 
58382f5224aeSachartre done:
58392f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
58402f5224aeSachartre 
58412f5224aeSachartre 	return (rv);
58422f5224aeSachartre }
58432f5224aeSachartre 
58442f5224aeSachartre /*
58452f5224aeSachartre  * Implement the MHIOCGRP_INRESV mhd(7i) ioctl. The ioctl is converted
58462f5224aeSachartre  * to a SCSI PERSISTENT IN READ RESERVATION command which is sent to
58472f5224aeSachartre  * the vdisk server with a VD_OP_SCSICMD operation.
58482f5224aeSachartre  */
58492f5224aeSachartre static int
58502f5224aeSachartre vdc_mhd_inresv(vdc_t *vdc, caddr_t arg, int mode)
58512f5224aeSachartre {
58522f5224aeSachartre 	vd_scsi_t *vd_scsi;
58532f5224aeSachartre 	mhioc_inresvs_t inresv;
58542f5224aeSachartre 	mhioc_resv_desc_list_t rlist;
58552f5224aeSachartre 	struct mhioc_inresvs32 inresv32;
58562f5224aeSachartre 	struct mhioc_resv_desc_list32 rlist32;
58572f5224aeSachartre 	mhioc_resv_desc_t mhd_resv;
58582f5224aeSachartre 	sd_prin_readresv_t *scsi_resv;
58592f5224aeSachartre 	sd_readresv_desc_t *resv;
58602f5224aeSachartre 	mhioc_resv_desc_t *user_resv;
58612f5224aeSachartre 	int vd_scsi_len;
58622f5224aeSachartre 	int listsize, listlen, i, rv;
58632f5224aeSachartre 
58642f5224aeSachartre 	/* copyin arguments */
58652f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
58662f5224aeSachartre 		rv = ddi_copyin(arg, &inresv32, sizeof (inresv32), mode);
58672f5224aeSachartre 		if (rv != 0)
58682f5224aeSachartre 			return (EFAULT);
58692f5224aeSachartre 
58702f5224aeSachartre 		rv = ddi_copyin((caddr_t)(uintptr_t)inresv32.li, &rlist32,
58712f5224aeSachartre 		    sizeof (rlist32), mode);
58722f5224aeSachartre 		if (rv != 0)
58732f5224aeSachartre 			return (EFAULT);
58742f5224aeSachartre 
58752f5224aeSachartre 		listsize = rlist32.listsize;
58762f5224aeSachartre 	} else {
58772f5224aeSachartre 		rv = ddi_copyin(arg, &inresv, sizeof (inresv), mode);
58782f5224aeSachartre 		if (rv != 0)
58792f5224aeSachartre 			return (EFAULT);
58802f5224aeSachartre 
58812f5224aeSachartre 		rv = ddi_copyin(inresv.li, &rlist, sizeof (rlist), mode);
58822f5224aeSachartre 		if (rv != 0)
58832f5224aeSachartre 			return (EFAULT);
58842f5224aeSachartre 
58852f5224aeSachartre 		listsize = rlist.listsize;
58862f5224aeSachartre 	}
58872f5224aeSachartre 
58882f5224aeSachartre 	/* build SCSI VD_OP request */
58892f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_in(SD_READ_RESV,
58902f5224aeSachartre 	    sizeof (sd_prin_readresv_t) - sizeof (caddr_t) +
58912f5224aeSachartre 	    (SCSI3_RESV_DESC_LEN * listsize), &vd_scsi_len);
58922f5224aeSachartre 
58932f5224aeSachartre 	scsi_resv = (sd_prin_readresv_t *)VD_SCSI_DATA_IN(vd_scsi);
58942f5224aeSachartre 
58952f5224aeSachartre 	/* submit the request */
58962f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
58972f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
58982f5224aeSachartre 
58992f5224aeSachartre 	if (rv != 0)
59002f5224aeSachartre 		goto done;
59012f5224aeSachartre 
59022f5224aeSachartre 	listlen = scsi_resv->len / SCSI3_RESV_DESC_LEN;
59032f5224aeSachartre 
59042f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
59052f5224aeSachartre 		inresv32.generation = scsi_resv->generation;
59062f5224aeSachartre 		rv = ddi_copyout(&inresv32, arg, sizeof (inresv32), mode);
59072f5224aeSachartre 		if (rv != 0) {
59082f5224aeSachartre 			rv = EFAULT;
59092f5224aeSachartre 			goto done;
59102f5224aeSachartre 		}
59112f5224aeSachartre 
59122f5224aeSachartre 		rlist32.listlen = listlen;
59132f5224aeSachartre 		rv = ddi_copyout(&rlist32, (caddr_t)(uintptr_t)inresv32.li,
59142f5224aeSachartre 		    sizeof (rlist32), mode);
59152f5224aeSachartre 		if (rv != 0) {
59162f5224aeSachartre 			rv = EFAULT;
59172f5224aeSachartre 			goto done;
59182f5224aeSachartre 		}
59192f5224aeSachartre 
59202f5224aeSachartre 		user_resv = (mhioc_resv_desc_t *)(uintptr_t)rlist32.list;
59212f5224aeSachartre 	} else {
59222f5224aeSachartre 		inresv.generation = scsi_resv->generation;
59232f5224aeSachartre 		rv = ddi_copyout(&inresv, arg, sizeof (inresv), mode);
59242f5224aeSachartre 		if (rv != 0) {
59252f5224aeSachartre 			rv = EFAULT;
59262f5224aeSachartre 			goto done;
59272f5224aeSachartre 		}
59282f5224aeSachartre 
59292f5224aeSachartre 		rlist.listlen = listlen;
59302f5224aeSachartre 		rv = ddi_copyout(&rlist, inresv.li, sizeof (rlist), mode);
59312f5224aeSachartre 		if (rv != 0) {
59322f5224aeSachartre 			rv = EFAULT;
59332f5224aeSachartre 			goto done;
59342f5224aeSachartre 		}
59352f5224aeSachartre 
59362f5224aeSachartre 		user_resv = rlist.list;
59372f5224aeSachartre 	}
59382f5224aeSachartre 
59392f5224aeSachartre 	/* copy out reservations */
59402f5224aeSachartre 	if (listsize > 0 && listlen > 0) {
59412f5224aeSachartre 		if (listsize < listlen)
59422f5224aeSachartre 			listlen = listsize;
59432f5224aeSachartre 		resv = (sd_readresv_desc_t *)&scsi_resv->readresv_desc;
59442f5224aeSachartre 
59452f5224aeSachartre 		for (i = 0; i < listlen; i++) {
59462f5224aeSachartre 			mhd_resv.type = resv->type;
59472f5224aeSachartre 			mhd_resv.scope = resv->scope;
59482f5224aeSachartre 			mhd_resv.scope_specific_addr =
59492f5224aeSachartre 			    BE_32(resv->scope_specific_addr);
59502f5224aeSachartre 			bcopy(&resv->resvkey, &mhd_resv.key,
59512f5224aeSachartre 			    MHIOC_RESV_KEY_SIZE);
59522f5224aeSachartre 
59532f5224aeSachartre 			rv = ddi_copyout(&mhd_resv, user_resv,
59542f5224aeSachartre 			    sizeof (mhd_resv), mode);
59552f5224aeSachartre 			if (rv != 0) {
59562f5224aeSachartre 				rv = EFAULT;
59572f5224aeSachartre 				goto done;
59582f5224aeSachartre 			}
59592f5224aeSachartre 			resv++;
59602f5224aeSachartre 			user_resv++;
59612f5224aeSachartre 		}
59622f5224aeSachartre 	}
59632f5224aeSachartre 
59642f5224aeSachartre 	if (rv == 0)
59652f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
59662f5224aeSachartre 
59672f5224aeSachartre done:
59682f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
59692f5224aeSachartre 	return (rv);
59702f5224aeSachartre }
59712f5224aeSachartre 
59722f5224aeSachartre /*
59732f5224aeSachartre  * Implement the MHIOCGRP_REGISTER mhd(7i) ioctl. The ioctl is converted
59742f5224aeSachartre  * to a SCSI PERSISTENT OUT REGISTER command which is sent to the vdisk
59752f5224aeSachartre  * server with a VD_OP_SCSICMD operation.
59762f5224aeSachartre  */
59772f5224aeSachartre static int
59782f5224aeSachartre vdc_mhd_register(vdc_t *vdc, caddr_t arg, int mode)
59792f5224aeSachartre {
59802f5224aeSachartre 	vd_scsi_t *vd_scsi;
59812f5224aeSachartre 	sd_prout_t *scsi_prout;
59822f5224aeSachartre 	mhioc_register_t mhd_reg;
59832f5224aeSachartre 	int vd_scsi_len, rv;
59842f5224aeSachartre 
59852f5224aeSachartre 	/* copyin arguments */
59862f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_reg, sizeof (mhd_reg), mode);
59872f5224aeSachartre 	if (rv != 0)
59882f5224aeSachartre 		return (EFAULT);
59892f5224aeSachartre 
59902f5224aeSachartre 	/* build SCSI VD_OP request */
59912f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_REGISTER,
59922f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
59932f5224aeSachartre 
59942f5224aeSachartre 	/* set parameters */
59952f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
59962f5224aeSachartre 	bcopy(mhd_reg.oldkey.key, scsi_prout->res_key, MHIOC_RESV_KEY_SIZE);
59972f5224aeSachartre 	bcopy(mhd_reg.newkey.key, scsi_prout->service_key, MHIOC_RESV_KEY_SIZE);
59982f5224aeSachartre 	scsi_prout->aptpl = (uchar_t)mhd_reg.aptpl;
59992f5224aeSachartre 
60002f5224aeSachartre 	/* submit the request */
60012f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
60022f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
60032f5224aeSachartre 
60042f5224aeSachartre 	if (rv == 0)
60052f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
60062f5224aeSachartre 
60072f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
60082f5224aeSachartre 	return (rv);
60092f5224aeSachartre }
60102f5224aeSachartre 
60112f5224aeSachartre /*
60122f5224aeSachartre  * Implement the MHIOCGRP_RESERVE mhd(7i) ioctl. The ioctl is converted
60132f5224aeSachartre  * to a SCSI PERSISTENT OUT RESERVE command which is sent to the vdisk
60142f5224aeSachartre  * server with a VD_OP_SCSICMD operation.
60152f5224aeSachartre  */
60162f5224aeSachartre static int
60172f5224aeSachartre vdc_mhd_reserve(vdc_t *vdc, caddr_t arg, int mode)
60182f5224aeSachartre {
60192f5224aeSachartre 	union scsi_cdb *cdb;
60202f5224aeSachartre 	vd_scsi_t *vd_scsi;
60212f5224aeSachartre 	sd_prout_t *scsi_prout;
60222f5224aeSachartre 	mhioc_resv_desc_t mhd_resv;
60232f5224aeSachartre 	int vd_scsi_len, rv;
60242f5224aeSachartre 
60252f5224aeSachartre 	/* copyin arguments */
60262f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_resv, sizeof (mhd_resv), mode);
60272f5224aeSachartre 	if (rv != 0)
60282f5224aeSachartre 		return (EFAULT);
60292f5224aeSachartre 
60302f5224aeSachartre 	/* build SCSI VD_OP request */
60312f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_RESERVE,
60322f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
60332f5224aeSachartre 
60342f5224aeSachartre 	/* set parameters */
60352f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
60362f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
60372f5224aeSachartre 	bcopy(mhd_resv.key.key, scsi_prout->res_key, MHIOC_RESV_KEY_SIZE);
60382f5224aeSachartre 	scsi_prout->scope_address = mhd_resv.scope_specific_addr;
60392f5224aeSachartre 	cdb->cdb_opaque[2] = mhd_resv.type;
60402f5224aeSachartre 
60412f5224aeSachartre 	/* submit the request */
60422f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
60432f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
60442f5224aeSachartre 
60452f5224aeSachartre 	if (rv == 0)
60462f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
60472f5224aeSachartre 
60482f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
60492f5224aeSachartre 	return (rv);
60502f5224aeSachartre }
60512f5224aeSachartre 
60522f5224aeSachartre /*
60532f5224aeSachartre  * Implement the MHIOCGRP_PREEMPTANDABORT mhd(7i) ioctl. The ioctl is
60542f5224aeSachartre  * converted to a SCSI PERSISTENT OUT PREEMPT AND ABORT command which
60552f5224aeSachartre  * is sent to the vdisk server with a VD_OP_SCSICMD operation.
60562f5224aeSachartre  */
60572f5224aeSachartre static int
60582f5224aeSachartre vdc_mhd_preemptabort(vdc_t *vdc, caddr_t arg, int mode)
60592f5224aeSachartre {
60602f5224aeSachartre 	union scsi_cdb *cdb;
60612f5224aeSachartre 	vd_scsi_t *vd_scsi;
60622f5224aeSachartre 	sd_prout_t *scsi_prout;
60632f5224aeSachartre 	mhioc_preemptandabort_t mhd_preempt;
60642f5224aeSachartre 	int vd_scsi_len, rv;
60652f5224aeSachartre 
60662f5224aeSachartre 	/* copyin arguments */
60672f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_preempt, sizeof (mhd_preempt), mode);
60682f5224aeSachartre 	if (rv != 0)
60692f5224aeSachartre 		return (EFAULT);
60702f5224aeSachartre 
60712f5224aeSachartre 	/* build SCSI VD_OP request */
60722f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_PREEMPTANDABORT,
60732f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
60742f5224aeSachartre 
60752f5224aeSachartre 	/* set parameters */
60762f5224aeSachartre 	vd_scsi->task_attribute = VD_SCSI_TASK_ACA;
60772f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
60782f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
60792f5224aeSachartre 	bcopy(mhd_preempt.resvdesc.key.key, scsi_prout->res_key,
60802f5224aeSachartre 	    MHIOC_RESV_KEY_SIZE);
60812f5224aeSachartre 	bcopy(mhd_preempt.victim_key.key, scsi_prout->service_key,
60822f5224aeSachartre 	    MHIOC_RESV_KEY_SIZE);
60832f5224aeSachartre 	scsi_prout->scope_address = mhd_preempt.resvdesc.scope_specific_addr;
60842f5224aeSachartre 	cdb->cdb_opaque[2] = mhd_preempt.resvdesc.type;
60852f5224aeSachartre 
60862f5224aeSachartre 	/* submit the request */
60872f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
60882f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
60892f5224aeSachartre 
60902f5224aeSachartre 	if (rv == 0)
60912f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
60922f5224aeSachartre 
60932f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
60942f5224aeSachartre 	return (rv);
60952f5224aeSachartre }
60962f5224aeSachartre 
60972f5224aeSachartre /*
60982f5224aeSachartre  * Implement the MHIOCGRP_REGISTERANDIGNOREKEY mhd(7i) ioctl. The ioctl
60992f5224aeSachartre  * is converted to a SCSI PERSISTENT OUT REGISTER AND IGNORE EXISTING KEY
61002f5224aeSachartre  * command which is sent to the vdisk server with a VD_OP_SCSICMD operation.
61012f5224aeSachartre  */
61022f5224aeSachartre static int
61032f5224aeSachartre vdc_mhd_registerignore(vdc_t *vdc, caddr_t arg, int mode)
61042f5224aeSachartre {
61052f5224aeSachartre 	vd_scsi_t *vd_scsi;
61062f5224aeSachartre 	sd_prout_t *scsi_prout;
61072f5224aeSachartre 	mhioc_registerandignorekey_t mhd_regi;
61082f5224aeSachartre 	int vd_scsi_len, rv;
61092f5224aeSachartre 
61102f5224aeSachartre 	/* copyin arguments */
61112f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_regi, sizeof (mhd_regi), mode);
61122f5224aeSachartre 	if (rv != 0)
61132f5224aeSachartre 		return (EFAULT);
61142f5224aeSachartre 
61152f5224aeSachartre 	/* build SCSI VD_OP request */
61162f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_REGISTERANDIGNOREKEY,
61172f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
61182f5224aeSachartre 
61192f5224aeSachartre 	/* set parameters */
61202f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
61212f5224aeSachartre 	bcopy(mhd_regi.newkey.key, scsi_prout->service_key,
61222f5224aeSachartre 	    MHIOC_RESV_KEY_SIZE);
61232f5224aeSachartre 	scsi_prout->aptpl = (uchar_t)mhd_regi.aptpl;
61242f5224aeSachartre 
61252f5224aeSachartre 	/* submit the request */
61262f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
61272f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
61282f5224aeSachartre 
61292f5224aeSachartre 	if (rv == 0)
61302f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
61312f5224aeSachartre 
61322f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
61332f5224aeSachartre 	return (rv);
61342f5224aeSachartre }
61352f5224aeSachartre 
61362f5224aeSachartre /*
61372f5224aeSachartre  * This function is used by the failfast mechanism to send a SCSI command
61382f5224aeSachartre  * to check for reservation conflict.
61392f5224aeSachartre  */
61402f5224aeSachartre static int
61412f5224aeSachartre vdc_failfast_scsi_cmd(vdc_t *vdc, uchar_t scmd)
61422f5224aeSachartre {
61432f5224aeSachartre 	int cdb_len, sense_len, vd_scsi_len;
61442f5224aeSachartre 	vd_scsi_t *vd_scsi;
61452f5224aeSachartre 	union scsi_cdb *cdb;
61462f5224aeSachartre 	int rv;
61472f5224aeSachartre 
61482f5224aeSachartre 	ASSERT(scmd == SCMD_TEST_UNIT_READY || scmd == SCMD_WRITE_G1);
61492f5224aeSachartre 
61502f5224aeSachartre 	if (scmd == SCMD_WRITE_G1)
61512f5224aeSachartre 		cdb_len = CDB_GROUP1;
61522f5224aeSachartre 	else
61532f5224aeSachartre 		cdb_len = CDB_GROUP0;
61542f5224aeSachartre 
61552f5224aeSachartre 	sense_len = sizeof (struct scsi_extended_sense);
61562f5224aeSachartre 
61572f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, 0, 0, &vd_scsi_len);
61582f5224aeSachartre 
61592f5224aeSachartre 	/* set cdb */
61602f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
61612f5224aeSachartre 	cdb->scc_cmd = scmd;
61622f5224aeSachartre 
61632f5224aeSachartre 	vd_scsi->timeout = vdc_scsi_timeout;
61642f5224aeSachartre 
61652f5224aeSachartre 	/*
61662f5224aeSachartre 	 * Submit the request. The last argument has to be B_FALSE so that
61672f5224aeSachartre 	 * vdc_do_sync_op does not loop checking for reservation conflict if
61682f5224aeSachartre 	 * the operation returns an error.
61692f5224aeSachartre 	 */
61702f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
61712f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)FKIOCTL, VIO_both_dir, B_FALSE);
61722f5224aeSachartre 
61732f5224aeSachartre 	if (rv == 0)
61742f5224aeSachartre 		(void) vdc_scsi_status(vdc, vd_scsi, B_FALSE);
61752f5224aeSachartre 
61762f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
61772f5224aeSachartre 	return (rv);
61782f5224aeSachartre }
61792f5224aeSachartre 
61802f5224aeSachartre /*
61812f5224aeSachartre  * This function is used by the failfast mechanism to check for reservation
61822f5224aeSachartre  * conflict. It sends some SCSI commands which will fail with a reservation
61832f5224aeSachartre  * conflict error if the system does not have access to the disk and this
61842f5224aeSachartre  * will panic the system.
61852f5224aeSachartre  *
61862f5224aeSachartre  * Returned Code:
61872f5224aeSachartre  *	0	- disk is accessible without reservation conflict error
61882f5224aeSachartre  *	!= 0	- unable to check if disk is accessible
61892f5224aeSachartre  */
61902f5224aeSachartre int
61912f5224aeSachartre vdc_failfast_check_resv(vdc_t *vdc)
61922f5224aeSachartre {
61932f5224aeSachartre 	int failure = 0;
61942f5224aeSachartre 
61952f5224aeSachartre 	/*
61962f5224aeSachartre 	 * Send a TEST UNIT READY command. The command will panic
61972f5224aeSachartre 	 * the system if it fails with a reservation conflict.
61982f5224aeSachartre 	 */
61992f5224aeSachartre 	if (vdc_failfast_scsi_cmd(vdc, SCMD_TEST_UNIT_READY) != 0)
62002f5224aeSachartre 		failure++;
62012f5224aeSachartre 
62022f5224aeSachartre 	/*
62032f5224aeSachartre 	 * With SPC-3 compliant devices TEST UNIT READY will succeed on
62042f5224aeSachartre 	 * a reserved device, so we also do a WRITE(10) of zero byte in
62052f5224aeSachartre 	 * order to provoke a Reservation Conflict status on those newer
62062f5224aeSachartre 	 * devices.
62072f5224aeSachartre 	 */
62082f5224aeSachartre 	if (vdc_failfast_scsi_cmd(vdc, SCMD_WRITE_G1) != 0)
62092f5224aeSachartre 		failure++;
62102f5224aeSachartre 
62112f5224aeSachartre 	return (failure);
62122f5224aeSachartre }
62132f5224aeSachartre 
62142f5224aeSachartre /*
62152f5224aeSachartre  * Add a pending I/O to the failfast I/O queue. An I/O is added to this
62162f5224aeSachartre  * queue when it has failed and failfast is enabled. Then we have to check
62172f5224aeSachartre  * if it has failed because of a reservation conflict in which case we have
62182f5224aeSachartre  * to panic the system.
62192f5224aeSachartre  *
62202f5224aeSachartre  * Async I/O should be queued with their block I/O data transfer structure
62212f5224aeSachartre  * (buf). Sync I/O should be queued with buf = NULL.
62222f5224aeSachartre  */
62232f5224aeSachartre static vdc_io_t *
62242f5224aeSachartre vdc_failfast_io_queue(vdc_t *vdc, struct buf *buf)
62252f5224aeSachartre {
62262f5224aeSachartre 	vdc_io_t *vio;
62272f5224aeSachartre 
62282f5224aeSachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
62292f5224aeSachartre 
62302f5224aeSachartre 	vio = kmem_alloc(sizeof (vdc_io_t), KM_SLEEP);
62312f5224aeSachartre 	vio->vio_next = vdc->failfast_io_queue;
62322f5224aeSachartre 	vio->vio_buf = buf;
62332f5224aeSachartre 	vio->vio_qtime = ddi_get_lbolt();
62342f5224aeSachartre 
62352f5224aeSachartre 	vdc->failfast_io_queue = vio;
62362f5224aeSachartre 
62372f5224aeSachartre 	/* notify the failfast thread that a new I/O is queued */
62382f5224aeSachartre 	cv_signal(&vdc->failfast_cv);
62392f5224aeSachartre 
62402f5224aeSachartre 	return (vio);
62412f5224aeSachartre }
62422f5224aeSachartre 
62432f5224aeSachartre /*
62442f5224aeSachartre  * Remove and complete I/O in the failfast I/O queue which have been
62452f5224aeSachartre  * added after the indicated deadline. A deadline of 0 means that all
62462f5224aeSachartre  * I/O have to be unqueued and marked as completed.
62472f5224aeSachartre  */
62482f5224aeSachartre static void
62492f5224aeSachartre vdc_failfast_io_unqueue(vdc_t *vdc, clock_t deadline)
62502f5224aeSachartre {
62512f5224aeSachartre 	vdc_io_t *vio, *vio_tmp;
62522f5224aeSachartre 
62532f5224aeSachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
62542f5224aeSachartre 
62552f5224aeSachartre 	vio_tmp = NULL;
62562f5224aeSachartre 	vio = vdc->failfast_io_queue;
62572f5224aeSachartre 
62582f5224aeSachartre 	if (deadline != 0) {
62592f5224aeSachartre 		/*
62602f5224aeSachartre 		 * Skip any io queued after the deadline. The failfast
62612f5224aeSachartre 		 * I/O queue is ordered starting with the last I/O added
62622f5224aeSachartre 		 * to the queue.
62632f5224aeSachartre 		 */
62642f5224aeSachartre 		while (vio != NULL && vio->vio_qtime > deadline) {
62652f5224aeSachartre 			vio_tmp = vio;
62662f5224aeSachartre 			vio = vio->vio_next;
62672f5224aeSachartre 		}
62682f5224aeSachartre 	}
62692f5224aeSachartre 
62702f5224aeSachartre 	if (vio == NULL)
62712f5224aeSachartre 		/* nothing to unqueue */
62722f5224aeSachartre 		return;
62732f5224aeSachartre 
62742f5224aeSachartre 	/* update the queue */
62752f5224aeSachartre 	if (vio_tmp == NULL)
62762f5224aeSachartre 		vdc->failfast_io_queue = NULL;
62772f5224aeSachartre 	else
62782f5224aeSachartre 		vio_tmp->vio_next = NULL;
62792f5224aeSachartre 
62802f5224aeSachartre 	/*
62812f5224aeSachartre 	 * Complete unqueued I/O. Async I/O have a block I/O data transfer
62822f5224aeSachartre 	 * structure (buf) and they are completed by calling biodone(). Sync
62832f5224aeSachartre 	 * I/O do not have a buf and they are completed by setting the
62842f5224aeSachartre 	 * vio_qtime to zero and signaling failfast_io_cv. In that case, the
62852f5224aeSachartre 	 * thread waiting for the I/O to complete is responsible for freeing
62862f5224aeSachartre 	 * the vio structure.
62872f5224aeSachartre 	 */
62882f5224aeSachartre 	while (vio != NULL) {
62892f5224aeSachartre 		vio_tmp = vio->vio_next;
62902f5224aeSachartre 		if (vio->vio_buf != NULL) {
629190e2f9dcSlm66018 			VD_KSTAT_RUNQ_EXIT(vdc);
6292366a92acSlm66018 			DTRACE_IO1(done, buf_t *, vio->vio_buf);
62932f5224aeSachartre 			biodone(vio->vio_buf);
62942f5224aeSachartre 			kmem_free(vio, sizeof (vdc_io_t));
62952f5224aeSachartre 		} else {
62962f5224aeSachartre 			vio->vio_qtime = 0;
62972f5224aeSachartre 		}
62982f5224aeSachartre 		vio = vio_tmp;
62992f5224aeSachartre 	}
63002f5224aeSachartre 
63012f5224aeSachartre 	cv_broadcast(&vdc->failfast_io_cv);
63022f5224aeSachartre }
63032f5224aeSachartre 
63042f5224aeSachartre /*
63052f5224aeSachartre  * Failfast Thread.
63062f5224aeSachartre  *
63072f5224aeSachartre  * While failfast is enabled, the failfast thread sends a TEST UNIT READY
63082f5224aeSachartre  * and a zero size WRITE(10) SCSI commands on a regular basis to check that
63092f5224aeSachartre  * we still have access to the disk. If a command fails with a RESERVATION
63102f5224aeSachartre  * CONFLICT error then the system will immediatly panic.
63112f5224aeSachartre  *
63122f5224aeSachartre  * The failfast thread is also woken up when an I/O has failed. It then check
63132f5224aeSachartre  * the access to the disk to ensure that the I/O failure was not due to a
63142f5224aeSachartre  * reservation conflict.
63152f5224aeSachartre  *
63162f5224aeSachartre  * There is one failfast thread for each virtual disk for which failfast is
63172f5224aeSachartre  * enabled. We could have only one thread sending requests for all disks but
63182f5224aeSachartre  * this would need vdc to send asynchronous requests and to have callbacks to
63192f5224aeSachartre  * process replies.
63202f5224aeSachartre  */
63212f5224aeSachartre static void
63222f5224aeSachartre vdc_failfast_thread(void *arg)
63232f5224aeSachartre {
63242f5224aeSachartre 	int status;
63252f5224aeSachartre 	vdc_t *vdc = (vdc_t *)arg;
63262f5224aeSachartre 	clock_t timeout, starttime;
63272f5224aeSachartre 
63282f5224aeSachartre 	mutex_enter(&vdc->lock);
63292f5224aeSachartre 
63302f5224aeSachartre 	while (vdc->failfast_interval != 0) {
63312f5224aeSachartre 
63322f5224aeSachartre 		starttime = ddi_get_lbolt();
63332f5224aeSachartre 
63342f5224aeSachartre 		mutex_exit(&vdc->lock);
63352f5224aeSachartre 
63362f5224aeSachartre 		/* check for reservation conflict */
63372f5224aeSachartre 		status = vdc_failfast_check_resv(vdc);
63382f5224aeSachartre 
63392f5224aeSachartre 		mutex_enter(&vdc->lock);
63402f5224aeSachartre 		/*
63412f5224aeSachartre 		 * We have dropped the lock to send the SCSI command so we have
63422f5224aeSachartre 		 * to check that failfast is still enabled.
63432f5224aeSachartre 		 */
63442f5224aeSachartre 		if (vdc->failfast_interval == 0)
63452f5224aeSachartre 			break;
63462f5224aeSachartre 
63472f5224aeSachartre 		/*
63482f5224aeSachartre 		 * If we have successfully check the disk access and there was
63492f5224aeSachartre 		 * no reservation conflict then we can complete any I/O queued
63502f5224aeSachartre 		 * before the last check.
63512f5224aeSachartre 		 */
63522f5224aeSachartre 		if (status == 0)
63532f5224aeSachartre 			vdc_failfast_io_unqueue(vdc, starttime);
63542f5224aeSachartre 
63552f5224aeSachartre 		/* proceed again if some I/O are still in the queue */
63562f5224aeSachartre 		if (vdc->failfast_io_queue != NULL)
63572f5224aeSachartre 			continue;
63582f5224aeSachartre 
63592f5224aeSachartre 		timeout = ddi_get_lbolt() +
63602f5224aeSachartre 		    drv_usectohz(vdc->failfast_interval);
63612f5224aeSachartre 		(void) cv_timedwait(&vdc->failfast_cv, &vdc->lock, timeout);
63622f5224aeSachartre 	}
63632f5224aeSachartre 
63642f5224aeSachartre 	/*
63652f5224aeSachartre 	 * Failfast is being stop so we can complete any queued I/O.
63662f5224aeSachartre 	 */
63672f5224aeSachartre 	vdc_failfast_io_unqueue(vdc, 0);
63682f5224aeSachartre 	vdc->failfast_thread = NULL;
63692f5224aeSachartre 	mutex_exit(&vdc->lock);
63702f5224aeSachartre 	thread_exit();
63712f5224aeSachartre }
63722f5224aeSachartre 
63732f5224aeSachartre /*
63742f5224aeSachartre  * Implement the MHIOCENFAILFAST mhd(7i) ioctl.
63752f5224aeSachartre  */
63762f5224aeSachartre static int
63772f5224aeSachartre vdc_failfast(vdc_t *vdc, caddr_t arg, int mode)
63782f5224aeSachartre {
63792f5224aeSachartre 	unsigned int mh_time;
63802f5224aeSachartre 
63812f5224aeSachartre 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), mode))
63822f5224aeSachartre 		return (EFAULT);
63832f5224aeSachartre 
63842f5224aeSachartre 	mutex_enter(&vdc->lock);
63852f5224aeSachartre 	if (mh_time != 0 && vdc->failfast_thread == NULL) {
63862f5224aeSachartre 		vdc->failfast_thread = thread_create(NULL, 0,
63872f5224aeSachartre 		    vdc_failfast_thread, vdc, 0, &p0, TS_RUN,
63882f5224aeSachartre 		    v.v_maxsyspri - 2);
63892f5224aeSachartre 	}
63902f5224aeSachartre 
63912f5224aeSachartre 	vdc->failfast_interval = mh_time * 1000;
63922f5224aeSachartre 	cv_signal(&vdc->failfast_cv);
63932f5224aeSachartre 	mutex_exit(&vdc->lock);
63942f5224aeSachartre 
63952f5224aeSachartre 	return (0);
63962f5224aeSachartre }
63972f5224aeSachartre 
63982f5224aeSachartre /*
63992f5224aeSachartre  * Implement the MHIOCTKOWN and MHIOCRELEASE mhd(7i) ioctls. These ioctls are
64002f5224aeSachartre  * converted to VD_OP_SET_ACCESS operations.
64012f5224aeSachartre  */
64022f5224aeSachartre static int
64032f5224aeSachartre vdc_access_set(vdc_t *vdc, uint64_t flags, int mode)
64042f5224aeSachartre {
64052f5224aeSachartre 	int rv;
64062f5224aeSachartre 
64072f5224aeSachartre 	/* submit owership command request */
64082f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SET_ACCESS, (caddr_t)&flags,
64092f5224aeSachartre 	    sizeof (uint64_t), 0, 0, CB_SYNC, (void *)(uint64_t)mode,
64102f5224aeSachartre 	    VIO_both_dir, B_TRUE);
64112f5224aeSachartre 
64122f5224aeSachartre 	return (rv);
64132f5224aeSachartre }
64142f5224aeSachartre 
64152f5224aeSachartre /*
64162f5224aeSachartre  * Implement the MHIOCSTATUS mhd(7i) ioctl. This ioctl is converted to a
64172f5224aeSachartre  * VD_OP_GET_ACCESS operation.
64182f5224aeSachartre  */
64192f5224aeSachartre static int
64202f5224aeSachartre vdc_access_get(vdc_t *vdc, uint64_t *status, int mode)
64212f5224aeSachartre {
64222f5224aeSachartre 	int rv;
64232f5224aeSachartre 
64242f5224aeSachartre 	/* submit owership command request */
64252f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_GET_ACCESS, (caddr_t)status,
64262f5224aeSachartre 	    sizeof (uint64_t), 0, 0, CB_SYNC, (void *)(uint64_t)mode,
64272f5224aeSachartre 	    VIO_both_dir, B_TRUE);
64282f5224aeSachartre 
64292f5224aeSachartre 	return (rv);
64302f5224aeSachartre }
64312f5224aeSachartre 
64322f5224aeSachartre /*
64332f5224aeSachartre  * Disk Ownership Thread.
64342f5224aeSachartre  *
64352f5224aeSachartre  * When we have taken the ownership of a disk, this thread waits to be
64362f5224aeSachartre  * notified when the LDC channel is reset so that it can recover the
64372f5224aeSachartre  * ownership.
64382f5224aeSachartre  *
64392f5224aeSachartre  * Note that the thread handling the LDC reset (vdc_process_msg_thread())
64402f5224aeSachartre  * can not be used to do the ownership recovery because it has to be
64412f5224aeSachartre  * running to handle the reply message to the ownership operation.
64422f5224aeSachartre  */
64432f5224aeSachartre static void
64442f5224aeSachartre vdc_ownership_thread(void *arg)
64452f5224aeSachartre {
64462f5224aeSachartre 	vdc_t *vdc = (vdc_t *)arg;
64472f5224aeSachartre 	clock_t timeout;
64482f5224aeSachartre 	uint64_t status;
64492f5224aeSachartre 
64502f5224aeSachartre 	mutex_enter(&vdc->ownership_lock);
64512f5224aeSachartre 	mutex_enter(&vdc->lock);
64522f5224aeSachartre 
64532f5224aeSachartre 	while (vdc->ownership & VDC_OWNERSHIP_WANTED) {
64542f5224aeSachartre 
64552f5224aeSachartre 		if ((vdc->ownership & VDC_OWNERSHIP_RESET) ||
64562f5224aeSachartre 		    !(vdc->ownership & VDC_OWNERSHIP_GRANTED)) {
64572f5224aeSachartre 			/*
64582f5224aeSachartre 			 * There was a reset so the ownership has been lost,
64592f5224aeSachartre 			 * try to recover. We do this without using the preempt
64602f5224aeSachartre 			 * option so that we don't steal the ownership from
64612f5224aeSachartre 			 * someone who has preempted us.
64622f5224aeSachartre 			 */
64632f5224aeSachartre 			DMSG(vdc, 0, "[%d] Ownership lost, recovering",
64642f5224aeSachartre 			    vdc->instance);
64652f5224aeSachartre 
64662f5224aeSachartre 			vdc->ownership &= ~(VDC_OWNERSHIP_RESET |
64672f5224aeSachartre 			    VDC_OWNERSHIP_GRANTED);
64682f5224aeSachartre 
64692f5224aeSachartre 			mutex_exit(&vdc->lock);
64702f5224aeSachartre 
64712f5224aeSachartre 			status = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE |
64722f5224aeSachartre 			    VD_ACCESS_SET_PRESERVE, FKIOCTL);
64732f5224aeSachartre 
64742f5224aeSachartre 			mutex_enter(&vdc->lock);
64752f5224aeSachartre 
64762f5224aeSachartre 			if (status == 0) {
64772f5224aeSachartre 				DMSG(vdc, 0, "[%d] Ownership recovered",
64782f5224aeSachartre 				    vdc->instance);
64792f5224aeSachartre 				vdc->ownership |= VDC_OWNERSHIP_GRANTED;
64802f5224aeSachartre 			} else {
64812f5224aeSachartre 				DMSG(vdc, 0, "[%d] Fail to recover ownership",
64822f5224aeSachartre 				    vdc->instance);
64832f5224aeSachartre 			}
64842f5224aeSachartre 
64852f5224aeSachartre 		}
64862f5224aeSachartre 
64872f5224aeSachartre 		/*
64882f5224aeSachartre 		 * If we have the ownership then we just wait for an event
64892f5224aeSachartre 		 * to happen (LDC reset), otherwise we will retry to recover
64902f5224aeSachartre 		 * after a delay.
64912f5224aeSachartre 		 */
64922f5224aeSachartre 		if (vdc->ownership & VDC_OWNERSHIP_GRANTED)
64932f5224aeSachartre 			timeout = 0;
64942f5224aeSachartre 		else
64952f5224aeSachartre 			timeout = ddi_get_lbolt() +
64962f5224aeSachartre 			    drv_usectohz(vdc_ownership_delay);
64972f5224aeSachartre 
64982f5224aeSachartre 		/* Release the ownership_lock and wait on the vdc lock */
64992f5224aeSachartre 		mutex_exit(&vdc->ownership_lock);
65002f5224aeSachartre 
65012f5224aeSachartre 		if (timeout == 0)
65022f5224aeSachartre 			(void) cv_wait(&vdc->ownership_cv, &vdc->lock);
65032f5224aeSachartre 		else
65042f5224aeSachartre 			(void) cv_timedwait(&vdc->ownership_cv,
65052f5224aeSachartre 			    &vdc->lock, timeout);
65062f5224aeSachartre 
65072f5224aeSachartre 		mutex_exit(&vdc->lock);
65082f5224aeSachartre 
65092f5224aeSachartre 		mutex_enter(&vdc->ownership_lock);
65102f5224aeSachartre 		mutex_enter(&vdc->lock);
65112f5224aeSachartre 	}
65122f5224aeSachartre 
65132f5224aeSachartre 	vdc->ownership_thread = NULL;
65142f5224aeSachartre 	mutex_exit(&vdc->lock);
65152f5224aeSachartre 	mutex_exit(&vdc->ownership_lock);
65162f5224aeSachartre 
65172f5224aeSachartre 	thread_exit();
65182f5224aeSachartre }
65192f5224aeSachartre 
65202f5224aeSachartre static void
65212f5224aeSachartre vdc_ownership_update(vdc_t *vdc, int ownership_flags)
65222f5224aeSachartre {
65232f5224aeSachartre 	ASSERT(MUTEX_HELD(&vdc->ownership_lock));
65242f5224aeSachartre 
65252f5224aeSachartre 	mutex_enter(&vdc->lock);
65262f5224aeSachartre 	vdc->ownership = ownership_flags;
65272f5224aeSachartre 	if ((vdc->ownership & VDC_OWNERSHIP_WANTED) &&
65282f5224aeSachartre 	    vdc->ownership_thread == NULL) {
65292f5224aeSachartre 		/* start ownership thread */
65302f5224aeSachartre 		vdc->ownership_thread = thread_create(NULL, 0,
65312f5224aeSachartre 		    vdc_ownership_thread, vdc, 0, &p0, TS_RUN,
65322f5224aeSachartre 		    v.v_maxsyspri - 2);
65332f5224aeSachartre 	} else {
65342f5224aeSachartre 		/* notify the ownership thread */
65352f5224aeSachartre 		cv_signal(&vdc->ownership_cv);
65362f5224aeSachartre 	}
65372f5224aeSachartre 	mutex_exit(&vdc->lock);
65382f5224aeSachartre }
65392f5224aeSachartre 
65402f5224aeSachartre /*
65412f5224aeSachartre  * Get the size and the block size of a virtual disk from the vdisk server.
65422f5224aeSachartre  * We need to use this operation when the vdisk_size attribute was not
65432f5224aeSachartre  * available during the handshake with the vdisk server.
65442f5224aeSachartre  */
65452f5224aeSachartre static int
65462f5224aeSachartre vdc_check_capacity(vdc_t *vdc)
65472f5224aeSachartre {
65482f5224aeSachartre 	int rv = 0;
65492f5224aeSachartre 	size_t alloc_len;
65502f5224aeSachartre 	vd_capacity_t *vd_cap;
65512f5224aeSachartre 
65522f5224aeSachartre 	if (vdc->vdisk_size != 0)
65532f5224aeSachartre 		return (0);
65542f5224aeSachartre 
65552f5224aeSachartre 	alloc_len = P2ROUNDUP(sizeof (vd_capacity_t), sizeof (uint64_t));
65562f5224aeSachartre 
65572f5224aeSachartre 	vd_cap = kmem_zalloc(alloc_len, KM_SLEEP);
65582f5224aeSachartre 
65592f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_GET_CAPACITY, (caddr_t)vd_cap, alloc_len,
65602f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)FKIOCTL, VIO_both_dir, B_TRUE);
65612f5224aeSachartre 
65622f5224aeSachartre 	if (rv == 0) {
65632f5224aeSachartre 		if (vd_cap->vdisk_block_size != vdc->block_size ||
65642f5224aeSachartre 		    vd_cap->vdisk_size == VD_SIZE_UNKNOWN ||
65652f5224aeSachartre 		    vd_cap->vdisk_size == 0)
65662f5224aeSachartre 			rv = EINVAL;
65672f5224aeSachartre 		else
65682f5224aeSachartre 			vdc->vdisk_size = vd_cap->vdisk_size;
65692f5224aeSachartre 	}
65702f5224aeSachartre 
65712f5224aeSachartre 	kmem_free(vd_cap, alloc_len);
65722f5224aeSachartre 	return (rv);
65732f5224aeSachartre }
65742f5224aeSachartre 
65752f5224aeSachartre /*
65761ae08745Sheppo  * This structure is used in the DKIO(7I) array below.
65771ae08745Sheppo  */
65781ae08745Sheppo typedef struct vdc_dk_ioctl {
65791ae08745Sheppo 	uint8_t		op;		/* VD_OP_XXX value */
65801ae08745Sheppo 	int		cmd;		/* Solaris ioctl operation number */
65811ae08745Sheppo 	size_t		nbytes;		/* size of structure to be copied */
65820a55fbb7Slm66018 
65830a55fbb7Slm66018 	/* function to convert between vDisk and Solaris structure formats */
6584d10e4ef2Snarayan 	int	(*convert)(vdc_t *vdc, void *vd_buf, void *ioctl_arg,
6585d10e4ef2Snarayan 	    int mode, int dir);
65861ae08745Sheppo } vdc_dk_ioctl_t;
65871ae08745Sheppo 
65881ae08745Sheppo /*
65891ae08745Sheppo  * Subset of DKIO(7I) operations currently supported
65901ae08745Sheppo  */
65911ae08745Sheppo static vdc_dk_ioctl_t	dk_ioctl[] = {
6592eff7243fSlm66018 	{VD_OP_FLUSH,		DKIOCFLUSHWRITECACHE,	0,
65930a55fbb7Slm66018 		vdc_null_copy_func},
65940a55fbb7Slm66018 	{VD_OP_GET_WCE,		DKIOCGETWCE,		sizeof (int),
65954bac2208Snarayan 		vdc_get_wce_convert},
65960a55fbb7Slm66018 	{VD_OP_SET_WCE,		DKIOCSETWCE,		sizeof (int),
65974bac2208Snarayan 		vdc_set_wce_convert},
65980a55fbb7Slm66018 	{VD_OP_GET_VTOC,	DKIOCGVTOC,		sizeof (vd_vtoc_t),
65990a55fbb7Slm66018 		vdc_get_vtoc_convert},
66000a55fbb7Slm66018 	{VD_OP_SET_VTOC,	DKIOCSVTOC,		sizeof (vd_vtoc_t),
66010a55fbb7Slm66018 		vdc_set_vtoc_convert},
66020a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM,	DKIOCGGEOM,		sizeof (vd_geom_t),
66030a55fbb7Slm66018 		vdc_get_geom_convert},
66040a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM,	DKIOCG_PHYGEOM,		sizeof (vd_geom_t),
66050a55fbb7Slm66018 		vdc_get_geom_convert},
66060a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM, 	DKIOCG_VIRTGEOM,	sizeof (vd_geom_t),
66070a55fbb7Slm66018 		vdc_get_geom_convert},
66080a55fbb7Slm66018 	{VD_OP_SET_DISKGEOM,	DKIOCSGEOM,		sizeof (vd_geom_t),
66090a55fbb7Slm66018 		vdc_set_geom_convert},
66104bac2208Snarayan 	{VD_OP_GET_EFI,		DKIOCGETEFI,		0,
66114bac2208Snarayan 		vdc_get_efi_convert},
66124bac2208Snarayan 	{VD_OP_SET_EFI,		DKIOCSETEFI,		0,
66134bac2208Snarayan 		vdc_set_efi_convert},
66140a55fbb7Slm66018 
661587a7269eSachartre 	/* DIOCTL_RWCMD is converted to a read or a write */
661687a7269eSachartre 	{0, DIOCTL_RWCMD,  sizeof (struct dadkio_rwcmd), NULL},
661787a7269eSachartre 
66182f5224aeSachartre 	/* mhd(7I) non-shared multihost disks ioctls */
66192f5224aeSachartre 	{0, MHIOCTKOWN,				0, vdc_null_copy_func},
66202f5224aeSachartre 	{0, MHIOCRELEASE,			0, vdc_null_copy_func},
66212f5224aeSachartre 	{0, MHIOCSTATUS,			0, vdc_null_copy_func},
66222f5224aeSachartre 	{0, MHIOCQRESERVE,			0, vdc_null_copy_func},
66232f5224aeSachartre 
66242f5224aeSachartre 	/* mhd(7I) shared multihost disks ioctls */
66252f5224aeSachartre 	{0, MHIOCGRP_INKEYS,			0, vdc_null_copy_func},
66262f5224aeSachartre 	{0, MHIOCGRP_INRESV,			0, vdc_null_copy_func},
66272f5224aeSachartre 	{0, MHIOCGRP_REGISTER,			0, vdc_null_copy_func},
66282f5224aeSachartre 	{0, MHIOCGRP_RESERVE, 			0, vdc_null_copy_func},
66292f5224aeSachartre 	{0, MHIOCGRP_PREEMPTANDABORT,		0, vdc_null_copy_func},
66302f5224aeSachartre 	{0, MHIOCGRP_REGISTERANDIGNOREKEY,	0, vdc_null_copy_func},
66312f5224aeSachartre 
66322f5224aeSachartre 	/* mhd(7I) failfast ioctl */
66332f5224aeSachartre 	{0, MHIOCENFAILFAST,			0, vdc_null_copy_func},
66342f5224aeSachartre 
66350a55fbb7Slm66018 	/*
66360a55fbb7Slm66018 	 * These particular ioctls are not sent to the server - vdc fakes up
66370a55fbb7Slm66018 	 * the necessary info.
66380a55fbb7Slm66018 	 */
66390a55fbb7Slm66018 	{0, DKIOCINFO, sizeof (struct dk_cinfo), vdc_null_copy_func},
66400a55fbb7Slm66018 	{0, DKIOCGMEDIAINFO, sizeof (struct dk_minfo), vdc_null_copy_func},
66410a55fbb7Slm66018 	{0, USCSICMD,	sizeof (struct uscsi_cmd), vdc_null_copy_func},
66429642afceSachartre 	{0, DKIOCPARTITION, 0, vdc_null_copy_func },
664387a7269eSachartre 	{0, DKIOCGAPART, 0, vdc_null_copy_func },
66440a55fbb7Slm66018 	{0, DKIOCREMOVABLE, 0, vdc_null_copy_func},
66450a55fbb7Slm66018 	{0, CDROMREADOFFSET, 0, vdc_null_copy_func}
66461ae08745Sheppo };
66471ae08745Sheppo 
66481ae08745Sheppo /*
6649edcc0754Sachartre  * This function handles ioctl requests from the vd_efi_alloc_and_read()
6650edcc0754Sachartre  * function and forward them to the vdisk.
66512f5224aeSachartre  */
66522f5224aeSachartre static int
6653edcc0754Sachartre vd_process_efi_ioctl(void *vdisk, int cmd, uintptr_t arg)
66542f5224aeSachartre {
6655edcc0754Sachartre 	vdc_t *vdc = (vdc_t *)vdisk;
6656edcc0754Sachartre 	dev_t dev;
66572f5224aeSachartre 	int rval;
6658edcc0754Sachartre 
6659edcc0754Sachartre 	dev = makedevice(ddi_driver_major(vdc->dip),
6660edcc0754Sachartre 	    VD_MAKE_DEV(vdc->instance, 0));
6661edcc0754Sachartre 
6662edcc0754Sachartre 	return (vd_process_ioctl(dev, cmd, (caddr_t)arg, FKIOCTL, &rval));
66632f5224aeSachartre }
66642f5224aeSachartre 
66652f5224aeSachartre /*
66661ae08745Sheppo  * Function:
66671ae08745Sheppo  *	vd_process_ioctl()
66681ae08745Sheppo  *
66691ae08745Sheppo  * Description:
66700a55fbb7Slm66018  *	This routine processes disk specific ioctl calls
66711ae08745Sheppo  *
66721ae08745Sheppo  * Arguments:
66731ae08745Sheppo  *	dev	- the device number
66741ae08745Sheppo  *	cmd	- the operation [dkio(7I)] to be processed
66751ae08745Sheppo  *	arg	- pointer to user provided structure
66761ae08745Sheppo  *		  (contains data to be set or reference parameter for get)
66771ae08745Sheppo  *	mode	- bit flag, indicating open settings, 32/64 bit type, etc
66782f5224aeSachartre  *	rvalp	- pointer to return value for calling process.
66791ae08745Sheppo  *
66801ae08745Sheppo  * Return Code:
66811ae08745Sheppo  *	0
66821ae08745Sheppo  *	EFAULT
66831ae08745Sheppo  *	ENXIO
66841ae08745Sheppo  *	EIO
66851ae08745Sheppo  *	ENOTSUP
66861ae08745Sheppo  */
66871ae08745Sheppo static int
66882f5224aeSachartre vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode, int *rvalp)
66891ae08745Sheppo {
66900d0c8d4bSnarayan 	int		instance = VDCUNIT(dev);
66911ae08745Sheppo 	vdc_t		*vdc = NULL;
66921ae08745Sheppo 	int		rv = -1;
66931ae08745Sheppo 	int		idx = 0;		/* index into dk_ioctl[] */
66941ae08745Sheppo 	size_t		len = 0;		/* #bytes to send to vds */
66951ae08745Sheppo 	size_t		alloc_len = 0;		/* #bytes to allocate mem for */
66961ae08745Sheppo 	caddr_t		mem_p = NULL;
66971ae08745Sheppo 	size_t		nioctls = (sizeof (dk_ioctl)) / (sizeof (dk_ioctl[0]));
66983af08d82Slm66018 	vdc_dk_ioctl_t	*iop;
66991ae08745Sheppo 
67001ae08745Sheppo 	vdc = ddi_get_soft_state(vdc_state, instance);
67011ae08745Sheppo 	if (vdc == NULL) {
67021ae08745Sheppo 		cmn_err(CE_NOTE, "![%d] Could not get soft state structure",
67031ae08745Sheppo 		    instance);
67041ae08745Sheppo 		return (ENXIO);
67051ae08745Sheppo 	}
67061ae08745Sheppo 
67073af08d82Slm66018 	DMSG(vdc, 0, "[%d] Processing ioctl(%x) for dev %lx : model %x\n",
67083af08d82Slm66018 	    instance, cmd, dev, ddi_model_convert_from(mode & FMODELS));
67091ae08745Sheppo 
67102f5224aeSachartre 	if (rvalp != NULL) {
67112f5224aeSachartre 		/* the return value of the ioctl is 0 by default */
67122f5224aeSachartre 		*rvalp = 0;
67132f5224aeSachartre 	}
67142f5224aeSachartre 
67151ae08745Sheppo 	/*
67161ae08745Sheppo 	 * Validate the ioctl operation to be performed.
67171ae08745Sheppo 	 *
67181ae08745Sheppo 	 * If we have looped through the array without finding a match then we
67191ae08745Sheppo 	 * don't support this ioctl.
67201ae08745Sheppo 	 */
67211ae08745Sheppo 	for (idx = 0; idx < nioctls; idx++) {
67221ae08745Sheppo 		if (cmd == dk_ioctl[idx].cmd)
67231ae08745Sheppo 			break;
67241ae08745Sheppo 	}
67251ae08745Sheppo 
67261ae08745Sheppo 	if (idx >= nioctls) {
67273af08d82Slm66018 		DMSG(vdc, 0, "[%d] Unsupported ioctl (0x%x)\n",
6728e1ebb9ecSlm66018 		    vdc->instance, cmd);
67291ae08745Sheppo 		return (ENOTSUP);
67301ae08745Sheppo 	}
67311ae08745Sheppo 
67323af08d82Slm66018 	iop = &(dk_ioctl[idx]);
67333af08d82Slm66018 
67344bac2208Snarayan 	if (cmd == DKIOCGETEFI || cmd == DKIOCSETEFI) {
67354bac2208Snarayan 		/* size is not fixed for EFI ioctls, it depends on ioctl arg */
67364bac2208Snarayan 		dk_efi_t	dk_efi;
67374bac2208Snarayan 
67384bac2208Snarayan 		rv = ddi_copyin(arg, &dk_efi, sizeof (dk_efi_t), mode);
67394bac2208Snarayan 		if (rv != 0)
67404bac2208Snarayan 			return (EFAULT);
67414bac2208Snarayan 
67424bac2208Snarayan 		len = sizeof (vd_efi_t) - 1 + dk_efi.dki_length;
67434bac2208Snarayan 	} else {
67443af08d82Slm66018 		len = iop->nbytes;
67454bac2208Snarayan 	}
67461ae08745Sheppo 
67472f5224aeSachartre 	/* check if the ioctl is applicable */
67481ae08745Sheppo 	switch (cmd) {
67491ae08745Sheppo 	case CDROMREADOFFSET:
67501ae08745Sheppo 	case DKIOCREMOVABLE:
67511ae08745Sheppo 		return (ENOTTY);
67521ae08745Sheppo 
67532f5224aeSachartre 	case USCSICMD:
67542f5224aeSachartre 	case MHIOCTKOWN:
67552f5224aeSachartre 	case MHIOCSTATUS:
67562f5224aeSachartre 	case MHIOCQRESERVE:
67572f5224aeSachartre 	case MHIOCRELEASE:
67582f5224aeSachartre 	case MHIOCGRP_INKEYS:
67592f5224aeSachartre 	case MHIOCGRP_INRESV:
67602f5224aeSachartre 	case MHIOCGRP_REGISTER:
67612f5224aeSachartre 	case MHIOCGRP_RESERVE:
67622f5224aeSachartre 	case MHIOCGRP_PREEMPTANDABORT:
67632f5224aeSachartre 	case MHIOCGRP_REGISTERANDIGNOREKEY:
67642f5224aeSachartre 	case MHIOCENFAILFAST:
67652f5224aeSachartre 		if (vdc->cinfo == NULL)
67662f5224aeSachartre 			return (ENXIO);
67672f5224aeSachartre 		if (vdc->cinfo->dki_ctype != DKC_SCSI_CCS)
67682f5224aeSachartre 			return (ENOTTY);
67692f5224aeSachartre 		break;
67702f5224aeSachartre 
67712f5224aeSachartre 	case DIOCTL_RWCMD:
67722f5224aeSachartre 		if (vdc->cinfo == NULL)
67732f5224aeSachartre 			return (ENXIO);
67742f5224aeSachartre 		if (vdc->cinfo->dki_ctype != DKC_DIRECT)
67752f5224aeSachartre 			return (ENOTTY);
67762f5224aeSachartre 		break;
67772f5224aeSachartre 
67782f5224aeSachartre 	case DKIOCINFO:
67792f5224aeSachartre 		if (vdc->cinfo == NULL)
67802f5224aeSachartre 			return (ENXIO);
67812f5224aeSachartre 		break;
67822f5224aeSachartre 
67832f5224aeSachartre 	case DKIOCGMEDIAINFO:
67842f5224aeSachartre 		if (vdc->minfo == NULL)
67852f5224aeSachartre 			return (ENXIO);
67862f5224aeSachartre 		if (vdc_check_capacity(vdc) != 0)
67872f5224aeSachartre 			/* disk capacity is not available */
67882f5224aeSachartre 			return (EIO);
67892f5224aeSachartre 		break;
67902f5224aeSachartre 	}
67912f5224aeSachartre 
67922f5224aeSachartre 	/*
67932f5224aeSachartre 	 * Deal with ioctls which require a processing different than
67942f5224aeSachartre 	 * converting ioctl arguments and sending a corresponding
67952f5224aeSachartre 	 * VD operation.
67962f5224aeSachartre 	 */
67972f5224aeSachartre 	switch (cmd) {
67982f5224aeSachartre 
67992f5224aeSachartre 	case USCSICMD:
68002f5224aeSachartre 	{
68012f5224aeSachartre 		return (vdc_uscsi_cmd(vdc, arg, mode));
68022f5224aeSachartre 	}
68032f5224aeSachartre 
68042f5224aeSachartre 	case MHIOCTKOWN:
68052f5224aeSachartre 	{
68062f5224aeSachartre 		mutex_enter(&vdc->ownership_lock);
68072f5224aeSachartre 		/*
68082f5224aeSachartre 		 * We have to set VDC_OWNERSHIP_WANTED now so that the ownership
68092f5224aeSachartre 		 * can be flagged with VDC_OWNERSHIP_RESET if the LDC is reset
68102f5224aeSachartre 		 * while we are processing the ioctl.
68112f5224aeSachartre 		 */
68122f5224aeSachartre 		vdc_ownership_update(vdc, VDC_OWNERSHIP_WANTED);
68132f5224aeSachartre 
68142f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE |
68152f5224aeSachartre 		    VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE, mode);
68162f5224aeSachartre 		if (rv == 0) {
68172f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_WANTED |
68182f5224aeSachartre 			    VDC_OWNERSHIP_GRANTED);
68192f5224aeSachartre 		} else {
68202f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
68212f5224aeSachartre 		}
68222f5224aeSachartre 		mutex_exit(&vdc->ownership_lock);
68232f5224aeSachartre 		return (rv);
68242f5224aeSachartre 	}
68252f5224aeSachartre 
68262f5224aeSachartre 	case MHIOCRELEASE:
68272f5224aeSachartre 	{
68282f5224aeSachartre 		mutex_enter(&vdc->ownership_lock);
68292f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_CLEAR, mode);
68302f5224aeSachartre 		if (rv == 0) {
68312f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
68322f5224aeSachartre 		}
68332f5224aeSachartre 		mutex_exit(&vdc->ownership_lock);
68342f5224aeSachartre 		return (rv);
68352f5224aeSachartre 	}
68362f5224aeSachartre 
68372f5224aeSachartre 	case MHIOCSTATUS:
68382f5224aeSachartre 	{
68392f5224aeSachartre 		uint64_t status;
68402f5224aeSachartre 
68412f5224aeSachartre 		rv = vdc_access_get(vdc, &status, mode);
68422f5224aeSachartre 		if (rv == 0 && rvalp != NULL)
68432f5224aeSachartre 			*rvalp = (status & VD_ACCESS_ALLOWED)? 0 : 1;
68442f5224aeSachartre 		return (rv);
68452f5224aeSachartre 	}
68462f5224aeSachartre 
68472f5224aeSachartre 	case MHIOCQRESERVE:
68482f5224aeSachartre 	{
68492f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE, mode);
68502f5224aeSachartre 		return (rv);
68512f5224aeSachartre 	}
68522f5224aeSachartre 
68532f5224aeSachartre 	case MHIOCGRP_INKEYS:
68542f5224aeSachartre 	{
68552f5224aeSachartre 		return (vdc_mhd_inkeys(vdc, arg, mode));
68562f5224aeSachartre 	}
68572f5224aeSachartre 
68582f5224aeSachartre 	case MHIOCGRP_INRESV:
68592f5224aeSachartre 	{
68602f5224aeSachartre 		return (vdc_mhd_inresv(vdc, arg, mode));
68612f5224aeSachartre 	}
68622f5224aeSachartre 
68632f5224aeSachartre 	case MHIOCGRP_REGISTER:
68642f5224aeSachartre 	{
68652f5224aeSachartre 		return (vdc_mhd_register(vdc, arg, mode));
68662f5224aeSachartre 	}
68672f5224aeSachartre 
68682f5224aeSachartre 	case MHIOCGRP_RESERVE:
68692f5224aeSachartre 	{
68702f5224aeSachartre 		return (vdc_mhd_reserve(vdc, arg, mode));
68712f5224aeSachartre 	}
68722f5224aeSachartre 
68732f5224aeSachartre 	case MHIOCGRP_PREEMPTANDABORT:
68742f5224aeSachartre 	{
68752f5224aeSachartre 		return (vdc_mhd_preemptabort(vdc, arg, mode));
68762f5224aeSachartre 	}
68772f5224aeSachartre 
68782f5224aeSachartre 	case MHIOCGRP_REGISTERANDIGNOREKEY:
68792f5224aeSachartre 	{
68802f5224aeSachartre 		return (vdc_mhd_registerignore(vdc, arg, mode));
68812f5224aeSachartre 	}
68822f5224aeSachartre 
68832f5224aeSachartre 	case MHIOCENFAILFAST:
68842f5224aeSachartre 	{
68852f5224aeSachartre 		rv = vdc_failfast(vdc, arg, mode);
68862f5224aeSachartre 		return (rv);
68872f5224aeSachartre 	}
68882f5224aeSachartre 
688987a7269eSachartre 	case DIOCTL_RWCMD:
689087a7269eSachartre 	{
689187a7269eSachartre 		return (vdc_dioctl_rwcmd(dev, arg, mode));
689287a7269eSachartre 	}
689387a7269eSachartre 
689487a7269eSachartre 	case DKIOCGAPART:
689587a7269eSachartre 	{
68969642afceSachartre 		return (vdc_dkio_gapart(vdc, arg, mode));
68979642afceSachartre 	}
68989642afceSachartre 
68999642afceSachartre 	case DKIOCPARTITION:
69009642afceSachartre 	{
69019642afceSachartre 		return (vdc_dkio_partition(vdc, arg, mode));
690287a7269eSachartre 	}
690387a7269eSachartre 
69041ae08745Sheppo 	case DKIOCINFO:
69051ae08745Sheppo 	{
69061ae08745Sheppo 		struct dk_cinfo	cinfo;
69071ae08745Sheppo 
69081ae08745Sheppo 		bcopy(vdc->cinfo, &cinfo, sizeof (struct dk_cinfo));
69090d0c8d4bSnarayan 		cinfo.dki_partition = VDCPART(dev);
69101ae08745Sheppo 
69111ae08745Sheppo 		rv = ddi_copyout(&cinfo, (void *)arg,
69121ae08745Sheppo 		    sizeof (struct dk_cinfo), mode);
69131ae08745Sheppo 		if (rv != 0)
69141ae08745Sheppo 			return (EFAULT);
69151ae08745Sheppo 
69161ae08745Sheppo 		return (0);
69171ae08745Sheppo 	}
69181ae08745Sheppo 
69191ae08745Sheppo 	case DKIOCGMEDIAINFO:
69208e6a2a04Slm66018 	{
69212f5224aeSachartre 		ASSERT(vdc->vdisk_size != 0);
69222f5224aeSachartre 		if (vdc->minfo->dki_capacity == 0)
69232f5224aeSachartre 			vdc->minfo->dki_capacity = vdc->vdisk_size;
69241ae08745Sheppo 		rv = ddi_copyout(vdc->minfo, (void *)arg,
69251ae08745Sheppo 		    sizeof (struct dk_minfo), mode);
69261ae08745Sheppo 		if (rv != 0)
69271ae08745Sheppo 			return (EFAULT);
69281ae08745Sheppo 
69291ae08745Sheppo 		return (0);
69301ae08745Sheppo 	}
69311ae08745Sheppo 
69328e6a2a04Slm66018 	case DKIOCFLUSHWRITECACHE:
69338e6a2a04Slm66018 		{
693417cadca8Slm66018 			struct dk_callback *dkc =
693517cadca8Slm66018 			    (struct dk_callback *)(uintptr_t)arg;
69368e6a2a04Slm66018 			vdc_dk_arg_t	*dkarg = NULL;
69378e6a2a04Slm66018 
69383af08d82Slm66018 			DMSG(vdc, 1, "[%d] Flush W$: mode %x\n",
69393af08d82Slm66018 			    instance, mode);
69408e6a2a04Slm66018 
69418e6a2a04Slm66018 			/*
69428e6a2a04Slm66018 			 * If arg is NULL, then there is no callback function
69438e6a2a04Slm66018 			 * registered and the call operates synchronously; we
69448e6a2a04Slm66018 			 * break and continue with the rest of the function and
69458e6a2a04Slm66018 			 * wait for vds to return (i.e. after the request to
69468e6a2a04Slm66018 			 * vds returns successfully, all writes completed prior
69478e6a2a04Slm66018 			 * to the ioctl will have been flushed from the disk
69488e6a2a04Slm66018 			 * write cache to persistent media.
69498e6a2a04Slm66018 			 *
69508e6a2a04Slm66018 			 * If a callback function is registered, we dispatch
69518e6a2a04Slm66018 			 * the request on a task queue and return immediately.
69528e6a2a04Slm66018 			 * The callback will deal with informing the calling
69538e6a2a04Slm66018 			 * thread that the flush request is completed.
69548e6a2a04Slm66018 			 */
69558e6a2a04Slm66018 			if (dkc == NULL)
69568e6a2a04Slm66018 				break;
69578e6a2a04Slm66018 
6958eff7243fSlm66018 			/*
6959eff7243fSlm66018 			 * the asynchronous callback is only supported if
6960eff7243fSlm66018 			 * invoked from within the kernel
6961eff7243fSlm66018 			 */
6962eff7243fSlm66018 			if ((mode & FKIOCTL) == 0)
6963eff7243fSlm66018 				return (ENOTSUP);
6964eff7243fSlm66018 
69658e6a2a04Slm66018 			dkarg = kmem_zalloc(sizeof (vdc_dk_arg_t), KM_SLEEP);
69668e6a2a04Slm66018 
69678e6a2a04Slm66018 			dkarg->mode = mode;
69688e6a2a04Slm66018 			dkarg->dev = dev;
69698e6a2a04Slm66018 			bcopy(dkc, &dkarg->dkc, sizeof (*dkc));
69708e6a2a04Slm66018 
69718e6a2a04Slm66018 			mutex_enter(&vdc->lock);
69728e6a2a04Slm66018 			vdc->dkio_flush_pending++;
69738e6a2a04Slm66018 			dkarg->vdc = vdc;
69748e6a2a04Slm66018 			mutex_exit(&vdc->lock);
69758e6a2a04Slm66018 
69768e6a2a04Slm66018 			/* put the request on a task queue */
69778e6a2a04Slm66018 			rv = taskq_dispatch(system_taskq, vdc_dkio_flush_cb,
69788e6a2a04Slm66018 			    (void *)dkarg, DDI_SLEEP);
69793af08d82Slm66018 			if (rv == NULL) {
69803af08d82Slm66018 				/* clean up if dispatch fails */
69813af08d82Slm66018 				mutex_enter(&vdc->lock);
69823af08d82Slm66018 				vdc->dkio_flush_pending--;
698378fcd0a1Sachartre 				mutex_exit(&vdc->lock);
69843af08d82Slm66018 				kmem_free(dkarg, sizeof (vdc_dk_arg_t));
69853af08d82Slm66018 			}
69868e6a2a04Slm66018 
69878e6a2a04Slm66018 			return (rv == NULL ? ENOMEM : 0);
69888e6a2a04Slm66018 		}
69898e6a2a04Slm66018 	}
69908e6a2a04Slm66018 
69911ae08745Sheppo 	/* catch programming error in vdc - should be a VD_OP_XXX ioctl */
69923af08d82Slm66018 	ASSERT(iop->op != 0);
69931ae08745Sheppo 
699417cadca8Slm66018 	/* check if the vDisk server handles the operation for this vDisk */
699517cadca8Slm66018 	if (VD_OP_SUPPORTED(vdc->operations, iop->op) == B_FALSE) {
699617cadca8Slm66018 		DMSG(vdc, 0, "[%d] Unsupported VD_OP operation (0x%x)\n",
699717cadca8Slm66018 		    vdc->instance, iop->op);
699817cadca8Slm66018 		return (ENOTSUP);
699917cadca8Slm66018 	}
700017cadca8Slm66018 
70011ae08745Sheppo 	/* LDC requires that the memory being mapped is 8-byte aligned */
70021ae08745Sheppo 	alloc_len = P2ROUNDUP(len, sizeof (uint64_t));
70033af08d82Slm66018 	DMSG(vdc, 1, "[%d] struct size %ld alloc %ld\n",
70043af08d82Slm66018 	    instance, len, alloc_len);
70051ae08745Sheppo 
7006eff7243fSlm66018 	if (alloc_len > 0)
70071ae08745Sheppo 		mem_p = kmem_zalloc(alloc_len, KM_SLEEP);
70081ae08745Sheppo 
70090a55fbb7Slm66018 	/*
7010eff7243fSlm66018 	 * Call the conversion function for this ioctl which, if necessary,
70110a55fbb7Slm66018 	 * converts from the Solaris format to the format ARC'ed
70120a55fbb7Slm66018 	 * as part of the vDisk protocol (FWARC 2006/195)
70130a55fbb7Slm66018 	 */
70143af08d82Slm66018 	ASSERT(iop->convert != NULL);
70153af08d82Slm66018 	rv = (iop->convert)(vdc, arg, mem_p, mode, VD_COPYIN);
70161ae08745Sheppo 	if (rv != 0) {
70173af08d82Slm66018 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
7018e1ebb9ecSlm66018 		    instance, rv, cmd);
70191ae08745Sheppo 		if (mem_p != NULL)
70201ae08745Sheppo 			kmem_free(mem_p, alloc_len);
70210a55fbb7Slm66018 		return (rv);
70221ae08745Sheppo 	}
70231ae08745Sheppo 
70241ae08745Sheppo 	/*
70251ae08745Sheppo 	 * send request to vds to service the ioctl.
70261ae08745Sheppo 	 */
70273af08d82Slm66018 	rv = vdc_do_sync_op(vdc, iop->op, mem_p, alloc_len,
70280d0c8d4bSnarayan 	    VDCPART(dev), 0, CB_SYNC, (void *)(uint64_t)mode,
70292f5224aeSachartre 	    VIO_both_dir, B_TRUE);
703078fcd0a1Sachartre 
70311ae08745Sheppo 	if (rv != 0) {
70321ae08745Sheppo 		/*
70331ae08745Sheppo 		 * This is not necessarily an error. The ioctl could
70341ae08745Sheppo 		 * be returning a value such as ENOTTY to indicate
70351ae08745Sheppo 		 * that the ioctl is not applicable.
70361ae08745Sheppo 		 */
70373af08d82Slm66018 		DMSG(vdc, 0, "[%d] vds returned %d for ioctl 0x%x\n",
7038e1ebb9ecSlm66018 		    instance, rv, cmd);
70391ae08745Sheppo 		if (mem_p != NULL)
70401ae08745Sheppo 			kmem_free(mem_p, alloc_len);
7041d10e4ef2Snarayan 
70421ae08745Sheppo 		return (rv);
70431ae08745Sheppo 	}
70441ae08745Sheppo 
70451ae08745Sheppo 	/*
70460a55fbb7Slm66018 	 * Call the conversion function (if it exists) for this ioctl
70470a55fbb7Slm66018 	 * which converts from the format ARC'ed as part of the vDisk
70480a55fbb7Slm66018 	 * protocol (FWARC 2006/195) back to a format understood by
70490a55fbb7Slm66018 	 * the rest of Solaris.
70501ae08745Sheppo 	 */
70513af08d82Slm66018 	rv = (iop->convert)(vdc, mem_p, arg, mode, VD_COPYOUT);
70520a55fbb7Slm66018 	if (rv != 0) {
70533af08d82Slm66018 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
7054e1ebb9ecSlm66018 		    instance, rv, cmd);
70551ae08745Sheppo 		if (mem_p != NULL)
70561ae08745Sheppo 			kmem_free(mem_p, alloc_len);
70570a55fbb7Slm66018 		return (rv);
70581ae08745Sheppo 	}
70591ae08745Sheppo 
70601ae08745Sheppo 	if (mem_p != NULL)
70611ae08745Sheppo 		kmem_free(mem_p, alloc_len);
70621ae08745Sheppo 
70631ae08745Sheppo 	return (rv);
70641ae08745Sheppo }
70651ae08745Sheppo 
70661ae08745Sheppo /*
70671ae08745Sheppo  * Function:
70680a55fbb7Slm66018  *
70690a55fbb7Slm66018  * Description:
70700a55fbb7Slm66018  *	This is an empty conversion function used by ioctl calls which
70710a55fbb7Slm66018  *	do not need to convert the data being passed in/out to userland
70720a55fbb7Slm66018  */
70730a55fbb7Slm66018 static int
7074d10e4ef2Snarayan vdc_null_copy_func(vdc_t *vdc, void *from, void *to, int mode, int dir)
70750a55fbb7Slm66018 {
7076d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
70770a55fbb7Slm66018 	_NOTE(ARGUNUSED(from))
70780a55fbb7Slm66018 	_NOTE(ARGUNUSED(to))
70790a55fbb7Slm66018 	_NOTE(ARGUNUSED(mode))
70800a55fbb7Slm66018 	_NOTE(ARGUNUSED(dir))
70810a55fbb7Slm66018 
70820a55fbb7Slm66018 	return (0);
70830a55fbb7Slm66018 }
70840a55fbb7Slm66018 
70854bac2208Snarayan static int
70864bac2208Snarayan vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
70874bac2208Snarayan     int mode, int dir)
70884bac2208Snarayan {
70894bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
70904bac2208Snarayan 
70914bac2208Snarayan 	if (dir == VD_COPYIN)
70924bac2208Snarayan 		return (0);		/* nothing to do */
70934bac2208Snarayan 
70944bac2208Snarayan 	if (ddi_copyout(from, to, sizeof (int), mode) != 0)
70954bac2208Snarayan 		return (EFAULT);
70964bac2208Snarayan 
70974bac2208Snarayan 	return (0);
70984bac2208Snarayan }
70994bac2208Snarayan 
71004bac2208Snarayan static int
71014bac2208Snarayan vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
71024bac2208Snarayan     int mode, int dir)
71034bac2208Snarayan {
71044bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
71054bac2208Snarayan 
71064bac2208Snarayan 	if (dir == VD_COPYOUT)
71074bac2208Snarayan 		return (0);		/* nothing to do */
71084bac2208Snarayan 
71094bac2208Snarayan 	if (ddi_copyin(from, to, sizeof (int), mode) != 0)
71104bac2208Snarayan 		return (EFAULT);
71114bac2208Snarayan 
71124bac2208Snarayan 	return (0);
71134bac2208Snarayan }
71144bac2208Snarayan 
71150a55fbb7Slm66018 /*
71160a55fbb7Slm66018  * Function:
71170a55fbb7Slm66018  *	vdc_get_vtoc_convert()
71180a55fbb7Slm66018  *
71190a55fbb7Slm66018  * Description:
7120d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCGVTOC
7121d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
7122d10e4ef2Snarayan  *
7123d10e4ef2Snarayan  *	In the struct vtoc definition, the timestamp field is marked as not
7124d10e4ef2Snarayan  *	supported so it is not part of vDisk protocol (FWARC 2006/195).
7125d10e4ef2Snarayan  *	However SVM uses that field to check it can write into the VTOC,
7126d10e4ef2Snarayan  *	so we fake up the info of that field.
71270a55fbb7Slm66018  *
71280a55fbb7Slm66018  * Arguments:
7129d10e4ef2Snarayan  *	vdc	- the vDisk client
71300a55fbb7Slm66018  *	from	- the buffer containing the data to be copied from
71310a55fbb7Slm66018  *	to	- the buffer to be copied to
71320a55fbb7Slm66018  *	mode	- flags passed to ioctl() call
71330a55fbb7Slm66018  *	dir	- the "direction" of the copy - VD_COPYIN or VD_COPYOUT
71340a55fbb7Slm66018  *
71350a55fbb7Slm66018  * Return Code:
71360a55fbb7Slm66018  *	0	- Success
71370a55fbb7Slm66018  *	ENXIO	- incorrect buffer passed in.
7138d10e4ef2Snarayan  *	EFAULT	- ddi_copyout routine encountered an error.
71390a55fbb7Slm66018  */
71400a55fbb7Slm66018 static int
7141d10e4ef2Snarayan vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
71420a55fbb7Slm66018 {
7143d10e4ef2Snarayan 	int		i;
71440a55fbb7Slm66018 	void		*tmp_mem = NULL;
71450a55fbb7Slm66018 	void		*tmp_memp;
71460a55fbb7Slm66018 	struct vtoc	vt;
71470a55fbb7Slm66018 	struct vtoc32	vt32;
71480a55fbb7Slm66018 	int		copy_len = 0;
71490a55fbb7Slm66018 	int		rv = 0;
71500a55fbb7Slm66018 
71510a55fbb7Slm66018 	if (dir != VD_COPYOUT)
71520a55fbb7Slm66018 		return (0);	/* nothing to do */
71530a55fbb7Slm66018 
71540a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
71550a55fbb7Slm66018 		return (ENXIO);
71560a55fbb7Slm66018 
71570a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
71580a55fbb7Slm66018 		copy_len = sizeof (struct vtoc32);
71590a55fbb7Slm66018 	else
71600a55fbb7Slm66018 		copy_len = sizeof (struct vtoc);
71610a55fbb7Slm66018 
71620a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
71630a55fbb7Slm66018 
71640a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)from, &vt);
7165d10e4ef2Snarayan 
7166d10e4ef2Snarayan 	/* fake the VTOC timestamp field */
7167d10e4ef2Snarayan 	for (i = 0; i < V_NUMPAR; i++) {
7168d10e4ef2Snarayan 		vt.timestamp[i] = vdc->vtoc->timestamp[i];
7169d10e4ef2Snarayan 	}
7170d10e4ef2Snarayan 
71710a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
717217cadca8Slm66018 		/* LINTED E_ASSIGN_NARROW_CONV */
71730a55fbb7Slm66018 		vtoctovtoc32(vt, vt32);
71740a55fbb7Slm66018 		tmp_memp = &vt32;
71750a55fbb7Slm66018 	} else {
71760a55fbb7Slm66018 		tmp_memp = &vt;
71770a55fbb7Slm66018 	}
71780a55fbb7Slm66018 	rv = ddi_copyout(tmp_memp, to, copy_len, mode);
71790a55fbb7Slm66018 	if (rv != 0)
71800a55fbb7Slm66018 		rv = EFAULT;
71810a55fbb7Slm66018 
71820a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
71830a55fbb7Slm66018 	return (rv);
71840a55fbb7Slm66018 }
71850a55fbb7Slm66018 
71860a55fbb7Slm66018 /*
71870a55fbb7Slm66018  * Function:
71880a55fbb7Slm66018  *	vdc_set_vtoc_convert()
71890a55fbb7Slm66018  *
71900a55fbb7Slm66018  * Description:
7191d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCSVTOC
7192d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
71930a55fbb7Slm66018  *
71940a55fbb7Slm66018  * Arguments:
7195d10e4ef2Snarayan  *	vdc	- the vDisk client
71960a55fbb7Slm66018  *	from	- Buffer with data
71970a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
71980a55fbb7Slm66018  *	mode	- flags passed to ioctl
71990a55fbb7Slm66018  *	dir	- direction of copy (in or out)
72000a55fbb7Slm66018  *
72010a55fbb7Slm66018  * Return Code:
72020a55fbb7Slm66018  *	0	- Success
72030a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
72040a55fbb7Slm66018  *	EFAULT	- ddi_copyin of data failed
72050a55fbb7Slm66018  */
72060a55fbb7Slm66018 static int
7207d10e4ef2Snarayan vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
72080a55fbb7Slm66018 {
720978fcd0a1Sachartre 	_NOTE(ARGUNUSED(vdc))
721078fcd0a1Sachartre 
72112f5224aeSachartre 	void		*tmp_mem = NULL, *uvtoc;
72120a55fbb7Slm66018 	struct vtoc	vt;
72130a55fbb7Slm66018 	struct vtoc	*vtp = &vt;
72140a55fbb7Slm66018 	vd_vtoc_t	vtvd;
72150a55fbb7Slm66018 	int		copy_len = 0;
72162f5224aeSachartre 	int		i, rv = 0;
72170a55fbb7Slm66018 
72180a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
72190a55fbb7Slm66018 		return (ENXIO);
72200a55fbb7Slm66018 
72212f5224aeSachartre 	if (dir == VD_COPYIN)
72222f5224aeSachartre 		uvtoc = from;
72232f5224aeSachartre 	else
72242f5224aeSachartre 		uvtoc = to;
72252f5224aeSachartre 
72260a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
72270a55fbb7Slm66018 		copy_len = sizeof (struct vtoc32);
72280a55fbb7Slm66018 	else
72290a55fbb7Slm66018 		copy_len = sizeof (struct vtoc);
72300a55fbb7Slm66018 
72310a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
72320a55fbb7Slm66018 
72332f5224aeSachartre 	rv = ddi_copyin(uvtoc, tmp_mem, copy_len, mode);
72340a55fbb7Slm66018 	if (rv != 0) {
72350a55fbb7Slm66018 		kmem_free(tmp_mem, copy_len);
72360a55fbb7Slm66018 		return (EFAULT);
72370a55fbb7Slm66018 	}
72380a55fbb7Slm66018 
72390a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
72400a55fbb7Slm66018 		vtoc32tovtoc((*(struct vtoc32 *)tmp_mem), vt);
72410a55fbb7Slm66018 	} else {
72420a55fbb7Slm66018 		vtp = tmp_mem;
72430a55fbb7Slm66018 	}
72440a55fbb7Slm66018 
72452f5224aeSachartre 	if (dir == VD_COPYOUT) {
72462f5224aeSachartre 		/*
72472f5224aeSachartre 		 * The disk label may have changed. Revalidate the disk
72485b98b509Sachartre 		 * geometry. This will also update the device nodes.
72492f5224aeSachartre 		 */
72502f5224aeSachartre 		vdc_validate(vdc);
72512f5224aeSachartre 
72522f5224aeSachartre 		/*
72532f5224aeSachartre 		 * We also need to keep track of the timestamp fields.
72542f5224aeSachartre 		 */
72552f5224aeSachartre 		for (i = 0; i < V_NUMPAR; i++) {
72562f5224aeSachartre 			vdc->vtoc->timestamp[i] = vtp->timestamp[i];
72572f5224aeSachartre 		}
72582f5224aeSachartre 
72592f5224aeSachartre 		return (0);
72602f5224aeSachartre 	}
72612f5224aeSachartre 
72620a55fbb7Slm66018 	VTOC2VD_VTOC(vtp, &vtvd);
72630a55fbb7Slm66018 	bcopy(&vtvd, to, sizeof (vd_vtoc_t));
72640a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
72650a55fbb7Slm66018 
72660a55fbb7Slm66018 	return (0);
72670a55fbb7Slm66018 }
72680a55fbb7Slm66018 
72690a55fbb7Slm66018 /*
72700a55fbb7Slm66018  * Function:
72710a55fbb7Slm66018  *	vdc_get_geom_convert()
72720a55fbb7Slm66018  *
72730a55fbb7Slm66018  * Description:
7274d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCGGEOM,
7275d10e4ef2Snarayan  *	DKIOCG_PHYSGEOM and DKIOG_VIRTGEOM Solaris structures to the format
7276d10e4ef2Snarayan  *	defined in FWARC 2006/195
72770a55fbb7Slm66018  *
72780a55fbb7Slm66018  * Arguments:
7279d10e4ef2Snarayan  *	vdc	- the vDisk client
72800a55fbb7Slm66018  *	from	- Buffer with data
72810a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
72820a55fbb7Slm66018  *	mode	- flags passed to ioctl
72830a55fbb7Slm66018  *	dir	- direction of copy (in or out)
72840a55fbb7Slm66018  *
72850a55fbb7Slm66018  * Return Code:
72860a55fbb7Slm66018  *	0	- Success
72870a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
7288d10e4ef2Snarayan  *	EFAULT	- ddi_copyout of data failed
72890a55fbb7Slm66018  */
72900a55fbb7Slm66018 static int
7291d10e4ef2Snarayan vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
72920a55fbb7Slm66018 {
7293d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
7294d10e4ef2Snarayan 
72950a55fbb7Slm66018 	struct dk_geom	geom;
72960a55fbb7Slm66018 	int	copy_len = sizeof (struct dk_geom);
72970a55fbb7Slm66018 	int	rv = 0;
72980a55fbb7Slm66018 
72990a55fbb7Slm66018 	if (dir != VD_COPYOUT)
73000a55fbb7Slm66018 		return (0);	/* nothing to do */
73010a55fbb7Slm66018 
73020a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
73030a55fbb7Slm66018 		return (ENXIO);
73040a55fbb7Slm66018 
73050a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)from, &geom);
73060a55fbb7Slm66018 	rv = ddi_copyout(&geom, to, copy_len, mode);
73070a55fbb7Slm66018 	if (rv != 0)
73080a55fbb7Slm66018 		rv = EFAULT;
73090a55fbb7Slm66018 
73100a55fbb7Slm66018 	return (rv);
73110a55fbb7Slm66018 }
73120a55fbb7Slm66018 
73130a55fbb7Slm66018 /*
73140a55fbb7Slm66018  * Function:
73150a55fbb7Slm66018  *	vdc_set_geom_convert()
73160a55fbb7Slm66018  *
73170a55fbb7Slm66018  * Description:
7318d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCSGEOM
7319d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
73200a55fbb7Slm66018  *
73210a55fbb7Slm66018  * Arguments:
7322d10e4ef2Snarayan  *	vdc	- the vDisk client
73230a55fbb7Slm66018  *	from	- Buffer with data
73240a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
73250a55fbb7Slm66018  *	mode	- flags passed to ioctl
73260a55fbb7Slm66018  *	dir	- direction of copy (in or out)
73270a55fbb7Slm66018  *
73280a55fbb7Slm66018  * Return Code:
73290a55fbb7Slm66018  *	0	- Success
73300a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
73310a55fbb7Slm66018  *	EFAULT	- ddi_copyin of data failed
73320a55fbb7Slm66018  */
73330a55fbb7Slm66018 static int
7334d10e4ef2Snarayan vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
73350a55fbb7Slm66018 {
7336d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
7337d10e4ef2Snarayan 
73380a55fbb7Slm66018 	vd_geom_t	vdgeom;
73390a55fbb7Slm66018 	void		*tmp_mem = NULL;
73400a55fbb7Slm66018 	int		copy_len = sizeof (struct dk_geom);
73410a55fbb7Slm66018 	int		rv = 0;
73420a55fbb7Slm66018 
73430a55fbb7Slm66018 	if (dir != VD_COPYIN)
73440a55fbb7Slm66018 		return (0);	/* nothing to do */
73450a55fbb7Slm66018 
73460a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
73470a55fbb7Slm66018 		return (ENXIO);
73480a55fbb7Slm66018 
73490a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
73500a55fbb7Slm66018 
73510a55fbb7Slm66018 	rv = ddi_copyin(from, tmp_mem, copy_len, mode);
73520a55fbb7Slm66018 	if (rv != 0) {
73530a55fbb7Slm66018 		kmem_free(tmp_mem, copy_len);
73540a55fbb7Slm66018 		return (EFAULT);
73550a55fbb7Slm66018 	}
73560a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)tmp_mem, &vdgeom);
73570a55fbb7Slm66018 	bcopy(&vdgeom, to, sizeof (vdgeom));
73580a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
73590a55fbb7Slm66018 
73600a55fbb7Slm66018 	return (0);
73610a55fbb7Slm66018 }
73620a55fbb7Slm66018 
73634bac2208Snarayan static int
73644bac2208Snarayan vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
73654bac2208Snarayan {
73664bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
73674bac2208Snarayan 
73684bac2208Snarayan 	vd_efi_t	*vd_efi;
73694bac2208Snarayan 	dk_efi_t	dk_efi;
73704bac2208Snarayan 	int		rv = 0;
73714bac2208Snarayan 	void		*uaddr;
73724bac2208Snarayan 
73734bac2208Snarayan 	if ((from == NULL) || (to == NULL))
73744bac2208Snarayan 		return (ENXIO);
73754bac2208Snarayan 
73764bac2208Snarayan 	if (dir == VD_COPYIN) {
73774bac2208Snarayan 
73784bac2208Snarayan 		vd_efi = (vd_efi_t *)to;
73794bac2208Snarayan 
73804bac2208Snarayan 		rv = ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode);
73814bac2208Snarayan 		if (rv != 0)
73824bac2208Snarayan 			return (EFAULT);
73834bac2208Snarayan 
73844bac2208Snarayan 		vd_efi->lba = dk_efi.dki_lba;
73854bac2208Snarayan 		vd_efi->length = dk_efi.dki_length;
73864bac2208Snarayan 		bzero(vd_efi->data, vd_efi->length);
73874bac2208Snarayan 
73884bac2208Snarayan 	} else {
73894bac2208Snarayan 
73904bac2208Snarayan 		rv = ddi_copyin(to, &dk_efi, sizeof (dk_efi_t), mode);
73914bac2208Snarayan 		if (rv != 0)
73924bac2208Snarayan 			return (EFAULT);
73934bac2208Snarayan 
73944bac2208Snarayan 		uaddr = dk_efi.dki_data;
73954bac2208Snarayan 
73964bac2208Snarayan 		dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
73974bac2208Snarayan 
73984bac2208Snarayan 		VD_EFI2DK_EFI((vd_efi_t *)from, &dk_efi);
73994bac2208Snarayan 
74004bac2208Snarayan 		rv = ddi_copyout(dk_efi.dki_data, uaddr, dk_efi.dki_length,
74014bac2208Snarayan 		    mode);
74024bac2208Snarayan 		if (rv != 0)
74034bac2208Snarayan 			return (EFAULT);
74044bac2208Snarayan 
74054bac2208Snarayan 		kmem_free(dk_efi.dki_data, dk_efi.dki_length);
74064bac2208Snarayan 	}
74074bac2208Snarayan 
74084bac2208Snarayan 	return (0);
74094bac2208Snarayan }
74104bac2208Snarayan 
74114bac2208Snarayan static int
74124bac2208Snarayan vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
74134bac2208Snarayan {
74144bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
74154bac2208Snarayan 
74164bac2208Snarayan 	dk_efi_t	dk_efi;
74174bac2208Snarayan 	void		*uaddr;
74184bac2208Snarayan 
74192f5224aeSachartre 	if (dir == VD_COPYOUT) {
74202f5224aeSachartre 		/*
74212f5224aeSachartre 		 * The disk label may have changed. Revalidate the disk
74225b98b509Sachartre 		 * geometry. This will also update the device nodes.
74232f5224aeSachartre 		 */
74242f5224aeSachartre 		vdc_validate(vdc);
74252f5224aeSachartre 		return (0);
74262f5224aeSachartre 	}
74274bac2208Snarayan 
74284bac2208Snarayan 	if ((from == NULL) || (to == NULL))
74294bac2208Snarayan 		return (ENXIO);
74304bac2208Snarayan 
74314bac2208Snarayan 	if (ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode) != 0)
74324bac2208Snarayan 		return (EFAULT);
74334bac2208Snarayan 
74344bac2208Snarayan 	uaddr = dk_efi.dki_data;
74354bac2208Snarayan 
74364bac2208Snarayan 	dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
74374bac2208Snarayan 
74384bac2208Snarayan 	if (ddi_copyin(uaddr, dk_efi.dki_data, dk_efi.dki_length, mode) != 0)
74394bac2208Snarayan 		return (EFAULT);
74404bac2208Snarayan 
74414bac2208Snarayan 	DK_EFI2VD_EFI(&dk_efi, (vd_efi_t *)to);
74424bac2208Snarayan 
74434bac2208Snarayan 	kmem_free(dk_efi.dki_data, dk_efi.dki_length);
74444bac2208Snarayan 
74454bac2208Snarayan 	return (0);
74464bac2208Snarayan }
74474bac2208Snarayan 
744817cadca8Slm66018 
744917cadca8Slm66018 /* -------------------------------------------------------------------------- */
745017cadca8Slm66018 
74510a55fbb7Slm66018 /*
74520a55fbb7Slm66018  * Function:
74531ae08745Sheppo  *	vdc_create_fake_geometry()
74541ae08745Sheppo  *
74551ae08745Sheppo  * Description:
745617cadca8Slm66018  *	This routine fakes up the disk info needed for some DKIO ioctls such
745717cadca8Slm66018  *	as DKIOCINFO and DKIOCGMEDIAINFO [just like lofi(7D) and ramdisk(7D) do]
74581ae08745Sheppo  *
745917cadca8Slm66018  *	Note: This function must not be called until the vDisk attributes have
746017cadca8Slm66018  *	been exchanged as part of the handshake with the vDisk server.
74611ae08745Sheppo  *
74621ae08745Sheppo  * Arguments:
74631ae08745Sheppo  *	vdc	- soft state pointer for this instance of the device driver.
74641ae08745Sheppo  *
74651ae08745Sheppo  * Return Code:
746678fcd0a1Sachartre  *	none.
74671ae08745Sheppo  */
746878fcd0a1Sachartre static void
74691ae08745Sheppo vdc_create_fake_geometry(vdc_t *vdc)
74701ae08745Sheppo {
74711ae08745Sheppo 	ASSERT(vdc != NULL);
747278fcd0a1Sachartre 	ASSERT(vdc->max_xfer_sz != 0);
74730d0c8d4bSnarayan 
74740d0c8d4bSnarayan 	/*
74751ae08745Sheppo 	 * DKIOCINFO support
74761ae08745Sheppo 	 */
747778fcd0a1Sachartre 	if (vdc->cinfo == NULL)
74781ae08745Sheppo 		vdc->cinfo = kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
74791ae08745Sheppo 
74801ae08745Sheppo 	(void) strcpy(vdc->cinfo->dki_cname, VDC_DRIVER_NAME);
74811ae08745Sheppo 	(void) strcpy(vdc->cinfo->dki_dname, VDC_DRIVER_NAME);
74828e6a2a04Slm66018 	/* max_xfer_sz is #blocks so we don't need to divide by DEV_BSIZE */
74838e6a2a04Slm66018 	vdc->cinfo->dki_maxtransfer = vdc->max_xfer_sz;
74842f5224aeSachartre 
748587a7269eSachartre 	/*
74862f5224aeSachartre 	 * We set the controller type to DKC_SCSI_CCS only if the VD_OP_SCSICMD
74872f5224aeSachartre 	 * operation is supported, otherwise the controller type is DKC_DIRECT.
74882f5224aeSachartre 	 * Version 1.0 does not support the VD_OP_SCSICMD operation, so the
74892f5224aeSachartre 	 * controller type is always DKC_DIRECT in that case.
74902f5224aeSachartre 	 *
749117cadca8Slm66018 	 * If the virtual disk is backed by a physical CD/DVD device or
749217cadca8Slm66018 	 * an ISO image, modify the controller type to indicate this
749387a7269eSachartre 	 */
749417cadca8Slm66018 	switch (vdc->vdisk_media) {
749517cadca8Slm66018 	case VD_MEDIA_CD:
749617cadca8Slm66018 	case VD_MEDIA_DVD:
749717cadca8Slm66018 		vdc->cinfo->dki_ctype = DKC_CDROM;
749817cadca8Slm66018 		break;
749917cadca8Slm66018 	case VD_MEDIA_FIXED:
75002f5224aeSachartre 		if (VD_OP_SUPPORTED(vdc->operations, VD_OP_SCSICMD))
75012f5224aeSachartre 			vdc->cinfo->dki_ctype = DKC_SCSI_CCS;
75022f5224aeSachartre 		else
750387a7269eSachartre 			vdc->cinfo->dki_ctype = DKC_DIRECT;
750417cadca8Slm66018 		break;
750517cadca8Slm66018 	default:
750617cadca8Slm66018 		/* in the case of v1.0 we default to a fixed disk */
750717cadca8Slm66018 		vdc->cinfo->dki_ctype = DKC_DIRECT;
750817cadca8Slm66018 		break;
750917cadca8Slm66018 	}
75101ae08745Sheppo 	vdc->cinfo->dki_flags = DKI_FMTVOL;
75111ae08745Sheppo 	vdc->cinfo->dki_cnum = 0;
75121ae08745Sheppo 	vdc->cinfo->dki_addr = 0;
75131ae08745Sheppo 	vdc->cinfo->dki_space = 0;
75141ae08745Sheppo 	vdc->cinfo->dki_prio = 0;
75151ae08745Sheppo 	vdc->cinfo->dki_vec = 0;
75161ae08745Sheppo 	vdc->cinfo->dki_unit = vdc->instance;
75171ae08745Sheppo 	vdc->cinfo->dki_slave = 0;
75181ae08745Sheppo 	/*
75191ae08745Sheppo 	 * The partition number will be created on the fly depending on the
75201ae08745Sheppo 	 * actual slice (i.e. minor node) that is used to request the data.
75211ae08745Sheppo 	 */
75221ae08745Sheppo 	vdc->cinfo->dki_partition = 0;
75231ae08745Sheppo 
75241ae08745Sheppo 	/*
75251ae08745Sheppo 	 * DKIOCGMEDIAINFO support
75261ae08745Sheppo 	 */
75270a55fbb7Slm66018 	if (vdc->minfo == NULL)
75281ae08745Sheppo 		vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
752917cadca8Slm66018 
753017cadca8Slm66018 	if (vio_ver_is_supported(vdc->ver, 1, 1)) {
753117cadca8Slm66018 		vdc->minfo->dki_media_type =
753217cadca8Slm66018 		    VD_MEDIATYPE2DK_MEDIATYPE(vdc->vdisk_media);
753317cadca8Slm66018 	} else {
75341ae08745Sheppo 		vdc->minfo->dki_media_type = DK_FIXED_DISK;
753517cadca8Slm66018 	}
753617cadca8Slm66018 
75374bac2208Snarayan 	vdc->minfo->dki_capacity = vdc->vdisk_size;
753817cadca8Slm66018 	vdc->minfo->dki_lbsize = vdc->block_size;
753978fcd0a1Sachartre }
75401ae08745Sheppo 
754178fcd0a1Sachartre static ushort_t
754278fcd0a1Sachartre vdc_lbl2cksum(struct dk_label *label)
754378fcd0a1Sachartre {
754478fcd0a1Sachartre 	int	count;
754578fcd0a1Sachartre 	ushort_t sum, *sp;
754678fcd0a1Sachartre 
754778fcd0a1Sachartre 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
754878fcd0a1Sachartre 	sp = (ushort_t *)label;
754978fcd0a1Sachartre 	sum = 0;
755078fcd0a1Sachartre 	while (count--) {
755178fcd0a1Sachartre 		sum ^= *sp++;
755278fcd0a1Sachartre 	}
755378fcd0a1Sachartre 
755478fcd0a1Sachartre 	return (sum);
75550a55fbb7Slm66018 }
75560a55fbb7Slm66018 
75570a55fbb7Slm66018 /*
75580a55fbb7Slm66018  * Function:
755978fcd0a1Sachartre  *	vdc_validate_geometry
75600a55fbb7Slm66018  *
75610a55fbb7Slm66018  * Description:
756278fcd0a1Sachartre  *	This routine discovers the label and geometry of the disk. It stores
756378fcd0a1Sachartre  *	the disk label and related information in the vdc structure. If it
756478fcd0a1Sachartre  *	fails to validate the geometry or to discover the disk label then
756578fcd0a1Sachartre  *	the label is marked as unknown (VD_DISK_LABEL_UNK).
75660a55fbb7Slm66018  *
75670a55fbb7Slm66018  * Arguments:
75680a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
75690a55fbb7Slm66018  *
75700a55fbb7Slm66018  * Return Code:
757178fcd0a1Sachartre  *	0	- success.
757278fcd0a1Sachartre  *	EINVAL	- unknown disk label.
757378fcd0a1Sachartre  *	ENOTSUP	- geometry not applicable (EFI label).
757478fcd0a1Sachartre  *	EIO	- error accessing the disk.
75750a55fbb7Slm66018  */
75760a55fbb7Slm66018 static int
757778fcd0a1Sachartre vdc_validate_geometry(vdc_t *vdc)
75780a55fbb7Slm66018 {
7579d10e4ef2Snarayan 	buf_t	*buf;	/* BREAD requests need to be in a buf_t structure */
75800a55fbb7Slm66018 	dev_t	dev;
75812f5224aeSachartre 	int	rv, rval;
758278fcd0a1Sachartre 	struct dk_label label;
758378fcd0a1Sachartre 	struct dk_geom geom;
758478fcd0a1Sachartre 	struct vtoc vtoc;
7585edcc0754Sachartre 	efi_gpt_t *gpt;
7586edcc0754Sachartre 	efi_gpe_t *gpe;
7587edcc0754Sachartre 	vd_efi_dev_t edev;
75880a55fbb7Slm66018 
75890a55fbb7Slm66018 	ASSERT(vdc != NULL);
759078fcd0a1Sachartre 	ASSERT(vdc->vtoc != NULL && vdc->geom != NULL);
759178fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
75920a55fbb7Slm66018 
759378fcd0a1Sachartre 	mutex_exit(&vdc->lock);
75940a55fbb7Slm66018 
75950a55fbb7Slm66018 	dev = makedevice(ddi_driver_major(vdc->dip),
75960a55fbb7Slm66018 	    VD_MAKE_DEV(vdc->instance, 0));
75974bac2208Snarayan 
75982f5224aeSachartre 	rv = vd_process_ioctl(dev, DKIOCGGEOM, (caddr_t)&geom, FKIOCTL, &rval);
759978fcd0a1Sachartre 	if (rv == 0)
76002f5224aeSachartre 		rv = vd_process_ioctl(dev, DKIOCGVTOC, (caddr_t)&vtoc,
76012f5224aeSachartre 		    FKIOCTL, &rval);
76020d0c8d4bSnarayan 
76034bac2208Snarayan 	if (rv == ENOTSUP) {
76044bac2208Snarayan 		/*
76054bac2208Snarayan 		 * If the device does not support VTOC then we try
76064bac2208Snarayan 		 * to read an EFI label.
7607edcc0754Sachartre 		 *
7608edcc0754Sachartre 		 * We need to know the block size and the disk size to
7609edcc0754Sachartre 		 * be able to read an EFI label.
76104bac2208Snarayan 		 */
7611edcc0754Sachartre 		if (vdc->vdisk_size == 0) {
7612edcc0754Sachartre 			if ((rv = vdc_check_capacity(vdc)) != 0) {
7613edcc0754Sachartre 				mutex_enter(&vdc->lock);
7614edcc0754Sachartre 				vdc_store_label_unk(vdc);
7615edcc0754Sachartre 				return (rv);
7616edcc0754Sachartre 			}
7617edcc0754Sachartre 		}
76184bac2208Snarayan 
7619edcc0754Sachartre 		VD_EFI_DEV_SET(edev, vdc, vd_process_efi_ioctl);
7620edcc0754Sachartre 
7621edcc0754Sachartre 		rv = vd_efi_alloc_and_read(&edev, &gpt, &gpe);
76224bac2208Snarayan 
76234bac2208Snarayan 		if (rv) {
76243af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to get EFI (err=%d)",
76254bac2208Snarayan 			    vdc->instance, rv);
762678fcd0a1Sachartre 			mutex_enter(&vdc->lock);
762778fcd0a1Sachartre 			vdc_store_label_unk(vdc);
762878fcd0a1Sachartre 			return (EIO);
762978fcd0a1Sachartre 		}
763078fcd0a1Sachartre 
763178fcd0a1Sachartre 		mutex_enter(&vdc->lock);
7632edcc0754Sachartre 		vdc_store_label_efi(vdc, gpt, gpe);
7633edcc0754Sachartre 		vd_efi_free(&edev, gpt, gpe);
763478fcd0a1Sachartre 		return (ENOTSUP);
763578fcd0a1Sachartre 	}
763678fcd0a1Sachartre 
763778fcd0a1Sachartre 	if (rv != 0) {
763878fcd0a1Sachartre 		DMSG(vdc, 0, "[%d] Failed to get VTOC (err=%d)",
763978fcd0a1Sachartre 		    vdc->instance, rv);
764078fcd0a1Sachartre 		mutex_enter(&vdc->lock);
764178fcd0a1Sachartre 		vdc_store_label_unk(vdc);
764278fcd0a1Sachartre 		if (rv != EINVAL)
764378fcd0a1Sachartre 			rv = EIO;
76444bac2208Snarayan 		return (rv);
76454bac2208Snarayan 	}
76464bac2208Snarayan 
764778fcd0a1Sachartre 	/* check that geometry and vtoc are valid */
764878fcd0a1Sachartre 	if (geom.dkg_nhead == 0 || geom.dkg_nsect == 0 ||
764978fcd0a1Sachartre 	    vtoc.v_sanity != VTOC_SANE) {
765078fcd0a1Sachartre 		mutex_enter(&vdc->lock);
765178fcd0a1Sachartre 		vdc_store_label_unk(vdc);
765278fcd0a1Sachartre 		return (EINVAL);
765378fcd0a1Sachartre 	}
76544bac2208Snarayan 
765578fcd0a1Sachartre 	/*
765678fcd0a1Sachartre 	 * We have a disk and a valid VTOC. However this does not mean
765778fcd0a1Sachartre 	 * that the disk currently have a VTOC label. The returned VTOC may
765878fcd0a1Sachartre 	 * be a default VTOC to be used for configuring the disk (this is
765978fcd0a1Sachartre 	 * what is done for disk image). So we read the label from the
766078fcd0a1Sachartre 	 * beginning of the disk to ensure we really have a VTOC label.
766178fcd0a1Sachartre 	 *
766278fcd0a1Sachartre 	 * FUTURE: This could be the default way for reading the VTOC
766378fcd0a1Sachartre 	 * from the disk as opposed to sending the VD_OP_GET_VTOC
766478fcd0a1Sachartre 	 * to the server. This will be the default if vdc is implemented
766578fcd0a1Sachartre 	 * ontop of cmlb.
766678fcd0a1Sachartre 	 */
766778fcd0a1Sachartre 
766878fcd0a1Sachartre 	/*
766978fcd0a1Sachartre 	 * Single slice disk does not support read using an absolute disk
767078fcd0a1Sachartre 	 * offset so we just rely on the DKIOCGVTOC ioctl in that case.
767178fcd0a1Sachartre 	 */
767278fcd0a1Sachartre 	if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) {
767378fcd0a1Sachartre 		mutex_enter(&vdc->lock);
767478fcd0a1Sachartre 		if (vtoc.v_nparts != 1) {
767578fcd0a1Sachartre 			vdc_store_label_unk(vdc);
767678fcd0a1Sachartre 			return (EINVAL);
767778fcd0a1Sachartre 		}
767878fcd0a1Sachartre 		vdc_store_label_vtoc(vdc, &geom, &vtoc);
76794bac2208Snarayan 		return (0);
76804bac2208Snarayan 	}
76814bac2208Snarayan 
768278fcd0a1Sachartre 	if (vtoc.v_nparts != V_NUMPAR) {
768378fcd0a1Sachartre 		mutex_enter(&vdc->lock);
768478fcd0a1Sachartre 		vdc_store_label_unk(vdc);
768578fcd0a1Sachartre 		return (EINVAL);
76860a55fbb7Slm66018 	}
7687d10e4ef2Snarayan 
7688d10e4ef2Snarayan 	/*
7689d10e4ef2Snarayan 	 * Read disk label from start of disk
7690d10e4ef2Snarayan 	 */
7691d10e4ef2Snarayan 	buf = kmem_alloc(sizeof (buf_t), KM_SLEEP);
7692d10e4ef2Snarayan 	bioinit(buf);
769378fcd0a1Sachartre 	buf->b_un.b_addr = (caddr_t)&label;
7694d10e4ef2Snarayan 	buf->b_bcount = DK_LABEL_SIZE;
7695d10e4ef2Snarayan 	buf->b_flags = B_BUSY | B_READ;
769617cadca8Slm66018 	buf->b_dev = cmpdev(dev);
769778fcd0a1Sachartre 	rv = vdc_send_request(vdc, VD_OP_BREAD, (caddr_t)&label,
769878fcd0a1Sachartre 	    DK_LABEL_SIZE, VD_SLICE_NONE, 0, CB_STRATEGY, buf, VIO_read_dir);
76993af08d82Slm66018 	if (rv) {
77003af08d82Slm66018 		DMSG(vdc, 1, "[%d] Failed to read disk block 0\n",
77013af08d82Slm66018 		    vdc->instance);
770278fcd0a1Sachartre 	} else {
7703d10e4ef2Snarayan 		rv = biowait(buf);
7704d10e4ef2Snarayan 		biofini(buf);
770578fcd0a1Sachartre 	}
7706d10e4ef2Snarayan 	kmem_free(buf, sizeof (buf_t));
77070a55fbb7Slm66018 
770878fcd0a1Sachartre 	if (rv != 0 || label.dkl_magic != DKL_MAGIC ||
770978fcd0a1Sachartre 	    label.dkl_cksum != vdc_lbl2cksum(&label)) {
771078fcd0a1Sachartre 		DMSG(vdc, 1, "[%d] Got VTOC with invalid label\n",
771178fcd0a1Sachartre 		    vdc->instance);
771278fcd0a1Sachartre 		mutex_enter(&vdc->lock);
771378fcd0a1Sachartre 		vdc_store_label_unk(vdc);
771478fcd0a1Sachartre 		return (EINVAL);
771578fcd0a1Sachartre 	}
771678fcd0a1Sachartre 
771778fcd0a1Sachartre 	mutex_enter(&vdc->lock);
771878fcd0a1Sachartre 	vdc_store_label_vtoc(vdc, &geom, &vtoc);
771978fcd0a1Sachartre 	return (0);
772078fcd0a1Sachartre }
772178fcd0a1Sachartre 
772278fcd0a1Sachartre /*
772378fcd0a1Sachartre  * Function:
772478fcd0a1Sachartre  *	vdc_validate
772578fcd0a1Sachartre  *
772678fcd0a1Sachartre  * Description:
772778fcd0a1Sachartre  *	This routine discovers the label of the disk and create the
772878fcd0a1Sachartre  *	appropriate device nodes if the label has changed.
772978fcd0a1Sachartre  *
773078fcd0a1Sachartre  * Arguments:
773178fcd0a1Sachartre  *	vdc	- soft state pointer for this instance of the device driver.
773278fcd0a1Sachartre  *
773378fcd0a1Sachartre  * Return Code:
773478fcd0a1Sachartre  *	none.
773578fcd0a1Sachartre  */
773678fcd0a1Sachartre static void
773778fcd0a1Sachartre vdc_validate(vdc_t *vdc)
773878fcd0a1Sachartre {
773978fcd0a1Sachartre 	vd_disk_label_t old_label;
7740edcc0754Sachartre 	vd_slice_t old_slice[V_NUMPAR];
774178fcd0a1Sachartre 	int rv;
774278fcd0a1Sachartre 
774378fcd0a1Sachartre 	ASSERT(!MUTEX_HELD(&vdc->lock));
774478fcd0a1Sachartre 
774578fcd0a1Sachartre 	mutex_enter(&vdc->lock);
774678fcd0a1Sachartre 
774778fcd0a1Sachartre 	/* save the current label and vtoc */
774878fcd0a1Sachartre 	old_label = vdc->vdisk_label;
7749edcc0754Sachartre 	bcopy(vdc->slice, &old_slice, sizeof (vd_slice_t) * V_NUMPAR);
775078fcd0a1Sachartre 
775178fcd0a1Sachartre 	/* check the geometry */
775278fcd0a1Sachartre 	(void) vdc_validate_geometry(vdc);
775378fcd0a1Sachartre 
775478fcd0a1Sachartre 	/* if the disk label has changed, update device nodes */
775578fcd0a1Sachartre 	if (vdc->vdisk_label != old_label) {
775678fcd0a1Sachartre 
775778fcd0a1Sachartre 		if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
775878fcd0a1Sachartre 			rv = vdc_create_device_nodes_efi(vdc);
775978fcd0a1Sachartre 		else
776078fcd0a1Sachartre 			rv = vdc_create_device_nodes_vtoc(vdc);
776178fcd0a1Sachartre 
776278fcd0a1Sachartre 		if (rv != 0) {
776378fcd0a1Sachartre 			DMSG(vdc, 0, "![%d] Failed to update device nodes",
776478fcd0a1Sachartre 			    vdc->instance);
776578fcd0a1Sachartre 		}
776678fcd0a1Sachartre 	}
776778fcd0a1Sachartre 
776878fcd0a1Sachartre 	mutex_exit(&vdc->lock);
776978fcd0a1Sachartre }
777078fcd0a1Sachartre 
777178fcd0a1Sachartre static void
777278fcd0a1Sachartre vdc_validate_task(void *arg)
777378fcd0a1Sachartre {
777478fcd0a1Sachartre 	vdc_t *vdc = (vdc_t *)arg;
777578fcd0a1Sachartre 
777678fcd0a1Sachartre 	vdc_validate(vdc);
777778fcd0a1Sachartre 
777878fcd0a1Sachartre 	mutex_enter(&vdc->lock);
777978fcd0a1Sachartre 	ASSERT(vdc->validate_pending > 0);
778078fcd0a1Sachartre 	vdc->validate_pending--;
778178fcd0a1Sachartre 	mutex_exit(&vdc->lock);
77821ae08745Sheppo }
77834bac2208Snarayan 
77844bac2208Snarayan /*
77854bac2208Snarayan  * Function:
77864bac2208Snarayan  *	vdc_setup_devid()
77874bac2208Snarayan  *
77884bac2208Snarayan  * Description:
77894bac2208Snarayan  *	This routine discovers the devid of a vDisk. It requests the devid of
77904bac2208Snarayan  *	the underlying device from the vDisk server, builds an encapsulated
77914bac2208Snarayan  *	devid based on the retrieved devid and registers that new devid to
77924bac2208Snarayan  *	the vDisk.
77934bac2208Snarayan  *
77944bac2208Snarayan  * Arguments:
77954bac2208Snarayan  *	vdc	- soft state pointer for this instance of the device driver.
77964bac2208Snarayan  *
77974bac2208Snarayan  * Return Code:
77984bac2208Snarayan  *	0	- A devid was succesfully registered for the vDisk
77994bac2208Snarayan  */
78004bac2208Snarayan static int
78014bac2208Snarayan vdc_setup_devid(vdc_t *vdc)
78024bac2208Snarayan {
78034bac2208Snarayan 	int rv;
78044bac2208Snarayan 	vd_devid_t *vd_devid;
78054bac2208Snarayan 	size_t bufsize, bufid_len;
78064bac2208Snarayan 
78074bac2208Snarayan 	/*
78084bac2208Snarayan 	 * At first sight, we don't know the size of the devid that the
78094bac2208Snarayan 	 * server will return but this size will be encoded into the
78104bac2208Snarayan 	 * reply. So we do a first request using a default size then we
78114bac2208Snarayan 	 * check if this size was large enough. If not then we do a second
78124bac2208Snarayan 	 * request with the correct size returned by the server. Note that
78134bac2208Snarayan 	 * ldc requires size to be 8-byte aligned.
78144bac2208Snarayan 	 */
78154bac2208Snarayan 	bufsize = P2ROUNDUP(VD_DEVID_SIZE(VD_DEVID_DEFAULT_LEN),
78164bac2208Snarayan 	    sizeof (uint64_t));
78174bac2208Snarayan 	vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
78184bac2208Snarayan 	bufid_len = bufsize - sizeof (vd_efi_t) - 1;
78194bac2208Snarayan 
78203af08d82Slm66018 	rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, (caddr_t)vd_devid,
78212f5224aeSachartre 	    bufsize, 0, 0, CB_SYNC, 0, VIO_both_dir, B_TRUE);
78223af08d82Slm66018 
78233af08d82Slm66018 	DMSG(vdc, 2, "sync_op returned %d\n", rv);
78243af08d82Slm66018 
78254bac2208Snarayan 	if (rv) {
78264bac2208Snarayan 		kmem_free(vd_devid, bufsize);
78274bac2208Snarayan 		return (rv);
78284bac2208Snarayan 	}
78294bac2208Snarayan 
78304bac2208Snarayan 	if (vd_devid->length > bufid_len) {
78314bac2208Snarayan 		/*
78324bac2208Snarayan 		 * The returned devid is larger than the buffer used. Try again
78334bac2208Snarayan 		 * with a buffer with the right size.
78344bac2208Snarayan 		 */
78354bac2208Snarayan 		kmem_free(vd_devid, bufsize);
78364bac2208Snarayan 		bufsize = P2ROUNDUP(VD_DEVID_SIZE(vd_devid->length),
78374bac2208Snarayan 		    sizeof (uint64_t));
78384bac2208Snarayan 		vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
78394bac2208Snarayan 		bufid_len = bufsize - sizeof (vd_efi_t) - 1;
78404bac2208Snarayan 
78413af08d82Slm66018 		rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID,
78423af08d82Slm66018 		    (caddr_t)vd_devid, bufsize, 0, 0, CB_SYNC, 0,
78432f5224aeSachartre 		    VIO_both_dir, B_TRUE);
78443af08d82Slm66018 
78454bac2208Snarayan 		if (rv) {
78464bac2208Snarayan 			kmem_free(vd_devid, bufsize);
78474bac2208Snarayan 			return (rv);
78484bac2208Snarayan 		}
78494bac2208Snarayan 	}
78504bac2208Snarayan 
78514bac2208Snarayan 	/*
78524bac2208Snarayan 	 * The virtual disk should have the same device id as the one associated
78534bac2208Snarayan 	 * with the physical disk it is mapped on, otherwise sharing a disk
78544bac2208Snarayan 	 * between a LDom and a non-LDom may not work (for example for a shared
78554bac2208Snarayan 	 * SVM disk set).
78564bac2208Snarayan 	 *
78574bac2208Snarayan 	 * The DDI framework does not allow creating a device id with any
78584bac2208Snarayan 	 * type so we first create a device id of type DEVID_ENCAP and then
78594bac2208Snarayan 	 * we restore the orignal type of the physical device.
78604bac2208Snarayan 	 */
78614bac2208Snarayan 
78623af08d82Slm66018 	DMSG(vdc, 2, ": devid length = %d\n", vd_devid->length);
78633af08d82Slm66018 
78644bac2208Snarayan 	/* build an encapsulated devid based on the returned devid */
78654bac2208Snarayan 	if (ddi_devid_init(vdc->dip, DEVID_ENCAP, vd_devid->length,
78664bac2208Snarayan 	    vd_devid->id, &vdc->devid) != DDI_SUCCESS) {
78673af08d82Slm66018 		DMSG(vdc, 1, "[%d] Fail to created devid\n", vdc->instance);
78684bac2208Snarayan 		kmem_free(vd_devid, bufsize);
78694bac2208Snarayan 		return (1);
78704bac2208Snarayan 	}
78714bac2208Snarayan 
78724bac2208Snarayan 	DEVID_FORMTYPE((impl_devid_t *)vdc->devid, vd_devid->type);
78734bac2208Snarayan 
78744bac2208Snarayan 	ASSERT(ddi_devid_valid(vdc->devid) == DDI_SUCCESS);
78754bac2208Snarayan 
78764bac2208Snarayan 	kmem_free(vd_devid, bufsize);
78774bac2208Snarayan 
78784bac2208Snarayan 	if (ddi_devid_register(vdc->dip, vdc->devid) != DDI_SUCCESS) {
78793af08d82Slm66018 		DMSG(vdc, 1, "[%d] Fail to register devid\n", vdc->instance);
78804bac2208Snarayan 		return (1);
78814bac2208Snarayan 	}
78824bac2208Snarayan 
78834bac2208Snarayan 	return (0);
78844bac2208Snarayan }
78854bac2208Snarayan 
78864bac2208Snarayan static void
7887edcc0754Sachartre vdc_store_label_efi(vdc_t *vdc, efi_gpt_t *gpt, efi_gpe_t *gpe)
78884bac2208Snarayan {
7889edcc0754Sachartre 	int i, nparts;
78904bac2208Snarayan 
789178fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
789278fcd0a1Sachartre 
789378fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_EFI;
7894edcc0754Sachartre 	bzero(vdc->vtoc, sizeof (struct vtoc));
789578fcd0a1Sachartre 	bzero(vdc->geom, sizeof (struct dk_geom));
7896edcc0754Sachartre 	bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
7897edcc0754Sachartre 
7898edcc0754Sachartre 	nparts = gpt->efi_gpt_NumberOfPartitionEntries;
7899edcc0754Sachartre 
7900edcc0754Sachartre 	for (i = 0; i < nparts && i < VD_EFI_WD_SLICE; i++) {
7901edcc0754Sachartre 
7902edcc0754Sachartre 		if (gpe[i].efi_gpe_StartingLBA == 0 ||
7903edcc0754Sachartre 		    gpe[i].efi_gpe_EndingLBA == 0) {
7904edcc0754Sachartre 			continue;
79054bac2208Snarayan 		}
7906edcc0754Sachartre 
7907edcc0754Sachartre 		vdc->slice[i].start = gpe[i].efi_gpe_StartingLBA;
7908edcc0754Sachartre 		vdc->slice[i].nblocks = gpe[i].efi_gpe_EndingLBA -
7909edcc0754Sachartre 		    gpe[i].efi_gpe_StartingLBA + 1;
7910edcc0754Sachartre 	}
7911edcc0754Sachartre 
7912edcc0754Sachartre 	ASSERT(vdc->vdisk_size != 0);
7913edcc0754Sachartre 	vdc->slice[VD_EFI_WD_SLICE].start = 0;
7914edcc0754Sachartre 	vdc->slice[VD_EFI_WD_SLICE].nblocks = vdc->vdisk_size;
7915edcc0754Sachartre 
79164bac2208Snarayan }
791778fcd0a1Sachartre 
791878fcd0a1Sachartre static void
791978fcd0a1Sachartre vdc_store_label_vtoc(vdc_t *vdc, struct dk_geom *geom, struct vtoc *vtoc)
792078fcd0a1Sachartre {
7921edcc0754Sachartre 	int i;
7922edcc0754Sachartre 
792378fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
7924edcc0754Sachartre 	ASSERT(vdc->block_size == vtoc->v_sectorsz);
792578fcd0a1Sachartre 
792678fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_VTOC;
792778fcd0a1Sachartre 	bcopy(vtoc, vdc->vtoc, sizeof (struct vtoc));
792878fcd0a1Sachartre 	bcopy(geom, vdc->geom, sizeof (struct dk_geom));
7929edcc0754Sachartre 	bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
7930edcc0754Sachartre 
7931edcc0754Sachartre 	for (i = 0; i < vtoc->v_nparts; i++) {
7932edcc0754Sachartre 		vdc->slice[i].start = vtoc->v_part[i].p_start;
7933edcc0754Sachartre 		vdc->slice[i].nblocks = vtoc->v_part[i].p_size;
7934edcc0754Sachartre 	}
793578fcd0a1Sachartre }
793678fcd0a1Sachartre 
793778fcd0a1Sachartre static void
793878fcd0a1Sachartre vdc_store_label_unk(vdc_t *vdc)
793978fcd0a1Sachartre {
794078fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
794178fcd0a1Sachartre 
794278fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
794378fcd0a1Sachartre 	bzero(vdc->vtoc, sizeof (struct vtoc));
794478fcd0a1Sachartre 	bzero(vdc->geom, sizeof (struct dk_geom));
7945edcc0754Sachartre 	bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
794678fcd0a1Sachartre }
7947