xref: /illumos-gate/usr/src/uts/common/io/scsi/adapters/scsi_vhci/scsi_vhci.c (revision 437220cd296f6d8b6654d6d52508b40b1e2d1ac7)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 #pragma ident	"%Z%%M%	%I%	%E% SMI"
26 
27 /*
28  * Multiplexed I/O SCSI vHCI implementation
29  */
30 
31 #include <sys/conf.h>
32 #include <sys/file.h>
33 #include <sys/ddi.h>
34 #include <sys/sunddi.h>
35 #include <sys/scsi/scsi.h>
36 #include <sys/scsi/impl/scsi_reset_notify.h>
37 #include <sys/sunmdi.h>
38 #include <sys/mdi_impldefs.h>
39 #include <sys/scsi/adapters/scsi_vhci.h>
40 #include <sys/disp.h>
41 #include <sys/byteorder.h>
42 
43 extern uintptr_t scsi_callback_id;
44 extern ddi_dma_attr_t scsi_alloc_attr;
45 
46 #ifdef	DEBUG
47 int	vhci_debug = VHCI_DEBUG_DEFAULT_VAL;
48 #endif
49 
50 /* retry for the vhci_do_prout command when a not ready is returned */
51 int vhci_prout_not_ready_retry = 180;
52 
53 /*
54  * These values are defined to support the internal retry of
55  * SCSI packets for better sense code handling.
56  */
57 #define	VHCI_CMD_CMPLT	0
58 #define	VHCI_CMD_RETRY	1
59 #define	VHCI_CMD_ERROR	-1
60 
61 #define	PROPFLAGS (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM)
62 #define	VHCI_SCSI_PERR		0x47
63 #define	VHCI_PGR_ILLEGALOP	-2
64 #define	VHCI_NUM_UPDATE_TASKQ	8
65 #define	VHCI_STD_INQ_SIZE	128
66 
67 /*
68  * Version Macros
69  */
70 #define	VHCI_NAME_VERSION	"SCSI VHCI Driver %I%"
71 char		vhci_version_name[] = VHCI_NAME_VERSION;
72 
73 int		vhci_first_time = 0;
74 clock_t		vhci_to_ticks = 0;
75 int		vhci_init_wait_timeout = VHCI_INIT_WAIT_TIMEOUT;
76 kcondvar_t	vhci_cv;
77 kmutex_t	vhci_global_mutex;
78 void		*vhci_softstate = NULL; /* for soft state */
79 
80 /*
81  * Flag to delay the retry of the reserve command
82  */
83 int		vhci_reserve_delay = 100000;
84 static int	vhci_path_quiesce_timeout = 60;
85 static uchar_t	zero_key[MHIOC_RESV_KEY_SIZE];
86 
87 /* uscsi delay for a TRAN_BUSY */
88 static int vhci_uscsi_delay = 100000;
89 static int vhci_uscsi_retry_count = 180;
90 /* uscsi_restart_sense timeout id in case it needs to get canceled */
91 static timeout_id_t vhci_restart_timeid = 0;
92 
93 /*
94  * Bidirectional map of 'target-port' to port id <pid> for support of
95  * iostat(1M) '-Xx' and '-Yx' output.
96  */
97 static kmutex_t		vhci_targetmap_mutex;
98 static uint_t		vhci_targetmap_pid = 1;
99 static mod_hash_t	*vhci_targetmap_bypid;	/* <pid> -> 'target-port' */
100 static mod_hash_t	*vhci_targetmap_byport;	/* 'target-port' -> <pid> */
101 
102 /*
103  * functions exported by scsi_vhci struct cb_ops
104  */
105 static int vhci_open(dev_t *, int, int, cred_t *);
106 static int vhci_close(dev_t, int, int, cred_t *);
107 static int vhci_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
108 
109 /*
110  * functions exported by scsi_vhci struct dev_ops
111  */
112 static int vhci_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
113 static int vhci_attach(dev_info_t *, ddi_attach_cmd_t);
114 static int vhci_detach(dev_info_t *, ddi_detach_cmd_t);
115 
116 /*
117  * functions exported by scsi_vhci scsi_hba_tran_t transport table
118  */
119 static int vhci_scsi_tgt_init(dev_info_t *, dev_info_t *,
120     scsi_hba_tran_t *, struct scsi_device *);
121 static void vhci_scsi_tgt_free(dev_info_t *, dev_info_t *, scsi_hba_tran_t *,
122     struct scsi_device *);
123 static int vhci_pgr_register_start(scsi_vhci_lun_t *, struct scsi_pkt *);
124 static int vhci_scsi_start(struct scsi_address *, struct scsi_pkt *);
125 static int vhci_scsi_abort(struct scsi_address *, struct scsi_pkt *);
126 static int vhci_scsi_reset(struct scsi_address *, int);
127 static int vhci_scsi_reset_target(struct scsi_address *, int level,
128     uint8_t select_path);
129 static int vhci_scsi_reset_bus(struct scsi_address *);
130 static int vhci_scsi_getcap(struct scsi_address *, char *, int);
131 static int vhci_scsi_setcap(struct scsi_address *, char *, int, int);
132 static int vhci_commoncap(struct scsi_address *, char *, int, int, int);
133 static int vhci_pHCI_cap(struct scsi_address *ap, char *cap, int val, int whom,
134     mdi_pathinfo_t *pip);
135 static struct scsi_pkt *vhci_scsi_init_pkt(struct scsi_address *,
136     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(), caddr_t);
137 static void vhci_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
138 static void vhci_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
139 static void vhci_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
140 static int vhci_scsi_reset_notify(struct scsi_address *, int, void (*)(caddr_t),
141     caddr_t);
142 static int vhci_scsi_get_bus_addr(struct scsi_device *, char *, int);
143 static int vhci_scsi_get_name(struct scsi_device *, char *, int);
144 static int vhci_scsi_bus_power(dev_info_t *, void *, pm_bus_power_op_t,
145     void *, void *);
146 static int vhci_scsi_bus_config(dev_info_t *, uint_t, ddi_bus_config_op_t,
147     void *, dev_info_t **);
148 
149 /*
150  * functions registered with the mpxio framework via mdi_vhci_ops_t
151  */
152 static int vhci_pathinfo_init(dev_info_t *, mdi_pathinfo_t *, int);
153 static int vhci_pathinfo_uninit(dev_info_t *, mdi_pathinfo_t *, int);
154 static int vhci_pathinfo_state_change(dev_info_t *, mdi_pathinfo_t *,
155     mdi_pathinfo_state_t, uint32_t, int);
156 static int vhci_pathinfo_online(dev_info_t *, mdi_pathinfo_t *, int);
157 static int vhci_pathinfo_offline(dev_info_t *, mdi_pathinfo_t *, int);
158 static int vhci_failover(dev_info_t *, dev_info_t *, int);
159 static void vhci_client_attached(dev_info_t *);
160 
161 static int vhci_ctl(dev_t, int, intptr_t, int, cred_t *, int *);
162 static int vhci_devctl(dev_t, int, intptr_t, int, cred_t *, int *);
163 static int vhci_ioc_get_phci_path(sv_iocdata_t *, caddr_t, int, caddr_t);
164 static int vhci_ioc_get_client_path(sv_iocdata_t *, caddr_t, int, caddr_t);
165 static int vhci_ioc_get_paddr(sv_iocdata_t *, caddr_t, int, caddr_t);
166 static int vhci_ioc_send_client_path(caddr_t, sv_iocdata_t *, int, caddr_t);
167 static void vhci_ioc_devi_to_path(dev_info_t *, caddr_t);
168 static int vhci_get_phci_path_list(dev_info_t *, sv_path_info_t *, uint_t);
169 static int vhci_get_client_path_list(dev_info_t *, sv_path_info_t *, uint_t);
170 static int vhci_get_iocdata(const void *, sv_iocdata_t *, int, caddr_t);
171 static int vhci_get_iocswitchdata(const void *, sv_switch_to_cntlr_iocdata_t *,
172     int, caddr_t);
173 static int vhci_ioc_alloc_pathinfo(sv_path_info_t **, sv_path_info_t **,
174     uint_t, sv_iocdata_t *, int, caddr_t);
175 static void vhci_ioc_free_pathinfo(sv_path_info_t *, sv_path_info_t *, uint_t);
176 static int vhci_ioc_send_pathinfo(sv_path_info_t *, sv_path_info_t *, uint_t,
177     sv_iocdata_t *, int, caddr_t);
178 static int vhci_handle_ext_fo(struct scsi_pkt *, int);
179 static int vhci_efo_watch_cb(caddr_t, struct scsi_watch_result *);
180 static int vhci_quiesce_lun(struct scsi_vhci_lun *);
181 static int vhci_pgr_validate_and_register(scsi_vhci_priv_t *);
182 static void vhci_dispatch_scsi_start(void *);
183 static void vhci_efo_done(void *);
184 static void vhci_initiate_auto_failback(void *);
185 static void vhci_update_pHCI_pkt(struct vhci_pkt *, struct scsi_pkt *);
186 static int vhci_update_pathinfo(struct scsi_device *, mdi_pathinfo_t *,
187     struct scsi_failover_ops *, scsi_vhci_lun_t *, struct scsi_vhci *);
188 static void vhci_kstat_create_pathinfo(mdi_pathinfo_t *);
189 static int vhci_quiesce_paths(dev_info_t *, dev_info_t *,
190     scsi_vhci_lun_t *, char *, char *);
191 
192 static char *vhci_devnm_to_guid(char *);
193 static int vhci_bind_transport(struct scsi_address *, struct vhci_pkt *,
194     int, int (*func)(caddr_t));
195 static void vhci_intr(struct scsi_pkt *);
196 static int vhci_do_prout(scsi_vhci_priv_t *);
197 static void vhci_run_cmd(void *);
198 static int vhci_do_prin(struct vhci_pkt **);
199 static struct scsi_pkt *vhci_create_retry_pkt(struct vhci_pkt *);
200 static struct vhci_pkt *vhci_sync_retry_pkt(struct vhci_pkt *);
201 static struct scsi_vhci_lun *vhci_lun_lookup(dev_info_t *);
202 static struct scsi_vhci_lun *vhci_lun_lookup_alloc(dev_info_t *, char *, int *);
203 static void vhci_lun_free(dev_info_t *);
204 static int vhci_recovery_reset(scsi_vhci_lun_t *, struct scsi_address *,
205     uint8_t, uint8_t);
206 void vhci_update_pathstates(void *);
207 
208 #ifdef DEBUG
209 static void vhci_print_prin_keys(vhci_prin_readkeys_t *, int);
210 #endif
211 static void vhci_print_prout_keys(scsi_vhci_lun_t *, char *);
212 static void vhci_uscsi_iodone(struct scsi_pkt *pkt);
213 
214 /*
215  * MP-API related functions
216  */
217 extern int vhci_mpapi_init(struct scsi_vhci *);
218 extern void vhci_mpapi_add_dev_prod(struct scsi_vhci *, char *);
219 extern int vhci_mpapi_ctl(dev_t, int, intptr_t, int, cred_t *, int *);
220 extern void vhci_update_mpapi_data(struct scsi_vhci *,
221     scsi_vhci_lun_t *, mdi_pathinfo_t *);
222 extern void* vhci_get_mpapi_item(struct scsi_vhci *, mpapi_list_header_t *,
223     uint8_t, void*);
224 extern void vhci_mpapi_set_path_state(dev_info_t *, mdi_pathinfo_t *, int);
225 extern int vhci_mpapi_update_tpg_acc_state_for_lu(struct scsi_vhci *,
226     scsi_vhci_lun_t *);
227 
228 /* Special export to MP-API of tpgs non-'fops' entry point */
229 int (*tpgs_set_target_groups)(struct scsi_address *, int, int);
230 
231 #define	VHCI_DMA_MAX_XFER_CAP	0xffffffffULL
232 
233 #define	VHCI_MAX_PGR_RETRIES	3
234 
235 /*
236  * Macros for the device-type mpxio options
237  */
238 #define	LOAD_BALANCE_OPTIONS		"load-balance-options"
239 #define	LOGICAL_BLOCK_REGION_SIZE	"region-size"
240 #define	MPXIO_OPTIONS_LIST		"device-type-mpxio-options-list"
241 #define	DEVICE_TYPE_STR			"device-type"
242 #define	isdigit(ch)			((ch) >= '0' && (ch) <= '9')
243 
244 static struct cb_ops vhci_cb_ops = {
245 	vhci_open,			/* open */
246 	vhci_close,			/* close */
247 	nodev,				/* strategy */
248 	nodev,				/* print */
249 	nodev,				/* dump */
250 	nodev,				/* read */
251 	nodev,				/* write */
252 	vhci_ioctl,			/* ioctl */
253 	nodev,				/* devmap */
254 	nodev,				/* mmap */
255 	nodev,				/* segmap */
256 	nochpoll,			/* chpoll */
257 	ddi_prop_op,			/* cb_prop_op */
258 	0,				/* streamtab */
259 	D_NEW | D_MP,			/* cb_flag */
260 	CB_REV,				/* rev */
261 	nodev,				/* aread */
262 	nodev				/* awrite */
263 };
264 
265 static struct dev_ops vhci_ops = {
266 	DEVO_REV,
267 	0,
268 	vhci_getinfo,
269 	nulldev,		/* identify */
270 	nulldev,		/* probe */
271 	vhci_attach,		/* attach and detach are mandatory */
272 	vhci_detach,
273 	nodev,			/* reset */
274 	&vhci_cb_ops,		/* cb_ops */
275 	NULL,			/* bus_ops */
276 	NULL,			/* power */
277 };
278 
279 extern struct mod_ops mod_driverops;
280 
281 static struct modldrv modldrv = {
282 	&mod_driverops,
283 	vhci_version_name,	/* module name */
284 	&vhci_ops
285 };
286 
287 static struct modlinkage modlinkage = {
288 	MODREV_1,
289 	&modldrv,
290 	NULL
291 };
292 
293 static mdi_vhci_ops_t vhci_opinfo = {
294 	MDI_VHCI_OPS_REV,
295 	vhci_pathinfo_init,		/* Pathinfo node init callback	*/
296 	vhci_pathinfo_uninit,		/* Pathinfo uninit callback	*/
297 	vhci_pathinfo_state_change,	/* Pathinfo node state change	*/
298 	vhci_failover,			/* failover callback		*/
299 	vhci_client_attached		/* client attached callback	*/
300 };
301 
302 /*
303  * The scsi_failover table defines an ordered set of 'fops' modules supported
304  * by scsi_vhci.  Currently, initialize this table from the 'ddi-forceload'
305  * property specified in scsi_vhci.conf.
306  */
307 struct scsi_failover {
308 	ddi_modhandle_t			sf_mod;
309 	struct scsi_failover_ops	*sf_sfo;
310 } *scsi_failover_table;
311 uint_t	scsi_nfailover;
312 
313 int
314 _init(void)
315 {
316 	int	rval;
317 
318 	/*
319 	 * Allocate soft state and prepare to do ddi_soft_state_zalloc()
320 	 * before registering with the transport first.
321 	 */
322 	if ((rval = ddi_soft_state_init(&vhci_softstate,
323 	    sizeof (struct scsi_vhci), 1)) != 0) {
324 		VHCI_DEBUG(1, (CE_NOTE, NULL,
325 		    "!_init:soft state init failed\n"));
326 		return (rval);
327 	}
328 
329 	if ((rval = scsi_hba_init(&modlinkage)) != 0) {
330 		VHCI_DEBUG(1, (CE_NOTE, NULL,
331 		    "!_init: scsi hba init failed\n"));
332 		ddi_soft_state_fini(&vhci_softstate);
333 		return (rval);
334 	}
335 
336 	mutex_init(&vhci_global_mutex, NULL, MUTEX_DRIVER, NULL);
337 	cv_init(&vhci_cv, NULL, CV_DRIVER, NULL);
338 
339 	mutex_init(&vhci_targetmap_mutex, NULL, MUTEX_DRIVER, NULL);
340 	vhci_targetmap_byport = mod_hash_create_strhash(
341 	    "vhci_targetmap_byport", 256, mod_hash_null_valdtor);
342 	vhci_targetmap_bypid = mod_hash_create_idhash(
343 	    "vhci_targetmap_bypid", 256, mod_hash_null_valdtor);
344 
345 	if ((rval = mod_install(&modlinkage)) != 0) {
346 		VHCI_DEBUG(1, (CE_NOTE, NULL, "!_init: mod_install failed\n"));
347 		if (vhci_targetmap_bypid)
348 			mod_hash_destroy_idhash(vhci_targetmap_bypid);
349 		if (vhci_targetmap_byport)
350 			mod_hash_destroy_strhash(vhci_targetmap_byport);
351 		mutex_destroy(&vhci_targetmap_mutex);
352 		cv_destroy(&vhci_cv);
353 		mutex_destroy(&vhci_global_mutex);
354 		scsi_hba_fini(&modlinkage);
355 		ddi_soft_state_fini(&vhci_softstate);
356 	}
357 	return (rval);
358 }
359 
360 
361 /*
362  * the system is done with us as a driver, so clean up
363  */
364 int
365 _fini(void)
366 {
367 	int rval;
368 
369 	/*
370 	 * don't start cleaning up until we know that the module remove
371 	 * has worked  -- if this works, then we know that each instance
372 	 * has successfully been DDI_DETACHed
373 	 */
374 	if ((rval = mod_remove(&modlinkage)) != 0) {
375 		VHCI_DEBUG(4, (CE_NOTE, NULL, "!_fini: mod_remove failed\n"));
376 		return (rval);
377 	}
378 
379 	if (vhci_targetmap_bypid)
380 		mod_hash_destroy_idhash(vhci_targetmap_bypid);
381 	if (vhci_targetmap_byport)
382 		mod_hash_destroy_strhash(vhci_targetmap_byport);
383 	mutex_destroy(&vhci_targetmap_mutex);
384 	cv_destroy(&vhci_cv);
385 	mutex_destroy(&vhci_global_mutex);
386 	scsi_hba_fini(&modlinkage);
387 	ddi_soft_state_fini(&vhci_softstate);
388 
389 	return (rval);
390 }
391 
392 int
393 _info(struct modinfo *modinfop)
394 {
395 	return (mod_info(&modlinkage, modinfop));
396 }
397 
398 /*
399  * Lookup scsi_failover by "short name" of failover module.
400  */
401 struct scsi_failover_ops *
402 vhci_failover_ops_by_name(char *name)
403 {
404 	struct scsi_failover	*sf;
405 
406 	for (sf = scsi_failover_table; sf->sf_mod; sf++) {
407 		if (sf->sf_sfo == NULL)
408 			continue;
409 		if (strcmp(sf->sf_sfo->sfo_name, name) == 0)
410 			return (sf->sf_sfo);
411 	}
412 	return (NULL);
413 }
414 
415 /*
416  * Load all scsi_failover_ops 'fops' modules.
417  */
418 static void
419 vhci_failover_modopen(struct scsi_vhci *vhci)
420 {
421 	char			**module;
422 	int			i;
423 	struct scsi_failover	*sf;
424 	char			**dt;
425 	int			e;
426 
427 	if (scsi_failover_table)
428 		return;
429 
430 	/* Get the list of modules from scsi_vhci.conf */
431 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY,
432 	    vhci->vhci_dip, DDI_PROP_DONTPASS, "ddi-forceload",
433 	    &module, &scsi_nfailover) != DDI_PROP_SUCCESS) {
434 		cmn_err(CE_WARN, "scsi_vhci: "
435 		    "scsi_vhci.conf is missing 'ddi-forceload'");
436 		return;
437 	}
438 	if (scsi_nfailover == 0) {
439 		cmn_err(CE_WARN, "scsi_vhci: "
440 		    "scsi_vhci.conf has empty 'ddi-forceload'");
441 		ddi_prop_free(module);
442 		return;
443 	}
444 
445 	/* allocate failover table based on number of modules */
446 	scsi_failover_table = (struct scsi_failover *)
447 	    kmem_zalloc(sizeof (struct scsi_failover) * (scsi_nfailover + 1),
448 	    KM_SLEEP);
449 
450 	/* loop over modules specified in scsi_vhci.conf and open each module */
451 	for (i = 0, sf = scsi_failover_table; i < scsi_nfailover; i++) {
452 		if (module[i] == NULL)
453 			continue;
454 
455 		sf->sf_mod = ddi_modopen(module[i], KRTLD_MODE_FIRST, &e);
456 		if (sf->sf_mod == NULL) {
457 			/*
458 			 * A module returns EEXIST if other software is
459 			 * supporting the intended function: for example
460 			 * the scsi_vhci_f_sum_emc module returns EEXIST
461 			 * from _init if EMC powerpath software is installed.
462 			 */
463 			if (e != EEXIST)
464 				cmn_err(CE_WARN, "scsi_vhci: unable to open "
465 				    "module '%s', error %d", module[i], e);
466 			continue;
467 		}
468 		sf->sf_sfo = ddi_modsym(sf->sf_mod,
469 		    "scsi_vhci_failover_ops", &e);
470 		if (sf->sf_sfo == NULL) {
471 			cmn_err(CE_WARN, "scsi_vhci: "
472 			    "unable to import 'scsi_failover_ops' from '%s', "
473 			    "error %d", module[i], e);
474 			(void) ddi_modclose(sf->sf_mod);
475 			sf->sf_mod = NULL;
476 			continue;
477 		}
478 
479 		/* register vid/pid of devices supported with mpapi */
480 		for (dt = sf->sf_sfo->sfo_devices; *dt; dt++)
481 			vhci_mpapi_add_dev_prod(vhci, *dt);
482 
483 		/*
484 		 * Special processing for SFO_NAME_TPGS module, which contains
485 		 * the `tpgs_set_target_groups` implementation needed by the
486 		 * MP-API code.
487 		 */
488 		if (strcmp(sf->sf_sfo->sfo_name, SFO_NAME_TPGS) == 0) {
489 			tpgs_set_target_groups =
490 			    (int (*)(struct scsi_address *, int, int))
491 			    ddi_modsym(sf->sf_mod, "std_set_target_groups", &e);
492 			if (tpgs_set_target_groups == NULL) {
493 				cmn_err(CE_WARN, "scsi_vhci: "
494 				    "unable to import 'std_set_target_groups' "
495 				    "from '%s', error %d", module[i], e);
496 			}
497 		}
498 
499 		sf++;
500 	}
501 
502 	/* verify that at least the "well-known" modules were there */
503 	if (vhci_failover_ops_by_name(SFO_NAME_SYM) == NULL)
504 		cmn_err(CE_WARN, "scsi_vhci: well-known module \""
505 		    SFO_NAME_SYM "\" not defined in scsi_vhci.conf's "
506 		    "'ddi-forceload'");
507 	if (vhci_failover_ops_by_name(SFO_NAME_TPGS) == NULL)
508 		cmn_err(CE_WARN, "scsi_vhci: well-known module \""
509 		    SFO_NAME_TPGS "\" not defined in scsi_vhci.conf's "
510 		    "'ddi-forceload'");
511 
512 	/* call sfo_init for modules that need it */
513 	for (sf = scsi_failover_table; sf->sf_mod; sf++) {
514 		if (sf->sf_sfo && sf->sf_sfo->sfo_init)
515 			(*sf->sf_sfo->sfo_init)();
516 	}
517 
518 	ddi_prop_free(module);
519 }
520 
521 /*
522  * unload all loaded scsi_failover_ops modules
523  */
524 static void
525 vhci_failover_modclose()
526 {
527 	struct scsi_failover	*sf;
528 
529 	for (sf = scsi_failover_table; sf->sf_mod; sf++) {
530 		if ((sf->sf_mod == NULL) || (sf->sf_sfo == NULL))
531 			continue;
532 		(void) ddi_modclose(sf->sf_mod);
533 		sf->sf_mod = NULL;
534 		sf->sf_sfo = NULL;
535 	}
536 
537 	if (scsi_failover_table && scsi_nfailover)
538 		kmem_free(scsi_failover_table,
539 		    sizeof (struct scsi_failover) * (scsi_nfailover + 1));
540 	scsi_failover_table = NULL;
541 	scsi_nfailover = 0;
542 }
543 
544 /* ARGSUSED */
545 static int
546 vhci_open(dev_t *devp, int flag, int otype, cred_t *credp)
547 {
548 	struct scsi_vhci	*vhci;
549 
550 	if (otype != OTYP_CHR) {
551 		return (EINVAL);
552 	}
553 
554 	vhci = ddi_get_soft_state(vhci_softstate, MINOR2INST(getminor(*devp)));
555 	if (vhci == NULL) {
556 		VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_open: failed ENXIO\n"));
557 		return (ENXIO);
558 	}
559 
560 	mutex_enter(&vhci->vhci_mutex);
561 	if ((flag & FEXCL) && (vhci->vhci_state & VHCI_STATE_OPEN)) {
562 		mutex_exit(&vhci->vhci_mutex);
563 		vhci_log(CE_NOTE, vhci->vhci_dip,
564 		    "!vhci%d: Already open\n", getminor(*devp));
565 		return (EBUSY);
566 	}
567 
568 	vhci->vhci_state |= VHCI_STATE_OPEN;
569 	mutex_exit(&vhci->vhci_mutex);
570 	return (0);
571 }
572 
573 
574 /* ARGSUSED */
575 static int
576 vhci_close(dev_t dev, int flag, int otype, cred_t *credp)
577 {
578 	struct scsi_vhci	*vhci;
579 
580 	if (otype != OTYP_CHR) {
581 		return (EINVAL);
582 	}
583 
584 	vhci = ddi_get_soft_state(vhci_softstate, MINOR2INST(getminor(dev)));
585 	if (vhci == NULL) {
586 		VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_close: failed ENXIO\n"));
587 		return (ENXIO);
588 	}
589 
590 	mutex_enter(&vhci->vhci_mutex);
591 	vhci->vhci_state &= ~VHCI_STATE_OPEN;
592 	mutex_exit(&vhci->vhci_mutex);
593 
594 	return (0);
595 }
596 
597 /* ARGSUSED */
598 static int
599 vhci_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
600 	cred_t *credp, int *rval)
601 {
602 	if (IS_DEVCTL(cmd)) {
603 		return (vhci_devctl(dev, cmd, data, mode, credp, rval));
604 	} else if (cmd == MP_CMD) {
605 		return (vhci_mpapi_ctl(dev, cmd, data, mode, credp, rval));
606 	} else {
607 		return (vhci_ctl(dev, cmd, data, mode, credp, rval));
608 	}
609 }
610 
611 /*
612  * attach the module
613  */
614 static int
615 vhci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
616 {
617 	int			rval = DDI_FAILURE;
618 	int			scsi_hba_attached = 0;
619 	int			vhci_attached = 0;
620 	int			mutex_initted = 0;
621 	int			instance;
622 	struct scsi_vhci	*vhci;
623 	scsi_hba_tran_t		*tran;
624 	char			cache_name_buf[64];
625 	char			*data;
626 
627 	VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_attach: cmd=0x%x\n", cmd));
628 
629 	instance = ddi_get_instance(dip);
630 
631 	switch (cmd) {
632 	case DDI_ATTACH:
633 		break;
634 
635 	case DDI_RESUME:
636 	case DDI_PM_RESUME:
637 		VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_attach: resume not yet"
638 		    "implemented\n"));
639 		return (rval);
640 
641 	default:
642 		VHCI_DEBUG(1, (CE_NOTE, NULL,
643 		    "!vhci_attach: unknown ddi command\n"));
644 		return (rval);
645 	}
646 
647 	/*
648 	 * Allocate vhci data structure.
649 	 */
650 	if (ddi_soft_state_zalloc(vhci_softstate, instance) != DDI_SUCCESS) {
651 		VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:"
652 		    "soft state alloc failed\n"));
653 		return (DDI_FAILURE);
654 	}
655 
656 	if ((vhci = ddi_get_soft_state(vhci_softstate, instance)) == NULL) {
657 		VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:"
658 		    "bad soft state\n"));
659 		ddi_soft_state_free(vhci_softstate, instance);
660 		return (DDI_FAILURE);
661 	}
662 
663 	/* Allocate packet cache */
664 	(void) snprintf(cache_name_buf, sizeof (cache_name_buf),
665 	    "vhci%d_cache", instance);
666 
667 	mutex_init(&vhci->vhci_mutex, NULL, MUTEX_DRIVER, NULL);
668 	mutex_initted++;
669 
670 	/*
671 	 * Allocate a transport structure
672 	 */
673 	tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
674 	ASSERT(tran != NULL);
675 
676 	vhci->vhci_tran		= tran;
677 	vhci->vhci_dip		= dip;
678 	vhci->vhci_instance	= instance;
679 
680 	tran->tran_hba_private	= vhci;
681 	tran->tran_tgt_private	= NULL;
682 	tran->tran_tgt_init	= vhci_scsi_tgt_init;
683 	tran->tran_tgt_probe	= NULL;
684 	tran->tran_tgt_free	= vhci_scsi_tgt_free;
685 
686 	tran->tran_start	= vhci_scsi_start;
687 	tran->tran_abort	= vhci_scsi_abort;
688 	tran->tran_reset	= vhci_scsi_reset;
689 	tran->tran_getcap	= vhci_scsi_getcap;
690 	tran->tran_setcap	= vhci_scsi_setcap;
691 	tran->tran_init_pkt	= vhci_scsi_init_pkt;
692 	tran->tran_destroy_pkt	= vhci_scsi_destroy_pkt;
693 	tran->tran_dmafree	= vhci_scsi_dmafree;
694 	tran->tran_sync_pkt	= vhci_scsi_sync_pkt;
695 	tran->tran_reset_notify = vhci_scsi_reset_notify;
696 
697 	tran->tran_get_bus_addr	= vhci_scsi_get_bus_addr;
698 	tran->tran_get_name	= vhci_scsi_get_name;
699 	tran->tran_bus_reset	= NULL;
700 	tran->tran_quiesce	= NULL;
701 	tran->tran_unquiesce	= NULL;
702 
703 	/*
704 	 * register event notification routines with scsa
705 	 */
706 	tran->tran_get_eventcookie = NULL;
707 	tran->tran_add_eventcall = NULL;
708 	tran->tran_remove_eventcall = NULL;
709 	tran->tran_post_event = NULL;
710 
711 	tran->tran_bus_power = vhci_scsi_bus_power;
712 
713 	tran->tran_bus_config = vhci_scsi_bus_config;
714 
715 	/*
716 	 * Attach this instance with the mpxio framework
717 	 */
718 	if (mdi_vhci_register(MDI_HCI_CLASS_SCSI, dip, &vhci_opinfo, 0)
719 	    != MDI_SUCCESS) {
720 		VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:"
721 		    "mdi_vhci_register failed\n"));
722 		goto attach_fail;
723 	}
724 	vhci_attached++;
725 
726 	/*
727 	 * Attach this instance of the hba.
728 	 *
729 	 * Regarding dma attributes: Since scsi_vhci is a virtual scsi HBA
730 	 * driver, it has nothing to do with DMA. However, when calling
731 	 * scsi_hba_attach_setup() we need to pass something valid in the
732 	 * dma attributes parameter. So we just use scsi_alloc_attr.
733 	 * SCSA itself seems to care only for dma_attr_minxfer and
734 	 * dma_attr_burstsizes fields of dma attributes structure.
735 	 * It expects those fileds to be non-zero.
736 	 */
737 	if (scsi_hba_attach_setup(dip, &scsi_alloc_attr, tran,
738 	    SCSI_HBA_TRAN_CLONE) != DDI_SUCCESS) {
739 		VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:"
740 		    "hba attach failed\n"));
741 		goto attach_fail;
742 	}
743 	scsi_hba_attached++;
744 
745 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
746 	    INST2DEVCTL(instance), DDI_NT_SCSI_NEXUS, 0) != DDI_SUCCESS) {
747 		VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:"
748 		    " ddi_create_minor_node failed\n"));
749 		goto attach_fail;
750 	}
751 
752 	/*
753 	 * Set pm-want-child-notification property for
754 	 * power management of the phci and client
755 	 */
756 	if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
757 	    "pm-want-child-notification?", NULL, NULL) != DDI_PROP_SUCCESS) {
758 		cmn_err(CE_WARN,
759 		    "%s%d fail to create pm-want-child-notification? prop",
760 		    ddi_driver_name(dip), ddi_get_instance(dip));
761 		goto attach_fail;
762 	}
763 
764 	vhci->vhci_taskq = taskq_create("vhci_taskq", 1, MINCLSYSPRI, 1, 4, 0);
765 	vhci->vhci_update_pathstates_taskq =
766 	    taskq_create("vhci_update_pathstates", VHCI_NUM_UPDATE_TASKQ,
767 	    MINCLSYSPRI, 1, 4, 0);
768 	ASSERT(vhci->vhci_taskq);
769 	ASSERT(vhci->vhci_update_pathstates_taskq);
770 
771 	/*
772 	 * Set appropriate configuration flags based on options set in
773 	 * conf file.
774 	 */
775 	vhci->vhci_conf_flags = 0;
776 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, PROPFLAGS,
777 	    "auto-failback", &data) == DDI_SUCCESS) {
778 		if (strcmp(data, "enable") == 0)
779 			vhci->vhci_conf_flags |= VHCI_CONF_FLAGS_AUTO_FAILBACK;
780 		ddi_prop_free(data);
781 	}
782 
783 	if (!(vhci->vhci_conf_flags & VHCI_CONF_FLAGS_AUTO_FAILBACK))
784 		vhci_log(CE_NOTE, dip, "!Auto-failback capability "
785 		    "disabled through scsi_vhci.conf file.");
786 
787 	/*
788 	 * Allocate an mpapi private structure
789 	 */
790 	vhci->mp_priv = kmem_zalloc(sizeof (mpapi_priv_t), KM_SLEEP);
791 	if (vhci_mpapi_init(vhci) != 0) {
792 		VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_attach: "
793 		    "vhci_mpapi_init() failed"));
794 	}
795 
796 	vhci_failover_modopen(vhci);		/* load failover modules */
797 
798 	ddi_report_dev(dip);
799 	return (DDI_SUCCESS);
800 
801 attach_fail:
802 	if (vhci_attached)
803 		(void) mdi_vhci_unregister(dip, 0);
804 
805 	if (scsi_hba_attached)
806 		(void) scsi_hba_detach(dip);
807 
808 	if (vhci->vhci_tran)
809 		scsi_hba_tran_free(vhci->vhci_tran);
810 
811 	if (mutex_initted) {
812 		mutex_destroy(&vhci->vhci_mutex);
813 	}
814 
815 	ddi_soft_state_free(vhci_softstate, instance);
816 	return (DDI_FAILURE);
817 }
818 
819 
820 /*ARGSUSED*/
821 static int
822 vhci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
823 {
824 	int			instance = ddi_get_instance(dip);
825 	scsi_hba_tran_t		*tran;
826 	struct scsi_vhci	*vhci;
827 
828 	VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_detach: cmd=0x%x\n", cmd));
829 
830 	if ((tran = ddi_get_driver_private(dip)) == NULL)
831 		return (DDI_FAILURE);
832 
833 	vhci = TRAN2HBAPRIVATE(tran);
834 	if (!vhci) {
835 		return (DDI_FAILURE);
836 	}
837 
838 	switch (cmd) {
839 	case DDI_DETACH:
840 		break;
841 
842 	case DDI_SUSPEND:
843 	case DDI_PM_SUSPEND:
844 		VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_detach: suspend/pm not yet"
845 		    "implemented\n"));
846 		return (DDI_FAILURE);
847 
848 	default:
849 		VHCI_DEBUG(1, (CE_NOTE, NULL,
850 		    "!vhci_detach: unknown ddi command\n"));
851 		return (DDI_FAILURE);
852 	}
853 
854 	(void) mdi_vhci_unregister(dip, 0);
855 	(void) scsi_hba_detach(dip);
856 	scsi_hba_tran_free(tran);
857 
858 	if (ddi_prop_remove(DDI_DEV_T_NONE, dip,
859 	    "pm-want-child-notification?") != DDI_PROP_SUCCESS) {
860 		cmn_err(CE_WARN,
861 		    "%s%d unable to remove prop pm-want_child_notification?",
862 		    ddi_driver_name(dip), ddi_get_instance(dip));
863 	}
864 	if (vhci_restart_timeid != 0) {
865 		(void) untimeout(vhci_restart_timeid);
866 	}
867 	vhci_restart_timeid = 0;
868 
869 	mutex_destroy(&vhci->vhci_mutex);
870 	vhci->vhci_dip = NULL;
871 	vhci->vhci_tran = NULL;
872 	taskq_destroy(vhci->vhci_taskq);
873 	taskq_destroy(vhci->vhci_update_pathstates_taskq);
874 	ddi_remove_minor_node(dip, NULL);
875 	ddi_soft_state_free(vhci_softstate, instance);
876 
877 	vhci_failover_modclose();		/* unload failover modules */
878 	return (DDI_SUCCESS);
879 }
880 
881 /*
882  * vhci_getinfo()
883  * Given the device number, return the devinfo pointer or the
884  * instance number.
885  * Note: always succeed DDI_INFO_DEVT2INSTANCE, even before attach.
886  */
887 
888 /*ARGSUSED*/
889 static int
890 vhci_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
891 {
892 	struct scsi_vhci	*vhcip;
893 	int			instance = MINOR2INST(getminor((dev_t)arg));
894 
895 	switch (cmd) {
896 	case DDI_INFO_DEVT2DEVINFO:
897 		vhcip = ddi_get_soft_state(vhci_softstate, instance);
898 		if (vhcip != NULL)
899 			*result = vhcip->vhci_dip;
900 		else {
901 			*result = NULL;
902 			return (DDI_FAILURE);
903 		}
904 		break;
905 
906 	case DDI_INFO_DEVT2INSTANCE:
907 		*result = (void *)(uintptr_t)instance;
908 		break;
909 
910 	default:
911 		return (DDI_FAILURE);
912 	}
913 
914 	return (DDI_SUCCESS);
915 }
916 
917 
918 /*ARGSUSED*/
919 static int
920 vhci_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
921 	scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
922 {
923 	char			*guid;
924 	scsi_vhci_lun_t		*vlun;
925 	struct scsi_vhci	*vhci;
926 	clock_t			from_ticks;
927 	mdi_pathinfo_t		*pip;
928 	int			rval;
929 
930 	ASSERT(hba_dip != NULL);
931 	ASSERT(tgt_dip != NULL);
932 
933 	vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(hba_dip));
934 	ASSERT(vhci != NULL);
935 
936 	VHCI_DEBUG(4, (CE_NOTE, hba_dip,
937 	    "!tgt_init: called for %s (instance %d)\n",
938 	    ddi_driver_name(tgt_dip), ddi_get_instance(tgt_dip)));
939 
940 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, tgt_dip, PROPFLAGS,
941 	    MDI_CLIENT_GUID_PROP, &guid) != DDI_SUCCESS) {
942 		/*
943 		 * This must be the .conf node.  The ssd node under
944 		 * fp already inserts a delay, so we just return from here.
945 		 * We rely on this delay to have all dips be posted to
946 		 * the ndi hotplug thread's newdev list.  This is
947 		 * necessary for the deferred attach mechanism to work
948 		 * and opens() done soon after boot to succeed.
949 		 */
950 		VHCI_DEBUG(4, (CE_WARN, hba_dip, "tgt_init: lun guid "
951 		    "property failed"));
952 		return (DDI_NOT_WELL_FORMED);
953 	}
954 
955 	vlun = vhci_lun_lookup(tgt_dip);
956 
957 	mutex_enter(&vhci_global_mutex);
958 
959 	from_ticks = ddi_get_lbolt();
960 	if (vhci_to_ticks == 0) {
961 		vhci_to_ticks = from_ticks +
962 		    drv_usectohz(vhci_init_wait_timeout);
963 	}
964 
965 #if DEBUG
966 	if (vlun) {
967 		VHCI_DEBUG(1, (CE_WARN, hba_dip, "tgt_init: "
968 		    "vhci_scsi_tgt_init: guid %s : found vlun 0x%p "
969 		    "from_ticks %lx to_ticks %lx",
970 		    guid, (void *)vlun, from_ticks, vhci_to_ticks));
971 	} else {
972 		VHCI_DEBUG(1, (CE_WARN, hba_dip, "tgt_init: "
973 		    "vhci_scsi_tgt_init: guid %s : vlun not found "
974 		    "from_ticks %lx to_ticks %lx", guid, from_ticks,
975 		    vhci_to_ticks));
976 	}
977 #endif
978 
979 	rval = mdi_select_path(tgt_dip, NULL,
980 	    (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH), NULL, &pip);
981 	if (rval == MDI_SUCCESS) {
982 		mdi_rele_path(pip);
983 	}
984 
985 	/*
986 	 * Wait for the following conditions :
987 	 *	1. no vlun available yet
988 	 *	2. no path established
989 	 *	3. timer did not expire
990 	 */
991 	while ((vlun == NULL) || (mdi_client_get_path_count(tgt_dip) == 0) ||
992 	    (rval != MDI_SUCCESS)) {
993 		if (vlun && vlun->svl_not_supported) {
994 			VHCI_DEBUG(1, (CE_WARN, hba_dip, "tgt_init: "
995 			    "vlun 0x%p lun guid %s not supported!",
996 			    (void *)vlun, guid));
997 			mutex_exit(&vhci_global_mutex);
998 			ddi_prop_free(guid);
999 			return (DDI_NOT_WELL_FORMED);
1000 		}
1001 		if ((vhci_first_time == 0) && (from_ticks >= vhci_to_ticks)) {
1002 			vhci_first_time = 1;
1003 		}
1004 		if (vhci_first_time == 1) {
1005 			VHCI_DEBUG(1, (CE_WARN, hba_dip, "vhci_scsi_tgt_init: "
1006 			    "no wait for %s. from_tick %lx, to_tick %lx",
1007 			    guid, from_ticks, vhci_to_ticks));
1008 			mutex_exit(&vhci_global_mutex);
1009 			ddi_prop_free(guid);
1010 			return (DDI_NOT_WELL_FORMED);
1011 		}
1012 
1013 		if (cv_timedwait(&vhci_cv,
1014 		    &vhci_global_mutex, vhci_to_ticks) == -1) {
1015 			/* Timed out */
1016 #ifdef DEBUG
1017 			if (vlun == NULL) {
1018 				VHCI_DEBUG(1, (CE_WARN, hba_dip,
1019 				    "tgt_init: no vlun for %s!", guid));
1020 			} else if (mdi_client_get_path_count(tgt_dip) == 0) {
1021 				VHCI_DEBUG(1, (CE_WARN, hba_dip,
1022 				    "tgt_init: client path count is "
1023 				    "zero for %s!", guid));
1024 			} else {
1025 				VHCI_DEBUG(1, (CE_WARN, hba_dip,
1026 				    "tgt_init: client path not "
1027 				    "available yet for %s!", guid));
1028 			}
1029 #endif /* DEBUG */
1030 			mutex_exit(&vhci_global_mutex);
1031 			ddi_prop_free(guid);
1032 			return (DDI_NOT_WELL_FORMED);
1033 		}
1034 		vlun = vhci_lun_lookup(tgt_dip);
1035 		rval = mdi_select_path(tgt_dip, NULL,
1036 		    (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH),
1037 		    NULL, &pip);
1038 		if (rval == MDI_SUCCESS) {
1039 			mdi_rele_path(pip);
1040 		}
1041 		from_ticks = ddi_get_lbolt();
1042 	}
1043 	mutex_exit(&vhci_global_mutex);
1044 
1045 	ASSERT(vlun != NULL);
1046 	ddi_prop_free(guid);
1047 	hba_tran->tran_tgt_private = vlun;
1048 
1049 	return (DDI_SUCCESS);
1050 }
1051 
1052 /*ARGSUSED*/
1053 static void
1054 vhci_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1055 	scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1056 {
1057 }
1058 
1059 /*
1060  * a PGR register command has started; copy the info we need
1061  */
1062 int
1063 vhci_pgr_register_start(scsi_vhci_lun_t *vlun, struct scsi_pkt *pkt)
1064 {
1065 	struct vhci_pkt		*vpkt = TGTPKT2VHCIPKT(pkt);
1066 	void			*addr;
1067 
1068 	if (!vpkt->vpkt_tgt_init_bp)
1069 		return (TRAN_BADPKT);
1070 
1071 	addr = bp_mapin_common(vpkt->vpkt_tgt_init_bp,
1072 	    (vpkt->vpkt_flags & CFLAG_NOWAIT) ? VM_NOSLEEP : VM_SLEEP);
1073 	if (addr == NULL)
1074 		return (TRAN_BUSY);
1075 
1076 	mutex_enter(&vlun->svl_mutex);
1077 
1078 	vhci_print_prout_keys(vlun, "v_pgr_reg_start: before bcopy:");
1079 
1080 	bcopy(addr, &vlun->svl_prout, sizeof (vhci_prout_t) -
1081 	    (2 * MHIOC_RESV_KEY_SIZE*sizeof (char)));
1082 	bcopy(pkt->pkt_cdbp, vlun->svl_cdb, sizeof (vlun->svl_cdb));
1083 
1084 	vhci_print_prout_keys(vlun, "v_pgr_reg_start: after bcopy:");
1085 
1086 	vlun->svl_time = pkt->pkt_time;
1087 	vlun->svl_bcount = vpkt->vpkt_tgt_init_bp->b_bcount;
1088 	vlun->svl_first_path = vpkt->vpkt_path;
1089 	mutex_exit(&vlun->svl_mutex);
1090 	return (0);
1091 }
1092 
1093 /*
1094  * Function name : vhci_scsi_start()
1095  *
1096  * Return Values : TRAN_FATAL_ERROR	- vhci has been shutdown
1097  *					  or other fatal failure
1098  *					  preventing packet transportation
1099  *		   TRAN_BUSY		- request queue is full
1100  *		   TRAN_ACCEPT		- pkt has been submitted to phci
1101  *					  (or is held in the waitQ)
1102  * Description	 : Implements SCSA's tran_start() entry point for
1103  *		   packet transport
1104  *
1105  */
1106 static int
1107 vhci_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
1108 {
1109 	int			rval = TRAN_ACCEPT;
1110 	int			instance, held;
1111 	struct scsi_vhci	*vhci = ADDR2VHCI(ap);
1112 	struct scsi_vhci_lun	*vlun = ADDR2VLUN(ap);
1113 	struct vhci_pkt		*vpkt = TGTPKT2VHCIPKT(pkt);
1114 	int			flags = 0;
1115 	scsi_vhci_priv_t	*svp;
1116 	dev_info_t 		*cdip;
1117 	client_lb_t		lbp;
1118 	int			restore_lbp = 0;
1119 	/* set if pkt is SCSI-II RESERVE cmd */
1120 	int			pkt_reserve_cmd = 0;
1121 	int			reserve_failed = 0;
1122 
1123 	ASSERT(vhci != NULL);
1124 	ASSERT(vpkt != NULL);
1125 	ASSERT(vpkt->vpkt_state != VHCI_PKT_ISSUED);
1126 	cdip = ADDR2DIP(ap);
1127 
1128 	/*
1129 	 * Block IOs if LUN is held or QUIESCED for IOs.
1130 	 */
1131 	if ((VHCI_LUN_IS_HELD(vlun)) ||
1132 	    ((vlun->svl_flags & VLUN_QUIESCED_FLG) == VLUN_QUIESCED_FLG)) {
1133 		return (TRAN_BUSY);
1134 	}
1135 
1136 	/*
1137 	 * vhci_lun needs to be quiesced before SCSI-II RESERVE command
1138 	 * can be issued.  This may require a cv_timedwait, which is
1139 	 * dangerous to perform in an interrupt context.  So if this
1140 	 * is a RESERVE command a taskq is dispatched to service it.
1141 	 * This taskq shall again call vhci_scsi_start, but we shall be
1142 	 * sure its not in an interrupt context.
1143 	 */
1144 	if ((pkt->pkt_cdbp[0] == SCMD_RESERVE) ||
1145 	    (pkt->pkt_cdbp[0] == SCMD_RESERVE_G1)) {
1146 		if (!(vpkt->vpkt_state & VHCI_PKT_THRU_TASKQ)) {
1147 			if (taskq_dispatch(vhci->vhci_taskq,
1148 			    vhci_dispatch_scsi_start, (void *) vpkt,
1149 			    KM_NOSLEEP)) {
1150 				return (TRAN_ACCEPT);
1151 			} else {
1152 				return (TRAN_BUSY);
1153 			}
1154 		}
1155 
1156 		/*
1157 		 * Here we ensure that simultaneous SCSI-II RESERVE cmds don't
1158 		 * get serviced for a lun.
1159 		 */
1160 		VHCI_HOLD_LUN(vlun, VH_NOSLEEP, held);
1161 		if (!held) {
1162 			return (TRAN_BUSY);
1163 		} else if ((vlun->svl_flags & VLUN_QUIESCED_FLG) ==
1164 		    VLUN_QUIESCED_FLG) {
1165 			VHCI_RELEASE_LUN(vlun);
1166 			return (TRAN_BUSY);
1167 		}
1168 
1169 		/*
1170 		 * To ensure that no IOs occur for this LUN for the duration
1171 		 * of this pkt set the VLUN_QUIESCED_FLG.
1172 		 * In case this routine needs to exit on error make sure that
1173 		 * this flag is cleared.
1174 		 */
1175 		vlun->svl_flags |= VLUN_QUIESCED_FLG;
1176 		pkt_reserve_cmd = 1;
1177 
1178 		/*
1179 		 * if this is a SCSI-II RESERVE command, set load balancing
1180 		 * policy to be ALTERNATE PATH to ensure that all subsequent
1181 		 * IOs are routed on the same path.  This is because if commands
1182 		 * are routed across multiple paths then IOs on paths other than
1183 		 * the one on which the RESERVE was executed will get a
1184 		 * RESERVATION CONFLICT
1185 		 */
1186 		lbp = mdi_get_lb_policy(cdip);
1187 		if (lbp != LOAD_BALANCE_NONE) {
1188 			if (vhci_quiesce_lun(vlun) != 1) {
1189 				vlun->svl_flags &= ~VLUN_QUIESCED_FLG;
1190 				VHCI_RELEASE_LUN(vlun);
1191 				return (TRAN_FATAL_ERROR);
1192 			}
1193 			vlun->svl_lb_policy_save = lbp;
1194 			if (mdi_set_lb_policy(cdip, LOAD_BALANCE_NONE) !=
1195 			    MDI_SUCCESS) {
1196 				vlun->svl_flags &= ~VLUN_QUIESCED_FLG;
1197 				VHCI_RELEASE_LUN(vlun);
1198 				return (TRAN_FATAL_ERROR);
1199 			}
1200 			restore_lbp = 1;
1201 		}
1202 		/*
1203 		 * See comments for VLUN_RESERVE_ACTIVE_FLG in scsi_vhci.h
1204 		 * To narrow this window where a reserve command may be sent
1205 		 * down an inactive path the path states first need to be
1206 		 * updated. Before calling vhci_update_pathstates reset
1207 		 * VLUN_RESERVE_ACTIVE_FLG, just in case it was already set
1208 		 * for this lun.  This shall prevent an unnecessary reset
1209 		 * from being sent out.
1210 		 */
1211 		vlun->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG;
1212 		vhci_update_pathstates((void *)vlun);
1213 	}
1214 
1215 	instance = ddi_get_instance(vhci->vhci_dip);
1216 
1217 	/*
1218 	 * If the command is PRIN with action of zero, then the cmd
1219 	 * is reading PR keys which requires filtering on completion.
1220 	 * Data cache sync must be guaranteed.
1221 	 */
1222 	if ((pkt->pkt_cdbp[0] == SCMD_PRIN) &&
1223 	    (pkt->pkt_cdbp[1] == 0) &&
1224 	    (vpkt->vpkt_org_vpkt == NULL)) {
1225 		vpkt->vpkt_tgt_init_pkt_flags |= PKT_CONSISTENT;
1226 	}
1227 
1228 	/*
1229 	 * Do not defer bind for PKT_DMA_PARTIAL
1230 	 */
1231 	if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) {
1232 
1233 		/* This is a non pkt_dma_partial case */
1234 		if ((rval = vhci_bind_transport(
1235 		    ap, vpkt, vpkt->vpkt_tgt_init_pkt_flags, NULL_FUNC))
1236 		    != TRAN_ACCEPT) {
1237 			VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip,
1238 			    "!vhci%d %x: failed to bind transport: "
1239 			    "vlun 0x%p pkt_reserved %x restore_lbp %x,"
1240 			    "lbp %x", instance, rval, (void *)vlun,
1241 			    pkt_reserve_cmd, restore_lbp, lbp));
1242 			if (restore_lbp)
1243 				(void) mdi_set_lb_policy(cdip, lbp);
1244 			if (pkt_reserve_cmd)
1245 				vlun->svl_flags &= ~VLUN_QUIESCED_FLG;
1246 			return (rval);
1247 		}
1248 		VHCI_DEBUG(8, (CE_NOTE, NULL,
1249 		    "vhci_scsi_start: v_b_t called 0x%p\n", (void *)vpkt));
1250 	}
1251 	ASSERT(vpkt->vpkt_hba_pkt != NULL);
1252 	ASSERT(vpkt->vpkt_path != NULL);
1253 
1254 	/*
1255 	 * This is the chance to adjust the pHCI's pkt and other information
1256 	 * from target driver's pkt.
1257 	 */
1258 	VHCI_DEBUG(8, (CE_NOTE, vhci->vhci_dip, "vhci_scsi_start vpkt %p\n",
1259 	    (void *)vpkt));
1260 	vhci_update_pHCI_pkt(vpkt, pkt);
1261 
1262 	if (vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) {
1263 		if (vpkt->vpkt_path != vlun->svl_resrv_pip) {
1264 			VHCI_DEBUG(1, (CE_WARN, vhci->vhci_dip,
1265 			    "!vhci_bind: reserve flag set for vlun 0x%p, but, "
1266 			    "pktpath 0x%p resrv path 0x%p differ. lb_policy %x",
1267 			    (void *)vlun, (void *)vpkt->vpkt_path,
1268 			    (void *)vlun->svl_resrv_pip,
1269 			    mdi_get_lb_policy(cdip)));
1270 			reserve_failed = 1;
1271 		}
1272 	}
1273 
1274 	svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(
1275 	    vpkt->vpkt_path);
1276 	if (svp == NULL || reserve_failed) {
1277 		if (pkt_reserve_cmd) {
1278 			VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip,
1279 			    "!vhci_bind returned null svp vlun 0x%p",
1280 			    (void *)vlun));
1281 			vlun->svl_flags &= ~VLUN_QUIESCED_FLG;
1282 			if (restore_lbp)
1283 				(void) mdi_set_lb_policy(cdip, lbp);
1284 		}
1285 pkt_cleanup:
1286 		if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) {
1287 			scsi_destroy_pkt(vpkt->vpkt_hba_pkt);
1288 			vpkt->vpkt_hba_pkt = NULL;
1289 			if (vpkt->vpkt_path) {
1290 				mdi_rele_path(vpkt->vpkt_path);
1291 				vpkt->vpkt_path = NULL;
1292 			}
1293 		}
1294 		if ((pkt->pkt_cdbp[0] == SCMD_PROUT) &&
1295 		    (((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) ||
1296 		    ((pkt->pkt_cdbp[1] & 0x1f) ==
1297 		    VHCI_PROUT_R_AND_IGNORE))) {
1298 			sema_v(&vlun->svl_pgr_sema);
1299 		}
1300 		return (TRAN_BUSY);
1301 	}
1302 
1303 	VHCI_INCR_PATH_CMDCOUNT(svp);
1304 
1305 	/*
1306 	 * Ensure that no other IOs raced ahead, while a RESERVE cmd was
1307 	 * QUIESCING the same lun.
1308 	 */
1309 	if ((!pkt_reserve_cmd) &&
1310 	    ((vlun->svl_flags & VLUN_QUIESCED_FLG) == VLUN_QUIESCED_FLG)) {
1311 		VHCI_DECR_PATH_CMDCOUNT(svp);
1312 		goto pkt_cleanup;
1313 	}
1314 
1315 	if ((pkt->pkt_cdbp[0] == SCMD_PRIN) ||
1316 	    (pkt->pkt_cdbp[0] == SCMD_PROUT)) {
1317 		/*
1318 		 * currently this thread only handles running PGR
1319 		 * commands, so don't bother creating it unless
1320 		 * something interesting is going to happen (like
1321 		 * either a PGR out, or a PGR in with enough space
1322 		 * to hold the keys that are getting returned)
1323 		 */
1324 		mutex_enter(&vlun->svl_mutex);
1325 		if (((vlun->svl_flags & VLUN_TASK_D_ALIVE_FLG) == 0) &&
1326 		    (pkt->pkt_cdbp[0] == SCMD_PROUT)) {
1327 			vlun->svl_taskq = taskq_create("vlun_pgr_task_daemon",
1328 			    1, MINCLSYSPRI, 1, 4, 0);
1329 			vlun->svl_flags |= VLUN_TASK_D_ALIVE_FLG;
1330 		}
1331 		mutex_exit(&vlun->svl_mutex);
1332 		if ((pkt->pkt_cdbp[0] == SCMD_PROUT) &&
1333 		    (((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) ||
1334 		    ((pkt->pkt_cdbp[1] & 0x1f) ==
1335 		    VHCI_PROUT_R_AND_IGNORE))) {
1336 			if (rval = vhci_pgr_register_start(vlun, pkt)) {
1337 				/* an error */
1338 				sema_v(&vlun->svl_pgr_sema);
1339 				return (rval);
1340 			}
1341 		}
1342 	}
1343 
1344 	/*
1345 	 * SCSI-II RESERVE cmd is not expected in polled mode.
1346 	 * If this changes it needs to be handled for the polled scenario.
1347 	 */
1348 	flags = vpkt->vpkt_hba_pkt->pkt_flags;
1349 	rval = scsi_transport(vpkt->vpkt_hba_pkt);
1350 	if (rval == TRAN_ACCEPT) {
1351 		if (flags & FLAG_NOINTR) {
1352 			struct scsi_pkt *tpkt = vpkt->vpkt_tgt_pkt;
1353 			struct scsi_pkt *pkt = vpkt->vpkt_hba_pkt;
1354 
1355 			ASSERT(tpkt != NULL);
1356 			*(tpkt->pkt_scbp) = *(pkt->pkt_scbp);
1357 			tpkt->pkt_resid = pkt->pkt_resid;
1358 			tpkt->pkt_state = pkt->pkt_state;
1359 			tpkt->pkt_statistics = pkt->pkt_statistics;
1360 			tpkt->pkt_reason = pkt->pkt_reason;
1361 
1362 			if ((*(pkt->pkt_scbp) == STATUS_CHECK) &&
1363 			    (pkt->pkt_state & STATE_ARQ_DONE)) {
1364 				bcopy(pkt->pkt_scbp, tpkt->pkt_scbp,
1365 				    vpkt->vpkt_tgt_init_scblen);
1366 			}
1367 
1368 			VHCI_DECR_PATH_CMDCOUNT(svp);
1369 			if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) {
1370 				scsi_destroy_pkt(vpkt->vpkt_hba_pkt);
1371 				vpkt->vpkt_hba_pkt = NULL;
1372 				if (vpkt->vpkt_path) {
1373 					mdi_rele_path(vpkt->vpkt_path);
1374 					vpkt->vpkt_path = NULL;
1375 				}
1376 			}
1377 			/*
1378 			 * This path will not automatically retry pkts
1379 			 * internally, therefore, vpkt_org_vpkt should
1380 			 * never be set.
1381 			 */
1382 			ASSERT(vpkt->vpkt_org_vpkt == NULL);
1383 			if (tpkt->pkt_comp) {
1384 				(*tpkt->pkt_comp)(tpkt);
1385 			}
1386 		}
1387 		return (rval);
1388 	} else if ((pkt->pkt_cdbp[0] == SCMD_PROUT) &&
1389 	    (((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) ||
1390 	    ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_R_AND_IGNORE))) {
1391 		/* the command exited with bad status */
1392 		sema_v(&vlun->svl_pgr_sema);
1393 	} else if (vpkt->vpkt_tgt_pkt->pkt_cdbp[0] == SCMD_PRIN) {
1394 		/* the command exited with bad status */
1395 		sema_v(&vlun->svl_pgr_sema);
1396 	} else if (pkt_reserve_cmd) {
1397 		VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip,
1398 		    "!vhci_scsi_start: reserve failed vlun 0x%p",
1399 		    (void *)vlun));
1400 		vlun->svl_flags &= ~VLUN_QUIESCED_FLG;
1401 		if (restore_lbp)
1402 			(void) mdi_set_lb_policy(cdip, lbp);
1403 	}
1404 
1405 	ASSERT(vpkt->vpkt_hba_pkt != NULL);
1406 	VHCI_DECR_PATH_CMDCOUNT(svp);
1407 
1408 	/* Do not destroy phci packet information for PKT_DMA_PARTIAL */
1409 	if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) {
1410 		scsi_destroy_pkt(vpkt->vpkt_hba_pkt);
1411 		vpkt->vpkt_hba_pkt = NULL;
1412 		if (vpkt->vpkt_path) {
1413 			MDI_PI_ERRSTAT(vpkt->vpkt_path, MDI_PI_TRANSERR);
1414 			mdi_rele_path(vpkt->vpkt_path);
1415 			vpkt->vpkt_path = NULL;
1416 		}
1417 	}
1418 	return (TRAN_BUSY);
1419 }
1420 
1421 /*
1422  * Function name : vhci_scsi_reset()
1423  *
1424  * Return Values : 0 - reset failed
1425  *		   1 - reset succeeded
1426  */
1427 
1428 /* ARGSUSED */
1429 static int
1430 vhci_scsi_reset(struct scsi_address *ap, int level)
1431 {
1432 	int rval = 0;
1433 
1434 	cmn_err(CE_WARN, "!vhci_scsi_reset 0x%x", level);
1435 	if ((level == RESET_TARGET) || (level == RESET_LUN)) {
1436 		return (vhci_scsi_reset_target(ap, level, TRUE));
1437 	} else if (level == RESET_ALL) {
1438 		return (vhci_scsi_reset_bus(ap));
1439 	}
1440 
1441 	return (rval);
1442 }
1443 
1444 /*
1445  * vhci_recovery_reset:
1446  *	Issues reset to the device
1447  * Input:
1448  *	vlun - vhci lun pointer of the device
1449  *	ap - address of the device
1450  *	select_path:
1451  *		If select_path is FALSE, then the address specified in ap is
1452  *		the path on which reset will be issued.
1453  *		If select_path is TRUE, then path is obtained by calling
1454  *		mdi_select_path.
1455  *
1456  *	recovery_depth:
1457  *		Caller can specify the level of reset.
1458  *		VHCI_DEPTH_LUN -
1459  *			Issues LUN RESET if device supports lun reset.
1460  *		VHCI_DEPTH_TARGET -
1461  *			If Lun Reset fails or the device does not support
1462  *			Lun Reset, issues TARGET RESET
1463  *		VHCI_DEPTH_ALL -
1464  *			If Lun Reset fails or the device does not support
1465  *			Lun Reset, issues TARGET RESET.
1466  *			If TARGET RESET does not succeed, issues Bus Reset.
1467  */
1468 
1469 static int
1470 vhci_recovery_reset(scsi_vhci_lun_t *vlun, struct scsi_address *ap,
1471 	uint8_t select_path, uint8_t recovery_depth)
1472 {
1473 	int	ret = 0;
1474 
1475 	ASSERT(ap != NULL);
1476 
1477 	if (vlun && vlun->svl_support_lun_reset == 1) {
1478 		ret = vhci_scsi_reset_target(ap, RESET_LUN,
1479 		    select_path);
1480 	}
1481 
1482 	recovery_depth--;
1483 
1484 	if ((ret == 0) && recovery_depth) {
1485 		ret = vhci_scsi_reset_target(ap, RESET_TARGET,
1486 		    select_path);
1487 		recovery_depth--;
1488 	}
1489 
1490 	if ((ret == 0) && recovery_depth) {
1491 		(void) scsi_reset(ap, RESET_ALL);
1492 	}
1493 
1494 	return (ret);
1495 }
1496 
1497 /*
1498  * Note: The scsi_address passed to this routine could be the scsi_address
1499  * for the virtual device or the physical device. No assumptions should be
1500  * made in this routine about the ap structure and a_hba_tran->tran_tgt_private
1501  * field of ap can not be assumed to be the vhci structure.
1502  * Further note that the child dip would be the dip of the ssd node irrespective
1503  * of the scsi_address passed.
1504  */
1505 
1506 static int
1507 vhci_scsi_reset_target(struct scsi_address *ap, int level, uint8_t select_path)
1508 {
1509 	dev_info_t		*vdip, *pdip, *cdip = ADDR2DIP(ap);
1510 	mdi_pathinfo_t		*pip = NULL;
1511 	mdi_pathinfo_t		*npip = NULL;
1512 	int			rval = -1;
1513 	scsi_vhci_priv_t	*svp = NULL;
1514 	struct scsi_address	*pap = NULL;
1515 	scsi_hba_tran_t		*hba = NULL;
1516 	int			sps;
1517 	struct scsi_vhci	*vhci = NULL;
1518 
1519 	if (select_path != TRUE) {
1520 		ASSERT(ap != NULL);
1521 		if (level == RESET_LUN) {
1522 			hba = ap->a_hba_tran;
1523 			ASSERT(hba != NULL);
1524 			return ((*hba->tran_reset)(ap, RESET_LUN));
1525 		}
1526 		return (scsi_reset(ap, level));
1527 	}
1528 
1529 	ASSERT(cdip != NULL);
1530 	vdip = ddi_get_parent(cdip);
1531 	ASSERT(vdip != NULL);
1532 	vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip));
1533 	ASSERT(vhci != NULL);
1534 
1535 	rval = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH, NULL, &pip);
1536 	if ((rval != MDI_SUCCESS) || (pip == NULL)) {
1537 		VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_scsi_reset_target: "
1538 		    "Unable to get a path, dip 0x%p", (void *)cdip));
1539 		return (0);
1540 	}
1541 again:
1542 	svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
1543 	if (svp == NULL) {
1544 		VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_scsi_reset_target: "
1545 		    "priv is NULL, pip 0x%p", (void *)pip));
1546 		mdi_rele_path(pip);
1547 		return (0);
1548 	}
1549 
1550 	if (svp->svp_psd == NULL) {
1551 		VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_scsi_reset_target: "
1552 		    "psd is NULL, pip 0x%p, svp 0x%p",
1553 		    (void *)pip, (void *)svp));
1554 		mdi_rele_path(pip);
1555 		return (0);
1556 	}
1557 
1558 	pap = &svp->svp_psd->sd_address;
1559 	hba = pap->a_hba_tran;
1560 
1561 	ASSERT(pap != NULL);
1562 	ASSERT(hba != NULL);
1563 
1564 	if (hba->tran_reset != NULL) {
1565 		if ((*hba->tran_reset)(pap, level) == 0) {
1566 			pdip = mdi_pi_get_phci(pip);
1567 			vhci_log(CE_WARN, vdip, "!(%s%d):"
1568 			    " path (%s%d), reset %d failed",
1569 			    ddi_driver_name(cdip), ddi_get_instance(cdip),
1570 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
1571 			    level);
1572 
1573 			/*
1574 			 * Select next path and issue the reset, repeat
1575 			 * until all paths are exhausted
1576 			 */
1577 			sps = mdi_select_path(cdip, NULL,
1578 			    MDI_SELECT_ONLINE_PATH, pip, &npip);
1579 			if ((sps != MDI_SUCCESS) || (npip == NULL)) {
1580 				mdi_rele_path(pip);
1581 				return (0);
1582 			}
1583 			mdi_rele_path(pip);
1584 			pip = npip;
1585 			goto again;
1586 		}
1587 		mdi_rele_path(pip);
1588 		mutex_enter(&vhci->vhci_mutex);
1589 		scsi_hba_reset_notify_callback(&vhci->vhci_mutex,
1590 		    &vhci->vhci_reset_notify_listf);
1591 		mutex_exit(&vhci->vhci_mutex);
1592 		VHCI_DEBUG(6, (CE_NOTE, NULL, "!vhci_scsi_reset_target: "
1593 		    "reset %d sent down pip:%p for cdip:%p\n", level,
1594 		    (void *)pip, (void *)cdip));
1595 		return (1);
1596 	}
1597 	mdi_rele_path(pip);
1598 	return (0);
1599 }
1600 
1601 
1602 /* ARGSUSED */
1603 static int
1604 vhci_scsi_reset_bus(struct scsi_address *ap)
1605 {
1606 	return (1);
1607 }
1608 
1609 
1610 /*
1611  * called by vhci_getcap and vhci_setcap to get and set (respectively)
1612  * SCSI capabilities
1613  */
1614 /* ARGSUSED */
1615 static int
1616 vhci_commoncap(struct scsi_address *ap, char *cap,
1617     int val, int tgtonly, int doset)
1618 {
1619 	struct scsi_vhci		*vhci = ADDR2VHCI(ap);
1620 	struct scsi_vhci_lun		*vlun = ADDR2VLUN(ap);
1621 	int			cidx;
1622 	int			rval = 0;
1623 
1624 	if (cap == (char *)0) {
1625 		VHCI_DEBUG(3, (CE_WARN, vhci->vhci_dip,
1626 		    "!vhci_commoncap: invalid arg"));
1627 		return (rval);
1628 	}
1629 
1630 	if (vlun == NULL) {
1631 		VHCI_DEBUG(3, (CE_WARN, vhci->vhci_dip,
1632 		    "!vhci_commoncap: vlun is null"));
1633 		return (rval);
1634 	}
1635 
1636 	if ((cidx = scsi_hba_lookup_capstr(cap)) == -1) {
1637 		return (UNDEFINED);
1638 	}
1639 
1640 	/*
1641 	 * Process setcap request.
1642 	 */
1643 	if (doset) {
1644 		/*
1645 		 * At present, we can only set binary (0/1) values
1646 		 */
1647 		switch (cidx) {
1648 		case SCSI_CAP_ARQ:
1649 			if (val == 0) {
1650 				rval = 0;
1651 			} else {
1652 				rval = 1;
1653 			}
1654 			break;
1655 
1656 		case SCSI_CAP_LUN_RESET:
1657 			if (tgtonly == 0) {
1658 				VHCI_DEBUG(1, (CE_WARN, vhci->vhci_dip,
1659 				    "scsi_vhci_setcap: "
1660 				    "Returning error since whom = 0"));
1661 				rval = -1;
1662 				break;
1663 			}
1664 			/*
1665 			 * Set the capability accordingly.
1666 			 */
1667 			mutex_enter(&vlun->svl_mutex);
1668 			vlun->svl_support_lun_reset = val;
1669 			rval = val;
1670 			mutex_exit(&vlun->svl_mutex);
1671 			break;
1672 
1673 		case SCSI_CAP_SECTOR_SIZE:
1674 			mutex_enter(&vlun->svl_mutex);
1675 			vlun->svl_sector_size = val;
1676 			vlun->svl_setcap_done = 1;
1677 			mutex_exit(&vlun->svl_mutex);
1678 			(void) vhci_pHCI_cap(ap, cap, val, tgtonly, NULL);
1679 
1680 			/* Always return success */
1681 			rval = 1;
1682 			break;
1683 
1684 		default:
1685 			VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip,
1686 			    "!vhci_setcap: unsupported %d", cidx));
1687 			rval = UNDEFINED;
1688 			break;
1689 		}
1690 
1691 		VHCI_DEBUG(6, (CE_NOTE, vhci->vhci_dip,
1692 		    "!set cap: cap=%s, val/tgtonly/doset/rval = "
1693 		    "0x%x/0x%x/0x%x/%d\n",
1694 		    cap, val, tgtonly, doset, rval));
1695 
1696 	} else {
1697 		/*
1698 		 * Process getcap request.
1699 		 */
1700 		switch (cidx) {
1701 		case SCSI_CAP_DMA_MAX:
1702 			rval = (int)VHCI_DMA_MAX_XFER_CAP;
1703 			break;
1704 
1705 		case SCSI_CAP_INITIATOR_ID:
1706 			rval = 0x00;
1707 			break;
1708 
1709 		case SCSI_CAP_ARQ:
1710 		case SCSI_CAP_RESET_NOTIFICATION:
1711 		case SCSI_CAP_TAGGED_QING:
1712 			rval = 1;
1713 			break;
1714 
1715 		case SCSI_CAP_SCSI_VERSION:
1716 			rval = 3;
1717 			break;
1718 
1719 		case SCSI_CAP_INTERCONNECT_TYPE:
1720 			rval = INTERCONNECT_FABRIC;
1721 			break;
1722 
1723 		case SCSI_CAP_LUN_RESET:
1724 			/*
1725 			 * scsi_vhci will always return success for LUN reset.
1726 			 * When request for doing LUN reset comes
1727 			 * through scsi_reset entry point, at that time attempt
1728 			 * will be made to do reset through all the possible
1729 			 * paths.
1730 			 */
1731 			mutex_enter(&vlun->svl_mutex);
1732 			rval = vlun->svl_support_lun_reset;
1733 			mutex_exit(&vlun->svl_mutex);
1734 			VHCI_DEBUG(4, (CE_WARN, vhci->vhci_dip,
1735 			    "scsi_vhci_getcap:"
1736 			    "Getting the Lun reset capability %d", rval));
1737 			break;
1738 
1739 		case SCSI_CAP_SECTOR_SIZE:
1740 			mutex_enter(&vlun->svl_mutex);
1741 			rval = vlun->svl_sector_size;
1742 			mutex_exit(&vlun->svl_mutex);
1743 			break;
1744 
1745 		default:
1746 			VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip,
1747 			    "!vhci_getcap: unsupported %d", cidx));
1748 			rval = UNDEFINED;
1749 			break;
1750 		}
1751 
1752 		VHCI_DEBUG(6, (CE_NOTE, vhci->vhci_dip,
1753 		    "!get cap: cap=%s, val/tgtonly/doset/rval = "
1754 		    "0x%x/0x%x/0x%x/%d\n",
1755 		    cap, val, tgtonly, doset, rval));
1756 	}
1757 	return (rval);
1758 }
1759 
1760 
1761 /*
1762  * Function name : vhci_scsi_getcap()
1763  *
1764  */
1765 static int
1766 vhci_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
1767 {
1768 	return (vhci_commoncap(ap, cap, 0, whom, 0));
1769 }
1770 
1771 static int
1772 vhci_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
1773 {
1774 	return (vhci_commoncap(ap, cap, value, whom, 1));
1775 }
1776 
1777 /*
1778  * Function name : vhci_scsi_abort()
1779  */
1780 /* ARGSUSED */
1781 static int
1782 vhci_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
1783 {
1784 	return (0);
1785 }
1786 
1787 /*
1788  * Function name : vhci_scsi_init_pkt
1789  *
1790  * Return Values : pointer to scsi_pkt, or NULL
1791  */
1792 /* ARGSUSED */
1793 static struct scsi_pkt *
1794 vhci_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
1795 	struct buf *bp, int cmdlen, int statuslen, int tgtlen,
1796 	int flags, int (*callback)(caddr_t), caddr_t arg)
1797 {
1798 	struct scsi_vhci	*vhci = ADDR2VHCI(ap);
1799 	struct vhci_pkt		*vpkt;
1800 	int			rval;
1801 	int			newpkt = 0;
1802 	struct scsi_pkt		*pktp;
1803 
1804 
1805 	if (pkt == NULL) {
1806 		if (cmdlen > VHCI_SCSI_CDB_SIZE) {
1807 			VHCI_DEBUG(1, (CE_NOTE, NULL,
1808 			    "!init pkt: cdb size not supported\n"));
1809 			return (NULL);
1810 		}
1811 
1812 		pktp = scsi_hba_pkt_alloc(vhci->vhci_dip,
1813 		    ap, cmdlen, statuslen, tgtlen, sizeof (*vpkt), callback,
1814 		    arg);
1815 
1816 		if (pktp == NULL) {
1817 			return (NULL);
1818 		}
1819 
1820 		/* Get the vhci's private structure */
1821 		vpkt = (struct vhci_pkt *)(pktp->pkt_ha_private);
1822 		ASSERT(vpkt);
1823 
1824 		/* Save the target driver's packet */
1825 		vpkt->vpkt_tgt_pkt = pktp;
1826 
1827 		/*
1828 		 * Save pkt_tgt_init_pkt fields if deferred binding
1829 		 * is needed or for other purposes.
1830 		 */
1831 		vpkt->vpkt_tgt_init_pkt_flags = flags;
1832 		vpkt->vpkt_flags = (callback == NULL_FUNC) ? CFLAG_NOWAIT : 0;
1833 		vpkt->vpkt_state = VHCI_PKT_IDLE;
1834 		vpkt->vpkt_tgt_init_cdblen = cmdlen;
1835 		vpkt->vpkt_tgt_init_scblen = statuslen;
1836 		vpkt->vpkt_tgt_init_privlen = tgtlen;
1837 		newpkt = 1;
1838 	} else { /* pkt not NULL */
1839 		vpkt = pkt->pkt_ha_private;
1840 	}
1841 
1842 	VHCI_DEBUG(8, (CE_NOTE, NULL, "vhci_scsi_init_pkt "
1843 	    "vpkt %p flags %x\n", (void *)vpkt, flags));
1844 
1845 	/* Clear any stale error flags */
1846 	if (bp) {
1847 		bioerror(bp, 0);
1848 	}
1849 
1850 	vpkt->vpkt_tgt_init_bp = bp;
1851 
1852 	if (flags & PKT_DMA_PARTIAL) {
1853 
1854 		/*
1855 		 * Immediate binding is needed.
1856 		 * Target driver may not set this flag in next invocation.
1857 		 * vhci has to remember this flag was set during first
1858 		 * invocation of vhci_scsi_init_pkt.
1859 		 */
1860 		vpkt->vpkt_flags |= CFLAG_DMA_PARTIAL;
1861 	}
1862 
1863 	if (vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) {
1864 
1865 		/*
1866 		 * Re-initialize some of the target driver packet state
1867 		 * information.
1868 		 */
1869 		vpkt->vpkt_tgt_pkt->pkt_state = 0;
1870 		vpkt->vpkt_tgt_pkt->pkt_statistics = 0;
1871 		vpkt->vpkt_tgt_pkt->pkt_reason = 0;
1872 
1873 		/*
1874 		 * Binding a vpkt->vpkt_path for this IO at init_time.
1875 		 * If an IO error happens later, target driver will clear
1876 		 * this vpkt->vpkt_path binding before re-init IO again.
1877 		 */
1878 		VHCI_DEBUG(8, (CE_NOTE, NULL,
1879 		    "vhci_scsi_init_pkt: calling v_b_t %p, newpkt %d\n",
1880 		    (void *)vpkt, newpkt));
1881 		if (pkt && vpkt->vpkt_hba_pkt) {
1882 			VHCI_DEBUG(4, (CE_NOTE, NULL,
1883 			    "v_s_i_p calling update_pHCI_pkt resid %ld\n",
1884 			    pkt->pkt_resid));
1885 			vhci_update_pHCI_pkt(vpkt, pkt);
1886 		}
1887 		if (callback == SLEEP_FUNC) {
1888 			rval = vhci_bind_transport(
1889 			    ap, vpkt, flags, callback);
1890 		} else {
1891 			rval = vhci_bind_transport(
1892 			    ap, vpkt, flags, NULL_FUNC);
1893 		}
1894 		VHCI_DEBUG(8, (CE_NOTE, NULL,
1895 		    "vhci_scsi_init_pkt: v_b_t called 0x%p rval 0x%x\n",
1896 		    (void *)vpkt, rval));
1897 		if (bp) {
1898 			if (rval == TRAN_FATAL_ERROR) {
1899 				/*
1900 				 * No paths available. Could not bind
1901 				 * any pHCI. Setting EFAULT as a way
1902 				 * to indicate no DMA is mapped.
1903 				 */
1904 				bioerror(bp, EFAULT);
1905 			} else {
1906 				/*
1907 				 * Do not indicate any pHCI errors to
1908 				 * target driver otherwise.
1909 				 */
1910 				bioerror(bp, 0);
1911 			}
1912 		}
1913 		if (rval != TRAN_ACCEPT) {
1914 			VHCI_DEBUG(8, (CE_NOTE, NULL,
1915 			    "vhci_scsi_init_pkt: "
1916 			    "v_b_t failed 0x%p newpkt %x\n",
1917 			    (void *)vpkt, newpkt));
1918 			if (newpkt) {
1919 				scsi_hba_pkt_free(ap,
1920 				    vpkt->vpkt_tgt_pkt);
1921 			}
1922 			return (NULL);
1923 		}
1924 		ASSERT(vpkt->vpkt_hba_pkt != NULL);
1925 		ASSERT(vpkt->vpkt_path != NULL);
1926 
1927 		/* Update the resid for the target driver */
1928 		vpkt->vpkt_tgt_pkt->pkt_resid =
1929 		    vpkt->vpkt_hba_pkt->pkt_resid;
1930 	}
1931 
1932 	return (vpkt->vpkt_tgt_pkt);
1933 }
1934 
1935 /*
1936  * Function name : vhci_scsi_destroy_pkt
1937  *
1938  * Return Values : none
1939  */
1940 static void
1941 vhci_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
1942 {
1943 	struct vhci_pkt		*vpkt = (struct vhci_pkt *)pkt->pkt_ha_private;
1944 
1945 	VHCI_DEBUG(8, (CE_NOTE, NULL,
1946 	    "vhci_scsi_destroy_pkt: vpkt 0x%p\n", (void *)vpkt));
1947 
1948 	vpkt->vpkt_tgt_init_pkt_flags = 0;
1949 	if (vpkt->vpkt_hba_pkt) {
1950 		scsi_destroy_pkt(vpkt->vpkt_hba_pkt);
1951 		vpkt->vpkt_hba_pkt = NULL;
1952 	}
1953 	if (vpkt->vpkt_path) {
1954 		mdi_rele_path(vpkt->vpkt_path);
1955 		vpkt->vpkt_path = NULL;
1956 	}
1957 
1958 	ASSERT(vpkt->vpkt_state != VHCI_PKT_ISSUED);
1959 	scsi_hba_pkt_free(ap, vpkt->vpkt_tgt_pkt);
1960 }
1961 
1962 /*
1963  * Function name : vhci_scsi_dmafree()
1964  *
1965  * Return Values : none
1966  */
1967 /*ARGSUSED*/
1968 static void
1969 vhci_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
1970 {
1971 	struct vhci_pkt	*vpkt = (struct vhci_pkt *)pkt->pkt_ha_private;
1972 
1973 	VHCI_DEBUG(6, (CE_NOTE, NULL,
1974 	    "vhci_scsi_dmafree: vpkt 0x%p\n", (void *)vpkt));
1975 
1976 	ASSERT(vpkt != NULL);
1977 	if (vpkt->vpkt_hba_pkt) {
1978 		scsi_destroy_pkt(vpkt->vpkt_hba_pkt);
1979 		vpkt->vpkt_hba_pkt = NULL;
1980 	}
1981 	if (vpkt->vpkt_path) {
1982 		mdi_rele_path(vpkt->vpkt_path);
1983 		vpkt->vpkt_path = NULL;
1984 	}
1985 }
1986 
1987 /*
1988  * Function name : vhci_scsi_sync_pkt()
1989  *
1990  * Return Values : none
1991  */
1992 /*ARGSUSED*/
1993 static void
1994 vhci_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
1995 {
1996 	struct vhci_pkt	*vpkt = (struct vhci_pkt *)pkt->pkt_ha_private;
1997 
1998 	ASSERT(vpkt != NULL);
1999 	if (vpkt->vpkt_hba_pkt) {
2000 		scsi_sync_pkt(vpkt->vpkt_hba_pkt);
2001 	}
2002 }
2003 
2004 /*
2005  * routine for reset notification setup, to register or cancel.
2006  */
2007 static int
2008 vhci_scsi_reset_notify(struct scsi_address *ap, int flag,
2009 	void (*callback)(caddr_t), caddr_t arg)
2010 {
2011 	struct scsi_vhci *vhci = ADDR2VHCI(ap);
2012 	return (scsi_hba_reset_notify_setup(ap, flag, callback, arg,
2013 	    &vhci->vhci_mutex, &vhci->vhci_reset_notify_listf));
2014 }
2015 
2016 static int
2017 vhci_scsi_get_name_bus_addr(struct scsi_device *sd,
2018     char *name, int len, int bus_addr)
2019 {
2020 	dev_info_t		*cdip;
2021 	char			*guid;
2022 	scsi_vhci_lun_t		*vlun;
2023 
2024 	ASSERT(sd != NULL);
2025 	ASSERT(name != NULL);
2026 
2027 	cdip = sd->sd_dev;
2028 
2029 	ASSERT(cdip != NULL);
2030 
2031 	if (mdi_component_is_client(cdip, NULL) != MDI_SUCCESS) {
2032 		name[0] = '\0';
2033 		return (1);
2034 	}
2035 
2036 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, cdip, PROPFLAGS,
2037 	    MDI_CLIENT_GUID_PROP, &guid) != DDI_SUCCESS) {
2038 		name[0] = '\0';
2039 		return (1);
2040 	}
2041 
2042 	vlun = ADDR2VLUN(&sd->sd_address);
2043 	if (bus_addr && vlun && vlun->svl_fops_name) {
2044 		/* report the guid and the name of the failover module */
2045 		(void) snprintf(name, len, "g%s %s", guid, vlun->svl_fops_name);
2046 	} else {
2047 		/* report the guid */
2048 		(void) snprintf(name, len, "g%s", guid);
2049 	}
2050 
2051 	ddi_prop_free(guid);
2052 	return (1);
2053 }
2054 
2055 static int
2056 vhci_scsi_get_bus_addr(struct scsi_device *sd, char *name, int len)
2057 {
2058 	return (vhci_scsi_get_name_bus_addr(sd, name, len, 1));
2059 }
2060 
2061 static int
2062 vhci_scsi_get_name(struct scsi_device *sd, char *name, int len)
2063 {
2064 	return (vhci_scsi_get_name_bus_addr(sd, name, len, 0));
2065 }
2066 
2067 /*
2068  * Return a pointer to the guid part of the devnm.
2069  * devnm format is "nodename@busaddr", busaddr format is "gGUID".
2070  */
2071 static char *
2072 vhci_devnm_to_guid(char *devnm)
2073 {
2074 	char *cp = devnm;
2075 
2076 	if (devnm == NULL)
2077 		return (NULL);
2078 
2079 	while (*cp != '\0' && *cp != '@')
2080 		cp++;
2081 	if (*cp == '@' && *(cp + 1) == 'g')
2082 		return (cp + 2);
2083 	return (NULL);
2084 }
2085 
2086 static int
2087 vhci_bind_transport(struct scsi_address *ap, struct vhci_pkt *vpkt, int flags,
2088     int (*func)(caddr_t))
2089 {
2090 	struct scsi_vhci	*vhci = ADDR2VHCI(ap);
2091 	dev_info_t		*cdip = ADDR2DIP(ap);
2092 	mdi_pathinfo_t		*pip = NULL;
2093 	mdi_pathinfo_t		*npip = NULL;
2094 	scsi_vhci_priv_t	*svp = NULL;
2095 	struct scsi_device	*psd = NULL;
2096 	struct scsi_address	*address = NULL;
2097 	struct scsi_pkt		*pkt = NULL;
2098 	int			rval = -1;
2099 	int			pgr_sema_held = 0;
2100 	int			held;
2101 	int			mps_flag = MDI_SELECT_ONLINE_PATH;
2102 	struct scsi_vhci_lun	*vlun;
2103 	time_t			tnow;
2104 
2105 	vlun = ADDR2VLUN(ap);
2106 	ASSERT(vlun != 0);
2107 
2108 	if ((vpkt->vpkt_tgt_pkt->pkt_cdbp[0] == SCMD_PROUT) &&
2109 	    (((vpkt->vpkt_tgt_pkt->pkt_cdbp[1] & 0x1f) ==
2110 	    VHCI_PROUT_REGISTER) ||
2111 	    ((vpkt->vpkt_tgt_pkt->pkt_cdbp[1] & 0x1f) ==
2112 	    VHCI_PROUT_R_AND_IGNORE))) {
2113 		if (!sema_tryp(&vlun->svl_pgr_sema))
2114 			return (TRAN_BUSY);
2115 		pgr_sema_held = 1;
2116 		if (vlun->svl_first_path != NULL) {
2117 			rval = mdi_select_path(cdip, NULL,
2118 			    MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH,
2119 			    NULL, &pip);
2120 			if ((rval != MDI_SUCCESS) || (pip == NULL)) {
2121 				VHCI_DEBUG(4, (CE_NOTE, NULL,
2122 				    "vhci_bind_transport: path select fail\n"));
2123 			} else {
2124 				npip = pip;
2125 				do {
2126 					if (npip == vlun->svl_first_path) {
2127 						VHCI_DEBUG(4, (CE_NOTE, NULL,
2128 						    "vhci_bind_transport: "
2129 						    "valid first path 0x%p\n",
2130 						    (void *)
2131 						    vlun->svl_first_path));
2132 						pip = vlun->svl_first_path;
2133 						goto bind_path;
2134 					}
2135 					pip = npip;
2136 					rval = mdi_select_path(cdip, NULL,
2137 					    MDI_SELECT_ONLINE_PATH |
2138 					    MDI_SELECT_STANDBY_PATH,
2139 					    pip, &npip);
2140 					mdi_rele_path(pip);
2141 				} while ((rval == MDI_SUCCESS) &&
2142 				    (npip != NULL));
2143 			}
2144 		}
2145 
2146 		if (vlun->svl_first_path) {
2147 			VHCI_DEBUG(4, (CE_NOTE, NULL,
2148 			    "vhci_bind_transport: invalid first path 0x%p\n",
2149 			    (void *)vlun->svl_first_path));
2150 			vlun->svl_first_path = NULL;
2151 		}
2152 	} else if (vpkt->vpkt_tgt_pkt->pkt_cdbp[0] == SCMD_PRIN) {
2153 		if ((vpkt->vpkt_state & VHCI_PKT_THRU_TASKQ) == 0) {
2154 			if (!sema_tryp(&vlun->svl_pgr_sema))
2155 				return (TRAN_BUSY);
2156 		}
2157 		pgr_sema_held = 1;
2158 	}
2159 
2160 	/*
2161 	 * If the path is already bound for PKT_PARTIAL_DMA case,
2162 	 * try to use the same path.
2163 	 */
2164 	if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) && vpkt->vpkt_path) {
2165 		VHCI_DEBUG(4, (CE_NOTE, NULL,
2166 		    "vhci_bind_transport: PKT_PARTIAL_DMA "
2167 		    "vpkt 0x%p, path 0x%p\n",
2168 		    (void *)vpkt, (void *)vpkt->vpkt_path));
2169 		pip = vpkt->vpkt_path;
2170 		goto bind_path;
2171 	}
2172 
2173 	/*
2174 	 * If reservation is active bind the transport directly to the pip
2175 	 * with the reservation.
2176 	 */
2177 	if (vpkt->vpkt_hba_pkt == NULL) {
2178 		if (vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) {
2179 			if (MDI_PI_IS_ONLINE(vlun->svl_resrv_pip)) {
2180 				pip = vlun->svl_resrv_pip;
2181 				mdi_hold_path(pip);
2182 				vlun->svl_waiting_for_activepath = 0;
2183 				rval = MDI_SUCCESS;
2184 				goto bind_path;
2185 			} else {
2186 				if (pgr_sema_held) {
2187 					sema_v(&vlun->svl_pgr_sema);
2188 				}
2189 				return (TRAN_BUSY);
2190 			}
2191 		}
2192 try_again:
2193 		rval = mdi_select_path(cdip, vpkt->vpkt_tgt_init_bp, 0, NULL,
2194 		    &pip);
2195 		if (rval == MDI_BUSY) {
2196 			if (pgr_sema_held) {
2197 				sema_v(&vlun->svl_pgr_sema);
2198 			}
2199 			return (TRAN_BUSY);
2200 		} else if (rval == MDI_DEVI_ONLINING) {
2201 			/*
2202 			 * if we are here then we are in the midst of
2203 			 * an attach/probe of the client device.
2204 			 * We attempt to bind to ONLINE path if available,
2205 			 * else it is OK to bind to a STANDBY path (instead
2206 			 * of triggering a failover) because IO associated
2207 			 * with attach/probe (eg. INQUIRY, block 0 read)
2208 			 * are completed by targets even on passive paths
2209 			 * If no ONLINE paths available, it is important
2210 			 * to set svl_waiting_for_activepath for two
2211 			 * reasons: (1) avoid sense analysis in the
2212 			 * "external failure detection" codepath in
2213 			 * vhci_intr().  Failure to do so will result in
2214 			 * infinite loop (unless an ONLINE path becomes
2215 			 * available at some point) (2) avoid
2216 			 * unnecessary failover (see "---Waiting For Active
2217 			 * Path---" comment below).
2218 			 */
2219 			VHCI_DEBUG(1, (CE_NOTE, NULL, "!%p in onlining "
2220 			    "state\n", (void *)cdip));
2221 			pip = NULL;
2222 			rval = mdi_select_path(cdip, vpkt->vpkt_tgt_init_bp,
2223 			    mps_flag, NULL, &pip);
2224 			if ((rval != MDI_SUCCESS) || (pip == NULL)) {
2225 				if (vlun->svl_waiting_for_activepath == 0) {
2226 					vlun->svl_waiting_for_activepath = 1;
2227 					vlun->svl_wfa_time = ddi_get_time();
2228 				}
2229 				mps_flag |= MDI_SELECT_STANDBY_PATH;
2230 				rval = mdi_select_path(cdip,
2231 				    vpkt->vpkt_tgt_init_bp,
2232 				    mps_flag, NULL, &pip);
2233 				if ((rval != MDI_SUCCESS) || (pip == NULL)) {
2234 					if (pgr_sema_held) {
2235 						sema_v(&vlun->svl_pgr_sema);
2236 					}
2237 					return (TRAN_FATAL_ERROR);
2238 				}
2239 				goto bind_path;
2240 			}
2241 		} else if (rval == MDI_FAILURE) {
2242 			if (pgr_sema_held) {
2243 				sema_v(&vlun->svl_pgr_sema);
2244 			}
2245 			return (TRAN_FATAL_ERROR);
2246 		}
2247 
2248 		if ((pip == NULL) || (rval == MDI_NOPATH)) {
2249 			while (vlun->svl_waiting_for_activepath) {
2250 				/*
2251 				 * ---Waiting For Active Path---
2252 				 * This device was discovered across a
2253 				 * passive path; lets wait for a little
2254 				 * bit, hopefully an active path will
2255 				 * show up obviating the need for a
2256 				 * failover
2257 				 */
2258 				tnow = ddi_get_time();
2259 				if (tnow - vlun->svl_wfa_time >= 60) {
2260 					vlun->svl_waiting_for_activepath = 0;
2261 				} else {
2262 					drv_usecwait(1000);
2263 					if (vlun->svl_waiting_for_activepath
2264 					    == 0) {
2265 						/*
2266 						 * an active path has come
2267 						 * online!
2268 						 */
2269 						goto try_again;
2270 					}
2271 				}
2272 			}
2273 			VHCI_HOLD_LUN(vlun, VH_NOSLEEP, held);
2274 			if (!held) {
2275 				VHCI_DEBUG(4, (CE_NOTE, NULL,
2276 				    "!Lun not held\n"));
2277 				if (pgr_sema_held) {
2278 					sema_v(&vlun->svl_pgr_sema);
2279 				}
2280 				return (TRAN_BUSY);
2281 			}
2282 			/*
2283 			 * now that the LUN is stable, one last check
2284 			 * to make sure no other changes sneaked in
2285 			 * (like a path coming online or a
2286 			 * failover initiated by another thread)
2287 			 */
2288 			pip = NULL;
2289 			rval = mdi_select_path(cdip, vpkt->vpkt_tgt_init_bp,
2290 			    0, NULL, &pip);
2291 			if (pip != NULL) {
2292 				VHCI_RELEASE_LUN(vlun);
2293 				vlun->svl_waiting_for_activepath = 0;
2294 				goto bind_path;
2295 			}
2296 
2297 			/*
2298 			 * Check if there is an ONLINE path OR a STANDBY path
2299 			 * available. If none is available, do not attempt
2300 			 * to do a failover, just return a fatal error at this
2301 			 * point.
2302 			 */
2303 			npip = NULL;
2304 			rval = mdi_select_path(cdip, NULL,
2305 			    (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH),
2306 			    NULL, &npip);
2307 			if ((npip == NULL) || (rval != MDI_SUCCESS)) {
2308 				/*
2309 				 * No paths available, jus return FATAL error.
2310 				 */
2311 				VHCI_RELEASE_LUN(vlun);
2312 				if (pgr_sema_held) {
2313 					sema_v(&vlun->svl_pgr_sema);
2314 				}
2315 				return (TRAN_FATAL_ERROR);
2316 			}
2317 			mdi_rele_path(npip);
2318 			VHCI_DEBUG(1, (CE_NOTE, NULL, "!invoking "
2319 			    "mdi_failover\n"));
2320 			rval = mdi_failover(vhci->vhci_dip, cdip,
2321 			    MDI_FAILOVER_ASYNC);
2322 			if (rval == MDI_FAILURE) {
2323 				VHCI_RELEASE_LUN(vlun);
2324 				if (pgr_sema_held) {
2325 					sema_v(&vlun->svl_pgr_sema);
2326 				}
2327 				return (TRAN_FATAL_ERROR);
2328 			} else if (rval == MDI_BUSY) {
2329 				VHCI_RELEASE_LUN(vlun);
2330 				if (pgr_sema_held) {
2331 					sema_v(&vlun->svl_pgr_sema);
2332 				}
2333 				return (TRAN_BUSY);
2334 			} else {
2335 				if (pgr_sema_held) {
2336 					sema_v(&vlun->svl_pgr_sema);
2337 				}
2338 				return (TRAN_BUSY);
2339 			}
2340 		}
2341 		vlun->svl_waiting_for_activepath = 0;
2342 bind_path:
2343 		vpkt->vpkt_path = pip;
2344 		svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
2345 		ASSERT(svp != NULL);
2346 
2347 		psd = svp->svp_psd;
2348 		ASSERT(psd != NULL);
2349 		address = &psd->sd_address;
2350 	} else {
2351 		pkt = vpkt->vpkt_hba_pkt;
2352 		address = &pkt->pkt_address;
2353 	}
2354 
2355 	/*
2356 	 * For PKT_PARTIAL_DMA case, call pHCI's scsi_init_pkt whenever
2357 	 * target driver calls vhci_scsi_init_pkt.
2358 	 */
2359 	if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) &&
2360 	    vpkt->vpkt_path && vpkt->vpkt_hba_pkt) {
2361 		VHCI_DEBUG(4, (CE_NOTE, NULL,
2362 		    "vhci_bind_transport: PKT_PARTIAL_DMA "
2363 		    "vpkt 0x%p, path 0x%p hba_pkt 0x%p\n",
2364 		    (void *)vpkt, (void *)vpkt->vpkt_path, (void *)pkt));
2365 		pkt = vpkt->vpkt_hba_pkt;
2366 		address = &pkt->pkt_address;
2367 	}
2368 
2369 	if (pkt == NULL || (vpkt->vpkt_flags & CFLAG_DMA_PARTIAL)) {
2370 		pkt = scsi_init_pkt(address, pkt,
2371 		    vpkt->vpkt_tgt_init_bp, vpkt->vpkt_tgt_init_cdblen,
2372 		    vpkt->vpkt_tgt_init_scblen,
2373 		    vpkt->vpkt_tgt_init_privlen, flags, func, NULL);
2374 
2375 		if (pkt == NULL) {
2376 			VHCI_DEBUG(4, (CE_NOTE, NULL,
2377 			    "!bind transport: 0x%p 0x%p 0x%p\n",
2378 			    (void *)vhci, (void *)psd, (void *)vpkt));
2379 			if ((vpkt->vpkt_hba_pkt == NULL) && vpkt->vpkt_path) {
2380 				MDI_PI_ERRSTAT(vpkt->vpkt_path,
2381 				    MDI_PI_TRANSERR);
2382 				mdi_rele_path(vpkt->vpkt_path);
2383 				vpkt->vpkt_path = NULL;
2384 			}
2385 			if (pgr_sema_held) {
2386 				sema_v(&vlun->svl_pgr_sema);
2387 			}
2388 			/*
2389 			 * Looks like a fatal error.
2390 			 * May be device disappeared underneath.
2391 			 * Give another chance to target driver for a retry to
2392 			 * get another path.
2393 			 */
2394 			return (TRAN_BUSY);
2395 		}
2396 	}
2397 
2398 	pkt->pkt_private = vpkt;
2399 	vpkt->vpkt_hba_pkt = pkt;
2400 	return (TRAN_ACCEPT);
2401 }
2402 
2403 
2404 /*PRINTFLIKE3*/
2405 void
2406 vhci_log(int level, dev_info_t *dip, const char *fmt, ...)
2407 {
2408 	char		buf[256];
2409 	va_list		ap;
2410 
2411 	va_start(ap, fmt);
2412 	(void) vsprintf(buf, fmt, ap);
2413 	va_end(ap);
2414 
2415 	scsi_log(dip, "scsi_vhci", level, buf);
2416 }
2417 
2418 /* do a PGR out with the information we've saved away */
2419 static int
2420 vhci_do_prout(scsi_vhci_priv_t *svp)
2421 {
2422 
2423 	struct scsi_pkt			*new_pkt;
2424 	struct buf			*bp;
2425 	scsi_vhci_lun_t			*vlun;
2426 	int				rval, retry, nr_retry, ua_retry;
2427 	struct scsi_extended_sense	*sns;
2428 
2429 	bp = getrbuf(KM_SLEEP);
2430 	bp->b_flags = B_WRITE;
2431 	bp->b_resid = 0;
2432 
2433 	VHCI_INCR_PATH_CMDCOUNT(svp);
2434 	vlun = svp->svp_svl;
2435 
2436 	new_pkt = scsi_init_pkt(&svp->svp_psd->sd_address, NULL, bp,
2437 	    CDB_GROUP1, sizeof (struct scsi_arq_status), 0, 0,
2438 	    SLEEP_FUNC, NULL);
2439 	if (new_pkt == NULL) {
2440 		VHCI_DECR_PATH_CMDCOUNT(svp);
2441 		freerbuf(bp);
2442 		cmn_err(CE_WARN, "!vhci_do_prout: scsi_init_pkt failed");
2443 		return (0);
2444 	}
2445 	mutex_enter(&vlun->svl_mutex);
2446 	bp->b_un.b_addr = (caddr_t)&vlun->svl_prout;
2447 	bp->b_bcount = vlun->svl_bcount;
2448 	bcopy(vlun->svl_cdb, new_pkt->pkt_cdbp,
2449 	    sizeof (vlun->svl_cdb));
2450 	new_pkt->pkt_time = vlun->svl_time;
2451 	mutex_exit(&vlun->svl_mutex);
2452 	new_pkt->pkt_flags = FLAG_NOINTR;
2453 
2454 	ua_retry = nr_retry = retry = 0;
2455 again:
2456 	rval = vhci_do_scsi_cmd(new_pkt);
2457 	if (rval != 1) {
2458 		if ((new_pkt->pkt_reason == CMD_CMPLT) &&
2459 		    (SCBP_C(new_pkt) == STATUS_CHECK) &&
2460 		    (new_pkt->pkt_state & STATE_ARQ_DONE)) {
2461 			sns = &(((struct scsi_arq_status *)(uintptr_t)
2462 			    (new_pkt->pkt_scbp))->sts_sensedata);
2463 			if ((sns->es_key == KEY_UNIT_ATTENTION) ||
2464 			    (sns->es_key == KEY_NOT_READY)) {
2465 				int max_retry;
2466 				struct scsi_failover_ops *fops;
2467 				fops = vlun->svl_fops;
2468 				rval = (*fops->sfo_analyze_sense)
2469 				    (svp->svp_psd, sns,
2470 				    vlun->svl_fops_ctpriv);
2471 				if (rval == SCSI_SENSE_NOT_READY) {
2472 					max_retry = vhci_prout_not_ready_retry;
2473 					retry = nr_retry++;
2474 					delay(1*drv_usectohz(1000000));
2475 				} else {
2476 					/* chk for state change and update */
2477 					if (rval == SCSI_SENSE_STATE_CHANGED) {
2478 						int held;
2479 						VHCI_HOLD_LUN(vlun,
2480 						    VH_NOSLEEP, held);
2481 						if (!held) {
2482 							rval = TRAN_BUSY;
2483 						} else {
2484 							/* chk for alua first */
2485 							vhci_update_pathstates(
2486 							    (void *)vlun);
2487 						}
2488 					}
2489 					retry = ua_retry++;
2490 					max_retry = VHCI_MAX_PGR_RETRIES;
2491 				}
2492 				if (retry < max_retry) {
2493 					VHCI_DEBUG(4, (CE_WARN, NULL,
2494 					    "!vhci_do_prout retry 0x%x "
2495 					    "(0x%x 0x%x 0x%x)",
2496 					    SCBP_C(new_pkt),
2497 					    new_pkt->pkt_cdbp[0],
2498 					    new_pkt->pkt_cdbp[1],
2499 					    new_pkt->pkt_cdbp[2]));
2500 					goto again;
2501 				}
2502 				rval = 0;
2503 				VHCI_DEBUG(4, (CE_WARN, NULL,
2504 				    "!vhci_do_prout 0x%x "
2505 				    "(0x%x 0x%x 0x%x)",
2506 				    SCBP_C(new_pkt),
2507 				    new_pkt->pkt_cdbp[0],
2508 				    new_pkt->pkt_cdbp[1],
2509 				    new_pkt->pkt_cdbp[2]));
2510 			} else if (sns->es_key == KEY_ILLEGAL_REQUEST)
2511 				rval = VHCI_PGR_ILLEGALOP;
2512 		}
2513 	} else {
2514 		rval = 1;
2515 	}
2516 	scsi_destroy_pkt(new_pkt);
2517 	VHCI_DECR_PATH_CMDCOUNT(svp);
2518 	freerbuf(bp);
2519 	return (rval);
2520 }
2521 
2522 static void
2523 vhci_run_cmd(void *arg)
2524 {
2525 	struct scsi_pkt		*pkt = (struct scsi_pkt *)arg;
2526 	struct scsi_pkt		*tpkt;
2527 	scsi_vhci_priv_t	*svp;
2528 	mdi_pathinfo_t		*pip, *npip;
2529 	scsi_vhci_lun_t		*vlun;
2530 	dev_info_t		*cdip;
2531 	scsi_vhci_priv_t	*nsvp;
2532 	int			fail = 0;
2533 	int			rval;
2534 	struct vhci_pkt		*vpkt;
2535 	uchar_t			cdb_1;
2536 	vhci_prout_t		*prout;
2537 
2538 	vpkt = (struct vhci_pkt *)pkt->pkt_private;
2539 	tpkt = vpkt->vpkt_tgt_pkt;
2540 	pip = vpkt->vpkt_path;
2541 	svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
2542 	if (svp == NULL) {
2543 		tpkt->pkt_reason = CMD_TRAN_ERR;
2544 		tpkt->pkt_statistics = STAT_ABORTED;
2545 		goto done;
2546 	}
2547 	vlun = svp->svp_svl;
2548 	prout = &vlun->svl_prout;
2549 	if (SCBP_C(pkt) != STATUS_GOOD)
2550 		fail++;
2551 	cdip = vlun->svl_dip;
2552 	pip = npip = NULL;
2553 	rval = mdi_select_path(cdip, NULL,
2554 	    MDI_SELECT_ONLINE_PATH|MDI_SELECT_STANDBY_PATH, NULL, &npip);
2555 	if ((rval != MDI_SUCCESS) || (npip == NULL)) {
2556 		VHCI_DEBUG(4, (CE_NOTE, NULL,
2557 		    "vhci_run_cmd: no path! 0x%p\n", (void *)svp));
2558 		tpkt->pkt_reason = CMD_TRAN_ERR;
2559 		tpkt->pkt_statistics = STAT_ABORTED;
2560 		goto done;
2561 	}
2562 
2563 	cdb_1 = vlun->svl_cdb[1];
2564 	vlun->svl_cdb[1] &= 0xe0;
2565 	vlun->svl_cdb[1] |= VHCI_PROUT_R_AND_IGNORE;
2566 
2567 	do {
2568 		nsvp = (scsi_vhci_priv_t *)
2569 		    mdi_pi_get_vhci_private(npip);
2570 		if (nsvp == NULL) {
2571 			VHCI_DEBUG(4, (CE_NOTE, NULL,
2572 			    "vhci_run_cmd: no "
2573 			    "client priv! 0x%p offlined?\n",
2574 			    (void *)npip));
2575 			goto next_path;
2576 		}
2577 		if (vlun->svl_first_path == npip) {
2578 			goto next_path;
2579 		} else {
2580 			if (vhci_do_prout(nsvp) != 1)
2581 				fail++;
2582 		}
2583 next_path:
2584 		pip = npip;
2585 		rval = mdi_select_path(cdip, NULL,
2586 		    MDI_SELECT_ONLINE_PATH|MDI_SELECT_STANDBY_PATH,
2587 		    pip, &npip);
2588 		mdi_rele_path(pip);
2589 	} while ((rval == MDI_SUCCESS) && (npip != NULL));
2590 
2591 	vlun->svl_cdb[1] = cdb_1;
2592 
2593 	if (fail) {
2594 		VHCI_DEBUG(4, (CE_WARN, NULL, "%s%d: key registration failed, "
2595 		    "couldn't be replicated on all paths",
2596 		    ddi_driver_name(cdip), ddi_get_instance(cdip)));
2597 		vhci_print_prout_keys(vlun, "vhci_run_cmd: ");
2598 
2599 		if (SCBP_C(pkt) != STATUS_GOOD) {
2600 			tpkt->pkt_reason = CMD_TRAN_ERR;
2601 			tpkt->pkt_statistics = STAT_ABORTED;
2602 		}
2603 	} else {
2604 		vlun->svl_pgr_active = 1;
2605 		vhci_print_prout_keys(vlun, "vhci_run_cmd: before bcopy:");
2606 
2607 		bcopy((const void *)prout->service_key,
2608 		    (void *)prout->active_service_key, MHIOC_RESV_KEY_SIZE);
2609 		bcopy((const void *)prout->res_key,
2610 		    (void *)prout->active_res_key, MHIOC_RESV_KEY_SIZE);
2611 
2612 		vhci_print_prout_keys(vlun, "vhci_run_cmd: after bcopy:");
2613 	}
2614 done:
2615 	if (SCBP_C(pkt) == STATUS_GOOD)
2616 		vlun->svl_first_path = NULL;
2617 
2618 	if (svp)
2619 		VHCI_DECR_PATH_CMDCOUNT(svp);
2620 
2621 	if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) {
2622 		scsi_destroy_pkt(pkt);
2623 		vpkt->vpkt_hba_pkt = NULL;
2624 		if (vpkt->vpkt_path) {
2625 			mdi_rele_path(vpkt->vpkt_path);
2626 			vpkt->vpkt_path = NULL;
2627 		}
2628 	}
2629 
2630 	sema_v(&vlun->svl_pgr_sema);
2631 	/*
2632 	 * The PROUT commands are not included in the automatic retry
2633 	 * mechanism, therefore, vpkt_org_vpkt should never be set here.
2634 	 */
2635 	ASSERT(vpkt->vpkt_org_vpkt == NULL);
2636 	if (tpkt->pkt_comp)
2637 		(*tpkt->pkt_comp)(tpkt);
2638 
2639 }
2640 
2641 /*
2642  * Get the keys registered with this target.  Since we will have
2643  * registered the same key with multiple initiators, strip out
2644  * any duplicate keys.
2645  *
2646  * The pointers which will be used to filter the registered keys from
2647  * the device will be stored in filter_prin and filter_pkt.  If the
2648  * allocation length of the buffer was sufficient for the number of
2649  * parameter data bytes available to be returned by the device then the
2650  * key filtering will use the keylist returned from the original
2651  * request.  If the allocation length of the buffer was not sufficient,
2652  * then the filtering will use the keylist returned from the request
2653  * that is resent below.
2654  *
2655  * If the device returns an additional length field that is greater than
2656  * the allocation length of the buffer, then allocate a new buffer which
2657  * can accommodate the number of parameter data bytes available to be
2658  * returned.  Resend the scsi PRIN command, filter out the duplicate
2659  * keys and return as many of the unique keys found that was originally
2660  * requested and set the additional length field equal to the data bytes
2661  * of unique reservation keys available to be returned.
2662  *
2663  * If the device returns an additional length field that is less than or
2664  * equal to the allocation length of the buffer, then all the available
2665  * keys registered were returned by the device.  Filter out the
2666  * duplicate keys and return all of the unique keys found and set the
2667  * additional length field equal to the data bytes of the reservation
2668  * keys to be returned.
2669  */
2670 static int
2671 vhci_do_prin(struct vhci_pkt **vpkt)
2672 {
2673 	scsi_vhci_priv_t *svp = (scsi_vhci_priv_t *)
2674 	    mdi_pi_get_vhci_private((*vpkt)->vpkt_path);
2675 	vhci_prin_readkeys_t *prin;
2676 	scsi_vhci_lun_t *vlun = svp->svp_svl;
2677 	struct scsi_vhci *vhci =
2678 	    ADDR2VHCI(&((*vpkt)->vpkt_tgt_pkt->pkt_address));
2679 
2680 	struct buf		*new_bp = NULL;
2681 	struct scsi_pkt		*new_pkt = NULL;
2682 	struct vhci_pkt		*new_vpkt = NULL;
2683 	int			hdr_len = 0;
2684 	int			rval = VHCI_CMD_CMPLT;
2685 	uint32_t		prin_length = 0;
2686 	uint32_t		svl_prin_length = 0;
2687 
2688 	prin = (vhci_prin_readkeys_t *)
2689 	    bp_mapin_common((*vpkt)->vpkt_tgt_init_bp, VM_NOSLEEP);
2690 
2691 	if (prin != NULL) {
2692 		prin_length = BE_32(prin->length);
2693 	}
2694 
2695 	if (prin == NULL) {
2696 		VHCI_DEBUG(5, (CE_WARN, NULL,
2697 		    "vhci_do_prin: bp_mapin_common failed."));
2698 		rval = VHCI_CMD_ERROR;
2699 	} else {
2700 		/*
2701 		 * According to SPC-3r22, sec 4.3.4.6: "If the amount of
2702 		 * information to be transferred exceeds the maximum value
2703 		 * that the ALLOCATION LENGTH field is capable of specifying,
2704 		 * the device server shall...terminate the command with CHECK
2705 		 * CONDITION status".  The ALLOCATION LENGTH field of the
2706 		 * PERSISTENT RESERVE IN command is 2 bytes. We should never
2707 		 * get here with an ADDITIONAL LENGTH greater than 0xFFFF
2708 		 * so if we do, then it is an error!
2709 		 */
2710 
2711 		hdr_len = sizeof (prin->length) + sizeof (prin->generation);
2712 
2713 		if ((prin_length + hdr_len) > 0xFFFF) {
2714 			VHCI_DEBUG(5, (CE_NOTE, NULL,
2715 			    "vhci_do_prin: Device returned invalid "
2716 			    "length 0x%x\n", prin_length));
2717 			rval = VHCI_CMD_ERROR;
2718 		}
2719 	}
2720 
2721 	/*
2722 	 * If prin->length is greater than the byte count allocated in the
2723 	 * original buffer, then resend the request with enough buffer
2724 	 * allocated to get all of the available registered keys.
2725 	 */
2726 	if (rval != VHCI_CMD_ERROR) {
2727 		if (((*vpkt)->vpkt_tgt_init_bp->b_bcount - hdr_len) <
2728 		    prin_length) {
2729 			if ((*vpkt)->vpkt_org_vpkt == NULL) {
2730 				new_pkt = vhci_create_retry_pkt(*vpkt);
2731 				if (new_pkt != NULL) {
2732 					new_vpkt = TGTPKT2VHCIPKT(new_pkt);
2733 
2734 					/*
2735 					 * This is the buf with buffer pointer
2736 					 * where the prin readkeys will be
2737 					 * returned from the device
2738 					 */
2739 					new_bp = scsi_alloc_consistent_buf(
2740 					    &svp->svp_psd->sd_address,
2741 					    NULL, (prin_length + hdr_len),
2742 					    ((*vpkt)->vpkt_tgt_init_bp->
2743 					    b_flags & (B_READ | B_WRITE)),
2744 					    NULL_FUNC, NULL);
2745 					if (new_bp != NULL) {
2746 						if (new_bp->b_un.b_addr !=
2747 						    NULL) {
2748 
2749 							new_bp->b_bcount =
2750 							    prin_length +
2751 							    hdr_len;
2752 
2753 							new_pkt->pkt_cdbp[7] =
2754 							    (uchar_t)(new_bp->
2755 							    b_bcount >> 8);
2756 							new_pkt->pkt_cdbp[8] =
2757 							    (uchar_t)new_bp->
2758 							    b_bcount;
2759 
2760 							rval = VHCI_CMD_RETRY;
2761 						} else {
2762 							rval = VHCI_CMD_ERROR;
2763 						}
2764 					} else {
2765 						rval = VHCI_CMD_ERROR;
2766 					}
2767 				} else {
2768 					rval = VHCI_CMD_ERROR;
2769 				}
2770 			} else {
2771 				rval = VHCI_CMD_ERROR;
2772 			}
2773 		}
2774 	}
2775 
2776 	if (rval == VHCI_CMD_RETRY) {
2777 		new_vpkt->vpkt_tgt_init_bp = new_bp;
2778 
2779 		/*
2780 		 * Release the old path because it does not matter which path
2781 		 * this command is sent down.  This allows the normal bind
2782 		 * transport mechanism to be used.
2783 		 */
2784 		if ((*vpkt)->vpkt_path != NULL) {
2785 			mdi_rele_path((*vpkt)->vpkt_path);
2786 			(*vpkt)->vpkt_path = NULL;
2787 		}
2788 
2789 		/*
2790 		 * Dispatch the retry command
2791 		 */
2792 		if (taskq_dispatch(vhci->vhci_taskq, vhci_dispatch_scsi_start,
2793 		    (void *) new_vpkt, KM_NOSLEEP) == NULL) {
2794 			rval = VHCI_CMD_ERROR;
2795 		} else {
2796 			/*
2797 			 * If we return VHCI_CMD_RETRY, that means the caller
2798 			 * is going to bail and wait for the reissued command
2799 			 * to complete.  In that case, we need to decrement
2800 			 * the path command count right now.  In any other
2801 			 * case, it'll be decremented by the caller.
2802 			 */
2803 			VHCI_DECR_PATH_CMDCOUNT(svp);
2804 		}
2805 	}
2806 
2807 	if ((rval != VHCI_CMD_ERROR) && (rval != VHCI_CMD_RETRY)) {
2808 		int new, old;
2809 		int data_len = 0;
2810 
2811 		data_len = prin_length / MHIOC_RESV_KEY_SIZE;
2812 		VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_do_prin: %d keys read\n",
2813 		    data_len));
2814 
2815 #ifdef DEBUG
2816 		VHCI_DEBUG(5, (CE_NOTE, NULL, "vhci_do_prin: from storage\n"));
2817 		if (vhci_debug == 5)
2818 			vhci_print_prin_keys(prin, data_len);
2819 		VHCI_DEBUG(5, (CE_NOTE, NULL,
2820 		    "vhci_do_prin: MPxIO old keys:\n"));
2821 		if (vhci_debug == 5)
2822 			vhci_print_prin_keys(&vlun->svl_prin, data_len);
2823 #endif
2824 
2825 		/*
2826 		 * Filter out all duplicate keys returned from the device
2827 		 * We know that we use a different key for every host, so we
2828 		 * can simply strip out duplicates. Otherwise we would need to
2829 		 * do more bookkeeping to figure out which keys to strip out.
2830 		 */
2831 
2832 		new = 0;
2833 
2834 		if (data_len > 0) {
2835 			vlun->svl_prin.keylist[0] = prin->keylist[0];
2836 			new++;
2837 		}
2838 
2839 		for (old = 1; old < data_len; old++) {
2840 			int j;
2841 			int match = 0;
2842 			for (j = 0; j < new; j++) {
2843 				if (bcmp(&prin->keylist[old],
2844 				    &vlun->svl_prin.keylist[j],
2845 				    sizeof (mhioc_resv_key_t)) == 0) {
2846 					match = 1;
2847 					break;
2848 				}
2849 			}
2850 			if (!match) {
2851 				vlun->svl_prin.keylist[new] =
2852 				    prin->keylist[old];
2853 				new++;
2854 			}
2855 		}
2856 
2857 		vlun->svl_prin.generation = prin->generation;
2858 		svl_prin_length = new * MHIOC_RESV_KEY_SIZE;
2859 		vlun->svl_prin.length = BE_32(svl_prin_length);
2860 
2861 		/*
2862 		 * If we arrived at this point after issuing a retry, make sure
2863 		 * that we put everything back the way it originally was so
2864 		 * that the target driver can complete the command correctly.
2865 		 */
2866 		if ((*vpkt)->vpkt_org_vpkt != NULL) {
2867 			new_bp = (*vpkt)->vpkt_tgt_init_bp;
2868 
2869 			scsi_free_consistent_buf(new_bp);
2870 
2871 			*vpkt = vhci_sync_retry_pkt(*vpkt);
2872 
2873 			/*
2874 			 * Make sure the original buffer is mapped into kernel
2875 			 * space before we try to copy the filtered keys into
2876 			 * it.
2877 			 */
2878 			prin = (vhci_prin_readkeys_t *)bp_mapin_common(
2879 			    (*vpkt)->vpkt_tgt_init_bp, VM_NOSLEEP);
2880 		}
2881 
2882 		/*
2883 		 * Now copy the desired number of prin keys into the original
2884 		 * target buffer.
2885 		 */
2886 		if (svl_prin_length <=
2887 		    ((*vpkt)->vpkt_tgt_init_bp->b_bcount - hdr_len)) {
2888 			/*
2889 			 * It is safe to return all of the available unique
2890 			 * keys
2891 			 */
2892 			bcopy(&vlun->svl_prin, prin, svl_prin_length + hdr_len);
2893 		} else {
2894 			/*
2895 			 * Not all of the available keys were requested by the
2896 			 * original command.
2897 			 */
2898 			bcopy(&vlun->svl_prin, prin,
2899 			    (*vpkt)->vpkt_tgt_init_bp->b_bcount);
2900 		}
2901 #ifdef DEBUG
2902 		VHCI_DEBUG(5, (CE_NOTE, NULL,
2903 		    "vhci_do_prin: To Application:\n"));
2904 		if (vhci_debug == 5)
2905 			vhci_print_prin_keys(prin, new);
2906 		VHCI_DEBUG(5, (CE_NOTE, NULL,
2907 		    "vhci_do_prin: MPxIO new keys:\n"));
2908 		if (vhci_debug == 5)
2909 			vhci_print_prin_keys(&vlun->svl_prin, new);
2910 #endif
2911 	}
2912 
2913 	if (rval == VHCI_CMD_ERROR) {
2914 		/*
2915 		 * If we arrived at this point after issuing a
2916 		 * retry, make sure that we put everything back
2917 		 * the way it originally was so that ssd can
2918 		 * complete the command correctly.
2919 		 */
2920 
2921 		if ((*vpkt)->vpkt_org_vpkt != NULL) {
2922 			new_bp = (*vpkt)->vpkt_tgt_init_bp;
2923 			if (new_bp != NULL) {
2924 				scsi_free_consistent_buf(new_bp);
2925 			}
2926 
2927 			new_vpkt = *vpkt;
2928 			*vpkt = (*vpkt)->vpkt_org_vpkt;
2929 
2930 			vhci_scsi_destroy_pkt(&svp->svp_psd->sd_address,
2931 			    new_vpkt->vpkt_tgt_pkt);
2932 		}
2933 
2934 		/*
2935 		 * Mark this command completion as having an error so that
2936 		 * ssd will retry the command.
2937 		 */
2938 
2939 		(*vpkt)->vpkt_tgt_pkt->pkt_reason = CMD_ABORTED;
2940 		(*vpkt)->vpkt_tgt_pkt->pkt_statistics |= STAT_ABORTED;
2941 
2942 		rval = VHCI_CMD_CMPLT;
2943 	}
2944 
2945 	/*
2946 	 * Make sure that the semaphore is only released once.
2947 	 */
2948 	if (rval == VHCI_CMD_CMPLT) {
2949 		sema_v(&vlun->svl_pgr_sema);
2950 	}
2951 
2952 	return (rval);
2953 }
2954 
2955 static void
2956 vhci_intr(struct scsi_pkt *pkt)
2957 {
2958 	struct vhci_pkt		*vpkt = (struct vhci_pkt *)pkt->pkt_private;
2959 	struct scsi_pkt		*tpkt;
2960 	scsi_vhci_priv_t	*svp;
2961 	scsi_vhci_lun_t		*vlun;
2962 	int			rval, held;
2963 	struct scsi_failover_ops	*fops;
2964 	struct scsi_extended_sense	*sns;
2965 	mdi_pathinfo_t		*lpath;
2966 	static char		*timeout_err = "Command Timeout";
2967 	static char		*parity_err = "Parity Error";
2968 	char			*err_str = NULL;
2969 	dev_info_t		*vdip, *cdip, *pdip;
2970 	char			*cpath, *dpath;
2971 
2972 	ASSERT(vpkt != NULL);
2973 	tpkt = vpkt->vpkt_tgt_pkt;
2974 	ASSERT(tpkt != NULL);
2975 	svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(vpkt->vpkt_path);
2976 	ASSERT(svp != NULL);
2977 	vlun = svp->svp_svl;
2978 	ASSERT(vlun != NULL);
2979 	lpath = vpkt->vpkt_path;
2980 
2981 	/*
2982 	 * sync up the target driver's pkt with the pkt that
2983 	 * we actually used
2984 	 */
2985 	*(tpkt->pkt_scbp) = *(pkt->pkt_scbp);
2986 	tpkt->pkt_resid = pkt->pkt_resid;
2987 	tpkt->pkt_state = pkt->pkt_state;
2988 	tpkt->pkt_statistics = pkt->pkt_statistics;
2989 	tpkt->pkt_reason = pkt->pkt_reason;
2990 
2991 	if (pkt->pkt_cdbp[0] == SCMD_PROUT &&
2992 	    ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) ||
2993 	    ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_R_AND_IGNORE)) {
2994 		if ((SCBP_C(pkt) != STATUS_GOOD) ||
2995 		    (pkt->pkt_reason != CMD_CMPLT)) {
2996 			sema_v(&vlun->svl_pgr_sema);
2997 		}
2998 	} else if (pkt->pkt_cdbp[0] == SCMD_PRIN) {
2999 		if (pkt->pkt_reason != CMD_CMPLT ||
3000 		    (SCBP_C(pkt) != STATUS_GOOD)) {
3001 			sema_v(&vlun->svl_pgr_sema);
3002 		}
3003 	}
3004 
3005 	switch (pkt->pkt_reason) {
3006 	case CMD_CMPLT:
3007 		/*
3008 		 * cmd completed successfully, check for scsi errors
3009 		 */
3010 		switch (*(pkt->pkt_scbp)) {
3011 		case STATUS_CHECK:
3012 			if (pkt->pkt_state & STATE_ARQ_DONE) {
3013 				sns = &(((struct scsi_arq_status *)(uintptr_t)
3014 				    (pkt->pkt_scbp))->sts_sensedata);
3015 				fops = vlun->svl_fops;
3016 				ASSERT(fops != NULL);
3017 				VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_intr: "
3018 				    "Received sns key %x  esc %x  escq %x\n",
3019 				    sns->es_key, sns->es_add_code,
3020 				    sns->es_qual_code));
3021 
3022 				if (vlun->svl_waiting_for_activepath == 1) {
3023 					/*
3024 					 * if we are here it means we are
3025 					 * in the midst of a probe/attach
3026 					 * through a passive path; this
3027 					 * case is exempt from sense analysis
3028 					 * for detection of ext. failover
3029 					 * because that would unnecessarily
3030 					 * increase attach time.
3031 					 */
3032 					bcopy(pkt->pkt_scbp, tpkt->pkt_scbp,
3033 					    vpkt->vpkt_tgt_init_scblen);
3034 					break;
3035 				}
3036 				if (sns->es_add_code == VHCI_SCSI_PERR) {
3037 					/*
3038 					 * parity error
3039 					 */
3040 					err_str = parity_err;
3041 					bcopy(pkt->pkt_scbp, tpkt->pkt_scbp,
3042 					    vpkt->vpkt_tgt_init_scblen);
3043 					break;
3044 				}
3045 				rval = (*fops->sfo_analyze_sense)
3046 				    (svp->svp_psd, sns, vlun->svl_fops_ctpriv);
3047 				if ((rval == SCSI_SENSE_NOFAILOVER) ||
3048 				    (rval == SCSI_SENSE_UNKNOWN) ||
3049 				    (rval == SCSI_SENSE_NOT_READY)) {
3050 					bcopy(pkt->pkt_scbp, tpkt->pkt_scbp,
3051 					    vpkt->vpkt_tgt_init_scblen);
3052 					break;
3053 				} else if (rval == SCSI_SENSE_STATE_CHANGED) {
3054 					struct scsi_vhci	*vhci;
3055 					vhci = ADDR2VHCI(&tpkt->pkt_address);
3056 					VHCI_HOLD_LUN(vlun, VH_NOSLEEP, held);
3057 					if (!held) {
3058 						/*
3059 						 * looks like some other thread
3060 						 * has already detected this
3061 						 * condition
3062 						 */
3063 						tpkt->pkt_state &=
3064 						    ~STATE_ARQ_DONE;
3065 						*(tpkt->pkt_scbp) =
3066 						    STATUS_BUSY;
3067 						break;
3068 					}
3069 					(void) taskq_dispatch(
3070 					    vhci->vhci_update_pathstates_taskq,
3071 					    vhci_update_pathstates,
3072 					    (void *)vlun, KM_SLEEP);
3073 				} else {
3074 					/*
3075 					 * externally initiated failover
3076 					 * has occurred or is in progress
3077 					 */
3078 					VHCI_HOLD_LUN(vlun, VH_NOSLEEP, held);
3079 					if (!held) {
3080 						/*
3081 						 * looks like some other thread
3082 						 * has already detected this
3083 						 * condition
3084 						 */
3085 						tpkt->pkt_state &=
3086 						    ~STATE_ARQ_DONE;
3087 						*(tpkt->pkt_scbp) =
3088 						    STATUS_BUSY;
3089 						break;
3090 					} else {
3091 						rval = vhci_handle_ext_fo
3092 						    (pkt, rval);
3093 						if (rval == BUSY_RETURN) {
3094 							tpkt->pkt_state &=
3095 							    ~STATE_ARQ_DONE;
3096 							*(tpkt->pkt_scbp) =
3097 							    STATUS_BUSY;
3098 							break;
3099 						}
3100 						bcopy(pkt->pkt_scbp,
3101 						    tpkt->pkt_scbp,
3102 						    vpkt->vpkt_tgt_init_scblen);
3103 						break;
3104 					}
3105 				}
3106 			}
3107 			break;
3108 
3109 		/*
3110 		 * If this is a good SCSI-II RELEASE cmd completion then restore
3111 		 * the load balancing policy and reset VLUN_RESERVE_ACTIVE_FLG.
3112 		 * If this is a good SCSI-II RESERVE cmd completion then set
3113 		 * VLUN_RESERVE_ACTIVE_FLG.
3114 		 */
3115 		case STATUS_GOOD:
3116 			if ((pkt->pkt_cdbp[0] == SCMD_RELEASE) ||
3117 			    (pkt->pkt_cdbp[0] == SCMD_RELEASE_G1)) {
3118 				(void) mdi_set_lb_policy(vlun->svl_dip,
3119 				    vlun->svl_lb_policy_save);
3120 				vlun->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG;
3121 				VHCI_DEBUG(1, (CE_WARN, NULL,
3122 				    "!vhci_intr: vlun 0x%p release path 0x%p",
3123 				    (void *)vlun, (void *)vpkt->vpkt_path));
3124 			}
3125 
3126 			if ((pkt->pkt_cdbp[0] == SCMD_RESERVE) ||
3127 			    (pkt->pkt_cdbp[0] == SCMD_RESERVE_G1)) {
3128 				vlun->svl_flags |= VLUN_RESERVE_ACTIVE_FLG;
3129 				vlun->svl_resrv_pip = vpkt->vpkt_path;
3130 				VHCI_DEBUG(1, (CE_WARN, NULL,
3131 				    "!vhci_intr: vlun 0x%p reserved path 0x%p",
3132 				    (void *)vlun, (void *)vpkt->vpkt_path));
3133 			}
3134 			break;
3135 
3136 		case STATUS_RESERVATION_CONFLICT:
3137 			VHCI_DEBUG(1, (CE_WARN, NULL,
3138 			    "!vhci_intr: vlun 0x%p "
3139 			    "reserve conflict on path 0x%p",
3140 			    (void *)vlun, (void *)vpkt->vpkt_path));
3141 			/* FALLTHROUGH */
3142 		default:
3143 			break;
3144 		}
3145 
3146 		/*
3147 		 * Update I/O completion statistics for the path
3148 		 */
3149 		mdi_pi_kstat_iosupdate(vpkt->vpkt_path, vpkt->vpkt_tgt_init_bp);
3150 
3151 		/*
3152 		 * Command completed successfully, release the dma binding and
3153 		 * destroy the transport side of the packet.
3154 		 */
3155 		if ((pkt->pkt_cdbp[0] ==  SCMD_PROUT) &&
3156 		    (((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) ||
3157 		    ((pkt->pkt_cdbp[1] & 0x1f) ==
3158 		    VHCI_PROUT_R_AND_IGNORE))) {
3159 			if (SCBP_C(pkt) == STATUS_GOOD) {
3160 				ASSERT(vlun->svl_taskq);
3161 				svp->svp_last_pkt_reason = pkt->pkt_reason;
3162 				(void) taskq_dispatch(vlun->svl_taskq,
3163 				    vhci_run_cmd, pkt, KM_SLEEP);
3164 				return;
3165 			}
3166 		}
3167 		if ((SCBP_C(pkt) == STATUS_GOOD) &&
3168 		    (pkt->pkt_cdbp[0] == SCMD_PRIN) &&
3169 		    vpkt->vpkt_tgt_init_bp) {
3170 			/*
3171 			 * If the action (value in byte 1 of the cdb) is zero,
3172 			 * we're reading keys, and that's the only condition
3173 			 * where we need to be concerned with filtering keys
3174 			 * and potential retries.  Otherwise, we simply signal
3175 			 * the semaphore and move on.
3176 			 */
3177 			if (pkt->pkt_cdbp[1] == 0) {
3178 				/*
3179 				 * If this is the completion of an internal
3180 				 * retry then we need to make sure that the
3181 				 * pkt and tpkt pointers are readjusted so
3182 				 * the calls to scsi_destroy_pkt and pkt_comp
3183 				 * below work * correctly.
3184 				 */
3185 				if (vpkt->vpkt_org_vpkt != NULL) {
3186 					pkt = vpkt->vpkt_org_vpkt->vpkt_hba_pkt;
3187 					tpkt = vpkt->vpkt_org_vpkt->
3188 					    vpkt_tgt_pkt;
3189 
3190 					/*
3191 					 * If this command was issued through
3192 					 * the taskq then we need to clear
3193 					 * this flag for proper processing in
3194 					 * the case of a retry from the target
3195 					 * driver.
3196 					 */
3197 					vpkt->vpkt_state &=
3198 					    ~VHCI_PKT_THRU_TASKQ;
3199 				}
3200 
3201 				/*
3202 				 * if vhci_do_prin returns VHCI_CMD_CMPLT then
3203 				 * vpkt will contain the address of the
3204 				 * original vpkt
3205 				 */
3206 				if (vhci_do_prin(&vpkt) == VHCI_CMD_RETRY) {
3207 					/*
3208 					 * The command has been resent to get
3209 					 * all the keys from the device.  Don't
3210 					 * complete the command with ssd until
3211 					 * the retry completes.
3212 					 */
3213 					return;
3214 				}
3215 			} else {
3216 				sema_v(&vlun->svl_pgr_sema);
3217 			}
3218 		}
3219 
3220 		break;
3221 
3222 	case CMD_TIMEOUT:
3223 		if ((pkt->pkt_statistics &
3224 		    (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
3225 
3226 			VHCI_DEBUG(1, (CE_NOTE, NULL,
3227 			    "!scsi vhci timeout invoked\n"));
3228 
3229 			(void) vhci_recovery_reset(vlun, &pkt->pkt_address,
3230 			    FALSE, VHCI_DEPTH_ALL);
3231 		}
3232 		MDI_PI_ERRSTAT(lpath, MDI_PI_TRANSERR);
3233 		tpkt->pkt_statistics |= STAT_ABORTED;
3234 		err_str = timeout_err;
3235 		break;
3236 
3237 	case CMD_TRAN_ERR:
3238 		/*
3239 		 * This status is returned if the transport has sent the cmd
3240 		 * down the link to the target and then some error occurs.
3241 		 * In case of SCSI-II RESERVE cmd, we don't know if the
3242 		 * reservation been accepted by the target or not, so we need
3243 		 * to clear the reservation.
3244 		 */
3245 		if ((pkt->pkt_cdbp[0] == SCMD_RESERVE) ||
3246 		    (pkt->pkt_cdbp[0] == SCMD_RESERVE_G1)) {
3247 			VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_intr received"
3248 			    " cmd_tran_err for scsi-2 reserve cmd\n"));
3249 			if (!vhci_recovery_reset(vlun, &pkt->pkt_address,
3250 			    TRUE, VHCI_DEPTH_TARGET)) {
3251 				VHCI_DEBUG(1, (CE_WARN, NULL,
3252 				    "!vhci_intr cmd_tran_err reset failed!"));
3253 			}
3254 		}
3255 		break;
3256 
3257 	case CMD_DEV_GONE:
3258 		tpkt->pkt_reason = CMD_CMPLT;
3259 		tpkt->pkt_state = STATE_GOT_BUS |
3260 		    STATE_GOT_TARGET | STATE_SENT_CMD |
3261 		    STATE_GOT_STATUS;
3262 		*(tpkt->pkt_scbp) = STATUS_BUSY;
3263 		break;
3264 
3265 	default:
3266 		break;
3267 	}
3268 
3269 	/*
3270 	 * SCSI-II RESERVE cmd has been serviced by the lower layers clear
3271 	 * the flag so the lun is not QUIESCED any longer.
3272 	 * Also clear the VHCI_PKT_THRU_TASKQ flag, to ensure that if this pkt
3273 	 * is retried, a taskq shall again be dispatched to service it.  Else
3274 	 * it may lead to a system hang if the retry is within interrupt
3275 	 * context.
3276 	 */
3277 	if ((pkt->pkt_cdbp[0] == SCMD_RESERVE) ||
3278 	    (pkt->pkt_cdbp[0] == SCMD_RESERVE_G1)) {
3279 		vlun->svl_flags &= ~VLUN_QUIESCED_FLG;
3280 		vpkt->vpkt_state &= ~VHCI_PKT_THRU_TASKQ;
3281 	}
3282 
3283 	/*
3284 	 * vpkt_org_vpkt should always be NULL here if the retry command
3285 	 * has been successfully processed.  If vpkt_org_vpkt != NULL at
3286 	 * this point, it is an error so restore the original vpkt and
3287 	 * return an error to the target driver so it can retry the
3288 	 * command as appropriate.
3289 	 */
3290 	if (vpkt->vpkt_org_vpkt != NULL) {
3291 		struct vhci_pkt *new_vpkt = vpkt;
3292 		vpkt = vpkt->vpkt_org_vpkt;
3293 
3294 		vhci_scsi_destroy_pkt(&svp->svp_psd->sd_address,
3295 		    new_vpkt->vpkt_tgt_pkt);
3296 
3297 		/*
3298 		 * Mark this command completion as having an error so that
3299 		 * ssd will retry the command.
3300 		 */
3301 		vpkt->vpkt_tgt_pkt->pkt_reason = CMD_ABORTED;
3302 		vpkt->vpkt_tgt_pkt->pkt_statistics |= STAT_ABORTED;
3303 
3304 		pkt = vpkt->vpkt_hba_pkt;
3305 		tpkt = vpkt->vpkt_tgt_pkt;
3306 	}
3307 
3308 	if ((err_str != NULL) && (pkt->pkt_reason !=
3309 	    svp->svp_last_pkt_reason)) {
3310 		cdip = vlun->svl_dip;
3311 		pdip = mdi_pi_get_phci(vpkt->vpkt_path);
3312 		vdip = ddi_get_parent(cdip);
3313 		cpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3314 		dpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3315 		vhci_log(CE_WARN, vdip, "!%s (%s%d): %s on path %s (%s%d)",
3316 		    ddi_pathname(cdip, cpath), ddi_driver_name(cdip),
3317 		    ddi_get_instance(cdip), err_str,
3318 		    ddi_pathname(pdip, dpath), ddi_driver_name(pdip),
3319 		    ddi_get_instance(pdip));
3320 		kmem_free(cpath, MAXPATHLEN);
3321 		kmem_free(dpath, MAXPATHLEN);
3322 	}
3323 	svp->svp_last_pkt_reason = pkt->pkt_reason;
3324 	VHCI_DECR_PATH_CMDCOUNT(svp);
3325 
3326 	/*
3327 	 * For PARTIAL_DMA, vhci should not free the path.
3328 	 * Target driver will call into vhci_scsi_dmafree or
3329 	 * destroy pkt to release this path.
3330 	 */
3331 	if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) {
3332 		scsi_destroy_pkt(pkt);
3333 		vpkt->vpkt_hba_pkt = NULL;
3334 		if (vpkt->vpkt_path) {
3335 			mdi_rele_path(vpkt->vpkt_path);
3336 			vpkt->vpkt_path = NULL;
3337 		}
3338 	}
3339 
3340 	if (tpkt->pkt_comp) {
3341 		(*tpkt->pkt_comp)(tpkt);
3342 	}
3343 }
3344 
3345 /*
3346  * two possibilities: (1) failover has completed
3347  * or (2) is in progress; update our path states for
3348  * the former case; for the latter case,
3349  * initiate a scsi_watch request to
3350  * determine when failover completes - vlun is HELD
3351  * until failover completes; BUSY is returned to upper
3352  * layer in both the cases
3353  */
3354 static int
3355 vhci_handle_ext_fo(struct scsi_pkt *pkt, int fostat)
3356 {
3357 	struct vhci_pkt		*vpkt = (struct vhci_pkt *)pkt->pkt_private;
3358 	struct scsi_pkt		*tpkt;
3359 	scsi_vhci_priv_t	*svp;
3360 	scsi_vhci_lun_t		*vlun;
3361 	struct scsi_vhci	*vhci;
3362 	scsi_vhci_swarg_t	*swarg;
3363 	char			*path;
3364 
3365 	ASSERT(vpkt != NULL);
3366 	tpkt = vpkt->vpkt_tgt_pkt;
3367 	ASSERT(tpkt != NULL);
3368 	svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(vpkt->vpkt_path);
3369 	ASSERT(svp != NULL);
3370 	vlun = svp->svp_svl;
3371 	ASSERT(vlun != NULL);
3372 	ASSERT(VHCI_LUN_IS_HELD(vlun));
3373 
3374 	vhci = ADDR2VHCI(&tpkt->pkt_address);
3375 
3376 	if (fostat == SCSI_SENSE_INACTIVE) {
3377 		VHCI_DEBUG(1, (CE_NOTE, NULL, "!Failover "
3378 		    "detected for %s; updating path states...\n",
3379 		    vlun->svl_lun_wwn));
3380 		/*
3381 		 * set the vlun flag to indicate to the task that the target
3382 		 * port group needs updating
3383 		 */
3384 		vlun->svl_flags |= VLUN_UPDATE_TPG;
3385 		(void) taskq_dispatch(vhci->vhci_update_pathstates_taskq,
3386 		    vhci_update_pathstates, (void *)vlun, KM_SLEEP);
3387 	} else {
3388 		path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3389 		vhci_log(CE_NOTE, ddi_get_parent(vlun->svl_dip),
3390 		    "!%s (%s%d): Waiting for externally initiated failover "
3391 		    "to complete", ddi_pathname(vlun->svl_dip, path),
3392 		    ddi_driver_name(vlun->svl_dip),
3393 		    ddi_get_instance(vlun->svl_dip));
3394 		kmem_free(path, MAXPATHLEN);
3395 		swarg = kmem_alloc(sizeof (*swarg), KM_NOSLEEP);
3396 		if (swarg == NULL) {
3397 			VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_handle_ext_fo: "
3398 			    "request packet allocation for %s failed....\n",
3399 			    vlun->svl_lun_wwn));
3400 			VHCI_RELEASE_LUN(vlun);
3401 			return (PKT_RETURN);
3402 		}
3403 		swarg->svs_svp = svp;
3404 		swarg->svs_tos = ddi_get_time();
3405 		swarg->svs_pi = vpkt->vpkt_path;
3406 		swarg->svs_release_lun = 0;
3407 		swarg->svs_done = 0;
3408 		/*
3409 		 * place a hold on the path...we don't want it to
3410 		 * vanish while scsi_watch is in progress
3411 		 */
3412 		mdi_hold_path(vpkt->vpkt_path);
3413 		svp->svp_sw_token = scsi_watch_request_submit(svp->svp_psd,
3414 		    VHCI_FOWATCH_INTERVAL, SENSE_LENGTH, vhci_efo_watch_cb,
3415 		    (caddr_t)swarg);
3416 	}
3417 	return (BUSY_RETURN);
3418 }
3419 
3420 /*
3421  * vhci_efo_watch_cb:
3422  *	Callback from scsi_watch request to check the failover status.
3423  *	Completion is either due to successful failover or timeout.
3424  *	Upon successful completion, vhci_update_path_states is called.
3425  *	For timeout condition, vhci_efo_done is called.
3426  *	Always returns 0 to scsi_watch to keep retrying till vhci_efo_done
3427  *	terminates this request properly in a separate thread.
3428  */
3429 
3430 static int
3431 vhci_efo_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
3432 {
3433 	struct scsi_status		*statusp = resultp->statusp;
3434 	struct scsi_extended_sense	*sensep = resultp->sensep;
3435 	struct scsi_pkt			*pkt = resultp->pkt;
3436 	scsi_vhci_swarg_t		*swarg;
3437 	scsi_vhci_priv_t		*svp;
3438 	scsi_vhci_lun_t			*vlun;
3439 	struct scsi_vhci		*vhci;
3440 	dev_info_t			*vdip;
3441 	int				rval, updt_paths;
3442 
3443 	swarg = (scsi_vhci_swarg_t *)(uintptr_t)arg;
3444 	svp = swarg->svs_svp;
3445 	if (swarg->svs_done) {
3446 		/*
3447 		 * Already completed failover or timedout.
3448 		 * Waiting for vhci_efo_done to terminate this scsi_watch.
3449 		 */
3450 		return (0);
3451 	}
3452 
3453 	ASSERT(svp != NULL);
3454 	vlun = svp->svp_svl;
3455 	ASSERT(vlun != NULL);
3456 	ASSERT(VHCI_LUN_IS_HELD(vlun));
3457 	vlun->svl_efo_update_path = 0;
3458 	vdip = ddi_get_parent(vlun->svl_dip);
3459 	vhci = ddi_get_soft_state(vhci_softstate,
3460 	    ddi_get_instance(vdip));
3461 
3462 	updt_paths = 0;
3463 
3464 	if (pkt->pkt_reason != CMD_CMPLT) {
3465 		if ((ddi_get_time() - swarg->svs_tos) >= VHCI_EXTFO_TIMEOUT) {
3466 			swarg->svs_release_lun = 1;
3467 			goto done;
3468 		}
3469 		return (0);
3470 	}
3471 	if (*((unsigned char *)statusp) == STATUS_CHECK) {
3472 		rval = (*(vlun->svl_fops->sfo_analyze_sense))
3473 		    (svp->svp_psd, sensep, vlun->svl_fops_ctpriv);
3474 		switch (rval) {
3475 			/*
3476 			 * Only update path states in case path is definitely
3477 			 * inactive, or no failover occurred.  For all other
3478 			 * check conditions continue pinging.  A unexpected
3479 			 * check condition shouldn't cause pinging to complete
3480 			 * prematurely.
3481 			 */
3482 			case SCSI_SENSE_INACTIVE:
3483 			case SCSI_SENSE_NOFAILOVER:
3484 				updt_paths = 1;
3485 				break;
3486 			default:
3487 				if ((ddi_get_time() - swarg->svs_tos)
3488 				    >= VHCI_EXTFO_TIMEOUT) {
3489 					swarg->svs_release_lun = 1;
3490 					goto done;
3491 				}
3492 				return (0);
3493 		}
3494 	} else if (*((unsigned char *)statusp) ==
3495 	    STATUS_RESERVATION_CONFLICT) {
3496 		updt_paths = 1;
3497 	} else if ((*((unsigned char *)statusp)) &
3498 	    (STATUS_BUSY | STATUS_QFULL)) {
3499 		return (0);
3500 	}
3501 	if ((*((unsigned char *)statusp) == STATUS_GOOD) ||
3502 	    (updt_paths == 1)) {
3503 		/*
3504 		 * we got here because we had detected an
3505 		 * externally initiated failover; things
3506 		 * have settled down now, so let's
3507 		 * start up a task to update the
3508 		 * path states and target port group
3509 		 */
3510 		vlun->svl_efo_update_path = 1;
3511 		swarg->svs_done = 1;
3512 		vlun->svl_swarg = swarg;
3513 		vlun->svl_flags |= VLUN_UPDATE_TPG;
3514 		(void) taskq_dispatch(vhci->vhci_update_pathstates_taskq,
3515 		    vhci_update_pathstates, (void *)vlun,
3516 		    KM_SLEEP);
3517 		return (0);
3518 	}
3519 	if ((ddi_get_time() - swarg->svs_tos) >= VHCI_EXTFO_TIMEOUT) {
3520 		swarg->svs_release_lun = 1;
3521 		goto done;
3522 	}
3523 	return (0);
3524 done:
3525 	swarg->svs_done = 1;
3526 	(void) taskq_dispatch(vhci->vhci_taskq,
3527 	    vhci_efo_done, (void *)swarg, KM_SLEEP);
3528 	return (0);
3529 }
3530 
3531 /*
3532  * vhci_efo_done:
3533  *	cleanly terminates scsi_watch and free up resources.
3534  *	Called as taskq function in vhci_efo_watch_cb for EFO timeout condition
3535  *	or by vhci_update_path_states invoked during external initiated
3536  *	failover completion.
3537  */
3538 static void
3539 vhci_efo_done(void *arg)
3540 {
3541 	scsi_vhci_lun_t			*vlun;
3542 	scsi_vhci_swarg_t		*swarg = (scsi_vhci_swarg_t *)arg;
3543 	scsi_vhci_priv_t		*svp = swarg->svs_svp;
3544 	ASSERT(svp);
3545 
3546 	vlun = svp->svp_svl;
3547 	ASSERT(vlun);
3548 
3549 	/* Wait for clean termination of scsi_watch */
3550 	(void) scsi_watch_request_terminate(svp->svp_sw_token,
3551 	    SCSI_WATCH_TERMINATE_WAIT);
3552 	svp->svp_sw_token = NULL;
3553 
3554 	/* release path and freeup resources to indicate failover completion */
3555 	mdi_rele_path(swarg->svs_pi);
3556 	if (swarg->svs_release_lun) {
3557 		VHCI_RELEASE_LUN(vlun);
3558 	}
3559 	kmem_free((void *)swarg, sizeof (*swarg));
3560 }
3561 
3562 /*
3563  * Update the path states
3564  * vlun should be HELD when this is invoked.
3565  * Calls vhci_efo_done to cleanup resources allocated for EFO.
3566  */
3567 void
3568 vhci_update_pathstates(void *arg)
3569 {
3570 	mdi_pathinfo_t			*pip, *npip;
3571 	dev_info_t			*dip, *pdip;
3572 	struct scsi_failover_ops	*fo;
3573 	struct scsi_vhci_priv		*svp;
3574 	struct scsi_device		*psd;
3575 	struct scsi_path_opinfo		opinfo;
3576 	char				*pclass, *tptr;
3577 	struct scsi_vhci_lun		*vlun = (struct scsi_vhci_lun *)arg;
3578 	int				sps; /* mdi_select_path() status */
3579 	char				*cpath, *dpath;
3580 	struct scsi_vhci		*vhci;
3581 	struct scsi_pkt			*pkt;
3582 	struct buf			*bp;
3583 	int				reserve_conflict = 0;
3584 
3585 	ASSERT(VHCI_LUN_IS_HELD(vlun));
3586 	dip  = vlun->svl_dip;
3587 	pip = npip = NULL;
3588 
3589 	vhci = ddi_get_soft_state(vhci_softstate,
3590 	    ddi_get_instance(ddi_get_parent(dip)));
3591 
3592 	sps = mdi_select_path(dip, NULL, (MDI_SELECT_ONLINE_PATH |
3593 	    MDI_SELECT_STANDBY_PATH), NULL, &npip);
3594 	if ((npip == NULL) || (sps != MDI_SUCCESS)) {
3595 		goto done;
3596 	}
3597 
3598 	fo = vlun->svl_fops;
3599 	do {
3600 		pip = npip;
3601 		svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
3602 		psd = svp->svp_psd;
3603 		if ((*fo->sfo_path_get_opinfo)(psd, &opinfo,
3604 		    vlun->svl_fops_ctpriv) != 0) {
3605 			sps = mdi_select_path(dip, NULL,
3606 			    (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH),
3607 			    pip, &npip);
3608 			mdi_rele_path(pip);
3609 			continue;
3610 		}
3611 
3612 		if (mdi_prop_lookup_string(pip, "path-class", &pclass) !=
3613 		    MDI_SUCCESS) {
3614 			VHCI_DEBUG(1, (CE_NOTE, NULL,
3615 			    "!vhci_update_pathstates: prop lookup failed for "
3616 			    "path 0x%p\n", (void *)pip));
3617 			sps = mdi_select_path(dip, NULL,
3618 			    (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH),
3619 			    pip, &npip);
3620 			mdi_rele_path(pip);
3621 			continue;
3622 		}
3623 
3624 		/*
3625 		 * Need to update the "path-class" property
3626 		 * value in the device tree if different
3627 		 * from the existing value.
3628 		 */
3629 		if (strcmp(pclass, opinfo.opinfo_path_attr) != 0) {
3630 			(void) mdi_prop_update_string(pip, "path-class",
3631 			    opinfo.opinfo_path_attr);
3632 		}
3633 
3634 		/*
3635 		 * Only change the state if needed. i.e. Don't call
3636 		 * mdi_pi_set_state to ONLINE a path if its already
3637 		 * ONLINE. Same for STANDBY paths.
3638 		 */
3639 
3640 		if ((opinfo.opinfo_path_state == SCSI_PATH_ACTIVE ||
3641 		    opinfo.opinfo_path_state == SCSI_PATH_ACTIVE_NONOPT)) {
3642 			if (!(MDI_PI_IS_ONLINE(pip))) {
3643 				VHCI_DEBUG(1, (CE_NOTE, NULL,
3644 				    "!vhci_update_pathstates: marking path"
3645 				    " 0x%p as ONLINE\n", (void *)pip));
3646 				pdip = mdi_pi_get_phci(pip);
3647 				cpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3648 				dpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3649 				vhci_log(CE_NOTE, ddi_get_parent(dip), "!%s"
3650 				    " (%s%d): path %s (%s%d) target address %s"
3651 				    " is now ONLINE because of"
3652 				    " an externally initiated failover",
3653 				    ddi_pathname(dip, cpath),
3654 				    ddi_driver_name(dip),
3655 				    ddi_get_instance(dip),
3656 				    ddi_pathname(pdip, dpath),
3657 				    ddi_driver_name(pdip),
3658 				    ddi_get_instance(pdip),
3659 				    mdi_pi_get_addr(pip));
3660 				kmem_free(cpath, MAXPATHLEN);
3661 				kmem_free(dpath, MAXPATHLEN);
3662 				mdi_pi_set_state(pip,
3663 				    MDI_PATHINFO_STATE_ONLINE);
3664 				mdi_pi_set_preferred(pip,
3665 				    opinfo.opinfo_preferred);
3666 				tptr = kmem_alloc(strlen
3667 				    (opinfo.opinfo_path_attr)+1, KM_SLEEP);
3668 				(void) strlcpy(tptr, opinfo.opinfo_path_attr,
3669 				    (strlen(opinfo.opinfo_path_attr)+1));
3670 				mutex_enter(&vlun->svl_mutex);
3671 				if (vlun->svl_active_pclass != NULL) {
3672 					kmem_free(vlun->svl_active_pclass,
3673 					    strlen(vlun->svl_active_pclass)+1);
3674 				}
3675 				vlun->svl_active_pclass = tptr;
3676 				if (vlun->svl_waiting_for_activepath) {
3677 					vlun->svl_waiting_for_activepath = 0;
3678 				}
3679 				mutex_exit(&vlun->svl_mutex);
3680 				/* Check for Reservation Conflict */
3681 				bp = scsi_alloc_consistent_buf(
3682 				    &svp->svp_psd->sd_address,
3683 				    (struct buf *)NULL, DEV_BSIZE, B_READ,
3684 				    NULL, NULL);
3685 				if (!bp) {
3686 					VHCI_DEBUG(1, (CE_NOTE, NULL,
3687 					    "vhci_update_pathstates: "
3688 					    "!No resources (buf)\n"));
3689 					mdi_rele_path(pip);
3690 					goto done;
3691 				}
3692 				pkt = scsi_init_pkt(&svp->svp_psd->sd_address,
3693 				    NULL, bp, CDB_GROUP1,
3694 				    sizeof (struct scsi_arq_status), 0,
3695 				    PKT_CONSISTENT, NULL, NULL);
3696 				if (pkt) {
3697 					(void) scsi_setup_cdb((union scsi_cdb *)
3698 					    (uintptr_t)pkt->pkt_cdbp,
3699 					    SCMD_READ, 1, 1, 0);
3700 					pkt->pkt_time = 3*30;
3701 					pkt->pkt_flags = FLAG_NOINTR;
3702 					if ((scsi_transport(pkt) ==
3703 					    TRAN_ACCEPT) && (pkt->pkt_reason
3704 					    == CMD_CMPLT) && (SCBP_C(pkt) ==
3705 					    STATUS_RESERVATION_CONFLICT)) {
3706 						reserve_conflict = 1;
3707 					}
3708 					scsi_destroy_pkt(pkt);
3709 				}
3710 				scsi_free_consistent_buf(bp);
3711 			} else if (MDI_PI_IS_ONLINE(pip)) {
3712 				if (strcmp(pclass, opinfo.opinfo_path_attr)
3713 				    != 0) {
3714 					mdi_pi_set_preferred(pip,
3715 					    opinfo.opinfo_preferred);
3716 					mutex_enter(&vlun->svl_mutex);
3717 					if (vlun->svl_active_pclass == NULL ||
3718 					    strcmp(opinfo.opinfo_path_attr,
3719 					    vlun->svl_active_pclass) != 0) {
3720 						mutex_exit(&vlun->svl_mutex);
3721 						tptr = kmem_alloc(strlen
3722 						    (opinfo.opinfo_path_attr)+1,
3723 						    KM_SLEEP);
3724 						(void) strlcpy(tptr,
3725 						    opinfo.opinfo_path_attr,
3726 						    (strlen
3727 						    (opinfo.opinfo_path_attr)
3728 						    +1));
3729 						mutex_enter(&vlun->svl_mutex);
3730 					} else {
3731 						/*
3732 						 * No need to update
3733 						 * svl_active_pclass
3734 						 */
3735 						tptr = NULL;
3736 						mutex_exit(&vlun->svl_mutex);
3737 					}
3738 					if (tptr) {
3739 						if (vlun->svl_active_pclass
3740 						    != NULL) {
3741 							kmem_free(vlun->
3742 							    svl_active_pclass,
3743 							    strlen(vlun->
3744 							    svl_active_pclass)
3745 							    +1);
3746 						}
3747 						vlun->svl_active_pclass = tptr;
3748 						mutex_exit(&vlun->svl_mutex);
3749 					}
3750 				}
3751 			}
3752 		} else if ((opinfo.opinfo_path_state == SCSI_PATH_INACTIVE) &&
3753 		    !(MDI_PI_IS_STANDBY(pip))) {
3754 			VHCI_DEBUG(1, (CE_NOTE, NULL,
3755 			    "!vhci_update_pathstates: marking path"
3756 			    " 0x%p as STANDBY\n", (void *)pip));
3757 			pdip = mdi_pi_get_phci(pip);
3758 			cpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3759 			dpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3760 			vhci_log(CE_NOTE, ddi_get_parent(dip), "!%s"
3761 			    " (%s%d): path %s (%s%d) target address %s"
3762 			    " is now STANDBY because of"
3763 			    " an externally initiated failover",
3764 			    ddi_pathname(dip, cpath),
3765 			    ddi_driver_name(dip),
3766 			    ddi_get_instance(dip),
3767 			    ddi_pathname(pdip, dpath),
3768 			    ddi_driver_name(pdip),
3769 			    ddi_get_instance(pdip),
3770 			    mdi_pi_get_addr(pip));
3771 			kmem_free(cpath, MAXPATHLEN);
3772 			kmem_free(dpath, MAXPATHLEN);
3773 			mdi_pi_set_state(pip,
3774 			    MDI_PATHINFO_STATE_STANDBY);
3775 			mdi_pi_set_preferred(pip,
3776 			    opinfo.opinfo_preferred);
3777 			mutex_enter(&vlun->svl_mutex);
3778 			if (vlun->svl_active_pclass != NULL) {
3779 				if (strcmp(vlun->svl_active_pclass,
3780 				    opinfo.opinfo_path_attr) == 0) {
3781 					kmem_free(vlun->
3782 					    svl_active_pclass,
3783 					    strlen(vlun->
3784 					    svl_active_pclass)+1);
3785 					vlun->svl_active_pclass = NULL;
3786 				}
3787 			}
3788 			mutex_exit(&vlun->svl_mutex);
3789 		}
3790 		(void) mdi_prop_free(pclass);
3791 		sps = mdi_select_path(dip, NULL,
3792 		    (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH),
3793 		    pip, &npip);
3794 		mdi_rele_path(pip);
3795 
3796 	} while ((npip != NULL) && (sps == MDI_SUCCESS));
3797 
3798 	/*
3799 	 * Check to see if this vlun has an active SCSI-II RESERVE.  If so
3800 	 * clear the reservation by sending a reset, so the host doesn't
3801 	 * receive a reservation conflict.
3802 	 * Reset VLUN_RESERVE_ACTIVE_FLG for this vlun. Also notify ssd
3803 	 * of the reset, explicitly.
3804 	 */
3805 	if (vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) {
3806 		if (reserve_conflict && (vlun->svl_xlf_capable == 0)) {
3807 			(void) vhci_recovery_reset(vlun,
3808 			    &svp->svp_psd->sd_address, FALSE,
3809 			    VHCI_DEPTH_TARGET);
3810 		}
3811 		vlun->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG;
3812 		mutex_enter(&vhci->vhci_mutex);
3813 		scsi_hba_reset_notify_callback(&vhci->vhci_mutex,
3814 		    &vhci->vhci_reset_notify_listf);
3815 		mutex_exit(&vhci->vhci_mutex);
3816 	}
3817 	if (vlun->svl_flags & VLUN_UPDATE_TPG) {
3818 		/*
3819 		 * Update the AccessState of related MP-API TPGs
3820 		 */
3821 		(void) vhci_mpapi_update_tpg_acc_state_for_lu(vhci, vlun);
3822 		vlun->svl_flags &= ~VLUN_UPDATE_TPG;
3823 	}
3824 done:
3825 	if (vlun->svl_efo_update_path) {
3826 		vlun->svl_efo_update_path = 0;
3827 		vhci_efo_done(vlun->svl_swarg);
3828 		vlun->svl_swarg = 0;
3829 	}
3830 	VHCI_RELEASE_LUN(vlun);
3831 }
3832 
3833 /* ARGSUSED */
3834 static int
3835 vhci_pathinfo_init(dev_info_t *vdip, mdi_pathinfo_t *pip, int flags)
3836 {
3837 	scsi_hba_tran_t		*hba = NULL;
3838 	struct scsi_device	*psd = NULL;
3839 	scsi_vhci_lun_t		*vlun = NULL;
3840 	dev_info_t		*pdip = NULL;
3841 	dev_info_t		*tgt_dip;
3842 	struct scsi_vhci	*vhci;
3843 	char			*guid;
3844 	scsi_vhci_priv_t	*svp = NULL;
3845 	int			rval = MDI_FAILURE;
3846 	int			vlun_alloced = 0;
3847 
3848 	ASSERT(vdip != NULL);
3849 	ASSERT(pip != NULL);
3850 
3851 	vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip));
3852 	ASSERT(vhci != NULL);
3853 
3854 	pdip = mdi_pi_get_phci(pip);
3855 	ASSERT(pdip != NULL);
3856 
3857 	hba = ddi_get_driver_private(pdip);
3858 	ASSERT(hba != NULL);
3859 
3860 	tgt_dip = mdi_pi_get_client(pip);
3861 	ASSERT(tgt_dip != NULL);
3862 
3863 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, tgt_dip, PROPFLAGS,
3864 	    MDI_CLIENT_GUID_PROP, &guid) != DDI_SUCCESS) {
3865 		VHCI_DEBUG(1, (CE_WARN, NULL,
3866 		    "vhci_pathinfo_init: lun guid property failed"));
3867 		goto failure;
3868 	}
3869 
3870 	vlun = vhci_lun_lookup_alloc(tgt_dip, guid, &vlun_alloced);
3871 	ddi_prop_free(guid);
3872 
3873 	vlun->svl_dip = tgt_dip;
3874 
3875 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
3876 	svp->svp_svl = vlun;
3877 
3878 	vlun->svl_lb_policy_save = mdi_get_lb_policy(tgt_dip);
3879 	mutex_init(&svp->svp_mutex, NULL, MUTEX_DRIVER, NULL);
3880 	cv_init(&svp->svp_cv, NULL, CV_DRIVER, NULL);
3881 
3882 	psd = kmem_zalloc(sizeof (*psd), KM_SLEEP);
3883 	mutex_init(&psd->sd_mutex, NULL, MUTEX_DRIVER, NULL);
3884 
3885 	/*
3886 	 * Clone transport structure if requested, so
3887 	 * Self enumerating HBAs always need to use cloning
3888 	 */
3889 
3890 	if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
3891 		scsi_hba_tran_t	*clone =
3892 		    kmem_alloc(sizeof (scsi_hba_tran_t), KM_SLEEP);
3893 		bcopy(hba, clone, sizeof (scsi_hba_tran_t));
3894 		hba = clone;
3895 		hba->tran_sd = psd;
3896 	} else {
3897 		ASSERT(hba->tran_sd == NULL);
3898 	}
3899 	psd->sd_dev = tgt_dip;
3900 	psd->sd_address.a_hba_tran = hba;
3901 	psd->sd_private = (caddr_t)pip;
3902 	svp->svp_psd = psd;
3903 	mdi_pi_set_vhci_private(pip, (caddr_t)svp);
3904 
3905 	/*
3906 	 * call hba's target init entry point if it exists
3907 	 */
3908 	if (hba->tran_tgt_init != NULL) {
3909 		if ((rval = (*hba->tran_tgt_init)(pdip, tgt_dip,
3910 		    hba, psd)) != DDI_SUCCESS) {
3911 			VHCI_DEBUG(1, (CE_WARN, pdip,
3912 			    "!vhci_pathinfo_init: tran_tgt_init failed for "
3913 			    "path=0x%p rval=%x", (void *)pip, rval));
3914 			goto failure;
3915 		}
3916 	}
3917 
3918 	svp->svp_new_path = 1;
3919 
3920 	psd->sd_inq = NULL;
3921 
3922 	VHCI_DEBUG(4, (CE_NOTE, NULL, "!vhci_pathinfo_init: path:%p\n",
3923 	    (void *)pip));
3924 	return (MDI_SUCCESS);
3925 
3926 failure:
3927 	if (psd) {
3928 		mutex_destroy(&psd->sd_mutex);
3929 		kmem_free(psd, sizeof (*psd));
3930 	}
3931 	if (svp) {
3932 		mdi_pi_set_vhci_private(pip, NULL);
3933 		mutex_destroy(&svp->svp_mutex);
3934 		cv_destroy(&svp->svp_cv);
3935 		kmem_free(svp, sizeof (*svp));
3936 	}
3937 	if (hba && hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE)
3938 		kmem_free(hba, sizeof (scsi_hba_tran_t));
3939 
3940 	if (vlun_alloced)
3941 		vhci_lun_free(tgt_dip);
3942 
3943 	return (rval);
3944 }
3945 
3946 /* ARGSUSED */
3947 static int
3948 vhci_pathinfo_uninit(dev_info_t *vdip, mdi_pathinfo_t *pip, int flags)
3949 {
3950 	scsi_hba_tran_t		*hba = NULL;
3951 	struct scsi_device	*psd = NULL;
3952 	dev_info_t		*pdip = NULL;
3953 	dev_info_t		*cdip = NULL;
3954 	scsi_vhci_priv_t	*svp = NULL;
3955 
3956 	ASSERT(vdip != NULL);
3957 	ASSERT(pip != NULL);
3958 
3959 	pdip = mdi_pi_get_phci(pip);
3960 	ASSERT(pdip != NULL);
3961 
3962 	cdip = mdi_pi_get_client(pip);
3963 	ASSERT(cdip != NULL);
3964 
3965 	hba = ddi_get_driver_private(pdip);
3966 	ASSERT(hba != NULL);
3967 
3968 	vhci_mpapi_set_path_state(vdip, pip, MP_DRVR_PATH_STATE_REMOVED);
3969 	svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
3970 	if (svp == NULL) {
3971 		/* path already freed. Nothing to do. */
3972 		return (MDI_SUCCESS);
3973 	}
3974 
3975 	psd = svp->svp_psd;
3976 	ASSERT(psd != NULL);
3977 
3978 	if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
3979 		hba = psd->sd_address.a_hba_tran;
3980 		ASSERT(hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE);
3981 		ASSERT(hba->tran_sd == psd);
3982 	} else {
3983 		ASSERT(hba->tran_sd == NULL);
3984 	}
3985 
3986 	if (hba->tran_tgt_free != NULL) {
3987 		(*hba->tran_tgt_free) (pdip, cdip, hba, psd);
3988 	}
3989 	mutex_destroy(&psd->sd_mutex);
3990 	if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
3991 		kmem_free(hba, sizeof (*hba));
3992 	}
3993 
3994 	mdi_pi_set_vhci_private(pip, NULL);
3995 	kmem_free((caddr_t)psd, sizeof (*psd));
3996 
3997 	mutex_destroy(&svp->svp_mutex);
3998 	cv_destroy(&svp->svp_cv);
3999 	kmem_free((caddr_t)svp, sizeof (*svp));
4000 
4001 	/*
4002 	 * If this is the last path to the client,
4003 	 * then free up the vlun as well.
4004 	 */
4005 	if (mdi_client_get_path_count(cdip) == 1) {
4006 		vhci_lun_free(cdip);
4007 	}
4008 
4009 	VHCI_DEBUG(4, (CE_NOTE, NULL, "!vhci_pathinfo_uninit: path=0x%p\n",
4010 	    (void *)pip));
4011 	return (MDI_SUCCESS);
4012 }
4013 
4014 /* ARGSUSED */
4015 static int
4016 vhci_pathinfo_state_change(dev_info_t *vdip, mdi_pathinfo_t *pip,
4017     mdi_pathinfo_state_t state, uint32_t ext_state, int flags)
4018 {
4019 	int			rval = MDI_SUCCESS;
4020 	scsi_vhci_priv_t	*svp;
4021 	scsi_vhci_lun_t		*vlun;
4022 	int			held;
4023 	int			op = (flags & 0xf00) >> 8;
4024 	struct scsi_vhci	*vhci;
4025 
4026 	vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip));
4027 
4028 	if (flags & MDI_EXT_STATE_CHANGE) {
4029 		/*
4030 		 * We do not want to issue any commands down the path in case
4031 		 * sync flag is set. Lower layers might not be ready to accept
4032 		 * any I/O commands.
4033 		 */
4034 		if (op == DRIVER_DISABLE)
4035 			return (MDI_SUCCESS);
4036 
4037 		svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
4038 		if (svp == NULL) {
4039 			return (MDI_FAILURE);
4040 		}
4041 		vlun = svp->svp_svl;
4042 
4043 		if (flags & MDI_BEFORE_STATE_CHANGE) {
4044 			/*
4045 			 * Hold the LUN.
4046 			 */
4047 			VHCI_HOLD_LUN(vlun, VH_SLEEP, held);
4048 			if (flags & MDI_DISABLE_OP)  {
4049 				/*
4050 				 * Issue scsi reset if it happens to be
4051 				 * reserved path.
4052 				 */
4053 				if (vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) {
4054 					/*
4055 					 * if reservation pending on
4056 					 * this path, dont' mark the
4057 					 * path busy
4058 					 */
4059 					if (op == DRIVER_DISABLE_TRANSIENT) {
4060 						VHCI_DEBUG(1, (CE_NOTE, NULL,
4061 						    "!vhci_pathinfo"
4062 						    "_state_change (pip:%p): "
4063 						    " reservation: fail busy\n",
4064 						    (void *)pip));
4065 						return (MDI_FAILURE);
4066 					}
4067 					if (pip == vlun->svl_resrv_pip) {
4068 						if (vhci_recovery_reset(
4069 						    svp->svp_svl,
4070 						    &svp->svp_psd->sd_address,
4071 						    TRUE,
4072 						    VHCI_DEPTH_TARGET) == 0) {
4073 							VHCI_DEBUG(1,
4074 							    (CE_NOTE, NULL,
4075 							    "!vhci_pathinfo"
4076 							    "_state_change "
4077 							    " (pip:%p): "
4078 							    "reset failed, "
4079 							    "give up!\n",
4080 							    (void *)pip));
4081 						}
4082 						vlun->svl_flags &=
4083 						    ~VLUN_RESERVE_ACTIVE_FLG;
4084 					}
4085 				}
4086 			} else if (flags & MDI_ENABLE_OP)  {
4087 				if (((vhci->vhci_conf_flags &
4088 				    VHCI_CONF_FLAGS_AUTO_FAILBACK) ==
4089 				    VHCI_CONF_FLAGS_AUTO_FAILBACK) &&
4090 				    MDI_PI_IS_USER_DISABLE(pip) &&
4091 				    MDI_PI_IS_STANDBY(pip)) {
4092 					struct scsi_failover_ops	*fo;
4093 					char *best_pclass, *pclass = NULL;
4094 					int  best_class, rv;
4095 					/*
4096 					 * Failback if enabling a standby path
4097 					 * and it is the primary class or
4098 					 * preferred class
4099 					 */
4100 					best_class = mdi_pi_get_preferred(pip);
4101 					if (best_class == 0) {
4102 						/*
4103 						 * if not preferred - compare
4104 						 * path-class with class
4105 						 */
4106 						fo = vlun->svl_fops;
4107 						(*fo->sfo_pathclass_next)(NULL,
4108 						    &best_pclass,
4109 						    vlun->svl_fops_ctpriv);
4110 						pclass = NULL;
4111 						rv = mdi_prop_lookup_string(pip,
4112 						    "path-class", &pclass);
4113 						if (rv != MDI_SUCCESS ||
4114 						    pclass == NULL) {
4115 							vhci_log(CE_NOTE, vdip,
4116 							    "!path-class "
4117 							    " lookup "
4118 							    "failed. rv: %d"
4119 							    "class: %p", rv,
4120 							    (void *)pclass);
4121 						} else if (strncmp(pclass,
4122 						    best_pclass,
4123 						    strlen(best_pclass)) == 0) {
4124 							best_class = 1;
4125 						}
4126 						if (rv == MDI_SUCCESS &&
4127 						    pclass != NULL) {
4128 							rv = mdi_prop_free(
4129 							    pclass);
4130 							if (rv !=
4131 							    DDI_PROP_SUCCESS) {
4132 								vhci_log(
4133 								    CE_NOTE,
4134 								    vdip,
4135 								    "!path-"
4136 								    "class"
4137 								    " free"
4138 								    " failed"
4139 								    " rv: %d"
4140 								    " class: "
4141 								    "%p",
4142 								    rv,
4143 								    (void *)
4144 								    pclass);
4145 							}
4146 						}
4147 					}
4148 					if (best_class == 1) {
4149 						VHCI_DEBUG(1, (CE_NOTE, NULL,
4150 						    "preferred path: %p "
4151 						    "USER_DISABLE->USER_ENABLE "
4152 						    "transition for lun %s\n",
4153 						    (void *)pip,
4154 						    vlun->svl_lun_wwn));
4155 						(void) taskq_dispatch(
4156 						    vhci->vhci_taskq,
4157 						    vhci_initiate_auto_failback,
4158 						    (void *) vlun, KM_SLEEP);
4159 					}
4160 				}
4161 				/*
4162 				 * if PGR is active, revalidate key and
4163 				 * register on this path also, if key is
4164 				 * still valid
4165 				 */
4166 				sema_p(&vlun->svl_pgr_sema);
4167 				if (vlun->svl_pgr_active)
4168 					(void)
4169 					    vhci_pgr_validate_and_register(svp);
4170 				sema_v(&vlun->svl_pgr_sema);
4171 				/*
4172 				 * Inform target driver about any
4173 				 * reservations to be reinstated if target
4174 				 * has dropped reservation during the busy
4175 				 * period.
4176 				 */
4177 				mutex_enter(&vhci->vhci_mutex);
4178 				scsi_hba_reset_notify_callback(
4179 				    &vhci->vhci_mutex,
4180 				    &vhci->vhci_reset_notify_listf);
4181 				mutex_exit(&vhci->vhci_mutex);
4182 			}
4183 		}
4184 		if (flags & MDI_AFTER_STATE_CHANGE) {
4185 			if (flags & MDI_ENABLE_OP)  {
4186 				mutex_enter(&vhci_global_mutex);
4187 				cv_broadcast(&vhci_cv);
4188 				mutex_exit(&vhci_global_mutex);
4189 			}
4190 			if (vlun->svl_setcap_done) {
4191 				(void) vhci_pHCI_cap(&svp->svp_psd->sd_address,
4192 				    "sector-size", vlun->svl_sector_size,
4193 				    1, pip);
4194 			}
4195 
4196 			/*
4197 			 * Release the LUN
4198 			 */
4199 			VHCI_RELEASE_LUN(vlun);
4200 
4201 			/*
4202 			 * Path transition is complete.
4203 			 * Run callback to indicate target driver to
4204 			 * retry to prevent IO starvation.
4205 			 */
4206 			if (scsi_callback_id != 0) {
4207 				ddi_run_callback(&scsi_callback_id);
4208 			}
4209 		}
4210 	} else {
4211 		switch (state) {
4212 		case MDI_PATHINFO_STATE_ONLINE:
4213 			rval = vhci_pathinfo_online(vdip, pip, flags);
4214 			break;
4215 
4216 		case MDI_PATHINFO_STATE_OFFLINE:
4217 			rval = vhci_pathinfo_offline(vdip, pip, flags);
4218 			break;
4219 
4220 		default:
4221 			break;
4222 		}
4223 		/*
4224 		 * Path transition is complete.
4225 		 * Run callback to indicate target driver to
4226 		 * retry to prevent IO starvation.
4227 		 */
4228 		if ((rval == MDI_SUCCESS) && (scsi_callback_id != 0)) {
4229 			ddi_run_callback(&scsi_callback_id);
4230 		}
4231 		return (rval);
4232 	}
4233 
4234 	return (MDI_SUCCESS);
4235 }
4236 
4237 /*
4238  * Parse the mpxio load balancing options. The datanameptr
4239  * will point to a string containing the load-balance-options value.
4240  * The load-balance-options value will be a property that
4241  * defines the load-balance algorithm and any arguments to that
4242  * algorithm.
4243  * For example:
4244  * device-type-mpxio-options-list=
4245  * "device-type=SUN    SENA", "load-balance-options=logical-block-options"
4246  * "device-type=SUN     SE6920", "round-robin-options";
4247  * logical-block-options="load-balance=logical-block", "region-size=15";
4248  * round-robin-options="load-balance=round-robin";
4249  *
4250  * If the load-balance is not defined the load balance algorithm will
4251  * default to the global setting. There will be default values assigned
4252  * to the arguments (region-size=18) and if an argument is one
4253  * that is not known, it will be ignored.
4254  */
4255 static void
4256 vhci_parse_mpxio_lb_options(dev_info_t *dip, dev_info_t *cdip,
4257 	caddr_t datanameptr)
4258 {
4259 	char			*dataptr, *next_entry;
4260 	caddr_t			config_list	= NULL;
4261 	int			config_list_len = 0, list_len = 0;
4262 	int			region_size = -1;
4263 	client_lb_t		load_balance;
4264 
4265 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, datanameptr,
4266 	    (caddr_t)&config_list, &config_list_len) != DDI_PROP_SUCCESS) {
4267 		return;
4268 	}
4269 
4270 	list_len = config_list_len;
4271 	next_entry = config_list;
4272 	while (config_list_len > 0) {
4273 		dataptr = next_entry;
4274 
4275 		if (strncmp(mdi_load_balance, dataptr,
4276 		    strlen(mdi_load_balance)) == 0) {
4277 			/* get the load-balance scheme */
4278 			dataptr += strlen(mdi_load_balance) + 1;
4279 			if (strcmp(dataptr, LOAD_BALANCE_PROP_RR) == 0) {
4280 				(void) mdi_set_lb_policy(cdip, LOAD_BALANCE_RR);
4281 				load_balance = LOAD_BALANCE_RR;
4282 			} else if (strcmp(dataptr,
4283 			    LOAD_BALANCE_PROP_LBA) == 0) {
4284 				(void) mdi_set_lb_policy(cdip,
4285 				    LOAD_BALANCE_LBA);
4286 				load_balance = LOAD_BALANCE_LBA;
4287 			} else if (strcmp(dataptr,
4288 			    LOAD_BALANCE_PROP_NONE) == 0) {
4289 				(void) mdi_set_lb_policy(cdip,
4290 				    LOAD_BALANCE_NONE);
4291 				load_balance = LOAD_BALANCE_NONE;
4292 			}
4293 		} else if (strncmp(dataptr, LOGICAL_BLOCK_REGION_SIZE,
4294 		    strlen(LOGICAL_BLOCK_REGION_SIZE)) == 0) {
4295 			int	i = 0;
4296 			char	*ptr;
4297 			char	*tmp;
4298 
4299 			tmp = dataptr + (strlen(LOGICAL_BLOCK_REGION_SIZE) + 1);
4300 			/* check for numeric value */
4301 			for (ptr = tmp; i < strlen(tmp); i++, ptr++) {
4302 				if (!isdigit(*ptr)) {
4303 					cmn_err(CE_WARN,
4304 					    "Illegal region size: %s."
4305 					    " Setting to default value: %d",
4306 					    tmp,
4307 					    LOAD_BALANCE_DEFAULT_REGION_SIZE);
4308 					region_size =
4309 					    LOAD_BALANCE_DEFAULT_REGION_SIZE;
4310 					break;
4311 				}
4312 			}
4313 			if (i >= strlen(tmp)) {
4314 				region_size = stoi(&tmp);
4315 			}
4316 			(void) mdi_set_lb_region_size(cdip, region_size);
4317 		}
4318 		config_list_len -= (strlen(next_entry) + 1);
4319 		next_entry += strlen(next_entry) + 1;
4320 	}
4321 #ifdef DEBUG
4322 	if ((region_size >= 0) && (load_balance != LOAD_BALANCE_LBA)) {
4323 		VHCI_DEBUG(1, (CE_NOTE, dip,
4324 		    "!vhci_parse_mpxio_lb_options: region-size: %d"
4325 		    "only valid for load-balance=logical-block\n",
4326 		    region_size));
4327 	}
4328 #endif
4329 	if ((region_size == -1) && (load_balance == LOAD_BALANCE_LBA)) {
4330 		VHCI_DEBUG(1, (CE_NOTE, dip,
4331 		    "!vhci_parse_mpxio_lb_options: No region-size"
4332 		    " defined load-balance=logical-block."
4333 		    " Default to: %d\n", LOAD_BALANCE_DEFAULT_REGION_SIZE));
4334 		(void) mdi_set_lb_region_size(cdip,
4335 		    LOAD_BALANCE_DEFAULT_REGION_SIZE);
4336 	}
4337 	if (list_len > 0) {
4338 		kmem_free(config_list, list_len);
4339 	}
4340 }
4341 
4342 /*
4343  * Parse the device-type-mpxio-options-list looking for the key of
4344  * "load-balance-options". If found, parse the load balancing options.
4345  * Check the comment of the vhci_get_device_type_mpxio_options()
4346  * for the device-type-mpxio-options-list.
4347  */
4348 static void
4349 vhci_parse_mpxio_options(dev_info_t *dip, dev_info_t *cdip,
4350 		caddr_t datanameptr, int list_len)
4351 {
4352 	char		*dataptr;
4353 	int		len;
4354 
4355 	/*
4356 	 * get the data list
4357 	 */
4358 	dataptr = datanameptr;
4359 	len = 0;
4360 	while (len < list_len &&
4361 	    strncmp(dataptr, DEVICE_TYPE_STR, strlen(DEVICE_TYPE_STR))
4362 	    != 0) {
4363 		if (strncmp(dataptr, LOAD_BALANCE_OPTIONS,
4364 		    strlen(LOAD_BALANCE_OPTIONS)) == 0) {
4365 			len += strlen(LOAD_BALANCE_OPTIONS) + 1;
4366 			dataptr += strlen(LOAD_BALANCE_OPTIONS) + 1;
4367 			vhci_parse_mpxio_lb_options(dip, cdip, dataptr);
4368 		}
4369 		len += strlen(dataptr) + 1;
4370 		dataptr += strlen(dataptr) + 1;
4371 	}
4372 }
4373 
4374 /*
4375  * Check the inquriy string returned from the device wiith the device-type
4376  * Check for the existence of the device-type-mpxio-options-list and
4377  * if found parse the list checking for a match with the device-type
4378  * value and the inquiry string returned from the device. If a match
4379  * is found, parse the mpxio options list. The format of the
4380  * device-type-mpxio-options-list is:
4381  * device-type-mpxio-options-list=
4382  * "device-type=SUN    SENA", "load-balance-options=logical-block-options"
4383  * "device-type=SUN     SE6920", "round-robin-options";
4384  * logical-block-options="load-balance=logical-block", "region-size=15";
4385  * round-robin-options="load-balance=round-robin";
4386  */
4387 void
4388 vhci_get_device_type_mpxio_options(dev_info_t *dip, dev_info_t *cdip,
4389 	struct scsi_device *devp)
4390 {
4391 
4392 	caddr_t			config_list	= NULL;
4393 	caddr_t			vidptr, datanameptr;
4394 	int			vidlen, dupletlen = 0;
4395 	int			config_list_len = 0, len;
4396 	struct scsi_inquiry	*inq = devp->sd_inq;
4397 
4398 	/*
4399 	 * look up the device-type-mpxio-options-list and walk thru
4400 	 * the list compare the vendor ids of the earlier inquiry command and
4401 	 * with those vids in the list if there is a match, lookup
4402 	 * the mpxio-options value
4403 	 */
4404 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
4405 	    MPXIO_OPTIONS_LIST,
4406 	    (caddr_t)&config_list, &config_list_len) == DDI_PROP_SUCCESS) {
4407 
4408 		/*
4409 		 * Compare vids in each duplet - if it matches,
4410 		 * parse the mpxio options list.
4411 		 */
4412 		for (len = config_list_len, vidptr = config_list; len > 0;
4413 		    len -= dupletlen) {
4414 
4415 			dupletlen = 0;
4416 
4417 			if (strlen(vidptr) != 0 &&
4418 			    strncmp(vidptr, DEVICE_TYPE_STR,
4419 			    strlen(DEVICE_TYPE_STR)) == 0) {
4420 				/* point to next duplet */
4421 				datanameptr = vidptr + strlen(vidptr) + 1;
4422 				/* add len of this duplet */
4423 				dupletlen += strlen(vidptr) + 1;
4424 				/* get to device type */
4425 				vidptr += strlen(DEVICE_TYPE_STR) + 1;
4426 				vidlen = strlen(vidptr);
4427 				if ((vidlen != 0) &&
4428 				    bcmp(inq->inq_vid, vidptr, vidlen) == 0) {
4429 					vhci_parse_mpxio_options(dip, cdip,
4430 					    datanameptr, len - dupletlen);
4431 					break;
4432 				}
4433 				/* get to next duplet */
4434 				vidptr += strlen(vidptr) + 1;
4435 			}
4436 			/* get to the next device-type */
4437 			while (len - dupletlen > 0 &&
4438 			    strlen(vidptr) != 0 &&
4439 			    strncmp(vidptr, DEVICE_TYPE_STR,
4440 			    strlen(DEVICE_TYPE_STR)) != 0) {
4441 				dupletlen += strlen(vidptr) + 1;
4442 				vidptr += strlen(vidptr) + 1;
4443 			}
4444 		}
4445 		if (config_list_len > 0) {
4446 			kmem_free(config_list, config_list_len);
4447 		}
4448 	}
4449 }
4450 
4451 static int
4452 vhci_update_pathinfo(struct scsi_device *psd,  mdi_pathinfo_t *pip,
4453 	struct scsi_failover_ops *fo,
4454 	scsi_vhci_lun_t		*vlun,
4455 	struct scsi_vhci	*vhci)
4456 {
4457 	struct scsi_path_opinfo		opinfo;
4458 	char				*pclass, *best_pclass;
4459 
4460 	if ((*fo->sfo_path_get_opinfo)(psd, &opinfo,
4461 	    vlun->svl_fops_ctpriv) != 0) {
4462 		VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_update_pathinfo: "
4463 		    "Failed to get operation info for path:%p\n", (void *)pip));
4464 		return (MDI_FAILURE);
4465 	}
4466 	/* set the xlf capable flag in the vlun for future use */
4467 	vlun->svl_xlf_capable = opinfo.opinfo_xlf_capable;
4468 	(void) mdi_prop_update_string(pip, "path-class",
4469 	    opinfo.opinfo_path_attr);
4470 
4471 	pclass = opinfo.opinfo_path_attr;
4472 	if (opinfo.opinfo_path_state == SCSI_PATH_ACTIVE) {
4473 		mutex_enter(&vlun->svl_mutex);
4474 		if (vlun->svl_active_pclass != NULL) {
4475 			if (strcmp(vlun->svl_active_pclass, pclass) != 0) {
4476 				mutex_exit(&vlun->svl_mutex);
4477 				/*
4478 				 * Externally initiated failover has happened;
4479 				 * force the path state to be STANDBY/ONLINE,
4480 				 * next IO will trigger failover and thus
4481 				 * sync-up the pathstates.  Reason we don't
4482 				 * sync-up immediately by invoking
4483 				 * vhci_update_pathstates() is because it
4484 				 * needs a VHCI_HOLD_LUN() and we don't
4485 				 * want to block here.
4486 				 *
4487 				 * Further, if the device is an ALUA device,
4488 				 * then failure to exactly match 'pclass' and
4489 				 * 'svl_active_pclass'(as is the case here)
4490 				 * indicates that the currently active path
4491 				 * is a 'non-optimized' path - which means
4492 				 * that 'svl_active_pclass' needs to be
4493 				 * replaced with opinfo.opinfo_path_state
4494 				 * value.
4495 				 */
4496 
4497 				if (SCSI_FAILOVER_IS_TPGS(vlun->svl_fops)) {
4498 					char	*tptr;
4499 
4500 					/*
4501 					 * The device is ALUA compliant. The
4502 					 * state need to be changed to online
4503 					 * rather than standby state which is
4504 					 * done typically for a asymmetric
4505 					 * device that is non ALUA compliant.
4506 					 */
4507 					mdi_pi_set_state(pip,
4508 					    MDI_PATHINFO_STATE_ONLINE);
4509 					tptr = kmem_alloc(strlen
4510 					    (opinfo.opinfo_path_attr)+1,
4511 					    KM_SLEEP);
4512 					(void) strlcpy(tptr,
4513 					    opinfo.opinfo_path_attr,
4514 					    (strlen(opinfo.opinfo_path_attr)
4515 					    +1));
4516 					mutex_enter(&vlun->svl_mutex);
4517 					kmem_free(vlun->svl_active_pclass,
4518 					    strlen(vlun->svl_active_pclass)+1);
4519 					vlun->svl_active_pclass = tptr;
4520 					mutex_exit(&vlun->svl_mutex);
4521 				} else {
4522 					/*
4523 					 * Non ALUA device case.
4524 					 */
4525 					mdi_pi_set_state(pip,
4526 					    MDI_PATHINFO_STATE_STANDBY);
4527 				}
4528 				vlun->svl_fo_support = opinfo.opinfo_mode;
4529 				mdi_pi_set_preferred(pip,
4530 				    opinfo.opinfo_preferred);
4531 				return (MDI_SUCCESS);
4532 			}
4533 		} else {
4534 			char	*tptr;
4535 
4536 			/*
4537 			 * lets release the mutex before we try to
4538 			 * allocate since the potential to sleep is
4539 			 * possible.
4540 			 */
4541 			mutex_exit(&vlun->svl_mutex);
4542 			tptr = kmem_alloc(strlen(pclass)+1, KM_SLEEP);
4543 			(void) strlcpy(tptr, pclass, (strlen(pclass)+1));
4544 			mutex_enter(&vlun->svl_mutex);
4545 			vlun->svl_active_pclass = tptr;
4546 		}
4547 		mutex_exit(&vlun->svl_mutex);
4548 		mdi_pi_set_state(pip, MDI_PATHINFO_STATE_ONLINE);
4549 		vlun->svl_waiting_for_activepath = 0;
4550 	} else if (opinfo.opinfo_path_state == SCSI_PATH_ACTIVE_NONOPT) {
4551 		mutex_enter(&vlun->svl_mutex);
4552 		if (vlun->svl_active_pclass == NULL) {
4553 			char	*tptr;
4554 
4555 			mutex_exit(&vlun->svl_mutex);
4556 			tptr = kmem_alloc(strlen(pclass)+1, KM_SLEEP);
4557 			(void) strlcpy(tptr, pclass, (strlen(pclass)+1));
4558 			mutex_enter(&vlun->svl_mutex);
4559 			vlun->svl_active_pclass = tptr;
4560 		}
4561 		mutex_exit(&vlun->svl_mutex);
4562 		mdi_pi_set_state(pip, MDI_PATHINFO_STATE_ONLINE);
4563 		vlun->svl_waiting_for_activepath = 0;
4564 	} else if (opinfo.opinfo_path_state == SCSI_PATH_INACTIVE) {
4565 		mutex_enter(&vlun->svl_mutex);
4566 		if (vlun->svl_active_pclass != NULL) {
4567 			if (strcmp(vlun->svl_active_pclass, pclass) == 0) {
4568 				mutex_exit(&vlun->svl_mutex);
4569 				/*
4570 				 * externally initiated failover has happened;
4571 				 * force state to ONLINE (see comment above)
4572 				 */
4573 				mdi_pi_set_state(pip,
4574 				    MDI_PATHINFO_STATE_ONLINE);
4575 				vlun->svl_fo_support = opinfo.opinfo_mode;
4576 				mdi_pi_set_preferred(pip,
4577 				    opinfo.opinfo_preferred);
4578 				return (MDI_SUCCESS);
4579 			}
4580 		}
4581 		mutex_exit(&vlun->svl_mutex);
4582 		mdi_pi_set_state(pip, MDI_PATHINFO_STATE_STANDBY);
4583 
4584 		/*
4585 		 * Initiate auto-failback, if enabled, for path if path-state
4586 		 * is transitioning from OFFLINE->STANDBY and pathclass is the
4587 		 * prefered pathclass for this storage.
4588 		 * NOTE: In case where opinfo_path_state is SCSI_PATH_ACTIVE
4589 		 * (above), where the pi state is set to STANDBY, we don't
4590 		 * initiate auto-failback as the next IO shall take care of.
4591 		 * this. See comment above.
4592 		 */
4593 		(*fo->sfo_pathclass_next)(NULL, &best_pclass,
4594 		    vlun->svl_fops_ctpriv);
4595 		if (((vhci->vhci_conf_flags & VHCI_CONF_FLAGS_AUTO_FAILBACK) ==
4596 		    VHCI_CONF_FLAGS_AUTO_FAILBACK) &&
4597 		    ((strcmp(pclass, best_pclass) == 0) ||
4598 		    mdi_pi_get_preferred(pip) == 1) &&
4599 		    ((MDI_PI_OLD_STATE(pip) == MDI_PATHINFO_STATE_OFFLINE)||
4600 		    (MDI_PI_OLD_STATE(pip) == MDI_PATHINFO_STATE_INIT))) {
4601 			VHCI_DEBUG(1, (CE_NOTE, NULL, "%s pathclass path: %p"
4602 			    " OFFLINE->STANDBY transition for lun %s\n",
4603 			    best_pclass, (void *)pip, vlun->svl_lun_wwn));
4604 			(void) taskq_dispatch(vhci->vhci_taskq,
4605 			    vhci_initiate_auto_failback, (void *) vlun,
4606 			    KM_SLEEP);
4607 		}
4608 	}
4609 	vlun->svl_fo_support = opinfo.opinfo_mode;
4610 	mdi_pi_set_preferred(pip, opinfo.opinfo_preferred);
4611 
4612 	VHCI_DEBUG(8, (CE_NOTE, NULL, "vhci_update_pathinfo: opinfo_rev = %x,"
4613 	    " opinfo_path_state = %x opinfo_preferred = %x, opinfo_mode = %x\n",
4614 	    opinfo.opinfo_rev, opinfo.opinfo_path_state,
4615 	    opinfo.opinfo_preferred, opinfo.opinfo_mode));
4616 
4617 	return (MDI_SUCCESS);
4618 }
4619 
4620 /*
4621  * Form the kstat name and and call mdi_pi_kstat_create()
4622  */
4623 void
4624 vhci_kstat_create_pathinfo(mdi_pathinfo_t *pip)
4625 {
4626 	dev_info_t	*tgt_dip;
4627 	dev_info_t	*pdip;
4628 	char		*guid;
4629 	char		*target_port, *target_port_dup;
4630 	char		ks_name[KSTAT_STRLEN];
4631 	uint_t		pid;
4632 	int		by_id;
4633 	mod_hash_val_t	hv;
4634 
4635 
4636 	/* return if we have already allocated kstats */
4637 	if (mdi_pi_kstat_exists(pip))
4638 		return;
4639 
4640 	/*
4641 	 * We need instance numbers to create a kstat name, return if we don't
4642 	 * have instance numbers assigned yet.
4643 	 */
4644 	tgt_dip = mdi_pi_get_client(pip);
4645 	pdip = mdi_pi_get_phci(pip);
4646 	if ((ddi_get_instance(tgt_dip) == -1) || (ddi_get_instance(pdip) == -1))
4647 		return;
4648 
4649 	/*
4650 	 * A path oriented kstat has a ks_name of the form:
4651 	 *
4652 	 * <client-driver><instance>.t<pid>.<pHCI-driver><instance>
4653 	 *
4654 	 * We maintain a bidirectional 'target-port' to <pid> map,
4655 	 * called targetmap. All pathinfo nodes with the same
4656 	 * 'target-port' map to the same <pid>. The iostat(1M) code,
4657 	 * when parsing a path oriented kstat name, uses the <pid> as
4658 	 * a SCSI_VHCI_GET_TARGET_LONGNAME ioctl argument in order
4659 	 * to get the 'target-port'. For KSTAT_FLAG_PERSISTENT kstats,
4660 	 * this ioctl needs to translate a <pid> to a 'target-port'
4661 	 * even after all pathinfo nodes associated with the
4662 	 * 'target-port' have been destroyed. This is needed to support
4663 	 * consistent first-iteration activity-since-boot iostat(1M)
4664 	 * output. Because of this requirement, the mapping can't be
4665 	 * based on pathinfo information in a devinfo snapshot.
4666 	 */
4667 
4668 	/* determine 'target-port' */
4669 	if (mdi_prop_lookup_string(pip,
4670 	    "target-port", &target_port) == MDI_SUCCESS) {
4671 		target_port_dup = i_ddi_strdup(target_port, KM_SLEEP);
4672 		(void) mdi_prop_free(target_port);
4673 		by_id = 1;
4674 	} else {
4675 		/*
4676 		 * If the pHCI did not set up 'target-port' on this
4677 		 * pathinfo node, assume that our client is the only
4678 		 * one with paths to the device by using the guid
4679 		 * value as the 'target-port'. Since no other client
4680 		 * will have the same guid, no other client will use
4681 		 * the same <pid>.  NOTE: a client with an instance
4682 		 * number always has a guid.
4683 		 */
4684 		(void) ddi_prop_lookup_string(DDI_DEV_T_ANY, tgt_dip,
4685 		    PROPFLAGS, MDI_CLIENT_GUID_PROP, &guid);
4686 		target_port_dup = i_ddi_strdup(guid, KM_SLEEP);
4687 		ddi_prop_free(guid);
4688 
4689 		/*
4690 		 * For this type of mapping we don't want the
4691 		 * <id> -> 'target-port' mapping to be made.  This
4692 		 * will cause the SCSI_VHCI_GET_TARGET_LONGNAME ioctl
4693 		 * to fail, and the iostat(1M) long '-n' output will
4694 		 * still use the <pid>.  We do this because we just
4695 		 * made up the 'target-port' using the guid, and we
4696 		 * don't want to expose that fact in iostat output.
4697 		 */
4698 		by_id = 0;
4699 	}
4700 
4701 	/* find/establish <pid> given 'target-port' */
4702 	mutex_enter(&vhci_targetmap_mutex);
4703 	if (mod_hash_find(vhci_targetmap_byport,
4704 	    (mod_hash_key_t)target_port_dup, &hv) == 0) {
4705 		pid = (int)(intptr_t)hv;	/* mapping exists */
4706 	} else {
4707 		pid = vhci_targetmap_pid++;	/* new mapping */
4708 
4709 		(void) mod_hash_insert(vhci_targetmap_byport,
4710 		    (mod_hash_key_t)target_port_dup,
4711 		    (mod_hash_val_t)(intptr_t)pid);
4712 		if (by_id) {
4713 			(void) mod_hash_insert(vhci_targetmap_bypid,
4714 			    (mod_hash_key_t)(uintptr_t)pid,
4715 			    (mod_hash_val_t)(uintptr_t)target_port_dup);
4716 		}
4717 		target_port_dup = NULL;		/* owned by hash */
4718 	}
4719 	mutex_exit(&vhci_targetmap_mutex);
4720 
4721 	/* form kstat name */
4722 	(void) snprintf(ks_name, KSTAT_STRLEN, "%s%d.t%d.%s%d",
4723 	    ddi_driver_name(tgt_dip), ddi_get_instance(tgt_dip),
4724 	    pid, ddi_driver_name(pdip), ddi_get_instance(pdip));
4725 
4726 	VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_path_online: path:%p "
4727 	    "kstat %s: pid %x <-> port %s\n", (void *)pip,
4728 	    ks_name, pid, target_port_dup));
4729 	if (target_port_dup)
4730 		kmem_free(target_port_dup, strlen(target_port_dup) + 1);
4731 
4732 	/* call mdi to create kstats with the name we built */
4733 	(void) mdi_pi_kstat_create(pip, ks_name);
4734 }
4735 
4736 /* ARGSUSED */
4737 static int
4738 vhci_pathinfo_online(dev_info_t *vdip, mdi_pathinfo_t *pip, int flags)
4739 {
4740 	scsi_hba_tran_t			*hba = NULL;
4741 	struct scsi_device		*psd = NULL;
4742 	scsi_vhci_lun_t			*vlun = NULL;
4743 	dev_info_t			*pdip = NULL;
4744 	dev_info_t			*tgt_dip;
4745 	struct scsi_vhci		*vhci;
4746 	char				*guid;
4747 	struct scsi_failover		*sf;
4748 	struct scsi_failover_ops	*sfo;
4749 	char				*override;
4750 	scsi_vhci_priv_t		*svp = NULL;
4751 	struct buf			*bp;
4752 	struct scsi_address		*ap;
4753 	struct scsi_pkt			*pkt;
4754 	int				rval = MDI_FAILURE;
4755 	uint_t				inq_size = VHCI_STD_INQ_SIZE;
4756 	mpapi_item_list_t		*list_ptr;
4757 	mpapi_lu_data_t			*ld;
4758 
4759 	ASSERT(vdip != NULL);
4760 	ASSERT(pip != NULL);
4761 
4762 	vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip));
4763 	ASSERT(vhci != NULL);
4764 
4765 	pdip = mdi_pi_get_phci(pip);
4766 	hba = ddi_get_driver_private(pdip);
4767 	ASSERT(hba != NULL);
4768 
4769 	svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
4770 	ASSERT(svp != NULL);
4771 
4772 	tgt_dip = mdi_pi_get_client(pip);
4773 	ASSERT(tgt_dip != NULL);
4774 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, tgt_dip, PROPFLAGS,
4775 	    MDI_CLIENT_GUID_PROP, &guid) != DDI_SUCCESS) {
4776 		VHCI_DEBUG(1, (CE_WARN, NULL, "vhci_path_online: lun guid "
4777 		    "property failed"));
4778 		goto failure;
4779 	}
4780 
4781 	vlun = vhci_lun_lookup(tgt_dip);
4782 	ASSERT(vlun != NULL);
4783 
4784 	ddi_prop_free(guid);
4785 
4786 	vlun->svl_dip = mdi_pi_get_client(pip);
4787 	ASSERT(vlun->svl_dip != NULL);
4788 
4789 	psd = svp->svp_psd;
4790 	ASSERT(psd != NULL);
4791 
4792 	/*
4793 	 * For INQUIRY response buffer size, we use VHCI_STD_INQ_SIZE(128bytes)
4794 	 * instead of SUN_INQSIZE(48bytes) which is used in sd layer. This is
4795 	 * because we could get the Vendor specific parameters(present 97th
4796 	 * byte onwards) which are required to process Vendor specific data
4797 	 * based on array type.
4798 	 * This INQUIRY buffer is freed in vhci_pathinfo_offline but NEVER
4799 	 * in a different layer like sd/phci transport. In other words, vhci
4800 	 * maintains its own copy of scsi_device and scsi_inquiry data on a
4801 	 * per-path basis.
4802 	 */
4803 	if (psd->sd_inq == NULL) {
4804 		psd->sd_inq = (struct scsi_inquiry *)
4805 		    kmem_zalloc(inq_size, KM_SLEEP);
4806 	}
4807 
4808 	tgt_dip = psd->sd_dev;
4809 	ASSERT(tgt_dip != NULL);
4810 
4811 	/*
4812 	 * do inquiry to pass into probe routine; this
4813 	 * will avoid each probe routine doing scsi inquiry
4814 	 */
4815 	bp = getrbuf(KM_SLEEP);
4816 	bp->b_un.b_addr = (caddr_t)psd->sd_inq;
4817 	bp->b_flags = B_READ;
4818 	bp->b_bcount = inq_size;
4819 	bp->b_resid = 0;
4820 
4821 	ap = &psd->sd_address;
4822 	pkt = scsi_init_pkt(ap, NULL, bp, CDB_GROUP0,
4823 	    sizeof (struct scsi_arq_status), 0, 0, SLEEP_FUNC, NULL);
4824 	if (pkt == NULL) {
4825 		VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_pathinfo_online: "
4826 		    "Inquiry init_pkt failed :%p\n", (void *)pip));
4827 		rval = MDI_FAILURE;
4828 		goto failure;
4829 	}
4830 	pkt->pkt_cdbp[0] = SCMD_INQUIRY;
4831 	pkt->pkt_cdbp[4] = (uchar_t)inq_size;
4832 	pkt->pkt_time = 60;
4833 
4834 	rval = vhci_do_scsi_cmd(pkt);
4835 	scsi_destroy_pkt(pkt);
4836 	freerbuf(bp);
4837 	if (rval == 0) {
4838 		VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_pathinfo_online: "
4839 		    "Failover Inquiry failed path:%p rval:%x\n",
4840 		    (void *)pip, rval));
4841 		rval = MDI_FAILURE;
4842 		goto failure;
4843 	}
4844 
4845 	/*
4846 	 * Determine if device is supported under scsi_vhci, and select
4847 	 * failover module.
4848 	 *
4849 	 * See if there is a scsi_vhci.conf file override for this devices's
4850 	 * VID/PID. The following values can be returned:
4851 	 *
4852 	 * NULL		If the NULL is returned then there is no scsi_vhci.conf
4853 	 *		override.  For NULL, we determine the failover_ops for
4854 	 *		this device by checking the sfo_device_probe entry
4855 	 *		point for each 'fops' module, in order.
4856 	 *
4857 	 *		NOTE: Correct operation may depend on module ordering
4858 	 *		of 'specific' (failover modules that are completely
4859 	 *		VID/PID table based) to 'generic' (failover modules
4860 	 *		that based on T10 standards like TPGS).  Currently,
4861 	 *		the value of 'ddi-forceload' in scsi_vhci.conf is used
4862 	 *		to establish the module list and probe order.
4863 	 *
4864 	 * "NONE"	If value "NONE" is returned then there is a
4865 	 *		scsi_vhci.conf VID/PID override to indicate the device
4866 	 *		should not be supported under scsi_vhci (even if there
4867 	 *		is an 'fops' module supporting the device).
4868 	 *
4869 	 * "<other>"	If another value is returned then that value is the
4870 	 *		name of the 'fops' module that should be used.
4871 	 */
4872 	sfo = NULL;	/* "NONE" */
4873 	override = scsi_get_device_type_string(
4874 	    "scsi-vhci-failover-override", vdip, psd);
4875 
4876 	if (override == NULL) {
4877 		/* NULL: default: select based on sfo_device_probe results */
4878 		for (sf = scsi_failover_table; sf->sf_mod; sf++) {
4879 			if ((sf->sf_sfo == NULL) ||
4880 			    ((*sf->sf_sfo->sfo_device_probe) (psd,
4881 			    psd->sd_inq, &vlun->svl_fops_ctpriv) ==
4882 			    SFO_DEVICE_PROBE_PHCI))
4883 				continue;
4884 
4885 			/* found failover module, supported under scsi_vhci */
4886 			sfo = sf->sf_sfo;
4887 			vlun->svl_fops_name =
4888 			    i_ddi_strdup(sfo->sfo_name, KM_SLEEP);
4889 			break;
4890 		}
4891 	} else if (strcmp(override, "NONE") && strcmp(override, "none")) {
4892 		/* !"NONE": select based on driver.conf specified name */
4893 		for (sf = scsi_failover_table, sfo = NULL; sf->sf_mod; sf++) {
4894 			if ((sf->sf_sfo == NULL) ||
4895 			    (sf->sf_sfo->sfo_name == NULL) ||
4896 			    strcmp(override, sf->sf_sfo->sfo_name))
4897 				continue;
4898 
4899 			/* found failover module, supported under scsi_vhci */
4900 			sfo = sf->sf_sfo;
4901 			vlun->svl_fops_name = kmem_alloc(strlen("conf ") +
4902 			    strlen(sfo->sfo_name) + 1, KM_SLEEP);
4903 			(void) sprintf(vlun->svl_fops_name, "conf %s",
4904 			    sfo->sfo_name);
4905 			break;
4906 		}
4907 	}
4908 	if (override)
4909 		kmem_free(override, strlen(override) + 1);
4910 
4911 	if (sfo == NULL) {
4912 		/* no failover module - device not supported */
4913 		VHCI_DEBUG(1, (CE_NOTE, vhci->vhci_dip,
4914 		    "!vhci_pathinfo_online: dev (path 0x%p) not "
4915 		    "supported\n", (void *)pip));
4916 		vlun->svl_not_supported = 1;
4917 		rval = MDI_NOT_SUPPORTED;
4918 		goto done;
4919 	}
4920 
4921 	/* failover supported for device - save failover_ops in vlun */
4922 	vlun->svl_fops = sfo;
4923 
4924 	/*
4925 	 * Obtain the device-type based mpxio options as specified in
4926 	 * scsi_vhci.conf file.
4927 	 *
4928 	 * NOTE: currently, the end result is a call to
4929 	 * mdi_set_lb_region_size().
4930 	 */
4931 	vhci_get_device_type_mpxio_options(vdip, tgt_dip, psd);
4932 
4933 	/*
4934 	 * if PGR is active, revalidate key and register on this path also,
4935 	 * if key is still valid
4936 	 */
4937 	sema_p(&vlun->svl_pgr_sema);
4938 	if (vlun->svl_pgr_active) {
4939 		rval = vhci_pgr_validate_and_register(svp);
4940 		if (rval != 1) {
4941 			rval = MDI_FAILURE;
4942 			sema_v(&vlun->svl_pgr_sema);
4943 			goto failure;
4944 		}
4945 	}
4946 	sema_v(&vlun->svl_pgr_sema);
4947 
4948 	if (svp->svp_new_path) {
4949 		/*
4950 		 * Last chance to perform any cleanup operations on this
4951 		 * new path before making this path completely online.
4952 		 */
4953 		svp->svp_new_path = 0;
4954 
4955 		/*
4956 		 * If scsi_vhci knows the lun is alread RESERVE'd,
4957 		 * then skip the issue of RELEASE on new path.
4958 		 */
4959 		if ((vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) == 0) {
4960 			/*
4961 			 * Issue SCSI-2 RELEASE only for the first time on
4962 			 * a new path just in case the host rebooted and
4963 			 * a reservation is still pending on this path.
4964 			 * IBM Shark storage does not clear RESERVE upon
4965 			 * host reboot.
4966 			 */
4967 			ap = &psd->sd_address;
4968 			pkt = scsi_init_pkt(ap, NULL, NULL, CDB_GROUP0,
4969 			    sizeof (struct scsi_arq_status), 0, 0,
4970 			    SLEEP_FUNC, NULL);
4971 			if (pkt == NULL) {
4972 				VHCI_DEBUG(1, (CE_NOTE, NULL,
4973 				    "!vhci_pathinfo_online: "
4974 				    "Release init_pkt failed :%p\n",
4975 				    (void *)pip));
4976 				rval = MDI_FAILURE;
4977 				goto failure;
4978 			}
4979 			pkt->pkt_cdbp[0] = SCMD_RELEASE;
4980 			pkt->pkt_time = 60;
4981 
4982 			VHCI_DEBUG(1, (CE_NOTE, NULL,
4983 			    "!vhci_path_online: path:%p "
4984 			    "Issued SCSI-2 RELEASE\n", (void *)pip));
4985 
4986 			/* Ignore the return value */
4987 			(void) vhci_do_scsi_cmd(pkt);
4988 			scsi_destroy_pkt(pkt);
4989 		}
4990 	}
4991 
4992 	rval = vhci_update_pathinfo(psd, pip, sfo, vlun, vhci);
4993 	if (rval == MDI_FAILURE) {
4994 		goto failure;
4995 	}
4996 
4997 	/* Initialize MP-API data */
4998 	vhci_update_mpapi_data(vhci, vlun, pip);
4999 
5000 	/*
5001 	 * MP-API also needs the Inquiry data to be maintained in the
5002 	 * mp_vendor_prop_t structure, so find the lun and update its
5003 	 * structure with this data.
5004 	 */
5005 	list_ptr = (mpapi_item_list_t *)vhci_get_mpapi_item(vhci, NULL,
5006 	    MP_OBJECT_TYPE_MULTIPATH_LU, (void *)vlun);
5007 	ld = (mpapi_lu_data_t *)list_ptr->item->idata;
5008 	if (ld != NULL) {
5009 		bcopy(psd->sd_inq->inq_vid, ld->prop.prodInfo.vendor, 8);
5010 		bcopy(psd->sd_inq->inq_pid, ld->prop.prodInfo.product, 16);
5011 		bcopy(psd->sd_inq->inq_revision, ld->prop.prodInfo.revision, 4);
5012 	} else {
5013 		VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_pathinfo_online: "
5014 		    "mpapi_lu_data_t is NULL"));
5015 	}
5016 
5017 	/* create kstats for path */
5018 	vhci_kstat_create_pathinfo(pip);
5019 
5020 done:
5021 	mutex_enter(&vhci_global_mutex);
5022 	cv_broadcast(&vhci_cv);
5023 	mutex_exit(&vhci_global_mutex);
5024 
5025 	if (vlun->svl_setcap_done) {
5026 		(void) vhci_pHCI_cap(ap, "sector-size",
5027 		    vlun->svl_sector_size, 1, pip);
5028 	}
5029 
5030 	VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_path_online: path:%p\n",
5031 	    (void *)pip));
5032 
5033 failure:
5034 	if ((rval != MDI_SUCCESS) && psd->sd_inq) {
5035 		kmem_free((caddr_t)psd->sd_inq, inq_size);
5036 		psd->sd_inq = (struct scsi_inquiry *)NULL;
5037 	}
5038 	return (rval);
5039 }
5040 
5041 /*
5042  * path offline handler.  Release all bindings that will not be
5043  * released by the normal packet transport/completion code path.
5044  * Since we don't (presently) keep any bindings alive outside of
5045  * the in-transport packets (which will be released on completion)
5046  * there is not much to do here.
5047  */
5048 /* ARGSUSED */
5049 static int
5050 vhci_pathinfo_offline(dev_info_t *vdip, mdi_pathinfo_t *pip, int flags)
5051 {
5052 	scsi_hba_tran_t		*hba = NULL;
5053 	struct scsi_device	*psd = NULL;
5054 	dev_info_t		*pdip = NULL;
5055 	dev_info_t		*cdip = NULL;
5056 	scsi_vhci_priv_t	*svp = NULL;
5057 	uint_t			inq_size = VHCI_STD_INQ_SIZE;
5058 
5059 	ASSERT(vdip != NULL);
5060 	ASSERT(pip != NULL);
5061 
5062 	pdip = mdi_pi_get_phci(pip);
5063 	ASSERT(pdip != NULL);
5064 	if (pdip == NULL) {
5065 		VHCI_DEBUG(1, (CE_WARN, vdip, "Invalid path 0x%p: NULL "
5066 		    "phci dip", (void *)pip));
5067 		return (MDI_FAILURE);
5068 	}
5069 
5070 	cdip = mdi_pi_get_client(pip);
5071 	ASSERT(cdip != NULL);
5072 	if (cdip == NULL) {
5073 		VHCI_DEBUG(1, (CE_WARN, vdip, "Invalid path 0x%p: NULL "
5074 		    "client dip", (void *)pip));
5075 		return (MDI_FAILURE);
5076 	}
5077 
5078 	hba = ddi_get_driver_private(pdip);
5079 	ASSERT(hba != NULL);
5080 
5081 	svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
5082 	if (svp == NULL) {
5083 		/*
5084 		 * mdi_pathinfo node in INIT state can have vHCI private
5085 		 * information set to null
5086 		 */
5087 		VHCI_DEBUG(1, (CE_NOTE, vdip, "!vhci_pathinfo_offline: "
5088 		    "svp is NULL for pip 0x%p\n", (void *)pip));
5089 		return (MDI_SUCCESS);
5090 	}
5091 
5092 	psd = svp->svp_psd;
5093 	ASSERT(psd != NULL);
5094 
5095 	mutex_enter(&svp->svp_mutex);
5096 
5097 	VHCI_DEBUG(1, (CE_NOTE, vdip, "!vhci_pathinfo_offline: "
5098 	    "%d cmds pending on path: 0x%p\n", svp->svp_cmds, (void *)pip));
5099 	while (svp->svp_cmds != 0) {
5100 		if (cv_timedwait(&svp->svp_cv, &svp->svp_mutex,
5101 		    ddi_get_lbolt() +
5102 		    drv_usectohz(vhci_path_quiesce_timeout * 1000000)) == -1) {
5103 			/*
5104 			 * The timeout time reached without the condition
5105 			 * being signaled.
5106 			 */
5107 			VHCI_DEBUG(1, (CE_NOTE, vdip, "!vhci_pathinfo_offline: "
5108 			    "Timeout reached on path 0x%p without the cond\n",
5109 			    (void *)pip));
5110 			VHCI_DEBUG(1, (CE_NOTE, vdip, "!vhci_pathinfo_offline: "
5111 			    "%d cmds still pending on path: 0x%p\n",
5112 			    svp->svp_cmds, (void *)pip));
5113 			break;
5114 		}
5115 	}
5116 	mutex_exit(&svp->svp_mutex);
5117 
5118 	/*
5119 	 * Check to see if this vlun has an active SCSI-II RESERVE. And this
5120 	 * is the pip for the path that has been reserved.
5121 	 * If so clear the reservation by sending a reset, so the host will not
5122 	 * get a reservation conflict.  Reset the flag VLUN_RESERVE_ACTIVE_FLG
5123 	 * for this lun.  Also a reset notify is sent to the target driver
5124 	 * just in case the POR check condition is cleared by some other layer
5125 	 * in the stack.
5126 	 */
5127 	if (svp->svp_svl->svl_flags & VLUN_RESERVE_ACTIVE_FLG) {
5128 		if (pip == svp->svp_svl->svl_resrv_pip) {
5129 			if (vhci_recovery_reset(svp->svp_svl,
5130 			    &svp->svp_psd->sd_address, TRUE,
5131 			    VHCI_DEPTH_TARGET) == 0) {
5132 				VHCI_DEBUG(1, (CE_NOTE, NULL,
5133 				    "!vhci_pathinfo_offline (pip:%p):"
5134 				    "reset failed, retrying\n", (void *)pip));
5135 				delay(1*drv_usectohz(1000000));
5136 				if (vhci_recovery_reset(svp->svp_svl,
5137 				    &svp->svp_psd->sd_address, TRUE,
5138 				    VHCI_DEPTH_TARGET) == 0) {
5139 					VHCI_DEBUG(1, (CE_NOTE, NULL,
5140 					    "!vhci_pathinfo_offline "
5141 					    "(pip:%p): reset failed, "
5142 					    "giving up!\n", (void *)pip));
5143 				}
5144 			}
5145 			svp->svp_svl->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG;
5146 		}
5147 	}
5148 
5149 	mdi_pi_set_state(pip, MDI_PATHINFO_STATE_OFFLINE);
5150 	if (psd->sd_inq) {
5151 		kmem_free((caddr_t)psd->sd_inq, inq_size);
5152 		psd->sd_inq = (struct scsi_inquiry *)NULL;
5153 	}
5154 	vhci_mpapi_set_path_state(vdip, pip, MP_DRVR_PATH_STATE_REMOVED);
5155 
5156 	VHCI_DEBUG(1, (CE_NOTE, NULL,
5157 	    "!vhci_pathinfo_offline: offlined path 0x%p\n", (void *)pip));
5158 	return (MDI_SUCCESS);
5159 }
5160 
5161 
5162 /*
5163  * routine for SCSI VHCI IOCTL implementation.
5164  */
5165 /* ARGSUSED */
5166 static int
5167 vhci_ctl(dev_t dev, int cmd, intptr_t data, int mode, cred_t *credp, int *rval)
5168 {
5169 	struct scsi_vhci		*vhci;
5170 	dev_info_t			*vdip;
5171 	mdi_pathinfo_t			*pip;
5172 	int				instance, held;
5173 	int				retval = 0;
5174 	caddr_t				phci_path = NULL, client_path = NULL;
5175 	caddr_t				paddr = NULL;
5176 	sv_iocdata_t			ioc;
5177 	sv_iocdata_t			*pioc = &ioc;
5178 	sv_switch_to_cntlr_iocdata_t	iocsc;
5179 	sv_switch_to_cntlr_iocdata_t	*piocsc = &iocsc;
5180 	caddr_t				s;
5181 	scsi_vhci_lun_t			*vlun;
5182 	struct scsi_failover_ops	*fo;
5183 	char				*pclass;
5184 
5185 	/* Check for validity of vhci structure */
5186 	vhci = ddi_get_soft_state(vhci_softstate, MINOR2INST(getminor(dev)));
5187 	if (vhci == NULL) {
5188 		return (ENXIO);
5189 	}
5190 
5191 	mutex_enter(&vhci->vhci_mutex);
5192 	if ((vhci->vhci_state & VHCI_STATE_OPEN) == 0) {
5193 		mutex_exit(&vhci->vhci_mutex);
5194 		return (ENXIO);
5195 	}
5196 	mutex_exit(&vhci->vhci_mutex);
5197 
5198 	/* Get the vhci dip */
5199 	vdip = vhci->vhci_dip;
5200 	ASSERT(vdip != NULL);
5201 	instance = ddi_get_instance(vdip);
5202 
5203 	/* Allocate memory for getting parameters from userland */
5204 	phci_path	= kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5205 	client_path	= kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5206 	paddr		= kmem_zalloc(MAXNAMELEN, KM_SLEEP);
5207 
5208 	/*
5209 	 * Set a local variable indicating the ioctl name. Used for
5210 	 * printing debug strings.
5211 	 */
5212 	switch (cmd) {
5213 	case SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO:
5214 		s = "GET_CLIENT_MULTIPATH_INFO";
5215 		break;
5216 
5217 	case SCSI_VHCI_GET_PHCI_MULTIPATH_INFO:
5218 		s = "GET_PHCI_MULTIPATH_INFO";
5219 		break;
5220 
5221 	case SCSI_VHCI_GET_CLIENT_NAME:
5222 		s = "GET_CLIENT_NAME";
5223 		break;
5224 
5225 	case SCSI_VHCI_PATH_ONLINE:
5226 		s = "PATH_ONLINE";
5227 		break;
5228 
5229 	case SCSI_VHCI_PATH_OFFLINE:
5230 		s = "PATH_OFFLINE";
5231 		break;
5232 
5233 	case SCSI_VHCI_PATH_STANDBY:
5234 		s = "PATH_STANDBY";
5235 		break;
5236 
5237 	case SCSI_VHCI_PATH_TEST:
5238 		s = "PATH_TEST";
5239 		break;
5240 
5241 	case SCSI_VHCI_SWITCH_TO_CNTLR:
5242 		s = "SWITCH_TO_CNTLR";
5243 		break;
5244 	case SCSI_VHCI_PATH_DISABLE:
5245 		s = "PATH_DISABLE";
5246 		break;
5247 	case SCSI_VHCI_PATH_ENABLE:
5248 		s = "PATH_ENABLE";
5249 		break;
5250 
5251 	case SCSI_VHCI_GET_TARGET_LONGNAME:
5252 		s = "GET_TARGET_LONGNAME";
5253 		break;
5254 
5255 #ifdef	DEBUG
5256 	case SCSI_VHCI_CONFIGURE_PHCI:
5257 		s = "CONFIGURE_PHCI";
5258 		break;
5259 
5260 	case SCSI_VHCI_UNCONFIGURE_PHCI:
5261 		s = "UNCONFIGURE_PHCI";
5262 		break;
5263 #endif
5264 
5265 	default:
5266 		s = "Unknown";
5267 		vhci_log(CE_NOTE, vdip,
5268 		    "!vhci%d: ioctl %x (unsupported ioctl)", instance, cmd);
5269 		retval = ENOTSUP;
5270 		break;
5271 	}
5272 	if (retval != 0) {
5273 		goto end;
5274 	}
5275 
5276 	VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci%d: ioctl <%s>", instance, s));
5277 
5278 	/*
5279 	 * Get IOCTL parameters from userland
5280 	 */
5281 	switch (cmd) {
5282 	case SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO:
5283 	case SCSI_VHCI_GET_PHCI_MULTIPATH_INFO:
5284 	case SCSI_VHCI_GET_CLIENT_NAME:
5285 	case SCSI_VHCI_PATH_ONLINE:
5286 	case SCSI_VHCI_PATH_OFFLINE:
5287 	case SCSI_VHCI_PATH_STANDBY:
5288 	case SCSI_VHCI_PATH_TEST:
5289 	case SCSI_VHCI_PATH_DISABLE:
5290 	case SCSI_VHCI_PATH_ENABLE:
5291 	case SCSI_VHCI_GET_TARGET_LONGNAME:
5292 #ifdef	DEBUG
5293 	case SCSI_VHCI_CONFIGURE_PHCI:
5294 	case SCSI_VHCI_UNCONFIGURE_PHCI:
5295 #endif
5296 		retval = vhci_get_iocdata((const void *)data, pioc, mode, s);
5297 		break;
5298 
5299 	case SCSI_VHCI_SWITCH_TO_CNTLR:
5300 		retval = vhci_get_iocswitchdata((const void *)data, piocsc,
5301 		    mode, s);
5302 		break;
5303 	}
5304 	if (retval != 0) {
5305 		goto end;
5306 	}
5307 
5308 
5309 	/*
5310 	 * Process the IOCTL
5311 	 */
5312 	switch (cmd) {
5313 	case SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO:
5314 	{
5315 		uint_t		num_paths;	/* Num paths to client dev */
5316 		sv_path_info_t	*upibuf = NULL;	/* To keep userland values */
5317 		sv_path_info_t	*kpibuf = NULL; /* Kernel data for ioctls */
5318 		dev_info_t	*cdip;		/* Client device dip */
5319 
5320 		if (pioc->ret_elem == NULL) {
5321 			retval = EINVAL;
5322 			break;
5323 		}
5324 
5325 		/* Get client device path from user land */
5326 		if (vhci_ioc_get_client_path(pioc, client_path, mode, s)) {
5327 			retval = EFAULT;
5328 			break;
5329 		}
5330 
5331 		VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5332 		    "client <%s>", s, client_path));
5333 
5334 		/* Get number of paths to this client device */
5335 		if ((cdip = mdi_client_path2devinfo(vdip, client_path))
5336 		    == NULL) {
5337 			retval = ENXIO;
5338 			VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5339 			    "client dip doesn't exist. invalid path <%s>",
5340 			    s, client_path));
5341 			break;
5342 		}
5343 		num_paths = mdi_client_get_path_count(cdip);
5344 
5345 		if (ddi_copyout(&num_paths, pioc->ret_elem,
5346 		    sizeof (num_paths), mode)) {
5347 			VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5348 			    "num_paths copyout failed", s));
5349 			retval = EFAULT;
5350 			break;
5351 		}
5352 
5353 		/* If  user just wanted num_paths, then return */
5354 		if (pioc->buf_elem == 0 || pioc->ret_buf == NULL ||
5355 		    num_paths == 0) {
5356 			break;
5357 		}
5358 
5359 		/* Set num_paths to value as much as can be sent to userland */
5360 		if (num_paths > pioc->buf_elem) {
5361 			num_paths = pioc->buf_elem;
5362 		}
5363 
5364 		/* Allocate memory and get userland pointers */
5365 		if (vhci_ioc_alloc_pathinfo(&upibuf, &kpibuf, num_paths,
5366 		    pioc, mode, s) != 0) {
5367 			retval = EFAULT;
5368 			break;
5369 		}
5370 		ASSERT(upibuf != NULL);
5371 		ASSERT(kpibuf != NULL);
5372 
5373 		/*
5374 		 * Get the path information and send it to userland.
5375 		 */
5376 		if (vhci_get_client_path_list(cdip, kpibuf, num_paths)
5377 		    != MDI_SUCCESS) {
5378 			retval = ENXIO;
5379 			vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths);
5380 			break;
5381 		}
5382 
5383 		if (vhci_ioc_send_pathinfo(upibuf, kpibuf, num_paths,
5384 		    pioc, mode, s)) {
5385 			retval = EFAULT;
5386 			vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths);
5387 			break;
5388 		}
5389 
5390 		/* Free the memory allocated for path information */
5391 		vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths);
5392 		break;
5393 	}
5394 
5395 	case SCSI_VHCI_GET_PHCI_MULTIPATH_INFO:
5396 	{
5397 		uint_t		num_paths;	/* Num paths to client dev */
5398 		sv_path_info_t	*upibuf = NULL;	/* To keep userland values */
5399 		sv_path_info_t	*kpibuf = NULL; /* Kernel data for ioctls */
5400 		dev_info_t	*pdip;		/* PHCI device dip */
5401 
5402 		if (pioc->ret_elem == NULL) {
5403 			retval = EINVAL;
5404 			break;
5405 		}
5406 
5407 		/* Get PHCI device path from user land */
5408 		if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s)) {
5409 			retval = EFAULT;
5410 			break;
5411 		}
5412 
5413 		VHCI_DEBUG(6, (CE_WARN, vdip,
5414 		    "!vhci_ioctl: ioctl <%s> phci <%s>", s, phci_path));
5415 
5416 		/* Get number of devices associated with this PHCI device */
5417 		if ((pdip = mdi_phci_path2devinfo(vdip, phci_path)) == NULL) {
5418 			VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5419 			    "phci dip doesn't exist. invalid path <%s>",
5420 			    s, phci_path));
5421 			retval = ENXIO;
5422 			break;
5423 		}
5424 
5425 		num_paths = mdi_phci_get_path_count(pdip);
5426 
5427 		if (ddi_copyout(&num_paths, pioc->ret_elem,
5428 		    sizeof (num_paths), mode)) {
5429 			VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5430 			    "num_paths copyout failed", s));
5431 			retval = EFAULT;
5432 			break;
5433 		}
5434 
5435 		/* If  user just wanted num_paths, then return */
5436 		if (pioc->buf_elem == 0 || pioc->ret_buf == NULL ||
5437 		    num_paths == 0) {
5438 			break;
5439 		}
5440 
5441 		/* Set num_paths to value as much as can be sent to userland */
5442 		if (num_paths > pioc->buf_elem) {
5443 			num_paths = pioc->buf_elem;
5444 		}
5445 
5446 		/* Allocate memory and get userland pointers */
5447 		if (vhci_ioc_alloc_pathinfo(&upibuf, &kpibuf, num_paths,
5448 		    pioc, mode, s) != 0) {
5449 			retval = EFAULT;
5450 			break;
5451 		}
5452 		ASSERT(upibuf != NULL);
5453 		ASSERT(kpibuf != NULL);
5454 
5455 		/*
5456 		 * Get the path information and send it to userland.
5457 		 */
5458 		if (vhci_get_phci_path_list(pdip, kpibuf, num_paths)
5459 		    != MDI_SUCCESS) {
5460 			retval = ENXIO;
5461 			vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths);
5462 			break;
5463 		}
5464 
5465 		if (vhci_ioc_send_pathinfo(upibuf, kpibuf, num_paths,
5466 		    pioc, mode, s)) {
5467 			retval = EFAULT;
5468 			vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths);
5469 			break;
5470 		}
5471 
5472 		/* Free the memory allocated for path information */
5473 		vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths);
5474 		break;
5475 	}
5476 
5477 	case SCSI_VHCI_GET_CLIENT_NAME:
5478 	{
5479 		dev_info_t		*cdip, *pdip;
5480 
5481 		/* Get PHCI path and device address from user land */
5482 		if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s) ||
5483 		    vhci_ioc_get_paddr(pioc, paddr, mode, s)) {
5484 			retval = EFAULT;
5485 			break;
5486 		}
5487 
5488 		VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5489 		    "phci <%s>, paddr <%s>", s, phci_path, paddr));
5490 
5491 		/* Get the PHCI dip */
5492 		if ((pdip = mdi_phci_path2devinfo(vdip, phci_path)) == NULL) {
5493 			VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5494 			    "phci dip doesn't exist. invalid path <%s>",
5495 			    s, phci_path));
5496 			retval = ENXIO;
5497 			break;
5498 		}
5499 
5500 		if ((pip = mdi_pi_find(pdip, NULL, paddr)) == NULL) {
5501 			VHCI_DEBUG(1, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5502 			    "pathinfo doesn't exist. invalid device addr", s));
5503 			retval = ENXIO;
5504 			break;
5505 		}
5506 
5507 		/* Get the client device pathname and send to userland */
5508 		cdip = mdi_pi_get_client(pip);
5509 		vhci_ioc_devi_to_path(cdip, client_path);
5510 
5511 		VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5512 		    "client <%s>", s, client_path));
5513 
5514 		if (vhci_ioc_send_client_path(client_path, pioc, mode, s)) {
5515 			retval = EFAULT;
5516 			break;
5517 		}
5518 		break;
5519 	}
5520 
5521 	case SCSI_VHCI_PATH_ONLINE:
5522 	case SCSI_VHCI_PATH_OFFLINE:
5523 	case SCSI_VHCI_PATH_STANDBY:
5524 	case SCSI_VHCI_PATH_TEST:
5525 	{
5526 		dev_info_t		*pdip;	/* PHCI dip */
5527 
5528 		/* Get PHCI path and device address from user land */
5529 		if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s) ||
5530 		    vhci_ioc_get_paddr(pioc, paddr, mode, s)) {
5531 			retval = EFAULT;
5532 			break;
5533 		}
5534 
5535 		VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5536 		    "phci <%s>, paddr <%s>", s, phci_path, paddr));
5537 
5538 		/* Get the PHCI dip */
5539 		if ((pdip = mdi_phci_path2devinfo(vdip, phci_path)) == NULL) {
5540 			VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5541 			    "phci dip doesn't exist. invalid path <%s>",
5542 			    s, phci_path));
5543 			retval = ENXIO;
5544 			break;
5545 		}
5546 
5547 		if ((pip = mdi_pi_find(pdip, NULL, paddr)) == NULL) {
5548 			VHCI_DEBUG(1, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5549 			    "pathinfo doesn't exist. invalid device addr", s));
5550 			retval = ENXIO;
5551 			break;
5552 		}
5553 
5554 		VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5555 		    "Calling MDI function to change device state", s));
5556 
5557 		switch (cmd) {
5558 		case SCSI_VHCI_PATH_ONLINE:
5559 			retval = mdi_pi_online(pip, 0);
5560 			break;
5561 
5562 		case SCSI_VHCI_PATH_OFFLINE:
5563 			retval = mdi_pi_offline(pip, 0);
5564 			break;
5565 
5566 		case SCSI_VHCI_PATH_STANDBY:
5567 			retval = mdi_pi_standby(pip, 0);
5568 			break;
5569 
5570 		case SCSI_VHCI_PATH_TEST:
5571 			break;
5572 		}
5573 		break;
5574 	}
5575 
5576 	case SCSI_VHCI_SWITCH_TO_CNTLR:
5577 	{
5578 		dev_info_t *cdip;
5579 		struct scsi_device *devp;
5580 
5581 		/* Get the client device pathname */
5582 		if (ddi_copyin(piocsc->client, client_path,
5583 		    MAXPATHLEN, mode)) {
5584 			VHCI_DEBUG(2, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5585 			    "client_path copyin failed", s));
5586 			retval = EFAULT;
5587 			break;
5588 		}
5589 
5590 		/* Get the path class to which user wants to switch */
5591 		if (ddi_copyin(piocsc->class, paddr, MAXNAMELEN, mode)) {
5592 			VHCI_DEBUG(2, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5593 			    "controller_class copyin failed", s));
5594 			retval = EFAULT;
5595 			break;
5596 		}
5597 
5598 		/* Perform validity checks */
5599 		if ((cdip = mdi_client_path2devinfo(vdip,
5600 		    client_path)) == NULL) {
5601 			VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5602 			    "client dip doesn't exist. invalid path <%s>",
5603 			    s, client_path));
5604 			retval = ENXIO;
5605 			break;
5606 		}
5607 
5608 		VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: Calling MDI func "
5609 		    "to switch controller"));
5610 		VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: client <%s> "
5611 		    "class <%s>", client_path, paddr));
5612 
5613 		if (strcmp(paddr, PCLASS_PRIMARY) &&
5614 		    strcmp(paddr, PCLASS_SECONDARY)) {
5615 			VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5616 			    "invalid path class <%s>", s, paddr));
5617 			retval = ENXIO;
5618 			break;
5619 		}
5620 
5621 		devp = ddi_get_driver_private(cdip);
5622 		if (devp == NULL) {
5623 			VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5624 			    "invalid scsi device <%s>", s, client_path));
5625 			retval = ENXIO;
5626 			break;
5627 		}
5628 		vlun = ADDR2VLUN(&devp->sd_address);
5629 		ASSERT(vlun);
5630 
5631 		/*
5632 		 * Checking to see if device has only one pclass, PRIMARY.
5633 		 * If so this device doesn't support failovers.  Assumed
5634 		 * that the devices with one pclass is PRIMARY, as thats the
5635 		 * case today.  If this is not true and in future other
5636 		 * symmetric devices are supported with other pclass, this
5637 		 * IOCTL shall have to be overhauled anyways as now the only
5638 		 * arguments it accepts are PRIMARY and SECONDARY.
5639 		 */
5640 		fo = vlun->svl_fops;
5641 		if ((*fo->sfo_pathclass_next)(PCLASS_PRIMARY, &pclass,
5642 		    vlun->svl_fops_ctpriv)) {
5643 			retval = ENOTSUP;
5644 			break;
5645 		}
5646 
5647 		VHCI_HOLD_LUN(vlun, VH_SLEEP, held);
5648 		mutex_enter(&vlun->svl_mutex);
5649 		if (vlun->svl_active_pclass != NULL) {
5650 			if (strcmp(vlun->svl_active_pclass, paddr) == 0) {
5651 				mutex_exit(&vlun->svl_mutex);
5652 				retval = EALREADY;
5653 				VHCI_RELEASE_LUN(vlun);
5654 				break;
5655 			}
5656 		}
5657 		mutex_exit(&vlun->svl_mutex);
5658 		/* Call mdi function to cause  a switch over */
5659 		retval = mdi_failover(vdip, cdip, MDI_FAILOVER_SYNC);
5660 		if (retval == MDI_SUCCESS) {
5661 			retval = 0;
5662 		} else if (retval == MDI_BUSY) {
5663 			retval = EBUSY;
5664 		} else {
5665 			retval = EIO;
5666 		}
5667 		VHCI_RELEASE_LUN(vlun);
5668 		break;
5669 	}
5670 
5671 	case SCSI_VHCI_PATH_ENABLE:
5672 	case SCSI_VHCI_PATH_DISABLE:
5673 	{
5674 		dev_info_t	*cdip, *pdip;
5675 
5676 		/*
5677 		 * Get client device path from user land
5678 		 */
5679 		if (vhci_ioc_get_client_path(pioc, client_path, mode, s)) {
5680 			retval = EFAULT;
5681 			break;
5682 		}
5683 
5684 		/*
5685 		 * Get Phci device path from user land
5686 		 */
5687 		if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s)) {
5688 			retval = EFAULT;
5689 			break;
5690 		}
5691 
5692 		/*
5693 		 * Get the devinfo for the Phci.
5694 		 */
5695 		if ((pdip = mdi_phci_path2devinfo(vdip, phci_path)) == NULL) {
5696 			VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5697 			    "phci dip doesn't exist. invalid path <%s>",
5698 			    s, phci_path));
5699 			retval = ENXIO;
5700 			break;
5701 		}
5702 
5703 		/*
5704 		 * If the client path is set to /scsi_vhci then we need
5705 		 * to do the operation on all the clients so set cdip to NULL.
5706 		 * Else, try to get the client dip.
5707 		 */
5708 		if (strcmp(client_path, "/scsi_vhci") == 0) {
5709 			cdip = NULL;
5710 		} else {
5711 			if ((cdip = mdi_client_path2devinfo(vdip,
5712 			    client_path)) == NULL) {
5713 				retval = ENXIO;
5714 				VHCI_DEBUG(1, (CE_WARN, NULL,
5715 				    "!vhci_ioctl: ioctl <%s> client dip "
5716 				    "doesn't exist. invalid path <%s>",
5717 				    s, client_path));
5718 				break;
5719 			}
5720 		}
5721 
5722 		if (cmd == SCSI_VHCI_PATH_ENABLE)
5723 			retval = mdi_pi_enable(cdip, pdip, USER_DISABLE);
5724 		else
5725 			retval = mdi_pi_disable(cdip, pdip, USER_DISABLE);
5726 
5727 		break;
5728 	}
5729 
5730 	case SCSI_VHCI_GET_TARGET_LONGNAME:
5731 	{
5732 		uint_t		pid = pioc->buf_elem;
5733 		char		*target_port;
5734 		mod_hash_val_t	hv;
5735 
5736 		/* targetmap lookup of 'target-port' by <pid> */
5737 		if (mod_hash_find(vhci_targetmap_bypid,
5738 		    (mod_hash_key_t)(uintptr_t)pid, &hv) != 0) {
5739 			/*
5740 			 * NOTE: failure to find the mapping is OK for guid
5741 			 * based 'target-port' values.
5742 			 */
5743 			VHCI_DEBUG(3, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5744 			    "targetport mapping doesn't exist: pid %d",
5745 			    s, pid));
5746 			retval = ENXIO;
5747 			break;
5748 		}
5749 
5750 		/* copyout 'target-port' result */
5751 		target_port = (char *)hv;
5752 		if (copyoutstr(target_port, pioc->addr, MAXNAMELEN, NULL)) {
5753 			VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5754 			    "targetport copyout failed: len: %d",
5755 			    s, (int)strlen(target_port)));
5756 			retval = EFAULT;
5757 		}
5758 		break;
5759 	}
5760 
5761 #ifdef	DEBUG
5762 	case SCSI_VHCI_CONFIGURE_PHCI:
5763 	{
5764 		dev_info_t		*pdip;
5765 
5766 		/* Get PHCI path and device address from user land */
5767 		if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s)) {
5768 			retval = EFAULT;
5769 			break;
5770 		}
5771 
5772 		VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5773 		    "phci <%s>", s, phci_path));
5774 
5775 		/* Get the PHCI dip */
5776 		if ((pdip = e_ddi_hold_devi_by_path(phci_path, 0)) == NULL) {
5777 			VHCI_DEBUG(3, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5778 			    "phci dip doesn't exist. invalid path <%s>",
5779 			    s, phci_path));
5780 			retval = ENXIO;
5781 			break;
5782 		}
5783 
5784 		if (ndi_devi_config(pdip,
5785 		    NDI_DEVFS_CLEAN|NDI_DEVI_PERSIST) != NDI_SUCCESS) {
5786 			retval = EIO;
5787 		}
5788 
5789 		ddi_release_devi(pdip);
5790 		break;
5791 	}
5792 
5793 	case SCSI_VHCI_UNCONFIGURE_PHCI:
5794 	{
5795 		dev_info_t		*pdip;
5796 
5797 		/* Get PHCI path and device address from user land */
5798 		if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s)) {
5799 			retval = EFAULT;
5800 			break;
5801 		}
5802 
5803 		VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5804 		    "phci <%s>", s, phci_path));
5805 
5806 		/* Get the PHCI dip */
5807 		if ((pdip = e_ddi_hold_devi_by_path(phci_path, 0)) == NULL) {
5808 			VHCI_DEBUG(3, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5809 			    "phci dip doesn't exist. invalid path <%s>",
5810 			    s, phci_path));
5811 			retval = ENXIO;
5812 			break;
5813 		}
5814 
5815 		if (ndi_devi_unconfig(pdip,
5816 		    NDI_DEVI_REMOVE|NDI_DEVFS_CLEAN) != NDI_SUCCESS) {
5817 			retval = EBUSY;
5818 		}
5819 
5820 		ddi_release_devi(pdip);
5821 		break;
5822 	}
5823 #endif
5824 	}
5825 
5826 end:
5827 	/* Free the memory allocated above */
5828 	if (phci_path != NULL) {
5829 		kmem_free(phci_path, MAXPATHLEN);
5830 	}
5831 	if (client_path != NULL) {
5832 		kmem_free(client_path, MAXPATHLEN);
5833 	}
5834 	if (paddr != NULL) {
5835 		kmem_free(paddr, MAXNAMELEN);
5836 	}
5837 	return (retval);
5838 }
5839 
5840 /*
5841  * devctl IOCTL support for client device DR
5842  */
5843 /* ARGSUSED */
5844 int
5845 vhci_devctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
5846     int *rvalp)
5847 {
5848 	dev_info_t *self;
5849 	dev_info_t *child;
5850 	scsi_hba_tran_t *hba;
5851 	struct devctl_iocdata *dcp;
5852 	struct scsi_vhci *vhci;
5853 	int rv = 0;
5854 	int retval = 0;
5855 	scsi_vhci_priv_t *svp;
5856 	mdi_pathinfo_t  *pip;
5857 
5858 	if ((vhci = ddi_get_soft_state(vhci_softstate,
5859 	    MINOR2INST(getminor(dev)))) == NULL)
5860 		return (ENXIO);
5861 
5862 	/*
5863 	 * check if :devctl minor device has been opened
5864 	 */
5865 	mutex_enter(&vhci->vhci_mutex);
5866 	if ((vhci->vhci_state & VHCI_STATE_OPEN) == 0) {
5867 		mutex_exit(&vhci->vhci_mutex);
5868 		return (ENXIO);
5869 	}
5870 	mutex_exit(&vhci->vhci_mutex);
5871 
5872 	self = vhci->vhci_dip;
5873 	hba = ddi_get_driver_private(self);
5874 	if (hba == NULL)
5875 		return (ENXIO);
5876 
5877 	/*
5878 	 * We can use the generic implementation for these ioctls
5879 	 */
5880 	switch (cmd) {
5881 	case DEVCTL_DEVICE_GETSTATE:
5882 	case DEVCTL_DEVICE_ONLINE:
5883 	case DEVCTL_DEVICE_OFFLINE:
5884 	case DEVCTL_DEVICE_REMOVE:
5885 	case DEVCTL_BUS_GETSTATE:
5886 		return (ndi_devctl_ioctl(self, cmd, arg, mode, 0));
5887 	}
5888 
5889 	/*
5890 	 * read devctl ioctl data
5891 	 */
5892 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
5893 		return (EFAULT);
5894 
5895 	switch (cmd) {
5896 
5897 	case DEVCTL_DEVICE_RESET:
5898 		/*
5899 		 * lookup and hold child device
5900 		 */
5901 		if ((child = ndi_devi_find(self, ndi_dc_getname(dcp),
5902 		    ndi_dc_getaddr(dcp))) == NULL) {
5903 			rv = ENXIO;
5904 			break;
5905 		}
5906 		retval = mdi_select_path(child, NULL,
5907 		    (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH),
5908 		    NULL, &pip);
5909 		if ((retval != MDI_SUCCESS) || (pip == NULL)) {
5910 			VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioctl:"
5911 			    "Unable to get a path, dip 0x%p", (void *)child));
5912 			rv = ENXIO;
5913 			break;
5914 		}
5915 		svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
5916 		if (vhci_recovery_reset(svp->svp_svl,
5917 		    &svp->svp_psd->sd_address, TRUE,
5918 		    VHCI_DEPTH_TARGET) == 0) {
5919 			VHCI_DEBUG(1, (CE_NOTE, NULL,
5920 			    "!vhci_ioctl(pip:%p): "
5921 			    "reset failed\n", (void *)pip));
5922 			rv = ENXIO;
5923 		}
5924 		mdi_rele_path(pip);
5925 		break;
5926 
5927 	case DEVCTL_BUS_QUIESCE:
5928 	case DEVCTL_BUS_UNQUIESCE:
5929 	case DEVCTL_BUS_RESET:
5930 	case DEVCTL_BUS_RESETALL:
5931 #ifdef	DEBUG
5932 	case DEVCTL_BUS_CONFIGURE:
5933 	case DEVCTL_BUS_UNCONFIGURE:
5934 #endif
5935 		rv = ENOTSUP;
5936 		break;
5937 
5938 	default:
5939 		rv = ENOTTY;
5940 	} /* end of outer switch */
5941 
5942 	ndi_dc_freehdl(dcp);
5943 	return (rv);
5944 }
5945 
5946 /*
5947  * Routine to get the PHCI pathname from ioctl structures in userland
5948  */
5949 /* ARGSUSED */
5950 static int
5951 vhci_ioc_get_phci_path(sv_iocdata_t *pioc, caddr_t phci_path,
5952 	int mode, caddr_t s)
5953 {
5954 	int retval = 0;
5955 
5956 	if (ddi_copyin(pioc->phci, phci_path, MAXPATHLEN, mode)) {
5957 		VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioc_get_phci: ioctl <%s> "
5958 		    "phci_path copyin failed", s));
5959 		retval = EFAULT;
5960 	}
5961 	return (retval);
5962 
5963 }
5964 
5965 
5966 /*
5967  * Routine to get the Client device pathname from ioctl structures in userland
5968  */
5969 /* ARGSUSED */
5970 static int
5971 vhci_ioc_get_client_path(sv_iocdata_t *pioc, caddr_t client_path,
5972 	int mode, caddr_t s)
5973 {
5974 	int retval = 0;
5975 
5976 	if (ddi_copyin(pioc->client, client_path, MAXPATHLEN, mode)) {
5977 		VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioc_get_client: "
5978 		    "ioctl <%s> client_path copyin failed", s));
5979 		retval = EFAULT;
5980 	}
5981 	return (retval);
5982 }
5983 
5984 
5985 /*
5986  * Routine to get physical device address from ioctl structure in userland
5987  */
5988 /* ARGSUSED */
5989 static int
5990 vhci_ioc_get_paddr(sv_iocdata_t *pioc, caddr_t paddr, int mode, caddr_t s)
5991 {
5992 	int retval = 0;
5993 
5994 	if (ddi_copyin(pioc->addr, paddr, MAXNAMELEN, mode)) {
5995 		VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioc_get_paddr: "
5996 		    "ioctl <%s> device addr copyin failed", s));
5997 		retval = EFAULT;
5998 	}
5999 	return (retval);
6000 }
6001 
6002 
6003 /*
6004  * Routine to send client device pathname to userland.
6005  */
6006 /* ARGSUSED */
6007 static int
6008 vhci_ioc_send_client_path(caddr_t client_path, sv_iocdata_t *pioc,
6009 	int mode, caddr_t s)
6010 {
6011 	int retval = 0;
6012 
6013 	if (ddi_copyout(client_path, pioc->client, MAXPATHLEN, mode)) {
6014 		VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioc_send_client: "
6015 		    "ioctl <%s> client_path copyout failed", s));
6016 		retval = EFAULT;
6017 	}
6018 	return (retval);
6019 }
6020 
6021 
6022 /*
6023  * Routine to translated dev_info pointer (dip) to device pathname.
6024  */
6025 static void
6026 vhci_ioc_devi_to_path(dev_info_t *dip, caddr_t path)
6027 {
6028 	(void) ddi_pathname(dip, path);
6029 }
6030 
6031 
6032 /*
6033  * vhci_get_phci_path_list:
6034  *		get information about devices associated with a
6035  *		given PHCI device.
6036  *
6037  * Return Values:
6038  *		path information elements
6039  */
6040 int
6041 vhci_get_phci_path_list(dev_info_t *pdip, sv_path_info_t *pibuf,
6042 	uint_t num_elems)
6043 {
6044 	uint_t			count, done;
6045 	mdi_pathinfo_t		*pip;
6046 	sv_path_info_t		*ret_pip;
6047 	int			status;
6048 	size_t			prop_size;
6049 	int			circular;
6050 
6051 	/*
6052 	 * Get the PHCI structure and retrieve the path information
6053 	 * from the GUID hash table.
6054 	 */
6055 
6056 	ret_pip = pibuf;
6057 	count = 0;
6058 
6059 	ndi_devi_enter(pdip, &circular);
6060 
6061 	done = (count >= num_elems);
6062 	pip = mdi_get_next_client_path(pdip, NULL);
6063 	while (pip && !done) {
6064 		mdi_pi_lock(pip);
6065 		(void) ddi_pathname(mdi_pi_get_phci(pip),
6066 		    ret_pip->device.ret_phci);
6067 		(void) strcpy(ret_pip->ret_addr, mdi_pi_get_addr(pip));
6068 		(void) mdi_pi_get_state2(pip, &ret_pip->ret_state,
6069 		    &ret_pip->ret_ext_state);
6070 
6071 		status = mdi_prop_size(pip, &prop_size);
6072 		if (status == MDI_SUCCESS && ret_pip->ret_prop.ret_buf_size) {
6073 			*ret_pip->ret_prop.ret_buf_size = (uint_t)prop_size;
6074 		}
6075 
6076 #ifdef DEBUG
6077 		if (status != MDI_SUCCESS) {
6078 			VHCI_DEBUG(2, (CE_WARN, NULL,
6079 			    "!vhci_get_phci_path_list: "
6080 			    "phci <%s>, prop size failure 0x%x",
6081 			    ret_pip->device.ret_phci, status));
6082 		}
6083 #endif /* DEBUG */
6084 
6085 
6086 		if (status == MDI_SUCCESS && ret_pip->ret_prop.buf &&
6087 		    prop_size && ret_pip->ret_prop.buf_size >= prop_size) {
6088 			status = mdi_prop_pack(pip,
6089 			    &ret_pip->ret_prop.buf,
6090 			    ret_pip->ret_prop.buf_size);
6091 
6092 #ifdef DEBUG
6093 			if (status != MDI_SUCCESS) {
6094 				VHCI_DEBUG(2, (CE_WARN, NULL,
6095 				    "!vhci_get_phci_path_list: "
6096 				    "phci <%s>, prop pack failure 0x%x",
6097 				    ret_pip->device.ret_phci, status));
6098 			}
6099 #endif /* DEBUG */
6100 		}
6101 
6102 		mdi_pi_unlock(pip);
6103 		pip = mdi_get_next_client_path(pdip, pip);
6104 		ret_pip++;
6105 		count++;
6106 		done = (count >= num_elems);
6107 	}
6108 
6109 	ndi_devi_exit(pdip, circular);
6110 
6111 	return (MDI_SUCCESS);
6112 }
6113 
6114 
6115 /*
6116  * vhci_get_client_path_list:
6117  *		get information about various paths associated with a
6118  *		given client device.
6119  *
6120  * Return Values:
6121  *		path information elements
6122  */
6123 int
6124 vhci_get_client_path_list(dev_info_t *cdip, sv_path_info_t *pibuf,
6125 	uint_t num_elems)
6126 {
6127 	uint_t			count, done;
6128 	mdi_pathinfo_t		*pip;
6129 	sv_path_info_t		*ret_pip;
6130 	int			status;
6131 	size_t			prop_size;
6132 	int			circular;
6133 
6134 	ret_pip = pibuf;
6135 	count = 0;
6136 
6137 	ndi_devi_enter(cdip, &circular);
6138 
6139 	done = (count >= num_elems);
6140 	pip = mdi_get_next_phci_path(cdip, NULL);
6141 	while (pip && !done) {
6142 		mdi_pi_lock(pip);
6143 		(void) ddi_pathname(mdi_pi_get_phci(pip),
6144 		    ret_pip->device.ret_phci);
6145 		(void) strcpy(ret_pip->ret_addr, mdi_pi_get_addr(pip));
6146 		(void) mdi_pi_get_state2(pip, &ret_pip->ret_state,
6147 		    &ret_pip->ret_ext_state);
6148 
6149 		status = mdi_prop_size(pip, &prop_size);
6150 		if (status == MDI_SUCCESS && ret_pip->ret_prop.ret_buf_size) {
6151 			*ret_pip->ret_prop.ret_buf_size = (uint_t)prop_size;
6152 		}
6153 
6154 #ifdef DEBUG
6155 		if (status != MDI_SUCCESS) {
6156 			VHCI_DEBUG(2, (CE_WARN, NULL,
6157 			    "!vhci_get_client_path_list: "
6158 			    "phci <%s>, prop size failure 0x%x",
6159 			    ret_pip->device.ret_phci, status));
6160 		}
6161 #endif /* DEBUG */
6162 
6163 
6164 		if (status == MDI_SUCCESS && ret_pip->ret_prop.buf &&
6165 		    prop_size && ret_pip->ret_prop.buf_size >= prop_size) {
6166 			status = mdi_prop_pack(pip,
6167 			    &ret_pip->ret_prop.buf,
6168 			    ret_pip->ret_prop.buf_size);
6169 
6170 #ifdef DEBUG
6171 			if (status != MDI_SUCCESS) {
6172 				VHCI_DEBUG(2, (CE_WARN, NULL,
6173 				    "!vhci_get_client_path_list: "
6174 				    "phci <%s>, prop pack failure 0x%x",
6175 				    ret_pip->device.ret_phci, status));
6176 			}
6177 #endif /* DEBUG */
6178 		}
6179 
6180 		mdi_pi_unlock(pip);
6181 		pip = mdi_get_next_phci_path(cdip, pip);
6182 		ret_pip++;
6183 		count++;
6184 		done = (count >= num_elems);
6185 	}
6186 
6187 	ndi_devi_exit(cdip, circular);
6188 
6189 	return (MDI_SUCCESS);
6190 }
6191 
6192 
6193 /*
6194  * Routine to get ioctl argument structure from userland.
6195  */
6196 /* ARGSUSED */
6197 static int
6198 vhci_get_iocdata(const void *data, sv_iocdata_t *pioc, int mode, caddr_t s)
6199 {
6200 	int	retval = 0;
6201 
6202 #ifdef  _MULTI_DATAMODEL
6203 	switch (ddi_model_convert_from(mode & FMODELS)) {
6204 	case DDI_MODEL_ILP32:
6205 	{
6206 		sv_iocdata32_t	ioc32;
6207 
6208 		if (ddi_copyin(data, &ioc32, sizeof (ioc32), mode)) {
6209 			retval = EFAULT;
6210 			break;
6211 		}
6212 		pioc->client	= (caddr_t)(uintptr_t)ioc32.client;
6213 		pioc->phci	= (caddr_t)(uintptr_t)ioc32.phci;
6214 		pioc->addr	= (caddr_t)(uintptr_t)ioc32.addr;
6215 		pioc->buf_elem	= (uint_t)ioc32.buf_elem;
6216 		pioc->ret_buf	= (sv_path_info_t *)(uintptr_t)ioc32.ret_buf;
6217 		pioc->ret_elem	= (uint_t *)(uintptr_t)ioc32.ret_elem;
6218 		break;
6219 	}
6220 
6221 	case DDI_MODEL_NONE:
6222 		if (ddi_copyin(data, pioc, sizeof (*pioc), mode)) {
6223 			retval = EFAULT;
6224 			break;
6225 		}
6226 		break;
6227 	}
6228 #else   /* _MULTI_DATAMODEL */
6229 	if (ddi_copyin(data, pioc, sizeof (*pioc), mode)) {
6230 		retval = EFAULT;
6231 	}
6232 #endif  /* _MULTI_DATAMODEL */
6233 
6234 #ifdef DEBUG
6235 	if (retval) {
6236 		VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_get_ioc: cmd <%s> "
6237 		    "iocdata copyin failed", s));
6238 	}
6239 #endif
6240 
6241 	return (retval);
6242 }
6243 
6244 
6245 /*
6246  * Routine to get the ioctl argument for ioctl causing controller switchover.
6247  */
6248 /* ARGSUSED */
6249 static int
6250 vhci_get_iocswitchdata(const void *data, sv_switch_to_cntlr_iocdata_t *piocsc,
6251     int mode, caddr_t s)
6252 {
6253 	int	retval = 0;
6254 
6255 #ifdef  _MULTI_DATAMODEL
6256 	switch (ddi_model_convert_from(mode & FMODELS)) {
6257 	case DDI_MODEL_ILP32:
6258 	{
6259 		sv_switch_to_cntlr_iocdata32_t	ioc32;
6260 
6261 		if (ddi_copyin(data, &ioc32, sizeof (ioc32), mode)) {
6262 			retval = EFAULT;
6263 			break;
6264 		}
6265 		piocsc->client	= (caddr_t)(uintptr_t)ioc32.client;
6266 		piocsc->class	= (caddr_t)(uintptr_t)ioc32.class;
6267 		break;
6268 	}
6269 
6270 	case DDI_MODEL_NONE:
6271 		if (ddi_copyin(data, piocsc, sizeof (*piocsc), mode)) {
6272 			retval = EFAULT;
6273 		}
6274 		break;
6275 	}
6276 #else   /* _MULTI_DATAMODEL */
6277 	if (ddi_copyin(data, piocsc, sizeof (*piocsc), mode)) {
6278 		retval = EFAULT;
6279 	}
6280 #endif  /* _MULTI_DATAMODEL */
6281 
6282 #ifdef DEBUG
6283 	if (retval) {
6284 		VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_get_ioc: cmd <%s> "
6285 		    "switch_to_cntlr_iocdata copyin failed", s));
6286 	}
6287 #endif
6288 
6289 	return (retval);
6290 }
6291 
6292 
6293 /*
6294  * Routine to allocate memory for the path information structures.
6295  * It allocates two chunks of memory - one for keeping userland
6296  * pointers/values for path information and path properties, second for
6297  * keeping allocating kernel memory for path properties. These path
6298  * properties are finally copied to userland.
6299  */
6300 /* ARGSUSED */
6301 static int
6302 vhci_ioc_alloc_pathinfo(sv_path_info_t **upibuf, sv_path_info_t **kpibuf,
6303     uint_t num_paths, sv_iocdata_t *pioc, int mode, caddr_t s)
6304 {
6305 	sv_path_info_t	*pi;
6306 	uint_t		bufsize;
6307 	int		retval = 0;
6308 	int		index;
6309 
6310 	/* Allocate memory */
6311 	*upibuf = (sv_path_info_t *)
6312 	    kmem_zalloc(sizeof (sv_path_info_t) * num_paths, KM_SLEEP);
6313 	ASSERT(*upibuf != NULL);
6314 	*kpibuf = (sv_path_info_t *)
6315 	    kmem_zalloc(sizeof (sv_path_info_t) * num_paths, KM_SLEEP);
6316 	ASSERT(*kpibuf != NULL);
6317 
6318 	/*
6319 	 * Get the path info structure from the user space.
6320 	 * We are interested in the following fields:
6321 	 *	- user size of buffer for per path properties.
6322 	 *	- user address of buffer for path info properties.
6323 	 *	- user pointer for returning actual buffer size
6324 	 * Keep these fields in the 'upibuf' structures.
6325 	 * Allocate buffer for per path info properties in kernel
6326 	 * structure ('kpibuf').
6327 	 * Size of these buffers will be equal to the size of buffers
6328 	 * in the user space.
6329 	 */
6330 #ifdef  _MULTI_DATAMODEL
6331 	switch (ddi_model_convert_from(mode & FMODELS)) {
6332 	case DDI_MODEL_ILP32:
6333 	{
6334 		sv_path_info32_t	*src;
6335 		sv_path_info32_t	pi32;
6336 
6337 		src  = (sv_path_info32_t *)pioc->ret_buf;
6338 		pi = (sv_path_info_t *)*upibuf;
6339 		for (index = 0; index < num_paths; index++, src++, pi++) {
6340 			if (ddi_copyin(src, &pi32, sizeof (pi32), mode)) {
6341 				retval = EFAULT;
6342 				break;
6343 			}
6344 
6345 			pi->ret_prop.buf_size	=
6346 			    (uint_t)pi32.ret_prop.buf_size;
6347 			pi->ret_prop.ret_buf_size =
6348 			    (uint_t *)(uintptr_t)pi32.ret_prop.ret_buf_size;
6349 			pi->ret_prop.buf	=
6350 			    (caddr_t)(uintptr_t)pi32.ret_prop.buf;
6351 		}
6352 		break;
6353 	}
6354 
6355 	case DDI_MODEL_NONE:
6356 		if (ddi_copyin(pioc->ret_buf, *upibuf,
6357 		    sizeof (sv_path_info_t) * num_paths, mode)) {
6358 			retval = EFAULT;
6359 		}
6360 		break;
6361 	}
6362 #else   /* _MULTI_DATAMODEL */
6363 	if (ddi_copyin(pioc->ret_buf, *upibuf,
6364 	    sizeof (sv_path_info_t) * num_paths, mode)) {
6365 		retval = EFAULT;
6366 	}
6367 #endif  /* _MULTI_DATAMODEL */
6368 
6369 	if (retval != 0) {
6370 		VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_alloc_path_info: "
6371 		    "ioctl <%s> normal: path_info copyin failed", s));
6372 		kmem_free(*upibuf, sizeof (sv_path_info_t) * num_paths);
6373 		kmem_free(*kpibuf, sizeof (sv_path_info_t) * num_paths);
6374 		*upibuf = NULL;
6375 		*kpibuf = NULL;
6376 		return (retval);
6377 	}
6378 
6379 	/*
6380 	 * Allocate memory for per path properties.
6381 	 */
6382 	for (index = 0, pi = *kpibuf; index < num_paths; index++, pi++) {
6383 		bufsize = (*upibuf)[index].ret_prop.buf_size;
6384 
6385 		if (bufsize && bufsize <= SV_PROP_MAX_BUF_SIZE) {
6386 			pi->ret_prop.buf_size = bufsize;
6387 			pi->ret_prop.buf = (caddr_t)
6388 			    kmem_zalloc(bufsize, KM_SLEEP);
6389 			ASSERT(pi->ret_prop.buf != NULL);
6390 		} else {
6391 			pi->ret_prop.buf_size = 0;
6392 			pi->ret_prop.buf = NULL;
6393 		}
6394 
6395 		if ((*upibuf)[index].ret_prop.ret_buf_size != NULL) {
6396 			pi->ret_prop.ret_buf_size = (uint_t *)kmem_zalloc(
6397 			    sizeof (*pi->ret_prop.ret_buf_size), KM_SLEEP);
6398 			ASSERT(pi->ret_prop.ret_buf_size != NULL);
6399 		} else {
6400 			pi->ret_prop.ret_buf_size = NULL;
6401 		}
6402 	}
6403 
6404 	return (0);
6405 }
6406 
6407 
6408 /*
6409  * Routine to free memory for the path information structures.
6410  * This is the memory which was allocated earlier.
6411  */
6412 /* ARGSUSED */
6413 static void
6414 vhci_ioc_free_pathinfo(sv_path_info_t *upibuf, sv_path_info_t *kpibuf,
6415     uint_t num_paths)
6416 {
6417 	sv_path_info_t	*pi;
6418 	int		index;
6419 
6420 	/* Free memory for per path properties */
6421 	for (index = 0, pi = kpibuf; index < num_paths; index++, pi++) {
6422 		if (pi->ret_prop.ret_buf_size != NULL) {
6423 			kmem_free(pi->ret_prop.ret_buf_size,
6424 			    sizeof (*pi->ret_prop.ret_buf_size));
6425 		}
6426 
6427 		if (pi->ret_prop.buf != NULL) {
6428 			kmem_free(pi->ret_prop.buf, pi->ret_prop.buf_size);
6429 		}
6430 	}
6431 
6432 	/* Free memory for path info structures */
6433 	kmem_free(upibuf, sizeof (sv_path_info_t) * num_paths);
6434 	kmem_free(kpibuf, sizeof (sv_path_info_t) * num_paths);
6435 }
6436 
6437 
6438 /*
6439  * Routine to copy path information and path properties to userland.
6440  */
6441 /* ARGSUSED */
6442 static int
6443 vhci_ioc_send_pathinfo(sv_path_info_t *upibuf, sv_path_info_t *kpibuf,
6444     uint_t num_paths, sv_iocdata_t *pioc, int mode, caddr_t s)
6445 {
6446 	int			retval = 0, index;
6447 	sv_path_info_t		*upi_ptr;
6448 	sv_path_info32_t	*upi32_ptr;
6449 
6450 #ifdef  _MULTI_DATAMODEL
6451 	switch (ddi_model_convert_from(mode & FMODELS)) {
6452 	case DDI_MODEL_ILP32:
6453 		goto copy_32bit;
6454 
6455 	case DDI_MODEL_NONE:
6456 		goto copy_normal;
6457 	}
6458 #else   /* _MULTI_DATAMODEL */
6459 
6460 	goto copy_normal;
6461 
6462 #endif  /* _MULTI_DATAMODEL */
6463 
6464 copy_normal:
6465 
6466 	/*
6467 	 * Copy path information and path properties to user land.
6468 	 * Pointer fields inside the path property structure were
6469 	 * saved in the 'upibuf' structure earlier.
6470 	 */
6471 	upi_ptr = pioc->ret_buf;
6472 	for (index = 0; index < num_paths; index++) {
6473 		if (ddi_copyout(kpibuf[index].device.ret_ct,
6474 		    upi_ptr[index].device.ret_ct, MAXPATHLEN, mode)) {
6475 			retval = EFAULT;
6476 			break;
6477 		}
6478 
6479 		if (ddi_copyout(kpibuf[index].ret_addr,
6480 		    upi_ptr[index].ret_addr, MAXNAMELEN, mode)) {
6481 			retval = EFAULT;
6482 			break;
6483 		}
6484 
6485 		if (ddi_copyout(&kpibuf[index].ret_state,
6486 		    &upi_ptr[index].ret_state, sizeof (kpibuf[index].ret_state),
6487 		    mode)) {
6488 			retval = EFAULT;
6489 			break;
6490 		}
6491 
6492 		if (ddi_copyout(&kpibuf[index].ret_ext_state,
6493 		    &upi_ptr[index].ret_ext_state,
6494 		    sizeof (kpibuf[index].ret_ext_state), mode)) {
6495 			retval = EFAULT;
6496 			break;
6497 		}
6498 
6499 		if ((kpibuf[index].ret_prop.ret_buf_size != NULL) &&
6500 		    ddi_copyout(kpibuf[index].ret_prop.ret_buf_size,
6501 		    upibuf[index].ret_prop.ret_buf_size,
6502 		    sizeof (*upibuf[index].ret_prop.ret_buf_size), mode)) {
6503 			retval = EFAULT;
6504 			break;
6505 		}
6506 
6507 		if ((kpibuf[index].ret_prop.buf != NULL) &&
6508 		    ddi_copyout(kpibuf[index].ret_prop.buf,
6509 		    upibuf[index].ret_prop.buf,
6510 		    upibuf[index].ret_prop.buf_size, mode)) {
6511 			retval = EFAULT;
6512 			break;
6513 		}
6514 	}
6515 
6516 #ifdef DEBUG
6517 	if (retval) {
6518 		VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_get_ioc: ioctl <%s> "
6519 		    "normal: path_info copyout failed", s));
6520 	}
6521 #endif
6522 
6523 	return (retval);
6524 
6525 copy_32bit:
6526 	/*
6527 	 * Copy path information and path properties to user land.
6528 	 * Pointer fields inside the path property structure were
6529 	 * saved in the 'upibuf' structure earlier.
6530 	 */
6531 	upi32_ptr = (sv_path_info32_t *)pioc->ret_buf;
6532 	for (index = 0; index < num_paths; index++) {
6533 		if (ddi_copyout(kpibuf[index].device.ret_ct,
6534 		    upi32_ptr[index].device.ret_ct, MAXPATHLEN, mode)) {
6535 			retval = EFAULT;
6536 			break;
6537 		}
6538 
6539 		if (ddi_copyout(kpibuf[index].ret_addr,
6540 		    upi32_ptr[index].ret_addr, MAXNAMELEN, mode)) {
6541 			retval = EFAULT;
6542 			break;
6543 		}
6544 
6545 		if (ddi_copyout(&kpibuf[index].ret_state,
6546 		    &upi32_ptr[index].ret_state,
6547 		    sizeof (kpibuf[index].ret_state), mode)) {
6548 			retval = EFAULT;
6549 			break;
6550 		}
6551 
6552 		if (ddi_copyout(&kpibuf[index].ret_ext_state,
6553 		    &upi32_ptr[index].ret_ext_state,
6554 		    sizeof (kpibuf[index].ret_ext_state), mode)) {
6555 			retval = EFAULT;
6556 			break;
6557 		}
6558 		if ((kpibuf[index].ret_prop.ret_buf_size != NULL) &&
6559 		    ddi_copyout(kpibuf[index].ret_prop.ret_buf_size,
6560 		    upibuf[index].ret_prop.ret_buf_size,
6561 		    sizeof (*upibuf[index].ret_prop.ret_buf_size), mode)) {
6562 			retval = EFAULT;
6563 			break;
6564 		}
6565 
6566 		if ((kpibuf[index].ret_prop.buf != NULL) &&
6567 		    ddi_copyout(kpibuf[index].ret_prop.buf,
6568 		    upibuf[index].ret_prop.buf,
6569 		    upibuf[index].ret_prop.buf_size, mode)) {
6570 			retval = EFAULT;
6571 			break;
6572 		}
6573 	}
6574 
6575 #ifdef DEBUG
6576 	if (retval) {
6577 		VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_get_ioc: ioctl <%s> "
6578 		    "normal: path_info copyout failed", s));
6579 	}
6580 #endif
6581 
6582 	return (retval);
6583 }
6584 
6585 
6586 /*
6587  * vhci_failover()
6588  * This routine expects VHCI_HOLD_LUN before being invoked.  It can be invoked
6589  * as MDI_FAILOVER_ASYNC or MDI_FAILOVER_SYNC.  For Asynchronous failovers
6590  * this routine shall VHCI_RELEASE_LUN on exiting.  For synchronous failovers
6591  * it is the callers responsibility to release lun.
6592  */
6593 
6594 /* ARGSUSED */
6595 static int
6596 vhci_failover(dev_info_t *vdip, dev_info_t *cdip, int flags)
6597 {
6598 	char			*guid;
6599 	scsi_vhci_lun_t		*vlun = NULL;
6600 	struct scsi_vhci	*vhci;
6601 	mdi_pathinfo_t		*pip, *npip;
6602 	char			*s_pclass, *pclass1, *pclass2, *pclass;
6603 	char			active_pclass_copy[255], *active_pclass_ptr;
6604 	char			*ptr1, *ptr2;
6605 	mdi_pathinfo_state_t	pi_state;
6606 	uint32_t		pi_ext_state;
6607 	scsi_vhci_priv_t	*svp;
6608 	struct scsi_device	*sd;
6609 	struct scsi_failover_ops	*sfo;
6610 	int			sps; /* mdi_select_path() status */
6611 	int			activation_done = 0;
6612 	int			rval, retval = MDI_FAILURE;
6613 	int			reserve_pending, check_condition, UA_condition;
6614 	struct scsi_pkt		*pkt;
6615 	struct buf		*bp;
6616 
6617 	vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip));
6618 	sd = ddi_get_driver_private(cdip);
6619 	vlun = ADDR2VLUN(&sd->sd_address);
6620 	ASSERT(vlun != 0);
6621 	ASSERT(VHCI_LUN_IS_HELD(vlun));
6622 	guid = vlun->svl_lun_wwn;
6623 	VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(1): guid %s\n", guid));
6624 	vhci_log(CE_NOTE, vdip, "!Initiating failover for device %s "
6625 	    "(GUID %s)", ddi_node_name(cdip), guid);
6626 
6627 	/*
6628 	 * Lets maintain a local copy of the vlun->svl_active_pclass
6629 	 * for the rest of the processing. Accessing the field
6630 	 * directly in the loop below causes loop logic to break
6631 	 * especially when the field gets updated by other threads
6632 	 * update path status etc and causes 'paths are not currently
6633 	 * available' condition to be declared prematurely.
6634 	 */
6635 	mutex_enter(&vlun->svl_mutex);
6636 	if (vlun->svl_active_pclass != NULL) {
6637 		(void) strlcpy(active_pclass_copy, vlun->svl_active_pclass,
6638 		    sizeof (active_pclass_copy));
6639 		active_pclass_ptr = &active_pclass_copy[0];
6640 		mutex_exit(&vlun->svl_mutex);
6641 		if (vhci_quiesce_paths(vdip, cdip, vlun, guid,
6642 		    active_pclass_ptr) != 0) {
6643 			retval = MDI_FAILURE;
6644 		}
6645 	} else {
6646 		/*
6647 		 * can happen only when the available path to device
6648 		 * discovered is a STANDBY path.
6649 		 */
6650 		mutex_exit(&vlun->svl_mutex);
6651 		active_pclass_copy[0] = '\0';
6652 		active_pclass_ptr = NULL;
6653 	}
6654 
6655 	sfo = vlun->svl_fops;
6656 	ASSERT(sfo != NULL);
6657 	pclass1 = s_pclass = active_pclass_ptr;
6658 	VHCI_DEBUG(1, (CE_NOTE, NULL, "!(%s)failing over from %s\n", guid,
6659 	    (s_pclass == NULL ? "<none>" : s_pclass)));
6660 
6661 next_pathclass:
6662 
6663 	rval = (*sfo->sfo_pathclass_next)(pclass1, &pclass2,
6664 	    vlun->svl_fops_ctpriv);
6665 	if (rval == ENOENT) {
6666 		if (s_pclass == NULL) {
6667 			VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(4)(%s): "
6668 			    "failed, no more pathclasses\n", guid));
6669 			goto done;
6670 		} else {
6671 			(*sfo->sfo_pathclass_next)(NULL, &pclass2,
6672 			    vlun->svl_fops_ctpriv);
6673 		}
6674 	} else if (rval == EINVAL) {
6675 		vhci_log(CE_NOTE, vdip, "!Failover operation failed for "
6676 		    "device %s (GUID %s): Invalid path-class %s",
6677 		    ddi_node_name(cdip), guid,
6678 		    ((pclass1 == NULL) ? "<none>" : pclass1));
6679 		goto done;
6680 	}
6681 	if ((s_pclass != NULL) && (strcmp(pclass2, s_pclass) == 0)) {
6682 		/*
6683 		 * paths are not currently available
6684 		 */
6685 		vhci_log(CE_NOTE, vdip, "!Failover path currently unavailable"
6686 		    " for device %s (GUID %s)",
6687 		    ddi_node_name(cdip), guid);
6688 		goto done;
6689 	}
6690 	pip = npip = NULL;
6691 	VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(5.2)(%s): considering "
6692 	    "%s as failover destination\n", guid, pclass2));
6693 	sps = mdi_select_path(cdip, NULL, MDI_SELECT_STANDBY_PATH, NULL, &npip);
6694 	if ((npip == NULL) || (sps != MDI_SUCCESS)) {
6695 		VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(%s): no "
6696 		    "STANDBY paths found (status:%x)!\n", guid, sps));
6697 		pclass1 = pclass2;
6698 		goto next_pathclass;
6699 	}
6700 	do {
6701 		pclass = NULL;
6702 		if ((mdi_prop_lookup_string(npip, "path-class",
6703 		    &pclass) != MDI_SUCCESS) || (strcmp(pclass2,
6704 		    pclass) != 0)) {
6705 			VHCI_DEBUG(1, (CE_NOTE, NULL,
6706 			    "!vhci_failover(5.5)(%s): skipping path "
6707 			    "%p(%s)...\n", guid, (void *)npip, pclass));
6708 			pip = npip;
6709 			sps = mdi_select_path(cdip, NULL,
6710 			    MDI_SELECT_STANDBY_PATH, pip, &npip);
6711 			mdi_rele_path(pip);
6712 			(void) mdi_prop_free(pclass);
6713 			continue;
6714 		}
6715 		svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(npip);
6716 
6717 		/*
6718 		 * Issue READ at non-zer block on this STANDBY path.
6719 		 * Purple returns
6720 		 * 1. RESERVATION_CONFLICT if reservation is pending
6721 		 * 2. POR check condition if it reset happened.
6722 		 * 2. failover Check Conditions if one is already in progress.
6723 		 */
6724 		reserve_pending = 0;
6725 		check_condition = 0;
6726 		UA_condition = 0;
6727 
6728 		bp = scsi_alloc_consistent_buf(&svp->svp_psd->sd_address,
6729 		    (struct buf *)NULL, DEV_BSIZE, B_READ, NULL, NULL);
6730 		if (!bp) {
6731 			VHCI_DEBUG(1, (CE_NOTE, NULL,
6732 			    "vhci_failover !No resources (buf)\n"));
6733 			mdi_rele_path(npip);
6734 			goto done;
6735 		}
6736 		pkt = scsi_init_pkt(&svp->svp_psd->sd_address, NULL, bp,
6737 		    CDB_GROUP1, sizeof (struct scsi_arq_status), 0,
6738 		    PKT_CONSISTENT, NULL, NULL);
6739 		if (pkt) {
6740 			(void) scsi_setup_cdb((union scsi_cdb *)(uintptr_t)
6741 			    pkt->pkt_cdbp, SCMD_READ, 1, 1, 0);
6742 			pkt->pkt_flags = FLAG_NOINTR;
6743 check_path_again:
6744 			pkt->pkt_time = 3*30;
6745 			if (scsi_transport(pkt) == TRAN_ACCEPT) {
6746 				switch (pkt->pkt_reason) {
6747 				case CMD_CMPLT:
6748 					switch (SCBP_C(pkt)) {
6749 					case STATUS_GOOD:
6750 						/* Already failed over */
6751 						activation_done = 1;
6752 						break;
6753 					case STATUS_RESERVATION_CONFLICT:
6754 						reserve_pending = 1;
6755 						break;
6756 					case STATUS_CHECK:
6757 						check_condition = 1;
6758 						break;
6759 					}
6760 				}
6761 			}
6762 			if (check_condition &&
6763 			    (pkt->pkt_state & STATE_ARQ_DONE)) {
6764 				struct scsi_extended_sense *sns =
6765 				    &(((struct scsi_arq_status *)(uintptr_t)
6766 				    (pkt->pkt_scbp))->sts_sensedata);
6767 				if (sns->es_key == KEY_UNIT_ATTENTION &&
6768 				    sns->es_add_code == 0x29) {
6769 					/* Already failed over */
6770 					VHCI_DEBUG(1, (CE_NOTE, NULL,
6771 					    "!vhci_failover(7)(%s): "
6772 					    "path 0x%p POR UA condition\n",
6773 					    guid, (void *)npip));
6774 					if (UA_condition == 0) {
6775 						UA_condition = 1;
6776 						goto check_path_again;
6777 					}
6778 				} else {
6779 					activation_done = 0;
6780 					VHCI_DEBUG(1, (CE_NOTE, NULL,
6781 					    "!vhci_failover(%s): path 0x%p "
6782 					    "unhandled chkcond %x %x %x\n",
6783 					    guid, (void *)npip, sns->es_key,
6784 					    sns->es_add_code,
6785 					    sns->es_qual_code));
6786 				}
6787 			}
6788 			scsi_destroy_pkt(pkt);
6789 		}
6790 		scsi_free_consistent_buf(bp);
6791 
6792 		if (activation_done) {
6793 			mdi_rele_path(npip);
6794 			VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(7)(%s): "
6795 			    "path 0x%p already failedover\n", guid,
6796 			    (void *)npip));
6797 			break;
6798 		}
6799 		if (reserve_pending && (vlun->svl_xlf_capable == 0)) {
6800 			(void) vhci_recovery_reset(vlun,
6801 			    &svp->svp_psd->sd_address,
6802 			    FALSE, VHCI_DEPTH_ALL);
6803 		}
6804 		VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(6)(%s): "
6805 		    "activating path 0x%p(psd:%p)\n", guid, (void *)npip,
6806 		    (void *)svp->svp_psd));
6807 		if ((*sfo->sfo_path_activate)(svp->svp_psd, pclass2,
6808 		    vlun->svl_fops_ctpriv) == 0) {
6809 			activation_done = 1;
6810 			mdi_rele_path(npip);
6811 			VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(7)(%s): "
6812 			    "path 0x%p successfully activated\n", guid,
6813 			    (void *)npip));
6814 			break;
6815 		}
6816 		pip = npip;
6817 		sps = mdi_select_path(cdip, NULL, MDI_SELECT_STANDBY_PATH,
6818 		    pip, &npip);
6819 		mdi_rele_path(pip);
6820 	} while ((npip != NULL) && (sps == MDI_SUCCESS));
6821 	if (activation_done == 0) {
6822 		pclass1 = pclass2;
6823 		goto next_pathclass;
6824 	}
6825 
6826 	/*
6827 	 * if we are here, we have succeeded in activating path npip of
6828 	 * pathclass pclass2; let us validate all paths of pclass2 by
6829 	 * "ping"-ing each one and mark the good ones ONLINE
6830 	 * Also, set the state of the paths belonging to the previously
6831 	 * active pathclass to STANDBY
6832 	 */
6833 	pip = npip = NULL;
6834 	sps = mdi_select_path(cdip, NULL, (MDI_SELECT_ONLINE_PATH |
6835 	    MDI_SELECT_STANDBY_PATH | MDI_SELECT_USER_DISABLE_PATH),
6836 	    NULL, &npip);
6837 	if (npip == NULL || sps != MDI_SUCCESS) {
6838 		VHCI_DEBUG(1, (CE_NOTE, NULL, "!Failover operation failed for "
6839 		    "device %s (GUID %s): paths may be busy\n",
6840 		    ddi_node_name(cdip), guid));
6841 		goto done;
6842 	}
6843 	do {
6844 		(void) mdi_pi_get_state2(npip, &pi_state, &pi_ext_state);
6845 		if (mdi_prop_lookup_string(npip, "path-class", &pclass)
6846 		    != MDI_SUCCESS) {
6847 			pip = npip;
6848 			sps = mdi_select_path(cdip, NULL,
6849 			    (MDI_SELECT_ONLINE_PATH |
6850 			    MDI_SELECT_STANDBY_PATH |
6851 			    MDI_SELECT_USER_DISABLE_PATH),
6852 			    pip, &npip);
6853 			mdi_rele_path(pip);
6854 			continue;
6855 		}
6856 		if (strcmp(pclass, pclass2) == 0) {
6857 			if (pi_state == MDI_PATHINFO_STATE_STANDBY) {
6858 				svp = (scsi_vhci_priv_t *)
6859 				    mdi_pi_get_vhci_private(npip);
6860 				VHCI_DEBUG(1, (CE_NOTE, NULL,
6861 				    "!vhci_failover(8)(%s): "
6862 				    "pinging path 0x%p\n",
6863 				    guid, (void *)npip));
6864 				if ((*sfo->sfo_path_ping)(svp->svp_psd,
6865 				    vlun->svl_fops_ctpriv) == 1) {
6866 					mdi_pi_set_state(npip,
6867 					    MDI_PATHINFO_STATE_ONLINE);
6868 					VHCI_DEBUG(1, (CE_NOTE, NULL,
6869 					    "!vhci_failover(9)(%s): "
6870 					    "path 0x%p ping successful, "
6871 					    "marked online\n", guid,
6872 					    (void *)npip));
6873 					MDI_PI_ERRSTAT(npip, MDI_PI_FAILTO);
6874 				}
6875 			}
6876 		} else if ((s_pclass != NULL) && (strcmp(pclass, s_pclass)
6877 		    == 0)) {
6878 			if (pi_state == MDI_PATHINFO_STATE_ONLINE) {
6879 				mdi_pi_set_state(npip,
6880 				    MDI_PATHINFO_STATE_STANDBY);
6881 				VHCI_DEBUG(1, (CE_NOTE, NULL,
6882 				    "!vhci_failover(10)(%s): path 0x%p marked "
6883 				    "STANDBY\n", guid, (void *)npip));
6884 				MDI_PI_ERRSTAT(npip, MDI_PI_FAILFROM);
6885 			}
6886 		}
6887 		(void) mdi_prop_free(pclass);
6888 		pip = npip;
6889 		sps = mdi_select_path(cdip, NULL, (MDI_SELECT_ONLINE_PATH |
6890 		    MDI_SELECT_STANDBY_PATH|MDI_SELECT_USER_DISABLE_PATH),
6891 		    pip, &npip);
6892 		mdi_rele_path(pip);
6893 	} while ((npip != NULL) && (sps == MDI_SUCCESS));
6894 
6895 	/*
6896 	 * Update the AccessState of related MP-API TPGs
6897 	 */
6898 	(void) vhci_mpapi_update_tpg_acc_state_for_lu(vhci, vlun);
6899 
6900 	vhci_log(CE_NOTE, vdip, "!Failover operation completed successfully "
6901 	    "for device %s (GUID %s): failed over from %s to %s",
6902 	    ddi_node_name(cdip), guid, ((s_pclass == NULL) ? "<none>" :
6903 	    s_pclass), pclass2);
6904 	ptr1 = kmem_alloc(strlen(pclass2)+1, KM_SLEEP);
6905 	(void) strlcpy(ptr1, pclass2, (strlen(pclass2)+1));
6906 	mutex_enter(&vlun->svl_mutex);
6907 	ptr2 = vlun->svl_active_pclass;
6908 	vlun->svl_active_pclass = ptr1;
6909 	mutex_exit(&vlun->svl_mutex);
6910 	if (ptr2) {
6911 		kmem_free(ptr2, strlen(ptr2)+1);
6912 	}
6913 	mutex_enter(&vhci->vhci_mutex);
6914 	scsi_hba_reset_notify_callback(&vhci->vhci_mutex,
6915 	    &vhci->vhci_reset_notify_listf);
6916 	/* All reservations are cleared upon these resets. */
6917 	vlun->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG;
6918 	mutex_exit(&vhci->vhci_mutex);
6919 	VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(11): DONE! Active "
6920 	    "pathclass for %s is now %s\n", guid, pclass2));
6921 	retval = MDI_SUCCESS;
6922 
6923 done:
6924 	if (flags == MDI_FAILOVER_ASYNC) {
6925 		VHCI_RELEASE_LUN(vlun);
6926 		VHCI_DEBUG(6, (CE_NOTE, NULL, "!vhci_failover(12): DONE! "
6927 		    "releasing lun, as failover was ASYNC\n"));
6928 	} else {
6929 		VHCI_DEBUG(6, (CE_NOTE, NULL, "!vhci_failover(12): DONE! "
6930 		    "NOT releasing lun, as failover was SYNC\n"));
6931 	}
6932 	return (retval);
6933 }
6934 
6935 /*
6936  * vhci_client_attached is called after the successful attach of a
6937  * client devinfo node.
6938  */
6939 static void
6940 vhci_client_attached(dev_info_t *cdip)
6941 {
6942 	mdi_pathinfo_t	*pip;
6943 	int		circular;
6944 
6945 	/*
6946 	 * At this point the client has attached and it's instance number is
6947 	 * valid, so we can set up kstats.  We need to do this here because it
6948 	 * is possible for paths to go online prior to client attach, in which
6949 	 * case the call to vhci_kstat_create_pathinfo in vhci_pathinfo_online
6950 	 * was a noop.
6951 	 */
6952 	ndi_devi_enter(cdip, &circular);
6953 	for (pip = mdi_get_next_phci_path(cdip, NULL); pip;
6954 	    pip = mdi_get_next_phci_path(cdip, pip))
6955 		vhci_kstat_create_pathinfo(pip);
6956 	ndi_devi_exit(cdip, circular);
6957 }
6958 
6959 /*
6960  * quiesce all of the online paths
6961  */
6962 static int
6963 vhci_quiesce_paths(dev_info_t *vdip, dev_info_t *cdip, scsi_vhci_lun_t *vlun,
6964 	char *guid, char *active_pclass_ptr)
6965 {
6966 	scsi_vhci_priv_t	*svp;
6967 	char			*s_pclass = NULL;
6968 	mdi_pathinfo_t		*npip, *pip;
6969 	int			sps;
6970 
6971 	/* quiesce currently active paths */
6972 	s_pclass = NULL;
6973 	pip = npip = NULL;
6974 	sps = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH, NULL, &npip);
6975 	if ((npip == NULL) || (sps != MDI_SUCCESS)) {
6976 		return (1);
6977 	}
6978 	do {
6979 		if (mdi_prop_lookup_string(npip, "path-class",
6980 		    &s_pclass) != MDI_SUCCESS) {
6981 			mdi_rele_path(npip);
6982 			vhci_log(CE_NOTE, vdip, "!Failover operation failed "
6983 			    "for device %s (GUID %s) due to an internal "
6984 			    "error", ddi_node_name(cdip), guid);
6985 			return (1);
6986 		}
6987 		if (strcmp(s_pclass, active_pclass_ptr) == 0) {
6988 			/*
6989 			 * quiesce path. Free s_pclass since
6990 			 * we don't need it anymore
6991 			 */
6992 			VHCI_DEBUG(1, (CE_NOTE, NULL,
6993 			    "!vhci_failover(2)(%s): failing over "
6994 			    "from %s; quiescing path %p\n",
6995 			    guid, s_pclass, (void *)npip));
6996 			(void) mdi_prop_free(s_pclass);
6997 			svp = (scsi_vhci_priv_t *)
6998 			    mdi_pi_get_vhci_private(npip);
6999 			if (svp == NULL) {
7000 				VHCI_DEBUG(1, (CE_NOTE, NULL,
7001 				    "!vhci_failover(2.5)(%s): no "
7002 				    "client priv! %p offlined?\n",
7003 				    guid, (void *)npip));
7004 				pip = npip;
7005 				sps = mdi_select_path(cdip, NULL,
7006 				    MDI_SELECT_ONLINE_PATH, pip, &npip);
7007 				mdi_rele_path(pip);
7008 				continue;
7009 			}
7010 			if (scsi_abort(&svp->svp_psd->sd_address, NULL)
7011 			    == 0) {
7012 				(void) vhci_recovery_reset(vlun,
7013 				    &svp->svp_psd->sd_address, FALSE,
7014 				    VHCI_DEPTH_TARGET);
7015 			}
7016 			mutex_enter(&svp->svp_mutex);
7017 			if (svp->svp_cmds == 0) {
7018 				VHCI_DEBUG(1, (CE_NOTE, NULL,
7019 				    "!vhci_failover(3)(%s):"
7020 				    "quiesced path %p\n", guid, (void *)npip));
7021 			} else {
7022 				while (svp->svp_cmds != 0) {
7023 					cv_wait(&svp->svp_cv, &svp->svp_mutex);
7024 					VHCI_DEBUG(1, (CE_NOTE, NULL,
7025 					    "!vhci_failover(3.cv)(%s):"
7026 					    "quiesced path %p\n", guid,
7027 					    (void *)npip));
7028 				}
7029 			}
7030 			mutex_exit(&svp->svp_mutex);
7031 		} else {
7032 			/*
7033 			 * make sure we freeup the memory
7034 			 */
7035 			(void) mdi_prop_free(s_pclass);
7036 		}
7037 		pip = npip;
7038 		sps = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH,
7039 		    pip, &npip);
7040 		mdi_rele_path(pip);
7041 	} while ((npip != NULL) && (sps == MDI_SUCCESS));
7042 	return (0);
7043 }
7044 
7045 static struct scsi_vhci_lun *
7046 vhci_lun_lookup(dev_info_t *tgt_dip)
7047 {
7048 	return ((struct scsi_vhci_lun *)
7049 	    mdi_client_get_vhci_private(tgt_dip));
7050 }
7051 
7052 static struct scsi_vhci_lun *
7053 vhci_lun_lookup_alloc(dev_info_t *tgt_dip, char *guid, int *didalloc)
7054 {
7055 	struct scsi_vhci_lun *svl;
7056 
7057 	if (svl = vhci_lun_lookup(tgt_dip)) {
7058 		return (svl);
7059 	}
7060 
7061 	svl = kmem_zalloc(sizeof (*svl), KM_SLEEP);
7062 	svl->svl_lun_wwn = kmem_zalloc(strlen(guid)+1, KM_SLEEP);
7063 	(void) strcpy(svl->svl_lun_wwn,  guid);
7064 	mutex_init(&svl->svl_mutex, NULL, MUTEX_DRIVER, NULL);
7065 	cv_init(&svl->svl_cv, NULL, CV_DRIVER, NULL);
7066 	sema_init(&svl->svl_pgr_sema, 1, NULL, SEMA_DRIVER, NULL);
7067 	svl->svl_waiting_for_activepath = 1;
7068 	svl->svl_sector_size = 1;
7069 	mdi_client_set_vhci_private(tgt_dip, svl);
7070 	*didalloc = 1;
7071 	VHCI_DEBUG(1, (CE_NOTE, NULL,
7072 	    "vhci_lun_lookup_alloc: guid %s vlun 0x%p\n",
7073 	    guid, (void *)svl));
7074 	return (svl);
7075 }
7076 
7077 static void
7078 vhci_lun_free(dev_info_t *tgt_dip)
7079 {
7080 	struct scsi_vhci_lun *dvlp;
7081 	char *guid;
7082 	struct scsi_device *sd;
7083 
7084 	/*
7085 	 * The scsi_device was set to driver private during child node
7086 	 * initialization in the scsi_hba_bus_ctl().
7087 	 */
7088 	sd = (struct scsi_device *)ddi_get_driver_private(tgt_dip);
7089 
7090 	dvlp = (struct scsi_vhci_lun *)
7091 	    mdi_client_get_vhci_private(tgt_dip);
7092 	ASSERT(dvlp != NULL);
7093 
7094 	mdi_client_set_vhci_private(tgt_dip, NULL);
7095 
7096 	guid = dvlp->svl_lun_wwn;
7097 	ASSERT(guid != NULL);
7098 	VHCI_DEBUG(4, (CE_NOTE, NULL, "!vhci_lun_free: %s\n", guid));
7099 
7100 	mutex_enter(&dvlp->svl_mutex);
7101 	if (dvlp->svl_active_pclass != NULL) {
7102 		kmem_free(dvlp->svl_active_pclass,
7103 		    strlen(dvlp->svl_active_pclass)+1);
7104 	}
7105 	dvlp->svl_active_pclass = NULL;
7106 	mutex_exit(&dvlp->svl_mutex);
7107 
7108 	if (dvlp->svl_lun_wwn != NULL) {
7109 		kmem_free(dvlp->svl_lun_wwn, strlen(dvlp->svl_lun_wwn)+1);
7110 	}
7111 	dvlp->svl_lun_wwn = NULL;
7112 
7113 	if (dvlp->svl_fops_name) {
7114 		kmem_free(dvlp->svl_fops_name, strlen(dvlp->svl_fops_name)+1);
7115 	}
7116 	dvlp->svl_fops_name = NULL;
7117 
7118 	if (dvlp->svl_flags & VLUN_TASK_D_ALIVE_FLG)
7119 		taskq_destroy(dvlp->svl_taskq);
7120 
7121 	mutex_destroy(&dvlp->svl_mutex);
7122 	cv_destroy(&dvlp->svl_cv);
7123 	sema_destroy(&dvlp->svl_pgr_sema);
7124 	kmem_free(dvlp, sizeof (*dvlp));
7125 	/*
7126 	 * vhci_lun_free may be called before the tgt_dip
7127 	 * initialization so check if the sd is NULL.
7128 	 */
7129 	if (sd != NULL)
7130 		sd->sd_address.a_hba_tran->tran_tgt_private = NULL;
7131 }
7132 
7133 
7134 int
7135 vhci_do_scsi_cmd(struct scsi_pkt *pkt)
7136 {
7137 	int	err = 0;
7138 	int	retry_cnt = 0;
7139 	struct scsi_extended_sense	*sns;
7140 
7141 retry:
7142 	err = scsi_poll(pkt);
7143 	if (err) {
7144 		if (pkt->pkt_cdbp[0] == SCMD_RELEASE) {
7145 			if (SCBP_C(pkt) == STATUS_RESERVATION_CONFLICT) {
7146 				VHCI_DEBUG(1, (CE_NOTE, NULL,
7147 				    "!v_s_do_s_c: RELEASE conflict\n"));
7148 				return (0);
7149 			}
7150 		}
7151 		if (retry_cnt++ < 3) {
7152 			VHCI_DEBUG(1, (CE_WARN, NULL,
7153 			    "!v_s_do_s_c:retry packet 0x%p "
7154 			    "status 0x%x reason %s",
7155 			    (void *)pkt, SCBP_C(pkt),
7156 			    scsi_rname(pkt->pkt_reason)));
7157 			if ((pkt->pkt_reason == CMD_CMPLT) &&
7158 			    (SCBP_C(pkt) == STATUS_CHECK) &&
7159 			    (pkt->pkt_state & STATE_ARQ_DONE)) {
7160 				sns = &(((struct scsi_arq_status *)(uintptr_t)
7161 				    (pkt->pkt_scbp))->sts_sensedata);
7162 				VHCI_DEBUG(1, (CE_WARN, NULL,
7163 				    "!v_s_do_s_c:retry "
7164 				    "packet 0x%p  sense data %s", (void *)pkt,
7165 				    scsi_sname(sns->es_key)));
7166 			}
7167 			goto retry;
7168 		}
7169 		VHCI_DEBUG(1, (CE_WARN, NULL,
7170 		    "!v_s_do_s_c: failed transport 0x%p 0x%x",
7171 		    (void *)pkt, SCBP_C(pkt)));
7172 		return (0);
7173 	}
7174 
7175 	switch (pkt->pkt_reason) {
7176 		case CMD_TIMEOUT:
7177 			VHCI_DEBUG(1, (CE_WARN, NULL, "!pkt timed "
7178 			    "out (pkt 0x%p)", (void *)pkt));
7179 			return (0);
7180 		case CMD_CMPLT:
7181 			switch (SCBP_C(pkt)) {
7182 				case STATUS_GOOD:
7183 					break;
7184 				case STATUS_CHECK:
7185 					if (pkt->pkt_state & STATE_ARQ_DONE) {
7186 						sns = &(((
7187 						    struct scsi_arq_status *)
7188 						    (uintptr_t)
7189 						    (pkt->pkt_scbp))->
7190 						    sts_sensedata);
7191 						if ((sns->es_key ==
7192 						    KEY_UNIT_ATTENTION) ||
7193 						    (sns->es_key ==
7194 						    KEY_NOT_READY)) {
7195 							/*
7196 							 * clear unit attn.
7197 							 */
7198 
7199 							VHCI_DEBUG(1,
7200 							    (CE_WARN, NULL,
7201 							    "!v_s_do_s_c: "
7202 							    "retry "
7203 							    "packet 0x%p sense "
7204 							    "data %s",
7205 							    (void *)pkt,
7206 							    scsi_sname
7207 							    (sns->es_key)));
7208 							goto retry;
7209 						}
7210 						VHCI_DEBUG(4, (CE_WARN, NULL,
7211 						    "!ARQ while "
7212 						    "transporting "
7213 						    "(pkt 0x%p)",
7214 						    (void *)pkt));
7215 						return (0);
7216 					}
7217 					return (0);
7218 				default:
7219 					VHCI_DEBUG(1, (CE_WARN, NULL,
7220 					    "!Bad status returned "
7221 					    "(pkt 0x%p, status %x)",
7222 					    (void *)pkt, SCBP_C(pkt)));
7223 					return (0);
7224 			}
7225 			break;
7226 		case CMD_INCOMPLETE:
7227 		case CMD_RESET:
7228 		case CMD_ABORTED:
7229 		case CMD_TRAN_ERR:
7230 			if (retry_cnt++ < 1) {
7231 				VHCI_DEBUG(1, (CE_WARN, NULL,
7232 				    "!v_s_do_s_c: retry packet 0x%p %s",
7233 				    (void *)pkt, scsi_rname(pkt->pkt_reason)));
7234 				goto retry;
7235 			}
7236 			/* FALLTHROUGH */
7237 		default:
7238 			VHCI_DEBUG(1, (CE_WARN, NULL, "!pkt did not "
7239 			    "complete successfully (pkt 0x%p,"
7240 			    "reason %x)", (void *)pkt, pkt->pkt_reason));
7241 			return (0);
7242 	}
7243 	return (1);
7244 }
7245 
7246 static int
7247 vhci_quiesce_lun(struct scsi_vhci_lun *vlun)
7248 {
7249 	mdi_pathinfo_t		*pip, *spip;
7250 	dev_info_t		*cdip;
7251 	struct scsi_vhci_priv	*svp;
7252 	mdi_pathinfo_state_t	pstate;
7253 	uint32_t		p_ext_state;
7254 	int			circular;
7255 
7256 	cdip = vlun->svl_dip;
7257 	pip = spip = NULL;
7258 	ndi_devi_enter(cdip, &circular);
7259 	pip = mdi_get_next_phci_path(cdip, NULL);
7260 	while (pip != NULL) {
7261 		(void) mdi_pi_get_state2(pip, &pstate, &p_ext_state);
7262 		if (pstate != MDI_PATHINFO_STATE_ONLINE) {
7263 			spip = pip;
7264 			pip = mdi_get_next_phci_path(cdip, spip);
7265 			continue;
7266 		}
7267 		mdi_hold_path(pip);
7268 		ndi_devi_exit(cdip, circular);
7269 		svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
7270 		mutex_enter(&svp->svp_mutex);
7271 		while (svp->svp_cmds != 0) {
7272 			if (cv_timedwait(&svp->svp_cv, &svp->svp_mutex,
7273 			    ddi_get_lbolt() + drv_usectohz
7274 			    (vhci_path_quiesce_timeout * 1000000)) == -1) {
7275 				mutex_exit(&svp->svp_mutex);
7276 				mdi_rele_path(pip);
7277 				VHCI_DEBUG(1, (CE_WARN, NULL,
7278 				    "Quiesce of lun is not successful "
7279 				    "vlun: 0x%p.", (void *)vlun));
7280 				return (0);
7281 			}
7282 		}
7283 		mutex_exit(&svp->svp_mutex);
7284 		ndi_devi_enter(cdip, &circular);
7285 		spip = pip;
7286 		pip = mdi_get_next_phci_path(cdip, spip);
7287 		mdi_rele_path(spip);
7288 	}
7289 	ndi_devi_exit(cdip, circular);
7290 	return (1);
7291 }
7292 
7293 static int
7294 vhci_pgr_validate_and_register(scsi_vhci_priv_t *svp)
7295 {
7296 	scsi_vhci_lun_t		*vlun;
7297 	vhci_prout_t		*prout;
7298 	int			rval, success;
7299 	mdi_pathinfo_t		*pip, *npip;
7300 	scsi_vhci_priv_t	*osvp;
7301 	dev_info_t		*cdip;
7302 	uchar_t			cdb_1;
7303 	uchar_t			temp_res_key[MHIOC_RESV_KEY_SIZE];
7304 
7305 
7306 	/*
7307 	 * see if there are any other paths available; if none,
7308 	 * then there is nothing to do.
7309 	 */
7310 	cdip = svp->svp_svl->svl_dip;
7311 	rval = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH |
7312 	    MDI_SELECT_STANDBY_PATH, NULL, &pip);
7313 	if ((rval != MDI_SUCCESS) || (pip == NULL)) {
7314 		VHCI_DEBUG(4, (CE_NOTE, NULL,
7315 		    "%s%d: vhci_pgr_validate_and_register: first path\n",
7316 		    ddi_driver_name(cdip), ddi_get_instance(cdip)));
7317 		return (1);
7318 	}
7319 
7320 	vlun = svp->svp_svl;
7321 	prout = &vlun->svl_prout;
7322 	ASSERT(vlun->svl_pgr_active != 0);
7323 
7324 	/*
7325 	 * When the path was busy/offlined, some other host might have
7326 	 * cleared this key. Validate key on some other path first.
7327 	 * If it fails, return failure.
7328 	 */
7329 
7330 	npip = pip;
7331 	pip = NULL;
7332 	success = 0;
7333 
7334 	/* Save the res key */
7335 	bcopy((const void *)prout->res_key,
7336 	    (void *)temp_res_key, MHIOC_RESV_KEY_SIZE);
7337 
7338 	/*
7339 	 * Sometimes CDB from application can be a Register_And_Ignore.
7340 	 * Instead of validation, this cdb would result in force registration.
7341 	 * Convert it to normal cdb for validation.
7342 	 * After that be sure to restore the cdb.
7343 	 */
7344 	cdb_1 = vlun->svl_cdb[1];
7345 	vlun->svl_cdb[1] &= 0xe0;
7346 
7347 	do {
7348 		osvp = (scsi_vhci_priv_t *)
7349 		    mdi_pi_get_vhci_private(npip);
7350 		if (osvp == NULL) {
7351 			VHCI_DEBUG(4, (CE_NOTE, NULL,
7352 			    "vhci_pgr_validate_and_register: no "
7353 			    "client priv! 0x%p offlined?\n",
7354 			    (void *)npip));
7355 			goto next_path_1;
7356 		}
7357 
7358 		if (osvp == svp) {
7359 			VHCI_DEBUG(4, (CE_NOTE, NULL,
7360 			    "vhci_pgr_validate_and_register: same svp 0x%p"
7361 			    " npip 0x%p vlun 0x%p\n",
7362 			    (void *)svp, (void *)npip, (void *)vlun));
7363 			goto next_path_1;
7364 		}
7365 
7366 		VHCI_DEBUG(4, (CE_NOTE, NULL,
7367 		    "vhci_pgr_validate_and_register: First validate on"
7368 		    " osvp 0x%p being done. vlun 0x%p thread 0x%p Before bcopy"
7369 		    " cdb1 %x\n", (void *)osvp, (void *)vlun,
7370 		    (void *)curthread, vlun->svl_cdb[1]));
7371 		vhci_print_prout_keys(vlun, "v_pgr_val_reg: before bcopy:");
7372 
7373 		bcopy((const void *)prout->service_key,
7374 		    (void *)prout->res_key, MHIOC_RESV_KEY_SIZE);
7375 
7376 		VHCI_DEBUG(4, (CE_WARN, NULL, "vlun 0x%p After bcopy",
7377 		    (void *)vlun));
7378 		vhci_print_prout_keys(vlun, "v_pgr_val_reg: after bcopy: ");
7379 
7380 		rval = vhci_do_prout(osvp);
7381 		if (rval == 1) {
7382 			VHCI_DEBUG(4, (CE_NOTE, NULL,
7383 			    "%s%d: vhci_pgr_validate_and_register: key"
7384 			    " validated thread 0x%p\n", ddi_driver_name(cdip),
7385 			    ddi_get_instance(cdip), (void *)curthread));
7386 			pip = npip;
7387 			success = 1;
7388 			break;
7389 		} else {
7390 			VHCI_DEBUG(4, (CE_NOTE, NULL,
7391 			    "vhci_pgr_validate_and_register: First validation"
7392 			    " on osvp 0x%p failed %x\n", (void *)osvp, rval));
7393 			vhci_print_prout_keys(vlun, "v_pgr_val_reg: failed:");
7394 		}
7395 
7396 		/*
7397 		 * Try other paths
7398 		 */
7399 next_path_1:
7400 		pip = npip;
7401 		rval = mdi_select_path(cdip, NULL,
7402 		    MDI_SELECT_ONLINE_PATH|MDI_SELECT_STANDBY_PATH,
7403 		    pip, &npip);
7404 		mdi_rele_path(pip);
7405 	} while ((rval == MDI_SUCCESS) && (npip != NULL));
7406 
7407 
7408 	/* Be sure to restore original cdb */
7409 	vlun->svl_cdb[1] = cdb_1;
7410 
7411 	/* Restore the res_key */
7412 	bcopy((const void *)temp_res_key,
7413 	    (void *)prout->res_key, MHIOC_RESV_KEY_SIZE);
7414 
7415 	/*
7416 	 * If key could not be registered on any path for the first time,
7417 	 * return success as online should still continue.
7418 	 */
7419 	if (success == 0) {
7420 		return (1);
7421 	}
7422 
7423 	ASSERT(pip != NULL);
7424 
7425 	/*
7426 	 * Force register on new path
7427 	 */
7428 	cdb_1 = vlun->svl_cdb[1];		/* store the cdb */
7429 
7430 	vlun->svl_cdb[1] &= 0xe0;
7431 	vlun->svl_cdb[1] |= VHCI_PROUT_R_AND_IGNORE;
7432 
7433 	vhci_print_prout_keys(vlun, "v_pgr_val_reg: keys before bcopy: ");
7434 
7435 	bcopy((const void *)prout->active_service_key,
7436 	    (void *)prout->service_key, MHIOC_RESV_KEY_SIZE);
7437 	bcopy((const void *)prout->active_res_key,
7438 	    (void *)prout->res_key, MHIOC_RESV_KEY_SIZE);
7439 
7440 	vhci_print_prout_keys(vlun, "v_pgr_val_reg:keys after bcopy: ");
7441 
7442 	rval = vhci_do_prout(svp);
7443 	vlun->svl_cdb[1] = cdb_1;		/* restore the cdb */
7444 	if (rval != 1) {
7445 		VHCI_DEBUG(4, (CE_NOTE, NULL,
7446 		    "vhci_pgr_validate_and_register: register on new"
7447 		    " path 0x%p svp 0x%p failed %x\n",
7448 		    (void *)pip, (void *)svp, rval));
7449 		vhci_print_prout_keys(vlun, "v_pgr_val_reg: reg failed: ");
7450 		mdi_rele_path(pip);
7451 		return (0);
7452 	}
7453 
7454 	if (bcmp(prout->service_key, zero_key, MHIOC_RESV_KEY_SIZE) == 0) {
7455 		VHCI_DEBUG(4, (CE_NOTE, NULL,
7456 		    "vhci_pgr_validate_and_register: zero service key\n"));
7457 		mdi_rele_path(pip);
7458 		return (rval);
7459 	}
7460 
7461 	/*
7462 	 * While the key was force registered, some other host might have
7463 	 * cleared the key. Re-validate key on another pre-existing path
7464 	 * before declaring success.
7465 	 */
7466 	npip = pip;
7467 	pip = NULL;
7468 
7469 	/*
7470 	 * Sometimes CDB from application can be Register and Ignore.
7471 	 * Instead of validation, it would result in force registration.
7472 	 * Convert it to normal cdb for validation.
7473 	 * After that be sure to restore the cdb.
7474 	 */
7475 	cdb_1 = vlun->svl_cdb[1];
7476 	vlun->svl_cdb[1] &= 0xe0;
7477 	success = 0;
7478 
7479 	do {
7480 		osvp = (scsi_vhci_priv_t *)
7481 		    mdi_pi_get_vhci_private(npip);
7482 		if (osvp == NULL) {
7483 			VHCI_DEBUG(4, (CE_NOTE, NULL,
7484 			    "vhci_pgr_validate_and_register: no "
7485 			    "client priv! 0x%p offlined?\n",
7486 			    (void *)npip));
7487 			goto next_path_2;
7488 		}
7489 
7490 		if (osvp == svp) {
7491 			VHCI_DEBUG(4, (CE_NOTE, NULL,
7492 			    "vhci_pgr_validate_and_register: same osvp 0x%p"
7493 			    " npip 0x%p vlun 0x%p\n",
7494 			    (void *)svp, (void *)npip, (void *)vlun));
7495 			goto next_path_2;
7496 		}
7497 
7498 		VHCI_DEBUG(4, (CE_NOTE, NULL,
7499 		    "vhci_pgr_validate_and_register: Re-validation on"
7500 		    " osvp 0x%p being done. vlun 0x%p Before bcopy cdb1 %x\n",
7501 		    (void *)osvp, (void *)vlun, vlun->svl_cdb[1]));
7502 		vhci_print_prout_keys(vlun, "v_pgr_val_reg: before bcopy: ");
7503 
7504 		bcopy((const void *)prout->service_key,
7505 		    (void *)prout->res_key, MHIOC_RESV_KEY_SIZE);
7506 
7507 		vhci_print_prout_keys(vlun, "v_pgr_val_reg: after bcopy: ");
7508 
7509 		rval = vhci_do_prout(osvp);
7510 		if (rval == 1) {
7511 			VHCI_DEBUG(4, (CE_NOTE, NULL,
7512 			    "%s%d: vhci_pgr_validate_and_register: key"
7513 			    " validated thread 0x%p\n", ddi_driver_name(cdip),
7514 			    ddi_get_instance(cdip), (void *)curthread));
7515 			pip = npip;
7516 			success = 1;
7517 			break;
7518 		} else {
7519 			VHCI_DEBUG(4, (CE_NOTE, NULL,
7520 			    "vhci_pgr_validate_and_register: Re-validation on"
7521 			    " osvp 0x%p failed %x\n", (void *)osvp, rval));
7522 			vhci_print_prout_keys(vlun,
7523 			    "v_pgr_val_reg: reval failed: ");
7524 		}
7525 
7526 		/*
7527 		 * Try other paths
7528 		 */
7529 next_path_2:
7530 		pip = npip;
7531 		rval = mdi_select_path(cdip, NULL,
7532 		    MDI_SELECT_ONLINE_PATH|MDI_SELECT_STANDBY_PATH,
7533 		    pip, &npip);
7534 		mdi_rele_path(pip);
7535 	} while ((rval == MDI_SUCCESS) && (npip != NULL));
7536 
7537 	/* Be sure to restore original cdb */
7538 	vlun->svl_cdb[1] = cdb_1;
7539 
7540 	if (success == 1) {
7541 		/* Successfully validated registration */
7542 		mdi_rele_path(pip);
7543 		return (1);
7544 	}
7545 
7546 	VHCI_DEBUG(4, (CE_WARN, NULL, "key validation failed"));
7547 
7548 	/*
7549 	 * key invalid, back out by registering key value of 0
7550 	 */
7551 	VHCI_DEBUG(4, (CE_NOTE, NULL,
7552 	    "vhci_pgr_validate_and_register: backout on"
7553 	    " svp 0x%p being done\n", (void *)svp));
7554 	vhci_print_prout_keys(vlun, "v_pgr_val_reg: before bcopy: ");
7555 
7556 	bcopy((const void *)prout->service_key, (void *)prout->res_key,
7557 	    MHIOC_RESV_KEY_SIZE);
7558 	bzero((void *)prout->service_key, MHIOC_RESV_KEY_SIZE);
7559 
7560 	vhci_print_prout_keys(vlun, "v_pgr_val_reg: before bcopy: ");
7561 
7562 	/*
7563 	 * Get a new path
7564 	 */
7565 	rval = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH |
7566 	    MDI_SELECT_STANDBY_PATH, NULL, &pip);
7567 	if ((rval != MDI_SUCCESS) || (pip == NULL)) {
7568 		VHCI_DEBUG(4, (CE_NOTE, NULL,
7569 		    "%s%d: vhci_pgr_validate_and_register: no valid pip\n",
7570 		    ddi_driver_name(cdip), ddi_get_instance(cdip)));
7571 		return (0);
7572 	}
7573 
7574 	if ((rval = vhci_do_prout(svp)) != 1) {
7575 		VHCI_DEBUG(4, (CE_NOTE, NULL,
7576 		    "vhci_pgr_validate_and_register: backout on"
7577 		    " svp 0x%p failed\n", (void *)svp));
7578 		vhci_print_prout_keys(vlun, "backout failed");
7579 
7580 		VHCI_DEBUG(4, (CE_WARN, NULL,
7581 		    "%s%d: vhci_pgr_validate_and_register: key"
7582 		    " validation and backout failed", ddi_driver_name(cdip),
7583 		    ddi_get_instance(cdip)));
7584 		if (rval == VHCI_PGR_ILLEGALOP) {
7585 			VHCI_DEBUG(4, (CE_WARN, NULL,
7586 			    "%s%d: vhci_pgr_validate_and_register: key"
7587 			    " already cleared", ddi_driver_name(cdip),
7588 			    ddi_get_instance(cdip)));
7589 			rval = 1;
7590 		} else
7591 			rval = 0;
7592 	} else {
7593 		VHCI_DEBUG(4, (CE_NOTE, NULL,
7594 		    "%s%d: vhci_pgr_validate_and_register: key"
7595 		    " validation failed, key backed out\n",
7596 		    ddi_driver_name(cdip), ddi_get_instance(cdip)));
7597 		vhci_print_prout_keys(vlun, "v_pgr_val_reg: key backed out: ");
7598 	}
7599 	mdi_rele_path(pip);
7600 
7601 	return (rval);
7602 }
7603 
7604 /*
7605  * taskq routine to dispatch a scsi cmd to vhci_scsi_start.  This ensures
7606  * that vhci_scsi_start is not called in interrupt context.
7607  * As the upper layer gets TRAN_ACCEPT when the command is dispatched, we
7608  * need to complete the command if something goes wrong.
7609  */
7610 static void
7611 vhci_dispatch_scsi_start(void *arg)
7612 {
7613 	struct vhci_pkt *vpkt = (struct vhci_pkt *)arg;
7614 	struct scsi_pkt *tpkt = vpkt->vpkt_tgt_pkt;
7615 	int			rval = TRAN_BUSY;
7616 
7617 	VHCI_DEBUG(6, (CE_NOTE, NULL, "!vhci_dispatch_scsi_start: sending"
7618 	    " scsi-2 reserve for 0x%p\n",
7619 	    (void *)ADDR2DIP(&(vpkt->vpkt_tgt_pkt->pkt_address))));
7620 
7621 	/*
7622 	 * To prevent the taskq from being called recursively we set the
7623 	 * the VHCI_PKT_THRU_TASKQ bit in the vhci_pkt_states.
7624 	 */
7625 	vpkt->vpkt_state |= VHCI_PKT_THRU_TASKQ;
7626 
7627 	/*
7628 	 * Wait for the transport to get ready to send packets
7629 	 * and if it times out, it will return something other than
7630 	 * TRAN_BUSY. The vhci_reserve_delay may want to
7631 	 * get tuned for other transports and is therefore a global.
7632 	 * Using delay since this routine is called by taskq dispatch
7633 	 * and not called during interrupt context.
7634 	 */
7635 	while ((rval = vhci_scsi_start(&(vpkt->vpkt_tgt_pkt->pkt_address),
7636 	    vpkt->vpkt_tgt_pkt)) == TRAN_BUSY) {
7637 		delay(drv_usectohz(vhci_reserve_delay));
7638 	}
7639 
7640 	switch (rval) {
7641 	case TRAN_ACCEPT:
7642 		return;
7643 
7644 	default:
7645 		/*
7646 		 * This pkt shall be retried, and to ensure another taskq
7647 		 * is dispatched for it, clear the VHCI_PKT_THRU_TASKQ
7648 		 * flag.
7649 		 */
7650 		vpkt->vpkt_state &= ~VHCI_PKT_THRU_TASKQ;
7651 
7652 		/* Ensure that the pkt is retried without a reset */
7653 		tpkt->pkt_reason = CMD_ABORTED;
7654 		tpkt->pkt_statistics |= STAT_ABORTED;
7655 		VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_dispatch_scsi_start: "
7656 		    "TRAN_rval %d returned for dip 0x%p", rval,
7657 		    (void *)ADDR2DIP(&(vpkt->vpkt_tgt_pkt->pkt_address))));
7658 		break;
7659 	}
7660 
7661 	/*
7662 	 * vpkt_org_vpkt should always be NULL here if the retry command
7663 	 * has been successfully dispatched.  If vpkt_org_vpkt != NULL at
7664 	 * this point, it is an error so restore the original vpkt and
7665 	 * return an error to the target driver so it can retry the
7666 	 * command as appropriate.
7667 	 */
7668 	if (vpkt->vpkt_org_vpkt != NULL) {
7669 		struct vhci_pkt		*new_vpkt = vpkt;
7670 		scsi_vhci_priv_t	*svp = (scsi_vhci_priv_t *)
7671 		    mdi_pi_get_vhci_private(vpkt->vpkt_path);
7672 
7673 		vpkt = vpkt->vpkt_org_vpkt;
7674 
7675 		vpkt->vpkt_tgt_pkt->pkt_reason = tpkt->pkt_reason;
7676 		vpkt->vpkt_tgt_pkt->pkt_statistics = tpkt->pkt_statistics;
7677 
7678 		vhci_scsi_destroy_pkt(&svp->svp_psd->sd_address,
7679 		    new_vpkt->vpkt_tgt_pkt);
7680 
7681 		tpkt = vpkt->vpkt_tgt_pkt;
7682 	}
7683 
7684 	if (tpkt->pkt_comp) {
7685 		(*tpkt->pkt_comp)(tpkt);
7686 	}
7687 }
7688 
7689 static void
7690 vhci_initiate_auto_failback(void *arg)
7691 {
7692 	struct scsi_vhci_lun	*vlun = (struct scsi_vhci_lun *)arg;
7693 	dev_info_t		*vdip, *cdip;
7694 	int			held;
7695 
7696 	cdip = vlun->svl_dip;
7697 	vdip = ddi_get_parent(cdip);
7698 
7699 	VHCI_HOLD_LUN(vlun, VH_SLEEP, held);
7700 
7701 	/*
7702 	 * Perform a final check to see if the active path class is indeed
7703 	 * not the prefered path class.  As in the time the auto failback
7704 	 * was dispatched, an external failover could have been detected.
7705 	 * [Some other host could have detected this condition and triggered
7706 	 *  the auto failback before].
7707 	 * In such a case if we go ahead with failover we will be negating the
7708 	 * whole purpose of auto failback.
7709 	 */
7710 	mutex_enter(&vlun->svl_mutex);
7711 	if (vlun->svl_active_pclass != NULL) {
7712 		char				*best_pclass;
7713 		struct scsi_failover_ops	*fo;
7714 
7715 		fo = vlun->svl_fops;
7716 
7717 		(*fo->sfo_pathclass_next)(NULL, &best_pclass,
7718 		    vlun->svl_fops_ctpriv);
7719 		if (strcmp(vlun->svl_active_pclass, best_pclass) == 0) {
7720 			mutex_exit(&vlun->svl_mutex);
7721 			VHCI_RELEASE_LUN(vlun);
7722 			VHCI_DEBUG(1, (CE_NOTE, NULL, "Not initiating "
7723 			    "auto failback for %s as %s pathclass already "
7724 			    "active.\n", vlun->svl_lun_wwn, best_pclass));
7725 			return;
7726 		}
7727 	}
7728 	mutex_exit(&vlun->svl_mutex);
7729 	if (mdi_failover(vdip, vlun->svl_dip, MDI_FAILOVER_SYNC)
7730 	    == MDI_SUCCESS) {
7731 		vhci_log(CE_NOTE, vdip, "!Auto failback operation "
7732 		    "succeeded for device %s (GUID %s)",
7733 		    ddi_node_name(cdip), vlun->svl_lun_wwn);
7734 	} else {
7735 		vhci_log(CE_NOTE, vdip, "!Auto failback operation "
7736 		    "failed for device %s (GUID %s)",
7737 		    ddi_node_name(cdip), vlun->svl_lun_wwn);
7738 	}
7739 	VHCI_RELEASE_LUN(vlun);
7740 }
7741 
7742 #ifdef DEBUG
7743 static void
7744 vhci_print_prin_keys(vhci_prin_readkeys_t *prin, int numkeys)
7745 {
7746 	uchar_t index = 0;
7747 	char buf[100];
7748 
7749 	VHCI_DEBUG(5, (CE_NOTE, NULL, "num keys %d\n", numkeys));
7750 
7751 	while (index < numkeys) {
7752 		bcopy(&prin->keylist[index], buf, MHIOC_RESV_KEY_SIZE);
7753 		VHCI_DEBUG(5, (CE_NOTE, NULL,
7754 		    "%02x%02x%02x%02x%02x%02x%02x%02x\t",
7755 		    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
7756 		    buf[7]));
7757 		index++;
7758 	}
7759 }
7760 #endif
7761 
7762 static void
7763 vhci_print_prout_keys(scsi_vhci_lun_t *vlun, char *msg)
7764 {
7765 	int			i;
7766 	vhci_prout_t		*prout;
7767 	char			buf1[4*MHIOC_RESV_KEY_SIZE + 1];
7768 	char			buf2[4*MHIOC_RESV_KEY_SIZE + 1];
7769 	char			buf3[4*MHIOC_RESV_KEY_SIZE + 1];
7770 	char			buf4[4*MHIOC_RESV_KEY_SIZE + 1];
7771 
7772 	prout = &vlun->svl_prout;
7773 
7774 	for (i = 0; i < MHIOC_RESV_KEY_SIZE; i++)
7775 		(void) sprintf(&buf1[4*i], "[%02x]", prout->res_key[i]);
7776 	for (i = 0; i < MHIOC_RESV_KEY_SIZE; i++)
7777 		(void) sprintf(&buf2[(4*i)], "[%02x]", prout->service_key[i]);
7778 	for (i = 0; i < MHIOC_RESV_KEY_SIZE; i++)
7779 		(void) sprintf(&buf3[4*i], "[%02x]", prout->active_res_key[i]);
7780 	for (i = 0; i < MHIOC_RESV_KEY_SIZE; i++)
7781 		(void) sprintf(&buf4[4*i], "[%02x]",
7782 		    prout->active_service_key[i]);
7783 
7784 	/* Printing all in one go. Otherwise it will jumble up */
7785 	VHCI_DEBUG(5, (CE_CONT, NULL, "%s vlun 0x%p, thread 0x%p\n"
7786 	    "res_key:          : %s\n"
7787 	    "service_key       : %s\n"
7788 	    "active_res_key    : %s\n"
7789 	    "active_service_key: %s\n",
7790 	    msg, (void *)vlun, (void *)curthread, buf1, buf2, buf3, buf4));
7791 }
7792 
7793 /*
7794  * Called from vhci_scsi_start to update the pHCI pkt with target packet.
7795  */
7796 static void
7797 vhci_update_pHCI_pkt(struct vhci_pkt *vpkt, struct scsi_pkt *pkt)
7798 {
7799 
7800 	ASSERT(vpkt->vpkt_hba_pkt);
7801 
7802 	vpkt->vpkt_hba_pkt->pkt_flags = pkt->pkt_flags;
7803 	vpkt->vpkt_hba_pkt->pkt_flags |= FLAG_NOQUEUE;
7804 
7805 	if ((vpkt->vpkt_hba_pkt->pkt_flags & FLAG_NOINTR) ||
7806 	    MDI_PI_IS_SUSPENDED(vpkt->vpkt_path)) {
7807 		/*
7808 		 * Polled Command is requested or HBA is in
7809 		 * suspended state
7810 		 */
7811 		vpkt->vpkt_hba_pkt->pkt_flags |= FLAG_NOINTR;
7812 		vpkt->vpkt_hba_pkt->pkt_comp = NULL;
7813 	} else {
7814 		vpkt->vpkt_hba_pkt->pkt_comp = vhci_intr;
7815 	}
7816 	vpkt->vpkt_hba_pkt->pkt_time = pkt->pkt_time;
7817 	bcopy(pkt->pkt_cdbp, vpkt->vpkt_hba_pkt->pkt_cdbp,
7818 	    vpkt->vpkt_tgt_init_cdblen);
7819 	vpkt->vpkt_hba_pkt->pkt_resid = pkt->pkt_resid;
7820 
7821 	/* Re-initialize the following pHCI packet state information */
7822 	vpkt->vpkt_hba_pkt->pkt_state = 0;
7823 	vpkt->vpkt_hba_pkt->pkt_statistics = 0;
7824 	vpkt->vpkt_hba_pkt->pkt_reason = 0;
7825 }
7826 
7827 static int
7828 vhci_scsi_bus_power(dev_info_t *parent, void *impl_arg, pm_bus_power_op_t op,
7829     void *arg, void *result)
7830 {
7831 	int ret = DDI_SUCCESS;
7832 
7833 	/*
7834 	 * Generic processing in MPxIO framework
7835 	 */
7836 	ret = mdi_bus_power(parent, impl_arg, op, arg, result);
7837 
7838 	switch (ret) {
7839 	case MDI_SUCCESS:
7840 		ret = DDI_SUCCESS;
7841 		break;
7842 	case MDI_FAILURE:
7843 		ret = DDI_FAILURE;
7844 		break;
7845 	default:
7846 		break;
7847 	}
7848 
7849 	return (ret);
7850 }
7851 
7852 static int
7853 vhci_pHCI_cap(struct scsi_address *ap, char *cap, int val, int whom,
7854     mdi_pathinfo_t *pip)
7855 {
7856 	dev_info_t		*cdip;
7857 	mdi_pathinfo_t		*npip = NULL;
7858 	scsi_vhci_priv_t	*svp = NULL;
7859 	struct scsi_address	*pap = NULL;
7860 	scsi_hba_tran_t		*hba = NULL;
7861 	int			sps;
7862 	int			mps_flag;
7863 	int			rval = 0;
7864 
7865 	mps_flag = (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH);
7866 	if (pip) {
7867 		/*
7868 		 * If the call is from vhci_pathinfo_state_change,
7869 		 * then this path was busy and is becoming ready to accept IO.
7870 		 */
7871 		ASSERT(ap != NULL);
7872 		hba = ap->a_hba_tran;
7873 		ASSERT(hba != NULL);
7874 		rval = scsi_ifsetcap(ap, cap, val, whom);
7875 
7876 		VHCI_DEBUG(2, (CE_NOTE, NULL,
7877 		    "!vhci_pHCI_cap: only on path %p, ap %p, rval %x\n",
7878 		    (void *)pip, (void *)ap, rval));
7879 
7880 		return (rval);
7881 	}
7882 
7883 	/*
7884 	 * Set capability on all the pHCIs.
7885 	 * If any path is busy, then the capability would be set by
7886 	 * vhci_pathinfo_state_change.
7887 	 */
7888 
7889 	cdip = ADDR2DIP(ap);
7890 	ASSERT(cdip != NULL);
7891 	sps = mdi_select_path(cdip, NULL, mps_flag, NULL, &pip);
7892 	if ((sps != MDI_SUCCESS) || (pip == NULL)) {
7893 		VHCI_DEBUG(2, (CE_WARN, NULL,
7894 		    "!vhci_pHCI_cap: Unable to get a path, dip 0x%p",
7895 		    (void *)cdip));
7896 		return (0);
7897 	}
7898 
7899 again:
7900 	svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
7901 	if (svp == NULL) {
7902 		VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_pHCI_cap: "
7903 		    "priv is NULL, pip 0x%p", (void *)pip));
7904 		mdi_rele_path(pip);
7905 		return (rval);
7906 	}
7907 
7908 	if (svp->svp_psd == NULL) {
7909 		VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_pHCI_cap: "
7910 		    "psd is NULL, pip 0x%p, svp 0x%p",
7911 		    (void *)pip, (void *)svp));
7912 		mdi_rele_path(pip);
7913 		return (rval);
7914 	}
7915 
7916 	pap = &svp->svp_psd->sd_address;
7917 	ASSERT(pap != NULL);
7918 	hba = pap->a_hba_tran;
7919 	ASSERT(hba != NULL);
7920 
7921 	if (hba->tran_setcap != NULL) {
7922 		rval = scsi_ifsetcap(pap, cap, val, whom);
7923 
7924 		VHCI_DEBUG(2, (CE_NOTE, NULL,
7925 		    "!vhci_pHCI_cap: path %p, ap %p, rval %x\n",
7926 		    (void *)pip, (void *)ap, rval));
7927 
7928 		/*
7929 		 * Select next path and issue the setcap, repeat
7930 		 * until all paths are exhausted
7931 		 */
7932 		sps = mdi_select_path(cdip, NULL, mps_flag, pip, &npip);
7933 		if ((sps != MDI_SUCCESS) || (npip == NULL)) {
7934 			mdi_rele_path(pip);
7935 			return (1);
7936 		}
7937 		mdi_rele_path(pip);
7938 		pip = npip;
7939 		goto again;
7940 	}
7941 	mdi_rele_path(pip);
7942 	return (rval);
7943 }
7944 
7945 static int
7946 vhci_scsi_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7947     void *arg, dev_info_t **child)
7948 {
7949 	char *guid;
7950 
7951 	if (op == BUS_CONFIG_ONE || op == BUS_UNCONFIG_ONE)
7952 		guid = vhci_devnm_to_guid((char *)arg);
7953 	else
7954 		guid = NULL;
7955 
7956 	if (mdi_vhci_bus_config(pdip, flags, op, arg, child, guid)
7957 	    == MDI_SUCCESS)
7958 		return (NDI_SUCCESS);
7959 	else
7960 		return (NDI_FAILURE);
7961 }
7962 
7963 /*
7964  * Take the original vhci_pkt, create a duplicate of the pkt for resending
7965  * as though it originated in ssd.
7966  */
7967 static struct scsi_pkt *
7968 vhci_create_retry_pkt(struct vhci_pkt *vpkt)
7969 {
7970 	struct vhci_pkt *new_vpkt = NULL;
7971 	struct scsi_pkt	*pkt = NULL;
7972 
7973 	scsi_vhci_priv_t *svp = (scsi_vhci_priv_t *)
7974 	    mdi_pi_get_vhci_private(vpkt->vpkt_path);
7975 
7976 	/*
7977 	 * Ensure consistent data at completion time by setting PKT_CONSISTENT
7978 	 */
7979 	pkt = vhci_scsi_init_pkt(&svp->svp_psd->sd_address, pkt,
7980 	    vpkt->vpkt_tgt_init_bp, vpkt->vpkt_tgt_init_cdblen,
7981 	    vpkt->vpkt_tgt_init_scblen, vpkt->vpkt_tgt_init_privlen,
7982 	    PKT_CONSISTENT,
7983 	    NULL_FUNC, NULL);
7984 	if (pkt != NULL) {
7985 		new_vpkt = TGTPKT2VHCIPKT(pkt);
7986 
7987 		pkt->pkt_address = vpkt->vpkt_tgt_pkt->pkt_address;
7988 		pkt->pkt_flags = vpkt->vpkt_tgt_pkt->pkt_flags;
7989 		pkt->pkt_time = vpkt->vpkt_tgt_pkt->pkt_time;
7990 		pkt->pkt_comp = vpkt->vpkt_tgt_pkt->pkt_comp;
7991 
7992 		pkt->pkt_resid = 0;
7993 		pkt->pkt_statistics = 0;
7994 		pkt->pkt_reason = 0;
7995 
7996 		bcopy(vpkt->vpkt_tgt_pkt->pkt_cdbp,
7997 		    pkt->pkt_cdbp, vpkt->vpkt_tgt_init_cdblen);
7998 
7999 		/*
8000 		 * Save a pointer to the original vhci_pkt
8001 		 */
8002 		new_vpkt->vpkt_org_vpkt = vpkt;
8003 	}
8004 
8005 	return (pkt);
8006 }
8007 
8008 /*
8009  * Copy the successful completion information from the hba packet into
8010  * the original target pkt from the upper layer.  Returns the original
8011  * vpkt and destroys the new vpkt from the internal retry.
8012  */
8013 static struct vhci_pkt *
8014 vhci_sync_retry_pkt(struct vhci_pkt *vpkt)
8015 {
8016 	struct vhci_pkt		*ret_vpkt = NULL;
8017 	struct scsi_pkt		*tpkt = NULL;
8018 	struct scsi_pkt		*hba_pkt = NULL;
8019 	scsi_vhci_priv_t	*svp = (scsi_vhci_priv_t *)
8020 	    mdi_pi_get_vhci_private(vpkt->vpkt_path);
8021 
8022 	ASSERT(vpkt->vpkt_org_vpkt != NULL);
8023 	VHCI_DEBUG(0, (CE_NOTE, NULL, "vhci_sync_retry_pkt: Retry pkt "
8024 	    "completed successfully!\n"));
8025 
8026 	ret_vpkt = vpkt->vpkt_org_vpkt;
8027 	tpkt = ret_vpkt->vpkt_tgt_pkt;
8028 	hba_pkt = vpkt->vpkt_hba_pkt;
8029 
8030 	/*
8031 	 * Copy the good status into the target driver's packet
8032 	 */
8033 	*(tpkt->pkt_scbp) = *(hba_pkt->pkt_scbp);
8034 	tpkt->pkt_resid = hba_pkt->pkt_resid;
8035 	tpkt->pkt_state = hba_pkt->pkt_state;
8036 	tpkt->pkt_statistics = hba_pkt->pkt_statistics;
8037 	tpkt->pkt_reason = hba_pkt->pkt_reason;
8038 
8039 	/*
8040 	 * Destroy the internally created vpkt for the retry
8041 	 */
8042 	vhci_scsi_destroy_pkt(&svp->svp_psd->sd_address,
8043 	    vpkt->vpkt_tgt_pkt);
8044 
8045 	return (ret_vpkt);
8046 }
8047 
8048 /* restart the request sense request */
8049 static void
8050 vhci_uscsi_restart_sense(void *arg)
8051 {
8052 	struct buf 	*rqbp;
8053 	struct buf 	*bp;
8054 	struct scsi_pkt *rqpkt = (struct scsi_pkt *)arg;
8055 	mp_uscsi_cmd_t 	*mp_uscmdp;
8056 
8057 	VHCI_DEBUG(4, (CE_WARN, NULL,
8058 	    "vhci_uscsi_restart_sense: enter: rqpkt: %p", (void *)rqpkt));
8059 
8060 	if (scsi_transport(rqpkt) != TRAN_ACCEPT) {
8061 		/* if it fails - need to wakeup the original command */
8062 		mp_uscmdp = rqpkt->pkt_private;
8063 		bp = mp_uscmdp->cmdbp;
8064 		rqbp = mp_uscmdp->rqbp;
8065 		ASSERT(mp_uscmdp && bp && rqbp);
8066 		scsi_free_consistent_buf(rqbp);
8067 		scsi_destroy_pkt(rqpkt);
8068 		bp->b_resid = bp->b_bcount;
8069 		bioerror(bp, EIO);
8070 		biodone(bp);
8071 	}
8072 }
8073 
8074 /*
8075  * auto-rqsense is not enabled so we have to retrieve the request sense
8076  * manually.
8077  */
8078 static int
8079 vhci_uscsi_send_sense(struct scsi_pkt *pkt, mp_uscsi_cmd_t *mp_uscmdp)
8080 {
8081 	struct buf 		*rqbp, *cmdbp;
8082 	struct scsi_pkt 	*rqpkt;
8083 	int			rval = 0;
8084 
8085 	cmdbp = mp_uscmdp->cmdbp;
8086 	ASSERT(cmdbp != NULL);
8087 
8088 	VHCI_DEBUG(4, (CE_WARN, NULL,
8089 	    "vhci_uscsi_send_sense: enter: bp: %p pkt: %p scmd: %p",
8090 	    (void *)cmdbp, (void *)pkt, (void *)mp_uscmdp));
8091 	/* set up the packet information and cdb */
8092 	if ((rqbp = scsi_alloc_consistent_buf(mp_uscmdp->ap, NULL,
8093 	    SENSE_LENGTH, B_READ, NULL, NULL)) == NULL) {
8094 		return (-1);
8095 	}
8096 
8097 	if ((rqpkt = scsi_init_pkt(mp_uscmdp->ap, NULL, rqbp,
8098 	    CDB_GROUP0, 1, 0, PKT_CONSISTENT, NULL, NULL)) == NULL) {
8099 		scsi_free_consistent_buf(rqbp);
8100 		return (-1);
8101 	}
8102 
8103 	(void) scsi_setup_cdb((union scsi_cdb *)(intptr_t)rqpkt->pkt_cdbp,
8104 	    SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
8105 
8106 	mp_uscmdp->rqbp = rqbp;
8107 	rqbp->b_private = mp_uscmdp;
8108 	rqpkt->pkt_flags |= FLAG_SENSING;
8109 	rqpkt->pkt_time = 60;
8110 	rqpkt->pkt_comp = vhci_uscsi_iodone;
8111 	rqpkt->pkt_private = mp_uscmdp;
8112 
8113 	/* get her done */
8114 	switch (scsi_transport(rqpkt)) {
8115 	case TRAN_ACCEPT:
8116 		VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_uscsi_send_sense: "
8117 		    "transport accepted."));
8118 		break;
8119 	case TRAN_BUSY:
8120 		VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_uscsi_send_sense: "
8121 		    "transport busy, setting timeout."));
8122 		vhci_restart_timeid = timeout(vhci_uscsi_restart_sense, rqpkt,
8123 		    (drv_usectohz(5 * 1000000)));
8124 		break;
8125 	default:
8126 		VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_uscsi_send_sense: "
8127 		    "transport failed"));
8128 		scsi_free_consistent_buf(rqbp);
8129 		scsi_destroy_pkt(rqpkt);
8130 		rval = -1;
8131 	}
8132 
8133 	return (rval);
8134 }
8135 
8136 /*
8137  * done routine for the mpapi uscsi command - this is behaving as though
8138  * FLAG_DIAGNOSE is set meaning there are no retries except for a manual
8139  * request sense.
8140  */
8141 void
8142 vhci_uscsi_iodone(struct scsi_pkt *pkt)
8143 {
8144 	struct buf 			*bp;
8145 	mp_uscsi_cmd_t 			*mp_uscmdp;
8146 	struct uscsi_cmd 		*uscmdp;
8147 	struct scsi_arq_status 		*arqstat;
8148 	int 				err;
8149 
8150 	mp_uscmdp = (mp_uscsi_cmd_t *)pkt->pkt_private;
8151 	uscmdp = mp_uscmdp->uscmdp;
8152 	bp = mp_uscmdp->cmdbp;
8153 	ASSERT(bp != NULL);
8154 	VHCI_DEBUG(4, (CE_WARN, NULL,
8155 	    "vhci_uscsi_iodone: enter: bp: %p pkt: %p scmd: %p",
8156 	    (void *)bp, (void *)pkt, (void *)mp_uscmdp));
8157 	/* Save the status and the residual into the uscsi_cmd struct */
8158 	uscmdp->uscsi_status = ((*(pkt)->pkt_scbp) & STATUS_MASK);
8159 	uscmdp->uscsi_resid = bp->b_resid;
8160 
8161 	/* return on a very successful command */
8162 	if (pkt->pkt_reason == CMD_CMPLT &&
8163 	    SCBP_C(pkt) == 0 && ((pkt->pkt_flags & FLAG_SENSING) == 0) &&
8164 	    pkt->pkt_resid == 0) {
8165 		mdi_pi_kstat_iosupdate(mp_uscmdp->pip, bp);
8166 		scsi_destroy_pkt(pkt);
8167 		biodone(bp);
8168 		return;
8169 	}
8170 	VHCI_DEBUG(4, (CE_NOTE, NULL, "iodone: reason=0x%x "
8171 	    " pkt_resid=%ld pkt_state: 0x%x b_count: %ld b_resid: %ld",
8172 	    pkt->pkt_reason, pkt->pkt_resid,
8173 	    pkt->pkt_state, bp->b_bcount, bp->b_resid));
8174 
8175 	err = EIO;
8176 
8177 	arqstat = (struct scsi_arq_status *)(intptr_t)(pkt->pkt_scbp);
8178 	if (pkt->pkt_reason != CMD_CMPLT) {
8179 		/*
8180 		 * The command did not complete.
8181 		 */
8182 		VHCI_DEBUG(4, (CE_NOTE, NULL,
8183 		    "vhci_uscsi_iodone: command did not complete."
8184 		    " reason: %x flag: %x", pkt->pkt_reason, pkt->pkt_flags));
8185 		if (pkt->pkt_flags & FLAG_SENSING) {
8186 			MDI_PI_ERRSTAT(mp_uscmdp->pip, MDI_PI_TRANSERR);
8187 		} else if (pkt->pkt_reason == CMD_TIMEOUT) {
8188 			MDI_PI_ERRSTAT(mp_uscmdp->pip, MDI_PI_HARDERR);
8189 			err = ETIMEDOUT;
8190 		}
8191 	} else if (pkt->pkt_state & STATE_ARQ_DONE && mp_uscmdp->arq_enabled) {
8192 		/*
8193 		 * The auto-rqsense happened, and the packet has a filled-in
8194 		 * scsi_arq_status structure, pointed to by pkt_scbp.
8195 		 */
8196 		VHCI_DEBUG(4, (CE_NOTE, NULL,
8197 		    "vhci_uscsi_iodone: received auto-requested sense"));
8198 		if (uscmdp->uscsi_flags & USCSI_RQENABLE) {
8199 			/* get the amount of data to copy into rqbuf */
8200 			int rqlen = SENSE_LENGTH - arqstat->sts_rqpkt_resid;
8201 			rqlen = min(((int)uscmdp->uscsi_rqlen), rqlen);
8202 			uscmdp->uscsi_rqresid = uscmdp->uscsi_rqlen - rqlen;
8203 			uscmdp->uscsi_rqstatus =
8204 			    *((char *)&arqstat->sts_rqpkt_status);
8205 			if (uscmdp->uscsi_rqbuf && uscmdp->uscsi_rqlen &&
8206 			    rqlen != 0) {
8207 				bcopy(&(arqstat->sts_sensedata),
8208 				    uscmdp->uscsi_rqbuf, rqlen);
8209 			}
8210 			mdi_pi_kstat_iosupdate(mp_uscmdp->pip, bp);
8211 			VHCI_DEBUG(4, (CE_NOTE, NULL,
8212 			    "vhci_uscsi_iodone: ARQ "
8213 			    "uscsi_rqstatus=0x%x uscsi_rqresid=%d rqlen: %d "
8214 			    "xfer: %d rqpkt_resid: %d\n",
8215 			    uscmdp->uscsi_rqstatus, uscmdp->uscsi_rqresid,
8216 			    uscmdp->uscsi_rqlen, rqlen,
8217 			    arqstat->sts_rqpkt_resid));
8218 		}
8219 	} else if (pkt->pkt_flags & FLAG_SENSING) {
8220 		struct buf *rqbp;
8221 		struct scsi_status *rqstatus;
8222 
8223 		rqstatus = (struct scsi_status *)pkt->pkt_scbp;
8224 		/* a manual request sense was done - get the information */
8225 		if (uscmdp->uscsi_flags & USCSI_RQENABLE) {
8226 			int rqlen = SENSE_LENGTH - pkt->pkt_resid;
8227 
8228 			rqbp = mp_uscmdp->rqbp;
8229 			/* get the amount of data to copy into rqbuf */
8230 			rqlen = min(((int)uscmdp->uscsi_rqlen), rqlen);
8231 			uscmdp->uscsi_rqresid = uscmdp->uscsi_rqlen - rqlen;
8232 			uscmdp->uscsi_rqstatus = *((char *)rqstatus);
8233 			if (uscmdp->uscsi_rqlen && uscmdp->uscsi_rqbuf) {
8234 				bcopy(rqbp->b_un.b_addr, uscmdp->uscsi_rqbuf,
8235 				    rqlen);
8236 			}
8237 			MDI_PI_ERRSTAT(mp_uscmdp->pip, MDI_PI_TRANSERR);
8238 			scsi_free_consistent_buf(rqbp);
8239 		}
8240 		VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_uscsi_iodone: FLAG_SENSING"
8241 		    "uscsi_rqstatus=0x%x uscsi_rqresid=%d\n",
8242 		    uscmdp->uscsi_rqstatus, uscmdp->uscsi_rqresid));
8243 	} else {
8244 		struct scsi_status *status =
8245 		    (struct scsi_status *)pkt->pkt_scbp;
8246 		/*
8247 		 * Command completed and we're not getting sense. Check for
8248 		 * errors and decide what to do next.
8249 		 */
8250 		VHCI_DEBUG(4, (CE_NOTE, NULL,
8251 		    "vhci_uscsi_iodone: command appears complete: reason: %x",
8252 		    pkt->pkt_reason));
8253 		if (status->sts_chk) {
8254 			/* need to manually get the request sense */
8255 			if (vhci_uscsi_send_sense(pkt, mp_uscmdp) == 0) {
8256 				scsi_destroy_pkt(pkt);
8257 				return;
8258 			}
8259 		} else {
8260 			VHCI_DEBUG(4, (CE_NOTE, NULL,
8261 			    "vhci_chk_err: appears complete"));
8262 			err = 0;
8263 			mdi_pi_kstat_iosupdate(mp_uscmdp->pip, bp);
8264 			if (pkt->pkt_resid) {
8265 				bp->b_resid += pkt->pkt_resid;
8266 			}
8267 		}
8268 	}
8269 
8270 	if (err) {
8271 		if (bp->b_resid == 0)
8272 			bp->b_resid = bp->b_bcount;
8273 		bioerror(bp, err);
8274 		bp->b_flags |= B_ERROR;
8275 	}
8276 
8277 	scsi_destroy_pkt(pkt);
8278 	biodone(bp);
8279 
8280 	VHCI_DEBUG(4, (CE_WARN, NULL, "vhci_uscsi_iodone: exit"));
8281 }
8282 
8283 /*
8284  * start routine for the mpapi uscsi command
8285  */
8286 int
8287 vhci_uscsi_iostart(struct buf *bp)
8288 {
8289 	struct scsi_pkt 	*pkt;
8290 	struct uscsi_cmd	*uscmdp;
8291 	mp_uscsi_cmd_t 		*mp_uscmdp;
8292 	int			stat_size, rval;
8293 	int			retry = 0;
8294 
8295 	ASSERT(bp->b_private != NULL);
8296 
8297 	mp_uscmdp = (mp_uscsi_cmd_t *)bp->b_private;
8298 	uscmdp = mp_uscmdp->uscmdp;
8299 	if (uscmdp->uscsi_flags & USCSI_RQENABLE) {
8300 		stat_size = SENSE_LENGTH;
8301 	} else {
8302 		stat_size = 1;
8303 	}
8304 
8305 	pkt = scsi_init_pkt(mp_uscmdp->ap, NULL, bp, uscmdp->uscsi_cdblen,
8306 	    stat_size, 0, 0, SLEEP_FUNC, NULL);
8307 	if (pkt == NULL) {
8308 		VHCI_DEBUG(4, (CE_NOTE, NULL,
8309 		    "vhci_uscsi_iostart: rval: EINVAL"));
8310 		bp->b_resid = bp->b_bcount;
8311 		uscmdp->uscsi_resid = bp->b_bcount;
8312 		bioerror(bp, EINVAL);
8313 		biodone(bp);
8314 		return (EINVAL);
8315 	}
8316 
8317 	pkt->pkt_time = uscmdp->uscsi_timeout;
8318 	bcopy(uscmdp->uscsi_cdb, pkt->pkt_cdbp, (size_t)uscmdp->uscsi_cdblen);
8319 	pkt->pkt_comp = vhci_uscsi_iodone;
8320 	pkt->pkt_private = mp_uscmdp;
8321 	if (uscmdp->uscsi_flags & USCSI_SILENT)
8322 		pkt->pkt_flags |= FLAG_SILENT;
8323 	if (uscmdp->uscsi_flags & USCSI_ISOLATE)
8324 		pkt->pkt_flags |= FLAG_ISOLATE;
8325 	if (uscmdp->uscsi_flags & USCSI_DIAGNOSE)
8326 		pkt->pkt_flags |= FLAG_DIAGNOSE;
8327 	if (uscmdp->uscsi_flags & USCSI_RENEGOT) {
8328 		pkt->pkt_flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
8329 	}
8330 	VHCI_DEBUG(4, (CE_WARN, NULL,
8331 	    "vhci_uscsi_iostart: ap: %p pkt: %p pcdbp: %p uscmdp: %p"
8332 	    " ucdbp: %p pcdblen: %d bp: %p count: %ld pip: %p"
8333 	    " stat_size: %d",
8334 	    (void *)mp_uscmdp->ap, (void *)pkt, (void *)pkt->pkt_cdbp,
8335 	    (void *)uscmdp, (void *)uscmdp->uscsi_cdb, pkt->pkt_cdblen,
8336 	    (void *)bp, bp->b_bcount, (void *)mp_uscmdp->pip, stat_size));
8337 
8338 	while (((rval = scsi_transport(pkt)) == TRAN_BUSY) &&
8339 	    retry < vhci_uscsi_retry_count) {
8340 		delay(drv_usectohz(vhci_uscsi_delay));
8341 		retry++;
8342 	}
8343 	if (retry >= vhci_uscsi_retry_count) {
8344 		VHCI_DEBUG(4, (CE_NOTE, NULL,
8345 		    "vhci_uscsi_iostart: tran_busy - retry: %d", retry));
8346 	}
8347 	switch (rval) {
8348 	case TRAN_ACCEPT:
8349 		rval =  0;
8350 		break;
8351 
8352 	default:
8353 		VHCI_DEBUG(4, (CE_NOTE, NULL,
8354 		    "vhci_uscsi_iostart: rval: %d count: %ld res: %ld",
8355 		    rval, bp->b_bcount, bp->b_resid));
8356 		bp->b_resid = bp->b_bcount;
8357 		uscmdp->uscsi_resid = bp->b_bcount;
8358 		bioerror(bp, EIO);
8359 		scsi_destroy_pkt(pkt);
8360 		biodone(bp);
8361 		rval = EIO;
8362 		MDI_PI_ERRSTAT(mp_uscmdp->pip, MDI_PI_TRANSERR);
8363 		break;
8364 	}
8365 	VHCI_DEBUG(4, (CE_NOTE, NULL,
8366 	    "vhci_uscsi_iostart: exit: rval: %d", rval));
8367 	return (rval);
8368 }
8369