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