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