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