xref: /titanic_51/usr/src/uts/common/os/sunmdi.c (revision bf002425f517afdc1d8b6a9602e59910eeee05aa)
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 /*
22*bf002425SStephen Hanson  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * Multipath driver interface (MDI) implementation; see mdi_impl.h for a more
277c478bd9Sstevel@tonic-gate  * detailed discussion of the overall mpxio architecture.
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * Default locking order:
307c478bd9Sstevel@tonic-gate  *
315e3986cbScth  * _NOTE(LOCK_ORDER(mdi_mutex, mdi_vhci:vh_phci_mutex);
325e3986cbScth  * _NOTE(LOCK_ORDER(mdi_mutex, mdi_vhci:vh_client_mutex);
335e3986cbScth  * _NOTE(LOCK_ORDER(mdi_vhci:vh_phci_mutex, mdi_phci::ph_mutex);
345e3986cbScth  * _NOTE(LOCK_ORDER(mdi_vhci:vh_client_mutex, mdi_client::ct_mutex);
357c478bd9Sstevel@tonic-gate  * _NOTE(LOCK_ORDER(mdi_phci::ph_mutex mdi_pathinfo::pi_mutex))
367c478bd9Sstevel@tonic-gate  * _NOTE(LOCK_ORDER(mdi_phci::ph_mutex mdi_client::ct_mutex))
377c478bd9Sstevel@tonic-gate  * _NOTE(LOCK_ORDER(mdi_client::ct_mutex mdi_pathinfo::pi_mutex))
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <sys/note.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
437c478bd9Sstevel@tonic-gate #include <sys/param.h>
447c478bd9Sstevel@tonic-gate #include <sys/errno.h>
457c478bd9Sstevel@tonic-gate #include <sys/uio.h>
467c478bd9Sstevel@tonic-gate #include <sys/buf.h>
477c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
487c478bd9Sstevel@tonic-gate #include <sys/open.h>
497c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
507c478bd9Sstevel@tonic-gate #include <sys/poll.h>
517c478bd9Sstevel@tonic-gate #include <sys/conf.h>
527c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
537c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
547c478bd9Sstevel@tonic-gate #include <sys/stat.h>
557c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
567c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
577c478bd9Sstevel@tonic-gate #include <sys/ddipropdefs.h>
587c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
597c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
607c478bd9Sstevel@tonic-gate #include <sys/promif.h>
617c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h>
627c478bd9Sstevel@tonic-gate #include <sys/mdi_impldefs.h>
637c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
647c478bd9Sstevel@tonic-gate #include <sys/epm.h>
657c478bd9Sstevel@tonic-gate #include <sys/sunpm.h>
663c34adc5Sramat #include <sys/modhash.h>
678c4f8890Srs135747 #include <sys/disp.h>
688c4f8890Srs135747 #include <sys/autoconf.h>
69f7209cf2Spramodbg #include <sys/sysmacros.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #ifdef	DEBUG
727c478bd9Sstevel@tonic-gate #include <sys/debug.h>
737c478bd9Sstevel@tonic-gate int	mdi_debug = 1;
745e3986cbScth int	mdi_debug_logonly = 0;
754c06356bSdh142964 #define	MDI_DEBUG(dbglevel, pargs) if (mdi_debug >= (dbglevel))	i_mdi_log pargs
764c06356bSdh142964 #define	MDI_WARN	CE_WARN, __func__
774c06356bSdh142964 #define	MDI_NOTE	CE_NOTE, __func__
784c06356bSdh142964 #define	MDI_CONT	CE_CONT, __func__
794c06356bSdh142964 static void i_mdi_log(int, const char *, dev_info_t *, const char *, ...);
807c478bd9Sstevel@tonic-gate #else	/* !DEBUG */
814c06356bSdh142964 #define	MDI_DEBUG(dbglevel, pargs)
827c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
834c06356bSdh142964 int	mdi_debug_consoleonly = 0;
8496c4a178SChris Horne int	mdi_delay = 3;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate extern pri_t	minclsyspri;
877c478bd9Sstevel@tonic-gate extern int	modrootloaded;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * Global mutex:
915e3986cbScth  * Protects vHCI list and structure members.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate kmutex_t	mdi_mutex;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Registered vHCI class driver lists
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate int		mdi_vhci_count;
997c478bd9Sstevel@tonic-gate mdi_vhci_t	*mdi_vhci_head;
1007c478bd9Sstevel@tonic-gate mdi_vhci_t	*mdi_vhci_tail;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * Client Hash Table size
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate static int	mdi_client_table_size = CLIENT_HASH_TABLE_SIZE;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * taskq interface definitions
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_N_THREADS	8
1117c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_PRI		minclsyspri
1127c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_MINALLOC	(4*mdi_taskq_n_threads)
1137c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_MAXALLOC	(500*mdi_taskq_n_threads)
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate taskq_t				*mdi_taskq;
1167c478bd9Sstevel@tonic-gate static uint_t			mdi_taskq_n_threads = MDI_TASKQ_N_THREADS;
1177c478bd9Sstevel@tonic-gate 
1183c34adc5Sramat #define	TICKS_PER_SECOND	(drv_usectohz(1000000))
1193c34adc5Sramat 
1207c478bd9Sstevel@tonic-gate /*
1213c34adc5Sramat  * The data should be "quiet" for this interval (in seconds) before the
1223c34adc5Sramat  * vhci cached data is flushed to the disk.
1237c478bd9Sstevel@tonic-gate  */
1243c34adc5Sramat static int mdi_vhcache_flush_delay = 10;
1253c34adc5Sramat 
1263c34adc5Sramat /* number of seconds the vhcache flush daemon will sleep idle before exiting */
1273c34adc5Sramat static int mdi_vhcache_flush_daemon_idle_time = 60;
1283c34adc5Sramat 
1293c34adc5Sramat /*
13067e56d35Sramat  * MDI falls back to discovery of all paths when a bus_config_one fails.
13167e56d35Sramat  * The following parameters can be used to tune this operation.
13267e56d35Sramat  *
13367e56d35Sramat  * mdi_path_discovery_boot
13467e56d35Sramat  *	Number of times path discovery will be attempted during early boot.
13567e56d35Sramat  *	Probably there is no reason to ever set this value to greater than one.
13667e56d35Sramat  *
13767e56d35Sramat  * mdi_path_discovery_postboot
13867e56d35Sramat  *	Number of times path discovery will be attempted after early boot.
13967e56d35Sramat  *	Set it to a minimum of two to allow for discovery of iscsi paths which
14067e56d35Sramat  *	may happen very late during booting.
14167e56d35Sramat  *
14267e56d35Sramat  * mdi_path_discovery_interval
14367e56d35Sramat  *	Minimum number of seconds MDI will wait between successive discovery
14467e56d35Sramat  *	of all paths. Set it to -1 to disable discovery of all paths.
14567e56d35Sramat  */
14667e56d35Sramat static int mdi_path_discovery_boot = 1;
14767e56d35Sramat static int mdi_path_discovery_postboot = 2;
14867e56d35Sramat static int mdi_path_discovery_interval = 10;
14967e56d35Sramat 
15067e56d35Sramat /*
1513c34adc5Sramat  * number of seconds the asynchronous configuration thread will sleep idle
1523c34adc5Sramat  * before exiting.
1533c34adc5Sramat  */
1543c34adc5Sramat static int mdi_async_config_idle_time = 600;
1553c34adc5Sramat 
1563c34adc5Sramat static int mdi_bus_config_cache_hash_size = 256;
1573c34adc5Sramat 
1583c34adc5Sramat /* turns off multithreaded configuration for certain operations */
1593c34adc5Sramat static int mdi_mtc_off = 0;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
162602ca9eaScth  * The "path" to a pathinfo node is identical to the /devices path to a
163602ca9eaScth  * devinfo node had the device been enumerated under a pHCI instead of
164602ca9eaScth  * a vHCI.  This pathinfo "path" is associated with a 'path_instance'.
165602ca9eaScth  * This association persists across create/delete of the pathinfo nodes,
166602ca9eaScth  * but not across reboot.
167602ca9eaScth  */
168602ca9eaScth static uint_t		mdi_pathmap_instance = 1;	/* 0 -> any path */
169602ca9eaScth static int		mdi_pathmap_hash_size = 256;
170602ca9eaScth static kmutex_t		mdi_pathmap_mutex;
171602ca9eaScth static mod_hash_t	*mdi_pathmap_bypath;		/* "path"->instance */
172602ca9eaScth static mod_hash_t	*mdi_pathmap_byinstance;	/* instance->"path" */
1734c06356bSdh142964 static mod_hash_t	*mdi_pathmap_sbyinstance;	/* inst->shortpath */
174602ca9eaScth 
175602ca9eaScth /*
1767c478bd9Sstevel@tonic-gate  * MDI component property name/value string definitions
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate const char 		*mdi_component_prop = "mpxio-component";
1797c478bd9Sstevel@tonic-gate const char		*mdi_component_prop_vhci = "vhci";
1807c478bd9Sstevel@tonic-gate const char		*mdi_component_prop_phci = "phci";
1817c478bd9Sstevel@tonic-gate const char		*mdi_component_prop_client = "client";
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * MDI client global unique identifier property name
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate const char		*mdi_client_guid_prop = "client-guid";
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * MDI client load balancing property name/value string definitions
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate const char		*mdi_load_balance = "load-balance";
1927c478bd9Sstevel@tonic-gate const char		*mdi_load_balance_none = "none";
1937c478bd9Sstevel@tonic-gate const char		*mdi_load_balance_rr = "round-robin";
1947c478bd9Sstevel@tonic-gate const char		*mdi_load_balance_lba = "logical-block";
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * Obsolete vHCI class definition; to be removed after Leadville update
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate const char *mdi_vhci_class_scsi = MDI_HCI_CLASS_SCSI;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static char vhci_greeting[] =
2027c478bd9Sstevel@tonic-gate 	"\tThere already exists one vHCI driver for class %s\n"
2037c478bd9Sstevel@tonic-gate 	"\tOnly one vHCI driver for each class is allowed\n";
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate  * Static function prototypes
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate static int		i_mdi_phci_offline(dev_info_t *, uint_t);
2097c478bd9Sstevel@tonic-gate static int		i_mdi_client_offline(dev_info_t *, uint_t);
2107c478bd9Sstevel@tonic-gate static int		i_mdi_phci_pre_detach(dev_info_t *, ddi_detach_cmd_t);
2117c478bd9Sstevel@tonic-gate static void		i_mdi_phci_post_detach(dev_info_t *,
2127c478bd9Sstevel@tonic-gate 			    ddi_detach_cmd_t, int);
2137c478bd9Sstevel@tonic-gate static int		i_mdi_client_pre_detach(dev_info_t *,
2147c478bd9Sstevel@tonic-gate 			    ddi_detach_cmd_t);
2157c478bd9Sstevel@tonic-gate static void		i_mdi_client_post_detach(dev_info_t *,
2167c478bd9Sstevel@tonic-gate 			    ddi_detach_cmd_t, int);
2177c478bd9Sstevel@tonic-gate static void		i_mdi_pm_hold_pip(mdi_pathinfo_t *);
2187c478bd9Sstevel@tonic-gate static void		i_mdi_pm_rele_pip(mdi_pathinfo_t *);
2197c478bd9Sstevel@tonic-gate static int 		i_mdi_lba_lb(mdi_client_t *ct,
2207c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_t **ret_pip, struct buf *buf);
2217c478bd9Sstevel@tonic-gate static void		i_mdi_pm_hold_client(mdi_client_t *, int);
2227c478bd9Sstevel@tonic-gate static void		i_mdi_pm_rele_client(mdi_client_t *, int);
2237c478bd9Sstevel@tonic-gate static void		i_mdi_pm_reset_client(mdi_client_t *);
2247c478bd9Sstevel@tonic-gate static int		i_mdi_power_all_phci(mdi_client_t *);
2258c4f8890Srs135747 static void		i_mdi_log_sysevent(dev_info_t *, char *, char *);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * Internal mdi_pathinfo node functions
2307c478bd9Sstevel@tonic-gate  */
2317c478bd9Sstevel@tonic-gate static void		i_mdi_pi_kstat_destroy(mdi_pathinfo_t *);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate static mdi_vhci_t	*i_mdi_vhci_class2vhci(char *);
2347c478bd9Sstevel@tonic-gate static mdi_vhci_t	*i_devi_get_vhci(dev_info_t *);
2357c478bd9Sstevel@tonic-gate static mdi_phci_t	*i_devi_get_phci(dev_info_t *);
2367c478bd9Sstevel@tonic-gate static void		i_mdi_phci_lock(mdi_phci_t *, mdi_pathinfo_t *);
2377c478bd9Sstevel@tonic-gate static void		i_mdi_phci_unlock(mdi_phci_t *);
2383c34adc5Sramat static mdi_pathinfo_t	*i_mdi_pi_alloc(mdi_phci_t *, char *, mdi_client_t *);
2397c478bd9Sstevel@tonic-gate static void		i_mdi_phci_add_path(mdi_phci_t *, mdi_pathinfo_t *);
2407c478bd9Sstevel@tonic-gate static void		i_mdi_client_add_path(mdi_client_t *, mdi_pathinfo_t *);
2417c478bd9Sstevel@tonic-gate static void		i_mdi_pi_free(mdi_phci_t *ph, mdi_pathinfo_t *,
2427c478bd9Sstevel@tonic-gate 			    mdi_client_t *);
2437c478bd9Sstevel@tonic-gate static void		i_mdi_phci_remove_path(mdi_phci_t *, mdi_pathinfo_t *);
2447c478bd9Sstevel@tonic-gate static void		i_mdi_client_remove_path(mdi_client_t *,
2457c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_t *);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate static int		i_mdi_pi_state_change(mdi_pathinfo_t *,
2487c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_state_t, int);
2497c478bd9Sstevel@tonic-gate static int		i_mdi_pi_offline(mdi_pathinfo_t *, int);
2507c478bd9Sstevel@tonic-gate static dev_info_t	*i_mdi_devinfo_create(mdi_vhci_t *, char *, char *,
2513c34adc5Sramat 			    char **, int);
2527c478bd9Sstevel@tonic-gate static dev_info_t	*i_mdi_devinfo_find(mdi_vhci_t *, char *, char *);
2537c478bd9Sstevel@tonic-gate static int		i_mdi_devinfo_remove(dev_info_t *, dev_info_t *, int);
2547c478bd9Sstevel@tonic-gate static int		i_mdi_is_child_present(dev_info_t *, dev_info_t *);
2553c34adc5Sramat static mdi_client_t	*i_mdi_client_alloc(mdi_vhci_t *, char *, char *);
2567c478bd9Sstevel@tonic-gate static void		i_mdi_client_enlist_table(mdi_vhci_t *, mdi_client_t *);
2577c478bd9Sstevel@tonic-gate static void		i_mdi_client_delist_table(mdi_vhci_t *, mdi_client_t *);
2583c34adc5Sramat static mdi_client_t	*i_mdi_client_find(mdi_vhci_t *, char *, char *);
2597c478bd9Sstevel@tonic-gate static void		i_mdi_client_update_state(mdi_client_t *);
2607c478bd9Sstevel@tonic-gate static int		i_mdi_client_compute_state(mdi_client_t *,
2617c478bd9Sstevel@tonic-gate 			    mdi_phci_t *);
2627c478bd9Sstevel@tonic-gate static void		i_mdi_client_lock(mdi_client_t *, mdi_pathinfo_t *);
2637c478bd9Sstevel@tonic-gate static void		i_mdi_client_unlock(mdi_client_t *);
2647c478bd9Sstevel@tonic-gate static int		i_mdi_client_free(mdi_vhci_t *, mdi_client_t *);
2657c478bd9Sstevel@tonic-gate static mdi_client_t	*i_devi_get_client(dev_info_t *);
266ee28b439Scm136836 /*
267ee28b439Scm136836  * NOTE: this will be removed once the NWS files are changed to use the new
268ee28b439Scm136836  * mdi_{enable,disable}_path interfaces
269ee28b439Scm136836  */
270ee28b439Scm136836 static int		i_mdi_pi_enable_disable(dev_info_t *, dev_info_t *,
271ee28b439Scm136836 				int, int);
272ee28b439Scm136836 static mdi_pathinfo_t 	*i_mdi_enable_disable_path(mdi_pathinfo_t *pip,
273ee28b439Scm136836 				mdi_vhci_t *vh, int flags, int op);
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate  * Failover related function prototypes
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate static int		i_mdi_failover(void *);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * misc internal functions
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate static int		i_mdi_get_hash_key(char *);
2837c478bd9Sstevel@tonic-gate static int		i_map_nvlist_error_to_mdi(int);
2847c478bd9Sstevel@tonic-gate static void		i_mdi_report_path_state(mdi_client_t *,
2857c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_t *);
2867c478bd9Sstevel@tonic-gate 
2873c34adc5Sramat static void		setup_vhci_cache(mdi_vhci_t *);
2883c34adc5Sramat static int		destroy_vhci_cache(mdi_vhci_t *);
2893c34adc5Sramat static int		stop_vhcache_async_threads(mdi_vhci_config_t *);
2903c34adc5Sramat static boolean_t	stop_vhcache_flush_thread(void *, int);
2913c34adc5Sramat static void		free_string_array(char **, int);
2923c34adc5Sramat static void		free_vhcache_phci(mdi_vhcache_phci_t *);
2933c34adc5Sramat static void		free_vhcache_pathinfo(mdi_vhcache_pathinfo_t *);
2943c34adc5Sramat static void		free_vhcache_client(mdi_vhcache_client_t *);
2953c34adc5Sramat static int		mainnvl_to_vhcache(mdi_vhci_cache_t *, nvlist_t *);
2963c34adc5Sramat static nvlist_t		*vhcache_to_mainnvl(mdi_vhci_cache_t *);
2973c34adc5Sramat static void		vhcache_phci_add(mdi_vhci_config_t *, mdi_phci_t *);
2983c34adc5Sramat static void		vhcache_phci_remove(mdi_vhci_config_t *, mdi_phci_t *);
2993c34adc5Sramat static void		vhcache_pi_add(mdi_vhci_config_t *,
3003c34adc5Sramat 			    struct mdi_pathinfo *);
3013c34adc5Sramat static void		vhcache_pi_remove(mdi_vhci_config_t *,
3023c34adc5Sramat 			    struct mdi_pathinfo *);
3033c34adc5Sramat static void		free_phclient_path_list(mdi_phys_path_t *);
3043c34adc5Sramat static void		sort_vhcache_paths(mdi_vhcache_client_t *);
3053c34adc5Sramat static int		flush_vhcache(mdi_vhci_config_t *, int);
3063c34adc5Sramat static void		vhcache_dirty(mdi_vhci_config_t *);
3073c34adc5Sramat static void		free_async_client_config(mdi_async_client_config_t *);
30867e56d35Sramat static void		single_threaded_vhconfig_enter(mdi_vhci_config_t *);
30967e56d35Sramat static void		single_threaded_vhconfig_exit(mdi_vhci_config_t *);
3103c34adc5Sramat static nvlist_t		*read_on_disk_vhci_cache(char *);
3113c34adc5Sramat extern int		fread_nvlist(char *, nvlist_t **);
3123c34adc5Sramat extern int		fwrite_nvlist(char *, nvlist_t *);
3133c34adc5Sramat 
3147c478bd9Sstevel@tonic-gate /* called once when first vhci registers with mdi */
3157c478bd9Sstevel@tonic-gate static void
3167c478bd9Sstevel@tonic-gate i_mdi_init()
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	static int initialized = 0;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	if (initialized)
3217c478bd9Sstevel@tonic-gate 		return;
3227c478bd9Sstevel@tonic-gate 	initialized = 1;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	mutex_init(&mdi_mutex, NULL, MUTEX_DEFAULT, NULL);
325602ca9eaScth 
326602ca9eaScth 	/* Create our taskq resources */
3277c478bd9Sstevel@tonic-gate 	mdi_taskq = taskq_create("mdi_taskq", mdi_taskq_n_threads,
3287c478bd9Sstevel@tonic-gate 	    MDI_TASKQ_PRI, MDI_TASKQ_MINALLOC, MDI_TASKQ_MAXALLOC,
3297c478bd9Sstevel@tonic-gate 	    TASKQ_PREPOPULATE | TASKQ_CPR_SAFE);
3307c478bd9Sstevel@tonic-gate 	ASSERT(mdi_taskq != NULL);	/* taskq_create never fails */
331602ca9eaScth 
332602ca9eaScth 	/* Allocate ['path_instance' <-> "path"] maps */
333602ca9eaScth 	mutex_init(&mdi_pathmap_mutex, NULL, MUTEX_DRIVER, NULL);
334602ca9eaScth 	mdi_pathmap_bypath = mod_hash_create_strhash(
335602ca9eaScth 	    "mdi_pathmap_bypath", mdi_pathmap_hash_size,
336602ca9eaScth 	    mod_hash_null_valdtor);
337602ca9eaScth 	mdi_pathmap_byinstance = mod_hash_create_idhash(
338602ca9eaScth 	    "mdi_pathmap_byinstance", mdi_pathmap_hash_size,
339602ca9eaScth 	    mod_hash_null_valdtor);
3404c06356bSdh142964 	mdi_pathmap_sbyinstance = mod_hash_create_idhash(
3414c06356bSdh142964 	    "mdi_pathmap_sbyinstance", mdi_pathmap_hash_size,
3424c06356bSdh142964 	    mod_hash_null_valdtor);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * mdi_get_component_type():
3477c478bd9Sstevel@tonic-gate  *		Return mpxio component type
3487c478bd9Sstevel@tonic-gate  * Return Values:
3497c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_NONE
3507c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_VHCI
3517c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_PHCI
3527c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_CLIENT
3537c478bd9Sstevel@tonic-gate  * XXX This doesn't work under multi-level MPxIO and should be
3545e3986cbScth  *	removed when clients migrate mdi_component_is_*() interfaces.
3557c478bd9Sstevel@tonic-gate  */
3567c478bd9Sstevel@tonic-gate int
3577c478bd9Sstevel@tonic-gate mdi_get_component_type(dev_info_t *dip)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_mdi_component);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * mdi_vhci_register():
3647c478bd9Sstevel@tonic-gate  *		Register a vHCI module with the mpxio framework
3657c478bd9Sstevel@tonic-gate  *		mdi_vhci_register() is called by vHCI drivers to register the
3667c478bd9Sstevel@tonic-gate  *		'class_driver' vHCI driver and its MDI entrypoints with the
3677c478bd9Sstevel@tonic-gate  *		mpxio framework.  The vHCI driver must call this interface as
3687c478bd9Sstevel@tonic-gate  *		part of its attach(9e) handler.
3697c478bd9Sstevel@tonic-gate  *		Competing threads may try to attach mdi_vhci_register() as
3707c478bd9Sstevel@tonic-gate  *		the vHCI drivers are loaded and attached as a result of pHCI
3717c478bd9Sstevel@tonic-gate  *		driver instance registration (mdi_phci_register()) with the
3727c478bd9Sstevel@tonic-gate  *		framework.
3737c478bd9Sstevel@tonic-gate  * Return Values:
3747c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
3757c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
3767c478bd9Sstevel@tonic-gate  */
3777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3787c478bd9Sstevel@tonic-gate int
3797c478bd9Sstevel@tonic-gate mdi_vhci_register(char *class, dev_info_t *vdip, mdi_vhci_ops_t *vops,
3807c478bd9Sstevel@tonic-gate     int flags)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh = NULL;
3837c478bd9Sstevel@tonic-gate 
38455e592a2SRandall Ralphs 	/* Registrant can't be older */
38555e592a2SRandall Ralphs 	ASSERT(vops->vo_revision <= MDI_VHCI_OPS_REV);
38655e592a2SRandall Ralphs 
38700a3eaf3SRamaswamy Tummala #ifdef DEBUG
38800a3eaf3SRamaswamy Tummala 	/*
38900a3eaf3SRamaswamy Tummala 	 * IB nexus driver is loaded only when IB hardware is present.
39000a3eaf3SRamaswamy Tummala 	 * In order to be able to do this there is a need to drive the loading
39100a3eaf3SRamaswamy Tummala 	 * and attaching of the IB nexus driver (especially when an IB hardware
39200a3eaf3SRamaswamy Tummala 	 * is dynamically plugged in) when an IB HCA driver (PHCI)
39300a3eaf3SRamaswamy Tummala 	 * is being attached. Unfortunately this gets into the limitations
39400a3eaf3SRamaswamy Tummala 	 * of devfs as there seems to be no clean way to drive configuration
39500a3eaf3SRamaswamy Tummala 	 * of a subtree from another subtree of a devfs. Hence, do not ASSERT
39600a3eaf3SRamaswamy Tummala 	 * for IB.
39700a3eaf3SRamaswamy Tummala 	 */
39800a3eaf3SRamaswamy Tummala 	if (strcmp(class, MDI_HCI_CLASS_IB) != 0)
3995e3986cbScth 		ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(vdip)));
40000a3eaf3SRamaswamy Tummala #endif
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	i_mdi_init();
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	mutex_enter(&mdi_mutex);
4057c478bd9Sstevel@tonic-gate 	/*
4067c478bd9Sstevel@tonic-gate 	 * Scan for already registered vhci
4077c478bd9Sstevel@tonic-gate 	 */
4087c478bd9Sstevel@tonic-gate 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
4097c478bd9Sstevel@tonic-gate 		if (strcmp(vh->vh_class, class) == 0) {
4107c478bd9Sstevel@tonic-gate 			/*
4117c478bd9Sstevel@tonic-gate 			 * vHCI has already been created.  Check for valid
4127c478bd9Sstevel@tonic-gate 			 * vHCI ops registration.  We only support one vHCI
4137c478bd9Sstevel@tonic-gate 			 * module per class
4147c478bd9Sstevel@tonic-gate 			 */
4157c478bd9Sstevel@tonic-gate 			if (vh->vh_ops != NULL) {
4167c478bd9Sstevel@tonic-gate 				mutex_exit(&mdi_mutex);
4177c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, vhci_greeting, class);
4187c478bd9Sstevel@tonic-gate 				return (MDI_FAILURE);
4197c478bd9Sstevel@tonic-gate 			}
4207c478bd9Sstevel@tonic-gate 			break;
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	/*
4257c478bd9Sstevel@tonic-gate 	 * if not yet created, create the vHCI component
4267c478bd9Sstevel@tonic-gate 	 */
4277c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
4287c478bd9Sstevel@tonic-gate 		struct client_hash	*hash = NULL;
4297c478bd9Sstevel@tonic-gate 		char			*load_balance;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 		/*
4327c478bd9Sstevel@tonic-gate 		 * Allocate and initialize the mdi extensions
4337c478bd9Sstevel@tonic-gate 		 */
4347c478bd9Sstevel@tonic-gate 		vh = kmem_zalloc(sizeof (mdi_vhci_t), KM_SLEEP);
4357c478bd9Sstevel@tonic-gate 		hash = kmem_zalloc(mdi_client_table_size * sizeof (*hash),
4367c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
4377c478bd9Sstevel@tonic-gate 		vh->vh_client_table = hash;
4387c478bd9Sstevel@tonic-gate 		vh->vh_class = kmem_zalloc(strlen(class) + 1, KM_SLEEP);
4397c478bd9Sstevel@tonic-gate 		(void) strcpy(vh->vh_class, class);
4407c478bd9Sstevel@tonic-gate 		vh->vh_lb = LOAD_BALANCE_RR;
4417c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, vdip,
4427c478bd9Sstevel@tonic-gate 		    0, LOAD_BALANCE_PROP, &load_balance) == DDI_SUCCESS) {
4437c478bd9Sstevel@tonic-gate 			if (strcmp(load_balance, LOAD_BALANCE_PROP_NONE) == 0) {
4447c478bd9Sstevel@tonic-gate 				vh->vh_lb = LOAD_BALANCE_NONE;
4457c478bd9Sstevel@tonic-gate 			} else if (strcmp(load_balance, LOAD_BALANCE_PROP_LBA)
4467c478bd9Sstevel@tonic-gate 				    == 0) {
4477c478bd9Sstevel@tonic-gate 				vh->vh_lb = LOAD_BALANCE_LBA;
4487c478bd9Sstevel@tonic-gate 			}
4497c478bd9Sstevel@tonic-gate 			ddi_prop_free(load_balance);
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 
4525e3986cbScth 		mutex_init(&vh->vh_phci_mutex, NULL, MUTEX_DEFAULT, NULL);
4535e3986cbScth 		mutex_init(&vh->vh_client_mutex, NULL, MUTEX_DEFAULT, NULL);
4545e3986cbScth 
4557c478bd9Sstevel@tonic-gate 		/*
4567c478bd9Sstevel@tonic-gate 		 * Store the vHCI ops vectors
4577c478bd9Sstevel@tonic-gate 		 */
4587c478bd9Sstevel@tonic-gate 		vh->vh_dip = vdip;
4597c478bd9Sstevel@tonic-gate 		vh->vh_ops = vops;
4607c478bd9Sstevel@tonic-gate 
4613c34adc5Sramat 		setup_vhci_cache(vh);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		if (mdi_vhci_head == NULL) {
4647c478bd9Sstevel@tonic-gate 			mdi_vhci_head = vh;
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate 		if (mdi_vhci_tail) {
4677c478bd9Sstevel@tonic-gate 			mdi_vhci_tail->vh_next = vh;
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 		mdi_vhci_tail = vh;
4707c478bd9Sstevel@tonic-gate 		mdi_vhci_count++;
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	/*
4747c478bd9Sstevel@tonic-gate 	 * Claim the devfs node as a vhci component
4757c478bd9Sstevel@tonic-gate 	 */
4767c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_component |= MDI_COMPONENT_VHCI;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	/*
4797c478bd9Sstevel@tonic-gate 	 * Initialize our back reference from dev_info node
4807c478bd9Sstevel@tonic-gate 	 */
4817c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_xhci = (caddr_t)vh;
4827c478bd9Sstevel@tonic-gate 	mutex_exit(&mdi_mutex);
4837c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * mdi_vhci_unregister():
4887c478bd9Sstevel@tonic-gate  *		Unregister a vHCI module from mpxio framework
4897c478bd9Sstevel@tonic-gate  *		mdi_vhci_unregister() is called from the detach(9E) entrypoint
4907c478bd9Sstevel@tonic-gate  * 		of a vhci to unregister it from the framework.
4917c478bd9Sstevel@tonic-gate  * Return Values:
4927c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
4937c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
4947c478bd9Sstevel@tonic-gate  */
4957c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4967c478bd9Sstevel@tonic-gate int
4977c478bd9Sstevel@tonic-gate mdi_vhci_unregister(dev_info_t *vdip, int flags)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*found, *vh, *prev = NULL;
5007c478bd9Sstevel@tonic-gate 
5015e3986cbScth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(vdip)));
5025e3986cbScth 
5037c478bd9Sstevel@tonic-gate 	/*
5047c478bd9Sstevel@tonic-gate 	 * Check for invalid VHCI
5057c478bd9Sstevel@tonic-gate 	 */
5067c478bd9Sstevel@tonic-gate 	if ((vh = i_devi_get_vhci(vdip)) == NULL)
5077c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/*
5107c478bd9Sstevel@tonic-gate 	 * Scan the list of registered vHCIs for a match
5117c478bd9Sstevel@tonic-gate 	 */
5125e3986cbScth 	mutex_enter(&mdi_mutex);
5137c478bd9Sstevel@tonic-gate 	for (found = mdi_vhci_head; found != NULL; found = found->vh_next) {
5147c478bd9Sstevel@tonic-gate 		if (found == vh)
5157c478bd9Sstevel@tonic-gate 			break;
5167c478bd9Sstevel@tonic-gate 		prev = found;
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	if (found == NULL) {
5207c478bd9Sstevel@tonic-gate 		mutex_exit(&mdi_mutex);
5217c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/*
5258c4f8890Srs135747 	 * Check the vHCI, pHCI and client count. All the pHCIs and clients
5267c478bd9Sstevel@tonic-gate 	 * should have been unregistered, before a vHCI can be
5277c478bd9Sstevel@tonic-gate 	 * unregistered.
5287c478bd9Sstevel@tonic-gate 	 */
5295e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
5305e3986cbScth 	if (vh->vh_refcnt || vh->vh_phci_count || vh->vh_client_count) {
5315e3986cbScth 		MDI_VHCI_PHCI_UNLOCK(vh);
5325e3986cbScth 		mutex_exit(&mdi_mutex);
5335e3986cbScth 		return (MDI_FAILURE);
5345e3986cbScth 	}
5355e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
5365e3986cbScth 
5375e3986cbScth 	if (destroy_vhci_cache(vh) != MDI_SUCCESS) {
5387c478bd9Sstevel@tonic-gate 		mutex_exit(&mdi_mutex);
5397c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/*
5437c478bd9Sstevel@tonic-gate 	 * Remove the vHCI from the global list
5447c478bd9Sstevel@tonic-gate 	 */
5457c478bd9Sstevel@tonic-gate 	if (vh == mdi_vhci_head) {
5467c478bd9Sstevel@tonic-gate 		mdi_vhci_head = vh->vh_next;
5477c478bd9Sstevel@tonic-gate 	} else {
5487c478bd9Sstevel@tonic-gate 		prev->vh_next = vh->vh_next;
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 	if (vh == mdi_vhci_tail) {
5517c478bd9Sstevel@tonic-gate 		mdi_vhci_tail = prev;
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	mdi_vhci_count--;
5547c478bd9Sstevel@tonic-gate 	mutex_exit(&mdi_mutex);
5553c34adc5Sramat 
5563c34adc5Sramat 	vh->vh_ops = NULL;
5577c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_component &= ~MDI_COMPONENT_VHCI;
5587c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_xhci = NULL;
5597c478bd9Sstevel@tonic-gate 	kmem_free(vh->vh_class, strlen(vh->vh_class)+1);
5607c478bd9Sstevel@tonic-gate 	kmem_free(vh->vh_client_table,
5617c478bd9Sstevel@tonic-gate 	    mdi_client_table_size * sizeof (struct client_hash));
5625e3986cbScth 	mutex_destroy(&vh->vh_phci_mutex);
5635e3986cbScth 	mutex_destroy(&vh->vh_client_mutex);
56478dc6db2Sllai1 
5657c478bd9Sstevel@tonic-gate 	kmem_free(vh, sizeof (mdi_vhci_t));
5667c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate /*
5707c478bd9Sstevel@tonic-gate  * i_mdi_vhci_class2vhci():
5717c478bd9Sstevel@tonic-gate  *		Look for a matching vHCI module given a vHCI class name
5727c478bd9Sstevel@tonic-gate  * Return Values:
5737c478bd9Sstevel@tonic-gate  *		Handle to a vHCI component
5747c478bd9Sstevel@tonic-gate  *		NULL
5757c478bd9Sstevel@tonic-gate  */
5767c478bd9Sstevel@tonic-gate static mdi_vhci_t *
5777c478bd9Sstevel@tonic-gate i_mdi_vhci_class2vhci(char *class)
5787c478bd9Sstevel@tonic-gate {
5797c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&mdi_mutex));
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	mutex_enter(&mdi_mutex);
5847c478bd9Sstevel@tonic-gate 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
5857c478bd9Sstevel@tonic-gate 		if (strcmp(vh->vh_class, class) == 0) {
5867c478bd9Sstevel@tonic-gate 			break;
5877c478bd9Sstevel@tonic-gate 		}
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 	mutex_exit(&mdi_mutex);
5907c478bd9Sstevel@tonic-gate 	return (vh);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate /*
5947c478bd9Sstevel@tonic-gate  * i_devi_get_vhci():
5957c478bd9Sstevel@tonic-gate  *		Utility function to get the handle to a vHCI component
5967c478bd9Sstevel@tonic-gate  * Return Values:
5977c478bd9Sstevel@tonic-gate  *		Handle to a vHCI component
5987c478bd9Sstevel@tonic-gate  *		NULL
5997c478bd9Sstevel@tonic-gate  */
6007c478bd9Sstevel@tonic-gate mdi_vhci_t *
6017c478bd9Sstevel@tonic-gate i_devi_get_vhci(dev_info_t *vdip)
6027c478bd9Sstevel@tonic-gate {
6037c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
6047c478bd9Sstevel@tonic-gate 	if (MDI_VHCI(vdip)) {
6057c478bd9Sstevel@tonic-gate 		vh = (mdi_vhci_t *)DEVI(vdip)->devi_mdi_xhci;
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 	return (vh);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate  * mdi_phci_register():
6127c478bd9Sstevel@tonic-gate  *		Register a pHCI module with mpxio framework
6137c478bd9Sstevel@tonic-gate  *		mdi_phci_register() is called by pHCI drivers to register with
6147c478bd9Sstevel@tonic-gate  *		the mpxio framework and a specific 'class_driver' vHCI.  The
6157c478bd9Sstevel@tonic-gate  *		pHCI driver must call this interface as part of its attach(9e)
6167c478bd9Sstevel@tonic-gate  *		handler.
6177c478bd9Sstevel@tonic-gate  * Return Values:
6187c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
6197c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
6207c478bd9Sstevel@tonic-gate  */
6217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6227c478bd9Sstevel@tonic-gate int
6237c478bd9Sstevel@tonic-gate mdi_phci_register(char *class, dev_info_t *pdip, int flags)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 	mdi_phci_t		*ph;
6267c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh;
6277c478bd9Sstevel@tonic-gate 	char			*data;
6287c478bd9Sstevel@tonic-gate 
6295e3986cbScth 	/*
6305e3986cbScth 	 * Some subsystems, like fcp, perform pHCI registration from a
6315e3986cbScth 	 * different thread than the one doing the pHCI attach(9E) - the
6325e3986cbScth 	 * driver attach code is waiting for this other thread to complete.
6335e3986cbScth 	 * This means we can only ASSERT DEVI_BUSY_CHANGING of parent
6345e3986cbScth 	 * (indicating that some thread has done an ndi_devi_enter of parent)
6355e3986cbScth 	 * not DEVI_BUSY_OWNED (which would indicate that we did the enter).
6365e3986cbScth 	 */
6375e3986cbScth 	ASSERT(DEVI_BUSY_CHANGING(ddi_get_parent(pdip)));
6385e3986cbScth 
6397c478bd9Sstevel@tonic-gate 	/*
6407c478bd9Sstevel@tonic-gate 	 * Check for mpxio-disable property. Enable mpxio if the property is
6417c478bd9Sstevel@tonic-gate 	 * missing or not set to "yes".
6427c478bd9Sstevel@tonic-gate 	 * If the property is set to "yes" then emit a brief message.
6437c478bd9Sstevel@tonic-gate 	 */
6447c478bd9Sstevel@tonic-gate 	if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip, 0, "mpxio-disable",
6457c478bd9Sstevel@tonic-gate 	    &data) == DDI_SUCCESS)) {
6467c478bd9Sstevel@tonic-gate 		if (strcmp(data, "yes") == 0) {
6474c06356bSdh142964 			MDI_DEBUG(1, (MDI_CONT, pdip,
6484c06356bSdh142964 			    "?multipath capabilities disabled via %s.conf.",
6497c478bd9Sstevel@tonic-gate 			    ddi_driver_name(pdip)));
6507c478bd9Sstevel@tonic-gate 			ddi_prop_free(data);
6517c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
6527c478bd9Sstevel@tonic-gate 		}
6537c478bd9Sstevel@tonic-gate 		ddi_prop_free(data);
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	/*
6577c478bd9Sstevel@tonic-gate 	 * Search for a matching vHCI
6587c478bd9Sstevel@tonic-gate 	 */
6597c478bd9Sstevel@tonic-gate 	vh = (mdi_vhci_t *)i_mdi_vhci_class2vhci(class);
6607c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
6617c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
6627c478bd9Sstevel@tonic-gate 	}
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	ph = kmem_zalloc(sizeof (mdi_phci_t), KM_SLEEP);
6657c478bd9Sstevel@tonic-gate 	mutex_init(&ph->ph_mutex, NULL, MUTEX_DEFAULT, NULL);
6667c478bd9Sstevel@tonic-gate 	ph->ph_dip = pdip;
6677c478bd9Sstevel@tonic-gate 	ph->ph_vhci = vh;
6687c478bd9Sstevel@tonic-gate 	ph->ph_next = NULL;
6697c478bd9Sstevel@tonic-gate 	ph->ph_unstable = 0;
6707c478bd9Sstevel@tonic-gate 	ph->ph_vprivate = 0;
6717c478bd9Sstevel@tonic-gate 	cv_init(&ph->ph_unstable_cv, NULL, CV_DRIVER, NULL);
6727c478bd9Sstevel@tonic-gate 
6735e3986cbScth 	MDI_PHCI_LOCK(ph);
6747c478bd9Sstevel@tonic-gate 	MDI_PHCI_SET_POWER_UP(ph);
6755e3986cbScth 	MDI_PHCI_UNLOCK(ph);
6767c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_component |= MDI_COMPONENT_PHCI;
6777c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_xhci = (caddr_t)ph;
6787c478bd9Sstevel@tonic-gate 
6793c34adc5Sramat 	vhcache_phci_add(vh->vh_config, ph);
6803c34adc5Sramat 
6815e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
6827c478bd9Sstevel@tonic-gate 	if (vh->vh_phci_head == NULL) {
6837c478bd9Sstevel@tonic-gate 		vh->vh_phci_head = ph;
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 	if (vh->vh_phci_tail) {
6867c478bd9Sstevel@tonic-gate 		vh->vh_phci_tail->ph_next = ph;
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 	vh->vh_phci_tail = ph;
6897c478bd9Sstevel@tonic-gate 	vh->vh_phci_count++;
6905e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
6915e3986cbScth 
6928c4f8890Srs135747 	i_mdi_log_sysevent(pdip, class, ESC_DDI_INITIATOR_REGISTER);
6937c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate /*
6977c478bd9Sstevel@tonic-gate  * mdi_phci_unregister():
6987c478bd9Sstevel@tonic-gate  *		Unregister a pHCI module from mpxio framework
6997c478bd9Sstevel@tonic-gate  *		mdi_phci_unregister() is called by the pHCI drivers from their
7007c478bd9Sstevel@tonic-gate  *		detach(9E) handler to unregister their instances from the
7017c478bd9Sstevel@tonic-gate  *		framework.
7027c478bd9Sstevel@tonic-gate  * Return Values:
7037c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
7047c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
7057c478bd9Sstevel@tonic-gate  */
7067c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7077c478bd9Sstevel@tonic-gate int
7087c478bd9Sstevel@tonic-gate mdi_phci_unregister(dev_info_t *pdip, int flags)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh;
7117c478bd9Sstevel@tonic-gate 	mdi_phci_t		*ph;
7127c478bd9Sstevel@tonic-gate 	mdi_phci_t		*tmp;
7137c478bd9Sstevel@tonic-gate 	mdi_phci_t		*prev = NULL;
7144c06356bSdh142964 	mdi_pathinfo_t		*pip;
7157c478bd9Sstevel@tonic-gate 
7165e3986cbScth 	ASSERT(DEVI_BUSY_CHANGING(ddi_get_parent(pdip)));
7175e3986cbScth 
7187c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
7197c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
7204c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, pdip, "!not a valid pHCI"));
7217c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
7257c478bd9Sstevel@tonic-gate 	ASSERT(vh != NULL);
7267c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
7274c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, pdip, "!not a valid vHCI"));
7287c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7315e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
7327c478bd9Sstevel@tonic-gate 	tmp = vh->vh_phci_head;
7337c478bd9Sstevel@tonic-gate 	while (tmp) {
7347c478bd9Sstevel@tonic-gate 		if (tmp == ph) {
7357c478bd9Sstevel@tonic-gate 			break;
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 		prev = tmp;
7387c478bd9Sstevel@tonic-gate 		tmp = tmp->ph_next;
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	if (ph == vh->vh_phci_head) {
7427c478bd9Sstevel@tonic-gate 		vh->vh_phci_head = ph->ph_next;
7437c478bd9Sstevel@tonic-gate 	} else {
7447c478bd9Sstevel@tonic-gate 		prev->ph_next = ph->ph_next;
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	if (ph == vh->vh_phci_tail) {
7487c478bd9Sstevel@tonic-gate 		vh->vh_phci_tail = prev;
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	vh->vh_phci_count--;
7525e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
7537c478bd9Sstevel@tonic-gate 
7544c06356bSdh142964 	/* Walk remaining pathinfo nodes and disassociate them from pHCI */
7554c06356bSdh142964 	MDI_PHCI_LOCK(ph);
7564c06356bSdh142964 	for (pip = (mdi_pathinfo_t *)ph->ph_path_head; pip;
7574c06356bSdh142964 	    pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link)
7584c06356bSdh142964 		MDI_PI(pip)->pi_phci = NULL;
7594c06356bSdh142964 	MDI_PHCI_UNLOCK(ph);
7604c06356bSdh142964 
7618c4f8890Srs135747 	i_mdi_log_sysevent(pdip, ph->ph_vhci->vh_class,
7628c4f8890Srs135747 	    ESC_DDI_INITIATOR_UNREGISTER);
7633c34adc5Sramat 	vhcache_phci_remove(vh->vh_config, ph);
7647c478bd9Sstevel@tonic-gate 	cv_destroy(&ph->ph_unstable_cv);
7657c478bd9Sstevel@tonic-gate 	mutex_destroy(&ph->ph_mutex);
7667c478bd9Sstevel@tonic-gate 	kmem_free(ph, sizeof (mdi_phci_t));
7677c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_component &= ~MDI_COMPONENT_PHCI;
7687c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_xhci = NULL;
7697c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate /*
7737c478bd9Sstevel@tonic-gate  * i_devi_get_phci():
7747c478bd9Sstevel@tonic-gate  * 		Utility function to return the phci extensions.
7757c478bd9Sstevel@tonic-gate  */
7767c478bd9Sstevel@tonic-gate static mdi_phci_t *
7777c478bd9Sstevel@tonic-gate i_devi_get_phci(dev_info_t *pdip)
7787c478bd9Sstevel@tonic-gate {
7797c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph = NULL;
78055e592a2SRandall Ralphs 
7817c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(pdip)) {
7827c478bd9Sstevel@tonic-gate 		ph = (mdi_phci_t *)DEVI(pdip)->devi_mdi_xhci;
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 	return (ph);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate /*
7885e3986cbScth  * Single thread mdi entry into devinfo node for modifying its children.
7895e3986cbScth  * If necessary we perform an ndi_devi_enter of the vHCI before doing
7905e3986cbScth  * an ndi_devi_enter of 'dip'.  We maintain circular in two parts: one
7915e3986cbScth  * for the vHCI and one for the pHCI.
7925e3986cbScth  */
7935e3986cbScth void
7945e3986cbScth mdi_devi_enter(dev_info_t *phci_dip, int *circular)
7955e3986cbScth {
7965e3986cbScth 	dev_info_t	*vdip;
7975e3986cbScth 	int		vcircular, pcircular;
7985e3986cbScth 
7995e3986cbScth 	/* Verify calling context */
8005e3986cbScth 	ASSERT(MDI_PHCI(phci_dip));
8015e3986cbScth 	vdip = mdi_devi_get_vdip(phci_dip);
8025e3986cbScth 	ASSERT(vdip);			/* A pHCI always has a vHCI */
8035e3986cbScth 
8045e3986cbScth 	/*
8055e3986cbScth 	 * If pHCI is detaching then the framework has already entered the
8065e3986cbScth 	 * vHCI on a threads that went down the code path leading to
8075e3986cbScth 	 * detach_node().  This framework enter of the vHCI during pHCI
8085e3986cbScth 	 * detach is done to avoid deadlock with vHCI power management
8095e3986cbScth 	 * operations which enter the vHCI and the enter down the path
8105e3986cbScth 	 * to the pHCI. If pHCI is detaching then we piggyback this calls
8115e3986cbScth 	 * enter of the vHCI on frameworks vHCI enter that has already
8125e3986cbScth 	 * occurred - this is OK because we know that the framework thread
8135e3986cbScth 	 * doing detach is waiting for our completion.
8145e3986cbScth 	 *
8155e3986cbScth 	 * We should DEVI_IS_DETACHING under an enter of the parent to avoid
8165e3986cbScth 	 * race with detach - but we can't do that because the framework has
8175e3986cbScth 	 * already entered the parent, so we have some complexity instead.
8185e3986cbScth 	 */
8195e3986cbScth 	for (;;) {
8205e3986cbScth 		if (ndi_devi_tryenter(vdip, &vcircular)) {
8215e3986cbScth 			ASSERT(vcircular != -1);
8225e3986cbScth 			if (DEVI_IS_DETACHING(phci_dip)) {
8235e3986cbScth 				ndi_devi_exit(vdip, vcircular);
8245e3986cbScth 				vcircular = -1;
8255e3986cbScth 			}
8265e3986cbScth 			break;
8275e3986cbScth 		} else if (DEVI_IS_DETACHING(phci_dip)) {
8285e3986cbScth 			vcircular = -1;
8295e3986cbScth 			break;
8304c06356bSdh142964 		} else if (servicing_interrupt()) {
8314c06356bSdh142964 			/*
8324c06356bSdh142964 			 * Don't delay an interrupt (and ensure adaptive
8334c06356bSdh142964 			 * mutex inversion support).
8344c06356bSdh142964 			 */
8354c06356bSdh142964 			ndi_devi_enter(vdip, &vcircular);
8364c06356bSdh142964 			break;
8375e3986cbScth 		} else {
83896c4a178SChris Horne 			delay_random(mdi_delay);
8395e3986cbScth 		}
8405e3986cbScth 	}
8415e3986cbScth 
8425e3986cbScth 	ndi_devi_enter(phci_dip, &pcircular);
8435e3986cbScth 	*circular = (vcircular << 16) | (pcircular & 0xFFFF);
8445e3986cbScth }
8455e3986cbScth 
8465e3986cbScth /*
84755e592a2SRandall Ralphs  * Attempt to mdi_devi_enter.
84855e592a2SRandall Ralphs  */
84955e592a2SRandall Ralphs int
85055e592a2SRandall Ralphs mdi_devi_tryenter(dev_info_t *phci_dip, int *circular)
85155e592a2SRandall Ralphs {
85255e592a2SRandall Ralphs 	dev_info_t	*vdip;
85355e592a2SRandall Ralphs 	int		vcircular, pcircular;
85455e592a2SRandall Ralphs 
85555e592a2SRandall Ralphs 	/* Verify calling context */
85655e592a2SRandall Ralphs 	ASSERT(MDI_PHCI(phci_dip));
85755e592a2SRandall Ralphs 	vdip = mdi_devi_get_vdip(phci_dip);
85855e592a2SRandall Ralphs 	ASSERT(vdip);			/* A pHCI always has a vHCI */
85955e592a2SRandall Ralphs 
86055e592a2SRandall Ralphs 	if (ndi_devi_tryenter(vdip, &vcircular)) {
86155e592a2SRandall Ralphs 		if (ndi_devi_tryenter(phci_dip, &pcircular)) {
86255e592a2SRandall Ralphs 			*circular = (vcircular << 16) | (pcircular & 0xFFFF);
86355e592a2SRandall Ralphs 			return (1);	/* locked */
86455e592a2SRandall Ralphs 		}
86555e592a2SRandall Ralphs 		ndi_devi_exit(vdip, vcircular);
86655e592a2SRandall Ralphs 	}
86755e592a2SRandall Ralphs 	return (0);			/* busy */
86855e592a2SRandall Ralphs }
86955e592a2SRandall Ralphs 
87055e592a2SRandall Ralphs /*
8715e3986cbScth  * Release mdi_devi_enter or successful mdi_devi_tryenter.
8725e3986cbScth  */
8735e3986cbScth void
8745e3986cbScth mdi_devi_exit(dev_info_t *phci_dip, int circular)
8755e3986cbScth {
8765e3986cbScth 	dev_info_t	*vdip;
8775e3986cbScth 	int		vcircular, pcircular;
8785e3986cbScth 
8795e3986cbScth 	/* Verify calling context */
8805e3986cbScth 	ASSERT(MDI_PHCI(phci_dip));
8815e3986cbScth 	vdip = mdi_devi_get_vdip(phci_dip);
8825e3986cbScth 	ASSERT(vdip);			/* A pHCI always has a vHCI */
8835e3986cbScth 
8845e3986cbScth 	/* extract two circular recursion values from single int */
8855e3986cbScth 	pcircular = (short)(circular & 0xFFFF);
8865e3986cbScth 	vcircular = (short)((circular >> 16) & 0xFFFF);
8875e3986cbScth 
8885e3986cbScth 	ndi_devi_exit(phci_dip, pcircular);
8895e3986cbScth 	if (vcircular != -1)
8905e3986cbScth 		ndi_devi_exit(vdip, vcircular);
8915e3986cbScth }
8925e3986cbScth 
8935e3986cbScth /*
8945e3986cbScth  * The functions mdi_devi_exit_phci() and mdi_devi_enter_phci() are used
8955e3986cbScth  * around a pHCI drivers calls to mdi_pi_online/offline, after holding
8965e3986cbScth  * the pathinfo node via mdi_hold_path/mdi_rele_path, to avoid deadlock
8975e3986cbScth  * with vHCI power management code during path online/offline.  Each
8985e3986cbScth  * mdi_devi_exit_phci must have a matching mdi_devi_enter_phci, and both must
8995e3986cbScth  * occur within the scope of an active mdi_devi_enter that establishes the
9005e3986cbScth  * circular value.
9015e3986cbScth  */
9025e3986cbScth void
9035e3986cbScth mdi_devi_exit_phci(dev_info_t *phci_dip, int circular)
9045e3986cbScth {
9055e3986cbScth 	int		pcircular;
9065e3986cbScth 
9075e3986cbScth 	/* Verify calling context */
9085e3986cbScth 	ASSERT(MDI_PHCI(phci_dip));
9095e3986cbScth 
9104c06356bSdh142964 	/* Keep hold on pHCI until we reenter in mdi_devi_enter_phci */
9114c06356bSdh142964 	ndi_hold_devi(phci_dip);
9124c06356bSdh142964 
9135e3986cbScth 	pcircular = (short)(circular & 0xFFFF);
9145e3986cbScth 	ndi_devi_exit(phci_dip, pcircular);
9155e3986cbScth }
9165e3986cbScth 
9175e3986cbScth void
9185e3986cbScth mdi_devi_enter_phci(dev_info_t *phci_dip, int *circular)
9195e3986cbScth {
9205e3986cbScth 	int		pcircular;
9215e3986cbScth 
9225e3986cbScth 	/* Verify calling context */
9235e3986cbScth 	ASSERT(MDI_PHCI(phci_dip));
9245e3986cbScth 
9255e3986cbScth 	ndi_devi_enter(phci_dip, &pcircular);
9265e3986cbScth 
9274c06356bSdh142964 	/* Drop hold from mdi_devi_exit_phci. */
9284c06356bSdh142964 	ndi_rele_devi(phci_dip);
9294c06356bSdh142964 
9305e3986cbScth 	/* verify matching mdi_devi_exit_phci/mdi_devi_enter_phci use */
9315e3986cbScth 	ASSERT(pcircular == ((short)(*circular & 0xFFFF)));
9325e3986cbScth }
9335e3986cbScth 
9345e3986cbScth /*
9355e3986cbScth  * mdi_devi_get_vdip():
9365e3986cbScth  *		given a pHCI dip return vHCI dip
9375e3986cbScth  */
9385e3986cbScth dev_info_t *
9395e3986cbScth mdi_devi_get_vdip(dev_info_t *pdip)
9405e3986cbScth {
9415e3986cbScth 	mdi_phci_t	*ph;
9425e3986cbScth 
9435e3986cbScth 	ph = i_devi_get_phci(pdip);
9445e3986cbScth 	if (ph && ph->ph_vhci)
9455e3986cbScth 		return (ph->ph_vhci->vh_dip);
9465e3986cbScth 	return (NULL);
9475e3986cbScth }
9485e3986cbScth 
9495e3986cbScth /*
9505e3986cbScth  * mdi_devi_pdip_entered():
9515e3986cbScth  *		Return 1 if we are vHCI and have done an ndi_devi_enter
9525e3986cbScth  *		of a pHCI
9535e3986cbScth  */
9545e3986cbScth int
9555e3986cbScth mdi_devi_pdip_entered(dev_info_t *vdip)
9565e3986cbScth {
9575e3986cbScth 	mdi_vhci_t	*vh;
9585e3986cbScth 	mdi_phci_t	*ph;
9595e3986cbScth 
9605e3986cbScth 	vh = i_devi_get_vhci(vdip);
9615e3986cbScth 	if (vh == NULL)
9625e3986cbScth 		return (0);
9635e3986cbScth 
9645e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
9655e3986cbScth 	ph = vh->vh_phci_head;
9665e3986cbScth 	while (ph) {
9675e3986cbScth 		if (ph->ph_dip && DEVI_BUSY_OWNED(ph->ph_dip)) {
9685e3986cbScth 			MDI_VHCI_PHCI_UNLOCK(vh);
9695e3986cbScth 			return (1);
9705e3986cbScth 		}
9715e3986cbScth 		ph = ph->ph_next;
9725e3986cbScth 	}
9735e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
9745e3986cbScth 	return (0);
9755e3986cbScth }
9765e3986cbScth 
9775e3986cbScth /*
9787c478bd9Sstevel@tonic-gate  * mdi_phci_path2devinfo():
9797c478bd9Sstevel@tonic-gate  * 		Utility function to search for a valid phci device given
9807c478bd9Sstevel@tonic-gate  *		the devfs pathname.
9817c478bd9Sstevel@tonic-gate  */
9827c478bd9Sstevel@tonic-gate dev_info_t *
9837c478bd9Sstevel@tonic-gate mdi_phci_path2devinfo(dev_info_t *vdip, caddr_t pathname)
9847c478bd9Sstevel@tonic-gate {
9857c478bd9Sstevel@tonic-gate 	char		*temp_pathname;
9867c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
9877c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
9887c478bd9Sstevel@tonic-gate 	dev_info_t 	*pdip = NULL;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	vh = i_devi_get_vhci(vdip);
9917c478bd9Sstevel@tonic-gate 	ASSERT(vh != NULL);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
9947c478bd9Sstevel@tonic-gate 		/*
9957c478bd9Sstevel@tonic-gate 		 * Invalid vHCI component, return failure
9967c478bd9Sstevel@tonic-gate 		 */
9977c478bd9Sstevel@tonic-gate 		return (NULL);
9987c478bd9Sstevel@tonic-gate 	}
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	temp_pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
10015e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
10027c478bd9Sstevel@tonic-gate 	ph = vh->vh_phci_head;
10037c478bd9Sstevel@tonic-gate 	while (ph != NULL) {
10047c478bd9Sstevel@tonic-gate 		pdip = ph->ph_dip;
10057c478bd9Sstevel@tonic-gate 		ASSERT(pdip != NULL);
10067c478bd9Sstevel@tonic-gate 		*temp_pathname = '\0';
10077c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(pdip, temp_pathname);
10087c478bd9Sstevel@tonic-gate 		if (strcmp(temp_pathname, pathname) == 0) {
10097c478bd9Sstevel@tonic-gate 			break;
10107c478bd9Sstevel@tonic-gate 		}
10117c478bd9Sstevel@tonic-gate 		ph = ph->ph_next;
10127c478bd9Sstevel@tonic-gate 	}
10137c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
10147c478bd9Sstevel@tonic-gate 		pdip = NULL;
10157c478bd9Sstevel@tonic-gate 	}
10165e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
10177c478bd9Sstevel@tonic-gate 	kmem_free(temp_pathname, MAXPATHLEN);
10187c478bd9Sstevel@tonic-gate 	return (pdip);
10197c478bd9Sstevel@tonic-gate }
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate /*
10227c478bd9Sstevel@tonic-gate  * mdi_phci_get_path_count():
10237c478bd9Sstevel@tonic-gate  * 		get number of path information nodes associated with a given
10247c478bd9Sstevel@tonic-gate  *		pHCI device.
10257c478bd9Sstevel@tonic-gate  */
10267c478bd9Sstevel@tonic-gate int
10277c478bd9Sstevel@tonic-gate mdi_phci_get_path_count(dev_info_t *pdip)
10287c478bd9Sstevel@tonic-gate {
10297c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
10307c478bd9Sstevel@tonic-gate 	int		count = 0;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
10337c478bd9Sstevel@tonic-gate 	if (ph != NULL) {
10347c478bd9Sstevel@tonic-gate 		count = ph->ph_path_count;
10357c478bd9Sstevel@tonic-gate 	}
10367c478bd9Sstevel@tonic-gate 	return (count);
10377c478bd9Sstevel@tonic-gate }
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate /*
10407c478bd9Sstevel@tonic-gate  * i_mdi_phci_lock():
10417c478bd9Sstevel@tonic-gate  *		Lock a pHCI device
10427c478bd9Sstevel@tonic-gate  * Return Values:
10437c478bd9Sstevel@tonic-gate  *		None
10447c478bd9Sstevel@tonic-gate  * Note:
10457c478bd9Sstevel@tonic-gate  *		The default locking order is:
10467c478bd9Sstevel@tonic-gate  *		_NOTE(LOCK_ORDER(mdi_phci::ph_mutex mdi_pathinfo::pi_mutex))
10477c478bd9Sstevel@tonic-gate  *		But there are number of situations where locks need to be
10487c478bd9Sstevel@tonic-gate  *		grabbed in reverse order.  This routine implements try and lock
10497c478bd9Sstevel@tonic-gate  *		mechanism depending on the requested parameter option.
10507c478bd9Sstevel@tonic-gate  */
10517c478bd9Sstevel@tonic-gate static void
10527c478bd9Sstevel@tonic-gate i_mdi_phci_lock(mdi_phci_t *ph, mdi_pathinfo_t *pip)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate 	if (pip) {
10557c478bd9Sstevel@tonic-gate 		/* Reverse locking is requested. */
10567c478bd9Sstevel@tonic-gate 		while (MDI_PHCI_TRYLOCK(ph) == 0) {
10574c06356bSdh142964 			if (servicing_interrupt()) {
10584c06356bSdh142964 				MDI_PI_HOLD(pip);
10594c06356bSdh142964 				MDI_PI_UNLOCK(pip);
10604c06356bSdh142964 				MDI_PHCI_LOCK(ph);
10614c06356bSdh142964 				MDI_PI_LOCK(pip);
10624c06356bSdh142964 				MDI_PI_RELE(pip);
10634c06356bSdh142964 				break;
10644c06356bSdh142964 			} else {
10657c478bd9Sstevel@tonic-gate 				/*
10667c478bd9Sstevel@tonic-gate 				 * tryenter failed. Try to grab again
10677c478bd9Sstevel@tonic-gate 				 * after a small delay
10687c478bd9Sstevel@tonic-gate 				 */
10697c478bd9Sstevel@tonic-gate 				MDI_PI_HOLD(pip);
10707c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
107196c4a178SChris Horne 				delay_random(mdi_delay);
10727c478bd9Sstevel@tonic-gate 				MDI_PI_LOCK(pip);
10737c478bd9Sstevel@tonic-gate 				MDI_PI_RELE(pip);
10747c478bd9Sstevel@tonic-gate 			}
10754c06356bSdh142964 		}
10767c478bd9Sstevel@tonic-gate 	} else {
10777c478bd9Sstevel@tonic-gate 		MDI_PHCI_LOCK(ph);
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate /*
10827c478bd9Sstevel@tonic-gate  * i_mdi_phci_unlock():
10837c478bd9Sstevel@tonic-gate  *		Unlock the pHCI component
10847c478bd9Sstevel@tonic-gate  */
10857c478bd9Sstevel@tonic-gate static void
10867c478bd9Sstevel@tonic-gate i_mdi_phci_unlock(mdi_phci_t *ph)
10877c478bd9Sstevel@tonic-gate {
10887c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate /*
10927c478bd9Sstevel@tonic-gate  * i_mdi_devinfo_create():
10937c478bd9Sstevel@tonic-gate  *		create client device's devinfo node
10947c478bd9Sstevel@tonic-gate  * Return Values:
10957c478bd9Sstevel@tonic-gate  *		dev_info
10967c478bd9Sstevel@tonic-gate  *		NULL
10977c478bd9Sstevel@tonic-gate  * Notes:
10987c478bd9Sstevel@tonic-gate  */
10997c478bd9Sstevel@tonic-gate static dev_info_t *
11007c478bd9Sstevel@tonic-gate i_mdi_devinfo_create(mdi_vhci_t *vh, char *name, char *guid,
11013c34adc5Sramat 	char **compatible, int ncompatible)
11027c478bd9Sstevel@tonic-gate {
11037c478bd9Sstevel@tonic-gate 	dev_info_t *cdip = NULL;
11047c478bd9Sstevel@tonic-gate 
11055e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	/* Verify for duplicate entry */
11087c478bd9Sstevel@tonic-gate 	cdip = i_mdi_devinfo_find(vh, name, guid);
11097c478bd9Sstevel@tonic-gate 	ASSERT(cdip == NULL);
11107c478bd9Sstevel@tonic-gate 	if (cdip) {
11117c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
11124c06356bSdh142964 		    "i_mdi_devinfo_create: client %s@%s already exists",
11134c06356bSdh142964 			name ? name : "", guid ? guid : "");
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 
11163c34adc5Sramat 	ndi_devi_alloc_sleep(vh->vh_dip, name, DEVI_SID_NODEID, &cdip);
11177c478bd9Sstevel@tonic-gate 	if (cdip == NULL)
11187c478bd9Sstevel@tonic-gate 		goto fail;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	/*
11217c478bd9Sstevel@tonic-gate 	 * Create component type and Global unique identifier
11227c478bd9Sstevel@tonic-gate 	 * properties
11237c478bd9Sstevel@tonic-gate 	 */
11247c478bd9Sstevel@tonic-gate 	if (ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
11257c478bd9Sstevel@tonic-gate 	    MDI_CLIENT_GUID_PROP, guid) != DDI_PROP_SUCCESS) {
11267c478bd9Sstevel@tonic-gate 		goto fail;
11277c478bd9Sstevel@tonic-gate 	}
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	/* Decorate the node with compatible property */
11307c478bd9Sstevel@tonic-gate 	if (compatible &&
11317c478bd9Sstevel@tonic-gate 	    (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip,
11327c478bd9Sstevel@tonic-gate 	    "compatible", compatible, ncompatible) != DDI_PROP_SUCCESS)) {
11337c478bd9Sstevel@tonic-gate 		goto fail;
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	return (cdip);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate fail:
11397c478bd9Sstevel@tonic-gate 	if (cdip) {
11407c478bd9Sstevel@tonic-gate 		(void) ndi_prop_remove_all(cdip);
11417c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(cdip);
11427c478bd9Sstevel@tonic-gate 	}
11437c478bd9Sstevel@tonic-gate 	return (NULL);
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate /*
11477c478bd9Sstevel@tonic-gate  * i_mdi_devinfo_find():
11487c478bd9Sstevel@tonic-gate  *		Find a matching devinfo node for given client node name
11497c478bd9Sstevel@tonic-gate  *		and its guid.
11507c478bd9Sstevel@tonic-gate  * Return Values:
11517c478bd9Sstevel@tonic-gate  *		Handle to a dev_info node or NULL
11527c478bd9Sstevel@tonic-gate  */
11537c478bd9Sstevel@tonic-gate static dev_info_t *
11547c478bd9Sstevel@tonic-gate i_mdi_devinfo_find(mdi_vhci_t *vh, caddr_t name, char *guid)
11557c478bd9Sstevel@tonic-gate {
11567c478bd9Sstevel@tonic-gate 	char			*data;
11577c478bd9Sstevel@tonic-gate 	dev_info_t 		*cdip = NULL;
11587c478bd9Sstevel@tonic-gate 	dev_info_t 		*ndip = NULL;
11597c478bd9Sstevel@tonic-gate 	int			circular;
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	ndi_devi_enter(vh->vh_dip, &circular);
11627c478bd9Sstevel@tonic-gate 	ndip = (dev_info_t *)DEVI(vh->vh_dip)->devi_child;
11637c478bd9Sstevel@tonic-gate 	while ((cdip = ndip) != NULL) {
11647c478bd9Sstevel@tonic-gate 		ndip = (dev_info_t *)DEVI(cdip)->devi_sibling;
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 		if (strcmp(DEVI(cdip)->devi_node_name, name)) {
11677c478bd9Sstevel@tonic-gate 			continue;
11687c478bd9Sstevel@tonic-gate 		}
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, cdip,
11717c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, MDI_CLIENT_GUID_PROP,
11727c478bd9Sstevel@tonic-gate 		    &data) != DDI_PROP_SUCCESS) {
11737c478bd9Sstevel@tonic-gate 			continue;
11747c478bd9Sstevel@tonic-gate 		}
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 		if (strcmp(data, guid) != 0) {
11777c478bd9Sstevel@tonic-gate 			ddi_prop_free(data);
11787c478bd9Sstevel@tonic-gate 			continue;
11797c478bd9Sstevel@tonic-gate 		}
11807c478bd9Sstevel@tonic-gate 		ddi_prop_free(data);
11817c478bd9Sstevel@tonic-gate 		break;
11827c478bd9Sstevel@tonic-gate 	}
11837c478bd9Sstevel@tonic-gate 	ndi_devi_exit(vh->vh_dip, circular);
11847c478bd9Sstevel@tonic-gate 	return (cdip);
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate /*
11887c478bd9Sstevel@tonic-gate  * i_mdi_devinfo_remove():
11897c478bd9Sstevel@tonic-gate  *		Remove a client device node
11907c478bd9Sstevel@tonic-gate  */
11917c478bd9Sstevel@tonic-gate static int
11927c478bd9Sstevel@tonic-gate i_mdi_devinfo_remove(dev_info_t *vdip, dev_info_t *cdip, int flags)
11937c478bd9Sstevel@tonic-gate {
11947c478bd9Sstevel@tonic-gate 	int	rv = MDI_SUCCESS;
11955e3986cbScth 
11967c478bd9Sstevel@tonic-gate 	if (i_mdi_is_child_present(vdip, cdip) == MDI_SUCCESS ||
11977c478bd9Sstevel@tonic-gate 	    (flags & MDI_CLIENT_FLAGS_DEV_NOT_SUPPORTED)) {
11984c06356bSdh142964 		rv = ndi_devi_offline(cdip, NDI_DEVFS_CLEAN | NDI_DEVI_REMOVE);
11997c478bd9Sstevel@tonic-gate 		if (rv != NDI_SUCCESS) {
12004c06356bSdh142964 			MDI_DEBUG(1, (MDI_NOTE, cdip,
12014c06356bSdh142964 			    "!failed: cdip %p", (void *)cdip));
12027c478bd9Sstevel@tonic-gate 		}
12037c478bd9Sstevel@tonic-gate 		/*
12047c478bd9Sstevel@tonic-gate 		 * Convert to MDI error code
12057c478bd9Sstevel@tonic-gate 		 */
12067c478bd9Sstevel@tonic-gate 		switch (rv) {
12077c478bd9Sstevel@tonic-gate 		case NDI_SUCCESS:
12087c478bd9Sstevel@tonic-gate 			rv = MDI_SUCCESS;
12097c478bd9Sstevel@tonic-gate 			break;
12107c478bd9Sstevel@tonic-gate 		case NDI_BUSY:
12117c478bd9Sstevel@tonic-gate 			rv = MDI_BUSY;
12127c478bd9Sstevel@tonic-gate 			break;
12137c478bd9Sstevel@tonic-gate 		default:
12147c478bd9Sstevel@tonic-gate 			rv = MDI_FAILURE;
12157c478bd9Sstevel@tonic-gate 			break;
12167c478bd9Sstevel@tonic-gate 		}
12177c478bd9Sstevel@tonic-gate 	}
12187c478bd9Sstevel@tonic-gate 	return (rv);
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate /*
12227c478bd9Sstevel@tonic-gate  * i_devi_get_client()
12237c478bd9Sstevel@tonic-gate  *		Utility function to get mpxio component extensions
12247c478bd9Sstevel@tonic-gate  */
12257c478bd9Sstevel@tonic-gate static mdi_client_t *
12267c478bd9Sstevel@tonic-gate i_devi_get_client(dev_info_t *cdip)
12277c478bd9Sstevel@tonic-gate {
12287c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct = NULL;
12295e3986cbScth 
12307c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(cdip)) {
12317c478bd9Sstevel@tonic-gate 		ct = (mdi_client_t *)DEVI(cdip)->devi_mdi_client;
12327c478bd9Sstevel@tonic-gate 	}
12337c478bd9Sstevel@tonic-gate 	return (ct);
12347c478bd9Sstevel@tonic-gate }
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate /*
12377c478bd9Sstevel@tonic-gate  * i_mdi_is_child_present():
12387c478bd9Sstevel@tonic-gate  *		Search for the presence of client device dev_info node
12397c478bd9Sstevel@tonic-gate  */
12407c478bd9Sstevel@tonic-gate static int
12417c478bd9Sstevel@tonic-gate i_mdi_is_child_present(dev_info_t *vdip, dev_info_t *cdip)
12427c478bd9Sstevel@tonic-gate {
12437c478bd9Sstevel@tonic-gate 	int		rv = MDI_FAILURE;
12447c478bd9Sstevel@tonic-gate 	struct dev_info	*dip;
12457c478bd9Sstevel@tonic-gate 	int		circular;
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	ndi_devi_enter(vdip, &circular);
12487c478bd9Sstevel@tonic-gate 	dip = DEVI(vdip)->devi_child;
12497c478bd9Sstevel@tonic-gate 	while (dip) {
12507c478bd9Sstevel@tonic-gate 		if (dip == DEVI(cdip)) {
12517c478bd9Sstevel@tonic-gate 			rv = MDI_SUCCESS;
12527c478bd9Sstevel@tonic-gate 			break;
12537c478bd9Sstevel@tonic-gate 		}
12547c478bd9Sstevel@tonic-gate 		dip = dip->devi_sibling;
12557c478bd9Sstevel@tonic-gate 	}
12567c478bd9Sstevel@tonic-gate 	ndi_devi_exit(vdip, circular);
12577c478bd9Sstevel@tonic-gate 	return (rv);
12587c478bd9Sstevel@tonic-gate }
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate /*
12627c478bd9Sstevel@tonic-gate  * i_mdi_client_lock():
12637c478bd9Sstevel@tonic-gate  *		Grab client component lock
12647c478bd9Sstevel@tonic-gate  * Return Values:
12657c478bd9Sstevel@tonic-gate  *		None
12667c478bd9Sstevel@tonic-gate  * Note:
12677c478bd9Sstevel@tonic-gate  *		The default locking order is:
12687c478bd9Sstevel@tonic-gate  *		_NOTE(LOCK_ORDER(mdi_client::ct_mutex mdi_pathinfo::pi_mutex))
12697c478bd9Sstevel@tonic-gate  *		But there are number of situations where locks need to be
12707c478bd9Sstevel@tonic-gate  *		grabbed in reverse order.  This routine implements try and lock
12717c478bd9Sstevel@tonic-gate  *		mechanism depending on the requested parameter option.
12727c478bd9Sstevel@tonic-gate  */
12737c478bd9Sstevel@tonic-gate static void
12747c478bd9Sstevel@tonic-gate i_mdi_client_lock(mdi_client_t *ct, mdi_pathinfo_t *pip)
12757c478bd9Sstevel@tonic-gate {
12767c478bd9Sstevel@tonic-gate 	if (pip) {
12777c478bd9Sstevel@tonic-gate 		/*
12787c478bd9Sstevel@tonic-gate 		 * Reverse locking is requested.
12797c478bd9Sstevel@tonic-gate 		 */
12807c478bd9Sstevel@tonic-gate 		while (MDI_CLIENT_TRYLOCK(ct) == 0) {
12814c06356bSdh142964 			if (servicing_interrupt()) {
12824c06356bSdh142964 				MDI_PI_HOLD(pip);
12834c06356bSdh142964 				MDI_PI_UNLOCK(pip);
12844c06356bSdh142964 				MDI_CLIENT_LOCK(ct);
12854c06356bSdh142964 				MDI_PI_LOCK(pip);
12864c06356bSdh142964 				MDI_PI_RELE(pip);
12874c06356bSdh142964 				break;
12884c06356bSdh142964 			} else {
12897c478bd9Sstevel@tonic-gate 				/*
12907c478bd9Sstevel@tonic-gate 				 * tryenter failed. Try to grab again
12917c478bd9Sstevel@tonic-gate 				 * after a small delay
12927c478bd9Sstevel@tonic-gate 				 */
12937c478bd9Sstevel@tonic-gate 				MDI_PI_HOLD(pip);
12947c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
129596c4a178SChris Horne 				delay_random(mdi_delay);
12967c478bd9Sstevel@tonic-gate 				MDI_PI_LOCK(pip);
12977c478bd9Sstevel@tonic-gate 				MDI_PI_RELE(pip);
12987c478bd9Sstevel@tonic-gate 			}
12994c06356bSdh142964 		}
13007c478bd9Sstevel@tonic-gate 	} else {
13017c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate }
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate /*
13067c478bd9Sstevel@tonic-gate  * i_mdi_client_unlock():
13077c478bd9Sstevel@tonic-gate  *		Unlock a client component
13087c478bd9Sstevel@tonic-gate  */
13097c478bd9Sstevel@tonic-gate static void
13107c478bd9Sstevel@tonic-gate i_mdi_client_unlock(mdi_client_t *ct)
13117c478bd9Sstevel@tonic-gate {
13127c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate /*
13167c478bd9Sstevel@tonic-gate  * i_mdi_client_alloc():
13177c478bd9Sstevel@tonic-gate  * 		Allocate and initialize a client structure.  Caller should
13185e3986cbScth  *		hold the vhci client lock.
13197c478bd9Sstevel@tonic-gate  * Return Values:
13207c478bd9Sstevel@tonic-gate  *		Handle to a client component
13217c478bd9Sstevel@tonic-gate  */
13227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13237c478bd9Sstevel@tonic-gate static mdi_client_t *
13243c34adc5Sramat i_mdi_client_alloc(mdi_vhci_t *vh, char *name, char *lguid)
13257c478bd9Sstevel@tonic-gate {
13267c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
13277c478bd9Sstevel@tonic-gate 
13285e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	/*
13317c478bd9Sstevel@tonic-gate 	 * Allocate and initialize a component structure.
13327c478bd9Sstevel@tonic-gate 	 */
13333c34adc5Sramat 	ct = kmem_zalloc(sizeof (*ct), KM_SLEEP);
13347c478bd9Sstevel@tonic-gate 	mutex_init(&ct->ct_mutex, NULL, MUTEX_DEFAULT, NULL);
13357c478bd9Sstevel@tonic-gate 	ct->ct_hnext = NULL;
13367c478bd9Sstevel@tonic-gate 	ct->ct_hprev = NULL;
13377c478bd9Sstevel@tonic-gate 	ct->ct_dip = NULL;
13387c478bd9Sstevel@tonic-gate 	ct->ct_vhci = vh;
13393c34adc5Sramat 	ct->ct_drvname = kmem_alloc(strlen(name) + 1, KM_SLEEP);
13407c478bd9Sstevel@tonic-gate 	(void) strcpy(ct->ct_drvname, name);
13413c34adc5Sramat 	ct->ct_guid = kmem_alloc(strlen(lguid) + 1, KM_SLEEP);
13427c478bd9Sstevel@tonic-gate 	(void) strcpy(ct->ct_guid, lguid);
13437c478bd9Sstevel@tonic-gate 	ct->ct_cprivate = NULL;
13447c478bd9Sstevel@tonic-gate 	ct->ct_vprivate = NULL;
13457c478bd9Sstevel@tonic-gate 	ct->ct_flags = 0;
13467c478bd9Sstevel@tonic-gate 	ct->ct_state = MDI_CLIENT_STATE_FAILED;
13475e3986cbScth 	MDI_CLIENT_LOCK(ct);
13487c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_OFFLINE(ct);
13497c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_DETACH(ct);
13507c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_POWER_UP(ct);
13515e3986cbScth 	MDI_CLIENT_UNLOCK(ct);
13527c478bd9Sstevel@tonic-gate 	ct->ct_failover_flags = 0;
13537c478bd9Sstevel@tonic-gate 	ct->ct_failover_status = 0;
13547c478bd9Sstevel@tonic-gate 	cv_init(&ct->ct_failover_cv, NULL, CV_DRIVER, NULL);
13557c478bd9Sstevel@tonic-gate 	ct->ct_unstable = 0;
13567c478bd9Sstevel@tonic-gate 	cv_init(&ct->ct_unstable_cv, NULL, CV_DRIVER, NULL);
13577c478bd9Sstevel@tonic-gate 	cv_init(&ct->ct_powerchange_cv, NULL, CV_DRIVER, NULL);
13587c478bd9Sstevel@tonic-gate 	ct->ct_lb = vh->vh_lb;
13593c34adc5Sramat 	ct->ct_lb_args =  kmem_zalloc(sizeof (client_lb_args_t), KM_SLEEP);
13607c478bd9Sstevel@tonic-gate 	ct->ct_lb_args->region_size = LOAD_BALANCE_DEFAULT_REGION_SIZE;
13617c478bd9Sstevel@tonic-gate 	ct->ct_path_count = 0;
13627c478bd9Sstevel@tonic-gate 	ct->ct_path_head = NULL;
13637c478bd9Sstevel@tonic-gate 	ct->ct_path_tail = NULL;
13647c478bd9Sstevel@tonic-gate 	ct->ct_path_last = NULL;
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	/*
13677c478bd9Sstevel@tonic-gate 	 * Add this client component to our client hash queue
13687c478bd9Sstevel@tonic-gate 	 */
13697c478bd9Sstevel@tonic-gate 	i_mdi_client_enlist_table(vh, ct);
13707c478bd9Sstevel@tonic-gate 	return (ct);
13717c478bd9Sstevel@tonic-gate }
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate /*
13747c478bd9Sstevel@tonic-gate  * i_mdi_client_enlist_table():
13757c478bd9Sstevel@tonic-gate  *		Attach the client device to the client hash table. Caller
13765e3986cbScth  *		should hold the vhci client lock.
13777c478bd9Sstevel@tonic-gate  */
13787c478bd9Sstevel@tonic-gate static void
13797c478bd9Sstevel@tonic-gate i_mdi_client_enlist_table(mdi_vhci_t *vh, mdi_client_t *ct)
13807c478bd9Sstevel@tonic-gate {
13817c478bd9Sstevel@tonic-gate 	int 			index;
13827c478bd9Sstevel@tonic-gate 	struct client_hash	*head;
13837c478bd9Sstevel@tonic-gate 
13845e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
13855e3986cbScth 
13867c478bd9Sstevel@tonic-gate 	index = i_mdi_get_hash_key(ct->ct_guid);
13877c478bd9Sstevel@tonic-gate 	head = &vh->vh_client_table[index];
13887c478bd9Sstevel@tonic-gate 	ct->ct_hnext = (mdi_client_t *)head->ct_hash_head;
13897c478bd9Sstevel@tonic-gate 	head->ct_hash_head = ct;
13907c478bd9Sstevel@tonic-gate 	head->ct_hash_count++;
13917c478bd9Sstevel@tonic-gate 	vh->vh_client_count++;
13927c478bd9Sstevel@tonic-gate }
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate /*
13957c478bd9Sstevel@tonic-gate  * i_mdi_client_delist_table():
13967c478bd9Sstevel@tonic-gate  *		Attach the client device to the client hash table.
13975e3986cbScth  *		Caller should hold the vhci client lock.
13987c478bd9Sstevel@tonic-gate  */
13997c478bd9Sstevel@tonic-gate static void
14007c478bd9Sstevel@tonic-gate i_mdi_client_delist_table(mdi_vhci_t *vh, mdi_client_t *ct)
14017c478bd9Sstevel@tonic-gate {
14027c478bd9Sstevel@tonic-gate 	int			index;
14037c478bd9Sstevel@tonic-gate 	char			*guid;
14047c478bd9Sstevel@tonic-gate 	struct client_hash 	*head;
14057c478bd9Sstevel@tonic-gate 	mdi_client_t		*next;
14067c478bd9Sstevel@tonic-gate 	mdi_client_t		*last;
14077c478bd9Sstevel@tonic-gate 
14085e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
14095e3986cbScth 
14107c478bd9Sstevel@tonic-gate 	guid = ct->ct_guid;
14117c478bd9Sstevel@tonic-gate 	index = i_mdi_get_hash_key(guid);
14127c478bd9Sstevel@tonic-gate 	head = &vh->vh_client_table[index];
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	last = NULL;
14157c478bd9Sstevel@tonic-gate 	next = (mdi_client_t *)head->ct_hash_head;
14167c478bd9Sstevel@tonic-gate 	while (next != NULL) {
14177c478bd9Sstevel@tonic-gate 		if (next == ct) {
14187c478bd9Sstevel@tonic-gate 			break;
14197c478bd9Sstevel@tonic-gate 		}
14207c478bd9Sstevel@tonic-gate 		last = next;
14217c478bd9Sstevel@tonic-gate 		next = next->ct_hnext;
14227c478bd9Sstevel@tonic-gate 	}
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	if (next) {
14257c478bd9Sstevel@tonic-gate 		head->ct_hash_count--;
14267c478bd9Sstevel@tonic-gate 		if (last == NULL) {
14277c478bd9Sstevel@tonic-gate 			head->ct_hash_head = ct->ct_hnext;
14287c478bd9Sstevel@tonic-gate 		} else {
14297c478bd9Sstevel@tonic-gate 			last->ct_hnext = ct->ct_hnext;
14307c478bd9Sstevel@tonic-gate 		}
14317c478bd9Sstevel@tonic-gate 		ct->ct_hnext = NULL;
14327c478bd9Sstevel@tonic-gate 		vh->vh_client_count--;
14337c478bd9Sstevel@tonic-gate 	}
14347c478bd9Sstevel@tonic-gate }
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate /*
14387c478bd9Sstevel@tonic-gate  * i_mdi_client_free():
14397c478bd9Sstevel@tonic-gate  *		Free a client component
14407c478bd9Sstevel@tonic-gate  */
14417c478bd9Sstevel@tonic-gate static int
14427c478bd9Sstevel@tonic-gate i_mdi_client_free(mdi_vhci_t *vh, mdi_client_t *ct)
14437c478bd9Sstevel@tonic-gate {
14447c478bd9Sstevel@tonic-gate 	int		rv = MDI_SUCCESS;
14457c478bd9Sstevel@tonic-gate 	int		flags = ct->ct_flags;
14467c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
14477c478bd9Sstevel@tonic-gate 	dev_info_t	*vdip;
14487c478bd9Sstevel@tonic-gate 
14495e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
14505e3986cbScth 
14517c478bd9Sstevel@tonic-gate 	vdip = vh->vh_dip;
14527c478bd9Sstevel@tonic-gate 	cdip = ct->ct_dip;
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	(void) ndi_prop_remove(DDI_DEV_T_NONE, cdip, MDI_CLIENT_GUID_PROP);
14557c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_component &= ~MDI_COMPONENT_CLIENT;
14567c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_client = NULL;
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	/*
14597c478bd9Sstevel@tonic-gate 	 * Clear out back ref. to dev_info_t node
14607c478bd9Sstevel@tonic-gate 	 */
14617c478bd9Sstevel@tonic-gate 	ct->ct_dip = NULL;
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	/*
14647c478bd9Sstevel@tonic-gate 	 * Remove this client from our hash queue
14657c478bd9Sstevel@tonic-gate 	 */
14667c478bd9Sstevel@tonic-gate 	i_mdi_client_delist_table(vh, ct);
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	/*
14697c478bd9Sstevel@tonic-gate 	 * Uninitialize and free the component
14707c478bd9Sstevel@tonic-gate 	 */
14717c478bd9Sstevel@tonic-gate 	kmem_free(ct->ct_drvname, strlen(ct->ct_drvname) + 1);
14727c478bd9Sstevel@tonic-gate 	kmem_free(ct->ct_guid, strlen(ct->ct_guid) + 1);
14737c478bd9Sstevel@tonic-gate 	kmem_free(ct->ct_lb_args, sizeof (client_lb_args_t));
14747c478bd9Sstevel@tonic-gate 	cv_destroy(&ct->ct_failover_cv);
14757c478bd9Sstevel@tonic-gate 	cv_destroy(&ct->ct_unstable_cv);
14767c478bd9Sstevel@tonic-gate 	cv_destroy(&ct->ct_powerchange_cv);
14777c478bd9Sstevel@tonic-gate 	mutex_destroy(&ct->ct_mutex);
14787c478bd9Sstevel@tonic-gate 	kmem_free(ct, sizeof (*ct));
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	if (cdip != NULL) {
14815e3986cbScth 		MDI_VHCI_CLIENT_UNLOCK(vh);
14827c478bd9Sstevel@tonic-gate 		(void) i_mdi_devinfo_remove(vdip, cdip, flags);
14835e3986cbScth 		MDI_VHCI_CLIENT_LOCK(vh);
14847c478bd9Sstevel@tonic-gate 	}
14857c478bd9Sstevel@tonic-gate 	return (rv);
14867c478bd9Sstevel@tonic-gate }
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate /*
14897c478bd9Sstevel@tonic-gate  * i_mdi_client_find():
14907c478bd9Sstevel@tonic-gate  * 		Find the client structure corresponding to a given guid
14915e3986cbScth  *		Caller should hold the vhci client lock.
14927c478bd9Sstevel@tonic-gate  */
14937c478bd9Sstevel@tonic-gate static mdi_client_t *
14943c34adc5Sramat i_mdi_client_find(mdi_vhci_t *vh, char *cname, char *guid)
14957c478bd9Sstevel@tonic-gate {
14967c478bd9Sstevel@tonic-gate 	int			index;
14977c478bd9Sstevel@tonic-gate 	struct client_hash	*head;
14987c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
14997c478bd9Sstevel@tonic-gate 
15005e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
15015e3986cbScth 
15027c478bd9Sstevel@tonic-gate 	index = i_mdi_get_hash_key(guid);
15037c478bd9Sstevel@tonic-gate 	head = &vh->vh_client_table[index];
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	ct = head->ct_hash_head;
15067c478bd9Sstevel@tonic-gate 	while (ct != NULL) {
15073c34adc5Sramat 		if (strcmp(ct->ct_guid, guid) == 0 &&
15083c34adc5Sramat 		    (cname == NULL || strcmp(ct->ct_drvname, cname) == 0)) {
15097c478bd9Sstevel@tonic-gate 			break;
15107c478bd9Sstevel@tonic-gate 		}
15117c478bd9Sstevel@tonic-gate 		ct = ct->ct_hnext;
15127c478bd9Sstevel@tonic-gate 	}
15137c478bd9Sstevel@tonic-gate 	return (ct);
15147c478bd9Sstevel@tonic-gate }
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate /*
15177c478bd9Sstevel@tonic-gate  * i_mdi_client_update_state():
15187c478bd9Sstevel@tonic-gate  *		Compute and update client device state
15197c478bd9Sstevel@tonic-gate  * Notes:
15207c478bd9Sstevel@tonic-gate  *		A client device can be in any of three possible states:
15217c478bd9Sstevel@tonic-gate  *
15227c478bd9Sstevel@tonic-gate  *		MDI_CLIENT_STATE_OPTIMAL - Client in optimal state with more
15237c478bd9Sstevel@tonic-gate  *		one online/standby paths. Can tolerate failures.
15247c478bd9Sstevel@tonic-gate  *		MDI_CLIENT_STATE_DEGRADED - Client device in degraded state with
15257c478bd9Sstevel@tonic-gate  *		no alternate paths available as standby. A failure on the online
15267c478bd9Sstevel@tonic-gate  *		would result in loss of access to device data.
15277c478bd9Sstevel@tonic-gate  *		MDI_CLIENT_STATE_FAILED - Client device in failed state with
15287c478bd9Sstevel@tonic-gate  *		no paths available to access the device.
15297c478bd9Sstevel@tonic-gate  */
15307c478bd9Sstevel@tonic-gate static void
15317c478bd9Sstevel@tonic-gate i_mdi_client_update_state(mdi_client_t *ct)
15327c478bd9Sstevel@tonic-gate {
15337c478bd9Sstevel@tonic-gate 	int state;
15345e3986cbScth 
15355e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
15367c478bd9Sstevel@tonic-gate 	state = i_mdi_client_compute_state(ct, NULL);
15377c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_STATE(ct, state);
15387c478bd9Sstevel@tonic-gate }
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate /*
15417c478bd9Sstevel@tonic-gate  * i_mdi_client_compute_state():
15427c478bd9Sstevel@tonic-gate  *		Compute client device state
15437c478bd9Sstevel@tonic-gate  *
15447c478bd9Sstevel@tonic-gate  *		mdi_phci_t *	Pointer to pHCI structure which should
15457c478bd9Sstevel@tonic-gate  *				while computing the new value.  Used by
15467c478bd9Sstevel@tonic-gate  *				i_mdi_phci_offline() to find the new
15477c478bd9Sstevel@tonic-gate  *				client state after DR of a pHCI.
15487c478bd9Sstevel@tonic-gate  */
15497c478bd9Sstevel@tonic-gate static int
15507c478bd9Sstevel@tonic-gate i_mdi_client_compute_state(mdi_client_t *ct, mdi_phci_t *ph)
15517c478bd9Sstevel@tonic-gate {
15527c478bd9Sstevel@tonic-gate 	int		state;
15537c478bd9Sstevel@tonic-gate 	int		online_count = 0;
15547c478bd9Sstevel@tonic-gate 	int		standby_count = 0;
15557c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip, *next;
15567c478bd9Sstevel@tonic-gate 
15575e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
15587c478bd9Sstevel@tonic-gate 	pip = ct->ct_path_head;
15597c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
15607c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
15617c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
15627c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_phci == ph) {
15637c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
15647c478bd9Sstevel@tonic-gate 			pip = next;
15657c478bd9Sstevel@tonic-gate 			continue;
15667c478bd9Sstevel@tonic-gate 		}
15675e3986cbScth 
15687c478bd9Sstevel@tonic-gate 		if ((MDI_PI(pip)->pi_state & MDI_PATHINFO_STATE_MASK)
15697c478bd9Sstevel@tonic-gate 				== MDI_PATHINFO_STATE_ONLINE)
15707c478bd9Sstevel@tonic-gate 			online_count++;
15717c478bd9Sstevel@tonic-gate 		else if ((MDI_PI(pip)->pi_state & MDI_PATHINFO_STATE_MASK)
15727c478bd9Sstevel@tonic-gate 				== MDI_PATHINFO_STATE_STANDBY)
15737c478bd9Sstevel@tonic-gate 			standby_count++;
15747c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
15757c478bd9Sstevel@tonic-gate 		pip = next;
15767c478bd9Sstevel@tonic-gate 	}
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	if (online_count == 0) {
15797c478bd9Sstevel@tonic-gate 		if (standby_count == 0) {
15807c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_FAILED;
15814c06356bSdh142964 			MDI_DEBUG(2, (MDI_NOTE, ct->ct_dip,
15824c06356bSdh142964 			    "client state failed: ct = %p", (void *)ct));
15837c478bd9Sstevel@tonic-gate 		} else if (standby_count == 1) {
15847c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_DEGRADED;
15857c478bd9Sstevel@tonic-gate 		} else {
15867c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_OPTIMAL;
15877c478bd9Sstevel@tonic-gate 		}
15887c478bd9Sstevel@tonic-gate 	} else if (online_count == 1) {
15897c478bd9Sstevel@tonic-gate 		if (standby_count == 0) {
15907c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_DEGRADED;
15917c478bd9Sstevel@tonic-gate 		} else {
15927c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_OPTIMAL;
15937c478bd9Sstevel@tonic-gate 		}
15947c478bd9Sstevel@tonic-gate 	} else {
15957c478bd9Sstevel@tonic-gate 		state = MDI_CLIENT_STATE_OPTIMAL;
15967c478bd9Sstevel@tonic-gate 	}
15977c478bd9Sstevel@tonic-gate 	return (state);
15987c478bd9Sstevel@tonic-gate }
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate /*
16017c478bd9Sstevel@tonic-gate  * i_mdi_client2devinfo():
16027c478bd9Sstevel@tonic-gate  *		Utility function
16037c478bd9Sstevel@tonic-gate  */
16047c478bd9Sstevel@tonic-gate dev_info_t *
16057c478bd9Sstevel@tonic-gate i_mdi_client2devinfo(mdi_client_t *ct)
16067c478bd9Sstevel@tonic-gate {
16077c478bd9Sstevel@tonic-gate 	return (ct->ct_dip);
16087c478bd9Sstevel@tonic-gate }
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate /*
16117c478bd9Sstevel@tonic-gate  * mdi_client_path2_devinfo():
16127c478bd9Sstevel@tonic-gate  * 		Given the parent devinfo and child devfs pathname, search for
16137c478bd9Sstevel@tonic-gate  *		a valid devfs node handle.
16147c478bd9Sstevel@tonic-gate  */
16157c478bd9Sstevel@tonic-gate dev_info_t *
16167c478bd9Sstevel@tonic-gate mdi_client_path2devinfo(dev_info_t *vdip, char *pathname)
16177c478bd9Sstevel@tonic-gate {
16187c478bd9Sstevel@tonic-gate 	dev_info_t 	*cdip = NULL;
16197c478bd9Sstevel@tonic-gate 	dev_info_t 	*ndip = NULL;
16207c478bd9Sstevel@tonic-gate 	char		*temp_pathname;
16217c478bd9Sstevel@tonic-gate 	int		circular;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	/*
16247c478bd9Sstevel@tonic-gate 	 * Allocate temp buffer
16257c478bd9Sstevel@tonic-gate 	 */
16267c478bd9Sstevel@tonic-gate 	temp_pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	/*
16297c478bd9Sstevel@tonic-gate 	 * Lock parent against changes
16307c478bd9Sstevel@tonic-gate 	 */
16317c478bd9Sstevel@tonic-gate 	ndi_devi_enter(vdip, &circular);
16327c478bd9Sstevel@tonic-gate 	ndip = (dev_info_t *)DEVI(vdip)->devi_child;
16337c478bd9Sstevel@tonic-gate 	while ((cdip = ndip) != NULL) {
16347c478bd9Sstevel@tonic-gate 		ndip = (dev_info_t *)DEVI(cdip)->devi_sibling;
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 		*temp_pathname = '\0';
16377c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(cdip, temp_pathname);
16387c478bd9Sstevel@tonic-gate 		if (strcmp(temp_pathname, pathname) == 0) {
16397c478bd9Sstevel@tonic-gate 			break;
16407c478bd9Sstevel@tonic-gate 		}
16417c478bd9Sstevel@tonic-gate 	}
16427c478bd9Sstevel@tonic-gate 	/*
16437c478bd9Sstevel@tonic-gate 	 * Release devinfo lock
16447c478bd9Sstevel@tonic-gate 	 */
16457c478bd9Sstevel@tonic-gate 	ndi_devi_exit(vdip, circular);
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	/*
16487c478bd9Sstevel@tonic-gate 	 * Free the temp buffer
16497c478bd9Sstevel@tonic-gate 	 */
16507c478bd9Sstevel@tonic-gate 	kmem_free(temp_pathname, MAXPATHLEN);
16517c478bd9Sstevel@tonic-gate 	return (cdip);
16527c478bd9Sstevel@tonic-gate }
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate /*
16557c478bd9Sstevel@tonic-gate  * mdi_client_get_path_count():
16567c478bd9Sstevel@tonic-gate  * 		Utility function to get number of path information nodes
16577c478bd9Sstevel@tonic-gate  *		associated with a given client device.
16587c478bd9Sstevel@tonic-gate  */
16597c478bd9Sstevel@tonic-gate int
16607c478bd9Sstevel@tonic-gate mdi_client_get_path_count(dev_info_t *cdip)
16617c478bd9Sstevel@tonic-gate {
16627c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
16637c478bd9Sstevel@tonic-gate 	int		count = 0;
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
16667c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
16677c478bd9Sstevel@tonic-gate 		count = ct->ct_path_count;
16687c478bd9Sstevel@tonic-gate 	}
16697c478bd9Sstevel@tonic-gate 	return (count);
16707c478bd9Sstevel@tonic-gate }
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate /*
16747c478bd9Sstevel@tonic-gate  * i_mdi_get_hash_key():
16757c478bd9Sstevel@tonic-gate  * 		Create a hash using strings as keys
16767c478bd9Sstevel@tonic-gate  *
16777c478bd9Sstevel@tonic-gate  */
16787c478bd9Sstevel@tonic-gate static int
16797c478bd9Sstevel@tonic-gate i_mdi_get_hash_key(char *str)
16807c478bd9Sstevel@tonic-gate {
16817c478bd9Sstevel@tonic-gate 	uint32_t	g, hash = 0;
16827c478bd9Sstevel@tonic-gate 	char		*p;
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	for (p = str; *p != '\0'; p++) {
16857c478bd9Sstevel@tonic-gate 		g = *p;
16867c478bd9Sstevel@tonic-gate 		hash += g;
16877c478bd9Sstevel@tonic-gate 	}
16887c478bd9Sstevel@tonic-gate 	return (hash % (CLIENT_HASH_TABLE_SIZE - 1));
16897c478bd9Sstevel@tonic-gate }
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate /*
16927c478bd9Sstevel@tonic-gate  * mdi_get_lb_policy():
16937c478bd9Sstevel@tonic-gate  * 		Get current load balancing policy for a given client device
16947c478bd9Sstevel@tonic-gate  */
16957c478bd9Sstevel@tonic-gate client_lb_t
16967c478bd9Sstevel@tonic-gate mdi_get_lb_policy(dev_info_t *cdip)
16977c478bd9Sstevel@tonic-gate {
16987c478bd9Sstevel@tonic-gate 	client_lb_t	lb = LOAD_BALANCE_NONE;
16997c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
17027c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
17037c478bd9Sstevel@tonic-gate 		lb = ct->ct_lb;
17047c478bd9Sstevel@tonic-gate 	}
17057c478bd9Sstevel@tonic-gate 	return (lb);
17067c478bd9Sstevel@tonic-gate }
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate /*
17097c478bd9Sstevel@tonic-gate  * mdi_set_lb_region_size():
17107c478bd9Sstevel@tonic-gate  * 		Set current region size for the load-balance
17117c478bd9Sstevel@tonic-gate  */
17127c478bd9Sstevel@tonic-gate int
17137c478bd9Sstevel@tonic-gate mdi_set_lb_region_size(dev_info_t *cdip, int region_size)
17147c478bd9Sstevel@tonic-gate {
17157c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
17167c478bd9Sstevel@tonic-gate 	int		rv = MDI_FAILURE;
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
17197c478bd9Sstevel@tonic-gate 	if (ct != NULL && ct->ct_lb_args != NULL) {
17207c478bd9Sstevel@tonic-gate 		ct->ct_lb_args->region_size = region_size;
17217c478bd9Sstevel@tonic-gate 		rv = MDI_SUCCESS;
17227c478bd9Sstevel@tonic-gate 	}
17237c478bd9Sstevel@tonic-gate 	return (rv);
17247c478bd9Sstevel@tonic-gate }
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate /*
17277c478bd9Sstevel@tonic-gate  * mdi_Set_lb_policy():
17287c478bd9Sstevel@tonic-gate  * 		Set current load balancing policy for a given client device
17297c478bd9Sstevel@tonic-gate  */
17307c478bd9Sstevel@tonic-gate int
17317c478bd9Sstevel@tonic-gate mdi_set_lb_policy(dev_info_t *cdip, client_lb_t lb)
17327c478bd9Sstevel@tonic-gate {
17337c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
17347c478bd9Sstevel@tonic-gate 	int		rv = MDI_FAILURE;
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
17377c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
17387c478bd9Sstevel@tonic-gate 		ct->ct_lb = lb;
17397c478bd9Sstevel@tonic-gate 		rv = MDI_SUCCESS;
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate 	return (rv);
17427c478bd9Sstevel@tonic-gate }
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate /*
17457c478bd9Sstevel@tonic-gate  * mdi_failover():
17467c478bd9Sstevel@tonic-gate  *		failover function called by the vHCI drivers to initiate
17477c478bd9Sstevel@tonic-gate  *		a failover operation.  This is typically due to non-availability
17487c478bd9Sstevel@tonic-gate  *		of online paths to route I/O requests.  Failover can be
17497c478bd9Sstevel@tonic-gate  *		triggered through user application also.
17507c478bd9Sstevel@tonic-gate  *
17517c478bd9Sstevel@tonic-gate  *		The vHCI driver calls mdi_failover() to initiate a failover
17527c478bd9Sstevel@tonic-gate  *		operation. mdi_failover() calls back into the vHCI driver's
17537c478bd9Sstevel@tonic-gate  *		vo_failover() entry point to perform the actual failover
17547c478bd9Sstevel@tonic-gate  *		operation.  The reason for requiring the vHCI driver to
17557c478bd9Sstevel@tonic-gate  *		initiate failover by calling mdi_failover(), instead of directly
17567c478bd9Sstevel@tonic-gate  *		executing vo_failover() itself, is to ensure that the mdi
17577c478bd9Sstevel@tonic-gate  *		framework can keep track of the client state properly.
17587c478bd9Sstevel@tonic-gate  *		Additionally, mdi_failover() provides as a convenience the
17597c478bd9Sstevel@tonic-gate  *		option of performing the failover operation synchronously or
17607c478bd9Sstevel@tonic-gate  *		asynchronously
17617c478bd9Sstevel@tonic-gate  *
17627c478bd9Sstevel@tonic-gate  *		Upon successful completion of the failover operation, the
17637c478bd9Sstevel@tonic-gate  *		paths that were previously ONLINE will be in the STANDBY state,
17647c478bd9Sstevel@tonic-gate  *		and the newly activated paths will be in the ONLINE state.
17657c478bd9Sstevel@tonic-gate  *
17667c478bd9Sstevel@tonic-gate  *		The flags modifier determines whether the activation is done
17677c478bd9Sstevel@tonic-gate  *		synchronously: MDI_FAILOVER_SYNC
17687c478bd9Sstevel@tonic-gate  * Return Values:
17697c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
17707c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
17717c478bd9Sstevel@tonic-gate  *		MDI_BUSY
17727c478bd9Sstevel@tonic-gate  */
17737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17747c478bd9Sstevel@tonic-gate int
17757c478bd9Sstevel@tonic-gate mdi_failover(dev_info_t *vdip, dev_info_t *cdip, int flags)
17767c478bd9Sstevel@tonic-gate {
17777c478bd9Sstevel@tonic-gate 	int			rv;
17787c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
17817c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
17827c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
17837c478bd9Sstevel@tonic-gate 		/* cdip is not a valid client device. Nothing more to do. */
17847c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
17857c478bd9Sstevel@tonic-gate 	}
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_PATH_FREE_IN_PROGRESS(ct)) {
17907c478bd9Sstevel@tonic-gate 		/* A path to the client is being freed */
17917c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
17927c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
17937c478bd9Sstevel@tonic-gate 	}
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_FAILED(ct)) {
17977c478bd9Sstevel@tonic-gate 		/*
17987c478bd9Sstevel@tonic-gate 		 * Client is in failed state. Nothing more to do.
17997c478bd9Sstevel@tonic-gate 		 */
18007c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
18017c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
18027c478bd9Sstevel@tonic-gate 	}
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
18057c478bd9Sstevel@tonic-gate 		/*
18067c478bd9Sstevel@tonic-gate 		 * Failover is already in progress; return BUSY
18077c478bd9Sstevel@tonic-gate 		 */
18087c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
18097c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
18107c478bd9Sstevel@tonic-gate 	}
18117c478bd9Sstevel@tonic-gate 	/*
18127c478bd9Sstevel@tonic-gate 	 * Make sure that mdi_pathinfo node state changes are processed.
18137c478bd9Sstevel@tonic-gate 	 * We do not allow failovers to progress while client path state
18147c478bd9Sstevel@tonic-gate 	 * changes are in progress
18157c478bd9Sstevel@tonic-gate 	 */
18167c478bd9Sstevel@tonic-gate 	if (ct->ct_unstable) {
18177c478bd9Sstevel@tonic-gate 		if (flags == MDI_FAILOVER_ASYNC) {
18187c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
18197c478bd9Sstevel@tonic-gate 			return (MDI_BUSY);
18207c478bd9Sstevel@tonic-gate 		} else {
18217c478bd9Sstevel@tonic-gate 			while (ct->ct_unstable)
18227c478bd9Sstevel@tonic-gate 				cv_wait(&ct->ct_unstable_cv, &ct->ct_mutex);
18237c478bd9Sstevel@tonic-gate 		}
18247c478bd9Sstevel@tonic-gate 	}
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	/*
18277c478bd9Sstevel@tonic-gate 	 * Client device is in stable state. Before proceeding, perform sanity
18287c478bd9Sstevel@tonic-gate 	 * checks again.
18297c478bd9Sstevel@tonic-gate 	 */
18307c478bd9Sstevel@tonic-gate 	if ((MDI_CLIENT_IS_DETACHED(ct)) || (MDI_CLIENT_IS_FAILED(ct)) ||
1831737d277aScth 	    (!i_ddi_devi_attached(ct->ct_dip))) {
18327c478bd9Sstevel@tonic-gate 		/*
18337c478bd9Sstevel@tonic-gate 		 * Client is in failed state. Nothing more to do.
18347c478bd9Sstevel@tonic-gate 		 */
18357c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
18367c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
18377c478bd9Sstevel@tonic-gate 	}
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	/*
18407c478bd9Sstevel@tonic-gate 	 * Set the client state as failover in progress.
18417c478bd9Sstevel@tonic-gate 	 */
18427c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_FAILOVER_IN_PROGRESS(ct);
18437c478bd9Sstevel@tonic-gate 	ct->ct_failover_flags = flags;
18447c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	if (flags == MDI_FAILOVER_ASYNC) {
18477c478bd9Sstevel@tonic-gate 		/*
18487c478bd9Sstevel@tonic-gate 		 * Submit the initiate failover request via CPR safe
18497c478bd9Sstevel@tonic-gate 		 * taskq threads.
18507c478bd9Sstevel@tonic-gate 		 */
18517c478bd9Sstevel@tonic-gate 		(void) taskq_dispatch(mdi_taskq, (task_func_t *)i_mdi_failover,
18527c478bd9Sstevel@tonic-gate 		    ct, KM_SLEEP);
18537c478bd9Sstevel@tonic-gate 		return (MDI_ACCEPT);
18547c478bd9Sstevel@tonic-gate 	} else {
18557c478bd9Sstevel@tonic-gate 		/*
18567c478bd9Sstevel@tonic-gate 		 * Synchronous failover mode.  Typically invoked from the user
18577c478bd9Sstevel@tonic-gate 		 * land.
18587c478bd9Sstevel@tonic-gate 		 */
18597c478bd9Sstevel@tonic-gate 		rv = i_mdi_failover(ct);
18607c478bd9Sstevel@tonic-gate 	}
18617c478bd9Sstevel@tonic-gate 	return (rv);
18627c478bd9Sstevel@tonic-gate }
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate /*
18657c478bd9Sstevel@tonic-gate  * i_mdi_failover():
18667c478bd9Sstevel@tonic-gate  *		internal failover function. Invokes vHCI drivers failover
18677c478bd9Sstevel@tonic-gate  *		callback function and process the failover status
18687c478bd9Sstevel@tonic-gate  * Return Values:
18697c478bd9Sstevel@tonic-gate  *		None
18707c478bd9Sstevel@tonic-gate  *
18717c478bd9Sstevel@tonic-gate  * Note: A client device in failover state can not be detached or freed.
18727c478bd9Sstevel@tonic-gate  */
18737c478bd9Sstevel@tonic-gate static int
18747c478bd9Sstevel@tonic-gate i_mdi_failover(void *arg)
18757c478bd9Sstevel@tonic-gate {
18767c478bd9Sstevel@tonic-gate 	int		rv = MDI_SUCCESS;
18777c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct = (mdi_client_t *)arg;
18787c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = ct->ct_vhci;
18797c478bd9Sstevel@tonic-gate 
18805e3986cbScth 	ASSERT(!MDI_CLIENT_LOCKED(ct));
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	if (vh->vh_ops->vo_failover != NULL) {
18837c478bd9Sstevel@tonic-gate 		/*
18847c478bd9Sstevel@tonic-gate 		 * Call vHCI drivers callback routine
18857c478bd9Sstevel@tonic-gate 		 */
18867c478bd9Sstevel@tonic-gate 		rv = (*vh->vh_ops->vo_failover)(vh->vh_dip, ct->ct_dip,
18877c478bd9Sstevel@tonic-gate 		    ct->ct_failover_flags);
18887c478bd9Sstevel@tonic-gate 	}
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
18917c478bd9Sstevel@tonic-gate 	MDI_CLIENT_CLEAR_FAILOVER_IN_PROGRESS(ct);
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	/*
18947c478bd9Sstevel@tonic-gate 	 * Save the failover return status
18957c478bd9Sstevel@tonic-gate 	 */
18967c478bd9Sstevel@tonic-gate 	ct->ct_failover_status = rv;
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 	/*
18997c478bd9Sstevel@tonic-gate 	 * As a result of failover, client status would have been changed.
19007c478bd9Sstevel@tonic-gate 	 * Update the client state and wake up anyone waiting on this client
19017c478bd9Sstevel@tonic-gate 	 * device.
19027c478bd9Sstevel@tonic-gate 	 */
19037c478bd9Sstevel@tonic-gate 	i_mdi_client_update_state(ct);
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	cv_broadcast(&ct->ct_failover_cv);
19067c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
19077c478bd9Sstevel@tonic-gate 	return (rv);
19087c478bd9Sstevel@tonic-gate }
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate /*
19117c478bd9Sstevel@tonic-gate  * Load balancing is logical block.
19127c478bd9Sstevel@tonic-gate  * IOs within the range described by region_size
19137c478bd9Sstevel@tonic-gate  * would go on the same path. This would improve the
19147c478bd9Sstevel@tonic-gate  * performance by cache-hit on some of the RAID devices.
19157c478bd9Sstevel@tonic-gate  * Search only for online paths(At some point we
19167c478bd9Sstevel@tonic-gate  * may want to balance across target ports).
19177c478bd9Sstevel@tonic-gate  * If no paths are found then default to round-robin.
19187c478bd9Sstevel@tonic-gate  */
19197c478bd9Sstevel@tonic-gate static int
19207c478bd9Sstevel@tonic-gate i_mdi_lba_lb(mdi_client_t *ct, mdi_pathinfo_t **ret_pip, struct buf *bp)
19217c478bd9Sstevel@tonic-gate {
19227c478bd9Sstevel@tonic-gate 	int		path_index = -1;
19237c478bd9Sstevel@tonic-gate 	int		online_path_count = 0;
19247c478bd9Sstevel@tonic-gate 	int		online_nonpref_path_count = 0;
19257c478bd9Sstevel@tonic-gate 	int 		region_size = ct->ct_lb_args->region_size;
19267c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
19277c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
19287c478bd9Sstevel@tonic-gate 	int		preferred, path_cnt;
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	pip = ct->ct_path_head;
19317c478bd9Sstevel@tonic-gate 	while (pip) {
19327c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
19337c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_state ==
19347c478bd9Sstevel@tonic-gate 		    MDI_PATHINFO_STATE_ONLINE && MDI_PI(pip)->pi_preferred) {
19357c478bd9Sstevel@tonic-gate 			online_path_count++;
19367c478bd9Sstevel@tonic-gate 		} else if (MDI_PI(pip)->pi_state ==
19377c478bd9Sstevel@tonic-gate 		    MDI_PATHINFO_STATE_ONLINE && !MDI_PI(pip)->pi_preferred) {
19387c478bd9Sstevel@tonic-gate 			online_nonpref_path_count++;
19397c478bd9Sstevel@tonic-gate 		}
19407c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)
19417c478bd9Sstevel@tonic-gate 		    MDI_PI(pip)->pi_client_link;
19427c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
19437c478bd9Sstevel@tonic-gate 		pip = next;
19447c478bd9Sstevel@tonic-gate 	}
19457c478bd9Sstevel@tonic-gate 	/* if found any online/preferred then use this type */
19467c478bd9Sstevel@tonic-gate 	if (online_path_count > 0) {
19477c478bd9Sstevel@tonic-gate 		path_cnt = online_path_count;
19487c478bd9Sstevel@tonic-gate 		preferred = 1;
19497c478bd9Sstevel@tonic-gate 	} else if (online_nonpref_path_count > 0) {
19507c478bd9Sstevel@tonic-gate 		path_cnt = online_nonpref_path_count;
19517c478bd9Sstevel@tonic-gate 		preferred = 0;
19527c478bd9Sstevel@tonic-gate 	} else {
19537c478bd9Sstevel@tonic-gate 		path_cnt = 0;
19547c478bd9Sstevel@tonic-gate 	}
19557c478bd9Sstevel@tonic-gate 	if (path_cnt) {
19567c478bd9Sstevel@tonic-gate 		path_index = (bp->b_blkno >> region_size) % path_cnt;
19577c478bd9Sstevel@tonic-gate 		pip = ct->ct_path_head;
19587c478bd9Sstevel@tonic-gate 		while (pip && path_index != -1) {
19597c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
19607c478bd9Sstevel@tonic-gate 			if (path_index == 0 &&
19617c478bd9Sstevel@tonic-gate 			    (MDI_PI(pip)->pi_state ==
19627c478bd9Sstevel@tonic-gate 			    MDI_PATHINFO_STATE_ONLINE) &&
19637c478bd9Sstevel@tonic-gate 				MDI_PI(pip)->pi_preferred == preferred) {
19647c478bd9Sstevel@tonic-gate 				MDI_PI_HOLD(pip);
19657c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
19667c478bd9Sstevel@tonic-gate 				*ret_pip = pip;
19677c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
19687c478bd9Sstevel@tonic-gate 			}
19697c478bd9Sstevel@tonic-gate 			path_index --;
19707c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)
19717c478bd9Sstevel@tonic-gate 			    MDI_PI(pip)->pi_client_link;
19727c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
19737c478bd9Sstevel@tonic-gate 			pip = next;
19747c478bd9Sstevel@tonic-gate 		}
19754c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
19764c06356bSdh142964 		    "lba %llx: path %s %p",
19774c06356bSdh142964 		    bp->b_lblkno, mdi_pi_spathname(pip), (void *)pip));
19787c478bd9Sstevel@tonic-gate 	}
19797c478bd9Sstevel@tonic-gate 	return (MDI_FAILURE);
19807c478bd9Sstevel@tonic-gate }
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate /*
19837c478bd9Sstevel@tonic-gate  * mdi_select_path():
19847c478bd9Sstevel@tonic-gate  *		select a path to access a client device.
19857c478bd9Sstevel@tonic-gate  *
19867c478bd9Sstevel@tonic-gate  *		mdi_select_path() function is called by the vHCI drivers to
19877c478bd9Sstevel@tonic-gate  *		select a path to route the I/O request to.  The caller passes
19887c478bd9Sstevel@tonic-gate  *		the block I/O data transfer structure ("buf") as one of the
19897c478bd9Sstevel@tonic-gate  *		parameters.  The mpxio framework uses the buf structure
19907c478bd9Sstevel@tonic-gate  *		contents to maintain per path statistics (total I/O size /
19917c478bd9Sstevel@tonic-gate  *		count pending).  If more than one online paths are available to
19927c478bd9Sstevel@tonic-gate  *		select, the framework automatically selects a suitable path
19937c478bd9Sstevel@tonic-gate  *		for routing I/O request. If a failover operation is active for
19947c478bd9Sstevel@tonic-gate  *		this client device the call shall be failed with MDI_BUSY error
19957c478bd9Sstevel@tonic-gate  *		code.
19967c478bd9Sstevel@tonic-gate  *
19977c478bd9Sstevel@tonic-gate  *		By default this function returns a suitable path in online
19987c478bd9Sstevel@tonic-gate  *		state based on the current load balancing policy.  Currently
19997c478bd9Sstevel@tonic-gate  *		we support LOAD_BALANCE_NONE (Previously selected online path
20007c478bd9Sstevel@tonic-gate  *		will continue to be used till the path is usable) and
20017c478bd9Sstevel@tonic-gate  *		LOAD_BALANCE_RR (Online paths will be selected in a round
20027c478bd9Sstevel@tonic-gate  *		robin fashion), LOAD_BALANCE_LB(Online paths will be selected
20037c478bd9Sstevel@tonic-gate  *		based on the logical block).  The load balancing
20047c478bd9Sstevel@tonic-gate  *		through vHCI drivers configuration file (driver.conf).
20057c478bd9Sstevel@tonic-gate  *
20067c478bd9Sstevel@tonic-gate  *		vHCI drivers may override this default behavior by specifying
2007602ca9eaScth  *		appropriate flags.  The meaning of the thrid argument depends
2008602ca9eaScth  *		on the flags specified. If MDI_SELECT_PATH_INSTANCE is set
2009602ca9eaScth  *		then the argument is the "path instance" of the path to select.
2010602ca9eaScth  *		If MDI_SELECT_PATH_INSTANCE is not set then the argument is
2011602ca9eaScth  *		"start_pip". A non NULL "start_pip" is the starting point to
2012602ca9eaScth  *		walk and find the next appropriate path.  The following values
2013602ca9eaScth  *		are currently defined: MDI_SELECT_ONLINE_PATH (to select an
2014602ca9eaScth  *		ONLINE path) and/or MDI_SELECT_STANDBY_PATH (to select an
2015602ca9eaScth  *		STANDBY path).
20167c478bd9Sstevel@tonic-gate  *
20177c478bd9Sstevel@tonic-gate  *		The non-standard behavior is used by the scsi_vhci driver,
20187c478bd9Sstevel@tonic-gate  *		whenever it has to use a STANDBY/FAULTED path.  Eg. during
20197c478bd9Sstevel@tonic-gate  *		attach of client devices (to avoid an unnecessary failover
20207c478bd9Sstevel@tonic-gate  *		when the STANDBY path comes up first), during failover
20217c478bd9Sstevel@tonic-gate  *		(to activate a STANDBY path as ONLINE).
20227c478bd9Sstevel@tonic-gate  *
20235e3986cbScth  *		The selected path is returned in a a mdi_hold_path() state
20245e3986cbScth  *		(pi_ref_cnt). Caller should release the hold by calling
20255e3986cbScth  *		mdi_rele_path().
20267c478bd9Sstevel@tonic-gate  *
20277c478bd9Sstevel@tonic-gate  * Return Values:
20287c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS	- Completed successfully
20297c478bd9Sstevel@tonic-gate  *		MDI_BUSY 	- Client device is busy failing over
20307c478bd9Sstevel@tonic-gate  *		MDI_NOPATH	- Client device is online, but no valid path are
20317c478bd9Sstevel@tonic-gate  *				  available to access this client device
20327c478bd9Sstevel@tonic-gate  *		MDI_FAILURE	- Invalid client device or state
20337c478bd9Sstevel@tonic-gate  *		MDI_DEVI_ONLINING
20347c478bd9Sstevel@tonic-gate  *				- Client device (struct dev_info state) is in
20357c478bd9Sstevel@tonic-gate  *				  onlining state.
20367c478bd9Sstevel@tonic-gate  */
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20397c478bd9Sstevel@tonic-gate int
20407c478bd9Sstevel@tonic-gate mdi_select_path(dev_info_t *cdip, struct buf *bp, int flags,
2041602ca9eaScth     void *arg, mdi_pathinfo_t **ret_pip)
20427c478bd9Sstevel@tonic-gate {
20437c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
20447c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
20457c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
20467c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*head;
20477c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*start;
20487c478bd9Sstevel@tonic-gate 	client_lb_t	lbp;	/* load balancing policy */
20497c478bd9Sstevel@tonic-gate 	int		sb = 1;	/* standard behavior */
20507c478bd9Sstevel@tonic-gate 	int		preferred = 1;	/* preferred path */
20517c478bd9Sstevel@tonic-gate 	int		cond, cont = 1;
20527c478bd9Sstevel@tonic-gate 	int		retry = 0;
2053602ca9eaScth 	mdi_pathinfo_t	*start_pip;	/* request starting pathinfo */
2054602ca9eaScth 	int		path_instance;	/* request specific path instance */
2055602ca9eaScth 
2056602ca9eaScth 	/* determine type of arg based on flags */
2057602ca9eaScth 	if (flags & MDI_SELECT_PATH_INSTANCE) {
2058602ca9eaScth 		path_instance = (int)(intptr_t)arg;
2059602ca9eaScth 		start_pip = NULL;
2060602ca9eaScth 	} else {
2061602ca9eaScth 		path_instance = 0;
2062602ca9eaScth 		start_pip = (mdi_pathinfo_t *)arg;
2063602ca9eaScth 	}
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	if (flags != 0) {
20667c478bd9Sstevel@tonic-gate 		/*
20677c478bd9Sstevel@tonic-gate 		 * disable default behavior
20687c478bd9Sstevel@tonic-gate 		 */
20697c478bd9Sstevel@tonic-gate 		sb = 0;
20707c478bd9Sstevel@tonic-gate 	}
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	*ret_pip = NULL;
20737c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
20747c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
20757c478bd9Sstevel@tonic-gate 		/* mdi extensions are NULL, Nothing more to do */
20767c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
20777c478bd9Sstevel@tonic-gate 	}
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	if (sb) {
20827c478bd9Sstevel@tonic-gate 		if (MDI_CLIENT_IS_FAILED(ct)) {
20837c478bd9Sstevel@tonic-gate 			/*
20847c478bd9Sstevel@tonic-gate 			 * Client is not ready to accept any I/O requests.
20857c478bd9Sstevel@tonic-gate 			 * Fail this request.
20867c478bd9Sstevel@tonic-gate 			 */
20874c06356bSdh142964 			MDI_DEBUG(2, (MDI_NOTE, cdip,
20884c06356bSdh142964 			    "client state offline ct = %p", (void *)ct));
20897c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
20907c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
20917c478bd9Sstevel@tonic-gate 		}
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 		if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
20947c478bd9Sstevel@tonic-gate 			/*
20957c478bd9Sstevel@tonic-gate 			 * Check for Failover is in progress. If so tell the
20967c478bd9Sstevel@tonic-gate 			 * caller that this device is busy.
20977c478bd9Sstevel@tonic-gate 			 */
20984c06356bSdh142964 			MDI_DEBUG(2, (MDI_NOTE, cdip,
20994c06356bSdh142964 			    "client failover in progress ct = %p",
21005e3986cbScth 			    (void *)ct));
21017c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
21027c478bd9Sstevel@tonic-gate 			return (MDI_BUSY);
21037c478bd9Sstevel@tonic-gate 		}
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 		/*
21067c478bd9Sstevel@tonic-gate 		 * Check to see whether the client device is attached.
21077c478bd9Sstevel@tonic-gate 		 * If not so, let the vHCI driver manually select a path
21087c478bd9Sstevel@tonic-gate 		 * (standby) and let the probe/attach process to continue.
21097c478bd9Sstevel@tonic-gate 		 */
2110737d277aScth 		if (MDI_CLIENT_IS_DETACHED(ct) || !i_ddi_devi_attached(cdip)) {
21114c06356bSdh142964 			MDI_DEBUG(4, (MDI_NOTE, cdip,
21124c06356bSdh142964 			    "devi is onlining ct = %p", (void *)ct));
21137c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
21147c478bd9Sstevel@tonic-gate 			return (MDI_DEVI_ONLINING);
21157c478bd9Sstevel@tonic-gate 		}
21167c478bd9Sstevel@tonic-gate 	}
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 	/*
21197c478bd9Sstevel@tonic-gate 	 * Cache in the client list head.  If head of the list is NULL
21207c478bd9Sstevel@tonic-gate 	 * return MDI_NOPATH
21217c478bd9Sstevel@tonic-gate 	 */
21227c478bd9Sstevel@tonic-gate 	head = ct->ct_path_head;
21237c478bd9Sstevel@tonic-gate 	if (head == NULL) {
21247c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
21257c478bd9Sstevel@tonic-gate 		return (MDI_NOPATH);
21267c478bd9Sstevel@tonic-gate 	}
21277c478bd9Sstevel@tonic-gate 
2128602ca9eaScth 	/* Caller is specifying a specific pathinfo path by path_instance */
2129602ca9eaScth 	if (path_instance) {
2130602ca9eaScth 		/* search for pathinfo with correct path_instance */
2131602ca9eaScth 		for (pip = head;
2132602ca9eaScth 		    pip && (mdi_pi_get_path_instance(pip) != path_instance);
2133602ca9eaScth 		    pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link)
2134602ca9eaScth 			;
2135602ca9eaScth 
2136ab412e72SSrikanth, Ramana 		/* If path can't be selected then MDI_NOPATH is returned. */
2137602ca9eaScth 		if (pip == NULL) {
2138602ca9eaScth 			MDI_CLIENT_UNLOCK(ct);
2139ab412e72SSrikanth, Ramana 			return (MDI_NOPATH);
2140602ca9eaScth 		}
2141602ca9eaScth 
21424c06356bSdh142964 		/*
21434c06356bSdh142964 		 * Verify state of path. When asked to select a specific
21444c06356bSdh142964 		 * path_instance, we select the requested path in any
21454c06356bSdh142964 		 * state (ONLINE, OFFLINE, STANDBY, FAULT) other than INIT.
21464c06356bSdh142964 		 * We don't however select paths where the pHCI has detached.
21474c06356bSdh142964 		 * NOTE: last pathinfo node of an opened client device may
21484c06356bSdh142964 		 * exist in an OFFLINE state after the pHCI associated with
21494c06356bSdh142964 		 * that path has detached (but pi_phci will be NULL if that
21504c06356bSdh142964 		 * has occurred).
21514c06356bSdh142964 		 */
2152602ca9eaScth 		MDI_PI_LOCK(pip);
21534c06356bSdh142964 		if ((MDI_PI(pip)->pi_state == MDI_PATHINFO_STATE_INIT) ||
21544c06356bSdh142964 		    (MDI_PI(pip)->pi_phci == NULL)) {
2155602ca9eaScth 			MDI_PI_UNLOCK(pip);
2156602ca9eaScth 			MDI_CLIENT_UNLOCK(ct);
2157602ca9eaScth 			return (MDI_FAILURE);
2158602ca9eaScth 		}
2159602ca9eaScth 
2160ab412e72SSrikanth, Ramana 		/* Return MDI_BUSY if we have a transient condition */
2161ab412e72SSrikanth, Ramana 		if (MDI_PI_IS_TRANSIENT(pip)) {
2162ab412e72SSrikanth, Ramana 			MDI_PI_UNLOCK(pip);
2163ab412e72SSrikanth, Ramana 			MDI_CLIENT_UNLOCK(ct);
2164ab412e72SSrikanth, Ramana 			return (MDI_BUSY);
2165ab412e72SSrikanth, Ramana 		}
2166ab412e72SSrikanth, Ramana 
2167602ca9eaScth 		/*
2168602ca9eaScth 		 * Return the path in hold state. Caller should release the
2169602ca9eaScth 		 * lock by calling mdi_rele_path()
2170602ca9eaScth 		 */
2171602ca9eaScth 		MDI_PI_HOLD(pip);
2172602ca9eaScth 		MDI_PI_UNLOCK(pip);
2173602ca9eaScth 		*ret_pip = pip;
2174602ca9eaScth 		MDI_CLIENT_UNLOCK(ct);
2175602ca9eaScth 		return (MDI_SUCCESS);
2176602ca9eaScth 	}
2177602ca9eaScth 
21787c478bd9Sstevel@tonic-gate 	/*
21797c478bd9Sstevel@tonic-gate 	 * for non default behavior, bypass current
21807c478bd9Sstevel@tonic-gate 	 * load balancing policy and always use LOAD_BALANCE_RR
21817c478bd9Sstevel@tonic-gate 	 * except that the start point will be adjusted based
21827c478bd9Sstevel@tonic-gate 	 * on the provided start_pip
21837c478bd9Sstevel@tonic-gate 	 */
21847c478bd9Sstevel@tonic-gate 	lbp = sb ? ct->ct_lb : LOAD_BALANCE_RR;
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 	switch (lbp) {
21877c478bd9Sstevel@tonic-gate 	case LOAD_BALANCE_NONE:
21887c478bd9Sstevel@tonic-gate 		/*
21897c478bd9Sstevel@tonic-gate 		 * Load balancing is None  or Alternate path mode
21907c478bd9Sstevel@tonic-gate 		 * Start looking for a online mdi_pathinfo node starting from
21917c478bd9Sstevel@tonic-gate 		 * last known selected path
21927c478bd9Sstevel@tonic-gate 		 */
21937c478bd9Sstevel@tonic-gate 		preferred = 1;
21947c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)ct->ct_path_last;
21957c478bd9Sstevel@tonic-gate 		if (pip == NULL) {
21967c478bd9Sstevel@tonic-gate 			pip = head;
21977c478bd9Sstevel@tonic-gate 		}
21987c478bd9Sstevel@tonic-gate 		start = pip;
21997c478bd9Sstevel@tonic-gate 		do {
22007c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
22017c478bd9Sstevel@tonic-gate 			/*
22027c478bd9Sstevel@tonic-gate 			 * No need to explicitly check if the path is disabled.
22037c478bd9Sstevel@tonic-gate 			 * Since we are checking for state == ONLINE and the
220455e592a2SRandall Ralphs 			 * same variable is used for DISABLE/ENABLE information.
22057c478bd9Sstevel@tonic-gate 			 */
2206ee28b439Scm136836 			if ((MDI_PI(pip)->pi_state  ==
2207ee28b439Scm136836 				MDI_PATHINFO_STATE_ONLINE) &&
22087c478bd9Sstevel@tonic-gate 				preferred == MDI_PI(pip)->pi_preferred) {
22097c478bd9Sstevel@tonic-gate 				/*
22107c478bd9Sstevel@tonic-gate 				 * Return the path in hold state. Caller should
22117c478bd9Sstevel@tonic-gate 				 * release the lock by calling mdi_rele_path()
22127c478bd9Sstevel@tonic-gate 				 */
22137c478bd9Sstevel@tonic-gate 				MDI_PI_HOLD(pip);
22147c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
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 			/*
22227c478bd9Sstevel@tonic-gate 			 * Path is busy.
22237c478bd9Sstevel@tonic-gate 			 */
22247c478bd9Sstevel@tonic-gate 			if (MDI_PI_IS_DRV_DISABLE_TRANSIENT(pip) ||
22257c478bd9Sstevel@tonic-gate 			    MDI_PI_IS_TRANSIENT(pip))
22267c478bd9Sstevel@tonic-gate 				retry = 1;
22277c478bd9Sstevel@tonic-gate 			/*
22287c478bd9Sstevel@tonic-gate 			 * Keep looking for a next available online path
22297c478bd9Sstevel@tonic-gate 			 */
22307c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
22317c478bd9Sstevel@tonic-gate 			if (next == NULL) {
22327c478bd9Sstevel@tonic-gate 				next = head;
22337c478bd9Sstevel@tonic-gate 			}
22347c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
22357c478bd9Sstevel@tonic-gate 			pip = next;
22367c478bd9Sstevel@tonic-gate 			if (start == pip && preferred) {
22377c478bd9Sstevel@tonic-gate 				preferred = 0;
22387c478bd9Sstevel@tonic-gate 			} else if (start == pip && !preferred) {
22397c478bd9Sstevel@tonic-gate 				cont = 0;
22407c478bd9Sstevel@tonic-gate 			}
22417c478bd9Sstevel@tonic-gate 		} while (cont);
22427c478bd9Sstevel@tonic-gate 		break;
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 	case LOAD_BALANCE_LBA:
22457c478bd9Sstevel@tonic-gate 		/*
22467c478bd9Sstevel@tonic-gate 		 * Make sure we are looking
22477c478bd9Sstevel@tonic-gate 		 * for an online path. Otherwise, if it is for a STANDBY
22487c478bd9Sstevel@tonic-gate 		 * path request, it will go through and fetch an ONLINE
22497c478bd9Sstevel@tonic-gate 		 * path which is not desirable.
22507c478bd9Sstevel@tonic-gate 		 */
22517c478bd9Sstevel@tonic-gate 		if ((ct->ct_lb_args != NULL) &&
22527c478bd9Sstevel@tonic-gate 			    (ct->ct_lb_args->region_size) && bp &&
22537c478bd9Sstevel@tonic-gate 				(sb || (flags == MDI_SELECT_ONLINE_PATH))) {
22547c478bd9Sstevel@tonic-gate 			if (i_mdi_lba_lb(ct, ret_pip, bp)
22557c478bd9Sstevel@tonic-gate 				    == MDI_SUCCESS) {
22567c478bd9Sstevel@tonic-gate 				MDI_CLIENT_UNLOCK(ct);
22577c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
22587c478bd9Sstevel@tonic-gate 			}
22597c478bd9Sstevel@tonic-gate 		}
22607c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
22617c478bd9Sstevel@tonic-gate 	case LOAD_BALANCE_RR:
22627c478bd9Sstevel@tonic-gate 		/*
22637c478bd9Sstevel@tonic-gate 		 * Load balancing is Round Robin. Start looking for a online
22647c478bd9Sstevel@tonic-gate 		 * mdi_pathinfo node starting from last known selected path
22657c478bd9Sstevel@tonic-gate 		 * as the start point.  If override flags are specified,
22667c478bd9Sstevel@tonic-gate 		 * process accordingly.
22677c478bd9Sstevel@tonic-gate 		 * If the search is already in effect(start_pip not null),
22687c478bd9Sstevel@tonic-gate 		 * then lets just use the same path preference to continue the
22697c478bd9Sstevel@tonic-gate 		 * traversal.
22707c478bd9Sstevel@tonic-gate 		 */
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 		if (start_pip != NULL) {
22737c478bd9Sstevel@tonic-gate 			preferred = MDI_PI(start_pip)->pi_preferred;
22747c478bd9Sstevel@tonic-gate 		} else {
22757c478bd9Sstevel@tonic-gate 			preferred = 1;
22767c478bd9Sstevel@tonic-gate 		}
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 		start = sb ? (mdi_pathinfo_t *)ct->ct_path_last : start_pip;
22797c478bd9Sstevel@tonic-gate 		if (start == NULL) {
22807c478bd9Sstevel@tonic-gate 			pip = head;
22817c478bd9Sstevel@tonic-gate 		} else {
22827c478bd9Sstevel@tonic-gate 			pip = (mdi_pathinfo_t *)MDI_PI(start)->pi_client_link;
22837c478bd9Sstevel@tonic-gate 			if (pip == NULL) {
2284b08fdaf7SSheshadri Vasudevan 				if ( flags & MDI_SELECT_NO_PREFERRED) {
2285b08fdaf7SSheshadri Vasudevan 					/*
2286b08fdaf7SSheshadri Vasudevan 					 * Return since we hit the end of list
2287b08fdaf7SSheshadri Vasudevan 					 */
2288b08fdaf7SSheshadri Vasudevan 					MDI_CLIENT_UNLOCK(ct);
2289b08fdaf7SSheshadri Vasudevan 					return (MDI_NOPATH);
2290b08fdaf7SSheshadri Vasudevan 				}
2291b08fdaf7SSheshadri Vasudevan 
22927c478bd9Sstevel@tonic-gate 				if (!sb) {
22937c478bd9Sstevel@tonic-gate 					if (preferred == 0) {
22947c478bd9Sstevel@tonic-gate 						/*
22957c478bd9Sstevel@tonic-gate 						 * Looks like we have completed
22967c478bd9Sstevel@tonic-gate 						 * the traversal as preferred
22977c478bd9Sstevel@tonic-gate 						 * value is 0. Time to bail out.
22987c478bd9Sstevel@tonic-gate 						 */
22997c478bd9Sstevel@tonic-gate 						*ret_pip = NULL;
23007c478bd9Sstevel@tonic-gate 						MDI_CLIENT_UNLOCK(ct);
23017c478bd9Sstevel@tonic-gate 						return (MDI_NOPATH);
23027c478bd9Sstevel@tonic-gate 					} else {
23037c478bd9Sstevel@tonic-gate 						/*
23047c478bd9Sstevel@tonic-gate 						 * Looks like we reached the
23057c478bd9Sstevel@tonic-gate 						 * end of the list. Lets enable
23067c478bd9Sstevel@tonic-gate 						 * traversal of non preferred
23077c478bd9Sstevel@tonic-gate 						 * paths.
23087c478bd9Sstevel@tonic-gate 						 */
23097c478bd9Sstevel@tonic-gate 						preferred = 0;
23107c478bd9Sstevel@tonic-gate 					}
23117c478bd9Sstevel@tonic-gate 				}
23127c478bd9Sstevel@tonic-gate 				pip = head;
23137c478bd9Sstevel@tonic-gate 			}
23147c478bd9Sstevel@tonic-gate 		}
23157c478bd9Sstevel@tonic-gate 		start = pip;
23167c478bd9Sstevel@tonic-gate 		do {
23177c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
23187c478bd9Sstevel@tonic-gate 			if (sb) {
23197c478bd9Sstevel@tonic-gate 				cond = ((MDI_PI(pip)->pi_state ==
23207c478bd9Sstevel@tonic-gate 				    MDI_PATHINFO_STATE_ONLINE &&
23217c478bd9Sstevel@tonic-gate 					MDI_PI(pip)->pi_preferred ==
23227c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
23237c478bd9Sstevel@tonic-gate 			} else {
23247c478bd9Sstevel@tonic-gate 				if (flags == MDI_SELECT_ONLINE_PATH) {
23257c478bd9Sstevel@tonic-gate 					cond = ((MDI_PI(pip)->pi_state ==
23267c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_ONLINE &&
23277c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_preferred ==
23287c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
23297c478bd9Sstevel@tonic-gate 				} else if (flags == MDI_SELECT_STANDBY_PATH) {
23307c478bd9Sstevel@tonic-gate 					cond = ((MDI_PI(pip)->pi_state ==
23317c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_STANDBY &&
23327c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_preferred ==
23337c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
23347c478bd9Sstevel@tonic-gate 				} else if (flags == (MDI_SELECT_ONLINE_PATH |
23357c478bd9Sstevel@tonic-gate 				    MDI_SELECT_STANDBY_PATH)) {
23367c478bd9Sstevel@tonic-gate 					cond = (((MDI_PI(pip)->pi_state ==
23377c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_ONLINE ||
23387c478bd9Sstevel@tonic-gate 					    (MDI_PI(pip)->pi_state ==
23397c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_STANDBY)) &&
23407c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_preferred ==
23417c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
2342ee28b439Scm136836 				} else if (flags ==
2343ee28b439Scm136836 					(MDI_SELECT_STANDBY_PATH |
2344ee28b439Scm136836 					MDI_SELECT_ONLINE_PATH |
2345ee28b439Scm136836 					MDI_SELECT_USER_DISABLE_PATH)) {
2346ee28b439Scm136836 					cond = (((MDI_PI(pip)->pi_state ==
2347ee28b439Scm136836 					    MDI_PATHINFO_STATE_ONLINE ||
2348ee28b439Scm136836 					    (MDI_PI(pip)->pi_state ==
2349ee28b439Scm136836 					    MDI_PATHINFO_STATE_STANDBY) ||
2350ee28b439Scm136836 						(MDI_PI(pip)->pi_state ==
2351ee28b439Scm136836 					    (MDI_PATHINFO_STATE_ONLINE|
2352ee28b439Scm136836 					    MDI_PATHINFO_STATE_USER_DISABLE)) ||
2353ee28b439Scm136836 						(MDI_PI(pip)->pi_state ==
2354ee28b439Scm136836 					    (MDI_PATHINFO_STATE_STANDBY |
2355ee28b439Scm136836 					    MDI_PATHINFO_STATE_USER_DISABLE)))&&
2356ee28b439Scm136836 						MDI_PI(pip)->pi_preferred ==
2357ee28b439Scm136836 						preferred) ? 1 : 0);
2358b08fdaf7SSheshadri Vasudevan 				} else if (flags ==
2359b08fdaf7SSheshadri Vasudevan 				    (MDI_SELECT_STANDBY_PATH |
2360b08fdaf7SSheshadri Vasudevan 				    MDI_SELECT_ONLINE_PATH |
2361b08fdaf7SSheshadri Vasudevan 				    MDI_SELECT_NO_PREFERRED)) {
2362b08fdaf7SSheshadri Vasudevan 					cond = (((MDI_PI(pip)->pi_state ==
2363b08fdaf7SSheshadri Vasudevan 					    MDI_PATHINFO_STATE_ONLINE) ||
2364b08fdaf7SSheshadri Vasudevan 					    (MDI_PI(pip)->pi_state ==
2365b08fdaf7SSheshadri Vasudevan 					    MDI_PATHINFO_STATE_STANDBY))
2366b08fdaf7SSheshadri Vasudevan 					    ? 1 : 0);
23677c478bd9Sstevel@tonic-gate 				} else {
23687c478bd9Sstevel@tonic-gate 					cond = 0;
23697c478bd9Sstevel@tonic-gate 				}
23707c478bd9Sstevel@tonic-gate 			}
23717c478bd9Sstevel@tonic-gate 			/*
23727c478bd9Sstevel@tonic-gate 			 * No need to explicitly check if the path is disabled.
23737c478bd9Sstevel@tonic-gate 			 * Since we are checking for state == ONLINE and the
237455e592a2SRandall Ralphs 			 * same variable is used for DISABLE/ENABLE information.
23757c478bd9Sstevel@tonic-gate 			 */
23767c478bd9Sstevel@tonic-gate 			if (cond) {
23777c478bd9Sstevel@tonic-gate 				/*
23787c478bd9Sstevel@tonic-gate 				 * Return the path in hold state. Caller should
23797c478bd9Sstevel@tonic-gate 				 * release the lock by calling mdi_rele_path()
23807c478bd9Sstevel@tonic-gate 				 */
23817c478bd9Sstevel@tonic-gate 				MDI_PI_HOLD(pip);
23827c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
23837c478bd9Sstevel@tonic-gate 				if (sb)
23847c478bd9Sstevel@tonic-gate 					ct->ct_path_last = pip;
23857c478bd9Sstevel@tonic-gate 				*ret_pip = pip;
23867c478bd9Sstevel@tonic-gate 				MDI_CLIENT_UNLOCK(ct);
23877c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
23887c478bd9Sstevel@tonic-gate 			}
23897c478bd9Sstevel@tonic-gate 			/*
23907c478bd9Sstevel@tonic-gate 			 * Path is busy.
23917c478bd9Sstevel@tonic-gate 			 */
23927c478bd9Sstevel@tonic-gate 			if (MDI_PI_IS_DRV_DISABLE_TRANSIENT(pip) ||
23937c478bd9Sstevel@tonic-gate 			    MDI_PI_IS_TRANSIENT(pip))
23947c478bd9Sstevel@tonic-gate 				retry = 1;
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 			/*
23977c478bd9Sstevel@tonic-gate 			 * Keep looking for a next available online path
23987c478bd9Sstevel@tonic-gate 			 */
23997c478bd9Sstevel@tonic-gate do_again:
24007c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
24017c478bd9Sstevel@tonic-gate 			if (next == NULL) {
2402b08fdaf7SSheshadri Vasudevan 				if ( flags & MDI_SELECT_NO_PREFERRED) {
2403b08fdaf7SSheshadri Vasudevan 					/*
2404b08fdaf7SSheshadri Vasudevan 					 * Bail out since we hit the end of list
2405b08fdaf7SSheshadri Vasudevan 					 */
2406b08fdaf7SSheshadri Vasudevan 					MDI_PI_UNLOCK(pip);
2407b08fdaf7SSheshadri Vasudevan 					break;
2408b08fdaf7SSheshadri Vasudevan 				}
2409b08fdaf7SSheshadri Vasudevan 
24107c478bd9Sstevel@tonic-gate 				if (!sb) {
24117c478bd9Sstevel@tonic-gate 					if (preferred == 1) {
24127c478bd9Sstevel@tonic-gate 						/*
24137c478bd9Sstevel@tonic-gate 						 * Looks like we reached the
24147c478bd9Sstevel@tonic-gate 						 * end of the list. Lets enable
24157c478bd9Sstevel@tonic-gate 						 * traversal of non preferred
24167c478bd9Sstevel@tonic-gate 						 * paths.
24177c478bd9Sstevel@tonic-gate 						 */
24187c478bd9Sstevel@tonic-gate 						preferred = 0;
24197c478bd9Sstevel@tonic-gate 						next = head;
24207c478bd9Sstevel@tonic-gate 					} else {
24217c478bd9Sstevel@tonic-gate 						/*
24227c478bd9Sstevel@tonic-gate 						 * We have done both the passes
24237c478bd9Sstevel@tonic-gate 						 * Preferred as well as for
24247c478bd9Sstevel@tonic-gate 						 * Non-preferred. Bail out now.
24257c478bd9Sstevel@tonic-gate 						 */
24267c478bd9Sstevel@tonic-gate 						cont = 0;
24277c478bd9Sstevel@tonic-gate 					}
24287c478bd9Sstevel@tonic-gate 				} else {
24297c478bd9Sstevel@tonic-gate 					/*
24307c478bd9Sstevel@tonic-gate 					 * Standard behavior case.
24317c478bd9Sstevel@tonic-gate 					 */
24327c478bd9Sstevel@tonic-gate 					next = head;
24337c478bd9Sstevel@tonic-gate 				}
24347c478bd9Sstevel@tonic-gate 			}
24357c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
24367c478bd9Sstevel@tonic-gate 			if (cont == 0) {
24377c478bd9Sstevel@tonic-gate 				break;
24387c478bd9Sstevel@tonic-gate 			}
24397c478bd9Sstevel@tonic-gate 			pip = next;
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 			if (!sb) {
24427c478bd9Sstevel@tonic-gate 				/*
24437c478bd9Sstevel@tonic-gate 				 * We need to handle the selection of
24447c478bd9Sstevel@tonic-gate 				 * non-preferred path in the following
24457c478bd9Sstevel@tonic-gate 				 * case:
24467c478bd9Sstevel@tonic-gate 				 *
24477c478bd9Sstevel@tonic-gate 				 * +------+   +------+   +------+   +-----+
24487c478bd9Sstevel@tonic-gate 				 * | A : 1| - | B : 1| - | C : 0| - |NULL |
24497c478bd9Sstevel@tonic-gate 				 * +------+   +------+   +------+   +-----+
24507c478bd9Sstevel@tonic-gate 				 *
24517c478bd9Sstevel@tonic-gate 				 * If we start the search with B, we need to
24527c478bd9Sstevel@tonic-gate 				 * skip beyond B to pick C which is non -
24537c478bd9Sstevel@tonic-gate 				 * preferred in the second pass. The following
24547c478bd9Sstevel@tonic-gate 				 * test, if true, will allow us to skip over
24557c478bd9Sstevel@tonic-gate 				 * the 'start'(B in the example) to select
24567c478bd9Sstevel@tonic-gate 				 * other non preferred elements.
24577c478bd9Sstevel@tonic-gate 				 */
24587c478bd9Sstevel@tonic-gate 				if ((start_pip != NULL) && (start_pip == pip) &&
24597c478bd9Sstevel@tonic-gate 				    (MDI_PI(start_pip)->pi_preferred
24607c478bd9Sstevel@tonic-gate 				    != preferred)) {
24617c478bd9Sstevel@tonic-gate 					/*
24627c478bd9Sstevel@tonic-gate 					 * try again after going past the start
24637c478bd9Sstevel@tonic-gate 					 * pip
24647c478bd9Sstevel@tonic-gate 					 */
24657c478bd9Sstevel@tonic-gate 					MDI_PI_LOCK(pip);
24667c478bd9Sstevel@tonic-gate 					goto do_again;
24677c478bd9Sstevel@tonic-gate 				}
24687c478bd9Sstevel@tonic-gate 			} else {
24697c478bd9Sstevel@tonic-gate 				/*
24707c478bd9Sstevel@tonic-gate 				 * Standard behavior case
24717c478bd9Sstevel@tonic-gate 				 */
24727c478bd9Sstevel@tonic-gate 				if (start == pip && preferred) {
24737c478bd9Sstevel@tonic-gate 					/* look for nonpreferred paths */
24747c478bd9Sstevel@tonic-gate 					preferred = 0;
24757c478bd9Sstevel@tonic-gate 				} else if (start == pip && !preferred) {
24767c478bd9Sstevel@tonic-gate 					/*
24777c478bd9Sstevel@tonic-gate 					 * Exit condition
24787c478bd9Sstevel@tonic-gate 					 */
24797c478bd9Sstevel@tonic-gate 					cont = 0;
24807c478bd9Sstevel@tonic-gate 				}
24817c478bd9Sstevel@tonic-gate 			}
24827c478bd9Sstevel@tonic-gate 		} while (cont);
24837c478bd9Sstevel@tonic-gate 		break;
24847c478bd9Sstevel@tonic-gate 	}
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
24877c478bd9Sstevel@tonic-gate 	if (retry == 1) {
24887c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
24897c478bd9Sstevel@tonic-gate 	} else {
24907c478bd9Sstevel@tonic-gate 		return (MDI_NOPATH);
24917c478bd9Sstevel@tonic-gate 	}
24927c478bd9Sstevel@tonic-gate }
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate /*
24957c478bd9Sstevel@tonic-gate  * For a client, return the next available path to any phci
24967c478bd9Sstevel@tonic-gate  *
24977c478bd9Sstevel@tonic-gate  * Note:
24987c478bd9Sstevel@tonic-gate  *		Caller should hold the branch's devinfo node to get a consistent
24997c478bd9Sstevel@tonic-gate  *		snap shot of the mdi_pathinfo nodes.
25007c478bd9Sstevel@tonic-gate  *
25017c478bd9Sstevel@tonic-gate  *		Please note that even the list is stable the mdi_pathinfo
25027c478bd9Sstevel@tonic-gate  *		node state and properties are volatile.  The caller should lock
25037c478bd9Sstevel@tonic-gate  *		and unlock the nodes by calling mdi_pi_lock() and
25047c478bd9Sstevel@tonic-gate  *		mdi_pi_unlock() functions to get a stable properties.
25057c478bd9Sstevel@tonic-gate  *
25067c478bd9Sstevel@tonic-gate  *		If there is a need to use the nodes beyond the hold of the
25077c478bd9Sstevel@tonic-gate  *		devinfo node period (For ex. I/O), then mdi_pathinfo node
25087c478bd9Sstevel@tonic-gate  *		need to be held against unexpected removal by calling
25097c478bd9Sstevel@tonic-gate  *		mdi_hold_path() and should be released by calling
25107c478bd9Sstevel@tonic-gate  *		mdi_rele_path() on completion.
25117c478bd9Sstevel@tonic-gate  */
25127c478bd9Sstevel@tonic-gate mdi_pathinfo_t *
25137c478bd9Sstevel@tonic-gate mdi_get_next_phci_path(dev_info_t *ct_dip, mdi_pathinfo_t *pip)
25147c478bd9Sstevel@tonic-gate {
25157c478bd9Sstevel@tonic-gate 	mdi_client_t *ct;
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	if (!MDI_CLIENT(ct_dip))
25187c478bd9Sstevel@tonic-gate 		return (NULL);
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	/*
25217c478bd9Sstevel@tonic-gate 	 * Walk through client link
25227c478bd9Sstevel@tonic-gate 	 */
25237c478bd9Sstevel@tonic-gate 	ct = (mdi_client_t *)DEVI(ct_dip)->devi_mdi_client;
25247c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 	if (pip == NULL)
25277c478bd9Sstevel@tonic-gate 		return ((mdi_pathinfo_t *)ct->ct_path_head);
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate 	return ((mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link);
25307c478bd9Sstevel@tonic-gate }
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate /*
25337c478bd9Sstevel@tonic-gate  * For a phci, return the next available path to any client
25347c478bd9Sstevel@tonic-gate  * Note: ditto mdi_get_next_phci_path()
25357c478bd9Sstevel@tonic-gate  */
25367c478bd9Sstevel@tonic-gate mdi_pathinfo_t *
25377c478bd9Sstevel@tonic-gate mdi_get_next_client_path(dev_info_t *ph_dip, mdi_pathinfo_t *pip)
25387c478bd9Sstevel@tonic-gate {
25397c478bd9Sstevel@tonic-gate 	mdi_phci_t *ph;
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 	if (!MDI_PHCI(ph_dip))
25427c478bd9Sstevel@tonic-gate 		return (NULL);
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 	/*
25457c478bd9Sstevel@tonic-gate 	 * Walk through pHCI link
25467c478bd9Sstevel@tonic-gate 	 */
25477c478bd9Sstevel@tonic-gate 	ph = (mdi_phci_t *)DEVI(ph_dip)->devi_mdi_xhci;
25487c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate 	if (pip == NULL)
25517c478bd9Sstevel@tonic-gate 		return ((mdi_pathinfo_t *)ph->ph_path_head);
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	return ((mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link);
25547c478bd9Sstevel@tonic-gate }
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate /*
25577c478bd9Sstevel@tonic-gate  * mdi_hold_path():
25587c478bd9Sstevel@tonic-gate  *		Hold the mdi_pathinfo node against unwanted unexpected free.
25597c478bd9Sstevel@tonic-gate  * Return Values:
25607c478bd9Sstevel@tonic-gate  *		None
25617c478bd9Sstevel@tonic-gate  */
25627c478bd9Sstevel@tonic-gate void
25637c478bd9Sstevel@tonic-gate mdi_hold_path(mdi_pathinfo_t *pip)
25647c478bd9Sstevel@tonic-gate {
25657c478bd9Sstevel@tonic-gate 	if (pip) {
25667c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
25677c478bd9Sstevel@tonic-gate 		MDI_PI_HOLD(pip);
25687c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
25697c478bd9Sstevel@tonic-gate 	}
25707c478bd9Sstevel@tonic-gate }
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate /*
25747c478bd9Sstevel@tonic-gate  * mdi_rele_path():
25757c478bd9Sstevel@tonic-gate  *		Release the mdi_pathinfo node which was selected
25767c478bd9Sstevel@tonic-gate  *		through mdi_select_path() mechanism or manually held by
25777c478bd9Sstevel@tonic-gate  *		calling mdi_hold_path().
25787c478bd9Sstevel@tonic-gate  * Return Values:
25797c478bd9Sstevel@tonic-gate  *		None
25807c478bd9Sstevel@tonic-gate  */
25817c478bd9Sstevel@tonic-gate void
25827c478bd9Sstevel@tonic-gate mdi_rele_path(mdi_pathinfo_t *pip)
25837c478bd9Sstevel@tonic-gate {
25847c478bd9Sstevel@tonic-gate 	if (pip) {
25857c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
25867c478bd9Sstevel@tonic-gate 		MDI_PI_RELE(pip);
25877c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_ref_cnt == 0) {
25887c478bd9Sstevel@tonic-gate 			cv_broadcast(&MDI_PI(pip)->pi_ref_cv);
25897c478bd9Sstevel@tonic-gate 		}
25907c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
25917c478bd9Sstevel@tonic-gate 	}
25927c478bd9Sstevel@tonic-gate }
25937c478bd9Sstevel@tonic-gate 
25947c478bd9Sstevel@tonic-gate /*
25957c478bd9Sstevel@tonic-gate  * mdi_pi_lock():
25967c478bd9Sstevel@tonic-gate  * 		Lock the mdi_pathinfo node.
25977c478bd9Sstevel@tonic-gate  * Note:
25987c478bd9Sstevel@tonic-gate  *		The caller should release the lock by calling mdi_pi_unlock()
25997c478bd9Sstevel@tonic-gate  */
26007c478bd9Sstevel@tonic-gate void
26017c478bd9Sstevel@tonic-gate mdi_pi_lock(mdi_pathinfo_t *pip)
26027c478bd9Sstevel@tonic-gate {
26037c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
26047c478bd9Sstevel@tonic-gate 	if (pip) {
26057c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
26067c478bd9Sstevel@tonic-gate 	}
26077c478bd9Sstevel@tonic-gate }
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate /*
26117c478bd9Sstevel@tonic-gate  * mdi_pi_unlock():
26127c478bd9Sstevel@tonic-gate  * 		Unlock the mdi_pathinfo node.
26137c478bd9Sstevel@tonic-gate  * Note:
26147c478bd9Sstevel@tonic-gate  *		The mdi_pathinfo node should have been locked with mdi_pi_lock()
26157c478bd9Sstevel@tonic-gate  */
26167c478bd9Sstevel@tonic-gate void
26177c478bd9Sstevel@tonic-gate mdi_pi_unlock(mdi_pathinfo_t *pip)
26187c478bd9Sstevel@tonic-gate {
26197c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
26207c478bd9Sstevel@tonic-gate 	if (pip) {
26217c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
26227c478bd9Sstevel@tonic-gate 	}
26237c478bd9Sstevel@tonic-gate }
26247c478bd9Sstevel@tonic-gate 
26257c478bd9Sstevel@tonic-gate /*
26267c478bd9Sstevel@tonic-gate  * mdi_pi_find():
26277c478bd9Sstevel@tonic-gate  *		Search the list of mdi_pathinfo nodes attached to the
26287c478bd9Sstevel@tonic-gate  *		pHCI/Client device node whose path address matches "paddr".
26297c478bd9Sstevel@tonic-gate  *		Returns a pointer to the mdi_pathinfo node if a matching node is
26307c478bd9Sstevel@tonic-gate  *		found.
26317c478bd9Sstevel@tonic-gate  * Return Values:
26327c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node handle
26337c478bd9Sstevel@tonic-gate  *		NULL
26347c478bd9Sstevel@tonic-gate  * Notes:
26357c478bd9Sstevel@tonic-gate  *		Caller need not hold any locks to call this function.
26367c478bd9Sstevel@tonic-gate  */
26377c478bd9Sstevel@tonic-gate mdi_pathinfo_t *
26387c478bd9Sstevel@tonic-gate mdi_pi_find(dev_info_t *pdip, char *caddr, char *paddr)
26397c478bd9Sstevel@tonic-gate {
26407c478bd9Sstevel@tonic-gate 	mdi_phci_t		*ph;
26417c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh;
26427c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
26437c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t		*pip = NULL;
26447c478bd9Sstevel@tonic-gate 
26454c06356bSdh142964 	MDI_DEBUG(2, (MDI_NOTE, pdip,
26464c06356bSdh142964 	    "caddr@%s paddr@%s", caddr ? caddr : "", paddr ? paddr : ""));
26477c478bd9Sstevel@tonic-gate 	if ((pdip == NULL) || (paddr == NULL)) {
26487c478bd9Sstevel@tonic-gate 		return (NULL);
26497c478bd9Sstevel@tonic-gate 	}
26507c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
26517c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
26527c478bd9Sstevel@tonic-gate 		/*
26537c478bd9Sstevel@tonic-gate 		 * Invalid pHCI device, Nothing more to do.
26547c478bd9Sstevel@tonic-gate 		 */
26554c06356bSdh142964 		MDI_DEBUG(2, (MDI_WARN, pdip, "invalid phci"));
26567c478bd9Sstevel@tonic-gate 		return (NULL);
26577c478bd9Sstevel@tonic-gate 	}
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
26607c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
26617c478bd9Sstevel@tonic-gate 		/*
26627c478bd9Sstevel@tonic-gate 		 * Invalid vHCI device, Nothing more to do.
26637c478bd9Sstevel@tonic-gate 		 */
26644c06356bSdh142964 		MDI_DEBUG(2, (MDI_WARN, pdip, "invalid vhci"));
26657c478bd9Sstevel@tonic-gate 		return (NULL);
26667c478bd9Sstevel@tonic-gate 	}
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 	/*
26695e3986cbScth 	 * Look for pathinfo node identified by paddr.
26707c478bd9Sstevel@tonic-gate 	 */
26717c478bd9Sstevel@tonic-gate 	if (caddr == NULL) {
26727c478bd9Sstevel@tonic-gate 		/*
26737c478bd9Sstevel@tonic-gate 		 * Find a mdi_pathinfo node under pHCI list for a matching
26747c478bd9Sstevel@tonic-gate 		 * unit address.
26757c478bd9Sstevel@tonic-gate 		 */
26765e3986cbScth 		MDI_PHCI_LOCK(ph);
26775e3986cbScth 		if (MDI_PHCI_IS_OFFLINE(ph)) {
26784c06356bSdh142964 			MDI_DEBUG(2, (MDI_WARN, pdip,
26794c06356bSdh142964 			    "offline phci %p", (void *)ph));
26805e3986cbScth 			MDI_PHCI_UNLOCK(ph);
26815e3986cbScth 			return (NULL);
26825e3986cbScth 		}
26837c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)ph->ph_path_head;
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
26867c478bd9Sstevel@tonic-gate 			if (strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
26877c478bd9Sstevel@tonic-gate 				break;
26887c478bd9Sstevel@tonic-gate 			}
26897c478bd9Sstevel@tonic-gate 			pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
26907c478bd9Sstevel@tonic-gate 		}
26915e3986cbScth 		MDI_PHCI_UNLOCK(ph);
26924c06356bSdh142964 		MDI_DEBUG(2, (MDI_NOTE, pdip,
26934c06356bSdh142964 		    "found %s %p", mdi_pi_spathname(pip), (void *)pip));
26947c478bd9Sstevel@tonic-gate 		return (pip);
26957c478bd9Sstevel@tonic-gate 	}
26967c478bd9Sstevel@tonic-gate 
26977c478bd9Sstevel@tonic-gate 	/*
26983c34adc5Sramat 	 * XXX - Is the rest of the code in this function really necessary?
26993c34adc5Sramat 	 * The consumers of mdi_pi_find() can search for the desired pathinfo
27003c34adc5Sramat 	 * node by calling mdi_pi_find(pdip, NULL, paddr). Irrespective of
27013c34adc5Sramat 	 * whether the search is based on the pathinfo nodes attached to
27023c34adc5Sramat 	 * the pHCI or the client node, the result will be the same.
27033c34adc5Sramat 	 */
27043c34adc5Sramat 
27053c34adc5Sramat 	/*
27067c478bd9Sstevel@tonic-gate 	 * Find the client device corresponding to 'caddr'
27077c478bd9Sstevel@tonic-gate 	 */
27085e3986cbScth 	MDI_VHCI_CLIENT_LOCK(vh);
27093c34adc5Sramat 
27103c34adc5Sramat 	/*
27113c34adc5Sramat 	 * XXX - Passing NULL to the following function works as long as the
27123c34adc5Sramat 	 * the client addresses (caddr) are unique per vhci basis.
27133c34adc5Sramat 	 */
27143c34adc5Sramat 	ct = i_mdi_client_find(vh, NULL, caddr);
27157c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
27167c478bd9Sstevel@tonic-gate 		/*
27177c478bd9Sstevel@tonic-gate 		 * Client not found, Obviously mdi_pathinfo node has not been
27187c478bd9Sstevel@tonic-gate 		 * created yet.
27197c478bd9Sstevel@tonic-gate 		 */
27205e3986cbScth 		MDI_VHCI_CLIENT_UNLOCK(vh);
27214c06356bSdh142964 		MDI_DEBUG(2, (MDI_NOTE, pdip,
27224c06356bSdh142964 		    "client not found for caddr @%s", caddr ? caddr : ""));
27235e3986cbScth 		return (NULL);
27247c478bd9Sstevel@tonic-gate 	}
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate 	/*
27277c478bd9Sstevel@tonic-gate 	 * Hold the client lock and look for a mdi_pathinfo node with matching
27287c478bd9Sstevel@tonic-gate 	 * pHCI and paddr
27297c478bd9Sstevel@tonic-gate 	 */
27307c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate 	/*
27337c478bd9Sstevel@tonic-gate 	 * Release the global mutex as it is no more needed. Note: We always
27347c478bd9Sstevel@tonic-gate 	 * respect the locking order while acquiring.
27357c478bd9Sstevel@tonic-gate 	 */
27365e3986cbScth 	MDI_VHCI_CLIENT_UNLOCK(vh);
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
27397c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
27407c478bd9Sstevel@tonic-gate 		/*
27417c478bd9Sstevel@tonic-gate 		 * Compare the unit address
27427c478bd9Sstevel@tonic-gate 		 */
27437c478bd9Sstevel@tonic-gate 		if ((MDI_PI(pip)->pi_phci == ph) &&
27447c478bd9Sstevel@tonic-gate 		    strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
27457c478bd9Sstevel@tonic-gate 			break;
27467c478bd9Sstevel@tonic-gate 		}
27477c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
27487c478bd9Sstevel@tonic-gate 	}
27497c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
27504c06356bSdh142964 	MDI_DEBUG(2, (MDI_NOTE, pdip,
27514c06356bSdh142964 	    "found: %s %p", mdi_pi_spathname(pip), (void *)pip));
27527c478bd9Sstevel@tonic-gate 	return (pip);
27537c478bd9Sstevel@tonic-gate }
27547c478bd9Sstevel@tonic-gate 
27557c478bd9Sstevel@tonic-gate /*
27567c478bd9Sstevel@tonic-gate  * mdi_pi_alloc():
27577c478bd9Sstevel@tonic-gate  *		Allocate and initialize a new instance of a mdi_pathinfo node.
27587c478bd9Sstevel@tonic-gate  *		The mdi_pathinfo node returned by this function identifies a
27597c478bd9Sstevel@tonic-gate  *		unique device path is capable of having properties attached
27607c478bd9Sstevel@tonic-gate  *		and passed to mdi_pi_online() to fully attach and online the
27617c478bd9Sstevel@tonic-gate  *		path and client device node.
27627c478bd9Sstevel@tonic-gate  *		The mdi_pathinfo node returned by this function must be
27637c478bd9Sstevel@tonic-gate  *		destroyed using mdi_pi_free() if the path is no longer
27647c478bd9Sstevel@tonic-gate  *		operational or if the caller fails to attach a client device
27657c478bd9Sstevel@tonic-gate  *		node when calling mdi_pi_online(). The framework will not free
27667c478bd9Sstevel@tonic-gate  *		the resources allocated.
27677c478bd9Sstevel@tonic-gate  *		This function can be called from both interrupt and kernel
27687c478bd9Sstevel@tonic-gate  *		contexts.  DDI_NOSLEEP flag should be used while calling
27697c478bd9Sstevel@tonic-gate  *		from interrupt contexts.
27707c478bd9Sstevel@tonic-gate  * Return Values:
27717c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
27727c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
27737c478bd9Sstevel@tonic-gate  *		MDI_NOMEM
27747c478bd9Sstevel@tonic-gate  */
27757c478bd9Sstevel@tonic-gate /*ARGSUSED*/
27767c478bd9Sstevel@tonic-gate int
27777c478bd9Sstevel@tonic-gate mdi_pi_alloc_compatible(dev_info_t *pdip, char *cname, char *caddr, char *paddr,
27787c478bd9Sstevel@tonic-gate     char **compatible, int ncompatible, int flags, mdi_pathinfo_t **ret_pip)
27797c478bd9Sstevel@tonic-gate {
27807c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
27817c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
27827c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
27837c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip = NULL;
27847c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
27857c478bd9Sstevel@tonic-gate 	int		rv = MDI_NOMEM;
27863c34adc5Sramat 	int		path_allocated = 0;
27877c478bd9Sstevel@tonic-gate 
27884c06356bSdh142964 	MDI_DEBUG(2, (MDI_NOTE, pdip,
27894c06356bSdh142964 	    "cname %s: caddr@%s paddr@%s",
27904c06356bSdh142964 	    cname ? cname : "", caddr ? caddr : "", paddr ? paddr : ""));
27915e3986cbScth 
27927c478bd9Sstevel@tonic-gate 	if (pdip == NULL || cname == NULL || caddr == NULL || paddr == NULL ||
27937c478bd9Sstevel@tonic-gate 	    ret_pip == NULL) {
27947c478bd9Sstevel@tonic-gate 		/* Nothing more to do */
27957c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
27967c478bd9Sstevel@tonic-gate 	}
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 	*ret_pip = NULL;
27995e3986cbScth 
28005e3986cbScth 	/* No allocations on detaching pHCI */
28015e3986cbScth 	if (DEVI_IS_DETACHING(pdip)) {
28025e3986cbScth 		/* Invalid pHCI device, return failure */
28034c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, pdip,
28044c06356bSdh142964 		    "!detaching pHCI=%p", (void *)pdip));
28055e3986cbScth 		return (MDI_FAILURE);
28065e3986cbScth 	}
28075e3986cbScth 
28087c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
28097c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
28107c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
28117c478bd9Sstevel@tonic-gate 		/* Invalid pHCI device, return failure */
28124c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, pdip,
28134c06356bSdh142964 		    "!invalid pHCI=%p", (void *)pdip));
28147c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
28157c478bd9Sstevel@tonic-gate 	}
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
28187c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
28197c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
28207c478bd9Sstevel@tonic-gate 		/* Invalid vHCI device, return failure */
28214c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, pdip,
28224c06356bSdh142964 		    "!invalid vHCI=%p", (void *)pdip));
28237c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
28247c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
28257c478bd9Sstevel@tonic-gate 	}
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_READY(ph) == 0) {
28287c478bd9Sstevel@tonic-gate 		/*
28297c478bd9Sstevel@tonic-gate 		 * Do not allow new node creation when pHCI is in
28307c478bd9Sstevel@tonic-gate 		 * offline/suspended states
28317c478bd9Sstevel@tonic-gate 		 */
28324c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, pdip,
28334c06356bSdh142964 		    "pHCI=%p is not ready", (void *)ph));
28347c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
28357c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
28367c478bd9Sstevel@tonic-gate 	}
28377c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNSTABLE(ph);
28387c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
28397c478bd9Sstevel@tonic-gate 
28403c34adc5Sramat 	/* look for a matching client, create one if not found */
28415e3986cbScth 	MDI_VHCI_CLIENT_LOCK(vh);
28423c34adc5Sramat 	ct = i_mdi_client_find(vh, cname, caddr);
28437c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
28443c34adc5Sramat 		ct = i_mdi_client_alloc(vh, cname, caddr);
28453c34adc5Sramat 		ASSERT(ct != NULL);
28467c478bd9Sstevel@tonic-gate 	}
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 	if (ct->ct_dip == NULL) {
28497c478bd9Sstevel@tonic-gate 		/*
28507c478bd9Sstevel@tonic-gate 		 * Allocate a devinfo node
28517c478bd9Sstevel@tonic-gate 		 */
28527c478bd9Sstevel@tonic-gate 		ct->ct_dip = i_mdi_devinfo_create(vh, cname, caddr,
28533c34adc5Sramat 		    compatible, ncompatible);
28547c478bd9Sstevel@tonic-gate 		if (ct->ct_dip == NULL) {
28557c478bd9Sstevel@tonic-gate 			(void) i_mdi_client_free(vh, ct);
28567c478bd9Sstevel@tonic-gate 			goto fail;
28577c478bd9Sstevel@tonic-gate 		}
28587c478bd9Sstevel@tonic-gate 	}
28597c478bd9Sstevel@tonic-gate 	cdip = ct->ct_dip;
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_component |= MDI_COMPONENT_CLIENT;
28627c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_client = (caddr_t)ct;
28637c478bd9Sstevel@tonic-gate 
28645e3986cbScth 	MDI_CLIENT_LOCK(ct);
28657c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
28667c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
28677c478bd9Sstevel@tonic-gate 		/*
28687c478bd9Sstevel@tonic-gate 		 * Compare the unit address
28697c478bd9Sstevel@tonic-gate 		 */
28707c478bd9Sstevel@tonic-gate 		if ((MDI_PI(pip)->pi_phci == ph) &&
28717c478bd9Sstevel@tonic-gate 		    strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
28727c478bd9Sstevel@tonic-gate 			break;
28737c478bd9Sstevel@tonic-gate 		}
28747c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
28757c478bd9Sstevel@tonic-gate 	}
28765e3986cbScth 	MDI_CLIENT_UNLOCK(ct);
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
28797c478bd9Sstevel@tonic-gate 		/*
28807c478bd9Sstevel@tonic-gate 		 * This is a new path for this client device.  Allocate and
28817c478bd9Sstevel@tonic-gate 		 * initialize a new pathinfo node
28827c478bd9Sstevel@tonic-gate 		 */
28833c34adc5Sramat 		pip = i_mdi_pi_alloc(ph, paddr, ct);
28843c34adc5Sramat 		ASSERT(pip != NULL);
28853c34adc5Sramat 		path_allocated = 1;
28867c478bd9Sstevel@tonic-gate 	}
28877c478bd9Sstevel@tonic-gate 	rv = MDI_SUCCESS;
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate fail:
28907c478bd9Sstevel@tonic-gate 	/*
28917c478bd9Sstevel@tonic-gate 	 * Release the global mutex.
28927c478bd9Sstevel@tonic-gate 	 */
28935e3986cbScth 	MDI_VHCI_CLIENT_UNLOCK(vh);
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 	/*
28967c478bd9Sstevel@tonic-gate 	 * Mark the pHCI as stable
28977c478bd9Sstevel@tonic-gate 	 */
28987c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
28997c478bd9Sstevel@tonic-gate 	MDI_PHCI_STABLE(ph);
29007c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
29017c478bd9Sstevel@tonic-gate 	*ret_pip = pip;
29023c34adc5Sramat 
29034c06356bSdh142964 	MDI_DEBUG(2, (MDI_NOTE, pdip,
29044c06356bSdh142964 	    "alloc %s %p", mdi_pi_spathname(pip), (void *)pip));
29055e3986cbScth 
29063c34adc5Sramat 	if (path_allocated)
29073c34adc5Sramat 		vhcache_pi_add(vh->vh_config, MDI_PI(pip));
29083c34adc5Sramat 
29097c478bd9Sstevel@tonic-gate 	return (rv);
29107c478bd9Sstevel@tonic-gate }
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
29137c478bd9Sstevel@tonic-gate int
29147c478bd9Sstevel@tonic-gate mdi_pi_alloc(dev_info_t *pdip, char *cname, char *caddr, char *paddr,
29157c478bd9Sstevel@tonic-gate     int flags, mdi_pathinfo_t **ret_pip)
29167c478bd9Sstevel@tonic-gate {
29177c478bd9Sstevel@tonic-gate 	return (mdi_pi_alloc_compatible(pdip, cname, caddr, paddr, NULL, 0,
29187c478bd9Sstevel@tonic-gate 	    flags, ret_pip));
29197c478bd9Sstevel@tonic-gate }
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate /*
29227c478bd9Sstevel@tonic-gate  * i_mdi_pi_alloc():
29237c478bd9Sstevel@tonic-gate  *		Allocate a mdi_pathinfo node and add to the pHCI path list
29247c478bd9Sstevel@tonic-gate  * Return Values:
29257c478bd9Sstevel@tonic-gate  *		mdi_pathinfo
29267c478bd9Sstevel@tonic-gate  */
29277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
29287c478bd9Sstevel@tonic-gate static mdi_pathinfo_t *
29293c34adc5Sramat i_mdi_pi_alloc(mdi_phci_t *ph, char *paddr, mdi_client_t *ct)
29307c478bd9Sstevel@tonic-gate {
29313c34adc5Sramat 	mdi_pathinfo_t	*pip;
29327c478bd9Sstevel@tonic-gate 	int		ct_circular;
29337c478bd9Sstevel@tonic-gate 	int		ph_circular;
29344c06356bSdh142964 	static char	path[MAXPATHLEN];	/* mdi_pathmap_mutex protects */
2935602ca9eaScth 	char		*path_persistent;
2936602ca9eaScth 	int		path_instance;
2937602ca9eaScth 	mod_hash_val_t	hv;
29387c478bd9Sstevel@tonic-gate 
29395e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(ph->ph_vhci));
29405e3986cbScth 
29413c34adc5Sramat 	pip = kmem_zalloc(sizeof (struct mdi_pathinfo), KM_SLEEP);
29427c478bd9Sstevel@tonic-gate 	mutex_init(&MDI_PI(pip)->pi_mutex, NULL, MUTEX_DEFAULT, NULL);
29437c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_state = MDI_PATHINFO_STATE_INIT |
29447c478bd9Sstevel@tonic-gate 	    MDI_PATHINFO_STATE_TRANSIENT;
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_USER_DISABLED(ph))
29477c478bd9Sstevel@tonic-gate 		MDI_PI_SET_USER_DISABLE(pip);
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_DRV_DISABLED_TRANSIENT(ph))
29507c478bd9Sstevel@tonic-gate 		MDI_PI_SET_DRV_DISABLE_TRANS(pip);
29517c478bd9Sstevel@tonic-gate 
29527c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_DRV_DISABLED(ph))
29537c478bd9Sstevel@tonic-gate 		MDI_PI_SET_DRV_DISABLE(pip);
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_old_state = MDI_PATHINFO_STATE_INIT;
29567c478bd9Sstevel@tonic-gate 	cv_init(&MDI_PI(pip)->pi_state_cv, NULL, CV_DEFAULT, NULL);
29577c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client = ct;
29587c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci = ph;
29593c34adc5Sramat 	MDI_PI(pip)->pi_addr = kmem_alloc(strlen(paddr) + 1, KM_SLEEP);
29607c478bd9Sstevel@tonic-gate 	(void) strcpy(MDI_PI(pip)->pi_addr, paddr);
2961602ca9eaScth 
2962602ca9eaScth         /*
2963602ca9eaScth 	 * We form the "path" to the pathinfo node, and see if we have
2964602ca9eaScth 	 * already allocated a 'path_instance' for that "path".  If so,
2965602ca9eaScth 	 * we use the already allocated 'path_instance'.  If not, we
2966602ca9eaScth 	 * allocate a new 'path_instance' and associate it with a copy of
2967602ca9eaScth 	 * the "path" string (which is never freed). The association
2968602ca9eaScth 	 * between a 'path_instance' this "path" string persists until
2969602ca9eaScth 	 * reboot.
2970602ca9eaScth 	 */
2971602ca9eaScth         mutex_enter(&mdi_pathmap_mutex);
2972602ca9eaScth 	(void) ddi_pathname(ph->ph_dip, path);
2973602ca9eaScth 	(void) sprintf(path + strlen(path), "/%s@%s",
297455e592a2SRandall Ralphs 	    mdi_pi_get_node_name(pip), mdi_pi_get_addr(pip));
2975602ca9eaScth         if (mod_hash_find(mdi_pathmap_bypath, (mod_hash_key_t)path, &hv) == 0) {
2976602ca9eaScth                 path_instance = (uint_t)(intptr_t)hv;
2977602ca9eaScth         } else {
2978602ca9eaScth 		/* allocate a new 'path_instance' and persistent "path" */
2979602ca9eaScth 		path_instance = mdi_pathmap_instance++;
2980602ca9eaScth 		path_persistent = i_ddi_strdup(path, KM_SLEEP);
2981602ca9eaScth                 (void) mod_hash_insert(mdi_pathmap_bypath,
2982602ca9eaScth                     (mod_hash_key_t)path_persistent,
2983602ca9eaScth                     (mod_hash_val_t)(intptr_t)path_instance);
2984602ca9eaScth 		(void) mod_hash_insert(mdi_pathmap_byinstance,
2985602ca9eaScth 		    (mod_hash_key_t)(intptr_t)path_instance,
2986602ca9eaScth 		    (mod_hash_val_t)path_persistent);
29874c06356bSdh142964 
29884c06356bSdh142964 		/* create shortpath name */
29894c06356bSdh142964 		(void) snprintf(path, sizeof(path), "%s%d/%s@%s",
29904c06356bSdh142964 		    ddi_driver_name(ph->ph_dip), ddi_get_instance(ph->ph_dip),
29914c06356bSdh142964 		    mdi_pi_get_node_name(pip), mdi_pi_get_addr(pip));
29924c06356bSdh142964 		path_persistent = i_ddi_strdup(path, KM_SLEEP);
29934c06356bSdh142964 		(void) mod_hash_insert(mdi_pathmap_sbyinstance,
29944c06356bSdh142964 		    (mod_hash_key_t)(intptr_t)path_instance,
29954c06356bSdh142964 		    (mod_hash_val_t)path_persistent);
2996602ca9eaScth         }
2997602ca9eaScth         mutex_exit(&mdi_pathmap_mutex);
2998602ca9eaScth 	MDI_PI(pip)->pi_path_instance = path_instance;
2999602ca9eaScth 
30003c34adc5Sramat 	(void) nvlist_alloc(&MDI_PI(pip)->pi_prop, NV_UNIQUE_NAME, KM_SLEEP);
30013c34adc5Sramat 	ASSERT(MDI_PI(pip)->pi_prop != NULL);
30027c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_pprivate = NULL;
30037c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_cprivate = NULL;
30047c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_vprivate = NULL;
30057c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client_link = NULL;
30067c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci_link = NULL;
30077c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_ref_cnt = 0;
30087c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_kstats = NULL;
30097c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_preferred = 1;
30107c478bd9Sstevel@tonic-gate 	cv_init(&MDI_PI(pip)->pi_ref_cv, NULL, CV_DEFAULT, NULL);
30117c478bd9Sstevel@tonic-gate 
30127c478bd9Sstevel@tonic-gate 	/*
30137c478bd9Sstevel@tonic-gate 	 * Lock both dev_info nodes against changes in parallel.
30145e3986cbScth 	 *
30155e3986cbScth 	 * The ndi_devi_enter(Client), is atypical since the client is a leaf.
30165e3986cbScth 	 * This atypical operation is done to synchronize pathinfo nodes
30175e3986cbScth 	 * during devinfo snapshot (see di_register_pip) by 'pretending' that
30185e3986cbScth 	 * the pathinfo nodes are children of the Client.
30197c478bd9Sstevel@tonic-gate 	 */
30207c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ct->ct_dip, &ct_circular);
30217c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ph->ph_dip, &ph_circular);
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate 	i_mdi_phci_add_path(ph, pip);
30247c478bd9Sstevel@tonic-gate 	i_mdi_client_add_path(ct, pip);
30257c478bd9Sstevel@tonic-gate 
30267c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ph->ph_dip, ph_circular);
30277c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ct->ct_dip, ct_circular);
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate 	return (pip);
30307c478bd9Sstevel@tonic-gate }
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate /*
3033602ca9eaScth  * mdi_pi_pathname_by_instance():
3034602ca9eaScth  *	Lookup of "path" by 'path_instance'. Return "path".
3035602ca9eaScth  *	NOTE: returned "path" remains valid forever (until reboot).
3036602ca9eaScth  */
3037602ca9eaScth char *
3038602ca9eaScth mdi_pi_pathname_by_instance(int path_instance)
3039602ca9eaScth {
3040602ca9eaScth 	char		*path;
3041602ca9eaScth 	mod_hash_val_t	hv;
3042602ca9eaScth 
3043602ca9eaScth 	/* mdi_pathmap lookup of "path" by 'path_instance' */
3044602ca9eaScth 	mutex_enter(&mdi_pathmap_mutex);
3045602ca9eaScth 	if (mod_hash_find(mdi_pathmap_byinstance,
3046602ca9eaScth 	    (mod_hash_key_t)(intptr_t)path_instance, &hv) == 0)
3047602ca9eaScth 		path = (char *)hv;
3048602ca9eaScth 	else
3049602ca9eaScth 		path = NULL;
3050602ca9eaScth 	mutex_exit(&mdi_pathmap_mutex);
3051602ca9eaScth 	return (path);
3052602ca9eaScth }
3053602ca9eaScth 
3054602ca9eaScth /*
30554c06356bSdh142964  * mdi_pi_spathname_by_instance():
30564c06356bSdh142964  *	Lookup of "shortpath" by 'path_instance'. Return "shortpath".
30574c06356bSdh142964  *	NOTE: returned "shortpath" remains valid forever (until reboot).
30584c06356bSdh142964  */
30594c06356bSdh142964 char *
30604c06356bSdh142964 mdi_pi_spathname_by_instance(int path_instance)
30614c06356bSdh142964 {
30624c06356bSdh142964 	char		*path;
30634c06356bSdh142964 	mod_hash_val_t	hv;
30644c06356bSdh142964 
30654c06356bSdh142964 	/* mdi_pathmap lookup of "path" by 'path_instance' */
30664c06356bSdh142964 	mutex_enter(&mdi_pathmap_mutex);
30674c06356bSdh142964 	if (mod_hash_find(mdi_pathmap_sbyinstance,
30684c06356bSdh142964 	    (mod_hash_key_t)(intptr_t)path_instance, &hv) == 0)
30694c06356bSdh142964 		path = (char *)hv;
30704c06356bSdh142964 	else
30714c06356bSdh142964 		path = NULL;
30724c06356bSdh142964 	mutex_exit(&mdi_pathmap_mutex);
30734c06356bSdh142964 	return (path);
30744c06356bSdh142964 }
30754c06356bSdh142964 
30764c06356bSdh142964 
30774c06356bSdh142964 /*
30787c478bd9Sstevel@tonic-gate  * i_mdi_phci_add_path():
30797c478bd9Sstevel@tonic-gate  * 		Add a mdi_pathinfo node to pHCI list.
30807c478bd9Sstevel@tonic-gate  * Notes:
30817c478bd9Sstevel@tonic-gate  *		Caller should per-pHCI mutex
30827c478bd9Sstevel@tonic-gate  */
30837c478bd9Sstevel@tonic-gate static void
30847c478bd9Sstevel@tonic-gate i_mdi_phci_add_path(mdi_phci_t *ph, mdi_pathinfo_t *pip)
30857c478bd9Sstevel@tonic-gate {
30867c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ph->ph_dip));
30877c478bd9Sstevel@tonic-gate 
30885e3986cbScth 	MDI_PHCI_LOCK(ph);
30897c478bd9Sstevel@tonic-gate 	if (ph->ph_path_head == NULL) {
30907c478bd9Sstevel@tonic-gate 		ph->ph_path_head = pip;
30917c478bd9Sstevel@tonic-gate 	} else {
30927c478bd9Sstevel@tonic-gate 		MDI_PI(ph->ph_path_tail)->pi_phci_link = MDI_PI(pip);
30937c478bd9Sstevel@tonic-gate 	}
30947c478bd9Sstevel@tonic-gate 	ph->ph_path_tail = pip;
30957c478bd9Sstevel@tonic-gate 	ph->ph_path_count++;
30965e3986cbScth 	MDI_PHCI_UNLOCK(ph);
30977c478bd9Sstevel@tonic-gate }
30987c478bd9Sstevel@tonic-gate 
30997c478bd9Sstevel@tonic-gate /*
31007c478bd9Sstevel@tonic-gate  * i_mdi_client_add_path():
31017c478bd9Sstevel@tonic-gate  *		Add mdi_pathinfo node to client list
31027c478bd9Sstevel@tonic-gate  */
31037c478bd9Sstevel@tonic-gate static void
31047c478bd9Sstevel@tonic-gate i_mdi_client_add_path(mdi_client_t *ct, mdi_pathinfo_t *pip)
31057c478bd9Sstevel@tonic-gate {
31067c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ct->ct_dip));
31077c478bd9Sstevel@tonic-gate 
31085e3986cbScth 	MDI_CLIENT_LOCK(ct);
31097c478bd9Sstevel@tonic-gate 	if (ct->ct_path_head == NULL) {
31107c478bd9Sstevel@tonic-gate 		ct->ct_path_head = pip;
31117c478bd9Sstevel@tonic-gate 	} else {
31127c478bd9Sstevel@tonic-gate 		MDI_PI(ct->ct_path_tail)->pi_client_link = MDI_PI(pip);
31137c478bd9Sstevel@tonic-gate 	}
31147c478bd9Sstevel@tonic-gate 	ct->ct_path_tail = pip;
31157c478bd9Sstevel@tonic-gate 	ct->ct_path_count++;
31165e3986cbScth 	MDI_CLIENT_UNLOCK(ct);
31177c478bd9Sstevel@tonic-gate }
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate /*
31207c478bd9Sstevel@tonic-gate  * mdi_pi_free():
31217c478bd9Sstevel@tonic-gate  *		Free the mdi_pathinfo node and also client device node if this
31227c478bd9Sstevel@tonic-gate  *		is the last path to the device
31237c478bd9Sstevel@tonic-gate  * Return Values:
31247c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
31257c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
31267c478bd9Sstevel@tonic-gate  *		MDI_BUSY
31277c478bd9Sstevel@tonic-gate  */
31287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
31297c478bd9Sstevel@tonic-gate int
31307c478bd9Sstevel@tonic-gate mdi_pi_free(mdi_pathinfo_t *pip, int flags)
31317c478bd9Sstevel@tonic-gate {
31329aed1621SDavid Hollister 	int		rv;
31337c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
31347c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
31357c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
31367c478bd9Sstevel@tonic-gate 	int		(*f)();
31377c478bd9Sstevel@tonic-gate 	int		client_held = 0;
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
31407c478bd9Sstevel@tonic-gate 	ph = MDI_PI(pip)->pi_phci;
31417c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
31427c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
31437c478bd9Sstevel@tonic-gate 		/*
31447c478bd9Sstevel@tonic-gate 		 * Invalid pHCI device, return failure
31457c478bd9Sstevel@tonic-gate 		 */
31464c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, NULL,
31474c06356bSdh142964 		    "!invalid pHCI: pip %s %p",
31484c06356bSdh142964 		    mdi_pi_spathname(pip), (void *)pip));
31497c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
31507c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
31517c478bd9Sstevel@tonic-gate 	}
31527c478bd9Sstevel@tonic-gate 
31537c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
31547c478bd9Sstevel@tonic-gate 	ASSERT(vh != NULL);
31557c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
31567c478bd9Sstevel@tonic-gate 		/* Invalid pHCI device, return failure */
31574c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, ph->ph_dip,
31584c06356bSdh142964 		    "!invalid vHCI: pip %s %p",
31594c06356bSdh142964 		    mdi_pi_spathname(pip), (void *)pip));
31607c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
31617c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
31627c478bd9Sstevel@tonic-gate 	}
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 	ct = MDI_PI(pip)->pi_client;
31657c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
31667c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
31677c478bd9Sstevel@tonic-gate 		/*
31687c478bd9Sstevel@tonic-gate 		 * Invalid Client device, return failure
31697c478bd9Sstevel@tonic-gate 		 */
31704c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, ph->ph_dip,
31714c06356bSdh142964 		    "!invalid client: pip %s %p",
31724c06356bSdh142964 		    mdi_pi_spathname(pip), (void *)pip));
31737c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
31747c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
31757c478bd9Sstevel@tonic-gate 	}
31767c478bd9Sstevel@tonic-gate 
31777c478bd9Sstevel@tonic-gate 	/*
31787c478bd9Sstevel@tonic-gate 	 * Check to see for busy condition.  A mdi_pathinfo can only be freed
31797c478bd9Sstevel@tonic-gate 	 * if the node state is either offline or init and the reference count
31807c478bd9Sstevel@tonic-gate 	 * is zero.
31817c478bd9Sstevel@tonic-gate 	 */
31827c478bd9Sstevel@tonic-gate 	if (!(MDI_PI_IS_OFFLINE(pip) || MDI_PI_IS_INIT(pip) ||
31837c478bd9Sstevel@tonic-gate 	    MDI_PI_IS_INITING(pip))) {
31847c478bd9Sstevel@tonic-gate 		/*
31857c478bd9Sstevel@tonic-gate 		 * Node is busy
31867c478bd9Sstevel@tonic-gate 		 */
31874c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, ct->ct_dip,
31884c06356bSdh142964 		    "!busy: pip %s %p", mdi_pi_spathname(pip), (void *)pip));
31897c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
31907c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
31917c478bd9Sstevel@tonic-gate 	}
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 	while (MDI_PI(pip)->pi_ref_cnt != 0) {
31947c478bd9Sstevel@tonic-gate 		/*
31957c478bd9Sstevel@tonic-gate 		 * Give a chance for pending I/Os to complete.
31967c478bd9Sstevel@tonic-gate 		 */
31974c06356bSdh142964 		MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
31984c06356bSdh142964 		    "!%d cmds still pending on path: %s %p",
31994c06356bSdh142964 		    MDI_PI(pip)->pi_ref_cnt,
32004c06356bSdh142964 		    mdi_pi_spathname(pip), (void *)pip));
3201d3d50737SRafael Vanoni 		if (cv_reltimedwait(&MDI_PI(pip)->pi_ref_cv,
3202d3d50737SRafael Vanoni 		    &MDI_PI(pip)->pi_mutex, drv_usectohz(60 * 1000000),
3203d3d50737SRafael Vanoni 		    TR_CLOCK_TICK) == -1) {
32047c478bd9Sstevel@tonic-gate 			/*
32057c478bd9Sstevel@tonic-gate 			 * The timeout time reached without ref_cnt being zero
32067c478bd9Sstevel@tonic-gate 			 * being signaled.
32077c478bd9Sstevel@tonic-gate 			 */
32084c06356bSdh142964 			MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
32094c06356bSdh142964 			    "!Timeout reached on path %s %p without the cond",
32104c06356bSdh142964 			    mdi_pi_spathname(pip), (void *)pip));
32114c06356bSdh142964 			MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
32124c06356bSdh142964 			    "!%d cmds still pending on path %s %p",
32134c06356bSdh142964 			    MDI_PI(pip)->pi_ref_cnt,
32144c06356bSdh142964 			    mdi_pi_spathname(pip), (void *)pip));
32157c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
32167c478bd9Sstevel@tonic-gate 			return (MDI_BUSY);
32177c478bd9Sstevel@tonic-gate 		}
32187c478bd9Sstevel@tonic-gate 	}
32197c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held) {
32207c478bd9Sstevel@tonic-gate 		client_held = 1;
32217c478bd9Sstevel@tonic-gate 	}
32227c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
32237c478bd9Sstevel@tonic-gate 
32243c34adc5Sramat 	vhcache_pi_remove(vh->vh_config, MDI_PI(pip));
32253c34adc5Sramat 
32267c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
32277c478bd9Sstevel@tonic-gate 
32285e3986cbScth 	/* Prevent further failovers till MDI_VHCI_CLIENT_LOCK is held */
32297c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_PATH_FREE_IN_PROGRESS(ct);
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 	/*
32327c478bd9Sstevel@tonic-gate 	 * Wait till failover is complete before removing this node.
32337c478bd9Sstevel@tonic-gate 	 */
32347c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct))
32357c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_failover_cv, &ct->ct_mutex);
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
32385e3986cbScth 	MDI_VHCI_CLIENT_LOCK(vh);
32397c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
32407c478bd9Sstevel@tonic-gate 	MDI_CLIENT_CLEAR_PATH_FREE_IN_PROGRESS(ct);
32417c478bd9Sstevel@tonic-gate 
32427c478bd9Sstevel@tonic-gate 	if (!MDI_PI_IS_INITING(pip)) {
32437c478bd9Sstevel@tonic-gate 		f = vh->vh_ops->vo_pi_uninit;
32447c478bd9Sstevel@tonic-gate 		if (f != NULL) {
32457c478bd9Sstevel@tonic-gate 			rv = (*f)(vh->vh_dip, pip, 0);
32467c478bd9Sstevel@tonic-gate 		}
32479aed1621SDavid Hollister 	} else
32489aed1621SDavid Hollister 		rv = MDI_SUCCESS;
32499aed1621SDavid Hollister 
32507c478bd9Sstevel@tonic-gate 	/*
32517c478bd9Sstevel@tonic-gate 	 * If vo_pi_uninit() completed successfully.
32527c478bd9Sstevel@tonic-gate 	 */
32537c478bd9Sstevel@tonic-gate 	if (rv == MDI_SUCCESS) {
32547c478bd9Sstevel@tonic-gate 		if (client_held) {
32554c06356bSdh142964 			MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
32567c478bd9Sstevel@tonic-gate 			    "i_mdi_pm_rele_client\n"));
32577c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, 1);
32587c478bd9Sstevel@tonic-gate 		}
32597c478bd9Sstevel@tonic-gate 		i_mdi_pi_free(ph, pip, ct);
32607c478bd9Sstevel@tonic-gate 		if (ct->ct_path_count == 0) {
32617c478bd9Sstevel@tonic-gate 			/*
32627c478bd9Sstevel@tonic-gate 			 * Client lost its last path.
32637c478bd9Sstevel@tonic-gate 			 * Clean up the client device
32647c478bd9Sstevel@tonic-gate 			 */
32657c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
32667c478bd9Sstevel@tonic-gate 			(void) i_mdi_client_free(ct->ct_vhci, ct);
32675e3986cbScth 			MDI_VHCI_CLIENT_UNLOCK(vh);
32687c478bd9Sstevel@tonic-gate 			return (rv);
32697c478bd9Sstevel@tonic-gate 		}
32707c478bd9Sstevel@tonic-gate 	}
32717c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
32725e3986cbScth 	MDI_VHCI_CLIENT_UNLOCK(vh);
32733c34adc5Sramat 
32743c34adc5Sramat 	if (rv == MDI_FAILURE)
32753c34adc5Sramat 		vhcache_pi_add(vh->vh_config, MDI_PI(pip));
32763c34adc5Sramat 
32777c478bd9Sstevel@tonic-gate 	return (rv);
32787c478bd9Sstevel@tonic-gate }
32797c478bd9Sstevel@tonic-gate 
32807c478bd9Sstevel@tonic-gate /*
32817c478bd9Sstevel@tonic-gate  * i_mdi_pi_free():
32827c478bd9Sstevel@tonic-gate  *		Free the mdi_pathinfo node
32837c478bd9Sstevel@tonic-gate  */
32847c478bd9Sstevel@tonic-gate static void
32857c478bd9Sstevel@tonic-gate i_mdi_pi_free(mdi_phci_t *ph, mdi_pathinfo_t *pip, mdi_client_t *ct)
32867c478bd9Sstevel@tonic-gate {
32877c478bd9Sstevel@tonic-gate 	int	ct_circular;
32887c478bd9Sstevel@tonic-gate 	int	ph_circular;
32897c478bd9Sstevel@tonic-gate 
32905e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
32915e3986cbScth 
32927c478bd9Sstevel@tonic-gate 	/*
32937c478bd9Sstevel@tonic-gate 	 * remove any per-path kstats
32947c478bd9Sstevel@tonic-gate 	 */
32957c478bd9Sstevel@tonic-gate 	i_mdi_pi_kstat_destroy(pip);
32967c478bd9Sstevel@tonic-gate 
32975e3986cbScth 	/* See comments in i_mdi_pi_alloc() */
32987c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ct->ct_dip, &ct_circular);
32997c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ph->ph_dip, &ph_circular);
33007c478bd9Sstevel@tonic-gate 
33017c478bd9Sstevel@tonic-gate 	i_mdi_client_remove_path(ct, pip);
33027c478bd9Sstevel@tonic-gate 	i_mdi_phci_remove_path(ph, pip);
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ph->ph_dip, ph_circular);
33057c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ct->ct_dip, ct_circular);
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate 	mutex_destroy(&MDI_PI(pip)->pi_mutex);
33087c478bd9Sstevel@tonic-gate 	cv_destroy(&MDI_PI(pip)->pi_state_cv);
33097c478bd9Sstevel@tonic-gate 	cv_destroy(&MDI_PI(pip)->pi_ref_cv);
33107c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_addr) {
33117c478bd9Sstevel@tonic-gate 		kmem_free(MDI_PI(pip)->pi_addr,
33127c478bd9Sstevel@tonic-gate 		    strlen(MDI_PI(pip)->pi_addr) + 1);
33137c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_addr = NULL;
33147c478bd9Sstevel@tonic-gate 	}
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop) {
33177c478bd9Sstevel@tonic-gate 		(void) nvlist_free(MDI_PI(pip)->pi_prop);
33187c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_prop = NULL;
33197c478bd9Sstevel@tonic-gate 	}
33207c478bd9Sstevel@tonic-gate 	kmem_free(pip, sizeof (struct mdi_pathinfo));
33217c478bd9Sstevel@tonic-gate }
33227c478bd9Sstevel@tonic-gate 
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate /*
33257c478bd9Sstevel@tonic-gate  * i_mdi_phci_remove_path():
33267c478bd9Sstevel@tonic-gate  * 		Remove a mdi_pathinfo node from pHCI list.
33277c478bd9Sstevel@tonic-gate  * Notes:
33287c478bd9Sstevel@tonic-gate  *		Caller should hold per-pHCI mutex
33297c478bd9Sstevel@tonic-gate  */
33307c478bd9Sstevel@tonic-gate static void
33317c478bd9Sstevel@tonic-gate i_mdi_phci_remove_path(mdi_phci_t *ph, mdi_pathinfo_t *pip)
33327c478bd9Sstevel@tonic-gate {
33337c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*prev = NULL;
33347c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*path = NULL;
33357c478bd9Sstevel@tonic-gate 
33367c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ph->ph_dip));
33377c478bd9Sstevel@tonic-gate 
33385e3986cbScth 	MDI_PHCI_LOCK(ph);
33397c478bd9Sstevel@tonic-gate 	path = ph->ph_path_head;
33407c478bd9Sstevel@tonic-gate 	while (path != NULL) {
33417c478bd9Sstevel@tonic-gate 		if (path == pip) {
33427c478bd9Sstevel@tonic-gate 			break;
33437c478bd9Sstevel@tonic-gate 		}
33447c478bd9Sstevel@tonic-gate 		prev = path;
33457c478bd9Sstevel@tonic-gate 		path = (mdi_pathinfo_t *)MDI_PI(path)->pi_phci_link;
33467c478bd9Sstevel@tonic-gate 	}
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 	if (path) {
33497c478bd9Sstevel@tonic-gate 		ph->ph_path_count--;
33507c478bd9Sstevel@tonic-gate 		if (prev) {
33517c478bd9Sstevel@tonic-gate 			MDI_PI(prev)->pi_phci_link = MDI_PI(path)->pi_phci_link;
33527c478bd9Sstevel@tonic-gate 		} else {
33537c478bd9Sstevel@tonic-gate 			ph->ph_path_head =
33547c478bd9Sstevel@tonic-gate 			    (mdi_pathinfo_t *)MDI_PI(path)->pi_phci_link;
33557c478bd9Sstevel@tonic-gate 		}
33567c478bd9Sstevel@tonic-gate 		if (ph->ph_path_tail == path) {
33577c478bd9Sstevel@tonic-gate 			ph->ph_path_tail = prev;
33587c478bd9Sstevel@tonic-gate 		}
33597c478bd9Sstevel@tonic-gate 	}
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate 	/*
33627c478bd9Sstevel@tonic-gate 	 * Clear the pHCI link
33637c478bd9Sstevel@tonic-gate 	 */
33647c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci_link = NULL;
33657c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci = NULL;
33665e3986cbScth 	MDI_PHCI_UNLOCK(ph);
33677c478bd9Sstevel@tonic-gate }
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate /*
33707c478bd9Sstevel@tonic-gate  * i_mdi_client_remove_path():
33717c478bd9Sstevel@tonic-gate  * 		Remove a mdi_pathinfo node from client path list.
33727c478bd9Sstevel@tonic-gate  */
33737c478bd9Sstevel@tonic-gate static void
33747c478bd9Sstevel@tonic-gate i_mdi_client_remove_path(mdi_client_t *ct, mdi_pathinfo_t *pip)
33757c478bd9Sstevel@tonic-gate {
33767c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*prev = NULL;
33777c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*path;
33787c478bd9Sstevel@tonic-gate 
33797c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ct->ct_dip));
33807c478bd9Sstevel@tonic-gate 
33815e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
33827c478bd9Sstevel@tonic-gate 	path = ct->ct_path_head;
33837c478bd9Sstevel@tonic-gate 	while (path != NULL) {
33847c478bd9Sstevel@tonic-gate 		if (path == pip) {
33857c478bd9Sstevel@tonic-gate 			break;
33867c478bd9Sstevel@tonic-gate 		}
33877c478bd9Sstevel@tonic-gate 		prev = path;
33887c478bd9Sstevel@tonic-gate 		path = (mdi_pathinfo_t *)MDI_PI(path)->pi_client_link;
33897c478bd9Sstevel@tonic-gate 	}
33907c478bd9Sstevel@tonic-gate 
33917c478bd9Sstevel@tonic-gate 	if (path) {
33927c478bd9Sstevel@tonic-gate 		ct->ct_path_count--;
33937c478bd9Sstevel@tonic-gate 		if (prev) {
33947c478bd9Sstevel@tonic-gate 			MDI_PI(prev)->pi_client_link =
33957c478bd9Sstevel@tonic-gate 			    MDI_PI(path)->pi_client_link;
33967c478bd9Sstevel@tonic-gate 		} else {
33977c478bd9Sstevel@tonic-gate 			ct->ct_path_head =
33987c478bd9Sstevel@tonic-gate 			    (mdi_pathinfo_t *)MDI_PI(path)->pi_client_link;
33997c478bd9Sstevel@tonic-gate 		}
34007c478bd9Sstevel@tonic-gate 		if (ct->ct_path_tail == path) {
34017c478bd9Sstevel@tonic-gate 			ct->ct_path_tail = prev;
34027c478bd9Sstevel@tonic-gate 		}
34037c478bd9Sstevel@tonic-gate 		if (ct->ct_path_last == path) {
34047c478bd9Sstevel@tonic-gate 			ct->ct_path_last = ct->ct_path_head;
34057c478bd9Sstevel@tonic-gate 		}
34067c478bd9Sstevel@tonic-gate 	}
34077c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client_link = NULL;
34087c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client = NULL;
34097c478bd9Sstevel@tonic-gate }
34107c478bd9Sstevel@tonic-gate 
34117c478bd9Sstevel@tonic-gate /*
34127c478bd9Sstevel@tonic-gate  * i_mdi_pi_state_change():
34137c478bd9Sstevel@tonic-gate  *		online a mdi_pathinfo node
34147c478bd9Sstevel@tonic-gate  *
34157c478bd9Sstevel@tonic-gate  * Return Values:
34167c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
34177c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
34187c478bd9Sstevel@tonic-gate  */
34197c478bd9Sstevel@tonic-gate /*ARGSUSED*/
34207c478bd9Sstevel@tonic-gate static int
34217c478bd9Sstevel@tonic-gate i_mdi_pi_state_change(mdi_pathinfo_t *pip, mdi_pathinfo_state_t state, int flag)
34227c478bd9Sstevel@tonic-gate {
34237c478bd9Sstevel@tonic-gate 	int		rv = MDI_SUCCESS;
34247c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
34257c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
34267c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
34277c478bd9Sstevel@tonic-gate 	int		(*f)();
34287c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
34297c478bd9Sstevel@tonic-gate 
34307c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
34317c478bd9Sstevel@tonic-gate 
34327c478bd9Sstevel@tonic-gate 	ph = MDI_PI(pip)->pi_phci;
34337c478bd9Sstevel@tonic-gate 	ASSERT(ph);
34347c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
34357c478bd9Sstevel@tonic-gate 		/*
34367c478bd9Sstevel@tonic-gate 		 * Invalid pHCI device, fail the request
34377c478bd9Sstevel@tonic-gate 		 */
34387c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
34394c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, NULL,
34404c06356bSdh142964 		    "!invalid phci: pip %s %p",
34414c06356bSdh142964 		    mdi_pi_spathname(pip), (void *)pip));
34427c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
34437c478bd9Sstevel@tonic-gate 	}
34447c478bd9Sstevel@tonic-gate 
34457c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
34467c478bd9Sstevel@tonic-gate 	ASSERT(vh);
34477c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
34487c478bd9Sstevel@tonic-gate 		/*
34497c478bd9Sstevel@tonic-gate 		 * Invalid vHCI device, fail the request
34507c478bd9Sstevel@tonic-gate 		 */
34517c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
34524c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, ph->ph_dip,
34534c06356bSdh142964 		    "!invalid vhci: pip %s %p",
34544c06356bSdh142964 		    mdi_pi_spathname(pip), (void *)pip));
34557c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
34567c478bd9Sstevel@tonic-gate 	}
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate 	ct = MDI_PI(pip)->pi_client;
34597c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
34607c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
34617c478bd9Sstevel@tonic-gate 		/*
34627c478bd9Sstevel@tonic-gate 		 * Invalid client device, fail the request
34637c478bd9Sstevel@tonic-gate 		 */
34647c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
34654c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, ph->ph_dip,
34664c06356bSdh142964 		    "!invalid client: pip %s %p",
34674c06356bSdh142964 		    mdi_pi_spathname(pip), (void *)pip));
34687c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
34697c478bd9Sstevel@tonic-gate 	}
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate 	/*
34727c478bd9Sstevel@tonic-gate 	 * If this path has not been initialized yet, Callback vHCI driver's
34737c478bd9Sstevel@tonic-gate 	 * pathinfo node initialize entry point
34747c478bd9Sstevel@tonic-gate 	 */
34757c478bd9Sstevel@tonic-gate 
34767c478bd9Sstevel@tonic-gate 	if (MDI_PI_IS_INITING(pip)) {
34777c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
34787c478bd9Sstevel@tonic-gate 		f = vh->vh_ops->vo_pi_init;
34797c478bd9Sstevel@tonic-gate 		if (f != NULL) {
34807c478bd9Sstevel@tonic-gate 			rv = (*f)(vh->vh_dip, pip, 0);
34817c478bd9Sstevel@tonic-gate 			if (rv != MDI_SUCCESS) {
34824c06356bSdh142964 				MDI_DEBUG(1, (MDI_WARN, ct->ct_dip,
34834c06356bSdh142964 				    "!vo_pi_init failed: vHCI %p, pip %s %p",
34844c06356bSdh142964 				    (void *)vh, mdi_pi_spathname(pip),
34854c06356bSdh142964 				    (void *)pip));
34867c478bd9Sstevel@tonic-gate 				return (MDI_FAILURE);
34877c478bd9Sstevel@tonic-gate 			}
34887c478bd9Sstevel@tonic-gate 		}
34897c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
34907c478bd9Sstevel@tonic-gate 		MDI_PI_CLEAR_TRANSIENT(pip);
34917c478bd9Sstevel@tonic-gate 	}
34927c478bd9Sstevel@tonic-gate 
34937c478bd9Sstevel@tonic-gate 	/*
34947c478bd9Sstevel@tonic-gate 	 * Do not allow state transition when pHCI is in offline/suspended
34957c478bd9Sstevel@tonic-gate 	 * states
34967c478bd9Sstevel@tonic-gate 	 */
34977c478bd9Sstevel@tonic-gate 	i_mdi_phci_lock(ph, pip);
34987c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_READY(ph) == 0) {
34994c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, ct->ct_dip,
35004c06356bSdh142964 		    "!pHCI not ready, pHCI=%p", (void *)ph));
35017c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
35027c478bd9Sstevel@tonic-gate 		i_mdi_phci_unlock(ph);
35037c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
35047c478bd9Sstevel@tonic-gate 	}
35057c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNSTABLE(ph);
35067c478bd9Sstevel@tonic-gate 	i_mdi_phci_unlock(ph);
35077c478bd9Sstevel@tonic-gate 
35087c478bd9Sstevel@tonic-gate 	/*
35097c478bd9Sstevel@tonic-gate 	 * Check if mdi_pathinfo state is in transient state.
35107c478bd9Sstevel@tonic-gate 	 * If yes, offlining is in progress and wait till transient state is
35117c478bd9Sstevel@tonic-gate 	 * cleared.
35127c478bd9Sstevel@tonic-gate 	 */
35137c478bd9Sstevel@tonic-gate 	if (MDI_PI_IS_TRANSIENT(pip)) {
35147c478bd9Sstevel@tonic-gate 		while (MDI_PI_IS_TRANSIENT(pip)) {
35157c478bd9Sstevel@tonic-gate 			cv_wait(&MDI_PI(pip)->pi_state_cv,
35167c478bd9Sstevel@tonic-gate 			    &MDI_PI(pip)->pi_mutex);
35177c478bd9Sstevel@tonic-gate 		}
35187c478bd9Sstevel@tonic-gate 	}
35197c478bd9Sstevel@tonic-gate 
35207c478bd9Sstevel@tonic-gate 	/*
35217c478bd9Sstevel@tonic-gate 	 * Grab the client lock in reverse order sequence and release the
35227c478bd9Sstevel@tonic-gate 	 * mdi_pathinfo mutex.
35237c478bd9Sstevel@tonic-gate 	 */
35247c478bd9Sstevel@tonic-gate 	i_mdi_client_lock(ct, pip);
35257c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate 	/*
35287c478bd9Sstevel@tonic-gate 	 * Wait till failover state is cleared
35297c478bd9Sstevel@tonic-gate 	 */
35307c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct))
35317c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_failover_cv, &ct->ct_mutex);
35327c478bd9Sstevel@tonic-gate 
35337c478bd9Sstevel@tonic-gate 	/*
35347c478bd9Sstevel@tonic-gate 	 * Mark the mdi_pathinfo node state as transient
35357c478bd9Sstevel@tonic-gate 	 */
35367c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
35377c478bd9Sstevel@tonic-gate 	switch (state) {
35387c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_ONLINE:
35397c478bd9Sstevel@tonic-gate 		MDI_PI_SET_ONLINING(pip);
35407c478bd9Sstevel@tonic-gate 		break;
35417c478bd9Sstevel@tonic-gate 
35427c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_STANDBY:
35437c478bd9Sstevel@tonic-gate 		MDI_PI_SET_STANDBYING(pip);
35447c478bd9Sstevel@tonic-gate 		break;
35457c478bd9Sstevel@tonic-gate 
35467c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_FAULT:
35477c478bd9Sstevel@tonic-gate 		/*
35487c478bd9Sstevel@tonic-gate 		 * Mark the pathinfo state as FAULTED
35497c478bd9Sstevel@tonic-gate 		 */
35507c478bd9Sstevel@tonic-gate 		MDI_PI_SET_FAULTING(pip);
35517c478bd9Sstevel@tonic-gate 		MDI_PI_ERRSTAT(pip, MDI_PI_HARDERR);
35527c478bd9Sstevel@tonic-gate 		break;
35537c478bd9Sstevel@tonic-gate 
35547c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_OFFLINE:
35557c478bd9Sstevel@tonic-gate 		/*
35567c478bd9Sstevel@tonic-gate 		 * ndi_devi_offline() cannot hold pip or ct locks.
35577c478bd9Sstevel@tonic-gate 		 */
35587c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
35594c06356bSdh142964 
35607c478bd9Sstevel@tonic-gate 		/*
35614c06356bSdh142964 		 * If this is a user initiated path online->offline operation
35624c06356bSdh142964 		 * who's success would transition a client from DEGRADED to
35634c06356bSdh142964 		 * FAILED then only proceed if we can offline the client first.
35647c478bd9Sstevel@tonic-gate 		 */
35657c478bd9Sstevel@tonic-gate 		cdip = ct->ct_dip;
35664c06356bSdh142964 		if ((flag & NDI_USER_REQ) &&
35674c06356bSdh142964 		    MDI_PI_IS_ONLINE(pip) &&
35684c06356bSdh142964 		    (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_DEGRADED)) {
35697c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
35704c06356bSdh142964 			rv = ndi_devi_offline(cdip, NDI_DEVFS_CLEAN);
35717c478bd9Sstevel@tonic-gate 			if (rv != NDI_SUCCESS) {
35727c478bd9Sstevel@tonic-gate 				/*
35737c478bd9Sstevel@tonic-gate 				 * Convert to MDI error code
35747c478bd9Sstevel@tonic-gate 				 */
35757c478bd9Sstevel@tonic-gate 				switch (rv) {
35767c478bd9Sstevel@tonic-gate 				case NDI_BUSY:
35777c478bd9Sstevel@tonic-gate 					rv = MDI_BUSY;
35787c478bd9Sstevel@tonic-gate 					break;
35797c478bd9Sstevel@tonic-gate 				default:
35807c478bd9Sstevel@tonic-gate 					rv = MDI_FAILURE;
35817c478bd9Sstevel@tonic-gate 					break;
35827c478bd9Sstevel@tonic-gate 				}
35837c478bd9Sstevel@tonic-gate 				goto state_change_exit;
35847c478bd9Sstevel@tonic-gate 			} else {
35857c478bd9Sstevel@tonic-gate 				i_mdi_client_lock(ct, NULL);
35867c478bd9Sstevel@tonic-gate 			}
35877c478bd9Sstevel@tonic-gate 		}
35887c478bd9Sstevel@tonic-gate 		/*
35897c478bd9Sstevel@tonic-gate 		 * Mark the mdi_pathinfo node state as transient
35907c478bd9Sstevel@tonic-gate 		 */
35917c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
35927c478bd9Sstevel@tonic-gate 		MDI_PI_SET_OFFLINING(pip);
35937c478bd9Sstevel@tonic-gate 		break;
35947c478bd9Sstevel@tonic-gate 	}
35957c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
35967c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNSTABLE(ct);
35977c478bd9Sstevel@tonic-gate 	i_mdi_client_unlock(ct);
35987c478bd9Sstevel@tonic-gate 
35997c478bd9Sstevel@tonic-gate 	f = vh->vh_ops->vo_pi_state_change;
36005e3986cbScth 	if (f != NULL)
36017c478bd9Sstevel@tonic-gate 		rv = (*f)(vh->vh_dip, pip, state, 0, flag);
36025e3986cbScth 
36035e3986cbScth 	MDI_CLIENT_LOCK(ct);
36045e3986cbScth 	MDI_PI_LOCK(pip);
36057c478bd9Sstevel@tonic-gate 	if (rv == MDI_NOT_SUPPORTED) {
36067c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_DEV_NOT_SUPPORTED(ct);
36077c478bd9Sstevel@tonic-gate 	}
36087c478bd9Sstevel@tonic-gate 	if (rv != MDI_SUCCESS) {
36094c06356bSdh142964 		MDI_DEBUG(2, (MDI_WARN, ct->ct_dip,
36104c06356bSdh142964 		    "vo_pi_state_change failed: rv %x", rv));
36117c478bd9Sstevel@tonic-gate 	}
36127c478bd9Sstevel@tonic-gate 	if (MDI_PI_IS_TRANSIENT(pip)) {
36137c478bd9Sstevel@tonic-gate 		if (rv == MDI_SUCCESS) {
36147c478bd9Sstevel@tonic-gate 			MDI_PI_CLEAR_TRANSIENT(pip);
36157c478bd9Sstevel@tonic-gate 		} else {
36167c478bd9Sstevel@tonic-gate 			MDI_PI(pip)->pi_state = MDI_PI_OLD_STATE(pip);
36177c478bd9Sstevel@tonic-gate 		}
36187c478bd9Sstevel@tonic-gate 	}
36197c478bd9Sstevel@tonic-gate 
36207c478bd9Sstevel@tonic-gate 	/*
36217c478bd9Sstevel@tonic-gate 	 * Wake anyone waiting for this mdi_pathinfo node
36227c478bd9Sstevel@tonic-gate 	 */
36237c478bd9Sstevel@tonic-gate 	cv_broadcast(&MDI_PI(pip)->pi_state_cv);
36247c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
36257c478bd9Sstevel@tonic-gate 
36267c478bd9Sstevel@tonic-gate 	/*
36277c478bd9Sstevel@tonic-gate 	 * Mark the client device as stable
36287c478bd9Sstevel@tonic-gate 	 */
36297c478bd9Sstevel@tonic-gate 	MDI_CLIENT_STABLE(ct);
36307c478bd9Sstevel@tonic-gate 	if (rv == MDI_SUCCESS) {
36317c478bd9Sstevel@tonic-gate 		if (ct->ct_unstable == 0) {
36327c478bd9Sstevel@tonic-gate 			cdip = ct->ct_dip;
36337c478bd9Sstevel@tonic-gate 
36347c478bd9Sstevel@tonic-gate 			/*
36357c478bd9Sstevel@tonic-gate 			 * Onlining the mdi_pathinfo node will impact the
36367c478bd9Sstevel@tonic-gate 			 * client state Update the client and dev_info node
36377c478bd9Sstevel@tonic-gate 			 * state accordingly
36387c478bd9Sstevel@tonic-gate 			 */
36397c478bd9Sstevel@tonic-gate 			rv = NDI_SUCCESS;
36407c478bd9Sstevel@tonic-gate 			i_mdi_client_update_state(ct);
36417c478bd9Sstevel@tonic-gate 			switch (MDI_CLIENT_STATE(ct)) {
36427c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_OPTIMAL:
36437c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_DEGRADED:
3644737d277aScth 				if (cdip && !i_ddi_devi_attached(cdip) &&
36457c478bd9Sstevel@tonic-gate 				    ((state == MDI_PATHINFO_STATE_ONLINE) ||
36467c478bd9Sstevel@tonic-gate 				    (state == MDI_PATHINFO_STATE_STANDBY))) {
36477c478bd9Sstevel@tonic-gate 
36487c478bd9Sstevel@tonic-gate 					/*
36497c478bd9Sstevel@tonic-gate 					 * Must do ndi_devi_online() through
36507c478bd9Sstevel@tonic-gate 					 * hotplug thread for deferred
36517c478bd9Sstevel@tonic-gate 					 * attach mechanism to work
36527c478bd9Sstevel@tonic-gate 					 */
36535e3986cbScth 					MDI_CLIENT_UNLOCK(ct);
36547c478bd9Sstevel@tonic-gate 					rv = ndi_devi_online(cdip, 0);
36555e3986cbScth 					MDI_CLIENT_LOCK(ct);
36567c478bd9Sstevel@tonic-gate 					if ((rv != NDI_SUCCESS) &&
36577c478bd9Sstevel@tonic-gate 					    (MDI_CLIENT_STATE(ct) ==
36587c478bd9Sstevel@tonic-gate 					    MDI_CLIENT_STATE_DEGRADED)) {
36597c478bd9Sstevel@tonic-gate 						/*
36607c478bd9Sstevel@tonic-gate 						 * ndi_devi_online failed.
36617c478bd9Sstevel@tonic-gate 						 * Reset client flags to
36627c478bd9Sstevel@tonic-gate 						 * offline.
36637c478bd9Sstevel@tonic-gate 						 */
36644c06356bSdh142964 						MDI_DEBUG(1, (MDI_WARN, cdip,
36654c06356bSdh142964 						    "!ndi_devi_online failed "
36664c06356bSdh142964 						    "error %x", rv));
36677c478bd9Sstevel@tonic-gate 						MDI_CLIENT_SET_OFFLINE(ct);
36687c478bd9Sstevel@tonic-gate 					}
36697c478bd9Sstevel@tonic-gate 					if (rv != NDI_SUCCESS) {
36707c478bd9Sstevel@tonic-gate 						/* Reset the path state */
36717c478bd9Sstevel@tonic-gate 						MDI_PI_LOCK(pip);
36727c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_state =
36737c478bd9Sstevel@tonic-gate 						    MDI_PI_OLD_STATE(pip);
36747c478bd9Sstevel@tonic-gate 						MDI_PI_UNLOCK(pip);
36757c478bd9Sstevel@tonic-gate 					}
36767c478bd9Sstevel@tonic-gate 				}
36777c478bd9Sstevel@tonic-gate 				break;
36787c478bd9Sstevel@tonic-gate 
36797c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_FAILED:
36807c478bd9Sstevel@tonic-gate 				/*
36817c478bd9Sstevel@tonic-gate 				 * This is the last path case for
36827c478bd9Sstevel@tonic-gate 				 * non-user initiated events.
36837c478bd9Sstevel@tonic-gate 				 */
36844c06356bSdh142964 				if (((flag & NDI_USER_REQ) == 0) &&
36857c478bd9Sstevel@tonic-gate 				    cdip && (i_ddi_node_state(cdip) >=
36867c478bd9Sstevel@tonic-gate 				    DS_INITIALIZED)) {
36875e3986cbScth 					MDI_CLIENT_UNLOCK(ct);
36884c06356bSdh142964 					rv = ndi_devi_offline(cdip,
36894c06356bSdh142964 					    NDI_DEVFS_CLEAN);
36905e3986cbScth 					MDI_CLIENT_LOCK(ct);
36917c478bd9Sstevel@tonic-gate 
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 as the path could not
36977c478bd9Sstevel@tonic-gate 						 * be offlined.
36987c478bd9Sstevel@tonic-gate 						 */
36994c06356bSdh142964 						MDI_DEBUG(1, (MDI_WARN, cdip,
37004c06356bSdh142964 						    "!ndi_devi_offline failed: "
37014c06356bSdh142964 						    "error %x", rv));
37027c478bd9Sstevel@tonic-gate 						MDI_CLIENT_SET_ONLINE(ct);
37037c478bd9Sstevel@tonic-gate 					}
37047c478bd9Sstevel@tonic-gate 				}
37057c478bd9Sstevel@tonic-gate 				break;
37067c478bd9Sstevel@tonic-gate 			}
37077c478bd9Sstevel@tonic-gate 			/*
37087c478bd9Sstevel@tonic-gate 			 * Convert to MDI error code
37097c478bd9Sstevel@tonic-gate 			 */
37107c478bd9Sstevel@tonic-gate 			switch (rv) {
37117c478bd9Sstevel@tonic-gate 			case NDI_SUCCESS:
37127c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_REPORT_DEV_NEEDED(ct);
37137c478bd9Sstevel@tonic-gate 				i_mdi_report_path_state(ct, pip);
37147c478bd9Sstevel@tonic-gate 				rv = MDI_SUCCESS;
37157c478bd9Sstevel@tonic-gate 				break;
37167c478bd9Sstevel@tonic-gate 			case NDI_BUSY:
37177c478bd9Sstevel@tonic-gate 				rv = MDI_BUSY;
37187c478bd9Sstevel@tonic-gate 				break;
37197c478bd9Sstevel@tonic-gate 			default:
37207c478bd9Sstevel@tonic-gate 				rv = MDI_FAILURE;
37217c478bd9Sstevel@tonic-gate 				break;
37227c478bd9Sstevel@tonic-gate 			}
37237c478bd9Sstevel@tonic-gate 		}
37247c478bd9Sstevel@tonic-gate 	}
37257c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate state_change_exit:
37287c478bd9Sstevel@tonic-gate 	/*
37297c478bd9Sstevel@tonic-gate 	 * Mark the pHCI as stable again.
37307c478bd9Sstevel@tonic-gate 	 */
37317c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
37327c478bd9Sstevel@tonic-gate 	MDI_PHCI_STABLE(ph);
37337c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
37347c478bd9Sstevel@tonic-gate 	return (rv);
37357c478bd9Sstevel@tonic-gate }
37367c478bd9Sstevel@tonic-gate 
37377c478bd9Sstevel@tonic-gate /*
37387c478bd9Sstevel@tonic-gate  * mdi_pi_online():
37397c478bd9Sstevel@tonic-gate  *		Place the path_info node in the online state.  The path is
37407c478bd9Sstevel@tonic-gate  *		now available to be selected by mdi_select_path() for
37417c478bd9Sstevel@tonic-gate  *		transporting I/O requests to client devices.
37427c478bd9Sstevel@tonic-gate  * Return Values:
37437c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
37447c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
37457c478bd9Sstevel@tonic-gate  */
37467c478bd9Sstevel@tonic-gate int
37477c478bd9Sstevel@tonic-gate mdi_pi_online(mdi_pathinfo_t *pip, int flags)
37487c478bd9Sstevel@tonic-gate {
37497c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct = MDI_PI(pip)->pi_client;
37507c478bd9Sstevel@tonic-gate 	int		client_held = 0;
37517c478bd9Sstevel@tonic-gate 	int		rv;
37527c478bd9Sstevel@tonic-gate 
37537c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
37547c478bd9Sstevel@tonic-gate 	rv = i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_ONLINE, flags);
37557c478bd9Sstevel@tonic-gate 	if (rv != MDI_SUCCESS)
37567c478bd9Sstevel@tonic-gate 		return (rv);
37577c478bd9Sstevel@tonic-gate 
37587c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
37597c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held == 0) {
37604c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
37614c06356bSdh142964 		    "i_mdi_pm_hold_pip %p", (void *)pip));
37627c478bd9Sstevel@tonic-gate 		i_mdi_pm_hold_pip(pip);
37637c478bd9Sstevel@tonic-gate 		client_held = 1;
37647c478bd9Sstevel@tonic-gate 	}
37657c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
37667c478bd9Sstevel@tonic-gate 
37677c478bd9Sstevel@tonic-gate 	if (client_held) {
37687c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
37697c478bd9Sstevel@tonic-gate 		if (ct->ct_power_cnt == 0) {
37707c478bd9Sstevel@tonic-gate 			rv = i_mdi_power_all_phci(ct);
37717c478bd9Sstevel@tonic-gate 		}
37727c478bd9Sstevel@tonic-gate 
37734c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
37744c06356bSdh142964 		    "i_mdi_pm_hold_client %p", (void *)ct));
37757c478bd9Sstevel@tonic-gate 		i_mdi_pm_hold_client(ct, 1);
37767c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
37777c478bd9Sstevel@tonic-gate 	}
37787c478bd9Sstevel@tonic-gate 
37797c478bd9Sstevel@tonic-gate 	return (rv);
37807c478bd9Sstevel@tonic-gate }
37817c478bd9Sstevel@tonic-gate 
37827c478bd9Sstevel@tonic-gate /*
37837c478bd9Sstevel@tonic-gate  * mdi_pi_standby():
37847c478bd9Sstevel@tonic-gate  *		Place the mdi_pathinfo node in standby state
37857c478bd9Sstevel@tonic-gate  *
37867c478bd9Sstevel@tonic-gate  * Return Values:
37877c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
37887c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
37897c478bd9Sstevel@tonic-gate  */
37907c478bd9Sstevel@tonic-gate int
37917c478bd9Sstevel@tonic-gate mdi_pi_standby(mdi_pathinfo_t *pip, int flags)
37927c478bd9Sstevel@tonic-gate {
37937c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_STANDBY, flags));
37947c478bd9Sstevel@tonic-gate }
37957c478bd9Sstevel@tonic-gate 
37967c478bd9Sstevel@tonic-gate /*
37977c478bd9Sstevel@tonic-gate  * mdi_pi_fault():
37987c478bd9Sstevel@tonic-gate  *		Place the mdi_pathinfo node in fault'ed state
37997c478bd9Sstevel@tonic-gate  * Return Values:
38007c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
38017c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
38027c478bd9Sstevel@tonic-gate  */
38037c478bd9Sstevel@tonic-gate int
38047c478bd9Sstevel@tonic-gate mdi_pi_fault(mdi_pathinfo_t *pip, int flags)
38057c478bd9Sstevel@tonic-gate {
38067c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_FAULT, flags));
38077c478bd9Sstevel@tonic-gate }
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate /*
38107c478bd9Sstevel@tonic-gate  * mdi_pi_offline():
38117c478bd9Sstevel@tonic-gate  *		Offline a mdi_pathinfo node.
38127c478bd9Sstevel@tonic-gate  * Return Values:
38137c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
38147c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
38157c478bd9Sstevel@tonic-gate  */
38167c478bd9Sstevel@tonic-gate int
38177c478bd9Sstevel@tonic-gate mdi_pi_offline(mdi_pathinfo_t *pip, int flags)
38187c478bd9Sstevel@tonic-gate {
38197c478bd9Sstevel@tonic-gate 	int	ret, client_held = 0;
38207c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
38214c06356bSdh142964 
38224c06356bSdh142964 	/*
38234c06356bSdh142964 	 * Original code overloaded NDI_DEVI_REMOVE to this interface, and
38244c06356bSdh142964 	 * used it to mean "user initiated operation" (i.e. devctl). Callers
38254c06356bSdh142964 	 * should now just use NDI_USER_REQ.
38264c06356bSdh142964 	 */
38274c06356bSdh142964 	if (flags & NDI_DEVI_REMOVE) {
38284c06356bSdh142964 		flags &= ~NDI_DEVI_REMOVE;
38294c06356bSdh142964 		flags |= NDI_USER_REQ;
38304c06356bSdh142964 	}
38317c478bd9Sstevel@tonic-gate 
38327c478bd9Sstevel@tonic-gate 	ret = i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_OFFLINE, flags);
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate 	if (ret == MDI_SUCCESS) {
38357c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
38367c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_pm_held) {
38377c478bd9Sstevel@tonic-gate 			client_held = 1;
38387c478bd9Sstevel@tonic-gate 		}
38397c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
38407c478bd9Sstevel@tonic-gate 
38417c478bd9Sstevel@tonic-gate 		if (client_held) {
38427c478bd9Sstevel@tonic-gate 			ct = MDI_PI(pip)->pi_client;
38437c478bd9Sstevel@tonic-gate 			MDI_CLIENT_LOCK(ct);
38444c06356bSdh142964 			MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
38454c06356bSdh142964 			    "i_mdi_pm_rele_client\n"));
38467c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, 1);
38477c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
38487c478bd9Sstevel@tonic-gate 		}
38497c478bd9Sstevel@tonic-gate 	}
38507c478bd9Sstevel@tonic-gate 
38517c478bd9Sstevel@tonic-gate 	return (ret);
38527c478bd9Sstevel@tonic-gate }
38537c478bd9Sstevel@tonic-gate 
38547c478bd9Sstevel@tonic-gate /*
38557c478bd9Sstevel@tonic-gate  * i_mdi_pi_offline():
38567c478bd9Sstevel@tonic-gate  *		Offline a mdi_pathinfo node and call the vHCI driver's callback
38577c478bd9Sstevel@tonic-gate  */
38587c478bd9Sstevel@tonic-gate static int
38597c478bd9Sstevel@tonic-gate i_mdi_pi_offline(mdi_pathinfo_t *pip, int flags)
38607c478bd9Sstevel@tonic-gate {
38617c478bd9Sstevel@tonic-gate 	dev_info_t	*vdip = NULL;
38627c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
38637c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct = NULL;
38647c478bd9Sstevel@tonic-gate 	int		(*f)();
38657c478bd9Sstevel@tonic-gate 	int		rv;
38667c478bd9Sstevel@tonic-gate 
38677c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
38687c478bd9Sstevel@tonic-gate 	ct = MDI_PI(pip)->pi_client;
38697c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
38707c478bd9Sstevel@tonic-gate 
38717c478bd9Sstevel@tonic-gate 	while (MDI_PI(pip)->pi_ref_cnt != 0) {
38727c478bd9Sstevel@tonic-gate 		/*
38737c478bd9Sstevel@tonic-gate 		 * Give a chance for pending I/Os to complete.
38747c478bd9Sstevel@tonic-gate 		 */
38754c06356bSdh142964 		MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
38764c06356bSdh142964 		    "!%d cmds still pending on path %s %p",
38774c06356bSdh142964 		    MDI_PI(pip)->pi_ref_cnt, mdi_pi_spathname(pip),
38784c06356bSdh142964 		    (void *)pip));
3879d3d50737SRafael Vanoni 		if (cv_reltimedwait(&MDI_PI(pip)->pi_ref_cv,
3880d3d50737SRafael Vanoni 		    &MDI_PI(pip)->pi_mutex, drv_usectohz(60 * 1000000),
3881d3d50737SRafael Vanoni 		    TR_CLOCK_TICK) == -1) {
38827c478bd9Sstevel@tonic-gate 			/*
38837c478bd9Sstevel@tonic-gate 			 * The timeout time reached without ref_cnt being zero
38847c478bd9Sstevel@tonic-gate 			 * being signaled.
38857c478bd9Sstevel@tonic-gate 			 */
38864c06356bSdh142964 			MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
38874c06356bSdh142964 			    "!Timeout reached on path %s %p without the cond",
38884c06356bSdh142964 			    mdi_pi_spathname(pip), (void *)pip));
38894c06356bSdh142964 			MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
38904c06356bSdh142964 			    "!%d cmds still pending on path %s %p",
38914c06356bSdh142964 			    MDI_PI(pip)->pi_ref_cnt,
38924c06356bSdh142964 			    mdi_pi_spathname(pip), (void *)pip));
38937c478bd9Sstevel@tonic-gate 		}
38947c478bd9Sstevel@tonic-gate 	}
38957c478bd9Sstevel@tonic-gate 	vh = ct->ct_vhci;
38967c478bd9Sstevel@tonic-gate 	vdip = vh->vh_dip;
38977c478bd9Sstevel@tonic-gate 
38987c478bd9Sstevel@tonic-gate 	/*
38997c478bd9Sstevel@tonic-gate 	 * Notify vHCI that has registered this event
39007c478bd9Sstevel@tonic-gate 	 */
39017c478bd9Sstevel@tonic-gate 	ASSERT(vh->vh_ops);
39027c478bd9Sstevel@tonic-gate 	f = vh->vh_ops->vo_pi_state_change;
39037c478bd9Sstevel@tonic-gate 
39047c478bd9Sstevel@tonic-gate 	if (f != NULL) {
39057c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
39067c478bd9Sstevel@tonic-gate 		if ((rv = (*f)(vdip, pip, MDI_PATHINFO_STATE_OFFLINE, 0,
39077c478bd9Sstevel@tonic-gate 		    flags)) != MDI_SUCCESS) {
39084c06356bSdh142964 			MDI_DEBUG(1, (MDI_WARN, ct->ct_dip,
39094c06356bSdh142964 			    "!vo_path_offline failed: vdip %s%d %p: path %s %p",
39104c06356bSdh142964 			    ddi_driver_name(vdip), ddi_get_instance(vdip),
39114c06356bSdh142964 			    (void *)vdip, mdi_pi_spathname(pip), (void *)pip));
39127c478bd9Sstevel@tonic-gate 		}
39137c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
39147c478bd9Sstevel@tonic-gate 	}
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate 	/*
39177c478bd9Sstevel@tonic-gate 	 * Set the mdi_pathinfo node state and clear the transient condition
39187c478bd9Sstevel@tonic-gate 	 */
39197c478bd9Sstevel@tonic-gate 	MDI_PI_SET_OFFLINE(pip);
39207c478bd9Sstevel@tonic-gate 	cv_broadcast(&MDI_PI(pip)->pi_state_cv);
39217c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
39227c478bd9Sstevel@tonic-gate 
39237c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
39247c478bd9Sstevel@tonic-gate 	if (rv == MDI_SUCCESS) {
39257c478bd9Sstevel@tonic-gate 		if (ct->ct_unstable == 0) {
39267c478bd9Sstevel@tonic-gate 			dev_info_t	*cdip = ct->ct_dip;
39277c478bd9Sstevel@tonic-gate 
39287c478bd9Sstevel@tonic-gate 			/*
39297c478bd9Sstevel@tonic-gate 			 * Onlining the mdi_pathinfo node will impact the
39307c478bd9Sstevel@tonic-gate 			 * client state Update the client and dev_info node
39317c478bd9Sstevel@tonic-gate 			 * state accordingly
39327c478bd9Sstevel@tonic-gate 			 */
39337c478bd9Sstevel@tonic-gate 			i_mdi_client_update_state(ct);
39347c478bd9Sstevel@tonic-gate 			rv = NDI_SUCCESS;
39357c478bd9Sstevel@tonic-gate 			if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_FAILED) {
39367c478bd9Sstevel@tonic-gate 				if (cdip &&
39377c478bd9Sstevel@tonic-gate 				    (i_ddi_node_state(cdip) >=
39387c478bd9Sstevel@tonic-gate 				    DS_INITIALIZED)) {
39397c478bd9Sstevel@tonic-gate 					MDI_CLIENT_UNLOCK(ct);
39404c06356bSdh142964 					rv = ndi_devi_offline(cdip,
39414c06356bSdh142964 					    NDI_DEVFS_CLEAN);
39427c478bd9Sstevel@tonic-gate 					MDI_CLIENT_LOCK(ct);
39437c478bd9Sstevel@tonic-gate 					if (rv != NDI_SUCCESS) {
39447c478bd9Sstevel@tonic-gate 						/*
39457c478bd9Sstevel@tonic-gate 						 * ndi_devi_offline failed.
39467c478bd9Sstevel@tonic-gate 						 * Reset client flags to
39477c478bd9Sstevel@tonic-gate 						 * online.
39487c478bd9Sstevel@tonic-gate 						 */
39494c06356bSdh142964 						MDI_DEBUG(4, (MDI_WARN, cdip,
39504c06356bSdh142964 						    "ndi_devi_offline failed: "
39514c06356bSdh142964 						    "error %x", rv));
39527c478bd9Sstevel@tonic-gate 						MDI_CLIENT_SET_ONLINE(ct);
39537c478bd9Sstevel@tonic-gate 					}
39547c478bd9Sstevel@tonic-gate 				}
39557c478bd9Sstevel@tonic-gate 			}
39567c478bd9Sstevel@tonic-gate 			/*
39577c478bd9Sstevel@tonic-gate 			 * Convert to MDI error code
39587c478bd9Sstevel@tonic-gate 			 */
39597c478bd9Sstevel@tonic-gate 			switch (rv) {
39607c478bd9Sstevel@tonic-gate 			case NDI_SUCCESS:
39617c478bd9Sstevel@tonic-gate 				rv = MDI_SUCCESS;
39627c478bd9Sstevel@tonic-gate 				break;
39637c478bd9Sstevel@tonic-gate 			case NDI_BUSY:
39647c478bd9Sstevel@tonic-gate 				rv = MDI_BUSY;
39657c478bd9Sstevel@tonic-gate 				break;
39667c478bd9Sstevel@tonic-gate 			default:
39677c478bd9Sstevel@tonic-gate 				rv = MDI_FAILURE;
39687c478bd9Sstevel@tonic-gate 				break;
39697c478bd9Sstevel@tonic-gate 			}
39707c478bd9Sstevel@tonic-gate 		}
39717c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_REPORT_DEV_NEEDED(ct);
39727c478bd9Sstevel@tonic-gate 		i_mdi_report_path_state(ct, pip);
39737c478bd9Sstevel@tonic-gate 	}
39747c478bd9Sstevel@tonic-gate 
39757c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
39767c478bd9Sstevel@tonic-gate 
39777c478bd9Sstevel@tonic-gate 	/*
39787c478bd9Sstevel@tonic-gate 	 * Change in the mdi_pathinfo node state will impact the client state
39797c478bd9Sstevel@tonic-gate 	 */
39804c06356bSdh142964 	MDI_DEBUG(2, (MDI_NOTE, ct->ct_dip,
39814c06356bSdh142964 	    "ct = %p pip = %p", (void *)ct, (void *)pip));
39827c478bd9Sstevel@tonic-gate 	return (rv);
39837c478bd9Sstevel@tonic-gate }
39847c478bd9Sstevel@tonic-gate 
398555e592a2SRandall Ralphs /*
398655e592a2SRandall Ralphs  * mdi_pi_get_node_name():
398755e592a2SRandall Ralphs  *              Get the name associated with a mdi_pathinfo node.
398855e592a2SRandall Ralphs  *              Since pathinfo nodes are not directly named, we
398955e592a2SRandall Ralphs  *              return the node_name of the client.
399055e592a2SRandall Ralphs  *
399155e592a2SRandall Ralphs  * Return Values:
399255e592a2SRandall Ralphs  *              char *
399355e592a2SRandall Ralphs  */
399455e592a2SRandall Ralphs char *
399555e592a2SRandall Ralphs mdi_pi_get_node_name(mdi_pathinfo_t *pip)
399655e592a2SRandall Ralphs {
399755e592a2SRandall Ralphs 	mdi_client_t    *ct;
399855e592a2SRandall Ralphs 
399955e592a2SRandall Ralphs 	if (pip == NULL)
400055e592a2SRandall Ralphs 		return (NULL);
400155e592a2SRandall Ralphs 	ct = MDI_PI(pip)->pi_client;
400255e592a2SRandall Ralphs 	if ((ct == NULL) || (ct->ct_dip == NULL))
400355e592a2SRandall Ralphs 		return (NULL);
400455e592a2SRandall Ralphs 	return (ddi_node_name(ct->ct_dip));
400555e592a2SRandall Ralphs }
40067c478bd9Sstevel@tonic-gate 
40077c478bd9Sstevel@tonic-gate /*
40087c478bd9Sstevel@tonic-gate  * mdi_pi_get_addr():
40097c478bd9Sstevel@tonic-gate  *		Get the unit address associated with a mdi_pathinfo node
40107c478bd9Sstevel@tonic-gate  *
40117c478bd9Sstevel@tonic-gate  * Return Values:
40127c478bd9Sstevel@tonic-gate  *		char *
40137c478bd9Sstevel@tonic-gate  */
40147c478bd9Sstevel@tonic-gate char *
40157c478bd9Sstevel@tonic-gate mdi_pi_get_addr(mdi_pathinfo_t *pip)
40167c478bd9Sstevel@tonic-gate {
40177c478bd9Sstevel@tonic-gate 	if (pip == NULL)
40187c478bd9Sstevel@tonic-gate 		return (NULL);
40197c478bd9Sstevel@tonic-gate 
402072a50065Scth 	return (MDI_PI(pip)->pi_addr);
40217c478bd9Sstevel@tonic-gate }
40227c478bd9Sstevel@tonic-gate 
40237c478bd9Sstevel@tonic-gate /*
4024602ca9eaScth  * mdi_pi_get_path_instance():
4025602ca9eaScth  *		Get the 'path_instance' of a mdi_pathinfo node
4026602ca9eaScth  *
4027602ca9eaScth  * Return Values:
4028602ca9eaScth  *		path_instance
4029602ca9eaScth  */
4030602ca9eaScth int
4031602ca9eaScth mdi_pi_get_path_instance(mdi_pathinfo_t *pip)
4032602ca9eaScth {
4033602ca9eaScth 	if (pip == NULL)
4034602ca9eaScth 		return (0);
4035602ca9eaScth 
4036602ca9eaScth 	return (MDI_PI(pip)->pi_path_instance);
4037602ca9eaScth }
4038602ca9eaScth 
4039602ca9eaScth /*
4040602ca9eaScth  * mdi_pi_pathname():
4041602ca9eaScth  *		Return pointer to path to pathinfo node.
4042602ca9eaScth  */
4043602ca9eaScth char *
4044602ca9eaScth mdi_pi_pathname(mdi_pathinfo_t *pip)
4045602ca9eaScth {
4046602ca9eaScth 	if (pip == NULL)
4047602ca9eaScth 		return (NULL);
4048602ca9eaScth 	return (mdi_pi_pathname_by_instance(mdi_pi_get_path_instance(pip)));
4049602ca9eaScth }
4050602ca9eaScth 
40514c06356bSdh142964 /*
40524c06356bSdh142964  * mdi_pi_spathname():
40534c06356bSdh142964  *		Return pointer to shortpath to pathinfo node. Used for debug
40544c06356bSdh142964  *		messages, so return "" instead of NULL when unknown.
40554c06356bSdh142964  */
40564c06356bSdh142964 char *
40574c06356bSdh142964 mdi_pi_spathname(mdi_pathinfo_t *pip)
40584c06356bSdh142964 {
40594c06356bSdh142964 	char	*spath = "";
40604c06356bSdh142964 
40614c06356bSdh142964 	if (pip) {
40624c06356bSdh142964 		spath = mdi_pi_spathname_by_instance(
40634c06356bSdh142964 		    mdi_pi_get_path_instance(pip));
40644c06356bSdh142964 		if (spath == NULL)
40654c06356bSdh142964 			spath = "";
40664c06356bSdh142964 	}
40674c06356bSdh142964 	return (spath);
40684c06356bSdh142964 }
40694c06356bSdh142964 
407038c67cbdSjiang wu - Sun Microsystems - Beijing China char *
407138c67cbdSjiang wu - Sun Microsystems - Beijing China mdi_pi_pathname_obp(mdi_pathinfo_t *pip, char *path)
407238c67cbdSjiang wu - Sun Microsystems - Beijing China {
407338c67cbdSjiang wu - Sun Microsystems - Beijing China 	char *obp_path = NULL;
407438c67cbdSjiang wu - Sun Microsystems - Beijing China 	if ((pip == NULL) || (path == NULL))
407538c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (NULL);
407638c67cbdSjiang wu - Sun Microsystems - Beijing China 
407738c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (mdi_prop_lookup_string(pip, "obp-path", &obp_path) == MDI_SUCCESS) {
407838c67cbdSjiang wu - Sun Microsystems - Beijing China 		(void) strcpy(path, obp_path);
407938c67cbdSjiang wu - Sun Microsystems - Beijing China 		(void) mdi_prop_free(obp_path);
408038c67cbdSjiang wu - Sun Microsystems - Beijing China 	} else {
408138c67cbdSjiang wu - Sun Microsystems - Beijing China 		path = NULL;
408238c67cbdSjiang wu - Sun Microsystems - Beijing China 	}
408338c67cbdSjiang wu - Sun Microsystems - Beijing China 	return (path);
408438c67cbdSjiang wu - Sun Microsystems - Beijing China }
408538c67cbdSjiang wu - Sun Microsystems - Beijing China 
408638c67cbdSjiang wu - Sun Microsystems - Beijing China int
408738c67cbdSjiang wu - Sun Microsystems - Beijing China mdi_pi_pathname_obp_set(mdi_pathinfo_t *pip, char *component)
408838c67cbdSjiang wu - Sun Microsystems - Beijing China {
408938c67cbdSjiang wu - Sun Microsystems - Beijing China 	dev_info_t *pdip;
4090caa9369fSjiang wu - Sun Microsystems - Beijing China 	char *obp_path = NULL;
4091caa9369fSjiang wu - Sun Microsystems - Beijing China 	int rc = MDI_FAILURE;
409238c67cbdSjiang wu - Sun Microsystems - Beijing China 
409338c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (pip == NULL)
409438c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (MDI_FAILURE);
409538c67cbdSjiang wu - Sun Microsystems - Beijing China 
409638c67cbdSjiang wu - Sun Microsystems - Beijing China 	pdip = mdi_pi_get_phci(pip);
409738c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (pdip == NULL)
409838c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (MDI_FAILURE);
409938c67cbdSjiang wu - Sun Microsystems - Beijing China 
4100caa9369fSjiang wu - Sun Microsystems - Beijing China 	obp_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
4101caa9369fSjiang wu - Sun Microsystems - Beijing China 
410238c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (ddi_pathname_obp(pdip, obp_path) == NULL) {
410338c67cbdSjiang wu - Sun Microsystems - Beijing China 		(void) ddi_pathname(pdip, obp_path);
410438c67cbdSjiang wu - Sun Microsystems - Beijing China 	}
410538c67cbdSjiang wu - Sun Microsystems - Beijing China 
410638c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (component) {
4107caa9369fSjiang wu - Sun Microsystems - Beijing China 		(void) strncat(obp_path, "/", MAXPATHLEN);
4108caa9369fSjiang wu - Sun Microsystems - Beijing China 		(void) strncat(obp_path, component, MAXPATHLEN);
410938c67cbdSjiang wu - Sun Microsystems - Beijing China 	}
4110caa9369fSjiang wu - Sun Microsystems - Beijing China 	rc = mdi_prop_update_string(pip, "obp-path", obp_path);
411138c67cbdSjiang wu - Sun Microsystems - Beijing China 
4112caa9369fSjiang wu - Sun Microsystems - Beijing China 	if (obp_path)
4113caa9369fSjiang wu - Sun Microsystems - Beijing China 		kmem_free(obp_path, MAXPATHLEN);
4114caa9369fSjiang wu - Sun Microsystems - Beijing China 	return (rc);
411538c67cbdSjiang wu - Sun Microsystems - Beijing China }
411638c67cbdSjiang wu - Sun Microsystems - Beijing China 
4117602ca9eaScth /*
41187c478bd9Sstevel@tonic-gate  * mdi_pi_get_client():
41197c478bd9Sstevel@tonic-gate  *		Get the client devinfo associated with a mdi_pathinfo node
41207c478bd9Sstevel@tonic-gate  *
41217c478bd9Sstevel@tonic-gate  * Return Values:
41227c478bd9Sstevel@tonic-gate  *		Handle to client device dev_info node
41237c478bd9Sstevel@tonic-gate  */
41247c478bd9Sstevel@tonic-gate dev_info_t *
41257c478bd9Sstevel@tonic-gate mdi_pi_get_client(mdi_pathinfo_t *pip)
41267c478bd9Sstevel@tonic-gate {
41277c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
41287c478bd9Sstevel@tonic-gate 	if (pip) {
41297c478bd9Sstevel@tonic-gate 		dip = MDI_PI(pip)->pi_client->ct_dip;
41307c478bd9Sstevel@tonic-gate 	}
41317c478bd9Sstevel@tonic-gate 	return (dip);
41327c478bd9Sstevel@tonic-gate }
41337c478bd9Sstevel@tonic-gate 
41347c478bd9Sstevel@tonic-gate /*
41357c478bd9Sstevel@tonic-gate  * mdi_pi_get_phci():
41367c478bd9Sstevel@tonic-gate  *		Get the pHCI devinfo associated with the mdi_pathinfo node
41377c478bd9Sstevel@tonic-gate  * Return Values:
41387c478bd9Sstevel@tonic-gate  *		Handle to dev_info node
41397c478bd9Sstevel@tonic-gate  */
41407c478bd9Sstevel@tonic-gate dev_info_t *
41417c478bd9Sstevel@tonic-gate mdi_pi_get_phci(mdi_pathinfo_t *pip)
41427c478bd9Sstevel@tonic-gate {
41437c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
41444c06356bSdh142964 	mdi_phci_t	*ph;
41454c06356bSdh142964 
41467c478bd9Sstevel@tonic-gate 	if (pip) {
41474c06356bSdh142964 		ph = MDI_PI(pip)->pi_phci;
41484c06356bSdh142964 		if (ph)
41494c06356bSdh142964 			dip = ph->ph_dip;
41507c478bd9Sstevel@tonic-gate 	}
41517c478bd9Sstevel@tonic-gate 	return (dip);
41527c478bd9Sstevel@tonic-gate }
41537c478bd9Sstevel@tonic-gate 
41547c478bd9Sstevel@tonic-gate /*
41557c478bd9Sstevel@tonic-gate  * mdi_pi_get_client_private():
41567c478bd9Sstevel@tonic-gate  *		Get the client private information associated with the
41577c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node
41587c478bd9Sstevel@tonic-gate  */
41597c478bd9Sstevel@tonic-gate void *
41607c478bd9Sstevel@tonic-gate mdi_pi_get_client_private(mdi_pathinfo_t *pip)
41617c478bd9Sstevel@tonic-gate {
41627c478bd9Sstevel@tonic-gate 	void *cprivate = NULL;
41637c478bd9Sstevel@tonic-gate 	if (pip) {
41647c478bd9Sstevel@tonic-gate 		cprivate = MDI_PI(pip)->pi_cprivate;
41657c478bd9Sstevel@tonic-gate 	}
41667c478bd9Sstevel@tonic-gate 	return (cprivate);
41677c478bd9Sstevel@tonic-gate }
41687c478bd9Sstevel@tonic-gate 
41697c478bd9Sstevel@tonic-gate /*
41707c478bd9Sstevel@tonic-gate  * mdi_pi_set_client_private():
41717c478bd9Sstevel@tonic-gate  *		Set the client private information in the mdi_pathinfo node
41727c478bd9Sstevel@tonic-gate  */
41737c478bd9Sstevel@tonic-gate void
41747c478bd9Sstevel@tonic-gate mdi_pi_set_client_private(mdi_pathinfo_t *pip, void *priv)
41757c478bd9Sstevel@tonic-gate {
41767c478bd9Sstevel@tonic-gate 	if (pip) {
41777c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_cprivate = priv;
41787c478bd9Sstevel@tonic-gate 	}
41797c478bd9Sstevel@tonic-gate }
41807c478bd9Sstevel@tonic-gate 
41817c478bd9Sstevel@tonic-gate /*
41827c478bd9Sstevel@tonic-gate  * mdi_pi_get_phci_private():
41837c478bd9Sstevel@tonic-gate  *		Get the pHCI private information associated with the
41847c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node
41857c478bd9Sstevel@tonic-gate  */
41867c478bd9Sstevel@tonic-gate caddr_t
41877c478bd9Sstevel@tonic-gate mdi_pi_get_phci_private(mdi_pathinfo_t *pip)
41887c478bd9Sstevel@tonic-gate {
41897c478bd9Sstevel@tonic-gate 	caddr_t	pprivate = NULL;
41904c06356bSdh142964 
41917c478bd9Sstevel@tonic-gate 	if (pip) {
41927c478bd9Sstevel@tonic-gate 		pprivate = MDI_PI(pip)->pi_pprivate;
41937c478bd9Sstevel@tonic-gate 	}
41947c478bd9Sstevel@tonic-gate 	return (pprivate);
41957c478bd9Sstevel@tonic-gate }
41967c478bd9Sstevel@tonic-gate 
41977c478bd9Sstevel@tonic-gate /*
41987c478bd9Sstevel@tonic-gate  * mdi_pi_set_phci_private():
41997c478bd9Sstevel@tonic-gate  *		Set the pHCI private information in the mdi_pathinfo node
42007c478bd9Sstevel@tonic-gate  */
42017c478bd9Sstevel@tonic-gate void
42027c478bd9Sstevel@tonic-gate mdi_pi_set_phci_private(mdi_pathinfo_t *pip, caddr_t priv)
42037c478bd9Sstevel@tonic-gate {
42047c478bd9Sstevel@tonic-gate 	if (pip) {
42057c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_pprivate = priv;
42067c478bd9Sstevel@tonic-gate 	}
42077c478bd9Sstevel@tonic-gate }
42087c478bd9Sstevel@tonic-gate 
42097c478bd9Sstevel@tonic-gate /*
42107c478bd9Sstevel@tonic-gate  * mdi_pi_get_state():
42117c478bd9Sstevel@tonic-gate  *		Get the mdi_pathinfo node state. Transient states are internal
42127c478bd9Sstevel@tonic-gate  *		and not provided to the users
42137c478bd9Sstevel@tonic-gate  */
42147c478bd9Sstevel@tonic-gate mdi_pathinfo_state_t
42157c478bd9Sstevel@tonic-gate mdi_pi_get_state(mdi_pathinfo_t *pip)
42167c478bd9Sstevel@tonic-gate {
42177c478bd9Sstevel@tonic-gate 	mdi_pathinfo_state_t    state = MDI_PATHINFO_STATE_INIT;
42187c478bd9Sstevel@tonic-gate 
42197c478bd9Sstevel@tonic-gate 	if (pip) {
42207c478bd9Sstevel@tonic-gate 		if (MDI_PI_IS_TRANSIENT(pip)) {
42217c478bd9Sstevel@tonic-gate 			/*
42227c478bd9Sstevel@tonic-gate 			 * mdi_pathinfo is in state transition.  Return the
42237c478bd9Sstevel@tonic-gate 			 * last good state.
42247c478bd9Sstevel@tonic-gate 			 */
42257c478bd9Sstevel@tonic-gate 			state = MDI_PI_OLD_STATE(pip);
42267c478bd9Sstevel@tonic-gate 		} else {
42277c478bd9Sstevel@tonic-gate 			state = MDI_PI_STATE(pip);
42287c478bd9Sstevel@tonic-gate 		}
42297c478bd9Sstevel@tonic-gate 	}
42307c478bd9Sstevel@tonic-gate 	return (state);
42317c478bd9Sstevel@tonic-gate }
42327c478bd9Sstevel@tonic-gate 
42337c478bd9Sstevel@tonic-gate /*
42344c06356bSdh142964  * mdi_pi_get_flags():
42354c06356bSdh142964  *		Get the mdi_pathinfo node flags.
42364c06356bSdh142964  */
42374c06356bSdh142964 uint_t
42384c06356bSdh142964 mdi_pi_get_flags(mdi_pathinfo_t *pip)
42394c06356bSdh142964 {
42404c06356bSdh142964 	return (pip ? MDI_PI(pip)->pi_flags : 0);
42414c06356bSdh142964 }
42424c06356bSdh142964 
42434c06356bSdh142964 /*
42447c478bd9Sstevel@tonic-gate  * Note that the following function needs to be the new interface for
42457c478bd9Sstevel@tonic-gate  * mdi_pi_get_state when mpxio gets integrated to ON.
42467c478bd9Sstevel@tonic-gate  */
42477c478bd9Sstevel@tonic-gate int
42487c478bd9Sstevel@tonic-gate mdi_pi_get_state2(mdi_pathinfo_t *pip, mdi_pathinfo_state_t *state,
42497c478bd9Sstevel@tonic-gate 		uint32_t *ext_state)
42507c478bd9Sstevel@tonic-gate {
42517c478bd9Sstevel@tonic-gate 	*state = MDI_PATHINFO_STATE_INIT;
42527c478bd9Sstevel@tonic-gate 
42537c478bd9Sstevel@tonic-gate 	if (pip) {
42547c478bd9Sstevel@tonic-gate 		if (MDI_PI_IS_TRANSIENT(pip)) {
42557c478bd9Sstevel@tonic-gate 			/*
42567c478bd9Sstevel@tonic-gate 			 * mdi_pathinfo is in state transition.  Return the
42577c478bd9Sstevel@tonic-gate 			 * last good state.
42587c478bd9Sstevel@tonic-gate 			 */
42597c478bd9Sstevel@tonic-gate 			*state = MDI_PI_OLD_STATE(pip);
42607c478bd9Sstevel@tonic-gate 			*ext_state = MDI_PI_OLD_EXT_STATE(pip);
42617c478bd9Sstevel@tonic-gate 		} else {
42627c478bd9Sstevel@tonic-gate 			*state = MDI_PI_STATE(pip);
42637c478bd9Sstevel@tonic-gate 			*ext_state = MDI_PI_EXT_STATE(pip);
42647c478bd9Sstevel@tonic-gate 		}
42657c478bd9Sstevel@tonic-gate 	}
42667c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
42677c478bd9Sstevel@tonic-gate }
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate /*
42707c478bd9Sstevel@tonic-gate  * mdi_pi_get_preferred:
42717c478bd9Sstevel@tonic-gate  *	Get the preferred path flag
42727c478bd9Sstevel@tonic-gate  */
42737c478bd9Sstevel@tonic-gate int
42747c478bd9Sstevel@tonic-gate mdi_pi_get_preferred(mdi_pathinfo_t *pip)
42757c478bd9Sstevel@tonic-gate {
42767c478bd9Sstevel@tonic-gate 	if (pip) {
42777c478bd9Sstevel@tonic-gate 		return (MDI_PI(pip)->pi_preferred);
42787c478bd9Sstevel@tonic-gate 	}
42797c478bd9Sstevel@tonic-gate 	return (0);
42807c478bd9Sstevel@tonic-gate }
42817c478bd9Sstevel@tonic-gate 
42827c478bd9Sstevel@tonic-gate /*
42837c478bd9Sstevel@tonic-gate  * mdi_pi_set_preferred:
42847c478bd9Sstevel@tonic-gate  *	Set the preferred path flag
42857c478bd9Sstevel@tonic-gate  */
42867c478bd9Sstevel@tonic-gate void
42877c478bd9Sstevel@tonic-gate mdi_pi_set_preferred(mdi_pathinfo_t *pip, int preferred)
42887c478bd9Sstevel@tonic-gate {
42897c478bd9Sstevel@tonic-gate 	if (pip) {
42907c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_preferred = preferred;
42917c478bd9Sstevel@tonic-gate 	}
42927c478bd9Sstevel@tonic-gate }
42937c478bd9Sstevel@tonic-gate 
42947c478bd9Sstevel@tonic-gate /*
42957c478bd9Sstevel@tonic-gate  * mdi_pi_set_state():
42967c478bd9Sstevel@tonic-gate  *		Set the mdi_pathinfo node state
42977c478bd9Sstevel@tonic-gate  */
42987c478bd9Sstevel@tonic-gate void
42997c478bd9Sstevel@tonic-gate mdi_pi_set_state(mdi_pathinfo_t *pip, mdi_pathinfo_state_t state)
43007c478bd9Sstevel@tonic-gate {
43017c478bd9Sstevel@tonic-gate 	uint32_t	ext_state;
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate 	if (pip) {
43047c478bd9Sstevel@tonic-gate 		ext_state = MDI_PI(pip)->pi_state & MDI_PATHINFO_EXT_STATE_MASK;
43057c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_state = state;
43067c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_state |= ext_state;
43074c06356bSdh142964 
43084c06356bSdh142964 		/* Path has changed state, invalidate DINFOCACHE snap shot. */
43094c06356bSdh142964 		i_ddi_di_cache_invalidate();
43107c478bd9Sstevel@tonic-gate 	}
43117c478bd9Sstevel@tonic-gate }
43127c478bd9Sstevel@tonic-gate 
43137c478bd9Sstevel@tonic-gate /*
43147c478bd9Sstevel@tonic-gate  * Property functions:
43157c478bd9Sstevel@tonic-gate  */
43167c478bd9Sstevel@tonic-gate int
43177c478bd9Sstevel@tonic-gate i_map_nvlist_error_to_mdi(int val)
43187c478bd9Sstevel@tonic-gate {
43197c478bd9Sstevel@tonic-gate 	int rv;
43207c478bd9Sstevel@tonic-gate 
43217c478bd9Sstevel@tonic-gate 	switch (val) {
43227c478bd9Sstevel@tonic-gate 	case 0:
43237c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_SUCCESS;
43247c478bd9Sstevel@tonic-gate 		break;
43257c478bd9Sstevel@tonic-gate 	case EINVAL:
43267c478bd9Sstevel@tonic-gate 	case ENOTSUP:
43277c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_INVAL_ARG;
43287c478bd9Sstevel@tonic-gate 		break;
43297c478bd9Sstevel@tonic-gate 	case ENOMEM:
43307c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_NO_MEMORY;
43317c478bd9Sstevel@tonic-gate 		break;
43327c478bd9Sstevel@tonic-gate 	default:
43337c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_NOT_FOUND;
43347c478bd9Sstevel@tonic-gate 		break;
43357c478bd9Sstevel@tonic-gate 	}
43367c478bd9Sstevel@tonic-gate 	return (rv);
43377c478bd9Sstevel@tonic-gate }
43387c478bd9Sstevel@tonic-gate 
43397c478bd9Sstevel@tonic-gate /*
43407c478bd9Sstevel@tonic-gate  * mdi_pi_get_next_prop():
43417c478bd9Sstevel@tonic-gate  * 		Property walk function.  The caller should hold mdi_pi_lock()
43427c478bd9Sstevel@tonic-gate  *		and release by calling mdi_pi_unlock() at the end of walk to
43437c478bd9Sstevel@tonic-gate  *		get a consistent value.
43447c478bd9Sstevel@tonic-gate  */
43457c478bd9Sstevel@tonic-gate nvpair_t *
43467c478bd9Sstevel@tonic-gate mdi_pi_get_next_prop(mdi_pathinfo_t *pip, nvpair_t *prev)
43477c478bd9Sstevel@tonic-gate {
43487c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
43497c478bd9Sstevel@tonic-gate 		return (NULL);
43507c478bd9Sstevel@tonic-gate 	}
43515e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
43527c478bd9Sstevel@tonic-gate 	return (nvlist_next_nvpair(MDI_PI(pip)->pi_prop, prev));
43537c478bd9Sstevel@tonic-gate }
43547c478bd9Sstevel@tonic-gate 
43557c478bd9Sstevel@tonic-gate /*
43567c478bd9Sstevel@tonic-gate  * mdi_prop_remove():
43577c478bd9Sstevel@tonic-gate  * 		Remove the named property from the named list.
43587c478bd9Sstevel@tonic-gate  */
43597c478bd9Sstevel@tonic-gate int
43607c478bd9Sstevel@tonic-gate mdi_prop_remove(mdi_pathinfo_t *pip, char *name)
43617c478bd9Sstevel@tonic-gate {
43627c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
43637c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
43647c478bd9Sstevel@tonic-gate 	}
43655e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
43667c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
43677c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
43687c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
43697c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
43707c478bd9Sstevel@tonic-gate 	}
43717c478bd9Sstevel@tonic-gate 	if (name) {
43727c478bd9Sstevel@tonic-gate 		(void) nvlist_remove_all(MDI_PI(pip)->pi_prop, name);
43737c478bd9Sstevel@tonic-gate 	} else {
43747c478bd9Sstevel@tonic-gate 		char		nvp_name[MAXNAMELEN];
43757c478bd9Sstevel@tonic-gate 		nvpair_t	*nvp;
43767c478bd9Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(MDI_PI(pip)->pi_prop, NULL);
43777c478bd9Sstevel@tonic-gate 		while (nvp) {
43787c478bd9Sstevel@tonic-gate 			nvpair_t	*next;
43797c478bd9Sstevel@tonic-gate 			next = nvlist_next_nvpair(MDI_PI(pip)->pi_prop, nvp);
43804c06356bSdh142964 			(void) snprintf(nvp_name, sizeof(nvp_name), "%s",
43817c478bd9Sstevel@tonic-gate 			    nvpair_name(nvp));
43827c478bd9Sstevel@tonic-gate 			(void) nvlist_remove_all(MDI_PI(pip)->pi_prop,
43837c478bd9Sstevel@tonic-gate 			    nvp_name);
43847c478bd9Sstevel@tonic-gate 			nvp = next;
43857c478bd9Sstevel@tonic-gate 		}
43867c478bd9Sstevel@tonic-gate 	}
43877c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
43887c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
43897c478bd9Sstevel@tonic-gate }
43907c478bd9Sstevel@tonic-gate 
43917c478bd9Sstevel@tonic-gate /*
43927c478bd9Sstevel@tonic-gate  * mdi_prop_size():
43937c478bd9Sstevel@tonic-gate  * 		Get buffer size needed to pack the property data.
43947c478bd9Sstevel@tonic-gate  * 		Caller should hold the mdi_pathinfo_t lock to get a consistent
43957c478bd9Sstevel@tonic-gate  *		buffer size.
43967c478bd9Sstevel@tonic-gate  */
43977c478bd9Sstevel@tonic-gate int
43987c478bd9Sstevel@tonic-gate mdi_prop_size(mdi_pathinfo_t *pip, size_t *buflenp)
43997c478bd9Sstevel@tonic-gate {
44007c478bd9Sstevel@tonic-gate 	int	rv;
44017c478bd9Sstevel@tonic-gate 	size_t	bufsize;
44027c478bd9Sstevel@tonic-gate 
44037c478bd9Sstevel@tonic-gate 	*buflenp = 0;
44047c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
44057c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44067c478bd9Sstevel@tonic-gate 	}
44075e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
44087c478bd9Sstevel@tonic-gate 	rv = nvlist_size(MDI_PI(pip)->pi_prop,
44097c478bd9Sstevel@tonic-gate 	    &bufsize, NV_ENCODE_NATIVE);
44107c478bd9Sstevel@tonic-gate 	*buflenp = bufsize;
44117c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
44127c478bd9Sstevel@tonic-gate }
44137c478bd9Sstevel@tonic-gate 
44147c478bd9Sstevel@tonic-gate /*
44157c478bd9Sstevel@tonic-gate  * mdi_prop_pack():
44167c478bd9Sstevel@tonic-gate  * 		pack the property list.  The caller should hold the
44177c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node to get a consistent data
44187c478bd9Sstevel@tonic-gate  */
44197c478bd9Sstevel@tonic-gate int
44207c478bd9Sstevel@tonic-gate mdi_prop_pack(mdi_pathinfo_t *pip, char **bufp, uint_t buflen)
44217c478bd9Sstevel@tonic-gate {
44227c478bd9Sstevel@tonic-gate 	int	rv;
44237c478bd9Sstevel@tonic-gate 	size_t	bufsize;
44247c478bd9Sstevel@tonic-gate 
44257c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || MDI_PI(pip)->pi_prop == NULL) {
44267c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44277c478bd9Sstevel@tonic-gate 	}
44287c478bd9Sstevel@tonic-gate 
44295e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
44307c478bd9Sstevel@tonic-gate 
44317c478bd9Sstevel@tonic-gate 	bufsize = buflen;
44327c478bd9Sstevel@tonic-gate 	rv = nvlist_pack(MDI_PI(pip)->pi_prop, bufp, (size_t *)&bufsize,
44337c478bd9Sstevel@tonic-gate 	    NV_ENCODE_NATIVE, KM_SLEEP);
44347c478bd9Sstevel@tonic-gate 
44357c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
44367c478bd9Sstevel@tonic-gate }
44377c478bd9Sstevel@tonic-gate 
44387c478bd9Sstevel@tonic-gate /*
44397c478bd9Sstevel@tonic-gate  * mdi_prop_update_byte():
44407c478bd9Sstevel@tonic-gate  *		Create/Update a byte property
44417c478bd9Sstevel@tonic-gate  */
44427c478bd9Sstevel@tonic-gate int
44437c478bd9Sstevel@tonic-gate mdi_prop_update_byte(mdi_pathinfo_t *pip, char *name, uchar_t data)
44447c478bd9Sstevel@tonic-gate {
44457c478bd9Sstevel@tonic-gate 	int rv;
44467c478bd9Sstevel@tonic-gate 
44477c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
44487c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
44497c478bd9Sstevel@tonic-gate 	}
44505e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
44517c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
44527c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
44537c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
44547c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44557c478bd9Sstevel@tonic-gate 	}
44567c478bd9Sstevel@tonic-gate 	rv = nvlist_add_byte(MDI_PI(pip)->pi_prop, name, data);
44577c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
44587c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
44597c478bd9Sstevel@tonic-gate }
44607c478bd9Sstevel@tonic-gate 
44617c478bd9Sstevel@tonic-gate /*
44627c478bd9Sstevel@tonic-gate  * mdi_prop_update_byte_array():
44637c478bd9Sstevel@tonic-gate  *		Create/Update a byte array property
44647c478bd9Sstevel@tonic-gate  */
44657c478bd9Sstevel@tonic-gate int
44667c478bd9Sstevel@tonic-gate mdi_prop_update_byte_array(mdi_pathinfo_t *pip, char *name, uchar_t *data,
44677c478bd9Sstevel@tonic-gate     uint_t nelements)
44687c478bd9Sstevel@tonic-gate {
44697c478bd9Sstevel@tonic-gate 	int rv;
44707c478bd9Sstevel@tonic-gate 
44717c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
44727c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
44737c478bd9Sstevel@tonic-gate 	}
44745e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
44757c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
44767c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
44777c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
44787c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44797c478bd9Sstevel@tonic-gate 	}
44807c478bd9Sstevel@tonic-gate 	rv = nvlist_add_byte_array(MDI_PI(pip)->pi_prop, name, data, nelements);
44817c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
44827c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
44837c478bd9Sstevel@tonic-gate }
44847c478bd9Sstevel@tonic-gate 
44857c478bd9Sstevel@tonic-gate /*
44867c478bd9Sstevel@tonic-gate  * mdi_prop_update_int():
44877c478bd9Sstevel@tonic-gate  *		Create/Update a 32 bit integer property
44887c478bd9Sstevel@tonic-gate  */
44897c478bd9Sstevel@tonic-gate int
44907c478bd9Sstevel@tonic-gate mdi_prop_update_int(mdi_pathinfo_t *pip, char *name, int data)
44917c478bd9Sstevel@tonic-gate {
44927c478bd9Sstevel@tonic-gate 	int rv;
44937c478bd9Sstevel@tonic-gate 
44947c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
44957c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
44967c478bd9Sstevel@tonic-gate 	}
44975e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
44987c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
44997c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
45007c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
45017c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
45027c478bd9Sstevel@tonic-gate 	}
45037c478bd9Sstevel@tonic-gate 	rv = nvlist_add_int32(MDI_PI(pip)->pi_prop, name, (int32_t)data);
45047c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
45057c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
45067c478bd9Sstevel@tonic-gate }
45077c478bd9Sstevel@tonic-gate 
45087c478bd9Sstevel@tonic-gate /*
45097c478bd9Sstevel@tonic-gate  * mdi_prop_update_int64():
45107c478bd9Sstevel@tonic-gate  *		Create/Update a 64 bit integer property
45117c478bd9Sstevel@tonic-gate  */
45127c478bd9Sstevel@tonic-gate int
45137c478bd9Sstevel@tonic-gate mdi_prop_update_int64(mdi_pathinfo_t *pip, char *name, int64_t data)
45147c478bd9Sstevel@tonic-gate {
45157c478bd9Sstevel@tonic-gate 	int rv;
45167c478bd9Sstevel@tonic-gate 
45177c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
45187c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
45197c478bd9Sstevel@tonic-gate 	}
45205e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
45217c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
45227c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
45237c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
45247c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
45257c478bd9Sstevel@tonic-gate 	}
45267c478bd9Sstevel@tonic-gate 	rv = nvlist_add_int64(MDI_PI(pip)->pi_prop, name, data);
45277c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
45287c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
45297c478bd9Sstevel@tonic-gate }
45307c478bd9Sstevel@tonic-gate 
45317c478bd9Sstevel@tonic-gate /*
45327c478bd9Sstevel@tonic-gate  * mdi_prop_update_int_array():
45337c478bd9Sstevel@tonic-gate  *		Create/Update a int array property
45347c478bd9Sstevel@tonic-gate  */
45357c478bd9Sstevel@tonic-gate int
45367c478bd9Sstevel@tonic-gate mdi_prop_update_int_array(mdi_pathinfo_t *pip, char *name, int *data,
45377c478bd9Sstevel@tonic-gate 	    uint_t nelements)
45387c478bd9Sstevel@tonic-gate {
45397c478bd9Sstevel@tonic-gate 	int rv;
45407c478bd9Sstevel@tonic-gate 
45417c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
45427c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
45437c478bd9Sstevel@tonic-gate 	}
45445e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
45457c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
45467c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
45477c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
45487c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
45497c478bd9Sstevel@tonic-gate 	}
45507c478bd9Sstevel@tonic-gate 	rv = nvlist_add_int32_array(MDI_PI(pip)->pi_prop, name, (int32_t *)data,
45517c478bd9Sstevel@tonic-gate 	    nelements);
45527c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
45537c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
45547c478bd9Sstevel@tonic-gate }
45557c478bd9Sstevel@tonic-gate 
45567c478bd9Sstevel@tonic-gate /*
45577c478bd9Sstevel@tonic-gate  * mdi_prop_update_string():
45587c478bd9Sstevel@tonic-gate  *		Create/Update a string property
45597c478bd9Sstevel@tonic-gate  */
45607c478bd9Sstevel@tonic-gate int
45617c478bd9Sstevel@tonic-gate mdi_prop_update_string(mdi_pathinfo_t *pip, char *name, char *data)
45627c478bd9Sstevel@tonic-gate {
45637c478bd9Sstevel@tonic-gate 	int rv;
45647c478bd9Sstevel@tonic-gate 
45657c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
45667c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
45677c478bd9Sstevel@tonic-gate 	}
45685e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
45697c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
45707c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
45717c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
45727c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
45737c478bd9Sstevel@tonic-gate 	}
45747c478bd9Sstevel@tonic-gate 	rv = nvlist_add_string(MDI_PI(pip)->pi_prop, name, data);
45757c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
45767c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
45777c478bd9Sstevel@tonic-gate }
45787c478bd9Sstevel@tonic-gate 
45797c478bd9Sstevel@tonic-gate /*
45807c478bd9Sstevel@tonic-gate  * mdi_prop_update_string_array():
45817c478bd9Sstevel@tonic-gate  *		Create/Update a string array property
45827c478bd9Sstevel@tonic-gate  */
45837c478bd9Sstevel@tonic-gate int
45847c478bd9Sstevel@tonic-gate mdi_prop_update_string_array(mdi_pathinfo_t *pip, char *name, char **data,
45857c478bd9Sstevel@tonic-gate     uint_t nelements)
45867c478bd9Sstevel@tonic-gate {
45877c478bd9Sstevel@tonic-gate 	int rv;
45887c478bd9Sstevel@tonic-gate 
45897c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
45907c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
45917c478bd9Sstevel@tonic-gate 	}
45925e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
45937c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
45947c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
45957c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
45967c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
45977c478bd9Sstevel@tonic-gate 	}
45987c478bd9Sstevel@tonic-gate 	rv = nvlist_add_string_array(MDI_PI(pip)->pi_prop, name, data,
45997c478bd9Sstevel@tonic-gate 	    nelements);
46007c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
46017c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
46027c478bd9Sstevel@tonic-gate }
46037c478bd9Sstevel@tonic-gate 
46047c478bd9Sstevel@tonic-gate /*
46057c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_byte():
46067c478bd9Sstevel@tonic-gate  * 		Look for byte property identified by name.  The data returned
46077c478bd9Sstevel@tonic-gate  *		is the actual property and valid as long as mdi_pathinfo_t node
46087c478bd9Sstevel@tonic-gate  *		is alive.
46097c478bd9Sstevel@tonic-gate  */
46107c478bd9Sstevel@tonic-gate int
46117c478bd9Sstevel@tonic-gate mdi_prop_lookup_byte(mdi_pathinfo_t *pip, char *name, uchar_t *data)
46127c478bd9Sstevel@tonic-gate {
46137c478bd9Sstevel@tonic-gate 	int rv;
46147c478bd9Sstevel@tonic-gate 
46157c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
46167c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
46177c478bd9Sstevel@tonic-gate 	}
46187c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_byte(MDI_PI(pip)->pi_prop, name, data);
46197c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
46207c478bd9Sstevel@tonic-gate }
46217c478bd9Sstevel@tonic-gate 
46227c478bd9Sstevel@tonic-gate 
46237c478bd9Sstevel@tonic-gate /*
46247c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_byte_array():
46257c478bd9Sstevel@tonic-gate  * 		Look for byte array property identified by name.  The data
46267c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
46277c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
46287c478bd9Sstevel@tonic-gate  */
46297c478bd9Sstevel@tonic-gate int
46307c478bd9Sstevel@tonic-gate mdi_prop_lookup_byte_array(mdi_pathinfo_t *pip, char *name, uchar_t **data,
46317c478bd9Sstevel@tonic-gate     uint_t *nelements)
46327c478bd9Sstevel@tonic-gate {
46337c478bd9Sstevel@tonic-gate 	int rv;
46347c478bd9Sstevel@tonic-gate 
46357c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
46367c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
46377c478bd9Sstevel@tonic-gate 	}
46387c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_byte_array(MDI_PI(pip)->pi_prop, name, data,
46397c478bd9Sstevel@tonic-gate 	    nelements);
46407c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
46417c478bd9Sstevel@tonic-gate }
46427c478bd9Sstevel@tonic-gate 
46437c478bd9Sstevel@tonic-gate /*
46447c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_int():
46457c478bd9Sstevel@tonic-gate  * 		Look for int property identified by name.  The data returned
46467c478bd9Sstevel@tonic-gate  *		is the actual property and valid as long as mdi_pathinfo_t
46477c478bd9Sstevel@tonic-gate  *		node is alive.
46487c478bd9Sstevel@tonic-gate  */
46497c478bd9Sstevel@tonic-gate int
46507c478bd9Sstevel@tonic-gate mdi_prop_lookup_int(mdi_pathinfo_t *pip, char *name, int *data)
46517c478bd9Sstevel@tonic-gate {
46527c478bd9Sstevel@tonic-gate 	int rv;
46537c478bd9Sstevel@tonic-gate 
46547c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
46557c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
46567c478bd9Sstevel@tonic-gate 	}
46577c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_int32(MDI_PI(pip)->pi_prop, name, (int32_t *)data);
46587c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
46597c478bd9Sstevel@tonic-gate }
46607c478bd9Sstevel@tonic-gate 
46617c478bd9Sstevel@tonic-gate /*
46627c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_int64():
46637c478bd9Sstevel@tonic-gate  * 		Look for int64 property identified by name.  The data returned
46647c478bd9Sstevel@tonic-gate  *		is the actual property and valid as long as mdi_pathinfo_t node
46657c478bd9Sstevel@tonic-gate  *		is alive.
46667c478bd9Sstevel@tonic-gate  */
46677c478bd9Sstevel@tonic-gate int
46687c478bd9Sstevel@tonic-gate mdi_prop_lookup_int64(mdi_pathinfo_t *pip, char *name, int64_t *data)
46697c478bd9Sstevel@tonic-gate {
46707c478bd9Sstevel@tonic-gate 	int rv;
46717c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
46727c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
46737c478bd9Sstevel@tonic-gate 	}
46747c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_int64(MDI_PI(pip)->pi_prop, name, data);
46757c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
46767c478bd9Sstevel@tonic-gate }
46777c478bd9Sstevel@tonic-gate 
46787c478bd9Sstevel@tonic-gate /*
46797c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_int_array():
46807c478bd9Sstevel@tonic-gate  * 		Look for int array property identified by name.  The data
46817c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
46827c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
46837c478bd9Sstevel@tonic-gate  */
46847c478bd9Sstevel@tonic-gate int
46857c478bd9Sstevel@tonic-gate mdi_prop_lookup_int_array(mdi_pathinfo_t *pip, char *name, int **data,
46867c478bd9Sstevel@tonic-gate     uint_t *nelements)
46877c478bd9Sstevel@tonic-gate {
46887c478bd9Sstevel@tonic-gate 	int rv;
46897c478bd9Sstevel@tonic-gate 
46907c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
46917c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
46927c478bd9Sstevel@tonic-gate 	}
46937c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_int32_array(MDI_PI(pip)->pi_prop, name,
46947c478bd9Sstevel@tonic-gate 	    (int32_t **)data, nelements);
46957c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
46967c478bd9Sstevel@tonic-gate }
46977c478bd9Sstevel@tonic-gate 
46987c478bd9Sstevel@tonic-gate /*
46997c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_string():
47007c478bd9Sstevel@tonic-gate  * 		Look for string property identified by name.  The data
47017c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
47027c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
47037c478bd9Sstevel@tonic-gate  */
47047c478bd9Sstevel@tonic-gate int
47057c478bd9Sstevel@tonic-gate mdi_prop_lookup_string(mdi_pathinfo_t *pip, char *name, char **data)
47067c478bd9Sstevel@tonic-gate {
47077c478bd9Sstevel@tonic-gate 	int rv;
47087c478bd9Sstevel@tonic-gate 
47097c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
47107c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
47117c478bd9Sstevel@tonic-gate 	}
47127c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_string(MDI_PI(pip)->pi_prop, name, data);
47137c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
47147c478bd9Sstevel@tonic-gate }
47157c478bd9Sstevel@tonic-gate 
47167c478bd9Sstevel@tonic-gate /*
47177c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_string_array():
47187c478bd9Sstevel@tonic-gate  * 		Look for string array property identified by name.  The data
47197c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
47207c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
47217c478bd9Sstevel@tonic-gate  */
47227c478bd9Sstevel@tonic-gate int
47237c478bd9Sstevel@tonic-gate mdi_prop_lookup_string_array(mdi_pathinfo_t *pip, char *name, char ***data,
47247c478bd9Sstevel@tonic-gate     uint_t *nelements)
47257c478bd9Sstevel@tonic-gate {
47267c478bd9Sstevel@tonic-gate 	int rv;
47277c478bd9Sstevel@tonic-gate 
47287c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
47297c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
47307c478bd9Sstevel@tonic-gate 	}
47317c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_string_array(MDI_PI(pip)->pi_prop, name, data,
47327c478bd9Sstevel@tonic-gate 	    nelements);
47337c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
47347c478bd9Sstevel@tonic-gate }
47357c478bd9Sstevel@tonic-gate 
47367c478bd9Sstevel@tonic-gate /*
47377c478bd9Sstevel@tonic-gate  * mdi_prop_free():
47387c478bd9Sstevel@tonic-gate  * 		Symmetrical function to ddi_prop_free(). nvlist_lookup_xx()
47397c478bd9Sstevel@tonic-gate  *		functions return the pointer to actual property data and not a
47407c478bd9Sstevel@tonic-gate  *		copy of it.  So the data returned is valid as long as
47417c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is valid.
47427c478bd9Sstevel@tonic-gate  */
47437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
47447c478bd9Sstevel@tonic-gate int
47457c478bd9Sstevel@tonic-gate mdi_prop_free(void *data)
47467c478bd9Sstevel@tonic-gate {
47477c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
47487c478bd9Sstevel@tonic-gate }
47497c478bd9Sstevel@tonic-gate 
47507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
47517c478bd9Sstevel@tonic-gate static void
47527c478bd9Sstevel@tonic-gate i_mdi_report_path_state(mdi_client_t *ct, mdi_pathinfo_t *pip)
47537c478bd9Sstevel@tonic-gate {
47544c06356bSdh142964 	char		*ct_path;
47557c478bd9Sstevel@tonic-gate 	char		*ct_status;
47567c478bd9Sstevel@tonic-gate 	char		*status;
47574c06356bSdh142964 	dev_info_t	*cdip = ct->ct_dip;
47587c478bd9Sstevel@tonic-gate 	char		lb_buf[64];
47594c06356bSdh142964 	int		report_lb_c = 0, report_lb_p = 0;
47607c478bd9Sstevel@tonic-gate 
47615e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
47624c06356bSdh142964 	if ((cdip == NULL) || (ddi_get_instance(cdip) == -1) ||
47637c478bd9Sstevel@tonic-gate 	    (MDI_CLIENT_IS_REPORT_DEV_NEEDED(ct) == 0)) {
47647c478bd9Sstevel@tonic-gate 		return;
47657c478bd9Sstevel@tonic-gate 	}
47667c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_OPTIMAL) {
47677c478bd9Sstevel@tonic-gate 		ct_status = "optimal";
47684c06356bSdh142964 		report_lb_c = 1;
47697c478bd9Sstevel@tonic-gate 	} else if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_DEGRADED) {
47707c478bd9Sstevel@tonic-gate 		ct_status = "degraded";
47717c478bd9Sstevel@tonic-gate 	} else if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_FAILED) {
47727c478bd9Sstevel@tonic-gate 		ct_status = "failed";
47737c478bd9Sstevel@tonic-gate 	} else {
47747c478bd9Sstevel@tonic-gate 		ct_status = "unknown";
47757c478bd9Sstevel@tonic-gate 	}
47767c478bd9Sstevel@tonic-gate 
47774c06356bSdh142964 	lb_buf[0] = 0;		/* not interested in load balancing config */
47784c06356bSdh142964 
47794c06356bSdh142964 	if (MDI_PI_FLAGS_IS_DEVICE_REMOVED(pip)) {
47804c06356bSdh142964 		status = "removed";
47814c06356bSdh142964 	} else if (MDI_PI_IS_OFFLINE(pip)) {
47827c478bd9Sstevel@tonic-gate 		status = "offline";
47837c478bd9Sstevel@tonic-gate 	} else if (MDI_PI_IS_ONLINE(pip)) {
47847c478bd9Sstevel@tonic-gate 		status = "online";
47854c06356bSdh142964 		report_lb_p = 1;
47867c478bd9Sstevel@tonic-gate 	} else if (MDI_PI_IS_STANDBY(pip)) {
47877c478bd9Sstevel@tonic-gate 		status = "standby";
47887c478bd9Sstevel@tonic-gate 	} else if (MDI_PI_IS_FAULT(pip)) {
47897c478bd9Sstevel@tonic-gate 		status = "faulted";
47907c478bd9Sstevel@tonic-gate 	} else {
47917c478bd9Sstevel@tonic-gate 		status = "unknown";
47927c478bd9Sstevel@tonic-gate 	}
47937c478bd9Sstevel@tonic-gate 
47944c06356bSdh142964 	if (cdip) {
47954c06356bSdh142964 		ct_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
47964c06356bSdh142964 
47974c06356bSdh142964 		/*
47984c06356bSdh142964 		 * NOTE: Keeping "multipath status: %s" and
47994c06356bSdh142964 		 * "Load balancing: %s" format unchanged in case someone
48004c06356bSdh142964 		 * scrubs /var/adm/messages looking for these messages.
48014c06356bSdh142964 		 */
48024c06356bSdh142964 		if (report_lb_c && report_lb_p) {
48037c478bd9Sstevel@tonic-gate 			if (ct->ct_lb == LOAD_BALANCE_LBA) {
48047c478bd9Sstevel@tonic-gate 				(void) snprintf(lb_buf, sizeof (lb_buf),
48057c478bd9Sstevel@tonic-gate 				    "%s, region-size: %d", mdi_load_balance_lba,
48067c478bd9Sstevel@tonic-gate 				    ct->ct_lb_args->region_size);
48077c478bd9Sstevel@tonic-gate 			} else if (ct->ct_lb == LOAD_BALANCE_NONE) {
48087c478bd9Sstevel@tonic-gate 				(void) snprintf(lb_buf, sizeof (lb_buf),
48097c478bd9Sstevel@tonic-gate 				    "%s", mdi_load_balance_none);
48107c478bd9Sstevel@tonic-gate 			} else {
48117c478bd9Sstevel@tonic-gate 				(void) snprintf(lb_buf, sizeof (lb_buf), "%s",
48127c478bd9Sstevel@tonic-gate 				    mdi_load_balance_rr);
48137c478bd9Sstevel@tonic-gate 			}
48147c478bd9Sstevel@tonic-gate 
48154c06356bSdh142964 			cmn_err(mdi_debug_consoleonly ? CE_NOTE : CE_CONT,
48164c06356bSdh142964 			    "?%s (%s%d) multipath status: %s: "
48174c06356bSdh142964 			    "path %d %s is %s: Load balancing: %s\n",
48184c06356bSdh142964 			    ddi_pathname(cdip, ct_path), ddi_driver_name(cdip),
48194c06356bSdh142964 			    ddi_get_instance(cdip), ct_status,
48204c06356bSdh142964 			    mdi_pi_get_path_instance(pip),
48214c06356bSdh142964 			    mdi_pi_spathname(pip), status, lb_buf);
48224c06356bSdh142964 		} else {
48234c06356bSdh142964 			cmn_err(mdi_debug_consoleonly ? CE_NOTE : CE_CONT,
48244c06356bSdh142964 			    "?%s (%s%d) multipath status: %s: "
48254c06356bSdh142964 			    "path %d %s is %s\n",
48264c06356bSdh142964 			    ddi_pathname(cdip, ct_path), ddi_driver_name(cdip),
48274c06356bSdh142964 			    ddi_get_instance(cdip), ct_status,
48284c06356bSdh142964 			    mdi_pi_get_path_instance(pip),
48294c06356bSdh142964 			    mdi_pi_spathname(pip), status);
48304c06356bSdh142964 		}
48314c06356bSdh142964 
48327c478bd9Sstevel@tonic-gate 		kmem_free(ct_path, MAXPATHLEN);
48337c478bd9Sstevel@tonic-gate 		MDI_CLIENT_CLEAR_REPORT_DEV_NEEDED(ct);
48347c478bd9Sstevel@tonic-gate 	}
48357c478bd9Sstevel@tonic-gate }
48367c478bd9Sstevel@tonic-gate 
48377c478bd9Sstevel@tonic-gate #ifdef	DEBUG
48387c478bd9Sstevel@tonic-gate /*
48397c478bd9Sstevel@tonic-gate  * i_mdi_log():
48407c478bd9Sstevel@tonic-gate  *		Utility function for error message management
48417c478bd9Sstevel@tonic-gate  *
48424c06356bSdh142964  *		NOTE: Implementation takes care of trailing \n for cmn_err,
48434c06356bSdh142964  *		MDI_DEBUG should not terminate fmt strings with \n.
48444c06356bSdh142964  *
48454c06356bSdh142964  *		NOTE: If the level is >= 2, and there is no leading !?^
48464c06356bSdh142964  *		then a leading ! is implied (but can be overriden via
48474c06356bSdh142964  *		mdi_debug_consoleonly). If you are using kmdb on the console,
48484c06356bSdh142964  *		consider setting mdi_debug_consoleonly to 1 as an aid.
48497c478bd9Sstevel@tonic-gate  */
48504c06356bSdh142964 /*PRINTFLIKE4*/
48517c478bd9Sstevel@tonic-gate static void
48524c06356bSdh142964 i_mdi_log(int level, const char *func, dev_info_t *dip, const char *fmt, ...)
48537c478bd9Sstevel@tonic-gate {
4854c73a93f2Sdm120769 	char		name[MAXNAMELEN];
48554c06356bSdh142964 	char		buf[512];
48565e3986cbScth 	char		*bp;
48577c478bd9Sstevel@tonic-gate 	va_list		ap;
48587c478bd9Sstevel@tonic-gate 	int		log_only = 0;
48597c478bd9Sstevel@tonic-gate 	int		boot_only = 0;
48607c478bd9Sstevel@tonic-gate 	int		console_only = 0;
48617c478bd9Sstevel@tonic-gate 
48627c478bd9Sstevel@tonic-gate 	if (dip) {
48634c06356bSdh142964 		(void) snprintf(name, sizeof(name), "%s%d: ",
486455e592a2SRandall Ralphs 		    ddi_driver_name(dip), ddi_get_instance(dip));
4865c73a93f2Sdm120769 	} else {
48665e3986cbScth 		name[0] = 0;
48677c478bd9Sstevel@tonic-gate 	}
48687c478bd9Sstevel@tonic-gate 
48697c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
48704c06356bSdh142964 	(void) vsnprintf(buf, sizeof(buf), fmt, ap);
48717c478bd9Sstevel@tonic-gate 	va_end(ap);
48727c478bd9Sstevel@tonic-gate 
48737c478bd9Sstevel@tonic-gate 	switch (buf[0]) {
48747c478bd9Sstevel@tonic-gate 	case '!':
48755e3986cbScth 		bp = &buf[1];
48767c478bd9Sstevel@tonic-gate 		log_only = 1;
48777c478bd9Sstevel@tonic-gate 		break;
48787c478bd9Sstevel@tonic-gate 	case '?':
48795e3986cbScth 		bp = &buf[1];
48807c478bd9Sstevel@tonic-gate 		boot_only = 1;
48817c478bd9Sstevel@tonic-gate 		break;
48827c478bd9Sstevel@tonic-gate 	case '^':
48835e3986cbScth 		bp = &buf[1];
48847c478bd9Sstevel@tonic-gate 		console_only = 1;
48857c478bd9Sstevel@tonic-gate 		break;
48865e3986cbScth 	default:
48874c06356bSdh142964 		if (level >= 2)
48884c06356bSdh142964 			log_only = 1;		/* ! implied */
48895e3986cbScth 		bp = buf;
48905e3986cbScth 		break;
48915e3986cbScth 	}
48925e3986cbScth 	if (mdi_debug_logonly) {
48935e3986cbScth 		log_only = 1;
48945e3986cbScth 		boot_only = 0;
48955e3986cbScth 		console_only = 0;
48967c478bd9Sstevel@tonic-gate 	}
48974c06356bSdh142964 	if (mdi_debug_consoleonly) {
48984c06356bSdh142964 		log_only = 0;
48994c06356bSdh142964 		boot_only = 0;
49004c06356bSdh142964 		console_only = 1;
49014c06356bSdh142964 		level = CE_NOTE;
49024c06356bSdh142964 		goto console;
49034c06356bSdh142964 	}
49047c478bd9Sstevel@tonic-gate 
49057c478bd9Sstevel@tonic-gate 	switch (level) {
49067c478bd9Sstevel@tonic-gate 	case CE_NOTE:
49077c478bd9Sstevel@tonic-gate 		level = CE_CONT;
49087c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
49097c478bd9Sstevel@tonic-gate 	case CE_CONT:
49104c06356bSdh142964 		if (boot_only) {
49114c06356bSdh142964 			cmn_err(level, "?mdi: %s%s: %s\n", name, func, bp);
49124c06356bSdh142964 		} else if (console_only) {
49134c06356bSdh142964 			cmn_err(level, "^mdi: %s%s: %s\n", name, func, bp);
49144c06356bSdh142964 		} else if (log_only) {
49154c06356bSdh142964 			cmn_err(level, "!mdi: %s%s: %s\n", name, func, bp);
49164c06356bSdh142964 		} else {
49174c06356bSdh142964 			cmn_err(level, "mdi: %s%s: %s\n", name, func, bp);
49184c06356bSdh142964 		}
49194c06356bSdh142964 		break;
49204c06356bSdh142964 
49217c478bd9Sstevel@tonic-gate 	case CE_WARN:
49227c478bd9Sstevel@tonic-gate 	case CE_PANIC:
49234c06356bSdh142964 	console:
49247c478bd9Sstevel@tonic-gate 		if (boot_only) {
49254c06356bSdh142964 			cmn_err(level, "?mdi: %s%s: %s", name, func, bp);
49267c478bd9Sstevel@tonic-gate 		} else if (console_only) {
49274c06356bSdh142964 			cmn_err(level, "^mdi: %s%s: %s", name, func, bp);
49287c478bd9Sstevel@tonic-gate 		} else if (log_only) {
49294c06356bSdh142964 			cmn_err(level, "!mdi: %s%s: %s", name, func, bp);
49307c478bd9Sstevel@tonic-gate 		} else {
49314c06356bSdh142964 			cmn_err(level, "mdi: %s%s: %s", name, func, bp);
49327c478bd9Sstevel@tonic-gate 		}
49337c478bd9Sstevel@tonic-gate 		break;
49347c478bd9Sstevel@tonic-gate 	default:
49355e3986cbScth 		cmn_err(level, "mdi: %s%s", name, bp);
49367c478bd9Sstevel@tonic-gate 		break;
49377c478bd9Sstevel@tonic-gate 	}
49387c478bd9Sstevel@tonic-gate }
49397c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
49407c478bd9Sstevel@tonic-gate 
49417c478bd9Sstevel@tonic-gate void
49427c478bd9Sstevel@tonic-gate i_mdi_client_online(dev_info_t *ct_dip)
49437c478bd9Sstevel@tonic-gate {
49447c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
49457c478bd9Sstevel@tonic-gate 
49467c478bd9Sstevel@tonic-gate 	/*
49477c478bd9Sstevel@tonic-gate 	 * Client online notification. Mark client state as online
49487c478bd9Sstevel@tonic-gate 	 * restore our binding with dev_info node
49497c478bd9Sstevel@tonic-gate 	 */
49507c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(ct_dip);
49517c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
49527c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
49537c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_ONLINE(ct);
49547c478bd9Sstevel@tonic-gate 	/* catch for any memory leaks */
49557c478bd9Sstevel@tonic-gate 	ASSERT((ct->ct_dip == NULL) || (ct->ct_dip == ct_dip));
49567c478bd9Sstevel@tonic-gate 	ct->ct_dip = ct_dip;
49577c478bd9Sstevel@tonic-gate 
49587c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0)
49597c478bd9Sstevel@tonic-gate 		(void) i_mdi_power_all_phci(ct);
49607c478bd9Sstevel@tonic-gate 
49614c06356bSdh142964 	MDI_DEBUG(4, (MDI_NOTE, ct_dip,
49624c06356bSdh142964 	    "i_mdi_pm_hold_client %p", (void *)ct));
49637c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_client(ct, 1);
49647c478bd9Sstevel@tonic-gate 
49657c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
49667c478bd9Sstevel@tonic-gate }
49677c478bd9Sstevel@tonic-gate 
49687c478bd9Sstevel@tonic-gate void
49697c478bd9Sstevel@tonic-gate i_mdi_phci_online(dev_info_t *ph_dip)
49707c478bd9Sstevel@tonic-gate {
49717c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
49727c478bd9Sstevel@tonic-gate 
49737c478bd9Sstevel@tonic-gate 	/* pHCI online notification. Mark state accordingly */
49747c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(ph_dip);
49757c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
49767c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
49777c478bd9Sstevel@tonic-gate 	MDI_PHCI_SET_ONLINE(ph);
49787c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
49797c478bd9Sstevel@tonic-gate }
49807c478bd9Sstevel@tonic-gate 
49817c478bd9Sstevel@tonic-gate /*
49827c478bd9Sstevel@tonic-gate  * mdi_devi_online():
49837c478bd9Sstevel@tonic-gate  * 		Online notification from NDI framework on pHCI/client
49847c478bd9Sstevel@tonic-gate  *		device online.
49857c478bd9Sstevel@tonic-gate  * Return Values:
49867c478bd9Sstevel@tonic-gate  *		NDI_SUCCESS
49877c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
49887c478bd9Sstevel@tonic-gate  */
49897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
49907c478bd9Sstevel@tonic-gate int
49917c478bd9Sstevel@tonic-gate mdi_devi_online(dev_info_t *dip, uint_t flags)
49927c478bd9Sstevel@tonic-gate {
49937c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
49947c478bd9Sstevel@tonic-gate 		i_mdi_phci_online(dip);
49957c478bd9Sstevel@tonic-gate 	}
49967c478bd9Sstevel@tonic-gate 
49977c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
49987c478bd9Sstevel@tonic-gate 		i_mdi_client_online(dip);
49997c478bd9Sstevel@tonic-gate 	}
50007c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
50017c478bd9Sstevel@tonic-gate }
50027c478bd9Sstevel@tonic-gate 
50037c478bd9Sstevel@tonic-gate /*
50047c478bd9Sstevel@tonic-gate  * mdi_devi_offline():
50057c478bd9Sstevel@tonic-gate  * 		Offline notification from NDI framework on pHCI/Client device
50067c478bd9Sstevel@tonic-gate  *		offline.
50077c478bd9Sstevel@tonic-gate  *
50087c478bd9Sstevel@tonic-gate  * Return Values:
50097c478bd9Sstevel@tonic-gate  *		NDI_SUCCESS
50107c478bd9Sstevel@tonic-gate  *		NDI_FAILURE
50117c478bd9Sstevel@tonic-gate  */
50127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
50137c478bd9Sstevel@tonic-gate int
50147c478bd9Sstevel@tonic-gate mdi_devi_offline(dev_info_t *dip, uint_t flags)
50157c478bd9Sstevel@tonic-gate {
50167c478bd9Sstevel@tonic-gate 	int		rv = NDI_SUCCESS;
50177c478bd9Sstevel@tonic-gate 
50187c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
50197c478bd9Sstevel@tonic-gate 		rv = i_mdi_client_offline(dip, flags);
50207c478bd9Sstevel@tonic-gate 		if (rv != NDI_SUCCESS)
50217c478bd9Sstevel@tonic-gate 			return (rv);
50227c478bd9Sstevel@tonic-gate 	}
50237c478bd9Sstevel@tonic-gate 
50247c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
50257c478bd9Sstevel@tonic-gate 		rv = i_mdi_phci_offline(dip, flags);
50265e3986cbScth 
50277c478bd9Sstevel@tonic-gate 		if ((rv != NDI_SUCCESS) && MDI_CLIENT(dip)) {
50287c478bd9Sstevel@tonic-gate 			/* set client back online */
50297c478bd9Sstevel@tonic-gate 			i_mdi_client_online(dip);
50307c478bd9Sstevel@tonic-gate 		}
50317c478bd9Sstevel@tonic-gate 	}
50327c478bd9Sstevel@tonic-gate 
50337c478bd9Sstevel@tonic-gate 	return (rv);
50347c478bd9Sstevel@tonic-gate }
50357c478bd9Sstevel@tonic-gate 
50367c478bd9Sstevel@tonic-gate /*ARGSUSED*/
50377c478bd9Sstevel@tonic-gate static int
50387c478bd9Sstevel@tonic-gate i_mdi_phci_offline(dev_info_t *dip, uint_t flags)
50397c478bd9Sstevel@tonic-gate {
50407c478bd9Sstevel@tonic-gate 	int		rv = NDI_SUCCESS;
50417c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
50427c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
50437c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
50447c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
50457c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*failed_pip = NULL;
50467c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
50477c478bd9Sstevel@tonic-gate 
50487c478bd9Sstevel@tonic-gate 	/*
50497c478bd9Sstevel@tonic-gate 	 * pHCI component offline notification
50507c478bd9Sstevel@tonic-gate 	 * Make sure that this pHCI instance is free to be offlined.
50517c478bd9Sstevel@tonic-gate 	 * If it is OK to proceed, Offline and remove all the child
50527c478bd9Sstevel@tonic-gate 	 * mdi_pathinfo nodes.  This process automatically offlines
50537c478bd9Sstevel@tonic-gate 	 * corresponding client devices, for which this pHCI provides
50547c478bd9Sstevel@tonic-gate 	 * critical services.
50557c478bd9Sstevel@tonic-gate 	 */
50567c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(dip);
50574c06356bSdh142964 	MDI_DEBUG(2, (MDI_NOTE, dip,
50584c06356bSdh142964 	    "called %p %p", (void *)dip, (void *)ph));
50597c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
50607c478bd9Sstevel@tonic-gate 		return (rv);
50617c478bd9Sstevel@tonic-gate 	}
50627c478bd9Sstevel@tonic-gate 
50637c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
50647c478bd9Sstevel@tonic-gate 
50657c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_OFFLINE(ph)) {
50664c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, dip,
50674c06356bSdh142964 		    "!pHCI already offlined: %p", (void *)dip));
50687c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
50697c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
50707c478bd9Sstevel@tonic-gate 	}
50717c478bd9Sstevel@tonic-gate 
50727c478bd9Sstevel@tonic-gate 	/*
50737c478bd9Sstevel@tonic-gate 	 * Check to see if the pHCI can be offlined
50747c478bd9Sstevel@tonic-gate 	 */
50757c478bd9Sstevel@tonic-gate 	if (ph->ph_unstable) {
50764c06356bSdh142964 		MDI_DEBUG(1, (MDI_WARN, dip,
50774c06356bSdh142964 		    "!One or more target devices are in transient state. "
50784c06356bSdh142964 		    "This device can not be removed at this moment. "
50794c06356bSdh142964 		    "Please try again later."));
50807c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
50817c478bd9Sstevel@tonic-gate 		return (NDI_BUSY);
50827c478bd9Sstevel@tonic-gate 	}
50837c478bd9Sstevel@tonic-gate 
50847c478bd9Sstevel@tonic-gate 	pip = ph->ph_path_head;
50857c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
50867c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
50877c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
50885e3986cbScth 
50897c478bd9Sstevel@tonic-gate 		/*
50907c478bd9Sstevel@tonic-gate 		 * The mdi_pathinfo state is OK. Check the client state.
50917c478bd9Sstevel@tonic-gate 		 * If failover in progress fail the pHCI from offlining
50927c478bd9Sstevel@tonic-gate 		 */
50937c478bd9Sstevel@tonic-gate 		ct = MDI_PI(pip)->pi_client;
50947c478bd9Sstevel@tonic-gate 		i_mdi_client_lock(ct, pip);
50957c478bd9Sstevel@tonic-gate 		if ((MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) ||
50967c478bd9Sstevel@tonic-gate 		    (ct->ct_unstable)) {
50977c478bd9Sstevel@tonic-gate 			/*
50987c478bd9Sstevel@tonic-gate 			 * Failover is in progress, Fail the DR
50997c478bd9Sstevel@tonic-gate 			 */
51004c06356bSdh142964 			MDI_DEBUG(1, (MDI_WARN, dip,
51014c06356bSdh142964 			    "!pHCI device is busy. "
51024c06356bSdh142964 			    "This device can not be removed at this moment. "
51034c06356bSdh142964 			    "Please try again later."));
51047c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
51055e3986cbScth 			i_mdi_client_unlock(ct);
51067c478bd9Sstevel@tonic-gate 			MDI_PHCI_UNLOCK(ph);
51077c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
51087c478bd9Sstevel@tonic-gate 		}
51097c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
51107c478bd9Sstevel@tonic-gate 
51117c478bd9Sstevel@tonic-gate 		/*
51127c478bd9Sstevel@tonic-gate 		 * Check to see of we are removing the last path of this
51137c478bd9Sstevel@tonic-gate 		 * client device...
51147c478bd9Sstevel@tonic-gate 		 */
51157c478bd9Sstevel@tonic-gate 		cdip = ct->ct_dip;
51167c478bd9Sstevel@tonic-gate 		if (cdip && (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
51177c478bd9Sstevel@tonic-gate 		    (i_mdi_client_compute_state(ct, ph) ==
51187c478bd9Sstevel@tonic-gate 		    MDI_CLIENT_STATE_FAILED)) {
51197c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
51207c478bd9Sstevel@tonic-gate 			MDI_PHCI_UNLOCK(ph);
51214c06356bSdh142964 			if (ndi_devi_offline(cdip,
51224c06356bSdh142964 			    NDI_DEVFS_CLEAN) != NDI_SUCCESS) {
51237c478bd9Sstevel@tonic-gate 				/*
51247c478bd9Sstevel@tonic-gate 				 * ndi_devi_offline() failed.
51257c478bd9Sstevel@tonic-gate 				 * This pHCI provides the critical path
51267c478bd9Sstevel@tonic-gate 				 * to one or more client devices.
51277c478bd9Sstevel@tonic-gate 				 * Return busy.
51287c478bd9Sstevel@tonic-gate 				 */
51297c478bd9Sstevel@tonic-gate 				MDI_PHCI_LOCK(ph);
51304c06356bSdh142964 				MDI_DEBUG(1, (MDI_WARN, dip,
51314c06356bSdh142964 				    "!pHCI device is busy. "
51324c06356bSdh142964 				    "This device can not be removed at this "
51334c06356bSdh142964 				    "moment. Please try again later."));
51347c478bd9Sstevel@tonic-gate 				failed_pip = pip;
51357c478bd9Sstevel@tonic-gate 				break;
51367c478bd9Sstevel@tonic-gate 			} else {
51377c478bd9Sstevel@tonic-gate 				MDI_PHCI_LOCK(ph);
51387c478bd9Sstevel@tonic-gate 				pip = next;
51397c478bd9Sstevel@tonic-gate 			}
51407c478bd9Sstevel@tonic-gate 		} else {
51417c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
51427c478bd9Sstevel@tonic-gate 			pip = next;
51437c478bd9Sstevel@tonic-gate 		}
51447c478bd9Sstevel@tonic-gate 	}
51457c478bd9Sstevel@tonic-gate 
51467c478bd9Sstevel@tonic-gate 	if (failed_pip) {
51477c478bd9Sstevel@tonic-gate 		pip = ph->ph_path_head;
51487c478bd9Sstevel@tonic-gate 		while (pip != failed_pip) {
51497c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
51507c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
51517c478bd9Sstevel@tonic-gate 			ct = MDI_PI(pip)->pi_client;
51527c478bd9Sstevel@tonic-gate 			i_mdi_client_lock(ct, pip);
51537c478bd9Sstevel@tonic-gate 			cdip = ct->ct_dip;
51547c478bd9Sstevel@tonic-gate 			switch (MDI_CLIENT_STATE(ct)) {
51557c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_OPTIMAL:
51567c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_DEGRADED:
51577c478bd9Sstevel@tonic-gate 				if (cdip) {
51587c478bd9Sstevel@tonic-gate 					MDI_PI_UNLOCK(pip);
51597c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
51607c478bd9Sstevel@tonic-gate 					MDI_PHCI_UNLOCK(ph);
51617c478bd9Sstevel@tonic-gate 					(void) ndi_devi_online(cdip, 0);
51627c478bd9Sstevel@tonic-gate 					MDI_PHCI_LOCK(ph);
51637c478bd9Sstevel@tonic-gate 					pip = next;
51647c478bd9Sstevel@tonic-gate 					continue;
51657c478bd9Sstevel@tonic-gate 				}
51667c478bd9Sstevel@tonic-gate 				break;
51677c478bd9Sstevel@tonic-gate 
51687c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_FAILED:
51697c478bd9Sstevel@tonic-gate 				if (cdip) {
51707c478bd9Sstevel@tonic-gate 					MDI_PI_UNLOCK(pip);
51717c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
51727c478bd9Sstevel@tonic-gate 					MDI_PHCI_UNLOCK(ph);
51734c06356bSdh142964 					(void) ndi_devi_offline(cdip,
51744c06356bSdh142964 						NDI_DEVFS_CLEAN);
51757c478bd9Sstevel@tonic-gate 					MDI_PHCI_LOCK(ph);
51767c478bd9Sstevel@tonic-gate 					pip = next;
51777c478bd9Sstevel@tonic-gate 					continue;
51787c478bd9Sstevel@tonic-gate 				}
51797c478bd9Sstevel@tonic-gate 				break;
51807c478bd9Sstevel@tonic-gate 			}
51817c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
51827c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
51837c478bd9Sstevel@tonic-gate 			pip = next;
51847c478bd9Sstevel@tonic-gate 		}
51857c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
51867c478bd9Sstevel@tonic-gate 		return (NDI_BUSY);
51877c478bd9Sstevel@tonic-gate 	}
51887c478bd9Sstevel@tonic-gate 
51897c478bd9Sstevel@tonic-gate 	/*
51907c478bd9Sstevel@tonic-gate 	 * Mark the pHCI as offline
51917c478bd9Sstevel@tonic-gate 	 */
51927c478bd9Sstevel@tonic-gate 	MDI_PHCI_SET_OFFLINE(ph);
51937c478bd9Sstevel@tonic-gate 
51947c478bd9Sstevel@tonic-gate 	/*
51957c478bd9Sstevel@tonic-gate 	 * Mark the child mdi_pathinfo nodes as transient
51967c478bd9Sstevel@tonic-gate 	 */
51977c478bd9Sstevel@tonic-gate 	pip = ph->ph_path_head;
51987c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
51997c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
52007c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
52017c478bd9Sstevel@tonic-gate 		MDI_PI_SET_OFFLINING(pip);
52027c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
52037c478bd9Sstevel@tonic-gate 		pip = next;
52047c478bd9Sstevel@tonic-gate 	}
52057c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
52067c478bd9Sstevel@tonic-gate 	/*
52077c478bd9Sstevel@tonic-gate 	 * Give a chance for any pending commands to execute
52087c478bd9Sstevel@tonic-gate 	 */
520996c4a178SChris Horne 	delay_random(mdi_delay);
52107c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
52117c478bd9Sstevel@tonic-gate 	pip = ph->ph_path_head;
52127c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
52137c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
52147c478bd9Sstevel@tonic-gate 		(void) i_mdi_pi_offline(pip, flags);
52157c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
52167c478bd9Sstevel@tonic-gate 		ct = MDI_PI(pip)->pi_client;
52177c478bd9Sstevel@tonic-gate 		if (!MDI_PI_IS_OFFLINE(pip)) {
52184c06356bSdh142964 			MDI_DEBUG(1, (MDI_WARN, dip,
52194c06356bSdh142964 			    "!pHCI device is busy. "
52204c06356bSdh142964 			    "This device can not be removed at this moment. "
52214c06356bSdh142964 			    "Please try again later."));
52227c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
52237c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_ONLINE(ph);
52247c478bd9Sstevel@tonic-gate 			MDI_PHCI_UNLOCK(ph);
52257c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
52267c478bd9Sstevel@tonic-gate 		}
52277c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
52287c478bd9Sstevel@tonic-gate 		pip = next;
52297c478bd9Sstevel@tonic-gate 	}
52307c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
52317c478bd9Sstevel@tonic-gate 
52327c478bd9Sstevel@tonic-gate 	return (rv);
52337c478bd9Sstevel@tonic-gate }
52347c478bd9Sstevel@tonic-gate 
523525e8c5aaSvikram void
523625e8c5aaSvikram mdi_phci_mark_retiring(dev_info_t *dip, char **cons_array)
523725e8c5aaSvikram {
523825e8c5aaSvikram 	mdi_phci_t	*ph;
523925e8c5aaSvikram 	mdi_client_t	*ct;
524025e8c5aaSvikram 	mdi_pathinfo_t	*pip;
524125e8c5aaSvikram 	mdi_pathinfo_t	*next;
524225e8c5aaSvikram 	dev_info_t	*cdip;
524325e8c5aaSvikram 
524425e8c5aaSvikram 	if (!MDI_PHCI(dip))
524525e8c5aaSvikram 		return;
524625e8c5aaSvikram 
524725e8c5aaSvikram 	ph = i_devi_get_phci(dip);
524825e8c5aaSvikram 	if (ph == NULL) {
524925e8c5aaSvikram 		return;
525025e8c5aaSvikram 	}
525125e8c5aaSvikram 
525225e8c5aaSvikram 	MDI_PHCI_LOCK(ph);
525325e8c5aaSvikram 
525425e8c5aaSvikram 	if (MDI_PHCI_IS_OFFLINE(ph)) {
525525e8c5aaSvikram 		/* has no last path */
525625e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
525725e8c5aaSvikram 		return;
525825e8c5aaSvikram 	}
525925e8c5aaSvikram 
526025e8c5aaSvikram 	pip = ph->ph_path_head;
526125e8c5aaSvikram 	while (pip != NULL) {
526225e8c5aaSvikram 		MDI_PI_LOCK(pip);
526325e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
526425e8c5aaSvikram 
526525e8c5aaSvikram 		ct = MDI_PI(pip)->pi_client;
526625e8c5aaSvikram 		i_mdi_client_lock(ct, pip);
526725e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
526825e8c5aaSvikram 
526925e8c5aaSvikram 		cdip = ct->ct_dip;
527025e8c5aaSvikram 		if (cdip && (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
527125e8c5aaSvikram 		    (i_mdi_client_compute_state(ct, ph) ==
527225e8c5aaSvikram 		    MDI_CLIENT_STATE_FAILED)) {
527325e8c5aaSvikram 			/* Last path. Mark client dip as retiring */
527425e8c5aaSvikram 			i_mdi_client_unlock(ct);
527525e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
527625e8c5aaSvikram 			(void) e_ddi_mark_retiring(cdip, cons_array);
527725e8c5aaSvikram 			MDI_PHCI_LOCK(ph);
527825e8c5aaSvikram 			pip = next;
527925e8c5aaSvikram 		} else {
528025e8c5aaSvikram 			i_mdi_client_unlock(ct);
528125e8c5aaSvikram 			pip = next;
528225e8c5aaSvikram 		}
528325e8c5aaSvikram 	}
528425e8c5aaSvikram 
528525e8c5aaSvikram 	MDI_PHCI_UNLOCK(ph);
528625e8c5aaSvikram 
528725e8c5aaSvikram 	return;
528825e8c5aaSvikram }
528925e8c5aaSvikram 
529025e8c5aaSvikram void
529125e8c5aaSvikram mdi_phci_retire_notify(dev_info_t *dip, int *constraint)
529225e8c5aaSvikram {
529325e8c5aaSvikram 	mdi_phci_t	*ph;
529425e8c5aaSvikram 	mdi_client_t	*ct;
529525e8c5aaSvikram 	mdi_pathinfo_t	*pip;
529625e8c5aaSvikram 	mdi_pathinfo_t	*next;
529725e8c5aaSvikram 	dev_info_t	*cdip;
529825e8c5aaSvikram 
529925e8c5aaSvikram 	if (!MDI_PHCI(dip))
530025e8c5aaSvikram 		return;
530125e8c5aaSvikram 
530225e8c5aaSvikram 	ph = i_devi_get_phci(dip);
530325e8c5aaSvikram 	if (ph == NULL)
530425e8c5aaSvikram 		return;
530525e8c5aaSvikram 
530625e8c5aaSvikram 	MDI_PHCI_LOCK(ph);
530725e8c5aaSvikram 
530825e8c5aaSvikram 	if (MDI_PHCI_IS_OFFLINE(ph)) {
530925e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
531025e8c5aaSvikram 		/* not last path */
531125e8c5aaSvikram 		return;
531225e8c5aaSvikram 	}
531325e8c5aaSvikram 
531425e8c5aaSvikram 	if (ph->ph_unstable) {
531525e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
531625e8c5aaSvikram 		/* can't check for constraints */
531725e8c5aaSvikram 		*constraint = 0;
531825e8c5aaSvikram 		return;
531925e8c5aaSvikram 	}
532025e8c5aaSvikram 
532125e8c5aaSvikram 	pip = ph->ph_path_head;
532225e8c5aaSvikram 	while (pip != NULL) {
532325e8c5aaSvikram 		MDI_PI_LOCK(pip);
532425e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
532525e8c5aaSvikram 
532625e8c5aaSvikram 		/*
532725e8c5aaSvikram 		 * The mdi_pathinfo state is OK. Check the client state.
532825e8c5aaSvikram 		 * If failover in progress fail the pHCI from offlining
532925e8c5aaSvikram 		 */
533025e8c5aaSvikram 		ct = MDI_PI(pip)->pi_client;
533125e8c5aaSvikram 		i_mdi_client_lock(ct, pip);
533225e8c5aaSvikram 		if ((MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) ||
533325e8c5aaSvikram 		    (ct->ct_unstable)) {
533425e8c5aaSvikram 			/*
533525e8c5aaSvikram 			 * Failover is in progress, can't check for constraints
533625e8c5aaSvikram 			 */
533725e8c5aaSvikram 			MDI_PI_UNLOCK(pip);
533825e8c5aaSvikram 			i_mdi_client_unlock(ct);
533925e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
534025e8c5aaSvikram 			*constraint = 0;
534125e8c5aaSvikram 			return;
534225e8c5aaSvikram 		}
534325e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
534425e8c5aaSvikram 
534525e8c5aaSvikram 		/*
534625e8c5aaSvikram 		 * Check to see of we are retiring the last path of this
534725e8c5aaSvikram 		 * client device...
534825e8c5aaSvikram 		 */
534925e8c5aaSvikram 		cdip = ct->ct_dip;
535025e8c5aaSvikram 		if (cdip && (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
535125e8c5aaSvikram 		    (i_mdi_client_compute_state(ct, ph) ==
535225e8c5aaSvikram 		    MDI_CLIENT_STATE_FAILED)) {
535325e8c5aaSvikram 			i_mdi_client_unlock(ct);
535425e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
535525e8c5aaSvikram 			(void) e_ddi_retire_notify(cdip, constraint);
535625e8c5aaSvikram 			MDI_PHCI_LOCK(ph);
535725e8c5aaSvikram 			pip = next;
535825e8c5aaSvikram 		} else {
535925e8c5aaSvikram 			i_mdi_client_unlock(ct);
536025e8c5aaSvikram 			pip = next;
536125e8c5aaSvikram 		}
536225e8c5aaSvikram 	}
536325e8c5aaSvikram 
536425e8c5aaSvikram 	MDI_PHCI_UNLOCK(ph);
536525e8c5aaSvikram 
536625e8c5aaSvikram 	return;
536725e8c5aaSvikram }
536825e8c5aaSvikram 
536925e8c5aaSvikram /*
53704c06356bSdh142964  * offline the path(s) hanging off the pHCI. If the
537125e8c5aaSvikram  * last path to any client, check that constraints
537225e8c5aaSvikram  * have been applied.
5373*bf002425SStephen Hanson  *
5374*bf002425SStephen Hanson  * If constraint is 0, we aren't going to retire the
5375*bf002425SStephen Hanson  * pHCI. However we still need to go through the paths
5376*bf002425SStephen Hanson  * calling e_ddi_retire_finalize() to clear their
5377*bf002425SStephen Hanson  * contract barriers.
537825e8c5aaSvikram  */
537925e8c5aaSvikram void
5380*bf002425SStephen Hanson mdi_phci_retire_finalize(dev_info_t *dip, int phci_only, void *constraint)
538125e8c5aaSvikram {
538225e8c5aaSvikram 	mdi_phci_t	*ph;
538325e8c5aaSvikram 	mdi_client_t	*ct;
538425e8c5aaSvikram 	mdi_pathinfo_t	*pip;
538525e8c5aaSvikram 	mdi_pathinfo_t	*next;
538625e8c5aaSvikram 	dev_info_t	*cdip;
538725e8c5aaSvikram 	int		unstable = 0;
5388*bf002425SStephen Hanson 	int		tmp_constraint;
538925e8c5aaSvikram 
539025e8c5aaSvikram 	if (!MDI_PHCI(dip))
539125e8c5aaSvikram 		return;
539225e8c5aaSvikram 
539325e8c5aaSvikram 	ph = i_devi_get_phci(dip);
539425e8c5aaSvikram 	if (ph == NULL) {
539525e8c5aaSvikram 		/* no last path and no pips */
539625e8c5aaSvikram 		return;
539725e8c5aaSvikram 	}
539825e8c5aaSvikram 
539925e8c5aaSvikram 	MDI_PHCI_LOCK(ph);
540025e8c5aaSvikram 
540125e8c5aaSvikram 	if (MDI_PHCI_IS_OFFLINE(ph)) {
540225e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
540325e8c5aaSvikram 		/* no last path and no pips */
540425e8c5aaSvikram 		return;
540525e8c5aaSvikram 	}
540625e8c5aaSvikram 
540725e8c5aaSvikram 	/*
540825e8c5aaSvikram 	 * Check to see if the pHCI can be offlined
540925e8c5aaSvikram 	 */
541025e8c5aaSvikram 	if (ph->ph_unstable) {
541125e8c5aaSvikram 		unstable = 1;
541225e8c5aaSvikram 	}
541325e8c5aaSvikram 
541425e8c5aaSvikram 	pip = ph->ph_path_head;
541525e8c5aaSvikram 	while (pip != NULL) {
541625e8c5aaSvikram 		MDI_PI_LOCK(pip);
541725e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
541825e8c5aaSvikram 
541925e8c5aaSvikram 		/*
542025e8c5aaSvikram 		 * if failover in progress fail the pHCI from offlining
542125e8c5aaSvikram 		 */
542225e8c5aaSvikram 		ct = MDI_PI(pip)->pi_client;
542325e8c5aaSvikram 		i_mdi_client_lock(ct, pip);
542425e8c5aaSvikram 		if ((MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) ||
542525e8c5aaSvikram 		    (ct->ct_unstable)) {
542625e8c5aaSvikram 			unstable = 1;
542725e8c5aaSvikram 		}
542825e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
542925e8c5aaSvikram 
543025e8c5aaSvikram 		/*
543125e8c5aaSvikram 		 * Check to see of we are removing the last path of this
543225e8c5aaSvikram 		 * client device...
543325e8c5aaSvikram 		 */
543425e8c5aaSvikram 		cdip = ct->ct_dip;
543525e8c5aaSvikram 		if (!phci_only && cdip &&
543625e8c5aaSvikram 		    (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
543725e8c5aaSvikram 		    (i_mdi_client_compute_state(ct, ph) ==
543825e8c5aaSvikram 		    MDI_CLIENT_STATE_FAILED)) {
543925e8c5aaSvikram 			i_mdi_client_unlock(ct);
544025e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
544125e8c5aaSvikram 			/*
5442*bf002425SStephen Hanson 			 * This is the last path to this client.
5443*bf002425SStephen Hanson 			 *
5444*bf002425SStephen Hanson 			 * Constraint will only be set to 1 if this client can
5445*bf002425SStephen Hanson 			 * be retired (as already determined by
5446*bf002425SStephen Hanson 			 * mdi_phci_retire_notify). However we don't actually
5447*bf002425SStephen Hanson 			 * need to retire the client (we just retire the last
5448*bf002425SStephen Hanson 			 * path - MPXIO will then fail all I/Os to the client).
5449*bf002425SStephen Hanson 			 * But we still need to call e_ddi_retire_finalize so
5450*bf002425SStephen Hanson 			 * the contract barriers can be cleared. Therefore we
5451*bf002425SStephen Hanson 			 * temporarily set constraint = 0 so that the client
5452*bf002425SStephen Hanson 			 * dip is not retired.
545325e8c5aaSvikram 			 */
5454*bf002425SStephen Hanson 			tmp_constraint = 0;
5455*bf002425SStephen Hanson 			(void) e_ddi_retire_finalize(cdip, &tmp_constraint);
545625e8c5aaSvikram 			MDI_PHCI_LOCK(ph);
545725e8c5aaSvikram 			pip = next;
545825e8c5aaSvikram 		} else {
545925e8c5aaSvikram 			i_mdi_client_unlock(ct);
546025e8c5aaSvikram 			pip = next;
546125e8c5aaSvikram 		}
546225e8c5aaSvikram 	}
546325e8c5aaSvikram 
5464*bf002425SStephen Hanson 	if (!phci_only && *((int *)constraint) == 0) {
5465*bf002425SStephen Hanson 		MDI_PHCI_UNLOCK(ph);
5466*bf002425SStephen Hanson 		return;
5467*bf002425SStephen Hanson 	}
5468*bf002425SStephen Hanson 
546925e8c5aaSvikram 	/*
547025e8c5aaSvikram 	 * Cannot offline pip(s)
547125e8c5aaSvikram 	 */
547225e8c5aaSvikram 	if (unstable) {
54734c06356bSdh142964 		cmn_err(CE_WARN, "%s%d: mdi_phci_retire_finalize: "
54744c06356bSdh142964 		    "pHCI in transient state, cannot retire",
54754c06356bSdh142964 		    ddi_driver_name(dip), ddi_get_instance(dip));
547625e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
547725e8c5aaSvikram 		return;
547825e8c5aaSvikram 	}
547925e8c5aaSvikram 
548025e8c5aaSvikram 	/*
548125e8c5aaSvikram 	 * Mark the pHCI as offline
548225e8c5aaSvikram 	 */
548325e8c5aaSvikram 	MDI_PHCI_SET_OFFLINE(ph);
548425e8c5aaSvikram 
548525e8c5aaSvikram 	/*
548625e8c5aaSvikram 	 * Mark the child mdi_pathinfo nodes as transient
548725e8c5aaSvikram 	 */
548825e8c5aaSvikram 	pip = ph->ph_path_head;
548925e8c5aaSvikram 	while (pip != NULL) {
549025e8c5aaSvikram 		MDI_PI_LOCK(pip);
549125e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
549225e8c5aaSvikram 		MDI_PI_SET_OFFLINING(pip);
549325e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
549425e8c5aaSvikram 		pip = next;
549525e8c5aaSvikram 	}
549625e8c5aaSvikram 	MDI_PHCI_UNLOCK(ph);
549725e8c5aaSvikram 	/*
549825e8c5aaSvikram 	 * Give a chance for any pending commands to execute
549925e8c5aaSvikram 	 */
550096c4a178SChris Horne 	delay_random(mdi_delay);
550125e8c5aaSvikram 	MDI_PHCI_LOCK(ph);
550225e8c5aaSvikram 	pip = ph->ph_path_head;
550325e8c5aaSvikram 	while (pip != NULL) {
550425e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
550525e8c5aaSvikram 		(void) i_mdi_pi_offline(pip, 0);
550625e8c5aaSvikram 		MDI_PI_LOCK(pip);
550725e8c5aaSvikram 		ct = MDI_PI(pip)->pi_client;
550825e8c5aaSvikram 		if (!MDI_PI_IS_OFFLINE(pip)) {
55094c06356bSdh142964 			cmn_err(CE_WARN, "mdi_phci_retire_finalize: "
55104c06356bSdh142964 			    "path %d %s busy, cannot offline",
55114c06356bSdh142964 			    mdi_pi_get_path_instance(pip),
55124c06356bSdh142964 			    mdi_pi_spathname(pip));
551325e8c5aaSvikram 			MDI_PI_UNLOCK(pip);
551425e8c5aaSvikram 			MDI_PHCI_SET_ONLINE(ph);
551525e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
551625e8c5aaSvikram 			return;
551725e8c5aaSvikram 		}
551825e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
551925e8c5aaSvikram 		pip = next;
552025e8c5aaSvikram 	}
552125e8c5aaSvikram 	MDI_PHCI_UNLOCK(ph);
552225e8c5aaSvikram 
552325e8c5aaSvikram 	return;
552425e8c5aaSvikram }
552525e8c5aaSvikram 
552625e8c5aaSvikram void
552725e8c5aaSvikram mdi_phci_unretire(dev_info_t *dip)
552825e8c5aaSvikram {
552925e8c5aaSvikram 	ASSERT(MDI_PHCI(dip));
553025e8c5aaSvikram 
553125e8c5aaSvikram 	/*
553225e8c5aaSvikram 	 * Online the phci
553325e8c5aaSvikram 	 */
553425e8c5aaSvikram 	i_mdi_phci_online(dip);
553525e8c5aaSvikram }
553625e8c5aaSvikram 
55377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
55387c478bd9Sstevel@tonic-gate static int
55397c478bd9Sstevel@tonic-gate i_mdi_client_offline(dev_info_t *dip, uint_t flags)
55407c478bd9Sstevel@tonic-gate {
55417c478bd9Sstevel@tonic-gate 	int		rv = NDI_SUCCESS;
55427c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
55437c478bd9Sstevel@tonic-gate 
55447c478bd9Sstevel@tonic-gate 	/*
55457c478bd9Sstevel@tonic-gate 	 * Client component to go offline.  Make sure that we are
55467c478bd9Sstevel@tonic-gate 	 * not in failing over state and update client state
55477c478bd9Sstevel@tonic-gate 	 * accordingly
55487c478bd9Sstevel@tonic-gate 	 */
55497c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(dip);
55504c06356bSdh142964 	MDI_DEBUG(2, (MDI_NOTE, dip,
55514c06356bSdh142964 	    "called %p %p", (void *)dip, (void *)ct));
55527c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
55537c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
55547c478bd9Sstevel@tonic-gate 		if (ct->ct_unstable) {
55557c478bd9Sstevel@tonic-gate 			/*
55567c478bd9Sstevel@tonic-gate 			 * One or more paths are in transient state,
55577c478bd9Sstevel@tonic-gate 			 * Dont allow offline of a client device
55587c478bd9Sstevel@tonic-gate 			 */
55594c06356bSdh142964 			MDI_DEBUG(1, (MDI_WARN, dip,
55604c06356bSdh142964 			    "!One or more paths to "
55614c06356bSdh142964 			    "this device are in transient state. "
55624c06356bSdh142964 			    "This device can not be removed at this moment. "
55637c478bd9Sstevel@tonic-gate 			    "Please try again later."));
55647c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
55657c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
55667c478bd9Sstevel@tonic-gate 		}
55677c478bd9Sstevel@tonic-gate 		if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
55687c478bd9Sstevel@tonic-gate 			/*
55697c478bd9Sstevel@tonic-gate 			 * Failover is in progress, Dont allow DR of
55707c478bd9Sstevel@tonic-gate 			 * a client device
55717c478bd9Sstevel@tonic-gate 			 */
55724c06356bSdh142964 			MDI_DEBUG(1, (MDI_WARN, dip,
55734c06356bSdh142964 			    "!Client device is Busy. "
55744c06356bSdh142964 			    "This device can not be removed at this moment. "
55754c06356bSdh142964 			    "Please try again later."));
55767c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
55777c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
55787c478bd9Sstevel@tonic-gate 		}
55797c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_OFFLINE(ct);
55807c478bd9Sstevel@tonic-gate 
55817c478bd9Sstevel@tonic-gate 		/*
55827c478bd9Sstevel@tonic-gate 		 * Unbind our relationship with the dev_info node
55837c478bd9Sstevel@tonic-gate 		 */
55847c478bd9Sstevel@tonic-gate 		if (flags & NDI_DEVI_REMOVE) {
55857c478bd9Sstevel@tonic-gate 			ct->ct_dip = NULL;
55867c478bd9Sstevel@tonic-gate 		}
55877c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
55887c478bd9Sstevel@tonic-gate 	}
55897c478bd9Sstevel@tonic-gate 	return (rv);
55907c478bd9Sstevel@tonic-gate }
55917c478bd9Sstevel@tonic-gate 
55927c478bd9Sstevel@tonic-gate /*
55937c478bd9Sstevel@tonic-gate  * mdi_pre_attach():
55947c478bd9Sstevel@tonic-gate  *		Pre attach() notification handler
55957c478bd9Sstevel@tonic-gate  */
55967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
55977c478bd9Sstevel@tonic-gate int
55987c478bd9Sstevel@tonic-gate mdi_pre_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
55997c478bd9Sstevel@tonic-gate {
56007c478bd9Sstevel@tonic-gate 	/* don't support old DDI_PM_RESUME */
56017c478bd9Sstevel@tonic-gate 	if ((DEVI(dip)->devi_mdi_component != MDI_COMPONENT_NONE) &&
56027c478bd9Sstevel@tonic-gate 	    (cmd == DDI_PM_RESUME))
56037c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
56047c478bd9Sstevel@tonic-gate 
56057c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
56067c478bd9Sstevel@tonic-gate }
56077c478bd9Sstevel@tonic-gate 
56087c478bd9Sstevel@tonic-gate /*
56097c478bd9Sstevel@tonic-gate  * mdi_post_attach():
56107c478bd9Sstevel@tonic-gate  *		Post attach() notification handler
56117c478bd9Sstevel@tonic-gate  */
56127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
56137c478bd9Sstevel@tonic-gate void
56147c478bd9Sstevel@tonic-gate mdi_post_attach(dev_info_t *dip, ddi_attach_cmd_t cmd, int error)
56157c478bd9Sstevel@tonic-gate {
56167c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
56177c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
561837fbbce5Scth 	mdi_vhci_t	*vh;
56197c478bd9Sstevel@tonic-gate 
56207c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
56217c478bd9Sstevel@tonic-gate 		ph = i_devi_get_phci(dip);
56227c478bd9Sstevel@tonic-gate 		ASSERT(ph != NULL);
56237c478bd9Sstevel@tonic-gate 
56247c478bd9Sstevel@tonic-gate 		MDI_PHCI_LOCK(ph);
56257c478bd9Sstevel@tonic-gate 		switch (cmd) {
56267c478bd9Sstevel@tonic-gate 		case DDI_ATTACH:
56274c06356bSdh142964 			MDI_DEBUG(2, (MDI_NOTE, dip,
56284c06356bSdh142964 			    "phci post_attach called %p", (void *)ph));
56297c478bd9Sstevel@tonic-gate 			if (error == DDI_SUCCESS) {
56307c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_ATTACH(ph);
56317c478bd9Sstevel@tonic-gate 			} else {
56324c06356bSdh142964 				MDI_DEBUG(1, (MDI_NOTE, dip,
56334c06356bSdh142964 				    "!pHCI post_attach failed: error %d",
56347c478bd9Sstevel@tonic-gate 				    error));
56357c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_DETACH(ph);
56367c478bd9Sstevel@tonic-gate 			}
56377c478bd9Sstevel@tonic-gate 			break;
56387c478bd9Sstevel@tonic-gate 
56397c478bd9Sstevel@tonic-gate 		case DDI_RESUME:
56404c06356bSdh142964 			MDI_DEBUG(2, (MDI_NOTE, dip,
56414c06356bSdh142964 			    "pHCI post_resume: called %p", (void *)ph));
56427c478bd9Sstevel@tonic-gate 			if (error == DDI_SUCCESS) {
56437c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_RESUME(ph);
56447c478bd9Sstevel@tonic-gate 			} else {
56454c06356bSdh142964 				MDI_DEBUG(1, (MDI_NOTE, dip,
56464c06356bSdh142964 				    "!pHCI post_resume failed: error %d",
56477c478bd9Sstevel@tonic-gate 				    error));
56487c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_SUSPEND(ph);
56497c478bd9Sstevel@tonic-gate 			}
56507c478bd9Sstevel@tonic-gate 			break;
56517c478bd9Sstevel@tonic-gate 		}
56527c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
56537c478bd9Sstevel@tonic-gate 	}
56547c478bd9Sstevel@tonic-gate 
56557c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
56567c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(dip);
56577c478bd9Sstevel@tonic-gate 		ASSERT(ct != NULL);
56587c478bd9Sstevel@tonic-gate 
56597c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
56607c478bd9Sstevel@tonic-gate 		switch (cmd) {
56617c478bd9Sstevel@tonic-gate 		case DDI_ATTACH:
56624c06356bSdh142964 			MDI_DEBUG(2, (MDI_NOTE, dip,
56634c06356bSdh142964 			    "client post_attach called %p", (void *)ct));
56647c478bd9Sstevel@tonic-gate 			if (error != DDI_SUCCESS) {
56654c06356bSdh142964 				MDI_DEBUG(1, (MDI_NOTE, dip,
56664c06356bSdh142964 				    "!client post_attach failed: error %d",
56677c478bd9Sstevel@tonic-gate 				    error));
56687c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_DETACH(ct);
56694c06356bSdh142964 				MDI_DEBUG(4, (MDI_WARN, dip,
56704c06356bSdh142964 				    "i_mdi_pm_reset_client"));
56717c478bd9Sstevel@tonic-gate 				i_mdi_pm_reset_client(ct);
56727c478bd9Sstevel@tonic-gate 				break;
56737c478bd9Sstevel@tonic-gate 			}
56747c478bd9Sstevel@tonic-gate 
56757c478bd9Sstevel@tonic-gate 			/*
567637fbbce5Scth 			 * Client device has successfully attached, inform
567737fbbce5Scth 			 * the vhci.
56787c478bd9Sstevel@tonic-gate 			 */
567937fbbce5Scth 			vh = ct->ct_vhci;
568037fbbce5Scth 			if (vh->vh_ops->vo_client_attached)
568137fbbce5Scth 				(*vh->vh_ops->vo_client_attached)(dip);
568237fbbce5Scth 
56837c478bd9Sstevel@tonic-gate 			MDI_CLIENT_SET_ATTACH(ct);
56847c478bd9Sstevel@tonic-gate 			break;
56857c478bd9Sstevel@tonic-gate 
56867c478bd9Sstevel@tonic-gate 		case DDI_RESUME:
56874c06356bSdh142964 			MDI_DEBUG(2, (MDI_NOTE, dip,
56884c06356bSdh142964 			    "client post_attach: called %p", (void *)ct));
56897c478bd9Sstevel@tonic-gate 			if (error == DDI_SUCCESS) {
56907c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_RESUME(ct);
56917c478bd9Sstevel@tonic-gate 			} else {
56924c06356bSdh142964 				MDI_DEBUG(1, (MDI_NOTE, dip,
56934c06356bSdh142964 				    "!client post_resume failed: error %d",
56947c478bd9Sstevel@tonic-gate 				    error));
56957c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_SUSPEND(ct);
56967c478bd9Sstevel@tonic-gate 			}
56977c478bd9Sstevel@tonic-gate 			break;
56987c478bd9Sstevel@tonic-gate 		}
56997c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
57007c478bd9Sstevel@tonic-gate 	}
57017c478bd9Sstevel@tonic-gate }
57027c478bd9Sstevel@tonic-gate 
57037c478bd9Sstevel@tonic-gate /*
57047c478bd9Sstevel@tonic-gate  * mdi_pre_detach():
57057c478bd9Sstevel@tonic-gate  *		Pre detach notification handler
57067c478bd9Sstevel@tonic-gate  */
57077c478bd9Sstevel@tonic-gate /*ARGSUSED*/
57087c478bd9Sstevel@tonic-gate int
57097c478bd9Sstevel@tonic-gate mdi_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
57107c478bd9Sstevel@tonic-gate {
57117c478bd9Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
57127c478bd9Sstevel@tonic-gate 
57137c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
57147c478bd9Sstevel@tonic-gate 		(void) i_mdi_client_pre_detach(dip, cmd);
57157c478bd9Sstevel@tonic-gate 	}
57167c478bd9Sstevel@tonic-gate 
57177c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
57187c478bd9Sstevel@tonic-gate 		rv = i_mdi_phci_pre_detach(dip, cmd);
57197c478bd9Sstevel@tonic-gate 	}
57207c478bd9Sstevel@tonic-gate 
57217c478bd9Sstevel@tonic-gate 	return (rv);
57227c478bd9Sstevel@tonic-gate }
57237c478bd9Sstevel@tonic-gate 
57247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
57257c478bd9Sstevel@tonic-gate static int
57267c478bd9Sstevel@tonic-gate i_mdi_phci_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
57277c478bd9Sstevel@tonic-gate {
57287c478bd9Sstevel@tonic-gate 	int		rv = DDI_SUCCESS;
57297c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
57307c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
57317c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
57327c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*failed_pip = NULL;
57337c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
57347c478bd9Sstevel@tonic-gate 
57357c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(dip);
57367c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
57377c478bd9Sstevel@tonic-gate 		return (rv);
57387c478bd9Sstevel@tonic-gate 	}
57397c478bd9Sstevel@tonic-gate 
57407c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
57417c478bd9Sstevel@tonic-gate 	switch (cmd) {
57427c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
57434c06356bSdh142964 		MDI_DEBUG(2, (MDI_NOTE, dip,
57444c06356bSdh142964 		    "pHCI pre_detach: called %p", (void *)ph));
57457c478bd9Sstevel@tonic-gate 		if (!MDI_PHCI_IS_OFFLINE(ph)) {
57467c478bd9Sstevel@tonic-gate 			/*
57477c478bd9Sstevel@tonic-gate 			 * mdi_pathinfo nodes are still attached to
57487c478bd9Sstevel@tonic-gate 			 * this pHCI. Fail the detach for this pHCI.
57497c478bd9Sstevel@tonic-gate 			 */
57504c06356bSdh142964 			MDI_DEBUG(2, (MDI_WARN, dip,
57514c06356bSdh142964 			    "pHCI pre_detach: paths are still attached %p",
57524c06356bSdh142964 			    (void *)ph));
57537c478bd9Sstevel@tonic-gate 			rv = DDI_FAILURE;
57547c478bd9Sstevel@tonic-gate 			break;
57557c478bd9Sstevel@tonic-gate 		}
57567c478bd9Sstevel@tonic-gate 		MDI_PHCI_SET_DETACH(ph);
57577c478bd9Sstevel@tonic-gate 		break;
57587c478bd9Sstevel@tonic-gate 
57597c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
57607c478bd9Sstevel@tonic-gate 		/*
57617c478bd9Sstevel@tonic-gate 		 * pHCI is getting suspended.  Since mpxio client
57627c478bd9Sstevel@tonic-gate 		 * devices may not be suspended at this point, to avoid
57637c478bd9Sstevel@tonic-gate 		 * a potential stack overflow, it is important to suspend
57647c478bd9Sstevel@tonic-gate 		 * client devices before pHCI can be suspended.
57657c478bd9Sstevel@tonic-gate 		 */
57667c478bd9Sstevel@tonic-gate 
57674c06356bSdh142964 		MDI_DEBUG(2, (MDI_NOTE, dip,
57684c06356bSdh142964 		    "pHCI pre_suspend: called %p", (void *)ph));
57697c478bd9Sstevel@tonic-gate 		/*
57707c478bd9Sstevel@tonic-gate 		 * Suspend all the client devices accessible through this pHCI
57717c478bd9Sstevel@tonic-gate 		 */
57727c478bd9Sstevel@tonic-gate 		pip = ph->ph_path_head;
57737c478bd9Sstevel@tonic-gate 		while (pip != NULL && rv == DDI_SUCCESS) {
57747c478bd9Sstevel@tonic-gate 			dev_info_t *cdip;
57757c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
57767c478bd9Sstevel@tonic-gate 			next =
57777c478bd9Sstevel@tonic-gate 			    (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
57787c478bd9Sstevel@tonic-gate 			ct = MDI_PI(pip)->pi_client;
57797c478bd9Sstevel@tonic-gate 			i_mdi_client_lock(ct, pip);
57807c478bd9Sstevel@tonic-gate 			cdip = ct->ct_dip;
57817c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
57827c478bd9Sstevel@tonic-gate 			if ((MDI_CLIENT_IS_DETACHED(ct) == 0) &&
57837c478bd9Sstevel@tonic-gate 			    MDI_CLIENT_IS_SUSPENDED(ct) == 0) {
57847c478bd9Sstevel@tonic-gate 				i_mdi_client_unlock(ct);
57857c478bd9Sstevel@tonic-gate 				if ((rv = devi_detach(cdip, DDI_SUSPEND)) !=
57867c478bd9Sstevel@tonic-gate 				    DDI_SUCCESS) {
57877c478bd9Sstevel@tonic-gate 					/*
57887c478bd9Sstevel@tonic-gate 					 * Suspend of one of the client
57897c478bd9Sstevel@tonic-gate 					 * device has failed.
57907c478bd9Sstevel@tonic-gate 					 */
57914c06356bSdh142964 					MDI_DEBUG(1, (MDI_WARN, dip,
57924c06356bSdh142964 					    "!suspend of device (%s%d) failed.",
57937c478bd9Sstevel@tonic-gate 					    ddi_driver_name(cdip),
57947c478bd9Sstevel@tonic-gate 					    ddi_get_instance(cdip)));
57957c478bd9Sstevel@tonic-gate 					failed_pip = pip;
57967c478bd9Sstevel@tonic-gate 					break;
57977c478bd9Sstevel@tonic-gate 				}
57987c478bd9Sstevel@tonic-gate 			} else {
57997c478bd9Sstevel@tonic-gate 				i_mdi_client_unlock(ct);
58007c478bd9Sstevel@tonic-gate 			}
58017c478bd9Sstevel@tonic-gate 			pip = next;
58027c478bd9Sstevel@tonic-gate 		}
58037c478bd9Sstevel@tonic-gate 
58047c478bd9Sstevel@tonic-gate 		if (rv == DDI_SUCCESS) {
58057c478bd9Sstevel@tonic-gate 			/*
58067c478bd9Sstevel@tonic-gate 			 * Suspend of client devices is complete. Proceed
58077c478bd9Sstevel@tonic-gate 			 * with pHCI suspend.
58087c478bd9Sstevel@tonic-gate 			 */
58097c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_SUSPEND(ph);
58107c478bd9Sstevel@tonic-gate 		} else {
58117c478bd9Sstevel@tonic-gate 			/*
58127c478bd9Sstevel@tonic-gate 			 * Revert back all the suspended client device states
58137c478bd9Sstevel@tonic-gate 			 * to converse.
58147c478bd9Sstevel@tonic-gate 			 */
58157c478bd9Sstevel@tonic-gate 			pip = ph->ph_path_head;
58167c478bd9Sstevel@tonic-gate 			while (pip != failed_pip) {
58177c478bd9Sstevel@tonic-gate 				dev_info_t *cdip;
58187c478bd9Sstevel@tonic-gate 				MDI_PI_LOCK(pip);
58197c478bd9Sstevel@tonic-gate 				next =
58207c478bd9Sstevel@tonic-gate 				    (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
58217c478bd9Sstevel@tonic-gate 				ct = MDI_PI(pip)->pi_client;
58227c478bd9Sstevel@tonic-gate 				i_mdi_client_lock(ct, pip);
58237c478bd9Sstevel@tonic-gate 				cdip = ct->ct_dip;
58247c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
58257c478bd9Sstevel@tonic-gate 				if (MDI_CLIENT_IS_SUSPENDED(ct)) {
58267c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
58277c478bd9Sstevel@tonic-gate 					(void) devi_attach(cdip, DDI_RESUME);
58287c478bd9Sstevel@tonic-gate 				} else {
58297c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
58307c478bd9Sstevel@tonic-gate 				}
58317c478bd9Sstevel@tonic-gate 				pip = next;
58327c478bd9Sstevel@tonic-gate 			}
58337c478bd9Sstevel@tonic-gate 		}
58347c478bd9Sstevel@tonic-gate 		break;
58357c478bd9Sstevel@tonic-gate 
58367c478bd9Sstevel@tonic-gate 	default:
58377c478bd9Sstevel@tonic-gate 		rv = DDI_FAILURE;
58387c478bd9Sstevel@tonic-gate 		break;
58397c478bd9Sstevel@tonic-gate 	}
58407c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
58417c478bd9Sstevel@tonic-gate 	return (rv);
58427c478bd9Sstevel@tonic-gate }
58437c478bd9Sstevel@tonic-gate 
58447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
58457c478bd9Sstevel@tonic-gate static int
58467c478bd9Sstevel@tonic-gate i_mdi_client_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
58477c478bd9Sstevel@tonic-gate {
58487c478bd9Sstevel@tonic-gate 	int		rv = DDI_SUCCESS;
58497c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
58507c478bd9Sstevel@tonic-gate 
58517c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(dip);
58527c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
58537c478bd9Sstevel@tonic-gate 		return (rv);
58547c478bd9Sstevel@tonic-gate 	}
58557c478bd9Sstevel@tonic-gate 
58567c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
58577c478bd9Sstevel@tonic-gate 	switch (cmd) {
58587c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
58594c06356bSdh142964 		MDI_DEBUG(2, (MDI_NOTE, dip,
58604c06356bSdh142964 		    "client pre_detach: called %p",
58614c06356bSdh142964 		     (void *)ct));
58627c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_DETACH(ct);
58637c478bd9Sstevel@tonic-gate 		break;
58647c478bd9Sstevel@tonic-gate 
58657c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
58664c06356bSdh142964 		MDI_DEBUG(2, (MDI_NOTE, dip,
58674c06356bSdh142964 		    "client pre_suspend: called %p",
58684c06356bSdh142964 		    (void *)ct));
58697c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_SUSPEND(ct);
58707c478bd9Sstevel@tonic-gate 		break;
58717c478bd9Sstevel@tonic-gate 
58727c478bd9Sstevel@tonic-gate 	default:
58737c478bd9Sstevel@tonic-gate 		rv = DDI_FAILURE;
58747c478bd9Sstevel@tonic-gate 		break;
58757c478bd9Sstevel@tonic-gate 	}
58767c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
58777c478bd9Sstevel@tonic-gate 	return (rv);
58787c478bd9Sstevel@tonic-gate }
58797c478bd9Sstevel@tonic-gate 
58807c478bd9Sstevel@tonic-gate /*
58817c478bd9Sstevel@tonic-gate  * mdi_post_detach():
58827c478bd9Sstevel@tonic-gate  *		Post detach notification handler
58837c478bd9Sstevel@tonic-gate  */
58847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
58857c478bd9Sstevel@tonic-gate void
58867c478bd9Sstevel@tonic-gate mdi_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
58877c478bd9Sstevel@tonic-gate {
58887c478bd9Sstevel@tonic-gate 	/*
58897c478bd9Sstevel@tonic-gate 	 * Detach/Suspend of mpxio component failed. Update our state
58907c478bd9Sstevel@tonic-gate 	 * too
58917c478bd9Sstevel@tonic-gate 	 */
58927c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip))
58937c478bd9Sstevel@tonic-gate 		i_mdi_phci_post_detach(dip, cmd, error);
58947c478bd9Sstevel@tonic-gate 
58957c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip))
58967c478bd9Sstevel@tonic-gate 		i_mdi_client_post_detach(dip, cmd, error);
58977c478bd9Sstevel@tonic-gate }
58987c478bd9Sstevel@tonic-gate 
58997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
59007c478bd9Sstevel@tonic-gate static void
59017c478bd9Sstevel@tonic-gate i_mdi_phci_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
59027c478bd9Sstevel@tonic-gate {
59037c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
59047c478bd9Sstevel@tonic-gate 
59057c478bd9Sstevel@tonic-gate 	/*
59067c478bd9Sstevel@tonic-gate 	 * Detach/Suspend of phci component failed. Update our state
59077c478bd9Sstevel@tonic-gate 	 * too
59087c478bd9Sstevel@tonic-gate 	 */
59097c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(dip);
59107c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
59117c478bd9Sstevel@tonic-gate 		return;
59127c478bd9Sstevel@tonic-gate 	}
59137c478bd9Sstevel@tonic-gate 
59147c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
59157c478bd9Sstevel@tonic-gate 	/*
59167c478bd9Sstevel@tonic-gate 	 * Detach of pHCI failed. Restore back converse
59177c478bd9Sstevel@tonic-gate 	 * state
59187c478bd9Sstevel@tonic-gate 	 */
59197c478bd9Sstevel@tonic-gate 	switch (cmd) {
59207c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
59214c06356bSdh142964 		MDI_DEBUG(2, (MDI_NOTE, dip,
59224c06356bSdh142964 		    "pHCI post_detach: called %p",
59234c06356bSdh142964 		    (void *)ph));
59247c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
59257c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_ATTACH(ph);
59267c478bd9Sstevel@tonic-gate 		break;
59277c478bd9Sstevel@tonic-gate 
59287c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
59294c06356bSdh142964 		MDI_DEBUG(2, (MDI_NOTE, dip,
59304c06356bSdh142964 		    "pHCI post_suspend: called %p",
59314c06356bSdh142964 		    (void *)ph));
59327c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
59337c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_RESUME(ph);
59347c478bd9Sstevel@tonic-gate 		break;
59357c478bd9Sstevel@tonic-gate 	}
59367c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
59377c478bd9Sstevel@tonic-gate }
59387c478bd9Sstevel@tonic-gate 
59397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
59407c478bd9Sstevel@tonic-gate static void
59417c478bd9Sstevel@tonic-gate i_mdi_client_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
59427c478bd9Sstevel@tonic-gate {
59437c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
59447c478bd9Sstevel@tonic-gate 
59457c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(dip);
59467c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
59477c478bd9Sstevel@tonic-gate 		return;
59487c478bd9Sstevel@tonic-gate 	}
59497c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
59507c478bd9Sstevel@tonic-gate 	/*
59517c478bd9Sstevel@tonic-gate 	 * Detach of Client failed. Restore back converse
59527c478bd9Sstevel@tonic-gate 	 * state
59537c478bd9Sstevel@tonic-gate 	 */
59547c478bd9Sstevel@tonic-gate 	switch (cmd) {
59557c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
59564c06356bSdh142964 		MDI_DEBUG(2, (MDI_NOTE, dip,
59574c06356bSdh142964 		    "client post_detach: called %p", (void *)ct));
59587c478bd9Sstevel@tonic-gate 		if (DEVI_IS_ATTACHING(ct->ct_dip)) {
59594c06356bSdh142964 			MDI_DEBUG(4, (MDI_NOTE, dip,
59607c478bd9Sstevel@tonic-gate 			    "i_mdi_pm_rele_client\n"));
59617c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, ct->ct_path_count);
59627c478bd9Sstevel@tonic-gate 		} else {
59634c06356bSdh142964 			MDI_DEBUG(4, (MDI_NOTE, dip,
59647c478bd9Sstevel@tonic-gate 			    "i_mdi_pm_reset_client\n"));
59657c478bd9Sstevel@tonic-gate 			i_mdi_pm_reset_client(ct);
59667c478bd9Sstevel@tonic-gate 		}
59677c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
59687c478bd9Sstevel@tonic-gate 			MDI_CLIENT_SET_ATTACH(ct);
59697c478bd9Sstevel@tonic-gate 		break;
59707c478bd9Sstevel@tonic-gate 
59717c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
59724c06356bSdh142964 		MDI_DEBUG(2, (MDI_NOTE, dip,
59734c06356bSdh142964 		    "called %p", (void *)ct));
59747c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
59757c478bd9Sstevel@tonic-gate 			MDI_CLIENT_SET_RESUME(ct);
59767c478bd9Sstevel@tonic-gate 		break;
59777c478bd9Sstevel@tonic-gate 	}
59787c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
59797c478bd9Sstevel@tonic-gate }
59807c478bd9Sstevel@tonic-gate 
598137fbbce5Scth int
598237fbbce5Scth mdi_pi_kstat_exists(mdi_pathinfo_t *pip)
598337fbbce5Scth {
598437fbbce5Scth 	return (MDI_PI(pip)->pi_kstats ? 1 : 0);
598537fbbce5Scth }
598637fbbce5Scth 
59877c478bd9Sstevel@tonic-gate /*
59887c478bd9Sstevel@tonic-gate  * create and install per-path (client - pHCI) statistics
59897c478bd9Sstevel@tonic-gate  * I/O stats supported: nread, nwritten, reads, and writes
59907c478bd9Sstevel@tonic-gate  * Error stats - hard errors, soft errors, & transport errors
59917c478bd9Sstevel@tonic-gate  */
599237fbbce5Scth int
599337fbbce5Scth mdi_pi_kstat_create(mdi_pathinfo_t *pip, char *ksname)
59947c478bd9Sstevel@tonic-gate {
59957c478bd9Sstevel@tonic-gate 	kstat_t			*kiosp, *kerrsp;
59967c478bd9Sstevel@tonic-gate 	struct pi_errs		*nsp;
59977c478bd9Sstevel@tonic-gate 	struct mdi_pi_kstats	*mdi_statp;
59987c478bd9Sstevel@tonic-gate 
59997c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_kstats != NULL)
60007c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
60017c478bd9Sstevel@tonic-gate 
60027c478bd9Sstevel@tonic-gate 	if ((kiosp = kstat_create("mdi", 0, ksname, "iopath",
600337fbbce5Scth 	    KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT)) == NULL) {
60047c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
60057c478bd9Sstevel@tonic-gate 	}
60067c478bd9Sstevel@tonic-gate 
600737fbbce5Scth 	(void) strcat(ksname, ",err");
60087c478bd9Sstevel@tonic-gate 	kerrsp = kstat_create("mdi", 0, ksname, "iopath_errors",
60097c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED,
60107c478bd9Sstevel@tonic-gate 	    sizeof (struct pi_errs) / sizeof (kstat_named_t), 0);
60117c478bd9Sstevel@tonic-gate 	if (kerrsp == NULL) {
60127c478bd9Sstevel@tonic-gate 		kstat_delete(kiosp);
60137c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
60147c478bd9Sstevel@tonic-gate 	}
60157c478bd9Sstevel@tonic-gate 
60167c478bd9Sstevel@tonic-gate 	nsp = (struct pi_errs *)kerrsp->ks_data;
60177c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_softerrs, "Soft Errors", KSTAT_DATA_UINT32);
60187c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_harderrs, "Hard Errors", KSTAT_DATA_UINT32);
60197c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_transerrs, "Transport Errors",
60207c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
60217c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_icnt_busy, "Interconnect Busy",
60227c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
60237c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_icnt_errors, "Interconnect Errors",
60247c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
60257c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_phci_rsrc, "pHCI No Resources",
60267c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
60277c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_phci_localerr, "pHCI Local Errors",
60287c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
60297c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_phci_invstate, "pHCI Invalid State",
60307c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
60317c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_failedfrom, "Failed From",
60327c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
60337c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_failedto, "Failed To", KSTAT_DATA_UINT32);
60347c478bd9Sstevel@tonic-gate 
60357c478bd9Sstevel@tonic-gate 	mdi_statp = kmem_alloc(sizeof (*mdi_statp), KM_SLEEP);
60367c478bd9Sstevel@tonic-gate 	mdi_statp->pi_kstat_ref = 1;
60377c478bd9Sstevel@tonic-gate 	mdi_statp->pi_kstat_iostats = kiosp;
60387c478bd9Sstevel@tonic-gate 	mdi_statp->pi_kstat_errstats = kerrsp;
60397c478bd9Sstevel@tonic-gate 	kstat_install(kiosp);
60407c478bd9Sstevel@tonic-gate 	kstat_install(kerrsp);
60417c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_kstats = mdi_statp;
60427c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
60437c478bd9Sstevel@tonic-gate }
60447c478bd9Sstevel@tonic-gate 
60457c478bd9Sstevel@tonic-gate /*
60467c478bd9Sstevel@tonic-gate  * destroy per-path properties
60477c478bd9Sstevel@tonic-gate  */
60487c478bd9Sstevel@tonic-gate static void
60497c478bd9Sstevel@tonic-gate i_mdi_pi_kstat_destroy(mdi_pathinfo_t *pip)
60507c478bd9Sstevel@tonic-gate {
60517c478bd9Sstevel@tonic-gate 
60527c478bd9Sstevel@tonic-gate 	struct mdi_pi_kstats *mdi_statp;
60537c478bd9Sstevel@tonic-gate 
605437fbbce5Scth 	if (MDI_PI(pip)->pi_kstats == NULL)
605537fbbce5Scth 		return;
60567c478bd9Sstevel@tonic-gate 	if ((mdi_statp = MDI_PI(pip)->pi_kstats) == NULL)
60577c478bd9Sstevel@tonic-gate 		return;
60587c478bd9Sstevel@tonic-gate 
60597c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_kstats = NULL;
60607c478bd9Sstevel@tonic-gate 
60617c478bd9Sstevel@tonic-gate 	/*
60627c478bd9Sstevel@tonic-gate 	 * the kstat may be shared between multiple pathinfo nodes
60637c478bd9Sstevel@tonic-gate 	 * decrement this pathinfo's usage, removing the kstats
60647c478bd9Sstevel@tonic-gate 	 * themselves when the last pathinfo reference is removed.
60657c478bd9Sstevel@tonic-gate 	 */
60667c478bd9Sstevel@tonic-gate 	ASSERT(mdi_statp->pi_kstat_ref > 0);
60677c478bd9Sstevel@tonic-gate 	if (--mdi_statp->pi_kstat_ref != 0)
60687c478bd9Sstevel@tonic-gate 		return;
60697c478bd9Sstevel@tonic-gate 
60707c478bd9Sstevel@tonic-gate 	kstat_delete(mdi_statp->pi_kstat_iostats);
60717c478bd9Sstevel@tonic-gate 	kstat_delete(mdi_statp->pi_kstat_errstats);
60727c478bd9Sstevel@tonic-gate 	kmem_free(mdi_statp, sizeof (*mdi_statp));
60737c478bd9Sstevel@tonic-gate }
60747c478bd9Sstevel@tonic-gate 
60757c478bd9Sstevel@tonic-gate /*
60767c478bd9Sstevel@tonic-gate  * update I/O paths KSTATS
60777c478bd9Sstevel@tonic-gate  */
60787c478bd9Sstevel@tonic-gate void
60797c478bd9Sstevel@tonic-gate mdi_pi_kstat_iosupdate(mdi_pathinfo_t *pip, struct buf *bp)
60807c478bd9Sstevel@tonic-gate {
60817c478bd9Sstevel@tonic-gate 	kstat_t *iostatp;
60827c478bd9Sstevel@tonic-gate 	size_t xfer_cnt;
60837c478bd9Sstevel@tonic-gate 
60847c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
60857c478bd9Sstevel@tonic-gate 
60867c478bd9Sstevel@tonic-gate 	/*
60877c478bd9Sstevel@tonic-gate 	 * I/O can be driven across a path prior to having path
60887c478bd9Sstevel@tonic-gate 	 * statistics available, i.e. probe(9e).
60897c478bd9Sstevel@tonic-gate 	 */
60907c478bd9Sstevel@tonic-gate 	if (bp != NULL && MDI_PI(pip)->pi_kstats != NULL) {
60917c478bd9Sstevel@tonic-gate 		iostatp = MDI_PI(pip)->pi_kstats->pi_kstat_iostats;
60927c478bd9Sstevel@tonic-gate 		xfer_cnt = bp->b_bcount - bp->b_resid;
60937c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
60947c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->reads++;
60957c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->nread += xfer_cnt;
60967c478bd9Sstevel@tonic-gate 		} else {
60977c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->writes++;
60987c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->nwritten += xfer_cnt;
60997c478bd9Sstevel@tonic-gate 		}
61007c478bd9Sstevel@tonic-gate 	}
61017c478bd9Sstevel@tonic-gate }
61027c478bd9Sstevel@tonic-gate 
61037c478bd9Sstevel@tonic-gate /*
6104ee28b439Scm136836  * Enable the path(specific client/target/initiator)
6105ee28b439Scm136836  * Enabling a path means that MPxIO may select the enabled path for routing
6106ee28b439Scm136836  * future I/O requests, subject to other path state constraints.
6107ee28b439Scm136836  */
6108ee28b439Scm136836 int
6109ee28b439Scm136836 mdi_pi_enable_path(mdi_pathinfo_t *pip, int flags)
6110ee28b439Scm136836 {
6111ee28b439Scm136836 	mdi_phci_t	*ph;
6112ee28b439Scm136836 
61134c06356bSdh142964 	ph = MDI_PI(pip)->pi_phci;
6114ee28b439Scm136836 	if (ph == NULL) {
61154c06356bSdh142964 		MDI_DEBUG(1, (MDI_NOTE, mdi_pi_get_phci(pip),
61164c06356bSdh142964 		    "!failed: path %s %p: NULL ph",
61174c06356bSdh142964 		    mdi_pi_spathname(pip), (void *)pip));
6118ee28b439Scm136836 		return (MDI_FAILURE);
6119ee28b439Scm136836 	}
6120ee28b439Scm136836 
6121ee28b439Scm136836 	(void) i_mdi_enable_disable_path(pip, ph->ph_vhci, flags,
6122ee28b439Scm136836 		MDI_ENABLE_OP);
61234c06356bSdh142964 	MDI_DEBUG(5, (MDI_NOTE, ph->ph_dip,
61244c06356bSdh142964 	    "!returning success pip = %p. ph = %p",
61255e3986cbScth 	    (void *)pip, (void *)ph));
6126ee28b439Scm136836 	return (MDI_SUCCESS);
6127ee28b439Scm136836 
6128ee28b439Scm136836 }
6129ee28b439Scm136836 
6130ee28b439Scm136836 /*
6131ee28b439Scm136836  * Disable the path (specific client/target/initiator)
6132ee28b439Scm136836  * Disabling a path means that MPxIO will not select the disabled path for
6133ee28b439Scm136836  * routing any new I/O requests.
6134ee28b439Scm136836  */
6135ee28b439Scm136836 int
6136ee28b439Scm136836 mdi_pi_disable_path(mdi_pathinfo_t *pip, int flags)
6137ee28b439Scm136836 {
6138ee28b439Scm136836 	mdi_phci_t	*ph;
6139ee28b439Scm136836 
61404c06356bSdh142964 	ph = MDI_PI(pip)->pi_phci;
6141ee28b439Scm136836 	if (ph == NULL) {
61424c06356bSdh142964 		MDI_DEBUG(1, (MDI_NOTE, mdi_pi_get_phci(pip),
61434c06356bSdh142964 		    "!failed: path %s %p: NULL ph",
61444c06356bSdh142964 		    mdi_pi_spathname(pip), (void *)pip));
6145ee28b439Scm136836 		return (MDI_FAILURE);
6146ee28b439Scm136836 	}
6147ee28b439Scm136836 
6148ee28b439Scm136836 	(void) i_mdi_enable_disable_path(pip,
6149ee28b439Scm136836 	    ph->ph_vhci, flags, MDI_DISABLE_OP);
61504c06356bSdh142964 	MDI_DEBUG(5, (MDI_NOTE, ph->ph_dip,
61514c06356bSdh142964 	    "!returning success pip = %p. ph = %p",
61525e3986cbScth 	    (void *)pip, (void *)ph));
6153ee28b439Scm136836 	return (MDI_SUCCESS);
6154ee28b439Scm136836 }
6155ee28b439Scm136836 
6156ee28b439Scm136836 /*
61577c478bd9Sstevel@tonic-gate  * disable the path to a particular pHCI (pHCI specified in the phci_path
61587c478bd9Sstevel@tonic-gate  * argument) for a particular client (specified in the client_path argument).
61597c478bd9Sstevel@tonic-gate  * Disabling a path means that MPxIO will not select the disabled path for
61607c478bd9Sstevel@tonic-gate  * routing any new I/O requests.
6161ee28b439Scm136836  * NOTE: this will be removed once the NWS files are changed to use the new
6162ee28b439Scm136836  * mdi_{enable,disable}_path interfaces
61637c478bd9Sstevel@tonic-gate  */
61647c478bd9Sstevel@tonic-gate int
61657c478bd9Sstevel@tonic-gate mdi_pi_disable(dev_info_t *cdip, dev_info_t *pdip, int flags)
61667c478bd9Sstevel@tonic-gate {
61677c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_enable_disable(cdip, pdip, flags, MDI_DISABLE_OP));
61687c478bd9Sstevel@tonic-gate }
61697c478bd9Sstevel@tonic-gate 
61707c478bd9Sstevel@tonic-gate /*
61717c478bd9Sstevel@tonic-gate  * Enable the path to a particular pHCI (pHCI specified in the phci_path
61727c478bd9Sstevel@tonic-gate  * argument) for a particular client (specified in the client_path argument).
61737c478bd9Sstevel@tonic-gate  * Enabling a path means that MPxIO may select the enabled path for routing
61747c478bd9Sstevel@tonic-gate  * future I/O requests, subject to other path state constraints.
6175ee28b439Scm136836  * NOTE: this will be removed once the NWS files are changed to use the new
6176ee28b439Scm136836  * mdi_{enable,disable}_path interfaces
61777c478bd9Sstevel@tonic-gate  */
61787c478bd9Sstevel@tonic-gate 
61797c478bd9Sstevel@tonic-gate int
61807c478bd9Sstevel@tonic-gate mdi_pi_enable(dev_info_t *cdip, dev_info_t *pdip, int flags)
61817c478bd9Sstevel@tonic-gate {
61827c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_enable_disable(cdip, pdip, flags, MDI_ENABLE_OP));
61837c478bd9Sstevel@tonic-gate }
61847c478bd9Sstevel@tonic-gate 
6185ee28b439Scm136836 /*
6186ee28b439Scm136836  * Common routine for doing enable/disable.
6187ee28b439Scm136836  */
6188ee28b439Scm136836 static mdi_pathinfo_t *
6189ee28b439Scm136836 i_mdi_enable_disable_path(mdi_pathinfo_t *pip, mdi_vhci_t *vh, int flags,
6190ee28b439Scm136836 		int op)
6191ee28b439Scm136836 {
6192ee28b439Scm136836 	int		sync_flag = 0;
6193ee28b439Scm136836 	int		rv;
6194ee28b439Scm136836 	mdi_pathinfo_t 	*next;
6195ee28b439Scm136836 	int		(*f)() = NULL;
6196ee28b439Scm136836 
61976c8e19d4SZach Kissel 	/*
61986c8e19d4SZach Kissel 	 * Check to make sure the path is not already in the
61996c8e19d4SZach Kissel 	 * requested state. If it is just return the next path
62006c8e19d4SZach Kissel 	 * as we have nothing to do here.
62016c8e19d4SZach Kissel 	 */
62026c8e19d4SZach Kissel 	if ((MDI_PI_IS_DISABLE(pip) && op == MDI_DISABLE_OP) ||
62036c8e19d4SZach Kissel 	    (!MDI_PI_IS_DISABLE(pip) && op == MDI_ENABLE_OP)) {
62046c8e19d4SZach Kissel 		MDI_PI_LOCK(pip);
62056c8e19d4SZach Kissel 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
62066c8e19d4SZach Kissel 		MDI_PI_UNLOCK(pip);
62076c8e19d4SZach Kissel 		return (next);
62086c8e19d4SZach Kissel 	}
62096c8e19d4SZach Kissel 
6210ee28b439Scm136836 	f = vh->vh_ops->vo_pi_state_change;
6211ee28b439Scm136836 
6212ee28b439Scm136836 	sync_flag = (flags << 8) & 0xf00;
6213ee28b439Scm136836 
6214ee28b439Scm136836 	/*
6215ee28b439Scm136836 	 * Do a callback into the mdi consumer to let it
6216ee28b439Scm136836 	 * know that path is about to get enabled/disabled.
6217ee28b439Scm136836 	 */
6218ee28b439Scm136836 	if (f != NULL) {
6219ee28b439Scm136836 		rv = (*f)(vh->vh_dip, pip, 0,
6220ee28b439Scm136836 			MDI_PI_EXT_STATE(pip),
6221ee28b439Scm136836 			MDI_EXT_STATE_CHANGE | sync_flag |
6222ee28b439Scm136836 			op | MDI_BEFORE_STATE_CHANGE);
6223ee28b439Scm136836 		if (rv != MDI_SUCCESS) {
62244c06356bSdh142964 			MDI_DEBUG(2, (MDI_WARN, vh->vh_dip,
62254c06356bSdh142964 			    "vo_pi_state_change: failed rv = %x", rv));
6226ee28b439Scm136836 		}
6227ee28b439Scm136836 	}
6228ee28b439Scm136836 	MDI_PI_LOCK(pip);
6229ee28b439Scm136836 	next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
6230ee28b439Scm136836 
6231ee28b439Scm136836 	switch (flags) {
6232ee28b439Scm136836 		case USER_DISABLE:
62335e3986cbScth 			if (op == MDI_DISABLE_OP) {
6234ee28b439Scm136836 				MDI_PI_SET_USER_DISABLE(pip);
62355e3986cbScth 			} else {
6236ee28b439Scm136836 				MDI_PI_SET_USER_ENABLE(pip);
62375e3986cbScth 			}
6238ee28b439Scm136836 			break;
6239ee28b439Scm136836 		case DRIVER_DISABLE:
62405e3986cbScth 			if (op == MDI_DISABLE_OP) {
6241ee28b439Scm136836 				MDI_PI_SET_DRV_DISABLE(pip);
62425e3986cbScth 			} else {
6243ee28b439Scm136836 				MDI_PI_SET_DRV_ENABLE(pip);
62445e3986cbScth 			}
6245ee28b439Scm136836 			break;
6246ee28b439Scm136836 		case DRIVER_DISABLE_TRANSIENT:
62475e3986cbScth 			if (op == MDI_DISABLE_OP && rv == MDI_SUCCESS) {
6248ee28b439Scm136836 				MDI_PI_SET_DRV_DISABLE_TRANS(pip);
62495e3986cbScth 			} else {
6250ee28b439Scm136836 				MDI_PI_SET_DRV_ENABLE_TRANS(pip);
62515e3986cbScth 			}
6252ee28b439Scm136836 			break;
6253ee28b439Scm136836 	}
6254ee28b439Scm136836 	MDI_PI_UNLOCK(pip);
6255ee28b439Scm136836 	/*
6256ee28b439Scm136836 	 * Do a callback into the mdi consumer to let it
6257ee28b439Scm136836 	 * know that path is now enabled/disabled.
6258ee28b439Scm136836 	 */
6259ee28b439Scm136836 	if (f != NULL) {
6260ee28b439Scm136836 		rv = (*f)(vh->vh_dip, pip, 0,
6261ee28b439Scm136836 			MDI_PI_EXT_STATE(pip),
6262ee28b439Scm136836 			MDI_EXT_STATE_CHANGE | sync_flag |
6263ee28b439Scm136836 			op | MDI_AFTER_STATE_CHANGE);
6264ee28b439Scm136836 		if (rv != MDI_SUCCESS) {
62654c06356bSdh142964 			MDI_DEBUG(2, (MDI_WARN, vh->vh_dip,
62664c06356bSdh142964 			    "vo_pi_state_change failed: rv = %x", rv));
6267ee28b439Scm136836 		}
6268ee28b439Scm136836 	}
6269ee28b439Scm136836 	return (next);
6270ee28b439Scm136836 }
62717c478bd9Sstevel@tonic-gate 
62727c478bd9Sstevel@tonic-gate /*
62737c478bd9Sstevel@tonic-gate  * Common routine for doing enable/disable.
6274ee28b439Scm136836  * NOTE: this will be removed once the NWS files are changed to use the new
6275ee28b439Scm136836  * mdi_{enable,disable}_path has been putback
62767c478bd9Sstevel@tonic-gate  */
62777c478bd9Sstevel@tonic-gate int
62787c478bd9Sstevel@tonic-gate i_mdi_pi_enable_disable(dev_info_t *cdip, dev_info_t *pdip, int flags, int op)
62797c478bd9Sstevel@tonic-gate {
62807c478bd9Sstevel@tonic-gate 
62817c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
62827c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
62837c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
62847c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next, *pip;
62857c478bd9Sstevel@tonic-gate 	int		found_it;
62867c478bd9Sstevel@tonic-gate 
62877c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
62884c06356bSdh142964 	MDI_DEBUG(5, (MDI_NOTE, cdip ? cdip : pdip,
62894c06356bSdh142964 	    "!op = %d pdip = %p cdip = %p", op, (void *)pdip,
62905e3986cbScth 	    (void *)cdip));
62917c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
62924c06356bSdh142964 		MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
62934c06356bSdh142964 		    "!failed: operation %d: NULL ph", op));
62947c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
62957c478bd9Sstevel@tonic-gate 	}
62967c478bd9Sstevel@tonic-gate 
62977c478bd9Sstevel@tonic-gate 	if ((op != MDI_ENABLE_OP) && (op != MDI_DISABLE_OP)) {
62984c06356bSdh142964 		MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
62994c06356bSdh142964 		    "!failed: invalid operation %d", op));
63007c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
63017c478bd9Sstevel@tonic-gate 	}
63027c478bd9Sstevel@tonic-gate 
63037c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
63047c478bd9Sstevel@tonic-gate 
63057c478bd9Sstevel@tonic-gate 	if (cdip == NULL) {
63067c478bd9Sstevel@tonic-gate 		/*
63077c478bd9Sstevel@tonic-gate 		 * Need to mark the Phci as enabled/disabled.
63087c478bd9Sstevel@tonic-gate 		 */
63094c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, cdip ? cdip : pdip,
63104c06356bSdh142964 		    "op %d for the phci", op));
63117c478bd9Sstevel@tonic-gate 		MDI_PHCI_LOCK(ph);
63127c478bd9Sstevel@tonic-gate 		switch (flags) {
63137c478bd9Sstevel@tonic-gate 			case USER_DISABLE:
63145e3986cbScth 				if (op == MDI_DISABLE_OP) {
63157c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_USER_DISABLE(ph);
63165e3986cbScth 				} else {
63177c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_USER_ENABLE(ph);
63185e3986cbScth 				}
63197c478bd9Sstevel@tonic-gate 				break;
63207c478bd9Sstevel@tonic-gate 			case DRIVER_DISABLE:
63215e3986cbScth 				if (op == MDI_DISABLE_OP) {
63227c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_DISABLE(ph);
63235e3986cbScth 				} else {
63247c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_ENABLE(ph);
63255e3986cbScth 				}
63267c478bd9Sstevel@tonic-gate 				break;
63277c478bd9Sstevel@tonic-gate 			case DRIVER_DISABLE_TRANSIENT:
63285e3986cbScth 				if (op == MDI_DISABLE_OP) {
63297c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_DISABLE_TRANSIENT(ph);
63305e3986cbScth 				} else {
63317c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_ENABLE_TRANSIENT(ph);
63325e3986cbScth 				}
63337c478bd9Sstevel@tonic-gate 				break;
63347c478bd9Sstevel@tonic-gate 			default:
63357c478bd9Sstevel@tonic-gate 				MDI_PHCI_UNLOCK(ph);
63364c06356bSdh142964 				MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
63374c06356bSdh142964 				    "!invalid flag argument= %d", flags));
63387c478bd9Sstevel@tonic-gate 		}
63397c478bd9Sstevel@tonic-gate 
63407c478bd9Sstevel@tonic-gate 		/*
63417c478bd9Sstevel@tonic-gate 		 * Phci has been disabled. Now try to enable/disable
63427c478bd9Sstevel@tonic-gate 		 * path info's to each client.
63437c478bd9Sstevel@tonic-gate 		 */
63447c478bd9Sstevel@tonic-gate 		pip = ph->ph_path_head;
63457c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
6346ee28b439Scm136836 			pip = i_mdi_enable_disable_path(pip, vh, flags, op);
63477c478bd9Sstevel@tonic-gate 		}
63487c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
63497c478bd9Sstevel@tonic-gate 	} else {
63507c478bd9Sstevel@tonic-gate 
63517c478bd9Sstevel@tonic-gate 		/*
63527c478bd9Sstevel@tonic-gate 		 * Disable a specific client.
63537c478bd9Sstevel@tonic-gate 		 */
63547c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(cdip);
63557c478bd9Sstevel@tonic-gate 		if (ct == NULL) {
63564c06356bSdh142964 			MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
63574c06356bSdh142964 			    "!failed: operation = %d: NULL ct", op));
63587c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
63597c478bd9Sstevel@tonic-gate 		}
63607c478bd9Sstevel@tonic-gate 
63617c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
63627c478bd9Sstevel@tonic-gate 		pip = ct->ct_path_head;
63637c478bd9Sstevel@tonic-gate 		found_it = 0;
63647c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
63657c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
63667c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
63677c478bd9Sstevel@tonic-gate 			if (MDI_PI(pip)->pi_phci == ph) {
63687c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
63697c478bd9Sstevel@tonic-gate 				found_it = 1;
63707c478bd9Sstevel@tonic-gate 				break;
63717c478bd9Sstevel@tonic-gate 			}
63727c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
63737c478bd9Sstevel@tonic-gate 			pip = next;
63747c478bd9Sstevel@tonic-gate 		}
63757c478bd9Sstevel@tonic-gate 
6376ee28b439Scm136836 
63777c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
63787c478bd9Sstevel@tonic-gate 		if (found_it == 0) {
63794c06356bSdh142964 			MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
63804c06356bSdh142964 			    "!failed. Could not find corresponding pip\n"));
63817c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
63827c478bd9Sstevel@tonic-gate 		}
6383ee28b439Scm136836 
6384ee28b439Scm136836 		(void) i_mdi_enable_disable_path(pip, vh, flags, op);
63857c478bd9Sstevel@tonic-gate 	}
63867c478bd9Sstevel@tonic-gate 
63874c06356bSdh142964 	MDI_DEBUG(5, (MDI_NOTE, cdip ? cdip : pdip,
63884c06356bSdh142964 	    "!op %d returning success pdip = %p cdip = %p",
63895e3986cbScth 	    op, (void *)pdip, (void *)cdip));
63907c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
63917c478bd9Sstevel@tonic-gate }
63927c478bd9Sstevel@tonic-gate 
63937c478bd9Sstevel@tonic-gate /*
63947c478bd9Sstevel@tonic-gate  * Ensure phci powered up
63957c478bd9Sstevel@tonic-gate  */
63967c478bd9Sstevel@tonic-gate static void
63977c478bd9Sstevel@tonic-gate i_mdi_pm_hold_pip(mdi_pathinfo_t *pip)
63987c478bd9Sstevel@tonic-gate {
63997c478bd9Sstevel@tonic-gate 	dev_info_t	*ph_dip;
64007c478bd9Sstevel@tonic-gate 
64017c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
64025e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
64037c478bd9Sstevel@tonic-gate 
64047c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held) {
64057c478bd9Sstevel@tonic-gate 		return;
64067c478bd9Sstevel@tonic-gate 	}
64077c478bd9Sstevel@tonic-gate 
64087c478bd9Sstevel@tonic-gate 	ph_dip = mdi_pi_get_phci(pip);
64094c06356bSdh142964 	MDI_DEBUG(4, (MDI_NOTE, ph_dip,
64104c06356bSdh142964 	    "%s %p", mdi_pi_spathname(pip), (void *)pip));
64117c478bd9Sstevel@tonic-gate 	if (ph_dip == NULL) {
64127c478bd9Sstevel@tonic-gate 		return;
64137c478bd9Sstevel@tonic-gate 	}
64147c478bd9Sstevel@tonic-gate 
64157c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
64164c06356bSdh142964 	MDI_DEBUG(4, (MDI_NOTE, ph_dip, "kidsupcnt was %d",
64177c478bd9Sstevel@tonic-gate 	    DEVI(ph_dip)->devi_pm_kidsupcnt));
64187c478bd9Sstevel@tonic-gate 	pm_hold_power(ph_dip);
64194c06356bSdh142964 	MDI_DEBUG(4, (MDI_NOTE, ph_dip, "kidsupcnt is %d",
64207c478bd9Sstevel@tonic-gate 	    DEVI(ph_dip)->devi_pm_kidsupcnt));
64217c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
64227c478bd9Sstevel@tonic-gate 
64235e3986cbScth 	/* If PM_GET_PM_INFO is NULL the pm_hold_power above was a noop */
64245e3986cbScth 	if (DEVI(ph_dip)->devi_pm_info)
64257c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_pm_held = 1;
64267c478bd9Sstevel@tonic-gate }
64277c478bd9Sstevel@tonic-gate 
64287c478bd9Sstevel@tonic-gate /*
64297c478bd9Sstevel@tonic-gate  * Allow phci powered down
64307c478bd9Sstevel@tonic-gate  */
64317c478bd9Sstevel@tonic-gate static void
64327c478bd9Sstevel@tonic-gate i_mdi_pm_rele_pip(mdi_pathinfo_t *pip)
64337c478bd9Sstevel@tonic-gate {
64347c478bd9Sstevel@tonic-gate 	dev_info_t	*ph_dip = NULL;
64357c478bd9Sstevel@tonic-gate 
64367c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
64375e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
64387c478bd9Sstevel@tonic-gate 
64397c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held == 0) {
64407c478bd9Sstevel@tonic-gate 		return;
64417c478bd9Sstevel@tonic-gate 	}
64427c478bd9Sstevel@tonic-gate 
64437c478bd9Sstevel@tonic-gate 	ph_dip = mdi_pi_get_phci(pip);
64447c478bd9Sstevel@tonic-gate 	ASSERT(ph_dip != NULL);
64457c478bd9Sstevel@tonic-gate 
64464c06356bSdh142964 	MDI_DEBUG(4, (MDI_NOTE, ph_dip,
64474c06356bSdh142964 	    "%s %p", mdi_pi_spathname(pip), (void *)pip));
64484c06356bSdh142964 
64497c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
64504c06356bSdh142964 	MDI_DEBUG(4, (MDI_NOTE, ph_dip,
64514c06356bSdh142964 	    "kidsupcnt was %d", DEVI(ph_dip)->devi_pm_kidsupcnt));
64527c478bd9Sstevel@tonic-gate 	pm_rele_power(ph_dip);
64534c06356bSdh142964 	MDI_DEBUG(4, (MDI_NOTE, ph_dip,
64544c06356bSdh142964 	    "kidsupcnt is %d", DEVI(ph_dip)->devi_pm_kidsupcnt));
64557c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
64564c06356bSdh142964 
64577c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_pm_held = 0;
64587c478bd9Sstevel@tonic-gate }
64597c478bd9Sstevel@tonic-gate 
64607c478bd9Sstevel@tonic-gate static void
64617c478bd9Sstevel@tonic-gate i_mdi_pm_hold_client(mdi_client_t *ct, int incr)
64627c478bd9Sstevel@tonic-gate {
64635e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
64647c478bd9Sstevel@tonic-gate 
64657c478bd9Sstevel@tonic-gate 	ct->ct_power_cnt += incr;
64664c06356bSdh142964 	MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
64674c06356bSdh142964 	    "%p ct_power_cnt = %d incr = %d",
64684c06356bSdh142964 	    (void *)ct, ct->ct_power_cnt, incr));
64697c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_power_cnt >= 0);
64707c478bd9Sstevel@tonic-gate }
64717c478bd9Sstevel@tonic-gate 
64727c478bd9Sstevel@tonic-gate static void
64737c478bd9Sstevel@tonic-gate i_mdi_rele_all_phci(mdi_client_t *ct)
64747c478bd9Sstevel@tonic-gate {
64757c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t  *pip;
64767c478bd9Sstevel@tonic-gate 
64775e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
64787c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
64797c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
64807c478bd9Sstevel@tonic-gate 		mdi_hold_path(pip);
64817c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
64827c478bd9Sstevel@tonic-gate 		i_mdi_pm_rele_pip(pip);
64837c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
64847c478bd9Sstevel@tonic-gate 		mdi_rele_path(pip);
64857c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
64867c478bd9Sstevel@tonic-gate 	}
64877c478bd9Sstevel@tonic-gate }
64887c478bd9Sstevel@tonic-gate 
64897c478bd9Sstevel@tonic-gate static void
64907c478bd9Sstevel@tonic-gate i_mdi_pm_rele_client(mdi_client_t *ct, int decr)
64917c478bd9Sstevel@tonic-gate {
64925e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
64937c478bd9Sstevel@tonic-gate 
6494737d277aScth 	if (i_ddi_devi_attached(ct->ct_dip)) {
64957c478bd9Sstevel@tonic-gate 		ct->ct_power_cnt -= decr;
64964c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
64974c06356bSdh142964 		    "%p ct_power_cnt = %d decr = %d",
64985e3986cbScth 		    (void *)ct, ct->ct_power_cnt, decr));
64997c478bd9Sstevel@tonic-gate 	}
65007c478bd9Sstevel@tonic-gate 
65017c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_power_cnt >= 0);
65027c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0) {
65037c478bd9Sstevel@tonic-gate 		i_mdi_rele_all_phci(ct);
65047c478bd9Sstevel@tonic-gate 		return;
65057c478bd9Sstevel@tonic-gate 	}
65067c478bd9Sstevel@tonic-gate }
65077c478bd9Sstevel@tonic-gate 
65087c478bd9Sstevel@tonic-gate static void
65097c478bd9Sstevel@tonic-gate i_mdi_pm_reset_client(mdi_client_t *ct)
65107c478bd9Sstevel@tonic-gate {
65114c06356bSdh142964 	MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
65124c06356bSdh142964 	    "%p ct_power_cnt = %d", (void *)ct, ct->ct_power_cnt));
65135e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
65147c478bd9Sstevel@tonic-gate 	ct->ct_power_cnt = 0;
65157c478bd9Sstevel@tonic-gate 	i_mdi_rele_all_phci(ct);
651678dc6db2Sllai1 	ct->ct_powercnt_config = 0;
651778dc6db2Sllai1 	ct->ct_powercnt_unconfig = 0;
65187c478bd9Sstevel@tonic-gate 	ct->ct_powercnt_reset = 1;
65197c478bd9Sstevel@tonic-gate }
65207c478bd9Sstevel@tonic-gate 
65217c478bd9Sstevel@tonic-gate static int
65227c478bd9Sstevel@tonic-gate i_mdi_power_one_phci(mdi_pathinfo_t *pip)
65237c478bd9Sstevel@tonic-gate {
65247c478bd9Sstevel@tonic-gate 	int		ret;
65257c478bd9Sstevel@tonic-gate 	dev_info_t	*ph_dip;
65267c478bd9Sstevel@tonic-gate 
65277c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
65287c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_pip(pip);
65297c478bd9Sstevel@tonic-gate 
65307c478bd9Sstevel@tonic-gate 	ph_dip = mdi_pi_get_phci(pip);
65317c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
65327c478bd9Sstevel@tonic-gate 
65337c478bd9Sstevel@tonic-gate 	/* bring all components of phci to full power */
65344c06356bSdh142964 	MDI_DEBUG(4, (MDI_NOTE, ph_dip,
65354c06356bSdh142964 	    "pm_powerup for %s%d %p", ddi_driver_name(ph_dip),
65365e3986cbScth 	    ddi_get_instance(ph_dip), (void *)pip));
65377c478bd9Sstevel@tonic-gate 
65387c478bd9Sstevel@tonic-gate 	ret = pm_powerup(ph_dip);
65397c478bd9Sstevel@tonic-gate 
65407c478bd9Sstevel@tonic-gate 	if (ret == DDI_FAILURE) {
65414c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, ph_dip,
65424c06356bSdh142964 		    "pm_powerup FAILED for %s%d %p",
654355e592a2SRandall Ralphs 		    ddi_driver_name(ph_dip), ddi_get_instance(ph_dip),
65445e3986cbScth 		    (void *)pip));
65457c478bd9Sstevel@tonic-gate 
65467c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
65477c478bd9Sstevel@tonic-gate 		i_mdi_pm_rele_pip(pip);
65487c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
65497c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
65507c478bd9Sstevel@tonic-gate 	}
65517c478bd9Sstevel@tonic-gate 
65527c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
65537c478bd9Sstevel@tonic-gate }
65547c478bd9Sstevel@tonic-gate 
65557c478bd9Sstevel@tonic-gate static int
65567c478bd9Sstevel@tonic-gate i_mdi_power_all_phci(mdi_client_t *ct)
65577c478bd9Sstevel@tonic-gate {
65587c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t  *pip;
65597c478bd9Sstevel@tonic-gate 	int		succeeded = 0;
65607c478bd9Sstevel@tonic-gate 
65615e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
65627c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
65637c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
65645e3986cbScth 		/*
65655e3986cbScth 		 * Don't power if MDI_PATHINFO_STATE_FAULT
65665e3986cbScth 		 * or MDI_PATHINFO_STATE_OFFLINE.
65675e3986cbScth 		 */
65685e3986cbScth 		if (MDI_PI_IS_INIT(pip) ||
65695e3986cbScth 		    MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip)) {
65707c478bd9Sstevel@tonic-gate 			mdi_hold_path(pip);
65717c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
65727c478bd9Sstevel@tonic-gate 			if (i_mdi_power_one_phci(pip) == MDI_SUCCESS)
65737c478bd9Sstevel@tonic-gate 				succeeded = 1;
65747c478bd9Sstevel@tonic-gate 
65757c478bd9Sstevel@tonic-gate 			ASSERT(ct == MDI_PI(pip)->pi_client);
65767c478bd9Sstevel@tonic-gate 			MDI_CLIENT_LOCK(ct);
65777c478bd9Sstevel@tonic-gate 			mdi_rele_path(pip);
65785e3986cbScth 		}
65797c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
65807c478bd9Sstevel@tonic-gate 	}
65817c478bd9Sstevel@tonic-gate 
65827c478bd9Sstevel@tonic-gate 	return (succeeded ? MDI_SUCCESS : MDI_FAILURE);
65837c478bd9Sstevel@tonic-gate }
65847c478bd9Sstevel@tonic-gate 
65857c478bd9Sstevel@tonic-gate /*
65867c478bd9Sstevel@tonic-gate  * mdi_bus_power():
65877c478bd9Sstevel@tonic-gate  *		1. Place the phci(s) into powered up state so that
65887c478bd9Sstevel@tonic-gate  *		   client can do power management
65897c478bd9Sstevel@tonic-gate  *		2. Ensure phci powered up as client power managing
65907c478bd9Sstevel@tonic-gate  * Return Values:
65917c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
65927c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
65937c478bd9Sstevel@tonic-gate  */
65947c478bd9Sstevel@tonic-gate int
65957c478bd9Sstevel@tonic-gate mdi_bus_power(dev_info_t *parent, void *impl_arg, pm_bus_power_op_t op,
65967c478bd9Sstevel@tonic-gate     void *arg, void *result)
65977c478bd9Sstevel@tonic-gate {
65987c478bd9Sstevel@tonic-gate 	int			ret = MDI_SUCCESS;
65997c478bd9Sstevel@tonic-gate 	pm_bp_child_pwrchg_t	*bpc;
66007c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
66017c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
66027c478bd9Sstevel@tonic-gate 	pm_bp_has_changed_t	*bphc;
66037c478bd9Sstevel@tonic-gate 
66047c478bd9Sstevel@tonic-gate 	/*
66057c478bd9Sstevel@tonic-gate 	 * BUS_POWER_NOINVOL not supported
66067c478bd9Sstevel@tonic-gate 	 */
66077c478bd9Sstevel@tonic-gate 	if (op == BUS_POWER_NOINVOL)
66087c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
66097c478bd9Sstevel@tonic-gate 
66107c478bd9Sstevel@tonic-gate 	/*
66117c478bd9Sstevel@tonic-gate 	 * ignore other OPs.
66127c478bd9Sstevel@tonic-gate 	 * return quickly to save cou cycles on the ct processing
66137c478bd9Sstevel@tonic-gate 	 */
66147c478bd9Sstevel@tonic-gate 	switch (op) {
66157c478bd9Sstevel@tonic-gate 	case BUS_POWER_PRE_NOTIFICATION:
66167c478bd9Sstevel@tonic-gate 	case BUS_POWER_POST_NOTIFICATION:
66177c478bd9Sstevel@tonic-gate 		bpc = (pm_bp_child_pwrchg_t *)arg;
66187c478bd9Sstevel@tonic-gate 		cdip = bpc->bpc_dip;
66197c478bd9Sstevel@tonic-gate 		break;
66207c478bd9Sstevel@tonic-gate 	case BUS_POWER_HAS_CHANGED:
66217c478bd9Sstevel@tonic-gate 		bphc = (pm_bp_has_changed_t *)arg;
66227c478bd9Sstevel@tonic-gate 		cdip = bphc->bphc_dip;
66237c478bd9Sstevel@tonic-gate 		break;
66247c478bd9Sstevel@tonic-gate 	default:
66257c478bd9Sstevel@tonic-gate 		return (pm_busop_bus_power(parent, impl_arg, op, arg, result));
66267c478bd9Sstevel@tonic-gate 	}
66277c478bd9Sstevel@tonic-gate 
66287c478bd9Sstevel@tonic-gate 	ASSERT(MDI_CLIENT(cdip));
66297c478bd9Sstevel@tonic-gate 
66307c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
66317c478bd9Sstevel@tonic-gate 	if (ct == NULL)
66327c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
66337c478bd9Sstevel@tonic-gate 
66347c478bd9Sstevel@tonic-gate 	/*
66357c478bd9Sstevel@tonic-gate 	 * wait till the mdi_pathinfo node state change are processed
66367c478bd9Sstevel@tonic-gate 	 */
66377c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
66387c478bd9Sstevel@tonic-gate 	switch (op) {
66397c478bd9Sstevel@tonic-gate 	case BUS_POWER_PRE_NOTIFICATION:
66404c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
66417c478bd9Sstevel@tonic-gate 		    "BUS_POWER_PRE_NOTIFICATION:"
66424c06356bSdh142964 		    "%s@%s, olevel=%d, nlevel=%d, comp=%d",
66434c06356bSdh142964 		    ddi_node_name(bpc->bpc_dip), PM_ADDR(bpc->bpc_dip),
66447c478bd9Sstevel@tonic-gate 		    bpc->bpc_olevel, bpc->bpc_nlevel, bpc->bpc_comp));
66457c478bd9Sstevel@tonic-gate 
66467c478bd9Sstevel@tonic-gate 		/* serialize power level change per client */
66477c478bd9Sstevel@tonic-gate 		while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
66487c478bd9Sstevel@tonic-gate 			cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
66497c478bd9Sstevel@tonic-gate 
66507c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_POWER_TRANSITION(ct);
66517c478bd9Sstevel@tonic-gate 
66527c478bd9Sstevel@tonic-gate 		if (ct->ct_power_cnt == 0) {
66537c478bd9Sstevel@tonic-gate 			ret = i_mdi_power_all_phci(ct);
66547c478bd9Sstevel@tonic-gate 		}
66557c478bd9Sstevel@tonic-gate 
66567c478bd9Sstevel@tonic-gate 		/*
66577c478bd9Sstevel@tonic-gate 		 * if new_level > 0:
66587c478bd9Sstevel@tonic-gate 		 *	- hold phci(s)
66597c478bd9Sstevel@tonic-gate 		 *	- power up phci(s) if not already
66607c478bd9Sstevel@tonic-gate 		 * ignore power down
66617c478bd9Sstevel@tonic-gate 		 */
66627c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel > 0) {
66637c478bd9Sstevel@tonic-gate 			if (!DEVI_IS_ATTACHING(ct->ct_dip)) {
66644c06356bSdh142964 				MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
66654c06356bSdh142964 				    "i_mdi_pm_hold_client\n"));
66667c478bd9Sstevel@tonic-gate 				i_mdi_pm_hold_client(ct, ct->ct_path_count);
66677c478bd9Sstevel@tonic-gate 			}
66687c478bd9Sstevel@tonic-gate 		}
66697c478bd9Sstevel@tonic-gate 		break;
66707c478bd9Sstevel@tonic-gate 	case BUS_POWER_POST_NOTIFICATION:
66714c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
66727c478bd9Sstevel@tonic-gate 		    "BUS_POWER_POST_NOTIFICATION:"
66734c06356bSdh142964 		    "%s@%s, olevel=%d, nlevel=%d, comp=%d result=%d",
66744c06356bSdh142964 		    ddi_node_name(bpc->bpc_dip), PM_ADDR(bpc->bpc_dip),
66757c478bd9Sstevel@tonic-gate 		    bpc->bpc_olevel, bpc->bpc_nlevel, bpc->bpc_comp,
66767c478bd9Sstevel@tonic-gate 		    *(int *)result));
66777c478bd9Sstevel@tonic-gate 
66787c478bd9Sstevel@tonic-gate 		if (*(int *)result == DDI_SUCCESS) {
66797c478bd9Sstevel@tonic-gate 			if (bpc->bpc_nlevel > 0) {
66807c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_POWER_UP(ct);
66817c478bd9Sstevel@tonic-gate 			} else {
66827c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_POWER_DOWN(ct);
66837c478bd9Sstevel@tonic-gate 			}
66847c478bd9Sstevel@tonic-gate 		}
66857c478bd9Sstevel@tonic-gate 
66867c478bd9Sstevel@tonic-gate 		/* release the hold we did in pre-notification */
66877c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel > 0 && (*(int *)result != DDI_SUCCESS) &&
66887c478bd9Sstevel@tonic-gate 		    !DEVI_IS_ATTACHING(ct->ct_dip)) {
66894c06356bSdh142964 			MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
66904c06356bSdh142964 			    "i_mdi_pm_rele_client\n"));
66917c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, ct->ct_path_count);
66927c478bd9Sstevel@tonic-gate 		}
66937c478bd9Sstevel@tonic-gate 
66947c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel == 0 && (*(int *)result == DDI_SUCCESS)) {
66957c478bd9Sstevel@tonic-gate 			/* another thread might started attaching */
66967c478bd9Sstevel@tonic-gate 			if (DEVI_IS_ATTACHING(ct->ct_dip)) {
66974c06356bSdh142964 				MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
66984c06356bSdh142964 				    "i_mdi_pm_rele_client\n"));
66997c478bd9Sstevel@tonic-gate 				i_mdi_pm_rele_client(ct, ct->ct_path_count);
67007c478bd9Sstevel@tonic-gate 			/* detaching has been taken care in pm_post_unconfig */
67017c478bd9Sstevel@tonic-gate 			} else if (!DEVI_IS_DETACHING(ct->ct_dip)) {
67024c06356bSdh142964 				MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
67034c06356bSdh142964 				    "i_mdi_pm_reset_client\n"));
67047c478bd9Sstevel@tonic-gate 				i_mdi_pm_reset_client(ct);
67057c478bd9Sstevel@tonic-gate 			}
67067c478bd9Sstevel@tonic-gate 		}
67077c478bd9Sstevel@tonic-gate 
67087c478bd9Sstevel@tonic-gate 		MDI_CLIENT_CLEAR_POWER_TRANSITION(ct);
67097c478bd9Sstevel@tonic-gate 		cv_broadcast(&ct->ct_powerchange_cv);
67107c478bd9Sstevel@tonic-gate 
67117c478bd9Sstevel@tonic-gate 		break;
67127c478bd9Sstevel@tonic-gate 
67137c478bd9Sstevel@tonic-gate 	/* need to do more */
67147c478bd9Sstevel@tonic-gate 	case BUS_POWER_HAS_CHANGED:
67154c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, bphc->bphc_dip,
67167c478bd9Sstevel@tonic-gate 		    "BUS_POWER_HAS_CHANGED:"
67174c06356bSdh142964 		    "%s@%s, olevel=%d, nlevel=%d, comp=%d",
67184c06356bSdh142964 		    ddi_node_name(bphc->bphc_dip), PM_ADDR(bphc->bphc_dip),
67197c478bd9Sstevel@tonic-gate 		    bphc->bphc_olevel, bphc->bphc_nlevel, bphc->bphc_comp));
67207c478bd9Sstevel@tonic-gate 
67217c478bd9Sstevel@tonic-gate 		if (bphc->bphc_nlevel > 0 &&
67227c478bd9Sstevel@tonic-gate 		    bphc->bphc_nlevel > bphc->bphc_olevel) {
67237c478bd9Sstevel@tonic-gate 			if (ct->ct_power_cnt == 0) {
67247c478bd9Sstevel@tonic-gate 				ret = i_mdi_power_all_phci(ct);
67257c478bd9Sstevel@tonic-gate 			}
67264c06356bSdh142964 			MDI_DEBUG(4, (MDI_NOTE, bphc->bphc_dip,
67274c06356bSdh142964 			    "i_mdi_pm_hold_client\n"));
67287c478bd9Sstevel@tonic-gate 			i_mdi_pm_hold_client(ct, ct->ct_path_count);
67297c478bd9Sstevel@tonic-gate 		}
67307c478bd9Sstevel@tonic-gate 
67317c478bd9Sstevel@tonic-gate 		if (bphc->bphc_nlevel == 0 && bphc->bphc_olevel != -1) {
67324c06356bSdh142964 			MDI_DEBUG(4, (MDI_NOTE, bphc->bphc_dip,
67334c06356bSdh142964 			    "i_mdi_pm_rele_client\n"));
67347c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, ct->ct_path_count);
67357c478bd9Sstevel@tonic-gate 		}
67367c478bd9Sstevel@tonic-gate 		break;
67377c478bd9Sstevel@tonic-gate 	}
67387c478bd9Sstevel@tonic-gate 
67397c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
67407c478bd9Sstevel@tonic-gate 	return (ret);
67417c478bd9Sstevel@tonic-gate }
67427c478bd9Sstevel@tonic-gate 
67437c478bd9Sstevel@tonic-gate static int
67447c478bd9Sstevel@tonic-gate i_mdi_pm_pre_config_one(dev_info_t *child)
67457c478bd9Sstevel@tonic-gate {
67467c478bd9Sstevel@tonic-gate 	int		ret = MDI_SUCCESS;
67477c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
67487c478bd9Sstevel@tonic-gate 
67497c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
67507c478bd9Sstevel@tonic-gate 	if (ct == NULL)
67517c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
67527c478bd9Sstevel@tonic-gate 
67537c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
67547c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
67557c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
67567c478bd9Sstevel@tonic-gate 
67577c478bd9Sstevel@tonic-gate 	if (!MDI_CLIENT_IS_FAILED(ct)) {
67587c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
67594c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, child, "already configured\n"));
67607c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
67617c478bd9Sstevel@tonic-gate 	}
67627c478bd9Sstevel@tonic-gate 
676378dc6db2Sllai1 	if (ct->ct_powercnt_config) {
67647c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
67654c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, child, "already held\n"));
67667c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
67677c478bd9Sstevel@tonic-gate 	}
67687c478bd9Sstevel@tonic-gate 
67697c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0) {
67707c478bd9Sstevel@tonic-gate 		ret = i_mdi_power_all_phci(ct);
67717c478bd9Sstevel@tonic-gate 	}
67724c06356bSdh142964 	MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_hold_client\n"));
67737c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_client(ct, ct->ct_path_count);
677478dc6db2Sllai1 	ct->ct_powercnt_config = 1;
67757c478bd9Sstevel@tonic-gate 	ct->ct_powercnt_reset = 0;
67767c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
67777c478bd9Sstevel@tonic-gate 	return (ret);
67787c478bd9Sstevel@tonic-gate }
67797c478bd9Sstevel@tonic-gate 
67807c478bd9Sstevel@tonic-gate static int
67815e3986cbScth i_mdi_pm_pre_config(dev_info_t *vdip, dev_info_t *child)
67827c478bd9Sstevel@tonic-gate {
67837c478bd9Sstevel@tonic-gate 	int			ret = MDI_SUCCESS;
67847c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
67857c478bd9Sstevel@tonic-gate 	int			circ;
67867c478bd9Sstevel@tonic-gate 
67875e3986cbScth 	ASSERT(MDI_VHCI(vdip));
67887c478bd9Sstevel@tonic-gate 
67897c478bd9Sstevel@tonic-gate 	/* ndi_devi_config_one */
67907c478bd9Sstevel@tonic-gate 	if (child) {
67915e3986cbScth 		ASSERT(DEVI_BUSY_OWNED(vdip));
67927c478bd9Sstevel@tonic-gate 		return (i_mdi_pm_pre_config_one(child));
67937c478bd9Sstevel@tonic-gate 	}
67947c478bd9Sstevel@tonic-gate 
67957c478bd9Sstevel@tonic-gate 	/* devi_config_common */
67965e3986cbScth 	ndi_devi_enter(vdip, &circ);
67975e3986cbScth 	cdip = ddi_get_child(vdip);
67987c478bd9Sstevel@tonic-gate 	while (cdip) {
67997c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
68007c478bd9Sstevel@tonic-gate 
68017c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_config_one(cdip);
68027c478bd9Sstevel@tonic-gate 		if (ret != MDI_SUCCESS)
68037c478bd9Sstevel@tonic-gate 			break;
68047c478bd9Sstevel@tonic-gate 		cdip = next;
68057c478bd9Sstevel@tonic-gate 	}
68065e3986cbScth 	ndi_devi_exit(vdip, circ);
68077c478bd9Sstevel@tonic-gate 	return (ret);
68087c478bd9Sstevel@tonic-gate }
68097c478bd9Sstevel@tonic-gate 
68107c478bd9Sstevel@tonic-gate static int
68117c478bd9Sstevel@tonic-gate i_mdi_pm_pre_unconfig_one(dev_info_t *child, int *held, int flags)
68127c478bd9Sstevel@tonic-gate {
68137c478bd9Sstevel@tonic-gate 	int		ret = MDI_SUCCESS;
68147c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
68157c478bd9Sstevel@tonic-gate 
68167c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
68177c478bd9Sstevel@tonic-gate 	if (ct == NULL)
68187c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
68197c478bd9Sstevel@tonic-gate 
68207c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
68217c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
68227c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
68237c478bd9Sstevel@tonic-gate 
6824737d277aScth 	if (!i_ddi_devi_attached(ct->ct_dip)) {
68254c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, child, "node detached already\n"));
68267c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
68277c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
68287c478bd9Sstevel@tonic-gate 	}
68297c478bd9Sstevel@tonic-gate 
68307c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_POWERED_DOWN(ct) &&
68317c478bd9Sstevel@tonic-gate 	    (flags & NDI_AUTODETACH)) {
68324c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, child, "auto-modunload\n"));
68337c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
68347c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
68357c478bd9Sstevel@tonic-gate 	}
68367c478bd9Sstevel@tonic-gate 
683778dc6db2Sllai1 	if (ct->ct_powercnt_unconfig) {
68384c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, child, "ct_powercnt_held\n"));
68397c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
68407c478bd9Sstevel@tonic-gate 		*held = 1;
68417c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
68427c478bd9Sstevel@tonic-gate 	}
68437c478bd9Sstevel@tonic-gate 
68447c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0) {
68457c478bd9Sstevel@tonic-gate 		ret = i_mdi_power_all_phci(ct);
68467c478bd9Sstevel@tonic-gate 	}
68474c06356bSdh142964 	MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_hold_client\n"));
68487c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_client(ct, ct->ct_path_count);
684978dc6db2Sllai1 	ct->ct_powercnt_unconfig = 1;
68507c478bd9Sstevel@tonic-gate 	ct->ct_powercnt_reset = 0;
68517c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
68527c478bd9Sstevel@tonic-gate 	if (ret == MDI_SUCCESS)
68537c478bd9Sstevel@tonic-gate 		*held = 1;
68547c478bd9Sstevel@tonic-gate 	return (ret);
68557c478bd9Sstevel@tonic-gate }
68567c478bd9Sstevel@tonic-gate 
68577c478bd9Sstevel@tonic-gate static int
68585e3986cbScth i_mdi_pm_pre_unconfig(dev_info_t *vdip, dev_info_t *child, int *held,
68597c478bd9Sstevel@tonic-gate     int flags)
68607c478bd9Sstevel@tonic-gate {
68617c478bd9Sstevel@tonic-gate 	int			ret = MDI_SUCCESS;
68627c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
68637c478bd9Sstevel@tonic-gate 	int			circ;
68647c478bd9Sstevel@tonic-gate 
68655e3986cbScth 	ASSERT(MDI_VHCI(vdip));
68667c478bd9Sstevel@tonic-gate 	*held = 0;
68677c478bd9Sstevel@tonic-gate 
68687c478bd9Sstevel@tonic-gate 	/* ndi_devi_unconfig_one */
68697c478bd9Sstevel@tonic-gate 	if (child) {
68705e3986cbScth 		ASSERT(DEVI_BUSY_OWNED(vdip));
68717c478bd9Sstevel@tonic-gate 		return (i_mdi_pm_pre_unconfig_one(child, held, flags));
68727c478bd9Sstevel@tonic-gate 	}
68737c478bd9Sstevel@tonic-gate 
68747c478bd9Sstevel@tonic-gate 	/* devi_unconfig_common */
68755e3986cbScth 	ndi_devi_enter(vdip, &circ);
68765e3986cbScth 	cdip = ddi_get_child(vdip);
68777c478bd9Sstevel@tonic-gate 	while (cdip) {
68787c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
68797c478bd9Sstevel@tonic-gate 
68807c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_unconfig_one(cdip, held, flags);
68817c478bd9Sstevel@tonic-gate 		cdip = next;
68827c478bd9Sstevel@tonic-gate 	}
68835e3986cbScth 	ndi_devi_exit(vdip, circ);
68847c478bd9Sstevel@tonic-gate 
68857c478bd9Sstevel@tonic-gate 	if (*held)
68867c478bd9Sstevel@tonic-gate 		ret = MDI_SUCCESS;
68877c478bd9Sstevel@tonic-gate 
68887c478bd9Sstevel@tonic-gate 	return (ret);
68897c478bd9Sstevel@tonic-gate }
68907c478bd9Sstevel@tonic-gate 
68917c478bd9Sstevel@tonic-gate static void
68927c478bd9Sstevel@tonic-gate i_mdi_pm_post_config_one(dev_info_t *child)
68937c478bd9Sstevel@tonic-gate {
68947c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
68957c478bd9Sstevel@tonic-gate 
68967c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
68977c478bd9Sstevel@tonic-gate 	if (ct == NULL)
68987c478bd9Sstevel@tonic-gate 		return;
68997c478bd9Sstevel@tonic-gate 
69007c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
69017c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
69027c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
69037c478bd9Sstevel@tonic-gate 
690478dc6db2Sllai1 	if (ct->ct_powercnt_reset || !ct->ct_powercnt_config) {
69054c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, child, "not configured\n"));
69067c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
69077c478bd9Sstevel@tonic-gate 		return;
69087c478bd9Sstevel@tonic-gate 	}
69097c478bd9Sstevel@tonic-gate 
69107c478bd9Sstevel@tonic-gate 	/* client has not been updated */
69117c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_FAILED(ct)) {
69124c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, child, "client failed\n"));
69137c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
69147c478bd9Sstevel@tonic-gate 		return;
69157c478bd9Sstevel@tonic-gate 	}
69167c478bd9Sstevel@tonic-gate 
69177c478bd9Sstevel@tonic-gate 	/* another thread might have powered it down or detached it */
69187c478bd9Sstevel@tonic-gate 	if ((MDI_CLIENT_IS_POWERED_DOWN(ct) &&
69197c478bd9Sstevel@tonic-gate 	    !DEVI_IS_ATTACHING(ct->ct_dip)) ||
6920737d277aScth 	    (!i_ddi_devi_attached(ct->ct_dip) &&
69217c478bd9Sstevel@tonic-gate 	    !DEVI_IS_ATTACHING(ct->ct_dip))) {
69224c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_reset_client\n"));
69237c478bd9Sstevel@tonic-gate 		i_mdi_pm_reset_client(ct);
69247c478bd9Sstevel@tonic-gate 	} else {
69257c478bd9Sstevel@tonic-gate 		mdi_pathinfo_t  *pip, *next;
69267c478bd9Sstevel@tonic-gate 		int	valid_path_count = 0;
69277c478bd9Sstevel@tonic-gate 
69284c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_rele_client\n"));
69297c478bd9Sstevel@tonic-gate 		pip = ct->ct_path_head;
69307c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
69317c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
69327c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
693378dc6db2Sllai1 			if (MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip))
69347c478bd9Sstevel@tonic-gate 				valid_path_count ++;
69357c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
69367c478bd9Sstevel@tonic-gate 			pip = next;
69377c478bd9Sstevel@tonic-gate 		}
69387c478bd9Sstevel@tonic-gate 		i_mdi_pm_rele_client(ct, valid_path_count);
69397c478bd9Sstevel@tonic-gate 	}
694078dc6db2Sllai1 	ct->ct_powercnt_config = 0;
69417c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
69427c478bd9Sstevel@tonic-gate }
69437c478bd9Sstevel@tonic-gate 
69447c478bd9Sstevel@tonic-gate static void
69455e3986cbScth i_mdi_pm_post_config(dev_info_t *vdip, dev_info_t *child)
69467c478bd9Sstevel@tonic-gate {
69477c478bd9Sstevel@tonic-gate 	int		circ;
69487c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
69495e3986cbScth 
69505e3986cbScth 	ASSERT(MDI_VHCI(vdip));
69517c478bd9Sstevel@tonic-gate 
69527c478bd9Sstevel@tonic-gate 	/* ndi_devi_config_one */
69537c478bd9Sstevel@tonic-gate 	if (child) {
69545e3986cbScth 		ASSERT(DEVI_BUSY_OWNED(vdip));
69557c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_config_one(child);
69567c478bd9Sstevel@tonic-gate 		return;
69577c478bd9Sstevel@tonic-gate 	}
69587c478bd9Sstevel@tonic-gate 
69597c478bd9Sstevel@tonic-gate 	/* devi_config_common */
69605e3986cbScth 	ndi_devi_enter(vdip, &circ);
69615e3986cbScth 	cdip = ddi_get_child(vdip);
69627c478bd9Sstevel@tonic-gate 	while (cdip) {
69637c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
69647c478bd9Sstevel@tonic-gate 
69657c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_config_one(cdip);
69667c478bd9Sstevel@tonic-gate 		cdip = next;
69677c478bd9Sstevel@tonic-gate 	}
69685e3986cbScth 	ndi_devi_exit(vdip, circ);
69697c478bd9Sstevel@tonic-gate }
69707c478bd9Sstevel@tonic-gate 
69717c478bd9Sstevel@tonic-gate static void
69727c478bd9Sstevel@tonic-gate i_mdi_pm_post_unconfig_one(dev_info_t *child)
69737c478bd9Sstevel@tonic-gate {
69747c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
69757c478bd9Sstevel@tonic-gate 
69767c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
69777c478bd9Sstevel@tonic-gate 	if (ct == NULL)
69787c478bd9Sstevel@tonic-gate 		return;
69797c478bd9Sstevel@tonic-gate 
69807c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
69817c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
69827c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
69837c478bd9Sstevel@tonic-gate 
698478dc6db2Sllai1 	if (!ct->ct_powercnt_unconfig || ct->ct_powercnt_reset) {
69854c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, child, "not held\n"));
69867c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
69877c478bd9Sstevel@tonic-gate 		return;
69887c478bd9Sstevel@tonic-gate 	}
69897c478bd9Sstevel@tonic-gate 
69907c478bd9Sstevel@tonic-gate 	/* failure detaching or another thread just attached it */
69917c478bd9Sstevel@tonic-gate 	if ((MDI_CLIENT_IS_POWERED_DOWN(ct) &&
6992737d277aScth 	    i_ddi_devi_attached(ct->ct_dip)) ||
6993737d277aScth 	    (!i_ddi_devi_attached(ct->ct_dip) &&
69947c478bd9Sstevel@tonic-gate 	    !DEVI_IS_ATTACHING(ct->ct_dip))) {
69954c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_reset_client\n"));
69967c478bd9Sstevel@tonic-gate 		i_mdi_pm_reset_client(ct);
699778dc6db2Sllai1 	} else {
699878dc6db2Sllai1 		mdi_pathinfo_t  *pip, *next;
699978dc6db2Sllai1 		int	valid_path_count = 0;
70007c478bd9Sstevel@tonic-gate 
70014c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_rele_client\n"));
700278dc6db2Sllai1 		pip = ct->ct_path_head;
700378dc6db2Sllai1 		while (pip != NULL) {
700478dc6db2Sllai1 			MDI_PI_LOCK(pip);
700578dc6db2Sllai1 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
700678dc6db2Sllai1 			if (MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip))
700778dc6db2Sllai1 				valid_path_count ++;
700878dc6db2Sllai1 			MDI_PI_UNLOCK(pip);
700978dc6db2Sllai1 			pip = next;
701078dc6db2Sllai1 		}
701178dc6db2Sllai1 		i_mdi_pm_rele_client(ct, valid_path_count);
701278dc6db2Sllai1 		ct->ct_powercnt_unconfig = 0;
701378dc6db2Sllai1 	}
701478dc6db2Sllai1 
70157c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
70167c478bd9Sstevel@tonic-gate }
70177c478bd9Sstevel@tonic-gate 
70187c478bd9Sstevel@tonic-gate static void
70195e3986cbScth i_mdi_pm_post_unconfig(dev_info_t *vdip, dev_info_t *child, int held)
70207c478bd9Sstevel@tonic-gate {
70217c478bd9Sstevel@tonic-gate 	int			circ;
70227c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
70237c478bd9Sstevel@tonic-gate 
70245e3986cbScth 	ASSERT(MDI_VHCI(vdip));
70257c478bd9Sstevel@tonic-gate 
70267c478bd9Sstevel@tonic-gate 	if (!held) {
70274c06356bSdh142964 		MDI_DEBUG(4, (MDI_NOTE, vdip, "held = %d", held));
70287c478bd9Sstevel@tonic-gate 		return;
70297c478bd9Sstevel@tonic-gate 	}
70307c478bd9Sstevel@tonic-gate 
70317c478bd9Sstevel@tonic-gate 	if (child) {
70325e3986cbScth 		ASSERT(DEVI_BUSY_OWNED(vdip));
70337c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_unconfig_one(child);
70347c478bd9Sstevel@tonic-gate 		return;
70357c478bd9Sstevel@tonic-gate 	}
70367c478bd9Sstevel@tonic-gate 
70375e3986cbScth 	ndi_devi_enter(vdip, &circ);
70385e3986cbScth 	cdip = ddi_get_child(vdip);
70397c478bd9Sstevel@tonic-gate 	while (cdip) {
70407c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
70417c478bd9Sstevel@tonic-gate 
70427c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_unconfig_one(cdip);
70437c478bd9Sstevel@tonic-gate 		cdip = next;
70447c478bd9Sstevel@tonic-gate 	}
70455e3986cbScth 	ndi_devi_exit(vdip, circ);
70467c478bd9Sstevel@tonic-gate }
70477c478bd9Sstevel@tonic-gate 
70487c478bd9Sstevel@tonic-gate int
70497c478bd9Sstevel@tonic-gate mdi_power(dev_info_t *vdip, mdi_pm_op_t op, void *args, char *devnm, int flags)
70507c478bd9Sstevel@tonic-gate {
70517c478bd9Sstevel@tonic-gate 	int			circ, ret = MDI_SUCCESS;
70527c478bd9Sstevel@tonic-gate 	dev_info_t		*client_dip = NULL;
70537c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
70547c478bd9Sstevel@tonic-gate 
70557c478bd9Sstevel@tonic-gate 	/*
70567c478bd9Sstevel@tonic-gate 	 * Handling ndi_devi_config_one and ndi_devi_unconfig_one.
70577c478bd9Sstevel@tonic-gate 	 * Power up pHCI for the named client device.
70587c478bd9Sstevel@tonic-gate 	 * Note: Before the client is enumerated under vhci by phci,
70597c478bd9Sstevel@tonic-gate 	 * client_dip can be NULL. Then proceed to power up all the
70607c478bd9Sstevel@tonic-gate 	 * pHCIs.
70617c478bd9Sstevel@tonic-gate 	 */
70627c478bd9Sstevel@tonic-gate 	if (devnm != NULL) {
70637c478bd9Sstevel@tonic-gate 		ndi_devi_enter(vdip, &circ);
70647c478bd9Sstevel@tonic-gate 		client_dip = ndi_devi_findchild(vdip, devnm);
70657c478bd9Sstevel@tonic-gate 	}
70667c478bd9Sstevel@tonic-gate 
70674c06356bSdh142964 	MDI_DEBUG(4, (MDI_NOTE, vdip,
70684c06356bSdh142964 	    "op = %d %s %p", op, devnm ? devnm : "", (void *)client_dip));
70697c478bd9Sstevel@tonic-gate 
70707c478bd9Sstevel@tonic-gate 	switch (op) {
70717c478bd9Sstevel@tonic-gate 	case MDI_PM_PRE_CONFIG:
70727c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_config(vdip, client_dip);
7073c73a93f2Sdm120769 		break;
70745e3986cbScth 
70757c478bd9Sstevel@tonic-gate 	case MDI_PM_PRE_UNCONFIG:
70767c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_unconfig(vdip, client_dip, (int *)args,
70777c478bd9Sstevel@tonic-gate 		    flags);
7078c73a93f2Sdm120769 		break;
70795e3986cbScth 
70807c478bd9Sstevel@tonic-gate 	case MDI_PM_POST_CONFIG:
70817c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_config(vdip, client_dip);
7082c73a93f2Sdm120769 		break;
70835e3986cbScth 
70847c478bd9Sstevel@tonic-gate 	case MDI_PM_POST_UNCONFIG:
70857c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_unconfig(vdip, client_dip, *(int *)args);
7086c73a93f2Sdm120769 		break;
70875e3986cbScth 
70887c478bd9Sstevel@tonic-gate 	case MDI_PM_HOLD_POWER:
70897c478bd9Sstevel@tonic-gate 	case MDI_PM_RELE_POWER:
70907c478bd9Sstevel@tonic-gate 		ASSERT(args);
70917c478bd9Sstevel@tonic-gate 
70927c478bd9Sstevel@tonic-gate 		client_dip = (dev_info_t *)args;
70937c478bd9Sstevel@tonic-gate 		ASSERT(MDI_CLIENT(client_dip));
70947c478bd9Sstevel@tonic-gate 
70957c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(client_dip);
70967c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
70977c478bd9Sstevel@tonic-gate 
70987c478bd9Sstevel@tonic-gate 		if (op == MDI_PM_HOLD_POWER) {
70997c478bd9Sstevel@tonic-gate 			if (ct->ct_power_cnt == 0) {
71007c478bd9Sstevel@tonic-gate 				(void) i_mdi_power_all_phci(ct);
71014c06356bSdh142964 				MDI_DEBUG(4, (MDI_NOTE, client_dip,
71024c06356bSdh142964 				    "i_mdi_pm_hold_client\n"));
71037c478bd9Sstevel@tonic-gate 				i_mdi_pm_hold_client(ct, ct->ct_path_count);
71047c478bd9Sstevel@tonic-gate 			}
71057c478bd9Sstevel@tonic-gate 		} else {
71067c478bd9Sstevel@tonic-gate 			if (DEVI_IS_ATTACHING(ct->ct_dip)) {
71074c06356bSdh142964 				MDI_DEBUG(4, (MDI_NOTE, client_dip,
71084c06356bSdh142964 				    "i_mdi_pm_rele_client\n"));
71097c478bd9Sstevel@tonic-gate 				i_mdi_pm_rele_client(ct, ct->ct_path_count);
71107c478bd9Sstevel@tonic-gate 			} else {
71114c06356bSdh142964 				MDI_DEBUG(4, (MDI_NOTE, client_dip,
71124c06356bSdh142964 				    "i_mdi_pm_reset_client\n"));
71137c478bd9Sstevel@tonic-gate 				i_mdi_pm_reset_client(ct);
71147c478bd9Sstevel@tonic-gate 			}
71157c478bd9Sstevel@tonic-gate 		}
71167c478bd9Sstevel@tonic-gate 
71177c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
71187c478bd9Sstevel@tonic-gate 		break;
71195e3986cbScth 
71207c478bd9Sstevel@tonic-gate 	default:
71217c478bd9Sstevel@tonic-gate 		break;
71227c478bd9Sstevel@tonic-gate 	}
71237c478bd9Sstevel@tonic-gate 
71245e3986cbScth 	if (devnm)
71255e3986cbScth 		ndi_devi_exit(vdip, circ);
71265e3986cbScth 
71277c478bd9Sstevel@tonic-gate 	return (ret);
71287c478bd9Sstevel@tonic-gate }
71297c478bd9Sstevel@tonic-gate 
71307c478bd9Sstevel@tonic-gate int
71317c478bd9Sstevel@tonic-gate mdi_component_is_vhci(dev_info_t *dip, const char **mdi_class)
71327c478bd9Sstevel@tonic-gate {
71337c478bd9Sstevel@tonic-gate 	mdi_vhci_t *vhci;
71347c478bd9Sstevel@tonic-gate 
71357c478bd9Sstevel@tonic-gate 	if (!MDI_VHCI(dip))
71367c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
71377c478bd9Sstevel@tonic-gate 
71387c478bd9Sstevel@tonic-gate 	if (mdi_class) {
71397c478bd9Sstevel@tonic-gate 		vhci = DEVI(dip)->devi_mdi_xhci;
71407c478bd9Sstevel@tonic-gate 		ASSERT(vhci);
71417c478bd9Sstevel@tonic-gate 		*mdi_class = vhci->vh_class;
71427c478bd9Sstevel@tonic-gate 	}
71437c478bd9Sstevel@tonic-gate 
71447c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
71457c478bd9Sstevel@tonic-gate }
71467c478bd9Sstevel@tonic-gate 
71477c478bd9Sstevel@tonic-gate int
71487c478bd9Sstevel@tonic-gate mdi_component_is_phci(dev_info_t *dip, const char **mdi_class)
71497c478bd9Sstevel@tonic-gate {
71507c478bd9Sstevel@tonic-gate 	mdi_phci_t *phci;
71517c478bd9Sstevel@tonic-gate 
71527c478bd9Sstevel@tonic-gate 	if (!MDI_PHCI(dip))
71537c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
71547c478bd9Sstevel@tonic-gate 
71557c478bd9Sstevel@tonic-gate 	if (mdi_class) {
71567c478bd9Sstevel@tonic-gate 		phci = DEVI(dip)->devi_mdi_xhci;
71577c478bd9Sstevel@tonic-gate 		ASSERT(phci);
71587c478bd9Sstevel@tonic-gate 		*mdi_class = phci->ph_vhci->vh_class;
71597c478bd9Sstevel@tonic-gate 	}
71607c478bd9Sstevel@tonic-gate 
71617c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
71627c478bd9Sstevel@tonic-gate }
71637c478bd9Sstevel@tonic-gate 
71647c478bd9Sstevel@tonic-gate int
71657c478bd9Sstevel@tonic-gate mdi_component_is_client(dev_info_t *dip, const char **mdi_class)
71667c478bd9Sstevel@tonic-gate {
71677c478bd9Sstevel@tonic-gate 	mdi_client_t *client;
71687c478bd9Sstevel@tonic-gate 
71697c478bd9Sstevel@tonic-gate 	if (!MDI_CLIENT(dip))
71707c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
71717c478bd9Sstevel@tonic-gate 
71727c478bd9Sstevel@tonic-gate 	if (mdi_class) {
71737c478bd9Sstevel@tonic-gate 		client = DEVI(dip)->devi_mdi_client;
71747c478bd9Sstevel@tonic-gate 		ASSERT(client);
71757c478bd9Sstevel@tonic-gate 		*mdi_class = client->ct_vhci->vh_class;
71767c478bd9Sstevel@tonic-gate 	}
71777c478bd9Sstevel@tonic-gate 
71787c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
71797c478bd9Sstevel@tonic-gate }
71807c478bd9Sstevel@tonic-gate 
71817c478bd9Sstevel@tonic-gate void *
71827c478bd9Sstevel@tonic-gate mdi_client_get_vhci_private(dev_info_t *dip)
71837c478bd9Sstevel@tonic-gate {
71847c478bd9Sstevel@tonic-gate 	ASSERT(mdi_component_is_client(dip, NULL) == MDI_SUCCESS);
71857c478bd9Sstevel@tonic-gate 	if (mdi_component_is_client(dip, NULL) == MDI_SUCCESS) {
71867c478bd9Sstevel@tonic-gate 		mdi_client_t	*ct;
71877c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(dip);
71887c478bd9Sstevel@tonic-gate 		return (ct->ct_vprivate);
71897c478bd9Sstevel@tonic-gate 	}
71907c478bd9Sstevel@tonic-gate 	return (NULL);
71917c478bd9Sstevel@tonic-gate }
71927c478bd9Sstevel@tonic-gate 
71937c478bd9Sstevel@tonic-gate void
71947c478bd9Sstevel@tonic-gate mdi_client_set_vhci_private(dev_info_t *dip, void *data)
71957c478bd9Sstevel@tonic-gate {
71967c478bd9Sstevel@tonic-gate 	ASSERT(mdi_component_is_client(dip, NULL) == MDI_SUCCESS);
71977c478bd9Sstevel@tonic-gate 	if (mdi_component_is_client(dip, NULL) == MDI_SUCCESS) {
71987c478bd9Sstevel@tonic-gate 		mdi_client_t	*ct;
71997c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(dip);
72007c478bd9Sstevel@tonic-gate 		ct->ct_vprivate = data;
72017c478bd9Sstevel@tonic-gate 	}
72027c478bd9Sstevel@tonic-gate }
72037c478bd9Sstevel@tonic-gate /*
72047c478bd9Sstevel@tonic-gate  * mdi_pi_get_vhci_private():
72057c478bd9Sstevel@tonic-gate  *		Get the vhci private information associated with the
72067c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node
72077c478bd9Sstevel@tonic-gate  */
72087c478bd9Sstevel@tonic-gate void *
72097c478bd9Sstevel@tonic-gate mdi_pi_get_vhci_private(mdi_pathinfo_t *pip)
72107c478bd9Sstevel@tonic-gate {
72117c478bd9Sstevel@tonic-gate 	caddr_t	vprivate = NULL;
72127c478bd9Sstevel@tonic-gate 	if (pip) {
72137c478bd9Sstevel@tonic-gate 		vprivate = MDI_PI(pip)->pi_vprivate;
72147c478bd9Sstevel@tonic-gate 	}
72157c478bd9Sstevel@tonic-gate 	return (vprivate);
72167c478bd9Sstevel@tonic-gate }
72177c478bd9Sstevel@tonic-gate 
72187c478bd9Sstevel@tonic-gate /*
72197c478bd9Sstevel@tonic-gate  * mdi_pi_set_vhci_private():
72207c478bd9Sstevel@tonic-gate  *		Set the vhci private information in the mdi_pathinfo node
72217c478bd9Sstevel@tonic-gate  */
72227c478bd9Sstevel@tonic-gate void
72237c478bd9Sstevel@tonic-gate mdi_pi_set_vhci_private(mdi_pathinfo_t *pip, void *priv)
72247c478bd9Sstevel@tonic-gate {
72257c478bd9Sstevel@tonic-gate 	if (pip) {
72267c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_vprivate = priv;
72277c478bd9Sstevel@tonic-gate 	}
72287c478bd9Sstevel@tonic-gate }
72297c478bd9Sstevel@tonic-gate 
72307c478bd9Sstevel@tonic-gate /*
72317c478bd9Sstevel@tonic-gate  * mdi_phci_get_vhci_private():
72327c478bd9Sstevel@tonic-gate  *		Get the vhci private information associated with the
72337c478bd9Sstevel@tonic-gate  *		mdi_phci node
72347c478bd9Sstevel@tonic-gate  */
72357c478bd9Sstevel@tonic-gate void *
72367c478bd9Sstevel@tonic-gate mdi_phci_get_vhci_private(dev_info_t *dip)
72377c478bd9Sstevel@tonic-gate {
72387c478bd9Sstevel@tonic-gate 	ASSERT(mdi_component_is_phci(dip, NULL) == MDI_SUCCESS);
72397c478bd9Sstevel@tonic-gate 	if (mdi_component_is_phci(dip, NULL) == MDI_SUCCESS) {
72407c478bd9Sstevel@tonic-gate 		mdi_phci_t	*ph;
72417c478bd9Sstevel@tonic-gate 		ph = i_devi_get_phci(dip);
72427c478bd9Sstevel@tonic-gate 		return (ph->ph_vprivate);
72437c478bd9Sstevel@tonic-gate 	}
72447c478bd9Sstevel@tonic-gate 	return (NULL);
72457c478bd9Sstevel@tonic-gate }
72467c478bd9Sstevel@tonic-gate 
72477c478bd9Sstevel@tonic-gate /*
72487c478bd9Sstevel@tonic-gate  * mdi_phci_set_vhci_private():
72497c478bd9Sstevel@tonic-gate  *		Set the vhci private information in the mdi_phci node
72507c478bd9Sstevel@tonic-gate  */
72517c478bd9Sstevel@tonic-gate void
72527c478bd9Sstevel@tonic-gate mdi_phci_set_vhci_private(dev_info_t *dip, void *priv)
72537c478bd9Sstevel@tonic-gate {
72547c478bd9Sstevel@tonic-gate 	ASSERT(mdi_component_is_phci(dip, NULL) == MDI_SUCCESS);
72557c478bd9Sstevel@tonic-gate 	if (mdi_component_is_phci(dip, NULL) == MDI_SUCCESS) {
72567c478bd9Sstevel@tonic-gate 		mdi_phci_t	*ph;
72577c478bd9Sstevel@tonic-gate 		ph = i_devi_get_phci(dip);
72587c478bd9Sstevel@tonic-gate 		ph->ph_vprivate = priv;
72597c478bd9Sstevel@tonic-gate 	}
72607c478bd9Sstevel@tonic-gate }
72613c34adc5Sramat 
72624c06356bSdh142964 int
72634c06356bSdh142964 mdi_pi_ishidden(mdi_pathinfo_t *pip)
72644c06356bSdh142964 {
72654c06356bSdh142964 	return (MDI_PI_FLAGS_IS_HIDDEN(pip));
72664c06356bSdh142964 }
72674c06356bSdh142964 
72684c06356bSdh142964 int
72694c06356bSdh142964 mdi_pi_device_isremoved(mdi_pathinfo_t *pip)
72704c06356bSdh142964 {
72714c06356bSdh142964 	return (MDI_PI_FLAGS_IS_DEVICE_REMOVED(pip));
72724c06356bSdh142964 }
72734c06356bSdh142964 
72744c06356bSdh142964 /*
72754c06356bSdh142964  * When processing hotplug, if mdi_pi_offline-mdi_pi_free fails then this
72764c06356bSdh142964  * interface is used to represent device removal.
72774c06356bSdh142964  */
72784c06356bSdh142964 int
72794c06356bSdh142964 mdi_pi_device_remove(mdi_pathinfo_t *pip)
72804c06356bSdh142964 {
72814c06356bSdh142964 	MDI_PI_LOCK(pip);
72824c06356bSdh142964 	if (mdi_pi_device_isremoved(pip)) {
72834c06356bSdh142964 		MDI_PI_UNLOCK(pip);
72844c06356bSdh142964 		return (0);
72854c06356bSdh142964 	}
72864c06356bSdh142964 	MDI_PI_FLAGS_SET_DEVICE_REMOVED(pip);
72874c06356bSdh142964 	MDI_PI_FLAGS_SET_HIDDEN(pip);
72884c06356bSdh142964 	MDI_PI_UNLOCK(pip);
72894c06356bSdh142964 
72904c06356bSdh142964 	i_ddi_di_cache_invalidate();
72914c06356bSdh142964 
72924c06356bSdh142964 	return (1);
72934c06356bSdh142964 }
72944c06356bSdh142964 
72954c06356bSdh142964 /*
72964c06356bSdh142964  * When processing hotplug, if a path marked mdi_pi_device_isremoved()
72974c06356bSdh142964  * is now accessible then this interfaces is used to represent device insertion.
72984c06356bSdh142964  */
72994c06356bSdh142964 int
73004c06356bSdh142964 mdi_pi_device_insert(mdi_pathinfo_t *pip)
73014c06356bSdh142964 {
73024c06356bSdh142964 	MDI_PI_LOCK(pip);
73034c06356bSdh142964 	if (!mdi_pi_device_isremoved(pip)) {
73044c06356bSdh142964 		MDI_PI_UNLOCK(pip);
73054c06356bSdh142964 		return (0);
73064c06356bSdh142964 	}
73074c06356bSdh142964 	MDI_PI_FLAGS_CLR_DEVICE_REMOVED(pip);
73084c06356bSdh142964 	MDI_PI_FLAGS_CLR_HIDDEN(pip);
73094c06356bSdh142964 	MDI_PI_UNLOCK(pip);
73104c06356bSdh142964 
73114c06356bSdh142964 	i_ddi_di_cache_invalidate();
73124c06356bSdh142964 
73134c06356bSdh142964 	return (1);
73144c06356bSdh142964 }
73154c06356bSdh142964 
73163c34adc5Sramat /*
73173c34adc5Sramat  * List of vhci class names:
73183c34adc5Sramat  * A vhci class name must be in this list only if the corresponding vhci
73193c34adc5Sramat  * driver intends to use the mdi provided bus config implementation
73203c34adc5Sramat  * (i.e., mdi_vhci_bus_config()).
73213c34adc5Sramat  */
73223c34adc5Sramat static char *vhci_class_list[] = { MDI_HCI_CLASS_SCSI, MDI_HCI_CLASS_IB };
73233c34adc5Sramat #define	N_VHCI_CLASSES	(sizeof (vhci_class_list) / sizeof (char *))
73243c34adc5Sramat 
73253c34adc5Sramat /*
73263c34adc5Sramat  * During boot time, the on-disk vhci cache for every vhci class is read
73273c34adc5Sramat  * in the form of an nvlist and stored here.
73283c34adc5Sramat  */
73293c34adc5Sramat static nvlist_t *vhcache_nvl[N_VHCI_CLASSES];
73303c34adc5Sramat 
73313c34adc5Sramat /* nvpair names in vhci cache nvlist */
73323c34adc5Sramat #define	MDI_VHCI_CACHE_VERSION	1
73333c34adc5Sramat #define	MDI_NVPNAME_VERSION	"version"
73343c34adc5Sramat #define	MDI_NVPNAME_PHCIS	"phcis"
73353c34adc5Sramat #define	MDI_NVPNAME_CTADDRMAP	"clientaddrmap"
73363c34adc5Sramat 
73373c34adc5Sramat /*
73383c34adc5Sramat  * Given vhci class name, return its on-disk vhci cache filename.
73393c34adc5Sramat  * Memory for the returned filename which includes the full path is allocated
73403c34adc5Sramat  * by this function.
73413c34adc5Sramat  */
73423c34adc5Sramat static char *
73433c34adc5Sramat vhclass2vhcache_filename(char *vhclass)
73443c34adc5Sramat {
73453c34adc5Sramat 	char *filename;
73463c34adc5Sramat 	int len;
73473c34adc5Sramat 	static char *fmt = "/etc/devices/mdi_%s_cache";
73483c34adc5Sramat 
73493c34adc5Sramat 	/*
73503c34adc5Sramat 	 * fmt contains the on-disk vhci cache file name format;
73513c34adc5Sramat 	 * for scsi_vhci the filename is "/etc/devices/mdi_scsi_vhci_cache".
73523c34adc5Sramat 	 */
73533c34adc5Sramat 
73543c34adc5Sramat 	/* the -1 below is to account for "%s" in the format string */
73553c34adc5Sramat 	len = strlen(fmt) + strlen(vhclass) - 1;
73563c34adc5Sramat 	filename = kmem_alloc(len, KM_SLEEP);
73573c34adc5Sramat 	(void) snprintf(filename, len, fmt, vhclass);
73583c34adc5Sramat 	ASSERT(len == (strlen(filename) + 1));
73593c34adc5Sramat 	return (filename);
73603c34adc5Sramat }
73613c34adc5Sramat 
73623c34adc5Sramat /*
73633c34adc5Sramat  * initialize the vhci cache related data structures and read the on-disk
73643c34adc5Sramat  * vhci cached data into memory.
73653c34adc5Sramat  */
73663c34adc5Sramat static void
73673c34adc5Sramat setup_vhci_cache(mdi_vhci_t *vh)
73683c34adc5Sramat {
73693c34adc5Sramat 	mdi_vhci_config_t *vhc;
73703c34adc5Sramat 	mdi_vhci_cache_t *vhcache;
73713c34adc5Sramat 	int i;
73723c34adc5Sramat 	nvlist_t *nvl = NULL;
73733c34adc5Sramat 
73743c34adc5Sramat 	vhc = kmem_zalloc(sizeof (mdi_vhci_config_t), KM_SLEEP);
73753c34adc5Sramat 	vh->vh_config = vhc;
73763c34adc5Sramat 	vhcache = &vhc->vhc_vhcache;
73773c34adc5Sramat 
73783c34adc5Sramat 	vhc->vhc_vhcache_filename = vhclass2vhcache_filename(vh->vh_class);
73793c34adc5Sramat 
73803c34adc5Sramat 	mutex_init(&vhc->vhc_lock, NULL, MUTEX_DEFAULT, NULL);
73813c34adc5Sramat 	cv_init(&vhc->vhc_cv, NULL, CV_DRIVER, NULL);
73823c34adc5Sramat 
73833c34adc5Sramat 	rw_init(&vhcache->vhcache_lock, NULL, RW_DRIVER, NULL);
73843c34adc5Sramat 
73853c34adc5Sramat 	/*
73863c34adc5Sramat 	 * Create string hash; same as mod_hash_create_strhash() except that
73873c34adc5Sramat 	 * we use NULL key destructor.
73883c34adc5Sramat 	 */
73893c34adc5Sramat 	vhcache->vhcache_client_hash = mod_hash_create_extended(vh->vh_class,
73903c34adc5Sramat 	    mdi_bus_config_cache_hash_size,
73913c34adc5Sramat 	    mod_hash_null_keydtor, mod_hash_null_valdtor,
73923c34adc5Sramat 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
73933c34adc5Sramat 
73943c34adc5Sramat 	/*
73953c34adc5Sramat 	 * The on-disk vhci cache is read during booting prior to the
73963c34adc5Sramat 	 * lights-out period by mdi_read_devices_files().
73973c34adc5Sramat 	 */
73983c34adc5Sramat 	for (i = 0; i < N_VHCI_CLASSES; i++) {
73993c34adc5Sramat 		if (strcmp(vhci_class_list[i], vh->vh_class) == 0) {
74003c34adc5Sramat 			nvl = vhcache_nvl[i];
74013c34adc5Sramat 			vhcache_nvl[i] = NULL;
74023c34adc5Sramat 			break;
74033c34adc5Sramat 		}
74043c34adc5Sramat 	}
74053c34adc5Sramat 
74063c34adc5Sramat 	/*
74073c34adc5Sramat 	 * this is to cover the case of some one manually causing unloading
74083c34adc5Sramat 	 * (or detaching) and reloading (or attaching) of a vhci driver.
74093c34adc5Sramat 	 */
74103c34adc5Sramat 	if (nvl == NULL && modrootloaded)
74113c34adc5Sramat 		nvl = read_on_disk_vhci_cache(vh->vh_class);
74123c34adc5Sramat 
74133c34adc5Sramat 	if (nvl != NULL) {
74143c34adc5Sramat 		rw_enter(&vhcache->vhcache_lock, RW_WRITER);
74153c34adc5Sramat 		if (mainnvl_to_vhcache(vhcache, nvl) == MDI_SUCCESS)
74163c34adc5Sramat 			vhcache->vhcache_flags |= MDI_VHCI_CACHE_SETUP_DONE;
74173c34adc5Sramat 		else  {
74183c34adc5Sramat 			cmn_err(CE_WARN,
74194c06356bSdh142964 			    "%s: data file corrupted, will recreate",
74203c34adc5Sramat 			    vhc->vhc_vhcache_filename);
74213c34adc5Sramat 		}
74223c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
74233c34adc5Sramat 		nvlist_free(nvl);
74243c34adc5Sramat 	}
74253c34adc5Sramat 
74263c34adc5Sramat 	vhc->vhc_cbid = callb_add(stop_vhcache_flush_thread, vhc,
74273c34adc5Sramat 	    CB_CL_UADMIN_PRE_VFS, "mdi_vhcache_flush");
742867e56d35Sramat 
742967e56d35Sramat 	vhc->vhc_path_discovery_boot = mdi_path_discovery_boot;
743067e56d35Sramat 	vhc->vhc_path_discovery_postboot = mdi_path_discovery_postboot;
74313c34adc5Sramat }
74323c34adc5Sramat 
74333c34adc5Sramat /*
74343c34adc5Sramat  * free all vhci cache related resources
74353c34adc5Sramat  */
74363c34adc5Sramat static int
74373c34adc5Sramat destroy_vhci_cache(mdi_vhci_t *vh)
74383c34adc5Sramat {
74393c34adc5Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
74403c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
74413c34adc5Sramat 	mdi_vhcache_phci_t *cphci, *cphci_next;
74423c34adc5Sramat 	mdi_vhcache_client_t *cct, *cct_next;
74433c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi, *cpi_next;
74443c34adc5Sramat 
74453c34adc5Sramat 	if (stop_vhcache_async_threads(vhc) != MDI_SUCCESS)
74463c34adc5Sramat 		return (MDI_FAILURE);
74473c34adc5Sramat 
74483c34adc5Sramat 	kmem_free(vhc->vhc_vhcache_filename,
74493c34adc5Sramat 	    strlen(vhc->vhc_vhcache_filename) + 1);
74503c34adc5Sramat 
74513c34adc5Sramat 	mod_hash_destroy_strhash(vhcache->vhcache_client_hash);
74523c34adc5Sramat 
74533c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
74543c34adc5Sramat 	    cphci = cphci_next) {
74553c34adc5Sramat 		cphci_next = cphci->cphci_next;
74563c34adc5Sramat 		free_vhcache_phci(cphci);
74573c34adc5Sramat 	}
74583c34adc5Sramat 
74593c34adc5Sramat 	for (cct = vhcache->vhcache_client_head; cct != NULL; cct = cct_next) {
74603c34adc5Sramat 		cct_next = cct->cct_next;
74613c34adc5Sramat 		for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi_next) {
74623c34adc5Sramat 			cpi_next = cpi->cpi_next;
74633c34adc5Sramat 			free_vhcache_pathinfo(cpi);
74643c34adc5Sramat 		}
74653c34adc5Sramat 		free_vhcache_client(cct);
74663c34adc5Sramat 	}
74673c34adc5Sramat 
74683c34adc5Sramat 	rw_destroy(&vhcache->vhcache_lock);
74693c34adc5Sramat 
74703c34adc5Sramat 	mutex_destroy(&vhc->vhc_lock);
74713c34adc5Sramat 	cv_destroy(&vhc->vhc_cv);
74723c34adc5Sramat 	kmem_free(vhc, sizeof (mdi_vhci_config_t));
74733c34adc5Sramat 	return (MDI_SUCCESS);
74743c34adc5Sramat }
74753c34adc5Sramat 
74763c34adc5Sramat /*
74773c34adc5Sramat  * Stop all vhci cache related async threads and free their resources.
74783c34adc5Sramat  */
74793c34adc5Sramat static int
74803c34adc5Sramat stop_vhcache_async_threads(mdi_vhci_config_t *vhc)
74813c34adc5Sramat {
74823c34adc5Sramat 	mdi_async_client_config_t *acc, *acc_next;
74833c34adc5Sramat 
74843c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
74853c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_EXIT;
74863c34adc5Sramat 	ASSERT(vhc->vhc_acc_thrcount >= 0);
74873c34adc5Sramat 	cv_broadcast(&vhc->vhc_cv);
74883c34adc5Sramat 
74893c34adc5Sramat 	while ((vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) ||
74903c34adc5Sramat 	    vhc->vhc_acc_thrcount != 0) {
74913c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
749296c4a178SChris Horne 		delay_random(mdi_delay);
74933c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
74943c34adc5Sramat 	}
74953c34adc5Sramat 
74963c34adc5Sramat 	vhc->vhc_flags &= ~MDI_VHC_EXIT;
74973c34adc5Sramat 
74983c34adc5Sramat 	for (acc = vhc->vhc_acc_list_head; acc != NULL; acc = acc_next) {
74993c34adc5Sramat 		acc_next = acc->acc_next;
75003c34adc5Sramat 		free_async_client_config(acc);
75013c34adc5Sramat 	}
75023c34adc5Sramat 	vhc->vhc_acc_list_head = NULL;
75033c34adc5Sramat 	vhc->vhc_acc_list_tail = NULL;
75043c34adc5Sramat 	vhc->vhc_acc_count = 0;
75053c34adc5Sramat 
75063c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) {
75073c34adc5Sramat 		vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
75083c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
75093c34adc5Sramat 		if (flush_vhcache(vhc, 0) != MDI_SUCCESS) {
75103c34adc5Sramat 			vhcache_dirty(vhc);
75113c34adc5Sramat 			return (MDI_FAILURE);
75123c34adc5Sramat 		}
75133c34adc5Sramat 	} else
75143c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
75153c34adc5Sramat 
75163c34adc5Sramat 	if (callb_delete(vhc->vhc_cbid) != 0)
75173c34adc5Sramat 		return (MDI_FAILURE);
75183c34adc5Sramat 
75193c34adc5Sramat 	return (MDI_SUCCESS);
75203c34adc5Sramat }
75213c34adc5Sramat 
75223c34adc5Sramat /*
75233c34adc5Sramat  * Stop vhci cache flush thread
75243c34adc5Sramat  */
75253c34adc5Sramat /* ARGSUSED */
75263c34adc5Sramat static boolean_t
75273c34adc5Sramat stop_vhcache_flush_thread(void *arg, int code)
75283c34adc5Sramat {
75293c34adc5Sramat 	mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
75303c34adc5Sramat 
75313c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
75323c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_EXIT;
75333c34adc5Sramat 	cv_broadcast(&vhc->vhc_cv);
75343c34adc5Sramat 
75353c34adc5Sramat 	while (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) {
75363c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
753796c4a178SChris Horne 		delay_random(mdi_delay);
75383c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
75393c34adc5Sramat 	}
75403c34adc5Sramat 
75413c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) {
75423c34adc5Sramat 		vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
75433c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
75443c34adc5Sramat 		(void) flush_vhcache(vhc, 1);
75453c34adc5Sramat 	} else
75463c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
75473c34adc5Sramat 
75483c34adc5Sramat 	return (B_TRUE);
75493c34adc5Sramat }
75503c34adc5Sramat 
75513c34adc5Sramat /*
75523c34adc5Sramat  * Enqueue the vhcache phci (cphci) at the tail of the list
75533c34adc5Sramat  */
75543c34adc5Sramat static void
75553c34adc5Sramat enqueue_vhcache_phci(mdi_vhci_cache_t *vhcache, mdi_vhcache_phci_t *cphci)
75563c34adc5Sramat {
75573c34adc5Sramat 	cphci->cphci_next = NULL;
75583c34adc5Sramat 	if (vhcache->vhcache_phci_head == NULL)
75593c34adc5Sramat 		vhcache->vhcache_phci_head = cphci;
75603c34adc5Sramat 	else
75613c34adc5Sramat 		vhcache->vhcache_phci_tail->cphci_next = cphci;
75623c34adc5Sramat 	vhcache->vhcache_phci_tail = cphci;
75633c34adc5Sramat }
75643c34adc5Sramat 
75653c34adc5Sramat /*
75663c34adc5Sramat  * Enqueue the vhcache pathinfo (cpi) at the tail of the list
75673c34adc5Sramat  */
75683c34adc5Sramat static void
75693c34adc5Sramat enqueue_tail_vhcache_pathinfo(mdi_vhcache_client_t *cct,
75703c34adc5Sramat     mdi_vhcache_pathinfo_t *cpi)
75713c34adc5Sramat {
75723c34adc5Sramat 	cpi->cpi_next = NULL;
75733c34adc5Sramat 	if (cct->cct_cpi_head == NULL)
75743c34adc5Sramat 		cct->cct_cpi_head = cpi;
75753c34adc5Sramat 	else
75763c34adc5Sramat 		cct->cct_cpi_tail->cpi_next = cpi;
75773c34adc5Sramat 	cct->cct_cpi_tail = cpi;
75783c34adc5Sramat }
75793c34adc5Sramat 
75803c34adc5Sramat /*
75813c34adc5Sramat  * Enqueue the vhcache pathinfo (cpi) at the correct location in the
75823c34adc5Sramat  * ordered list. All cpis which do not have MDI_CPI_HINT_PATH_DOES_NOT_EXIST
75833c34adc5Sramat  * flag set come at the beginning of the list. All cpis which have this
75843c34adc5Sramat  * flag set come at the end of the list.
75853c34adc5Sramat  */
75863c34adc5Sramat static void
75873c34adc5Sramat enqueue_vhcache_pathinfo(mdi_vhcache_client_t *cct,
75883c34adc5Sramat     mdi_vhcache_pathinfo_t *newcpi)
75893c34adc5Sramat {
75903c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi, *prev_cpi;
75913c34adc5Sramat 
75923c34adc5Sramat 	if (cct->cct_cpi_head == NULL ||
75933c34adc5Sramat 	    (newcpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST))
75943c34adc5Sramat 		enqueue_tail_vhcache_pathinfo(cct, newcpi);
75953c34adc5Sramat 	else {
75963c34adc5Sramat 		for (cpi = cct->cct_cpi_head, prev_cpi = NULL; cpi != NULL &&
75973c34adc5Sramat 		    !(cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST);
75983c34adc5Sramat 		    prev_cpi = cpi, cpi = cpi->cpi_next)
75993c34adc5Sramat 			;
76003c34adc5Sramat 
76013c34adc5Sramat 		if (prev_cpi == NULL)
76023c34adc5Sramat 			cct->cct_cpi_head = newcpi;
76033c34adc5Sramat 		else
76043c34adc5Sramat 			prev_cpi->cpi_next = newcpi;
76053c34adc5Sramat 
76063c34adc5Sramat 		newcpi->cpi_next = cpi;
76073c34adc5Sramat 
76083c34adc5Sramat 		if (cpi == NULL)
76093c34adc5Sramat 			cct->cct_cpi_tail = newcpi;
76103c34adc5Sramat 	}
76113c34adc5Sramat }
76123c34adc5Sramat 
76133c34adc5Sramat /*
76143c34adc5Sramat  * Enqueue the vhcache client (cct) at the tail of the list
76153c34adc5Sramat  */
76163c34adc5Sramat static void
76173c34adc5Sramat enqueue_vhcache_client(mdi_vhci_cache_t *vhcache,
76183c34adc5Sramat     mdi_vhcache_client_t *cct)
76193c34adc5Sramat {
76203c34adc5Sramat 	cct->cct_next = NULL;
76213c34adc5Sramat 	if (vhcache->vhcache_client_head == NULL)
76223c34adc5Sramat 		vhcache->vhcache_client_head = cct;
76233c34adc5Sramat 	else
76243c34adc5Sramat 		vhcache->vhcache_client_tail->cct_next = cct;
76253c34adc5Sramat 	vhcache->vhcache_client_tail = cct;
76263c34adc5Sramat }
76273c34adc5Sramat 
76283c34adc5Sramat static void
76293c34adc5Sramat free_string_array(char **str, int nelem)
76303c34adc5Sramat {
76313c34adc5Sramat 	int i;
76323c34adc5Sramat 
76333c34adc5Sramat 	if (str) {
76343c34adc5Sramat 		for (i = 0; i < nelem; i++) {
76353c34adc5Sramat 			if (str[i])
76363c34adc5Sramat 				kmem_free(str[i], strlen(str[i]) + 1);
76373c34adc5Sramat 		}
76383c34adc5Sramat 		kmem_free(str, sizeof (char *) * nelem);
76393c34adc5Sramat 	}
76403c34adc5Sramat }
76413c34adc5Sramat 
76423c34adc5Sramat static void
76433c34adc5Sramat free_vhcache_phci(mdi_vhcache_phci_t *cphci)
76443c34adc5Sramat {
76453c34adc5Sramat 	kmem_free(cphci->cphci_path, strlen(cphci->cphci_path) + 1);
76463c34adc5Sramat 	kmem_free(cphci, sizeof (*cphci));
76473c34adc5Sramat }
76483c34adc5Sramat 
76493c34adc5Sramat static void
76503c34adc5Sramat free_vhcache_pathinfo(mdi_vhcache_pathinfo_t *cpi)
76513c34adc5Sramat {
76523c34adc5Sramat 	kmem_free(cpi->cpi_addr, strlen(cpi->cpi_addr) + 1);
76533c34adc5Sramat 	kmem_free(cpi, sizeof (*cpi));
76543c34adc5Sramat }
76553c34adc5Sramat 
76563c34adc5Sramat static void
76573c34adc5Sramat free_vhcache_client(mdi_vhcache_client_t *cct)
76583c34adc5Sramat {
76593c34adc5Sramat 	kmem_free(cct->cct_name_addr, strlen(cct->cct_name_addr) + 1);
76603c34adc5Sramat 	kmem_free(cct, sizeof (*cct));
76613c34adc5Sramat }
76623c34adc5Sramat 
76633c34adc5Sramat static char *
76643c34adc5Sramat vhcache_mknameaddr(char *ct_name, char *ct_addr, int *ret_len)
76653c34adc5Sramat {
76663c34adc5Sramat 	char *name_addr;
76673c34adc5Sramat 	int len;
76683c34adc5Sramat 
76693c34adc5Sramat 	len = strlen(ct_name) + strlen(ct_addr) + 2;
76703c34adc5Sramat 	name_addr = kmem_alloc(len, KM_SLEEP);
76713c34adc5Sramat 	(void) snprintf(name_addr, len, "%s@%s", ct_name, ct_addr);
76723c34adc5Sramat 
76733c34adc5Sramat 	if (ret_len)
76743c34adc5Sramat 		*ret_len = len;
76753c34adc5Sramat 	return (name_addr);
76763c34adc5Sramat }
76773c34adc5Sramat 
76783c34adc5Sramat /*
76793c34adc5Sramat  * Copy the contents of paddrnvl to vhci cache.
76803c34adc5Sramat  * paddrnvl nvlist contains path information for a vhci client.
76813c34adc5Sramat  * See the comment in mainnvl_to_vhcache() for the format of this nvlist.
76823c34adc5Sramat  */
76833c34adc5Sramat static void
76843c34adc5Sramat paddrnvl_to_vhcache(nvlist_t *nvl, mdi_vhcache_phci_t *cphci_list[],
76853c34adc5Sramat     mdi_vhcache_client_t *cct)
76863c34adc5Sramat {
76873c34adc5Sramat 	nvpair_t *nvp = NULL;
76883c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
76893c34adc5Sramat 	uint_t nelem;
76903c34adc5Sramat 	uint32_t *val;
76913c34adc5Sramat 
76923c34adc5Sramat 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
76933c34adc5Sramat 		ASSERT(nvpair_type(nvp) == DATA_TYPE_UINT32_ARRAY);
76943c34adc5Sramat 		cpi = kmem_zalloc(sizeof (*cpi), KM_SLEEP);
76953c34adc5Sramat 		cpi->cpi_addr = i_ddi_strdup(nvpair_name(nvp), KM_SLEEP);
76963c34adc5Sramat 		(void) nvpair_value_uint32_array(nvp, &val, &nelem);
76973c34adc5Sramat 		ASSERT(nelem == 2);
76983c34adc5Sramat 		cpi->cpi_cphci = cphci_list[val[0]];
76993c34adc5Sramat 		cpi->cpi_flags = val[1];
77003c34adc5Sramat 		enqueue_tail_vhcache_pathinfo(cct, cpi);
77013c34adc5Sramat 	}
77023c34adc5Sramat }
77033c34adc5Sramat 
77043c34adc5Sramat /*
77053c34adc5Sramat  * Copy the contents of caddrmapnvl to vhci cache.
77063c34adc5Sramat  * caddrmapnvl nvlist contains vhci client address to phci client address
77073c34adc5Sramat  * mappings. See the comment in mainnvl_to_vhcache() for the format of
77083c34adc5Sramat  * this nvlist.
77093c34adc5Sramat  */
77103c34adc5Sramat static void
77113c34adc5Sramat caddrmapnvl_to_vhcache(mdi_vhci_cache_t *vhcache, nvlist_t *nvl,
77123c34adc5Sramat     mdi_vhcache_phci_t *cphci_list[])
77133c34adc5Sramat {
77143c34adc5Sramat 	nvpair_t *nvp = NULL;
77153c34adc5Sramat 	nvlist_t *paddrnvl;
77163c34adc5Sramat 	mdi_vhcache_client_t *cct;
77173c34adc5Sramat 
77183c34adc5Sramat 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
77193c34adc5Sramat 		ASSERT(nvpair_type(nvp) == DATA_TYPE_NVLIST);
77203c34adc5Sramat 		cct = kmem_zalloc(sizeof (*cct), KM_SLEEP);
77213c34adc5Sramat 		cct->cct_name_addr = i_ddi_strdup(nvpair_name(nvp), KM_SLEEP);
77223c34adc5Sramat 		(void) nvpair_value_nvlist(nvp, &paddrnvl);
77233c34adc5Sramat 		paddrnvl_to_vhcache(paddrnvl, cphci_list, cct);
77243c34adc5Sramat 		/* the client must contain at least one path */
77253c34adc5Sramat 		ASSERT(cct->cct_cpi_head != NULL);
77263c34adc5Sramat 
77273c34adc5Sramat 		enqueue_vhcache_client(vhcache, cct);
77283c34adc5Sramat 		(void) mod_hash_insert(vhcache->vhcache_client_hash,
77293c34adc5Sramat 		    (mod_hash_key_t)cct->cct_name_addr, (mod_hash_val_t)cct);
77303c34adc5Sramat 	}
77313c34adc5Sramat }
77323c34adc5Sramat 
77333c34adc5Sramat /*
77343c34adc5Sramat  * Copy the contents of the main nvlist to vhci cache.
77353c34adc5Sramat  *
77363c34adc5Sramat  * VHCI busconfig cached data is stored in the form of a nvlist on the disk.
77373c34adc5Sramat  * The nvlist contains the mappings between the vhci client addresses and
77383c34adc5Sramat  * their corresponding phci client addresses.
77393c34adc5Sramat  *
77403c34adc5Sramat  * The structure of the nvlist is as follows:
77413c34adc5Sramat  *
77423c34adc5Sramat  * Main nvlist:
77433c34adc5Sramat  *	NAME		TYPE		DATA
77443c34adc5Sramat  *	version		int32		version number
77453c34adc5Sramat  *	phcis		string array	array of phci paths
77463c34adc5Sramat  *	clientaddrmap	nvlist_t	c2paddrs_nvl (see below)
77473c34adc5Sramat  *
77483c34adc5Sramat  * structure of c2paddrs_nvl:
77493c34adc5Sramat  *	NAME		TYPE		DATA
77503c34adc5Sramat  *	caddr1		nvlist_t	paddrs_nvl1
77513c34adc5Sramat  *	caddr2		nvlist_t	paddrs_nvl2
77523c34adc5Sramat  *	...
77533c34adc5Sramat  * where caddr1, caddr2, ... are vhci client name and addresses in the
77543c34adc5Sramat  * form of "<clientname>@<clientaddress>".
77553c34adc5Sramat  * (for example: "ssd@2000002037cd9f72");
77563c34adc5Sramat  * paddrs_nvl1, paddrs_nvl2, .. are nvlists that contain path information.
77573c34adc5Sramat  *
77583c34adc5Sramat  * structure of paddrs_nvl:
77593c34adc5Sramat  *	NAME		TYPE		DATA
77603c34adc5Sramat  *	pi_addr1	uint32_array	(phci-id, cpi_flags)
77613c34adc5Sramat  *	pi_addr2	uint32_array	(phci-id, cpi_flags)
77623c34adc5Sramat  *	...
77633c34adc5Sramat  * where pi_addr1, pi_addr2, ... are bus specific addresses of pathinfo nodes
77643c34adc5Sramat  * (so called pi_addrs, for example: "w2100002037cd9f72,0");
77654c06356bSdh142964  * phci-ids are integers that identify pHCIs to which the
77663c34adc5Sramat  * the bus specific address belongs to. These integers are used as an index
77674c06356bSdh142964  * into to the phcis string array in the main nvlist to get the pHCI path.
77683c34adc5Sramat  */
77693c34adc5Sramat static int
77703c34adc5Sramat mainnvl_to_vhcache(mdi_vhci_cache_t *vhcache, nvlist_t *nvl)
77713c34adc5Sramat {
77723c34adc5Sramat 	char **phcis, **phci_namep;
77733c34adc5Sramat 	uint_t nphcis;
77743c34adc5Sramat 	mdi_vhcache_phci_t *cphci, **cphci_list;
77753c34adc5Sramat 	nvlist_t *caddrmapnvl;
77763c34adc5Sramat 	int32_t ver;
77773c34adc5Sramat 	int i;
77783c34adc5Sramat 	size_t cphci_list_size;
77793c34adc5Sramat 
77803c34adc5Sramat 	ASSERT(RW_WRITE_HELD(&vhcache->vhcache_lock));
77813c34adc5Sramat 
77823c34adc5Sramat 	if (nvlist_lookup_int32(nvl, MDI_NVPNAME_VERSION, &ver) != 0 ||
77833c34adc5Sramat 	    ver != MDI_VHCI_CACHE_VERSION)
77843c34adc5Sramat 		return (MDI_FAILURE);
77853c34adc5Sramat 
77863c34adc5Sramat 	if (nvlist_lookup_string_array(nvl, MDI_NVPNAME_PHCIS, &phcis,
77873c34adc5Sramat 	    &nphcis) != 0)
77883c34adc5Sramat 		return (MDI_SUCCESS);
77893c34adc5Sramat 
77903c34adc5Sramat 	ASSERT(nphcis > 0);
77913c34adc5Sramat 
77923c34adc5Sramat 	cphci_list_size = sizeof (mdi_vhcache_phci_t *) * nphcis;
77933c34adc5Sramat 	cphci_list = kmem_alloc(cphci_list_size, KM_SLEEP);
77943c34adc5Sramat 	for (i = 0, phci_namep = phcis; i < nphcis; i++, phci_namep++) {
77953c34adc5Sramat 		cphci = kmem_zalloc(sizeof (mdi_vhcache_phci_t), KM_SLEEP);
77963c34adc5Sramat 		cphci->cphci_path = i_ddi_strdup(*phci_namep, KM_SLEEP);
77973c34adc5Sramat 		enqueue_vhcache_phci(vhcache, cphci);
77983c34adc5Sramat 		cphci_list[i] = cphci;
77993c34adc5Sramat 	}
78003c34adc5Sramat 
78013c34adc5Sramat 	ASSERT(vhcache->vhcache_phci_head != NULL);
78023c34adc5Sramat 
78033c34adc5Sramat 	if (nvlist_lookup_nvlist(nvl, MDI_NVPNAME_CTADDRMAP, &caddrmapnvl) == 0)
78043c34adc5Sramat 		caddrmapnvl_to_vhcache(vhcache, caddrmapnvl, cphci_list);
78053c34adc5Sramat 
78063c34adc5Sramat 	kmem_free(cphci_list, cphci_list_size);
78073c34adc5Sramat 	return (MDI_SUCCESS);
78083c34adc5Sramat }
78093c34adc5Sramat 
78103c34adc5Sramat /*
78113c34adc5Sramat  * Build paddrnvl for the specified client using the information in the
78123c34adc5Sramat  * vhci cache and add it to the caddrmapnnvl.
78133c34adc5Sramat  * Returns 0 on success, errno on failure.
78143c34adc5Sramat  */
78153c34adc5Sramat static int
78163c34adc5Sramat vhcache_to_paddrnvl(mdi_vhci_cache_t *vhcache, mdi_vhcache_client_t *cct,
78173c34adc5Sramat     nvlist_t *caddrmapnvl)
78183c34adc5Sramat {
78193c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
78203c34adc5Sramat 	nvlist_t *nvl;
78213c34adc5Sramat 	int err;
78223c34adc5Sramat 	uint32_t val[2];
78233c34adc5Sramat 
78243c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
78253c34adc5Sramat 
78263c34adc5Sramat 	if ((err = nvlist_alloc(&nvl, 0, KM_SLEEP)) != 0)
78273c34adc5Sramat 		return (err);
78283c34adc5Sramat 
78293c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
78303c34adc5Sramat 		val[0] = cpi->cpi_cphci->cphci_id;
78313c34adc5Sramat 		val[1] = cpi->cpi_flags;
78323c34adc5Sramat 		if ((err = nvlist_add_uint32_array(nvl, cpi->cpi_addr, val, 2))
78333c34adc5Sramat 		    != 0)
78343c34adc5Sramat 			goto out;
78353c34adc5Sramat 	}
78363c34adc5Sramat 
78373c34adc5Sramat 	err = nvlist_add_nvlist(caddrmapnvl, cct->cct_name_addr, nvl);
78383c34adc5Sramat out:
78393c34adc5Sramat 	nvlist_free(nvl);
78403c34adc5Sramat 	return (err);
78413c34adc5Sramat }
78423c34adc5Sramat 
78433c34adc5Sramat /*
78443c34adc5Sramat  * Build caddrmapnvl using the information in the vhci cache
78453c34adc5Sramat  * and add it to the mainnvl.
78463c34adc5Sramat  * Returns 0 on success, errno on failure.
78473c34adc5Sramat  */
78483c34adc5Sramat static int
78493c34adc5Sramat vhcache_to_caddrmapnvl(mdi_vhci_cache_t *vhcache, nvlist_t *mainnvl)
78503c34adc5Sramat {
78513c34adc5Sramat 	mdi_vhcache_client_t *cct;
78523c34adc5Sramat 	nvlist_t *nvl;
78533c34adc5Sramat 	int err;
78543c34adc5Sramat 
78553c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
78563c34adc5Sramat 
78573c34adc5Sramat 	if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) != 0)
78583c34adc5Sramat 		return (err);
78593c34adc5Sramat 
78603c34adc5Sramat 	for (cct = vhcache->vhcache_client_head; cct != NULL;
78613c34adc5Sramat 	    cct = cct->cct_next) {
78623c34adc5Sramat 		if ((err = vhcache_to_paddrnvl(vhcache, cct, nvl)) != 0)
78633c34adc5Sramat 			goto out;
78643c34adc5Sramat 	}
78653c34adc5Sramat 
78663c34adc5Sramat 	err = nvlist_add_nvlist(mainnvl, MDI_NVPNAME_CTADDRMAP, nvl);
78673c34adc5Sramat out:
78683c34adc5Sramat 	nvlist_free(nvl);
78693c34adc5Sramat 	return (err);
78703c34adc5Sramat }
78713c34adc5Sramat 
78723c34adc5Sramat /*
78733c34adc5Sramat  * Build nvlist using the information in the vhci cache.
78743c34adc5Sramat  * See the comment in mainnvl_to_vhcache() for the format of the nvlist.
78753c34adc5Sramat  * Returns nvl on success, NULL on failure.
78763c34adc5Sramat  */
78773c34adc5Sramat static nvlist_t *
78783c34adc5Sramat vhcache_to_mainnvl(mdi_vhci_cache_t *vhcache)
78793c34adc5Sramat {
78803c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
78813c34adc5Sramat 	uint_t phci_count;
78823c34adc5Sramat 	char **phcis;
78833c34adc5Sramat 	nvlist_t *nvl;
78843c34adc5Sramat 	int err, i;
78853c34adc5Sramat 
78863c34adc5Sramat 	if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) != 0) {
78873c34adc5Sramat 		nvl = NULL;
78883c34adc5Sramat 		goto out;
78893c34adc5Sramat 	}
78903c34adc5Sramat 
78913c34adc5Sramat 	if ((err = nvlist_add_int32(nvl, MDI_NVPNAME_VERSION,
78923c34adc5Sramat 	    MDI_VHCI_CACHE_VERSION)) != 0)
78933c34adc5Sramat 		goto out;
78943c34adc5Sramat 
78953c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
78963c34adc5Sramat 	if (vhcache->vhcache_phci_head == NULL) {
78973c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
78983c34adc5Sramat 		return (nvl);
78993c34adc5Sramat 	}
79003c34adc5Sramat 
79013c34adc5Sramat 	phci_count = 0;
79023c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
79033c34adc5Sramat 	    cphci = cphci->cphci_next)
79043c34adc5Sramat 		cphci->cphci_id = phci_count++;
79053c34adc5Sramat 
79063c34adc5Sramat 	/* build phci pathname list */
79073c34adc5Sramat 	phcis = kmem_alloc(sizeof (char *) * phci_count, KM_SLEEP);
79083c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head, i = 0; cphci != NULL;
79093c34adc5Sramat 	    cphci = cphci->cphci_next, i++)
79103c34adc5Sramat 		phcis[i] = i_ddi_strdup(cphci->cphci_path, KM_SLEEP);
79113c34adc5Sramat 
79123c34adc5Sramat 	err = nvlist_add_string_array(nvl, MDI_NVPNAME_PHCIS, phcis,
79133c34adc5Sramat 	    phci_count);
79143c34adc5Sramat 	free_string_array(phcis, phci_count);
79153c34adc5Sramat 
79163c34adc5Sramat 	if (err == 0 &&
79173c34adc5Sramat 	    (err = vhcache_to_caddrmapnvl(vhcache, nvl)) == 0) {
79183c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
79193c34adc5Sramat 		return (nvl);
79203c34adc5Sramat 	}
79213c34adc5Sramat 
79223c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
79233c34adc5Sramat out:
79243c34adc5Sramat 	if (nvl)
79253c34adc5Sramat 		nvlist_free(nvl);
79263c34adc5Sramat 	return (NULL);
79273c34adc5Sramat }
79283c34adc5Sramat 
79293c34adc5Sramat /*
79303c34adc5Sramat  * Lookup vhcache phci structure for the specified phci path.
79313c34adc5Sramat  */
79323c34adc5Sramat static mdi_vhcache_phci_t *
79333c34adc5Sramat lookup_vhcache_phci_by_name(mdi_vhci_cache_t *vhcache, char *phci_path)
79343c34adc5Sramat {
79353c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
79363c34adc5Sramat 
79373c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
79383c34adc5Sramat 
79393c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
79403c34adc5Sramat 	    cphci = cphci->cphci_next) {
79413c34adc5Sramat 		if (strcmp(cphci->cphci_path, phci_path) == 0)
79423c34adc5Sramat 			return (cphci);
79433c34adc5Sramat 	}
79443c34adc5Sramat 
79453c34adc5Sramat 	return (NULL);
79463c34adc5Sramat }
79473c34adc5Sramat 
79483c34adc5Sramat /*
79493c34adc5Sramat  * Lookup vhcache phci structure for the specified phci.
79503c34adc5Sramat  */
79513c34adc5Sramat static mdi_vhcache_phci_t *
79523c34adc5Sramat lookup_vhcache_phci_by_addr(mdi_vhci_cache_t *vhcache, mdi_phci_t *ph)
79533c34adc5Sramat {
79543c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
79553c34adc5Sramat 
79563c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
79573c34adc5Sramat 
79583c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
79593c34adc5Sramat 	    cphci = cphci->cphci_next) {
79603c34adc5Sramat 		if (cphci->cphci_phci == ph)
79613c34adc5Sramat 			return (cphci);
79623c34adc5Sramat 	}
79633c34adc5Sramat 
79643c34adc5Sramat 	return (NULL);
79653c34adc5Sramat }
79663c34adc5Sramat 
79673c34adc5Sramat /*
79683c34adc5Sramat  * Add the specified phci to the vhci cache if not already present.
79693c34adc5Sramat  */
79703c34adc5Sramat static void
79713c34adc5Sramat vhcache_phci_add(mdi_vhci_config_t *vhc, mdi_phci_t *ph)
79723c34adc5Sramat {
79733c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
79743c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
79753c34adc5Sramat 	char *pathname;
79763c34adc5Sramat 	int cache_updated;
79773c34adc5Sramat 
79783c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
79793c34adc5Sramat 
79803c34adc5Sramat 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
79813c34adc5Sramat 	(void) ddi_pathname(ph->ph_dip, pathname);
79823c34adc5Sramat 	if ((cphci = lookup_vhcache_phci_by_name(vhcache, pathname))
79833c34adc5Sramat 	    != NULL) {
79843c34adc5Sramat 		cphci->cphci_phci = ph;
79853c34adc5Sramat 		cache_updated = 0;
79863c34adc5Sramat 	} else {
79873c34adc5Sramat 		cphci = kmem_zalloc(sizeof (*cphci), KM_SLEEP);
79883c34adc5Sramat 		cphci->cphci_path = i_ddi_strdup(pathname, KM_SLEEP);
79893c34adc5Sramat 		cphci->cphci_phci = ph;
79903c34adc5Sramat 		enqueue_vhcache_phci(vhcache, cphci);
79913c34adc5Sramat 		cache_updated = 1;
79923c34adc5Sramat 	}
799367e56d35Sramat 
79943c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
79953c34adc5Sramat 
799667e56d35Sramat 	/*
799767e56d35Sramat 	 * Since a new phci has been added, reset
799867e56d35Sramat 	 * vhc_path_discovery_cutoff_time to allow for discovery of paths
799967e56d35Sramat 	 * during next vhcache_discover_paths().
800067e56d35Sramat 	 */
800167e56d35Sramat 	mutex_enter(&vhc->vhc_lock);
800267e56d35Sramat 	vhc->vhc_path_discovery_cutoff_time = 0;
800367e56d35Sramat 	mutex_exit(&vhc->vhc_lock);
800467e56d35Sramat 
80053c34adc5Sramat 	kmem_free(pathname, MAXPATHLEN);
80063c34adc5Sramat 	if (cache_updated)
80073c34adc5Sramat 		vhcache_dirty(vhc);
80083c34adc5Sramat }
80093c34adc5Sramat 
80103c34adc5Sramat /*
80113c34adc5Sramat  * Remove the reference to the specified phci from the vhci cache.
80123c34adc5Sramat  */
80133c34adc5Sramat static void
80143c34adc5Sramat vhcache_phci_remove(mdi_vhci_config_t *vhc, mdi_phci_t *ph)
80153c34adc5Sramat {
80163c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
80173c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
80183c34adc5Sramat 
80193c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
80203c34adc5Sramat 	if ((cphci = lookup_vhcache_phci_by_addr(vhcache, ph)) != NULL) {
80213c34adc5Sramat 		/* do not remove the actual mdi_vhcache_phci structure */
80223c34adc5Sramat 		cphci->cphci_phci = NULL;
80233c34adc5Sramat 	}
80243c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
80253c34adc5Sramat }
80263c34adc5Sramat 
80273c34adc5Sramat static void
80283c34adc5Sramat init_vhcache_lookup_token(mdi_vhcache_lookup_token_t *dst,
80293c34adc5Sramat     mdi_vhcache_lookup_token_t *src)
80303c34adc5Sramat {
80313c34adc5Sramat 	if (src == NULL) {
80323c34adc5Sramat 		dst->lt_cct = NULL;
80333c34adc5Sramat 		dst->lt_cct_lookup_time = 0;
80343c34adc5Sramat 	} else {
80353c34adc5Sramat 		dst->lt_cct = src->lt_cct;
80363c34adc5Sramat 		dst->lt_cct_lookup_time = src->lt_cct_lookup_time;
80373c34adc5Sramat 	}
80383c34adc5Sramat }
80393c34adc5Sramat 
80403c34adc5Sramat /*
80413c34adc5Sramat  * Look up vhcache client for the specified client.
80423c34adc5Sramat  */
80433c34adc5Sramat static mdi_vhcache_client_t *
80443c34adc5Sramat lookup_vhcache_client(mdi_vhci_cache_t *vhcache, char *ct_name, char *ct_addr,
80453c34adc5Sramat     mdi_vhcache_lookup_token_t *token)
80463c34adc5Sramat {
80473c34adc5Sramat 	mod_hash_val_t hv;
80483c34adc5Sramat 	char *name_addr;
80493c34adc5Sramat 	int len;
80503c34adc5Sramat 
80513c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
80523c34adc5Sramat 
80533c34adc5Sramat 	/*
80543c34adc5Sramat 	 * If no vhcache clean occurred since the last lookup, we can
80553c34adc5Sramat 	 * simply return the cct from the last lookup operation.
80563c34adc5Sramat 	 * It works because ccts are never freed except during the vhcache
80573c34adc5Sramat 	 * cleanup operation.
80583c34adc5Sramat 	 */
80593c34adc5Sramat 	if (token != NULL &&
80603c34adc5Sramat 	    vhcache->vhcache_clean_time < token->lt_cct_lookup_time)
80613c34adc5Sramat 		return (token->lt_cct);
80623c34adc5Sramat 
80633c34adc5Sramat 	name_addr = vhcache_mknameaddr(ct_name, ct_addr, &len);
80643c34adc5Sramat 	if (mod_hash_find(vhcache->vhcache_client_hash,
80653c34adc5Sramat 	    (mod_hash_key_t)name_addr, &hv) == 0) {
80663c34adc5Sramat 		if (token) {
80673c34adc5Sramat 			token->lt_cct = (mdi_vhcache_client_t *)hv;
8068d3d50737SRafael Vanoni 			token->lt_cct_lookup_time = ddi_get_lbolt64();
80693c34adc5Sramat 		}
80703c34adc5Sramat 	} else {
80713c34adc5Sramat 		if (token) {
80723c34adc5Sramat 			token->lt_cct = NULL;
80733c34adc5Sramat 			token->lt_cct_lookup_time = 0;
80743c34adc5Sramat 		}
80753c34adc5Sramat 		hv = NULL;
80763c34adc5Sramat 	}
80773c34adc5Sramat 	kmem_free(name_addr, len);
80783c34adc5Sramat 	return ((mdi_vhcache_client_t *)hv);
80793c34adc5Sramat }
80803c34adc5Sramat 
80813c34adc5Sramat /*
80823c34adc5Sramat  * Add the specified path to the vhci cache if not already present.
80833c34adc5Sramat  * Also add the vhcache client for the client corresponding to this path
80843c34adc5Sramat  * if it doesn't already exist.
80853c34adc5Sramat  */
80863c34adc5Sramat static void
80873c34adc5Sramat vhcache_pi_add(mdi_vhci_config_t *vhc, struct mdi_pathinfo *pip)
80883c34adc5Sramat {
80893c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
80903c34adc5Sramat 	mdi_vhcache_client_t *cct;
80913c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
80923c34adc5Sramat 	mdi_phci_t *ph = pip->pi_phci;
80933c34adc5Sramat 	mdi_client_t *ct = pip->pi_client;
80943c34adc5Sramat 	int cache_updated = 0;
80953c34adc5Sramat 
80963c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
80973c34adc5Sramat 
80983c34adc5Sramat 	/* if vhcache client for this pip doesn't already exist, add it */
80993c34adc5Sramat 	if ((cct = lookup_vhcache_client(vhcache, ct->ct_drvname, ct->ct_guid,
81003c34adc5Sramat 	    NULL)) == NULL) {
81013c34adc5Sramat 		cct = kmem_zalloc(sizeof (*cct), KM_SLEEP);
81023c34adc5Sramat 		cct->cct_name_addr = vhcache_mknameaddr(ct->ct_drvname,
81033c34adc5Sramat 		    ct->ct_guid, NULL);
81043c34adc5Sramat 		enqueue_vhcache_client(vhcache, cct);
81053c34adc5Sramat 		(void) mod_hash_insert(vhcache->vhcache_client_hash,
81063c34adc5Sramat 		    (mod_hash_key_t)cct->cct_name_addr, (mod_hash_val_t)cct);
81073c34adc5Sramat 		cache_updated = 1;
81083c34adc5Sramat 	}
81093c34adc5Sramat 
81103c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
81113c34adc5Sramat 		if (cpi->cpi_cphci->cphci_phci == ph &&
81123c34adc5Sramat 		    strcmp(cpi->cpi_addr, pip->pi_addr) == 0) {
81133c34adc5Sramat 			cpi->cpi_pip = pip;
81143c34adc5Sramat 			if (cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST) {
81153c34adc5Sramat 				cpi->cpi_flags &=
81163c34adc5Sramat 				    ~MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
81173c34adc5Sramat 				sort_vhcache_paths(cct);
81183c34adc5Sramat 				cache_updated = 1;
81193c34adc5Sramat 			}
81203c34adc5Sramat 			break;
81213c34adc5Sramat 		}
81223c34adc5Sramat 	}
81233c34adc5Sramat 
81243c34adc5Sramat 	if (cpi == NULL) {
81253c34adc5Sramat 		cpi = kmem_zalloc(sizeof (*cpi), KM_SLEEP);
81263c34adc5Sramat 		cpi->cpi_addr = i_ddi_strdup(pip->pi_addr, KM_SLEEP);
81273c34adc5Sramat 		cpi->cpi_cphci = lookup_vhcache_phci_by_addr(vhcache, ph);
81283c34adc5Sramat 		ASSERT(cpi->cpi_cphci != NULL);
81293c34adc5Sramat 		cpi->cpi_pip = pip;
81303c34adc5Sramat 		enqueue_vhcache_pathinfo(cct, cpi);
81313c34adc5Sramat 		cache_updated = 1;
81323c34adc5Sramat 	}
81333c34adc5Sramat 
81343c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
81353c34adc5Sramat 
81363c34adc5Sramat 	if (cache_updated)
81373c34adc5Sramat 		vhcache_dirty(vhc);
81383c34adc5Sramat }
81393c34adc5Sramat 
81403c34adc5Sramat /*
81413c34adc5Sramat  * Remove the reference to the specified path from the vhci cache.
81423c34adc5Sramat  */
81433c34adc5Sramat static void
81443c34adc5Sramat vhcache_pi_remove(mdi_vhci_config_t *vhc, struct mdi_pathinfo *pip)
81453c34adc5Sramat {
81463c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
81473c34adc5Sramat 	mdi_client_t *ct = pip->pi_client;
81483c34adc5Sramat 	mdi_vhcache_client_t *cct;
81493c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
81503c34adc5Sramat 
81513c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
81523c34adc5Sramat 	if ((cct = lookup_vhcache_client(vhcache, ct->ct_drvname, ct->ct_guid,
81533c34adc5Sramat 	    NULL)) != NULL) {
81543c34adc5Sramat 		for (cpi = cct->cct_cpi_head; cpi != NULL;
81553c34adc5Sramat 		    cpi = cpi->cpi_next) {
81563c34adc5Sramat 			if (cpi->cpi_pip == pip) {
81573c34adc5Sramat 				cpi->cpi_pip = NULL;
81583c34adc5Sramat 				break;
81593c34adc5Sramat 			}
81603c34adc5Sramat 		}
81613c34adc5Sramat 	}
81623c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
81633c34adc5Sramat }
81643c34adc5Sramat 
81653c34adc5Sramat /*
81663c34adc5Sramat  * Flush the vhci cache to disk.
81673c34adc5Sramat  * Returns MDI_SUCCESS on success, MDI_FAILURE on failure.
81683c34adc5Sramat  */
81693c34adc5Sramat static int
81703c34adc5Sramat flush_vhcache(mdi_vhci_config_t *vhc, int force_flag)
81713c34adc5Sramat {
81723c34adc5Sramat 	nvlist_t *nvl;
81733c34adc5Sramat 	int err;
81743c34adc5Sramat 	int rv;
81753c34adc5Sramat 
81763c34adc5Sramat 	/*
81773c34adc5Sramat 	 * It is possible that the system may shutdown before
81783c34adc5Sramat 	 * i_ddi_io_initialized (during stmsboot for example). To allow for
81793c34adc5Sramat 	 * flushing the cache in this case do not check for
81803c34adc5Sramat 	 * i_ddi_io_initialized when force flag is set.
81813c34adc5Sramat 	 */
81823c34adc5Sramat 	if (force_flag == 0 && !i_ddi_io_initialized())
81833c34adc5Sramat 		return (MDI_FAILURE);
81843c34adc5Sramat 
81853c34adc5Sramat 	if ((nvl = vhcache_to_mainnvl(&vhc->vhc_vhcache)) != NULL) {
81863c34adc5Sramat 		err = fwrite_nvlist(vhc->vhc_vhcache_filename, nvl);
81873c34adc5Sramat 		nvlist_free(nvl);
81883c34adc5Sramat 	} else
81893c34adc5Sramat 		err = EFAULT;
81903c34adc5Sramat 
81913c34adc5Sramat 	rv = MDI_SUCCESS;
81923c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
81933c34adc5Sramat 	if (err != 0) {
81943c34adc5Sramat 		if (err == EROFS) {
81953c34adc5Sramat 			vhc->vhc_flags |= MDI_VHC_READONLY_FS;
81963c34adc5Sramat 			vhc->vhc_flags &= ~(MDI_VHC_VHCACHE_FLUSH_ERROR |
81973c34adc5Sramat 			    MDI_VHC_VHCACHE_DIRTY);
81983c34adc5Sramat 		} else {
81993c34adc5Sramat 			if (!(vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_ERROR)) {
82003c34adc5Sramat 				cmn_err(CE_CONT, "%s: update failed\n",
82013c34adc5Sramat 				    vhc->vhc_vhcache_filename);
82023c34adc5Sramat 				vhc->vhc_flags |= MDI_VHC_VHCACHE_FLUSH_ERROR;
82033c34adc5Sramat 			}
82043c34adc5Sramat 			rv = MDI_FAILURE;
82053c34adc5Sramat 		}
82063c34adc5Sramat 	} else if (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_ERROR) {
82073c34adc5Sramat 		cmn_err(CE_CONT,
82083c34adc5Sramat 		    "%s: update now ok\n", vhc->vhc_vhcache_filename);
82093c34adc5Sramat 		vhc->vhc_flags &= ~MDI_VHC_VHCACHE_FLUSH_ERROR;
82103c34adc5Sramat 	}
82113c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
82123c34adc5Sramat 
82133c34adc5Sramat 	return (rv);
82143c34adc5Sramat }
82153c34adc5Sramat 
82163c34adc5Sramat /*
82173c34adc5Sramat  * Call flush_vhcache() to flush the vhci cache at the scheduled time.
82183c34adc5Sramat  * Exits itself if left idle for the idle timeout period.
82193c34adc5Sramat  */
82203c34adc5Sramat static void
82213c34adc5Sramat vhcache_flush_thread(void *arg)
82223c34adc5Sramat {
82233c34adc5Sramat 	mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
82243c34adc5Sramat 	clock_t idle_time, quit_at_ticks;
82253c34adc5Sramat 	callb_cpr_t cprinfo;
82263c34adc5Sramat 
82273c34adc5Sramat 	/* number of seconds to sleep idle before exiting */
82283c34adc5Sramat 	idle_time = mdi_vhcache_flush_daemon_idle_time * TICKS_PER_SECOND;
82293c34adc5Sramat 
82303c34adc5Sramat 	CALLB_CPR_INIT(&cprinfo, &vhc->vhc_lock, callb_generic_cpr,
82313c34adc5Sramat 	    "mdi_vhcache_flush");
82323c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
82333c34adc5Sramat 	for (; ; ) {
82343c34adc5Sramat 		while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
82353c34adc5Sramat 		    (vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY)) {
82363c34adc5Sramat 			if (ddi_get_lbolt() < vhc->vhc_flush_at_ticks) {
82373c34adc5Sramat 				CALLB_CPR_SAFE_BEGIN(&cprinfo);
82383c34adc5Sramat 				(void) cv_timedwait(&vhc->vhc_cv,
82393c34adc5Sramat 				    &vhc->vhc_lock, vhc->vhc_flush_at_ticks);
82403c34adc5Sramat 				CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
82413c34adc5Sramat 			} else {
82423c34adc5Sramat 				vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
82433c34adc5Sramat 				mutex_exit(&vhc->vhc_lock);
82443c34adc5Sramat 
82453c34adc5Sramat 				if (flush_vhcache(vhc, 0) != MDI_SUCCESS)
82463c34adc5Sramat 					vhcache_dirty(vhc);
82473c34adc5Sramat 
82483c34adc5Sramat 				mutex_enter(&vhc->vhc_lock);
82493c34adc5Sramat 			}
82503c34adc5Sramat 		}
82513c34adc5Sramat 
82523c34adc5Sramat 		quit_at_ticks = ddi_get_lbolt() + idle_time;
82533c34adc5Sramat 
82543c34adc5Sramat 		while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
82553c34adc5Sramat 		    !(vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) &&
82563c34adc5Sramat 		    ddi_get_lbolt() < quit_at_ticks) {
82573c34adc5Sramat 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
82583c34adc5Sramat 			(void) cv_timedwait(&vhc->vhc_cv, &vhc->vhc_lock,
82593c34adc5Sramat 			    quit_at_ticks);
82603c34adc5Sramat 			CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
82613c34adc5Sramat 		}
82623c34adc5Sramat 
82633c34adc5Sramat 		if ((vhc->vhc_flags & MDI_VHC_EXIT) ||
82643c34adc5Sramat 		    !(vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY))
82653c34adc5Sramat 			goto out;
82663c34adc5Sramat 	}
82673c34adc5Sramat 
82683c34adc5Sramat out:
82693c34adc5Sramat 	vhc->vhc_flags &= ~MDI_VHC_VHCACHE_FLUSH_THREAD;
82703c34adc5Sramat 	/* CALLB_CPR_EXIT releases the vhc->vhc_lock */
82713c34adc5Sramat 	CALLB_CPR_EXIT(&cprinfo);
82723c34adc5Sramat }
82733c34adc5Sramat 
82743c34adc5Sramat /*
82753c34adc5Sramat  * Make vhci cache dirty and schedule flushing by vhcache flush thread.
82763c34adc5Sramat  */
82773c34adc5Sramat static void
82783c34adc5Sramat vhcache_dirty(mdi_vhci_config_t *vhc)
82793c34adc5Sramat {
82803c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
82813c34adc5Sramat 	int create_thread;
82823c34adc5Sramat 
82833c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
82843c34adc5Sramat 	/* do not flush cache until the cache is fully built */
82853c34adc5Sramat 	if (!(vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE)) {
82863c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
82873c34adc5Sramat 		return;
82883c34adc5Sramat 	}
82893c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
82903c34adc5Sramat 
82913c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
82923c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_READONLY_FS) {
82933c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
82943c34adc5Sramat 		return;
82953c34adc5Sramat 	}
82963c34adc5Sramat 
82973c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_VHCACHE_DIRTY;
82983c34adc5Sramat 	vhc->vhc_flush_at_ticks = ddi_get_lbolt() +
82993c34adc5Sramat 	    mdi_vhcache_flush_delay * TICKS_PER_SECOND;
83003c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) {
83013c34adc5Sramat 		cv_broadcast(&vhc->vhc_cv);
83023c34adc5Sramat 		create_thread = 0;
83033c34adc5Sramat 	} else {
83043c34adc5Sramat 		vhc->vhc_flags |= MDI_VHC_VHCACHE_FLUSH_THREAD;
83053c34adc5Sramat 		create_thread = 1;
83063c34adc5Sramat 	}
83073c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
83083c34adc5Sramat 
83093c34adc5Sramat 	if (create_thread)
83103c34adc5Sramat 		(void) thread_create(NULL, 0, vhcache_flush_thread, vhc,
83113c34adc5Sramat 		    0, &p0, TS_RUN, minclsyspri);
83123c34adc5Sramat }
83133c34adc5Sramat 
83143c34adc5Sramat /*
83153c34adc5Sramat  * phci bus config structure - one for for each phci bus config operation that
83163c34adc5Sramat  * we initiate on behalf of a vhci.
83173c34adc5Sramat  */
83183c34adc5Sramat typedef struct mdi_phci_bus_config_s {
83193c34adc5Sramat 	char *phbc_phci_path;
83203c34adc5Sramat 	struct mdi_vhci_bus_config_s *phbc_vhbusconfig;	/* vhci bus config */
83213c34adc5Sramat 	struct mdi_phci_bus_config_s *phbc_next;
83223c34adc5Sramat } mdi_phci_bus_config_t;
83233c34adc5Sramat 
83243c34adc5Sramat /* vhci bus config structure - one for each vhci bus config operation */
83253c34adc5Sramat typedef struct mdi_vhci_bus_config_s {
83263c34adc5Sramat 	ddi_bus_config_op_t vhbc_op;	/* bus config op */
83273c34adc5Sramat 	major_t vhbc_op_major;		/* bus config op major */
83283c34adc5Sramat 	uint_t vhbc_op_flags;		/* bus config op flags */
83293c34adc5Sramat 	kmutex_t vhbc_lock;
83303c34adc5Sramat 	kcondvar_t vhbc_cv;
83313c34adc5Sramat 	int vhbc_thr_count;
83323c34adc5Sramat } mdi_vhci_bus_config_t;
83333c34adc5Sramat 
83343c34adc5Sramat /*
83353c34adc5Sramat  * bus config the specified phci
83363c34adc5Sramat  */
83373c34adc5Sramat static void
83383c34adc5Sramat bus_config_phci(void *arg)
83393c34adc5Sramat {
83403c34adc5Sramat 	mdi_phci_bus_config_t *phbc = (mdi_phci_bus_config_t *)arg;
83413c34adc5Sramat 	mdi_vhci_bus_config_t *vhbc = phbc->phbc_vhbusconfig;
83423c34adc5Sramat 	dev_info_t *ph_dip;
83433c34adc5Sramat 
83443c34adc5Sramat 	/*
83453c34adc5Sramat 	 * first configure all path components upto phci and then configure
83463c34adc5Sramat 	 * the phci children.
83473c34adc5Sramat 	 */
83483c34adc5Sramat 	if ((ph_dip = e_ddi_hold_devi_by_path(phbc->phbc_phci_path, 0))
83493c34adc5Sramat 	    != NULL) {
83503c34adc5Sramat 		if (vhbc->vhbc_op == BUS_CONFIG_DRIVER ||
83513c34adc5Sramat 		    vhbc->vhbc_op == BUS_UNCONFIG_DRIVER) {
83523c34adc5Sramat 			(void) ndi_devi_config_driver(ph_dip,
83533c34adc5Sramat 			    vhbc->vhbc_op_flags,
83543c34adc5Sramat 			    vhbc->vhbc_op_major);
83553c34adc5Sramat 		} else
83563c34adc5Sramat 			(void) ndi_devi_config(ph_dip,
83573c34adc5Sramat 			    vhbc->vhbc_op_flags);
83583c34adc5Sramat 
83593c34adc5Sramat 		/* release the hold that e_ddi_hold_devi_by_path() placed */
83603c34adc5Sramat 		ndi_rele_devi(ph_dip);
83613c34adc5Sramat 	}
83623c34adc5Sramat 
83633c34adc5Sramat 	kmem_free(phbc->phbc_phci_path, strlen(phbc->phbc_phci_path) + 1);
83643c34adc5Sramat 	kmem_free(phbc, sizeof (*phbc));
83653c34adc5Sramat 
83663c34adc5Sramat 	mutex_enter(&vhbc->vhbc_lock);
83673c34adc5Sramat 	vhbc->vhbc_thr_count--;
83683c34adc5Sramat 	if (vhbc->vhbc_thr_count == 0)
83693c34adc5Sramat 		cv_broadcast(&vhbc->vhbc_cv);
83703c34adc5Sramat 	mutex_exit(&vhbc->vhbc_lock);
83713c34adc5Sramat }
83723c34adc5Sramat 
83733c34adc5Sramat /*
83743c34adc5Sramat  * Bus config all phcis associated with the vhci in parallel.
83753c34adc5Sramat  * op must be BUS_CONFIG_DRIVER or BUS_CONFIG_ALL.
83763c34adc5Sramat  */
83773c34adc5Sramat static void
83783c34adc5Sramat bus_config_all_phcis(mdi_vhci_cache_t *vhcache, uint_t flags,
83793c34adc5Sramat     ddi_bus_config_op_t op, major_t maj)
83803c34adc5Sramat {
83813c34adc5Sramat 	mdi_phci_bus_config_t *phbc_head = NULL, *phbc, *phbc_next;
83823c34adc5Sramat 	mdi_vhci_bus_config_t *vhbc;
83833c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
83843c34adc5Sramat 
83853c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
83863c34adc5Sramat 	if (vhcache->vhcache_phci_head == NULL) {
83873c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
83883c34adc5Sramat 		return;
83893c34adc5Sramat 	}
83903c34adc5Sramat 
83913c34adc5Sramat 	vhbc = kmem_zalloc(sizeof (*vhbc), KM_SLEEP);
83923c34adc5Sramat 
83933c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
83943c34adc5Sramat 	    cphci = cphci->cphci_next) {
8395273f4511Sgp87344 		/* skip phcis that haven't attached before root is available */
8396273f4511Sgp87344 		if (!modrootloaded && (cphci->cphci_phci == NULL))
8397273f4511Sgp87344 			continue;
83983c34adc5Sramat 		phbc = kmem_zalloc(sizeof (*phbc), KM_SLEEP);
83993c34adc5Sramat 		phbc->phbc_phci_path = i_ddi_strdup(cphci->cphci_path,
84003c34adc5Sramat 		    KM_SLEEP);
84013c34adc5Sramat 		phbc->phbc_vhbusconfig = vhbc;
84023c34adc5Sramat 		phbc->phbc_next = phbc_head;
84033c34adc5Sramat 		phbc_head = phbc;
84043c34adc5Sramat 		vhbc->vhbc_thr_count++;
84053c34adc5Sramat 	}
84063c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
84073c34adc5Sramat 
84083c34adc5Sramat 	vhbc->vhbc_op = op;
84093c34adc5Sramat 	vhbc->vhbc_op_major = maj;
84103c34adc5Sramat 	vhbc->vhbc_op_flags = NDI_NO_EVENT |
84113c34adc5Sramat 	    (flags & (NDI_CONFIG_REPROBE | NDI_DRV_CONF_REPROBE));
84123c34adc5Sramat 	mutex_init(&vhbc->vhbc_lock, NULL, MUTEX_DEFAULT, NULL);
84133c34adc5Sramat 	cv_init(&vhbc->vhbc_cv, NULL, CV_DRIVER, NULL);
84143c34adc5Sramat 
84153c34adc5Sramat 	/* now create threads to initiate bus config on all phcis in parallel */
84163c34adc5Sramat 	for (phbc = phbc_head; phbc != NULL; phbc = phbc_next) {
84173c34adc5Sramat 		phbc_next = phbc->phbc_next;
84183c34adc5Sramat 		if (mdi_mtc_off)
84193c34adc5Sramat 			bus_config_phci((void *)phbc);
84203c34adc5Sramat 		else
84213c34adc5Sramat 			(void) thread_create(NULL, 0, bus_config_phci, phbc,
84223c34adc5Sramat 			    0, &p0, TS_RUN, minclsyspri);
84233c34adc5Sramat 	}
84243c34adc5Sramat 
84253c34adc5Sramat 	mutex_enter(&vhbc->vhbc_lock);
84263c34adc5Sramat 	/* wait until all threads exit */
84273c34adc5Sramat 	while (vhbc->vhbc_thr_count > 0)
84283c34adc5Sramat 		cv_wait(&vhbc->vhbc_cv, &vhbc->vhbc_lock);
84293c34adc5Sramat 	mutex_exit(&vhbc->vhbc_lock);
84303c34adc5Sramat 
84313c34adc5Sramat 	mutex_destroy(&vhbc->vhbc_lock);
84323c34adc5Sramat 	cv_destroy(&vhbc->vhbc_cv);
84333c34adc5Sramat 	kmem_free(vhbc, sizeof (*vhbc));
84343c34adc5Sramat }
84353c34adc5Sramat 
84363c34adc5Sramat /*
843767e56d35Sramat  * Single threaded version of bus_config_all_phcis()
843867e56d35Sramat  */
843967e56d35Sramat static void
844067e56d35Sramat st_bus_config_all_phcis(mdi_vhci_config_t *vhc, uint_t flags,
844167e56d35Sramat     ddi_bus_config_op_t op, major_t maj)
844267e56d35Sramat {
844367e56d35Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
844467e56d35Sramat 
844567e56d35Sramat 	single_threaded_vhconfig_enter(vhc);
844667e56d35Sramat 	bus_config_all_phcis(vhcache, flags, op, maj);
844767e56d35Sramat 	single_threaded_vhconfig_exit(vhc);
844867e56d35Sramat }
844967e56d35Sramat 
845067e56d35Sramat /*
84513c34adc5Sramat  * Perform BUS_CONFIG_ONE on the specified child of the phci.
84523c34adc5Sramat  * The path includes the child component in addition to the phci path.
84533c34adc5Sramat  */
84543c34adc5Sramat static int
84553c34adc5Sramat bus_config_one_phci_child(char *path)
84563c34adc5Sramat {
84573c34adc5Sramat 	dev_info_t *ph_dip, *child;
84583c34adc5Sramat 	char *devnm;
84593c34adc5Sramat 	int rv = MDI_FAILURE;
84603c34adc5Sramat 
84613c34adc5Sramat 	/* extract the child component of the phci */
84623c34adc5Sramat 	devnm = strrchr(path, '/');
84633c34adc5Sramat 	*devnm++ = '\0';
84643c34adc5Sramat 
84653c34adc5Sramat 	/*
84663c34adc5Sramat 	 * first configure all path components upto phci and then
84673c34adc5Sramat 	 * configure the phci child.
84683c34adc5Sramat 	 */
84693c34adc5Sramat 	if ((ph_dip = e_ddi_hold_devi_by_path(path, 0)) != NULL) {
84703c34adc5Sramat 		if (ndi_devi_config_one(ph_dip, devnm, &child, NDI_NO_EVENT) ==
84713c34adc5Sramat 		    NDI_SUCCESS) {
84723c34adc5Sramat 			/*
84733c34adc5Sramat 			 * release the hold that ndi_devi_config_one() placed
84743c34adc5Sramat 			 */
84753c34adc5Sramat 			ndi_rele_devi(child);
84763c34adc5Sramat 			rv = MDI_SUCCESS;
84773c34adc5Sramat 		}
84783c34adc5Sramat 
84793c34adc5Sramat 		/* release the hold that e_ddi_hold_devi_by_path() placed */
84803c34adc5Sramat 		ndi_rele_devi(ph_dip);
84813c34adc5Sramat 	}
84823c34adc5Sramat 
84833c34adc5Sramat 	devnm--;
84843c34adc5Sramat 	*devnm = '/';
84853c34adc5Sramat 	return (rv);
84863c34adc5Sramat }
84873c34adc5Sramat 
84883c34adc5Sramat /*
84893c34adc5Sramat  * Build a list of phci client paths for the specified vhci client.
84903c34adc5Sramat  * The list includes only those phci client paths which aren't configured yet.
84913c34adc5Sramat  */
84923c34adc5Sramat static mdi_phys_path_t *
84933c34adc5Sramat build_phclient_path_list(mdi_vhcache_client_t *cct, char *ct_name)
84943c34adc5Sramat {
84953c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
84963c34adc5Sramat 	mdi_phys_path_t *pp_head = NULL, *pp_tail = NULL, *pp;
84973c34adc5Sramat 	int config_path, len;
84983c34adc5Sramat 
84993c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
85003c34adc5Sramat 		/*
85013c34adc5Sramat 		 * include only those paths that aren't configured.
85023c34adc5Sramat 		 */
85033c34adc5Sramat 		config_path = 0;
85043c34adc5Sramat 		if (cpi->cpi_pip == NULL)
85053c34adc5Sramat 			config_path = 1;
85063c34adc5Sramat 		else {
85073c34adc5Sramat 			MDI_PI_LOCK(cpi->cpi_pip);
85083c34adc5Sramat 			if (MDI_PI_IS_INIT(cpi->cpi_pip))
85093c34adc5Sramat 				config_path = 1;
85103c34adc5Sramat 			MDI_PI_UNLOCK(cpi->cpi_pip);
85113c34adc5Sramat 		}
85123c34adc5Sramat 
85133c34adc5Sramat 		if (config_path) {
85143c34adc5Sramat 			pp = kmem_alloc(sizeof (*pp), KM_SLEEP);
85153c34adc5Sramat 			len = strlen(cpi->cpi_cphci->cphci_path) +
85163c34adc5Sramat 			    strlen(ct_name) + strlen(cpi->cpi_addr) + 3;
85173c34adc5Sramat 			pp->phys_path = kmem_alloc(len, KM_SLEEP);
85183c34adc5Sramat 			(void) snprintf(pp->phys_path, len, "%s/%s@%s",
85193c34adc5Sramat 			    cpi->cpi_cphci->cphci_path, ct_name,
85203c34adc5Sramat 			    cpi->cpi_addr);
85213c34adc5Sramat 			pp->phys_path_next = NULL;
85223c34adc5Sramat 
85233c34adc5Sramat 			if (pp_head == NULL)
85243c34adc5Sramat 				pp_head = pp;
85253c34adc5Sramat 			else
85263c34adc5Sramat 				pp_tail->phys_path_next = pp;
85273c34adc5Sramat 			pp_tail = pp;
85283c34adc5Sramat 		}
85293c34adc5Sramat 	}
85303c34adc5Sramat 
85313c34adc5Sramat 	return (pp_head);
85323c34adc5Sramat }
85333c34adc5Sramat 
85343c34adc5Sramat /*
85353c34adc5Sramat  * Free the memory allocated for phci client path list.
85363c34adc5Sramat  */
85373c34adc5Sramat static void
85383c34adc5Sramat free_phclient_path_list(mdi_phys_path_t *pp_head)
85393c34adc5Sramat {
85403c34adc5Sramat 	mdi_phys_path_t *pp, *pp_next;
85413c34adc5Sramat 
85423c34adc5Sramat 	for (pp = pp_head; pp != NULL; pp = pp_next) {
85433c34adc5Sramat 		pp_next = pp->phys_path_next;
85443c34adc5Sramat 		kmem_free(pp->phys_path, strlen(pp->phys_path) + 1);
85453c34adc5Sramat 		kmem_free(pp, sizeof (*pp));
85463c34adc5Sramat 	}
85473c34adc5Sramat }
85483c34adc5Sramat 
85493c34adc5Sramat /*
85503c34adc5Sramat  * Allocated async client structure and initialize with the specified values.
85513c34adc5Sramat  */
85523c34adc5Sramat static mdi_async_client_config_t *
85533c34adc5Sramat alloc_async_client_config(char *ct_name, char *ct_addr,
85543c34adc5Sramat     mdi_phys_path_t *pp_head, mdi_vhcache_lookup_token_t *tok)
85553c34adc5Sramat {
85563c34adc5Sramat 	mdi_async_client_config_t *acc;
85573c34adc5Sramat 
85583c34adc5Sramat 	acc = kmem_alloc(sizeof (*acc), KM_SLEEP);
85593c34adc5Sramat 	acc->acc_ct_name = i_ddi_strdup(ct_name, KM_SLEEP);
85603c34adc5Sramat 	acc->acc_ct_addr = i_ddi_strdup(ct_addr, KM_SLEEP);
85613c34adc5Sramat 	acc->acc_phclient_path_list_head = pp_head;
85623c34adc5Sramat 	init_vhcache_lookup_token(&acc->acc_token, tok);
85633c34adc5Sramat 	acc->acc_next = NULL;
85643c34adc5Sramat 	return (acc);
85653c34adc5Sramat }
85663c34adc5Sramat 
85673c34adc5Sramat /*
85683c34adc5Sramat  * Free the memory allocated for the async client structure and their members.
85693c34adc5Sramat  */
85703c34adc5Sramat static void
85713c34adc5Sramat free_async_client_config(mdi_async_client_config_t *acc)
85723c34adc5Sramat {
85733c34adc5Sramat 	if (acc->acc_phclient_path_list_head)
85743c34adc5Sramat 		free_phclient_path_list(acc->acc_phclient_path_list_head);
85753c34adc5Sramat 	kmem_free(acc->acc_ct_name, strlen(acc->acc_ct_name) + 1);
85763c34adc5Sramat 	kmem_free(acc->acc_ct_addr, strlen(acc->acc_ct_addr) + 1);
85773c34adc5Sramat 	kmem_free(acc, sizeof (*acc));
85783c34adc5Sramat }
85793c34adc5Sramat 
85803c34adc5Sramat /*
85813c34adc5Sramat  * Sort vhcache pathinfos (cpis) of the specified client.
85823c34adc5Sramat  * All cpis which do not have MDI_CPI_HINT_PATH_DOES_NOT_EXIST
85833c34adc5Sramat  * flag set come at the beginning of the list. All cpis which have this
85843c34adc5Sramat  * flag set come at the end of the list.
85853c34adc5Sramat  */
85863c34adc5Sramat static void
85873c34adc5Sramat sort_vhcache_paths(mdi_vhcache_client_t *cct)
85883c34adc5Sramat {
85893c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi, *cpi_next, *cpi_head;
85903c34adc5Sramat 
85913c34adc5Sramat 	cpi_head = cct->cct_cpi_head;
85923c34adc5Sramat 	cct->cct_cpi_head = cct->cct_cpi_tail = NULL;
85933c34adc5Sramat 	for (cpi = cpi_head; cpi != NULL; cpi = cpi_next) {
85943c34adc5Sramat 		cpi_next = cpi->cpi_next;
85953c34adc5Sramat 		enqueue_vhcache_pathinfo(cct, cpi);
85963c34adc5Sramat 	}
85973c34adc5Sramat }
85983c34adc5Sramat 
85993c34adc5Sramat /*
86003c34adc5Sramat  * Verify whether MDI_CPI_HINT_PATH_DOES_NOT_EXIST flag setting is correct for
86013c34adc5Sramat  * every vhcache pathinfo of the specified client. If not adjust the flag
86023c34adc5Sramat  * setting appropriately.
86033c34adc5Sramat  *
86043c34adc5Sramat  * Note that MDI_CPI_HINT_PATH_DOES_NOT_EXIST flag is persisted in the
86053c34adc5Sramat  * on-disk vhci cache. So every time this flag is updated the cache must be
86063c34adc5Sramat  * flushed.
86073c34adc5Sramat  */
86083c34adc5Sramat static void
86093c34adc5Sramat adjust_sort_vhcache_paths(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr,
86103c34adc5Sramat     mdi_vhcache_lookup_token_t *tok)
86113c34adc5Sramat {
86123c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
86133c34adc5Sramat 	mdi_vhcache_client_t *cct;
86143c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
86153c34adc5Sramat 
86163c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
86173c34adc5Sramat 	if ((cct = lookup_vhcache_client(vhcache, ct_name, ct_addr, tok))
86183c34adc5Sramat 	    == NULL) {
86193c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
86203c34adc5Sramat 		return;
86213c34adc5Sramat 	}
86223c34adc5Sramat 
86233c34adc5Sramat 	/*
86243c34adc5Sramat 	 * to avoid unnecessary on-disk cache updates, first check if an
86253c34adc5Sramat 	 * update is really needed. If no update is needed simply return.
86263c34adc5Sramat 	 */
86273c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
86283c34adc5Sramat 		if ((cpi->cpi_pip != NULL &&
86293c34adc5Sramat 		    (cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST)) ||
86303c34adc5Sramat 		    (cpi->cpi_pip == NULL &&
86313c34adc5Sramat 		    !(cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST))) {
86323c34adc5Sramat 			break;
86333c34adc5Sramat 		}
86343c34adc5Sramat 	}
86353c34adc5Sramat 	if (cpi == NULL) {
86363c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
86373c34adc5Sramat 		return;
86383c34adc5Sramat 	}
86393c34adc5Sramat 
86403c34adc5Sramat 	if (rw_tryupgrade(&vhcache->vhcache_lock) == 0) {
86413c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
86423c34adc5Sramat 		rw_enter(&vhcache->vhcache_lock, RW_WRITER);
86433c34adc5Sramat 		if ((cct = lookup_vhcache_client(vhcache, ct_name, ct_addr,
86443c34adc5Sramat 		    tok)) == NULL) {
86453c34adc5Sramat 			rw_exit(&vhcache->vhcache_lock);
86463c34adc5Sramat 			return;
86473c34adc5Sramat 		}
86483c34adc5Sramat 	}
86493c34adc5Sramat 
86503c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
86513c34adc5Sramat 		if (cpi->cpi_pip != NULL)
86523c34adc5Sramat 			cpi->cpi_flags &= ~MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
86533c34adc5Sramat 		else
86543c34adc5Sramat 			cpi->cpi_flags |= MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
86553c34adc5Sramat 	}
86563c34adc5Sramat 	sort_vhcache_paths(cct);
86573c34adc5Sramat 
86583c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
86593c34adc5Sramat 	vhcache_dirty(vhc);
86603c34adc5Sramat }
86613c34adc5Sramat 
86623c34adc5Sramat /*
86633c34adc5Sramat  * Configure all specified paths of the client.
86643c34adc5Sramat  */
86653c34adc5Sramat static void
86663c34adc5Sramat config_client_paths_sync(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr,
86673c34adc5Sramat     mdi_phys_path_t *pp_head, mdi_vhcache_lookup_token_t *tok)
86683c34adc5Sramat {
86693c34adc5Sramat 	mdi_phys_path_t *pp;
86703c34adc5Sramat 
86713c34adc5Sramat 	for (pp = pp_head; pp != NULL; pp = pp->phys_path_next)
86723c34adc5Sramat 		(void) bus_config_one_phci_child(pp->phys_path);
86733c34adc5Sramat 	adjust_sort_vhcache_paths(vhc, ct_name, ct_addr, tok);
86743c34adc5Sramat }
86753c34adc5Sramat 
86763c34adc5Sramat /*
86773c34adc5Sramat  * Dequeue elements from vhci async client config list and bus configure
86783c34adc5Sramat  * their corresponding phci clients.
86793c34adc5Sramat  */
86803c34adc5Sramat static void
86813c34adc5Sramat config_client_paths_thread(void *arg)
86823c34adc5Sramat {
86833c34adc5Sramat 	mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
86843c34adc5Sramat 	mdi_async_client_config_t *acc;
86853c34adc5Sramat 	clock_t quit_at_ticks;
86863c34adc5Sramat 	clock_t idle_time = mdi_async_config_idle_time * TICKS_PER_SECOND;
86873c34adc5Sramat 	callb_cpr_t cprinfo;
86883c34adc5Sramat 
86893c34adc5Sramat 	CALLB_CPR_INIT(&cprinfo, &vhc->vhc_lock, callb_generic_cpr,
86903c34adc5Sramat 	    "mdi_config_client_paths");
86913c34adc5Sramat 
86923c34adc5Sramat 	for (; ; ) {
86933c34adc5Sramat 		quit_at_ticks = ddi_get_lbolt() + idle_time;
86943c34adc5Sramat 
86953c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
86963c34adc5Sramat 		while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
86973c34adc5Sramat 		    vhc->vhc_acc_list_head == NULL &&
86983c34adc5Sramat 		    ddi_get_lbolt() < quit_at_ticks) {
86993c34adc5Sramat 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
87003c34adc5Sramat 			(void) cv_timedwait(&vhc->vhc_cv, &vhc->vhc_lock,
87013c34adc5Sramat 			    quit_at_ticks);
87023c34adc5Sramat 			CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
87033c34adc5Sramat 		}
87043c34adc5Sramat 
87053c34adc5Sramat 		if ((vhc->vhc_flags & MDI_VHC_EXIT) ||
87063c34adc5Sramat 		    vhc->vhc_acc_list_head == NULL)
87073c34adc5Sramat 			goto out;
87083c34adc5Sramat 
87093c34adc5Sramat 		acc = vhc->vhc_acc_list_head;
87103c34adc5Sramat 		vhc->vhc_acc_list_head = acc->acc_next;
87113c34adc5Sramat 		if (vhc->vhc_acc_list_head == NULL)
87123c34adc5Sramat 			vhc->vhc_acc_list_tail = NULL;
87133c34adc5Sramat 		vhc->vhc_acc_count--;
87143c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
87153c34adc5Sramat 
87163c34adc5Sramat 		config_client_paths_sync(vhc, acc->acc_ct_name,
87173c34adc5Sramat 		    acc->acc_ct_addr, acc->acc_phclient_path_list_head,
87183c34adc5Sramat 		    &acc->acc_token);
87193c34adc5Sramat 
87203c34adc5Sramat 		free_async_client_config(acc);
87213c34adc5Sramat 	}
87223c34adc5Sramat 
87233c34adc5Sramat out:
87243c34adc5Sramat 	vhc->vhc_acc_thrcount--;
87253c34adc5Sramat 	/* CALLB_CPR_EXIT releases the vhc->vhc_lock */
87263c34adc5Sramat 	CALLB_CPR_EXIT(&cprinfo);
87273c34adc5Sramat }
87283c34adc5Sramat 
87293c34adc5Sramat /*
87303c34adc5Sramat  * Arrange for all the phci client paths (pp_head) for the specified client
87313c34adc5Sramat  * to be bus configured asynchronously by a thread.
87323c34adc5Sramat  */
87333c34adc5Sramat static void
87343c34adc5Sramat config_client_paths_async(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr,
87353c34adc5Sramat     mdi_phys_path_t *pp_head, mdi_vhcache_lookup_token_t *tok)
87363c34adc5Sramat {
87373c34adc5Sramat 	mdi_async_client_config_t *acc, *newacc;
87383c34adc5Sramat 	int create_thread;
87393c34adc5Sramat 
87403c34adc5Sramat 	if (pp_head == NULL)
87413c34adc5Sramat 		return;
87423c34adc5Sramat 
87433c34adc5Sramat 	if (mdi_mtc_off) {
87443c34adc5Sramat 		config_client_paths_sync(vhc, ct_name, ct_addr, pp_head, tok);
87453c34adc5Sramat 		free_phclient_path_list(pp_head);
87463c34adc5Sramat 		return;
87473c34adc5Sramat 	}
87483c34adc5Sramat 
87493c34adc5Sramat 	newacc = alloc_async_client_config(ct_name, ct_addr, pp_head, tok);
87503c34adc5Sramat 	ASSERT(newacc);
87513c34adc5Sramat 
87523c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
87533c34adc5Sramat 	for (acc = vhc->vhc_acc_list_head; acc != NULL; acc = acc->acc_next) {
87543c34adc5Sramat 		if (strcmp(ct_name, acc->acc_ct_name) == 0 &&
87553c34adc5Sramat 		    strcmp(ct_addr, acc->acc_ct_addr) == 0) {
87563c34adc5Sramat 			free_async_client_config(newacc);
87573c34adc5Sramat 			mutex_exit(&vhc->vhc_lock);
87583c34adc5Sramat 			return;
87593c34adc5Sramat 		}
87603c34adc5Sramat 	}
87613c34adc5Sramat 
87623c34adc5Sramat 	if (vhc->vhc_acc_list_head == NULL)
87633c34adc5Sramat 		vhc->vhc_acc_list_head = newacc;
87643c34adc5Sramat 	else
87653c34adc5Sramat 		vhc->vhc_acc_list_tail->acc_next = newacc;
87663c34adc5Sramat 	vhc->vhc_acc_list_tail = newacc;
87673c34adc5Sramat 	vhc->vhc_acc_count++;
87683c34adc5Sramat 	if (vhc->vhc_acc_count <= vhc->vhc_acc_thrcount) {
87693c34adc5Sramat 		cv_broadcast(&vhc->vhc_cv);
87703c34adc5Sramat 		create_thread = 0;
87713c34adc5Sramat 	} else {
87723c34adc5Sramat 		vhc->vhc_acc_thrcount++;
87733c34adc5Sramat 		create_thread = 1;
87743c34adc5Sramat 	}
87753c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
87763c34adc5Sramat 
87773c34adc5Sramat 	if (create_thread)
87783c34adc5Sramat 		(void) thread_create(NULL, 0, config_client_paths_thread, vhc,
87793c34adc5Sramat 		    0, &p0, TS_RUN, minclsyspri);
87803c34adc5Sramat }
87813c34adc5Sramat 
87823c34adc5Sramat /*
87833c34adc5Sramat  * Return number of online paths for the specified client.
87843c34adc5Sramat  */
87853c34adc5Sramat static int
87863c34adc5Sramat nonline_paths(mdi_vhcache_client_t *cct)
87873c34adc5Sramat {
87883c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
87893c34adc5Sramat 	int online_count = 0;
87903c34adc5Sramat 
87913c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
87923c34adc5Sramat 		if (cpi->cpi_pip != NULL) {
87933c34adc5Sramat 			MDI_PI_LOCK(cpi->cpi_pip);
87943c34adc5Sramat 			if (cpi->cpi_pip->pi_state == MDI_PATHINFO_STATE_ONLINE)
87953c34adc5Sramat 				online_count++;
87963c34adc5Sramat 			MDI_PI_UNLOCK(cpi->cpi_pip);
87973c34adc5Sramat 		}
87983c34adc5Sramat 	}
87993c34adc5Sramat 
88003c34adc5Sramat 	return (online_count);
88013c34adc5Sramat }
88023c34adc5Sramat 
88033c34adc5Sramat /*
88043c34adc5Sramat  * Bus configure all paths for the specified vhci client.
88053c34adc5Sramat  * If at least one path for the client is already online, the remaining paths
88063c34adc5Sramat  * will be configured asynchronously. Otherwise, it synchronously configures
88073c34adc5Sramat  * the paths until at least one path is online and then rest of the paths
88083c34adc5Sramat  * will be configured asynchronously.
88093c34adc5Sramat  */
88103c34adc5Sramat static void
88113c34adc5Sramat config_client_paths(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr)
88123c34adc5Sramat {
88133c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
88143c34adc5Sramat 	mdi_phys_path_t *pp_head, *pp;
88153c34adc5Sramat 	mdi_vhcache_client_t *cct;
88163c34adc5Sramat 	mdi_vhcache_lookup_token_t tok;
88173c34adc5Sramat 
88183c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
88193c34adc5Sramat 
88203c34adc5Sramat 	init_vhcache_lookup_token(&tok, NULL);
88213c34adc5Sramat 
88223c34adc5Sramat 	if (ct_name == NULL || ct_addr == NULL ||
88233c34adc5Sramat 	    (cct = lookup_vhcache_client(vhcache, ct_name, ct_addr, &tok))
88243c34adc5Sramat 	    == NULL ||
88253c34adc5Sramat 	    (pp_head = build_phclient_path_list(cct, ct_name)) == NULL) {
88263c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
88273c34adc5Sramat 		return;
88283c34adc5Sramat 	}
88293c34adc5Sramat 
88303c34adc5Sramat 	/* if at least one path is online, configure the rest asynchronously */
88313c34adc5Sramat 	if (nonline_paths(cct) > 0) {
88323c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
88333c34adc5Sramat 		config_client_paths_async(vhc, ct_name, ct_addr, pp_head, &tok);
88343c34adc5Sramat 		return;
88353c34adc5Sramat 	}
88363c34adc5Sramat 
88373c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
88383c34adc5Sramat 
88393c34adc5Sramat 	for (pp = pp_head; pp != NULL; pp = pp->phys_path_next) {
88403c34adc5Sramat 		if (bus_config_one_phci_child(pp->phys_path) == MDI_SUCCESS) {
88413c34adc5Sramat 			rw_enter(&vhcache->vhcache_lock, RW_READER);
88423c34adc5Sramat 
88433c34adc5Sramat 			if ((cct = lookup_vhcache_client(vhcache, ct_name,
88443c34adc5Sramat 			    ct_addr, &tok)) == NULL) {
88453c34adc5Sramat 				rw_exit(&vhcache->vhcache_lock);
88463c34adc5Sramat 				goto out;
88473c34adc5Sramat 			}
88483c34adc5Sramat 
88493c34adc5Sramat 			if (nonline_paths(cct) > 0 &&
88503c34adc5Sramat 			    pp->phys_path_next != NULL) {
88513c34adc5Sramat 				rw_exit(&vhcache->vhcache_lock);
88523c34adc5Sramat 				config_client_paths_async(vhc, ct_name, ct_addr,
88533c34adc5Sramat 				    pp->phys_path_next, &tok);
88543c34adc5Sramat 				pp->phys_path_next = NULL;
88553c34adc5Sramat 				goto out;
88563c34adc5Sramat 			}
88573c34adc5Sramat 
88583c34adc5Sramat 			rw_exit(&vhcache->vhcache_lock);
88593c34adc5Sramat 		}
88603c34adc5Sramat 	}
88613c34adc5Sramat 
88623c34adc5Sramat 	adjust_sort_vhcache_paths(vhc, ct_name, ct_addr, &tok);
88633c34adc5Sramat out:
88643c34adc5Sramat 	free_phclient_path_list(pp_head);
88653c34adc5Sramat }
88663c34adc5Sramat 
88673c34adc5Sramat static void
88683c34adc5Sramat single_threaded_vhconfig_enter(mdi_vhci_config_t *vhc)
88693c34adc5Sramat {
88703c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
88713c34adc5Sramat 	while (vhc->vhc_flags & MDI_VHC_SINGLE_THREADED)
88723c34adc5Sramat 		cv_wait(&vhc->vhc_cv, &vhc->vhc_lock);
88733c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_SINGLE_THREADED;
88743c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
88753c34adc5Sramat }
88763c34adc5Sramat 
88773c34adc5Sramat static void
88783c34adc5Sramat single_threaded_vhconfig_exit(mdi_vhci_config_t *vhc)
88793c34adc5Sramat {
88803c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
88813c34adc5Sramat 	vhc->vhc_flags &= ~MDI_VHC_SINGLE_THREADED;
88823c34adc5Sramat 	cv_broadcast(&vhc->vhc_cv);
88833c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
88843c34adc5Sramat }
88853c34adc5Sramat 
888652cac543Sramat typedef struct mdi_phci_driver_info {
888752cac543Sramat 	char	*phdriver_name;	/* name of the phci driver */
888852cac543Sramat 
888952cac543Sramat 	/* set to non zero if the phci driver supports root device */
889052cac543Sramat 	int	phdriver_root_support;
889152cac543Sramat } mdi_phci_driver_info_t;
889252cac543Sramat 
88933c34adc5Sramat /*
889452cac543Sramat  * vhci class and root support capability of a phci driver can be
889552cac543Sramat  * specified using ddi-vhci-class and ddi-no-root-support properties in the
889652cac543Sramat  * phci driver.conf file. The built-in tables below contain this information
889752cac543Sramat  * for those phci drivers whose driver.conf files don't yet contain this info.
889852cac543Sramat  *
889952cac543Sramat  * All phci drivers expect iscsi have root device support.
890052cac543Sramat  */
890152cac543Sramat static mdi_phci_driver_info_t scsi_phci_driver_list[] = {
890252cac543Sramat 	{ "fp", 1 },
890352cac543Sramat 	{ "iscsi", 0 },
890452cac543Sramat 	{ "ibsrp", 1 }
890552cac543Sramat 	};
890652cac543Sramat 
890752cac543Sramat static mdi_phci_driver_info_t ib_phci_driver_list[] = { "tavor", 1 };
890852cac543Sramat 
890952cac543Sramat static void *
891052cac543Sramat mdi_realloc(void *old_ptr, size_t old_size, size_t new_size)
891152cac543Sramat {
891252cac543Sramat 	void *new_ptr;
891352cac543Sramat 
891452cac543Sramat 	new_ptr = kmem_zalloc(new_size, KM_SLEEP);
891552cac543Sramat 	if (old_ptr) {
8916f7209cf2Spramodbg 		bcopy(old_ptr, new_ptr, MIN(old_size, new_size));
891752cac543Sramat 		kmem_free(old_ptr, old_size);
891852cac543Sramat 	}
891952cac543Sramat 	return (new_ptr);
892052cac543Sramat }
892152cac543Sramat 
892252cac543Sramat static void
892352cac543Sramat add_to_phci_list(char ***driver_list, int **root_support_list,
892452cac543Sramat     int *cur_elements, int *max_elements, char *driver_name, int root_support)
892552cac543Sramat {
892652cac543Sramat 	ASSERT(*cur_elements <= *max_elements);
892752cac543Sramat 	if (*cur_elements == *max_elements) {
892852cac543Sramat 		*max_elements += 10;
892952cac543Sramat 		*driver_list = mdi_realloc(*driver_list,
893052cac543Sramat 		    sizeof (char *) * (*cur_elements),
893152cac543Sramat 		    sizeof (char *) * (*max_elements));
893252cac543Sramat 		*root_support_list = mdi_realloc(*root_support_list,
893352cac543Sramat 		    sizeof (int) * (*cur_elements),
893452cac543Sramat 		    sizeof (int) * (*max_elements));
893552cac543Sramat 	}
893652cac543Sramat 	(*driver_list)[*cur_elements] = i_ddi_strdup(driver_name, KM_SLEEP);
893752cac543Sramat 	(*root_support_list)[*cur_elements] = root_support;
893852cac543Sramat 	(*cur_elements)++;
893952cac543Sramat }
894052cac543Sramat 
894152cac543Sramat static void
894252cac543Sramat get_phci_driver_list(char *vhci_class, char ***driver_list,
894352cac543Sramat     int **root_support_list, int *cur_elements, int *max_elements)
894452cac543Sramat {
894552cac543Sramat 	mdi_phci_driver_info_t	*st_driver_list, *p;
894652cac543Sramat 	int		st_ndrivers, root_support, i, j, driver_conf_count;
894752cac543Sramat 	major_t		m;
894852cac543Sramat 	struct devnames	*dnp;
894952cac543Sramat 	ddi_prop_t	*propp;
895052cac543Sramat 
895152cac543Sramat 	*driver_list = NULL;
895252cac543Sramat 	*root_support_list = NULL;
895352cac543Sramat 	*cur_elements = 0;
895452cac543Sramat 	*max_elements = 0;
895552cac543Sramat 
895652cac543Sramat 	/* add the phci drivers derived from the phci driver.conf files */
895752cac543Sramat 	for (m = 0; m < devcnt; m++) {
895852cac543Sramat 		dnp = &devnamesp[m];
895952cac543Sramat 
896052cac543Sramat 		if (dnp->dn_flags & DN_PHCI_DRIVER) {
896152cac543Sramat 			LOCK_DEV_OPS(&dnp->dn_lock);
896252cac543Sramat 			if (dnp->dn_global_prop_ptr != NULL &&
896352cac543Sramat 			    (propp = i_ddi_prop_search(DDI_DEV_T_ANY,
896452cac543Sramat 			    DDI_VHCI_CLASS, DDI_PROP_TYPE_STRING,
896552cac543Sramat 			    &dnp->dn_global_prop_ptr->prop_list)) != NULL &&
896652cac543Sramat 			    strcmp(propp->prop_val, vhci_class) == 0) {
896752cac543Sramat 
896852cac543Sramat 				root_support = (i_ddi_prop_search(DDI_DEV_T_ANY,
896952cac543Sramat 				    DDI_NO_ROOT_SUPPORT, DDI_PROP_TYPE_INT,
897052cac543Sramat 				    &dnp->dn_global_prop_ptr->prop_list)
897152cac543Sramat 				    == NULL) ? 1 : 0;
897252cac543Sramat 
897352cac543Sramat 				add_to_phci_list(driver_list, root_support_list,
897452cac543Sramat 				    cur_elements, max_elements, dnp->dn_name,
897552cac543Sramat 				    root_support);
897652cac543Sramat 
897752cac543Sramat 				UNLOCK_DEV_OPS(&dnp->dn_lock);
897852cac543Sramat 			} else
897952cac543Sramat 				UNLOCK_DEV_OPS(&dnp->dn_lock);
898052cac543Sramat 		}
898152cac543Sramat 	}
898252cac543Sramat 
898352cac543Sramat 	driver_conf_count = *cur_elements;
898452cac543Sramat 
898552cac543Sramat 	/* add the phci drivers specified in the built-in tables */
898652cac543Sramat 	if (strcmp(vhci_class, MDI_HCI_CLASS_SCSI) == 0) {
898752cac543Sramat 		st_driver_list = scsi_phci_driver_list;
898852cac543Sramat 		st_ndrivers = sizeof (scsi_phci_driver_list) /
898952cac543Sramat 		    sizeof (mdi_phci_driver_info_t);
899052cac543Sramat 	} else if (strcmp(vhci_class, MDI_HCI_CLASS_IB) == 0) {
899152cac543Sramat 		st_driver_list = ib_phci_driver_list;
899252cac543Sramat 		st_ndrivers = sizeof (ib_phci_driver_list) /
899352cac543Sramat 		    sizeof (mdi_phci_driver_info_t);
899452cac543Sramat 	} else {
899552cac543Sramat 		st_driver_list = NULL;
899652cac543Sramat 		st_ndrivers = 0;
899752cac543Sramat 	}
899852cac543Sramat 
899952cac543Sramat 	for (i = 0, p = st_driver_list; i < st_ndrivers; i++, p++) {
900052cac543Sramat 		/* add this phci driver if not already added before */
900152cac543Sramat 		for (j = 0; j < driver_conf_count; j++) {
900252cac543Sramat 			if (strcmp((*driver_list)[j], p->phdriver_name) == 0)
900352cac543Sramat 				break;
900452cac543Sramat 		}
900552cac543Sramat 		if (j == driver_conf_count) {
900652cac543Sramat 			add_to_phci_list(driver_list, root_support_list,
900752cac543Sramat 			    cur_elements, max_elements, p->phdriver_name,
900852cac543Sramat 			    p->phdriver_root_support);
900952cac543Sramat 		}
901052cac543Sramat 	}
901152cac543Sramat }
901252cac543Sramat 
901352cac543Sramat /*
901452cac543Sramat  * Attach the phci driver instances associated with the specified vhci class.
90153c34adc5Sramat  * If root is mounted attach all phci driver instances.
90163c34adc5Sramat  * If root is not mounted, attach the instances of only those phci
90173c34adc5Sramat  * drivers that have the root support.
90183c34adc5Sramat  */
90193c34adc5Sramat static void
902052cac543Sramat attach_phci_drivers(char *vhci_class)
90213c34adc5Sramat {
902252cac543Sramat 	char	**driver_list, **p;
902352cac543Sramat 	int	*root_support_list;
902452cac543Sramat 	int	cur_elements, max_elements, i;
90253c34adc5Sramat 	major_t	m;
90263c34adc5Sramat 
902752cac543Sramat 	get_phci_driver_list(vhci_class, &driver_list, &root_support_list,
902852cac543Sramat 	    &cur_elements, &max_elements);
90293c34adc5Sramat 
903052cac543Sramat 	for (i = 0; i < cur_elements; i++) {
903152cac543Sramat 		if (modrootloaded || root_support_list[i]) {
903252cac543Sramat 			m = ddi_name_to_major(driver_list[i]);
9033a204de77Scth 			if (m != DDI_MAJOR_T_NONE &&
9034a204de77Scth 			    ddi_hold_installed_driver(m))
90353c34adc5Sramat 				ddi_rele_driver(m);
90363c34adc5Sramat 		}
90373c34adc5Sramat 	}
903852cac543Sramat 
903952cac543Sramat 	if (driver_list) {
904052cac543Sramat 		for (i = 0, p = driver_list; i < cur_elements; i++, p++)
904152cac543Sramat 			kmem_free(*p, strlen(*p) + 1);
904252cac543Sramat 		kmem_free(driver_list, sizeof (char *) * max_elements);
904352cac543Sramat 		kmem_free(root_support_list, sizeof (int) * max_elements);
904452cac543Sramat 	}
90453c34adc5Sramat }
90463c34adc5Sramat 
90473c34adc5Sramat /*
90483c34adc5Sramat  * Build vhci cache:
90493c34adc5Sramat  *
90503c34adc5Sramat  * Attach phci driver instances and then drive BUS_CONFIG_ALL on
90513c34adc5Sramat  * the phci driver instances. During this process the cache gets built.
90523c34adc5Sramat  *
905367e56d35Sramat  * Cache is built fully if the root is mounted.
90543c34adc5Sramat  * If the root is not mounted, phci drivers that do not have root support
90553c34adc5Sramat  * are not attached. As a result the cache is built partially. The entries
90563c34adc5Sramat  * in the cache reflect only those phci drivers that have root support.
90573c34adc5Sramat  */
905867e56d35Sramat static int
905952cac543Sramat build_vhci_cache(mdi_vhci_t *vh)
90603c34adc5Sramat {
906152cac543Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
90623c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
90633c34adc5Sramat 
906467e56d35Sramat 	single_threaded_vhconfig_enter(vhc);
906567e56d35Sramat 
90663c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
90673c34adc5Sramat 	if (vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE) {
90683c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
906967e56d35Sramat 		single_threaded_vhconfig_exit(vhc);
907067e56d35Sramat 		return (0);
90713c34adc5Sramat 	}
90723c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
90733c34adc5Sramat 
907452cac543Sramat 	attach_phci_drivers(vh->vh_class);
90753c34adc5Sramat 	bus_config_all_phcis(vhcache, NDI_DRV_CONF_REPROBE | NDI_NO_EVENT,
9076a204de77Scth 	    BUS_CONFIG_ALL, DDI_MAJOR_T_NONE);
90773c34adc5Sramat 
90783c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
90793c34adc5Sramat 	vhcache->vhcache_flags |= MDI_VHCI_CACHE_SETUP_DONE;
90803c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
908167e56d35Sramat 
908267e56d35Sramat 	single_threaded_vhconfig_exit(vhc);
90833c34adc5Sramat 	vhcache_dirty(vhc);
908467e56d35Sramat 	return (1);
90853c34adc5Sramat }
90863c34adc5Sramat 
90873c34adc5Sramat /*
908867e56d35Sramat  * Determine if discovery of paths is needed.
90893c34adc5Sramat  */
90903c34adc5Sramat static int
909167e56d35Sramat vhcache_do_discovery(mdi_vhci_config_t *vhc)
90923c34adc5Sramat {
909367e56d35Sramat 	int rv = 1;
909467e56d35Sramat 
909567e56d35Sramat 	mutex_enter(&vhc->vhc_lock);
909667e56d35Sramat 	if (i_ddi_io_initialized() == 0) {
909767e56d35Sramat 		if (vhc->vhc_path_discovery_boot > 0) {
909867e56d35Sramat 			vhc->vhc_path_discovery_boot--;
909967e56d35Sramat 			goto out;
910067e56d35Sramat 		}
910167e56d35Sramat 	} else {
910267e56d35Sramat 		if (vhc->vhc_path_discovery_postboot > 0) {
910367e56d35Sramat 			vhc->vhc_path_discovery_postboot--;
910467e56d35Sramat 			goto out;
910567e56d35Sramat 		}
910667e56d35Sramat 	}
910767e56d35Sramat 
910867e56d35Sramat 	/*
910967e56d35Sramat 	 * Do full path discovery at most once per mdi_path_discovery_interval.
911067e56d35Sramat 	 * This is to avoid a series of full path discoveries when opening
911167e56d35Sramat 	 * stale /dev/[r]dsk links.
911267e56d35Sramat 	 */
911367e56d35Sramat 	if (mdi_path_discovery_interval != -1 &&
9114d3d50737SRafael Vanoni 	    ddi_get_lbolt64() >= vhc->vhc_path_discovery_cutoff_time)
911567e56d35Sramat 		goto out;
911667e56d35Sramat 
911767e56d35Sramat 	rv = 0;
911867e56d35Sramat out:
911967e56d35Sramat 	mutex_exit(&vhc->vhc_lock);
912067e56d35Sramat 	return (rv);
912167e56d35Sramat }
912267e56d35Sramat 
912367e56d35Sramat /*
912467e56d35Sramat  * Discover all paths:
912567e56d35Sramat  *
912667e56d35Sramat  * Attach phci driver instances and then drive BUS_CONFIG_ALL on all the phci
912767e56d35Sramat  * driver instances. During this process all paths will be discovered.
912867e56d35Sramat  */
912967e56d35Sramat static int
913052cac543Sramat vhcache_discover_paths(mdi_vhci_t *vh)
913167e56d35Sramat {
913252cac543Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
913367e56d35Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
913467e56d35Sramat 	int rv = 0;
91353c34adc5Sramat 
91363c34adc5Sramat 	single_threaded_vhconfig_enter(vhc);
91373c34adc5Sramat 
913867e56d35Sramat 	if (vhcache_do_discovery(vhc)) {
913952cac543Sramat 		attach_phci_drivers(vh->vh_class);
914067e56d35Sramat 		bus_config_all_phcis(vhcache, NDI_DRV_CONF_REPROBE |
9141a204de77Scth 		    NDI_NO_EVENT, BUS_CONFIG_ALL, DDI_MAJOR_T_NONE);
914267e56d35Sramat 
91433c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
9144d3d50737SRafael Vanoni 		vhc->vhc_path_discovery_cutoff_time = ddi_get_lbolt64() +
914567e56d35Sramat 		    mdi_path_discovery_interval * TICKS_PER_SECOND;
91463c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
914767e56d35Sramat 		rv = 1;
91483c34adc5Sramat 	}
91493c34adc5Sramat 
91503c34adc5Sramat 	single_threaded_vhconfig_exit(vhc);
91513c34adc5Sramat 	return (rv);
91523c34adc5Sramat }
91533c34adc5Sramat 
91543c34adc5Sramat /*
91553c34adc5Sramat  * Generic vhci bus config implementation:
91563c34adc5Sramat  *
91573c34adc5Sramat  * Parameters
91583c34adc5Sramat  *	vdip	vhci dip
91593c34adc5Sramat  *	flags	bus config flags
91603c34adc5Sramat  *	op	bus config operation
91613c34adc5Sramat  *	The remaining parameters are bus config operation specific
91623c34adc5Sramat  *
91633c34adc5Sramat  * for BUS_CONFIG_ONE
91643c34adc5Sramat  *	arg	pointer to name@addr
91653c34adc5Sramat  *	child	upon successful return from this function, *child will be
91663c34adc5Sramat  *		set to the configured and held devinfo child node of vdip.
91673c34adc5Sramat  *	ct_addr	pointer to client address (i.e. GUID)
91683c34adc5Sramat  *
91693c34adc5Sramat  * for BUS_CONFIG_DRIVER
91703c34adc5Sramat  *	arg	major number of the driver
91713c34adc5Sramat  *	child and ct_addr parameters are ignored
91723c34adc5Sramat  *
91733c34adc5Sramat  * for BUS_CONFIG_ALL
91743c34adc5Sramat  *	arg, child, and ct_addr parameters are ignored
91753c34adc5Sramat  *
91763c34adc5Sramat  * Note that for the rest of the bus config operations, this function simply
91773c34adc5Sramat  * calls the framework provided default bus config routine.
91783c34adc5Sramat  */
91793c34adc5Sramat int
91803c34adc5Sramat mdi_vhci_bus_config(dev_info_t *vdip, uint_t flags, ddi_bus_config_op_t op,
91813c34adc5Sramat     void *arg, dev_info_t **child, char *ct_addr)
91823c34adc5Sramat {
91833c34adc5Sramat 	mdi_vhci_t *vh = i_devi_get_vhci(vdip);
91843c34adc5Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
91853c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
918667e56d35Sramat 	int rv = 0;
918767e56d35Sramat 	int params_valid = 0;
91883c34adc5Sramat 	char *cp;
91893c34adc5Sramat 
91903c34adc5Sramat 	/*
91915e3986cbScth 	 * To bus config vhcis we relay operation, possibly using another
91925e3986cbScth 	 * thread, to phcis. The phci driver then interacts with MDI to cause
91935e3986cbScth 	 * vhci child nodes to be enumerated under the vhci node.  Adding a
91945e3986cbScth 	 * vhci child requires an ndi_devi_enter of the vhci. Since another
91955e3986cbScth 	 * thread may be adding the child, to avoid deadlock we can't wait
91965e3986cbScth 	 * for the relayed operations to complete if we have already entered
91975e3986cbScth 	 * the vhci node.
91983c34adc5Sramat 	 */
91993c34adc5Sramat 	if (DEVI_BUSY_OWNED(vdip)) {
92004c06356bSdh142964 		MDI_DEBUG(2, (MDI_NOTE, vdip,
92014c06356bSdh142964 		    "vhci dip is busy owned %p", (void *)vdip));
92023c34adc5Sramat 		goto default_bus_config;
92033c34adc5Sramat 	}
92043c34adc5Sramat 
92053c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
92063c34adc5Sramat 	if (!(vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE)) {
92073c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
920852cac543Sramat 		rv = build_vhci_cache(vh);
92093c34adc5Sramat 		rw_enter(&vhcache->vhcache_lock, RW_READER);
92103c34adc5Sramat 	}
92113c34adc5Sramat 
92123c34adc5Sramat 	switch (op) {
92133c34adc5Sramat 	case BUS_CONFIG_ONE:
921467e56d35Sramat 		if (arg != NULL && ct_addr != NULL) {
92153c34adc5Sramat 			/* extract node name */
92163c34adc5Sramat 			cp = (char *)arg;
92173c34adc5Sramat 			while (*cp != '\0' && *cp != '@')
92183c34adc5Sramat 				cp++;
92193c34adc5Sramat 			if (*cp == '@') {
922067e56d35Sramat 				params_valid = 1;
92213c34adc5Sramat 				*cp = '\0';
92223c34adc5Sramat 				config_client_paths(vhc, (char *)arg, ct_addr);
922367e56d35Sramat 				/* config_client_paths() releases cache_lock */
92243c34adc5Sramat 				*cp = '@';
922567e56d35Sramat 				break;
922667e56d35Sramat 			}
922767e56d35Sramat 		}
922867e56d35Sramat 
92293c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
92303c34adc5Sramat 		break;
92313c34adc5Sramat 
92323c34adc5Sramat 	case BUS_CONFIG_DRIVER:
92333c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
923467e56d35Sramat 		if (rv == 0)
923567e56d35Sramat 			st_bus_config_all_phcis(vhc, flags, op,
92363c34adc5Sramat 			    (major_t)(uintptr_t)arg);
92373c34adc5Sramat 		break;
92383c34adc5Sramat 
92393c34adc5Sramat 	case BUS_CONFIG_ALL:
92403c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
924167e56d35Sramat 		if (rv == 0)
924267e56d35Sramat 			st_bus_config_all_phcis(vhc, flags, op, -1);
92433c34adc5Sramat 		break;
92443c34adc5Sramat 
92453c34adc5Sramat 	default:
92463c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
92473c34adc5Sramat 		break;
92483c34adc5Sramat 	}
92493c34adc5Sramat 
92503c34adc5Sramat 
92513c34adc5Sramat default_bus_config:
92523c34adc5Sramat 	/*
92533c34adc5Sramat 	 * All requested child nodes are enumerated under the vhci.
92543c34adc5Sramat 	 * Now configure them.
92553c34adc5Sramat 	 */
92563c34adc5Sramat 	if (ndi_busop_bus_config(vdip, flags, op, arg, child, 0) ==
92573c34adc5Sramat 	    NDI_SUCCESS) {
92583c34adc5Sramat 		return (MDI_SUCCESS);
925967e56d35Sramat 	} else if (op == BUS_CONFIG_ONE && rv == 0 && params_valid) {
926067e56d35Sramat 		/* discover all paths and try configuring again */
926152cac543Sramat 		if (vhcache_discover_paths(vh) &&
926267e56d35Sramat 		    ndi_busop_bus_config(vdip, flags, op, arg, child, 0) ==
926367e56d35Sramat 		    NDI_SUCCESS)
926467e56d35Sramat 			return (MDI_SUCCESS);
92653c34adc5Sramat 	}
92663c34adc5Sramat 
92673c34adc5Sramat 	return (MDI_FAILURE);
92683c34adc5Sramat }
92693c34adc5Sramat 
92703c34adc5Sramat /*
92713c34adc5Sramat  * Read the on-disk vhci cache into an nvlist for the specified vhci class.
92723c34adc5Sramat  */
92733c34adc5Sramat static nvlist_t *
92743c34adc5Sramat read_on_disk_vhci_cache(char *vhci_class)
92753c34adc5Sramat {
92763c34adc5Sramat 	nvlist_t *nvl;
92773c34adc5Sramat 	int err;
92783c34adc5Sramat 	char *filename;
92793c34adc5Sramat 
92803c34adc5Sramat 	filename = vhclass2vhcache_filename(vhci_class);
92813c34adc5Sramat 
92823c34adc5Sramat 	if ((err = fread_nvlist(filename, &nvl)) == 0) {
92833c34adc5Sramat 		kmem_free(filename, strlen(filename) + 1);
92843c34adc5Sramat 		return (nvl);
92853c34adc5Sramat 	} else if (err == EIO)
92864c06356bSdh142964 		cmn_err(CE_WARN, "%s: I/O error, will recreate", filename);
92873c34adc5Sramat 	else if (err == EINVAL)
92883c34adc5Sramat 		cmn_err(CE_WARN,
92894c06356bSdh142964 		    "%s: data file corrupted, will recreate", filename);
92903c34adc5Sramat 
92913c34adc5Sramat 	kmem_free(filename, strlen(filename) + 1);
92923c34adc5Sramat 	return (NULL);
92933c34adc5Sramat }
92943c34adc5Sramat 
92953c34adc5Sramat /*
92963c34adc5Sramat  * Read on-disk vhci cache into nvlists for all vhci classes.
92973c34adc5Sramat  * Called during booting by i_ddi_read_devices_files().
92983c34adc5Sramat  */
92993c34adc5Sramat void
93003c34adc5Sramat mdi_read_devices_files(void)
93013c34adc5Sramat {
93023c34adc5Sramat 	int i;
93033c34adc5Sramat 
93043c34adc5Sramat 	for (i = 0; i < N_VHCI_CLASSES; i++)
93053c34adc5Sramat 		vhcache_nvl[i] = read_on_disk_vhci_cache(vhci_class_list[i]);
93063c34adc5Sramat }
93073c34adc5Sramat 
93083c34adc5Sramat /*
93093c34adc5Sramat  * Remove all stale entries from vhci cache.
93103c34adc5Sramat  */
93113c34adc5Sramat static void
93123c34adc5Sramat clean_vhcache(mdi_vhci_config_t *vhc)
93133c34adc5Sramat {
93143c34adc5Sramat 	mdi_vhci_cache_t	*vhcache = &vhc->vhc_vhcache;
931542353512SRandall Ralphs 	mdi_vhcache_phci_t	*phci, *nxt_phci;
931642353512SRandall Ralphs 	mdi_vhcache_client_t	*client, *nxt_client;
931742353512SRandall Ralphs 	mdi_vhcache_pathinfo_t	*path, *nxt_path;
93183c34adc5Sramat 
93193c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
93203c34adc5Sramat 
932142353512SRandall Ralphs 	client = vhcache->vhcache_client_head;
93223c34adc5Sramat 	vhcache->vhcache_client_head = vhcache->vhcache_client_tail = NULL;
932342353512SRandall Ralphs 	for ( ; client != NULL; client = nxt_client) {
932442353512SRandall Ralphs 		nxt_client = client->cct_next;
93253c34adc5Sramat 
932642353512SRandall Ralphs 		path = client->cct_cpi_head;
932742353512SRandall Ralphs 		client->cct_cpi_head = client->cct_cpi_tail = NULL;
932842353512SRandall Ralphs 		for ( ; path != NULL; path = nxt_path) {
932942353512SRandall Ralphs 			nxt_path = path->cpi_next;
933042353512SRandall Ralphs 			if ((path->cpi_cphci->cphci_phci != NULL) &&
933142353512SRandall Ralphs 			    (path->cpi_pip != NULL)) {
933242353512SRandall Ralphs 				enqueue_tail_vhcache_pathinfo(client, path);
933342353512SRandall Ralphs 			} else if (path->cpi_pip != NULL) {
933442353512SRandall Ralphs 				/* Not valid to have a path without a phci. */
933542353512SRandall Ralphs 				free_vhcache_pathinfo(path);
933642353512SRandall Ralphs 			}
93373c34adc5Sramat 		}
93383c34adc5Sramat 
933942353512SRandall Ralphs 		if (client->cct_cpi_head != NULL)
934042353512SRandall Ralphs 			enqueue_vhcache_client(vhcache, client);
93413c34adc5Sramat 		else {
93423c34adc5Sramat 			(void) mod_hash_destroy(vhcache->vhcache_client_hash,
934342353512SRandall Ralphs 			    (mod_hash_key_t)client->cct_name_addr);
934442353512SRandall Ralphs 			free_vhcache_client(client);
93453c34adc5Sramat 		}
93463c34adc5Sramat 	}
93473c34adc5Sramat 
934842353512SRandall Ralphs 	phci = vhcache->vhcache_phci_head;
93493c34adc5Sramat 	vhcache->vhcache_phci_head = vhcache->vhcache_phci_tail = NULL;
935042353512SRandall Ralphs 	for ( ; phci != NULL; phci = nxt_phci) {
935142353512SRandall Ralphs 
935242353512SRandall Ralphs 		nxt_phci = phci->cphci_next;
935342353512SRandall Ralphs 		if (phci->cphci_phci != NULL)
935442353512SRandall Ralphs 			enqueue_vhcache_phci(vhcache, phci);
93553c34adc5Sramat 		else
935642353512SRandall Ralphs 			free_vhcache_phci(phci);
93573c34adc5Sramat 	}
93583c34adc5Sramat 
9359d3d50737SRafael Vanoni 	vhcache->vhcache_clean_time = ddi_get_lbolt64();
93603c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
93613c34adc5Sramat 	vhcache_dirty(vhc);
93623c34adc5Sramat }
93633c34adc5Sramat 
93643c34adc5Sramat /*
93653c34adc5Sramat  * Remove all stale entries from vhci cache.
93663c34adc5Sramat  * Called by i_ddi_clean_devices_files() during the execution of devfsadm -C
93673c34adc5Sramat  */
93683c34adc5Sramat void
93693c34adc5Sramat mdi_clean_vhcache(void)
93703c34adc5Sramat {
93713c34adc5Sramat 	mdi_vhci_t *vh;
93723c34adc5Sramat 
93733c34adc5Sramat 	mutex_enter(&mdi_mutex);
93743c34adc5Sramat 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
93753c34adc5Sramat 		vh->vh_refcnt++;
93763c34adc5Sramat 		mutex_exit(&mdi_mutex);
93773c34adc5Sramat 		clean_vhcache(vh->vh_config);
93783c34adc5Sramat 		mutex_enter(&mdi_mutex);
93793c34adc5Sramat 		vh->vh_refcnt--;
93803c34adc5Sramat 	}
93813c34adc5Sramat 	mutex_exit(&mdi_mutex);
93823c34adc5Sramat }
93838c4f8890Srs135747 
93848c4f8890Srs135747 /*
93858c4f8890Srs135747  * mdi_vhci_walk_clients():
93868c4f8890Srs135747  *		Walker routine to traverse client dev_info nodes
93878c4f8890Srs135747  * ddi_walk_devs(ddi_get_child(vdip), f, arg) returns the entire tree
93888c4f8890Srs135747  * below the client, including nexus devices, which we dont want.
93898c4f8890Srs135747  * So we just traverse the immediate siblings, starting from 1st client.
93908c4f8890Srs135747  */
93918c4f8890Srs135747 void
93928c4f8890Srs135747 mdi_vhci_walk_clients(dev_info_t *vdip,
93938c4f8890Srs135747     int (*f)(dev_info_t *, void *), void *arg)
93948c4f8890Srs135747 {
93955e3986cbScth 	mdi_vhci_t	*vh = i_devi_get_vhci(vdip);
93968c4f8890Srs135747 	dev_info_t	*cdip;
93978c4f8890Srs135747 	mdi_client_t	*ct;
93988c4f8890Srs135747 
93995e3986cbScth 	MDI_VHCI_CLIENT_LOCK(vh);
94008c4f8890Srs135747 	cdip = ddi_get_child(vdip);
94018c4f8890Srs135747 	while (cdip) {
94028c4f8890Srs135747 		ct = i_devi_get_client(cdip);
94038c4f8890Srs135747 		MDI_CLIENT_LOCK(ct);
94048c4f8890Srs135747 
94055e3986cbScth 		if (((*f)(cdip, arg)) == DDI_WALK_CONTINUE)
94068c4f8890Srs135747 			cdip = ddi_get_next_sibling(cdip);
94075e3986cbScth 		else
94085e3986cbScth 			cdip = NULL;
9409c73a93f2Sdm120769 
9410c73a93f2Sdm120769 		MDI_CLIENT_UNLOCK(ct);
94118c4f8890Srs135747 	}
94125e3986cbScth 	MDI_VHCI_CLIENT_UNLOCK(vh);
94138c4f8890Srs135747 }
94148c4f8890Srs135747 
94158c4f8890Srs135747 /*
94168c4f8890Srs135747  * mdi_vhci_walk_phcis():
94178c4f8890Srs135747  *		Walker routine to traverse phci dev_info nodes
94188c4f8890Srs135747  */
94198c4f8890Srs135747 void
94208c4f8890Srs135747 mdi_vhci_walk_phcis(dev_info_t *vdip,
94218c4f8890Srs135747     int (*f)(dev_info_t *, void *), void *arg)
94228c4f8890Srs135747 {
94235e3986cbScth 	mdi_vhci_t	*vh = i_devi_get_vhci(vdip);
94245e3986cbScth 	mdi_phci_t	*ph, *next;
94258c4f8890Srs135747 
94265e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
94278c4f8890Srs135747 	ph = vh->vh_phci_head;
94288c4f8890Srs135747 	while (ph) {
94298c4f8890Srs135747 		MDI_PHCI_LOCK(ph);
94308c4f8890Srs135747 
94315e3986cbScth 		if (((*f)(ph->ph_dip, arg)) == DDI_WALK_CONTINUE)
94325e3986cbScth 			next = ph->ph_next;
94335e3986cbScth 		else
94345e3986cbScth 			next = NULL;
9435c73a93f2Sdm120769 
9436c73a93f2Sdm120769 		MDI_PHCI_UNLOCK(ph);
94375e3986cbScth 		ph = next;
94388c4f8890Srs135747 	}
94395e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
94408c4f8890Srs135747 }
94418c4f8890Srs135747 
94428c4f8890Srs135747 
94438c4f8890Srs135747 /*
94448c4f8890Srs135747  * mdi_walk_vhcis():
94458c4f8890Srs135747  *		Walker routine to traverse vhci dev_info nodes
94468c4f8890Srs135747  */
94478c4f8890Srs135747 void
94488c4f8890Srs135747 mdi_walk_vhcis(int (*f)(dev_info_t *, void *), void *arg)
94498c4f8890Srs135747 {
94508c4f8890Srs135747 	mdi_vhci_t	*vh = NULL;
94518c4f8890Srs135747 
94528c4f8890Srs135747 	mutex_enter(&mdi_mutex);
94538c4f8890Srs135747 	/*
94548c4f8890Srs135747 	 * Scan for already registered vhci
94558c4f8890Srs135747 	 */
94568c4f8890Srs135747 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
94578c4f8890Srs135747 		vh->vh_refcnt++;
94588c4f8890Srs135747 		mutex_exit(&mdi_mutex);
94598c4f8890Srs135747 		if (((*f)(vh->vh_dip, arg)) != DDI_WALK_CONTINUE) {
94608c4f8890Srs135747 			mutex_enter(&mdi_mutex);
94618c4f8890Srs135747 			vh->vh_refcnt--;
94628c4f8890Srs135747 			break;
94638c4f8890Srs135747 		} else {
94648c4f8890Srs135747 			mutex_enter(&mdi_mutex);
94658c4f8890Srs135747 			vh->vh_refcnt--;
94668c4f8890Srs135747 		}
94678c4f8890Srs135747 	}
94688c4f8890Srs135747 
94698c4f8890Srs135747 	mutex_exit(&mdi_mutex);
94708c4f8890Srs135747 }
94718c4f8890Srs135747 
94728c4f8890Srs135747 /*
94738c4f8890Srs135747  * i_mdi_log_sysevent():
94748c4f8890Srs135747  *		Logs events for pickup by syseventd
94758c4f8890Srs135747  */
94768c4f8890Srs135747 static void
94778c4f8890Srs135747 i_mdi_log_sysevent(dev_info_t *dip, char *ph_vh_class, char *subclass)
94788c4f8890Srs135747 {
94798c4f8890Srs135747 	char		*path_name;
94808c4f8890Srs135747 	nvlist_t	*attr_list;
94818c4f8890Srs135747 
94828c4f8890Srs135747 	if (nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE,
94838c4f8890Srs135747 	    KM_SLEEP) != DDI_SUCCESS) {
94848c4f8890Srs135747 		goto alloc_failed;
94858c4f8890Srs135747 	}
94868c4f8890Srs135747 
94878c4f8890Srs135747 	path_name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
94888c4f8890Srs135747 	(void) ddi_pathname(dip, path_name);
94898c4f8890Srs135747 
94908c4f8890Srs135747 	if (nvlist_add_string(attr_list, DDI_DRIVER_NAME,
94918c4f8890Srs135747 	    ddi_driver_name(dip)) != DDI_SUCCESS) {
94928c4f8890Srs135747 		goto error;
94938c4f8890Srs135747 	}
94948c4f8890Srs135747 
94958c4f8890Srs135747 	if (nvlist_add_int32(attr_list, DDI_DRIVER_MAJOR,
94968c4f8890Srs135747 	    (int32_t)ddi_driver_major(dip)) != DDI_SUCCESS) {
94978c4f8890Srs135747 		goto error;
94988c4f8890Srs135747 	}
94998c4f8890Srs135747 
95008c4f8890Srs135747 	if (nvlist_add_int32(attr_list, DDI_INSTANCE,
95018c4f8890Srs135747 	    (int32_t)ddi_get_instance(dip)) != DDI_SUCCESS) {
95028c4f8890Srs135747 		goto error;
95038c4f8890Srs135747 	}
95048c4f8890Srs135747 
95058c4f8890Srs135747 	if (nvlist_add_string(attr_list, DDI_PATHNAME,
95068c4f8890Srs135747 	    path_name) != DDI_SUCCESS) {
95078c4f8890Srs135747 		goto error;
95088c4f8890Srs135747 	}
95098c4f8890Srs135747 
95108c4f8890Srs135747 	if (nvlist_add_string(attr_list, DDI_CLASS,
95118c4f8890Srs135747 	    ph_vh_class) != DDI_SUCCESS) {
95128c4f8890Srs135747 		goto error;
95138c4f8890Srs135747 	}
95148c4f8890Srs135747 
95158c4f8890Srs135747 	(void) ddi_log_sysevent(dip, DDI_VENDOR_SUNW, EC_DDI, subclass,
95168c4f8890Srs135747 	    attr_list, NULL, DDI_SLEEP);
95178c4f8890Srs135747 
95188c4f8890Srs135747 error:
95198c4f8890Srs135747 	kmem_free(path_name, MAXPATHLEN);
95208c4f8890Srs135747 	nvlist_free(attr_list);
95218c4f8890Srs135747 	return;
95228c4f8890Srs135747 
95238c4f8890Srs135747 alloc_failed:
95244c06356bSdh142964 	MDI_DEBUG(1, (MDI_WARN, dip, "!unable to send sysevent"));
95258c4f8890Srs135747 }
9526f7209cf2Spramodbg 
9527f7209cf2Spramodbg char **
9528f7209cf2Spramodbg mdi_get_phci_driver_list(char *vhci_class, int	*ndrivers)
9529f7209cf2Spramodbg {
9530f7209cf2Spramodbg 	char	**driver_list, **ret_driver_list = NULL;
9531f7209cf2Spramodbg 	int	*root_support_list;
9532f7209cf2Spramodbg 	int	cur_elements, max_elements;
9533f7209cf2Spramodbg 
9534f7209cf2Spramodbg 	get_phci_driver_list(vhci_class, &driver_list, &root_support_list,
9535f7209cf2Spramodbg 	    &cur_elements, &max_elements);
9536f7209cf2Spramodbg 
9537f7209cf2Spramodbg 
9538f7209cf2Spramodbg 	if (driver_list) {
9539f7209cf2Spramodbg 		kmem_free(root_support_list, sizeof (int) * max_elements);
9540f7209cf2Spramodbg 		ret_driver_list = mdi_realloc(driver_list, sizeof (char *)
9541f7209cf2Spramodbg 		    * max_elements, sizeof (char *) * cur_elements);
9542f7209cf2Spramodbg 	}
9543f7209cf2Spramodbg 	*ndrivers = cur_elements;
9544f7209cf2Spramodbg 
9545f7209cf2Spramodbg 	return (ret_driver_list);
9546f7209cf2Spramodbg 
9547f7209cf2Spramodbg }
9548f7209cf2Spramodbg 
9549f7209cf2Spramodbg void
9550f7209cf2Spramodbg mdi_free_phci_driver_list(char **driver_list, int ndrivers)
9551f7209cf2Spramodbg {
9552f7209cf2Spramodbg 	char	**p;
9553f7209cf2Spramodbg 	int	i;
9554f7209cf2Spramodbg 
9555f7209cf2Spramodbg 	if (driver_list) {
9556f7209cf2Spramodbg 		for (i = 0, p = driver_list; i < ndrivers; i++, p++)
9557f7209cf2Spramodbg 			kmem_free(*p, strlen(*p) + 1);
9558f7209cf2Spramodbg 		kmem_free(driver_list, sizeof (char *) * ndrivers);
9559f7209cf2Spramodbg 	}
9560f7209cf2Spramodbg }
956155e592a2SRandall Ralphs 
956255e592a2SRandall Ralphs /*
956355e592a2SRandall Ralphs  * mdi_is_dev_supported():
956455e592a2SRandall Ralphs  *		function called by pHCI bus config operation to determine if a
956555e592a2SRandall Ralphs  *		device should be represented as a child of the vHCI or the
956655e592a2SRandall Ralphs  *		pHCI.  This decision is made by the vHCI, using cinfo idenity
956755e592a2SRandall Ralphs  *		information passed by the pHCI - specifics of the cinfo
956855e592a2SRandall Ralphs  *		representation are by agreement between the pHCI and vHCI.
956955e592a2SRandall Ralphs  * Return Values:
957055e592a2SRandall Ralphs  *		MDI_SUCCESS
957155e592a2SRandall Ralphs  *		MDI_FAILURE
957255e592a2SRandall Ralphs  */
957355e592a2SRandall Ralphs int
957455e592a2SRandall Ralphs mdi_is_dev_supported(char *class, dev_info_t *pdip, void *cinfo)
957555e592a2SRandall Ralphs {
957655e592a2SRandall Ralphs 	mdi_vhci_t	*vh;
957755e592a2SRandall Ralphs 
957855e592a2SRandall Ralphs 	ASSERT(class && pdip);
957955e592a2SRandall Ralphs 
958055e592a2SRandall Ralphs 	/*
958155e592a2SRandall Ralphs 	 * For dev_supported, mdi_phci_register() must have established pdip as
958255e592a2SRandall Ralphs 	 * a pHCI.
958355e592a2SRandall Ralphs 	 *
958455e592a2SRandall Ralphs 	 * NOTE: mdi_phci_register() does "mpxio-disable" processing, and
958555e592a2SRandall Ralphs 	 * MDI_PHCI(pdip) will return false if mpxio is disabled.
958655e592a2SRandall Ralphs 	 */
958755e592a2SRandall Ralphs 	if (!MDI_PHCI(pdip))
958855e592a2SRandall Ralphs 		return (MDI_FAILURE);
958955e592a2SRandall Ralphs 
959055e592a2SRandall Ralphs 	/* Return MDI_FAILURE if vHCI does not support asking the question. */
959155e592a2SRandall Ralphs 	vh = (mdi_vhci_t *)i_mdi_vhci_class2vhci(class);
959255e592a2SRandall Ralphs 	if ((vh == NULL) || (vh->vh_ops->vo_is_dev_supported == NULL)) {
959355e592a2SRandall Ralphs 		return (MDI_FAILURE);
959455e592a2SRandall Ralphs 	}
959555e592a2SRandall Ralphs 
959655e592a2SRandall Ralphs 	/* Return vHCI answer */
959755e592a2SRandall Ralphs 	return (vh->vh_ops->vo_is_dev_supported(vh->vh_dip, pdip, cinfo));
959855e592a2SRandall Ralphs }
959955e592a2SRandall Ralphs 
960055e592a2SRandall Ralphs int
960155e592a2SRandall Ralphs mdi_dc_return_dev_state(mdi_pathinfo_t *pip, struct devctl_iocdata *dcp)
960255e592a2SRandall Ralphs {
960355e592a2SRandall Ralphs 	uint_t devstate = 0;
960455e592a2SRandall Ralphs 	dev_info_t *cdip;
960555e592a2SRandall Ralphs 
960655e592a2SRandall Ralphs 	if ((pip == NULL) || (dcp == NULL))
960755e592a2SRandall Ralphs 		return (MDI_FAILURE);
960855e592a2SRandall Ralphs 
960955e592a2SRandall Ralphs 	cdip = mdi_pi_get_client(pip);
961055e592a2SRandall Ralphs 
961155e592a2SRandall Ralphs 	switch (mdi_pi_get_state(pip)) {
961255e592a2SRandall Ralphs 	case MDI_PATHINFO_STATE_INIT:
961355e592a2SRandall Ralphs 		devstate = DEVICE_DOWN;
961455e592a2SRandall Ralphs 		break;
961555e592a2SRandall Ralphs 	case MDI_PATHINFO_STATE_ONLINE:
961655e592a2SRandall Ralphs 		devstate = DEVICE_ONLINE;
961755e592a2SRandall Ralphs 		if ((cdip) && (devi_stillreferenced(cdip) == DEVI_REFERENCED))
961855e592a2SRandall Ralphs 			devstate |= DEVICE_BUSY;
961955e592a2SRandall Ralphs 		break;
962055e592a2SRandall Ralphs 	case MDI_PATHINFO_STATE_STANDBY:
962155e592a2SRandall Ralphs 		devstate = DEVICE_ONLINE;
962255e592a2SRandall Ralphs 		break;
962355e592a2SRandall Ralphs 	case MDI_PATHINFO_STATE_FAULT:
962455e592a2SRandall Ralphs 		devstate = DEVICE_DOWN;
962555e592a2SRandall Ralphs 		break;
962655e592a2SRandall Ralphs 	case MDI_PATHINFO_STATE_OFFLINE:
962755e592a2SRandall Ralphs 		devstate = DEVICE_OFFLINE;
962855e592a2SRandall Ralphs 		break;
962955e592a2SRandall Ralphs 	default:
963055e592a2SRandall Ralphs 		ASSERT(MDI_PI(pip)->pi_state);
963155e592a2SRandall Ralphs 	}
963255e592a2SRandall Ralphs 
963355e592a2SRandall Ralphs 	if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
963455e592a2SRandall Ralphs 		return (MDI_FAILURE);
963555e592a2SRandall Ralphs 
963655e592a2SRandall Ralphs 	return (MDI_SUCCESS);
963755e592a2SRandall Ralphs }
9638