xref: /titanic_51/usr/src/uts/common/os/sunmdi.c (revision a204de77cd937c018f628c7dc0357c2cdc90a07e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ee28b439Scm136836  * Common Development and Distribution License (the "License").
6ee28b439Scm136836  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22602ca9eaScth  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Multipath driver interface (MDI) implementation; see mdi_impl.h for a more
297c478bd9Sstevel@tonic-gate  * detailed discussion of the overall mpxio architecture.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * Default locking order:
327c478bd9Sstevel@tonic-gate  *
335e3986cbScth  * _NOTE(LOCK_ORDER(mdi_mutex, mdi_vhci:vh_phci_mutex);
345e3986cbScth  * _NOTE(LOCK_ORDER(mdi_mutex, mdi_vhci:vh_client_mutex);
355e3986cbScth  * _NOTE(LOCK_ORDER(mdi_vhci:vh_phci_mutex, mdi_phci::ph_mutex);
365e3986cbScth  * _NOTE(LOCK_ORDER(mdi_vhci:vh_client_mutex, mdi_client::ct_mutex);
377c478bd9Sstevel@tonic-gate  * _NOTE(LOCK_ORDER(mdi_phci::ph_mutex mdi_pathinfo::pi_mutex))
387c478bd9Sstevel@tonic-gate  * _NOTE(LOCK_ORDER(mdi_phci::ph_mutex mdi_client::ct_mutex))
397c478bd9Sstevel@tonic-gate  * _NOTE(LOCK_ORDER(mdi_client::ct_mutex mdi_pathinfo::pi_mutex))
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/note.h>
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
457c478bd9Sstevel@tonic-gate #include <sys/param.h>
467c478bd9Sstevel@tonic-gate #include <sys/errno.h>
477c478bd9Sstevel@tonic-gate #include <sys/uio.h>
487c478bd9Sstevel@tonic-gate #include <sys/buf.h>
497c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
507c478bd9Sstevel@tonic-gate #include <sys/open.h>
517c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
527c478bd9Sstevel@tonic-gate #include <sys/poll.h>
537c478bd9Sstevel@tonic-gate #include <sys/conf.h>
547c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
557c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
567c478bd9Sstevel@tonic-gate #include <sys/stat.h>
577c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
587c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
597c478bd9Sstevel@tonic-gate #include <sys/ddipropdefs.h>
607c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
617c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
627c478bd9Sstevel@tonic-gate #include <sys/promif.h>
637c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h>
647c478bd9Sstevel@tonic-gate #include <sys/mdi_impldefs.h>
657c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
667c478bd9Sstevel@tonic-gate #include <sys/epm.h>
677c478bd9Sstevel@tonic-gate #include <sys/sunpm.h>
683c34adc5Sramat #include <sys/modhash.h>
698c4f8890Srs135747 #include <sys/disp.h>
708c4f8890Srs135747 #include <sys/autoconf.h>
71f7209cf2Spramodbg #include <sys/sysmacros.h>
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #ifdef	DEBUG
747c478bd9Sstevel@tonic-gate #include <sys/debug.h>
757c478bd9Sstevel@tonic-gate int	mdi_debug = 1;
765e3986cbScth int	mdi_debug_logonly = 0;
777c478bd9Sstevel@tonic-gate #define	MDI_DEBUG(level, stmnt) \
787c478bd9Sstevel@tonic-gate 	    if (mdi_debug >= (level)) i_mdi_log stmnt
797c478bd9Sstevel@tonic-gate static void i_mdi_log(int, dev_info_t *, const char *fmt, ...);
807c478bd9Sstevel@tonic-gate #else	/* !DEBUG */
817c478bd9Sstevel@tonic-gate #define	MDI_DEBUG(level, stmnt)
827c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate extern pri_t	minclsyspri;
857c478bd9Sstevel@tonic-gate extern int	modrootloaded;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Global mutex:
895e3986cbScth  * Protects vHCI list and structure members.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate kmutex_t	mdi_mutex;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * Registered vHCI class driver lists
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate int		mdi_vhci_count;
977c478bd9Sstevel@tonic-gate mdi_vhci_t	*mdi_vhci_head;
987c478bd9Sstevel@tonic-gate mdi_vhci_t	*mdi_vhci_tail;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * Client Hash Table size
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate static int	mdi_client_table_size = CLIENT_HASH_TABLE_SIZE;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * taskq interface definitions
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_N_THREADS	8
1097c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_PRI		minclsyspri
1107c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_MINALLOC	(4*mdi_taskq_n_threads)
1117c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_MAXALLOC	(500*mdi_taskq_n_threads)
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate taskq_t				*mdi_taskq;
1147c478bd9Sstevel@tonic-gate static uint_t			mdi_taskq_n_threads = MDI_TASKQ_N_THREADS;
1157c478bd9Sstevel@tonic-gate 
1163c34adc5Sramat #define	TICKS_PER_SECOND	(drv_usectohz(1000000))
1173c34adc5Sramat 
1187c478bd9Sstevel@tonic-gate /*
1193c34adc5Sramat  * The data should be "quiet" for this interval (in seconds) before the
1203c34adc5Sramat  * vhci cached data is flushed to the disk.
1217c478bd9Sstevel@tonic-gate  */
1223c34adc5Sramat static int mdi_vhcache_flush_delay = 10;
1233c34adc5Sramat 
1243c34adc5Sramat /* number of seconds the vhcache flush daemon will sleep idle before exiting */
1253c34adc5Sramat static int mdi_vhcache_flush_daemon_idle_time = 60;
1263c34adc5Sramat 
1273c34adc5Sramat /*
12867e56d35Sramat  * MDI falls back to discovery of all paths when a bus_config_one fails.
12967e56d35Sramat  * The following parameters can be used to tune this operation.
13067e56d35Sramat  *
13167e56d35Sramat  * mdi_path_discovery_boot
13267e56d35Sramat  *	Number of times path discovery will be attempted during early boot.
13367e56d35Sramat  *	Probably there is no reason to ever set this value to greater than one.
13467e56d35Sramat  *
13567e56d35Sramat  * mdi_path_discovery_postboot
13667e56d35Sramat  *	Number of times path discovery will be attempted after early boot.
13767e56d35Sramat  *	Set it to a minimum of two to allow for discovery of iscsi paths which
13867e56d35Sramat  *	may happen very late during booting.
13967e56d35Sramat  *
14067e56d35Sramat  * mdi_path_discovery_interval
14167e56d35Sramat  *	Minimum number of seconds MDI will wait between successive discovery
14267e56d35Sramat  *	of all paths. Set it to -1 to disable discovery of all paths.
14367e56d35Sramat  */
14467e56d35Sramat static int mdi_path_discovery_boot = 1;
14567e56d35Sramat static int mdi_path_discovery_postboot = 2;
14667e56d35Sramat static int mdi_path_discovery_interval = 10;
14767e56d35Sramat 
14867e56d35Sramat /*
1493c34adc5Sramat  * number of seconds the asynchronous configuration thread will sleep idle
1503c34adc5Sramat  * before exiting.
1513c34adc5Sramat  */
1523c34adc5Sramat static int mdi_async_config_idle_time = 600;
1533c34adc5Sramat 
1543c34adc5Sramat static int mdi_bus_config_cache_hash_size = 256;
1553c34adc5Sramat 
1563c34adc5Sramat /* turns off multithreaded configuration for certain operations */
1573c34adc5Sramat static int mdi_mtc_off = 0;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
160602ca9eaScth  * The "path" to a pathinfo node is identical to the /devices path to a
161602ca9eaScth  * devinfo node had the device been enumerated under a pHCI instead of
162602ca9eaScth  * a vHCI.  This pathinfo "path" is associated with a 'path_instance'.
163602ca9eaScth  * This association persists across create/delete of the pathinfo nodes,
164602ca9eaScth  * but not across reboot.
165602ca9eaScth  */
166602ca9eaScth static uint_t		mdi_pathmap_instance = 1;	/* 0 -> any path */
167602ca9eaScth static int		mdi_pathmap_hash_size = 256;
168602ca9eaScth static kmutex_t		mdi_pathmap_mutex;
169602ca9eaScth static mod_hash_t	*mdi_pathmap_bypath;		/* "path"->instance */
170602ca9eaScth static mod_hash_t	*mdi_pathmap_byinstance;	/* instance->"path" */
171602ca9eaScth 
172602ca9eaScth /*
1737c478bd9Sstevel@tonic-gate  * MDI component property name/value string definitions
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate const char 		*mdi_component_prop = "mpxio-component";
1767c478bd9Sstevel@tonic-gate const char		*mdi_component_prop_vhci = "vhci";
1777c478bd9Sstevel@tonic-gate const char		*mdi_component_prop_phci = "phci";
1787c478bd9Sstevel@tonic-gate const char		*mdi_component_prop_client = "client";
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * MDI client global unique identifier property name
1827c478bd9Sstevel@tonic-gate  */
1837c478bd9Sstevel@tonic-gate const char		*mdi_client_guid_prop = "client-guid";
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * MDI client load balancing property name/value string definitions
1877c478bd9Sstevel@tonic-gate  */
1887c478bd9Sstevel@tonic-gate const char		*mdi_load_balance = "load-balance";
1897c478bd9Sstevel@tonic-gate const char		*mdi_load_balance_none = "none";
1907c478bd9Sstevel@tonic-gate const char		*mdi_load_balance_rr = "round-robin";
1917c478bd9Sstevel@tonic-gate const char		*mdi_load_balance_lba = "logical-block";
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate  * Obsolete vHCI class definition; to be removed after Leadville update
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate const char *mdi_vhci_class_scsi = MDI_HCI_CLASS_SCSI;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate static char vhci_greeting[] =
1997c478bd9Sstevel@tonic-gate 	"\tThere already exists one vHCI driver for class %s\n"
2007c478bd9Sstevel@tonic-gate 	"\tOnly one vHCI driver for each class is allowed\n";
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * Static function prototypes
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate static int		i_mdi_phci_offline(dev_info_t *, uint_t);
2067c478bd9Sstevel@tonic-gate static int		i_mdi_client_offline(dev_info_t *, uint_t);
2077c478bd9Sstevel@tonic-gate static int		i_mdi_phci_pre_detach(dev_info_t *, ddi_detach_cmd_t);
2087c478bd9Sstevel@tonic-gate static void		i_mdi_phci_post_detach(dev_info_t *,
2097c478bd9Sstevel@tonic-gate 			    ddi_detach_cmd_t, int);
2107c478bd9Sstevel@tonic-gate static int		i_mdi_client_pre_detach(dev_info_t *,
2117c478bd9Sstevel@tonic-gate 			    ddi_detach_cmd_t);
2127c478bd9Sstevel@tonic-gate static void		i_mdi_client_post_detach(dev_info_t *,
2137c478bd9Sstevel@tonic-gate 			    ddi_detach_cmd_t, int);
2147c478bd9Sstevel@tonic-gate static void		i_mdi_pm_hold_pip(mdi_pathinfo_t *);
2157c478bd9Sstevel@tonic-gate static void		i_mdi_pm_rele_pip(mdi_pathinfo_t *);
2167c478bd9Sstevel@tonic-gate static int 		i_mdi_lba_lb(mdi_client_t *ct,
2177c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_t **ret_pip, struct buf *buf);
2187c478bd9Sstevel@tonic-gate static void		i_mdi_pm_hold_client(mdi_client_t *, int);
2197c478bd9Sstevel@tonic-gate static void		i_mdi_pm_rele_client(mdi_client_t *, int);
2207c478bd9Sstevel@tonic-gate static void		i_mdi_pm_reset_client(mdi_client_t *);
2217c478bd9Sstevel@tonic-gate static int		i_mdi_power_all_phci(mdi_client_t *);
2228c4f8890Srs135747 static void		i_mdi_log_sysevent(dev_info_t *, char *, char *);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * Internal mdi_pathinfo node functions
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate static void		i_mdi_pi_kstat_destroy(mdi_pathinfo_t *);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate static mdi_vhci_t	*i_mdi_vhci_class2vhci(char *);
2317c478bd9Sstevel@tonic-gate static mdi_vhci_t	*i_devi_get_vhci(dev_info_t *);
2327c478bd9Sstevel@tonic-gate static mdi_phci_t	*i_devi_get_phci(dev_info_t *);
2337c478bd9Sstevel@tonic-gate static void		i_mdi_phci_lock(mdi_phci_t *, mdi_pathinfo_t *);
2347c478bd9Sstevel@tonic-gate static void		i_mdi_phci_unlock(mdi_phci_t *);
2353c34adc5Sramat static mdi_pathinfo_t	*i_mdi_pi_alloc(mdi_phci_t *, char *, mdi_client_t *);
2367c478bd9Sstevel@tonic-gate static void		i_mdi_phci_add_path(mdi_phci_t *, mdi_pathinfo_t *);
2377c478bd9Sstevel@tonic-gate static void		i_mdi_client_add_path(mdi_client_t *, mdi_pathinfo_t *);
2387c478bd9Sstevel@tonic-gate static void		i_mdi_pi_free(mdi_phci_t *ph, mdi_pathinfo_t *,
2397c478bd9Sstevel@tonic-gate 			    mdi_client_t *);
2407c478bd9Sstevel@tonic-gate static void		i_mdi_phci_remove_path(mdi_phci_t *, mdi_pathinfo_t *);
2417c478bd9Sstevel@tonic-gate static void		i_mdi_client_remove_path(mdi_client_t *,
2427c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_t *);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate static int		i_mdi_pi_state_change(mdi_pathinfo_t *,
2457c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_state_t, int);
2467c478bd9Sstevel@tonic-gate static int		i_mdi_pi_offline(mdi_pathinfo_t *, int);
2477c478bd9Sstevel@tonic-gate static dev_info_t	*i_mdi_devinfo_create(mdi_vhci_t *, char *, char *,
2483c34adc5Sramat 			    char **, int);
2497c478bd9Sstevel@tonic-gate static dev_info_t	*i_mdi_devinfo_find(mdi_vhci_t *, char *, char *);
2507c478bd9Sstevel@tonic-gate static int		i_mdi_devinfo_remove(dev_info_t *, dev_info_t *, int);
2517c478bd9Sstevel@tonic-gate static int		i_mdi_is_child_present(dev_info_t *, dev_info_t *);
2523c34adc5Sramat static mdi_client_t	*i_mdi_client_alloc(mdi_vhci_t *, char *, char *);
2537c478bd9Sstevel@tonic-gate static void		i_mdi_client_enlist_table(mdi_vhci_t *, mdi_client_t *);
2547c478bd9Sstevel@tonic-gate static void		i_mdi_client_delist_table(mdi_vhci_t *, mdi_client_t *);
2553c34adc5Sramat static mdi_client_t	*i_mdi_client_find(mdi_vhci_t *, char *, char *);
2567c478bd9Sstevel@tonic-gate static void		i_mdi_client_update_state(mdi_client_t *);
2577c478bd9Sstevel@tonic-gate static int		i_mdi_client_compute_state(mdi_client_t *,
2587c478bd9Sstevel@tonic-gate 			    mdi_phci_t *);
2597c478bd9Sstevel@tonic-gate static void		i_mdi_client_lock(mdi_client_t *, mdi_pathinfo_t *);
2607c478bd9Sstevel@tonic-gate static void		i_mdi_client_unlock(mdi_client_t *);
2617c478bd9Sstevel@tonic-gate static int		i_mdi_client_free(mdi_vhci_t *, mdi_client_t *);
2627c478bd9Sstevel@tonic-gate static mdi_client_t	*i_devi_get_client(dev_info_t *);
263ee28b439Scm136836 /*
264ee28b439Scm136836  * NOTE: this will be removed once the NWS files are changed to use the new
265ee28b439Scm136836  * mdi_{enable,disable}_path interfaces
266ee28b439Scm136836  */
267ee28b439Scm136836 static int		i_mdi_pi_enable_disable(dev_info_t *, dev_info_t *,
268ee28b439Scm136836 				int, int);
269ee28b439Scm136836 static mdi_pathinfo_t 	*i_mdi_enable_disable_path(mdi_pathinfo_t *pip,
270ee28b439Scm136836 				mdi_vhci_t *vh, int flags, int op);
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate  * Failover related function prototypes
2737c478bd9Sstevel@tonic-gate  */
2747c478bd9Sstevel@tonic-gate static int		i_mdi_failover(void *);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate  * misc internal functions
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate static int		i_mdi_get_hash_key(char *);
2807c478bd9Sstevel@tonic-gate static int		i_map_nvlist_error_to_mdi(int);
2817c478bd9Sstevel@tonic-gate static void		i_mdi_report_path_state(mdi_client_t *,
2827c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_t *);
2837c478bd9Sstevel@tonic-gate 
2843c34adc5Sramat static void		setup_vhci_cache(mdi_vhci_t *);
2853c34adc5Sramat static int		destroy_vhci_cache(mdi_vhci_t *);
2863c34adc5Sramat static int		stop_vhcache_async_threads(mdi_vhci_config_t *);
2873c34adc5Sramat static boolean_t	stop_vhcache_flush_thread(void *, int);
2883c34adc5Sramat static void		free_string_array(char **, int);
2893c34adc5Sramat static void		free_vhcache_phci(mdi_vhcache_phci_t *);
2903c34adc5Sramat static void		free_vhcache_pathinfo(mdi_vhcache_pathinfo_t *);
2913c34adc5Sramat static void		free_vhcache_client(mdi_vhcache_client_t *);
2923c34adc5Sramat static int		mainnvl_to_vhcache(mdi_vhci_cache_t *, nvlist_t *);
2933c34adc5Sramat static nvlist_t		*vhcache_to_mainnvl(mdi_vhci_cache_t *);
2943c34adc5Sramat static void		vhcache_phci_add(mdi_vhci_config_t *, mdi_phci_t *);
2953c34adc5Sramat static void		vhcache_phci_remove(mdi_vhci_config_t *, mdi_phci_t *);
2963c34adc5Sramat static void		vhcache_pi_add(mdi_vhci_config_t *,
2973c34adc5Sramat 			    struct mdi_pathinfo *);
2983c34adc5Sramat static void		vhcache_pi_remove(mdi_vhci_config_t *,
2993c34adc5Sramat 			    struct mdi_pathinfo *);
3003c34adc5Sramat static void		free_phclient_path_list(mdi_phys_path_t *);
3013c34adc5Sramat static void		sort_vhcache_paths(mdi_vhcache_client_t *);
3023c34adc5Sramat static int		flush_vhcache(mdi_vhci_config_t *, int);
3033c34adc5Sramat static void		vhcache_dirty(mdi_vhci_config_t *);
3043c34adc5Sramat static void		free_async_client_config(mdi_async_client_config_t *);
30567e56d35Sramat static void		single_threaded_vhconfig_enter(mdi_vhci_config_t *);
30667e56d35Sramat static void		single_threaded_vhconfig_exit(mdi_vhci_config_t *);
3073c34adc5Sramat static nvlist_t		*read_on_disk_vhci_cache(char *);
3083c34adc5Sramat extern int		fread_nvlist(char *, nvlist_t **);
3093c34adc5Sramat extern int		fwrite_nvlist(char *, nvlist_t *);
3103c34adc5Sramat 
3117c478bd9Sstevel@tonic-gate /* called once when first vhci registers with mdi */
3127c478bd9Sstevel@tonic-gate static void
3137c478bd9Sstevel@tonic-gate i_mdi_init()
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	static int initialized = 0;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (initialized)
3187c478bd9Sstevel@tonic-gate 		return;
3197c478bd9Sstevel@tonic-gate 	initialized = 1;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	mutex_init(&mdi_mutex, NULL, MUTEX_DEFAULT, NULL);
322602ca9eaScth 
323602ca9eaScth 	/* Create our taskq resources */
3247c478bd9Sstevel@tonic-gate 	mdi_taskq = taskq_create("mdi_taskq", mdi_taskq_n_threads,
3257c478bd9Sstevel@tonic-gate 	    MDI_TASKQ_PRI, MDI_TASKQ_MINALLOC, MDI_TASKQ_MAXALLOC,
3267c478bd9Sstevel@tonic-gate 	    TASKQ_PREPOPULATE | TASKQ_CPR_SAFE);
3277c478bd9Sstevel@tonic-gate 	ASSERT(mdi_taskq != NULL);	/* taskq_create never fails */
328602ca9eaScth 
329602ca9eaScth 	/* Allocate ['path_instance' <-> "path"] maps */
330602ca9eaScth 	mutex_init(&mdi_pathmap_mutex, NULL, MUTEX_DRIVER, NULL);
331602ca9eaScth 	mdi_pathmap_bypath = mod_hash_create_strhash(
332602ca9eaScth 	    "mdi_pathmap_bypath", mdi_pathmap_hash_size,
333602ca9eaScth 	    mod_hash_null_valdtor);
334602ca9eaScth 	mdi_pathmap_byinstance = mod_hash_create_idhash(
335602ca9eaScth 	    "mdi_pathmap_byinstance", mdi_pathmap_hash_size,
336602ca9eaScth 	    mod_hash_null_valdtor);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  * mdi_get_component_type():
3417c478bd9Sstevel@tonic-gate  *		Return mpxio component type
3427c478bd9Sstevel@tonic-gate  * Return Values:
3437c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_NONE
3447c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_VHCI
3457c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_PHCI
3467c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_CLIENT
3477c478bd9Sstevel@tonic-gate  * XXX This doesn't work under multi-level MPxIO and should be
3485e3986cbScth  *	removed when clients migrate mdi_component_is_*() interfaces.
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate int
3517c478bd9Sstevel@tonic-gate mdi_get_component_type(dev_info_t *dip)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_mdi_component);
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate /*
3577c478bd9Sstevel@tonic-gate  * mdi_vhci_register():
3587c478bd9Sstevel@tonic-gate  *		Register a vHCI module with the mpxio framework
3597c478bd9Sstevel@tonic-gate  *		mdi_vhci_register() is called by vHCI drivers to register the
3607c478bd9Sstevel@tonic-gate  *		'class_driver' vHCI driver and its MDI entrypoints with the
3617c478bd9Sstevel@tonic-gate  *		mpxio framework.  The vHCI driver must call this interface as
3627c478bd9Sstevel@tonic-gate  *		part of its attach(9e) handler.
3637c478bd9Sstevel@tonic-gate  *		Competing threads may try to attach mdi_vhci_register() as
3647c478bd9Sstevel@tonic-gate  *		the vHCI drivers are loaded and attached as a result of pHCI
3657c478bd9Sstevel@tonic-gate  *		driver instance registration (mdi_phci_register()) with the
3667c478bd9Sstevel@tonic-gate  *		framework.
3677c478bd9Sstevel@tonic-gate  * Return Values:
3687c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
3697c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3727c478bd9Sstevel@tonic-gate int
3737c478bd9Sstevel@tonic-gate mdi_vhci_register(char *class, dev_info_t *vdip, mdi_vhci_ops_t *vops,
3747c478bd9Sstevel@tonic-gate     int flags)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh = NULL;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	ASSERT(vops->vo_revision == MDI_VHCI_OPS_REV);
3795e3986cbScth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(vdip)));
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	i_mdi_init();
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	mutex_enter(&mdi_mutex);
3847c478bd9Sstevel@tonic-gate 	/*
3857c478bd9Sstevel@tonic-gate 	 * Scan for already registered vhci
3867c478bd9Sstevel@tonic-gate 	 */
3877c478bd9Sstevel@tonic-gate 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
3887c478bd9Sstevel@tonic-gate 		if (strcmp(vh->vh_class, class) == 0) {
3897c478bd9Sstevel@tonic-gate 			/*
3907c478bd9Sstevel@tonic-gate 			 * vHCI has already been created.  Check for valid
3917c478bd9Sstevel@tonic-gate 			 * vHCI ops registration.  We only support one vHCI
3927c478bd9Sstevel@tonic-gate 			 * module per class
3937c478bd9Sstevel@tonic-gate 			 */
3947c478bd9Sstevel@tonic-gate 			if (vh->vh_ops != NULL) {
3957c478bd9Sstevel@tonic-gate 				mutex_exit(&mdi_mutex);
3967c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, vhci_greeting, class);
3977c478bd9Sstevel@tonic-gate 				return (MDI_FAILURE);
3987c478bd9Sstevel@tonic-gate 			}
3997c478bd9Sstevel@tonic-gate 			break;
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	/*
4047c478bd9Sstevel@tonic-gate 	 * if not yet created, create the vHCI component
4057c478bd9Sstevel@tonic-gate 	 */
4067c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
4077c478bd9Sstevel@tonic-gate 		struct client_hash	*hash = NULL;
4087c478bd9Sstevel@tonic-gate 		char			*load_balance;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 		/*
4117c478bd9Sstevel@tonic-gate 		 * Allocate and initialize the mdi extensions
4127c478bd9Sstevel@tonic-gate 		 */
4137c478bd9Sstevel@tonic-gate 		vh = kmem_zalloc(sizeof (mdi_vhci_t), KM_SLEEP);
4147c478bd9Sstevel@tonic-gate 		hash = kmem_zalloc(mdi_client_table_size * sizeof (*hash),
4157c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
4167c478bd9Sstevel@tonic-gate 		vh->vh_client_table = hash;
4177c478bd9Sstevel@tonic-gate 		vh->vh_class = kmem_zalloc(strlen(class) + 1, KM_SLEEP);
4187c478bd9Sstevel@tonic-gate 		(void) strcpy(vh->vh_class, class);
4197c478bd9Sstevel@tonic-gate 		vh->vh_lb = LOAD_BALANCE_RR;
4207c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, vdip,
4217c478bd9Sstevel@tonic-gate 		    0, LOAD_BALANCE_PROP, &load_balance) == DDI_SUCCESS) {
4227c478bd9Sstevel@tonic-gate 			if (strcmp(load_balance, LOAD_BALANCE_PROP_NONE) == 0) {
4237c478bd9Sstevel@tonic-gate 				vh->vh_lb = LOAD_BALANCE_NONE;
4247c478bd9Sstevel@tonic-gate 			} else if (strcmp(load_balance, LOAD_BALANCE_PROP_LBA)
4257c478bd9Sstevel@tonic-gate 				    == 0) {
4267c478bd9Sstevel@tonic-gate 				vh->vh_lb = LOAD_BALANCE_LBA;
4277c478bd9Sstevel@tonic-gate 			}
4287c478bd9Sstevel@tonic-gate 			ddi_prop_free(load_balance);
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 
4315e3986cbScth 		mutex_init(&vh->vh_phci_mutex, NULL, MUTEX_DEFAULT, NULL);
4325e3986cbScth 		mutex_init(&vh->vh_client_mutex, NULL, MUTEX_DEFAULT, NULL);
4335e3986cbScth 
4347c478bd9Sstevel@tonic-gate 		/*
4357c478bd9Sstevel@tonic-gate 		 * Store the vHCI ops vectors
4367c478bd9Sstevel@tonic-gate 		 */
4377c478bd9Sstevel@tonic-gate 		vh->vh_dip = vdip;
4387c478bd9Sstevel@tonic-gate 		vh->vh_ops = vops;
4397c478bd9Sstevel@tonic-gate 
4403c34adc5Sramat 		setup_vhci_cache(vh);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		if (mdi_vhci_head == NULL) {
4437c478bd9Sstevel@tonic-gate 			mdi_vhci_head = vh;
4447c478bd9Sstevel@tonic-gate 		}
4457c478bd9Sstevel@tonic-gate 		if (mdi_vhci_tail) {
4467c478bd9Sstevel@tonic-gate 			mdi_vhci_tail->vh_next = vh;
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 		mdi_vhci_tail = vh;
4497c478bd9Sstevel@tonic-gate 		mdi_vhci_count++;
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	/*
4537c478bd9Sstevel@tonic-gate 	 * Claim the devfs node as a vhci component
4547c478bd9Sstevel@tonic-gate 	 */
4557c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_component |= MDI_COMPONENT_VHCI;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/*
4587c478bd9Sstevel@tonic-gate 	 * Initialize our back reference from dev_info node
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_xhci = (caddr_t)vh;
4617c478bd9Sstevel@tonic-gate 	mutex_exit(&mdi_mutex);
4627c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate  * mdi_vhci_unregister():
4677c478bd9Sstevel@tonic-gate  *		Unregister a vHCI module from mpxio framework
4687c478bd9Sstevel@tonic-gate  *		mdi_vhci_unregister() is called from the detach(9E) entrypoint
4697c478bd9Sstevel@tonic-gate  * 		of a vhci to unregister it from the framework.
4707c478bd9Sstevel@tonic-gate  * Return Values:
4717c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
4727c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
4737c478bd9Sstevel@tonic-gate  */
4747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4757c478bd9Sstevel@tonic-gate int
4767c478bd9Sstevel@tonic-gate mdi_vhci_unregister(dev_info_t *vdip, int flags)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*found, *vh, *prev = NULL;
4797c478bd9Sstevel@tonic-gate 
4805e3986cbScth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(vdip)));
4815e3986cbScth 
4827c478bd9Sstevel@tonic-gate 	/*
4837c478bd9Sstevel@tonic-gate 	 * Check for invalid VHCI
4847c478bd9Sstevel@tonic-gate 	 */
4857c478bd9Sstevel@tonic-gate 	if ((vh = i_devi_get_vhci(vdip)) == NULL)
4867c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	/*
4897c478bd9Sstevel@tonic-gate 	 * Scan the list of registered vHCIs for a match
4907c478bd9Sstevel@tonic-gate 	 */
4915e3986cbScth 	mutex_enter(&mdi_mutex);
4927c478bd9Sstevel@tonic-gate 	for (found = mdi_vhci_head; found != NULL; found = found->vh_next) {
4937c478bd9Sstevel@tonic-gate 		if (found == vh)
4947c478bd9Sstevel@tonic-gate 			break;
4957c478bd9Sstevel@tonic-gate 		prev = found;
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	if (found == NULL) {
4997c478bd9Sstevel@tonic-gate 		mutex_exit(&mdi_mutex);
5007c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	/*
5048c4f8890Srs135747 	 * Check the vHCI, pHCI and client count. All the pHCIs and clients
5057c478bd9Sstevel@tonic-gate 	 * should have been unregistered, before a vHCI can be
5067c478bd9Sstevel@tonic-gate 	 * unregistered.
5077c478bd9Sstevel@tonic-gate 	 */
5085e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
5095e3986cbScth 	if (vh->vh_refcnt || vh->vh_phci_count || vh->vh_client_count) {
5105e3986cbScth 		MDI_VHCI_PHCI_UNLOCK(vh);
5115e3986cbScth 		mutex_exit(&mdi_mutex);
5125e3986cbScth 		return (MDI_FAILURE);
5135e3986cbScth 	}
5145e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
5155e3986cbScth 
5165e3986cbScth 	if (destroy_vhci_cache(vh) != MDI_SUCCESS) {
5177c478bd9Sstevel@tonic-gate 		mutex_exit(&mdi_mutex);
5187c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/*
5227c478bd9Sstevel@tonic-gate 	 * Remove the vHCI from the global list
5237c478bd9Sstevel@tonic-gate 	 */
5247c478bd9Sstevel@tonic-gate 	if (vh == mdi_vhci_head) {
5257c478bd9Sstevel@tonic-gate 		mdi_vhci_head = vh->vh_next;
5267c478bd9Sstevel@tonic-gate 	} else {
5277c478bd9Sstevel@tonic-gate 		prev->vh_next = vh->vh_next;
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 	if (vh == mdi_vhci_tail) {
5307c478bd9Sstevel@tonic-gate 		mdi_vhci_tail = prev;
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 	mdi_vhci_count--;
5337c478bd9Sstevel@tonic-gate 	mutex_exit(&mdi_mutex);
5343c34adc5Sramat 
5353c34adc5Sramat 	vh->vh_ops = NULL;
5367c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_component &= ~MDI_COMPONENT_VHCI;
5377c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_xhci = NULL;
5387c478bd9Sstevel@tonic-gate 	kmem_free(vh->vh_class, strlen(vh->vh_class)+1);
5397c478bd9Sstevel@tonic-gate 	kmem_free(vh->vh_client_table,
5407c478bd9Sstevel@tonic-gate 	    mdi_client_table_size * sizeof (struct client_hash));
5415e3986cbScth 	mutex_destroy(&vh->vh_phci_mutex);
5425e3986cbScth 	mutex_destroy(&vh->vh_client_mutex);
54378dc6db2Sllai1 
5447c478bd9Sstevel@tonic-gate 	kmem_free(vh, sizeof (mdi_vhci_t));
5457c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate /*
5497c478bd9Sstevel@tonic-gate  * i_mdi_vhci_class2vhci():
5507c478bd9Sstevel@tonic-gate  *		Look for a matching vHCI module given a vHCI class name
5517c478bd9Sstevel@tonic-gate  * Return Values:
5527c478bd9Sstevel@tonic-gate  *		Handle to a vHCI component
5537c478bd9Sstevel@tonic-gate  *		NULL
5547c478bd9Sstevel@tonic-gate  */
5557c478bd9Sstevel@tonic-gate static mdi_vhci_t *
5567c478bd9Sstevel@tonic-gate i_mdi_vhci_class2vhci(char *class)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&mdi_mutex));
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	mutex_enter(&mdi_mutex);
5637c478bd9Sstevel@tonic-gate 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
5647c478bd9Sstevel@tonic-gate 		if (strcmp(vh->vh_class, class) == 0) {
5657c478bd9Sstevel@tonic-gate 			break;
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 	mutex_exit(&mdi_mutex);
5697c478bd9Sstevel@tonic-gate 	return (vh);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /*
5737c478bd9Sstevel@tonic-gate  * i_devi_get_vhci():
5747c478bd9Sstevel@tonic-gate  *		Utility function to get the handle to a vHCI component
5757c478bd9Sstevel@tonic-gate  * Return Values:
5767c478bd9Sstevel@tonic-gate  *		Handle to a vHCI component
5777c478bd9Sstevel@tonic-gate  *		NULL
5787c478bd9Sstevel@tonic-gate  */
5797c478bd9Sstevel@tonic-gate mdi_vhci_t *
5807c478bd9Sstevel@tonic-gate i_devi_get_vhci(dev_info_t *vdip)
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
5837c478bd9Sstevel@tonic-gate 	if (MDI_VHCI(vdip)) {
5847c478bd9Sstevel@tonic-gate 		vh = (mdi_vhci_t *)DEVI(vdip)->devi_mdi_xhci;
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 	return (vh);
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate /*
5907c478bd9Sstevel@tonic-gate  * mdi_phci_register():
5917c478bd9Sstevel@tonic-gate  *		Register a pHCI module with mpxio framework
5927c478bd9Sstevel@tonic-gate  *		mdi_phci_register() is called by pHCI drivers to register with
5937c478bd9Sstevel@tonic-gate  *		the mpxio framework and a specific 'class_driver' vHCI.  The
5947c478bd9Sstevel@tonic-gate  *		pHCI driver must call this interface as part of its attach(9e)
5957c478bd9Sstevel@tonic-gate  *		handler.
5967c478bd9Sstevel@tonic-gate  * Return Values:
5977c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
5987c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
5997c478bd9Sstevel@tonic-gate  */
6007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6017c478bd9Sstevel@tonic-gate int
6027c478bd9Sstevel@tonic-gate mdi_phci_register(char *class, dev_info_t *pdip, int flags)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate 	mdi_phci_t		*ph;
6057c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh;
6067c478bd9Sstevel@tonic-gate 	char			*data;
6077c478bd9Sstevel@tonic-gate 	char			*pathname;
6087c478bd9Sstevel@tonic-gate 
6095e3986cbScth 	/*
6105e3986cbScth 	 * Some subsystems, like fcp, perform pHCI registration from a
6115e3986cbScth 	 * different thread than the one doing the pHCI attach(9E) - the
6125e3986cbScth 	 * driver attach code is waiting for this other thread to complete.
6135e3986cbScth 	 * This means we can only ASSERT DEVI_BUSY_CHANGING of parent
6145e3986cbScth 	 * (indicating that some thread has done an ndi_devi_enter of parent)
6155e3986cbScth 	 * not DEVI_BUSY_OWNED (which would indicate that we did the enter).
6165e3986cbScth 	 */
6175e3986cbScth 	ASSERT(DEVI_BUSY_CHANGING(ddi_get_parent(pdip)));
6185e3986cbScth 
6197c478bd9Sstevel@tonic-gate 	pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
6207c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(pdip, pathname);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	/*
6237c478bd9Sstevel@tonic-gate 	 * Check for mpxio-disable property. Enable mpxio if the property is
6247c478bd9Sstevel@tonic-gate 	 * missing or not set to "yes".
6257c478bd9Sstevel@tonic-gate 	 * If the property is set to "yes" then emit a brief message.
6267c478bd9Sstevel@tonic-gate 	 */
6277c478bd9Sstevel@tonic-gate 	if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip, 0, "mpxio-disable",
6287c478bd9Sstevel@tonic-gate 	    &data) == DDI_SUCCESS)) {
6297c478bd9Sstevel@tonic-gate 		if (strcmp(data, "yes") == 0) {
6307c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_CONT, pdip,
6317c478bd9Sstevel@tonic-gate 			    "?%s (%s%d) multipath capabilities "
6327c478bd9Sstevel@tonic-gate 			    "disabled via %s.conf.\n", pathname,
6337c478bd9Sstevel@tonic-gate 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
6347c478bd9Sstevel@tonic-gate 			    ddi_driver_name(pdip)));
6357c478bd9Sstevel@tonic-gate 			ddi_prop_free(data);
6367c478bd9Sstevel@tonic-gate 			kmem_free(pathname, MAXPATHLEN);
6377c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
6387c478bd9Sstevel@tonic-gate 		}
6397c478bd9Sstevel@tonic-gate 		ddi_prop_free(data);
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	/*
6457c478bd9Sstevel@tonic-gate 	 * Search for a matching vHCI
6467c478bd9Sstevel@tonic-gate 	 */
6477c478bd9Sstevel@tonic-gate 	vh = (mdi_vhci_t *)i_mdi_vhci_class2vhci(class);
6487c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
6497c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	ph = kmem_zalloc(sizeof (mdi_phci_t), KM_SLEEP);
6537c478bd9Sstevel@tonic-gate 	mutex_init(&ph->ph_mutex, NULL, MUTEX_DEFAULT, NULL);
6547c478bd9Sstevel@tonic-gate 	ph->ph_dip = pdip;
6557c478bd9Sstevel@tonic-gate 	ph->ph_vhci = vh;
6567c478bd9Sstevel@tonic-gate 	ph->ph_next = NULL;
6577c478bd9Sstevel@tonic-gate 	ph->ph_unstable = 0;
6587c478bd9Sstevel@tonic-gate 	ph->ph_vprivate = 0;
6597c478bd9Sstevel@tonic-gate 	cv_init(&ph->ph_unstable_cv, NULL, CV_DRIVER, NULL);
6607c478bd9Sstevel@tonic-gate 
6615e3986cbScth 	MDI_PHCI_LOCK(ph);
6627c478bd9Sstevel@tonic-gate 	MDI_PHCI_SET_POWER_UP(ph);
6635e3986cbScth 	MDI_PHCI_UNLOCK(ph);
6647c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_component |= MDI_COMPONENT_PHCI;
6657c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_xhci = (caddr_t)ph;
6667c478bd9Sstevel@tonic-gate 
6673c34adc5Sramat 	vhcache_phci_add(vh->vh_config, ph);
6683c34adc5Sramat 
6695e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
6707c478bd9Sstevel@tonic-gate 	if (vh->vh_phci_head == NULL) {
6717c478bd9Sstevel@tonic-gate 		vh->vh_phci_head = ph;
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 	if (vh->vh_phci_tail) {
6747c478bd9Sstevel@tonic-gate 		vh->vh_phci_tail->ph_next = ph;
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 	vh->vh_phci_tail = ph;
6777c478bd9Sstevel@tonic-gate 	vh->vh_phci_count++;
6785e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
6795e3986cbScth 
6808c4f8890Srs135747 	i_mdi_log_sysevent(pdip, class, ESC_DDI_INITIATOR_REGISTER);
6817c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate /*
6857c478bd9Sstevel@tonic-gate  * mdi_phci_unregister():
6867c478bd9Sstevel@tonic-gate  *		Unregister a pHCI module from mpxio framework
6877c478bd9Sstevel@tonic-gate  *		mdi_phci_unregister() is called by the pHCI drivers from their
6887c478bd9Sstevel@tonic-gate  *		detach(9E) handler to unregister their instances from the
6897c478bd9Sstevel@tonic-gate  *		framework.
6907c478bd9Sstevel@tonic-gate  * Return Values:
6917c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
6927c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
6937c478bd9Sstevel@tonic-gate  */
6947c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6957c478bd9Sstevel@tonic-gate int
6967c478bd9Sstevel@tonic-gate mdi_phci_unregister(dev_info_t *pdip, int flags)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh;
6997c478bd9Sstevel@tonic-gate 	mdi_phci_t		*ph;
7007c478bd9Sstevel@tonic-gate 	mdi_phci_t		*tmp;
7017c478bd9Sstevel@tonic-gate 	mdi_phci_t		*prev = NULL;
7027c478bd9Sstevel@tonic-gate 
7035e3986cbScth 	ASSERT(DEVI_BUSY_CHANGING(ddi_get_parent(pdip)));
7045e3986cbScth 
7057c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
7067c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
7077c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, pdip,
7087c478bd9Sstevel@tonic-gate 		    "!pHCI unregister: Not a valid pHCI"));
7097c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
7137c478bd9Sstevel@tonic-gate 	ASSERT(vh != NULL);
7147c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
7157c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, pdip,
7167c478bd9Sstevel@tonic-gate 		    "!pHCI unregister: Not a valid vHCI"));
7177c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 
7205e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
7217c478bd9Sstevel@tonic-gate 	tmp = vh->vh_phci_head;
7227c478bd9Sstevel@tonic-gate 	while (tmp) {
7237c478bd9Sstevel@tonic-gate 		if (tmp == ph) {
7247c478bd9Sstevel@tonic-gate 			break;
7257c478bd9Sstevel@tonic-gate 		}
7267c478bd9Sstevel@tonic-gate 		prev = tmp;
7277c478bd9Sstevel@tonic-gate 		tmp = tmp->ph_next;
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	if (ph == vh->vh_phci_head) {
7317c478bd9Sstevel@tonic-gate 		vh->vh_phci_head = ph->ph_next;
7327c478bd9Sstevel@tonic-gate 	} else {
7337c478bd9Sstevel@tonic-gate 		prev->ph_next = ph->ph_next;
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	if (ph == vh->vh_phci_tail) {
7377c478bd9Sstevel@tonic-gate 		vh->vh_phci_tail = prev;
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	vh->vh_phci_count--;
7415e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
7427c478bd9Sstevel@tonic-gate 
7438c4f8890Srs135747 	i_mdi_log_sysevent(pdip, ph->ph_vhci->vh_class,
7448c4f8890Srs135747 	    ESC_DDI_INITIATOR_UNREGISTER);
7453c34adc5Sramat 	vhcache_phci_remove(vh->vh_config, ph);
7467c478bd9Sstevel@tonic-gate 	cv_destroy(&ph->ph_unstable_cv);
7477c478bd9Sstevel@tonic-gate 	mutex_destroy(&ph->ph_mutex);
7487c478bd9Sstevel@tonic-gate 	kmem_free(ph, sizeof (mdi_phci_t));
7497c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_component &= ~MDI_COMPONENT_PHCI;
7507c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_xhci = NULL;
7517c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate /*
7557c478bd9Sstevel@tonic-gate  * i_devi_get_phci():
7567c478bd9Sstevel@tonic-gate  * 		Utility function to return the phci extensions.
7577c478bd9Sstevel@tonic-gate  */
7587c478bd9Sstevel@tonic-gate static mdi_phci_t *
7597c478bd9Sstevel@tonic-gate i_devi_get_phci(dev_info_t *pdip)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph = NULL;
7627c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(pdip)) {
7637c478bd9Sstevel@tonic-gate 		ph = (mdi_phci_t *)DEVI(pdip)->devi_mdi_xhci;
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 	return (ph);
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate /*
7695e3986cbScth  * Single thread mdi entry into devinfo node for modifying its children.
7705e3986cbScth  * If necessary we perform an ndi_devi_enter of the vHCI before doing
7715e3986cbScth  * an ndi_devi_enter of 'dip'.  We maintain circular in two parts: one
7725e3986cbScth  * for the vHCI and one for the pHCI.
7735e3986cbScth  */
7745e3986cbScth void
7755e3986cbScth mdi_devi_enter(dev_info_t *phci_dip, int *circular)
7765e3986cbScth {
7775e3986cbScth 	dev_info_t	*vdip;
7785e3986cbScth 	int		vcircular, pcircular;
7795e3986cbScth 
7805e3986cbScth 	/* Verify calling context */
7815e3986cbScth 	ASSERT(MDI_PHCI(phci_dip));
7825e3986cbScth 	vdip = mdi_devi_get_vdip(phci_dip);
7835e3986cbScth 	ASSERT(vdip);			/* A pHCI always has a vHCI */
7845e3986cbScth 
7855e3986cbScth 	/*
7865e3986cbScth 	 * If pHCI is detaching then the framework has already entered the
7875e3986cbScth 	 * vHCI on a threads that went down the code path leading to
7885e3986cbScth 	 * detach_node().  This framework enter of the vHCI during pHCI
7895e3986cbScth 	 * detach is done to avoid deadlock with vHCI power management
7905e3986cbScth 	 * operations which enter the vHCI and the enter down the path
7915e3986cbScth 	 * to the pHCI. If pHCI is detaching then we piggyback this calls
7925e3986cbScth 	 * enter of the vHCI on frameworks vHCI enter that has already
7935e3986cbScth 	 * occurred - this is OK because we know that the framework thread
7945e3986cbScth 	 * doing detach is waiting for our completion.
7955e3986cbScth 	 *
7965e3986cbScth 	 * We should DEVI_IS_DETACHING under an enter of the parent to avoid
7975e3986cbScth 	 * race with detach - but we can't do that because the framework has
7985e3986cbScth 	 * already entered the parent, so we have some complexity instead.
7995e3986cbScth 	 */
8005e3986cbScth 	for (;;) {
8015e3986cbScth 		if (ndi_devi_tryenter(vdip, &vcircular)) {
8025e3986cbScth 			ASSERT(vcircular != -1);
8035e3986cbScth 			if (DEVI_IS_DETACHING(phci_dip)) {
8045e3986cbScth 				ndi_devi_exit(vdip, vcircular);
8055e3986cbScth 				vcircular = -1;
8065e3986cbScth 			}
8075e3986cbScth 			break;
8085e3986cbScth 		} else if (DEVI_IS_DETACHING(phci_dip)) {
8095e3986cbScth 			vcircular = -1;
8105e3986cbScth 			break;
8115e3986cbScth 		} else {
8125e3986cbScth 			delay(1);
8135e3986cbScth 		}
8145e3986cbScth 	}
8155e3986cbScth 
8165e3986cbScth 	ndi_devi_enter(phci_dip, &pcircular);
8175e3986cbScth 	*circular = (vcircular << 16) | (pcircular & 0xFFFF);
8185e3986cbScth }
8195e3986cbScth 
8205e3986cbScth /*
8215e3986cbScth  * Release mdi_devi_enter or successful mdi_devi_tryenter.
8225e3986cbScth  */
8235e3986cbScth void
8245e3986cbScth mdi_devi_exit(dev_info_t *phci_dip, int circular)
8255e3986cbScth {
8265e3986cbScth 	dev_info_t	*vdip;
8275e3986cbScth 	int		vcircular, pcircular;
8285e3986cbScth 
8295e3986cbScth 	/* Verify calling context */
8305e3986cbScth 	ASSERT(MDI_PHCI(phci_dip));
8315e3986cbScth 	vdip = mdi_devi_get_vdip(phci_dip);
8325e3986cbScth 	ASSERT(vdip);			/* A pHCI always has a vHCI */
8335e3986cbScth 
8345e3986cbScth 	/* extract two circular recursion values from single int */
8355e3986cbScth 	pcircular = (short)(circular & 0xFFFF);
8365e3986cbScth 	vcircular = (short)((circular >> 16) & 0xFFFF);
8375e3986cbScth 
8385e3986cbScth 	ndi_devi_exit(phci_dip, pcircular);
8395e3986cbScth 	if (vcircular != -1)
8405e3986cbScth 		ndi_devi_exit(vdip, vcircular);
8415e3986cbScth }
8425e3986cbScth 
8435e3986cbScth /*
8445e3986cbScth  * The functions mdi_devi_exit_phci() and mdi_devi_enter_phci() are used
8455e3986cbScth  * around a pHCI drivers calls to mdi_pi_online/offline, after holding
8465e3986cbScth  * the pathinfo node via mdi_hold_path/mdi_rele_path, to avoid deadlock
8475e3986cbScth  * with vHCI power management code during path online/offline.  Each
8485e3986cbScth  * mdi_devi_exit_phci must have a matching mdi_devi_enter_phci, and both must
8495e3986cbScth  * occur within the scope of an active mdi_devi_enter that establishes the
8505e3986cbScth  * circular value.
8515e3986cbScth  */
8525e3986cbScth void
8535e3986cbScth mdi_devi_exit_phci(dev_info_t *phci_dip, int circular)
8545e3986cbScth {
8555e3986cbScth 	int		pcircular;
8565e3986cbScth 
8575e3986cbScth 	/* Verify calling context */
8585e3986cbScth 	ASSERT(MDI_PHCI(phci_dip));
8595e3986cbScth 
8605e3986cbScth 	pcircular = (short)(circular & 0xFFFF);
8615e3986cbScth 	ndi_devi_exit(phci_dip, pcircular);
8625e3986cbScth }
8635e3986cbScth 
8645e3986cbScth void
8655e3986cbScth mdi_devi_enter_phci(dev_info_t *phci_dip, int *circular)
8665e3986cbScth {
8675e3986cbScth 	int		pcircular;
8685e3986cbScth 
8695e3986cbScth 	/* Verify calling context */
8705e3986cbScth 	ASSERT(MDI_PHCI(phci_dip));
8715e3986cbScth 
8725e3986cbScth 	ndi_devi_enter(phci_dip, &pcircular);
8735e3986cbScth 
8745e3986cbScth 	/* verify matching mdi_devi_exit_phci/mdi_devi_enter_phci use */
8755e3986cbScth 	ASSERT(pcircular == ((short)(*circular & 0xFFFF)));
8765e3986cbScth }
8775e3986cbScth 
8785e3986cbScth /*
8795e3986cbScth  * mdi_devi_get_vdip():
8805e3986cbScth  *		given a pHCI dip return vHCI dip
8815e3986cbScth  */
8825e3986cbScth dev_info_t *
8835e3986cbScth mdi_devi_get_vdip(dev_info_t *pdip)
8845e3986cbScth {
8855e3986cbScth 	mdi_phci_t	*ph;
8865e3986cbScth 
8875e3986cbScth 	ph = i_devi_get_phci(pdip);
8885e3986cbScth 	if (ph && ph->ph_vhci)
8895e3986cbScth 		return (ph->ph_vhci->vh_dip);
8905e3986cbScth 	return (NULL);
8915e3986cbScth }
8925e3986cbScth 
8935e3986cbScth /*
8945e3986cbScth  * mdi_devi_pdip_entered():
8955e3986cbScth  *		Return 1 if we are vHCI and have done an ndi_devi_enter
8965e3986cbScth  *		of a pHCI
8975e3986cbScth  */
8985e3986cbScth int
8995e3986cbScth mdi_devi_pdip_entered(dev_info_t *vdip)
9005e3986cbScth {
9015e3986cbScth 	mdi_vhci_t	*vh;
9025e3986cbScth 	mdi_phci_t	*ph;
9035e3986cbScth 
9045e3986cbScth 	vh = i_devi_get_vhci(vdip);
9055e3986cbScth 	if (vh == NULL)
9065e3986cbScth 		return (0);
9075e3986cbScth 
9085e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
9095e3986cbScth 	ph = vh->vh_phci_head;
9105e3986cbScth 	while (ph) {
9115e3986cbScth 		if (ph->ph_dip && DEVI_BUSY_OWNED(ph->ph_dip)) {
9125e3986cbScth 			MDI_VHCI_PHCI_UNLOCK(vh);
9135e3986cbScth 			return (1);
9145e3986cbScth 		}
9155e3986cbScth 		ph = ph->ph_next;
9165e3986cbScth 	}
9175e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
9185e3986cbScth 	return (0);
9195e3986cbScth }
9205e3986cbScth 
9215e3986cbScth /*
9227c478bd9Sstevel@tonic-gate  * mdi_phci_path2devinfo():
9237c478bd9Sstevel@tonic-gate  * 		Utility function to search for a valid phci device given
9247c478bd9Sstevel@tonic-gate  *		the devfs pathname.
9257c478bd9Sstevel@tonic-gate  */
9267c478bd9Sstevel@tonic-gate dev_info_t *
9277c478bd9Sstevel@tonic-gate mdi_phci_path2devinfo(dev_info_t *vdip, caddr_t pathname)
9287c478bd9Sstevel@tonic-gate {
9297c478bd9Sstevel@tonic-gate 	char		*temp_pathname;
9307c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
9317c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
9327c478bd9Sstevel@tonic-gate 	dev_info_t 	*pdip = NULL;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	vh = i_devi_get_vhci(vdip);
9357c478bd9Sstevel@tonic-gate 	ASSERT(vh != NULL);
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
9387c478bd9Sstevel@tonic-gate 		/*
9397c478bd9Sstevel@tonic-gate 		 * Invalid vHCI component, return failure
9407c478bd9Sstevel@tonic-gate 		 */
9417c478bd9Sstevel@tonic-gate 		return (NULL);
9427c478bd9Sstevel@tonic-gate 	}
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	temp_pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
9455e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
9467c478bd9Sstevel@tonic-gate 	ph = vh->vh_phci_head;
9477c478bd9Sstevel@tonic-gate 	while (ph != NULL) {
9487c478bd9Sstevel@tonic-gate 		pdip = ph->ph_dip;
9497c478bd9Sstevel@tonic-gate 		ASSERT(pdip != NULL);
9507c478bd9Sstevel@tonic-gate 		*temp_pathname = '\0';
9517c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(pdip, temp_pathname);
9527c478bd9Sstevel@tonic-gate 		if (strcmp(temp_pathname, pathname) == 0) {
9537c478bd9Sstevel@tonic-gate 			break;
9547c478bd9Sstevel@tonic-gate 		}
9557c478bd9Sstevel@tonic-gate 		ph = ph->ph_next;
9567c478bd9Sstevel@tonic-gate 	}
9577c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
9587c478bd9Sstevel@tonic-gate 		pdip = NULL;
9597c478bd9Sstevel@tonic-gate 	}
9605e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
9617c478bd9Sstevel@tonic-gate 	kmem_free(temp_pathname, MAXPATHLEN);
9627c478bd9Sstevel@tonic-gate 	return (pdip);
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate /*
9667c478bd9Sstevel@tonic-gate  * mdi_phci_get_path_count():
9677c478bd9Sstevel@tonic-gate  * 		get number of path information nodes associated with a given
9687c478bd9Sstevel@tonic-gate  *		pHCI device.
9697c478bd9Sstevel@tonic-gate  */
9707c478bd9Sstevel@tonic-gate int
9717c478bd9Sstevel@tonic-gate mdi_phci_get_path_count(dev_info_t *pdip)
9727c478bd9Sstevel@tonic-gate {
9737c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
9747c478bd9Sstevel@tonic-gate 	int		count = 0;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
9777c478bd9Sstevel@tonic-gate 	if (ph != NULL) {
9787c478bd9Sstevel@tonic-gate 		count = ph->ph_path_count;
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 	return (count);
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate /*
9847c478bd9Sstevel@tonic-gate  * i_mdi_phci_lock():
9857c478bd9Sstevel@tonic-gate  *		Lock a pHCI device
9867c478bd9Sstevel@tonic-gate  * Return Values:
9877c478bd9Sstevel@tonic-gate  *		None
9887c478bd9Sstevel@tonic-gate  * Note:
9897c478bd9Sstevel@tonic-gate  *		The default locking order is:
9907c478bd9Sstevel@tonic-gate  *		_NOTE(LOCK_ORDER(mdi_phci::ph_mutex mdi_pathinfo::pi_mutex))
9917c478bd9Sstevel@tonic-gate  *		But there are number of situations where locks need to be
9927c478bd9Sstevel@tonic-gate  *		grabbed in reverse order.  This routine implements try and lock
9937c478bd9Sstevel@tonic-gate  *		mechanism depending on the requested parameter option.
9947c478bd9Sstevel@tonic-gate  */
9957c478bd9Sstevel@tonic-gate static void
9967c478bd9Sstevel@tonic-gate i_mdi_phci_lock(mdi_phci_t *ph, mdi_pathinfo_t *pip)
9977c478bd9Sstevel@tonic-gate {
9987c478bd9Sstevel@tonic-gate 	if (pip) {
9997c478bd9Sstevel@tonic-gate 		/* Reverse locking is requested. */
10007c478bd9Sstevel@tonic-gate 		while (MDI_PHCI_TRYLOCK(ph) == 0) {
10017c478bd9Sstevel@tonic-gate 			/*
10027c478bd9Sstevel@tonic-gate 			 * tryenter failed. Try to grab again
10037c478bd9Sstevel@tonic-gate 			 * after a small delay
10047c478bd9Sstevel@tonic-gate 			 */
10057c478bd9Sstevel@tonic-gate 			MDI_PI_HOLD(pip);
10067c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
10077c478bd9Sstevel@tonic-gate 			delay(1);
10087c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
10097c478bd9Sstevel@tonic-gate 			MDI_PI_RELE(pip);
10107c478bd9Sstevel@tonic-gate 		}
10117c478bd9Sstevel@tonic-gate 	} else {
10127c478bd9Sstevel@tonic-gate 		MDI_PHCI_LOCK(ph);
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate }
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate /*
10177c478bd9Sstevel@tonic-gate  * i_mdi_phci_unlock():
10187c478bd9Sstevel@tonic-gate  *		Unlock the pHCI component
10197c478bd9Sstevel@tonic-gate  */
10207c478bd9Sstevel@tonic-gate static void
10217c478bd9Sstevel@tonic-gate i_mdi_phci_unlock(mdi_phci_t *ph)
10227c478bd9Sstevel@tonic-gate {
10237c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate /*
10277c478bd9Sstevel@tonic-gate  * i_mdi_devinfo_create():
10287c478bd9Sstevel@tonic-gate  *		create client device's devinfo node
10297c478bd9Sstevel@tonic-gate  * Return Values:
10307c478bd9Sstevel@tonic-gate  *		dev_info
10317c478bd9Sstevel@tonic-gate  *		NULL
10327c478bd9Sstevel@tonic-gate  * Notes:
10337c478bd9Sstevel@tonic-gate  */
10347c478bd9Sstevel@tonic-gate static dev_info_t *
10357c478bd9Sstevel@tonic-gate i_mdi_devinfo_create(mdi_vhci_t *vh, char *name, char *guid,
10363c34adc5Sramat 	char **compatible, int ncompatible)
10377c478bd9Sstevel@tonic-gate {
10387c478bd9Sstevel@tonic-gate 	dev_info_t *cdip = NULL;
10397c478bd9Sstevel@tonic-gate 
10405e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	/* Verify for duplicate entry */
10437c478bd9Sstevel@tonic-gate 	cdip = i_mdi_devinfo_find(vh, name, guid);
10447c478bd9Sstevel@tonic-gate 	ASSERT(cdip == NULL);
10457c478bd9Sstevel@tonic-gate 	if (cdip) {
10467c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
10477c478bd9Sstevel@tonic-gate 		    "i_mdi_devinfo_create: client dip %p already exists",
10487c478bd9Sstevel@tonic-gate 			(void *)cdip);
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 
10513c34adc5Sramat 	ndi_devi_alloc_sleep(vh->vh_dip, name, DEVI_SID_NODEID, &cdip);
10527c478bd9Sstevel@tonic-gate 	if (cdip == NULL)
10537c478bd9Sstevel@tonic-gate 		goto fail;
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	/*
10567c478bd9Sstevel@tonic-gate 	 * Create component type and Global unique identifier
10577c478bd9Sstevel@tonic-gate 	 * properties
10587c478bd9Sstevel@tonic-gate 	 */
10597c478bd9Sstevel@tonic-gate 	if (ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10607c478bd9Sstevel@tonic-gate 	    MDI_CLIENT_GUID_PROP, guid) != DDI_PROP_SUCCESS) {
10617c478bd9Sstevel@tonic-gate 		goto fail;
10627c478bd9Sstevel@tonic-gate 	}
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	/* Decorate the node with compatible property */
10657c478bd9Sstevel@tonic-gate 	if (compatible &&
10667c478bd9Sstevel@tonic-gate 	    (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip,
10677c478bd9Sstevel@tonic-gate 	    "compatible", compatible, ncompatible) != DDI_PROP_SUCCESS)) {
10687c478bd9Sstevel@tonic-gate 		goto fail;
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	return (cdip);
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate fail:
10747c478bd9Sstevel@tonic-gate 	if (cdip) {
10757c478bd9Sstevel@tonic-gate 		(void) ndi_prop_remove_all(cdip);
10767c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(cdip);
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 	return (NULL);
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate /*
10827c478bd9Sstevel@tonic-gate  * i_mdi_devinfo_find():
10837c478bd9Sstevel@tonic-gate  *		Find a matching devinfo node for given client node name
10847c478bd9Sstevel@tonic-gate  *		and its guid.
10857c478bd9Sstevel@tonic-gate  * Return Values:
10867c478bd9Sstevel@tonic-gate  *		Handle to a dev_info node or NULL
10877c478bd9Sstevel@tonic-gate  */
10887c478bd9Sstevel@tonic-gate static dev_info_t *
10897c478bd9Sstevel@tonic-gate i_mdi_devinfo_find(mdi_vhci_t *vh, caddr_t name, char *guid)
10907c478bd9Sstevel@tonic-gate {
10917c478bd9Sstevel@tonic-gate 	char			*data;
10927c478bd9Sstevel@tonic-gate 	dev_info_t 		*cdip = NULL;
10937c478bd9Sstevel@tonic-gate 	dev_info_t 		*ndip = NULL;
10947c478bd9Sstevel@tonic-gate 	int			circular;
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	ndi_devi_enter(vh->vh_dip, &circular);
10977c478bd9Sstevel@tonic-gate 	ndip = (dev_info_t *)DEVI(vh->vh_dip)->devi_child;
10987c478bd9Sstevel@tonic-gate 	while ((cdip = ndip) != NULL) {
10997c478bd9Sstevel@tonic-gate 		ndip = (dev_info_t *)DEVI(cdip)->devi_sibling;
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 		if (strcmp(DEVI(cdip)->devi_node_name, name)) {
11027c478bd9Sstevel@tonic-gate 			continue;
11037c478bd9Sstevel@tonic-gate 		}
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, cdip,
11067c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, MDI_CLIENT_GUID_PROP,
11077c478bd9Sstevel@tonic-gate 		    &data) != DDI_PROP_SUCCESS) {
11087c478bd9Sstevel@tonic-gate 			continue;
11097c478bd9Sstevel@tonic-gate 		}
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 		if (strcmp(data, guid) != 0) {
11127c478bd9Sstevel@tonic-gate 			ddi_prop_free(data);
11137c478bd9Sstevel@tonic-gate 			continue;
11147c478bd9Sstevel@tonic-gate 		}
11157c478bd9Sstevel@tonic-gate 		ddi_prop_free(data);
11167c478bd9Sstevel@tonic-gate 		break;
11177c478bd9Sstevel@tonic-gate 	}
11187c478bd9Sstevel@tonic-gate 	ndi_devi_exit(vh->vh_dip, circular);
11197c478bd9Sstevel@tonic-gate 	return (cdip);
11207c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate /*
11237c478bd9Sstevel@tonic-gate  * i_mdi_devinfo_remove():
11247c478bd9Sstevel@tonic-gate  *		Remove a client device node
11257c478bd9Sstevel@tonic-gate  */
11267c478bd9Sstevel@tonic-gate static int
11277c478bd9Sstevel@tonic-gate i_mdi_devinfo_remove(dev_info_t *vdip, dev_info_t *cdip, int flags)
11287c478bd9Sstevel@tonic-gate {
11297c478bd9Sstevel@tonic-gate 	int	rv = MDI_SUCCESS;
11305e3986cbScth 
11317c478bd9Sstevel@tonic-gate 	if (i_mdi_is_child_present(vdip, cdip) == MDI_SUCCESS ||
11327c478bd9Sstevel@tonic-gate 	    (flags & MDI_CLIENT_FLAGS_DEV_NOT_SUPPORTED)) {
11337c478bd9Sstevel@tonic-gate 		rv = ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
11347c478bd9Sstevel@tonic-gate 		if (rv != NDI_SUCCESS) {
11357c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_NOTE, NULL, "!i_mdi_devinfo_remove:"
11365e3986cbScth 			    " failed. cdip = %p\n", (void *)cdip));
11377c478bd9Sstevel@tonic-gate 		}
11387c478bd9Sstevel@tonic-gate 		/*
11397c478bd9Sstevel@tonic-gate 		 * Convert to MDI error code
11407c478bd9Sstevel@tonic-gate 		 */
11417c478bd9Sstevel@tonic-gate 		switch (rv) {
11427c478bd9Sstevel@tonic-gate 		case NDI_SUCCESS:
11437c478bd9Sstevel@tonic-gate 			rv = MDI_SUCCESS;
11447c478bd9Sstevel@tonic-gate 			break;
11457c478bd9Sstevel@tonic-gate 		case NDI_BUSY:
11467c478bd9Sstevel@tonic-gate 			rv = MDI_BUSY;
11477c478bd9Sstevel@tonic-gate 			break;
11487c478bd9Sstevel@tonic-gate 		default:
11497c478bd9Sstevel@tonic-gate 			rv = MDI_FAILURE;
11507c478bd9Sstevel@tonic-gate 			break;
11517c478bd9Sstevel@tonic-gate 		}
11527c478bd9Sstevel@tonic-gate 	}
11537c478bd9Sstevel@tonic-gate 	return (rv);
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate /*
11577c478bd9Sstevel@tonic-gate  * i_devi_get_client()
11587c478bd9Sstevel@tonic-gate  *		Utility function to get mpxio component extensions
11597c478bd9Sstevel@tonic-gate  */
11607c478bd9Sstevel@tonic-gate static mdi_client_t *
11617c478bd9Sstevel@tonic-gate i_devi_get_client(dev_info_t *cdip)
11627c478bd9Sstevel@tonic-gate {
11637c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct = NULL;
11645e3986cbScth 
11657c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(cdip)) {
11667c478bd9Sstevel@tonic-gate 		ct = (mdi_client_t *)DEVI(cdip)->devi_mdi_client;
11677c478bd9Sstevel@tonic-gate 	}
11687c478bd9Sstevel@tonic-gate 	return (ct);
11697c478bd9Sstevel@tonic-gate }
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate /*
11727c478bd9Sstevel@tonic-gate  * i_mdi_is_child_present():
11737c478bd9Sstevel@tonic-gate  *		Search for the presence of client device dev_info node
11747c478bd9Sstevel@tonic-gate  */
11757c478bd9Sstevel@tonic-gate static int
11767c478bd9Sstevel@tonic-gate i_mdi_is_child_present(dev_info_t *vdip, dev_info_t *cdip)
11777c478bd9Sstevel@tonic-gate {
11787c478bd9Sstevel@tonic-gate 	int		rv = MDI_FAILURE;
11797c478bd9Sstevel@tonic-gate 	struct dev_info	*dip;
11807c478bd9Sstevel@tonic-gate 	int		circular;
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	ndi_devi_enter(vdip, &circular);
11837c478bd9Sstevel@tonic-gate 	dip = DEVI(vdip)->devi_child;
11847c478bd9Sstevel@tonic-gate 	while (dip) {
11857c478bd9Sstevel@tonic-gate 		if (dip == DEVI(cdip)) {
11867c478bd9Sstevel@tonic-gate 			rv = MDI_SUCCESS;
11877c478bd9Sstevel@tonic-gate 			break;
11887c478bd9Sstevel@tonic-gate 		}
11897c478bd9Sstevel@tonic-gate 		dip = dip->devi_sibling;
11907c478bd9Sstevel@tonic-gate 	}
11917c478bd9Sstevel@tonic-gate 	ndi_devi_exit(vdip, circular);
11927c478bd9Sstevel@tonic-gate 	return (rv);
11937c478bd9Sstevel@tonic-gate }
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate /*
11977c478bd9Sstevel@tonic-gate  * i_mdi_client_lock():
11987c478bd9Sstevel@tonic-gate  *		Grab client component lock
11997c478bd9Sstevel@tonic-gate  * Return Values:
12007c478bd9Sstevel@tonic-gate  *		None
12017c478bd9Sstevel@tonic-gate  * Note:
12027c478bd9Sstevel@tonic-gate  *		The default locking order is:
12037c478bd9Sstevel@tonic-gate  *		_NOTE(LOCK_ORDER(mdi_client::ct_mutex mdi_pathinfo::pi_mutex))
12047c478bd9Sstevel@tonic-gate  *		But there are number of situations where locks need to be
12057c478bd9Sstevel@tonic-gate  *		grabbed in reverse order.  This routine implements try and lock
12067c478bd9Sstevel@tonic-gate  *		mechanism depending on the requested parameter option.
12077c478bd9Sstevel@tonic-gate  */
12087c478bd9Sstevel@tonic-gate static void
12097c478bd9Sstevel@tonic-gate i_mdi_client_lock(mdi_client_t *ct, mdi_pathinfo_t *pip)
12107c478bd9Sstevel@tonic-gate {
12117c478bd9Sstevel@tonic-gate 	if (pip) {
12127c478bd9Sstevel@tonic-gate 		/*
12137c478bd9Sstevel@tonic-gate 		 * Reverse locking is requested.
12147c478bd9Sstevel@tonic-gate 		 */
12157c478bd9Sstevel@tonic-gate 		while (MDI_CLIENT_TRYLOCK(ct) == 0) {
12167c478bd9Sstevel@tonic-gate 			/*
12177c478bd9Sstevel@tonic-gate 			 * tryenter failed. Try to grab again
12187c478bd9Sstevel@tonic-gate 			 * after a small delay
12197c478bd9Sstevel@tonic-gate 			 */
12207c478bd9Sstevel@tonic-gate 			MDI_PI_HOLD(pip);
12217c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
12227c478bd9Sstevel@tonic-gate 			delay(1);
12237c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
12247c478bd9Sstevel@tonic-gate 			MDI_PI_RELE(pip);
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 	} else {
12277c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
12287c478bd9Sstevel@tonic-gate 	}
12297c478bd9Sstevel@tonic-gate }
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate /*
12327c478bd9Sstevel@tonic-gate  * i_mdi_client_unlock():
12337c478bd9Sstevel@tonic-gate  *		Unlock a client component
12347c478bd9Sstevel@tonic-gate  */
12357c478bd9Sstevel@tonic-gate static void
12367c478bd9Sstevel@tonic-gate i_mdi_client_unlock(mdi_client_t *ct)
12377c478bd9Sstevel@tonic-gate {
12387c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
12397c478bd9Sstevel@tonic-gate }
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate /*
12427c478bd9Sstevel@tonic-gate  * i_mdi_client_alloc():
12437c478bd9Sstevel@tonic-gate  * 		Allocate and initialize a client structure.  Caller should
12445e3986cbScth  *		hold the vhci client lock.
12457c478bd9Sstevel@tonic-gate  * Return Values:
12467c478bd9Sstevel@tonic-gate  *		Handle to a client component
12477c478bd9Sstevel@tonic-gate  */
12487c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12497c478bd9Sstevel@tonic-gate static mdi_client_t *
12503c34adc5Sramat i_mdi_client_alloc(mdi_vhci_t *vh, char *name, char *lguid)
12517c478bd9Sstevel@tonic-gate {
12527c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
12537c478bd9Sstevel@tonic-gate 
12545e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	/*
12577c478bd9Sstevel@tonic-gate 	 * Allocate and initialize a component structure.
12587c478bd9Sstevel@tonic-gate 	 */
12593c34adc5Sramat 	ct = kmem_zalloc(sizeof (*ct), KM_SLEEP);
12607c478bd9Sstevel@tonic-gate 	mutex_init(&ct->ct_mutex, NULL, MUTEX_DEFAULT, NULL);
12617c478bd9Sstevel@tonic-gate 	ct->ct_hnext = NULL;
12627c478bd9Sstevel@tonic-gate 	ct->ct_hprev = NULL;
12637c478bd9Sstevel@tonic-gate 	ct->ct_dip = NULL;
12647c478bd9Sstevel@tonic-gate 	ct->ct_vhci = vh;
12653c34adc5Sramat 	ct->ct_drvname = kmem_alloc(strlen(name) + 1, KM_SLEEP);
12667c478bd9Sstevel@tonic-gate 	(void) strcpy(ct->ct_drvname, name);
12673c34adc5Sramat 	ct->ct_guid = kmem_alloc(strlen(lguid) + 1, KM_SLEEP);
12687c478bd9Sstevel@tonic-gate 	(void) strcpy(ct->ct_guid, lguid);
12697c478bd9Sstevel@tonic-gate 	ct->ct_cprivate = NULL;
12707c478bd9Sstevel@tonic-gate 	ct->ct_vprivate = NULL;
12717c478bd9Sstevel@tonic-gate 	ct->ct_flags = 0;
12727c478bd9Sstevel@tonic-gate 	ct->ct_state = MDI_CLIENT_STATE_FAILED;
12735e3986cbScth 	MDI_CLIENT_LOCK(ct);
12747c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_OFFLINE(ct);
12757c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_DETACH(ct);
12767c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_POWER_UP(ct);
12775e3986cbScth 	MDI_CLIENT_UNLOCK(ct);
12787c478bd9Sstevel@tonic-gate 	ct->ct_failover_flags = 0;
12797c478bd9Sstevel@tonic-gate 	ct->ct_failover_status = 0;
12807c478bd9Sstevel@tonic-gate 	cv_init(&ct->ct_failover_cv, NULL, CV_DRIVER, NULL);
12817c478bd9Sstevel@tonic-gate 	ct->ct_unstable = 0;
12827c478bd9Sstevel@tonic-gate 	cv_init(&ct->ct_unstable_cv, NULL, CV_DRIVER, NULL);
12837c478bd9Sstevel@tonic-gate 	cv_init(&ct->ct_powerchange_cv, NULL, CV_DRIVER, NULL);
12847c478bd9Sstevel@tonic-gate 	ct->ct_lb = vh->vh_lb;
12853c34adc5Sramat 	ct->ct_lb_args =  kmem_zalloc(sizeof (client_lb_args_t), KM_SLEEP);
12867c478bd9Sstevel@tonic-gate 	ct->ct_lb_args->region_size = LOAD_BALANCE_DEFAULT_REGION_SIZE;
12877c478bd9Sstevel@tonic-gate 	ct->ct_path_count = 0;
12887c478bd9Sstevel@tonic-gate 	ct->ct_path_head = NULL;
12897c478bd9Sstevel@tonic-gate 	ct->ct_path_tail = NULL;
12907c478bd9Sstevel@tonic-gate 	ct->ct_path_last = NULL;
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	/*
12937c478bd9Sstevel@tonic-gate 	 * Add this client component to our client hash queue
12947c478bd9Sstevel@tonic-gate 	 */
12957c478bd9Sstevel@tonic-gate 	i_mdi_client_enlist_table(vh, ct);
12967c478bd9Sstevel@tonic-gate 	return (ct);
12977c478bd9Sstevel@tonic-gate }
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate /*
13007c478bd9Sstevel@tonic-gate  * i_mdi_client_enlist_table():
13017c478bd9Sstevel@tonic-gate  *		Attach the client device to the client hash table. Caller
13025e3986cbScth  *		should hold the vhci client lock.
13037c478bd9Sstevel@tonic-gate  */
13047c478bd9Sstevel@tonic-gate static void
13057c478bd9Sstevel@tonic-gate i_mdi_client_enlist_table(mdi_vhci_t *vh, mdi_client_t *ct)
13067c478bd9Sstevel@tonic-gate {
13077c478bd9Sstevel@tonic-gate 	int 			index;
13087c478bd9Sstevel@tonic-gate 	struct client_hash	*head;
13097c478bd9Sstevel@tonic-gate 
13105e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
13115e3986cbScth 
13127c478bd9Sstevel@tonic-gate 	index = i_mdi_get_hash_key(ct->ct_guid);
13137c478bd9Sstevel@tonic-gate 	head = &vh->vh_client_table[index];
13147c478bd9Sstevel@tonic-gate 	ct->ct_hnext = (mdi_client_t *)head->ct_hash_head;
13157c478bd9Sstevel@tonic-gate 	head->ct_hash_head = ct;
13167c478bd9Sstevel@tonic-gate 	head->ct_hash_count++;
13177c478bd9Sstevel@tonic-gate 	vh->vh_client_count++;
13187c478bd9Sstevel@tonic-gate }
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate /*
13217c478bd9Sstevel@tonic-gate  * i_mdi_client_delist_table():
13227c478bd9Sstevel@tonic-gate  *		Attach the client device to the client hash table.
13235e3986cbScth  *		Caller should hold the vhci client lock.
13247c478bd9Sstevel@tonic-gate  */
13257c478bd9Sstevel@tonic-gate static void
13267c478bd9Sstevel@tonic-gate i_mdi_client_delist_table(mdi_vhci_t *vh, mdi_client_t *ct)
13277c478bd9Sstevel@tonic-gate {
13287c478bd9Sstevel@tonic-gate 	int			index;
13297c478bd9Sstevel@tonic-gate 	char			*guid;
13307c478bd9Sstevel@tonic-gate 	struct client_hash 	*head;
13317c478bd9Sstevel@tonic-gate 	mdi_client_t		*next;
13327c478bd9Sstevel@tonic-gate 	mdi_client_t		*last;
13337c478bd9Sstevel@tonic-gate 
13345e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
13355e3986cbScth 
13367c478bd9Sstevel@tonic-gate 	guid = ct->ct_guid;
13377c478bd9Sstevel@tonic-gate 	index = i_mdi_get_hash_key(guid);
13387c478bd9Sstevel@tonic-gate 	head = &vh->vh_client_table[index];
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	last = NULL;
13417c478bd9Sstevel@tonic-gate 	next = (mdi_client_t *)head->ct_hash_head;
13427c478bd9Sstevel@tonic-gate 	while (next != NULL) {
13437c478bd9Sstevel@tonic-gate 		if (next == ct) {
13447c478bd9Sstevel@tonic-gate 			break;
13457c478bd9Sstevel@tonic-gate 		}
13467c478bd9Sstevel@tonic-gate 		last = next;
13477c478bd9Sstevel@tonic-gate 		next = next->ct_hnext;
13487c478bd9Sstevel@tonic-gate 	}
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	if (next) {
13517c478bd9Sstevel@tonic-gate 		head->ct_hash_count--;
13527c478bd9Sstevel@tonic-gate 		if (last == NULL) {
13537c478bd9Sstevel@tonic-gate 			head->ct_hash_head = ct->ct_hnext;
13547c478bd9Sstevel@tonic-gate 		} else {
13557c478bd9Sstevel@tonic-gate 			last->ct_hnext = ct->ct_hnext;
13567c478bd9Sstevel@tonic-gate 		}
13577c478bd9Sstevel@tonic-gate 		ct->ct_hnext = NULL;
13587c478bd9Sstevel@tonic-gate 		vh->vh_client_count--;
13597c478bd9Sstevel@tonic-gate 	}
13607c478bd9Sstevel@tonic-gate }
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate /*
13647c478bd9Sstevel@tonic-gate  * i_mdi_client_free():
13657c478bd9Sstevel@tonic-gate  *		Free a client component
13667c478bd9Sstevel@tonic-gate  */
13677c478bd9Sstevel@tonic-gate static int
13687c478bd9Sstevel@tonic-gate i_mdi_client_free(mdi_vhci_t *vh, mdi_client_t *ct)
13697c478bd9Sstevel@tonic-gate {
13707c478bd9Sstevel@tonic-gate 	int		rv = MDI_SUCCESS;
13717c478bd9Sstevel@tonic-gate 	int		flags = ct->ct_flags;
13727c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
13737c478bd9Sstevel@tonic-gate 	dev_info_t	*vdip;
13747c478bd9Sstevel@tonic-gate 
13755e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
13765e3986cbScth 
13777c478bd9Sstevel@tonic-gate 	vdip = vh->vh_dip;
13787c478bd9Sstevel@tonic-gate 	cdip = ct->ct_dip;
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	(void) ndi_prop_remove(DDI_DEV_T_NONE, cdip, MDI_CLIENT_GUID_PROP);
13817c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_component &= ~MDI_COMPONENT_CLIENT;
13827c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_client = NULL;
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	/*
13857c478bd9Sstevel@tonic-gate 	 * Clear out back ref. to dev_info_t node
13867c478bd9Sstevel@tonic-gate 	 */
13877c478bd9Sstevel@tonic-gate 	ct->ct_dip = NULL;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	/*
13907c478bd9Sstevel@tonic-gate 	 * Remove this client from our hash queue
13917c478bd9Sstevel@tonic-gate 	 */
13927c478bd9Sstevel@tonic-gate 	i_mdi_client_delist_table(vh, ct);
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	/*
13957c478bd9Sstevel@tonic-gate 	 * Uninitialize and free the component
13967c478bd9Sstevel@tonic-gate 	 */
13977c478bd9Sstevel@tonic-gate 	kmem_free(ct->ct_drvname, strlen(ct->ct_drvname) + 1);
13987c478bd9Sstevel@tonic-gate 	kmem_free(ct->ct_guid, strlen(ct->ct_guid) + 1);
13997c478bd9Sstevel@tonic-gate 	kmem_free(ct->ct_lb_args, sizeof (client_lb_args_t));
14007c478bd9Sstevel@tonic-gate 	cv_destroy(&ct->ct_failover_cv);
14017c478bd9Sstevel@tonic-gate 	cv_destroy(&ct->ct_unstable_cv);
14027c478bd9Sstevel@tonic-gate 	cv_destroy(&ct->ct_powerchange_cv);
14037c478bd9Sstevel@tonic-gate 	mutex_destroy(&ct->ct_mutex);
14047c478bd9Sstevel@tonic-gate 	kmem_free(ct, sizeof (*ct));
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	if (cdip != NULL) {
14075e3986cbScth 		MDI_VHCI_CLIENT_UNLOCK(vh);
14087c478bd9Sstevel@tonic-gate 		(void) i_mdi_devinfo_remove(vdip, cdip, flags);
14095e3986cbScth 		MDI_VHCI_CLIENT_LOCK(vh);
14107c478bd9Sstevel@tonic-gate 	}
14117c478bd9Sstevel@tonic-gate 	return (rv);
14127c478bd9Sstevel@tonic-gate }
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate /*
14157c478bd9Sstevel@tonic-gate  * i_mdi_client_find():
14167c478bd9Sstevel@tonic-gate  * 		Find the client structure corresponding to a given guid
14175e3986cbScth  *		Caller should hold the vhci client lock.
14187c478bd9Sstevel@tonic-gate  */
14197c478bd9Sstevel@tonic-gate static mdi_client_t *
14203c34adc5Sramat i_mdi_client_find(mdi_vhci_t *vh, char *cname, char *guid)
14217c478bd9Sstevel@tonic-gate {
14227c478bd9Sstevel@tonic-gate 	int			index;
14237c478bd9Sstevel@tonic-gate 	struct client_hash	*head;
14247c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
14257c478bd9Sstevel@tonic-gate 
14265e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
14275e3986cbScth 
14287c478bd9Sstevel@tonic-gate 	index = i_mdi_get_hash_key(guid);
14297c478bd9Sstevel@tonic-gate 	head = &vh->vh_client_table[index];
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	ct = head->ct_hash_head;
14327c478bd9Sstevel@tonic-gate 	while (ct != NULL) {
14333c34adc5Sramat 		if (strcmp(ct->ct_guid, guid) == 0 &&
14343c34adc5Sramat 		    (cname == NULL || strcmp(ct->ct_drvname, cname) == 0)) {
14357c478bd9Sstevel@tonic-gate 			break;
14367c478bd9Sstevel@tonic-gate 		}
14377c478bd9Sstevel@tonic-gate 		ct = ct->ct_hnext;
14387c478bd9Sstevel@tonic-gate 	}
14397c478bd9Sstevel@tonic-gate 	return (ct);
14407c478bd9Sstevel@tonic-gate }
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate /*
14437c478bd9Sstevel@tonic-gate  * i_mdi_client_update_state():
14447c478bd9Sstevel@tonic-gate  *		Compute and update client device state
14457c478bd9Sstevel@tonic-gate  * Notes:
14467c478bd9Sstevel@tonic-gate  *		A client device can be in any of three possible states:
14477c478bd9Sstevel@tonic-gate  *
14487c478bd9Sstevel@tonic-gate  *		MDI_CLIENT_STATE_OPTIMAL - Client in optimal state with more
14497c478bd9Sstevel@tonic-gate  *		one online/standby paths. Can tolerate failures.
14507c478bd9Sstevel@tonic-gate  *		MDI_CLIENT_STATE_DEGRADED - Client device in degraded state with
14517c478bd9Sstevel@tonic-gate  *		no alternate paths available as standby. A failure on the online
14527c478bd9Sstevel@tonic-gate  *		would result in loss of access to device data.
14537c478bd9Sstevel@tonic-gate  *		MDI_CLIENT_STATE_FAILED - Client device in failed state with
14547c478bd9Sstevel@tonic-gate  *		no paths available to access the device.
14557c478bd9Sstevel@tonic-gate  */
14567c478bd9Sstevel@tonic-gate static void
14577c478bd9Sstevel@tonic-gate i_mdi_client_update_state(mdi_client_t *ct)
14587c478bd9Sstevel@tonic-gate {
14597c478bd9Sstevel@tonic-gate 	int state;
14605e3986cbScth 
14615e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
14627c478bd9Sstevel@tonic-gate 	state = i_mdi_client_compute_state(ct, NULL);
14637c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_STATE(ct, state);
14647c478bd9Sstevel@tonic-gate }
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate /*
14677c478bd9Sstevel@tonic-gate  * i_mdi_client_compute_state():
14687c478bd9Sstevel@tonic-gate  *		Compute client device state
14697c478bd9Sstevel@tonic-gate  *
14707c478bd9Sstevel@tonic-gate  *		mdi_phci_t *	Pointer to pHCI structure which should
14717c478bd9Sstevel@tonic-gate  *				while computing the new value.  Used by
14727c478bd9Sstevel@tonic-gate  *				i_mdi_phci_offline() to find the new
14737c478bd9Sstevel@tonic-gate  *				client state after DR of a pHCI.
14747c478bd9Sstevel@tonic-gate  */
14757c478bd9Sstevel@tonic-gate static int
14767c478bd9Sstevel@tonic-gate i_mdi_client_compute_state(mdi_client_t *ct, mdi_phci_t *ph)
14777c478bd9Sstevel@tonic-gate {
14787c478bd9Sstevel@tonic-gate 	int		state;
14797c478bd9Sstevel@tonic-gate 	int		online_count = 0;
14807c478bd9Sstevel@tonic-gate 	int		standby_count = 0;
14817c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip, *next;
14827c478bd9Sstevel@tonic-gate 
14835e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
14847c478bd9Sstevel@tonic-gate 	pip = ct->ct_path_head;
14857c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
14867c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
14877c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
14887c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_phci == ph) {
14897c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
14907c478bd9Sstevel@tonic-gate 			pip = next;
14917c478bd9Sstevel@tonic-gate 			continue;
14927c478bd9Sstevel@tonic-gate 		}
14935e3986cbScth 
14947c478bd9Sstevel@tonic-gate 		if ((MDI_PI(pip)->pi_state & MDI_PATHINFO_STATE_MASK)
14957c478bd9Sstevel@tonic-gate 				== MDI_PATHINFO_STATE_ONLINE)
14967c478bd9Sstevel@tonic-gate 			online_count++;
14977c478bd9Sstevel@tonic-gate 		else if ((MDI_PI(pip)->pi_state & MDI_PATHINFO_STATE_MASK)
14987c478bd9Sstevel@tonic-gate 				== MDI_PATHINFO_STATE_STANDBY)
14997c478bd9Sstevel@tonic-gate 			standby_count++;
15007c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
15017c478bd9Sstevel@tonic-gate 		pip = next;
15027c478bd9Sstevel@tonic-gate 	}
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	if (online_count == 0) {
15057c478bd9Sstevel@tonic-gate 		if (standby_count == 0) {
15067c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_FAILED;
15077c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_NOTE, NULL, "!client state: failed"
15085e3986cbScth 			    " ct = %p\n", (void *)ct));
15097c478bd9Sstevel@tonic-gate 		} else if (standby_count == 1) {
15107c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_DEGRADED;
15117c478bd9Sstevel@tonic-gate 		} else {
15127c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_OPTIMAL;
15137c478bd9Sstevel@tonic-gate 		}
15147c478bd9Sstevel@tonic-gate 	} else if (online_count == 1) {
15157c478bd9Sstevel@tonic-gate 		if (standby_count == 0) {
15167c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_DEGRADED;
15177c478bd9Sstevel@tonic-gate 		} else {
15187c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_OPTIMAL;
15197c478bd9Sstevel@tonic-gate 		}
15207c478bd9Sstevel@tonic-gate 	} else {
15217c478bd9Sstevel@tonic-gate 		state = MDI_CLIENT_STATE_OPTIMAL;
15227c478bd9Sstevel@tonic-gate 	}
15237c478bd9Sstevel@tonic-gate 	return (state);
15247c478bd9Sstevel@tonic-gate }
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate /*
15277c478bd9Sstevel@tonic-gate  * i_mdi_client2devinfo():
15287c478bd9Sstevel@tonic-gate  *		Utility function
15297c478bd9Sstevel@tonic-gate  */
15307c478bd9Sstevel@tonic-gate dev_info_t *
15317c478bd9Sstevel@tonic-gate i_mdi_client2devinfo(mdi_client_t *ct)
15327c478bd9Sstevel@tonic-gate {
15337c478bd9Sstevel@tonic-gate 	return (ct->ct_dip);
15347c478bd9Sstevel@tonic-gate }
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate /*
15377c478bd9Sstevel@tonic-gate  * mdi_client_path2_devinfo():
15387c478bd9Sstevel@tonic-gate  * 		Given the parent devinfo and child devfs pathname, search for
15397c478bd9Sstevel@tonic-gate  *		a valid devfs node handle.
15407c478bd9Sstevel@tonic-gate  */
15417c478bd9Sstevel@tonic-gate dev_info_t *
15427c478bd9Sstevel@tonic-gate mdi_client_path2devinfo(dev_info_t *vdip, char *pathname)
15437c478bd9Sstevel@tonic-gate {
15447c478bd9Sstevel@tonic-gate 	dev_info_t 	*cdip = NULL;
15457c478bd9Sstevel@tonic-gate 	dev_info_t 	*ndip = NULL;
15467c478bd9Sstevel@tonic-gate 	char		*temp_pathname;
15477c478bd9Sstevel@tonic-gate 	int		circular;
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	/*
15507c478bd9Sstevel@tonic-gate 	 * Allocate temp buffer
15517c478bd9Sstevel@tonic-gate 	 */
15527c478bd9Sstevel@tonic-gate 	temp_pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	/*
15557c478bd9Sstevel@tonic-gate 	 * Lock parent against changes
15567c478bd9Sstevel@tonic-gate 	 */
15577c478bd9Sstevel@tonic-gate 	ndi_devi_enter(vdip, &circular);
15587c478bd9Sstevel@tonic-gate 	ndip = (dev_info_t *)DEVI(vdip)->devi_child;
15597c478bd9Sstevel@tonic-gate 	while ((cdip = ndip) != NULL) {
15607c478bd9Sstevel@tonic-gate 		ndip = (dev_info_t *)DEVI(cdip)->devi_sibling;
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 		*temp_pathname = '\0';
15637c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(cdip, temp_pathname);
15647c478bd9Sstevel@tonic-gate 		if (strcmp(temp_pathname, pathname) == 0) {
15657c478bd9Sstevel@tonic-gate 			break;
15667c478bd9Sstevel@tonic-gate 		}
15677c478bd9Sstevel@tonic-gate 	}
15687c478bd9Sstevel@tonic-gate 	/*
15697c478bd9Sstevel@tonic-gate 	 * Release devinfo lock
15707c478bd9Sstevel@tonic-gate 	 */
15717c478bd9Sstevel@tonic-gate 	ndi_devi_exit(vdip, circular);
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	/*
15747c478bd9Sstevel@tonic-gate 	 * Free the temp buffer
15757c478bd9Sstevel@tonic-gate 	 */
15767c478bd9Sstevel@tonic-gate 	kmem_free(temp_pathname, MAXPATHLEN);
15777c478bd9Sstevel@tonic-gate 	return (cdip);
15787c478bd9Sstevel@tonic-gate }
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate /*
15817c478bd9Sstevel@tonic-gate  * mdi_client_get_path_count():
15827c478bd9Sstevel@tonic-gate  * 		Utility function to get number of path information nodes
15837c478bd9Sstevel@tonic-gate  *		associated with a given client device.
15847c478bd9Sstevel@tonic-gate  */
15857c478bd9Sstevel@tonic-gate int
15867c478bd9Sstevel@tonic-gate mdi_client_get_path_count(dev_info_t *cdip)
15877c478bd9Sstevel@tonic-gate {
15887c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
15897c478bd9Sstevel@tonic-gate 	int		count = 0;
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
15927c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
15937c478bd9Sstevel@tonic-gate 		count = ct->ct_path_count;
15947c478bd9Sstevel@tonic-gate 	}
15957c478bd9Sstevel@tonic-gate 	return (count);
15967c478bd9Sstevel@tonic-gate }
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate /*
16007c478bd9Sstevel@tonic-gate  * i_mdi_get_hash_key():
16017c478bd9Sstevel@tonic-gate  * 		Create a hash using strings as keys
16027c478bd9Sstevel@tonic-gate  *
16037c478bd9Sstevel@tonic-gate  */
16047c478bd9Sstevel@tonic-gate static int
16057c478bd9Sstevel@tonic-gate i_mdi_get_hash_key(char *str)
16067c478bd9Sstevel@tonic-gate {
16077c478bd9Sstevel@tonic-gate 	uint32_t	g, hash = 0;
16087c478bd9Sstevel@tonic-gate 	char		*p;
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	for (p = str; *p != '\0'; p++) {
16117c478bd9Sstevel@tonic-gate 		g = *p;
16127c478bd9Sstevel@tonic-gate 		hash += g;
16137c478bd9Sstevel@tonic-gate 	}
16147c478bd9Sstevel@tonic-gate 	return (hash % (CLIENT_HASH_TABLE_SIZE - 1));
16157c478bd9Sstevel@tonic-gate }
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate /*
16187c478bd9Sstevel@tonic-gate  * mdi_get_lb_policy():
16197c478bd9Sstevel@tonic-gate  * 		Get current load balancing policy for a given client device
16207c478bd9Sstevel@tonic-gate  */
16217c478bd9Sstevel@tonic-gate client_lb_t
16227c478bd9Sstevel@tonic-gate mdi_get_lb_policy(dev_info_t *cdip)
16237c478bd9Sstevel@tonic-gate {
16247c478bd9Sstevel@tonic-gate 	client_lb_t	lb = LOAD_BALANCE_NONE;
16257c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
16287c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
16297c478bd9Sstevel@tonic-gate 		lb = ct->ct_lb;
16307c478bd9Sstevel@tonic-gate 	}
16317c478bd9Sstevel@tonic-gate 	return (lb);
16327c478bd9Sstevel@tonic-gate }
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate /*
16357c478bd9Sstevel@tonic-gate  * mdi_set_lb_region_size():
16367c478bd9Sstevel@tonic-gate  * 		Set current region size for the load-balance
16377c478bd9Sstevel@tonic-gate  */
16387c478bd9Sstevel@tonic-gate int
16397c478bd9Sstevel@tonic-gate mdi_set_lb_region_size(dev_info_t *cdip, int region_size)
16407c478bd9Sstevel@tonic-gate {
16417c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
16427c478bd9Sstevel@tonic-gate 	int		rv = MDI_FAILURE;
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
16457c478bd9Sstevel@tonic-gate 	if (ct != NULL && ct->ct_lb_args != NULL) {
16467c478bd9Sstevel@tonic-gate 		ct->ct_lb_args->region_size = region_size;
16477c478bd9Sstevel@tonic-gate 		rv = MDI_SUCCESS;
16487c478bd9Sstevel@tonic-gate 	}
16497c478bd9Sstevel@tonic-gate 	return (rv);
16507c478bd9Sstevel@tonic-gate }
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate /*
16537c478bd9Sstevel@tonic-gate  * mdi_Set_lb_policy():
16547c478bd9Sstevel@tonic-gate  * 		Set current load balancing policy for a given client device
16557c478bd9Sstevel@tonic-gate  */
16567c478bd9Sstevel@tonic-gate int
16577c478bd9Sstevel@tonic-gate mdi_set_lb_policy(dev_info_t *cdip, client_lb_t lb)
16587c478bd9Sstevel@tonic-gate {
16597c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
16607c478bd9Sstevel@tonic-gate 	int		rv = MDI_FAILURE;
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
16637c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
16647c478bd9Sstevel@tonic-gate 		ct->ct_lb = lb;
16657c478bd9Sstevel@tonic-gate 		rv = MDI_SUCCESS;
16667c478bd9Sstevel@tonic-gate 	}
16677c478bd9Sstevel@tonic-gate 	return (rv);
16687c478bd9Sstevel@tonic-gate }
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate /*
16717c478bd9Sstevel@tonic-gate  * mdi_failover():
16727c478bd9Sstevel@tonic-gate  *		failover function called by the vHCI drivers to initiate
16737c478bd9Sstevel@tonic-gate  *		a failover operation.  This is typically due to non-availability
16747c478bd9Sstevel@tonic-gate  *		of online paths to route I/O requests.  Failover can be
16757c478bd9Sstevel@tonic-gate  *		triggered through user application also.
16767c478bd9Sstevel@tonic-gate  *
16777c478bd9Sstevel@tonic-gate  *		The vHCI driver calls mdi_failover() to initiate a failover
16787c478bd9Sstevel@tonic-gate  *		operation. mdi_failover() calls back into the vHCI driver's
16797c478bd9Sstevel@tonic-gate  *		vo_failover() entry point to perform the actual failover
16807c478bd9Sstevel@tonic-gate  *		operation.  The reason for requiring the vHCI driver to
16817c478bd9Sstevel@tonic-gate  *		initiate failover by calling mdi_failover(), instead of directly
16827c478bd9Sstevel@tonic-gate  *		executing vo_failover() itself, is to ensure that the mdi
16837c478bd9Sstevel@tonic-gate  *		framework can keep track of the client state properly.
16847c478bd9Sstevel@tonic-gate  *		Additionally, mdi_failover() provides as a convenience the
16857c478bd9Sstevel@tonic-gate  *		option of performing the failover operation synchronously or
16867c478bd9Sstevel@tonic-gate  *		asynchronously
16877c478bd9Sstevel@tonic-gate  *
16887c478bd9Sstevel@tonic-gate  *		Upon successful completion of the failover operation, the
16897c478bd9Sstevel@tonic-gate  *		paths that were previously ONLINE will be in the STANDBY state,
16907c478bd9Sstevel@tonic-gate  *		and the newly activated paths will be in the ONLINE state.
16917c478bd9Sstevel@tonic-gate  *
16927c478bd9Sstevel@tonic-gate  *		The flags modifier determines whether the activation is done
16937c478bd9Sstevel@tonic-gate  *		synchronously: MDI_FAILOVER_SYNC
16947c478bd9Sstevel@tonic-gate  * Return Values:
16957c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
16967c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
16977c478bd9Sstevel@tonic-gate  *		MDI_BUSY
16987c478bd9Sstevel@tonic-gate  */
16997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17007c478bd9Sstevel@tonic-gate int
17017c478bd9Sstevel@tonic-gate mdi_failover(dev_info_t *vdip, dev_info_t *cdip, int flags)
17027c478bd9Sstevel@tonic-gate {
17037c478bd9Sstevel@tonic-gate 	int			rv;
17047c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
17077c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
17087c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
17097c478bd9Sstevel@tonic-gate 		/* cdip is not a valid client device. Nothing more to do. */
17107c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
17117c478bd9Sstevel@tonic-gate 	}
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_PATH_FREE_IN_PROGRESS(ct)) {
17167c478bd9Sstevel@tonic-gate 		/* A path to the client is being freed */
17177c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
17187c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
17197c478bd9Sstevel@tonic-gate 	}
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_FAILED(ct)) {
17237c478bd9Sstevel@tonic-gate 		/*
17247c478bd9Sstevel@tonic-gate 		 * Client is in failed state. Nothing more to do.
17257c478bd9Sstevel@tonic-gate 		 */
17267c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
17277c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
17317c478bd9Sstevel@tonic-gate 		/*
17327c478bd9Sstevel@tonic-gate 		 * Failover is already in progress; return BUSY
17337c478bd9Sstevel@tonic-gate 		 */
17347c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
17357c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
17367c478bd9Sstevel@tonic-gate 	}
17377c478bd9Sstevel@tonic-gate 	/*
17387c478bd9Sstevel@tonic-gate 	 * Make sure that mdi_pathinfo node state changes are processed.
17397c478bd9Sstevel@tonic-gate 	 * We do not allow failovers to progress while client path state
17407c478bd9Sstevel@tonic-gate 	 * changes are in progress
17417c478bd9Sstevel@tonic-gate 	 */
17427c478bd9Sstevel@tonic-gate 	if (ct->ct_unstable) {
17437c478bd9Sstevel@tonic-gate 		if (flags == MDI_FAILOVER_ASYNC) {
17447c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
17457c478bd9Sstevel@tonic-gate 			return (MDI_BUSY);
17467c478bd9Sstevel@tonic-gate 		} else {
17477c478bd9Sstevel@tonic-gate 			while (ct->ct_unstable)
17487c478bd9Sstevel@tonic-gate 				cv_wait(&ct->ct_unstable_cv, &ct->ct_mutex);
17497c478bd9Sstevel@tonic-gate 		}
17507c478bd9Sstevel@tonic-gate 	}
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	/*
17537c478bd9Sstevel@tonic-gate 	 * Client device is in stable state. Before proceeding, perform sanity
17547c478bd9Sstevel@tonic-gate 	 * checks again.
17557c478bd9Sstevel@tonic-gate 	 */
17567c478bd9Sstevel@tonic-gate 	if ((MDI_CLIENT_IS_DETACHED(ct)) || (MDI_CLIENT_IS_FAILED(ct)) ||
1757737d277aScth 	    (!i_ddi_devi_attached(ct->ct_dip))) {
17587c478bd9Sstevel@tonic-gate 		/*
17597c478bd9Sstevel@tonic-gate 		 * Client is in failed state. Nothing more to do.
17607c478bd9Sstevel@tonic-gate 		 */
17617c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
17627c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
17637c478bd9Sstevel@tonic-gate 	}
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	/*
17667c478bd9Sstevel@tonic-gate 	 * Set the client state as failover in progress.
17677c478bd9Sstevel@tonic-gate 	 */
17687c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_FAILOVER_IN_PROGRESS(ct);
17697c478bd9Sstevel@tonic-gate 	ct->ct_failover_flags = flags;
17707c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 	if (flags == MDI_FAILOVER_ASYNC) {
17737c478bd9Sstevel@tonic-gate 		/*
17747c478bd9Sstevel@tonic-gate 		 * Submit the initiate failover request via CPR safe
17757c478bd9Sstevel@tonic-gate 		 * taskq threads.
17767c478bd9Sstevel@tonic-gate 		 */
17777c478bd9Sstevel@tonic-gate 		(void) taskq_dispatch(mdi_taskq, (task_func_t *)i_mdi_failover,
17787c478bd9Sstevel@tonic-gate 		    ct, KM_SLEEP);
17797c478bd9Sstevel@tonic-gate 		return (MDI_ACCEPT);
17807c478bd9Sstevel@tonic-gate 	} else {
17817c478bd9Sstevel@tonic-gate 		/*
17827c478bd9Sstevel@tonic-gate 		 * Synchronous failover mode.  Typically invoked from the user
17837c478bd9Sstevel@tonic-gate 		 * land.
17847c478bd9Sstevel@tonic-gate 		 */
17857c478bd9Sstevel@tonic-gate 		rv = i_mdi_failover(ct);
17867c478bd9Sstevel@tonic-gate 	}
17877c478bd9Sstevel@tonic-gate 	return (rv);
17887c478bd9Sstevel@tonic-gate }
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate /*
17917c478bd9Sstevel@tonic-gate  * i_mdi_failover():
17927c478bd9Sstevel@tonic-gate  *		internal failover function. Invokes vHCI drivers failover
17937c478bd9Sstevel@tonic-gate  *		callback function and process the failover status
17947c478bd9Sstevel@tonic-gate  * Return Values:
17957c478bd9Sstevel@tonic-gate  *		None
17967c478bd9Sstevel@tonic-gate  *
17977c478bd9Sstevel@tonic-gate  * Note: A client device in failover state can not be detached or freed.
17987c478bd9Sstevel@tonic-gate  */
17997c478bd9Sstevel@tonic-gate static int
18007c478bd9Sstevel@tonic-gate i_mdi_failover(void *arg)
18017c478bd9Sstevel@tonic-gate {
18027c478bd9Sstevel@tonic-gate 	int		rv = MDI_SUCCESS;
18037c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct = (mdi_client_t *)arg;
18047c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = ct->ct_vhci;
18057c478bd9Sstevel@tonic-gate 
18065e3986cbScth 	ASSERT(!MDI_CLIENT_LOCKED(ct));
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	if (vh->vh_ops->vo_failover != NULL) {
18097c478bd9Sstevel@tonic-gate 		/*
18107c478bd9Sstevel@tonic-gate 		 * Call vHCI drivers callback routine
18117c478bd9Sstevel@tonic-gate 		 */
18127c478bd9Sstevel@tonic-gate 		rv = (*vh->vh_ops->vo_failover)(vh->vh_dip, ct->ct_dip,
18137c478bd9Sstevel@tonic-gate 		    ct->ct_failover_flags);
18147c478bd9Sstevel@tonic-gate 	}
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
18177c478bd9Sstevel@tonic-gate 	MDI_CLIENT_CLEAR_FAILOVER_IN_PROGRESS(ct);
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	/*
18207c478bd9Sstevel@tonic-gate 	 * Save the failover return status
18217c478bd9Sstevel@tonic-gate 	 */
18227c478bd9Sstevel@tonic-gate 	ct->ct_failover_status = rv;
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	/*
18257c478bd9Sstevel@tonic-gate 	 * As a result of failover, client status would have been changed.
18267c478bd9Sstevel@tonic-gate 	 * Update the client state and wake up anyone waiting on this client
18277c478bd9Sstevel@tonic-gate 	 * device.
18287c478bd9Sstevel@tonic-gate 	 */
18297c478bd9Sstevel@tonic-gate 	i_mdi_client_update_state(ct);
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	cv_broadcast(&ct->ct_failover_cv);
18327c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
18337c478bd9Sstevel@tonic-gate 	return (rv);
18347c478bd9Sstevel@tonic-gate }
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate /*
18377c478bd9Sstevel@tonic-gate  * Load balancing is logical block.
18387c478bd9Sstevel@tonic-gate  * IOs within the range described by region_size
18397c478bd9Sstevel@tonic-gate  * would go on the same path. This would improve the
18407c478bd9Sstevel@tonic-gate  * performance by cache-hit on some of the RAID devices.
18417c478bd9Sstevel@tonic-gate  * Search only for online paths(At some point we
18427c478bd9Sstevel@tonic-gate  * may want to balance across target ports).
18437c478bd9Sstevel@tonic-gate  * If no paths are found then default to round-robin.
18447c478bd9Sstevel@tonic-gate  */
18457c478bd9Sstevel@tonic-gate static int
18467c478bd9Sstevel@tonic-gate i_mdi_lba_lb(mdi_client_t *ct, mdi_pathinfo_t **ret_pip, struct buf *bp)
18477c478bd9Sstevel@tonic-gate {
18487c478bd9Sstevel@tonic-gate 	int		path_index = -1;
18497c478bd9Sstevel@tonic-gate 	int		online_path_count = 0;
18507c478bd9Sstevel@tonic-gate 	int		online_nonpref_path_count = 0;
18517c478bd9Sstevel@tonic-gate 	int 		region_size = ct->ct_lb_args->region_size;
18527c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
18537c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
18547c478bd9Sstevel@tonic-gate 	int		preferred, path_cnt;
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 	pip = ct->ct_path_head;
18577c478bd9Sstevel@tonic-gate 	while (pip) {
18587c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
18597c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_state ==
18607c478bd9Sstevel@tonic-gate 		    MDI_PATHINFO_STATE_ONLINE && MDI_PI(pip)->pi_preferred) {
18617c478bd9Sstevel@tonic-gate 			online_path_count++;
18627c478bd9Sstevel@tonic-gate 		} else if (MDI_PI(pip)->pi_state ==
18637c478bd9Sstevel@tonic-gate 		    MDI_PATHINFO_STATE_ONLINE && !MDI_PI(pip)->pi_preferred) {
18647c478bd9Sstevel@tonic-gate 			online_nonpref_path_count++;
18657c478bd9Sstevel@tonic-gate 		}
18667c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)
18677c478bd9Sstevel@tonic-gate 		    MDI_PI(pip)->pi_client_link;
18687c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
18697c478bd9Sstevel@tonic-gate 		pip = next;
18707c478bd9Sstevel@tonic-gate 	}
18717c478bd9Sstevel@tonic-gate 	/* if found any online/preferred then use this type */
18727c478bd9Sstevel@tonic-gate 	if (online_path_count > 0) {
18737c478bd9Sstevel@tonic-gate 		path_cnt = online_path_count;
18747c478bd9Sstevel@tonic-gate 		preferred = 1;
18757c478bd9Sstevel@tonic-gate 	} else if (online_nonpref_path_count > 0) {
18767c478bd9Sstevel@tonic-gate 		path_cnt = online_nonpref_path_count;
18777c478bd9Sstevel@tonic-gate 		preferred = 0;
18787c478bd9Sstevel@tonic-gate 	} else {
18797c478bd9Sstevel@tonic-gate 		path_cnt = 0;
18807c478bd9Sstevel@tonic-gate 	}
18817c478bd9Sstevel@tonic-gate 	if (path_cnt) {
18827c478bd9Sstevel@tonic-gate 		path_index = (bp->b_blkno >> region_size) % path_cnt;
18837c478bd9Sstevel@tonic-gate 		pip = ct->ct_path_head;
18847c478bd9Sstevel@tonic-gate 		while (pip && path_index != -1) {
18857c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
18867c478bd9Sstevel@tonic-gate 			if (path_index == 0 &&
18877c478bd9Sstevel@tonic-gate 			    (MDI_PI(pip)->pi_state ==
18887c478bd9Sstevel@tonic-gate 			    MDI_PATHINFO_STATE_ONLINE) &&
18897c478bd9Sstevel@tonic-gate 				MDI_PI(pip)->pi_preferred == preferred) {
18907c478bd9Sstevel@tonic-gate 				MDI_PI_HOLD(pip);
18917c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
18927c478bd9Sstevel@tonic-gate 				*ret_pip = pip;
18937c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
18947c478bd9Sstevel@tonic-gate 			}
18957c478bd9Sstevel@tonic-gate 			path_index --;
18967c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)
18977c478bd9Sstevel@tonic-gate 			    MDI_PI(pip)->pi_client_link;
18987c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
18997c478bd9Sstevel@tonic-gate 			pip = next;
19007c478bd9Sstevel@tonic-gate 		}
19017c478bd9Sstevel@tonic-gate 		if (pip == NULL) {
19027c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, NULL,
19035e3986cbScth 			    "!lba %llx, no pip !!\n",
19045e3986cbScth 				bp->b_lblkno));
19057c478bd9Sstevel@tonic-gate 		} else {
19067c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, NULL,
19075e3986cbScth 			    "!lba %llx, no pip for path_index, "
19085e3986cbScth 			    "pip %p\n", bp->b_lblkno, (void *)pip));
19097c478bd9Sstevel@tonic-gate 		}
19107c478bd9Sstevel@tonic-gate 	}
19117c478bd9Sstevel@tonic-gate 	return (MDI_FAILURE);
19127c478bd9Sstevel@tonic-gate }
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate /*
19157c478bd9Sstevel@tonic-gate  * mdi_select_path():
19167c478bd9Sstevel@tonic-gate  *		select a path to access a client device.
19177c478bd9Sstevel@tonic-gate  *
19187c478bd9Sstevel@tonic-gate  *		mdi_select_path() function is called by the vHCI drivers to
19197c478bd9Sstevel@tonic-gate  *		select a path to route the I/O request to.  The caller passes
19207c478bd9Sstevel@tonic-gate  *		the block I/O data transfer structure ("buf") as one of the
19217c478bd9Sstevel@tonic-gate  *		parameters.  The mpxio framework uses the buf structure
19227c478bd9Sstevel@tonic-gate  *		contents to maintain per path statistics (total I/O size /
19237c478bd9Sstevel@tonic-gate  *		count pending).  If more than one online paths are available to
19247c478bd9Sstevel@tonic-gate  *		select, the framework automatically selects a suitable path
19257c478bd9Sstevel@tonic-gate  *		for routing I/O request. If a failover operation is active for
19267c478bd9Sstevel@tonic-gate  *		this client device the call shall be failed with MDI_BUSY error
19277c478bd9Sstevel@tonic-gate  *		code.
19287c478bd9Sstevel@tonic-gate  *
19297c478bd9Sstevel@tonic-gate  *		By default this function returns a suitable path in online
19307c478bd9Sstevel@tonic-gate  *		state based on the current load balancing policy.  Currently
19317c478bd9Sstevel@tonic-gate  *		we support LOAD_BALANCE_NONE (Previously selected online path
19327c478bd9Sstevel@tonic-gate  *		will continue to be used till the path is usable) and
19337c478bd9Sstevel@tonic-gate  *		LOAD_BALANCE_RR (Online paths will be selected in a round
19347c478bd9Sstevel@tonic-gate  *		robin fashion), LOAD_BALANCE_LB(Online paths will be selected
19357c478bd9Sstevel@tonic-gate  *		based on the logical block).  The load balancing
19367c478bd9Sstevel@tonic-gate  *		through vHCI drivers configuration file (driver.conf).
19377c478bd9Sstevel@tonic-gate  *
19387c478bd9Sstevel@tonic-gate  *		vHCI drivers may override this default behavior by specifying
1939602ca9eaScth  *		appropriate flags.  The meaning of the thrid argument depends
1940602ca9eaScth  *		on the flags specified. If MDI_SELECT_PATH_INSTANCE is set
1941602ca9eaScth  *		then the argument is the "path instance" of the path to select.
1942602ca9eaScth  *		If MDI_SELECT_PATH_INSTANCE is not set then the argument is
1943602ca9eaScth  *		"start_pip". A non NULL "start_pip" is the starting point to
1944602ca9eaScth  *		walk and find the next appropriate path.  The following values
1945602ca9eaScth  *		are currently defined: MDI_SELECT_ONLINE_PATH (to select an
1946602ca9eaScth  *		ONLINE path) and/or MDI_SELECT_STANDBY_PATH (to select an
1947602ca9eaScth  *		STANDBY path).
19487c478bd9Sstevel@tonic-gate  *
19497c478bd9Sstevel@tonic-gate  *		The non-standard behavior is used by the scsi_vhci driver,
19507c478bd9Sstevel@tonic-gate  *		whenever it has to use a STANDBY/FAULTED path.  Eg. during
19517c478bd9Sstevel@tonic-gate  *		attach of client devices (to avoid an unnecessary failover
19527c478bd9Sstevel@tonic-gate  *		when the STANDBY path comes up first), during failover
19537c478bd9Sstevel@tonic-gate  *		(to activate a STANDBY path as ONLINE).
19547c478bd9Sstevel@tonic-gate  *
19555e3986cbScth  *		The selected path is returned in a a mdi_hold_path() state
19565e3986cbScth  *		(pi_ref_cnt). Caller should release the hold by calling
19575e3986cbScth  *		mdi_rele_path().
19587c478bd9Sstevel@tonic-gate  *
19597c478bd9Sstevel@tonic-gate  * Return Values:
19607c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS	- Completed successfully
19617c478bd9Sstevel@tonic-gate  *		MDI_BUSY 	- Client device is busy failing over
19627c478bd9Sstevel@tonic-gate  *		MDI_NOPATH	- Client device is online, but no valid path are
19637c478bd9Sstevel@tonic-gate  *				  available to access this client device
19647c478bd9Sstevel@tonic-gate  *		MDI_FAILURE	- Invalid client device or state
19657c478bd9Sstevel@tonic-gate  *		MDI_DEVI_ONLINING
19667c478bd9Sstevel@tonic-gate  *				- Client device (struct dev_info state) is in
19677c478bd9Sstevel@tonic-gate  *				  onlining state.
19687c478bd9Sstevel@tonic-gate  */
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19717c478bd9Sstevel@tonic-gate int
19727c478bd9Sstevel@tonic-gate mdi_select_path(dev_info_t *cdip, struct buf *bp, int flags,
1973602ca9eaScth     void *arg, mdi_pathinfo_t **ret_pip)
19747c478bd9Sstevel@tonic-gate {
19757c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
19767c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
19777c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
19787c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*head;
19797c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*start;
19807c478bd9Sstevel@tonic-gate 	client_lb_t	lbp;	/* load balancing policy */
19817c478bd9Sstevel@tonic-gate 	int		sb = 1;	/* standard behavior */
19827c478bd9Sstevel@tonic-gate 	int		preferred = 1;	/* preferred path */
19837c478bd9Sstevel@tonic-gate 	int		cond, cont = 1;
19847c478bd9Sstevel@tonic-gate 	int		retry = 0;
1985602ca9eaScth 	mdi_pathinfo_t	*start_pip;	/* request starting pathinfo */
1986602ca9eaScth 	int		path_instance;	/* request specific path instance */
1987602ca9eaScth 
1988602ca9eaScth 	/* determine type of arg based on flags */
1989602ca9eaScth 	if (flags & MDI_SELECT_PATH_INSTANCE) {
1990602ca9eaScth 		flags &= ~MDI_SELECT_PATH_INSTANCE;
1991602ca9eaScth 		path_instance = (int)(intptr_t)arg;
1992602ca9eaScth 		start_pip = NULL;
1993602ca9eaScth 	} else {
1994602ca9eaScth 		path_instance = 0;
1995602ca9eaScth 		start_pip = (mdi_pathinfo_t *)arg;
1996602ca9eaScth 	}
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 	if (flags != 0) {
19997c478bd9Sstevel@tonic-gate 		/*
20007c478bd9Sstevel@tonic-gate 		 * disable default behavior
20017c478bd9Sstevel@tonic-gate 		 */
20027c478bd9Sstevel@tonic-gate 		sb = 0;
20037c478bd9Sstevel@tonic-gate 	}
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	*ret_pip = NULL;
20067c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
20077c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
20087c478bd9Sstevel@tonic-gate 		/* mdi extensions are NULL, Nothing more to do */
20097c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
20107c478bd9Sstevel@tonic-gate 	}
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 	if (sb) {
20157c478bd9Sstevel@tonic-gate 		if (MDI_CLIENT_IS_FAILED(ct)) {
20167c478bd9Sstevel@tonic-gate 			/*
20177c478bd9Sstevel@tonic-gate 			 * Client is not ready to accept any I/O requests.
20187c478bd9Sstevel@tonic-gate 			 * Fail this request.
20197c478bd9Sstevel@tonic-gate 			 */
20207c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_NOTE, cdip, "!mdi_select_path: "
20215e3986cbScth 			    "client state offline ct = %p\n", (void *)ct));
20227c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
20237c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
20247c478bd9Sstevel@tonic-gate 		}
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 		if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
20277c478bd9Sstevel@tonic-gate 			/*
20287c478bd9Sstevel@tonic-gate 			 * Check for Failover is in progress. If so tell the
20297c478bd9Sstevel@tonic-gate 			 * caller that this device is busy.
20307c478bd9Sstevel@tonic-gate 			 */
20317c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_NOTE, cdip, "!mdi_select_path: "
20325e3986cbScth 			    "client failover in progress ct = %p\n",
20335e3986cbScth 			    (void *)ct));
20347c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
20357c478bd9Sstevel@tonic-gate 			return (MDI_BUSY);
20367c478bd9Sstevel@tonic-gate 		}
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 		/*
20397c478bd9Sstevel@tonic-gate 		 * Check to see whether the client device is attached.
20407c478bd9Sstevel@tonic-gate 		 * If not so, let the vHCI driver manually select a path
20417c478bd9Sstevel@tonic-gate 		 * (standby) and let the probe/attach process to continue.
20427c478bd9Sstevel@tonic-gate 		 */
2043737d277aScth 		if (MDI_CLIENT_IS_DETACHED(ct) || !i_ddi_devi_attached(cdip)) {
20445e3986cbScth 			MDI_DEBUG(4, (CE_NOTE, cdip, "!Devi is onlining "
20455e3986cbScth 			    "ct = %p\n", (void *)ct));
20467c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
20477c478bd9Sstevel@tonic-gate 			return (MDI_DEVI_ONLINING);
20487c478bd9Sstevel@tonic-gate 		}
20497c478bd9Sstevel@tonic-gate 	}
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	/*
20527c478bd9Sstevel@tonic-gate 	 * Cache in the client list head.  If head of the list is NULL
20537c478bd9Sstevel@tonic-gate 	 * return MDI_NOPATH
20547c478bd9Sstevel@tonic-gate 	 */
20557c478bd9Sstevel@tonic-gate 	head = ct->ct_path_head;
20567c478bd9Sstevel@tonic-gate 	if (head == NULL) {
20577c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
20587c478bd9Sstevel@tonic-gate 		return (MDI_NOPATH);
20597c478bd9Sstevel@tonic-gate 	}
20607c478bd9Sstevel@tonic-gate 
2061602ca9eaScth 	/* Caller is specifying a specific pathinfo path by path_instance */
2062602ca9eaScth 	if (path_instance) {
2063602ca9eaScth 		/* search for pathinfo with correct path_instance */
2064602ca9eaScth 		for (pip = head;
2065602ca9eaScth 		    pip && (mdi_pi_get_path_instance(pip) != path_instance);
2066602ca9eaScth 		    pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link)
2067602ca9eaScth 			;
2068602ca9eaScth 
2069602ca9eaScth 		/* If path can't be selected then MDI_FAILURE is returned. */
2070602ca9eaScth 		if (pip == NULL) {
2071602ca9eaScth 			MDI_CLIENT_UNLOCK(ct);
2072602ca9eaScth 			return (MDI_FAILURE);
2073602ca9eaScth 		}
2074602ca9eaScth 
2075602ca9eaScth 		/* verify state of path */
2076602ca9eaScth 		MDI_PI_LOCK(pip);
2077602ca9eaScth 		if (MDI_PI(pip)->pi_state != MDI_PATHINFO_STATE_ONLINE) {
2078602ca9eaScth 			MDI_PI_UNLOCK(pip);
2079602ca9eaScth 			MDI_CLIENT_UNLOCK(ct);
2080602ca9eaScth 			return (MDI_FAILURE);
2081602ca9eaScth 		}
2082602ca9eaScth 
2083602ca9eaScth 		/*
2084602ca9eaScth 		 * Return the path in hold state. Caller should release the
2085602ca9eaScth 		 * lock by calling mdi_rele_path()
2086602ca9eaScth 		 */
2087602ca9eaScth 		MDI_PI_HOLD(pip);
2088602ca9eaScth 		MDI_PI_UNLOCK(pip);
2089602ca9eaScth 		ct->ct_path_last = pip;
2090602ca9eaScth 		*ret_pip = pip;
2091602ca9eaScth 		MDI_CLIENT_UNLOCK(ct);
2092602ca9eaScth 		return (MDI_SUCCESS);
2093602ca9eaScth 	}
2094602ca9eaScth 
20957c478bd9Sstevel@tonic-gate 	/*
20967c478bd9Sstevel@tonic-gate 	 * for non default behavior, bypass current
20977c478bd9Sstevel@tonic-gate 	 * load balancing policy and always use LOAD_BALANCE_RR
20987c478bd9Sstevel@tonic-gate 	 * except that the start point will be adjusted based
20997c478bd9Sstevel@tonic-gate 	 * on the provided start_pip
21007c478bd9Sstevel@tonic-gate 	 */
21017c478bd9Sstevel@tonic-gate 	lbp = sb ? ct->ct_lb : LOAD_BALANCE_RR;
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	switch (lbp) {
21047c478bd9Sstevel@tonic-gate 	case LOAD_BALANCE_NONE:
21057c478bd9Sstevel@tonic-gate 		/*
21067c478bd9Sstevel@tonic-gate 		 * Load balancing is None  or Alternate path mode
21077c478bd9Sstevel@tonic-gate 		 * Start looking for a online mdi_pathinfo node starting from
21087c478bd9Sstevel@tonic-gate 		 * last known selected path
21097c478bd9Sstevel@tonic-gate 		 */
21107c478bd9Sstevel@tonic-gate 		preferred = 1;
21117c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)ct->ct_path_last;
21127c478bd9Sstevel@tonic-gate 		if (pip == NULL) {
21137c478bd9Sstevel@tonic-gate 			pip = head;
21147c478bd9Sstevel@tonic-gate 		}
21157c478bd9Sstevel@tonic-gate 		start = pip;
21167c478bd9Sstevel@tonic-gate 		do {
21177c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
21187c478bd9Sstevel@tonic-gate 			/*
21197c478bd9Sstevel@tonic-gate 			 * No need to explicitly check if the path is disabled.
21207c478bd9Sstevel@tonic-gate 			 * Since we are checking for state == ONLINE and the
21217c478bd9Sstevel@tonic-gate 			 * same veriable is used for DISABLE/ENABLE information.
21227c478bd9Sstevel@tonic-gate 			 */
2123ee28b439Scm136836 			if ((MDI_PI(pip)->pi_state  ==
2124ee28b439Scm136836 				MDI_PATHINFO_STATE_ONLINE) &&
21257c478bd9Sstevel@tonic-gate 				preferred == MDI_PI(pip)->pi_preferred) {
21267c478bd9Sstevel@tonic-gate 				/*
21277c478bd9Sstevel@tonic-gate 				 * Return the path in hold state. Caller should
21287c478bd9Sstevel@tonic-gate 				 * release the lock by calling mdi_rele_path()
21297c478bd9Sstevel@tonic-gate 				 */
21307c478bd9Sstevel@tonic-gate 				MDI_PI_HOLD(pip);
21317c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
21327c478bd9Sstevel@tonic-gate 				ct->ct_path_last = pip;
21337c478bd9Sstevel@tonic-gate 				*ret_pip = pip;
21347c478bd9Sstevel@tonic-gate 				MDI_CLIENT_UNLOCK(ct);
21357c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
21367c478bd9Sstevel@tonic-gate 			}
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 			/*
21397c478bd9Sstevel@tonic-gate 			 * Path is busy.
21407c478bd9Sstevel@tonic-gate 			 */
21417c478bd9Sstevel@tonic-gate 			if (MDI_PI_IS_DRV_DISABLE_TRANSIENT(pip) ||
21427c478bd9Sstevel@tonic-gate 			    MDI_PI_IS_TRANSIENT(pip))
21437c478bd9Sstevel@tonic-gate 				retry = 1;
21447c478bd9Sstevel@tonic-gate 			/*
21457c478bd9Sstevel@tonic-gate 			 * Keep looking for a next available online path
21467c478bd9Sstevel@tonic-gate 			 */
21477c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
21487c478bd9Sstevel@tonic-gate 			if (next == NULL) {
21497c478bd9Sstevel@tonic-gate 				next = head;
21507c478bd9Sstevel@tonic-gate 			}
21517c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
21527c478bd9Sstevel@tonic-gate 			pip = next;
21537c478bd9Sstevel@tonic-gate 			if (start == pip && preferred) {
21547c478bd9Sstevel@tonic-gate 				preferred = 0;
21557c478bd9Sstevel@tonic-gate 			} else if (start == pip && !preferred) {
21567c478bd9Sstevel@tonic-gate 				cont = 0;
21577c478bd9Sstevel@tonic-gate 			}
21587c478bd9Sstevel@tonic-gate 		} while (cont);
21597c478bd9Sstevel@tonic-gate 		break;
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	case LOAD_BALANCE_LBA:
21627c478bd9Sstevel@tonic-gate 		/*
21637c478bd9Sstevel@tonic-gate 		 * Make sure we are looking
21647c478bd9Sstevel@tonic-gate 		 * for an online path. Otherwise, if it is for a STANDBY
21657c478bd9Sstevel@tonic-gate 		 * path request, it will go through and fetch an ONLINE
21667c478bd9Sstevel@tonic-gate 		 * path which is not desirable.
21677c478bd9Sstevel@tonic-gate 		 */
21687c478bd9Sstevel@tonic-gate 		if ((ct->ct_lb_args != NULL) &&
21697c478bd9Sstevel@tonic-gate 			    (ct->ct_lb_args->region_size) && bp &&
21707c478bd9Sstevel@tonic-gate 				(sb || (flags == MDI_SELECT_ONLINE_PATH))) {
21717c478bd9Sstevel@tonic-gate 			if (i_mdi_lba_lb(ct, ret_pip, bp)
21727c478bd9Sstevel@tonic-gate 				    == MDI_SUCCESS) {
21737c478bd9Sstevel@tonic-gate 				MDI_CLIENT_UNLOCK(ct);
21747c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
21757c478bd9Sstevel@tonic-gate 			}
21767c478bd9Sstevel@tonic-gate 		}
21777c478bd9Sstevel@tonic-gate 		/*  FALLTHROUGH */
21787c478bd9Sstevel@tonic-gate 	case LOAD_BALANCE_RR:
21797c478bd9Sstevel@tonic-gate 		/*
21807c478bd9Sstevel@tonic-gate 		 * Load balancing is Round Robin. Start looking for a online
21817c478bd9Sstevel@tonic-gate 		 * mdi_pathinfo node starting from last known selected path
21827c478bd9Sstevel@tonic-gate 		 * as the start point.  If override flags are specified,
21837c478bd9Sstevel@tonic-gate 		 * process accordingly.
21847c478bd9Sstevel@tonic-gate 		 * If the search is already in effect(start_pip not null),
21857c478bd9Sstevel@tonic-gate 		 * then lets just use the same path preference to continue the
21867c478bd9Sstevel@tonic-gate 		 * traversal.
21877c478bd9Sstevel@tonic-gate 		 */
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate 		if (start_pip != NULL) {
21907c478bd9Sstevel@tonic-gate 			preferred = MDI_PI(start_pip)->pi_preferred;
21917c478bd9Sstevel@tonic-gate 		} else {
21927c478bd9Sstevel@tonic-gate 			preferred = 1;
21937c478bd9Sstevel@tonic-gate 		}
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 		start = sb ? (mdi_pathinfo_t *)ct->ct_path_last : start_pip;
21967c478bd9Sstevel@tonic-gate 		if (start == NULL) {
21977c478bd9Sstevel@tonic-gate 			pip = head;
21987c478bd9Sstevel@tonic-gate 		} else {
21997c478bd9Sstevel@tonic-gate 			pip = (mdi_pathinfo_t *)MDI_PI(start)->pi_client_link;
22007c478bd9Sstevel@tonic-gate 			if (pip == NULL) {
22017c478bd9Sstevel@tonic-gate 				if (!sb) {
22027c478bd9Sstevel@tonic-gate 					if (preferred == 0) {
22037c478bd9Sstevel@tonic-gate 						/*
22047c478bd9Sstevel@tonic-gate 						 * Looks like we have completed
22057c478bd9Sstevel@tonic-gate 						 * the traversal as preferred
22067c478bd9Sstevel@tonic-gate 						 * value is 0. Time to bail out.
22077c478bd9Sstevel@tonic-gate 						 */
22087c478bd9Sstevel@tonic-gate 						*ret_pip = NULL;
22097c478bd9Sstevel@tonic-gate 						MDI_CLIENT_UNLOCK(ct);
22107c478bd9Sstevel@tonic-gate 						return (MDI_NOPATH);
22117c478bd9Sstevel@tonic-gate 					} else {
22127c478bd9Sstevel@tonic-gate 						/*
22137c478bd9Sstevel@tonic-gate 						 * Looks like we reached the
22147c478bd9Sstevel@tonic-gate 						 * end of the list. Lets enable
22157c478bd9Sstevel@tonic-gate 						 * traversal of non preferred
22167c478bd9Sstevel@tonic-gate 						 * paths.
22177c478bd9Sstevel@tonic-gate 						 */
22187c478bd9Sstevel@tonic-gate 						preferred = 0;
22197c478bd9Sstevel@tonic-gate 					}
22207c478bd9Sstevel@tonic-gate 				}
22217c478bd9Sstevel@tonic-gate 				pip = head;
22227c478bd9Sstevel@tonic-gate 			}
22237c478bd9Sstevel@tonic-gate 		}
22247c478bd9Sstevel@tonic-gate 		start = pip;
22257c478bd9Sstevel@tonic-gate 		do {
22267c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
22277c478bd9Sstevel@tonic-gate 			if (sb) {
22287c478bd9Sstevel@tonic-gate 				cond = ((MDI_PI(pip)->pi_state ==
22297c478bd9Sstevel@tonic-gate 				    MDI_PATHINFO_STATE_ONLINE &&
22307c478bd9Sstevel@tonic-gate 					MDI_PI(pip)->pi_preferred ==
22317c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
22327c478bd9Sstevel@tonic-gate 			} else {
22337c478bd9Sstevel@tonic-gate 				if (flags == MDI_SELECT_ONLINE_PATH) {
22347c478bd9Sstevel@tonic-gate 					cond = ((MDI_PI(pip)->pi_state ==
22357c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_ONLINE &&
22367c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_preferred ==
22377c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
22387c478bd9Sstevel@tonic-gate 				} else if (flags == MDI_SELECT_STANDBY_PATH) {
22397c478bd9Sstevel@tonic-gate 					cond = ((MDI_PI(pip)->pi_state ==
22407c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_STANDBY &&
22417c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_preferred ==
22427c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
22437c478bd9Sstevel@tonic-gate 				} else if (flags == (MDI_SELECT_ONLINE_PATH |
22447c478bd9Sstevel@tonic-gate 				    MDI_SELECT_STANDBY_PATH)) {
22457c478bd9Sstevel@tonic-gate 					cond = (((MDI_PI(pip)->pi_state ==
22467c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_ONLINE ||
22477c478bd9Sstevel@tonic-gate 					    (MDI_PI(pip)->pi_state ==
22487c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_STANDBY)) &&
22497c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_preferred ==
22507c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
2251ee28b439Scm136836 				} else if (flags ==
2252ee28b439Scm136836 					(MDI_SELECT_STANDBY_PATH |
2253ee28b439Scm136836 					MDI_SELECT_ONLINE_PATH |
2254ee28b439Scm136836 					MDI_SELECT_USER_DISABLE_PATH)) {
2255ee28b439Scm136836 					cond = (((MDI_PI(pip)->pi_state ==
2256ee28b439Scm136836 					    MDI_PATHINFO_STATE_ONLINE ||
2257ee28b439Scm136836 					    (MDI_PI(pip)->pi_state ==
2258ee28b439Scm136836 					    MDI_PATHINFO_STATE_STANDBY) ||
2259ee28b439Scm136836 						(MDI_PI(pip)->pi_state ==
2260ee28b439Scm136836 					    (MDI_PATHINFO_STATE_ONLINE|
2261ee28b439Scm136836 					    MDI_PATHINFO_STATE_USER_DISABLE)) ||
2262ee28b439Scm136836 						(MDI_PI(pip)->pi_state ==
2263ee28b439Scm136836 					    (MDI_PATHINFO_STATE_STANDBY |
2264ee28b439Scm136836 					    MDI_PATHINFO_STATE_USER_DISABLE)))&&
2265ee28b439Scm136836 						MDI_PI(pip)->pi_preferred ==
2266ee28b439Scm136836 						preferred) ? 1 : 0);
22677c478bd9Sstevel@tonic-gate 				} else {
22687c478bd9Sstevel@tonic-gate 					cond = 0;
22697c478bd9Sstevel@tonic-gate 				}
22707c478bd9Sstevel@tonic-gate 			}
22717c478bd9Sstevel@tonic-gate 			/*
22727c478bd9Sstevel@tonic-gate 			 * No need to explicitly check if the path is disabled.
22737c478bd9Sstevel@tonic-gate 			 * Since we are checking for state == ONLINE and the
22747c478bd9Sstevel@tonic-gate 			 * same veriable is used for DISABLE/ENABLE information.
22757c478bd9Sstevel@tonic-gate 			 */
22767c478bd9Sstevel@tonic-gate 			if (cond) {
22777c478bd9Sstevel@tonic-gate 				/*
22787c478bd9Sstevel@tonic-gate 				 * Return the path in hold state. Caller should
22797c478bd9Sstevel@tonic-gate 				 * release the lock by calling mdi_rele_path()
22807c478bd9Sstevel@tonic-gate 				 */
22817c478bd9Sstevel@tonic-gate 				MDI_PI_HOLD(pip);
22827c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
22837c478bd9Sstevel@tonic-gate 				if (sb)
22847c478bd9Sstevel@tonic-gate 					ct->ct_path_last = pip;
22857c478bd9Sstevel@tonic-gate 				*ret_pip = pip;
22867c478bd9Sstevel@tonic-gate 				MDI_CLIENT_UNLOCK(ct);
22877c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
22887c478bd9Sstevel@tonic-gate 			}
22897c478bd9Sstevel@tonic-gate 			/*
22907c478bd9Sstevel@tonic-gate 			 * Path is busy.
22917c478bd9Sstevel@tonic-gate 			 */
22927c478bd9Sstevel@tonic-gate 			if (MDI_PI_IS_DRV_DISABLE_TRANSIENT(pip) ||
22937c478bd9Sstevel@tonic-gate 			    MDI_PI_IS_TRANSIENT(pip))
22947c478bd9Sstevel@tonic-gate 				retry = 1;
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 			/*
22977c478bd9Sstevel@tonic-gate 			 * Keep looking for a next available online path
22987c478bd9Sstevel@tonic-gate 			 */
22997c478bd9Sstevel@tonic-gate do_again:
23007c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
23017c478bd9Sstevel@tonic-gate 			if (next == NULL) {
23027c478bd9Sstevel@tonic-gate 				if (!sb) {
23037c478bd9Sstevel@tonic-gate 					if (preferred == 1) {
23047c478bd9Sstevel@tonic-gate 						/*
23057c478bd9Sstevel@tonic-gate 						 * Looks like we reached the
23067c478bd9Sstevel@tonic-gate 						 * end of the list. Lets enable
23077c478bd9Sstevel@tonic-gate 						 * traversal of non preferred
23087c478bd9Sstevel@tonic-gate 						 * paths.
23097c478bd9Sstevel@tonic-gate 						 */
23107c478bd9Sstevel@tonic-gate 						preferred = 0;
23117c478bd9Sstevel@tonic-gate 						next = head;
23127c478bd9Sstevel@tonic-gate 					} else {
23137c478bd9Sstevel@tonic-gate 						/*
23147c478bd9Sstevel@tonic-gate 						 * We have done both the passes
23157c478bd9Sstevel@tonic-gate 						 * Preferred as well as for
23167c478bd9Sstevel@tonic-gate 						 * Non-preferred. Bail out now.
23177c478bd9Sstevel@tonic-gate 						 */
23187c478bd9Sstevel@tonic-gate 						cont = 0;
23197c478bd9Sstevel@tonic-gate 					}
23207c478bd9Sstevel@tonic-gate 				} else {
23217c478bd9Sstevel@tonic-gate 					/*
23227c478bd9Sstevel@tonic-gate 					 * Standard behavior case.
23237c478bd9Sstevel@tonic-gate 					 */
23247c478bd9Sstevel@tonic-gate 					next = head;
23257c478bd9Sstevel@tonic-gate 				}
23267c478bd9Sstevel@tonic-gate 			}
23277c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
23287c478bd9Sstevel@tonic-gate 			if (cont == 0) {
23297c478bd9Sstevel@tonic-gate 				break;
23307c478bd9Sstevel@tonic-gate 			}
23317c478bd9Sstevel@tonic-gate 			pip = next;
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 			if (!sb) {
23347c478bd9Sstevel@tonic-gate 				/*
23357c478bd9Sstevel@tonic-gate 				 * We need to handle the selection of
23367c478bd9Sstevel@tonic-gate 				 * non-preferred path in the following
23377c478bd9Sstevel@tonic-gate 				 * case:
23387c478bd9Sstevel@tonic-gate 				 *
23397c478bd9Sstevel@tonic-gate 				 * +------+   +------+   +------+   +-----+
23407c478bd9Sstevel@tonic-gate 				 * | A : 1| - | B : 1| - | C : 0| - |NULL |
23417c478bd9Sstevel@tonic-gate 				 * +------+   +------+   +------+   +-----+
23427c478bd9Sstevel@tonic-gate 				 *
23437c478bd9Sstevel@tonic-gate 				 * If we start the search with B, we need to
23447c478bd9Sstevel@tonic-gate 				 * skip beyond B to pick C which is non -
23457c478bd9Sstevel@tonic-gate 				 * preferred in the second pass. The following
23467c478bd9Sstevel@tonic-gate 				 * test, if true, will allow us to skip over
23477c478bd9Sstevel@tonic-gate 				 * the 'start'(B in the example) to select
23487c478bd9Sstevel@tonic-gate 				 * other non preferred elements.
23497c478bd9Sstevel@tonic-gate 				 */
23507c478bd9Sstevel@tonic-gate 				if ((start_pip != NULL) && (start_pip == pip) &&
23517c478bd9Sstevel@tonic-gate 				    (MDI_PI(start_pip)->pi_preferred
23527c478bd9Sstevel@tonic-gate 				    != preferred)) {
23537c478bd9Sstevel@tonic-gate 					/*
23547c478bd9Sstevel@tonic-gate 					 * try again after going past the start
23557c478bd9Sstevel@tonic-gate 					 * pip
23567c478bd9Sstevel@tonic-gate 					 */
23577c478bd9Sstevel@tonic-gate 					MDI_PI_LOCK(pip);
23587c478bd9Sstevel@tonic-gate 					goto do_again;
23597c478bd9Sstevel@tonic-gate 				}
23607c478bd9Sstevel@tonic-gate 			} else {
23617c478bd9Sstevel@tonic-gate 				/*
23627c478bd9Sstevel@tonic-gate 				 * Standard behavior case
23637c478bd9Sstevel@tonic-gate 				 */
23647c478bd9Sstevel@tonic-gate 				if (start == pip && preferred) {
23657c478bd9Sstevel@tonic-gate 					/* look for nonpreferred paths */
23667c478bd9Sstevel@tonic-gate 					preferred = 0;
23677c478bd9Sstevel@tonic-gate 				} else if (start == pip && !preferred) {
23687c478bd9Sstevel@tonic-gate 					/*
23697c478bd9Sstevel@tonic-gate 					 * Exit condition
23707c478bd9Sstevel@tonic-gate 					 */
23717c478bd9Sstevel@tonic-gate 					cont = 0;
23727c478bd9Sstevel@tonic-gate 				}
23737c478bd9Sstevel@tonic-gate 			}
23747c478bd9Sstevel@tonic-gate 		} while (cont);
23757c478bd9Sstevel@tonic-gate 		break;
23767c478bd9Sstevel@tonic-gate 	}
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
23797c478bd9Sstevel@tonic-gate 	if (retry == 1) {
23807c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
23817c478bd9Sstevel@tonic-gate 	} else {
23827c478bd9Sstevel@tonic-gate 		return (MDI_NOPATH);
23837c478bd9Sstevel@tonic-gate 	}
23847c478bd9Sstevel@tonic-gate }
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate /*
23877c478bd9Sstevel@tonic-gate  * For a client, return the next available path to any phci
23887c478bd9Sstevel@tonic-gate  *
23897c478bd9Sstevel@tonic-gate  * Note:
23907c478bd9Sstevel@tonic-gate  *		Caller should hold the branch's devinfo node to get a consistent
23917c478bd9Sstevel@tonic-gate  *		snap shot of the mdi_pathinfo nodes.
23927c478bd9Sstevel@tonic-gate  *
23937c478bd9Sstevel@tonic-gate  *		Please note that even the list is stable the mdi_pathinfo
23947c478bd9Sstevel@tonic-gate  *		node state and properties are volatile.  The caller should lock
23957c478bd9Sstevel@tonic-gate  *		and unlock the nodes by calling mdi_pi_lock() and
23967c478bd9Sstevel@tonic-gate  *		mdi_pi_unlock() functions to get a stable properties.
23977c478bd9Sstevel@tonic-gate  *
23987c478bd9Sstevel@tonic-gate  *		If there is a need to use the nodes beyond the hold of the
23997c478bd9Sstevel@tonic-gate  *		devinfo node period (For ex. I/O), then mdi_pathinfo node
24007c478bd9Sstevel@tonic-gate  *		need to be held against unexpected removal by calling
24017c478bd9Sstevel@tonic-gate  *		mdi_hold_path() and should be released by calling
24027c478bd9Sstevel@tonic-gate  *		mdi_rele_path() on completion.
24037c478bd9Sstevel@tonic-gate  */
24047c478bd9Sstevel@tonic-gate mdi_pathinfo_t *
24057c478bd9Sstevel@tonic-gate mdi_get_next_phci_path(dev_info_t *ct_dip, mdi_pathinfo_t *pip)
24067c478bd9Sstevel@tonic-gate {
24077c478bd9Sstevel@tonic-gate 	mdi_client_t *ct;
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 	if (!MDI_CLIENT(ct_dip))
24107c478bd9Sstevel@tonic-gate 		return (NULL);
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 	/*
24137c478bd9Sstevel@tonic-gate 	 * Walk through client link
24147c478bd9Sstevel@tonic-gate 	 */
24157c478bd9Sstevel@tonic-gate 	ct = (mdi_client_t *)DEVI(ct_dip)->devi_mdi_client;
24167c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 	if (pip == NULL)
24197c478bd9Sstevel@tonic-gate 		return ((mdi_pathinfo_t *)ct->ct_path_head);
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 	return ((mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link);
24227c478bd9Sstevel@tonic-gate }
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate /*
24257c478bd9Sstevel@tonic-gate  * For a phci, return the next available path to any client
24267c478bd9Sstevel@tonic-gate  * Note: ditto mdi_get_next_phci_path()
24277c478bd9Sstevel@tonic-gate  */
24287c478bd9Sstevel@tonic-gate mdi_pathinfo_t *
24297c478bd9Sstevel@tonic-gate mdi_get_next_client_path(dev_info_t *ph_dip, mdi_pathinfo_t *pip)
24307c478bd9Sstevel@tonic-gate {
24317c478bd9Sstevel@tonic-gate 	mdi_phci_t *ph;
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 	if (!MDI_PHCI(ph_dip))
24347c478bd9Sstevel@tonic-gate 		return (NULL);
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 	/*
24377c478bd9Sstevel@tonic-gate 	 * Walk through pHCI link
24387c478bd9Sstevel@tonic-gate 	 */
24397c478bd9Sstevel@tonic-gate 	ph = (mdi_phci_t *)DEVI(ph_dip)->devi_mdi_xhci;
24407c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 	if (pip == NULL)
24437c478bd9Sstevel@tonic-gate 		return ((mdi_pathinfo_t *)ph->ph_path_head);
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate 	return ((mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link);
24467c478bd9Sstevel@tonic-gate }
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate /*
24497c478bd9Sstevel@tonic-gate  * mdi_hold_path():
24507c478bd9Sstevel@tonic-gate  *		Hold the mdi_pathinfo node against unwanted unexpected free.
24517c478bd9Sstevel@tonic-gate  * Return Values:
24527c478bd9Sstevel@tonic-gate  *		None
24537c478bd9Sstevel@tonic-gate  */
24547c478bd9Sstevel@tonic-gate void
24557c478bd9Sstevel@tonic-gate mdi_hold_path(mdi_pathinfo_t *pip)
24567c478bd9Sstevel@tonic-gate {
24577c478bd9Sstevel@tonic-gate 	if (pip) {
24587c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
24597c478bd9Sstevel@tonic-gate 		MDI_PI_HOLD(pip);
24607c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
24617c478bd9Sstevel@tonic-gate 	}
24627c478bd9Sstevel@tonic-gate }
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate /*
24667c478bd9Sstevel@tonic-gate  * mdi_rele_path():
24677c478bd9Sstevel@tonic-gate  *		Release the mdi_pathinfo node which was selected
24687c478bd9Sstevel@tonic-gate  *		through mdi_select_path() mechanism or manually held by
24697c478bd9Sstevel@tonic-gate  *		calling mdi_hold_path().
24707c478bd9Sstevel@tonic-gate  * Return Values:
24717c478bd9Sstevel@tonic-gate  *		None
24727c478bd9Sstevel@tonic-gate  */
24737c478bd9Sstevel@tonic-gate void
24747c478bd9Sstevel@tonic-gate mdi_rele_path(mdi_pathinfo_t *pip)
24757c478bd9Sstevel@tonic-gate {
24767c478bd9Sstevel@tonic-gate 	if (pip) {
24777c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
24787c478bd9Sstevel@tonic-gate 		MDI_PI_RELE(pip);
24797c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_ref_cnt == 0) {
24807c478bd9Sstevel@tonic-gate 			cv_broadcast(&MDI_PI(pip)->pi_ref_cv);
24817c478bd9Sstevel@tonic-gate 		}
24827c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
24837c478bd9Sstevel@tonic-gate 	}
24847c478bd9Sstevel@tonic-gate }
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate /*
24877c478bd9Sstevel@tonic-gate  * mdi_pi_lock():
24887c478bd9Sstevel@tonic-gate  * 		Lock the mdi_pathinfo node.
24897c478bd9Sstevel@tonic-gate  * Note:
24907c478bd9Sstevel@tonic-gate  *		The caller should release the lock by calling mdi_pi_unlock()
24917c478bd9Sstevel@tonic-gate  */
24927c478bd9Sstevel@tonic-gate void
24937c478bd9Sstevel@tonic-gate mdi_pi_lock(mdi_pathinfo_t *pip)
24947c478bd9Sstevel@tonic-gate {
24957c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
24967c478bd9Sstevel@tonic-gate 	if (pip) {
24977c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
24987c478bd9Sstevel@tonic-gate 	}
24997c478bd9Sstevel@tonic-gate }
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate /*
25037c478bd9Sstevel@tonic-gate  * mdi_pi_unlock():
25047c478bd9Sstevel@tonic-gate  * 		Unlock the mdi_pathinfo node.
25057c478bd9Sstevel@tonic-gate  * Note:
25067c478bd9Sstevel@tonic-gate  *		The mdi_pathinfo node should have been locked with mdi_pi_lock()
25077c478bd9Sstevel@tonic-gate  */
25087c478bd9Sstevel@tonic-gate void
25097c478bd9Sstevel@tonic-gate mdi_pi_unlock(mdi_pathinfo_t *pip)
25107c478bd9Sstevel@tonic-gate {
25117c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
25127c478bd9Sstevel@tonic-gate 	if (pip) {
25137c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
25147c478bd9Sstevel@tonic-gate 	}
25157c478bd9Sstevel@tonic-gate }
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate /*
25187c478bd9Sstevel@tonic-gate  * mdi_pi_find():
25197c478bd9Sstevel@tonic-gate  *		Search the list of mdi_pathinfo nodes attached to the
25207c478bd9Sstevel@tonic-gate  *		pHCI/Client device node whose path address matches "paddr".
25217c478bd9Sstevel@tonic-gate  *		Returns a pointer to the mdi_pathinfo node if a matching node is
25227c478bd9Sstevel@tonic-gate  *		found.
25237c478bd9Sstevel@tonic-gate  * Return Values:
25247c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node handle
25257c478bd9Sstevel@tonic-gate  *		NULL
25267c478bd9Sstevel@tonic-gate  * Notes:
25277c478bd9Sstevel@tonic-gate  *		Caller need not hold any locks to call this function.
25287c478bd9Sstevel@tonic-gate  */
25297c478bd9Sstevel@tonic-gate mdi_pathinfo_t *
25307c478bd9Sstevel@tonic-gate mdi_pi_find(dev_info_t *pdip, char *caddr, char *paddr)
25317c478bd9Sstevel@tonic-gate {
25327c478bd9Sstevel@tonic-gate 	mdi_phci_t		*ph;
25337c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh;
25347c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
25357c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t		*pip = NULL;
25367c478bd9Sstevel@tonic-gate 
25375e3986cbScth 	MDI_DEBUG(2, (CE_NOTE, pdip, "!mdi_pi_find: %s %s",
25385e3986cbScth 	    caddr ? caddr : "NULL", paddr ? paddr : "NULL"));
25397c478bd9Sstevel@tonic-gate 	if ((pdip == NULL) || (paddr == NULL)) {
25407c478bd9Sstevel@tonic-gate 		return (NULL);
25417c478bd9Sstevel@tonic-gate 	}
25427c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
25437c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
25447c478bd9Sstevel@tonic-gate 		/*
25457c478bd9Sstevel@tonic-gate 		 * Invalid pHCI device, Nothing more to do.
25467c478bd9Sstevel@tonic-gate 		 */
25475e3986cbScth 		MDI_DEBUG(2, (CE_WARN, pdip,
25487c478bd9Sstevel@tonic-gate 		    "!mdi_pi_find: invalid phci"));
25497c478bd9Sstevel@tonic-gate 		return (NULL);
25507c478bd9Sstevel@tonic-gate 	}
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
25537c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
25547c478bd9Sstevel@tonic-gate 		/*
25557c478bd9Sstevel@tonic-gate 		 * Invalid vHCI device, Nothing more to do.
25567c478bd9Sstevel@tonic-gate 		 */
25575e3986cbScth 		MDI_DEBUG(2, (CE_WARN, pdip,
25585e3986cbScth 		    "!mdi_pi_find: invalid vhci"));
25597c478bd9Sstevel@tonic-gate 		return (NULL);
25607c478bd9Sstevel@tonic-gate 	}
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 	/*
25635e3986cbScth 	 * Look for pathinfo node identified by paddr.
25647c478bd9Sstevel@tonic-gate 	 */
25657c478bd9Sstevel@tonic-gate 	if (caddr == NULL) {
25667c478bd9Sstevel@tonic-gate 		/*
25677c478bd9Sstevel@tonic-gate 		 * Find a mdi_pathinfo node under pHCI list for a matching
25687c478bd9Sstevel@tonic-gate 		 * unit address.
25697c478bd9Sstevel@tonic-gate 		 */
25705e3986cbScth 		MDI_PHCI_LOCK(ph);
25715e3986cbScth 		if (MDI_PHCI_IS_OFFLINE(ph)) {
25725e3986cbScth 			MDI_DEBUG(2, (CE_WARN, pdip,
25735e3986cbScth 			    "!mdi_pi_find: offline phci %p", (void *)ph));
25745e3986cbScth 			MDI_PHCI_UNLOCK(ph);
25755e3986cbScth 			return (NULL);
25765e3986cbScth 		}
25777c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)ph->ph_path_head;
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
25807c478bd9Sstevel@tonic-gate 			if (strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
25817c478bd9Sstevel@tonic-gate 				break;
25827c478bd9Sstevel@tonic-gate 			}
25837c478bd9Sstevel@tonic-gate 			pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
25847c478bd9Sstevel@tonic-gate 		}
25855e3986cbScth 		MDI_PHCI_UNLOCK(ph);
25865e3986cbScth 		MDI_DEBUG(2, (CE_NOTE, pdip, "!mdi_pi_find: found %p",
25875e3986cbScth 		    (void *)pip));
25887c478bd9Sstevel@tonic-gate 		return (pip);
25897c478bd9Sstevel@tonic-gate 	}
25907c478bd9Sstevel@tonic-gate 
25917c478bd9Sstevel@tonic-gate 	/*
25923c34adc5Sramat 	 * XXX - Is the rest of the code in this function really necessary?
25933c34adc5Sramat 	 * The consumers of mdi_pi_find() can search for the desired pathinfo
25943c34adc5Sramat 	 * node by calling mdi_pi_find(pdip, NULL, paddr). Irrespective of
25953c34adc5Sramat 	 * whether the search is based on the pathinfo nodes attached to
25963c34adc5Sramat 	 * the pHCI or the client node, the result will be the same.
25973c34adc5Sramat 	 */
25983c34adc5Sramat 
25993c34adc5Sramat 	/*
26007c478bd9Sstevel@tonic-gate 	 * Find the client device corresponding to 'caddr'
26017c478bd9Sstevel@tonic-gate 	 */
26025e3986cbScth 	MDI_VHCI_CLIENT_LOCK(vh);
26033c34adc5Sramat 
26043c34adc5Sramat 	/*
26053c34adc5Sramat 	 * XXX - Passing NULL to the following function works as long as the
26063c34adc5Sramat 	 * the client addresses (caddr) are unique per vhci basis.
26073c34adc5Sramat 	 */
26083c34adc5Sramat 	ct = i_mdi_client_find(vh, NULL, caddr);
26097c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
26107c478bd9Sstevel@tonic-gate 		/*
26117c478bd9Sstevel@tonic-gate 		 * Client not found, Obviously mdi_pathinfo node has not been
26127c478bd9Sstevel@tonic-gate 		 * created yet.
26137c478bd9Sstevel@tonic-gate 		 */
26145e3986cbScth 		MDI_VHCI_CLIENT_UNLOCK(vh);
26155e3986cbScth 		MDI_DEBUG(2, (CE_NOTE, pdip, "!mdi_pi_find: client not "
26165e3986cbScth 		    "found for caddr %s", caddr ? caddr : "NULL"));
26175e3986cbScth 		return (NULL);
26187c478bd9Sstevel@tonic-gate 	}
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 	/*
26217c478bd9Sstevel@tonic-gate 	 * Hold the client lock and look for a mdi_pathinfo node with matching
26227c478bd9Sstevel@tonic-gate 	 * pHCI and paddr
26237c478bd9Sstevel@tonic-gate 	 */
26247c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate 	/*
26277c478bd9Sstevel@tonic-gate 	 * Release the global mutex as it is no more needed. Note: We always
26287c478bd9Sstevel@tonic-gate 	 * respect the locking order while acquiring.
26297c478bd9Sstevel@tonic-gate 	 */
26305e3986cbScth 	MDI_VHCI_CLIENT_UNLOCK(vh);
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
26337c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
26347c478bd9Sstevel@tonic-gate 		/*
26357c478bd9Sstevel@tonic-gate 		 * Compare the unit address
26367c478bd9Sstevel@tonic-gate 		 */
26377c478bd9Sstevel@tonic-gate 		if ((MDI_PI(pip)->pi_phci == ph) &&
26387c478bd9Sstevel@tonic-gate 		    strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
26397c478bd9Sstevel@tonic-gate 			break;
26407c478bd9Sstevel@tonic-gate 		}
26417c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
26427c478bd9Sstevel@tonic-gate 	}
26437c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
26445e3986cbScth 	MDI_DEBUG(2, (CE_NOTE, pdip, "!mdi_pi_find: found:: %p", (void *)pip));
26457c478bd9Sstevel@tonic-gate 	return (pip);
26467c478bd9Sstevel@tonic-gate }
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate /*
26497c478bd9Sstevel@tonic-gate  * mdi_pi_alloc():
26507c478bd9Sstevel@tonic-gate  *		Allocate and initialize a new instance of a mdi_pathinfo node.
26517c478bd9Sstevel@tonic-gate  *		The mdi_pathinfo node returned by this function identifies a
26527c478bd9Sstevel@tonic-gate  *		unique device path is capable of having properties attached
26537c478bd9Sstevel@tonic-gate  *		and passed to mdi_pi_online() to fully attach and online the
26547c478bd9Sstevel@tonic-gate  *		path and client device node.
26557c478bd9Sstevel@tonic-gate  *		The mdi_pathinfo node returned by this function must be
26567c478bd9Sstevel@tonic-gate  *		destroyed using mdi_pi_free() if the path is no longer
26577c478bd9Sstevel@tonic-gate  *		operational or if the caller fails to attach a client device
26587c478bd9Sstevel@tonic-gate  *		node when calling mdi_pi_online(). The framework will not free
26597c478bd9Sstevel@tonic-gate  *		the resources allocated.
26607c478bd9Sstevel@tonic-gate  *		This function can be called from both interrupt and kernel
26617c478bd9Sstevel@tonic-gate  *		contexts.  DDI_NOSLEEP flag should be used while calling
26627c478bd9Sstevel@tonic-gate  *		from interrupt contexts.
26637c478bd9Sstevel@tonic-gate  * Return Values:
26647c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
26657c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
26667c478bd9Sstevel@tonic-gate  *		MDI_NOMEM
26677c478bd9Sstevel@tonic-gate  */
26687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
26697c478bd9Sstevel@tonic-gate int
26707c478bd9Sstevel@tonic-gate mdi_pi_alloc_compatible(dev_info_t *pdip, char *cname, char *caddr, char *paddr,
26717c478bd9Sstevel@tonic-gate     char **compatible, int ncompatible, int flags, mdi_pathinfo_t **ret_pip)
26727c478bd9Sstevel@tonic-gate {
26737c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
26747c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
26757c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
26767c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip = NULL;
26777c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
26787c478bd9Sstevel@tonic-gate 	int		rv = MDI_NOMEM;
26793c34adc5Sramat 	int		path_allocated = 0;
26807c478bd9Sstevel@tonic-gate 
26815e3986cbScth 	MDI_DEBUG(2, (CE_NOTE, pdip, "!mdi_pi_alloc_compatible: %s %s %s",
26825e3986cbScth 	    cname ? cname : "NULL", caddr ? caddr : "NULL",
26835e3986cbScth 	    paddr ? paddr : "NULL"));
26845e3986cbScth 
26857c478bd9Sstevel@tonic-gate 	if (pdip == NULL || cname == NULL || caddr == NULL || paddr == NULL ||
26867c478bd9Sstevel@tonic-gate 	    ret_pip == NULL) {
26877c478bd9Sstevel@tonic-gate 		/* Nothing more to do */
26887c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
26897c478bd9Sstevel@tonic-gate 	}
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate 	*ret_pip = NULL;
26925e3986cbScth 
26935e3986cbScth 	/* No allocations on detaching pHCI */
26945e3986cbScth 	if (DEVI_IS_DETACHING(pdip)) {
26955e3986cbScth 		/* Invalid pHCI device, return failure */
26965e3986cbScth 		MDI_DEBUG(1, (CE_WARN, pdip,
26975e3986cbScth 		    "!mdi_pi_alloc: detaching pHCI=%p", (void *)pdip));
26985e3986cbScth 		return (MDI_FAILURE);
26995e3986cbScth 	}
27005e3986cbScth 
27017c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
27027c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
27037c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
27047c478bd9Sstevel@tonic-gate 		/* Invalid pHCI device, return failure */
27055e3986cbScth 		MDI_DEBUG(1, (CE_WARN, pdip,
27065e3986cbScth 		    "!mdi_pi_alloc: invalid pHCI=%p", (void *)pdip));
27077c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
27087c478bd9Sstevel@tonic-gate 	}
27097c478bd9Sstevel@tonic-gate 
27107c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
27117c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
27127c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
27137c478bd9Sstevel@tonic-gate 		/* Invalid vHCI device, return failure */
27145e3986cbScth 		MDI_DEBUG(1, (CE_WARN, pdip,
27155e3986cbScth 		    "!mdi_pi_alloc: invalid vHCI=%p", (void *)pdip));
27167c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
27177c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
27187c478bd9Sstevel@tonic-gate 	}
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_READY(ph) == 0) {
27217c478bd9Sstevel@tonic-gate 		/*
27227c478bd9Sstevel@tonic-gate 		 * Do not allow new node creation when pHCI is in
27237c478bd9Sstevel@tonic-gate 		 * offline/suspended states
27247c478bd9Sstevel@tonic-gate 		 */
27255e3986cbScth 		MDI_DEBUG(1, (CE_WARN, pdip,
27265e3986cbScth 		    "mdi_pi_alloc: pHCI=%p is not ready", (void *)ph));
27277c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
27287c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
27297c478bd9Sstevel@tonic-gate 	}
27307c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNSTABLE(ph);
27317c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
27327c478bd9Sstevel@tonic-gate 
27333c34adc5Sramat 	/* look for a matching client, create one if not found */
27345e3986cbScth 	MDI_VHCI_CLIENT_LOCK(vh);
27353c34adc5Sramat 	ct = i_mdi_client_find(vh, cname, caddr);
27367c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
27373c34adc5Sramat 		ct = i_mdi_client_alloc(vh, cname, caddr);
27383c34adc5Sramat 		ASSERT(ct != NULL);
27397c478bd9Sstevel@tonic-gate 	}
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	if (ct->ct_dip == NULL) {
27427c478bd9Sstevel@tonic-gate 		/*
27437c478bd9Sstevel@tonic-gate 		 * Allocate a devinfo node
27447c478bd9Sstevel@tonic-gate 		 */
27457c478bd9Sstevel@tonic-gate 		ct->ct_dip = i_mdi_devinfo_create(vh, cname, caddr,
27463c34adc5Sramat 		    compatible, ncompatible);
27477c478bd9Sstevel@tonic-gate 		if (ct->ct_dip == NULL) {
27487c478bd9Sstevel@tonic-gate 			(void) i_mdi_client_free(vh, ct);
27497c478bd9Sstevel@tonic-gate 			goto fail;
27507c478bd9Sstevel@tonic-gate 		}
27517c478bd9Sstevel@tonic-gate 	}
27527c478bd9Sstevel@tonic-gate 	cdip = ct->ct_dip;
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_component |= MDI_COMPONENT_CLIENT;
27557c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_client = (caddr_t)ct;
27567c478bd9Sstevel@tonic-gate 
27575e3986cbScth 	MDI_CLIENT_LOCK(ct);
27587c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
27597c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
27607c478bd9Sstevel@tonic-gate 		/*
27617c478bd9Sstevel@tonic-gate 		 * Compare the unit address
27627c478bd9Sstevel@tonic-gate 		 */
27637c478bd9Sstevel@tonic-gate 		if ((MDI_PI(pip)->pi_phci == ph) &&
27647c478bd9Sstevel@tonic-gate 		    strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
27657c478bd9Sstevel@tonic-gate 			break;
27667c478bd9Sstevel@tonic-gate 		}
27677c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
27687c478bd9Sstevel@tonic-gate 	}
27695e3986cbScth 	MDI_CLIENT_UNLOCK(ct);
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
27727c478bd9Sstevel@tonic-gate 		/*
27737c478bd9Sstevel@tonic-gate 		 * This is a new path for this client device.  Allocate and
27747c478bd9Sstevel@tonic-gate 		 * initialize a new pathinfo node
27757c478bd9Sstevel@tonic-gate 		 */
27763c34adc5Sramat 		pip = i_mdi_pi_alloc(ph, paddr, ct);
27773c34adc5Sramat 		ASSERT(pip != NULL);
27783c34adc5Sramat 		path_allocated = 1;
27797c478bd9Sstevel@tonic-gate 	}
27807c478bd9Sstevel@tonic-gate 	rv = MDI_SUCCESS;
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate fail:
27837c478bd9Sstevel@tonic-gate 	/*
27847c478bd9Sstevel@tonic-gate 	 * Release the global mutex.
27857c478bd9Sstevel@tonic-gate 	 */
27865e3986cbScth 	MDI_VHCI_CLIENT_UNLOCK(vh);
27877c478bd9Sstevel@tonic-gate 
27887c478bd9Sstevel@tonic-gate 	/*
27897c478bd9Sstevel@tonic-gate 	 * Mark the pHCI as stable
27907c478bd9Sstevel@tonic-gate 	 */
27917c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
27927c478bd9Sstevel@tonic-gate 	MDI_PHCI_STABLE(ph);
27937c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
27947c478bd9Sstevel@tonic-gate 	*ret_pip = pip;
27953c34adc5Sramat 
27965e3986cbScth 	MDI_DEBUG(2, (CE_NOTE, pdip,
27975e3986cbScth 	    "!mdi_pi_alloc_compatible: alloc %p", (void *)pip));
27985e3986cbScth 
27993c34adc5Sramat 	if (path_allocated)
28003c34adc5Sramat 		vhcache_pi_add(vh->vh_config, MDI_PI(pip));
28013c34adc5Sramat 
28027c478bd9Sstevel@tonic-gate 	return (rv);
28037c478bd9Sstevel@tonic-gate }
28047c478bd9Sstevel@tonic-gate 
28057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
28067c478bd9Sstevel@tonic-gate int
28077c478bd9Sstevel@tonic-gate mdi_pi_alloc(dev_info_t *pdip, char *cname, char *caddr, char *paddr,
28087c478bd9Sstevel@tonic-gate     int flags, mdi_pathinfo_t **ret_pip)
28097c478bd9Sstevel@tonic-gate {
28107c478bd9Sstevel@tonic-gate 	return (mdi_pi_alloc_compatible(pdip, cname, caddr, paddr, NULL, 0,
28117c478bd9Sstevel@tonic-gate 	    flags, ret_pip));
28127c478bd9Sstevel@tonic-gate }
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate /*
28157c478bd9Sstevel@tonic-gate  * i_mdi_pi_alloc():
28167c478bd9Sstevel@tonic-gate  *		Allocate a mdi_pathinfo node and add to the pHCI path list
28177c478bd9Sstevel@tonic-gate  * Return Values:
28187c478bd9Sstevel@tonic-gate  *		mdi_pathinfo
28197c478bd9Sstevel@tonic-gate  */
28207c478bd9Sstevel@tonic-gate /*ARGSUSED*/
28217c478bd9Sstevel@tonic-gate static mdi_pathinfo_t *
28223c34adc5Sramat i_mdi_pi_alloc(mdi_phci_t *ph, char *paddr, mdi_client_t *ct)
28237c478bd9Sstevel@tonic-gate {
28243c34adc5Sramat 	mdi_pathinfo_t	*pip;
28257c478bd9Sstevel@tonic-gate 	int		ct_circular;
28267c478bd9Sstevel@tonic-gate 	int		ph_circular;
2827602ca9eaScth 	static char	path[MAXPATHLEN];
2828602ca9eaScth 	char		*path_persistent;
2829602ca9eaScth 	int		path_instance;
28308c4f8890Srs135747 	int		se_flag;
28318c4f8890Srs135747 	int		kmem_flag;
2832602ca9eaScth 	mod_hash_val_t	hv;
28337c478bd9Sstevel@tonic-gate 
28345e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(ph->ph_vhci));
28355e3986cbScth 
28363c34adc5Sramat 	pip = kmem_zalloc(sizeof (struct mdi_pathinfo), KM_SLEEP);
28377c478bd9Sstevel@tonic-gate 	mutex_init(&MDI_PI(pip)->pi_mutex, NULL, MUTEX_DEFAULT, NULL);
28387c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_state = MDI_PATHINFO_STATE_INIT |
28397c478bd9Sstevel@tonic-gate 	    MDI_PATHINFO_STATE_TRANSIENT;
28407c478bd9Sstevel@tonic-gate 
28417c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_USER_DISABLED(ph))
28427c478bd9Sstevel@tonic-gate 		MDI_PI_SET_USER_DISABLE(pip);
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_DRV_DISABLED_TRANSIENT(ph))
28457c478bd9Sstevel@tonic-gate 		MDI_PI_SET_DRV_DISABLE_TRANS(pip);
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_DRV_DISABLED(ph))
28487c478bd9Sstevel@tonic-gate 		MDI_PI_SET_DRV_DISABLE(pip);
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_old_state = MDI_PATHINFO_STATE_INIT;
28517c478bd9Sstevel@tonic-gate 	cv_init(&MDI_PI(pip)->pi_state_cv, NULL, CV_DEFAULT, NULL);
28527c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client = ct;
28537c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci = ph;
28543c34adc5Sramat 	MDI_PI(pip)->pi_addr = kmem_alloc(strlen(paddr) + 1, KM_SLEEP);
28557c478bd9Sstevel@tonic-gate 	(void) strcpy(MDI_PI(pip)->pi_addr, paddr);
2856602ca9eaScth 
2857602ca9eaScth         /*
2858602ca9eaScth 	 * We form the "path" to the pathinfo node, and see if we have
2859602ca9eaScth 	 * already allocated a 'path_instance' for that "path".  If so,
2860602ca9eaScth 	 * we use the already allocated 'path_instance'.  If not, we
2861602ca9eaScth 	 * allocate a new 'path_instance' and associate it with a copy of
2862602ca9eaScth 	 * the "path" string (which is never freed). The association
2863602ca9eaScth 	 * between a 'path_instance' this "path" string persists until
2864602ca9eaScth 	 * reboot.
2865602ca9eaScth 	 */
2866602ca9eaScth         mutex_enter(&mdi_pathmap_mutex);
2867602ca9eaScth 	(void) ddi_pathname(ph->ph_dip, path);
2868602ca9eaScth 	(void) sprintf(path + strlen(path), "/%s@%s",
2869602ca9eaScth 	    ddi_node_name(ct->ct_dip), MDI_PI(pip)->pi_addr);
2870602ca9eaScth         if (mod_hash_find(mdi_pathmap_bypath, (mod_hash_key_t)path, &hv) == 0) {
2871602ca9eaScth                 path_instance = (uint_t)(intptr_t)hv;
2872602ca9eaScth         } else {
2873602ca9eaScth 		/* allocate a new 'path_instance' and persistent "path" */
2874602ca9eaScth 		path_instance = mdi_pathmap_instance++;
2875602ca9eaScth 		path_persistent = i_ddi_strdup(path, KM_SLEEP);
2876602ca9eaScth                 (void) mod_hash_insert(mdi_pathmap_bypath,
2877602ca9eaScth                     (mod_hash_key_t)path_persistent,
2878602ca9eaScth                     (mod_hash_val_t)(intptr_t)path_instance);
2879602ca9eaScth 		(void) mod_hash_insert(mdi_pathmap_byinstance,
2880602ca9eaScth 		    (mod_hash_key_t)(intptr_t)path_instance,
2881602ca9eaScth 		    (mod_hash_val_t)path_persistent);
2882602ca9eaScth         }
2883602ca9eaScth         mutex_exit(&mdi_pathmap_mutex);
2884602ca9eaScth 	MDI_PI(pip)->pi_path_instance = path_instance;
2885602ca9eaScth 
28863c34adc5Sramat 	(void) nvlist_alloc(&MDI_PI(pip)->pi_prop, NV_UNIQUE_NAME, KM_SLEEP);
28873c34adc5Sramat 	ASSERT(MDI_PI(pip)->pi_prop != NULL);
28887c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_pprivate = NULL;
28897c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_cprivate = NULL;
28907c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_vprivate = NULL;
28917c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client_link = NULL;
28927c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci_link = NULL;
28937c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_ref_cnt = 0;
28947c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_kstats = NULL;
28957c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_preferred = 1;
28967c478bd9Sstevel@tonic-gate 	cv_init(&MDI_PI(pip)->pi_ref_cv, NULL, CV_DEFAULT, NULL);
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 	/*
28997c478bd9Sstevel@tonic-gate 	 * Lock both dev_info nodes against changes in parallel.
29005e3986cbScth 	 *
29015e3986cbScth 	 * The ndi_devi_enter(Client), is atypical since the client is a leaf.
29025e3986cbScth 	 * This atypical operation is done to synchronize pathinfo nodes
29035e3986cbScth 	 * during devinfo snapshot (see di_register_pip) by 'pretending' that
29045e3986cbScth 	 * the pathinfo nodes are children of the Client.
29057c478bd9Sstevel@tonic-gate 	 */
29067c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ct->ct_dip, &ct_circular);
29077c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ph->ph_dip, &ph_circular);
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 	i_mdi_phci_add_path(ph, pip);
29107c478bd9Sstevel@tonic-gate 	i_mdi_client_add_path(ct, pip);
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ph->ph_dip, ph_circular);
29137c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ct->ct_dip, ct_circular);
29147c478bd9Sstevel@tonic-gate 
29158c4f8890Srs135747 	/* determine interrupt context */
29168c4f8890Srs135747 	se_flag = (servicing_interrupt()) ? SE_NOSLEEP : SE_SLEEP;
29178c4f8890Srs135747 	kmem_flag = (se_flag == SE_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
29188c4f8890Srs135747 
29198c4f8890Srs135747 	i_ddi_di_cache_invalidate(kmem_flag);
29208c4f8890Srs135747 
29217c478bd9Sstevel@tonic-gate 	return (pip);
29227c478bd9Sstevel@tonic-gate }
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate /*
2925602ca9eaScth  * mdi_pi_pathname_by_instance():
2926602ca9eaScth  *	Lookup of "path" by 'path_instance'. Return "path".
2927602ca9eaScth  *	NOTE: returned "path" remains valid forever (until reboot).
2928602ca9eaScth  */
2929602ca9eaScth char *
2930602ca9eaScth mdi_pi_pathname_by_instance(int path_instance)
2931602ca9eaScth {
2932602ca9eaScth 	char		*path;
2933602ca9eaScth 	mod_hash_val_t	hv;
2934602ca9eaScth 
2935602ca9eaScth 	/* mdi_pathmap lookup of "path" by 'path_instance' */
2936602ca9eaScth 	mutex_enter(&mdi_pathmap_mutex);
2937602ca9eaScth 	if (mod_hash_find(mdi_pathmap_byinstance,
2938602ca9eaScth 	    (mod_hash_key_t)(intptr_t)path_instance, &hv) == 0)
2939602ca9eaScth 		path = (char *)hv;
2940602ca9eaScth 	else
2941602ca9eaScth 		path = NULL;
2942602ca9eaScth 	mutex_exit(&mdi_pathmap_mutex);
2943602ca9eaScth 	return (path);
2944602ca9eaScth }
2945602ca9eaScth 
2946602ca9eaScth /*
29477c478bd9Sstevel@tonic-gate  * i_mdi_phci_add_path():
29487c478bd9Sstevel@tonic-gate  * 		Add a mdi_pathinfo node to pHCI list.
29497c478bd9Sstevel@tonic-gate  * Notes:
29507c478bd9Sstevel@tonic-gate  *		Caller should per-pHCI mutex
29517c478bd9Sstevel@tonic-gate  */
29527c478bd9Sstevel@tonic-gate static void
29537c478bd9Sstevel@tonic-gate i_mdi_phci_add_path(mdi_phci_t *ph, mdi_pathinfo_t *pip)
29547c478bd9Sstevel@tonic-gate {
29557c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ph->ph_dip));
29567c478bd9Sstevel@tonic-gate 
29575e3986cbScth 	MDI_PHCI_LOCK(ph);
29587c478bd9Sstevel@tonic-gate 	if (ph->ph_path_head == NULL) {
29597c478bd9Sstevel@tonic-gate 		ph->ph_path_head = pip;
29607c478bd9Sstevel@tonic-gate 	} else {
29617c478bd9Sstevel@tonic-gate 		MDI_PI(ph->ph_path_tail)->pi_phci_link = MDI_PI(pip);
29627c478bd9Sstevel@tonic-gate 	}
29637c478bd9Sstevel@tonic-gate 	ph->ph_path_tail = pip;
29647c478bd9Sstevel@tonic-gate 	ph->ph_path_count++;
29655e3986cbScth 	MDI_PHCI_UNLOCK(ph);
29667c478bd9Sstevel@tonic-gate }
29677c478bd9Sstevel@tonic-gate 
29687c478bd9Sstevel@tonic-gate /*
29697c478bd9Sstevel@tonic-gate  * i_mdi_client_add_path():
29707c478bd9Sstevel@tonic-gate  *		Add mdi_pathinfo node to client list
29717c478bd9Sstevel@tonic-gate  */
29727c478bd9Sstevel@tonic-gate static void
29737c478bd9Sstevel@tonic-gate i_mdi_client_add_path(mdi_client_t *ct, mdi_pathinfo_t *pip)
29747c478bd9Sstevel@tonic-gate {
29757c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ct->ct_dip));
29767c478bd9Sstevel@tonic-gate 
29775e3986cbScth 	MDI_CLIENT_LOCK(ct);
29787c478bd9Sstevel@tonic-gate 	if (ct->ct_path_head == NULL) {
29797c478bd9Sstevel@tonic-gate 		ct->ct_path_head = pip;
29807c478bd9Sstevel@tonic-gate 	} else {
29817c478bd9Sstevel@tonic-gate 		MDI_PI(ct->ct_path_tail)->pi_client_link = MDI_PI(pip);
29827c478bd9Sstevel@tonic-gate 	}
29837c478bd9Sstevel@tonic-gate 	ct->ct_path_tail = pip;
29847c478bd9Sstevel@tonic-gate 	ct->ct_path_count++;
29855e3986cbScth 	MDI_CLIENT_UNLOCK(ct);
29867c478bd9Sstevel@tonic-gate }
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate /*
29897c478bd9Sstevel@tonic-gate  * mdi_pi_free():
29907c478bd9Sstevel@tonic-gate  *		Free the mdi_pathinfo node and also client device node if this
29917c478bd9Sstevel@tonic-gate  *		is the last path to the device
29927c478bd9Sstevel@tonic-gate  * Return Values:
29937c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
29947c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
29957c478bd9Sstevel@tonic-gate  *		MDI_BUSY
29967c478bd9Sstevel@tonic-gate  */
29977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
29987c478bd9Sstevel@tonic-gate int
29997c478bd9Sstevel@tonic-gate mdi_pi_free(mdi_pathinfo_t *pip, int flags)
30007c478bd9Sstevel@tonic-gate {
3001e9b79356Srs135747 	int		rv = MDI_FAILURE;
30027c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
30037c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
30047c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
30057c478bd9Sstevel@tonic-gate 	int		(*f)();
30067c478bd9Sstevel@tonic-gate 	int		client_held = 0;
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
30097c478bd9Sstevel@tonic-gate 	ph = MDI_PI(pip)->pi_phci;
30107c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
30117c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
30127c478bd9Sstevel@tonic-gate 		/*
30137c478bd9Sstevel@tonic-gate 		 * Invalid pHCI device, return failure
30147c478bd9Sstevel@tonic-gate 		 */
30157c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, NULL,
30165e3986cbScth 		    "!mdi_pi_free: invalid pHCI pip=%p", (void *)pip));
30177c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
30187c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
30197c478bd9Sstevel@tonic-gate 	}
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
30227c478bd9Sstevel@tonic-gate 	ASSERT(vh != NULL);
30237c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
30247c478bd9Sstevel@tonic-gate 		/* Invalid pHCI device, return failure */
30257c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, NULL,
30265e3986cbScth 		    "!mdi_pi_free: invalid vHCI pip=%p", (void *)pip));
30277c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
30287c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
30297c478bd9Sstevel@tonic-gate 	}
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate 	ct = MDI_PI(pip)->pi_client;
30327c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
30337c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
30347c478bd9Sstevel@tonic-gate 		/*
30357c478bd9Sstevel@tonic-gate 		 * Invalid Client device, return failure
30367c478bd9Sstevel@tonic-gate 		 */
30377c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, NULL,
30385e3986cbScth 		    "!mdi_pi_free: invalid client pip=%p", (void *)pip));
30397c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
30407c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
30417c478bd9Sstevel@tonic-gate 	}
30427c478bd9Sstevel@tonic-gate 
30437c478bd9Sstevel@tonic-gate 	/*
30447c478bd9Sstevel@tonic-gate 	 * Check to see for busy condition.  A mdi_pathinfo can only be freed
30457c478bd9Sstevel@tonic-gate 	 * if the node state is either offline or init and the reference count
30467c478bd9Sstevel@tonic-gate 	 * is zero.
30477c478bd9Sstevel@tonic-gate 	 */
30487c478bd9Sstevel@tonic-gate 	if (!(MDI_PI_IS_OFFLINE(pip) || MDI_PI_IS_INIT(pip) ||
30497c478bd9Sstevel@tonic-gate 	    MDI_PI_IS_INITING(pip))) {
30507c478bd9Sstevel@tonic-gate 		/*
30517c478bd9Sstevel@tonic-gate 		 * Node is busy
30527c478bd9Sstevel@tonic-gate 		 */
30535e3986cbScth 		MDI_DEBUG(1, (CE_WARN, ct->ct_dip,
30545e3986cbScth 		    "!mdi_pi_free: pathinfo node is busy pip=%p", (void *)pip));
30557c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
30567c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
30577c478bd9Sstevel@tonic-gate 	}
30587c478bd9Sstevel@tonic-gate 
30597c478bd9Sstevel@tonic-gate 	while (MDI_PI(pip)->pi_ref_cnt != 0) {
30607c478bd9Sstevel@tonic-gate 		/*
30617c478bd9Sstevel@tonic-gate 		 * Give a chance for pending I/Os to complete.
30627c478bd9Sstevel@tonic-gate 		 */
30635e3986cbScth 		MDI_DEBUG(1, (CE_NOTE, ct->ct_dip, "!mdi_pi_free: "
30647c478bd9Sstevel@tonic-gate 		    "%d cmds still pending on path: %p\n",
30655e3986cbScth 		    MDI_PI(pip)->pi_ref_cnt, (void *)pip));
30667c478bd9Sstevel@tonic-gate 		if (cv_timedwait(&MDI_PI(pip)->pi_ref_cv,
30677c478bd9Sstevel@tonic-gate 		    &MDI_PI(pip)->pi_mutex,
30687c478bd9Sstevel@tonic-gate 		    ddi_get_lbolt() + drv_usectohz(60 * 1000000)) == -1) {
30697c478bd9Sstevel@tonic-gate 			/*
30707c478bd9Sstevel@tonic-gate 			 * The timeout time reached without ref_cnt being zero
30717c478bd9Sstevel@tonic-gate 			 * being signaled.
30727c478bd9Sstevel@tonic-gate 			 */
30735e3986cbScth 			MDI_DEBUG(1, (CE_NOTE, ct->ct_dip,
30748c4f8890Srs135747 			    "!mdi_pi_free: "
30757c478bd9Sstevel@tonic-gate 			    "Timeout reached on path %p without the cond\n",
30765e3986cbScth 			    (void *)pip));
30775e3986cbScth 			MDI_DEBUG(1, (CE_NOTE, ct->ct_dip,
30788c4f8890Srs135747 			    "!mdi_pi_free: "
30797c478bd9Sstevel@tonic-gate 			    "%d cmds still pending on path: %p\n",
30805e3986cbScth 			    MDI_PI(pip)->pi_ref_cnt, (void *)pip));
30817c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
30827c478bd9Sstevel@tonic-gate 			return (MDI_BUSY);
30837c478bd9Sstevel@tonic-gate 		}
30847c478bd9Sstevel@tonic-gate 	}
30857c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held) {
30867c478bd9Sstevel@tonic-gate 		client_held = 1;
30877c478bd9Sstevel@tonic-gate 	}
30887c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
30897c478bd9Sstevel@tonic-gate 
30903c34adc5Sramat 	vhcache_pi_remove(vh->vh_config, MDI_PI(pip));
30913c34adc5Sramat 
30927c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
30937c478bd9Sstevel@tonic-gate 
30945e3986cbScth 	/* Prevent further failovers till MDI_VHCI_CLIENT_LOCK is held */
30957c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_PATH_FREE_IN_PROGRESS(ct);
30967c478bd9Sstevel@tonic-gate 
30977c478bd9Sstevel@tonic-gate 	/*
30987c478bd9Sstevel@tonic-gate 	 * Wait till failover is complete before removing this node.
30997c478bd9Sstevel@tonic-gate 	 */
31007c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct))
31017c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_failover_cv, &ct->ct_mutex);
31027c478bd9Sstevel@tonic-gate 
31037c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
31045e3986cbScth 	MDI_VHCI_CLIENT_LOCK(vh);
31057c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
31067c478bd9Sstevel@tonic-gate 	MDI_CLIENT_CLEAR_PATH_FREE_IN_PROGRESS(ct);
31077c478bd9Sstevel@tonic-gate 
31087c478bd9Sstevel@tonic-gate 	if (!MDI_PI_IS_INITING(pip)) {
31097c478bd9Sstevel@tonic-gate 		f = vh->vh_ops->vo_pi_uninit;
31107c478bd9Sstevel@tonic-gate 		if (f != NULL) {
31117c478bd9Sstevel@tonic-gate 			rv = (*f)(vh->vh_dip, pip, 0);
31127c478bd9Sstevel@tonic-gate 		}
31137c478bd9Sstevel@tonic-gate 	}
31147c478bd9Sstevel@tonic-gate 	/*
31157c478bd9Sstevel@tonic-gate 	 * If vo_pi_uninit() completed successfully.
31167c478bd9Sstevel@tonic-gate 	 */
31177c478bd9Sstevel@tonic-gate 	if (rv == MDI_SUCCESS) {
31187c478bd9Sstevel@tonic-gate 		if (client_held) {
31197c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, ct->ct_dip, "mdi_pi_free "
31207c478bd9Sstevel@tonic-gate 			    "i_mdi_pm_rele_client\n"));
31217c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, 1);
31227c478bd9Sstevel@tonic-gate 		}
31237c478bd9Sstevel@tonic-gate 		i_mdi_pi_free(ph, pip, ct);
31247c478bd9Sstevel@tonic-gate 		if (ct->ct_path_count == 0) {
31257c478bd9Sstevel@tonic-gate 			/*
31267c478bd9Sstevel@tonic-gate 			 * Client lost its last path.
31277c478bd9Sstevel@tonic-gate 			 * Clean up the client device
31287c478bd9Sstevel@tonic-gate 			 */
31297c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
31307c478bd9Sstevel@tonic-gate 			(void) i_mdi_client_free(ct->ct_vhci, ct);
31315e3986cbScth 			MDI_VHCI_CLIENT_UNLOCK(vh);
31327c478bd9Sstevel@tonic-gate 			return (rv);
31337c478bd9Sstevel@tonic-gate 		}
31347c478bd9Sstevel@tonic-gate 	}
31357c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
31365e3986cbScth 	MDI_VHCI_CLIENT_UNLOCK(vh);
31373c34adc5Sramat 
31383c34adc5Sramat 	if (rv == MDI_FAILURE)
31393c34adc5Sramat 		vhcache_pi_add(vh->vh_config, MDI_PI(pip));
31403c34adc5Sramat 
31417c478bd9Sstevel@tonic-gate 	return (rv);
31427c478bd9Sstevel@tonic-gate }
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate /*
31457c478bd9Sstevel@tonic-gate  * i_mdi_pi_free():
31467c478bd9Sstevel@tonic-gate  *		Free the mdi_pathinfo node
31477c478bd9Sstevel@tonic-gate  */
31487c478bd9Sstevel@tonic-gate static void
31497c478bd9Sstevel@tonic-gate i_mdi_pi_free(mdi_phci_t *ph, mdi_pathinfo_t *pip, mdi_client_t *ct)
31507c478bd9Sstevel@tonic-gate {
31517c478bd9Sstevel@tonic-gate 	int	ct_circular;
31527c478bd9Sstevel@tonic-gate 	int	ph_circular;
31538c4f8890Srs135747 	int	se_flag;
31548c4f8890Srs135747 	int	kmem_flag;
31557c478bd9Sstevel@tonic-gate 
31565e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
31575e3986cbScth 
31587c478bd9Sstevel@tonic-gate 	/*
31597c478bd9Sstevel@tonic-gate 	 * remove any per-path kstats
31607c478bd9Sstevel@tonic-gate 	 */
31617c478bd9Sstevel@tonic-gate 	i_mdi_pi_kstat_destroy(pip);
31627c478bd9Sstevel@tonic-gate 
31635e3986cbScth 	/* See comments in i_mdi_pi_alloc() */
31647c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ct->ct_dip, &ct_circular);
31657c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ph->ph_dip, &ph_circular);
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 	i_mdi_client_remove_path(ct, pip);
31687c478bd9Sstevel@tonic-gate 	i_mdi_phci_remove_path(ph, pip);
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ph->ph_dip, ph_circular);
31717c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ct->ct_dip, ct_circular);
31727c478bd9Sstevel@tonic-gate 
31738c4f8890Srs135747 	/* determine interrupt context */
31748c4f8890Srs135747 	se_flag = (servicing_interrupt()) ? SE_NOSLEEP : SE_SLEEP;
31758c4f8890Srs135747 	kmem_flag = (se_flag == SE_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
31768c4f8890Srs135747 
31778c4f8890Srs135747 	i_ddi_di_cache_invalidate(kmem_flag);
31788c4f8890Srs135747 
31797c478bd9Sstevel@tonic-gate 	mutex_destroy(&MDI_PI(pip)->pi_mutex);
31807c478bd9Sstevel@tonic-gate 	cv_destroy(&MDI_PI(pip)->pi_state_cv);
31817c478bd9Sstevel@tonic-gate 	cv_destroy(&MDI_PI(pip)->pi_ref_cv);
31827c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_addr) {
31837c478bd9Sstevel@tonic-gate 		kmem_free(MDI_PI(pip)->pi_addr,
31847c478bd9Sstevel@tonic-gate 		    strlen(MDI_PI(pip)->pi_addr) + 1);
31857c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_addr = NULL;
31867c478bd9Sstevel@tonic-gate 	}
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop) {
31897c478bd9Sstevel@tonic-gate 		(void) nvlist_free(MDI_PI(pip)->pi_prop);
31907c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_prop = NULL;
31917c478bd9Sstevel@tonic-gate 	}
31927c478bd9Sstevel@tonic-gate 	kmem_free(pip, sizeof (struct mdi_pathinfo));
31937c478bd9Sstevel@tonic-gate }
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 
31967c478bd9Sstevel@tonic-gate /*
31977c478bd9Sstevel@tonic-gate  * i_mdi_phci_remove_path():
31987c478bd9Sstevel@tonic-gate  * 		Remove a mdi_pathinfo node from pHCI list.
31997c478bd9Sstevel@tonic-gate  * Notes:
32007c478bd9Sstevel@tonic-gate  *		Caller should hold per-pHCI mutex
32017c478bd9Sstevel@tonic-gate  */
32027c478bd9Sstevel@tonic-gate static void
32037c478bd9Sstevel@tonic-gate i_mdi_phci_remove_path(mdi_phci_t *ph, mdi_pathinfo_t *pip)
32047c478bd9Sstevel@tonic-gate {
32057c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*prev = NULL;
32067c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*path = NULL;
32077c478bd9Sstevel@tonic-gate 
32087c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ph->ph_dip));
32097c478bd9Sstevel@tonic-gate 
32105e3986cbScth 	MDI_PHCI_LOCK(ph);
32117c478bd9Sstevel@tonic-gate 	path = ph->ph_path_head;
32127c478bd9Sstevel@tonic-gate 	while (path != NULL) {
32137c478bd9Sstevel@tonic-gate 		if (path == pip) {
32147c478bd9Sstevel@tonic-gate 			break;
32157c478bd9Sstevel@tonic-gate 		}
32167c478bd9Sstevel@tonic-gate 		prev = path;
32177c478bd9Sstevel@tonic-gate 		path = (mdi_pathinfo_t *)MDI_PI(path)->pi_phci_link;
32187c478bd9Sstevel@tonic-gate 	}
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate 	if (path) {
32217c478bd9Sstevel@tonic-gate 		ph->ph_path_count--;
32227c478bd9Sstevel@tonic-gate 		if (prev) {
32237c478bd9Sstevel@tonic-gate 			MDI_PI(prev)->pi_phci_link = MDI_PI(path)->pi_phci_link;
32247c478bd9Sstevel@tonic-gate 		} else {
32257c478bd9Sstevel@tonic-gate 			ph->ph_path_head =
32267c478bd9Sstevel@tonic-gate 			    (mdi_pathinfo_t *)MDI_PI(path)->pi_phci_link;
32277c478bd9Sstevel@tonic-gate 		}
32287c478bd9Sstevel@tonic-gate 		if (ph->ph_path_tail == path) {
32297c478bd9Sstevel@tonic-gate 			ph->ph_path_tail = prev;
32307c478bd9Sstevel@tonic-gate 		}
32317c478bd9Sstevel@tonic-gate 	}
32327c478bd9Sstevel@tonic-gate 
32337c478bd9Sstevel@tonic-gate 	/*
32347c478bd9Sstevel@tonic-gate 	 * Clear the pHCI link
32357c478bd9Sstevel@tonic-gate 	 */
32367c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci_link = NULL;
32377c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci = NULL;
32385e3986cbScth 	MDI_PHCI_UNLOCK(ph);
32397c478bd9Sstevel@tonic-gate }
32407c478bd9Sstevel@tonic-gate 
32417c478bd9Sstevel@tonic-gate /*
32427c478bd9Sstevel@tonic-gate  * i_mdi_client_remove_path():
32437c478bd9Sstevel@tonic-gate  * 		Remove a mdi_pathinfo node from client path list.
32447c478bd9Sstevel@tonic-gate  */
32457c478bd9Sstevel@tonic-gate static void
32467c478bd9Sstevel@tonic-gate i_mdi_client_remove_path(mdi_client_t *ct, mdi_pathinfo_t *pip)
32477c478bd9Sstevel@tonic-gate {
32487c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*prev = NULL;
32497c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*path;
32507c478bd9Sstevel@tonic-gate 
32517c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ct->ct_dip));
32527c478bd9Sstevel@tonic-gate 
32535e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
32547c478bd9Sstevel@tonic-gate 	path = ct->ct_path_head;
32557c478bd9Sstevel@tonic-gate 	while (path != NULL) {
32567c478bd9Sstevel@tonic-gate 		if (path == pip) {
32577c478bd9Sstevel@tonic-gate 			break;
32587c478bd9Sstevel@tonic-gate 		}
32597c478bd9Sstevel@tonic-gate 		prev = path;
32607c478bd9Sstevel@tonic-gate 		path = (mdi_pathinfo_t *)MDI_PI(path)->pi_client_link;
32617c478bd9Sstevel@tonic-gate 	}
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate 	if (path) {
32647c478bd9Sstevel@tonic-gate 		ct->ct_path_count--;
32657c478bd9Sstevel@tonic-gate 		if (prev) {
32667c478bd9Sstevel@tonic-gate 			MDI_PI(prev)->pi_client_link =
32677c478bd9Sstevel@tonic-gate 			    MDI_PI(path)->pi_client_link;
32687c478bd9Sstevel@tonic-gate 		} else {
32697c478bd9Sstevel@tonic-gate 			ct->ct_path_head =
32707c478bd9Sstevel@tonic-gate 			    (mdi_pathinfo_t *)MDI_PI(path)->pi_client_link;
32717c478bd9Sstevel@tonic-gate 		}
32727c478bd9Sstevel@tonic-gate 		if (ct->ct_path_tail == path) {
32737c478bd9Sstevel@tonic-gate 			ct->ct_path_tail = prev;
32747c478bd9Sstevel@tonic-gate 		}
32757c478bd9Sstevel@tonic-gate 		if (ct->ct_path_last == path) {
32767c478bd9Sstevel@tonic-gate 			ct->ct_path_last = ct->ct_path_head;
32777c478bd9Sstevel@tonic-gate 		}
32787c478bd9Sstevel@tonic-gate 	}
32797c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client_link = NULL;
32807c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client = NULL;
32817c478bd9Sstevel@tonic-gate }
32827c478bd9Sstevel@tonic-gate 
32837c478bd9Sstevel@tonic-gate /*
32847c478bd9Sstevel@tonic-gate  * i_mdi_pi_state_change():
32857c478bd9Sstevel@tonic-gate  *		online a mdi_pathinfo node
32867c478bd9Sstevel@tonic-gate  *
32877c478bd9Sstevel@tonic-gate  * Return Values:
32887c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
32897c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
32907c478bd9Sstevel@tonic-gate  */
32917c478bd9Sstevel@tonic-gate /*ARGSUSED*/
32927c478bd9Sstevel@tonic-gate static int
32937c478bd9Sstevel@tonic-gate i_mdi_pi_state_change(mdi_pathinfo_t *pip, mdi_pathinfo_state_t state, int flag)
32947c478bd9Sstevel@tonic-gate {
32957c478bd9Sstevel@tonic-gate 	int		rv = MDI_SUCCESS;
32967c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
32977c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
32987c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
32997c478bd9Sstevel@tonic-gate 	int		(*f)();
33007c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate 	ph = MDI_PI(pip)->pi_phci;
33057c478bd9Sstevel@tonic-gate 	ASSERT(ph);
33067c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
33077c478bd9Sstevel@tonic-gate 		/*
33087c478bd9Sstevel@tonic-gate 		 * Invalid pHCI device, fail the request
33097c478bd9Sstevel@tonic-gate 		 */
33107c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
33117c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, NULL,
33125e3986cbScth 		    "!mdi_pi_state_change: invalid phci pip=%p", (void *)pip));
33137c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
33147c478bd9Sstevel@tonic-gate 	}
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
33177c478bd9Sstevel@tonic-gate 	ASSERT(vh);
33187c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
33197c478bd9Sstevel@tonic-gate 		/*
33207c478bd9Sstevel@tonic-gate 		 * Invalid vHCI device, fail the request
33217c478bd9Sstevel@tonic-gate 		 */
33227c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
33237c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, NULL,
33245e3986cbScth 		    "!mdi_pi_state_change: invalid vhci pip=%p", (void *)pip));
33257c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
33267c478bd9Sstevel@tonic-gate 	}
33277c478bd9Sstevel@tonic-gate 
33287c478bd9Sstevel@tonic-gate 	ct = MDI_PI(pip)->pi_client;
33297c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
33307c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
33317c478bd9Sstevel@tonic-gate 		/*
33327c478bd9Sstevel@tonic-gate 		 * Invalid client device, fail the request
33337c478bd9Sstevel@tonic-gate 		 */
33347c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
33357c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, NULL,
33365e3986cbScth 		    "!mdi_pi_state_change: invalid client pip=%p",
33375e3986cbScth 		    (void *)pip));
33387c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
33397c478bd9Sstevel@tonic-gate 	}
33407c478bd9Sstevel@tonic-gate 
33417c478bd9Sstevel@tonic-gate 	/*
33427c478bd9Sstevel@tonic-gate 	 * If this path has not been initialized yet, Callback vHCI driver's
33437c478bd9Sstevel@tonic-gate 	 * pathinfo node initialize entry point
33447c478bd9Sstevel@tonic-gate 	 */
33457c478bd9Sstevel@tonic-gate 
33467c478bd9Sstevel@tonic-gate 	if (MDI_PI_IS_INITING(pip)) {
33477c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
33487c478bd9Sstevel@tonic-gate 		f = vh->vh_ops->vo_pi_init;
33497c478bd9Sstevel@tonic-gate 		if (f != NULL) {
33507c478bd9Sstevel@tonic-gate 			rv = (*f)(vh->vh_dip, pip, 0);
33517c478bd9Sstevel@tonic-gate 			if (rv != MDI_SUCCESS) {
33525e3986cbScth 				MDI_DEBUG(1, (CE_WARN, ct->ct_dip,
33537c478bd9Sstevel@tonic-gate 				    "!vo_pi_init: failed vHCI=0x%p, pip=0x%p",
33545e3986cbScth 				    (void *)vh, (void *)pip));
33557c478bd9Sstevel@tonic-gate 				return (MDI_FAILURE);
33567c478bd9Sstevel@tonic-gate 			}
33577c478bd9Sstevel@tonic-gate 		}
33587c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
33597c478bd9Sstevel@tonic-gate 		MDI_PI_CLEAR_TRANSIENT(pip);
33607c478bd9Sstevel@tonic-gate 	}
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate 	/*
33637c478bd9Sstevel@tonic-gate 	 * Do not allow state transition when pHCI is in offline/suspended
33647c478bd9Sstevel@tonic-gate 	 * states
33657c478bd9Sstevel@tonic-gate 	 */
33667c478bd9Sstevel@tonic-gate 	i_mdi_phci_lock(ph, pip);
33677c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_READY(ph) == 0) {
33685e3986cbScth 		MDI_DEBUG(1, (CE_WARN, ct->ct_dip,
33695e3986cbScth 		    "!mdi_pi_state_change: pHCI not ready, pHCI=%p",
33705e3986cbScth 		    (void *)ph));
33717c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
33727c478bd9Sstevel@tonic-gate 		i_mdi_phci_unlock(ph);
33737c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
33747c478bd9Sstevel@tonic-gate 	}
33757c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNSTABLE(ph);
33767c478bd9Sstevel@tonic-gate 	i_mdi_phci_unlock(ph);
33777c478bd9Sstevel@tonic-gate 
33787c478bd9Sstevel@tonic-gate 	/*
33797c478bd9Sstevel@tonic-gate 	 * Check if mdi_pathinfo state is in transient state.
33807c478bd9Sstevel@tonic-gate 	 * If yes, offlining is in progress and wait till transient state is
33817c478bd9Sstevel@tonic-gate 	 * cleared.
33827c478bd9Sstevel@tonic-gate 	 */
33837c478bd9Sstevel@tonic-gate 	if (MDI_PI_IS_TRANSIENT(pip)) {
33847c478bd9Sstevel@tonic-gate 		while (MDI_PI_IS_TRANSIENT(pip)) {
33857c478bd9Sstevel@tonic-gate 			cv_wait(&MDI_PI(pip)->pi_state_cv,
33867c478bd9Sstevel@tonic-gate 			    &MDI_PI(pip)->pi_mutex);
33877c478bd9Sstevel@tonic-gate 		}
33887c478bd9Sstevel@tonic-gate 	}
33897c478bd9Sstevel@tonic-gate 
33907c478bd9Sstevel@tonic-gate 	/*
33917c478bd9Sstevel@tonic-gate 	 * Grab the client lock in reverse order sequence and release the
33927c478bd9Sstevel@tonic-gate 	 * mdi_pathinfo mutex.
33937c478bd9Sstevel@tonic-gate 	 */
33947c478bd9Sstevel@tonic-gate 	i_mdi_client_lock(ct, pip);
33957c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
33967c478bd9Sstevel@tonic-gate 
33977c478bd9Sstevel@tonic-gate 	/*
33987c478bd9Sstevel@tonic-gate 	 * Wait till failover state is cleared
33997c478bd9Sstevel@tonic-gate 	 */
34007c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct))
34017c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_failover_cv, &ct->ct_mutex);
34027c478bd9Sstevel@tonic-gate 
34037c478bd9Sstevel@tonic-gate 	/*
34047c478bd9Sstevel@tonic-gate 	 * Mark the mdi_pathinfo node state as transient
34057c478bd9Sstevel@tonic-gate 	 */
34067c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
34077c478bd9Sstevel@tonic-gate 	switch (state) {
34087c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_ONLINE:
34097c478bd9Sstevel@tonic-gate 		MDI_PI_SET_ONLINING(pip);
34107c478bd9Sstevel@tonic-gate 		break;
34117c478bd9Sstevel@tonic-gate 
34127c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_STANDBY:
34137c478bd9Sstevel@tonic-gate 		MDI_PI_SET_STANDBYING(pip);
34147c478bd9Sstevel@tonic-gate 		break;
34157c478bd9Sstevel@tonic-gate 
34167c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_FAULT:
34177c478bd9Sstevel@tonic-gate 		/*
34187c478bd9Sstevel@tonic-gate 		 * Mark the pathinfo state as FAULTED
34197c478bd9Sstevel@tonic-gate 		 */
34207c478bd9Sstevel@tonic-gate 		MDI_PI_SET_FAULTING(pip);
34217c478bd9Sstevel@tonic-gate 		MDI_PI_ERRSTAT(pip, MDI_PI_HARDERR);
34227c478bd9Sstevel@tonic-gate 		break;
34237c478bd9Sstevel@tonic-gate 
34247c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_OFFLINE:
34257c478bd9Sstevel@tonic-gate 		/*
34267c478bd9Sstevel@tonic-gate 		 * ndi_devi_offline() cannot hold pip or ct locks.
34277c478bd9Sstevel@tonic-gate 		 */
34287c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
34297c478bd9Sstevel@tonic-gate 		/*
3430fb3838e9Srb150246 		 * Don't offline the client dev_info node unless we have
3431fb3838e9Srb150246 		 * no available paths left at all.
34327c478bd9Sstevel@tonic-gate 		 */
34337c478bd9Sstevel@tonic-gate 		cdip = ct->ct_dip;
34347c478bd9Sstevel@tonic-gate 		if ((flag & NDI_DEVI_REMOVE) &&
3435fb3838e9Srb150246 		    (ct->ct_path_count == 1)) {
34367c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
34377c478bd9Sstevel@tonic-gate 			rv = ndi_devi_offline(cdip, 0);
34387c478bd9Sstevel@tonic-gate 			if (rv != NDI_SUCCESS) {
34397c478bd9Sstevel@tonic-gate 				/*
34407c478bd9Sstevel@tonic-gate 				 * Convert to MDI error code
34417c478bd9Sstevel@tonic-gate 				 */
34427c478bd9Sstevel@tonic-gate 				switch (rv) {
34437c478bd9Sstevel@tonic-gate 				case NDI_BUSY:
34447c478bd9Sstevel@tonic-gate 					rv = MDI_BUSY;
34457c478bd9Sstevel@tonic-gate 					break;
34467c478bd9Sstevel@tonic-gate 				default:
34477c478bd9Sstevel@tonic-gate 					rv = MDI_FAILURE;
34487c478bd9Sstevel@tonic-gate 					break;
34497c478bd9Sstevel@tonic-gate 				}
34507c478bd9Sstevel@tonic-gate 				goto state_change_exit;
34517c478bd9Sstevel@tonic-gate 			} else {
34527c478bd9Sstevel@tonic-gate 				i_mdi_client_lock(ct, NULL);
34537c478bd9Sstevel@tonic-gate 			}
34547c478bd9Sstevel@tonic-gate 		}
34557c478bd9Sstevel@tonic-gate 		/*
34567c478bd9Sstevel@tonic-gate 		 * Mark the mdi_pathinfo node state as transient
34577c478bd9Sstevel@tonic-gate 		 */
34587c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
34597c478bd9Sstevel@tonic-gate 		MDI_PI_SET_OFFLINING(pip);
34607c478bd9Sstevel@tonic-gate 		break;
34617c478bd9Sstevel@tonic-gate 	}
34627c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
34637c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNSTABLE(ct);
34647c478bd9Sstevel@tonic-gate 	i_mdi_client_unlock(ct);
34657c478bd9Sstevel@tonic-gate 
34667c478bd9Sstevel@tonic-gate 	f = vh->vh_ops->vo_pi_state_change;
34675e3986cbScth 	if (f != NULL)
34687c478bd9Sstevel@tonic-gate 		rv = (*f)(vh->vh_dip, pip, state, 0, flag);
34695e3986cbScth 
34705e3986cbScth 	MDI_CLIENT_LOCK(ct);
34715e3986cbScth 	MDI_PI_LOCK(pip);
34727c478bd9Sstevel@tonic-gate 	if (rv == MDI_NOT_SUPPORTED) {
34737c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_DEV_NOT_SUPPORTED(ct);
34747c478bd9Sstevel@tonic-gate 	}
34757c478bd9Sstevel@tonic-gate 	if (rv != MDI_SUCCESS) {
34765e3986cbScth 		MDI_DEBUG(2, (CE_WARN, ct->ct_dip,
34777c478bd9Sstevel@tonic-gate 		    "!vo_pi_state_change: failed rv = %x", rv));
34787c478bd9Sstevel@tonic-gate 	}
34797c478bd9Sstevel@tonic-gate 	if (MDI_PI_IS_TRANSIENT(pip)) {
34807c478bd9Sstevel@tonic-gate 		if (rv == MDI_SUCCESS) {
34817c478bd9Sstevel@tonic-gate 			MDI_PI_CLEAR_TRANSIENT(pip);
34827c478bd9Sstevel@tonic-gate 		} else {
34837c478bd9Sstevel@tonic-gate 			MDI_PI(pip)->pi_state = MDI_PI_OLD_STATE(pip);
34847c478bd9Sstevel@tonic-gate 		}
34857c478bd9Sstevel@tonic-gate 	}
34867c478bd9Sstevel@tonic-gate 
34877c478bd9Sstevel@tonic-gate 	/*
34887c478bd9Sstevel@tonic-gate 	 * Wake anyone waiting for this mdi_pathinfo node
34897c478bd9Sstevel@tonic-gate 	 */
34907c478bd9Sstevel@tonic-gate 	cv_broadcast(&MDI_PI(pip)->pi_state_cv);
34917c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
34927c478bd9Sstevel@tonic-gate 
34937c478bd9Sstevel@tonic-gate 	/*
34947c478bd9Sstevel@tonic-gate 	 * Mark the client device as stable
34957c478bd9Sstevel@tonic-gate 	 */
34967c478bd9Sstevel@tonic-gate 	MDI_CLIENT_STABLE(ct);
34977c478bd9Sstevel@tonic-gate 	if (rv == MDI_SUCCESS) {
34987c478bd9Sstevel@tonic-gate 		if (ct->ct_unstable == 0) {
34997c478bd9Sstevel@tonic-gate 			cdip = ct->ct_dip;
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate 			/*
35027c478bd9Sstevel@tonic-gate 			 * Onlining the mdi_pathinfo node will impact the
35037c478bd9Sstevel@tonic-gate 			 * client state Update the client and dev_info node
35047c478bd9Sstevel@tonic-gate 			 * state accordingly
35057c478bd9Sstevel@tonic-gate 			 */
35067c478bd9Sstevel@tonic-gate 			rv = NDI_SUCCESS;
35077c478bd9Sstevel@tonic-gate 			i_mdi_client_update_state(ct);
35087c478bd9Sstevel@tonic-gate 			switch (MDI_CLIENT_STATE(ct)) {
35097c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_OPTIMAL:
35107c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_DEGRADED:
3511737d277aScth 				if (cdip && !i_ddi_devi_attached(cdip) &&
35127c478bd9Sstevel@tonic-gate 				    ((state == MDI_PATHINFO_STATE_ONLINE) ||
35137c478bd9Sstevel@tonic-gate 				    (state == MDI_PATHINFO_STATE_STANDBY))) {
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate 					/*
35167c478bd9Sstevel@tonic-gate 					 * Must do ndi_devi_online() through
35177c478bd9Sstevel@tonic-gate 					 * hotplug thread for deferred
35187c478bd9Sstevel@tonic-gate 					 * attach mechanism to work
35197c478bd9Sstevel@tonic-gate 					 */
35205e3986cbScth 					MDI_CLIENT_UNLOCK(ct);
35217c478bd9Sstevel@tonic-gate 					rv = ndi_devi_online(cdip, 0);
35225e3986cbScth 					MDI_CLIENT_LOCK(ct);
35237c478bd9Sstevel@tonic-gate 					if ((rv != NDI_SUCCESS) &&
35247c478bd9Sstevel@tonic-gate 					    (MDI_CLIENT_STATE(ct) ==
35257c478bd9Sstevel@tonic-gate 					    MDI_CLIENT_STATE_DEGRADED)) {
35267c478bd9Sstevel@tonic-gate 						/*
35277c478bd9Sstevel@tonic-gate 						 * ndi_devi_online failed.
35287c478bd9Sstevel@tonic-gate 						 * Reset client flags to
35297c478bd9Sstevel@tonic-gate 						 * offline.
35307c478bd9Sstevel@tonic-gate 						 */
35317c478bd9Sstevel@tonic-gate 						MDI_DEBUG(1, (CE_WARN, cdip,
35327c478bd9Sstevel@tonic-gate 						    "!ndi_devi_online: failed "
35337c478bd9Sstevel@tonic-gate 						    " Error: %x", rv));
35347c478bd9Sstevel@tonic-gate 						MDI_CLIENT_SET_OFFLINE(ct);
35357c478bd9Sstevel@tonic-gate 					}
35367c478bd9Sstevel@tonic-gate 					if (rv != NDI_SUCCESS) {
35377c478bd9Sstevel@tonic-gate 						/* Reset the path state */
35387c478bd9Sstevel@tonic-gate 						MDI_PI_LOCK(pip);
35397c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_state =
35407c478bd9Sstevel@tonic-gate 						    MDI_PI_OLD_STATE(pip);
35417c478bd9Sstevel@tonic-gate 						MDI_PI_UNLOCK(pip);
35427c478bd9Sstevel@tonic-gate 					}
35437c478bd9Sstevel@tonic-gate 				}
35447c478bd9Sstevel@tonic-gate 				break;
35457c478bd9Sstevel@tonic-gate 
35467c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_FAILED:
35477c478bd9Sstevel@tonic-gate 				/*
35487c478bd9Sstevel@tonic-gate 				 * This is the last path case for
35497c478bd9Sstevel@tonic-gate 				 * non-user initiated events.
35507c478bd9Sstevel@tonic-gate 				 */
35517c478bd9Sstevel@tonic-gate 				if (((flag & NDI_DEVI_REMOVE) == 0) &&
35527c478bd9Sstevel@tonic-gate 				    cdip && (i_ddi_node_state(cdip) >=
35537c478bd9Sstevel@tonic-gate 				    DS_INITIALIZED)) {
35545e3986cbScth 					MDI_CLIENT_UNLOCK(ct);
35557c478bd9Sstevel@tonic-gate 					rv = ndi_devi_offline(cdip, 0);
35565e3986cbScth 					MDI_CLIENT_LOCK(ct);
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 					if (rv != NDI_SUCCESS) {
35597c478bd9Sstevel@tonic-gate 						/*
35607c478bd9Sstevel@tonic-gate 						 * ndi_devi_offline failed.
35617c478bd9Sstevel@tonic-gate 						 * Reset client flags to
35627c478bd9Sstevel@tonic-gate 						 * online as the path could not
35637c478bd9Sstevel@tonic-gate 						 * be offlined.
35647c478bd9Sstevel@tonic-gate 						 */
35657c478bd9Sstevel@tonic-gate 						MDI_DEBUG(1, (CE_WARN, cdip,
35667c478bd9Sstevel@tonic-gate 						    "!ndi_devi_offline: failed "
35677c478bd9Sstevel@tonic-gate 						    " Error: %x", rv));
35687c478bd9Sstevel@tonic-gate 						MDI_CLIENT_SET_ONLINE(ct);
35697c478bd9Sstevel@tonic-gate 					}
35707c478bd9Sstevel@tonic-gate 				}
35717c478bd9Sstevel@tonic-gate 				break;
35727c478bd9Sstevel@tonic-gate 			}
35737c478bd9Sstevel@tonic-gate 			/*
35747c478bd9Sstevel@tonic-gate 			 * Convert to MDI error code
35757c478bd9Sstevel@tonic-gate 			 */
35767c478bd9Sstevel@tonic-gate 			switch (rv) {
35777c478bd9Sstevel@tonic-gate 			case NDI_SUCCESS:
35787c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_REPORT_DEV_NEEDED(ct);
35797c478bd9Sstevel@tonic-gate 				i_mdi_report_path_state(ct, pip);
35807c478bd9Sstevel@tonic-gate 				rv = MDI_SUCCESS;
35817c478bd9Sstevel@tonic-gate 				break;
35827c478bd9Sstevel@tonic-gate 			case NDI_BUSY:
35837c478bd9Sstevel@tonic-gate 				rv = MDI_BUSY;
35847c478bd9Sstevel@tonic-gate 				break;
35857c478bd9Sstevel@tonic-gate 			default:
35867c478bd9Sstevel@tonic-gate 				rv = MDI_FAILURE;
35877c478bd9Sstevel@tonic-gate 				break;
35887c478bd9Sstevel@tonic-gate 			}
35897c478bd9Sstevel@tonic-gate 		}
35907c478bd9Sstevel@tonic-gate 	}
35917c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
35927c478bd9Sstevel@tonic-gate 
35937c478bd9Sstevel@tonic-gate state_change_exit:
35947c478bd9Sstevel@tonic-gate 	/*
35957c478bd9Sstevel@tonic-gate 	 * Mark the pHCI as stable again.
35967c478bd9Sstevel@tonic-gate 	 */
35977c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
35987c478bd9Sstevel@tonic-gate 	MDI_PHCI_STABLE(ph);
35997c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
36007c478bd9Sstevel@tonic-gate 	return (rv);
36017c478bd9Sstevel@tonic-gate }
36027c478bd9Sstevel@tonic-gate 
36037c478bd9Sstevel@tonic-gate /*
36047c478bd9Sstevel@tonic-gate  * mdi_pi_online():
36057c478bd9Sstevel@tonic-gate  *		Place the path_info node in the online state.  The path is
36067c478bd9Sstevel@tonic-gate  *		now available to be selected by mdi_select_path() for
36077c478bd9Sstevel@tonic-gate  *		transporting I/O requests to client devices.
36087c478bd9Sstevel@tonic-gate  * Return Values:
36097c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
36107c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
36117c478bd9Sstevel@tonic-gate  */
36127c478bd9Sstevel@tonic-gate int
36137c478bd9Sstevel@tonic-gate mdi_pi_online(mdi_pathinfo_t *pip, int flags)
36147c478bd9Sstevel@tonic-gate {
36157c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct = MDI_PI(pip)->pi_client;
36167c478bd9Sstevel@tonic-gate 	int		client_held = 0;
36177c478bd9Sstevel@tonic-gate 	int		rv;
36187c478bd9Sstevel@tonic-gate 
36197c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
36207c478bd9Sstevel@tonic-gate 	rv = i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_ONLINE, flags);
36217c478bd9Sstevel@tonic-gate 	if (rv != MDI_SUCCESS)
36227c478bd9Sstevel@tonic-gate 		return (rv);
36237c478bd9Sstevel@tonic-gate 
36247c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
36257c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held == 0) {
36267c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, ct->ct_dip, "mdi_pi_online "
36275e3986cbScth 		    "i_mdi_pm_hold_pip %p\n", (void *)pip));
36287c478bd9Sstevel@tonic-gate 		i_mdi_pm_hold_pip(pip);
36297c478bd9Sstevel@tonic-gate 		client_held = 1;
36307c478bd9Sstevel@tonic-gate 	}
36317c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
36327c478bd9Sstevel@tonic-gate 
36337c478bd9Sstevel@tonic-gate 	if (client_held) {
36347c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
36357c478bd9Sstevel@tonic-gate 		if (ct->ct_power_cnt == 0) {
36367c478bd9Sstevel@tonic-gate 			rv = i_mdi_power_all_phci(ct);
36377c478bd9Sstevel@tonic-gate 		}
36387c478bd9Sstevel@tonic-gate 
36397c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, ct->ct_dip, "mdi_pi_online "
36405e3986cbScth 		    "i_mdi_pm_hold_client %p\n", (void *)ct));
36417c478bd9Sstevel@tonic-gate 		i_mdi_pm_hold_client(ct, 1);
36427c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
36437c478bd9Sstevel@tonic-gate 	}
36447c478bd9Sstevel@tonic-gate 
36457c478bd9Sstevel@tonic-gate 	return (rv);
36467c478bd9Sstevel@tonic-gate }
36477c478bd9Sstevel@tonic-gate 
36487c478bd9Sstevel@tonic-gate /*
36497c478bd9Sstevel@tonic-gate  * mdi_pi_standby():
36507c478bd9Sstevel@tonic-gate  *		Place the mdi_pathinfo node in standby state
36517c478bd9Sstevel@tonic-gate  *
36527c478bd9Sstevel@tonic-gate  * Return Values:
36537c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
36547c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
36557c478bd9Sstevel@tonic-gate  */
36567c478bd9Sstevel@tonic-gate int
36577c478bd9Sstevel@tonic-gate mdi_pi_standby(mdi_pathinfo_t *pip, int flags)
36587c478bd9Sstevel@tonic-gate {
36597c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_STANDBY, flags));
36607c478bd9Sstevel@tonic-gate }
36617c478bd9Sstevel@tonic-gate 
36627c478bd9Sstevel@tonic-gate /*
36637c478bd9Sstevel@tonic-gate  * mdi_pi_fault():
36647c478bd9Sstevel@tonic-gate  *		Place the mdi_pathinfo node in fault'ed state
36657c478bd9Sstevel@tonic-gate  * Return Values:
36667c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
36677c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
36687c478bd9Sstevel@tonic-gate  */
36697c478bd9Sstevel@tonic-gate int
36707c478bd9Sstevel@tonic-gate mdi_pi_fault(mdi_pathinfo_t *pip, int flags)
36717c478bd9Sstevel@tonic-gate {
36727c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_FAULT, flags));
36737c478bd9Sstevel@tonic-gate }
36747c478bd9Sstevel@tonic-gate 
36757c478bd9Sstevel@tonic-gate /*
36767c478bd9Sstevel@tonic-gate  * mdi_pi_offline():
36777c478bd9Sstevel@tonic-gate  *		Offline a mdi_pathinfo node.
36787c478bd9Sstevel@tonic-gate  * Return Values:
36797c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
36807c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
36817c478bd9Sstevel@tonic-gate  */
36827c478bd9Sstevel@tonic-gate int
36837c478bd9Sstevel@tonic-gate mdi_pi_offline(mdi_pathinfo_t *pip, int flags)
36847c478bd9Sstevel@tonic-gate {
36857c478bd9Sstevel@tonic-gate 	int	ret, client_held = 0;
36867c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
36877c478bd9Sstevel@tonic-gate 
36887c478bd9Sstevel@tonic-gate 	ret = i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_OFFLINE, flags);
36897c478bd9Sstevel@tonic-gate 
36907c478bd9Sstevel@tonic-gate 	if (ret == MDI_SUCCESS) {
36917c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
36927c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_pm_held) {
36937c478bd9Sstevel@tonic-gate 			client_held = 1;
36947c478bd9Sstevel@tonic-gate 		}
36957c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
36967c478bd9Sstevel@tonic-gate 
36977c478bd9Sstevel@tonic-gate 		if (client_held) {
36987c478bd9Sstevel@tonic-gate 			ct = MDI_PI(pip)->pi_client;
36997c478bd9Sstevel@tonic-gate 			MDI_CLIENT_LOCK(ct);
37007c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, ct->ct_dip,
37017c478bd9Sstevel@tonic-gate 			    "mdi_pi_offline i_mdi_pm_rele_client\n"));
37027c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, 1);
37037c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
37047c478bd9Sstevel@tonic-gate 		}
37057c478bd9Sstevel@tonic-gate 	}
37067c478bd9Sstevel@tonic-gate 
37077c478bd9Sstevel@tonic-gate 	return (ret);
37087c478bd9Sstevel@tonic-gate }
37097c478bd9Sstevel@tonic-gate 
37107c478bd9Sstevel@tonic-gate /*
37117c478bd9Sstevel@tonic-gate  * i_mdi_pi_offline():
37127c478bd9Sstevel@tonic-gate  *		Offline a mdi_pathinfo node and call the vHCI driver's callback
37137c478bd9Sstevel@tonic-gate  */
37147c478bd9Sstevel@tonic-gate static int
37157c478bd9Sstevel@tonic-gate i_mdi_pi_offline(mdi_pathinfo_t *pip, int flags)
37167c478bd9Sstevel@tonic-gate {
37177c478bd9Sstevel@tonic-gate 	dev_info_t	*vdip = NULL;
37187c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
37197c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct = NULL;
37207c478bd9Sstevel@tonic-gate 	int		(*f)();
37217c478bd9Sstevel@tonic-gate 	int		rv;
37227c478bd9Sstevel@tonic-gate 
37237c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
37247c478bd9Sstevel@tonic-gate 	ct = MDI_PI(pip)->pi_client;
37257c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate 	while (MDI_PI(pip)->pi_ref_cnt != 0) {
37287c478bd9Sstevel@tonic-gate 		/*
37297c478bd9Sstevel@tonic-gate 		 * Give a chance for pending I/Os to complete.
37307c478bd9Sstevel@tonic-gate 		 */
37315e3986cbScth 		MDI_DEBUG(1, (CE_NOTE, ct->ct_dip, "!i_mdi_pi_offline: "
37327c478bd9Sstevel@tonic-gate 		    "%d cmds still pending on path: %p\n",
37335e3986cbScth 		    MDI_PI(pip)->pi_ref_cnt, (void *)pip));
37347c478bd9Sstevel@tonic-gate 		if (cv_timedwait(&MDI_PI(pip)->pi_ref_cv,
37357c478bd9Sstevel@tonic-gate 		    &MDI_PI(pip)->pi_mutex,
37367c478bd9Sstevel@tonic-gate 		    ddi_get_lbolt() + drv_usectohz(60 * 1000000)) == -1) {
37377c478bd9Sstevel@tonic-gate 			/*
37387c478bd9Sstevel@tonic-gate 			 * The timeout time reached without ref_cnt being zero
37397c478bd9Sstevel@tonic-gate 			 * being signaled.
37407c478bd9Sstevel@tonic-gate 			 */
37415e3986cbScth 			MDI_DEBUG(1, (CE_NOTE, ct->ct_dip, "!i_mdi_pi_offline: "
37427c478bd9Sstevel@tonic-gate 			    "Timeout reached on path %p without the cond\n",
37435e3986cbScth 			    (void *)pip));
37445e3986cbScth 			MDI_DEBUG(1, (CE_NOTE, ct->ct_dip, "!i_mdi_pi_offline: "
37457c478bd9Sstevel@tonic-gate 			    "%d cmds still pending on path: %p\n",
37465e3986cbScth 			    MDI_PI(pip)->pi_ref_cnt, (void *)pip));
37477c478bd9Sstevel@tonic-gate 		}
37487c478bd9Sstevel@tonic-gate 	}
37497c478bd9Sstevel@tonic-gate 	vh = ct->ct_vhci;
37507c478bd9Sstevel@tonic-gate 	vdip = vh->vh_dip;
37517c478bd9Sstevel@tonic-gate 
37527c478bd9Sstevel@tonic-gate 	/*
37537c478bd9Sstevel@tonic-gate 	 * Notify vHCI that has registered this event
37547c478bd9Sstevel@tonic-gate 	 */
37557c478bd9Sstevel@tonic-gate 	ASSERT(vh->vh_ops);
37567c478bd9Sstevel@tonic-gate 	f = vh->vh_ops->vo_pi_state_change;
37577c478bd9Sstevel@tonic-gate 
37587c478bd9Sstevel@tonic-gate 	if (f != NULL) {
37597c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
37607c478bd9Sstevel@tonic-gate 		if ((rv = (*f)(vdip, pip, MDI_PATHINFO_STATE_OFFLINE, 0,
37617c478bd9Sstevel@tonic-gate 		    flags)) != MDI_SUCCESS) {
37625e3986cbScth 			MDI_DEBUG(1, (CE_WARN, ct->ct_dip,
37635e3986cbScth 			    "!vo_path_offline failed "
37645e3986cbScth 			    "vdip %p, pip %p", (void *)vdip, (void *)pip));
37657c478bd9Sstevel@tonic-gate 		}
37667c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
37677c478bd9Sstevel@tonic-gate 	}
37687c478bd9Sstevel@tonic-gate 
37697c478bd9Sstevel@tonic-gate 	/*
37707c478bd9Sstevel@tonic-gate 	 * Set the mdi_pathinfo node state and clear the transient condition
37717c478bd9Sstevel@tonic-gate 	 */
37727c478bd9Sstevel@tonic-gate 	MDI_PI_SET_OFFLINE(pip);
37737c478bd9Sstevel@tonic-gate 	cv_broadcast(&MDI_PI(pip)->pi_state_cv);
37747c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
37757c478bd9Sstevel@tonic-gate 
37767c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
37777c478bd9Sstevel@tonic-gate 	if (rv == MDI_SUCCESS) {
37787c478bd9Sstevel@tonic-gate 		if (ct->ct_unstable == 0) {
37797c478bd9Sstevel@tonic-gate 			dev_info_t	*cdip = ct->ct_dip;
37807c478bd9Sstevel@tonic-gate 
37817c478bd9Sstevel@tonic-gate 			/*
37827c478bd9Sstevel@tonic-gate 			 * Onlining the mdi_pathinfo node will impact the
37837c478bd9Sstevel@tonic-gate 			 * client state Update the client and dev_info node
37847c478bd9Sstevel@tonic-gate 			 * state accordingly
37857c478bd9Sstevel@tonic-gate 			 */
37867c478bd9Sstevel@tonic-gate 			i_mdi_client_update_state(ct);
37877c478bd9Sstevel@tonic-gate 			rv = NDI_SUCCESS;
37887c478bd9Sstevel@tonic-gate 			if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_FAILED) {
37897c478bd9Sstevel@tonic-gate 				if (cdip &&
37907c478bd9Sstevel@tonic-gate 				    (i_ddi_node_state(cdip) >=
37917c478bd9Sstevel@tonic-gate 				    DS_INITIALIZED)) {
37927c478bd9Sstevel@tonic-gate 					MDI_CLIENT_UNLOCK(ct);
37937c478bd9Sstevel@tonic-gate 					rv = ndi_devi_offline(cdip, 0);
37947c478bd9Sstevel@tonic-gate 					MDI_CLIENT_LOCK(ct);
37957c478bd9Sstevel@tonic-gate 					if (rv != NDI_SUCCESS) {
37967c478bd9Sstevel@tonic-gate 						/*
37977c478bd9Sstevel@tonic-gate 						 * ndi_devi_offline failed.
37987c478bd9Sstevel@tonic-gate 						 * Reset client flags to
37997c478bd9Sstevel@tonic-gate 						 * online.
38007c478bd9Sstevel@tonic-gate 						 */
38017c478bd9Sstevel@tonic-gate 						MDI_DEBUG(4, (CE_WARN, cdip,
38027c478bd9Sstevel@tonic-gate 						    "!ndi_devi_offline: failed "
38037c478bd9Sstevel@tonic-gate 						    " Error: %x", rv));
38047c478bd9Sstevel@tonic-gate 						MDI_CLIENT_SET_ONLINE(ct);
38057c478bd9Sstevel@tonic-gate 					}
38067c478bd9Sstevel@tonic-gate 				}
38077c478bd9Sstevel@tonic-gate 			}
38087c478bd9Sstevel@tonic-gate 			/*
38097c478bd9Sstevel@tonic-gate 			 * Convert to MDI error code
38107c478bd9Sstevel@tonic-gate 			 */
38117c478bd9Sstevel@tonic-gate 			switch (rv) {
38127c478bd9Sstevel@tonic-gate 			case NDI_SUCCESS:
38137c478bd9Sstevel@tonic-gate 				rv = MDI_SUCCESS;
38147c478bd9Sstevel@tonic-gate 				break;
38157c478bd9Sstevel@tonic-gate 			case NDI_BUSY:
38167c478bd9Sstevel@tonic-gate 				rv = MDI_BUSY;
38177c478bd9Sstevel@tonic-gate 				break;
38187c478bd9Sstevel@tonic-gate 			default:
38197c478bd9Sstevel@tonic-gate 				rv = MDI_FAILURE;
38207c478bd9Sstevel@tonic-gate 				break;
38217c478bd9Sstevel@tonic-gate 			}
38227c478bd9Sstevel@tonic-gate 		}
38237c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_REPORT_DEV_NEEDED(ct);
38247c478bd9Sstevel@tonic-gate 		i_mdi_report_path_state(ct, pip);
38257c478bd9Sstevel@tonic-gate 	}
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
38287c478bd9Sstevel@tonic-gate 
38297c478bd9Sstevel@tonic-gate 	/*
38307c478bd9Sstevel@tonic-gate 	 * Change in the mdi_pathinfo node state will impact the client state
38317c478bd9Sstevel@tonic-gate 	 */
38327c478bd9Sstevel@tonic-gate 	MDI_DEBUG(2, (CE_NOTE, NULL, "!i_mdi_pi_offline ct = %p pip = %p",
38335e3986cbScth 	    (void *)ct, (void *)pip));
38347c478bd9Sstevel@tonic-gate 	return (rv);
38357c478bd9Sstevel@tonic-gate }
38367c478bd9Sstevel@tonic-gate 
38377c478bd9Sstevel@tonic-gate 
38387c478bd9Sstevel@tonic-gate /*
38397c478bd9Sstevel@tonic-gate  * mdi_pi_get_addr():
38407c478bd9Sstevel@tonic-gate  *		Get the unit address associated with a mdi_pathinfo node
38417c478bd9Sstevel@tonic-gate  *
38427c478bd9Sstevel@tonic-gate  * Return Values:
38437c478bd9Sstevel@tonic-gate  *		char *
38447c478bd9Sstevel@tonic-gate  */
38457c478bd9Sstevel@tonic-gate char *
38467c478bd9Sstevel@tonic-gate mdi_pi_get_addr(mdi_pathinfo_t *pip)
38477c478bd9Sstevel@tonic-gate {
38487c478bd9Sstevel@tonic-gate 	if (pip == NULL)
38497c478bd9Sstevel@tonic-gate 		return (NULL);
38507c478bd9Sstevel@tonic-gate 
385172a50065Scth 	return (MDI_PI(pip)->pi_addr);
38527c478bd9Sstevel@tonic-gate }
38537c478bd9Sstevel@tonic-gate 
38547c478bd9Sstevel@tonic-gate /*
3855602ca9eaScth  * mdi_pi_get_path_instance():
3856602ca9eaScth  *		Get the 'path_instance' of a mdi_pathinfo node
3857602ca9eaScth  *
3858602ca9eaScth  * Return Values:
3859602ca9eaScth  *		path_instance
3860602ca9eaScth  */
3861602ca9eaScth int
3862602ca9eaScth mdi_pi_get_path_instance(mdi_pathinfo_t *pip)
3863602ca9eaScth {
3864602ca9eaScth 	if (pip == NULL)
3865602ca9eaScth 		return (0);
3866602ca9eaScth 
3867602ca9eaScth 	return (MDI_PI(pip)->pi_path_instance);
3868602ca9eaScth }
3869602ca9eaScth 
3870602ca9eaScth /*
3871602ca9eaScth  * mdi_pi_pathname():
3872602ca9eaScth  *		Return pointer to path to pathinfo node.
3873602ca9eaScth  */
3874602ca9eaScth char *
3875602ca9eaScth mdi_pi_pathname(mdi_pathinfo_t *pip)
3876602ca9eaScth {
3877602ca9eaScth 	if (pip == NULL)
3878602ca9eaScth 		return (NULL);
3879602ca9eaScth 	return (mdi_pi_pathname_by_instance(mdi_pi_get_path_instance(pip)));
3880602ca9eaScth }
3881602ca9eaScth 
3882602ca9eaScth /*
38837c478bd9Sstevel@tonic-gate  * mdi_pi_get_client():
38847c478bd9Sstevel@tonic-gate  *		Get the client devinfo associated with a mdi_pathinfo node
38857c478bd9Sstevel@tonic-gate  *
38867c478bd9Sstevel@tonic-gate  * Return Values:
38877c478bd9Sstevel@tonic-gate  *		Handle to client device dev_info node
38887c478bd9Sstevel@tonic-gate  */
38897c478bd9Sstevel@tonic-gate dev_info_t *
38907c478bd9Sstevel@tonic-gate mdi_pi_get_client(mdi_pathinfo_t *pip)
38917c478bd9Sstevel@tonic-gate {
38927c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
38937c478bd9Sstevel@tonic-gate 	if (pip) {
38947c478bd9Sstevel@tonic-gate 		dip = MDI_PI(pip)->pi_client->ct_dip;
38957c478bd9Sstevel@tonic-gate 	}
38967c478bd9Sstevel@tonic-gate 	return (dip);
38977c478bd9Sstevel@tonic-gate }
38987c478bd9Sstevel@tonic-gate 
38997c478bd9Sstevel@tonic-gate /*
39007c478bd9Sstevel@tonic-gate  * mdi_pi_get_phci():
39017c478bd9Sstevel@tonic-gate  *		Get the pHCI devinfo associated with the mdi_pathinfo node
39027c478bd9Sstevel@tonic-gate  * Return Values:
39037c478bd9Sstevel@tonic-gate  *		Handle to dev_info node
39047c478bd9Sstevel@tonic-gate  */
39057c478bd9Sstevel@tonic-gate dev_info_t *
39067c478bd9Sstevel@tonic-gate mdi_pi_get_phci(mdi_pathinfo_t *pip)
39077c478bd9Sstevel@tonic-gate {
39087c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
39097c478bd9Sstevel@tonic-gate 	if (pip) {
39107c478bd9Sstevel@tonic-gate 		dip = MDI_PI(pip)->pi_phci->ph_dip;
39117c478bd9Sstevel@tonic-gate 	}
39127c478bd9Sstevel@tonic-gate 	return (dip);
39137c478bd9Sstevel@tonic-gate }
39147c478bd9Sstevel@tonic-gate 
39157c478bd9Sstevel@tonic-gate /*
39167c478bd9Sstevel@tonic-gate  * mdi_pi_get_client_private():
39177c478bd9Sstevel@tonic-gate  *		Get the client private information associated with the
39187c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node
39197c478bd9Sstevel@tonic-gate  */
39207c478bd9Sstevel@tonic-gate void *
39217c478bd9Sstevel@tonic-gate mdi_pi_get_client_private(mdi_pathinfo_t *pip)
39227c478bd9Sstevel@tonic-gate {
39237c478bd9Sstevel@tonic-gate 	void *cprivate = NULL;
39247c478bd9Sstevel@tonic-gate 	if (pip) {
39257c478bd9Sstevel@tonic-gate 		cprivate = MDI_PI(pip)->pi_cprivate;
39267c478bd9Sstevel@tonic-gate 	}
39277c478bd9Sstevel@tonic-gate 	return (cprivate);
39287c478bd9Sstevel@tonic-gate }
39297c478bd9Sstevel@tonic-gate 
39307c478bd9Sstevel@tonic-gate /*
39317c478bd9Sstevel@tonic-gate  * mdi_pi_set_client_private():
39327c478bd9Sstevel@tonic-gate  *		Set the client private information in the mdi_pathinfo node
39337c478bd9Sstevel@tonic-gate  */
39347c478bd9Sstevel@tonic-gate void
39357c478bd9Sstevel@tonic-gate mdi_pi_set_client_private(mdi_pathinfo_t *pip, void *priv)
39367c478bd9Sstevel@tonic-gate {
39377c478bd9Sstevel@tonic-gate 	if (pip) {
39387c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_cprivate = priv;
39397c478bd9Sstevel@tonic-gate 	}
39407c478bd9Sstevel@tonic-gate }
39417c478bd9Sstevel@tonic-gate 
39427c478bd9Sstevel@tonic-gate /*
39437c478bd9Sstevel@tonic-gate  * mdi_pi_get_phci_private():
39447c478bd9Sstevel@tonic-gate  *		Get the pHCI private information associated with the
39457c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node
39467c478bd9Sstevel@tonic-gate  */
39477c478bd9Sstevel@tonic-gate caddr_t
39487c478bd9Sstevel@tonic-gate mdi_pi_get_phci_private(mdi_pathinfo_t *pip)
39497c478bd9Sstevel@tonic-gate {
39507c478bd9Sstevel@tonic-gate 	caddr_t	pprivate = NULL;
39517c478bd9Sstevel@tonic-gate 	if (pip) {
39527c478bd9Sstevel@tonic-gate 		pprivate = MDI_PI(pip)->pi_pprivate;
39537c478bd9Sstevel@tonic-gate 	}
39547c478bd9Sstevel@tonic-gate 	return (pprivate);
39557c478bd9Sstevel@tonic-gate }
39567c478bd9Sstevel@tonic-gate 
39577c478bd9Sstevel@tonic-gate /*
39587c478bd9Sstevel@tonic-gate  * mdi_pi_set_phci_private():
39597c478bd9Sstevel@tonic-gate  *		Set the pHCI private information in the mdi_pathinfo node
39607c478bd9Sstevel@tonic-gate  */
39617c478bd9Sstevel@tonic-gate void
39627c478bd9Sstevel@tonic-gate mdi_pi_set_phci_private(mdi_pathinfo_t *pip, caddr_t priv)
39637c478bd9Sstevel@tonic-gate {
39647c478bd9Sstevel@tonic-gate 	if (pip) {
39657c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_pprivate = priv;
39667c478bd9Sstevel@tonic-gate 	}
39677c478bd9Sstevel@tonic-gate }
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate /*
39707c478bd9Sstevel@tonic-gate  * mdi_pi_get_state():
39717c478bd9Sstevel@tonic-gate  *		Get the mdi_pathinfo node state. Transient states are internal
39727c478bd9Sstevel@tonic-gate  *		and not provided to the users
39737c478bd9Sstevel@tonic-gate  */
39747c478bd9Sstevel@tonic-gate mdi_pathinfo_state_t
39757c478bd9Sstevel@tonic-gate mdi_pi_get_state(mdi_pathinfo_t *pip)
39767c478bd9Sstevel@tonic-gate {
39777c478bd9Sstevel@tonic-gate 	mdi_pathinfo_state_t    state = MDI_PATHINFO_STATE_INIT;
39787c478bd9Sstevel@tonic-gate 
39797c478bd9Sstevel@tonic-gate 	if (pip) {
39807c478bd9Sstevel@tonic-gate 		if (MDI_PI_IS_TRANSIENT(pip)) {
39817c478bd9Sstevel@tonic-gate 			/*
39827c478bd9Sstevel@tonic-gate 			 * mdi_pathinfo is in state transition.  Return the
39837c478bd9Sstevel@tonic-gate 			 * last good state.
39847c478bd9Sstevel@tonic-gate 			 */
39857c478bd9Sstevel@tonic-gate 			state = MDI_PI_OLD_STATE(pip);
39867c478bd9Sstevel@tonic-gate 		} else {
39877c478bd9Sstevel@tonic-gate 			state = MDI_PI_STATE(pip);
39887c478bd9Sstevel@tonic-gate 		}
39897c478bd9Sstevel@tonic-gate 	}
39907c478bd9Sstevel@tonic-gate 	return (state);
39917c478bd9Sstevel@tonic-gate }
39927c478bd9Sstevel@tonic-gate 
39937c478bd9Sstevel@tonic-gate /*
39947c478bd9Sstevel@tonic-gate  * Note that the following function needs to be the new interface for
39957c478bd9Sstevel@tonic-gate  * mdi_pi_get_state when mpxio gets integrated to ON.
39967c478bd9Sstevel@tonic-gate  */
39977c478bd9Sstevel@tonic-gate int
39987c478bd9Sstevel@tonic-gate mdi_pi_get_state2(mdi_pathinfo_t *pip, mdi_pathinfo_state_t *state,
39997c478bd9Sstevel@tonic-gate 		uint32_t *ext_state)
40007c478bd9Sstevel@tonic-gate {
40017c478bd9Sstevel@tonic-gate 	*state = MDI_PATHINFO_STATE_INIT;
40027c478bd9Sstevel@tonic-gate 
40037c478bd9Sstevel@tonic-gate 	if (pip) {
40047c478bd9Sstevel@tonic-gate 		if (MDI_PI_IS_TRANSIENT(pip)) {
40057c478bd9Sstevel@tonic-gate 			/*
40067c478bd9Sstevel@tonic-gate 			 * mdi_pathinfo is in state transition.  Return the
40077c478bd9Sstevel@tonic-gate 			 * last good state.
40087c478bd9Sstevel@tonic-gate 			 */
40097c478bd9Sstevel@tonic-gate 			*state = MDI_PI_OLD_STATE(pip);
40107c478bd9Sstevel@tonic-gate 			*ext_state = MDI_PI_OLD_EXT_STATE(pip);
40117c478bd9Sstevel@tonic-gate 		} else {
40127c478bd9Sstevel@tonic-gate 			*state = MDI_PI_STATE(pip);
40137c478bd9Sstevel@tonic-gate 			*ext_state = MDI_PI_EXT_STATE(pip);
40147c478bd9Sstevel@tonic-gate 		}
40157c478bd9Sstevel@tonic-gate 	}
40167c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
40177c478bd9Sstevel@tonic-gate }
40187c478bd9Sstevel@tonic-gate 
40197c478bd9Sstevel@tonic-gate /*
40207c478bd9Sstevel@tonic-gate  * mdi_pi_get_preferred:
40217c478bd9Sstevel@tonic-gate  *	Get the preferred path flag
40227c478bd9Sstevel@tonic-gate  */
40237c478bd9Sstevel@tonic-gate int
40247c478bd9Sstevel@tonic-gate mdi_pi_get_preferred(mdi_pathinfo_t *pip)
40257c478bd9Sstevel@tonic-gate {
40267c478bd9Sstevel@tonic-gate 	if (pip) {
40277c478bd9Sstevel@tonic-gate 		return (MDI_PI(pip)->pi_preferred);
40287c478bd9Sstevel@tonic-gate 	}
40297c478bd9Sstevel@tonic-gate 	return (0);
40307c478bd9Sstevel@tonic-gate }
40317c478bd9Sstevel@tonic-gate 
40327c478bd9Sstevel@tonic-gate /*
40337c478bd9Sstevel@tonic-gate  * mdi_pi_set_preferred:
40347c478bd9Sstevel@tonic-gate  *	Set the preferred path flag
40357c478bd9Sstevel@tonic-gate  */
40367c478bd9Sstevel@tonic-gate void
40377c478bd9Sstevel@tonic-gate mdi_pi_set_preferred(mdi_pathinfo_t *pip, int preferred)
40387c478bd9Sstevel@tonic-gate {
40397c478bd9Sstevel@tonic-gate 	if (pip) {
40407c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_preferred = preferred;
40417c478bd9Sstevel@tonic-gate 	}
40427c478bd9Sstevel@tonic-gate }
40437c478bd9Sstevel@tonic-gate 
40447c478bd9Sstevel@tonic-gate /*
40457c478bd9Sstevel@tonic-gate  * mdi_pi_set_state():
40467c478bd9Sstevel@tonic-gate  *		Set the mdi_pathinfo node state
40477c478bd9Sstevel@tonic-gate  */
40487c478bd9Sstevel@tonic-gate void
40497c478bd9Sstevel@tonic-gate mdi_pi_set_state(mdi_pathinfo_t *pip, mdi_pathinfo_state_t state)
40507c478bd9Sstevel@tonic-gate {
40517c478bd9Sstevel@tonic-gate 	uint32_t	ext_state;
40527c478bd9Sstevel@tonic-gate 
40537c478bd9Sstevel@tonic-gate 	if (pip) {
40547c478bd9Sstevel@tonic-gate 		ext_state = MDI_PI(pip)->pi_state & MDI_PATHINFO_EXT_STATE_MASK;
40557c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_state = state;
40567c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_state |= ext_state;
40577c478bd9Sstevel@tonic-gate 	}
40587c478bd9Sstevel@tonic-gate }
40597c478bd9Sstevel@tonic-gate 
40607c478bd9Sstevel@tonic-gate /*
40617c478bd9Sstevel@tonic-gate  * Property functions:
40627c478bd9Sstevel@tonic-gate  */
40637c478bd9Sstevel@tonic-gate int
40647c478bd9Sstevel@tonic-gate i_map_nvlist_error_to_mdi(int val)
40657c478bd9Sstevel@tonic-gate {
40667c478bd9Sstevel@tonic-gate 	int rv;
40677c478bd9Sstevel@tonic-gate 
40687c478bd9Sstevel@tonic-gate 	switch (val) {
40697c478bd9Sstevel@tonic-gate 	case 0:
40707c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_SUCCESS;
40717c478bd9Sstevel@tonic-gate 		break;
40727c478bd9Sstevel@tonic-gate 	case EINVAL:
40737c478bd9Sstevel@tonic-gate 	case ENOTSUP:
40747c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_INVAL_ARG;
40757c478bd9Sstevel@tonic-gate 		break;
40767c478bd9Sstevel@tonic-gate 	case ENOMEM:
40777c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_NO_MEMORY;
40787c478bd9Sstevel@tonic-gate 		break;
40797c478bd9Sstevel@tonic-gate 	default:
40807c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_NOT_FOUND;
40817c478bd9Sstevel@tonic-gate 		break;
40827c478bd9Sstevel@tonic-gate 	}
40837c478bd9Sstevel@tonic-gate 	return (rv);
40847c478bd9Sstevel@tonic-gate }
40857c478bd9Sstevel@tonic-gate 
40867c478bd9Sstevel@tonic-gate /*
40877c478bd9Sstevel@tonic-gate  * mdi_pi_get_next_prop():
40887c478bd9Sstevel@tonic-gate  * 		Property walk function.  The caller should hold mdi_pi_lock()
40897c478bd9Sstevel@tonic-gate  *		and release by calling mdi_pi_unlock() at the end of walk to
40907c478bd9Sstevel@tonic-gate  *		get a consistent value.
40917c478bd9Sstevel@tonic-gate  */
40927c478bd9Sstevel@tonic-gate nvpair_t *
40937c478bd9Sstevel@tonic-gate mdi_pi_get_next_prop(mdi_pathinfo_t *pip, nvpair_t *prev)
40947c478bd9Sstevel@tonic-gate {
40957c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
40967c478bd9Sstevel@tonic-gate 		return (NULL);
40977c478bd9Sstevel@tonic-gate 	}
40985e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
40997c478bd9Sstevel@tonic-gate 	return (nvlist_next_nvpair(MDI_PI(pip)->pi_prop, prev));
41007c478bd9Sstevel@tonic-gate }
41017c478bd9Sstevel@tonic-gate 
41027c478bd9Sstevel@tonic-gate /*
41037c478bd9Sstevel@tonic-gate  * mdi_prop_remove():
41047c478bd9Sstevel@tonic-gate  * 		Remove the named property from the named list.
41057c478bd9Sstevel@tonic-gate  */
41067c478bd9Sstevel@tonic-gate int
41077c478bd9Sstevel@tonic-gate mdi_prop_remove(mdi_pathinfo_t *pip, char *name)
41087c478bd9Sstevel@tonic-gate {
41097c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
41107c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
41117c478bd9Sstevel@tonic-gate 	}
41125e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
41137c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
41147c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
41157c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
41167c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
41177c478bd9Sstevel@tonic-gate 	}
41187c478bd9Sstevel@tonic-gate 	if (name) {
41197c478bd9Sstevel@tonic-gate 		(void) nvlist_remove_all(MDI_PI(pip)->pi_prop, name);
41207c478bd9Sstevel@tonic-gate 	} else {
41217c478bd9Sstevel@tonic-gate 		char		nvp_name[MAXNAMELEN];
41227c478bd9Sstevel@tonic-gate 		nvpair_t	*nvp;
41237c478bd9Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(MDI_PI(pip)->pi_prop, NULL);
41247c478bd9Sstevel@tonic-gate 		while (nvp) {
41257c478bd9Sstevel@tonic-gate 			nvpair_t	*next;
41267c478bd9Sstevel@tonic-gate 			next = nvlist_next_nvpair(MDI_PI(pip)->pi_prop, nvp);
41277c478bd9Sstevel@tonic-gate 			(void) snprintf(nvp_name, MAXNAMELEN, "%s",
41287c478bd9Sstevel@tonic-gate 			    nvpair_name(nvp));
41297c478bd9Sstevel@tonic-gate 			(void) nvlist_remove_all(MDI_PI(pip)->pi_prop,
41307c478bd9Sstevel@tonic-gate 			    nvp_name);
41317c478bd9Sstevel@tonic-gate 			nvp = next;
41327c478bd9Sstevel@tonic-gate 		}
41337c478bd9Sstevel@tonic-gate 	}
41347c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
41357c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
41367c478bd9Sstevel@tonic-gate }
41377c478bd9Sstevel@tonic-gate 
41387c478bd9Sstevel@tonic-gate /*
41397c478bd9Sstevel@tonic-gate  * mdi_prop_size():
41407c478bd9Sstevel@tonic-gate  * 		Get buffer size needed to pack the property data.
41417c478bd9Sstevel@tonic-gate  * 		Caller should hold the mdi_pathinfo_t lock to get a consistent
41427c478bd9Sstevel@tonic-gate  *		buffer size.
41437c478bd9Sstevel@tonic-gate  */
41447c478bd9Sstevel@tonic-gate int
41457c478bd9Sstevel@tonic-gate mdi_prop_size(mdi_pathinfo_t *pip, size_t *buflenp)
41467c478bd9Sstevel@tonic-gate {
41477c478bd9Sstevel@tonic-gate 	int	rv;
41487c478bd9Sstevel@tonic-gate 	size_t	bufsize;
41497c478bd9Sstevel@tonic-gate 
41507c478bd9Sstevel@tonic-gate 	*buflenp = 0;
41517c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
41527c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
41537c478bd9Sstevel@tonic-gate 	}
41545e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
41557c478bd9Sstevel@tonic-gate 	rv = nvlist_size(MDI_PI(pip)->pi_prop,
41567c478bd9Sstevel@tonic-gate 	    &bufsize, NV_ENCODE_NATIVE);
41577c478bd9Sstevel@tonic-gate 	*buflenp = bufsize;
41587c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
41597c478bd9Sstevel@tonic-gate }
41607c478bd9Sstevel@tonic-gate 
41617c478bd9Sstevel@tonic-gate /*
41627c478bd9Sstevel@tonic-gate  * mdi_prop_pack():
41637c478bd9Sstevel@tonic-gate  * 		pack the property list.  The caller should hold the
41647c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node to get a consistent data
41657c478bd9Sstevel@tonic-gate  */
41667c478bd9Sstevel@tonic-gate int
41677c478bd9Sstevel@tonic-gate mdi_prop_pack(mdi_pathinfo_t *pip, char **bufp, uint_t buflen)
41687c478bd9Sstevel@tonic-gate {
41697c478bd9Sstevel@tonic-gate 	int	rv;
41707c478bd9Sstevel@tonic-gate 	size_t	bufsize;
41717c478bd9Sstevel@tonic-gate 
41727c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || MDI_PI(pip)->pi_prop == NULL) {
41737c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
41747c478bd9Sstevel@tonic-gate 	}
41757c478bd9Sstevel@tonic-gate 
41765e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
41777c478bd9Sstevel@tonic-gate 
41787c478bd9Sstevel@tonic-gate 	bufsize = buflen;
41797c478bd9Sstevel@tonic-gate 	rv = nvlist_pack(MDI_PI(pip)->pi_prop, bufp, (size_t *)&bufsize,
41807c478bd9Sstevel@tonic-gate 	    NV_ENCODE_NATIVE, KM_SLEEP);
41817c478bd9Sstevel@tonic-gate 
41827c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
41837c478bd9Sstevel@tonic-gate }
41847c478bd9Sstevel@tonic-gate 
41857c478bd9Sstevel@tonic-gate /*
41867c478bd9Sstevel@tonic-gate  * mdi_prop_update_byte():
41877c478bd9Sstevel@tonic-gate  *		Create/Update a byte property
41887c478bd9Sstevel@tonic-gate  */
41897c478bd9Sstevel@tonic-gate int
41907c478bd9Sstevel@tonic-gate mdi_prop_update_byte(mdi_pathinfo_t *pip, char *name, uchar_t data)
41917c478bd9Sstevel@tonic-gate {
41927c478bd9Sstevel@tonic-gate 	int rv;
41937c478bd9Sstevel@tonic-gate 
41947c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
41957c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
41967c478bd9Sstevel@tonic-gate 	}
41975e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
41987c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
41997c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
42007c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
42017c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
42027c478bd9Sstevel@tonic-gate 	}
42037c478bd9Sstevel@tonic-gate 	rv = nvlist_add_byte(MDI_PI(pip)->pi_prop, name, data);
42047c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
42057c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
42067c478bd9Sstevel@tonic-gate }
42077c478bd9Sstevel@tonic-gate 
42087c478bd9Sstevel@tonic-gate /*
42097c478bd9Sstevel@tonic-gate  * mdi_prop_update_byte_array():
42107c478bd9Sstevel@tonic-gate  *		Create/Update a byte array property
42117c478bd9Sstevel@tonic-gate  */
42127c478bd9Sstevel@tonic-gate int
42137c478bd9Sstevel@tonic-gate mdi_prop_update_byte_array(mdi_pathinfo_t *pip, char *name, uchar_t *data,
42147c478bd9Sstevel@tonic-gate     uint_t nelements)
42157c478bd9Sstevel@tonic-gate {
42167c478bd9Sstevel@tonic-gate 	int rv;
42177c478bd9Sstevel@tonic-gate 
42187c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
42197c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
42207c478bd9Sstevel@tonic-gate 	}
42215e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
42227c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
42237c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
42247c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
42257c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
42267c478bd9Sstevel@tonic-gate 	}
42277c478bd9Sstevel@tonic-gate 	rv = nvlist_add_byte_array(MDI_PI(pip)->pi_prop, name, data, nelements);
42287c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
42297c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
42307c478bd9Sstevel@tonic-gate }
42317c478bd9Sstevel@tonic-gate 
42327c478bd9Sstevel@tonic-gate /*
42337c478bd9Sstevel@tonic-gate  * mdi_prop_update_int():
42347c478bd9Sstevel@tonic-gate  *		Create/Update a 32 bit integer property
42357c478bd9Sstevel@tonic-gate  */
42367c478bd9Sstevel@tonic-gate int
42377c478bd9Sstevel@tonic-gate mdi_prop_update_int(mdi_pathinfo_t *pip, char *name, int data)
42387c478bd9Sstevel@tonic-gate {
42397c478bd9Sstevel@tonic-gate 	int rv;
42407c478bd9Sstevel@tonic-gate 
42417c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
42427c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
42437c478bd9Sstevel@tonic-gate 	}
42445e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
42457c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
42467c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
42477c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
42487c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
42497c478bd9Sstevel@tonic-gate 	}
42507c478bd9Sstevel@tonic-gate 	rv = nvlist_add_int32(MDI_PI(pip)->pi_prop, name, (int32_t)data);
42517c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
42527c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
42537c478bd9Sstevel@tonic-gate }
42547c478bd9Sstevel@tonic-gate 
42557c478bd9Sstevel@tonic-gate /*
42567c478bd9Sstevel@tonic-gate  * mdi_prop_update_int64():
42577c478bd9Sstevel@tonic-gate  *		Create/Update a 64 bit integer property
42587c478bd9Sstevel@tonic-gate  */
42597c478bd9Sstevel@tonic-gate int
42607c478bd9Sstevel@tonic-gate mdi_prop_update_int64(mdi_pathinfo_t *pip, char *name, int64_t data)
42617c478bd9Sstevel@tonic-gate {
42627c478bd9Sstevel@tonic-gate 	int rv;
42637c478bd9Sstevel@tonic-gate 
42647c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
42657c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
42667c478bd9Sstevel@tonic-gate 	}
42675e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
42687c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
42697c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
42707c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
42717c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
42727c478bd9Sstevel@tonic-gate 	}
42737c478bd9Sstevel@tonic-gate 	rv = nvlist_add_int64(MDI_PI(pip)->pi_prop, name, data);
42747c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
42757c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
42767c478bd9Sstevel@tonic-gate }
42777c478bd9Sstevel@tonic-gate 
42787c478bd9Sstevel@tonic-gate /*
42797c478bd9Sstevel@tonic-gate  * mdi_prop_update_int_array():
42807c478bd9Sstevel@tonic-gate  *		Create/Update a int array property
42817c478bd9Sstevel@tonic-gate  */
42827c478bd9Sstevel@tonic-gate int
42837c478bd9Sstevel@tonic-gate mdi_prop_update_int_array(mdi_pathinfo_t *pip, char *name, int *data,
42847c478bd9Sstevel@tonic-gate 	    uint_t nelements)
42857c478bd9Sstevel@tonic-gate {
42867c478bd9Sstevel@tonic-gate 	int rv;
42877c478bd9Sstevel@tonic-gate 
42887c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
42897c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
42907c478bd9Sstevel@tonic-gate 	}
42915e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
42927c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
42937c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
42947c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
42957c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
42967c478bd9Sstevel@tonic-gate 	}
42977c478bd9Sstevel@tonic-gate 	rv = nvlist_add_int32_array(MDI_PI(pip)->pi_prop, name, (int32_t *)data,
42987c478bd9Sstevel@tonic-gate 	    nelements);
42997c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
43007c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
43017c478bd9Sstevel@tonic-gate }
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate /*
43047c478bd9Sstevel@tonic-gate  * mdi_prop_update_string():
43057c478bd9Sstevel@tonic-gate  *		Create/Update a string property
43067c478bd9Sstevel@tonic-gate  */
43077c478bd9Sstevel@tonic-gate int
43087c478bd9Sstevel@tonic-gate mdi_prop_update_string(mdi_pathinfo_t *pip, char *name, char *data)
43097c478bd9Sstevel@tonic-gate {
43107c478bd9Sstevel@tonic-gate 	int rv;
43117c478bd9Sstevel@tonic-gate 
43127c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
43137c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
43147c478bd9Sstevel@tonic-gate 	}
43155e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
43167c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
43177c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
43187c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
43197c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
43207c478bd9Sstevel@tonic-gate 	}
43217c478bd9Sstevel@tonic-gate 	rv = nvlist_add_string(MDI_PI(pip)->pi_prop, name, data);
43227c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
43237c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
43247c478bd9Sstevel@tonic-gate }
43257c478bd9Sstevel@tonic-gate 
43267c478bd9Sstevel@tonic-gate /*
43277c478bd9Sstevel@tonic-gate  * mdi_prop_update_string_array():
43287c478bd9Sstevel@tonic-gate  *		Create/Update a string array property
43297c478bd9Sstevel@tonic-gate  */
43307c478bd9Sstevel@tonic-gate int
43317c478bd9Sstevel@tonic-gate mdi_prop_update_string_array(mdi_pathinfo_t *pip, char *name, char **data,
43327c478bd9Sstevel@tonic-gate     uint_t nelements)
43337c478bd9Sstevel@tonic-gate {
43347c478bd9Sstevel@tonic-gate 	int rv;
43357c478bd9Sstevel@tonic-gate 
43367c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
43377c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
43387c478bd9Sstevel@tonic-gate 	}
43395e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
43407c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
43417c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
43427c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
43437c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
43447c478bd9Sstevel@tonic-gate 	}
43457c478bd9Sstevel@tonic-gate 	rv = nvlist_add_string_array(MDI_PI(pip)->pi_prop, name, data,
43467c478bd9Sstevel@tonic-gate 	    nelements);
43477c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
43487c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
43497c478bd9Sstevel@tonic-gate }
43507c478bd9Sstevel@tonic-gate 
43517c478bd9Sstevel@tonic-gate /*
43527c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_byte():
43537c478bd9Sstevel@tonic-gate  * 		Look for byte property identified by name.  The data returned
43547c478bd9Sstevel@tonic-gate  *		is the actual property and valid as long as mdi_pathinfo_t node
43557c478bd9Sstevel@tonic-gate  *		is alive.
43567c478bd9Sstevel@tonic-gate  */
43577c478bd9Sstevel@tonic-gate int
43587c478bd9Sstevel@tonic-gate mdi_prop_lookup_byte(mdi_pathinfo_t *pip, char *name, uchar_t *data)
43597c478bd9Sstevel@tonic-gate {
43607c478bd9Sstevel@tonic-gate 	int rv;
43617c478bd9Sstevel@tonic-gate 
43627c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
43637c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
43647c478bd9Sstevel@tonic-gate 	}
43657c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_byte(MDI_PI(pip)->pi_prop, name, data);
43667c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
43677c478bd9Sstevel@tonic-gate }
43687c478bd9Sstevel@tonic-gate 
43697c478bd9Sstevel@tonic-gate 
43707c478bd9Sstevel@tonic-gate /*
43717c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_byte_array():
43727c478bd9Sstevel@tonic-gate  * 		Look for byte array property identified by name.  The data
43737c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
43747c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
43757c478bd9Sstevel@tonic-gate  */
43767c478bd9Sstevel@tonic-gate int
43777c478bd9Sstevel@tonic-gate mdi_prop_lookup_byte_array(mdi_pathinfo_t *pip, char *name, uchar_t **data,
43787c478bd9Sstevel@tonic-gate     uint_t *nelements)
43797c478bd9Sstevel@tonic-gate {
43807c478bd9Sstevel@tonic-gate 	int rv;
43817c478bd9Sstevel@tonic-gate 
43827c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
43837c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
43847c478bd9Sstevel@tonic-gate 	}
43857c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_byte_array(MDI_PI(pip)->pi_prop, name, data,
43867c478bd9Sstevel@tonic-gate 	    nelements);
43877c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
43887c478bd9Sstevel@tonic-gate }
43897c478bd9Sstevel@tonic-gate 
43907c478bd9Sstevel@tonic-gate /*
43917c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_int():
43927c478bd9Sstevel@tonic-gate  * 		Look for int property identified by name.  The data returned
43937c478bd9Sstevel@tonic-gate  *		is the actual property and valid as long as mdi_pathinfo_t
43947c478bd9Sstevel@tonic-gate  *		node is alive.
43957c478bd9Sstevel@tonic-gate  */
43967c478bd9Sstevel@tonic-gate int
43977c478bd9Sstevel@tonic-gate mdi_prop_lookup_int(mdi_pathinfo_t *pip, char *name, int *data)
43987c478bd9Sstevel@tonic-gate {
43997c478bd9Sstevel@tonic-gate 	int rv;
44007c478bd9Sstevel@tonic-gate 
44017c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
44027c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44037c478bd9Sstevel@tonic-gate 	}
44047c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_int32(MDI_PI(pip)->pi_prop, name, (int32_t *)data);
44057c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
44067c478bd9Sstevel@tonic-gate }
44077c478bd9Sstevel@tonic-gate 
44087c478bd9Sstevel@tonic-gate /*
44097c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_int64():
44107c478bd9Sstevel@tonic-gate  * 		Look for int64 property identified by name.  The data returned
44117c478bd9Sstevel@tonic-gate  *		is the actual property and valid as long as mdi_pathinfo_t node
44127c478bd9Sstevel@tonic-gate  *		is alive.
44137c478bd9Sstevel@tonic-gate  */
44147c478bd9Sstevel@tonic-gate int
44157c478bd9Sstevel@tonic-gate mdi_prop_lookup_int64(mdi_pathinfo_t *pip, char *name, int64_t *data)
44167c478bd9Sstevel@tonic-gate {
44177c478bd9Sstevel@tonic-gate 	int rv;
44187c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
44197c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44207c478bd9Sstevel@tonic-gate 	}
44217c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_int64(MDI_PI(pip)->pi_prop, name, data);
44227c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
44237c478bd9Sstevel@tonic-gate }
44247c478bd9Sstevel@tonic-gate 
44257c478bd9Sstevel@tonic-gate /*
44267c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_int_array():
44277c478bd9Sstevel@tonic-gate  * 		Look for int array property identified by name.  The data
44287c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
44297c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
44307c478bd9Sstevel@tonic-gate  */
44317c478bd9Sstevel@tonic-gate int
44327c478bd9Sstevel@tonic-gate mdi_prop_lookup_int_array(mdi_pathinfo_t *pip, char *name, int **data,
44337c478bd9Sstevel@tonic-gate     uint_t *nelements)
44347c478bd9Sstevel@tonic-gate {
44357c478bd9Sstevel@tonic-gate 	int rv;
44367c478bd9Sstevel@tonic-gate 
44377c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
44387c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44397c478bd9Sstevel@tonic-gate 	}
44407c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_int32_array(MDI_PI(pip)->pi_prop, name,
44417c478bd9Sstevel@tonic-gate 	    (int32_t **)data, nelements);
44427c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
44437c478bd9Sstevel@tonic-gate }
44447c478bd9Sstevel@tonic-gate 
44457c478bd9Sstevel@tonic-gate /*
44467c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_string():
44477c478bd9Sstevel@tonic-gate  * 		Look for string property identified by name.  The data
44487c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
44497c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
44507c478bd9Sstevel@tonic-gate  */
44517c478bd9Sstevel@tonic-gate int
44527c478bd9Sstevel@tonic-gate mdi_prop_lookup_string(mdi_pathinfo_t *pip, char *name, char **data)
44537c478bd9Sstevel@tonic-gate {
44547c478bd9Sstevel@tonic-gate 	int rv;
44557c478bd9Sstevel@tonic-gate 
44567c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
44577c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44587c478bd9Sstevel@tonic-gate 	}
44597c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_string(MDI_PI(pip)->pi_prop, name, data);
44607c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
44617c478bd9Sstevel@tonic-gate }
44627c478bd9Sstevel@tonic-gate 
44637c478bd9Sstevel@tonic-gate /*
44647c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_string_array():
44657c478bd9Sstevel@tonic-gate  * 		Look for string array property identified by name.  The data
44667c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
44677c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
44687c478bd9Sstevel@tonic-gate  */
44697c478bd9Sstevel@tonic-gate int
44707c478bd9Sstevel@tonic-gate mdi_prop_lookup_string_array(mdi_pathinfo_t *pip, char *name, char ***data,
44717c478bd9Sstevel@tonic-gate     uint_t *nelements)
44727c478bd9Sstevel@tonic-gate {
44737c478bd9Sstevel@tonic-gate 	int rv;
44747c478bd9Sstevel@tonic-gate 
44757c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
44767c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44777c478bd9Sstevel@tonic-gate 	}
44787c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_string_array(MDI_PI(pip)->pi_prop, name, data,
44797c478bd9Sstevel@tonic-gate 	    nelements);
44807c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
44817c478bd9Sstevel@tonic-gate }
44827c478bd9Sstevel@tonic-gate 
44837c478bd9Sstevel@tonic-gate /*
44847c478bd9Sstevel@tonic-gate  * mdi_prop_free():
44857c478bd9Sstevel@tonic-gate  * 		Symmetrical function to ddi_prop_free(). nvlist_lookup_xx()
44867c478bd9Sstevel@tonic-gate  *		functions return the pointer to actual property data and not a
44877c478bd9Sstevel@tonic-gate  *		copy of it.  So the data returned is valid as long as
44887c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is valid.
44897c478bd9Sstevel@tonic-gate  */
44907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
44917c478bd9Sstevel@tonic-gate int
44927c478bd9Sstevel@tonic-gate mdi_prop_free(void *data)
44937c478bd9Sstevel@tonic-gate {
44947c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
44957c478bd9Sstevel@tonic-gate }
44967c478bd9Sstevel@tonic-gate 
44977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
44987c478bd9Sstevel@tonic-gate static void
44997c478bd9Sstevel@tonic-gate i_mdi_report_path_state(mdi_client_t *ct, mdi_pathinfo_t *pip)
45007c478bd9Sstevel@tonic-gate {
45017c478bd9Sstevel@tonic-gate 	char		*phci_path, *ct_path;
45027c478bd9Sstevel@tonic-gate 	char		*ct_status;
45037c478bd9Sstevel@tonic-gate 	char		*status;
45047c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = ct->ct_dip;
45057c478bd9Sstevel@tonic-gate 	char		lb_buf[64];
45067c478bd9Sstevel@tonic-gate 
45075e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
45087c478bd9Sstevel@tonic-gate 	if ((dip == NULL) || (ddi_get_instance(dip) == -1) ||
45097c478bd9Sstevel@tonic-gate 	    (MDI_CLIENT_IS_REPORT_DEV_NEEDED(ct) == 0)) {
45107c478bd9Sstevel@tonic-gate 		return;
45117c478bd9Sstevel@tonic-gate 	}
45127c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_OPTIMAL) {
45137c478bd9Sstevel@tonic-gate 		ct_status = "optimal";
45147c478bd9Sstevel@tonic-gate 	} else if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_DEGRADED) {
45157c478bd9Sstevel@tonic-gate 		ct_status = "degraded";
45167c478bd9Sstevel@tonic-gate 	} else if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_FAILED) {
45177c478bd9Sstevel@tonic-gate 		ct_status = "failed";
45187c478bd9Sstevel@tonic-gate 	} else {
45197c478bd9Sstevel@tonic-gate 		ct_status = "unknown";
45207c478bd9Sstevel@tonic-gate 	}
45217c478bd9Sstevel@tonic-gate 
45227c478bd9Sstevel@tonic-gate 	if (MDI_PI_IS_OFFLINE(pip)) {
45237c478bd9Sstevel@tonic-gate 		status = "offline";
45247c478bd9Sstevel@tonic-gate 	} else if (MDI_PI_IS_ONLINE(pip)) {
45257c478bd9Sstevel@tonic-gate 		status = "online";
45267c478bd9Sstevel@tonic-gate 	} else if (MDI_PI_IS_STANDBY(pip)) {
45277c478bd9Sstevel@tonic-gate 		status = "standby";
45287c478bd9Sstevel@tonic-gate 	} else if (MDI_PI_IS_FAULT(pip)) {
45297c478bd9Sstevel@tonic-gate 		status = "faulted";
45307c478bd9Sstevel@tonic-gate 	} else {
45317c478bd9Sstevel@tonic-gate 		status = "unknown";
45327c478bd9Sstevel@tonic-gate 	}
45337c478bd9Sstevel@tonic-gate 
45347c478bd9Sstevel@tonic-gate 	if (ct->ct_lb == LOAD_BALANCE_LBA) {
45357c478bd9Sstevel@tonic-gate 		(void) snprintf(lb_buf, sizeof (lb_buf),
45367c478bd9Sstevel@tonic-gate 		    "%s, region-size: %d", mdi_load_balance_lba,
45377c478bd9Sstevel@tonic-gate 			ct->ct_lb_args->region_size);
45387c478bd9Sstevel@tonic-gate 	} else if (ct->ct_lb == LOAD_BALANCE_NONE) {
45397c478bd9Sstevel@tonic-gate 		(void) snprintf(lb_buf, sizeof (lb_buf),
45407c478bd9Sstevel@tonic-gate 		    "%s", mdi_load_balance_none);
45417c478bd9Sstevel@tonic-gate 	} else {
45427c478bd9Sstevel@tonic-gate 		(void) snprintf(lb_buf, sizeof (lb_buf), "%s",
45437c478bd9Sstevel@tonic-gate 		    mdi_load_balance_rr);
45447c478bd9Sstevel@tonic-gate 	}
45457c478bd9Sstevel@tonic-gate 
45467c478bd9Sstevel@tonic-gate 	if (dip) {
45477c478bd9Sstevel@tonic-gate 		ct_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
45487c478bd9Sstevel@tonic-gate 		phci_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
45497c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) multipath status: %s, "
45507c478bd9Sstevel@tonic-gate 		    "path %s (%s%d) to target address: %s is %s"
45517c478bd9Sstevel@tonic-gate 		    " Load balancing: %s\n",
45527c478bd9Sstevel@tonic-gate 		    ddi_pathname(dip, ct_path), ddi_driver_name(dip),
45537c478bd9Sstevel@tonic-gate 		    ddi_get_instance(dip), ct_status,
45547c478bd9Sstevel@tonic-gate 		    ddi_pathname(MDI_PI(pip)->pi_phci->ph_dip, phci_path),
45557c478bd9Sstevel@tonic-gate 		    ddi_driver_name(MDI_PI(pip)->pi_phci->ph_dip),
45567c478bd9Sstevel@tonic-gate 		    ddi_get_instance(MDI_PI(pip)->pi_phci->ph_dip),
45577c478bd9Sstevel@tonic-gate 		    MDI_PI(pip)->pi_addr, status, lb_buf);
45587c478bd9Sstevel@tonic-gate 		kmem_free(phci_path, MAXPATHLEN);
45597c478bd9Sstevel@tonic-gate 		kmem_free(ct_path, MAXPATHLEN);
45607c478bd9Sstevel@tonic-gate 		MDI_CLIENT_CLEAR_REPORT_DEV_NEEDED(ct);
45617c478bd9Sstevel@tonic-gate 	}
45627c478bd9Sstevel@tonic-gate }
45637c478bd9Sstevel@tonic-gate 
45647c478bd9Sstevel@tonic-gate #ifdef	DEBUG
45657c478bd9Sstevel@tonic-gate /*
45667c478bd9Sstevel@tonic-gate  * i_mdi_log():
45677c478bd9Sstevel@tonic-gate  *		Utility function for error message management
45687c478bd9Sstevel@tonic-gate  *
45697c478bd9Sstevel@tonic-gate  */
45705e3986cbScth /*PRINTFLIKE3*/
45717c478bd9Sstevel@tonic-gate static void
45727c478bd9Sstevel@tonic-gate i_mdi_log(int level, dev_info_t *dip, const char *fmt, ...)
45737c478bd9Sstevel@tonic-gate {
4574c73a93f2Sdm120769 	char		name[MAXNAMELEN];
45755e3986cbScth 	char		buf[MAXNAMELEN];
45765e3986cbScth 	char		*bp;
45777c478bd9Sstevel@tonic-gate 	va_list		ap;
45787c478bd9Sstevel@tonic-gate 	int		log_only = 0;
45797c478bd9Sstevel@tonic-gate 	int		boot_only = 0;
45807c478bd9Sstevel@tonic-gate 	int		console_only = 0;
45817c478bd9Sstevel@tonic-gate 
45827c478bd9Sstevel@tonic-gate 	if (dip) {
4583c73a93f2Sdm120769 		(void) snprintf(name, MAXNAMELEN, "%s%d: ",
4584c73a93f2Sdm120769 		    ddi_node_name(dip), ddi_get_instance(dip));
4585c73a93f2Sdm120769 	} else {
45865e3986cbScth 		name[0] = 0;
45877c478bd9Sstevel@tonic-gate 	}
45887c478bd9Sstevel@tonic-gate 
45897c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
45907c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, MAXNAMELEN, fmt, ap);
45917c478bd9Sstevel@tonic-gate 	va_end(ap);
45927c478bd9Sstevel@tonic-gate 
45937c478bd9Sstevel@tonic-gate 	switch (buf[0]) {
45947c478bd9Sstevel@tonic-gate 	case '!':
45955e3986cbScth 		bp = &buf[1];
45967c478bd9Sstevel@tonic-gate 		log_only = 1;
45977c478bd9Sstevel@tonic-gate 		break;
45987c478bd9Sstevel@tonic-gate 	case '?':
45995e3986cbScth 		bp = &buf[1];
46007c478bd9Sstevel@tonic-gate 		boot_only = 1;
46017c478bd9Sstevel@tonic-gate 		break;
46027c478bd9Sstevel@tonic-gate 	case '^':
46035e3986cbScth 		bp = &buf[1];
46047c478bd9Sstevel@tonic-gate 		console_only = 1;
46057c478bd9Sstevel@tonic-gate 		break;
46065e3986cbScth 	default:
46075e3986cbScth 		bp = buf;
46085e3986cbScth 		break;
46095e3986cbScth 	}
46105e3986cbScth 	if (mdi_debug_logonly) {
46115e3986cbScth 		log_only = 1;
46125e3986cbScth 		boot_only = 0;
46135e3986cbScth 		console_only = 0;
46147c478bd9Sstevel@tonic-gate 	}
46157c478bd9Sstevel@tonic-gate 
46167c478bd9Sstevel@tonic-gate 	switch (level) {
46177c478bd9Sstevel@tonic-gate 	case CE_NOTE:
46187c478bd9Sstevel@tonic-gate 		level = CE_CONT;
46197c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
46207c478bd9Sstevel@tonic-gate 	case CE_CONT:
46217c478bd9Sstevel@tonic-gate 	case CE_WARN:
46227c478bd9Sstevel@tonic-gate 	case CE_PANIC:
46237c478bd9Sstevel@tonic-gate 		if (boot_only) {
46245e3986cbScth 			cmn_err(level, "?mdi: %s%s", name, bp);
46257c478bd9Sstevel@tonic-gate 		} else if (console_only) {
46265e3986cbScth 			cmn_err(level, "^mdi: %s%s", name, bp);
46277c478bd9Sstevel@tonic-gate 		} else if (log_only) {
46285e3986cbScth 			cmn_err(level, "!mdi: %s%s", name, bp);
46297c478bd9Sstevel@tonic-gate 		} else {
46305e3986cbScth 			cmn_err(level, "mdi: %s%s", name, bp);
46317c478bd9Sstevel@tonic-gate 		}
46327c478bd9Sstevel@tonic-gate 		break;
46337c478bd9Sstevel@tonic-gate 	default:
46345e3986cbScth 		cmn_err(level, "mdi: %s%s", name, bp);
46357c478bd9Sstevel@tonic-gate 		break;
46367c478bd9Sstevel@tonic-gate 	}
46377c478bd9Sstevel@tonic-gate }
46387c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
46397c478bd9Sstevel@tonic-gate 
46407c478bd9Sstevel@tonic-gate void
46417c478bd9Sstevel@tonic-gate i_mdi_client_online(dev_info_t *ct_dip)
46427c478bd9Sstevel@tonic-gate {
46437c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
46447c478bd9Sstevel@tonic-gate 
46457c478bd9Sstevel@tonic-gate 	/*
46467c478bd9Sstevel@tonic-gate 	 * Client online notification. Mark client state as online
46477c478bd9Sstevel@tonic-gate 	 * restore our binding with dev_info node
46487c478bd9Sstevel@tonic-gate 	 */
46497c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(ct_dip);
46507c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
46517c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
46527c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_ONLINE(ct);
46537c478bd9Sstevel@tonic-gate 	/* catch for any memory leaks */
46547c478bd9Sstevel@tonic-gate 	ASSERT((ct->ct_dip == NULL) || (ct->ct_dip == ct_dip));
46557c478bd9Sstevel@tonic-gate 	ct->ct_dip = ct_dip;
46567c478bd9Sstevel@tonic-gate 
46577c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0)
46587c478bd9Sstevel@tonic-gate 		(void) i_mdi_power_all_phci(ct);
46597c478bd9Sstevel@tonic-gate 
46607c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, ct_dip, "i_mdi_client_online "
46615e3986cbScth 	    "i_mdi_pm_hold_client %p\n", (void *)ct));
46627c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_client(ct, 1);
46637c478bd9Sstevel@tonic-gate 
46647c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
46657c478bd9Sstevel@tonic-gate }
46667c478bd9Sstevel@tonic-gate 
46677c478bd9Sstevel@tonic-gate void
46687c478bd9Sstevel@tonic-gate i_mdi_phci_online(dev_info_t *ph_dip)
46697c478bd9Sstevel@tonic-gate {
46707c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
46717c478bd9Sstevel@tonic-gate 
46727c478bd9Sstevel@tonic-gate 	/* pHCI online notification. Mark state accordingly */
46737c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(ph_dip);
46747c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
46757c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
46767c478bd9Sstevel@tonic-gate 	MDI_PHCI_SET_ONLINE(ph);
46777c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
46787c478bd9Sstevel@tonic-gate }
46797c478bd9Sstevel@tonic-gate 
46807c478bd9Sstevel@tonic-gate /*
46817c478bd9Sstevel@tonic-gate  * mdi_devi_online():
46827c478bd9Sstevel@tonic-gate  * 		Online notification from NDI framework on pHCI/client
46837c478bd9Sstevel@tonic-gate  *		device online.
46847c478bd9Sstevel@tonic-gate  * Return Values:
46857c478bd9Sstevel@tonic-gate  *		NDI_SUCCESS
46867c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
46877c478bd9Sstevel@tonic-gate  */
46887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
46897c478bd9Sstevel@tonic-gate int
46907c478bd9Sstevel@tonic-gate mdi_devi_online(dev_info_t *dip, uint_t flags)
46917c478bd9Sstevel@tonic-gate {
46927c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
46937c478bd9Sstevel@tonic-gate 		i_mdi_phci_online(dip);
46947c478bd9Sstevel@tonic-gate 	}
46957c478bd9Sstevel@tonic-gate 
46967c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
46977c478bd9Sstevel@tonic-gate 		i_mdi_client_online(dip);
46987c478bd9Sstevel@tonic-gate 	}
46997c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
47007c478bd9Sstevel@tonic-gate }
47017c478bd9Sstevel@tonic-gate 
47027c478bd9Sstevel@tonic-gate /*
47037c478bd9Sstevel@tonic-gate  * mdi_devi_offline():
47047c478bd9Sstevel@tonic-gate  * 		Offline notification from NDI framework on pHCI/Client device
47057c478bd9Sstevel@tonic-gate  *		offline.
47067c478bd9Sstevel@tonic-gate  *
47077c478bd9Sstevel@tonic-gate  * Return Values:
47087c478bd9Sstevel@tonic-gate  *		NDI_SUCCESS
47097c478bd9Sstevel@tonic-gate  *		NDI_FAILURE
47107c478bd9Sstevel@tonic-gate  */
47117c478bd9Sstevel@tonic-gate /*ARGSUSED*/
47127c478bd9Sstevel@tonic-gate int
47137c478bd9Sstevel@tonic-gate mdi_devi_offline(dev_info_t *dip, uint_t flags)
47147c478bd9Sstevel@tonic-gate {
47157c478bd9Sstevel@tonic-gate 	int		rv = NDI_SUCCESS;
47167c478bd9Sstevel@tonic-gate 
47177c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
47187c478bd9Sstevel@tonic-gate 		rv = i_mdi_client_offline(dip, flags);
47197c478bd9Sstevel@tonic-gate 		if (rv != NDI_SUCCESS)
47207c478bd9Sstevel@tonic-gate 			return (rv);
47217c478bd9Sstevel@tonic-gate 	}
47227c478bd9Sstevel@tonic-gate 
47237c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
47247c478bd9Sstevel@tonic-gate 		rv = i_mdi_phci_offline(dip, flags);
47255e3986cbScth 
47267c478bd9Sstevel@tonic-gate 		if ((rv != NDI_SUCCESS) && MDI_CLIENT(dip)) {
47277c478bd9Sstevel@tonic-gate 			/* set client back online */
47287c478bd9Sstevel@tonic-gate 			i_mdi_client_online(dip);
47297c478bd9Sstevel@tonic-gate 		}
47307c478bd9Sstevel@tonic-gate 	}
47317c478bd9Sstevel@tonic-gate 
47327c478bd9Sstevel@tonic-gate 	return (rv);
47337c478bd9Sstevel@tonic-gate }
47347c478bd9Sstevel@tonic-gate 
47357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
47367c478bd9Sstevel@tonic-gate static int
47377c478bd9Sstevel@tonic-gate i_mdi_phci_offline(dev_info_t *dip, uint_t flags)
47387c478bd9Sstevel@tonic-gate {
47397c478bd9Sstevel@tonic-gate 	int		rv = NDI_SUCCESS;
47407c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
47417c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
47427c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
47437c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
47447c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*failed_pip = NULL;
47457c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
47467c478bd9Sstevel@tonic-gate 
47477c478bd9Sstevel@tonic-gate 	/*
47487c478bd9Sstevel@tonic-gate 	 * pHCI component offline notification
47497c478bd9Sstevel@tonic-gate 	 * Make sure that this pHCI instance is free to be offlined.
47507c478bd9Sstevel@tonic-gate 	 * If it is OK to proceed, Offline and remove all the child
47517c478bd9Sstevel@tonic-gate 	 * mdi_pathinfo nodes.  This process automatically offlines
47527c478bd9Sstevel@tonic-gate 	 * corresponding client devices, for which this pHCI provides
47537c478bd9Sstevel@tonic-gate 	 * critical services.
47547c478bd9Sstevel@tonic-gate 	 */
47557c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(dip);
47565e3986cbScth 	MDI_DEBUG(2, (CE_NOTE, dip, "!mdi_phci_offline called %p %p\n",
47575e3986cbScth 	    (void *)dip, (void *)ph));
47587c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
47597c478bd9Sstevel@tonic-gate 		return (rv);
47607c478bd9Sstevel@tonic-gate 	}
47617c478bd9Sstevel@tonic-gate 
47627c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
47637c478bd9Sstevel@tonic-gate 
47647c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_OFFLINE(ph)) {
47655e3986cbScth 		MDI_DEBUG(1, (CE_WARN, dip, "!pHCI %p already offlined",
47665e3986cbScth 		    (void *)ph));
47677c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
47687c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
47697c478bd9Sstevel@tonic-gate 	}
47707c478bd9Sstevel@tonic-gate 
47717c478bd9Sstevel@tonic-gate 	/*
47727c478bd9Sstevel@tonic-gate 	 * Check to see if the pHCI can be offlined
47737c478bd9Sstevel@tonic-gate 	 */
47747c478bd9Sstevel@tonic-gate 	if (ph->ph_unstable) {
47757c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, dip,
47767c478bd9Sstevel@tonic-gate 		    "!One or more target devices are in transient "
47777c478bd9Sstevel@tonic-gate 		    "state. This device can not be removed at "
47787c478bd9Sstevel@tonic-gate 		    "this moment. Please try again later."));
47797c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
47807c478bd9Sstevel@tonic-gate 		return (NDI_BUSY);
47817c478bd9Sstevel@tonic-gate 	}
47827c478bd9Sstevel@tonic-gate 
47837c478bd9Sstevel@tonic-gate 	pip = ph->ph_path_head;
47847c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
47857c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
47867c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
47875e3986cbScth 
47887c478bd9Sstevel@tonic-gate 		/*
47897c478bd9Sstevel@tonic-gate 		 * The mdi_pathinfo state is OK. Check the client state.
47907c478bd9Sstevel@tonic-gate 		 * If failover in progress fail the pHCI from offlining
47917c478bd9Sstevel@tonic-gate 		 */
47927c478bd9Sstevel@tonic-gate 		ct = MDI_PI(pip)->pi_client;
47937c478bd9Sstevel@tonic-gate 		i_mdi_client_lock(ct, pip);
47947c478bd9Sstevel@tonic-gate 		if ((MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) ||
47957c478bd9Sstevel@tonic-gate 		    (ct->ct_unstable)) {
47967c478bd9Sstevel@tonic-gate 			/*
47977c478bd9Sstevel@tonic-gate 			 * Failover is in progress, Fail the DR
47987c478bd9Sstevel@tonic-gate 			 */
47997c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_WARN, dip,
48007c478bd9Sstevel@tonic-gate 			    "!pHCI device (%s%d) is Busy. %s",
48017c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
48027c478bd9Sstevel@tonic-gate 			    "This device can not be removed at "
48037c478bd9Sstevel@tonic-gate 			    "this moment. Please try again later."));
48047c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
48055e3986cbScth 			i_mdi_client_unlock(ct);
48067c478bd9Sstevel@tonic-gate 			MDI_PHCI_UNLOCK(ph);
48077c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
48087c478bd9Sstevel@tonic-gate 		}
48097c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
48107c478bd9Sstevel@tonic-gate 
48117c478bd9Sstevel@tonic-gate 		/*
48127c478bd9Sstevel@tonic-gate 		 * Check to see of we are removing the last path of this
48137c478bd9Sstevel@tonic-gate 		 * client device...
48147c478bd9Sstevel@tonic-gate 		 */
48157c478bd9Sstevel@tonic-gate 		cdip = ct->ct_dip;
48167c478bd9Sstevel@tonic-gate 		if (cdip && (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
48177c478bd9Sstevel@tonic-gate 		    (i_mdi_client_compute_state(ct, ph) ==
48187c478bd9Sstevel@tonic-gate 		    MDI_CLIENT_STATE_FAILED)) {
48197c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
48207c478bd9Sstevel@tonic-gate 			MDI_PHCI_UNLOCK(ph);
48217c478bd9Sstevel@tonic-gate 			if (ndi_devi_offline(cdip, 0) != NDI_SUCCESS) {
48227c478bd9Sstevel@tonic-gate 				/*
48237c478bd9Sstevel@tonic-gate 				 * ndi_devi_offline() failed.
48247c478bd9Sstevel@tonic-gate 				 * This pHCI provides the critical path
48257c478bd9Sstevel@tonic-gate 				 * to one or more client devices.
48267c478bd9Sstevel@tonic-gate 				 * Return busy.
48277c478bd9Sstevel@tonic-gate 				 */
48287c478bd9Sstevel@tonic-gate 				MDI_PHCI_LOCK(ph);
48297c478bd9Sstevel@tonic-gate 				MDI_DEBUG(1, (CE_WARN, dip,
48307c478bd9Sstevel@tonic-gate 				    "!pHCI device (%s%d) is Busy. %s",
48317c478bd9Sstevel@tonic-gate 				    ddi_driver_name(dip), ddi_get_instance(dip),
48327c478bd9Sstevel@tonic-gate 				    "This device can not be removed at "
48337c478bd9Sstevel@tonic-gate 				    "this moment. Please try again later."));
48347c478bd9Sstevel@tonic-gate 				failed_pip = pip;
48357c478bd9Sstevel@tonic-gate 				break;
48367c478bd9Sstevel@tonic-gate 			} else {
48377c478bd9Sstevel@tonic-gate 				MDI_PHCI_LOCK(ph);
48387c478bd9Sstevel@tonic-gate 				pip = next;
48397c478bd9Sstevel@tonic-gate 			}
48407c478bd9Sstevel@tonic-gate 		} else {
48417c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
48427c478bd9Sstevel@tonic-gate 			pip = next;
48437c478bd9Sstevel@tonic-gate 		}
48447c478bd9Sstevel@tonic-gate 	}
48457c478bd9Sstevel@tonic-gate 
48467c478bd9Sstevel@tonic-gate 	if (failed_pip) {
48477c478bd9Sstevel@tonic-gate 		pip = ph->ph_path_head;
48487c478bd9Sstevel@tonic-gate 		while (pip != failed_pip) {
48497c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
48507c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
48517c478bd9Sstevel@tonic-gate 			ct = MDI_PI(pip)->pi_client;
48527c478bd9Sstevel@tonic-gate 			i_mdi_client_lock(ct, pip);
48537c478bd9Sstevel@tonic-gate 			cdip = ct->ct_dip;
48547c478bd9Sstevel@tonic-gate 			switch (MDI_CLIENT_STATE(ct)) {
48557c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_OPTIMAL:
48567c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_DEGRADED:
48577c478bd9Sstevel@tonic-gate 				if (cdip) {
48587c478bd9Sstevel@tonic-gate 					MDI_PI_UNLOCK(pip);
48597c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
48607c478bd9Sstevel@tonic-gate 					MDI_PHCI_UNLOCK(ph);
48617c478bd9Sstevel@tonic-gate 					(void) ndi_devi_online(cdip, 0);
48627c478bd9Sstevel@tonic-gate 					MDI_PHCI_LOCK(ph);
48637c478bd9Sstevel@tonic-gate 					pip = next;
48647c478bd9Sstevel@tonic-gate 					continue;
48657c478bd9Sstevel@tonic-gate 				}
48667c478bd9Sstevel@tonic-gate 				break;
48677c478bd9Sstevel@tonic-gate 
48687c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_FAILED:
48697c478bd9Sstevel@tonic-gate 				if (cdip) {
48707c478bd9Sstevel@tonic-gate 					MDI_PI_UNLOCK(pip);
48717c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
48727c478bd9Sstevel@tonic-gate 					MDI_PHCI_UNLOCK(ph);
48737c478bd9Sstevel@tonic-gate 					(void) ndi_devi_offline(cdip, 0);
48747c478bd9Sstevel@tonic-gate 					MDI_PHCI_LOCK(ph);
48757c478bd9Sstevel@tonic-gate 					pip = next;
48767c478bd9Sstevel@tonic-gate 					continue;
48777c478bd9Sstevel@tonic-gate 				}
48787c478bd9Sstevel@tonic-gate 				break;
48797c478bd9Sstevel@tonic-gate 			}
48807c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
48817c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
48827c478bd9Sstevel@tonic-gate 			pip = next;
48837c478bd9Sstevel@tonic-gate 		}
48847c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
48857c478bd9Sstevel@tonic-gate 		return (NDI_BUSY);
48867c478bd9Sstevel@tonic-gate 	}
48877c478bd9Sstevel@tonic-gate 
48887c478bd9Sstevel@tonic-gate 	/*
48897c478bd9Sstevel@tonic-gate 	 * Mark the pHCI as offline
48907c478bd9Sstevel@tonic-gate 	 */
48917c478bd9Sstevel@tonic-gate 	MDI_PHCI_SET_OFFLINE(ph);
48927c478bd9Sstevel@tonic-gate 
48937c478bd9Sstevel@tonic-gate 	/*
48947c478bd9Sstevel@tonic-gate 	 * Mark the child mdi_pathinfo nodes as transient
48957c478bd9Sstevel@tonic-gate 	 */
48967c478bd9Sstevel@tonic-gate 	pip = ph->ph_path_head;
48977c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
48987c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
48997c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
49007c478bd9Sstevel@tonic-gate 		MDI_PI_SET_OFFLINING(pip);
49017c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
49027c478bd9Sstevel@tonic-gate 		pip = next;
49037c478bd9Sstevel@tonic-gate 	}
49047c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
49057c478bd9Sstevel@tonic-gate 	/*
49067c478bd9Sstevel@tonic-gate 	 * Give a chance for any pending commands to execute
49077c478bd9Sstevel@tonic-gate 	 */
49087c478bd9Sstevel@tonic-gate 	delay(1);
49097c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
49107c478bd9Sstevel@tonic-gate 	pip = ph->ph_path_head;
49117c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
49127c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
49137c478bd9Sstevel@tonic-gate 		(void) i_mdi_pi_offline(pip, flags);
49147c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
49157c478bd9Sstevel@tonic-gate 		ct = MDI_PI(pip)->pi_client;
49167c478bd9Sstevel@tonic-gate 		if (!MDI_PI_IS_OFFLINE(pip)) {
49177c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_WARN, dip,
49187c478bd9Sstevel@tonic-gate 			    "!pHCI device (%s%d) is Busy. %s",
49197c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
49207c478bd9Sstevel@tonic-gate 			    "This device can not be removed at "
49217c478bd9Sstevel@tonic-gate 			    "this moment. Please try again later."));
49227c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
49237c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_ONLINE(ph);
49247c478bd9Sstevel@tonic-gate 			MDI_PHCI_UNLOCK(ph);
49257c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
49267c478bd9Sstevel@tonic-gate 		}
49277c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
49287c478bd9Sstevel@tonic-gate 		pip = next;
49297c478bd9Sstevel@tonic-gate 	}
49307c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
49317c478bd9Sstevel@tonic-gate 
49327c478bd9Sstevel@tonic-gate 	return (rv);
49337c478bd9Sstevel@tonic-gate }
49347c478bd9Sstevel@tonic-gate 
493525e8c5aaSvikram void
493625e8c5aaSvikram mdi_phci_mark_retiring(dev_info_t *dip, char **cons_array)
493725e8c5aaSvikram {
493825e8c5aaSvikram 	mdi_phci_t	*ph;
493925e8c5aaSvikram 	mdi_client_t	*ct;
494025e8c5aaSvikram 	mdi_pathinfo_t	*pip;
494125e8c5aaSvikram 	mdi_pathinfo_t	*next;
494225e8c5aaSvikram 	dev_info_t	*cdip;
494325e8c5aaSvikram 
494425e8c5aaSvikram 	if (!MDI_PHCI(dip))
494525e8c5aaSvikram 		return;
494625e8c5aaSvikram 
494725e8c5aaSvikram 	ph = i_devi_get_phci(dip);
494825e8c5aaSvikram 	if (ph == NULL) {
494925e8c5aaSvikram 		return;
495025e8c5aaSvikram 	}
495125e8c5aaSvikram 
495225e8c5aaSvikram 	MDI_PHCI_LOCK(ph);
495325e8c5aaSvikram 
495425e8c5aaSvikram 	if (MDI_PHCI_IS_OFFLINE(ph)) {
495525e8c5aaSvikram 		/* has no last path */
495625e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
495725e8c5aaSvikram 		return;
495825e8c5aaSvikram 	}
495925e8c5aaSvikram 
496025e8c5aaSvikram 	pip = ph->ph_path_head;
496125e8c5aaSvikram 	while (pip != NULL) {
496225e8c5aaSvikram 		MDI_PI_LOCK(pip);
496325e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
496425e8c5aaSvikram 
496525e8c5aaSvikram 		ct = MDI_PI(pip)->pi_client;
496625e8c5aaSvikram 		i_mdi_client_lock(ct, pip);
496725e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
496825e8c5aaSvikram 
496925e8c5aaSvikram 		cdip = ct->ct_dip;
497025e8c5aaSvikram 		if (cdip && (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
497125e8c5aaSvikram 		    (i_mdi_client_compute_state(ct, ph) ==
497225e8c5aaSvikram 		    MDI_CLIENT_STATE_FAILED)) {
497325e8c5aaSvikram 			/* Last path. Mark client dip as retiring */
497425e8c5aaSvikram 			i_mdi_client_unlock(ct);
497525e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
497625e8c5aaSvikram 			(void) e_ddi_mark_retiring(cdip, cons_array);
497725e8c5aaSvikram 			MDI_PHCI_LOCK(ph);
497825e8c5aaSvikram 			pip = next;
497925e8c5aaSvikram 		} else {
498025e8c5aaSvikram 			i_mdi_client_unlock(ct);
498125e8c5aaSvikram 			pip = next;
498225e8c5aaSvikram 		}
498325e8c5aaSvikram 	}
498425e8c5aaSvikram 
498525e8c5aaSvikram 	MDI_PHCI_UNLOCK(ph);
498625e8c5aaSvikram 
498725e8c5aaSvikram 	return;
498825e8c5aaSvikram }
498925e8c5aaSvikram 
499025e8c5aaSvikram void
499125e8c5aaSvikram mdi_phci_retire_notify(dev_info_t *dip, int *constraint)
499225e8c5aaSvikram {
499325e8c5aaSvikram 	mdi_phci_t	*ph;
499425e8c5aaSvikram 	mdi_client_t	*ct;
499525e8c5aaSvikram 	mdi_pathinfo_t	*pip;
499625e8c5aaSvikram 	mdi_pathinfo_t	*next;
499725e8c5aaSvikram 	dev_info_t	*cdip;
499825e8c5aaSvikram 
499925e8c5aaSvikram 	if (!MDI_PHCI(dip))
500025e8c5aaSvikram 		return;
500125e8c5aaSvikram 
500225e8c5aaSvikram 	ph = i_devi_get_phci(dip);
500325e8c5aaSvikram 	if (ph == NULL)
500425e8c5aaSvikram 		return;
500525e8c5aaSvikram 
500625e8c5aaSvikram 	MDI_PHCI_LOCK(ph);
500725e8c5aaSvikram 
500825e8c5aaSvikram 	if (MDI_PHCI_IS_OFFLINE(ph)) {
500925e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
501025e8c5aaSvikram 		/* not last path */
501125e8c5aaSvikram 		return;
501225e8c5aaSvikram 	}
501325e8c5aaSvikram 
501425e8c5aaSvikram 	if (ph->ph_unstable) {
501525e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
501625e8c5aaSvikram 		/* can't check for constraints */
501725e8c5aaSvikram 		*constraint = 0;
501825e8c5aaSvikram 		return;
501925e8c5aaSvikram 	}
502025e8c5aaSvikram 
502125e8c5aaSvikram 	pip = ph->ph_path_head;
502225e8c5aaSvikram 	while (pip != NULL) {
502325e8c5aaSvikram 		MDI_PI_LOCK(pip);
502425e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
502525e8c5aaSvikram 
502625e8c5aaSvikram 		/*
502725e8c5aaSvikram 		 * The mdi_pathinfo state is OK. Check the client state.
502825e8c5aaSvikram 		 * If failover in progress fail the pHCI from offlining
502925e8c5aaSvikram 		 */
503025e8c5aaSvikram 		ct = MDI_PI(pip)->pi_client;
503125e8c5aaSvikram 		i_mdi_client_lock(ct, pip);
503225e8c5aaSvikram 		if ((MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) ||
503325e8c5aaSvikram 		    (ct->ct_unstable)) {
503425e8c5aaSvikram 			/*
503525e8c5aaSvikram 			 * Failover is in progress, can't check for constraints
503625e8c5aaSvikram 			 */
503725e8c5aaSvikram 			MDI_PI_UNLOCK(pip);
503825e8c5aaSvikram 			i_mdi_client_unlock(ct);
503925e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
504025e8c5aaSvikram 			*constraint = 0;
504125e8c5aaSvikram 			return;
504225e8c5aaSvikram 		}
504325e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
504425e8c5aaSvikram 
504525e8c5aaSvikram 		/*
504625e8c5aaSvikram 		 * Check to see of we are retiring the last path of this
504725e8c5aaSvikram 		 * client device...
504825e8c5aaSvikram 		 */
504925e8c5aaSvikram 		cdip = ct->ct_dip;
505025e8c5aaSvikram 		if (cdip && (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
505125e8c5aaSvikram 		    (i_mdi_client_compute_state(ct, ph) ==
505225e8c5aaSvikram 		    MDI_CLIENT_STATE_FAILED)) {
505325e8c5aaSvikram 			i_mdi_client_unlock(ct);
505425e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
505525e8c5aaSvikram 			(void) e_ddi_retire_notify(cdip, constraint);
505625e8c5aaSvikram 			MDI_PHCI_LOCK(ph);
505725e8c5aaSvikram 			pip = next;
505825e8c5aaSvikram 		} else {
505925e8c5aaSvikram 			i_mdi_client_unlock(ct);
506025e8c5aaSvikram 			pip = next;
506125e8c5aaSvikram 		}
506225e8c5aaSvikram 	}
506325e8c5aaSvikram 
506425e8c5aaSvikram 	MDI_PHCI_UNLOCK(ph);
506525e8c5aaSvikram 
506625e8c5aaSvikram 	return;
506725e8c5aaSvikram }
506825e8c5aaSvikram 
506925e8c5aaSvikram /*
507025e8c5aaSvikram  * offline the path(s) hanging off the PHCI. If the
507125e8c5aaSvikram  * last path to any client, check that constraints
507225e8c5aaSvikram  * have been applied.
507325e8c5aaSvikram  */
507425e8c5aaSvikram void
507525e8c5aaSvikram mdi_phci_retire_finalize(dev_info_t *dip, int phci_only)
507625e8c5aaSvikram {
507725e8c5aaSvikram 	mdi_phci_t	*ph;
507825e8c5aaSvikram 	mdi_client_t	*ct;
507925e8c5aaSvikram 	mdi_pathinfo_t	*pip;
508025e8c5aaSvikram 	mdi_pathinfo_t	*next;
508125e8c5aaSvikram 	dev_info_t	*cdip;
508225e8c5aaSvikram 	int		unstable = 0;
508325e8c5aaSvikram 	int		constraint;
508425e8c5aaSvikram 
508525e8c5aaSvikram 	if (!MDI_PHCI(dip))
508625e8c5aaSvikram 		return;
508725e8c5aaSvikram 
508825e8c5aaSvikram 	ph = i_devi_get_phci(dip);
508925e8c5aaSvikram 	if (ph == NULL) {
509025e8c5aaSvikram 		/* no last path and no pips */
509125e8c5aaSvikram 		return;
509225e8c5aaSvikram 	}
509325e8c5aaSvikram 
509425e8c5aaSvikram 	MDI_PHCI_LOCK(ph);
509525e8c5aaSvikram 
509625e8c5aaSvikram 	if (MDI_PHCI_IS_OFFLINE(ph)) {
509725e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
509825e8c5aaSvikram 		/* no last path and no pips */
509925e8c5aaSvikram 		return;
510025e8c5aaSvikram 	}
510125e8c5aaSvikram 
510225e8c5aaSvikram 	/*
510325e8c5aaSvikram 	 * Check to see if the pHCI can be offlined
510425e8c5aaSvikram 	 */
510525e8c5aaSvikram 	if (ph->ph_unstable) {
510625e8c5aaSvikram 		unstable = 1;
510725e8c5aaSvikram 	}
510825e8c5aaSvikram 
510925e8c5aaSvikram 	pip = ph->ph_path_head;
511025e8c5aaSvikram 	while (pip != NULL) {
511125e8c5aaSvikram 		MDI_PI_LOCK(pip);
511225e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
511325e8c5aaSvikram 
511425e8c5aaSvikram 		/*
511525e8c5aaSvikram 		 * if failover in progress fail the pHCI from offlining
511625e8c5aaSvikram 		 */
511725e8c5aaSvikram 		ct = MDI_PI(pip)->pi_client;
511825e8c5aaSvikram 		i_mdi_client_lock(ct, pip);
511925e8c5aaSvikram 		if ((MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) ||
512025e8c5aaSvikram 		    (ct->ct_unstable)) {
512125e8c5aaSvikram 			unstable = 1;
512225e8c5aaSvikram 		}
512325e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
512425e8c5aaSvikram 
512525e8c5aaSvikram 		/*
512625e8c5aaSvikram 		 * Check to see of we are removing the last path of this
512725e8c5aaSvikram 		 * client device...
512825e8c5aaSvikram 		 */
512925e8c5aaSvikram 		cdip = ct->ct_dip;
513025e8c5aaSvikram 		if (!phci_only && cdip &&
513125e8c5aaSvikram 		    (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
513225e8c5aaSvikram 		    (i_mdi_client_compute_state(ct, ph) ==
513325e8c5aaSvikram 		    MDI_CLIENT_STATE_FAILED)) {
513425e8c5aaSvikram 			i_mdi_client_unlock(ct);
513525e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
513625e8c5aaSvikram 			/*
513725e8c5aaSvikram 			 * We don't retire clients we just retire the
513825e8c5aaSvikram 			 * path to a client. If it is the last path
513925e8c5aaSvikram 			 * to a client, constraints are checked and
514025e8c5aaSvikram 			 * if we pass the last path is offlined. MPXIO will
514125e8c5aaSvikram 			 * then fail all I/Os to the client. Since we don't
514225e8c5aaSvikram 			 * want to retire the client on a path error
514325e8c5aaSvikram 			 * set constraint = 0 so that the client dip
514425e8c5aaSvikram 			 * is not retired.
514525e8c5aaSvikram 			 */
514625e8c5aaSvikram 			constraint = 0;
514725e8c5aaSvikram 			(void) e_ddi_retire_finalize(cdip, &constraint);
514825e8c5aaSvikram 			MDI_PHCI_LOCK(ph);
514925e8c5aaSvikram 			pip = next;
515025e8c5aaSvikram 		} else {
515125e8c5aaSvikram 			i_mdi_client_unlock(ct);
515225e8c5aaSvikram 			pip = next;
515325e8c5aaSvikram 		}
515425e8c5aaSvikram 	}
515525e8c5aaSvikram 
515625e8c5aaSvikram 	/*
515725e8c5aaSvikram 	 * Cannot offline pip(s)
515825e8c5aaSvikram 	 */
515925e8c5aaSvikram 	if (unstable) {
516025e8c5aaSvikram 		cmn_err(CE_WARN, "PHCI in transient state, cannot "
516125e8c5aaSvikram 		    "retire, dip = %p", (void *)dip);
516225e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
516325e8c5aaSvikram 		return;
516425e8c5aaSvikram 	}
516525e8c5aaSvikram 
516625e8c5aaSvikram 	/*
516725e8c5aaSvikram 	 * Mark the pHCI as offline
516825e8c5aaSvikram 	 */
516925e8c5aaSvikram 	MDI_PHCI_SET_OFFLINE(ph);
517025e8c5aaSvikram 
517125e8c5aaSvikram 	/*
517225e8c5aaSvikram 	 * Mark the child mdi_pathinfo nodes as transient
517325e8c5aaSvikram 	 */
517425e8c5aaSvikram 	pip = ph->ph_path_head;
517525e8c5aaSvikram 	while (pip != NULL) {
517625e8c5aaSvikram 		MDI_PI_LOCK(pip);
517725e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
517825e8c5aaSvikram 		MDI_PI_SET_OFFLINING(pip);
517925e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
518025e8c5aaSvikram 		pip = next;
518125e8c5aaSvikram 	}
518225e8c5aaSvikram 	MDI_PHCI_UNLOCK(ph);
518325e8c5aaSvikram 	/*
518425e8c5aaSvikram 	 * Give a chance for any pending commands to execute
518525e8c5aaSvikram 	 */
518625e8c5aaSvikram 	delay(1);
518725e8c5aaSvikram 	MDI_PHCI_LOCK(ph);
518825e8c5aaSvikram 	pip = ph->ph_path_head;
518925e8c5aaSvikram 	while (pip != NULL) {
519025e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
519125e8c5aaSvikram 		(void) i_mdi_pi_offline(pip, 0);
519225e8c5aaSvikram 		MDI_PI_LOCK(pip);
519325e8c5aaSvikram 		ct = MDI_PI(pip)->pi_client;
519425e8c5aaSvikram 		if (!MDI_PI_IS_OFFLINE(pip)) {
519525e8c5aaSvikram 			cmn_err(CE_WARN, "PHCI busy, cannot offline path: "
519625e8c5aaSvikram 			    "PHCI dip = %p", (void *)dip);
519725e8c5aaSvikram 			MDI_PI_UNLOCK(pip);
519825e8c5aaSvikram 			MDI_PHCI_SET_ONLINE(ph);
519925e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
520025e8c5aaSvikram 			return;
520125e8c5aaSvikram 		}
520225e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
520325e8c5aaSvikram 		pip = next;
520425e8c5aaSvikram 	}
520525e8c5aaSvikram 	MDI_PHCI_UNLOCK(ph);
520625e8c5aaSvikram 
520725e8c5aaSvikram 	return;
520825e8c5aaSvikram }
520925e8c5aaSvikram 
521025e8c5aaSvikram void
521125e8c5aaSvikram mdi_phci_unretire(dev_info_t *dip)
521225e8c5aaSvikram {
521325e8c5aaSvikram 	ASSERT(MDI_PHCI(dip));
521425e8c5aaSvikram 
521525e8c5aaSvikram 	/*
521625e8c5aaSvikram 	 * Online the phci
521725e8c5aaSvikram 	 */
521825e8c5aaSvikram 	i_mdi_phci_online(dip);
521925e8c5aaSvikram }
522025e8c5aaSvikram 
52217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
52227c478bd9Sstevel@tonic-gate static int
52237c478bd9Sstevel@tonic-gate i_mdi_client_offline(dev_info_t *dip, uint_t flags)
52247c478bd9Sstevel@tonic-gate {
52257c478bd9Sstevel@tonic-gate 	int		rv = NDI_SUCCESS;
52267c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
52277c478bd9Sstevel@tonic-gate 
52287c478bd9Sstevel@tonic-gate 	/*
52297c478bd9Sstevel@tonic-gate 	 * Client component to go offline.  Make sure that we are
52307c478bd9Sstevel@tonic-gate 	 * not in failing over state and update client state
52317c478bd9Sstevel@tonic-gate 	 * accordingly
52327c478bd9Sstevel@tonic-gate 	 */
52337c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(dip);
52345e3986cbScth 	MDI_DEBUG(2, (CE_NOTE, dip, "!i_mdi_client_offline called %p %p\n",
52355e3986cbScth 	    (void *)dip, (void *)ct));
52367c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
52377c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
52387c478bd9Sstevel@tonic-gate 		if (ct->ct_unstable) {
52397c478bd9Sstevel@tonic-gate 			/*
52407c478bd9Sstevel@tonic-gate 			 * One or more paths are in transient state,
52417c478bd9Sstevel@tonic-gate 			 * Dont allow offline of a client device
52427c478bd9Sstevel@tonic-gate 			 */
52437c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_WARN, dip,
52447c478bd9Sstevel@tonic-gate 			    "!One or more paths to this device is "
52457c478bd9Sstevel@tonic-gate 			    "in transient state. This device can not "
52467c478bd9Sstevel@tonic-gate 			    "be removed at this moment. "
52477c478bd9Sstevel@tonic-gate 			    "Please try again later."));
52487c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
52497c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
52507c478bd9Sstevel@tonic-gate 		}
52517c478bd9Sstevel@tonic-gate 		if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
52527c478bd9Sstevel@tonic-gate 			/*
52537c478bd9Sstevel@tonic-gate 			 * Failover is in progress, Dont allow DR of
52547c478bd9Sstevel@tonic-gate 			 * a client device
52557c478bd9Sstevel@tonic-gate 			 */
52567c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_WARN, dip,
52577c478bd9Sstevel@tonic-gate 			    "!Client device (%s%d) is Busy. %s",
52587c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
52597c478bd9Sstevel@tonic-gate 			    "This device can not be removed at "
52607c478bd9Sstevel@tonic-gate 			    "this moment. Please try again later."));
52617c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
52627c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
52637c478bd9Sstevel@tonic-gate 		}
52647c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_OFFLINE(ct);
52657c478bd9Sstevel@tonic-gate 
52667c478bd9Sstevel@tonic-gate 		/*
52677c478bd9Sstevel@tonic-gate 		 * Unbind our relationship with the dev_info node
52687c478bd9Sstevel@tonic-gate 		 */
52697c478bd9Sstevel@tonic-gate 		if (flags & NDI_DEVI_REMOVE) {
52707c478bd9Sstevel@tonic-gate 			ct->ct_dip = NULL;
52717c478bd9Sstevel@tonic-gate 		}
52727c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
52737c478bd9Sstevel@tonic-gate 	}
52747c478bd9Sstevel@tonic-gate 	return (rv);
52757c478bd9Sstevel@tonic-gate }
52767c478bd9Sstevel@tonic-gate 
52777c478bd9Sstevel@tonic-gate /*
52787c478bd9Sstevel@tonic-gate  * mdi_pre_attach():
52797c478bd9Sstevel@tonic-gate  *		Pre attach() notification handler
52807c478bd9Sstevel@tonic-gate  */
52817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
52827c478bd9Sstevel@tonic-gate int
52837c478bd9Sstevel@tonic-gate mdi_pre_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
52847c478bd9Sstevel@tonic-gate {
52857c478bd9Sstevel@tonic-gate 	/* don't support old DDI_PM_RESUME */
52867c478bd9Sstevel@tonic-gate 	if ((DEVI(dip)->devi_mdi_component != MDI_COMPONENT_NONE) &&
52877c478bd9Sstevel@tonic-gate 	    (cmd == DDI_PM_RESUME))
52887c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
52897c478bd9Sstevel@tonic-gate 
52907c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
52917c478bd9Sstevel@tonic-gate }
52927c478bd9Sstevel@tonic-gate 
52937c478bd9Sstevel@tonic-gate /*
52947c478bd9Sstevel@tonic-gate  * mdi_post_attach():
52957c478bd9Sstevel@tonic-gate  *		Post attach() notification handler
52967c478bd9Sstevel@tonic-gate  */
52977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
52987c478bd9Sstevel@tonic-gate void
52997c478bd9Sstevel@tonic-gate mdi_post_attach(dev_info_t *dip, ddi_attach_cmd_t cmd, int error)
53007c478bd9Sstevel@tonic-gate {
53017c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
53027c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
530337fbbce5Scth 	mdi_vhci_t	*vh;
53047c478bd9Sstevel@tonic-gate 
53057c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
53067c478bd9Sstevel@tonic-gate 		ph = i_devi_get_phci(dip);
53077c478bd9Sstevel@tonic-gate 		ASSERT(ph != NULL);
53087c478bd9Sstevel@tonic-gate 
53097c478bd9Sstevel@tonic-gate 		MDI_PHCI_LOCK(ph);
53107c478bd9Sstevel@tonic-gate 		switch (cmd) {
53117c478bd9Sstevel@tonic-gate 		case DDI_ATTACH:
53127c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_NOTE, dip,
53135e3986cbScth 			    "!pHCI post_attach: called %p\n", (void *)ph));
53147c478bd9Sstevel@tonic-gate 			if (error == DDI_SUCCESS) {
53157c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_ATTACH(ph);
53167c478bd9Sstevel@tonic-gate 			} else {
53177c478bd9Sstevel@tonic-gate 				MDI_DEBUG(1, (CE_NOTE, dip,
53187c478bd9Sstevel@tonic-gate 				    "!pHCI post_attach: failed error=%d\n",
53197c478bd9Sstevel@tonic-gate 				    error));
53207c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_DETACH(ph);
53217c478bd9Sstevel@tonic-gate 			}
53227c478bd9Sstevel@tonic-gate 			break;
53237c478bd9Sstevel@tonic-gate 
53247c478bd9Sstevel@tonic-gate 		case DDI_RESUME:
53257c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_NOTE, dip,
53265e3986cbScth 			    "!pHCI post_resume: called %p\n", (void *)ph));
53277c478bd9Sstevel@tonic-gate 			if (error == DDI_SUCCESS) {
53287c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_RESUME(ph);
53297c478bd9Sstevel@tonic-gate 			} else {
53307c478bd9Sstevel@tonic-gate 				MDI_DEBUG(1, (CE_NOTE, dip,
53317c478bd9Sstevel@tonic-gate 				    "!pHCI post_resume: failed error=%d\n",
53327c478bd9Sstevel@tonic-gate 				    error));
53337c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_SUSPEND(ph);
53347c478bd9Sstevel@tonic-gate 			}
53357c478bd9Sstevel@tonic-gate 			break;
53367c478bd9Sstevel@tonic-gate 		}
53377c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
53387c478bd9Sstevel@tonic-gate 	}
53397c478bd9Sstevel@tonic-gate 
53407c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
53417c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(dip);
53427c478bd9Sstevel@tonic-gate 		ASSERT(ct != NULL);
53437c478bd9Sstevel@tonic-gate 
53447c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
53457c478bd9Sstevel@tonic-gate 		switch (cmd) {
53467c478bd9Sstevel@tonic-gate 		case DDI_ATTACH:
53477c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_NOTE, dip,
53485e3986cbScth 			    "!Client post_attach: called %p\n", (void *)ct));
53497c478bd9Sstevel@tonic-gate 			if (error != DDI_SUCCESS) {
53507c478bd9Sstevel@tonic-gate 				MDI_DEBUG(1, (CE_NOTE, dip,
53517c478bd9Sstevel@tonic-gate 				    "!Client post_attach: failed error=%d\n",
53527c478bd9Sstevel@tonic-gate 				    error));
53537c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_DETACH(ct);
53547c478bd9Sstevel@tonic-gate 				MDI_DEBUG(4, (CE_WARN, dip,
53557c478bd9Sstevel@tonic-gate 				    "mdi_post_attach i_mdi_pm_reset_client\n"));
53567c478bd9Sstevel@tonic-gate 				i_mdi_pm_reset_client(ct);
53577c478bd9Sstevel@tonic-gate 				break;
53587c478bd9Sstevel@tonic-gate 			}
53597c478bd9Sstevel@tonic-gate 
53607c478bd9Sstevel@tonic-gate 			/*
536137fbbce5Scth 			 * Client device has successfully attached, inform
536237fbbce5Scth 			 * the vhci.
53637c478bd9Sstevel@tonic-gate 			 */
536437fbbce5Scth 			vh = ct->ct_vhci;
536537fbbce5Scth 			if (vh->vh_ops->vo_client_attached)
536637fbbce5Scth 				(*vh->vh_ops->vo_client_attached)(dip);
536737fbbce5Scth 
53687c478bd9Sstevel@tonic-gate 			MDI_CLIENT_SET_ATTACH(ct);
53697c478bd9Sstevel@tonic-gate 			break;
53707c478bd9Sstevel@tonic-gate 
53717c478bd9Sstevel@tonic-gate 		case DDI_RESUME:
53727c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_NOTE, dip,
53735e3986cbScth 			    "!Client post_attach: called %p\n", (void *)ct));
53747c478bd9Sstevel@tonic-gate 			if (error == DDI_SUCCESS) {
53757c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_RESUME(ct);
53767c478bd9Sstevel@tonic-gate 			} else {
53777c478bd9Sstevel@tonic-gate 				MDI_DEBUG(1, (CE_NOTE, dip,
53787c478bd9Sstevel@tonic-gate 				    "!Client post_resume: failed error=%d\n",
53797c478bd9Sstevel@tonic-gate 				    error));
53807c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_SUSPEND(ct);
53817c478bd9Sstevel@tonic-gate 			}
53827c478bd9Sstevel@tonic-gate 			break;
53837c478bd9Sstevel@tonic-gate 		}
53847c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
53857c478bd9Sstevel@tonic-gate 	}
53867c478bd9Sstevel@tonic-gate }
53877c478bd9Sstevel@tonic-gate 
53887c478bd9Sstevel@tonic-gate /*
53897c478bd9Sstevel@tonic-gate  * mdi_pre_detach():
53907c478bd9Sstevel@tonic-gate  *		Pre detach notification handler
53917c478bd9Sstevel@tonic-gate  */
53927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
53937c478bd9Sstevel@tonic-gate int
53947c478bd9Sstevel@tonic-gate mdi_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
53957c478bd9Sstevel@tonic-gate {
53967c478bd9Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
53977c478bd9Sstevel@tonic-gate 
53987c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
53997c478bd9Sstevel@tonic-gate 		(void) i_mdi_client_pre_detach(dip, cmd);
54007c478bd9Sstevel@tonic-gate 	}
54017c478bd9Sstevel@tonic-gate 
54027c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
54037c478bd9Sstevel@tonic-gate 		rv = i_mdi_phci_pre_detach(dip, cmd);
54047c478bd9Sstevel@tonic-gate 	}
54057c478bd9Sstevel@tonic-gate 
54067c478bd9Sstevel@tonic-gate 	return (rv);
54077c478bd9Sstevel@tonic-gate }
54087c478bd9Sstevel@tonic-gate 
54097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
54107c478bd9Sstevel@tonic-gate static int
54117c478bd9Sstevel@tonic-gate i_mdi_phci_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
54127c478bd9Sstevel@tonic-gate {
54137c478bd9Sstevel@tonic-gate 	int		rv = DDI_SUCCESS;
54147c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
54157c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
54167c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
54177c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*failed_pip = NULL;
54187c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
54197c478bd9Sstevel@tonic-gate 
54207c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(dip);
54217c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
54227c478bd9Sstevel@tonic-gate 		return (rv);
54237c478bd9Sstevel@tonic-gate 	}
54247c478bd9Sstevel@tonic-gate 
54257c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
54267c478bd9Sstevel@tonic-gate 	switch (cmd) {
54277c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
54287c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
54295e3986cbScth 		    "!pHCI pre_detach: called %p\n", (void *)ph));
54307c478bd9Sstevel@tonic-gate 		if (!MDI_PHCI_IS_OFFLINE(ph)) {
54317c478bd9Sstevel@tonic-gate 			/*
54327c478bd9Sstevel@tonic-gate 			 * mdi_pathinfo nodes are still attached to
54337c478bd9Sstevel@tonic-gate 			 * this pHCI. Fail the detach for this pHCI.
54347c478bd9Sstevel@tonic-gate 			 */
54357c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_WARN, dip,
54367c478bd9Sstevel@tonic-gate 			    "!pHCI pre_detach: "
54377c478bd9Sstevel@tonic-gate 			    "mdi_pathinfo nodes are still attached "
54385e3986cbScth 			    "%p\n", (void *)ph));
54397c478bd9Sstevel@tonic-gate 			rv = DDI_FAILURE;
54407c478bd9Sstevel@tonic-gate 			break;
54417c478bd9Sstevel@tonic-gate 		}
54427c478bd9Sstevel@tonic-gate 		MDI_PHCI_SET_DETACH(ph);
54437c478bd9Sstevel@tonic-gate 		break;
54447c478bd9Sstevel@tonic-gate 
54457c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
54467c478bd9Sstevel@tonic-gate 		/*
54477c478bd9Sstevel@tonic-gate 		 * pHCI is getting suspended.  Since mpxio client
54487c478bd9Sstevel@tonic-gate 		 * devices may not be suspended at this point, to avoid
54497c478bd9Sstevel@tonic-gate 		 * a potential stack overflow, it is important to suspend
54507c478bd9Sstevel@tonic-gate 		 * client devices before pHCI can be suspended.
54517c478bd9Sstevel@tonic-gate 		 */
54527c478bd9Sstevel@tonic-gate 
54537c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
54545e3986cbScth 		    "!pHCI pre_suspend: called %p\n", (void *)ph));
54557c478bd9Sstevel@tonic-gate 		/*
54567c478bd9Sstevel@tonic-gate 		 * Suspend all the client devices accessible through this pHCI
54577c478bd9Sstevel@tonic-gate 		 */
54587c478bd9Sstevel@tonic-gate 		pip = ph->ph_path_head;
54597c478bd9Sstevel@tonic-gate 		while (pip != NULL && rv == DDI_SUCCESS) {
54607c478bd9Sstevel@tonic-gate 			dev_info_t *cdip;
54617c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
54627c478bd9Sstevel@tonic-gate 			next =
54637c478bd9Sstevel@tonic-gate 			    (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
54647c478bd9Sstevel@tonic-gate 			ct = MDI_PI(pip)->pi_client;
54657c478bd9Sstevel@tonic-gate 			i_mdi_client_lock(ct, pip);
54667c478bd9Sstevel@tonic-gate 			cdip = ct->ct_dip;
54677c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
54687c478bd9Sstevel@tonic-gate 			if ((MDI_CLIENT_IS_DETACHED(ct) == 0) &&
54697c478bd9Sstevel@tonic-gate 			    MDI_CLIENT_IS_SUSPENDED(ct) == 0) {
54707c478bd9Sstevel@tonic-gate 				i_mdi_client_unlock(ct);
54717c478bd9Sstevel@tonic-gate 				if ((rv = devi_detach(cdip, DDI_SUSPEND)) !=
54727c478bd9Sstevel@tonic-gate 				    DDI_SUCCESS) {
54737c478bd9Sstevel@tonic-gate 					/*
54747c478bd9Sstevel@tonic-gate 					 * Suspend of one of the client
54757c478bd9Sstevel@tonic-gate 					 * device has failed.
54767c478bd9Sstevel@tonic-gate 					 */
54777c478bd9Sstevel@tonic-gate 					MDI_DEBUG(1, (CE_WARN, dip,
54787c478bd9Sstevel@tonic-gate 					    "!Suspend of device (%s%d) failed.",
54797c478bd9Sstevel@tonic-gate 					    ddi_driver_name(cdip),
54807c478bd9Sstevel@tonic-gate 					    ddi_get_instance(cdip)));
54817c478bd9Sstevel@tonic-gate 					failed_pip = pip;
54827c478bd9Sstevel@tonic-gate 					break;
54837c478bd9Sstevel@tonic-gate 				}
54847c478bd9Sstevel@tonic-gate 			} else {
54857c478bd9Sstevel@tonic-gate 				i_mdi_client_unlock(ct);
54867c478bd9Sstevel@tonic-gate 			}
54877c478bd9Sstevel@tonic-gate 			pip = next;
54887c478bd9Sstevel@tonic-gate 		}
54897c478bd9Sstevel@tonic-gate 
54907c478bd9Sstevel@tonic-gate 		if (rv == DDI_SUCCESS) {
54917c478bd9Sstevel@tonic-gate 			/*
54927c478bd9Sstevel@tonic-gate 			 * Suspend of client devices is complete. Proceed
54937c478bd9Sstevel@tonic-gate 			 * with pHCI suspend.
54947c478bd9Sstevel@tonic-gate 			 */
54957c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_SUSPEND(ph);
54967c478bd9Sstevel@tonic-gate 		} else {
54977c478bd9Sstevel@tonic-gate 			/*
54987c478bd9Sstevel@tonic-gate 			 * Revert back all the suspended client device states
54997c478bd9Sstevel@tonic-gate 			 * to converse.
55007c478bd9Sstevel@tonic-gate 			 */
55017c478bd9Sstevel@tonic-gate 			pip = ph->ph_path_head;
55027c478bd9Sstevel@tonic-gate 			while (pip != failed_pip) {
55037c478bd9Sstevel@tonic-gate 				dev_info_t *cdip;
55047c478bd9Sstevel@tonic-gate 				MDI_PI_LOCK(pip);
55057c478bd9Sstevel@tonic-gate 				next =
55067c478bd9Sstevel@tonic-gate 				    (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
55077c478bd9Sstevel@tonic-gate 				ct = MDI_PI(pip)->pi_client;
55087c478bd9Sstevel@tonic-gate 				i_mdi_client_lock(ct, pip);
55097c478bd9Sstevel@tonic-gate 				cdip = ct->ct_dip;
55107c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
55117c478bd9Sstevel@tonic-gate 				if (MDI_CLIENT_IS_SUSPENDED(ct)) {
55127c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
55137c478bd9Sstevel@tonic-gate 					(void) devi_attach(cdip, DDI_RESUME);
55147c478bd9Sstevel@tonic-gate 				} else {
55157c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
55167c478bd9Sstevel@tonic-gate 				}
55177c478bd9Sstevel@tonic-gate 				pip = next;
55187c478bd9Sstevel@tonic-gate 			}
55197c478bd9Sstevel@tonic-gate 		}
55207c478bd9Sstevel@tonic-gate 		break;
55217c478bd9Sstevel@tonic-gate 
55227c478bd9Sstevel@tonic-gate 	default:
55237c478bd9Sstevel@tonic-gate 		rv = DDI_FAILURE;
55247c478bd9Sstevel@tonic-gate 		break;
55257c478bd9Sstevel@tonic-gate 	}
55267c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
55277c478bd9Sstevel@tonic-gate 	return (rv);
55287c478bd9Sstevel@tonic-gate }
55297c478bd9Sstevel@tonic-gate 
55307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
55317c478bd9Sstevel@tonic-gate static int
55327c478bd9Sstevel@tonic-gate i_mdi_client_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
55337c478bd9Sstevel@tonic-gate {
55347c478bd9Sstevel@tonic-gate 	int		rv = DDI_SUCCESS;
55357c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
55367c478bd9Sstevel@tonic-gate 
55377c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(dip);
55387c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
55397c478bd9Sstevel@tonic-gate 		return (rv);
55407c478bd9Sstevel@tonic-gate 	}
55417c478bd9Sstevel@tonic-gate 
55427c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
55437c478bd9Sstevel@tonic-gate 	switch (cmd) {
55447c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
55457c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
55465e3986cbScth 		    "!Client pre_detach: called %p\n", (void *)ct));
55477c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_DETACH(ct);
55487c478bd9Sstevel@tonic-gate 		break;
55497c478bd9Sstevel@tonic-gate 
55507c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
55517c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
55525e3986cbScth 		    "!Client pre_suspend: called %p\n", (void *)ct));
55537c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_SUSPEND(ct);
55547c478bd9Sstevel@tonic-gate 		break;
55557c478bd9Sstevel@tonic-gate 
55567c478bd9Sstevel@tonic-gate 	default:
55577c478bd9Sstevel@tonic-gate 		rv = DDI_FAILURE;
55587c478bd9Sstevel@tonic-gate 		break;
55597c478bd9Sstevel@tonic-gate 	}
55607c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
55617c478bd9Sstevel@tonic-gate 	return (rv);
55627c478bd9Sstevel@tonic-gate }
55637c478bd9Sstevel@tonic-gate 
55647c478bd9Sstevel@tonic-gate /*
55657c478bd9Sstevel@tonic-gate  * mdi_post_detach():
55667c478bd9Sstevel@tonic-gate  *		Post detach notification handler
55677c478bd9Sstevel@tonic-gate  */
55687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
55697c478bd9Sstevel@tonic-gate void
55707c478bd9Sstevel@tonic-gate mdi_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
55717c478bd9Sstevel@tonic-gate {
55727c478bd9Sstevel@tonic-gate 	/*
55737c478bd9Sstevel@tonic-gate 	 * Detach/Suspend of mpxio component failed. Update our state
55747c478bd9Sstevel@tonic-gate 	 * too
55757c478bd9Sstevel@tonic-gate 	 */
55767c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip))
55777c478bd9Sstevel@tonic-gate 		i_mdi_phci_post_detach(dip, cmd, error);
55787c478bd9Sstevel@tonic-gate 
55797c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip))
55807c478bd9Sstevel@tonic-gate 		i_mdi_client_post_detach(dip, cmd, error);
55817c478bd9Sstevel@tonic-gate }
55827c478bd9Sstevel@tonic-gate 
55837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
55847c478bd9Sstevel@tonic-gate static void
55857c478bd9Sstevel@tonic-gate i_mdi_phci_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
55867c478bd9Sstevel@tonic-gate {
55877c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
55887c478bd9Sstevel@tonic-gate 
55897c478bd9Sstevel@tonic-gate 	/*
55907c478bd9Sstevel@tonic-gate 	 * Detach/Suspend of phci component failed. Update our state
55917c478bd9Sstevel@tonic-gate 	 * too
55927c478bd9Sstevel@tonic-gate 	 */
55937c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(dip);
55947c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
55957c478bd9Sstevel@tonic-gate 		return;
55967c478bd9Sstevel@tonic-gate 	}
55977c478bd9Sstevel@tonic-gate 
55987c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
55997c478bd9Sstevel@tonic-gate 	/*
56007c478bd9Sstevel@tonic-gate 	 * Detach of pHCI failed. Restore back converse
56017c478bd9Sstevel@tonic-gate 	 * state
56027c478bd9Sstevel@tonic-gate 	 */
56037c478bd9Sstevel@tonic-gate 	switch (cmd) {
56047c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
56057c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
56065e3986cbScth 		    "!pHCI post_detach: called %p\n", (void *)ph));
56077c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
56087c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_ATTACH(ph);
56097c478bd9Sstevel@tonic-gate 		break;
56107c478bd9Sstevel@tonic-gate 
56117c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
56127c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
56135e3986cbScth 		    "!pHCI post_suspend: called %p\n", (void *)ph));
56147c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
56157c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_RESUME(ph);
56167c478bd9Sstevel@tonic-gate 		break;
56177c478bd9Sstevel@tonic-gate 	}
56187c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
56197c478bd9Sstevel@tonic-gate }
56207c478bd9Sstevel@tonic-gate 
56217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
56227c478bd9Sstevel@tonic-gate static void
56237c478bd9Sstevel@tonic-gate i_mdi_client_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
56247c478bd9Sstevel@tonic-gate {
56257c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
56267c478bd9Sstevel@tonic-gate 
56277c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(dip);
56287c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
56297c478bd9Sstevel@tonic-gate 		return;
56307c478bd9Sstevel@tonic-gate 	}
56317c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
56327c478bd9Sstevel@tonic-gate 	/*
56337c478bd9Sstevel@tonic-gate 	 * Detach of Client failed. Restore back converse
56347c478bd9Sstevel@tonic-gate 	 * state
56357c478bd9Sstevel@tonic-gate 	 */
56367c478bd9Sstevel@tonic-gate 	switch (cmd) {
56377c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
56387c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
56395e3986cbScth 		    "!Client post_detach: called %p\n", (void *)ct));
56407c478bd9Sstevel@tonic-gate 		if (DEVI_IS_ATTACHING(ct->ct_dip)) {
56417c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, dip, "i_mdi_client_post_detach "
56427c478bd9Sstevel@tonic-gate 			    "i_mdi_pm_rele_client\n"));
56437c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, ct->ct_path_count);
56447c478bd9Sstevel@tonic-gate 		} else {
56457c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, dip, "i_mdi_client_post_detach "
56467c478bd9Sstevel@tonic-gate 			    "i_mdi_pm_reset_client\n"));
56477c478bd9Sstevel@tonic-gate 			i_mdi_pm_reset_client(ct);
56487c478bd9Sstevel@tonic-gate 		}
56497c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
56507c478bd9Sstevel@tonic-gate 			MDI_CLIENT_SET_ATTACH(ct);
56517c478bd9Sstevel@tonic-gate 		break;
56527c478bd9Sstevel@tonic-gate 
56537c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
56547c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
56555e3986cbScth 		    "!Client post_suspend: called %p\n", (void *)ct));
56567c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
56577c478bd9Sstevel@tonic-gate 			MDI_CLIENT_SET_RESUME(ct);
56587c478bd9Sstevel@tonic-gate 		break;
56597c478bd9Sstevel@tonic-gate 	}
56607c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
56617c478bd9Sstevel@tonic-gate }
56627c478bd9Sstevel@tonic-gate 
566337fbbce5Scth int
566437fbbce5Scth mdi_pi_kstat_exists(mdi_pathinfo_t *pip)
566537fbbce5Scth {
566637fbbce5Scth 	return (MDI_PI(pip)->pi_kstats ? 1 : 0);
566737fbbce5Scth }
566837fbbce5Scth 
56697c478bd9Sstevel@tonic-gate /*
56707c478bd9Sstevel@tonic-gate  * create and install per-path (client - pHCI) statistics
56717c478bd9Sstevel@tonic-gate  * I/O stats supported: nread, nwritten, reads, and writes
56727c478bd9Sstevel@tonic-gate  * Error stats - hard errors, soft errors, & transport errors
56737c478bd9Sstevel@tonic-gate  */
567437fbbce5Scth int
567537fbbce5Scth mdi_pi_kstat_create(mdi_pathinfo_t *pip, char *ksname)
56767c478bd9Sstevel@tonic-gate {
56777c478bd9Sstevel@tonic-gate 	kstat_t			*kiosp, *kerrsp;
56787c478bd9Sstevel@tonic-gate 	struct pi_errs		*nsp;
56797c478bd9Sstevel@tonic-gate 	struct mdi_pi_kstats	*mdi_statp;
56807c478bd9Sstevel@tonic-gate 
56817c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_kstats != NULL)
56827c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
56837c478bd9Sstevel@tonic-gate 
56847c478bd9Sstevel@tonic-gate 	if ((kiosp = kstat_create("mdi", 0, ksname, "iopath",
568537fbbce5Scth 	    KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT)) == NULL) {
56867c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
56877c478bd9Sstevel@tonic-gate 	}
56887c478bd9Sstevel@tonic-gate 
568937fbbce5Scth 	(void) strcat(ksname, ",err");
56907c478bd9Sstevel@tonic-gate 	kerrsp = kstat_create("mdi", 0, ksname, "iopath_errors",
56917c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED,
56927c478bd9Sstevel@tonic-gate 	    sizeof (struct pi_errs) / sizeof (kstat_named_t), 0);
56937c478bd9Sstevel@tonic-gate 	if (kerrsp == NULL) {
56947c478bd9Sstevel@tonic-gate 		kstat_delete(kiosp);
56957c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
56967c478bd9Sstevel@tonic-gate 	}
56977c478bd9Sstevel@tonic-gate 
56987c478bd9Sstevel@tonic-gate 	nsp = (struct pi_errs *)kerrsp->ks_data;
56997c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_softerrs, "Soft Errors", KSTAT_DATA_UINT32);
57007c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_harderrs, "Hard Errors", KSTAT_DATA_UINT32);
57017c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_transerrs, "Transport Errors",
57027c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
57037c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_icnt_busy, "Interconnect Busy",
57047c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
57057c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_icnt_errors, "Interconnect Errors",
57067c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
57077c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_phci_rsrc, "pHCI No Resources",
57087c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
57097c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_phci_localerr, "pHCI Local Errors",
57107c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
57117c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_phci_invstate, "pHCI Invalid State",
57127c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
57137c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_failedfrom, "Failed From",
57147c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
57157c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_failedto, "Failed To", KSTAT_DATA_UINT32);
57167c478bd9Sstevel@tonic-gate 
57177c478bd9Sstevel@tonic-gate 	mdi_statp = kmem_alloc(sizeof (*mdi_statp), KM_SLEEP);
57187c478bd9Sstevel@tonic-gate 	mdi_statp->pi_kstat_ref = 1;
57197c478bd9Sstevel@tonic-gate 	mdi_statp->pi_kstat_iostats = kiosp;
57207c478bd9Sstevel@tonic-gate 	mdi_statp->pi_kstat_errstats = kerrsp;
57217c478bd9Sstevel@tonic-gate 	kstat_install(kiosp);
57227c478bd9Sstevel@tonic-gate 	kstat_install(kerrsp);
57237c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_kstats = mdi_statp;
57247c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
57257c478bd9Sstevel@tonic-gate }
57267c478bd9Sstevel@tonic-gate 
57277c478bd9Sstevel@tonic-gate /*
57287c478bd9Sstevel@tonic-gate  * destroy per-path properties
57297c478bd9Sstevel@tonic-gate  */
57307c478bd9Sstevel@tonic-gate static void
57317c478bd9Sstevel@tonic-gate i_mdi_pi_kstat_destroy(mdi_pathinfo_t *pip)
57327c478bd9Sstevel@tonic-gate {
57337c478bd9Sstevel@tonic-gate 
57347c478bd9Sstevel@tonic-gate 	struct mdi_pi_kstats *mdi_statp;
57357c478bd9Sstevel@tonic-gate 
573637fbbce5Scth 	if (MDI_PI(pip)->pi_kstats == NULL)
573737fbbce5Scth 		return;
57387c478bd9Sstevel@tonic-gate 	if ((mdi_statp = MDI_PI(pip)->pi_kstats) == NULL)
57397c478bd9Sstevel@tonic-gate 		return;
57407c478bd9Sstevel@tonic-gate 
57417c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_kstats = NULL;
57427c478bd9Sstevel@tonic-gate 
57437c478bd9Sstevel@tonic-gate 	/*
57447c478bd9Sstevel@tonic-gate 	 * the kstat may be shared between multiple pathinfo nodes
57457c478bd9Sstevel@tonic-gate 	 * decrement this pathinfo's usage, removing the kstats
57467c478bd9Sstevel@tonic-gate 	 * themselves when the last pathinfo reference is removed.
57477c478bd9Sstevel@tonic-gate 	 */
57487c478bd9Sstevel@tonic-gate 	ASSERT(mdi_statp->pi_kstat_ref > 0);
57497c478bd9Sstevel@tonic-gate 	if (--mdi_statp->pi_kstat_ref != 0)
57507c478bd9Sstevel@tonic-gate 		return;
57517c478bd9Sstevel@tonic-gate 
57527c478bd9Sstevel@tonic-gate 	kstat_delete(mdi_statp->pi_kstat_iostats);
57537c478bd9Sstevel@tonic-gate 	kstat_delete(mdi_statp->pi_kstat_errstats);
57547c478bd9Sstevel@tonic-gate 	kmem_free(mdi_statp, sizeof (*mdi_statp));
57557c478bd9Sstevel@tonic-gate }
57567c478bd9Sstevel@tonic-gate 
57577c478bd9Sstevel@tonic-gate /*
57587c478bd9Sstevel@tonic-gate  * update I/O paths KSTATS
57597c478bd9Sstevel@tonic-gate  */
57607c478bd9Sstevel@tonic-gate void
57617c478bd9Sstevel@tonic-gate mdi_pi_kstat_iosupdate(mdi_pathinfo_t *pip, struct buf *bp)
57627c478bd9Sstevel@tonic-gate {
57637c478bd9Sstevel@tonic-gate 	kstat_t *iostatp;
57647c478bd9Sstevel@tonic-gate 	size_t xfer_cnt;
57657c478bd9Sstevel@tonic-gate 
57667c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
57677c478bd9Sstevel@tonic-gate 
57687c478bd9Sstevel@tonic-gate 	/*
57697c478bd9Sstevel@tonic-gate 	 * I/O can be driven across a path prior to having path
57707c478bd9Sstevel@tonic-gate 	 * statistics available, i.e. probe(9e).
57717c478bd9Sstevel@tonic-gate 	 */
57727c478bd9Sstevel@tonic-gate 	if (bp != NULL && MDI_PI(pip)->pi_kstats != NULL) {
57737c478bd9Sstevel@tonic-gate 		iostatp = MDI_PI(pip)->pi_kstats->pi_kstat_iostats;
57747c478bd9Sstevel@tonic-gate 		xfer_cnt = bp->b_bcount - bp->b_resid;
57757c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
57767c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->reads++;
57777c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->nread += xfer_cnt;
57787c478bd9Sstevel@tonic-gate 		} else {
57797c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->writes++;
57807c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->nwritten += xfer_cnt;
57817c478bd9Sstevel@tonic-gate 		}
57827c478bd9Sstevel@tonic-gate 	}
57837c478bd9Sstevel@tonic-gate }
57847c478bd9Sstevel@tonic-gate 
57857c478bd9Sstevel@tonic-gate /*
5786ee28b439Scm136836  * Enable the path(specific client/target/initiator)
5787ee28b439Scm136836  * Enabling a path means that MPxIO may select the enabled path for routing
5788ee28b439Scm136836  * future I/O requests, subject to other path state constraints.
5789ee28b439Scm136836  */
5790ee28b439Scm136836 int
5791ee28b439Scm136836 mdi_pi_enable_path(mdi_pathinfo_t *pip, int flags)
5792ee28b439Scm136836 {
5793ee28b439Scm136836 	mdi_phci_t	*ph;
5794ee28b439Scm136836 
5795ee28b439Scm136836 	ph = i_devi_get_phci(mdi_pi_get_phci(pip));
5796ee28b439Scm136836 	if (ph == NULL) {
5797ee28b439Scm136836 		MDI_DEBUG(1, (CE_NOTE, NULL, "!mdi_pi_enable_path:"
57985e3986cbScth 			" failed. pip: %p ph = NULL\n", (void *)pip));
5799ee28b439Scm136836 		return (MDI_FAILURE);
5800ee28b439Scm136836 	}
5801ee28b439Scm136836 
5802ee28b439Scm136836 	(void) i_mdi_enable_disable_path(pip, ph->ph_vhci, flags,
5803ee28b439Scm136836 		MDI_ENABLE_OP);
5804ee28b439Scm136836 	MDI_DEBUG(5, (CE_NOTE, NULL, "!mdi_pi_enable_path:"
58055e3986cbScth 		" Returning success pip = %p. ph = %p\n",
58065e3986cbScth 		(void *)pip, (void *)ph));
5807ee28b439Scm136836 	return (MDI_SUCCESS);
5808ee28b439Scm136836 
5809ee28b439Scm136836 }
5810ee28b439Scm136836 
5811ee28b439Scm136836 /*
5812ee28b439Scm136836  * Disable the path (specific client/target/initiator)
5813ee28b439Scm136836  * Disabling a path means that MPxIO will not select the disabled path for
5814ee28b439Scm136836  * routing any new I/O requests.
5815ee28b439Scm136836  */
5816ee28b439Scm136836 int
5817ee28b439Scm136836 mdi_pi_disable_path(mdi_pathinfo_t *pip, int flags)
5818ee28b439Scm136836 {
5819ee28b439Scm136836 	mdi_phci_t	*ph;
5820ee28b439Scm136836 
5821ee28b439Scm136836 	ph = i_devi_get_phci(mdi_pi_get_phci(pip));
5822ee28b439Scm136836 	if (ph == NULL) {
5823ee28b439Scm136836 		MDI_DEBUG(1, (CE_NOTE, NULL, "!mdi_pi_disable_path:"
58245e3986cbScth 			" failed. pip: %p ph = NULL\n", (void *)pip));
5825ee28b439Scm136836 		return (MDI_FAILURE);
5826ee28b439Scm136836 	}
5827ee28b439Scm136836 
5828ee28b439Scm136836 	(void) i_mdi_enable_disable_path(pip,
5829ee28b439Scm136836 			ph->ph_vhci, flags, MDI_DISABLE_OP);
5830ee28b439Scm136836 	MDI_DEBUG(5, (CE_NOTE, NULL, "!mdi_pi_disable_path:"
58315e3986cbScth 		"Returning success pip = %p. ph = %p",
58325e3986cbScth 		(void *)pip, (void *)ph));
5833ee28b439Scm136836 	return (MDI_SUCCESS);
5834ee28b439Scm136836 }
5835ee28b439Scm136836 
5836ee28b439Scm136836 /*
58377c478bd9Sstevel@tonic-gate  * disable the path to a particular pHCI (pHCI specified in the phci_path
58387c478bd9Sstevel@tonic-gate  * argument) for a particular client (specified in the client_path argument).
58397c478bd9Sstevel@tonic-gate  * Disabling a path means that MPxIO will not select the disabled path for
58407c478bd9Sstevel@tonic-gate  * routing any new I/O requests.
5841ee28b439Scm136836  * NOTE: this will be removed once the NWS files are changed to use the new
5842ee28b439Scm136836  * mdi_{enable,disable}_path interfaces
58437c478bd9Sstevel@tonic-gate  */
58447c478bd9Sstevel@tonic-gate int
58457c478bd9Sstevel@tonic-gate mdi_pi_disable(dev_info_t *cdip, dev_info_t *pdip, int flags)
58467c478bd9Sstevel@tonic-gate {
58477c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_enable_disable(cdip, pdip, flags, MDI_DISABLE_OP));
58487c478bd9Sstevel@tonic-gate }
58497c478bd9Sstevel@tonic-gate 
58507c478bd9Sstevel@tonic-gate /*
58517c478bd9Sstevel@tonic-gate  * Enable the path to a particular pHCI (pHCI specified in the phci_path
58527c478bd9Sstevel@tonic-gate  * argument) for a particular client (specified in the client_path argument).
58537c478bd9Sstevel@tonic-gate  * Enabling a path means that MPxIO may select the enabled path for routing
58547c478bd9Sstevel@tonic-gate  * future I/O requests, subject to other path state constraints.
5855ee28b439Scm136836  * NOTE: this will be removed once the NWS files are changed to use the new
5856ee28b439Scm136836  * mdi_{enable,disable}_path interfaces
58577c478bd9Sstevel@tonic-gate  */
58587c478bd9Sstevel@tonic-gate 
58597c478bd9Sstevel@tonic-gate int
58607c478bd9Sstevel@tonic-gate mdi_pi_enable(dev_info_t *cdip, dev_info_t *pdip, int flags)
58617c478bd9Sstevel@tonic-gate {
58627c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_enable_disable(cdip, pdip, flags, MDI_ENABLE_OP));
58637c478bd9Sstevel@tonic-gate }
58647c478bd9Sstevel@tonic-gate 
5865ee28b439Scm136836 /*
5866ee28b439Scm136836  * Common routine for doing enable/disable.
5867ee28b439Scm136836  */
5868ee28b439Scm136836 static mdi_pathinfo_t *
5869ee28b439Scm136836 i_mdi_enable_disable_path(mdi_pathinfo_t *pip, mdi_vhci_t *vh, int flags,
5870ee28b439Scm136836 		int op)
5871ee28b439Scm136836 {
5872ee28b439Scm136836 	int		sync_flag = 0;
5873ee28b439Scm136836 	int		rv;
5874ee28b439Scm136836 	mdi_pathinfo_t 	*next;
5875ee28b439Scm136836 	int		(*f)() = NULL;
5876ee28b439Scm136836 
5877ee28b439Scm136836 	f = vh->vh_ops->vo_pi_state_change;
5878ee28b439Scm136836 
5879ee28b439Scm136836 	sync_flag = (flags << 8) & 0xf00;
5880ee28b439Scm136836 
5881ee28b439Scm136836 	/*
5882ee28b439Scm136836 	 * Do a callback into the mdi consumer to let it
5883ee28b439Scm136836 	 * know that path is about to get enabled/disabled.
5884ee28b439Scm136836 	 */
5885ee28b439Scm136836 	if (f != NULL) {
5886ee28b439Scm136836 		rv = (*f)(vh->vh_dip, pip, 0,
5887ee28b439Scm136836 			MDI_PI_EXT_STATE(pip),
5888ee28b439Scm136836 			MDI_EXT_STATE_CHANGE | sync_flag |
5889ee28b439Scm136836 			op | MDI_BEFORE_STATE_CHANGE);
5890ee28b439Scm136836 		if (rv != MDI_SUCCESS) {
5891ee28b439Scm136836 			MDI_DEBUG(2, (CE_WARN, vh->vh_dip,
5892ee28b439Scm136836 			"!vo_pi_state_change: failed rv = %x", rv));
5893ee28b439Scm136836 		}
5894ee28b439Scm136836 	}
5895ee28b439Scm136836 	MDI_PI_LOCK(pip);
5896ee28b439Scm136836 	next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
5897ee28b439Scm136836 
5898ee28b439Scm136836 	switch (flags) {
5899ee28b439Scm136836 		case USER_DISABLE:
59005e3986cbScth 			if (op == MDI_DISABLE_OP) {
5901ee28b439Scm136836 				MDI_PI_SET_USER_DISABLE(pip);
59025e3986cbScth 			} else {
5903ee28b439Scm136836 				MDI_PI_SET_USER_ENABLE(pip);
59045e3986cbScth 			}
5905ee28b439Scm136836 			break;
5906ee28b439Scm136836 		case DRIVER_DISABLE:
59075e3986cbScth 			if (op == MDI_DISABLE_OP) {
5908ee28b439Scm136836 				MDI_PI_SET_DRV_DISABLE(pip);
59095e3986cbScth 			} else {
5910ee28b439Scm136836 				MDI_PI_SET_DRV_ENABLE(pip);
59115e3986cbScth 			}
5912ee28b439Scm136836 			break;
5913ee28b439Scm136836 		case DRIVER_DISABLE_TRANSIENT:
59145e3986cbScth 			if (op == MDI_DISABLE_OP && rv == MDI_SUCCESS) {
5915ee28b439Scm136836 				MDI_PI_SET_DRV_DISABLE_TRANS(pip);
59165e3986cbScth 			} else {
5917ee28b439Scm136836 				MDI_PI_SET_DRV_ENABLE_TRANS(pip);
59185e3986cbScth 			}
5919ee28b439Scm136836 			break;
5920ee28b439Scm136836 	}
5921ee28b439Scm136836 	MDI_PI_UNLOCK(pip);
5922ee28b439Scm136836 	/*
5923ee28b439Scm136836 	 * Do a callback into the mdi consumer to let it
5924ee28b439Scm136836 	 * know that path is now enabled/disabled.
5925ee28b439Scm136836 	 */
5926ee28b439Scm136836 	if (f != NULL) {
5927ee28b439Scm136836 		rv = (*f)(vh->vh_dip, pip, 0,
5928ee28b439Scm136836 			MDI_PI_EXT_STATE(pip),
5929ee28b439Scm136836 			MDI_EXT_STATE_CHANGE | sync_flag |
5930ee28b439Scm136836 			op | MDI_AFTER_STATE_CHANGE);
5931ee28b439Scm136836 		if (rv != MDI_SUCCESS) {
5932ee28b439Scm136836 			MDI_DEBUG(2, (CE_WARN, vh->vh_dip,
5933ee28b439Scm136836 			"!vo_pi_state_change: failed rv = %x", rv));
5934ee28b439Scm136836 		}
5935ee28b439Scm136836 	}
5936ee28b439Scm136836 	return (next);
5937ee28b439Scm136836 }
59387c478bd9Sstevel@tonic-gate 
59397c478bd9Sstevel@tonic-gate /*
59407c478bd9Sstevel@tonic-gate  * Common routine for doing enable/disable.
5941ee28b439Scm136836  * NOTE: this will be removed once the NWS files are changed to use the new
5942ee28b439Scm136836  * mdi_{enable,disable}_path has been putback
59437c478bd9Sstevel@tonic-gate  */
59447c478bd9Sstevel@tonic-gate int
59457c478bd9Sstevel@tonic-gate i_mdi_pi_enable_disable(dev_info_t *cdip, dev_info_t *pdip, int flags, int op)
59467c478bd9Sstevel@tonic-gate {
59477c478bd9Sstevel@tonic-gate 
59487c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
59497c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
59507c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
59517c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next, *pip;
59527c478bd9Sstevel@tonic-gate 	int		found_it;
59537c478bd9Sstevel@tonic-gate 
59547c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
59557c478bd9Sstevel@tonic-gate 	MDI_DEBUG(5, (CE_NOTE, NULL, "!i_mdi_pi_enable_disable: "
59565e3986cbScth 		"Op = %d pdip = %p cdip = %p\n", op, (void *)pdip,
59575e3986cbScth 		(void *)cdip));
59587c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
59597c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_NOTE, NULL, "!i_mdi_pi_enable_disable:"
59605e3986cbScth 			"Op %d failed. ph = NULL\n", op));
59617c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
59627c478bd9Sstevel@tonic-gate 	}
59637c478bd9Sstevel@tonic-gate 
59647c478bd9Sstevel@tonic-gate 	if ((op != MDI_ENABLE_OP) && (op != MDI_DISABLE_OP)) {
59657c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_NOTE, NULL, "!i_mdi_pi_enable_disable: "
59665e3986cbScth 			"Op Invalid operation = %d\n", op));
59677c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
59687c478bd9Sstevel@tonic-gate 	}
59697c478bd9Sstevel@tonic-gate 
59707c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
59717c478bd9Sstevel@tonic-gate 
59727c478bd9Sstevel@tonic-gate 	if (cdip == NULL) {
59737c478bd9Sstevel@tonic-gate 		/*
59747c478bd9Sstevel@tonic-gate 		 * Need to mark the Phci as enabled/disabled.
59757c478bd9Sstevel@tonic-gate 		 */
59767c478bd9Sstevel@tonic-gate 		MDI_DEBUG(3, (CE_NOTE, NULL, "!i_mdi_pi_enable_disable: "
59775e3986cbScth 		"Op %d for the phci\n", op));
59787c478bd9Sstevel@tonic-gate 		MDI_PHCI_LOCK(ph);
59797c478bd9Sstevel@tonic-gate 		switch (flags) {
59807c478bd9Sstevel@tonic-gate 			case USER_DISABLE:
59815e3986cbScth 				if (op == MDI_DISABLE_OP) {
59827c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_USER_DISABLE(ph);
59835e3986cbScth 				} else {
59847c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_USER_ENABLE(ph);
59855e3986cbScth 				}
59867c478bd9Sstevel@tonic-gate 				break;
59877c478bd9Sstevel@tonic-gate 			case DRIVER_DISABLE:
59885e3986cbScth 				if (op == MDI_DISABLE_OP) {
59897c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_DISABLE(ph);
59905e3986cbScth 				} else {
59917c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_ENABLE(ph);
59925e3986cbScth 				}
59937c478bd9Sstevel@tonic-gate 				break;
59947c478bd9Sstevel@tonic-gate 			case DRIVER_DISABLE_TRANSIENT:
59955e3986cbScth 				if (op == MDI_DISABLE_OP) {
59967c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_DISABLE_TRANSIENT(ph);
59975e3986cbScth 				} else {
59987c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_ENABLE_TRANSIENT(ph);
59995e3986cbScth 				}
60007c478bd9Sstevel@tonic-gate 				break;
60017c478bd9Sstevel@tonic-gate 			default:
60027c478bd9Sstevel@tonic-gate 				MDI_PHCI_UNLOCK(ph);
60037c478bd9Sstevel@tonic-gate 				MDI_DEBUG(1, (CE_NOTE, NULL,
60047c478bd9Sstevel@tonic-gate 				"!i_mdi_pi_enable_disable:"
60057c478bd9Sstevel@tonic-gate 				" Invalid flag argument= %d\n", flags));
60067c478bd9Sstevel@tonic-gate 		}
60077c478bd9Sstevel@tonic-gate 
60087c478bd9Sstevel@tonic-gate 		/*
60097c478bd9Sstevel@tonic-gate 		 * Phci has been disabled. Now try to enable/disable
60107c478bd9Sstevel@tonic-gate 		 * path info's to each client.
60117c478bd9Sstevel@tonic-gate 		 */
60127c478bd9Sstevel@tonic-gate 		pip = ph->ph_path_head;
60137c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
6014ee28b439Scm136836 			pip = i_mdi_enable_disable_path(pip, vh, flags, op);
60157c478bd9Sstevel@tonic-gate 		}
60167c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
60177c478bd9Sstevel@tonic-gate 	} else {
60187c478bd9Sstevel@tonic-gate 
60197c478bd9Sstevel@tonic-gate 		/*
60207c478bd9Sstevel@tonic-gate 		 * Disable a specific client.
60217c478bd9Sstevel@tonic-gate 		 */
60227c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(cdip);
60237c478bd9Sstevel@tonic-gate 		if (ct == NULL) {
60247c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_NOTE, NULL,
60257c478bd9Sstevel@tonic-gate 			"!i_mdi_pi_enable_disable:"
60267c478bd9Sstevel@tonic-gate 			" failed. ct = NULL operation = %d\n", op));
60277c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
60287c478bd9Sstevel@tonic-gate 		}
60297c478bd9Sstevel@tonic-gate 
60307c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
60317c478bd9Sstevel@tonic-gate 		pip = ct->ct_path_head;
60327c478bd9Sstevel@tonic-gate 		found_it = 0;
60337c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
60347c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
60357c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
60367c478bd9Sstevel@tonic-gate 			if (MDI_PI(pip)->pi_phci == ph) {
60377c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
60387c478bd9Sstevel@tonic-gate 				found_it = 1;
60397c478bd9Sstevel@tonic-gate 				break;
60407c478bd9Sstevel@tonic-gate 			}
60417c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
60427c478bd9Sstevel@tonic-gate 			pip = next;
60437c478bd9Sstevel@tonic-gate 		}
60447c478bd9Sstevel@tonic-gate 
6045ee28b439Scm136836 
60467c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
60477c478bd9Sstevel@tonic-gate 		if (found_it == 0) {
60487c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_NOTE, NULL,
60497c478bd9Sstevel@tonic-gate 			"!i_mdi_pi_enable_disable:"
60507c478bd9Sstevel@tonic-gate 			" failed. Could not find corresponding pip\n"));
60517c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
60527c478bd9Sstevel@tonic-gate 		}
6053ee28b439Scm136836 
6054ee28b439Scm136836 		(void) i_mdi_enable_disable_path(pip, vh, flags, op);
60557c478bd9Sstevel@tonic-gate 	}
60567c478bd9Sstevel@tonic-gate 
60577c478bd9Sstevel@tonic-gate 	MDI_DEBUG(5, (CE_NOTE, NULL, "!i_mdi_pi_enable_disable: "
60585e3986cbScth 		"Op %d Returning success pdip = %p cdip = %p\n",
60595e3986cbScth 		op, (void *)pdip, (void *)cdip));
60607c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
60617c478bd9Sstevel@tonic-gate }
60627c478bd9Sstevel@tonic-gate 
60637c478bd9Sstevel@tonic-gate /*
60647c478bd9Sstevel@tonic-gate  * Ensure phci powered up
60657c478bd9Sstevel@tonic-gate  */
60667c478bd9Sstevel@tonic-gate static void
60677c478bd9Sstevel@tonic-gate i_mdi_pm_hold_pip(mdi_pathinfo_t *pip)
60687c478bd9Sstevel@tonic-gate {
60697c478bd9Sstevel@tonic-gate 	dev_info_t	*ph_dip;
60707c478bd9Sstevel@tonic-gate 
60717c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
60725e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
60737c478bd9Sstevel@tonic-gate 
60747c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held) {
60757c478bd9Sstevel@tonic-gate 		return;
60767c478bd9Sstevel@tonic-gate 	}
60777c478bd9Sstevel@tonic-gate 
60787c478bd9Sstevel@tonic-gate 	ph_dip = mdi_pi_get_phci(pip);
60795e3986cbScth 	MDI_DEBUG(4, (CE_NOTE, ph_dip, "i_mdi_pm_hold_pip for %s%d %p\n",
60805e3986cbScth 	    ddi_get_name(ph_dip), ddi_get_instance(ph_dip), (void *)pip));
60817c478bd9Sstevel@tonic-gate 	if (ph_dip == NULL) {
60827c478bd9Sstevel@tonic-gate 		return;
60837c478bd9Sstevel@tonic-gate 	}
60847c478bd9Sstevel@tonic-gate 
60857c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
60867c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, ph_dip, "kidsupcnt was %d\n",
60877c478bd9Sstevel@tonic-gate 	    DEVI(ph_dip)->devi_pm_kidsupcnt));
60885e3986cbScth 
60897c478bd9Sstevel@tonic-gate 	pm_hold_power(ph_dip);
60905e3986cbScth 
60917c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, ph_dip, "kidsupcnt is %d\n",
60927c478bd9Sstevel@tonic-gate 	    DEVI(ph_dip)->devi_pm_kidsupcnt));
60937c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
60947c478bd9Sstevel@tonic-gate 
60955e3986cbScth 	/* If PM_GET_PM_INFO is NULL the pm_hold_power above was a noop */
60965e3986cbScth 	if (DEVI(ph_dip)->devi_pm_info)
60977c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_pm_held = 1;
60987c478bd9Sstevel@tonic-gate }
60997c478bd9Sstevel@tonic-gate 
61007c478bd9Sstevel@tonic-gate /*
61017c478bd9Sstevel@tonic-gate  * Allow phci powered down
61027c478bd9Sstevel@tonic-gate  */
61037c478bd9Sstevel@tonic-gate static void
61047c478bd9Sstevel@tonic-gate i_mdi_pm_rele_pip(mdi_pathinfo_t *pip)
61057c478bd9Sstevel@tonic-gate {
61067c478bd9Sstevel@tonic-gate 	dev_info_t	*ph_dip = NULL;
61077c478bd9Sstevel@tonic-gate 
61087c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
61095e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
61107c478bd9Sstevel@tonic-gate 
61117c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held == 0) {
61127c478bd9Sstevel@tonic-gate 		return;
61137c478bd9Sstevel@tonic-gate 	}
61147c478bd9Sstevel@tonic-gate 
61157c478bd9Sstevel@tonic-gate 	ph_dip = mdi_pi_get_phci(pip);
61167c478bd9Sstevel@tonic-gate 	ASSERT(ph_dip != NULL);
61177c478bd9Sstevel@tonic-gate 
61187c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
61195e3986cbScth 	MDI_DEBUG(4, (CE_NOTE, ph_dip, "i_mdi_pm_rele_pip for %s%d %p\n",
61205e3986cbScth 	    ddi_get_name(ph_dip), ddi_get_instance(ph_dip), (void *)pip));
61217c478bd9Sstevel@tonic-gate 
61227c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, ph_dip, "kidsupcnt was %d\n",
61237c478bd9Sstevel@tonic-gate 	    DEVI(ph_dip)->devi_pm_kidsupcnt));
61247c478bd9Sstevel@tonic-gate 	pm_rele_power(ph_dip);
61257c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, ph_dip, "kidsupcnt is %d\n",
61267c478bd9Sstevel@tonic-gate 	    DEVI(ph_dip)->devi_pm_kidsupcnt));
61277c478bd9Sstevel@tonic-gate 
61287c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
61297c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_pm_held = 0;
61307c478bd9Sstevel@tonic-gate }
61317c478bd9Sstevel@tonic-gate 
61327c478bd9Sstevel@tonic-gate static void
61337c478bd9Sstevel@tonic-gate i_mdi_pm_hold_client(mdi_client_t *ct, int incr)
61347c478bd9Sstevel@tonic-gate {
61355e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
61367c478bd9Sstevel@tonic-gate 
61377c478bd9Sstevel@tonic-gate 	ct->ct_power_cnt += incr;
61385e3986cbScth 	MDI_DEBUG(4, (CE_NOTE, ct->ct_dip, "i_mdi_pm_hold_client %p "
61395e3986cbScth 	    "ct_power_cnt = %d incr = %d\n", (void *)ct,
61405e3986cbScth 	    ct->ct_power_cnt, incr));
61417c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_power_cnt >= 0);
61427c478bd9Sstevel@tonic-gate }
61437c478bd9Sstevel@tonic-gate 
61447c478bd9Sstevel@tonic-gate static void
61457c478bd9Sstevel@tonic-gate i_mdi_rele_all_phci(mdi_client_t *ct)
61467c478bd9Sstevel@tonic-gate {
61477c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t  *pip;
61487c478bd9Sstevel@tonic-gate 
61495e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
61507c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
61517c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
61527c478bd9Sstevel@tonic-gate 		mdi_hold_path(pip);
61537c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
61547c478bd9Sstevel@tonic-gate 		i_mdi_pm_rele_pip(pip);
61557c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
61567c478bd9Sstevel@tonic-gate 		mdi_rele_path(pip);
61577c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
61587c478bd9Sstevel@tonic-gate 	}
61597c478bd9Sstevel@tonic-gate }
61607c478bd9Sstevel@tonic-gate 
61617c478bd9Sstevel@tonic-gate static void
61627c478bd9Sstevel@tonic-gate i_mdi_pm_rele_client(mdi_client_t *ct, int decr)
61637c478bd9Sstevel@tonic-gate {
61645e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
61657c478bd9Sstevel@tonic-gate 
6166737d277aScth 	if (i_ddi_devi_attached(ct->ct_dip)) {
61677c478bd9Sstevel@tonic-gate 		ct->ct_power_cnt -= decr;
61685e3986cbScth 		MDI_DEBUG(4, (CE_NOTE, ct->ct_dip, "i_mdi_pm_rele_client %p "
61695e3986cbScth 		    "ct_power_cnt = %d decr = %d\n",
61705e3986cbScth 		    (void *)ct, ct->ct_power_cnt, decr));
61717c478bd9Sstevel@tonic-gate 	}
61727c478bd9Sstevel@tonic-gate 
61737c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_power_cnt >= 0);
61747c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0) {
61757c478bd9Sstevel@tonic-gate 		i_mdi_rele_all_phci(ct);
61767c478bd9Sstevel@tonic-gate 		return;
61777c478bd9Sstevel@tonic-gate 	}
61787c478bd9Sstevel@tonic-gate }
61797c478bd9Sstevel@tonic-gate 
61807c478bd9Sstevel@tonic-gate static void
61817c478bd9Sstevel@tonic-gate i_mdi_pm_reset_client(mdi_client_t *ct)
61827c478bd9Sstevel@tonic-gate {
61835e3986cbScth 	MDI_DEBUG(4, (CE_NOTE, ct->ct_dip, "i_mdi_pm_reset_client %p "
61845e3986cbScth 	    "ct_power_cnt = %d\n", (void *)ct, ct->ct_power_cnt));
61855e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
61867c478bd9Sstevel@tonic-gate 	ct->ct_power_cnt = 0;
61877c478bd9Sstevel@tonic-gate 	i_mdi_rele_all_phci(ct);
618878dc6db2Sllai1 	ct->ct_powercnt_config = 0;
618978dc6db2Sllai1 	ct->ct_powercnt_unconfig = 0;
61907c478bd9Sstevel@tonic-gate 	ct->ct_powercnt_reset = 1;
61917c478bd9Sstevel@tonic-gate }
61927c478bd9Sstevel@tonic-gate 
61937c478bd9Sstevel@tonic-gate static int
61947c478bd9Sstevel@tonic-gate i_mdi_power_one_phci(mdi_pathinfo_t *pip)
61957c478bd9Sstevel@tonic-gate {
61967c478bd9Sstevel@tonic-gate 	int		ret;
61977c478bd9Sstevel@tonic-gate 	dev_info_t	*ph_dip;
61987c478bd9Sstevel@tonic-gate 
61997c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
62007c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_pip(pip);
62017c478bd9Sstevel@tonic-gate 
62027c478bd9Sstevel@tonic-gate 	ph_dip = mdi_pi_get_phci(pip);
62037c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
62047c478bd9Sstevel@tonic-gate 
62057c478bd9Sstevel@tonic-gate 	/* bring all components of phci to full power */
62067c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, ph_dip, "i_mdi_power_one_phci "
62075e3986cbScth 	    "pm_powerup for %s%d %p\n", ddi_get_name(ph_dip),
62085e3986cbScth 	    ddi_get_instance(ph_dip), (void *)pip));
62097c478bd9Sstevel@tonic-gate 
62107c478bd9Sstevel@tonic-gate 	ret = pm_powerup(ph_dip);
62117c478bd9Sstevel@tonic-gate 
62127c478bd9Sstevel@tonic-gate 	if (ret == DDI_FAILURE) {
62137c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, ph_dip, "i_mdi_power_one_phci "
62145e3986cbScth 		    "pm_powerup FAILED for %s%d %p\n",
62155e3986cbScth 		    ddi_get_name(ph_dip), ddi_get_instance(ph_dip),
62165e3986cbScth 		    (void *)pip));
62177c478bd9Sstevel@tonic-gate 
62187c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
62197c478bd9Sstevel@tonic-gate 		i_mdi_pm_rele_pip(pip);
62207c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
62217c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
62227c478bd9Sstevel@tonic-gate 	}
62237c478bd9Sstevel@tonic-gate 
62247c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
62257c478bd9Sstevel@tonic-gate }
62267c478bd9Sstevel@tonic-gate 
62277c478bd9Sstevel@tonic-gate static int
62287c478bd9Sstevel@tonic-gate i_mdi_power_all_phci(mdi_client_t *ct)
62297c478bd9Sstevel@tonic-gate {
62307c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t  *pip;
62317c478bd9Sstevel@tonic-gate 	int		succeeded = 0;
62327c478bd9Sstevel@tonic-gate 
62335e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
62347c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
62357c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
62365e3986cbScth 		/*
62375e3986cbScth 		 * Don't power if MDI_PATHINFO_STATE_FAULT
62385e3986cbScth 		 * or MDI_PATHINFO_STATE_OFFLINE.
62395e3986cbScth 		 */
62405e3986cbScth 		if (MDI_PI_IS_INIT(pip) ||
62415e3986cbScth 		    MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip)) {
62427c478bd9Sstevel@tonic-gate 			mdi_hold_path(pip);
62437c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
62447c478bd9Sstevel@tonic-gate 			if (i_mdi_power_one_phci(pip) == MDI_SUCCESS)
62457c478bd9Sstevel@tonic-gate 				succeeded = 1;
62467c478bd9Sstevel@tonic-gate 
62477c478bd9Sstevel@tonic-gate 			ASSERT(ct == MDI_PI(pip)->pi_client);
62487c478bd9Sstevel@tonic-gate 			MDI_CLIENT_LOCK(ct);
62497c478bd9Sstevel@tonic-gate 			mdi_rele_path(pip);
62505e3986cbScth 		}
62517c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
62527c478bd9Sstevel@tonic-gate 	}
62537c478bd9Sstevel@tonic-gate 
62547c478bd9Sstevel@tonic-gate 	return (succeeded ? MDI_SUCCESS : MDI_FAILURE);
62557c478bd9Sstevel@tonic-gate }
62567c478bd9Sstevel@tonic-gate 
62577c478bd9Sstevel@tonic-gate /*
62587c478bd9Sstevel@tonic-gate  * mdi_bus_power():
62597c478bd9Sstevel@tonic-gate  *		1. Place the phci(s) into powered up state so that
62607c478bd9Sstevel@tonic-gate  *		   client can do power management
62617c478bd9Sstevel@tonic-gate  *		2. Ensure phci powered up as client power managing
62627c478bd9Sstevel@tonic-gate  * Return Values:
62637c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
62647c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
62657c478bd9Sstevel@tonic-gate  */
62667c478bd9Sstevel@tonic-gate int
62677c478bd9Sstevel@tonic-gate mdi_bus_power(dev_info_t *parent, void *impl_arg, pm_bus_power_op_t op,
62687c478bd9Sstevel@tonic-gate     void *arg, void *result)
62697c478bd9Sstevel@tonic-gate {
62707c478bd9Sstevel@tonic-gate 	int			ret = MDI_SUCCESS;
62717c478bd9Sstevel@tonic-gate 	pm_bp_child_pwrchg_t	*bpc;
62727c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
62737c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
62747c478bd9Sstevel@tonic-gate 	pm_bp_has_changed_t	*bphc;
62757c478bd9Sstevel@tonic-gate 
62767c478bd9Sstevel@tonic-gate 	/*
62777c478bd9Sstevel@tonic-gate 	 * BUS_POWER_NOINVOL not supported
62787c478bd9Sstevel@tonic-gate 	 */
62797c478bd9Sstevel@tonic-gate 	if (op == BUS_POWER_NOINVOL)
62807c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
62817c478bd9Sstevel@tonic-gate 
62827c478bd9Sstevel@tonic-gate 	/*
62837c478bd9Sstevel@tonic-gate 	 * ignore other OPs.
62847c478bd9Sstevel@tonic-gate 	 * return quickly to save cou cycles on the ct processing
62857c478bd9Sstevel@tonic-gate 	 */
62867c478bd9Sstevel@tonic-gate 	switch (op) {
62877c478bd9Sstevel@tonic-gate 	case BUS_POWER_PRE_NOTIFICATION:
62887c478bd9Sstevel@tonic-gate 	case BUS_POWER_POST_NOTIFICATION:
62897c478bd9Sstevel@tonic-gate 		bpc = (pm_bp_child_pwrchg_t *)arg;
62907c478bd9Sstevel@tonic-gate 		cdip = bpc->bpc_dip;
62917c478bd9Sstevel@tonic-gate 		break;
62927c478bd9Sstevel@tonic-gate 	case BUS_POWER_HAS_CHANGED:
62937c478bd9Sstevel@tonic-gate 		bphc = (pm_bp_has_changed_t *)arg;
62947c478bd9Sstevel@tonic-gate 		cdip = bphc->bphc_dip;
62957c478bd9Sstevel@tonic-gate 		break;
62967c478bd9Sstevel@tonic-gate 	default:
62977c478bd9Sstevel@tonic-gate 		return (pm_busop_bus_power(parent, impl_arg, op, arg, result));
62987c478bd9Sstevel@tonic-gate 	}
62997c478bd9Sstevel@tonic-gate 
63007c478bd9Sstevel@tonic-gate 	ASSERT(MDI_CLIENT(cdip));
63017c478bd9Sstevel@tonic-gate 
63027c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
63037c478bd9Sstevel@tonic-gate 	if (ct == NULL)
63047c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
63057c478bd9Sstevel@tonic-gate 
63067c478bd9Sstevel@tonic-gate 	/*
63077c478bd9Sstevel@tonic-gate 	 * wait till the mdi_pathinfo node state change are processed
63087c478bd9Sstevel@tonic-gate 	 */
63097c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
63107c478bd9Sstevel@tonic-gate 	switch (op) {
63117c478bd9Sstevel@tonic-gate 	case BUS_POWER_PRE_NOTIFICATION:
63127c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, bpc->bpc_dip, "mdi_bus_power "
63137c478bd9Sstevel@tonic-gate 		    "BUS_POWER_PRE_NOTIFICATION:"
63147c478bd9Sstevel@tonic-gate 		    "%s@%s, olevel=%d, nlevel=%d, comp=%d\n",
63157c478bd9Sstevel@tonic-gate 		    PM_NAME(bpc->bpc_dip), PM_ADDR(bpc->bpc_dip),
63167c478bd9Sstevel@tonic-gate 		    bpc->bpc_olevel, bpc->bpc_nlevel, bpc->bpc_comp));
63177c478bd9Sstevel@tonic-gate 
63187c478bd9Sstevel@tonic-gate 		/* serialize power level change per client */
63197c478bd9Sstevel@tonic-gate 		while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
63207c478bd9Sstevel@tonic-gate 			cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
63217c478bd9Sstevel@tonic-gate 
63227c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_POWER_TRANSITION(ct);
63237c478bd9Sstevel@tonic-gate 
63247c478bd9Sstevel@tonic-gate 		if (ct->ct_power_cnt == 0) {
63257c478bd9Sstevel@tonic-gate 			ret = i_mdi_power_all_phci(ct);
63267c478bd9Sstevel@tonic-gate 		}
63277c478bd9Sstevel@tonic-gate 
63287c478bd9Sstevel@tonic-gate 		/*
63297c478bd9Sstevel@tonic-gate 		 * if new_level > 0:
63307c478bd9Sstevel@tonic-gate 		 *	- hold phci(s)
63317c478bd9Sstevel@tonic-gate 		 *	- power up phci(s) if not already
63327c478bd9Sstevel@tonic-gate 		 * ignore power down
63337c478bd9Sstevel@tonic-gate 		 */
63347c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel > 0) {
63357c478bd9Sstevel@tonic-gate 			if (!DEVI_IS_ATTACHING(ct->ct_dip)) {
63367c478bd9Sstevel@tonic-gate 				MDI_DEBUG(4, (CE_NOTE, bpc->bpc_dip,
63377c478bd9Sstevel@tonic-gate 				    "mdi_bus_power i_mdi_pm_hold_client\n"));
63387c478bd9Sstevel@tonic-gate 				i_mdi_pm_hold_client(ct, ct->ct_path_count);
63397c478bd9Sstevel@tonic-gate 			}
63407c478bd9Sstevel@tonic-gate 		}
63417c478bd9Sstevel@tonic-gate 		break;
63427c478bd9Sstevel@tonic-gate 	case BUS_POWER_POST_NOTIFICATION:
63437c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, bpc->bpc_dip, "mdi_bus_power "
63447c478bd9Sstevel@tonic-gate 		    "BUS_POWER_POST_NOTIFICATION:"
63457c478bd9Sstevel@tonic-gate 		    "%s@%s, olevel=%d, nlevel=%d, comp=%d result=%d\n",
63467c478bd9Sstevel@tonic-gate 		    PM_NAME(bpc->bpc_dip), PM_ADDR(bpc->bpc_dip),
63477c478bd9Sstevel@tonic-gate 		    bpc->bpc_olevel, bpc->bpc_nlevel, bpc->bpc_comp,
63487c478bd9Sstevel@tonic-gate 		    *(int *)result));
63497c478bd9Sstevel@tonic-gate 
63507c478bd9Sstevel@tonic-gate 		if (*(int *)result == DDI_SUCCESS) {
63517c478bd9Sstevel@tonic-gate 			if (bpc->bpc_nlevel > 0) {
63527c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_POWER_UP(ct);
63537c478bd9Sstevel@tonic-gate 			} else {
63547c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_POWER_DOWN(ct);
63557c478bd9Sstevel@tonic-gate 			}
63567c478bd9Sstevel@tonic-gate 		}
63577c478bd9Sstevel@tonic-gate 
63587c478bd9Sstevel@tonic-gate 		/* release the hold we did in pre-notification */
63597c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel > 0 && (*(int *)result != DDI_SUCCESS) &&
63607c478bd9Sstevel@tonic-gate 		    !DEVI_IS_ATTACHING(ct->ct_dip)) {
63617c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, bpc->bpc_dip,
63627c478bd9Sstevel@tonic-gate 			    "mdi_bus_power i_mdi_pm_rele_client\n"));
63637c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, ct->ct_path_count);
63647c478bd9Sstevel@tonic-gate 		}
63657c478bd9Sstevel@tonic-gate 
63667c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel == 0 && (*(int *)result == DDI_SUCCESS)) {
63677c478bd9Sstevel@tonic-gate 			/* another thread might started attaching */
63687c478bd9Sstevel@tonic-gate 			if (DEVI_IS_ATTACHING(ct->ct_dip)) {
63697c478bd9Sstevel@tonic-gate 				MDI_DEBUG(4, (CE_NOTE, bpc->bpc_dip,
63707c478bd9Sstevel@tonic-gate 				    "mdi_bus_power i_mdi_pm_rele_client\n"));
63717c478bd9Sstevel@tonic-gate 				i_mdi_pm_rele_client(ct, ct->ct_path_count);
63727c478bd9Sstevel@tonic-gate 			/* detaching has been taken care in pm_post_unconfig */
63737c478bd9Sstevel@tonic-gate 			} else if (!DEVI_IS_DETACHING(ct->ct_dip)) {
63747c478bd9Sstevel@tonic-gate 				MDI_DEBUG(4, (CE_NOTE, bpc->bpc_dip,
63757c478bd9Sstevel@tonic-gate 				    "mdi_bus_power i_mdi_pm_reset_client\n"));
63767c478bd9Sstevel@tonic-gate 				i_mdi_pm_reset_client(ct);
63777c478bd9Sstevel@tonic-gate 			}
63787c478bd9Sstevel@tonic-gate 		}
63797c478bd9Sstevel@tonic-gate 
63807c478bd9Sstevel@tonic-gate 		MDI_CLIENT_CLEAR_POWER_TRANSITION(ct);
63817c478bd9Sstevel@tonic-gate 		cv_broadcast(&ct->ct_powerchange_cv);
63827c478bd9Sstevel@tonic-gate 
63837c478bd9Sstevel@tonic-gate 		break;
63847c478bd9Sstevel@tonic-gate 
63857c478bd9Sstevel@tonic-gate 	/* need to do more */
63867c478bd9Sstevel@tonic-gate 	case BUS_POWER_HAS_CHANGED:
63877c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, bphc->bphc_dip, "mdi_bus_power "
63887c478bd9Sstevel@tonic-gate 		    "BUS_POWER_HAS_CHANGED:"
63897c478bd9Sstevel@tonic-gate 		    "%s@%s, olevel=%d, nlevel=%d, comp=%d\n",
63907c478bd9Sstevel@tonic-gate 		    PM_NAME(bphc->bphc_dip), PM_ADDR(bphc->bphc_dip),
63917c478bd9Sstevel@tonic-gate 		    bphc->bphc_olevel, bphc->bphc_nlevel, bphc->bphc_comp));
63927c478bd9Sstevel@tonic-gate 
63937c478bd9Sstevel@tonic-gate 		if (bphc->bphc_nlevel > 0 &&
63947c478bd9Sstevel@tonic-gate 		    bphc->bphc_nlevel > bphc->bphc_olevel) {
63957c478bd9Sstevel@tonic-gate 			if (ct->ct_power_cnt == 0) {
63967c478bd9Sstevel@tonic-gate 				ret = i_mdi_power_all_phci(ct);
63977c478bd9Sstevel@tonic-gate 			}
63987c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, bphc->bphc_dip,
63997c478bd9Sstevel@tonic-gate 			    "mdi_bus_power i_mdi_pm_hold_client\n"));
64007c478bd9Sstevel@tonic-gate 			i_mdi_pm_hold_client(ct, ct->ct_path_count);
64017c478bd9Sstevel@tonic-gate 		}
64027c478bd9Sstevel@tonic-gate 
64037c478bd9Sstevel@tonic-gate 		if (bphc->bphc_nlevel == 0 && bphc->bphc_olevel != -1) {
64047c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, bphc->bphc_dip,
64057c478bd9Sstevel@tonic-gate 			    "mdi_bus_power i_mdi_pm_rele_client\n"));
64067c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, ct->ct_path_count);
64077c478bd9Sstevel@tonic-gate 		}
64087c478bd9Sstevel@tonic-gate 		break;
64097c478bd9Sstevel@tonic-gate 	}
64107c478bd9Sstevel@tonic-gate 
64117c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
64127c478bd9Sstevel@tonic-gate 	return (ret);
64137c478bd9Sstevel@tonic-gate }
64147c478bd9Sstevel@tonic-gate 
64157c478bd9Sstevel@tonic-gate static int
64167c478bd9Sstevel@tonic-gate i_mdi_pm_pre_config_one(dev_info_t *child)
64177c478bd9Sstevel@tonic-gate {
64187c478bd9Sstevel@tonic-gate 	int		ret = MDI_SUCCESS;
64197c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
64207c478bd9Sstevel@tonic-gate 
64217c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
64227c478bd9Sstevel@tonic-gate 	if (ct == NULL)
64237c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
64247c478bd9Sstevel@tonic-gate 
64257c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
64267c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
64277c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
64287c478bd9Sstevel@tonic-gate 
64297c478bd9Sstevel@tonic-gate 	if (!MDI_CLIENT_IS_FAILED(ct)) {
64307c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
64317c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
64327c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_pre_config_one already configured\n"));
64337c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
64347c478bd9Sstevel@tonic-gate 	}
64357c478bd9Sstevel@tonic-gate 
643678dc6db2Sllai1 	if (ct->ct_powercnt_config) {
64377c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
64387c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
64397c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_pre_config_one ALREADY held\n"));
64407c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
64417c478bd9Sstevel@tonic-gate 	}
64427c478bd9Sstevel@tonic-gate 
64437c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0) {
64447c478bd9Sstevel@tonic-gate 		ret = i_mdi_power_all_phci(ct);
64457c478bd9Sstevel@tonic-gate 	}
64467c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, child,
64477c478bd9Sstevel@tonic-gate 	    "i_mdi_pm_pre_config_one i_mdi_pm_hold_client\n"));
64487c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_client(ct, ct->ct_path_count);
644978dc6db2Sllai1 	ct->ct_powercnt_config = 1;
64507c478bd9Sstevel@tonic-gate 	ct->ct_powercnt_reset = 0;
64517c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
64527c478bd9Sstevel@tonic-gate 	return (ret);
64537c478bd9Sstevel@tonic-gate }
64547c478bd9Sstevel@tonic-gate 
64557c478bd9Sstevel@tonic-gate static int
64565e3986cbScth i_mdi_pm_pre_config(dev_info_t *vdip, dev_info_t *child)
64577c478bd9Sstevel@tonic-gate {
64587c478bd9Sstevel@tonic-gate 	int			ret = MDI_SUCCESS;
64597c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
64607c478bd9Sstevel@tonic-gate 	int			circ;
64617c478bd9Sstevel@tonic-gate 
64625e3986cbScth 	ASSERT(MDI_VHCI(vdip));
64637c478bd9Sstevel@tonic-gate 
64647c478bd9Sstevel@tonic-gate 	/* ndi_devi_config_one */
64657c478bd9Sstevel@tonic-gate 	if (child) {
64665e3986cbScth 		ASSERT(DEVI_BUSY_OWNED(vdip));
64677c478bd9Sstevel@tonic-gate 		return (i_mdi_pm_pre_config_one(child));
64687c478bd9Sstevel@tonic-gate 	}
64697c478bd9Sstevel@tonic-gate 
64707c478bd9Sstevel@tonic-gate 	/* devi_config_common */
64715e3986cbScth 	ndi_devi_enter(vdip, &circ);
64725e3986cbScth 	cdip = ddi_get_child(vdip);
64737c478bd9Sstevel@tonic-gate 	while (cdip) {
64747c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
64757c478bd9Sstevel@tonic-gate 
64767c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_config_one(cdip);
64777c478bd9Sstevel@tonic-gate 		if (ret != MDI_SUCCESS)
64787c478bd9Sstevel@tonic-gate 			break;
64797c478bd9Sstevel@tonic-gate 		cdip = next;
64807c478bd9Sstevel@tonic-gate 	}
64815e3986cbScth 	ndi_devi_exit(vdip, circ);
64827c478bd9Sstevel@tonic-gate 	return (ret);
64837c478bd9Sstevel@tonic-gate }
64847c478bd9Sstevel@tonic-gate 
64857c478bd9Sstevel@tonic-gate static int
64867c478bd9Sstevel@tonic-gate i_mdi_pm_pre_unconfig_one(dev_info_t *child, int *held, int flags)
64877c478bd9Sstevel@tonic-gate {
64887c478bd9Sstevel@tonic-gate 	int		ret = MDI_SUCCESS;
64897c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
64907c478bd9Sstevel@tonic-gate 
64917c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
64927c478bd9Sstevel@tonic-gate 	if (ct == NULL)
64937c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
64947c478bd9Sstevel@tonic-gate 
64957c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
64967c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
64977c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
64987c478bd9Sstevel@tonic-gate 
6499737d277aScth 	if (!i_ddi_devi_attached(ct->ct_dip)) {
65007c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
65017c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_pre_unconfig node detached already\n"));
65027c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
65037c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
65047c478bd9Sstevel@tonic-gate 	}
65057c478bd9Sstevel@tonic-gate 
65067c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_POWERED_DOWN(ct) &&
65077c478bd9Sstevel@tonic-gate 	    (flags & NDI_AUTODETACH)) {
65087c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
65097c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_pre_unconfig auto-modunload\n"));
65107c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
65117c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
65127c478bd9Sstevel@tonic-gate 	}
65137c478bd9Sstevel@tonic-gate 
651478dc6db2Sllai1 	if (ct->ct_powercnt_unconfig) {
65157c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
65167c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_pre_unconfig ct_powercnt_held\n"));
65177c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
65187c478bd9Sstevel@tonic-gate 		*held = 1;
65197c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
65207c478bd9Sstevel@tonic-gate 	}
65217c478bd9Sstevel@tonic-gate 
65227c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0) {
65237c478bd9Sstevel@tonic-gate 		ret = i_mdi_power_all_phci(ct);
65247c478bd9Sstevel@tonic-gate 	}
65257c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, child,
65267c478bd9Sstevel@tonic-gate 	    "i_mdi_pm_pre_unconfig i_mdi_pm_hold_client\n"));
65277c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_client(ct, ct->ct_path_count);
652878dc6db2Sllai1 	ct->ct_powercnt_unconfig = 1;
65297c478bd9Sstevel@tonic-gate 	ct->ct_powercnt_reset = 0;
65307c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
65317c478bd9Sstevel@tonic-gate 	if (ret == MDI_SUCCESS)
65327c478bd9Sstevel@tonic-gate 		*held = 1;
65337c478bd9Sstevel@tonic-gate 	return (ret);
65347c478bd9Sstevel@tonic-gate }
65357c478bd9Sstevel@tonic-gate 
65367c478bd9Sstevel@tonic-gate static int
65375e3986cbScth i_mdi_pm_pre_unconfig(dev_info_t *vdip, dev_info_t *child, int *held,
65387c478bd9Sstevel@tonic-gate     int flags)
65397c478bd9Sstevel@tonic-gate {
65407c478bd9Sstevel@tonic-gate 	int			ret = MDI_SUCCESS;
65417c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
65427c478bd9Sstevel@tonic-gate 	int			circ;
65437c478bd9Sstevel@tonic-gate 
65445e3986cbScth 	ASSERT(MDI_VHCI(vdip));
65457c478bd9Sstevel@tonic-gate 	*held = 0;
65467c478bd9Sstevel@tonic-gate 
65477c478bd9Sstevel@tonic-gate 	/* ndi_devi_unconfig_one */
65487c478bd9Sstevel@tonic-gate 	if (child) {
65495e3986cbScth 		ASSERT(DEVI_BUSY_OWNED(vdip));
65507c478bd9Sstevel@tonic-gate 		return (i_mdi_pm_pre_unconfig_one(child, held, flags));
65517c478bd9Sstevel@tonic-gate 	}
65527c478bd9Sstevel@tonic-gate 
65537c478bd9Sstevel@tonic-gate 	/* devi_unconfig_common */
65545e3986cbScth 	ndi_devi_enter(vdip, &circ);
65555e3986cbScth 	cdip = ddi_get_child(vdip);
65567c478bd9Sstevel@tonic-gate 	while (cdip) {
65577c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
65587c478bd9Sstevel@tonic-gate 
65597c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_unconfig_one(cdip, held, flags);
65607c478bd9Sstevel@tonic-gate 		cdip = next;
65617c478bd9Sstevel@tonic-gate 	}
65625e3986cbScth 	ndi_devi_exit(vdip, circ);
65637c478bd9Sstevel@tonic-gate 
65647c478bd9Sstevel@tonic-gate 	if (*held)
65657c478bd9Sstevel@tonic-gate 		ret = MDI_SUCCESS;
65667c478bd9Sstevel@tonic-gate 
65677c478bd9Sstevel@tonic-gate 	return (ret);
65687c478bd9Sstevel@tonic-gate }
65697c478bd9Sstevel@tonic-gate 
65707c478bd9Sstevel@tonic-gate static void
65717c478bd9Sstevel@tonic-gate i_mdi_pm_post_config_one(dev_info_t *child)
65727c478bd9Sstevel@tonic-gate {
65737c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
65747c478bd9Sstevel@tonic-gate 
65757c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
65767c478bd9Sstevel@tonic-gate 	if (ct == NULL)
65777c478bd9Sstevel@tonic-gate 		return;
65787c478bd9Sstevel@tonic-gate 
65797c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
65807c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
65817c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
65827c478bd9Sstevel@tonic-gate 
658378dc6db2Sllai1 	if (ct->ct_powercnt_reset || !ct->ct_powercnt_config) {
65847c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
658578dc6db2Sllai1 		    "i_mdi_pm_post_config_one NOT configured\n"));
65867c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
65877c478bd9Sstevel@tonic-gate 		return;
65887c478bd9Sstevel@tonic-gate 	}
65897c478bd9Sstevel@tonic-gate 
65907c478bd9Sstevel@tonic-gate 	/* client has not been updated */
65917c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_FAILED(ct)) {
65927c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
65937c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_post_config_one NOT configured\n"));
65947c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
65957c478bd9Sstevel@tonic-gate 		return;
65967c478bd9Sstevel@tonic-gate 	}
65977c478bd9Sstevel@tonic-gate 
65987c478bd9Sstevel@tonic-gate 	/* another thread might have powered it down or detached it */
65997c478bd9Sstevel@tonic-gate 	if ((MDI_CLIENT_IS_POWERED_DOWN(ct) &&
66007c478bd9Sstevel@tonic-gate 	    !DEVI_IS_ATTACHING(ct->ct_dip)) ||
6601737d277aScth 	    (!i_ddi_devi_attached(ct->ct_dip) &&
66027c478bd9Sstevel@tonic-gate 	    !DEVI_IS_ATTACHING(ct->ct_dip))) {
66037c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
66047c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_post_config i_mdi_pm_reset_client\n"));
66057c478bd9Sstevel@tonic-gate 		i_mdi_pm_reset_client(ct);
66067c478bd9Sstevel@tonic-gate 	} else {
66077c478bd9Sstevel@tonic-gate 		mdi_pathinfo_t  *pip, *next;
66087c478bd9Sstevel@tonic-gate 		int	valid_path_count = 0;
66097c478bd9Sstevel@tonic-gate 
66107c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
66117c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_post_config i_mdi_pm_rele_client\n"));
66127c478bd9Sstevel@tonic-gate 		pip = ct->ct_path_head;
66137c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
66147c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
66157c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
661678dc6db2Sllai1 			if (MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip))
66177c478bd9Sstevel@tonic-gate 				valid_path_count ++;
66187c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
66197c478bd9Sstevel@tonic-gate 			pip = next;
66207c478bd9Sstevel@tonic-gate 		}
66217c478bd9Sstevel@tonic-gate 		i_mdi_pm_rele_client(ct, valid_path_count);
66227c478bd9Sstevel@tonic-gate 	}
662378dc6db2Sllai1 	ct->ct_powercnt_config = 0;
66247c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
66257c478bd9Sstevel@tonic-gate }
66267c478bd9Sstevel@tonic-gate 
66277c478bd9Sstevel@tonic-gate static void
66285e3986cbScth i_mdi_pm_post_config(dev_info_t *vdip, dev_info_t *child)
66297c478bd9Sstevel@tonic-gate {
66307c478bd9Sstevel@tonic-gate 	int		circ;
66317c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
66325e3986cbScth 
66335e3986cbScth 	ASSERT(MDI_VHCI(vdip));
66347c478bd9Sstevel@tonic-gate 
66357c478bd9Sstevel@tonic-gate 	/* ndi_devi_config_one */
66367c478bd9Sstevel@tonic-gate 	if (child) {
66375e3986cbScth 		ASSERT(DEVI_BUSY_OWNED(vdip));
66387c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_config_one(child);
66397c478bd9Sstevel@tonic-gate 		return;
66407c478bd9Sstevel@tonic-gate 	}
66417c478bd9Sstevel@tonic-gate 
66427c478bd9Sstevel@tonic-gate 	/* devi_config_common */
66435e3986cbScth 	ndi_devi_enter(vdip, &circ);
66445e3986cbScth 	cdip = ddi_get_child(vdip);
66457c478bd9Sstevel@tonic-gate 	while (cdip) {
66467c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
66477c478bd9Sstevel@tonic-gate 
66487c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_config_one(cdip);
66497c478bd9Sstevel@tonic-gate 		cdip = next;
66507c478bd9Sstevel@tonic-gate 	}
66515e3986cbScth 	ndi_devi_exit(vdip, circ);
66527c478bd9Sstevel@tonic-gate }
66537c478bd9Sstevel@tonic-gate 
66547c478bd9Sstevel@tonic-gate static void
66557c478bd9Sstevel@tonic-gate i_mdi_pm_post_unconfig_one(dev_info_t *child)
66567c478bd9Sstevel@tonic-gate {
66577c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
66587c478bd9Sstevel@tonic-gate 
66597c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
66607c478bd9Sstevel@tonic-gate 	if (ct == NULL)
66617c478bd9Sstevel@tonic-gate 		return;
66627c478bd9Sstevel@tonic-gate 
66637c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
66647c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
66657c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
66667c478bd9Sstevel@tonic-gate 
666778dc6db2Sllai1 	if (!ct->ct_powercnt_unconfig || ct->ct_powercnt_reset) {
66687c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
66697c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_post_unconfig NOT held\n"));
66707c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
66717c478bd9Sstevel@tonic-gate 		return;
66727c478bd9Sstevel@tonic-gate 	}
66737c478bd9Sstevel@tonic-gate 
66747c478bd9Sstevel@tonic-gate 	/* failure detaching or another thread just attached it */
66757c478bd9Sstevel@tonic-gate 	if ((MDI_CLIENT_IS_POWERED_DOWN(ct) &&
6676737d277aScth 	    i_ddi_devi_attached(ct->ct_dip)) ||
6677737d277aScth 	    (!i_ddi_devi_attached(ct->ct_dip) &&
66787c478bd9Sstevel@tonic-gate 	    !DEVI_IS_ATTACHING(ct->ct_dip))) {
66797c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
66807c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_post_unconfig i_mdi_pm_reset_client\n"));
66817c478bd9Sstevel@tonic-gate 		i_mdi_pm_reset_client(ct);
668278dc6db2Sllai1 	} else {
668378dc6db2Sllai1 		mdi_pathinfo_t  *pip, *next;
668478dc6db2Sllai1 		int	valid_path_count = 0;
66857c478bd9Sstevel@tonic-gate 
66867c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
668778dc6db2Sllai1 		    "i_mdi_pm_post_unconfig i_mdi_pm_rele_client\n"));
668878dc6db2Sllai1 		pip = ct->ct_path_head;
668978dc6db2Sllai1 		while (pip != NULL) {
669078dc6db2Sllai1 			MDI_PI_LOCK(pip);
669178dc6db2Sllai1 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
669278dc6db2Sllai1 			if (MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip))
669378dc6db2Sllai1 				valid_path_count ++;
669478dc6db2Sllai1 			MDI_PI_UNLOCK(pip);
669578dc6db2Sllai1 			pip = next;
669678dc6db2Sllai1 		}
669778dc6db2Sllai1 		i_mdi_pm_rele_client(ct, valid_path_count);
669878dc6db2Sllai1 		ct->ct_powercnt_unconfig = 0;
669978dc6db2Sllai1 	}
670078dc6db2Sllai1 
67017c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
67027c478bd9Sstevel@tonic-gate }
67037c478bd9Sstevel@tonic-gate 
67047c478bd9Sstevel@tonic-gate static void
67055e3986cbScth i_mdi_pm_post_unconfig(dev_info_t *vdip, dev_info_t *child, int held)
67067c478bd9Sstevel@tonic-gate {
67077c478bd9Sstevel@tonic-gate 	int			circ;
67087c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
67097c478bd9Sstevel@tonic-gate 
67105e3986cbScth 	ASSERT(MDI_VHCI(vdip));
67117c478bd9Sstevel@tonic-gate 
67127c478bd9Sstevel@tonic-gate 	if (!held) {
67135e3986cbScth 		MDI_DEBUG(4, (CE_NOTE, vdip,
67147c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_post_unconfig held = %d\n", held));
67157c478bd9Sstevel@tonic-gate 		return;
67167c478bd9Sstevel@tonic-gate 	}
67177c478bd9Sstevel@tonic-gate 
67187c478bd9Sstevel@tonic-gate 	if (child) {
67195e3986cbScth 		ASSERT(DEVI_BUSY_OWNED(vdip));
67207c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_unconfig_one(child);
67217c478bd9Sstevel@tonic-gate 		return;
67227c478bd9Sstevel@tonic-gate 	}
67237c478bd9Sstevel@tonic-gate 
67245e3986cbScth 	ndi_devi_enter(vdip, &circ);
67255e3986cbScth 	cdip = ddi_get_child(vdip);
67267c478bd9Sstevel@tonic-gate 	while (cdip) {
67277c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
67287c478bd9Sstevel@tonic-gate 
67297c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_unconfig_one(cdip);
67307c478bd9Sstevel@tonic-gate 		cdip = next;
67317c478bd9Sstevel@tonic-gate 	}
67325e3986cbScth 	ndi_devi_exit(vdip, circ);
67337c478bd9Sstevel@tonic-gate }
67347c478bd9Sstevel@tonic-gate 
67357c478bd9Sstevel@tonic-gate int
67367c478bd9Sstevel@tonic-gate mdi_power(dev_info_t *vdip, mdi_pm_op_t op, void *args, char *devnm, int flags)
67377c478bd9Sstevel@tonic-gate {
67387c478bd9Sstevel@tonic-gate 	int			circ, ret = MDI_SUCCESS;
67397c478bd9Sstevel@tonic-gate 	dev_info_t		*client_dip = NULL;
67407c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
67417c478bd9Sstevel@tonic-gate 
67427c478bd9Sstevel@tonic-gate 	/*
67437c478bd9Sstevel@tonic-gate 	 * Handling ndi_devi_config_one and ndi_devi_unconfig_one.
67447c478bd9Sstevel@tonic-gate 	 * Power up pHCI for the named client device.
67457c478bd9Sstevel@tonic-gate 	 * Note: Before the client is enumerated under vhci by phci,
67467c478bd9Sstevel@tonic-gate 	 * client_dip can be NULL. Then proceed to power up all the
67477c478bd9Sstevel@tonic-gate 	 * pHCIs.
67487c478bd9Sstevel@tonic-gate 	 */
67497c478bd9Sstevel@tonic-gate 	if (devnm != NULL) {
67507c478bd9Sstevel@tonic-gate 		ndi_devi_enter(vdip, &circ);
67517c478bd9Sstevel@tonic-gate 		client_dip = ndi_devi_findchild(vdip, devnm);
67527c478bd9Sstevel@tonic-gate 	}
67537c478bd9Sstevel@tonic-gate 
67545e3986cbScth 	MDI_DEBUG(4, (CE_NOTE, vdip, "mdi_power op = %d %s %p\n",
67555e3986cbScth 	    op, devnm ? devnm : "NULL", (void *)client_dip));
67567c478bd9Sstevel@tonic-gate 
67577c478bd9Sstevel@tonic-gate 	switch (op) {
67587c478bd9Sstevel@tonic-gate 	case MDI_PM_PRE_CONFIG:
67597c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_config(vdip, client_dip);
6760c73a93f2Sdm120769 		break;
67615e3986cbScth 
67627c478bd9Sstevel@tonic-gate 	case MDI_PM_PRE_UNCONFIG:
67637c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_unconfig(vdip, client_dip, (int *)args,
67647c478bd9Sstevel@tonic-gate 		    flags);
6765c73a93f2Sdm120769 		break;
67665e3986cbScth 
67677c478bd9Sstevel@tonic-gate 	case MDI_PM_POST_CONFIG:
67687c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_config(vdip, client_dip);
6769c73a93f2Sdm120769 		break;
67705e3986cbScth 
67717c478bd9Sstevel@tonic-gate 	case MDI_PM_POST_UNCONFIG:
67727c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_unconfig(vdip, client_dip, *(int *)args);
6773c73a93f2Sdm120769 		break;
67745e3986cbScth 
67757c478bd9Sstevel@tonic-gate 	case MDI_PM_HOLD_POWER:
67767c478bd9Sstevel@tonic-gate 	case MDI_PM_RELE_POWER:
67777c478bd9Sstevel@tonic-gate 		ASSERT(args);
67787c478bd9Sstevel@tonic-gate 
67797c478bd9Sstevel@tonic-gate 		client_dip = (dev_info_t *)args;
67807c478bd9Sstevel@tonic-gate 		ASSERT(MDI_CLIENT(client_dip));
67817c478bd9Sstevel@tonic-gate 
67827c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(client_dip);
67837c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
67847c478bd9Sstevel@tonic-gate 
67857c478bd9Sstevel@tonic-gate 		if (op == MDI_PM_HOLD_POWER) {
67867c478bd9Sstevel@tonic-gate 			if (ct->ct_power_cnt == 0) {
67877c478bd9Sstevel@tonic-gate 				(void) i_mdi_power_all_phci(ct);
67887c478bd9Sstevel@tonic-gate 				MDI_DEBUG(4, (CE_NOTE, client_dip,
67897c478bd9Sstevel@tonic-gate 				    "mdi_power i_mdi_pm_hold_client\n"));
67907c478bd9Sstevel@tonic-gate 				i_mdi_pm_hold_client(ct, ct->ct_path_count);
67917c478bd9Sstevel@tonic-gate 			}
67927c478bd9Sstevel@tonic-gate 		} else {
67937c478bd9Sstevel@tonic-gate 			if (DEVI_IS_ATTACHING(ct->ct_dip)) {
67947c478bd9Sstevel@tonic-gate 				MDI_DEBUG(4, (CE_NOTE, client_dip,
67957c478bd9Sstevel@tonic-gate 				    "mdi_power i_mdi_pm_rele_client\n"));
67967c478bd9Sstevel@tonic-gate 				i_mdi_pm_rele_client(ct, ct->ct_path_count);
67977c478bd9Sstevel@tonic-gate 			} else {
67987c478bd9Sstevel@tonic-gate 				MDI_DEBUG(4, (CE_NOTE, client_dip,
67997c478bd9Sstevel@tonic-gate 				    "mdi_power i_mdi_pm_reset_client\n"));
68007c478bd9Sstevel@tonic-gate 				i_mdi_pm_reset_client(ct);
68017c478bd9Sstevel@tonic-gate 			}
68027c478bd9Sstevel@tonic-gate 		}
68037c478bd9Sstevel@tonic-gate 
68047c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
68057c478bd9Sstevel@tonic-gate 		break;
68065e3986cbScth 
68077c478bd9Sstevel@tonic-gate 	default:
68087c478bd9Sstevel@tonic-gate 		break;
68097c478bd9Sstevel@tonic-gate 	}
68107c478bd9Sstevel@tonic-gate 
68115e3986cbScth 	if (devnm)
68125e3986cbScth 		ndi_devi_exit(vdip, circ);
68135e3986cbScth 
68147c478bd9Sstevel@tonic-gate 	return (ret);
68157c478bd9Sstevel@tonic-gate }
68167c478bd9Sstevel@tonic-gate 
68177c478bd9Sstevel@tonic-gate int
68187c478bd9Sstevel@tonic-gate mdi_component_is_vhci(dev_info_t *dip, const char **mdi_class)
68197c478bd9Sstevel@tonic-gate {
68207c478bd9Sstevel@tonic-gate 	mdi_vhci_t *vhci;
68217c478bd9Sstevel@tonic-gate 
68227c478bd9Sstevel@tonic-gate 	if (!MDI_VHCI(dip))
68237c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
68247c478bd9Sstevel@tonic-gate 
68257c478bd9Sstevel@tonic-gate 	if (mdi_class) {
68267c478bd9Sstevel@tonic-gate 		vhci = DEVI(dip)->devi_mdi_xhci;
68277c478bd9Sstevel@tonic-gate 		ASSERT(vhci);
68287c478bd9Sstevel@tonic-gate 		*mdi_class = vhci->vh_class;
68297c478bd9Sstevel@tonic-gate 	}
68307c478bd9Sstevel@tonic-gate 
68317c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
68327c478bd9Sstevel@tonic-gate }
68337c478bd9Sstevel@tonic-gate 
68347c478bd9Sstevel@tonic-gate int
68357c478bd9Sstevel@tonic-gate mdi_component_is_phci(dev_info_t *dip, const char **mdi_class)
68367c478bd9Sstevel@tonic-gate {
68377c478bd9Sstevel@tonic-gate 	mdi_phci_t *phci;
68387c478bd9Sstevel@tonic-gate 
68397c478bd9Sstevel@tonic-gate 	if (!MDI_PHCI(dip))
68407c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
68417c478bd9Sstevel@tonic-gate 
68427c478bd9Sstevel@tonic-gate 	if (mdi_class) {
68437c478bd9Sstevel@tonic-gate 		phci = DEVI(dip)->devi_mdi_xhci;
68447c478bd9Sstevel@tonic-gate 		ASSERT(phci);
68457c478bd9Sstevel@tonic-gate 		*mdi_class = phci->ph_vhci->vh_class;
68467c478bd9Sstevel@tonic-gate 	}
68477c478bd9Sstevel@tonic-gate 
68487c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
68497c478bd9Sstevel@tonic-gate }
68507c478bd9Sstevel@tonic-gate 
68517c478bd9Sstevel@tonic-gate int
68527c478bd9Sstevel@tonic-gate mdi_component_is_client(dev_info_t *dip, const char **mdi_class)
68537c478bd9Sstevel@tonic-gate {
68547c478bd9Sstevel@tonic-gate 	mdi_client_t *client;
68557c478bd9Sstevel@tonic-gate 
68567c478bd9Sstevel@tonic-gate 	if (!MDI_CLIENT(dip))
68577c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
68587c478bd9Sstevel@tonic-gate 
68597c478bd9Sstevel@tonic-gate 	if (mdi_class) {
68607c478bd9Sstevel@tonic-gate 		client = DEVI(dip)->devi_mdi_client;
68617c478bd9Sstevel@tonic-gate 		ASSERT(client);
68627c478bd9Sstevel@tonic-gate 		*mdi_class = client->ct_vhci->vh_class;
68637c478bd9Sstevel@tonic-gate 	}
68647c478bd9Sstevel@tonic-gate 
68657c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
68667c478bd9Sstevel@tonic-gate }
68677c478bd9Sstevel@tonic-gate 
68687c478bd9Sstevel@tonic-gate void *
68697c478bd9Sstevel@tonic-gate mdi_client_get_vhci_private(dev_info_t *dip)
68707c478bd9Sstevel@tonic-gate {
68717c478bd9Sstevel@tonic-gate 	ASSERT(mdi_component_is_client(dip, NULL) == MDI_SUCCESS);
68727c478bd9Sstevel@tonic-gate 	if (mdi_component_is_client(dip, NULL) == MDI_SUCCESS) {
68737c478bd9Sstevel@tonic-gate 		mdi_client_t	*ct;
68747c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(dip);
68757c478bd9Sstevel@tonic-gate 		return (ct->ct_vprivate);
68767c478bd9Sstevel@tonic-gate 	}
68777c478bd9Sstevel@tonic-gate 	return (NULL);
68787c478bd9Sstevel@tonic-gate }
68797c478bd9Sstevel@tonic-gate 
68807c478bd9Sstevel@tonic-gate void
68817c478bd9Sstevel@tonic-gate mdi_client_set_vhci_private(dev_info_t *dip, void *data)
68827c478bd9Sstevel@tonic-gate {
68837c478bd9Sstevel@tonic-gate 	ASSERT(mdi_component_is_client(dip, NULL) == MDI_SUCCESS);
68847c478bd9Sstevel@tonic-gate 	if (mdi_component_is_client(dip, NULL) == MDI_SUCCESS) {
68857c478bd9Sstevel@tonic-gate 		mdi_client_t	*ct;
68867c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(dip);
68877c478bd9Sstevel@tonic-gate 		ct->ct_vprivate = data;
68887c478bd9Sstevel@tonic-gate 	}
68897c478bd9Sstevel@tonic-gate }
68907c478bd9Sstevel@tonic-gate /*
68917c478bd9Sstevel@tonic-gate  * mdi_pi_get_vhci_private():
68927c478bd9Sstevel@tonic-gate  *		Get the vhci private information associated with the
68937c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node
68947c478bd9Sstevel@tonic-gate  */
68957c478bd9Sstevel@tonic-gate void *
68967c478bd9Sstevel@tonic-gate mdi_pi_get_vhci_private(mdi_pathinfo_t *pip)
68977c478bd9Sstevel@tonic-gate {
68987c478bd9Sstevel@tonic-gate 	caddr_t	vprivate = NULL;
68997c478bd9Sstevel@tonic-gate 	if (pip) {
69007c478bd9Sstevel@tonic-gate 		vprivate = MDI_PI(pip)->pi_vprivate;
69017c478bd9Sstevel@tonic-gate 	}
69027c478bd9Sstevel@tonic-gate 	return (vprivate);
69037c478bd9Sstevel@tonic-gate }
69047c478bd9Sstevel@tonic-gate 
69057c478bd9Sstevel@tonic-gate /*
69067c478bd9Sstevel@tonic-gate  * mdi_pi_set_vhci_private():
69077c478bd9Sstevel@tonic-gate  *		Set the vhci private information in the mdi_pathinfo node
69087c478bd9Sstevel@tonic-gate  */
69097c478bd9Sstevel@tonic-gate void
69107c478bd9Sstevel@tonic-gate mdi_pi_set_vhci_private(mdi_pathinfo_t *pip, void *priv)
69117c478bd9Sstevel@tonic-gate {
69127c478bd9Sstevel@tonic-gate 	if (pip) {
69137c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_vprivate = priv;
69147c478bd9Sstevel@tonic-gate 	}
69157c478bd9Sstevel@tonic-gate }
69167c478bd9Sstevel@tonic-gate 
69177c478bd9Sstevel@tonic-gate /*
69187c478bd9Sstevel@tonic-gate  * mdi_phci_get_vhci_private():
69197c478bd9Sstevel@tonic-gate  *		Get the vhci private information associated with the
69207c478bd9Sstevel@tonic-gate  *		mdi_phci node
69217c478bd9Sstevel@tonic-gate  */
69227c478bd9Sstevel@tonic-gate void *
69237c478bd9Sstevel@tonic-gate mdi_phci_get_vhci_private(dev_info_t *dip)
69247c478bd9Sstevel@tonic-gate {
69257c478bd9Sstevel@tonic-gate 	ASSERT(mdi_component_is_phci(dip, NULL) == MDI_SUCCESS);
69267c478bd9Sstevel@tonic-gate 	if (mdi_component_is_phci(dip, NULL) == MDI_SUCCESS) {
69277c478bd9Sstevel@tonic-gate 		mdi_phci_t	*ph;
69287c478bd9Sstevel@tonic-gate 		ph = i_devi_get_phci(dip);
69297c478bd9Sstevel@tonic-gate 		return (ph->ph_vprivate);
69307c478bd9Sstevel@tonic-gate 	}
69317c478bd9Sstevel@tonic-gate 	return (NULL);
69327c478bd9Sstevel@tonic-gate }
69337c478bd9Sstevel@tonic-gate 
69347c478bd9Sstevel@tonic-gate /*
69357c478bd9Sstevel@tonic-gate  * mdi_phci_set_vhci_private():
69367c478bd9Sstevel@tonic-gate  *		Set the vhci private information in the mdi_phci node
69377c478bd9Sstevel@tonic-gate  */
69387c478bd9Sstevel@tonic-gate void
69397c478bd9Sstevel@tonic-gate mdi_phci_set_vhci_private(dev_info_t *dip, void *priv)
69407c478bd9Sstevel@tonic-gate {
69417c478bd9Sstevel@tonic-gate 	ASSERT(mdi_component_is_phci(dip, NULL) == MDI_SUCCESS);
69427c478bd9Sstevel@tonic-gate 	if (mdi_component_is_phci(dip, NULL) == MDI_SUCCESS) {
69437c478bd9Sstevel@tonic-gate 		mdi_phci_t	*ph;
69447c478bd9Sstevel@tonic-gate 		ph = i_devi_get_phci(dip);
69457c478bd9Sstevel@tonic-gate 		ph->ph_vprivate = priv;
69467c478bd9Sstevel@tonic-gate 	}
69477c478bd9Sstevel@tonic-gate }
69483c34adc5Sramat 
69493c34adc5Sramat /*
69503c34adc5Sramat  * List of vhci class names:
69513c34adc5Sramat  * A vhci class name must be in this list only if the corresponding vhci
69523c34adc5Sramat  * driver intends to use the mdi provided bus config implementation
69533c34adc5Sramat  * (i.e., mdi_vhci_bus_config()).
69543c34adc5Sramat  */
69553c34adc5Sramat static char *vhci_class_list[] = { MDI_HCI_CLASS_SCSI, MDI_HCI_CLASS_IB };
69563c34adc5Sramat #define	N_VHCI_CLASSES	(sizeof (vhci_class_list) / sizeof (char *))
69573c34adc5Sramat 
69583c34adc5Sramat /*
69593c34adc5Sramat  * During boot time, the on-disk vhci cache for every vhci class is read
69603c34adc5Sramat  * in the form of an nvlist and stored here.
69613c34adc5Sramat  */
69623c34adc5Sramat static nvlist_t *vhcache_nvl[N_VHCI_CLASSES];
69633c34adc5Sramat 
69643c34adc5Sramat /* nvpair names in vhci cache nvlist */
69653c34adc5Sramat #define	MDI_VHCI_CACHE_VERSION	1
69663c34adc5Sramat #define	MDI_NVPNAME_VERSION	"version"
69673c34adc5Sramat #define	MDI_NVPNAME_PHCIS	"phcis"
69683c34adc5Sramat #define	MDI_NVPNAME_CTADDRMAP	"clientaddrmap"
69693c34adc5Sramat 
69703c34adc5Sramat /*
69713c34adc5Sramat  * Given vhci class name, return its on-disk vhci cache filename.
69723c34adc5Sramat  * Memory for the returned filename which includes the full path is allocated
69733c34adc5Sramat  * by this function.
69743c34adc5Sramat  */
69753c34adc5Sramat static char *
69763c34adc5Sramat vhclass2vhcache_filename(char *vhclass)
69773c34adc5Sramat {
69783c34adc5Sramat 	char *filename;
69793c34adc5Sramat 	int len;
69803c34adc5Sramat 	static char *fmt = "/etc/devices/mdi_%s_cache";
69813c34adc5Sramat 
69823c34adc5Sramat 	/*
69833c34adc5Sramat 	 * fmt contains the on-disk vhci cache file name format;
69843c34adc5Sramat 	 * for scsi_vhci the filename is "/etc/devices/mdi_scsi_vhci_cache".
69853c34adc5Sramat 	 */
69863c34adc5Sramat 
69873c34adc5Sramat 	/* the -1 below is to account for "%s" in the format string */
69883c34adc5Sramat 	len = strlen(fmt) + strlen(vhclass) - 1;
69893c34adc5Sramat 	filename = kmem_alloc(len, KM_SLEEP);
69903c34adc5Sramat 	(void) snprintf(filename, len, fmt, vhclass);
69913c34adc5Sramat 	ASSERT(len == (strlen(filename) + 1));
69923c34adc5Sramat 	return (filename);
69933c34adc5Sramat }
69943c34adc5Sramat 
69953c34adc5Sramat /*
69963c34adc5Sramat  * initialize the vhci cache related data structures and read the on-disk
69973c34adc5Sramat  * vhci cached data into memory.
69983c34adc5Sramat  */
69993c34adc5Sramat static void
70003c34adc5Sramat setup_vhci_cache(mdi_vhci_t *vh)
70013c34adc5Sramat {
70023c34adc5Sramat 	mdi_vhci_config_t *vhc;
70033c34adc5Sramat 	mdi_vhci_cache_t *vhcache;
70043c34adc5Sramat 	int i;
70053c34adc5Sramat 	nvlist_t *nvl = NULL;
70063c34adc5Sramat 
70073c34adc5Sramat 	vhc = kmem_zalloc(sizeof (mdi_vhci_config_t), KM_SLEEP);
70083c34adc5Sramat 	vh->vh_config = vhc;
70093c34adc5Sramat 	vhcache = &vhc->vhc_vhcache;
70103c34adc5Sramat 
70113c34adc5Sramat 	vhc->vhc_vhcache_filename = vhclass2vhcache_filename(vh->vh_class);
70123c34adc5Sramat 
70133c34adc5Sramat 	mutex_init(&vhc->vhc_lock, NULL, MUTEX_DEFAULT, NULL);
70143c34adc5Sramat 	cv_init(&vhc->vhc_cv, NULL, CV_DRIVER, NULL);
70153c34adc5Sramat 
70163c34adc5Sramat 	rw_init(&vhcache->vhcache_lock, NULL, RW_DRIVER, NULL);
70173c34adc5Sramat 
70183c34adc5Sramat 	/*
70193c34adc5Sramat 	 * Create string hash; same as mod_hash_create_strhash() except that
70203c34adc5Sramat 	 * we use NULL key destructor.
70213c34adc5Sramat 	 */
70223c34adc5Sramat 	vhcache->vhcache_client_hash = mod_hash_create_extended(vh->vh_class,
70233c34adc5Sramat 	    mdi_bus_config_cache_hash_size,
70243c34adc5Sramat 	    mod_hash_null_keydtor, mod_hash_null_valdtor,
70253c34adc5Sramat 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
70263c34adc5Sramat 
70273c34adc5Sramat 	/*
70283c34adc5Sramat 	 * The on-disk vhci cache is read during booting prior to the
70293c34adc5Sramat 	 * lights-out period by mdi_read_devices_files().
70303c34adc5Sramat 	 */
70313c34adc5Sramat 	for (i = 0; i < N_VHCI_CLASSES; i++) {
70323c34adc5Sramat 		if (strcmp(vhci_class_list[i], vh->vh_class) == 0) {
70333c34adc5Sramat 			nvl = vhcache_nvl[i];
70343c34adc5Sramat 			vhcache_nvl[i] = NULL;
70353c34adc5Sramat 			break;
70363c34adc5Sramat 		}
70373c34adc5Sramat 	}
70383c34adc5Sramat 
70393c34adc5Sramat 	/*
70403c34adc5Sramat 	 * this is to cover the case of some one manually causing unloading
70413c34adc5Sramat 	 * (or detaching) and reloading (or attaching) of a vhci driver.
70423c34adc5Sramat 	 */
70433c34adc5Sramat 	if (nvl == NULL && modrootloaded)
70443c34adc5Sramat 		nvl = read_on_disk_vhci_cache(vh->vh_class);
70453c34adc5Sramat 
70463c34adc5Sramat 	if (nvl != NULL) {
70473c34adc5Sramat 		rw_enter(&vhcache->vhcache_lock, RW_WRITER);
70483c34adc5Sramat 		if (mainnvl_to_vhcache(vhcache, nvl) == MDI_SUCCESS)
70493c34adc5Sramat 			vhcache->vhcache_flags |= MDI_VHCI_CACHE_SETUP_DONE;
70503c34adc5Sramat 		else  {
70513c34adc5Sramat 			cmn_err(CE_WARN,
70523c34adc5Sramat 			    "%s: data file corrupted, will recreate\n",
70533c34adc5Sramat 			    vhc->vhc_vhcache_filename);
70543c34adc5Sramat 		}
70553c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
70563c34adc5Sramat 		nvlist_free(nvl);
70573c34adc5Sramat 	}
70583c34adc5Sramat 
70593c34adc5Sramat 	vhc->vhc_cbid = callb_add(stop_vhcache_flush_thread, vhc,
70603c34adc5Sramat 	    CB_CL_UADMIN_PRE_VFS, "mdi_vhcache_flush");
706167e56d35Sramat 
706267e56d35Sramat 	vhc->vhc_path_discovery_boot = mdi_path_discovery_boot;
706367e56d35Sramat 	vhc->vhc_path_discovery_postboot = mdi_path_discovery_postboot;
70643c34adc5Sramat }
70653c34adc5Sramat 
70663c34adc5Sramat /*
70673c34adc5Sramat  * free all vhci cache related resources
70683c34adc5Sramat  */
70693c34adc5Sramat static int
70703c34adc5Sramat destroy_vhci_cache(mdi_vhci_t *vh)
70713c34adc5Sramat {
70723c34adc5Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
70733c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
70743c34adc5Sramat 	mdi_vhcache_phci_t *cphci, *cphci_next;
70753c34adc5Sramat 	mdi_vhcache_client_t *cct, *cct_next;
70763c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi, *cpi_next;
70773c34adc5Sramat 
70783c34adc5Sramat 	if (stop_vhcache_async_threads(vhc) != MDI_SUCCESS)
70793c34adc5Sramat 		return (MDI_FAILURE);
70803c34adc5Sramat 
70813c34adc5Sramat 	kmem_free(vhc->vhc_vhcache_filename,
70823c34adc5Sramat 	    strlen(vhc->vhc_vhcache_filename) + 1);
70833c34adc5Sramat 
70843c34adc5Sramat 	mod_hash_destroy_strhash(vhcache->vhcache_client_hash);
70853c34adc5Sramat 
70863c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
70873c34adc5Sramat 	    cphci = cphci_next) {
70883c34adc5Sramat 		cphci_next = cphci->cphci_next;
70893c34adc5Sramat 		free_vhcache_phci(cphci);
70903c34adc5Sramat 	}
70913c34adc5Sramat 
70923c34adc5Sramat 	for (cct = vhcache->vhcache_client_head; cct != NULL; cct = cct_next) {
70933c34adc5Sramat 		cct_next = cct->cct_next;
70943c34adc5Sramat 		for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi_next) {
70953c34adc5Sramat 			cpi_next = cpi->cpi_next;
70963c34adc5Sramat 			free_vhcache_pathinfo(cpi);
70973c34adc5Sramat 		}
70983c34adc5Sramat 		free_vhcache_client(cct);
70993c34adc5Sramat 	}
71003c34adc5Sramat 
71013c34adc5Sramat 	rw_destroy(&vhcache->vhcache_lock);
71023c34adc5Sramat 
71033c34adc5Sramat 	mutex_destroy(&vhc->vhc_lock);
71043c34adc5Sramat 	cv_destroy(&vhc->vhc_cv);
71053c34adc5Sramat 	kmem_free(vhc, sizeof (mdi_vhci_config_t));
71063c34adc5Sramat 	return (MDI_SUCCESS);
71073c34adc5Sramat }
71083c34adc5Sramat 
71093c34adc5Sramat /*
71103c34adc5Sramat  * Stop all vhci cache related async threads and free their resources.
71113c34adc5Sramat  */
71123c34adc5Sramat static int
71133c34adc5Sramat stop_vhcache_async_threads(mdi_vhci_config_t *vhc)
71143c34adc5Sramat {
71153c34adc5Sramat 	mdi_async_client_config_t *acc, *acc_next;
71163c34adc5Sramat 
71173c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
71183c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_EXIT;
71193c34adc5Sramat 	ASSERT(vhc->vhc_acc_thrcount >= 0);
71203c34adc5Sramat 	cv_broadcast(&vhc->vhc_cv);
71213c34adc5Sramat 
71223c34adc5Sramat 	while ((vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) ||
71233c34adc5Sramat 	    vhc->vhc_acc_thrcount != 0) {
71243c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
71253c34adc5Sramat 		delay(1);
71263c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
71273c34adc5Sramat 	}
71283c34adc5Sramat 
71293c34adc5Sramat 	vhc->vhc_flags &= ~MDI_VHC_EXIT;
71303c34adc5Sramat 
71313c34adc5Sramat 	for (acc = vhc->vhc_acc_list_head; acc != NULL; acc = acc_next) {
71323c34adc5Sramat 		acc_next = acc->acc_next;
71333c34adc5Sramat 		free_async_client_config(acc);
71343c34adc5Sramat 	}
71353c34adc5Sramat 	vhc->vhc_acc_list_head = NULL;
71363c34adc5Sramat 	vhc->vhc_acc_list_tail = NULL;
71373c34adc5Sramat 	vhc->vhc_acc_count = 0;
71383c34adc5Sramat 
71393c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) {
71403c34adc5Sramat 		vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
71413c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
71423c34adc5Sramat 		if (flush_vhcache(vhc, 0) != MDI_SUCCESS) {
71433c34adc5Sramat 			vhcache_dirty(vhc);
71443c34adc5Sramat 			return (MDI_FAILURE);
71453c34adc5Sramat 		}
71463c34adc5Sramat 	} else
71473c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
71483c34adc5Sramat 
71493c34adc5Sramat 	if (callb_delete(vhc->vhc_cbid) != 0)
71503c34adc5Sramat 		return (MDI_FAILURE);
71513c34adc5Sramat 
71523c34adc5Sramat 	return (MDI_SUCCESS);
71533c34adc5Sramat }
71543c34adc5Sramat 
71553c34adc5Sramat /*
71563c34adc5Sramat  * Stop vhci cache flush thread
71573c34adc5Sramat  */
71583c34adc5Sramat /* ARGSUSED */
71593c34adc5Sramat static boolean_t
71603c34adc5Sramat stop_vhcache_flush_thread(void *arg, int code)
71613c34adc5Sramat {
71623c34adc5Sramat 	mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
71633c34adc5Sramat 
71643c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
71653c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_EXIT;
71663c34adc5Sramat 	cv_broadcast(&vhc->vhc_cv);
71673c34adc5Sramat 
71683c34adc5Sramat 	while (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) {
71693c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
71703c34adc5Sramat 		delay(1);
71713c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
71723c34adc5Sramat 	}
71733c34adc5Sramat 
71743c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) {
71753c34adc5Sramat 		vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
71763c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
71773c34adc5Sramat 		(void) flush_vhcache(vhc, 1);
71783c34adc5Sramat 	} else
71793c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
71803c34adc5Sramat 
71813c34adc5Sramat 	return (B_TRUE);
71823c34adc5Sramat }
71833c34adc5Sramat 
71843c34adc5Sramat /*
71853c34adc5Sramat  * Enqueue the vhcache phci (cphci) at the tail of the list
71863c34adc5Sramat  */
71873c34adc5Sramat static void
71883c34adc5Sramat enqueue_vhcache_phci(mdi_vhci_cache_t *vhcache, mdi_vhcache_phci_t *cphci)
71893c34adc5Sramat {
71903c34adc5Sramat 	cphci->cphci_next = NULL;
71913c34adc5Sramat 	if (vhcache->vhcache_phci_head == NULL)
71923c34adc5Sramat 		vhcache->vhcache_phci_head = cphci;
71933c34adc5Sramat 	else
71943c34adc5Sramat 		vhcache->vhcache_phci_tail->cphci_next = cphci;
71953c34adc5Sramat 	vhcache->vhcache_phci_tail = cphci;
71963c34adc5Sramat }
71973c34adc5Sramat 
71983c34adc5Sramat /*
71993c34adc5Sramat  * Enqueue the vhcache pathinfo (cpi) at the tail of the list
72003c34adc5Sramat  */
72013c34adc5Sramat static void
72023c34adc5Sramat enqueue_tail_vhcache_pathinfo(mdi_vhcache_client_t *cct,
72033c34adc5Sramat     mdi_vhcache_pathinfo_t *cpi)
72043c34adc5Sramat {
72053c34adc5Sramat 	cpi->cpi_next = NULL;
72063c34adc5Sramat 	if (cct->cct_cpi_head == NULL)
72073c34adc5Sramat 		cct->cct_cpi_head = cpi;
72083c34adc5Sramat 	else
72093c34adc5Sramat 		cct->cct_cpi_tail->cpi_next = cpi;
72103c34adc5Sramat 	cct->cct_cpi_tail = cpi;
72113c34adc5Sramat }
72123c34adc5Sramat 
72133c34adc5Sramat /*
72143c34adc5Sramat  * Enqueue the vhcache pathinfo (cpi) at the correct location in the
72153c34adc5Sramat  * ordered list. All cpis which do not have MDI_CPI_HINT_PATH_DOES_NOT_EXIST
72163c34adc5Sramat  * flag set come at the beginning of the list. All cpis which have this
72173c34adc5Sramat  * flag set come at the end of the list.
72183c34adc5Sramat  */
72193c34adc5Sramat static void
72203c34adc5Sramat enqueue_vhcache_pathinfo(mdi_vhcache_client_t *cct,
72213c34adc5Sramat     mdi_vhcache_pathinfo_t *newcpi)
72223c34adc5Sramat {
72233c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi, *prev_cpi;
72243c34adc5Sramat 
72253c34adc5Sramat 	if (cct->cct_cpi_head == NULL ||
72263c34adc5Sramat 	    (newcpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST))
72273c34adc5Sramat 		enqueue_tail_vhcache_pathinfo(cct, newcpi);
72283c34adc5Sramat 	else {
72293c34adc5Sramat 		for (cpi = cct->cct_cpi_head, prev_cpi = NULL; cpi != NULL &&
72303c34adc5Sramat 		    !(cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST);
72313c34adc5Sramat 		    prev_cpi = cpi, cpi = cpi->cpi_next)
72323c34adc5Sramat 			;
72333c34adc5Sramat 
72343c34adc5Sramat 		if (prev_cpi == NULL)
72353c34adc5Sramat 			cct->cct_cpi_head = newcpi;
72363c34adc5Sramat 		else
72373c34adc5Sramat 			prev_cpi->cpi_next = newcpi;
72383c34adc5Sramat 
72393c34adc5Sramat 		newcpi->cpi_next = cpi;
72403c34adc5Sramat 
72413c34adc5Sramat 		if (cpi == NULL)
72423c34adc5Sramat 			cct->cct_cpi_tail = newcpi;
72433c34adc5Sramat 	}
72443c34adc5Sramat }
72453c34adc5Sramat 
72463c34adc5Sramat /*
72473c34adc5Sramat  * Enqueue the vhcache client (cct) at the tail of the list
72483c34adc5Sramat  */
72493c34adc5Sramat static void
72503c34adc5Sramat enqueue_vhcache_client(mdi_vhci_cache_t *vhcache,
72513c34adc5Sramat     mdi_vhcache_client_t *cct)
72523c34adc5Sramat {
72533c34adc5Sramat 	cct->cct_next = NULL;
72543c34adc5Sramat 	if (vhcache->vhcache_client_head == NULL)
72553c34adc5Sramat 		vhcache->vhcache_client_head = cct;
72563c34adc5Sramat 	else
72573c34adc5Sramat 		vhcache->vhcache_client_tail->cct_next = cct;
72583c34adc5Sramat 	vhcache->vhcache_client_tail = cct;
72593c34adc5Sramat }
72603c34adc5Sramat 
72613c34adc5Sramat static void
72623c34adc5Sramat free_string_array(char **str, int nelem)
72633c34adc5Sramat {
72643c34adc5Sramat 	int i;
72653c34adc5Sramat 
72663c34adc5Sramat 	if (str) {
72673c34adc5Sramat 		for (i = 0; i < nelem; i++) {
72683c34adc5Sramat 			if (str[i])
72693c34adc5Sramat 				kmem_free(str[i], strlen(str[i]) + 1);
72703c34adc5Sramat 		}
72713c34adc5Sramat 		kmem_free(str, sizeof (char *) * nelem);
72723c34adc5Sramat 	}
72733c34adc5Sramat }
72743c34adc5Sramat 
72753c34adc5Sramat static void
72763c34adc5Sramat free_vhcache_phci(mdi_vhcache_phci_t *cphci)
72773c34adc5Sramat {
72783c34adc5Sramat 	kmem_free(cphci->cphci_path, strlen(cphci->cphci_path) + 1);
72793c34adc5Sramat 	kmem_free(cphci, sizeof (*cphci));
72803c34adc5Sramat }
72813c34adc5Sramat 
72823c34adc5Sramat static void
72833c34adc5Sramat free_vhcache_pathinfo(mdi_vhcache_pathinfo_t *cpi)
72843c34adc5Sramat {
72853c34adc5Sramat 	kmem_free(cpi->cpi_addr, strlen(cpi->cpi_addr) + 1);
72863c34adc5Sramat 	kmem_free(cpi, sizeof (*cpi));
72873c34adc5Sramat }
72883c34adc5Sramat 
72893c34adc5Sramat static void
72903c34adc5Sramat free_vhcache_client(mdi_vhcache_client_t *cct)
72913c34adc5Sramat {
72923c34adc5Sramat 	kmem_free(cct->cct_name_addr, strlen(cct->cct_name_addr) + 1);
72933c34adc5Sramat 	kmem_free(cct, sizeof (*cct));
72943c34adc5Sramat }
72953c34adc5Sramat 
72963c34adc5Sramat static char *
72973c34adc5Sramat vhcache_mknameaddr(char *ct_name, char *ct_addr, int *ret_len)
72983c34adc5Sramat {
72993c34adc5Sramat 	char *name_addr;
73003c34adc5Sramat 	int len;
73013c34adc5Sramat 
73023c34adc5Sramat 	len = strlen(ct_name) + strlen(ct_addr) + 2;
73033c34adc5Sramat 	name_addr = kmem_alloc(len, KM_SLEEP);
73043c34adc5Sramat 	(void) snprintf(name_addr, len, "%s@%s", ct_name, ct_addr);
73053c34adc5Sramat 
73063c34adc5Sramat 	if (ret_len)
73073c34adc5Sramat 		*ret_len = len;
73083c34adc5Sramat 	return (name_addr);
73093c34adc5Sramat }
73103c34adc5Sramat 
73113c34adc5Sramat /*
73123c34adc5Sramat  * Copy the contents of paddrnvl to vhci cache.
73133c34adc5Sramat  * paddrnvl nvlist contains path information for a vhci client.
73143c34adc5Sramat  * See the comment in mainnvl_to_vhcache() for the format of this nvlist.
73153c34adc5Sramat  */
73163c34adc5Sramat static void
73173c34adc5Sramat paddrnvl_to_vhcache(nvlist_t *nvl, mdi_vhcache_phci_t *cphci_list[],
73183c34adc5Sramat     mdi_vhcache_client_t *cct)
73193c34adc5Sramat {
73203c34adc5Sramat 	nvpair_t *nvp = NULL;
73213c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
73223c34adc5Sramat 	uint_t nelem;
73233c34adc5Sramat 	uint32_t *val;
73243c34adc5Sramat 
73253c34adc5Sramat 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
73263c34adc5Sramat 		ASSERT(nvpair_type(nvp) == DATA_TYPE_UINT32_ARRAY);
73273c34adc5Sramat 		cpi = kmem_zalloc(sizeof (*cpi), KM_SLEEP);
73283c34adc5Sramat 		cpi->cpi_addr = i_ddi_strdup(nvpair_name(nvp), KM_SLEEP);
73293c34adc5Sramat 		(void) nvpair_value_uint32_array(nvp, &val, &nelem);
73303c34adc5Sramat 		ASSERT(nelem == 2);
73313c34adc5Sramat 		cpi->cpi_cphci = cphci_list[val[0]];
73323c34adc5Sramat 		cpi->cpi_flags = val[1];
73333c34adc5Sramat 		enqueue_tail_vhcache_pathinfo(cct, cpi);
73343c34adc5Sramat 	}
73353c34adc5Sramat }
73363c34adc5Sramat 
73373c34adc5Sramat /*
73383c34adc5Sramat  * Copy the contents of caddrmapnvl to vhci cache.
73393c34adc5Sramat  * caddrmapnvl nvlist contains vhci client address to phci client address
73403c34adc5Sramat  * mappings. See the comment in mainnvl_to_vhcache() for the format of
73413c34adc5Sramat  * this nvlist.
73423c34adc5Sramat  */
73433c34adc5Sramat static void
73443c34adc5Sramat caddrmapnvl_to_vhcache(mdi_vhci_cache_t *vhcache, nvlist_t *nvl,
73453c34adc5Sramat     mdi_vhcache_phci_t *cphci_list[])
73463c34adc5Sramat {
73473c34adc5Sramat 	nvpair_t *nvp = NULL;
73483c34adc5Sramat 	nvlist_t *paddrnvl;
73493c34adc5Sramat 	mdi_vhcache_client_t *cct;
73503c34adc5Sramat 
73513c34adc5Sramat 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
73523c34adc5Sramat 		ASSERT(nvpair_type(nvp) == DATA_TYPE_NVLIST);
73533c34adc5Sramat 		cct = kmem_zalloc(sizeof (*cct), KM_SLEEP);
73543c34adc5Sramat 		cct->cct_name_addr = i_ddi_strdup(nvpair_name(nvp), KM_SLEEP);
73553c34adc5Sramat 		(void) nvpair_value_nvlist(nvp, &paddrnvl);
73563c34adc5Sramat 		paddrnvl_to_vhcache(paddrnvl, cphci_list, cct);
73573c34adc5Sramat 		/* the client must contain at least one path */
73583c34adc5Sramat 		ASSERT(cct->cct_cpi_head != NULL);
73593c34adc5Sramat 
73603c34adc5Sramat 		enqueue_vhcache_client(vhcache, cct);
73613c34adc5Sramat 		(void) mod_hash_insert(vhcache->vhcache_client_hash,
73623c34adc5Sramat 		    (mod_hash_key_t)cct->cct_name_addr, (mod_hash_val_t)cct);
73633c34adc5Sramat 	}
73643c34adc5Sramat }
73653c34adc5Sramat 
73663c34adc5Sramat /*
73673c34adc5Sramat  * Copy the contents of the main nvlist to vhci cache.
73683c34adc5Sramat  *
73693c34adc5Sramat  * VHCI busconfig cached data is stored in the form of a nvlist on the disk.
73703c34adc5Sramat  * The nvlist contains the mappings between the vhci client addresses and
73713c34adc5Sramat  * their corresponding phci client addresses.
73723c34adc5Sramat  *
73733c34adc5Sramat  * The structure of the nvlist is as follows:
73743c34adc5Sramat  *
73753c34adc5Sramat  * Main nvlist:
73763c34adc5Sramat  *	NAME		TYPE		DATA
73773c34adc5Sramat  *	version		int32		version number
73783c34adc5Sramat  *	phcis		string array	array of phci paths
73793c34adc5Sramat  *	clientaddrmap	nvlist_t	c2paddrs_nvl (see below)
73803c34adc5Sramat  *
73813c34adc5Sramat  * structure of c2paddrs_nvl:
73823c34adc5Sramat  *	NAME		TYPE		DATA
73833c34adc5Sramat  *	caddr1		nvlist_t	paddrs_nvl1
73843c34adc5Sramat  *	caddr2		nvlist_t	paddrs_nvl2
73853c34adc5Sramat  *	...
73863c34adc5Sramat  * where caddr1, caddr2, ... are vhci client name and addresses in the
73873c34adc5Sramat  * form of "<clientname>@<clientaddress>".
73883c34adc5Sramat  * (for example: "ssd@2000002037cd9f72");
73893c34adc5Sramat  * paddrs_nvl1, paddrs_nvl2, .. are nvlists that contain path information.
73903c34adc5Sramat  *
73913c34adc5Sramat  * structure of paddrs_nvl:
73923c34adc5Sramat  *	NAME		TYPE		DATA
73933c34adc5Sramat  *	pi_addr1	uint32_array	(phci-id, cpi_flags)
73943c34adc5Sramat  *	pi_addr2	uint32_array	(phci-id, cpi_flags)
73953c34adc5Sramat  *	...
73963c34adc5Sramat  * where pi_addr1, pi_addr2, ... are bus specific addresses of pathinfo nodes
73973c34adc5Sramat  * (so called pi_addrs, for example: "w2100002037cd9f72,0");
73983c34adc5Sramat  * phci-ids are integers that identify PHCIs to which the
73993c34adc5Sramat  * the bus specific address belongs to. These integers are used as an index
74003c34adc5Sramat  * into to the phcis string array in the main nvlist to get the PHCI path.
74013c34adc5Sramat  */
74023c34adc5Sramat static int
74033c34adc5Sramat mainnvl_to_vhcache(mdi_vhci_cache_t *vhcache, nvlist_t *nvl)
74043c34adc5Sramat {
74053c34adc5Sramat 	char **phcis, **phci_namep;
74063c34adc5Sramat 	uint_t nphcis;
74073c34adc5Sramat 	mdi_vhcache_phci_t *cphci, **cphci_list;
74083c34adc5Sramat 	nvlist_t *caddrmapnvl;
74093c34adc5Sramat 	int32_t ver;
74103c34adc5Sramat 	int i;
74113c34adc5Sramat 	size_t cphci_list_size;
74123c34adc5Sramat 
74133c34adc5Sramat 	ASSERT(RW_WRITE_HELD(&vhcache->vhcache_lock));
74143c34adc5Sramat 
74153c34adc5Sramat 	if (nvlist_lookup_int32(nvl, MDI_NVPNAME_VERSION, &ver) != 0 ||
74163c34adc5Sramat 	    ver != MDI_VHCI_CACHE_VERSION)
74173c34adc5Sramat 		return (MDI_FAILURE);
74183c34adc5Sramat 
74193c34adc5Sramat 	if (nvlist_lookup_string_array(nvl, MDI_NVPNAME_PHCIS, &phcis,
74203c34adc5Sramat 	    &nphcis) != 0)
74213c34adc5Sramat 		return (MDI_SUCCESS);
74223c34adc5Sramat 
74233c34adc5Sramat 	ASSERT(nphcis > 0);
74243c34adc5Sramat 
74253c34adc5Sramat 	cphci_list_size = sizeof (mdi_vhcache_phci_t *) * nphcis;
74263c34adc5Sramat 	cphci_list = kmem_alloc(cphci_list_size, KM_SLEEP);
74273c34adc5Sramat 	for (i = 0, phci_namep = phcis; i < nphcis; i++, phci_namep++) {
74283c34adc5Sramat 		cphci = kmem_zalloc(sizeof (mdi_vhcache_phci_t), KM_SLEEP);
74293c34adc5Sramat 		cphci->cphci_path = i_ddi_strdup(*phci_namep, KM_SLEEP);
74303c34adc5Sramat 		enqueue_vhcache_phci(vhcache, cphci);
74313c34adc5Sramat 		cphci_list[i] = cphci;
74323c34adc5Sramat 	}
74333c34adc5Sramat 
74343c34adc5Sramat 	ASSERT(vhcache->vhcache_phci_head != NULL);
74353c34adc5Sramat 
74363c34adc5Sramat 	if (nvlist_lookup_nvlist(nvl, MDI_NVPNAME_CTADDRMAP, &caddrmapnvl) == 0)
74373c34adc5Sramat 		caddrmapnvl_to_vhcache(vhcache, caddrmapnvl, cphci_list);
74383c34adc5Sramat 
74393c34adc5Sramat 	kmem_free(cphci_list, cphci_list_size);
74403c34adc5Sramat 	return (MDI_SUCCESS);
74413c34adc5Sramat }
74423c34adc5Sramat 
74433c34adc5Sramat /*
74443c34adc5Sramat  * Build paddrnvl for the specified client using the information in the
74453c34adc5Sramat  * vhci cache and add it to the caddrmapnnvl.
74463c34adc5Sramat  * Returns 0 on success, errno on failure.
74473c34adc5Sramat  */
74483c34adc5Sramat static int
74493c34adc5Sramat vhcache_to_paddrnvl(mdi_vhci_cache_t *vhcache, mdi_vhcache_client_t *cct,
74503c34adc5Sramat     nvlist_t *caddrmapnvl)
74513c34adc5Sramat {
74523c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
74533c34adc5Sramat 	nvlist_t *nvl;
74543c34adc5Sramat 	int err;
74553c34adc5Sramat 	uint32_t val[2];
74563c34adc5Sramat 
74573c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
74583c34adc5Sramat 
74593c34adc5Sramat 	if ((err = nvlist_alloc(&nvl, 0, KM_SLEEP)) != 0)
74603c34adc5Sramat 		return (err);
74613c34adc5Sramat 
74623c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
74633c34adc5Sramat 		val[0] = cpi->cpi_cphci->cphci_id;
74643c34adc5Sramat 		val[1] = cpi->cpi_flags;
74653c34adc5Sramat 		if ((err = nvlist_add_uint32_array(nvl, cpi->cpi_addr, val, 2))
74663c34adc5Sramat 		    != 0)
74673c34adc5Sramat 			goto out;
74683c34adc5Sramat 	}
74693c34adc5Sramat 
74703c34adc5Sramat 	err = nvlist_add_nvlist(caddrmapnvl, cct->cct_name_addr, nvl);
74713c34adc5Sramat out:
74723c34adc5Sramat 	nvlist_free(nvl);
74733c34adc5Sramat 	return (err);
74743c34adc5Sramat }
74753c34adc5Sramat 
74763c34adc5Sramat /*
74773c34adc5Sramat  * Build caddrmapnvl using the information in the vhci cache
74783c34adc5Sramat  * and add it to the mainnvl.
74793c34adc5Sramat  * Returns 0 on success, errno on failure.
74803c34adc5Sramat  */
74813c34adc5Sramat static int
74823c34adc5Sramat vhcache_to_caddrmapnvl(mdi_vhci_cache_t *vhcache, nvlist_t *mainnvl)
74833c34adc5Sramat {
74843c34adc5Sramat 	mdi_vhcache_client_t *cct;
74853c34adc5Sramat 	nvlist_t *nvl;
74863c34adc5Sramat 	int err;
74873c34adc5Sramat 
74883c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
74893c34adc5Sramat 
74903c34adc5Sramat 	if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) != 0)
74913c34adc5Sramat 		return (err);
74923c34adc5Sramat 
74933c34adc5Sramat 	for (cct = vhcache->vhcache_client_head; cct != NULL;
74943c34adc5Sramat 	    cct = cct->cct_next) {
74953c34adc5Sramat 		if ((err = vhcache_to_paddrnvl(vhcache, cct, nvl)) != 0)
74963c34adc5Sramat 			goto out;
74973c34adc5Sramat 	}
74983c34adc5Sramat 
74993c34adc5Sramat 	err = nvlist_add_nvlist(mainnvl, MDI_NVPNAME_CTADDRMAP, nvl);
75003c34adc5Sramat out:
75013c34adc5Sramat 	nvlist_free(nvl);
75023c34adc5Sramat 	return (err);
75033c34adc5Sramat }
75043c34adc5Sramat 
75053c34adc5Sramat /*
75063c34adc5Sramat  * Build nvlist using the information in the vhci cache.
75073c34adc5Sramat  * See the comment in mainnvl_to_vhcache() for the format of the nvlist.
75083c34adc5Sramat  * Returns nvl on success, NULL on failure.
75093c34adc5Sramat  */
75103c34adc5Sramat static nvlist_t *
75113c34adc5Sramat vhcache_to_mainnvl(mdi_vhci_cache_t *vhcache)
75123c34adc5Sramat {
75133c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
75143c34adc5Sramat 	uint_t phci_count;
75153c34adc5Sramat 	char **phcis;
75163c34adc5Sramat 	nvlist_t *nvl;
75173c34adc5Sramat 	int err, i;
75183c34adc5Sramat 
75193c34adc5Sramat 	if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) != 0) {
75203c34adc5Sramat 		nvl = NULL;
75213c34adc5Sramat 		goto out;
75223c34adc5Sramat 	}
75233c34adc5Sramat 
75243c34adc5Sramat 	if ((err = nvlist_add_int32(nvl, MDI_NVPNAME_VERSION,
75253c34adc5Sramat 	    MDI_VHCI_CACHE_VERSION)) != 0)
75263c34adc5Sramat 		goto out;
75273c34adc5Sramat 
75283c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
75293c34adc5Sramat 	if (vhcache->vhcache_phci_head == NULL) {
75303c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
75313c34adc5Sramat 		return (nvl);
75323c34adc5Sramat 	}
75333c34adc5Sramat 
75343c34adc5Sramat 	phci_count = 0;
75353c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
75363c34adc5Sramat 	    cphci = cphci->cphci_next)
75373c34adc5Sramat 		cphci->cphci_id = phci_count++;
75383c34adc5Sramat 
75393c34adc5Sramat 	/* build phci pathname list */
75403c34adc5Sramat 	phcis = kmem_alloc(sizeof (char *) * phci_count, KM_SLEEP);
75413c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head, i = 0; cphci != NULL;
75423c34adc5Sramat 	    cphci = cphci->cphci_next, i++)
75433c34adc5Sramat 		phcis[i] = i_ddi_strdup(cphci->cphci_path, KM_SLEEP);
75443c34adc5Sramat 
75453c34adc5Sramat 	err = nvlist_add_string_array(nvl, MDI_NVPNAME_PHCIS, phcis,
75463c34adc5Sramat 	    phci_count);
75473c34adc5Sramat 	free_string_array(phcis, phci_count);
75483c34adc5Sramat 
75493c34adc5Sramat 	if (err == 0 &&
75503c34adc5Sramat 	    (err = vhcache_to_caddrmapnvl(vhcache, nvl)) == 0) {
75513c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
75523c34adc5Sramat 		return (nvl);
75533c34adc5Sramat 	}
75543c34adc5Sramat 
75553c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
75563c34adc5Sramat out:
75573c34adc5Sramat 	if (nvl)
75583c34adc5Sramat 		nvlist_free(nvl);
75593c34adc5Sramat 	return (NULL);
75603c34adc5Sramat }
75613c34adc5Sramat 
75623c34adc5Sramat /*
75633c34adc5Sramat  * Lookup vhcache phci structure for the specified phci path.
75643c34adc5Sramat  */
75653c34adc5Sramat static mdi_vhcache_phci_t *
75663c34adc5Sramat lookup_vhcache_phci_by_name(mdi_vhci_cache_t *vhcache, char *phci_path)
75673c34adc5Sramat {
75683c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
75693c34adc5Sramat 
75703c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
75713c34adc5Sramat 
75723c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
75733c34adc5Sramat 	    cphci = cphci->cphci_next) {
75743c34adc5Sramat 		if (strcmp(cphci->cphci_path, phci_path) == 0)
75753c34adc5Sramat 			return (cphci);
75763c34adc5Sramat 	}
75773c34adc5Sramat 
75783c34adc5Sramat 	return (NULL);
75793c34adc5Sramat }
75803c34adc5Sramat 
75813c34adc5Sramat /*
75823c34adc5Sramat  * Lookup vhcache phci structure for the specified phci.
75833c34adc5Sramat  */
75843c34adc5Sramat static mdi_vhcache_phci_t *
75853c34adc5Sramat lookup_vhcache_phci_by_addr(mdi_vhci_cache_t *vhcache, mdi_phci_t *ph)
75863c34adc5Sramat {
75873c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
75883c34adc5Sramat 
75893c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
75903c34adc5Sramat 
75913c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
75923c34adc5Sramat 	    cphci = cphci->cphci_next) {
75933c34adc5Sramat 		if (cphci->cphci_phci == ph)
75943c34adc5Sramat 			return (cphci);
75953c34adc5Sramat 	}
75963c34adc5Sramat 
75973c34adc5Sramat 	return (NULL);
75983c34adc5Sramat }
75993c34adc5Sramat 
76003c34adc5Sramat /*
76013c34adc5Sramat  * Add the specified phci to the vhci cache if not already present.
76023c34adc5Sramat  */
76033c34adc5Sramat static void
76043c34adc5Sramat vhcache_phci_add(mdi_vhci_config_t *vhc, mdi_phci_t *ph)
76053c34adc5Sramat {
76063c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
76073c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
76083c34adc5Sramat 	char *pathname;
76093c34adc5Sramat 	int cache_updated;
76103c34adc5Sramat 
76113c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
76123c34adc5Sramat 
76133c34adc5Sramat 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
76143c34adc5Sramat 	(void) ddi_pathname(ph->ph_dip, pathname);
76153c34adc5Sramat 	if ((cphci = lookup_vhcache_phci_by_name(vhcache, pathname))
76163c34adc5Sramat 	    != NULL) {
76173c34adc5Sramat 		cphci->cphci_phci = ph;
76183c34adc5Sramat 		cache_updated = 0;
76193c34adc5Sramat 	} else {
76203c34adc5Sramat 		cphci = kmem_zalloc(sizeof (*cphci), KM_SLEEP);
76213c34adc5Sramat 		cphci->cphci_path = i_ddi_strdup(pathname, KM_SLEEP);
76223c34adc5Sramat 		cphci->cphci_phci = ph;
76233c34adc5Sramat 		enqueue_vhcache_phci(vhcache, cphci);
76243c34adc5Sramat 		cache_updated = 1;
76253c34adc5Sramat 	}
762667e56d35Sramat 
76273c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
76283c34adc5Sramat 
762967e56d35Sramat 	/*
763067e56d35Sramat 	 * Since a new phci has been added, reset
763167e56d35Sramat 	 * vhc_path_discovery_cutoff_time to allow for discovery of paths
763267e56d35Sramat 	 * during next vhcache_discover_paths().
763367e56d35Sramat 	 */
763467e56d35Sramat 	mutex_enter(&vhc->vhc_lock);
763567e56d35Sramat 	vhc->vhc_path_discovery_cutoff_time = 0;
763667e56d35Sramat 	mutex_exit(&vhc->vhc_lock);
763767e56d35Sramat 
76383c34adc5Sramat 	kmem_free(pathname, MAXPATHLEN);
76393c34adc5Sramat 	if (cache_updated)
76403c34adc5Sramat 		vhcache_dirty(vhc);
76413c34adc5Sramat }
76423c34adc5Sramat 
76433c34adc5Sramat /*
76443c34adc5Sramat  * Remove the reference to the specified phci from the vhci cache.
76453c34adc5Sramat  */
76463c34adc5Sramat static void
76473c34adc5Sramat vhcache_phci_remove(mdi_vhci_config_t *vhc, mdi_phci_t *ph)
76483c34adc5Sramat {
76493c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
76503c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
76513c34adc5Sramat 
76523c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
76533c34adc5Sramat 	if ((cphci = lookup_vhcache_phci_by_addr(vhcache, ph)) != NULL) {
76543c34adc5Sramat 		/* do not remove the actual mdi_vhcache_phci structure */
76553c34adc5Sramat 		cphci->cphci_phci = NULL;
76563c34adc5Sramat 	}
76573c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
76583c34adc5Sramat }
76593c34adc5Sramat 
76603c34adc5Sramat static void
76613c34adc5Sramat init_vhcache_lookup_token(mdi_vhcache_lookup_token_t *dst,
76623c34adc5Sramat     mdi_vhcache_lookup_token_t *src)
76633c34adc5Sramat {
76643c34adc5Sramat 	if (src == NULL) {
76653c34adc5Sramat 		dst->lt_cct = NULL;
76663c34adc5Sramat 		dst->lt_cct_lookup_time = 0;
76673c34adc5Sramat 	} else {
76683c34adc5Sramat 		dst->lt_cct = src->lt_cct;
76693c34adc5Sramat 		dst->lt_cct_lookup_time = src->lt_cct_lookup_time;
76703c34adc5Sramat 	}
76713c34adc5Sramat }
76723c34adc5Sramat 
76733c34adc5Sramat /*
76743c34adc5Sramat  * Look up vhcache client for the specified client.
76753c34adc5Sramat  */
76763c34adc5Sramat static mdi_vhcache_client_t *
76773c34adc5Sramat lookup_vhcache_client(mdi_vhci_cache_t *vhcache, char *ct_name, char *ct_addr,
76783c34adc5Sramat     mdi_vhcache_lookup_token_t *token)
76793c34adc5Sramat {
76803c34adc5Sramat 	mod_hash_val_t hv;
76813c34adc5Sramat 	char *name_addr;
76823c34adc5Sramat 	int len;
76833c34adc5Sramat 
76843c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
76853c34adc5Sramat 
76863c34adc5Sramat 	/*
76873c34adc5Sramat 	 * If no vhcache clean occurred since the last lookup, we can
76883c34adc5Sramat 	 * simply return the cct from the last lookup operation.
76893c34adc5Sramat 	 * It works because ccts are never freed except during the vhcache
76903c34adc5Sramat 	 * cleanup operation.
76913c34adc5Sramat 	 */
76923c34adc5Sramat 	if (token != NULL &&
76933c34adc5Sramat 	    vhcache->vhcache_clean_time < token->lt_cct_lookup_time)
76943c34adc5Sramat 		return (token->lt_cct);
76953c34adc5Sramat 
76963c34adc5Sramat 	name_addr = vhcache_mknameaddr(ct_name, ct_addr, &len);
76973c34adc5Sramat 	if (mod_hash_find(vhcache->vhcache_client_hash,
76983c34adc5Sramat 	    (mod_hash_key_t)name_addr, &hv) == 0) {
76993c34adc5Sramat 		if (token) {
77003c34adc5Sramat 			token->lt_cct = (mdi_vhcache_client_t *)hv;
77013c34adc5Sramat 			token->lt_cct_lookup_time = lbolt64;
77023c34adc5Sramat 		}
77033c34adc5Sramat 	} else {
77043c34adc5Sramat 		if (token) {
77053c34adc5Sramat 			token->lt_cct = NULL;
77063c34adc5Sramat 			token->lt_cct_lookup_time = 0;
77073c34adc5Sramat 		}
77083c34adc5Sramat 		hv = NULL;
77093c34adc5Sramat 	}
77103c34adc5Sramat 	kmem_free(name_addr, len);
77113c34adc5Sramat 	return ((mdi_vhcache_client_t *)hv);
77123c34adc5Sramat }
77133c34adc5Sramat 
77143c34adc5Sramat /*
77153c34adc5Sramat  * Add the specified path to the vhci cache if not already present.
77163c34adc5Sramat  * Also add the vhcache client for the client corresponding to this path
77173c34adc5Sramat  * if it doesn't already exist.
77183c34adc5Sramat  */
77193c34adc5Sramat static void
77203c34adc5Sramat vhcache_pi_add(mdi_vhci_config_t *vhc, struct mdi_pathinfo *pip)
77213c34adc5Sramat {
77223c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
77233c34adc5Sramat 	mdi_vhcache_client_t *cct;
77243c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
77253c34adc5Sramat 	mdi_phci_t *ph = pip->pi_phci;
77263c34adc5Sramat 	mdi_client_t *ct = pip->pi_client;
77273c34adc5Sramat 	int cache_updated = 0;
77283c34adc5Sramat 
77293c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
77303c34adc5Sramat 
77313c34adc5Sramat 	/* if vhcache client for this pip doesn't already exist, add it */
77323c34adc5Sramat 	if ((cct = lookup_vhcache_client(vhcache, ct->ct_drvname, ct->ct_guid,
77333c34adc5Sramat 	    NULL)) == NULL) {
77343c34adc5Sramat 		cct = kmem_zalloc(sizeof (*cct), KM_SLEEP);
77353c34adc5Sramat 		cct->cct_name_addr = vhcache_mknameaddr(ct->ct_drvname,
77363c34adc5Sramat 		    ct->ct_guid, NULL);
77373c34adc5Sramat 		enqueue_vhcache_client(vhcache, cct);
77383c34adc5Sramat 		(void) mod_hash_insert(vhcache->vhcache_client_hash,
77393c34adc5Sramat 		    (mod_hash_key_t)cct->cct_name_addr, (mod_hash_val_t)cct);
77403c34adc5Sramat 		cache_updated = 1;
77413c34adc5Sramat 	}
77423c34adc5Sramat 
77433c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
77443c34adc5Sramat 		if (cpi->cpi_cphci->cphci_phci == ph &&
77453c34adc5Sramat 		    strcmp(cpi->cpi_addr, pip->pi_addr) == 0) {
77463c34adc5Sramat 			cpi->cpi_pip = pip;
77473c34adc5Sramat 			if (cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST) {
77483c34adc5Sramat 				cpi->cpi_flags &=
77493c34adc5Sramat 				    ~MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
77503c34adc5Sramat 				sort_vhcache_paths(cct);
77513c34adc5Sramat 				cache_updated = 1;
77523c34adc5Sramat 			}
77533c34adc5Sramat 			break;
77543c34adc5Sramat 		}
77553c34adc5Sramat 	}
77563c34adc5Sramat 
77573c34adc5Sramat 	if (cpi == NULL) {
77583c34adc5Sramat 		cpi = kmem_zalloc(sizeof (*cpi), KM_SLEEP);
77593c34adc5Sramat 		cpi->cpi_addr = i_ddi_strdup(pip->pi_addr, KM_SLEEP);
77603c34adc5Sramat 		cpi->cpi_cphci = lookup_vhcache_phci_by_addr(vhcache, ph);
77613c34adc5Sramat 		ASSERT(cpi->cpi_cphci != NULL);
77623c34adc5Sramat 		cpi->cpi_pip = pip;
77633c34adc5Sramat 		enqueue_vhcache_pathinfo(cct, cpi);
77643c34adc5Sramat 		cache_updated = 1;
77653c34adc5Sramat 	}
77663c34adc5Sramat 
77673c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
77683c34adc5Sramat 
77693c34adc5Sramat 	if (cache_updated)
77703c34adc5Sramat 		vhcache_dirty(vhc);
77713c34adc5Sramat }
77723c34adc5Sramat 
77733c34adc5Sramat /*
77743c34adc5Sramat  * Remove the reference to the specified path from the vhci cache.
77753c34adc5Sramat  */
77763c34adc5Sramat static void
77773c34adc5Sramat vhcache_pi_remove(mdi_vhci_config_t *vhc, struct mdi_pathinfo *pip)
77783c34adc5Sramat {
77793c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
77803c34adc5Sramat 	mdi_client_t *ct = pip->pi_client;
77813c34adc5Sramat 	mdi_vhcache_client_t *cct;
77823c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
77833c34adc5Sramat 
77843c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
77853c34adc5Sramat 	if ((cct = lookup_vhcache_client(vhcache, ct->ct_drvname, ct->ct_guid,
77863c34adc5Sramat 	    NULL)) != NULL) {
77873c34adc5Sramat 		for (cpi = cct->cct_cpi_head; cpi != NULL;
77883c34adc5Sramat 		    cpi = cpi->cpi_next) {
77893c34adc5Sramat 			if (cpi->cpi_pip == pip) {
77903c34adc5Sramat 				cpi->cpi_pip = NULL;
77913c34adc5Sramat 				break;
77923c34adc5Sramat 			}
77933c34adc5Sramat 		}
77943c34adc5Sramat 	}
77953c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
77963c34adc5Sramat }
77973c34adc5Sramat 
77983c34adc5Sramat /*
77993c34adc5Sramat  * Flush the vhci cache to disk.
78003c34adc5Sramat  * Returns MDI_SUCCESS on success, MDI_FAILURE on failure.
78013c34adc5Sramat  */
78023c34adc5Sramat static int
78033c34adc5Sramat flush_vhcache(mdi_vhci_config_t *vhc, int force_flag)
78043c34adc5Sramat {
78053c34adc5Sramat 	nvlist_t *nvl;
78063c34adc5Sramat 	int err;
78073c34adc5Sramat 	int rv;
78083c34adc5Sramat 
78093c34adc5Sramat 	/*
78103c34adc5Sramat 	 * It is possible that the system may shutdown before
78113c34adc5Sramat 	 * i_ddi_io_initialized (during stmsboot for example). To allow for
78123c34adc5Sramat 	 * flushing the cache in this case do not check for
78133c34adc5Sramat 	 * i_ddi_io_initialized when force flag is set.
78143c34adc5Sramat 	 */
78153c34adc5Sramat 	if (force_flag == 0 && !i_ddi_io_initialized())
78163c34adc5Sramat 		return (MDI_FAILURE);
78173c34adc5Sramat 
78183c34adc5Sramat 	if ((nvl = vhcache_to_mainnvl(&vhc->vhc_vhcache)) != NULL) {
78193c34adc5Sramat 		err = fwrite_nvlist(vhc->vhc_vhcache_filename, nvl);
78203c34adc5Sramat 		nvlist_free(nvl);
78213c34adc5Sramat 	} else
78223c34adc5Sramat 		err = EFAULT;
78233c34adc5Sramat 
78243c34adc5Sramat 	rv = MDI_SUCCESS;
78253c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
78263c34adc5Sramat 	if (err != 0) {
78273c34adc5Sramat 		if (err == EROFS) {
78283c34adc5Sramat 			vhc->vhc_flags |= MDI_VHC_READONLY_FS;
78293c34adc5Sramat 			vhc->vhc_flags &= ~(MDI_VHC_VHCACHE_FLUSH_ERROR |
78303c34adc5Sramat 			    MDI_VHC_VHCACHE_DIRTY);
78313c34adc5Sramat 		} else {
78323c34adc5Sramat 			if (!(vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_ERROR)) {
78333c34adc5Sramat 				cmn_err(CE_CONT, "%s: update failed\n",
78343c34adc5Sramat 				    vhc->vhc_vhcache_filename);
78353c34adc5Sramat 				vhc->vhc_flags |= MDI_VHC_VHCACHE_FLUSH_ERROR;
78363c34adc5Sramat 			}
78373c34adc5Sramat 			rv = MDI_FAILURE;
78383c34adc5Sramat 		}
78393c34adc5Sramat 	} else if (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_ERROR) {
78403c34adc5Sramat 		cmn_err(CE_CONT,
78413c34adc5Sramat 		    "%s: update now ok\n", vhc->vhc_vhcache_filename);
78423c34adc5Sramat 		vhc->vhc_flags &= ~MDI_VHC_VHCACHE_FLUSH_ERROR;
78433c34adc5Sramat 	}
78443c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
78453c34adc5Sramat 
78463c34adc5Sramat 	return (rv);
78473c34adc5Sramat }
78483c34adc5Sramat 
78493c34adc5Sramat /*
78503c34adc5Sramat  * Call flush_vhcache() to flush the vhci cache at the scheduled time.
78513c34adc5Sramat  * Exits itself if left idle for the idle timeout period.
78523c34adc5Sramat  */
78533c34adc5Sramat static void
78543c34adc5Sramat vhcache_flush_thread(void *arg)
78553c34adc5Sramat {
78563c34adc5Sramat 	mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
78573c34adc5Sramat 	clock_t idle_time, quit_at_ticks;
78583c34adc5Sramat 	callb_cpr_t cprinfo;
78593c34adc5Sramat 
78603c34adc5Sramat 	/* number of seconds to sleep idle before exiting */
78613c34adc5Sramat 	idle_time = mdi_vhcache_flush_daemon_idle_time * TICKS_PER_SECOND;
78623c34adc5Sramat 
78633c34adc5Sramat 	CALLB_CPR_INIT(&cprinfo, &vhc->vhc_lock, callb_generic_cpr,
78643c34adc5Sramat 	    "mdi_vhcache_flush");
78653c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
78663c34adc5Sramat 	for (; ; ) {
78673c34adc5Sramat 		while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
78683c34adc5Sramat 		    (vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY)) {
78693c34adc5Sramat 			if (ddi_get_lbolt() < vhc->vhc_flush_at_ticks) {
78703c34adc5Sramat 				CALLB_CPR_SAFE_BEGIN(&cprinfo);
78713c34adc5Sramat 				(void) cv_timedwait(&vhc->vhc_cv,
78723c34adc5Sramat 				    &vhc->vhc_lock, vhc->vhc_flush_at_ticks);
78733c34adc5Sramat 				CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
78743c34adc5Sramat 			} else {
78753c34adc5Sramat 				vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
78763c34adc5Sramat 				mutex_exit(&vhc->vhc_lock);
78773c34adc5Sramat 
78783c34adc5Sramat 				if (flush_vhcache(vhc, 0) != MDI_SUCCESS)
78793c34adc5Sramat 					vhcache_dirty(vhc);
78803c34adc5Sramat 
78813c34adc5Sramat 				mutex_enter(&vhc->vhc_lock);
78823c34adc5Sramat 			}
78833c34adc5Sramat 		}
78843c34adc5Sramat 
78853c34adc5Sramat 		quit_at_ticks = ddi_get_lbolt() + idle_time;
78863c34adc5Sramat 
78873c34adc5Sramat 		while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
78883c34adc5Sramat 		    !(vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) &&
78893c34adc5Sramat 		    ddi_get_lbolt() < quit_at_ticks) {
78903c34adc5Sramat 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
78913c34adc5Sramat 			(void) cv_timedwait(&vhc->vhc_cv, &vhc->vhc_lock,
78923c34adc5Sramat 			    quit_at_ticks);
78933c34adc5Sramat 			CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
78943c34adc5Sramat 		}
78953c34adc5Sramat 
78963c34adc5Sramat 		if ((vhc->vhc_flags & MDI_VHC_EXIT) ||
78973c34adc5Sramat 		    !(vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY))
78983c34adc5Sramat 			goto out;
78993c34adc5Sramat 	}
79003c34adc5Sramat 
79013c34adc5Sramat out:
79023c34adc5Sramat 	vhc->vhc_flags &= ~MDI_VHC_VHCACHE_FLUSH_THREAD;
79033c34adc5Sramat 	/* CALLB_CPR_EXIT releases the vhc->vhc_lock */
79043c34adc5Sramat 	CALLB_CPR_EXIT(&cprinfo);
79053c34adc5Sramat }
79063c34adc5Sramat 
79073c34adc5Sramat /*
79083c34adc5Sramat  * Make vhci cache dirty and schedule flushing by vhcache flush thread.
79093c34adc5Sramat  */
79103c34adc5Sramat static void
79113c34adc5Sramat vhcache_dirty(mdi_vhci_config_t *vhc)
79123c34adc5Sramat {
79133c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
79143c34adc5Sramat 	int create_thread;
79153c34adc5Sramat 
79163c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
79173c34adc5Sramat 	/* do not flush cache until the cache is fully built */
79183c34adc5Sramat 	if (!(vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE)) {
79193c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
79203c34adc5Sramat 		return;
79213c34adc5Sramat 	}
79223c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
79233c34adc5Sramat 
79243c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
79253c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_READONLY_FS) {
79263c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
79273c34adc5Sramat 		return;
79283c34adc5Sramat 	}
79293c34adc5Sramat 
79303c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_VHCACHE_DIRTY;
79313c34adc5Sramat 	vhc->vhc_flush_at_ticks = ddi_get_lbolt() +
79323c34adc5Sramat 	    mdi_vhcache_flush_delay * TICKS_PER_SECOND;
79333c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) {
79343c34adc5Sramat 		cv_broadcast(&vhc->vhc_cv);
79353c34adc5Sramat 		create_thread = 0;
79363c34adc5Sramat 	} else {
79373c34adc5Sramat 		vhc->vhc_flags |= MDI_VHC_VHCACHE_FLUSH_THREAD;
79383c34adc5Sramat 		create_thread = 1;
79393c34adc5Sramat 	}
79403c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
79413c34adc5Sramat 
79423c34adc5Sramat 	if (create_thread)
79433c34adc5Sramat 		(void) thread_create(NULL, 0, vhcache_flush_thread, vhc,
79443c34adc5Sramat 		    0, &p0, TS_RUN, minclsyspri);
79453c34adc5Sramat }
79463c34adc5Sramat 
79473c34adc5Sramat /*
79483c34adc5Sramat  * phci bus config structure - one for for each phci bus config operation that
79493c34adc5Sramat  * we initiate on behalf of a vhci.
79503c34adc5Sramat  */
79513c34adc5Sramat typedef struct mdi_phci_bus_config_s {
79523c34adc5Sramat 	char *phbc_phci_path;
79533c34adc5Sramat 	struct mdi_vhci_bus_config_s *phbc_vhbusconfig;	/* vhci bus config */
79543c34adc5Sramat 	struct mdi_phci_bus_config_s *phbc_next;
79553c34adc5Sramat } mdi_phci_bus_config_t;
79563c34adc5Sramat 
79573c34adc5Sramat /* vhci bus config structure - one for each vhci bus config operation */
79583c34adc5Sramat typedef struct mdi_vhci_bus_config_s {
79593c34adc5Sramat 	ddi_bus_config_op_t vhbc_op;	/* bus config op */
79603c34adc5Sramat 	major_t vhbc_op_major;		/* bus config op major */
79613c34adc5Sramat 	uint_t vhbc_op_flags;		/* bus config op flags */
79623c34adc5Sramat 	kmutex_t vhbc_lock;
79633c34adc5Sramat 	kcondvar_t vhbc_cv;
79643c34adc5Sramat 	int vhbc_thr_count;
79653c34adc5Sramat } mdi_vhci_bus_config_t;
79663c34adc5Sramat 
79673c34adc5Sramat /*
79683c34adc5Sramat  * bus config the specified phci
79693c34adc5Sramat  */
79703c34adc5Sramat static void
79713c34adc5Sramat bus_config_phci(void *arg)
79723c34adc5Sramat {
79733c34adc5Sramat 	mdi_phci_bus_config_t *phbc = (mdi_phci_bus_config_t *)arg;
79743c34adc5Sramat 	mdi_vhci_bus_config_t *vhbc = phbc->phbc_vhbusconfig;
79753c34adc5Sramat 	dev_info_t *ph_dip;
79763c34adc5Sramat 
79773c34adc5Sramat 	/*
79783c34adc5Sramat 	 * first configure all path components upto phci and then configure
79793c34adc5Sramat 	 * the phci children.
79803c34adc5Sramat 	 */
79813c34adc5Sramat 	if ((ph_dip = e_ddi_hold_devi_by_path(phbc->phbc_phci_path, 0))
79823c34adc5Sramat 	    != NULL) {
79833c34adc5Sramat 		if (vhbc->vhbc_op == BUS_CONFIG_DRIVER ||
79843c34adc5Sramat 		    vhbc->vhbc_op == BUS_UNCONFIG_DRIVER) {
79853c34adc5Sramat 			(void) ndi_devi_config_driver(ph_dip,
79863c34adc5Sramat 			    vhbc->vhbc_op_flags,
79873c34adc5Sramat 			    vhbc->vhbc_op_major);
79883c34adc5Sramat 		} else
79893c34adc5Sramat 			(void) ndi_devi_config(ph_dip,
79903c34adc5Sramat 			    vhbc->vhbc_op_flags);
79913c34adc5Sramat 
79923c34adc5Sramat 		/* release the hold that e_ddi_hold_devi_by_path() placed */
79933c34adc5Sramat 		ndi_rele_devi(ph_dip);
79943c34adc5Sramat 	}
79953c34adc5Sramat 
79963c34adc5Sramat 	kmem_free(phbc->phbc_phci_path, strlen(phbc->phbc_phci_path) + 1);
79973c34adc5Sramat 	kmem_free(phbc, sizeof (*phbc));
79983c34adc5Sramat 
79993c34adc5Sramat 	mutex_enter(&vhbc->vhbc_lock);
80003c34adc5Sramat 	vhbc->vhbc_thr_count--;
80013c34adc5Sramat 	if (vhbc->vhbc_thr_count == 0)
80023c34adc5Sramat 		cv_broadcast(&vhbc->vhbc_cv);
80033c34adc5Sramat 	mutex_exit(&vhbc->vhbc_lock);
80043c34adc5Sramat }
80053c34adc5Sramat 
80063c34adc5Sramat /*
80073c34adc5Sramat  * Bus config all phcis associated with the vhci in parallel.
80083c34adc5Sramat  * op must be BUS_CONFIG_DRIVER or BUS_CONFIG_ALL.
80093c34adc5Sramat  */
80103c34adc5Sramat static void
80113c34adc5Sramat bus_config_all_phcis(mdi_vhci_cache_t *vhcache, uint_t flags,
80123c34adc5Sramat     ddi_bus_config_op_t op, major_t maj)
80133c34adc5Sramat {
80143c34adc5Sramat 	mdi_phci_bus_config_t *phbc_head = NULL, *phbc, *phbc_next;
80153c34adc5Sramat 	mdi_vhci_bus_config_t *vhbc;
80163c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
80173c34adc5Sramat 
80183c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
80193c34adc5Sramat 	if (vhcache->vhcache_phci_head == NULL) {
80203c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
80213c34adc5Sramat 		return;
80223c34adc5Sramat 	}
80233c34adc5Sramat 
80243c34adc5Sramat 	vhbc = kmem_zalloc(sizeof (*vhbc), KM_SLEEP);
80253c34adc5Sramat 
80263c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
80273c34adc5Sramat 	    cphci = cphci->cphci_next) {
8028273f4511Sgp87344 		/* skip phcis that haven't attached before root is available */
8029273f4511Sgp87344 		if (!modrootloaded && (cphci->cphci_phci == NULL))
8030273f4511Sgp87344 			continue;
80313c34adc5Sramat 		phbc = kmem_zalloc(sizeof (*phbc), KM_SLEEP);
80323c34adc5Sramat 		phbc->phbc_phci_path = i_ddi_strdup(cphci->cphci_path,
80333c34adc5Sramat 		    KM_SLEEP);
80343c34adc5Sramat 		phbc->phbc_vhbusconfig = vhbc;
80353c34adc5Sramat 		phbc->phbc_next = phbc_head;
80363c34adc5Sramat 		phbc_head = phbc;
80373c34adc5Sramat 		vhbc->vhbc_thr_count++;
80383c34adc5Sramat 	}
80393c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
80403c34adc5Sramat 
80413c34adc5Sramat 	vhbc->vhbc_op = op;
80423c34adc5Sramat 	vhbc->vhbc_op_major = maj;
80433c34adc5Sramat 	vhbc->vhbc_op_flags = NDI_NO_EVENT |
80443c34adc5Sramat 	    (flags & (NDI_CONFIG_REPROBE | NDI_DRV_CONF_REPROBE));
80453c34adc5Sramat 	mutex_init(&vhbc->vhbc_lock, NULL, MUTEX_DEFAULT, NULL);
80463c34adc5Sramat 	cv_init(&vhbc->vhbc_cv, NULL, CV_DRIVER, NULL);
80473c34adc5Sramat 
80483c34adc5Sramat 	/* now create threads to initiate bus config on all phcis in parallel */
80493c34adc5Sramat 	for (phbc = phbc_head; phbc != NULL; phbc = phbc_next) {
80503c34adc5Sramat 		phbc_next = phbc->phbc_next;
80513c34adc5Sramat 		if (mdi_mtc_off)
80523c34adc5Sramat 			bus_config_phci((void *)phbc);
80533c34adc5Sramat 		else
80543c34adc5Sramat 			(void) thread_create(NULL, 0, bus_config_phci, phbc,
80553c34adc5Sramat 			    0, &p0, TS_RUN, minclsyspri);
80563c34adc5Sramat 	}
80573c34adc5Sramat 
80583c34adc5Sramat 	mutex_enter(&vhbc->vhbc_lock);
80593c34adc5Sramat 	/* wait until all threads exit */
80603c34adc5Sramat 	while (vhbc->vhbc_thr_count > 0)
80613c34adc5Sramat 		cv_wait(&vhbc->vhbc_cv, &vhbc->vhbc_lock);
80623c34adc5Sramat 	mutex_exit(&vhbc->vhbc_lock);
80633c34adc5Sramat 
80643c34adc5Sramat 	mutex_destroy(&vhbc->vhbc_lock);
80653c34adc5Sramat 	cv_destroy(&vhbc->vhbc_cv);
80663c34adc5Sramat 	kmem_free(vhbc, sizeof (*vhbc));
80673c34adc5Sramat }
80683c34adc5Sramat 
80693c34adc5Sramat /*
807067e56d35Sramat  * Single threaded version of bus_config_all_phcis()
807167e56d35Sramat  */
807267e56d35Sramat static void
807367e56d35Sramat st_bus_config_all_phcis(mdi_vhci_config_t *vhc, uint_t flags,
807467e56d35Sramat     ddi_bus_config_op_t op, major_t maj)
807567e56d35Sramat {
807667e56d35Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
807767e56d35Sramat 
807867e56d35Sramat 	single_threaded_vhconfig_enter(vhc);
807967e56d35Sramat 	bus_config_all_phcis(vhcache, flags, op, maj);
808067e56d35Sramat 	single_threaded_vhconfig_exit(vhc);
808167e56d35Sramat }
808267e56d35Sramat 
808367e56d35Sramat /*
80843c34adc5Sramat  * Perform BUS_CONFIG_ONE on the specified child of the phci.
80853c34adc5Sramat  * The path includes the child component in addition to the phci path.
80863c34adc5Sramat  */
80873c34adc5Sramat static int
80883c34adc5Sramat bus_config_one_phci_child(char *path)
80893c34adc5Sramat {
80903c34adc5Sramat 	dev_info_t *ph_dip, *child;
80913c34adc5Sramat 	char *devnm;
80923c34adc5Sramat 	int rv = MDI_FAILURE;
80933c34adc5Sramat 
80943c34adc5Sramat 	/* extract the child component of the phci */
80953c34adc5Sramat 	devnm = strrchr(path, '/');
80963c34adc5Sramat 	*devnm++ = '\0';
80973c34adc5Sramat 
80983c34adc5Sramat 	/*
80993c34adc5Sramat 	 * first configure all path components upto phci and then
81003c34adc5Sramat 	 * configure the phci child.
81013c34adc5Sramat 	 */
81023c34adc5Sramat 	if ((ph_dip = e_ddi_hold_devi_by_path(path, 0)) != NULL) {
81033c34adc5Sramat 		if (ndi_devi_config_one(ph_dip, devnm, &child, NDI_NO_EVENT) ==
81043c34adc5Sramat 		    NDI_SUCCESS) {
81053c34adc5Sramat 			/*
81063c34adc5Sramat 			 * release the hold that ndi_devi_config_one() placed
81073c34adc5Sramat 			 */
81083c34adc5Sramat 			ndi_rele_devi(child);
81093c34adc5Sramat 			rv = MDI_SUCCESS;
81103c34adc5Sramat 		}
81113c34adc5Sramat 
81123c34adc5Sramat 		/* release the hold that e_ddi_hold_devi_by_path() placed */
81133c34adc5Sramat 		ndi_rele_devi(ph_dip);
81143c34adc5Sramat 	}
81153c34adc5Sramat 
81163c34adc5Sramat 	devnm--;
81173c34adc5Sramat 	*devnm = '/';
81183c34adc5Sramat 	return (rv);
81193c34adc5Sramat }
81203c34adc5Sramat 
81213c34adc5Sramat /*
81223c34adc5Sramat  * Build a list of phci client paths for the specified vhci client.
81233c34adc5Sramat  * The list includes only those phci client paths which aren't configured yet.
81243c34adc5Sramat  */
81253c34adc5Sramat static mdi_phys_path_t *
81263c34adc5Sramat build_phclient_path_list(mdi_vhcache_client_t *cct, char *ct_name)
81273c34adc5Sramat {
81283c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
81293c34adc5Sramat 	mdi_phys_path_t *pp_head = NULL, *pp_tail = NULL, *pp;
81303c34adc5Sramat 	int config_path, len;
81313c34adc5Sramat 
81323c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
81333c34adc5Sramat 		/*
81343c34adc5Sramat 		 * include only those paths that aren't configured.
81353c34adc5Sramat 		 */
81363c34adc5Sramat 		config_path = 0;
81373c34adc5Sramat 		if (cpi->cpi_pip == NULL)
81383c34adc5Sramat 			config_path = 1;
81393c34adc5Sramat 		else {
81403c34adc5Sramat 			MDI_PI_LOCK(cpi->cpi_pip);
81413c34adc5Sramat 			if (MDI_PI_IS_INIT(cpi->cpi_pip))
81423c34adc5Sramat 				config_path = 1;
81433c34adc5Sramat 			MDI_PI_UNLOCK(cpi->cpi_pip);
81443c34adc5Sramat 		}
81453c34adc5Sramat 
81463c34adc5Sramat 		if (config_path) {
81473c34adc5Sramat 			pp = kmem_alloc(sizeof (*pp), KM_SLEEP);
81483c34adc5Sramat 			len = strlen(cpi->cpi_cphci->cphci_path) +
81493c34adc5Sramat 			    strlen(ct_name) + strlen(cpi->cpi_addr) + 3;
81503c34adc5Sramat 			pp->phys_path = kmem_alloc(len, KM_SLEEP);
81513c34adc5Sramat 			(void) snprintf(pp->phys_path, len, "%s/%s@%s",
81523c34adc5Sramat 			    cpi->cpi_cphci->cphci_path, ct_name,
81533c34adc5Sramat 			    cpi->cpi_addr);
81543c34adc5Sramat 			pp->phys_path_next = NULL;
81553c34adc5Sramat 
81563c34adc5Sramat 			if (pp_head == NULL)
81573c34adc5Sramat 				pp_head = pp;
81583c34adc5Sramat 			else
81593c34adc5Sramat 				pp_tail->phys_path_next = pp;
81603c34adc5Sramat 			pp_tail = pp;
81613c34adc5Sramat 		}
81623c34adc5Sramat 	}
81633c34adc5Sramat 
81643c34adc5Sramat 	return (pp_head);
81653c34adc5Sramat }
81663c34adc5Sramat 
81673c34adc5Sramat /*
81683c34adc5Sramat  * Free the memory allocated for phci client path list.
81693c34adc5Sramat  */
81703c34adc5Sramat static void
81713c34adc5Sramat free_phclient_path_list(mdi_phys_path_t *pp_head)
81723c34adc5Sramat {
81733c34adc5Sramat 	mdi_phys_path_t *pp, *pp_next;
81743c34adc5Sramat 
81753c34adc5Sramat 	for (pp = pp_head; pp != NULL; pp = pp_next) {
81763c34adc5Sramat 		pp_next = pp->phys_path_next;
81773c34adc5Sramat 		kmem_free(pp->phys_path, strlen(pp->phys_path) + 1);
81783c34adc5Sramat 		kmem_free(pp, sizeof (*pp));
81793c34adc5Sramat 	}
81803c34adc5Sramat }
81813c34adc5Sramat 
81823c34adc5Sramat /*
81833c34adc5Sramat  * Allocated async client structure and initialize with the specified values.
81843c34adc5Sramat  */
81853c34adc5Sramat static mdi_async_client_config_t *
81863c34adc5Sramat alloc_async_client_config(char *ct_name, char *ct_addr,
81873c34adc5Sramat     mdi_phys_path_t *pp_head, mdi_vhcache_lookup_token_t *tok)
81883c34adc5Sramat {
81893c34adc5Sramat 	mdi_async_client_config_t *acc;
81903c34adc5Sramat 
81913c34adc5Sramat 	acc = kmem_alloc(sizeof (*acc), KM_SLEEP);
81923c34adc5Sramat 	acc->acc_ct_name = i_ddi_strdup(ct_name, KM_SLEEP);
81933c34adc5Sramat 	acc->acc_ct_addr = i_ddi_strdup(ct_addr, KM_SLEEP);
81943c34adc5Sramat 	acc->acc_phclient_path_list_head = pp_head;
81953c34adc5Sramat 	init_vhcache_lookup_token(&acc->acc_token, tok);
81963c34adc5Sramat 	acc->acc_next = NULL;
81973c34adc5Sramat 	return (acc);
81983c34adc5Sramat }
81993c34adc5Sramat 
82003c34adc5Sramat /*
82013c34adc5Sramat  * Free the memory allocated for the async client structure and their members.
82023c34adc5Sramat  */
82033c34adc5Sramat static void
82043c34adc5Sramat free_async_client_config(mdi_async_client_config_t *acc)
82053c34adc5Sramat {
82063c34adc5Sramat 	if (acc->acc_phclient_path_list_head)
82073c34adc5Sramat 		free_phclient_path_list(acc->acc_phclient_path_list_head);
82083c34adc5Sramat 	kmem_free(acc->acc_ct_name, strlen(acc->acc_ct_name) + 1);
82093c34adc5Sramat 	kmem_free(acc->acc_ct_addr, strlen(acc->acc_ct_addr) + 1);
82103c34adc5Sramat 	kmem_free(acc, sizeof (*acc));
82113c34adc5Sramat }
82123c34adc5Sramat 
82133c34adc5Sramat /*
82143c34adc5Sramat  * Sort vhcache pathinfos (cpis) of the specified client.
82153c34adc5Sramat  * All cpis which do not have MDI_CPI_HINT_PATH_DOES_NOT_EXIST
82163c34adc5Sramat  * flag set come at the beginning of the list. All cpis which have this
82173c34adc5Sramat  * flag set come at the end of the list.
82183c34adc5Sramat  */
82193c34adc5Sramat static void
82203c34adc5Sramat sort_vhcache_paths(mdi_vhcache_client_t *cct)
82213c34adc5Sramat {
82223c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi, *cpi_next, *cpi_head;
82233c34adc5Sramat 
82243c34adc5Sramat 	cpi_head = cct->cct_cpi_head;
82253c34adc5Sramat 	cct->cct_cpi_head = cct->cct_cpi_tail = NULL;
82263c34adc5Sramat 	for (cpi = cpi_head; cpi != NULL; cpi = cpi_next) {
82273c34adc5Sramat 		cpi_next = cpi->cpi_next;
82283c34adc5Sramat 		enqueue_vhcache_pathinfo(cct, cpi);
82293c34adc5Sramat 	}
82303c34adc5Sramat }
82313c34adc5Sramat 
82323c34adc5Sramat /*
82333c34adc5Sramat  * Verify whether MDI_CPI_HINT_PATH_DOES_NOT_EXIST flag setting is correct for
82343c34adc5Sramat  * every vhcache pathinfo of the specified client. If not adjust the flag
82353c34adc5Sramat  * setting appropriately.
82363c34adc5Sramat  *
82373c34adc5Sramat  * Note that MDI_CPI_HINT_PATH_DOES_NOT_EXIST flag is persisted in the
82383c34adc5Sramat  * on-disk vhci cache. So every time this flag is updated the cache must be
82393c34adc5Sramat  * flushed.
82403c34adc5Sramat  */
82413c34adc5Sramat static void
82423c34adc5Sramat adjust_sort_vhcache_paths(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr,
82433c34adc5Sramat     mdi_vhcache_lookup_token_t *tok)
82443c34adc5Sramat {
82453c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
82463c34adc5Sramat 	mdi_vhcache_client_t *cct;
82473c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
82483c34adc5Sramat 
82493c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
82503c34adc5Sramat 	if ((cct = lookup_vhcache_client(vhcache, ct_name, ct_addr, tok))
82513c34adc5Sramat 	    == NULL) {
82523c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
82533c34adc5Sramat 		return;
82543c34adc5Sramat 	}
82553c34adc5Sramat 
82563c34adc5Sramat 	/*
82573c34adc5Sramat 	 * to avoid unnecessary on-disk cache updates, first check if an
82583c34adc5Sramat 	 * update is really needed. If no update is needed simply return.
82593c34adc5Sramat 	 */
82603c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
82613c34adc5Sramat 		if ((cpi->cpi_pip != NULL &&
82623c34adc5Sramat 		    (cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST)) ||
82633c34adc5Sramat 		    (cpi->cpi_pip == NULL &&
82643c34adc5Sramat 		    !(cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST))) {
82653c34adc5Sramat 			break;
82663c34adc5Sramat 		}
82673c34adc5Sramat 	}
82683c34adc5Sramat 	if (cpi == NULL) {
82693c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
82703c34adc5Sramat 		return;
82713c34adc5Sramat 	}
82723c34adc5Sramat 
82733c34adc5Sramat 	if (rw_tryupgrade(&vhcache->vhcache_lock) == 0) {
82743c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
82753c34adc5Sramat 		rw_enter(&vhcache->vhcache_lock, RW_WRITER);
82763c34adc5Sramat 		if ((cct = lookup_vhcache_client(vhcache, ct_name, ct_addr,
82773c34adc5Sramat 		    tok)) == NULL) {
82783c34adc5Sramat 			rw_exit(&vhcache->vhcache_lock);
82793c34adc5Sramat 			return;
82803c34adc5Sramat 		}
82813c34adc5Sramat 	}
82823c34adc5Sramat 
82833c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
82843c34adc5Sramat 		if (cpi->cpi_pip != NULL)
82853c34adc5Sramat 			cpi->cpi_flags &= ~MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
82863c34adc5Sramat 		else
82873c34adc5Sramat 			cpi->cpi_flags |= MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
82883c34adc5Sramat 	}
82893c34adc5Sramat 	sort_vhcache_paths(cct);
82903c34adc5Sramat 
82913c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
82923c34adc5Sramat 	vhcache_dirty(vhc);
82933c34adc5Sramat }
82943c34adc5Sramat 
82953c34adc5Sramat /*
82963c34adc5Sramat  * Configure all specified paths of the client.
82973c34adc5Sramat  */
82983c34adc5Sramat static void
82993c34adc5Sramat config_client_paths_sync(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr,
83003c34adc5Sramat     mdi_phys_path_t *pp_head, mdi_vhcache_lookup_token_t *tok)
83013c34adc5Sramat {
83023c34adc5Sramat 	mdi_phys_path_t *pp;
83033c34adc5Sramat 
83043c34adc5Sramat 	for (pp = pp_head; pp != NULL; pp = pp->phys_path_next)
83053c34adc5Sramat 		(void) bus_config_one_phci_child(pp->phys_path);
83063c34adc5Sramat 	adjust_sort_vhcache_paths(vhc, ct_name, ct_addr, tok);
83073c34adc5Sramat }
83083c34adc5Sramat 
83093c34adc5Sramat /*
83103c34adc5Sramat  * Dequeue elements from vhci async client config list and bus configure
83113c34adc5Sramat  * their corresponding phci clients.
83123c34adc5Sramat  */
83133c34adc5Sramat static void
83143c34adc5Sramat config_client_paths_thread(void *arg)
83153c34adc5Sramat {
83163c34adc5Sramat 	mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
83173c34adc5Sramat 	mdi_async_client_config_t *acc;
83183c34adc5Sramat 	clock_t quit_at_ticks;
83193c34adc5Sramat 	clock_t idle_time = mdi_async_config_idle_time * TICKS_PER_SECOND;
83203c34adc5Sramat 	callb_cpr_t cprinfo;
83213c34adc5Sramat 
83223c34adc5Sramat 	CALLB_CPR_INIT(&cprinfo, &vhc->vhc_lock, callb_generic_cpr,
83233c34adc5Sramat 	    "mdi_config_client_paths");
83243c34adc5Sramat 
83253c34adc5Sramat 	for (; ; ) {
83263c34adc5Sramat 		quit_at_ticks = ddi_get_lbolt() + idle_time;
83273c34adc5Sramat 
83283c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
83293c34adc5Sramat 		while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
83303c34adc5Sramat 		    vhc->vhc_acc_list_head == NULL &&
83313c34adc5Sramat 		    ddi_get_lbolt() < quit_at_ticks) {
83323c34adc5Sramat 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
83333c34adc5Sramat 			(void) cv_timedwait(&vhc->vhc_cv, &vhc->vhc_lock,
83343c34adc5Sramat 			    quit_at_ticks);
83353c34adc5Sramat 			CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
83363c34adc5Sramat 		}
83373c34adc5Sramat 
83383c34adc5Sramat 		if ((vhc->vhc_flags & MDI_VHC_EXIT) ||
83393c34adc5Sramat 		    vhc->vhc_acc_list_head == NULL)
83403c34adc5Sramat 			goto out;
83413c34adc5Sramat 
83423c34adc5Sramat 		acc = vhc->vhc_acc_list_head;
83433c34adc5Sramat 		vhc->vhc_acc_list_head = acc->acc_next;
83443c34adc5Sramat 		if (vhc->vhc_acc_list_head == NULL)
83453c34adc5Sramat 			vhc->vhc_acc_list_tail = NULL;
83463c34adc5Sramat 		vhc->vhc_acc_count--;
83473c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
83483c34adc5Sramat 
83493c34adc5Sramat 		config_client_paths_sync(vhc, acc->acc_ct_name,
83503c34adc5Sramat 		    acc->acc_ct_addr, acc->acc_phclient_path_list_head,
83513c34adc5Sramat 		    &acc->acc_token);
83523c34adc5Sramat 
83533c34adc5Sramat 		free_async_client_config(acc);
83543c34adc5Sramat 	}
83553c34adc5Sramat 
83563c34adc5Sramat out:
83573c34adc5Sramat 	vhc->vhc_acc_thrcount--;
83583c34adc5Sramat 	/* CALLB_CPR_EXIT releases the vhc->vhc_lock */
83593c34adc5Sramat 	CALLB_CPR_EXIT(&cprinfo);
83603c34adc5Sramat }
83613c34adc5Sramat 
83623c34adc5Sramat /*
83633c34adc5Sramat  * Arrange for all the phci client paths (pp_head) for the specified client
83643c34adc5Sramat  * to be bus configured asynchronously by a thread.
83653c34adc5Sramat  */
83663c34adc5Sramat static void
83673c34adc5Sramat config_client_paths_async(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr,
83683c34adc5Sramat     mdi_phys_path_t *pp_head, mdi_vhcache_lookup_token_t *tok)
83693c34adc5Sramat {
83703c34adc5Sramat 	mdi_async_client_config_t *acc, *newacc;
83713c34adc5Sramat 	int create_thread;
83723c34adc5Sramat 
83733c34adc5Sramat 	if (pp_head == NULL)
83743c34adc5Sramat 		return;
83753c34adc5Sramat 
83763c34adc5Sramat 	if (mdi_mtc_off) {
83773c34adc5Sramat 		config_client_paths_sync(vhc, ct_name, ct_addr, pp_head, tok);
83783c34adc5Sramat 		free_phclient_path_list(pp_head);
83793c34adc5Sramat 		return;
83803c34adc5Sramat 	}
83813c34adc5Sramat 
83823c34adc5Sramat 	newacc = alloc_async_client_config(ct_name, ct_addr, pp_head, tok);
83833c34adc5Sramat 	ASSERT(newacc);
83843c34adc5Sramat 
83853c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
83863c34adc5Sramat 	for (acc = vhc->vhc_acc_list_head; acc != NULL; acc = acc->acc_next) {
83873c34adc5Sramat 		if (strcmp(ct_name, acc->acc_ct_name) == 0 &&
83883c34adc5Sramat 		    strcmp(ct_addr, acc->acc_ct_addr) == 0) {
83893c34adc5Sramat 			free_async_client_config(newacc);
83903c34adc5Sramat 			mutex_exit(&vhc->vhc_lock);
83913c34adc5Sramat 			return;
83923c34adc5Sramat 		}
83933c34adc5Sramat 	}
83943c34adc5Sramat 
83953c34adc5Sramat 	if (vhc->vhc_acc_list_head == NULL)
83963c34adc5Sramat 		vhc->vhc_acc_list_head = newacc;
83973c34adc5Sramat 	else
83983c34adc5Sramat 		vhc->vhc_acc_list_tail->acc_next = newacc;
83993c34adc5Sramat 	vhc->vhc_acc_list_tail = newacc;
84003c34adc5Sramat 	vhc->vhc_acc_count++;
84013c34adc5Sramat 	if (vhc->vhc_acc_count <= vhc->vhc_acc_thrcount) {
84023c34adc5Sramat 		cv_broadcast(&vhc->vhc_cv);
84033c34adc5Sramat 		create_thread = 0;
84043c34adc5Sramat 	} else {
84053c34adc5Sramat 		vhc->vhc_acc_thrcount++;
84063c34adc5Sramat 		create_thread = 1;
84073c34adc5Sramat 	}
84083c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
84093c34adc5Sramat 
84103c34adc5Sramat 	if (create_thread)
84113c34adc5Sramat 		(void) thread_create(NULL, 0, config_client_paths_thread, vhc,
84123c34adc5Sramat 		    0, &p0, TS_RUN, minclsyspri);
84133c34adc5Sramat }
84143c34adc5Sramat 
84153c34adc5Sramat /*
84163c34adc5Sramat  * Return number of online paths for the specified client.
84173c34adc5Sramat  */
84183c34adc5Sramat static int
84193c34adc5Sramat nonline_paths(mdi_vhcache_client_t *cct)
84203c34adc5Sramat {
84213c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
84223c34adc5Sramat 	int online_count = 0;
84233c34adc5Sramat 
84243c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
84253c34adc5Sramat 		if (cpi->cpi_pip != NULL) {
84263c34adc5Sramat 			MDI_PI_LOCK(cpi->cpi_pip);
84273c34adc5Sramat 			if (cpi->cpi_pip->pi_state == MDI_PATHINFO_STATE_ONLINE)
84283c34adc5Sramat 				online_count++;
84293c34adc5Sramat 			MDI_PI_UNLOCK(cpi->cpi_pip);
84303c34adc5Sramat 		}
84313c34adc5Sramat 	}
84323c34adc5Sramat 
84333c34adc5Sramat 	return (online_count);
84343c34adc5Sramat }
84353c34adc5Sramat 
84363c34adc5Sramat /*
84373c34adc5Sramat  * Bus configure all paths for the specified vhci client.
84383c34adc5Sramat  * If at least one path for the client is already online, the remaining paths
84393c34adc5Sramat  * will be configured asynchronously. Otherwise, it synchronously configures
84403c34adc5Sramat  * the paths until at least one path is online and then rest of the paths
84413c34adc5Sramat  * will be configured asynchronously.
84423c34adc5Sramat  */
84433c34adc5Sramat static void
84443c34adc5Sramat config_client_paths(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr)
84453c34adc5Sramat {
84463c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
84473c34adc5Sramat 	mdi_phys_path_t *pp_head, *pp;
84483c34adc5Sramat 	mdi_vhcache_client_t *cct;
84493c34adc5Sramat 	mdi_vhcache_lookup_token_t tok;
84503c34adc5Sramat 
84513c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
84523c34adc5Sramat 
84533c34adc5Sramat 	init_vhcache_lookup_token(&tok, NULL);
84543c34adc5Sramat 
84553c34adc5Sramat 	if (ct_name == NULL || ct_addr == NULL ||
84563c34adc5Sramat 	    (cct = lookup_vhcache_client(vhcache, ct_name, ct_addr, &tok))
84573c34adc5Sramat 	    == NULL ||
84583c34adc5Sramat 	    (pp_head = build_phclient_path_list(cct, ct_name)) == NULL) {
84593c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
84603c34adc5Sramat 		return;
84613c34adc5Sramat 	}
84623c34adc5Sramat 
84633c34adc5Sramat 	/* if at least one path is online, configure the rest asynchronously */
84643c34adc5Sramat 	if (nonline_paths(cct) > 0) {
84653c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
84663c34adc5Sramat 		config_client_paths_async(vhc, ct_name, ct_addr, pp_head, &tok);
84673c34adc5Sramat 		return;
84683c34adc5Sramat 	}
84693c34adc5Sramat 
84703c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
84713c34adc5Sramat 
84723c34adc5Sramat 	for (pp = pp_head; pp != NULL; pp = pp->phys_path_next) {
84733c34adc5Sramat 		if (bus_config_one_phci_child(pp->phys_path) == MDI_SUCCESS) {
84743c34adc5Sramat 			rw_enter(&vhcache->vhcache_lock, RW_READER);
84753c34adc5Sramat 
84763c34adc5Sramat 			if ((cct = lookup_vhcache_client(vhcache, ct_name,
84773c34adc5Sramat 			    ct_addr, &tok)) == NULL) {
84783c34adc5Sramat 				rw_exit(&vhcache->vhcache_lock);
84793c34adc5Sramat 				goto out;
84803c34adc5Sramat 			}
84813c34adc5Sramat 
84823c34adc5Sramat 			if (nonline_paths(cct) > 0 &&
84833c34adc5Sramat 			    pp->phys_path_next != NULL) {
84843c34adc5Sramat 				rw_exit(&vhcache->vhcache_lock);
84853c34adc5Sramat 				config_client_paths_async(vhc, ct_name, ct_addr,
84863c34adc5Sramat 				    pp->phys_path_next, &tok);
84873c34adc5Sramat 				pp->phys_path_next = NULL;
84883c34adc5Sramat 				goto out;
84893c34adc5Sramat 			}
84903c34adc5Sramat 
84913c34adc5Sramat 			rw_exit(&vhcache->vhcache_lock);
84923c34adc5Sramat 		}
84933c34adc5Sramat 	}
84943c34adc5Sramat 
84953c34adc5Sramat 	adjust_sort_vhcache_paths(vhc, ct_name, ct_addr, &tok);
84963c34adc5Sramat out:
84973c34adc5Sramat 	free_phclient_path_list(pp_head);
84983c34adc5Sramat }
84993c34adc5Sramat 
85003c34adc5Sramat static void
85013c34adc5Sramat single_threaded_vhconfig_enter(mdi_vhci_config_t *vhc)
85023c34adc5Sramat {
85033c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
85043c34adc5Sramat 	while (vhc->vhc_flags & MDI_VHC_SINGLE_THREADED)
85053c34adc5Sramat 		cv_wait(&vhc->vhc_cv, &vhc->vhc_lock);
85063c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_SINGLE_THREADED;
85073c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
85083c34adc5Sramat }
85093c34adc5Sramat 
85103c34adc5Sramat static void
85113c34adc5Sramat single_threaded_vhconfig_exit(mdi_vhci_config_t *vhc)
85123c34adc5Sramat {
85133c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
85143c34adc5Sramat 	vhc->vhc_flags &= ~MDI_VHC_SINGLE_THREADED;
85153c34adc5Sramat 	cv_broadcast(&vhc->vhc_cv);
85163c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
85173c34adc5Sramat }
85183c34adc5Sramat 
851952cac543Sramat typedef struct mdi_phci_driver_info {
852052cac543Sramat 	char	*phdriver_name;	/* name of the phci driver */
852152cac543Sramat 
852252cac543Sramat 	/* set to non zero if the phci driver supports root device */
852352cac543Sramat 	int	phdriver_root_support;
852452cac543Sramat } mdi_phci_driver_info_t;
852552cac543Sramat 
85263c34adc5Sramat /*
852752cac543Sramat  * vhci class and root support capability of a phci driver can be
852852cac543Sramat  * specified using ddi-vhci-class and ddi-no-root-support properties in the
852952cac543Sramat  * phci driver.conf file. The built-in tables below contain this information
853052cac543Sramat  * for those phci drivers whose driver.conf files don't yet contain this info.
853152cac543Sramat  *
853252cac543Sramat  * All phci drivers expect iscsi have root device support.
853352cac543Sramat  */
853452cac543Sramat static mdi_phci_driver_info_t scsi_phci_driver_list[] = {
853552cac543Sramat 	{ "fp", 1 },
853652cac543Sramat 	{ "iscsi", 0 },
853752cac543Sramat 	{ "ibsrp", 1 }
853852cac543Sramat 	};
853952cac543Sramat 
854052cac543Sramat static mdi_phci_driver_info_t ib_phci_driver_list[] = { "tavor", 1 };
854152cac543Sramat 
854252cac543Sramat static void *
854352cac543Sramat mdi_realloc(void *old_ptr, size_t old_size, size_t new_size)
854452cac543Sramat {
854552cac543Sramat 	void *new_ptr;
854652cac543Sramat 
854752cac543Sramat 	new_ptr = kmem_zalloc(new_size, KM_SLEEP);
854852cac543Sramat 	if (old_ptr) {
8549f7209cf2Spramodbg 		bcopy(old_ptr, new_ptr, MIN(old_size, new_size));
855052cac543Sramat 		kmem_free(old_ptr, old_size);
855152cac543Sramat 	}
855252cac543Sramat 	return (new_ptr);
855352cac543Sramat }
855452cac543Sramat 
855552cac543Sramat static void
855652cac543Sramat add_to_phci_list(char ***driver_list, int **root_support_list,
855752cac543Sramat     int *cur_elements, int *max_elements, char *driver_name, int root_support)
855852cac543Sramat {
855952cac543Sramat 	ASSERT(*cur_elements <= *max_elements);
856052cac543Sramat 	if (*cur_elements == *max_elements) {
856152cac543Sramat 		*max_elements += 10;
856252cac543Sramat 		*driver_list = mdi_realloc(*driver_list,
856352cac543Sramat 		    sizeof (char *) * (*cur_elements),
856452cac543Sramat 		    sizeof (char *) * (*max_elements));
856552cac543Sramat 		*root_support_list = mdi_realloc(*root_support_list,
856652cac543Sramat 		    sizeof (int) * (*cur_elements),
856752cac543Sramat 		    sizeof (int) * (*max_elements));
856852cac543Sramat 	}
856952cac543Sramat 	(*driver_list)[*cur_elements] = i_ddi_strdup(driver_name, KM_SLEEP);
857052cac543Sramat 	(*root_support_list)[*cur_elements] = root_support;
857152cac543Sramat 	(*cur_elements)++;
857252cac543Sramat }
857352cac543Sramat 
857452cac543Sramat static void
857552cac543Sramat get_phci_driver_list(char *vhci_class, char ***driver_list,
857652cac543Sramat     int **root_support_list, int *cur_elements, int *max_elements)
857752cac543Sramat {
857852cac543Sramat 	mdi_phci_driver_info_t	*st_driver_list, *p;
857952cac543Sramat 	int		st_ndrivers, root_support, i, j, driver_conf_count;
858052cac543Sramat 	major_t		m;
858152cac543Sramat 	struct devnames	*dnp;
858252cac543Sramat 	ddi_prop_t	*propp;
858352cac543Sramat 
858452cac543Sramat 	*driver_list = NULL;
858552cac543Sramat 	*root_support_list = NULL;
858652cac543Sramat 	*cur_elements = 0;
858752cac543Sramat 	*max_elements = 0;
858852cac543Sramat 
858952cac543Sramat 	/* add the phci drivers derived from the phci driver.conf files */
859052cac543Sramat 	for (m = 0; m < devcnt; m++) {
859152cac543Sramat 		dnp = &devnamesp[m];
859252cac543Sramat 
859352cac543Sramat 		if (dnp->dn_flags & DN_PHCI_DRIVER) {
859452cac543Sramat 			LOCK_DEV_OPS(&dnp->dn_lock);
859552cac543Sramat 			if (dnp->dn_global_prop_ptr != NULL &&
859652cac543Sramat 			    (propp = i_ddi_prop_search(DDI_DEV_T_ANY,
859752cac543Sramat 			    DDI_VHCI_CLASS, DDI_PROP_TYPE_STRING,
859852cac543Sramat 			    &dnp->dn_global_prop_ptr->prop_list)) != NULL &&
859952cac543Sramat 			    strcmp(propp->prop_val, vhci_class) == 0) {
860052cac543Sramat 
860152cac543Sramat 				root_support = (i_ddi_prop_search(DDI_DEV_T_ANY,
860252cac543Sramat 				    DDI_NO_ROOT_SUPPORT, DDI_PROP_TYPE_INT,
860352cac543Sramat 				    &dnp->dn_global_prop_ptr->prop_list)
860452cac543Sramat 				    == NULL) ? 1 : 0;
860552cac543Sramat 
860652cac543Sramat 				add_to_phci_list(driver_list, root_support_list,
860752cac543Sramat 				    cur_elements, max_elements, dnp->dn_name,
860852cac543Sramat 				    root_support);
860952cac543Sramat 
861052cac543Sramat 				UNLOCK_DEV_OPS(&dnp->dn_lock);
861152cac543Sramat 			} else
861252cac543Sramat 				UNLOCK_DEV_OPS(&dnp->dn_lock);
861352cac543Sramat 		}
861452cac543Sramat 	}
861552cac543Sramat 
861652cac543Sramat 	driver_conf_count = *cur_elements;
861752cac543Sramat 
861852cac543Sramat 	/* add the phci drivers specified in the built-in tables */
861952cac543Sramat 	if (strcmp(vhci_class, MDI_HCI_CLASS_SCSI) == 0) {
862052cac543Sramat 		st_driver_list = scsi_phci_driver_list;
862152cac543Sramat 		st_ndrivers = sizeof (scsi_phci_driver_list) /
862252cac543Sramat 		    sizeof (mdi_phci_driver_info_t);
862352cac543Sramat 	} else if (strcmp(vhci_class, MDI_HCI_CLASS_IB) == 0) {
862452cac543Sramat 		st_driver_list = ib_phci_driver_list;
862552cac543Sramat 		st_ndrivers = sizeof (ib_phci_driver_list) /
862652cac543Sramat 		    sizeof (mdi_phci_driver_info_t);
862752cac543Sramat 	} else {
862852cac543Sramat 		st_driver_list = NULL;
862952cac543Sramat 		st_ndrivers = 0;
863052cac543Sramat 	}
863152cac543Sramat 
863252cac543Sramat 	for (i = 0, p = st_driver_list; i < st_ndrivers; i++, p++) {
863352cac543Sramat 		/* add this phci driver if not already added before */
863452cac543Sramat 		for (j = 0; j < driver_conf_count; j++) {
863552cac543Sramat 			if (strcmp((*driver_list)[j], p->phdriver_name) == 0)
863652cac543Sramat 				break;
863752cac543Sramat 		}
863852cac543Sramat 		if (j == driver_conf_count) {
863952cac543Sramat 			add_to_phci_list(driver_list, root_support_list,
864052cac543Sramat 			    cur_elements, max_elements, p->phdriver_name,
864152cac543Sramat 			    p->phdriver_root_support);
864252cac543Sramat 		}
864352cac543Sramat 	}
864452cac543Sramat }
864552cac543Sramat 
864652cac543Sramat /*
864752cac543Sramat  * Attach the phci driver instances associated with the specified vhci class.
86483c34adc5Sramat  * If root is mounted attach all phci driver instances.
86493c34adc5Sramat  * If root is not mounted, attach the instances of only those phci
86503c34adc5Sramat  * drivers that have the root support.
86513c34adc5Sramat  */
86523c34adc5Sramat static void
865352cac543Sramat attach_phci_drivers(char *vhci_class)
86543c34adc5Sramat {
865552cac543Sramat 	char	**driver_list, **p;
865652cac543Sramat 	int	*root_support_list;
865752cac543Sramat 	int	cur_elements, max_elements, i;
86583c34adc5Sramat 	major_t	m;
86593c34adc5Sramat 
866052cac543Sramat 	get_phci_driver_list(vhci_class, &driver_list, &root_support_list,
866152cac543Sramat 	    &cur_elements, &max_elements);
86623c34adc5Sramat 
866352cac543Sramat 	for (i = 0; i < cur_elements; i++) {
866452cac543Sramat 		if (modrootloaded || root_support_list[i]) {
866552cac543Sramat 			m = ddi_name_to_major(driver_list[i]);
8666*a204de77Scth 			if (m != DDI_MAJOR_T_NONE &&
8667*a204de77Scth 			    ddi_hold_installed_driver(m))
86683c34adc5Sramat 				ddi_rele_driver(m);
86693c34adc5Sramat 		}
86703c34adc5Sramat 	}
867152cac543Sramat 
867252cac543Sramat 	if (driver_list) {
867352cac543Sramat 		for (i = 0, p = driver_list; i < cur_elements; i++, p++)
867452cac543Sramat 			kmem_free(*p, strlen(*p) + 1);
867552cac543Sramat 		kmem_free(driver_list, sizeof (char *) * max_elements);
867652cac543Sramat 		kmem_free(root_support_list, sizeof (int) * max_elements);
867752cac543Sramat 	}
86783c34adc5Sramat }
86793c34adc5Sramat 
86803c34adc5Sramat /*
86813c34adc5Sramat  * Build vhci cache:
86823c34adc5Sramat  *
86833c34adc5Sramat  * Attach phci driver instances and then drive BUS_CONFIG_ALL on
86843c34adc5Sramat  * the phci driver instances. During this process the cache gets built.
86853c34adc5Sramat  *
868667e56d35Sramat  * Cache is built fully if the root is mounted.
86873c34adc5Sramat  * If the root is not mounted, phci drivers that do not have root support
86883c34adc5Sramat  * are not attached. As a result the cache is built partially. The entries
86893c34adc5Sramat  * in the cache reflect only those phci drivers that have root support.
86903c34adc5Sramat  */
869167e56d35Sramat static int
869252cac543Sramat build_vhci_cache(mdi_vhci_t *vh)
86933c34adc5Sramat {
869452cac543Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
86953c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
86963c34adc5Sramat 
869767e56d35Sramat 	single_threaded_vhconfig_enter(vhc);
869867e56d35Sramat 
86993c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
87003c34adc5Sramat 	if (vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE) {
87013c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
870267e56d35Sramat 		single_threaded_vhconfig_exit(vhc);
870367e56d35Sramat 		return (0);
87043c34adc5Sramat 	}
87053c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
87063c34adc5Sramat 
870752cac543Sramat 	attach_phci_drivers(vh->vh_class);
87083c34adc5Sramat 	bus_config_all_phcis(vhcache, NDI_DRV_CONF_REPROBE | NDI_NO_EVENT,
8709*a204de77Scth 	    BUS_CONFIG_ALL, DDI_MAJOR_T_NONE);
87103c34adc5Sramat 
87113c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
87123c34adc5Sramat 	vhcache->vhcache_flags |= MDI_VHCI_CACHE_SETUP_DONE;
87133c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
871467e56d35Sramat 
871567e56d35Sramat 	single_threaded_vhconfig_exit(vhc);
87163c34adc5Sramat 	vhcache_dirty(vhc);
871767e56d35Sramat 	return (1);
87183c34adc5Sramat }
87193c34adc5Sramat 
87203c34adc5Sramat /*
872167e56d35Sramat  * Determine if discovery of paths is needed.
87223c34adc5Sramat  */
87233c34adc5Sramat static int
872467e56d35Sramat vhcache_do_discovery(mdi_vhci_config_t *vhc)
87253c34adc5Sramat {
872667e56d35Sramat 	int rv = 1;
872767e56d35Sramat 
872867e56d35Sramat 	mutex_enter(&vhc->vhc_lock);
872967e56d35Sramat 	if (i_ddi_io_initialized() == 0) {
873067e56d35Sramat 		if (vhc->vhc_path_discovery_boot > 0) {
873167e56d35Sramat 			vhc->vhc_path_discovery_boot--;
873267e56d35Sramat 			goto out;
873367e56d35Sramat 		}
873467e56d35Sramat 	} else {
873567e56d35Sramat 		if (vhc->vhc_path_discovery_postboot > 0) {
873667e56d35Sramat 			vhc->vhc_path_discovery_postboot--;
873767e56d35Sramat 			goto out;
873867e56d35Sramat 		}
873967e56d35Sramat 	}
874067e56d35Sramat 
874167e56d35Sramat 	/*
874267e56d35Sramat 	 * Do full path discovery at most once per mdi_path_discovery_interval.
874367e56d35Sramat 	 * This is to avoid a series of full path discoveries when opening
874467e56d35Sramat 	 * stale /dev/[r]dsk links.
874567e56d35Sramat 	 */
874667e56d35Sramat 	if (mdi_path_discovery_interval != -1 &&
874767e56d35Sramat 	    lbolt64 >= vhc->vhc_path_discovery_cutoff_time)
874867e56d35Sramat 		goto out;
874967e56d35Sramat 
875067e56d35Sramat 	rv = 0;
875167e56d35Sramat out:
875267e56d35Sramat 	mutex_exit(&vhc->vhc_lock);
875367e56d35Sramat 	return (rv);
875467e56d35Sramat }
875567e56d35Sramat 
875667e56d35Sramat /*
875767e56d35Sramat  * Discover all paths:
875867e56d35Sramat  *
875967e56d35Sramat  * Attach phci driver instances and then drive BUS_CONFIG_ALL on all the phci
876067e56d35Sramat  * driver instances. During this process all paths will be discovered.
876167e56d35Sramat  */
876267e56d35Sramat static int
876352cac543Sramat vhcache_discover_paths(mdi_vhci_t *vh)
876467e56d35Sramat {
876552cac543Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
876667e56d35Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
876767e56d35Sramat 	int rv = 0;
87683c34adc5Sramat 
87693c34adc5Sramat 	single_threaded_vhconfig_enter(vhc);
87703c34adc5Sramat 
877167e56d35Sramat 	if (vhcache_do_discovery(vhc)) {
877252cac543Sramat 		attach_phci_drivers(vh->vh_class);
877367e56d35Sramat 		bus_config_all_phcis(vhcache, NDI_DRV_CONF_REPROBE |
8774*a204de77Scth 		    NDI_NO_EVENT, BUS_CONFIG_ALL, DDI_MAJOR_T_NONE);
877567e56d35Sramat 
87763c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
877767e56d35Sramat 		vhc->vhc_path_discovery_cutoff_time = lbolt64 +
877867e56d35Sramat 		    mdi_path_discovery_interval * TICKS_PER_SECOND;
87793c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
878067e56d35Sramat 		rv = 1;
87813c34adc5Sramat 	}
87823c34adc5Sramat 
87833c34adc5Sramat 	single_threaded_vhconfig_exit(vhc);
87843c34adc5Sramat 	return (rv);
87853c34adc5Sramat }
87863c34adc5Sramat 
87873c34adc5Sramat /*
87883c34adc5Sramat  * Generic vhci bus config implementation:
87893c34adc5Sramat  *
87903c34adc5Sramat  * Parameters
87913c34adc5Sramat  *	vdip	vhci dip
87923c34adc5Sramat  *	flags	bus config flags
87933c34adc5Sramat  *	op	bus config operation
87943c34adc5Sramat  *	The remaining parameters are bus config operation specific
87953c34adc5Sramat  *
87963c34adc5Sramat  * for BUS_CONFIG_ONE
87973c34adc5Sramat  *	arg	pointer to name@addr
87983c34adc5Sramat  *	child	upon successful return from this function, *child will be
87993c34adc5Sramat  *		set to the configured and held devinfo child node of vdip.
88003c34adc5Sramat  *	ct_addr	pointer to client address (i.e. GUID)
88013c34adc5Sramat  *
88023c34adc5Sramat  * for BUS_CONFIG_DRIVER
88033c34adc5Sramat  *	arg	major number of the driver
88043c34adc5Sramat  *	child and ct_addr parameters are ignored
88053c34adc5Sramat  *
88063c34adc5Sramat  * for BUS_CONFIG_ALL
88073c34adc5Sramat  *	arg, child, and ct_addr parameters are ignored
88083c34adc5Sramat  *
88093c34adc5Sramat  * Note that for the rest of the bus config operations, this function simply
88103c34adc5Sramat  * calls the framework provided default bus config routine.
88113c34adc5Sramat  */
88123c34adc5Sramat int
88133c34adc5Sramat mdi_vhci_bus_config(dev_info_t *vdip, uint_t flags, ddi_bus_config_op_t op,
88143c34adc5Sramat     void *arg, dev_info_t **child, char *ct_addr)
88153c34adc5Sramat {
88163c34adc5Sramat 	mdi_vhci_t *vh = i_devi_get_vhci(vdip);
88173c34adc5Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
88183c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
881967e56d35Sramat 	int rv = 0;
882067e56d35Sramat 	int params_valid = 0;
88213c34adc5Sramat 	char *cp;
88223c34adc5Sramat 
88233c34adc5Sramat 	/*
88245e3986cbScth 	 * To bus config vhcis we relay operation, possibly using another
88255e3986cbScth 	 * thread, to phcis. The phci driver then interacts with MDI to cause
88265e3986cbScth 	 * vhci child nodes to be enumerated under the vhci node.  Adding a
88275e3986cbScth 	 * vhci child requires an ndi_devi_enter of the vhci. Since another
88285e3986cbScth 	 * thread may be adding the child, to avoid deadlock we can't wait
88295e3986cbScth 	 * for the relayed operations to complete if we have already entered
88305e3986cbScth 	 * the vhci node.
88313c34adc5Sramat 	 */
88323c34adc5Sramat 	if (DEVI_BUSY_OWNED(vdip)) {
88335e3986cbScth 		MDI_DEBUG(2, (CE_NOTE, vdip, "!MDI: vhci bus config: "
88345e3986cbScth 		    "vhci dip is busy owned %p\n", (void *)vdip));
88353c34adc5Sramat 		goto default_bus_config;
88363c34adc5Sramat 	}
88373c34adc5Sramat 
88383c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
88393c34adc5Sramat 	if (!(vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE)) {
88403c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
884152cac543Sramat 		rv = build_vhci_cache(vh);
88423c34adc5Sramat 		rw_enter(&vhcache->vhcache_lock, RW_READER);
88433c34adc5Sramat 	}
88443c34adc5Sramat 
88453c34adc5Sramat 	switch (op) {
88463c34adc5Sramat 	case BUS_CONFIG_ONE:
884767e56d35Sramat 		if (arg != NULL && ct_addr != NULL) {
88483c34adc5Sramat 			/* extract node name */
88493c34adc5Sramat 			cp = (char *)arg;
88503c34adc5Sramat 			while (*cp != '\0' && *cp != '@')
88513c34adc5Sramat 				cp++;
88523c34adc5Sramat 			if (*cp == '@') {
885367e56d35Sramat 				params_valid = 1;
88543c34adc5Sramat 				*cp = '\0';
88553c34adc5Sramat 				config_client_paths(vhc, (char *)arg, ct_addr);
885667e56d35Sramat 				/* config_client_paths() releases cache_lock */
88573c34adc5Sramat 				*cp = '@';
885867e56d35Sramat 				break;
885967e56d35Sramat 			}
886067e56d35Sramat 		}
886167e56d35Sramat 
88623c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
88633c34adc5Sramat 		break;
88643c34adc5Sramat 
88653c34adc5Sramat 	case BUS_CONFIG_DRIVER:
88663c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
886767e56d35Sramat 		if (rv == 0)
886867e56d35Sramat 			st_bus_config_all_phcis(vhc, flags, op,
88693c34adc5Sramat 			    (major_t)(uintptr_t)arg);
88703c34adc5Sramat 		break;
88713c34adc5Sramat 
88723c34adc5Sramat 	case BUS_CONFIG_ALL:
88733c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
887467e56d35Sramat 		if (rv == 0)
887567e56d35Sramat 			st_bus_config_all_phcis(vhc, flags, op, -1);
88763c34adc5Sramat 		break;
88773c34adc5Sramat 
88783c34adc5Sramat 	default:
88793c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
88803c34adc5Sramat 		break;
88813c34adc5Sramat 	}
88823c34adc5Sramat 
88833c34adc5Sramat 
88843c34adc5Sramat default_bus_config:
88853c34adc5Sramat 	/*
88863c34adc5Sramat 	 * All requested child nodes are enumerated under the vhci.
88873c34adc5Sramat 	 * Now configure them.
88883c34adc5Sramat 	 */
88893c34adc5Sramat 	if (ndi_busop_bus_config(vdip, flags, op, arg, child, 0) ==
88903c34adc5Sramat 	    NDI_SUCCESS) {
88913c34adc5Sramat 		return (MDI_SUCCESS);
889267e56d35Sramat 	} else if (op == BUS_CONFIG_ONE && rv == 0 && params_valid) {
889367e56d35Sramat 		/* discover all paths and try configuring again */
889452cac543Sramat 		if (vhcache_discover_paths(vh) &&
889567e56d35Sramat 		    ndi_busop_bus_config(vdip, flags, op, arg, child, 0) ==
889667e56d35Sramat 		    NDI_SUCCESS)
889767e56d35Sramat 			return (MDI_SUCCESS);
88983c34adc5Sramat 	}
88993c34adc5Sramat 
89003c34adc5Sramat 	return (MDI_FAILURE);
89013c34adc5Sramat }
89023c34adc5Sramat 
89033c34adc5Sramat /*
89043c34adc5Sramat  * Read the on-disk vhci cache into an nvlist for the specified vhci class.
89053c34adc5Sramat  */
89063c34adc5Sramat static nvlist_t *
89073c34adc5Sramat read_on_disk_vhci_cache(char *vhci_class)
89083c34adc5Sramat {
89093c34adc5Sramat 	nvlist_t *nvl;
89103c34adc5Sramat 	int err;
89113c34adc5Sramat 	char *filename;
89123c34adc5Sramat 
89133c34adc5Sramat 	filename = vhclass2vhcache_filename(vhci_class);
89143c34adc5Sramat 
89153c34adc5Sramat 	if ((err = fread_nvlist(filename, &nvl)) == 0) {
89163c34adc5Sramat 		kmem_free(filename, strlen(filename) + 1);
89173c34adc5Sramat 		return (nvl);
89183c34adc5Sramat 	} else if (err == EIO)
89193c34adc5Sramat 		cmn_err(CE_WARN, "%s: I/O error, will recreate\n", filename);
89203c34adc5Sramat 	else if (err == EINVAL)
89213c34adc5Sramat 		cmn_err(CE_WARN,
89223c34adc5Sramat 		    "%s: data file corrupted, will recreate\n", filename);
89233c34adc5Sramat 
89243c34adc5Sramat 	kmem_free(filename, strlen(filename) + 1);
89253c34adc5Sramat 	return (NULL);
89263c34adc5Sramat }
89273c34adc5Sramat 
89283c34adc5Sramat /*
89293c34adc5Sramat  * Read on-disk vhci cache into nvlists for all vhci classes.
89303c34adc5Sramat  * Called during booting by i_ddi_read_devices_files().
89313c34adc5Sramat  */
89323c34adc5Sramat void
89333c34adc5Sramat mdi_read_devices_files(void)
89343c34adc5Sramat {
89353c34adc5Sramat 	int i;
89363c34adc5Sramat 
89373c34adc5Sramat 	for (i = 0; i < N_VHCI_CLASSES; i++)
89383c34adc5Sramat 		vhcache_nvl[i] = read_on_disk_vhci_cache(vhci_class_list[i]);
89393c34adc5Sramat }
89403c34adc5Sramat 
89413c34adc5Sramat /*
89423c34adc5Sramat  * Remove all stale entries from vhci cache.
89433c34adc5Sramat  */
89443c34adc5Sramat static void
89453c34adc5Sramat clean_vhcache(mdi_vhci_config_t *vhc)
89463c34adc5Sramat {
89473c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
89483c34adc5Sramat 	mdi_vhcache_phci_t *cphci, *cphci_head, *cphci_next;
89493c34adc5Sramat 	mdi_vhcache_client_t *cct, *cct_head, *cct_next;
89503c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi, *cpi_head, *cpi_next;
89513c34adc5Sramat 
89523c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
89533c34adc5Sramat 
89543c34adc5Sramat 	cct_head = vhcache->vhcache_client_head;
89553c34adc5Sramat 	vhcache->vhcache_client_head = vhcache->vhcache_client_tail = NULL;
89563c34adc5Sramat 	for (cct = cct_head; cct != NULL; cct = cct_next) {
89573c34adc5Sramat 		cct_next = cct->cct_next;
89583c34adc5Sramat 
89593c34adc5Sramat 		cpi_head = cct->cct_cpi_head;
89603c34adc5Sramat 		cct->cct_cpi_head = cct->cct_cpi_tail = NULL;
89613c34adc5Sramat 		for (cpi = cpi_head; cpi != NULL; cpi = cpi_next) {
89623c34adc5Sramat 			cpi_next = cpi->cpi_next;
89633c34adc5Sramat 			if (cpi->cpi_pip != NULL) {
89643c34adc5Sramat 				ASSERT(cpi->cpi_cphci->cphci_phci != NULL);
89653c34adc5Sramat 				enqueue_tail_vhcache_pathinfo(cct, cpi);
89663c34adc5Sramat 			} else
89673c34adc5Sramat 				free_vhcache_pathinfo(cpi);
89683c34adc5Sramat 		}
89693c34adc5Sramat 
89703c34adc5Sramat 		if (cct->cct_cpi_head != NULL)
89713c34adc5Sramat 			enqueue_vhcache_client(vhcache, cct);
89723c34adc5Sramat 		else {
89733c34adc5Sramat 			(void) mod_hash_destroy(vhcache->vhcache_client_hash,
89743c34adc5Sramat 			    (mod_hash_key_t)cct->cct_name_addr);
89753c34adc5Sramat 			free_vhcache_client(cct);
89763c34adc5Sramat 		}
89773c34adc5Sramat 	}
89783c34adc5Sramat 
89793c34adc5Sramat 	cphci_head = vhcache->vhcache_phci_head;
89803c34adc5Sramat 	vhcache->vhcache_phci_head = vhcache->vhcache_phci_tail = NULL;
89813c34adc5Sramat 	for (cphci = cphci_head; cphci != NULL; cphci = cphci_next) {
89823c34adc5Sramat 		cphci_next = cphci->cphci_next;
89833c34adc5Sramat 		if (cphci->cphci_phci != NULL)
89843c34adc5Sramat 			enqueue_vhcache_phci(vhcache, cphci);
89853c34adc5Sramat 		else
89863c34adc5Sramat 			free_vhcache_phci(cphci);
89873c34adc5Sramat 	}
89883c34adc5Sramat 
89893c34adc5Sramat 	vhcache->vhcache_clean_time = lbolt64;
89903c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
89913c34adc5Sramat 	vhcache_dirty(vhc);
89923c34adc5Sramat }
89933c34adc5Sramat 
89943c34adc5Sramat /*
89953c34adc5Sramat  * Remove all stale entries from vhci cache.
89963c34adc5Sramat  * Called by i_ddi_clean_devices_files() during the execution of devfsadm -C
89973c34adc5Sramat  */
89983c34adc5Sramat void
89993c34adc5Sramat mdi_clean_vhcache(void)
90003c34adc5Sramat {
90013c34adc5Sramat 	mdi_vhci_t *vh;
90023c34adc5Sramat 
90033c34adc5Sramat 	mutex_enter(&mdi_mutex);
90043c34adc5Sramat 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
90053c34adc5Sramat 		vh->vh_refcnt++;
90063c34adc5Sramat 		mutex_exit(&mdi_mutex);
90073c34adc5Sramat 		clean_vhcache(vh->vh_config);
90083c34adc5Sramat 		mutex_enter(&mdi_mutex);
90093c34adc5Sramat 		vh->vh_refcnt--;
90103c34adc5Sramat 	}
90113c34adc5Sramat 	mutex_exit(&mdi_mutex);
90123c34adc5Sramat }
90138c4f8890Srs135747 
90148c4f8890Srs135747 /*
90158c4f8890Srs135747  * mdi_vhci_walk_clients():
90168c4f8890Srs135747  *		Walker routine to traverse client dev_info nodes
90178c4f8890Srs135747  * ddi_walk_devs(ddi_get_child(vdip), f, arg) returns the entire tree
90188c4f8890Srs135747  * below the client, including nexus devices, which we dont want.
90198c4f8890Srs135747  * So we just traverse the immediate siblings, starting from 1st client.
90208c4f8890Srs135747  */
90218c4f8890Srs135747 void
90228c4f8890Srs135747 mdi_vhci_walk_clients(dev_info_t *vdip,
90238c4f8890Srs135747     int (*f)(dev_info_t *, void *), void *arg)
90248c4f8890Srs135747 {
90255e3986cbScth 	mdi_vhci_t	*vh = i_devi_get_vhci(vdip);
90268c4f8890Srs135747 	dev_info_t	*cdip;
90278c4f8890Srs135747 	mdi_client_t	*ct;
90288c4f8890Srs135747 
90295e3986cbScth 	MDI_VHCI_CLIENT_LOCK(vh);
90308c4f8890Srs135747 	cdip = ddi_get_child(vdip);
90318c4f8890Srs135747 	while (cdip) {
90328c4f8890Srs135747 		ct = i_devi_get_client(cdip);
90338c4f8890Srs135747 		MDI_CLIENT_LOCK(ct);
90348c4f8890Srs135747 
90355e3986cbScth 		if (((*f)(cdip, arg)) == DDI_WALK_CONTINUE)
90368c4f8890Srs135747 			cdip = ddi_get_next_sibling(cdip);
90375e3986cbScth 		else
90385e3986cbScth 			cdip = NULL;
9039c73a93f2Sdm120769 
9040c73a93f2Sdm120769 		MDI_CLIENT_UNLOCK(ct);
90418c4f8890Srs135747 	}
90425e3986cbScth 	MDI_VHCI_CLIENT_UNLOCK(vh);
90438c4f8890Srs135747 }
90448c4f8890Srs135747 
90458c4f8890Srs135747 /*
90468c4f8890Srs135747  * mdi_vhci_walk_phcis():
90478c4f8890Srs135747  *		Walker routine to traverse phci dev_info nodes
90488c4f8890Srs135747  */
90498c4f8890Srs135747 void
90508c4f8890Srs135747 mdi_vhci_walk_phcis(dev_info_t *vdip,
90518c4f8890Srs135747     int (*f)(dev_info_t *, void *), void *arg)
90528c4f8890Srs135747 {
90535e3986cbScth 	mdi_vhci_t	*vh = i_devi_get_vhci(vdip);
90545e3986cbScth 	mdi_phci_t	*ph, *next;
90558c4f8890Srs135747 
90565e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
90578c4f8890Srs135747 	ph = vh->vh_phci_head;
90588c4f8890Srs135747 	while (ph) {
90598c4f8890Srs135747 		MDI_PHCI_LOCK(ph);
90608c4f8890Srs135747 
90615e3986cbScth 		if (((*f)(ph->ph_dip, arg)) == DDI_WALK_CONTINUE)
90625e3986cbScth 			next = ph->ph_next;
90635e3986cbScth 		else
90645e3986cbScth 			next = NULL;
9065c73a93f2Sdm120769 
9066c73a93f2Sdm120769 		MDI_PHCI_UNLOCK(ph);
90675e3986cbScth 		ph = next;
90688c4f8890Srs135747 	}
90695e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
90708c4f8890Srs135747 }
90718c4f8890Srs135747 
90728c4f8890Srs135747 
90738c4f8890Srs135747 /*
90748c4f8890Srs135747  * mdi_walk_vhcis():
90758c4f8890Srs135747  *		Walker routine to traverse vhci dev_info nodes
90768c4f8890Srs135747  */
90778c4f8890Srs135747 void
90788c4f8890Srs135747 mdi_walk_vhcis(int (*f)(dev_info_t *, void *), void *arg)
90798c4f8890Srs135747 {
90808c4f8890Srs135747 	mdi_vhci_t	*vh = NULL;
90818c4f8890Srs135747 
90828c4f8890Srs135747 	mutex_enter(&mdi_mutex);
90838c4f8890Srs135747 	/*
90848c4f8890Srs135747 	 * Scan for already registered vhci
90858c4f8890Srs135747 	 */
90868c4f8890Srs135747 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
90878c4f8890Srs135747 		vh->vh_refcnt++;
90888c4f8890Srs135747 		mutex_exit(&mdi_mutex);
90898c4f8890Srs135747 		if (((*f)(vh->vh_dip, arg)) != DDI_WALK_CONTINUE) {
90908c4f8890Srs135747 			mutex_enter(&mdi_mutex);
90918c4f8890Srs135747 			vh->vh_refcnt--;
90928c4f8890Srs135747 			break;
90938c4f8890Srs135747 		} else {
90948c4f8890Srs135747 			mutex_enter(&mdi_mutex);
90958c4f8890Srs135747 			vh->vh_refcnt--;
90968c4f8890Srs135747 		}
90978c4f8890Srs135747 	}
90988c4f8890Srs135747 
90998c4f8890Srs135747 	mutex_exit(&mdi_mutex);
91008c4f8890Srs135747 }
91018c4f8890Srs135747 
91028c4f8890Srs135747 /*
91038c4f8890Srs135747  * i_mdi_log_sysevent():
91048c4f8890Srs135747  *		Logs events for pickup by syseventd
91058c4f8890Srs135747  */
91068c4f8890Srs135747 static void
91078c4f8890Srs135747 i_mdi_log_sysevent(dev_info_t *dip, char *ph_vh_class, char *subclass)
91088c4f8890Srs135747 {
91098c4f8890Srs135747 	char		*path_name;
91108c4f8890Srs135747 	nvlist_t	*attr_list;
91118c4f8890Srs135747 
91128c4f8890Srs135747 	if (nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE,
91138c4f8890Srs135747 	    KM_SLEEP) != DDI_SUCCESS) {
91148c4f8890Srs135747 		goto alloc_failed;
91158c4f8890Srs135747 	}
91168c4f8890Srs135747 
91178c4f8890Srs135747 	path_name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
91188c4f8890Srs135747 	(void) ddi_pathname(dip, path_name);
91198c4f8890Srs135747 
91208c4f8890Srs135747 	if (nvlist_add_string(attr_list, DDI_DRIVER_NAME,
91218c4f8890Srs135747 	    ddi_driver_name(dip)) != DDI_SUCCESS) {
91228c4f8890Srs135747 		goto error;
91238c4f8890Srs135747 	}
91248c4f8890Srs135747 
91258c4f8890Srs135747 	if (nvlist_add_int32(attr_list, DDI_DRIVER_MAJOR,
91268c4f8890Srs135747 	    (int32_t)ddi_driver_major(dip)) != DDI_SUCCESS) {
91278c4f8890Srs135747 		goto error;
91288c4f8890Srs135747 	}
91298c4f8890Srs135747 
91308c4f8890Srs135747 	if (nvlist_add_int32(attr_list, DDI_INSTANCE,
91318c4f8890Srs135747 	    (int32_t)ddi_get_instance(dip)) != DDI_SUCCESS) {
91328c4f8890Srs135747 		goto error;
91338c4f8890Srs135747 	}
91348c4f8890Srs135747 
91358c4f8890Srs135747 	if (nvlist_add_string(attr_list, DDI_PATHNAME,
91368c4f8890Srs135747 	    path_name) != DDI_SUCCESS) {
91378c4f8890Srs135747 		goto error;
91388c4f8890Srs135747 	}
91398c4f8890Srs135747 
91408c4f8890Srs135747 	if (nvlist_add_string(attr_list, DDI_CLASS,
91418c4f8890Srs135747 	    ph_vh_class) != DDI_SUCCESS) {
91428c4f8890Srs135747 		goto error;
91438c4f8890Srs135747 	}
91448c4f8890Srs135747 
91458c4f8890Srs135747 	(void) ddi_log_sysevent(dip, DDI_VENDOR_SUNW, EC_DDI, subclass,
91468c4f8890Srs135747 	    attr_list, NULL, DDI_SLEEP);
91478c4f8890Srs135747 
91488c4f8890Srs135747 error:
91498c4f8890Srs135747 	kmem_free(path_name, MAXPATHLEN);
91508c4f8890Srs135747 	nvlist_free(attr_list);
91518c4f8890Srs135747 	return;
91528c4f8890Srs135747 
91538c4f8890Srs135747 alloc_failed:
91548c4f8890Srs135747 	MDI_DEBUG(1, (CE_WARN, dip,
91558c4f8890Srs135747 	    "!i_mdi_log_sysevent: Unable to send sysevent"));
91568c4f8890Srs135747 }
9157f7209cf2Spramodbg 
9158f7209cf2Spramodbg char **
9159f7209cf2Spramodbg mdi_get_phci_driver_list(char *vhci_class, int	*ndrivers)
9160f7209cf2Spramodbg {
9161f7209cf2Spramodbg 	char	**driver_list, **ret_driver_list = NULL;
9162f7209cf2Spramodbg 	int	*root_support_list;
9163f7209cf2Spramodbg 	int	cur_elements, max_elements;
9164f7209cf2Spramodbg 
9165f7209cf2Spramodbg 	get_phci_driver_list(vhci_class, &driver_list, &root_support_list,
9166f7209cf2Spramodbg 	    &cur_elements, &max_elements);
9167f7209cf2Spramodbg 
9168f7209cf2Spramodbg 
9169f7209cf2Spramodbg 	if (driver_list) {
9170f7209cf2Spramodbg 		kmem_free(root_support_list, sizeof (int) * max_elements);
9171f7209cf2Spramodbg 		ret_driver_list = mdi_realloc(driver_list, sizeof (char *)
9172f7209cf2Spramodbg 		    * max_elements, sizeof (char *) * cur_elements);
9173f7209cf2Spramodbg 	}
9174f7209cf2Spramodbg 	*ndrivers = cur_elements;
9175f7209cf2Spramodbg 
9176f7209cf2Spramodbg 	return (ret_driver_list);
9177f7209cf2Spramodbg 
9178f7209cf2Spramodbg }
9179f7209cf2Spramodbg 
9180f7209cf2Spramodbg void
9181f7209cf2Spramodbg mdi_free_phci_driver_list(char **driver_list, int ndrivers)
9182f7209cf2Spramodbg {
9183f7209cf2Spramodbg 	char	**p;
9184f7209cf2Spramodbg 	int	i;
9185f7209cf2Spramodbg 
9186f7209cf2Spramodbg 	if (driver_list) {
9187f7209cf2Spramodbg 		for (i = 0, p = driver_list; i < ndrivers; i++, p++)
9188f7209cf2Spramodbg 			kmem_free(*p, strlen(*p) + 1);
9189f7209cf2Spramodbg 		kmem_free(driver_list, sizeof (char *) * ndrivers);
9190f7209cf2Spramodbg 	}
9191f7209cf2Spramodbg }
9192