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