Home | History | Annotate | Download | only in io
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 /*
     28  * LDoms virtual disk client (vdc) device driver
     29  *
     30  * This driver runs on a guest logical domain and communicates with the virtual
     31  * disk server (vds) driver running on the service domain which is exporting
     32  * virtualized "disks" to the guest logical domain.
     33  *
     34  * The driver can be divided into four sections:
     35  *
     36  * 1) generic device driver housekeeping
     37  *	_init, _fini, attach, detach, ops structures, etc.
     38  *
     39  * 2) communication channel setup
     40  *	Setup the communications link over the LDC channel that vdc uses to
     41  *	talk to the vDisk server. Initialise the descriptor ring which
     42  *	allows the LDC clients to transfer data via memory mappings.
     43  *
     44  * 3) Support exported to upper layers (filesystems, etc)
     45  *	The upper layers call into vdc via strategy(9E) and DKIO(7I)
     46  *	ioctl calls. vdc will copy the data to be written to the descriptor
     47  *	ring or maps the buffer to store the data read by the vDisk
     48  *	server into the descriptor ring. It then sends a message to the
     49  *	vDisk server requesting it to complete the operation.
     50  *
     51  * 4) Handling responses from vDisk server.
     52  *	The vDisk server will ACK some or all of the messages vdc sends to it
     53  *	(this is configured during the handshake). Upon receipt of an ACK
     54  *	vdc will check the descriptor ring and signal to the upper layer
     55  *	code waiting on the IO.
     56  */
     57 
     58 #include <sys/atomic.h>
     59 #include <sys/conf.h>
     60 #include <sys/disp.h>
     61 #include <sys/ddi.h>
     62 #include <sys/dkio.h>
     63 #include <sys/efi_partition.h>
     64 #include <sys/fcntl.h>
     65 #include <sys/file.h>
     66 #include <sys/kstat.h>
     67 #include <sys/mach_descrip.h>
     68 #include <sys/modctl.h>
     69 #include <sys/mdeg.h>
     70 #include <sys/note.h>
     71 #include <sys/open.h>
     72 #include <sys/random.h>
     73 #include <sys/sdt.h>
     74 #include <sys/stat.h>
     75 #include <sys/sunddi.h>
     76 #include <sys/types.h>
     77 #include <sys/promif.h>
     78 #include <sys/var.h>
     79 #include <sys/vtoc.h>
     80 #include <sys/archsystm.h>
     81 #include <sys/sysmacros.h>
     82 
     83 #include <sys/cdio.h>
     84 #include <sys/dktp/fdisk.h>
     85 #include <sys/dktp/dadkio.h>
     86 #include <sys/fs/dv_node.h>
     87 #include <sys/mhd.h>
     88 #include <sys/scsi/generic/sense.h>
     89 #include <sys/scsi/impl/uscsi.h>
     90 #include <sys/scsi/impl/services.h>
     91 #include <sys/scsi/targets/sddef.h>
     92 
     93 #include <sys/ldoms.h>
     94 #include <sys/ldc.h>
     95 #include <sys/vio_common.h>
     96 #include <sys/vio_mailbox.h>
     97 #include <sys/vio_util.h>
     98 #include <sys/vdsk_common.h>
     99 #include <sys/vdsk_mailbox.h>
    100 #include <sys/vdc.h>
    101 
    102 #define	VD_OLDVTOC_LIMIT	0x7fffffff
    103 
    104 /*
    105  * function prototypes
    106  */
    107 
    108 /* standard driver functions */
    109 static int	vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred);
    110 static int	vdc_close(dev_t dev, int flag, int otyp, cred_t *cred);
    111 static int	vdc_strategy(struct buf *buf);
    112 static int	vdc_print(dev_t dev, char *str);
    113 static int	vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
    114 static int	vdc_read(dev_t dev, struct uio *uio, cred_t *cred);
    115 static int	vdc_write(dev_t dev, struct uio *uio, cred_t *cred);
    116 static int	vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
    117 			cred_t *credp, int *rvalp);
    118 static int	vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred);
    119 static int	vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred);
    120 
    121 static int	vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
    122 			void *arg, void **resultp);
    123 static int	vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
    124 static int	vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
    125 static int	vdc_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
    126 		    int mod_flags, char *name, caddr_t valuep, int *lengthp);
    127 
    128 /* setup */
    129 static void	vdc_min(struct buf *bufp);
    130 static int	vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen);
    131 static int	vdc_do_ldc_init(vdc_t *vdc, vdc_server_t *srvr);
    132 static int	vdc_start_ldc_connection(vdc_t *vdc);
    133 static int	vdc_create_device_nodes(vdc_t *vdc);
    134 static int	vdc_create_device_nodes_efi(vdc_t *vdc);
    135 static int	vdc_create_device_nodes_vtoc(vdc_t *vdc);
    136 static void	vdc_create_io_kstats(vdc_t *vdc);
    137 static void	vdc_create_err_kstats(vdc_t *vdc);
    138 static void	vdc_set_err_kstats(vdc_t *vdc);
    139 static int	vdc_get_md_node(dev_info_t *dip, md_t **mdpp,
    140 		    mde_cookie_t *vd_nodep);
    141 static int	vdc_init_ports(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_nodep);
    142 static void	vdc_fini_ports(vdc_t *vdc);
    143 static void	vdc_switch_server(vdc_t *vdcp);
    144 static int	vdc_do_ldc_up(vdc_t *vdc);
    145 static void	vdc_terminate_ldc(vdc_t *vdc, vdc_server_t *srvr);
    146 static int	vdc_init_descriptor_ring(vdc_t *vdc);
    147 static void	vdc_destroy_descriptor_ring(vdc_t *vdc);
    148 static int	vdc_setup_devid(vdc_t *vdc);
    149 static void	vdc_store_label_efi(vdc_t *, efi_gpt_t *, efi_gpe_t *);
    150 static void	vdc_store_label_vtoc(vdc_t *, struct dk_geom *,
    151 		    struct extvtoc *);
    152 static void	vdc_store_label_unk(vdc_t *vdc);
    153 static boolean_t vdc_is_opened(vdc_t *vdc);
    154 static void	vdc_update_size(vdc_t *vdc, size_t, size_t, size_t);
    155 static int	vdc_update_vio_bsize(vdc_t *vdc, uint32_t);
    156 
    157 /* handshake with vds */
    158 static int		vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver);
    159 static int		vdc_ver_negotiation(vdc_t *vdcp);
    160 static int		vdc_init_attr_negotiation(vdc_t *vdc);
    161 static int		vdc_attr_negotiation(vdc_t *vdcp);
    162 static int		vdc_init_dring_negotiate(vdc_t *vdc);
    163 static int		vdc_dring_negotiation(vdc_t *vdcp);
    164 static int		vdc_send_rdx(vdc_t *vdcp);
    165 static int		vdc_rdx_exchange(vdc_t *vdcp);
    166 static boolean_t	vdc_is_supported_version(vio_ver_msg_t *ver_msg);
    167 
    168 /* processing incoming messages from vDisk server */
    169 static void	vdc_process_msg_thread(vdc_t *vdc);
    170 static int	vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp);
    171 
    172 static uint_t	vdc_handle_cb(uint64_t event, caddr_t arg);
    173 static int	vdc_process_data_msg(vdc_t *vdc, vio_msg_t *msg);
    174 static int	vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg);
    175 static int	vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg);
    176 static int	vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *msg);
    177 static int 	vdc_send_request(vdc_t *vdcp, int operation,
    178 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
    179 		    buf_t *bufp, vio_desc_direction_t dir, int flags);
    180 static int	vdc_map_to_shared_dring(vdc_t *vdcp, int idx);
    181 static int 	vdc_populate_descriptor(vdc_t *vdcp, int operation,
    182 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
    183 		    buf_t *bufp, vio_desc_direction_t dir, int flags);
    184 static int 	vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr,
    185 		    size_t nbytes, int slice, diskaddr_t offset,
    186 		    vio_desc_direction_t dir, boolean_t);
    187 static int	vdc_do_op(vdc_t *vdc, int op, caddr_t addr, size_t nbytes,
    188 		    int slice, diskaddr_t offset, struct buf *bufp,
    189 		    vio_desc_direction_t dir, int flags);
    190 
    191 static int	vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp);
    192 static int	vdc_drain_response(vdc_t *vdcp, struct buf *buf);
    193 static int	vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx);
    194 static int	vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep);
    195 static int	vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg);
    196 
    197 /* dkio */
    198 static int	vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode,
    199 		    int *rvalp);
    200 static int	vd_process_efi_ioctl(void *vdisk, int cmd, uintptr_t arg);
    201 static void	vdc_create_fake_geometry(vdc_t *vdc);
    202 static int	vdc_validate_geometry(vdc_t *vdc);
    203 static void	vdc_validate(vdc_t *vdc);
    204 static void	vdc_validate_task(void *arg);
    205 static int	vdc_null_copy_func(vdc_t *vdc, void *from, void *to,
    206 		    int mode, int dir);
    207 static int	vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
    208 		    int mode, int dir);
    209 static int	vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
    210 		    int mode, int dir);
    211 static int	vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to,
    212 		    int mode, int dir);
    213 static int	vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to,
    214 		    int mode, int dir);
    215 static int	vdc_get_extvtoc_convert(vdc_t *vdc, void *from, void *to,
    216 		    int mode, int dir);
    217 static int	vdc_set_extvtoc_convert(vdc_t *vdc, void *from, void *to,
    218 		    int mode, int dir);
    219 static int	vdc_get_geom_convert(vdc_t *vdc, void *from, void *to,
    220 		    int mode, int dir);
    221 static int	vdc_set_geom_convert(vdc_t *vdc, void *from, void *to,
    222 		    int mode, int dir);
    223 static int	vdc_get_efi_convert(vdc_t *vdc, void *from, void *to,
    224 		    int mode, int dir);
    225 static int	vdc_set_efi_convert(vdc_t *vdc, void *from, void *to,
    226 		    int mode, int dir);
    227 
    228 static void 	vdc_ownership_update(vdc_t *vdc, int ownership_flags);
    229 static int	vdc_access_set(vdc_t *vdc, uint64_t flags);
    230 static vdc_io_t	*vdc_eio_queue(vdc_t *vdc, int index);
    231 static void	vdc_eio_unqueue(vdc_t *vdc, clock_t deadline,
    232 		    boolean_t complete_io);
    233 static int	vdc_eio_check(vdc_t *vdc, int flags);
    234 static void	vdc_eio_thread(void *arg);
    235 
    236 /*
    237  * Module variables
    238  */
    239 
    240 /*
    241  * Tunable variables to control how long vdc waits before timing out on
    242  * various operations
    243  */
    244 static int	vdc_hshake_retries = 3;
    245 
    246 static int	vdc_timeout = 0; /* units: seconds */
    247 static int 	vdc_ldcup_timeout = 1; /* units: seconds */
    248 
    249 static uint64_t vdc_hz_min_ldc_delay;
    250 static uint64_t vdc_min_timeout_ldc = 1 * MILLISEC;
    251 static uint64_t vdc_hz_max_ldc_delay;
    252 static uint64_t vdc_max_timeout_ldc = 100 * MILLISEC;
    253 
    254 static uint64_t vdc_ldc_read_init_delay = 1 * MILLISEC;
    255 static uint64_t vdc_ldc_read_max_delay = 100 * MILLISEC;
    256 
    257 /* values for dumping - need to run in a tighter loop */
    258 static uint64_t	vdc_usec_timeout_dump = 100 * MILLISEC;	/* 0.1s units: ns */
    259 static int	vdc_dump_retries = 100;
    260 
    261 static uint16_t	vdc_scsi_timeout = 60;	/* 60s units: seconds  */
    262 
    263 static uint64_t vdc_ownership_delay = 6 * MICROSEC; /* 6s units: usec */
    264 
    265 /* Count of the number of vdc instances attached */
    266 static volatile uint32_t	vdc_instance_count = 0;
    267 
    268 /* Tunable to log all SCSI errors */
    269 static boolean_t vdc_scsi_log_error = B_FALSE;
    270 
    271 /* Soft state pointer */
    272 static void	*vdc_state;
    273 
    274 /*
    275  * Controlling the verbosity of the error/debug messages
    276  *
    277  * vdc_msglevel - controls level of messages
    278  * vdc_matchinst - 64-bit variable where each bit corresponds
    279  *                 to the vdc instance the vdc_msglevel applies.
    280  */
    281 int		vdc_msglevel = 0x0;
    282 uint64_t	vdc_matchinst = 0ull;
    283 
    284 /*
    285  * Supported vDisk protocol version pairs.
    286  *
    287  * The first array entry is the latest and preferred version.
    288  */
    289 static const vio_ver_t	vdc_version[] = {{1, 1}};
    290 
    291 static struct cb_ops vdc_cb_ops = {
    292 	vdc_open,	/* cb_open */
    293 	vdc_close,	/* cb_close */
    294 	vdc_strategy,	/* cb_strategy */
    295 	vdc_print,	/* cb_print */
    296 	vdc_dump,	/* cb_dump */
    297 	vdc_read,	/* cb_read */
    298 	vdc_write,	/* cb_write */
    299 	vdc_ioctl,	/* cb_ioctl */
    300 	nodev,		/* cb_devmap */
    301 	nodev,		/* cb_mmap */
    302 	nodev,		/* cb_segmap */
    303 	nochpoll,	/* cb_chpoll */
    304 	vdc_prop_op,	/* cb_prop_op */
    305 	NULL,		/* cb_str */
    306 	D_MP | D_64BIT,	/* cb_flag */
    307 	CB_REV,		/* cb_rev */
    308 	vdc_aread,	/* cb_aread */
    309 	vdc_awrite	/* cb_awrite */
    310 };
    311 
    312 static struct dev_ops vdc_ops = {
    313 	DEVO_REV,	/* devo_rev */
    314 	0,		/* devo_refcnt */
    315 	vdc_getinfo,	/* devo_getinfo */
    316 	nulldev,	/* devo_identify */
    317 	nulldev,	/* devo_probe */
    318 	vdc_attach,	/* devo_attach */
    319 	vdc_detach,	/* devo_detach */
    320 	nodev,		/* devo_reset */
    321 	&vdc_cb_ops,	/* devo_cb_ops */
    322 	NULL,		/* devo_bus_ops */
    323 	nulldev,	/* devo_power */
    324 	ddi_quiesce_not_needed,	/* devo_quiesce */
    325 };
    326 
    327 static struct modldrv modldrv = {
    328 	&mod_driverops,
    329 	"virtual disk client",
    330 	&vdc_ops,
    331 };
    332 
    333 static struct modlinkage modlinkage = {
    334 	MODREV_1,
    335 	&modldrv,
    336 	NULL
    337 };
    338 
    339 /* -------------------------------------------------------------------------- */
    340 
    341 /*
    342  * Device Driver housekeeping and setup
    343  */
    344 
    345 int
    346 _init(void)
    347 {
    348 	int	status;
    349 
    350 	if ((status = ddi_soft_state_init(&vdc_state, sizeof (vdc_t), 1)) != 0)
    351 		return (status);
    352 	if ((status = mod_install(&modlinkage)) != 0)
    353 		ddi_soft_state_fini(&vdc_state);
    354 	return (status);
    355 }
    356 
    357 int
    358 _info(struct modinfo *modinfop)
    359 {
    360 	return (mod_info(&modlinkage, modinfop));
    361 }
    362 
    363 int
    364 _fini(void)
    365 {
    366 	int	status;
    367 
    368 	if ((status = mod_remove(&modlinkage)) != 0)
    369 		return (status);
    370 	ddi_soft_state_fini(&vdc_state);
    371 	return (0);
    372 }
    373 
    374 static int
    375 vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,  void *arg, void **resultp)
    376 {
    377 	_NOTE(ARGUNUSED(dip))
    378 
    379 	int	instance = VDCUNIT((dev_t)arg);
    380 	vdc_t	*vdc = NULL;
    381 
    382 	switch (cmd) {
    383 	case DDI_INFO_DEVT2DEVINFO:
    384 		if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
    385 			*resultp = NULL;
    386 			return (DDI_FAILURE);
    387 		}
    388 		*resultp = vdc->dip;
    389 		return (DDI_SUCCESS);
    390 	case DDI_INFO_DEVT2INSTANCE:
    391 		*resultp = (void *)(uintptr_t)instance;
    392 		return (DDI_SUCCESS);
    393 	default:
    394 		*resultp = NULL;
    395 		return (DDI_FAILURE);
    396 	}
    397 }
    398 
    399 static int
    400 vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
    401 {
    402 	kt_did_t eio_tid, ownership_tid;
    403 	int	instance;
    404 	int	rv;
    405 	vdc_server_t *srvr;
    406 	vdc_t	*vdc = NULL;
    407 
    408 	switch (cmd) {
    409 	case DDI_DETACH:
    410 		/* the real work happens below */
    411 		break;
    412 	case DDI_SUSPEND:
    413 		/* nothing to do for this non-device */
    414 		return (DDI_SUCCESS);
    415 	default:
    416 		return (DDI_FAILURE);
    417 	}
    418 
    419 	ASSERT(cmd == DDI_DETACH);
    420 	instance = ddi_get_instance(dip);
    421 	DMSGX(1, "[%d] Entered\n", instance);
    422 
    423 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
    424 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
    425 		return (DDI_FAILURE);
    426 	}
    427 
    428 	if (vdc_is_opened(vdc)) {
    429 		DMSG(vdc, 0, "[%d] Cannot detach: device is open", instance);
    430 		return (DDI_FAILURE);
    431 	}
    432 
    433 	if (vdc->dkio_flush_pending) {
    434 		DMSG(vdc, 0,
    435 		    "[%d] Cannot detach: %d outstanding DKIO flushes\n",
    436 		    instance, vdc->dkio_flush_pending);
    437 		return (DDI_FAILURE);
    438 	}
    439 
    440 	if (vdc->validate_pending) {
    441 		DMSG(vdc, 0,
    442 		    "[%d] Cannot detach: %d outstanding validate request\n",
    443 		    instance, vdc->validate_pending);
    444 		return (DDI_FAILURE);
    445 	}
    446 
    447 	DMSG(vdc, 0, "[%d] proceeding...\n", instance);
    448 
    449 	/* If we took ownership, release ownership */
    450 	mutex_enter(&vdc->ownership_lock);
    451 	if (vdc->ownership & VDC_OWNERSHIP_GRANTED) {
    452 		rv = vdc_access_set(vdc, VD_ACCESS_SET_CLEAR);
    453 		if (rv == 0) {
    454 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
    455 		}
    456 	}
    457 	mutex_exit(&vdc->ownership_lock);
    458 
    459 	/* mark instance as detaching */
    460 	vdc->lifecycle	= VDC_LC_DETACHING;
    461 
    462 	/*
    463 	 * Try and disable callbacks to prevent another handshake. We have to
    464 	 * disable callbacks for all servers.
    465 	 */
    466 	for (srvr = vdc->server_list; srvr != NULL; srvr = srvr->next) {
    467 		rv = ldc_set_cb_mode(srvr->ldc_handle, LDC_CB_DISABLE);
    468 		DMSG(vdc, 0, "callback disabled (ldc=%lu, rv=%d)\n",
    469 		    srvr->ldc_id, rv);
    470 	}
    471 
    472 	if (vdc->initialized & VDC_THREAD) {
    473 		mutex_enter(&vdc->read_lock);
    474 		if ((vdc->read_state == VDC_READ_WAITING) ||
    475 		    (vdc->read_state == VDC_READ_RESET)) {
    476 			vdc->read_state = VDC_READ_RESET;
    477 			cv_signal(&vdc->read_cv);
    478 		}
    479 
    480 		mutex_exit(&vdc->read_lock);
    481 
    482 		/* wake up any thread waiting for connection to come online */
    483 		mutex_enter(&vdc->lock);
    484 		if (vdc->state == VDC_STATE_INIT_WAITING) {
    485 			DMSG(vdc, 0,
    486 			    "[%d] write reset - move to resetting state...\n",
    487 			    instance);
    488 			vdc->state = VDC_STATE_RESETTING;
    489 			cv_signal(&vdc->initwait_cv);
    490 		} else if (vdc->state == VDC_STATE_FAILED) {
    491 			vdc->io_pending = B_TRUE;
    492 			cv_signal(&vdc->io_pending_cv);
    493 		}
    494 		mutex_exit(&vdc->lock);
    495 
    496 		/* now wait until state transitions to VDC_STATE_DETACH */
    497 		thread_join(vdc->msg_proc_thr->t_did);
    498 		ASSERT(vdc->state == VDC_STATE_DETACH);
    499 		DMSG(vdc, 0, "[%d] Reset thread exit and join ..\n",
    500 		    vdc->instance);
    501 	}
    502 
    503 	mutex_enter(&vdc->lock);
    504 
    505 	if (vdc->initialized & VDC_DRING)
    506 		vdc_destroy_descriptor_ring(vdc);
    507 
    508 	vdc_fini_ports(vdc);
    509 
    510 	if (vdc->eio_thread) {
    511 		eio_tid = vdc->eio_thread->t_did;
    512 		vdc->failfast_interval = 0;
    513 		ASSERT(vdc->num_servers == 0);
    514 		cv_signal(&vdc->eio_cv);
    515 	} else {
    516 		eio_tid = 0;
    517 	}
    518 
    519 	if (vdc->ownership & VDC_OWNERSHIP_WANTED) {
    520 		ownership_tid = vdc->ownership_thread->t_did;
    521 		vdc->ownership = VDC_OWNERSHIP_NONE;
    522 		cv_signal(&vdc->ownership_cv);
    523 	} else {
    524 		ownership_tid = 0;
    525 	}
    526 
    527 	mutex_exit(&vdc->lock);
    528 
    529 	if (eio_tid != 0)
    530 		thread_join(eio_tid);
    531 
    532 	if (ownership_tid != 0)
    533 		thread_join(ownership_tid);
    534 
    535 	if (vdc->initialized & VDC_MINOR)
    536 		ddi_remove_minor_node(dip, NULL);
    537 
    538 	if (vdc->io_stats) {
    539 		kstat_delete(vdc->io_stats);
    540 		vdc->io_stats = NULL;
    541 	}
    542 
    543 	if (vdc->err_stats) {
    544 		kstat_delete(vdc->err_stats);
    545 		vdc->err_stats = NULL;
    546 	}
    547 
    548 	if (vdc->initialized & VDC_LOCKS) {
    549 		mutex_destroy(&vdc->lock);
    550 		mutex_destroy(&vdc->read_lock);
    551 		mutex_destroy(&vdc->ownership_lock);
    552 		cv_destroy(&vdc->initwait_cv);
    553 		cv_destroy(&vdc->dring_free_cv);
    554 		cv_destroy(&vdc->membind_cv);
    555 		cv_destroy(&vdc->sync_blocked_cv);
    556 		cv_destroy(&vdc->read_cv);
    557 		cv_destroy(&vdc->running_cv);
    558 		cv_destroy(&vdc->io_pending_cv);
    559 		cv_destroy(&vdc->ownership_cv);
    560 		cv_destroy(&vdc->eio_cv);
    561 	}
    562 
    563 	if (vdc->minfo)
    564 		kmem_free(vdc->minfo, sizeof (struct dk_minfo));
    565 
    566 	if (vdc->cinfo)
    567 		kmem_free(vdc->cinfo, sizeof (struct dk_cinfo));
    568 
    569 	if (vdc->vtoc)
    570 		kmem_free(vdc->vtoc, sizeof (struct extvtoc));
    571 
    572 	if (vdc->geom)
    573 		kmem_free(vdc->geom, sizeof (struct dk_geom));
    574 
    575 	if (vdc->devid) {
    576 		ddi_devid_unregister(dip);
    577 		ddi_devid_free(vdc->devid);
    578 	}
    579 
    580 	if (vdc->initialized & VDC_SOFT_STATE)
    581 		ddi_soft_state_free(vdc_state, instance);
    582 
    583 	DMSG(vdc, 0, "[%d] End %p\n", instance, (void *)vdc);
    584 
    585 	return (DDI_SUCCESS);
    586 }
    587 
    588 
    589 static int
    590 vdc_do_attach(dev_info_t *dip)
    591 {
    592 	int		instance;
    593 	vdc_t		*vdc = NULL;
    594 	int		status;
    595 	md_t		*mdp;
    596 	mde_cookie_t	vd_node;
    597 
    598 	ASSERT(dip != NULL);
    599 
    600 	instance = ddi_get_instance(dip);
    601 	if (ddi_soft_state_zalloc(vdc_state, instance) != DDI_SUCCESS) {
    602 		cmn_err(CE_NOTE, "[%d] Couldn't alloc state structure",
    603 		    instance);
    604 		return (DDI_FAILURE);
    605 	}
    606 
    607 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
    608 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
    609 		return (DDI_FAILURE);
    610 	}
    611 
    612 	/*
    613 	 * We assign the value to initialized in this case to zero out the
    614 	 * variable and then set bits in it to indicate what has been done
    615 	 */
    616 	vdc->initialized = VDC_SOFT_STATE;
    617 
    618 	vdc_hz_min_ldc_delay = drv_usectohz(vdc_min_timeout_ldc);
    619 	vdc_hz_max_ldc_delay = drv_usectohz(vdc_max_timeout_ldc);
    620 
    621 	vdc->dip	= dip;
    622 	vdc->instance	= instance;
    623 	vdc->vdisk_type	= VD_DISK_TYPE_UNK;
    624 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
    625 	vdc->state	= VDC_STATE_INIT;
    626 	vdc->lifecycle	= VDC_LC_ATTACHING;
    627 	vdc->session_id = 0;
    628 	vdc->vdisk_bsize = DEV_BSIZE;
    629 	vdc->vio_bmask = 0;
    630 	vdc->vio_bshift = 0;
    631 	vdc->max_xfer_sz = maxphys / vdc->vdisk_bsize;
    632 
    633 	/*
    634 	 * We assume, for now, that the vDisk server will export 'read'
    635 	 * operations to us at a minimum (this is needed because of checks
    636 	 * in vdc for supported operations early in the handshake process).
    637 	 * The vDisk server will return ENOTSUP if this is not the case.
    638 	 * The value will be overwritten during the attribute exchange with
    639 	 * the bitmask of operations exported by server.
    640 	 */
    641 	vdc->operations = VD_OP_MASK_READ;
    642 
    643 	vdc->vtoc = NULL;
    644 	vdc->geom = NULL;
    645 	vdc->cinfo = NULL;
    646 	vdc->minfo = NULL;
    647 
    648 	mutex_init(&vdc->lock, NULL, MUTEX_DRIVER, NULL);
    649 	cv_init(&vdc->initwait_cv, NULL, CV_DRIVER, NULL);
    650 	cv_init(&vdc->dring_free_cv, NULL, CV_DRIVER, NULL);
    651 	cv_init(&vdc->membind_cv, NULL, CV_DRIVER, NULL);
    652 	cv_init(&vdc->running_cv, NULL, CV_DRIVER, NULL);
    653 	cv_init(&vdc->io_pending_cv, NULL, CV_DRIVER, NULL);
    654 
    655 	vdc->io_pending = B_FALSE;
    656 	vdc->threads_pending = 0;
    657 	vdc->sync_op_blocked = B_FALSE;
    658 	cv_init(&vdc->sync_blocked_cv, NULL, CV_DRIVER, NULL);
    659 
    660 	mutex_init(&vdc->ownership_lock, NULL, MUTEX_DRIVER, NULL);
    661 	cv_init(&vdc->ownership_cv, NULL, CV_DRIVER, NULL);
    662 	cv_init(&vdc->eio_cv, NULL, CV_DRIVER, NULL);
    663 
    664 	/* init blocking msg read functionality */
    665 	mutex_init(&vdc->read_lock, NULL, MUTEX_DRIVER, NULL);
    666 	cv_init(&vdc->read_cv, NULL, CV_DRIVER, NULL);
    667 	vdc->read_state = VDC_READ_IDLE;
    668 
    669 	vdc->initialized |= VDC_LOCKS;
    670 
    671 	/* get device and port MD node for this disk instance */
    672 	if (vdc_get_md_node(dip, &mdp, &vd_node) != 0) {
    673 		cmn_err(CE_NOTE, "[%d] Could not get machine description node",
    674 		    instance);
    675 		return (DDI_FAILURE);
    676 	}
    677 
    678 	if (vdc_init_ports(vdc, mdp, vd_node) != 0) {
    679 		cmn_err(CE_NOTE, "[%d] Error initialising ports", instance);
    680 		return (DDI_FAILURE);
    681 	}
    682 
    683 	(void) md_fini_handle(mdp);
    684 
    685 	/* Create the kstats for saving the I/O statistics used by iostat(1M) */
    686 	vdc_create_io_kstats(vdc);
    687 	vdc_create_err_kstats(vdc);
    688 
    689 	/* Initialize remaining structures before starting the msg thread */
    690 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
    691 	vdc->vtoc = kmem_zalloc(sizeof (struct extvtoc), KM_SLEEP);
    692 	vdc->geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
    693 	vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
    694 
    695 	/* initialize the thread responsible for managing state with server */
    696 	vdc->msg_proc_thr = thread_create(NULL, 0, vdc_process_msg_thread,
    697 	    vdc, 0, &p0, TS_RUN, minclsyspri);
    698 	if (vdc->msg_proc_thr == NULL) {
    699 		cmn_err(CE_NOTE, "[%d] Failed to create msg processing thread",
    700 		    instance);
    701 		return (DDI_FAILURE);
    702 	}
    703 
    704 	/*
    705 	 * If there are multiple servers then start the eio thread.
    706 	 */
    707 	if (vdc->num_servers > 1) {
    708 		vdc->eio_thread = thread_create(NULL, 0, vdc_eio_thread, vdc, 0,
    709 		    &p0, TS_RUN, v.v_maxsyspri - 2);
    710 		if (vdc->eio_thread == NULL) {
    711 			cmn_err(CE_NOTE, "[%d] Failed to create error "
    712 			    "I/O thread", instance);
    713 			return (DDI_FAILURE);
    714 		}
    715 	}
    716 
    717 	vdc->initialized |= VDC_THREAD;
    718 
    719 	atomic_inc_32(&vdc_instance_count);
    720 
    721 	/*
    722 	 * Check the disk label. This will send requests and do the handshake.
    723 	 * We don't really care about the disk label now. What we really need is
    724 	 * the handshake do be done so that we know the type of the disk (slice
    725 	 * or full disk) and the appropriate device nodes can be created.
    726 	 */
    727 
    728 	mutex_enter(&vdc->lock);
    729 	(void) vdc_validate_geometry(vdc);
    730 	mutex_exit(&vdc->lock);
    731 
    732 	/*
    733 	 * Now that we have the device info we can create the device nodes
    734 	 */
    735 	status = vdc_create_device_nodes(vdc);
    736 	if (status) {
    737 		DMSG(vdc, 0, "[%d] Failed to create device nodes",
    738 		    instance);
    739 		goto return_status;
    740 	}
    741 
    742 	/*
    743 	 * Fill in the fields of the error statistics kstat that were not
    744 	 * available when creating the kstat
    745 	 */
    746 	vdc_set_err_kstats(vdc);
    747 
    748 	ddi_report_dev(dip);
    749 	vdc->lifecycle	= VDC_LC_ONLINE;
    750 	DMSG(vdc, 0, "[%d] Attach tasks successful\n", instance);
    751 
    752 return_status:
    753 	DMSG(vdc, 0, "[%d] Attach completed\n", instance);
    754 	return (status);
    755 }
    756 
    757 static int
    758 vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
    759 {
    760 	int	status;
    761 
    762 	switch (cmd) {
    763 	case DDI_ATTACH:
    764 		if ((status = vdc_do_attach(dip)) != 0)
    765 			(void) vdc_detach(dip, DDI_DETACH);
    766 		return (status);
    767 	case DDI_RESUME:
    768 		/* nothing to do for this non-device */
    769 		return (DDI_SUCCESS);
    770 	default:
    771 		return (DDI_FAILURE);
    772 	}
    773 }
    774 
    775 static int
    776 vdc_do_ldc_init(vdc_t *vdc, vdc_server_t *srvr)
    777 {
    778 	int			status = 0;
    779 	ldc_status_t		ldc_state;
    780 	ldc_attr_t		ldc_attr;
    781 
    782 	ASSERT(vdc != NULL);
    783 	ASSERT(srvr != NULL);
    784 
    785 	ldc_attr.devclass = LDC_DEV_BLK;
    786 	ldc_attr.instance = vdc->instance;
    787 	ldc_attr.mode = LDC_MODE_UNRELIABLE;	/* unreliable transport */
    788 	ldc_attr.mtu = VD_LDC_MTU;
    789 
    790 	if ((srvr->state & VDC_LDC_INIT) == 0) {
    791 		status = ldc_init(srvr->ldc_id, &ldc_attr,
    792 		    &srvr->ldc_handle);
    793 		if (status != 0) {
    794 			DMSG(vdc, 0, "[%d] ldc_init(chan %ld) returned %d",
    795 			    vdc->instance, srvr->ldc_id, status);
    796 			return (status);
    797 		}
    798 		srvr->state |= VDC_LDC_INIT;
    799 	}
    800 	status = ldc_status(srvr->ldc_handle, &ldc_state);
    801 	if (status != 0) {
    802 		DMSG(vdc, 0, "[%d] Cannot discover LDC status [err=%d]",
    803 		    vdc->instance, status);
    804 		goto init_exit;
    805 	}
    806 	srvr->ldc_state = ldc_state;
    807 
    808 	if ((srvr->state & VDC_LDC_CB) == 0) {
    809 		status = ldc_reg_callback(srvr->ldc_handle, vdc_handle_cb,
    810 		    (caddr_t)srvr);
    811 		if (status != 0) {
    812 			DMSG(vdc, 0, "[%d] LDC callback reg. failed (%d)",
    813 			    vdc->instance, status);
    814 			goto init_exit;
    815 		}
    816 		srvr->state |= VDC_LDC_CB;
    817 	}
    818 
    819 	/*
    820 	 * At this stage we have initialised LDC, we will now try and open
    821 	 * the connection.
    822 	 */
    823 	if (srvr->ldc_state == LDC_INIT) {
    824 		status = ldc_open(srvr->ldc_handle);
    825 		if (status != 0) {
    826 			DMSG(vdc, 0, "[%d] ldc_open(chan %ld) returned %d",
    827 			    vdc->instance, srvr->ldc_id, status);
    828 			goto init_exit;
    829 		}
    830 		srvr->state |= VDC_LDC_OPEN;
    831 	}
    832 
    833 init_exit:
    834 	if (status) {
    835 		vdc_terminate_ldc(vdc, srvr);
    836 	}
    837 
    838 	return (status);
    839 }
    840 
    841 static int
    842 vdc_start_ldc_connection(vdc_t *vdc)
    843 {
    844 	int		status = 0;
    845 
    846 	ASSERT(vdc != NULL);
    847 
    848 	ASSERT(MUTEX_HELD(&vdc->lock));
    849 
    850 	status = vdc_do_ldc_up(vdc);
    851 
    852 	DMSG(vdc, 0, "[%d] Finished bringing up LDC\n", vdc->instance);
    853 
    854 	return (status);
    855 }
    856 
    857 static int
    858 vdc_stop_ldc_connection(vdc_t *vdcp)
    859 {
    860 	int	status;
    861 
    862 	ASSERT(vdcp != NULL);
    863 
    864 	ASSERT(MUTEX_HELD(&vdcp->lock));
    865 
    866 	DMSG(vdcp, 0, ": Resetting connection to vDisk server : state %d\n",
    867 	    vdcp->state);
    868 
    869 	status = ldc_down(vdcp->curr_server->ldc_handle);
    870 	DMSG(vdcp, 0, "ldc_down() = %d\n", status);
    871 
    872 	vdcp->initialized &= ~VDC_HANDSHAKE;
    873 	DMSG(vdcp, 0, "initialized=%x\n", vdcp->initialized);
    874 
    875 	return (status);
    876 }
    877 
    878 static void
    879 vdc_create_io_kstats(vdc_t *vdc)
    880 {
    881 	if (vdc->io_stats != NULL) {
    882 		DMSG(vdc, 0, "[%d] I/O kstat already exists\n", vdc->instance);
    883 		return;
    884 	}
    885 
    886 	vdc->io_stats = kstat_create(VDC_DRIVER_NAME, vdc->instance, NULL,
    887 	    "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
    888 	if (vdc->io_stats != NULL) {
    889 		vdc->io_stats->ks_lock = &vdc->lock;
    890 		kstat_install(vdc->io_stats);
    891 	} else {
    892 		cmn_err(CE_NOTE, "[%d] Failed to create kstat: I/O statistics"
    893 		    " will not be gathered", vdc->instance);
    894 	}
    895 }
    896 
    897 static void
    898 vdc_create_err_kstats(vdc_t *vdc)
    899 {
    900 	vd_err_stats_t	*stp;
    901 	char	kstatmodule_err[KSTAT_STRLEN];
    902 	char	kstatname[KSTAT_STRLEN];
    903 	int	ndata = (sizeof (vd_err_stats_t) / sizeof (kstat_named_t));
    904 	int	instance = vdc->instance;
    905 
    906 	if (vdc->err_stats != NULL) {
    907 		DMSG(vdc, 0, "[%d] ERR kstat already exists\n", vdc->instance);
    908 		return;
    909 	}
    910 
    911 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
    912 	    "%serr", VDC_DRIVER_NAME);
    913 	(void) snprintf(kstatname, sizeof (kstatname),
    914 	    "%s%d,err", VDC_DRIVER_NAME, instance);
    915 
    916 	vdc->err_stats = kstat_create(kstatmodule_err, instance, kstatname,
    917 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
    918 
    919 	if (vdc->err_stats == NULL) {
    920 		cmn_err(CE_NOTE, "[%d] Failed to create kstat: Error statistics"
    921 		    " will not be gathered", instance);
    922 		return;
    923 	}
    924 
    925 	stp = (vd_err_stats_t *)vdc->err_stats->ks_data;
    926 	kstat_named_init(&stp->vd_softerrs,	"Soft Errors",
    927 	    KSTAT_DATA_UINT32);
    928 	kstat_named_init(&stp->vd_transerrs,	"Transport Errors",
    929 	    KSTAT_DATA_UINT32);
    930 	kstat_named_init(&stp->vd_protoerrs,	"Protocol Errors",
    931 	    KSTAT_DATA_UINT32);
    932 	kstat_named_init(&stp->vd_vid,		"Vendor",
    933 	    KSTAT_DATA_CHAR);
    934 	kstat_named_init(&stp->vd_pid,		"Product",
    935 	    KSTAT_DATA_CHAR);
    936 	kstat_named_init(&stp->vd_capacity,	"Size",
    937 	    KSTAT_DATA_ULONGLONG);
    938 
    939 	vdc->err_stats->ks_update  = nulldev;
    940 
    941 	kstat_install(vdc->err_stats);
    942 }
    943 
    944 static void
    945 vdc_set_err_kstats(vdc_t *vdc)
    946 {
    947 	vd_err_stats_t  *stp;
    948 
    949 	if (vdc->err_stats == NULL)
    950 		return;
    951 
    952 	mutex_enter(&vdc->lock);
    953 
    954 	stp = (vd_err_stats_t *)vdc->err_stats->ks_data;
    955 	ASSERT(stp != NULL);
    956 
    957 	stp->vd_capacity.value.ui64 = vdc->vdisk_size * vdc->vdisk_bsize;
    958 	(void) strcpy(stp->vd_vid.value.c, "SUN");
    959 	(void) strcpy(stp->vd_pid.value.c, "VDSK");
    960 
    961 	mutex_exit(&vdc->lock);
    962 }
    963 
    964 static int
    965 vdc_create_device_nodes_efi(vdc_t *vdc)
    966 {
    967 	ddi_remove_minor_node(vdc->dip, "h");
    968 	ddi_remove_minor_node(vdc->dip, "h,raw");
    969 
    970 	if (ddi_create_minor_node(vdc->dip, "wd", S_IFBLK,
    971 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
    972 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
    973 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd'",
    974 		    vdc->instance);
    975 		return (EIO);
    976 	}
    977 
    978 	/* if any device node is created we set this flag */
    979 	vdc->initialized |= VDC_MINOR;
    980 
    981 	if (ddi_create_minor_node(vdc->dip, "wd,raw", S_IFCHR,
    982 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
    983 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
    984 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd,raw'",
    985 		    vdc->instance);
    986 		return (EIO);
    987 	}
    988 
    989 	return (0);
    990 }
    991 
    992 static int
    993 vdc_create_device_nodes_vtoc(vdc_t *vdc)
    994 {
    995 	ddi_remove_minor_node(vdc->dip, "wd");
    996 	ddi_remove_minor_node(vdc->dip, "wd,raw");
    997 
    998 	if (ddi_create_minor_node(vdc->dip, "h", S_IFBLK,
    999 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
   1000 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
   1001 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h'",
   1002 		    vdc->instance);
   1003 		return (EIO);
   1004 	}
   1005 
   1006 	/* if any device node is created we set this flag */
   1007 	vdc->initialized |= VDC_MINOR;
   1008 
   1009 	if (ddi_create_minor_node(vdc->dip, "h,raw", S_IFCHR,
   1010 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
   1011 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
   1012 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h,raw'",
   1013 		    vdc->instance);
   1014 		return (EIO);
   1015 	}
   1016 
   1017 	return (0);
   1018 }
   1019 
   1020 /*
   1021  * Function:
   1022  *	vdc_create_device_nodes
   1023  *
   1024  * Description:
   1025  *	This function creates the block and character device nodes under
   1026  *	/devices. It is called as part of the attach(9E) of the instance
   1027  *	during the handshake with vds after vds has sent the attributes
   1028  *	to vdc.
   1029  *
   1030  *	If the device is of type VD_DISK_TYPE_SLICE then the minor node
   1031  *	of 2 is used in keeping with the Solaris convention that slice 2
   1032  *	refers to a whole disk. Slices start at 'a'
   1033  *
   1034  * Parameters:
   1035  *	vdc 		- soft state pointer
   1036  *
   1037  * Return Values
   1038  *	0		- Success
   1039  *	EIO		- Failed to create node
   1040  */
   1041 static int
   1042 vdc_create_device_nodes(vdc_t *vdc)
   1043 {
   1044 	char		name[sizeof ("s,raw")];
   1045 	dev_info_t	*dip = NULL;
   1046 	int		instance, status;
   1047 	int		num_slices = 1;
   1048 	int		i;
   1049 
   1050 	ASSERT(vdc != NULL);
   1051 
   1052 	instance = vdc->instance;
   1053 	dip = vdc->dip;
   1054 
   1055 	switch (vdc->vdisk_type) {
   1056 	case VD_DISK_TYPE_DISK:
   1057 	case VD_DISK_TYPE_UNK:
   1058 		num_slices = V_NUMPAR;
   1059 		break;
   1060 	case VD_DISK_TYPE_SLICE:
   1061 		num_slices = 1;
   1062 		break;
   1063 	default:
   1064 		ASSERT(0);
   1065 	}
   1066 
   1067 	/*
   1068 	 * Minor nodes are different for EFI disks: EFI disks do not have
   1069 	 * a minor node 'g' for the minor number corresponding to slice
   1070 	 * VD_EFI_WD_SLICE (slice 7) instead they have a minor node 'wd'
   1071 	 * representing the whole disk.
   1072 	 */
   1073 	for (i = 0; i < num_slices; i++) {
   1074 
   1075 		if (i == VD_EFI_WD_SLICE) {
   1076 			if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
   1077 				status = vdc_create_device_nodes_efi(vdc);
   1078 			else
   1079 				status = vdc_create_device_nodes_vtoc(vdc);
   1080 			if (status != 0)
   1081 				return (status);
   1082 			continue;
   1083 		}
   1084 
   1085 		(void) snprintf(name, sizeof (name), "%c", 'a' + i);
   1086 		if (ddi_create_minor_node(dip, name, S_IFBLK,
   1087 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
   1088 			cmn_err(CE_NOTE, "[%d] Couldn't add block node '%s'",
   1089 			    instance, name);
   1090 			return (EIO);
   1091 		}
   1092 
   1093 		/* if any device node is created we set this flag */
   1094 		vdc->initialized |= VDC_MINOR;
   1095 
   1096 		(void) snprintf(name, sizeof (name), "%c%s", 'a' + i, ",raw");
   1097 
   1098 		if (ddi_create_minor_node(dip, name, S_IFCHR,
   1099 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
   1100 			cmn_err(CE_NOTE, "[%d] Couldn't add raw node '%s'",
   1101 			    instance, name);
   1102 			return (EIO);
   1103 		}
   1104 	}
   1105 
   1106 	return (0);
   1107 }
   1108 
   1109 /*
   1110  * Driver prop_op(9e) entry point function. Return the number of blocks for
   1111  * the partition in question or forward the request to the property facilities.
   1112  */
   1113 static int
   1114 vdc_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
   1115     char *name, caddr_t valuep, int *lengthp)
   1116 {
   1117 	int instance = ddi_get_instance(dip);
   1118 	vdc_t *vdc;
   1119 	uint64_t nblocks;
   1120 	uint_t blksize;
   1121 
   1122 	vdc = ddi_get_soft_state(vdc_state, instance);
   1123 
   1124 	if (dev == DDI_DEV_T_ANY || vdc == NULL) {
   1125 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
   1126 		    name, valuep, lengthp));
   1127 	}
   1128 
   1129 	mutex_enter(&vdc->lock);
   1130 	(void) vdc_validate_geometry(vdc);
   1131 	if (vdc->vdisk_label == VD_DISK_LABEL_UNK) {
   1132 		mutex_exit(&vdc->lock);
   1133 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
   1134 		    name, valuep, lengthp));
   1135 	}
   1136 	nblocks = vdc->slice[VDCPART(dev)].nblocks;
   1137 	blksize = vdc->vdisk_bsize;
   1138 	mutex_exit(&vdc->lock);
   1139 
   1140 	return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op, mod_flags,
   1141 	    name, valuep, lengthp, nblocks, blksize));
   1142 }
   1143 
   1144 /*
   1145  * Function:
   1146  *	vdc_is_opened
   1147  *
   1148  * Description:
   1149  *	This function checks if any slice of a given virtual disk is
   1150  *	currently opened.
   1151  *
   1152  * Parameters:
   1153  *	vdc 		- soft state pointer
   1154  *
   1155  * Return Values
   1156  *	B_TRUE		- at least one slice is opened.
   1157  *	B_FALSE		- no slice is opened.
   1158  */
   1159 static boolean_t
   1160 vdc_is_opened(vdc_t *vdc)
   1161 {
   1162 	int i;
   1163 
   1164 	/* check if there's any layered open */
   1165 	for (i = 0; i < V_NUMPAR; i++) {
   1166 		if (vdc->open_lyr[i] > 0)
   1167 			return (B_TRUE);
   1168 	}
   1169 
   1170 	/* check if there is any other kind of open */
   1171 	for (i = 0; i < OTYPCNT; i++) {
   1172 		if (vdc->open[i] != 0)
   1173 			return (B_TRUE);
   1174 	}
   1175 
   1176 	return (B_FALSE);
   1177 }
   1178 
   1179 static int
   1180 vdc_mark_opened(vdc_t *vdc, int slice, int flag, int otyp)
   1181 {
   1182 	uint8_t slicemask;
   1183 	int i;
   1184 
   1185 	ASSERT(otyp < OTYPCNT);
   1186 	ASSERT(slice < V_NUMPAR);
   1187 	ASSERT(MUTEX_HELD(&vdc->lock));
   1188 
   1189 	slicemask = 1 << slice;
   1190 
   1191 	/*
   1192 	 * If we have a single-slice disk which was unavailable during the
   1193 	 * attach then a device was created for each 8 slices. Now that
   1194 	 * the type is known, we prevent opening any slice other than 0
   1195 	 * even if a device still exists.
   1196 	 */
   1197 	if (vdc->vdisk_type == VD_DISK_TYPE_SLICE && slice != 0)
   1198 		return (EIO);
   1199 
   1200 	/* check if slice is already exclusively opened */
   1201 	if (vdc->open_excl & slicemask)
   1202 		return (EBUSY);
   1203 
   1204 	/* if open exclusive, check if slice is already opened */
   1205 	if (flag & FEXCL) {
   1206 		if (vdc->open_lyr[slice] > 0)
   1207 			return (EBUSY);
   1208 		for (i = 0; i < OTYPCNT; i++) {
   1209 			if (vdc->open[i] & slicemask)
   1210 				return (EBUSY);
   1211 		}
   1212 		vdc->open_excl |= slicemask;
   1213 	}
   1214 
   1215 	/* mark slice as opened */
   1216 	if (otyp == OTYP_LYR) {
   1217 		vdc->open_lyr[slice]++;
   1218 	} else {
   1219 		vdc->open[otyp] |= slicemask;
   1220 	}
   1221 
   1222 	return (0);
   1223 }
   1224 
   1225 static void
   1226 vdc_mark_closed(vdc_t *vdc, int slice, int flag, int otyp)
   1227 {
   1228 	uint8_t slicemask;
   1229 
   1230 	ASSERT(otyp < OTYPCNT);
   1231 	ASSERT(slice < V_NUMPAR);
   1232 	ASSERT(MUTEX_HELD(&vdc->lock));
   1233 
   1234 	slicemask = 1 << slice;
   1235 
   1236 	if (otyp == OTYP_LYR) {
   1237 		ASSERT(vdc->open_lyr[slice] > 0);
   1238 		vdc->open_lyr[slice]--;
   1239 	} else {
   1240 		vdc->open[otyp] &= ~slicemask;
   1241 	}
   1242 
   1243 	if (flag & FEXCL)
   1244 		vdc->open_excl &= ~slicemask;
   1245 }
   1246 
   1247 static int
   1248 vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred)
   1249 {
   1250 	_NOTE(ARGUNUSED(cred))
   1251 
   1252 	int	instance, nodelay;
   1253 	int	slice, status = 0;
   1254 	vdc_t	*vdc;
   1255 
   1256 	ASSERT(dev != NULL);
   1257 	instance = VDCUNIT(*dev);
   1258 
   1259 	if (otyp >= OTYPCNT)
   1260 		return (EINVAL);
   1261 
   1262 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
   1263 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
   1264 		return (ENXIO);
   1265 	}
   1266 
   1267 	DMSG(vdc, 0, "minor = %d flag = %x, otyp = %x\n",
   1268 	    getminor(*dev), flag, otyp);
   1269 
   1270 	slice = VDCPART(*dev);
   1271 
   1272 	nodelay = flag & (FNDELAY | FNONBLOCK);
   1273 
   1274 	if ((flag & FWRITE) && (!nodelay) &&
   1275 	    !(VD_OP_SUPPORTED(vdc->operations, VD_OP_BWRITE))) {
   1276 		return (EROFS);
   1277 	}
   1278 
   1279 	mutex_enter(&vdc->lock);
   1280 
   1281 	status = vdc_mark_opened(vdc, slice, flag, otyp);
   1282 
   1283 	if (status != 0) {
   1284 		mutex_exit(&vdc->lock);
   1285 		return (status);
   1286 	}
   1287 
   1288 	/*
   1289 	 * If the disk type is unknown then we have to wait for the
   1290 	 * handshake to complete because we don't know if the slice
   1291 	 * device we are opening effectively exists.
   1292 	 */
   1293 	if (vdc->vdisk_type != VD_DISK_TYPE_UNK && nodelay) {
   1294 
   1295 		/* don't resubmit a validate request if there's already one */
   1296 		if (vdc->validate_pending > 0) {
   1297 			mutex_exit(&vdc->lock);
   1298 			return (0);
   1299 		}
   1300 
   1301 		/* call vdc_validate() asynchronously to avoid blocking */
   1302 		if (taskq_dispatch(system_taskq, vdc_validate_task,
   1303 		    (void *)vdc, TQ_NOSLEEP) == NULL) {
   1304 			vdc_mark_closed(vdc, slice, flag, otyp);
   1305 			mutex_exit(&vdc->lock);
   1306 			return (ENXIO);
   1307 		}
   1308 
   1309 		vdc->validate_pending++;
   1310 		mutex_exit(&vdc->lock);
   1311 		return (0);
   1312 	}
   1313 
   1314 	mutex_exit(&vdc->lock);
   1315 
   1316 	vdc_validate(vdc);
   1317 
   1318 	mutex_enter(&vdc->lock);
   1319 
   1320 	if (vdc->vdisk_type == VD_DISK_TYPE_UNK ||
   1321 	    (vdc->vdisk_type == VD_DISK_TYPE_SLICE && slice != 0) ||
   1322 	    (!nodelay && (vdc->vdisk_label == VD_DISK_LABEL_UNK ||
   1323 	    vdc->slice[slice].nblocks == 0))) {
   1324 		vdc_mark_closed(vdc, slice, flag, otyp);
   1325 		status = EIO;
   1326 	}
   1327 
   1328 	mutex_exit(&vdc->lock);
   1329 
   1330 	return (status);
   1331 }
   1332 
   1333 static int
   1334 vdc_close(dev_t dev, int flag, int otyp, cred_t *cred)
   1335 {
   1336 	_NOTE(ARGUNUSED(cred))
   1337 
   1338 	int	instance;
   1339 	int	slice;
   1340 	int	rv, rval;
   1341 	vdc_t	*vdc;
   1342 
   1343 	instance = VDCUNIT(dev);
   1344 
   1345 	if (otyp >= OTYPCNT)
   1346 		return (EINVAL);
   1347 
   1348 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
   1349 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
   1350 		return (ENXIO);
   1351 	}
   1352 
   1353 	DMSG(vdc, 0, "[%d] flag = %x, otyp = %x\n", instance, flag, otyp);
   1354 
   1355 	slice = VDCPART(dev);
   1356 
   1357 	/*
   1358 	 * Attempt to flush the W$ on a close operation. If this is
   1359 	 * not a supported IOCTL command or the backing device is read-only
   1360 	 * do not fail the close operation.
   1361 	 */
   1362 	rv = vd_process_ioctl(dev, DKIOCFLUSHWRITECACHE, NULL, FKIOCTL, &rval);
   1363 
   1364 	if (rv != 0 && rv != ENOTSUP && rv != ENOTTY && rv != EROFS) {
   1365 		DMSG(vdc, 0, "[%d] flush failed with error %d on close\n",
   1366 		    instance, rv);
   1367 		return (EIO);
   1368 	}
   1369 
   1370 	mutex_enter(&vdc->lock);
   1371 	vdc_mark_closed(vdc, slice, flag, otyp);
   1372 	mutex_exit(&vdc->lock);
   1373 
   1374 	return (0);
   1375 }
   1376 
   1377 static int
   1378 vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
   1379 {
   1380 	_NOTE(ARGUNUSED(credp))
   1381 
   1382 	return (vd_process_ioctl(dev, cmd, (caddr_t)arg, mode, rvalp));
   1383 }
   1384 
   1385 static int
   1386 vdc_print(dev_t dev, char *str)
   1387 {
   1388 	cmn_err(CE_NOTE, "vdc%d:  %s", VDCUNIT(dev), str);
   1389 	return (0);
   1390 }
   1391 
   1392 static int
   1393 vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
   1394 {
   1395 	int	rv, flags;
   1396 	size_t	nbytes = nblk * DEV_BSIZE;
   1397 	int	instance = VDCUNIT(dev);
   1398 	vdc_t	*vdc = NULL;
   1399 	diskaddr_t vio_blkno;
   1400 
   1401 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
   1402 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
   1403 		return (ENXIO);
   1404 	}
   1405 
   1406 	DMSG(vdc, 2, "[%d] dump %ld bytes at block 0x%lx : addr=0x%p\n",
   1407 	    instance, nbytes, blkno, (void *)addr);
   1408 
   1409 	/* convert logical block to vio block */
   1410 	if ((blkno & vdc->vio_bmask) != 0) {
   1411 		DMSG(vdc, 0, "Misaligned block number (%lu)\n", blkno);
   1412 		return (EINVAL);
   1413 	}
   1414 	vio_blkno = blkno >> vdc->vio_bshift;
   1415 
   1416 	/*
   1417 	 * If we are panicking, we need the state to be "running" so that we
   1418 	 * can submit I/Os, but we don't want to check for any backend error.
   1419 	 */
   1420 	flags = (ddi_in_panic())? VDC_OP_STATE_RUNNING : VDC_OP_NORMAL;
   1421 
   1422 	rv = vdc_do_op(vdc, VD_OP_BWRITE, addr, nbytes, VDCPART(dev),
   1423 	    vio_blkno, NULL, VIO_write_dir, flags);
   1424 
   1425 	if (rv) {
   1426 		DMSG(vdc, 0, "Failed to do a disk dump (err=%d)\n", rv);
   1427 		return (rv);
   1428 	}
   1429 
   1430 	DMSG(vdc, 0, "[%d] End\n", instance);
   1431 
   1432 	return (0);
   1433 }
   1434 
   1435 /* -------------------------------------------------------------------------- */
   1436 
   1437 /*
   1438  * Disk access routines
   1439  *
   1440  */
   1441 
   1442 /*
   1443  * vdc_strategy()
   1444  *
   1445  * Return Value:
   1446  *	0:	As per strategy(9E), the strategy() function must return 0
   1447  *		[ bioerror(9f) sets b_flags to the proper error code ]
   1448  */
   1449 static int
   1450 vdc_strategy(struct buf *buf)
   1451 {
   1452 	diskaddr_t vio_blkno;
   1453 	vdc_t	*vdc = NULL;
   1454 	int	instance = VDCUNIT(buf->b_edev);
   1455 	int	op = (buf->b_flags & B_READ) ? VD_OP_BREAD : VD_OP_BWRITE;
   1456 	int	slice;
   1457 
   1458 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
   1459 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
   1460 		bioerror(buf, ENXIO);
   1461 		biodone(buf);
   1462 		return (0);
   1463 	}
   1464 
   1465 	DMSG(vdc, 2, "[%d] %s %ld bytes at block %llx : b_addr=0x%p\n",
   1466 	    instance, (buf->b_flags & B_READ) ? "Read" : "Write",
   1467 	    buf->b_bcount, buf->b_lblkno, (void *)buf->b_un.b_addr);
   1468 
   1469 	bp_mapin(buf);
   1470 
   1471 	if ((long)buf->b_private == VD_SLICE_NONE) {
   1472 		/* I/O using an absolute disk offset */
   1473 		slice = VD_SLICE_NONE;
   1474 	} else {
   1475 		slice = VDCPART(buf->b_edev);
   1476 	}
   1477 
   1478 	/*
   1479 	 * In the buf structure, b_lblkno represents a logical block number
   1480 	 * using a block size of 512 bytes. For the VIO request, this block
   1481 	 * number has to be converted to be represented with the block size
   1482 	 * used by the VIO protocol.
   1483 	 */
   1484 	if ((buf->b_lblkno & vdc->vio_bmask) != 0) {
   1485 		bioerror(buf, EINVAL);
   1486 		biodone(buf);
   1487 		return (0);
   1488 	}
   1489 	vio_blkno = buf->b_lblkno >> vdc->vio_bshift;
   1490 
   1491 	/* submit the I/O, any error will be reported in the buf structure */
   1492 	(void) vdc_do_op(vdc, op, (caddr_t)buf->b_un.b_addr,
   1493 	    buf->b_bcount, slice, vio_blkno,
   1494 	    buf, (op == VD_OP_BREAD) ? VIO_read_dir : VIO_write_dir,
   1495 	    VDC_OP_NORMAL);
   1496 
   1497 	return (0);
   1498 }
   1499 
   1500 /*
   1501  * Function:
   1502  *	vdc_min
   1503  *
   1504  * Description:
   1505  *	Routine to limit the size of a data transfer. Used in
   1506  *	conjunction with physio(9F).
   1507  *
   1508  * Arguments:
   1509  *	bp - pointer to the indicated buf(9S) struct.
   1510  *
   1511  */
   1512 static void
   1513 vdc_min(struct buf *bufp)
   1514 {
   1515 	vdc_t	*vdc = NULL;
   1516 	int	instance = VDCUNIT(bufp->b_edev);
   1517 
   1518 	vdc = ddi_get_soft_state(vdc_state, instance);
   1519 	VERIFY(vdc != NULL);
   1520 
   1521 	if (bufp->b_bcount > (vdc->max_xfer_sz * vdc->vdisk_bsize)) {
   1522 		bufp->b_bcount = vdc->max_xfer_sz * vdc->vdisk_bsize;
   1523 	}
   1524 }
   1525 
   1526 static int
   1527 vdc_read(dev_t dev, struct uio *uio, cred_t *cred)
   1528 {
   1529 	_NOTE(ARGUNUSED(cred))
   1530 
   1531 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
   1532 	return (physio(vdc_strategy, NULL, dev, B_READ, vdc_min, uio));
   1533 }
   1534 
   1535 static int
   1536 vdc_write(dev_t dev, struct uio *uio, cred_t *cred)
   1537 {
   1538 	_NOTE(ARGUNUSED(cred))
   1539 
   1540 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
   1541 	return (physio(vdc_strategy, NULL, dev, B_WRITE, vdc_min, uio));
   1542 }
   1543 
   1544 static int
   1545 vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred)
   1546 {
   1547 	_NOTE(ARGUNUSED(cred))
   1548 
   1549 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
   1550 	return (aphysio(vdc_strategy, anocancel, dev, B_READ, vdc_min, aio));
   1551 }
   1552 
   1553 static int
   1554 vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred)
   1555 {
   1556 	_NOTE(ARGUNUSED(cred))
   1557 
   1558 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
   1559 	return (aphysio(vdc_strategy, anocancel, dev, B_WRITE, vdc_min, aio));
   1560 }
   1561 
   1562 
   1563 /* -------------------------------------------------------------------------- */
   1564 
   1565 /*
   1566  * Handshake support
   1567  */
   1568 
   1569 
   1570 /*
   1571  * Function:
   1572  *	vdc_init_ver_negotiation()
   1573  *
   1574  * Description:
   1575  *
   1576  * Arguments:
   1577  *	vdc	- soft state pointer for this instance of the device driver.
   1578  *
   1579  * Return Code:
   1580  *	0	- Success
   1581  */
   1582 static int
   1583 vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver)
   1584 {
   1585 	vio_ver_msg_t	pkt;
   1586 	size_t		msglen = sizeof (pkt);
   1587 	int		status = -1;
   1588 
   1589 	ASSERT(vdc != NULL);
   1590 	ASSERT(mutex_owned(&vdc->lock));
   1591 
   1592 	DMSG(vdc, 0, "[%d] Entered.\n", vdc->instance);
   1593 
   1594 	/*
   1595 	 * set the Session ID to a unique value
   1596 	 * (the lower 32 bits of the clock tick)
   1597 	 */
   1598 	vdc->session_id = ((uint32_t)gettick() & 0xffffffff);
   1599 	DMSG(vdc, 0, "[%d] Set SID to 0x%lx\n", vdc->instance, vdc->session_id);
   1600 
   1601 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
   1602 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
   1603 	pkt.tag.vio_subtype_env = VIO_VER_INFO;
   1604 	pkt.tag.vio_sid = vdc->session_id;
   1605 	pkt.dev_class = VDEV_DISK;
   1606 	pkt.ver_major = ver.major;
   1607 	pkt.ver_minor = ver.minor;
   1608 
   1609 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
   1610 	DMSG(vdc, 0, "[%d] Ver info sent (status = %d)\n",
   1611 	    vdc->instance, status);
   1612 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
   1613 		DMSG(vdc, 0, "[%d] Failed to send Ver negotiation info: "
   1614 		    "id(%lx) rv(%d) size(%ld)", vdc->instance,
   1615 		    vdc->curr_server->ldc_handle, status, msglen);
   1616 		if (msglen != sizeof (vio_ver_msg_t))
   1617 			status = ENOMSG;
   1618 	}
   1619 
   1620 	return (status);
   1621 }
   1622 
   1623 /*
   1624  * Function:
   1625  *	vdc_ver_negotiation()
   1626  *
   1627  * Description:
   1628  *
   1629  * Arguments:
   1630  *	vdcp	- soft state pointer for this instance of the device driver.
   1631  *
   1632  * Return Code:
   1633  *	0	- Success
   1634  */
   1635 static int
   1636 vdc_ver_negotiation(vdc_t *vdcp)
   1637 {
   1638 	vio_msg_t vio_msg;
   1639 	int status;
   1640 
   1641 	if (status = vdc_init_ver_negotiation(vdcp, vdc_version[0]))
   1642 		return (status);
   1643 
   1644 	/* release lock and wait for response */
   1645 	mutex_exit(&vdcp->lock);
   1646 	status = vdc_wait_for_response(vdcp, &vio_msg);
   1647 	mutex_enter(&vdcp->lock);
   1648 	if (status) {
   1649 		DMSG(vdcp, 0,
   1650 		    "[%d] Failed waiting for Ver negotiation response, rv(%d)",
   1651 		    vdcp->instance, status);
   1652 		return (status);
   1653 	}
   1654 
   1655 	/* check type and sub_type ... */
   1656 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
   1657 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
   1658 		DMSG(vdcp, 0, "[%d] Invalid ver negotiation response\n",
   1659 		    vdcp->instance);
   1660 		return (EPROTO);
   1661 	}
   1662 
   1663 	return (vdc_handle_ver_msg(vdcp, (vio_ver_msg_t *)&vio_msg));
   1664 }
   1665 
   1666 /*
   1667  * Function:
   1668  *	vdc_init_attr_negotiation()
   1669  *
   1670  * Description:
   1671  *
   1672  * Arguments:
   1673  *	vdc	- soft state pointer for this instance of the device driver.
   1674  *
   1675  * Return Code:
   1676  *	0	- Success
   1677  */
   1678 static int
   1679 vdc_init_attr_negotiation(vdc_t *vdc)
   1680 {
   1681 	vd_attr_msg_t	pkt;
   1682 	size_t		msglen = sizeof (pkt);
   1683 	int		status;
   1684 
   1685 	ASSERT(vdc != NULL);
   1686 	ASSERT(mutex_owned(&vdc->lock));
   1687 
   1688 	DMSG(vdc, 0, "[%d] entered\n", vdc->instance);
   1689 
   1690 	/* fill in tag */
   1691 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
   1692 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
   1693 	pkt.tag.vio_subtype_env = VIO_ATTR_INFO;
   1694 	pkt.tag.vio_sid = vdc->session_id;
   1695 	/* fill in payload */
   1696 	pkt.max_xfer_sz = vdc->max_xfer_sz;
   1697 	pkt.vdisk_block_size = vdc->vdisk_bsize;
   1698 	pkt.xfer_mode = VIO_DRING_MODE_V1_0;
   1699 	pkt.operations = 0;	/* server will set bits of valid operations */
   1700 	pkt.vdisk_type = 0;	/* server will set to valid device type */
   1701 	pkt.vdisk_media = 0;	/* server will set to valid media type */
   1702 	pkt.vdisk_size = 0;	/* server will set to valid size */
   1703 
   1704 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
   1705 	DMSG(vdc, 0, "Attr info sent (status = %d)\n", status);
   1706 
   1707 	if ((status != 0) || (msglen != sizeof (vd_attr_msg_t))) {
   1708 		DMSG(vdc, 0, "[%d] Failed to send Attr negotiation info: "
   1709 		    "id(%lx) rv(%d) size(%ld)", vdc->instance,
   1710 		    vdc->curr_server->ldc_handle, status, msglen);
   1711 		if (msglen != sizeof (vd_attr_msg_t))
   1712 			status = ENOMSG;
   1713 	}
   1714 
   1715 	return (status);
   1716 }
   1717 
   1718 /*
   1719  * Function:
   1720  *	vdc_attr_negotiation()
   1721  *
   1722  * Description:
   1723  *
   1724  * Arguments:
   1725  *	vdc	- soft state pointer for this instance of the device driver.
   1726  *
   1727  * Return Code:
   1728  *	0	- Success
   1729  */
   1730 static int
   1731 vdc_attr_negotiation(vdc_t *vdcp)
   1732 {
   1733 	int status;
   1734 	vio_msg_t vio_msg;
   1735 
   1736 	if (status = vdc_init_attr_negotiation(vdcp))
   1737 		return (status);
   1738 
   1739 	/* release lock and wait for response */
   1740 	mutex_exit(&vdcp->lock);
   1741 	status = vdc_wait_for_response(vdcp, &vio_msg);
   1742 	mutex_enter(&vdcp->lock);
   1743 	if (status) {
   1744 		DMSG(vdcp, 0,
   1745 		    "[%d] Failed waiting for Attr negotiation response, rv(%d)",
   1746 		    vdcp->instance, status);
   1747 		return (status);
   1748 	}
   1749 
   1750 	/* check type and sub_type ... */
   1751 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
   1752 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
   1753 		DMSG(vdcp, 0, "[%d] Invalid attr negotiation response\n",
   1754 		    vdcp->instance);
   1755 		return (EPROTO);
   1756 	}
   1757 
   1758 	return (vdc_handle_attr_msg(vdcp, (vd_attr_msg_t *)&vio_msg));
   1759 }
   1760 
   1761 
   1762 /*
   1763  * Function:
   1764  *	vdc_init_dring_negotiate()
   1765  *
   1766  * Description:
   1767  *
   1768  * Arguments:
   1769  *	vdc	- soft state pointer for this instance of the device driver.
   1770  *
   1771  * Return Code:
   1772  *	0	- Success
   1773  */
   1774 static int
   1775 vdc_init_dring_negotiate(vdc_t *vdc)
   1776 {
   1777 	vio_dring_reg_msg_t	pkt;
   1778 	size_t			msglen = sizeof (pkt);
   1779 	int			status = -1;
   1780 	int			retry;
   1781 	int			nretries = 10;
   1782 
   1783 	ASSERT(vdc != NULL);
   1784 	ASSERT(mutex_owned(&vdc->lock));
   1785 
   1786 	for (retry = 0; retry < nretries; retry++) {
   1787 		status = vdc_init_descriptor_ring(vdc);
   1788 		if (status != EAGAIN)
   1789 			break;
   1790 		drv_usecwait(vdc_min_timeout_ldc);
   1791 	}
   1792 
   1793 	if (status != 0) {
   1794 		DMSG(vdc, 0, "[%d] Failed to init DRing (status = %d)\n",
   1795 		    vdc->instance, status);
   1796 		return (status);
   1797 	}
   1798 
   1799 	DMSG(vdc, 0, "[%d] Init of descriptor ring completed (status = %d)\n",
   1800 	    vdc->instance, status);
   1801 
   1802 	/* fill in tag */
   1803 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
   1804 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
   1805 	pkt.tag.vio_subtype_env = VIO_DRING_REG;
   1806 	pkt.tag.vio_sid = vdc->session_id;
   1807 	/* fill in payload */
   1808 	pkt.dring_ident = 0;
   1809 	pkt.num_descriptors = vdc->dring_len;
   1810 	pkt.descriptor_size = vdc->dring_entry_size;
   1811 	pkt.options = (VIO_TX_DRING | VIO_RX_DRING);
   1812 	pkt.ncookies = vdc->dring_cookie_count;
   1813 	pkt.cookie[0] = vdc->dring_cookie[0];	/* for now just one cookie */
   1814 
   1815 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
   1816 	if (status != 0) {
   1817 		DMSG(vdc, 0, "[%d] Failed to register DRing (err = %d)",
   1818 		    vdc->instance, status);
   1819 	}
   1820 
   1821 	return (status);
   1822 }
   1823 
   1824 
   1825 /*
   1826  * Function:
   1827  *	vdc_dring_negotiation()
   1828  *
   1829  * Description:
   1830  *
   1831  * Arguments:
   1832  *	vdc	- soft state pointer for this instance of the device driver.
   1833  *
   1834  * Return Code:
   1835  *	0	- Success
   1836  */
   1837 static int
   1838 vdc_dring_negotiation(vdc_t *vdcp)
   1839 {
   1840 	int status;
   1841 	vio_msg_t vio_msg;
   1842 
   1843 	if (status = vdc_init_dring_negotiate(vdcp))
   1844 		return (status);
   1845 
   1846 	/* release lock and wait for response */
   1847 	mutex_exit(&vdcp->lock);
   1848 	status = vdc_wait_for_response(vdcp, &vio_msg);
   1849 	mutex_enter(&vdcp->lock);
   1850 	if (status) {
   1851 		DMSG(vdcp, 0,
   1852 		    "[%d] Failed waiting for Dring negotiation response,"
   1853 		    " rv(%d)", vdcp->instance, status);
   1854 		return (status);
   1855 	}
   1856 
   1857 	/* check type and sub_type ... */
   1858 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
   1859 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
   1860 		DMSG(vdcp, 0, "[%d] Invalid Dring negotiation response\n",
   1861 		    vdcp->instance);
   1862 		return (EPROTO);
   1863 	}
   1864 
   1865 	return (vdc_handle_dring_reg_msg(vdcp,
   1866 	    (vio_dring_reg_msg_t *)&vio_msg));
   1867 }
   1868 
   1869 
   1870 /*
   1871  * Function:
   1872  *	vdc_send_rdx()
   1873  *
   1874  * Description:
   1875  *
   1876  * Arguments:
   1877  *	vdc	- soft state pointer for this instance of the device driver.
   1878  *
   1879  * Return Code:
   1880  *	0	- Success
   1881  */
   1882 static int
   1883 vdc_send_rdx(vdc_t *vdcp)
   1884 {
   1885 	vio_msg_t	msg;
   1886 	size_t		msglen = sizeof (vio_msg_t);
   1887 	int		status;
   1888 
   1889 	/*
   1890 	 * Send an RDX message to vds to indicate we are ready
   1891 	 * to send data
   1892 	 */
   1893 	msg.tag.vio_msgtype = VIO_TYPE_CTRL;
   1894 	msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
   1895 	msg.tag.vio_subtype_env = VIO_RDX;
   1896 	msg.tag.vio_sid = vdcp->session_id;
   1897 	status = vdc_send(vdcp, (caddr_t)&msg, &msglen);
   1898 	if (status != 0) {
   1899 		DMSG(vdcp, 0, "[%d] Failed to send RDX message (%d)",
   1900 		    vdcp->instance, status);
   1901 	}
   1902 
   1903 	return (status);
   1904 }
   1905 
   1906 /*
   1907  * Function:
   1908  *	vdc_handle_rdx()
   1909  *
   1910  * Description:
   1911  *
   1912  * Arguments:
   1913  *	vdc	- soft state pointer for this instance of the device driver.
   1914  *	msgp	- received msg
   1915  *
   1916  * Return Code:
   1917  *	0	- Success
   1918  */
   1919 static int
   1920 vdc_handle_rdx(vdc_t *vdcp, vio_rdx_msg_t *msgp)
   1921 {
   1922 	_NOTE(ARGUNUSED(vdcp))
   1923 	_NOTE(ARGUNUSED(msgp))
   1924 
   1925 	ASSERT(msgp->tag.vio_msgtype == VIO_TYPE_CTRL);
   1926 	ASSERT(msgp->tag.vio_subtype == VIO_SUBTYPE_ACK);
   1927 	ASSERT(msgp->tag.vio_subtype_env == VIO_RDX);
   1928 
   1929 	DMSG(vdcp, 1, "[%d] Got an RDX msg", vdcp->instance);
   1930 
   1931 	return (0);
   1932 }
   1933 
   1934 /*
   1935  * Function:
   1936  *	vdc_rdx_exchange()
   1937  *
   1938  * Description:
   1939  *
   1940  * Arguments:
   1941  *	vdc	- soft state pointer for this instance of the device driver.
   1942  *
   1943  * Return Code:
   1944  *	0	- Success
   1945  */
   1946 static int
   1947 vdc_rdx_exchange(vdc_t *vdcp)
   1948 {
   1949 	int status;
   1950 	vio_msg_t vio_msg;
   1951 
   1952 	if (status = vdc_send_rdx(vdcp))
   1953 		return (status);
   1954 
   1955 	/* release lock and wait for response */
   1956 	mutex_exit(&vdcp->lock);
   1957 	status = vdc_wait_for_response(vdcp, &vio_msg);
   1958 	mutex_enter(&vdcp->lock);
   1959 	if (status) {
   1960 		DMSG(vdcp, 0, "[%d] Failed waiting for RDX response, rv(%d)",
   1961 		    vdcp->instance, status);
   1962 		return (status);
   1963 	}
   1964 
   1965 	/* check type and sub_type ... */
   1966 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
   1967 	    vio_msg.tag.vio_subtype != VIO_SUBTYPE_ACK) {
   1968 		DMSG(vdcp, 0, "[%d] Invalid RDX response\n", vdcp->instance);
   1969 		return (EPROTO);
   1970 	}
   1971 
   1972 	return (vdc_handle_rdx(vdcp, (vio_rdx_msg_t *)&vio_msg));
   1973 }
   1974 
   1975 
   1976 /* -------------------------------------------------------------------------- */
   1977 
   1978 /*
   1979  * LDC helper routines
   1980  */
   1981 
   1982 static int
   1983 vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp)
   1984 {
   1985 	int		status;
   1986 	uint64_t	delay_time;
   1987 	size_t		len;
   1988 
   1989 	/*
   1990 	 * Until we get a blocking ldc read we have to retry until the entire
   1991 	 * LDC message has arrived before ldc_read() will return that message.
   1992 	 * If ldc_read() succeed but returns a zero length message then that
   1993 	 * means that the LDC queue is empty and we have to wait for a
   1994 	 * notification from the LDC callback which will set the read_state to
   1995 	 * VDC_READ_PENDING. Note we also bail out if the channel is reset or
   1996 	 * goes away.
   1997 	 */
   1998 	delay_time = vdc_ldc_read_init_delay;
   1999 
   2000 	for (;;) {
   2001 
   2002 		len = *nbytesp;
   2003 		/*
   2004 		 * vdc->curr_server is protected by vdc->lock but to avoid
   2005 		 * contentions we don't take the lock here. We can do this
   2006 		 * safely because vdc_recv() is only called from thread
   2007 		 * process_msg_thread() which is also the only thread that
   2008 		 * can change vdc->curr_server.
   2009 		 */
   2010 		status = ldc_read(vdc->curr_server->ldc_handle,
   2011 		    (caddr_t)msgp, &len);
   2012 
   2013 		if (status == EAGAIN) {
   2014 			delay_time *= 2;
   2015 			if (delay_time >= vdc_ldc_read_max_delay)
   2016 				delay_time = vdc_ldc_read_max_delay;
   2017 			delay(delay_time);
   2018 			continue;
   2019 		}
   2020 
   2021 		if (status != 0) {
   2022 			DMSG(vdc, 0, "ldc_read returned %d\n", status);
   2023 			break;
   2024 		}
   2025 
   2026 		if (len != 0) {
   2027 			*nbytesp = len;
   2028 			break;
   2029 		}
   2030 
   2031 		mutex_enter(&vdc->read_lock);
   2032 
   2033 		while (vdc->read_state != VDC_READ_PENDING) {
   2034 
   2035 			/* detect if the connection has been reset */
   2036 			if (vdc->read_state == VDC_READ_RESET) {
   2037 				mutex_exit(&vdc->read_lock);
   2038 				return (ECONNRESET);
   2039 			}
   2040 
   2041 			vdc->read_state = VDC_READ_WAITING;
   2042 			cv_wait(&vdc->read_cv, &vdc->read_lock);
   2043 		}
   2044 
   2045 		vdc->read_state = VDC_READ_IDLE;
   2046 		mutex_exit(&vdc->read_lock);
   2047 
   2048 		delay_time = vdc_ldc_read_init_delay;
   2049 	}
   2050 
   2051 	return (status);
   2052 }
   2053 
   2054 
   2055 
   2056 #ifdef DEBUG
   2057 void
   2058 vdc_decode_tag(vdc_t *vdcp, vio_msg_t *msg)
   2059 {
   2060 	char *ms, *ss, *ses;
   2061 	switch (msg->tag.vio_msgtype) {
   2062 #define	Q(_s)	case _s : ms = #_s; break;
   2063 	Q(VIO_TYPE_CTRL)
   2064 	Q(VIO_TYPE_DATA)
   2065 	Q(VIO_TYPE_ERR)
   2066 #undef Q
   2067 	default: ms = "unknown"; break;
   2068 	}
   2069 
   2070 	switch (msg->tag.vio_subtype) {
   2071 #define	Q(_s)	case _s : ss = #_s; break;
   2072 	Q(VIO_SUBTYPE_INFO)
   2073 	Q(VIO_SUBTYPE_ACK)
   2074 	Q(VIO_SUBTYPE_NACK)
   2075 #undef Q
   2076 	default: ss = "unknown"; break;
   2077 	}
   2078 
   2079 	switch (msg->tag.vio_subtype_env) {
   2080 #define	Q(_s)	case _s : ses = #_s; break;
   2081 	Q(VIO_VER_INFO)
   2082 	Q(VIO_ATTR_INFO)
   2083 	Q(VIO_DRING_REG)
   2084 	Q(VIO_DRING_UNREG)
   2085 	Q(VIO_RDX)
   2086 	Q(VIO_PKT_DATA)
   2087 	Q(VIO_DESC_DATA)
   2088 	Q(VIO_DRING_DATA)
   2089 #undef Q
   2090 	default: ses = "unknown"; break;
   2091 	}
   2092 
   2093 	DMSG(vdcp, 3, "(%x/%x/%x) message : (%s/%s/%s)\n",
   2094 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
   2095 	    msg->tag.vio_subtype_env, ms, ss, ses);
   2096 }
   2097 #endif
   2098 
   2099 /*
   2100  * Function:
   2101  *	vdc_send()
   2102  *
   2103  * Description:
   2104  *	The function encapsulates the call to write a message using LDC.
   2105  *	If LDC indicates that the call failed due to the queue being full,
   2106  *	we retry the ldc_write(), otherwise we return the error returned by LDC.
   2107  *
   2108  * Arguments:
   2109  *	ldc_handle	- LDC handle for the channel this instance of vdc uses
   2110  *	pkt		- address of LDC message to be sent
   2111  *	msglen		- the size of the message being sent. When the function
   2112  *			  returns, this contains the number of bytes written.
   2113  *
   2114  * Return Code:
   2115  *	0		- Success.
   2116  *	EINVAL		- pkt or msglen were NULL
   2117  *	ECONNRESET	- The connection was not up.
   2118  *	EWOULDBLOCK	- LDC queue is full
   2119  *	xxx		- other error codes returned by ldc_write
   2120  */
   2121 static int
   2122 vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen)
   2123 {
   2124 	size_t	size = 0;
   2125 	int	status = 0;
   2126 	clock_t delay_ticks;
   2127 
   2128 	ASSERT(vdc != NULL);
   2129 	ASSERT(mutex_owned(&vdc->lock));
   2130 	ASSERT(msglen != NULL);
   2131 	ASSERT(*msglen != 0);
   2132 
   2133 #ifdef DEBUG
   2134 	vdc_decode_tag(vdc, (vio_msg_t *)(uintptr_t)pkt);
   2135 #endif
   2136 	/*
   2137 	 * Wait indefinitely to send if channel
   2138 	 * is busy, but bail out if we succeed or
   2139 	 * if the channel closes or is reset.
   2140 	 */
   2141 	delay_ticks = vdc_hz_min_ldc_delay;
   2142 	do {
   2143 		size = *msglen;
   2144 		status = ldc_write(vdc->curr_server->ldc_handle, pkt, &size);
   2145 		if (status == EWOULDBLOCK) {
   2146 			delay(delay_ticks);
   2147 			/* geometric backoff */
   2148 			delay_ticks *= 2;
   2149 			if (delay_ticks > vdc_hz_max_ldc_delay)
   2150 				delay_ticks = vdc_hz_max_ldc_delay;
   2151 		}
   2152 	} while (status == EWOULDBLOCK);
   2153 
   2154 	/* if LDC had serious issues --- reset vdc state */
   2155 	if (status == EIO || status == ECONNRESET) {
   2156 		/* LDC had serious issues --- reset vdc state */
   2157 		mutex_enter(&vdc->read_lock);
   2158 		if ((vdc->read_state == VDC_READ_WAITING) ||
   2159 		    (vdc->read_state == VDC_READ_RESET))
   2160 			cv_signal(&vdc->read_cv);
   2161 		vdc->read_state = VDC_READ_RESET;
   2162 		mutex_exit(&vdc->read_lock);
   2163 
   2164 		/* wake up any waiters in the reset thread */
   2165 		if (vdc->state == VDC_STATE_INIT_WAITING) {
   2166 			DMSG(vdc, 0, "[%d] write reset - "
   2167 			    "vdc is resetting ..\n", vdc->instance);
   2168 			vdc->state = VDC_STATE_RESETTING;
   2169 			cv_signal(&vdc->initwait_cv);
   2170 		}
   2171 
   2172 		return (ECONNRESET);
   2173 	}
   2174 
   2175 	/* return the last size written */
   2176 	*msglen = size;
   2177 
   2178 	return (status);
   2179 }
   2180 
   2181 /*
   2182  * Function:
   2183  *	vdc_get_md_node
   2184  *
   2185  * Description:
   2186  *	Get the MD, the device node for the given disk instance. The
   2187  *	caller is responsible for cleaning up the reference to the
   2188  *	returned MD (mdpp) by calling md_fini_handle().
   2189  *
   2190  * Arguments:
   2191  *	dip	- dev info pointer for this instance of the device driver.
   2192  *	mdpp	- the returned MD.
   2193  *	vd_nodep - the returned device node.
   2194  *
   2195  * Return Code:
   2196  *	0	- Success.
   2197  *	ENOENT	- Expected node or property did not exist.
   2198  *	ENXIO	- Unexpected error communicating with MD framework
   2199  */
   2200 static int
   2201 vdc_get_md_node(dev_info_t *dip, md_t **mdpp, mde_cookie_t *vd_nodep)
   2202 {
   2203 	int		status = ENOENT;
   2204 	char		*node_name = NULL;
   2205 	md_t		*mdp = NULL;
   2206 	int		num_nodes;
   2207 	int		num_vdevs;
   2208 	mde_cookie_t	rootnode;
   2209 	mde_cookie_t	*listp = NULL;
   2210 	boolean_t	found_inst = B_FALSE;
   2211 	int		listsz;
   2212 	int		idx;
   2213 	uint64_t	md_inst;
   2214 	int		obp_inst;
   2215 	int		instance = ddi_get_instance(dip);
   2216 
   2217 	/*
   2218 	 * Get the OBP instance number for comparison with the MD instance
   2219 	 *
   2220 	 * The "cfg-handle" property of a vdc node in an MD contains the MD's
   2221 	 * notion of "instance", or unique identifier, for that node; OBP
   2222 	 * stores the value of the "cfg-handle" MD property as the value of
   2223 	 * the "reg" property on the node in the device tree it builds from
   2224 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
   2225 	 * "reg" property value to uniquely identify this device instance.
   2226 	 * If the "reg" property cannot be found, the device tree state is
   2227 	 * presumably so broken that there is no point in continuing.
   2228 	 */
   2229 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, OBP_REG)) {
   2230 		cmn_err(CE_WARN, "'%s' property does not exist", OBP_REG);
   2231 		return (ENOENT);
   2232 	}
   2233 	obp_inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
   2234 	    OBP_REG, -1);
   2235 	DMSGX(1, "[%d] OBP inst=%d\n", instance, obp_inst);
   2236 
   2237 	/*
   2238 	 * We now walk the MD nodes to find the node for this vdisk.
   2239 	 */
   2240 	if ((mdp = md_get_handle()) == NULL) {
   2241 		cmn_err(CE_WARN, "unable to init machine description");
   2242 		return (ENXIO);
   2243 	}
   2244 
   2245 	num_nodes = md_node_count(mdp);
   2246 	ASSERT(num_nodes > 0);
   2247 
   2248 	listsz = num_nodes * sizeof (mde_cookie_t);
   2249 
   2250 	/* allocate memory for nodes */
   2251 	listp = kmem_zalloc(listsz, KM_SLEEP);
   2252 
   2253 	rootnode = md_root_node(mdp);
   2254 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
   2255 
   2256 	/*
   2257 	 * Search for all the virtual devices, we will then check to see which
   2258 	 * ones are disk nodes.
   2259 	 */
   2260 	num_vdevs = md_scan_dag(mdp, rootnode,
   2261 	    md_find_name(mdp, VDC_MD_VDEV_NAME),
   2262 	    md_find_name(mdp, "fwd"), listp);
   2263 
   2264 	if (num_vdevs <= 0) {
   2265 		cmn_err(CE_NOTE, "No '%s' node found", VDC_MD_VDEV_NAME);
   2266 		status = ENOENT;
   2267 		goto done;
   2268 	}
   2269 
   2270 	DMSGX(1, "[%d] num_vdevs=%d\n", instance, num_vdevs);
   2271 	for (idx = 0; idx < num_vdevs; idx++) {
   2272 		status = md_get_prop_str(mdp, listp[idx], "name", &node_name);
   2273 		if ((status != 0) || (node_name == NULL)) {
   2274 			cmn_err(CE_NOTE, "Unable to get name of node type '%s'"
   2275 			    ": err %d", VDC_MD_VDEV_NAME, status);
   2276 			continue;
   2277 		}
   2278 
   2279 		DMSGX(1, "[%d] Found node '%s'\n", instance, node_name);
   2280 		if (strcmp(VDC_MD_DISK_NAME, node_name) == 0) {
   2281 			status = md_get_prop_val(mdp, listp[idx],
   2282 			    VDC_MD_CFG_HDL, &md_inst);
   2283 			DMSGX(1, "[%d] vdc inst in MD=%lx\n",
   2284 			    instance, md_inst);
   2285 			if ((status == 0) && (md_inst == obp_inst)) {
   2286 				found_inst = B_TRUE;
   2287 				break;
   2288 			}
   2289 		}
   2290 	}
   2291 
   2292 	if (!found_inst) {
   2293 		DMSGX(0, "Unable to find correct '%s' node", VDC_MD_DISK_NAME);
   2294 		status = ENOENT;
   2295 		goto done;
   2296 	}
   2297 	DMSGX(0, "[%d] MD inst=%lx\n", instance, md_inst);
   2298 
   2299 	*vd_nodep = listp[idx];
   2300 	*mdpp = mdp;
   2301 done:
   2302 	kmem_free(listp, listsz);
   2303 	return (status);
   2304 }
   2305 
   2306 /*
   2307  * Function:
   2308  *	vdc_init_ports
   2309  *
   2310  * Description:
   2311  *	Initialize all the ports for this vdisk instance.
   2312  *
   2313  * Arguments:
   2314  *	vdc	- soft state pointer for this instance of the device driver.
   2315  *	mdp	- md pointer
   2316  *	vd_nodep - device md node.
   2317  *
   2318  * Return Code:
   2319  *	0	- Success.
   2320  *	ENOENT	- Expected node or property did not exist.
   2321  */
   2322 static int
   2323 vdc_init_ports(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_nodep)
   2324 {
   2325 	int		status = 0;
   2326 	int		idx;
   2327 	int		num_nodes;
   2328 	int		num_vports;
   2329 	int		num_chans;
   2330 	int		listsz;
   2331 	mde_cookie_t	vd_port;
   2332 	mde_cookie_t	*chanp = NULL;
   2333 	mde_cookie_t	*portp = NULL;
   2334 	vdc_server_t	*srvr;
   2335 	vdc_server_t	*prev_srvr = NULL;
   2336 
   2337 	/*
   2338 	 * We now walk the MD nodes to find the port nodes for this vdisk.
   2339 	 */
   2340 	num_nodes = md_node_count(mdp);
   2341 	ASSERT(num_nodes > 0);
   2342 
   2343 	listsz = num_nodes * sizeof (mde_cookie_t);
   2344 
   2345 	/* allocate memory for nodes */
   2346 	portp = kmem_zalloc(listsz, KM_SLEEP);
   2347 	chanp = kmem_zalloc(listsz, KM_SLEEP);
   2348 
   2349 	num_vports = md_scan_dag(mdp, vd_nodep,
   2350 	    md_find_name(mdp, VDC_MD_PORT_NAME),
   2351 	    md_find_name(mdp, "fwd"), portp);
   2352 	if (num_vports == 0) {
   2353 		DMSGX(0, "Found no '%s' node for '%s' port\n",
   2354 		    VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME);
   2355 		status = ENOENT;
   2356 		goto done;
   2357 	}
   2358 
   2359 	DMSGX(1, "Found %d '%s' node(s) for '%s' port\n",
   2360 	    num_vports, VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME);
   2361 
   2362 	vdc->num_servers = 0;
   2363 	for (idx = 0; idx < num_vports; idx++) {
   2364 
   2365 		/* initialize this port */
   2366 		vd_port = portp[idx];
   2367 		srvr = kmem_zalloc(sizeof (vdc_server_t), KM_SLEEP);
   2368 		srvr->vdcp = vdc;
   2369 		srvr->svc_state = VDC_SERVICE_OFFLINE;
   2370 		srvr->log_state = VDC_SERVICE_NONE;
   2371 
   2372 		/* get port id */
   2373 		if (md_get_prop_val(mdp, vd_port, VDC_MD_ID, &srvr->id) != 0) {
   2374 			cmn_err(CE_NOTE, "vDisk port '%s' property not found",
   2375 			    VDC_MD_ID);
   2376 			kmem_free(srvr, sizeof (vdc_server_t));
   2377 			continue;
   2378 		}
   2379 
   2380 		/* set the connection timeout */
   2381 		if (md_get_prop_val(mdp, vd_port, VDC_MD_TIMEOUT,
   2382 		    &srvr->ctimeout) != 0) {
   2383 			srvr->ctimeout = 0;
   2384 		}
   2385 
   2386 		/* get the ldc id */
   2387 		num_chans = md_scan_dag(mdp, vd_port,
   2388 		    md_find_name(mdp, VDC_MD_CHAN_NAME),
   2389 		    md_find_name(mdp, "fwd"), chanp);
   2390 
   2391 		/* expecting at least one channel */
   2392 		if (num_chans <= 0) {
   2393 			cmn_err(CE_NOTE, "No '%s' node for '%s' port",
   2394 			    VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME);
   2395 			kmem_free(srvr, sizeof (vdc_server_t));
   2396 			continue;
   2397 		} else if (num_chans != 1) {
   2398 			DMSGX(0, "Expected 1 '%s' node for '%s' port, "
   2399 			    "found %d\n", VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME,
   2400 			    num_chans);
   2401 		}
   2402 
   2403 		/*
   2404 		 * We use the first channel found (index 0), irrespective of how
   2405 		 * many are there in total.
   2406 		 */
   2407 		if (md_get_prop_val(mdp, chanp[0], VDC_MD_ID,
   2408 		    &srvr->ldc_id) != 0) {
   2409 			cmn_err(CE_NOTE, "Channel '%s' property not found",
   2410 			    VDC_MD_ID);
   2411 			kmem_free(srvr, sizeof (vdc_server_t));
   2412 			continue;
   2413 		}
   2414 
   2415 		/*
   2416 		 * now initialise LDC channel which will be used to
   2417 		 * communicate with this server
   2418 		 */
   2419 		if (vdc_do_ldc_init(vdc, srvr) != 0) {
   2420 			kmem_free(srvr, sizeof (vdc_server_t));
   2421 			continue;
   2422 		}
   2423 
   2424 		/* add server to list */
   2425 		if (prev_srvr)
   2426 			prev_srvr->next = srvr;
   2427 		else
   2428 			vdc->server_list = srvr;
   2429 
   2430 		prev_srvr = srvr;
   2431 
   2432 		/* inc numbers of servers */
   2433 		vdc->num_servers++;
   2434 	}
   2435 
   2436 	/*
   2437 	 * Adjust the max number of handshake retries to match
   2438 	 * the number of vdisk servers.
   2439 	 */
   2440 	if (vdc_hshake_retries < vdc->num_servers)
   2441 		vdc_hshake_retries = vdc->num_servers;
   2442 
   2443 	/* pick first server as current server */
   2444 	if (vdc->server_list != NULL) {
   2445 		vdc->curr_server = vdc->server_list;
   2446 		status = 0;
   2447 	} else {
   2448 		status = ENOENT;
   2449 	}
   2450 
   2451 done:
   2452 	kmem_free(chanp, listsz);
   2453 	kmem_free(portp, listsz);
   2454 	return (status);
   2455 }
   2456 
   2457 
   2458 /*
   2459  * Function:
   2460  *	vdc_do_ldc_up
   2461  *
   2462  * Description:
   2463  *	Bring the channel for the current server up.
   2464  *
   2465  * Arguments:
   2466  *	vdc	- soft state pointer for this instance of the device driver.
   2467  *
   2468  * Return Code:
   2469  *	0		- Success.
   2470  *	EINVAL		- Driver is detaching / LDC error
   2471  *	ECONNREFUSED	- Other end is not listening
   2472  */
   2473 static int
   2474 vdc_do_ldc_up(vdc_t *vdc)
   2475 {
   2476 	int		status;
   2477 	ldc_status_t	ldc_state;
   2478 
   2479 	ASSERT(MUTEX_HELD(&vdc->lock));
   2480 
   2481 	DMSG(vdc, 0, "[%d] Bringing up channel %lx\n",
   2482 	    vdc->instance, vdc->curr_server->ldc_id);
   2483 
   2484 	if (vdc->lifecycle == VDC_LC_DETACHING)
   2485 		return (EINVAL);
   2486 
   2487 	if ((status = ldc_up(vdc->curr_server->ldc_handle)) != 0) {
   2488 		switch (status) {
   2489 		case ECONNREFUSED:	/* listener not ready at other end */
   2490 			DMSG(vdc, 0, "[%d] ldc_up(%lx,...) return %d\n",
   2491 			    vdc->instance, vdc->curr_server->ldc_id, status);
   2492 			status = 0;
   2493 			break;
   2494 		default:
   2495 			DMSG(vdc, 0, "[%d] Failed to bring up LDC: "
   2496 			    "channel=%ld, err=%d", vdc->instance,
   2497 			    vdc->curr_server->ldc_id, status);
   2498 			break;
   2499 		}
   2500 	}
   2501 
   2502 	if (ldc_status(vdc->curr_server->ldc_handle, &ldc_state) == 0) {
   2503 		vdc->curr_server->ldc_state = ldc_state;
   2504 		if (ldc_state == LDC_UP) {
   2505 			DMSG(vdc, 0, "[%d] LDC channel already up\n",
   2506 			    vdc->instance);
   2507 			vdc->seq_num = 1;
   2508 			vdc->seq_num_reply = 0;
   2509 		}
   2510 	}
   2511 
   2512 	return (status);
   2513 }
   2514 
   2515 /*
   2516  * Function:
   2517  *	vdc_terminate_ldc()
   2518  *
   2519  * Description:
   2520  *
   2521  * Arguments:
   2522  *	vdc	- soft state pointer for this instance of the device driver.
   2523  *	srvr	- vdc per-server info structure
   2524  *
   2525  * Return Code:
   2526  *	None
   2527  */
   2528 static void
   2529 vdc_terminate_ldc(vdc_t *vdc, vdc_server_t *srvr)
   2530 {
   2531 	int	instance = ddi_get_instance(vdc->dip);
   2532 
   2533 	if (srvr->state & VDC_LDC_OPEN) {
   2534 		DMSG(vdc, 0, "[%d] ldc_close()\n", instance);
   2535 		(void) ldc_close(srvr->ldc_handle);
   2536 	}
   2537 	if (srvr->state & VDC_LDC_CB) {
   2538 		DMSG(vdc, 0, "[%d] ldc_unreg_callback()\n", instance);
   2539 		(void) ldc_unreg_callback(srvr->ldc_handle);
   2540 	}
   2541 	if (srvr->state & VDC_LDC_INIT) {
   2542 		DMSG(vdc, 0, "[%d] ldc_fini()\n", instance);
   2543 		(void) ldc_fini(srvr->ldc_handle);
   2544 		srvr->ldc_handle = NULL;
   2545 	}
   2546 
   2547 	srvr->state &= ~(VDC_LDC_INIT | VDC_LDC_CB | VDC_LDC_OPEN);
   2548 }
   2549 
   2550 /*
   2551  * Function:
   2552  *	vdc_fini_ports()
   2553  *
   2554  * Description:
   2555  *	Finalize all ports by closing the channel associated with each
   2556  *	port and also freeing the server structure.
   2557  *
   2558  * Arguments:
   2559  *	vdc	- soft state pointer for this instance of the device driver.
   2560  *
   2561  * Return Code:
   2562  *	None
   2563  */
   2564 static void
   2565 vdc_fini_ports(vdc_t *vdc)
   2566 {
   2567 	int		instance = ddi_get_instance(vdc->dip);
   2568 	vdc_server_t	*srvr, *prev_srvr;
   2569 
   2570 	ASSERT(vdc != NULL);
   2571 	ASSERT(mutex_owned(&vdc->lock));
   2572 
   2573 	DMSG(vdc, 0, "[%d] initialized=%x\n", instance, vdc->initialized);
   2574 
   2575 	srvr = vdc->server_list;
   2576 
   2577 	while (srvr) {
   2578 
   2579 		vdc_terminate_ldc(vdc, srvr);
   2580 
   2581 		/* next server */
   2582 		prev_srvr = srvr;
   2583 		srvr = srvr->next;
   2584 
   2585 		/* free server */
   2586 		kmem_free(prev_srvr, sizeof (vdc_server_t));
   2587 	}
   2588 
   2589 	vdc->server_list = NULL;
   2590 	vdc->num_servers = 0;
   2591 }
   2592 
   2593 /* -------------------------------------------------------------------------- */
   2594 
   2595 /*
   2596  * Descriptor Ring helper routines
   2597  */
   2598 
   2599 /*
   2600  * Function:
   2601  *	vdc_init_descriptor_ring()
   2602  *
   2603  * Description:
   2604  *
   2605  * Arguments:
   2606  *	vdc	- soft state pointer for this instance of the device driver.
   2607  *
   2608  * Return Code:
   2609  *	0	- Success
   2610  */
   2611 static int
   2612 vdc_init_descriptor_ring(vdc_t *vdc)
   2613 {
   2614 	vd_dring_entry_t	*dep = NULL;	/* DRing Entry pointer */
   2615 	int	status = 0;
   2616 	int	i;
   2617 
   2618 	DMSG(vdc, 0, "[%d] initialized=%x\n", vdc->instance, vdc->initialized);
   2619 
   2620 	ASSERT(vdc != NULL);
   2621 	ASSERT(mutex_owned(&vdc->lock));
   2622 
   2623 	/* ensure we have enough room to store max sized block */
   2624 	ASSERT(maxphys <= VD_MAX_BLOCK_SIZE);
   2625 
   2626 	if ((vdc->initialized & VDC_DRING_INIT) == 0) {
   2627 		DMSG(vdc, 0, "[%d] ldc_mem_dring_create\n", vdc->instance);
   2628 		/*
   2629 		 * Calculate the maximum block size we can transmit using one
   2630 		 * Descriptor Ring entry from the attributes returned by the
   2631 		 * vDisk server. This is subject to a minimum of 'maxphys'
   2632 		 * as we do not have the capability to split requests over
   2633 		 * multiple DRing entries.
   2634 		 */
   2635 		if ((vdc->max_xfer_sz * vdc->vdisk_bsize) < maxphys) {
   2636 			DMSG(vdc, 0, "[%d] using minimum DRing size\n",
   2637 			    vdc->instance);
   2638 			vdc->dring_max_cookies = maxphys / PAGESIZE;
   2639 		} else {
   2640 			vdc->dring_max_cookies =
   2641 			    (vdc->max_xfer_sz * vdc->vdisk_bsize) / PAGESIZE;
   2642 		}
   2643 		vdc->dring_entry_size = (sizeof (vd_dring_entry_t) +
   2644 		    (sizeof (ldc_mem_cookie_t) *
   2645 		    (vdc->dring_max_cookies - 1)));
   2646 		vdc->dring_len = VD_DRING_LEN;
   2647 
   2648 		status = ldc_mem_dring_create(vdc->dring_len,
   2649 		    vdc->dring_entry_size, &vdc->dring_hdl);
   2650 		if ((vdc->dring_hdl == NULL) || (status != 0)) {
   2651 			DMSG(vdc, 0, "[%d] Descriptor ring creation failed",
   2652 			    vdc->instance);
   2653 			return (status);
   2654 		}
   2655 		vdc->initialized |= VDC_DRING_INIT;
   2656 	}
   2657 
   2658 	if ((vdc->initialized & VDC_DRING_BOUND) == 0) {
   2659 		DMSG(vdc, 0, "[%d] ldc_mem_dring_bind\n", vdc->instance);
   2660 		vdc->dring_cookie =
   2661 		    kmem_zalloc(sizeof (ldc_mem_cookie_t), KM_SLEEP);
   2662 
   2663 		status = ldc_mem_dring_bind(vdc->curr_server->ldc_handle,
   2664 		    vdc->dring_hdl,
   2665 		    LDC_SHADOW_MAP|LDC_DIRECT_MAP, LDC_MEM_RW,
   2666 		    &vdc->dring_cookie[0],
   2667 		    &vdc->dring_cookie_count);
   2668 		if (status != 0) {
   2669 			DMSG(vdc, 0, "[%d] Failed to bind descriptor ring "
   2670 			    "(%lx) to channel (%lx) status=%d\n",
   2671 			    vdc->instance, vdc->dring_hdl,
   2672 			    vdc->curr_server->ldc_handle, status);
   2673 			return (status);
   2674 		}
   2675 		ASSERT(vdc->dring_cookie_count == 1);
   2676 		vdc->initialized |= VDC_DRING_BOUND;
   2677 	}
   2678 
   2679 	status = ldc_mem_dring_info(vdc->dring_hdl, &vdc->dring_mem_info);
   2680 	if (status != 0) {
   2681 		DMSG(vdc, 0,
   2682 		    "[%d] Failed to get info for descriptor ring (%lx)\n",
   2683 		    vdc->instance, vdc->dring_hdl);
   2684 		return (status);
   2685 	}
   2686 
   2687 	if ((vdc->initialized & VDC_DRING_LOCAL) == 0) {
   2688 		DMSG(vdc, 0, "[%d] local dring\n", vdc->instance);
   2689 
   2690 		/* Allocate the local copy of this dring */
   2691 		vdc->local_dring =
   2692 		    kmem_zalloc(vdc->dring_len * sizeof (vdc_local_desc_t),
   2693 		    KM_SLEEP);
   2694 		vdc->initialized |= VDC_DRING_LOCAL;
   2695 	}
   2696 
   2697 	/*
   2698 	 * Mark all DRing entries as free and initialize the private
   2699 	 * descriptor's memory handles. If any entry is initialized,
   2700 	 * we need to free it later so we set the bit in 'initialized'
   2701 	 * at the start.
   2702 	 */
   2703 	vdc->initialized |= VDC_DRING_ENTRY;
   2704 	for (i = 0; i < vdc->dring_len; i++) {
   2705 		dep = VDC_GET_DRING_ENTRY_PTR(vdc, i);
   2706 		dep->hdr.dstate = VIO_DESC_FREE;
   2707 
   2708 		status = ldc_mem_alloc_handle(vdc->curr_server->ldc_handle,
   2709 		    &vdc->local_dring[i].desc_mhdl);
   2710 		if (status != 0) {
   2711 			DMSG(vdc, 0, "![%d] Failed to alloc mem handle for"
   2712 			    " descriptor %d", vdc->instance, i);
   2713 			return (status);
   2714 		}
   2715 		vdc->local_dring[i].is_free = B_TRUE;
   2716 		vdc->local_dring[i].dep = dep;
   2717 	}
   2718 
   2719 	/* Initialize the starting index */
   2720 	vdc->dring_curr_idx = 0;
   2721 
   2722 	return (status);
   2723 }
   2724 
   2725 /*
   2726  * Function:
   2727  *	vdc_destroy_descriptor_ring()
   2728  *
   2729  * Description:
   2730  *
   2731  * Arguments:
   2732  *	vdc	- soft state pointer for this instance of the device driver.
   2733  *
   2734  * Return Code:
   2735  *	None
   2736  */
   2737 static void
   2738 vdc_destroy_descriptor_ring(vdc_t *vdc)
   2739 {
   2740 	vdc_local_desc_t	*ldep = NULL;	/* Local Dring Entry Pointer */
   2741 	ldc_mem_handle_t	mhdl = NULL;
   2742 	ldc_mem_info_t		minfo;
   2743 	int			status = -1;
   2744 	int			i;	/* loop */
   2745 
   2746 	ASSERT(vdc != NULL);
   2747 	ASSERT(mutex_owned(&vdc->lock));
   2748 
   2749 	DMSG(vdc, 0, "[%d] Entered\n", vdc->instance);
   2750 
   2751 	if (vdc->initialized & VDC_DRING_ENTRY) {
   2752 		DMSG(vdc, 0,
   2753 		    "[%d] Removing Local DRing entries\n", vdc->instance);
   2754 		for (i = 0; i < vdc->dring_len; i++) {
   2755 			ldep = &vdc->local_dring[i];
   2756 			mhdl = ldep->desc_mhdl;
   2757 
   2758 			if (mhdl == NULL)
   2759 				continue;
   2760 
   2761 			if ((status = ldc_mem_info(mhdl, &minfo)) != 0) {
   2762 				DMSG(vdc, 0,
   2763 				    "ldc_mem_info returned an error: %d\n",
   2764 				    status);
   2765 
   2766 				/*
   2767 				 * This must mean that the mem handle
   2768 				 * is not valid. Clear it out so that
   2769 				 * no one tries to use it.
   2770 				 */
   2771 				ldep->desc_mhdl = NULL;
   2772 				continue;
   2773 			}
   2774 
   2775 			if (minfo.status == LDC_BOUND) {
   2776 				(void) ldc_mem_unbind_handle(mhdl);
   2777 			}
   2778 
   2779 			(void) ldc_mem_free_handle(mhdl);
   2780 
   2781 			ldep->desc_mhdl = NULL;
   2782 		}
   2783 		vdc->initialized &= ~VDC_DRING_ENTRY;
   2784 	}
   2785 
   2786 	if (vdc->initialized & VDC_DRING_LOCAL) {
   2787 		DMSG(vdc, 0, "[%d] Freeing Local DRing\n", vdc->instance);
   2788 		kmem_free(vdc->local_dring,
   2789 		    vdc->dring_len * sizeof (vdc_local_desc_t));
   2790 		vdc->initialized &= ~VDC_DRING_LOCAL;
   2791 	}
   2792 
   2793 	if (vdc->initialized & VDC_DRING_BOUND) {
   2794 		DMSG(vdc, 0, "[%d] Unbinding DRing\n", vdc->instance);
   2795 		status = ldc_mem_dring_unbind(vdc->dring_hdl);
   2796 		if (status == 0) {
   2797 			vdc->initialized &= ~VDC_DRING_BOUND;
   2798 		} else {
   2799 			DMSG(vdc, 0, "[%d] Error %d unbinding DRing %lx",
   2800 			    vdc->instance, status, vdc->dring_hdl);
   2801 		}
   2802 		kmem_free(vdc->dring_cookie, sizeof (ldc_mem_cookie_t));
   2803 	}
   2804 
   2805 	if (vdc->initialized & VDC_DRING_INIT) {
   2806 		DMSG(vdc, 0, "[%d] Destroying DRing\n", vdc->instance);
   2807 		status = ldc_mem_dring_destroy(vdc->dring_hdl);
   2808 		if (status == 0) {
   2809 			vdc->dring_hdl = NULL;
   2810 			bzero(&vdc->dring_mem_info, sizeof (ldc_mem_info_t));
   2811 			vdc->initialized &= ~VDC_DRING_INIT;
   2812 		} else {
   2813 			DMSG(vdc, 0, "[%d] Error %d destroying DRing (%lx)",
   2814 			    vdc->instance, status, vdc->dring_hdl);
   2815 		}
   2816 	}
   2817 }
   2818 
   2819 /*
   2820  * Function:
   2821  *	vdc_map_to_shared_dring()
   2822  *
   2823  * Description:
   2824  *	Copy contents of the local descriptor to the shared
   2825  *	memory descriptor.
   2826  *
   2827  * Arguments:
   2828  *	vdcp	- soft state pointer for this instance of the device driver.
   2829  *	idx	- descriptor ring index
   2830  *
   2831  * Return Code:
   2832  *	None
   2833  */
   2834 static int
   2835 vdc_map_to_shared_dring(vdc_t *vdcp, int idx)
   2836 {
   2837 	vdc_local_desc_t	*ldep;
   2838 	vd_dring_entry_t	*dep;
   2839 	int			rv;
   2840 
   2841 	ldep = &(vdcp->local_dring[idx]);
   2842 
   2843 	/* for now leave in the old pop_mem_hdl stuff */
   2844 	if (ldep->nbytes > 0) {
   2845 		rv = vdc_populate_mem_hdl(vdcp, ldep);
   2846 		if (rv) {
   2847 			DMSG(vdcp, 0, "[%d] Cannot populate mem handle\n",
   2848 			    vdcp->instance);
   2849 			return (rv);
   2850 		}
   2851 	}
   2852 
   2853 	/*
   2854 	 * fill in the data details into the DRing
   2855 	 */
   2856 	dep = ldep->dep;
   2857 	ASSERT(dep != NULL);
   2858 
   2859 	dep->payload.req_id = VDC_GET_NEXT_REQ_ID(vdcp);
   2860 	dep->payload.operation = ldep->operation;
   2861 	dep->payload.addr = ldep->offset;
   2862 	dep->payload.nbytes = ldep->nbytes;
   2863 	dep->payload.status = (uint32_t)-1;	/* vds will set valid value */
   2864 	dep->payload.slice = ldep->slice;
   2865 	dep->hdr.dstate = VIO_DESC_READY;
   2866 	dep->hdr.ack = 1;		/* request an ACK for every message */
   2867 
   2868 	return (0);
   2869 }
   2870 
   2871 /*
   2872  * Function:
   2873  *	vdc_send_request
   2874  *
   2875  * Description:
   2876  *	This routine writes the data to be transmitted to vds into the
   2877  *	descriptor, notifies vds that the ring has been updated and
   2878  *	then waits for the request to be processed.
   2879  *
   2880  * Arguments:
   2881  *	vdcp	  - the soft state pointer
   2882  *	operation - operation we want vds to perform (VD_OP_XXX)
   2883  *	addr	  - address of data buf to be read/written.
   2884  *	nbytes	  - number of bytes to read/write
   2885  *	slice	  - the disk slice this request is for
   2886  *	offset	  - relative disk offset
   2887  *	bufp	  - buf of operation
   2888  *	dir	  - direction of operation (READ/WRITE/BOTH)
   2889  *
   2890  * Return Codes:
   2891  *	0
   2892  *	ENXIO
   2893  */
   2894 static int
   2895 vdc_send_request(vdc_t *vdcp, int operation, caddr_t addr,
   2896     size_t nbytes, int slice, diskaddr_t offset, buf_t *bufp,
   2897     vio_desc_direction_t dir, int flags)
   2898 {
   2899 	int	rv = 0;
   2900 
   2901 	ASSERT(vdcp != NULL);
   2902 	ASSERT(slice == VD_SLICE_NONE || slice < V_NUMPAR);
   2903 
   2904 	mutex_enter(&vdcp->lock);
   2905 
   2906 	/*
   2907 	 * If this is a block read/write operation we update the I/O statistics
   2908 	 * to indicate that the request is being put on the waitq to be
   2909 	 * serviced.
   2910 	 *
   2911 	 * We do it here (a common routine for both synchronous and strategy
   2912 	 * calls) for performance reasons - we are already holding vdc->lock
   2913 	 * so there is no extra locking overhead. We would have to explicitly
   2914 	 * grab the 'lock' mutex to update the stats if we were to do this
   2915 	 * higher up the stack in vdc_strategy() et. al.
   2916 	 */
   2917 	if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) {
   2918 		DTRACE_IO1(start, buf_t *, bufp);
   2919 		VD_KSTAT_WAITQ_ENTER(vdcp);
   2920 	}
   2921 
   2922 	/*
   2923 	 * If the request does not expect the state to be VDC_STATE_RUNNING
   2924 	 * then we just try to populate the descriptor ring once.
   2925 	 */
   2926 	if (!(flags & VDC_OP_STATE_RUNNING)) {
   2927 		rv = vdc_populate_descriptor(vdcp, operation, addr,
   2928 		    nbytes, slice, offset, bufp, dir, flags);
   2929 		goto done;
   2930 	}
   2931 
   2932 	do {
   2933 		while (vdcp->state != VDC_STATE_RUNNING) {
   2934 
   2935 			/* return error if detaching */
   2936 			if (vdcp->state == VDC_STATE_DETACH) {
   2937 				rv = ENXIO;
   2938 				goto done;
   2939 			}
   2940 
   2941 			/*
   2942 			 * If we are panicking and the disk is not ready then
   2943 			 * we can't send any request because we can't complete
   2944 			 * the handshake now.
   2945 			 */
   2946 			if (ddi_in_panic()) {
   2947 				rv = EIO;
   2948 				goto done;
   2949 			}
   2950 
   2951 			/*
   2952 			 * If the state is faulted, notify that a new I/O is
   2953 			 * being submitted to force the system to check if any
   2954 			 * server has recovered.
   2955 			 */
   2956 			if (vdcp->state == VDC_STATE_FAILED) {
   2957 				vdcp->io_pending = B_TRUE;
   2958 				cv_signal(&vdcp->io_pending_cv);
   2959 			}
   2960 
   2961 			cv_wait(&vdcp->running_cv, &vdcp->lock);
   2962 
   2963 			/* if service is still faulted then fail the request */
   2964 			if (vdcp->state == VDC_STATE_FAILED) {
   2965 				rv = EIO;
   2966 				goto done;
   2967 			}
   2968 		}
   2969 
   2970 	} while (vdc_populate_descriptor(vdcp, operation, addr,
   2971 	    nbytes, slice, offset, bufp, dir, flags));
   2972 
   2973 done:
   2974 	/*
   2975 	 * If this is a block read/write we update the I/O statistics kstat
   2976 	 * to indicate that this request has been placed on the queue for
   2977 	 * processing (i.e sent to the vDisk server) - iostat(1M) will
   2978 	 * report the time waiting for the vDisk server under the %b column
   2979 	 * In the case of an error we simply take it off the wait queue.
   2980 	 */
   2981 	if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) {
   2982 		if (rv == 0) {
   2983 			VD_KSTAT_WAITQ_TO_RUNQ(vdcp);
   2984 			DTRACE_PROBE1(send, buf_t *, bufp);
   2985 		} else {
   2986 			VD_UPDATE_ERR_STATS(vdcp, vd_transerrs);
   2987 			VD_KSTAT_WAITQ_EXIT(vdcp);
   2988 			DTRACE_IO1(done, buf_t *, bufp);
   2989 		}
   2990 	}
   2991 
   2992 	mutex_exit(&vdcp->lock);
   2993 
   2994 	return (rv);
   2995 }
   2996 
   2997 
   2998 /*
   2999  * Function:
   3000  *	vdc_populate_descriptor
   3001  *
   3002  * Description:
   3003  *	This routine writes the data to be transmitted to vds into the
   3004  *	descriptor, notifies vds that the ring has been updated and
   3005  *	then waits for the request to be processed.
   3006  *
   3007  * Arguments:
   3008  *	vdcp	  - the soft state pointer
   3009  *	operation - operation we want vds to perform (VD_OP_XXX)
   3010  *	addr	  - address of data buf to be read/written.
   3011  *	nbytes	  - number of bytes to read/write
   3012  *	slice	  - the disk slice this request is for
   3013  *	offset	  - relative disk offset
   3014  *	bufp	  - buf of operation
   3015  *	dir	  - direction of operation (READ/WRITE/BOTH)
   3016  *
   3017  * Return Codes:
   3018  *	0
   3019  *	EAGAIN
   3020  *	ECONNRESET
   3021  *	ENXIO
   3022  */
   3023 static int
   3024 vdc_populate_descriptor(vdc_t *vdcp, int operation, caddr_t addr,
   3025     size_t nbytes, int slice, diskaddr_t offset,
   3026     buf_t *bufp, vio_desc_direction_t dir, int flags)
   3027 {
   3028 	vdc_local_desc_t	*local_dep = NULL; /* Local Dring Pointer */
   3029 	int			idx;		/* Index of DRing entry used */
   3030 	int			next_idx;
   3031 	vio_dring_msg_t		dmsg;
   3032 	size_t			msglen;
   3033 	int			rv;
   3034 
   3035 	ASSERT(MUTEX_HELD(&vdcp->lock));
   3036 	vdcp->threads_pending++;
   3037 loop:
   3038 	DMSG(vdcp, 2, ": dring_curr_idx = %d\n", vdcp->dring_curr_idx);
   3039 
   3040 	/* Get next available D-Ring entry */
   3041 	idx = vdcp->dring_curr_idx;
   3042 	local_dep = &(vdcp->local_dring[idx]);
   3043 
   3044 	if (!local_dep->is_free) {
   3045 		DMSG(vdcp, 2, "[%d]: dring full - waiting for space\n",
   3046 		    vdcp->instance);
   3047 		cv_wait(&vdcp->dring_free_cv, &vdcp->lock);
   3048 		if (vdcp->state == VDC_STATE_RUNNING ||
   3049 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
   3050 			goto loop;
   3051 		}
   3052 		vdcp->threads_pending--;
   3053 		return (ECONNRESET);
   3054 	}
   3055 
   3056 	next_idx = idx + 1;
   3057 	if (next_idx >= vdcp->dring_len)
   3058 		next_idx = 0;
   3059 	vdcp->dring_curr_idx = next_idx;
   3060 
   3061 	ASSERT(local_dep->is_free);
   3062 
   3063 	local_dep->operation = operation;
   3064 	local_dep->addr = addr;
   3065 	local_dep->nbytes = nbytes;
   3066 	local_dep->slice = slice;
   3067 	local_dep->offset = offset;
   3068 	local_dep->buf = bufp;
   3069 	local_dep->dir = dir;
   3070 	local_dep->flags = flags;
   3071 
   3072 	local_dep->is_free = B_FALSE;
   3073 
   3074 	rv = vdc_map_to_shared_dring(vdcp, idx);
   3075 	if (rv) {
   3076 		DMSG(vdcp, 0, "[%d]: cannot bind memory - waiting ..\n",
   3077 		    vdcp->instance);
   3078 		/* free the descriptor */
   3079 		local_dep->is_free = B_TRUE;
   3080 		vdcp->dring_curr_idx = idx;
   3081 		cv_wait(&vdcp->membind_cv, &vdcp->lock);
   3082 		if (vdcp->state == VDC_STATE_RUNNING ||
   3083 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
   3084 			goto loop;
   3085 		}
   3086 		vdcp->threads_pending--;
   3087 		return (ECONNRESET);
   3088 	}
   3089 
   3090 	/*
   3091 	 * Send a msg with the DRing details to vds
   3092 	 */
   3093 	VIO_INIT_DRING_DATA_TAG(dmsg);
   3094 	VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdcp);
   3095 	dmsg.dring_ident = vdcp->dring_ident;
   3096 	dmsg.start_idx = idx;
   3097 	dmsg.end_idx = idx;
   3098 	vdcp->seq_num++;
   3099 
   3100 	DTRACE_PROBE2(populate, int, vdcp->instance,
   3101 	    vdc_local_desc_t *, local_dep);
   3102 	DMSG(vdcp, 2, "ident=0x%lx, st=%u, end=%u, seq=%ld\n",
   3103 	    vdcp->dring_ident, dmsg.start_idx, dmsg.end_idx, dmsg.seq_num);
   3104 
   3105 	/*
   3106 	 * note we're still holding the lock here to
   3107 	 * make sure the message goes out in order !!!...
   3108 	 */
   3109 	msglen = sizeof (dmsg);
   3110 	rv = vdc_send(vdcp, (caddr_t)&dmsg, &msglen);
   3111 	switch (rv) {
   3112 	case ECONNRESET:
   3113 		/*
   3114 		 * vdc_send initiates the reset on failure.
   3115 		 * Since the transaction has already been put
   3116 		 * on the local dring, it will automatically get
   3117 		 * retried when the channel is reset. Given that,
   3118 		 * it is ok to just return success even though the
   3119 		 * send failed.
   3120 		 */
   3121 		rv = 0;
   3122 		break;
   3123 
   3124 	case 0: /* EOK */
   3125 		DMSG(vdcp, 1, "sent via LDC: rv=%d\n", rv);
   3126 		break;
   3127 
   3128 	default:
   3129 		goto cleanup_and_exit;
   3130 	}
   3131 
   3132 	vdcp->threads_pending--;
   3133 	return (rv);
   3134 
   3135 cleanup_and_exit:
   3136 	DMSG(vdcp, 0, "unexpected error, rv=%d\n", rv);
   3137 	return (ENXIO);
   3138 }
   3139 
   3140 /*
   3141  * Function:
   3142  *	vdc_do_op
   3143  *
   3144  * Description:
   3145  * 	Wrapper around vdc_submit_request(). Each request is associated with a
   3146  *	buf structure. If a buf structure is provided (bufp != NULL) then the
   3147  *	request will be submitted with that buf, and the caller can wait for
   3148  *	completion of the request with biowait(). If a buf structure is not
   3149  *	provided (bufp == NULL) then a buf structure is created and the function
   3150  *	waits for the completion of the request.
   3151  *
   3152  *	If the flag VD_OP_STATE_RUNNING is set then vdc_submit_request() will
   3153  *	submit the request only when the vdisk is in state VD_STATE_RUNNING.
   3154  *	If the vdisk is not in that state then the vdc_submit_request() will
   3155  *	wait for that state to be reached. After the request is submitted, the
   3156  *	reply will be processed asynchronously by the vdc_process_msg_thread()
   3157  *	thread.
   3158  *
   3159  *	If the flag VD_OP_STATE_RUNNING is not set then vdc_submit_request()
   3160  *	submit the request whatever the state of the vdisk is. Then vdc_do_op()
   3161  *	will wait for a reply message, process the reply and complete the
   3162  *	request.
   3163  *
   3164  * Arguments:
   3165  *	vdc	- the soft state pointer
   3166  *	op	- operation we want vds to perform (VD_OP_XXX)
   3167  *	addr	- address of data buf to be read/written.
   3168  *	nbytes	- number of bytes to read/write
   3169  *	slice	- the disk slice this request is for
   3170  *	offset	- relative disk offset
   3171  *	bufp	- buf structure associated with the request (can be NULL).
   3172  *	dir	- direction of operation (READ/WRITE/BOTH)
   3173  *	flags	- flags for the request.
   3174  *
   3175  * Return Codes:
   3176  *	0	- the request has been succesfully submitted and completed.
   3177  *	!= 0	- the request has failed. In that case, if a buf structure
   3178  *		  was provided (bufp != NULL) then the B_ERROR flag is set
   3179  *		  and the b_error field of the buf structure is set to EIO.
   3180  */
   3181 static int
   3182 vdc_do_op(vdc_t *vdc, int op, caddr_t addr, size_t nbytes, int slice,
   3183     diskaddr_t offset, struct buf *bufp, vio_desc_direction_t dir, int flags)
   3184 {
   3185 	vio_msg_t vio_msg;
   3186 	struct buf buf;
   3187 	int rv;
   3188 
   3189 	if (bufp == NULL) {
   3190 		/*
   3191 		 * We use buf just as a convenient way to get a notification
   3192 		 * that the request is completed, so we initialize buf to the
   3193 		 * minimum we need.
   3194 		 */
   3195 		bioinit(&buf);
   3196 		buf.b_bcount = nbytes;
   3197 		buf.b_flags = B_BUSY;
   3198 		bufp = &buf;
   3199 	}
   3200 
   3201 	rv = vdc_send_request(vdc, op, addr, nbytes, slice, offset, bufp,
   3202 	    dir, flags);
   3203 
   3204 	if (rv != 0)
   3205 		goto done;
   3206 
   3207 	/*
   3208 	 * If the request should be done in VDC_STATE_RUNNING state then the
   3209 	 * reply will be received and processed by vdc_process_msg_thread()
   3210 	 * and we just have to handle the panic case. Otherwise we have to
   3211 	 * wait for the reply message and process it.
   3212 	 */
   3213 	if (flags & VDC_OP_STATE_RUNNING) {
   3214 
   3215 		if (ddi_in_panic()) {
   3216 			rv = vdc_drain_response(vdc, bufp);
   3217 			goto done;
   3218 		}
   3219 
   3220 	} else {
   3221 		/* wait for the response message */
   3222 		rv = vdc_wait_for_response(vdc, &vio_msg);
   3223 		if (rv) {
   3224 			/*
   3225 			 * If this is a block read/write we update the I/O
   3226 			 * statistics kstat to take it off the run queue.
   3227 			 */
   3228 			mutex_enter(&vdc->lock);
   3229 			if (op == VD_OP_BREAD || op == VD_OP_BWRITE) {
   3230 				VD_UPDATE_ERR_STATS(vdc, vd_transerrs);
   3231 				VD_KSTAT_RUNQ_EXIT(vdc);
   3232 				DTRACE_IO1(done, buf_t *, bufp);
   3233 			}
   3234 			mutex_exit(&vdc->lock);
   3235 			goto done;
   3236 		}
   3237 
   3238 		rv = vdc_process_data_msg(vdc, &vio_msg);
   3239 		if (rv)
   3240 			goto done;
   3241 	}
   3242 
   3243 	if (bufp == &buf)
   3244 		rv = biowait(bufp);
   3245 
   3246 done:
   3247 	if (bufp == &buf) {
   3248 		biofini(bufp);
   3249 	} else if (rv != 0) {
   3250 		bioerror(bufp, EIO);
   3251 		biodone(bufp);
   3252 	}
   3253 
   3254 	return (rv);
   3255 }
   3256 
   3257 /*
   3258  * Function:
   3259  *	vdc_do_sync_op
   3260  *
   3261  * Description:
   3262  * 	Wrapper around vdc_do_op that serializes requests.
   3263  *
   3264  * Arguments:
   3265  *	vdcp	  - the soft state pointer
   3266  *	operation - operation we want vds to perform (VD_OP_XXX)
   3267  *	addr	  - address of data buf to be read/written.
   3268  *	nbytes	  - number of bytes to read/write
   3269  *	slice	  - the disk slice this request is for
   3270  *	offset	  - relative disk offset
   3271  *	dir	  - direction of operation (READ/WRITE/BOTH)
   3272  *	rconflict - check for reservation conflict in case of failure
   3273  *
   3274  * rconflict should be set to B_TRUE by most callers. Callers invoking the
   3275  * VD_OP_SCSICMD operation can set rconflict to B_FALSE if they check the
   3276  * result of a successful operation with vdc_scsi_status().
   3277  *
   3278  * Return Codes:
   3279  *	0
   3280  *	EAGAIN
   3281  *	EFAULT
   3282  *	ENXIO
   3283  *	EIO
   3284  */
   3285 static int
   3286 vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr, size_t nbytes,
   3287     int slice, diskaddr_t offset, vio_desc_direction_t dir, boolean_t rconflict)
   3288 {
   3289 	int status;
   3290 	int flags = VDC_OP_NORMAL;
   3291 
   3292 	/*
   3293 	 * Grab the lock, if blocked wait until the server
   3294 	 * response causes us to wake up again.
   3295 	 */
   3296 	mutex_enter(&vdcp->lock);
   3297 	vdcp->sync_op_cnt++;
   3298 	while (vdcp->sync_op_blocked && vdcp->state != VDC_STATE_DETACH) {
   3299 		if (ddi_in_panic()) {
   3300 			/* don't block if we are panicking */
   3301 			vdcp->sync_op_cnt--;
   3302 			mutex_exit(&vdcp->lock);
   3303 			return (EIO);
   3304 		} else {
   3305 			cv_wait(&vdcp->sync_blocked_cv, &vdcp->lock);
   3306 		}
   3307 	}
   3308 
   3309 	if (vdcp->state == VDC_STATE_DETACH) {
   3310 		cv_broadcast(&vdcp->sync_blocked_cv);
   3311 		vdcp->sync_op_cnt--;
   3312 		mutex_exit(&vdcp->lock);
   3313 		return (ENXIO);
   3314 	}
   3315 
   3316 	/* now block anyone other thread entering after us */
   3317 	vdcp->sync_op_blocked = B_TRUE;
   3318 
   3319 	mutex_exit(&vdcp->lock);
   3320 
   3321 	if (!rconflict)
   3322 		flags &= ~VDC_OP_ERRCHK_CONFLICT;
   3323 
   3324 	status = vdc_do_op(vdcp, operation, addr, nbytes, slice, offset,
   3325 	    NULL, dir, flags);
   3326 
   3327 	mutex_enter(&vdcp->lock);
   3328 
   3329 	DMSG(vdcp, 2, ": operation returned %d\n", status);
   3330 
   3331 	if (vdcp->state == VDC_STATE_DETACH) {
   3332 		status = ENXIO;
   3333 	}
   3334 
   3335 	vdcp->sync_op_blocked = B_FALSE;
   3336 	vdcp->sync_op_cnt--;
   3337 
   3338 	/* signal the next waiting thread */
   3339 	cv_signal(&vdcp->sync_blocked_cv);
   3340 
   3341 	mutex_exit(&vdcp->lock);
   3342 
   3343 	return (status);
   3344 }
   3345 
   3346 
   3347 /*
   3348  * Function:
   3349  *	vdc_drain_response()
   3350  *
   3351  * Description:
   3352  * 	When a guest is panicking, the completion of requests needs to be
   3353  * 	handled differently because interrupts are disabled and vdc
   3354  * 	will not get messages. We have to poll for the messages instead.
   3355  *
   3356  *	Note: since we are panicking we don't implement	the io:::done
   3357  *	DTrace probe or update the I/O statistics kstats.
   3358  *
   3359  * Arguments:
   3360  *	vdc	- soft state pointer for this instance of the device driver.
   3361  *	buf	- if buf is NULL then we drain all responses, otherwise we
   3362  *		  poll until we receive a ACK/NACK for the specific I/O
   3363  *		  described by buf.
   3364  *
   3365  * Return Code:
   3366  *	0	- Success. If we were expecting a response to a particular
   3367  *		  request then this means that a response has been received.
   3368  */
   3369 static int
   3370 vdc_drain_response(vdc_t *vdc, struct buf *buf)
   3371 {
   3372 	int 			rv, idx, retries;
   3373 	size_t			msglen;
   3374 	vdc_local_desc_t 	*ldep = NULL;	/* Local Dring Entry Pointer */
   3375 	vio_dring_msg_t		dmsg;
   3376 	struct buf		*mbuf;
   3377 	boolean_t		ack;
   3378 
   3379 	mutex_enter(&vdc->lock);
   3380 
   3381 	retries = 0;
   3382 	for (;;) {
   3383 		msglen = sizeof (dmsg);
   3384 		rv = ldc_read(vdc->curr_server->ldc_handle, (caddr_t)&dmsg,
   3385 		    &msglen);
   3386 		if (rv) {
   3387 			rv = EINVAL;
   3388 			break;
   3389 		}
   3390 
   3391 		/*
   3392 		 * if there are no packets wait and check again
   3393 		 */
   3394 		if ((rv == 0) && (msglen == 0)) {
   3395 			if (retries++ > vdc_dump_retries) {
   3396 				rv = EAGAIN;
   3397 				break;
   3398 			}
   3399 
   3400 			drv_usecwait(vdc_usec_timeout_dump);
   3401 			continue;
   3402 		}
   3403 
   3404 		/*
   3405 		 * Ignore all messages that are not ACKs/NACKs to
   3406 		 * DRing requests.
   3407 		 */
   3408 		if ((dmsg.tag.vio_msgtype != VIO_TYPE_DATA) ||
   3409 		    (dmsg.tag.vio_subtype_env != VIO_DRING_DATA)) {
   3410 			DMSG(vdc, 0, "discard pkt: type=%d sub=%d env=%d\n",
   3411 			    dmsg.tag.vio_msgtype,
   3412 			    dmsg.tag.vio_subtype,
   3413 			    dmsg.tag.vio_subtype_env);
   3414 			continue;
   3415 		}
   3416 
   3417 		/*
   3418 		 * Record if the packet was ACK'ed or not. If the packet was not
   3419 		 * ACK'ed then we will just mark the request as failed; we don't
   3420 		 * want to reset the connection at this point.
   3421 		 */
   3422 		switch (dmsg.tag.vio_subtype) {
   3423 		case VIO_SUBTYPE_ACK:
   3424 			ack = B_TRUE;
   3425 			break;
   3426 		case VIO_SUBTYPE_NACK:
   3427 			ack = B_FALSE;
   3428 			break;
   3429 		default:
   3430 			continue;
   3431 		}
   3432 
   3433 		idx = dmsg.start_idx;
   3434 		if (idx >= vdc->dring_len) {
   3435 			DMSG(vdc, 0, "[%d] Bogus ack data : start %d\n",
   3436 			    vdc->instance, idx);
   3437 			continue;
   3438 		}
   3439 		ldep = &vdc->local_dring[idx];
   3440 		if (ldep->dep->hdr.dstate != VIO_DESC_DONE) {
   3441 			DMSG(vdc, 0, "[%d] Entry @ %d - state !DONE %d\n",
   3442 			    vdc->instance, idx, ldep->dep->hdr.dstate);
   3443 			continue;
   3444 		}
   3445 
   3446 		mbuf = ldep->buf;
   3447 		ASSERT(mbuf != NULL);
   3448 		mbuf->b_resid = mbuf->b_bcount - ldep->dep->payload.nbytes;
   3449 		bioerror(mbuf, ack ? ldep->dep->payload.status : EIO);
   3450 		biodone(mbuf);
   3451 
   3452 		rv = vdc_depopulate_descriptor(vdc, idx);
   3453 		if (buf != NULL && buf == mbuf) {
   3454 			rv = 0;
   3455 			goto done;
   3456 		}
   3457 
   3458 		/* if this is the last descriptor - break out of loop */
   3459 		if ((idx + 1) % vdc->dring_len == vdc->dring_curr_idx) {
   3460 			/*
   3461 			 * If we were expecting a response for a particular
   3462 			 * request then we return with an error otherwise we
   3463 			 * have successfully completed the drain.
   3464 			 */
   3465 			rv = (buf != NULL)? ESRCH: 0;
   3466 			break;
   3467 		}
   3468 	}
   3469 
   3470 done:
   3471 	mutex_exit(&vdc->lock);
   3472 	DMSG(vdc, 0, "End idx=%d\n", idx);
   3473 
   3474 	return (rv);
   3475 }
   3476 
   3477 
   3478 /*
   3479  * Function:
   3480  *	vdc_depopulate_descriptor()
   3481  *
   3482  * Description:
   3483  *
   3484  * Arguments:
   3485  *	vdc	- soft state pointer for this instance of the device driver.
   3486  *	idx	- Index of the Descriptor Ring entry being modified
   3487  *
   3488  * Return Code:
   3489  *	0	- Success
   3490  */
   3491 static int
   3492 vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx)
   3493 {
   3494 	vd_dring_entry_t *dep = NULL;		/* Dring Entry Pointer */
   3495 	vdc_local_desc_t *ldep = NULL;		/* Local Dring Entry Pointer */
   3496 	int		status = ENXIO;
   3497 	int		rv = 0;
   3498 
   3499 	ASSERT(vdc != NULL);
   3500 	ASSERT(idx < vdc->dring_len);
   3501 	ldep = &vdc->local_dring[idx];
   3502 	ASSERT(ldep != NULL);
   3503 	ASSERT(MUTEX_HELD(&vdc->lock));
   3504 
   3505 	DTRACE_PROBE2(depopulate, int, vdc->instance, vdc_local_desc_t *, ldep);
   3506 	DMSG(vdc, 2, ": idx = %d\n", idx);
   3507 
   3508 	dep = ldep->dep;
   3509 	ASSERT(dep != NULL);
   3510 	ASSERT((dep->hdr.dstate == VIO_DESC_DONE) ||
   3511 	    (dep->payload.status == ECANCELED));
   3512 
   3513 	VDC_MARK_DRING_ENTRY_FREE(vdc, idx);
   3514 
   3515 	ldep->is_free = B_TRUE;
   3516 	status = dep->payload.status;
   3517 	DMSG(vdc, 2, ": is_free = %d : status = %d\n", ldep->is_free, status);
   3518 
   3519 	/*
   3520 	 * If no buffers were used to transfer information to the server when
   3521 	 * populating the descriptor then no memory handles need to be unbound
   3522 	 * and we can return now.
   3523 	 */
   3524 	if (ldep->nbytes == 0) {
   3525 		cv_signal(&vdc->dring_free_cv);
   3526 		return (status);
   3527 	}
   3528 
   3529 	/*
   3530 	 * If the upper layer passed in a misaligned address we copied the
   3531 	 * data into an aligned buffer before sending it to LDC - we now
   3532 	 * copy it back to the original buffer.
   3533 	 */
   3534 	if (ldep->align_addr) {
   3535 		ASSERT(ldep->addr != NULL);
   3536 
   3537 		if (dep->payload.nbytes > 0)
   3538 			bcopy(ldep->align_addr, ldep->addr,
   3539 			    dep->payload.nbytes);
   3540 		kmem_free(ldep->align_addr,
   3541 		    sizeof (caddr_t) * P2ROUNDUP(ldep->nbytes, 8));
   3542 		ldep->align_addr = NULL;
   3543 	}
   3544 
   3545 	rv = ldc_mem_unbind_handle(ldep->desc_mhdl);
   3546 	if (rv != 0) {
   3547 		DMSG(vdc, 0, "?[%d] unbind mhdl 0x%lx @ idx %d failed (%d)",
   3548 		    vdc->instance, ldep->desc_mhdl, idx, rv);
   3549 		/*
   3550 		 * The error returned by the vDisk server is more informative
   3551 		 * and thus has a higher priority but if it isn't set we ensure
   3552 		 * that this function returns an error.
   3553 		 */
   3554 		if (status == 0)
   3555 			status = EINVAL;
   3556 	}
   3557 
   3558 	cv_signal(&vdc->membind_cv);
   3559 	cv_signal(&vdc->dring_free_cv);
   3560 
   3561 	return (status);
   3562 }
   3563 
   3564 /*
   3565  * Function:
   3566  *	vdc_populate_mem_hdl()
   3567  *
   3568  * Description:
   3569  *
   3570  * Arguments:
   3571  *	vdc	- soft state pointer for this instance of the device driver.
   3572  *	idx	- Index of the Descriptor Ring entry being modified
   3573  *	addr	- virtual address being mapped in
   3574  *	nybtes	- number of bytes in 'addr'
   3575  *	operation - the vDisk operation being performed (VD_OP_xxx)
   3576  *
   3577  * Return Code:
   3578  *	0	- Success
   3579  */
   3580 static int
   3581 vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep)
   3582 {
   3583 	vd_dring_entry_t	*dep = NULL;
   3584 	ldc_mem_handle_t	mhdl;
   3585 	caddr_t			vaddr;
   3586 	size_t			nbytes;
   3587 	uint8_t			perm = LDC_MEM_RW;
   3588 	uint8_t			maptype;
   3589 	int			rv = 0;
   3590 	int			i;
   3591 
   3592 	ASSERT(vdcp != NULL);
   3593 
   3594 	dep = ldep->dep;
   3595 	mhdl = ldep->desc_mhdl;
   3596 
   3597 	switch (ldep->dir) {
   3598 	case VIO_read_dir:
   3599 		perm = LDC_MEM_W;
   3600 		break;
   3601 
   3602 	case VIO_write_dir:
   3603 		perm = LDC_MEM_R;
   3604 		break;
   3605 
   3606 	case VIO_both_dir:
   3607 		perm = LDC_MEM_RW;
   3608 		break;
   3609 
   3610 	default:
   3611 		ASSERT(0);	/* catch bad programming in vdc */
   3612 	}
   3613 
   3614 	/*
   3615 	 * LDC expects any addresses passed in to be 8-byte aligned. We need
   3616 	 * to copy the contents of any misaligned buffers to a newly allocated
   3617 	 * buffer and bind it instead (and copy the the contents back to the
   3618 	 * original buffer passed in when depopulating the descriptor)
   3619 	 */
   3620 	vaddr = ldep->addr;
   3621 	nbytes = ldep->nbytes;
   3622 	if (((uint64_t)vaddr & 0x7) != 0) {
   3623 		ASSERT(ldep->align_addr == NULL);
   3624 		ldep->align_addr =
   3625 		    kmem_alloc(sizeof (caddr_t) *
   3626 		    P2ROUNDUP(nbytes, 8), KM_SLEEP);
   3627 		DMSG(vdcp, 0, "[%d] Misaligned address %p reallocating "
   3628 		    "(buf=%p nb=%ld op=%d)\n",
   3629 		    vdcp->instance, (void *)vaddr, (void *)ldep->align_addr,
   3630 		    nbytes, ldep->operation);
   3631 		if (perm != LDC_MEM_W)
   3632 			bcopy(vaddr, ldep->align_addr, nbytes);
   3633 		vaddr = ldep->align_addr;
   3634 	}
   3635 
   3636 	maptype = LDC_IO_MAP|LDC_SHADOW_MAP|LDC_DIRECT_MAP;
   3637 	rv = ldc_mem_bind_handle(mhdl, vaddr, P2ROUNDUP(nbytes, 8),
   3638 	    maptype, perm, &dep->payload.cookie[0], &dep->payload.ncookies);
   3639 	DMSG(vdcp, 2, "[%d] bound mem handle; ncookies=%d\n",
   3640 	    vdcp->instance, dep->payload.ncookies);
   3641 	if (rv != 0) {
   3642 		DMSG(vdcp, 0, "[%d] Failed to bind LDC memory handle "
   3643 		    "(mhdl=%p, buf=%p, err=%d)\n",
   3644 		    vdcp->instance, (void *)mhdl, (void *)vaddr, rv);
   3645 		if (ldep->align_addr) {
   3646 			kmem_free(ldep->align_addr,
   3647 			    sizeof (caddr_t) * P2ROUNDUP(nbytes, 8));
   3648 			ldep->align_addr = NULL;
   3649 		}
   3650 		return (EAGAIN);
   3651 	}
   3652 
   3653 	/*
   3654 	 * Get the other cookies (if any).
   3655 	 */
   3656 	for (i = 1; i < dep->payload.ncookies; i++) {
   3657 		rv = ldc_mem_nextcookie(mhdl, &dep->payload.cookie[i]);
   3658 		if (rv != 0) {
   3659 			(void) ldc_mem_unbind_handle(mhdl);
   3660 			DMSG(vdcp, 0, "?[%d] Failed to get next cookie "
   3661 			    "(mhdl=%lx cnum=%d), err=%d",
   3662 			    vdcp->instance, mhdl, i, rv);
   3663 			if (ldep->align_addr) {
   3664 				kmem_free(ldep->align_addr,
   3665 				    sizeof (caddr_t) * ldep->nbytes);
   3666 				ldep->align_addr = NULL;
   3667 			}
   3668 			return (EAGAIN);
   3669 		}
   3670 	}
   3671 
   3672 	return (rv);
   3673 }
   3674 
   3675 /*
   3676  * Interrupt handlers for messages from LDC
   3677  */
   3678 
   3679 /*
   3680  * Function:
   3681  *	vdc_handle_cb()
   3682  *
   3683  * Description:
   3684  *
   3685  * Arguments:
   3686  *	event	- Type of event (LDC_EVT_xxx) that triggered the callback
   3687  *	arg	- soft state pointer for this instance of the device driver.
   3688  *
   3689  * Return Code:
   3690  *	0	- Success
   3691  */
   3692 static uint_t
   3693 vdc_handle_cb(uint64_t event, caddr_t arg)
   3694 {
   3695 	ldc_status_t	ldc_state;
   3696 	int		rv = 0;
   3697 	vdc_server_t	*srvr = (vdc_server_t *)(void *)arg;
   3698 	vdc_t		*vdc = srvr->vdcp;
   3699 
   3700 	ASSERT(vdc != NULL);
   3701 
   3702 	DMSG(vdc, 1, "evt=%lx seqID=%ld\n", event, vdc->seq_num);
   3703 
   3704 	/* If callback is not for the current server, ignore it */
   3705 	mutex_enter(&vdc->lock);
   3706 
   3707 	if (vdc->curr_server != srvr) {
   3708 		DMSG(vdc, 0, "[%d] Ignoring event 0x%lx for port@%ld\n",
   3709 		    vdc->instance, event, srvr->id);
   3710 		mutex_exit(&vdc->lock);
   3711 		return (LDC_SUCCESS);
   3712 	}
   3713 
   3714 	/*
   3715 	 * Depending on the type of event that triggered this callback,
   3716 	 * we modify the handshake state or read the data.
   3717 	 *
   3718 	 * NOTE: not done as a switch() as event could be triggered by
   3719 	 * a state change and a read request. Also the ordering	of the
   3720 	 * check for the event types is deliberate.
   3721 	 */
   3722 	if (event & LDC_EVT_UP) {
   3723 		DMSG(vdc, 0, "[%d] Received LDC_EVT_UP\n", vdc->instance);
   3724 
   3725 		/* get LDC state */
   3726 		rv = ldc_status(srvr->ldc_handle, &ldc_state);
   3727 		if (rv != 0) {
   3728 			DMSG(vdc, 0, "[%d] Couldn't get LDC status %d",
   3729 			    vdc->instance, rv);
   3730 			mutex_exit(&vdc->lock);
   3731 			return (LDC_SUCCESS);
   3732 		}
   3733 		if (srvr->ldc_state != LDC_UP &&
   3734 		    ldc_state == LDC_UP) {
   3735 			/*
   3736 			 * Reset the transaction sequence numbers when
   3737 			 * LDC comes up. We then kick off the handshake
   3738 			 * negotiation with the vDisk server.
   3739 			 */
   3740 			vdc->seq_num = 1;
   3741 			vdc->seq_num_reply = 0;
   3742 			vdc->io_pending = B_TRUE;
   3743 			srvr->ldc_state = ldc_state;
   3744 			cv_signal(&vdc->initwait_cv);
   3745 			cv_signal(&vdc->io_pending_cv);
   3746 		}
   3747 	}
   3748 
   3749 	if (event & LDC_EVT_READ) {
   3750 		DMSG(vdc, 1, "[%d] Received LDC_EVT_READ\n", vdc->instance);
   3751 		mutex_enter(&vdc->read_lock);
   3752 		cv_signal(&vdc->read_cv);
   3753 		vdc->read_state = VDC_READ_PENDING;
   3754 		mutex_exit(&vdc->read_lock);
   3755 		mutex_exit(&vdc->lock);
   3756 
   3757 		/* that's all we have to do - no need to handle DOWN/RESET */
   3758 		return (LDC_SUCCESS);
   3759 	}
   3760 
   3761 	if (event & (LDC_EVT_RESET|LDC_EVT_DOWN)) {
   3762 
   3763 		DMSG(vdc, 0, "[%d] Received LDC RESET event\n", vdc->instance);
   3764 
   3765 		/*
   3766 		 * Need to wake up any readers so they will
   3767 		 * detect that a reset has occurred.
   3768 		 */
   3769 		mutex_enter(&vdc->read_lock);
   3770 		if ((vdc->read_state == VDC_READ_WAITING) ||
   3771 		    (vdc->read_state == VDC_READ_RESET))
   3772 			cv_signal(&vdc->read_cv);
   3773 		vdc->read_state = VDC_READ_RESET;
   3774 		mutex_exit(&vdc->read_lock);
   3775 
   3776 		/* wake up any threads waiting for connection to come up */
   3777 		if (vdc->state == VDC_STATE_INIT_WAITING) {
   3778 			vdc->state = VDC_STATE_RESETTING;
   3779 			cv_signal(&vdc->initwait_cv);
   3780 		} else if (vdc->state == VDC_STATE_FAILED) {
   3781 			vdc->io_pending = B_TRUE;
   3782 			cv_signal(&vdc->io_pending_cv);
   3783 		}
   3784 
   3785 	}
   3786 
   3787 	mutex_exit(&vdc->lock);
   3788 
   3789 	if (event & ~(LDC_EVT_UP | LDC_EVT_RESET | LDC_EVT_DOWN | LDC_EVT_READ))
   3790 		DMSG(vdc, 0, "![%d] Unexpected LDC event (%lx) received",
   3791 		    vdc->instance, event);
   3792 
   3793 	return (LDC_SUCCESS);
   3794 }
   3795 
   3796 /*
   3797  * Function:
   3798  *	vdc_wait_for_response()
   3799  *
   3800  * Description:
   3801  *	Block waiting for a response from the server. If there is
   3802  *	no data the thread block on the read_cv that is signalled
   3803  *	by the callback when an EVT_READ occurs.
   3804  *
   3805  * Arguments:
   3806  *	vdcp	- soft state pointer for this instance of the device driver.
   3807  *
   3808  * Return Code:
   3809  *	0	- Success
   3810  */
   3811 static int
   3812 vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp)
   3813 {
   3814 	size_t		nbytes = sizeof (*msgp);
   3815 	int		status;
   3816 
   3817 	ASSERT(vdcp != NULL);
   3818 
   3819 	DMSG(vdcp, 1, "[%d] Entered\n", vdcp->instance);
   3820 
   3821 	status = vdc_recv(vdcp, msgp, &nbytes);
   3822 	DMSG(vdcp, 3, "vdc_read() done.. status=0x%x size=0x%x\n",
   3823 	    status, (int)nbytes);
   3824 	if (status) {
   3825 		DMSG(vdcp, 0, "?[%d] Error %d reading LDC msg\n",
   3826 		    vdcp->instance, status);
   3827 		return (status);
   3828 	}
   3829 
   3830 	if (nbytes < sizeof (vio_msg_tag_t)) {
   3831 		DMSG(vdcp, 0, "?[%d] Expect %lu bytes; recv'd %lu\n",
   3832 		    vdcp->instance, sizeof (vio_msg_tag_t), nbytes);
   3833 		return (ENOMSG);
   3834 	}
   3835 
   3836 	DMSG(vdcp, 2, "[%d] (%x/%x/%x)\n", vdcp->instance,
   3837 	    msgp->tag.vio_msgtype,
   3838 	    msgp->tag.vio_subtype,
   3839 	    msgp->tag.vio_subtype_env);
   3840 
   3841 	/*
   3842 	 * Verify the Session ID of the message
   3843 	 *
   3844 	 * Every message after the Version has been negotiated should
   3845 	 * have the correct session ID set.
   3846 	 */
   3847 	if ((msgp->tag.vio_sid != vdcp->session_id) &&
   3848 	    (msgp->tag.vio_subtype_env != VIO_VER_INFO)) {
   3849 		DMSG(vdcp, 0, "[%d] Invalid SID: received 0x%x, "
   3850 		    "expected 0x%lx [seq num %lx @ %d]",
   3851 		    vdcp->instance, msgp->tag.vio_sid,
   3852 		    vdcp->session_id,
   3853 		    ((vio_dring_msg_t *)msgp)->seq_num,
   3854 		    ((vio_dring_msg_t *)msgp)->start_idx);
   3855 		return (ENOMSG);
   3856 	}
   3857 	return (0);
   3858 }
   3859 
   3860 
   3861 /*
   3862  * Function:
   3863  *	vdc_resubmit_backup_dring()
   3864  *
   3865  * Description:
   3866  *	Resubmit each descriptor in the backed up dring to
   3867  * 	vDisk server. The Dring was backed up during connection
   3868  *	reset.
   3869  *
   3870  * Arguments:
   3871  *	vdcp	- soft state pointer for this instance of the device driver.
   3872  *
   3873  * Return Code:
   3874  *	0	- Success
   3875  */
   3876 static int
   3877 vdc_resubmit_backup_dring(vdc_t *vdcp)
   3878 {
   3879 	int		processed = 0;
   3880 	int		count;
   3881 	int		b_idx;
   3882 	int		rv = 0;
   3883 	int		dring_size;
   3884 	vdc_local_desc_t	*curr_ldep;
   3885 
   3886 	ASSERT(MUTEX_NOT_HELD(&vdcp->lock));
   3887 	ASSERT(vdcp->state == VDC_STATE_HANDLE_PENDING);
   3888 
   3889 	if (vdcp->local_dring_backup == NULL) {
   3890 		/* the pending requests have already been processed */
   3891 		return (0);
   3892 	}
   3893 
   3894 	DMSG(vdcp, 1, "restoring pending dring entries (len=%d, tail=%d)\n",
   3895 	    vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
   3896 
   3897 	/*
   3898 	 * Walk the backup copy of the local descriptor ring and
   3899 	 * resubmit all the outstanding transactions.
   3900 	 */
   3901 	b_idx = vdcp->local_dring_backup_tail;
   3902 	for (count = 0; count < vdcp->local_dring_backup_len; count++) {
   3903 
   3904 		curr_ldep = &(vdcp->local_dring_backup[b_idx]);
   3905 
   3906 		/* only resubmit outstanding transactions */
   3907 		if (!curr_ldep->is_free) {
   3908 
   3909 			DMSG(vdcp, 1, "resubmitting entry idx=%x\n", b_idx);
   3910 
   3911 			rv = vdc_do_op(vdcp, curr_ldep->operation,
   3912 			    curr_ldep->addr, curr_ldep->nbytes,
   3913 			    curr_ldep->slice, curr_ldep->offset,
   3914 			    curr_ldep->buf, curr_ldep->dir,
   3915 			    curr_ldep->flags & ~VDC_OP_STATE_RUNNING);
   3916 
   3917 			if (rv) {
   3918 				DMSG(vdcp, 1, "[%d] resubmit entry %d failed\n",
   3919 				    vdcp->instance, b_idx);
   3920 				goto done;
   3921 			}
   3922 
   3923 			/*
   3924 			 * Mark this entry as free so that we will not resubmit
   3925 			 * this "done" request again, if we were to use the same
   3926 			 * backup_dring again in future. This could happen when
   3927 			 * a reset happens while processing the backup_dring.
   3928 			 */
   3929 			curr_ldep->is_free = B_TRUE;
   3930 			processed++;
   3931 		}
   3932 
   3933 		/* get the next element to submit */
   3934 		if (++b_idx >= vdcp->local_dring_backup_len)
   3935 			b_idx = 0;
   3936 	}
   3937 
   3938 	/* all done - now clear up pending dring copy */
   3939 	dring_size = vdcp->local_dring_backup_len *
   3940 	    sizeof (vdcp->local_dring_backup[0]);
   3941 
   3942 	(void) kmem_free(vdcp->local_dring_backup, dring_size);
   3943 
   3944 	vdcp->local_dring_backup = NULL;
   3945 
   3946 done:
   3947 	DTRACE_PROBE2(processed, int, processed, vdc_t *, vdcp);
   3948 
   3949 	return (rv);
   3950 }
   3951 
   3952 /*
   3953  * Function:
   3954  *	vdc_cancel_backup_dring
   3955  *
   3956  * Description:
   3957  *	Cancel each descriptor in the backed up dring to vDisk server.
   3958  *	The Dring was backed up during connection reset.
   3959  *
   3960  * Arguments:
   3961  *	vdcp	- soft state pointer for this instance of the device driver.
   3962  *
   3963  * Return Code:
   3964  *	None
   3965  */
   3966 void
   3967 vdc_cancel_backup_dring(vdc_t *vdcp)
   3968 {
   3969 	vdc_local_desc_t *ldep;
   3970 	struct buf 	*bufp;
   3971 	int		count;
   3972 	int		b_idx;
   3973 	int		dring_size;
   3974 	int		cancelled = 0;
   3975 
   3976 	ASSERT(MUTEX_HELD(&vdcp->lock));
   3977 	ASSERT(vdcp->state == VDC_STATE_FAILED);
   3978 
   3979 	if (vdcp->local_dring_backup == NULL) {
   3980 		/* the pending requests have already been processed */
   3981 		return;
   3982 	}
   3983 
   3984 	DMSG(vdcp, 1, "cancelling pending dring entries (len=%d, tail=%d)\n",
   3985 	    vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
   3986 
   3987 	/*
   3988 	 * Walk the backup copy of the local descriptor ring and
   3989 	 * cancel all the outstanding transactions.
   3990 	 */
   3991 	b_idx = vdcp->local_dring_backup_tail;
   3992 	for (count = 0; count < vdcp->local_dring_backup_len; count++) {
   3993 
   3994 		ldep = &(vdcp->local_dring_backup[b_idx]);
   3995 
   3996 		/* only cancel outstanding transactions */
   3997 		if (!ldep->is_free) {
   3998 
   3999 			DMSG(vdcp, 1, "cancelling entry idx=%x\n", b_idx);
   4000 			cancelled++;
   4001 
   4002 			/*
   4003 			 * All requests have already been cleared from the
   4004 			 * local descriptor ring and the LDC channel has been
   4005 			 * reset so we will never get any reply for these
   4006 			 * requests. Now we just have to notify threads waiting
   4007 			 * for replies that the request has failed.
   4008 			 */
   4009 			bufp = ldep->buf;
   4010 			ASSERT(bufp != NULL);
   4011 			bufp->b_resid = bufp->b_bcount;
   4012 			if (ldep->operation == VD_OP_BREAD ||
   4013 			    ldep->operation == VD_OP_BWRITE) {
   4014 				VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
   4015 				VD_KSTAT_RUNQ_EXIT(vdcp);
   4016 				DTRACE_IO1(done, buf_t *, bufp);
   4017 			}
   4018 			bioerror(bufp, EIO);
   4019 			biodone(bufp);
   4020 		}
   4021 
   4022 		/* get the next element to cancel */
   4023 		if (++b_idx >= vdcp->local_dring_backup_len)
   4024 			b_idx = 0;
   4025 	}
   4026 
   4027 	/* all done - now clear up pending dring copy */
   4028 	dring_size = vdcp->local_dring_backup_len *
   4029 	    sizeof (vdcp->local_dring_backup[0]);
   4030 
   4031 	(void) kmem_free(vdcp->local_dring_backup, dring_size);
   4032 
   4033 	vdcp->local_dring_backup = NULL;
   4034 
   4035 	DTRACE_PROBE2(cancelled, int, cancelled, vdc_t *, vdcp);
   4036 }
   4037 
   4038 /*
   4039  * Function:
   4040  *	vdc_connection_timeout
   4041  *
   4042  * Description:
   4043  *	This function is invoked if the timeout set to establish the connection
   4044  *	with vds expires. This will happen if we spend too much time in the
   4045  *	VDC_STATE_INIT_WAITING or VDC_STATE_NEGOTIATE states.
   4046  *
   4047  *	If the timeout does not expire, it will be cancelled when we reach the
   4048  *	VDC_STATE_HANDLE_PENDING, VDC_STATE_FAILED or VDC_STATE_DETACH state.
   4049  *	This function can also be invoked while we are in those states, in
   4050  *	which case we do nothing because the timeout is being cancelled.
   4051  *
   4052  * Arguments:
   4053  *	arg	- argument of the timeout function actually a soft state
   4054  *		  pointer for the instance of the device driver.
   4055  *
   4056  * Return Code:
   4057  *	None
   4058  */
   4059 void
   4060 vdc_connection_timeout(void *arg)
   4061 {
   4062 	vdc_t 		*vdcp = (vdc_t *)arg;
   4063 
   4064 	mutex_enter(&vdcp->lock);
   4065 
   4066 	if (vdcp->state == VDC_STATE_HANDLE_PENDING ||
   4067 	    vdcp->state == VDC_STATE_DETACH ||
   4068 	    vdcp->state == VDC_STATE_FAILED) {
   4069 		/*
   4070 		 * The connection has just been re-established, has failed or
   4071 		 * we are detaching.
   4072 		 */
   4073 		vdcp->ctimeout_reached = B_FALSE;
   4074 	} else {
   4075 		vdcp->ctimeout_reached = B_TRUE;
   4076 	}
   4077 
   4078 	mutex_exit(&vdcp->lock);
   4079 }
   4080 
   4081 /*
   4082  * Function:
   4083  *	vdc_backup_local_dring()
   4084  *
   4085  * Description:
   4086  *	Backup the current dring in the event of a reset. The Dring
   4087  *	transactions will be resubmitted to the server when the
   4088  *	connection is restored.
   4089  *
   4090  * Arguments:
   4091  *	vdcp	- soft state pointer for this instance of the device driver.
   4092  *
   4093  * Return Code:
   4094  *	NONE
   4095  */
   4096 static void
   4097 vdc_backup_local_dring(vdc_t *vdcp)
   4098 {
   4099 	int dring_size;
   4100 
   4101 	ASSERT(MUTEX_HELD(&vdcp->lock));
   4102 	ASSERT(vdcp->state == VDC_STATE_RESETTING);
   4103 
   4104 	/*
   4105 	 * If the backup dring is stil around, it means
   4106 	 * that the last restore did not complete. However,
   4107 	 * since we never got back into the running state,
   4108 	 * the backup copy we have is still valid.
   4109 	 */
   4110 	if (vdcp->local_dring_backup != NULL) {
   4111 		DMSG(vdcp, 1, "reusing local descriptor ring backup "
   4112 		    "(len=%d, tail=%d)\n", vdcp->local_dring_backup_len,
   4113 		    vdcp->local_dring_backup_tail);
   4114 		return;
   4115 	}
   4116 
   4117 	/*
   4118 	 * The backup dring can be NULL and the local dring may not be
   4119 	 * initialized. This can happen if we had a reset while establishing
   4120 	 * a new connection but after the connection has timed out. In that
   4121 	 * case the backup dring is NULL because the requests have been
   4122 	 * cancelled and the request occured before the local dring is
   4123 	 * initialized.
   4124 	 */
   4125 	if (!(vdcp->initialized & VDC_DRING_LOCAL))
   4126 		return;
   4127 
   4128 	DMSG(vdcp, 1, "backing up the local descriptor ring (len=%d, "
   4129 	    "tail=%d)\n", vdcp->dring_len, vdcp->dring_curr_idx);
   4130 
   4131 	dring_size = vdcp->dring_len * sizeof (vdcp->local_dring[0]);
   4132 
   4133 	vdcp->local_dring_backup = kmem_alloc(dring_size, KM_SLEEP);
   4134 	bcopy(vdcp->local_dring, vdcp->local_dring_backup, dring_size);
   4135 
   4136 	vdcp->local_dring_backup_tail = vdcp->dring_curr_idx;
   4137 	vdcp->local_dring_backup_len = vdcp->dring_len;
   4138 }
   4139 
   4140 static void
   4141 vdc_switch_server(vdc_t *vdcp)
   4142 {
   4143 	int		rv;
   4144 	vdc_server_t 	*curr_server, *new_server;
   4145 
   4146 	ASSERT(MUTEX_HELD(&vdcp->lock));
   4147 
   4148 	/* if there is only one server return back */
   4149 	if (vdcp->num_servers == 1) {
   4150 		return;
   4151 	}
   4152 
   4153 	/* Get current and next server */
   4154 	curr_server = vdcp->curr_server;
   4155 	new_server =
   4156 	    (curr_server->next) ? curr_server->next : vdcp->server_list;
   4157 	ASSERT(curr_server != new_server);
   4158 
   4159 	/* bring current server's channel down */
   4160 	rv = ldc_down(curr_server->ldc_handle);
   4161 	if (rv) {
   4162 		DMSG(vdcp, 0, "[%d] Cannot bring channel down, port %ld\n",
   4163 		    vdcp->instance, curr_server->id);
   4164 		return;
   4165 	}
   4166 
   4167 	/* switch the server */
   4168 	vdcp->curr_server = new_server;
   4169 
   4170 	DMSG(vdcp, 0, "[%d] Switched to next vdisk server, port@%ld, ldc@%ld\n",
   4171 	    vdcp->instance, vdcp->curr_server->id, vdcp->curr_server->ldc_id);
   4172 }
   4173 
   4174 static void
   4175 vdc_print_svc_status(vdc_t *vdcp)
   4176 {
   4177 	int instance;
   4178 	uint64_t ldc_id, port_id;
   4179 	vdc_service_state_t svc_state;
   4180 
   4181 	ASSERT(mutex_owned(&vdcp->lock));
   4182 
   4183 	svc_state = vdcp->curr_server->svc_state;
   4184 
   4185 	if (vdcp->curr_server->log_state == svc_state)
   4186 		return;
   4187 
   4188 	instance = vdcp->instance;
   4189 	ldc_id = vdcp->curr_server->ldc_id;
   4190 	port_id = vdcp->curr_server->id;
   4191 
   4192 	switch (svc_state) {
   4193 
   4194 	case VDC_SERVICE_OFFLINE:
   4195 		cmn_err(CE_CONT, "?vdisk@%d is offline\n", instance);
   4196 		break;
   4197 
   4198 	case VDC_SERVICE_CONNECTED:
   4199 		cmn_err(CE_CONT, "?vdisk@%d is connected using ldc@%ld,%ld\n",
   4200 		    instance, ldc_id, port_id);
   4201 		break;
   4202 
   4203 	case VDC_SERVICE_ONLINE:
   4204 		cmn_err(CE_CONT, "?vdisk@%d is online using ldc@%ld,%ld\n",
   4205 		    instance, ldc_id, port_id);
   4206 		break;
   4207 
   4208 	case VDC_SERVICE_FAILED:
   4209 		cmn_err(CE_CONT, "?vdisk@%d access to service failed "
   4210 		    "using ldc@%ld,%ld\n", instance, ldc_id, port_id);
   4211 		break;
   4212 
   4213 	case VDC_SERVICE_FAULTED:
   4214 		cmn_err(CE_CONT, "?vdisk@%d access to backend failed "
   4215 		    "using ldc@%ld,%ld\n", instance, ldc_id, port_id);
   4216 		break;
   4217 
   4218 	default:
   4219 		ASSERT(0);
   4220 		break;
   4221 	}
   4222 
   4223 	vdcp->curr_server->log_state = svc_state;
   4224 }
   4225 
   4226 /* -------------------------------------------------------------------------- */
   4227 
   4228 /*
   4229  * The following functions process the incoming messages from vds
   4230  */
   4231 
   4232 /*
   4233  * Function:
   4234  *      vdc_process_msg_thread()
   4235  *
   4236  * Description:
   4237  *
   4238  *	Main VDC message processing thread. Each vDisk instance
   4239  * 	consists of a copy of this thread. This thread triggers
   4240  * 	all the handshakes and data exchange with the server. It
   4241  * 	also handles all channel resets
   4242  *
   4243  * Arguments:
   4244  *      vdc     - soft state pointer for this instance of the device driver.
   4245  *
   4246  * Return Code:
   4247  *      None
   4248  */
   4249 static void
   4250 vdc_process_msg_thread(vdc_t *vdcp)
   4251 {
   4252 	int		status;
   4253 	int		ctimeout;
   4254 	timeout_id_t	tmid = 0;
   4255 	clock_t		ldcup_timeout = 0;
   4256 	vdc_server_t	*srvr;
   4257 	vdc_service_state_t svc_state;
   4258 
   4259 	mutex_enter(&vdcp->lock);
   4260 
   4261 	for (;;) {
   4262 
   4263 #define	Q(_s)	(vdcp->state == _s) ? #_s :
   4264 		DMSG(vdcp, 3, "state = %d (%s)\n", vdcp->state,
   4265 		    Q(VDC_STATE_INIT)
   4266 		    Q(VDC_STATE_INIT_WAITING)
   4267 		    Q(VDC_STATE_NEGOTIATE)
   4268 		    Q(VDC_STATE_HANDLE_PENDING)
   4269 		    Q(VDC_STATE_FAULTED)
   4270 		    Q(VDC_STATE_FAILED)
   4271 		    Q(VDC_STATE_RUNNING)
   4272 		    Q(VDC_STATE_RESETTING)
   4273 		    Q(VDC_STATE_DETACH)
   4274 		    "UNKNOWN");
   4275 
   4276 		switch (vdcp->state) {
   4277 		case VDC_STATE_INIT:
   4278 
   4279 			/*
   4280 			 * If requested, start a timeout to check if the
   4281 			 * connection with vds is established in the
   4282 			 * specified delay. If the timeout expires, we
   4283 			 * will cancel any pending request.
   4284 			 *
   4285 			 * If some reset have occurred while establishing
   4286 			 * the connection, we already have a timeout armed
   4287 			 * and in that case we don't need to arm a new one.
   4288 			 *
   4289 			 * The same rule applies when there are multiple vds'.
   4290 			 * If either a connection cannot be established or
   4291 			 * the handshake times out, the connection thread will
   4292 			 * try another server. The 'ctimeout' will report
   4293 			 * back an error after it expires irrespective of
   4294 			 * whether the vdisk is trying to connect to just
   4295 			 * one or multiple servers.
   4296 			 */
   4297 			ctimeout = (vdc_timeout != 0)?
   4298 			    vdc_timeout : vdcp->curr_server->ctimeout;
   4299 
   4300 			if (ctimeout != 0 && tmid == 0) {
   4301 				tmid = timeout(vdc_connection_timeout, vdcp,
   4302 				    ctimeout * drv_usectohz(MICROSEC));
   4303 			}
   4304 
   4305 			/* Switch to STATE_DETACH if drv is detaching */
   4306 			if (vdcp->lifecycle == VDC_LC_DETACHING) {
   4307 				vdcp->state = VDC_STATE_DETACH;
   4308 				break;
   4309 			}
   4310 
   4311 			/* Check if the timeout has been reached */
   4312 			if (vdcp->ctimeout_reached) {
   4313 				ASSERT(tmid != 0);
   4314 				tmid = 0;
   4315 				vdcp->state = VDC_STATE_FAILED;
   4316 				break;
   4317 			}
   4318 
   4319 			/* Check if we are re-initializing repeatedly */
   4320 			if (vdcp->hshake_cnt > vdc_hshake_retries &&
   4321 			    vdcp->lifecycle != VDC_LC_ONLINE) {
   4322 
   4323 				DMSG(vdcp, 0, "[%d] too many handshakes,cnt=%d",
   4324 				    vdcp->instance, vdcp->hshake_cnt);
   4325 				vdcp->state = VDC_STATE_FAILED;
   4326 				break;
   4327 			}
   4328 
   4329 			/* Switch server */
   4330 			if (vdcp->hshake_cnt > 0)
   4331 				vdc_switch_server(vdcp);
   4332 			vdcp->hshake_cnt++;
   4333 
   4334 			/* Bring up connection with vds via LDC */
   4335 			status = vdc_start_ldc_connection(vdcp);
   4336 			if (status != EINVAL) {
   4337 				vdcp->state = VDC_STATE_INIT_WAITING;
   4338 			} else {
   4339 				vdcp->curr_server->svc_state =
   4340 				    VDC_SERVICE_FAILED;
   4341 				vdc_print_svc_status(vdcp);
   4342 			}
   4343 			break;
   4344 
   4345 		case VDC_STATE_INIT_WAITING:
   4346 
   4347 			/* if channel is UP, start negotiation */
   4348 			if (vdcp->curr_server->ldc_state == LDC_UP) {
   4349 				vdcp->state = VDC_STATE_NEGOTIATE;
   4350 				break;
   4351 			}
   4352 
   4353 			/*
   4354 			 * Wait for LDC_UP. If it times out and we have multiple
   4355 			 * servers then we will retry using a different server.
   4356 			 */
   4357 			ldcup_timeout = ddi_get_lbolt() + (vdc_ldcup_timeout *
   4358 			    drv_usectohz(MICROSEC));
   4359 			status = cv_timedwait(&vdcp->initwait_cv, &vdcp->lock,
   4360 			    ldcup_timeout);
   4361 			if (status == -1 &&
   4362 			    vdcp->state == VDC_STATE_INIT_WAITING &&
   4363 			    vdcp->curr_server->ldc_state != LDC_UP) {
   4364 				/* timed out & still waiting */
   4365 				vdcp->curr_server->svc_state =
   4366 				    VDC_SERVICE_FAILED;
   4367 				vdc_print_svc_status(vdcp);
   4368 				vdcp->state = VDC_STATE_INIT;
   4369 				break;
   4370 			}
   4371 
   4372 			if (vdcp->state != VDC_STATE_INIT_WAITING) {
   4373 				DMSG(vdcp, 0,
   4374 				    "state moved to %d out from under us...\n",
   4375 				    vdcp->state);
   4376 			}
   4377 			break;
   4378 
   4379 		case VDC_STATE_NEGOTIATE:
   4380 			switch (status = vdc_ver_negotiation(vdcp)) {
   4381 			case 0:
   4382 				break;
   4383 			default:
   4384 				DMSG(vdcp, 0, "ver negotiate failed (%d)..\n",
   4385 				    status);
   4386 				goto reset;
   4387 			}
   4388 
   4389 			switch (status = vdc_attr_negotiation(vdcp)) {
   4390 			case 0:
   4391 				break;
   4392 			default:
   4393 				DMSG(vdcp, 0, "attr negotiate failed (%d)..\n",
   4394 				    status);
   4395 				goto reset;
   4396 			}
   4397 
   4398 			switch (status = vdc_dring_negotiation(vdcp)) {
   4399 			case 0:
   4400 				break;
   4401 			default:
   4402 				DMSG(vdcp, 0, "dring negotiate failed (%d)..\n",
   4403 				    status);
   4404 				goto reset;
   4405 			}
   4406 
   4407 			switch (status = vdc_rdx_exchange(vdcp)) {
   4408 			case 0:
   4409 				vdcp->state = VDC_STATE_HANDLE_PENDING;
   4410 				goto done;
   4411 			default:
   4412 				DMSG(vdcp, 0, "RDX xchg failed ..(%d)\n",
   4413 				    status);
   4414 				goto reset;
   4415 			}
   4416 reset:
   4417 			DMSG(vdcp, 0, "negotiation failed: resetting (%d)\n",
   4418 			    status);
   4419 			vdcp->state = VDC_STATE_RESETTING;
   4420 			vdcp->self_reset = B_TRUE;
   4421 			vdcp->curr_server->svc_state = VDC_SERVICE_FAILED;
   4422 			vdc_print_svc_status(vdcp);
   4423 done:
   4424 			DMSG(vdcp, 0, "negotiation complete (state=0x%x)...\n",
   4425 			    vdcp->state);
   4426 			break;
   4427 
   4428 		case VDC_STATE_HANDLE_PENDING:
   4429 
   4430 			DMSG(vdcp, 0, "[%d] connection to service domain is up",
   4431 			    vdcp->instance);
   4432 			vdcp->curr_server->svc_state = VDC_SERVICE_CONNECTED;
   4433 
   4434 			mutex_exit(&vdcp->lock);
   4435 
   4436 			/*
   4437 			 * If we have multiple servers, check that the backend
   4438 			 * is effectively available before resubmitting any IO.
   4439 			 */
   4440 			if (vdcp->num_servers > 1 &&
   4441 			    vdc_eio_check(vdcp, 0) != 0) {
   4442 				mutex_enter(&vdcp->lock);
   4443 				vdcp->curr_server->svc_state =
   4444 				    VDC_SERVICE_FAULTED;
   4445 				vdcp->state = VDC_STATE_FAULTED;
   4446 				break;
   4447 			}
   4448 
   4449 			if (tmid != 0) {
   4450 				(void) untimeout(tmid);
   4451 				tmid = 0;
   4452 				vdcp->ctimeout_reached = B_FALSE;
   4453 			}
   4454 
   4455 			/*
   4456 			 * Setup devid
   4457 			 */
   4458 			(void) vdc_setup_devid(vdcp);
   4459 
   4460 			status = vdc_resubmit_backup_dring(vdcp);
   4461 
   4462 			mutex_enter(&vdcp->lock);
   4463 
   4464 			if (status) {
   4465 				vdcp->state = VDC_STATE_RESETTING;
   4466 				vdcp->self_reset = B_TRUE;
   4467 				vdcp->curr_server->svc_state =
   4468 				    VDC_SERVICE_FAILED;
   4469 				vdc_print_svc_status(vdcp);
   4470 			} else {
   4471 				vdcp->state = VDC_STATE_RUNNING;
   4472 			}
   4473 			break;
   4474 
   4475 		case VDC_STATE_FAULTED:
   4476 			/*
   4477 			 * Server is faulted because the backend is unavailable.
   4478 			 * If all servers are faulted then we mark the service
   4479 			 * as failed, otherwise we reset to switch to another
   4480 			 * server.
   4481 			 */
   4482 			vdc_print_svc_status(vdcp);
   4483 
   4484 			/* check if all servers are faulted */
   4485 			for (srvr = vdcp->server_list; srvr != NULL;
   4486 			    srvr = srvr->next) {
   4487 				svc_state = srvr->svc_state;
   4488 				if (svc_state != VDC_SERVICE_FAULTED)
   4489 					break;
   4490 			}
   4491 
   4492 			if (srvr != NULL) {
   4493 				vdcp->state = VDC_STATE_RESETTING;
   4494 				vdcp->self_reset = B_TRUE;
   4495 			} else {
   4496 				vdcp->state = VDC_STATE_FAILED;
   4497 			}
   4498 			break;
   4499 
   4500 		case VDC_STATE_FAILED:
   4501 			/*
   4502 			 * We reach this state when we are unable to access the
   4503 			 * backend from any server, either because of a maximum
   4504 			 * connection retries or timeout, or because the backend
   4505 			 * is unavailable.
   4506 			 *
   4507 			 * Then we cancel the backup DRing so that errors get
   4508 			 * reported and we wait for a new I/O before attempting
   4509 			 * another connection.
   4510 			 */
   4511 			cmn_err(CE_NOTE, "vdisk@%d disk access failed",
   4512 			    vdcp->instance);
   4513 
   4514 			/* cancel any timeout */
   4515 			if (tmid != 0) {
   4516 				(void) untimeout(tmid);
   4517 				tmid = 0;
   4518 			}
   4519 
   4520 			/* cancel pending I/Os */
   4521 			cv_broadcast(&vdcp->running_cv);
   4522 			vdc_cancel_backup_dring(vdcp);
   4523 
   4524 			/* wait for new I/O */
   4525 			while (!vdcp->io_pending)
   4526 				cv_wait(&vdcp->io_pending_cv, &vdcp->lock);
   4527 
   4528 			/*
   4529 			 * There's a new IO pending. Try to re-establish a
   4530 			 * connection. Mark all services as offline, so that
   4531 			 * we don't stop again before having retried all
   4532 			 * servers.
   4533 			 */
   4534 			for (srvr = vdcp->server_list; srvr != NULL;
   4535 			    srvr = srvr->next) {
   4536 				srvr->svc_state = VDC_SERVICE_OFFLINE;
   4537 			}
   4538 
   4539 			/* reset variables */
   4540 			vdcp->hshake_cnt = 0;
   4541 			vdcp->ctimeout_reached = B_FALSE;
   4542 
   4543 			vdcp->state = VDC_STATE_RESETTING;
   4544 			vdcp->self_reset = B_TRUE;
   4545 			break;
   4546 
   4547 		/* enter running state */
   4548 		case VDC_STATE_RUNNING:
   4549 			/*
   4550 			 * Signal anyone waiting for the connection
   4551 			 * to come on line.
   4552 			 */
   4553 			vdcp->hshake_cnt = 0;
   4554 			cv_broadcast(&vdcp->running_cv);
   4555 
   4556 			/* backend has to be checked after reset */
   4557 			if (vdcp->failfast_interval != 0 ||
   4558 			    vdcp->num_servers > 1)
   4559 				cv_signal(&vdcp->eio_cv);
   4560 
   4561 			/* ownership is lost during reset */
   4562 			if (vdcp->ownership & VDC_OWNERSHIP_WANTED)
   4563 				vdcp->ownership |= VDC_OWNERSHIP_RESET;
   4564 			cv_signal(&vdcp->ownership_cv);
   4565 
   4566 			vdcp->curr_server->svc_state = VDC_SERVICE_ONLINE;
   4567 			vdc_print_svc_status(vdcp);
   4568 
   4569 			mutex_exit(&vdcp->lock);
   4570 
   4571 			for (;;) {
   4572 				vio_msg_t msg;
   4573 				status = vdc_wait_for_response(vdcp, &msg);
   4574 				if (status) break;
   4575 
   4576 				DMSG(vdcp, 1, "[%d] new pkt(s) available\n",
   4577 				    vdcp->instance);
   4578 				status = vdc_process_data_msg(vdcp, &msg);
   4579 				if (status) {
   4580 					DMSG(vdcp, 1, "[%d] process_data_msg "
   4581 					    "returned err=%d\n", vdcp->instance,
   4582 					    status);
   4583 					break;
   4584 				}
   4585 
   4586 			}
   4587 
   4588 			mutex_enter(&vdcp->lock);
   4589 
   4590 			/* all servers are now offline */
   4591 			for (srvr = vdcp->server_list; srvr != NULL;
   4592 			    srvr = srvr->next) {
   4593 				srvr->svc_state = VDC_SERVICE_OFFLINE;
   4594 				srvr->log_state = VDC_SERVICE_NONE;
   4595 			}
   4596 
   4597 			vdc_print_svc_status(vdcp);
   4598 
   4599 			vdcp->state = VDC_STATE_RESETTING;
   4600 			vdcp->self_reset = B_TRUE;
   4601 			break;
   4602 
   4603 		case VDC_STATE_RESETTING:
   4604 			/*
   4605 			 * When we reach this state, we either come from the
   4606 			 * VDC_STATE_RUNNING state and we can have pending
   4607 			 * request but no timeout is armed; or we come from
   4608 			 * the VDC_STATE_INIT_WAITING, VDC_NEGOTIATE or
   4609 			 * VDC_HANDLE_PENDING state and there is no pending
   4610 			 * request or pending requests have already been copied
   4611 			 * into the backup dring. So we can safely keep the
   4612 			 * connection timeout armed while we are in this state.
   4613 			 */
   4614 
   4615 			DMSG(vdcp, 0, "Initiating channel reset "
   4616 			    "(pending = %d)\n", (int)vdcp->threads_pending);
   4617 
   4618 			if (vdcp->self_reset) {
   4619 				DMSG(vdcp, 0,
   4620 				    "[%d] calling stop_ldc_connection.\n",
   4621 				    vdcp->instance);
   4622 				status = vdc_stop_ldc_connection(vdcp);
   4623 				vdcp->self_reset = B_FALSE;
   4624 			}
   4625 
   4626 			/*
   4627 			 * Wait for all threads currently waiting
   4628 			 * for a free dring entry to use.
   4629 			 */
   4630 			while (vdcp->threads_pending) {
   4631 				cv_broadcast(&vdcp->membind_cv);
   4632 				cv_broadcast(&vdcp->dring_free_cv);
   4633 				mutex_exit(&vdcp->lock);
   4634 				/* give the waiters enough time to wake up */
   4635 				delay(vdc_hz_min_ldc_delay);
   4636 				mutex_enter(&vdcp->lock);
   4637 			}
   4638 
   4639 			ASSERT(vdcp->threads_pending == 0);
   4640 
   4641 			/* Sanity check that no thread is receiving */
   4642 			ASSERT(vdcp->read_state != VDC_READ_WAITING);
   4643 
   4644 			vdcp->read_state = VDC_READ_IDLE;
   4645 			vdcp->io_pending = B_FALSE;
   4646 
   4647 			/*
   4648 			 * Cleanup any pending eio. These I/Os are going to
   4649 			 * be resubmitted.
   4650 			 */
   4651 			vdc_eio_unqueue(vdcp, 0, B_FALSE);
   4652 
   4653 			vdc_backup_local_dring(vdcp);
   4654 
   4655 			/* cleanup the old d-ring */
   4656 			vdc_destroy_descriptor_ring(vdcp);
   4657 
   4658 			/* go and start again */
   4659 			vdcp->state = VDC_STATE_INIT;
   4660 
   4661 			break;
   4662 
   4663 		case VDC_STATE_DETACH:
   4664 			DMSG(vdcp, 0, "[%d] Reset thread exit cleanup ..\n",
   4665 			    vdcp->instance);
   4666 
   4667 			/* cancel any pending timeout */
   4668 			mutex_exit(&vdcp->lock);
   4669 			if (tmid != 0) {
   4670 				(void) untimeout(tmid);
   4671 				tmid = 0;
   4672 			}
   4673 			mutex_enter(&vdcp->lock);
   4674 
   4675 			/*
   4676 			 * Signal anyone waiting for connection
   4677 			 * to come online
   4678 			 */
   4679 			cv_broadcast(&vdcp->running_cv);
   4680 
   4681 			while (vdcp->sync_op_cnt > 0) {
   4682 				cv_broadcast(&vdcp->sync_blocked_cv);
   4683 				mutex_exit(&vdcp->lock);
   4684 				/* give the waiters enough time to wake up */
   4685 				delay(vdc_hz_min_ldc_delay);
   4686 				mutex_enter(&vdcp->lock);
   4687 			}
   4688 
   4689 			mutex_exit(&vdcp->lock);
   4690 
   4691 			DMSG(vdcp, 0, "[%d] Msg processing thread exiting ..\n",
   4692 			    vdcp->instance);
   4693 			thread_exit();
   4694 			break;
   4695 		}
   4696 	}
   4697 }
   4698 
   4699 
   4700 /*
   4701  * Function:
   4702  *	vdc_process_data_msg()
   4703  *
   4704  * Description:
   4705  *	This function is called by the message processing thread each time
   4706  *	a message with a msgtype of VIO_TYPE_DATA is received. It will either
   4707  *	be an ACK or NACK from vds[1] which vdc handles as follows.
   4708  *		ACK	- wake up the waiting thread
   4709  *		NACK	- resend any messages necessary
   4710  *
   4711  *	[1] Although the message format allows it, vds should not send a
   4712  *	    VIO_SUBTYPE_INFO message to vdc asking it to read data; if for
   4713  *	    some bizarre reason it does, vdc will reset the connection.
   4714  *
   4715  * Arguments:
   4716  *	vdc	- soft state pointer for this instance of the device driver.
   4717  *	msg	- the LDC message sent by vds
   4718  *
   4719  * Return Code:
   4720  *	0	- Success.
   4721  *	> 0	- error value returned by LDC
   4722  */
   4723 static int
   4724 vdc_process_data_msg(vdc_t *vdcp, vio_msg_t *msg)
   4725 {
   4726 	int			status = 0;
   4727 	vio_dring_msg_t		*dring_msg;
   4728 	vdc_local_desc_t	*ldep = NULL;
   4729 	int			start, end;
   4730 	int			idx;
   4731 	int			op;
   4732 
   4733 	dring_msg = (vio_dring_msg_t *)msg;
   4734 
   4735 	ASSERT(msg->tag.vio_msgtype == VIO_TYPE_DATA);
   4736 	ASSERT(vdcp != NULL);
   4737 
   4738 	mutex_enter(&vdcp->lock);
   4739 
   4740 	/*
   4741 	 * Check to see if the message has bogus data
   4742 	 */
   4743 	idx = start = dring_msg->start_idx;
   4744 	end = dring_msg->end_idx;
   4745 	if ((start >= vdcp->dring_len) ||
   4746 	    (end >= vdcp->dring_len) || (end < -1)) {
   4747 		/*
   4748 		 * Update the I/O statistics to indicate that an error ocurred.
   4749 		 * No need to update the wait/run queues as no specific read or
   4750 		 * write request is being completed in response to this 'msg'.
   4751 		 */
   4752 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
   4753 		DMSG(vdcp, 0, "[%d] Bogus ACK data : start %d, end %d\n",
   4754 		    vdcp->instance, start, end);
   4755 		mutex_exit(&vdcp->lock);
   4756 		return (EINVAL);
   4757 	}
   4758 
   4759 	/*
   4760 	 * Verify that the sequence number is what vdc expects.
   4761 	 */
   4762 	switch (vdc_verify_seq_num(vdcp, dring_msg)) {
   4763 	case VDC_SEQ_NUM_TODO:
   4764 		break;	/* keep processing this message */
   4765 	case VDC_SEQ_NUM_SKIP:
   4766 		mutex_exit(&vdcp->lock);
   4767 		return (0);
   4768 	case VDC_SEQ_NUM_INVALID:
   4769 		/*
   4770 		 * Update the I/O statistics to indicate that an error ocurred.
   4771 		 * No need to update the wait/run queues as no specific read or
   4772 		 * write request is being completed in response to this 'msg'.
   4773 		 */
   4774 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
   4775 		DMSG(vdcp, 0, "[%d] invalid seqno\n", vdcp->instance);
   4776 		mutex_exit(&vdcp->lock);
   4777 		return (ENXIO);
   4778 	}
   4779 
   4780 	if (msg->tag.vio_subtype == VIO_SUBTYPE_NACK) {
   4781 		/*
   4782 		 * Update the I/O statistics to indicate that an error ocurred.
   4783 		 *
   4784 		 * We need to update the run queue if a read or write request
   4785 		 * is being NACKed - otherwise there will appear to be an
   4786 		 * indefinite outstanding request and statistics reported by
   4787 		 * iostat(1M) will be incorrect. The transaction will be
   4788 		 * resubmitted from the backup DRing following the reset
   4789 		 * and the wait/run queues will be entered again.
   4790 		 */
   4791 		ldep = &vdcp->local_dring[idx];
   4792 		op = ldep->operation;
   4793 		if ((op == VD_OP_BREAD) || (op == VD_OP_BWRITE)) {
   4794 			DTRACE_IO1(done, buf_t *, ldep->buf);
   4795 			VD_KSTAT_RUNQ_EXIT(vdcp);
   4796 		}
   4797 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
   4798 		VDC_DUMP_DRING_MSG(dring_msg);
   4799 		DMSG(vdcp, 0, "[%d] DATA NACK\n", vdcp->instance);
   4800 		mutex_exit(&vdcp->lock);
   4801 		return (EIO);
   4802 
   4803 	} else if (msg->tag.vio_subtype == VIO_SUBTYPE_INFO) {
   4804 		/*
   4805 		 * Update the I/O statistics to indicate that an error occurred.
   4806 		 * No need to update the wait/run queues as no specific read or
   4807 		 * write request is being completed in response to this 'msg'.
   4808 		 */
   4809 		VD_UPDATE_ERR_STATS(vdcp, vd_protoerrs);
   4810 		mutex_exit(&vdcp->lock);
   4811 		return (EPROTO);
   4812 	}
   4813 
   4814 	DMSG(vdcp, 1, ": start %d end %d\n", start, end);
   4815 	ASSERT(start == end);
   4816 
   4817 	ldep = &vdcp->local_dring[idx];
   4818 
   4819 	DMSG(vdcp, 1, ": state 0x%x\n", ldep->dep->hdr.dstate);
   4820 
   4821 	if (ldep->dep->hdr.dstate == VIO_DESC_DONE) {
   4822 		struct buf *bufp;
   4823 
   4824 		status = ldep->dep->payload.status;
   4825 
   4826 		bufp = ldep->buf;
   4827 		ASSERT(bufp != NULL);
   4828 
   4829 		bufp->b_resid = bufp->b_bcount - ldep->dep->payload.nbytes;
   4830 		bioerror(bufp, status);
   4831 
   4832 		if (status != 0) {
   4833 			DMSG(vdcp, 1, "I/O status=%d\n", status);
   4834 		}
   4835 
   4836 		DMSG(vdcp, 1,
   4837 		    "I/O complete req=%ld bytes resp=%ld bytes\n",
   4838 		    bufp->b_bcount, ldep->dep->payload.nbytes);
   4839 
   4840 		/*
   4841 		 * If the request has failed and we have multiple servers or
   4842 		 * failfast is enabled then we will have to defer the completion
   4843 		 * of the request until we have checked that the vdisk backend
   4844 		 * is effectively available (if multiple server) or that there
   4845 		 * is no reservation conflict (if failfast).
   4846 		 */
   4847 		if ((status != 0 &&
   4848 		    (vdcp->num_servers > 1 &&
   4849 		    (ldep->flags & VDC_OP_ERRCHK_BACKEND)) ||
   4850 		    (vdcp->failfast_interval != 0 &&
   4851 		    (ldep->flags & VDC_OP_ERRCHK_CONFLICT)))) {
   4852 			/*
   4853 			 * The I/O has failed and we need to check the error.
   4854 			 */
   4855 			(void) vdc_eio_queue(vdcp, idx);
   4856 		} else {
   4857 			op = ldep->operation;
   4858 			if (op == VD_OP_BREAD || op == VD_OP_BWRITE) {
   4859 				if (status == 0) {
   4860 					VD_UPDATE_IO_STATS(vdcp, op,
   4861 					    ldep->dep->payload.nbytes);
   4862 				} else {
   4863 					VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
   4864 				}
   4865 				VD_KSTAT_RUNQ_EXIT(vdcp);
   4866 				DTRACE_IO1(done, buf_t *, bufp);
   4867 			}
   4868 			(void) vdc_depopulate_descriptor(vdcp, idx);
   4869 			biodone(bufp);
   4870 		}
   4871 	}
   4872 
   4873 	/* let the arrival signal propogate */
   4874 	mutex_exit(&vdcp->lock);
   4875 
   4876 	/* probe gives the count of how many entries were processed */
   4877 	DTRACE_PROBE2(processed, int, 1, vdc_t *, vdcp);
   4878 
   4879 	return (0);
   4880 }
   4881 
   4882 
   4883 /*
   4884  * Function:
   4885  *	vdc_handle_ver_msg()
   4886  *
   4887  * Description:
   4888  *
   4889  * Arguments:
   4890  *	vdc	- soft state pointer for this instance of the device driver.
   4891  *	ver_msg	- LDC message sent by vDisk server
   4892  *
   4893  * Return Code:
   4894  *	0	- Success
   4895  */
   4896 static int
   4897 vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg)
   4898 {
   4899 	int status = 0;
   4900 
   4901 	ASSERT(vdc != NULL);
   4902 	ASSERT(mutex_owned(&vdc->lock));
   4903 
   4904 	if (ver_msg->tag.vio_subtype_env != VIO_VER_INFO) {
   4905 		return (EPROTO);
   4906 	}
   4907 
   4908 	if (ver_msg->dev_class != VDEV_DISK_SERVER) {
   4909 		return (EINVAL);
   4910 	}
   4911 
   4912 	switch (ver_msg->tag.vio_subtype) {
   4913 	case VIO_SUBTYPE_ACK:
   4914 		/*
   4915 		 * We check to see if the version returned is indeed supported
   4916 		 * (The server may have also adjusted the minor number downwards
   4917 		 * and if so 'ver_msg' will contain the actual version agreed)
   4918 		 */
   4919 		if (vdc_is_supported_version(ver_msg)) {
   4920 			vdc->ver.major = ver_msg->ver_major;
   4921 			vdc->ver.minor = ver_msg->ver_minor;
   4922 			ASSERT(vdc->ver.major > 0);
   4923 		} else {
   4924 			status = EPROTO;
   4925 		}
   4926 		break;
   4927 
   4928 	case VIO_SUBTYPE_NACK:
   4929 		/*
   4930 		 * call vdc_is_supported_version() which will return the next
   4931 		 * supported version (if any) in 'ver_msg'
   4932 		 */
   4933 		(void) vdc_is_supported_version(ver_msg);
   4934 		if (ver_msg->ver_major > 0) {
   4935 			size_t len = sizeof (*ver_msg);
   4936 
   4937 			ASSERT(vdc->ver.major > 0);
   4938 
   4939 			/* reset the necessary fields and resend */
   4940 			ver_msg->tag.vio_subtype = VIO_SUBTYPE_INFO;
   4941 			ver_msg->dev_class = VDEV_DISK;
   4942 
   4943 			status = vdc_send(vdc, (caddr_t)ver_msg, &len);
   4944 			DMSG(vdc, 0, "[%d] Resend VER info (LDC status = %d)\n",
   4945 			    vdc->instance, status);
   4946 			if (len != sizeof (*ver_msg))
   4947 				status = EBADMSG;
   4948 		} else {
   4949 			DMSG(vdc, 0, "[%d] No common version with vDisk server",
   4950 			    vdc->instance);
   4951 			status = ENOTSUP;
   4952 		}
   4953 
   4954 		break;
   4955 	case VIO_SUBTYPE_INFO:
   4956 		/*
   4957 		 * Handle the case where vds starts handshake
   4958 		 * (for now only vdc is the instigator)
   4959 		 */
   4960 		status = ENOTSUP;
   4961 		break;
   4962 
   4963 	default:
   4964 		status = EINVAL;
   4965 		break;
   4966 	}
   4967 
   4968 	return (status);
   4969 }
   4970 
   4971 /*
   4972  * Function:
   4973  *	vdc_handle_attr_msg()
   4974  *
   4975  * Description:
   4976  *
   4977  * Arguments:
   4978  *	vdc	- soft state pointer for this instance of the device driver.
   4979  *	attr_msg	- LDC message sent by vDisk server
   4980  *
   4981  * Return Code:
   4982  *	0	- Success
   4983  */
   4984 static int
   4985 vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg)
   4986 {
   4987 	int status = 0;
   4988 	vd_disk_type_t old_type;
   4989 
   4990 	ASSERT(vdc != NULL);
   4991 	ASSERT(mutex_owned(&vdc->lock));
   4992 
   4993 	if (attr_msg->tag.vio_subtype_env != VIO_ATTR_INFO) {
   4994 		return (EPROTO);
   4995 	}
   4996 
   4997 	switch (attr_msg->tag.vio_subtype) {
   4998 	case VIO_SUBTYPE_ACK:
   4999 		/*
   5000 		 * We now verify the attributes sent by vds.
   5001 		 */
   5002 		if (attr_msg->vdisk_size == 0) {
   5003 			DMSG(vdc, 0, "[%d] Invalid disk size from vds",
   5004 			    vdc->instance);
   5005 			status = EINVAL;
   5006 			break;
   5007 		}
   5008 
   5009 		if (attr_msg->max_xfer_sz == 0) {
   5010 			DMSG(vdc, 0, "[%d] Invalid transfer size from vds",
   5011 			    vdc->instance);
   5012 			status = EINVAL;
   5013 			break;
   5014 		}
   5015 
   5016 		if (attr_msg->vdisk_size == VD_SIZE_UNKNOWN) {
   5017 			DMSG(vdc, 0, "[%d] Unknown disk size from vds",
   5018 			    vdc->instance);
   5019 			attr_msg->vdisk_size = 0;
   5020 		}
   5021 
   5022 		/* update the VIO block size */
   5023 		if (attr_msg->vdisk_block_size > 0 &&
   5024 		    vdc_update_vio_bsize(vdc,
   5025 		    attr_msg->vdisk_block_size) != 0) {
   5026 			DMSG(vdc, 0, "[%d] Invalid block size (%u) from vds",
   5027 			    vdc->instance, attr_msg->vdisk_block_size);
   5028 			status = EINVAL;
   5029 			break;
   5030 		}
   5031 
   5032 		/* update disk, block and transfer sizes */
   5033 		old_type = vdc->vdisk_type;
   5034 		vdc_update_size(vdc, attr_msg->vdisk_size,
   5035 		    attr_msg->vdisk_block_size, attr_msg->max_xfer_sz);
   5036 		vdc->vdisk_type = attr_msg->vdisk_type;
   5037 		vdc->operations = attr_msg->operations;
   5038 		if (vio_ver_is_supported(vdc->ver, 1, 1))
   5039 			vdc->vdisk_media = attr_msg->vdisk_media;
   5040 		else
   5041 			vdc->vdisk_media = 0;
   5042 
   5043 		DMSG(vdc, 0, "[%d] max_xfer_sz: sent %lx acked %lx\n",
   5044 		    vdc->instance, vdc->max_xfer_sz, attr_msg->max_xfer_sz);
   5045 		DMSG(vdc, 0, "[%d] vdisk_block_size: sent %lx acked %x\n",
   5046 		    vdc->instance, vdc->vdisk_bsize,
   5047 		    attr_msg->vdisk_block_size);
   5048 
   5049 		if ((attr_msg->xfer_mode != VIO_DRING_MODE_V1_0) ||
   5050 		    (attr_msg->vdisk_size > INT64_MAX) ||
   5051 		    (attr_msg->operations == 0) ||
   5052 		    (attr_msg->vdisk_type > VD_DISK_TYPE_DISK)) {
   5053 			DMSG(vdc, 0, "[%d] Invalid attributes from vds",
   5054 			    vdc->instance);
   5055 			status = EINVAL;
   5056 			break;
   5057 		}
   5058 
   5059 		/*
   5060 		 * Now that we have received all attributes we can create a
   5061 		 * fake geometry for the disk.
   5062 		 */
   5063 		vdc_create_fake_geometry(vdc);
   5064 
   5065 		/*
   5066 		 * If the disk type was previously unknown and device nodes
   5067 		 * were created then the driver would have created 8 device
   5068 		 * nodes. If we now find out that this is a single-slice disk
   5069 		 * then we need to re-create the appropriate device nodes.
   5070 		 */
   5071 		if (old_type == VD_DISK_TYPE_UNK &&
   5072 		    (vdc->initialized & VDC_MINOR) &&
   5073 		    vdc->vdisk_type == VD_DISK_TYPE_SLICE) {
   5074 			ddi_remove_minor_node(vdc->dip, NULL);
   5075 			(void) devfs_clean(ddi_get_parent(vdc->dip),
   5076 			    NULL, DV_CLEAN_FORCE);
   5077 			if (vdc_create_device_nodes(vdc) != 0) {
   5078 				DMSG(vdc, 0, "![%d] Failed to update "
   5079 				    "device nodes", vdc->instance);
   5080 			}
   5081 		}
   5082 
   5083 		break;
   5084 
   5085 	case VIO_SUBTYPE_NACK:
   5086 		/*
   5087 		 * vds could not handle the attributes we sent so we
   5088 		 * stop negotiating.
   5089 		 */
   5090 		status = EPROTO;
   5091 		break;
   5092 
   5093 	case VIO_SUBTYPE_INFO:
   5094 		/*
   5095 		 * Handle the case where vds starts the handshake
   5096 		 * (for now; vdc is the only supported instigatior)
   5097 		 */
   5098 		status = ENOTSUP;
   5099 		break;
   5100 
   5101 	default:
   5102 		status = ENOTSUP;
   5103 		break;
   5104 	}
   5105 
   5106 	return (status);
   5107 }
   5108 
   5109 /*
   5110  * Function:
   5111  *	vdc_handle_dring_reg_msg()
   5112  *
   5113  * Description:
   5114  *
   5115  * Arguments:
   5116  *	vdc		- soft state pointer for this instance of the driver.
   5117  *	dring_msg	- LDC message sent by vDisk server
   5118  *
   5119  * Return Code:
   5120  *	0	- Success
   5121  */
   5122 static int
   5123 vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *dring_msg)
   5124 {
   5125 	int		status = 0;
   5126 
   5127 	ASSERT(vdc != NULL);
   5128 	ASSERT(mutex_owned(&vdc->lock));
   5129 
   5130 	if (dring_msg->tag.vio_subtype_env != VIO_DRING_REG) {
   5131 		return (EPROTO);
   5132 	}
   5133 
   5134 	switch (dring_msg->tag.vio_subtype) {
   5135 	case VIO_SUBTYPE_ACK:
   5136 		/* save the received dring_ident */
   5137 		vdc->dring_ident = dring_msg->dring_ident;
   5138 		DMSG(vdc, 0, "[%d] Received dring ident=0x%lx\n",
   5139 		    vdc->instance, vdc->dring_ident);
   5140 		break;
   5141 
   5142 	case VIO_SUBTYPE_NACK:
   5143 		/*
   5144 		 * vds could not handle the DRing info we sent so we
   5145 		 * stop negotiating.
   5146 		 */
   5147 		DMSG(vdc, 0, "[%d] server could not register DRing\n",
   5148 		    vdc->instance);
   5149 		status = EPROTO;
   5150 		break;
   5151 
   5152 	case VIO_SUBTYPE_INFO:
   5153 		/*
   5154 		 * Handle the case where vds starts handshake
   5155 		 * (for now only vdc is the instigatior)
   5156 		 */
   5157 		status = ENOTSUP;
   5158 		break;
   5159 	default:
   5160 		status = ENOTSUP;
   5161 	}
   5162 
   5163 	return (status);
   5164 }
   5165 
   5166 /*
   5167  * Function:
   5168  *	vdc_verify_seq_num()
   5169  *
   5170  * Description:
   5171  *	This functions verifies that the sequence number sent back by the vDisk
   5172  *	server with the latest message is what is expected (i.e. it is greater
   5173  *	than the last seq num sent by the vDisk server and less than or equal
   5174  *	to the last seq num generated by vdc).
   5175  *
   5176  *	It then checks the request ID to see if any requests need processing
   5177  *	in the DRing.
   5178  *
   5179  * Arguments:
   5180  *	vdc		- soft state pointer for this instance of the driver.
   5181  *	dring_msg	- pointer to the LDC message sent by vds
   5182  *
   5183  * Return Code:
   5184  *	VDC_SEQ_NUM_TODO	- Message needs to be processed
   5185  *	VDC_SEQ_NUM_SKIP	- Message has already been processed
   5186  *	VDC_SEQ_NUM_INVALID	- The seq numbers are so out of sync,
   5187  *				  vdc cannot deal with them
   5188  */
   5189 static int
   5190 vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg)
   5191 {
   5192 	ASSERT(vdc != NULL);
   5193 	ASSERT(dring_msg != NULL);
   5194 	ASSERT(mutex_owned(&vdc->lock));
   5195 
   5196 	/*
   5197 	 * Check to see if the messages were responded to in the correct
   5198 	 * order by vds.
   5199 	 */
   5200 	if ((dring_msg->seq_num <= vdc->seq_num_reply) ||
   5201 	    (dring_msg->seq_num > vdc->seq_num)) {
   5202 		DMSG(vdc, 0, "?[%d] Bogus sequence_number %lu: "
   5203 		    "%lu > expected <= %lu (last proc req %lu sent %lu)\n",
   5204 		    vdc->instance, dring_msg->seq_num,
   5205 		    vdc->seq_num_reply, vdc->seq_num,
   5206 		    vdc->req_id_proc, vdc->req_id);
   5207 		return (VDC_SEQ_NUM_INVALID);
   5208 	}
   5209 	vdc->seq_num_reply = dring_msg->seq_num;
   5210 
   5211 	if (vdc->req_id_proc < vdc->req_id)
   5212 		return (VDC_SEQ_NUM_TODO);
   5213 	else
   5214 		return (VDC_SEQ_NUM_SKIP);
   5215 }
   5216 
   5217 
   5218 /*
   5219  * Function:
   5220  *	vdc_is_supported_version()
   5221  *
   5222  * Description:
   5223  *	This routine checks if the major/minor version numbers specified in
   5224  *	'ver_msg' are supported. If not it finds the next version that is
   5225  *	in the supported version list 'vdc_version[]' and sets the fields in
   5226  *	'ver_msg' to those values
   5227  *
   5228  * Arguments:
   5229  *	ver_msg	- LDC message sent by vDisk server
   5230  *
   5231  * Return Code:
   5232  *	B_TRUE	- Success
   5233  *	B_FALSE	- Version not supported
   5234  */
   5235 static boolean_t
   5236 vdc_is_supported_version(vio_ver_msg_t *ver_msg)
   5237 {
   5238 	int vdc_num_versions = sizeof (vdc_version) / sizeof (vdc_version[0]);
   5239 
   5240 	for (int i = 0; i < vdc_num_versions; i++) {
   5241 		ASSERT(vdc_version[i].major > 0);
   5242 		ASSERT((i == 0) ||
   5243 		    (vdc_version[i].major < vdc_version[i-1].major));
   5244 
   5245 		/*
   5246 		 * If the major versions match, adjust the minor version, if
   5247 		 * necessary, down to the highest value supported by this
   5248 		 * client. The server should support all minor versions lower
   5249 		 * than the value it sent
   5250 		 */
   5251 		if (ver_msg->ver_major == vdc_version[i].major) {
   5252 			if (ver_msg->ver_minor > vdc_version[i].minor) {
   5253 				DMSGX(0,
   5254 				    "Adjusting minor version from %u to %u",
   5255 				    ver_msg->ver_minor, vdc_version[i].minor);
   5256 				ver_msg->ver_minor = vdc_version[i].minor;
   5257 			}
   5258 			return (B_TRUE);
   5259 		}
   5260 
   5261 		/*
   5262 		 * If the message contains a higher major version number, set
   5263 		 * the message's major/minor versions to the current values
   5264 		 * and return false, so this message will get resent with
   5265 		 * these values, and the server will potentially try again
   5266 		 * with the same or a lower version
   5267 		 */
   5268 		if (ver_msg->ver_major > vdc_version[i].major) {
   5269 			ver_msg->ver_major = vdc_version[i].major;
   5270 			ver_msg->ver_minor = vdc_version[i].minor;
   5271 			DMSGX(0, "Suggesting major/minor (0x%x/0x%x)\n",
   5272 			    ver_msg->ver_major, ver_msg->ver_minor);
   5273 
   5274 			return (B_FALSE);
   5275 		}
   5276 
   5277 		/*
   5278 		 * Otherwise, the message's major version is less than the
   5279 		 * current major version, so continue the loop to the next
   5280 		 * (lower) supported version
   5281 		 */
   5282 	}
   5283 
   5284 	/*
   5285 	 * No common version was found; "ground" the version pair in the
   5286 	 * message to terminate negotiation
   5287 	 */
   5288 	ver_msg->ver_major = 0;
   5289 	ver_msg->ver_minor = 0;
   5290 
   5291 	return (B_FALSE);
   5292 }
   5293 /* -------------------------------------------------------------------------- */
   5294 
   5295 /*
   5296  * DKIO(7) support
   5297  */
   5298 
   5299 typedef struct vdc_dk_arg {
   5300 	struct dk_callback	dkc;
   5301 	int			mode;
   5302 	dev_t			dev;
   5303 	vdc_t			*vdc;
   5304 } vdc_dk_arg_t;
   5305 
   5306 /*
   5307  * Function:
   5308  * 	vdc_dkio_flush_cb()
   5309  *
   5310  * Description:
   5311  *	This routine is a callback for DKIOCFLUSHWRITECACHE which can be called
   5312  *	by kernel code.
   5313  *
   5314  * Arguments:
   5315  *	arg	- a pointer to a vdc_dk_arg_t structure.
   5316  */
   5317 void
   5318 vdc_dkio_flush_cb(void *arg)
   5319 {
   5320 	struct vdc_dk_arg	*dk_arg = (struct vdc_dk_arg *)arg;
   5321 	struct dk_callback	*dkc = NULL;
   5322 	vdc_t			*vdc = NULL;
   5323 	int			rv;
   5324 
   5325 	if (dk_arg == NULL) {
   5326 		cmn_err(CE_NOTE, "?[Unk] DKIOCFLUSHWRITECACHE arg is NULL\n");
   5327 		return;
   5328 	}
   5329 	dkc = &dk_arg->dkc;
   5330 	vdc = dk_arg->vdc;
   5331 	ASSERT(vdc != NULL);
   5332 
   5333 	rv = vdc_do_sync_op(vdc, VD_OP_FLUSH, NULL, 0,
   5334 	    VDCPART(dk_arg->dev), 0, VIO_both_dir, B_TRUE);
   5335 	if (rv != 0) {
   5336 		DMSG(vdc, 0, "[%d] DKIOCFLUSHWRITECACHE failed %d : model %x\n",
   5337 		    vdc->instance, rv,
   5338 		    ddi_model_convert_from(dk_arg->mode & FMODELS));
   5339 	}
   5340 
   5341 	/*
   5342 	 * Trigger the call back to notify the caller the the ioctl call has
   5343 	 * been completed.
   5344 	 */
   5345 	if ((dk_arg->mode & FKIOCTL) &&
   5346 	    (dkc != NULL) &&
   5347 	    (dkc->dkc_callback != NULL)) {
   5348 		ASSERT(dkc->dkc_cookie != NULL);
   5349 		(*dkc->dkc_callback)(dkc->dkc_cookie, rv);
   5350 	}
   5351 
   5352 	/* Indicate that one less DKIO write flush is outstanding */
   5353 	mutex_enter(&vdc->lock);
   5354 	vdc->dkio_flush_pending--;
   5355 	ASSERT(vdc->dkio_flush_pending >= 0);
   5356 	mutex_exit(&vdc->lock);
   5357 
   5358 	/* free the mem that was allocated when the callback was dispatched */
   5359 	kmem_free(arg, sizeof (vdc_dk_arg_t));
   5360 }
   5361 
   5362 /*
   5363  * Function:
   5364  * 	vdc_dkio_gapart()
   5365  *
   5366  * Description:
   5367  *	This function implements the DKIOCGAPART ioctl.
   5368  *
   5369  * Arguments:
   5370  *	vdc	- soft state pointer
   5371  *	arg	- a pointer to a dk_map[NDKMAP] or dk_map32[NDKMAP] structure
   5372  *	flag	- ioctl flags
   5373  */
   5374 static int
   5375 vdc_dkio_gapart(vdc_t *vdc, caddr_t arg, int flag)
   5376 {
   5377 	struct dk_geom *geom;
   5378 	struct extvtoc *vtoc;
   5379 	union {
   5380 		struct dk_map map[NDKMAP];
   5381 		struct dk_map32 map32[NDKMAP];
   5382 	} data;
   5383 	int i, rv, size;
   5384 
   5385 	mutex_enter(&vdc->lock);
   5386 
   5387 	if ((rv = vdc_validate_geometry(vdc)) != 0) {
   5388 		mutex_exit(&vdc->lock);
   5389 		return (rv);
   5390 	}
   5391 
   5392 	if (vdc->vdisk_size > VD_OLDVTOC_LIMIT) {
   5393 		mutex_exit(&vdc->lock);
   5394 		return (EOVERFLOW);
   5395 	}
   5396 
   5397 	vtoc = vdc->vtoc;
   5398 	geom = vdc->geom;
   5399 
   5400 	if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) {
   5401 
   5402 		for (i = 0; i < vtoc->v_nparts; i++) {
   5403 			data.map32[i].dkl_cylno = vtoc->v_part[i].p_start /
   5404 			    (geom->dkg_nhead * geom->dkg_nsect);
   5405 			data.map32[i].dkl_nblk = vtoc->v_part[i].p_size;
   5406 		}
   5407 		size = NDKMAP * sizeof (struct dk_map32);
   5408 
   5409 	} else {
   5410 
   5411 		for (i = 0; i < vtoc->v_nparts; i++) {
   5412 			data.map[i].dkl_cylno = vtoc->v_part[i].p_start /
   5413 			    (geom->dkg_nhead * geom->dkg_nsect);
   5414 			data.map[i].dkl_nblk = vtoc->v_part[i].p_size;
   5415 		}
   5416 		size = NDKMAP * sizeof (struct dk_map);
   5417 
   5418 	}
   5419 
   5420 	mutex_exit(&vdc->lock);
   5421 
   5422 	if (ddi_copyout(&data, arg, size, flag) != 0)
   5423 		return (EFAULT);
   5424 
   5425 	return (0);
   5426 }
   5427 
   5428 /*
   5429  * Function:
   5430  * 	vdc_dkio_partition()
   5431  *
   5432  * Description:
   5433  *	This function implements the DKIOCPARTITION ioctl.
   5434  *
   5435  * Arguments:
   5436  *	vdc	- soft state pointer
   5437  *	arg	- a pointer to a struct partition64 structure
   5438  *	flag	- ioctl flags
   5439  */
   5440 static int
   5441 vdc_dkio_partition(vdc_t *vdc, caddr_t arg, int flag)
   5442 {
   5443 	struct partition64 p64;
   5444 	efi_gpt_t *gpt;
   5445 	efi_gpe_t *gpe;
   5446 	vd_efi_dev_t edev;
   5447 	uint_t partno;
   5448 	int rv;
   5449 
   5450 	if (ddi_copyin(arg, &p64, sizeof (struct partition64), flag)) {
   5451 		return (EFAULT);
   5452 	}
   5453 
   5454 	VDC_EFI_DEV_SET(edev, vdc, vd_process_efi_ioctl);
   5455 
   5456 	if ((rv = vd_efi_alloc_and_read(&edev, &gpt, &gpe)) != 0) {
   5457 		return (rv);
   5458 	}
   5459 
   5460 	partno = p64.p_partno;
   5461 
   5462 	if (partno >= gpt->efi_gpt_NumberOfPartitionEntries) {
   5463 		vd_efi_free(&edev, gpt, gpe);
   5464 		return (ESRCH);
   5465 	}
   5466 
   5467 	bcopy(&gpe[partno].efi_gpe_PartitionTypeGUID, &p64.p_type,
   5468 	    sizeof (struct uuid));
   5469 	p64.p_start = gpe[partno].efi_gpe_StartingLBA;
   5470 	p64.p_size = gpe[partno].efi_gpe_EndingLBA - p64.p_start + 1;
   5471 
   5472 	if (ddi_copyout(&p64, arg, sizeof (struct partition64), flag)) {
   5473 		vd_efi_free(&edev, gpt, gpe);
   5474 		return (EFAULT);
   5475 	}
   5476 
   5477 	vd_efi_free(&edev, gpt, gpe);
   5478 	return (0);
   5479 }
   5480 
   5481 /*
   5482  * Function:
   5483  * 	vdc_dioctl_rwcmd()
   5484  *
   5485  * Description:
   5486  *	This function implements the DIOCTL_RWCMD ioctl. This ioctl is used
   5487  *	for DKC_DIRECT disks to read or write at an absolute disk offset.
   5488  *
   5489  * Arguments:
   5490  *	dev	- device
   5491  *	arg	- a pointer to a dadkio_rwcmd or dadkio_rwcmd32 structure
   5492  *	flag	- ioctl flags
   5493  */
   5494 static int
   5495 vdc_dioctl_rwcmd(vdc_t *vdc, caddr_t arg, int flag)
   5496 {
   5497 	struct dadkio_rwcmd32 rwcmd32;
   5498 	struct dadkio_rwcmd rwcmd;
   5499 	struct iovec aiov;
   5500 	struct uio auio;
   5501 	int rw, status;
   5502 	struct buf *buf;
   5503 
   5504 	if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) {
   5505 		if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd32,
   5506 		    sizeof (struct dadkio_rwcmd32), flag)) {
   5507 			return (EFAULT);
   5508 		}
   5509 		rwcmd.cmd = rwcmd32.cmd;
   5510 		rwcmd.flags = rwcmd32.flags;
   5511 		rwcmd.blkaddr = (daddr_t)rwcmd32.blkaddr;
   5512 		rwcmd.buflen = rwcmd32.buflen;
   5513 		rwcmd.bufaddr = (caddr_t)(uintptr_t)rwcmd32.bufaddr;
   5514 	} else {
   5515 		if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd,
   5516 		    sizeof (struct dadkio_rwcmd), flag)) {
   5517 			return (EFAULT);
   5518 		}
   5519 	}
   5520 
   5521 	switch (rwcmd.cmd) {
   5522 	case DADKIO_RWCMD_READ:
   5523 		rw = B_READ;
   5524 		break;
   5525 	case DADKIO_RWCMD_WRITE:
   5526 		rw = B_WRITE;
   5527 		break;
   5528 	default:
   5529 		return (EINVAL);
   5530 	}
   5531 
   5532 	bzero((caddr_t)&aiov, sizeof (struct iovec));
   5533 	aiov.iov_base   = rwcmd.bufaddr;
   5534 	aiov.iov_len    = rwcmd.buflen;
   5535 
   5536 	bzero((caddr_t)&auio, sizeof (struct uio));
   5537 	auio.uio_iov    = &aiov;
   5538 	auio.uio_iovcnt = 1;
   5539 	auio.uio_loffset = rwcmd.blkaddr * vdc->vdisk_bsize;
   5540 	auio.uio_resid  = rwcmd.buflen;
   5541 	auio.uio_segflg = flag & FKIOCTL ? UIO_SYSSPACE : UIO_USERSPACE;
   5542 
   5543 	buf = kmem_alloc(sizeof (buf_t), KM_SLEEP);
   5544 	bioinit(buf);
   5545 	/*
   5546 	 * We use the private field of buf to specify that this is an
   5547 	 * I/O using an absolute offset.
   5548 	 */
   5549 	buf->b_private = (void *)VD_SLICE_NONE;
   5550 
   5551 	status = physio(vdc_strategy, buf, VD_MAKE_DEV(vdc->instance, 0),
   5552 	    rw, vdc_min, &auio);
   5553 
   5554 	biofini(buf);
   5555 	kmem_free(buf, sizeof (buf_t));
   5556 
   5557 	return (status);
   5558 }
   5559 
   5560 /*
   5561  * Allocate a buffer for a VD_OP_SCSICMD operation. The size of the allocated
   5562  * buffer is returned in alloc_len.
   5563  */
   5564 static vd_scsi_t *
   5565 vdc_scsi_alloc(int cdb_len, int sense_len, int datain_len, int dataout_len,
   5566     int *alloc_len)
   5567 {
   5568 	vd_scsi_t *vd_scsi;
   5569 	int vd_scsi_len = VD_SCSI_SIZE;
   5570 
   5571 	vd_scsi_len += P2ROUNDUP(cdb_len, sizeof (uint64_t));
   5572 	vd_scsi_len += P2ROUNDUP(sense_len, sizeof (uint64_t));
   5573 	vd_scsi_len += P2ROUNDUP(datain_len, sizeof (uint64_t));
   5574 	vd_scsi_len += P2ROUNDUP(dataout_len, sizeof (uint64_t));
   5575 
   5576 	ASSERT(vd_scsi_len % sizeof (uint64_t) == 0);
   5577 
   5578 	vd_scsi = kmem_zalloc(vd_scsi_len, KM_SLEEP);
   5579 
   5580 	vd_scsi->cdb_len = cdb_len;
   5581 	vd_scsi->sense_len = sense_len;
   5582 	vd_scsi->datain_len = datain_len;
   5583 	vd_scsi->dataout_len = dataout_len;
   5584 
   5585 	*alloc_len = vd_scsi_len;
   5586 
   5587 	return (vd_scsi);
   5588 }
   5589 
   5590 /*
   5591  * Convert the status of a SCSI command to a Solaris return code.
   5592  *
   5593  * Arguments:
   5594  *	vd_scsi		- The SCSI operation buffer.
   5595  *	log_error	- indicate if an error message should be logged.
   5596  *
   5597  * Note that our SCSI error messages are rather primitive for the moment
   5598  * and could be improved by decoding some data like the SCSI command and
   5599  * the sense key.
   5600  *
   5601  * Return value:
   5602  *	0		- Status is good.
   5603  *	EACCES		- Status reports a reservation conflict.
   5604  *	ENOTSUP		- Status reports a check condition and sense key
   5605  *			  reports an illegal request.
   5606  *	EIO		- Any other status.
   5607  */
   5608 static int
   5609 vdc_scsi_status(vdc_t *vdc, vd_scsi_t *vd_scsi, boolean_t log_error)
   5610 {
   5611 	int rv;
   5612 	char path_str[MAXPATHLEN];
   5613 	char panic_str[VDC_RESV_CONFLICT_FMT_LEN + MAXPATHLEN];
   5614 	union scsi_cdb *cdb;
   5615 	struct scsi_extended_sense *sense;
   5616 
   5617 	if (vd_scsi->cmd_status == STATUS_GOOD)
   5618 		/* no error */
   5619 		return (0);
   5620 
   5621 	/* when the tunable vdc_scsi_log_error is true we log all errors */
   5622 	if (vdc_scsi_log_error)
   5623 		log_error = B_TRUE;
   5624 
   5625 	if (log_error) {
   5626 		cmn_err(CE_WARN, "%s (vdc%d):\tError for Command: 0x%x)\n",
   5627 		    ddi_pathname(vdc->dip, path_str), vdc->instance,
   5628 		    GETCMD(VD_SCSI_DATA_CDB(vd_scsi)));
   5629 	}
   5630 
   5631 	/* default returned value */
   5632 	rv = EIO;
   5633 
   5634 	switch (vd_scsi->cmd_status) {
   5635 
   5636 	case STATUS_CHECK:
   5637 	case STATUS_TERMINATED:
   5638 		if (log_error)
   5639 			cmn_err(CE_CONT, "\tCheck Condition Error\n");
   5640 
   5641 		/* check sense buffer */
   5642 		if (vd_scsi->sense_len == 0 ||
   5643 		    vd_scsi->sense_status != STATUS_GOOD) {
   5644 			if (log_error)
   5645 				cmn_err(CE_CONT, "\tNo Sense Data Available\n");
   5646 			break;
   5647 		}
   5648 
   5649 		sense = VD_SCSI_DATA_SENSE(vd_scsi);
   5650 
   5651 		if (log_error) {
   5652 			cmn_err(CE_CONT, "\tSense Key:  0x%x\n"
   5653 			    "\tASC: 0x%x, ASCQ: 0x%x\n",
   5654 			    scsi_sense_key((uint8_t *)sense),
   5655 			    scsi_sense_asc((uint8_t *)sense),
   5656 			    scsi_sense_ascq((uint8_t *)sense));
   5657 		}
   5658 
   5659 		if (scsi_sense_key((uint8_t *)sense) == KEY_ILLEGAL_REQUEST)
   5660 			rv = ENOTSUP;
   5661 		break;
   5662 
   5663 	case STATUS_BUSY:
   5664 		if (log_error)
   5665 			cmn_err(CE_NOTE, "\tDevice Busy\n");
   5666 		break;
   5667 
   5668 	case STATUS_RESERVATION_CONFLICT:
   5669 		/*
   5670 		 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then
   5671 		 * reservation conflict could be due to various reasons like
   5672 		 * incorrect keys, not registered or not reserved etc. So,
   5673 		 * we should not panic in that case.
   5674 		 */
   5675 		cdb = VD_SCSI_DATA_CDB(vd_scsi);
   5676 		if (vdc->failfast_interval != 0 &&
   5677 		    cdb->scc_cmd != SCMD_PERSISTENT_RESERVE_IN &&
   5678 		    cdb->scc_cmd != SCMD_PERSISTENT_RESERVE_OUT) {
   5679 			/* failfast is enabled so we have to panic */
   5680 			(void) snprintf(panic_str, sizeof (panic_str),
   5681 			    VDC_RESV_CONFLICT_FMT_STR "%s",
   5682 			    ddi_pathname(vdc->dip, path_str));
   5683 			panic(panic_str);
   5684 		}
   5685 		if (log_error)
   5686 			cmn_err(CE_NOTE, "\tReservation Conflict\n");
   5687 		rv = EACCES;
   5688 		break;
   5689 
   5690 	case STATUS_QFULL:
   5691 		if (log_error)
   5692 			cmn_err(CE_NOTE, "\tQueue Full\n");
   5693 		break;
   5694 
   5695 	case STATUS_MET:
   5696 	case STATUS_INTERMEDIATE:
   5697 	case STATUS_SCSI2:
   5698 	case STATUS_INTERMEDIATE_MET:
   5699 	case STATUS_ACA_ACTIVE:
   5700 		if (log_error)
   5701 			cmn_err(CE_CONT,
   5702 			    "\tUnexpected SCSI status received: 0x%x\n",
   5703 			    vd_scsi->cmd_status);
   5704 		break;
   5705 
   5706 	default:
   5707 		if (log_error)
   5708 			cmn_err(CE_CONT,
   5709 			    "\tInvalid SCSI status received: 0x%x\n",
   5710 			    vd_scsi->cmd_status);
   5711 		break;
   5712 	}
   5713 
   5714 	return (rv);
   5715 }
   5716 
   5717 /*
   5718  * Implemented the USCSICMD uscsi(7I) ioctl. This ioctl is converted to
   5719  * a VD_OP_SCSICMD operation which is sent to the vdisk server. If a SCSI
   5720  * reset is requested (i.e. a flag USCSI_RESET* is set) then the ioctl is
   5721  * converted to a VD_OP_RESET operation.
   5722  */
   5723 static int
   5724 vdc_uscsi_cmd(vdc_t *vdc, caddr_t arg, int mode)
   5725 {
   5726 	struct uscsi_cmd 	uscsi;
   5727 	struct uscsi_cmd32	uscsi32;
   5728 	vd_scsi_t 		*vd_scsi;
   5729 	int 			vd_scsi_len;
   5730 	union scsi_cdb		*cdb;
   5731 	struct scsi_extended_sense *sense;
   5732 	char 			*datain, *dataout;
   5733 	size_t			cdb_len, datain_len, dataout_len, sense_len;
   5734 	int 			rv;
   5735 
   5736 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
   5737 		if (ddi_copyin(arg, &uscsi32, sizeof (struct uscsi_cmd32),
   5738 		    mode) != 0)
   5739 			return (EFAULT);
   5740 		uscsi_cmd32touscsi_cmd((&uscsi32), (&uscsi));
   5741 	} else {
   5742 		if (ddi_copyin(arg, &uscsi, sizeof (struct uscsi_cmd),
   5743 		    mode) != 0)
   5744 			return (EFAULT);
   5745 	}
   5746 
   5747 	/* a uscsi reset is converted to a VD_OP_RESET operation */
   5748 	if (uscsi.uscsi_flags & (USCSI_RESET | USCSI_RESET_LUN |
   5749 	    USCSI_RESET_ALL)) {
   5750 		rv = vdc_do_sync_op(vdc, VD_OP_RESET, NULL, 0, 0, 0,
   5751 		    VIO_both_dir, B_TRUE);
   5752 		return (rv);
   5753 	}
   5754 
   5755 	/* cdb buffer length */
   5756 	cdb_len = uscsi.uscsi_cdblen;
   5757 
   5758 	/* data in and out buffers length */
   5759 	if (uscsi.uscsi_flags & USCSI_READ) {
   5760 		datain_len = uscsi.uscsi_buflen;
   5761 		dataout_len = 0;
   5762 	} else {
   5763 		datain_len = 0;
   5764 		dataout_len = uscsi.uscsi_buflen;
   5765 	}
   5766 
   5767 	/* sense buffer length */
   5768 	if (uscsi.uscsi_flags & USCSI_RQENABLE)
   5769 		sense_len = uscsi.uscsi_rqlen;
   5770 	else
   5771 		sense_len = 0;
   5772 
   5773 	/* allocate buffer for the VD_SCSICMD_OP operation */
   5774 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
   5775 	    &vd_scsi_len);
   5776 
   5777 	/*
   5778 	 * The documentation of USCSI_ISOLATE and USCSI_DIAGNOSE is very vague,
   5779 	 * but basically they prevent a SCSI command from being retried in case
   5780 	 * of an error.
   5781 	 */
   5782 	if ((uscsi.uscsi_flags & USCSI_ISOLATE) ||
   5783 	    (uscsi.uscsi_flags & USCSI_DIAGNOSE))
   5784 		vd_scsi->options |= VD_SCSI_OPT_NORETRY;
   5785 
   5786 	/* set task attribute */
   5787 	if (uscsi.uscsi_flags & USCSI_NOTAG) {
   5788 		vd_scsi->task_attribute = 0;
   5789 	} else {
   5790 		if (uscsi.uscsi_flags & USCSI_HEAD)
   5791 			vd_scsi->task_attribute = VD_SCSI_TASK_ACA;
   5792 		else if (uscsi.uscsi_flags & USCSI_HTAG)
   5793 			vd_scsi->task_attribute = VD_SCSI_TASK_HQUEUE;
   5794 		else if (uscsi.uscsi_flags & USCSI_OTAG)
   5795 			vd_scsi->task_attribute = VD_SCSI_TASK_ORDERED;
   5796 		else
   5797 			vd_scsi->task_attribute = 0;
   5798 	}
   5799 
   5800 	/* set timeout */
   5801 	vd_scsi->timeout = uscsi.uscsi_timeout;
   5802 
   5803 	/* copy-in cdb data */
   5804 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
   5805 	if (ddi_copyin(uscsi.uscsi_cdb, cdb, cdb_len, mode) != 0) {
   5806 		rv = EFAULT;
   5807 		goto done;
   5808 	}
   5809 
   5810 	/* keep a pointer to the sense buffer */
   5811 	sense = VD_SCSI_DATA_SENSE(vd_scsi);
   5812 
   5813 	/* keep a pointer to the data-in buffer */
   5814 	datain = (char *)VD_SCSI_DATA_IN(vd_scsi);
   5815 
   5816 	/* copy-in request data to the data-out buffer */
   5817 	dataout = (char *)VD_SCSI_DATA_OUT(vd_scsi);
   5818 	if (!(uscsi.uscsi_flags & USCSI_READ)) {
   5819 		if (ddi_copyin(uscsi.uscsi_bufaddr, dataout, dataout_len,
   5820 		    mode)) {
   5821 			rv = EFAULT;
   5822 			goto done;
   5823 		}
   5824 	}
   5825 
   5826 	/* submit the request */
   5827 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
   5828 	    0, 0, VIO_both_dir, B_FALSE);
   5829 
   5830 	if (rv != 0)
   5831 		goto done;
   5832 
   5833 	/* update scsi status */
   5834 	uscsi.uscsi_status = vd_scsi->cmd_status;
   5835 
   5836 	/* update sense data */
   5837 	if ((uscsi.uscsi_flags & USCSI_RQENABLE) &&
   5838 	    (uscsi.uscsi_status == STATUS_CHECK ||
   5839 	    uscsi.uscsi_status == STATUS_TERMINATED)) {
   5840 
   5841 		uscsi.uscsi_rqstatus = vd_scsi->sense_status;
   5842 
   5843 		if (uscsi.uscsi_rqstatus == STATUS_GOOD) {
   5844 			uscsi.uscsi_rqresid = uscsi.uscsi_rqlen -
   5845 			    vd_scsi->sense_len;
   5846 			if (ddi_copyout(sense, uscsi.uscsi_rqbuf,
   5847 			    vd_scsi->sense_len, mode) != 0) {
   5848 				rv = EFAULT;
   5849 				goto done;
   5850 			}
   5851 		}
   5852 	}
   5853 
   5854 	/* update request data */
   5855 	if (uscsi.uscsi_status == STATUS_GOOD) {
   5856 		if (uscsi.uscsi_flags & USCSI_READ) {
   5857 			uscsi.uscsi_resid = uscsi.uscsi_buflen -
   5858 			    vd_scsi->datain_len;
   5859 			if (ddi_copyout(datain, uscsi.uscsi_bufaddr,
   5860 			    vd_scsi->datain_len, mode) != 0) {
   5861 				rv = EFAULT;
   5862 				goto done;
   5863 			}
   5864 		} else {
   5865 			uscsi.uscsi_resid = uscsi.uscsi_buflen -
   5866 			    vd_scsi->dataout_len;
   5867 		}
   5868 	}
   5869 
   5870 	/* copy-out result */
   5871 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
   5872 		uscsi_cmdtouscsi_cmd32((&uscsi), (&uscsi32));
   5873 		if (ddi_copyout(&uscsi32, arg, sizeof (struct uscsi_cmd32),
   5874 		    mode) != 0) {
   5875 			rv = EFAULT;
   5876 			goto done;
   5877 		}
   5878 	} else {
   5879 		if (ddi_copyout(&uscsi, arg, sizeof (struct uscsi_cmd),
   5880 		    mode) != 0) {
   5881 			rv = EFAULT;
   5882 			goto done;
   5883 		}
   5884 	}
   5885 
   5886 	/* get the return code from the SCSI command status */
   5887 	rv = vdc_scsi_status(vdc, vd_scsi,
   5888 	    !(uscsi.uscsi_flags & USCSI_SILENT));
   5889 
   5890 done:
   5891 	kmem_free(vd_scsi, vd_scsi_len);
   5892 	return (rv);
   5893 }
   5894 
   5895 /*
   5896  * Create a VD_OP_SCSICMD buffer for a SCSI PERSISTENT IN command.
   5897  *
   5898  * Arguments:
   5899  *	cmd		- SCSI PERSISTENT IN command
   5900  *	len		- length of the SCSI input buffer
   5901  *	vd_scsi_len	- return the length of the allocated buffer
   5902  *
   5903  * Returned Value:
   5904  *	a pointer to the allocated VD_OP_SCSICMD buffer.
   5905  */
   5906 static vd_scsi_t *
   5907 vdc_scsi_alloc_persistent_in(uchar_t cmd, int len, int *vd_scsi_len)
   5908 {
   5909 	int cdb_len, sense_len, datain_len, dataout_len;
   5910 	vd_scsi_t *vd_scsi;
   5911 	union scsi_cdb *cdb;
   5912 
   5913 	cdb_len = CDB_GROUP1;
   5914 	sense_len = sizeof (struct scsi_extended_sense);
   5915 	datain_len = len;
   5916 	dataout_len = 0;
   5917 
   5918 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
   5919 	    vd_scsi_len);
   5920 
   5921 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
   5922 
   5923 	/* set cdb */
   5924 	cdb->scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
   5925 	cdb->cdb_opaque[1] = cmd;
   5926 	FORMG1COUNT(cdb, datain_len);
   5927 
   5928 	vd_scsi->timeout = vdc_scsi_timeout;
   5929 
   5930 	return (vd_scsi);
   5931 }
   5932 
   5933 /*
   5934  * Create a VD_OP_SCSICMD buffer for a SCSI PERSISTENT OUT command.
   5935  *
   5936  * Arguments:
   5937  *	cmd		- SCSI PERSISTENT OUT command
   5938  *	len		- length of the SCSI output buffer
   5939  *	vd_scsi_len	- return the length of the allocated buffer
   5940  *
   5941  * Returned Code:
   5942  *	a pointer to the allocated VD_OP_SCSICMD buffer.
   5943  */
   5944 static vd_scsi_t *
   5945 vdc_scsi_alloc_persistent_out(uchar_t cmd, int len, int *vd_scsi_len)
   5946 {
   5947 	int cdb_len, sense_len, datain_len, dataout_len;
   5948 	vd_scsi_t *vd_scsi;
   5949 	union scsi_cdb *cdb;
   5950 
   5951 	cdb_len = CDB_GROUP1;
   5952 	sense_len = sizeof (struct scsi_extended_sense);
   5953 	datain_len = 0;
   5954 	dataout_len = len;
   5955 
   5956 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
   5957 	    vd_scsi_len);
   5958 
   5959 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
   5960 
   5961 	/* set cdb */
   5962 	cdb->scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
   5963 	cdb->cdb_opaque[1] = cmd;
   5964 	FORMG1COUNT(cdb, dataout_len);
   5965 
   5966 	vd_scsi->timeout = vdc_scsi_timeout;
   5967 
   5968 	return (vd_scsi);
   5969 }
   5970 
   5971 /*
   5972  * Implement the MHIOCGRP_INKEYS mhd(7i) ioctl. The ioctl is converted
   5973  * to a SCSI PERSISTENT IN READ KEYS command which is sent to the vdisk
   5974  * server with a VD_OP_SCSICMD operation.
   5975  */
   5976 static int
   5977 vdc_mhd_inkeys(vdc_t *vdc, caddr_t arg, int mode)
   5978 {
   5979 	vd_scsi_t *vd_scsi;
   5980 	mhioc_inkeys_t inkeys;
   5981 	mhioc_key_list_t klist;
   5982 	struct mhioc_inkeys32 inkeys32;
   5983 	struct mhioc_key_list32 klist32;
   5984 	sd_prin_readkeys_t *scsi_keys;
   5985 	void *user_keys;
   5986 	int vd_scsi_len;
   5987 	int listsize, listlen, rv;
   5988 
   5989 	/* copyin arguments */
   5990 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
   5991 		rv = ddi_copyin(arg, &inkeys32, sizeof (inkeys32), mode);
   5992 		if (rv != 0)
   5993 			return (EFAULT);
   5994 
   5995 		rv = ddi_copyin((caddr_t)(uintptr_t)inkeys32.li, &klist32,
   5996 		    sizeof (klist32), mode);
   5997 		if (rv != 0)
   5998 			return (EFAULT);
   5999 
   6000 		listsize = klist32.listsize;
   6001 	} else {
   6002 		rv = ddi_copyin(arg, &inkeys, sizeof (inkeys), mode);
   6003 		if (rv != 0)
   6004 			return (EFAULT);
   6005 
   6006 		rv = ddi_copyin(inkeys.li, &klist, sizeof (klist), mode);
   6007 		if (rv != 0)
   6008 			return (EFAULT);
   6009 
   6010 		listsize = klist.listsize;
   6011 	}
   6012 
   6013 	/* build SCSI VD_OP request */
   6014 	vd_scsi = vdc_scsi_alloc_persistent_in(SD_READ_KEYS,
   6015 	    sizeof (sd_prin_readkeys_t) - sizeof (caddr_t) +
   6016 	    (sizeof (mhioc_resv_key_t) * listsize), &vd_scsi_len);
   6017 
   6018 	scsi_keys = (sd_prin_readkeys_t *)VD_SCSI_DATA_IN(vd_scsi);
   6019 
   6020 	/* submit the request */
   6021 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
   6022 	    0, 0, VIO_both_dir, B_FALSE);
   6023 
   6024 	if (rv != 0)
   6025 		goto done;
   6026 
   6027 	listlen = scsi_keys->len / MHIOC_RESV_KEY_SIZE;
   6028 
   6029 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
   6030 		inkeys32.generation = scsi_keys->generation;
   6031 		rv = ddi_copyout(&inkeys32, arg, sizeof (inkeys32), mode);
   6032 		if (rv != 0) {
   6033 			rv = EFAULT;
   6034 			goto done;
   6035 		}
   6036 
   6037 		klist32.listlen = listlen;
   6038 		rv = ddi_copyout(&klist32, (caddr_t)(uintptr_t)inkeys32.li,
   6039 		    sizeof (klist32), mode);
   6040 		if (rv != 0) {
   6041 			rv = EFAULT;
   6042 			goto done;
   6043 		}
   6044 
   6045 		user_keys = (caddr_t)(uintptr_t)klist32.list;
   6046 	} else {
   6047 		inkeys.generation = scsi_keys->generation;
   6048 		rv = ddi_copyout(&inkeys, arg, sizeof (inkeys), mode);
   6049 		if (rv != 0) {
   6050 			rv = EFAULT;
   6051 			goto done;
   6052 		}
   6053 
   6054 		klist.listlen = listlen;
   6055 		rv = ddi_copyout(&klist, inkeys.li, sizeof (klist), mode);
   6056 		if (rv != 0) {
   6057 			rv = EFAULT;
   6058 			goto done;
   6059 		}
   6060 
   6061 		user_keys = klist.list;
   6062 	}
   6063 
   6064 	/* copy out keys */
   6065 	if (listlen > 0 && listsize > 0) {
   6066 		if (listsize < listlen)
   6067 			listlen = listsize;
   6068 		rv = ddi_copyout(&scsi_keys->keylist, user_keys,
   6069 		    listlen * MHIOC_RESV_KEY_SIZE, mode);
   6070 		if (rv != 0)
   6071 			rv = EFAULT;
   6072 	}
   6073 
   6074 	if (rv == 0)
   6075 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
   6076 
   6077 done:
   6078 	kmem_free(vd_scsi, vd_scsi_len);
   6079 
   6080 	return (rv);
   6081 }
   6082 
   6083 /*
   6084  * Implement the MHIOCGRP_INRESV mhd(7i) ioctl. The ioctl is converted
   6085  * to a SCSI PERSISTENT IN READ RESERVATION command which is sent to
   6086  * the vdisk server with a VD_OP_SCSICMD operation.
   6087  */
   6088 static int
   6089 vdc_mhd_inresv(vdc_t *vdc, caddr_t arg, int mode)
   6090 {
   6091 	vd_scsi_t *vd_scsi;
   6092 	mhioc_inresvs_t inresv;
   6093 	mhioc_resv_desc_list_t rlist;
   6094 	struct mhioc_inresvs32 inresv32;
   6095 	struct mhioc_resv_desc_list32 rlist32;
   6096 	mhioc_resv_desc_t mhd_resv;
   6097 	sd_prin_readresv_t *scsi_resv;
   6098 	sd_readresv_desc_t *resv;
   6099 	mhioc_resv_desc_t *user_resv;
   6100 	int vd_scsi_len;
   6101 	int listsize, listlen, i, rv;
   6102 
   6103 	/* copyin arguments */
   6104 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
   6105 		rv = ddi_copyin(arg, &inresv32, sizeof (inresv32), mode);
   6106 		if (rv != 0)
   6107 			return (EFAULT);
   6108 
   6109 		rv = ddi_copyin((caddr_t)(uintptr_t)inresv32.li, &rlist32,
   6110 		    sizeof (rlist32), mode);
   6111 		if (rv != 0)
   6112 			return (EFAULT);
   6113 
   6114 		listsize = rlist32.listsize;
   6115 	} else {
   6116 		rv = ddi_copyin(arg, &inresv, sizeof (inresv), mode);
   6117 		if (rv != 0)
   6118 			return (EFAULT);
   6119 
   6120 		rv = ddi_copyin(inresv.li, &rlist, sizeof (rlist), mode);
   6121 		if (rv != 0)
   6122 			return (EFAULT);
   6123 
   6124 		listsize = rlist.listsize;
   6125 	}
   6126 
   6127 	/* build SCSI VD_OP request */
   6128 	vd_scsi = vdc_scsi_alloc_persistent_in(SD_READ_RESV,
   6129 	    sizeof (sd_prin_readresv_t) - sizeof (caddr_t) +
   6130 	    (SCSI3_RESV_DESC_LEN * listsize), &vd_scsi_len);
   6131 
   6132 	scsi_resv = (sd_prin_readresv_t *)VD_SCSI_DATA_IN(vd_scsi);
   6133 
   6134 	/* submit the request */
   6135 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
   6136 	    0, 0, VIO_both_dir, B_FALSE);
   6137 
   6138 	if (rv != 0)
   6139 		goto done;
   6140 
   6141 	listlen = scsi_resv->len / SCSI3_RESV_DESC_LEN;
   6142 
   6143 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
   6144 		inresv32.generation = scsi_resv->generation;
   6145 		rv = ddi_copyout(&inresv32, arg, sizeof (inresv32), mode);
   6146 		if (rv != 0) {
   6147 			rv = EFAULT;
   6148 			goto done;
   6149 		}
   6150 
   6151 		rlist32.listlen = listlen;
   6152 		rv = ddi_copyout(&rlist32, (caddr_t)(uintptr_t)inresv32.li,
   6153 		    sizeof (rlist32), mode);
   6154 		if (rv != 0) {
   6155 			rv = EFAULT;
   6156 			goto done;
   6157 		}
   6158 
   6159 		user_resv = (mhioc_resv_desc_t *)(uintptr_t)rlist32.list;
   6160 	} else {
   6161 		inresv.generation = scsi_resv->generation;
   6162 		rv = ddi_copyout(&inresv, arg, sizeof (inresv), mode);
   6163 		if (rv != 0) {
   6164 			rv = EFAULT;
   6165 			goto done;
   6166 		}
   6167 
   6168 		rlist.listlen = listlen;
   6169 		rv = ddi_copyout(&rlist, inresv.li, sizeof (rlist), mode);
   6170 		if (rv != 0) {
   6171 			rv = EFAULT;
   6172 			goto done;
   6173 		}
   6174 
   6175 		user_resv = rlist.list;
   6176 	}
   6177 
   6178 	/* copy out reservations */
   6179 	if (listsize > 0 && listlen > 0) {
   6180 		if (listsize < listlen)
   6181 			listlen = listsize;
   6182 		resv = (sd_readresv_desc_t *)&scsi_resv->readresv_desc;
   6183 
   6184 		for (i = 0; i < listlen; i++) {
   6185 			mhd_resv.type = resv->type;
   6186 			mhd_resv.scope = resv->scope;
   6187 			mhd_resv.scope_specific_addr =
   6188 			    BE_32(resv->scope_specific_addr);
   6189 			bcopy(&resv->resvkey, &mhd_resv.key,
   6190 			    MHIOC_RESV_KEY_SIZE);
   6191 
   6192 			rv = ddi_copyout(&mhd_resv, user_resv,
   6193 			    sizeof (mhd_resv), mode);
   6194 			if (rv != 0) {
   6195 				rv = EFAULT;
   6196 				goto done;
   6197 			}
   6198 			resv++;
   6199 			user_resv++;
   6200 		}
   6201 	}
   6202 
   6203 	if (rv == 0)
   6204 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
   6205 
   6206 done:
   6207 	kmem_free(vd_scsi, vd_scsi_len);
   6208 	return (rv);
   6209 }
   6210 
   6211 /*
   6212  * Implement the MHIOCGRP_REGISTER mhd(7i) ioctl. The ioctl is converted
   6213  * to a SCSI PERSISTENT OUT REGISTER command which is sent to the vdisk
   6214  * server with a VD_OP_SCSICMD operation.
   6215  */
   6216 static int
   6217 vdc_mhd_register(vdc_t *vdc, caddr_t arg, int mode)
   6218 {
   6219 	vd_scsi_t *vd_scsi;
   6220 	sd_prout_t *scsi_prout;
   6221 	mhioc_register_t mhd_reg;
   6222 	int vd_scsi_len, rv;
   6223 
   6224 	/* copyin arguments */
   6225 	rv = ddi_copyin(arg, &mhd_reg, sizeof (mhd_reg), mode);
   6226 	if (rv != 0)
   6227 		return (EFAULT);
   6228 
   6229 	/* build SCSI VD_OP request */
   6230 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_REGISTER,
   6231 	    sizeof (sd_prout_t), &vd_scsi_len);
   6232 
   6233 	/* set parameters */
   6234 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
   6235 	bcopy(mhd_reg.oldkey.key, scsi_prout->res_key, MHIOC_RESV_KEY_SIZE);
   6236 	bcopy(mhd_reg.newkey.key, scsi_prout->service_key, MHIOC_RESV_KEY_SIZE);
   6237 	scsi_prout->aptpl = (uchar_t)mhd_reg.aptpl;
   6238 
   6239 	/* submit the request */
   6240 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
   6241 	    0, 0, VIO_both_dir, B_FALSE);
   6242 
   6243 	if (rv == 0)
   6244 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
   6245 
   6246 	kmem_free(vd_scsi, vd_scsi_len);
   6247 	return (rv);
   6248 }
   6249 
   6250 /*
   6251  * Implement the MHIOCGRP_RESERVE mhd(7i) ioctl. The ioctl is converted
   6252  * to a SCSI PERSISTENT OUT RESERVE command which is sent to the vdisk
   6253  * server with a VD_OP_SCSICMD operation.
   6254  */
   6255 static int
   6256 vdc_mhd_reserve(vdc_t *vdc, caddr_t arg, int mode)
   6257 {
   6258 	union scsi_cdb *cdb;
   6259 	vd_scsi_t *vd_scsi;
   6260 	sd_prout_t *scsi_prout;
   6261 	mhioc_resv_desc_t mhd_resv;
   6262 	int vd_scsi_len, rv;
   6263 
   6264 	/* copyin arguments */
   6265 	rv = ddi_copyin(arg, &mhd_resv, sizeof (mhd_resv), mode);
   6266 	if (rv != 0)
   6267 		return (EFAULT);
   6268 
   6269 	/* build SCSI VD_OP request */
   6270 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_RESERVE,
   6271 	    sizeof (sd_prout_t), &vd_scsi_len);
   6272 
   6273 	/* set parameters */
   6274 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
   6275 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
   6276 	bcopy(mhd_resv.key.key, scsi_prout->res_key, MHIOC_RESV_KEY_SIZE);
   6277 	scsi_prout->scope_address = mhd_resv.scope_specific_addr;
   6278 	cdb->cdb_opaque[2] = mhd_resv.type;
   6279 
   6280 	/* submit the request */
   6281 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
   6282 	    0, 0, VIO_both_dir, B_FALSE);
   6283 
   6284 	if (rv == 0)
   6285 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
   6286 
   6287 	kmem_free(vd_scsi, vd_scsi_len);
   6288 	return (rv);
   6289 }
   6290 
   6291 /*
   6292  * Implement the MHIOCGRP_PREEMPTANDABORT mhd(7i) ioctl. The ioctl is
   6293  * converted to a SCSI PERSISTENT OUT PREEMPT AND ABORT command which
   6294  * is sent to the vdisk server with a VD_OP_SCSICMD operation.
   6295  */
   6296 static int
   6297 vdc_mhd_preemptabort(vdc_t *vdc, caddr_t arg, int mode)
   6298 {
   6299 	union scsi_cdb *cdb;
   6300 	vd_scsi_t *vd_scsi;
   6301 	sd_prout_t *scsi_prout;
   6302 	mhioc_preemptandabort_t mhd_preempt;
   6303 	int vd_scsi_len, rv;
   6304 
   6305 	/* copyin arguments */
   6306 	rv = ddi_copyin(arg, &mhd_preempt, sizeof (mhd_preempt), mode);
   6307 	if (rv != 0)
   6308 		return (EFAULT);
   6309 
   6310 	/* build SCSI VD_OP request */
   6311 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_PREEMPTANDABORT,
   6312 	    sizeof (sd_prout_t), &vd_scsi_len);
   6313 
   6314 	/* set parameters */
   6315 	vd_scsi->task_attribute = VD_SCSI_TASK_ACA;
   6316 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
   6317 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
   6318 	bcopy(mhd_preempt.resvdesc.key.key, scsi_prout->res_key,
   6319 	    MHIOC_RESV_KEY_SIZE);
   6320 	bcopy(mhd_preempt.victim_key.key, scsi_prout->service_key,
   6321 	    MHIOC_RESV_KEY_SIZE);
   6322 	scsi_prout->scope_address = mhd_preempt.resvdesc.scope_specific_addr;
   6323 	cdb->cdb_opaque[2] = mhd_preempt.resvdesc.type;
   6324 
   6325 	/* submit the request */
   6326 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
   6327 	    0, 0, VIO_both_dir, B_FALSE);
   6328 
   6329 	if (rv == 0)
   6330 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
   6331 
   6332 	kmem_free(vd_scsi, vd_scsi_len);
   6333 	return (rv);
   6334 }
   6335 
   6336 /*
   6337  * Implement the MHIOCGRP_REGISTERANDIGNOREKEY mhd(7i) ioctl. The ioctl
   6338  * is converted to a SCSI PERSISTENT OUT REGISTER AND IGNORE EXISTING KEY
   6339  * command which is sent to the vdisk server with a VD_OP_SCSICMD operation.
   6340  */
   6341 static int
   6342 vdc_mhd_registerignore(vdc_t *vdc, caddr_t arg, int mode)
   6343 {
   6344 	vd_scsi_t *vd_scsi;
   6345 	sd_prout_t *scsi_prout;
   6346 	mhioc_registerandignorekey_t mhd_regi;
   6347 	int vd_scsi_len, rv;
   6348 
   6349 	/* copyin arguments */
   6350 	rv = ddi_copyin(arg, &mhd_regi, sizeof (mhd_regi), mode);
   6351 	if (rv != 0)
   6352 		return (EFAULT);
   6353 
   6354 	/* build SCSI VD_OP request */
   6355 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_REGISTERANDIGNOREKEY,
   6356 	    sizeof (sd_prout_t), &vd_scsi_len);
   6357 
   6358 	/* set parameters */
   6359 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
   6360 	bcopy(mhd_regi.newkey.key, scsi_prout->service_key,
   6361 	    MHIOC_RESV_KEY_SIZE);
   6362 	scsi_prout->aptpl = (uchar_t)mhd_regi.aptpl;
   6363 
   6364 	/* submit the request */
   6365 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
   6366 	    0, 0, VIO_both_dir, B_FALSE);
   6367 
   6368 	if (rv == 0)
   6369 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
   6370 
   6371 	kmem_free(vd_scsi, vd_scsi_len);
   6372 	return (rv);
   6373 }
   6374 
   6375 /*
   6376  * This function is used to send a (simple) SCSI command and check errors.
   6377  */
   6378 static int
   6379 vdc_eio_scsi_cmd(vdc_t *vdc, uchar_t scmd, int flags)
   6380 {
   6381 	int cdb_len, sense_len, vd_scsi_len;
   6382 	vd_scsi_t *vd_scsi;
   6383 	union scsi_cdb *cdb;
   6384 	int rv;
   6385 
   6386 	ASSERT(scmd == SCMD_TEST_UNIT_READY || scmd == SCMD_WRITE_G1);
   6387 
   6388 	if (scmd == SCMD_WRITE_G1)
   6389 		cdb_len = CDB_GROUP1;
   6390 	else
   6391 		cdb_len = CDB_GROUP0;
   6392 
   6393 	sense_len = sizeof (struct scsi_extended_sense);
   6394 
   6395 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, 0, 0, &vd_scsi_len);
   6396 
   6397 	/* set cdb */
   6398 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
   6399 	cdb->scc_cmd = scmd;
   6400 
   6401 	vd_scsi->timeout = vdc_scsi_timeout;
   6402 
   6403 	/*
   6404 	 * Submit the request. Note the operation should not request that any
   6405 	 * error is checked because this function is precisely called when
   6406 	 * checking errors.
   6407 	 */
   6408 	ASSERT((flags & VDC_OP_ERRCHK) == 0);
   6409 
   6410 	rv = vdc_do_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
   6411 	    0, 0, NULL, VIO_both_dir, flags);
   6412 
   6413 	if (rv == 0)
   6414 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
   6415 
   6416 	kmem_free(vd_scsi, vd_scsi_len);
   6417 	return (rv);
   6418 }
   6419 
   6420 /*
   6421  * This function is used to check if a SCSI backend is accessible. It will
   6422  * also detect reservation conflict if failfast is enabled, and panic the
   6423  * system in that case.
   6424  *
   6425  * Returned Code:
   6426  *	0	- disk is accessible
   6427  *	!= 0	- disk is inaccessible or unable to check if disk is accessible
   6428  */
   6429 static int
   6430 vdc_eio_scsi_check(vdc_t *vdc, int flags)
   6431 {
   6432 	int failure = 0;
   6433 	int rv;
   6434 
   6435 	/*
   6436 	 * Send a TEST UNIT READY command. The command will panic
   6437 	 * the system if it fails with a reservation conflict and
   6438 	 * failfast is enabled. If there is a reservation conflict
   6439 	 * and failfast is not enabled then the function will return
   6440 	 * EACCES. In that case, there's no problem with accessing
   6441 	 * the backend, it is just reserved.
   6442 	 */
   6443 	rv = vdc_eio_scsi_cmd(vdc, SCMD_TEST_UNIT_READY, flags);
   6444 	if (rv != 0 && rv != EACCES)
   6445 		failure++;
   6446 
   6447 	/* we don't need to do more checking if failfast is not enabled */
   6448 	if (vdc->failfast_interval == 0)
   6449 		return (failure);
   6450 
   6451 	/*
   6452 	 * With SPC-3 compliant devices TEST UNIT READY will succeed on
   6453 	 * a reserved device, so we also do a WRITE(10) of zero byte in
   6454 	 * order to provoke a Reservation Conflict status on those newer
   6455 	 * devices.
   6456 	 */
   6457 	if (vdc_eio_scsi_cmd(vdc, SCMD_WRITE_G1, flags) != 0)
   6458 		failure++;
   6459 
   6460 	return (failure);
   6461 }
   6462 
   6463 /*
   6464  * This function is used to check if a backend is effectively accessible.
   6465  *
   6466  * Returned Code:
   6467  *	0	- disk is accessible
   6468  *	!= 0	- disk is inaccessible or unable to check if disk is accessible
   6469  */
   6470 static int
   6471 vdc_eio_check(vdc_t *vdc, int flags)
   6472 {
   6473 	char *buffer;
   6474 	diskaddr_t blkno;
   6475 	int rv;
   6476 
   6477 	ASSERT((flags & VDC_OP_ERRCHK) == 0);
   6478 
   6479 	if (VD_OP_SUPPORTED(vdc->operations, VD_OP_SCSICMD))
   6480 		return (vdc_eio_scsi_check(vdc, flags));
   6481 
   6482 	ASSERT(vdc->failfast_interval == 0);
   6483 
   6484 	/*
   6485 	 * If the backend does not support SCSI operations then we simply
   6486 	 * check if the backend is accessible by reading some data blocks.
   6487 	 * We first try to read a random block, to try to avoid getting
   6488 	 * a block that might have been cached on the service domain. Then
   6489 	 * we try the last block, and finally the first block.
   6490 	 *
   6491 	 * We return success as soon as we are able to read any block.
   6492 	 */
   6493 	buffer = kmem_alloc(vdc->vdisk_bsize, KM_SLEEP);
   6494 
   6495 	if (vdc->vdisk_size > 0) {
   6496 
   6497 		/* try a random block */
   6498 		(void) random_get_pseudo_bytes((uint8_t *)&blkno,
   6499 		    sizeof (diskaddr_t));
   6500 		blkno = blkno % vdc->vdisk_size;
   6501 		rv = vdc_do_op(vdc, VD_OP_BREAD, (caddr_t)buffer,
   6502 		    vdc->vdisk_bsize, VD_SLICE_NONE, blkno, NULL,
   6503 		    VIO_read_dir, flags);
   6504 
   6505 		if (rv == 0)
   6506 			goto done;
   6507 
   6508 		/* try the last block */
   6509 		blkno = vdc->vdisk_size - 1;
   6510 		rv = vdc_do_op(vdc, VD_OP_BREAD, (caddr_t)buffer,
   6511 		    vdc->vdisk_bsize, VD_SLICE_NONE, blkno, NULL,
   6512 		    VIO_read_dir, flags);
   6513 
   6514 		if (rv == 0)
   6515 			goto done;
   6516 	}
   6517 
   6518 	/* try block 0 */
   6519 	blkno = 0;
   6520 	rv = vdc_do_op(vdc, VD_OP_BREAD, (caddr_t)buffer, vdc->vdisk_bsize,
   6521 	    VD_SLICE_NONE, blkno, NULL, VIO_read_dir, flags);
   6522 
   6523 done:
   6524 	kmem_free(buffer, vdc->vdisk_bsize);
   6525 	return (rv);
   6526 }
   6527 
   6528 /*
   6529  * Add a pending I/O to the eio queue. An I/O is added to this queue
   6530  * when it has failed and failfast is enabled or the vdisk has multiple
   6531  * servers. It will then be handled by the eio thread (vdc_eio_thread).
   6532  * The eio queue is ordered starting with the most recent I/O added.
   6533  */
   6534 static vdc_io_t *
   6535 vdc_eio_queue(vdc_t *vdc, int index)
   6536 {
   6537 	vdc_io_t *vio;
   6538 
   6539 	ASSERT(MUTEX_HELD(&vdc->lock));
   6540 
   6541 	vio = kmem_alloc(sizeof (vdc_io_t), KM_SLEEP);
   6542 	vio->vio_next = vdc->eio_queue;
   6543 	vio->vio_index = index;
   6544 	vio->vio_qtime = ddi_get_lbolt();
   6545 
   6546 	vdc->eio_queue = vio;
   6547 
   6548 	/* notify the eio thread that a new I/O is queued */
   6549 	cv_signal(&vdc->eio_cv);
   6550 
   6551 	return (vio);
   6552 }
   6553 
   6554 /*
   6555  * Remove I/Os added before the indicated deadline from the eio queue. A
   6556  * deadline of 0 means that all I/Os have to be unqueued. The complete_io
   6557  * boolean specifies if unqueued I/Os should be marked as completed or not.
   6558  */
   6559 static void
   6560 vdc_eio_unqueue(vdc_t *vdc, clock_t deadline, boolean_t complete_io)
   6561 {
   6562 	struct buf *buf;
   6563 	vdc_io_t *vio, *vio_tmp;
   6564 	int index, op;
   6565 
   6566 	ASSERT(MUTEX_HELD(&vdc->lock));
   6567 
   6568 	vio_tmp = NULL;
   6569 	vio = vdc->eio_queue;
   6570 
   6571 	if (deadline != 0) {
   6572 		/*
   6573 		 * Skip any io queued after the deadline. The eio queue is
   6574 		 * ordered starting with the last I/O added to the queue.
   6575 		 */
   6576 		while (vio != NULL && vio->vio_qtime > deadline) {
   6577 			vio_tmp = vio;
   6578 			vio = vio->vio_next;
   6579 		}
   6580 	}
   6581 
   6582 	if (vio == NULL)
   6583 		/* nothing to unqueue */
   6584 		return;
   6585 
   6586 	/* update the queue */
   6587 	if (vio_tmp == NULL)
   6588 		vdc->eio_queue = NULL;
   6589 	else
   6590 		vio_tmp->vio_next = NULL;
   6591 
   6592 	/*
   6593 	 * Free and complete unqueued I/Os if this was requested. All I/Os
   6594 	 * have a block I/O data transfer structure (buf) and they are
   6595 	 * completed by calling biodone().
   6596 	 */
   6597 	while (vio != NULL) {
   6598 		vio_tmp = vio->vio_next;
   6599 
   6600 		if (complete_io) {
   6601 			index = vio->vio_index;
   6602 			op = vdc->local_dring[index].operation;
   6603 			buf = vdc->local_dring[index].buf;
   6604 			(void) vdc_depopulate_descriptor(vdc, index);
   6605 			ASSERT(buf->b_flags & B_ERROR);
   6606 			if (op == VD_OP_BREAD || op == VD_OP_BWRITE) {
   6607 				VD_UPDATE_ERR_STATS(vdc, vd_softerrs);
   6608 				VD_KSTAT_RUNQ_EXIT(vdc);
   6609 				DTRACE_IO1(done, buf_t *, buf);
   6610 			}
   6611 			biodone(buf);
   6612 		}
   6613 
   6614 		kmem_free(vio, sizeof (vdc_io_t));
   6615 		vio = vio_tmp;
   6616 	}
   6617 }
   6618 
   6619 /*
   6620  * Error I/O Thread.  There is one eio thread for each virtual disk that
   6621  * has multiple servers or for which failfast is enabled. Failfast can only
   6622  * be enabled for vdisk supporting SCSI commands.
   6623  *
   6624  * While failfast is enabled, the eio thread sends a TEST UNIT READY
   6625  * and a zero size WRITE(10) SCSI commands on a regular basis to check that
   6626  * we still have access to the disk. If a command fails with a RESERVATION
   6627  * CONFLICT error then the system will immediatly panic.
   6628  *
   6629  * The eio thread is also woken up when an I/O has failed. It then checks
   6630  * the access to the disk to ensure that the I/O failure was not due to a
   6631  * reservation conflict or to the backend been inaccessible.
   6632  *
   6633  */
   6634 static void
   6635 vdc_eio_thread(void *arg)
   6636 {
   6637 	int status;
   6638 	vdc_t *vdc = (vdc_t *)arg;
   6639 	clock_t starttime, timeout = drv_usectohz(vdc->failfast_interval);
   6640 
   6641 	mutex_enter(&vdc->lock);
   6642 
   6643 	while (vdc->failfast_interval != 0 || vdc->num_servers > 1) {
   6644 		/*
   6645 		 * Wait if there is nothing in the eio queue or if the state
   6646 		 * is not VDC_STATE_RUNNING.
   6647 		 */
   6648 		if (vdc->eio_queue == NULL || vdc->state != VDC_STATE_RUNNING) {
   6649 			if (vdc->failfast_interval != 0) {
   6650 				timeout = ddi_get_lbolt() +
   6651 				    drv_usectohz(vdc->failfast_interval);
   6652 				(void) cv_timedwait(&vdc->eio_cv, &vdc->lock,
   6653 				    timeout);
   6654 			} else {
   6655 				ASSERT(vdc->num_servers > 1);
   6656 				(void) cv_wait(&vdc->eio_cv, &vdc->lock);
   6657 			}
   6658 
   6659 			if (vdc->state != VDC_STATE_RUNNING)
   6660 				continue;
   6661 		}
   6662 
   6663 		mutex_exit(&vdc->lock);
   6664 
   6665 		starttime = ddi_get_lbolt();
   6666 
   6667 		/* check error */
   6668 		status = vdc_eio_check(vdc, VDC_OP_STATE_RUNNING);
   6669 
   6670 		mutex_enter(&vdc->lock);
   6671 		/*
   6672 		 * We have dropped the lock to check the backend so we have
   6673 		 * to check that the eio thread is still enabled.
   6674 		 */
   6675 		if (vdc->failfast_interval == 0 && vdc->num_servers <= 1)
   6676 			break;
   6677 
   6678 		/*
   6679 		 * If the eio queue is empty or we are not in running state
   6680 		 * anymore then there is nothing to do.
   6681 		 */
   6682 		if (vdc->state != VDC_STATE_RUNNING || vdc->eio_queue == NULL)
   6683 			continue;
   6684 
   6685 		if (status == 0) {
   6686 			/*
   6687 			 * The backend access has been successfully checked,
   6688 			 * we can complete any I/O queued before the last check.
   6689 			 */
   6690 			vdc_eio_unqueue(vdc, starttime, B_TRUE);
   6691 
   6692 		} else if (vdc->num_servers > 1) {
   6693 			/*
   6694 			 * The backend is inaccessible for a disk with multiple
   6695 			 * servers. So we force a reset to switch to another
   6696 			 * server. The reset will also clear the eio queue and
   6697 			 * resubmit all pending I/Os.
   6698 			 */
   6699 			mutex_enter(&vdc->read_lock);
   6700 			vdc->read_state = VDC_READ_RESET;
   6701 			cv_signal(&vdc->read_cv);
   6702 			mutex_exit(&vdc->read_lock);
   6703 		}
   6704 	}
   6705 
   6706 	/*
   6707 	 * The thread is being stopped so we can complete any queued I/O.
   6708 	 */
   6709 	vdc_eio_unqueue(vdc, 0, B_TRUE);
   6710 	vdc->eio_thread = NULL;
   6711 	mutex_exit(&vdc->lock);
   6712 	thread_exit();
   6713 }
   6714 
   6715 /*
   6716  * Implement the MHIOCENFAILFAST mhd(7i) ioctl.
   6717  */
   6718 static int
   6719 vdc_failfast(vdc_t *vdc, caddr_t arg, int mode)
   6720 {
   6721 	unsigned int mh_time;
   6722 
   6723 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), mode))
   6724 		return (EFAULT);
   6725 
   6726 	mutex_enter(&vdc->lock);
   6727 	if (mh_time != 0 && vdc->eio_thread == NULL) {
   6728 		vdc->eio_thread = thread_create(NULL, 0,
   6729 		    vdc_eio_thread, vdc, 0, &p0, TS_RUN,
   6730 		    v.v_maxsyspri - 2);
   6731 	}
   6732 
   6733 	vdc->failfast_interval = ((long)mh_time) * MILLISEC;
   6734 	cv_signal(&vdc->eio_cv);
   6735 	mutex_exit(&vdc->lock);
   6736 
   6737 	return (0);
   6738 }
   6739 
   6740 /*
   6741  * Implement the MHIOCTKOWN and MHIOCRELEASE mhd(7i) ioctls. These ioctls are
   6742  * converted to VD_OP_SET_ACCESS operations.
   6743  */
   6744 static int
   6745 vdc_access_set(vdc_t *vdc, uint64_t flags)
   6746 {
   6747 	int rv;
   6748 
   6749 	/* submit owership command request */
   6750 	rv = vdc_do_sync_op(vdc, VD_OP_SET_ACCESS, (caddr_t)&flags,
   6751 	    sizeof (uint64_t), 0, 0, VIO_both_dir, B_TRUE);
   6752 
   6753 	return (rv);
   6754 }
   6755 
   6756 /*
   6757  * Implement the MHIOCSTATUS mhd(7i) ioctl. This ioctl is converted to a
   6758  * VD_OP_GET_ACCESS operation.
   6759  */
   6760 static int
   6761 vdc_access_get(vdc_t *vdc, uint64_t *status)
   6762 {
   6763 	int rv;
   6764 
   6765 	/* submit owership command request */
   6766 	rv = vdc_do_sync_op(vdc, VD_OP_GET_ACCESS, (caddr_t)status,
   6767 	    sizeof (uint64_t), 0, 0, VIO_both_dir, B_TRUE);
   6768 
   6769 	return (rv);
   6770 }
   6771 
   6772 /*
   6773  * Disk Ownership Thread.
   6774  *
   6775  * When we have taken the ownership of a disk, this thread waits to be
   6776  * notified when the LDC channel is reset so that it can recover the
   6777  * ownership.
   6778  *
   6779  * Note that the thread handling the LDC reset (vdc_process_msg_thread())
   6780  * can not be used to do the ownership recovery because it has to be
   6781  * running to handle the reply message to the ownership operation.
   6782  */
   6783 static void
   6784 vdc_ownership_thread(void *arg)
   6785 {
   6786 	vdc_t *vdc = (vdc_t *)arg;
   6787 	clock_t timeout;
   6788 	uint64_t status;
   6789 
   6790 	mutex_enter(&vdc->ownership_lock);
   6791 	mutex_enter(&vdc->lock);
   6792 
   6793 	while (vdc->ownership & VDC_OWNERSHIP_WANTED) {
   6794 
   6795 		if ((vdc->ownership & VDC_OWNERSHIP_RESET) ||
   6796 		    !(vdc->ownership & VDC_OWNERSHIP_GRANTED)) {
   6797 			/*
   6798 			 * There was a reset so the ownership has been lost,
   6799 			 * try to recover. We do this without using the preempt
   6800 			 * option so that we don't steal the ownership from
   6801 			 * someone who has preempted us.
   6802 			 */
   6803 			DMSG(vdc, 0, "[%d] Ownership lost, recovering",
   6804 			    vdc->instance);
   6805 
   6806 			vdc->ownership &= ~(VDC_OWNERSHIP_RESET |
   6807 			    VDC_OWNERSHIP_GRANTED);
   6808 
   6809 			mutex_exit(&vdc->lock);
   6810 
   6811 			status = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE |
   6812 			    VD_ACCESS_SET_PRESERVE);
   6813 
   6814 			mutex_enter(&vdc->lock);
   6815 
   6816 			if (status == 0) {
   6817 				DMSG(vdc, 0, "[%d] Ownership recovered",
   6818 				    vdc->instance);
   6819 				vdc->ownership |= VDC_OWNERSHIP_GRANTED;
   6820 			} else {
   6821 				DMSG(vdc, 0, "[%d] Fail to recover ownership",
   6822 				    vdc->instance);
   6823 			}
   6824 
   6825 		}
   6826 
   6827 		/*
   6828 		 * If we have the ownership then we just wait for an event
   6829 		 * to happen (LDC reset), otherwise we will retry to recover
   6830 		 * after a delay.
   6831 		 */
   6832 		if (vdc->ownership & VDC_OWNERSHIP_GRANTED)
   6833 			timeout = 0;
   6834 		else
   6835 			timeout = drv_usectohz(vdc_ownership_delay);
   6836 
   6837 		/* Release the ownership_lock and wait on the vdc lock */
   6838 		mutex_exit(&vdc->ownership_lock);
   6839 
   6840 		if (timeout == 0)
   6841 			(void) cv_wait(&vdc->ownership_cv, &vdc->lock);
   6842 		else
   6843 			(void) cv_reltimedwait(&vdc->ownership_cv, &vdc->lock,
   6844 			    timeout, TR_CLOCK_TICK);
   6845 
   6846 		mutex_exit(&vdc->lock);
   6847 
   6848 		mutex_enter(&vdc->ownership_lock);
   6849 		mutex_enter(&vdc->lock);
   6850 	}
   6851 
   6852 	vdc->ownership_thread = NULL;
   6853 	mutex_exit(&vdc->lock);
   6854 	mutex_exit(&vdc->ownership_lock);
   6855 
   6856 	thread_exit();
   6857 }
   6858 
   6859 static void
   6860 vdc_ownership_update(vdc_t *vdc, int ownership_flags)
   6861 {
   6862 	ASSERT(MUTEX_HELD(&vdc->ownership_lock));
   6863 
   6864 	mutex_enter(&vdc->lock);
   6865 	vdc->ownership = ownership_flags;
   6866 	if ((vdc->ownership & VDC_OWNERSHIP_WANTED) &&
   6867 	    vdc->ownership_thread == NULL) {
   6868 		/* start ownership thread */
   6869 		vdc->ownership_thread = thread_create(NULL, 0,
   6870 		    vdc_ownership_thread, vdc, 0, &p0, TS_RUN,
   6871 		    v.v_maxsyspri - 2);
   6872 	} else {
   6873 		/* notify the ownership thread */
   6874 		cv_signal(&vdc->ownership_cv);
   6875 	}
   6876 	mutex_exit(&vdc->lock);
   6877 }
   6878 
   6879 /*
   6880  * Get the size and the block size of a virtual disk from the vdisk server.
   6881  */
   6882 static int
   6883 vdc_get_capacity(vdc_t *vdc, size_t *dsk_size, size_t *blk_size)
   6884 {
   6885 	int rv = 0;
   6886 	size_t alloc_len;
   6887 	vd_capacity_t *vd_cap;
   6888 
   6889 	ASSERT(MUTEX_NOT_HELD(&vdc->lock));
   6890 
   6891 	alloc_len = P2ROUNDUP(sizeof (vd_capacity_t), sizeof (uint64_t));
   6892 
   6893 	vd_cap = kmem_zalloc(alloc_len, KM_SLEEP);
   6894 
   6895 	rv = vdc_do_sync_op(vdc, VD_OP_GET_CAPACITY, (caddr_t)vd_cap, alloc_len,
   6896 	    0, 0, VIO_both_dir, B_TRUE);
   6897 
   6898 	*dsk_size = vd_cap->vdisk_size;
   6899 	*blk_size = vd_cap->vdisk_block_size;
   6900 
   6901 	kmem_free(vd_cap, alloc_len);
   6902 	return (rv);
   6903 }
   6904 
   6905 /*
   6906  * Check the disk capacity. Disk size information is updated if size has
   6907  * changed.
   6908  *
   6909  * Return 0 if the disk capacity is available, or non-zero if it is not.
   6910  */
   6911 static int
   6912 vdc_check_capacity(vdc_t *vdc)
   6913 {
   6914 	size_t dsk_size, blk_size;
   6915 	int rv;
   6916 
   6917 	/*
   6918 	 * If the vdisk does not support the VD_OP_GET_CAPACITY operation
   6919 	 * then the disk capacity has been retrieved during the handshake
   6920 	 * and there's nothing more to do here.
   6921 	 */
   6922 	if (!VD_OP_SUPPORTED(vdc->operations, VD_OP_GET_CAPACITY))
   6923 		return (0);
   6924 
   6925 	if ((rv = vdc_get_capacity(vdc, &dsk_size, &blk_size)) != 0)
   6926 		return (rv);
   6927 
   6928 	if (dsk_size == VD_SIZE_UNKNOWN || dsk_size == 0 || blk_size == 0)
   6929 		return (EINVAL);
   6930 
   6931 	mutex_enter(&vdc->lock);
   6932 	/*
   6933 	 * First try to update the VIO block size (which is the same as the
   6934 	 * vdisk block size). If this returns an error then that means that
   6935 	 * we can not use that block size so basically the vdisk is unusable
   6936 	 * and we return an error.
   6937 	 */
   6938 	rv = vdc_update_vio_bsize(vdc, blk_size);
   6939 	if (rv == 0)
   6940 		vdc_update_size(vdc, dsk_size, blk_size, vdc->max_xfer_sz);
   6941 
   6942 	mutex_exit(&vdc->lock);
   6943 
   6944 	return (rv);
   6945 }
   6946 
   6947 /*
   6948  * This structure is used in the DKIO(7I) array below.
   6949  */
   6950 typedef struct vdc_dk_ioctl {
   6951 	uint8_t		op;		/* VD_OP_XXX value */
   6952 	int		cmd;		/* Solaris ioctl operation number */
   6953 	size_t		nbytes;		/* size of structure to be copied */
   6954 
   6955 	/* function to convert between vDisk and Solaris structure formats */
   6956 	int	(*convert)(vdc_t *vdc, void *vd_buf, void *ioctl_arg,
   6957 	    int mode, int dir);
   6958 } vdc_dk_ioctl_t;
   6959 
   6960 /*
   6961  * Subset of DKIO(7I) operations currently supported
   6962  */
   6963 static vdc_dk_ioctl_t	dk_ioctl[] = {
   6964 	{VD_OP_FLUSH,		DKIOCFLUSHWRITECACHE,	0,
   6965 		vdc_null_copy_func},
   6966 	{VD_OP_GET_WCE,		DKIOCGETWCE,		sizeof (int),
   6967 		vdc_get_wce_convert},
   6968 	{VD_OP_SET_WCE,		DKIOCSETWCE,		sizeof (int),
   6969 		vdc_set_wce_convert},
   6970 	{VD_OP_GET_VTOC,	DKIOCGVTOC,		sizeof (vd_vtoc_t),
   6971 		vdc_get_vtoc_convert},
   6972 	{VD_OP_SET_VTOC,	DKIOCSVTOC,		sizeof (vd_vtoc_t),
   6973 		vdc_set_vtoc_convert},
   6974 	{VD_OP_GET_VTOC,	DKIOCGEXTVTOC,		sizeof (vd_vtoc_t),
   6975 		vdc_get_extvtoc_convert},
   6976 	{VD_OP_SET_VTOC,	DKIOCSEXTVTOC,		sizeof (vd_vtoc_t),
   6977 		vdc_set_extvtoc_convert},
   6978 	{VD_OP_GET_DISKGEOM,	DKIOCGGEOM,		sizeof (vd_geom_t),
   6979 		vdc_get_geom_convert},
   6980 	{VD_OP_GET_DISKGEOM,	DKIOCG_PHYGEOM,		sizeof (vd_geom_t),
   6981 		vdc_get_geom_convert},
   6982 	{VD_OP_GET_DISKGEOM, 	DKIOCG_VIRTGEOM,	sizeof (vd_geom_t),
   6983 		vdc_get_geom_convert},
   6984 	{VD_OP_SET_DISKGEOM,	DKIOCSGEOM,		sizeof (vd_geom_t),
   6985 		vdc_set_geom_convert},
   6986 	{VD_OP_GET_EFI,		DKIOCGETEFI,		0,
   6987 		vdc_get_efi_convert},
   6988 	{VD_OP_SET_EFI,		DKIOCSETEFI,		0,
   6989 		vdc_set_efi_convert},
   6990 
   6991 	/* DIOCTL_RWCMD is converted to a read or a write */
   6992 	{0, DIOCTL_RWCMD,  sizeof (struct dadkio_rwcmd), NULL},
   6993 
   6994 	/* mhd(7I) non-shared multihost disks ioctls */
   6995 	{0, MHIOCTKOWN,				0, vdc_null_copy_func},
   6996 	{0, MHIOCRELEASE,			0, vdc_null_copy_func},
   6997 	{0, MHIOCSTATUS,			0, vdc_null_copy_func},
   6998 	{0, MHIOCQRESERVE,			0, vdc_null_copy_func},
   6999 
   7000 	/* mhd(7I) shared multihost disks ioctls */
   7001 	{0, MHIOCGRP_INKEYS,			0, vdc_null_copy_func},
   7002 	{0, MHIOCGRP_INRESV,			0, vdc_null_copy_func},
   7003 	{0, MHIOCGRP_REGISTER,			0, vdc_null_copy_func},
   7004 	{0, MHIOCGRP_RESERVE, 			0, vdc_null_copy_func},
   7005 	{0, MHIOCGRP_PREEMPTANDABORT,		0, vdc_null_copy_func},
   7006 	{0, MHIOCGRP_REGISTERANDIGNOREKEY,	0, vdc_null_copy_func},
   7007 
   7008 	/* mhd(7I) failfast ioctl */
   7009 	{0, MHIOCENFAILFAST,			0, vdc_null_copy_func},
   7010 
   7011 	/*
   7012 	 * These particular ioctls are not sent to the server - vdc fakes up
   7013 	 * the necessary info.
   7014 	 */
   7015 	{0, DKIOCINFO, sizeof (struct dk_cinfo), vdc_null_copy_func},
   7016 	{0, DKIOCGMEDIAINFO, sizeof (struct dk_minfo), vdc_null_copy_func},
   7017 	{0, USCSICMD,	sizeof (struct uscsi_cmd), vdc_null_copy_func},
   7018 	{0, DKIOCPARTITION, 0, vdc_null_copy_func },
   7019 	{0, DKIOCGAPART, 0, vdc_null_copy_func },
   7020 	{0, DKIOCREMOVABLE, 0, vdc_null_copy_func},
   7021 	{0, CDROMREADOFFSET, 0, vdc_null_copy_func}
   7022 };
   7023 
   7024 /*
   7025  * This function handles ioctl requests from the vd_efi_alloc_and_read()
   7026  * function and forward them to the vdisk.
   7027  */
   7028 static int
   7029 vd_process_efi_ioctl(void *vdisk, int cmd, uintptr_t arg)
   7030 {
   7031 	vdc_t *vdc = (vdc_t *)vdisk;
   7032 	dev_t dev;
   7033 	int rval;
   7034 
   7035 	dev = makedevice(ddi_driver_major(vdc->dip),
   7036 	    VD_MAKE_DEV(vdc->instance, 0));
   7037 
   7038 	return (vd_process_ioctl(dev, cmd, (caddr_t)arg, FKIOCTL, &rval));
   7039 }
   7040 
   7041 /*
   7042  * Function:
   7043  *	vd_process_ioctl()
   7044  *
   7045  * Description:
   7046  *	This routine processes disk specific ioctl calls
   7047  *
   7048  * Arguments:
   7049  *	dev	- the device number
   7050  *	cmd	- the operation [dkio(7I)] to be processed
   7051  *	arg	- pointer to user provided structure
   7052  *		  (contains data to be set or reference parameter for get)
   7053  *	mode	- bit flag, indicating open settings, 32/64 bit type, etc
   7054  *	rvalp	- pointer to return value for calling process.
   7055  *
   7056  * Return Code:
   7057  *	0
   7058  *	EFAULT
   7059  *	ENXIO
   7060  *	EIO
   7061  *	ENOTSUP
   7062  */
   7063 static int
   7064 vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode, int *rvalp)
   7065 {
   7066 	int		instance = VDCUNIT(dev);
   7067 	vdc_t		*vdc = NULL;
   7068 	int		rv = -1;
   7069 	int		idx = 0;		/* index into dk_ioctl[] */
   7070 	size_t		len = 0;		/* #bytes to send to vds */
   7071 	size_t		alloc_len = 0;		/* #bytes to allocate mem for */
   7072 	caddr_t		mem_p = NULL;
   7073 	size_t		nioctls = (sizeof (dk_ioctl)) / (sizeof (dk_ioctl[0]));
   7074 	vdc_dk_ioctl_t	*iop;
   7075 
   7076 	vdc = ddi_get_soft_state(vdc_state, instance);
   7077 	if (vdc == NULL) {
   7078 		cmn_err(CE_NOTE, "![%d] Could not get soft state structure",
   7079 		    instance);
   7080 		return (ENXIO);
   7081 	}
   7082 
   7083 	DMSG(vdc, 0, "[%d] Processing ioctl(%x) for dev %lx : model %x\n",
   7084 	    instance, cmd, dev, ddi_model_convert_from(mode & FMODELS));
   7085 
   7086 	if (rvalp != NULL) {
   7087 		/* the return value of the ioctl is 0 by default */
   7088 		*rvalp = 0;
   7089 	}
   7090 
   7091 	/*
   7092 	 * Validate the ioctl operation to be performed.
   7093 	 *
   7094 	 * If we have looped through the array without finding a match then we
   7095 	 * don't support this ioctl.
   7096 	 */
   7097 	for (idx = 0; idx < nioctls; idx++) {
   7098 		if (cmd == dk_ioctl[idx].cmd)
   7099 			break;
   7100 	}
   7101 
   7102 	if (idx >= nioctls) {
   7103 		DMSG(vdc, 0, "[%d] Unsupported ioctl (0x%x)\n",
   7104 		    vdc->instance, cmd);
   7105 		return (ENOTSUP);
   7106 	}
   7107 
   7108 	iop = &(dk_ioctl[idx]);
   7109 
   7110 	if (cmd == DKIOCGETEFI || cmd == DKIOCSETEFI) {
   7111 		/* size is not fixed for EFI ioctls, it depends on ioctl arg */
   7112 		dk_efi_t	dk_efi;
   7113 
   7114 		rv = ddi_copyin(arg, &dk_efi, sizeof (dk_efi_t), mode);
   7115 		if (rv != 0)
   7116 			return (EFAULT);
   7117 
   7118 		len = sizeof (vd_efi_t) - 1 + dk_efi.dki_length;
   7119 	} else {
   7120 		len = iop->nbytes;
   7121 	}
   7122 
   7123 	/* check if the ioctl is applicable */
   7124 	switch (cmd) {
   7125 	case CDROMREADOFFSET:
   7126 	case DKIOCREMOVABLE:
   7127 		return (ENOTTY);
   7128 
   7129 	case USCSICMD:
   7130 	case MHIOCTKOWN:
   7131 	case MHIOCSTATUS:
   7132 	case MHIOCQRESERVE:
   7133 	case MHIOCRELEASE:
   7134 	case MHIOCGRP_INKEYS:
   7135 	case MHIOCGRP_INRESV:
   7136 	case MHIOCGRP_REGISTER:
   7137 	case MHIOCGRP_RESERVE:
   7138 	case MHIOCGRP_PREEMPTANDABORT:
   7139 	case MHIOCGRP_REGISTERANDIGNOREKEY:
   7140 	case MHIOCENFAILFAST:
   7141 		if (vdc->cinfo == NULL)
   7142 			return (ENXIO);
   7143 		if (vdc->cinfo->dki_ctype != DKC_SCSI_CCS)
   7144 			return (ENOTTY);
   7145 		break;
   7146 
   7147 	case DIOCTL_RWCMD:
   7148 		if (vdc->cinfo == NULL)
   7149 			return (ENXIO);
   7150 		if (vdc->cinfo->dki_ctype != DKC_DIRECT)
   7151 			return (ENOTTY);
   7152 		break;
   7153 
   7154 	case DKIOCINFO:
   7155 		if (vdc->cinfo == NULL)
   7156 			return (ENXIO);
   7157 		break;
   7158 
   7159 	case DKIOCGMEDIAINFO:
   7160 		if (vdc->minfo == NULL)
   7161 			return (ENXIO);
   7162 		if (vdc_check_capacity(vdc) != 0)
   7163 			/* disk capacity is not available */
   7164 			return (EIO);
   7165 		break;
   7166 	}
   7167 
   7168 	/*
   7169 	 * Deal with ioctls which require a processing different than
   7170 	 * converting ioctl arguments and sending a corresponding
   7171 	 * VD operation.
   7172 	 */
   7173 	switch (cmd) {
   7174 
   7175 	case USCSICMD:
   7176 	{
   7177 		return (vdc_uscsi_cmd(vdc, arg, mode));
   7178 	}
   7179 
   7180 	case MHIOCTKOWN:
   7181 	{
   7182 		mutex_enter(&vdc->ownership_lock);
   7183 		/*
   7184 		 * We have to set VDC_OWNERSHIP_WANTED now so that the ownership
   7185 		 * can be flagged with VDC_OWNERSHIP_RESET if the LDC is reset
   7186 		 * while we are processing the ioctl.
   7187 		 */
   7188 		vdc_ownership_update(vdc, VDC_OWNERSHIP_WANTED);
   7189 
   7190 		rv = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE |
   7191 		    VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE);
   7192 		if (rv == 0) {
   7193 			vdc_ownership_update(vdc, VDC_OWNERSHIP_WANTED |
   7194 			    VDC_OWNERSHIP_GRANTED);
   7195 		} else {
   7196 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
   7197 		}
   7198 		mutex_exit(&vdc->ownership_lock);
   7199 		return (rv);
   7200 	}
   7201 
   7202 	case MHIOCRELEASE:
   7203 	{
   7204 		mutex_enter(&vdc->ownership_lock);
   7205 		rv = vdc_access_set(vdc, VD_ACCESS_SET_CLEAR);
   7206 		if (rv == 0) {
   7207 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
   7208 		}
   7209 		mutex_exit(&vdc->ownership_lock);
   7210 		return (rv);
   7211 	}
   7212 
   7213 	case MHIOCSTATUS:
   7214 	{
   7215 		uint64_t status;
   7216 
   7217 		rv = vdc_access_get(vdc, &status);
   7218 		if (rv == 0 && rvalp != NULL)
   7219 			*rvalp = (status & VD_ACCESS_ALLOWED)? 0 : 1;
   7220 		return (rv);
   7221 	}
   7222 
   7223 	case MHIOCQRESERVE:
   7224 	{
   7225 		rv = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE);
   7226 		return (rv);
   7227 	}
   7228 
   7229 	case MHIOCGRP_INKEYS:
   7230 	{
   7231 		return (vdc_mhd_inkeys(vdc, arg, mode));
   7232 	}
   7233 
   7234 	case MHIOCGRP_INRESV:
   7235 	{
   7236 		return (vdc_mhd_inresv(vdc, arg, mode));
   7237 	}
   7238 
   7239 	case MHIOCGRP_REGISTER:
   7240 	{
   7241 		return (vdc_mhd_register(vdc, arg, mode));
   7242 	}
   7243 
   7244 	case MHIOCGRP_RESERVE:
   7245 	{
   7246 		return (vdc_mhd_reserve(vdc, arg, mode));
   7247 	}
   7248 
   7249 	case MHIOCGRP_PREEMPTANDABORT:
   7250 	{
   7251 		return (vdc_mhd_preemptabort(vdc, arg, mode));
   7252 	}
   7253 
   7254 	case MHIOCGRP_REGISTERANDIGNOREKEY:
   7255 	{
   7256 		return (vdc_mhd_registerignore(vdc, arg, mode));
   7257 	}
   7258 
   7259 	case MHIOCENFAILFAST:
   7260 	{
   7261 		rv = vdc_failfast(vdc, arg, mode);
   7262 		return (rv);
   7263 	}
   7264 
   7265 	case DIOCTL_RWCMD:
   7266 	{
   7267 		return (vdc_dioctl_rwcmd(vdc, arg, mode));
   7268 	}
   7269 
   7270 	case DKIOCGAPART:
   7271 	{
   7272 		return (vdc_dkio_gapart(vdc, arg, mode));
   7273 	}
   7274 
   7275 	case DKIOCPARTITION:
   7276 	{
   7277 		return (vdc_dkio_partition(vdc, arg, mode));
   7278 	}
   7279 
   7280 	case DKIOCINFO:
   7281 	{
   7282 		struct dk_cinfo	cinfo;
   7283 
   7284 		bcopy(vdc->cinfo, &cinfo, sizeof (struct dk_cinfo));
   7285 		cinfo.dki_partition = VDCPART(dev);
   7286 
   7287 		rv = ddi_copyout(&cinfo, (void *)arg,
   7288 		    sizeof (struct dk_cinfo), mode);
   7289 		if (rv != 0)
   7290 			return (EFAULT);
   7291 
   7292 		return (0);
   7293 	}
   7294 
   7295 	case DKIOCGMEDIAINFO:
   7296 	{
   7297 		ASSERT(vdc->vdisk_size != 0);
   7298 		ASSERT(vdc->minfo->dki_capacity != 0);
   7299 		rv = ddi_copyout(vdc->minfo, (void *)arg,
   7300 		    sizeof (struct dk_minfo), mode);
   7301 		if (rv != 0)
   7302 			return (EFAULT);
   7303 
   7304 		return (0);
   7305 	}
   7306 
   7307 	case DKIOCFLUSHWRITECACHE:
   7308 		{
   7309 			struct dk_callback *dkc =
   7310 			    (struct dk_callback *)(uintptr_t)arg;
   7311 			vdc_dk_arg_t	*dkarg = NULL;
   7312 
   7313 			DMSG(vdc, 1, "[%d] Flush W$: mode %x\n",
   7314 			    instance, mode);
   7315 
   7316 			/*
   7317 			 * If arg is NULL, then there is no callback function
   7318 			 * registered and the call operates synchronously; we
   7319 			 * break and continue with the rest of the function and
   7320 			 * wait for vds to return (i.e. after the request to
   7321 			 * vds returns successfully, all writes completed prior
   7322 			 * to the ioctl will have been flushed from the disk
   7323 			 * write cache to persistent media.
   7324 			 *
   7325 			 * If a callback function is registered, we dispatch
   7326 			 * the request on a task queue and return immediately.
   7327 			 * The callback will deal with informing the calling
   7328 			 * thread that the flush request is completed.
   7329 			 */
   7330 			if (dkc == NULL)
   7331 				break;
   7332 
   7333 			/*
   7334 			 * the asynchronous callback is only supported if
   7335 			 * invoked from within the kernel
   7336 			 */
   7337 			if ((mode & FKIOCTL) == 0)
   7338 				return (ENOTSUP);
   7339 
   7340 			dkarg = kmem_zalloc(sizeof (vdc_dk_arg_t), KM_SLEEP);
   7341 
   7342 			dkarg->mode = mode;
   7343 			dkarg->dev = dev;
   7344 			bcopy(dkc, &dkarg->dkc, sizeof (*dkc));
   7345 
   7346 			mutex_enter(&vdc->lock);
   7347 			vdc->dkio_flush_pending++;
   7348 			dkarg->vdc = vdc;
   7349 			mutex_exit(&vdc->lock);
   7350 
   7351 			/* put the request on a task queue */
   7352 			rv = taskq_dispatch(system_taskq, vdc_dkio_flush_cb,
   7353 			    (void *)dkarg, DDI_SLEEP);
   7354 			if (rv == NULL) {
   7355 				/* clean up if dispatch fails */
   7356 				mutex_enter(&vdc->lock);
   7357 				vdc->dkio_flush_pending--;
   7358 				mutex_exit(&vdc->lock);
   7359 				kmem_free(dkarg, sizeof (vdc_dk_arg_t));
   7360 			}
   7361 
   7362 			return (rv == NULL ? ENOMEM : 0);
   7363 		}
   7364 	}
   7365 
   7366 	/* catch programming error in vdc - should be a VD_OP_XXX ioctl */
   7367 	ASSERT(iop->op != 0);
   7368 
   7369 	/* check if the vDisk server handles the operation for this vDisk */
   7370 	if (VD_OP_SUPPORTED(vdc->operations, iop->op) == B_FALSE) {
   7371 		DMSG(vdc, 0, "[%d] Unsupported VD_OP operation (0x%x)\n",
   7372 		    vdc->instance, iop->op);
   7373 		return (ENOTSUP);
   7374 	}
   7375 
   7376 	/* LDC requires that the memory being mapped is 8-byte aligned */
   7377 	alloc_len = P2ROUNDUP(len, sizeof (uint64_t));
   7378 	DMSG(vdc, 1, "[%d] struct size %ld alloc %ld\n",
   7379 	    instance, len, alloc_len);
   7380 
   7381 	if (alloc_len > 0)
   7382 		mem_p = kmem_zalloc(alloc_len, KM_SLEEP);
   7383 
   7384 	/*
   7385 	 * Call the conversion function for this ioctl which, if necessary,
   7386 	 * converts from the Solaris format to the format ARC'ed
   7387 	 * as part of the vDisk protocol (FWARC 2006/195)
   7388 	 */
   7389 	ASSERT(iop->convert != NULL);
   7390 	rv = (iop->convert)(vdc, arg, mem_p, mode, VD_COPYIN);
   7391 	if (rv != 0) {
   7392 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
   7393 		    instance, rv, cmd);
   7394 		if (mem_p != NULL)
   7395 			kmem_free(mem_p, alloc_len);
   7396 		return (rv);
   7397 	}
   7398 
   7399 	/*
   7400 	 * send request to vds to service the ioctl.
   7401 	 */
   7402 	rv = vdc_do_sync_op(vdc, iop->op, mem_p, alloc_len,
   7403 	    VDCPART(dev), 0, VIO_both_dir, B_TRUE);
   7404 
   7405 	if (rv != 0) {
   7406 		/*
   7407 		 * This is not necessarily an error. The ioctl could
   7408 		 * be returning a value such as ENOTTY to indicate
   7409 		 * that the ioctl is not applicable.
   7410 		 */
   7411 		DMSG(vdc, 0, "[%d] vds returned %d for ioctl 0x%x\n",
   7412 		    instance, rv, cmd);
   7413 		if (mem_p != NULL)
   7414 			kmem_free(mem_p, alloc_len);
   7415 
   7416 		return (rv);
   7417 	}
   7418 
   7419 	/*
   7420 	 * Call the conversion function (if it exists) for this ioctl
   7421 	 * which converts from the format ARC'ed as part of the vDisk
   7422 	 * protocol (FWARC 2006/195) back to a format understood by
   7423 	 * the rest of Solaris.
   7424 	 */
   7425 	rv = (iop->convert)(vdc, mem_p, arg, mode, VD_COPYOUT);
   7426 	if (rv != 0) {
   7427 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
   7428 		    instance, rv, cmd);
   7429 		if (mem_p != NULL)
   7430 			kmem_free(mem_p, alloc_len);
   7431 		return (rv);
   7432 	}
   7433 
   7434 	if (mem_p != NULL)
   7435 		kmem_free(mem_p, alloc_len);
   7436 
   7437 	return (rv);
   7438 }
   7439 
   7440 /*
   7441  * Function:
   7442  *
   7443  * Description:
   7444  *	This is an empty conversion function used by ioctl calls which
   7445  *	do not need to convert the data being passed in/out to userland
   7446  */
   7447 static int
   7448 vdc_null_copy_func(vdc_t *vdc, void *from, void *to, int mode, int dir)
   7449 {
   7450 	_NOTE(ARGUNUSED(vdc))
   7451 	_NOTE(ARGUNUSED(from))
   7452 	_NOTE(ARGUNUSED(to))
   7453 	_NOTE(ARGUNUSED(mode))
   7454 	_NOTE(ARGUNUSED(dir))
   7455 
   7456 	return (0);
   7457 }
   7458 
   7459 static int
   7460 vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
   7461     int mode, int dir)
   7462 {
   7463 	_NOTE(ARGUNUSED(vdc))
   7464 
   7465 	if (dir == VD_COPYIN)
   7466 		return (0);		/* nothing to do */
   7467 
   7468 	if (ddi_copyout(from, to, sizeof (int), mode) != 0)
   7469 		return (EFAULT);
   7470 
   7471 	return (0);
   7472 }
   7473 
   7474 static int
   7475 vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
   7476     int mode, int dir)
   7477 {
   7478 	_NOTE(ARGUNUSED(vdc))
   7479 
   7480 	if (dir == VD_COPYOUT)
   7481 		return (0);		/* nothing to do */
   7482 
   7483 	if (ddi_copyin(from, to, sizeof (int), mode) != 0)
   7484 		return (EFAULT);
   7485 
   7486 	return (0);
   7487 }
   7488 
   7489 /*
   7490  * Function:
   7491  *	vdc_get_vtoc_convert()
   7492  *
   7493  * Description:
   7494  *	This routine performs the necessary convertions from the DKIOCGVTOC
   7495  *	Solaris structure to the format defined in FWARC 2006/195.
   7496  *
   7497  *	In the struct vtoc definition, the timestamp field is marked as not
   7498  *	supported so it is not part of vDisk protocol (FWARC 2006/195).
   7499  *	However SVM uses that field to check it can write into the VTOC,
   7500  *	so we fake up the info of that field.
   7501  *
   7502  * Arguments:
   7503  *	vdc	- the vDisk client
   7504  *	from	- the buffer containing the data to be copied from
   7505  *	to	- the buffer to be copied to
   7506  *	mode	- flags passed to ioctl() call
   7507  *	dir	- the "direction" of the copy - VD_COPYIN or VD_COPYOUT
   7508  *
   7509  * Return Code:
   7510  *	0	- Success
   7511  *	ENXIO	- incorrect buffer passed in.
   7512  *	EFAULT	- ddi_copyout routine encountered an error.
   7513  */
   7514 static int
   7515 vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
   7516 {
   7517 	int		i;
   7518 	struct vtoc	vtoc;
   7519 	struct vtoc32	vtoc32;
   7520 	struct extvtoc	evtoc;
   7521 	int		rv;
   7522 
   7523 	if (dir != VD_COPYOUT)
   7524 		return (0);	/* nothing to do */
   7525 
   7526 	if ((from == NULL) || (to == NULL))
   7527 		return (ENXIO);
   7528 
   7529 	if (vdc->vdisk_size > VD_OLDVTOC_LIMIT)
   7530 		return (EOVERFLOW);
   7531 
   7532 	VD_VTOC2VTOC((vd_vtoc_t *)from, &evtoc);
   7533 
   7534 	/* fake the VTOC timestamp field */
   7535 	for (i = 0; i < V_NUMPAR; i++) {
   7536 		evtoc.timestamp[i] = vdc->vtoc->timestamp[i];
   7537 	}
   7538 
   7539 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
   7540 		/* LINTED E_ASSIGN_NARROW_CONV */
   7541 		extvtoctovtoc32(evtoc, vtoc32);
   7542 		rv = ddi_copyout(&vtoc32, to, sizeof (vtoc32), mode);
   7543 		if (rv != 0)
   7544 			rv = EFAULT;
   7545 	} else {
   7546 		extvtoctovtoc(evtoc, vtoc);
   7547 		rv = ddi_copyout(&vtoc, to, sizeof (vtoc), mode);
   7548 		if (rv != 0)
   7549 			rv = EFAULT;
   7550 	}
   7551 
   7552 	return (rv);
   7553 }
   7554 
   7555 /*
   7556  * Function:
   7557  *	vdc_set_vtoc_convert()
   7558  *
   7559  * Description:
   7560  *	This routine performs the necessary convertions from the DKIOCSVTOC
   7561  *	Solaris structure to the format defined in FWARC 2006/195.
   7562  *
   7563  * Arguments:
   7564  *	vdc	- the vDisk client
   7565  *	from	- Buffer with data
   7566  *	to	- Buffer where data is to be copied to
   7567  *	mode	- flags passed to ioctl
   7568  *	dir	- direction of copy (in or out)
   7569  *
   7570  * Return Code:
   7571  *	0	- Success
   7572  *	ENXIO	- Invalid buffer passed in
   7573  *	EFAULT	- ddi_copyin of data failed
   7574  */
   7575 static int
   7576 vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
   7577 {
   7578 	void		*uvtoc;
   7579 	struct vtoc	vtoc;
   7580 	struct vtoc32	vtoc32;
   7581 	struct extvtoc	evtoc;
   7582 	int		i, rv;
   7583 
   7584 	if ((from == NULL) || (to == NULL))
   7585 		return (ENXIO);
   7586 
   7587 	if (vdc->vdisk_size > VD_OLDVTOC_LIMIT)
   7588 		return (EOVERFLOW);
   7589 
   7590 	uvtoc = (dir == VD_COPYIN)? from : to;
   7591 
   7592 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
   7593 		rv = ddi_copyin(uvtoc, &vtoc32, sizeof (vtoc32), mode);
   7594 		if (rv != 0)
   7595 			return (EFAULT);
   7596 		vtoc32toextvtoc(vtoc32, evtoc);
   7597 	} else {
   7598 		rv = ddi_copyin(uvtoc, &vtoc, sizeof (vtoc), mode);
   7599 		if (rv != 0)
   7600 			return (EFAULT);
   7601 		vtoctoextvtoc(vtoc, evtoc);
   7602 	}
   7603 
   7604 	if (dir == VD_COPYOUT) {
   7605 		/*
   7606 		 * The disk label may have changed. Revalidate the disk
   7607 		 * geometry. This will also update the device nodes.
   7608 		 */
   7609 		vdc_validate(vdc);
   7610 
   7611 		/*
   7612 		 * We also need to keep track of the timestamp fields.
   7613 		 */
   7614 		for (i = 0; i < V_NUMPAR; i++) {
   7615 			vdc->vtoc->timestamp[i] = evtoc.timestamp[i];
   7616 		}
   7617 
   7618 	} else {
   7619 		VTOC2VD_VTOC(&evtoc, (vd_vtoc_t *)to);
   7620 	}
   7621 
   7622 	return (0);
   7623 }
   7624 
   7625 static int
   7626 vdc_get_extvtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
   7627 {
   7628 	int		i, rv;
   7629 	struct extvtoc	evtoc;
   7630 
   7631 	if (dir != VD_COPYOUT)
   7632 		return (0);	/* nothing to do */
   7633 
   7634 	if ((from == NULL) || (to == NULL))
   7635 		return (ENXIO);
   7636 
   7637 	VD_VTOC2VTOC((vd_vtoc_t *)from, &evtoc);
   7638 
   7639 	/* fake the VTOC timestamp field */
   7640 	for (i = 0; i < V_NUMPAR; i++) {
   7641 		evtoc.timestamp[i] = vdc->vtoc->timestamp[i];
   7642 	}
   7643 
   7644 	rv = ddi_copyout(&evtoc, to, sizeof (struct extvtoc), mode);
   7645 	if (rv != 0)
   7646 		rv = EFAULT;
   7647 
   7648 	return (rv);
   7649 }
   7650 
   7651 static int
   7652 vdc_set_extvtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
   7653 {
   7654 	void		*uvtoc;
   7655 	struct extvtoc	evtoc;
   7656 	int		i, rv;
   7657 
   7658 	if ((from == NULL) || (to == NULL))
   7659 		return (ENXIO);
   7660 
   7661 	uvtoc = (dir == VD_COPYIN)? from : to;
   7662 
   7663 	rv = ddi_copyin(uvtoc, &evtoc, sizeof (struct extvtoc), mode);
   7664 	if (rv != 0)
   7665 		return (EFAULT);
   7666 
   7667 	if (dir == VD_COPYOUT) {
   7668 		/*
   7669 		 * The disk label may have changed. Revalidate the disk
   7670 		 * geometry. This will also update the device nodes.
   7671 		 */
   7672 		vdc_validate(vdc);
   7673 
   7674 		/*
   7675 		 * We also need to keep track of the timestamp fields.
   7676 		 */
   7677 		for (i = 0; i < V_NUMPAR; i++) {
   7678 			vdc->vtoc->timestamp[i] = evtoc.timestamp[i];
   7679 		}
   7680 
   7681 	} else {
   7682 		VTOC2VD_VTOC(&evtoc, (vd_vtoc_t *)to);
   7683 	}
   7684 
   7685 	return (0);
   7686 }
   7687 
   7688 /*
   7689  * Function:
   7690  *	vdc_get_geom_convert()
   7691  *
   7692  * Description:
   7693  *	This routine performs the necessary convertions from the DKIOCGGEOM,
   7694  *	DKIOCG_PHYSGEOM and DKIOG_VIRTGEOM Solaris structures to the format
   7695  *	defined in FWARC 2006/195
   7696  *
   7697  * Arguments:
   7698  *	vdc	- the vDisk client
   7699  *	from	- Buffer with data
   7700  *	to	- Buffer where data is to be copied to
   7701  *	mode	- flags passed to ioctl
   7702  *	dir	- direction of copy (in or out)
   7703  *
   7704  * Return Code:
   7705  *	0	- Success
   7706  *	ENXIO	- Invalid buffer passed in
   7707  *	EFAULT	- ddi_copyout of data failed
   7708  */
   7709 static int
   7710 vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
   7711 {
   7712 	_NOTE(ARGUNUSED(vdc))
   7713 
   7714 	struct dk_geom	geom;
   7715 	int	copy_len = sizeof (struct dk_geom);
   7716 	int	rv = 0;
   7717 
   7718 	if (dir != VD_COPYOUT)
   7719 		return (0);	/* nothing to do */
   7720 
   7721 	if ((from == NULL) || (to == NULL))
   7722 		return (ENXIO);
   7723 
   7724 	VD_GEOM2DK_GEOM((vd_geom_t *)from, &geom);
   7725 	rv = ddi_copyout(&geom, to, copy_len, mode);
   7726 	if (rv != 0)
   7727 		rv = EFAULT;
   7728 
   7729 	return (rv);
   7730 }
   7731 
   7732 /*
   7733  * Function:
   7734  *	vdc_set_geom_convert()
   7735  *
   7736  * Description:
   7737  *	This routine performs the necessary convertions from the DKIOCSGEOM
   7738  *	Solaris structure to the format defined in FWARC 2006/195.
   7739  *
   7740  * Arguments:
   7741  *	vdc	- the vDisk client
   7742  *	from	- Buffer with data
   7743  *	to	- Buffer where data is to be copied to
   7744  *	mode	- flags passed to ioctl
   7745  *	dir	- direction of copy (in or out)
   7746  *
   7747  * Return Code:
   7748  *	0	- Success
   7749  *	ENXIO	- Invalid buffer passed in
   7750  *	EFAULT	- ddi_copyin of data failed
   7751  */
   7752 static int
   7753 vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
   7754 {
   7755 	_NOTE(ARGUNUSED(vdc))
   7756 
   7757 	vd_geom_t	vdgeom;
   7758 	void		*tmp_mem = NULL;
   7759 	int		copy_len = sizeof (struct dk_geom);
   7760 	int		rv = 0;
   7761 
   7762 	if (dir != VD_COPYIN)
   7763 		return (0);	/* nothing to do */
   7764 
   7765 	if ((from == NULL) || (to == NULL))
   7766 		return (ENXIO);
   7767 
   7768 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
   7769 
   7770 	rv = ddi_copyin(from, tmp_mem, copy_len, mode);
   7771 	if (rv != 0) {
   7772 		kmem_free(tmp_mem, copy_len);
   7773 		return (EFAULT);
   7774 	}
   7775 	DK_GEOM2VD_GEOM((struct dk_geom *)tmp_mem, &vdgeom);
   7776 	bcopy(&vdgeom, to, sizeof (vdgeom));
   7777 	kmem_free(tmp_mem, copy_len);
   7778 
   7779 	return (0);
   7780 }
   7781 
   7782 static int
   7783 vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
   7784 {
   7785 	_NOTE(ARGUNUSED(vdc))
   7786 
   7787 	vd_efi_t	*vd_efi;
   7788 	dk_efi_t	dk_efi;
   7789 	int		rv = 0;
   7790 	void		*uaddr;
   7791 
   7792 	if ((from == NULL) || (to == NULL))
   7793 		return (ENXIO);
   7794 
   7795 	if (dir == VD_COPYIN) {
   7796 
   7797 		vd_efi = (vd_efi_t *)to;
   7798 
   7799 		rv = ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode);
   7800 		if (rv != 0)
   7801 			return (EFAULT);
   7802 
   7803 		vd_efi->lba = dk_efi.dki_lba;
   7804 		vd_efi->length = dk_efi.dki_length;
   7805 		bzero(vd_efi->data, vd_efi->length);
   7806 
   7807 	} else {
   7808 
   7809 		rv = ddi_copyin(to, &dk_efi, sizeof (dk_efi_t), mode);
   7810 		if (rv != 0)
   7811 			return (EFAULT);
   7812 
   7813 		uaddr = dk_efi.dki_data;
   7814 
   7815 		dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
   7816 
   7817 		VD_EFI2DK_EFI((vd_efi_t *)from, &dk_efi);
   7818 
   7819 		rv = ddi_copyout(dk_efi.dki_data, uaddr, dk_efi.dki_length,
   7820 		    mode);
   7821 		if (rv != 0)
   7822 			return (EFAULT);
   7823 
   7824 		kmem_free(dk_efi.dki_data, dk_efi.dki_length);
   7825 	}
   7826 
   7827 	return (0);
   7828 }
   7829 
   7830 static int
   7831 vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
   7832 {
   7833 	_NOTE(ARGUNUSED(vdc))
   7834 
   7835 	dk_efi_t	dk_efi;
   7836 	void		*uaddr;
   7837 
   7838 	if (dir == VD_COPYOUT) {
   7839 		/*
   7840 		 * The disk label may have changed. Revalidate the disk
   7841 		 * geometry. This will also update the device nodes.
   7842 		 */
   7843 		vdc_validate(vdc);
   7844 		return (0);
   7845 	}
   7846 
   7847 	if ((from == NULL) || (to == NULL))
   7848 		return (ENXIO);
   7849 
   7850 	if (ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode) != 0)
   7851 		return (EFAULT);
   7852 
   7853 	uaddr = dk_efi.dki_data;
   7854 
   7855 	dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
   7856 
   7857 	if (ddi_copyin(uaddr, dk_efi.dki_data, dk_efi.dki_length, mode) != 0)
   7858 		return (EFAULT);
   7859 
   7860 	DK_EFI2VD_EFI(&dk_efi, (vd_efi_t *)to);
   7861 
   7862 	kmem_free(dk_efi.dki_data, dk_efi.dki_length);
   7863 
   7864 	return (0);
   7865 }
   7866 
   7867 
   7868 /* -------------------------------------------------------------------------- */
   7869 
   7870 /*
   7871  * Function:
   7872  *	vdc_create_fake_geometry()
   7873  *
   7874  * Description:
   7875  *	This routine fakes up the disk info needed for some DKIO ioctls such
   7876  *	as DKIOCINFO and DKIOCGMEDIAINFO [just like lofi(7D) and ramdisk(7D) do]
   7877  *
   7878  *	Note: This function must not be called until the vDisk attributes have
   7879  *	been exchanged as part of the handshake with the vDisk server.
   7880  *
   7881  * Arguments:
   7882  *	vdc	- soft state pointer for this instance of the device driver.
   7883  *
   7884  * Return Code:
   7885  *	none.
   7886  */
   7887 static void
   7888 vdc_create_fake_geometry(vdc_t *vdc)
   7889 {
   7890 	ASSERT(vdc != NULL);
   7891 	ASSERT(vdc->max_xfer_sz != 0);
   7892 
   7893 	/*
   7894 	 * DKIOCINFO support
   7895 	 */
   7896 	if (vdc->cinfo == NULL)
   7897 		vdc->cinfo = kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
   7898 
   7899 	(void) strcpy(vdc->cinfo->dki_cname, VDC_DRIVER_NAME);
   7900 	(void) strcpy(vdc->cinfo->dki_dname, VDC_DRIVER_NAME);
   7901 	/* max_xfer_sz is #blocks so we don't need to divide by vdisk_bsize */
   7902 	vdc->cinfo->dki_maxtransfer = vdc->max_xfer_sz;
   7903 
   7904 	/*
   7905 	 * We set the controller type to DKC_SCSI_CCS only if the VD_OP_SCSICMD
   7906 	 * operation is supported, otherwise the controller type is DKC_DIRECT.
   7907 	 * Version 1.0 does not support the VD_OP_SCSICMD operation, so the
   7908 	 * controller type is always DKC_DIRECT in that case.
   7909 	 *
   7910 	 * If the virtual disk is backed by a physical CD/DVD device or
   7911 	 * an ISO image, modify the controller type to indicate this
   7912 	 */
   7913 	switch (vdc->vdisk_media) {
   7914 	case VD_MEDIA_CD:
   7915 	case VD_MEDIA_DVD:
   7916 		vdc->cinfo->dki_ctype = DKC_CDROM;
   7917 		break;
   7918 	case VD_MEDIA_FIXED:
   7919 		if (VD_OP_SUPPORTED(vdc->operations, VD_OP_SCSICMD))
   7920 			vdc->cinfo->dki_ctype = DKC_SCSI_CCS;
   7921 		else
   7922 			vdc->cinfo->dki_ctype = DKC_DIRECT;
   7923 		break;
   7924 	default:
   7925 		/* in the case of v1.0 we default to a fixed disk */
   7926 		vdc->cinfo->dki_ctype = DKC_DIRECT;
   7927 		break;
   7928 	}
   7929 	vdc->cinfo->dki_flags = DKI_FMTVOL;
   7930 	vdc->cinfo->dki_cnum = 0;
   7931 	vdc->cinfo->dki_addr = 0;
   7932 	vdc->cinfo->dki_space = 0;
   7933 	vdc->cinfo->dki_prio = 0;
   7934 	vdc->cinfo->dki_vec = 0;
   7935 	vdc->cinfo->dki_unit = vdc->instance;
   7936 	vdc->cinfo->dki_slave = 0;
   7937 	/*
   7938 	 * The partition number will be created on the fly depending on the
   7939 	 * actual slice (i.e. minor node) that is used to request the data.
   7940 	 */
   7941 	vdc->cinfo->dki_partition = 0;
   7942 
   7943 	/*
   7944 	 * DKIOCGMEDIAINFO support
   7945 	 */
   7946 	if (vdc->minfo == NULL)
   7947 		vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
   7948 
   7949 	if (vio_ver_is_supported(vdc->ver, 1, 1)) {
   7950 		vdc->minfo->dki_media_type =
   7951 		    VD_MEDIATYPE2DK_MEDIATYPE(vdc->vdisk_media);
   7952 	} else {
   7953 		vdc->minfo->dki_media_type = DK_FIXED_DISK;
   7954 	}
   7955 
   7956 	vdc->minfo->dki_capacity = vdc->vdisk_size;
   7957 	vdc->minfo->dki_lbsize = vdc->vdisk_bsize;
   7958 }
   7959 
   7960 static ushort_t
   7961 vdc_lbl2cksum(struct dk_label *label)
   7962 {
   7963 	int	count;
   7964 	ushort_t sum, *sp;
   7965 
   7966 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
   7967 	sp = (ushort_t *)label;
   7968 	sum = 0;
   7969 	while (count--) {
   7970 		sum ^= *sp++;
   7971 	}
   7972 
   7973 	return (sum);
   7974 }
   7975 
   7976 static void
   7977 vdc_update_size(vdc_t *vdc, size_t dsk_size, size_t blk_size, size_t xfr_size)
   7978 {
   7979 	vd_err_stats_t  *stp;
   7980 
   7981 	ASSERT(MUTEX_HELD(&vdc->lock));
   7982 	ASSERT(xfr_size != 0);
   7983 
   7984 	/*
   7985 	 * If the disk size is unknown or sizes are unchanged then don't
   7986 	 * update anything.
   7987 	 */
   7988 	if (dsk_size == VD_SIZE_UNKNOWN || dsk_size == 0 ||
   7989 	    (blk_size == vdc->vdisk_bsize && dsk_size == vdc->vdisk_size &&
   7990 	    xfr_size == vdc->max_xfer_sz))
   7991 		return;
   7992 
   7993 	/*
   7994 	 * We don't know at compile time what the vDisk server will think
   7995 	 * are good values but we apply a large (arbitrary) upper bound to
   7996 	 * prevent memory exhaustion in vdc if it was allocating a DRing
   7997 	 * based of huge values sent by the server. We probably will never
   7998 	 * exceed this except if the message was garbage.
   7999 	 */
   8000 	if ((xfr_size * blk_size) > (PAGESIZE * DEV_BSIZE)) {
   8001 		DMSG(vdc, 0, "[%d] vds block transfer size too big;"
   8002 		    " using max supported by vdc", vdc->instance);
   8003 		xfr_size = maxphys / blk_size;
   8004 	}
   8005 
   8006 	vdc->max_xfer_sz = xfr_size;
   8007 	vdc->vdisk_bsize = blk_size;
   8008 	vdc->vdisk_size = dsk_size;
   8009 
   8010 	stp = (vd_err_stats_t *)vdc->err_stats->ks_data;
   8011 	stp->vd_capacity.value.ui64 = dsk_size * blk_size;
   8012 
   8013 	vdc->minfo->dki_capacity = dsk_size;
   8014 	vdc->minfo->dki_lbsize = (uint_t)blk_size;
   8015 }
   8016 
   8017 /*
   8018  * Update information about the VIO block size. The VIO block size is the
   8019  * same as the vdisk block size which is stored in vdc->vdisk_bsize so we
   8020  * do not store that information again.
   8021  *
   8022  * However, buf structures will always use a logical block size of 512 bytes
   8023  * (DEV_BSIZE) and we will need to convert logical block numbers to VIO block
   8024  * numbers for each read or write operation using vdc_strategy(). To speed up
   8025  * this conversion, we expect the VIO block size to be a power of 2 and a
   8026  * multiple 512 bytes (DEV_BSIZE), and we cache some useful information.
   8027  *
   8028  * The function return EINVAL if the new VIO block size (blk_size) is not a
   8029  * power of 2 or not a multiple of 512 bytes, otherwise it returns 0.
   8030  */
   8031 static int
   8032 vdc_update_vio_bsize(vdc_t *vdc, uint32_t blk_size)
   8033 {
   8034 	uint32_t ratio, n;
   8035 	int nshift = 0;
   8036 
   8037 	vdc->vio_bmask = 0;
   8038 	vdc->vio_bshift = 0;
   8039 
   8040 	ASSERT(blk_size > 0);
   8041 
   8042 	if ((blk_size % DEV_BSIZE) != 0)
   8043 		return (EINVAL);
   8044 
   8045 	ratio = blk_size / DEV_BSIZE;
   8046 
   8047 	for (n = ratio; n > 1; n >>= 1) {
   8048 		if ((n & 0x1) != 0) {
   8049 			/* blk_size is not a power of 2 */
   8050 			return (EINVAL);
   8051 		}
   8052 		nshift++;
   8053 	}
   8054 
   8055 	vdc->vio_bshift = nshift;
   8056 	vdc->vio_bmask = ratio - 1;
   8057 
   8058 	return (0);
   8059 }
   8060 
   8061 /*
   8062  * Function:
   8063  *	vdc_validate_geometry
   8064  *
   8065  * Description:
   8066  *	This routine discovers the label and geometry of the disk. It stores
   8067  *	the disk label and related information in the vdc structure. If it
   8068  *	fails to validate the geometry or to discover the disk label then
   8069  *	the label is marked as unknown (VD_DISK_LABEL_UNK).
   8070  *
   8071  * Arguments:
   8072  *	vdc	- soft state pointer for this instance of the device driver.
   8073  *
   8074  * Return Code:
   8075  *	0	- success.
   8076  *	EINVAL	- unknown disk label.
   8077  *	ENOTSUP	- geometry not applicable (EFI label).
   8078  *	EIO	- error accessing the disk.
   8079  */
   8080 static int
   8081 vdc_validate_geometry(vdc_t *vdc)
   8082 {
   8083 	dev_t	dev;
   8084 	int	rv, rval;
   8085 	struct dk_label *label;
   8086 	struct dk_geom geom;
   8087 	struct extvtoc vtoc;
   8088 	efi_gpt_t *gpt;
   8089 	efi_gpe_t *gpe;
   8090 	vd_efi_dev_t edev;
   8091 
   8092 	ASSERT(vdc != NULL);
   8093 	ASSERT(vdc->vtoc != NULL && vdc->geom != NULL);
   8094 	ASSERT(MUTEX_HELD(&vdc->lock));
   8095 
   8096 	mutex_exit(&vdc->lock);
   8097 	/*
   8098 	 * Check the disk capacity in case it has changed. If that fails then
   8099 	 * we proceed and we will be using the disk size we currently have.
   8100 	 */
   8101 	(void) vdc_check_capacity(vdc);
   8102 	dev = makedevice(ddi_driver_major(vdc->dip),
   8103 	    VD_MAKE_DEV(vdc->instance, 0));
   8104 
   8105 	rv = vd_process_ioctl(dev, DKIOCGGEOM, (caddr_t)&geom, FKIOCTL, &rval);
   8106 	if (rv == 0)
   8107 		rv = vd_process_ioctl(dev, DKIOCGEXTVTOC, (caddr_t)&vtoc,
   8108 		    FKIOCTL, &rval);
   8109 
   8110 	if (rv == ENOTSUP) {
   8111 		/*
   8112 		 * If the device does not support VTOC then we try
   8113 		 * to read an EFI label.
   8114 		 *
   8115 		 * We need to know the block size and the disk size to
   8116 		 * be able to read an EFI label.
   8117 		 */
   8118 		if (vdc->vdisk_size == 0) {
   8119 			mutex_enter(&vdc->lock);
   8120 			vdc_store_label_unk(vdc);
   8121 			return (EIO);
   8122 		}
   8123 
   8124 		VDC_EFI_DEV_SET(edev, vdc, vd_process_efi_ioctl);
   8125 
   8126 		rv = vd_efi_alloc_and_read(&edev, &gpt, &gpe);
   8127 
   8128 		if (rv) {
   8129 			DMSG(vdc, 0, "[%d] Failed to get EFI (err=%d)",
   8130 			    vdc->instance, rv);
   8131 			mutex_enter(&vdc->lock);
   8132 			vdc_store_label_unk(vdc);
   8133 			return (EIO);
   8134 		}
   8135 
   8136 		mutex_enter(&vdc->lock);
   8137 		vdc_store_label_efi(vdc, gpt, gpe);
   8138 		vd_efi_free(&edev, gpt, gpe);
   8139 		return (ENOTSUP);
   8140 	}
   8141 
   8142 	if (rv != 0) {
   8143 		DMSG(vdc, 0, "[%d] Failed to get VTOC (err=%d)",
   8144 		    vdc->instance, rv);
   8145 		mutex_enter(&vdc->lock);
   8146 		vdc_store_label_unk(vdc);
   8147 		if (rv != EINVAL)
   8148 			rv = EIO;
   8149 		return (rv);
   8150 	}
   8151 
   8152 	/* check that geometry and vtoc are valid */
   8153 	if (geom.dkg_nhead == 0 || geom.dkg_nsect == 0 ||
   8154 	    vtoc.v_sanity != VTOC_SANE) {
   8155 		mutex_enter(&vdc->lock);
   8156 		vdc_store_label_unk(vdc);
   8157 		return (EINVAL);
   8158 	}
   8159 
   8160 	/*
   8161 	 * We have a disk and a valid VTOC. However this does not mean
   8162 	 * that the disk currently have a VTOC label. The returned VTOC may
   8163 	 * be a default VTOC to be used for configuring the disk (this is
   8164 	 * what is done for disk image). So we read the label from the
   8165 	 * beginning of the disk to ensure we really have a VTOC label.
   8166 	 *
   8167 	 * FUTURE: This could be the default way for reading the VTOC
   8168 	 * from the disk as opposed to sending the VD_OP_GET_VTOC
   8169 	 * to the server. This will be the default if vdc is implemented
   8170 	 * ontop of cmlb.
   8171 	 */
   8172 
   8173 	/*
   8174 	 * Single slice disk does not support read using an absolute disk
   8175 	 * offset so we just rely on the DKIOCGVTOC ioctl in that case.
   8176 	 */
   8177 	if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) {
   8178 		mutex_enter(&vdc->lock);
   8179 		if (vtoc.v_nparts != 1) {
   8180 			vdc_store_label_unk(vdc);
   8181 			return (EINVAL);
   8182 		}
   8183 		vdc_store_label_vtoc(vdc, &geom, &vtoc);
   8184 		return (0);
   8185 	}
   8186 
   8187 	if (vtoc.v_nparts != V_NUMPAR) {
   8188 		mutex_enter(&vdc->lock);
   8189 		vdc_store_label_unk(vdc);
   8190 		return (EINVAL);
   8191 	}
   8192 
   8193 	/*
   8194 	 * Most CD/DVDs do not have a disk label and the label is
   8195 	 * generated by the disk driver.  So the on-disk label check
   8196 	 * below may fail and we return now to avoid this problem.
   8197 	 */
   8198 	if (vdc->vdisk_media == VD_MEDIA_CD ||
   8199 	    vdc->vdisk_media == VD_MEDIA_DVD) {
   8200 		mutex_enter(&vdc->lock);
   8201 		vdc_store_label_vtoc(vdc, &geom, &vtoc);
   8202 		return (0);
   8203 	}
   8204 
   8205 	/*
   8206 	 * Read disk label from start of disk
   8207 	 */
   8208 	label = kmem_alloc(vdc->vdisk_bsize, KM_SLEEP);
   8209 
   8210 	rv = vdc_do_op(vdc, VD_OP_BREAD, (caddr_t)label, vdc->vdisk_bsize,
   8211 	    VD_SLICE_NONE, 0, NULL, VIO_read_dir, VDC_OP_NORMAL);
   8212 
   8213 	if (rv != 0 || label->dkl_magic != DKL_MAGIC ||
   8214 	    label->dkl_cksum != vdc_lbl2cksum(label)) {
   8215 		DMSG(vdc, 1, "[%d] Got VTOC with invalid label\n",
   8216 		    vdc->instance);
   8217 		kmem_free(label, vdc->vdisk_bsize);
   8218 		mutex_enter(&vdc->lock);
   8219 		vdc_store_label_unk(vdc);
   8220 		return (EINVAL);
   8221 	}
   8222 
   8223 	kmem_free(label, vdc->vdisk_bsize);
   8224 	mutex_enter(&vdc->lock);
   8225 	vdc_store_label_vtoc(vdc, &geom, &vtoc);
   8226 	return (0);
   8227 }
   8228 
   8229 /*
   8230  * Function:
   8231  *	vdc_validate
   8232  *
   8233  * Description:
   8234  *	This routine discovers the label of the disk and create the
   8235  *	appropriate device nodes if the label has changed.
   8236  *
   8237  * Arguments:
   8238  *	vdc	- soft state pointer for this instance of the device driver.
   8239  *
   8240  * Return Code:
   8241  *	none.
   8242  */
   8243 static void
   8244 vdc_validate(vdc_t *vdc)
   8245 {
   8246 	vd_disk_label_t old_label;
   8247 	vd_slice_t old_slice[V_NUMPAR];
   8248 	int rv;
   8249 
   8250 	ASSERT(!MUTEX_HELD(&vdc->lock));
   8251 
   8252 	mutex_enter(&vdc->lock);
   8253 
   8254 	/* save the current label and vtoc */
   8255 	old_label = vdc->vdisk_label;
   8256 	bcopy(vdc->slice, &old_slice, sizeof (vd_slice_t) * V_NUMPAR);
   8257 
   8258 	/* check the geometry */
   8259 	(void) vdc_validate_geometry(vdc);
   8260 
   8261 	/* if the disk label has changed, update device nodes */
   8262 	if (vdc->vdisk_type == VD_DISK_TYPE_DISK &&
   8263 	    vdc->vdisk_label != old_label) {
   8264 
   8265 		if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
   8266 			rv = vdc_create_device_nodes_efi(vdc);
   8267 		else
   8268 			rv = vdc_create_device_nodes_vtoc(vdc);
   8269 
   8270 		if (rv != 0) {
   8271 			DMSG(vdc, 0, "![%d] Failed to update device nodes",
   8272 			    vdc->instance);
   8273 		}
   8274 	}
   8275 
   8276 	mutex_exit(&vdc->lock);
   8277 }
   8278 
   8279 static void
   8280 vdc_validate_task(void *arg)
   8281 {
   8282 	vdc_t *vdc = (vdc_t *)arg;
   8283 
   8284 	vdc_validate(vdc);
   8285 
   8286 	mutex_enter(&vdc->lock);
   8287 	ASSERT(vdc->validate_pending > 0);
   8288 	vdc->validate_pending--;
   8289 	mutex_exit(&vdc->lock);
   8290 }