xref: /titanic_54/usr/src/uts/common/os/sunmdi.c (revision 144dfaa9a648eea321858b34d4941d2268130176)
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 /*
22737d277aScth  * Copyright 2006 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  *
33*144dfaa9Scth  * _NOTE(LOCK_ORDER(mdi_mutex, mdi_vhci:vh_phci_mutex);
34*144dfaa9Scth  * _NOTE(LOCK_ORDER(mdi_mutex, mdi_vhci:vh_client_mutex);
35*144dfaa9Scth  * _NOTE(LOCK_ORDER(mdi_vhci:vh_phci_mutex, mdi_phci::ph_mutex);
36*144dfaa9Scth  * _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>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #ifdef	DEBUG
737c478bd9Sstevel@tonic-gate #include <sys/debug.h>
747c478bd9Sstevel@tonic-gate int	mdi_debug = 1;
75*144dfaa9Scth int	mdi_debug_logonly = 0;
767c478bd9Sstevel@tonic-gate #define	MDI_DEBUG(level, stmnt) \
777c478bd9Sstevel@tonic-gate 	    if (mdi_debug >= (level)) i_mdi_log stmnt
787c478bd9Sstevel@tonic-gate static void i_mdi_log(int, dev_info_t *, const char *fmt, ...);
797c478bd9Sstevel@tonic-gate #else	/* !DEBUG */
807c478bd9Sstevel@tonic-gate #define	MDI_DEBUG(level, stmnt)
817c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate extern pri_t	minclsyspri;
847c478bd9Sstevel@tonic-gate extern int	modrootloaded;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Global mutex:
88*144dfaa9Scth  * Protects vHCI list and structure members.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate kmutex_t	mdi_mutex;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * Registered vHCI class driver lists
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate int		mdi_vhci_count;
967c478bd9Sstevel@tonic-gate mdi_vhci_t	*mdi_vhci_head;
977c478bd9Sstevel@tonic-gate mdi_vhci_t	*mdi_vhci_tail;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * Client Hash Table size
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate static int	mdi_client_table_size = CLIENT_HASH_TABLE_SIZE;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * taskq interface definitions
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_N_THREADS	8
1087c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_PRI		minclsyspri
1097c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_MINALLOC	(4*mdi_taskq_n_threads)
1107c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_MAXALLOC	(500*mdi_taskq_n_threads)
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate taskq_t				*mdi_taskq;
1137c478bd9Sstevel@tonic-gate static uint_t			mdi_taskq_n_threads = MDI_TASKQ_N_THREADS;
1147c478bd9Sstevel@tonic-gate 
1153c34adc5Sramat #define	TICKS_PER_SECOND	(drv_usectohz(1000000))
1163c34adc5Sramat 
1177c478bd9Sstevel@tonic-gate /*
1183c34adc5Sramat  * The data should be "quiet" for this interval (in seconds) before the
1193c34adc5Sramat  * vhci cached data is flushed to the disk.
1207c478bd9Sstevel@tonic-gate  */
1213c34adc5Sramat static int mdi_vhcache_flush_delay = 10;
1223c34adc5Sramat 
1233c34adc5Sramat /* number of seconds the vhcache flush daemon will sleep idle before exiting */
1243c34adc5Sramat static int mdi_vhcache_flush_daemon_idle_time = 60;
1253c34adc5Sramat 
1263c34adc5Sramat /*
12767e56d35Sramat  * MDI falls back to discovery of all paths when a bus_config_one fails.
12867e56d35Sramat  * The following parameters can be used to tune this operation.
12967e56d35Sramat  *
13067e56d35Sramat  * mdi_path_discovery_boot
13167e56d35Sramat  *	Number of times path discovery will be attempted during early boot.
13267e56d35Sramat  *	Probably there is no reason to ever set this value to greater than one.
13367e56d35Sramat  *
13467e56d35Sramat  * mdi_path_discovery_postboot
13567e56d35Sramat  *	Number of times path discovery will be attempted after early boot.
13667e56d35Sramat  *	Set it to a minimum of two to allow for discovery of iscsi paths which
13767e56d35Sramat  *	may happen very late during booting.
13867e56d35Sramat  *
13967e56d35Sramat  * mdi_path_discovery_interval
14067e56d35Sramat  *	Minimum number of seconds MDI will wait between successive discovery
14167e56d35Sramat  *	of all paths. Set it to -1 to disable discovery of all paths.
14267e56d35Sramat  */
14367e56d35Sramat static int mdi_path_discovery_boot = 1;
14467e56d35Sramat static int mdi_path_discovery_postboot = 2;
14567e56d35Sramat static int mdi_path_discovery_interval = 10;
14667e56d35Sramat 
14767e56d35Sramat /*
1483c34adc5Sramat  * number of seconds the asynchronous configuration thread will sleep idle
1493c34adc5Sramat  * before exiting.
1503c34adc5Sramat  */
1513c34adc5Sramat static int mdi_async_config_idle_time = 600;
1523c34adc5Sramat 
1533c34adc5Sramat static int mdi_bus_config_cache_hash_size = 256;
1543c34adc5Sramat 
1553c34adc5Sramat /* turns off multithreaded configuration for certain operations */
1563c34adc5Sramat static int mdi_mtc_off = 0;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * MDI component property name/value string definitions
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate const char 		*mdi_component_prop = "mpxio-component";
1627c478bd9Sstevel@tonic-gate const char		*mdi_component_prop_vhci = "vhci";
1637c478bd9Sstevel@tonic-gate const char		*mdi_component_prop_phci = "phci";
1647c478bd9Sstevel@tonic-gate const char		*mdi_component_prop_client = "client";
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  * MDI client global unique identifier property name
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate const char		*mdi_client_guid_prop = "client-guid";
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * MDI client load balancing property name/value string definitions
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate const char		*mdi_load_balance = "load-balance";
1757c478bd9Sstevel@tonic-gate const char		*mdi_load_balance_none = "none";
1767c478bd9Sstevel@tonic-gate const char		*mdi_load_balance_rr = "round-robin";
1777c478bd9Sstevel@tonic-gate const char		*mdi_load_balance_lba = "logical-block";
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * Obsolete vHCI class definition; to be removed after Leadville update
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate const char *mdi_vhci_class_scsi = MDI_HCI_CLASS_SCSI;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate static char vhci_greeting[] =
1857c478bd9Sstevel@tonic-gate 	"\tThere already exists one vHCI driver for class %s\n"
1867c478bd9Sstevel@tonic-gate 	"\tOnly one vHCI driver for each class is allowed\n";
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * Static function prototypes
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate static int		i_mdi_phci_offline(dev_info_t *, uint_t);
1927c478bd9Sstevel@tonic-gate static int		i_mdi_client_offline(dev_info_t *, uint_t);
1937c478bd9Sstevel@tonic-gate static int		i_mdi_phci_pre_detach(dev_info_t *, ddi_detach_cmd_t);
1947c478bd9Sstevel@tonic-gate static void		i_mdi_phci_post_detach(dev_info_t *,
1957c478bd9Sstevel@tonic-gate 			    ddi_detach_cmd_t, int);
1967c478bd9Sstevel@tonic-gate static int		i_mdi_client_pre_detach(dev_info_t *,
1977c478bd9Sstevel@tonic-gate 			    ddi_detach_cmd_t);
1987c478bd9Sstevel@tonic-gate static void		i_mdi_client_post_detach(dev_info_t *,
1997c478bd9Sstevel@tonic-gate 			    ddi_detach_cmd_t, int);
2007c478bd9Sstevel@tonic-gate static void		i_mdi_pm_hold_pip(mdi_pathinfo_t *);
2017c478bd9Sstevel@tonic-gate static void		i_mdi_pm_rele_pip(mdi_pathinfo_t *);
2027c478bd9Sstevel@tonic-gate static int 		i_mdi_lba_lb(mdi_client_t *ct,
2037c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_t **ret_pip, struct buf *buf);
2047c478bd9Sstevel@tonic-gate static void		i_mdi_pm_hold_client(mdi_client_t *, int);
2057c478bd9Sstevel@tonic-gate static void		i_mdi_pm_rele_client(mdi_client_t *, int);
2067c478bd9Sstevel@tonic-gate static void		i_mdi_pm_reset_client(mdi_client_t *);
2077c478bd9Sstevel@tonic-gate static int		i_mdi_power_all_phci(mdi_client_t *);
2088c4f8890Srs135747 static void		i_mdi_log_sysevent(dev_info_t *, char *, char *);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  * Internal mdi_pathinfo node functions
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate static int		i_mdi_pi_kstat_create(mdi_pathinfo_t *);
2157c478bd9Sstevel@tonic-gate static void		i_mdi_pi_kstat_destroy(mdi_pathinfo_t *);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static mdi_vhci_t	*i_mdi_vhci_class2vhci(char *);
2187c478bd9Sstevel@tonic-gate static mdi_vhci_t	*i_devi_get_vhci(dev_info_t *);
2197c478bd9Sstevel@tonic-gate static mdi_phci_t	*i_devi_get_phci(dev_info_t *);
2207c478bd9Sstevel@tonic-gate static void		i_mdi_phci_lock(mdi_phci_t *, mdi_pathinfo_t *);
2217c478bd9Sstevel@tonic-gate static void		i_mdi_phci_unlock(mdi_phci_t *);
2223c34adc5Sramat static mdi_pathinfo_t	*i_mdi_pi_alloc(mdi_phci_t *, char *, mdi_client_t *);
2237c478bd9Sstevel@tonic-gate static void		i_mdi_phci_add_path(mdi_phci_t *, mdi_pathinfo_t *);
2247c478bd9Sstevel@tonic-gate static void		i_mdi_client_add_path(mdi_client_t *, mdi_pathinfo_t *);
2257c478bd9Sstevel@tonic-gate static void		i_mdi_pi_free(mdi_phci_t *ph, mdi_pathinfo_t *,
2267c478bd9Sstevel@tonic-gate 			    mdi_client_t *);
2277c478bd9Sstevel@tonic-gate static void		i_mdi_phci_remove_path(mdi_phci_t *, mdi_pathinfo_t *);
2287c478bd9Sstevel@tonic-gate static void		i_mdi_client_remove_path(mdi_client_t *,
2297c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_t *);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static int		i_mdi_pi_state_change(mdi_pathinfo_t *,
2327c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_state_t, int);
2337c478bd9Sstevel@tonic-gate static int		i_mdi_pi_offline(mdi_pathinfo_t *, int);
2347c478bd9Sstevel@tonic-gate static dev_info_t	*i_mdi_devinfo_create(mdi_vhci_t *, char *, char *,
2353c34adc5Sramat 			    char **, int);
2367c478bd9Sstevel@tonic-gate static dev_info_t	*i_mdi_devinfo_find(mdi_vhci_t *, char *, char *);
2377c478bd9Sstevel@tonic-gate static int		i_mdi_devinfo_remove(dev_info_t *, dev_info_t *, int);
2387c478bd9Sstevel@tonic-gate static int		i_mdi_is_child_present(dev_info_t *, dev_info_t *);
2393c34adc5Sramat static mdi_client_t	*i_mdi_client_alloc(mdi_vhci_t *, char *, char *);
2407c478bd9Sstevel@tonic-gate static void		i_mdi_client_enlist_table(mdi_vhci_t *, mdi_client_t *);
2417c478bd9Sstevel@tonic-gate static void		i_mdi_client_delist_table(mdi_vhci_t *, mdi_client_t *);
2423c34adc5Sramat static mdi_client_t	*i_mdi_client_find(mdi_vhci_t *, char *, char *);
2437c478bd9Sstevel@tonic-gate static void		i_mdi_client_update_state(mdi_client_t *);
2447c478bd9Sstevel@tonic-gate static int		i_mdi_client_compute_state(mdi_client_t *,
2457c478bd9Sstevel@tonic-gate 			    mdi_phci_t *);
2467c478bd9Sstevel@tonic-gate static void		i_mdi_client_lock(mdi_client_t *, mdi_pathinfo_t *);
2477c478bd9Sstevel@tonic-gate static void		i_mdi_client_unlock(mdi_client_t *);
2487c478bd9Sstevel@tonic-gate static int		i_mdi_client_free(mdi_vhci_t *, mdi_client_t *);
2497c478bd9Sstevel@tonic-gate static mdi_client_t	*i_devi_get_client(dev_info_t *);
250ee28b439Scm136836 /*
251ee28b439Scm136836  * NOTE: this will be removed once the NWS files are changed to use the new
252ee28b439Scm136836  * mdi_{enable,disable}_path interfaces
253ee28b439Scm136836  */
254ee28b439Scm136836 static int		i_mdi_pi_enable_disable(dev_info_t *, dev_info_t *,
255ee28b439Scm136836 				int, int);
256ee28b439Scm136836 static mdi_pathinfo_t 	*i_mdi_enable_disable_path(mdi_pathinfo_t *pip,
257ee28b439Scm136836 				mdi_vhci_t *vh, int flags, int op);
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * Failover related function prototypes
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate static int		i_mdi_failover(void *);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * misc internal functions
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate static int		i_mdi_get_hash_key(char *);
2677c478bd9Sstevel@tonic-gate static int		i_map_nvlist_error_to_mdi(int);
2687c478bd9Sstevel@tonic-gate static void		i_mdi_report_path_state(mdi_client_t *,
2697c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_t *);
2707c478bd9Sstevel@tonic-gate 
2713c34adc5Sramat static void		setup_vhci_cache(mdi_vhci_t *);
2723c34adc5Sramat static int		destroy_vhci_cache(mdi_vhci_t *);
2733c34adc5Sramat static void		setup_phci_driver_list(mdi_vhci_t *);
2743c34adc5Sramat static void		free_phci_driver_list(mdi_vhci_config_t *);
2753c34adc5Sramat static int		stop_vhcache_async_threads(mdi_vhci_config_t *);
2763c34adc5Sramat static boolean_t	stop_vhcache_flush_thread(void *, int);
2773c34adc5Sramat static void		free_string_array(char **, int);
2783c34adc5Sramat static void		free_vhcache_phci(mdi_vhcache_phci_t *);
2793c34adc5Sramat static void		free_vhcache_pathinfo(mdi_vhcache_pathinfo_t *);
2803c34adc5Sramat static void		free_vhcache_client(mdi_vhcache_client_t *);
2813c34adc5Sramat static int		mainnvl_to_vhcache(mdi_vhci_cache_t *, nvlist_t *);
2823c34adc5Sramat static nvlist_t		*vhcache_to_mainnvl(mdi_vhci_cache_t *);
2833c34adc5Sramat static void		vhcache_phci_add(mdi_vhci_config_t *, mdi_phci_t *);
2843c34adc5Sramat static void		vhcache_phci_remove(mdi_vhci_config_t *, mdi_phci_t *);
2853c34adc5Sramat static void		vhcache_pi_add(mdi_vhci_config_t *,
2863c34adc5Sramat 			    struct mdi_pathinfo *);
2873c34adc5Sramat static void		vhcache_pi_remove(mdi_vhci_config_t *,
2883c34adc5Sramat 			    struct mdi_pathinfo *);
2893c34adc5Sramat static void		free_phclient_path_list(mdi_phys_path_t *);
2903c34adc5Sramat static void		sort_vhcache_paths(mdi_vhcache_client_t *);
2913c34adc5Sramat static int		flush_vhcache(mdi_vhci_config_t *, int);
2923c34adc5Sramat static void		vhcache_dirty(mdi_vhci_config_t *);
2933c34adc5Sramat static void		free_async_client_config(mdi_async_client_config_t *);
29467e56d35Sramat static void		single_threaded_vhconfig_enter(mdi_vhci_config_t *);
29567e56d35Sramat static void		single_threaded_vhconfig_exit(mdi_vhci_config_t *);
2963c34adc5Sramat static nvlist_t		*read_on_disk_vhci_cache(char *);
2973c34adc5Sramat extern int		fread_nvlist(char *, nvlist_t **);
2983c34adc5Sramat extern int		fwrite_nvlist(char *, nvlist_t *);
2993c34adc5Sramat 
3007c478bd9Sstevel@tonic-gate /* called once when first vhci registers with mdi */
3017c478bd9Sstevel@tonic-gate static void
3027c478bd9Sstevel@tonic-gate i_mdi_init()
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	static int initialized = 0;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (initialized)
3077c478bd9Sstevel@tonic-gate 		return;
3087c478bd9Sstevel@tonic-gate 	initialized = 1;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	mutex_init(&mdi_mutex, NULL, MUTEX_DEFAULT, NULL);
3117c478bd9Sstevel@tonic-gate 	/*
3127c478bd9Sstevel@tonic-gate 	 * Create our taskq resources
3137c478bd9Sstevel@tonic-gate 	 */
3147c478bd9Sstevel@tonic-gate 	mdi_taskq = taskq_create("mdi_taskq", mdi_taskq_n_threads,
3157c478bd9Sstevel@tonic-gate 	    MDI_TASKQ_PRI, MDI_TASKQ_MINALLOC, MDI_TASKQ_MAXALLOC,
3167c478bd9Sstevel@tonic-gate 	    TASKQ_PREPOPULATE | TASKQ_CPR_SAFE);
3177c478bd9Sstevel@tonic-gate 	ASSERT(mdi_taskq != NULL);	/* taskq_create never fails */
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate  * mdi_get_component_type():
3227c478bd9Sstevel@tonic-gate  *		Return mpxio component type
3237c478bd9Sstevel@tonic-gate  * Return Values:
3247c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_NONE
3257c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_VHCI
3267c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_PHCI
3277c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_CLIENT
3287c478bd9Sstevel@tonic-gate  * XXX This doesn't work under multi-level MPxIO and should be
329*144dfaa9Scth  *	removed when clients migrate mdi_component_is_*() interfaces.
3307c478bd9Sstevel@tonic-gate  */
3317c478bd9Sstevel@tonic-gate int
3327c478bd9Sstevel@tonic-gate mdi_get_component_type(dev_info_t *dip)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_mdi_component);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate  * mdi_vhci_register():
3397c478bd9Sstevel@tonic-gate  *		Register a vHCI module with the mpxio framework
3407c478bd9Sstevel@tonic-gate  *		mdi_vhci_register() is called by vHCI drivers to register the
3417c478bd9Sstevel@tonic-gate  *		'class_driver' vHCI driver and its MDI entrypoints with the
3427c478bd9Sstevel@tonic-gate  *		mpxio framework.  The vHCI driver must call this interface as
3437c478bd9Sstevel@tonic-gate  *		part of its attach(9e) handler.
3447c478bd9Sstevel@tonic-gate  *		Competing threads may try to attach mdi_vhci_register() as
3457c478bd9Sstevel@tonic-gate  *		the vHCI drivers are loaded and attached as a result of pHCI
3467c478bd9Sstevel@tonic-gate  *		driver instance registration (mdi_phci_register()) with the
3477c478bd9Sstevel@tonic-gate  *		framework.
3487c478bd9Sstevel@tonic-gate  * Return Values:
3497c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
3507c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
3517c478bd9Sstevel@tonic-gate  */
3527c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3537c478bd9Sstevel@tonic-gate int
3547c478bd9Sstevel@tonic-gate mdi_vhci_register(char *class, dev_info_t *vdip, mdi_vhci_ops_t *vops,
3557c478bd9Sstevel@tonic-gate     int flags)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh = NULL;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	ASSERT(vops->vo_revision == MDI_VHCI_OPS_REV);
360*144dfaa9Scth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(vdip)));
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	i_mdi_init();
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	mutex_enter(&mdi_mutex);
3657c478bd9Sstevel@tonic-gate 	/*
3667c478bd9Sstevel@tonic-gate 	 * Scan for already registered vhci
3677c478bd9Sstevel@tonic-gate 	 */
3687c478bd9Sstevel@tonic-gate 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
3697c478bd9Sstevel@tonic-gate 		if (strcmp(vh->vh_class, class) == 0) {
3707c478bd9Sstevel@tonic-gate 			/*
3717c478bd9Sstevel@tonic-gate 			 * vHCI has already been created.  Check for valid
3727c478bd9Sstevel@tonic-gate 			 * vHCI ops registration.  We only support one vHCI
3737c478bd9Sstevel@tonic-gate 			 * module per class
3747c478bd9Sstevel@tonic-gate 			 */
3757c478bd9Sstevel@tonic-gate 			if (vh->vh_ops != NULL) {
3767c478bd9Sstevel@tonic-gate 				mutex_exit(&mdi_mutex);
3777c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, vhci_greeting, class);
3787c478bd9Sstevel@tonic-gate 				return (MDI_FAILURE);
3797c478bd9Sstevel@tonic-gate 			}
3807c478bd9Sstevel@tonic-gate 			break;
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	/*
3857c478bd9Sstevel@tonic-gate 	 * if not yet created, create the vHCI component
3867c478bd9Sstevel@tonic-gate 	 */
3877c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
3887c478bd9Sstevel@tonic-gate 		struct client_hash	*hash = NULL;
3897c478bd9Sstevel@tonic-gate 		char			*load_balance;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		/*
3927c478bd9Sstevel@tonic-gate 		 * Allocate and initialize the mdi extensions
3937c478bd9Sstevel@tonic-gate 		 */
3947c478bd9Sstevel@tonic-gate 		vh = kmem_zalloc(sizeof (mdi_vhci_t), KM_SLEEP);
3957c478bd9Sstevel@tonic-gate 		hash = kmem_zalloc(mdi_client_table_size * sizeof (*hash),
3967c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
3977c478bd9Sstevel@tonic-gate 		vh->vh_client_table = hash;
3987c478bd9Sstevel@tonic-gate 		vh->vh_class = kmem_zalloc(strlen(class) + 1, KM_SLEEP);
3997c478bd9Sstevel@tonic-gate 		(void) strcpy(vh->vh_class, class);
4007c478bd9Sstevel@tonic-gate 		vh->vh_lb = LOAD_BALANCE_RR;
4017c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, vdip,
4027c478bd9Sstevel@tonic-gate 		    0, LOAD_BALANCE_PROP, &load_balance) == DDI_SUCCESS) {
4037c478bd9Sstevel@tonic-gate 			if (strcmp(load_balance, LOAD_BALANCE_PROP_NONE) == 0) {
4047c478bd9Sstevel@tonic-gate 				vh->vh_lb = LOAD_BALANCE_NONE;
4057c478bd9Sstevel@tonic-gate 			} else if (strcmp(load_balance, LOAD_BALANCE_PROP_LBA)
4067c478bd9Sstevel@tonic-gate 				    == 0) {
4077c478bd9Sstevel@tonic-gate 				vh->vh_lb = LOAD_BALANCE_LBA;
4087c478bd9Sstevel@tonic-gate 			}
4097c478bd9Sstevel@tonic-gate 			ddi_prop_free(load_balance);
4107c478bd9Sstevel@tonic-gate 		}
4117c478bd9Sstevel@tonic-gate 
412*144dfaa9Scth 		mutex_init(&vh->vh_phci_mutex, NULL, MUTEX_DEFAULT, NULL);
413*144dfaa9Scth 		mutex_init(&vh->vh_client_mutex, NULL, MUTEX_DEFAULT, NULL);
414*144dfaa9Scth 
4157c478bd9Sstevel@tonic-gate 		/*
4167c478bd9Sstevel@tonic-gate 		 * Store the vHCI ops vectors
4177c478bd9Sstevel@tonic-gate 		 */
4187c478bd9Sstevel@tonic-gate 		vh->vh_dip = vdip;
4197c478bd9Sstevel@tonic-gate 		vh->vh_ops = vops;
4207c478bd9Sstevel@tonic-gate 
4213c34adc5Sramat 		setup_vhci_cache(vh);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 		if (mdi_vhci_head == NULL) {
4247c478bd9Sstevel@tonic-gate 			mdi_vhci_head = vh;
4257c478bd9Sstevel@tonic-gate 		}
4267c478bd9Sstevel@tonic-gate 		if (mdi_vhci_tail) {
4277c478bd9Sstevel@tonic-gate 			mdi_vhci_tail->vh_next = vh;
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 		mdi_vhci_tail = vh;
4307c478bd9Sstevel@tonic-gate 		mdi_vhci_count++;
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	/*
4347c478bd9Sstevel@tonic-gate 	 * Claim the devfs node as a vhci component
4357c478bd9Sstevel@tonic-gate 	 */
4367c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_component |= MDI_COMPONENT_VHCI;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	/*
4397c478bd9Sstevel@tonic-gate 	 * Initialize our back reference from dev_info node
4407c478bd9Sstevel@tonic-gate 	 */
4417c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_xhci = (caddr_t)vh;
4427c478bd9Sstevel@tonic-gate 	mutex_exit(&mdi_mutex);
4437c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate /*
4477c478bd9Sstevel@tonic-gate  * mdi_vhci_unregister():
4487c478bd9Sstevel@tonic-gate  *		Unregister a vHCI module from mpxio framework
4497c478bd9Sstevel@tonic-gate  *		mdi_vhci_unregister() is called from the detach(9E) entrypoint
4507c478bd9Sstevel@tonic-gate  * 		of a vhci to unregister it from the framework.
4517c478bd9Sstevel@tonic-gate  * Return Values:
4527c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
4537c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
4547c478bd9Sstevel@tonic-gate  */
4557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4567c478bd9Sstevel@tonic-gate int
4577c478bd9Sstevel@tonic-gate mdi_vhci_unregister(dev_info_t *vdip, int flags)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*found, *vh, *prev = NULL;
4607c478bd9Sstevel@tonic-gate 
461*144dfaa9Scth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(vdip)));
462*144dfaa9Scth 
4637c478bd9Sstevel@tonic-gate 	/*
4647c478bd9Sstevel@tonic-gate 	 * Check for invalid VHCI
4657c478bd9Sstevel@tonic-gate 	 */
4667c478bd9Sstevel@tonic-gate 	if ((vh = i_devi_get_vhci(vdip)) == NULL)
4677c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	/*
4707c478bd9Sstevel@tonic-gate 	 * Scan the list of registered vHCIs for a match
4717c478bd9Sstevel@tonic-gate 	 */
472*144dfaa9Scth 	mutex_enter(&mdi_mutex);
4737c478bd9Sstevel@tonic-gate 	for (found = mdi_vhci_head; found != NULL; found = found->vh_next) {
4747c478bd9Sstevel@tonic-gate 		if (found == vh)
4757c478bd9Sstevel@tonic-gate 			break;
4767c478bd9Sstevel@tonic-gate 		prev = found;
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (found == NULL) {
4807c478bd9Sstevel@tonic-gate 		mutex_exit(&mdi_mutex);
4817c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/*
4858c4f8890Srs135747 	 * Check the vHCI, pHCI and client count. All the pHCIs and clients
4867c478bd9Sstevel@tonic-gate 	 * should have been unregistered, before a vHCI can be
4877c478bd9Sstevel@tonic-gate 	 * unregistered.
4887c478bd9Sstevel@tonic-gate 	 */
489*144dfaa9Scth 	MDI_VHCI_PHCI_LOCK(vh);
490*144dfaa9Scth 	if (vh->vh_refcnt || vh->vh_phci_count || vh->vh_client_count) {
491*144dfaa9Scth 		MDI_VHCI_PHCI_UNLOCK(vh);
492*144dfaa9Scth 		mutex_exit(&mdi_mutex);
493*144dfaa9Scth 		return (MDI_FAILURE);
494*144dfaa9Scth 	}
495*144dfaa9Scth 	MDI_VHCI_PHCI_UNLOCK(vh);
496*144dfaa9Scth 
497*144dfaa9Scth 	if (destroy_vhci_cache(vh) != MDI_SUCCESS) {
4987c478bd9Sstevel@tonic-gate 		mutex_exit(&mdi_mutex);
4997c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	/*
5037c478bd9Sstevel@tonic-gate 	 * Remove the vHCI from the global list
5047c478bd9Sstevel@tonic-gate 	 */
5057c478bd9Sstevel@tonic-gate 	if (vh == mdi_vhci_head) {
5067c478bd9Sstevel@tonic-gate 		mdi_vhci_head = vh->vh_next;
5077c478bd9Sstevel@tonic-gate 	} else {
5087c478bd9Sstevel@tonic-gate 		prev->vh_next = vh->vh_next;
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 	if (vh == mdi_vhci_tail) {
5117c478bd9Sstevel@tonic-gate 		mdi_vhci_tail = prev;
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 	mdi_vhci_count--;
5147c478bd9Sstevel@tonic-gate 	mutex_exit(&mdi_mutex);
5153c34adc5Sramat 
5163c34adc5Sramat 	vh->vh_ops = NULL;
5177c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_component &= ~MDI_COMPONENT_VHCI;
5187c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_xhci = NULL;
5197c478bd9Sstevel@tonic-gate 	kmem_free(vh->vh_class, strlen(vh->vh_class)+1);
5207c478bd9Sstevel@tonic-gate 	kmem_free(vh->vh_client_table,
5217c478bd9Sstevel@tonic-gate 	    mdi_client_table_size * sizeof (struct client_hash));
522*144dfaa9Scth 	mutex_destroy(&vh->vh_phci_mutex);
523*144dfaa9Scth 	mutex_destroy(&vh->vh_client_mutex);
52478dc6db2Sllai1 
5257c478bd9Sstevel@tonic-gate 	kmem_free(vh, sizeof (mdi_vhci_t));
5267c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate /*
5307c478bd9Sstevel@tonic-gate  * i_mdi_vhci_class2vhci():
5317c478bd9Sstevel@tonic-gate  *		Look for a matching vHCI module given a vHCI class name
5327c478bd9Sstevel@tonic-gate  * Return Values:
5337c478bd9Sstevel@tonic-gate  *		Handle to a vHCI component
5347c478bd9Sstevel@tonic-gate  *		NULL
5357c478bd9Sstevel@tonic-gate  */
5367c478bd9Sstevel@tonic-gate static mdi_vhci_t *
5377c478bd9Sstevel@tonic-gate i_mdi_vhci_class2vhci(char *class)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&mdi_mutex));
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	mutex_enter(&mdi_mutex);
5447c478bd9Sstevel@tonic-gate 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
5457c478bd9Sstevel@tonic-gate 		if (strcmp(vh->vh_class, class) == 0) {
5467c478bd9Sstevel@tonic-gate 			break;
5477c478bd9Sstevel@tonic-gate 		}
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 	mutex_exit(&mdi_mutex);
5507c478bd9Sstevel@tonic-gate 	return (vh);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate  * i_devi_get_vhci():
5557c478bd9Sstevel@tonic-gate  *		Utility function to get the handle to a vHCI component
5567c478bd9Sstevel@tonic-gate  * Return Values:
5577c478bd9Sstevel@tonic-gate  *		Handle to a vHCI component
5587c478bd9Sstevel@tonic-gate  *		NULL
5597c478bd9Sstevel@tonic-gate  */
5607c478bd9Sstevel@tonic-gate mdi_vhci_t *
5617c478bd9Sstevel@tonic-gate i_devi_get_vhci(dev_info_t *vdip)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
5647c478bd9Sstevel@tonic-gate 	if (MDI_VHCI(vdip)) {
5657c478bd9Sstevel@tonic-gate 		vh = (mdi_vhci_t *)DEVI(vdip)->devi_mdi_xhci;
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 	return (vh);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate  * mdi_phci_register():
5727c478bd9Sstevel@tonic-gate  *		Register a pHCI module with mpxio framework
5737c478bd9Sstevel@tonic-gate  *		mdi_phci_register() is called by pHCI drivers to register with
5747c478bd9Sstevel@tonic-gate  *		the mpxio framework and a specific 'class_driver' vHCI.  The
5757c478bd9Sstevel@tonic-gate  *		pHCI driver must call this interface as part of its attach(9e)
5767c478bd9Sstevel@tonic-gate  *		handler.
5777c478bd9Sstevel@tonic-gate  * Return Values:
5787c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
5797c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
5807c478bd9Sstevel@tonic-gate  */
5817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5827c478bd9Sstevel@tonic-gate int
5837c478bd9Sstevel@tonic-gate mdi_phci_register(char *class, dev_info_t *pdip, int flags)
5847c478bd9Sstevel@tonic-gate {
5857c478bd9Sstevel@tonic-gate 	mdi_phci_t		*ph;
5867c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh;
5877c478bd9Sstevel@tonic-gate 	char			*data;
5887c478bd9Sstevel@tonic-gate 	char			*pathname;
5897c478bd9Sstevel@tonic-gate 
590*144dfaa9Scth 	/*
591*144dfaa9Scth 	 * Some subsystems, like fcp, perform pHCI registration from a
592*144dfaa9Scth 	 * different thread than the one doing the pHCI attach(9E) - the
593*144dfaa9Scth 	 * driver attach code is waiting for this other thread to complete.
594*144dfaa9Scth 	 * This means we can only ASSERT DEVI_BUSY_CHANGING of parent
595*144dfaa9Scth 	 * (indicating that some thread has done an ndi_devi_enter of parent)
596*144dfaa9Scth 	 * not DEVI_BUSY_OWNED (which would indicate that we did the enter).
597*144dfaa9Scth 	 */
598*144dfaa9Scth 	ASSERT(DEVI_BUSY_CHANGING(ddi_get_parent(pdip)));
599*144dfaa9Scth 
6007c478bd9Sstevel@tonic-gate 	pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
6017c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(pdip, pathname);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	/*
6047c478bd9Sstevel@tonic-gate 	 * Check for mpxio-disable property. Enable mpxio if the property is
6057c478bd9Sstevel@tonic-gate 	 * missing or not set to "yes".
6067c478bd9Sstevel@tonic-gate 	 * If the property is set to "yes" then emit a brief message.
6077c478bd9Sstevel@tonic-gate 	 */
6087c478bd9Sstevel@tonic-gate 	if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip, 0, "mpxio-disable",
6097c478bd9Sstevel@tonic-gate 	    &data) == DDI_SUCCESS)) {
6107c478bd9Sstevel@tonic-gate 		if (strcmp(data, "yes") == 0) {
6117c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_CONT, pdip,
6127c478bd9Sstevel@tonic-gate 			    "?%s (%s%d) multipath capabilities "
6137c478bd9Sstevel@tonic-gate 			    "disabled via %s.conf.\n", pathname,
6147c478bd9Sstevel@tonic-gate 			    ddi_driver_name(pdip), ddi_get_instance(pdip),
6157c478bd9Sstevel@tonic-gate 			    ddi_driver_name(pdip)));
6167c478bd9Sstevel@tonic-gate 			ddi_prop_free(data);
6177c478bd9Sstevel@tonic-gate 			kmem_free(pathname, MAXPATHLEN);
6187c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
6197c478bd9Sstevel@tonic-gate 		}
6207c478bd9Sstevel@tonic-gate 		ddi_prop_free(data);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/*
6267c478bd9Sstevel@tonic-gate 	 * Search for a matching vHCI
6277c478bd9Sstevel@tonic-gate 	 */
6287c478bd9Sstevel@tonic-gate 	vh = (mdi_vhci_t *)i_mdi_vhci_class2vhci(class);
6297c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
6307c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	ph = kmem_zalloc(sizeof (mdi_phci_t), KM_SLEEP);
6347c478bd9Sstevel@tonic-gate 	mutex_init(&ph->ph_mutex, NULL, MUTEX_DEFAULT, NULL);
6357c478bd9Sstevel@tonic-gate 	ph->ph_dip = pdip;
6367c478bd9Sstevel@tonic-gate 	ph->ph_vhci = vh;
6377c478bd9Sstevel@tonic-gate 	ph->ph_next = NULL;
6387c478bd9Sstevel@tonic-gate 	ph->ph_unstable = 0;
6397c478bd9Sstevel@tonic-gate 	ph->ph_vprivate = 0;
6407c478bd9Sstevel@tonic-gate 	cv_init(&ph->ph_unstable_cv, NULL, CV_DRIVER, NULL);
6417c478bd9Sstevel@tonic-gate 
642*144dfaa9Scth 	MDI_PHCI_LOCK(ph);
6437c478bd9Sstevel@tonic-gate 	MDI_PHCI_SET_POWER_UP(ph);
644*144dfaa9Scth 	MDI_PHCI_UNLOCK(ph);
6457c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_component |= MDI_COMPONENT_PHCI;
6467c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_xhci = (caddr_t)ph;
6477c478bd9Sstevel@tonic-gate 
6483c34adc5Sramat 	vhcache_phci_add(vh->vh_config, ph);
6493c34adc5Sramat 
650*144dfaa9Scth 	MDI_VHCI_PHCI_LOCK(vh);
6517c478bd9Sstevel@tonic-gate 	if (vh->vh_phci_head == NULL) {
6527c478bd9Sstevel@tonic-gate 		vh->vh_phci_head = ph;
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 	if (vh->vh_phci_tail) {
6557c478bd9Sstevel@tonic-gate 		vh->vh_phci_tail->ph_next = ph;
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate 	vh->vh_phci_tail = ph;
6587c478bd9Sstevel@tonic-gate 	vh->vh_phci_count++;
659*144dfaa9Scth 	MDI_VHCI_PHCI_UNLOCK(vh);
660*144dfaa9Scth 
6618c4f8890Srs135747 	i_mdi_log_sysevent(pdip, class, ESC_DDI_INITIATOR_REGISTER);
6627c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate /*
6667c478bd9Sstevel@tonic-gate  * mdi_phci_unregister():
6677c478bd9Sstevel@tonic-gate  *		Unregister a pHCI module from mpxio framework
6687c478bd9Sstevel@tonic-gate  *		mdi_phci_unregister() is called by the pHCI drivers from their
6697c478bd9Sstevel@tonic-gate  *		detach(9E) handler to unregister their instances from the
6707c478bd9Sstevel@tonic-gate  *		framework.
6717c478bd9Sstevel@tonic-gate  * Return Values:
6727c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
6737c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
6747c478bd9Sstevel@tonic-gate  */
6757c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6767c478bd9Sstevel@tonic-gate int
6777c478bd9Sstevel@tonic-gate mdi_phci_unregister(dev_info_t *pdip, int flags)
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh;
6807c478bd9Sstevel@tonic-gate 	mdi_phci_t		*ph;
6817c478bd9Sstevel@tonic-gate 	mdi_phci_t		*tmp;
6827c478bd9Sstevel@tonic-gate 	mdi_phci_t		*prev = NULL;
6837c478bd9Sstevel@tonic-gate 
684*144dfaa9Scth 	ASSERT(DEVI_BUSY_CHANGING(ddi_get_parent(pdip)));
685*144dfaa9Scth 
6867c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
6877c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
6887c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, pdip,
6897c478bd9Sstevel@tonic-gate 		    "!pHCI unregister: Not a valid pHCI"));
6907c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
6947c478bd9Sstevel@tonic-gate 	ASSERT(vh != NULL);
6957c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
6967c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, pdip,
6977c478bd9Sstevel@tonic-gate 		    "!pHCI unregister: Not a valid vHCI"));
6987c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 
701*144dfaa9Scth 	MDI_VHCI_PHCI_LOCK(vh);
7027c478bd9Sstevel@tonic-gate 	tmp = vh->vh_phci_head;
7037c478bd9Sstevel@tonic-gate 	while (tmp) {
7047c478bd9Sstevel@tonic-gate 		if (tmp == ph) {
7057c478bd9Sstevel@tonic-gate 			break;
7067c478bd9Sstevel@tonic-gate 		}
7077c478bd9Sstevel@tonic-gate 		prev = tmp;
7087c478bd9Sstevel@tonic-gate 		tmp = tmp->ph_next;
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	if (ph == vh->vh_phci_head) {
7127c478bd9Sstevel@tonic-gate 		vh->vh_phci_head = ph->ph_next;
7137c478bd9Sstevel@tonic-gate 	} else {
7147c478bd9Sstevel@tonic-gate 		prev->ph_next = ph->ph_next;
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	if (ph == vh->vh_phci_tail) {
7187c478bd9Sstevel@tonic-gate 		vh->vh_phci_tail = prev;
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	vh->vh_phci_count--;
722*144dfaa9Scth 	MDI_VHCI_PHCI_UNLOCK(vh);
7237c478bd9Sstevel@tonic-gate 
7248c4f8890Srs135747 	i_mdi_log_sysevent(pdip, ph->ph_vhci->vh_class,
7258c4f8890Srs135747 	    ESC_DDI_INITIATOR_UNREGISTER);
7263c34adc5Sramat 	vhcache_phci_remove(vh->vh_config, ph);
7277c478bd9Sstevel@tonic-gate 	cv_destroy(&ph->ph_unstable_cv);
7287c478bd9Sstevel@tonic-gate 	mutex_destroy(&ph->ph_mutex);
7297c478bd9Sstevel@tonic-gate 	kmem_free(ph, sizeof (mdi_phci_t));
7307c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_component &= ~MDI_COMPONENT_PHCI;
7317c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_xhci = NULL;
7327c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate /*
7367c478bd9Sstevel@tonic-gate  * i_devi_get_phci():
7377c478bd9Sstevel@tonic-gate  * 		Utility function to return the phci extensions.
7387c478bd9Sstevel@tonic-gate  */
7397c478bd9Sstevel@tonic-gate static mdi_phci_t *
7407c478bd9Sstevel@tonic-gate i_devi_get_phci(dev_info_t *pdip)
7417c478bd9Sstevel@tonic-gate {
7427c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph = NULL;
7437c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(pdip)) {
7447c478bd9Sstevel@tonic-gate 		ph = (mdi_phci_t *)DEVI(pdip)->devi_mdi_xhci;
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 	return (ph);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate /*
750*144dfaa9Scth  * Single thread mdi entry into devinfo node for modifying its children.
751*144dfaa9Scth  * If necessary we perform an ndi_devi_enter of the vHCI before doing
752*144dfaa9Scth  * an ndi_devi_enter of 'dip'.  We maintain circular in two parts: one
753*144dfaa9Scth  * for the vHCI and one for the pHCI.
754*144dfaa9Scth  */
755*144dfaa9Scth void
756*144dfaa9Scth mdi_devi_enter(dev_info_t *phci_dip, int *circular)
757*144dfaa9Scth {
758*144dfaa9Scth 	dev_info_t	*vdip;
759*144dfaa9Scth 	int		vcircular, pcircular;
760*144dfaa9Scth 
761*144dfaa9Scth 	/* Verify calling context */
762*144dfaa9Scth 	ASSERT(MDI_PHCI(phci_dip));
763*144dfaa9Scth 	vdip = mdi_devi_get_vdip(phci_dip);
764*144dfaa9Scth 	ASSERT(vdip);			/* A pHCI always has a vHCI */
765*144dfaa9Scth 
766*144dfaa9Scth 	/*
767*144dfaa9Scth 	 * If pHCI is detaching then the framework has already entered the
768*144dfaa9Scth 	 * vHCI on a threads that went down the code path leading to
769*144dfaa9Scth 	 * detach_node().  This framework enter of the vHCI during pHCI
770*144dfaa9Scth 	 * detach is done to avoid deadlock with vHCI power management
771*144dfaa9Scth 	 * operations which enter the vHCI and the enter down the path
772*144dfaa9Scth 	 * to the pHCI. If pHCI is detaching then we piggyback this calls
773*144dfaa9Scth 	 * enter of the vHCI on frameworks vHCI enter that has already
774*144dfaa9Scth 	 * occurred - this is OK because we know that the framework thread
775*144dfaa9Scth 	 * doing detach is waiting for our completion.
776*144dfaa9Scth 	 *
777*144dfaa9Scth 	 * We should DEVI_IS_DETACHING under an enter of the parent to avoid
778*144dfaa9Scth 	 * race with detach - but we can't do that because the framework has
779*144dfaa9Scth 	 * already entered the parent, so we have some complexity instead.
780*144dfaa9Scth 	 */
781*144dfaa9Scth 	for (;;) {
782*144dfaa9Scth 		if (ndi_devi_tryenter(vdip, &vcircular)) {
783*144dfaa9Scth 			ASSERT(vcircular != -1);
784*144dfaa9Scth 			if (DEVI_IS_DETACHING(phci_dip)) {
785*144dfaa9Scth 				ndi_devi_exit(vdip, vcircular);
786*144dfaa9Scth 				vcircular = -1;
787*144dfaa9Scth 			}
788*144dfaa9Scth 			break;
789*144dfaa9Scth 		} else if (DEVI_IS_DETACHING(phci_dip)) {
790*144dfaa9Scth 			vcircular = -1;
791*144dfaa9Scth 			break;
792*144dfaa9Scth 		} else {
793*144dfaa9Scth 			delay(1);
794*144dfaa9Scth 		}
795*144dfaa9Scth 	}
796*144dfaa9Scth 
797*144dfaa9Scth 	ndi_devi_enter(phci_dip, &pcircular);
798*144dfaa9Scth 	*circular = (vcircular << 16) | (pcircular & 0xFFFF);
799*144dfaa9Scth }
800*144dfaa9Scth 
801*144dfaa9Scth /*
802*144dfaa9Scth  * Release mdi_devi_enter or successful mdi_devi_tryenter.
803*144dfaa9Scth  */
804*144dfaa9Scth void
805*144dfaa9Scth mdi_devi_exit(dev_info_t *phci_dip, int circular)
806*144dfaa9Scth {
807*144dfaa9Scth 	dev_info_t	*vdip;
808*144dfaa9Scth 	int		vcircular, pcircular;
809*144dfaa9Scth 
810*144dfaa9Scth 	/* Verify calling context */
811*144dfaa9Scth 	ASSERT(MDI_PHCI(phci_dip));
812*144dfaa9Scth 	vdip = mdi_devi_get_vdip(phci_dip);
813*144dfaa9Scth 	ASSERT(vdip);			/* A pHCI always has a vHCI */
814*144dfaa9Scth 
815*144dfaa9Scth 	/* extract two circular recursion values from single int */
816*144dfaa9Scth 	pcircular = (short)(circular & 0xFFFF);
817*144dfaa9Scth 	vcircular = (short)((circular >> 16) & 0xFFFF);
818*144dfaa9Scth 
819*144dfaa9Scth 	ndi_devi_exit(phci_dip, pcircular);
820*144dfaa9Scth 	if (vcircular != -1)
821*144dfaa9Scth 		ndi_devi_exit(vdip, vcircular);
822*144dfaa9Scth }
823*144dfaa9Scth 
824*144dfaa9Scth /*
825*144dfaa9Scth  * The functions mdi_devi_exit_phci() and mdi_devi_enter_phci() are used
826*144dfaa9Scth  * around a pHCI drivers calls to mdi_pi_online/offline, after holding
827*144dfaa9Scth  * the pathinfo node via mdi_hold_path/mdi_rele_path, to avoid deadlock
828*144dfaa9Scth  * with vHCI power management code during path online/offline.  Each
829*144dfaa9Scth  * mdi_devi_exit_phci must have a matching mdi_devi_enter_phci, and both must
830*144dfaa9Scth  * occur within the scope of an active mdi_devi_enter that establishes the
831*144dfaa9Scth  * circular value.
832*144dfaa9Scth  */
833*144dfaa9Scth void
834*144dfaa9Scth mdi_devi_exit_phci(dev_info_t *phci_dip, int circular)
835*144dfaa9Scth {
836*144dfaa9Scth 	int		pcircular;
837*144dfaa9Scth 
838*144dfaa9Scth 	/* Verify calling context */
839*144dfaa9Scth 	ASSERT(MDI_PHCI(phci_dip));
840*144dfaa9Scth 
841*144dfaa9Scth 	pcircular = (short)(circular & 0xFFFF);
842*144dfaa9Scth 	ndi_devi_exit(phci_dip, pcircular);
843*144dfaa9Scth }
844*144dfaa9Scth 
845*144dfaa9Scth void
846*144dfaa9Scth mdi_devi_enter_phci(dev_info_t *phci_dip, int *circular)
847*144dfaa9Scth {
848*144dfaa9Scth 	int		pcircular;
849*144dfaa9Scth 
850*144dfaa9Scth 	/* Verify calling context */
851*144dfaa9Scth 	ASSERT(MDI_PHCI(phci_dip));
852*144dfaa9Scth 
853*144dfaa9Scth 	ndi_devi_enter(phci_dip, &pcircular);
854*144dfaa9Scth 
855*144dfaa9Scth 	/* verify matching mdi_devi_exit_phci/mdi_devi_enter_phci use */
856*144dfaa9Scth 	ASSERT(pcircular == ((short)(*circular & 0xFFFF)));
857*144dfaa9Scth }
858*144dfaa9Scth 
859*144dfaa9Scth /*
860*144dfaa9Scth  * mdi_devi_get_vdip():
861*144dfaa9Scth  *		given a pHCI dip return vHCI dip
862*144dfaa9Scth  */
863*144dfaa9Scth dev_info_t *
864*144dfaa9Scth mdi_devi_get_vdip(dev_info_t *pdip)
865*144dfaa9Scth {
866*144dfaa9Scth 	mdi_phci_t	*ph;
867*144dfaa9Scth 
868*144dfaa9Scth 	ph = i_devi_get_phci(pdip);
869*144dfaa9Scth 	if (ph && ph->ph_vhci)
870*144dfaa9Scth 		return (ph->ph_vhci->vh_dip);
871*144dfaa9Scth 	return (NULL);
872*144dfaa9Scth }
873*144dfaa9Scth 
874*144dfaa9Scth /*
875*144dfaa9Scth  * mdi_devi_pdip_entered():
876*144dfaa9Scth  *		Return 1 if we are vHCI and have done an ndi_devi_enter
877*144dfaa9Scth  *		of a pHCI
878*144dfaa9Scth  */
879*144dfaa9Scth int
880*144dfaa9Scth mdi_devi_pdip_entered(dev_info_t *vdip)
881*144dfaa9Scth {
882*144dfaa9Scth 	mdi_vhci_t	*vh;
883*144dfaa9Scth 	mdi_phci_t	*ph;
884*144dfaa9Scth 
885*144dfaa9Scth 	vh = i_devi_get_vhci(vdip);
886*144dfaa9Scth 	if (vh == NULL)
887*144dfaa9Scth 		return (0);
888*144dfaa9Scth 
889*144dfaa9Scth 	MDI_VHCI_PHCI_LOCK(vh);
890*144dfaa9Scth 	ph = vh->vh_phci_head;
891*144dfaa9Scth 	while (ph) {
892*144dfaa9Scth 		if (ph->ph_dip && DEVI_BUSY_OWNED(ph->ph_dip)) {
893*144dfaa9Scth 			MDI_VHCI_PHCI_UNLOCK(vh);
894*144dfaa9Scth 			return (1);
895*144dfaa9Scth 		}
896*144dfaa9Scth 		ph = ph->ph_next;
897*144dfaa9Scth 	}
898*144dfaa9Scth 	MDI_VHCI_PHCI_UNLOCK(vh);
899*144dfaa9Scth 	return (0);
900*144dfaa9Scth }
901*144dfaa9Scth 
902*144dfaa9Scth /*
9037c478bd9Sstevel@tonic-gate  * mdi_phci_path2devinfo():
9047c478bd9Sstevel@tonic-gate  * 		Utility function to search for a valid phci device given
9057c478bd9Sstevel@tonic-gate  *		the devfs pathname.
9067c478bd9Sstevel@tonic-gate  */
9077c478bd9Sstevel@tonic-gate dev_info_t *
9087c478bd9Sstevel@tonic-gate mdi_phci_path2devinfo(dev_info_t *vdip, caddr_t pathname)
9097c478bd9Sstevel@tonic-gate {
9107c478bd9Sstevel@tonic-gate 	char		*temp_pathname;
9117c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
9127c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
9137c478bd9Sstevel@tonic-gate 	dev_info_t 	*pdip = NULL;
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	vh = i_devi_get_vhci(vdip);
9167c478bd9Sstevel@tonic-gate 	ASSERT(vh != NULL);
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
9197c478bd9Sstevel@tonic-gate 		/*
9207c478bd9Sstevel@tonic-gate 		 * Invalid vHCI component, return failure
9217c478bd9Sstevel@tonic-gate 		 */
9227c478bd9Sstevel@tonic-gate 		return (NULL);
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	temp_pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
926*144dfaa9Scth 	MDI_VHCI_PHCI_LOCK(vh);
9277c478bd9Sstevel@tonic-gate 	ph = vh->vh_phci_head;
9287c478bd9Sstevel@tonic-gate 	while (ph != NULL) {
9297c478bd9Sstevel@tonic-gate 		pdip = ph->ph_dip;
9307c478bd9Sstevel@tonic-gate 		ASSERT(pdip != NULL);
9317c478bd9Sstevel@tonic-gate 		*temp_pathname = '\0';
9327c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(pdip, temp_pathname);
9337c478bd9Sstevel@tonic-gate 		if (strcmp(temp_pathname, pathname) == 0) {
9347c478bd9Sstevel@tonic-gate 			break;
9357c478bd9Sstevel@tonic-gate 		}
9367c478bd9Sstevel@tonic-gate 		ph = ph->ph_next;
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
9397c478bd9Sstevel@tonic-gate 		pdip = NULL;
9407c478bd9Sstevel@tonic-gate 	}
941*144dfaa9Scth 	MDI_VHCI_PHCI_UNLOCK(vh);
9427c478bd9Sstevel@tonic-gate 	kmem_free(temp_pathname, MAXPATHLEN);
9437c478bd9Sstevel@tonic-gate 	return (pdip);
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate /*
9477c478bd9Sstevel@tonic-gate  * mdi_phci_get_path_count():
9487c478bd9Sstevel@tonic-gate  * 		get number of path information nodes associated with a given
9497c478bd9Sstevel@tonic-gate  *		pHCI device.
9507c478bd9Sstevel@tonic-gate  */
9517c478bd9Sstevel@tonic-gate int
9527c478bd9Sstevel@tonic-gate mdi_phci_get_path_count(dev_info_t *pdip)
9537c478bd9Sstevel@tonic-gate {
9547c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
9557c478bd9Sstevel@tonic-gate 	int		count = 0;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
9587c478bd9Sstevel@tonic-gate 	if (ph != NULL) {
9597c478bd9Sstevel@tonic-gate 		count = ph->ph_path_count;
9607c478bd9Sstevel@tonic-gate 	}
9617c478bd9Sstevel@tonic-gate 	return (count);
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate /*
9657c478bd9Sstevel@tonic-gate  * i_mdi_phci_lock():
9667c478bd9Sstevel@tonic-gate  *		Lock a pHCI device
9677c478bd9Sstevel@tonic-gate  * Return Values:
9687c478bd9Sstevel@tonic-gate  *		None
9697c478bd9Sstevel@tonic-gate  * Note:
9707c478bd9Sstevel@tonic-gate  *		The default locking order is:
9717c478bd9Sstevel@tonic-gate  *		_NOTE(LOCK_ORDER(mdi_phci::ph_mutex mdi_pathinfo::pi_mutex))
9727c478bd9Sstevel@tonic-gate  *		But there are number of situations where locks need to be
9737c478bd9Sstevel@tonic-gate  *		grabbed in reverse order.  This routine implements try and lock
9747c478bd9Sstevel@tonic-gate  *		mechanism depending on the requested parameter option.
9757c478bd9Sstevel@tonic-gate  */
9767c478bd9Sstevel@tonic-gate static void
9777c478bd9Sstevel@tonic-gate i_mdi_phci_lock(mdi_phci_t *ph, mdi_pathinfo_t *pip)
9787c478bd9Sstevel@tonic-gate {
9797c478bd9Sstevel@tonic-gate 	if (pip) {
9807c478bd9Sstevel@tonic-gate 		/* Reverse locking is requested. */
9817c478bd9Sstevel@tonic-gate 		while (MDI_PHCI_TRYLOCK(ph) == 0) {
9827c478bd9Sstevel@tonic-gate 			/*
9837c478bd9Sstevel@tonic-gate 			 * tryenter failed. Try to grab again
9847c478bd9Sstevel@tonic-gate 			 * after a small delay
9857c478bd9Sstevel@tonic-gate 			 */
9867c478bd9Sstevel@tonic-gate 			MDI_PI_HOLD(pip);
9877c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
9887c478bd9Sstevel@tonic-gate 			delay(1);
9897c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
9907c478bd9Sstevel@tonic-gate 			MDI_PI_RELE(pip);
9917c478bd9Sstevel@tonic-gate 		}
9927c478bd9Sstevel@tonic-gate 	} else {
9937c478bd9Sstevel@tonic-gate 		MDI_PHCI_LOCK(ph);
9947c478bd9Sstevel@tonic-gate 	}
9957c478bd9Sstevel@tonic-gate }
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate /*
9987c478bd9Sstevel@tonic-gate  * i_mdi_phci_unlock():
9997c478bd9Sstevel@tonic-gate  *		Unlock the pHCI component
10007c478bd9Sstevel@tonic-gate  */
10017c478bd9Sstevel@tonic-gate static void
10027c478bd9Sstevel@tonic-gate i_mdi_phci_unlock(mdi_phci_t *ph)
10037c478bd9Sstevel@tonic-gate {
10047c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
10057c478bd9Sstevel@tonic-gate }
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate /*
10087c478bd9Sstevel@tonic-gate  * i_mdi_devinfo_create():
10097c478bd9Sstevel@tonic-gate  *		create client device's devinfo node
10107c478bd9Sstevel@tonic-gate  * Return Values:
10117c478bd9Sstevel@tonic-gate  *		dev_info
10127c478bd9Sstevel@tonic-gate  *		NULL
10137c478bd9Sstevel@tonic-gate  * Notes:
10147c478bd9Sstevel@tonic-gate  */
10157c478bd9Sstevel@tonic-gate static dev_info_t *
10167c478bd9Sstevel@tonic-gate i_mdi_devinfo_create(mdi_vhci_t *vh, char *name, char *guid,
10173c34adc5Sramat 	char **compatible, int ncompatible)
10187c478bd9Sstevel@tonic-gate {
10197c478bd9Sstevel@tonic-gate 	dev_info_t *cdip = NULL;
10207c478bd9Sstevel@tonic-gate 
1021*144dfaa9Scth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	/* Verify for duplicate entry */
10247c478bd9Sstevel@tonic-gate 	cdip = i_mdi_devinfo_find(vh, name, guid);
10257c478bd9Sstevel@tonic-gate 	ASSERT(cdip == NULL);
10267c478bd9Sstevel@tonic-gate 	if (cdip) {
10277c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
10287c478bd9Sstevel@tonic-gate 		    "i_mdi_devinfo_create: client dip %p already exists",
10297c478bd9Sstevel@tonic-gate 			(void *)cdip);
10307c478bd9Sstevel@tonic-gate 	}
10317c478bd9Sstevel@tonic-gate 
10323c34adc5Sramat 	ndi_devi_alloc_sleep(vh->vh_dip, name, DEVI_SID_NODEID, &cdip);
10337c478bd9Sstevel@tonic-gate 	if (cdip == NULL)
10347c478bd9Sstevel@tonic-gate 		goto fail;
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	/*
10377c478bd9Sstevel@tonic-gate 	 * Create component type and Global unique identifier
10387c478bd9Sstevel@tonic-gate 	 * properties
10397c478bd9Sstevel@tonic-gate 	 */
10407c478bd9Sstevel@tonic-gate 	if (ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10417c478bd9Sstevel@tonic-gate 	    MDI_CLIENT_GUID_PROP, guid) != DDI_PROP_SUCCESS) {
10427c478bd9Sstevel@tonic-gate 		goto fail;
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	/* Decorate the node with compatible property */
10467c478bd9Sstevel@tonic-gate 	if (compatible &&
10477c478bd9Sstevel@tonic-gate 	    (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip,
10487c478bd9Sstevel@tonic-gate 	    "compatible", compatible, ncompatible) != DDI_PROP_SUCCESS)) {
10497c478bd9Sstevel@tonic-gate 		goto fail;
10507c478bd9Sstevel@tonic-gate 	}
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	return (cdip);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate fail:
10557c478bd9Sstevel@tonic-gate 	if (cdip) {
10567c478bd9Sstevel@tonic-gate 		(void) ndi_prop_remove_all(cdip);
10577c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(cdip);
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 	return (NULL);
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate /*
10637c478bd9Sstevel@tonic-gate  * i_mdi_devinfo_find():
10647c478bd9Sstevel@tonic-gate  *		Find a matching devinfo node for given client node name
10657c478bd9Sstevel@tonic-gate  *		and its guid.
10667c478bd9Sstevel@tonic-gate  * Return Values:
10677c478bd9Sstevel@tonic-gate  *		Handle to a dev_info node or NULL
10687c478bd9Sstevel@tonic-gate  */
10697c478bd9Sstevel@tonic-gate static dev_info_t *
10707c478bd9Sstevel@tonic-gate i_mdi_devinfo_find(mdi_vhci_t *vh, caddr_t name, char *guid)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	char			*data;
10737c478bd9Sstevel@tonic-gate 	dev_info_t 		*cdip = NULL;
10747c478bd9Sstevel@tonic-gate 	dev_info_t 		*ndip = NULL;
10757c478bd9Sstevel@tonic-gate 	int			circular;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	ndi_devi_enter(vh->vh_dip, &circular);
10787c478bd9Sstevel@tonic-gate 	ndip = (dev_info_t *)DEVI(vh->vh_dip)->devi_child;
10797c478bd9Sstevel@tonic-gate 	while ((cdip = ndip) != NULL) {
10807c478bd9Sstevel@tonic-gate 		ndip = (dev_info_t *)DEVI(cdip)->devi_sibling;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 		if (strcmp(DEVI(cdip)->devi_node_name, name)) {
10837c478bd9Sstevel@tonic-gate 			continue;
10847c478bd9Sstevel@tonic-gate 		}
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, cdip,
10877c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, MDI_CLIENT_GUID_PROP,
10887c478bd9Sstevel@tonic-gate 		    &data) != DDI_PROP_SUCCESS) {
10897c478bd9Sstevel@tonic-gate 			continue;
10907c478bd9Sstevel@tonic-gate 		}
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 		if (strcmp(data, guid) != 0) {
10937c478bd9Sstevel@tonic-gate 			ddi_prop_free(data);
10947c478bd9Sstevel@tonic-gate 			continue;
10957c478bd9Sstevel@tonic-gate 		}
10967c478bd9Sstevel@tonic-gate 		ddi_prop_free(data);
10977c478bd9Sstevel@tonic-gate 		break;
10987c478bd9Sstevel@tonic-gate 	}
10997c478bd9Sstevel@tonic-gate 	ndi_devi_exit(vh->vh_dip, circular);
11007c478bd9Sstevel@tonic-gate 	return (cdip);
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate /*
11047c478bd9Sstevel@tonic-gate  * i_mdi_devinfo_remove():
11057c478bd9Sstevel@tonic-gate  *		Remove a client device node
11067c478bd9Sstevel@tonic-gate  */
11077c478bd9Sstevel@tonic-gate static int
11087c478bd9Sstevel@tonic-gate i_mdi_devinfo_remove(dev_info_t *vdip, dev_info_t *cdip, int flags)
11097c478bd9Sstevel@tonic-gate {
11107c478bd9Sstevel@tonic-gate 	int	rv = MDI_SUCCESS;
1111*144dfaa9Scth 
11127c478bd9Sstevel@tonic-gate 	if (i_mdi_is_child_present(vdip, cdip) == MDI_SUCCESS ||
11137c478bd9Sstevel@tonic-gate 	    (flags & MDI_CLIENT_FLAGS_DEV_NOT_SUPPORTED)) {
11147c478bd9Sstevel@tonic-gate 		rv = ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
11157c478bd9Sstevel@tonic-gate 		if (rv != NDI_SUCCESS) {
11167c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_NOTE, NULL, "!i_mdi_devinfo_remove:"
1117*144dfaa9Scth 			    " failed. cdip = %p\n", (void *)cdip));
11187c478bd9Sstevel@tonic-gate 		}
11197c478bd9Sstevel@tonic-gate 		/*
11207c478bd9Sstevel@tonic-gate 		 * Convert to MDI error code
11217c478bd9Sstevel@tonic-gate 		 */
11227c478bd9Sstevel@tonic-gate 		switch (rv) {
11237c478bd9Sstevel@tonic-gate 		case NDI_SUCCESS:
11247c478bd9Sstevel@tonic-gate 			rv = MDI_SUCCESS;
11257c478bd9Sstevel@tonic-gate 			break;
11267c478bd9Sstevel@tonic-gate 		case NDI_BUSY:
11277c478bd9Sstevel@tonic-gate 			rv = MDI_BUSY;
11287c478bd9Sstevel@tonic-gate 			break;
11297c478bd9Sstevel@tonic-gate 		default:
11307c478bd9Sstevel@tonic-gate 			rv = MDI_FAILURE;
11317c478bd9Sstevel@tonic-gate 			break;
11327c478bd9Sstevel@tonic-gate 		}
11337c478bd9Sstevel@tonic-gate 	}
11347c478bd9Sstevel@tonic-gate 	return (rv);
11357c478bd9Sstevel@tonic-gate }
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate /*
11387c478bd9Sstevel@tonic-gate  * i_devi_get_client()
11397c478bd9Sstevel@tonic-gate  *		Utility function to get mpxio component extensions
11407c478bd9Sstevel@tonic-gate  */
11417c478bd9Sstevel@tonic-gate static mdi_client_t *
11427c478bd9Sstevel@tonic-gate i_devi_get_client(dev_info_t *cdip)
11437c478bd9Sstevel@tonic-gate {
11447c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct = NULL;
1145*144dfaa9Scth 
11467c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(cdip)) {
11477c478bd9Sstevel@tonic-gate 		ct = (mdi_client_t *)DEVI(cdip)->devi_mdi_client;
11487c478bd9Sstevel@tonic-gate 	}
11497c478bd9Sstevel@tonic-gate 	return (ct);
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate /*
11537c478bd9Sstevel@tonic-gate  * i_mdi_is_child_present():
11547c478bd9Sstevel@tonic-gate  *		Search for the presence of client device dev_info node
11557c478bd9Sstevel@tonic-gate  */
11567c478bd9Sstevel@tonic-gate static int
11577c478bd9Sstevel@tonic-gate i_mdi_is_child_present(dev_info_t *vdip, dev_info_t *cdip)
11587c478bd9Sstevel@tonic-gate {
11597c478bd9Sstevel@tonic-gate 	int		rv = MDI_FAILURE;
11607c478bd9Sstevel@tonic-gate 	struct dev_info	*dip;
11617c478bd9Sstevel@tonic-gate 	int		circular;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	ndi_devi_enter(vdip, &circular);
11647c478bd9Sstevel@tonic-gate 	dip = DEVI(vdip)->devi_child;
11657c478bd9Sstevel@tonic-gate 	while (dip) {
11667c478bd9Sstevel@tonic-gate 		if (dip == DEVI(cdip)) {
11677c478bd9Sstevel@tonic-gate 			rv = MDI_SUCCESS;
11687c478bd9Sstevel@tonic-gate 			break;
11697c478bd9Sstevel@tonic-gate 		}
11707c478bd9Sstevel@tonic-gate 		dip = dip->devi_sibling;
11717c478bd9Sstevel@tonic-gate 	}
11727c478bd9Sstevel@tonic-gate 	ndi_devi_exit(vdip, circular);
11737c478bd9Sstevel@tonic-gate 	return (rv);
11747c478bd9Sstevel@tonic-gate }
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate /*
11787c478bd9Sstevel@tonic-gate  * i_mdi_client_lock():
11797c478bd9Sstevel@tonic-gate  *		Grab client component lock
11807c478bd9Sstevel@tonic-gate  * Return Values:
11817c478bd9Sstevel@tonic-gate  *		None
11827c478bd9Sstevel@tonic-gate  * Note:
11837c478bd9Sstevel@tonic-gate  *		The default locking order is:
11847c478bd9Sstevel@tonic-gate  *		_NOTE(LOCK_ORDER(mdi_client::ct_mutex mdi_pathinfo::pi_mutex))
11857c478bd9Sstevel@tonic-gate  *		But there are number of situations where locks need to be
11867c478bd9Sstevel@tonic-gate  *		grabbed in reverse order.  This routine implements try and lock
11877c478bd9Sstevel@tonic-gate  *		mechanism depending on the requested parameter option.
11887c478bd9Sstevel@tonic-gate  */
11897c478bd9Sstevel@tonic-gate static void
11907c478bd9Sstevel@tonic-gate i_mdi_client_lock(mdi_client_t *ct, mdi_pathinfo_t *pip)
11917c478bd9Sstevel@tonic-gate {
11927c478bd9Sstevel@tonic-gate 	if (pip) {
11937c478bd9Sstevel@tonic-gate 		/*
11947c478bd9Sstevel@tonic-gate 		 * Reverse locking is requested.
11957c478bd9Sstevel@tonic-gate 		 */
11967c478bd9Sstevel@tonic-gate 		while (MDI_CLIENT_TRYLOCK(ct) == 0) {
11977c478bd9Sstevel@tonic-gate 			/*
11987c478bd9Sstevel@tonic-gate 			 * tryenter failed. Try to grab again
11997c478bd9Sstevel@tonic-gate 			 * after a small delay
12007c478bd9Sstevel@tonic-gate 			 */
12017c478bd9Sstevel@tonic-gate 			MDI_PI_HOLD(pip);
12027c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
12037c478bd9Sstevel@tonic-gate 			delay(1);
12047c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
12057c478bd9Sstevel@tonic-gate 			MDI_PI_RELE(pip);
12067c478bd9Sstevel@tonic-gate 		}
12077c478bd9Sstevel@tonic-gate 	} else {
12087c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
12097c478bd9Sstevel@tonic-gate 	}
12107c478bd9Sstevel@tonic-gate }
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate /*
12137c478bd9Sstevel@tonic-gate  * i_mdi_client_unlock():
12147c478bd9Sstevel@tonic-gate  *		Unlock a client component
12157c478bd9Sstevel@tonic-gate  */
12167c478bd9Sstevel@tonic-gate static void
12177c478bd9Sstevel@tonic-gate i_mdi_client_unlock(mdi_client_t *ct)
12187c478bd9Sstevel@tonic-gate {
12197c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
12207c478bd9Sstevel@tonic-gate }
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate /*
12237c478bd9Sstevel@tonic-gate  * i_mdi_client_alloc():
12247c478bd9Sstevel@tonic-gate  * 		Allocate and initialize a client structure.  Caller should
1225*144dfaa9Scth  *		hold the vhci client lock.
12267c478bd9Sstevel@tonic-gate  * Return Values:
12277c478bd9Sstevel@tonic-gate  *		Handle to a client component
12287c478bd9Sstevel@tonic-gate  */
12297c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12307c478bd9Sstevel@tonic-gate static mdi_client_t *
12313c34adc5Sramat i_mdi_client_alloc(mdi_vhci_t *vh, char *name, char *lguid)
12327c478bd9Sstevel@tonic-gate {
12337c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
12347c478bd9Sstevel@tonic-gate 
1235*144dfaa9Scth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	/*
12387c478bd9Sstevel@tonic-gate 	 * Allocate and initialize a component structure.
12397c478bd9Sstevel@tonic-gate 	 */
12403c34adc5Sramat 	ct = kmem_zalloc(sizeof (*ct), KM_SLEEP);
12417c478bd9Sstevel@tonic-gate 	mutex_init(&ct->ct_mutex, NULL, MUTEX_DEFAULT, NULL);
12427c478bd9Sstevel@tonic-gate 	ct->ct_hnext = NULL;
12437c478bd9Sstevel@tonic-gate 	ct->ct_hprev = NULL;
12447c478bd9Sstevel@tonic-gate 	ct->ct_dip = NULL;
12457c478bd9Sstevel@tonic-gate 	ct->ct_vhci = vh;
12463c34adc5Sramat 	ct->ct_drvname = kmem_alloc(strlen(name) + 1, KM_SLEEP);
12477c478bd9Sstevel@tonic-gate 	(void) strcpy(ct->ct_drvname, name);
12483c34adc5Sramat 	ct->ct_guid = kmem_alloc(strlen(lguid) + 1, KM_SLEEP);
12497c478bd9Sstevel@tonic-gate 	(void) strcpy(ct->ct_guid, lguid);
12507c478bd9Sstevel@tonic-gate 	ct->ct_cprivate = NULL;
12517c478bd9Sstevel@tonic-gate 	ct->ct_vprivate = NULL;
12527c478bd9Sstevel@tonic-gate 	ct->ct_flags = 0;
12537c478bd9Sstevel@tonic-gate 	ct->ct_state = MDI_CLIENT_STATE_FAILED;
1254*144dfaa9Scth 	MDI_CLIENT_LOCK(ct);
12557c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_OFFLINE(ct);
12567c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_DETACH(ct);
12577c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_POWER_UP(ct);
1258*144dfaa9Scth 	MDI_CLIENT_UNLOCK(ct);
12597c478bd9Sstevel@tonic-gate 	ct->ct_failover_flags = 0;
12607c478bd9Sstevel@tonic-gate 	ct->ct_failover_status = 0;
12617c478bd9Sstevel@tonic-gate 	cv_init(&ct->ct_failover_cv, NULL, CV_DRIVER, NULL);
12627c478bd9Sstevel@tonic-gate 	ct->ct_unstable = 0;
12637c478bd9Sstevel@tonic-gate 	cv_init(&ct->ct_unstable_cv, NULL, CV_DRIVER, NULL);
12647c478bd9Sstevel@tonic-gate 	cv_init(&ct->ct_powerchange_cv, NULL, CV_DRIVER, NULL);
12657c478bd9Sstevel@tonic-gate 	ct->ct_lb = vh->vh_lb;
12663c34adc5Sramat 	ct->ct_lb_args =  kmem_zalloc(sizeof (client_lb_args_t), KM_SLEEP);
12677c478bd9Sstevel@tonic-gate 	ct->ct_lb_args->region_size = LOAD_BALANCE_DEFAULT_REGION_SIZE;
12687c478bd9Sstevel@tonic-gate 	ct->ct_path_count = 0;
12697c478bd9Sstevel@tonic-gate 	ct->ct_path_head = NULL;
12707c478bd9Sstevel@tonic-gate 	ct->ct_path_tail = NULL;
12717c478bd9Sstevel@tonic-gate 	ct->ct_path_last = NULL;
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 	/*
12747c478bd9Sstevel@tonic-gate 	 * Add this client component to our client hash queue
12757c478bd9Sstevel@tonic-gate 	 */
12767c478bd9Sstevel@tonic-gate 	i_mdi_client_enlist_table(vh, ct);
12777c478bd9Sstevel@tonic-gate 	return (ct);
12787c478bd9Sstevel@tonic-gate }
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate /*
12817c478bd9Sstevel@tonic-gate  * i_mdi_client_enlist_table():
12827c478bd9Sstevel@tonic-gate  *		Attach the client device to the client hash table. Caller
1283*144dfaa9Scth  *		should hold the vhci client lock.
12847c478bd9Sstevel@tonic-gate  */
12857c478bd9Sstevel@tonic-gate static void
12867c478bd9Sstevel@tonic-gate i_mdi_client_enlist_table(mdi_vhci_t *vh, mdi_client_t *ct)
12877c478bd9Sstevel@tonic-gate {
12887c478bd9Sstevel@tonic-gate 	int 			index;
12897c478bd9Sstevel@tonic-gate 	struct client_hash	*head;
12907c478bd9Sstevel@tonic-gate 
1291*144dfaa9Scth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
1292*144dfaa9Scth 
12937c478bd9Sstevel@tonic-gate 	index = i_mdi_get_hash_key(ct->ct_guid);
12947c478bd9Sstevel@tonic-gate 	head = &vh->vh_client_table[index];
12957c478bd9Sstevel@tonic-gate 	ct->ct_hnext = (mdi_client_t *)head->ct_hash_head;
12967c478bd9Sstevel@tonic-gate 	head->ct_hash_head = ct;
12977c478bd9Sstevel@tonic-gate 	head->ct_hash_count++;
12987c478bd9Sstevel@tonic-gate 	vh->vh_client_count++;
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate /*
13027c478bd9Sstevel@tonic-gate  * i_mdi_client_delist_table():
13037c478bd9Sstevel@tonic-gate  *		Attach the client device to the client hash table.
1304*144dfaa9Scth  *		Caller should hold the vhci client lock.
13057c478bd9Sstevel@tonic-gate  */
13067c478bd9Sstevel@tonic-gate static void
13077c478bd9Sstevel@tonic-gate i_mdi_client_delist_table(mdi_vhci_t *vh, mdi_client_t *ct)
13087c478bd9Sstevel@tonic-gate {
13097c478bd9Sstevel@tonic-gate 	int			index;
13107c478bd9Sstevel@tonic-gate 	char			*guid;
13117c478bd9Sstevel@tonic-gate 	struct client_hash 	*head;
13127c478bd9Sstevel@tonic-gate 	mdi_client_t		*next;
13137c478bd9Sstevel@tonic-gate 	mdi_client_t		*last;
13147c478bd9Sstevel@tonic-gate 
1315*144dfaa9Scth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
1316*144dfaa9Scth 
13177c478bd9Sstevel@tonic-gate 	guid = ct->ct_guid;
13187c478bd9Sstevel@tonic-gate 	index = i_mdi_get_hash_key(guid);
13197c478bd9Sstevel@tonic-gate 	head = &vh->vh_client_table[index];
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	last = NULL;
13227c478bd9Sstevel@tonic-gate 	next = (mdi_client_t *)head->ct_hash_head;
13237c478bd9Sstevel@tonic-gate 	while (next != NULL) {
13247c478bd9Sstevel@tonic-gate 		if (next == ct) {
13257c478bd9Sstevel@tonic-gate 			break;
13267c478bd9Sstevel@tonic-gate 		}
13277c478bd9Sstevel@tonic-gate 		last = next;
13287c478bd9Sstevel@tonic-gate 		next = next->ct_hnext;
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	if (next) {
13327c478bd9Sstevel@tonic-gate 		head->ct_hash_count--;
13337c478bd9Sstevel@tonic-gate 		if (last == NULL) {
13347c478bd9Sstevel@tonic-gate 			head->ct_hash_head = ct->ct_hnext;
13357c478bd9Sstevel@tonic-gate 		} else {
13367c478bd9Sstevel@tonic-gate 			last->ct_hnext = ct->ct_hnext;
13377c478bd9Sstevel@tonic-gate 		}
13387c478bd9Sstevel@tonic-gate 		ct->ct_hnext = NULL;
13397c478bd9Sstevel@tonic-gate 		vh->vh_client_count--;
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate }
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate /*
13457c478bd9Sstevel@tonic-gate  * i_mdi_client_free():
13467c478bd9Sstevel@tonic-gate  *		Free a client component
13477c478bd9Sstevel@tonic-gate  */
13487c478bd9Sstevel@tonic-gate static int
13497c478bd9Sstevel@tonic-gate i_mdi_client_free(mdi_vhci_t *vh, mdi_client_t *ct)
13507c478bd9Sstevel@tonic-gate {
13517c478bd9Sstevel@tonic-gate 	int		rv = MDI_SUCCESS;
13527c478bd9Sstevel@tonic-gate 	int		flags = ct->ct_flags;
13537c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
13547c478bd9Sstevel@tonic-gate 	dev_info_t	*vdip;
13557c478bd9Sstevel@tonic-gate 
1356*144dfaa9Scth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
1357*144dfaa9Scth 
13587c478bd9Sstevel@tonic-gate 	vdip = vh->vh_dip;
13597c478bd9Sstevel@tonic-gate 	cdip = ct->ct_dip;
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	(void) ndi_prop_remove(DDI_DEV_T_NONE, cdip, MDI_CLIENT_GUID_PROP);
13627c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_component &= ~MDI_COMPONENT_CLIENT;
13637c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_client = NULL;
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	/*
13667c478bd9Sstevel@tonic-gate 	 * Clear out back ref. to dev_info_t node
13677c478bd9Sstevel@tonic-gate 	 */
13687c478bd9Sstevel@tonic-gate 	ct->ct_dip = NULL;
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	/*
13717c478bd9Sstevel@tonic-gate 	 * Remove this client from our hash queue
13727c478bd9Sstevel@tonic-gate 	 */
13737c478bd9Sstevel@tonic-gate 	i_mdi_client_delist_table(vh, ct);
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	/*
13767c478bd9Sstevel@tonic-gate 	 * Uninitialize and free the component
13777c478bd9Sstevel@tonic-gate 	 */
13787c478bd9Sstevel@tonic-gate 	kmem_free(ct->ct_drvname, strlen(ct->ct_drvname) + 1);
13797c478bd9Sstevel@tonic-gate 	kmem_free(ct->ct_guid, strlen(ct->ct_guid) + 1);
13807c478bd9Sstevel@tonic-gate 	kmem_free(ct->ct_lb_args, sizeof (client_lb_args_t));
13817c478bd9Sstevel@tonic-gate 	cv_destroy(&ct->ct_failover_cv);
13827c478bd9Sstevel@tonic-gate 	cv_destroy(&ct->ct_unstable_cv);
13837c478bd9Sstevel@tonic-gate 	cv_destroy(&ct->ct_powerchange_cv);
13847c478bd9Sstevel@tonic-gate 	mutex_destroy(&ct->ct_mutex);
13857c478bd9Sstevel@tonic-gate 	kmem_free(ct, sizeof (*ct));
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	if (cdip != NULL) {
1388*144dfaa9Scth 		MDI_VHCI_CLIENT_UNLOCK(vh);
13897c478bd9Sstevel@tonic-gate 		(void) i_mdi_devinfo_remove(vdip, cdip, flags);
1390*144dfaa9Scth 		MDI_VHCI_CLIENT_LOCK(vh);
13917c478bd9Sstevel@tonic-gate 	}
13927c478bd9Sstevel@tonic-gate 	return (rv);
13937c478bd9Sstevel@tonic-gate }
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate /*
13967c478bd9Sstevel@tonic-gate  * i_mdi_client_find():
13977c478bd9Sstevel@tonic-gate  * 		Find the client structure corresponding to a given guid
1398*144dfaa9Scth  *		Caller should hold the vhci client lock.
13997c478bd9Sstevel@tonic-gate  */
14007c478bd9Sstevel@tonic-gate static mdi_client_t *
14013c34adc5Sramat i_mdi_client_find(mdi_vhci_t *vh, char *cname, char *guid)
14027c478bd9Sstevel@tonic-gate {
14037c478bd9Sstevel@tonic-gate 	int			index;
14047c478bd9Sstevel@tonic-gate 	struct client_hash	*head;
14057c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
14067c478bd9Sstevel@tonic-gate 
1407*144dfaa9Scth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
1408*144dfaa9Scth 
14097c478bd9Sstevel@tonic-gate 	index = i_mdi_get_hash_key(guid);
14107c478bd9Sstevel@tonic-gate 	head = &vh->vh_client_table[index];
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	ct = head->ct_hash_head;
14137c478bd9Sstevel@tonic-gate 	while (ct != NULL) {
14143c34adc5Sramat 		if (strcmp(ct->ct_guid, guid) == 0 &&
14153c34adc5Sramat 		    (cname == NULL || strcmp(ct->ct_drvname, cname) == 0)) {
14167c478bd9Sstevel@tonic-gate 			break;
14177c478bd9Sstevel@tonic-gate 		}
14187c478bd9Sstevel@tonic-gate 		ct = ct->ct_hnext;
14197c478bd9Sstevel@tonic-gate 	}
14207c478bd9Sstevel@tonic-gate 	return (ct);
14217c478bd9Sstevel@tonic-gate }
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate /*
14247c478bd9Sstevel@tonic-gate  * i_mdi_client_update_state():
14257c478bd9Sstevel@tonic-gate  *		Compute and update client device state
14267c478bd9Sstevel@tonic-gate  * Notes:
14277c478bd9Sstevel@tonic-gate  *		A client device can be in any of three possible states:
14287c478bd9Sstevel@tonic-gate  *
14297c478bd9Sstevel@tonic-gate  *		MDI_CLIENT_STATE_OPTIMAL - Client in optimal state with more
14307c478bd9Sstevel@tonic-gate  *		one online/standby paths. Can tolerate failures.
14317c478bd9Sstevel@tonic-gate  *		MDI_CLIENT_STATE_DEGRADED - Client device in degraded state with
14327c478bd9Sstevel@tonic-gate  *		no alternate paths available as standby. A failure on the online
14337c478bd9Sstevel@tonic-gate  *		would result in loss of access to device data.
14347c478bd9Sstevel@tonic-gate  *		MDI_CLIENT_STATE_FAILED - Client device in failed state with
14357c478bd9Sstevel@tonic-gate  *		no paths available to access the device.
14367c478bd9Sstevel@tonic-gate  */
14377c478bd9Sstevel@tonic-gate static void
14387c478bd9Sstevel@tonic-gate i_mdi_client_update_state(mdi_client_t *ct)
14397c478bd9Sstevel@tonic-gate {
14407c478bd9Sstevel@tonic-gate 	int state;
1441*144dfaa9Scth 
1442*144dfaa9Scth 	ASSERT(MDI_CLIENT_LOCKED(ct));
14437c478bd9Sstevel@tonic-gate 	state = i_mdi_client_compute_state(ct, NULL);
14447c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_STATE(ct, state);
14457c478bd9Sstevel@tonic-gate }
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate /*
14487c478bd9Sstevel@tonic-gate  * i_mdi_client_compute_state():
14497c478bd9Sstevel@tonic-gate  *		Compute client device state
14507c478bd9Sstevel@tonic-gate  *
14517c478bd9Sstevel@tonic-gate  *		mdi_phci_t *	Pointer to pHCI structure which should
14527c478bd9Sstevel@tonic-gate  *				while computing the new value.  Used by
14537c478bd9Sstevel@tonic-gate  *				i_mdi_phci_offline() to find the new
14547c478bd9Sstevel@tonic-gate  *				client state after DR of a pHCI.
14557c478bd9Sstevel@tonic-gate  */
14567c478bd9Sstevel@tonic-gate static int
14577c478bd9Sstevel@tonic-gate i_mdi_client_compute_state(mdi_client_t *ct, mdi_phci_t *ph)
14587c478bd9Sstevel@tonic-gate {
14597c478bd9Sstevel@tonic-gate 	int		state;
14607c478bd9Sstevel@tonic-gate 	int		online_count = 0;
14617c478bd9Sstevel@tonic-gate 	int		standby_count = 0;
14627c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip, *next;
14637c478bd9Sstevel@tonic-gate 
1464*144dfaa9Scth 	ASSERT(MDI_CLIENT_LOCKED(ct));
14657c478bd9Sstevel@tonic-gate 	pip = ct->ct_path_head;
14667c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
14677c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
14687c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
14697c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_phci == ph) {
14707c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
14717c478bd9Sstevel@tonic-gate 			pip = next;
14727c478bd9Sstevel@tonic-gate 			continue;
14737c478bd9Sstevel@tonic-gate 		}
1474*144dfaa9Scth 
14757c478bd9Sstevel@tonic-gate 		if ((MDI_PI(pip)->pi_state & MDI_PATHINFO_STATE_MASK)
14767c478bd9Sstevel@tonic-gate 				== MDI_PATHINFO_STATE_ONLINE)
14777c478bd9Sstevel@tonic-gate 			online_count++;
14787c478bd9Sstevel@tonic-gate 		else if ((MDI_PI(pip)->pi_state & MDI_PATHINFO_STATE_MASK)
14797c478bd9Sstevel@tonic-gate 				== MDI_PATHINFO_STATE_STANDBY)
14807c478bd9Sstevel@tonic-gate 			standby_count++;
14817c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
14827c478bd9Sstevel@tonic-gate 		pip = next;
14837c478bd9Sstevel@tonic-gate 	}
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	if (online_count == 0) {
14867c478bd9Sstevel@tonic-gate 		if (standby_count == 0) {
14877c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_FAILED;
14887c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_NOTE, NULL, "!client state: failed"
1489*144dfaa9Scth 			    " ct = %p\n", (void *)ct));
14907c478bd9Sstevel@tonic-gate 		} else if (standby_count == 1) {
14917c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_DEGRADED;
14927c478bd9Sstevel@tonic-gate 		} else {
14937c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_OPTIMAL;
14947c478bd9Sstevel@tonic-gate 		}
14957c478bd9Sstevel@tonic-gate 	} else if (online_count == 1) {
14967c478bd9Sstevel@tonic-gate 		if (standby_count == 0) {
14977c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_DEGRADED;
14987c478bd9Sstevel@tonic-gate 		} else {
14997c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_OPTIMAL;
15007c478bd9Sstevel@tonic-gate 		}
15017c478bd9Sstevel@tonic-gate 	} else {
15027c478bd9Sstevel@tonic-gate 		state = MDI_CLIENT_STATE_OPTIMAL;
15037c478bd9Sstevel@tonic-gate 	}
15047c478bd9Sstevel@tonic-gate 	return (state);
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate /*
15087c478bd9Sstevel@tonic-gate  * i_mdi_client2devinfo():
15097c478bd9Sstevel@tonic-gate  *		Utility function
15107c478bd9Sstevel@tonic-gate  */
15117c478bd9Sstevel@tonic-gate dev_info_t *
15127c478bd9Sstevel@tonic-gate i_mdi_client2devinfo(mdi_client_t *ct)
15137c478bd9Sstevel@tonic-gate {
15147c478bd9Sstevel@tonic-gate 	return (ct->ct_dip);
15157c478bd9Sstevel@tonic-gate }
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate /*
15187c478bd9Sstevel@tonic-gate  * mdi_client_path2_devinfo():
15197c478bd9Sstevel@tonic-gate  * 		Given the parent devinfo and child devfs pathname, search for
15207c478bd9Sstevel@tonic-gate  *		a valid devfs node handle.
15217c478bd9Sstevel@tonic-gate  */
15227c478bd9Sstevel@tonic-gate dev_info_t *
15237c478bd9Sstevel@tonic-gate mdi_client_path2devinfo(dev_info_t *vdip, char *pathname)
15247c478bd9Sstevel@tonic-gate {
15257c478bd9Sstevel@tonic-gate 	dev_info_t 	*cdip = NULL;
15267c478bd9Sstevel@tonic-gate 	dev_info_t 	*ndip = NULL;
15277c478bd9Sstevel@tonic-gate 	char		*temp_pathname;
15287c478bd9Sstevel@tonic-gate 	int		circular;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	/*
15317c478bd9Sstevel@tonic-gate 	 * Allocate temp buffer
15327c478bd9Sstevel@tonic-gate 	 */
15337c478bd9Sstevel@tonic-gate 	temp_pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	/*
15367c478bd9Sstevel@tonic-gate 	 * Lock parent against changes
15377c478bd9Sstevel@tonic-gate 	 */
15387c478bd9Sstevel@tonic-gate 	ndi_devi_enter(vdip, &circular);
15397c478bd9Sstevel@tonic-gate 	ndip = (dev_info_t *)DEVI(vdip)->devi_child;
15407c478bd9Sstevel@tonic-gate 	while ((cdip = ndip) != NULL) {
15417c478bd9Sstevel@tonic-gate 		ndip = (dev_info_t *)DEVI(cdip)->devi_sibling;
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 		*temp_pathname = '\0';
15447c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(cdip, temp_pathname);
15457c478bd9Sstevel@tonic-gate 		if (strcmp(temp_pathname, pathname) == 0) {
15467c478bd9Sstevel@tonic-gate 			break;
15477c478bd9Sstevel@tonic-gate 		}
15487c478bd9Sstevel@tonic-gate 	}
15497c478bd9Sstevel@tonic-gate 	/*
15507c478bd9Sstevel@tonic-gate 	 * Release devinfo lock
15517c478bd9Sstevel@tonic-gate 	 */
15527c478bd9Sstevel@tonic-gate 	ndi_devi_exit(vdip, circular);
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	/*
15557c478bd9Sstevel@tonic-gate 	 * Free the temp buffer
15567c478bd9Sstevel@tonic-gate 	 */
15577c478bd9Sstevel@tonic-gate 	kmem_free(temp_pathname, MAXPATHLEN);
15587c478bd9Sstevel@tonic-gate 	return (cdip);
15597c478bd9Sstevel@tonic-gate }
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate /*
15627c478bd9Sstevel@tonic-gate  * mdi_client_get_path_count():
15637c478bd9Sstevel@tonic-gate  * 		Utility function to get number of path information nodes
15647c478bd9Sstevel@tonic-gate  *		associated with a given client device.
15657c478bd9Sstevel@tonic-gate  */
15667c478bd9Sstevel@tonic-gate int
15677c478bd9Sstevel@tonic-gate mdi_client_get_path_count(dev_info_t *cdip)
15687c478bd9Sstevel@tonic-gate {
15697c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
15707c478bd9Sstevel@tonic-gate 	int		count = 0;
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
15737c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
15747c478bd9Sstevel@tonic-gate 		count = ct->ct_path_count;
15757c478bd9Sstevel@tonic-gate 	}
15767c478bd9Sstevel@tonic-gate 	return (count);
15777c478bd9Sstevel@tonic-gate }
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate /*
15817c478bd9Sstevel@tonic-gate  * i_mdi_get_hash_key():
15827c478bd9Sstevel@tonic-gate  * 		Create a hash using strings as keys
15837c478bd9Sstevel@tonic-gate  *
15847c478bd9Sstevel@tonic-gate  */
15857c478bd9Sstevel@tonic-gate static int
15867c478bd9Sstevel@tonic-gate i_mdi_get_hash_key(char *str)
15877c478bd9Sstevel@tonic-gate {
15887c478bd9Sstevel@tonic-gate 	uint32_t	g, hash = 0;
15897c478bd9Sstevel@tonic-gate 	char		*p;
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	for (p = str; *p != '\0'; p++) {
15927c478bd9Sstevel@tonic-gate 		g = *p;
15937c478bd9Sstevel@tonic-gate 		hash += g;
15947c478bd9Sstevel@tonic-gate 	}
15957c478bd9Sstevel@tonic-gate 	return (hash % (CLIENT_HASH_TABLE_SIZE - 1));
15967c478bd9Sstevel@tonic-gate }
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate /*
15997c478bd9Sstevel@tonic-gate  * mdi_get_lb_policy():
16007c478bd9Sstevel@tonic-gate  * 		Get current load balancing policy for a given client device
16017c478bd9Sstevel@tonic-gate  */
16027c478bd9Sstevel@tonic-gate client_lb_t
16037c478bd9Sstevel@tonic-gate mdi_get_lb_policy(dev_info_t *cdip)
16047c478bd9Sstevel@tonic-gate {
16057c478bd9Sstevel@tonic-gate 	client_lb_t	lb = LOAD_BALANCE_NONE;
16067c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
16097c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
16107c478bd9Sstevel@tonic-gate 		lb = ct->ct_lb;
16117c478bd9Sstevel@tonic-gate 	}
16127c478bd9Sstevel@tonic-gate 	return (lb);
16137c478bd9Sstevel@tonic-gate }
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate /*
16167c478bd9Sstevel@tonic-gate  * mdi_set_lb_region_size():
16177c478bd9Sstevel@tonic-gate  * 		Set current region size for the load-balance
16187c478bd9Sstevel@tonic-gate  */
16197c478bd9Sstevel@tonic-gate int
16207c478bd9Sstevel@tonic-gate mdi_set_lb_region_size(dev_info_t *cdip, int region_size)
16217c478bd9Sstevel@tonic-gate {
16227c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
16237c478bd9Sstevel@tonic-gate 	int		rv = MDI_FAILURE;
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
16267c478bd9Sstevel@tonic-gate 	if (ct != NULL && ct->ct_lb_args != NULL) {
16277c478bd9Sstevel@tonic-gate 		ct->ct_lb_args->region_size = region_size;
16287c478bd9Sstevel@tonic-gate 		rv = MDI_SUCCESS;
16297c478bd9Sstevel@tonic-gate 	}
16307c478bd9Sstevel@tonic-gate 	return (rv);
16317c478bd9Sstevel@tonic-gate }
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate /*
16347c478bd9Sstevel@tonic-gate  * mdi_Set_lb_policy():
16357c478bd9Sstevel@tonic-gate  * 		Set current load balancing policy for a given client device
16367c478bd9Sstevel@tonic-gate  */
16377c478bd9Sstevel@tonic-gate int
16387c478bd9Sstevel@tonic-gate mdi_set_lb_policy(dev_info_t *cdip, client_lb_t lb)
16397c478bd9Sstevel@tonic-gate {
16407c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
16417c478bd9Sstevel@tonic-gate 	int		rv = MDI_FAILURE;
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
16447c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
16457c478bd9Sstevel@tonic-gate 		ct->ct_lb = lb;
16467c478bd9Sstevel@tonic-gate 		rv = MDI_SUCCESS;
16477c478bd9Sstevel@tonic-gate 	}
16487c478bd9Sstevel@tonic-gate 	return (rv);
16497c478bd9Sstevel@tonic-gate }
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate /*
16527c478bd9Sstevel@tonic-gate  * mdi_failover():
16537c478bd9Sstevel@tonic-gate  *		failover function called by the vHCI drivers to initiate
16547c478bd9Sstevel@tonic-gate  *		a failover operation.  This is typically due to non-availability
16557c478bd9Sstevel@tonic-gate  *		of online paths to route I/O requests.  Failover can be
16567c478bd9Sstevel@tonic-gate  *		triggered through user application also.
16577c478bd9Sstevel@tonic-gate  *
16587c478bd9Sstevel@tonic-gate  *		The vHCI driver calls mdi_failover() to initiate a failover
16597c478bd9Sstevel@tonic-gate  *		operation. mdi_failover() calls back into the vHCI driver's
16607c478bd9Sstevel@tonic-gate  *		vo_failover() entry point to perform the actual failover
16617c478bd9Sstevel@tonic-gate  *		operation.  The reason for requiring the vHCI driver to
16627c478bd9Sstevel@tonic-gate  *		initiate failover by calling mdi_failover(), instead of directly
16637c478bd9Sstevel@tonic-gate  *		executing vo_failover() itself, is to ensure that the mdi
16647c478bd9Sstevel@tonic-gate  *		framework can keep track of the client state properly.
16657c478bd9Sstevel@tonic-gate  *		Additionally, mdi_failover() provides as a convenience the
16667c478bd9Sstevel@tonic-gate  *		option of performing the failover operation synchronously or
16677c478bd9Sstevel@tonic-gate  *		asynchronously
16687c478bd9Sstevel@tonic-gate  *
16697c478bd9Sstevel@tonic-gate  *		Upon successful completion of the failover operation, the
16707c478bd9Sstevel@tonic-gate  *		paths that were previously ONLINE will be in the STANDBY state,
16717c478bd9Sstevel@tonic-gate  *		and the newly activated paths will be in the ONLINE state.
16727c478bd9Sstevel@tonic-gate  *
16737c478bd9Sstevel@tonic-gate  *		The flags modifier determines whether the activation is done
16747c478bd9Sstevel@tonic-gate  *		synchronously: MDI_FAILOVER_SYNC
16757c478bd9Sstevel@tonic-gate  * Return Values:
16767c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
16777c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
16787c478bd9Sstevel@tonic-gate  *		MDI_BUSY
16797c478bd9Sstevel@tonic-gate  */
16807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16817c478bd9Sstevel@tonic-gate int
16827c478bd9Sstevel@tonic-gate mdi_failover(dev_info_t *vdip, dev_info_t *cdip, int flags)
16837c478bd9Sstevel@tonic-gate {
16847c478bd9Sstevel@tonic-gate 	int			rv;
16857c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
16887c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
16897c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
16907c478bd9Sstevel@tonic-gate 		/* cdip is not a valid client device. Nothing more to do. */
16917c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
16927c478bd9Sstevel@tonic-gate 	}
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_PATH_FREE_IN_PROGRESS(ct)) {
16977c478bd9Sstevel@tonic-gate 		/* A path to the client is being freed */
16987c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
16997c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
17007c478bd9Sstevel@tonic-gate 	}
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_FAILED(ct)) {
17047c478bd9Sstevel@tonic-gate 		/*
17057c478bd9Sstevel@tonic-gate 		 * Client is in failed state. Nothing more to do.
17067c478bd9Sstevel@tonic-gate 		 */
17077c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
17087c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
17097c478bd9Sstevel@tonic-gate 	}
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
17127c478bd9Sstevel@tonic-gate 		/*
17137c478bd9Sstevel@tonic-gate 		 * Failover is already in progress; return BUSY
17147c478bd9Sstevel@tonic-gate 		 */
17157c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
17167c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
17177c478bd9Sstevel@tonic-gate 	}
17187c478bd9Sstevel@tonic-gate 	/*
17197c478bd9Sstevel@tonic-gate 	 * Make sure that mdi_pathinfo node state changes are processed.
17207c478bd9Sstevel@tonic-gate 	 * We do not allow failovers to progress while client path state
17217c478bd9Sstevel@tonic-gate 	 * changes are in progress
17227c478bd9Sstevel@tonic-gate 	 */
17237c478bd9Sstevel@tonic-gate 	if (ct->ct_unstable) {
17247c478bd9Sstevel@tonic-gate 		if (flags == MDI_FAILOVER_ASYNC) {
17257c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
17267c478bd9Sstevel@tonic-gate 			return (MDI_BUSY);
17277c478bd9Sstevel@tonic-gate 		} else {
17287c478bd9Sstevel@tonic-gate 			while (ct->ct_unstable)
17297c478bd9Sstevel@tonic-gate 				cv_wait(&ct->ct_unstable_cv, &ct->ct_mutex);
17307c478bd9Sstevel@tonic-gate 		}
17317c478bd9Sstevel@tonic-gate 	}
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 	/*
17347c478bd9Sstevel@tonic-gate 	 * Client device is in stable state. Before proceeding, perform sanity
17357c478bd9Sstevel@tonic-gate 	 * checks again.
17367c478bd9Sstevel@tonic-gate 	 */
17377c478bd9Sstevel@tonic-gate 	if ((MDI_CLIENT_IS_DETACHED(ct)) || (MDI_CLIENT_IS_FAILED(ct)) ||
1738737d277aScth 	    (!i_ddi_devi_attached(ct->ct_dip))) {
17397c478bd9Sstevel@tonic-gate 		/*
17407c478bd9Sstevel@tonic-gate 		 * Client is in failed state. Nothing more to do.
17417c478bd9Sstevel@tonic-gate 		 */
17427c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
17437c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
17447c478bd9Sstevel@tonic-gate 	}
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	/*
17477c478bd9Sstevel@tonic-gate 	 * Set the client state as failover in progress.
17487c478bd9Sstevel@tonic-gate 	 */
17497c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_FAILOVER_IN_PROGRESS(ct);
17507c478bd9Sstevel@tonic-gate 	ct->ct_failover_flags = flags;
17517c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	if (flags == MDI_FAILOVER_ASYNC) {
17547c478bd9Sstevel@tonic-gate 		/*
17557c478bd9Sstevel@tonic-gate 		 * Submit the initiate failover request via CPR safe
17567c478bd9Sstevel@tonic-gate 		 * taskq threads.
17577c478bd9Sstevel@tonic-gate 		 */
17587c478bd9Sstevel@tonic-gate 		(void) taskq_dispatch(mdi_taskq, (task_func_t *)i_mdi_failover,
17597c478bd9Sstevel@tonic-gate 		    ct, KM_SLEEP);
17607c478bd9Sstevel@tonic-gate 		return (MDI_ACCEPT);
17617c478bd9Sstevel@tonic-gate 	} else {
17627c478bd9Sstevel@tonic-gate 		/*
17637c478bd9Sstevel@tonic-gate 		 * Synchronous failover mode.  Typically invoked from the user
17647c478bd9Sstevel@tonic-gate 		 * land.
17657c478bd9Sstevel@tonic-gate 		 */
17667c478bd9Sstevel@tonic-gate 		rv = i_mdi_failover(ct);
17677c478bd9Sstevel@tonic-gate 	}
17687c478bd9Sstevel@tonic-gate 	return (rv);
17697c478bd9Sstevel@tonic-gate }
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate /*
17727c478bd9Sstevel@tonic-gate  * i_mdi_failover():
17737c478bd9Sstevel@tonic-gate  *		internal failover function. Invokes vHCI drivers failover
17747c478bd9Sstevel@tonic-gate  *		callback function and process the failover status
17757c478bd9Sstevel@tonic-gate  * Return Values:
17767c478bd9Sstevel@tonic-gate  *		None
17777c478bd9Sstevel@tonic-gate  *
17787c478bd9Sstevel@tonic-gate  * Note: A client device in failover state can not be detached or freed.
17797c478bd9Sstevel@tonic-gate  */
17807c478bd9Sstevel@tonic-gate static int
17817c478bd9Sstevel@tonic-gate i_mdi_failover(void *arg)
17827c478bd9Sstevel@tonic-gate {
17837c478bd9Sstevel@tonic-gate 	int		rv = MDI_SUCCESS;
17847c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct = (mdi_client_t *)arg;
17857c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = ct->ct_vhci;
17867c478bd9Sstevel@tonic-gate 
1787*144dfaa9Scth 	ASSERT(!MDI_CLIENT_LOCKED(ct));
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	if (vh->vh_ops->vo_failover != NULL) {
17907c478bd9Sstevel@tonic-gate 		/*
17917c478bd9Sstevel@tonic-gate 		 * Call vHCI drivers callback routine
17927c478bd9Sstevel@tonic-gate 		 */
17937c478bd9Sstevel@tonic-gate 		rv = (*vh->vh_ops->vo_failover)(vh->vh_dip, ct->ct_dip,
17947c478bd9Sstevel@tonic-gate 		    ct->ct_failover_flags);
17957c478bd9Sstevel@tonic-gate 	}
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
17987c478bd9Sstevel@tonic-gate 	MDI_CLIENT_CLEAR_FAILOVER_IN_PROGRESS(ct);
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	/*
18017c478bd9Sstevel@tonic-gate 	 * Save the failover return status
18027c478bd9Sstevel@tonic-gate 	 */
18037c478bd9Sstevel@tonic-gate 	ct->ct_failover_status = rv;
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	/*
18067c478bd9Sstevel@tonic-gate 	 * As a result of failover, client status would have been changed.
18077c478bd9Sstevel@tonic-gate 	 * Update the client state and wake up anyone waiting on this client
18087c478bd9Sstevel@tonic-gate 	 * device.
18097c478bd9Sstevel@tonic-gate 	 */
18107c478bd9Sstevel@tonic-gate 	i_mdi_client_update_state(ct);
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	cv_broadcast(&ct->ct_failover_cv);
18137c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
18147c478bd9Sstevel@tonic-gate 	return (rv);
18157c478bd9Sstevel@tonic-gate }
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate /*
18187c478bd9Sstevel@tonic-gate  * Load balancing is logical block.
18197c478bd9Sstevel@tonic-gate  * IOs within the range described by region_size
18207c478bd9Sstevel@tonic-gate  * would go on the same path. This would improve the
18217c478bd9Sstevel@tonic-gate  * performance by cache-hit on some of the RAID devices.
18227c478bd9Sstevel@tonic-gate  * Search only for online paths(At some point we
18237c478bd9Sstevel@tonic-gate  * may want to balance across target ports).
18247c478bd9Sstevel@tonic-gate  * If no paths are found then default to round-robin.
18257c478bd9Sstevel@tonic-gate  */
18267c478bd9Sstevel@tonic-gate static int
18277c478bd9Sstevel@tonic-gate i_mdi_lba_lb(mdi_client_t *ct, mdi_pathinfo_t **ret_pip, struct buf *bp)
18287c478bd9Sstevel@tonic-gate {
18297c478bd9Sstevel@tonic-gate 	int		path_index = -1;
18307c478bd9Sstevel@tonic-gate 	int		online_path_count = 0;
18317c478bd9Sstevel@tonic-gate 	int		online_nonpref_path_count = 0;
18327c478bd9Sstevel@tonic-gate 	int 		region_size = ct->ct_lb_args->region_size;
18337c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
18347c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
18357c478bd9Sstevel@tonic-gate 	int		preferred, path_cnt;
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	pip = ct->ct_path_head;
18387c478bd9Sstevel@tonic-gate 	while (pip) {
18397c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
18407c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_state ==
18417c478bd9Sstevel@tonic-gate 		    MDI_PATHINFO_STATE_ONLINE && MDI_PI(pip)->pi_preferred) {
18427c478bd9Sstevel@tonic-gate 			online_path_count++;
18437c478bd9Sstevel@tonic-gate 		} else if (MDI_PI(pip)->pi_state ==
18447c478bd9Sstevel@tonic-gate 		    MDI_PATHINFO_STATE_ONLINE && !MDI_PI(pip)->pi_preferred) {
18457c478bd9Sstevel@tonic-gate 			online_nonpref_path_count++;
18467c478bd9Sstevel@tonic-gate 		}
18477c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)
18487c478bd9Sstevel@tonic-gate 		    MDI_PI(pip)->pi_client_link;
18497c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
18507c478bd9Sstevel@tonic-gate 		pip = next;
18517c478bd9Sstevel@tonic-gate 	}
18527c478bd9Sstevel@tonic-gate 	/* if found any online/preferred then use this type */
18537c478bd9Sstevel@tonic-gate 	if (online_path_count > 0) {
18547c478bd9Sstevel@tonic-gate 		path_cnt = online_path_count;
18557c478bd9Sstevel@tonic-gate 		preferred = 1;
18567c478bd9Sstevel@tonic-gate 	} else if (online_nonpref_path_count > 0) {
18577c478bd9Sstevel@tonic-gate 		path_cnt = online_nonpref_path_count;
18587c478bd9Sstevel@tonic-gate 		preferred = 0;
18597c478bd9Sstevel@tonic-gate 	} else {
18607c478bd9Sstevel@tonic-gate 		path_cnt = 0;
18617c478bd9Sstevel@tonic-gate 	}
18627c478bd9Sstevel@tonic-gate 	if (path_cnt) {
18637c478bd9Sstevel@tonic-gate 		path_index = (bp->b_blkno >> region_size) % path_cnt;
18647c478bd9Sstevel@tonic-gate 		pip = ct->ct_path_head;
18657c478bd9Sstevel@tonic-gate 		while (pip && path_index != -1) {
18667c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
18677c478bd9Sstevel@tonic-gate 			if (path_index == 0 &&
18687c478bd9Sstevel@tonic-gate 			    (MDI_PI(pip)->pi_state ==
18697c478bd9Sstevel@tonic-gate 			    MDI_PATHINFO_STATE_ONLINE) &&
18707c478bd9Sstevel@tonic-gate 				MDI_PI(pip)->pi_preferred == preferred) {
18717c478bd9Sstevel@tonic-gate 				MDI_PI_HOLD(pip);
18727c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
18737c478bd9Sstevel@tonic-gate 				*ret_pip = pip;
18747c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
18757c478bd9Sstevel@tonic-gate 			}
18767c478bd9Sstevel@tonic-gate 			path_index --;
18777c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)
18787c478bd9Sstevel@tonic-gate 			    MDI_PI(pip)->pi_client_link;
18797c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
18807c478bd9Sstevel@tonic-gate 			pip = next;
18817c478bd9Sstevel@tonic-gate 		}
18827c478bd9Sstevel@tonic-gate 		if (pip == NULL) {
18837c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, NULL,
1884*144dfaa9Scth 			    "!lba %llx, no pip !!\n",
1885*144dfaa9Scth 				bp->b_lblkno));
18867c478bd9Sstevel@tonic-gate 		} else {
18877c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, NULL,
1888*144dfaa9Scth 			    "!lba %llx, no pip for path_index, "
1889*144dfaa9Scth 			    "pip %p\n", bp->b_lblkno, (void *)pip));
18907c478bd9Sstevel@tonic-gate 		}
18917c478bd9Sstevel@tonic-gate 	}
18927c478bd9Sstevel@tonic-gate 	return (MDI_FAILURE);
18937c478bd9Sstevel@tonic-gate }
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate /*
18967c478bd9Sstevel@tonic-gate  * mdi_select_path():
18977c478bd9Sstevel@tonic-gate  *		select a path to access a client device.
18987c478bd9Sstevel@tonic-gate  *
18997c478bd9Sstevel@tonic-gate  *		mdi_select_path() function is called by the vHCI drivers to
19007c478bd9Sstevel@tonic-gate  *		select a path to route the I/O request to.  The caller passes
19017c478bd9Sstevel@tonic-gate  *		the block I/O data transfer structure ("buf") as one of the
19027c478bd9Sstevel@tonic-gate  *		parameters.  The mpxio framework uses the buf structure
19037c478bd9Sstevel@tonic-gate  *		contents to maintain per path statistics (total I/O size /
19047c478bd9Sstevel@tonic-gate  *		count pending).  If more than one online paths are available to
19057c478bd9Sstevel@tonic-gate  *		select, the framework automatically selects a suitable path
19067c478bd9Sstevel@tonic-gate  *		for routing I/O request. If a failover operation is active for
19077c478bd9Sstevel@tonic-gate  *		this client device the call shall be failed with MDI_BUSY error
19087c478bd9Sstevel@tonic-gate  *		code.
19097c478bd9Sstevel@tonic-gate  *
19107c478bd9Sstevel@tonic-gate  *		By default this function returns a suitable path in online
19117c478bd9Sstevel@tonic-gate  *		state based on the current load balancing policy.  Currently
19127c478bd9Sstevel@tonic-gate  *		we support LOAD_BALANCE_NONE (Previously selected online path
19137c478bd9Sstevel@tonic-gate  *		will continue to be used till the path is usable) and
19147c478bd9Sstevel@tonic-gate  *		LOAD_BALANCE_RR (Online paths will be selected in a round
19157c478bd9Sstevel@tonic-gate  *		robin fashion), LOAD_BALANCE_LB(Online paths will be selected
19167c478bd9Sstevel@tonic-gate  *		based on the logical block).  The load balancing
19177c478bd9Sstevel@tonic-gate  *		through vHCI drivers configuration file (driver.conf).
19187c478bd9Sstevel@tonic-gate  *
19197c478bd9Sstevel@tonic-gate  *		vHCI drivers may override this default behavior by specifying
19207c478bd9Sstevel@tonic-gate  *		appropriate flags.  If start_pip is specified (non NULL) is
19217c478bd9Sstevel@tonic-gate  *		used as start point to walk and find the next appropriate path.
19227c478bd9Sstevel@tonic-gate  *		The following values are currently defined:
19237c478bd9Sstevel@tonic-gate  *		MDI_SELECT_ONLINE_PATH (to select an ONLINE path) and/or
19247c478bd9Sstevel@tonic-gate  *		MDI_SELECT_STANDBY_PATH (to select an STANDBY path).
19257c478bd9Sstevel@tonic-gate  *
19267c478bd9Sstevel@tonic-gate  *		The non-standard behavior is used by the scsi_vhci driver,
19277c478bd9Sstevel@tonic-gate  *		whenever it has to use a STANDBY/FAULTED path.  Eg. during
19287c478bd9Sstevel@tonic-gate  *		attach of client devices (to avoid an unnecessary failover
19297c478bd9Sstevel@tonic-gate  *		when the STANDBY path comes up first), during failover
19307c478bd9Sstevel@tonic-gate  *		(to activate a STANDBY path as ONLINE).
19317c478bd9Sstevel@tonic-gate  *
1932*144dfaa9Scth  *		The selected path is returned in a a mdi_hold_path() state
1933*144dfaa9Scth  *		(pi_ref_cnt). Caller should release the hold by calling
1934*144dfaa9Scth  *		mdi_rele_path().
19357c478bd9Sstevel@tonic-gate  *
19367c478bd9Sstevel@tonic-gate  * Return Values:
19377c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS	- Completed successfully
19387c478bd9Sstevel@tonic-gate  *		MDI_BUSY 	- Client device is busy failing over
19397c478bd9Sstevel@tonic-gate  *		MDI_NOPATH	- Client device is online, but no valid path are
19407c478bd9Sstevel@tonic-gate  *				  available to access this client device
19417c478bd9Sstevel@tonic-gate  *		MDI_FAILURE	- Invalid client device or state
19427c478bd9Sstevel@tonic-gate  *		MDI_DEVI_ONLINING
19437c478bd9Sstevel@tonic-gate  *				- Client device (struct dev_info state) is in
19447c478bd9Sstevel@tonic-gate  *				  onlining state.
19457c478bd9Sstevel@tonic-gate  */
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19487c478bd9Sstevel@tonic-gate int
19497c478bd9Sstevel@tonic-gate mdi_select_path(dev_info_t *cdip, struct buf *bp, int flags,
19507c478bd9Sstevel@tonic-gate     mdi_pathinfo_t *start_pip, mdi_pathinfo_t **ret_pip)
19517c478bd9Sstevel@tonic-gate {
19527c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
19537c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
19547c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
19557c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*head;
19567c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*start;
19577c478bd9Sstevel@tonic-gate 	client_lb_t	lbp;	/* load balancing policy */
19587c478bd9Sstevel@tonic-gate 	int		sb = 1;	/* standard behavior */
19597c478bd9Sstevel@tonic-gate 	int		preferred = 1;	/* preferred path */
19607c478bd9Sstevel@tonic-gate 	int		cond, cont = 1;
19617c478bd9Sstevel@tonic-gate 	int		retry = 0;
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 	if (flags != 0) {
19647c478bd9Sstevel@tonic-gate 		/*
19657c478bd9Sstevel@tonic-gate 		 * disable default behavior
19667c478bd9Sstevel@tonic-gate 		 */
19677c478bd9Sstevel@tonic-gate 		sb = 0;
19687c478bd9Sstevel@tonic-gate 	}
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	*ret_pip = NULL;
19717c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
19727c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
19737c478bd9Sstevel@tonic-gate 		/* mdi extensions are NULL, Nothing more to do */
19747c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
19757c478bd9Sstevel@tonic-gate 	}
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	if (sb) {
19807c478bd9Sstevel@tonic-gate 		if (MDI_CLIENT_IS_FAILED(ct)) {
19817c478bd9Sstevel@tonic-gate 			/*
19827c478bd9Sstevel@tonic-gate 			 * Client is not ready to accept any I/O requests.
19837c478bd9Sstevel@tonic-gate 			 * Fail this request.
19847c478bd9Sstevel@tonic-gate 			 */
19857c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_NOTE, cdip, "!mdi_select_path: "
1986*144dfaa9Scth 			    "client state offline ct = %p\n", (void *)ct));
19877c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
19887c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
19897c478bd9Sstevel@tonic-gate 		}
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 		if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
19927c478bd9Sstevel@tonic-gate 			/*
19937c478bd9Sstevel@tonic-gate 			 * Check for Failover is in progress. If so tell the
19947c478bd9Sstevel@tonic-gate 			 * caller that this device is busy.
19957c478bd9Sstevel@tonic-gate 			 */
19967c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_NOTE, cdip, "!mdi_select_path: "
1997*144dfaa9Scth 			    "client failover in progress ct = %p\n",
1998*144dfaa9Scth 			    (void *)ct));
19997c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
20007c478bd9Sstevel@tonic-gate 			return (MDI_BUSY);
20017c478bd9Sstevel@tonic-gate 		}
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 		/*
20047c478bd9Sstevel@tonic-gate 		 * Check to see whether the client device is attached.
20057c478bd9Sstevel@tonic-gate 		 * If not so, let the vHCI driver manually select a path
20067c478bd9Sstevel@tonic-gate 		 * (standby) and let the probe/attach process to continue.
20077c478bd9Sstevel@tonic-gate 		 */
2008737d277aScth 		if (MDI_CLIENT_IS_DETACHED(ct) || !i_ddi_devi_attached(cdip)) {
2009*144dfaa9Scth 			MDI_DEBUG(4, (CE_NOTE, cdip, "!Devi is onlining "
2010*144dfaa9Scth 			    "ct = %p\n", (void *)ct));
20117c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
20127c478bd9Sstevel@tonic-gate 			return (MDI_DEVI_ONLINING);
20137c478bd9Sstevel@tonic-gate 		}
20147c478bd9Sstevel@tonic-gate 	}
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	/*
20177c478bd9Sstevel@tonic-gate 	 * Cache in the client list head.  If head of the list is NULL
20187c478bd9Sstevel@tonic-gate 	 * return MDI_NOPATH
20197c478bd9Sstevel@tonic-gate 	 */
20207c478bd9Sstevel@tonic-gate 	head = ct->ct_path_head;
20217c478bd9Sstevel@tonic-gate 	if (head == NULL) {
20227c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
20237c478bd9Sstevel@tonic-gate 		return (MDI_NOPATH);
20247c478bd9Sstevel@tonic-gate 	}
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	/*
20277c478bd9Sstevel@tonic-gate 	 * for non default behavior, bypass current
20287c478bd9Sstevel@tonic-gate 	 * load balancing policy and always use LOAD_BALANCE_RR
20297c478bd9Sstevel@tonic-gate 	 * except that the start point will be adjusted based
20307c478bd9Sstevel@tonic-gate 	 * on the provided start_pip
20317c478bd9Sstevel@tonic-gate 	 */
20327c478bd9Sstevel@tonic-gate 	lbp = sb ? ct->ct_lb : LOAD_BALANCE_RR;
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	switch (lbp) {
20357c478bd9Sstevel@tonic-gate 	case LOAD_BALANCE_NONE:
20367c478bd9Sstevel@tonic-gate 		/*
20377c478bd9Sstevel@tonic-gate 		 * Load balancing is None  or Alternate path mode
20387c478bd9Sstevel@tonic-gate 		 * Start looking for a online mdi_pathinfo node starting from
20397c478bd9Sstevel@tonic-gate 		 * last known selected path
20407c478bd9Sstevel@tonic-gate 		 */
20417c478bd9Sstevel@tonic-gate 		preferred = 1;
20427c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)ct->ct_path_last;
20437c478bd9Sstevel@tonic-gate 		if (pip == NULL) {
20447c478bd9Sstevel@tonic-gate 			pip = head;
20457c478bd9Sstevel@tonic-gate 		}
20467c478bd9Sstevel@tonic-gate 		start = pip;
20477c478bd9Sstevel@tonic-gate 		do {
20487c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
20497c478bd9Sstevel@tonic-gate 			/*
20507c478bd9Sstevel@tonic-gate 			 * No need to explicitly check if the path is disabled.
20517c478bd9Sstevel@tonic-gate 			 * Since we are checking for state == ONLINE and the
20527c478bd9Sstevel@tonic-gate 			 * same veriable is used for DISABLE/ENABLE information.
20537c478bd9Sstevel@tonic-gate 			 */
2054ee28b439Scm136836 			if ((MDI_PI(pip)->pi_state  ==
2055ee28b439Scm136836 				MDI_PATHINFO_STATE_ONLINE) &&
20567c478bd9Sstevel@tonic-gate 				preferred == MDI_PI(pip)->pi_preferred) {
20577c478bd9Sstevel@tonic-gate 				/*
20587c478bd9Sstevel@tonic-gate 				 * Return the path in hold state. Caller should
20597c478bd9Sstevel@tonic-gate 				 * release the lock by calling mdi_rele_path()
20607c478bd9Sstevel@tonic-gate 				 */
20617c478bd9Sstevel@tonic-gate 				MDI_PI_HOLD(pip);
20627c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
20637c478bd9Sstevel@tonic-gate 				ct->ct_path_last = pip;
20647c478bd9Sstevel@tonic-gate 				*ret_pip = pip;
20657c478bd9Sstevel@tonic-gate 				MDI_CLIENT_UNLOCK(ct);
20667c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
20677c478bd9Sstevel@tonic-gate 			}
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 			/*
20707c478bd9Sstevel@tonic-gate 			 * Path is busy.
20717c478bd9Sstevel@tonic-gate 			 */
20727c478bd9Sstevel@tonic-gate 			if (MDI_PI_IS_DRV_DISABLE_TRANSIENT(pip) ||
20737c478bd9Sstevel@tonic-gate 			    MDI_PI_IS_TRANSIENT(pip))
20747c478bd9Sstevel@tonic-gate 				retry = 1;
20757c478bd9Sstevel@tonic-gate 			/*
20767c478bd9Sstevel@tonic-gate 			 * Keep looking for a next available online path
20777c478bd9Sstevel@tonic-gate 			 */
20787c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
20797c478bd9Sstevel@tonic-gate 			if (next == NULL) {
20807c478bd9Sstevel@tonic-gate 				next = head;
20817c478bd9Sstevel@tonic-gate 			}
20827c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
20837c478bd9Sstevel@tonic-gate 			pip = next;
20847c478bd9Sstevel@tonic-gate 			if (start == pip && preferred) {
20857c478bd9Sstevel@tonic-gate 				preferred = 0;
20867c478bd9Sstevel@tonic-gate 			} else if (start == pip && !preferred) {
20877c478bd9Sstevel@tonic-gate 				cont = 0;
20887c478bd9Sstevel@tonic-gate 			}
20897c478bd9Sstevel@tonic-gate 		} while (cont);
20907c478bd9Sstevel@tonic-gate 		break;
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 	case LOAD_BALANCE_LBA:
20937c478bd9Sstevel@tonic-gate 		/*
20947c478bd9Sstevel@tonic-gate 		 * Make sure we are looking
20957c478bd9Sstevel@tonic-gate 		 * for an online path. Otherwise, if it is for a STANDBY
20967c478bd9Sstevel@tonic-gate 		 * path request, it will go through and fetch an ONLINE
20977c478bd9Sstevel@tonic-gate 		 * path which is not desirable.
20987c478bd9Sstevel@tonic-gate 		 */
20997c478bd9Sstevel@tonic-gate 		if ((ct->ct_lb_args != NULL) &&
21007c478bd9Sstevel@tonic-gate 			    (ct->ct_lb_args->region_size) && bp &&
21017c478bd9Sstevel@tonic-gate 				(sb || (flags == MDI_SELECT_ONLINE_PATH))) {
21027c478bd9Sstevel@tonic-gate 			if (i_mdi_lba_lb(ct, ret_pip, bp)
21037c478bd9Sstevel@tonic-gate 				    == MDI_SUCCESS) {
21047c478bd9Sstevel@tonic-gate 				MDI_CLIENT_UNLOCK(ct);
21057c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
21067c478bd9Sstevel@tonic-gate 			}
21077c478bd9Sstevel@tonic-gate 		}
21087c478bd9Sstevel@tonic-gate 		/*  FALLTHROUGH */
21097c478bd9Sstevel@tonic-gate 	case LOAD_BALANCE_RR:
21107c478bd9Sstevel@tonic-gate 		/*
21117c478bd9Sstevel@tonic-gate 		 * Load balancing is Round Robin. Start looking for a online
21127c478bd9Sstevel@tonic-gate 		 * mdi_pathinfo node starting from last known selected path
21137c478bd9Sstevel@tonic-gate 		 * as the start point.  If override flags are specified,
21147c478bd9Sstevel@tonic-gate 		 * process accordingly.
21157c478bd9Sstevel@tonic-gate 		 * If the search is already in effect(start_pip not null),
21167c478bd9Sstevel@tonic-gate 		 * then lets just use the same path preference to continue the
21177c478bd9Sstevel@tonic-gate 		 * traversal.
21187c478bd9Sstevel@tonic-gate 		 */
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 		if (start_pip != NULL) {
21217c478bd9Sstevel@tonic-gate 			preferred = MDI_PI(start_pip)->pi_preferred;
21227c478bd9Sstevel@tonic-gate 		} else {
21237c478bd9Sstevel@tonic-gate 			preferred = 1;
21247c478bd9Sstevel@tonic-gate 		}
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 		start = sb ? (mdi_pathinfo_t *)ct->ct_path_last : start_pip;
21277c478bd9Sstevel@tonic-gate 		if (start == NULL) {
21287c478bd9Sstevel@tonic-gate 			pip = head;
21297c478bd9Sstevel@tonic-gate 		} else {
21307c478bd9Sstevel@tonic-gate 			pip = (mdi_pathinfo_t *)MDI_PI(start)->pi_client_link;
21317c478bd9Sstevel@tonic-gate 			if (pip == NULL) {
21327c478bd9Sstevel@tonic-gate 				if (!sb) {
21337c478bd9Sstevel@tonic-gate 					if (preferred == 0) {
21347c478bd9Sstevel@tonic-gate 						/*
21357c478bd9Sstevel@tonic-gate 						 * Looks like we have completed
21367c478bd9Sstevel@tonic-gate 						 * the traversal as preferred
21377c478bd9Sstevel@tonic-gate 						 * value is 0. Time to bail out.
21387c478bd9Sstevel@tonic-gate 						 */
21397c478bd9Sstevel@tonic-gate 						*ret_pip = NULL;
21407c478bd9Sstevel@tonic-gate 						MDI_CLIENT_UNLOCK(ct);
21417c478bd9Sstevel@tonic-gate 						return (MDI_NOPATH);
21427c478bd9Sstevel@tonic-gate 					} else {
21437c478bd9Sstevel@tonic-gate 						/*
21447c478bd9Sstevel@tonic-gate 						 * Looks like we reached the
21457c478bd9Sstevel@tonic-gate 						 * end of the list. Lets enable
21467c478bd9Sstevel@tonic-gate 						 * traversal of non preferred
21477c478bd9Sstevel@tonic-gate 						 * paths.
21487c478bd9Sstevel@tonic-gate 						 */
21497c478bd9Sstevel@tonic-gate 						preferred = 0;
21507c478bd9Sstevel@tonic-gate 					}
21517c478bd9Sstevel@tonic-gate 				}
21527c478bd9Sstevel@tonic-gate 				pip = head;
21537c478bd9Sstevel@tonic-gate 			}
21547c478bd9Sstevel@tonic-gate 		}
21557c478bd9Sstevel@tonic-gate 		start = pip;
21567c478bd9Sstevel@tonic-gate 		do {
21577c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
21587c478bd9Sstevel@tonic-gate 			if (sb) {
21597c478bd9Sstevel@tonic-gate 				cond = ((MDI_PI(pip)->pi_state ==
21607c478bd9Sstevel@tonic-gate 				    MDI_PATHINFO_STATE_ONLINE &&
21617c478bd9Sstevel@tonic-gate 					MDI_PI(pip)->pi_preferred ==
21627c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
21637c478bd9Sstevel@tonic-gate 			} else {
21647c478bd9Sstevel@tonic-gate 				if (flags == MDI_SELECT_ONLINE_PATH) {
21657c478bd9Sstevel@tonic-gate 					cond = ((MDI_PI(pip)->pi_state ==
21667c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_ONLINE &&
21677c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_preferred ==
21687c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
21697c478bd9Sstevel@tonic-gate 				} else if (flags == MDI_SELECT_STANDBY_PATH) {
21707c478bd9Sstevel@tonic-gate 					cond = ((MDI_PI(pip)->pi_state ==
21717c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_STANDBY &&
21727c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_preferred ==
21737c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
21747c478bd9Sstevel@tonic-gate 				} else if (flags == (MDI_SELECT_ONLINE_PATH |
21757c478bd9Sstevel@tonic-gate 				    MDI_SELECT_STANDBY_PATH)) {
21767c478bd9Sstevel@tonic-gate 					cond = (((MDI_PI(pip)->pi_state ==
21777c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_ONLINE ||
21787c478bd9Sstevel@tonic-gate 					    (MDI_PI(pip)->pi_state ==
21797c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_STANDBY)) &&
21807c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_preferred ==
21817c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
2182ee28b439Scm136836 				} else if (flags ==
2183ee28b439Scm136836 					(MDI_SELECT_STANDBY_PATH |
2184ee28b439Scm136836 					MDI_SELECT_ONLINE_PATH |
2185ee28b439Scm136836 					MDI_SELECT_USER_DISABLE_PATH)) {
2186ee28b439Scm136836 					cond = (((MDI_PI(pip)->pi_state ==
2187ee28b439Scm136836 					    MDI_PATHINFO_STATE_ONLINE ||
2188ee28b439Scm136836 					    (MDI_PI(pip)->pi_state ==
2189ee28b439Scm136836 					    MDI_PATHINFO_STATE_STANDBY) ||
2190ee28b439Scm136836 						(MDI_PI(pip)->pi_state ==
2191ee28b439Scm136836 					    (MDI_PATHINFO_STATE_ONLINE|
2192ee28b439Scm136836 					    MDI_PATHINFO_STATE_USER_DISABLE)) ||
2193ee28b439Scm136836 						(MDI_PI(pip)->pi_state ==
2194ee28b439Scm136836 					    (MDI_PATHINFO_STATE_STANDBY |
2195ee28b439Scm136836 					    MDI_PATHINFO_STATE_USER_DISABLE)))&&
2196ee28b439Scm136836 						MDI_PI(pip)->pi_preferred ==
2197ee28b439Scm136836 						preferred) ? 1 : 0);
21987c478bd9Sstevel@tonic-gate 				} else {
21997c478bd9Sstevel@tonic-gate 					cond = 0;
22007c478bd9Sstevel@tonic-gate 				}
22017c478bd9Sstevel@tonic-gate 			}
22027c478bd9Sstevel@tonic-gate 			/*
22037c478bd9Sstevel@tonic-gate 			 * No need to explicitly check if the path is disabled.
22047c478bd9Sstevel@tonic-gate 			 * Since we are checking for state == ONLINE and the
22057c478bd9Sstevel@tonic-gate 			 * same veriable is used for DISABLE/ENABLE information.
22067c478bd9Sstevel@tonic-gate 			 */
22077c478bd9Sstevel@tonic-gate 			if (cond) {
22087c478bd9Sstevel@tonic-gate 				/*
22097c478bd9Sstevel@tonic-gate 				 * Return the path in hold state. Caller should
22107c478bd9Sstevel@tonic-gate 				 * release the lock by calling mdi_rele_path()
22117c478bd9Sstevel@tonic-gate 				 */
22127c478bd9Sstevel@tonic-gate 				MDI_PI_HOLD(pip);
22137c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
22147c478bd9Sstevel@tonic-gate 				if (sb)
22157c478bd9Sstevel@tonic-gate 					ct->ct_path_last = pip;
22167c478bd9Sstevel@tonic-gate 				*ret_pip = pip;
22177c478bd9Sstevel@tonic-gate 				MDI_CLIENT_UNLOCK(ct);
22187c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
22197c478bd9Sstevel@tonic-gate 			}
22207c478bd9Sstevel@tonic-gate 			/*
22217c478bd9Sstevel@tonic-gate 			 * Path is busy.
22227c478bd9Sstevel@tonic-gate 			 */
22237c478bd9Sstevel@tonic-gate 			if (MDI_PI_IS_DRV_DISABLE_TRANSIENT(pip) ||
22247c478bd9Sstevel@tonic-gate 			    MDI_PI_IS_TRANSIENT(pip))
22257c478bd9Sstevel@tonic-gate 				retry = 1;
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 			/*
22287c478bd9Sstevel@tonic-gate 			 * Keep looking for a next available online path
22297c478bd9Sstevel@tonic-gate 			 */
22307c478bd9Sstevel@tonic-gate do_again:
22317c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
22327c478bd9Sstevel@tonic-gate 			if (next == NULL) {
22337c478bd9Sstevel@tonic-gate 				if (!sb) {
22347c478bd9Sstevel@tonic-gate 					if (preferred == 1) {
22357c478bd9Sstevel@tonic-gate 						/*
22367c478bd9Sstevel@tonic-gate 						 * Looks like we reached the
22377c478bd9Sstevel@tonic-gate 						 * end of the list. Lets enable
22387c478bd9Sstevel@tonic-gate 						 * traversal of non preferred
22397c478bd9Sstevel@tonic-gate 						 * paths.
22407c478bd9Sstevel@tonic-gate 						 */
22417c478bd9Sstevel@tonic-gate 						preferred = 0;
22427c478bd9Sstevel@tonic-gate 						next = head;
22437c478bd9Sstevel@tonic-gate 					} else {
22447c478bd9Sstevel@tonic-gate 						/*
22457c478bd9Sstevel@tonic-gate 						 * We have done both the passes
22467c478bd9Sstevel@tonic-gate 						 * Preferred as well as for
22477c478bd9Sstevel@tonic-gate 						 * Non-preferred. Bail out now.
22487c478bd9Sstevel@tonic-gate 						 */
22497c478bd9Sstevel@tonic-gate 						cont = 0;
22507c478bd9Sstevel@tonic-gate 					}
22517c478bd9Sstevel@tonic-gate 				} else {
22527c478bd9Sstevel@tonic-gate 					/*
22537c478bd9Sstevel@tonic-gate 					 * Standard behavior case.
22547c478bd9Sstevel@tonic-gate 					 */
22557c478bd9Sstevel@tonic-gate 					next = head;
22567c478bd9Sstevel@tonic-gate 				}
22577c478bd9Sstevel@tonic-gate 			}
22587c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
22597c478bd9Sstevel@tonic-gate 			if (cont == 0) {
22607c478bd9Sstevel@tonic-gate 				break;
22617c478bd9Sstevel@tonic-gate 			}
22627c478bd9Sstevel@tonic-gate 			pip = next;
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate 			if (!sb) {
22657c478bd9Sstevel@tonic-gate 				/*
22667c478bd9Sstevel@tonic-gate 				 * We need to handle the selection of
22677c478bd9Sstevel@tonic-gate 				 * non-preferred path in the following
22687c478bd9Sstevel@tonic-gate 				 * case:
22697c478bd9Sstevel@tonic-gate 				 *
22707c478bd9Sstevel@tonic-gate 				 * +------+   +------+   +------+   +-----+
22717c478bd9Sstevel@tonic-gate 				 * | A : 1| - | B : 1| - | C : 0| - |NULL |
22727c478bd9Sstevel@tonic-gate 				 * +------+   +------+   +------+   +-----+
22737c478bd9Sstevel@tonic-gate 				 *
22747c478bd9Sstevel@tonic-gate 				 * If we start the search with B, we need to
22757c478bd9Sstevel@tonic-gate 				 * skip beyond B to pick C which is non -
22767c478bd9Sstevel@tonic-gate 				 * preferred in the second pass. The following
22777c478bd9Sstevel@tonic-gate 				 * test, if true, will allow us to skip over
22787c478bd9Sstevel@tonic-gate 				 * the 'start'(B in the example) to select
22797c478bd9Sstevel@tonic-gate 				 * other non preferred elements.
22807c478bd9Sstevel@tonic-gate 				 */
22817c478bd9Sstevel@tonic-gate 				if ((start_pip != NULL) && (start_pip == pip) &&
22827c478bd9Sstevel@tonic-gate 				    (MDI_PI(start_pip)->pi_preferred
22837c478bd9Sstevel@tonic-gate 				    != preferred)) {
22847c478bd9Sstevel@tonic-gate 					/*
22857c478bd9Sstevel@tonic-gate 					 * try again after going past the start
22867c478bd9Sstevel@tonic-gate 					 * pip
22877c478bd9Sstevel@tonic-gate 					 */
22887c478bd9Sstevel@tonic-gate 					MDI_PI_LOCK(pip);
22897c478bd9Sstevel@tonic-gate 					goto do_again;
22907c478bd9Sstevel@tonic-gate 				}
22917c478bd9Sstevel@tonic-gate 			} else {
22927c478bd9Sstevel@tonic-gate 				/*
22937c478bd9Sstevel@tonic-gate 				 * Standard behavior case
22947c478bd9Sstevel@tonic-gate 				 */
22957c478bd9Sstevel@tonic-gate 				if (start == pip && preferred) {
22967c478bd9Sstevel@tonic-gate 					/* look for nonpreferred paths */
22977c478bd9Sstevel@tonic-gate 					preferred = 0;
22987c478bd9Sstevel@tonic-gate 				} else if (start == pip && !preferred) {
22997c478bd9Sstevel@tonic-gate 					/*
23007c478bd9Sstevel@tonic-gate 					 * Exit condition
23017c478bd9Sstevel@tonic-gate 					 */
23027c478bd9Sstevel@tonic-gate 					cont = 0;
23037c478bd9Sstevel@tonic-gate 				}
23047c478bd9Sstevel@tonic-gate 			}
23057c478bd9Sstevel@tonic-gate 		} while (cont);
23067c478bd9Sstevel@tonic-gate 		break;
23077c478bd9Sstevel@tonic-gate 	}
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
23107c478bd9Sstevel@tonic-gate 	if (retry == 1) {
23117c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
23127c478bd9Sstevel@tonic-gate 	} else {
23137c478bd9Sstevel@tonic-gate 		return (MDI_NOPATH);
23147c478bd9Sstevel@tonic-gate 	}
23157c478bd9Sstevel@tonic-gate }
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate /*
23187c478bd9Sstevel@tonic-gate  * For a client, return the next available path to any phci
23197c478bd9Sstevel@tonic-gate  *
23207c478bd9Sstevel@tonic-gate  * Note:
23217c478bd9Sstevel@tonic-gate  *		Caller should hold the branch's devinfo node to get a consistent
23227c478bd9Sstevel@tonic-gate  *		snap shot of the mdi_pathinfo nodes.
23237c478bd9Sstevel@tonic-gate  *
23247c478bd9Sstevel@tonic-gate  *		Please note that even the list is stable the mdi_pathinfo
23257c478bd9Sstevel@tonic-gate  *		node state and properties are volatile.  The caller should lock
23267c478bd9Sstevel@tonic-gate  *		and unlock the nodes by calling mdi_pi_lock() and
23277c478bd9Sstevel@tonic-gate  *		mdi_pi_unlock() functions to get a stable properties.
23287c478bd9Sstevel@tonic-gate  *
23297c478bd9Sstevel@tonic-gate  *		If there is a need to use the nodes beyond the hold of the
23307c478bd9Sstevel@tonic-gate  *		devinfo node period (For ex. I/O), then mdi_pathinfo node
23317c478bd9Sstevel@tonic-gate  *		need to be held against unexpected removal by calling
23327c478bd9Sstevel@tonic-gate  *		mdi_hold_path() and should be released by calling
23337c478bd9Sstevel@tonic-gate  *		mdi_rele_path() on completion.
23347c478bd9Sstevel@tonic-gate  */
23357c478bd9Sstevel@tonic-gate mdi_pathinfo_t *
23367c478bd9Sstevel@tonic-gate mdi_get_next_phci_path(dev_info_t *ct_dip, mdi_pathinfo_t *pip)
23377c478bd9Sstevel@tonic-gate {
23387c478bd9Sstevel@tonic-gate 	mdi_client_t *ct;
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 	if (!MDI_CLIENT(ct_dip))
23417c478bd9Sstevel@tonic-gate 		return (NULL);
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate 	/*
23447c478bd9Sstevel@tonic-gate 	 * Walk through client link
23457c478bd9Sstevel@tonic-gate 	 */
23467c478bd9Sstevel@tonic-gate 	ct = (mdi_client_t *)DEVI(ct_dip)->devi_mdi_client;
23477c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 	if (pip == NULL)
23507c478bd9Sstevel@tonic-gate 		return ((mdi_pathinfo_t *)ct->ct_path_head);
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 	return ((mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link);
23537c478bd9Sstevel@tonic-gate }
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate /*
23567c478bd9Sstevel@tonic-gate  * For a phci, return the next available path to any client
23577c478bd9Sstevel@tonic-gate  * Note: ditto mdi_get_next_phci_path()
23587c478bd9Sstevel@tonic-gate  */
23597c478bd9Sstevel@tonic-gate mdi_pathinfo_t *
23607c478bd9Sstevel@tonic-gate mdi_get_next_client_path(dev_info_t *ph_dip, mdi_pathinfo_t *pip)
23617c478bd9Sstevel@tonic-gate {
23627c478bd9Sstevel@tonic-gate 	mdi_phci_t *ph;
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 	if (!MDI_PHCI(ph_dip))
23657c478bd9Sstevel@tonic-gate 		return (NULL);
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	/*
23687c478bd9Sstevel@tonic-gate 	 * Walk through pHCI link
23697c478bd9Sstevel@tonic-gate 	 */
23707c478bd9Sstevel@tonic-gate 	ph = (mdi_phci_t *)DEVI(ph_dip)->devi_mdi_xhci;
23717c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 	if (pip == NULL)
23747c478bd9Sstevel@tonic-gate 		return ((mdi_pathinfo_t *)ph->ph_path_head);
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate 	return ((mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link);
23777c478bd9Sstevel@tonic-gate }
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate /*
23807c478bd9Sstevel@tonic-gate  * mdi_hold_path():
23817c478bd9Sstevel@tonic-gate  *		Hold the mdi_pathinfo node against unwanted unexpected free.
23827c478bd9Sstevel@tonic-gate  * Return Values:
23837c478bd9Sstevel@tonic-gate  *		None
23847c478bd9Sstevel@tonic-gate  */
23857c478bd9Sstevel@tonic-gate void
23867c478bd9Sstevel@tonic-gate mdi_hold_path(mdi_pathinfo_t *pip)
23877c478bd9Sstevel@tonic-gate {
23887c478bd9Sstevel@tonic-gate 	if (pip) {
23897c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
23907c478bd9Sstevel@tonic-gate 		MDI_PI_HOLD(pip);
23917c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
23927c478bd9Sstevel@tonic-gate 	}
23937c478bd9Sstevel@tonic-gate }
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate /*
23977c478bd9Sstevel@tonic-gate  * mdi_rele_path():
23987c478bd9Sstevel@tonic-gate  *		Release the mdi_pathinfo node which was selected
23997c478bd9Sstevel@tonic-gate  *		through mdi_select_path() mechanism or manually held by
24007c478bd9Sstevel@tonic-gate  *		calling mdi_hold_path().
24017c478bd9Sstevel@tonic-gate  * Return Values:
24027c478bd9Sstevel@tonic-gate  *		None
24037c478bd9Sstevel@tonic-gate  */
24047c478bd9Sstevel@tonic-gate void
24057c478bd9Sstevel@tonic-gate mdi_rele_path(mdi_pathinfo_t *pip)
24067c478bd9Sstevel@tonic-gate {
24077c478bd9Sstevel@tonic-gate 	if (pip) {
24087c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
24097c478bd9Sstevel@tonic-gate 		MDI_PI_RELE(pip);
24107c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_ref_cnt == 0) {
24117c478bd9Sstevel@tonic-gate 			cv_broadcast(&MDI_PI(pip)->pi_ref_cv);
24127c478bd9Sstevel@tonic-gate 		}
24137c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
24147c478bd9Sstevel@tonic-gate 	}
24157c478bd9Sstevel@tonic-gate }
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate /*
24187c478bd9Sstevel@tonic-gate  * mdi_pi_lock():
24197c478bd9Sstevel@tonic-gate  * 		Lock the mdi_pathinfo node.
24207c478bd9Sstevel@tonic-gate  * Note:
24217c478bd9Sstevel@tonic-gate  *		The caller should release the lock by calling mdi_pi_unlock()
24227c478bd9Sstevel@tonic-gate  */
24237c478bd9Sstevel@tonic-gate void
24247c478bd9Sstevel@tonic-gate mdi_pi_lock(mdi_pathinfo_t *pip)
24257c478bd9Sstevel@tonic-gate {
24267c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
24277c478bd9Sstevel@tonic-gate 	if (pip) {
24287c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
24297c478bd9Sstevel@tonic-gate 	}
24307c478bd9Sstevel@tonic-gate }
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate /*
24347c478bd9Sstevel@tonic-gate  * mdi_pi_unlock():
24357c478bd9Sstevel@tonic-gate  * 		Unlock the mdi_pathinfo node.
24367c478bd9Sstevel@tonic-gate  * Note:
24377c478bd9Sstevel@tonic-gate  *		The mdi_pathinfo node should have been locked with mdi_pi_lock()
24387c478bd9Sstevel@tonic-gate  */
24397c478bd9Sstevel@tonic-gate void
24407c478bd9Sstevel@tonic-gate mdi_pi_unlock(mdi_pathinfo_t *pip)
24417c478bd9Sstevel@tonic-gate {
24427c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
24437c478bd9Sstevel@tonic-gate 	if (pip) {
24447c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
24457c478bd9Sstevel@tonic-gate 	}
24467c478bd9Sstevel@tonic-gate }
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate /*
24497c478bd9Sstevel@tonic-gate  * mdi_pi_find():
24507c478bd9Sstevel@tonic-gate  *		Search the list of mdi_pathinfo nodes attached to the
24517c478bd9Sstevel@tonic-gate  *		pHCI/Client device node whose path address matches "paddr".
24527c478bd9Sstevel@tonic-gate  *		Returns a pointer to the mdi_pathinfo node if a matching node is
24537c478bd9Sstevel@tonic-gate  *		found.
24547c478bd9Sstevel@tonic-gate  * Return Values:
24557c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node handle
24567c478bd9Sstevel@tonic-gate  *		NULL
24577c478bd9Sstevel@tonic-gate  * Notes:
24587c478bd9Sstevel@tonic-gate  *		Caller need not hold any locks to call this function.
24597c478bd9Sstevel@tonic-gate  */
24607c478bd9Sstevel@tonic-gate mdi_pathinfo_t *
24617c478bd9Sstevel@tonic-gate mdi_pi_find(dev_info_t *pdip, char *caddr, char *paddr)
24627c478bd9Sstevel@tonic-gate {
24637c478bd9Sstevel@tonic-gate 	mdi_phci_t		*ph;
24647c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh;
24657c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
24667c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t		*pip = NULL;
24677c478bd9Sstevel@tonic-gate 
2468*144dfaa9Scth 	MDI_DEBUG(2, (CE_NOTE, pdip, "!mdi_pi_find: %s %s",
2469*144dfaa9Scth 	    caddr ? caddr : "NULL", paddr ? paddr : "NULL"));
24707c478bd9Sstevel@tonic-gate 	if ((pdip == NULL) || (paddr == NULL)) {
24717c478bd9Sstevel@tonic-gate 		return (NULL);
24727c478bd9Sstevel@tonic-gate 	}
24737c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
24747c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
24757c478bd9Sstevel@tonic-gate 		/*
24767c478bd9Sstevel@tonic-gate 		 * Invalid pHCI device, Nothing more to do.
24777c478bd9Sstevel@tonic-gate 		 */
2478*144dfaa9Scth 		MDI_DEBUG(2, (CE_WARN, pdip,
24797c478bd9Sstevel@tonic-gate 		    "!mdi_pi_find: invalid phci"));
24807c478bd9Sstevel@tonic-gate 		return (NULL);
24817c478bd9Sstevel@tonic-gate 	}
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
24847c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
24857c478bd9Sstevel@tonic-gate 		/*
24867c478bd9Sstevel@tonic-gate 		 * Invalid vHCI device, Nothing more to do.
24877c478bd9Sstevel@tonic-gate 		 */
2488*144dfaa9Scth 		MDI_DEBUG(2, (CE_WARN, pdip,
2489*144dfaa9Scth 		    "!mdi_pi_find: invalid vhci"));
24907c478bd9Sstevel@tonic-gate 		return (NULL);
24917c478bd9Sstevel@tonic-gate 	}
24927c478bd9Sstevel@tonic-gate 
24937c478bd9Sstevel@tonic-gate 	/*
2494*144dfaa9Scth 	 * Look for pathinfo node identified by paddr.
24957c478bd9Sstevel@tonic-gate 	 */
24967c478bd9Sstevel@tonic-gate 	if (caddr == NULL) {
24977c478bd9Sstevel@tonic-gate 		/*
24987c478bd9Sstevel@tonic-gate 		 * Find a mdi_pathinfo node under pHCI list for a matching
24997c478bd9Sstevel@tonic-gate 		 * unit address.
25007c478bd9Sstevel@tonic-gate 		 */
2501*144dfaa9Scth 		MDI_PHCI_LOCK(ph);
2502*144dfaa9Scth 		if (MDI_PHCI_IS_OFFLINE(ph)) {
2503*144dfaa9Scth 			MDI_DEBUG(2, (CE_WARN, pdip,
2504*144dfaa9Scth 			    "!mdi_pi_find: offline phci %p", (void *)ph));
2505*144dfaa9Scth 			MDI_PHCI_UNLOCK(ph);
2506*144dfaa9Scth 			return (NULL);
2507*144dfaa9Scth 		}
25087c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)ph->ph_path_head;
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
25117c478bd9Sstevel@tonic-gate 			if (strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
25127c478bd9Sstevel@tonic-gate 				break;
25137c478bd9Sstevel@tonic-gate 			}
25147c478bd9Sstevel@tonic-gate 			pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
25157c478bd9Sstevel@tonic-gate 		}
2516*144dfaa9Scth 		MDI_PHCI_UNLOCK(ph);
2517*144dfaa9Scth 		MDI_DEBUG(2, (CE_NOTE, pdip, "!mdi_pi_find: found %p",
2518*144dfaa9Scth 		    (void *)pip));
25197c478bd9Sstevel@tonic-gate 		return (pip);
25207c478bd9Sstevel@tonic-gate 	}
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 	/*
25233c34adc5Sramat 	 * XXX - Is the rest of the code in this function really necessary?
25243c34adc5Sramat 	 * The consumers of mdi_pi_find() can search for the desired pathinfo
25253c34adc5Sramat 	 * node by calling mdi_pi_find(pdip, NULL, paddr). Irrespective of
25263c34adc5Sramat 	 * whether the search is based on the pathinfo nodes attached to
25273c34adc5Sramat 	 * the pHCI or the client node, the result will be the same.
25283c34adc5Sramat 	 */
25293c34adc5Sramat 
25303c34adc5Sramat 	/*
25317c478bd9Sstevel@tonic-gate 	 * Find the client device corresponding to 'caddr'
25327c478bd9Sstevel@tonic-gate 	 */
2533*144dfaa9Scth 	MDI_VHCI_CLIENT_LOCK(vh);
25343c34adc5Sramat 
25353c34adc5Sramat 	/*
25363c34adc5Sramat 	 * XXX - Passing NULL to the following function works as long as the
25373c34adc5Sramat 	 * the client addresses (caddr) are unique per vhci basis.
25383c34adc5Sramat 	 */
25393c34adc5Sramat 	ct = i_mdi_client_find(vh, NULL, caddr);
25407c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
25417c478bd9Sstevel@tonic-gate 		/*
25427c478bd9Sstevel@tonic-gate 		 * Client not found, Obviously mdi_pathinfo node has not been
25437c478bd9Sstevel@tonic-gate 		 * created yet.
25447c478bd9Sstevel@tonic-gate 		 */
2545*144dfaa9Scth 		MDI_VHCI_CLIENT_UNLOCK(vh);
2546*144dfaa9Scth 		MDI_DEBUG(2, (CE_NOTE, pdip, "!mdi_pi_find: client not "
2547*144dfaa9Scth 		    "found for caddr %s", caddr ? caddr : "NULL"));
2548*144dfaa9Scth 		return (NULL);
25497c478bd9Sstevel@tonic-gate 	}
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	/*
25527c478bd9Sstevel@tonic-gate 	 * Hold the client lock and look for a mdi_pathinfo node with matching
25537c478bd9Sstevel@tonic-gate 	 * pHCI and paddr
25547c478bd9Sstevel@tonic-gate 	 */
25557c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate 	/*
25587c478bd9Sstevel@tonic-gate 	 * Release the global mutex as it is no more needed. Note: We always
25597c478bd9Sstevel@tonic-gate 	 * respect the locking order while acquiring.
25607c478bd9Sstevel@tonic-gate 	 */
2561*144dfaa9Scth 	MDI_VHCI_CLIENT_UNLOCK(vh);
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
25647c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
25657c478bd9Sstevel@tonic-gate 		/*
25667c478bd9Sstevel@tonic-gate 		 * Compare the unit address
25677c478bd9Sstevel@tonic-gate 		 */
25687c478bd9Sstevel@tonic-gate 		if ((MDI_PI(pip)->pi_phci == ph) &&
25697c478bd9Sstevel@tonic-gate 		    strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
25707c478bd9Sstevel@tonic-gate 			break;
25717c478bd9Sstevel@tonic-gate 		}
25727c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
25737c478bd9Sstevel@tonic-gate 	}
25747c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
2575*144dfaa9Scth 	MDI_DEBUG(2, (CE_NOTE, pdip, "!mdi_pi_find: found:: %p", (void *)pip));
25767c478bd9Sstevel@tonic-gate 	return (pip);
25777c478bd9Sstevel@tonic-gate }
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate /*
25807c478bd9Sstevel@tonic-gate  * mdi_pi_alloc():
25817c478bd9Sstevel@tonic-gate  *		Allocate and initialize a new instance of a mdi_pathinfo node.
25827c478bd9Sstevel@tonic-gate  *		The mdi_pathinfo node returned by this function identifies a
25837c478bd9Sstevel@tonic-gate  *		unique device path is capable of having properties attached
25847c478bd9Sstevel@tonic-gate  *		and passed to mdi_pi_online() to fully attach and online the
25857c478bd9Sstevel@tonic-gate  *		path and client device node.
25867c478bd9Sstevel@tonic-gate  *		The mdi_pathinfo node returned by this function must be
25877c478bd9Sstevel@tonic-gate  *		destroyed using mdi_pi_free() if the path is no longer
25887c478bd9Sstevel@tonic-gate  *		operational or if the caller fails to attach a client device
25897c478bd9Sstevel@tonic-gate  *		node when calling mdi_pi_online(). The framework will not free
25907c478bd9Sstevel@tonic-gate  *		the resources allocated.
25917c478bd9Sstevel@tonic-gate  *		This function can be called from both interrupt and kernel
25927c478bd9Sstevel@tonic-gate  *		contexts.  DDI_NOSLEEP flag should be used while calling
25937c478bd9Sstevel@tonic-gate  *		from interrupt contexts.
25947c478bd9Sstevel@tonic-gate  * Return Values:
25957c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
25967c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
25977c478bd9Sstevel@tonic-gate  *		MDI_NOMEM
25987c478bd9Sstevel@tonic-gate  */
25997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
26007c478bd9Sstevel@tonic-gate int
26017c478bd9Sstevel@tonic-gate mdi_pi_alloc_compatible(dev_info_t *pdip, char *cname, char *caddr, char *paddr,
26027c478bd9Sstevel@tonic-gate     char **compatible, int ncompatible, int flags, mdi_pathinfo_t **ret_pip)
26037c478bd9Sstevel@tonic-gate {
26047c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
26057c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
26067c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
26077c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip = NULL;
26087c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
26097c478bd9Sstevel@tonic-gate 	int		rv = MDI_NOMEM;
26103c34adc5Sramat 	int		path_allocated = 0;
26117c478bd9Sstevel@tonic-gate 
2612*144dfaa9Scth 	MDI_DEBUG(2, (CE_NOTE, pdip, "!mdi_pi_alloc_compatible: %s %s %s",
2613*144dfaa9Scth 	    cname ? cname : "NULL", caddr ? caddr : "NULL",
2614*144dfaa9Scth 	    paddr ? paddr : "NULL"));
2615*144dfaa9Scth 
26167c478bd9Sstevel@tonic-gate 	if (pdip == NULL || cname == NULL || caddr == NULL || paddr == NULL ||
26177c478bd9Sstevel@tonic-gate 	    ret_pip == NULL) {
26187c478bd9Sstevel@tonic-gate 		/* Nothing more to do */
26197c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
26207c478bd9Sstevel@tonic-gate 	}
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate 	*ret_pip = NULL;
2623*144dfaa9Scth 
2624*144dfaa9Scth 	/* No allocations on detaching pHCI */
2625*144dfaa9Scth 	if (DEVI_IS_DETACHING(pdip)) {
2626*144dfaa9Scth 		/* Invalid pHCI device, return failure */
2627*144dfaa9Scth 		MDI_DEBUG(1, (CE_WARN, pdip,
2628*144dfaa9Scth 		    "!mdi_pi_alloc: detaching pHCI=%p", (void *)pdip));
2629*144dfaa9Scth 		return (MDI_FAILURE);
2630*144dfaa9Scth 	}
2631*144dfaa9Scth 
26327c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
26337c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
26347c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
26357c478bd9Sstevel@tonic-gate 		/* Invalid pHCI device, return failure */
2636*144dfaa9Scth 		MDI_DEBUG(1, (CE_WARN, pdip,
2637*144dfaa9Scth 		    "!mdi_pi_alloc: invalid pHCI=%p", (void *)pdip));
26387c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
26397c478bd9Sstevel@tonic-gate 	}
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
26427c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
26437c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
26447c478bd9Sstevel@tonic-gate 		/* Invalid vHCI device, return failure */
2645*144dfaa9Scth 		MDI_DEBUG(1, (CE_WARN, pdip,
2646*144dfaa9Scth 		    "!mdi_pi_alloc: invalid vHCI=%p", (void *)pdip));
26477c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
26487c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
26497c478bd9Sstevel@tonic-gate 	}
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_READY(ph) == 0) {
26527c478bd9Sstevel@tonic-gate 		/*
26537c478bd9Sstevel@tonic-gate 		 * Do not allow new node creation when pHCI is in
26547c478bd9Sstevel@tonic-gate 		 * offline/suspended states
26557c478bd9Sstevel@tonic-gate 		 */
2656*144dfaa9Scth 		MDI_DEBUG(1, (CE_WARN, pdip,
2657*144dfaa9Scth 		    "mdi_pi_alloc: pHCI=%p is not ready", (void *)ph));
26587c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
26597c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
26607c478bd9Sstevel@tonic-gate 	}
26617c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNSTABLE(ph);
26627c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
26637c478bd9Sstevel@tonic-gate 
26643c34adc5Sramat 	/* look for a matching client, create one if not found */
2665*144dfaa9Scth 	MDI_VHCI_CLIENT_LOCK(vh);
26663c34adc5Sramat 	ct = i_mdi_client_find(vh, cname, caddr);
26677c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
26683c34adc5Sramat 		ct = i_mdi_client_alloc(vh, cname, caddr);
26693c34adc5Sramat 		ASSERT(ct != NULL);
26707c478bd9Sstevel@tonic-gate 	}
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 	if (ct->ct_dip == NULL) {
26737c478bd9Sstevel@tonic-gate 		/*
26747c478bd9Sstevel@tonic-gate 		 * Allocate a devinfo node
26757c478bd9Sstevel@tonic-gate 		 */
26767c478bd9Sstevel@tonic-gate 		ct->ct_dip = i_mdi_devinfo_create(vh, cname, caddr,
26773c34adc5Sramat 		    compatible, ncompatible);
26787c478bd9Sstevel@tonic-gate 		if (ct->ct_dip == NULL) {
26797c478bd9Sstevel@tonic-gate 			(void) i_mdi_client_free(vh, ct);
26807c478bd9Sstevel@tonic-gate 			goto fail;
26817c478bd9Sstevel@tonic-gate 		}
26827c478bd9Sstevel@tonic-gate 	}
26837c478bd9Sstevel@tonic-gate 	cdip = ct->ct_dip;
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_component |= MDI_COMPONENT_CLIENT;
26867c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_client = (caddr_t)ct;
26877c478bd9Sstevel@tonic-gate 
2688*144dfaa9Scth 	MDI_CLIENT_LOCK(ct);
26897c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
26907c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
26917c478bd9Sstevel@tonic-gate 		/*
26927c478bd9Sstevel@tonic-gate 		 * Compare the unit address
26937c478bd9Sstevel@tonic-gate 		 */
26947c478bd9Sstevel@tonic-gate 		if ((MDI_PI(pip)->pi_phci == ph) &&
26957c478bd9Sstevel@tonic-gate 		    strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
26967c478bd9Sstevel@tonic-gate 			break;
26977c478bd9Sstevel@tonic-gate 		}
26987c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
26997c478bd9Sstevel@tonic-gate 	}
2700*144dfaa9Scth 	MDI_CLIENT_UNLOCK(ct);
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
27037c478bd9Sstevel@tonic-gate 		/*
27047c478bd9Sstevel@tonic-gate 		 * This is a new path for this client device.  Allocate and
27057c478bd9Sstevel@tonic-gate 		 * initialize a new pathinfo node
27067c478bd9Sstevel@tonic-gate 		 */
27073c34adc5Sramat 		pip = i_mdi_pi_alloc(ph, paddr, ct);
27083c34adc5Sramat 		ASSERT(pip != NULL);
27093c34adc5Sramat 		path_allocated = 1;
27107c478bd9Sstevel@tonic-gate 	}
27117c478bd9Sstevel@tonic-gate 	rv = MDI_SUCCESS;
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate fail:
27147c478bd9Sstevel@tonic-gate 	/*
27157c478bd9Sstevel@tonic-gate 	 * Release the global mutex.
27167c478bd9Sstevel@tonic-gate 	 */
2717*144dfaa9Scth 	MDI_VHCI_CLIENT_UNLOCK(vh);
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 	/*
27207c478bd9Sstevel@tonic-gate 	 * Mark the pHCI as stable
27217c478bd9Sstevel@tonic-gate 	 */
27227c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
27237c478bd9Sstevel@tonic-gate 	MDI_PHCI_STABLE(ph);
27247c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
27257c478bd9Sstevel@tonic-gate 	*ret_pip = pip;
27263c34adc5Sramat 
2727*144dfaa9Scth 	MDI_DEBUG(2, (CE_NOTE, pdip,
2728*144dfaa9Scth 	    "!mdi_pi_alloc_compatible: alloc %p", (void *)pip));
2729*144dfaa9Scth 
27303c34adc5Sramat 	if (path_allocated)
27313c34adc5Sramat 		vhcache_pi_add(vh->vh_config, MDI_PI(pip));
27323c34adc5Sramat 
27337c478bd9Sstevel@tonic-gate 	return (rv);
27347c478bd9Sstevel@tonic-gate }
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate /*ARGSUSED*/
27377c478bd9Sstevel@tonic-gate int
27387c478bd9Sstevel@tonic-gate mdi_pi_alloc(dev_info_t *pdip, char *cname, char *caddr, char *paddr,
27397c478bd9Sstevel@tonic-gate     int flags, mdi_pathinfo_t **ret_pip)
27407c478bd9Sstevel@tonic-gate {
27417c478bd9Sstevel@tonic-gate 	return (mdi_pi_alloc_compatible(pdip, cname, caddr, paddr, NULL, 0,
27427c478bd9Sstevel@tonic-gate 	    flags, ret_pip));
27437c478bd9Sstevel@tonic-gate }
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate /*
27467c478bd9Sstevel@tonic-gate  * i_mdi_pi_alloc():
27477c478bd9Sstevel@tonic-gate  *		Allocate a mdi_pathinfo node and add to the pHCI path list
27487c478bd9Sstevel@tonic-gate  * Return Values:
27497c478bd9Sstevel@tonic-gate  *		mdi_pathinfo
27507c478bd9Sstevel@tonic-gate  */
27517c478bd9Sstevel@tonic-gate /*ARGSUSED*/
27527c478bd9Sstevel@tonic-gate static mdi_pathinfo_t *
27533c34adc5Sramat i_mdi_pi_alloc(mdi_phci_t *ph, char *paddr, mdi_client_t *ct)
27547c478bd9Sstevel@tonic-gate {
27553c34adc5Sramat 	mdi_pathinfo_t	*pip;
27567c478bd9Sstevel@tonic-gate 	int		ct_circular;
27577c478bd9Sstevel@tonic-gate 	int		ph_circular;
27588c4f8890Srs135747 	int		se_flag;
27598c4f8890Srs135747 	int		kmem_flag;
27607c478bd9Sstevel@tonic-gate 
2761*144dfaa9Scth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(ph->ph_vhci));
2762*144dfaa9Scth 
27633c34adc5Sramat 	pip = kmem_zalloc(sizeof (struct mdi_pathinfo), KM_SLEEP);
27647c478bd9Sstevel@tonic-gate 	mutex_init(&MDI_PI(pip)->pi_mutex, NULL, MUTEX_DEFAULT, NULL);
27657c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_state = MDI_PATHINFO_STATE_INIT |
27667c478bd9Sstevel@tonic-gate 	    MDI_PATHINFO_STATE_TRANSIENT;
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_USER_DISABLED(ph))
27697c478bd9Sstevel@tonic-gate 		MDI_PI_SET_USER_DISABLE(pip);
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_DRV_DISABLED_TRANSIENT(ph))
27727c478bd9Sstevel@tonic-gate 		MDI_PI_SET_DRV_DISABLE_TRANS(pip);
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_DRV_DISABLED(ph))
27757c478bd9Sstevel@tonic-gate 		MDI_PI_SET_DRV_DISABLE(pip);
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_old_state = MDI_PATHINFO_STATE_INIT;
27787c478bd9Sstevel@tonic-gate 	cv_init(&MDI_PI(pip)->pi_state_cv, NULL, CV_DEFAULT, NULL);
27797c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client = ct;
27807c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci = ph;
27813c34adc5Sramat 	MDI_PI(pip)->pi_addr = kmem_alloc(strlen(paddr) + 1, KM_SLEEP);
27827c478bd9Sstevel@tonic-gate 	(void) strcpy(MDI_PI(pip)->pi_addr, paddr);
27833c34adc5Sramat 	(void) nvlist_alloc(&MDI_PI(pip)->pi_prop, NV_UNIQUE_NAME, KM_SLEEP);
27843c34adc5Sramat 	ASSERT(MDI_PI(pip)->pi_prop != NULL);
27857c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_pprivate = NULL;
27867c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_cprivate = NULL;
27877c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_vprivate = NULL;
27887c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client_link = NULL;
27897c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci_link = NULL;
27907c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_ref_cnt = 0;
27917c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_kstats = NULL;
27927c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_preferred = 1;
27937c478bd9Sstevel@tonic-gate 	cv_init(&MDI_PI(pip)->pi_ref_cv, NULL, CV_DEFAULT, NULL);
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 	/*
27967c478bd9Sstevel@tonic-gate 	 * Lock both dev_info nodes against changes in parallel.
2797*144dfaa9Scth 	 *
2798*144dfaa9Scth 	 * The ndi_devi_enter(Client), is atypical since the client is a leaf.
2799*144dfaa9Scth 	 * This atypical operation is done to synchronize pathinfo nodes
2800*144dfaa9Scth 	 * during devinfo snapshot (see di_register_pip) by 'pretending' that
2801*144dfaa9Scth 	 * the pathinfo nodes are children of the Client.
28027c478bd9Sstevel@tonic-gate 	 */
28037c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ct->ct_dip, &ct_circular);
28047c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ph->ph_dip, &ph_circular);
28057c478bd9Sstevel@tonic-gate 
28067c478bd9Sstevel@tonic-gate 	i_mdi_phci_add_path(ph, pip);
28077c478bd9Sstevel@tonic-gate 	i_mdi_client_add_path(ct, pip);
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ph->ph_dip, ph_circular);
28107c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ct->ct_dip, ct_circular);
28117c478bd9Sstevel@tonic-gate 
28128c4f8890Srs135747 	/* determine interrupt context */
28138c4f8890Srs135747 	se_flag = (servicing_interrupt()) ? SE_NOSLEEP : SE_SLEEP;
28148c4f8890Srs135747 	kmem_flag = (se_flag == SE_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
28158c4f8890Srs135747 
28168c4f8890Srs135747 	i_ddi_di_cache_invalidate(kmem_flag);
28178c4f8890Srs135747 
28187c478bd9Sstevel@tonic-gate 	return (pip);
28197c478bd9Sstevel@tonic-gate }
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate /*
28227c478bd9Sstevel@tonic-gate  * i_mdi_phci_add_path():
28237c478bd9Sstevel@tonic-gate  * 		Add a mdi_pathinfo node to pHCI list.
28247c478bd9Sstevel@tonic-gate  * Notes:
28257c478bd9Sstevel@tonic-gate  *		Caller should per-pHCI mutex
28267c478bd9Sstevel@tonic-gate  */
28277c478bd9Sstevel@tonic-gate static void
28287c478bd9Sstevel@tonic-gate i_mdi_phci_add_path(mdi_phci_t *ph, mdi_pathinfo_t *pip)
28297c478bd9Sstevel@tonic-gate {
28307c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ph->ph_dip));
28317c478bd9Sstevel@tonic-gate 
2832*144dfaa9Scth 	MDI_PHCI_LOCK(ph);
28337c478bd9Sstevel@tonic-gate 	if (ph->ph_path_head == NULL) {
28347c478bd9Sstevel@tonic-gate 		ph->ph_path_head = pip;
28357c478bd9Sstevel@tonic-gate 	} else {
28367c478bd9Sstevel@tonic-gate 		MDI_PI(ph->ph_path_tail)->pi_phci_link = MDI_PI(pip);
28377c478bd9Sstevel@tonic-gate 	}
28387c478bd9Sstevel@tonic-gate 	ph->ph_path_tail = pip;
28397c478bd9Sstevel@tonic-gate 	ph->ph_path_count++;
2840*144dfaa9Scth 	MDI_PHCI_UNLOCK(ph);
28417c478bd9Sstevel@tonic-gate }
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate /*
28447c478bd9Sstevel@tonic-gate  * i_mdi_client_add_path():
28457c478bd9Sstevel@tonic-gate  *		Add mdi_pathinfo node to client list
28467c478bd9Sstevel@tonic-gate  */
28477c478bd9Sstevel@tonic-gate static void
28487c478bd9Sstevel@tonic-gate i_mdi_client_add_path(mdi_client_t *ct, mdi_pathinfo_t *pip)
28497c478bd9Sstevel@tonic-gate {
28507c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ct->ct_dip));
28517c478bd9Sstevel@tonic-gate 
2852*144dfaa9Scth 	MDI_CLIENT_LOCK(ct);
28537c478bd9Sstevel@tonic-gate 	if (ct->ct_path_head == NULL) {
28547c478bd9Sstevel@tonic-gate 		ct->ct_path_head = pip;
28557c478bd9Sstevel@tonic-gate 	} else {
28567c478bd9Sstevel@tonic-gate 		MDI_PI(ct->ct_path_tail)->pi_client_link = MDI_PI(pip);
28577c478bd9Sstevel@tonic-gate 	}
28587c478bd9Sstevel@tonic-gate 	ct->ct_path_tail = pip;
28597c478bd9Sstevel@tonic-gate 	ct->ct_path_count++;
2860*144dfaa9Scth 	MDI_CLIENT_UNLOCK(ct);
28617c478bd9Sstevel@tonic-gate }
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate /*
28647c478bd9Sstevel@tonic-gate  * mdi_pi_free():
28657c478bd9Sstevel@tonic-gate  *		Free the mdi_pathinfo node and also client device node if this
28667c478bd9Sstevel@tonic-gate  *		is the last path to the device
28677c478bd9Sstevel@tonic-gate  * Return Values:
28687c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
28697c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
28707c478bd9Sstevel@tonic-gate  *		MDI_BUSY
28717c478bd9Sstevel@tonic-gate  */
28727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
28737c478bd9Sstevel@tonic-gate int
28747c478bd9Sstevel@tonic-gate mdi_pi_free(mdi_pathinfo_t *pip, int flags)
28757c478bd9Sstevel@tonic-gate {
28767c478bd9Sstevel@tonic-gate 	int		rv = MDI_SUCCESS;
28777c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
28787c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
28797c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
28807c478bd9Sstevel@tonic-gate 	int		(*f)();
28817c478bd9Sstevel@tonic-gate 	int		client_held = 0;
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
28847c478bd9Sstevel@tonic-gate 	ph = MDI_PI(pip)->pi_phci;
28857c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
28867c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
28877c478bd9Sstevel@tonic-gate 		/*
28887c478bd9Sstevel@tonic-gate 		 * Invalid pHCI device, return failure
28897c478bd9Sstevel@tonic-gate 		 */
28907c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, NULL,
2891*144dfaa9Scth 		    "!mdi_pi_free: invalid pHCI pip=%p", (void *)pip));
28927c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
28937c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
28947c478bd9Sstevel@tonic-gate 	}
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
28977c478bd9Sstevel@tonic-gate 	ASSERT(vh != NULL);
28987c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
28997c478bd9Sstevel@tonic-gate 		/* Invalid pHCI device, return failure */
29007c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, NULL,
2901*144dfaa9Scth 		    "!mdi_pi_free: invalid vHCI pip=%p", (void *)pip));
29027c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
29037c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
29047c478bd9Sstevel@tonic-gate 	}
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 	ct = MDI_PI(pip)->pi_client;
29077c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
29087c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
29097c478bd9Sstevel@tonic-gate 		/*
29107c478bd9Sstevel@tonic-gate 		 * Invalid Client device, return failure
29117c478bd9Sstevel@tonic-gate 		 */
29127c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, NULL,
2913*144dfaa9Scth 		    "!mdi_pi_free: invalid client pip=%p", (void *)pip));
29147c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
29157c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
29167c478bd9Sstevel@tonic-gate 	}
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 	/*
29197c478bd9Sstevel@tonic-gate 	 * Check to see for busy condition.  A mdi_pathinfo can only be freed
29207c478bd9Sstevel@tonic-gate 	 * if the node state is either offline or init and the reference count
29217c478bd9Sstevel@tonic-gate 	 * is zero.
29227c478bd9Sstevel@tonic-gate 	 */
29237c478bd9Sstevel@tonic-gate 	if (!(MDI_PI_IS_OFFLINE(pip) || MDI_PI_IS_INIT(pip) ||
29247c478bd9Sstevel@tonic-gate 	    MDI_PI_IS_INITING(pip))) {
29257c478bd9Sstevel@tonic-gate 		/*
29267c478bd9Sstevel@tonic-gate 		 * Node is busy
29277c478bd9Sstevel@tonic-gate 		 */
2928*144dfaa9Scth 		MDI_DEBUG(1, (CE_WARN, ct->ct_dip,
2929*144dfaa9Scth 		    "!mdi_pi_free: pathinfo node is busy pip=%p", (void *)pip));
29307c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
29317c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
29327c478bd9Sstevel@tonic-gate 	}
29337c478bd9Sstevel@tonic-gate 
29347c478bd9Sstevel@tonic-gate 	while (MDI_PI(pip)->pi_ref_cnt != 0) {
29357c478bd9Sstevel@tonic-gate 		/*
29367c478bd9Sstevel@tonic-gate 		 * Give a chance for pending I/Os to complete.
29377c478bd9Sstevel@tonic-gate 		 */
2938*144dfaa9Scth 		MDI_DEBUG(1, (CE_NOTE, ct->ct_dip, "!mdi_pi_free: "
29397c478bd9Sstevel@tonic-gate 		    "%d cmds still pending on path: %p\n",
2940*144dfaa9Scth 		    MDI_PI(pip)->pi_ref_cnt, (void *)pip));
29417c478bd9Sstevel@tonic-gate 		if (cv_timedwait(&MDI_PI(pip)->pi_ref_cv,
29427c478bd9Sstevel@tonic-gate 		    &MDI_PI(pip)->pi_mutex,
29437c478bd9Sstevel@tonic-gate 		    ddi_get_lbolt() + drv_usectohz(60 * 1000000)) == -1) {
29447c478bd9Sstevel@tonic-gate 			/*
29457c478bd9Sstevel@tonic-gate 			 * The timeout time reached without ref_cnt being zero
29467c478bd9Sstevel@tonic-gate 			 * being signaled.
29477c478bd9Sstevel@tonic-gate 			 */
2948*144dfaa9Scth 			MDI_DEBUG(1, (CE_NOTE, ct->ct_dip,
29498c4f8890Srs135747 			    "!mdi_pi_free: "
29507c478bd9Sstevel@tonic-gate 			    "Timeout reached on path %p without the cond\n",
2951*144dfaa9Scth 			    (void *)pip));
2952*144dfaa9Scth 			MDI_DEBUG(1, (CE_NOTE, ct->ct_dip,
29538c4f8890Srs135747 			    "!mdi_pi_free: "
29547c478bd9Sstevel@tonic-gate 			    "%d cmds still pending on path: %p\n",
2955*144dfaa9Scth 			    MDI_PI(pip)->pi_ref_cnt, (void *)pip));
29567c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
29577c478bd9Sstevel@tonic-gate 			return (MDI_BUSY);
29587c478bd9Sstevel@tonic-gate 		}
29597c478bd9Sstevel@tonic-gate 	}
29607c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held) {
29617c478bd9Sstevel@tonic-gate 		client_held = 1;
29627c478bd9Sstevel@tonic-gate 	}
29637c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
29647c478bd9Sstevel@tonic-gate 
29653c34adc5Sramat 	vhcache_pi_remove(vh->vh_config, MDI_PI(pip));
29663c34adc5Sramat 
29677c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
29687c478bd9Sstevel@tonic-gate 
2969*144dfaa9Scth 	/* Prevent further failovers till MDI_VHCI_CLIENT_LOCK is held */
29707c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_PATH_FREE_IN_PROGRESS(ct);
29717c478bd9Sstevel@tonic-gate 
29727c478bd9Sstevel@tonic-gate 	/*
29737c478bd9Sstevel@tonic-gate 	 * Wait till failover is complete before removing this node.
29747c478bd9Sstevel@tonic-gate 	 */
29757c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct))
29767c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_failover_cv, &ct->ct_mutex);
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
2979*144dfaa9Scth 	MDI_VHCI_CLIENT_LOCK(vh);
29807c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
29817c478bd9Sstevel@tonic-gate 	MDI_CLIENT_CLEAR_PATH_FREE_IN_PROGRESS(ct);
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate 	if (!MDI_PI_IS_INITING(pip)) {
29847c478bd9Sstevel@tonic-gate 		f = vh->vh_ops->vo_pi_uninit;
29857c478bd9Sstevel@tonic-gate 		if (f != NULL) {
29867c478bd9Sstevel@tonic-gate 			rv = (*f)(vh->vh_dip, pip, 0);
29877c478bd9Sstevel@tonic-gate 		}
29887c478bd9Sstevel@tonic-gate 	}
29897c478bd9Sstevel@tonic-gate 	/*
29907c478bd9Sstevel@tonic-gate 	 * If vo_pi_uninit() completed successfully.
29917c478bd9Sstevel@tonic-gate 	 */
29927c478bd9Sstevel@tonic-gate 	if (rv == MDI_SUCCESS) {
29937c478bd9Sstevel@tonic-gate 		if (client_held) {
29947c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, ct->ct_dip, "mdi_pi_free "
29957c478bd9Sstevel@tonic-gate 			    "i_mdi_pm_rele_client\n"));
29967c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, 1);
29977c478bd9Sstevel@tonic-gate 		}
29987c478bd9Sstevel@tonic-gate 		i_mdi_pi_free(ph, pip, ct);
29997c478bd9Sstevel@tonic-gate 		if (ct->ct_path_count == 0) {
30007c478bd9Sstevel@tonic-gate 			/*
30017c478bd9Sstevel@tonic-gate 			 * Client lost its last path.
30027c478bd9Sstevel@tonic-gate 			 * Clean up the client device
30037c478bd9Sstevel@tonic-gate 			 */
30047c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
30057c478bd9Sstevel@tonic-gate 			(void) i_mdi_client_free(ct->ct_vhci, ct);
3006*144dfaa9Scth 			MDI_VHCI_CLIENT_UNLOCK(vh);
30077c478bd9Sstevel@tonic-gate 			return (rv);
30087c478bd9Sstevel@tonic-gate 		}
30097c478bd9Sstevel@tonic-gate 	}
30107c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
3011*144dfaa9Scth 	MDI_VHCI_CLIENT_UNLOCK(vh);
30123c34adc5Sramat 
30133c34adc5Sramat 	if (rv == MDI_FAILURE)
30143c34adc5Sramat 		vhcache_pi_add(vh->vh_config, MDI_PI(pip));
30153c34adc5Sramat 
30167c478bd9Sstevel@tonic-gate 	return (rv);
30177c478bd9Sstevel@tonic-gate }
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate /*
30207c478bd9Sstevel@tonic-gate  * i_mdi_pi_free():
30217c478bd9Sstevel@tonic-gate  *		Free the mdi_pathinfo node
30227c478bd9Sstevel@tonic-gate  */
30237c478bd9Sstevel@tonic-gate static void
30247c478bd9Sstevel@tonic-gate i_mdi_pi_free(mdi_phci_t *ph, mdi_pathinfo_t *pip, mdi_client_t *ct)
30257c478bd9Sstevel@tonic-gate {
30267c478bd9Sstevel@tonic-gate 	int	ct_circular;
30277c478bd9Sstevel@tonic-gate 	int	ph_circular;
30288c4f8890Srs135747 	int	se_flag;
30298c4f8890Srs135747 	int	kmem_flag;
30307c478bd9Sstevel@tonic-gate 
3031*144dfaa9Scth 	ASSERT(MDI_CLIENT_LOCKED(ct));
3032*144dfaa9Scth 
30337c478bd9Sstevel@tonic-gate 	/*
30347c478bd9Sstevel@tonic-gate 	 * remove any per-path kstats
30357c478bd9Sstevel@tonic-gate 	 */
30367c478bd9Sstevel@tonic-gate 	i_mdi_pi_kstat_destroy(pip);
30377c478bd9Sstevel@tonic-gate 
3038*144dfaa9Scth 	/* See comments in i_mdi_pi_alloc() */
30397c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ct->ct_dip, &ct_circular);
30407c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ph->ph_dip, &ph_circular);
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate 	i_mdi_client_remove_path(ct, pip);
30437c478bd9Sstevel@tonic-gate 	i_mdi_phci_remove_path(ph, pip);
30447c478bd9Sstevel@tonic-gate 
30457c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ph->ph_dip, ph_circular);
30467c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ct->ct_dip, ct_circular);
30477c478bd9Sstevel@tonic-gate 
30488c4f8890Srs135747 	/* determine interrupt context */
30498c4f8890Srs135747 	se_flag = (servicing_interrupt()) ? SE_NOSLEEP : SE_SLEEP;
30508c4f8890Srs135747 	kmem_flag = (se_flag == SE_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
30518c4f8890Srs135747 
30528c4f8890Srs135747 	i_ddi_di_cache_invalidate(kmem_flag);
30538c4f8890Srs135747 
30547c478bd9Sstevel@tonic-gate 	mutex_destroy(&MDI_PI(pip)->pi_mutex);
30557c478bd9Sstevel@tonic-gate 	cv_destroy(&MDI_PI(pip)->pi_state_cv);
30567c478bd9Sstevel@tonic-gate 	cv_destroy(&MDI_PI(pip)->pi_ref_cv);
30577c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_addr) {
30587c478bd9Sstevel@tonic-gate 		kmem_free(MDI_PI(pip)->pi_addr,
30597c478bd9Sstevel@tonic-gate 		    strlen(MDI_PI(pip)->pi_addr) + 1);
30607c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_addr = NULL;
30617c478bd9Sstevel@tonic-gate 	}
30627c478bd9Sstevel@tonic-gate 
30637c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop) {
30647c478bd9Sstevel@tonic-gate 		(void) nvlist_free(MDI_PI(pip)->pi_prop);
30657c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_prop = NULL;
30667c478bd9Sstevel@tonic-gate 	}
30677c478bd9Sstevel@tonic-gate 	kmem_free(pip, sizeof (struct mdi_pathinfo));
30687c478bd9Sstevel@tonic-gate }
30697c478bd9Sstevel@tonic-gate 
30707c478bd9Sstevel@tonic-gate 
30717c478bd9Sstevel@tonic-gate /*
30727c478bd9Sstevel@tonic-gate  * i_mdi_phci_remove_path():
30737c478bd9Sstevel@tonic-gate  * 		Remove a mdi_pathinfo node from pHCI list.
30747c478bd9Sstevel@tonic-gate  * Notes:
30757c478bd9Sstevel@tonic-gate  *		Caller should hold per-pHCI mutex
30767c478bd9Sstevel@tonic-gate  */
30777c478bd9Sstevel@tonic-gate static void
30787c478bd9Sstevel@tonic-gate i_mdi_phci_remove_path(mdi_phci_t *ph, mdi_pathinfo_t *pip)
30797c478bd9Sstevel@tonic-gate {
30807c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*prev = NULL;
30817c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*path = NULL;
30827c478bd9Sstevel@tonic-gate 
30837c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ph->ph_dip));
30847c478bd9Sstevel@tonic-gate 
3085*144dfaa9Scth 	MDI_PHCI_LOCK(ph);
30867c478bd9Sstevel@tonic-gate 	path = ph->ph_path_head;
30877c478bd9Sstevel@tonic-gate 	while (path != NULL) {
30887c478bd9Sstevel@tonic-gate 		if (path == pip) {
30897c478bd9Sstevel@tonic-gate 			break;
30907c478bd9Sstevel@tonic-gate 		}
30917c478bd9Sstevel@tonic-gate 		prev = path;
30927c478bd9Sstevel@tonic-gate 		path = (mdi_pathinfo_t *)MDI_PI(path)->pi_phci_link;
30937c478bd9Sstevel@tonic-gate 	}
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 	if (path) {
30967c478bd9Sstevel@tonic-gate 		ph->ph_path_count--;
30977c478bd9Sstevel@tonic-gate 		if (prev) {
30987c478bd9Sstevel@tonic-gate 			MDI_PI(prev)->pi_phci_link = MDI_PI(path)->pi_phci_link;
30997c478bd9Sstevel@tonic-gate 		} else {
31007c478bd9Sstevel@tonic-gate 			ph->ph_path_head =
31017c478bd9Sstevel@tonic-gate 			    (mdi_pathinfo_t *)MDI_PI(path)->pi_phci_link;
31027c478bd9Sstevel@tonic-gate 		}
31037c478bd9Sstevel@tonic-gate 		if (ph->ph_path_tail == path) {
31047c478bd9Sstevel@tonic-gate 			ph->ph_path_tail = prev;
31057c478bd9Sstevel@tonic-gate 		}
31067c478bd9Sstevel@tonic-gate 	}
31077c478bd9Sstevel@tonic-gate 
31087c478bd9Sstevel@tonic-gate 	/*
31097c478bd9Sstevel@tonic-gate 	 * Clear the pHCI link
31107c478bd9Sstevel@tonic-gate 	 */
31117c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci_link = NULL;
31127c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci = NULL;
3113*144dfaa9Scth 	MDI_PHCI_UNLOCK(ph);
31147c478bd9Sstevel@tonic-gate }
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate /*
31177c478bd9Sstevel@tonic-gate  * i_mdi_client_remove_path():
31187c478bd9Sstevel@tonic-gate  * 		Remove a mdi_pathinfo node from client path list.
31197c478bd9Sstevel@tonic-gate  */
31207c478bd9Sstevel@tonic-gate static void
31217c478bd9Sstevel@tonic-gate i_mdi_client_remove_path(mdi_client_t *ct, mdi_pathinfo_t *pip)
31227c478bd9Sstevel@tonic-gate {
31237c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*prev = NULL;
31247c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*path;
31257c478bd9Sstevel@tonic-gate 
31267c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ct->ct_dip));
31277c478bd9Sstevel@tonic-gate 
3128*144dfaa9Scth 	ASSERT(MDI_CLIENT_LOCKED(ct));
31297c478bd9Sstevel@tonic-gate 	path = ct->ct_path_head;
31307c478bd9Sstevel@tonic-gate 	while (path != NULL) {
31317c478bd9Sstevel@tonic-gate 		if (path == pip) {
31327c478bd9Sstevel@tonic-gate 			break;
31337c478bd9Sstevel@tonic-gate 		}
31347c478bd9Sstevel@tonic-gate 		prev = path;
31357c478bd9Sstevel@tonic-gate 		path = (mdi_pathinfo_t *)MDI_PI(path)->pi_client_link;
31367c478bd9Sstevel@tonic-gate 	}
31377c478bd9Sstevel@tonic-gate 
31387c478bd9Sstevel@tonic-gate 	if (path) {
31397c478bd9Sstevel@tonic-gate 		ct->ct_path_count--;
31407c478bd9Sstevel@tonic-gate 		if (prev) {
31417c478bd9Sstevel@tonic-gate 			MDI_PI(prev)->pi_client_link =
31427c478bd9Sstevel@tonic-gate 			    MDI_PI(path)->pi_client_link;
31437c478bd9Sstevel@tonic-gate 		} else {
31447c478bd9Sstevel@tonic-gate 			ct->ct_path_head =
31457c478bd9Sstevel@tonic-gate 			    (mdi_pathinfo_t *)MDI_PI(path)->pi_client_link;
31467c478bd9Sstevel@tonic-gate 		}
31477c478bd9Sstevel@tonic-gate 		if (ct->ct_path_tail == path) {
31487c478bd9Sstevel@tonic-gate 			ct->ct_path_tail = prev;
31497c478bd9Sstevel@tonic-gate 		}
31507c478bd9Sstevel@tonic-gate 		if (ct->ct_path_last == path) {
31517c478bd9Sstevel@tonic-gate 			ct->ct_path_last = ct->ct_path_head;
31527c478bd9Sstevel@tonic-gate 		}
31537c478bd9Sstevel@tonic-gate 	}
31547c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client_link = NULL;
31557c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client = NULL;
31567c478bd9Sstevel@tonic-gate }
31577c478bd9Sstevel@tonic-gate 
31587c478bd9Sstevel@tonic-gate /*
31597c478bd9Sstevel@tonic-gate  * i_mdi_pi_state_change():
31607c478bd9Sstevel@tonic-gate  *		online a mdi_pathinfo node
31617c478bd9Sstevel@tonic-gate  *
31627c478bd9Sstevel@tonic-gate  * Return Values:
31637c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
31647c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
31657c478bd9Sstevel@tonic-gate  */
31667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
31677c478bd9Sstevel@tonic-gate static int
31687c478bd9Sstevel@tonic-gate i_mdi_pi_state_change(mdi_pathinfo_t *pip, mdi_pathinfo_state_t state, int flag)
31697c478bd9Sstevel@tonic-gate {
31707c478bd9Sstevel@tonic-gate 	int		rv = MDI_SUCCESS;
31717c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
31727c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
31737c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
31747c478bd9Sstevel@tonic-gate 	int		(*f)();
31757c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
31767c478bd9Sstevel@tonic-gate 
31777c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate 	ph = MDI_PI(pip)->pi_phci;
31807c478bd9Sstevel@tonic-gate 	ASSERT(ph);
31817c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
31827c478bd9Sstevel@tonic-gate 		/*
31837c478bd9Sstevel@tonic-gate 		 * Invalid pHCI device, fail the request
31847c478bd9Sstevel@tonic-gate 		 */
31857c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
31867c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, NULL,
3187*144dfaa9Scth 		    "!mdi_pi_state_change: invalid phci pip=%p", (void *)pip));
31887c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
31897c478bd9Sstevel@tonic-gate 	}
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
31927c478bd9Sstevel@tonic-gate 	ASSERT(vh);
31937c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
31947c478bd9Sstevel@tonic-gate 		/*
31957c478bd9Sstevel@tonic-gate 		 * Invalid vHCI device, fail the request
31967c478bd9Sstevel@tonic-gate 		 */
31977c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
31987c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, NULL,
3199*144dfaa9Scth 		    "!mdi_pi_state_change: invalid vhci pip=%p", (void *)pip));
32007c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
32017c478bd9Sstevel@tonic-gate 	}
32027c478bd9Sstevel@tonic-gate 
32037c478bd9Sstevel@tonic-gate 	ct = MDI_PI(pip)->pi_client;
32047c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
32057c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
32067c478bd9Sstevel@tonic-gate 		/*
32077c478bd9Sstevel@tonic-gate 		 * Invalid client device, fail the request
32087c478bd9Sstevel@tonic-gate 		 */
32097c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
32107c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, NULL,
3211*144dfaa9Scth 		    "!mdi_pi_state_change: invalid client pip=%p",
3212*144dfaa9Scth 		    (void *)pip));
32137c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
32147c478bd9Sstevel@tonic-gate 	}
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate 	/*
32177c478bd9Sstevel@tonic-gate 	 * If this path has not been initialized yet, Callback vHCI driver's
32187c478bd9Sstevel@tonic-gate 	 * pathinfo node initialize entry point
32197c478bd9Sstevel@tonic-gate 	 */
32207c478bd9Sstevel@tonic-gate 
32217c478bd9Sstevel@tonic-gate 	if (MDI_PI_IS_INITING(pip)) {
32227c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
32237c478bd9Sstevel@tonic-gate 		f = vh->vh_ops->vo_pi_init;
32247c478bd9Sstevel@tonic-gate 		if (f != NULL) {
32257c478bd9Sstevel@tonic-gate 			rv = (*f)(vh->vh_dip, pip, 0);
32267c478bd9Sstevel@tonic-gate 			if (rv != MDI_SUCCESS) {
3227*144dfaa9Scth 				MDI_DEBUG(1, (CE_WARN, ct->ct_dip,
32287c478bd9Sstevel@tonic-gate 				    "!vo_pi_init: failed vHCI=0x%p, pip=0x%p",
3229*144dfaa9Scth 				    (void *)vh, (void *)pip));
32307c478bd9Sstevel@tonic-gate 				return (MDI_FAILURE);
32317c478bd9Sstevel@tonic-gate 			}
32327c478bd9Sstevel@tonic-gate 		}
32337c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
32347c478bd9Sstevel@tonic-gate 		MDI_PI_CLEAR_TRANSIENT(pip);
32357c478bd9Sstevel@tonic-gate 	}
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate 	/*
32387c478bd9Sstevel@tonic-gate 	 * Do not allow state transition when pHCI is in offline/suspended
32397c478bd9Sstevel@tonic-gate 	 * states
32407c478bd9Sstevel@tonic-gate 	 */
32417c478bd9Sstevel@tonic-gate 	i_mdi_phci_lock(ph, pip);
32427c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_READY(ph) == 0) {
3243*144dfaa9Scth 		MDI_DEBUG(1, (CE_WARN, ct->ct_dip,
3244*144dfaa9Scth 		    "!mdi_pi_state_change: pHCI not ready, pHCI=%p",
3245*144dfaa9Scth 		    (void *)ph));
32467c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
32477c478bd9Sstevel@tonic-gate 		i_mdi_phci_unlock(ph);
32487c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
32497c478bd9Sstevel@tonic-gate 	}
32507c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNSTABLE(ph);
32517c478bd9Sstevel@tonic-gate 	i_mdi_phci_unlock(ph);
32527c478bd9Sstevel@tonic-gate 
32537c478bd9Sstevel@tonic-gate 	/*
32547c478bd9Sstevel@tonic-gate 	 * Check if mdi_pathinfo state is in transient state.
32557c478bd9Sstevel@tonic-gate 	 * If yes, offlining is in progress and wait till transient state is
32567c478bd9Sstevel@tonic-gate 	 * cleared.
32577c478bd9Sstevel@tonic-gate 	 */
32587c478bd9Sstevel@tonic-gate 	if (MDI_PI_IS_TRANSIENT(pip)) {
32597c478bd9Sstevel@tonic-gate 		while (MDI_PI_IS_TRANSIENT(pip)) {
32607c478bd9Sstevel@tonic-gate 			cv_wait(&MDI_PI(pip)->pi_state_cv,
32617c478bd9Sstevel@tonic-gate 			    &MDI_PI(pip)->pi_mutex);
32627c478bd9Sstevel@tonic-gate 		}
32637c478bd9Sstevel@tonic-gate 	}
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate 	/*
32667c478bd9Sstevel@tonic-gate 	 * Grab the client lock in reverse order sequence and release the
32677c478bd9Sstevel@tonic-gate 	 * mdi_pathinfo mutex.
32687c478bd9Sstevel@tonic-gate 	 */
32697c478bd9Sstevel@tonic-gate 	i_mdi_client_lock(ct, pip);
32707c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 	/*
32737c478bd9Sstevel@tonic-gate 	 * Wait till failover state is cleared
32747c478bd9Sstevel@tonic-gate 	 */
32757c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct))
32767c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_failover_cv, &ct->ct_mutex);
32777c478bd9Sstevel@tonic-gate 
32787c478bd9Sstevel@tonic-gate 	/*
32797c478bd9Sstevel@tonic-gate 	 * Mark the mdi_pathinfo node state as transient
32807c478bd9Sstevel@tonic-gate 	 */
32817c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
32827c478bd9Sstevel@tonic-gate 	switch (state) {
32837c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_ONLINE:
32847c478bd9Sstevel@tonic-gate 		MDI_PI_SET_ONLINING(pip);
32857c478bd9Sstevel@tonic-gate 		break;
32867c478bd9Sstevel@tonic-gate 
32877c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_STANDBY:
32887c478bd9Sstevel@tonic-gate 		MDI_PI_SET_STANDBYING(pip);
32897c478bd9Sstevel@tonic-gate 		break;
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_FAULT:
32927c478bd9Sstevel@tonic-gate 		/*
32937c478bd9Sstevel@tonic-gate 		 * Mark the pathinfo state as FAULTED
32947c478bd9Sstevel@tonic-gate 		 */
32957c478bd9Sstevel@tonic-gate 		MDI_PI_SET_FAULTING(pip);
32967c478bd9Sstevel@tonic-gate 		MDI_PI_ERRSTAT(pip, MDI_PI_HARDERR);
32977c478bd9Sstevel@tonic-gate 		break;
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_OFFLINE:
33007c478bd9Sstevel@tonic-gate 		/*
33017c478bd9Sstevel@tonic-gate 		 * ndi_devi_offline() cannot hold pip or ct locks.
33027c478bd9Sstevel@tonic-gate 		 */
33037c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
33047c478bd9Sstevel@tonic-gate 		/*
33057c478bd9Sstevel@tonic-gate 		 * Do not offline if path will become last path and path
33067c478bd9Sstevel@tonic-gate 		 * is busy for user initiated events.
33077c478bd9Sstevel@tonic-gate 		 */
33087c478bd9Sstevel@tonic-gate 		cdip = ct->ct_dip;
33097c478bd9Sstevel@tonic-gate 		if ((flag & NDI_DEVI_REMOVE) &&
33107c478bd9Sstevel@tonic-gate 		    (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_DEGRADED)) {
33117c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
33127c478bd9Sstevel@tonic-gate 			rv = ndi_devi_offline(cdip, 0);
33137c478bd9Sstevel@tonic-gate 			if (rv != NDI_SUCCESS) {
33147c478bd9Sstevel@tonic-gate 				/*
33157c478bd9Sstevel@tonic-gate 				 * Convert to MDI error code
33167c478bd9Sstevel@tonic-gate 				 */
33177c478bd9Sstevel@tonic-gate 				switch (rv) {
33187c478bd9Sstevel@tonic-gate 				case NDI_BUSY:
33197c478bd9Sstevel@tonic-gate 					rv = MDI_BUSY;
33207c478bd9Sstevel@tonic-gate 					break;
33217c478bd9Sstevel@tonic-gate 				default:
33227c478bd9Sstevel@tonic-gate 					rv = MDI_FAILURE;
33237c478bd9Sstevel@tonic-gate 					break;
33247c478bd9Sstevel@tonic-gate 				}
33257c478bd9Sstevel@tonic-gate 				goto state_change_exit;
33267c478bd9Sstevel@tonic-gate 			} else {
33277c478bd9Sstevel@tonic-gate 				i_mdi_client_lock(ct, NULL);
33287c478bd9Sstevel@tonic-gate 			}
33297c478bd9Sstevel@tonic-gate 		}
33307c478bd9Sstevel@tonic-gate 		/*
33317c478bd9Sstevel@tonic-gate 		 * Mark the mdi_pathinfo node state as transient
33327c478bd9Sstevel@tonic-gate 		 */
33337c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
33347c478bd9Sstevel@tonic-gate 		MDI_PI_SET_OFFLINING(pip);
33357c478bd9Sstevel@tonic-gate 		break;
33367c478bd9Sstevel@tonic-gate 	}
33377c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
33387c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNSTABLE(ct);
33397c478bd9Sstevel@tonic-gate 	i_mdi_client_unlock(ct);
33407c478bd9Sstevel@tonic-gate 
33417c478bd9Sstevel@tonic-gate 	f = vh->vh_ops->vo_pi_state_change;
3342*144dfaa9Scth 	if (f != NULL)
33437c478bd9Sstevel@tonic-gate 		rv = (*f)(vh->vh_dip, pip, state, 0, flag);
3344*144dfaa9Scth 
3345*144dfaa9Scth 	MDI_CLIENT_LOCK(ct);
3346*144dfaa9Scth 	MDI_PI_LOCK(pip);
33477c478bd9Sstevel@tonic-gate 	if (rv == MDI_NOT_SUPPORTED) {
33487c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_DEV_NOT_SUPPORTED(ct);
33497c478bd9Sstevel@tonic-gate 	}
33507c478bd9Sstevel@tonic-gate 	if (rv != MDI_SUCCESS) {
3351*144dfaa9Scth 		MDI_DEBUG(2, (CE_WARN, ct->ct_dip,
33527c478bd9Sstevel@tonic-gate 		    "!vo_pi_state_change: failed rv = %x", rv));
33537c478bd9Sstevel@tonic-gate 	}
33547c478bd9Sstevel@tonic-gate 	if (MDI_PI_IS_TRANSIENT(pip)) {
33557c478bd9Sstevel@tonic-gate 		if (rv == MDI_SUCCESS) {
33567c478bd9Sstevel@tonic-gate 			MDI_PI_CLEAR_TRANSIENT(pip);
33577c478bd9Sstevel@tonic-gate 		} else {
33587c478bd9Sstevel@tonic-gate 			MDI_PI(pip)->pi_state = MDI_PI_OLD_STATE(pip);
33597c478bd9Sstevel@tonic-gate 		}
33607c478bd9Sstevel@tonic-gate 	}
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate 	/*
33637c478bd9Sstevel@tonic-gate 	 * Wake anyone waiting for this mdi_pathinfo node
33647c478bd9Sstevel@tonic-gate 	 */
33657c478bd9Sstevel@tonic-gate 	cv_broadcast(&MDI_PI(pip)->pi_state_cv);
33667c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
33677c478bd9Sstevel@tonic-gate 
33687c478bd9Sstevel@tonic-gate 	/*
33697c478bd9Sstevel@tonic-gate 	 * Mark the client device as stable
33707c478bd9Sstevel@tonic-gate 	 */
33717c478bd9Sstevel@tonic-gate 	MDI_CLIENT_STABLE(ct);
33727c478bd9Sstevel@tonic-gate 	if (rv == MDI_SUCCESS) {
33737c478bd9Sstevel@tonic-gate 		if (ct->ct_unstable == 0) {
33747c478bd9Sstevel@tonic-gate 			cdip = ct->ct_dip;
33757c478bd9Sstevel@tonic-gate 
33767c478bd9Sstevel@tonic-gate 			/*
33777c478bd9Sstevel@tonic-gate 			 * Onlining the mdi_pathinfo node will impact the
33787c478bd9Sstevel@tonic-gate 			 * client state Update the client and dev_info node
33797c478bd9Sstevel@tonic-gate 			 * state accordingly
33807c478bd9Sstevel@tonic-gate 			 */
33817c478bd9Sstevel@tonic-gate 			rv = NDI_SUCCESS;
33827c478bd9Sstevel@tonic-gate 			i_mdi_client_update_state(ct);
33837c478bd9Sstevel@tonic-gate 			switch (MDI_CLIENT_STATE(ct)) {
33847c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_OPTIMAL:
33857c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_DEGRADED:
3386737d277aScth 				if (cdip && !i_ddi_devi_attached(cdip) &&
33877c478bd9Sstevel@tonic-gate 				    ((state == MDI_PATHINFO_STATE_ONLINE) ||
33887c478bd9Sstevel@tonic-gate 				    (state == MDI_PATHINFO_STATE_STANDBY))) {
33897c478bd9Sstevel@tonic-gate 
33907c478bd9Sstevel@tonic-gate 					/*
33917c478bd9Sstevel@tonic-gate 					 * Must do ndi_devi_online() through
33927c478bd9Sstevel@tonic-gate 					 * hotplug thread for deferred
33937c478bd9Sstevel@tonic-gate 					 * attach mechanism to work
33947c478bd9Sstevel@tonic-gate 					 */
3395*144dfaa9Scth 					MDI_CLIENT_UNLOCK(ct);
33967c478bd9Sstevel@tonic-gate 					rv = ndi_devi_online(cdip, 0);
3397*144dfaa9Scth 					MDI_CLIENT_LOCK(ct);
33987c478bd9Sstevel@tonic-gate 					if ((rv != NDI_SUCCESS) &&
33997c478bd9Sstevel@tonic-gate 					    (MDI_CLIENT_STATE(ct) ==
34007c478bd9Sstevel@tonic-gate 					    MDI_CLIENT_STATE_DEGRADED)) {
34017c478bd9Sstevel@tonic-gate 						/*
34027c478bd9Sstevel@tonic-gate 						 * ndi_devi_online failed.
34037c478bd9Sstevel@tonic-gate 						 * Reset client flags to
34047c478bd9Sstevel@tonic-gate 						 * offline.
34057c478bd9Sstevel@tonic-gate 						 */
34067c478bd9Sstevel@tonic-gate 						MDI_DEBUG(1, (CE_WARN, cdip,
34077c478bd9Sstevel@tonic-gate 						    "!ndi_devi_online: failed "
34087c478bd9Sstevel@tonic-gate 						    " Error: %x", rv));
34097c478bd9Sstevel@tonic-gate 						MDI_CLIENT_SET_OFFLINE(ct);
34107c478bd9Sstevel@tonic-gate 					}
34117c478bd9Sstevel@tonic-gate 					if (rv != NDI_SUCCESS) {
34127c478bd9Sstevel@tonic-gate 						/* Reset the path state */
34137c478bd9Sstevel@tonic-gate 						MDI_PI_LOCK(pip);
34147c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_state =
34157c478bd9Sstevel@tonic-gate 						    MDI_PI_OLD_STATE(pip);
34167c478bd9Sstevel@tonic-gate 						MDI_PI_UNLOCK(pip);
34177c478bd9Sstevel@tonic-gate 					}
34187c478bd9Sstevel@tonic-gate 				}
34197c478bd9Sstevel@tonic-gate 				break;
34207c478bd9Sstevel@tonic-gate 
34217c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_FAILED:
34227c478bd9Sstevel@tonic-gate 				/*
34237c478bd9Sstevel@tonic-gate 				 * This is the last path case for
34247c478bd9Sstevel@tonic-gate 				 * non-user initiated events.
34257c478bd9Sstevel@tonic-gate 				 */
34267c478bd9Sstevel@tonic-gate 				if (((flag & NDI_DEVI_REMOVE) == 0) &&
34277c478bd9Sstevel@tonic-gate 				    cdip && (i_ddi_node_state(cdip) >=
34287c478bd9Sstevel@tonic-gate 				    DS_INITIALIZED)) {
3429*144dfaa9Scth 					MDI_CLIENT_UNLOCK(ct);
34307c478bd9Sstevel@tonic-gate 					rv = ndi_devi_offline(cdip, 0);
3431*144dfaa9Scth 					MDI_CLIENT_LOCK(ct);
34327c478bd9Sstevel@tonic-gate 
34337c478bd9Sstevel@tonic-gate 					if (rv != NDI_SUCCESS) {
34347c478bd9Sstevel@tonic-gate 						/*
34357c478bd9Sstevel@tonic-gate 						 * ndi_devi_offline failed.
34367c478bd9Sstevel@tonic-gate 						 * Reset client flags to
34377c478bd9Sstevel@tonic-gate 						 * online as the path could not
34387c478bd9Sstevel@tonic-gate 						 * be offlined.
34397c478bd9Sstevel@tonic-gate 						 */
34407c478bd9Sstevel@tonic-gate 						MDI_DEBUG(1, (CE_WARN, cdip,
34417c478bd9Sstevel@tonic-gate 						    "!ndi_devi_offline: failed "
34427c478bd9Sstevel@tonic-gate 						    " Error: %x", rv));
34437c478bd9Sstevel@tonic-gate 						MDI_CLIENT_SET_ONLINE(ct);
34447c478bd9Sstevel@tonic-gate 					}
34457c478bd9Sstevel@tonic-gate 				}
34467c478bd9Sstevel@tonic-gate 				break;
34477c478bd9Sstevel@tonic-gate 			}
34487c478bd9Sstevel@tonic-gate 			/*
34497c478bd9Sstevel@tonic-gate 			 * Convert to MDI error code
34507c478bd9Sstevel@tonic-gate 			 */
34517c478bd9Sstevel@tonic-gate 			switch (rv) {
34527c478bd9Sstevel@tonic-gate 			case NDI_SUCCESS:
34537c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_REPORT_DEV_NEEDED(ct);
34547c478bd9Sstevel@tonic-gate 				i_mdi_report_path_state(ct, pip);
34557c478bd9Sstevel@tonic-gate 				rv = MDI_SUCCESS;
34567c478bd9Sstevel@tonic-gate 				break;
34577c478bd9Sstevel@tonic-gate 			case NDI_BUSY:
34587c478bd9Sstevel@tonic-gate 				rv = MDI_BUSY;
34597c478bd9Sstevel@tonic-gate 				break;
34607c478bd9Sstevel@tonic-gate 			default:
34617c478bd9Sstevel@tonic-gate 				rv = MDI_FAILURE;
34627c478bd9Sstevel@tonic-gate 				break;
34637c478bd9Sstevel@tonic-gate 			}
34647c478bd9Sstevel@tonic-gate 		}
34657c478bd9Sstevel@tonic-gate 	}
34667c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate state_change_exit:
34697c478bd9Sstevel@tonic-gate 	/*
34707c478bd9Sstevel@tonic-gate 	 * Mark the pHCI as stable again.
34717c478bd9Sstevel@tonic-gate 	 */
34727c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
34737c478bd9Sstevel@tonic-gate 	MDI_PHCI_STABLE(ph);
34747c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
34757c478bd9Sstevel@tonic-gate 	return (rv);
34767c478bd9Sstevel@tonic-gate }
34777c478bd9Sstevel@tonic-gate 
34787c478bd9Sstevel@tonic-gate /*
34797c478bd9Sstevel@tonic-gate  * mdi_pi_online():
34807c478bd9Sstevel@tonic-gate  *		Place the path_info node in the online state.  The path is
34817c478bd9Sstevel@tonic-gate  *		now available to be selected by mdi_select_path() for
34827c478bd9Sstevel@tonic-gate  *		transporting I/O requests to client devices.
34837c478bd9Sstevel@tonic-gate  * Return Values:
34847c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
34857c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
34867c478bd9Sstevel@tonic-gate  */
34877c478bd9Sstevel@tonic-gate int
34887c478bd9Sstevel@tonic-gate mdi_pi_online(mdi_pathinfo_t *pip, int flags)
34897c478bd9Sstevel@tonic-gate {
34907c478bd9Sstevel@tonic-gate 	mdi_client_t *ct = MDI_PI(pip)->pi_client;
34917c478bd9Sstevel@tonic-gate 	dev_info_t *cdip;
34927c478bd9Sstevel@tonic-gate 	int		client_held = 0;
34937c478bd9Sstevel@tonic-gate 	int rv;
34947c478bd9Sstevel@tonic-gate 
34957c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
34967c478bd9Sstevel@tonic-gate 	rv = i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_ONLINE, flags);
34977c478bd9Sstevel@tonic-gate 	if (rv != MDI_SUCCESS)
34987c478bd9Sstevel@tonic-gate 		return (rv);
34997c478bd9Sstevel@tonic-gate 
35007c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
35017c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held == 0) {
35027c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, ct->ct_dip, "mdi_pi_online "
3503*144dfaa9Scth 		    "i_mdi_pm_hold_pip %p\n", (void *)pip));
35047c478bd9Sstevel@tonic-gate 		i_mdi_pm_hold_pip(pip);
35057c478bd9Sstevel@tonic-gate 		client_held = 1;
35067c478bd9Sstevel@tonic-gate 	}
35077c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
35087c478bd9Sstevel@tonic-gate 
35097c478bd9Sstevel@tonic-gate 	if (client_held) {
35107c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
35117c478bd9Sstevel@tonic-gate 		if (ct->ct_power_cnt == 0) {
35127c478bd9Sstevel@tonic-gate 			rv = i_mdi_power_all_phci(ct);
35137c478bd9Sstevel@tonic-gate 		}
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, ct->ct_dip, "mdi_pi_online "
3516*144dfaa9Scth 		    "i_mdi_pm_hold_client %p\n", (void *)ct));
35177c478bd9Sstevel@tonic-gate 		i_mdi_pm_hold_client(ct, 1);
35187c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
35197c478bd9Sstevel@tonic-gate 	}
35207c478bd9Sstevel@tonic-gate 
35217c478bd9Sstevel@tonic-gate 	/*
35227c478bd9Sstevel@tonic-gate 	 * Create the per-path (pathinfo) IO and error kstats which
35237c478bd9Sstevel@tonic-gate 	 * are reported via iostat(1m).
35247c478bd9Sstevel@tonic-gate 	 *
35257c478bd9Sstevel@tonic-gate 	 * Defer creating the per-path kstats if device is not yet
35267c478bd9Sstevel@tonic-gate 	 * attached;  the names of the kstats are constructed in part
35277c478bd9Sstevel@tonic-gate 	 * using the devices instance number which is assigned during
35287c478bd9Sstevel@tonic-gate 	 * process of attaching the client device.
35297c478bd9Sstevel@tonic-gate 	 *
35307c478bd9Sstevel@tonic-gate 	 * The framework post_attach handler, mdi_post_attach(), is
35317c478bd9Sstevel@tonic-gate 	 * is responsible for initializing the client's pathinfo list
35327c478bd9Sstevel@tonic-gate 	 * once successfully attached.
35337c478bd9Sstevel@tonic-gate 	 */
35347c478bd9Sstevel@tonic-gate 	cdip = ct->ct_dip;
35357c478bd9Sstevel@tonic-gate 	ASSERT(cdip);
3536737d277aScth 	if (cdip == NULL || !i_ddi_devi_attached(cdip))
35377c478bd9Sstevel@tonic-gate 		return (rv);
35387c478bd9Sstevel@tonic-gate 
35397c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
35407c478bd9Sstevel@tonic-gate 	rv = i_mdi_pi_kstat_create(pip);
35417c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
35427c478bd9Sstevel@tonic-gate 	return (rv);
35437c478bd9Sstevel@tonic-gate }
35447c478bd9Sstevel@tonic-gate 
35457c478bd9Sstevel@tonic-gate /*
35467c478bd9Sstevel@tonic-gate  * mdi_pi_standby():
35477c478bd9Sstevel@tonic-gate  *		Place the mdi_pathinfo node in standby state
35487c478bd9Sstevel@tonic-gate  *
35497c478bd9Sstevel@tonic-gate  * Return Values:
35507c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
35517c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
35527c478bd9Sstevel@tonic-gate  */
35537c478bd9Sstevel@tonic-gate int
35547c478bd9Sstevel@tonic-gate mdi_pi_standby(mdi_pathinfo_t *pip, int flags)
35557c478bd9Sstevel@tonic-gate {
35567c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_STANDBY, flags));
35577c478bd9Sstevel@tonic-gate }
35587c478bd9Sstevel@tonic-gate 
35597c478bd9Sstevel@tonic-gate /*
35607c478bd9Sstevel@tonic-gate  * mdi_pi_fault():
35617c478bd9Sstevel@tonic-gate  *		Place the mdi_pathinfo node in fault'ed state
35627c478bd9Sstevel@tonic-gate  * Return Values:
35637c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
35647c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
35657c478bd9Sstevel@tonic-gate  */
35667c478bd9Sstevel@tonic-gate int
35677c478bd9Sstevel@tonic-gate mdi_pi_fault(mdi_pathinfo_t *pip, int flags)
35687c478bd9Sstevel@tonic-gate {
35697c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_FAULT, flags));
35707c478bd9Sstevel@tonic-gate }
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate /*
35737c478bd9Sstevel@tonic-gate  * mdi_pi_offline():
35747c478bd9Sstevel@tonic-gate  *		Offline a mdi_pathinfo node.
35757c478bd9Sstevel@tonic-gate  * Return Values:
35767c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
35777c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
35787c478bd9Sstevel@tonic-gate  */
35797c478bd9Sstevel@tonic-gate int
35807c478bd9Sstevel@tonic-gate mdi_pi_offline(mdi_pathinfo_t *pip, int flags)
35817c478bd9Sstevel@tonic-gate {
35827c478bd9Sstevel@tonic-gate 	int	ret, client_held = 0;
35837c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
35847c478bd9Sstevel@tonic-gate 
35857c478bd9Sstevel@tonic-gate 	ret = i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_OFFLINE, flags);
35867c478bd9Sstevel@tonic-gate 
35877c478bd9Sstevel@tonic-gate 	if (ret == MDI_SUCCESS) {
35887c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
35897c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_pm_held) {
35907c478bd9Sstevel@tonic-gate 			client_held = 1;
35917c478bd9Sstevel@tonic-gate 		}
35927c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
35937c478bd9Sstevel@tonic-gate 
35947c478bd9Sstevel@tonic-gate 		if (client_held) {
35957c478bd9Sstevel@tonic-gate 			ct = MDI_PI(pip)->pi_client;
35967c478bd9Sstevel@tonic-gate 			MDI_CLIENT_LOCK(ct);
35977c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, ct->ct_dip,
35987c478bd9Sstevel@tonic-gate 			    "mdi_pi_offline i_mdi_pm_rele_client\n"));
35997c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, 1);
36007c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
36017c478bd9Sstevel@tonic-gate 		}
36027c478bd9Sstevel@tonic-gate 	}
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate 	return (ret);
36057c478bd9Sstevel@tonic-gate }
36067c478bd9Sstevel@tonic-gate 
36077c478bd9Sstevel@tonic-gate /*
36087c478bd9Sstevel@tonic-gate  * i_mdi_pi_offline():
36097c478bd9Sstevel@tonic-gate  *		Offline a mdi_pathinfo node and call the vHCI driver's callback
36107c478bd9Sstevel@tonic-gate  */
36117c478bd9Sstevel@tonic-gate static int
36127c478bd9Sstevel@tonic-gate i_mdi_pi_offline(mdi_pathinfo_t *pip, int flags)
36137c478bd9Sstevel@tonic-gate {
36147c478bd9Sstevel@tonic-gate 	dev_info_t	*vdip = NULL;
36157c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
36167c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct = NULL;
36177c478bd9Sstevel@tonic-gate 	int		(*f)();
36187c478bd9Sstevel@tonic-gate 	int		rv;
36197c478bd9Sstevel@tonic-gate 
36207c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
36217c478bd9Sstevel@tonic-gate 	ct = MDI_PI(pip)->pi_client;
36227c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
36237c478bd9Sstevel@tonic-gate 
36247c478bd9Sstevel@tonic-gate 	while (MDI_PI(pip)->pi_ref_cnt != 0) {
36257c478bd9Sstevel@tonic-gate 		/*
36267c478bd9Sstevel@tonic-gate 		 * Give a chance for pending I/Os to complete.
36277c478bd9Sstevel@tonic-gate 		 */
3628*144dfaa9Scth 		MDI_DEBUG(1, (CE_NOTE, ct->ct_dip, "!i_mdi_pi_offline: "
36297c478bd9Sstevel@tonic-gate 		    "%d cmds still pending on path: %p\n",
3630*144dfaa9Scth 		    MDI_PI(pip)->pi_ref_cnt, (void *)pip));
36317c478bd9Sstevel@tonic-gate 		if (cv_timedwait(&MDI_PI(pip)->pi_ref_cv,
36327c478bd9Sstevel@tonic-gate 		    &MDI_PI(pip)->pi_mutex,
36337c478bd9Sstevel@tonic-gate 		    ddi_get_lbolt() + drv_usectohz(60 * 1000000)) == -1) {
36347c478bd9Sstevel@tonic-gate 			/*
36357c478bd9Sstevel@tonic-gate 			 * The timeout time reached without ref_cnt being zero
36367c478bd9Sstevel@tonic-gate 			 * being signaled.
36377c478bd9Sstevel@tonic-gate 			 */
3638*144dfaa9Scth 			MDI_DEBUG(1, (CE_NOTE, ct->ct_dip, "!i_mdi_pi_offline: "
36397c478bd9Sstevel@tonic-gate 			    "Timeout reached on path %p without the cond\n",
3640*144dfaa9Scth 			    (void *)pip));
3641*144dfaa9Scth 			MDI_DEBUG(1, (CE_NOTE, ct->ct_dip, "!i_mdi_pi_offline: "
36427c478bd9Sstevel@tonic-gate 			    "%d cmds still pending on path: %p\n",
3643*144dfaa9Scth 			    MDI_PI(pip)->pi_ref_cnt, (void *)pip));
36447c478bd9Sstevel@tonic-gate 		}
36457c478bd9Sstevel@tonic-gate 	}
36467c478bd9Sstevel@tonic-gate 	vh = ct->ct_vhci;
36477c478bd9Sstevel@tonic-gate 	vdip = vh->vh_dip;
36487c478bd9Sstevel@tonic-gate 
36497c478bd9Sstevel@tonic-gate 	/*
36507c478bd9Sstevel@tonic-gate 	 * Notify vHCI that has registered this event
36517c478bd9Sstevel@tonic-gate 	 */
36527c478bd9Sstevel@tonic-gate 	ASSERT(vh->vh_ops);
36537c478bd9Sstevel@tonic-gate 	f = vh->vh_ops->vo_pi_state_change;
36547c478bd9Sstevel@tonic-gate 
36557c478bd9Sstevel@tonic-gate 	if (f != NULL) {
36567c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
36577c478bd9Sstevel@tonic-gate 		if ((rv = (*f)(vdip, pip, MDI_PATHINFO_STATE_OFFLINE, 0,
36587c478bd9Sstevel@tonic-gate 		    flags)) != MDI_SUCCESS) {
3659*144dfaa9Scth 			MDI_DEBUG(1, (CE_WARN, ct->ct_dip,
3660*144dfaa9Scth 			    "!vo_path_offline failed "
3661*144dfaa9Scth 			    "vdip %p, pip %p", (void *)vdip, (void *)pip));
36627c478bd9Sstevel@tonic-gate 		}
36637c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
36647c478bd9Sstevel@tonic-gate 	}
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate 	/*
36677c478bd9Sstevel@tonic-gate 	 * Set the mdi_pathinfo node state and clear the transient condition
36687c478bd9Sstevel@tonic-gate 	 */
36697c478bd9Sstevel@tonic-gate 	MDI_PI_SET_OFFLINE(pip);
36707c478bd9Sstevel@tonic-gate 	cv_broadcast(&MDI_PI(pip)->pi_state_cv);
36717c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
36727c478bd9Sstevel@tonic-gate 
36737c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
36747c478bd9Sstevel@tonic-gate 	if (rv == MDI_SUCCESS) {
36757c478bd9Sstevel@tonic-gate 		if (ct->ct_unstable == 0) {
36767c478bd9Sstevel@tonic-gate 			dev_info_t	*cdip = ct->ct_dip;
36777c478bd9Sstevel@tonic-gate 
36787c478bd9Sstevel@tonic-gate 			/*
36797c478bd9Sstevel@tonic-gate 			 * Onlining the mdi_pathinfo node will impact the
36807c478bd9Sstevel@tonic-gate 			 * client state Update the client and dev_info node
36817c478bd9Sstevel@tonic-gate 			 * state accordingly
36827c478bd9Sstevel@tonic-gate 			 */
36837c478bd9Sstevel@tonic-gate 			i_mdi_client_update_state(ct);
36847c478bd9Sstevel@tonic-gate 			rv = NDI_SUCCESS;
36857c478bd9Sstevel@tonic-gate 			if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_FAILED) {
36867c478bd9Sstevel@tonic-gate 				if (cdip &&
36877c478bd9Sstevel@tonic-gate 				    (i_ddi_node_state(cdip) >=
36887c478bd9Sstevel@tonic-gate 				    DS_INITIALIZED)) {
36897c478bd9Sstevel@tonic-gate 					MDI_CLIENT_UNLOCK(ct);
36907c478bd9Sstevel@tonic-gate 					rv = ndi_devi_offline(cdip, 0);
36917c478bd9Sstevel@tonic-gate 					MDI_CLIENT_LOCK(ct);
36927c478bd9Sstevel@tonic-gate 					if (rv != NDI_SUCCESS) {
36937c478bd9Sstevel@tonic-gate 						/*
36947c478bd9Sstevel@tonic-gate 						 * ndi_devi_offline failed.
36957c478bd9Sstevel@tonic-gate 						 * Reset client flags to
36967c478bd9Sstevel@tonic-gate 						 * online.
36977c478bd9Sstevel@tonic-gate 						 */
36987c478bd9Sstevel@tonic-gate 						MDI_DEBUG(4, (CE_WARN, cdip,
36997c478bd9Sstevel@tonic-gate 						    "!ndi_devi_offline: failed "
37007c478bd9Sstevel@tonic-gate 						    " Error: %x", rv));
37017c478bd9Sstevel@tonic-gate 						MDI_CLIENT_SET_ONLINE(ct);
37027c478bd9Sstevel@tonic-gate 					}
37037c478bd9Sstevel@tonic-gate 				}
37047c478bd9Sstevel@tonic-gate 			}
37057c478bd9Sstevel@tonic-gate 			/*
37067c478bd9Sstevel@tonic-gate 			 * Convert to MDI error code
37077c478bd9Sstevel@tonic-gate 			 */
37087c478bd9Sstevel@tonic-gate 			switch (rv) {
37097c478bd9Sstevel@tonic-gate 			case NDI_SUCCESS:
37107c478bd9Sstevel@tonic-gate 				rv = MDI_SUCCESS;
37117c478bd9Sstevel@tonic-gate 				break;
37127c478bd9Sstevel@tonic-gate 			case NDI_BUSY:
37137c478bd9Sstevel@tonic-gate 				rv = MDI_BUSY;
37147c478bd9Sstevel@tonic-gate 				break;
37157c478bd9Sstevel@tonic-gate 			default:
37167c478bd9Sstevel@tonic-gate 				rv = MDI_FAILURE;
37177c478bd9Sstevel@tonic-gate 				break;
37187c478bd9Sstevel@tonic-gate 			}
37197c478bd9Sstevel@tonic-gate 		}
37207c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_REPORT_DEV_NEEDED(ct);
37217c478bd9Sstevel@tonic-gate 		i_mdi_report_path_state(ct, pip);
37227c478bd9Sstevel@tonic-gate 	}
37237c478bd9Sstevel@tonic-gate 
37247c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
37257c478bd9Sstevel@tonic-gate 
37267c478bd9Sstevel@tonic-gate 	/*
37277c478bd9Sstevel@tonic-gate 	 * Change in the mdi_pathinfo node state will impact the client state
37287c478bd9Sstevel@tonic-gate 	 */
37297c478bd9Sstevel@tonic-gate 	MDI_DEBUG(2, (CE_NOTE, NULL, "!i_mdi_pi_offline ct = %p pip = %p",
3730*144dfaa9Scth 	    (void *)ct, (void *)pip));
37317c478bd9Sstevel@tonic-gate 	return (rv);
37327c478bd9Sstevel@tonic-gate }
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate 
37357c478bd9Sstevel@tonic-gate /*
37367c478bd9Sstevel@tonic-gate  * mdi_pi_get_addr():
37377c478bd9Sstevel@tonic-gate  *		Get the unit address associated with a mdi_pathinfo node
37387c478bd9Sstevel@tonic-gate  *
37397c478bd9Sstevel@tonic-gate  * Return Values:
37407c478bd9Sstevel@tonic-gate  *		char *
37417c478bd9Sstevel@tonic-gate  */
37427c478bd9Sstevel@tonic-gate char *
37437c478bd9Sstevel@tonic-gate mdi_pi_get_addr(mdi_pathinfo_t *pip)
37447c478bd9Sstevel@tonic-gate {
37457c478bd9Sstevel@tonic-gate 	if (pip == NULL)
37467c478bd9Sstevel@tonic-gate 		return (NULL);
37477c478bd9Sstevel@tonic-gate 
374872a50065Scth 	return (MDI_PI(pip)->pi_addr);
37497c478bd9Sstevel@tonic-gate }
37507c478bd9Sstevel@tonic-gate 
37517c478bd9Sstevel@tonic-gate /*
37527c478bd9Sstevel@tonic-gate  * mdi_pi_get_client():
37537c478bd9Sstevel@tonic-gate  *		Get the client devinfo associated with a mdi_pathinfo node
37547c478bd9Sstevel@tonic-gate  *
37557c478bd9Sstevel@tonic-gate  * Return Values:
37567c478bd9Sstevel@tonic-gate  *		Handle to client device dev_info node
37577c478bd9Sstevel@tonic-gate  */
37587c478bd9Sstevel@tonic-gate dev_info_t *
37597c478bd9Sstevel@tonic-gate mdi_pi_get_client(mdi_pathinfo_t *pip)
37607c478bd9Sstevel@tonic-gate {
37617c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
37627c478bd9Sstevel@tonic-gate 	if (pip) {
37637c478bd9Sstevel@tonic-gate 		dip = MDI_PI(pip)->pi_client->ct_dip;
37647c478bd9Sstevel@tonic-gate 	}
37657c478bd9Sstevel@tonic-gate 	return (dip);
37667c478bd9Sstevel@tonic-gate }
37677c478bd9Sstevel@tonic-gate 
37687c478bd9Sstevel@tonic-gate /*
37697c478bd9Sstevel@tonic-gate  * mdi_pi_get_phci():
37707c478bd9Sstevel@tonic-gate  *		Get the pHCI devinfo associated with the mdi_pathinfo node
37717c478bd9Sstevel@tonic-gate  * Return Values:
37727c478bd9Sstevel@tonic-gate  *		Handle to dev_info node
37737c478bd9Sstevel@tonic-gate  */
37747c478bd9Sstevel@tonic-gate dev_info_t *
37757c478bd9Sstevel@tonic-gate mdi_pi_get_phci(mdi_pathinfo_t *pip)
37767c478bd9Sstevel@tonic-gate {
37777c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
37787c478bd9Sstevel@tonic-gate 	if (pip) {
37797c478bd9Sstevel@tonic-gate 		dip = MDI_PI(pip)->pi_phci->ph_dip;
37807c478bd9Sstevel@tonic-gate 	}
37817c478bd9Sstevel@tonic-gate 	return (dip);
37827c478bd9Sstevel@tonic-gate }
37837c478bd9Sstevel@tonic-gate 
37847c478bd9Sstevel@tonic-gate /*
37857c478bd9Sstevel@tonic-gate  * mdi_pi_get_client_private():
37867c478bd9Sstevel@tonic-gate  *		Get the client private information associated with the
37877c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node
37887c478bd9Sstevel@tonic-gate  */
37897c478bd9Sstevel@tonic-gate void *
37907c478bd9Sstevel@tonic-gate mdi_pi_get_client_private(mdi_pathinfo_t *pip)
37917c478bd9Sstevel@tonic-gate {
37927c478bd9Sstevel@tonic-gate 	void *cprivate = NULL;
37937c478bd9Sstevel@tonic-gate 	if (pip) {
37947c478bd9Sstevel@tonic-gate 		cprivate = MDI_PI(pip)->pi_cprivate;
37957c478bd9Sstevel@tonic-gate 	}
37967c478bd9Sstevel@tonic-gate 	return (cprivate);
37977c478bd9Sstevel@tonic-gate }
37987c478bd9Sstevel@tonic-gate 
37997c478bd9Sstevel@tonic-gate /*
38007c478bd9Sstevel@tonic-gate  * mdi_pi_set_client_private():
38017c478bd9Sstevel@tonic-gate  *		Set the client private information in the mdi_pathinfo node
38027c478bd9Sstevel@tonic-gate  */
38037c478bd9Sstevel@tonic-gate void
38047c478bd9Sstevel@tonic-gate mdi_pi_set_client_private(mdi_pathinfo_t *pip, void *priv)
38057c478bd9Sstevel@tonic-gate {
38067c478bd9Sstevel@tonic-gate 	if (pip) {
38077c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_cprivate = priv;
38087c478bd9Sstevel@tonic-gate 	}
38097c478bd9Sstevel@tonic-gate }
38107c478bd9Sstevel@tonic-gate 
38117c478bd9Sstevel@tonic-gate /*
38127c478bd9Sstevel@tonic-gate  * mdi_pi_get_phci_private():
38137c478bd9Sstevel@tonic-gate  *		Get the pHCI private information associated with the
38147c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node
38157c478bd9Sstevel@tonic-gate  */
38167c478bd9Sstevel@tonic-gate caddr_t
38177c478bd9Sstevel@tonic-gate mdi_pi_get_phci_private(mdi_pathinfo_t *pip)
38187c478bd9Sstevel@tonic-gate {
38197c478bd9Sstevel@tonic-gate 	caddr_t	pprivate = NULL;
38207c478bd9Sstevel@tonic-gate 	if (pip) {
38217c478bd9Sstevel@tonic-gate 		pprivate = MDI_PI(pip)->pi_pprivate;
38227c478bd9Sstevel@tonic-gate 	}
38237c478bd9Sstevel@tonic-gate 	return (pprivate);
38247c478bd9Sstevel@tonic-gate }
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate /*
38277c478bd9Sstevel@tonic-gate  * mdi_pi_set_phci_private():
38287c478bd9Sstevel@tonic-gate  *		Set the pHCI private information in the mdi_pathinfo node
38297c478bd9Sstevel@tonic-gate  */
38307c478bd9Sstevel@tonic-gate void
38317c478bd9Sstevel@tonic-gate mdi_pi_set_phci_private(mdi_pathinfo_t *pip, caddr_t priv)
38327c478bd9Sstevel@tonic-gate {
38337c478bd9Sstevel@tonic-gate 	if (pip) {
38347c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_pprivate = priv;
38357c478bd9Sstevel@tonic-gate 	}
38367c478bd9Sstevel@tonic-gate }
38377c478bd9Sstevel@tonic-gate 
38387c478bd9Sstevel@tonic-gate /*
38397c478bd9Sstevel@tonic-gate  * mdi_pi_get_state():
38407c478bd9Sstevel@tonic-gate  *		Get the mdi_pathinfo node state. Transient states are internal
38417c478bd9Sstevel@tonic-gate  *		and not provided to the users
38427c478bd9Sstevel@tonic-gate  */
38437c478bd9Sstevel@tonic-gate mdi_pathinfo_state_t
38447c478bd9Sstevel@tonic-gate mdi_pi_get_state(mdi_pathinfo_t *pip)
38457c478bd9Sstevel@tonic-gate {
38467c478bd9Sstevel@tonic-gate 	mdi_pathinfo_state_t    state = MDI_PATHINFO_STATE_INIT;
38477c478bd9Sstevel@tonic-gate 
38487c478bd9Sstevel@tonic-gate 	if (pip) {
38497c478bd9Sstevel@tonic-gate 		if (MDI_PI_IS_TRANSIENT(pip)) {
38507c478bd9Sstevel@tonic-gate 			/*
38517c478bd9Sstevel@tonic-gate 			 * mdi_pathinfo is in state transition.  Return the
38527c478bd9Sstevel@tonic-gate 			 * last good state.
38537c478bd9Sstevel@tonic-gate 			 */
38547c478bd9Sstevel@tonic-gate 			state = MDI_PI_OLD_STATE(pip);
38557c478bd9Sstevel@tonic-gate 		} else {
38567c478bd9Sstevel@tonic-gate 			state = MDI_PI_STATE(pip);
38577c478bd9Sstevel@tonic-gate 		}
38587c478bd9Sstevel@tonic-gate 	}
38597c478bd9Sstevel@tonic-gate 	return (state);
38607c478bd9Sstevel@tonic-gate }
38617c478bd9Sstevel@tonic-gate 
38627c478bd9Sstevel@tonic-gate /*
38637c478bd9Sstevel@tonic-gate  * Note that the following function needs to be the new interface for
38647c478bd9Sstevel@tonic-gate  * mdi_pi_get_state when mpxio gets integrated to ON.
38657c478bd9Sstevel@tonic-gate  */
38667c478bd9Sstevel@tonic-gate int
38677c478bd9Sstevel@tonic-gate mdi_pi_get_state2(mdi_pathinfo_t *pip, mdi_pathinfo_state_t *state,
38687c478bd9Sstevel@tonic-gate 		uint32_t *ext_state)
38697c478bd9Sstevel@tonic-gate {
38707c478bd9Sstevel@tonic-gate 	*state = MDI_PATHINFO_STATE_INIT;
38717c478bd9Sstevel@tonic-gate 
38727c478bd9Sstevel@tonic-gate 	if (pip) {
38737c478bd9Sstevel@tonic-gate 		if (MDI_PI_IS_TRANSIENT(pip)) {
38747c478bd9Sstevel@tonic-gate 			/*
38757c478bd9Sstevel@tonic-gate 			 * mdi_pathinfo is in state transition.  Return the
38767c478bd9Sstevel@tonic-gate 			 * last good state.
38777c478bd9Sstevel@tonic-gate 			 */
38787c478bd9Sstevel@tonic-gate 			*state = MDI_PI_OLD_STATE(pip);
38797c478bd9Sstevel@tonic-gate 			*ext_state = MDI_PI_OLD_EXT_STATE(pip);
38807c478bd9Sstevel@tonic-gate 		} else {
38817c478bd9Sstevel@tonic-gate 			*state = MDI_PI_STATE(pip);
38827c478bd9Sstevel@tonic-gate 			*ext_state = MDI_PI_EXT_STATE(pip);
38837c478bd9Sstevel@tonic-gate 		}
38847c478bd9Sstevel@tonic-gate 	}
38857c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
38867c478bd9Sstevel@tonic-gate }
38877c478bd9Sstevel@tonic-gate 
38887c478bd9Sstevel@tonic-gate /*
38897c478bd9Sstevel@tonic-gate  * mdi_pi_get_preferred:
38907c478bd9Sstevel@tonic-gate  *	Get the preferred path flag
38917c478bd9Sstevel@tonic-gate  */
38927c478bd9Sstevel@tonic-gate int
38937c478bd9Sstevel@tonic-gate mdi_pi_get_preferred(mdi_pathinfo_t *pip)
38947c478bd9Sstevel@tonic-gate {
38957c478bd9Sstevel@tonic-gate 	if (pip) {
38967c478bd9Sstevel@tonic-gate 		return (MDI_PI(pip)->pi_preferred);
38977c478bd9Sstevel@tonic-gate 	}
38987c478bd9Sstevel@tonic-gate 	return (0);
38997c478bd9Sstevel@tonic-gate }
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate /*
39027c478bd9Sstevel@tonic-gate  * mdi_pi_set_preferred:
39037c478bd9Sstevel@tonic-gate  *	Set the preferred path flag
39047c478bd9Sstevel@tonic-gate  */
39057c478bd9Sstevel@tonic-gate void
39067c478bd9Sstevel@tonic-gate mdi_pi_set_preferred(mdi_pathinfo_t *pip, int preferred)
39077c478bd9Sstevel@tonic-gate {
39087c478bd9Sstevel@tonic-gate 	if (pip) {
39097c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_preferred = preferred;
39107c478bd9Sstevel@tonic-gate 	}
39117c478bd9Sstevel@tonic-gate }
39127c478bd9Sstevel@tonic-gate 
39137c478bd9Sstevel@tonic-gate /*
39147c478bd9Sstevel@tonic-gate  * mdi_pi_set_state():
39157c478bd9Sstevel@tonic-gate  *		Set the mdi_pathinfo node state
39167c478bd9Sstevel@tonic-gate  */
39177c478bd9Sstevel@tonic-gate void
39187c478bd9Sstevel@tonic-gate mdi_pi_set_state(mdi_pathinfo_t *pip, mdi_pathinfo_state_t state)
39197c478bd9Sstevel@tonic-gate {
39207c478bd9Sstevel@tonic-gate 	uint32_t	ext_state;
39217c478bd9Sstevel@tonic-gate 
39227c478bd9Sstevel@tonic-gate 	if (pip) {
39237c478bd9Sstevel@tonic-gate 		ext_state = MDI_PI(pip)->pi_state & MDI_PATHINFO_EXT_STATE_MASK;
39247c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_state = state;
39257c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_state |= ext_state;
39267c478bd9Sstevel@tonic-gate 	}
39277c478bd9Sstevel@tonic-gate }
39287c478bd9Sstevel@tonic-gate 
39297c478bd9Sstevel@tonic-gate /*
39307c478bd9Sstevel@tonic-gate  * Property functions:
39317c478bd9Sstevel@tonic-gate  */
39327c478bd9Sstevel@tonic-gate int
39337c478bd9Sstevel@tonic-gate i_map_nvlist_error_to_mdi(int val)
39347c478bd9Sstevel@tonic-gate {
39357c478bd9Sstevel@tonic-gate 	int rv;
39367c478bd9Sstevel@tonic-gate 
39377c478bd9Sstevel@tonic-gate 	switch (val) {
39387c478bd9Sstevel@tonic-gate 	case 0:
39397c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_SUCCESS;
39407c478bd9Sstevel@tonic-gate 		break;
39417c478bd9Sstevel@tonic-gate 	case EINVAL:
39427c478bd9Sstevel@tonic-gate 	case ENOTSUP:
39437c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_INVAL_ARG;
39447c478bd9Sstevel@tonic-gate 		break;
39457c478bd9Sstevel@tonic-gate 	case ENOMEM:
39467c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_NO_MEMORY;
39477c478bd9Sstevel@tonic-gate 		break;
39487c478bd9Sstevel@tonic-gate 	default:
39497c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_NOT_FOUND;
39507c478bd9Sstevel@tonic-gate 		break;
39517c478bd9Sstevel@tonic-gate 	}
39527c478bd9Sstevel@tonic-gate 	return (rv);
39537c478bd9Sstevel@tonic-gate }
39547c478bd9Sstevel@tonic-gate 
39557c478bd9Sstevel@tonic-gate /*
39567c478bd9Sstevel@tonic-gate  * mdi_pi_get_next_prop():
39577c478bd9Sstevel@tonic-gate  * 		Property walk function.  The caller should hold mdi_pi_lock()
39587c478bd9Sstevel@tonic-gate  *		and release by calling mdi_pi_unlock() at the end of walk to
39597c478bd9Sstevel@tonic-gate  *		get a consistent value.
39607c478bd9Sstevel@tonic-gate  */
39617c478bd9Sstevel@tonic-gate nvpair_t *
39627c478bd9Sstevel@tonic-gate mdi_pi_get_next_prop(mdi_pathinfo_t *pip, nvpair_t *prev)
39637c478bd9Sstevel@tonic-gate {
39647c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
39657c478bd9Sstevel@tonic-gate 		return (NULL);
39667c478bd9Sstevel@tonic-gate 	}
3967*144dfaa9Scth 	ASSERT(MDI_PI_LOCKED(pip));
39687c478bd9Sstevel@tonic-gate 	return (nvlist_next_nvpair(MDI_PI(pip)->pi_prop, prev));
39697c478bd9Sstevel@tonic-gate }
39707c478bd9Sstevel@tonic-gate 
39717c478bd9Sstevel@tonic-gate /*
39727c478bd9Sstevel@tonic-gate  * mdi_prop_remove():
39737c478bd9Sstevel@tonic-gate  * 		Remove the named property from the named list.
39747c478bd9Sstevel@tonic-gate  */
39757c478bd9Sstevel@tonic-gate int
39767c478bd9Sstevel@tonic-gate mdi_prop_remove(mdi_pathinfo_t *pip, char *name)
39777c478bd9Sstevel@tonic-gate {
39787c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
39797c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
39807c478bd9Sstevel@tonic-gate 	}
3981*144dfaa9Scth 	ASSERT(!MDI_PI_LOCKED(pip));
39827c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
39837c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
39847c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
39857c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
39867c478bd9Sstevel@tonic-gate 	}
39877c478bd9Sstevel@tonic-gate 	if (name) {
39887c478bd9Sstevel@tonic-gate 		(void) nvlist_remove_all(MDI_PI(pip)->pi_prop, name);
39897c478bd9Sstevel@tonic-gate 	} else {
39907c478bd9Sstevel@tonic-gate 		char		nvp_name[MAXNAMELEN];
39917c478bd9Sstevel@tonic-gate 		nvpair_t	*nvp;
39927c478bd9Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(MDI_PI(pip)->pi_prop, NULL);
39937c478bd9Sstevel@tonic-gate 		while (nvp) {
39947c478bd9Sstevel@tonic-gate 			nvpair_t	*next;
39957c478bd9Sstevel@tonic-gate 			next = nvlist_next_nvpair(MDI_PI(pip)->pi_prop, nvp);
39967c478bd9Sstevel@tonic-gate 			(void) snprintf(nvp_name, MAXNAMELEN, "%s",
39977c478bd9Sstevel@tonic-gate 			    nvpair_name(nvp));
39987c478bd9Sstevel@tonic-gate 			(void) nvlist_remove_all(MDI_PI(pip)->pi_prop,
39997c478bd9Sstevel@tonic-gate 			    nvp_name);
40007c478bd9Sstevel@tonic-gate 			nvp = next;
40017c478bd9Sstevel@tonic-gate 		}
40027c478bd9Sstevel@tonic-gate 	}
40037c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
40047c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
40057c478bd9Sstevel@tonic-gate }
40067c478bd9Sstevel@tonic-gate 
40077c478bd9Sstevel@tonic-gate /*
40087c478bd9Sstevel@tonic-gate  * mdi_prop_size():
40097c478bd9Sstevel@tonic-gate  * 		Get buffer size needed to pack the property data.
40107c478bd9Sstevel@tonic-gate  * 		Caller should hold the mdi_pathinfo_t lock to get a consistent
40117c478bd9Sstevel@tonic-gate  *		buffer size.
40127c478bd9Sstevel@tonic-gate  */
40137c478bd9Sstevel@tonic-gate int
40147c478bd9Sstevel@tonic-gate mdi_prop_size(mdi_pathinfo_t *pip, size_t *buflenp)
40157c478bd9Sstevel@tonic-gate {
40167c478bd9Sstevel@tonic-gate 	int	rv;
40177c478bd9Sstevel@tonic-gate 	size_t	bufsize;
40187c478bd9Sstevel@tonic-gate 
40197c478bd9Sstevel@tonic-gate 	*buflenp = 0;
40207c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
40217c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
40227c478bd9Sstevel@tonic-gate 	}
4023*144dfaa9Scth 	ASSERT(MDI_PI_LOCKED(pip));
40247c478bd9Sstevel@tonic-gate 	rv = nvlist_size(MDI_PI(pip)->pi_prop,
40257c478bd9Sstevel@tonic-gate 	    &bufsize, NV_ENCODE_NATIVE);
40267c478bd9Sstevel@tonic-gate 	*buflenp = bufsize;
40277c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
40287c478bd9Sstevel@tonic-gate }
40297c478bd9Sstevel@tonic-gate 
40307c478bd9Sstevel@tonic-gate /*
40317c478bd9Sstevel@tonic-gate  * mdi_prop_pack():
40327c478bd9Sstevel@tonic-gate  * 		pack the property list.  The caller should hold the
40337c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node to get a consistent data
40347c478bd9Sstevel@tonic-gate  */
40357c478bd9Sstevel@tonic-gate int
40367c478bd9Sstevel@tonic-gate mdi_prop_pack(mdi_pathinfo_t *pip, char **bufp, uint_t buflen)
40377c478bd9Sstevel@tonic-gate {
40387c478bd9Sstevel@tonic-gate 	int	rv;
40397c478bd9Sstevel@tonic-gate 	size_t	bufsize;
40407c478bd9Sstevel@tonic-gate 
40417c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || MDI_PI(pip)->pi_prop == NULL) {
40427c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
40437c478bd9Sstevel@tonic-gate 	}
40447c478bd9Sstevel@tonic-gate 
4045*144dfaa9Scth 	ASSERT(MDI_PI_LOCKED(pip));
40467c478bd9Sstevel@tonic-gate 
40477c478bd9Sstevel@tonic-gate 	bufsize = buflen;
40487c478bd9Sstevel@tonic-gate 	rv = nvlist_pack(MDI_PI(pip)->pi_prop, bufp, (size_t *)&bufsize,
40497c478bd9Sstevel@tonic-gate 	    NV_ENCODE_NATIVE, KM_SLEEP);
40507c478bd9Sstevel@tonic-gate 
40517c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
40527c478bd9Sstevel@tonic-gate }
40537c478bd9Sstevel@tonic-gate 
40547c478bd9Sstevel@tonic-gate /*
40557c478bd9Sstevel@tonic-gate  * mdi_prop_update_byte():
40567c478bd9Sstevel@tonic-gate  *		Create/Update a byte property
40577c478bd9Sstevel@tonic-gate  */
40587c478bd9Sstevel@tonic-gate int
40597c478bd9Sstevel@tonic-gate mdi_prop_update_byte(mdi_pathinfo_t *pip, char *name, uchar_t data)
40607c478bd9Sstevel@tonic-gate {
40617c478bd9Sstevel@tonic-gate 	int rv;
40627c478bd9Sstevel@tonic-gate 
40637c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
40647c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
40657c478bd9Sstevel@tonic-gate 	}
4066*144dfaa9Scth 	ASSERT(!MDI_PI_LOCKED(pip));
40677c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
40687c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
40697c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
40707c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
40717c478bd9Sstevel@tonic-gate 	}
40727c478bd9Sstevel@tonic-gate 	rv = nvlist_add_byte(MDI_PI(pip)->pi_prop, name, data);
40737c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
40747c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
40757c478bd9Sstevel@tonic-gate }
40767c478bd9Sstevel@tonic-gate 
40777c478bd9Sstevel@tonic-gate /*
40787c478bd9Sstevel@tonic-gate  * mdi_prop_update_byte_array():
40797c478bd9Sstevel@tonic-gate  *		Create/Update a byte array property
40807c478bd9Sstevel@tonic-gate  */
40817c478bd9Sstevel@tonic-gate int
40827c478bd9Sstevel@tonic-gate mdi_prop_update_byte_array(mdi_pathinfo_t *pip, char *name, uchar_t *data,
40837c478bd9Sstevel@tonic-gate     uint_t nelements)
40847c478bd9Sstevel@tonic-gate {
40857c478bd9Sstevel@tonic-gate 	int rv;
40867c478bd9Sstevel@tonic-gate 
40877c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
40887c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
40897c478bd9Sstevel@tonic-gate 	}
4090*144dfaa9Scth 	ASSERT(!MDI_PI_LOCKED(pip));
40917c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
40927c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
40937c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
40947c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
40957c478bd9Sstevel@tonic-gate 	}
40967c478bd9Sstevel@tonic-gate 	rv = nvlist_add_byte_array(MDI_PI(pip)->pi_prop, name, data, nelements);
40977c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
40987c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
40997c478bd9Sstevel@tonic-gate }
41007c478bd9Sstevel@tonic-gate 
41017c478bd9Sstevel@tonic-gate /*
41027c478bd9Sstevel@tonic-gate  * mdi_prop_update_int():
41037c478bd9Sstevel@tonic-gate  *		Create/Update a 32 bit integer property
41047c478bd9Sstevel@tonic-gate  */
41057c478bd9Sstevel@tonic-gate int
41067c478bd9Sstevel@tonic-gate mdi_prop_update_int(mdi_pathinfo_t *pip, char *name, int data)
41077c478bd9Sstevel@tonic-gate {
41087c478bd9Sstevel@tonic-gate 	int rv;
41097c478bd9Sstevel@tonic-gate 
41107c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
41117c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
41127c478bd9Sstevel@tonic-gate 	}
4113*144dfaa9Scth 	ASSERT(!MDI_PI_LOCKED(pip));
41147c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
41157c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
41167c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
41177c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
41187c478bd9Sstevel@tonic-gate 	}
41197c478bd9Sstevel@tonic-gate 	rv = nvlist_add_int32(MDI_PI(pip)->pi_prop, name, (int32_t)data);
41207c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
41217c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
41227c478bd9Sstevel@tonic-gate }
41237c478bd9Sstevel@tonic-gate 
41247c478bd9Sstevel@tonic-gate /*
41257c478bd9Sstevel@tonic-gate  * mdi_prop_update_int64():
41267c478bd9Sstevel@tonic-gate  *		Create/Update a 64 bit integer property
41277c478bd9Sstevel@tonic-gate  */
41287c478bd9Sstevel@tonic-gate int
41297c478bd9Sstevel@tonic-gate mdi_prop_update_int64(mdi_pathinfo_t *pip, char *name, int64_t data)
41307c478bd9Sstevel@tonic-gate {
41317c478bd9Sstevel@tonic-gate 	int rv;
41327c478bd9Sstevel@tonic-gate 
41337c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
41347c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
41357c478bd9Sstevel@tonic-gate 	}
4136*144dfaa9Scth 	ASSERT(!MDI_PI_LOCKED(pip));
41377c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
41387c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
41397c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
41407c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
41417c478bd9Sstevel@tonic-gate 	}
41427c478bd9Sstevel@tonic-gate 	rv = nvlist_add_int64(MDI_PI(pip)->pi_prop, name, data);
41437c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
41447c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
41457c478bd9Sstevel@tonic-gate }
41467c478bd9Sstevel@tonic-gate 
41477c478bd9Sstevel@tonic-gate /*
41487c478bd9Sstevel@tonic-gate  * mdi_prop_update_int_array():
41497c478bd9Sstevel@tonic-gate  *		Create/Update a int array property
41507c478bd9Sstevel@tonic-gate  */
41517c478bd9Sstevel@tonic-gate int
41527c478bd9Sstevel@tonic-gate mdi_prop_update_int_array(mdi_pathinfo_t *pip, char *name, int *data,
41537c478bd9Sstevel@tonic-gate 	    uint_t nelements)
41547c478bd9Sstevel@tonic-gate {
41557c478bd9Sstevel@tonic-gate 	int rv;
41567c478bd9Sstevel@tonic-gate 
41577c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
41587c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
41597c478bd9Sstevel@tonic-gate 	}
4160*144dfaa9Scth 	ASSERT(!MDI_PI_LOCKED(pip));
41617c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
41627c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
41637c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
41647c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
41657c478bd9Sstevel@tonic-gate 	}
41667c478bd9Sstevel@tonic-gate 	rv = nvlist_add_int32_array(MDI_PI(pip)->pi_prop, name, (int32_t *)data,
41677c478bd9Sstevel@tonic-gate 	    nelements);
41687c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
41697c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
41707c478bd9Sstevel@tonic-gate }
41717c478bd9Sstevel@tonic-gate 
41727c478bd9Sstevel@tonic-gate /*
41737c478bd9Sstevel@tonic-gate  * mdi_prop_update_string():
41747c478bd9Sstevel@tonic-gate  *		Create/Update a string property
41757c478bd9Sstevel@tonic-gate  */
41767c478bd9Sstevel@tonic-gate int
41777c478bd9Sstevel@tonic-gate mdi_prop_update_string(mdi_pathinfo_t *pip, char *name, char *data)
41787c478bd9Sstevel@tonic-gate {
41797c478bd9Sstevel@tonic-gate 	int rv;
41807c478bd9Sstevel@tonic-gate 
41817c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
41827c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
41837c478bd9Sstevel@tonic-gate 	}
4184*144dfaa9Scth 	ASSERT(!MDI_PI_LOCKED(pip));
41857c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
41867c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
41877c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
41887c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
41897c478bd9Sstevel@tonic-gate 	}
41907c478bd9Sstevel@tonic-gate 	rv = nvlist_add_string(MDI_PI(pip)->pi_prop, name, data);
41917c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
41927c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
41937c478bd9Sstevel@tonic-gate }
41947c478bd9Sstevel@tonic-gate 
41957c478bd9Sstevel@tonic-gate /*
41967c478bd9Sstevel@tonic-gate  * mdi_prop_update_string_array():
41977c478bd9Sstevel@tonic-gate  *		Create/Update a string array property
41987c478bd9Sstevel@tonic-gate  */
41997c478bd9Sstevel@tonic-gate int
42007c478bd9Sstevel@tonic-gate mdi_prop_update_string_array(mdi_pathinfo_t *pip, char *name, char **data,
42017c478bd9Sstevel@tonic-gate     uint_t nelements)
42027c478bd9Sstevel@tonic-gate {
42037c478bd9Sstevel@tonic-gate 	int rv;
42047c478bd9Sstevel@tonic-gate 
42057c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
42067c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
42077c478bd9Sstevel@tonic-gate 	}
4208*144dfaa9Scth 	ASSERT(!MDI_PI_LOCKED(pip));
42097c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
42107c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
42117c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
42127c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
42137c478bd9Sstevel@tonic-gate 	}
42147c478bd9Sstevel@tonic-gate 	rv = nvlist_add_string_array(MDI_PI(pip)->pi_prop, name, data,
42157c478bd9Sstevel@tonic-gate 	    nelements);
42167c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
42177c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
42187c478bd9Sstevel@tonic-gate }
42197c478bd9Sstevel@tonic-gate 
42207c478bd9Sstevel@tonic-gate /*
42217c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_byte():
42227c478bd9Sstevel@tonic-gate  * 		Look for byte property identified by name.  The data returned
42237c478bd9Sstevel@tonic-gate  *		is the actual property and valid as long as mdi_pathinfo_t node
42247c478bd9Sstevel@tonic-gate  *		is alive.
42257c478bd9Sstevel@tonic-gate  */
42267c478bd9Sstevel@tonic-gate int
42277c478bd9Sstevel@tonic-gate mdi_prop_lookup_byte(mdi_pathinfo_t *pip, char *name, uchar_t *data)
42287c478bd9Sstevel@tonic-gate {
42297c478bd9Sstevel@tonic-gate 	int rv;
42307c478bd9Sstevel@tonic-gate 
42317c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
42327c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
42337c478bd9Sstevel@tonic-gate 	}
42347c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_byte(MDI_PI(pip)->pi_prop, name, data);
42357c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
42367c478bd9Sstevel@tonic-gate }
42377c478bd9Sstevel@tonic-gate 
42387c478bd9Sstevel@tonic-gate 
42397c478bd9Sstevel@tonic-gate /*
42407c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_byte_array():
42417c478bd9Sstevel@tonic-gate  * 		Look for byte array property identified by name.  The data
42427c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
42437c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
42447c478bd9Sstevel@tonic-gate  */
42457c478bd9Sstevel@tonic-gate int
42467c478bd9Sstevel@tonic-gate mdi_prop_lookup_byte_array(mdi_pathinfo_t *pip, char *name, uchar_t **data,
42477c478bd9Sstevel@tonic-gate     uint_t *nelements)
42487c478bd9Sstevel@tonic-gate {
42497c478bd9Sstevel@tonic-gate 	int rv;
42507c478bd9Sstevel@tonic-gate 
42517c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
42527c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
42537c478bd9Sstevel@tonic-gate 	}
42547c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_byte_array(MDI_PI(pip)->pi_prop, name, data,
42557c478bd9Sstevel@tonic-gate 	    nelements);
42567c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
42577c478bd9Sstevel@tonic-gate }
42587c478bd9Sstevel@tonic-gate 
42597c478bd9Sstevel@tonic-gate /*
42607c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_int():
42617c478bd9Sstevel@tonic-gate  * 		Look for int property identified by name.  The data returned
42627c478bd9Sstevel@tonic-gate  *		is the actual property and valid as long as mdi_pathinfo_t
42637c478bd9Sstevel@tonic-gate  *		node is alive.
42647c478bd9Sstevel@tonic-gate  */
42657c478bd9Sstevel@tonic-gate int
42667c478bd9Sstevel@tonic-gate mdi_prop_lookup_int(mdi_pathinfo_t *pip, char *name, int *data)
42677c478bd9Sstevel@tonic-gate {
42687c478bd9Sstevel@tonic-gate 	int rv;
42697c478bd9Sstevel@tonic-gate 
42707c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
42717c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
42727c478bd9Sstevel@tonic-gate 	}
42737c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_int32(MDI_PI(pip)->pi_prop, name, (int32_t *)data);
42747c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
42757c478bd9Sstevel@tonic-gate }
42767c478bd9Sstevel@tonic-gate 
42777c478bd9Sstevel@tonic-gate /*
42787c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_int64():
42797c478bd9Sstevel@tonic-gate  * 		Look for int64 property identified by name.  The data returned
42807c478bd9Sstevel@tonic-gate  *		is the actual property and valid as long as mdi_pathinfo_t node
42817c478bd9Sstevel@tonic-gate  *		is alive.
42827c478bd9Sstevel@tonic-gate  */
42837c478bd9Sstevel@tonic-gate int
42847c478bd9Sstevel@tonic-gate mdi_prop_lookup_int64(mdi_pathinfo_t *pip, char *name, int64_t *data)
42857c478bd9Sstevel@tonic-gate {
42867c478bd9Sstevel@tonic-gate 	int rv;
42877c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
42887c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
42897c478bd9Sstevel@tonic-gate 	}
42907c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_int64(MDI_PI(pip)->pi_prop, name, data);
42917c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
42927c478bd9Sstevel@tonic-gate }
42937c478bd9Sstevel@tonic-gate 
42947c478bd9Sstevel@tonic-gate /*
42957c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_int_array():
42967c478bd9Sstevel@tonic-gate  * 		Look for int array property identified by name.  The data
42977c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
42987c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
42997c478bd9Sstevel@tonic-gate  */
43007c478bd9Sstevel@tonic-gate int
43017c478bd9Sstevel@tonic-gate mdi_prop_lookup_int_array(mdi_pathinfo_t *pip, char *name, int **data,
43027c478bd9Sstevel@tonic-gate     uint_t *nelements)
43037c478bd9Sstevel@tonic-gate {
43047c478bd9Sstevel@tonic-gate 	int rv;
43057c478bd9Sstevel@tonic-gate 
43067c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
43077c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
43087c478bd9Sstevel@tonic-gate 	}
43097c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_int32_array(MDI_PI(pip)->pi_prop, name,
43107c478bd9Sstevel@tonic-gate 	    (int32_t **)data, nelements);
43117c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
43127c478bd9Sstevel@tonic-gate }
43137c478bd9Sstevel@tonic-gate 
43147c478bd9Sstevel@tonic-gate /*
43157c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_string():
43167c478bd9Sstevel@tonic-gate  * 		Look for string property identified by name.  The data
43177c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
43187c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
43197c478bd9Sstevel@tonic-gate  */
43207c478bd9Sstevel@tonic-gate int
43217c478bd9Sstevel@tonic-gate mdi_prop_lookup_string(mdi_pathinfo_t *pip, char *name, char **data)
43227c478bd9Sstevel@tonic-gate {
43237c478bd9Sstevel@tonic-gate 	int rv;
43247c478bd9Sstevel@tonic-gate 
43257c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
43267c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
43277c478bd9Sstevel@tonic-gate 	}
43287c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_string(MDI_PI(pip)->pi_prop, name, data);
43297c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
43307c478bd9Sstevel@tonic-gate }
43317c478bd9Sstevel@tonic-gate 
43327c478bd9Sstevel@tonic-gate /*
43337c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_string_array():
43347c478bd9Sstevel@tonic-gate  * 		Look for string array property identified by name.  The data
43357c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
43367c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
43377c478bd9Sstevel@tonic-gate  */
43387c478bd9Sstevel@tonic-gate int
43397c478bd9Sstevel@tonic-gate mdi_prop_lookup_string_array(mdi_pathinfo_t *pip, char *name, char ***data,
43407c478bd9Sstevel@tonic-gate     uint_t *nelements)
43417c478bd9Sstevel@tonic-gate {
43427c478bd9Sstevel@tonic-gate 	int rv;
43437c478bd9Sstevel@tonic-gate 
43447c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
43457c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
43467c478bd9Sstevel@tonic-gate 	}
43477c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_string_array(MDI_PI(pip)->pi_prop, name, data,
43487c478bd9Sstevel@tonic-gate 	    nelements);
43497c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
43507c478bd9Sstevel@tonic-gate }
43517c478bd9Sstevel@tonic-gate 
43527c478bd9Sstevel@tonic-gate /*
43537c478bd9Sstevel@tonic-gate  * mdi_prop_free():
43547c478bd9Sstevel@tonic-gate  * 		Symmetrical function to ddi_prop_free(). nvlist_lookup_xx()
43557c478bd9Sstevel@tonic-gate  *		functions return the pointer to actual property data and not a
43567c478bd9Sstevel@tonic-gate  *		copy of it.  So the data returned is valid as long as
43577c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is valid.
43587c478bd9Sstevel@tonic-gate  */
43597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
43607c478bd9Sstevel@tonic-gate int
43617c478bd9Sstevel@tonic-gate mdi_prop_free(void *data)
43627c478bd9Sstevel@tonic-gate {
43637c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
43647c478bd9Sstevel@tonic-gate }
43657c478bd9Sstevel@tonic-gate 
43667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
43677c478bd9Sstevel@tonic-gate static void
43687c478bd9Sstevel@tonic-gate i_mdi_report_path_state(mdi_client_t *ct, mdi_pathinfo_t *pip)
43697c478bd9Sstevel@tonic-gate {
43707c478bd9Sstevel@tonic-gate 	char		*phci_path, *ct_path;
43717c478bd9Sstevel@tonic-gate 	char		*ct_status;
43727c478bd9Sstevel@tonic-gate 	char		*status;
43737c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = ct->ct_dip;
43747c478bd9Sstevel@tonic-gate 	char		lb_buf[64];
43757c478bd9Sstevel@tonic-gate 
4376*144dfaa9Scth 	ASSERT(MDI_CLIENT_LOCKED(ct));
43777c478bd9Sstevel@tonic-gate 	if ((dip == NULL) || (ddi_get_instance(dip) == -1) ||
43787c478bd9Sstevel@tonic-gate 	    (MDI_CLIENT_IS_REPORT_DEV_NEEDED(ct) == 0)) {
43797c478bd9Sstevel@tonic-gate 		return;
43807c478bd9Sstevel@tonic-gate 	}
43817c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_OPTIMAL) {
43827c478bd9Sstevel@tonic-gate 		ct_status = "optimal";
43837c478bd9Sstevel@tonic-gate 	} else if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_DEGRADED) {
43847c478bd9Sstevel@tonic-gate 		ct_status = "degraded";
43857c478bd9Sstevel@tonic-gate 	} else if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_FAILED) {
43867c478bd9Sstevel@tonic-gate 		ct_status = "failed";
43877c478bd9Sstevel@tonic-gate 	} else {
43887c478bd9Sstevel@tonic-gate 		ct_status = "unknown";
43897c478bd9Sstevel@tonic-gate 	}
43907c478bd9Sstevel@tonic-gate 
43917c478bd9Sstevel@tonic-gate 	if (MDI_PI_IS_OFFLINE(pip)) {
43927c478bd9Sstevel@tonic-gate 		status = "offline";
43937c478bd9Sstevel@tonic-gate 	} else if (MDI_PI_IS_ONLINE(pip)) {
43947c478bd9Sstevel@tonic-gate 		status = "online";
43957c478bd9Sstevel@tonic-gate 	} else if (MDI_PI_IS_STANDBY(pip)) {
43967c478bd9Sstevel@tonic-gate 		status = "standby";
43977c478bd9Sstevel@tonic-gate 	} else if (MDI_PI_IS_FAULT(pip)) {
43987c478bd9Sstevel@tonic-gate 		status = "faulted";
43997c478bd9Sstevel@tonic-gate 	} else {
44007c478bd9Sstevel@tonic-gate 		status = "unknown";
44017c478bd9Sstevel@tonic-gate 	}
44027c478bd9Sstevel@tonic-gate 
44037c478bd9Sstevel@tonic-gate 	if (ct->ct_lb == LOAD_BALANCE_LBA) {
44047c478bd9Sstevel@tonic-gate 		(void) snprintf(lb_buf, sizeof (lb_buf),
44057c478bd9Sstevel@tonic-gate 		    "%s, region-size: %d", mdi_load_balance_lba,
44067c478bd9Sstevel@tonic-gate 			ct->ct_lb_args->region_size);
44077c478bd9Sstevel@tonic-gate 	} else if (ct->ct_lb == LOAD_BALANCE_NONE) {
44087c478bd9Sstevel@tonic-gate 		(void) snprintf(lb_buf, sizeof (lb_buf),
44097c478bd9Sstevel@tonic-gate 		    "%s", mdi_load_balance_none);
44107c478bd9Sstevel@tonic-gate 	} else {
44117c478bd9Sstevel@tonic-gate 		(void) snprintf(lb_buf, sizeof (lb_buf), "%s",
44127c478bd9Sstevel@tonic-gate 		    mdi_load_balance_rr);
44137c478bd9Sstevel@tonic-gate 	}
44147c478bd9Sstevel@tonic-gate 
44157c478bd9Sstevel@tonic-gate 	if (dip) {
44167c478bd9Sstevel@tonic-gate 		ct_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
44177c478bd9Sstevel@tonic-gate 		phci_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
44187c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s (%s%d) multipath status: %s, "
44197c478bd9Sstevel@tonic-gate 		    "path %s (%s%d) to target address: %s is %s"
44207c478bd9Sstevel@tonic-gate 		    " Load balancing: %s\n",
44217c478bd9Sstevel@tonic-gate 		    ddi_pathname(dip, ct_path), ddi_driver_name(dip),
44227c478bd9Sstevel@tonic-gate 		    ddi_get_instance(dip), ct_status,
44237c478bd9Sstevel@tonic-gate 		    ddi_pathname(MDI_PI(pip)->pi_phci->ph_dip, phci_path),
44247c478bd9Sstevel@tonic-gate 		    ddi_driver_name(MDI_PI(pip)->pi_phci->ph_dip),
44257c478bd9Sstevel@tonic-gate 		    ddi_get_instance(MDI_PI(pip)->pi_phci->ph_dip),
44267c478bd9Sstevel@tonic-gate 		    MDI_PI(pip)->pi_addr, status, lb_buf);
44277c478bd9Sstevel@tonic-gate 		kmem_free(phci_path, MAXPATHLEN);
44287c478bd9Sstevel@tonic-gate 		kmem_free(ct_path, MAXPATHLEN);
44297c478bd9Sstevel@tonic-gate 		MDI_CLIENT_CLEAR_REPORT_DEV_NEEDED(ct);
44307c478bd9Sstevel@tonic-gate 	}
44317c478bd9Sstevel@tonic-gate }
44327c478bd9Sstevel@tonic-gate 
44337c478bd9Sstevel@tonic-gate #ifdef	DEBUG
44347c478bd9Sstevel@tonic-gate /*
44357c478bd9Sstevel@tonic-gate  * i_mdi_log():
44367c478bd9Sstevel@tonic-gate  *		Utility function for error message management
44377c478bd9Sstevel@tonic-gate  *
44387c478bd9Sstevel@tonic-gate  */
4439*144dfaa9Scth /*PRINTFLIKE3*/
44407c478bd9Sstevel@tonic-gate static void
44417c478bd9Sstevel@tonic-gate i_mdi_log(int level, dev_info_t *dip, const char *fmt, ...)
44427c478bd9Sstevel@tonic-gate {
44437c478bd9Sstevel@tonic-gate 	char		name[MAXNAMELEN];
4444*144dfaa9Scth 	char		buf[MAXNAMELEN];
4445*144dfaa9Scth 	char		*bp;
44467c478bd9Sstevel@tonic-gate 	va_list		ap;
44477c478bd9Sstevel@tonic-gate 	int		log_only = 0;
44487c478bd9Sstevel@tonic-gate 	int		boot_only = 0;
44497c478bd9Sstevel@tonic-gate 	int		console_only = 0;
44507c478bd9Sstevel@tonic-gate 
44517c478bd9Sstevel@tonic-gate 	if (dip) {
44527c478bd9Sstevel@tonic-gate 		(void) snprintf(name, MAXNAMELEN, "%s%d: ",
44537c478bd9Sstevel@tonic-gate 		    ddi_node_name(dip), ddi_get_instance(dip));
44547c478bd9Sstevel@tonic-gate 	} else {
4455*144dfaa9Scth 		name[0] = 0;
44567c478bd9Sstevel@tonic-gate 	}
44577c478bd9Sstevel@tonic-gate 
44587c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
44597c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, MAXNAMELEN, fmt, ap);
44607c478bd9Sstevel@tonic-gate 	va_end(ap);
44617c478bd9Sstevel@tonic-gate 
44627c478bd9Sstevel@tonic-gate 	switch (buf[0]) {
44637c478bd9Sstevel@tonic-gate 	case '!':
4464*144dfaa9Scth 		bp = &buf[1];
44657c478bd9Sstevel@tonic-gate 		log_only = 1;
44667c478bd9Sstevel@tonic-gate 		break;
44677c478bd9Sstevel@tonic-gate 	case '?':
4468*144dfaa9Scth 		bp = &buf[1];
44697c478bd9Sstevel@tonic-gate 		boot_only = 1;
44707c478bd9Sstevel@tonic-gate 		break;
44717c478bd9Sstevel@tonic-gate 	case '^':
4472*144dfaa9Scth 		bp = &buf[1];
44737c478bd9Sstevel@tonic-gate 		console_only = 1;
44747c478bd9Sstevel@tonic-gate 		break;
4475*144dfaa9Scth 	default:
4476*144dfaa9Scth 		bp = buf;
4477*144dfaa9Scth 		break;
4478*144dfaa9Scth 	}
4479*144dfaa9Scth 	if (mdi_debug_logonly) {
4480*144dfaa9Scth 		log_only = 1;
4481*144dfaa9Scth 		boot_only = 0;
4482*144dfaa9Scth 		console_only = 0;
44837c478bd9Sstevel@tonic-gate 	}
44847c478bd9Sstevel@tonic-gate 
44857c478bd9Sstevel@tonic-gate 	switch (level) {
44867c478bd9Sstevel@tonic-gate 	case CE_NOTE:
44877c478bd9Sstevel@tonic-gate 		level = CE_CONT;
44887c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
44897c478bd9Sstevel@tonic-gate 	case CE_CONT:
44907c478bd9Sstevel@tonic-gate 	case CE_WARN:
44917c478bd9Sstevel@tonic-gate 	case CE_PANIC:
44927c478bd9Sstevel@tonic-gate 		if (boot_only) {
4493*144dfaa9Scth 			cmn_err(level, "?mdi: %s%s", name, bp);
44947c478bd9Sstevel@tonic-gate 		} else if (console_only) {
4495*144dfaa9Scth 			cmn_err(level, "^mdi: %s%s", name, bp);
44967c478bd9Sstevel@tonic-gate 		} else if (log_only) {
4497*144dfaa9Scth 			cmn_err(level, "!mdi: %s%s", name, bp);
44987c478bd9Sstevel@tonic-gate 		} else {
4499*144dfaa9Scth 			cmn_err(level, "mdi: %s%s", name, bp);
45007c478bd9Sstevel@tonic-gate 		}
45017c478bd9Sstevel@tonic-gate 		break;
45027c478bd9Sstevel@tonic-gate 	default:
4503*144dfaa9Scth 		cmn_err(level, "mdi: %s%s", name, bp);
45047c478bd9Sstevel@tonic-gate 		break;
45057c478bd9Sstevel@tonic-gate 	}
45067c478bd9Sstevel@tonic-gate }
45077c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
45087c478bd9Sstevel@tonic-gate 
45097c478bd9Sstevel@tonic-gate void
45107c478bd9Sstevel@tonic-gate i_mdi_client_online(dev_info_t *ct_dip)
45117c478bd9Sstevel@tonic-gate {
45127c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
45137c478bd9Sstevel@tonic-gate 
45147c478bd9Sstevel@tonic-gate 	/*
45157c478bd9Sstevel@tonic-gate 	 * Client online notification. Mark client state as online
45167c478bd9Sstevel@tonic-gate 	 * restore our binding with dev_info node
45177c478bd9Sstevel@tonic-gate 	 */
45187c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(ct_dip);
45197c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
45207c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
45217c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_ONLINE(ct);
45227c478bd9Sstevel@tonic-gate 	/* catch for any memory leaks */
45237c478bd9Sstevel@tonic-gate 	ASSERT((ct->ct_dip == NULL) || (ct->ct_dip == ct_dip));
45247c478bd9Sstevel@tonic-gate 	ct->ct_dip = ct_dip;
45257c478bd9Sstevel@tonic-gate 
45267c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0)
45277c478bd9Sstevel@tonic-gate 		(void) i_mdi_power_all_phci(ct);
45287c478bd9Sstevel@tonic-gate 
45297c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, ct_dip, "i_mdi_client_online "
4530*144dfaa9Scth 	    "i_mdi_pm_hold_client %p\n", (void *)ct));
45317c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_client(ct, 1);
45327c478bd9Sstevel@tonic-gate 
45337c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
45347c478bd9Sstevel@tonic-gate }
45357c478bd9Sstevel@tonic-gate 
45367c478bd9Sstevel@tonic-gate void
45377c478bd9Sstevel@tonic-gate i_mdi_phci_online(dev_info_t *ph_dip)
45387c478bd9Sstevel@tonic-gate {
45397c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
45407c478bd9Sstevel@tonic-gate 
45417c478bd9Sstevel@tonic-gate 	/* pHCI online notification. Mark state accordingly */
45427c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(ph_dip);
45437c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
45447c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
45457c478bd9Sstevel@tonic-gate 	MDI_PHCI_SET_ONLINE(ph);
45467c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
45477c478bd9Sstevel@tonic-gate }
45487c478bd9Sstevel@tonic-gate 
45497c478bd9Sstevel@tonic-gate /*
45507c478bd9Sstevel@tonic-gate  * mdi_devi_online():
45517c478bd9Sstevel@tonic-gate  * 		Online notification from NDI framework on pHCI/client
45527c478bd9Sstevel@tonic-gate  *		device online.
45537c478bd9Sstevel@tonic-gate  * Return Values:
45547c478bd9Sstevel@tonic-gate  *		NDI_SUCCESS
45557c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
45567c478bd9Sstevel@tonic-gate  */
45577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
45587c478bd9Sstevel@tonic-gate int
45597c478bd9Sstevel@tonic-gate mdi_devi_online(dev_info_t *dip, uint_t flags)
45607c478bd9Sstevel@tonic-gate {
45617c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
45627c478bd9Sstevel@tonic-gate 		i_mdi_phci_online(dip);
45637c478bd9Sstevel@tonic-gate 	}
45647c478bd9Sstevel@tonic-gate 
45657c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
45667c478bd9Sstevel@tonic-gate 		i_mdi_client_online(dip);
45677c478bd9Sstevel@tonic-gate 	}
45687c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
45697c478bd9Sstevel@tonic-gate }
45707c478bd9Sstevel@tonic-gate 
45717c478bd9Sstevel@tonic-gate /*
45727c478bd9Sstevel@tonic-gate  * mdi_devi_offline():
45737c478bd9Sstevel@tonic-gate  * 		Offline notification from NDI framework on pHCI/Client device
45747c478bd9Sstevel@tonic-gate  *		offline.
45757c478bd9Sstevel@tonic-gate  *
45767c478bd9Sstevel@tonic-gate  * Return Values:
45777c478bd9Sstevel@tonic-gate  *		NDI_SUCCESS
45787c478bd9Sstevel@tonic-gate  *		NDI_FAILURE
45797c478bd9Sstevel@tonic-gate  */
45807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
45817c478bd9Sstevel@tonic-gate int
45827c478bd9Sstevel@tonic-gate mdi_devi_offline(dev_info_t *dip, uint_t flags)
45837c478bd9Sstevel@tonic-gate {
45847c478bd9Sstevel@tonic-gate 	int		rv = NDI_SUCCESS;
45857c478bd9Sstevel@tonic-gate 
45867c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
45877c478bd9Sstevel@tonic-gate 		rv = i_mdi_client_offline(dip, flags);
45887c478bd9Sstevel@tonic-gate 		if (rv != NDI_SUCCESS)
45897c478bd9Sstevel@tonic-gate 			return (rv);
45907c478bd9Sstevel@tonic-gate 	}
45917c478bd9Sstevel@tonic-gate 
45927c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
45937c478bd9Sstevel@tonic-gate 		rv = i_mdi_phci_offline(dip, flags);
4594*144dfaa9Scth 
45957c478bd9Sstevel@tonic-gate 		if ((rv != NDI_SUCCESS) && MDI_CLIENT(dip)) {
45967c478bd9Sstevel@tonic-gate 			/* set client back online */
45977c478bd9Sstevel@tonic-gate 			i_mdi_client_online(dip);
45987c478bd9Sstevel@tonic-gate 		}
45997c478bd9Sstevel@tonic-gate 	}
46007c478bd9Sstevel@tonic-gate 
46017c478bd9Sstevel@tonic-gate 	return (rv);
46027c478bd9Sstevel@tonic-gate }
46037c478bd9Sstevel@tonic-gate 
46047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
46057c478bd9Sstevel@tonic-gate static int
46067c478bd9Sstevel@tonic-gate i_mdi_phci_offline(dev_info_t *dip, uint_t flags)
46077c478bd9Sstevel@tonic-gate {
46087c478bd9Sstevel@tonic-gate 	int		rv = NDI_SUCCESS;
46097c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
46107c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
46117c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
46127c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
46137c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*failed_pip = NULL;
46147c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
46157c478bd9Sstevel@tonic-gate 
46167c478bd9Sstevel@tonic-gate 	/*
46177c478bd9Sstevel@tonic-gate 	 * pHCI component offline notification
46187c478bd9Sstevel@tonic-gate 	 * Make sure that this pHCI instance is free to be offlined.
46197c478bd9Sstevel@tonic-gate 	 * If it is OK to proceed, Offline and remove all the child
46207c478bd9Sstevel@tonic-gate 	 * mdi_pathinfo nodes.  This process automatically offlines
46217c478bd9Sstevel@tonic-gate 	 * corresponding client devices, for which this pHCI provides
46227c478bd9Sstevel@tonic-gate 	 * critical services.
46237c478bd9Sstevel@tonic-gate 	 */
46247c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(dip);
4625*144dfaa9Scth 	MDI_DEBUG(2, (CE_NOTE, dip, "!mdi_phci_offline called %p %p\n",
4626*144dfaa9Scth 	    (void *)dip, (void *)ph));
46277c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
46287c478bd9Sstevel@tonic-gate 		return (rv);
46297c478bd9Sstevel@tonic-gate 	}
46307c478bd9Sstevel@tonic-gate 
46317c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
46327c478bd9Sstevel@tonic-gate 
46337c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_OFFLINE(ph)) {
4634*144dfaa9Scth 		MDI_DEBUG(1, (CE_WARN, dip, "!pHCI %p already offlined",
4635*144dfaa9Scth 		    (void *)ph));
46367c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
46377c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
46387c478bd9Sstevel@tonic-gate 	}
46397c478bd9Sstevel@tonic-gate 
46407c478bd9Sstevel@tonic-gate 	/*
46417c478bd9Sstevel@tonic-gate 	 * Check to see if the pHCI can be offlined
46427c478bd9Sstevel@tonic-gate 	 */
46437c478bd9Sstevel@tonic-gate 	if (ph->ph_unstable) {
46447c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_WARN, dip,
46457c478bd9Sstevel@tonic-gate 		    "!One or more target devices are in transient "
46467c478bd9Sstevel@tonic-gate 		    "state. This device can not be removed at "
46477c478bd9Sstevel@tonic-gate 		    "this moment. Please try again later."));
46487c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
46497c478bd9Sstevel@tonic-gate 		return (NDI_BUSY);
46507c478bd9Sstevel@tonic-gate 	}
46517c478bd9Sstevel@tonic-gate 
46527c478bd9Sstevel@tonic-gate 	pip = ph->ph_path_head;
46537c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
46547c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
46557c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
4656*144dfaa9Scth 
46577c478bd9Sstevel@tonic-gate 		/*
46587c478bd9Sstevel@tonic-gate 		 * The mdi_pathinfo state is OK. Check the client state.
46597c478bd9Sstevel@tonic-gate 		 * If failover in progress fail the pHCI from offlining
46607c478bd9Sstevel@tonic-gate 		 */
46617c478bd9Sstevel@tonic-gate 		ct = MDI_PI(pip)->pi_client;
46627c478bd9Sstevel@tonic-gate 		i_mdi_client_lock(ct, pip);
46637c478bd9Sstevel@tonic-gate 		if ((MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) ||
46647c478bd9Sstevel@tonic-gate 		    (ct->ct_unstable)) {
46657c478bd9Sstevel@tonic-gate 			/*
46667c478bd9Sstevel@tonic-gate 			 * Failover is in progress, Fail the DR
46677c478bd9Sstevel@tonic-gate 			 */
46687c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_WARN, dip,
46697c478bd9Sstevel@tonic-gate 			    "!pHCI device (%s%d) is Busy. %s",
46707c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
46717c478bd9Sstevel@tonic-gate 			    "This device can not be removed at "
46727c478bd9Sstevel@tonic-gate 			    "this moment. Please try again later."));
46737c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
4674*144dfaa9Scth 			i_mdi_client_unlock(ct);
46757c478bd9Sstevel@tonic-gate 			MDI_PHCI_UNLOCK(ph);
46767c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
46777c478bd9Sstevel@tonic-gate 		}
46787c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
46797c478bd9Sstevel@tonic-gate 
46807c478bd9Sstevel@tonic-gate 		/*
46817c478bd9Sstevel@tonic-gate 		 * Check to see of we are removing the last path of this
46827c478bd9Sstevel@tonic-gate 		 * client device...
46837c478bd9Sstevel@tonic-gate 		 */
46847c478bd9Sstevel@tonic-gate 		cdip = ct->ct_dip;
46857c478bd9Sstevel@tonic-gate 		if (cdip && (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
46867c478bd9Sstevel@tonic-gate 		    (i_mdi_client_compute_state(ct, ph) ==
46877c478bd9Sstevel@tonic-gate 		    MDI_CLIENT_STATE_FAILED)) {
46887c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
46897c478bd9Sstevel@tonic-gate 			MDI_PHCI_UNLOCK(ph);
46907c478bd9Sstevel@tonic-gate 			if (ndi_devi_offline(cdip, 0) != NDI_SUCCESS) {
46917c478bd9Sstevel@tonic-gate 				/*
46927c478bd9Sstevel@tonic-gate 				 * ndi_devi_offline() failed.
46937c478bd9Sstevel@tonic-gate 				 * This pHCI provides the critical path
46947c478bd9Sstevel@tonic-gate 				 * to one or more client devices.
46957c478bd9Sstevel@tonic-gate 				 * Return busy.
46967c478bd9Sstevel@tonic-gate 				 */
46977c478bd9Sstevel@tonic-gate 				MDI_PHCI_LOCK(ph);
46987c478bd9Sstevel@tonic-gate 				MDI_DEBUG(1, (CE_WARN, dip,
46997c478bd9Sstevel@tonic-gate 				    "!pHCI device (%s%d) is Busy. %s",
47007c478bd9Sstevel@tonic-gate 				    ddi_driver_name(dip), ddi_get_instance(dip),
47017c478bd9Sstevel@tonic-gate 				    "This device can not be removed at "
47027c478bd9Sstevel@tonic-gate 				    "this moment. Please try again later."));
47037c478bd9Sstevel@tonic-gate 				failed_pip = pip;
47047c478bd9Sstevel@tonic-gate 				break;
47057c478bd9Sstevel@tonic-gate 			} else {
47067c478bd9Sstevel@tonic-gate 				MDI_PHCI_LOCK(ph);
47077c478bd9Sstevel@tonic-gate 				pip = next;
47087c478bd9Sstevel@tonic-gate 			}
47097c478bd9Sstevel@tonic-gate 		} else {
47107c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
47117c478bd9Sstevel@tonic-gate 			pip = next;
47127c478bd9Sstevel@tonic-gate 		}
47137c478bd9Sstevel@tonic-gate 	}
47147c478bd9Sstevel@tonic-gate 
47157c478bd9Sstevel@tonic-gate 	if (failed_pip) {
47167c478bd9Sstevel@tonic-gate 		pip = ph->ph_path_head;
47177c478bd9Sstevel@tonic-gate 		while (pip != failed_pip) {
47187c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
47197c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
47207c478bd9Sstevel@tonic-gate 			ct = MDI_PI(pip)->pi_client;
47217c478bd9Sstevel@tonic-gate 			i_mdi_client_lock(ct, pip);
47227c478bd9Sstevel@tonic-gate 			cdip = ct->ct_dip;
47237c478bd9Sstevel@tonic-gate 			switch (MDI_CLIENT_STATE(ct)) {
47247c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_OPTIMAL:
47257c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_DEGRADED:
47267c478bd9Sstevel@tonic-gate 				if (cdip) {
47277c478bd9Sstevel@tonic-gate 					MDI_PI_UNLOCK(pip);
47287c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
47297c478bd9Sstevel@tonic-gate 					MDI_PHCI_UNLOCK(ph);
47307c478bd9Sstevel@tonic-gate 					(void) ndi_devi_online(cdip, 0);
47317c478bd9Sstevel@tonic-gate 					MDI_PHCI_LOCK(ph);
47327c478bd9Sstevel@tonic-gate 					pip = next;
47337c478bd9Sstevel@tonic-gate 					continue;
47347c478bd9Sstevel@tonic-gate 				}
47357c478bd9Sstevel@tonic-gate 				break;
47367c478bd9Sstevel@tonic-gate 
47377c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_FAILED:
47387c478bd9Sstevel@tonic-gate 				if (cdip) {
47397c478bd9Sstevel@tonic-gate 					MDI_PI_UNLOCK(pip);
47407c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
47417c478bd9Sstevel@tonic-gate 					MDI_PHCI_UNLOCK(ph);
47427c478bd9Sstevel@tonic-gate 					(void) ndi_devi_offline(cdip, 0);
47437c478bd9Sstevel@tonic-gate 					MDI_PHCI_LOCK(ph);
47447c478bd9Sstevel@tonic-gate 					pip = next;
47457c478bd9Sstevel@tonic-gate 					continue;
47467c478bd9Sstevel@tonic-gate 				}
47477c478bd9Sstevel@tonic-gate 				break;
47487c478bd9Sstevel@tonic-gate 			}
47497c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
47507c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
47517c478bd9Sstevel@tonic-gate 			pip = next;
47527c478bd9Sstevel@tonic-gate 		}
47537c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
47547c478bd9Sstevel@tonic-gate 		return (NDI_BUSY);
47557c478bd9Sstevel@tonic-gate 	}
47567c478bd9Sstevel@tonic-gate 
47577c478bd9Sstevel@tonic-gate 	/*
47587c478bd9Sstevel@tonic-gate 	 * Mark the pHCI as offline
47597c478bd9Sstevel@tonic-gate 	 */
47607c478bd9Sstevel@tonic-gate 	MDI_PHCI_SET_OFFLINE(ph);
47617c478bd9Sstevel@tonic-gate 
47627c478bd9Sstevel@tonic-gate 	/*
47637c478bd9Sstevel@tonic-gate 	 * Mark the child mdi_pathinfo nodes as transient
47647c478bd9Sstevel@tonic-gate 	 */
47657c478bd9Sstevel@tonic-gate 	pip = ph->ph_path_head;
47667c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
47677c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
47687c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
47697c478bd9Sstevel@tonic-gate 		MDI_PI_SET_OFFLINING(pip);
47707c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
47717c478bd9Sstevel@tonic-gate 		pip = next;
47727c478bd9Sstevel@tonic-gate 	}
47737c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
47747c478bd9Sstevel@tonic-gate 	/*
47757c478bd9Sstevel@tonic-gate 	 * Give a chance for any pending commands to execute
47767c478bd9Sstevel@tonic-gate 	 */
47777c478bd9Sstevel@tonic-gate 	delay(1);
47787c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
47797c478bd9Sstevel@tonic-gate 	pip = ph->ph_path_head;
47807c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
47817c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
47827c478bd9Sstevel@tonic-gate 		(void) i_mdi_pi_offline(pip, flags);
47837c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
47847c478bd9Sstevel@tonic-gate 		ct = MDI_PI(pip)->pi_client;
47857c478bd9Sstevel@tonic-gate 		if (!MDI_PI_IS_OFFLINE(pip)) {
47867c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_WARN, dip,
47877c478bd9Sstevel@tonic-gate 			    "!pHCI device (%s%d) is Busy. %s",
47887c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
47897c478bd9Sstevel@tonic-gate 			    "This device can not be removed at "
47907c478bd9Sstevel@tonic-gate 			    "this moment. Please try again later."));
47917c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
47927c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_ONLINE(ph);
47937c478bd9Sstevel@tonic-gate 			MDI_PHCI_UNLOCK(ph);
47947c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
47957c478bd9Sstevel@tonic-gate 		}
47967c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
47977c478bd9Sstevel@tonic-gate 		pip = next;
47987c478bd9Sstevel@tonic-gate 	}
47997c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
48007c478bd9Sstevel@tonic-gate 
48017c478bd9Sstevel@tonic-gate 	return (rv);
48027c478bd9Sstevel@tonic-gate }
48037c478bd9Sstevel@tonic-gate 
48047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
48057c478bd9Sstevel@tonic-gate static int
48067c478bd9Sstevel@tonic-gate i_mdi_client_offline(dev_info_t *dip, uint_t flags)
48077c478bd9Sstevel@tonic-gate {
48087c478bd9Sstevel@tonic-gate 	int		rv = NDI_SUCCESS;
48097c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
48107c478bd9Sstevel@tonic-gate 
48117c478bd9Sstevel@tonic-gate 	/*
48127c478bd9Sstevel@tonic-gate 	 * Client component to go offline.  Make sure that we are
48137c478bd9Sstevel@tonic-gate 	 * not in failing over state and update client state
48147c478bd9Sstevel@tonic-gate 	 * accordingly
48157c478bd9Sstevel@tonic-gate 	 */
48167c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(dip);
4817*144dfaa9Scth 	MDI_DEBUG(2, (CE_NOTE, dip, "!i_mdi_client_offline called %p %p\n",
4818*144dfaa9Scth 	    (void *)dip, (void *)ct));
48197c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
48207c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
48217c478bd9Sstevel@tonic-gate 		if (ct->ct_unstable) {
48227c478bd9Sstevel@tonic-gate 			/*
48237c478bd9Sstevel@tonic-gate 			 * One or more paths are in transient state,
48247c478bd9Sstevel@tonic-gate 			 * Dont allow offline of a client device
48257c478bd9Sstevel@tonic-gate 			 */
48267c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_WARN, dip,
48277c478bd9Sstevel@tonic-gate 			    "!One or more paths to this device is "
48287c478bd9Sstevel@tonic-gate 			    "in transient state. This device can not "
48297c478bd9Sstevel@tonic-gate 			    "be removed at this moment. "
48307c478bd9Sstevel@tonic-gate 			    "Please try again later."));
48317c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
48327c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
48337c478bd9Sstevel@tonic-gate 		}
48347c478bd9Sstevel@tonic-gate 		if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
48357c478bd9Sstevel@tonic-gate 			/*
48367c478bd9Sstevel@tonic-gate 			 * Failover is in progress, Dont allow DR of
48377c478bd9Sstevel@tonic-gate 			 * a client device
48387c478bd9Sstevel@tonic-gate 			 */
48397c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_WARN, dip,
48407c478bd9Sstevel@tonic-gate 			    "!Client device (%s%d) is Busy. %s",
48417c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
48427c478bd9Sstevel@tonic-gate 			    "This device can not be removed at "
48437c478bd9Sstevel@tonic-gate 			    "this moment. Please try again later."));
48447c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
48457c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
48467c478bd9Sstevel@tonic-gate 		}
48477c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_OFFLINE(ct);
48487c478bd9Sstevel@tonic-gate 
48497c478bd9Sstevel@tonic-gate 		/*
48507c478bd9Sstevel@tonic-gate 		 * Unbind our relationship with the dev_info node
48517c478bd9Sstevel@tonic-gate 		 */
48527c478bd9Sstevel@tonic-gate 		if (flags & NDI_DEVI_REMOVE) {
48537c478bd9Sstevel@tonic-gate 			ct->ct_dip = NULL;
48547c478bd9Sstevel@tonic-gate 		}
48557c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
48567c478bd9Sstevel@tonic-gate 	}
48577c478bd9Sstevel@tonic-gate 	return (rv);
48587c478bd9Sstevel@tonic-gate }
48597c478bd9Sstevel@tonic-gate 
48607c478bd9Sstevel@tonic-gate /*
48617c478bd9Sstevel@tonic-gate  * mdi_pre_attach():
48627c478bd9Sstevel@tonic-gate  *		Pre attach() notification handler
48637c478bd9Sstevel@tonic-gate  */
48647c478bd9Sstevel@tonic-gate /*ARGSUSED*/
48657c478bd9Sstevel@tonic-gate int
48667c478bd9Sstevel@tonic-gate mdi_pre_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
48677c478bd9Sstevel@tonic-gate {
48687c478bd9Sstevel@tonic-gate 	/* don't support old DDI_PM_RESUME */
48697c478bd9Sstevel@tonic-gate 	if ((DEVI(dip)->devi_mdi_component != MDI_COMPONENT_NONE) &&
48707c478bd9Sstevel@tonic-gate 	    (cmd == DDI_PM_RESUME))
48717c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
48727c478bd9Sstevel@tonic-gate 
48737c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
48747c478bd9Sstevel@tonic-gate }
48757c478bd9Sstevel@tonic-gate 
48767c478bd9Sstevel@tonic-gate /*
48777c478bd9Sstevel@tonic-gate  * mdi_post_attach():
48787c478bd9Sstevel@tonic-gate  *		Post attach() notification handler
48797c478bd9Sstevel@tonic-gate  */
48807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
48817c478bd9Sstevel@tonic-gate void
48827c478bd9Sstevel@tonic-gate mdi_post_attach(dev_info_t *dip, ddi_attach_cmd_t cmd, int error)
48837c478bd9Sstevel@tonic-gate {
48847c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
48857c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
48867c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
48877c478bd9Sstevel@tonic-gate 
48887c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
48897c478bd9Sstevel@tonic-gate 		ph = i_devi_get_phci(dip);
48907c478bd9Sstevel@tonic-gate 		ASSERT(ph != NULL);
48917c478bd9Sstevel@tonic-gate 
48927c478bd9Sstevel@tonic-gate 		MDI_PHCI_LOCK(ph);
48937c478bd9Sstevel@tonic-gate 		switch (cmd) {
48947c478bd9Sstevel@tonic-gate 		case DDI_ATTACH:
48957c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_NOTE, dip,
4896*144dfaa9Scth 			    "!pHCI post_attach: called %p\n", (void *)ph));
48977c478bd9Sstevel@tonic-gate 			if (error == DDI_SUCCESS) {
48987c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_ATTACH(ph);
48997c478bd9Sstevel@tonic-gate 			} else {
49007c478bd9Sstevel@tonic-gate 				MDI_DEBUG(1, (CE_NOTE, dip,
49017c478bd9Sstevel@tonic-gate 				    "!pHCI post_attach: failed error=%d\n",
49027c478bd9Sstevel@tonic-gate 				    error));
49037c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_DETACH(ph);
49047c478bd9Sstevel@tonic-gate 			}
49057c478bd9Sstevel@tonic-gate 			break;
49067c478bd9Sstevel@tonic-gate 
49077c478bd9Sstevel@tonic-gate 		case DDI_RESUME:
49087c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_NOTE, dip,
4909*144dfaa9Scth 			    "!pHCI post_resume: called %p\n", (void *)ph));
49107c478bd9Sstevel@tonic-gate 			if (error == DDI_SUCCESS) {
49117c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_RESUME(ph);
49127c478bd9Sstevel@tonic-gate 			} else {
49137c478bd9Sstevel@tonic-gate 				MDI_DEBUG(1, (CE_NOTE, dip,
49147c478bd9Sstevel@tonic-gate 				    "!pHCI post_resume: failed error=%d\n",
49157c478bd9Sstevel@tonic-gate 				    error));
49167c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_SUSPEND(ph);
49177c478bd9Sstevel@tonic-gate 			}
49187c478bd9Sstevel@tonic-gate 			break;
49197c478bd9Sstevel@tonic-gate 		}
49207c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
49217c478bd9Sstevel@tonic-gate 	}
49227c478bd9Sstevel@tonic-gate 
49237c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
49247c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(dip);
49257c478bd9Sstevel@tonic-gate 		ASSERT(ct != NULL);
49267c478bd9Sstevel@tonic-gate 
49277c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
49287c478bd9Sstevel@tonic-gate 		switch (cmd) {
49297c478bd9Sstevel@tonic-gate 		case DDI_ATTACH:
49307c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_NOTE, dip,
4931*144dfaa9Scth 			    "!Client post_attach: called %p\n", (void *)ct));
49327c478bd9Sstevel@tonic-gate 			if (error != DDI_SUCCESS) {
49337c478bd9Sstevel@tonic-gate 				MDI_DEBUG(1, (CE_NOTE, dip,
49347c478bd9Sstevel@tonic-gate 				    "!Client post_attach: failed error=%d\n",
49357c478bd9Sstevel@tonic-gate 				    error));
49367c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_DETACH(ct);
49377c478bd9Sstevel@tonic-gate 				MDI_DEBUG(4, (CE_WARN, dip,
49387c478bd9Sstevel@tonic-gate 				    "mdi_post_attach i_mdi_pm_reset_client\n"));
49397c478bd9Sstevel@tonic-gate 				i_mdi_pm_reset_client(ct);
49407c478bd9Sstevel@tonic-gate 				break;
49417c478bd9Sstevel@tonic-gate 			}
49427c478bd9Sstevel@tonic-gate 
49437c478bd9Sstevel@tonic-gate 			/*
49447c478bd9Sstevel@tonic-gate 			 * Client device has successfully attached.
49457c478bd9Sstevel@tonic-gate 			 * Create kstats for any pathinfo structures
49467c478bd9Sstevel@tonic-gate 			 * initially associated with this client.
49477c478bd9Sstevel@tonic-gate 			 */
49487c478bd9Sstevel@tonic-gate 			for (pip = ct->ct_path_head; pip != NULL;
49497c478bd9Sstevel@tonic-gate 			    pip = (mdi_pathinfo_t *)
49507c478bd9Sstevel@tonic-gate 			    MDI_PI(pip)->pi_client_link) {
4951*144dfaa9Scth 				if (!MDI_PI_IS_OFFLINE(pip)) {
49527c478bd9Sstevel@tonic-gate 					(void) i_mdi_pi_kstat_create(pip);
49537c478bd9Sstevel@tonic-gate 					i_mdi_report_path_state(ct, pip);
49547c478bd9Sstevel@tonic-gate 				}
4955*144dfaa9Scth 			}
49567c478bd9Sstevel@tonic-gate 			MDI_CLIENT_SET_ATTACH(ct);
49577c478bd9Sstevel@tonic-gate 			break;
49587c478bd9Sstevel@tonic-gate 
49597c478bd9Sstevel@tonic-gate 		case DDI_RESUME:
49607c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_NOTE, dip,
4961*144dfaa9Scth 			    "!Client post_attach: called %p\n", (void *)ct));
49627c478bd9Sstevel@tonic-gate 			if (error == DDI_SUCCESS) {
49637c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_RESUME(ct);
49647c478bd9Sstevel@tonic-gate 			} else {
49657c478bd9Sstevel@tonic-gate 				MDI_DEBUG(1, (CE_NOTE, dip,
49667c478bd9Sstevel@tonic-gate 				    "!Client post_resume: failed error=%d\n",
49677c478bd9Sstevel@tonic-gate 				    error));
49687c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_SUSPEND(ct);
49697c478bd9Sstevel@tonic-gate 			}
49707c478bd9Sstevel@tonic-gate 			break;
49717c478bd9Sstevel@tonic-gate 		}
49727c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
49737c478bd9Sstevel@tonic-gate 	}
49747c478bd9Sstevel@tonic-gate }
49757c478bd9Sstevel@tonic-gate 
49767c478bd9Sstevel@tonic-gate /*
49777c478bd9Sstevel@tonic-gate  * mdi_pre_detach():
49787c478bd9Sstevel@tonic-gate  *		Pre detach notification handler
49797c478bd9Sstevel@tonic-gate  */
49807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
49817c478bd9Sstevel@tonic-gate int
49827c478bd9Sstevel@tonic-gate mdi_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
49837c478bd9Sstevel@tonic-gate {
49847c478bd9Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
49857c478bd9Sstevel@tonic-gate 
49867c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
49877c478bd9Sstevel@tonic-gate 		(void) i_mdi_client_pre_detach(dip, cmd);
49887c478bd9Sstevel@tonic-gate 	}
49897c478bd9Sstevel@tonic-gate 
49907c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
49917c478bd9Sstevel@tonic-gate 		rv = i_mdi_phci_pre_detach(dip, cmd);
49927c478bd9Sstevel@tonic-gate 	}
49937c478bd9Sstevel@tonic-gate 
49947c478bd9Sstevel@tonic-gate 	return (rv);
49957c478bd9Sstevel@tonic-gate }
49967c478bd9Sstevel@tonic-gate 
49977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
49987c478bd9Sstevel@tonic-gate static int
49997c478bd9Sstevel@tonic-gate i_mdi_phci_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
50007c478bd9Sstevel@tonic-gate {
50017c478bd9Sstevel@tonic-gate 	int		rv = DDI_SUCCESS;
50027c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
50037c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
50047c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
50057c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*failed_pip = NULL;
50067c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
50077c478bd9Sstevel@tonic-gate 
50087c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(dip);
50097c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
50107c478bd9Sstevel@tonic-gate 		return (rv);
50117c478bd9Sstevel@tonic-gate 	}
50127c478bd9Sstevel@tonic-gate 
50137c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
50147c478bd9Sstevel@tonic-gate 	switch (cmd) {
50157c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
50167c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
5017*144dfaa9Scth 		    "!pHCI pre_detach: called %p\n", (void *)ph));
50187c478bd9Sstevel@tonic-gate 		if (!MDI_PHCI_IS_OFFLINE(ph)) {
50197c478bd9Sstevel@tonic-gate 			/*
50207c478bd9Sstevel@tonic-gate 			 * mdi_pathinfo nodes are still attached to
50217c478bd9Sstevel@tonic-gate 			 * this pHCI. Fail the detach for this pHCI.
50227c478bd9Sstevel@tonic-gate 			 */
50237c478bd9Sstevel@tonic-gate 			MDI_DEBUG(2, (CE_WARN, dip,
50247c478bd9Sstevel@tonic-gate 			    "!pHCI pre_detach: "
50257c478bd9Sstevel@tonic-gate 			    "mdi_pathinfo nodes are still attached "
5026*144dfaa9Scth 			    "%p\n", (void *)ph));
50277c478bd9Sstevel@tonic-gate 			rv = DDI_FAILURE;
50287c478bd9Sstevel@tonic-gate 			break;
50297c478bd9Sstevel@tonic-gate 		}
50307c478bd9Sstevel@tonic-gate 		MDI_PHCI_SET_DETACH(ph);
50317c478bd9Sstevel@tonic-gate 		break;
50327c478bd9Sstevel@tonic-gate 
50337c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
50347c478bd9Sstevel@tonic-gate 		/*
50357c478bd9Sstevel@tonic-gate 		 * pHCI is getting suspended.  Since mpxio client
50367c478bd9Sstevel@tonic-gate 		 * devices may not be suspended at this point, to avoid
50377c478bd9Sstevel@tonic-gate 		 * a potential stack overflow, it is important to suspend
50387c478bd9Sstevel@tonic-gate 		 * client devices before pHCI can be suspended.
50397c478bd9Sstevel@tonic-gate 		 */
50407c478bd9Sstevel@tonic-gate 
50417c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
5042*144dfaa9Scth 		    "!pHCI pre_suspend: called %p\n", (void *)ph));
50437c478bd9Sstevel@tonic-gate 		/*
50447c478bd9Sstevel@tonic-gate 		 * Suspend all the client devices accessible through this pHCI
50457c478bd9Sstevel@tonic-gate 		 */
50467c478bd9Sstevel@tonic-gate 		pip = ph->ph_path_head;
50477c478bd9Sstevel@tonic-gate 		while (pip != NULL && rv == DDI_SUCCESS) {
50487c478bd9Sstevel@tonic-gate 			dev_info_t *cdip;
50497c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
50507c478bd9Sstevel@tonic-gate 			next =
50517c478bd9Sstevel@tonic-gate 			    (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
50527c478bd9Sstevel@tonic-gate 			ct = MDI_PI(pip)->pi_client;
50537c478bd9Sstevel@tonic-gate 			i_mdi_client_lock(ct, pip);
50547c478bd9Sstevel@tonic-gate 			cdip = ct->ct_dip;
50557c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
50567c478bd9Sstevel@tonic-gate 			if ((MDI_CLIENT_IS_DETACHED(ct) == 0) &&
50577c478bd9Sstevel@tonic-gate 			    MDI_CLIENT_IS_SUSPENDED(ct) == 0) {
50587c478bd9Sstevel@tonic-gate 				i_mdi_client_unlock(ct);
50597c478bd9Sstevel@tonic-gate 				if ((rv = devi_detach(cdip, DDI_SUSPEND)) !=
50607c478bd9Sstevel@tonic-gate 				    DDI_SUCCESS) {
50617c478bd9Sstevel@tonic-gate 					/*
50627c478bd9Sstevel@tonic-gate 					 * Suspend of one of the client
50637c478bd9Sstevel@tonic-gate 					 * device has failed.
50647c478bd9Sstevel@tonic-gate 					 */
50657c478bd9Sstevel@tonic-gate 					MDI_DEBUG(1, (CE_WARN, dip,
50667c478bd9Sstevel@tonic-gate 					    "!Suspend of device (%s%d) failed.",
50677c478bd9Sstevel@tonic-gate 					    ddi_driver_name(cdip),
50687c478bd9Sstevel@tonic-gate 					    ddi_get_instance(cdip)));
50697c478bd9Sstevel@tonic-gate 					failed_pip = pip;
50707c478bd9Sstevel@tonic-gate 					break;
50717c478bd9Sstevel@tonic-gate 				}
50727c478bd9Sstevel@tonic-gate 			} else {
50737c478bd9Sstevel@tonic-gate 				i_mdi_client_unlock(ct);
50747c478bd9Sstevel@tonic-gate 			}
50757c478bd9Sstevel@tonic-gate 			pip = next;
50767c478bd9Sstevel@tonic-gate 		}
50777c478bd9Sstevel@tonic-gate 
50787c478bd9Sstevel@tonic-gate 		if (rv == DDI_SUCCESS) {
50797c478bd9Sstevel@tonic-gate 			/*
50807c478bd9Sstevel@tonic-gate 			 * Suspend of client devices is complete. Proceed
50817c478bd9Sstevel@tonic-gate 			 * with pHCI suspend.
50827c478bd9Sstevel@tonic-gate 			 */
50837c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_SUSPEND(ph);
50847c478bd9Sstevel@tonic-gate 		} else {
50857c478bd9Sstevel@tonic-gate 			/*
50867c478bd9Sstevel@tonic-gate 			 * Revert back all the suspended client device states
50877c478bd9Sstevel@tonic-gate 			 * to converse.
50887c478bd9Sstevel@tonic-gate 			 */
50897c478bd9Sstevel@tonic-gate 			pip = ph->ph_path_head;
50907c478bd9Sstevel@tonic-gate 			while (pip != failed_pip) {
50917c478bd9Sstevel@tonic-gate 				dev_info_t *cdip;
50927c478bd9Sstevel@tonic-gate 				MDI_PI_LOCK(pip);
50937c478bd9Sstevel@tonic-gate 				next =
50947c478bd9Sstevel@tonic-gate 				    (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
50957c478bd9Sstevel@tonic-gate 				ct = MDI_PI(pip)->pi_client;
50967c478bd9Sstevel@tonic-gate 				i_mdi_client_lock(ct, pip);
50977c478bd9Sstevel@tonic-gate 				cdip = ct->ct_dip;
50987c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
50997c478bd9Sstevel@tonic-gate 				if (MDI_CLIENT_IS_SUSPENDED(ct)) {
51007c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
51017c478bd9Sstevel@tonic-gate 					(void) devi_attach(cdip, DDI_RESUME);
51027c478bd9Sstevel@tonic-gate 				} else {
51037c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
51047c478bd9Sstevel@tonic-gate 				}
51057c478bd9Sstevel@tonic-gate 				pip = next;
51067c478bd9Sstevel@tonic-gate 			}
51077c478bd9Sstevel@tonic-gate 		}
51087c478bd9Sstevel@tonic-gate 		break;
51097c478bd9Sstevel@tonic-gate 
51107c478bd9Sstevel@tonic-gate 	default:
51117c478bd9Sstevel@tonic-gate 		rv = DDI_FAILURE;
51127c478bd9Sstevel@tonic-gate 		break;
51137c478bd9Sstevel@tonic-gate 	}
51147c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
51157c478bd9Sstevel@tonic-gate 	return (rv);
51167c478bd9Sstevel@tonic-gate }
51177c478bd9Sstevel@tonic-gate 
51187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
51197c478bd9Sstevel@tonic-gate static int
51207c478bd9Sstevel@tonic-gate i_mdi_client_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
51217c478bd9Sstevel@tonic-gate {
51227c478bd9Sstevel@tonic-gate 	int		rv = DDI_SUCCESS;
51237c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
51247c478bd9Sstevel@tonic-gate 
51257c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(dip);
51267c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
51277c478bd9Sstevel@tonic-gate 		return (rv);
51287c478bd9Sstevel@tonic-gate 	}
51297c478bd9Sstevel@tonic-gate 
51307c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
51317c478bd9Sstevel@tonic-gate 	switch (cmd) {
51327c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
51337c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
5134*144dfaa9Scth 		    "!Client pre_detach: called %p\n", (void *)ct));
51357c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_DETACH(ct);
51367c478bd9Sstevel@tonic-gate 		break;
51377c478bd9Sstevel@tonic-gate 
51387c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
51397c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
5140*144dfaa9Scth 		    "!Client pre_suspend: called %p\n", (void *)ct));
51417c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_SUSPEND(ct);
51427c478bd9Sstevel@tonic-gate 		break;
51437c478bd9Sstevel@tonic-gate 
51447c478bd9Sstevel@tonic-gate 	default:
51457c478bd9Sstevel@tonic-gate 		rv = DDI_FAILURE;
51467c478bd9Sstevel@tonic-gate 		break;
51477c478bd9Sstevel@tonic-gate 	}
51487c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
51497c478bd9Sstevel@tonic-gate 	return (rv);
51507c478bd9Sstevel@tonic-gate }
51517c478bd9Sstevel@tonic-gate 
51527c478bd9Sstevel@tonic-gate /*
51537c478bd9Sstevel@tonic-gate  * mdi_post_detach():
51547c478bd9Sstevel@tonic-gate  *		Post detach notification handler
51557c478bd9Sstevel@tonic-gate  */
51567c478bd9Sstevel@tonic-gate /*ARGSUSED*/
51577c478bd9Sstevel@tonic-gate void
51587c478bd9Sstevel@tonic-gate mdi_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
51597c478bd9Sstevel@tonic-gate {
51607c478bd9Sstevel@tonic-gate 	/*
51617c478bd9Sstevel@tonic-gate 	 * Detach/Suspend of mpxio component failed. Update our state
51627c478bd9Sstevel@tonic-gate 	 * too
51637c478bd9Sstevel@tonic-gate 	 */
51647c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip))
51657c478bd9Sstevel@tonic-gate 		i_mdi_phci_post_detach(dip, cmd, error);
51667c478bd9Sstevel@tonic-gate 
51677c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip))
51687c478bd9Sstevel@tonic-gate 		i_mdi_client_post_detach(dip, cmd, error);
51697c478bd9Sstevel@tonic-gate }
51707c478bd9Sstevel@tonic-gate 
51717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
51727c478bd9Sstevel@tonic-gate static void
51737c478bd9Sstevel@tonic-gate i_mdi_phci_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
51747c478bd9Sstevel@tonic-gate {
51757c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
51767c478bd9Sstevel@tonic-gate 
51777c478bd9Sstevel@tonic-gate 	/*
51787c478bd9Sstevel@tonic-gate 	 * Detach/Suspend of phci component failed. Update our state
51797c478bd9Sstevel@tonic-gate 	 * too
51807c478bd9Sstevel@tonic-gate 	 */
51817c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(dip);
51827c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
51837c478bd9Sstevel@tonic-gate 		return;
51847c478bd9Sstevel@tonic-gate 	}
51857c478bd9Sstevel@tonic-gate 
51867c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
51877c478bd9Sstevel@tonic-gate 	/*
51887c478bd9Sstevel@tonic-gate 	 * Detach of pHCI failed. Restore back converse
51897c478bd9Sstevel@tonic-gate 	 * state
51907c478bd9Sstevel@tonic-gate 	 */
51917c478bd9Sstevel@tonic-gate 	switch (cmd) {
51927c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
51937c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
5194*144dfaa9Scth 		    "!pHCI post_detach: called %p\n", (void *)ph));
51957c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
51967c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_ATTACH(ph);
51977c478bd9Sstevel@tonic-gate 		break;
51987c478bd9Sstevel@tonic-gate 
51997c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
52007c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
5201*144dfaa9Scth 		    "!pHCI post_suspend: called %p\n", (void *)ph));
52027c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
52037c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_RESUME(ph);
52047c478bd9Sstevel@tonic-gate 		break;
52057c478bd9Sstevel@tonic-gate 	}
52067c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
52077c478bd9Sstevel@tonic-gate }
52087c478bd9Sstevel@tonic-gate 
52097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
52107c478bd9Sstevel@tonic-gate static void
52117c478bd9Sstevel@tonic-gate i_mdi_client_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
52127c478bd9Sstevel@tonic-gate {
52137c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
52147c478bd9Sstevel@tonic-gate 
52157c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(dip);
52167c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
52177c478bd9Sstevel@tonic-gate 		return;
52187c478bd9Sstevel@tonic-gate 	}
52197c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
52207c478bd9Sstevel@tonic-gate 	/*
52217c478bd9Sstevel@tonic-gate 	 * Detach of Client failed. Restore back converse
52227c478bd9Sstevel@tonic-gate 	 * state
52237c478bd9Sstevel@tonic-gate 	 */
52247c478bd9Sstevel@tonic-gate 	switch (cmd) {
52257c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
52267c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
5227*144dfaa9Scth 		    "!Client post_detach: called %p\n", (void *)ct));
52287c478bd9Sstevel@tonic-gate 		if (DEVI_IS_ATTACHING(ct->ct_dip)) {
52297c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, dip, "i_mdi_client_post_detach "
52307c478bd9Sstevel@tonic-gate 			    "i_mdi_pm_rele_client\n"));
52317c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, ct->ct_path_count);
52327c478bd9Sstevel@tonic-gate 		} else {
52337c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, dip, "i_mdi_client_post_detach "
52347c478bd9Sstevel@tonic-gate 			    "i_mdi_pm_reset_client\n"));
52357c478bd9Sstevel@tonic-gate 			i_mdi_pm_reset_client(ct);
52367c478bd9Sstevel@tonic-gate 		}
52377c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
52387c478bd9Sstevel@tonic-gate 			MDI_CLIENT_SET_ATTACH(ct);
52397c478bd9Sstevel@tonic-gate 		break;
52407c478bd9Sstevel@tonic-gate 
52417c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
52427c478bd9Sstevel@tonic-gate 		MDI_DEBUG(2, (CE_NOTE, dip,
5243*144dfaa9Scth 		    "!Client post_suspend: called %p\n", (void *)ct));
52447c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
52457c478bd9Sstevel@tonic-gate 			MDI_CLIENT_SET_RESUME(ct);
52467c478bd9Sstevel@tonic-gate 		break;
52477c478bd9Sstevel@tonic-gate 	}
52487c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
52497c478bd9Sstevel@tonic-gate }
52507c478bd9Sstevel@tonic-gate 
52517c478bd9Sstevel@tonic-gate /*
52527c478bd9Sstevel@tonic-gate  * create and install per-path (client - pHCI) statistics
52537c478bd9Sstevel@tonic-gate  * I/O stats supported: nread, nwritten, reads, and writes
52547c478bd9Sstevel@tonic-gate  * Error stats - hard errors, soft errors, & transport errors
52557c478bd9Sstevel@tonic-gate  */
52567c478bd9Sstevel@tonic-gate static int
52577c478bd9Sstevel@tonic-gate i_mdi_pi_kstat_create(mdi_pathinfo_t *pip)
52587c478bd9Sstevel@tonic-gate {
52597c478bd9Sstevel@tonic-gate 
52607c478bd9Sstevel@tonic-gate 	dev_info_t *client = MDI_PI(pip)->pi_client->ct_dip;
52617c478bd9Sstevel@tonic-gate 	dev_info_t *ppath = MDI_PI(pip)->pi_phci->ph_dip;
52627c478bd9Sstevel@tonic-gate 	char ksname[KSTAT_STRLEN];
52637c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t *cpip;
52647c478bd9Sstevel@tonic-gate 	const char *err_postfix = ",err";
52657c478bd9Sstevel@tonic-gate 	kstat_t	*kiosp, *kerrsp;
52667c478bd9Sstevel@tonic-gate 	struct pi_errs	*nsp;
52677c478bd9Sstevel@tonic-gate 	struct mdi_pi_kstats *mdi_statp;
52687c478bd9Sstevel@tonic-gate 
52697c478bd9Sstevel@tonic-gate 	ASSERT(client != NULL && ppath != NULL);
52707c478bd9Sstevel@tonic-gate 
5271*144dfaa9Scth 	ASSERT(MDI_CLIENT_LOCKED(MDI_PI(pip)->pi_client));
52727c478bd9Sstevel@tonic-gate 
52737c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_kstats != NULL)
52747c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
52757c478bd9Sstevel@tonic-gate 
52767c478bd9Sstevel@tonic-gate 	for (cpip = MDI_PI(pip)->pi_client->ct_path_head; cpip != NULL;
52777c478bd9Sstevel@tonic-gate 	    cpip = (mdi_pathinfo_t *)(MDI_PI(cpip)->pi_client_link)) {
5278*144dfaa9Scth 		if ((cpip == pip) || MDI_PI_IS_OFFLINE(pip))
52797c478bd9Sstevel@tonic-gate 			continue;
52807c478bd9Sstevel@tonic-gate 		/*
52817c478bd9Sstevel@tonic-gate 		 * We have found a different path with same parent
52827c478bd9Sstevel@tonic-gate 		 * kstats for a given client-pHCI are common
52837c478bd9Sstevel@tonic-gate 		 */
52847c478bd9Sstevel@tonic-gate 		if ((MDI_PI(cpip)->pi_phci->ph_dip == ppath) &&
52857c478bd9Sstevel@tonic-gate 		    (MDI_PI(cpip)->pi_kstats != NULL)) {
52867c478bd9Sstevel@tonic-gate 			MDI_PI(cpip)->pi_kstats->pi_kstat_ref++;
52877c478bd9Sstevel@tonic-gate 			MDI_PI(pip)->pi_kstats = MDI_PI(cpip)->pi_kstats;
52887c478bd9Sstevel@tonic-gate 			return (MDI_SUCCESS);
52897c478bd9Sstevel@tonic-gate 		}
52907c478bd9Sstevel@tonic-gate 	}
52917c478bd9Sstevel@tonic-gate 
52927c478bd9Sstevel@tonic-gate 	/*
52937c478bd9Sstevel@tonic-gate 	 * stats are named as follows: TGTx.HBAy, e.g. "ssd0.fp0"
52947c478bd9Sstevel@tonic-gate 	 * clamp length of name against max length of error kstat name
52957c478bd9Sstevel@tonic-gate 	 */
52967c478bd9Sstevel@tonic-gate 	if (snprintf(ksname, KSTAT_STRLEN, "%s%d.%s%d",
52977c478bd9Sstevel@tonic-gate 	    ddi_driver_name(client), ddi_get_instance(client),
52987c478bd9Sstevel@tonic-gate 	    ddi_driver_name(ppath), ddi_get_instance(ppath)) >
52997c478bd9Sstevel@tonic-gate 	    (KSTAT_STRLEN - strlen(err_postfix))) {
53007c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
53017c478bd9Sstevel@tonic-gate 	}
53027c478bd9Sstevel@tonic-gate 	if ((kiosp = kstat_create("mdi", 0, ksname, "iopath",
53037c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_IO, 1, 0)) == NULL) {
53047c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
53057c478bd9Sstevel@tonic-gate 	}
53067c478bd9Sstevel@tonic-gate 
53077c478bd9Sstevel@tonic-gate 	(void) strcat(ksname, err_postfix);
53087c478bd9Sstevel@tonic-gate 	kerrsp = kstat_create("mdi", 0, ksname, "iopath_errors",
53097c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED,
53107c478bd9Sstevel@tonic-gate 	    sizeof (struct pi_errs) / sizeof (kstat_named_t), 0);
53117c478bd9Sstevel@tonic-gate 
53127c478bd9Sstevel@tonic-gate 	if (kerrsp == NULL) {
53137c478bd9Sstevel@tonic-gate 		kstat_delete(kiosp);
53147c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
53157c478bd9Sstevel@tonic-gate 	}
53167c478bd9Sstevel@tonic-gate 
53177c478bd9Sstevel@tonic-gate 	nsp = (struct pi_errs *)kerrsp->ks_data;
53187c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_softerrs, "Soft Errors", KSTAT_DATA_UINT32);
53197c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_harderrs, "Hard Errors", KSTAT_DATA_UINT32);
53207c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_transerrs, "Transport Errors",
53217c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
53227c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_icnt_busy, "Interconnect Busy",
53237c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
53247c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_icnt_errors, "Interconnect Errors",
53257c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
53267c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_phci_rsrc, "pHCI No Resources",
53277c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
53287c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_phci_localerr, "pHCI Local Errors",
53297c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
53307c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_phci_invstate, "pHCI Invalid State",
53317c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
53327c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_failedfrom, "Failed From",
53337c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
53347c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_failedto, "Failed To", KSTAT_DATA_UINT32);
53357c478bd9Sstevel@tonic-gate 
53367c478bd9Sstevel@tonic-gate 	mdi_statp = kmem_alloc(sizeof (*mdi_statp), KM_SLEEP);
53377c478bd9Sstevel@tonic-gate 	mdi_statp->pi_kstat_ref = 1;
53387c478bd9Sstevel@tonic-gate 	mdi_statp->pi_kstat_iostats = kiosp;
53397c478bd9Sstevel@tonic-gate 	mdi_statp->pi_kstat_errstats = kerrsp;
53407c478bd9Sstevel@tonic-gate 	kstat_install(kiosp);
53417c478bd9Sstevel@tonic-gate 	kstat_install(kerrsp);
53427c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_kstats = mdi_statp;
53437c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
53447c478bd9Sstevel@tonic-gate }
53457c478bd9Sstevel@tonic-gate 
53467c478bd9Sstevel@tonic-gate /*
53477c478bd9Sstevel@tonic-gate  * destroy per-path properties
53487c478bd9Sstevel@tonic-gate  */
53497c478bd9Sstevel@tonic-gate static void
53507c478bd9Sstevel@tonic-gate i_mdi_pi_kstat_destroy(mdi_pathinfo_t *pip)
53517c478bd9Sstevel@tonic-gate {
53527c478bd9Sstevel@tonic-gate 
53537c478bd9Sstevel@tonic-gate 	struct mdi_pi_kstats *mdi_statp;
53547c478bd9Sstevel@tonic-gate 
53557c478bd9Sstevel@tonic-gate 	if ((mdi_statp = MDI_PI(pip)->pi_kstats) == NULL)
53567c478bd9Sstevel@tonic-gate 		return;
53577c478bd9Sstevel@tonic-gate 
53587c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_kstats = NULL;
53597c478bd9Sstevel@tonic-gate 
53607c478bd9Sstevel@tonic-gate 	/*
53617c478bd9Sstevel@tonic-gate 	 * the kstat may be shared between multiple pathinfo nodes
53627c478bd9Sstevel@tonic-gate 	 * decrement this pathinfo's usage, removing the kstats
53637c478bd9Sstevel@tonic-gate 	 * themselves when the last pathinfo reference is removed.
53647c478bd9Sstevel@tonic-gate 	 */
53657c478bd9Sstevel@tonic-gate 	ASSERT(mdi_statp->pi_kstat_ref > 0);
53667c478bd9Sstevel@tonic-gate 	if (--mdi_statp->pi_kstat_ref != 0)
53677c478bd9Sstevel@tonic-gate 		return;
53687c478bd9Sstevel@tonic-gate 
53697c478bd9Sstevel@tonic-gate 	kstat_delete(mdi_statp->pi_kstat_iostats);
53707c478bd9Sstevel@tonic-gate 	kstat_delete(mdi_statp->pi_kstat_errstats);
53717c478bd9Sstevel@tonic-gate 	kmem_free(mdi_statp, sizeof (*mdi_statp));
53727c478bd9Sstevel@tonic-gate }
53737c478bd9Sstevel@tonic-gate 
53747c478bd9Sstevel@tonic-gate /*
53757c478bd9Sstevel@tonic-gate  * update I/O paths KSTATS
53767c478bd9Sstevel@tonic-gate  */
53777c478bd9Sstevel@tonic-gate void
53787c478bd9Sstevel@tonic-gate mdi_pi_kstat_iosupdate(mdi_pathinfo_t *pip, struct buf *bp)
53797c478bd9Sstevel@tonic-gate {
53807c478bd9Sstevel@tonic-gate 	kstat_t *iostatp;
53817c478bd9Sstevel@tonic-gate 	size_t xfer_cnt;
53827c478bd9Sstevel@tonic-gate 
53837c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
53847c478bd9Sstevel@tonic-gate 
53857c478bd9Sstevel@tonic-gate 	/*
53867c478bd9Sstevel@tonic-gate 	 * I/O can be driven across a path prior to having path
53877c478bd9Sstevel@tonic-gate 	 * statistics available, i.e. probe(9e).
53887c478bd9Sstevel@tonic-gate 	 */
53897c478bd9Sstevel@tonic-gate 	if (bp != NULL && MDI_PI(pip)->pi_kstats != NULL) {
53907c478bd9Sstevel@tonic-gate 		iostatp = MDI_PI(pip)->pi_kstats->pi_kstat_iostats;
53917c478bd9Sstevel@tonic-gate 		xfer_cnt = bp->b_bcount - bp->b_resid;
53927c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
53937c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->reads++;
53947c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->nread += xfer_cnt;
53957c478bd9Sstevel@tonic-gate 		} else {
53967c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->writes++;
53977c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->nwritten += xfer_cnt;
53987c478bd9Sstevel@tonic-gate 		}
53997c478bd9Sstevel@tonic-gate 	}
54007c478bd9Sstevel@tonic-gate }
54017c478bd9Sstevel@tonic-gate 
54027c478bd9Sstevel@tonic-gate /*
5403ee28b439Scm136836  * Enable the path(specific client/target/initiator)
5404ee28b439Scm136836  * Enabling a path means that MPxIO may select the enabled path for routing
5405ee28b439Scm136836  * future I/O requests, subject to other path state constraints.
5406ee28b439Scm136836  */
5407ee28b439Scm136836 int
5408ee28b439Scm136836 mdi_pi_enable_path(mdi_pathinfo_t *pip, int flags)
5409ee28b439Scm136836 {
5410ee28b439Scm136836 	mdi_phci_t	*ph;
5411ee28b439Scm136836 
5412ee28b439Scm136836 	ph = i_devi_get_phci(mdi_pi_get_phci(pip));
5413ee28b439Scm136836 	if (ph == NULL) {
5414ee28b439Scm136836 		MDI_DEBUG(1, (CE_NOTE, NULL, "!mdi_pi_enable_path:"
5415*144dfaa9Scth 			" failed. pip: %p ph = NULL\n", (void *)pip));
5416ee28b439Scm136836 		return (MDI_FAILURE);
5417ee28b439Scm136836 	}
5418ee28b439Scm136836 
5419ee28b439Scm136836 	(void) i_mdi_enable_disable_path(pip, ph->ph_vhci, flags,
5420ee28b439Scm136836 		MDI_ENABLE_OP);
5421ee28b439Scm136836 	MDI_DEBUG(5, (CE_NOTE, NULL, "!mdi_pi_enable_path:"
5422*144dfaa9Scth 		" Returning success pip = %p. ph = %p\n",
5423*144dfaa9Scth 		(void *)pip, (void *)ph));
5424ee28b439Scm136836 	return (MDI_SUCCESS);
5425ee28b439Scm136836 
5426ee28b439Scm136836 }
5427ee28b439Scm136836 
5428ee28b439Scm136836 /*
5429ee28b439Scm136836  * Disable the path (specific client/target/initiator)
5430ee28b439Scm136836  * Disabling a path means that MPxIO will not select the disabled path for
5431ee28b439Scm136836  * routing any new I/O requests.
5432ee28b439Scm136836  */
5433ee28b439Scm136836 int
5434ee28b439Scm136836 mdi_pi_disable_path(mdi_pathinfo_t *pip, int flags)
5435ee28b439Scm136836 {
5436ee28b439Scm136836 	mdi_phci_t	*ph;
5437ee28b439Scm136836 
5438ee28b439Scm136836 	ph = i_devi_get_phci(mdi_pi_get_phci(pip));
5439ee28b439Scm136836 	if (ph == NULL) {
5440ee28b439Scm136836 		MDI_DEBUG(1, (CE_NOTE, NULL, "!mdi_pi_disable_path:"
5441*144dfaa9Scth 			" failed. pip: %p ph = NULL\n", (void *)pip));
5442ee28b439Scm136836 		return (MDI_FAILURE);
5443ee28b439Scm136836 	}
5444ee28b439Scm136836 
5445ee28b439Scm136836 	(void) i_mdi_enable_disable_path(pip,
5446ee28b439Scm136836 			ph->ph_vhci, flags, MDI_DISABLE_OP);
5447ee28b439Scm136836 	MDI_DEBUG(5, (CE_NOTE, NULL, "!mdi_pi_disable_path:"
5448*144dfaa9Scth 		"Returning success pip = %p. ph = %p",
5449*144dfaa9Scth 		(void *)pip, (void *)ph));
5450ee28b439Scm136836 	return (MDI_SUCCESS);
5451ee28b439Scm136836 }
5452ee28b439Scm136836 
5453ee28b439Scm136836 /*
54547c478bd9Sstevel@tonic-gate  * disable the path to a particular pHCI (pHCI specified in the phci_path
54557c478bd9Sstevel@tonic-gate  * argument) for a particular client (specified in the client_path argument).
54567c478bd9Sstevel@tonic-gate  * Disabling a path means that MPxIO will not select the disabled path for
54577c478bd9Sstevel@tonic-gate  * routing any new I/O requests.
5458ee28b439Scm136836  * NOTE: this will be removed once the NWS files are changed to use the new
5459ee28b439Scm136836  * mdi_{enable,disable}_path interfaces
54607c478bd9Sstevel@tonic-gate  */
54617c478bd9Sstevel@tonic-gate int
54627c478bd9Sstevel@tonic-gate mdi_pi_disable(dev_info_t *cdip, dev_info_t *pdip, int flags)
54637c478bd9Sstevel@tonic-gate {
54647c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_enable_disable(cdip, pdip, flags, MDI_DISABLE_OP));
54657c478bd9Sstevel@tonic-gate }
54667c478bd9Sstevel@tonic-gate 
54677c478bd9Sstevel@tonic-gate /*
54687c478bd9Sstevel@tonic-gate  * Enable the path to a particular pHCI (pHCI specified in the phci_path
54697c478bd9Sstevel@tonic-gate  * argument) for a particular client (specified in the client_path argument).
54707c478bd9Sstevel@tonic-gate  * Enabling a path means that MPxIO may select the enabled path for routing
54717c478bd9Sstevel@tonic-gate  * future I/O requests, subject to other path state constraints.
5472ee28b439Scm136836  * NOTE: this will be removed once the NWS files are changed to use the new
5473ee28b439Scm136836  * mdi_{enable,disable}_path interfaces
54747c478bd9Sstevel@tonic-gate  */
54757c478bd9Sstevel@tonic-gate 
54767c478bd9Sstevel@tonic-gate int
54777c478bd9Sstevel@tonic-gate mdi_pi_enable(dev_info_t *cdip, dev_info_t *pdip, int flags)
54787c478bd9Sstevel@tonic-gate {
54797c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_enable_disable(cdip, pdip, flags, MDI_ENABLE_OP));
54807c478bd9Sstevel@tonic-gate }
54817c478bd9Sstevel@tonic-gate 
5482ee28b439Scm136836 /*
5483ee28b439Scm136836  * Common routine for doing enable/disable.
5484ee28b439Scm136836  */
5485ee28b439Scm136836 static mdi_pathinfo_t *
5486ee28b439Scm136836 i_mdi_enable_disable_path(mdi_pathinfo_t *pip, mdi_vhci_t *vh, int flags,
5487ee28b439Scm136836 		int op)
5488ee28b439Scm136836 {
5489ee28b439Scm136836 	int		sync_flag = 0;
5490ee28b439Scm136836 	int		rv;
5491ee28b439Scm136836 	mdi_pathinfo_t 	*next;
5492ee28b439Scm136836 	int		(*f)() = NULL;
5493ee28b439Scm136836 
5494ee28b439Scm136836 	f = vh->vh_ops->vo_pi_state_change;
5495ee28b439Scm136836 
5496ee28b439Scm136836 	sync_flag = (flags << 8) & 0xf00;
5497ee28b439Scm136836 
5498ee28b439Scm136836 	/*
5499ee28b439Scm136836 	 * Do a callback into the mdi consumer to let it
5500ee28b439Scm136836 	 * know that path is about to get enabled/disabled.
5501ee28b439Scm136836 	 */
5502ee28b439Scm136836 	if (f != NULL) {
5503ee28b439Scm136836 		rv = (*f)(vh->vh_dip, pip, 0,
5504ee28b439Scm136836 			MDI_PI_EXT_STATE(pip),
5505ee28b439Scm136836 			MDI_EXT_STATE_CHANGE | sync_flag |
5506ee28b439Scm136836 			op | MDI_BEFORE_STATE_CHANGE);
5507ee28b439Scm136836 		if (rv != MDI_SUCCESS) {
5508ee28b439Scm136836 			MDI_DEBUG(2, (CE_WARN, vh->vh_dip,
5509ee28b439Scm136836 			"!vo_pi_state_change: failed rv = %x", rv));
5510ee28b439Scm136836 		}
5511ee28b439Scm136836 	}
5512ee28b439Scm136836 	MDI_PI_LOCK(pip);
5513ee28b439Scm136836 	next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
5514ee28b439Scm136836 
5515ee28b439Scm136836 	switch (flags) {
5516ee28b439Scm136836 		case USER_DISABLE:
5517*144dfaa9Scth 			if (op == MDI_DISABLE_OP) {
5518ee28b439Scm136836 				MDI_PI_SET_USER_DISABLE(pip);
5519*144dfaa9Scth 			} else {
5520ee28b439Scm136836 				MDI_PI_SET_USER_ENABLE(pip);
5521*144dfaa9Scth 			}
5522ee28b439Scm136836 			break;
5523ee28b439Scm136836 		case DRIVER_DISABLE:
5524*144dfaa9Scth 			if (op == MDI_DISABLE_OP) {
5525ee28b439Scm136836 				MDI_PI_SET_DRV_DISABLE(pip);
5526*144dfaa9Scth 			} else {
5527ee28b439Scm136836 				MDI_PI_SET_DRV_ENABLE(pip);
5528*144dfaa9Scth 			}
5529ee28b439Scm136836 			break;
5530ee28b439Scm136836 		case DRIVER_DISABLE_TRANSIENT:
5531*144dfaa9Scth 			if (op == MDI_DISABLE_OP && rv == MDI_SUCCESS) {
5532ee28b439Scm136836 				MDI_PI_SET_DRV_DISABLE_TRANS(pip);
5533*144dfaa9Scth 			} else {
5534ee28b439Scm136836 				MDI_PI_SET_DRV_ENABLE_TRANS(pip);
5535*144dfaa9Scth 			}
5536ee28b439Scm136836 			break;
5537ee28b439Scm136836 	}
5538ee28b439Scm136836 	MDI_PI_UNLOCK(pip);
5539ee28b439Scm136836 	/*
5540ee28b439Scm136836 	 * Do a callback into the mdi consumer to let it
5541ee28b439Scm136836 	 * know that path is now enabled/disabled.
5542ee28b439Scm136836 	 */
5543ee28b439Scm136836 	if (f != NULL) {
5544ee28b439Scm136836 		rv = (*f)(vh->vh_dip, pip, 0,
5545ee28b439Scm136836 			MDI_PI_EXT_STATE(pip),
5546ee28b439Scm136836 			MDI_EXT_STATE_CHANGE | sync_flag |
5547ee28b439Scm136836 			op | MDI_AFTER_STATE_CHANGE);
5548ee28b439Scm136836 		if (rv != MDI_SUCCESS) {
5549ee28b439Scm136836 			MDI_DEBUG(2, (CE_WARN, vh->vh_dip,
5550ee28b439Scm136836 			"!vo_pi_state_change: failed rv = %x", rv));
5551ee28b439Scm136836 		}
5552ee28b439Scm136836 	}
5553ee28b439Scm136836 	return (next);
5554ee28b439Scm136836 }
55557c478bd9Sstevel@tonic-gate 
55567c478bd9Sstevel@tonic-gate /*
55577c478bd9Sstevel@tonic-gate  * Common routine for doing enable/disable.
5558ee28b439Scm136836  * NOTE: this will be removed once the NWS files are changed to use the new
5559ee28b439Scm136836  * mdi_{enable,disable}_path has been putback
55607c478bd9Sstevel@tonic-gate  */
55617c478bd9Sstevel@tonic-gate int
55627c478bd9Sstevel@tonic-gate i_mdi_pi_enable_disable(dev_info_t *cdip, dev_info_t *pdip, int flags, int op)
55637c478bd9Sstevel@tonic-gate {
55647c478bd9Sstevel@tonic-gate 
55657c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
55667c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
55677c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
55687c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next, *pip;
55697c478bd9Sstevel@tonic-gate 	int		found_it;
55707c478bd9Sstevel@tonic-gate 
55717c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
55727c478bd9Sstevel@tonic-gate 	MDI_DEBUG(5, (CE_NOTE, NULL, "!i_mdi_pi_enable_disable: "
5573*144dfaa9Scth 		"Op = %d pdip = %p cdip = %p\n", op, (void *)pdip,
5574*144dfaa9Scth 		(void *)cdip));
55757c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
55767c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_NOTE, NULL, "!i_mdi_pi_enable_disable:"
5577*144dfaa9Scth 			"Op %d failed. ph = NULL\n", op));
55787c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
55797c478bd9Sstevel@tonic-gate 	}
55807c478bd9Sstevel@tonic-gate 
55817c478bd9Sstevel@tonic-gate 	if ((op != MDI_ENABLE_OP) && (op != MDI_DISABLE_OP)) {
55827c478bd9Sstevel@tonic-gate 		MDI_DEBUG(1, (CE_NOTE, NULL, "!i_mdi_pi_enable_disable: "
5583*144dfaa9Scth 			"Op Invalid operation = %d\n", op));
55847c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
55857c478bd9Sstevel@tonic-gate 	}
55867c478bd9Sstevel@tonic-gate 
55877c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
55887c478bd9Sstevel@tonic-gate 
55897c478bd9Sstevel@tonic-gate 	if (cdip == NULL) {
55907c478bd9Sstevel@tonic-gate 		/*
55917c478bd9Sstevel@tonic-gate 		 * Need to mark the Phci as enabled/disabled.
55927c478bd9Sstevel@tonic-gate 		 */
55937c478bd9Sstevel@tonic-gate 		MDI_DEBUG(3, (CE_NOTE, NULL, "!i_mdi_pi_enable_disable: "
5594*144dfaa9Scth 		"Op %d for the phci\n", op));
55957c478bd9Sstevel@tonic-gate 		MDI_PHCI_LOCK(ph);
55967c478bd9Sstevel@tonic-gate 		switch (flags) {
55977c478bd9Sstevel@tonic-gate 			case USER_DISABLE:
5598*144dfaa9Scth 				if (op == MDI_DISABLE_OP) {
55997c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_USER_DISABLE(ph);
5600*144dfaa9Scth 				} else {
56017c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_USER_ENABLE(ph);
5602*144dfaa9Scth 				}
56037c478bd9Sstevel@tonic-gate 				break;
56047c478bd9Sstevel@tonic-gate 			case DRIVER_DISABLE:
5605*144dfaa9Scth 				if (op == MDI_DISABLE_OP) {
56067c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_DISABLE(ph);
5607*144dfaa9Scth 				} else {
56087c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_ENABLE(ph);
5609*144dfaa9Scth 				}
56107c478bd9Sstevel@tonic-gate 				break;
56117c478bd9Sstevel@tonic-gate 			case DRIVER_DISABLE_TRANSIENT:
5612*144dfaa9Scth 				if (op == MDI_DISABLE_OP) {
56137c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_DISABLE_TRANSIENT(ph);
5614*144dfaa9Scth 				} else {
56157c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_ENABLE_TRANSIENT(ph);
5616*144dfaa9Scth 				}
56177c478bd9Sstevel@tonic-gate 				break;
56187c478bd9Sstevel@tonic-gate 			default:
56197c478bd9Sstevel@tonic-gate 				MDI_PHCI_UNLOCK(ph);
56207c478bd9Sstevel@tonic-gate 				MDI_DEBUG(1, (CE_NOTE, NULL,
56217c478bd9Sstevel@tonic-gate 				"!i_mdi_pi_enable_disable:"
56227c478bd9Sstevel@tonic-gate 				" Invalid flag argument= %d\n", flags));
56237c478bd9Sstevel@tonic-gate 		}
56247c478bd9Sstevel@tonic-gate 
56257c478bd9Sstevel@tonic-gate 		/*
56267c478bd9Sstevel@tonic-gate 		 * Phci has been disabled. Now try to enable/disable
56277c478bd9Sstevel@tonic-gate 		 * path info's to each client.
56287c478bd9Sstevel@tonic-gate 		 */
56297c478bd9Sstevel@tonic-gate 		pip = ph->ph_path_head;
56307c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
5631ee28b439Scm136836 			pip = i_mdi_enable_disable_path(pip, vh, flags, op);
56327c478bd9Sstevel@tonic-gate 		}
56337c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
56347c478bd9Sstevel@tonic-gate 	} else {
56357c478bd9Sstevel@tonic-gate 
56367c478bd9Sstevel@tonic-gate 		/*
56377c478bd9Sstevel@tonic-gate 		 * Disable a specific client.
56387c478bd9Sstevel@tonic-gate 		 */
56397c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(cdip);
56407c478bd9Sstevel@tonic-gate 		if (ct == NULL) {
56417c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_NOTE, NULL,
56427c478bd9Sstevel@tonic-gate 			"!i_mdi_pi_enable_disable:"
56437c478bd9Sstevel@tonic-gate 			" failed. ct = NULL operation = %d\n", op));
56447c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
56457c478bd9Sstevel@tonic-gate 		}
56467c478bd9Sstevel@tonic-gate 
56477c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
56487c478bd9Sstevel@tonic-gate 		pip = ct->ct_path_head;
56497c478bd9Sstevel@tonic-gate 		found_it = 0;
56507c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
56517c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
56527c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
56537c478bd9Sstevel@tonic-gate 			if (MDI_PI(pip)->pi_phci == ph) {
56547c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
56557c478bd9Sstevel@tonic-gate 				found_it = 1;
56567c478bd9Sstevel@tonic-gate 				break;
56577c478bd9Sstevel@tonic-gate 			}
56587c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
56597c478bd9Sstevel@tonic-gate 			pip = next;
56607c478bd9Sstevel@tonic-gate 		}
56617c478bd9Sstevel@tonic-gate 
5662ee28b439Scm136836 
56637c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
56647c478bd9Sstevel@tonic-gate 		if (found_it == 0) {
56657c478bd9Sstevel@tonic-gate 			MDI_DEBUG(1, (CE_NOTE, NULL,
56667c478bd9Sstevel@tonic-gate 			"!i_mdi_pi_enable_disable:"
56677c478bd9Sstevel@tonic-gate 			" failed. Could not find corresponding pip\n"));
56687c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
56697c478bd9Sstevel@tonic-gate 		}
5670ee28b439Scm136836 
5671ee28b439Scm136836 		(void) i_mdi_enable_disable_path(pip, vh, flags, op);
56727c478bd9Sstevel@tonic-gate 	}
56737c478bd9Sstevel@tonic-gate 
56747c478bd9Sstevel@tonic-gate 	MDI_DEBUG(5, (CE_NOTE, NULL, "!i_mdi_pi_enable_disable: "
5675*144dfaa9Scth 		"Op %d Returning success pdip = %p cdip = %p\n",
5676*144dfaa9Scth 		op, (void *)pdip, (void *)cdip));
56777c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
56787c478bd9Sstevel@tonic-gate }
56797c478bd9Sstevel@tonic-gate 
56807c478bd9Sstevel@tonic-gate /*
56817c478bd9Sstevel@tonic-gate  * Ensure phci powered up
56827c478bd9Sstevel@tonic-gate  */
56837c478bd9Sstevel@tonic-gate static void
56847c478bd9Sstevel@tonic-gate i_mdi_pm_hold_pip(mdi_pathinfo_t *pip)
56857c478bd9Sstevel@tonic-gate {
56867c478bd9Sstevel@tonic-gate 	dev_info_t	*ph_dip;
56877c478bd9Sstevel@tonic-gate 
56887c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
5689*144dfaa9Scth 	ASSERT(MDI_PI_LOCKED(pip));
56907c478bd9Sstevel@tonic-gate 
56917c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held) {
56927c478bd9Sstevel@tonic-gate 		return;
56937c478bd9Sstevel@tonic-gate 	}
56947c478bd9Sstevel@tonic-gate 
56957c478bd9Sstevel@tonic-gate 	ph_dip = mdi_pi_get_phci(pip);
5696*144dfaa9Scth 	MDI_DEBUG(4, (CE_NOTE, ph_dip, "i_mdi_pm_hold_pip for %s%d %p\n",
5697*144dfaa9Scth 	    ddi_get_name(ph_dip), ddi_get_instance(ph_dip), (void *)pip));
56987c478bd9Sstevel@tonic-gate 	if (ph_dip == NULL) {
56997c478bd9Sstevel@tonic-gate 		return;
57007c478bd9Sstevel@tonic-gate 	}
57017c478bd9Sstevel@tonic-gate 
57027c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
57037c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, ph_dip, "kidsupcnt was %d\n",
57047c478bd9Sstevel@tonic-gate 	    DEVI(ph_dip)->devi_pm_kidsupcnt));
5705*144dfaa9Scth 
57067c478bd9Sstevel@tonic-gate 	pm_hold_power(ph_dip);
5707*144dfaa9Scth 
57087c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, ph_dip, "kidsupcnt is %d\n",
57097c478bd9Sstevel@tonic-gate 	    DEVI(ph_dip)->devi_pm_kidsupcnt));
57107c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
57117c478bd9Sstevel@tonic-gate 
57127c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_pm_held = 1;
57137c478bd9Sstevel@tonic-gate }
57147c478bd9Sstevel@tonic-gate 
57157c478bd9Sstevel@tonic-gate /*
57167c478bd9Sstevel@tonic-gate  * Allow phci powered down
57177c478bd9Sstevel@tonic-gate  */
57187c478bd9Sstevel@tonic-gate static void
57197c478bd9Sstevel@tonic-gate i_mdi_pm_rele_pip(mdi_pathinfo_t *pip)
57207c478bd9Sstevel@tonic-gate {
57217c478bd9Sstevel@tonic-gate 	dev_info_t	*ph_dip = NULL;
57227c478bd9Sstevel@tonic-gate 
57237c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
5724*144dfaa9Scth 	ASSERT(MDI_PI_LOCKED(pip));
57257c478bd9Sstevel@tonic-gate 
57267c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held == 0) {
57277c478bd9Sstevel@tonic-gate 		return;
57287c478bd9Sstevel@tonic-gate 	}
57297c478bd9Sstevel@tonic-gate 
57307c478bd9Sstevel@tonic-gate 	ph_dip = mdi_pi_get_phci(pip);
57317c478bd9Sstevel@tonic-gate 	ASSERT(ph_dip != NULL);
57327c478bd9Sstevel@tonic-gate 
57337c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
5734*144dfaa9Scth 	MDI_DEBUG(4, (CE_NOTE, ph_dip, "i_mdi_pm_rele_pip for %s%d %p\n",
5735*144dfaa9Scth 	    ddi_get_name(ph_dip), ddi_get_instance(ph_dip), (void *)pip));
57367c478bd9Sstevel@tonic-gate 
57377c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, ph_dip, "kidsupcnt was %d\n",
57387c478bd9Sstevel@tonic-gate 	    DEVI(ph_dip)->devi_pm_kidsupcnt));
57397c478bd9Sstevel@tonic-gate 	pm_rele_power(ph_dip);
57407c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, ph_dip, "kidsupcnt is %d\n",
57417c478bd9Sstevel@tonic-gate 	    DEVI(ph_dip)->devi_pm_kidsupcnt));
57427c478bd9Sstevel@tonic-gate 
57437c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
57447c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_pm_held = 0;
57457c478bd9Sstevel@tonic-gate }
57467c478bd9Sstevel@tonic-gate 
57477c478bd9Sstevel@tonic-gate static void
57487c478bd9Sstevel@tonic-gate i_mdi_pm_hold_client(mdi_client_t *ct, int incr)
57497c478bd9Sstevel@tonic-gate {
5750*144dfaa9Scth 	ASSERT(MDI_CLIENT_LOCKED(ct));
57517c478bd9Sstevel@tonic-gate 
57527c478bd9Sstevel@tonic-gate 	ct->ct_power_cnt += incr;
5753*144dfaa9Scth 	MDI_DEBUG(4, (CE_NOTE, ct->ct_dip, "i_mdi_pm_hold_client %p "
5754*144dfaa9Scth 	    "ct_power_cnt = %d incr = %d\n", (void *)ct,
5755*144dfaa9Scth 	    ct->ct_power_cnt, incr));
57567c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_power_cnt >= 0);
57577c478bd9Sstevel@tonic-gate }
57587c478bd9Sstevel@tonic-gate 
57597c478bd9Sstevel@tonic-gate static void
57607c478bd9Sstevel@tonic-gate i_mdi_rele_all_phci(mdi_client_t *ct)
57617c478bd9Sstevel@tonic-gate {
57627c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t  *pip;
57637c478bd9Sstevel@tonic-gate 
5764*144dfaa9Scth 	ASSERT(MDI_CLIENT_LOCKED(ct));
57657c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
57667c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
57677c478bd9Sstevel@tonic-gate 		mdi_hold_path(pip);
57687c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
57697c478bd9Sstevel@tonic-gate 		i_mdi_pm_rele_pip(pip);
57707c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
57717c478bd9Sstevel@tonic-gate 		mdi_rele_path(pip);
57727c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
57737c478bd9Sstevel@tonic-gate 	}
57747c478bd9Sstevel@tonic-gate }
57757c478bd9Sstevel@tonic-gate 
57767c478bd9Sstevel@tonic-gate static void
57777c478bd9Sstevel@tonic-gate i_mdi_pm_rele_client(mdi_client_t *ct, int decr)
57787c478bd9Sstevel@tonic-gate {
5779*144dfaa9Scth 	ASSERT(MDI_CLIENT_LOCKED(ct));
57807c478bd9Sstevel@tonic-gate 
5781737d277aScth 	if (i_ddi_devi_attached(ct->ct_dip)) {
57827c478bd9Sstevel@tonic-gate 		ct->ct_power_cnt -= decr;
5783*144dfaa9Scth 		MDI_DEBUG(4, (CE_NOTE, ct->ct_dip, "i_mdi_pm_rele_client %p "
5784*144dfaa9Scth 		    "ct_power_cnt = %d decr = %d\n",
5785*144dfaa9Scth 		    (void *)ct, ct->ct_power_cnt, decr));
57867c478bd9Sstevel@tonic-gate 	}
57877c478bd9Sstevel@tonic-gate 
57887c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_power_cnt >= 0);
57897c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0) {
57907c478bd9Sstevel@tonic-gate 		i_mdi_rele_all_phci(ct);
57917c478bd9Sstevel@tonic-gate 		return;
57927c478bd9Sstevel@tonic-gate 	}
57937c478bd9Sstevel@tonic-gate }
57947c478bd9Sstevel@tonic-gate 
57957c478bd9Sstevel@tonic-gate static void
57967c478bd9Sstevel@tonic-gate i_mdi_pm_reset_client(mdi_client_t *ct)
57977c478bd9Sstevel@tonic-gate {
5798*144dfaa9Scth 	MDI_DEBUG(4, (CE_NOTE, ct->ct_dip, "i_mdi_pm_reset_client %p "
5799*144dfaa9Scth 	    "ct_power_cnt = %d\n", (void *)ct, ct->ct_power_cnt));
5800*144dfaa9Scth 	ASSERT(MDI_CLIENT_LOCKED(ct));
58017c478bd9Sstevel@tonic-gate 	ct->ct_power_cnt = 0;
58027c478bd9Sstevel@tonic-gate 	i_mdi_rele_all_phci(ct);
580378dc6db2Sllai1 	ct->ct_powercnt_config = 0;
580478dc6db2Sllai1 	ct->ct_powercnt_unconfig = 0;
58057c478bd9Sstevel@tonic-gate 	ct->ct_powercnt_reset = 1;
58067c478bd9Sstevel@tonic-gate }
58077c478bd9Sstevel@tonic-gate 
58087c478bd9Sstevel@tonic-gate static int
58097c478bd9Sstevel@tonic-gate i_mdi_power_one_phci(mdi_pathinfo_t *pip)
58107c478bd9Sstevel@tonic-gate {
58117c478bd9Sstevel@tonic-gate 	int		ret;
58127c478bd9Sstevel@tonic-gate 	dev_info_t	*ph_dip;
58137c478bd9Sstevel@tonic-gate 
58147c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
58157c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_pip(pip);
58167c478bd9Sstevel@tonic-gate 
58177c478bd9Sstevel@tonic-gate 	ph_dip = mdi_pi_get_phci(pip);
58187c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
58197c478bd9Sstevel@tonic-gate 
58207c478bd9Sstevel@tonic-gate 	/* bring all components of phci to full power */
58217c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, ph_dip, "i_mdi_power_one_phci "
5822*144dfaa9Scth 	    "pm_powerup for %s%d %p\n", ddi_get_name(ph_dip),
5823*144dfaa9Scth 	    ddi_get_instance(ph_dip), (void *)pip));
58247c478bd9Sstevel@tonic-gate 
58257c478bd9Sstevel@tonic-gate 	ret = pm_powerup(ph_dip);
58267c478bd9Sstevel@tonic-gate 
58277c478bd9Sstevel@tonic-gate 	if (ret == DDI_FAILURE) {
58287c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, ph_dip, "i_mdi_power_one_phci "
5829*144dfaa9Scth 		    "pm_powerup FAILED for %s%d %p\n",
5830*144dfaa9Scth 		    ddi_get_name(ph_dip), ddi_get_instance(ph_dip),
5831*144dfaa9Scth 		    (void *)pip));
58327c478bd9Sstevel@tonic-gate 
58337c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
58347c478bd9Sstevel@tonic-gate 		i_mdi_pm_rele_pip(pip);
58357c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
58367c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
58377c478bd9Sstevel@tonic-gate 	}
58387c478bd9Sstevel@tonic-gate 
58397c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
58407c478bd9Sstevel@tonic-gate }
58417c478bd9Sstevel@tonic-gate 
58427c478bd9Sstevel@tonic-gate static int
58437c478bd9Sstevel@tonic-gate i_mdi_power_all_phci(mdi_client_t *ct)
58447c478bd9Sstevel@tonic-gate {
58457c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t  *pip;
58467c478bd9Sstevel@tonic-gate 	int		succeeded = 0;
58477c478bd9Sstevel@tonic-gate 
5848*144dfaa9Scth 	ASSERT(MDI_CLIENT_LOCKED(ct));
58497c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
58507c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
5851*144dfaa9Scth 		if (MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip)) {
58527c478bd9Sstevel@tonic-gate 			mdi_hold_path(pip);
58537c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
58547c478bd9Sstevel@tonic-gate 			if (i_mdi_power_one_phci(pip) == MDI_SUCCESS)
58557c478bd9Sstevel@tonic-gate 				succeeded = 1;
58567c478bd9Sstevel@tonic-gate 
58577c478bd9Sstevel@tonic-gate 			ASSERT(ct == MDI_PI(pip)->pi_client);
58587c478bd9Sstevel@tonic-gate 			MDI_CLIENT_LOCK(ct);
58597c478bd9Sstevel@tonic-gate 			mdi_rele_path(pip);
5860*144dfaa9Scth 		}
58617c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
58627c478bd9Sstevel@tonic-gate 	}
58637c478bd9Sstevel@tonic-gate 
58647c478bd9Sstevel@tonic-gate 	return (succeeded ? MDI_SUCCESS : MDI_FAILURE);
58657c478bd9Sstevel@tonic-gate }
58667c478bd9Sstevel@tonic-gate 
58677c478bd9Sstevel@tonic-gate /*
58687c478bd9Sstevel@tonic-gate  * mdi_bus_power():
58697c478bd9Sstevel@tonic-gate  *		1. Place the phci(s) into powered up state so that
58707c478bd9Sstevel@tonic-gate  *		   client can do power management
58717c478bd9Sstevel@tonic-gate  *		2. Ensure phci powered up as client power managing
58727c478bd9Sstevel@tonic-gate  * Return Values:
58737c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
58747c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
58757c478bd9Sstevel@tonic-gate  */
58767c478bd9Sstevel@tonic-gate int
58777c478bd9Sstevel@tonic-gate mdi_bus_power(dev_info_t *parent, void *impl_arg, pm_bus_power_op_t op,
58787c478bd9Sstevel@tonic-gate     void *arg, void *result)
58797c478bd9Sstevel@tonic-gate {
58807c478bd9Sstevel@tonic-gate 	int			ret = MDI_SUCCESS;
58817c478bd9Sstevel@tonic-gate 	pm_bp_child_pwrchg_t	*bpc;
58827c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
58837c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
58847c478bd9Sstevel@tonic-gate 	pm_bp_has_changed_t	*bphc;
58857c478bd9Sstevel@tonic-gate 
58867c478bd9Sstevel@tonic-gate 	/*
58877c478bd9Sstevel@tonic-gate 	 * BUS_POWER_NOINVOL not supported
58887c478bd9Sstevel@tonic-gate 	 */
58897c478bd9Sstevel@tonic-gate 	if (op == BUS_POWER_NOINVOL)
58907c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
58917c478bd9Sstevel@tonic-gate 
58927c478bd9Sstevel@tonic-gate 	/*
58937c478bd9Sstevel@tonic-gate 	 * ignore other OPs.
58947c478bd9Sstevel@tonic-gate 	 * return quickly to save cou cycles on the ct processing
58957c478bd9Sstevel@tonic-gate 	 */
58967c478bd9Sstevel@tonic-gate 	switch (op) {
58977c478bd9Sstevel@tonic-gate 	case BUS_POWER_PRE_NOTIFICATION:
58987c478bd9Sstevel@tonic-gate 	case BUS_POWER_POST_NOTIFICATION:
58997c478bd9Sstevel@tonic-gate 		bpc = (pm_bp_child_pwrchg_t *)arg;
59007c478bd9Sstevel@tonic-gate 		cdip = bpc->bpc_dip;
59017c478bd9Sstevel@tonic-gate 		break;
59027c478bd9Sstevel@tonic-gate 	case BUS_POWER_HAS_CHANGED:
59037c478bd9Sstevel@tonic-gate 		bphc = (pm_bp_has_changed_t *)arg;
59047c478bd9Sstevel@tonic-gate 		cdip = bphc->bphc_dip;
59057c478bd9Sstevel@tonic-gate 		break;
59067c478bd9Sstevel@tonic-gate 	default:
59077c478bd9Sstevel@tonic-gate 		return (pm_busop_bus_power(parent, impl_arg, op, arg, result));
59087c478bd9Sstevel@tonic-gate 	}
59097c478bd9Sstevel@tonic-gate 
59107c478bd9Sstevel@tonic-gate 	ASSERT(MDI_CLIENT(cdip));
59117c478bd9Sstevel@tonic-gate 
59127c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
59137c478bd9Sstevel@tonic-gate 	if (ct == NULL)
59147c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
59157c478bd9Sstevel@tonic-gate 
59167c478bd9Sstevel@tonic-gate 	/*
59177c478bd9Sstevel@tonic-gate 	 * wait till the mdi_pathinfo node state change are processed
59187c478bd9Sstevel@tonic-gate 	 */
59197c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
59207c478bd9Sstevel@tonic-gate 	switch (op) {
59217c478bd9Sstevel@tonic-gate 	case BUS_POWER_PRE_NOTIFICATION:
59227c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, bpc->bpc_dip, "mdi_bus_power "
59237c478bd9Sstevel@tonic-gate 		    "BUS_POWER_PRE_NOTIFICATION:"
59247c478bd9Sstevel@tonic-gate 		    "%s@%s, olevel=%d, nlevel=%d, comp=%d\n",
59257c478bd9Sstevel@tonic-gate 		    PM_NAME(bpc->bpc_dip), PM_ADDR(bpc->bpc_dip),
59267c478bd9Sstevel@tonic-gate 		    bpc->bpc_olevel, bpc->bpc_nlevel, bpc->bpc_comp));
59277c478bd9Sstevel@tonic-gate 
59287c478bd9Sstevel@tonic-gate 		/* serialize power level change per client */
59297c478bd9Sstevel@tonic-gate 		while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
59307c478bd9Sstevel@tonic-gate 			cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
59317c478bd9Sstevel@tonic-gate 
59327c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_POWER_TRANSITION(ct);
59337c478bd9Sstevel@tonic-gate 
59347c478bd9Sstevel@tonic-gate 		if (ct->ct_power_cnt == 0) {
59357c478bd9Sstevel@tonic-gate 			ret = i_mdi_power_all_phci(ct);
59367c478bd9Sstevel@tonic-gate 		}
59377c478bd9Sstevel@tonic-gate 
59387c478bd9Sstevel@tonic-gate 		/*
59397c478bd9Sstevel@tonic-gate 		 * if new_level > 0:
59407c478bd9Sstevel@tonic-gate 		 *	- hold phci(s)
59417c478bd9Sstevel@tonic-gate 		 *	- power up phci(s) if not already
59427c478bd9Sstevel@tonic-gate 		 * ignore power down
59437c478bd9Sstevel@tonic-gate 		 */
59447c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel > 0) {
59457c478bd9Sstevel@tonic-gate 			if (!DEVI_IS_ATTACHING(ct->ct_dip)) {
59467c478bd9Sstevel@tonic-gate 				MDI_DEBUG(4, (CE_NOTE, bpc->bpc_dip,
59477c478bd9Sstevel@tonic-gate 				    "mdi_bus_power i_mdi_pm_hold_client\n"));
59487c478bd9Sstevel@tonic-gate 				i_mdi_pm_hold_client(ct, ct->ct_path_count);
59497c478bd9Sstevel@tonic-gate 			}
59507c478bd9Sstevel@tonic-gate 		}
59517c478bd9Sstevel@tonic-gate 		break;
59527c478bd9Sstevel@tonic-gate 	case BUS_POWER_POST_NOTIFICATION:
59537c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, bpc->bpc_dip, "mdi_bus_power "
59547c478bd9Sstevel@tonic-gate 		    "BUS_POWER_POST_NOTIFICATION:"
59557c478bd9Sstevel@tonic-gate 		    "%s@%s, olevel=%d, nlevel=%d, comp=%d result=%d\n",
59567c478bd9Sstevel@tonic-gate 		    PM_NAME(bpc->bpc_dip), PM_ADDR(bpc->bpc_dip),
59577c478bd9Sstevel@tonic-gate 		    bpc->bpc_olevel, bpc->bpc_nlevel, bpc->bpc_comp,
59587c478bd9Sstevel@tonic-gate 		    *(int *)result));
59597c478bd9Sstevel@tonic-gate 
59607c478bd9Sstevel@tonic-gate 		if (*(int *)result == DDI_SUCCESS) {
59617c478bd9Sstevel@tonic-gate 			if (bpc->bpc_nlevel > 0) {
59627c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_POWER_UP(ct);
59637c478bd9Sstevel@tonic-gate 			} else {
59647c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_POWER_DOWN(ct);
59657c478bd9Sstevel@tonic-gate 			}
59667c478bd9Sstevel@tonic-gate 		}
59677c478bd9Sstevel@tonic-gate 
59687c478bd9Sstevel@tonic-gate 		/* release the hold we did in pre-notification */
59697c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel > 0 && (*(int *)result != DDI_SUCCESS) &&
59707c478bd9Sstevel@tonic-gate 		    !DEVI_IS_ATTACHING(ct->ct_dip)) {
59717c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, bpc->bpc_dip,
59727c478bd9Sstevel@tonic-gate 			    "mdi_bus_power i_mdi_pm_rele_client\n"));
59737c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, ct->ct_path_count);
59747c478bd9Sstevel@tonic-gate 		}
59757c478bd9Sstevel@tonic-gate 
59767c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel == 0 && (*(int *)result == DDI_SUCCESS)) {
59777c478bd9Sstevel@tonic-gate 			/* another thread might started attaching */
59787c478bd9Sstevel@tonic-gate 			if (DEVI_IS_ATTACHING(ct->ct_dip)) {
59797c478bd9Sstevel@tonic-gate 				MDI_DEBUG(4, (CE_NOTE, bpc->bpc_dip,
59807c478bd9Sstevel@tonic-gate 				    "mdi_bus_power i_mdi_pm_rele_client\n"));
59817c478bd9Sstevel@tonic-gate 				i_mdi_pm_rele_client(ct, ct->ct_path_count);
59827c478bd9Sstevel@tonic-gate 			/* detaching has been taken care in pm_post_unconfig */
59837c478bd9Sstevel@tonic-gate 			} else if (!DEVI_IS_DETACHING(ct->ct_dip)) {
59847c478bd9Sstevel@tonic-gate 				MDI_DEBUG(4, (CE_NOTE, bpc->bpc_dip,
59857c478bd9Sstevel@tonic-gate 				    "mdi_bus_power i_mdi_pm_reset_client\n"));
59867c478bd9Sstevel@tonic-gate 				i_mdi_pm_reset_client(ct);
59877c478bd9Sstevel@tonic-gate 			}
59887c478bd9Sstevel@tonic-gate 		}
59897c478bd9Sstevel@tonic-gate 
59907c478bd9Sstevel@tonic-gate 		MDI_CLIENT_CLEAR_POWER_TRANSITION(ct);
59917c478bd9Sstevel@tonic-gate 		cv_broadcast(&ct->ct_powerchange_cv);
59927c478bd9Sstevel@tonic-gate 
59937c478bd9Sstevel@tonic-gate 		break;
59947c478bd9Sstevel@tonic-gate 
59957c478bd9Sstevel@tonic-gate 	/* need to do more */
59967c478bd9Sstevel@tonic-gate 	case BUS_POWER_HAS_CHANGED:
59977c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, bphc->bphc_dip, "mdi_bus_power "
59987c478bd9Sstevel@tonic-gate 		    "BUS_POWER_HAS_CHANGED:"
59997c478bd9Sstevel@tonic-gate 		    "%s@%s, olevel=%d, nlevel=%d, comp=%d\n",
60007c478bd9Sstevel@tonic-gate 		    PM_NAME(bphc->bphc_dip), PM_ADDR(bphc->bphc_dip),
60017c478bd9Sstevel@tonic-gate 		    bphc->bphc_olevel, bphc->bphc_nlevel, bphc->bphc_comp));
60027c478bd9Sstevel@tonic-gate 
60037c478bd9Sstevel@tonic-gate 		if (bphc->bphc_nlevel > 0 &&
60047c478bd9Sstevel@tonic-gate 		    bphc->bphc_nlevel > bphc->bphc_olevel) {
60057c478bd9Sstevel@tonic-gate 			if (ct->ct_power_cnt == 0) {
60067c478bd9Sstevel@tonic-gate 				ret = i_mdi_power_all_phci(ct);
60077c478bd9Sstevel@tonic-gate 			}
60087c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, bphc->bphc_dip,
60097c478bd9Sstevel@tonic-gate 			    "mdi_bus_power i_mdi_pm_hold_client\n"));
60107c478bd9Sstevel@tonic-gate 			i_mdi_pm_hold_client(ct, ct->ct_path_count);
60117c478bd9Sstevel@tonic-gate 		}
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate 		if (bphc->bphc_nlevel == 0 && bphc->bphc_olevel != -1) {
60147c478bd9Sstevel@tonic-gate 			MDI_DEBUG(4, (CE_NOTE, bphc->bphc_dip,
60157c478bd9Sstevel@tonic-gate 			    "mdi_bus_power i_mdi_pm_rele_client\n"));
60167c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, ct->ct_path_count);
60177c478bd9Sstevel@tonic-gate 		}
60187c478bd9Sstevel@tonic-gate 		break;
60197c478bd9Sstevel@tonic-gate 	}
60207c478bd9Sstevel@tonic-gate 
60217c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
60227c478bd9Sstevel@tonic-gate 	return (ret);
60237c478bd9Sstevel@tonic-gate }
60247c478bd9Sstevel@tonic-gate 
60257c478bd9Sstevel@tonic-gate static int
60267c478bd9Sstevel@tonic-gate i_mdi_pm_pre_config_one(dev_info_t *child)
60277c478bd9Sstevel@tonic-gate {
60287c478bd9Sstevel@tonic-gate 	int		ret = MDI_SUCCESS;
60297c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
60307c478bd9Sstevel@tonic-gate 
60317c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
60327c478bd9Sstevel@tonic-gate 	if (ct == NULL)
60337c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
60347c478bd9Sstevel@tonic-gate 
60357c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
60367c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
60377c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
60387c478bd9Sstevel@tonic-gate 
60397c478bd9Sstevel@tonic-gate 	if (!MDI_CLIENT_IS_FAILED(ct)) {
60407c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
60417c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
60427c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_pre_config_one already configured\n"));
60437c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
60447c478bd9Sstevel@tonic-gate 	}
60457c478bd9Sstevel@tonic-gate 
604678dc6db2Sllai1 	if (ct->ct_powercnt_config) {
60477c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
60487c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
60497c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_pre_config_one ALREADY held\n"));
60507c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
60517c478bd9Sstevel@tonic-gate 	}
60527c478bd9Sstevel@tonic-gate 
60537c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0) {
60547c478bd9Sstevel@tonic-gate 		ret = i_mdi_power_all_phci(ct);
60557c478bd9Sstevel@tonic-gate 	}
60567c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, child,
60577c478bd9Sstevel@tonic-gate 	    "i_mdi_pm_pre_config_one i_mdi_pm_hold_client\n"));
60587c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_client(ct, ct->ct_path_count);
605978dc6db2Sllai1 	ct->ct_powercnt_config = 1;
60607c478bd9Sstevel@tonic-gate 	ct->ct_powercnt_reset = 0;
60617c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
60627c478bd9Sstevel@tonic-gate 	return (ret);
60637c478bd9Sstevel@tonic-gate }
60647c478bd9Sstevel@tonic-gate 
60657c478bd9Sstevel@tonic-gate static int
6066*144dfaa9Scth i_mdi_pm_pre_config(dev_info_t *vdip, dev_info_t *child)
60677c478bd9Sstevel@tonic-gate {
60687c478bd9Sstevel@tonic-gate 	int			ret = MDI_SUCCESS;
60697c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
60707c478bd9Sstevel@tonic-gate 	int			circ;
60717c478bd9Sstevel@tonic-gate 
6072*144dfaa9Scth 	ASSERT(MDI_VHCI(vdip));
60737c478bd9Sstevel@tonic-gate 
60747c478bd9Sstevel@tonic-gate 	/* ndi_devi_config_one */
60757c478bd9Sstevel@tonic-gate 	if (child) {
6076*144dfaa9Scth 		ASSERT(DEVI_BUSY_OWNED(vdip));
60777c478bd9Sstevel@tonic-gate 		return (i_mdi_pm_pre_config_one(child));
60787c478bd9Sstevel@tonic-gate 	}
60797c478bd9Sstevel@tonic-gate 
60807c478bd9Sstevel@tonic-gate 	/* devi_config_common */
6081*144dfaa9Scth 	ndi_devi_enter(vdip, &circ);
6082*144dfaa9Scth 	cdip = ddi_get_child(vdip);
60837c478bd9Sstevel@tonic-gate 	while (cdip) {
60847c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
60857c478bd9Sstevel@tonic-gate 
60867c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_config_one(cdip);
60877c478bd9Sstevel@tonic-gate 		if (ret != MDI_SUCCESS)
60887c478bd9Sstevel@tonic-gate 			break;
60897c478bd9Sstevel@tonic-gate 		cdip = next;
60907c478bd9Sstevel@tonic-gate 	}
6091*144dfaa9Scth 	ndi_devi_exit(vdip, circ);
60927c478bd9Sstevel@tonic-gate 	return (ret);
60937c478bd9Sstevel@tonic-gate }
60947c478bd9Sstevel@tonic-gate 
60957c478bd9Sstevel@tonic-gate static int
60967c478bd9Sstevel@tonic-gate i_mdi_pm_pre_unconfig_one(dev_info_t *child, int *held, int flags)
60977c478bd9Sstevel@tonic-gate {
60987c478bd9Sstevel@tonic-gate 	int		ret = MDI_SUCCESS;
60997c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
61007c478bd9Sstevel@tonic-gate 
61017c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
61027c478bd9Sstevel@tonic-gate 	if (ct == NULL)
61037c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
61047c478bd9Sstevel@tonic-gate 
61057c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
61067c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
61077c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
61087c478bd9Sstevel@tonic-gate 
6109737d277aScth 	if (!i_ddi_devi_attached(ct->ct_dip)) {
61107c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
61117c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_pre_unconfig node detached already\n"));
61127c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
61137c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
61147c478bd9Sstevel@tonic-gate 	}
61157c478bd9Sstevel@tonic-gate 
61167c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_POWERED_DOWN(ct) &&
61177c478bd9Sstevel@tonic-gate 	    (flags & NDI_AUTODETACH)) {
61187c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
61197c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_pre_unconfig auto-modunload\n"));
61207c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
61217c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
61227c478bd9Sstevel@tonic-gate 	}
61237c478bd9Sstevel@tonic-gate 
612478dc6db2Sllai1 	if (ct->ct_powercnt_unconfig) {
61257c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
61267c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_pre_unconfig ct_powercnt_held\n"));
61277c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
61287c478bd9Sstevel@tonic-gate 		*held = 1;
61297c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
61307c478bd9Sstevel@tonic-gate 	}
61317c478bd9Sstevel@tonic-gate 
61327c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0) {
61337c478bd9Sstevel@tonic-gate 		ret = i_mdi_power_all_phci(ct);
61347c478bd9Sstevel@tonic-gate 	}
61357c478bd9Sstevel@tonic-gate 	MDI_DEBUG(4, (CE_NOTE, child,
61367c478bd9Sstevel@tonic-gate 	    "i_mdi_pm_pre_unconfig i_mdi_pm_hold_client\n"));
61377c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_client(ct, ct->ct_path_count);
613878dc6db2Sllai1 	ct->ct_powercnt_unconfig = 1;
61397c478bd9Sstevel@tonic-gate 	ct->ct_powercnt_reset = 0;
61407c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
61417c478bd9Sstevel@tonic-gate 	if (ret == MDI_SUCCESS)
61427c478bd9Sstevel@tonic-gate 		*held = 1;
61437c478bd9Sstevel@tonic-gate 	return (ret);
61447c478bd9Sstevel@tonic-gate }
61457c478bd9Sstevel@tonic-gate 
61467c478bd9Sstevel@tonic-gate static int
6147*144dfaa9Scth i_mdi_pm_pre_unconfig(dev_info_t *vdip, dev_info_t *child, int *held,
61487c478bd9Sstevel@tonic-gate     int flags)
61497c478bd9Sstevel@tonic-gate {
61507c478bd9Sstevel@tonic-gate 	int			ret = MDI_SUCCESS;
61517c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
61527c478bd9Sstevel@tonic-gate 	int			circ;
61537c478bd9Sstevel@tonic-gate 
6154*144dfaa9Scth 	ASSERT(MDI_VHCI(vdip));
61557c478bd9Sstevel@tonic-gate 	*held = 0;
61567c478bd9Sstevel@tonic-gate 
61577c478bd9Sstevel@tonic-gate 	/* ndi_devi_unconfig_one */
61587c478bd9Sstevel@tonic-gate 	if (child) {
6159*144dfaa9Scth 		ASSERT(DEVI_BUSY_OWNED(vdip));
61607c478bd9Sstevel@tonic-gate 		return (i_mdi_pm_pre_unconfig_one(child, held, flags));
61617c478bd9Sstevel@tonic-gate 	}
61627c478bd9Sstevel@tonic-gate 
61637c478bd9Sstevel@tonic-gate 	/* devi_unconfig_common */
6164*144dfaa9Scth 	ndi_devi_enter(vdip, &circ);
6165*144dfaa9Scth 	cdip = ddi_get_child(vdip);
61667c478bd9Sstevel@tonic-gate 	while (cdip) {
61677c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
61687c478bd9Sstevel@tonic-gate 
61697c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_unconfig_one(cdip, held, flags);
61707c478bd9Sstevel@tonic-gate 		cdip = next;
61717c478bd9Sstevel@tonic-gate 	}
6172*144dfaa9Scth 	ndi_devi_exit(vdip, circ);
61737c478bd9Sstevel@tonic-gate 
61747c478bd9Sstevel@tonic-gate 	if (*held)
61757c478bd9Sstevel@tonic-gate 		ret = MDI_SUCCESS;
61767c478bd9Sstevel@tonic-gate 
61777c478bd9Sstevel@tonic-gate 	return (ret);
61787c478bd9Sstevel@tonic-gate }
61797c478bd9Sstevel@tonic-gate 
61807c478bd9Sstevel@tonic-gate static void
61817c478bd9Sstevel@tonic-gate i_mdi_pm_post_config_one(dev_info_t *child)
61827c478bd9Sstevel@tonic-gate {
61837c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
61847c478bd9Sstevel@tonic-gate 
61857c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
61867c478bd9Sstevel@tonic-gate 	if (ct == NULL)
61877c478bd9Sstevel@tonic-gate 		return;
61887c478bd9Sstevel@tonic-gate 
61897c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
61907c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
61917c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
61927c478bd9Sstevel@tonic-gate 
619378dc6db2Sllai1 	if (ct->ct_powercnt_reset || !ct->ct_powercnt_config) {
61947c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
619578dc6db2Sllai1 		    "i_mdi_pm_post_config_one NOT configured\n"));
61967c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
61977c478bd9Sstevel@tonic-gate 		return;
61987c478bd9Sstevel@tonic-gate 	}
61997c478bd9Sstevel@tonic-gate 
62007c478bd9Sstevel@tonic-gate 	/* client has not been updated */
62017c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_FAILED(ct)) {
62027c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
62037c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_post_config_one NOT configured\n"));
62047c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
62057c478bd9Sstevel@tonic-gate 		return;
62067c478bd9Sstevel@tonic-gate 	}
62077c478bd9Sstevel@tonic-gate 
62087c478bd9Sstevel@tonic-gate 	/* another thread might have powered it down or detached it */
62097c478bd9Sstevel@tonic-gate 	if ((MDI_CLIENT_IS_POWERED_DOWN(ct) &&
62107c478bd9Sstevel@tonic-gate 	    !DEVI_IS_ATTACHING(ct->ct_dip)) ||
6211737d277aScth 	    (!i_ddi_devi_attached(ct->ct_dip) &&
62127c478bd9Sstevel@tonic-gate 	    !DEVI_IS_ATTACHING(ct->ct_dip))) {
62137c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
62147c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_post_config i_mdi_pm_reset_client\n"));
62157c478bd9Sstevel@tonic-gate 		i_mdi_pm_reset_client(ct);
62167c478bd9Sstevel@tonic-gate 	} else {
62177c478bd9Sstevel@tonic-gate 		mdi_pathinfo_t  *pip, *next;
62187c478bd9Sstevel@tonic-gate 		int	valid_path_count = 0;
62197c478bd9Sstevel@tonic-gate 
62207c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
62217c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_post_config i_mdi_pm_rele_client\n"));
62227c478bd9Sstevel@tonic-gate 		pip = ct->ct_path_head;
62237c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
62247c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
62257c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
622678dc6db2Sllai1 			if (MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip))
62277c478bd9Sstevel@tonic-gate 				valid_path_count ++;
62287c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
62297c478bd9Sstevel@tonic-gate 			pip = next;
62307c478bd9Sstevel@tonic-gate 		}
62317c478bd9Sstevel@tonic-gate 		i_mdi_pm_rele_client(ct, valid_path_count);
62327c478bd9Sstevel@tonic-gate 	}
623378dc6db2Sllai1 	ct->ct_powercnt_config = 0;
62347c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
62357c478bd9Sstevel@tonic-gate }
62367c478bd9Sstevel@tonic-gate 
62377c478bd9Sstevel@tonic-gate static void
6238*144dfaa9Scth i_mdi_pm_post_config(dev_info_t *vdip, dev_info_t *child)
62397c478bd9Sstevel@tonic-gate {
62407c478bd9Sstevel@tonic-gate 	int		circ;
62417c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
6242*144dfaa9Scth 
6243*144dfaa9Scth 	ASSERT(MDI_VHCI(vdip));
62447c478bd9Sstevel@tonic-gate 
62457c478bd9Sstevel@tonic-gate 	/* ndi_devi_config_one */
62467c478bd9Sstevel@tonic-gate 	if (child) {
6247*144dfaa9Scth 		ASSERT(DEVI_BUSY_OWNED(vdip));
62487c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_config_one(child);
62497c478bd9Sstevel@tonic-gate 		return;
62507c478bd9Sstevel@tonic-gate 	}
62517c478bd9Sstevel@tonic-gate 
62527c478bd9Sstevel@tonic-gate 	/* devi_config_common */
6253*144dfaa9Scth 	ndi_devi_enter(vdip, &circ);
6254*144dfaa9Scth 	cdip = ddi_get_child(vdip);
62557c478bd9Sstevel@tonic-gate 	while (cdip) {
62567c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
62577c478bd9Sstevel@tonic-gate 
62587c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_config_one(cdip);
62597c478bd9Sstevel@tonic-gate 		cdip = next;
62607c478bd9Sstevel@tonic-gate 	}
6261*144dfaa9Scth 	ndi_devi_exit(vdip, circ);
62627c478bd9Sstevel@tonic-gate }
62637c478bd9Sstevel@tonic-gate 
62647c478bd9Sstevel@tonic-gate static void
62657c478bd9Sstevel@tonic-gate i_mdi_pm_post_unconfig_one(dev_info_t *child)
62667c478bd9Sstevel@tonic-gate {
62677c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
62687c478bd9Sstevel@tonic-gate 
62697c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
62707c478bd9Sstevel@tonic-gate 	if (ct == NULL)
62717c478bd9Sstevel@tonic-gate 		return;
62727c478bd9Sstevel@tonic-gate 
62737c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
62747c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
62757c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
62767c478bd9Sstevel@tonic-gate 
627778dc6db2Sllai1 	if (!ct->ct_powercnt_unconfig || ct->ct_powercnt_reset) {
62787c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
62797c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_post_unconfig NOT held\n"));
62807c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
62817c478bd9Sstevel@tonic-gate 		return;
62827c478bd9Sstevel@tonic-gate 	}
62837c478bd9Sstevel@tonic-gate 
62847c478bd9Sstevel@tonic-gate 	/* failure detaching or another thread just attached it */
62857c478bd9Sstevel@tonic-gate 	if ((MDI_CLIENT_IS_POWERED_DOWN(ct) &&
6286737d277aScth 	    i_ddi_devi_attached(ct->ct_dip)) ||
6287737d277aScth 	    (!i_ddi_devi_attached(ct->ct_dip) &&
62887c478bd9Sstevel@tonic-gate 	    !DEVI_IS_ATTACHING(ct->ct_dip))) {
62897c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
62907c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_post_unconfig i_mdi_pm_reset_client\n"));
62917c478bd9Sstevel@tonic-gate 		i_mdi_pm_reset_client(ct);
629278dc6db2Sllai1 	} else {
629378dc6db2Sllai1 		mdi_pathinfo_t  *pip, *next;
629478dc6db2Sllai1 		int	valid_path_count = 0;
62957c478bd9Sstevel@tonic-gate 
62967c478bd9Sstevel@tonic-gate 		MDI_DEBUG(4, (CE_NOTE, child,
629778dc6db2Sllai1 		    "i_mdi_pm_post_unconfig i_mdi_pm_rele_client\n"));
629878dc6db2Sllai1 		pip = ct->ct_path_head;
629978dc6db2Sllai1 		while (pip != NULL) {
630078dc6db2Sllai1 			MDI_PI_LOCK(pip);
630178dc6db2Sllai1 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
630278dc6db2Sllai1 			if (MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip))
630378dc6db2Sllai1 				valid_path_count ++;
630478dc6db2Sllai1 			MDI_PI_UNLOCK(pip);
630578dc6db2Sllai1 			pip = next;
630678dc6db2Sllai1 		}
630778dc6db2Sllai1 		i_mdi_pm_rele_client(ct, valid_path_count);
630878dc6db2Sllai1 		ct->ct_powercnt_unconfig = 0;
630978dc6db2Sllai1 	}
631078dc6db2Sllai1 
63117c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
63127c478bd9Sstevel@tonic-gate }
63137c478bd9Sstevel@tonic-gate 
63147c478bd9Sstevel@tonic-gate static void
6315*144dfaa9Scth i_mdi_pm_post_unconfig(dev_info_t *vdip, dev_info_t *child, int held)
63167c478bd9Sstevel@tonic-gate {
63177c478bd9Sstevel@tonic-gate 	int			circ;
63187c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
63197c478bd9Sstevel@tonic-gate 
6320*144dfaa9Scth 	ASSERT(MDI_VHCI(vdip));
63217c478bd9Sstevel@tonic-gate 
63227c478bd9Sstevel@tonic-gate 	if (!held) {
6323*144dfaa9Scth 		MDI_DEBUG(4, (CE_NOTE, vdip,
63247c478bd9Sstevel@tonic-gate 		    "i_mdi_pm_post_unconfig held = %d\n", held));
63257c478bd9Sstevel@tonic-gate 		return;
63267c478bd9Sstevel@tonic-gate 	}
63277c478bd9Sstevel@tonic-gate 
63287c478bd9Sstevel@tonic-gate 	if (child) {
6329*144dfaa9Scth 		ASSERT(DEVI_BUSY_OWNED(vdip));
63307c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_unconfig_one(child);
63317c478bd9Sstevel@tonic-gate 		return;
63327c478bd9Sstevel@tonic-gate 	}
63337c478bd9Sstevel@tonic-gate 
6334*144dfaa9Scth 	ndi_devi_enter(vdip, &circ);
6335*144dfaa9Scth 	cdip = ddi_get_child(vdip);
63367c478bd9Sstevel@tonic-gate 	while (cdip) {
63377c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
63387c478bd9Sstevel@tonic-gate 
63397c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_unconfig_one(cdip);
63407c478bd9Sstevel@tonic-gate 		cdip = next;
63417c478bd9Sstevel@tonic-gate 	}
6342*144dfaa9Scth 	ndi_devi_exit(vdip, circ);
63437c478bd9Sstevel@tonic-gate }
63447c478bd9Sstevel@tonic-gate 
63457c478bd9Sstevel@tonic-gate int
63467c478bd9Sstevel@tonic-gate mdi_power(dev_info_t *vdip, mdi_pm_op_t op, void *args, char *devnm, int flags)
63477c478bd9Sstevel@tonic-gate {
63487c478bd9Sstevel@tonic-gate 	int			circ, ret = MDI_SUCCESS;
63497c478bd9Sstevel@tonic-gate 	dev_info_t		*client_dip = NULL;
63507c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
63517c478bd9Sstevel@tonic-gate 
63527c478bd9Sstevel@tonic-gate 	/*
63537c478bd9Sstevel@tonic-gate 	 * Handling ndi_devi_config_one and ndi_devi_unconfig_one.
63547c478bd9Sstevel@tonic-gate 	 * Power up pHCI for the named client device.
63557c478bd9Sstevel@tonic-gate 	 * Note: Before the client is enumerated under vhci by phci,
63567c478bd9Sstevel@tonic-gate 	 * client_dip can be NULL. Then proceed to power up all the
63577c478bd9Sstevel@tonic-gate 	 * pHCIs.
63587c478bd9Sstevel@tonic-gate 	 */
63597c478bd9Sstevel@tonic-gate 	if (devnm != NULL) {
63607c478bd9Sstevel@tonic-gate 		ndi_devi_enter(vdip, &circ);
63617c478bd9Sstevel@tonic-gate 		client_dip = ndi_devi_findchild(vdip, devnm);
63627c478bd9Sstevel@tonic-gate 	}
63637c478bd9Sstevel@tonic-gate 
6364*144dfaa9Scth 	MDI_DEBUG(4, (CE_NOTE, vdip, "mdi_power op = %d %s %p\n",
6365*144dfaa9Scth 	    op, devnm ? devnm : "NULL", (void *)client_dip));
63667c478bd9Sstevel@tonic-gate 
63677c478bd9Sstevel@tonic-gate 	switch (op) {
63687c478bd9Sstevel@tonic-gate 	case MDI_PM_PRE_CONFIG:
63697c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_config(vdip, client_dip);
63707c478bd9Sstevel@tonic-gate 		break;
6371*144dfaa9Scth 
63727c478bd9Sstevel@tonic-gate 	case MDI_PM_PRE_UNCONFIG:
63737c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_unconfig(vdip, client_dip, (int *)args,
63747c478bd9Sstevel@tonic-gate 		    flags);
63757c478bd9Sstevel@tonic-gate 		break;
6376*144dfaa9Scth 
63777c478bd9Sstevel@tonic-gate 	case MDI_PM_POST_CONFIG:
63787c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_config(vdip, client_dip);
63797c478bd9Sstevel@tonic-gate 		break;
6380*144dfaa9Scth 
63817c478bd9Sstevel@tonic-gate 	case MDI_PM_POST_UNCONFIG:
63827c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_unconfig(vdip, client_dip, *(int *)args);
63837c478bd9Sstevel@tonic-gate 		break;
6384*144dfaa9Scth 
63857c478bd9Sstevel@tonic-gate 	case MDI_PM_HOLD_POWER:
63867c478bd9Sstevel@tonic-gate 	case MDI_PM_RELE_POWER:
63877c478bd9Sstevel@tonic-gate 		ASSERT(args);
63887c478bd9Sstevel@tonic-gate 
63897c478bd9Sstevel@tonic-gate 		client_dip = (dev_info_t *)args;
63907c478bd9Sstevel@tonic-gate 		ASSERT(MDI_CLIENT(client_dip));
63917c478bd9Sstevel@tonic-gate 
63927c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(client_dip);
63937c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
63947c478bd9Sstevel@tonic-gate 
63957c478bd9Sstevel@tonic-gate 		if (op == MDI_PM_HOLD_POWER) {
63967c478bd9Sstevel@tonic-gate 			if (ct->ct_power_cnt == 0) {
63977c478bd9Sstevel@tonic-gate 				(void) i_mdi_power_all_phci(ct);
63987c478bd9Sstevel@tonic-gate 				MDI_DEBUG(4, (CE_NOTE, client_dip,
63997c478bd9Sstevel@tonic-gate 				    "mdi_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 		} else {
64037c478bd9Sstevel@tonic-gate 			if (DEVI_IS_ATTACHING(ct->ct_dip)) {
64047c478bd9Sstevel@tonic-gate 				MDI_DEBUG(4, (CE_NOTE, client_dip,
64057c478bd9Sstevel@tonic-gate 				    "mdi_power i_mdi_pm_rele_client\n"));
64067c478bd9Sstevel@tonic-gate 				i_mdi_pm_rele_client(ct, ct->ct_path_count);
64077c478bd9Sstevel@tonic-gate 			} else {
64087c478bd9Sstevel@tonic-gate 				MDI_DEBUG(4, (CE_NOTE, client_dip,
64097c478bd9Sstevel@tonic-gate 				    "mdi_power i_mdi_pm_reset_client\n"));
64107c478bd9Sstevel@tonic-gate 				i_mdi_pm_reset_client(ct);
64117c478bd9Sstevel@tonic-gate 			}
64127c478bd9Sstevel@tonic-gate 		}
64137c478bd9Sstevel@tonic-gate 
64147c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
64157c478bd9Sstevel@tonic-gate 		break;
6416*144dfaa9Scth 
64177c478bd9Sstevel@tonic-gate 	default:
64187c478bd9Sstevel@tonic-gate 		break;
64197c478bd9Sstevel@tonic-gate 	}
64207c478bd9Sstevel@tonic-gate 
6421*144dfaa9Scth 	if (devnm)
6422*144dfaa9Scth 		ndi_devi_exit(vdip, circ);
6423*144dfaa9Scth 
64247c478bd9Sstevel@tonic-gate 	return (ret);
64257c478bd9Sstevel@tonic-gate }
64267c478bd9Sstevel@tonic-gate 
64277c478bd9Sstevel@tonic-gate int
64287c478bd9Sstevel@tonic-gate mdi_component_is_vhci(dev_info_t *dip, const char **mdi_class)
64297c478bd9Sstevel@tonic-gate {
64307c478bd9Sstevel@tonic-gate 	mdi_vhci_t *vhci;
64317c478bd9Sstevel@tonic-gate 
64327c478bd9Sstevel@tonic-gate 	if (!MDI_VHCI(dip))
64337c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
64347c478bd9Sstevel@tonic-gate 
64357c478bd9Sstevel@tonic-gate 	if (mdi_class) {
64367c478bd9Sstevel@tonic-gate 		vhci = DEVI(dip)->devi_mdi_xhci;
64377c478bd9Sstevel@tonic-gate 		ASSERT(vhci);
64387c478bd9Sstevel@tonic-gate 		*mdi_class = vhci->vh_class;
64397c478bd9Sstevel@tonic-gate 	}
64407c478bd9Sstevel@tonic-gate 
64417c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
64427c478bd9Sstevel@tonic-gate }
64437c478bd9Sstevel@tonic-gate 
64447c478bd9Sstevel@tonic-gate int
64457c478bd9Sstevel@tonic-gate mdi_component_is_phci(dev_info_t *dip, const char **mdi_class)
64467c478bd9Sstevel@tonic-gate {
64477c478bd9Sstevel@tonic-gate 	mdi_phci_t *phci;
64487c478bd9Sstevel@tonic-gate 
64497c478bd9Sstevel@tonic-gate 	if (!MDI_PHCI(dip))
64507c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
64517c478bd9Sstevel@tonic-gate 
64527c478bd9Sstevel@tonic-gate 	if (mdi_class) {
64537c478bd9Sstevel@tonic-gate 		phci = DEVI(dip)->devi_mdi_xhci;
64547c478bd9Sstevel@tonic-gate 		ASSERT(phci);
64557c478bd9Sstevel@tonic-gate 		*mdi_class = phci->ph_vhci->vh_class;
64567c478bd9Sstevel@tonic-gate 	}
64577c478bd9Sstevel@tonic-gate 
64587c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
64597c478bd9Sstevel@tonic-gate }
64607c478bd9Sstevel@tonic-gate 
64617c478bd9Sstevel@tonic-gate int
64627c478bd9Sstevel@tonic-gate mdi_component_is_client(dev_info_t *dip, const char **mdi_class)
64637c478bd9Sstevel@tonic-gate {
64647c478bd9Sstevel@tonic-gate 	mdi_client_t *client;
64657c478bd9Sstevel@tonic-gate 
64667c478bd9Sstevel@tonic-gate 	if (!MDI_CLIENT(dip))
64677c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
64687c478bd9Sstevel@tonic-gate 
64697c478bd9Sstevel@tonic-gate 	if (mdi_class) {
64707c478bd9Sstevel@tonic-gate 		client = DEVI(dip)->devi_mdi_client;
64717c478bd9Sstevel@tonic-gate 		ASSERT(client);
64727c478bd9Sstevel@tonic-gate 		*mdi_class = client->ct_vhci->vh_class;
64737c478bd9Sstevel@tonic-gate 	}
64747c478bd9Sstevel@tonic-gate 
64757c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
64767c478bd9Sstevel@tonic-gate }
64777c478bd9Sstevel@tonic-gate 
64787c478bd9Sstevel@tonic-gate void *
64797c478bd9Sstevel@tonic-gate mdi_client_get_vhci_private(dev_info_t *dip)
64807c478bd9Sstevel@tonic-gate {
64817c478bd9Sstevel@tonic-gate 	ASSERT(mdi_component_is_client(dip, NULL) == MDI_SUCCESS);
64827c478bd9Sstevel@tonic-gate 	if (mdi_component_is_client(dip, NULL) == MDI_SUCCESS) {
64837c478bd9Sstevel@tonic-gate 		mdi_client_t	*ct;
64847c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(dip);
64857c478bd9Sstevel@tonic-gate 		return (ct->ct_vprivate);
64867c478bd9Sstevel@tonic-gate 	}
64877c478bd9Sstevel@tonic-gate 	return (NULL);
64887c478bd9Sstevel@tonic-gate }
64897c478bd9Sstevel@tonic-gate 
64907c478bd9Sstevel@tonic-gate void
64917c478bd9Sstevel@tonic-gate mdi_client_set_vhci_private(dev_info_t *dip, void *data)
64927c478bd9Sstevel@tonic-gate {
64937c478bd9Sstevel@tonic-gate 	ASSERT(mdi_component_is_client(dip, NULL) == MDI_SUCCESS);
64947c478bd9Sstevel@tonic-gate 	if (mdi_component_is_client(dip, NULL) == MDI_SUCCESS) {
64957c478bd9Sstevel@tonic-gate 		mdi_client_t	*ct;
64967c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(dip);
64977c478bd9Sstevel@tonic-gate 		ct->ct_vprivate = data;
64987c478bd9Sstevel@tonic-gate 	}
64997c478bd9Sstevel@tonic-gate }
65007c478bd9Sstevel@tonic-gate /*
65017c478bd9Sstevel@tonic-gate  * mdi_pi_get_vhci_private():
65027c478bd9Sstevel@tonic-gate  *		Get the vhci private information associated with the
65037c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node
65047c478bd9Sstevel@tonic-gate  */
65057c478bd9Sstevel@tonic-gate void *
65067c478bd9Sstevel@tonic-gate mdi_pi_get_vhci_private(mdi_pathinfo_t *pip)
65077c478bd9Sstevel@tonic-gate {
65087c478bd9Sstevel@tonic-gate 	caddr_t	vprivate = NULL;
65097c478bd9Sstevel@tonic-gate 	if (pip) {
65107c478bd9Sstevel@tonic-gate 		vprivate = MDI_PI(pip)->pi_vprivate;
65117c478bd9Sstevel@tonic-gate 	}
65127c478bd9Sstevel@tonic-gate 	return (vprivate);
65137c478bd9Sstevel@tonic-gate }
65147c478bd9Sstevel@tonic-gate 
65157c478bd9Sstevel@tonic-gate /*
65167c478bd9Sstevel@tonic-gate  * mdi_pi_set_vhci_private():
65177c478bd9Sstevel@tonic-gate  *		Set the vhci private information in the mdi_pathinfo node
65187c478bd9Sstevel@tonic-gate  */
65197c478bd9Sstevel@tonic-gate void
65207c478bd9Sstevel@tonic-gate mdi_pi_set_vhci_private(mdi_pathinfo_t *pip, void *priv)
65217c478bd9Sstevel@tonic-gate {
65227c478bd9Sstevel@tonic-gate 	if (pip) {
65237c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_vprivate = priv;
65247c478bd9Sstevel@tonic-gate 	}
65257c478bd9Sstevel@tonic-gate }
65267c478bd9Sstevel@tonic-gate 
65277c478bd9Sstevel@tonic-gate /*
65287c478bd9Sstevel@tonic-gate  * mdi_phci_get_vhci_private():
65297c478bd9Sstevel@tonic-gate  *		Get the vhci private information associated with the
65307c478bd9Sstevel@tonic-gate  *		mdi_phci node
65317c478bd9Sstevel@tonic-gate  */
65327c478bd9Sstevel@tonic-gate void *
65337c478bd9Sstevel@tonic-gate mdi_phci_get_vhci_private(dev_info_t *dip)
65347c478bd9Sstevel@tonic-gate {
65357c478bd9Sstevel@tonic-gate 	ASSERT(mdi_component_is_phci(dip, NULL) == MDI_SUCCESS);
65367c478bd9Sstevel@tonic-gate 	if (mdi_component_is_phci(dip, NULL) == MDI_SUCCESS) {
65377c478bd9Sstevel@tonic-gate 		mdi_phci_t	*ph;
65387c478bd9Sstevel@tonic-gate 		ph = i_devi_get_phci(dip);
65397c478bd9Sstevel@tonic-gate 		return (ph->ph_vprivate);
65407c478bd9Sstevel@tonic-gate 	}
65417c478bd9Sstevel@tonic-gate 	return (NULL);
65427c478bd9Sstevel@tonic-gate }
65437c478bd9Sstevel@tonic-gate 
65447c478bd9Sstevel@tonic-gate /*
65457c478bd9Sstevel@tonic-gate  * mdi_phci_set_vhci_private():
65467c478bd9Sstevel@tonic-gate  *		Set the vhci private information in the mdi_phci node
65477c478bd9Sstevel@tonic-gate  */
65487c478bd9Sstevel@tonic-gate void
65497c478bd9Sstevel@tonic-gate mdi_phci_set_vhci_private(dev_info_t *dip, void *priv)
65507c478bd9Sstevel@tonic-gate {
65517c478bd9Sstevel@tonic-gate 	ASSERT(mdi_component_is_phci(dip, NULL) == MDI_SUCCESS);
65527c478bd9Sstevel@tonic-gate 	if (mdi_component_is_phci(dip, NULL) == MDI_SUCCESS) {
65537c478bd9Sstevel@tonic-gate 		mdi_phci_t	*ph;
65547c478bd9Sstevel@tonic-gate 		ph = i_devi_get_phci(dip);
65557c478bd9Sstevel@tonic-gate 		ph->ph_vprivate = priv;
65567c478bd9Sstevel@tonic-gate 	}
65577c478bd9Sstevel@tonic-gate }
65583c34adc5Sramat 
65593c34adc5Sramat /*
65603c34adc5Sramat  * List of vhci class names:
65613c34adc5Sramat  * A vhci class name must be in this list only if the corresponding vhci
65623c34adc5Sramat  * driver intends to use the mdi provided bus config implementation
65633c34adc5Sramat  * (i.e., mdi_vhci_bus_config()).
65643c34adc5Sramat  */
65653c34adc5Sramat static char *vhci_class_list[] = { MDI_HCI_CLASS_SCSI, MDI_HCI_CLASS_IB };
65663c34adc5Sramat #define	N_VHCI_CLASSES	(sizeof (vhci_class_list) / sizeof (char *))
65673c34adc5Sramat 
65683c34adc5Sramat /*
65693c34adc5Sramat  * Built-in list of phci drivers for every vhci class.
65703c34adc5Sramat  * All phci drivers expect iscsi have root device support.
65713c34adc5Sramat  */
65723c34adc5Sramat static mdi_phci_driver_info_t scsi_phci_driver_list[] = {
65733c34adc5Sramat 	{ "fp", 1 },
65743c34adc5Sramat 	{ "iscsi", 0 },
65753c34adc5Sramat 	{ "ibsrp", 1 }
65763c34adc5Sramat 	};
65773c34adc5Sramat 
65783c34adc5Sramat static mdi_phci_driver_info_t ib_phci_driver_list[] = { "tavor", 1 };
65793c34adc5Sramat 
65803c34adc5Sramat /*
65813c34adc5Sramat  * During boot time, the on-disk vhci cache for every vhci class is read
65823c34adc5Sramat  * in the form of an nvlist and stored here.
65833c34adc5Sramat  */
65843c34adc5Sramat static nvlist_t *vhcache_nvl[N_VHCI_CLASSES];
65853c34adc5Sramat 
65863c34adc5Sramat /* nvpair names in vhci cache nvlist */
65873c34adc5Sramat #define	MDI_VHCI_CACHE_VERSION	1
65883c34adc5Sramat #define	MDI_NVPNAME_VERSION	"version"
65893c34adc5Sramat #define	MDI_NVPNAME_PHCIS	"phcis"
65903c34adc5Sramat #define	MDI_NVPNAME_CTADDRMAP	"clientaddrmap"
65913c34adc5Sramat 
65923c34adc5Sramat /*
65933c34adc5Sramat  * Given vhci class name, return its on-disk vhci cache filename.
65943c34adc5Sramat  * Memory for the returned filename which includes the full path is allocated
65953c34adc5Sramat  * by this function.
65963c34adc5Sramat  */
65973c34adc5Sramat static char *
65983c34adc5Sramat vhclass2vhcache_filename(char *vhclass)
65993c34adc5Sramat {
66003c34adc5Sramat 	char *filename;
66013c34adc5Sramat 	int len;
66023c34adc5Sramat 	static char *fmt = "/etc/devices/mdi_%s_cache";
66033c34adc5Sramat 
66043c34adc5Sramat 	/*
66053c34adc5Sramat 	 * fmt contains the on-disk vhci cache file name format;
66063c34adc5Sramat 	 * for scsi_vhci the filename is "/etc/devices/mdi_scsi_vhci_cache".
66073c34adc5Sramat 	 */
66083c34adc5Sramat 
66093c34adc5Sramat 	/* the -1 below is to account for "%s" in the format string */
66103c34adc5Sramat 	len = strlen(fmt) + strlen(vhclass) - 1;
66113c34adc5Sramat 	filename = kmem_alloc(len, KM_SLEEP);
66123c34adc5Sramat 	(void) snprintf(filename, len, fmt, vhclass);
66133c34adc5Sramat 	ASSERT(len == (strlen(filename) + 1));
66143c34adc5Sramat 	return (filename);
66153c34adc5Sramat }
66163c34adc5Sramat 
66173c34adc5Sramat /*
66183c34adc5Sramat  * initialize the vhci cache related data structures and read the on-disk
66193c34adc5Sramat  * vhci cached data into memory.
66203c34adc5Sramat  */
66213c34adc5Sramat static void
66223c34adc5Sramat setup_vhci_cache(mdi_vhci_t *vh)
66233c34adc5Sramat {
66243c34adc5Sramat 	mdi_vhci_config_t *vhc;
66253c34adc5Sramat 	mdi_vhci_cache_t *vhcache;
66263c34adc5Sramat 	int i;
66273c34adc5Sramat 	nvlist_t *nvl = NULL;
66283c34adc5Sramat 
66293c34adc5Sramat 	vhc = kmem_zalloc(sizeof (mdi_vhci_config_t), KM_SLEEP);
66303c34adc5Sramat 	vh->vh_config = vhc;
66313c34adc5Sramat 	vhcache = &vhc->vhc_vhcache;
66323c34adc5Sramat 
66333c34adc5Sramat 	vhc->vhc_vhcache_filename = vhclass2vhcache_filename(vh->vh_class);
66343c34adc5Sramat 
66353c34adc5Sramat 	mutex_init(&vhc->vhc_lock, NULL, MUTEX_DEFAULT, NULL);
66363c34adc5Sramat 	cv_init(&vhc->vhc_cv, NULL, CV_DRIVER, NULL);
66373c34adc5Sramat 
66383c34adc5Sramat 	rw_init(&vhcache->vhcache_lock, NULL, RW_DRIVER, NULL);
66393c34adc5Sramat 
66403c34adc5Sramat 	/*
66413c34adc5Sramat 	 * Create string hash; same as mod_hash_create_strhash() except that
66423c34adc5Sramat 	 * we use NULL key destructor.
66433c34adc5Sramat 	 */
66443c34adc5Sramat 	vhcache->vhcache_client_hash = mod_hash_create_extended(vh->vh_class,
66453c34adc5Sramat 	    mdi_bus_config_cache_hash_size,
66463c34adc5Sramat 	    mod_hash_null_keydtor, mod_hash_null_valdtor,
66473c34adc5Sramat 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
66483c34adc5Sramat 
66493c34adc5Sramat 	setup_phci_driver_list(vh);
66503c34adc5Sramat 
66513c34adc5Sramat 	/*
66523c34adc5Sramat 	 * The on-disk vhci cache is read during booting prior to the
66533c34adc5Sramat 	 * lights-out period by mdi_read_devices_files().
66543c34adc5Sramat 	 */
66553c34adc5Sramat 	for (i = 0; i < N_VHCI_CLASSES; i++) {
66563c34adc5Sramat 		if (strcmp(vhci_class_list[i], vh->vh_class) == 0) {
66573c34adc5Sramat 			nvl = vhcache_nvl[i];
66583c34adc5Sramat 			vhcache_nvl[i] = NULL;
66593c34adc5Sramat 			break;
66603c34adc5Sramat 		}
66613c34adc5Sramat 	}
66623c34adc5Sramat 
66633c34adc5Sramat 	/*
66643c34adc5Sramat 	 * this is to cover the case of some one manually causing unloading
66653c34adc5Sramat 	 * (or detaching) and reloading (or attaching) of a vhci driver.
66663c34adc5Sramat 	 */
66673c34adc5Sramat 	if (nvl == NULL && modrootloaded)
66683c34adc5Sramat 		nvl = read_on_disk_vhci_cache(vh->vh_class);
66693c34adc5Sramat 
66703c34adc5Sramat 	if (nvl != NULL) {
66713c34adc5Sramat 		rw_enter(&vhcache->vhcache_lock, RW_WRITER);
66723c34adc5Sramat 		if (mainnvl_to_vhcache(vhcache, nvl) == MDI_SUCCESS)
66733c34adc5Sramat 			vhcache->vhcache_flags |= MDI_VHCI_CACHE_SETUP_DONE;
66743c34adc5Sramat 		else  {
66753c34adc5Sramat 			cmn_err(CE_WARN,
66763c34adc5Sramat 			    "%s: data file corrupted, will recreate\n",
66773c34adc5Sramat 			    vhc->vhc_vhcache_filename);
66783c34adc5Sramat 		}
66793c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
66803c34adc5Sramat 		nvlist_free(nvl);
66813c34adc5Sramat 	}
66823c34adc5Sramat 
66833c34adc5Sramat 	vhc->vhc_cbid = callb_add(stop_vhcache_flush_thread, vhc,
66843c34adc5Sramat 	    CB_CL_UADMIN_PRE_VFS, "mdi_vhcache_flush");
668567e56d35Sramat 
668667e56d35Sramat 	vhc->vhc_path_discovery_boot = mdi_path_discovery_boot;
668767e56d35Sramat 	vhc->vhc_path_discovery_postboot = mdi_path_discovery_postboot;
66883c34adc5Sramat }
66893c34adc5Sramat 
66903c34adc5Sramat /*
66913c34adc5Sramat  * free all vhci cache related resources
66923c34adc5Sramat  */
66933c34adc5Sramat static int
66943c34adc5Sramat destroy_vhci_cache(mdi_vhci_t *vh)
66953c34adc5Sramat {
66963c34adc5Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
66973c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
66983c34adc5Sramat 	mdi_vhcache_phci_t *cphci, *cphci_next;
66993c34adc5Sramat 	mdi_vhcache_client_t *cct, *cct_next;
67003c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi, *cpi_next;
67013c34adc5Sramat 
67023c34adc5Sramat 	if (stop_vhcache_async_threads(vhc) != MDI_SUCCESS)
67033c34adc5Sramat 		return (MDI_FAILURE);
67043c34adc5Sramat 
67053c34adc5Sramat 	kmem_free(vhc->vhc_vhcache_filename,
67063c34adc5Sramat 	    strlen(vhc->vhc_vhcache_filename) + 1);
67073c34adc5Sramat 
67083c34adc5Sramat 	if (vhc->vhc_phci_driver_list)
67093c34adc5Sramat 		free_phci_driver_list(vhc);
67103c34adc5Sramat 
67113c34adc5Sramat 	mod_hash_destroy_strhash(vhcache->vhcache_client_hash);
67123c34adc5Sramat 
67133c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
67143c34adc5Sramat 	    cphci = cphci_next) {
67153c34adc5Sramat 		cphci_next = cphci->cphci_next;
67163c34adc5Sramat 		free_vhcache_phci(cphci);
67173c34adc5Sramat 	}
67183c34adc5Sramat 
67193c34adc5Sramat 	for (cct = vhcache->vhcache_client_head; cct != NULL; cct = cct_next) {
67203c34adc5Sramat 		cct_next = cct->cct_next;
67213c34adc5Sramat 		for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi_next) {
67223c34adc5Sramat 			cpi_next = cpi->cpi_next;
67233c34adc5Sramat 			free_vhcache_pathinfo(cpi);
67243c34adc5Sramat 		}
67253c34adc5Sramat 		free_vhcache_client(cct);
67263c34adc5Sramat 	}
67273c34adc5Sramat 
67283c34adc5Sramat 	rw_destroy(&vhcache->vhcache_lock);
67293c34adc5Sramat 
67303c34adc5Sramat 	mutex_destroy(&vhc->vhc_lock);
67313c34adc5Sramat 	cv_destroy(&vhc->vhc_cv);
67323c34adc5Sramat 	kmem_free(vhc, sizeof (mdi_vhci_config_t));
67333c34adc5Sramat 	return (MDI_SUCCESS);
67343c34adc5Sramat }
67353c34adc5Sramat 
67363c34adc5Sramat /*
67373c34adc5Sramat  * Setup the list of phci drivers associated with the specified vhci class.
67383c34adc5Sramat  * MDI uses this information to rebuild bus config cache if in case the
67393c34adc5Sramat  * cache is not available or corrupted.
67403c34adc5Sramat  */
67413c34adc5Sramat static void
67423c34adc5Sramat setup_phci_driver_list(mdi_vhci_t *vh)
67433c34adc5Sramat {
67443c34adc5Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
67453c34adc5Sramat 	mdi_phci_driver_info_t *driver_list;
67463c34adc5Sramat 	char **driver_list1;
67473c34adc5Sramat 	uint_t ndrivers, ndrivers1;
67483c34adc5Sramat 	int i, j;
67493c34adc5Sramat 
67503c34adc5Sramat 	if (strcmp(vh->vh_class, MDI_HCI_CLASS_SCSI) == 0) {
67513c34adc5Sramat 		driver_list = scsi_phci_driver_list;
67523c34adc5Sramat 		ndrivers = sizeof (scsi_phci_driver_list) /
67533c34adc5Sramat 		    sizeof (mdi_phci_driver_info_t);
67543c34adc5Sramat 	} else if (strcmp(vh->vh_class, MDI_HCI_CLASS_IB) == 0) {
67553c34adc5Sramat 		driver_list = ib_phci_driver_list;
67563c34adc5Sramat 		ndrivers = sizeof (ib_phci_driver_list) /
67573c34adc5Sramat 		    sizeof (mdi_phci_driver_info_t);
67583c34adc5Sramat 	} else {
67593c34adc5Sramat 		driver_list = NULL;
67603c34adc5Sramat 		ndrivers = 0;
67613c34adc5Sramat 	}
67623c34adc5Sramat 
67633c34adc5Sramat 	/*
67643c34adc5Sramat 	 * The driver.conf file of a vhci driver can specify additional
67653c34adc5Sramat 	 * phci drivers using a project private "phci-drivers" property.
67663c34adc5Sramat 	 */
67673c34adc5Sramat 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, vh->vh_dip,
67683c34adc5Sramat 	    DDI_PROP_DONTPASS, "phci-drivers", &driver_list1,
67693c34adc5Sramat 	    &ndrivers1) != DDI_PROP_SUCCESS)
67703c34adc5Sramat 		ndrivers1 = 0;
67713c34adc5Sramat 
67723c34adc5Sramat 	vhc->vhc_nphci_drivers = ndrivers + ndrivers1;
67733c34adc5Sramat 	if (vhc->vhc_nphci_drivers == 0)
67743c34adc5Sramat 		return;
67753c34adc5Sramat 
67763c34adc5Sramat 	vhc->vhc_phci_driver_list = kmem_alloc(
67773c34adc5Sramat 	    sizeof (mdi_phci_driver_info_t) * vhc->vhc_nphci_drivers, KM_SLEEP);
67783c34adc5Sramat 
67793c34adc5Sramat 	for (i = 0; i < ndrivers; i++) {
67803c34adc5Sramat 		vhc->vhc_phci_driver_list[i].phdriver_name =
67813c34adc5Sramat 		    i_ddi_strdup(driver_list[i].phdriver_name, KM_SLEEP);
67823c34adc5Sramat 		vhc->vhc_phci_driver_list[i].phdriver_root_support =
67833c34adc5Sramat 		    driver_list[i].phdriver_root_support;
67843c34adc5Sramat 	}
67853c34adc5Sramat 
67863c34adc5Sramat 	for (j = 0; j < ndrivers1; j++, i++) {
67873c34adc5Sramat 		vhc->vhc_phci_driver_list[i].phdriver_name =
67883c34adc5Sramat 		    i_ddi_strdup(driver_list1[j], KM_SLEEP);
67893c34adc5Sramat 		vhc->vhc_phci_driver_list[i].phdriver_root_support = 1;
67903c34adc5Sramat 	}
67913c34adc5Sramat 
67923c34adc5Sramat 	if (ndrivers1)
67933c34adc5Sramat 		ddi_prop_free(driver_list1);
67943c34adc5Sramat }
67953c34adc5Sramat 
67963c34adc5Sramat /*
67973c34adc5Sramat  * Free the memory allocated for the phci driver list
67983c34adc5Sramat  */
67993c34adc5Sramat static void
68003c34adc5Sramat free_phci_driver_list(mdi_vhci_config_t *vhc)
68013c34adc5Sramat {
68023c34adc5Sramat 	int i;
68033c34adc5Sramat 
68043c34adc5Sramat 	if (vhc->vhc_phci_driver_list == NULL)
68053c34adc5Sramat 		return;
68063c34adc5Sramat 
68073c34adc5Sramat 	for (i = 0; i < vhc->vhc_nphci_drivers; i++) {
68083c34adc5Sramat 		kmem_free(vhc->vhc_phci_driver_list[i].phdriver_name,
68093c34adc5Sramat 		    strlen(vhc->vhc_phci_driver_list[i].phdriver_name) + 1);
68103c34adc5Sramat 	}
68113c34adc5Sramat 
68123c34adc5Sramat 	kmem_free(vhc->vhc_phci_driver_list,
68133c34adc5Sramat 	    sizeof (mdi_phci_driver_info_t) * vhc->vhc_nphci_drivers);
68143c34adc5Sramat }
68153c34adc5Sramat 
68163c34adc5Sramat /*
68173c34adc5Sramat  * Stop all vhci cache related async threads and free their resources.
68183c34adc5Sramat  */
68193c34adc5Sramat static int
68203c34adc5Sramat stop_vhcache_async_threads(mdi_vhci_config_t *vhc)
68213c34adc5Sramat {
68223c34adc5Sramat 	mdi_async_client_config_t *acc, *acc_next;
68233c34adc5Sramat 
68243c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
68253c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_EXIT;
68263c34adc5Sramat 	ASSERT(vhc->vhc_acc_thrcount >= 0);
68273c34adc5Sramat 	cv_broadcast(&vhc->vhc_cv);
68283c34adc5Sramat 
68293c34adc5Sramat 	while ((vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) ||
68303c34adc5Sramat 	    vhc->vhc_acc_thrcount != 0) {
68313c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
68323c34adc5Sramat 		delay(1);
68333c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
68343c34adc5Sramat 	}
68353c34adc5Sramat 
68363c34adc5Sramat 	vhc->vhc_flags &= ~MDI_VHC_EXIT;
68373c34adc5Sramat 
68383c34adc5Sramat 	for (acc = vhc->vhc_acc_list_head; acc != NULL; acc = acc_next) {
68393c34adc5Sramat 		acc_next = acc->acc_next;
68403c34adc5Sramat 		free_async_client_config(acc);
68413c34adc5Sramat 	}
68423c34adc5Sramat 	vhc->vhc_acc_list_head = NULL;
68433c34adc5Sramat 	vhc->vhc_acc_list_tail = NULL;
68443c34adc5Sramat 	vhc->vhc_acc_count = 0;
68453c34adc5Sramat 
68463c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) {
68473c34adc5Sramat 		vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
68483c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
68493c34adc5Sramat 		if (flush_vhcache(vhc, 0) != MDI_SUCCESS) {
68503c34adc5Sramat 			vhcache_dirty(vhc);
68513c34adc5Sramat 			return (MDI_FAILURE);
68523c34adc5Sramat 		}
68533c34adc5Sramat 	} else
68543c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
68553c34adc5Sramat 
68563c34adc5Sramat 	if (callb_delete(vhc->vhc_cbid) != 0)
68573c34adc5Sramat 		return (MDI_FAILURE);
68583c34adc5Sramat 
68593c34adc5Sramat 	return (MDI_SUCCESS);
68603c34adc5Sramat }
68613c34adc5Sramat 
68623c34adc5Sramat /*
68633c34adc5Sramat  * Stop vhci cache flush thread
68643c34adc5Sramat  */
68653c34adc5Sramat /* ARGSUSED */
68663c34adc5Sramat static boolean_t
68673c34adc5Sramat stop_vhcache_flush_thread(void *arg, int code)
68683c34adc5Sramat {
68693c34adc5Sramat 	mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
68703c34adc5Sramat 
68713c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
68723c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_EXIT;
68733c34adc5Sramat 	cv_broadcast(&vhc->vhc_cv);
68743c34adc5Sramat 
68753c34adc5Sramat 	while (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) {
68763c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
68773c34adc5Sramat 		delay(1);
68783c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
68793c34adc5Sramat 	}
68803c34adc5Sramat 
68813c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) {
68823c34adc5Sramat 		vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
68833c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
68843c34adc5Sramat 		(void) flush_vhcache(vhc, 1);
68853c34adc5Sramat 	} else
68863c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
68873c34adc5Sramat 
68883c34adc5Sramat 	return (B_TRUE);
68893c34adc5Sramat }
68903c34adc5Sramat 
68913c34adc5Sramat /*
68923c34adc5Sramat  * Enqueue the vhcache phci (cphci) at the tail of the list
68933c34adc5Sramat  */
68943c34adc5Sramat static void
68953c34adc5Sramat enqueue_vhcache_phci(mdi_vhci_cache_t *vhcache, mdi_vhcache_phci_t *cphci)
68963c34adc5Sramat {
68973c34adc5Sramat 	cphci->cphci_next = NULL;
68983c34adc5Sramat 	if (vhcache->vhcache_phci_head == NULL)
68993c34adc5Sramat 		vhcache->vhcache_phci_head = cphci;
69003c34adc5Sramat 	else
69013c34adc5Sramat 		vhcache->vhcache_phci_tail->cphci_next = cphci;
69023c34adc5Sramat 	vhcache->vhcache_phci_tail = cphci;
69033c34adc5Sramat }
69043c34adc5Sramat 
69053c34adc5Sramat /*
69063c34adc5Sramat  * Enqueue the vhcache pathinfo (cpi) at the tail of the list
69073c34adc5Sramat  */
69083c34adc5Sramat static void
69093c34adc5Sramat enqueue_tail_vhcache_pathinfo(mdi_vhcache_client_t *cct,
69103c34adc5Sramat     mdi_vhcache_pathinfo_t *cpi)
69113c34adc5Sramat {
69123c34adc5Sramat 	cpi->cpi_next = NULL;
69133c34adc5Sramat 	if (cct->cct_cpi_head == NULL)
69143c34adc5Sramat 		cct->cct_cpi_head = cpi;
69153c34adc5Sramat 	else
69163c34adc5Sramat 		cct->cct_cpi_tail->cpi_next = cpi;
69173c34adc5Sramat 	cct->cct_cpi_tail = cpi;
69183c34adc5Sramat }
69193c34adc5Sramat 
69203c34adc5Sramat /*
69213c34adc5Sramat  * Enqueue the vhcache pathinfo (cpi) at the correct location in the
69223c34adc5Sramat  * ordered list. All cpis which do not have MDI_CPI_HINT_PATH_DOES_NOT_EXIST
69233c34adc5Sramat  * flag set come at the beginning of the list. All cpis which have this
69243c34adc5Sramat  * flag set come at the end of the list.
69253c34adc5Sramat  */
69263c34adc5Sramat static void
69273c34adc5Sramat enqueue_vhcache_pathinfo(mdi_vhcache_client_t *cct,
69283c34adc5Sramat     mdi_vhcache_pathinfo_t *newcpi)
69293c34adc5Sramat {
69303c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi, *prev_cpi;
69313c34adc5Sramat 
69323c34adc5Sramat 	if (cct->cct_cpi_head == NULL ||
69333c34adc5Sramat 	    (newcpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST))
69343c34adc5Sramat 		enqueue_tail_vhcache_pathinfo(cct, newcpi);
69353c34adc5Sramat 	else {
69363c34adc5Sramat 		for (cpi = cct->cct_cpi_head, prev_cpi = NULL; cpi != NULL &&
69373c34adc5Sramat 		    !(cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST);
69383c34adc5Sramat 		    prev_cpi = cpi, cpi = cpi->cpi_next)
69393c34adc5Sramat 			;
69403c34adc5Sramat 
69413c34adc5Sramat 		if (prev_cpi == NULL)
69423c34adc5Sramat 			cct->cct_cpi_head = newcpi;
69433c34adc5Sramat 		else
69443c34adc5Sramat 			prev_cpi->cpi_next = newcpi;
69453c34adc5Sramat 
69463c34adc5Sramat 		newcpi->cpi_next = cpi;
69473c34adc5Sramat 
69483c34adc5Sramat 		if (cpi == NULL)
69493c34adc5Sramat 			cct->cct_cpi_tail = newcpi;
69503c34adc5Sramat 	}
69513c34adc5Sramat }
69523c34adc5Sramat 
69533c34adc5Sramat /*
69543c34adc5Sramat  * Enqueue the vhcache client (cct) at the tail of the list
69553c34adc5Sramat  */
69563c34adc5Sramat static void
69573c34adc5Sramat enqueue_vhcache_client(mdi_vhci_cache_t *vhcache,
69583c34adc5Sramat     mdi_vhcache_client_t *cct)
69593c34adc5Sramat {
69603c34adc5Sramat 	cct->cct_next = NULL;
69613c34adc5Sramat 	if (vhcache->vhcache_client_head == NULL)
69623c34adc5Sramat 		vhcache->vhcache_client_head = cct;
69633c34adc5Sramat 	else
69643c34adc5Sramat 		vhcache->vhcache_client_tail->cct_next = cct;
69653c34adc5Sramat 	vhcache->vhcache_client_tail = cct;
69663c34adc5Sramat }
69673c34adc5Sramat 
69683c34adc5Sramat static void
69693c34adc5Sramat free_string_array(char **str, int nelem)
69703c34adc5Sramat {
69713c34adc5Sramat 	int i;
69723c34adc5Sramat 
69733c34adc5Sramat 	if (str) {
69743c34adc5Sramat 		for (i = 0; i < nelem; i++) {
69753c34adc5Sramat 			if (str[i])
69763c34adc5Sramat 				kmem_free(str[i], strlen(str[i]) + 1);
69773c34adc5Sramat 		}
69783c34adc5Sramat 		kmem_free(str, sizeof (char *) * nelem);
69793c34adc5Sramat 	}
69803c34adc5Sramat }
69813c34adc5Sramat 
69823c34adc5Sramat static void
69833c34adc5Sramat free_vhcache_phci(mdi_vhcache_phci_t *cphci)
69843c34adc5Sramat {
69853c34adc5Sramat 	kmem_free(cphci->cphci_path, strlen(cphci->cphci_path) + 1);
69863c34adc5Sramat 	kmem_free(cphci, sizeof (*cphci));
69873c34adc5Sramat }
69883c34adc5Sramat 
69893c34adc5Sramat static void
69903c34adc5Sramat free_vhcache_pathinfo(mdi_vhcache_pathinfo_t *cpi)
69913c34adc5Sramat {
69923c34adc5Sramat 	kmem_free(cpi->cpi_addr, strlen(cpi->cpi_addr) + 1);
69933c34adc5Sramat 	kmem_free(cpi, sizeof (*cpi));
69943c34adc5Sramat }
69953c34adc5Sramat 
69963c34adc5Sramat static void
69973c34adc5Sramat free_vhcache_client(mdi_vhcache_client_t *cct)
69983c34adc5Sramat {
69993c34adc5Sramat 	kmem_free(cct->cct_name_addr, strlen(cct->cct_name_addr) + 1);
70003c34adc5Sramat 	kmem_free(cct, sizeof (*cct));
70013c34adc5Sramat }
70023c34adc5Sramat 
70033c34adc5Sramat static char *
70043c34adc5Sramat vhcache_mknameaddr(char *ct_name, char *ct_addr, int *ret_len)
70053c34adc5Sramat {
70063c34adc5Sramat 	char *name_addr;
70073c34adc5Sramat 	int len;
70083c34adc5Sramat 
70093c34adc5Sramat 	len = strlen(ct_name) + strlen(ct_addr) + 2;
70103c34adc5Sramat 	name_addr = kmem_alloc(len, KM_SLEEP);
70113c34adc5Sramat 	(void) snprintf(name_addr, len, "%s@%s", ct_name, ct_addr);
70123c34adc5Sramat 
70133c34adc5Sramat 	if (ret_len)
70143c34adc5Sramat 		*ret_len = len;
70153c34adc5Sramat 	return (name_addr);
70163c34adc5Sramat }
70173c34adc5Sramat 
70183c34adc5Sramat /*
70193c34adc5Sramat  * Copy the contents of paddrnvl to vhci cache.
70203c34adc5Sramat  * paddrnvl nvlist contains path information for a vhci client.
70213c34adc5Sramat  * See the comment in mainnvl_to_vhcache() for the format of this nvlist.
70223c34adc5Sramat  */
70233c34adc5Sramat static void
70243c34adc5Sramat paddrnvl_to_vhcache(nvlist_t *nvl, mdi_vhcache_phci_t *cphci_list[],
70253c34adc5Sramat     mdi_vhcache_client_t *cct)
70263c34adc5Sramat {
70273c34adc5Sramat 	nvpair_t *nvp = NULL;
70283c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
70293c34adc5Sramat 	uint_t nelem;
70303c34adc5Sramat 	uint32_t *val;
70313c34adc5Sramat 
70323c34adc5Sramat 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
70333c34adc5Sramat 		ASSERT(nvpair_type(nvp) == DATA_TYPE_UINT32_ARRAY);
70343c34adc5Sramat 		cpi = kmem_zalloc(sizeof (*cpi), KM_SLEEP);
70353c34adc5Sramat 		cpi->cpi_addr = i_ddi_strdup(nvpair_name(nvp), KM_SLEEP);
70363c34adc5Sramat 		(void) nvpair_value_uint32_array(nvp, &val, &nelem);
70373c34adc5Sramat 		ASSERT(nelem == 2);
70383c34adc5Sramat 		cpi->cpi_cphci = cphci_list[val[0]];
70393c34adc5Sramat 		cpi->cpi_flags = val[1];
70403c34adc5Sramat 		enqueue_tail_vhcache_pathinfo(cct, cpi);
70413c34adc5Sramat 	}
70423c34adc5Sramat }
70433c34adc5Sramat 
70443c34adc5Sramat /*
70453c34adc5Sramat  * Copy the contents of caddrmapnvl to vhci cache.
70463c34adc5Sramat  * caddrmapnvl nvlist contains vhci client address to phci client address
70473c34adc5Sramat  * mappings. See the comment in mainnvl_to_vhcache() for the format of
70483c34adc5Sramat  * this nvlist.
70493c34adc5Sramat  */
70503c34adc5Sramat static void
70513c34adc5Sramat caddrmapnvl_to_vhcache(mdi_vhci_cache_t *vhcache, nvlist_t *nvl,
70523c34adc5Sramat     mdi_vhcache_phci_t *cphci_list[])
70533c34adc5Sramat {
70543c34adc5Sramat 	nvpair_t *nvp = NULL;
70553c34adc5Sramat 	nvlist_t *paddrnvl;
70563c34adc5Sramat 	mdi_vhcache_client_t *cct;
70573c34adc5Sramat 
70583c34adc5Sramat 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
70593c34adc5Sramat 		ASSERT(nvpair_type(nvp) == DATA_TYPE_NVLIST);
70603c34adc5Sramat 		cct = kmem_zalloc(sizeof (*cct), KM_SLEEP);
70613c34adc5Sramat 		cct->cct_name_addr = i_ddi_strdup(nvpair_name(nvp), KM_SLEEP);
70623c34adc5Sramat 		(void) nvpair_value_nvlist(nvp, &paddrnvl);
70633c34adc5Sramat 		paddrnvl_to_vhcache(paddrnvl, cphci_list, cct);
70643c34adc5Sramat 		/* the client must contain at least one path */
70653c34adc5Sramat 		ASSERT(cct->cct_cpi_head != NULL);
70663c34adc5Sramat 
70673c34adc5Sramat 		enqueue_vhcache_client(vhcache, cct);
70683c34adc5Sramat 		(void) mod_hash_insert(vhcache->vhcache_client_hash,
70693c34adc5Sramat 		    (mod_hash_key_t)cct->cct_name_addr, (mod_hash_val_t)cct);
70703c34adc5Sramat 	}
70713c34adc5Sramat }
70723c34adc5Sramat 
70733c34adc5Sramat /*
70743c34adc5Sramat  * Copy the contents of the main nvlist to vhci cache.
70753c34adc5Sramat  *
70763c34adc5Sramat  * VHCI busconfig cached data is stored in the form of a nvlist on the disk.
70773c34adc5Sramat  * The nvlist contains the mappings between the vhci client addresses and
70783c34adc5Sramat  * their corresponding phci client addresses.
70793c34adc5Sramat  *
70803c34adc5Sramat  * The structure of the nvlist is as follows:
70813c34adc5Sramat  *
70823c34adc5Sramat  * Main nvlist:
70833c34adc5Sramat  *	NAME		TYPE		DATA
70843c34adc5Sramat  *	version		int32		version number
70853c34adc5Sramat  *	phcis		string array	array of phci paths
70863c34adc5Sramat  *	clientaddrmap	nvlist_t	c2paddrs_nvl (see below)
70873c34adc5Sramat  *
70883c34adc5Sramat  * structure of c2paddrs_nvl:
70893c34adc5Sramat  *	NAME		TYPE		DATA
70903c34adc5Sramat  *	caddr1		nvlist_t	paddrs_nvl1
70913c34adc5Sramat  *	caddr2		nvlist_t	paddrs_nvl2
70923c34adc5Sramat  *	...
70933c34adc5Sramat  * where caddr1, caddr2, ... are vhci client name and addresses in the
70943c34adc5Sramat  * form of "<clientname>@<clientaddress>".
70953c34adc5Sramat  * (for example: "ssd@2000002037cd9f72");
70963c34adc5Sramat  * paddrs_nvl1, paddrs_nvl2, .. are nvlists that contain path information.
70973c34adc5Sramat  *
70983c34adc5Sramat  * structure of paddrs_nvl:
70993c34adc5Sramat  *	NAME		TYPE		DATA
71003c34adc5Sramat  *	pi_addr1	uint32_array	(phci-id, cpi_flags)
71013c34adc5Sramat  *	pi_addr2	uint32_array	(phci-id, cpi_flags)
71023c34adc5Sramat  *	...
71033c34adc5Sramat  * where pi_addr1, pi_addr2, ... are bus specific addresses of pathinfo nodes
71043c34adc5Sramat  * (so called pi_addrs, for example: "w2100002037cd9f72,0");
71053c34adc5Sramat  * phci-ids are integers that identify PHCIs to which the
71063c34adc5Sramat  * the bus specific address belongs to. These integers are used as an index
71073c34adc5Sramat  * into to the phcis string array in the main nvlist to get the PHCI path.
71083c34adc5Sramat  */
71093c34adc5Sramat static int
71103c34adc5Sramat mainnvl_to_vhcache(mdi_vhci_cache_t *vhcache, nvlist_t *nvl)
71113c34adc5Sramat {
71123c34adc5Sramat 	char **phcis, **phci_namep;
71133c34adc5Sramat 	uint_t nphcis;
71143c34adc5Sramat 	mdi_vhcache_phci_t *cphci, **cphci_list;
71153c34adc5Sramat 	nvlist_t *caddrmapnvl;
71163c34adc5Sramat 	int32_t ver;
71173c34adc5Sramat 	int i;
71183c34adc5Sramat 	size_t cphci_list_size;
71193c34adc5Sramat 
71203c34adc5Sramat 	ASSERT(RW_WRITE_HELD(&vhcache->vhcache_lock));
71213c34adc5Sramat 
71223c34adc5Sramat 	if (nvlist_lookup_int32(nvl, MDI_NVPNAME_VERSION, &ver) != 0 ||
71233c34adc5Sramat 	    ver != MDI_VHCI_CACHE_VERSION)
71243c34adc5Sramat 		return (MDI_FAILURE);
71253c34adc5Sramat 
71263c34adc5Sramat 	if (nvlist_lookup_string_array(nvl, MDI_NVPNAME_PHCIS, &phcis,
71273c34adc5Sramat 	    &nphcis) != 0)
71283c34adc5Sramat 		return (MDI_SUCCESS);
71293c34adc5Sramat 
71303c34adc5Sramat 	ASSERT(nphcis > 0);
71313c34adc5Sramat 
71323c34adc5Sramat 	cphci_list_size = sizeof (mdi_vhcache_phci_t *) * nphcis;
71333c34adc5Sramat 	cphci_list = kmem_alloc(cphci_list_size, KM_SLEEP);
71343c34adc5Sramat 	for (i = 0, phci_namep = phcis; i < nphcis; i++, phci_namep++) {
71353c34adc5Sramat 		cphci = kmem_zalloc(sizeof (mdi_vhcache_phci_t), KM_SLEEP);
71363c34adc5Sramat 		cphci->cphci_path = i_ddi_strdup(*phci_namep, KM_SLEEP);
71373c34adc5Sramat 		enqueue_vhcache_phci(vhcache, cphci);
71383c34adc5Sramat 		cphci_list[i] = cphci;
71393c34adc5Sramat 	}
71403c34adc5Sramat 
71413c34adc5Sramat 	ASSERT(vhcache->vhcache_phci_head != NULL);
71423c34adc5Sramat 
71433c34adc5Sramat 	if (nvlist_lookup_nvlist(nvl, MDI_NVPNAME_CTADDRMAP, &caddrmapnvl) == 0)
71443c34adc5Sramat 		caddrmapnvl_to_vhcache(vhcache, caddrmapnvl, cphci_list);
71453c34adc5Sramat 
71463c34adc5Sramat 	kmem_free(cphci_list, cphci_list_size);
71473c34adc5Sramat 	return (MDI_SUCCESS);
71483c34adc5Sramat }
71493c34adc5Sramat 
71503c34adc5Sramat /*
71513c34adc5Sramat  * Build paddrnvl for the specified client using the information in the
71523c34adc5Sramat  * vhci cache and add it to the caddrmapnnvl.
71533c34adc5Sramat  * Returns 0 on success, errno on failure.
71543c34adc5Sramat  */
71553c34adc5Sramat static int
71563c34adc5Sramat vhcache_to_paddrnvl(mdi_vhci_cache_t *vhcache, mdi_vhcache_client_t *cct,
71573c34adc5Sramat     nvlist_t *caddrmapnvl)
71583c34adc5Sramat {
71593c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
71603c34adc5Sramat 	nvlist_t *nvl;
71613c34adc5Sramat 	int err;
71623c34adc5Sramat 	uint32_t val[2];
71633c34adc5Sramat 
71643c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
71653c34adc5Sramat 
71663c34adc5Sramat 	if ((err = nvlist_alloc(&nvl, 0, KM_SLEEP)) != 0)
71673c34adc5Sramat 		return (err);
71683c34adc5Sramat 
71693c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
71703c34adc5Sramat 		val[0] = cpi->cpi_cphci->cphci_id;
71713c34adc5Sramat 		val[1] = cpi->cpi_flags;
71723c34adc5Sramat 		if ((err = nvlist_add_uint32_array(nvl, cpi->cpi_addr, val, 2))
71733c34adc5Sramat 		    != 0)
71743c34adc5Sramat 			goto out;
71753c34adc5Sramat 	}
71763c34adc5Sramat 
71773c34adc5Sramat 	err = nvlist_add_nvlist(caddrmapnvl, cct->cct_name_addr, nvl);
71783c34adc5Sramat out:
71793c34adc5Sramat 	nvlist_free(nvl);
71803c34adc5Sramat 	return (err);
71813c34adc5Sramat }
71823c34adc5Sramat 
71833c34adc5Sramat /*
71843c34adc5Sramat  * Build caddrmapnvl using the information in the vhci cache
71853c34adc5Sramat  * and add it to the mainnvl.
71863c34adc5Sramat  * Returns 0 on success, errno on failure.
71873c34adc5Sramat  */
71883c34adc5Sramat static int
71893c34adc5Sramat vhcache_to_caddrmapnvl(mdi_vhci_cache_t *vhcache, nvlist_t *mainnvl)
71903c34adc5Sramat {
71913c34adc5Sramat 	mdi_vhcache_client_t *cct;
71923c34adc5Sramat 	nvlist_t *nvl;
71933c34adc5Sramat 	int err;
71943c34adc5Sramat 
71953c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
71963c34adc5Sramat 
71973c34adc5Sramat 	if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) != 0)
71983c34adc5Sramat 		return (err);
71993c34adc5Sramat 
72003c34adc5Sramat 	for (cct = vhcache->vhcache_client_head; cct != NULL;
72013c34adc5Sramat 	    cct = cct->cct_next) {
72023c34adc5Sramat 		if ((err = vhcache_to_paddrnvl(vhcache, cct, nvl)) != 0)
72033c34adc5Sramat 			goto out;
72043c34adc5Sramat 	}
72053c34adc5Sramat 
72063c34adc5Sramat 	err = nvlist_add_nvlist(mainnvl, MDI_NVPNAME_CTADDRMAP, nvl);
72073c34adc5Sramat out:
72083c34adc5Sramat 	nvlist_free(nvl);
72093c34adc5Sramat 	return (err);
72103c34adc5Sramat }
72113c34adc5Sramat 
72123c34adc5Sramat /*
72133c34adc5Sramat  * Build nvlist using the information in the vhci cache.
72143c34adc5Sramat  * See the comment in mainnvl_to_vhcache() for the format of the nvlist.
72153c34adc5Sramat  * Returns nvl on success, NULL on failure.
72163c34adc5Sramat  */
72173c34adc5Sramat static nvlist_t *
72183c34adc5Sramat vhcache_to_mainnvl(mdi_vhci_cache_t *vhcache)
72193c34adc5Sramat {
72203c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
72213c34adc5Sramat 	uint_t phci_count;
72223c34adc5Sramat 	char **phcis;
72233c34adc5Sramat 	nvlist_t *nvl;
72243c34adc5Sramat 	int err, i;
72253c34adc5Sramat 
72263c34adc5Sramat 	if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) != 0) {
72273c34adc5Sramat 		nvl = NULL;
72283c34adc5Sramat 		goto out;
72293c34adc5Sramat 	}
72303c34adc5Sramat 
72313c34adc5Sramat 	if ((err = nvlist_add_int32(nvl, MDI_NVPNAME_VERSION,
72323c34adc5Sramat 	    MDI_VHCI_CACHE_VERSION)) != 0)
72333c34adc5Sramat 		goto out;
72343c34adc5Sramat 
72353c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
72363c34adc5Sramat 	if (vhcache->vhcache_phci_head == NULL) {
72373c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
72383c34adc5Sramat 		return (nvl);
72393c34adc5Sramat 	}
72403c34adc5Sramat 
72413c34adc5Sramat 	phci_count = 0;
72423c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
72433c34adc5Sramat 	    cphci = cphci->cphci_next)
72443c34adc5Sramat 		cphci->cphci_id = phci_count++;
72453c34adc5Sramat 
72463c34adc5Sramat 	/* build phci pathname list */
72473c34adc5Sramat 	phcis = kmem_alloc(sizeof (char *) * phci_count, KM_SLEEP);
72483c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head, i = 0; cphci != NULL;
72493c34adc5Sramat 	    cphci = cphci->cphci_next, i++)
72503c34adc5Sramat 		phcis[i] = i_ddi_strdup(cphci->cphci_path, KM_SLEEP);
72513c34adc5Sramat 
72523c34adc5Sramat 	err = nvlist_add_string_array(nvl, MDI_NVPNAME_PHCIS, phcis,
72533c34adc5Sramat 	    phci_count);
72543c34adc5Sramat 	free_string_array(phcis, phci_count);
72553c34adc5Sramat 
72563c34adc5Sramat 	if (err == 0 &&
72573c34adc5Sramat 	    (err = vhcache_to_caddrmapnvl(vhcache, nvl)) == 0) {
72583c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
72593c34adc5Sramat 		return (nvl);
72603c34adc5Sramat 	}
72613c34adc5Sramat 
72623c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
72633c34adc5Sramat out:
72643c34adc5Sramat 	if (nvl)
72653c34adc5Sramat 		nvlist_free(nvl);
72663c34adc5Sramat 	return (NULL);
72673c34adc5Sramat }
72683c34adc5Sramat 
72693c34adc5Sramat /*
72703c34adc5Sramat  * Lookup vhcache phci structure for the specified phci path.
72713c34adc5Sramat  */
72723c34adc5Sramat static mdi_vhcache_phci_t *
72733c34adc5Sramat lookup_vhcache_phci_by_name(mdi_vhci_cache_t *vhcache, char *phci_path)
72743c34adc5Sramat {
72753c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
72763c34adc5Sramat 
72773c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
72783c34adc5Sramat 
72793c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
72803c34adc5Sramat 	    cphci = cphci->cphci_next) {
72813c34adc5Sramat 		if (strcmp(cphci->cphci_path, phci_path) == 0)
72823c34adc5Sramat 			return (cphci);
72833c34adc5Sramat 	}
72843c34adc5Sramat 
72853c34adc5Sramat 	return (NULL);
72863c34adc5Sramat }
72873c34adc5Sramat 
72883c34adc5Sramat /*
72893c34adc5Sramat  * Lookup vhcache phci structure for the specified phci.
72903c34adc5Sramat  */
72913c34adc5Sramat static mdi_vhcache_phci_t *
72923c34adc5Sramat lookup_vhcache_phci_by_addr(mdi_vhci_cache_t *vhcache, mdi_phci_t *ph)
72933c34adc5Sramat {
72943c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
72953c34adc5Sramat 
72963c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
72973c34adc5Sramat 
72983c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
72993c34adc5Sramat 	    cphci = cphci->cphci_next) {
73003c34adc5Sramat 		if (cphci->cphci_phci == ph)
73013c34adc5Sramat 			return (cphci);
73023c34adc5Sramat 	}
73033c34adc5Sramat 
73043c34adc5Sramat 	return (NULL);
73053c34adc5Sramat }
73063c34adc5Sramat 
73073c34adc5Sramat /*
73083c34adc5Sramat  * Add the specified phci to the vhci cache if not already present.
73093c34adc5Sramat  */
73103c34adc5Sramat static void
73113c34adc5Sramat vhcache_phci_add(mdi_vhci_config_t *vhc, mdi_phci_t *ph)
73123c34adc5Sramat {
73133c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
73143c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
73153c34adc5Sramat 	char *pathname;
73163c34adc5Sramat 	int cache_updated;
73173c34adc5Sramat 
73183c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
73193c34adc5Sramat 
73203c34adc5Sramat 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
73213c34adc5Sramat 	(void) ddi_pathname(ph->ph_dip, pathname);
73223c34adc5Sramat 	if ((cphci = lookup_vhcache_phci_by_name(vhcache, pathname))
73233c34adc5Sramat 	    != NULL) {
73243c34adc5Sramat 		cphci->cphci_phci = ph;
73253c34adc5Sramat 		cache_updated = 0;
73263c34adc5Sramat 	} else {
73273c34adc5Sramat 		cphci = kmem_zalloc(sizeof (*cphci), KM_SLEEP);
73283c34adc5Sramat 		cphci->cphci_path = i_ddi_strdup(pathname, KM_SLEEP);
73293c34adc5Sramat 		cphci->cphci_phci = ph;
73303c34adc5Sramat 		enqueue_vhcache_phci(vhcache, cphci);
73313c34adc5Sramat 		cache_updated = 1;
73323c34adc5Sramat 	}
733367e56d35Sramat 
73343c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
73353c34adc5Sramat 
733667e56d35Sramat 	/*
733767e56d35Sramat 	 * Since a new phci has been added, reset
733867e56d35Sramat 	 * vhc_path_discovery_cutoff_time to allow for discovery of paths
733967e56d35Sramat 	 * during next vhcache_discover_paths().
734067e56d35Sramat 	 */
734167e56d35Sramat 	mutex_enter(&vhc->vhc_lock);
734267e56d35Sramat 	vhc->vhc_path_discovery_cutoff_time = 0;
734367e56d35Sramat 	mutex_exit(&vhc->vhc_lock);
734467e56d35Sramat 
73453c34adc5Sramat 	kmem_free(pathname, MAXPATHLEN);
73463c34adc5Sramat 	if (cache_updated)
73473c34adc5Sramat 		vhcache_dirty(vhc);
73483c34adc5Sramat }
73493c34adc5Sramat 
73503c34adc5Sramat /*
73513c34adc5Sramat  * Remove the reference to the specified phci from the vhci cache.
73523c34adc5Sramat  */
73533c34adc5Sramat static void
73543c34adc5Sramat vhcache_phci_remove(mdi_vhci_config_t *vhc, mdi_phci_t *ph)
73553c34adc5Sramat {
73563c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
73573c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
73583c34adc5Sramat 
73593c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
73603c34adc5Sramat 	if ((cphci = lookup_vhcache_phci_by_addr(vhcache, ph)) != NULL) {
73613c34adc5Sramat 		/* do not remove the actual mdi_vhcache_phci structure */
73623c34adc5Sramat 		cphci->cphci_phci = NULL;
73633c34adc5Sramat 	}
73643c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
73653c34adc5Sramat }
73663c34adc5Sramat 
73673c34adc5Sramat static void
73683c34adc5Sramat init_vhcache_lookup_token(mdi_vhcache_lookup_token_t *dst,
73693c34adc5Sramat     mdi_vhcache_lookup_token_t *src)
73703c34adc5Sramat {
73713c34adc5Sramat 	if (src == NULL) {
73723c34adc5Sramat 		dst->lt_cct = NULL;
73733c34adc5Sramat 		dst->lt_cct_lookup_time = 0;
73743c34adc5Sramat 	} else {
73753c34adc5Sramat 		dst->lt_cct = src->lt_cct;
73763c34adc5Sramat 		dst->lt_cct_lookup_time = src->lt_cct_lookup_time;
73773c34adc5Sramat 	}
73783c34adc5Sramat }
73793c34adc5Sramat 
73803c34adc5Sramat /*
73813c34adc5Sramat  * Look up vhcache client for the specified client.
73823c34adc5Sramat  */
73833c34adc5Sramat static mdi_vhcache_client_t *
73843c34adc5Sramat lookup_vhcache_client(mdi_vhci_cache_t *vhcache, char *ct_name, char *ct_addr,
73853c34adc5Sramat     mdi_vhcache_lookup_token_t *token)
73863c34adc5Sramat {
73873c34adc5Sramat 	mod_hash_val_t hv;
73883c34adc5Sramat 	char *name_addr;
73893c34adc5Sramat 	int len;
73903c34adc5Sramat 
73913c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
73923c34adc5Sramat 
73933c34adc5Sramat 	/*
73943c34adc5Sramat 	 * If no vhcache clean occurred since the last lookup, we can
73953c34adc5Sramat 	 * simply return the cct from the last lookup operation.
73963c34adc5Sramat 	 * It works because ccts are never freed except during the vhcache
73973c34adc5Sramat 	 * cleanup operation.
73983c34adc5Sramat 	 */
73993c34adc5Sramat 	if (token != NULL &&
74003c34adc5Sramat 	    vhcache->vhcache_clean_time < token->lt_cct_lookup_time)
74013c34adc5Sramat 		return (token->lt_cct);
74023c34adc5Sramat 
74033c34adc5Sramat 	name_addr = vhcache_mknameaddr(ct_name, ct_addr, &len);
74043c34adc5Sramat 	if (mod_hash_find(vhcache->vhcache_client_hash,
74053c34adc5Sramat 	    (mod_hash_key_t)name_addr, &hv) == 0) {
74063c34adc5Sramat 		if (token) {
74073c34adc5Sramat 			token->lt_cct = (mdi_vhcache_client_t *)hv;
74083c34adc5Sramat 			token->lt_cct_lookup_time = lbolt64;
74093c34adc5Sramat 		}
74103c34adc5Sramat 	} else {
74113c34adc5Sramat 		if (token) {
74123c34adc5Sramat 			token->lt_cct = NULL;
74133c34adc5Sramat 			token->lt_cct_lookup_time = 0;
74143c34adc5Sramat 		}
74153c34adc5Sramat 		hv = NULL;
74163c34adc5Sramat 	}
74173c34adc5Sramat 	kmem_free(name_addr, len);
74183c34adc5Sramat 	return ((mdi_vhcache_client_t *)hv);
74193c34adc5Sramat }
74203c34adc5Sramat 
74213c34adc5Sramat /*
74223c34adc5Sramat  * Add the specified path to the vhci cache if not already present.
74233c34adc5Sramat  * Also add the vhcache client for the client corresponding to this path
74243c34adc5Sramat  * if it doesn't already exist.
74253c34adc5Sramat  */
74263c34adc5Sramat static void
74273c34adc5Sramat vhcache_pi_add(mdi_vhci_config_t *vhc, struct mdi_pathinfo *pip)
74283c34adc5Sramat {
74293c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
74303c34adc5Sramat 	mdi_vhcache_client_t *cct;
74313c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
74323c34adc5Sramat 	mdi_phci_t *ph = pip->pi_phci;
74333c34adc5Sramat 	mdi_client_t *ct = pip->pi_client;
74343c34adc5Sramat 	int cache_updated = 0;
74353c34adc5Sramat 
74363c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
74373c34adc5Sramat 
74383c34adc5Sramat 	/* if vhcache client for this pip doesn't already exist, add it */
74393c34adc5Sramat 	if ((cct = lookup_vhcache_client(vhcache, ct->ct_drvname, ct->ct_guid,
74403c34adc5Sramat 	    NULL)) == NULL) {
74413c34adc5Sramat 		cct = kmem_zalloc(sizeof (*cct), KM_SLEEP);
74423c34adc5Sramat 		cct->cct_name_addr = vhcache_mknameaddr(ct->ct_drvname,
74433c34adc5Sramat 		    ct->ct_guid, NULL);
74443c34adc5Sramat 		enqueue_vhcache_client(vhcache, cct);
74453c34adc5Sramat 		(void) mod_hash_insert(vhcache->vhcache_client_hash,
74463c34adc5Sramat 		    (mod_hash_key_t)cct->cct_name_addr, (mod_hash_val_t)cct);
74473c34adc5Sramat 		cache_updated = 1;
74483c34adc5Sramat 	}
74493c34adc5Sramat 
74503c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
74513c34adc5Sramat 		if (cpi->cpi_cphci->cphci_phci == ph &&
74523c34adc5Sramat 		    strcmp(cpi->cpi_addr, pip->pi_addr) == 0) {
74533c34adc5Sramat 			cpi->cpi_pip = pip;
74543c34adc5Sramat 			if (cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST) {
74553c34adc5Sramat 				cpi->cpi_flags &=
74563c34adc5Sramat 				    ~MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
74573c34adc5Sramat 				sort_vhcache_paths(cct);
74583c34adc5Sramat 				cache_updated = 1;
74593c34adc5Sramat 			}
74603c34adc5Sramat 			break;
74613c34adc5Sramat 		}
74623c34adc5Sramat 	}
74633c34adc5Sramat 
74643c34adc5Sramat 	if (cpi == NULL) {
74653c34adc5Sramat 		cpi = kmem_zalloc(sizeof (*cpi), KM_SLEEP);
74663c34adc5Sramat 		cpi->cpi_addr = i_ddi_strdup(pip->pi_addr, KM_SLEEP);
74673c34adc5Sramat 		cpi->cpi_cphci = lookup_vhcache_phci_by_addr(vhcache, ph);
74683c34adc5Sramat 		ASSERT(cpi->cpi_cphci != NULL);
74693c34adc5Sramat 		cpi->cpi_pip = pip;
74703c34adc5Sramat 		enqueue_vhcache_pathinfo(cct, cpi);
74713c34adc5Sramat 		cache_updated = 1;
74723c34adc5Sramat 	}
74733c34adc5Sramat 
74743c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
74753c34adc5Sramat 
74763c34adc5Sramat 	if (cache_updated)
74773c34adc5Sramat 		vhcache_dirty(vhc);
74783c34adc5Sramat }
74793c34adc5Sramat 
74803c34adc5Sramat /*
74813c34adc5Sramat  * Remove the reference to the specified path from the vhci cache.
74823c34adc5Sramat  */
74833c34adc5Sramat static void
74843c34adc5Sramat vhcache_pi_remove(mdi_vhci_config_t *vhc, struct mdi_pathinfo *pip)
74853c34adc5Sramat {
74863c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
74873c34adc5Sramat 	mdi_client_t *ct = pip->pi_client;
74883c34adc5Sramat 	mdi_vhcache_client_t *cct;
74893c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
74903c34adc5Sramat 
74913c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
74923c34adc5Sramat 	if ((cct = lookup_vhcache_client(vhcache, ct->ct_drvname, ct->ct_guid,
74933c34adc5Sramat 	    NULL)) != NULL) {
74943c34adc5Sramat 		for (cpi = cct->cct_cpi_head; cpi != NULL;
74953c34adc5Sramat 		    cpi = cpi->cpi_next) {
74963c34adc5Sramat 			if (cpi->cpi_pip == pip) {
74973c34adc5Sramat 				cpi->cpi_pip = NULL;
74983c34adc5Sramat 				break;
74993c34adc5Sramat 			}
75003c34adc5Sramat 		}
75013c34adc5Sramat 	}
75023c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
75033c34adc5Sramat }
75043c34adc5Sramat 
75053c34adc5Sramat /*
75063c34adc5Sramat  * Flush the vhci cache to disk.
75073c34adc5Sramat  * Returns MDI_SUCCESS on success, MDI_FAILURE on failure.
75083c34adc5Sramat  */
75093c34adc5Sramat static int
75103c34adc5Sramat flush_vhcache(mdi_vhci_config_t *vhc, int force_flag)
75113c34adc5Sramat {
75123c34adc5Sramat 	nvlist_t *nvl;
75133c34adc5Sramat 	int err;
75143c34adc5Sramat 	int rv;
75153c34adc5Sramat 
75163c34adc5Sramat 	/*
75173c34adc5Sramat 	 * It is possible that the system may shutdown before
75183c34adc5Sramat 	 * i_ddi_io_initialized (during stmsboot for example). To allow for
75193c34adc5Sramat 	 * flushing the cache in this case do not check for
75203c34adc5Sramat 	 * i_ddi_io_initialized when force flag is set.
75213c34adc5Sramat 	 */
75223c34adc5Sramat 	if (force_flag == 0 && !i_ddi_io_initialized())
75233c34adc5Sramat 		return (MDI_FAILURE);
75243c34adc5Sramat 
75253c34adc5Sramat 	if ((nvl = vhcache_to_mainnvl(&vhc->vhc_vhcache)) != NULL) {
75263c34adc5Sramat 		err = fwrite_nvlist(vhc->vhc_vhcache_filename, nvl);
75273c34adc5Sramat 		nvlist_free(nvl);
75283c34adc5Sramat 	} else
75293c34adc5Sramat 		err = EFAULT;
75303c34adc5Sramat 
75313c34adc5Sramat 	rv = MDI_SUCCESS;
75323c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
75333c34adc5Sramat 	if (err != 0) {
75343c34adc5Sramat 		if (err == EROFS) {
75353c34adc5Sramat 			vhc->vhc_flags |= MDI_VHC_READONLY_FS;
75363c34adc5Sramat 			vhc->vhc_flags &= ~(MDI_VHC_VHCACHE_FLUSH_ERROR |
75373c34adc5Sramat 			    MDI_VHC_VHCACHE_DIRTY);
75383c34adc5Sramat 		} else {
75393c34adc5Sramat 			if (!(vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_ERROR)) {
75403c34adc5Sramat 				cmn_err(CE_CONT, "%s: update failed\n",
75413c34adc5Sramat 				    vhc->vhc_vhcache_filename);
75423c34adc5Sramat 				vhc->vhc_flags |= MDI_VHC_VHCACHE_FLUSH_ERROR;
75433c34adc5Sramat 			}
75443c34adc5Sramat 			rv = MDI_FAILURE;
75453c34adc5Sramat 		}
75463c34adc5Sramat 	} else if (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_ERROR) {
75473c34adc5Sramat 		cmn_err(CE_CONT,
75483c34adc5Sramat 		    "%s: update now ok\n", vhc->vhc_vhcache_filename);
75493c34adc5Sramat 		vhc->vhc_flags &= ~MDI_VHC_VHCACHE_FLUSH_ERROR;
75503c34adc5Sramat 	}
75513c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
75523c34adc5Sramat 
75533c34adc5Sramat 	return (rv);
75543c34adc5Sramat }
75553c34adc5Sramat 
75563c34adc5Sramat /*
75573c34adc5Sramat  * Call flush_vhcache() to flush the vhci cache at the scheduled time.
75583c34adc5Sramat  * Exits itself if left idle for the idle timeout period.
75593c34adc5Sramat  */
75603c34adc5Sramat static void
75613c34adc5Sramat vhcache_flush_thread(void *arg)
75623c34adc5Sramat {
75633c34adc5Sramat 	mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
75643c34adc5Sramat 	clock_t idle_time, quit_at_ticks;
75653c34adc5Sramat 	callb_cpr_t cprinfo;
75663c34adc5Sramat 
75673c34adc5Sramat 	/* number of seconds to sleep idle before exiting */
75683c34adc5Sramat 	idle_time = mdi_vhcache_flush_daemon_idle_time * TICKS_PER_SECOND;
75693c34adc5Sramat 
75703c34adc5Sramat 	CALLB_CPR_INIT(&cprinfo, &vhc->vhc_lock, callb_generic_cpr,
75713c34adc5Sramat 	    "mdi_vhcache_flush");
75723c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
75733c34adc5Sramat 	for (; ; ) {
75743c34adc5Sramat 		while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
75753c34adc5Sramat 		    (vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY)) {
75763c34adc5Sramat 			if (ddi_get_lbolt() < vhc->vhc_flush_at_ticks) {
75773c34adc5Sramat 				CALLB_CPR_SAFE_BEGIN(&cprinfo);
75783c34adc5Sramat 				(void) cv_timedwait(&vhc->vhc_cv,
75793c34adc5Sramat 				    &vhc->vhc_lock, vhc->vhc_flush_at_ticks);
75803c34adc5Sramat 				CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
75813c34adc5Sramat 			} else {
75823c34adc5Sramat 				vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
75833c34adc5Sramat 				mutex_exit(&vhc->vhc_lock);
75843c34adc5Sramat 
75853c34adc5Sramat 				if (flush_vhcache(vhc, 0) != MDI_SUCCESS)
75863c34adc5Sramat 					vhcache_dirty(vhc);
75873c34adc5Sramat 
75883c34adc5Sramat 				mutex_enter(&vhc->vhc_lock);
75893c34adc5Sramat 			}
75903c34adc5Sramat 		}
75913c34adc5Sramat 
75923c34adc5Sramat 		quit_at_ticks = ddi_get_lbolt() + idle_time;
75933c34adc5Sramat 
75943c34adc5Sramat 		while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
75953c34adc5Sramat 		    !(vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) &&
75963c34adc5Sramat 		    ddi_get_lbolt() < quit_at_ticks) {
75973c34adc5Sramat 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
75983c34adc5Sramat 			(void) cv_timedwait(&vhc->vhc_cv, &vhc->vhc_lock,
75993c34adc5Sramat 			    quit_at_ticks);
76003c34adc5Sramat 			CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
76013c34adc5Sramat 		}
76023c34adc5Sramat 
76033c34adc5Sramat 		if ((vhc->vhc_flags & MDI_VHC_EXIT) ||
76043c34adc5Sramat 		    !(vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY))
76053c34adc5Sramat 			goto out;
76063c34adc5Sramat 	}
76073c34adc5Sramat 
76083c34adc5Sramat out:
76093c34adc5Sramat 	vhc->vhc_flags &= ~MDI_VHC_VHCACHE_FLUSH_THREAD;
76103c34adc5Sramat 	/* CALLB_CPR_EXIT releases the vhc->vhc_lock */
76113c34adc5Sramat 	CALLB_CPR_EXIT(&cprinfo);
76123c34adc5Sramat }
76133c34adc5Sramat 
76143c34adc5Sramat /*
76153c34adc5Sramat  * Make vhci cache dirty and schedule flushing by vhcache flush thread.
76163c34adc5Sramat  */
76173c34adc5Sramat static void
76183c34adc5Sramat vhcache_dirty(mdi_vhci_config_t *vhc)
76193c34adc5Sramat {
76203c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
76213c34adc5Sramat 	int create_thread;
76223c34adc5Sramat 
76233c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
76243c34adc5Sramat 	/* do not flush cache until the cache is fully built */
76253c34adc5Sramat 	if (!(vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE)) {
76263c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
76273c34adc5Sramat 		return;
76283c34adc5Sramat 	}
76293c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
76303c34adc5Sramat 
76313c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
76323c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_READONLY_FS) {
76333c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
76343c34adc5Sramat 		return;
76353c34adc5Sramat 	}
76363c34adc5Sramat 
76373c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_VHCACHE_DIRTY;
76383c34adc5Sramat 	vhc->vhc_flush_at_ticks = ddi_get_lbolt() +
76393c34adc5Sramat 	    mdi_vhcache_flush_delay * TICKS_PER_SECOND;
76403c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) {
76413c34adc5Sramat 		cv_broadcast(&vhc->vhc_cv);
76423c34adc5Sramat 		create_thread = 0;
76433c34adc5Sramat 	} else {
76443c34adc5Sramat 		vhc->vhc_flags |= MDI_VHC_VHCACHE_FLUSH_THREAD;
76453c34adc5Sramat 		create_thread = 1;
76463c34adc5Sramat 	}
76473c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
76483c34adc5Sramat 
76493c34adc5Sramat 	if (create_thread)
76503c34adc5Sramat 		(void) thread_create(NULL, 0, vhcache_flush_thread, vhc,
76513c34adc5Sramat 		    0, &p0, TS_RUN, minclsyspri);
76523c34adc5Sramat }
76533c34adc5Sramat 
76543c34adc5Sramat /*
76553c34adc5Sramat  * phci bus config structure - one for for each phci bus config operation that
76563c34adc5Sramat  * we initiate on behalf of a vhci.
76573c34adc5Sramat  */
76583c34adc5Sramat typedef struct mdi_phci_bus_config_s {
76593c34adc5Sramat 	char *phbc_phci_path;
76603c34adc5Sramat 	struct mdi_vhci_bus_config_s *phbc_vhbusconfig;	/* vhci bus config */
76613c34adc5Sramat 	struct mdi_phci_bus_config_s *phbc_next;
76623c34adc5Sramat } mdi_phci_bus_config_t;
76633c34adc5Sramat 
76643c34adc5Sramat /* vhci bus config structure - one for each vhci bus config operation */
76653c34adc5Sramat typedef struct mdi_vhci_bus_config_s {
76663c34adc5Sramat 	ddi_bus_config_op_t vhbc_op;	/* bus config op */
76673c34adc5Sramat 	major_t vhbc_op_major;		/* bus config op major */
76683c34adc5Sramat 	uint_t vhbc_op_flags;		/* bus config op flags */
76693c34adc5Sramat 	kmutex_t vhbc_lock;
76703c34adc5Sramat 	kcondvar_t vhbc_cv;
76713c34adc5Sramat 	int vhbc_thr_count;
76723c34adc5Sramat } mdi_vhci_bus_config_t;
76733c34adc5Sramat 
76743c34adc5Sramat /*
76753c34adc5Sramat  * bus config the specified phci
76763c34adc5Sramat  */
76773c34adc5Sramat static void
76783c34adc5Sramat bus_config_phci(void *arg)
76793c34adc5Sramat {
76803c34adc5Sramat 	mdi_phci_bus_config_t *phbc = (mdi_phci_bus_config_t *)arg;
76813c34adc5Sramat 	mdi_vhci_bus_config_t *vhbc = phbc->phbc_vhbusconfig;
76823c34adc5Sramat 	dev_info_t *ph_dip;
76833c34adc5Sramat 
76843c34adc5Sramat 	/*
76853c34adc5Sramat 	 * first configure all path components upto phci and then configure
76863c34adc5Sramat 	 * the phci children.
76873c34adc5Sramat 	 */
76883c34adc5Sramat 	if ((ph_dip = e_ddi_hold_devi_by_path(phbc->phbc_phci_path, 0))
76893c34adc5Sramat 	    != NULL) {
76903c34adc5Sramat 		if (vhbc->vhbc_op == BUS_CONFIG_DRIVER ||
76913c34adc5Sramat 		    vhbc->vhbc_op == BUS_UNCONFIG_DRIVER) {
76923c34adc5Sramat 			(void) ndi_devi_config_driver(ph_dip,
76933c34adc5Sramat 			    vhbc->vhbc_op_flags,
76943c34adc5Sramat 			    vhbc->vhbc_op_major);
76953c34adc5Sramat 		} else
76963c34adc5Sramat 			(void) ndi_devi_config(ph_dip,
76973c34adc5Sramat 			    vhbc->vhbc_op_flags);
76983c34adc5Sramat 
76993c34adc5Sramat 		/* release the hold that e_ddi_hold_devi_by_path() placed */
77003c34adc5Sramat 		ndi_rele_devi(ph_dip);
77013c34adc5Sramat 	}
77023c34adc5Sramat 
77033c34adc5Sramat 	kmem_free(phbc->phbc_phci_path, strlen(phbc->phbc_phci_path) + 1);
77043c34adc5Sramat 	kmem_free(phbc, sizeof (*phbc));
77053c34adc5Sramat 
77063c34adc5Sramat 	mutex_enter(&vhbc->vhbc_lock);
77073c34adc5Sramat 	vhbc->vhbc_thr_count--;
77083c34adc5Sramat 	if (vhbc->vhbc_thr_count == 0)
77093c34adc5Sramat 		cv_broadcast(&vhbc->vhbc_cv);
77103c34adc5Sramat 	mutex_exit(&vhbc->vhbc_lock);
77113c34adc5Sramat }
77123c34adc5Sramat 
77133c34adc5Sramat /*
77143c34adc5Sramat  * Bus config all phcis associated with the vhci in parallel.
77153c34adc5Sramat  * op must be BUS_CONFIG_DRIVER or BUS_CONFIG_ALL.
77163c34adc5Sramat  */
77173c34adc5Sramat static void
77183c34adc5Sramat bus_config_all_phcis(mdi_vhci_cache_t *vhcache, uint_t flags,
77193c34adc5Sramat     ddi_bus_config_op_t op, major_t maj)
77203c34adc5Sramat {
77213c34adc5Sramat 	mdi_phci_bus_config_t *phbc_head = NULL, *phbc, *phbc_next;
77223c34adc5Sramat 	mdi_vhci_bus_config_t *vhbc;
77233c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
77243c34adc5Sramat 
77253c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
77263c34adc5Sramat 	if (vhcache->vhcache_phci_head == NULL) {
77273c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
77283c34adc5Sramat 		return;
77293c34adc5Sramat 	}
77303c34adc5Sramat 
77313c34adc5Sramat 	vhbc = kmem_zalloc(sizeof (*vhbc), KM_SLEEP);
77323c34adc5Sramat 
77333c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
77343c34adc5Sramat 	    cphci = cphci->cphci_next) {
77353c34adc5Sramat 		phbc = kmem_zalloc(sizeof (*phbc), KM_SLEEP);
77363c34adc5Sramat 		phbc->phbc_phci_path = i_ddi_strdup(cphci->cphci_path,
77373c34adc5Sramat 		    KM_SLEEP);
77383c34adc5Sramat 		phbc->phbc_vhbusconfig = vhbc;
77393c34adc5Sramat 		phbc->phbc_next = phbc_head;
77403c34adc5Sramat 		phbc_head = phbc;
77413c34adc5Sramat 		vhbc->vhbc_thr_count++;
77423c34adc5Sramat 	}
77433c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
77443c34adc5Sramat 
77453c34adc5Sramat 	vhbc->vhbc_op = op;
77463c34adc5Sramat 	vhbc->vhbc_op_major = maj;
77473c34adc5Sramat 	vhbc->vhbc_op_flags = NDI_NO_EVENT |
77483c34adc5Sramat 	    (flags & (NDI_CONFIG_REPROBE | NDI_DRV_CONF_REPROBE));
77493c34adc5Sramat 	mutex_init(&vhbc->vhbc_lock, NULL, MUTEX_DEFAULT, NULL);
77503c34adc5Sramat 	cv_init(&vhbc->vhbc_cv, NULL, CV_DRIVER, NULL);
77513c34adc5Sramat 
77523c34adc5Sramat 	/* now create threads to initiate bus config on all phcis in parallel */
77533c34adc5Sramat 	for (phbc = phbc_head; phbc != NULL; phbc = phbc_next) {
77543c34adc5Sramat 		phbc_next = phbc->phbc_next;
77553c34adc5Sramat 		if (mdi_mtc_off)
77563c34adc5Sramat 			bus_config_phci((void *)phbc);
77573c34adc5Sramat 		else
77583c34adc5Sramat 			(void) thread_create(NULL, 0, bus_config_phci, phbc,
77593c34adc5Sramat 			    0, &p0, TS_RUN, minclsyspri);
77603c34adc5Sramat 	}
77613c34adc5Sramat 
77623c34adc5Sramat 	mutex_enter(&vhbc->vhbc_lock);
77633c34adc5Sramat 	/* wait until all threads exit */
77643c34adc5Sramat 	while (vhbc->vhbc_thr_count > 0)
77653c34adc5Sramat 		cv_wait(&vhbc->vhbc_cv, &vhbc->vhbc_lock);
77663c34adc5Sramat 	mutex_exit(&vhbc->vhbc_lock);
77673c34adc5Sramat 
77683c34adc5Sramat 	mutex_destroy(&vhbc->vhbc_lock);
77693c34adc5Sramat 	cv_destroy(&vhbc->vhbc_cv);
77703c34adc5Sramat 	kmem_free(vhbc, sizeof (*vhbc));
77713c34adc5Sramat }
77723c34adc5Sramat 
77733c34adc5Sramat /*
777467e56d35Sramat  * Single threaded version of bus_config_all_phcis()
777567e56d35Sramat  */
777667e56d35Sramat static void
777767e56d35Sramat st_bus_config_all_phcis(mdi_vhci_config_t *vhc, uint_t flags,
777867e56d35Sramat     ddi_bus_config_op_t op, major_t maj)
777967e56d35Sramat {
778067e56d35Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
778167e56d35Sramat 
778267e56d35Sramat 	single_threaded_vhconfig_enter(vhc);
778367e56d35Sramat 	bus_config_all_phcis(vhcache, flags, op, maj);
778467e56d35Sramat 	single_threaded_vhconfig_exit(vhc);
778567e56d35Sramat }
778667e56d35Sramat 
778767e56d35Sramat /*
77883c34adc5Sramat  * Perform BUS_CONFIG_ONE on the specified child of the phci.
77893c34adc5Sramat  * The path includes the child component in addition to the phci path.
77903c34adc5Sramat  */
77913c34adc5Sramat static int
77923c34adc5Sramat bus_config_one_phci_child(char *path)
77933c34adc5Sramat {
77943c34adc5Sramat 	dev_info_t *ph_dip, *child;
77953c34adc5Sramat 	char *devnm;
77963c34adc5Sramat 	int rv = MDI_FAILURE;
77973c34adc5Sramat 
77983c34adc5Sramat 	/* extract the child component of the phci */
77993c34adc5Sramat 	devnm = strrchr(path, '/');
78003c34adc5Sramat 	*devnm++ = '\0';
78013c34adc5Sramat 
78023c34adc5Sramat 	/*
78033c34adc5Sramat 	 * first configure all path components upto phci and then
78043c34adc5Sramat 	 * configure the phci child.
78053c34adc5Sramat 	 */
78063c34adc5Sramat 	if ((ph_dip = e_ddi_hold_devi_by_path(path, 0)) != NULL) {
78073c34adc5Sramat 		if (ndi_devi_config_one(ph_dip, devnm, &child, NDI_NO_EVENT) ==
78083c34adc5Sramat 		    NDI_SUCCESS) {
78093c34adc5Sramat 			/*
78103c34adc5Sramat 			 * release the hold that ndi_devi_config_one() placed
78113c34adc5Sramat 			 */
78123c34adc5Sramat 			ndi_rele_devi(child);
78133c34adc5Sramat 			rv = MDI_SUCCESS;
78143c34adc5Sramat 		}
78153c34adc5Sramat 
78163c34adc5Sramat 		/* release the hold that e_ddi_hold_devi_by_path() placed */
78173c34adc5Sramat 		ndi_rele_devi(ph_dip);
78183c34adc5Sramat 	}
78193c34adc5Sramat 
78203c34adc5Sramat 	devnm--;
78213c34adc5Sramat 	*devnm = '/';
78223c34adc5Sramat 	return (rv);
78233c34adc5Sramat }
78243c34adc5Sramat 
78253c34adc5Sramat /*
78263c34adc5Sramat  * Build a list of phci client paths for the specified vhci client.
78273c34adc5Sramat  * The list includes only those phci client paths which aren't configured yet.
78283c34adc5Sramat  */
78293c34adc5Sramat static mdi_phys_path_t *
78303c34adc5Sramat build_phclient_path_list(mdi_vhcache_client_t *cct, char *ct_name)
78313c34adc5Sramat {
78323c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
78333c34adc5Sramat 	mdi_phys_path_t *pp_head = NULL, *pp_tail = NULL, *pp;
78343c34adc5Sramat 	int config_path, len;
78353c34adc5Sramat 
78363c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
78373c34adc5Sramat 		/*
78383c34adc5Sramat 		 * include only those paths that aren't configured.
78393c34adc5Sramat 		 */
78403c34adc5Sramat 		config_path = 0;
78413c34adc5Sramat 		if (cpi->cpi_pip == NULL)
78423c34adc5Sramat 			config_path = 1;
78433c34adc5Sramat 		else {
78443c34adc5Sramat 			MDI_PI_LOCK(cpi->cpi_pip);
78453c34adc5Sramat 			if (MDI_PI_IS_INIT(cpi->cpi_pip))
78463c34adc5Sramat 				config_path = 1;
78473c34adc5Sramat 			MDI_PI_UNLOCK(cpi->cpi_pip);
78483c34adc5Sramat 		}
78493c34adc5Sramat 
78503c34adc5Sramat 		if (config_path) {
78513c34adc5Sramat 			pp = kmem_alloc(sizeof (*pp), KM_SLEEP);
78523c34adc5Sramat 			len = strlen(cpi->cpi_cphci->cphci_path) +
78533c34adc5Sramat 			    strlen(ct_name) + strlen(cpi->cpi_addr) + 3;
78543c34adc5Sramat 			pp->phys_path = kmem_alloc(len, KM_SLEEP);
78553c34adc5Sramat 			(void) snprintf(pp->phys_path, len, "%s/%s@%s",
78563c34adc5Sramat 			    cpi->cpi_cphci->cphci_path, ct_name,
78573c34adc5Sramat 			    cpi->cpi_addr);
78583c34adc5Sramat 			pp->phys_path_next = NULL;
78593c34adc5Sramat 
78603c34adc5Sramat 			if (pp_head == NULL)
78613c34adc5Sramat 				pp_head = pp;
78623c34adc5Sramat 			else
78633c34adc5Sramat 				pp_tail->phys_path_next = pp;
78643c34adc5Sramat 			pp_tail = pp;
78653c34adc5Sramat 		}
78663c34adc5Sramat 	}
78673c34adc5Sramat 
78683c34adc5Sramat 	return (pp_head);
78693c34adc5Sramat }
78703c34adc5Sramat 
78713c34adc5Sramat /*
78723c34adc5Sramat  * Free the memory allocated for phci client path list.
78733c34adc5Sramat  */
78743c34adc5Sramat static void
78753c34adc5Sramat free_phclient_path_list(mdi_phys_path_t *pp_head)
78763c34adc5Sramat {
78773c34adc5Sramat 	mdi_phys_path_t *pp, *pp_next;
78783c34adc5Sramat 
78793c34adc5Sramat 	for (pp = pp_head; pp != NULL; pp = pp_next) {
78803c34adc5Sramat 		pp_next = pp->phys_path_next;
78813c34adc5Sramat 		kmem_free(pp->phys_path, strlen(pp->phys_path) + 1);
78823c34adc5Sramat 		kmem_free(pp, sizeof (*pp));
78833c34adc5Sramat 	}
78843c34adc5Sramat }
78853c34adc5Sramat 
78863c34adc5Sramat /*
78873c34adc5Sramat  * Allocated async client structure and initialize with the specified values.
78883c34adc5Sramat  */
78893c34adc5Sramat static mdi_async_client_config_t *
78903c34adc5Sramat alloc_async_client_config(char *ct_name, char *ct_addr,
78913c34adc5Sramat     mdi_phys_path_t *pp_head, mdi_vhcache_lookup_token_t *tok)
78923c34adc5Sramat {
78933c34adc5Sramat 	mdi_async_client_config_t *acc;
78943c34adc5Sramat 
78953c34adc5Sramat 	acc = kmem_alloc(sizeof (*acc), KM_SLEEP);
78963c34adc5Sramat 	acc->acc_ct_name = i_ddi_strdup(ct_name, KM_SLEEP);
78973c34adc5Sramat 	acc->acc_ct_addr = i_ddi_strdup(ct_addr, KM_SLEEP);
78983c34adc5Sramat 	acc->acc_phclient_path_list_head = pp_head;
78993c34adc5Sramat 	init_vhcache_lookup_token(&acc->acc_token, tok);
79003c34adc5Sramat 	acc->acc_next = NULL;
79013c34adc5Sramat 	return (acc);
79023c34adc5Sramat }
79033c34adc5Sramat 
79043c34adc5Sramat /*
79053c34adc5Sramat  * Free the memory allocated for the async client structure and their members.
79063c34adc5Sramat  */
79073c34adc5Sramat static void
79083c34adc5Sramat free_async_client_config(mdi_async_client_config_t *acc)
79093c34adc5Sramat {
79103c34adc5Sramat 	if (acc->acc_phclient_path_list_head)
79113c34adc5Sramat 		free_phclient_path_list(acc->acc_phclient_path_list_head);
79123c34adc5Sramat 	kmem_free(acc->acc_ct_name, strlen(acc->acc_ct_name) + 1);
79133c34adc5Sramat 	kmem_free(acc->acc_ct_addr, strlen(acc->acc_ct_addr) + 1);
79143c34adc5Sramat 	kmem_free(acc, sizeof (*acc));
79153c34adc5Sramat }
79163c34adc5Sramat 
79173c34adc5Sramat /*
79183c34adc5Sramat  * Sort vhcache pathinfos (cpis) of the specified client.
79193c34adc5Sramat  * All cpis which do not have MDI_CPI_HINT_PATH_DOES_NOT_EXIST
79203c34adc5Sramat  * flag set come at the beginning of the list. All cpis which have this
79213c34adc5Sramat  * flag set come at the end of the list.
79223c34adc5Sramat  */
79233c34adc5Sramat static void
79243c34adc5Sramat sort_vhcache_paths(mdi_vhcache_client_t *cct)
79253c34adc5Sramat {
79263c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi, *cpi_next, *cpi_head;
79273c34adc5Sramat 
79283c34adc5Sramat 	cpi_head = cct->cct_cpi_head;
79293c34adc5Sramat 	cct->cct_cpi_head = cct->cct_cpi_tail = NULL;
79303c34adc5Sramat 	for (cpi = cpi_head; cpi != NULL; cpi = cpi_next) {
79313c34adc5Sramat 		cpi_next = cpi->cpi_next;
79323c34adc5Sramat 		enqueue_vhcache_pathinfo(cct, cpi);
79333c34adc5Sramat 	}
79343c34adc5Sramat }
79353c34adc5Sramat 
79363c34adc5Sramat /*
79373c34adc5Sramat  * Verify whether MDI_CPI_HINT_PATH_DOES_NOT_EXIST flag setting is correct for
79383c34adc5Sramat  * every vhcache pathinfo of the specified client. If not adjust the flag
79393c34adc5Sramat  * setting appropriately.
79403c34adc5Sramat  *
79413c34adc5Sramat  * Note that MDI_CPI_HINT_PATH_DOES_NOT_EXIST flag is persisted in the
79423c34adc5Sramat  * on-disk vhci cache. So every time this flag is updated the cache must be
79433c34adc5Sramat  * flushed.
79443c34adc5Sramat  */
79453c34adc5Sramat static void
79463c34adc5Sramat adjust_sort_vhcache_paths(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr,
79473c34adc5Sramat     mdi_vhcache_lookup_token_t *tok)
79483c34adc5Sramat {
79493c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
79503c34adc5Sramat 	mdi_vhcache_client_t *cct;
79513c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
79523c34adc5Sramat 
79533c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
79543c34adc5Sramat 	if ((cct = lookup_vhcache_client(vhcache, ct_name, ct_addr, tok))
79553c34adc5Sramat 	    == NULL) {
79563c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
79573c34adc5Sramat 		return;
79583c34adc5Sramat 	}
79593c34adc5Sramat 
79603c34adc5Sramat 	/*
79613c34adc5Sramat 	 * to avoid unnecessary on-disk cache updates, first check if an
79623c34adc5Sramat 	 * update is really needed. If no update is needed simply return.
79633c34adc5Sramat 	 */
79643c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
79653c34adc5Sramat 		if ((cpi->cpi_pip != NULL &&
79663c34adc5Sramat 		    (cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST)) ||
79673c34adc5Sramat 		    (cpi->cpi_pip == NULL &&
79683c34adc5Sramat 		    !(cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST))) {
79693c34adc5Sramat 			break;
79703c34adc5Sramat 		}
79713c34adc5Sramat 	}
79723c34adc5Sramat 	if (cpi == NULL) {
79733c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
79743c34adc5Sramat 		return;
79753c34adc5Sramat 	}
79763c34adc5Sramat 
79773c34adc5Sramat 	if (rw_tryupgrade(&vhcache->vhcache_lock) == 0) {
79783c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
79793c34adc5Sramat 		rw_enter(&vhcache->vhcache_lock, RW_WRITER);
79803c34adc5Sramat 		if ((cct = lookup_vhcache_client(vhcache, ct_name, ct_addr,
79813c34adc5Sramat 		    tok)) == NULL) {
79823c34adc5Sramat 			rw_exit(&vhcache->vhcache_lock);
79833c34adc5Sramat 			return;
79843c34adc5Sramat 		}
79853c34adc5Sramat 	}
79863c34adc5Sramat 
79873c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
79883c34adc5Sramat 		if (cpi->cpi_pip != NULL)
79893c34adc5Sramat 			cpi->cpi_flags &= ~MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
79903c34adc5Sramat 		else
79913c34adc5Sramat 			cpi->cpi_flags |= MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
79923c34adc5Sramat 	}
79933c34adc5Sramat 	sort_vhcache_paths(cct);
79943c34adc5Sramat 
79953c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
79963c34adc5Sramat 	vhcache_dirty(vhc);
79973c34adc5Sramat }
79983c34adc5Sramat 
79993c34adc5Sramat /*
80003c34adc5Sramat  * Configure all specified paths of the client.
80013c34adc5Sramat  */
80023c34adc5Sramat static void
80033c34adc5Sramat config_client_paths_sync(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr,
80043c34adc5Sramat     mdi_phys_path_t *pp_head, mdi_vhcache_lookup_token_t *tok)
80053c34adc5Sramat {
80063c34adc5Sramat 	mdi_phys_path_t *pp;
80073c34adc5Sramat 
80083c34adc5Sramat 	for (pp = pp_head; pp != NULL; pp = pp->phys_path_next)
80093c34adc5Sramat 		(void) bus_config_one_phci_child(pp->phys_path);
80103c34adc5Sramat 	adjust_sort_vhcache_paths(vhc, ct_name, ct_addr, tok);
80113c34adc5Sramat }
80123c34adc5Sramat 
80133c34adc5Sramat /*
80143c34adc5Sramat  * Dequeue elements from vhci async client config list and bus configure
80153c34adc5Sramat  * their corresponding phci clients.
80163c34adc5Sramat  */
80173c34adc5Sramat static void
80183c34adc5Sramat config_client_paths_thread(void *arg)
80193c34adc5Sramat {
80203c34adc5Sramat 	mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
80213c34adc5Sramat 	mdi_async_client_config_t *acc;
80223c34adc5Sramat 	clock_t quit_at_ticks;
80233c34adc5Sramat 	clock_t idle_time = mdi_async_config_idle_time * TICKS_PER_SECOND;
80243c34adc5Sramat 	callb_cpr_t cprinfo;
80253c34adc5Sramat 
80263c34adc5Sramat 	CALLB_CPR_INIT(&cprinfo, &vhc->vhc_lock, callb_generic_cpr,
80273c34adc5Sramat 	    "mdi_config_client_paths");
80283c34adc5Sramat 
80293c34adc5Sramat 	for (; ; ) {
80303c34adc5Sramat 		quit_at_ticks = ddi_get_lbolt() + idle_time;
80313c34adc5Sramat 
80323c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
80333c34adc5Sramat 		while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
80343c34adc5Sramat 		    vhc->vhc_acc_list_head == NULL &&
80353c34adc5Sramat 		    ddi_get_lbolt() < quit_at_ticks) {
80363c34adc5Sramat 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
80373c34adc5Sramat 			(void) cv_timedwait(&vhc->vhc_cv, &vhc->vhc_lock,
80383c34adc5Sramat 			    quit_at_ticks);
80393c34adc5Sramat 			CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
80403c34adc5Sramat 		}
80413c34adc5Sramat 
80423c34adc5Sramat 		if ((vhc->vhc_flags & MDI_VHC_EXIT) ||
80433c34adc5Sramat 		    vhc->vhc_acc_list_head == NULL)
80443c34adc5Sramat 			goto out;
80453c34adc5Sramat 
80463c34adc5Sramat 		acc = vhc->vhc_acc_list_head;
80473c34adc5Sramat 		vhc->vhc_acc_list_head = acc->acc_next;
80483c34adc5Sramat 		if (vhc->vhc_acc_list_head == NULL)
80493c34adc5Sramat 			vhc->vhc_acc_list_tail = NULL;
80503c34adc5Sramat 		vhc->vhc_acc_count--;
80513c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
80523c34adc5Sramat 
80533c34adc5Sramat 		config_client_paths_sync(vhc, acc->acc_ct_name,
80543c34adc5Sramat 		    acc->acc_ct_addr, acc->acc_phclient_path_list_head,
80553c34adc5Sramat 		    &acc->acc_token);
80563c34adc5Sramat 
80573c34adc5Sramat 		free_async_client_config(acc);
80583c34adc5Sramat 	}
80593c34adc5Sramat 
80603c34adc5Sramat out:
80613c34adc5Sramat 	vhc->vhc_acc_thrcount--;
80623c34adc5Sramat 	/* CALLB_CPR_EXIT releases the vhc->vhc_lock */
80633c34adc5Sramat 	CALLB_CPR_EXIT(&cprinfo);
80643c34adc5Sramat }
80653c34adc5Sramat 
80663c34adc5Sramat /*
80673c34adc5Sramat  * Arrange for all the phci client paths (pp_head) for the specified client
80683c34adc5Sramat  * to be bus configured asynchronously by a thread.
80693c34adc5Sramat  */
80703c34adc5Sramat static void
80713c34adc5Sramat config_client_paths_async(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr,
80723c34adc5Sramat     mdi_phys_path_t *pp_head, mdi_vhcache_lookup_token_t *tok)
80733c34adc5Sramat {
80743c34adc5Sramat 	mdi_async_client_config_t *acc, *newacc;
80753c34adc5Sramat 	int create_thread;
80763c34adc5Sramat 
80773c34adc5Sramat 	if (pp_head == NULL)
80783c34adc5Sramat 		return;
80793c34adc5Sramat 
80803c34adc5Sramat 	if (mdi_mtc_off) {
80813c34adc5Sramat 		config_client_paths_sync(vhc, ct_name, ct_addr, pp_head, tok);
80823c34adc5Sramat 		free_phclient_path_list(pp_head);
80833c34adc5Sramat 		return;
80843c34adc5Sramat 	}
80853c34adc5Sramat 
80863c34adc5Sramat 	newacc = alloc_async_client_config(ct_name, ct_addr, pp_head, tok);
80873c34adc5Sramat 	ASSERT(newacc);
80883c34adc5Sramat 
80893c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
80903c34adc5Sramat 	for (acc = vhc->vhc_acc_list_head; acc != NULL; acc = acc->acc_next) {
80913c34adc5Sramat 		if (strcmp(ct_name, acc->acc_ct_name) == 0 &&
80923c34adc5Sramat 		    strcmp(ct_addr, acc->acc_ct_addr) == 0) {
80933c34adc5Sramat 			free_async_client_config(newacc);
80943c34adc5Sramat 			mutex_exit(&vhc->vhc_lock);
80953c34adc5Sramat 			return;
80963c34adc5Sramat 		}
80973c34adc5Sramat 	}
80983c34adc5Sramat 
80993c34adc5Sramat 	if (vhc->vhc_acc_list_head == NULL)
81003c34adc5Sramat 		vhc->vhc_acc_list_head = newacc;
81013c34adc5Sramat 	else
81023c34adc5Sramat 		vhc->vhc_acc_list_tail->acc_next = newacc;
81033c34adc5Sramat 	vhc->vhc_acc_list_tail = newacc;
81043c34adc5Sramat 	vhc->vhc_acc_count++;
81053c34adc5Sramat 	if (vhc->vhc_acc_count <= vhc->vhc_acc_thrcount) {
81063c34adc5Sramat 		cv_broadcast(&vhc->vhc_cv);
81073c34adc5Sramat 		create_thread = 0;
81083c34adc5Sramat 	} else {
81093c34adc5Sramat 		vhc->vhc_acc_thrcount++;
81103c34adc5Sramat 		create_thread = 1;
81113c34adc5Sramat 	}
81123c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
81133c34adc5Sramat 
81143c34adc5Sramat 	if (create_thread)
81153c34adc5Sramat 		(void) thread_create(NULL, 0, config_client_paths_thread, vhc,
81163c34adc5Sramat 		    0, &p0, TS_RUN, minclsyspri);
81173c34adc5Sramat }
81183c34adc5Sramat 
81193c34adc5Sramat /*
81203c34adc5Sramat  * Return number of online paths for the specified client.
81213c34adc5Sramat  */
81223c34adc5Sramat static int
81233c34adc5Sramat nonline_paths(mdi_vhcache_client_t *cct)
81243c34adc5Sramat {
81253c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
81263c34adc5Sramat 	int online_count = 0;
81273c34adc5Sramat 
81283c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
81293c34adc5Sramat 		if (cpi->cpi_pip != NULL) {
81303c34adc5Sramat 			MDI_PI_LOCK(cpi->cpi_pip);
81313c34adc5Sramat 			if (cpi->cpi_pip->pi_state == MDI_PATHINFO_STATE_ONLINE)
81323c34adc5Sramat 				online_count++;
81333c34adc5Sramat 			MDI_PI_UNLOCK(cpi->cpi_pip);
81343c34adc5Sramat 		}
81353c34adc5Sramat 	}
81363c34adc5Sramat 
81373c34adc5Sramat 	return (online_count);
81383c34adc5Sramat }
81393c34adc5Sramat 
81403c34adc5Sramat /*
81413c34adc5Sramat  * Bus configure all paths for the specified vhci client.
81423c34adc5Sramat  * If at least one path for the client is already online, the remaining paths
81433c34adc5Sramat  * will be configured asynchronously. Otherwise, it synchronously configures
81443c34adc5Sramat  * the paths until at least one path is online and then rest of the paths
81453c34adc5Sramat  * will be configured asynchronously.
81463c34adc5Sramat  */
81473c34adc5Sramat static void
81483c34adc5Sramat config_client_paths(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr)
81493c34adc5Sramat {
81503c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
81513c34adc5Sramat 	mdi_phys_path_t *pp_head, *pp;
81523c34adc5Sramat 	mdi_vhcache_client_t *cct;
81533c34adc5Sramat 	mdi_vhcache_lookup_token_t tok;
81543c34adc5Sramat 
81553c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
81563c34adc5Sramat 
81573c34adc5Sramat 	init_vhcache_lookup_token(&tok, NULL);
81583c34adc5Sramat 
81593c34adc5Sramat 	if (ct_name == NULL || ct_addr == NULL ||
81603c34adc5Sramat 	    (cct = lookup_vhcache_client(vhcache, ct_name, ct_addr, &tok))
81613c34adc5Sramat 	    == NULL ||
81623c34adc5Sramat 	    (pp_head = build_phclient_path_list(cct, ct_name)) == NULL) {
81633c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
81643c34adc5Sramat 		return;
81653c34adc5Sramat 	}
81663c34adc5Sramat 
81673c34adc5Sramat 	/* if at least one path is online, configure the rest asynchronously */
81683c34adc5Sramat 	if (nonline_paths(cct) > 0) {
81693c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
81703c34adc5Sramat 		config_client_paths_async(vhc, ct_name, ct_addr, pp_head, &tok);
81713c34adc5Sramat 		return;
81723c34adc5Sramat 	}
81733c34adc5Sramat 
81743c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
81753c34adc5Sramat 
81763c34adc5Sramat 	for (pp = pp_head; pp != NULL; pp = pp->phys_path_next) {
81773c34adc5Sramat 		if (bus_config_one_phci_child(pp->phys_path) == MDI_SUCCESS) {
81783c34adc5Sramat 			rw_enter(&vhcache->vhcache_lock, RW_READER);
81793c34adc5Sramat 
81803c34adc5Sramat 			if ((cct = lookup_vhcache_client(vhcache, ct_name,
81813c34adc5Sramat 			    ct_addr, &tok)) == NULL) {
81823c34adc5Sramat 				rw_exit(&vhcache->vhcache_lock);
81833c34adc5Sramat 				goto out;
81843c34adc5Sramat 			}
81853c34adc5Sramat 
81863c34adc5Sramat 			if (nonline_paths(cct) > 0 &&
81873c34adc5Sramat 			    pp->phys_path_next != NULL) {
81883c34adc5Sramat 				rw_exit(&vhcache->vhcache_lock);
81893c34adc5Sramat 				config_client_paths_async(vhc, ct_name, ct_addr,
81903c34adc5Sramat 				    pp->phys_path_next, &tok);
81913c34adc5Sramat 				pp->phys_path_next = NULL;
81923c34adc5Sramat 				goto out;
81933c34adc5Sramat 			}
81943c34adc5Sramat 
81953c34adc5Sramat 			rw_exit(&vhcache->vhcache_lock);
81963c34adc5Sramat 		}
81973c34adc5Sramat 	}
81983c34adc5Sramat 
81993c34adc5Sramat 	adjust_sort_vhcache_paths(vhc, ct_name, ct_addr, &tok);
82003c34adc5Sramat out:
82013c34adc5Sramat 	free_phclient_path_list(pp_head);
82023c34adc5Sramat }
82033c34adc5Sramat 
82043c34adc5Sramat static void
82053c34adc5Sramat single_threaded_vhconfig_enter(mdi_vhci_config_t *vhc)
82063c34adc5Sramat {
82073c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
82083c34adc5Sramat 	while (vhc->vhc_flags & MDI_VHC_SINGLE_THREADED)
82093c34adc5Sramat 		cv_wait(&vhc->vhc_cv, &vhc->vhc_lock);
82103c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_SINGLE_THREADED;
82113c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
82123c34adc5Sramat }
82133c34adc5Sramat 
82143c34adc5Sramat static void
82153c34adc5Sramat single_threaded_vhconfig_exit(mdi_vhci_config_t *vhc)
82163c34adc5Sramat {
82173c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
82183c34adc5Sramat 	vhc->vhc_flags &= ~MDI_VHC_SINGLE_THREADED;
82193c34adc5Sramat 	cv_broadcast(&vhc->vhc_cv);
82203c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
82213c34adc5Sramat }
82223c34adc5Sramat 
82233c34adc5Sramat /*
82243c34adc5Sramat  * Attach the phci driver instances associated with the vhci:
82253c34adc5Sramat  * If root is mounted attach all phci driver instances.
82263c34adc5Sramat  * If root is not mounted, attach the instances of only those phci
82273c34adc5Sramat  * drivers that have the root support.
82283c34adc5Sramat  */
82293c34adc5Sramat static void
823067e56d35Sramat attach_phci_drivers(mdi_vhci_config_t *vhc)
82313c34adc5Sramat {
82323c34adc5Sramat 	int  i;
82333c34adc5Sramat 	major_t m;
82343c34adc5Sramat 
82353c34adc5Sramat 	for (i = 0; i < vhc->vhc_nphci_drivers; i++) {
823667e56d35Sramat 		if (modrootloaded == 0 &&
82373c34adc5Sramat 		    vhc->vhc_phci_driver_list[i].phdriver_root_support == 0)
82383c34adc5Sramat 			continue;
82393c34adc5Sramat 
82403c34adc5Sramat 		m = ddi_name_to_major(
82413c34adc5Sramat 		    vhc->vhc_phci_driver_list[i].phdriver_name);
82423c34adc5Sramat 		if (m != (major_t)-1) {
82433c34adc5Sramat 			if (ddi_hold_installed_driver(m) != NULL)
82443c34adc5Sramat 				ddi_rele_driver(m);
82453c34adc5Sramat 		}
82463c34adc5Sramat 	}
82473c34adc5Sramat }
82483c34adc5Sramat 
82493c34adc5Sramat /*
82503c34adc5Sramat  * Build vhci cache:
82513c34adc5Sramat  *
82523c34adc5Sramat  * Attach phci driver instances and then drive BUS_CONFIG_ALL on
82533c34adc5Sramat  * the phci driver instances. During this process the cache gets built.
82543c34adc5Sramat  *
825567e56d35Sramat  * Cache is built fully if the root is mounted.
82563c34adc5Sramat  * If the root is not mounted, phci drivers that do not have root support
82573c34adc5Sramat  * are not attached. As a result the cache is built partially. The entries
82583c34adc5Sramat  * in the cache reflect only those phci drivers that have root support.
82593c34adc5Sramat  */
826067e56d35Sramat static int
826167e56d35Sramat build_vhci_cache(mdi_vhci_config_t *vhc)
82623c34adc5Sramat {
82633c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
82643c34adc5Sramat 
826567e56d35Sramat 	single_threaded_vhconfig_enter(vhc);
826667e56d35Sramat 
82673c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
82683c34adc5Sramat 	if (vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE) {
82693c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
827067e56d35Sramat 		single_threaded_vhconfig_exit(vhc);
827167e56d35Sramat 		return (0);
82723c34adc5Sramat 	}
82733c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
82743c34adc5Sramat 
827567e56d35Sramat 	attach_phci_drivers(vhc);
82763c34adc5Sramat 	bus_config_all_phcis(vhcache, NDI_DRV_CONF_REPROBE | NDI_NO_EVENT,
82773c34adc5Sramat 	    BUS_CONFIG_ALL, (major_t)-1);
82783c34adc5Sramat 
82793c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
82803c34adc5Sramat 	vhcache->vhcache_flags |= MDI_VHCI_CACHE_SETUP_DONE;
82813c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
828267e56d35Sramat 
828367e56d35Sramat 	single_threaded_vhconfig_exit(vhc);
82843c34adc5Sramat 	vhcache_dirty(vhc);
828567e56d35Sramat 	return (1);
82863c34adc5Sramat }
82873c34adc5Sramat 
82883c34adc5Sramat /*
828967e56d35Sramat  * Determine if discovery of paths is needed.
82903c34adc5Sramat  */
82913c34adc5Sramat static int
829267e56d35Sramat vhcache_do_discovery(mdi_vhci_config_t *vhc)
82933c34adc5Sramat {
829467e56d35Sramat 	int rv = 1;
829567e56d35Sramat 
829667e56d35Sramat 	mutex_enter(&vhc->vhc_lock);
829767e56d35Sramat 	if (i_ddi_io_initialized() == 0) {
829867e56d35Sramat 		if (vhc->vhc_path_discovery_boot > 0) {
829967e56d35Sramat 			vhc->vhc_path_discovery_boot--;
830067e56d35Sramat 			goto out;
830167e56d35Sramat 		}
830267e56d35Sramat 	} else {
830367e56d35Sramat 		if (vhc->vhc_path_discovery_postboot > 0) {
830467e56d35Sramat 			vhc->vhc_path_discovery_postboot--;
830567e56d35Sramat 			goto out;
830667e56d35Sramat 		}
830767e56d35Sramat 	}
830867e56d35Sramat 
830967e56d35Sramat 	/*
831067e56d35Sramat 	 * Do full path discovery at most once per mdi_path_discovery_interval.
831167e56d35Sramat 	 * This is to avoid a series of full path discoveries when opening
831267e56d35Sramat 	 * stale /dev/[r]dsk links.
831367e56d35Sramat 	 */
831467e56d35Sramat 	if (mdi_path_discovery_interval != -1 &&
831567e56d35Sramat 	    lbolt64 >= vhc->vhc_path_discovery_cutoff_time)
831667e56d35Sramat 		goto out;
831767e56d35Sramat 
831867e56d35Sramat 	rv = 0;
831967e56d35Sramat out:
832067e56d35Sramat 	mutex_exit(&vhc->vhc_lock);
832167e56d35Sramat 	return (rv);
832267e56d35Sramat }
832367e56d35Sramat 
832467e56d35Sramat /*
832567e56d35Sramat  * Discover all paths:
832667e56d35Sramat  *
832767e56d35Sramat  * Attach phci driver instances and then drive BUS_CONFIG_ALL on all the phci
832867e56d35Sramat  * driver instances. During this process all paths will be discovered.
832967e56d35Sramat  */
833067e56d35Sramat static int
833167e56d35Sramat vhcache_discover_paths(mdi_vhci_config_t *vhc)
833267e56d35Sramat {
833367e56d35Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
833467e56d35Sramat 	int rv = 0;
83353c34adc5Sramat 
83363c34adc5Sramat 	single_threaded_vhconfig_enter(vhc);
83373c34adc5Sramat 
833867e56d35Sramat 	if (vhcache_do_discovery(vhc)) {
833967e56d35Sramat 		attach_phci_drivers(vhc);
834067e56d35Sramat 		bus_config_all_phcis(vhcache, NDI_DRV_CONF_REPROBE |
834167e56d35Sramat 		    NDI_NO_EVENT, BUS_CONFIG_ALL, (major_t)-1);
834267e56d35Sramat 
83433c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
834467e56d35Sramat 		vhc->vhc_path_discovery_cutoff_time = lbolt64 +
834567e56d35Sramat 		    mdi_path_discovery_interval * TICKS_PER_SECOND;
83463c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
834767e56d35Sramat 		rv = 1;
83483c34adc5Sramat 	}
83493c34adc5Sramat 
83503c34adc5Sramat 	single_threaded_vhconfig_exit(vhc);
83513c34adc5Sramat 	return (rv);
83523c34adc5Sramat }
83533c34adc5Sramat 
83543c34adc5Sramat /*
83553c34adc5Sramat  * Generic vhci bus config implementation:
83563c34adc5Sramat  *
83573c34adc5Sramat  * Parameters
83583c34adc5Sramat  *	vdip	vhci dip
83593c34adc5Sramat  *	flags	bus config flags
83603c34adc5Sramat  *	op	bus config operation
83613c34adc5Sramat  *	The remaining parameters are bus config operation specific
83623c34adc5Sramat  *
83633c34adc5Sramat  * for BUS_CONFIG_ONE
83643c34adc5Sramat  *	arg	pointer to name@addr
83653c34adc5Sramat  *	child	upon successful return from this function, *child will be
83663c34adc5Sramat  *		set to the configured and held devinfo child node of vdip.
83673c34adc5Sramat  *	ct_addr	pointer to client address (i.e. GUID)
83683c34adc5Sramat  *
83693c34adc5Sramat  * for BUS_CONFIG_DRIVER
83703c34adc5Sramat  *	arg	major number of the driver
83713c34adc5Sramat  *	child and ct_addr parameters are ignored
83723c34adc5Sramat  *
83733c34adc5Sramat  * for BUS_CONFIG_ALL
83743c34adc5Sramat  *	arg, child, and ct_addr parameters are ignored
83753c34adc5Sramat  *
83763c34adc5Sramat  * Note that for the rest of the bus config operations, this function simply
83773c34adc5Sramat  * calls the framework provided default bus config routine.
83783c34adc5Sramat  */
83793c34adc5Sramat int
83803c34adc5Sramat mdi_vhci_bus_config(dev_info_t *vdip, uint_t flags, ddi_bus_config_op_t op,
83813c34adc5Sramat     void *arg, dev_info_t **child, char *ct_addr)
83823c34adc5Sramat {
83833c34adc5Sramat 	mdi_vhci_t *vh = i_devi_get_vhci(vdip);
83843c34adc5Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
83853c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
838667e56d35Sramat 	int rv = 0;
838767e56d35Sramat 	int params_valid = 0;
83883c34adc5Sramat 	char *cp;
83893c34adc5Sramat 
83903c34adc5Sramat 	/*
8391*144dfaa9Scth 	 * To bus config vhcis we relay operation, possibly using another
8392*144dfaa9Scth 	 * thread, to phcis. The phci driver then interacts with MDI to cause
8393*144dfaa9Scth 	 * vhci child nodes to be enumerated under the vhci node.  Adding a
8394*144dfaa9Scth 	 * vhci child requires an ndi_devi_enter of the vhci. Since another
8395*144dfaa9Scth 	 * thread may be adding the child, to avoid deadlock we can't wait
8396*144dfaa9Scth 	 * for the relayed operations to complete if we have already entered
8397*144dfaa9Scth 	 * the vhci node.
83983c34adc5Sramat 	 */
83993c34adc5Sramat 	if (DEVI_BUSY_OWNED(vdip)) {
8400*144dfaa9Scth 		MDI_DEBUG(2, (CE_NOTE, vdip, "!MDI: vhci bus config: "
8401*144dfaa9Scth 		    "vhci dip is busy owned %p\n", (void *)vdip));
84023c34adc5Sramat 		goto default_bus_config;
84033c34adc5Sramat 	}
84043c34adc5Sramat 
84053c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
84063c34adc5Sramat 	if (!(vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE)) {
84073c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
840867e56d35Sramat 		rv = build_vhci_cache(vhc);
84093c34adc5Sramat 		rw_enter(&vhcache->vhcache_lock, RW_READER);
84103c34adc5Sramat 	}
84113c34adc5Sramat 
84123c34adc5Sramat 	switch (op) {
84133c34adc5Sramat 	case BUS_CONFIG_ONE:
841467e56d35Sramat 		if (arg != NULL && ct_addr != NULL) {
84153c34adc5Sramat 			/* extract node name */
84163c34adc5Sramat 			cp = (char *)arg;
84173c34adc5Sramat 			while (*cp != '\0' && *cp != '@')
84183c34adc5Sramat 				cp++;
84193c34adc5Sramat 			if (*cp == '@') {
842067e56d35Sramat 				params_valid = 1;
84213c34adc5Sramat 				*cp = '\0';
84223c34adc5Sramat 				config_client_paths(vhc, (char *)arg, ct_addr);
842367e56d35Sramat 				/* config_client_paths() releases cache_lock */
84243c34adc5Sramat 				*cp = '@';
842567e56d35Sramat 				break;
842667e56d35Sramat 			}
842767e56d35Sramat 		}
842867e56d35Sramat 
84293c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
84303c34adc5Sramat 		break;
84313c34adc5Sramat 
84323c34adc5Sramat 	case BUS_CONFIG_DRIVER:
84333c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
843467e56d35Sramat 		if (rv == 0)
843567e56d35Sramat 			st_bus_config_all_phcis(vhc, flags, op,
84363c34adc5Sramat 			    (major_t)(uintptr_t)arg);
84373c34adc5Sramat 		break;
84383c34adc5Sramat 
84393c34adc5Sramat 	case BUS_CONFIG_ALL:
84403c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
844167e56d35Sramat 		if (rv == 0)
844267e56d35Sramat 			st_bus_config_all_phcis(vhc, flags, op, -1);
84433c34adc5Sramat 		break;
84443c34adc5Sramat 
84453c34adc5Sramat 	default:
84463c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
84473c34adc5Sramat 		break;
84483c34adc5Sramat 	}
84493c34adc5Sramat 
84503c34adc5Sramat 
84513c34adc5Sramat default_bus_config:
84523c34adc5Sramat 	/*
84533c34adc5Sramat 	 * All requested child nodes are enumerated under the vhci.
84543c34adc5Sramat 	 * Now configure them.
84553c34adc5Sramat 	 */
84563c34adc5Sramat 	if (ndi_busop_bus_config(vdip, flags, op, arg, child, 0) ==
84573c34adc5Sramat 	    NDI_SUCCESS) {
84583c34adc5Sramat 		return (MDI_SUCCESS);
845967e56d35Sramat 	} else if (op == BUS_CONFIG_ONE && rv == 0 && params_valid) {
846067e56d35Sramat 		/* discover all paths and try configuring again */
846167e56d35Sramat 		if (vhcache_discover_paths(vhc) &&
846267e56d35Sramat 		    ndi_busop_bus_config(vdip, flags, op, arg, child, 0) ==
846367e56d35Sramat 		    NDI_SUCCESS)
846467e56d35Sramat 			return (MDI_SUCCESS);
84653c34adc5Sramat 	}
84663c34adc5Sramat 
84673c34adc5Sramat 	return (MDI_FAILURE);
84683c34adc5Sramat }
84693c34adc5Sramat 
84703c34adc5Sramat /*
84713c34adc5Sramat  * Read the on-disk vhci cache into an nvlist for the specified vhci class.
84723c34adc5Sramat  */
84733c34adc5Sramat static nvlist_t *
84743c34adc5Sramat read_on_disk_vhci_cache(char *vhci_class)
84753c34adc5Sramat {
84763c34adc5Sramat 	nvlist_t *nvl;
84773c34adc5Sramat 	int err;
84783c34adc5Sramat 	char *filename;
84793c34adc5Sramat 
84803c34adc5Sramat 	filename = vhclass2vhcache_filename(vhci_class);
84813c34adc5Sramat 
84823c34adc5Sramat 	if ((err = fread_nvlist(filename, &nvl)) == 0) {
84833c34adc5Sramat 		kmem_free(filename, strlen(filename) + 1);
84843c34adc5Sramat 		return (nvl);
84853c34adc5Sramat 	} else if (err == EIO)
84863c34adc5Sramat 		cmn_err(CE_WARN, "%s: I/O error, will recreate\n", filename);
84873c34adc5Sramat 	else if (err == EINVAL)
84883c34adc5Sramat 		cmn_err(CE_WARN,
84893c34adc5Sramat 		    "%s: data file corrupted, will recreate\n", filename);
84903c34adc5Sramat 
84913c34adc5Sramat 	kmem_free(filename, strlen(filename) + 1);
84923c34adc5Sramat 	return (NULL);
84933c34adc5Sramat }
84943c34adc5Sramat 
84953c34adc5Sramat /*
84963c34adc5Sramat  * Read on-disk vhci cache into nvlists for all vhci classes.
84973c34adc5Sramat  * Called during booting by i_ddi_read_devices_files().
84983c34adc5Sramat  */
84993c34adc5Sramat void
85003c34adc5Sramat mdi_read_devices_files(void)
85013c34adc5Sramat {
85023c34adc5Sramat 	int i;
85033c34adc5Sramat 
85043c34adc5Sramat 	for (i = 0; i < N_VHCI_CLASSES; i++)
85053c34adc5Sramat 		vhcache_nvl[i] = read_on_disk_vhci_cache(vhci_class_list[i]);
85063c34adc5Sramat }
85073c34adc5Sramat 
85083c34adc5Sramat /*
85093c34adc5Sramat  * Remove all stale entries from vhci cache.
85103c34adc5Sramat  */
85113c34adc5Sramat static void
85123c34adc5Sramat clean_vhcache(mdi_vhci_config_t *vhc)
85133c34adc5Sramat {
85143c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
85153c34adc5Sramat 	mdi_vhcache_phci_t *cphci, *cphci_head, *cphci_next;
85163c34adc5Sramat 	mdi_vhcache_client_t *cct, *cct_head, *cct_next;
85173c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi, *cpi_head, *cpi_next;
85183c34adc5Sramat 
85193c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
85203c34adc5Sramat 
85213c34adc5Sramat 	cct_head = vhcache->vhcache_client_head;
85223c34adc5Sramat 	vhcache->vhcache_client_head = vhcache->vhcache_client_tail = NULL;
85233c34adc5Sramat 	for (cct = cct_head; cct != NULL; cct = cct_next) {
85243c34adc5Sramat 		cct_next = cct->cct_next;
85253c34adc5Sramat 
85263c34adc5Sramat 		cpi_head = cct->cct_cpi_head;
85273c34adc5Sramat 		cct->cct_cpi_head = cct->cct_cpi_tail = NULL;
85283c34adc5Sramat 		for (cpi = cpi_head; cpi != NULL; cpi = cpi_next) {
85293c34adc5Sramat 			cpi_next = cpi->cpi_next;
85303c34adc5Sramat 			if (cpi->cpi_pip != NULL) {
85313c34adc5Sramat 				ASSERT(cpi->cpi_cphci->cphci_phci != NULL);
85323c34adc5Sramat 				enqueue_tail_vhcache_pathinfo(cct, cpi);
85333c34adc5Sramat 			} else
85343c34adc5Sramat 				free_vhcache_pathinfo(cpi);
85353c34adc5Sramat 		}
85363c34adc5Sramat 
85373c34adc5Sramat 		if (cct->cct_cpi_head != NULL)
85383c34adc5Sramat 			enqueue_vhcache_client(vhcache, cct);
85393c34adc5Sramat 		else {
85403c34adc5Sramat 			(void) mod_hash_destroy(vhcache->vhcache_client_hash,
85413c34adc5Sramat 			    (mod_hash_key_t)cct->cct_name_addr);
85423c34adc5Sramat 			free_vhcache_client(cct);
85433c34adc5Sramat 		}
85443c34adc5Sramat 	}
85453c34adc5Sramat 
85463c34adc5Sramat 	cphci_head = vhcache->vhcache_phci_head;
85473c34adc5Sramat 	vhcache->vhcache_phci_head = vhcache->vhcache_phci_tail = NULL;
85483c34adc5Sramat 	for (cphci = cphci_head; cphci != NULL; cphci = cphci_next) {
85493c34adc5Sramat 		cphci_next = cphci->cphci_next;
85503c34adc5Sramat 		if (cphci->cphci_phci != NULL)
85513c34adc5Sramat 			enqueue_vhcache_phci(vhcache, cphci);
85523c34adc5Sramat 		else
85533c34adc5Sramat 			free_vhcache_phci(cphci);
85543c34adc5Sramat 	}
85553c34adc5Sramat 
85563c34adc5Sramat 	vhcache->vhcache_clean_time = lbolt64;
85573c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
85583c34adc5Sramat 	vhcache_dirty(vhc);
85593c34adc5Sramat }
85603c34adc5Sramat 
85613c34adc5Sramat /*
85623c34adc5Sramat  * Remove all stale entries from vhci cache.
85633c34adc5Sramat  * Called by i_ddi_clean_devices_files() during the execution of devfsadm -C
85643c34adc5Sramat  */
85653c34adc5Sramat void
85663c34adc5Sramat mdi_clean_vhcache(void)
85673c34adc5Sramat {
85683c34adc5Sramat 	mdi_vhci_t *vh;
85693c34adc5Sramat 
85703c34adc5Sramat 	mutex_enter(&mdi_mutex);
85713c34adc5Sramat 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
85723c34adc5Sramat 		vh->vh_refcnt++;
85733c34adc5Sramat 		mutex_exit(&mdi_mutex);
85743c34adc5Sramat 		clean_vhcache(vh->vh_config);
85753c34adc5Sramat 		mutex_enter(&mdi_mutex);
85763c34adc5Sramat 		vh->vh_refcnt--;
85773c34adc5Sramat 	}
85783c34adc5Sramat 	mutex_exit(&mdi_mutex);
85793c34adc5Sramat }
85808c4f8890Srs135747 
85818c4f8890Srs135747 /*
85828c4f8890Srs135747  * mdi_vhci_walk_clients():
85838c4f8890Srs135747  *		Walker routine to traverse client dev_info nodes
85848c4f8890Srs135747  * ddi_walk_devs(ddi_get_child(vdip), f, arg) returns the entire tree
85858c4f8890Srs135747  * below the client, including nexus devices, which we dont want.
85868c4f8890Srs135747  * So we just traverse the immediate siblings, starting from 1st client.
85878c4f8890Srs135747  */
85888c4f8890Srs135747 void
85898c4f8890Srs135747 mdi_vhci_walk_clients(dev_info_t *vdip,
85908c4f8890Srs135747     int (*f)(dev_info_t *, void *), void *arg)
85918c4f8890Srs135747 {
8592*144dfaa9Scth 	mdi_vhci_t	*vh = i_devi_get_vhci(vdip);
85938c4f8890Srs135747 	dev_info_t	*cdip;
85948c4f8890Srs135747 	mdi_client_t	*ct;
85958c4f8890Srs135747 
8596*144dfaa9Scth 	MDI_VHCI_CLIENT_LOCK(vh);
85978c4f8890Srs135747 	cdip = ddi_get_child(vdip);
85988c4f8890Srs135747 	while (cdip) {
85998c4f8890Srs135747 		ct = i_devi_get_client(cdip);
86008c4f8890Srs135747 		MDI_CLIENT_LOCK(ct);
86018c4f8890Srs135747 
8602*144dfaa9Scth 		if (((*f)(cdip, arg)) == DDI_WALK_CONTINUE)
86038c4f8890Srs135747 			cdip = ddi_get_next_sibling(cdip);
8604*144dfaa9Scth 		else
8605*144dfaa9Scth 			cdip = NULL;
86068c4f8890Srs135747 
86078c4f8890Srs135747 		MDI_CLIENT_UNLOCK(ct);
86088c4f8890Srs135747 	}
8609*144dfaa9Scth 	MDI_VHCI_CLIENT_UNLOCK(vh);
86108c4f8890Srs135747 }
86118c4f8890Srs135747 
86128c4f8890Srs135747 /*
86138c4f8890Srs135747  * mdi_vhci_walk_phcis():
86148c4f8890Srs135747  *		Walker routine to traverse phci dev_info nodes
86158c4f8890Srs135747  */
86168c4f8890Srs135747 void
86178c4f8890Srs135747 mdi_vhci_walk_phcis(dev_info_t *vdip,
86188c4f8890Srs135747     int (*f)(dev_info_t *, void *), void *arg)
86198c4f8890Srs135747 {
8620*144dfaa9Scth 	mdi_vhci_t	*vh = i_devi_get_vhci(vdip);
8621*144dfaa9Scth 	mdi_phci_t	*ph, *next;
86228c4f8890Srs135747 
8623*144dfaa9Scth 	MDI_VHCI_PHCI_LOCK(vh);
86248c4f8890Srs135747 	ph = vh->vh_phci_head;
86258c4f8890Srs135747 	while (ph) {
86268c4f8890Srs135747 		MDI_PHCI_LOCK(ph);
86278c4f8890Srs135747 
8628*144dfaa9Scth 		if (((*f)(ph->ph_dip, arg)) == DDI_WALK_CONTINUE)
8629*144dfaa9Scth 			next = ph->ph_next;
8630*144dfaa9Scth 		else
8631*144dfaa9Scth 			next = NULL;
86328c4f8890Srs135747 
86338c4f8890Srs135747 		MDI_PHCI_UNLOCK(ph);
8634*144dfaa9Scth 		ph = next;
86358c4f8890Srs135747 	}
8636*144dfaa9Scth 	MDI_VHCI_PHCI_UNLOCK(vh);
86378c4f8890Srs135747 }
86388c4f8890Srs135747 
86398c4f8890Srs135747 
86408c4f8890Srs135747 /*
86418c4f8890Srs135747  * mdi_walk_vhcis():
86428c4f8890Srs135747  *		Walker routine to traverse vhci dev_info nodes
86438c4f8890Srs135747  */
86448c4f8890Srs135747 void
86458c4f8890Srs135747 mdi_walk_vhcis(int (*f)(dev_info_t *, void *), void *arg)
86468c4f8890Srs135747 {
86478c4f8890Srs135747 	mdi_vhci_t	*vh = NULL;
86488c4f8890Srs135747 
86498c4f8890Srs135747 	mutex_enter(&mdi_mutex);
86508c4f8890Srs135747 	/*
86518c4f8890Srs135747 	 * Scan for already registered vhci
86528c4f8890Srs135747 	 */
86538c4f8890Srs135747 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
86548c4f8890Srs135747 		vh->vh_refcnt++;
86558c4f8890Srs135747 		mutex_exit(&mdi_mutex);
86568c4f8890Srs135747 		if (((*f)(vh->vh_dip, arg)) != DDI_WALK_CONTINUE) {
86578c4f8890Srs135747 			mutex_enter(&mdi_mutex);
86588c4f8890Srs135747 			vh->vh_refcnt--;
86598c4f8890Srs135747 			break;
86608c4f8890Srs135747 		} else {
86618c4f8890Srs135747 			mutex_enter(&mdi_mutex);
86628c4f8890Srs135747 			vh->vh_refcnt--;
86638c4f8890Srs135747 		}
86648c4f8890Srs135747 	}
86658c4f8890Srs135747 
86668c4f8890Srs135747 	mutex_exit(&mdi_mutex);
86678c4f8890Srs135747 }
86688c4f8890Srs135747 
86698c4f8890Srs135747 /*
86708c4f8890Srs135747  * i_mdi_log_sysevent():
86718c4f8890Srs135747  *		Logs events for pickup by syseventd
86728c4f8890Srs135747  */
86738c4f8890Srs135747 static void
86748c4f8890Srs135747 i_mdi_log_sysevent(dev_info_t *dip, char *ph_vh_class, char *subclass)
86758c4f8890Srs135747 {
86768c4f8890Srs135747 	char		*path_name;
86778c4f8890Srs135747 	nvlist_t	*attr_list;
86788c4f8890Srs135747 
86798c4f8890Srs135747 	if (nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE,
86808c4f8890Srs135747 	    KM_SLEEP) != DDI_SUCCESS) {
86818c4f8890Srs135747 		goto alloc_failed;
86828c4f8890Srs135747 	}
86838c4f8890Srs135747 
86848c4f8890Srs135747 	path_name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
86858c4f8890Srs135747 	(void) ddi_pathname(dip, path_name);
86868c4f8890Srs135747 
86878c4f8890Srs135747 	if (nvlist_add_string(attr_list, DDI_DRIVER_NAME,
86888c4f8890Srs135747 	    ddi_driver_name(dip)) != DDI_SUCCESS) {
86898c4f8890Srs135747 		goto error;
86908c4f8890Srs135747 	}
86918c4f8890Srs135747 
86928c4f8890Srs135747 	if (nvlist_add_int32(attr_list, DDI_DRIVER_MAJOR,
86938c4f8890Srs135747 	    (int32_t)ddi_driver_major(dip)) != DDI_SUCCESS) {
86948c4f8890Srs135747 		goto error;
86958c4f8890Srs135747 	}
86968c4f8890Srs135747 
86978c4f8890Srs135747 	if (nvlist_add_int32(attr_list, DDI_INSTANCE,
86988c4f8890Srs135747 	    (int32_t)ddi_get_instance(dip)) != DDI_SUCCESS) {
86998c4f8890Srs135747 		goto error;
87008c4f8890Srs135747 	}
87018c4f8890Srs135747 
87028c4f8890Srs135747 	if (nvlist_add_string(attr_list, DDI_PATHNAME,
87038c4f8890Srs135747 	    path_name) != DDI_SUCCESS) {
87048c4f8890Srs135747 		goto error;
87058c4f8890Srs135747 	}
87068c4f8890Srs135747 
87078c4f8890Srs135747 	if (nvlist_add_string(attr_list, DDI_CLASS,
87088c4f8890Srs135747 	    ph_vh_class) != DDI_SUCCESS) {
87098c4f8890Srs135747 		goto error;
87108c4f8890Srs135747 	}
87118c4f8890Srs135747 
87128c4f8890Srs135747 	(void) ddi_log_sysevent(dip, DDI_VENDOR_SUNW, EC_DDI, subclass,
87138c4f8890Srs135747 	    attr_list, NULL, DDI_SLEEP);
87148c4f8890Srs135747 
87158c4f8890Srs135747 error:
87168c4f8890Srs135747 	kmem_free(path_name, MAXPATHLEN);
87178c4f8890Srs135747 	nvlist_free(attr_list);
87188c4f8890Srs135747 	return;
87198c4f8890Srs135747 
87208c4f8890Srs135747 alloc_failed:
87218c4f8890Srs135747 	MDI_DEBUG(1, (CE_WARN, dip,
87228c4f8890Srs135747 	    "!i_mdi_log_sysevent: Unable to send sysevent"));
87238c4f8890Srs135747 }
8724