xref: /illumos-gate/usr/src/uts/common/os/sunmdi.c (revision d5ebc493)
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
5ee28b439Scm  * Common Development and Distribution License (the "License").
6ee28b439Scm  * 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 /*
22bf002425SStephen Hanson  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2311779b4cSJeffry Molanus  * Copyright (c) 2014 Nexenta Systems Inc. All rights reserved.
249c7f5315SJohn Levon  * Copyright (c) 2018, Joyent, Inc.
25d27aea3fSDan Cross  * Copyright 2023 Oxide Computer Company
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
291724dc7bSJoshua M. Clulow  * Multipath driver interface (MDI) implementation; see mdi_impldefs.h for a
301724dc7bSJoshua M. Clulow  * more detailed discussion of the overall mpxio architecture.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * Default locking order:
337c478bd9Sstevel@tonic-gate  *
345e3986cbScth  * _NOTE(LOCK_ORDER(mdi_mutex, mdi_vhci:vh_phci_mutex);
355e3986cbScth  * _NOTE(LOCK_ORDER(mdi_mutex, mdi_vhci:vh_client_mutex);
365e3986cbScth  * _NOTE(LOCK_ORDER(mdi_vhci:vh_phci_mutex, mdi_phci::ph_mutex);
375e3986cbScth  * _NOTE(LOCK_ORDER(mdi_vhci:vh_client_mutex, mdi_client::ct_mutex);
387c478bd9Sstevel@tonic-gate  * _NOTE(LOCK_ORDER(mdi_phci::ph_mutex mdi_pathinfo::pi_mutex))
397c478bd9Sstevel@tonic-gate  * _NOTE(LOCK_ORDER(mdi_phci::ph_mutex mdi_client::ct_mutex))
407c478bd9Sstevel@tonic-gate  * _NOTE(LOCK_ORDER(mdi_client::ct_mutex mdi_pathinfo::pi_mutex))
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <sys/note.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
467c478bd9Sstevel@tonic-gate #include <sys/param.h>
477c478bd9Sstevel@tonic-gate #include <sys/errno.h>
487c478bd9Sstevel@tonic-gate #include <sys/uio.h>
497c478bd9Sstevel@tonic-gate #include <sys/buf.h>
507c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
517c478bd9Sstevel@tonic-gate #include <sys/open.h>
527c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
537c478bd9Sstevel@tonic-gate #include <sys/poll.h>
547c478bd9Sstevel@tonic-gate #include <sys/conf.h>
557c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
567c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
577c478bd9Sstevel@tonic-gate #include <sys/stat.h>
587c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
597c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
607c478bd9Sstevel@tonic-gate #include <sys/ddipropdefs.h>
617c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
627c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
637c478bd9Sstevel@tonic-gate #include <sys/promif.h>
647c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h>
657c478bd9Sstevel@tonic-gate #include <sys/mdi_impldefs.h>
667c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
677c478bd9Sstevel@tonic-gate #include <sys/epm.h>
687c478bd9Sstevel@tonic-gate #include <sys/sunpm.h>
693c34adc5Sramat #include <sys/modhash.h>
708c4f8890Srs #include <sys/disp.h>
718c4f8890Srs #include <sys/autoconf.h>
72f7209cf2Spramodbg #include <sys/sysmacros.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #ifdef	DEBUG
757c478bd9Sstevel@tonic-gate #include <sys/debug.h>
767c478bd9Sstevel@tonic-gate int	mdi_debug = 1;
775e3986cbScth int	mdi_debug_logonly = 0;
784c06356bSdh #define	MDI_DEBUG(dbglevel, pargs) if (mdi_debug >= (dbglevel))	i_mdi_log pargs
794c06356bSdh #define	MDI_WARN	CE_WARN, __func__
804c06356bSdh #define	MDI_NOTE	CE_NOTE, __func__
814c06356bSdh #define	MDI_CONT	CE_CONT, __func__
824c06356bSdh static void i_mdi_log(int, const char *, dev_info_t *, const char *, ...);
837c478bd9Sstevel@tonic-gate #else	/* !DEBUG */
844c06356bSdh #define	MDI_DEBUG(dbglevel, pargs)
857c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
864c06356bSdh int	mdi_debug_consoleonly = 0;
8796c4a178SChris Horne int	mdi_delay = 3;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate extern pri_t	minclsyspri;
907c478bd9Sstevel@tonic-gate extern int	modrootloaded;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * Global mutex:
945e3986cbScth  * Protects vHCI list and structure members.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate kmutex_t	mdi_mutex;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * Registered vHCI class driver lists
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate int		mdi_vhci_count;
1027c478bd9Sstevel@tonic-gate mdi_vhci_t	*mdi_vhci_head;
1037c478bd9Sstevel@tonic-gate mdi_vhci_t	*mdi_vhci_tail;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Client Hash Table size
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate static int	mdi_client_table_size = CLIENT_HASH_TABLE_SIZE;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * taskq interface definitions
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_N_THREADS	8
1147c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_PRI		minclsyspri
1157c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_MINALLOC	(4*mdi_taskq_n_threads)
1167c478bd9Sstevel@tonic-gate #define	MDI_TASKQ_MAXALLOC	(500*mdi_taskq_n_threads)
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate taskq_t				*mdi_taskq;
1197c478bd9Sstevel@tonic-gate static uint_t			mdi_taskq_n_threads = MDI_TASKQ_N_THREADS;
1207c478bd9Sstevel@tonic-gate 
1213c34adc5Sramat #define	TICKS_PER_SECOND	(drv_usectohz(1000000))
1223c34adc5Sramat 
1233c34adc5Sramat /*
1243c34adc5Sramat  * The data should be "quiet" for this interval (in seconds) before the
1253c34adc5Sramat  * vhci cached data is flushed to the disk.
1263c34adc5Sramat  */
1273c34adc5Sramat static int mdi_vhcache_flush_delay = 10;
1283c34adc5Sramat 
1293c34adc5Sramat /* number of seconds the vhcache flush daemon will sleep idle before exiting */
1303c34adc5Sramat static int mdi_vhcache_flush_daemon_idle_time = 60;
1313c34adc5Sramat 
13267e56d35Sramat /*
13367e56d35Sramat  * MDI falls back to discovery of all paths when a bus_config_one fails.
13467e56d35Sramat  * The following parameters can be used to tune this operation.
13567e56d35Sramat  *
13667e56d35Sramat  * mdi_path_discovery_boot
13767e56d35Sramat  *	Number of times path discovery will be attempted during early boot.
13867e56d35Sramat  *	Probably there is no reason to ever set this value to greater than one.
13967e56d35Sramat  *
14067e56d35Sramat  * mdi_path_discovery_postboot
14167e56d35Sramat  *	Number of times path discovery will be attempted after early boot.
14267e56d35Sramat  *	Set it to a minimum of two to allow for discovery of iscsi paths which
14367e56d35Sramat  *	may happen very late during booting.
14467e56d35Sramat  *
14567e56d35Sramat  * mdi_path_discovery_interval
14667e56d35Sramat  *	Minimum number of seconds MDI will wait between successive discovery
14767e56d35Sramat  *	of all paths. Set it to -1 to disable discovery of all paths.
14867e56d35Sramat  */
14967e56d35Sramat static int mdi_path_discovery_boot = 1;
15067e56d35Sramat static int mdi_path_discovery_postboot = 2;
15167e56d35Sramat static int mdi_path_discovery_interval = 10;
15267e56d35Sramat 
1537c478bd9Sstevel@tonic-gate /*
1543c34adc5Sramat  * number of seconds the asynchronous configuration thread will sleep idle
1553c34adc5Sramat  * before exiting.
1567c478bd9Sstevel@tonic-gate  */
1573c34adc5Sramat static int mdi_async_config_idle_time = 600;
1583c34adc5Sramat 
1593c34adc5Sramat static int mdi_bus_config_cache_hash_size = 256;
1603c34adc5Sramat 
1613c34adc5Sramat /* turns off multithreaded configuration for certain operations */
1623c34adc5Sramat static int mdi_mtc_off = 0;
1637c478bd9Sstevel@tonic-gate 
164602ca9eaScth /*
165602ca9eaScth  * The "path" to a pathinfo node is identical to the /devices path to a
166602ca9eaScth  * devinfo node had the device been enumerated under a pHCI instead of
167602ca9eaScth  * a vHCI.  This pathinfo "path" is associated with a 'path_instance'.
168602ca9eaScth  * This association persists across create/delete of the pathinfo nodes,
169602ca9eaScth  * but not across reboot.
170602ca9eaScth  */
171602ca9eaScth static uint_t		mdi_pathmap_instance = 1;	/* 0 -> any path */
172602ca9eaScth static int		mdi_pathmap_hash_size = 256;
173602ca9eaScth static kmutex_t		mdi_pathmap_mutex;
174602ca9eaScth static mod_hash_t	*mdi_pathmap_bypath;		/* "path"->instance */
175602ca9eaScth static mod_hash_t	*mdi_pathmap_byinstance;	/* instance->"path" */
1764c06356bSdh static mod_hash_t	*mdi_pathmap_sbyinstance;	/* inst->shortpath */
177602ca9eaScth 
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate  * MDI component property name/value string definitions
1807c478bd9Sstevel@tonic-gate  */
181d27aea3fSDan Cross const char		*mdi_component_prop = "mpxio-component";
1827c478bd9Sstevel@tonic-gate const char		*mdi_component_prop_vhci = "vhci";
1837c478bd9Sstevel@tonic-gate const char		*mdi_component_prop_phci = "phci";
1847c478bd9Sstevel@tonic-gate const char		*mdi_component_prop_client = "client";
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * MDI client global unique identifier property name
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate const char		*mdi_client_guid_prop = "client-guid";
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * MDI client load balancing property name/value string definitions
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate const char		*mdi_load_balance = "load-balance";
1957c478bd9Sstevel@tonic-gate const char		*mdi_load_balance_none = "none";
1967c478bd9Sstevel@tonic-gate const char		*mdi_load_balance_rr = "round-robin";
1977c478bd9Sstevel@tonic-gate const char		*mdi_load_balance_lba = "logical-block";
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate  * Obsolete vHCI class definition; to be removed after Leadville update
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate const char *mdi_vhci_class_scsi = MDI_HCI_CLASS_SCSI;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate static char vhci_greeting[] =
2057c478bd9Sstevel@tonic-gate 	"\tThere already exists one vHCI driver for class %s\n"
2067c478bd9Sstevel@tonic-gate 	"\tOnly one vHCI driver for each class is allowed\n";
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * Static function prototypes
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate static int		i_mdi_phci_offline(dev_info_t *, uint_t);
2127c478bd9Sstevel@tonic-gate static int		i_mdi_client_offline(dev_info_t *, uint_t);
2137c478bd9Sstevel@tonic-gate static int		i_mdi_phci_pre_detach(dev_info_t *, ddi_detach_cmd_t);
2147c478bd9Sstevel@tonic-gate static void		i_mdi_phci_post_detach(dev_info_t *,
2157c478bd9Sstevel@tonic-gate 			    ddi_detach_cmd_t, int);
2167c478bd9Sstevel@tonic-gate static int		i_mdi_client_pre_detach(dev_info_t *,
2177c478bd9Sstevel@tonic-gate 			    ddi_detach_cmd_t);
2187c478bd9Sstevel@tonic-gate static void		i_mdi_client_post_detach(dev_info_t *,
2197c478bd9Sstevel@tonic-gate 			    ddi_detach_cmd_t, int);
2207c478bd9Sstevel@tonic-gate static void		i_mdi_pm_hold_pip(mdi_pathinfo_t *);
2217c478bd9Sstevel@tonic-gate static void		i_mdi_pm_rele_pip(mdi_pathinfo_t *);
222d27aea3fSDan Cross static int		i_mdi_lba_lb(mdi_client_t *ct,
2237c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_t **ret_pip, struct buf *buf);
2247c478bd9Sstevel@tonic-gate static void		i_mdi_pm_hold_client(mdi_client_t *, int);
2257c478bd9Sstevel@tonic-gate static void		i_mdi_pm_rele_client(mdi_client_t *, int);
2267c478bd9Sstevel@tonic-gate static void		i_mdi_pm_reset_client(mdi_client_t *);
2277c478bd9Sstevel@tonic-gate static int		i_mdi_power_all_phci(mdi_client_t *);
2288c4f8890Srs static void		i_mdi_log_sysevent(dev_info_t *, char *, char *);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * Internal mdi_pathinfo node functions
2337c478bd9Sstevel@tonic-gate  */
2347c478bd9Sstevel@tonic-gate static void		i_mdi_pi_kstat_destroy(mdi_pathinfo_t *);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate static mdi_vhci_t	*i_mdi_vhci_class2vhci(char *);
2377c478bd9Sstevel@tonic-gate static mdi_vhci_t	*i_devi_get_vhci(dev_info_t *);
2387c478bd9Sstevel@tonic-gate static mdi_phci_t	*i_devi_get_phci(dev_info_t *);
2397c478bd9Sstevel@tonic-gate static void		i_mdi_phci_lock(mdi_phci_t *, mdi_pathinfo_t *);
2407c478bd9Sstevel@tonic-gate static void		i_mdi_phci_unlock(mdi_phci_t *);
2413c34adc5Sramat static mdi_pathinfo_t	*i_mdi_pi_alloc(mdi_phci_t *, char *, mdi_client_t *);
2427c478bd9Sstevel@tonic-gate static void		i_mdi_phci_add_path(mdi_phci_t *, mdi_pathinfo_t *);
2437c478bd9Sstevel@tonic-gate static void		i_mdi_client_add_path(mdi_client_t *, mdi_pathinfo_t *);
2447c478bd9Sstevel@tonic-gate static void		i_mdi_pi_free(mdi_phci_t *ph, mdi_pathinfo_t *,
2457c478bd9Sstevel@tonic-gate 			    mdi_client_t *);
2467c478bd9Sstevel@tonic-gate static void		i_mdi_phci_remove_path(mdi_phci_t *, mdi_pathinfo_t *);
2477c478bd9Sstevel@tonic-gate static void		i_mdi_client_remove_path(mdi_client_t *,
2487c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_t *);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate static int		i_mdi_pi_state_change(mdi_pathinfo_t *,
2517c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_state_t, int);
2527c478bd9Sstevel@tonic-gate static int		i_mdi_pi_offline(mdi_pathinfo_t *, int);
2537c478bd9Sstevel@tonic-gate static dev_info_t	*i_mdi_devinfo_create(mdi_vhci_t *, char *, char *,
2543c34adc5Sramat 			    char **, int);
2557c478bd9Sstevel@tonic-gate static dev_info_t	*i_mdi_devinfo_find(mdi_vhci_t *, char *, char *);
2567c478bd9Sstevel@tonic-gate static int		i_mdi_devinfo_remove(dev_info_t *, dev_info_t *, int);
2577c478bd9Sstevel@tonic-gate static int		i_mdi_is_child_present(dev_info_t *, dev_info_t *);
2583c34adc5Sramat static mdi_client_t	*i_mdi_client_alloc(mdi_vhci_t *, char *, char *);
2597c478bd9Sstevel@tonic-gate static void		i_mdi_client_enlist_table(mdi_vhci_t *, mdi_client_t *);
2607c478bd9Sstevel@tonic-gate static void		i_mdi_client_delist_table(mdi_vhci_t *, mdi_client_t *);
2613c34adc5Sramat static mdi_client_t	*i_mdi_client_find(mdi_vhci_t *, char *, char *);
2627c478bd9Sstevel@tonic-gate static void		i_mdi_client_update_state(mdi_client_t *);
2637c478bd9Sstevel@tonic-gate static int		i_mdi_client_compute_state(mdi_client_t *,
2647c478bd9Sstevel@tonic-gate 			    mdi_phci_t *);
2657c478bd9Sstevel@tonic-gate static void		i_mdi_client_lock(mdi_client_t *, mdi_pathinfo_t *);
2667c478bd9Sstevel@tonic-gate static void		i_mdi_client_unlock(mdi_client_t *);
2677c478bd9Sstevel@tonic-gate static int		i_mdi_client_free(mdi_vhci_t *, mdi_client_t *);
2687c478bd9Sstevel@tonic-gate static mdi_client_t	*i_devi_get_client(dev_info_t *);
269ee28b439Scm /*
270ee28b439Scm  * NOTE: this will be removed once the NWS files are changed to use the new
271ee28b439Scm  * mdi_{enable,disable}_path interfaces
272ee28b439Scm  */
273ee28b439Scm static int		i_mdi_pi_enable_disable(dev_info_t *, dev_info_t *,
274ee28b439Scm 				int, int);
275d27aea3fSDan Cross static mdi_pathinfo_t	*i_mdi_enable_disable_path(mdi_pathinfo_t *pip,
276ee28b439Scm 				mdi_vhci_t *vh, int flags, int op);
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * Failover related function prototypes
2797c478bd9Sstevel@tonic-gate  */
2807c478bd9Sstevel@tonic-gate static int		i_mdi_failover(void *);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate  * misc internal functions
2847c478bd9Sstevel@tonic-gate  */
2857c478bd9Sstevel@tonic-gate static int		i_mdi_get_hash_key(char *);
2867c478bd9Sstevel@tonic-gate static int		i_map_nvlist_error_to_mdi(int);
2877c478bd9Sstevel@tonic-gate static void		i_mdi_report_path_state(mdi_client_t *,
2887c478bd9Sstevel@tonic-gate 			    mdi_pathinfo_t *);
2897c478bd9Sstevel@tonic-gate 
2903c34adc5Sramat static void		setup_vhci_cache(mdi_vhci_t *);
2913c34adc5Sramat static int		destroy_vhci_cache(mdi_vhci_t *);
2923c34adc5Sramat static int		stop_vhcache_async_threads(mdi_vhci_config_t *);
2933c34adc5Sramat static boolean_t	stop_vhcache_flush_thread(void *, int);
2943c34adc5Sramat static void		free_string_array(char **, int);
2953c34adc5Sramat static void		free_vhcache_phci(mdi_vhcache_phci_t *);
2963c34adc5Sramat static void		free_vhcache_pathinfo(mdi_vhcache_pathinfo_t *);
2973c34adc5Sramat static void		free_vhcache_client(mdi_vhcache_client_t *);
2983c34adc5Sramat static int		mainnvl_to_vhcache(mdi_vhci_cache_t *, nvlist_t *);
2993c34adc5Sramat static nvlist_t		*vhcache_to_mainnvl(mdi_vhci_cache_t *);
3003c34adc5Sramat static void		vhcache_phci_add(mdi_vhci_config_t *, mdi_phci_t *);
3013c34adc5Sramat static void		vhcache_phci_remove(mdi_vhci_config_t *, mdi_phci_t *);
3023c34adc5Sramat static void		vhcache_pi_add(mdi_vhci_config_t *,
3033c34adc5Sramat 			    struct mdi_pathinfo *);
3043c34adc5Sramat static void		vhcache_pi_remove(mdi_vhci_config_t *,
3053c34adc5Sramat 			    struct mdi_pathinfo *);
3063c34adc5Sramat static void		free_phclient_path_list(mdi_phys_path_t *);
3073c34adc5Sramat static void		sort_vhcache_paths(mdi_vhcache_client_t *);
3083c34adc5Sramat static int		flush_vhcache(mdi_vhci_config_t *, int);
3093c34adc5Sramat static void		vhcache_dirty(mdi_vhci_config_t *);
3103c34adc5Sramat static void		free_async_client_config(mdi_async_client_config_t *);
31167e56d35Sramat static void		single_threaded_vhconfig_enter(mdi_vhci_config_t *);
31267e56d35Sramat static void		single_threaded_vhconfig_exit(mdi_vhci_config_t *);
3133c34adc5Sramat static nvlist_t		*read_on_disk_vhci_cache(char *);
3143c34adc5Sramat extern int		fread_nvlist(char *, nvlist_t **);
3153c34adc5Sramat extern int		fwrite_nvlist(char *, nvlist_t *);
3163c34adc5Sramat 
3177c478bd9Sstevel@tonic-gate /* called once when first vhci registers with mdi */
3187c478bd9Sstevel@tonic-gate static void
i_mdi_init()3197c478bd9Sstevel@tonic-gate i_mdi_init()
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	static int initialized = 0;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (initialized)
3247c478bd9Sstevel@tonic-gate 		return;
3257c478bd9Sstevel@tonic-gate 	initialized = 1;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	mutex_init(&mdi_mutex, NULL, MUTEX_DEFAULT, NULL);
328602ca9eaScth 
329602ca9eaScth 	/* Create our taskq resources */
3307c478bd9Sstevel@tonic-gate 	mdi_taskq = taskq_create("mdi_taskq", mdi_taskq_n_threads,
3317c478bd9Sstevel@tonic-gate 	    MDI_TASKQ_PRI, MDI_TASKQ_MINALLOC, MDI_TASKQ_MAXALLOC,
3327c478bd9Sstevel@tonic-gate 	    TASKQ_PREPOPULATE | TASKQ_CPR_SAFE);
3337c478bd9Sstevel@tonic-gate 	ASSERT(mdi_taskq != NULL);	/* taskq_create never fails */
334602ca9eaScth 
335602ca9eaScth 	/* Allocate ['path_instance' <-> "path"] maps */
336602ca9eaScth 	mutex_init(&mdi_pathmap_mutex, NULL, MUTEX_DRIVER, NULL);
337602ca9eaScth 	mdi_pathmap_bypath = mod_hash_create_strhash(
338602ca9eaScth 	    "mdi_pathmap_bypath", mdi_pathmap_hash_size,
339602ca9eaScth 	    mod_hash_null_valdtor);
340602ca9eaScth 	mdi_pathmap_byinstance = mod_hash_create_idhash(
341602ca9eaScth 	    "mdi_pathmap_byinstance", mdi_pathmap_hash_size,
342602ca9eaScth 	    mod_hash_null_valdtor);
3434c06356bSdh 	mdi_pathmap_sbyinstance = mod_hash_create_idhash(
3444c06356bSdh 	    "mdi_pathmap_sbyinstance", mdi_pathmap_hash_size,
3454c06356bSdh 	    mod_hash_null_valdtor);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate  * mdi_get_component_type():
3507c478bd9Sstevel@tonic-gate  *		Return mpxio component type
3517c478bd9Sstevel@tonic-gate  * Return Values:
3527c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_NONE
3537c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_VHCI
3547c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_PHCI
3557c478bd9Sstevel@tonic-gate  *		MDI_COMPONENT_CLIENT
3567c478bd9Sstevel@tonic-gate  * XXX This doesn't work under multi-level MPxIO and should be
3575e3986cbScth  *	removed when clients migrate mdi_component_is_*() interfaces.
3587c478bd9Sstevel@tonic-gate  */
3597c478bd9Sstevel@tonic-gate int
mdi_get_component_type(dev_info_t * dip)3607c478bd9Sstevel@tonic-gate mdi_get_component_type(dev_info_t *dip)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_mdi_component);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * mdi_vhci_register():
3677c478bd9Sstevel@tonic-gate  *		Register a vHCI module with the mpxio framework
3687c478bd9Sstevel@tonic-gate  *		mdi_vhci_register() is called by vHCI drivers to register the
3697c478bd9Sstevel@tonic-gate  *		'class_driver' vHCI driver and its MDI entrypoints with the
3707c478bd9Sstevel@tonic-gate  *		mpxio framework.  The vHCI driver must call this interface as
3717c478bd9Sstevel@tonic-gate  *		part of its attach(9e) handler.
3727c478bd9Sstevel@tonic-gate  *		Competing threads may try to attach mdi_vhci_register() as
3737c478bd9Sstevel@tonic-gate  *		the vHCI drivers are loaded and attached as a result of pHCI
3747c478bd9Sstevel@tonic-gate  *		driver instance registration (mdi_phci_register()) with the
3757c478bd9Sstevel@tonic-gate  *		framework.
3767c478bd9Sstevel@tonic-gate  * Return Values:
3777c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
3787c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3817c478bd9Sstevel@tonic-gate int
mdi_vhci_register(char * class,dev_info_t * vdip,mdi_vhci_ops_t * vops,int flags)3827c478bd9Sstevel@tonic-gate mdi_vhci_register(char *class, dev_info_t *vdip, mdi_vhci_ops_t *vops,
3837c478bd9Sstevel@tonic-gate     int flags)
3847c478bd9Sstevel@tonic-gate {
3857c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh = NULL;
3867c478bd9Sstevel@tonic-gate 
38755e592a2SRandall Ralphs 	/* Registrant can't be older */
38855e592a2SRandall Ralphs 	ASSERT(vops->vo_revision <= MDI_VHCI_OPS_REV);
38955e592a2SRandall Ralphs 
39000a3eaf3SRamaswamy Tummala #ifdef DEBUG
39100a3eaf3SRamaswamy Tummala 	/*
39200a3eaf3SRamaswamy Tummala 	 * IB nexus driver is loaded only when IB hardware is present.
39300a3eaf3SRamaswamy Tummala 	 * In order to be able to do this there is a need to drive the loading
39400a3eaf3SRamaswamy Tummala 	 * and attaching of the IB nexus driver (especially when an IB hardware
39500a3eaf3SRamaswamy Tummala 	 * is dynamically plugged in) when an IB HCA driver (PHCI)
39600a3eaf3SRamaswamy Tummala 	 * is being attached. Unfortunately this gets into the limitations
39700a3eaf3SRamaswamy Tummala 	 * of devfs as there seems to be no clean way to drive configuration
39800a3eaf3SRamaswamy Tummala 	 * of a subtree from another subtree of a devfs. Hence, do not ASSERT
39900a3eaf3SRamaswamy Tummala 	 * for IB.
40000a3eaf3SRamaswamy Tummala 	 */
40100a3eaf3SRamaswamy Tummala 	if (strcmp(class, MDI_HCI_CLASS_IB) != 0)
40200a3eaf3SRamaswamy Tummala 		ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(vdip)));
40300a3eaf3SRamaswamy Tummala #endif
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	i_mdi_init();
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	mutex_enter(&mdi_mutex);
4087c478bd9Sstevel@tonic-gate 	/*
4097c478bd9Sstevel@tonic-gate 	 * Scan for already registered vhci
4107c478bd9Sstevel@tonic-gate 	 */
4117c478bd9Sstevel@tonic-gate 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
4127c478bd9Sstevel@tonic-gate 		if (strcmp(vh->vh_class, class) == 0) {
4137c478bd9Sstevel@tonic-gate 			/*
4147c478bd9Sstevel@tonic-gate 			 * vHCI has already been created.  Check for valid
4157c478bd9Sstevel@tonic-gate 			 * vHCI ops registration.  We only support one vHCI
4167c478bd9Sstevel@tonic-gate 			 * module per class
4177c478bd9Sstevel@tonic-gate 			 */
4187c478bd9Sstevel@tonic-gate 			if (vh->vh_ops != NULL) {
4197c478bd9Sstevel@tonic-gate 				mutex_exit(&mdi_mutex);
4207c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, vhci_greeting, class);
4217c478bd9Sstevel@tonic-gate 				return (MDI_FAILURE);
4227c478bd9Sstevel@tonic-gate 			}
4237c478bd9Sstevel@tonic-gate 			break;
4247c478bd9Sstevel@tonic-gate 		}
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	/*
4287c478bd9Sstevel@tonic-gate 	 * if not yet created, create the vHCI component
4297c478bd9Sstevel@tonic-gate 	 */
4307c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
4317c478bd9Sstevel@tonic-gate 		struct client_hash	*hash = NULL;
4327c478bd9Sstevel@tonic-gate 		char			*load_balance;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		/*
4357c478bd9Sstevel@tonic-gate 		 * Allocate and initialize the mdi extensions
4367c478bd9Sstevel@tonic-gate 		 */
4377c478bd9Sstevel@tonic-gate 		vh = kmem_zalloc(sizeof (mdi_vhci_t), KM_SLEEP);
4387c478bd9Sstevel@tonic-gate 		hash = kmem_zalloc(mdi_client_table_size * sizeof (*hash),
4397c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
4407c478bd9Sstevel@tonic-gate 		vh->vh_client_table = hash;
4417c478bd9Sstevel@tonic-gate 		vh->vh_class = kmem_zalloc(strlen(class) + 1, KM_SLEEP);
4427c478bd9Sstevel@tonic-gate 		(void) strcpy(vh->vh_class, class);
4437c478bd9Sstevel@tonic-gate 		vh->vh_lb = LOAD_BALANCE_RR;
4447c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, vdip,
4457c478bd9Sstevel@tonic-gate 		    0, LOAD_BALANCE_PROP, &load_balance) == DDI_SUCCESS) {
4467c478bd9Sstevel@tonic-gate 			if (strcmp(load_balance, LOAD_BALANCE_PROP_NONE) == 0) {
4477c478bd9Sstevel@tonic-gate 				vh->vh_lb = LOAD_BALANCE_NONE;
4487c478bd9Sstevel@tonic-gate 			} else if (strcmp(load_balance, LOAD_BALANCE_PROP_LBA)
4497c478bd9Sstevel@tonic-gate 				    == 0) {
4507c478bd9Sstevel@tonic-gate 				vh->vh_lb = LOAD_BALANCE_LBA;
4517c478bd9Sstevel@tonic-gate 			}
4527c478bd9Sstevel@tonic-gate 			ddi_prop_free(load_balance);
4537c478bd9Sstevel@tonic-gate 		}
4547c478bd9Sstevel@tonic-gate 
4555e3986cbScth 		mutex_init(&vh->vh_phci_mutex, NULL, MUTEX_DEFAULT, NULL);
4565e3986cbScth 		mutex_init(&vh->vh_client_mutex, NULL, MUTEX_DEFAULT, NULL);
4575e3986cbScth 
4587c478bd9Sstevel@tonic-gate 		/*
4597c478bd9Sstevel@tonic-gate 		 * Store the vHCI ops vectors
4607c478bd9Sstevel@tonic-gate 		 */
4617c478bd9Sstevel@tonic-gate 		vh->vh_dip = vdip;
4627c478bd9Sstevel@tonic-gate 		vh->vh_ops = vops;
4637c478bd9Sstevel@tonic-gate 
4643c34adc5Sramat 		setup_vhci_cache(vh);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		if (mdi_vhci_head == NULL) {
4677c478bd9Sstevel@tonic-gate 			mdi_vhci_head = vh;
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 		if (mdi_vhci_tail) {
4707c478bd9Sstevel@tonic-gate 			mdi_vhci_tail->vh_next = vh;
4717c478bd9Sstevel@tonic-gate 		}
4727c478bd9Sstevel@tonic-gate 		mdi_vhci_tail = vh;
4737c478bd9Sstevel@tonic-gate 		mdi_vhci_count++;
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	/*
4777c478bd9Sstevel@tonic-gate 	 * Claim the devfs node as a vhci component
4787c478bd9Sstevel@tonic-gate 	 */
4797c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_component |= MDI_COMPONENT_VHCI;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	/*
4827c478bd9Sstevel@tonic-gate 	 * Initialize our back reference from dev_info node
4837c478bd9Sstevel@tonic-gate 	 */
4847c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_xhci = (caddr_t)vh;
4857c478bd9Sstevel@tonic-gate 	mutex_exit(&mdi_mutex);
4867c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate  * mdi_vhci_unregister():
4917c478bd9Sstevel@tonic-gate  *		Unregister a vHCI module from mpxio framework
4927c478bd9Sstevel@tonic-gate  *		mdi_vhci_unregister() is called from the detach(9E) entrypoint
493d27aea3fSDan Cross  *		of a vhci to unregister it from the framework.
4947c478bd9Sstevel@tonic-gate  * Return Values:
4957c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
4967c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
4977c478bd9Sstevel@tonic-gate  */
4987c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4997c478bd9Sstevel@tonic-gate int
mdi_vhci_unregister(dev_info_t * vdip,int flags)5007c478bd9Sstevel@tonic-gate mdi_vhci_unregister(dev_info_t *vdip, int flags)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*found, *vh, *prev = NULL;
5037c478bd9Sstevel@tonic-gate 
5045e3986cbScth 	ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(vdip)));
5055e3986cbScth 
5067c478bd9Sstevel@tonic-gate 	/*
5077c478bd9Sstevel@tonic-gate 	 * Check for invalid VHCI
5087c478bd9Sstevel@tonic-gate 	 */
5097c478bd9Sstevel@tonic-gate 	if ((vh = i_devi_get_vhci(vdip)) == NULL)
5107c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/*
5137c478bd9Sstevel@tonic-gate 	 * Scan the list of registered vHCIs for a match
5147c478bd9Sstevel@tonic-gate 	 */
5155e3986cbScth 	mutex_enter(&mdi_mutex);
5167c478bd9Sstevel@tonic-gate 	for (found = mdi_vhci_head; found != NULL; found = found->vh_next) {
5177c478bd9Sstevel@tonic-gate 		if (found == vh)
5187c478bd9Sstevel@tonic-gate 			break;
5197c478bd9Sstevel@tonic-gate 		prev = found;
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (found == NULL) {
5237c478bd9Sstevel@tonic-gate 		mutex_exit(&mdi_mutex);
5247c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	/*
5288c4f8890Srs 	 * Check the vHCI, pHCI and client count. All the pHCIs and clients
5297c478bd9Sstevel@tonic-gate 	 * should have been unregistered, before a vHCI can be
5307c478bd9Sstevel@tonic-gate 	 * unregistered.
5317c478bd9Sstevel@tonic-gate 	 */
5325e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
5335e3986cbScth 	if (vh->vh_refcnt || vh->vh_phci_count || vh->vh_client_count) {
5345e3986cbScth 		MDI_VHCI_PHCI_UNLOCK(vh);
5355e3986cbScth 		mutex_exit(&mdi_mutex);
5365e3986cbScth 		return (MDI_FAILURE);
5375e3986cbScth 	}
5385e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
5395e3986cbScth 
5405e3986cbScth 	if (destroy_vhci_cache(vh) != MDI_SUCCESS) {
5417c478bd9Sstevel@tonic-gate 		mutex_exit(&mdi_mutex);
5427c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/*
5467c478bd9Sstevel@tonic-gate 	 * Remove the vHCI from the global list
5477c478bd9Sstevel@tonic-gate 	 */
5487c478bd9Sstevel@tonic-gate 	if (vh == mdi_vhci_head) {
5497c478bd9Sstevel@tonic-gate 		mdi_vhci_head = vh->vh_next;
5507c478bd9Sstevel@tonic-gate 	} else {
5517c478bd9Sstevel@tonic-gate 		prev->vh_next = vh->vh_next;
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	if (vh == mdi_vhci_tail) {
5547c478bd9Sstevel@tonic-gate 		mdi_vhci_tail = prev;
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate 	mdi_vhci_count--;
5577c478bd9Sstevel@tonic-gate 	mutex_exit(&mdi_mutex);
5583c34adc5Sramat 
5593c34adc5Sramat 	vh->vh_ops = NULL;
5607c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_component &= ~MDI_COMPONENT_VHCI;
5617c478bd9Sstevel@tonic-gate 	DEVI(vdip)->devi_mdi_xhci = NULL;
5627c478bd9Sstevel@tonic-gate 	kmem_free(vh->vh_class, strlen(vh->vh_class)+1);
5637c478bd9Sstevel@tonic-gate 	kmem_free(vh->vh_client_table,
5647c478bd9Sstevel@tonic-gate 	    mdi_client_table_size * sizeof (struct client_hash));
5655e3986cbScth 	mutex_destroy(&vh->vh_phci_mutex);
5665e3986cbScth 	mutex_destroy(&vh->vh_client_mutex);
56778dc6db2Sllai 
5687c478bd9Sstevel@tonic-gate 	kmem_free(vh, sizeof (mdi_vhci_t));
5697c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /*
5737c478bd9Sstevel@tonic-gate  * i_mdi_vhci_class2vhci():
5747c478bd9Sstevel@tonic-gate  *		Look for a matching vHCI module given a vHCI class name
5757c478bd9Sstevel@tonic-gate  * Return Values:
5767c478bd9Sstevel@tonic-gate  *		Handle to a vHCI component
5777c478bd9Sstevel@tonic-gate  *		NULL
5787c478bd9Sstevel@tonic-gate  */
5797c478bd9Sstevel@tonic-gate static mdi_vhci_t *
i_mdi_vhci_class2vhci(char * class)5807c478bd9Sstevel@tonic-gate i_mdi_vhci_class2vhci(char *class)
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&mdi_mutex));
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	mutex_enter(&mdi_mutex);
5877c478bd9Sstevel@tonic-gate 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
5887c478bd9Sstevel@tonic-gate 		if (strcmp(vh->vh_class, class) == 0) {
5897c478bd9Sstevel@tonic-gate 			break;
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 	mutex_exit(&mdi_mutex);
5937c478bd9Sstevel@tonic-gate 	return (vh);
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate /*
5977c478bd9Sstevel@tonic-gate  * i_devi_get_vhci():
5987c478bd9Sstevel@tonic-gate  *		Utility function to get the handle to a vHCI component
5997c478bd9Sstevel@tonic-gate  * Return Values:
6007c478bd9Sstevel@tonic-gate  *		Handle to a vHCI component
6017c478bd9Sstevel@tonic-gate  *		NULL
6027c478bd9Sstevel@tonic-gate  */
6037c478bd9Sstevel@tonic-gate mdi_vhci_t *
i_devi_get_vhci(dev_info_t * vdip)6047c478bd9Sstevel@tonic-gate i_devi_get_vhci(dev_info_t *vdip)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
6077c478bd9Sstevel@tonic-gate 	if (MDI_VHCI(vdip)) {
6087c478bd9Sstevel@tonic-gate 		vh = (mdi_vhci_t *)DEVI(vdip)->devi_mdi_xhci;
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 	return (vh);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate /*
6147c478bd9Sstevel@tonic-gate  * mdi_phci_register():
6157c478bd9Sstevel@tonic-gate  *		Register a pHCI module with mpxio framework
6167c478bd9Sstevel@tonic-gate  *		mdi_phci_register() is called by pHCI drivers to register with
6177c478bd9Sstevel@tonic-gate  *		the mpxio framework and a specific 'class_driver' vHCI.  The
6187c478bd9Sstevel@tonic-gate  *		pHCI driver must call this interface as part of its attach(9e)
6197c478bd9Sstevel@tonic-gate  *		handler.
6207c478bd9Sstevel@tonic-gate  * Return Values:
6217c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
6227c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
6237c478bd9Sstevel@tonic-gate  */
6247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6257c478bd9Sstevel@tonic-gate int
mdi_phci_register(char * class,dev_info_t * pdip,int flags)6267c478bd9Sstevel@tonic-gate mdi_phci_register(char *class, dev_info_t *pdip, int flags)
6277c478bd9Sstevel@tonic-gate {
6287c478bd9Sstevel@tonic-gate 	mdi_phci_t		*ph;
6297c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh;
6307c478bd9Sstevel@tonic-gate 	char			*data;
6317c478bd9Sstevel@tonic-gate 
6325e3986cbScth 	/*
6335e3986cbScth 	 * Some subsystems, like fcp, perform pHCI registration from a
6345e3986cbScth 	 * different thread than the one doing the pHCI attach(9E) - the
6355e3986cbScth 	 * driver attach code is waiting for this other thread to complete.
6365e3986cbScth 	 * This means we can only ASSERT DEVI_BUSY_CHANGING of parent
6375e3986cbScth 	 * (indicating that some thread has done an ndi_devi_enter of parent)
6385e3986cbScth 	 * not DEVI_BUSY_OWNED (which would indicate that we did the enter).
6395e3986cbScth 	 */
6405e3986cbScth 	ASSERT(DEVI_BUSY_CHANGING(ddi_get_parent(pdip)));
6415e3986cbScth 
6427c478bd9Sstevel@tonic-gate 	/*
6437c478bd9Sstevel@tonic-gate 	 * Check for mpxio-disable property. Enable mpxio if the property is
6447c478bd9Sstevel@tonic-gate 	 * missing or not set to "yes".
6457c478bd9Sstevel@tonic-gate 	 * If the property is set to "yes" then emit a brief message.
6467c478bd9Sstevel@tonic-gate 	 */
6477c478bd9Sstevel@tonic-gate 	if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip, 0, "mpxio-disable",
6487c478bd9Sstevel@tonic-gate 	    &data) == DDI_SUCCESS)) {
6497c478bd9Sstevel@tonic-gate 		if (strcmp(data, "yes") == 0) {
6504c06356bSdh 			MDI_DEBUG(1, (MDI_CONT, pdip,
6514c06356bSdh 			    "?multipath capabilities disabled via %s.conf.",
6527c478bd9Sstevel@tonic-gate 			    ddi_driver_name(pdip)));
6537c478bd9Sstevel@tonic-gate 			ddi_prop_free(data);
6547c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
6557c478bd9Sstevel@tonic-gate 		}
6567c478bd9Sstevel@tonic-gate 		ddi_prop_free(data);
6577c478bd9Sstevel@tonic-gate 	}
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	/*
6607c478bd9Sstevel@tonic-gate 	 * Search for a matching vHCI
6617c478bd9Sstevel@tonic-gate 	 */
6627c478bd9Sstevel@tonic-gate 	vh = (mdi_vhci_t *)i_mdi_vhci_class2vhci(class);
6637c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
6647c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
6657c478bd9Sstevel@tonic-gate 	}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	ph = kmem_zalloc(sizeof (mdi_phci_t), KM_SLEEP);
6687c478bd9Sstevel@tonic-gate 	mutex_init(&ph->ph_mutex, NULL, MUTEX_DEFAULT, NULL);
6697c478bd9Sstevel@tonic-gate 	ph->ph_dip = pdip;
6707c478bd9Sstevel@tonic-gate 	ph->ph_vhci = vh;
6717c478bd9Sstevel@tonic-gate 	ph->ph_next = NULL;
6727c478bd9Sstevel@tonic-gate 	ph->ph_unstable = 0;
6737c478bd9Sstevel@tonic-gate 	ph->ph_vprivate = 0;
6747c478bd9Sstevel@tonic-gate 	cv_init(&ph->ph_unstable_cv, NULL, CV_DRIVER, NULL);
6757c478bd9Sstevel@tonic-gate 
6765e3986cbScth 	MDI_PHCI_LOCK(ph);
6777c478bd9Sstevel@tonic-gate 	MDI_PHCI_SET_POWER_UP(ph);
6785e3986cbScth 	MDI_PHCI_UNLOCK(ph);
6797c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_component |= MDI_COMPONENT_PHCI;
6807c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_xhci = (caddr_t)ph;
6817c478bd9Sstevel@tonic-gate 
6823c34adc5Sramat 	vhcache_phci_add(vh->vh_config, ph);
6833c34adc5Sramat 
6845e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
6857c478bd9Sstevel@tonic-gate 	if (vh->vh_phci_head == NULL) {
6867c478bd9Sstevel@tonic-gate 		vh->vh_phci_head = ph;
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 	if (vh->vh_phci_tail) {
6897c478bd9Sstevel@tonic-gate 		vh->vh_phci_tail->ph_next = ph;
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 	vh->vh_phci_tail = ph;
6927c478bd9Sstevel@tonic-gate 	vh->vh_phci_count++;
6935e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
6945e3986cbScth 
6958c4f8890Srs 	i_mdi_log_sysevent(pdip, class, ESC_DDI_INITIATOR_REGISTER);
6967c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate /*
7007c478bd9Sstevel@tonic-gate  * mdi_phci_unregister():
7017c478bd9Sstevel@tonic-gate  *		Unregister a pHCI module from mpxio framework
7027c478bd9Sstevel@tonic-gate  *		mdi_phci_unregister() is called by the pHCI drivers from their
7037c478bd9Sstevel@tonic-gate  *		detach(9E) handler to unregister their instances from the
7047c478bd9Sstevel@tonic-gate  *		framework.
7057c478bd9Sstevel@tonic-gate  * Return Values:
7067c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
7077c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
7087c478bd9Sstevel@tonic-gate  */
7097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7107c478bd9Sstevel@tonic-gate int
mdi_phci_unregister(dev_info_t * pdip,int flags)7117c478bd9Sstevel@tonic-gate mdi_phci_unregister(dev_info_t *pdip, int flags)
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh;
7147c478bd9Sstevel@tonic-gate 	mdi_phci_t		*ph;
7157c478bd9Sstevel@tonic-gate 	mdi_phci_t		*tmp;
7167c478bd9Sstevel@tonic-gate 	mdi_phci_t		*prev = NULL;
7174c06356bSdh 	mdi_pathinfo_t		*pip;
7187c478bd9Sstevel@tonic-gate 
7195e3986cbScth 	ASSERT(DEVI_BUSY_CHANGING(ddi_get_parent(pdip)));
7205e3986cbScth 
7217c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
7227c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
7234c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, pdip, "!not a valid pHCI"));
7247c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
7287c478bd9Sstevel@tonic-gate 	ASSERT(vh != NULL);
7297c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
7304c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, pdip, "!not a valid vHCI"));
7317c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 
7345e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
7357c478bd9Sstevel@tonic-gate 	tmp = vh->vh_phci_head;
7367c478bd9Sstevel@tonic-gate 	while (tmp) {
7377c478bd9Sstevel@tonic-gate 		if (tmp == ph) {
7387c478bd9Sstevel@tonic-gate 			break;
7397c478bd9Sstevel@tonic-gate 		}
7407c478bd9Sstevel@tonic-gate 		prev = tmp;
7417c478bd9Sstevel@tonic-gate 		tmp = tmp->ph_next;
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	if (ph == vh->vh_phci_head) {
7457c478bd9Sstevel@tonic-gate 		vh->vh_phci_head = ph->ph_next;
7467c478bd9Sstevel@tonic-gate 	} else {
7477c478bd9Sstevel@tonic-gate 		prev->ph_next = ph->ph_next;
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	if (ph == vh->vh_phci_tail) {
7517c478bd9Sstevel@tonic-gate 		vh->vh_phci_tail = prev;
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	vh->vh_phci_count--;
7555e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
7567c478bd9Sstevel@tonic-gate 
7574c06356bSdh 	/* Walk remaining pathinfo nodes and disassociate them from pHCI */
7584c06356bSdh 	MDI_PHCI_LOCK(ph);
7594c06356bSdh 	for (pip = (mdi_pathinfo_t *)ph->ph_path_head; pip;
7604c06356bSdh 	    pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link)
7614c06356bSdh 		MDI_PI(pip)->pi_phci = NULL;
7624c06356bSdh 	MDI_PHCI_UNLOCK(ph);
7634c06356bSdh 
7648c4f8890Srs 	i_mdi_log_sysevent(pdip, ph->ph_vhci->vh_class,
7658c4f8890Srs 	    ESC_DDI_INITIATOR_UNREGISTER);
7663c34adc5Sramat 	vhcache_phci_remove(vh->vh_config, ph);
7677c478bd9Sstevel@tonic-gate 	cv_destroy(&ph->ph_unstable_cv);
7687c478bd9Sstevel@tonic-gate 	mutex_destroy(&ph->ph_mutex);
7697c478bd9Sstevel@tonic-gate 	kmem_free(ph, sizeof (mdi_phci_t));
7707c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_component &= ~MDI_COMPONENT_PHCI;
7717c478bd9Sstevel@tonic-gate 	DEVI(pdip)->devi_mdi_xhci = NULL;
7727c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate /*
7767c478bd9Sstevel@tonic-gate  * i_devi_get_phci():
777d27aea3fSDan Cross  *		Utility function to return the phci extensions.
7787c478bd9Sstevel@tonic-gate  */
7797c478bd9Sstevel@tonic-gate static mdi_phci_t *
i_devi_get_phci(dev_info_t * pdip)7807c478bd9Sstevel@tonic-gate i_devi_get_phci(dev_info_t *pdip)
7817c478bd9Sstevel@tonic-gate {
7827c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph = NULL;
78355e592a2SRandall Ralphs 
7847c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(pdip)) {
7857c478bd9Sstevel@tonic-gate 		ph = (mdi_phci_t *)DEVI(pdip)->devi_mdi_xhci;
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 	return (ph);
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7905e3986cbScth /*
7915e3986cbScth  * Single thread mdi entry into devinfo node for modifying its children.
7925e3986cbScth  * If necessary we perform an ndi_devi_enter of the vHCI before doing
793d27aea3fSDan Cross  * an ndi_devi_enter of 'dip'.  If we enter the vHCI, we set *enteredvp
7943fe80ca4SDan Cross  * to true, otherwise it is unconditionally set to false.
7955e3986cbScth  */
7965e3986cbScth void
mdi_devi_enter(dev_info_t * phci_dip,boolean_t * enteredvp)7973fe80ca4SDan Cross mdi_devi_enter(dev_info_t *phci_dip, boolean_t *enteredvp)
7985e3986cbScth {
7995e3986cbScth 	dev_info_t	*vdip;
8005e3986cbScth 
8015e3986cbScth 	/* Verify calling context */
802d27aea3fSDan Cross 	ASSERT3P(enteredvp, !=, NULL);
8035e3986cbScth 	ASSERT(MDI_PHCI(phci_dip));
8045e3986cbScth 	vdip = mdi_devi_get_vdip(phci_dip);
8053fe80ca4SDan Cross 	ASSERT3P(vdip, !=, NULL);	/* A pHCI always has a vHCI */
8065e3986cbScth 
8075e3986cbScth 	/*
8085e3986cbScth 	 * If pHCI is detaching then the framework has already entered the
809d27aea3fSDan Cross 	 * vHCI on a thread that went down the code path leading to
8105e3986cbScth 	 * detach_node().  This framework enter of the vHCI during pHCI
8115e3986cbScth 	 * detach is done to avoid deadlock with vHCI power management
812d27aea3fSDan Cross 	 * operations which enter the vHCI and then enter down the path
813d27aea3fSDan Cross 	 * to the pHCI.  If pHCI is detaching then we piggyback this call's
814d27aea3fSDan Cross 	 * enter of the vHCI on the framework's vHCI enter that has already
8155e3986cbScth 	 * occurred - this is OK because we know that the framework thread
8165e3986cbScth 	 * doing detach is waiting for our completion.
8175e3986cbScth 	 *
818d27aea3fSDan Cross 	 * We should check DEVI_IS_DETACHING under an enter of the parent to
819d27aea3fSDan Cross 	 * avoid a race with detach, but we can't because the framework has
820d27aea3fSDan Cross 	 * already entered the parent, so we have this complexity instead.
8215e3986cbScth 	 */
8223fe80ca4SDan Cross 	*enteredvp = B_FALSE;
8235e3986cbScth 	for (;;) {
824d27aea3fSDan Cross 		if (panicstr != NULL)
825d27aea3fSDan Cross 			return;
826d27aea3fSDan Cross 
8273fe80ca4SDan Cross 		if (ndi_devi_tryenter(vdip)) {
8283fe80ca4SDan Cross 			*enteredvp = B_TRUE;
8295e3986cbScth 			if (DEVI_IS_DETACHING(phci_dip)) {
8303fe80ca4SDan Cross 				ndi_devi_exit(vdip);
8313fe80ca4SDan Cross 				*enteredvp = B_FALSE;
8325e3986cbScth 			}
8335e3986cbScth 			break;
8345e3986cbScth 		} else if (DEVI_IS_DETACHING(phci_dip)) {
8353fe80ca4SDan Cross 			*enteredvp = B_FALSE;
8365e3986cbScth 			break;
8374c06356bSdh 		} else if (servicing_interrupt()) {
8384c06356bSdh 			/*
8394c06356bSdh 			 * Don't delay an interrupt (and ensure adaptive
8404c06356bSdh 			 * mutex inversion support).
8414c06356bSdh 			 */
8423fe80ca4SDan Cross 			ndi_devi_enter(vdip);
8433fe80ca4SDan Cross 			*enteredvp = B_TRUE;
8444c06356bSdh 			break;
8455e3986cbScth 		} else {
84696c4a178SChris Horne 			delay_random(mdi_delay);
8475e3986cbScth 		}
8485e3986cbScth 	}
8495e3986cbScth 
8503fe80ca4SDan Cross 	ndi_devi_enter(phci_dip);
8515e3986cbScth }
8525e3986cbScth 
85355e592a2SRandall Ralphs /*
85455e592a2SRandall Ralphs  * Attempt to mdi_devi_enter.
85555e592a2SRandall Ralphs  */
85655e592a2SRandall Ralphs int
mdi_devi_tryenter(dev_info_t * phci_dip,boolean_t * enteredvp)8573fe80ca4SDan Cross mdi_devi_tryenter(dev_info_t *phci_dip, boolean_t *enteredvp)
85855e592a2SRandall Ralphs {
85955e592a2SRandall Ralphs 	dev_info_t	*vdip;
86055e592a2SRandall Ralphs 
86155e592a2SRandall Ralphs 	/* Verify calling context */
86255e592a2SRandall Ralphs 	ASSERT(MDI_PHCI(phci_dip));
86355e592a2SRandall Ralphs 	vdip = mdi_devi_get_vdip(phci_dip);
8643fe80ca4SDan Cross 	ASSERT3P(vdip, !=, NULL);	/* A pHCI always has a vHCI */
86555e592a2SRandall Ralphs 
8663fe80ca4SDan Cross 	*enteredvp = B_FALSE;
8673fe80ca4SDan Cross 	if (ndi_devi_tryenter(vdip)) {
8683fe80ca4SDan Cross 		if (ndi_devi_tryenter(phci_dip)) {
8693fe80ca4SDan Cross 			*enteredvp = B_TRUE;
87055e592a2SRandall Ralphs 			return (1);	/* locked */
87155e592a2SRandall Ralphs 		}
8723fe80ca4SDan Cross 		ndi_devi_exit(vdip);
87355e592a2SRandall Ralphs 	}
87455e592a2SRandall Ralphs 	return (0);			/* busy */
87555e592a2SRandall Ralphs }
87655e592a2SRandall Ralphs 
8775e3986cbScth /*
8785e3986cbScth  * Release mdi_devi_enter or successful mdi_devi_tryenter.
8795e3986cbScth  */
8805e3986cbScth void
mdi_devi_exit(dev_info_t * phci_dip,boolean_t enteredv)8813fe80ca4SDan Cross mdi_devi_exit(dev_info_t *phci_dip, boolean_t enteredv)
8825e3986cbScth {
8835e3986cbScth 	dev_info_t	*vdip;
8845e3986cbScth 
8855e3986cbScth 	/* Verify calling context */
8865e3986cbScth 	ASSERT(MDI_PHCI(phci_dip));
8875e3986cbScth 	vdip = mdi_devi_get_vdip(phci_dip);
8883fe80ca4SDan Cross 	ASSERT3P(vdip, !=, NULL);	/* A pHCI always has a vHCI */
8895e3986cbScth 
8903fe80ca4SDan Cross 	ndi_devi_exit(phci_dip);
8913fe80ca4SDan Cross 	if (enteredv)
8923fe80ca4SDan Cross 		ndi_devi_exit(vdip);
8935e3986cbScth }
8945e3986cbScth 
8955e3986cbScth /*
8965e3986cbScth  * The functions mdi_devi_exit_phci() and mdi_devi_enter_phci() are used
8975e3986cbScth  * around a pHCI drivers calls to mdi_pi_online/offline, after holding
8985e3986cbScth  * the pathinfo node via mdi_hold_path/mdi_rele_path, to avoid deadlock
8995e3986cbScth  * with vHCI power management code during path online/offline.  Each
9005e3986cbScth  * mdi_devi_exit_phci must have a matching mdi_devi_enter_phci, and both must
9015e3986cbScth  * occur within the scope of an active mdi_devi_enter that establishes the
9025e3986cbScth  * circular value.
9035e3986cbScth  */
9045e3986cbScth void
mdi_devi_exit_phci(dev_info_t * phci_dip)9053fe80ca4SDan Cross mdi_devi_exit_phci(dev_info_t *phci_dip)
9065e3986cbScth {
9075e3986cbScth 	/* Verify calling context */
9085e3986cbScth 	ASSERT(MDI_PHCI(phci_dip));
9095e3986cbScth 
9104c06356bSdh 	/* Keep hold on pHCI until we reenter in mdi_devi_enter_phci */
9114c06356bSdh 	ndi_hold_devi(phci_dip);
9124c06356bSdh 
9133fe80ca4SDan Cross 	ndi_devi_exit(phci_dip);
9145e3986cbScth }
9155e3986cbScth 
9165e3986cbScth void
mdi_devi_enter_phci(dev_info_t * phci_dip)9173fe80ca4SDan Cross mdi_devi_enter_phci(dev_info_t *phci_dip)
9185e3986cbScth {
9195e3986cbScth 	/* Verify calling context */
9205e3986cbScth 	ASSERT(MDI_PHCI(phci_dip));
9215e3986cbScth 
9223fe80ca4SDan Cross 	ndi_devi_enter(phci_dip);
9235e3986cbScth 
9244c06356bSdh 	/* Drop hold from mdi_devi_exit_phci. */
9254c06356bSdh 	ndi_rele_devi(phci_dip);
9265e3986cbScth }
9275e3986cbScth 
9285e3986cbScth /*
9295e3986cbScth  * mdi_devi_get_vdip():
9305e3986cbScth  *		given a pHCI dip return vHCI dip
9313fe80ca4SDan Cross  * Returns:
932*d5ebc493SDan Cross  *		the vHCI dip if it exists
933*d5ebc493SDan Cross  *		else NULL
9345e3986cbScth  */
9355e3986cbScth dev_info_t *
mdi_devi_get_vdip(dev_info_t * pdip)9365e3986cbScth mdi_devi_get_vdip(dev_info_t *pdip)
9375e3986cbScth {
9385e3986cbScth 	mdi_phci_t	*ph;
9395e3986cbScth 
9405e3986cbScth 	ph = i_devi_get_phci(pdip);
9415e3986cbScth 	if (ph && ph->ph_vhci)
9425e3986cbScth 		return (ph->ph_vhci->vh_dip);
9435e3986cbScth 	return (NULL);
9445e3986cbScth }
9455e3986cbScth 
9465e3986cbScth /*
9475e3986cbScth  * mdi_devi_pdip_entered():
9485e3986cbScth  *		Return 1 if we are vHCI and have done an ndi_devi_enter
9495e3986cbScth  *		of a pHCI
9505e3986cbScth  */
9515e3986cbScth int
mdi_devi_pdip_entered(dev_info_t * vdip)9525e3986cbScth mdi_devi_pdip_entered(dev_info_t *vdip)
9535e3986cbScth {
9545e3986cbScth 	mdi_vhci_t	*vh;
9555e3986cbScth 	mdi_phci_t	*ph;
9565e3986cbScth 
9575e3986cbScth 	vh = i_devi_get_vhci(vdip);
9585e3986cbScth 	if (vh == NULL)
9595e3986cbScth 		return (0);
9605e3986cbScth 
9615e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
9625e3986cbScth 	ph = vh->vh_phci_head;
9635e3986cbScth 	while (ph) {
9645e3986cbScth 		if (ph->ph_dip && DEVI_BUSY_OWNED(ph->ph_dip)) {
9655e3986cbScth 			MDI_VHCI_PHCI_UNLOCK(vh);
9665e3986cbScth 			return (1);
9675e3986cbScth 		}
9685e3986cbScth 		ph = ph->ph_next;
9695e3986cbScth 	}
9705e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
9715e3986cbScth 	return (0);
9725e3986cbScth }
9735e3986cbScth 
9747c478bd9Sstevel@tonic-gate /*
9757c478bd9Sstevel@tonic-gate  * mdi_phci_path2devinfo():
976d27aea3fSDan Cross  *		Utility function to search for a valid phci device given
9777c478bd9Sstevel@tonic-gate  *		the devfs pathname.
9787c478bd9Sstevel@tonic-gate  */
9797c478bd9Sstevel@tonic-gate dev_info_t *
mdi_phci_path2devinfo(dev_info_t * vdip,caddr_t pathname)9807c478bd9Sstevel@tonic-gate mdi_phci_path2devinfo(dev_info_t *vdip, caddr_t pathname)
9817c478bd9Sstevel@tonic-gate {
9827c478bd9Sstevel@tonic-gate 	char		*temp_pathname;
9837c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
9847c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
985d27aea3fSDan Cross 	dev_info_t	*pdip = NULL;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	vh = i_devi_get_vhci(vdip);
9887c478bd9Sstevel@tonic-gate 	ASSERT(vh != NULL);
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
9917c478bd9Sstevel@tonic-gate 		/*
9927c478bd9Sstevel@tonic-gate 		 * Invalid vHCI component, return failure
9937c478bd9Sstevel@tonic-gate 		 */
9947c478bd9Sstevel@tonic-gate 		return (NULL);
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	temp_pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
9985e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
9997c478bd9Sstevel@tonic-gate 	ph = vh->vh_phci_head;
10007c478bd9Sstevel@tonic-gate 	while (ph != NULL) {
10017c478bd9Sstevel@tonic-gate 		pdip = ph->ph_dip;
10027c478bd9Sstevel@tonic-gate 		ASSERT(pdip != NULL);
10037c478bd9Sstevel@tonic-gate 		*temp_pathname = '\0';
10047c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(pdip, temp_pathname);
10057c478bd9Sstevel@tonic-gate 		if (strcmp(temp_pathname, pathname) == 0) {
10067c478bd9Sstevel@tonic-gate 			break;
10077c478bd9Sstevel@tonic-gate 		}
10087c478bd9Sstevel@tonic-gate 		ph = ph->ph_next;
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
10117c478bd9Sstevel@tonic-gate 		pdip = NULL;
10127c478bd9Sstevel@tonic-gate 	}
10135e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
10147c478bd9Sstevel@tonic-gate 	kmem_free(temp_pathname, MAXPATHLEN);
10157c478bd9Sstevel@tonic-gate 	return (pdip);
10167c478bd9Sstevel@tonic-gate }
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate /*
10197c478bd9Sstevel@tonic-gate  * mdi_phci_get_path_count():
1020d27aea3fSDan Cross  *		get number of path information nodes associated with a given
10217c478bd9Sstevel@tonic-gate  *		pHCI device.
10227c478bd9Sstevel@tonic-gate  */
10237c478bd9Sstevel@tonic-gate int
mdi_phci_get_path_count(dev_info_t * pdip)10247c478bd9Sstevel@tonic-gate mdi_phci_get_path_count(dev_info_t *pdip)
10257c478bd9Sstevel@tonic-gate {
10267c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
10277c478bd9Sstevel@tonic-gate 	int		count = 0;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
10307c478bd9Sstevel@tonic-gate 	if (ph != NULL) {
10317c478bd9Sstevel@tonic-gate 		count = ph->ph_path_count;
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 	return (count);
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate /*
10377c478bd9Sstevel@tonic-gate  * i_mdi_phci_lock():
10387c478bd9Sstevel@tonic-gate  *		Lock a pHCI device
10397c478bd9Sstevel@tonic-gate  * Return Values:
10407c478bd9Sstevel@tonic-gate  *		None
10417c478bd9Sstevel@tonic-gate  * Note:
10427c478bd9Sstevel@tonic-gate  *		The default locking order is:
10437c478bd9Sstevel@tonic-gate  *		_NOTE(LOCK_ORDER(mdi_phci::ph_mutex mdi_pathinfo::pi_mutex))
10447c478bd9Sstevel@tonic-gate  *		But there are number of situations where locks need to be
10457c478bd9Sstevel@tonic-gate  *		grabbed in reverse order.  This routine implements try and lock
10467c478bd9Sstevel@tonic-gate  *		mechanism depending on the requested parameter option.
10477c478bd9Sstevel@tonic-gate  */
10487c478bd9Sstevel@tonic-gate static void
i_mdi_phci_lock(mdi_phci_t * ph,mdi_pathinfo_t * pip)10497c478bd9Sstevel@tonic-gate i_mdi_phci_lock(mdi_phci_t *ph, mdi_pathinfo_t *pip)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 	if (pip) {
10527c478bd9Sstevel@tonic-gate 		/* Reverse locking is requested. */
10537c478bd9Sstevel@tonic-gate 		while (MDI_PHCI_TRYLOCK(ph) == 0) {
10544c06356bSdh 			if (servicing_interrupt()) {
10554c06356bSdh 				MDI_PI_HOLD(pip);
10564c06356bSdh 				MDI_PI_UNLOCK(pip);
10574c06356bSdh 				MDI_PHCI_LOCK(ph);
10584c06356bSdh 				MDI_PI_LOCK(pip);
10594c06356bSdh 				MDI_PI_RELE(pip);
10604c06356bSdh 				break;
10614c06356bSdh 			} else {
10624c06356bSdh 				/*
10634c06356bSdh 				 * tryenter failed. Try to grab again
10644c06356bSdh 				 * after a small delay
10654c06356bSdh 				 */
10664c06356bSdh 				MDI_PI_HOLD(pip);
10674c06356bSdh 				MDI_PI_UNLOCK(pip);
106896c4a178SChris Horne 				delay_random(mdi_delay);
10694c06356bSdh 				MDI_PI_LOCK(pip);
10704c06356bSdh 				MDI_PI_RELE(pip);
10714c06356bSdh 			}
10727c478bd9Sstevel@tonic-gate 		}
10737c478bd9Sstevel@tonic-gate 	} else {
10747c478bd9Sstevel@tonic-gate 		MDI_PHCI_LOCK(ph);
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate /*
10797c478bd9Sstevel@tonic-gate  * i_mdi_phci_unlock():
10807c478bd9Sstevel@tonic-gate  *		Unlock the pHCI component
10817c478bd9Sstevel@tonic-gate  */
10827c478bd9Sstevel@tonic-gate static void
i_mdi_phci_unlock(mdi_phci_t * ph)10837c478bd9Sstevel@tonic-gate i_mdi_phci_unlock(mdi_phci_t *ph)
10847c478bd9Sstevel@tonic-gate {
10857c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate /*
10897c478bd9Sstevel@tonic-gate  * i_mdi_devinfo_create():
10907c478bd9Sstevel@tonic-gate  *		create client device's devinfo node
10917c478bd9Sstevel@tonic-gate  * Return Values:
10927c478bd9Sstevel@tonic-gate  *		dev_info
10937c478bd9Sstevel@tonic-gate  *		NULL
10947c478bd9Sstevel@tonic-gate  * Notes:
10957c478bd9Sstevel@tonic-gate  */
10967c478bd9Sstevel@tonic-gate static dev_info_t *
i_mdi_devinfo_create(mdi_vhci_t * vh,char * name,char * guid,char ** compatible,int ncompatible)10977c478bd9Sstevel@tonic-gate i_mdi_devinfo_create(mdi_vhci_t *vh, char *name, char *guid,
10983c34adc5Sramat 	char **compatible, int ncompatible)
10997c478bd9Sstevel@tonic-gate {
11007c478bd9Sstevel@tonic-gate 	dev_info_t *cdip = NULL;
11017c478bd9Sstevel@tonic-gate 
11025e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	/* Verify for duplicate entry */
11057c478bd9Sstevel@tonic-gate 	cdip = i_mdi_devinfo_find(vh, name, guid);
11067c478bd9Sstevel@tonic-gate 	ASSERT(cdip == NULL);
11077c478bd9Sstevel@tonic-gate 	if (cdip) {
11087c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
11094c06356bSdh 		    "i_mdi_devinfo_create: client %s@%s already exists",
11104c06356bSdh 			name ? name : "", guid ? guid : "");
11117c478bd9Sstevel@tonic-gate 	}
11127c478bd9Sstevel@tonic-gate 
11133c34adc5Sramat 	ndi_devi_alloc_sleep(vh->vh_dip, name, DEVI_SID_NODEID, &cdip);
11147c478bd9Sstevel@tonic-gate 	if (cdip == NULL)
11157c478bd9Sstevel@tonic-gate 		goto fail;
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	/*
11187c478bd9Sstevel@tonic-gate 	 * Create component type and Global unique identifier
11197c478bd9Sstevel@tonic-gate 	 * properties
11207c478bd9Sstevel@tonic-gate 	 */
11217c478bd9Sstevel@tonic-gate 	if (ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
11227c478bd9Sstevel@tonic-gate 	    MDI_CLIENT_GUID_PROP, guid) != DDI_PROP_SUCCESS) {
11237c478bd9Sstevel@tonic-gate 		goto fail;
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	/* Decorate the node with compatible property */
11277c478bd9Sstevel@tonic-gate 	if (compatible &&
11287c478bd9Sstevel@tonic-gate 	    (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip,
11297c478bd9Sstevel@tonic-gate 	    "compatible", compatible, ncompatible) != DDI_PROP_SUCCESS)) {
11307c478bd9Sstevel@tonic-gate 		goto fail;
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	return (cdip);
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate fail:
11367c478bd9Sstevel@tonic-gate 	if (cdip) {
11377c478bd9Sstevel@tonic-gate 		(void) ndi_prop_remove_all(cdip);
11387c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(cdip);
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 	return (NULL);
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate /*
11447c478bd9Sstevel@tonic-gate  * i_mdi_devinfo_find():
11457c478bd9Sstevel@tonic-gate  *		Find a matching devinfo node for given client node name
11467c478bd9Sstevel@tonic-gate  *		and its guid.
11477c478bd9Sstevel@tonic-gate  * Return Values:
11487c478bd9Sstevel@tonic-gate  *		Handle to a dev_info node or NULL
11497c478bd9Sstevel@tonic-gate  */
11507c478bd9Sstevel@tonic-gate static dev_info_t *
i_mdi_devinfo_find(mdi_vhci_t * vh,caddr_t name,char * guid)11517c478bd9Sstevel@tonic-gate i_mdi_devinfo_find(mdi_vhci_t *vh, caddr_t name, char *guid)
11527c478bd9Sstevel@tonic-gate {
11537c478bd9Sstevel@tonic-gate 	char			*data;
1154d27aea3fSDan Cross 	dev_info_t		*cdip = NULL;
1155d27aea3fSDan Cross 	dev_info_t		*ndip = NULL;
11567c478bd9Sstevel@tonic-gate 
11573fe80ca4SDan Cross 	ndi_devi_enter(vh->vh_dip);
11587c478bd9Sstevel@tonic-gate 	ndip = (dev_info_t *)DEVI(vh->vh_dip)->devi_child;
11597c478bd9Sstevel@tonic-gate 	while ((cdip = ndip) != NULL) {
11607c478bd9Sstevel@tonic-gate 		ndip = (dev_info_t *)DEVI(cdip)->devi_sibling;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 		if (strcmp(DEVI(cdip)->devi_node_name, name)) {
11637c478bd9Sstevel@tonic-gate 			continue;
11647c478bd9Sstevel@tonic-gate 		}
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, cdip,
11677c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, MDI_CLIENT_GUID_PROP,
11687c478bd9Sstevel@tonic-gate 		    &data) != DDI_PROP_SUCCESS) {
11697c478bd9Sstevel@tonic-gate 			continue;
11707c478bd9Sstevel@tonic-gate 		}
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 		if (strcmp(data, guid) != 0) {
11737c478bd9Sstevel@tonic-gate 			ddi_prop_free(data);
11747c478bd9Sstevel@tonic-gate 			continue;
11757c478bd9Sstevel@tonic-gate 		}
11767c478bd9Sstevel@tonic-gate 		ddi_prop_free(data);
11777c478bd9Sstevel@tonic-gate 		break;
11787c478bd9Sstevel@tonic-gate 	}
11793fe80ca4SDan Cross 	ndi_devi_exit(vh->vh_dip);
11807c478bd9Sstevel@tonic-gate 	return (cdip);
11817c478bd9Sstevel@tonic-gate }
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate /*
11847c478bd9Sstevel@tonic-gate  * i_mdi_devinfo_remove():
11857c478bd9Sstevel@tonic-gate  *		Remove a client device node
11867c478bd9Sstevel@tonic-gate  */
11877c478bd9Sstevel@tonic-gate static int
i_mdi_devinfo_remove(dev_info_t * vdip,dev_info_t * cdip,int flags)11887c478bd9Sstevel@tonic-gate i_mdi_devinfo_remove(dev_info_t *vdip, dev_info_t *cdip, int flags)
11897c478bd9Sstevel@tonic-gate {
11907c478bd9Sstevel@tonic-gate 	int	rv = MDI_SUCCESS;
11915e3986cbScth 
11927c478bd9Sstevel@tonic-gate 	if (i_mdi_is_child_present(vdip, cdip) == MDI_SUCCESS ||
11937c478bd9Sstevel@tonic-gate 	    (flags & MDI_CLIENT_FLAGS_DEV_NOT_SUPPORTED)) {
11944c06356bSdh 		rv = ndi_devi_offline(cdip, NDI_DEVFS_CLEAN | NDI_DEVI_REMOVE);
11957c478bd9Sstevel@tonic-gate 		if (rv != NDI_SUCCESS) {
11964c06356bSdh 			MDI_DEBUG(1, (MDI_NOTE, cdip,
11974c06356bSdh 			    "!failed: cdip %p", (void *)cdip));
11987c478bd9Sstevel@tonic-gate 		}
11997c478bd9Sstevel@tonic-gate 		/*
12007c478bd9Sstevel@tonic-gate 		 * Convert to MDI error code
12017c478bd9Sstevel@tonic-gate 		 */
12027c478bd9Sstevel@tonic-gate 		switch (rv) {
12037c478bd9Sstevel@tonic-gate 		case NDI_SUCCESS:
12047c478bd9Sstevel@tonic-gate 			rv = MDI_SUCCESS;
12057c478bd9Sstevel@tonic-gate 			break;
12067c478bd9Sstevel@tonic-gate 		case NDI_BUSY:
12077c478bd9Sstevel@tonic-gate 			rv = MDI_BUSY;
12087c478bd9Sstevel@tonic-gate 			break;
12097c478bd9Sstevel@tonic-gate 		default:
12107c478bd9Sstevel@tonic-gate 			rv = MDI_FAILURE;
12117c478bd9Sstevel@tonic-gate 			break;
12127c478bd9Sstevel@tonic-gate 		}
12137c478bd9Sstevel@tonic-gate 	}
12147c478bd9Sstevel@tonic-gate 	return (rv);
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate /*
12187c478bd9Sstevel@tonic-gate  * i_devi_get_client()
12197c478bd9Sstevel@tonic-gate  *		Utility function to get mpxio component extensions
12207c478bd9Sstevel@tonic-gate  */
12217c478bd9Sstevel@tonic-gate static mdi_client_t *
i_devi_get_client(dev_info_t * cdip)12227c478bd9Sstevel@tonic-gate i_devi_get_client(dev_info_t *cdip)
12237c478bd9Sstevel@tonic-gate {
12247c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct = NULL;
12255e3986cbScth 
12267c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(cdip)) {
12277c478bd9Sstevel@tonic-gate 		ct = (mdi_client_t *)DEVI(cdip)->devi_mdi_client;
12287c478bd9Sstevel@tonic-gate 	}
12297c478bd9Sstevel@tonic-gate 	return (ct);
12307c478bd9Sstevel@tonic-gate }
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate /*
12337c478bd9Sstevel@tonic-gate  * i_mdi_is_child_present():
12347c478bd9Sstevel@tonic-gate  *		Search for the presence of client device dev_info node
12357c478bd9Sstevel@tonic-gate  */
12367c478bd9Sstevel@tonic-gate static int
i_mdi_is_child_present(dev_info_t * vdip,dev_info_t * cdip)12377c478bd9Sstevel@tonic-gate i_mdi_is_child_present(dev_info_t *vdip, dev_info_t *cdip)
12387c478bd9Sstevel@tonic-gate {
12397c478bd9Sstevel@tonic-gate 	int		rv = MDI_FAILURE;
12407c478bd9Sstevel@tonic-gate 	struct dev_info	*dip;
12417c478bd9Sstevel@tonic-gate 
12423fe80ca4SDan Cross 	ndi_devi_enter(vdip);
12437c478bd9Sstevel@tonic-gate 	dip = DEVI(vdip)->devi_child;
12447c478bd9Sstevel@tonic-gate 	while (dip) {
12457c478bd9Sstevel@tonic-gate 		if (dip == DEVI(cdip)) {
12467c478bd9Sstevel@tonic-gate 			rv = MDI_SUCCESS;
12477c478bd9Sstevel@tonic-gate 			break;
12487c478bd9Sstevel@tonic-gate 		}
12497c478bd9Sstevel@tonic-gate 		dip = dip->devi_sibling;
12507c478bd9Sstevel@tonic-gate 	}
12513fe80ca4SDan Cross 	ndi_devi_exit(vdip);
12527c478bd9Sstevel@tonic-gate 	return (rv);
12537c478bd9Sstevel@tonic-gate }
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate /*
12577c478bd9Sstevel@tonic-gate  * i_mdi_client_lock():
12587c478bd9Sstevel@tonic-gate  *		Grab client component lock
12597c478bd9Sstevel@tonic-gate  * Return Values:
12607c478bd9Sstevel@tonic-gate  *		None
12617c478bd9Sstevel@tonic-gate  * Note:
12627c478bd9Sstevel@tonic-gate  *		The default locking order is:
12637c478bd9Sstevel@tonic-gate  *		_NOTE(LOCK_ORDER(mdi_client::ct_mutex mdi_pathinfo::pi_mutex))
12647c478bd9Sstevel@tonic-gate  *		But there are number of situations where locks need to be
12657c478bd9Sstevel@tonic-gate  *		grabbed in reverse order.  This routine implements try and lock
12667c478bd9Sstevel@tonic-gate  *		mechanism depending on the requested parameter option.
12677c478bd9Sstevel@tonic-gate  */
12687c478bd9Sstevel@tonic-gate static void
i_mdi_client_lock(mdi_client_t * ct,mdi_pathinfo_t * pip)12697c478bd9Sstevel@tonic-gate i_mdi_client_lock(mdi_client_t *ct, mdi_pathinfo_t *pip)
12707c478bd9Sstevel@tonic-gate {
12717c478bd9Sstevel@tonic-gate 	if (pip) {
12727c478bd9Sstevel@tonic-gate 		/*
12737c478bd9Sstevel@tonic-gate 		 * Reverse locking is requested.
12747c478bd9Sstevel@tonic-gate 		 */
12757c478bd9Sstevel@tonic-gate 		while (MDI_CLIENT_TRYLOCK(ct) == 0) {
12764c06356bSdh 			if (servicing_interrupt()) {
12774c06356bSdh 				MDI_PI_HOLD(pip);
12784c06356bSdh 				MDI_PI_UNLOCK(pip);
12794c06356bSdh 				MDI_CLIENT_LOCK(ct);
12804c06356bSdh 				MDI_PI_LOCK(pip);
12814c06356bSdh 				MDI_PI_RELE(pip);
12824c06356bSdh 				break;
12834c06356bSdh 			} else {
12844c06356bSdh 				/*
12854c06356bSdh 				 * tryenter failed. Try to grab again
12864c06356bSdh 				 * after a small delay
12874c06356bSdh 				 */
12884c06356bSdh 				MDI_PI_HOLD(pip);
12894c06356bSdh 				MDI_PI_UNLOCK(pip);
129096c4a178SChris Horne 				delay_random(mdi_delay);
12914c06356bSdh 				MDI_PI_LOCK(pip);
12924c06356bSdh 				MDI_PI_RELE(pip);
12934c06356bSdh 			}
12947c478bd9Sstevel@tonic-gate 		}
12957c478bd9Sstevel@tonic-gate 	} else {
12967c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
12977c478bd9Sstevel@tonic-gate 	}
12987c478bd9Sstevel@tonic-gate }
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate /*
13017c478bd9Sstevel@tonic-gate  * i_mdi_client_unlock():
13027c478bd9Sstevel@tonic-gate  *		Unlock a client component
13037c478bd9Sstevel@tonic-gate  */
13047c478bd9Sstevel@tonic-gate static void
i_mdi_client_unlock(mdi_client_t * ct)13057c478bd9Sstevel@tonic-gate i_mdi_client_unlock(mdi_client_t *ct)
13067c478bd9Sstevel@tonic-gate {
13077c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate /*
13117c478bd9Sstevel@tonic-gate  * i_mdi_client_alloc():
1312d27aea3fSDan Cross  *		Allocate and initialize a client structure.  Caller should
13135e3986cbScth  *		hold the vhci client lock.
13147c478bd9Sstevel@tonic-gate  * Return Values:
13157c478bd9Sstevel@tonic-gate  *		Handle to a client component
13167c478bd9Sstevel@tonic-gate  */
13177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13187c478bd9Sstevel@tonic-gate static mdi_client_t *
i_mdi_client_alloc(mdi_vhci_t * vh,char * name,char * lguid)13193c34adc5Sramat i_mdi_client_alloc(mdi_vhci_t *vh, char *name, char *lguid)
13207c478bd9Sstevel@tonic-gate {
13217c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
13227c478bd9Sstevel@tonic-gate 
13235e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	/*
13267c478bd9Sstevel@tonic-gate 	 * Allocate and initialize a component structure.
13277c478bd9Sstevel@tonic-gate 	 */
13283c34adc5Sramat 	ct = kmem_zalloc(sizeof (*ct), KM_SLEEP);
13297c478bd9Sstevel@tonic-gate 	mutex_init(&ct->ct_mutex, NULL, MUTEX_DEFAULT, NULL);
13307c478bd9Sstevel@tonic-gate 	ct->ct_hnext = NULL;
13317c478bd9Sstevel@tonic-gate 	ct->ct_hprev = NULL;
13327c478bd9Sstevel@tonic-gate 	ct->ct_dip = NULL;
13337c478bd9Sstevel@tonic-gate 	ct->ct_vhci = vh;
13343c34adc5Sramat 	ct->ct_drvname = kmem_alloc(strlen(name) + 1, KM_SLEEP);
13357c478bd9Sstevel@tonic-gate 	(void) strcpy(ct->ct_drvname, name);
13363c34adc5Sramat 	ct->ct_guid = kmem_alloc(strlen(lguid) + 1, KM_SLEEP);
13377c478bd9Sstevel@tonic-gate 	(void) strcpy(ct->ct_guid, lguid);
13387c478bd9Sstevel@tonic-gate 	ct->ct_cprivate = NULL;
13397c478bd9Sstevel@tonic-gate 	ct->ct_vprivate = NULL;
13407c478bd9Sstevel@tonic-gate 	ct->ct_flags = 0;
13417c478bd9Sstevel@tonic-gate 	ct->ct_state = MDI_CLIENT_STATE_FAILED;
13425e3986cbScth 	MDI_CLIENT_LOCK(ct);
13437c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_OFFLINE(ct);
13447c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_DETACH(ct);
13457c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_POWER_UP(ct);
13465e3986cbScth 	MDI_CLIENT_UNLOCK(ct);
13477c478bd9Sstevel@tonic-gate 	ct->ct_failover_flags = 0;
13487c478bd9Sstevel@tonic-gate 	ct->ct_failover_status = 0;
13497c478bd9Sstevel@tonic-gate 	cv_init(&ct->ct_failover_cv, NULL, CV_DRIVER, NULL);
13507c478bd9Sstevel@tonic-gate 	ct->ct_unstable = 0;
13517c478bd9Sstevel@tonic-gate 	cv_init(&ct->ct_unstable_cv, NULL, CV_DRIVER, NULL);
13527c478bd9Sstevel@tonic-gate 	cv_init(&ct->ct_powerchange_cv, NULL, CV_DRIVER, NULL);
13537c478bd9Sstevel@tonic-gate 	ct->ct_lb = vh->vh_lb;
13543c34adc5Sramat 	ct->ct_lb_args =  kmem_zalloc(sizeof (client_lb_args_t), KM_SLEEP);
13557c478bd9Sstevel@tonic-gate 	ct->ct_lb_args->region_size = LOAD_BALANCE_DEFAULT_REGION_SIZE;
13567c478bd9Sstevel@tonic-gate 	ct->ct_path_count = 0;
13577c478bd9Sstevel@tonic-gate 	ct->ct_path_head = NULL;
13587c478bd9Sstevel@tonic-gate 	ct->ct_path_tail = NULL;
13597c478bd9Sstevel@tonic-gate 	ct->ct_path_last = NULL;
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	/*
13627c478bd9Sstevel@tonic-gate 	 * Add this client component to our client hash queue
13637c478bd9Sstevel@tonic-gate 	 */
13647c478bd9Sstevel@tonic-gate 	i_mdi_client_enlist_table(vh, ct);
13657c478bd9Sstevel@tonic-gate 	return (ct);
13667c478bd9Sstevel@tonic-gate }
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate /*
13697c478bd9Sstevel@tonic-gate  * i_mdi_client_enlist_table():
13707c478bd9Sstevel@tonic-gate  *		Attach the client device to the client hash table. Caller
13715e3986cbScth  *		should hold the vhci client lock.
13727c478bd9Sstevel@tonic-gate  */
13737c478bd9Sstevel@tonic-gate static void
i_mdi_client_enlist_table(mdi_vhci_t * vh,mdi_client_t * ct)13747c478bd9Sstevel@tonic-gate i_mdi_client_enlist_table(mdi_vhci_t *vh, mdi_client_t *ct)
13757c478bd9Sstevel@tonic-gate {
1376d27aea3fSDan Cross 	int			index;
13777c478bd9Sstevel@tonic-gate 	struct client_hash	*head;
13787c478bd9Sstevel@tonic-gate 
13795e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
13805e3986cbScth 
13817c478bd9Sstevel@tonic-gate 	index = i_mdi_get_hash_key(ct->ct_guid);
13827c478bd9Sstevel@tonic-gate 	head = &vh->vh_client_table[index];
13837c478bd9Sstevel@tonic-gate 	ct->ct_hnext = (mdi_client_t *)head->ct_hash_head;
13847c478bd9Sstevel@tonic-gate 	head->ct_hash_head = ct;
13857c478bd9Sstevel@tonic-gate 	head->ct_hash_count++;
13867c478bd9Sstevel@tonic-gate 	vh->vh_client_count++;
13877c478bd9Sstevel@tonic-gate }
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate /*
13907c478bd9Sstevel@tonic-gate  * i_mdi_client_delist_table():
13917c478bd9Sstevel@tonic-gate  *		Attach the client device to the client hash table.
13925e3986cbScth  *		Caller should hold the vhci client lock.
13937c478bd9Sstevel@tonic-gate  */
13947c478bd9Sstevel@tonic-gate static void
i_mdi_client_delist_table(mdi_vhci_t * vh,mdi_client_t * ct)13957c478bd9Sstevel@tonic-gate i_mdi_client_delist_table(mdi_vhci_t *vh, mdi_client_t *ct)
13967c478bd9Sstevel@tonic-gate {
13977c478bd9Sstevel@tonic-gate 	int			index;
13987c478bd9Sstevel@tonic-gate 	char			*guid;
1399d27aea3fSDan Cross 	struct client_hash	*head;
14007c478bd9Sstevel@tonic-gate 	mdi_client_t		*next;
14017c478bd9Sstevel@tonic-gate 	mdi_client_t		*last;
14027c478bd9Sstevel@tonic-gate 
14035e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
14045e3986cbScth 
14057c478bd9Sstevel@tonic-gate 	guid = ct->ct_guid;
14067c478bd9Sstevel@tonic-gate 	index = i_mdi_get_hash_key(guid);
14077c478bd9Sstevel@tonic-gate 	head = &vh->vh_client_table[index];
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	last = NULL;
14107c478bd9Sstevel@tonic-gate 	next = (mdi_client_t *)head->ct_hash_head;
14117c478bd9Sstevel@tonic-gate 	while (next != NULL) {
14127c478bd9Sstevel@tonic-gate 		if (next == ct) {
14137c478bd9Sstevel@tonic-gate 			break;
14147c478bd9Sstevel@tonic-gate 		}
14157c478bd9Sstevel@tonic-gate 		last = next;
14167c478bd9Sstevel@tonic-gate 		next = next->ct_hnext;
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	if (next) {
14207c478bd9Sstevel@tonic-gate 		head->ct_hash_count--;
14217c478bd9Sstevel@tonic-gate 		if (last == NULL) {
14227c478bd9Sstevel@tonic-gate 			head->ct_hash_head = ct->ct_hnext;
14237c478bd9Sstevel@tonic-gate 		} else {
14247c478bd9Sstevel@tonic-gate 			last->ct_hnext = ct->ct_hnext;
14257c478bd9Sstevel@tonic-gate 		}
14267c478bd9Sstevel@tonic-gate 		ct->ct_hnext = NULL;
14277c478bd9Sstevel@tonic-gate 		vh->vh_client_count--;
14287c478bd9Sstevel@tonic-gate 	}
14297c478bd9Sstevel@tonic-gate }
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate /*
14337c478bd9Sstevel@tonic-gate  * i_mdi_client_free():
14347c478bd9Sstevel@tonic-gate  *		Free a client component
14357c478bd9Sstevel@tonic-gate  */
14367c478bd9Sstevel@tonic-gate static int
i_mdi_client_free(mdi_vhci_t * vh,mdi_client_t * ct)14377c478bd9Sstevel@tonic-gate i_mdi_client_free(mdi_vhci_t *vh, mdi_client_t *ct)
14387c478bd9Sstevel@tonic-gate {
14397c478bd9Sstevel@tonic-gate 	int		rv = MDI_SUCCESS;
14407c478bd9Sstevel@tonic-gate 	int		flags = ct->ct_flags;
14417c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
14427c478bd9Sstevel@tonic-gate 	dev_info_t	*vdip;
14437c478bd9Sstevel@tonic-gate 
14445e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
14455e3986cbScth 
14467c478bd9Sstevel@tonic-gate 	vdip = vh->vh_dip;
14477c478bd9Sstevel@tonic-gate 	cdip = ct->ct_dip;
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	(void) ndi_prop_remove(DDI_DEV_T_NONE, cdip, MDI_CLIENT_GUID_PROP);
14507c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_component &= ~MDI_COMPONENT_CLIENT;
14517c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_client = NULL;
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	/*
14547c478bd9Sstevel@tonic-gate 	 * Clear out back ref. to dev_info_t node
14557c478bd9Sstevel@tonic-gate 	 */
14567c478bd9Sstevel@tonic-gate 	ct->ct_dip = NULL;
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	/*
14597c478bd9Sstevel@tonic-gate 	 * Remove this client from our hash queue
14607c478bd9Sstevel@tonic-gate 	 */
14617c478bd9Sstevel@tonic-gate 	i_mdi_client_delist_table(vh, ct);
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	/*
14647c478bd9Sstevel@tonic-gate 	 * Uninitialize and free the component
14657c478bd9Sstevel@tonic-gate 	 */
14667c478bd9Sstevel@tonic-gate 	kmem_free(ct->ct_drvname, strlen(ct->ct_drvname) + 1);
14677c478bd9Sstevel@tonic-gate 	kmem_free(ct->ct_guid, strlen(ct->ct_guid) + 1);
14687c478bd9Sstevel@tonic-gate 	kmem_free(ct->ct_lb_args, sizeof (client_lb_args_t));
14697c478bd9Sstevel@tonic-gate 	cv_destroy(&ct->ct_failover_cv);
14707c478bd9Sstevel@tonic-gate 	cv_destroy(&ct->ct_unstable_cv);
14717c478bd9Sstevel@tonic-gate 	cv_destroy(&ct->ct_powerchange_cv);
14727c478bd9Sstevel@tonic-gate 	mutex_destroy(&ct->ct_mutex);
14737c478bd9Sstevel@tonic-gate 	kmem_free(ct, sizeof (*ct));
14747c478bd9Sstevel@tonic-gate 
14759c7f5315SJohn Levon 	MDI_VHCI_CLIENT_UNLOCK(vh);
14769c7f5315SJohn Levon 	(void) i_mdi_devinfo_remove(vdip, cdip, flags);
14779c7f5315SJohn Levon 	MDI_VHCI_CLIENT_LOCK(vh);
14789c7f5315SJohn Levon 
14797c478bd9Sstevel@tonic-gate 	return (rv);
14807c478bd9Sstevel@tonic-gate }
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate /*
14837c478bd9Sstevel@tonic-gate  * i_mdi_client_find():
1484d27aea3fSDan Cross  *		Find the client structure corresponding to a given guid
14855e3986cbScth  *		Caller should hold the vhci client lock.
14867c478bd9Sstevel@tonic-gate  */
14877c478bd9Sstevel@tonic-gate static mdi_client_t *
i_mdi_client_find(mdi_vhci_t * vh,char * cname,char * guid)14883c34adc5Sramat i_mdi_client_find(mdi_vhci_t *vh, char *cname, char *guid)
14897c478bd9Sstevel@tonic-gate {
14907c478bd9Sstevel@tonic-gate 	int			index;
14917c478bd9Sstevel@tonic-gate 	struct client_hash	*head;
14927c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
14937c478bd9Sstevel@tonic-gate 
14945e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
14955e3986cbScth 
14967c478bd9Sstevel@tonic-gate 	index = i_mdi_get_hash_key(guid);
14977c478bd9Sstevel@tonic-gate 	head = &vh->vh_client_table[index];
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	ct = head->ct_hash_head;
15007c478bd9Sstevel@tonic-gate 	while (ct != NULL) {
15013c34adc5Sramat 		if (strcmp(ct->ct_guid, guid) == 0 &&
15023c34adc5Sramat 		    (cname == NULL || strcmp(ct->ct_drvname, cname) == 0)) {
15037c478bd9Sstevel@tonic-gate 			break;
15047c478bd9Sstevel@tonic-gate 		}
15057c478bd9Sstevel@tonic-gate 		ct = ct->ct_hnext;
15067c478bd9Sstevel@tonic-gate 	}
15077c478bd9Sstevel@tonic-gate 	return (ct);
15087c478bd9Sstevel@tonic-gate }
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate /*
15117c478bd9Sstevel@tonic-gate  * i_mdi_client_update_state():
15127c478bd9Sstevel@tonic-gate  *		Compute and update client device state
15137c478bd9Sstevel@tonic-gate  * Notes:
15147c478bd9Sstevel@tonic-gate  *		A client device can be in any of three possible states:
15157c478bd9Sstevel@tonic-gate  *
15167c478bd9Sstevel@tonic-gate  *		MDI_CLIENT_STATE_OPTIMAL - Client in optimal state with more
15177c478bd9Sstevel@tonic-gate  *		one online/standby paths. Can tolerate failures.
15187c478bd9Sstevel@tonic-gate  *		MDI_CLIENT_STATE_DEGRADED - Client device in degraded state with
15197c478bd9Sstevel@tonic-gate  *		no alternate paths available as standby. A failure on the online
15207c478bd9Sstevel@tonic-gate  *		would result in loss of access to device data.
15217c478bd9Sstevel@tonic-gate  *		MDI_CLIENT_STATE_FAILED - Client device in failed state with
15227c478bd9Sstevel@tonic-gate  *		no paths available to access the device.
15237c478bd9Sstevel@tonic-gate  */
15247c478bd9Sstevel@tonic-gate static void
i_mdi_client_update_state(mdi_client_t * ct)15257c478bd9Sstevel@tonic-gate i_mdi_client_update_state(mdi_client_t *ct)
15267c478bd9Sstevel@tonic-gate {
15277c478bd9Sstevel@tonic-gate 	int state;
15285e3986cbScth 
15295e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
15307c478bd9Sstevel@tonic-gate 	state = i_mdi_client_compute_state(ct, NULL);
15317c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_STATE(ct, state);
15327c478bd9Sstevel@tonic-gate }
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate /*
15357c478bd9Sstevel@tonic-gate  * i_mdi_client_compute_state():
15367c478bd9Sstevel@tonic-gate  *		Compute client device state
15377c478bd9Sstevel@tonic-gate  *
15387c478bd9Sstevel@tonic-gate  *		mdi_phci_t *	Pointer to pHCI structure which should
15397c478bd9Sstevel@tonic-gate  *				while computing the new value.  Used by
15407c478bd9Sstevel@tonic-gate  *				i_mdi_phci_offline() to find the new
15417c478bd9Sstevel@tonic-gate  *				client state after DR of a pHCI.
15427c478bd9Sstevel@tonic-gate  */
15437c478bd9Sstevel@tonic-gate static int
i_mdi_client_compute_state(mdi_client_t * ct,mdi_phci_t * ph)15447c478bd9Sstevel@tonic-gate i_mdi_client_compute_state(mdi_client_t *ct, mdi_phci_t *ph)
15457c478bd9Sstevel@tonic-gate {
15467c478bd9Sstevel@tonic-gate 	int		state;
15477c478bd9Sstevel@tonic-gate 	int		online_count = 0;
15487c478bd9Sstevel@tonic-gate 	int		standby_count = 0;
15497c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip, *next;
15507c478bd9Sstevel@tonic-gate 
15515e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
15527c478bd9Sstevel@tonic-gate 	pip = ct->ct_path_head;
15537c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
15547c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
15557c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
15567c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_phci == ph) {
15577c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
15587c478bd9Sstevel@tonic-gate 			pip = next;
15597c478bd9Sstevel@tonic-gate 			continue;
15607c478bd9Sstevel@tonic-gate 		}
15615e3986cbScth 
15627c478bd9Sstevel@tonic-gate 		if ((MDI_PI(pip)->pi_state & MDI_PATHINFO_STATE_MASK)
15637c478bd9Sstevel@tonic-gate 				== MDI_PATHINFO_STATE_ONLINE)
15647c478bd9Sstevel@tonic-gate 			online_count++;
15657c478bd9Sstevel@tonic-gate 		else if ((MDI_PI(pip)->pi_state & MDI_PATHINFO_STATE_MASK)
15667c478bd9Sstevel@tonic-gate 				== MDI_PATHINFO_STATE_STANDBY)
15677c478bd9Sstevel@tonic-gate 			standby_count++;
15687c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
15697c478bd9Sstevel@tonic-gate 		pip = next;
15707c478bd9Sstevel@tonic-gate 	}
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	if (online_count == 0) {
15737c478bd9Sstevel@tonic-gate 		if (standby_count == 0) {
15747c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_FAILED;
15754c06356bSdh 			MDI_DEBUG(2, (MDI_NOTE, ct->ct_dip,
15764c06356bSdh 			    "client state failed: ct = %p", (void *)ct));
15777c478bd9Sstevel@tonic-gate 		} else if (standby_count == 1) {
15787c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_DEGRADED;
15797c478bd9Sstevel@tonic-gate 		} else {
15807c478bd9Sstevel@tonic-gate 			state = MDI_CLIENT_STATE_OPTIMAL;
15817c478bd9Sstevel@tonic-gate 		}
15827c478bd9Sstevel@tonic-gate 	} else if (online_count == 1) {
15837c478bd9Sstevel@tonic-gate 		if (standby_count == 0) {
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 {
15897c478bd9Sstevel@tonic-gate 		state = MDI_CLIENT_STATE_OPTIMAL;
15907c478bd9Sstevel@tonic-gate 	}
15917c478bd9Sstevel@tonic-gate 	return (state);
15927c478bd9Sstevel@tonic-gate }
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate /*
15957c478bd9Sstevel@tonic-gate  * i_mdi_client2devinfo():
15967c478bd9Sstevel@tonic-gate  *		Utility function
15977c478bd9Sstevel@tonic-gate  */
15987c478bd9Sstevel@tonic-gate dev_info_t *
i_mdi_client2devinfo(mdi_client_t * ct)15997c478bd9Sstevel@tonic-gate i_mdi_client2devinfo(mdi_client_t *ct)
16007c478bd9Sstevel@tonic-gate {
16017c478bd9Sstevel@tonic-gate 	return (ct->ct_dip);
16027c478bd9Sstevel@tonic-gate }
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate /*
16057c478bd9Sstevel@tonic-gate  * mdi_client_path2_devinfo():
1606d27aea3fSDan Cross  *		Given the parent devinfo and child devfs pathname, search for
16077c478bd9Sstevel@tonic-gate  *		a valid devfs node handle.
16087c478bd9Sstevel@tonic-gate  */
16097c478bd9Sstevel@tonic-gate dev_info_t *
mdi_client_path2devinfo(dev_info_t * vdip,char * pathname)16107c478bd9Sstevel@tonic-gate mdi_client_path2devinfo(dev_info_t *vdip, char *pathname)
16117c478bd9Sstevel@tonic-gate {
1612d27aea3fSDan Cross 	dev_info_t	*cdip = NULL;
1613d27aea3fSDan Cross 	dev_info_t	*ndip = NULL;
16147c478bd9Sstevel@tonic-gate 	char		*temp_pathname;
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	/*
16177c478bd9Sstevel@tonic-gate 	 * Allocate temp buffer
16187c478bd9Sstevel@tonic-gate 	 */
16197c478bd9Sstevel@tonic-gate 	temp_pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 	/*
16227c478bd9Sstevel@tonic-gate 	 * Lock parent against changes
16237c478bd9Sstevel@tonic-gate 	 */
16243fe80ca4SDan Cross 	ndi_devi_enter(vdip);
16257c478bd9Sstevel@tonic-gate 	ndip = (dev_info_t *)DEVI(vdip)->devi_child;
16267c478bd9Sstevel@tonic-gate 	while ((cdip = ndip) != NULL) {
16277c478bd9Sstevel@tonic-gate 		ndip = (dev_info_t *)DEVI(cdip)->devi_sibling;
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 		*temp_pathname = '\0';
16307c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(cdip, temp_pathname);
16317c478bd9Sstevel@tonic-gate 		if (strcmp(temp_pathname, pathname) == 0) {
16327c478bd9Sstevel@tonic-gate 			break;
16337c478bd9Sstevel@tonic-gate 		}
16347c478bd9Sstevel@tonic-gate 	}
16357c478bd9Sstevel@tonic-gate 	/*
16367c478bd9Sstevel@tonic-gate 	 * Release devinfo lock
16377c478bd9Sstevel@tonic-gate 	 */
16383fe80ca4SDan Cross 	ndi_devi_exit(vdip);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	/*
16417c478bd9Sstevel@tonic-gate 	 * Free the temp buffer
16427c478bd9Sstevel@tonic-gate 	 */
16437c478bd9Sstevel@tonic-gate 	kmem_free(temp_pathname, MAXPATHLEN);
16447c478bd9Sstevel@tonic-gate 	return (cdip);
16457c478bd9Sstevel@tonic-gate }
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate /*
16487c478bd9Sstevel@tonic-gate  * mdi_client_get_path_count():
1649d27aea3fSDan Cross  *		Utility function to get number of path information nodes
16507c478bd9Sstevel@tonic-gate  *		associated with a given client device.
16517c478bd9Sstevel@tonic-gate  */
16527c478bd9Sstevel@tonic-gate int
mdi_client_get_path_count(dev_info_t * cdip)16537c478bd9Sstevel@tonic-gate mdi_client_get_path_count(dev_info_t *cdip)
16547c478bd9Sstevel@tonic-gate {
16557c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
16567c478bd9Sstevel@tonic-gate 	int		count = 0;
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
16597c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
16607c478bd9Sstevel@tonic-gate 		count = ct->ct_path_count;
16617c478bd9Sstevel@tonic-gate 	}
16627c478bd9Sstevel@tonic-gate 	return (count);
16637c478bd9Sstevel@tonic-gate }
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate /*
16677c478bd9Sstevel@tonic-gate  * i_mdi_get_hash_key():
1668d27aea3fSDan Cross  *		Create a hash using strings as keys
16697c478bd9Sstevel@tonic-gate  *
16707c478bd9Sstevel@tonic-gate  */
16717c478bd9Sstevel@tonic-gate static int
i_mdi_get_hash_key(char * str)16727c478bd9Sstevel@tonic-gate i_mdi_get_hash_key(char *str)
16737c478bd9Sstevel@tonic-gate {
16747c478bd9Sstevel@tonic-gate 	uint32_t	g, hash = 0;
16757c478bd9Sstevel@tonic-gate 	char		*p;
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 	for (p = str; *p != '\0'; p++) {
16787c478bd9Sstevel@tonic-gate 		g = *p;
16797c478bd9Sstevel@tonic-gate 		hash += g;
16807c478bd9Sstevel@tonic-gate 	}
16817c478bd9Sstevel@tonic-gate 	return (hash % (CLIENT_HASH_TABLE_SIZE - 1));
16827c478bd9Sstevel@tonic-gate }
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate /*
16857c478bd9Sstevel@tonic-gate  * mdi_get_lb_policy():
1686d27aea3fSDan Cross  *		Get current load balancing policy for a given client device
16877c478bd9Sstevel@tonic-gate  */
16887c478bd9Sstevel@tonic-gate client_lb_t
mdi_get_lb_policy(dev_info_t * cdip)16897c478bd9Sstevel@tonic-gate mdi_get_lb_policy(dev_info_t *cdip)
16907c478bd9Sstevel@tonic-gate {
16917c478bd9Sstevel@tonic-gate 	client_lb_t	lb = LOAD_BALANCE_NONE;
16927c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
16957c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
16967c478bd9Sstevel@tonic-gate 		lb = ct->ct_lb;
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate 	return (lb);
16997c478bd9Sstevel@tonic-gate }
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate /*
17027c478bd9Sstevel@tonic-gate  * mdi_set_lb_region_size():
1703d27aea3fSDan Cross  *		Set current region size for the load-balance
17047c478bd9Sstevel@tonic-gate  */
17057c478bd9Sstevel@tonic-gate int
mdi_set_lb_region_size(dev_info_t * cdip,int region_size)17067c478bd9Sstevel@tonic-gate mdi_set_lb_region_size(dev_info_t *cdip, int region_size)
17077c478bd9Sstevel@tonic-gate {
17087c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
17097c478bd9Sstevel@tonic-gate 	int		rv = MDI_FAILURE;
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
17127c478bd9Sstevel@tonic-gate 	if (ct != NULL && ct->ct_lb_args != NULL) {
17137c478bd9Sstevel@tonic-gate 		ct->ct_lb_args->region_size = region_size;
17147c478bd9Sstevel@tonic-gate 		rv = MDI_SUCCESS;
17157c478bd9Sstevel@tonic-gate 	}
17167c478bd9Sstevel@tonic-gate 	return (rv);
17177c478bd9Sstevel@tonic-gate }
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate /*
17207c478bd9Sstevel@tonic-gate  * mdi_Set_lb_policy():
1721d27aea3fSDan Cross  *		Set current load balancing policy for a given client device
17227c478bd9Sstevel@tonic-gate  */
17237c478bd9Sstevel@tonic-gate int
mdi_set_lb_policy(dev_info_t * cdip,client_lb_t lb)17247c478bd9Sstevel@tonic-gate mdi_set_lb_policy(dev_info_t *cdip, client_lb_t lb)
17257c478bd9Sstevel@tonic-gate {
17267c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
17277c478bd9Sstevel@tonic-gate 	int		rv = MDI_FAILURE;
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
17307c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
17317c478bd9Sstevel@tonic-gate 		ct->ct_lb = lb;
17327c478bd9Sstevel@tonic-gate 		rv = MDI_SUCCESS;
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate 	return (rv);
17357c478bd9Sstevel@tonic-gate }
17367c478bd9Sstevel@tonic-gate 
17379426efbbSToomas Soome static void
mdi_failover_cb(void * arg)17389426efbbSToomas Soome mdi_failover_cb(void *arg)
17399426efbbSToomas Soome {
17409426efbbSToomas Soome 	(void)i_mdi_failover(arg);
17419426efbbSToomas Soome }
17429426efbbSToomas Soome 
17437c478bd9Sstevel@tonic-gate /*
17447c478bd9Sstevel@tonic-gate  * mdi_failover():
17457c478bd9Sstevel@tonic-gate  *		failover function called by the vHCI drivers to initiate
17467c478bd9Sstevel@tonic-gate  *		a failover operation.  This is typically due to non-availability
17477c478bd9Sstevel@tonic-gate  *		of online paths to route I/O requests.  Failover can be
17487c478bd9Sstevel@tonic-gate  *		triggered through user application also.
17497c478bd9Sstevel@tonic-gate  *
17507c478bd9Sstevel@tonic-gate  *		The vHCI driver calls mdi_failover() to initiate a failover
17517c478bd9Sstevel@tonic-gate  *		operation. mdi_failover() calls back into the vHCI driver's
17527c478bd9Sstevel@tonic-gate  *		vo_failover() entry point to perform the actual failover
17537c478bd9Sstevel@tonic-gate  *		operation.  The reason for requiring the vHCI driver to
17547c478bd9Sstevel@tonic-gate  *		initiate failover by calling mdi_failover(), instead of directly
17557c478bd9Sstevel@tonic-gate  *		executing vo_failover() itself, is to ensure that the mdi
17567c478bd9Sstevel@tonic-gate  *		framework can keep track of the client state properly.
17577c478bd9Sstevel@tonic-gate  *		Additionally, mdi_failover() provides as a convenience the
17587c478bd9Sstevel@tonic-gate  *		option of performing the failover operation synchronously or
17597c478bd9Sstevel@tonic-gate  *		asynchronously
17607c478bd9Sstevel@tonic-gate  *
17617c478bd9Sstevel@tonic-gate  *		Upon successful completion of the failover operation, the
17627c478bd9Sstevel@tonic-gate  *		paths that were previously ONLINE will be in the STANDBY state,
17637c478bd9Sstevel@tonic-gate  *		and the newly activated paths will be in the ONLINE state.
17647c478bd9Sstevel@tonic-gate  *
17657c478bd9Sstevel@tonic-gate  *		The flags modifier determines whether the activation is done
17667c478bd9Sstevel@tonic-gate  *		synchronously: MDI_FAILOVER_SYNC
17677c478bd9Sstevel@tonic-gate  * Return Values:
17687c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
17697c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
17707c478bd9Sstevel@tonic-gate  *		MDI_BUSY
17717c478bd9Sstevel@tonic-gate  */
17727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17737c478bd9Sstevel@tonic-gate int
mdi_failover(dev_info_t * vdip,dev_info_t * cdip,int flags)17747c478bd9Sstevel@tonic-gate mdi_failover(dev_info_t *vdip, dev_info_t *cdip, int flags)
17757c478bd9Sstevel@tonic-gate {
17767c478bd9Sstevel@tonic-gate 	int			rv;
17777c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
17807c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
17817c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
17827c478bd9Sstevel@tonic-gate 		/* cdip is not a valid client device. Nothing more to do. */
17837c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
17847c478bd9Sstevel@tonic-gate 	}
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_PATH_FREE_IN_PROGRESS(ct)) {
17897c478bd9Sstevel@tonic-gate 		/* A path to the client is being freed */
17907c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
17917c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
17927c478bd9Sstevel@tonic-gate 	}
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_FAILED(ct)) {
17967c478bd9Sstevel@tonic-gate 		/*
17977c478bd9Sstevel@tonic-gate 		 * Client is in failed state. Nothing more to do.
17987c478bd9Sstevel@tonic-gate 		 */
17997c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
18007c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
18017c478bd9Sstevel@tonic-gate 	}
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
18047c478bd9Sstevel@tonic-gate 		/*
18057c478bd9Sstevel@tonic-gate 		 * Failover is already in progress; return BUSY
18067c478bd9Sstevel@tonic-gate 		 */
18077c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
18087c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
18097c478bd9Sstevel@tonic-gate 	}
18107c478bd9Sstevel@tonic-gate 	/*
18117c478bd9Sstevel@tonic-gate 	 * Make sure that mdi_pathinfo node state changes are processed.
18127c478bd9Sstevel@tonic-gate 	 * We do not allow failovers to progress while client path state
18137c478bd9Sstevel@tonic-gate 	 * changes are in progress
18147c478bd9Sstevel@tonic-gate 	 */
18157c478bd9Sstevel@tonic-gate 	if (ct->ct_unstable) {
18167c478bd9Sstevel@tonic-gate 		if (flags == MDI_FAILOVER_ASYNC) {
18177c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
18187c478bd9Sstevel@tonic-gate 			return (MDI_BUSY);
18197c478bd9Sstevel@tonic-gate 		} else {
18207c478bd9Sstevel@tonic-gate 			while (ct->ct_unstable)
18217c478bd9Sstevel@tonic-gate 				cv_wait(&ct->ct_unstable_cv, &ct->ct_mutex);
18227c478bd9Sstevel@tonic-gate 		}
18237c478bd9Sstevel@tonic-gate 	}
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	/*
18267c478bd9Sstevel@tonic-gate 	 * Client device is in stable state. Before proceeding, perform sanity
18277c478bd9Sstevel@tonic-gate 	 * checks again.
18287c478bd9Sstevel@tonic-gate 	 */
18297c478bd9Sstevel@tonic-gate 	if ((MDI_CLIENT_IS_DETACHED(ct)) || (MDI_CLIENT_IS_FAILED(ct)) ||
18301b94a41bSChris Horne 	    (!i_ddi_devi_attached(cdip))) {
18317c478bd9Sstevel@tonic-gate 		/*
18327c478bd9Sstevel@tonic-gate 		 * Client is in failed state. Nothing more to do.
18337c478bd9Sstevel@tonic-gate 		 */
18347c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
18357c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
18367c478bd9Sstevel@tonic-gate 	}
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	/*
18397c478bd9Sstevel@tonic-gate 	 * Set the client state as failover in progress.
18407c478bd9Sstevel@tonic-gate 	 */
18417c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_FAILOVER_IN_PROGRESS(ct);
18427c478bd9Sstevel@tonic-gate 	ct->ct_failover_flags = flags;
18437c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	if (flags == MDI_FAILOVER_ASYNC) {
18467c478bd9Sstevel@tonic-gate 		/*
18477c478bd9Sstevel@tonic-gate 		 * Submit the initiate failover request via CPR safe
18487c478bd9Sstevel@tonic-gate 		 * taskq threads.
18497c478bd9Sstevel@tonic-gate 		 */
18509426efbbSToomas Soome 		(void) taskq_dispatch(mdi_taskq, mdi_failover_cb, ct, KM_SLEEP);
18517c478bd9Sstevel@tonic-gate 		return (MDI_ACCEPT);
18527c478bd9Sstevel@tonic-gate 	} else {
18537c478bd9Sstevel@tonic-gate 		/*
18547c478bd9Sstevel@tonic-gate 		 * Synchronous failover mode.  Typically invoked from the user
18557c478bd9Sstevel@tonic-gate 		 * land.
18567c478bd9Sstevel@tonic-gate 		 */
18577c478bd9Sstevel@tonic-gate 		rv = i_mdi_failover(ct);
18587c478bd9Sstevel@tonic-gate 	}
18597c478bd9Sstevel@tonic-gate 	return (rv);
18607c478bd9Sstevel@tonic-gate }
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate /*
18637c478bd9Sstevel@tonic-gate  * i_mdi_failover():
18647c478bd9Sstevel@tonic-gate  *		internal failover function. Invokes vHCI drivers failover
18657c478bd9Sstevel@tonic-gate  *		callback function and process the failover status
18667c478bd9Sstevel@tonic-gate  * Return Values:
18677c478bd9Sstevel@tonic-gate  *		None
18687c478bd9Sstevel@tonic-gate  *
18697c478bd9Sstevel@tonic-gate  * Note: A client device in failover state can not be detached or freed.
18707c478bd9Sstevel@tonic-gate  */
18717c478bd9Sstevel@tonic-gate static int
i_mdi_failover(void * arg)18727c478bd9Sstevel@tonic-gate i_mdi_failover(void *arg)
18737c478bd9Sstevel@tonic-gate {
18747c478bd9Sstevel@tonic-gate 	int		rv = MDI_SUCCESS;
18757c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct = (mdi_client_t *)arg;
18767c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = ct->ct_vhci;
18777c478bd9Sstevel@tonic-gate 
18785e3986cbScth 	ASSERT(!MDI_CLIENT_LOCKED(ct));
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	if (vh->vh_ops->vo_failover != NULL) {
18817c478bd9Sstevel@tonic-gate 		/*
18827c478bd9Sstevel@tonic-gate 		 * Call vHCI drivers callback routine
18837c478bd9Sstevel@tonic-gate 		 */
18847c478bd9Sstevel@tonic-gate 		rv = (*vh->vh_ops->vo_failover)(vh->vh_dip, ct->ct_dip,
18857c478bd9Sstevel@tonic-gate 		    ct->ct_failover_flags);
18867c478bd9Sstevel@tonic-gate 	}
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
18897c478bd9Sstevel@tonic-gate 	MDI_CLIENT_CLEAR_FAILOVER_IN_PROGRESS(ct);
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	/*
18927c478bd9Sstevel@tonic-gate 	 * Save the failover return status
18937c478bd9Sstevel@tonic-gate 	 */
18947c478bd9Sstevel@tonic-gate 	ct->ct_failover_status = rv;
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	/*
18977c478bd9Sstevel@tonic-gate 	 * As a result of failover, client status would have been changed.
18987c478bd9Sstevel@tonic-gate 	 * Update the client state and wake up anyone waiting on this client
18997c478bd9Sstevel@tonic-gate 	 * device.
19007c478bd9Sstevel@tonic-gate 	 */
19017c478bd9Sstevel@tonic-gate 	i_mdi_client_update_state(ct);
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	cv_broadcast(&ct->ct_failover_cv);
19047c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
19057c478bd9Sstevel@tonic-gate 	return (rv);
19067c478bd9Sstevel@tonic-gate }
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate /*
19097c478bd9Sstevel@tonic-gate  * Load balancing is logical block.
19107c478bd9Sstevel@tonic-gate  * IOs within the range described by region_size
19117c478bd9Sstevel@tonic-gate  * would go on the same path. This would improve the
19127c478bd9Sstevel@tonic-gate  * performance by cache-hit on some of the RAID devices.
19137c478bd9Sstevel@tonic-gate  * Search only for online paths(At some point we
19147c478bd9Sstevel@tonic-gate  * may want to balance across target ports).
19157c478bd9Sstevel@tonic-gate  * If no paths are found then default to round-robin.
19167c478bd9Sstevel@tonic-gate  */
19177c478bd9Sstevel@tonic-gate static int
i_mdi_lba_lb(mdi_client_t * ct,mdi_pathinfo_t ** ret_pip,struct buf * bp)19187c478bd9Sstevel@tonic-gate i_mdi_lba_lb(mdi_client_t *ct, mdi_pathinfo_t **ret_pip, struct buf *bp)
19197c478bd9Sstevel@tonic-gate {
19207c478bd9Sstevel@tonic-gate 	int		path_index = -1;
19217c478bd9Sstevel@tonic-gate 	int		online_path_count = 0;
19227c478bd9Sstevel@tonic-gate 	int		online_nonpref_path_count = 0;
1923d27aea3fSDan Cross 	int		region_size = ct->ct_lb_args->region_size;
19247c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
19257c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
19267c478bd9Sstevel@tonic-gate 	int		preferred, path_cnt;
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 	pip = ct->ct_path_head;
19297c478bd9Sstevel@tonic-gate 	while (pip) {
19307c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
19317c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_state ==
19327c478bd9Sstevel@tonic-gate 		    MDI_PATHINFO_STATE_ONLINE && MDI_PI(pip)->pi_preferred) {
19337c478bd9Sstevel@tonic-gate 			online_path_count++;
19347c478bd9Sstevel@tonic-gate 		} else if (MDI_PI(pip)->pi_state ==
19357c478bd9Sstevel@tonic-gate 		    MDI_PATHINFO_STATE_ONLINE && !MDI_PI(pip)->pi_preferred) {
19367c478bd9Sstevel@tonic-gate 			online_nonpref_path_count++;
19377c478bd9Sstevel@tonic-gate 		}
19387c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)
19397c478bd9Sstevel@tonic-gate 		    MDI_PI(pip)->pi_client_link;
19407c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
19417c478bd9Sstevel@tonic-gate 		pip = next;
19427c478bd9Sstevel@tonic-gate 	}
19437c478bd9Sstevel@tonic-gate 	/* if found any online/preferred then use this type */
19447c478bd9Sstevel@tonic-gate 	if (online_path_count > 0) {
19457c478bd9Sstevel@tonic-gate 		path_cnt = online_path_count;
19467c478bd9Sstevel@tonic-gate 		preferred = 1;
19477c478bd9Sstevel@tonic-gate 	} else if (online_nonpref_path_count > 0) {
19487c478bd9Sstevel@tonic-gate 		path_cnt = online_nonpref_path_count;
19497c478bd9Sstevel@tonic-gate 		preferred = 0;
19507c478bd9Sstevel@tonic-gate 	} else {
19517c478bd9Sstevel@tonic-gate 		path_cnt = 0;
19527c478bd9Sstevel@tonic-gate 	}
19537c478bd9Sstevel@tonic-gate 	if (path_cnt) {
19547c478bd9Sstevel@tonic-gate 		path_index = (bp->b_blkno >> region_size) % path_cnt;
19557c478bd9Sstevel@tonic-gate 		pip = ct->ct_path_head;
19567c478bd9Sstevel@tonic-gate 		while (pip && path_index != -1) {
19577c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
19587c478bd9Sstevel@tonic-gate 			if (path_index == 0 &&
19597c478bd9Sstevel@tonic-gate 			    (MDI_PI(pip)->pi_state ==
19607c478bd9Sstevel@tonic-gate 			    MDI_PATHINFO_STATE_ONLINE) &&
19617c478bd9Sstevel@tonic-gate 				MDI_PI(pip)->pi_preferred == preferred) {
19627c478bd9Sstevel@tonic-gate 				MDI_PI_HOLD(pip);
19637c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
19647c478bd9Sstevel@tonic-gate 				*ret_pip = pip;
19657c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
19667c478bd9Sstevel@tonic-gate 			}
19677c478bd9Sstevel@tonic-gate 			path_index --;
19687c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)
19697c478bd9Sstevel@tonic-gate 			    MDI_PI(pip)->pi_client_link;
19707c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
19717c478bd9Sstevel@tonic-gate 			pip = next;
19727c478bd9Sstevel@tonic-gate 		}
19734c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
19744c06356bSdh 		    "lba %llx: path %s %p",
19754c06356bSdh 		    bp->b_lblkno, mdi_pi_spathname(pip), (void *)pip));
19767c478bd9Sstevel@tonic-gate 	}
19777c478bd9Sstevel@tonic-gate 	return (MDI_FAILURE);
19787c478bd9Sstevel@tonic-gate }
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate /*
19817c478bd9Sstevel@tonic-gate  * mdi_select_path():
19827c478bd9Sstevel@tonic-gate  *		select a path to access a client device.
19837c478bd9Sstevel@tonic-gate  *
19847c478bd9Sstevel@tonic-gate  *		mdi_select_path() function is called by the vHCI drivers to
19857c478bd9Sstevel@tonic-gate  *		select a path to route the I/O request to.  The caller passes
19867c478bd9Sstevel@tonic-gate  *		the block I/O data transfer structure ("buf") as one of the
19877c478bd9Sstevel@tonic-gate  *		parameters.  The mpxio framework uses the buf structure
19887c478bd9Sstevel@tonic-gate  *		contents to maintain per path statistics (total I/O size /
19897c478bd9Sstevel@tonic-gate  *		count pending).  If more than one online paths are available to
19907c478bd9Sstevel@tonic-gate  *		select, the framework automatically selects a suitable path
19917c478bd9Sstevel@tonic-gate  *		for routing I/O request. If a failover operation is active for
19927c478bd9Sstevel@tonic-gate  *		this client device the call shall be failed with MDI_BUSY error
19937c478bd9Sstevel@tonic-gate  *		code.
19947c478bd9Sstevel@tonic-gate  *
19957c478bd9Sstevel@tonic-gate  *		By default this function returns a suitable path in online
19967c478bd9Sstevel@tonic-gate  *		state based on the current load balancing policy.  Currently
19977c478bd9Sstevel@tonic-gate  *		we support LOAD_BALANCE_NONE (Previously selected online path
19987c478bd9Sstevel@tonic-gate  *		will continue to be used till the path is usable) and
19997c478bd9Sstevel@tonic-gate  *		LOAD_BALANCE_RR (Online paths will be selected in a round
20007c478bd9Sstevel@tonic-gate  *		robin fashion), LOAD_BALANCE_LB(Online paths will be selected
20017c478bd9Sstevel@tonic-gate  *		based on the logical block).  The load balancing
20027c478bd9Sstevel@tonic-gate  *		through vHCI drivers configuration file (driver.conf).
20037c478bd9Sstevel@tonic-gate  *
20047c478bd9Sstevel@tonic-gate  *		vHCI drivers may override this default behavior by specifying
2005602ca9eaScth  *		appropriate flags.  The meaning of the thrid argument depends
2006602ca9eaScth  *		on the flags specified. If MDI_SELECT_PATH_INSTANCE is set
2007602ca9eaScth  *		then the argument is the "path instance" of the path to select.
2008602ca9eaScth  *		If MDI_SELECT_PATH_INSTANCE is not set then the argument is
2009602ca9eaScth  *		"start_pip". A non NULL "start_pip" is the starting point to
2010602ca9eaScth  *		walk and find the next appropriate path.  The following values
2011602ca9eaScth  *		are currently defined: MDI_SELECT_ONLINE_PATH (to select an
2012602ca9eaScth  *		ONLINE path) and/or MDI_SELECT_STANDBY_PATH (to select an
2013602ca9eaScth  *		STANDBY path).
20147c478bd9Sstevel@tonic-gate  *
20157c478bd9Sstevel@tonic-gate  *		The non-standard behavior is used by the scsi_vhci driver,
20167c478bd9Sstevel@tonic-gate  *		whenever it has to use a STANDBY/FAULTED path.  Eg. during
20177c478bd9Sstevel@tonic-gate  *		attach of client devices (to avoid an unnecessary failover
20187c478bd9Sstevel@tonic-gate  *		when the STANDBY path comes up first), during failover
20197c478bd9Sstevel@tonic-gate  *		(to activate a STANDBY path as ONLINE).
20207c478bd9Sstevel@tonic-gate  *
20215e3986cbScth  *		The selected path is returned in a a mdi_hold_path() state
20225e3986cbScth  *		(pi_ref_cnt). Caller should release the hold by calling
20235e3986cbScth  *		mdi_rele_path().
20247c478bd9Sstevel@tonic-gate  *
20257c478bd9Sstevel@tonic-gate  * Return Values:
20267c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS	- Completed successfully
2027d27aea3fSDan Cross  *		MDI_BUSY	- Client device is busy failing over
20287c478bd9Sstevel@tonic-gate  *		MDI_NOPATH	- Client device is online, but no valid path are
20297c478bd9Sstevel@tonic-gate  *				  available to access this client device
20307c478bd9Sstevel@tonic-gate  *		MDI_FAILURE	- Invalid client device or state
20317c478bd9Sstevel@tonic-gate  *		MDI_DEVI_ONLINING
20327c478bd9Sstevel@tonic-gate  *				- Client device (struct dev_info state) is in
20337c478bd9Sstevel@tonic-gate  *				  onlining state.
20347c478bd9Sstevel@tonic-gate  */
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20377c478bd9Sstevel@tonic-gate int
mdi_select_path(dev_info_t * cdip,struct buf * bp,int flags,void * arg,mdi_pathinfo_t ** ret_pip)20387c478bd9Sstevel@tonic-gate mdi_select_path(dev_info_t *cdip, struct buf *bp, int flags,
2039602ca9eaScth     void *arg, mdi_pathinfo_t **ret_pip)
20407c478bd9Sstevel@tonic-gate {
20417c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
20427c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
20437c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
20447c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*head;
20457c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*start;
20467c478bd9Sstevel@tonic-gate 	client_lb_t	lbp;	/* load balancing policy */
20477c478bd9Sstevel@tonic-gate 	int		sb = 1;	/* standard behavior */
20487c478bd9Sstevel@tonic-gate 	int		preferred = 1;	/* preferred path */
20497c478bd9Sstevel@tonic-gate 	int		cond, cont = 1;
20507c478bd9Sstevel@tonic-gate 	int		retry = 0;
2051602ca9eaScth 	mdi_pathinfo_t	*start_pip;	/* request starting pathinfo */
2052602ca9eaScth 	int		path_instance;	/* request specific path instance */
2053602ca9eaScth 
2054602ca9eaScth 	/* determine type of arg based on flags */
2055602ca9eaScth 	if (flags & MDI_SELECT_PATH_INSTANCE) {
2056602ca9eaScth 		path_instance = (int)(intptr_t)arg;
2057602ca9eaScth 		start_pip = NULL;
2058602ca9eaScth 	} else {
2059602ca9eaScth 		path_instance = 0;
2060602ca9eaScth 		start_pip = (mdi_pathinfo_t *)arg;
2061602ca9eaScth 	}
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	if (flags != 0) {
20647c478bd9Sstevel@tonic-gate 		/*
20657c478bd9Sstevel@tonic-gate 		 * disable default behavior
20667c478bd9Sstevel@tonic-gate 		 */
20677c478bd9Sstevel@tonic-gate 		sb = 0;
20687c478bd9Sstevel@tonic-gate 	}
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	*ret_pip = NULL;
20717c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
20727c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
20737c478bd9Sstevel@tonic-gate 		/* mdi extensions are NULL, Nothing more to do */
20747c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 	if (sb) {
20807c478bd9Sstevel@tonic-gate 		if (MDI_CLIENT_IS_FAILED(ct)) {
20817c478bd9Sstevel@tonic-gate 			/*
20827c478bd9Sstevel@tonic-gate 			 * Client is not ready to accept any I/O requests.
20837c478bd9Sstevel@tonic-gate 			 * Fail this request.
20847c478bd9Sstevel@tonic-gate 			 */
20854c06356bSdh 			MDI_DEBUG(2, (MDI_NOTE, cdip,
20864c06356bSdh 			    "client state offline ct = %p", (void *)ct));
20877c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
20887c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
20897c478bd9Sstevel@tonic-gate 		}
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 		if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
20927c478bd9Sstevel@tonic-gate 			/*
20937c478bd9Sstevel@tonic-gate 			 * Check for Failover is in progress. If so tell the
20947c478bd9Sstevel@tonic-gate 			 * caller that this device is busy.
20957c478bd9Sstevel@tonic-gate 			 */
20964c06356bSdh 			MDI_DEBUG(2, (MDI_NOTE, cdip,
20974c06356bSdh 			    "client failover in progress ct = %p",
20985e3986cbScth 			    (void *)ct));
20997c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
21007c478bd9Sstevel@tonic-gate 			return (MDI_BUSY);
21017c478bd9Sstevel@tonic-gate 		}
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 		/*
21047c478bd9Sstevel@tonic-gate 		 * Check to see whether the client device is attached.
21057c478bd9Sstevel@tonic-gate 		 * If not so, let the vHCI driver manually select a path
21067c478bd9Sstevel@tonic-gate 		 * (standby) and let the probe/attach process to continue.
21077c478bd9Sstevel@tonic-gate 		 */
2108737d277aScth 		if (MDI_CLIENT_IS_DETACHED(ct) || !i_ddi_devi_attached(cdip)) {
21094c06356bSdh 			MDI_DEBUG(4, (MDI_NOTE, cdip,
21104c06356bSdh 			    "devi is onlining ct = %p", (void *)ct));
21117c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
21127c478bd9Sstevel@tonic-gate 			return (MDI_DEVI_ONLINING);
21137c478bd9Sstevel@tonic-gate 		}
21147c478bd9Sstevel@tonic-gate 	}
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 	/*
21177c478bd9Sstevel@tonic-gate 	 * Cache in the client list head.  If head of the list is NULL
21187c478bd9Sstevel@tonic-gate 	 * return MDI_NOPATH
21197c478bd9Sstevel@tonic-gate 	 */
21207c478bd9Sstevel@tonic-gate 	head = ct->ct_path_head;
21217c478bd9Sstevel@tonic-gate 	if (head == NULL) {
21227c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
21237c478bd9Sstevel@tonic-gate 		return (MDI_NOPATH);
21247c478bd9Sstevel@tonic-gate 	}
21257c478bd9Sstevel@tonic-gate 
2126602ca9eaScth 	/* Caller is specifying a specific pathinfo path by path_instance */
2127602ca9eaScth 	if (path_instance) {
2128602ca9eaScth 		/* search for pathinfo with correct path_instance */
2129602ca9eaScth 		for (pip = head;
2130602ca9eaScth 		    pip && (mdi_pi_get_path_instance(pip) != path_instance);
2131602ca9eaScth 		    pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link)
2132602ca9eaScth 			;
2133602ca9eaScth 
2134ab412e72SSrikanth, Ramana 		/* If path can't be selected then MDI_NOPATH is returned. */
2135602ca9eaScth 		if (pip == NULL) {
2136602ca9eaScth 			MDI_CLIENT_UNLOCK(ct);
2137ab412e72SSrikanth, Ramana 			return (MDI_NOPATH);
2138602ca9eaScth 		}
2139602ca9eaScth 
21404c06356bSdh 		/*
21414c06356bSdh 		 * Verify state of path. When asked to select a specific
21424c06356bSdh 		 * path_instance, we select the requested path in any
21434c06356bSdh 		 * state (ONLINE, OFFLINE, STANDBY, FAULT) other than INIT.
21444c06356bSdh 		 * We don't however select paths where the pHCI has detached.
21454c06356bSdh 		 * NOTE: last pathinfo node of an opened client device may
21464c06356bSdh 		 * exist in an OFFLINE state after the pHCI associated with
21474c06356bSdh 		 * that path has detached (but pi_phci will be NULL if that
21484c06356bSdh 		 * has occurred).
21494c06356bSdh 		 */
2150602ca9eaScth 		MDI_PI_LOCK(pip);
21514c06356bSdh 		if ((MDI_PI(pip)->pi_state == MDI_PATHINFO_STATE_INIT) ||
21524c06356bSdh 		    (MDI_PI(pip)->pi_phci == NULL)) {
2153602ca9eaScth 			MDI_PI_UNLOCK(pip);
2154602ca9eaScth 			MDI_CLIENT_UNLOCK(ct);
2155602ca9eaScth 			return (MDI_FAILURE);
2156602ca9eaScth 		}
2157602ca9eaScth 
2158ab412e72SSrikanth, Ramana 		/* Return MDI_BUSY if we have a transient condition */
2159ab412e72SSrikanth, Ramana 		if (MDI_PI_IS_TRANSIENT(pip)) {
2160ab412e72SSrikanth, Ramana 			MDI_PI_UNLOCK(pip);
2161ab412e72SSrikanth, Ramana 			MDI_CLIENT_UNLOCK(ct);
2162ab412e72SSrikanth, Ramana 			return (MDI_BUSY);
2163ab412e72SSrikanth, Ramana 		}
2164ab412e72SSrikanth, Ramana 
2165602ca9eaScth 		/*
2166602ca9eaScth 		 * Return the path in hold state. Caller should release the
2167602ca9eaScth 		 * lock by calling mdi_rele_path()
2168602ca9eaScth 		 */
2169602ca9eaScth 		MDI_PI_HOLD(pip);
2170602ca9eaScth 		MDI_PI_UNLOCK(pip);
2171602ca9eaScth 		*ret_pip = pip;
2172602ca9eaScth 		MDI_CLIENT_UNLOCK(ct);
2173602ca9eaScth 		return (MDI_SUCCESS);
2174602ca9eaScth 	}
2175602ca9eaScth 
21767c478bd9Sstevel@tonic-gate 	/*
21777c478bd9Sstevel@tonic-gate 	 * for non default behavior, bypass current
21787c478bd9Sstevel@tonic-gate 	 * load balancing policy and always use LOAD_BALANCE_RR
21797c478bd9Sstevel@tonic-gate 	 * except that the start point will be adjusted based
21807c478bd9Sstevel@tonic-gate 	 * on the provided start_pip
21817c478bd9Sstevel@tonic-gate 	 */
21827c478bd9Sstevel@tonic-gate 	lbp = sb ? ct->ct_lb : LOAD_BALANCE_RR;
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 	switch (lbp) {
21857c478bd9Sstevel@tonic-gate 	case LOAD_BALANCE_NONE:
21867c478bd9Sstevel@tonic-gate 		/*
21877c478bd9Sstevel@tonic-gate 		 * Load balancing is None  or Alternate path mode
21887c478bd9Sstevel@tonic-gate 		 * Start looking for a online mdi_pathinfo node starting from
21897c478bd9Sstevel@tonic-gate 		 * last known selected path
21907c478bd9Sstevel@tonic-gate 		 */
21917c478bd9Sstevel@tonic-gate 		preferred = 1;
21927c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)ct->ct_path_last;
21937c478bd9Sstevel@tonic-gate 		if (pip == NULL) {
21947c478bd9Sstevel@tonic-gate 			pip = head;
21957c478bd9Sstevel@tonic-gate 		}
21967c478bd9Sstevel@tonic-gate 		start = pip;
21977c478bd9Sstevel@tonic-gate 		do {
21987c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
21997c478bd9Sstevel@tonic-gate 			/*
22007c478bd9Sstevel@tonic-gate 			 * No need to explicitly check if the path is disabled.
22017c478bd9Sstevel@tonic-gate 			 * Since we are checking for state == ONLINE and the
220255e592a2SRandall Ralphs 			 * same variable is used for DISABLE/ENABLE information.
22037c478bd9Sstevel@tonic-gate 			 */
2204ee28b439Scm 			if ((MDI_PI(pip)->pi_state  ==
2205ee28b439Scm 				MDI_PATHINFO_STATE_ONLINE) &&
22067c478bd9Sstevel@tonic-gate 				preferred == MDI_PI(pip)->pi_preferred) {
22077c478bd9Sstevel@tonic-gate 				/*
22087c478bd9Sstevel@tonic-gate 				 * Return the path in hold state. Caller should
22097c478bd9Sstevel@tonic-gate 				 * release the lock by calling mdi_rele_path()
22107c478bd9Sstevel@tonic-gate 				 */
22117c478bd9Sstevel@tonic-gate 				MDI_PI_HOLD(pip);
22127c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
22137c478bd9Sstevel@tonic-gate 				ct->ct_path_last = pip;
22147c478bd9Sstevel@tonic-gate 				*ret_pip = pip;
22157c478bd9Sstevel@tonic-gate 				MDI_CLIENT_UNLOCK(ct);
22167c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
22177c478bd9Sstevel@tonic-gate 			}
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 			/*
22207c478bd9Sstevel@tonic-gate 			 * Path is busy.
22217c478bd9Sstevel@tonic-gate 			 */
22227c478bd9Sstevel@tonic-gate 			if (MDI_PI_IS_DRV_DISABLE_TRANSIENT(pip) ||
22237c478bd9Sstevel@tonic-gate 			    MDI_PI_IS_TRANSIENT(pip))
22247c478bd9Sstevel@tonic-gate 				retry = 1;
22257c478bd9Sstevel@tonic-gate 			/*
22267c478bd9Sstevel@tonic-gate 			 * Keep looking for a next available online path
22277c478bd9Sstevel@tonic-gate 			 */
22287c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
22297c478bd9Sstevel@tonic-gate 			if (next == NULL) {
22307c478bd9Sstevel@tonic-gate 				next = head;
22317c478bd9Sstevel@tonic-gate 			}
22327c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
22337c478bd9Sstevel@tonic-gate 			pip = next;
22347c478bd9Sstevel@tonic-gate 			if (start == pip && preferred) {
22357c478bd9Sstevel@tonic-gate 				preferred = 0;
22367c478bd9Sstevel@tonic-gate 			} else if (start == pip && !preferred) {
22377c478bd9Sstevel@tonic-gate 				cont = 0;
22387c478bd9Sstevel@tonic-gate 			}
22397c478bd9Sstevel@tonic-gate 		} while (cont);
22407c478bd9Sstevel@tonic-gate 		break;
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	case LOAD_BALANCE_LBA:
22437c478bd9Sstevel@tonic-gate 		/*
22447c478bd9Sstevel@tonic-gate 		 * Make sure we are looking
22457c478bd9Sstevel@tonic-gate 		 * for an online path. Otherwise, if it is for a STANDBY
22467c478bd9Sstevel@tonic-gate 		 * path request, it will go through and fetch an ONLINE
22477c478bd9Sstevel@tonic-gate 		 * path which is not desirable.
22487c478bd9Sstevel@tonic-gate 		 */
22497c478bd9Sstevel@tonic-gate 		if ((ct->ct_lb_args != NULL) &&
22507c478bd9Sstevel@tonic-gate 			    (ct->ct_lb_args->region_size) && bp &&
22517c478bd9Sstevel@tonic-gate 				(sb || (flags == MDI_SELECT_ONLINE_PATH))) {
22527c478bd9Sstevel@tonic-gate 			if (i_mdi_lba_lb(ct, ret_pip, bp)
22537c478bd9Sstevel@tonic-gate 				    == MDI_SUCCESS) {
22547c478bd9Sstevel@tonic-gate 				MDI_CLIENT_UNLOCK(ct);
22557c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
22567c478bd9Sstevel@tonic-gate 			}
22577c478bd9Sstevel@tonic-gate 		}
22584c06356bSdh 		/* FALLTHROUGH */
22597c478bd9Sstevel@tonic-gate 	case LOAD_BALANCE_RR:
22607c478bd9Sstevel@tonic-gate 		/*
22617c478bd9Sstevel@tonic-gate 		 * Load balancing is Round Robin. Start looking for a online
22627c478bd9Sstevel@tonic-gate 		 * mdi_pathinfo node starting from last known selected path
22637c478bd9Sstevel@tonic-gate 		 * as the start point.  If override flags are specified,
22647c478bd9Sstevel@tonic-gate 		 * process accordingly.
22657c478bd9Sstevel@tonic-gate 		 * If the search is already in effect(start_pip not null),
22667c478bd9Sstevel@tonic-gate 		 * then lets just use the same path preference to continue the
22677c478bd9Sstevel@tonic-gate 		 * traversal.
22687c478bd9Sstevel@tonic-gate 		 */
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 		if (start_pip != NULL) {
22717c478bd9Sstevel@tonic-gate 			preferred = MDI_PI(start_pip)->pi_preferred;
22727c478bd9Sstevel@tonic-gate 		} else {
22737c478bd9Sstevel@tonic-gate 			preferred = 1;
22747c478bd9Sstevel@tonic-gate 		}
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 		start = sb ? (mdi_pathinfo_t *)ct->ct_path_last : start_pip;
22777c478bd9Sstevel@tonic-gate 		if (start == NULL) {
22787c478bd9Sstevel@tonic-gate 			pip = head;
22797c478bd9Sstevel@tonic-gate 		} else {
22807c478bd9Sstevel@tonic-gate 			pip = (mdi_pathinfo_t *)MDI_PI(start)->pi_client_link;
22817c478bd9Sstevel@tonic-gate 			if (pip == NULL) {
2282b08fdaf7SSheshadri Vasudevan 				if ( flags & MDI_SELECT_NO_PREFERRED) {
2283b08fdaf7SSheshadri Vasudevan 					/*
2284b08fdaf7SSheshadri Vasudevan 					 * Return since we hit the end of list
2285b08fdaf7SSheshadri Vasudevan 					 */
2286b08fdaf7SSheshadri Vasudevan 					MDI_CLIENT_UNLOCK(ct);
2287b08fdaf7SSheshadri Vasudevan 					return (MDI_NOPATH);
2288b08fdaf7SSheshadri Vasudevan 				}
2289b08fdaf7SSheshadri Vasudevan 
22907c478bd9Sstevel@tonic-gate 				if (!sb) {
22917c478bd9Sstevel@tonic-gate 					if (preferred == 0) {
22927c478bd9Sstevel@tonic-gate 						/*
22937c478bd9Sstevel@tonic-gate 						 * Looks like we have completed
22947c478bd9Sstevel@tonic-gate 						 * the traversal as preferred
22957c478bd9Sstevel@tonic-gate 						 * value is 0. Time to bail out.
22967c478bd9Sstevel@tonic-gate 						 */
22977c478bd9Sstevel@tonic-gate 						*ret_pip = NULL;
22987c478bd9Sstevel@tonic-gate 						MDI_CLIENT_UNLOCK(ct);
22997c478bd9Sstevel@tonic-gate 						return (MDI_NOPATH);
23007c478bd9Sstevel@tonic-gate 					} else {
23017c478bd9Sstevel@tonic-gate 						/*
23027c478bd9Sstevel@tonic-gate 						 * Looks like we reached the
23037c478bd9Sstevel@tonic-gate 						 * end of the list. Lets enable
23047c478bd9Sstevel@tonic-gate 						 * traversal of non preferred
23057c478bd9Sstevel@tonic-gate 						 * paths.
23067c478bd9Sstevel@tonic-gate 						 */
23077c478bd9Sstevel@tonic-gate 						preferred = 0;
23087c478bd9Sstevel@tonic-gate 					}
23097c478bd9Sstevel@tonic-gate 				}
23107c478bd9Sstevel@tonic-gate 				pip = head;
23117c478bd9Sstevel@tonic-gate 			}
23127c478bd9Sstevel@tonic-gate 		}
23137c478bd9Sstevel@tonic-gate 		start = pip;
23147c478bd9Sstevel@tonic-gate 		do {
23157c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
23167c478bd9Sstevel@tonic-gate 			if (sb) {
23177c478bd9Sstevel@tonic-gate 				cond = ((MDI_PI(pip)->pi_state ==
23187c478bd9Sstevel@tonic-gate 				    MDI_PATHINFO_STATE_ONLINE &&
23197c478bd9Sstevel@tonic-gate 					MDI_PI(pip)->pi_preferred ==
23207c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
23217c478bd9Sstevel@tonic-gate 			} else {
23227c478bd9Sstevel@tonic-gate 				if (flags == MDI_SELECT_ONLINE_PATH) {
23237c478bd9Sstevel@tonic-gate 					cond = ((MDI_PI(pip)->pi_state ==
23247c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_ONLINE &&
23257c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_preferred ==
23267c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
23277c478bd9Sstevel@tonic-gate 				} else if (flags == MDI_SELECT_STANDBY_PATH) {
23287c478bd9Sstevel@tonic-gate 					cond = ((MDI_PI(pip)->pi_state ==
23297c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_STANDBY &&
23307c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_preferred ==
23317c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
23327c478bd9Sstevel@tonic-gate 				} else if (flags == (MDI_SELECT_ONLINE_PATH |
23337c478bd9Sstevel@tonic-gate 				    MDI_SELECT_STANDBY_PATH)) {
23347c478bd9Sstevel@tonic-gate 					cond = (((MDI_PI(pip)->pi_state ==
23357c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_ONLINE ||
23367c478bd9Sstevel@tonic-gate 					    (MDI_PI(pip)->pi_state ==
23377c478bd9Sstevel@tonic-gate 					    MDI_PATHINFO_STATE_STANDBY)) &&
23387c478bd9Sstevel@tonic-gate 						MDI_PI(pip)->pi_preferred ==
23397c478bd9Sstevel@tonic-gate 						preferred) ? 1 : 0);
2340ee28b439Scm 				} else if (flags ==
2341ee28b439Scm 					(MDI_SELECT_STANDBY_PATH |
2342ee28b439Scm 					MDI_SELECT_ONLINE_PATH |
2343ee28b439Scm 					MDI_SELECT_USER_DISABLE_PATH)) {
2344ee28b439Scm 					cond = (((MDI_PI(pip)->pi_state ==
2345ee28b439Scm 					    MDI_PATHINFO_STATE_ONLINE ||
2346ee28b439Scm 					    (MDI_PI(pip)->pi_state ==
2347ee28b439Scm 					    MDI_PATHINFO_STATE_STANDBY) ||
2348ee28b439Scm 						(MDI_PI(pip)->pi_state ==
2349ee28b439Scm 					    (MDI_PATHINFO_STATE_ONLINE|
2350ee28b439Scm 					    MDI_PATHINFO_STATE_USER_DISABLE)) ||
2351ee28b439Scm 						(MDI_PI(pip)->pi_state ==
2352ee28b439Scm 					    (MDI_PATHINFO_STATE_STANDBY |
2353ee28b439Scm 					    MDI_PATHINFO_STATE_USER_DISABLE)))&&
2354ee28b439Scm 						MDI_PI(pip)->pi_preferred ==
2355ee28b439Scm 						preferred) ? 1 : 0);
2356b08fdaf7SSheshadri Vasudevan 				} else if (flags ==
2357b08fdaf7SSheshadri Vasudevan 				    (MDI_SELECT_STANDBY_PATH |
2358b08fdaf7SSheshadri Vasudevan 				    MDI_SELECT_ONLINE_PATH |
2359b08fdaf7SSheshadri Vasudevan 				    MDI_SELECT_NO_PREFERRED)) {
2360b08fdaf7SSheshadri Vasudevan 					cond = (((MDI_PI(pip)->pi_state ==
2361b08fdaf7SSheshadri Vasudevan 					    MDI_PATHINFO_STATE_ONLINE) ||
2362b08fdaf7SSheshadri Vasudevan 					    (MDI_PI(pip)->pi_state ==
2363b08fdaf7SSheshadri Vasudevan 					    MDI_PATHINFO_STATE_STANDBY))
2364b08fdaf7SSheshadri Vasudevan 					    ? 1 : 0);
23657c478bd9Sstevel@tonic-gate 				} else {
23667c478bd9Sstevel@tonic-gate 					cond = 0;
23677c478bd9Sstevel@tonic-gate 				}
23687c478bd9Sstevel@tonic-gate 			}
23697c478bd9Sstevel@tonic-gate 			/*
23707c478bd9Sstevel@tonic-gate 			 * No need to explicitly check if the path is disabled.
23717c478bd9Sstevel@tonic-gate 			 * Since we are checking for state == ONLINE and the
237255e592a2SRandall Ralphs 			 * same variable is used for DISABLE/ENABLE information.
23737c478bd9Sstevel@tonic-gate 			 */
23747c478bd9Sstevel@tonic-gate 			if (cond) {
23757c478bd9Sstevel@tonic-gate 				/*
23767c478bd9Sstevel@tonic-gate 				 * Return the path in hold state. Caller should
23777c478bd9Sstevel@tonic-gate 				 * release the lock by calling mdi_rele_path()
23787c478bd9Sstevel@tonic-gate 				 */
23797c478bd9Sstevel@tonic-gate 				MDI_PI_HOLD(pip);
23807c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
23817c478bd9Sstevel@tonic-gate 				if (sb)
23827c478bd9Sstevel@tonic-gate 					ct->ct_path_last = pip;
23837c478bd9Sstevel@tonic-gate 				*ret_pip = pip;
23847c478bd9Sstevel@tonic-gate 				MDI_CLIENT_UNLOCK(ct);
23857c478bd9Sstevel@tonic-gate 				return (MDI_SUCCESS);
23867c478bd9Sstevel@tonic-gate 			}
23877c478bd9Sstevel@tonic-gate 			/*
23887c478bd9Sstevel@tonic-gate 			 * Path is busy.
23897c478bd9Sstevel@tonic-gate 			 */
23907c478bd9Sstevel@tonic-gate 			if (MDI_PI_IS_DRV_DISABLE_TRANSIENT(pip) ||
23917c478bd9Sstevel@tonic-gate 			    MDI_PI_IS_TRANSIENT(pip))
23927c478bd9Sstevel@tonic-gate 				retry = 1;
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate 			/*
23957c478bd9Sstevel@tonic-gate 			 * Keep looking for a next available online path
23967c478bd9Sstevel@tonic-gate 			 */
23977c478bd9Sstevel@tonic-gate do_again:
23987c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
23997c478bd9Sstevel@tonic-gate 			if (next == NULL) {
2400b08fdaf7SSheshadri Vasudevan 				if ( flags & MDI_SELECT_NO_PREFERRED) {
2401b08fdaf7SSheshadri Vasudevan 					/*
2402b08fdaf7SSheshadri Vasudevan 					 * Bail out since we hit the end of list
2403b08fdaf7SSheshadri Vasudevan 					 */
2404b08fdaf7SSheshadri Vasudevan 					MDI_PI_UNLOCK(pip);
2405b08fdaf7SSheshadri Vasudevan 					break;
2406b08fdaf7SSheshadri Vasudevan 				}
2407b08fdaf7SSheshadri Vasudevan 
24087c478bd9Sstevel@tonic-gate 				if (!sb) {
24097c478bd9Sstevel@tonic-gate 					if (preferred == 1) {
24107c478bd9Sstevel@tonic-gate 						/*
24117c478bd9Sstevel@tonic-gate 						 * Looks like we reached the
24127c478bd9Sstevel@tonic-gate 						 * end of the list. Lets enable
24137c478bd9Sstevel@tonic-gate 						 * traversal of non preferred
24147c478bd9Sstevel@tonic-gate 						 * paths.
24157c478bd9Sstevel@tonic-gate 						 */
24167c478bd9Sstevel@tonic-gate 						preferred = 0;
24177c478bd9Sstevel@tonic-gate 						next = head;
24187c478bd9Sstevel@tonic-gate 					} else {
24197c478bd9Sstevel@tonic-gate 						/*
24207c478bd9Sstevel@tonic-gate 						 * We have done both the passes
24217c478bd9Sstevel@tonic-gate 						 * Preferred as well as for
24227c478bd9Sstevel@tonic-gate 						 * Non-preferred. Bail out now.
24237c478bd9Sstevel@tonic-gate 						 */
24247c478bd9Sstevel@tonic-gate 						cont = 0;
24257c478bd9Sstevel@tonic-gate 					}
24267c478bd9Sstevel@tonic-gate 				} else {
24277c478bd9Sstevel@tonic-gate 					/*
24287c478bd9Sstevel@tonic-gate 					 * Standard behavior case.
24297c478bd9Sstevel@tonic-gate 					 */
24307c478bd9Sstevel@tonic-gate 					next = head;
24317c478bd9Sstevel@tonic-gate 				}
24327c478bd9Sstevel@tonic-gate 			}
24337c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
24347c478bd9Sstevel@tonic-gate 			if (cont == 0) {
24357c478bd9Sstevel@tonic-gate 				break;
24367c478bd9Sstevel@tonic-gate 			}
24377c478bd9Sstevel@tonic-gate 			pip = next;
24387c478bd9Sstevel@tonic-gate 
24397c478bd9Sstevel@tonic-gate 			if (!sb) {
24407c478bd9Sstevel@tonic-gate 				/*
24417c478bd9Sstevel@tonic-gate 				 * We need to handle the selection of
24427c478bd9Sstevel@tonic-gate 				 * non-preferred path in the following
24437c478bd9Sstevel@tonic-gate 				 * case:
24447c478bd9Sstevel@tonic-gate 				 *
24457c478bd9Sstevel@tonic-gate 				 * +------+   +------+   +------+   +-----+
24467c478bd9Sstevel@tonic-gate 				 * | A : 1| - | B : 1| - | C : 0| - |NULL |
24477c478bd9Sstevel@tonic-gate 				 * +------+   +------+   +------+   +-----+
24487c478bd9Sstevel@tonic-gate 				 *
24497c478bd9Sstevel@tonic-gate 				 * If we start the search with B, we need to
24507c478bd9Sstevel@tonic-gate 				 * skip beyond B to pick C which is non -
24517c478bd9Sstevel@tonic-gate 				 * preferred in the second pass. The following
24527c478bd9Sstevel@tonic-gate 				 * test, if true, will allow us to skip over
24537c478bd9Sstevel@tonic-gate 				 * the 'start'(B in the example) to select
24547c478bd9Sstevel@tonic-gate 				 * other non preferred elements.
24557c478bd9Sstevel@tonic-gate 				 */
24567c478bd9Sstevel@tonic-gate 				if ((start_pip != NULL) && (start_pip == pip) &&
24577c478bd9Sstevel@tonic-gate 				    (MDI_PI(start_pip)->pi_preferred
24587c478bd9Sstevel@tonic-gate 				    != preferred)) {
24597c478bd9Sstevel@tonic-gate 					/*
24607c478bd9Sstevel@tonic-gate 					 * try again after going past the start
24617c478bd9Sstevel@tonic-gate 					 * pip
24627c478bd9Sstevel@tonic-gate 					 */
24637c478bd9Sstevel@tonic-gate 					MDI_PI_LOCK(pip);
24647c478bd9Sstevel@tonic-gate 					goto do_again;
24657c478bd9Sstevel@tonic-gate 				}
24667c478bd9Sstevel@tonic-gate 			} else {
24677c478bd9Sstevel@tonic-gate 				/*
24687c478bd9Sstevel@tonic-gate 				 * Standard behavior case
24697c478bd9Sstevel@tonic-gate 				 */
24707c478bd9Sstevel@tonic-gate 				if (start == pip && preferred) {
24717c478bd9Sstevel@tonic-gate 					/* look for nonpreferred paths */
24727c478bd9Sstevel@tonic-gate 					preferred = 0;
24737c478bd9Sstevel@tonic-gate 				} else if (start == pip && !preferred) {
24747c478bd9Sstevel@tonic-gate 					/*
24757c478bd9Sstevel@tonic-gate 					 * Exit condition
24767c478bd9Sstevel@tonic-gate 					 */
24777c478bd9Sstevel@tonic-gate 					cont = 0;
24787c478bd9Sstevel@tonic-gate 				}
24797c478bd9Sstevel@tonic-gate 			}
24807c478bd9Sstevel@tonic-gate 		} while (cont);
24817c478bd9Sstevel@tonic-gate 		break;
24827c478bd9Sstevel@tonic-gate 	}
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
24857c478bd9Sstevel@tonic-gate 	if (retry == 1) {
24867c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
24877c478bd9Sstevel@tonic-gate 	} else {
24887c478bd9Sstevel@tonic-gate 		return (MDI_NOPATH);
24897c478bd9Sstevel@tonic-gate 	}
24907c478bd9Sstevel@tonic-gate }
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate /*
24937c478bd9Sstevel@tonic-gate  * For a client, return the next available path to any phci
24947c478bd9Sstevel@tonic-gate  *
24957c478bd9Sstevel@tonic-gate  * Note:
24967c478bd9Sstevel@tonic-gate  *		Caller should hold the branch's devinfo node to get a consistent
24977c478bd9Sstevel@tonic-gate  *		snap shot of the mdi_pathinfo nodes.
24987c478bd9Sstevel@tonic-gate  *
24997c478bd9Sstevel@tonic-gate  *		Please note that even the list is stable the mdi_pathinfo
25007c478bd9Sstevel@tonic-gate  *		node state and properties are volatile.  The caller should lock
25017c478bd9Sstevel@tonic-gate  *		and unlock the nodes by calling mdi_pi_lock() and
25027c478bd9Sstevel@tonic-gate  *		mdi_pi_unlock() functions to get a stable properties.
25037c478bd9Sstevel@tonic-gate  *
25047c478bd9Sstevel@tonic-gate  *		If there is a need to use the nodes beyond the hold of the
25057c478bd9Sstevel@tonic-gate  *		devinfo node period (For ex. I/O), then mdi_pathinfo node
25067c478bd9Sstevel@tonic-gate  *		need to be held against unexpected removal by calling
25077c478bd9Sstevel@tonic-gate  *		mdi_hold_path() and should be released by calling
25087c478bd9Sstevel@tonic-gate  *		mdi_rele_path() on completion.
25097c478bd9Sstevel@tonic-gate  */
25107c478bd9Sstevel@tonic-gate mdi_pathinfo_t *
mdi_get_next_phci_path(dev_info_t * ct_dip,mdi_pathinfo_t * pip)25117c478bd9Sstevel@tonic-gate mdi_get_next_phci_path(dev_info_t *ct_dip, mdi_pathinfo_t *pip)
25127c478bd9Sstevel@tonic-gate {
25137c478bd9Sstevel@tonic-gate 	mdi_client_t *ct;
25147c478bd9Sstevel@tonic-gate 
25157c478bd9Sstevel@tonic-gate 	if (!MDI_CLIENT(ct_dip))
25167c478bd9Sstevel@tonic-gate 		return (NULL);
25177c478bd9Sstevel@tonic-gate 
25187c478bd9Sstevel@tonic-gate 	/*
25197c478bd9Sstevel@tonic-gate 	 * Walk through client link
25207c478bd9Sstevel@tonic-gate 	 */
25217c478bd9Sstevel@tonic-gate 	ct = (mdi_client_t *)DEVI(ct_dip)->devi_mdi_client;
25227c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate 	if (pip == NULL)
25257c478bd9Sstevel@tonic-gate 		return ((mdi_pathinfo_t *)ct->ct_path_head);
25267c478bd9Sstevel@tonic-gate 
25277c478bd9Sstevel@tonic-gate 	return ((mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link);
25287c478bd9Sstevel@tonic-gate }
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate /*
25317c478bd9Sstevel@tonic-gate  * For a phci, return the next available path to any client
25327c478bd9Sstevel@tonic-gate  * Note: ditto mdi_get_next_phci_path()
25337c478bd9Sstevel@tonic-gate  */
25347c478bd9Sstevel@tonic-gate mdi_pathinfo_t *
mdi_get_next_client_path(dev_info_t * ph_dip,mdi_pathinfo_t * pip)25357c478bd9Sstevel@tonic-gate mdi_get_next_client_path(dev_info_t *ph_dip, mdi_pathinfo_t *pip)
25367c478bd9Sstevel@tonic-gate {
25377c478bd9Sstevel@tonic-gate 	mdi_phci_t *ph;
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 	if (!MDI_PHCI(ph_dip))
25407c478bd9Sstevel@tonic-gate 		return (NULL);
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	/*
25437c478bd9Sstevel@tonic-gate 	 * Walk through pHCI link
25447c478bd9Sstevel@tonic-gate 	 */
25457c478bd9Sstevel@tonic-gate 	ph = (mdi_phci_t *)DEVI(ph_dip)->devi_mdi_xhci;
25467c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate 	if (pip == NULL)
25497c478bd9Sstevel@tonic-gate 		return ((mdi_pathinfo_t *)ph->ph_path_head);
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	return ((mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link);
25527c478bd9Sstevel@tonic-gate }
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate /*
25557c478bd9Sstevel@tonic-gate  * mdi_hold_path():
25567c478bd9Sstevel@tonic-gate  *		Hold the mdi_pathinfo node against unwanted unexpected free.
25577c478bd9Sstevel@tonic-gate  * Return Values:
25587c478bd9Sstevel@tonic-gate  *		None
25597c478bd9Sstevel@tonic-gate  */
25607c478bd9Sstevel@tonic-gate void
mdi_hold_path(mdi_pathinfo_t * pip)25617c478bd9Sstevel@tonic-gate mdi_hold_path(mdi_pathinfo_t *pip)
25627c478bd9Sstevel@tonic-gate {
25637c478bd9Sstevel@tonic-gate 	if (pip) {
25647c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
25657c478bd9Sstevel@tonic-gate 		MDI_PI_HOLD(pip);
25667c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
25677c478bd9Sstevel@tonic-gate 	}
25687c478bd9Sstevel@tonic-gate }
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate /*
25727c478bd9Sstevel@tonic-gate  * mdi_rele_path():
25737c478bd9Sstevel@tonic-gate  *		Release the mdi_pathinfo node which was selected
25747c478bd9Sstevel@tonic-gate  *		through mdi_select_path() mechanism or manually held by
25757c478bd9Sstevel@tonic-gate  *		calling mdi_hold_path().
25767c478bd9Sstevel@tonic-gate  * Return Values:
25777c478bd9Sstevel@tonic-gate  *		None
25787c478bd9Sstevel@tonic-gate  */
25797c478bd9Sstevel@tonic-gate void
mdi_rele_path(mdi_pathinfo_t * pip)25807c478bd9Sstevel@tonic-gate mdi_rele_path(mdi_pathinfo_t *pip)
25817c478bd9Sstevel@tonic-gate {
25827c478bd9Sstevel@tonic-gate 	if (pip) {
25837c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
25847c478bd9Sstevel@tonic-gate 		MDI_PI_RELE(pip);
25857c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_ref_cnt == 0) {
25867c478bd9Sstevel@tonic-gate 			cv_broadcast(&MDI_PI(pip)->pi_ref_cv);
25877c478bd9Sstevel@tonic-gate 		}
25887c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
25897c478bd9Sstevel@tonic-gate 	}
25907c478bd9Sstevel@tonic-gate }
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate /*
25937c478bd9Sstevel@tonic-gate  * mdi_pi_lock():
2594d27aea3fSDan Cross  *		Lock the mdi_pathinfo node.
25957c478bd9Sstevel@tonic-gate  * Note:
25967c478bd9Sstevel@tonic-gate  *		The caller should release the lock by calling mdi_pi_unlock()
25977c478bd9Sstevel@tonic-gate  */
25987c478bd9Sstevel@tonic-gate void
mdi_pi_lock(mdi_pathinfo_t * pip)25997c478bd9Sstevel@tonic-gate mdi_pi_lock(mdi_pathinfo_t *pip)
26007c478bd9Sstevel@tonic-gate {
26017c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
26027c478bd9Sstevel@tonic-gate 	if (pip) {
26037c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
26047c478bd9Sstevel@tonic-gate 	}
26057c478bd9Sstevel@tonic-gate }
26067c478bd9Sstevel@tonic-gate 
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate /*
26097c478bd9Sstevel@tonic-gate  * mdi_pi_unlock():
2610d27aea3fSDan Cross  *		Unlock the mdi_pathinfo node.
26117c478bd9Sstevel@tonic-gate  * Note:
26127c478bd9Sstevel@tonic-gate  *		The mdi_pathinfo node should have been locked with mdi_pi_lock()
26137c478bd9Sstevel@tonic-gate  */
26147c478bd9Sstevel@tonic-gate void
mdi_pi_unlock(mdi_pathinfo_t * pip)26157c478bd9Sstevel@tonic-gate mdi_pi_unlock(mdi_pathinfo_t *pip)
26167c478bd9Sstevel@tonic-gate {
26177c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
26187c478bd9Sstevel@tonic-gate 	if (pip) {
26197c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
26207c478bd9Sstevel@tonic-gate 	}
26217c478bd9Sstevel@tonic-gate }
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate /*
26247c478bd9Sstevel@tonic-gate  * mdi_pi_find():
26257c478bd9Sstevel@tonic-gate  *		Search the list of mdi_pathinfo nodes attached to the
26267c478bd9Sstevel@tonic-gate  *		pHCI/Client device node whose path address matches "paddr".
26277c478bd9Sstevel@tonic-gate  *		Returns a pointer to the mdi_pathinfo node if a matching node is
26287c478bd9Sstevel@tonic-gate  *		found.
26297c478bd9Sstevel@tonic-gate  * Return Values:
26307c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node handle
26317c478bd9Sstevel@tonic-gate  *		NULL
26327c478bd9Sstevel@tonic-gate  * Notes:
26337c478bd9Sstevel@tonic-gate  *		Caller need not hold any locks to call this function.
26347c478bd9Sstevel@tonic-gate  */
26357c478bd9Sstevel@tonic-gate mdi_pathinfo_t *
mdi_pi_find(dev_info_t * pdip,char * caddr,char * paddr)26367c478bd9Sstevel@tonic-gate mdi_pi_find(dev_info_t *pdip, char *caddr, char *paddr)
26377c478bd9Sstevel@tonic-gate {
26387c478bd9Sstevel@tonic-gate 	mdi_phci_t		*ph;
26397c478bd9Sstevel@tonic-gate 	mdi_vhci_t		*vh;
26407c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
26417c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t		*pip = NULL;
26427c478bd9Sstevel@tonic-gate 
26434c06356bSdh 	MDI_DEBUG(2, (MDI_NOTE, pdip,
26444c06356bSdh 	    "caddr@%s paddr@%s", caddr ? caddr : "", paddr ? paddr : ""));
26457c478bd9Sstevel@tonic-gate 	if ((pdip == NULL) || (paddr == NULL)) {
26467c478bd9Sstevel@tonic-gate 		return (NULL);
26477c478bd9Sstevel@tonic-gate 	}
26487c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
26497c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
26507c478bd9Sstevel@tonic-gate 		/*
26517c478bd9Sstevel@tonic-gate 		 * Invalid pHCI device, Nothing more to do.
26527c478bd9Sstevel@tonic-gate 		 */
26534c06356bSdh 		MDI_DEBUG(2, (MDI_WARN, pdip, "invalid phci"));
26547c478bd9Sstevel@tonic-gate 		return (NULL);
26557c478bd9Sstevel@tonic-gate 	}
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
26587c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
26597c478bd9Sstevel@tonic-gate 		/*
26607c478bd9Sstevel@tonic-gate 		 * Invalid vHCI device, Nothing more to do.
26617c478bd9Sstevel@tonic-gate 		 */
26624c06356bSdh 		MDI_DEBUG(2, (MDI_WARN, pdip, "invalid vhci"));
26637c478bd9Sstevel@tonic-gate 		return (NULL);
26647c478bd9Sstevel@tonic-gate 	}
26657c478bd9Sstevel@tonic-gate 
26667c478bd9Sstevel@tonic-gate 	/*
26675e3986cbScth 	 * Look for pathinfo node identified by paddr.
26687c478bd9Sstevel@tonic-gate 	 */
26697c478bd9Sstevel@tonic-gate 	if (caddr == NULL) {
26707c478bd9Sstevel@tonic-gate 		/*
26717c478bd9Sstevel@tonic-gate 		 * Find a mdi_pathinfo node under pHCI list for a matching
26727c478bd9Sstevel@tonic-gate 		 * unit address.
26737c478bd9Sstevel@tonic-gate 		 */
26745e3986cbScth 		MDI_PHCI_LOCK(ph);
26755e3986cbScth 		if (MDI_PHCI_IS_OFFLINE(ph)) {
26764c06356bSdh 			MDI_DEBUG(2, (MDI_WARN, pdip,
26774c06356bSdh 			    "offline phci %p", (void *)ph));
26785e3986cbScth 			MDI_PHCI_UNLOCK(ph);
26795e3986cbScth 			return (NULL);
26805e3986cbScth 		}
26817c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)ph->ph_path_head;
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
26847c478bd9Sstevel@tonic-gate 			if (strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
26857c478bd9Sstevel@tonic-gate 				break;
26867c478bd9Sstevel@tonic-gate 			}
26877c478bd9Sstevel@tonic-gate 			pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
26887c478bd9Sstevel@tonic-gate 		}
26895e3986cbScth 		MDI_PHCI_UNLOCK(ph);
26904c06356bSdh 		MDI_DEBUG(2, (MDI_NOTE, pdip,
26914c06356bSdh 		    "found %s %p", mdi_pi_spathname(pip), (void *)pip));
26927c478bd9Sstevel@tonic-gate 		return (pip);
26937c478bd9Sstevel@tonic-gate 	}
26947c478bd9Sstevel@tonic-gate 
26953c34adc5Sramat 	/*
26963c34adc5Sramat 	 * XXX - Is the rest of the code in this function really necessary?
26973c34adc5Sramat 	 * The consumers of mdi_pi_find() can search for the desired pathinfo
26983c34adc5Sramat 	 * node by calling mdi_pi_find(pdip, NULL, paddr). Irrespective of
26993c34adc5Sramat 	 * whether the search is based on the pathinfo nodes attached to
27003c34adc5Sramat 	 * the pHCI or the client node, the result will be the same.
27013c34adc5Sramat 	 */
27023c34adc5Sramat 
27037c478bd9Sstevel@tonic-gate 	/*
27047c478bd9Sstevel@tonic-gate 	 * Find the client device corresponding to 'caddr'
27057c478bd9Sstevel@tonic-gate 	 */
27065e3986cbScth 	MDI_VHCI_CLIENT_LOCK(vh);
27073c34adc5Sramat 
27083c34adc5Sramat 	/*
27093c34adc5Sramat 	 * XXX - Passing NULL to the following function works as long as the
27103c34adc5Sramat 	 * the client addresses (caddr) are unique per vhci basis.
27113c34adc5Sramat 	 */
27123c34adc5Sramat 	ct = i_mdi_client_find(vh, NULL, caddr);
27137c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
27147c478bd9Sstevel@tonic-gate 		/*
27157c478bd9Sstevel@tonic-gate 		 * Client not found, Obviously mdi_pathinfo node has not been
27167c478bd9Sstevel@tonic-gate 		 * created yet.
27177c478bd9Sstevel@tonic-gate 		 */
27185e3986cbScth 		MDI_VHCI_CLIENT_UNLOCK(vh);
27194c06356bSdh 		MDI_DEBUG(2, (MDI_NOTE, pdip,
27204c06356bSdh 		    "client not found for caddr @%s", caddr ? caddr : ""));
27215e3986cbScth 		return (NULL);
27227c478bd9Sstevel@tonic-gate 	}
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 	/*
27257c478bd9Sstevel@tonic-gate 	 * Hold the client lock and look for a mdi_pathinfo node with matching
27267c478bd9Sstevel@tonic-gate 	 * pHCI and paddr
27277c478bd9Sstevel@tonic-gate 	 */
27287c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
27297c478bd9Sstevel@tonic-gate 
27307c478bd9Sstevel@tonic-gate 	/*
27317c478bd9Sstevel@tonic-gate 	 * Release the global mutex as it is no more needed. Note: We always
27327c478bd9Sstevel@tonic-gate 	 * respect the locking order while acquiring.
27337c478bd9Sstevel@tonic-gate 	 */
27345e3986cbScth 	MDI_VHCI_CLIENT_UNLOCK(vh);
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
27377c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
27387c478bd9Sstevel@tonic-gate 		/*
27397c478bd9Sstevel@tonic-gate 		 * Compare the unit address
27407c478bd9Sstevel@tonic-gate 		 */
27417c478bd9Sstevel@tonic-gate 		if ((MDI_PI(pip)->pi_phci == ph) &&
27427c478bd9Sstevel@tonic-gate 		    strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
27437c478bd9Sstevel@tonic-gate 			break;
27447c478bd9Sstevel@tonic-gate 		}
27457c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
27467c478bd9Sstevel@tonic-gate 	}
27477c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
27484c06356bSdh 	MDI_DEBUG(2, (MDI_NOTE, pdip,
27494c06356bSdh 	    "found: %s %p", mdi_pi_spathname(pip), (void *)pip));
27507c478bd9Sstevel@tonic-gate 	return (pip);
27517c478bd9Sstevel@tonic-gate }
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate /*
27547c478bd9Sstevel@tonic-gate  * mdi_pi_alloc():
27557c478bd9Sstevel@tonic-gate  *		Allocate and initialize a new instance of a mdi_pathinfo node.
27567c478bd9Sstevel@tonic-gate  *		The mdi_pathinfo node returned by this function identifies a
27577c478bd9Sstevel@tonic-gate  *		unique device path is capable of having properties attached
27587c478bd9Sstevel@tonic-gate  *		and passed to mdi_pi_online() to fully attach and online the
27597c478bd9Sstevel@tonic-gate  *		path and client device node.
27607c478bd9Sstevel@tonic-gate  *		The mdi_pathinfo node returned by this function must be
27617c478bd9Sstevel@tonic-gate  *		destroyed using mdi_pi_free() if the path is no longer
27627c478bd9Sstevel@tonic-gate  *		operational or if the caller fails to attach a client device
27637c478bd9Sstevel@tonic-gate  *		node when calling mdi_pi_online(). The framework will not free
27647c478bd9Sstevel@tonic-gate  *		the resources allocated.
27657c478bd9Sstevel@tonic-gate  *		This function can be called from both interrupt and kernel
27667c478bd9Sstevel@tonic-gate  *		contexts.  DDI_NOSLEEP flag should be used while calling
27677c478bd9Sstevel@tonic-gate  *		from interrupt contexts.
27687c478bd9Sstevel@tonic-gate  * Return Values:
27697c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
27707c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
27717c478bd9Sstevel@tonic-gate  *		MDI_NOMEM
27727c478bd9Sstevel@tonic-gate  */
27737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
27747c478bd9Sstevel@tonic-gate int
mdi_pi_alloc_compatible(dev_info_t * pdip,char * cname,char * caddr,char * paddr,char ** compatible,int ncompatible,int flags,mdi_pathinfo_t ** ret_pip)27757c478bd9Sstevel@tonic-gate mdi_pi_alloc_compatible(dev_info_t *pdip, char *cname, char *caddr, char *paddr,
27767c478bd9Sstevel@tonic-gate     char **compatible, int ncompatible, int flags, mdi_pathinfo_t **ret_pip)
27777c478bd9Sstevel@tonic-gate {
27787c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
27797c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
27807c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
27817c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip = NULL;
27827c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
27837c478bd9Sstevel@tonic-gate 	int		rv = MDI_NOMEM;
27843c34adc5Sramat 	int		path_allocated = 0;
27857c478bd9Sstevel@tonic-gate 
27864c06356bSdh 	MDI_DEBUG(2, (MDI_NOTE, pdip,
27874c06356bSdh 	    "cname %s: caddr@%s paddr@%s",
27884c06356bSdh 	    cname ? cname : "", caddr ? caddr : "", paddr ? paddr : ""));
27895e3986cbScth 
27907c478bd9Sstevel@tonic-gate 	if (pdip == NULL || cname == NULL || caddr == NULL || paddr == NULL ||
27917c478bd9Sstevel@tonic-gate 	    ret_pip == NULL) {
27927c478bd9Sstevel@tonic-gate 		/* Nothing more to do */
27937c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
27947c478bd9Sstevel@tonic-gate 	}
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate 	*ret_pip = NULL;
27975e3986cbScth 
27985e3986cbScth 	/* No allocations on detaching pHCI */
27995e3986cbScth 	if (DEVI_IS_DETACHING(pdip)) {
28005e3986cbScth 		/* Invalid pHCI device, return failure */
28014c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, pdip,
28024c06356bSdh 		    "!detaching pHCI=%p", (void *)pdip));
28035e3986cbScth 		return (MDI_FAILURE);
28045e3986cbScth 	}
28055e3986cbScth 
28067c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
28077c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
28087c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
28097c478bd9Sstevel@tonic-gate 		/* Invalid pHCI device, return failure */
28104c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, pdip,
28114c06356bSdh 		    "!invalid pHCI=%p", (void *)pdip));
28127c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
28137c478bd9Sstevel@tonic-gate 	}
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
28167c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
28177c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
28187c478bd9Sstevel@tonic-gate 		/* Invalid vHCI device, return failure */
28194c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, pdip,
28204c06356bSdh 		    "!invalid vHCI=%p", (void *)pdip));
28217c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
28227c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
28237c478bd9Sstevel@tonic-gate 	}
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_READY(ph) == 0) {
28267c478bd9Sstevel@tonic-gate 		/*
28277c478bd9Sstevel@tonic-gate 		 * Do not allow new node creation when pHCI is in
28287c478bd9Sstevel@tonic-gate 		 * offline/suspended states
28297c478bd9Sstevel@tonic-gate 		 */
28304c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, pdip,
28314c06356bSdh 		    "pHCI=%p is not ready", (void *)ph));
28327c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
28337c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
28347c478bd9Sstevel@tonic-gate 	}
28357c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNSTABLE(ph);
28367c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
28377c478bd9Sstevel@tonic-gate 
28383c34adc5Sramat 	/* look for a matching client, create one if not found */
28395e3986cbScth 	MDI_VHCI_CLIENT_LOCK(vh);
28403c34adc5Sramat 	ct = i_mdi_client_find(vh, cname, caddr);
28417c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
28423c34adc5Sramat 		ct = i_mdi_client_alloc(vh, cname, caddr);
28433c34adc5Sramat 		ASSERT(ct != NULL);
28447c478bd9Sstevel@tonic-gate 	}
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate 	if (ct->ct_dip == NULL) {
28477c478bd9Sstevel@tonic-gate 		/*
28487c478bd9Sstevel@tonic-gate 		 * Allocate a devinfo node
28497c478bd9Sstevel@tonic-gate 		 */
28507c478bd9Sstevel@tonic-gate 		ct->ct_dip = i_mdi_devinfo_create(vh, cname, caddr,
28513c34adc5Sramat 		    compatible, ncompatible);
28527c478bd9Sstevel@tonic-gate 		if (ct->ct_dip == NULL) {
28537c478bd9Sstevel@tonic-gate 			(void) i_mdi_client_free(vh, ct);
28547c478bd9Sstevel@tonic-gate 			goto fail;
28557c478bd9Sstevel@tonic-gate 		}
28567c478bd9Sstevel@tonic-gate 	}
28577c478bd9Sstevel@tonic-gate 	cdip = ct->ct_dip;
28587c478bd9Sstevel@tonic-gate 
28597c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_component |= MDI_COMPONENT_CLIENT;
28607c478bd9Sstevel@tonic-gate 	DEVI(cdip)->devi_mdi_client = (caddr_t)ct;
28617c478bd9Sstevel@tonic-gate 
28625e3986cbScth 	MDI_CLIENT_LOCK(ct);
28637c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
28647c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
28657c478bd9Sstevel@tonic-gate 		/*
28667c478bd9Sstevel@tonic-gate 		 * Compare the unit address
28677c478bd9Sstevel@tonic-gate 		 */
28687c478bd9Sstevel@tonic-gate 		if ((MDI_PI(pip)->pi_phci == ph) &&
28697c478bd9Sstevel@tonic-gate 		    strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
28707c478bd9Sstevel@tonic-gate 			break;
28717c478bd9Sstevel@tonic-gate 		}
28727c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
28737c478bd9Sstevel@tonic-gate 	}
28745e3986cbScth 	MDI_CLIENT_UNLOCK(ct);
28757c478bd9Sstevel@tonic-gate 
28767c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
28777c478bd9Sstevel@tonic-gate 		/*
28787c478bd9Sstevel@tonic-gate 		 * This is a new path for this client device.  Allocate and
28797c478bd9Sstevel@tonic-gate 		 * initialize a new pathinfo node
28807c478bd9Sstevel@tonic-gate 		 */
28813c34adc5Sramat 		pip = i_mdi_pi_alloc(ph, paddr, ct);
28823c34adc5Sramat 		ASSERT(pip != NULL);
28833c34adc5Sramat 		path_allocated = 1;
28847c478bd9Sstevel@tonic-gate 	}
28857c478bd9Sstevel@tonic-gate 	rv = MDI_SUCCESS;
28867c478bd9Sstevel@tonic-gate 
28877c478bd9Sstevel@tonic-gate fail:
28887c478bd9Sstevel@tonic-gate 	/*
28897c478bd9Sstevel@tonic-gate 	 * Release the global mutex.
28907c478bd9Sstevel@tonic-gate 	 */
28915e3986cbScth 	MDI_VHCI_CLIENT_UNLOCK(vh);
28927c478bd9Sstevel@tonic-gate 
28937c478bd9Sstevel@tonic-gate 	/*
28947c478bd9Sstevel@tonic-gate 	 * Mark the pHCI as stable
28957c478bd9Sstevel@tonic-gate 	 */
28967c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
28977c478bd9Sstevel@tonic-gate 	MDI_PHCI_STABLE(ph);
28987c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
28997c478bd9Sstevel@tonic-gate 	*ret_pip = pip;
29003c34adc5Sramat 
29014c06356bSdh 	MDI_DEBUG(2, (MDI_NOTE, pdip,
29024c06356bSdh 	    "alloc %s %p", mdi_pi_spathname(pip), (void *)pip));
29035e3986cbScth 
29043c34adc5Sramat 	if (path_allocated)
29053c34adc5Sramat 		vhcache_pi_add(vh->vh_config, MDI_PI(pip));
29063c34adc5Sramat 
29077c478bd9Sstevel@tonic-gate 	return (rv);
29087c478bd9Sstevel@tonic-gate }
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate /*ARGSUSED*/
29117c478bd9Sstevel@tonic-gate int
mdi_pi_alloc(dev_info_t * pdip,char * cname,char * caddr,char * paddr,int flags,mdi_pathinfo_t ** ret_pip)29127c478bd9Sstevel@tonic-gate mdi_pi_alloc(dev_info_t *pdip, char *cname, char *caddr, char *paddr,
29137c478bd9Sstevel@tonic-gate     int flags, mdi_pathinfo_t **ret_pip)
29147c478bd9Sstevel@tonic-gate {
29157c478bd9Sstevel@tonic-gate 	return (mdi_pi_alloc_compatible(pdip, cname, caddr, paddr, NULL, 0,
29167c478bd9Sstevel@tonic-gate 	    flags, ret_pip));
29177c478bd9Sstevel@tonic-gate }
29187c478bd9Sstevel@tonic-gate 
29197c478bd9Sstevel@tonic-gate /*
29207c478bd9Sstevel@tonic-gate  * i_mdi_pi_alloc():
29217c478bd9Sstevel@tonic-gate  *		Allocate a mdi_pathinfo node and add to the pHCI path list
29227c478bd9Sstevel@tonic-gate  * Return Values:
29237c478bd9Sstevel@tonic-gate  *		mdi_pathinfo
29247c478bd9Sstevel@tonic-gate  */
29257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
29267c478bd9Sstevel@tonic-gate static mdi_pathinfo_t *
i_mdi_pi_alloc(mdi_phci_t * ph,char * paddr,mdi_client_t * ct)29273c34adc5Sramat i_mdi_pi_alloc(mdi_phci_t *ph, char *paddr, mdi_client_t *ct)
29287c478bd9Sstevel@tonic-gate {
29293c34adc5Sramat 	mdi_pathinfo_t	*pip;
29304c06356bSdh 	static char	path[MAXPATHLEN];	/* mdi_pathmap_mutex protects */
2931602ca9eaScth 	char		*path_persistent;
2932602ca9eaScth 	int		path_instance;
2933602ca9eaScth 	mod_hash_val_t	hv;
29347c478bd9Sstevel@tonic-gate 
29355e3986cbScth 	ASSERT(MDI_VHCI_CLIENT_LOCKED(ph->ph_vhci));
29365e3986cbScth 
29373c34adc5Sramat 	pip = kmem_zalloc(sizeof (struct mdi_pathinfo), KM_SLEEP);
29387c478bd9Sstevel@tonic-gate 	mutex_init(&MDI_PI(pip)->pi_mutex, NULL, MUTEX_DEFAULT, NULL);
29397c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_state = MDI_PATHINFO_STATE_INIT |
29407c478bd9Sstevel@tonic-gate 	    MDI_PATHINFO_STATE_TRANSIENT;
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_USER_DISABLED(ph))
29437c478bd9Sstevel@tonic-gate 		MDI_PI_SET_USER_DISABLE(pip);
29447c478bd9Sstevel@tonic-gate 
29457c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_DRV_DISABLED_TRANSIENT(ph))
29467c478bd9Sstevel@tonic-gate 		MDI_PI_SET_DRV_DISABLE_TRANS(pip);
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_DRV_DISABLED(ph))
29497c478bd9Sstevel@tonic-gate 		MDI_PI_SET_DRV_DISABLE(pip);
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_old_state = MDI_PATHINFO_STATE_INIT;
29527c478bd9Sstevel@tonic-gate 	cv_init(&MDI_PI(pip)->pi_state_cv, NULL, CV_DEFAULT, NULL);
29537c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client = ct;
29547c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci = ph;
29553c34adc5Sramat 	MDI_PI(pip)->pi_addr = kmem_alloc(strlen(paddr) + 1, KM_SLEEP);
29567c478bd9Sstevel@tonic-gate 	(void) strcpy(MDI_PI(pip)->pi_addr, paddr);
2957602ca9eaScth 
2958602ca9eaScth         /*
2959602ca9eaScth 	 * We form the "path" to the pathinfo node, and see if we have
2960602ca9eaScth 	 * already allocated a 'path_instance' for that "path".  If so,
2961602ca9eaScth 	 * we use the already allocated 'path_instance'.  If not, we
2962602ca9eaScth 	 * allocate a new 'path_instance' and associate it with a copy of
2963602ca9eaScth 	 * the "path" string (which is never freed). The association
2964602ca9eaScth 	 * between a 'path_instance' this "path" string persists until
2965602ca9eaScth 	 * reboot.
2966602ca9eaScth 	 */
2967602ca9eaScth         mutex_enter(&mdi_pathmap_mutex);
2968602ca9eaScth 	(void) ddi_pathname(ph->ph_dip, path);
29694c06356bSdh 	(void) sprintf(path + strlen(path), "/%s@%s",
297055e592a2SRandall Ralphs 	    mdi_pi_get_node_name(pip), mdi_pi_get_addr(pip));
2971602ca9eaScth         if (mod_hash_find(mdi_pathmap_bypath, (mod_hash_key_t)path, &hv) == 0) {
2972602ca9eaScth                 path_instance = (uint_t)(intptr_t)hv;
2973602ca9eaScth         } else {
2974602ca9eaScth 		/* allocate a new 'path_instance' and persistent "path" */
2975602ca9eaScth 		path_instance = mdi_pathmap_instance++;
2976602ca9eaScth 		path_persistent = i_ddi_strdup(path, KM_SLEEP);
2977602ca9eaScth                 (void) mod_hash_insert(mdi_pathmap_bypath,
2978602ca9eaScth                     (mod_hash_key_t)path_persistent,
2979602ca9eaScth                     (mod_hash_val_t)(intptr_t)path_instance);
2980602ca9eaScth 		(void) mod_hash_insert(mdi_pathmap_byinstance,
2981602ca9eaScth 		    (mod_hash_key_t)(intptr_t)path_instance,
2982602ca9eaScth 		    (mod_hash_val_t)path_persistent);
29834c06356bSdh 
29844c06356bSdh 		/* create shortpath name */
29854c06356bSdh 		(void) snprintf(path, sizeof(path), "%s%d/%s@%s",
29864c06356bSdh 		    ddi_driver_name(ph->ph_dip), ddi_get_instance(ph->ph_dip),
29874c06356bSdh 		    mdi_pi_get_node_name(pip), mdi_pi_get_addr(pip));
29884c06356bSdh 		path_persistent = i_ddi_strdup(path, KM_SLEEP);
29894c06356bSdh 		(void) mod_hash_insert(mdi_pathmap_sbyinstance,
29904c06356bSdh 		    (mod_hash_key_t)(intptr_t)path_instance,
29914c06356bSdh 		    (mod_hash_val_t)path_persistent);
2992602ca9eaScth         }
2993602ca9eaScth         mutex_exit(&mdi_pathmap_mutex);
2994602ca9eaScth 	MDI_PI(pip)->pi_path_instance = path_instance;
2995602ca9eaScth 
29963c34adc5Sramat 	(void) nvlist_alloc(&MDI_PI(pip)->pi_prop, NV_UNIQUE_NAME, KM_SLEEP);
29973c34adc5Sramat 	ASSERT(MDI_PI(pip)->pi_prop != NULL);
29987c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_pprivate = NULL;
29997c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_cprivate = NULL;
30007c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_vprivate = NULL;
30017c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client_link = NULL;
30027c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci_link = NULL;
30037c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_ref_cnt = 0;
30047c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_kstats = NULL;
30057c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_preferred = 1;
30067c478bd9Sstevel@tonic-gate 	cv_init(&MDI_PI(pip)->pi_ref_cv, NULL, CV_DEFAULT, NULL);
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate 	/*
30097c478bd9Sstevel@tonic-gate 	 * Lock both dev_info nodes against changes in parallel.
30105e3986cbScth 	 *
30115e3986cbScth 	 * The ndi_devi_enter(Client), is atypical since the client is a leaf.
30125e3986cbScth 	 * This atypical operation is done to synchronize pathinfo nodes
30135e3986cbScth 	 * during devinfo snapshot (see di_register_pip) by 'pretending' that
30145e3986cbScth 	 * the pathinfo nodes are children of the Client.
30157c478bd9Sstevel@tonic-gate 	 */
30163fe80ca4SDan Cross 	ndi_devi_enter(ct->ct_dip);
30173fe80ca4SDan Cross 	ndi_devi_enter(ph->ph_dip);
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate 	i_mdi_phci_add_path(ph, pip);
30207c478bd9Sstevel@tonic-gate 	i_mdi_client_add_path(ct, pip);
30217c478bd9Sstevel@tonic-gate 
30223fe80ca4SDan Cross 	ndi_devi_exit(ph->ph_dip);
30233fe80ca4SDan Cross 	ndi_devi_exit(ct->ct_dip);
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate 	return (pip);
30267c478bd9Sstevel@tonic-gate }
30277c478bd9Sstevel@tonic-gate 
3028602ca9eaScth /*
3029602ca9eaScth  * mdi_pi_pathname_by_instance():
3030602ca9eaScth  *	Lookup of "path" by 'path_instance'. Return "path".
3031602ca9eaScth  *	NOTE: returned "path" remains valid forever (until reboot).
3032602ca9eaScth  */
3033602ca9eaScth char *
mdi_pi_pathname_by_instance(int path_instance)3034602ca9eaScth mdi_pi_pathname_by_instance(int path_instance)
3035602ca9eaScth {
3036602ca9eaScth 	char		*path;
3037602ca9eaScth 	mod_hash_val_t	hv;
3038602ca9eaScth 
3039602ca9eaScth 	/* mdi_pathmap lookup of "path" by 'path_instance' */
3040602ca9eaScth 	mutex_enter(&mdi_pathmap_mutex);
3041602ca9eaScth 	if (mod_hash_find(mdi_pathmap_byinstance,
3042602ca9eaScth 	    (mod_hash_key_t)(intptr_t)path_instance, &hv) == 0)
3043602ca9eaScth 		path = (char *)hv;
3044602ca9eaScth 	else
3045602ca9eaScth 		path = NULL;
3046602ca9eaScth 	mutex_exit(&mdi_pathmap_mutex);
3047602ca9eaScth 	return (path);
3048602ca9eaScth }
3049602ca9eaScth 
30504c06356bSdh /*
30514c06356bSdh  * mdi_pi_spathname_by_instance():
30524c06356bSdh  *	Lookup of "shortpath" by 'path_instance'. Return "shortpath".
30534c06356bSdh  *	NOTE: returned "shortpath" remains valid forever (until reboot).
30544c06356bSdh  */
30554c06356bSdh char *
mdi_pi_spathname_by_instance(int path_instance)30564c06356bSdh mdi_pi_spathname_by_instance(int path_instance)
30574c06356bSdh {
30584c06356bSdh 	char		*path;
30594c06356bSdh 	mod_hash_val_t	hv;
30604c06356bSdh 
30614c06356bSdh 	/* mdi_pathmap lookup of "path" by 'path_instance' */
30624c06356bSdh 	mutex_enter(&mdi_pathmap_mutex);
30634c06356bSdh 	if (mod_hash_find(mdi_pathmap_sbyinstance,
30644c06356bSdh 	    (mod_hash_key_t)(intptr_t)path_instance, &hv) == 0)
30654c06356bSdh 		path = (char *)hv;
30664c06356bSdh 	else
30674c06356bSdh 		path = NULL;
30684c06356bSdh 	mutex_exit(&mdi_pathmap_mutex);
30694c06356bSdh 	return (path);
30704c06356bSdh }
30714c06356bSdh 
30724c06356bSdh 
30737c478bd9Sstevel@tonic-gate /*
30747c478bd9Sstevel@tonic-gate  * i_mdi_phci_add_path():
3075d27aea3fSDan Cross  *		Add a mdi_pathinfo node to pHCI list.
30767c478bd9Sstevel@tonic-gate  * Notes:
30777c478bd9Sstevel@tonic-gate  *		Caller should per-pHCI mutex
30787c478bd9Sstevel@tonic-gate  */
30797c478bd9Sstevel@tonic-gate static void
i_mdi_phci_add_path(mdi_phci_t * ph,mdi_pathinfo_t * pip)30807c478bd9Sstevel@tonic-gate i_mdi_phci_add_path(mdi_phci_t *ph, mdi_pathinfo_t *pip)
30817c478bd9Sstevel@tonic-gate {
30827c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ph->ph_dip));
30837c478bd9Sstevel@tonic-gate 
30845e3986cbScth 	MDI_PHCI_LOCK(ph);
30857c478bd9Sstevel@tonic-gate 	if (ph->ph_path_head == NULL) {
30867c478bd9Sstevel@tonic-gate 		ph->ph_path_head = pip;
30877c478bd9Sstevel@tonic-gate 	} else {
30887c478bd9Sstevel@tonic-gate 		MDI_PI(ph->ph_path_tail)->pi_phci_link = MDI_PI(pip);
30897c478bd9Sstevel@tonic-gate 	}
30907c478bd9Sstevel@tonic-gate 	ph->ph_path_tail = pip;
30917c478bd9Sstevel@tonic-gate 	ph->ph_path_count++;
30925e3986cbScth 	MDI_PHCI_UNLOCK(ph);
30937c478bd9Sstevel@tonic-gate }
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate /*
30967c478bd9Sstevel@tonic-gate  * i_mdi_client_add_path():
30977c478bd9Sstevel@tonic-gate  *		Add mdi_pathinfo node to client list
30987c478bd9Sstevel@tonic-gate  */
30997c478bd9Sstevel@tonic-gate static void
i_mdi_client_add_path(mdi_client_t * ct,mdi_pathinfo_t * pip)31007c478bd9Sstevel@tonic-gate i_mdi_client_add_path(mdi_client_t *ct, mdi_pathinfo_t *pip)
31017c478bd9Sstevel@tonic-gate {
31027c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ct->ct_dip));
31037c478bd9Sstevel@tonic-gate 
31045e3986cbScth 	MDI_CLIENT_LOCK(ct);
31057c478bd9Sstevel@tonic-gate 	if (ct->ct_path_head == NULL) {
31067c478bd9Sstevel@tonic-gate 		ct->ct_path_head = pip;
31077c478bd9Sstevel@tonic-gate 	} else {
31087c478bd9Sstevel@tonic-gate 		MDI_PI(ct->ct_path_tail)->pi_client_link = MDI_PI(pip);
31097c478bd9Sstevel@tonic-gate 	}
31107c478bd9Sstevel@tonic-gate 	ct->ct_path_tail = pip;
31117c478bd9Sstevel@tonic-gate 	ct->ct_path_count++;
31125e3986cbScth 	MDI_CLIENT_UNLOCK(ct);
31137c478bd9Sstevel@tonic-gate }
31147c478bd9Sstevel@tonic-gate 
31157c478bd9Sstevel@tonic-gate /*
31167c478bd9Sstevel@tonic-gate  * mdi_pi_free():
31177c478bd9Sstevel@tonic-gate  *		Free the mdi_pathinfo node and also client device node if this
31187c478bd9Sstevel@tonic-gate  *		is the last path to the device
31197c478bd9Sstevel@tonic-gate  * Return Values:
31207c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
31217c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
31227c478bd9Sstevel@tonic-gate  *		MDI_BUSY
31237c478bd9Sstevel@tonic-gate  */
31247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
31257c478bd9Sstevel@tonic-gate int
mdi_pi_free(mdi_pathinfo_t * pip,int flags)31267c478bd9Sstevel@tonic-gate mdi_pi_free(mdi_pathinfo_t *pip, int flags)
31277c478bd9Sstevel@tonic-gate {
31289aed1621SDavid Hollister 	int		rv;
31297c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
31307c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
31317c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
31327c478bd9Sstevel@tonic-gate 	int		(*f)();
31337c478bd9Sstevel@tonic-gate 	int		client_held = 0;
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
31367c478bd9Sstevel@tonic-gate 	ph = MDI_PI(pip)->pi_phci;
31377c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
31387c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
31397c478bd9Sstevel@tonic-gate 		/*
31407c478bd9Sstevel@tonic-gate 		 * Invalid pHCI device, return failure
31417c478bd9Sstevel@tonic-gate 		 */
31424c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, NULL,
31434c06356bSdh 		    "!invalid pHCI: pip %s %p",
31444c06356bSdh 		    mdi_pi_spathname(pip), (void *)pip));
31457c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
31467c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
31477c478bd9Sstevel@tonic-gate 	}
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
31507c478bd9Sstevel@tonic-gate 	ASSERT(vh != NULL);
31517c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
31527c478bd9Sstevel@tonic-gate 		/* Invalid pHCI device, return failure */
31534c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, ph->ph_dip,
31544c06356bSdh 		    "!invalid vHCI: pip %s %p",
31554c06356bSdh 		    mdi_pi_spathname(pip), (void *)pip));
31567c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
31577c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
31587c478bd9Sstevel@tonic-gate 	}
31597c478bd9Sstevel@tonic-gate 
31607c478bd9Sstevel@tonic-gate 	ct = MDI_PI(pip)->pi_client;
31617c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
31627c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
31637c478bd9Sstevel@tonic-gate 		/*
31647c478bd9Sstevel@tonic-gate 		 * Invalid Client device, return failure
31657c478bd9Sstevel@tonic-gate 		 */
31664c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, ph->ph_dip,
31674c06356bSdh 		    "!invalid client: pip %s %p",
31684c06356bSdh 		    mdi_pi_spathname(pip), (void *)pip));
31697c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
31707c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
31717c478bd9Sstevel@tonic-gate 	}
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 	/*
31747c478bd9Sstevel@tonic-gate 	 * Check to see for busy condition.  A mdi_pathinfo can only be freed
31757c478bd9Sstevel@tonic-gate 	 * if the node state is either offline or init and the reference count
31767c478bd9Sstevel@tonic-gate 	 * is zero.
31777c478bd9Sstevel@tonic-gate 	 */
31787c478bd9Sstevel@tonic-gate 	if (!(MDI_PI_IS_OFFLINE(pip) || MDI_PI_IS_INIT(pip) ||
31797c478bd9Sstevel@tonic-gate 	    MDI_PI_IS_INITING(pip))) {
31807c478bd9Sstevel@tonic-gate 		/*
31817c478bd9Sstevel@tonic-gate 		 * Node is busy
31827c478bd9Sstevel@tonic-gate 		 */
31834c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, ct->ct_dip,
31844c06356bSdh 		    "!busy: pip %s %p", mdi_pi_spathname(pip), (void *)pip));
31857c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
31867c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
31877c478bd9Sstevel@tonic-gate 	}
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate 	while (MDI_PI(pip)->pi_ref_cnt != 0) {
31907c478bd9Sstevel@tonic-gate 		/*
31917c478bd9Sstevel@tonic-gate 		 * Give a chance for pending I/Os to complete.
31927c478bd9Sstevel@tonic-gate 		 */
31934c06356bSdh 		MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
31944c06356bSdh 		    "!%d cmds still pending on path: %s %p",
31954c06356bSdh 		    MDI_PI(pip)->pi_ref_cnt,
31964c06356bSdh 		    mdi_pi_spathname(pip), (void *)pip));
3197d3d50737SRafael Vanoni 		if (cv_reltimedwait(&MDI_PI(pip)->pi_ref_cv,
3198d3d50737SRafael Vanoni 		    &MDI_PI(pip)->pi_mutex, drv_usectohz(60 * 1000000),
3199d3d50737SRafael Vanoni 		    TR_CLOCK_TICK) == -1) {
32007c478bd9Sstevel@tonic-gate 			/*
32017c478bd9Sstevel@tonic-gate 			 * The timeout time reached without ref_cnt being zero
32027c478bd9Sstevel@tonic-gate 			 * being signaled.
32037c478bd9Sstevel@tonic-gate 			 */
32044c06356bSdh 			MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
32054c06356bSdh 			    "!Timeout reached on path %s %p without the cond",
32064c06356bSdh 			    mdi_pi_spathname(pip), (void *)pip));
32074c06356bSdh 			MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
32084c06356bSdh 			    "!%d cmds still pending on path %s %p",
32094c06356bSdh 			    MDI_PI(pip)->pi_ref_cnt,
32104c06356bSdh 			    mdi_pi_spathname(pip), (void *)pip));
32117c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
32127c478bd9Sstevel@tonic-gate 			return (MDI_BUSY);
32137c478bd9Sstevel@tonic-gate 		}
32147c478bd9Sstevel@tonic-gate 	}
32157c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held) {
32167c478bd9Sstevel@tonic-gate 		client_held = 1;
32177c478bd9Sstevel@tonic-gate 	}
32187c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
32197c478bd9Sstevel@tonic-gate 
32203c34adc5Sramat 	vhcache_pi_remove(vh->vh_config, MDI_PI(pip));
32213c34adc5Sramat 
32227c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
32237c478bd9Sstevel@tonic-gate 
32245e3986cbScth 	/* Prevent further failovers till MDI_VHCI_CLIENT_LOCK is held */
32257c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_PATH_FREE_IN_PROGRESS(ct);
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 	/*
32287c478bd9Sstevel@tonic-gate 	 * Wait till failover is complete before removing this node.
32297c478bd9Sstevel@tonic-gate 	 */
32307c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct))
32317c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_failover_cv, &ct->ct_mutex);
32327c478bd9Sstevel@tonic-gate 
32337c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
32345e3986cbScth 	MDI_VHCI_CLIENT_LOCK(vh);
32357c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
32367c478bd9Sstevel@tonic-gate 	MDI_CLIENT_CLEAR_PATH_FREE_IN_PROGRESS(ct);
32377c478bd9Sstevel@tonic-gate 
3238c6f039c7SToomas Soome 	rv = MDI_SUCCESS;
32397c478bd9Sstevel@tonic-gate 	if (!MDI_PI_IS_INITING(pip)) {
32407c478bd9Sstevel@tonic-gate 		f = vh->vh_ops->vo_pi_uninit;
32417c478bd9Sstevel@tonic-gate 		if (f != NULL) {
32427c478bd9Sstevel@tonic-gate 			rv = (*f)(vh->vh_dip, pip, 0);
32437c478bd9Sstevel@tonic-gate 		}
3244c6f039c7SToomas Soome 	}
32459aed1621SDavid Hollister 
32467c478bd9Sstevel@tonic-gate 	/*
32477c478bd9Sstevel@tonic-gate 	 * If vo_pi_uninit() completed successfully.
32487c478bd9Sstevel@tonic-gate 	 */
32497c478bd9Sstevel@tonic-gate 	if (rv == MDI_SUCCESS) {
32507c478bd9Sstevel@tonic-gate 		if (client_held) {
32514c06356bSdh 			MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
32527c478bd9Sstevel@tonic-gate 			    "i_mdi_pm_rele_client\n"));
32537c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, 1);
32547c478bd9Sstevel@tonic-gate 		}
32557c478bd9Sstevel@tonic-gate 		i_mdi_pi_free(ph, pip, ct);
32567c478bd9Sstevel@tonic-gate 		if (ct->ct_path_count == 0) {
32577c478bd9Sstevel@tonic-gate 			/*
32587c478bd9Sstevel@tonic-gate 			 * Client lost its last path.
32597c478bd9Sstevel@tonic-gate 			 * Clean up the client device
32607c478bd9Sstevel@tonic-gate 			 */
32617c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
32627c478bd9Sstevel@tonic-gate 			(void) i_mdi_client_free(ct->ct_vhci, ct);
32635e3986cbScth 			MDI_VHCI_CLIENT_UNLOCK(vh);
32647c478bd9Sstevel@tonic-gate 			return (rv);
32657c478bd9Sstevel@tonic-gate 		}
32667c478bd9Sstevel@tonic-gate 	}
32677c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
32685e3986cbScth 	MDI_VHCI_CLIENT_UNLOCK(vh);
32693c34adc5Sramat 
32703c34adc5Sramat 	if (rv == MDI_FAILURE)
32713c34adc5Sramat 		vhcache_pi_add(vh->vh_config, MDI_PI(pip));
32723c34adc5Sramat 
32737c478bd9Sstevel@tonic-gate 	return (rv);
32747c478bd9Sstevel@tonic-gate }
32757c478bd9Sstevel@tonic-gate 
32767c478bd9Sstevel@tonic-gate /*
32777c478bd9Sstevel@tonic-gate  * i_mdi_pi_free():
32787c478bd9Sstevel@tonic-gate  *		Free the mdi_pathinfo node
32797c478bd9Sstevel@tonic-gate  */
32807c478bd9Sstevel@tonic-gate static void
i_mdi_pi_free(mdi_phci_t * ph,mdi_pathinfo_t * pip,mdi_client_t * ct)32817c478bd9Sstevel@tonic-gate i_mdi_pi_free(mdi_phci_t *ph, mdi_pathinfo_t *pip, mdi_client_t *ct)
32827c478bd9Sstevel@tonic-gate {
32835e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
32845e3986cbScth 
32857c478bd9Sstevel@tonic-gate 	/*
32867c478bd9Sstevel@tonic-gate 	 * remove any per-path kstats
32877c478bd9Sstevel@tonic-gate 	 */
32887c478bd9Sstevel@tonic-gate 	i_mdi_pi_kstat_destroy(pip);
32897c478bd9Sstevel@tonic-gate 
32905e3986cbScth 	/* See comments in i_mdi_pi_alloc() */
32913fe80ca4SDan Cross 	ndi_devi_enter(ct->ct_dip);
32923fe80ca4SDan Cross 	ndi_devi_enter(ph->ph_dip);
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate 	i_mdi_client_remove_path(ct, pip);
32957c478bd9Sstevel@tonic-gate 	i_mdi_phci_remove_path(ph, pip);
32967c478bd9Sstevel@tonic-gate 
32973fe80ca4SDan Cross 	ndi_devi_exit(ph->ph_dip);
32983fe80ca4SDan Cross 	ndi_devi_exit(ct->ct_dip);
32997c478bd9Sstevel@tonic-gate 
33007c478bd9Sstevel@tonic-gate 	mutex_destroy(&MDI_PI(pip)->pi_mutex);
33017c478bd9Sstevel@tonic-gate 	cv_destroy(&MDI_PI(pip)->pi_state_cv);
33027c478bd9Sstevel@tonic-gate 	cv_destroy(&MDI_PI(pip)->pi_ref_cv);
33037c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_addr) {
33047c478bd9Sstevel@tonic-gate 		kmem_free(MDI_PI(pip)->pi_addr,
33057c478bd9Sstevel@tonic-gate 		    strlen(MDI_PI(pip)->pi_addr) + 1);
33067c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_addr = NULL;
33077c478bd9Sstevel@tonic-gate 	}
33087c478bd9Sstevel@tonic-gate 
33097c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop) {
33107c478bd9Sstevel@tonic-gate 		(void) nvlist_free(MDI_PI(pip)->pi_prop);
33117c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_prop = NULL;
33127c478bd9Sstevel@tonic-gate 	}
33137c478bd9Sstevel@tonic-gate 	kmem_free(pip, sizeof (struct mdi_pathinfo));
33147c478bd9Sstevel@tonic-gate }
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate 
33177c478bd9Sstevel@tonic-gate /*
33187c478bd9Sstevel@tonic-gate  * i_mdi_phci_remove_path():
3319d27aea3fSDan Cross  *		Remove a mdi_pathinfo node from pHCI list.
33207c478bd9Sstevel@tonic-gate  * Notes:
33217c478bd9Sstevel@tonic-gate  *		Caller should hold per-pHCI mutex
33227c478bd9Sstevel@tonic-gate  */
33237c478bd9Sstevel@tonic-gate static void
i_mdi_phci_remove_path(mdi_phci_t * ph,mdi_pathinfo_t * pip)33247c478bd9Sstevel@tonic-gate i_mdi_phci_remove_path(mdi_phci_t *ph, mdi_pathinfo_t *pip)
33257c478bd9Sstevel@tonic-gate {
33267c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*prev = NULL;
33277c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*path = NULL;
33287c478bd9Sstevel@tonic-gate 
33297c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ph->ph_dip));
33307c478bd9Sstevel@tonic-gate 
33315e3986cbScth 	MDI_PHCI_LOCK(ph);
33327c478bd9Sstevel@tonic-gate 	path = ph->ph_path_head;
33337c478bd9Sstevel@tonic-gate 	while (path != NULL) {
33347c478bd9Sstevel@tonic-gate 		if (path == pip) {
33357c478bd9Sstevel@tonic-gate 			break;
33367c478bd9Sstevel@tonic-gate 		}
33377c478bd9Sstevel@tonic-gate 		prev = path;
33387c478bd9Sstevel@tonic-gate 		path = (mdi_pathinfo_t *)MDI_PI(path)->pi_phci_link;
33397c478bd9Sstevel@tonic-gate 	}
33407c478bd9Sstevel@tonic-gate 
33417c478bd9Sstevel@tonic-gate 	if (path) {
33427c478bd9Sstevel@tonic-gate 		ph->ph_path_count--;
33437c478bd9Sstevel@tonic-gate 		if (prev) {
33447c478bd9Sstevel@tonic-gate 			MDI_PI(prev)->pi_phci_link = MDI_PI(path)->pi_phci_link;
33457c478bd9Sstevel@tonic-gate 		} else {
33467c478bd9Sstevel@tonic-gate 			ph->ph_path_head =
33477c478bd9Sstevel@tonic-gate 			    (mdi_pathinfo_t *)MDI_PI(path)->pi_phci_link;
33487c478bd9Sstevel@tonic-gate 		}
33497c478bd9Sstevel@tonic-gate 		if (ph->ph_path_tail == path) {
33507c478bd9Sstevel@tonic-gate 			ph->ph_path_tail = prev;
33517c478bd9Sstevel@tonic-gate 		}
33527c478bd9Sstevel@tonic-gate 	}
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate 	/*
33557c478bd9Sstevel@tonic-gate 	 * Clear the pHCI link
33567c478bd9Sstevel@tonic-gate 	 */
33577c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci_link = NULL;
33587c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_phci = NULL;
33595e3986cbScth 	MDI_PHCI_UNLOCK(ph);
33607c478bd9Sstevel@tonic-gate }
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate /*
33637c478bd9Sstevel@tonic-gate  * i_mdi_client_remove_path():
3364d27aea3fSDan Cross  *		Remove a mdi_pathinfo node from client path list.
33657c478bd9Sstevel@tonic-gate  */
33667c478bd9Sstevel@tonic-gate static void
i_mdi_client_remove_path(mdi_client_t * ct,mdi_pathinfo_t * pip)33677c478bd9Sstevel@tonic-gate i_mdi_client_remove_path(mdi_client_t *ct, mdi_pathinfo_t *pip)
33687c478bd9Sstevel@tonic-gate {
33697c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*prev = NULL;
33707c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*path;
33717c478bd9Sstevel@tonic-gate 
33727c478bd9Sstevel@tonic-gate 	ASSERT(DEVI_BUSY_OWNED(ct->ct_dip));
33737c478bd9Sstevel@tonic-gate 
33745e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
33757c478bd9Sstevel@tonic-gate 	path = ct->ct_path_head;
33767c478bd9Sstevel@tonic-gate 	while (path != NULL) {
33777c478bd9Sstevel@tonic-gate 		if (path == pip) {
33787c478bd9Sstevel@tonic-gate 			break;
33797c478bd9Sstevel@tonic-gate 		}
33807c478bd9Sstevel@tonic-gate 		prev = path;
33817c478bd9Sstevel@tonic-gate 		path = (mdi_pathinfo_t *)MDI_PI(path)->pi_client_link;
33827c478bd9Sstevel@tonic-gate 	}
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate 	if (path) {
33857c478bd9Sstevel@tonic-gate 		ct->ct_path_count--;
33867c478bd9Sstevel@tonic-gate 		if (prev) {
33877c478bd9Sstevel@tonic-gate 			MDI_PI(prev)->pi_client_link =
33887c478bd9Sstevel@tonic-gate 			    MDI_PI(path)->pi_client_link;
33897c478bd9Sstevel@tonic-gate 		} else {
33907c478bd9Sstevel@tonic-gate 			ct->ct_path_head =
33917c478bd9Sstevel@tonic-gate 			    (mdi_pathinfo_t *)MDI_PI(path)->pi_client_link;
33927c478bd9Sstevel@tonic-gate 		}
33937c478bd9Sstevel@tonic-gate 		if (ct->ct_path_tail == path) {
33947c478bd9Sstevel@tonic-gate 			ct->ct_path_tail = prev;
33957c478bd9Sstevel@tonic-gate 		}
33967c478bd9Sstevel@tonic-gate 		if (ct->ct_path_last == path) {
33977c478bd9Sstevel@tonic-gate 			ct->ct_path_last = ct->ct_path_head;
33987c478bd9Sstevel@tonic-gate 		}
33997c478bd9Sstevel@tonic-gate 	}
34007c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client_link = NULL;
34017c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_client = NULL;
34027c478bd9Sstevel@tonic-gate }
34037c478bd9Sstevel@tonic-gate 
34047c478bd9Sstevel@tonic-gate /*
34057c478bd9Sstevel@tonic-gate  * i_mdi_pi_state_change():
34067c478bd9Sstevel@tonic-gate  *		online a mdi_pathinfo node
34077c478bd9Sstevel@tonic-gate  *
34087c478bd9Sstevel@tonic-gate  * Return Values:
34097c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
34107c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
34117c478bd9Sstevel@tonic-gate  */
34127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
34137c478bd9Sstevel@tonic-gate static int
i_mdi_pi_state_change(mdi_pathinfo_t * pip,mdi_pathinfo_state_t state,int flag)34147c478bd9Sstevel@tonic-gate i_mdi_pi_state_change(mdi_pathinfo_t *pip, mdi_pathinfo_state_t state, int flag)
34157c478bd9Sstevel@tonic-gate {
34167c478bd9Sstevel@tonic-gate 	int		rv = MDI_SUCCESS;
34177c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh;
34187c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
34197c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
34207c478bd9Sstevel@tonic-gate 	int		(*f)();
34217c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
34227c478bd9Sstevel@tonic-gate 
34237c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
34247c478bd9Sstevel@tonic-gate 
34257c478bd9Sstevel@tonic-gate 	ph = MDI_PI(pip)->pi_phci;
34267c478bd9Sstevel@tonic-gate 	ASSERT(ph);
34277c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
34287c478bd9Sstevel@tonic-gate 		/*
34297c478bd9Sstevel@tonic-gate 		 * Invalid pHCI device, fail the request
34307c478bd9Sstevel@tonic-gate 		 */
34317c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
34324c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, NULL,
34334c06356bSdh 		    "!invalid phci: pip %s %p",
34344c06356bSdh 		    mdi_pi_spathname(pip), (void *)pip));
34357c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
34367c478bd9Sstevel@tonic-gate 	}
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
34397c478bd9Sstevel@tonic-gate 	ASSERT(vh);
34407c478bd9Sstevel@tonic-gate 	if (vh == NULL) {
34417c478bd9Sstevel@tonic-gate 		/*
34427c478bd9Sstevel@tonic-gate 		 * Invalid vHCI device, fail the request
34437c478bd9Sstevel@tonic-gate 		 */
34447c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
34454c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, ph->ph_dip,
34464c06356bSdh 		    "!invalid vhci: pip %s %p",
34474c06356bSdh 		    mdi_pi_spathname(pip), (void *)pip));
34487c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
34497c478bd9Sstevel@tonic-gate 	}
34507c478bd9Sstevel@tonic-gate 
34517c478bd9Sstevel@tonic-gate 	ct = MDI_PI(pip)->pi_client;
34527c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
34537c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
34547c478bd9Sstevel@tonic-gate 		/*
34557c478bd9Sstevel@tonic-gate 		 * Invalid client device, fail the request
34567c478bd9Sstevel@tonic-gate 		 */
34577c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
34584c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, ph->ph_dip,
34594c06356bSdh 		    "!invalid client: pip %s %p",
34604c06356bSdh 		    mdi_pi_spathname(pip), (void *)pip));
34617c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
34627c478bd9Sstevel@tonic-gate 	}
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate 	/*
34657c478bd9Sstevel@tonic-gate 	 * If this path has not been initialized yet, Callback vHCI driver's
34667c478bd9Sstevel@tonic-gate 	 * pathinfo node initialize entry point
34677c478bd9Sstevel@tonic-gate 	 */
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate 	if (MDI_PI_IS_INITING(pip)) {
34707c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
34717c478bd9Sstevel@tonic-gate 		f = vh->vh_ops->vo_pi_init;
34727c478bd9Sstevel@tonic-gate 		if (f != NULL) {
34737c478bd9Sstevel@tonic-gate 			rv = (*f)(vh->vh_dip, pip, 0);
34747c478bd9Sstevel@tonic-gate 			if (rv != MDI_SUCCESS) {
34754c06356bSdh 				MDI_DEBUG(1, (MDI_WARN, ct->ct_dip,
34764c06356bSdh 				    "!vo_pi_init failed: vHCI %p, pip %s %p",
34774c06356bSdh 				    (void *)vh, mdi_pi_spathname(pip),
34784c06356bSdh 				    (void *)pip));
34797c478bd9Sstevel@tonic-gate 				return (MDI_FAILURE);
34807c478bd9Sstevel@tonic-gate 			}
34817c478bd9Sstevel@tonic-gate 		}
34827c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
34837c478bd9Sstevel@tonic-gate 		MDI_PI_CLEAR_TRANSIENT(pip);
34847c478bd9Sstevel@tonic-gate 	}
34857c478bd9Sstevel@tonic-gate 
34867c478bd9Sstevel@tonic-gate 	/*
34877c478bd9Sstevel@tonic-gate 	 * Do not allow state transition when pHCI is in offline/suspended
34887c478bd9Sstevel@tonic-gate 	 * states
34897c478bd9Sstevel@tonic-gate 	 */
34907c478bd9Sstevel@tonic-gate 	i_mdi_phci_lock(ph, pip);
34917c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_READY(ph) == 0) {
34924c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, ct->ct_dip,
34934c06356bSdh 		    "!pHCI not ready, pHCI=%p", (void *)ph));
34947c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
34957c478bd9Sstevel@tonic-gate 		i_mdi_phci_unlock(ph);
34967c478bd9Sstevel@tonic-gate 		return (MDI_BUSY);
34977c478bd9Sstevel@tonic-gate 	}
34987c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNSTABLE(ph);
34997c478bd9Sstevel@tonic-gate 	i_mdi_phci_unlock(ph);
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate 	/*
35027c478bd9Sstevel@tonic-gate 	 * Check if mdi_pathinfo state is in transient state.
35037c478bd9Sstevel@tonic-gate 	 * If yes, offlining is in progress and wait till transient state is
35047c478bd9Sstevel@tonic-gate 	 * cleared.
35057c478bd9Sstevel@tonic-gate 	 */
35067c478bd9Sstevel@tonic-gate 	if (MDI_PI_IS_TRANSIENT(pip)) {
35077c478bd9Sstevel@tonic-gate 		while (MDI_PI_IS_TRANSIENT(pip)) {
35087c478bd9Sstevel@tonic-gate 			cv_wait(&MDI_PI(pip)->pi_state_cv,
35097c478bd9Sstevel@tonic-gate 			    &MDI_PI(pip)->pi_mutex);
35107c478bd9Sstevel@tonic-gate 		}
35117c478bd9Sstevel@tonic-gate 	}
35127c478bd9Sstevel@tonic-gate 
35137c478bd9Sstevel@tonic-gate 	/*
35147c478bd9Sstevel@tonic-gate 	 * Grab the client lock in reverse order sequence and release the
35157c478bd9Sstevel@tonic-gate 	 * mdi_pathinfo mutex.
35167c478bd9Sstevel@tonic-gate 	 */
35177c478bd9Sstevel@tonic-gate 	i_mdi_client_lock(ct, pip);
35187c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
35197c478bd9Sstevel@tonic-gate 
35207c478bd9Sstevel@tonic-gate 	/*
35217c478bd9Sstevel@tonic-gate 	 * Wait till failover state is cleared
35227c478bd9Sstevel@tonic-gate 	 */
35237c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct))
35247c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_failover_cv, &ct->ct_mutex);
35257c478bd9Sstevel@tonic-gate 
35267c478bd9Sstevel@tonic-gate 	/*
35277c478bd9Sstevel@tonic-gate 	 * Mark the mdi_pathinfo node state as transient
35287c478bd9Sstevel@tonic-gate 	 */
35297c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
35307c478bd9Sstevel@tonic-gate 	switch (state) {
35317c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_ONLINE:
35327c478bd9Sstevel@tonic-gate 		MDI_PI_SET_ONLINING(pip);
35337c478bd9Sstevel@tonic-gate 		break;
35347c478bd9Sstevel@tonic-gate 
35357c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_STANDBY:
35367c478bd9Sstevel@tonic-gate 		MDI_PI_SET_STANDBYING(pip);
35377c478bd9Sstevel@tonic-gate 		break;
35387c478bd9Sstevel@tonic-gate 
35397c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_FAULT:
35407c478bd9Sstevel@tonic-gate 		/*
35417c478bd9Sstevel@tonic-gate 		 * Mark the pathinfo state as FAULTED
35427c478bd9Sstevel@tonic-gate 		 */
35437c478bd9Sstevel@tonic-gate 		MDI_PI_SET_FAULTING(pip);
35447c478bd9Sstevel@tonic-gate 		MDI_PI_ERRSTAT(pip, MDI_PI_HARDERR);
35457c478bd9Sstevel@tonic-gate 		break;
35467c478bd9Sstevel@tonic-gate 
35477c478bd9Sstevel@tonic-gate 	case MDI_PATHINFO_STATE_OFFLINE:
35487c478bd9Sstevel@tonic-gate 		/*
35497c478bd9Sstevel@tonic-gate 		 * ndi_devi_offline() cannot hold pip or ct locks.
35507c478bd9Sstevel@tonic-gate 		 */
35517c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
35524c06356bSdh 
35537c478bd9Sstevel@tonic-gate 		/*
35544c06356bSdh 		 * If this is a user initiated path online->offline operation
35554c06356bSdh 		 * who's success would transition a client from DEGRADED to
35564c06356bSdh 		 * FAILED then only proceed if we can offline the client first.
35577c478bd9Sstevel@tonic-gate 		 */
35587c478bd9Sstevel@tonic-gate 		cdip = ct->ct_dip;
35594c06356bSdh 		if ((flag & NDI_USER_REQ) &&
35604c06356bSdh 		    MDI_PI_IS_ONLINE(pip) &&
35614c06356bSdh 		    (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_DEGRADED)) {
35627c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
35634c06356bSdh 			rv = ndi_devi_offline(cdip, NDI_DEVFS_CLEAN);
35647c478bd9Sstevel@tonic-gate 			if (rv != NDI_SUCCESS) {
35657c478bd9Sstevel@tonic-gate 				/*
35667c478bd9Sstevel@tonic-gate 				 * Convert to MDI error code
35677c478bd9Sstevel@tonic-gate 				 */
35687c478bd9Sstevel@tonic-gate 				switch (rv) {
35697c478bd9Sstevel@tonic-gate 				case NDI_BUSY:
35707c478bd9Sstevel@tonic-gate 					rv = MDI_BUSY;
35717c478bd9Sstevel@tonic-gate 					break;
35727c478bd9Sstevel@tonic-gate 				default:
35737c478bd9Sstevel@tonic-gate 					rv = MDI_FAILURE;
35747c478bd9Sstevel@tonic-gate 					break;
35757c478bd9Sstevel@tonic-gate 				}
35767c478bd9Sstevel@tonic-gate 				goto state_change_exit;
35777c478bd9Sstevel@tonic-gate 			} else {
35787c478bd9Sstevel@tonic-gate 				i_mdi_client_lock(ct, NULL);
35797c478bd9Sstevel@tonic-gate 			}
35807c478bd9Sstevel@tonic-gate 		}
35817c478bd9Sstevel@tonic-gate 		/*
35827c478bd9Sstevel@tonic-gate 		 * Mark the mdi_pathinfo node state as transient
35837c478bd9Sstevel@tonic-gate 		 */
35847c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
35857c478bd9Sstevel@tonic-gate 		MDI_PI_SET_OFFLINING(pip);
35867c478bd9Sstevel@tonic-gate 		break;
35873df2e8b2SRobert Mustacchi 
35883df2e8b2SRobert Mustacchi 	case MDI_PATHINFO_STATE_INIT:
35893df2e8b2SRobert Mustacchi 		/*
35903df2e8b2SRobert Mustacchi 		 * Callers are not allowed to ask us to change the state to the
35913df2e8b2SRobert Mustacchi 		 * initial state.
35923df2e8b2SRobert Mustacchi 		 */
35933df2e8b2SRobert Mustacchi 		rv = MDI_FAILURE;
35943df2e8b2SRobert Mustacchi 		MDI_PI_UNLOCK(pip);
35953df2e8b2SRobert Mustacchi 		goto state_change_exit;
35963df2e8b2SRobert Mustacchi 
35977c478bd9Sstevel@tonic-gate 	}
35987c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
35997c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNSTABLE(ct);
36007c478bd9Sstevel@tonic-gate 	i_mdi_client_unlock(ct);
36017c478bd9Sstevel@tonic-gate 
36027c478bd9Sstevel@tonic-gate 	f = vh->vh_ops->vo_pi_state_change;
36035e3986cbScth 	if (f != NULL)
36047c478bd9Sstevel@tonic-gate 		rv = (*f)(vh->vh_dip, pip, state, 0, flag);
36055e3986cbScth 
36067c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
36077c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
36085e3986cbScth 	if (rv == MDI_NOT_SUPPORTED) {
36095e3986cbScth 		MDI_CLIENT_SET_DEV_NOT_SUPPORTED(ct);
36105e3986cbScth 	}
36115e3986cbScth 	if (rv != MDI_SUCCESS) {
36124c06356bSdh 		MDI_DEBUG(2, (MDI_WARN, ct->ct_dip,
36134c06356bSdh 		    "vo_pi_state_change failed: rv %x", rv));
36145e3986cbScth 	}
36157c478bd9Sstevel@tonic-gate 	if (MDI_PI_IS_TRANSIENT(pip)) {
36167c478bd9Sstevel@tonic-gate 		if (rv == MDI_SUCCESS) {
36177c478bd9Sstevel@tonic-gate 			MDI_PI_CLEAR_TRANSIENT(pip);
36187c478bd9Sstevel@tonic-gate 		} else {
36197c478bd9Sstevel@tonic-gate 			MDI_PI(pip)->pi_state = MDI_PI_OLD_STATE(pip);
36207c478bd9Sstevel@tonic-gate 		}
36217c478bd9Sstevel@tonic-gate 	}
36227c478bd9Sstevel@tonic-gate 
36237c478bd9Sstevel@tonic-gate 	/*
36247c478bd9Sstevel@tonic-gate 	 * Wake anyone waiting for this mdi_pathinfo node
36257c478bd9Sstevel@tonic-gate 	 */
36267c478bd9Sstevel@tonic-gate 	cv_broadcast(&MDI_PI(pip)->pi_state_cv);
36277c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
36287c478bd9Sstevel@tonic-gate 
36297c478bd9Sstevel@tonic-gate 	/*
36307c478bd9Sstevel@tonic-gate 	 * Mark the client device as stable
36317c478bd9Sstevel@tonic-gate 	 */
36327c478bd9Sstevel@tonic-gate 	MDI_CLIENT_STABLE(ct);
36337c478bd9Sstevel@tonic-gate 	if (rv == MDI_SUCCESS) {
36347c478bd9Sstevel@tonic-gate 		if (ct->ct_unstable == 0) {
36357c478bd9Sstevel@tonic-gate 			cdip = ct->ct_dip;
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate 			/*
36387c478bd9Sstevel@tonic-gate 			 * Onlining the mdi_pathinfo node will impact the
36397c478bd9Sstevel@tonic-gate 			 * client state Update the client and dev_info node
36407c478bd9Sstevel@tonic-gate 			 * state accordingly
36417c478bd9Sstevel@tonic-gate 			 */
36427c478bd9Sstevel@tonic-gate 			rv = NDI_SUCCESS;
36437c478bd9Sstevel@tonic-gate 			i_mdi_client_update_state(ct);
36447c478bd9Sstevel@tonic-gate 			switch (MDI_CLIENT_STATE(ct)) {
36457c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_OPTIMAL:
36467c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_DEGRADED:
3647737d277aScth 				if (cdip && !i_ddi_devi_attached(cdip) &&
36487c478bd9Sstevel@tonic-gate 				    ((state == MDI_PATHINFO_STATE_ONLINE) ||
36497c478bd9Sstevel@tonic-gate 				    (state == MDI_PATHINFO_STATE_STANDBY))) {
36507c478bd9Sstevel@tonic-gate 
36517c478bd9Sstevel@tonic-gate 					/*
36527c478bd9Sstevel@tonic-gate 					 * Must do ndi_devi_online() through
36537c478bd9Sstevel@tonic-gate 					 * hotplug thread for deferred
36547c478bd9Sstevel@tonic-gate 					 * attach mechanism to work
36557c478bd9Sstevel@tonic-gate 					 */
36565e3986cbScth 					MDI_CLIENT_UNLOCK(ct);
36577c478bd9Sstevel@tonic-gate 					rv = ndi_devi_online(cdip, 0);
36585e3986cbScth 					MDI_CLIENT_LOCK(ct);
36597c478bd9Sstevel@tonic-gate 					if ((rv != NDI_SUCCESS) &&
36607c478bd9Sstevel@tonic-gate 					    (MDI_CLIENT_STATE(ct) ==
36617c478bd9Sstevel@tonic-gate 					    MDI_CLIENT_STATE_DEGRADED)) {
36624c06356bSdh 						MDI_DEBUG(1, (MDI_WARN, cdip,
36634c06356bSdh 						    "!ndi_devi_online failed "
36644c06356bSdh 						    "error %x", rv));
36657c478bd9Sstevel@tonic-gate 					}
366611779b4cSJeffry Molanus 					rv = NDI_SUCCESS;
36677c478bd9Sstevel@tonic-gate 				}
36687c478bd9Sstevel@tonic-gate 				break;
36697c478bd9Sstevel@tonic-gate 
36707c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_FAILED:
36717c478bd9Sstevel@tonic-gate 				/*
36727c478bd9Sstevel@tonic-gate 				 * This is the last path case for
36737c478bd9Sstevel@tonic-gate 				 * non-user initiated events.
36747c478bd9Sstevel@tonic-gate 				 */
36754c06356bSdh 				if (((flag & NDI_USER_REQ) == 0) &&
36767c478bd9Sstevel@tonic-gate 				    cdip && (i_ddi_node_state(cdip) >=
36777c478bd9Sstevel@tonic-gate 				    DS_INITIALIZED)) {
36785e3986cbScth 					MDI_CLIENT_UNLOCK(ct);
36794c06356bSdh 					rv = ndi_devi_offline(cdip,
36804c06356bSdh 					    NDI_DEVFS_CLEAN);
36815e3986cbScth 					MDI_CLIENT_LOCK(ct);
36827c478bd9Sstevel@tonic-gate 
36837c478bd9Sstevel@tonic-gate 					if (rv != NDI_SUCCESS) {
36847c478bd9Sstevel@tonic-gate 						/*
36857c478bd9Sstevel@tonic-gate 						 * ndi_devi_offline failed.
36867c478bd9Sstevel@tonic-gate 						 * Reset client flags to
36877c478bd9Sstevel@tonic-gate 						 * online as the path could not
36887c478bd9Sstevel@tonic-gate 						 * be offlined.
36897c478bd9Sstevel@tonic-gate 						 */
36904c06356bSdh 						MDI_DEBUG(1, (MDI_WARN, cdip,
36914c06356bSdh 						    "!ndi_devi_offline failed: "
36924c06356bSdh 						    "error %x", rv));
36937c478bd9Sstevel@tonic-gate 						MDI_CLIENT_SET_ONLINE(ct);
36947c478bd9Sstevel@tonic-gate 					}
36957c478bd9Sstevel@tonic-gate 				}
36967c478bd9Sstevel@tonic-gate 				break;
36977c478bd9Sstevel@tonic-gate 			}
36987c478bd9Sstevel@tonic-gate 			/*
36997c478bd9Sstevel@tonic-gate 			 * Convert to MDI error code
37007c478bd9Sstevel@tonic-gate 			 */
37017c478bd9Sstevel@tonic-gate 			switch (rv) {
37027c478bd9Sstevel@tonic-gate 			case NDI_SUCCESS:
37037c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_REPORT_DEV_NEEDED(ct);
37047c478bd9Sstevel@tonic-gate 				i_mdi_report_path_state(ct, pip);
37057c478bd9Sstevel@tonic-gate 				rv = MDI_SUCCESS;
37067c478bd9Sstevel@tonic-gate 				break;
37077c478bd9Sstevel@tonic-gate 			case NDI_BUSY:
37087c478bd9Sstevel@tonic-gate 				rv = MDI_BUSY;
37097c478bd9Sstevel@tonic-gate 				break;
37107c478bd9Sstevel@tonic-gate 			default:
37117c478bd9Sstevel@tonic-gate 				rv = MDI_FAILURE;
37127c478bd9Sstevel@tonic-gate 				break;
37137c478bd9Sstevel@tonic-gate 			}
37147c478bd9Sstevel@tonic-gate 		}
37157c478bd9Sstevel@tonic-gate 	}
37167c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
37177c478bd9Sstevel@tonic-gate 
37187c478bd9Sstevel@tonic-gate state_change_exit:
37197c478bd9Sstevel@tonic-gate 	/*
37207c478bd9Sstevel@tonic-gate 	 * Mark the pHCI as stable again.
37217c478bd9Sstevel@tonic-gate 	 */
37227c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
37237c478bd9Sstevel@tonic-gate 	MDI_PHCI_STABLE(ph);
37247c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
37257c478bd9Sstevel@tonic-gate 	return (rv);
37267c478bd9Sstevel@tonic-gate }
37277c478bd9Sstevel@tonic-gate 
37287c478bd9Sstevel@tonic-gate /*
37297c478bd9Sstevel@tonic-gate  * mdi_pi_online():
37307c478bd9Sstevel@tonic-gate  *		Place the path_info node in the online state.  The path is
37317c478bd9Sstevel@tonic-gate  *		now available to be selected by mdi_select_path() for
37327c478bd9Sstevel@tonic-gate  *		transporting I/O requests to client devices.
37337c478bd9Sstevel@tonic-gate  * Return Values:
37347c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
37357c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
37367c478bd9Sstevel@tonic-gate  */
37377c478bd9Sstevel@tonic-gate int
mdi_pi_online(mdi_pathinfo_t * pip,int flags)37387c478bd9Sstevel@tonic-gate mdi_pi_online(mdi_pathinfo_t *pip, int flags)
37397c478bd9Sstevel@tonic-gate {
374037fbbce5Scth 	mdi_client_t	*ct = MDI_PI(pip)->pi_client;
37417c478bd9Sstevel@tonic-gate 	int		client_held = 0;
374237fbbce5Scth 	int		rv;
37437c478bd9Sstevel@tonic-gate 
37447c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
37457c478bd9Sstevel@tonic-gate 	rv = i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_ONLINE, flags);
37467c478bd9Sstevel@tonic-gate 	if (rv != MDI_SUCCESS)
37477c478bd9Sstevel@tonic-gate 		return (rv);
37487c478bd9Sstevel@tonic-gate 
37497c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
37507c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held == 0) {
37514c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
37524c06356bSdh 		    "i_mdi_pm_hold_pip %p", (void *)pip));
37537c478bd9Sstevel@tonic-gate 		i_mdi_pm_hold_pip(pip);
37547c478bd9Sstevel@tonic-gate 		client_held = 1;
37557c478bd9Sstevel@tonic-gate 	}
37567c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
37577c478bd9Sstevel@tonic-gate 
37587c478bd9Sstevel@tonic-gate 	if (client_held) {
37597c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
37607c478bd9Sstevel@tonic-gate 		if (ct->ct_power_cnt == 0) {
37617c478bd9Sstevel@tonic-gate 			rv = i_mdi_power_all_phci(ct);
37627c478bd9Sstevel@tonic-gate 		}
37637c478bd9Sstevel@tonic-gate 
37644c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
37654c06356bSdh 		    "i_mdi_pm_hold_client %p", (void *)ct));
37667c478bd9Sstevel@tonic-gate 		i_mdi_pm_hold_client(ct, 1);
37677c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
37687c478bd9Sstevel@tonic-gate 	}
37697c478bd9Sstevel@tonic-gate 
37707c478bd9Sstevel@tonic-gate 	return (rv);
37717c478bd9Sstevel@tonic-gate }
37727c478bd9Sstevel@tonic-gate 
37737c478bd9Sstevel@tonic-gate /*
37747c478bd9Sstevel@tonic-gate  * mdi_pi_standby():
37757c478bd9Sstevel@tonic-gate  *		Place the mdi_pathinfo node in standby state
37767c478bd9Sstevel@tonic-gate  *
37777c478bd9Sstevel@tonic-gate  * Return Values:
37787c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
37797c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
37807c478bd9Sstevel@tonic-gate  */
37817c478bd9Sstevel@tonic-gate int
mdi_pi_standby(mdi_pathinfo_t * pip,int flags)37827c478bd9Sstevel@tonic-gate mdi_pi_standby(mdi_pathinfo_t *pip, int flags)
37837c478bd9Sstevel@tonic-gate {
37847c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_STANDBY, flags));
37857c478bd9Sstevel@tonic-gate }
37867c478bd9Sstevel@tonic-gate 
37877c478bd9Sstevel@tonic-gate /*
37887c478bd9Sstevel@tonic-gate  * mdi_pi_fault():
37897c478bd9Sstevel@tonic-gate  *		Place the mdi_pathinfo node in fault'ed state
37907c478bd9Sstevel@tonic-gate  * Return Values:
37917c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
37927c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
37937c478bd9Sstevel@tonic-gate  */
37947c478bd9Sstevel@tonic-gate int
mdi_pi_fault(mdi_pathinfo_t * pip,int flags)37957c478bd9Sstevel@tonic-gate mdi_pi_fault(mdi_pathinfo_t *pip, int flags)
37967c478bd9Sstevel@tonic-gate {
37977c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_FAULT, flags));
37987c478bd9Sstevel@tonic-gate }
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate /*
38017c478bd9Sstevel@tonic-gate  * mdi_pi_offline():
38027c478bd9Sstevel@tonic-gate  *		Offline a mdi_pathinfo node.
38037c478bd9Sstevel@tonic-gate  * Return Values:
38047c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
38057c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
38067c478bd9Sstevel@tonic-gate  */
38077c478bd9Sstevel@tonic-gate int
mdi_pi_offline(mdi_pathinfo_t * pip,int flags)38087c478bd9Sstevel@tonic-gate mdi_pi_offline(mdi_pathinfo_t *pip, int flags)
38097c478bd9Sstevel@tonic-gate {
38107c478bd9Sstevel@tonic-gate 	int	ret, client_held = 0;
38117c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
38124c06356bSdh 
38134c06356bSdh 	/*
38144c06356bSdh 	 * Original code overloaded NDI_DEVI_REMOVE to this interface, and
38154c06356bSdh 	 * used it to mean "user initiated operation" (i.e. devctl). Callers
38164c06356bSdh 	 * should now just use NDI_USER_REQ.
38174c06356bSdh 	 */
38184c06356bSdh 	if (flags & NDI_DEVI_REMOVE) {
38194c06356bSdh 		flags &= ~NDI_DEVI_REMOVE;
38204c06356bSdh 		flags |= NDI_USER_REQ;
38214c06356bSdh 	}
38227c478bd9Sstevel@tonic-gate 
38237c478bd9Sstevel@tonic-gate 	ret = i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_OFFLINE, flags);
38247c478bd9Sstevel@tonic-gate 
38257c478bd9Sstevel@tonic-gate 	if (ret == MDI_SUCCESS) {
38267c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
38277c478bd9Sstevel@tonic-gate 		if (MDI_PI(pip)->pi_pm_held) {
38287c478bd9Sstevel@tonic-gate 			client_held = 1;
38297c478bd9Sstevel@tonic-gate 		}
38307c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
38317c478bd9Sstevel@tonic-gate 
38327c478bd9Sstevel@tonic-gate 		if (client_held) {
38337c478bd9Sstevel@tonic-gate 			ct = MDI_PI(pip)->pi_client;
38347c478bd9Sstevel@tonic-gate 			MDI_CLIENT_LOCK(ct);
38354c06356bSdh 			MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
38364c06356bSdh 			    "i_mdi_pm_rele_client\n"));
38377c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, 1);
38387c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
38397c478bd9Sstevel@tonic-gate 		}
38407c478bd9Sstevel@tonic-gate 	}
38417c478bd9Sstevel@tonic-gate 
38427c478bd9Sstevel@tonic-gate 	return (ret);
38437c478bd9Sstevel@tonic-gate }
38447c478bd9Sstevel@tonic-gate 
38457c478bd9Sstevel@tonic-gate /*
38467c478bd9Sstevel@tonic-gate  * i_mdi_pi_offline():
38477c478bd9Sstevel@tonic-gate  *		Offline a mdi_pathinfo node and call the vHCI driver's callback
38487c478bd9Sstevel@tonic-gate  */
38497c478bd9Sstevel@tonic-gate static int
i_mdi_pi_offline(mdi_pathinfo_t * pip,int flags)38507c478bd9Sstevel@tonic-gate i_mdi_pi_offline(mdi_pathinfo_t *pip, int flags)
38517c478bd9Sstevel@tonic-gate {
38527c478bd9Sstevel@tonic-gate 	dev_info_t	*vdip = NULL;
38537c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
38547c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct = NULL;
38557c478bd9Sstevel@tonic-gate 	int		(*f)();
38567c478bd9Sstevel@tonic-gate 	int		rv;
38577c478bd9Sstevel@tonic-gate 
38587c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
38597c478bd9Sstevel@tonic-gate 	ct = MDI_PI(pip)->pi_client;
38607c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
38617c478bd9Sstevel@tonic-gate 
38627c478bd9Sstevel@tonic-gate 	while (MDI_PI(pip)->pi_ref_cnt != 0) {
38637c478bd9Sstevel@tonic-gate 		/*
38647c478bd9Sstevel@tonic-gate 		 * Give a chance for pending I/Os to complete.
38657c478bd9Sstevel@tonic-gate 		 */
38664c06356bSdh 		MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
38674c06356bSdh 		    "!%d cmds still pending on path %s %p",
38684c06356bSdh 		    MDI_PI(pip)->pi_ref_cnt, mdi_pi_spathname(pip),
38694c06356bSdh 		    (void *)pip));
3870d3d50737SRafael Vanoni 		if (cv_reltimedwait(&MDI_PI(pip)->pi_ref_cv,
3871d3d50737SRafael Vanoni 		    &MDI_PI(pip)->pi_mutex, drv_usectohz(60 * 1000000),
3872d3d50737SRafael Vanoni 		    TR_CLOCK_TICK) == -1) {
38737c478bd9Sstevel@tonic-gate 			/*
38747c478bd9Sstevel@tonic-gate 			 * The timeout time reached without ref_cnt being zero
38757c478bd9Sstevel@tonic-gate 			 * being signaled.
38767c478bd9Sstevel@tonic-gate 			 */
38774c06356bSdh 			MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
38784c06356bSdh 			    "!Timeout reached on path %s %p without the cond",
38794c06356bSdh 			    mdi_pi_spathname(pip), (void *)pip));
38804c06356bSdh 			MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
38814c06356bSdh 			    "!%d cmds still pending on path %s %p",
38824c06356bSdh 			    MDI_PI(pip)->pi_ref_cnt,
38834c06356bSdh 			    mdi_pi_spathname(pip), (void *)pip));
38847c478bd9Sstevel@tonic-gate 		}
38857c478bd9Sstevel@tonic-gate 	}
38867c478bd9Sstevel@tonic-gate 	vh = ct->ct_vhci;
38877c478bd9Sstevel@tonic-gate 	vdip = vh->vh_dip;
38887c478bd9Sstevel@tonic-gate 
38897c478bd9Sstevel@tonic-gate 	/*
38907c478bd9Sstevel@tonic-gate 	 * Notify vHCI that has registered this event
38917c478bd9Sstevel@tonic-gate 	 */
38927c478bd9Sstevel@tonic-gate 	ASSERT(vh->vh_ops);
38937c478bd9Sstevel@tonic-gate 	f = vh->vh_ops->vo_pi_state_change;
38947c478bd9Sstevel@tonic-gate 
3895c6f039c7SToomas Soome 	rv = MDI_SUCCESS;
38967c478bd9Sstevel@tonic-gate 	if (f != NULL) {
38977c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
38987c478bd9Sstevel@tonic-gate 		if ((rv = (*f)(vdip, pip, MDI_PATHINFO_STATE_OFFLINE, 0,
38997c478bd9Sstevel@tonic-gate 		    flags)) != MDI_SUCCESS) {
39004c06356bSdh 			MDI_DEBUG(1, (MDI_WARN, ct->ct_dip,
39014c06356bSdh 			    "!vo_path_offline failed: vdip %s%d %p: path %s %p",
39024c06356bSdh 			    ddi_driver_name(vdip), ddi_get_instance(vdip),
39034c06356bSdh 			    (void *)vdip, mdi_pi_spathname(pip), (void *)pip));
39047c478bd9Sstevel@tonic-gate 		}
39057c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
39067c478bd9Sstevel@tonic-gate 	}
39077c478bd9Sstevel@tonic-gate 
39087c478bd9Sstevel@tonic-gate 	/*
39097c478bd9Sstevel@tonic-gate 	 * Set the mdi_pathinfo node state and clear the transient condition
39107c478bd9Sstevel@tonic-gate 	 */
39117c478bd9Sstevel@tonic-gate 	MDI_PI_SET_OFFLINE(pip);
39127c478bd9Sstevel@tonic-gate 	cv_broadcast(&MDI_PI(pip)->pi_state_cv);
39137c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
39147c478bd9Sstevel@tonic-gate 
39157c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
39167c478bd9Sstevel@tonic-gate 	if (rv == MDI_SUCCESS) {
39177c478bd9Sstevel@tonic-gate 		if (ct->ct_unstable == 0) {
39187c478bd9Sstevel@tonic-gate 			dev_info_t	*cdip = ct->ct_dip;
39197c478bd9Sstevel@tonic-gate 
39207c478bd9Sstevel@tonic-gate 			/*
39217c478bd9Sstevel@tonic-gate 			 * Onlining the mdi_pathinfo node will impact the
39227c478bd9Sstevel@tonic-gate 			 * client state Update the client and dev_info node
39237c478bd9Sstevel@tonic-gate 			 * state accordingly
39247c478bd9Sstevel@tonic-gate 			 */
39257c478bd9Sstevel@tonic-gate 			i_mdi_client_update_state(ct);
39267c478bd9Sstevel@tonic-gate 			rv = NDI_SUCCESS;
39277c478bd9Sstevel@tonic-gate 			if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_FAILED) {
39287c478bd9Sstevel@tonic-gate 				if (cdip &&
39297c478bd9Sstevel@tonic-gate 				    (i_ddi_node_state(cdip) >=
39307c478bd9Sstevel@tonic-gate 				    DS_INITIALIZED)) {
39317c478bd9Sstevel@tonic-gate 					MDI_CLIENT_UNLOCK(ct);
39324c06356bSdh 					rv = ndi_devi_offline(cdip,
39334c06356bSdh 					    NDI_DEVFS_CLEAN);
39347c478bd9Sstevel@tonic-gate 					MDI_CLIENT_LOCK(ct);
39357c478bd9Sstevel@tonic-gate 					if (rv != NDI_SUCCESS) {
39367c478bd9Sstevel@tonic-gate 						/*
39377c478bd9Sstevel@tonic-gate 						 * ndi_devi_offline failed.
39387c478bd9Sstevel@tonic-gate 						 * Reset client flags to
39397c478bd9Sstevel@tonic-gate 						 * online.
39407c478bd9Sstevel@tonic-gate 						 */
39414c06356bSdh 						MDI_DEBUG(4, (MDI_WARN, cdip,
39424c06356bSdh 						    "ndi_devi_offline failed: "
39434c06356bSdh 						    "error %x", rv));
39447c478bd9Sstevel@tonic-gate 						MDI_CLIENT_SET_ONLINE(ct);
39457c478bd9Sstevel@tonic-gate 					}
39467c478bd9Sstevel@tonic-gate 				}
39477c478bd9Sstevel@tonic-gate 			}
39487c478bd9Sstevel@tonic-gate 			/*
39497c478bd9Sstevel@tonic-gate 			 * Convert to MDI error code
39507c478bd9Sstevel@tonic-gate 			 */
39517c478bd9Sstevel@tonic-gate 			switch (rv) {
39527c478bd9Sstevel@tonic-gate 			case NDI_SUCCESS:
39537c478bd9Sstevel@tonic-gate 				rv = MDI_SUCCESS;
39547c478bd9Sstevel@tonic-gate 				break;
39557c478bd9Sstevel@tonic-gate 			case NDI_BUSY:
39567c478bd9Sstevel@tonic-gate 				rv = MDI_BUSY;
39577c478bd9Sstevel@tonic-gate 				break;
39587c478bd9Sstevel@tonic-gate 			default:
39597c478bd9Sstevel@tonic-gate 				rv = MDI_FAILURE;
39607c478bd9Sstevel@tonic-gate 				break;
39617c478bd9Sstevel@tonic-gate 			}
39627c478bd9Sstevel@tonic-gate 		}
39637c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_REPORT_DEV_NEEDED(ct);
39647c478bd9Sstevel@tonic-gate 		i_mdi_report_path_state(ct, pip);
39657c478bd9Sstevel@tonic-gate 	}
39667c478bd9Sstevel@tonic-gate 
39677c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate 	/*
39707c478bd9Sstevel@tonic-gate 	 * Change in the mdi_pathinfo node state will impact the client state
39717c478bd9Sstevel@tonic-gate 	 */
39724c06356bSdh 	MDI_DEBUG(2, (MDI_NOTE, ct->ct_dip,
39734c06356bSdh 	    "ct = %p pip = %p", (void *)ct, (void *)pip));
39747c478bd9Sstevel@tonic-gate 	return (rv);
39757c478bd9Sstevel@tonic-gate }
39767c478bd9Sstevel@tonic-gate 
3977f76de749SStephen Hanson /*
3978f76de749SStephen Hanson  * i_mdi_pi_online():
3979f76de749SStephen Hanson  *		Online a mdi_pathinfo node and call the vHCI driver's callback
3980f76de749SStephen Hanson  */
3981f76de749SStephen Hanson static int
i_mdi_pi_online(mdi_pathinfo_t * pip,int flags)3982f76de749SStephen Hanson i_mdi_pi_online(mdi_pathinfo_t *pip, int flags)
3983f76de749SStephen Hanson {
3984f76de749SStephen Hanson 	mdi_vhci_t	*vh = NULL;
3985f76de749SStephen Hanson 	mdi_client_t	*ct = NULL;
3986f76de749SStephen Hanson 	mdi_phci_t	*ph;
3987f76de749SStephen Hanson 	int		(*f)();
3988f76de749SStephen Hanson 	int		rv;
3989f76de749SStephen Hanson 
3990f76de749SStephen Hanson 	MDI_PI_LOCK(pip);
3991f76de749SStephen Hanson 	ph = MDI_PI(pip)->pi_phci;
3992f76de749SStephen Hanson 	vh = ph->ph_vhci;
3993f76de749SStephen Hanson 	ct = MDI_PI(pip)->pi_client;
3994f76de749SStephen Hanson 	MDI_PI_SET_ONLINING(pip)
3995f76de749SStephen Hanson 	MDI_PI_UNLOCK(pip);
3996f76de749SStephen Hanson 	f = vh->vh_ops->vo_pi_state_change;
3997c6f039c7SToomas Soome 	rv = MDI_SUCCESS;
3998f76de749SStephen Hanson 	if (f != NULL)
3999c6f039c7SToomas Soome 		rv = (*f)(vh->vh_dip, pip, MDI_PATHINFO_STATE_ONLINE, 0, flags);
4000f76de749SStephen Hanson 	MDI_CLIENT_LOCK(ct);
4001f76de749SStephen Hanson 	MDI_PI_LOCK(pip);
4002f76de749SStephen Hanson 	cv_broadcast(&MDI_PI(pip)->pi_state_cv);
4003f76de749SStephen Hanson 	MDI_PI_UNLOCK(pip);
4004f76de749SStephen Hanson 	if (rv == MDI_SUCCESS) {
4005f76de749SStephen Hanson 		dev_info_t	*cdip = ct->ct_dip;
4006f76de749SStephen Hanson 
4007f76de749SStephen Hanson 		i_mdi_client_update_state(ct);
4008f76de749SStephen Hanson 		if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_OPTIMAL ||
4009f76de749SStephen Hanson 		    MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_DEGRADED) {
4010f76de749SStephen Hanson 			if (cdip && !i_ddi_devi_attached(cdip)) {
4011f76de749SStephen Hanson 				MDI_CLIENT_UNLOCK(ct);
4012f76de749SStephen Hanson 				rv = ndi_devi_online(cdip, 0);
4013f76de749SStephen Hanson 				MDI_CLIENT_LOCK(ct);
4014f76de749SStephen Hanson 				if ((rv != NDI_SUCCESS) &&
4015f76de749SStephen Hanson 				    (MDI_CLIENT_STATE(ct) ==
4016f76de749SStephen Hanson 				    MDI_CLIENT_STATE_DEGRADED)) {
4017f76de749SStephen Hanson 					MDI_CLIENT_SET_OFFLINE(ct);
4018f76de749SStephen Hanson 				}
4019f76de749SStephen Hanson 				if (rv != NDI_SUCCESS) {
4020f76de749SStephen Hanson 					/* Reset the path state */
4021f76de749SStephen Hanson 					MDI_PI_LOCK(pip);
4022f76de749SStephen Hanson 					MDI_PI(pip)->pi_state =
4023f76de749SStephen Hanson 					    MDI_PI_OLD_STATE(pip);
4024f76de749SStephen Hanson 					MDI_PI_UNLOCK(pip);
4025f76de749SStephen Hanson 				}
4026f76de749SStephen Hanson 			}
4027f76de749SStephen Hanson 		}
4028f76de749SStephen Hanson 		switch (rv) {
4029f76de749SStephen Hanson 		case NDI_SUCCESS:
4030f76de749SStephen Hanson 			MDI_CLIENT_SET_REPORT_DEV_NEEDED(ct);
4031f76de749SStephen Hanson 			i_mdi_report_path_state(ct, pip);
4032f76de749SStephen Hanson 			rv = MDI_SUCCESS;
4033f76de749SStephen Hanson 			break;
4034f76de749SStephen Hanson 		case NDI_BUSY:
4035f76de749SStephen Hanson 			rv = MDI_BUSY;
4036f76de749SStephen Hanson 			break;
4037f76de749SStephen Hanson 		default:
4038f76de749SStephen Hanson 			rv = MDI_FAILURE;
4039f76de749SStephen Hanson 			break;
4040f76de749SStephen Hanson 		}
4041f76de749SStephen Hanson 	} else {
4042f76de749SStephen Hanson 		/* Reset the path state */
4043f76de749SStephen Hanson 		MDI_PI_LOCK(pip);
4044f76de749SStephen Hanson 		MDI_PI(pip)->pi_state = MDI_PI_OLD_STATE(pip);
4045f76de749SStephen Hanson 		MDI_PI_UNLOCK(pip);
4046f76de749SStephen Hanson 	}
4047f76de749SStephen Hanson 	MDI_CLIENT_UNLOCK(ct);
4048f76de749SStephen Hanson 	return (rv);
4049f76de749SStephen Hanson }
4050f76de749SStephen Hanson 
405155e592a2SRandall Ralphs /*
405255e592a2SRandall Ralphs  * mdi_pi_get_node_name():
405355e592a2SRandall Ralphs  *              Get the name associated with a mdi_pathinfo node.
405455e592a2SRandall Ralphs  *              Since pathinfo nodes are not directly named, we
405555e592a2SRandall Ralphs  *              return the node_name of the client.
405655e592a2SRandall Ralphs  *
405755e592a2SRandall Ralphs  * Return Values:
405855e592a2SRandall Ralphs  *              char *
405955e592a2SRandall Ralphs  */
406055e592a2SRandall Ralphs char *
mdi_pi_get_node_name(mdi_pathinfo_t * pip)406155e592a2SRandall Ralphs mdi_pi_get_node_name(mdi_pathinfo_t *pip)
406255e592a2SRandall Ralphs {
406355e592a2SRandall Ralphs 	mdi_client_t    *ct;
406455e592a2SRandall Ralphs 
406555e592a2SRandall Ralphs 	if (pip == NULL)
406655e592a2SRandall Ralphs 		return (NULL);
406755e592a2SRandall Ralphs 	ct = MDI_PI(pip)->pi_client;
406855e592a2SRandall Ralphs 	if ((ct == NULL) || (ct->ct_dip == NULL))
406955e592a2SRandall Ralphs 		return (NULL);
407055e592a2SRandall Ralphs 	return (ddi_node_name(ct->ct_dip));
407155e592a2SRandall Ralphs }
40727c478bd9Sstevel@tonic-gate 
40737c478bd9Sstevel@tonic-gate /*
40747c478bd9Sstevel@tonic-gate  * mdi_pi_get_addr():
40757c478bd9Sstevel@tonic-gate  *		Get the unit address associated with a mdi_pathinfo node
40767c478bd9Sstevel@tonic-gate  *
40777c478bd9Sstevel@tonic-gate  * Return Values:
40787c478bd9Sstevel@tonic-gate  *		char *
40797c478bd9Sstevel@tonic-gate  */
40807c478bd9Sstevel@tonic-gate char *
mdi_pi_get_addr(mdi_pathinfo_t * pip)40817c478bd9Sstevel@tonic-gate mdi_pi_get_addr(mdi_pathinfo_t *pip)
40827c478bd9Sstevel@tonic-gate {
40837c478bd9Sstevel@tonic-gate 	if (pip == NULL)
40847c478bd9Sstevel@tonic-gate 		return (NULL);
40857c478bd9Sstevel@tonic-gate 
408672a50065Scth 	return (MDI_PI(pip)->pi_addr);
40877c478bd9Sstevel@tonic-gate }
40887c478bd9Sstevel@tonic-gate 
4089602ca9eaScth /*
4090602ca9eaScth  * mdi_pi_get_path_instance():
4091602ca9eaScth  *		Get the 'path_instance' of a mdi_pathinfo node
4092602ca9eaScth  *
4093602ca9eaScth  * Return Values:
4094602ca9eaScth  *		path_instance
4095602ca9eaScth  */
4096602ca9eaScth int
mdi_pi_get_path_instance(mdi_pathinfo_t * pip)4097602ca9eaScth mdi_pi_get_path_instance(mdi_pathinfo_t *pip)
4098602ca9eaScth {
4099602ca9eaScth 	if (pip == NULL)
4100602ca9eaScth 		return (0);
4101602ca9eaScth 
4102602ca9eaScth 	return (MDI_PI(pip)->pi_path_instance);
4103602ca9eaScth }
4104602ca9eaScth 
4105602ca9eaScth /*
4106602ca9eaScth  * mdi_pi_pathname():
4107602ca9eaScth  *		Return pointer to path to pathinfo node.
4108602ca9eaScth  */
4109602ca9eaScth char *
mdi_pi_pathname(mdi_pathinfo_t * pip)4110602ca9eaScth mdi_pi_pathname(mdi_pathinfo_t *pip)
4111602ca9eaScth {
4112602ca9eaScth 	if (pip == NULL)
4113602ca9eaScth 		return (NULL);
4114602ca9eaScth 	return (mdi_pi_pathname_by_instance(mdi_pi_get_path_instance(pip)));
4115602ca9eaScth }
4116602ca9eaScth 
41174c06356bSdh /*
41184c06356bSdh  * mdi_pi_spathname():
41194c06356bSdh  *		Return pointer to shortpath to pathinfo node. Used for debug
41204c06356bSdh  *		messages, so return "" instead of NULL when unknown.
41214c06356bSdh  */
41224c06356bSdh char *
mdi_pi_spathname(mdi_pathinfo_t * pip)41234c06356bSdh mdi_pi_spathname(mdi_pathinfo_t *pip)
41244c06356bSdh {
41254c06356bSdh 	char	*spath = "";
41264c06356bSdh 
41274c06356bSdh 	if (pip) {
41284c06356bSdh 		spath = mdi_pi_spathname_by_instance(
41294c06356bSdh 		    mdi_pi_get_path_instance(pip));
41304c06356bSdh 		if (spath == NULL)
41314c06356bSdh 			spath = "";
41324c06356bSdh 	}
41334c06356bSdh 	return (spath);
41344c06356bSdh }
41354c06356bSdh 
413638c67cbdSjiang wu - Sun Microsystems - Beijing China char *
mdi_pi_pathname_obp(mdi_pathinfo_t * pip,char * path)413738c67cbdSjiang wu - Sun Microsystems - Beijing China mdi_pi_pathname_obp(mdi_pathinfo_t *pip, char *path)
413838c67cbdSjiang wu - Sun Microsystems - Beijing China {
413938c67cbdSjiang wu - Sun Microsystems - Beijing China 	char *obp_path = NULL;
414038c67cbdSjiang wu - Sun Microsystems - Beijing China 	if ((pip == NULL) || (path == NULL))
414138c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (NULL);
414238c67cbdSjiang wu - Sun Microsystems - Beijing China 
414338c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (mdi_prop_lookup_string(pip, "obp-path", &obp_path) == MDI_SUCCESS) {
414438c67cbdSjiang wu - Sun Microsystems - Beijing China 		(void) strcpy(path, obp_path);
414538c67cbdSjiang wu - Sun Microsystems - Beijing China 		(void) mdi_prop_free(obp_path);
414638c67cbdSjiang wu - Sun Microsystems - Beijing China 	} else {
414738c67cbdSjiang wu - Sun Microsystems - Beijing China 		path = NULL;
414838c67cbdSjiang wu - Sun Microsystems - Beijing China 	}
414938c67cbdSjiang wu - Sun Microsystems - Beijing China 	return (path);
415038c67cbdSjiang wu - Sun Microsystems - Beijing China }
415138c67cbdSjiang wu - Sun Microsystems - Beijing China 
415238c67cbdSjiang wu - Sun Microsystems - Beijing China int
mdi_pi_pathname_obp_set(mdi_pathinfo_t * pip,char * component)415338c67cbdSjiang wu - Sun Microsystems - Beijing China mdi_pi_pathname_obp_set(mdi_pathinfo_t *pip, char *component)
415438c67cbdSjiang wu - Sun Microsystems - Beijing China {
415538c67cbdSjiang wu - Sun Microsystems - Beijing China 	dev_info_t *pdip;
4156caa9369fSjiang wu - Sun Microsystems - Beijing China 	char *obp_path = NULL;
4157caa9369fSjiang wu - Sun Microsystems - Beijing China 	int rc = MDI_FAILURE;
415838c67cbdSjiang wu - Sun Microsystems - Beijing China 
415938c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (pip == NULL)
416038c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (MDI_FAILURE);
416138c67cbdSjiang wu - Sun Microsystems - Beijing China 
416238c67cbdSjiang wu - Sun Microsystems - Beijing China 	pdip = mdi_pi_get_phci(pip);
416338c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (pdip == NULL)
416438c67cbdSjiang wu - Sun Microsystems - Beijing China 		return (MDI_FAILURE);
416538c67cbdSjiang wu - Sun Microsystems - Beijing China 
4166caa9369fSjiang wu - Sun Microsystems - Beijing China 	obp_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
4167caa9369fSjiang wu - Sun Microsystems - Beijing China 
416838c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (ddi_pathname_obp(pdip, obp_path) == NULL) {
416938c67cbdSjiang wu - Sun Microsystems - Beijing China 		(void) ddi_pathname(pdip, obp_path);
417038c67cbdSjiang wu - Sun Microsystems - Beijing China 	}
417138c67cbdSjiang wu - Sun Microsystems - Beijing China 
417238c67cbdSjiang wu - Sun Microsystems - Beijing China 	if (component) {
4173caa9369fSjiang wu - Sun Microsystems - Beijing China 		(void) strncat(obp_path, "/", MAXPATHLEN);
4174caa9369fSjiang wu - Sun Microsystems - Beijing China 		(void) strncat(obp_path, component, MAXPATHLEN);
4175caa9369fSjiang wu - Sun Microsystems - Beijing China 	}
4176caa9369fSjiang wu - Sun Microsystems - Beijing China 	rc = mdi_prop_update_string(pip, "obp-path", obp_path);
41774c06356bSdh 
4178caa9369fSjiang wu - Sun Microsystems - Beijing China 	if (obp_path)
4179caa9369fSjiang wu - Sun Microsystems - Beijing China 		kmem_free(obp_path, MAXPATHLEN);
4180caa9369fSjiang wu - Sun Microsystems - Beijing China 	return (rc);
418138c67cbdSjiang wu - Sun Microsystems - Beijing China }
418238c67cbdSjiang wu - Sun Microsystems - Beijing China 
41837c478bd9Sstevel@tonic-gate /*
41847c478bd9Sstevel@tonic-gate  * mdi_pi_get_client():
41857c478bd9Sstevel@tonic-gate  *		Get the client devinfo associated with a mdi_pathinfo node
41867c478bd9Sstevel@tonic-gate  *
41877c478bd9Sstevel@tonic-gate  * Return Values:
41887c478bd9Sstevel@tonic-gate  *		Handle to client device dev_info node
41897c478bd9Sstevel@tonic-gate  */
41907c478bd9Sstevel@tonic-gate dev_info_t *
mdi_pi_get_client(mdi_pathinfo_t * pip)41917c478bd9Sstevel@tonic-gate mdi_pi_get_client(mdi_pathinfo_t *pip)
41927c478bd9Sstevel@tonic-gate {
41937c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
41947c478bd9Sstevel@tonic-gate 	if (pip) {
41957c478bd9Sstevel@tonic-gate 		dip = MDI_PI(pip)->pi_client->ct_dip;
41967c478bd9Sstevel@tonic-gate 	}
41977c478bd9Sstevel@tonic-gate 	return (dip);
41987c478bd9Sstevel@tonic-gate }
41997c478bd9Sstevel@tonic-gate 
42007c478bd9Sstevel@tonic-gate /*
42017c478bd9Sstevel@tonic-gate  * mdi_pi_get_phci():
42027c478bd9Sstevel@tonic-gate  *		Get the pHCI devinfo associated with the mdi_pathinfo node
42037c478bd9Sstevel@tonic-gate  * Return Values:
42047c478bd9Sstevel@tonic-gate  *		Handle to dev_info node
42057c478bd9Sstevel@tonic-gate  */
42067c478bd9Sstevel@tonic-gate dev_info_t *
mdi_pi_get_phci(mdi_pathinfo_t * pip)42077c478bd9Sstevel@tonic-gate mdi_pi_get_phci(mdi_pathinfo_t *pip)
42087c478bd9Sstevel@tonic-gate {
42097c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
42104c06356bSdh 	mdi_phci_t	*ph;
42114c06356bSdh 
42127c478bd9Sstevel@tonic-gate 	if (pip) {
42134c06356bSdh 		ph = MDI_PI(pip)->pi_phci;
42144c06356bSdh 		if (ph)
42154c06356bSdh 			dip = ph->ph_dip;
42167c478bd9Sstevel@tonic-gate 	}
42177c478bd9Sstevel@tonic-gate 	return (dip);
42187c478bd9Sstevel@tonic-gate }
42197c478bd9Sstevel@tonic-gate 
42207c478bd9Sstevel@tonic-gate /*
42217c478bd9Sstevel@tonic-gate  * mdi_pi_get_client_private():
42227c478bd9Sstevel@tonic-gate  *		Get the client private information associated with the
42237c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node
42247c478bd9Sstevel@tonic-gate  */
42257c478bd9Sstevel@tonic-gate void *
mdi_pi_get_client_private(mdi_pathinfo_t * pip)42267c478bd9Sstevel@tonic-gate mdi_pi_get_client_private(mdi_pathinfo_t *pip)
42277c478bd9Sstevel@tonic-gate {
42287c478bd9Sstevel@tonic-gate 	void *cprivate = NULL;
42297c478bd9Sstevel@tonic-gate 	if (pip) {
42307c478bd9Sstevel@tonic-gate 		cprivate = MDI_PI(pip)->pi_cprivate;
42317c478bd9Sstevel@tonic-gate 	}
42327c478bd9Sstevel@tonic-gate 	return (cprivate);
42337c478bd9Sstevel@tonic-gate }
42347c478bd9Sstevel@tonic-gate 
42357c478bd9Sstevel@tonic-gate /*
42367c478bd9Sstevel@tonic-gate  * mdi_pi_set_client_private():
42377c478bd9Sstevel@tonic-gate  *		Set the client private information in the mdi_pathinfo node
42387c478bd9Sstevel@tonic-gate  */
42397c478bd9Sstevel@tonic-gate void
mdi_pi_set_client_private(mdi_pathinfo_t * pip,void * priv)42407c478bd9Sstevel@tonic-gate mdi_pi_set_client_private(mdi_pathinfo_t *pip, void *priv)
42417c478bd9Sstevel@tonic-gate {
42427c478bd9Sstevel@tonic-gate 	if (pip) {
42437c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_cprivate = priv;
42447c478bd9Sstevel@tonic-gate 	}
42457c478bd9Sstevel@tonic-gate }
42467c478bd9Sstevel@tonic-gate 
42477c478bd9Sstevel@tonic-gate /*
42487c478bd9Sstevel@tonic-gate  * mdi_pi_get_phci_private():
42497c478bd9Sstevel@tonic-gate  *		Get the pHCI private information associated with the
42507c478bd9Sstevel@tonic-gate  *		mdi_pathinfo node
42517c478bd9Sstevel@tonic-gate  */
42527c478bd9Sstevel@tonic-gate caddr_t
mdi_pi_get_phci_private(mdi_pathinfo_t * pip)42537c478bd9Sstevel@tonic-gate mdi_pi_get_phci_private(mdi_pathinfo_t *pip)
42547c478bd9Sstevel@tonic-gate {
42557c478bd9Sstevel@tonic-gate 	caddr_t	pprivate = NULL;
42564c06356bSdh 
42577c478bd9Sstevel@tonic-gate 	if (pip) {
42587c478bd9Sstevel@tonic-gate 		pprivate = MDI_PI(pip)->pi_pprivate;
42597c478bd9Sstevel@tonic-gate 	}
42607c478bd9Sstevel@tonic-gate 	return (pprivate);
42617c478bd9Sstevel@tonic-gate }
42627c478bd9Sstevel@tonic-gate 
42637c478bd9Sstevel@tonic-gate /*
42647c478bd9Sstevel@tonic-gate  * mdi_pi_set_phci_private():
42657c478bd9Sstevel@tonic-gate  *		Set the pHCI private information in the mdi_pathinfo node
42667c478bd9Sstevel@tonic-gate  */
42677c478bd9Sstevel@tonic-gate void
mdi_pi_set_phci_private(mdi_pathinfo_t * pip,caddr_t priv)42687c478bd9Sstevel@tonic-gate mdi_pi_set_phci_private(mdi_pathinfo_t *pip, caddr_t priv)
42697c478bd9Sstevel@tonic-gate {
42707c478bd9Sstevel@tonic-gate 	if (pip) {
42717c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_pprivate = priv;
42727c478bd9Sstevel@tonic-gate 	}
42737c478bd9Sstevel@tonic-gate }
42747c478bd9Sstevel@tonic-gate 
42757c478bd9Sstevel@tonic-gate /*
42767c478bd9Sstevel@tonic-gate  * mdi_pi_get_state():
42777c478bd9Sstevel@tonic-gate  *		Get the mdi_pathinfo node state. Transient states are internal
42787c478bd9Sstevel@tonic-gate  *		and not provided to the users
42797c478bd9Sstevel@tonic-gate  */
42807c478bd9Sstevel@tonic-gate mdi_pathinfo_state_t
mdi_pi_get_state(mdi_pathinfo_t * pip)42817c478bd9Sstevel@tonic-gate mdi_pi_get_state(mdi_pathinfo_t *pip)
42827c478bd9Sstevel@tonic-gate {
42837c478bd9Sstevel@tonic-gate 	mdi_pathinfo_state_t    state = MDI_PATHINFO_STATE_INIT;
42847c478bd9Sstevel@tonic-gate 
42857c478bd9Sstevel@tonic-gate 	if (pip) {
42867c478bd9Sstevel@tonic-gate 		if (MDI_PI_IS_TRANSIENT(pip)) {
42877c478bd9Sstevel@tonic-gate 			/*
42887c478bd9Sstevel@tonic-gate 			 * mdi_pathinfo is in state transition.  Return the
42897c478bd9Sstevel@tonic-gate 			 * last good state.
42907c478bd9Sstevel@tonic-gate 			 */
42917c478bd9Sstevel@tonic-gate 			state = MDI_PI_OLD_STATE(pip);
42927c478bd9Sstevel@tonic-gate 		} else {
42937c478bd9Sstevel@tonic-gate 			state = MDI_PI_STATE(pip);
42947c478bd9Sstevel@tonic-gate 		}
42957c478bd9Sstevel@tonic-gate 	}
42967c478bd9Sstevel@tonic-gate 	return (state);
42977c478bd9Sstevel@tonic-gate }
42987c478bd9Sstevel@tonic-gate 
42994c06356bSdh /*
43004c06356bSdh  * mdi_pi_get_flags():
43014c06356bSdh  *		Get the mdi_pathinfo node flags.
43024c06356bSdh  */
43034c06356bSdh uint_t
mdi_pi_get_flags(mdi_pathinfo_t * pip)43044c06356bSdh mdi_pi_get_flags(mdi_pathinfo_t *pip)
43054c06356bSdh {
43064c06356bSdh 	return (pip ? MDI_PI(pip)->pi_flags : 0);
43074c06356bSdh }
43084c06356bSdh 
43097c478bd9Sstevel@tonic-gate /*
43107c478bd9Sstevel@tonic-gate  * Note that the following function needs to be the new interface for
43117c478bd9Sstevel@tonic-gate  * mdi_pi_get_state when mpxio gets integrated to ON.
43127c478bd9Sstevel@tonic-gate  */
43137c478bd9Sstevel@tonic-gate int
mdi_pi_get_state2(mdi_pathinfo_t * pip,mdi_pathinfo_state_t * state,uint32_t * ext_state)43147c478bd9Sstevel@tonic-gate mdi_pi_get_state2(mdi_pathinfo_t *pip, mdi_pathinfo_state_t *state,
43157c478bd9Sstevel@tonic-gate 		uint32_t *ext_state)
43167c478bd9Sstevel@tonic-gate {
43177c478bd9Sstevel@tonic-gate 	*state = MDI_PATHINFO_STATE_INIT;
43187c478bd9Sstevel@tonic-gate 
43197c478bd9Sstevel@tonic-gate 	if (pip) {
43207c478bd9Sstevel@tonic-gate 		if (MDI_PI_IS_TRANSIENT(pip)) {
43217c478bd9Sstevel@tonic-gate 			/*
43227c478bd9Sstevel@tonic-gate 			 * mdi_pathinfo is in state transition.  Return the
43237c478bd9Sstevel@tonic-gate 			 * last good state.
43247c478bd9Sstevel@tonic-gate 			 */
43257c478bd9Sstevel@tonic-gate 			*state = MDI_PI_OLD_STATE(pip);
43267c478bd9Sstevel@tonic-gate 			*ext_state = MDI_PI_OLD_EXT_STATE(pip);
43277c478bd9Sstevel@tonic-gate 		} else {
43287c478bd9Sstevel@tonic-gate 			*state = MDI_PI_STATE(pip);
43297c478bd9Sstevel@tonic-gate 			*ext_state = MDI_PI_EXT_STATE(pip);
43307c478bd9Sstevel@tonic-gate 		}
43317c478bd9Sstevel@tonic-gate 	}
43327c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
43337c478bd9Sstevel@tonic-gate }
43347c478bd9Sstevel@tonic-gate 
43357c478bd9Sstevel@tonic-gate /*
43367c478bd9Sstevel@tonic-gate  * mdi_pi_get_preferred:
43377c478bd9Sstevel@tonic-gate  *	Get the preferred path flag
43387c478bd9Sstevel@tonic-gate  */
43397c478bd9Sstevel@tonic-gate int
mdi_pi_get_preferred(mdi_pathinfo_t * pip)43407c478bd9Sstevel@tonic-gate mdi_pi_get_preferred(mdi_pathinfo_t *pip)
43417c478bd9Sstevel@tonic-gate {
43427c478bd9Sstevel@tonic-gate 	if (pip) {
43437c478bd9Sstevel@tonic-gate 		return (MDI_PI(pip)->pi_preferred);
43447c478bd9Sstevel@tonic-gate 	}
43457c478bd9Sstevel@tonic-gate 	return (0);
43467c478bd9Sstevel@tonic-gate }
43477c478bd9Sstevel@tonic-gate 
43487c478bd9Sstevel@tonic-gate /*
43497c478bd9Sstevel@tonic-gate  * mdi_pi_set_preferred:
43507c478bd9Sstevel@tonic-gate  *	Set the preferred path flag
43517c478bd9Sstevel@tonic-gate  */
43527c478bd9Sstevel@tonic-gate void
mdi_pi_set_preferred(mdi_pathinfo_t * pip,int preferred)43537c478bd9Sstevel@tonic-gate mdi_pi_set_preferred(mdi_pathinfo_t *pip, int preferred)
43547c478bd9Sstevel@tonic-gate {
43557c478bd9Sstevel@tonic-gate 	if (pip) {
43567c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_preferred = preferred;
43577c478bd9Sstevel@tonic-gate 	}
43587c478bd9Sstevel@tonic-gate }
43597c478bd9Sstevel@tonic-gate 
43607c478bd9Sstevel@tonic-gate /*
43617c478bd9Sstevel@tonic-gate  * mdi_pi_set_state():
43627c478bd9Sstevel@tonic-gate  *		Set the mdi_pathinfo node state
43637c478bd9Sstevel@tonic-gate  */
43647c478bd9Sstevel@tonic-gate void
mdi_pi_set_state(mdi_pathinfo_t * pip,mdi_pathinfo_state_t state)43657c478bd9Sstevel@tonic-gate mdi_pi_set_state(mdi_pathinfo_t *pip, mdi_pathinfo_state_t state)
43667c478bd9Sstevel@tonic-gate {
43677c478bd9Sstevel@tonic-gate 	uint32_t	ext_state;
43687c478bd9Sstevel@tonic-gate 
43697c478bd9Sstevel@tonic-gate 	if (pip) {
43707c478bd9Sstevel@tonic-gate 		ext_state = MDI_PI(pip)->pi_state & MDI_PATHINFO_EXT_STATE_MASK;
43717c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_state = state;
43727c478bd9Sstevel@tonic-gate 		MDI_PI(pip)->pi_state |= ext_state;
43734c06356bSdh 
43744c06356bSdh 		/* Path has changed state, invalidate DINFOCACHE snap shot. */
43754c06356bSdh 		i_ddi_di_cache_invalidate();
43767c478bd9Sstevel@tonic-gate 	}
43777c478bd9Sstevel@tonic-gate }
43787c478bd9Sstevel@tonic-gate 
43797c478bd9Sstevel@tonic-gate /*
43807c478bd9Sstevel@tonic-gate  * Property functions:
43817c478bd9Sstevel@tonic-gate  */
43827c478bd9Sstevel@tonic-gate int
i_map_nvlist_error_to_mdi(int val)43837c478bd9Sstevel@tonic-gate i_map_nvlist_error_to_mdi(int val)
43847c478bd9Sstevel@tonic-gate {
43857c478bd9Sstevel@tonic-gate 	int rv;
43867c478bd9Sstevel@tonic-gate 
43877c478bd9Sstevel@tonic-gate 	switch (val) {
43887c478bd9Sstevel@tonic-gate 	case 0:
43897c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_SUCCESS;
43907c478bd9Sstevel@tonic-gate 		break;
43917c478bd9Sstevel@tonic-gate 	case EINVAL:
43927c478bd9Sstevel@tonic-gate 	case ENOTSUP:
43937c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_INVAL_ARG;
43947c478bd9Sstevel@tonic-gate 		break;
43957c478bd9Sstevel@tonic-gate 	case ENOMEM:
43967c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_NO_MEMORY;
43977c478bd9Sstevel@tonic-gate 		break;
43987c478bd9Sstevel@tonic-gate 	default:
43997c478bd9Sstevel@tonic-gate 		rv = DDI_PROP_NOT_FOUND;
44007c478bd9Sstevel@tonic-gate 		break;
44017c478bd9Sstevel@tonic-gate 	}
44027c478bd9Sstevel@tonic-gate 	return (rv);
44037c478bd9Sstevel@tonic-gate }
44047c478bd9Sstevel@tonic-gate 
44057c478bd9Sstevel@tonic-gate /*
44067c478bd9Sstevel@tonic-gate  * mdi_pi_get_next_prop():
4407d27aea3fSDan Cross  *		Property walk function.  The caller should hold mdi_pi_lock()
44087c478bd9Sstevel@tonic-gate  *		and release by calling mdi_pi_unlock() at the end of walk to
44097c478bd9Sstevel@tonic-gate  *		get a consistent value.
44107c478bd9Sstevel@tonic-gate  */
44117c478bd9Sstevel@tonic-gate nvpair_t *
mdi_pi_get_next_prop(mdi_pathinfo_t * pip,nvpair_t * prev)44127c478bd9Sstevel@tonic-gate mdi_pi_get_next_prop(mdi_pathinfo_t *pip, nvpair_t *prev)
44137c478bd9Sstevel@tonic-gate {
44147c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
44157c478bd9Sstevel@tonic-gate 		return (NULL);
44167c478bd9Sstevel@tonic-gate 	}
44175e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
44187c478bd9Sstevel@tonic-gate 	return (nvlist_next_nvpair(MDI_PI(pip)->pi_prop, prev));
44197c478bd9Sstevel@tonic-gate }
44207c478bd9Sstevel@tonic-gate 
44217c478bd9Sstevel@tonic-gate /*
44227c478bd9Sstevel@tonic-gate  * mdi_prop_remove():
4423d27aea3fSDan Cross  *		Remove the named property from the named list.
44247c478bd9Sstevel@tonic-gate  */
44257c478bd9Sstevel@tonic-gate int
mdi_prop_remove(mdi_pathinfo_t * pip,char * name)44267c478bd9Sstevel@tonic-gate mdi_prop_remove(mdi_pathinfo_t *pip, char *name)
44277c478bd9Sstevel@tonic-gate {
44287c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
44297c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44307c478bd9Sstevel@tonic-gate 	}
44315e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
44327c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
44337c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
44347c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
44357c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44367c478bd9Sstevel@tonic-gate 	}
44377c478bd9Sstevel@tonic-gate 	if (name) {
44387c478bd9Sstevel@tonic-gate 		(void) nvlist_remove_all(MDI_PI(pip)->pi_prop, name);
44397c478bd9Sstevel@tonic-gate 	} else {
44407c478bd9Sstevel@tonic-gate 		char		nvp_name[MAXNAMELEN];
44417c478bd9Sstevel@tonic-gate 		nvpair_t	*nvp;
44427c478bd9Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(MDI_PI(pip)->pi_prop, NULL);
44437c478bd9Sstevel@tonic-gate 		while (nvp) {
44447c478bd9Sstevel@tonic-gate 			nvpair_t	*next;
44457c478bd9Sstevel@tonic-gate 			next = nvlist_next_nvpair(MDI_PI(pip)->pi_prop, nvp);
44464c06356bSdh 			(void) snprintf(nvp_name, sizeof(nvp_name), "%s",
44477c478bd9Sstevel@tonic-gate 			    nvpair_name(nvp));
44487c478bd9Sstevel@tonic-gate 			(void) nvlist_remove_all(MDI_PI(pip)->pi_prop,
44497c478bd9Sstevel@tonic-gate 			    nvp_name);
44507c478bd9Sstevel@tonic-gate 			nvp = next;
44517c478bd9Sstevel@tonic-gate 		}
44527c478bd9Sstevel@tonic-gate 	}
44537c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
44547c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
44557c478bd9Sstevel@tonic-gate }
44567c478bd9Sstevel@tonic-gate 
44577c478bd9Sstevel@tonic-gate /*
44587c478bd9Sstevel@tonic-gate  * mdi_prop_size():
4459d27aea3fSDan Cross  *		Get buffer size needed to pack the property data.
4460d27aea3fSDan Cross  *		Caller should hold the mdi_pathinfo_t lock to get a consistent
44617c478bd9Sstevel@tonic-gate  *		buffer size.
44627c478bd9Sstevel@tonic-gate  */
44637c478bd9Sstevel@tonic-gate int
mdi_prop_size(mdi_pathinfo_t * pip,size_t * buflenp)44647c478bd9Sstevel@tonic-gate mdi_prop_size(mdi_pathinfo_t *pip, size_t *buflenp)
44657c478bd9Sstevel@tonic-gate {
44667c478bd9Sstevel@tonic-gate 	int	rv;
44677c478bd9Sstevel@tonic-gate 	size_t	bufsize;
44687c478bd9Sstevel@tonic-gate 
44697c478bd9Sstevel@tonic-gate 	*buflenp = 0;
44707c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
44717c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44727c478bd9Sstevel@tonic-gate 	}
44735e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
44747c478bd9Sstevel@tonic-gate 	rv = nvlist_size(MDI_PI(pip)->pi_prop,
44757c478bd9Sstevel@tonic-gate 	    &bufsize, NV_ENCODE_NATIVE);
44767c478bd9Sstevel@tonic-gate 	*buflenp = bufsize;
44777c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
44787c478bd9Sstevel@tonic-gate }
44797c478bd9Sstevel@tonic-gate 
44807c478bd9Sstevel@tonic-gate /*
44817c478bd9Sstevel@tonic-gate  * mdi_prop_pack():
4482d27aea3fSDan Cross  *		pack the property list.  The caller should hold the
44837c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node to get a consistent data
44847c478bd9Sstevel@tonic-gate  */
44857c478bd9Sstevel@tonic-gate int
mdi_prop_pack(mdi_pathinfo_t * pip,char ** bufp,uint_t buflen)44867c478bd9Sstevel@tonic-gate mdi_prop_pack(mdi_pathinfo_t *pip, char **bufp, uint_t buflen)
44877c478bd9Sstevel@tonic-gate {
44887c478bd9Sstevel@tonic-gate 	int	rv;
44897c478bd9Sstevel@tonic-gate 	size_t	bufsize;
44907c478bd9Sstevel@tonic-gate 
44917c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || MDI_PI(pip)->pi_prop == NULL) {
44927c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
44937c478bd9Sstevel@tonic-gate 	}
44947c478bd9Sstevel@tonic-gate 
44955e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
44967c478bd9Sstevel@tonic-gate 
44977c478bd9Sstevel@tonic-gate 	bufsize = buflen;
44987c478bd9Sstevel@tonic-gate 	rv = nvlist_pack(MDI_PI(pip)->pi_prop, bufp, (size_t *)&bufsize,
44997c478bd9Sstevel@tonic-gate 	    NV_ENCODE_NATIVE, KM_SLEEP);
45007c478bd9Sstevel@tonic-gate 
45017c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
45027c478bd9Sstevel@tonic-gate }
45037c478bd9Sstevel@tonic-gate 
45047c478bd9Sstevel@tonic-gate /*
45057c478bd9Sstevel@tonic-gate  * mdi_prop_update_byte():
45067c478bd9Sstevel@tonic-gate  *		Create/Update a byte property
45077c478bd9Sstevel@tonic-gate  */
45087c478bd9Sstevel@tonic-gate int
mdi_prop_update_byte(mdi_pathinfo_t * pip,char * name,uchar_t data)45097c478bd9Sstevel@tonic-gate mdi_prop_update_byte(mdi_pathinfo_t *pip, char *name, uchar_t data)
45107c478bd9Sstevel@tonic-gate {
45117c478bd9Sstevel@tonic-gate 	int rv;
45127c478bd9Sstevel@tonic-gate 
45137c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
45147c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
45157c478bd9Sstevel@tonic-gate 	}
45165e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
45177c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
45187c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
45197c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
45207c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
45217c478bd9Sstevel@tonic-gate 	}
45227c478bd9Sstevel@tonic-gate 	rv = nvlist_add_byte(MDI_PI(pip)->pi_prop, name, data);
45237c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
45247c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
45257c478bd9Sstevel@tonic-gate }
45267c478bd9Sstevel@tonic-gate 
45277c478bd9Sstevel@tonic-gate /*
45287c478bd9Sstevel@tonic-gate  * mdi_prop_update_byte_array():
45297c478bd9Sstevel@tonic-gate  *		Create/Update a byte array property
45307c478bd9Sstevel@tonic-gate  */
45317c478bd9Sstevel@tonic-gate int
mdi_prop_update_byte_array(mdi_pathinfo_t * pip,char * name,uchar_t * data,uint_t nelements)45327c478bd9Sstevel@tonic-gate mdi_prop_update_byte_array(mdi_pathinfo_t *pip, char *name, uchar_t *data,
45337c478bd9Sstevel@tonic-gate     uint_t nelements)
45347c478bd9Sstevel@tonic-gate {
45357c478bd9Sstevel@tonic-gate 	int rv;
45367c478bd9Sstevel@tonic-gate 
45377c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
45387c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
45397c478bd9Sstevel@tonic-gate 	}
45405e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
45417c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
45427c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
45437c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
45447c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
45457c478bd9Sstevel@tonic-gate 	}
45467c478bd9Sstevel@tonic-gate 	rv = nvlist_add_byte_array(MDI_PI(pip)->pi_prop, name, data, nelements);
45477c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
45487c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
45497c478bd9Sstevel@tonic-gate }
45507c478bd9Sstevel@tonic-gate 
45517c478bd9Sstevel@tonic-gate /*
45527c478bd9Sstevel@tonic-gate  * mdi_prop_update_int():
45537c478bd9Sstevel@tonic-gate  *		Create/Update a 32 bit integer property
45547c478bd9Sstevel@tonic-gate  */
45557c478bd9Sstevel@tonic-gate int
mdi_prop_update_int(mdi_pathinfo_t * pip,char * name,int data)45567c478bd9Sstevel@tonic-gate mdi_prop_update_int(mdi_pathinfo_t *pip, char *name, int data)
45577c478bd9Sstevel@tonic-gate {
45587c478bd9Sstevel@tonic-gate 	int rv;
45597c478bd9Sstevel@tonic-gate 
45607c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
45617c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
45627c478bd9Sstevel@tonic-gate 	}
45635e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
45647c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
45657c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
45667c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
45677c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
45687c478bd9Sstevel@tonic-gate 	}
45697c478bd9Sstevel@tonic-gate 	rv = nvlist_add_int32(MDI_PI(pip)->pi_prop, name, (int32_t)data);
45707c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
45717c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
45727c478bd9Sstevel@tonic-gate }
45737c478bd9Sstevel@tonic-gate 
45747c478bd9Sstevel@tonic-gate /*
45757c478bd9Sstevel@tonic-gate  * mdi_prop_update_int64():
45767c478bd9Sstevel@tonic-gate  *		Create/Update a 64 bit integer property
45777c478bd9Sstevel@tonic-gate  */
45787c478bd9Sstevel@tonic-gate int
mdi_prop_update_int64(mdi_pathinfo_t * pip,char * name,int64_t data)45797c478bd9Sstevel@tonic-gate mdi_prop_update_int64(mdi_pathinfo_t *pip, char *name, int64_t data)
45807c478bd9Sstevel@tonic-gate {
45817c478bd9Sstevel@tonic-gate 	int rv;
45827c478bd9Sstevel@tonic-gate 
45837c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
45847c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
45857c478bd9Sstevel@tonic-gate 	}
45865e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
45877c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
45887c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
45897c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
45907c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
45917c478bd9Sstevel@tonic-gate 	}
45927c478bd9Sstevel@tonic-gate 	rv = nvlist_add_int64(MDI_PI(pip)->pi_prop, name, data);
45937c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
45947c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
45957c478bd9Sstevel@tonic-gate }
45967c478bd9Sstevel@tonic-gate 
45977c478bd9Sstevel@tonic-gate /*
45987c478bd9Sstevel@tonic-gate  * mdi_prop_update_int_array():
45997c478bd9Sstevel@tonic-gate  *		Create/Update a int array property
46007c478bd9Sstevel@tonic-gate  */
46017c478bd9Sstevel@tonic-gate int
mdi_prop_update_int_array(mdi_pathinfo_t * pip,char * name,int * data,uint_t nelements)46027c478bd9Sstevel@tonic-gate mdi_prop_update_int_array(mdi_pathinfo_t *pip, char *name, int *data,
46037c478bd9Sstevel@tonic-gate 	    uint_t nelements)
46047c478bd9Sstevel@tonic-gate {
46057c478bd9Sstevel@tonic-gate 	int rv;
46067c478bd9Sstevel@tonic-gate 
46077c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
46087c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
46097c478bd9Sstevel@tonic-gate 	}
46105e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
46117c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
46127c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
46137c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
46147c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
46157c478bd9Sstevel@tonic-gate 	}
46167c478bd9Sstevel@tonic-gate 	rv = nvlist_add_int32_array(MDI_PI(pip)->pi_prop, name, (int32_t *)data,
46177c478bd9Sstevel@tonic-gate 	    nelements);
46187c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
46197c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
46207c478bd9Sstevel@tonic-gate }
46217c478bd9Sstevel@tonic-gate 
46227c478bd9Sstevel@tonic-gate /*
46237c478bd9Sstevel@tonic-gate  * mdi_prop_update_string():
46247c478bd9Sstevel@tonic-gate  *		Create/Update a string property
46257c478bd9Sstevel@tonic-gate  */
46267c478bd9Sstevel@tonic-gate int
mdi_prop_update_string(mdi_pathinfo_t * pip,char * name,char * data)46277c478bd9Sstevel@tonic-gate mdi_prop_update_string(mdi_pathinfo_t *pip, char *name, char *data)
46287c478bd9Sstevel@tonic-gate {
46297c478bd9Sstevel@tonic-gate 	int rv;
46307c478bd9Sstevel@tonic-gate 
46317c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
46327c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
46337c478bd9Sstevel@tonic-gate 	}
46345e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
46357c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
46367c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
46377c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
46387c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
46397c478bd9Sstevel@tonic-gate 	}
46407c478bd9Sstevel@tonic-gate 	rv = nvlist_add_string(MDI_PI(pip)->pi_prop, name, data);
46417c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
46427c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
46437c478bd9Sstevel@tonic-gate }
46447c478bd9Sstevel@tonic-gate 
46457c478bd9Sstevel@tonic-gate /*
46467c478bd9Sstevel@tonic-gate  * mdi_prop_update_string_array():
46477c478bd9Sstevel@tonic-gate  *		Create/Update a string array property
46487c478bd9Sstevel@tonic-gate  */
46497c478bd9Sstevel@tonic-gate int
mdi_prop_update_string_array(mdi_pathinfo_t * pip,char * name,char ** data,uint_t nelements)46507c478bd9Sstevel@tonic-gate mdi_prop_update_string_array(mdi_pathinfo_t *pip, char *name, char **data,
46517c478bd9Sstevel@tonic-gate     uint_t nelements)
46527c478bd9Sstevel@tonic-gate {
46537c478bd9Sstevel@tonic-gate 	int rv;
46547c478bd9Sstevel@tonic-gate 
46557c478bd9Sstevel@tonic-gate 	if (pip == NULL) {
46567c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
46577c478bd9Sstevel@tonic-gate 	}
46585e3986cbScth 	ASSERT(!MDI_PI_LOCKED(pip));
46597c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
46607c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_prop == NULL) {
46617c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
46627c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
46637c478bd9Sstevel@tonic-gate 	}
46647c478bd9Sstevel@tonic-gate 	rv = nvlist_add_string_array(MDI_PI(pip)->pi_prop, name, data,
46657c478bd9Sstevel@tonic-gate 	    nelements);
46667c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
46677c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
46687c478bd9Sstevel@tonic-gate }
46697c478bd9Sstevel@tonic-gate 
46707c478bd9Sstevel@tonic-gate /*
46717c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_byte():
4672d27aea3fSDan Cross  *		Look for byte property identified by name.  The data returned
46737c478bd9Sstevel@tonic-gate  *		is the actual property and valid as long as mdi_pathinfo_t node
46747c478bd9Sstevel@tonic-gate  *		is alive.
46757c478bd9Sstevel@tonic-gate  */
46767c478bd9Sstevel@tonic-gate int
mdi_prop_lookup_byte(mdi_pathinfo_t * pip,char * name,uchar_t * data)46777c478bd9Sstevel@tonic-gate mdi_prop_lookup_byte(mdi_pathinfo_t *pip, char *name, uchar_t *data)
46787c478bd9Sstevel@tonic-gate {
46797c478bd9Sstevel@tonic-gate 	int rv;
46807c478bd9Sstevel@tonic-gate 
46817c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
46827c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
46837c478bd9Sstevel@tonic-gate 	}
46847c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_byte(MDI_PI(pip)->pi_prop, name, data);
46857c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
46867c478bd9Sstevel@tonic-gate }
46877c478bd9Sstevel@tonic-gate 
46887c478bd9Sstevel@tonic-gate 
46897c478bd9Sstevel@tonic-gate /*
46907c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_byte_array():
4691d27aea3fSDan Cross  *		Look for byte array property identified by name.  The data
46927c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
46937c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
46947c478bd9Sstevel@tonic-gate  */
46957c478bd9Sstevel@tonic-gate int
mdi_prop_lookup_byte_array(mdi_pathinfo_t * pip,char * name,uchar_t ** data,uint_t * nelements)46967c478bd9Sstevel@tonic-gate mdi_prop_lookup_byte_array(mdi_pathinfo_t *pip, char *name, uchar_t **data,
46977c478bd9Sstevel@tonic-gate     uint_t *nelements)
46987c478bd9Sstevel@tonic-gate {
46997c478bd9Sstevel@tonic-gate 	int rv;
47007c478bd9Sstevel@tonic-gate 
47017c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
47027c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
47037c478bd9Sstevel@tonic-gate 	}
47047c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_byte_array(MDI_PI(pip)->pi_prop, name, data,
47057c478bd9Sstevel@tonic-gate 	    nelements);
47067c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
47077c478bd9Sstevel@tonic-gate }
47087c478bd9Sstevel@tonic-gate 
47097c478bd9Sstevel@tonic-gate /*
47107c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_int():
4711d27aea3fSDan Cross  *		Look for int property identified by name.  The data returned
47127c478bd9Sstevel@tonic-gate  *		is the actual property and valid as long as mdi_pathinfo_t
47137c478bd9Sstevel@tonic-gate  *		node is alive.
47147c478bd9Sstevel@tonic-gate  */
47157c478bd9Sstevel@tonic-gate int
mdi_prop_lookup_int(mdi_pathinfo_t * pip,char * name,int * data)47167c478bd9Sstevel@tonic-gate mdi_prop_lookup_int(mdi_pathinfo_t *pip, char *name, int *data)
47177c478bd9Sstevel@tonic-gate {
47187c478bd9Sstevel@tonic-gate 	int rv;
47197c478bd9Sstevel@tonic-gate 
47207c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
47217c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
47227c478bd9Sstevel@tonic-gate 	}
47237c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_int32(MDI_PI(pip)->pi_prop, name, (int32_t *)data);
47247c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
47257c478bd9Sstevel@tonic-gate }
47267c478bd9Sstevel@tonic-gate 
47277c478bd9Sstevel@tonic-gate /*
47287c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_int64():
4729d27aea3fSDan Cross  *		Look for int64 property identified by name.  The data returned
47307c478bd9Sstevel@tonic-gate  *		is the actual property and valid as long as mdi_pathinfo_t node
47317c478bd9Sstevel@tonic-gate  *		is alive.
47327c478bd9Sstevel@tonic-gate  */
47337c478bd9Sstevel@tonic-gate int
mdi_prop_lookup_int64(mdi_pathinfo_t * pip,char * name,int64_t * data)47347c478bd9Sstevel@tonic-gate mdi_prop_lookup_int64(mdi_pathinfo_t *pip, char *name, int64_t *data)
47357c478bd9Sstevel@tonic-gate {
47367c478bd9Sstevel@tonic-gate 	int rv;
47377c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
47387c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
47397c478bd9Sstevel@tonic-gate 	}
47407c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_int64(MDI_PI(pip)->pi_prop, name, data);
47417c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
47427c478bd9Sstevel@tonic-gate }
47437c478bd9Sstevel@tonic-gate 
47447c478bd9Sstevel@tonic-gate /*
47457c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_int_array():
4746d27aea3fSDan Cross  *		Look for int array property identified by name.  The data
47477c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
47487c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
47497c478bd9Sstevel@tonic-gate  */
47507c478bd9Sstevel@tonic-gate int
mdi_prop_lookup_int_array(mdi_pathinfo_t * pip,char * name,int ** data,uint_t * nelements)47517c478bd9Sstevel@tonic-gate mdi_prop_lookup_int_array(mdi_pathinfo_t *pip, char *name, int **data,
47527c478bd9Sstevel@tonic-gate     uint_t *nelements)
47537c478bd9Sstevel@tonic-gate {
47547c478bd9Sstevel@tonic-gate 	int rv;
47557c478bd9Sstevel@tonic-gate 
47567c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
47577c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
47587c478bd9Sstevel@tonic-gate 	}
47597c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_int32_array(MDI_PI(pip)->pi_prop, name,
47607c478bd9Sstevel@tonic-gate 	    (int32_t **)data, nelements);
47617c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
47627c478bd9Sstevel@tonic-gate }
47637c478bd9Sstevel@tonic-gate 
47647c478bd9Sstevel@tonic-gate /*
47657c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_string():
4766d27aea3fSDan Cross  *		Look for string property identified by name.  The data
47677c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
47687c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
47697c478bd9Sstevel@tonic-gate  */
47707c478bd9Sstevel@tonic-gate int
mdi_prop_lookup_string(mdi_pathinfo_t * pip,char * name,char ** data)47717c478bd9Sstevel@tonic-gate mdi_prop_lookup_string(mdi_pathinfo_t *pip, char *name, char **data)
47727c478bd9Sstevel@tonic-gate {
47737c478bd9Sstevel@tonic-gate 	int rv;
47747c478bd9Sstevel@tonic-gate 
47757c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
47767c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
47777c478bd9Sstevel@tonic-gate 	}
47787c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_string(MDI_PI(pip)->pi_prop, name, data);
47797c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
47807c478bd9Sstevel@tonic-gate }
47817c478bd9Sstevel@tonic-gate 
47827c478bd9Sstevel@tonic-gate /*
47837c478bd9Sstevel@tonic-gate  * mdi_prop_lookup_string_array():
4784d27aea3fSDan Cross  *		Look for string array property identified by name.  The data
47857c478bd9Sstevel@tonic-gate  *		returned is the actual property and valid as long as
47867c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is alive.
47877c478bd9Sstevel@tonic-gate  */
47887c478bd9Sstevel@tonic-gate int
mdi_prop_lookup_string_array(mdi_pathinfo_t * pip,char * name,char *** data,uint_t * nelements)47897c478bd9Sstevel@tonic-gate mdi_prop_lookup_string_array(mdi_pathinfo_t *pip, char *name, char ***data,
47907c478bd9Sstevel@tonic-gate     uint_t *nelements)
47917c478bd9Sstevel@tonic-gate {
47927c478bd9Sstevel@tonic-gate 	int rv;
47937c478bd9Sstevel@tonic-gate 
47947c478bd9Sstevel@tonic-gate 	if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
47957c478bd9Sstevel@tonic-gate 		return (DDI_PROP_NOT_FOUND);
47967c478bd9Sstevel@tonic-gate 	}
47977c478bd9Sstevel@tonic-gate 	rv = nvlist_lookup_string_array(MDI_PI(pip)->pi_prop, name, data,
47987c478bd9Sstevel@tonic-gate 	    nelements);
47997c478bd9Sstevel@tonic-gate 	return (i_map_nvlist_error_to_mdi(rv));
48007c478bd9Sstevel@tonic-gate }
48017c478bd9Sstevel@tonic-gate 
48027c478bd9Sstevel@tonic-gate /*
48037c478bd9Sstevel@tonic-gate  * mdi_prop_free():
4804d27aea3fSDan Cross  *		Symmetrical function to ddi_prop_free(). nvlist_lookup_xx()
48057c478bd9Sstevel@tonic-gate  *		functions return the pointer to actual property data and not a
48067c478bd9Sstevel@tonic-gate  *		copy of it.  So the data returned is valid as long as
48077c478bd9Sstevel@tonic-gate  *		mdi_pathinfo_t node is valid.
48087c478bd9Sstevel@tonic-gate  */
48097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
48107c478bd9Sstevel@tonic-gate int
mdi_prop_free(void * data)48117c478bd9Sstevel@tonic-gate mdi_prop_free(void *data)
48127c478bd9Sstevel@tonic-gate {
48137c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
48147c478bd9Sstevel@tonic-gate }
48157c478bd9Sstevel@tonic-gate 
48167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
48177c478bd9Sstevel@tonic-gate static void
i_mdi_report_path_state(mdi_client_t * ct,mdi_pathinfo_t * pip)48187c478bd9Sstevel@tonic-gate i_mdi_report_path_state(mdi_client_t *ct, mdi_pathinfo_t *pip)
48197c478bd9Sstevel@tonic-gate {
48204c06356bSdh 	char		*ct_path;
48217c478bd9Sstevel@tonic-gate 	char		*ct_status;
48227c478bd9Sstevel@tonic-gate 	char		*status;
48234c06356bSdh 	dev_info_t	*cdip = ct->ct_dip;
48247c478bd9Sstevel@tonic-gate 	char		lb_buf[64];
48254c06356bSdh 	int		report_lb_c = 0, report_lb_p = 0;
48267c478bd9Sstevel@tonic-gate 
48275e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
48284c06356bSdh 	if ((cdip == NULL) || (ddi_get_instance(cdip) == -1) ||
48297c478bd9Sstevel@tonic-gate 	    (MDI_CLIENT_IS_REPORT_DEV_NEEDED(ct) == 0)) {
48307c478bd9Sstevel@tonic-gate 		return;
48317c478bd9Sstevel@tonic-gate 	}
48327c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_OPTIMAL) {
48337c478bd9Sstevel@tonic-gate 		ct_status = "optimal";
48344c06356bSdh 		report_lb_c = 1;
48357c478bd9Sstevel@tonic-gate 	} else if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_DEGRADED) {
48367c478bd9Sstevel@tonic-gate 		ct_status = "degraded";
48377c478bd9Sstevel@tonic-gate 	} else if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_FAILED) {
48387c478bd9Sstevel@tonic-gate 		ct_status = "failed";
48397c478bd9Sstevel@tonic-gate 	} else {
48407c478bd9Sstevel@tonic-gate 		ct_status = "unknown";
48417c478bd9Sstevel@tonic-gate 	}
48427c478bd9Sstevel@tonic-gate 
48434c06356bSdh 	lb_buf[0] = 0;		/* not interested in load balancing config */
48444c06356bSdh 
48454c06356bSdh 	if (MDI_PI_FLAGS_IS_DEVICE_REMOVED(pip)) {
48464c06356bSdh 		status = "removed";
48474c06356bSdh 	} else if (MDI_PI_IS_OFFLINE(pip)) {
48487c478bd9Sstevel@tonic-gate 		status = "offline";
48497c478bd9Sstevel@tonic-gate 	} else if (MDI_PI_IS_ONLINE(pip)) {
48507c478bd9Sstevel@tonic-gate 		status = "online";
48514c06356bSdh 		report_lb_p = 1;
48527c478bd9Sstevel@tonic-gate 	} else if (MDI_PI_IS_STANDBY(pip)) {
48537c478bd9Sstevel@tonic-gate 		status = "standby";
48547c478bd9Sstevel@tonic-gate 	} else if (MDI_PI_IS_FAULT(pip)) {
48557c478bd9Sstevel@tonic-gate 		status = "faulted";
48567c478bd9Sstevel@tonic-gate 	} else {
48577c478bd9Sstevel@tonic-gate 		status = "unknown";
48587c478bd9Sstevel@tonic-gate 	}
48597c478bd9Sstevel@tonic-gate 
48604c06356bSdh 	if (cdip) {
48617c478bd9Sstevel@tonic-gate 		ct_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
48624c06356bSdh 
48634c06356bSdh 		/*
48644c06356bSdh 		 * NOTE: Keeping "multipath status: %s" and
48654c06356bSdh 		 * "Load balancing: %s" format unchanged in case someone
48664c06356bSdh 		 * scrubs /var/adm/messages looking for these messages.
48674c06356bSdh 		 */
48684c06356bSdh 		if (report_lb_c && report_lb_p) {
48694c06356bSdh 			if (ct->ct_lb == LOAD_BALANCE_LBA) {
48704c06356bSdh 				(void) snprintf(lb_buf, sizeof (lb_buf),
48714c06356bSdh 				    "%s, region-size: %d", mdi_load_balance_lba,
48724c06356bSdh 				    ct->ct_lb_args->region_size);
48734c06356bSdh 			} else if (ct->ct_lb == LOAD_BALANCE_NONE) {
48744c06356bSdh 				(void) snprintf(lb_buf, sizeof (lb_buf),
48754c06356bSdh 				    "%s", mdi_load_balance_none);
48764c06356bSdh 			} else {
48774c06356bSdh 				(void) snprintf(lb_buf, sizeof (lb_buf), "%s",
48784c06356bSdh 				    mdi_load_balance_rr);
48794c06356bSdh 			}
48804c06356bSdh 
48814c06356bSdh 			cmn_err(mdi_debug_consoleonly ? CE_NOTE : CE_CONT,
48824c06356bSdh 			    "?%s (%s%d) multipath status: %s: "
48834c06356bSdh 			    "path %d %s is %s: Load balancing: %s\n",
48844c06356bSdh 			    ddi_pathname(cdip, ct_path), ddi_driver_name(cdip),
48854c06356bSdh 			    ddi_get_instance(cdip), ct_status,
48864c06356bSdh 			    mdi_pi_get_path_instance(pip),
48874c06356bSdh 			    mdi_pi_spathname(pip), status, lb_buf);
48884c06356bSdh 		} else {
48894c06356bSdh 			cmn_err(mdi_debug_consoleonly ? CE_NOTE : CE_CONT,
48904c06356bSdh 			    "?%s (%s%d) multipath status: %s: "
48914c06356bSdh 			    "path %d %s is %s\n",
48924c06356bSdh 			    ddi_pathname(cdip, ct_path), ddi_driver_name(cdip),
48934c06356bSdh 			    ddi_get_instance(cdip), ct_status,
48944c06356bSdh 			    mdi_pi_get_path_instance(pip),
48954c06356bSdh 			    mdi_pi_spathname(pip), status);
48964c06356bSdh 		}
48974c06356bSdh 
48987c478bd9Sstevel@tonic-gate 		kmem_free(ct_path, MAXPATHLEN);
48997c478bd9Sstevel@tonic-gate 		MDI_CLIENT_CLEAR_REPORT_DEV_NEEDED(ct);
49007c478bd9Sstevel@tonic-gate 	}
49017c478bd9Sstevel@tonic-gate }
49027c478bd9Sstevel@tonic-gate 
49037c478bd9Sstevel@tonic-gate #ifdef	DEBUG
49047c478bd9Sstevel@tonic-gate /*
49057c478bd9Sstevel@tonic-gate  * i_mdi_log():
49067c478bd9Sstevel@tonic-gate  *		Utility function for error message management
49077c478bd9Sstevel@tonic-gate  *
49084c06356bSdh  *		NOTE: Implementation takes care of trailing \n for cmn_err,
49094c06356bSdh  *		MDI_DEBUG should not terminate fmt strings with \n.
49104c06356bSdh  *
49114c06356bSdh  *		NOTE: If the level is >= 2, and there is no leading !?^
49124c06356bSdh  *		then a leading ! is implied (but can be overriden via
49134c06356bSdh  *		mdi_debug_consoleonly). If you are using kmdb on the console,
49144c06356bSdh  *		consider setting mdi_debug_consoleonly to 1 as an aid.
49157c478bd9Sstevel@tonic-gate  */
49164c06356bSdh /*PRINTFLIKE4*/
49177c478bd9Sstevel@tonic-gate static void
i_mdi_log(int level,const char * func,dev_info_t * dip,const char * fmt,...)49184c06356bSdh i_mdi_log(int level, const char *func, dev_info_t *dip, const char *fmt, ...)
49197c478bd9Sstevel@tonic-gate {
4920c73a93f2Sdm 	char		name[MAXNAMELEN];
49214c06356bSdh 	char		buf[512];
49225e3986cbScth 	char		*bp;
49237c478bd9Sstevel@tonic-gate 	va_list		ap;
49247c478bd9Sstevel@tonic-gate 	int		log_only = 0;
49257c478bd9Sstevel@tonic-gate 	int		boot_only = 0;
49267c478bd9Sstevel@tonic-gate 	int		console_only = 0;
49277c478bd9Sstevel@tonic-gate 
49287c478bd9Sstevel@tonic-gate 	if (dip) {
49294c06356bSdh 		(void) snprintf(name, sizeof(name), "%s%d: ",
493055e592a2SRandall Ralphs 		    ddi_driver_name(dip), ddi_get_instance(dip));
49317c478bd9Sstevel@tonic-gate 	} else {
49325e3986cbScth 		name[0] = 0;
49337c478bd9Sstevel@tonic-gate 	}
49347c478bd9Sstevel@tonic-gate 
49357c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
49364c06356bSdh 	(void) vsnprintf(buf, sizeof(buf), fmt, ap);
49377c478bd9Sstevel@tonic-gate 	va_end(ap);
49387c478bd9Sstevel@tonic-gate 
49397c478bd9Sstevel@tonic-gate 	switch (buf[0]) {
49407c478bd9Sstevel@tonic-gate 	case '!':
49415e3986cbScth 		bp = &buf[1];
49427c478bd9Sstevel@tonic-gate 		log_only = 1;
49437c478bd9Sstevel@tonic-gate 		break;
49447c478bd9Sstevel@tonic-gate 	case '?':
49455e3986cbScth 		bp = &buf[1];
49467c478bd9Sstevel@tonic-gate 		boot_only = 1;
49477c478bd9Sstevel@tonic-gate 		break;
49487c478bd9Sstevel@tonic-gate 	case '^':
49495e3986cbScth 		bp = &buf[1];
49507c478bd9Sstevel@tonic-gate 		console_only = 1;
49517c478bd9Sstevel@tonic-gate 		break;
49525e3986cbScth 	default:
49534c06356bSdh 		if (level >= 2)
49544c06356bSdh 			log_only = 1;		/* ! implied */
49555e3986cbScth 		bp = buf;
49565e3986cbScth 		break;
49575e3986cbScth 	}
49585e3986cbScth 	if (mdi_debug_logonly) {
49595e3986cbScth 		log_only = 1;
49605e3986cbScth 		boot_only = 0;
49615e3986cbScth 		console_only = 0;
49627c478bd9Sstevel@tonic-gate 	}
49634c06356bSdh 	if (mdi_debug_consoleonly) {
49644c06356bSdh 		log_only = 0;
49654c06356bSdh 		boot_only = 0;
49664c06356bSdh 		console_only = 1;
49674c06356bSdh 		level = CE_NOTE;
49684c06356bSdh 		goto console;
49694c06356bSdh 	}
49707c478bd9Sstevel@tonic-gate 
49717c478bd9Sstevel@tonic-gate 	switch (level) {
49727c478bd9Sstevel@tonic-gate 	case CE_NOTE:
49737c478bd9Sstevel@tonic-gate 		level = CE_CONT;
49747c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
49757c478bd9Sstevel@tonic-gate 	case CE_CONT:
49764c06356bSdh 		if (boot_only) {
49774c06356bSdh 			cmn_err(level, "?mdi: %s%s: %s\n", name, func, bp);
49784c06356bSdh 		} else if (console_only) {
49794c06356bSdh 			cmn_err(level, "^mdi: %s%s: %s\n", name, func, bp);
49804c06356bSdh 		} else if (log_only) {
49814c06356bSdh 			cmn_err(level, "!mdi: %s%s: %s\n", name, func, bp);
49824c06356bSdh 		} else {
49834c06356bSdh 			cmn_err(level, "mdi: %s%s: %s\n", name, func, bp);
49844c06356bSdh 		}
49854c06356bSdh 		break;
49864c06356bSdh 
49877c478bd9Sstevel@tonic-gate 	case CE_WARN:
49887c478bd9Sstevel@tonic-gate 	case CE_PANIC:
49894c06356bSdh 	console:
49907c478bd9Sstevel@tonic-gate 		if (boot_only) {
49914c06356bSdh 			cmn_err(level, "?mdi: %s%s: %s", name, func, bp);
49927c478bd9Sstevel@tonic-gate 		} else if (console_only) {
49934c06356bSdh 			cmn_err(level, "^mdi: %s%s: %s", name, func, bp);
49947c478bd9Sstevel@tonic-gate 		} else if (log_only) {
49954c06356bSdh 			cmn_err(level, "!mdi: %s%s: %s", name, func, bp);
49967c478bd9Sstevel@tonic-gate 		} else {
49974c06356bSdh 			cmn_err(level, "mdi: %s%s: %s", name, func, bp);
49987c478bd9Sstevel@tonic-gate 		}
49997c478bd9Sstevel@tonic-gate 		break;
50007c478bd9Sstevel@tonic-gate 	default:
50015e3986cbScth 		cmn_err(level, "mdi: %s%s", name, bp);
50027c478bd9Sstevel@tonic-gate 		break;
50037c478bd9Sstevel@tonic-gate 	}
50047c478bd9Sstevel@tonic-gate }
50057c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
50067c478bd9Sstevel@tonic-gate 
50077c478bd9Sstevel@tonic-gate void
i_mdi_client_online(dev_info_t * ct_dip)50087c478bd9Sstevel@tonic-gate i_mdi_client_online(dev_info_t *ct_dip)
50097c478bd9Sstevel@tonic-gate {
50107c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
50117c478bd9Sstevel@tonic-gate 
50127c478bd9Sstevel@tonic-gate 	/*
50137c478bd9Sstevel@tonic-gate 	 * Client online notification. Mark client state as online
50147c478bd9Sstevel@tonic-gate 	 * restore our binding with dev_info node
50157c478bd9Sstevel@tonic-gate 	 */
50167c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(ct_dip);
50177c478bd9Sstevel@tonic-gate 	ASSERT(ct != NULL);
50187c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
50197c478bd9Sstevel@tonic-gate 	MDI_CLIENT_SET_ONLINE(ct);
50207c478bd9Sstevel@tonic-gate 	/* catch for any memory leaks */
50217c478bd9Sstevel@tonic-gate 	ASSERT((ct->ct_dip == NULL) || (ct->ct_dip == ct_dip));
50227c478bd9Sstevel@tonic-gate 	ct->ct_dip = ct_dip;
50237c478bd9Sstevel@tonic-gate 
50247c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0)
50257c478bd9Sstevel@tonic-gate 		(void) i_mdi_power_all_phci(ct);
50267c478bd9Sstevel@tonic-gate 
50274c06356bSdh 	MDI_DEBUG(4, (MDI_NOTE, ct_dip,
50284c06356bSdh 	    "i_mdi_pm_hold_client %p", (void *)ct));
50297c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_client(ct, 1);
50307c478bd9Sstevel@tonic-gate 
50317c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
50327c478bd9Sstevel@tonic-gate }
50337c478bd9Sstevel@tonic-gate 
50347c478bd9Sstevel@tonic-gate void
i_mdi_phci_online(dev_info_t * ph_dip)50357c478bd9Sstevel@tonic-gate i_mdi_phci_online(dev_info_t *ph_dip)
50367c478bd9Sstevel@tonic-gate {
50377c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
50387c478bd9Sstevel@tonic-gate 
50397c478bd9Sstevel@tonic-gate 	/* pHCI online notification. Mark state accordingly */
50407c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(ph_dip);
50417c478bd9Sstevel@tonic-gate 	ASSERT(ph != NULL);
50427c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
50437c478bd9Sstevel@tonic-gate 	MDI_PHCI_SET_ONLINE(ph);
50447c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
50457c478bd9Sstevel@tonic-gate }
50467c478bd9Sstevel@tonic-gate 
50477c478bd9Sstevel@tonic-gate /*
50487c478bd9Sstevel@tonic-gate  * mdi_devi_online():
5049d27aea3fSDan Cross  *		Online notification from NDI framework on pHCI/client
50507c478bd9Sstevel@tonic-gate  *		device online.
50517c478bd9Sstevel@tonic-gate  * Return Values:
50527c478bd9Sstevel@tonic-gate  *		NDI_SUCCESS
50537c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
50547c478bd9Sstevel@tonic-gate  */
50557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
50567c478bd9Sstevel@tonic-gate int
mdi_devi_online(dev_info_t * dip,uint_t flags)50577c478bd9Sstevel@tonic-gate mdi_devi_online(dev_info_t *dip, uint_t flags)
50587c478bd9Sstevel@tonic-gate {
50597c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
50607c478bd9Sstevel@tonic-gate 		i_mdi_phci_online(dip);
50617c478bd9Sstevel@tonic-gate 	}
50627c478bd9Sstevel@tonic-gate 
50637c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
50647c478bd9Sstevel@tonic-gate 		i_mdi_client_online(dip);
50657c478bd9Sstevel@tonic-gate 	}
50667c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
50677c478bd9Sstevel@tonic-gate }
50687c478bd9Sstevel@tonic-gate 
50697c478bd9Sstevel@tonic-gate /*
50707c478bd9Sstevel@tonic-gate  * mdi_devi_offline():
5071d27aea3fSDan Cross  *		Offline notification from NDI framework on pHCI/Client device
50727c478bd9Sstevel@tonic-gate  *		offline.
50737c478bd9Sstevel@tonic-gate  *
50747c478bd9Sstevel@tonic-gate  * Return Values:
50757c478bd9Sstevel@tonic-gate  *		NDI_SUCCESS
50767c478bd9Sstevel@tonic-gate  *		NDI_FAILURE
50777c478bd9Sstevel@tonic-gate  */
50787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
50797c478bd9Sstevel@tonic-gate int
mdi_devi_offline(dev_info_t * dip,uint_t flags)50807c478bd9Sstevel@tonic-gate mdi_devi_offline(dev_info_t *dip, uint_t flags)
50817c478bd9Sstevel@tonic-gate {
50827c478bd9Sstevel@tonic-gate 	int		rv = NDI_SUCCESS;
50837c478bd9Sstevel@tonic-gate 
50847c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
50857c478bd9Sstevel@tonic-gate 		rv = i_mdi_client_offline(dip, flags);
50867c478bd9Sstevel@tonic-gate 		if (rv != NDI_SUCCESS)
50877c478bd9Sstevel@tonic-gate 			return (rv);
50887c478bd9Sstevel@tonic-gate 	}
50897c478bd9Sstevel@tonic-gate 
50907c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
50917c478bd9Sstevel@tonic-gate 		rv = i_mdi_phci_offline(dip, flags);
50925e3986cbScth 
50937c478bd9Sstevel@tonic-gate 		if ((rv != NDI_SUCCESS) && MDI_CLIENT(dip)) {
50947c478bd9Sstevel@tonic-gate 			/* set client back online */
50957c478bd9Sstevel@tonic-gate 			i_mdi_client_online(dip);
50967c478bd9Sstevel@tonic-gate 		}
50977c478bd9Sstevel@tonic-gate 	}
50987c478bd9Sstevel@tonic-gate 
50997c478bd9Sstevel@tonic-gate 	return (rv);
51007c478bd9Sstevel@tonic-gate }
51017c478bd9Sstevel@tonic-gate 
51027c478bd9Sstevel@tonic-gate /*ARGSUSED*/
51037c478bd9Sstevel@tonic-gate static int
i_mdi_phci_offline(dev_info_t * dip,uint_t flags)51047c478bd9Sstevel@tonic-gate i_mdi_phci_offline(dev_info_t *dip, uint_t flags)
51057c478bd9Sstevel@tonic-gate {
51067c478bd9Sstevel@tonic-gate 	int		rv = NDI_SUCCESS;
51077c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
51087c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
51097c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
51107c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
51117c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*failed_pip = NULL;
51127c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
51137c478bd9Sstevel@tonic-gate 
51147c478bd9Sstevel@tonic-gate 	/*
51157c478bd9Sstevel@tonic-gate 	 * pHCI component offline notification
51167c478bd9Sstevel@tonic-gate 	 * Make sure that this pHCI instance is free to be offlined.
51177c478bd9Sstevel@tonic-gate 	 * If it is OK to proceed, Offline and remove all the child
51187c478bd9Sstevel@tonic-gate 	 * mdi_pathinfo nodes.  This process automatically offlines
51197c478bd9Sstevel@tonic-gate 	 * corresponding client devices, for which this pHCI provides
51207c478bd9Sstevel@tonic-gate 	 * critical services.
51217c478bd9Sstevel@tonic-gate 	 */
51227c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(dip);
51234c06356bSdh 	MDI_DEBUG(2, (MDI_NOTE, dip,
51244c06356bSdh 	    "called %p %p", (void *)dip, (void *)ph));
51257c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
51267c478bd9Sstevel@tonic-gate 		return (rv);
51277c478bd9Sstevel@tonic-gate 	}
51287c478bd9Sstevel@tonic-gate 
51297c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
51307c478bd9Sstevel@tonic-gate 
51317c478bd9Sstevel@tonic-gate 	if (MDI_PHCI_IS_OFFLINE(ph)) {
51324c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, dip,
51334c06356bSdh 		    "!pHCI already offlined: %p", (void *)dip));
51347c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
51357c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
51367c478bd9Sstevel@tonic-gate 	}
51377c478bd9Sstevel@tonic-gate 
51387c478bd9Sstevel@tonic-gate 	/*
51397c478bd9Sstevel@tonic-gate 	 * Check to see if the pHCI can be offlined
51407c478bd9Sstevel@tonic-gate 	 */
51417c478bd9Sstevel@tonic-gate 	if (ph->ph_unstable) {
51424c06356bSdh 		MDI_DEBUG(1, (MDI_WARN, dip,
51434c06356bSdh 		    "!One or more target devices are in transient state. "
51444c06356bSdh 		    "This device can not be removed at this moment. "
51454c06356bSdh 		    "Please try again later."));
51467c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
51477c478bd9Sstevel@tonic-gate 		return (NDI_BUSY);
51487c478bd9Sstevel@tonic-gate 	}
51497c478bd9Sstevel@tonic-gate 
51507c478bd9Sstevel@tonic-gate 	pip = ph->ph_path_head;
51517c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
51527c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
51537c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
51545e3986cbScth 
51557c478bd9Sstevel@tonic-gate 		/*
51567c478bd9Sstevel@tonic-gate 		 * The mdi_pathinfo state is OK. Check the client state.
51577c478bd9Sstevel@tonic-gate 		 * If failover in progress fail the pHCI from offlining
51587c478bd9Sstevel@tonic-gate 		 */
51597c478bd9Sstevel@tonic-gate 		ct = MDI_PI(pip)->pi_client;
51607c478bd9Sstevel@tonic-gate 		i_mdi_client_lock(ct, pip);
51617c478bd9Sstevel@tonic-gate 		if ((MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) ||
51627c478bd9Sstevel@tonic-gate 		    (ct->ct_unstable)) {
51637c478bd9Sstevel@tonic-gate 			/*
51647c478bd9Sstevel@tonic-gate 			 * Failover is in progress, Fail the DR
51657c478bd9Sstevel@tonic-gate 			 */
51664c06356bSdh 			MDI_DEBUG(1, (MDI_WARN, dip,
51674c06356bSdh 			    "!pHCI device is busy. "
51684c06356bSdh 			    "This device can not be removed at this moment. "
51694c06356bSdh 			    "Please try again later."));
51707c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
51715e3986cbScth 			i_mdi_client_unlock(ct);
51727c478bd9Sstevel@tonic-gate 			MDI_PHCI_UNLOCK(ph);
51737c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
51747c478bd9Sstevel@tonic-gate 		}
51757c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
51767c478bd9Sstevel@tonic-gate 
51777c478bd9Sstevel@tonic-gate 		/*
51787c478bd9Sstevel@tonic-gate 		 * Check to see of we are removing the last path of this
51797c478bd9Sstevel@tonic-gate 		 * client device...
51807c478bd9Sstevel@tonic-gate 		 */
51817c478bd9Sstevel@tonic-gate 		cdip = ct->ct_dip;
51827c478bd9Sstevel@tonic-gate 		if (cdip && (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
51837c478bd9Sstevel@tonic-gate 		    (i_mdi_client_compute_state(ct, ph) ==
51847c478bd9Sstevel@tonic-gate 		    MDI_CLIENT_STATE_FAILED)) {
51857c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
51867c478bd9Sstevel@tonic-gate 			MDI_PHCI_UNLOCK(ph);
51874c06356bSdh 			if (ndi_devi_offline(cdip,
51884c06356bSdh 			    NDI_DEVFS_CLEAN) != NDI_SUCCESS) {
51897c478bd9Sstevel@tonic-gate 				/*
51907c478bd9Sstevel@tonic-gate 				 * ndi_devi_offline() failed.
51917c478bd9Sstevel@tonic-gate 				 * This pHCI provides the critical path
51927c478bd9Sstevel@tonic-gate 				 * to one or more client devices.
51937c478bd9Sstevel@tonic-gate 				 * Return busy.
51947c478bd9Sstevel@tonic-gate 				 */
51957c478bd9Sstevel@tonic-gate 				MDI_PHCI_LOCK(ph);
51964c06356bSdh 				MDI_DEBUG(1, (MDI_WARN, dip,
51974c06356bSdh 				    "!pHCI device is busy. "
51984c06356bSdh 				    "This device can not be removed at this "
51994c06356bSdh 				    "moment. Please try again later."));
52007c478bd9Sstevel@tonic-gate 				failed_pip = pip;
52017c478bd9Sstevel@tonic-gate 				break;
52027c478bd9Sstevel@tonic-gate 			} else {
52037c478bd9Sstevel@tonic-gate 				MDI_PHCI_LOCK(ph);
52047c478bd9Sstevel@tonic-gate 				pip = next;
52057c478bd9Sstevel@tonic-gate 			}
52067c478bd9Sstevel@tonic-gate 		} else {
52077c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
52087c478bd9Sstevel@tonic-gate 			pip = next;
52097c478bd9Sstevel@tonic-gate 		}
52107c478bd9Sstevel@tonic-gate 	}
52117c478bd9Sstevel@tonic-gate 
52127c478bd9Sstevel@tonic-gate 	if (failed_pip) {
52137c478bd9Sstevel@tonic-gate 		pip = ph->ph_path_head;
52147c478bd9Sstevel@tonic-gate 		while (pip != failed_pip) {
52157c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
52167c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
52177c478bd9Sstevel@tonic-gate 			ct = MDI_PI(pip)->pi_client;
52187c478bd9Sstevel@tonic-gate 			i_mdi_client_lock(ct, pip);
52197c478bd9Sstevel@tonic-gate 			cdip = ct->ct_dip;
52207c478bd9Sstevel@tonic-gate 			switch (MDI_CLIENT_STATE(ct)) {
52217c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_OPTIMAL:
52227c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_DEGRADED:
52237c478bd9Sstevel@tonic-gate 				if (cdip) {
52247c478bd9Sstevel@tonic-gate 					MDI_PI_UNLOCK(pip);
52257c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
52267c478bd9Sstevel@tonic-gate 					MDI_PHCI_UNLOCK(ph);
52277c478bd9Sstevel@tonic-gate 					(void) ndi_devi_online(cdip, 0);
52287c478bd9Sstevel@tonic-gate 					MDI_PHCI_LOCK(ph);
52297c478bd9Sstevel@tonic-gate 					pip = next;
52307c478bd9Sstevel@tonic-gate 					continue;
52317c478bd9Sstevel@tonic-gate 				}
52327c478bd9Sstevel@tonic-gate 				break;
52337c478bd9Sstevel@tonic-gate 
52347c478bd9Sstevel@tonic-gate 			case MDI_CLIENT_STATE_FAILED:
52357c478bd9Sstevel@tonic-gate 				if (cdip) {
52367c478bd9Sstevel@tonic-gate 					MDI_PI_UNLOCK(pip);
52377c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
52387c478bd9Sstevel@tonic-gate 					MDI_PHCI_UNLOCK(ph);
52394c06356bSdh 					(void) ndi_devi_offline(cdip,
52404c06356bSdh 						NDI_DEVFS_CLEAN);
52417c478bd9Sstevel@tonic-gate 					MDI_PHCI_LOCK(ph);
52427c478bd9Sstevel@tonic-gate 					pip = next;
52437c478bd9Sstevel@tonic-gate 					continue;
52447c478bd9Sstevel@tonic-gate 				}
52457c478bd9Sstevel@tonic-gate 				break;
52467c478bd9Sstevel@tonic-gate 			}
52477c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
52487c478bd9Sstevel@tonic-gate 			i_mdi_client_unlock(ct);
52497c478bd9Sstevel@tonic-gate 			pip = next;
52507c478bd9Sstevel@tonic-gate 		}
52517c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
52527c478bd9Sstevel@tonic-gate 		return (NDI_BUSY);
52537c478bd9Sstevel@tonic-gate 	}
52547c478bd9Sstevel@tonic-gate 
52557c478bd9Sstevel@tonic-gate 	/*
52567c478bd9Sstevel@tonic-gate 	 * Mark the pHCI as offline
52577c478bd9Sstevel@tonic-gate 	 */
52587c478bd9Sstevel@tonic-gate 	MDI_PHCI_SET_OFFLINE(ph);
52597c478bd9Sstevel@tonic-gate 
52607c478bd9Sstevel@tonic-gate 	/*
52617c478bd9Sstevel@tonic-gate 	 * Mark the child mdi_pathinfo nodes as transient
52627c478bd9Sstevel@tonic-gate 	 */
52637c478bd9Sstevel@tonic-gate 	pip = ph->ph_path_head;
52647c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
52657c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
52667c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
52677c478bd9Sstevel@tonic-gate 		MDI_PI_SET_OFFLINING(pip);
52687c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
52697c478bd9Sstevel@tonic-gate 		pip = next;
52707c478bd9Sstevel@tonic-gate 	}
52717c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
52727c478bd9Sstevel@tonic-gate 	/*
52737c478bd9Sstevel@tonic-gate 	 * Give a chance for any pending commands to execute
52747c478bd9Sstevel@tonic-gate 	 */
527596c4a178SChris Horne 	delay_random(mdi_delay);
52767c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
52777c478bd9Sstevel@tonic-gate 	pip = ph->ph_path_head;
52787c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
52797c478bd9Sstevel@tonic-gate 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
52807c478bd9Sstevel@tonic-gate 		(void) i_mdi_pi_offline(pip, flags);
52817c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
52827c478bd9Sstevel@tonic-gate 		ct = MDI_PI(pip)->pi_client;
52837c478bd9Sstevel@tonic-gate 		if (!MDI_PI_IS_OFFLINE(pip)) {
52844c06356bSdh 			MDI_DEBUG(1, (MDI_WARN, dip,
52854c06356bSdh 			    "!pHCI device is busy. "
52864c06356bSdh 			    "This device can not be removed at this moment. "
52874c06356bSdh 			    "Please try again later."));
52887c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
52897c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_ONLINE(ph);
52907c478bd9Sstevel@tonic-gate 			MDI_PHCI_UNLOCK(ph);
52917c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
52927c478bd9Sstevel@tonic-gate 		}
52937c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
52947c478bd9Sstevel@tonic-gate 		pip = next;
52957c478bd9Sstevel@tonic-gate 	}
52967c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
52977c478bd9Sstevel@tonic-gate 
52987c478bd9Sstevel@tonic-gate 	return (rv);
52997c478bd9Sstevel@tonic-gate }
53007c478bd9Sstevel@tonic-gate 
530125e8c5aaSvikram void
mdi_phci_mark_retiring(dev_info_t * dip,char ** cons_array)530225e8c5aaSvikram mdi_phci_mark_retiring(dev_info_t *dip, char **cons_array)
530325e8c5aaSvikram {
530425e8c5aaSvikram 	mdi_phci_t	*ph;
530525e8c5aaSvikram 	mdi_client_t	*ct;
530625e8c5aaSvikram 	mdi_pathinfo_t	*pip;
530725e8c5aaSvikram 	mdi_pathinfo_t	*next;
530825e8c5aaSvikram 	dev_info_t	*cdip;
530925e8c5aaSvikram 
531025e8c5aaSvikram 	if (!MDI_PHCI(dip))
531125e8c5aaSvikram 		return;
531225e8c5aaSvikram 
531325e8c5aaSvikram 	ph = i_devi_get_phci(dip);
531425e8c5aaSvikram 	if (ph == NULL) {
531525e8c5aaSvikram 		return;
531625e8c5aaSvikram 	}
531725e8c5aaSvikram 
531825e8c5aaSvikram 	MDI_PHCI_LOCK(ph);
531925e8c5aaSvikram 
532025e8c5aaSvikram 	if (MDI_PHCI_IS_OFFLINE(ph)) {
532125e8c5aaSvikram 		/* has no last path */
532225e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
532325e8c5aaSvikram 		return;
532425e8c5aaSvikram 	}
532525e8c5aaSvikram 
532625e8c5aaSvikram 	pip = ph->ph_path_head;
532725e8c5aaSvikram 	while (pip != NULL) {
532825e8c5aaSvikram 		MDI_PI_LOCK(pip);
532925e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
533025e8c5aaSvikram 
533125e8c5aaSvikram 		ct = MDI_PI(pip)->pi_client;
533225e8c5aaSvikram 		i_mdi_client_lock(ct, pip);
533325e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
533425e8c5aaSvikram 
533525e8c5aaSvikram 		cdip = ct->ct_dip;
533625e8c5aaSvikram 		if (cdip && (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
533725e8c5aaSvikram 		    (i_mdi_client_compute_state(ct, ph) ==
533825e8c5aaSvikram 		    MDI_CLIENT_STATE_FAILED)) {
533925e8c5aaSvikram 			/* Last path. Mark client dip as retiring */
534025e8c5aaSvikram 			i_mdi_client_unlock(ct);
534125e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
534225e8c5aaSvikram 			(void) e_ddi_mark_retiring(cdip, cons_array);
534325e8c5aaSvikram 			MDI_PHCI_LOCK(ph);
534425e8c5aaSvikram 			pip = next;
534525e8c5aaSvikram 		} else {
534625e8c5aaSvikram 			i_mdi_client_unlock(ct);
534725e8c5aaSvikram 			pip = next;
534825e8c5aaSvikram 		}
534925e8c5aaSvikram 	}
535025e8c5aaSvikram 
535125e8c5aaSvikram 	MDI_PHCI_UNLOCK(ph);
535225e8c5aaSvikram 
535325e8c5aaSvikram 	return;
535425e8c5aaSvikram }
535525e8c5aaSvikram 
535625e8c5aaSvikram void
mdi_phci_retire_notify(dev_info_t * dip,int * constraint)535725e8c5aaSvikram mdi_phci_retire_notify(dev_info_t *dip, int *constraint)
535825e8c5aaSvikram {
535925e8c5aaSvikram 	mdi_phci_t	*ph;
536025e8c5aaSvikram 	mdi_client_t	*ct;
536125e8c5aaSvikram 	mdi_pathinfo_t	*pip;
536225e8c5aaSvikram 	mdi_pathinfo_t	*next;
536325e8c5aaSvikram 	dev_info_t	*cdip;
536425e8c5aaSvikram 
536525e8c5aaSvikram 	if (!MDI_PHCI(dip))
536625e8c5aaSvikram 		return;
536725e8c5aaSvikram 
536825e8c5aaSvikram 	ph = i_devi_get_phci(dip);
536925e8c5aaSvikram 	if (ph == NULL)
537025e8c5aaSvikram 		return;
537125e8c5aaSvikram 
537225e8c5aaSvikram 	MDI_PHCI_LOCK(ph);
537325e8c5aaSvikram 
537425e8c5aaSvikram 	if (MDI_PHCI_IS_OFFLINE(ph)) {
537525e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
537625e8c5aaSvikram 		/* not last path */
537725e8c5aaSvikram 		return;
537825e8c5aaSvikram 	}
537925e8c5aaSvikram 
538025e8c5aaSvikram 	if (ph->ph_unstable) {
538125e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
538225e8c5aaSvikram 		/* can't check for constraints */
538325e8c5aaSvikram 		*constraint = 0;
538425e8c5aaSvikram 		return;
538525e8c5aaSvikram 	}
538625e8c5aaSvikram 
538725e8c5aaSvikram 	pip = ph->ph_path_head;
538825e8c5aaSvikram 	while (pip != NULL) {
538925e8c5aaSvikram 		MDI_PI_LOCK(pip);
539025e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
539125e8c5aaSvikram 
539225e8c5aaSvikram 		/*
539325e8c5aaSvikram 		 * The mdi_pathinfo state is OK. Check the client state.
539425e8c5aaSvikram 		 * If failover in progress fail the pHCI from offlining
539525e8c5aaSvikram 		 */
539625e8c5aaSvikram 		ct = MDI_PI(pip)->pi_client;
539725e8c5aaSvikram 		i_mdi_client_lock(ct, pip);
539825e8c5aaSvikram 		if ((MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) ||
539925e8c5aaSvikram 		    (ct->ct_unstable)) {
540025e8c5aaSvikram 			/*
54014c06356bSdh 			 * Failover is in progress, can't check for constraints
540225e8c5aaSvikram 			 */
540325e8c5aaSvikram 			MDI_PI_UNLOCK(pip);
540425e8c5aaSvikram 			i_mdi_client_unlock(ct);
540525e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
540625e8c5aaSvikram 			*constraint = 0;
540725e8c5aaSvikram 			return;
540825e8c5aaSvikram 		}
540925e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
541025e8c5aaSvikram 
541125e8c5aaSvikram 		/*
541225e8c5aaSvikram 		 * Check to see of we are retiring the last path of this
541325e8c5aaSvikram 		 * client device...
541425e8c5aaSvikram 		 */
541525e8c5aaSvikram 		cdip = ct->ct_dip;
541625e8c5aaSvikram 		if (cdip && (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
541725e8c5aaSvikram 		    (i_mdi_client_compute_state(ct, ph) ==
541825e8c5aaSvikram 		    MDI_CLIENT_STATE_FAILED)) {
541925e8c5aaSvikram 			i_mdi_client_unlock(ct);
542025e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
542125e8c5aaSvikram 			(void) e_ddi_retire_notify(cdip, constraint);
542225e8c5aaSvikram 			MDI_PHCI_LOCK(ph);
542325e8c5aaSvikram 			pip = next;
542425e8c5aaSvikram 		} else {
542525e8c5aaSvikram 			i_mdi_client_unlock(ct);
542625e8c5aaSvikram 			pip = next;
542725e8c5aaSvikram 		}
542825e8c5aaSvikram 	}
542925e8c5aaSvikram 
543025e8c5aaSvikram 	MDI_PHCI_UNLOCK(ph);
543125e8c5aaSvikram 
543225e8c5aaSvikram 	return;
543325e8c5aaSvikram }
543425e8c5aaSvikram 
543525e8c5aaSvikram /*
54364c06356bSdh  * offline the path(s) hanging off the pHCI. If the
543725e8c5aaSvikram  * last path to any client, check that constraints
543825e8c5aaSvikram  * have been applied.
5439bf002425SStephen Hanson  *
544011779b4cSJeffry Molanus  * If constraint is 0, we aren't going to retire the
5441bf002425SStephen Hanson  * pHCI. However we still need to go through the paths
5442bf002425SStephen Hanson  * calling e_ddi_retire_finalize() to clear their
5443bf002425SStephen Hanson  * contract barriers.
544425e8c5aaSvikram  */
544525e8c5aaSvikram void
mdi_phci_retire_finalize(dev_info_t * dip,int phci_only,void * constraint)5446bf002425SStephen Hanson mdi_phci_retire_finalize(dev_info_t *dip, int phci_only, void *constraint)
544725e8c5aaSvikram {
544825e8c5aaSvikram 	mdi_phci_t	*ph;
544925e8c5aaSvikram 	mdi_client_t	*ct;
545025e8c5aaSvikram 	mdi_pathinfo_t	*pip;
545125e8c5aaSvikram 	mdi_pathinfo_t	*next;
545225e8c5aaSvikram 	dev_info_t	*cdip;
545325e8c5aaSvikram 	int		unstable = 0;
5454bf002425SStephen Hanson 	int		tmp_constraint;
545525e8c5aaSvikram 
545625e8c5aaSvikram 	if (!MDI_PHCI(dip))
545725e8c5aaSvikram 		return;
545825e8c5aaSvikram 
545925e8c5aaSvikram 	ph = i_devi_get_phci(dip);
546025e8c5aaSvikram 	if (ph == NULL) {
546125e8c5aaSvikram 		/* no last path and no pips */
546225e8c5aaSvikram 		return;
546325e8c5aaSvikram 	}
546425e8c5aaSvikram 
546525e8c5aaSvikram 	MDI_PHCI_LOCK(ph);
546625e8c5aaSvikram 
546725e8c5aaSvikram 	if (MDI_PHCI_IS_OFFLINE(ph)) {
546825e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
546925e8c5aaSvikram 		/* no last path and no pips */
547025e8c5aaSvikram 		return;
547125e8c5aaSvikram 	}
547225e8c5aaSvikram 
547325e8c5aaSvikram 	/*
547425e8c5aaSvikram 	 * Check to see if the pHCI can be offlined
547525e8c5aaSvikram 	 */
547625e8c5aaSvikram 	if (ph->ph_unstable) {
547725e8c5aaSvikram 		unstable = 1;
547825e8c5aaSvikram 	}
547925e8c5aaSvikram 
548025e8c5aaSvikram 	pip = ph->ph_path_head;
548125e8c5aaSvikram 	while (pip != NULL) {
548225e8c5aaSvikram 		MDI_PI_LOCK(pip);
548325e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
548425e8c5aaSvikram 
548525e8c5aaSvikram 		/*
548625e8c5aaSvikram 		 * if failover in progress fail the pHCI from offlining
548725e8c5aaSvikram 		 */
548825e8c5aaSvikram 		ct = MDI_PI(pip)->pi_client;
548925e8c5aaSvikram 		i_mdi_client_lock(ct, pip);
549025e8c5aaSvikram 		if ((MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) ||
549125e8c5aaSvikram 		    (ct->ct_unstable)) {
549225e8c5aaSvikram 			unstable = 1;
549325e8c5aaSvikram 		}
549425e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
549525e8c5aaSvikram 
549625e8c5aaSvikram 		/*
549725e8c5aaSvikram 		 * Check to see of we are removing the last path of this
549825e8c5aaSvikram 		 * client device...
549925e8c5aaSvikram 		 */
550025e8c5aaSvikram 		cdip = ct->ct_dip;
550125e8c5aaSvikram 		if (!phci_only && cdip &&
550225e8c5aaSvikram 		    (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
550325e8c5aaSvikram 		    (i_mdi_client_compute_state(ct, ph) ==
550425e8c5aaSvikram 		    MDI_CLIENT_STATE_FAILED)) {
550525e8c5aaSvikram 			i_mdi_client_unlock(ct);
550625e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
550725e8c5aaSvikram 			/*
5508bf002425SStephen Hanson 			 * This is the last path to this client.
5509bf002425SStephen Hanson 			 *
5510bf002425SStephen Hanson 			 * Constraint will only be set to 1 if this client can
5511bf002425SStephen Hanson 			 * be retired (as already determined by
5512bf002425SStephen Hanson 			 * mdi_phci_retire_notify). However we don't actually
5513bf002425SStephen Hanson 			 * need to retire the client (we just retire the last
5514bf002425SStephen Hanson 			 * path - MPXIO will then fail all I/Os to the client).
5515bf002425SStephen Hanson 			 * But we still need to call e_ddi_retire_finalize so
5516bf002425SStephen Hanson 			 * the contract barriers can be cleared. Therefore we
5517bf002425SStephen Hanson 			 * temporarily set constraint = 0 so that the client
5518bf002425SStephen Hanson 			 * dip is not retired.
551925e8c5aaSvikram 			 */
5520bf002425SStephen Hanson 			tmp_constraint = 0;
5521bf002425SStephen Hanson 			(void) e_ddi_retire_finalize(cdip, &tmp_constraint);
552225e8c5aaSvikram 			MDI_PHCI_LOCK(ph);
552325e8c5aaSvikram 			pip = next;
552425e8c5aaSvikram 		} else {
552525e8c5aaSvikram 			i_mdi_client_unlock(ct);
552625e8c5aaSvikram 			pip = next;
552725e8c5aaSvikram 		}
552825e8c5aaSvikram 	}
552925e8c5aaSvikram 
5530bf002425SStephen Hanson 	if (!phci_only && *((int *)constraint) == 0) {
5531bf002425SStephen Hanson 		MDI_PHCI_UNLOCK(ph);
5532bf002425SStephen Hanson 		return;
5533bf002425SStephen Hanson 	}
5534bf002425SStephen Hanson 
553525e8c5aaSvikram 	/*
553625e8c5aaSvikram 	 * Cannot offline pip(s)
553725e8c5aaSvikram 	 */
553825e8c5aaSvikram 	if (unstable) {
55394c06356bSdh 		cmn_err(CE_WARN, "%s%d: mdi_phci_retire_finalize: "
55404c06356bSdh 		    "pHCI in transient state, cannot retire",
55414c06356bSdh 		    ddi_driver_name(dip), ddi_get_instance(dip));
554225e8c5aaSvikram 		MDI_PHCI_UNLOCK(ph);
554325e8c5aaSvikram 		return;
554425e8c5aaSvikram 	}
554525e8c5aaSvikram 
554625e8c5aaSvikram 	/*
554725e8c5aaSvikram 	 * Mark the pHCI as offline
554825e8c5aaSvikram 	 */
554925e8c5aaSvikram 	MDI_PHCI_SET_OFFLINE(ph);
555025e8c5aaSvikram 
555125e8c5aaSvikram 	/*
555225e8c5aaSvikram 	 * Mark the child mdi_pathinfo nodes as transient
555325e8c5aaSvikram 	 */
555425e8c5aaSvikram 	pip = ph->ph_path_head;
555525e8c5aaSvikram 	while (pip != NULL) {
555625e8c5aaSvikram 		MDI_PI_LOCK(pip);
555725e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
555825e8c5aaSvikram 		MDI_PI_SET_OFFLINING(pip);
555925e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
556025e8c5aaSvikram 		pip = next;
556125e8c5aaSvikram 	}
556225e8c5aaSvikram 	MDI_PHCI_UNLOCK(ph);
556325e8c5aaSvikram 	/*
556425e8c5aaSvikram 	 * Give a chance for any pending commands to execute
556525e8c5aaSvikram 	 */
556696c4a178SChris Horne 	delay_random(mdi_delay);
556725e8c5aaSvikram 	MDI_PHCI_LOCK(ph);
556825e8c5aaSvikram 	pip = ph->ph_path_head;
556925e8c5aaSvikram 	while (pip != NULL) {
557025e8c5aaSvikram 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
557125e8c5aaSvikram 		(void) i_mdi_pi_offline(pip, 0);
557225e8c5aaSvikram 		MDI_PI_LOCK(pip);
557325e8c5aaSvikram 		ct = MDI_PI(pip)->pi_client;
557425e8c5aaSvikram 		if (!MDI_PI_IS_OFFLINE(pip)) {
55754c06356bSdh 			cmn_err(CE_WARN, "mdi_phci_retire_finalize: "
55764c06356bSdh 			    "path %d %s busy, cannot offline",
55774c06356bSdh 			    mdi_pi_get_path_instance(pip),
55784c06356bSdh 			    mdi_pi_spathname(pip));
557925e8c5aaSvikram 			MDI_PI_UNLOCK(pip);
558025e8c5aaSvikram 			MDI_PHCI_SET_ONLINE(ph);
558125e8c5aaSvikram 			MDI_PHCI_UNLOCK(ph);
558225e8c5aaSvikram 			return;
558325e8c5aaSvikram 		}
558425e8c5aaSvikram 		MDI_PI_UNLOCK(pip);
558525e8c5aaSvikram 		pip = next;
558625e8c5aaSvikram 	}
558725e8c5aaSvikram 	MDI_PHCI_UNLOCK(ph);
558825e8c5aaSvikram 
558925e8c5aaSvikram 	return;
559025e8c5aaSvikram }
559125e8c5aaSvikram 
559225e8c5aaSvikram void
mdi_phci_unretire(dev_info_t * dip)559325e8c5aaSvikram mdi_phci_unretire(dev_info_t *dip)
559425e8c5aaSvikram {
5595f76de749SStephen Hanson 	mdi_phci_t	*ph;
5596f76de749SStephen Hanson 	mdi_pathinfo_t	*pip;
5597f76de749SStephen Hanson 	mdi_pathinfo_t	*next;
5598f76de749SStephen Hanson 
559925e8c5aaSvikram 	ASSERT(MDI_PHCI(dip));
560025e8c5aaSvikram 
560125e8c5aaSvikram 	/*
560225e8c5aaSvikram 	 * Online the phci
560325e8c5aaSvikram 	 */
560425e8c5aaSvikram 	i_mdi_phci_online(dip);
5605f76de749SStephen Hanson 
5606f76de749SStephen Hanson 	ph = i_devi_get_phci(dip);
5607f76de749SStephen Hanson 	MDI_PHCI_LOCK(ph);
5608f76de749SStephen Hanson 	pip = ph->ph_path_head;
5609f76de749SStephen Hanson 	while (pip != NULL) {
5610f76de749SStephen Hanson 		MDI_PI_LOCK(pip);
5611f76de749SStephen Hanson 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
5612f76de749SStephen Hanson 		MDI_PI_UNLOCK(pip);
5613f76de749SStephen Hanson 		(void) i_mdi_pi_online(pip, 0);
5614f76de749SStephen Hanson 		pip = next;
5615f76de749SStephen Hanson 	}
5616f76de749SStephen Hanson 	MDI_PHCI_UNLOCK(ph);
561725e8c5aaSvikram }
561825e8c5aaSvikram 
56197c478bd9Sstevel@tonic-gate /*ARGSUSED*/
56207c478bd9Sstevel@tonic-gate static int
i_mdi_client_offline(dev_info_t * dip,uint_t flags)56217c478bd9Sstevel@tonic-gate i_mdi_client_offline(dev_info_t *dip, uint_t flags)
56227c478bd9Sstevel@tonic-gate {
56237c478bd9Sstevel@tonic-gate 	int		rv = NDI_SUCCESS;
56247c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
56257c478bd9Sstevel@tonic-gate 
56267c478bd9Sstevel@tonic-gate 	/*
56277c478bd9Sstevel@tonic-gate 	 * Client component to go offline.  Make sure that we are
56287c478bd9Sstevel@tonic-gate 	 * not in failing over state and update client state
56297c478bd9Sstevel@tonic-gate 	 * accordingly
56307c478bd9Sstevel@tonic-gate 	 */
56317c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(dip);
56324c06356bSdh 	MDI_DEBUG(2, (MDI_NOTE, dip,
56334c06356bSdh 	    "called %p %p", (void *)dip, (void *)ct));
56347c478bd9Sstevel@tonic-gate 	if (ct != NULL) {
56357c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
56367c478bd9Sstevel@tonic-gate 		if (ct->ct_unstable) {
56377c478bd9Sstevel@tonic-gate 			/*
56387c478bd9Sstevel@tonic-gate 			 * One or more paths are in transient state,
56397c478bd9Sstevel@tonic-gate 			 * Dont allow offline of a client device
56407c478bd9Sstevel@tonic-gate 			 */
56414c06356bSdh 			MDI_DEBUG(1, (MDI_WARN, dip,
56424c06356bSdh 			    "!One or more paths to "
56434c06356bSdh 			    "this device are in transient state. "
56444c06356bSdh 			    "This device can not be removed at this moment. "
56457c478bd9Sstevel@tonic-gate 			    "Please try again later."));
56467c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
56477c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
56487c478bd9Sstevel@tonic-gate 		}
56497c478bd9Sstevel@tonic-gate 		if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
56507c478bd9Sstevel@tonic-gate 			/*
56517c478bd9Sstevel@tonic-gate 			 * Failover is in progress, Dont allow DR of
56527c478bd9Sstevel@tonic-gate 			 * a client device
56537c478bd9Sstevel@tonic-gate 			 */
56544c06356bSdh 			MDI_DEBUG(1, (MDI_WARN, dip,
56554c06356bSdh 			    "!Client device is Busy. "
56564c06356bSdh 			    "This device can not be removed at this moment. "
56574c06356bSdh 			    "Please try again later."));
56587c478bd9Sstevel@tonic-gate 			MDI_CLIENT_UNLOCK(ct);
56597c478bd9Sstevel@tonic-gate 			return (NDI_BUSY);
56607c478bd9Sstevel@tonic-gate 		}
56617c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_OFFLINE(ct);
56627c478bd9Sstevel@tonic-gate 
56637c478bd9Sstevel@tonic-gate 		/*
56647c478bd9Sstevel@tonic-gate 		 * Unbind our relationship with the dev_info node
56657c478bd9Sstevel@tonic-gate 		 */
56667c478bd9Sstevel@tonic-gate 		if (flags & NDI_DEVI_REMOVE) {
56677c478bd9Sstevel@tonic-gate 			ct->ct_dip = NULL;
56687c478bd9Sstevel@tonic-gate 		}
56697c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
56707c478bd9Sstevel@tonic-gate 	}
56717c478bd9Sstevel@tonic-gate 	return (rv);
56727c478bd9Sstevel@tonic-gate }
56737c478bd9Sstevel@tonic-gate 
56747c478bd9Sstevel@tonic-gate /*
56757c478bd9Sstevel@tonic-gate  * mdi_pre_attach():
56767c478bd9Sstevel@tonic-gate  *		Pre attach() notification handler
56777c478bd9Sstevel@tonic-gate  */
56787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
56797c478bd9Sstevel@tonic-gate int
mdi_pre_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)56807c478bd9Sstevel@tonic-gate mdi_pre_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
56817c478bd9Sstevel@tonic-gate {
56827c478bd9Sstevel@tonic-gate 	/* don't support old DDI_PM_RESUME */
56837c478bd9Sstevel@tonic-gate 	if ((DEVI(dip)->devi_mdi_component != MDI_COMPONENT_NONE) &&
56847c478bd9Sstevel@tonic-gate 	    (cmd == DDI_PM_RESUME))
56857c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
56867c478bd9Sstevel@tonic-gate 
56877c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
56887c478bd9Sstevel@tonic-gate }
56897c478bd9Sstevel@tonic-gate 
56907c478bd9Sstevel@tonic-gate /*
56917c478bd9Sstevel@tonic-gate  * mdi_post_attach():
56927c478bd9Sstevel@tonic-gate  *		Post attach() notification handler
56937c478bd9Sstevel@tonic-gate  */
56947c478bd9Sstevel@tonic-gate /*ARGSUSED*/
56957c478bd9Sstevel@tonic-gate void
mdi_post_attach(dev_info_t * dip,ddi_attach_cmd_t cmd,int error)56967c478bd9Sstevel@tonic-gate mdi_post_attach(dev_info_t *dip, ddi_attach_cmd_t cmd, int error)
56977c478bd9Sstevel@tonic-gate {
56987c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
56997c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
570037fbbce5Scth 	mdi_vhci_t	*vh;
57017c478bd9Sstevel@tonic-gate 
57027c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
57037c478bd9Sstevel@tonic-gate 		ph = i_devi_get_phci(dip);
57047c478bd9Sstevel@tonic-gate 		ASSERT(ph != NULL);
57057c478bd9Sstevel@tonic-gate 
57067c478bd9Sstevel@tonic-gate 		MDI_PHCI_LOCK(ph);
57077c478bd9Sstevel@tonic-gate 		switch (cmd) {
57087c478bd9Sstevel@tonic-gate 		case DDI_ATTACH:
57094c06356bSdh 			MDI_DEBUG(2, (MDI_NOTE, dip,
57104c06356bSdh 			    "phci post_attach called %p", (void *)ph));
57117c478bd9Sstevel@tonic-gate 			if (error == DDI_SUCCESS) {
57127c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_ATTACH(ph);
57137c478bd9Sstevel@tonic-gate 			} else {
57144c06356bSdh 				MDI_DEBUG(1, (MDI_NOTE, dip,
57154c06356bSdh 				    "!pHCI post_attach failed: error %d",
57167c478bd9Sstevel@tonic-gate 				    error));
57177c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_DETACH(ph);
57187c478bd9Sstevel@tonic-gate 			}
57197c478bd9Sstevel@tonic-gate 			break;
57207c478bd9Sstevel@tonic-gate 
57217c478bd9Sstevel@tonic-gate 		case DDI_RESUME:
57223df2e8b2SRobert Mustacchi 		case DDI_PM_RESUME:
57234c06356bSdh 			MDI_DEBUG(2, (MDI_NOTE, dip,
57244c06356bSdh 			    "pHCI post_resume: called %p", (void *)ph));
57257c478bd9Sstevel@tonic-gate 			if (error == DDI_SUCCESS) {
57267c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_RESUME(ph);
57277c478bd9Sstevel@tonic-gate 			} else {
57284c06356bSdh 				MDI_DEBUG(1, (MDI_NOTE, dip,
57294c06356bSdh 				    "!pHCI post_resume failed: error %d",
57307c478bd9Sstevel@tonic-gate 				    error));
57317c478bd9Sstevel@tonic-gate 				MDI_PHCI_SET_SUSPEND(ph);
57327c478bd9Sstevel@tonic-gate 			}
57337c478bd9Sstevel@tonic-gate 			break;
57347c478bd9Sstevel@tonic-gate 		}
57357c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
57367c478bd9Sstevel@tonic-gate 	}
57377c478bd9Sstevel@tonic-gate 
57387c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
57397c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(dip);
57407c478bd9Sstevel@tonic-gate 		ASSERT(ct != NULL);
57417c478bd9Sstevel@tonic-gate 
57427c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
57437c478bd9Sstevel@tonic-gate 		switch (cmd) {
57447c478bd9Sstevel@tonic-gate 		case DDI_ATTACH:
57454c06356bSdh 			MDI_DEBUG(2, (MDI_NOTE, dip,
57464c06356bSdh 			    "client post_attach called %p", (void *)ct));
57477c478bd9Sstevel@tonic-gate 			if (error != DDI_SUCCESS) {
57484c06356bSdh 				MDI_DEBUG(1, (MDI_NOTE, dip,
57494c06356bSdh 				    "!client post_attach failed: error %d",
57507c478bd9Sstevel@tonic-gate 				    error));
57517c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_DETACH(ct);
57524c06356bSdh 				MDI_DEBUG(4, (MDI_WARN, dip,
57534c06356bSdh 				    "i_mdi_pm_reset_client"));
57547c478bd9Sstevel@tonic-gate 				i_mdi_pm_reset_client(ct);
57557c478bd9Sstevel@tonic-gate 				break;
57567c478bd9Sstevel@tonic-gate 			}
57577c478bd9Sstevel@tonic-gate 
57587c478bd9Sstevel@tonic-gate 			/*
575937fbbce5Scth 			 * Client device has successfully attached, inform
576037fbbce5Scth 			 * the vhci.
57617c478bd9Sstevel@tonic-gate 			 */
576237fbbce5Scth 			vh = ct->ct_vhci;
576337fbbce5Scth 			if (vh->vh_ops->vo_client_attached)
576437fbbce5Scth 				(*vh->vh_ops->vo_client_attached)(dip);
576537fbbce5Scth 
57667c478bd9Sstevel@tonic-gate 			MDI_CLIENT_SET_ATTACH(ct);
57677c478bd9Sstevel@tonic-gate 			break;
57687c478bd9Sstevel@tonic-gate 
57697c478bd9Sstevel@tonic-gate 		case DDI_RESUME:
57703df2e8b2SRobert Mustacchi 		case DDI_PM_RESUME:
57714c06356bSdh 			MDI_DEBUG(2, (MDI_NOTE, dip,
57724c06356bSdh 			    "client post_attach: called %p", (void *)ct));
57737c478bd9Sstevel@tonic-gate 			if (error == DDI_SUCCESS) {
57747c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_RESUME(ct);
57757c478bd9Sstevel@tonic-gate 			} else {
57764c06356bSdh 				MDI_DEBUG(1, (MDI_NOTE, dip,
57774c06356bSdh 				    "!client post_resume failed: error %d",
57787c478bd9Sstevel@tonic-gate 				    error));
57797c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_SUSPEND(ct);
57807c478bd9Sstevel@tonic-gate 			}
57817c478bd9Sstevel@tonic-gate 			break;
57827c478bd9Sstevel@tonic-gate 		}
57837c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
57847c478bd9Sstevel@tonic-gate 	}
57857c478bd9Sstevel@tonic-gate }
57867c478bd9Sstevel@tonic-gate 
57877c478bd9Sstevel@tonic-gate /*
57887c478bd9Sstevel@tonic-gate  * mdi_pre_detach():
57897c478bd9Sstevel@tonic-gate  *		Pre detach notification handler
57907c478bd9Sstevel@tonic-gate  */
57917c478bd9Sstevel@tonic-gate /*ARGSUSED*/
57927c478bd9Sstevel@tonic-gate int
mdi_pre_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)57937c478bd9Sstevel@tonic-gate mdi_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
57947c478bd9Sstevel@tonic-gate {
57957c478bd9Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
57967c478bd9Sstevel@tonic-gate 
57977c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip)) {
57987c478bd9Sstevel@tonic-gate 		(void) i_mdi_client_pre_detach(dip, cmd);
57997c478bd9Sstevel@tonic-gate 	}
58007c478bd9Sstevel@tonic-gate 
58017c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip)) {
58027c478bd9Sstevel@tonic-gate 		rv = i_mdi_phci_pre_detach(dip, cmd);
58037c478bd9Sstevel@tonic-gate 	}
58047c478bd9Sstevel@tonic-gate 
58057c478bd9Sstevel@tonic-gate 	return (rv);
58067c478bd9Sstevel@tonic-gate }
58077c478bd9Sstevel@tonic-gate 
58087c478bd9Sstevel@tonic-gate /*ARGSUSED*/
58097c478bd9Sstevel@tonic-gate static int
i_mdi_phci_pre_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)58107c478bd9Sstevel@tonic-gate i_mdi_phci_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
58117c478bd9Sstevel@tonic-gate {
58127c478bd9Sstevel@tonic-gate 	int		rv = DDI_SUCCESS;
58137c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
58147c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
58157c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*pip;
58167c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*failed_pip = NULL;
58177c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next;
58187c478bd9Sstevel@tonic-gate 
58197c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(dip);
58207c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
58217c478bd9Sstevel@tonic-gate 		return (rv);
58227c478bd9Sstevel@tonic-gate 	}
58237c478bd9Sstevel@tonic-gate 
58247c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
58257c478bd9Sstevel@tonic-gate 	switch (cmd) {
58267c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
58274c06356bSdh 		MDI_DEBUG(2, (MDI_NOTE, dip,
58284c06356bSdh 		    "pHCI pre_detach: called %p", (void *)ph));
58297c478bd9Sstevel@tonic-gate 		if (!MDI_PHCI_IS_OFFLINE(ph)) {
58307c478bd9Sstevel@tonic-gate 			/*
58317c478bd9Sstevel@tonic-gate 			 * mdi_pathinfo nodes are still attached to
58327c478bd9Sstevel@tonic-gate 			 * this pHCI. Fail the detach for this pHCI.
58337c478bd9Sstevel@tonic-gate 			 */
58344c06356bSdh 			MDI_DEBUG(2, (MDI_WARN, dip,
58354c06356bSdh 			    "pHCI pre_detach: paths are still attached %p",
58364c06356bSdh 			    (void *)ph));
58377c478bd9Sstevel@tonic-gate 			rv = DDI_FAILURE;
58387c478bd9Sstevel@tonic-gate 			break;
58397c478bd9Sstevel@tonic-gate 		}
58407c478bd9Sstevel@tonic-gate 		MDI_PHCI_SET_DETACH(ph);
58417c478bd9Sstevel@tonic-gate 		break;
58427c478bd9Sstevel@tonic-gate 
58437c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
58447c478bd9Sstevel@tonic-gate 		/*
58457c478bd9Sstevel@tonic-gate 		 * pHCI is getting suspended.  Since mpxio client
58467c478bd9Sstevel@tonic-gate 		 * devices may not be suspended at this point, to avoid
58477c478bd9Sstevel@tonic-gate 		 * a potential stack overflow, it is important to suspend
58487c478bd9Sstevel@tonic-gate 		 * client devices before pHCI can be suspended.
58497c478bd9Sstevel@tonic-gate 		 */
58507c478bd9Sstevel@tonic-gate 
58514c06356bSdh 		MDI_DEBUG(2, (MDI_NOTE, dip,
58524c06356bSdh 		    "pHCI pre_suspend: called %p", (void *)ph));
58537c478bd9Sstevel@tonic-gate 		/*
58547c478bd9Sstevel@tonic-gate 		 * Suspend all the client devices accessible through this pHCI
58557c478bd9Sstevel@tonic-gate 		 */
58567c478bd9Sstevel@tonic-gate 		pip = ph->ph_path_head;
58577c478bd9Sstevel@tonic-gate 		while (pip != NULL && rv == DDI_SUCCESS) {
58587c478bd9Sstevel@tonic-gate 			dev_info_t *cdip;
58597c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
58607c478bd9Sstevel@tonic-gate 			next =
58617c478bd9Sstevel@tonic-gate 			    (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
58627c478bd9Sstevel@tonic-gate 			ct = MDI_PI(pip)->pi_client;
58637c478bd9Sstevel@tonic-gate 			i_mdi_client_lock(ct, pip);
58647c478bd9Sstevel@tonic-gate 			cdip = ct->ct_dip;
58657c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
58667c478bd9Sstevel@tonic-gate 			if ((MDI_CLIENT_IS_DETACHED(ct) == 0) &&
58677c478bd9Sstevel@tonic-gate 			    MDI_CLIENT_IS_SUSPENDED(ct) == 0) {
58687c478bd9Sstevel@tonic-gate 				i_mdi_client_unlock(ct);
58697c478bd9Sstevel@tonic-gate 				if ((rv = devi_detach(cdip, DDI_SUSPEND)) !=
58707c478bd9Sstevel@tonic-gate 				    DDI_SUCCESS) {
58717c478bd9Sstevel@tonic-gate 					/*
58727c478bd9Sstevel@tonic-gate 					 * Suspend of one of the client
58737c478bd9Sstevel@tonic-gate 					 * device has failed.
58747c478bd9Sstevel@tonic-gate 					 */
58754c06356bSdh 					MDI_DEBUG(1, (MDI_WARN, dip,
58764c06356bSdh 					    "!suspend of device (%s%d) failed.",
58777c478bd9Sstevel@tonic-gate 					    ddi_driver_name(cdip),
58787c478bd9Sstevel@tonic-gate 					    ddi_get_instance(cdip)));
58797c478bd9Sstevel@tonic-gate 					failed_pip = pip;
58807c478bd9Sstevel@tonic-gate 					break;
58817c478bd9Sstevel@tonic-gate 				}
58827c478bd9Sstevel@tonic-gate 			} else {
58837c478bd9Sstevel@tonic-gate 				i_mdi_client_unlock(ct);
58847c478bd9Sstevel@tonic-gate 			}
58857c478bd9Sstevel@tonic-gate 			pip = next;
58867c478bd9Sstevel@tonic-gate 		}
58877c478bd9Sstevel@tonic-gate 
58887c478bd9Sstevel@tonic-gate 		if (rv == DDI_SUCCESS) {
58897c478bd9Sstevel@tonic-gate 			/*
58907c478bd9Sstevel@tonic-gate 			 * Suspend of client devices is complete. Proceed
58917c478bd9Sstevel@tonic-gate 			 * with pHCI suspend.
58927c478bd9Sstevel@tonic-gate 			 */
58937c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_SUSPEND(ph);
58947c478bd9Sstevel@tonic-gate 		} else {
58957c478bd9Sstevel@tonic-gate 			/*
58967c478bd9Sstevel@tonic-gate 			 * Revert back all the suspended client device states
58977c478bd9Sstevel@tonic-gate 			 * to converse.
58987c478bd9Sstevel@tonic-gate 			 */
58997c478bd9Sstevel@tonic-gate 			pip = ph->ph_path_head;
59007c478bd9Sstevel@tonic-gate 			while (pip != failed_pip) {
59017c478bd9Sstevel@tonic-gate 				dev_info_t *cdip;
59027c478bd9Sstevel@tonic-gate 				MDI_PI_LOCK(pip);
59037c478bd9Sstevel@tonic-gate 				next =
59047c478bd9Sstevel@tonic-gate 				    (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
59057c478bd9Sstevel@tonic-gate 				ct = MDI_PI(pip)->pi_client;
59067c478bd9Sstevel@tonic-gate 				i_mdi_client_lock(ct, pip);
59077c478bd9Sstevel@tonic-gate 				cdip = ct->ct_dip;
59087c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
59097c478bd9Sstevel@tonic-gate 				if (MDI_CLIENT_IS_SUSPENDED(ct)) {
59107c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
59117c478bd9Sstevel@tonic-gate 					(void) devi_attach(cdip, DDI_RESUME);
59127c478bd9Sstevel@tonic-gate 				} else {
59137c478bd9Sstevel@tonic-gate 					i_mdi_client_unlock(ct);
59147c478bd9Sstevel@tonic-gate 				}
59157c478bd9Sstevel@tonic-gate 				pip = next;
59167c478bd9Sstevel@tonic-gate 			}
59177c478bd9Sstevel@tonic-gate 		}
59187c478bd9Sstevel@tonic-gate 		break;
59197c478bd9Sstevel@tonic-gate 
59207c478bd9Sstevel@tonic-gate 	default:
59217c478bd9Sstevel@tonic-gate 		rv = DDI_FAILURE;
59227c478bd9Sstevel@tonic-gate 		break;
59237c478bd9Sstevel@tonic-gate 	}
59247c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
59257c478bd9Sstevel@tonic-gate 	return (rv);
59267c478bd9Sstevel@tonic-gate }
59277c478bd9Sstevel@tonic-gate 
59287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
59297c478bd9Sstevel@tonic-gate static int
i_mdi_client_pre_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)59307c478bd9Sstevel@tonic-gate i_mdi_client_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
59317c478bd9Sstevel@tonic-gate {
59327c478bd9Sstevel@tonic-gate 	int		rv = DDI_SUCCESS;
59337c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
59347c478bd9Sstevel@tonic-gate 
59357c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(dip);
59367c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
59377c478bd9Sstevel@tonic-gate 		return (rv);
59387c478bd9Sstevel@tonic-gate 	}
59397c478bd9Sstevel@tonic-gate 
59407c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
59417c478bd9Sstevel@tonic-gate 	switch (cmd) {
59427c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
59434c06356bSdh 		MDI_DEBUG(2, (MDI_NOTE, dip,
59444c06356bSdh 		    "client pre_detach: called %p",
59454c06356bSdh 		     (void *)ct));
59467c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_DETACH(ct);
59477c478bd9Sstevel@tonic-gate 		break;
59487c478bd9Sstevel@tonic-gate 
59497c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
59504c06356bSdh 		MDI_DEBUG(2, (MDI_NOTE, dip,
59514c06356bSdh 		    "client pre_suspend: called %p",
59524c06356bSdh 		    (void *)ct));
59537c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_SUSPEND(ct);
59547c478bd9Sstevel@tonic-gate 		break;
59557c478bd9Sstevel@tonic-gate 
59567c478bd9Sstevel@tonic-gate 	default:
59577c478bd9Sstevel@tonic-gate 		rv = DDI_FAILURE;
59587c478bd9Sstevel@tonic-gate 		break;
59597c478bd9Sstevel@tonic-gate 	}
59607c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
59617c478bd9Sstevel@tonic-gate 	return (rv);
59627c478bd9Sstevel@tonic-gate }
59637c478bd9Sstevel@tonic-gate 
59647c478bd9Sstevel@tonic-gate /*
59657c478bd9Sstevel@tonic-gate  * mdi_post_detach():
59667c478bd9Sstevel@tonic-gate  *		Post detach notification handler
59677c478bd9Sstevel@tonic-gate  */
59687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
59697c478bd9Sstevel@tonic-gate void
mdi_post_detach(dev_info_t * dip,ddi_detach_cmd_t cmd,int error)59707c478bd9Sstevel@tonic-gate mdi_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
59717c478bd9Sstevel@tonic-gate {
59727c478bd9Sstevel@tonic-gate 	/*
59737c478bd9Sstevel@tonic-gate 	 * Detach/Suspend of mpxio component failed. Update our state
59747c478bd9Sstevel@tonic-gate 	 * too
59757c478bd9Sstevel@tonic-gate 	 */
59767c478bd9Sstevel@tonic-gate 	if (MDI_PHCI(dip))
59777c478bd9Sstevel@tonic-gate 		i_mdi_phci_post_detach(dip, cmd, error);
59787c478bd9Sstevel@tonic-gate 
59797c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT(dip))
59807c478bd9Sstevel@tonic-gate 		i_mdi_client_post_detach(dip, cmd, error);
59817c478bd9Sstevel@tonic-gate }
59827c478bd9Sstevel@tonic-gate 
59837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
59847c478bd9Sstevel@tonic-gate static void
i_mdi_phci_post_detach(dev_info_t * dip,ddi_detach_cmd_t cmd,int error)59857c478bd9Sstevel@tonic-gate i_mdi_phci_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
59867c478bd9Sstevel@tonic-gate {
59877c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
59887c478bd9Sstevel@tonic-gate 
59897c478bd9Sstevel@tonic-gate 	/*
59907c478bd9Sstevel@tonic-gate 	 * Detach/Suspend of phci component failed. Update our state
59917c478bd9Sstevel@tonic-gate 	 * too
59927c478bd9Sstevel@tonic-gate 	 */
59937c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(dip);
59947c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
59957c478bd9Sstevel@tonic-gate 		return;
59967c478bd9Sstevel@tonic-gate 	}
59977c478bd9Sstevel@tonic-gate 
59987c478bd9Sstevel@tonic-gate 	MDI_PHCI_LOCK(ph);
59997c478bd9Sstevel@tonic-gate 	/*
60007c478bd9Sstevel@tonic-gate 	 * Detach of pHCI failed. Restore back converse
60017c478bd9Sstevel@tonic-gate 	 * state
60027c478bd9Sstevel@tonic-gate 	 */
60037c478bd9Sstevel@tonic-gate 	switch (cmd) {
60047c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
60054c06356bSdh 		MDI_DEBUG(2, (MDI_NOTE, dip,
60064c06356bSdh 		    "pHCI post_detach: called %p",
60074c06356bSdh 		    (void *)ph));
60087c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
60097c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_ATTACH(ph);
60107c478bd9Sstevel@tonic-gate 		break;
60117c478bd9Sstevel@tonic-gate 
60127c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
60133df2e8b2SRobert Mustacchi 	case DDI_PM_SUSPEND:
60144c06356bSdh 		MDI_DEBUG(2, (MDI_NOTE, dip,
60154c06356bSdh 		    "pHCI post_suspend: called %p",
60164c06356bSdh 		    (void *)ph));
60177c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
60187c478bd9Sstevel@tonic-gate 			MDI_PHCI_SET_RESUME(ph);
60197c478bd9Sstevel@tonic-gate 		break;
60203df2e8b2SRobert Mustacchi 	case DDI_HOTPLUG_DETACH:
60213df2e8b2SRobert Mustacchi 		break;
60227c478bd9Sstevel@tonic-gate 	}
60237c478bd9Sstevel@tonic-gate 	MDI_PHCI_UNLOCK(ph);
60247c478bd9Sstevel@tonic-gate }
60257c478bd9Sstevel@tonic-gate 
60267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
60277c478bd9Sstevel@tonic-gate static void
i_mdi_client_post_detach(dev_info_t * dip,ddi_detach_cmd_t cmd,int error)60287c478bd9Sstevel@tonic-gate i_mdi_client_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
60297c478bd9Sstevel@tonic-gate {
60307c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
60317c478bd9Sstevel@tonic-gate 
60327c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(dip);
60337c478bd9Sstevel@tonic-gate 	if (ct == NULL) {
60347c478bd9Sstevel@tonic-gate 		return;
60357c478bd9Sstevel@tonic-gate 	}
60367c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
60377c478bd9Sstevel@tonic-gate 	/*
60387c478bd9Sstevel@tonic-gate 	 * Detach of Client failed. Restore back converse
60397c478bd9Sstevel@tonic-gate 	 * state
60407c478bd9Sstevel@tonic-gate 	 */
60417c478bd9Sstevel@tonic-gate 	switch (cmd) {
60427c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
60434c06356bSdh 		MDI_DEBUG(2, (MDI_NOTE, dip,
60444c06356bSdh 		    "client post_detach: called %p", (void *)ct));
60451b94a41bSChris Horne 		if (DEVI_IS_ATTACHING(dip)) {
60464c06356bSdh 			MDI_DEBUG(4, (MDI_NOTE, dip,
60477c478bd9Sstevel@tonic-gate 			    "i_mdi_pm_rele_client\n"));
60487c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, ct->ct_path_count);
60497c478bd9Sstevel@tonic-gate 		} else {
60504c06356bSdh 			MDI_DEBUG(4, (MDI_NOTE, dip,
60517c478bd9Sstevel@tonic-gate 			    "i_mdi_pm_reset_client\n"));
60527c478bd9Sstevel@tonic-gate 			i_mdi_pm_reset_client(ct);
60537c478bd9Sstevel@tonic-gate 		}
60547c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
60557c478bd9Sstevel@tonic-gate 			MDI_CLIENT_SET_ATTACH(ct);
60567c478bd9Sstevel@tonic-gate 		break;
60577c478bd9Sstevel@tonic-gate 
60587c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
60593df2e8b2SRobert Mustacchi 	case DDI_PM_SUSPEND:
60604c06356bSdh 		MDI_DEBUG(2, (MDI_NOTE, dip,
60614c06356bSdh 		    "called %p", (void *)ct));
60627c478bd9Sstevel@tonic-gate 		if (error != DDI_SUCCESS)
60637c478bd9Sstevel@tonic-gate 			MDI_CLIENT_SET_RESUME(ct);
60647c478bd9Sstevel@tonic-gate 		break;
60653df2e8b2SRobert Mustacchi 	case DDI_HOTPLUG_DETACH:
60663df2e8b2SRobert Mustacchi 		break;
60677c478bd9Sstevel@tonic-gate 	}
60687c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
60697c478bd9Sstevel@tonic-gate }
60707c478bd9Sstevel@tonic-gate 
607137fbbce5Scth int
mdi_pi_kstat_exists(mdi_pathinfo_t * pip)607237fbbce5Scth mdi_pi_kstat_exists(mdi_pathinfo_t *pip)
607337fbbce5Scth {
607437fbbce5Scth 	return (MDI_PI(pip)->pi_kstats ? 1 : 0);
607537fbbce5Scth }
607637fbbce5Scth 
60777c478bd9Sstevel@tonic-gate /*
60787c478bd9Sstevel@tonic-gate  * create and install per-path (client - pHCI) statistics
60797c478bd9Sstevel@tonic-gate  * I/O stats supported: nread, nwritten, reads, and writes
60807c478bd9Sstevel@tonic-gate  * Error stats - hard errors, soft errors, & transport errors
60817c478bd9Sstevel@tonic-gate  */
608237fbbce5Scth int
mdi_pi_kstat_create(mdi_pathinfo_t * pip,char * ksname)608337fbbce5Scth mdi_pi_kstat_create(mdi_pathinfo_t *pip, char *ksname)
60847c478bd9Sstevel@tonic-gate {
608537fbbce5Scth 	kstat_t			*kiosp, *kerrsp;
608637fbbce5Scth 	struct pi_errs		*nsp;
608737fbbce5Scth 	struct mdi_pi_kstats	*mdi_statp;
60887c478bd9Sstevel@tonic-gate 
60897c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_kstats != NULL)
60907c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
60917c478bd9Sstevel@tonic-gate 
60927c478bd9Sstevel@tonic-gate 	if ((kiosp = kstat_create("mdi", 0, ksname, "iopath",
609337fbbce5Scth 	    KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT)) == NULL) {
60947c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
60957c478bd9Sstevel@tonic-gate 	}
60967c478bd9Sstevel@tonic-gate 
609737fbbce5Scth 	(void) strcat(ksname, ",err");
60987c478bd9Sstevel@tonic-gate 	kerrsp = kstat_create("mdi", 0, ksname, "iopath_errors",
60997c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED,
61007c478bd9Sstevel@tonic-gate 	    sizeof (struct pi_errs) / sizeof (kstat_named_t), 0);
61017c478bd9Sstevel@tonic-gate 	if (kerrsp == NULL) {
61027c478bd9Sstevel@tonic-gate 		kstat_delete(kiosp);
61037c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
61047c478bd9Sstevel@tonic-gate 	}
61057c478bd9Sstevel@tonic-gate 
61067c478bd9Sstevel@tonic-gate 	nsp = (struct pi_errs *)kerrsp->ks_data;
61077c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_softerrs, "Soft Errors", KSTAT_DATA_UINT32);
61087c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_harderrs, "Hard Errors", KSTAT_DATA_UINT32);
61097c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_transerrs, "Transport Errors",
61107c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
61117c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_icnt_busy, "Interconnect Busy",
61127c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
61137c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_icnt_errors, "Interconnect Errors",
61147c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
61157c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_phci_rsrc, "pHCI No Resources",
61167c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
61177c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_phci_localerr, "pHCI Local Errors",
61187c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
61197c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_phci_invstate, "pHCI Invalid State",
61207c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
61217c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_failedfrom, "Failed From",
61227c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
61237c478bd9Sstevel@tonic-gate 	kstat_named_init(&nsp->pi_failedto, "Failed To", KSTAT_DATA_UINT32);
61247c478bd9Sstevel@tonic-gate 
61257c478bd9Sstevel@tonic-gate 	mdi_statp = kmem_alloc(sizeof (*mdi_statp), KM_SLEEP);
61267c478bd9Sstevel@tonic-gate 	mdi_statp->pi_kstat_ref = 1;
61277c478bd9Sstevel@tonic-gate 	mdi_statp->pi_kstat_iostats = kiosp;
61287c478bd9Sstevel@tonic-gate 	mdi_statp->pi_kstat_errstats = kerrsp;
61297c478bd9Sstevel@tonic-gate 	kstat_install(kiosp);
61307c478bd9Sstevel@tonic-gate 	kstat_install(kerrsp);
61317c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_kstats = mdi_statp;
61327c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
61337c478bd9Sstevel@tonic-gate }
61347c478bd9Sstevel@tonic-gate 
61357c478bd9Sstevel@tonic-gate /*
61367c478bd9Sstevel@tonic-gate  * destroy per-path properties
61377c478bd9Sstevel@tonic-gate  */
61387c478bd9Sstevel@tonic-gate static void
i_mdi_pi_kstat_destroy(mdi_pathinfo_t * pip)61397c478bd9Sstevel@tonic-gate i_mdi_pi_kstat_destroy(mdi_pathinfo_t *pip)
61407c478bd9Sstevel@tonic-gate {
61417c478bd9Sstevel@tonic-gate 
61427c478bd9Sstevel@tonic-gate 	struct mdi_pi_kstats *mdi_statp;
61437c478bd9Sstevel@tonic-gate 
614437fbbce5Scth 	if (MDI_PI(pip)->pi_kstats == NULL)
614537fbbce5Scth 		return;
61467c478bd9Sstevel@tonic-gate 	if ((mdi_statp = MDI_PI(pip)->pi_kstats) == NULL)
61477c478bd9Sstevel@tonic-gate 		return;
61487c478bd9Sstevel@tonic-gate 
61497c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_kstats = NULL;
61507c478bd9Sstevel@tonic-gate 
61517c478bd9Sstevel@tonic-gate 	/*
61527c478bd9Sstevel@tonic-gate 	 * the kstat may be shared between multiple pathinfo nodes
61537c478bd9Sstevel@tonic-gate 	 * decrement this pathinfo's usage, removing the kstats
61547c478bd9Sstevel@tonic-gate 	 * themselves when the last pathinfo reference is removed.
61557c478bd9Sstevel@tonic-gate 	 */
61567c478bd9Sstevel@tonic-gate 	ASSERT(mdi_statp->pi_kstat_ref > 0);
61577c478bd9Sstevel@tonic-gate 	if (--mdi_statp->pi_kstat_ref != 0)
61587c478bd9Sstevel@tonic-gate 		return;
61597c478bd9Sstevel@tonic-gate 
61607c478bd9Sstevel@tonic-gate 	kstat_delete(mdi_statp->pi_kstat_iostats);
61617c478bd9Sstevel@tonic-gate 	kstat_delete(mdi_statp->pi_kstat_errstats);
61627c478bd9Sstevel@tonic-gate 	kmem_free(mdi_statp, sizeof (*mdi_statp));
61637c478bd9Sstevel@tonic-gate }
61647c478bd9Sstevel@tonic-gate 
61657c478bd9Sstevel@tonic-gate /*
61667c478bd9Sstevel@tonic-gate  * update I/O paths KSTATS
61677c478bd9Sstevel@tonic-gate  */
61687c478bd9Sstevel@tonic-gate void
mdi_pi_kstat_iosupdate(mdi_pathinfo_t * pip,struct buf * bp)61697c478bd9Sstevel@tonic-gate mdi_pi_kstat_iosupdate(mdi_pathinfo_t *pip, struct buf *bp)
61707c478bd9Sstevel@tonic-gate {
61717c478bd9Sstevel@tonic-gate 	kstat_t *iostatp;
61727c478bd9Sstevel@tonic-gate 	size_t xfer_cnt;
61737c478bd9Sstevel@tonic-gate 
61747c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
61757c478bd9Sstevel@tonic-gate 
61767c478bd9Sstevel@tonic-gate 	/*
61777c478bd9Sstevel@tonic-gate 	 * I/O can be driven across a path prior to having path
61787c478bd9Sstevel@tonic-gate 	 * statistics available, i.e. probe(9e).
61797c478bd9Sstevel@tonic-gate 	 */
61807c478bd9Sstevel@tonic-gate 	if (bp != NULL && MDI_PI(pip)->pi_kstats != NULL) {
61817c478bd9Sstevel@tonic-gate 		iostatp = MDI_PI(pip)->pi_kstats->pi_kstat_iostats;
61827c478bd9Sstevel@tonic-gate 		xfer_cnt = bp->b_bcount - bp->b_resid;
61837c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
61847c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->reads++;
61857c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->nread += xfer_cnt;
61867c478bd9Sstevel@tonic-gate 		} else {
61877c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->writes++;
61887c478bd9Sstevel@tonic-gate 			KSTAT_IO_PTR(iostatp)->nwritten += xfer_cnt;
61897c478bd9Sstevel@tonic-gate 		}
61907c478bd9Sstevel@tonic-gate 	}
61917c478bd9Sstevel@tonic-gate }
61927c478bd9Sstevel@tonic-gate 
6193ee28b439Scm /*
6194ee28b439Scm  * Enable the path(specific client/target/initiator)
6195ee28b439Scm  * Enabling a path means that MPxIO may select the enabled path for routing
6196ee28b439Scm  * future I/O requests, subject to other path state constraints.
6197ee28b439Scm  */
6198ee28b439Scm int
mdi_pi_enable_path(mdi_pathinfo_t * pip,int flags)6199ee28b439Scm mdi_pi_enable_path(mdi_pathinfo_t *pip, int flags)
6200ee28b439Scm {
6201ee28b439Scm 	mdi_phci_t	*ph;
6202ee28b439Scm 
62034c06356bSdh 	ph = MDI_PI(pip)->pi_phci;
6204ee28b439Scm 	if (ph == NULL) {
62054c06356bSdh 		MDI_DEBUG(1, (MDI_NOTE, mdi_pi_get_phci(pip),
62064c06356bSdh 		    "!failed: path %s %p: NULL ph",
62074c06356bSdh 		    mdi_pi_spathname(pip), (void *)pip));
6208ee28b439Scm 		return (MDI_FAILURE);
6209ee28b439Scm 	}
6210ee28b439Scm 
6211ee28b439Scm 	(void) i_mdi_enable_disable_path(pip, ph->ph_vhci, flags,
6212ee28b439Scm 		MDI_ENABLE_OP);
62134c06356bSdh 	MDI_DEBUG(5, (MDI_NOTE, ph->ph_dip,
62144c06356bSdh 	    "!returning success pip = %p. ph = %p",
62154c06356bSdh 	    (void *)pip, (void *)ph));
6216ee28b439Scm 	return (MDI_SUCCESS);
6217ee28b439Scm 
6218ee28b439Scm }
6219ee28b439Scm 
6220ee28b439Scm /*
6221ee28b439Scm  * Disable the path (specific client/target/initiator)
6222ee28b439Scm  * Disabling a path means that MPxIO will not select the disabled path for
6223ee28b439Scm  * routing any new I/O requests.
6224ee28b439Scm  */
6225ee28b439Scm int
mdi_pi_disable_path(mdi_pathinfo_t * pip,int flags)6226ee28b439Scm mdi_pi_disable_path(mdi_pathinfo_t *pip, int flags)
6227ee28b439Scm {
6228ee28b439Scm 	mdi_phci_t	*ph;
6229ee28b439Scm 
62304c06356bSdh 	ph = MDI_PI(pip)->pi_phci;
6231ee28b439Scm 	if (ph == NULL) {
62324c06356bSdh 		MDI_DEBUG(1, (MDI_NOTE, mdi_pi_get_phci(pip),
62334c06356bSdh 		    "!failed: path %s %p: NULL ph",
62344c06356bSdh 		    mdi_pi_spathname(pip), (void *)pip));
6235ee28b439Scm 		return (MDI_FAILURE);
6236ee28b439Scm 	}
6237ee28b439Scm 
6238ee28b439Scm 	(void) i_mdi_enable_disable_path(pip,
62394c06356bSdh 	    ph->ph_vhci, flags, MDI_DISABLE_OP);
62404c06356bSdh 	MDI_DEBUG(5, (MDI_NOTE, ph->ph_dip,
62414c06356bSdh 	    "!returning success pip = %p. ph = %p",
62424c06356bSdh 	    (void *)pip, (void *)ph));
6243ee28b439Scm 	return (MDI_SUCCESS);
6244ee28b439Scm }
6245ee28b439Scm 
62467c478bd9Sstevel@tonic-gate /*
62477c478bd9Sstevel@tonic-gate  * disable the path to a particular pHCI (pHCI specified in the phci_path
62487c478bd9Sstevel@tonic-gate  * argument) for a particular client (specified in the client_path argument).
62497c478bd9Sstevel@tonic-gate  * Disabling a path means that MPxIO will not select the disabled path for
62507c478bd9Sstevel@tonic-gate  * routing any new I/O requests.
6251ee28b439Scm  * NOTE: this will be removed once the NWS files are changed to use the new
6252ee28b439Scm  * mdi_{enable,disable}_path interfaces
62537c478bd9Sstevel@tonic-gate  */
62547c478bd9Sstevel@tonic-gate int
mdi_pi_disable(dev_info_t * cdip,dev_info_t * pdip,int flags)62557c478bd9Sstevel@tonic-gate mdi_pi_disable(dev_info_t *cdip, dev_info_t *pdip, int flags)
62567c478bd9Sstevel@tonic-gate {
62577c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_enable_disable(cdip, pdip, flags, MDI_DISABLE_OP));
62587c478bd9Sstevel@tonic-gate }
62597c478bd9Sstevel@tonic-gate 
62607c478bd9Sstevel@tonic-gate /*
62617c478bd9Sstevel@tonic-gate  * Enable the path to a particular pHCI (pHCI specified in the phci_path
62627c478bd9Sstevel@tonic-gate  * argument) for a particular client (specified in the client_path argument).
62637c478bd9Sstevel@tonic-gate  * Enabling a path means that MPxIO may select the enabled path for routing
62647c478bd9Sstevel@tonic-gate  * future I/O requests, subject to other path state constraints.
6265ee28b439Scm  * NOTE: this will be removed once the NWS files are changed to use the new
6266ee28b439Scm  * mdi_{enable,disable}_path interfaces
62677c478bd9Sstevel@tonic-gate  */
62687c478bd9Sstevel@tonic-gate 
62697c478bd9Sstevel@tonic-gate int
mdi_pi_enable(dev_info_t * cdip,dev_info_t * pdip,int flags)62707c478bd9Sstevel@tonic-gate mdi_pi_enable(dev_info_t *cdip, dev_info_t *pdip, int flags)
62717c478bd9Sstevel@tonic-gate {
62727c478bd9Sstevel@tonic-gate 	return (i_mdi_pi_enable_disable(cdip, pdip, flags, MDI_ENABLE_OP));
62737c478bd9Sstevel@tonic-gate }
62747c478bd9Sstevel@tonic-gate 
6275ee28b439Scm /*
6276ee28b439Scm  * Common routine for doing enable/disable.
6277ee28b439Scm  */
6278ee28b439Scm static mdi_pathinfo_t *
i_mdi_enable_disable_path(mdi_pathinfo_t * pip,mdi_vhci_t * vh,int flags,int op)6279ee28b439Scm i_mdi_enable_disable_path(mdi_pathinfo_t *pip, mdi_vhci_t *vh, int flags,
6280ee28b439Scm 		int op)
6281ee28b439Scm {
6282ee28b439Scm 	int		sync_flag = 0;
6283ee28b439Scm 	int		rv;
6284d27aea3fSDan Cross 	mdi_pathinfo_t	*next;
6285ee28b439Scm 	int		(*f)() = NULL;
6286ee28b439Scm 
62876c8e19d4SZach Kissel 	/*
62886c8e19d4SZach Kissel 	 * Check to make sure the path is not already in the
62896c8e19d4SZach Kissel 	 * requested state. If it is just return the next path
62906c8e19d4SZach Kissel 	 * as we have nothing to do here.
62916c8e19d4SZach Kissel 	 */
62926c8e19d4SZach Kissel 	if ((MDI_PI_IS_DISABLE(pip) && op == MDI_DISABLE_OP) ||
62936c8e19d4SZach Kissel 	    (!MDI_PI_IS_DISABLE(pip) && op == MDI_ENABLE_OP)) {
62946c8e19d4SZach Kissel 		MDI_PI_LOCK(pip);
62956c8e19d4SZach Kissel 		next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
62966c8e19d4SZach Kissel 		MDI_PI_UNLOCK(pip);
62976c8e19d4SZach Kissel 		return (next);
62986c8e19d4SZach Kissel 	}
62996c8e19d4SZach Kissel 
6300ee28b439Scm 	f = vh->vh_ops->vo_pi_state_change;
6301ee28b439Scm 
6302ee28b439Scm 	sync_flag = (flags << 8) & 0xf00;
6303ee28b439Scm 
6304ee28b439Scm 	/*
6305ee28b439Scm 	 * Do a callback into the mdi consumer to let it
6306ee28b439Scm 	 * know that path is about to get enabled/disabled.
6307ee28b439Scm 	 */
6308c6f039c7SToomas Soome 	rv = MDI_SUCCESS;
6309ee28b439Scm 	if (f != NULL) {
6310ee28b439Scm 		rv = (*f)(vh->vh_dip, pip, 0,
6311ee28b439Scm 			MDI_PI_EXT_STATE(pip),
6312ee28b439Scm 			MDI_EXT_STATE_CHANGE | sync_flag |
6313ee28b439Scm 			op | MDI_BEFORE_STATE_CHANGE);
6314ee28b439Scm 		if (rv != MDI_SUCCESS) {
63154c06356bSdh 			MDI_DEBUG(2, (MDI_WARN, vh->vh_dip,
63164c06356bSdh 			    "vo_pi_state_change: failed rv = %x", rv));
6317ee28b439Scm 		}
6318ee28b439Scm 	}
6319ee28b439Scm 	MDI_PI_LOCK(pip);
6320ee28b439Scm 	next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
6321ee28b439Scm 
6322ee28b439Scm 	switch (flags) {
6323ee28b439Scm 		case USER_DISABLE:
63245e3986cbScth 			if (op == MDI_DISABLE_OP) {
6325ee28b439Scm 				MDI_PI_SET_USER_DISABLE(pip);
63265e3986cbScth 			} else {
6327ee28b439Scm 				MDI_PI_SET_USER_ENABLE(pip);
63285e3986cbScth 			}
6329ee28b439Scm 			break;
6330ee28b439Scm 		case DRIVER_DISABLE:
63315e3986cbScth 			if (op == MDI_DISABLE_OP) {
6332ee28b439Scm 				MDI_PI_SET_DRV_DISABLE(pip);
63335e3986cbScth 			} else {
6334ee28b439Scm 				MDI_PI_SET_DRV_ENABLE(pip);
63355e3986cbScth 			}
6336ee28b439Scm 			break;
6337ee28b439Scm 		case DRIVER_DISABLE_TRANSIENT:
63385e3986cbScth 			if (op == MDI_DISABLE_OP && rv == MDI_SUCCESS) {
6339ee28b439Scm 				MDI_PI_SET_DRV_DISABLE_TRANS(pip);
63405e3986cbScth 			} else {
6341ee28b439Scm 				MDI_PI_SET_DRV_ENABLE_TRANS(pip);
63425e3986cbScth 			}
6343ee28b439Scm 			break;
6344ee28b439Scm 	}
6345ee28b439Scm 	MDI_PI_UNLOCK(pip);
6346ee28b439Scm 	/*
6347ee28b439Scm 	 * Do a callback into the mdi consumer to let it
6348ee28b439Scm 	 * know that path is now enabled/disabled.
6349ee28b439Scm 	 */
6350ee28b439Scm 	if (f != NULL) {
6351ee28b439Scm 		rv = (*f)(vh->vh_dip, pip, 0,
6352ee28b439Scm 			MDI_PI_EXT_STATE(pip),
6353ee28b439Scm 			MDI_EXT_STATE_CHANGE | sync_flag |
6354ee28b439Scm 			op | MDI_AFTER_STATE_CHANGE);
6355ee28b439Scm 		if (rv != MDI_SUCCESS) {
63564c06356bSdh 			MDI_DEBUG(2, (MDI_WARN, vh->vh_dip,
63574c06356bSdh 			    "vo_pi_state_change failed: rv = %x", rv));
6358ee28b439Scm 		}
6359ee28b439Scm 	}
6360ee28b439Scm 	return (next);
6361ee28b439Scm }
63627c478bd9Sstevel@tonic-gate 
63637c478bd9Sstevel@tonic-gate /*
63647c478bd9Sstevel@tonic-gate  * Common routine for doing enable/disable.
6365ee28b439Scm  * NOTE: this will be removed once the NWS files are changed to use the new
6366ee28b439Scm  * mdi_{enable,disable}_path has been putback
63677c478bd9Sstevel@tonic-gate  */
63687c478bd9Sstevel@tonic-gate int
i_mdi_pi_enable_disable(dev_info_t * cdip,dev_info_t * pdip,int flags,int op)63697c478bd9Sstevel@tonic-gate i_mdi_pi_enable_disable(dev_info_t *cdip, dev_info_t *pdip, int flags, int op)
63707c478bd9Sstevel@tonic-gate {
63717c478bd9Sstevel@tonic-gate 
63727c478bd9Sstevel@tonic-gate 	mdi_phci_t	*ph;
63737c478bd9Sstevel@tonic-gate 	mdi_vhci_t	*vh = NULL;
63747c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
63757c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t	*next, *pip;
63767c478bd9Sstevel@tonic-gate 	int		found_it;
63777c478bd9Sstevel@tonic-gate 
63787c478bd9Sstevel@tonic-gate 	ph = i_devi_get_phci(pdip);
63794c06356bSdh 	MDI_DEBUG(5, (MDI_NOTE, cdip ? cdip : pdip,
63804c06356bSdh 	    "!op = %d pdip = %p cdip = %p", op, (void *)pdip,
63814c06356bSdh 	    (void *)cdip));
63827c478bd9Sstevel@tonic-gate 	if (ph == NULL) {
63834c06356bSdh 		MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
63844c06356bSdh 		    "!failed: operation %d: NULL ph", op));
63857c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
63867c478bd9Sstevel@tonic-gate 	}
63877c478bd9Sstevel@tonic-gate 
63887c478bd9Sstevel@tonic-gate 	if ((op != MDI_ENABLE_OP) && (op != MDI_DISABLE_OP)) {
63894c06356bSdh 		MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
63904c06356bSdh 		    "!failed: invalid operation %d", op));
63917c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
63927c478bd9Sstevel@tonic-gate 	}
63937c478bd9Sstevel@tonic-gate 
63947c478bd9Sstevel@tonic-gate 	vh = ph->ph_vhci;
63957c478bd9Sstevel@tonic-gate 
63967c478bd9Sstevel@tonic-gate 	if (cdip == NULL) {
63977c478bd9Sstevel@tonic-gate 		/*
63987c478bd9Sstevel@tonic-gate 		 * Need to mark the Phci as enabled/disabled.
63997c478bd9Sstevel@tonic-gate 		 */
64004c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, cdip ? cdip : pdip,
64014c06356bSdh 		    "op %d for the phci", op));
64027c478bd9Sstevel@tonic-gate 		MDI_PHCI_LOCK(ph);
64037c478bd9Sstevel@tonic-gate 		switch (flags) {
64047c478bd9Sstevel@tonic-gate 			case USER_DISABLE:
64055e3986cbScth 				if (op == MDI_DISABLE_OP) {
64067c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_USER_DISABLE(ph);
64075e3986cbScth 				} else {
64087c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_USER_ENABLE(ph);
64095e3986cbScth 				}
64107c478bd9Sstevel@tonic-gate 				break;
64117c478bd9Sstevel@tonic-gate 			case DRIVER_DISABLE:
64125e3986cbScth 				if (op == MDI_DISABLE_OP) {
64137c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_DISABLE(ph);
64145e3986cbScth 				} else {
64157c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_ENABLE(ph);
64165e3986cbScth 				}
64177c478bd9Sstevel@tonic-gate 				break;
64187c478bd9Sstevel@tonic-gate 			case DRIVER_DISABLE_TRANSIENT:
64195e3986cbScth 				if (op == MDI_DISABLE_OP) {
64207c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_DISABLE_TRANSIENT(ph);
64215e3986cbScth 				} else {
64227c478bd9Sstevel@tonic-gate 					MDI_PHCI_SET_DRV_ENABLE_TRANSIENT(ph);
64235e3986cbScth 				}
64247c478bd9Sstevel@tonic-gate 				break;
64257c478bd9Sstevel@tonic-gate 			default:
64267c478bd9Sstevel@tonic-gate 				MDI_PHCI_UNLOCK(ph);
64274c06356bSdh 				MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
64284c06356bSdh 				    "!invalid flag argument= %d", flags));
64297c478bd9Sstevel@tonic-gate 		}
64307c478bd9Sstevel@tonic-gate 
64317c478bd9Sstevel@tonic-gate 		/*
64327c478bd9Sstevel@tonic-gate 		 * Phci has been disabled. Now try to enable/disable
64337c478bd9Sstevel@tonic-gate 		 * path info's to each client.
64347c478bd9Sstevel@tonic-gate 		 */
64357c478bd9Sstevel@tonic-gate 		pip = ph->ph_path_head;
64367c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
6437ee28b439Scm 			pip = i_mdi_enable_disable_path(pip, vh, flags, op);
64387c478bd9Sstevel@tonic-gate 		}
64397c478bd9Sstevel@tonic-gate 		MDI_PHCI_UNLOCK(ph);
64407c478bd9Sstevel@tonic-gate 	} else {
64417c478bd9Sstevel@tonic-gate 
64427c478bd9Sstevel@tonic-gate 		/*
64437c478bd9Sstevel@tonic-gate 		 * Disable a specific client.
64447c478bd9Sstevel@tonic-gate 		 */
64457c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(cdip);
64467c478bd9Sstevel@tonic-gate 		if (ct == NULL) {
64474c06356bSdh 			MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
64484c06356bSdh 			    "!failed: operation = %d: NULL ct", op));
64497c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
64507c478bd9Sstevel@tonic-gate 		}
64517c478bd9Sstevel@tonic-gate 
64527c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
64537c478bd9Sstevel@tonic-gate 		pip = ct->ct_path_head;
64547c478bd9Sstevel@tonic-gate 		found_it = 0;
64557c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
64567c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
64577c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
64587c478bd9Sstevel@tonic-gate 			if (MDI_PI(pip)->pi_phci == ph) {
64597c478bd9Sstevel@tonic-gate 				MDI_PI_UNLOCK(pip);
64607c478bd9Sstevel@tonic-gate 				found_it = 1;
64617c478bd9Sstevel@tonic-gate 				break;
64627c478bd9Sstevel@tonic-gate 			}
64637c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
64647c478bd9Sstevel@tonic-gate 			pip = next;
64657c478bd9Sstevel@tonic-gate 		}
64667c478bd9Sstevel@tonic-gate 
6467ee28b439Scm 
64687c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
64697c478bd9Sstevel@tonic-gate 		if (found_it == 0) {
64704c06356bSdh 			MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
64714c06356bSdh 			    "!failed. Could not find corresponding pip\n"));
64727c478bd9Sstevel@tonic-gate 			return (MDI_FAILURE);
64737c478bd9Sstevel@tonic-gate 		}
6474ee28b439Scm 
6475ee28b439Scm 		(void) i_mdi_enable_disable_path(pip, vh, flags, op);
64767c478bd9Sstevel@tonic-gate 	}
64777c478bd9Sstevel@tonic-gate 
64784c06356bSdh 	MDI_DEBUG(5, (MDI_NOTE, cdip ? cdip : pdip,
64794c06356bSdh 	    "!op %d returning success pdip = %p cdip = %p",
64804c06356bSdh 	    op, (void *)pdip, (void *)cdip));
64817c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
64827c478bd9Sstevel@tonic-gate }
64837c478bd9Sstevel@tonic-gate 
64847c478bd9Sstevel@tonic-gate /*
64857c478bd9Sstevel@tonic-gate  * Ensure phci powered up
64867c478bd9Sstevel@tonic-gate  */
64877c478bd9Sstevel@tonic-gate static void
i_mdi_pm_hold_pip(mdi_pathinfo_t * pip)64887c478bd9Sstevel@tonic-gate i_mdi_pm_hold_pip(mdi_pathinfo_t *pip)
64897c478bd9Sstevel@tonic-gate {
64907c478bd9Sstevel@tonic-gate 	dev_info_t	*ph_dip;
64917c478bd9Sstevel@tonic-gate 
64927c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
64935e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
64947c478bd9Sstevel@tonic-gate 
64957c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held) {
64967c478bd9Sstevel@tonic-gate 		return;
64977c478bd9Sstevel@tonic-gate 	}
64987c478bd9Sstevel@tonic-gate 
64997c478bd9Sstevel@tonic-gate 	ph_dip = mdi_pi_get_phci(pip);
65004c06356bSdh 	MDI_DEBUG(4, (MDI_NOTE, ph_dip,
65014c06356bSdh 	    "%s %p", mdi_pi_spathname(pip), (void *)pip));
65027c478bd9Sstevel@tonic-gate 	if (ph_dip == NULL) {
65037c478bd9Sstevel@tonic-gate 		return;
65047c478bd9Sstevel@tonic-gate 	}
65057c478bd9Sstevel@tonic-gate 
65067c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
65074c06356bSdh 	MDI_DEBUG(4, (MDI_NOTE, ph_dip, "kidsupcnt was %d",
65087c478bd9Sstevel@tonic-gate 	    DEVI(ph_dip)->devi_pm_kidsupcnt));
65097c478bd9Sstevel@tonic-gate 	pm_hold_power(ph_dip);
65104c06356bSdh 	MDI_DEBUG(4, (MDI_NOTE, ph_dip, "kidsupcnt is %d",
65117c478bd9Sstevel@tonic-gate 	    DEVI(ph_dip)->devi_pm_kidsupcnt));
65127c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
65137c478bd9Sstevel@tonic-gate 
65145e3986cbScth 	/* If PM_GET_PM_INFO is NULL the pm_hold_power above was a noop */
65155e3986cbScth 	if (DEVI(ph_dip)->devi_pm_info)
65165e3986cbScth 		MDI_PI(pip)->pi_pm_held = 1;
65177c478bd9Sstevel@tonic-gate }
65187c478bd9Sstevel@tonic-gate 
65197c478bd9Sstevel@tonic-gate /*
65207c478bd9Sstevel@tonic-gate  * Allow phci powered down
65217c478bd9Sstevel@tonic-gate  */
65227c478bd9Sstevel@tonic-gate static void
i_mdi_pm_rele_pip(mdi_pathinfo_t * pip)65237c478bd9Sstevel@tonic-gate i_mdi_pm_rele_pip(mdi_pathinfo_t *pip)
65247c478bd9Sstevel@tonic-gate {
65257c478bd9Sstevel@tonic-gate 	dev_info_t	*ph_dip = NULL;
65267c478bd9Sstevel@tonic-gate 
65277c478bd9Sstevel@tonic-gate 	ASSERT(pip != NULL);
65285e3986cbScth 	ASSERT(MDI_PI_LOCKED(pip));
65297c478bd9Sstevel@tonic-gate 
65307c478bd9Sstevel@tonic-gate 	if (MDI_PI(pip)->pi_pm_held == 0) {
65317c478bd9Sstevel@tonic-gate 		return;
65327c478bd9Sstevel@tonic-gate 	}
65337c478bd9Sstevel@tonic-gate 
65347c478bd9Sstevel@tonic-gate 	ph_dip = mdi_pi_get_phci(pip);
65357c478bd9Sstevel@tonic-gate 	ASSERT(ph_dip != NULL);
65367c478bd9Sstevel@tonic-gate 
65374c06356bSdh 	MDI_DEBUG(4, (MDI_NOTE, ph_dip,
65384c06356bSdh 	    "%s %p", mdi_pi_spathname(pip), (void *)pip));
65397c478bd9Sstevel@tonic-gate 
65404c06356bSdh 	MDI_PI_UNLOCK(pip);
65414c06356bSdh 	MDI_DEBUG(4, (MDI_NOTE, ph_dip,
65424c06356bSdh 	    "kidsupcnt was %d", DEVI(ph_dip)->devi_pm_kidsupcnt));
65437c478bd9Sstevel@tonic-gate 	pm_rele_power(ph_dip);
65444c06356bSdh 	MDI_DEBUG(4, (MDI_NOTE, ph_dip,
65454c06356bSdh 	    "kidsupcnt is %d", DEVI(ph_dip)->devi_pm_kidsupcnt));
65467c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
65474c06356bSdh 
65487c478bd9Sstevel@tonic-gate 	MDI_PI(pip)->pi_pm_held = 0;
65497c478bd9Sstevel@tonic-gate }
65507c478bd9Sstevel@tonic-gate 
65517c478bd9Sstevel@tonic-gate static void
i_mdi_pm_hold_client(mdi_client_t * ct,int incr)65527c478bd9Sstevel@tonic-gate i_mdi_pm_hold_client(mdi_client_t *ct, int incr)
65537c478bd9Sstevel@tonic-gate {
65545e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
65557c478bd9Sstevel@tonic-gate 
65567c478bd9Sstevel@tonic-gate 	ct->ct_power_cnt += incr;
65574c06356bSdh 	MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
65584c06356bSdh 	    "%p ct_power_cnt = %d incr = %d",
65594c06356bSdh 	    (void *)ct, ct->ct_power_cnt, incr));
65607c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_power_cnt >= 0);
65617c478bd9Sstevel@tonic-gate }
65627c478bd9Sstevel@tonic-gate 
65637c478bd9Sstevel@tonic-gate static void
i_mdi_rele_all_phci(mdi_client_t * ct)65647c478bd9Sstevel@tonic-gate i_mdi_rele_all_phci(mdi_client_t *ct)
65657c478bd9Sstevel@tonic-gate {
65667c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t  *pip;
65677c478bd9Sstevel@tonic-gate 
65685e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
65697c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
65707c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
65717c478bd9Sstevel@tonic-gate 		mdi_hold_path(pip);
65727c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
65737c478bd9Sstevel@tonic-gate 		i_mdi_pm_rele_pip(pip);
65747c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
65757c478bd9Sstevel@tonic-gate 		mdi_rele_path(pip);
65767c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
65777c478bd9Sstevel@tonic-gate 	}
65787c478bd9Sstevel@tonic-gate }
65797c478bd9Sstevel@tonic-gate 
65807c478bd9Sstevel@tonic-gate static void
i_mdi_pm_rele_client(mdi_client_t * ct,int decr)65817c478bd9Sstevel@tonic-gate i_mdi_pm_rele_client(mdi_client_t *ct, int decr)
65827c478bd9Sstevel@tonic-gate {
65835e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
65847c478bd9Sstevel@tonic-gate 
6585737d277aScth 	if (i_ddi_devi_attached(ct->ct_dip)) {
65867c478bd9Sstevel@tonic-gate 		ct->ct_power_cnt -= decr;
65874c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
65884c06356bSdh 		    "%p ct_power_cnt = %d decr = %d",
65895e3986cbScth 		    (void *)ct, ct->ct_power_cnt, decr));
65907c478bd9Sstevel@tonic-gate 	}
65917c478bd9Sstevel@tonic-gate 
65927c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_power_cnt >= 0);
65937c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0) {
65947c478bd9Sstevel@tonic-gate 		i_mdi_rele_all_phci(ct);
65957c478bd9Sstevel@tonic-gate 		return;
65967c478bd9Sstevel@tonic-gate 	}
65977c478bd9Sstevel@tonic-gate }
65987c478bd9Sstevel@tonic-gate 
65997c478bd9Sstevel@tonic-gate static void
i_mdi_pm_reset_client(mdi_client_t * ct)66007c478bd9Sstevel@tonic-gate i_mdi_pm_reset_client(mdi_client_t *ct)
66017c478bd9Sstevel@tonic-gate {
66024c06356bSdh 	MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
66034c06356bSdh 	    "%p ct_power_cnt = %d", (void *)ct, ct->ct_power_cnt));
66045e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
66057c478bd9Sstevel@tonic-gate 	ct->ct_power_cnt = 0;
66067c478bd9Sstevel@tonic-gate 	i_mdi_rele_all_phci(ct);
660778dc6db2Sllai 	ct->ct_powercnt_config = 0;
660878dc6db2Sllai 	ct->ct_powercnt_unconfig = 0;
66097c478bd9Sstevel@tonic-gate 	ct->ct_powercnt_reset = 1;
66107c478bd9Sstevel@tonic-gate }
66117c478bd9Sstevel@tonic-gate 
66127c478bd9Sstevel@tonic-gate static int
i_mdi_power_one_phci(mdi_pathinfo_t * pip)66137c478bd9Sstevel@tonic-gate i_mdi_power_one_phci(mdi_pathinfo_t *pip)
66147c478bd9Sstevel@tonic-gate {
66157c478bd9Sstevel@tonic-gate 	int		ret;
66167c478bd9Sstevel@tonic-gate 	dev_info_t	*ph_dip;
66177c478bd9Sstevel@tonic-gate 
66187c478bd9Sstevel@tonic-gate 	MDI_PI_LOCK(pip);
66197c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_pip(pip);
66207c478bd9Sstevel@tonic-gate 
66217c478bd9Sstevel@tonic-gate 	ph_dip = mdi_pi_get_phci(pip);
66227c478bd9Sstevel@tonic-gate 	MDI_PI_UNLOCK(pip);
66237c478bd9Sstevel@tonic-gate 
66247c478bd9Sstevel@tonic-gate 	/* bring all components of phci to full power */
66254c06356bSdh 	MDI_DEBUG(4, (MDI_NOTE, ph_dip,
66264c06356bSdh 	    "pm_powerup for %s%d %p", ddi_driver_name(ph_dip),
66275e3986cbScth 	    ddi_get_instance(ph_dip), (void *)pip));
66287c478bd9Sstevel@tonic-gate 
66297c478bd9Sstevel@tonic-gate 	ret = pm_powerup(ph_dip);
66307c478bd9Sstevel@tonic-gate 
66317c478bd9Sstevel@tonic-gate 	if (ret == DDI_FAILURE) {
66324c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, ph_dip,
66334c06356bSdh 		    "pm_powerup FAILED for %s%d %p",
663455e592a2SRandall Ralphs 		    ddi_driver_name(ph_dip), ddi_get_instance(ph_dip),
66355e3986cbScth 		    (void *)pip));
66367c478bd9Sstevel@tonic-gate 
66377c478bd9Sstevel@tonic-gate 		MDI_PI_LOCK(pip);
66387c478bd9Sstevel@tonic-gate 		i_mdi_pm_rele_pip(pip);
66397c478bd9Sstevel@tonic-gate 		MDI_PI_UNLOCK(pip);
66407c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
66417c478bd9Sstevel@tonic-gate 	}
66427c478bd9Sstevel@tonic-gate 
66437c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
66447c478bd9Sstevel@tonic-gate }
66457c478bd9Sstevel@tonic-gate 
66467c478bd9Sstevel@tonic-gate static int
i_mdi_power_all_phci(mdi_client_t * ct)66477c478bd9Sstevel@tonic-gate i_mdi_power_all_phci(mdi_client_t *ct)
66487c478bd9Sstevel@tonic-gate {
66497c478bd9Sstevel@tonic-gate 	mdi_pathinfo_t  *pip;
66507c478bd9Sstevel@tonic-gate 	int		succeeded = 0;
66517c478bd9Sstevel@tonic-gate 
66525e3986cbScth 	ASSERT(MDI_CLIENT_LOCKED(ct));
66537c478bd9Sstevel@tonic-gate 	pip = (mdi_pathinfo_t *)ct->ct_path_head;
66547c478bd9Sstevel@tonic-gate 	while (pip != NULL) {
66555e3986cbScth 		/*
66565e3986cbScth 		 * Don't power if MDI_PATHINFO_STATE_FAULT
66575e3986cbScth 		 * or MDI_PATHINFO_STATE_OFFLINE.
66585e3986cbScth 		 */
66595e3986cbScth 		if (MDI_PI_IS_INIT(pip) ||
66605e3986cbScth 		    MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip)) {
66615e3986cbScth 			mdi_hold_path(pip);
66625e3986cbScth 			MDI_CLIENT_UNLOCK(ct);
66635e3986cbScth 			if (i_mdi_power_one_phci(pip) == MDI_SUCCESS)
66645e3986cbScth 				succeeded = 1;
66657c478bd9Sstevel@tonic-gate 
66665e3986cbScth 			ASSERT(ct == MDI_PI(pip)->pi_client);
66675e3986cbScth 			MDI_CLIENT_LOCK(ct);
66685e3986cbScth 			mdi_rele_path(pip);
66695e3986cbScth 		}
66707c478bd9Sstevel@tonic-gate 		pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
66717c478bd9Sstevel@tonic-gate 	}
66727c478bd9Sstevel@tonic-gate 
66737c478bd9Sstevel@tonic-gate 	return (succeeded ? MDI_SUCCESS : MDI_FAILURE);
66747c478bd9Sstevel@tonic-gate }
66757c478bd9Sstevel@tonic-gate 
66767c478bd9Sstevel@tonic-gate /*
66777c478bd9Sstevel@tonic-gate  * mdi_bus_power():
66787c478bd9Sstevel@tonic-gate  *		1. Place the phci(s) into powered up state so that
66797c478bd9Sstevel@tonic-gate  *		   client can do power management
66807c478bd9Sstevel@tonic-gate  *		2. Ensure phci powered up as client power managing
66817c478bd9Sstevel@tonic-gate  * Return Values:
66827c478bd9Sstevel@tonic-gate  *		MDI_SUCCESS
66837c478bd9Sstevel@tonic-gate  *		MDI_FAILURE
66847c478bd9Sstevel@tonic-gate  */
66857c478bd9Sstevel@tonic-gate int
mdi_bus_power(dev_info_t * parent,void * impl_arg,pm_bus_power_op_t op,void * arg,void * result)66867c478bd9Sstevel@tonic-gate mdi_bus_power(dev_info_t *parent, void *impl_arg, pm_bus_power_op_t op,
66877c478bd9Sstevel@tonic-gate     void *arg, void *result)
66887c478bd9Sstevel@tonic-gate {
66897c478bd9Sstevel@tonic-gate 	int			ret = MDI_SUCCESS;
66907c478bd9Sstevel@tonic-gate 	pm_bp_child_pwrchg_t	*bpc;
66917c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
66927c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
66937c478bd9Sstevel@tonic-gate 	pm_bp_has_changed_t	*bphc;
66947c478bd9Sstevel@tonic-gate 
66957c478bd9Sstevel@tonic-gate 	/*
66967c478bd9Sstevel@tonic-gate 	 * BUS_POWER_NOINVOL not supported
66977c478bd9Sstevel@tonic-gate 	 */
66987c478bd9Sstevel@tonic-gate 	if (op == BUS_POWER_NOINVOL)
66997c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
67007c478bd9Sstevel@tonic-gate 
67017c478bd9Sstevel@tonic-gate 	/*
67027c478bd9Sstevel@tonic-gate 	 * ignore other OPs.
67037c478bd9Sstevel@tonic-gate 	 * return quickly to save cou cycles on the ct processing
67047c478bd9Sstevel@tonic-gate 	 */
67057c478bd9Sstevel@tonic-gate 	switch (op) {
67067c478bd9Sstevel@tonic-gate 	case BUS_POWER_PRE_NOTIFICATION:
67077c478bd9Sstevel@tonic-gate 	case BUS_POWER_POST_NOTIFICATION:
67087c478bd9Sstevel@tonic-gate 		bpc = (pm_bp_child_pwrchg_t *)arg;
67097c478bd9Sstevel@tonic-gate 		cdip = bpc->bpc_dip;
67107c478bd9Sstevel@tonic-gate 		break;
67117c478bd9Sstevel@tonic-gate 	case BUS_POWER_HAS_CHANGED:
67127c478bd9Sstevel@tonic-gate 		bphc = (pm_bp_has_changed_t *)arg;
67137c478bd9Sstevel@tonic-gate 		cdip = bphc->bphc_dip;
67147c478bd9Sstevel@tonic-gate 		break;
67157c478bd9Sstevel@tonic-gate 	default:
67167c478bd9Sstevel@tonic-gate 		return (pm_busop_bus_power(parent, impl_arg, op, arg, result));
67177c478bd9Sstevel@tonic-gate 	}
67187c478bd9Sstevel@tonic-gate 
67197c478bd9Sstevel@tonic-gate 	ASSERT(MDI_CLIENT(cdip));
67207c478bd9Sstevel@tonic-gate 
67217c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(cdip);
67227c478bd9Sstevel@tonic-gate 	if (ct == NULL)
67237c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
67247c478bd9Sstevel@tonic-gate 
67257c478bd9Sstevel@tonic-gate 	/*
67267c478bd9Sstevel@tonic-gate 	 * wait till the mdi_pathinfo node state change are processed
67277c478bd9Sstevel@tonic-gate 	 */
67287c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
67297c478bd9Sstevel@tonic-gate 	switch (op) {
67307c478bd9Sstevel@tonic-gate 	case BUS_POWER_PRE_NOTIFICATION:
67314c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
67327c478bd9Sstevel@tonic-gate 		    "BUS_POWER_PRE_NOTIFICATION:"
67334c06356bSdh 		    "%s@%s, olevel=%d, nlevel=%d, comp=%d",
67344c06356bSdh 		    ddi_node_name(bpc->bpc_dip), PM_ADDR(bpc->bpc_dip),
67357c478bd9Sstevel@tonic-gate 		    bpc->bpc_olevel, bpc->bpc_nlevel, bpc->bpc_comp));
67367c478bd9Sstevel@tonic-gate 
67377c478bd9Sstevel@tonic-gate 		/* serialize power level change per client */
67387c478bd9Sstevel@tonic-gate 		while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
67397c478bd9Sstevel@tonic-gate 			cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
67407c478bd9Sstevel@tonic-gate 
67417c478bd9Sstevel@tonic-gate 		MDI_CLIENT_SET_POWER_TRANSITION(ct);
67427c478bd9Sstevel@tonic-gate 
67437c478bd9Sstevel@tonic-gate 		if (ct->ct_power_cnt == 0) {
67447c478bd9Sstevel@tonic-gate 			ret = i_mdi_power_all_phci(ct);
67457c478bd9Sstevel@tonic-gate 		}
67467c478bd9Sstevel@tonic-gate 
67477c478bd9Sstevel@tonic-gate 		/*
67487c478bd9Sstevel@tonic-gate 		 * if new_level > 0:
67497c478bd9Sstevel@tonic-gate 		 *	- hold phci(s)
67507c478bd9Sstevel@tonic-gate 		 *	- power up phci(s) if not already
67517c478bd9Sstevel@tonic-gate 		 * ignore power down
67527c478bd9Sstevel@tonic-gate 		 */
67537c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel > 0) {
67547c478bd9Sstevel@tonic-gate 			if (!DEVI_IS_ATTACHING(ct->ct_dip)) {
67554c06356bSdh 				MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
67564c06356bSdh 				    "i_mdi_pm_hold_client\n"));
67577c478bd9Sstevel@tonic-gate 				i_mdi_pm_hold_client(ct, ct->ct_path_count);
67587c478bd9Sstevel@tonic-gate 			}
67597c478bd9Sstevel@tonic-gate 		}
67607c478bd9Sstevel@tonic-gate 		break;
67617c478bd9Sstevel@tonic-gate 	case BUS_POWER_POST_NOTIFICATION:
67624c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
67637c478bd9Sstevel@tonic-gate 		    "BUS_POWER_POST_NOTIFICATION:"
67644c06356bSdh 		    "%s@%s, olevel=%d, nlevel=%d, comp=%d result=%d",
67654c06356bSdh 		    ddi_node_name(bpc->bpc_dip), PM_ADDR(bpc->bpc_dip),
67667c478bd9Sstevel@tonic-gate 		    bpc->bpc_olevel, bpc->bpc_nlevel, bpc->bpc_comp,
67677c478bd9Sstevel@tonic-gate 		    *(int *)result));
67687c478bd9Sstevel@tonic-gate 
67697c478bd9Sstevel@tonic-gate 		if (*(int *)result == DDI_SUCCESS) {
67707c478bd9Sstevel@tonic-gate 			if (bpc->bpc_nlevel > 0) {
67717c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_POWER_UP(ct);
67727c478bd9Sstevel@tonic-gate 			} else {
67737c478bd9Sstevel@tonic-gate 				MDI_CLIENT_SET_POWER_DOWN(ct);
67747c478bd9Sstevel@tonic-gate 			}
67757c478bd9Sstevel@tonic-gate 		}
67767c478bd9Sstevel@tonic-gate 
67777c478bd9Sstevel@tonic-gate 		/* release the hold we did in pre-notification */
67787c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel > 0 && (*(int *)result != DDI_SUCCESS) &&
67797c478bd9Sstevel@tonic-gate 		    !DEVI_IS_ATTACHING(ct->ct_dip)) {
67804c06356bSdh 			MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
67814c06356bSdh 			    "i_mdi_pm_rele_client\n"));
67827c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, ct->ct_path_count);
67837c478bd9Sstevel@tonic-gate 		}
67847c478bd9Sstevel@tonic-gate 
67857c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel == 0 && (*(int *)result == DDI_SUCCESS)) {
67867c478bd9Sstevel@tonic-gate 			/* another thread might started attaching */
67877c478bd9Sstevel@tonic-gate 			if (DEVI_IS_ATTACHING(ct->ct_dip)) {
67884c06356bSdh 				MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
67894c06356bSdh 				    "i_mdi_pm_rele_client\n"));
67907c478bd9Sstevel@tonic-gate 				i_mdi_pm_rele_client(ct, ct->ct_path_count);
67917c478bd9Sstevel@tonic-gate 			/* detaching has been taken care in pm_post_unconfig */
67927c478bd9Sstevel@tonic-gate 			} else if (!DEVI_IS_DETACHING(ct->ct_dip)) {
67934c06356bSdh 				MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
67944c06356bSdh 				    "i_mdi_pm_reset_client\n"));
67957c478bd9Sstevel@tonic-gate 				i_mdi_pm_reset_client(ct);
67967c478bd9Sstevel@tonic-gate 			}
67977c478bd9Sstevel@tonic-gate 		}
67987c478bd9Sstevel@tonic-gate 
67997c478bd9Sstevel@tonic-gate 		MDI_CLIENT_CLEAR_POWER_TRANSITION(ct);
68007c478bd9Sstevel@tonic-gate 		cv_broadcast(&ct->ct_powerchange_cv);
68017c478bd9Sstevel@tonic-gate 
68027c478bd9Sstevel@tonic-gate 		break;
68037c478bd9Sstevel@tonic-gate 
68047c478bd9Sstevel@tonic-gate 	/* need to do more */
68057c478bd9Sstevel@tonic-gate 	case BUS_POWER_HAS_CHANGED:
68064c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, bphc->bphc_dip,
68077c478bd9Sstevel@tonic-gate 		    "BUS_POWER_HAS_CHANGED:"
68084c06356bSdh 		    "%s@%s, olevel=%d, nlevel=%d, comp=%d",
68094c06356bSdh 		    ddi_node_name(bphc->bphc_dip), PM_ADDR(bphc->bphc_dip),
68107c478bd9Sstevel@tonic-gate 		    bphc->bphc_olevel, bphc->bphc_nlevel, bphc->bphc_comp));
68117c478bd9Sstevel@tonic-gate 
68127c478bd9Sstevel@tonic-gate 		if (bphc->bphc_nlevel > 0 &&
68137c478bd9Sstevel@tonic-gate 		    bphc->bphc_nlevel > bphc->bphc_olevel) {
68147c478bd9Sstevel@tonic-gate 			if (ct->ct_power_cnt == 0) {
68157c478bd9Sstevel@tonic-gate 				ret = i_mdi_power_all_phci(ct);
68167c478bd9Sstevel@tonic-gate 			}
68174c06356bSdh 			MDI_DEBUG(4, (MDI_NOTE, bphc->bphc_dip,
68184c06356bSdh 			    "i_mdi_pm_hold_client\n"));
68197c478bd9Sstevel@tonic-gate 			i_mdi_pm_hold_client(ct, ct->ct_path_count);
68207c478bd9Sstevel@tonic-gate 		}
68217c478bd9Sstevel@tonic-gate 
68227c478bd9Sstevel@tonic-gate 		if (bphc->bphc_nlevel == 0 && bphc->bphc_olevel != -1) {
68234c06356bSdh 			MDI_DEBUG(4, (MDI_NOTE, bphc->bphc_dip,
68244c06356bSdh 			    "i_mdi_pm_rele_client\n"));
68257c478bd9Sstevel@tonic-gate 			i_mdi_pm_rele_client(ct, ct->ct_path_count);
68267c478bd9Sstevel@tonic-gate 		}
68277c478bd9Sstevel@tonic-gate 		break;
68283df2e8b2SRobert Mustacchi 	default:
68293df2e8b2SRobert Mustacchi 		dev_err(parent, CE_WARN, "!unhandled bus power operation: 0x%x",
68303df2e8b2SRobert Mustacchi 		    op);
68313df2e8b2SRobert Mustacchi 		break;
68327c478bd9Sstevel@tonic-gate 	}
68337c478bd9Sstevel@tonic-gate 
68347c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
68357c478bd9Sstevel@tonic-gate 	return (ret);
68367c478bd9Sstevel@tonic-gate }
68377c478bd9Sstevel@tonic-gate 
68387c478bd9Sstevel@tonic-gate static int
i_mdi_pm_pre_config_one(dev_info_t * child)68397c478bd9Sstevel@tonic-gate i_mdi_pm_pre_config_one(dev_info_t *child)
68407c478bd9Sstevel@tonic-gate {
68417c478bd9Sstevel@tonic-gate 	int		ret = MDI_SUCCESS;
68427c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
68437c478bd9Sstevel@tonic-gate 
68447c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
68457c478bd9Sstevel@tonic-gate 	if (ct == NULL)
68467c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
68477c478bd9Sstevel@tonic-gate 
68487c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
68497c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
68507c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
68517c478bd9Sstevel@tonic-gate 
68527c478bd9Sstevel@tonic-gate 	if (!MDI_CLIENT_IS_FAILED(ct)) {
68537c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
68544c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, child, "already configured\n"));
68557c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
68567c478bd9Sstevel@tonic-gate 	}
68577c478bd9Sstevel@tonic-gate 
685878dc6db2Sllai 	if (ct->ct_powercnt_config) {
68597c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
68604c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, child, "already held\n"));
68617c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
68627c478bd9Sstevel@tonic-gate 	}
68637c478bd9Sstevel@tonic-gate 
68647c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0) {
68657c478bd9Sstevel@tonic-gate 		ret = i_mdi_power_all_phci(ct);
68667c478bd9Sstevel@tonic-gate 	}
68674c06356bSdh 	MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_hold_client\n"));
68687c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_client(ct, ct->ct_path_count);
686978dc6db2Sllai 	ct->ct_powercnt_config = 1;
68707c478bd9Sstevel@tonic-gate 	ct->ct_powercnt_reset = 0;
68717c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
68727c478bd9Sstevel@tonic-gate 	return (ret);
68737c478bd9Sstevel@tonic-gate }
68747c478bd9Sstevel@tonic-gate 
68757c478bd9Sstevel@tonic-gate static int
i_mdi_pm_pre_config(dev_info_t * vdip,dev_info_t * child)68765e3986cbScth i_mdi_pm_pre_config(dev_info_t *vdip, dev_info_t *child)
68777c478bd9Sstevel@tonic-gate {
68787c478bd9Sstevel@tonic-gate 	int			ret = MDI_SUCCESS;
68797c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
68807c478bd9Sstevel@tonic-gate 
68815e3986cbScth 	ASSERT(MDI_VHCI(vdip));
68827c478bd9Sstevel@tonic-gate 
68837c478bd9Sstevel@tonic-gate 	/* ndi_devi_config_one */
68847c478bd9Sstevel@tonic-gate 	if (child) {
68855e3986cbScth 		ASSERT(DEVI_BUSY_OWNED(vdip));
68867c478bd9Sstevel@tonic-gate 		return (i_mdi_pm_pre_config_one(child));
68877c478bd9Sstevel@tonic-gate 	}
68887c478bd9Sstevel@tonic-gate 
68897c478bd9Sstevel@tonic-gate 	/* devi_config_common */
68903fe80ca4SDan Cross 	ndi_devi_enter(vdip);
68915e3986cbScth 	cdip = ddi_get_child(vdip);
68927c478bd9Sstevel@tonic-gate 	while (cdip) {
68937c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
68947c478bd9Sstevel@tonic-gate 
68957c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_config_one(cdip);
68967c478bd9Sstevel@tonic-gate 		if (ret != MDI_SUCCESS)
68977c478bd9Sstevel@tonic-gate 			break;
68987c478bd9Sstevel@tonic-gate 		cdip = next;
68997c478bd9Sstevel@tonic-gate 	}
69003fe80ca4SDan Cross 	ndi_devi_exit(vdip);
69017c478bd9Sstevel@tonic-gate 	return (ret);
69027c478bd9Sstevel@tonic-gate }
69037c478bd9Sstevel@tonic-gate 
69047c478bd9Sstevel@tonic-gate static int
i_mdi_pm_pre_unconfig_one(dev_info_t * child,int * held,int flags)69057c478bd9Sstevel@tonic-gate i_mdi_pm_pre_unconfig_one(dev_info_t *child, int *held, int flags)
69067c478bd9Sstevel@tonic-gate {
69077c478bd9Sstevel@tonic-gate 	int		ret = MDI_SUCCESS;
69087c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
69097c478bd9Sstevel@tonic-gate 
69107c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
69117c478bd9Sstevel@tonic-gate 	if (ct == NULL)
69127c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
69137c478bd9Sstevel@tonic-gate 
69147c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
69157c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
69167c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
69177c478bd9Sstevel@tonic-gate 
69181b94a41bSChris Horne 	if (!i_ddi_devi_attached(child)) {
69194c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, child, "node detached already\n"));
69207c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
69217c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
69227c478bd9Sstevel@tonic-gate 	}
69237c478bd9Sstevel@tonic-gate 
69247c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_POWERED_DOWN(ct) &&
69257c478bd9Sstevel@tonic-gate 	    (flags & NDI_AUTODETACH)) {
69264c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, child, "auto-modunload\n"));
69277c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
69287c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
69297c478bd9Sstevel@tonic-gate 	}
69307c478bd9Sstevel@tonic-gate 
693178dc6db2Sllai 	if (ct->ct_powercnt_unconfig) {
69324c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, child, "ct_powercnt_held\n"));
69337c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
69347c478bd9Sstevel@tonic-gate 		*held = 1;
69357c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
69367c478bd9Sstevel@tonic-gate 	}
69377c478bd9Sstevel@tonic-gate 
69387c478bd9Sstevel@tonic-gate 	if (ct->ct_power_cnt == 0) {
69397c478bd9Sstevel@tonic-gate 		ret = i_mdi_power_all_phci(ct);
69407c478bd9Sstevel@tonic-gate 	}
69414c06356bSdh 	MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_hold_client\n"));
69427c478bd9Sstevel@tonic-gate 	i_mdi_pm_hold_client(ct, ct->ct_path_count);
694378dc6db2Sllai 	ct->ct_powercnt_unconfig = 1;
69447c478bd9Sstevel@tonic-gate 	ct->ct_powercnt_reset = 0;
69457c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
69467c478bd9Sstevel@tonic-gate 	if (ret == MDI_SUCCESS)
69477c478bd9Sstevel@tonic-gate 		*held = 1;
69487c478bd9Sstevel@tonic-gate 	return (ret);
69497c478bd9Sstevel@tonic-gate }
69507c478bd9Sstevel@tonic-gate 
69517c478bd9Sstevel@tonic-gate static int
i_mdi_pm_pre_unconfig(dev_info_t * vdip,dev_info_t * child,int * held,int flags)69525e3986cbScth i_mdi_pm_pre_unconfig(dev_info_t *vdip, dev_info_t *child, int *held,
69537c478bd9Sstevel@tonic-gate     int flags)
69547c478bd9Sstevel@tonic-gate {
69557c478bd9Sstevel@tonic-gate 	int			ret = MDI_SUCCESS;
69567c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
69577c478bd9Sstevel@tonic-gate 
69585e3986cbScth 	ASSERT(MDI_VHCI(vdip));
69597c478bd9Sstevel@tonic-gate 	*held = 0;
69607c478bd9Sstevel@tonic-gate 
69617c478bd9Sstevel@tonic-gate 	/* ndi_devi_unconfig_one */
69627c478bd9Sstevel@tonic-gate 	if (child) {
69635e3986cbScth 		ASSERT(DEVI_BUSY_OWNED(vdip));
69647c478bd9Sstevel@tonic-gate 		return (i_mdi_pm_pre_unconfig_one(child, held, flags));
69657c478bd9Sstevel@tonic-gate 	}
69667c478bd9Sstevel@tonic-gate 
69677c478bd9Sstevel@tonic-gate 	/* devi_unconfig_common */
69683fe80ca4SDan Cross 	ndi_devi_enter(vdip);
69695e3986cbScth 	cdip = ddi_get_child(vdip);
69707c478bd9Sstevel@tonic-gate 	while (cdip) {
69717c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
69727c478bd9Sstevel@tonic-gate 
69737c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_unconfig_one(cdip, held, flags);
69747c478bd9Sstevel@tonic-gate 		cdip = next;
69757c478bd9Sstevel@tonic-gate 	}
69763fe80ca4SDan Cross 	ndi_devi_exit(vdip);
69777c478bd9Sstevel@tonic-gate 
69787c478bd9Sstevel@tonic-gate 	if (*held)
69797c478bd9Sstevel@tonic-gate 		ret = MDI_SUCCESS;
69807c478bd9Sstevel@tonic-gate 
69817c478bd9Sstevel@tonic-gate 	return (ret);
69827c478bd9Sstevel@tonic-gate }
69837c478bd9Sstevel@tonic-gate 
69847c478bd9Sstevel@tonic-gate static void
i_mdi_pm_post_config_one(dev_info_t * child)69857c478bd9Sstevel@tonic-gate i_mdi_pm_post_config_one(dev_info_t *child)
69867c478bd9Sstevel@tonic-gate {
69877c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
69887c478bd9Sstevel@tonic-gate 
69897c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
69907c478bd9Sstevel@tonic-gate 	if (ct == NULL)
69917c478bd9Sstevel@tonic-gate 		return;
69927c478bd9Sstevel@tonic-gate 
69937c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
69947c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
69957c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
69967c478bd9Sstevel@tonic-gate 
699778dc6db2Sllai 	if (ct->ct_powercnt_reset || !ct->ct_powercnt_config) {
69984c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, child, "not configured\n"));
69997c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
70007c478bd9Sstevel@tonic-gate 		return;
70017c478bd9Sstevel@tonic-gate 	}
70027c478bd9Sstevel@tonic-gate 
70037c478bd9Sstevel@tonic-gate 	/* client has not been updated */
70047c478bd9Sstevel@tonic-gate 	if (MDI_CLIENT_IS_FAILED(ct)) {
70054c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, child, "client failed\n"));
70067c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
70077c478bd9Sstevel@tonic-gate 		return;
70087c478bd9Sstevel@tonic-gate 	}
70097c478bd9Sstevel@tonic-gate 
70107c478bd9Sstevel@tonic-gate 	/* another thread might have powered it down or detached it */
70117c478bd9Sstevel@tonic-gate 	if ((MDI_CLIENT_IS_POWERED_DOWN(ct) &&
70121b94a41bSChris Horne 	    !DEVI_IS_ATTACHING(child)) ||
70131b94a41bSChris Horne 	    (!i_ddi_devi_attached(child) &&
70141b94a41bSChris Horne 	    !DEVI_IS_ATTACHING(child))) {
70154c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_reset_client\n"));
70167c478bd9Sstevel@tonic-gate 		i_mdi_pm_reset_client(ct);
70177c478bd9Sstevel@tonic-gate 	} else {
701878dc6db2Sllai 		mdi_pathinfo_t  *pip, *next;
70197c478bd9Sstevel@tonic-gate 		int	valid_path_count = 0;
70207c478bd9Sstevel@tonic-gate 
70214c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_rele_client\n"));
70227c478bd9Sstevel@tonic-gate 		pip = ct->ct_path_head;
70237c478bd9Sstevel@tonic-gate 		while (pip != NULL) {
70247c478bd9Sstevel@tonic-gate 			MDI_PI_LOCK(pip);
70257c478bd9Sstevel@tonic-gate 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
702678dc6db2Sllai 			if (MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip))
70277c478bd9Sstevel@tonic-gate 				valid_path_count ++;
70287c478bd9Sstevel@tonic-gate 			MDI_PI_UNLOCK(pip);
70297c478bd9Sstevel@tonic-gate 			pip = next;
70307c478bd9Sstevel@tonic-gate 		}
70317c478bd9Sstevel@tonic-gate 		i_mdi_pm_rele_client(ct, valid_path_count);
70327c478bd9Sstevel@tonic-gate 	}
703378dc6db2Sllai 	ct->ct_powercnt_config = 0;
70347c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
70357c478bd9Sstevel@tonic-gate }
70367c478bd9Sstevel@tonic-gate 
70377c478bd9Sstevel@tonic-gate static void
i_mdi_pm_post_config(dev_info_t * vdip,dev_info_t * child)70385e3986cbScth i_mdi_pm_post_config(dev_info_t *vdip, dev_info_t *child)
70397c478bd9Sstevel@tonic-gate {
70407c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
70415e3986cbScth 
70425e3986cbScth 	ASSERT(MDI_VHCI(vdip));
70437c478bd9Sstevel@tonic-gate 
70447c478bd9Sstevel@tonic-gate 	/* ndi_devi_config_one */
70457c478bd9Sstevel@tonic-gate 	if (child) {
70465e3986cbScth 		ASSERT(DEVI_BUSY_OWNED(vdip));
70477c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_config_one(child);
70487c478bd9Sstevel@tonic-gate 		return;
70497c478bd9Sstevel@tonic-gate 	}
70507c478bd9Sstevel@tonic-gate 
70517c478bd9Sstevel@tonic-gate 	/* devi_config_common */
70523fe80ca4SDan Cross 	ndi_devi_enter(vdip);
70535e3986cbScth 	cdip = ddi_get_child(vdip);
70547c478bd9Sstevel@tonic-gate 	while (cdip) {
70557c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
70567c478bd9Sstevel@tonic-gate 
70577c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_config_one(cdip);
70587c478bd9Sstevel@tonic-gate 		cdip = next;
70597c478bd9Sstevel@tonic-gate 	}
70603fe80ca4SDan Cross 	ndi_devi_exit(vdip);
70617c478bd9Sstevel@tonic-gate }
70627c478bd9Sstevel@tonic-gate 
70637c478bd9Sstevel@tonic-gate static void
i_mdi_pm_post_unconfig_one(dev_info_t * child)70647c478bd9Sstevel@tonic-gate i_mdi_pm_post_unconfig_one(dev_info_t *child)
70657c478bd9Sstevel@tonic-gate {
70667c478bd9Sstevel@tonic-gate 	mdi_client_t	*ct;
70677c478bd9Sstevel@tonic-gate 
70687c478bd9Sstevel@tonic-gate 	ct = i_devi_get_client(child);
70697c478bd9Sstevel@tonic-gate 	if (ct == NULL)
70707c478bd9Sstevel@tonic-gate 		return;
70717c478bd9Sstevel@tonic-gate 
70727c478bd9Sstevel@tonic-gate 	MDI_CLIENT_LOCK(ct);
70737c478bd9Sstevel@tonic-gate 	while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
70747c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
70757c478bd9Sstevel@tonic-gate 
707678dc6db2Sllai 	if (!ct->ct_powercnt_unconfig || ct->ct_powercnt_reset) {
70774c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, child, "not held\n"));
70787c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
70797c478bd9Sstevel@tonic-gate 		return;
70807c478bd9Sstevel@tonic-gate 	}
70817c478bd9Sstevel@tonic-gate 
70827c478bd9Sstevel@tonic-gate 	/* failure detaching or another thread just attached it */
70837c478bd9Sstevel@tonic-gate 	if ((MDI_CLIENT_IS_POWERED_DOWN(ct) &&
70841b94a41bSChris Horne 	    i_ddi_devi_attached(child)) ||
70851b94a41bSChris Horne 	    (!i_ddi_devi_attached(child) &&
70861b94a41bSChris Horne 	    !DEVI_IS_ATTACHING(child))) {
70874c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_reset_client\n"));
70887c478bd9Sstevel@tonic-gate 		i_mdi_pm_reset_client(ct);
708978dc6db2Sllai 	} else {
709078dc6db2Sllai 		mdi_pathinfo_t  *pip, *next;
709178dc6db2Sllai 		int	valid_path_count = 0;
709278dc6db2Sllai 
70934c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_rele_client\n"));
709478dc6db2Sllai 		pip = ct->ct_path_head;
709578dc6db2Sllai 		while (pip != NULL) {
709678dc6db2Sllai 			MDI_PI_LOCK(pip);
709778dc6db2Sllai 			next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
709878dc6db2Sllai 			if (MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip))
709978dc6db2Sllai 				valid_path_count ++;
710078dc6db2Sllai 			MDI_PI_UNLOCK(pip);
710178dc6db2Sllai 			pip = next;
710278dc6db2Sllai 		}
710378dc6db2Sllai 		i_mdi_pm_rele_client(ct, valid_path_count);
710478dc6db2Sllai 		ct->ct_powercnt_unconfig = 0;
71057c478bd9Sstevel@tonic-gate 	}
71067c478bd9Sstevel@tonic-gate 
71077c478bd9Sstevel@tonic-gate 	MDI_CLIENT_UNLOCK(ct);
71087c478bd9Sstevel@tonic-gate }
71097c478bd9Sstevel@tonic-gate 
71107c478bd9Sstevel@tonic-gate static void
i_mdi_pm_post_unconfig(dev_info_t * vdip,dev_info_t * child,int held)71115e3986cbScth i_mdi_pm_post_unconfig(dev_info_t *vdip, dev_info_t *child, int held)
71127c478bd9Sstevel@tonic-gate {
71137c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
71147c478bd9Sstevel@tonic-gate 
71155e3986cbScth 	ASSERT(MDI_VHCI(vdip));
71167c478bd9Sstevel@tonic-gate 
71177c478bd9Sstevel@tonic-gate 	if (!held) {
71184c06356bSdh 		MDI_DEBUG(4, (MDI_NOTE, vdip, "held = %d", held));
71197c478bd9Sstevel@tonic-gate 		return;
71207c478bd9Sstevel@tonic-gate 	}
71217c478bd9Sstevel@tonic-gate 
71227c478bd9Sstevel@tonic-gate 	if (child) {
71235e3986cbScth 		ASSERT(DEVI_BUSY_OWNED(vdip));
71247c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_unconfig_one(child);
71257c478bd9Sstevel@tonic-gate 		return;
71267c478bd9Sstevel@tonic-gate 	}
71277c478bd9Sstevel@tonic-gate 
71283fe80ca4SDan Cross 	ndi_devi_enter(vdip);
71295e3986cbScth 	cdip = ddi_get_child(vdip);
71307c478bd9Sstevel@tonic-gate 	while (cdip) {
71317c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(cdip);
71327c478bd9Sstevel@tonic-gate 
71337c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_unconfig_one(cdip);
71347c478bd9Sstevel@tonic-gate 		cdip = next;
71357c478bd9Sstevel@tonic-gate 	}
71363fe80ca4SDan Cross 	ndi_devi_exit(vdip);
71377c478bd9Sstevel@tonic-gate }
71387c478bd9Sstevel@tonic-gate 
71397c478bd9Sstevel@tonic-gate int
mdi_power(dev_info_t * vdip,mdi_pm_op_t op,void * args,char * devnm,int flags)71407c478bd9Sstevel@tonic-gate mdi_power(dev_info_t *vdip, mdi_pm_op_t op, void *args, char *devnm, int flags)
71417c478bd9Sstevel@tonic-gate {
71423fe80ca4SDan Cross 	int			ret = MDI_SUCCESS;
71437c478bd9Sstevel@tonic-gate 	dev_info_t		*client_dip = NULL;
71447c478bd9Sstevel@tonic-gate 	mdi_client_t		*ct;
71457c478bd9Sstevel@tonic-gate 
71467c478bd9Sstevel@tonic-gate 	/*
71477c478bd9Sstevel@tonic-gate 	 * Handling ndi_devi_config_one and ndi_devi_unconfig_one.
71487c478bd9Sstevel@tonic-gate 	 * Power up pHCI for the named client device.
71497c478bd9Sstevel@tonic-gate 	 * Note: Before the client is enumerated under vhci by phci,
71507c478bd9Sstevel@tonic-gate 	 * client_dip can be NULL. Then proceed to power up all the
71517c478bd9Sstevel@tonic-gate 	 * pHCIs.
71527c478bd9Sstevel@tonic-gate 	 */
71537c478bd9Sstevel@tonic-gate 	if (devnm != NULL) {
71543fe80ca4SDan Cross 		ndi_devi_enter(vdip);
71557c478bd9Sstevel@tonic-gate 		client_dip = ndi_devi_findchild(vdip, devnm);
71567c478bd9Sstevel@tonic-gate 	}
71577c478bd9Sstevel@tonic-gate 
71584c06356bSdh 	MDI_DEBUG(4, (MDI_NOTE, vdip,
71594c06356bSdh 	    "op = %d %s %p", op, devnm ? devnm : "", (void *)client_dip));
71607c478bd9Sstevel@tonic-gate 
71617c478bd9Sstevel@tonic-gate 	switch (op) {
71627c478bd9Sstevel@tonic-gate 	case MDI_PM_PRE_CONFIG:
71637c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_config(vdip, client_dip);
7164c73a93f2Sdm 		break;
71655e3986cbScth 
71667c478bd9Sstevel@tonic-gate 	case MDI_PM_PRE_UNCONFIG:
71677c478bd9Sstevel@tonic-gate 		ret = i_mdi_pm_pre_unconfig(vdip, client_dip, (int *)args,
71687c478bd9Sstevel@tonic-gate 		    flags);
7169c73a93f2Sdm 		break;
71705e3986cbScth 
71717c478bd9Sstevel@tonic-gate 	case MDI_PM_POST_CONFIG:
71727c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_config(vdip, client_dip);
7173c73a93f2Sdm 		break;
71745e3986cbScth 
71757c478bd9Sstevel@tonic-gate 	case MDI_PM_POST_UNCONFIG:
71767c478bd9Sstevel@tonic-gate 		i_mdi_pm_post_unconfig(vdip, client_dip, *(int *)args);
7177c73a93f2Sdm 		break;
71785e3986cbScth 
71797c478bd9Sstevel@tonic-gate 	case MDI_PM_HOLD_POWER:
71807c478bd9Sstevel@tonic-gate 	case MDI_PM_RELE_POWER:
71817c478bd9Sstevel@tonic-gate 		ASSERT(args);
71827c478bd9Sstevel@tonic-gate 
71837c478bd9Sstevel@tonic-gate 		client_dip = (dev_info_t *)args;
71847c478bd9Sstevel@tonic-gate 		ASSERT(MDI_CLIENT(client_dip));
71857c478bd9Sstevel@tonic-gate 
71867c478bd9Sstevel@tonic-gate 		ct = i_devi_get_client(client_dip);
71877c478bd9Sstevel@tonic-gate 		MDI_CLIENT_LOCK(ct);
71887c478bd9Sstevel@tonic-gate 
71897c478bd9Sstevel@tonic-gate 		if (op == MDI_PM_HOLD_POWER) {
71907c478bd9Sstevel@tonic-gate 			if (ct->ct_power_cnt == 0) {
71917c478bd9Sstevel@tonic-gate 				(void) i_mdi_power_all_phci(ct);
71924c06356bSdh 				MDI_DEBUG(4, (MDI_NOTE, client_dip,
71934c06356bSdh 				    "i_mdi_pm_hold_client\n"));
71947c478bd9Sstevel@tonic-gate 				i_mdi_pm_hold_client(ct, ct->ct_path_count);
71957c478bd9Sstevel@tonic-gate 			}
71967c478bd9Sstevel@tonic-gate 		} else {
71971b94a41bSChris Horne 			if (DEVI_IS_ATTACHING(client_dip)) {
71984c06356bSdh 				MDI_DEBUG(4, (MDI_NOTE, client_dip,
71994c06356bSdh 				    "i_mdi_pm_rele_client\n"));
72007c478bd9Sstevel@tonic-gate 				i_mdi_pm_rele_client(ct, ct->ct_path_count);
72017c478bd9Sstevel@tonic-gate 			} else {
72024c06356bSdh 				MDI_DEBUG(4, (MDI_NOTE, client_dip,
72034c06356bSdh 				    "i_mdi_pm_reset_client\n"));
72047c478bd9Sstevel@tonic-gate 				i_mdi_pm_reset_client(ct);
72057c478bd9Sstevel@tonic-gate 			}
72067c478bd9Sstevel@tonic-gate 		}
72077c478bd9Sstevel@tonic-gate 
72087c478bd9Sstevel@tonic-gate 		MDI_CLIENT_UNLOCK(ct);
72097c478bd9Sstevel@tonic-gate 		break;
72105e3986cbScth 
72117c478bd9Sstevel@tonic-gate 	default:
72127c478bd9Sstevel@tonic-gate 		break;
72137c478bd9Sstevel@tonic-gate 	}
72147c478bd9Sstevel@tonic-gate 
72155e3986cbScth 	if (devnm)
72163fe80ca4SDan Cross 		ndi_devi_exit(vdip);
72175e3986cbScth 
72187c478bd9Sstevel@tonic-gate 	return (ret);
72197c478bd9Sstevel@tonic-gate }
72207c478bd9Sstevel@tonic-gate 
72217c478bd9Sstevel@tonic-gate int
mdi_component_is_vhci(dev_info_t * dip,const char ** mdi_class)72227c478bd9Sstevel@tonic-gate mdi_component_is_vhci(dev_info_t *dip, const char **mdi_class)
72237c478bd9Sstevel@tonic-gate {
72247c478bd9Sstevel@tonic-gate 	mdi_vhci_t *vhci;
72257c478bd9Sstevel@tonic-gate 
72267c478bd9Sstevel@tonic-gate 	if (!MDI_VHCI(dip))
72277c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
72287c478bd9Sstevel@tonic-gate 
72297c478bd9Sstevel@tonic-gate 	if (mdi_class) {
72307c478bd9Sstevel@tonic-gate 		vhci = DEVI(dip)->devi_mdi_xhci;
72317c478bd9Sstevel@tonic-gate 		ASSERT(vhci);
72327c478bd9Sstevel@tonic-gate 		*mdi_class = vhci->vh_class;
72337c478bd9Sstevel@tonic-gate 	}
72347c478bd9Sstevel@tonic-gate 
72357c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
72367c478bd9Sstevel@tonic-gate }
72377c478bd9Sstevel@tonic-gate 
72387c478bd9Sstevel@tonic-gate int
mdi_component_is_phci(dev_info_t * dip,const char ** mdi_class)72397c478bd9Sstevel@tonic-gate mdi_component_is_phci(dev_info_t *dip, const char **mdi_class)
72407c478bd9Sstevel@tonic-gate {
72417c478bd9Sstevel@tonic-gate 	mdi_phci_t *phci;
72427c478bd9Sstevel@tonic-gate 
72437c478bd9Sstevel@tonic-gate 	if (!MDI_PHCI(dip))
72447c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
72457c478bd9Sstevel@tonic-gate 
72467c478bd9Sstevel@tonic-gate 	if (mdi_class) {
72477c478bd9Sstevel@tonic-gate 		phci = DEVI(dip)->devi_mdi_xhci;
72487c478bd9Sstevel@tonic-gate 		ASSERT(phci);
72497c478bd9Sstevel@tonic-gate 		*mdi_class = phci->ph_vhci->vh_class;
72507c478bd9Sstevel@tonic-gate 	}
72517c478bd9Sstevel@tonic-gate 
72527c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
72537c478bd9Sstevel@tonic-gate }
72547c478bd9Sstevel@tonic-gate 
72557c478bd9Sstevel@tonic-gate int
mdi_component_is_client(dev_info_t * dip,const char ** mdi_class)72567c478bd9Sstevel@tonic-gate mdi_component_is_client(dev_info_t *dip, const char **mdi_class)
72577c478bd9Sstevel@tonic-gate {
72587c478bd9Sstevel@tonic-gate 	mdi_client_t *client;
72597c478bd9Sstevel@tonic-gate 
72607c478bd9Sstevel@tonic-gate 	if (!MDI_CLIENT(dip))
72617c478bd9Sstevel@tonic-gate 		return (MDI_FAILURE);
72627c478bd9Sstevel@tonic-gate 
72637c478bd9Sstevel@tonic-gate 	if (mdi_class) {
72647c478bd9Sstevel@tonic-gate 		client = DEVI(dip)->devi_mdi_client;
72657c478bd9Sstevel@tonic-gate 		ASSERT(client);
72667c478bd9Sstevel@tonic-gate 		*mdi_class = client->ct_vhci->vh_class;
72677c478bd9Sstevel@tonic-gate 	}
72687c478bd9Sstevel@tonic-gate 
72697c478bd9Sstevel@tonic-gate 	return (MDI_SUCCESS);
72707c478bd9Sstevel@tonic-gate }
72717c478bd9Sstevel@tonic-gate 
72723c34adc5Sramat void *
mdi_client_get_vhci_private(dev_info_t * dip)72733c34adc5Sramat mdi_client_get_vhci_private(dev_info_t *dip)
72743c34adc5Sramat {
72753c34adc5Sramat 	ASSERT(mdi_component_is_client(dip, NULL) == MDI_SUCCESS);
72763c34adc5Sramat 	if (mdi_component_is_client(dip, NULL) == MDI_SUCCESS) {
72773c34adc5Sramat 		mdi_client_t	*ct;
72783c34adc5Sramat 		ct = i_devi_get_client(dip);
72793c34adc5Sramat 		return (ct->ct_vprivate);
72803c34adc5Sramat 	}
72813c34adc5Sramat 	return (NULL);
72823c34adc5Sramat }
72833c34adc5Sramat 
72843c34adc5Sramat void
mdi_client_set_vhci_private(dev_info_t * dip,void * data)72853c34adc5Sramat mdi_client_set_vhci_private(dev_info_t *dip, void *data)
72863c34adc5Sramat {
72873c34adc5Sramat 	ASSERT(mdi_component_is_client(dip, NULL) == MDI_SUCCESS);
72883c34adc5Sramat 	if (mdi_component_is_client(dip, NULL) == MDI_SUCCESS) {
72893c34adc5Sramat 		mdi_client_t	*ct;
72903c34adc5Sramat 		ct = i_devi_get_client(dip);
72913c34adc5Sramat 		ct->ct_vprivate = data;
72923c34adc5Sramat 	}
72933c34adc5Sramat }
72947c478bd9Sstevel@tonic-gate /*
72953c34adc5Sramat  * mdi_pi_get_vhci_private():
72963c34adc5Sramat  *		Get the vhci private information associated with the
72973c34adc5Sramat  *		mdi_pathinfo node
72987c478bd9Sstevel@tonic-gate  */
72993c34adc5Sramat void *
mdi_pi_get_vhci_private(mdi_pathinfo_t * pip)73003c34adc5Sramat mdi_pi_get_vhci_private(mdi_pathinfo_t *pip)
73017c478bd9Sstevel@tonic-gate {
73023c34adc5Sramat 	caddr_t	vprivate = NULL;
73033c34adc5Sramat 	if (pip) {
73043c34adc5Sramat 		vprivate = MDI_PI(pip)->pi_vprivate;
73057c478bd9Sstevel@tonic-gate 	}
73063c34adc5Sramat 	return (vprivate);
73077c478bd9Sstevel@tonic-gate }
73087c478bd9Sstevel@tonic-gate 
73093c34adc5Sramat /*
73103c34adc5Sramat  * mdi_pi_set_vhci_private():
73113c34adc5Sramat  *		Set the vhci private information in the mdi_pathinfo node
73123c34adc5Sramat  */
73133c34adc5Sramat void
mdi_pi_set_vhci_private(mdi_pathinfo_t * pip,void * priv)73143c34adc5Sramat mdi_pi_set_vhci_private(mdi_pathinfo_t *pip, void *priv)
73157c478bd9Sstevel@tonic-gate {
73163c34adc5Sramat 	if (pip) {
73173c34adc5Sramat 		MDI_PI(pip)->pi_vprivate = priv;
73183c34adc5Sramat 	}
73193c34adc5Sramat }
73207c478bd9Sstevel@tonic-gate 
73213c34adc5Sramat /*
73223c34adc5Sramat  * mdi_phci_get_vhci_private():
73233c34adc5Sramat  *		Get the vhci private information associated with the
73243c34adc5Sramat  *		mdi_phci node
73253c34adc5Sramat  */
73263c34adc5Sramat void *
mdi_phci_get_vhci_private(dev_info_t * dip)73273c34adc5Sramat mdi_phci_get_vhci_private(dev_info_t *dip)
73283c34adc5Sramat {
73293c34adc5Sramat 	ASSERT(mdi_component_is_phci(dip, NULL) == MDI_SUCCESS);
73303c34adc5Sramat 	if (mdi_component_is_phci(dip, NULL) == MDI_SUCCESS) {
73313c34adc5Sramat 		mdi_phci_t	*ph;
73323c34adc5Sramat 		ph = i_devi_get_phci(dip);
73333c34adc5Sramat 		return (ph->ph_vprivate);
73347c478bd9Sstevel@tonic-gate 	}
73353c34adc5Sramat 	return (NULL);
73367c478bd9Sstevel@tonic-gate }
73377c478bd9Sstevel@tonic-gate 
73387c478bd9Sstevel@tonic-gate /*
73393c34adc5Sramat  * mdi_phci_set_vhci_private():
73403c34adc5Sramat  *		Set the vhci private information in the mdi_phci node
73417c478bd9Sstevel@tonic-gate  */
73423c34adc5Sramat void
mdi_phci_set_vhci_private(dev_info_t * dip,void * priv)73433c34adc5Sramat mdi_phci_set_vhci_private(dev_info_t *dip, void *priv)
73447c478bd9Sstevel@tonic-gate {
73453c34adc5Sramat 	ASSERT(mdi_component_is_phci(dip, NULL) == MDI_SUCCESS);
73463c34adc5Sramat 	if (mdi_component_is_phci(dip, NULL) == MDI_SUCCESS) {
73473c34adc5Sramat 		mdi_phci_t	*ph;
73483c34adc5Sramat 		ph = i_devi_get_phci(dip);
73493c34adc5Sramat 		ph->ph_vprivate = priv;
73503c34adc5Sramat 	}
73513c34adc5Sramat }
73527c478bd9Sstevel@tonic-gate 
73534c06356bSdh int
mdi_pi_ishidden(mdi_pathinfo_t * pip)73544c06356bSdh mdi_pi_ishidden(mdi_pathinfo_t *pip)
73554c06356bSdh {
73564c06356bSdh 	return (MDI_PI_FLAGS_IS_HIDDEN(pip));
73574c06356bSdh }
73584c06356bSdh 
73594c06356bSdh int
mdi_pi_device_isremoved(mdi_pathinfo_t * pip)73604c06356bSdh mdi_pi_device_isremoved(mdi_pathinfo_t *pip)
73614c06356bSdh {
73624c06356bSdh 	return (MDI_PI_FLAGS_IS_DEVICE_REMOVED(pip));
73634c06356bSdh }
73644c06356bSdh 
7365328d222bSChris Horne /* Return 1 if all client paths are device_removed */
7366328d222bSChris Horne static int
i_mdi_client_all_devices_removed(mdi_client_t * ct)7367328d222bSChris Horne i_mdi_client_all_devices_removed(mdi_client_t *ct)
7368328d222bSChris Horne {
7369328d222bSChris Horne 	mdi_pathinfo_t  *pip;
7370328d222bSChris Horne 	int		all_devices_removed = 1;
7371328d222bSChris Horne 
7372328d222bSChris Horne 	MDI_CLIENT_LOCK(ct);
7373328d222bSChris Horne 	for (pip = ct->ct_path_head; pip;
7374328d222bSChris Horne 	    pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link) {
7375328d222bSChris Horne 		if (!mdi_pi_device_isremoved(pip)) {
7376328d222bSChris Horne 			all_devices_removed = 0;
7377328d222bSChris Horne 			break;
7378328d222bSChris Horne 		}
7379328d222bSChris Horne 	}
7380328d222bSChris Horne 	MDI_CLIENT_UNLOCK(ct);
7381328d222bSChris Horne 	return (all_devices_removed);
7382328d222bSChris Horne }
7383328d222bSChris Horne 
73844c06356bSdh /*
7385328d222bSChris Horne  * When processing path hotunplug, represent device removal.
73864c06356bSdh  */
73874c06356bSdh int
mdi_pi_device_remove(mdi_pathinfo_t * pip)73884c06356bSdh mdi_pi_device_remove(mdi_pathinfo_t *pip)
73894c06356bSdh {
7390328d222bSChris Horne 	mdi_client_t	*ct;
7391328d222bSChris Horne 
73924c06356bSdh 	MDI_PI_LOCK(pip);
73934c06356bSdh 	if (mdi_pi_device_isremoved(pip)) {
73944c06356bSdh 		MDI_PI_UNLOCK(pip);
73954c06356bSdh 		return (0);
73964c06356bSdh 	}
73974c06356bSdh 	MDI_PI_FLAGS_SET_DEVICE_REMOVED(pip);
73984c06356bSdh 	MDI_PI_FLAGS_SET_HIDDEN(pip);
73994c06356bSdh 	MDI_PI_UNLOCK(pip);
74004c06356bSdh 
7401328d222bSChris Horne 	/*
7402328d222bSChris Horne 	 * If all paths associated with the client are now DEVICE_REMOVED,
7403328d222bSChris Horne 	 * reflect DEVICE_REMOVED in the client.
7404328d222bSChris Horne 	 */
7405328d222bSChris Horne 	ct = MDI_PI(pip)->pi_client;
7406328d222bSChris Horne 	if (ct && ct->ct_dip && i_mdi_client_all_devices_removed(ct))
7407328d222bSChris Horne 		(void) ndi_devi_device_remove(ct->ct_dip);
7408328d222bSChris Horne 	else
7409328d222bSChris Horne 		i_ddi_di_cache_invalidate();
74104c06356bSdh 
74114c06356bSdh 	return (1);
74124c06356bSdh }
74134c06356bSdh 
74144c06356bSdh /*
74154c06356bSdh  * When processing hotplug, if a path marked mdi_pi_device_isremoved()
74164c06356bSdh  * is now accessible then this interfaces is used to represent device insertion.
74174c06356bSdh  */
74184c06356bSdh int
mdi_pi_device_insert(mdi_pathinfo_t * pip)74194c06356bSdh mdi_pi_device_insert(mdi_pathinfo_t *pip)
74204c06356bSdh {
74214c06356bSdh 	MDI_PI_LOCK(pip);
74224c06356bSdh 	if (!mdi_pi_device_isremoved(pip)) {
74234c06356bSdh 		MDI_PI_UNLOCK(pip);
74244c06356bSdh 		return (0);
74254c06356bSdh 	}
74264c06356bSdh 	MDI_PI_FLAGS_CLR_DEVICE_REMOVED(pip);
74274c06356bSdh 	MDI_PI_FLAGS_CLR_HIDDEN(pip);
74284c06356bSdh 	MDI_PI_UNLOCK(pip);
74294c06356bSdh 
74304c06356bSdh 	i_ddi_di_cache_invalidate();
74314c06356bSdh 
74324c06356bSdh 	return (1);
74334c06356bSdh }
74344c06356bSdh 
74353c34adc5Sramat /*
74363c34adc5Sramat  * List of vhci class names:
74373c34adc5Sramat  * A vhci class name must be in this list only if the corresponding vhci
74383c34adc5Sramat  * driver intends to use the mdi provided bus config implementation
74393c34adc5Sramat  * (i.e., mdi_vhci_bus_config()).
74403c34adc5Sramat  */
74413c34adc5Sramat static char *vhci_class_list[] = { MDI_HCI_CLASS_SCSI, MDI_HCI_CLASS_IB };
74423c34adc5Sramat #define	N_VHCI_CLASSES	(sizeof (vhci_class_list) / sizeof (char *))
74437c478bd9Sstevel@tonic-gate 
74443c34adc5Sramat /*
74453c34adc5Sramat  * During boot time, the on-disk vhci cache for every vhci class is read
74463c34adc5Sramat  * in the form of an nvlist and stored here.
74473c34adc5Sramat  */
74483c34adc5Sramat static nvlist_t *vhcache_nvl[N_VHCI_CLASSES];
74497c478bd9Sstevel@tonic-gate 
74503c34adc5Sramat /* nvpair names in vhci cache nvlist */
74513c34adc5Sramat #define	MDI_VHCI_CACHE_VERSION	1
74523c34adc5Sramat #define	MDI_NVPNAME_VERSION	"version"
74533c34adc5Sramat #define	MDI_NVPNAME_PHCIS	"phcis"
74543c34adc5Sramat #define	MDI_NVPNAME_CTADDRMAP	"clientaddrmap"
74557c478bd9Sstevel@tonic-gate 
74563c34adc5Sramat /*
74573c34adc5Sramat  * Given vhci class name, return its on-disk vhci cache filename.
74583c34adc5Sramat  * Memory for the returned filename which includes the full path is allocated
74593c34adc5Sramat  * by this function.
74603c34adc5Sramat  */
74613c34adc5Sramat static char *
vhclass2vhcache_filename(char * vhclass)74623c34adc5Sramat vhclass2vhcache_filename(char *vhclass)
74633c34adc5Sramat {
74643c34adc5Sramat 	char *filename;
74653c34adc5Sramat 	int len;
74663c34adc5Sramat 	static char *fmt = "/etc/devices/mdi_%s_cache";
74677c478bd9Sstevel@tonic-gate 
74687c478bd9Sstevel@tonic-gate 	/*
74693c34adc5Sramat 	 * fmt contains the on-disk vhci cache file name format;
74703c34adc5Sramat 	 * for scsi_vhci the filename is "/etc/devices/mdi_scsi_vhci_cache".
74717c478bd9Sstevel@tonic-gate 	 */
74727c478bd9Sstevel@tonic-gate 
74733c34adc5Sramat 	/* the -1 below is to account for "%s" in the format string */
74743c34adc5Sramat 	len = strlen(fmt) + strlen(vhclass) - 1;
74753c34adc5Sramat 	filename = kmem_alloc(len, KM_SLEEP);
74763c34adc5Sramat 	(void) snprintf(filename, len, fmt, vhclass);
74773c34adc5Sramat 	ASSERT(len == (strlen(filename) + 1));
74783c34adc5Sramat 	return (filename);
74793c34adc5Sramat }
74807c478bd9Sstevel@tonic-gate 
74813c34adc5Sramat /*
74823c34adc5Sramat  * initialize the vhci cache related data structures and read the on-disk
74833c34adc5Sramat  * vhci cached data into memory.
74843c34adc5Sramat  */
74853c34adc5Sramat static void
setup_vhci_cache(mdi_vhci_t * vh)74863c34adc5Sramat setup_vhci_cache(mdi_vhci_t *vh)
74873c34adc5Sramat {
74883c34adc5Sramat 	mdi_vhci_config_t *vhc;
74893c34adc5Sramat 	mdi_vhci_cache_t *vhcache;
74903c34adc5Sramat 	int i;
74913c34adc5Sramat 	nvlist_t *nvl = NULL;
74927c478bd9Sstevel@tonic-gate 
74933c34adc5Sramat 	vhc = kmem_zalloc(sizeof (mdi_vhci_config_t), KM_SLEEP);
74943c34adc5Sramat 	vh->vh_config = vhc;
74953c34adc5Sramat 	vhcache = &vhc->vhc_vhcache;
74967c478bd9Sstevel@tonic-gate 
74973c34adc5Sramat 	vhc->vhc_vhcache_filename = vhclass2vhcache_filename(vh->vh_class);
74987c478bd9Sstevel@tonic-gate 
74993c34adc5Sramat 	mutex_init(&vhc->vhc_lock, NULL, MUTEX_DEFAULT, NULL);
75003c34adc5Sramat 	cv_init(&vhc->vhc_cv, NULL, CV_DRIVER, NULL);
75017c478bd9Sstevel@tonic-gate 
75023c34adc5Sramat 	rw_init(&vhcache->vhcache_lock, NULL, RW_DRIVER, NULL);
75037c478bd9Sstevel@tonic-gate 
75043c34adc5Sramat 	/*
75053c34adc5Sramat 	 * Create string hash; same as mod_hash_create_strhash() except that
75063c34adc5Sramat 	 * we use NULL key destructor.
75073c34adc5Sramat 	 */
75083c34adc5Sramat 	vhcache->vhcache_client_hash = mod_hash_create_extended(vh->vh_class,
75093c34adc5Sramat 	    mdi_bus_config_cache_hash_size,
75103c34adc5Sramat 	    mod_hash_null_keydtor, mod_hash_null_valdtor,
75113c34adc5Sramat 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
75127c478bd9Sstevel@tonic-gate 
75137c478bd9Sstevel@tonic-gate 	/*
75143c34adc5Sramat 	 * The on-disk vhci cache is read during booting prior to the
75153c34adc5Sramat 	 * lights-out period by mdi_read_devices_files().
75167c478bd9Sstevel@tonic-gate 	 */
75173c34adc5Sramat 	for (i = 0; i < N_VHCI_CLASSES; i++) {
75183c34adc5Sramat 		if (strcmp(vhci_class_list[i], vh->vh_class) == 0) {
75193c34adc5Sramat 			nvl = vhcache_nvl[i];
75203c34adc5Sramat 			vhcache_nvl[i] = NULL;
75213c34adc5Sramat 			break;
75227c478bd9Sstevel@tonic-gate 		}
75237c478bd9Sstevel@tonic-gate 	}
75247c478bd9Sstevel@tonic-gate 
75253c34adc5Sramat 	/*
75263c34adc5Sramat 	 * this is to cover the case of some one manually causing unloading
75273c34adc5Sramat 	 * (or detaching) and reloading (or attaching) of a vhci driver.
75283c34adc5Sramat 	 */
75293c34adc5Sramat 	if (nvl == NULL && modrootloaded)
75303c34adc5Sramat 		nvl = read_on_disk_vhci_cache(vh->vh_class);
75313c34adc5Sramat 
75323c34adc5Sramat 	if (nvl != NULL) {
75333c34adc5Sramat 		rw_enter(&vhcache->vhcache_lock, RW_WRITER);
75343c34adc5Sramat 		if (mainnvl_to_vhcache(vhcache, nvl) == MDI_SUCCESS)
75353c34adc5Sramat 			vhcache->vhcache_flags |= MDI_VHCI_CACHE_SETUP_DONE;
75363c34adc5Sramat 		else  {
75373c34adc5Sramat 			cmn_err(CE_WARN,
75384c06356bSdh 			    "%s: data file corrupted, will recreate",
75393c34adc5Sramat 			    vhc->vhc_vhcache_filename);
75407c478bd9Sstevel@tonic-gate 		}
75413c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
75423c34adc5Sramat 		nvlist_free(nvl);
75437c478bd9Sstevel@tonic-gate 	}
75447c478bd9Sstevel@tonic-gate 
75453c34adc5Sramat 	vhc->vhc_cbid = callb_add(stop_vhcache_flush_thread, vhc,
75463c34adc5Sramat 	    CB_CL_UADMIN_PRE_VFS, "mdi_vhcache_flush");
754767e56d35Sramat 
754867e56d35Sramat 	vhc->vhc_path_discovery_boot = mdi_path_discovery_boot;
754967e56d35Sramat 	vhc->vhc_path_discovery_postboot = mdi_path_discovery_postboot;
75503c34adc5Sramat }
75517c478bd9Sstevel@tonic-gate 
75523c34adc5Sramat /*
75533c34adc5Sramat  * free all vhci cache related resources
75543c34adc5Sramat  */
75553c34adc5Sramat static int
destroy_vhci_cache(mdi_vhci_t * vh)75563c34adc5Sramat destroy_vhci_cache(mdi_vhci_t *vh)
75573c34adc5Sramat {
75583c34adc5Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
75593c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
75603c34adc5Sramat 	mdi_vhcache_phci_t *cphci, *cphci_next;
75613c34adc5Sramat 	mdi_vhcache_client_t *cct, *cct_next;
75623c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi, *cpi_next;
75637c478bd9Sstevel@tonic-gate 
75643c34adc5Sramat 	if (stop_vhcache_async_threads(vhc) != MDI_SUCCESS)
75653c34adc5Sramat 		return (MDI_FAILURE);
75667c478bd9Sstevel@tonic-gate 
75673c34adc5Sramat 	kmem_free(vhc->vhc_vhcache_filename,
75683c34adc5Sramat 	    strlen(vhc->vhc_vhcache_filename) + 1);
75697c478bd9Sstevel@tonic-gate 
75703c34adc5Sramat 	mod_hash_destroy_strhash(vhcache->vhcache_client_hash);
75717c478bd9Sstevel@tonic-gate 
75723c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
75733c34adc5Sramat 	    cphci = cphci_next) {
75743c34adc5Sramat 		cphci_next = cphci->cphci_next;
75753c34adc5Sramat 		free_vhcache_phci(cphci);
75767c478bd9Sstevel@tonic-gate 	}
75777c478bd9Sstevel@tonic-gate 
75783c34adc5Sramat 	for (cct = vhcache->vhcache_client_head; cct != NULL; cct = cct_next) {
75793c34adc5Sramat 		cct_next = cct->cct_next;
75803c34adc5Sramat 		for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi_next) {
75813c34adc5Sramat 			cpi_next = cpi->cpi_next;
75823c34adc5Sramat 			free_vhcache_pathinfo(cpi);
75833c34adc5Sramat 		}
75843c34adc5Sramat 		free_vhcache_client(cct);
75857c478bd9Sstevel@tonic-gate 	}
75867c478bd9Sstevel@tonic-gate 
75873c34adc5Sramat 	rw_destroy(&vhcache->vhcache_lock);
75887c478bd9Sstevel@tonic-gate 
75893c34adc5Sramat 	mutex_destroy(&vhc->vhc_lock);
75903c34adc5Sramat 	cv_destroy(&vhc->vhc_cv);
75913c34adc5Sramat 	kmem_free(vhc, sizeof (mdi_vhci_config_t));
75923c34adc5Sramat 	return (MDI_SUCCESS);
75937c478bd9Sstevel@tonic-gate }
75947c478bd9Sstevel@tonic-gate 
75953c34adc5Sramat /*
75963c34adc5Sramat  * Stop all vhci cache related async threads and free their resources.
75973c34adc5Sramat  */
75983c34adc5Sramat static int
stop_vhcache_async_threads(mdi_vhci_config_t * vhc)75993c34adc5Sramat stop_vhcache_async_threads(mdi_vhci_config_t *vhc)
76003c34adc5Sramat {
76013c34adc5Sramat 	mdi_async_client_config_t *acc, *acc_next;
76023c34adc5Sramat 
76033c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
76043c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_EXIT;
76053c34adc5Sramat 	ASSERT(vhc->vhc_acc_thrcount >= 0);
76063c34adc5Sramat 	cv_broadcast(&vhc->vhc_cv);
76073c34adc5Sramat 
76083c34adc5Sramat 	while ((vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) ||
76093c34adc5Sramat 	    vhc->vhc_acc_thrcount != 0) {
76103c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
761196c4a178SChris Horne 		delay_random(mdi_delay);
76123c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
76133c34adc5Sramat 	}
76143c34adc5Sramat 
76153c34adc5Sramat 	vhc->vhc_flags &= ~MDI_VHC_EXIT;
76163c34adc5Sramat 
76173c34adc5Sramat 	for (acc = vhc->vhc_acc_list_head; acc != NULL; acc = acc_next) {
76183c34adc5Sramat 		acc_next = acc->acc_next;
76193c34adc5Sramat 		free_async_client_config(acc);
76203c34adc5Sramat 	}
76213c34adc5Sramat 	vhc->vhc_acc_list_head = NULL;
76223c34adc5Sramat 	vhc->vhc_acc_list_tail = NULL;
76233c34adc5Sramat 	vhc->vhc_acc_count = 0;
76243c34adc5Sramat 
76253c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) {
76263c34adc5Sramat 		vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
76273c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
76283c34adc5Sramat 		if (flush_vhcache(vhc, 0) != MDI_SUCCESS) {
76293c34adc5Sramat 			vhcache_dirty(vhc);
76303c34adc5Sramat 			return (MDI_FAILURE);
76313c34adc5Sramat 		}
76323c34adc5Sramat 	} else
76333c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
76343c34adc5Sramat 
76353c34adc5Sramat 	if (callb_delete(vhc->vhc_cbid) != 0)
76363c34adc5Sramat 		return (MDI_FAILURE);
76373c34adc5Sramat 
76383c34adc5Sramat 	return (MDI_SUCCESS);
76393c34adc5Sramat }
76403c34adc5Sramat 
76413c34adc5Sramat /*
76423c34adc5Sramat  * Stop vhci cache flush thread
76433c34adc5Sramat  */
76443c34adc5Sramat /* ARGSUSED */
76453c34adc5Sramat static boolean_t
stop_vhcache_flush_thread(void * arg,int code)76463c34adc5Sramat stop_vhcache_flush_thread(void *arg, int code)
76473c34adc5Sramat {
76483c34adc5Sramat 	mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
76493c34adc5Sramat 
76503c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
76513c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_EXIT;
76523c34adc5Sramat 	cv_broadcast(&vhc->vhc_cv);
76533c34adc5Sramat 
76543c34adc5Sramat 	while (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) {
76553c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
765696c4a178SChris Horne 		delay_random(mdi_delay);
76573c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
76583c34adc5Sramat 	}
76593c34adc5Sramat 
76603c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) {
76613c34adc5Sramat 		vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
76623c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
76633c34adc5Sramat 		(void) flush_vhcache(vhc, 1);
76643c34adc5Sramat 	} else
76653c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
76663c34adc5Sramat 
76673c34adc5Sramat 	return (B_TRUE);
76683c34adc5Sramat }
76693c34adc5Sramat 
76703c34adc5Sramat /*
76713c34adc5Sramat  * Enqueue the vhcache phci (cphci) at the tail of the list
76723c34adc5Sramat  */
76733c34adc5Sramat static void
enqueue_vhcache_phci(mdi_vhci_cache_t * vhcache,mdi_vhcache_phci_t * cphci)76743c34adc5Sramat enqueue_vhcache_phci(mdi_vhci_cache_t *vhcache, mdi_vhcache_phci_t *cphci)
76753c34adc5Sramat {
76763c34adc5Sramat 	cphci->cphci_next = NULL;
76773c34adc5Sramat 	if (vhcache->vhcache_phci_head == NULL)
76783c34adc5Sramat 		vhcache->vhcache_phci_head = cphci;
76793c34adc5Sramat 	else
76803c34adc5Sramat 		vhcache->vhcache_phci_tail->cphci_next = cphci;
76813c34adc5Sramat 	vhcache->vhcache_phci_tail = cphci;
76823c34adc5Sramat }
76833c34adc5Sramat 
76843c34adc5Sramat /*
76853c34adc5Sramat  * Enqueue the vhcache pathinfo (cpi) at the tail of the list
76863c34adc5Sramat  */
76873c34adc5Sramat static void
enqueue_tail_vhcache_pathinfo(mdi_vhcache_client_t * cct,mdi_vhcache_pathinfo_t * cpi)76883c34adc5Sramat enqueue_tail_vhcache_pathinfo(mdi_vhcache_client_t *cct,
76893c34adc5Sramat     mdi_vhcache_pathinfo_t *cpi)
76903c34adc5Sramat {
76913c34adc5Sramat 	cpi->cpi_next = NULL;
76923c34adc5Sramat 	if (cct->cct_cpi_head == NULL)
76933c34adc5Sramat 		cct->cct_cpi_head = cpi;
76943c34adc5Sramat 	else
76953c34adc5Sramat 		cct->cct_cpi_tail->cpi_next = cpi;
76963c34adc5Sramat 	cct->cct_cpi_tail = cpi;
76973c34adc5Sramat }
76983c34adc5Sramat 
76993c34adc5Sramat /*
77003c34adc5Sramat  * Enqueue the vhcache pathinfo (cpi) at the correct location in the
77013c34adc5Sramat  * ordered list. All cpis which do not have MDI_CPI_HINT_PATH_DOES_NOT_EXIST
77023c34adc5Sramat  * flag set come at the beginning of the list. All cpis which have this
77033c34adc5Sramat  * flag set come at the end of the list.
77043c34adc5Sramat  */
77053c34adc5Sramat static void
enqueue_vhcache_pathinfo(mdi_vhcache_client_t * cct,mdi_vhcache_pathinfo_t * newcpi)77063c34adc5Sramat enqueue_vhcache_pathinfo(mdi_vhcache_client_t *cct,
77073c34adc5Sramat     mdi_vhcache_pathinfo_t *newcpi)
77083c34adc5Sramat {
77093c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi, *prev_cpi;
77103c34adc5Sramat 
77113c34adc5Sramat 	if (cct->cct_cpi_head == NULL ||
77123c34adc5Sramat 	    (newcpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST))
77133c34adc5Sramat 		enqueue_tail_vhcache_pathinfo(cct, newcpi);
77143c34adc5Sramat 	else {
77153c34adc5Sramat 		for (cpi = cct->cct_cpi_head, prev_cpi = NULL; cpi != NULL &&
77163c34adc5Sramat 		    !(cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST);
77173c34adc5Sramat 		    prev_cpi = cpi, cpi = cpi->cpi_next)
77183c34adc5Sramat 			;
77193c34adc5Sramat 
77203c34adc5Sramat 		if (prev_cpi == NULL)
77213c34adc5Sramat 			cct->cct_cpi_head = newcpi;
77223c34adc5Sramat 		else
77233c34adc5Sramat 			prev_cpi->cpi_next = newcpi;
77243c34adc5Sramat 
77253c34adc5Sramat 		newcpi->cpi_next = cpi;
77263c34adc5Sramat 
77273c34adc5Sramat 		if (cpi == NULL)
77283c34adc5Sramat 			cct->cct_cpi_tail = newcpi;
77293c34adc5Sramat 	}
77303c34adc5Sramat }
77313c34adc5Sramat 
77323c34adc5Sramat /*
77333c34adc5Sramat  * Enqueue the vhcache client (cct) at the tail of the list
77343c34adc5Sramat  */
77353c34adc5Sramat static void
enqueue_vhcache_client(mdi_vhci_cache_t * vhcache,mdi_vhcache_client_t * cct)77363c34adc5Sramat enqueue_vhcache_client(mdi_vhci_cache_t *vhcache,
77373c34adc5Sramat     mdi_vhcache_client_t *cct)
77383c34adc5Sramat {
77393c34adc5Sramat 	cct->cct_next = NULL;
77403c34adc5Sramat 	if (vhcache->vhcache_client_head == NULL)
77413c34adc5Sramat 		vhcache->vhcache_client_head = cct;
77423c34adc5Sramat 	else
77433c34adc5Sramat 		vhcache->vhcache_client_tail->cct_next = cct;
77443c34adc5Sramat 	vhcache->vhcache_client_tail = cct;
77453c34adc5Sramat }
77463c34adc5Sramat 
77473c34adc5Sramat static void
free_string_array(char ** str,int nelem)77483c34adc5Sramat free_string_array(char **str, int nelem)
77493c34adc5Sramat {
77503c34adc5Sramat 	int i;
77513c34adc5Sramat 
77523c34adc5Sramat 	if (str) {
77533c34adc5Sramat 		for (i = 0; i < nelem; i++) {
77543c34adc5Sramat 			if (str[i])
77553c34adc5Sramat 				kmem_free(str[i], strlen(str[i]) + 1);
77563c34adc5Sramat 		}
77573c34adc5Sramat 		kmem_free(str, sizeof (char *) * nelem);
77583c34adc5Sramat 	}
77593c34adc5Sramat }
77603c34adc5Sramat 
77613c34adc5Sramat static void
free_vhcache_phci(mdi_vhcache_phci_t * cphci)77623c34adc5Sramat free_vhcache_phci(mdi_vhcache_phci_t *cphci)
77633c34adc5Sramat {
77643c34adc5Sramat 	kmem_free(cphci->cphci_path, strlen(cphci->cphci_path) + 1);
77653c34adc5Sramat 	kmem_free(cphci, sizeof (*cphci));
77663c34adc5Sramat }
77673c34adc5Sramat 
77683c34adc5Sramat static void
free_vhcache_pathinfo(mdi_vhcache_pathinfo_t * cpi)77693c34adc5Sramat free_vhcache_pathinfo(mdi_vhcache_pathinfo_t *cpi)
77703c34adc5Sramat {
77713c34adc5Sramat 	kmem_free(cpi->cpi_addr, strlen(cpi->cpi_addr) + 1);
77723c34adc5Sramat 	kmem_free(cpi, sizeof (*cpi));
77733c34adc5Sramat }
77743c34adc5Sramat 
77753c34adc5Sramat static void
free_vhcache_client(mdi_vhcache_client_t * cct)77763c34adc5Sramat free_vhcache_client(mdi_vhcache_client_t *cct)
77773c34adc5Sramat {
77783c34adc5Sramat 	kmem_free(cct->cct_name_addr, strlen(cct->cct_name_addr) + 1);
77793c34adc5Sramat 	kmem_free(cct, sizeof (*cct));
77803c34adc5Sramat }
77813c34adc5Sramat 
77823c34adc5Sramat static char *
vhcache_mknameaddr(char * ct_name,char * ct_addr,int * ret_len)77833c34adc5Sramat vhcache_mknameaddr(char *ct_name, char *ct_addr, int *ret_len)
77843c34adc5Sramat {
77853c34adc5Sramat 	char *name_addr;
77863c34adc5Sramat 	int len;
77873c34adc5Sramat 
77883c34adc5Sramat 	len = strlen(ct_name) + strlen(ct_addr) + 2;
77893c34adc5Sramat 	name_addr = kmem_alloc(len, KM_SLEEP);
77903c34adc5Sramat 	(void) snprintf(name_addr, len, "%s@%s", ct_name, ct_addr);
77913c34adc5Sramat 
77923c34adc5Sramat 	if (ret_len)
77933c34adc5Sramat 		*ret_len = len;
77943c34adc5Sramat 	return (name_addr);
77953c34adc5Sramat }
77963c34adc5Sramat 
77973c34adc5Sramat /*
77983c34adc5Sramat  * Copy the contents of paddrnvl to vhci cache.
77993c34adc5Sramat  * paddrnvl nvlist contains path information for a vhci client.
78003c34adc5Sramat  * See the comment in mainnvl_to_vhcache() for the format of this nvlist.
78013c34adc5Sramat  */
78023c34adc5Sramat static void
paddrnvl_to_vhcache(nvlist_t * nvl,mdi_vhcache_phci_t * cphci_list[],mdi_vhcache_client_t * cct)78033c34adc5Sramat paddrnvl_to_vhcache(nvlist_t *nvl, mdi_vhcache_phci_t *cphci_list[],
78043c34adc5Sramat     mdi_vhcache_client_t *cct)
78053c34adc5Sramat {
78063c34adc5Sramat 	nvpair_t *nvp = NULL;
78073c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
78083c34adc5Sramat 	uint_t nelem;
78093c34adc5Sramat 	uint32_t *val;
78103c34adc5Sramat 
78113c34adc5Sramat 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
78123c34adc5Sramat 		ASSERT(nvpair_type(nvp) == DATA_TYPE_UINT32_ARRAY);
78133c34adc5Sramat 		cpi = kmem_zalloc(sizeof (*cpi), KM_SLEEP);
78143c34adc5Sramat 		cpi->cpi_addr = i_ddi_strdup(nvpair_name(nvp), KM_SLEEP);
78153c34adc5Sramat 		(void) nvpair_value_uint32_array(nvp, &val, &nelem);
78163c34adc5Sramat 		ASSERT(nelem == 2);
78173c34adc5Sramat 		cpi->cpi_cphci = cphci_list[val[0]];
78183c34adc5Sramat 		cpi->cpi_flags = val[1];
78193c34adc5Sramat 		enqueue_tail_vhcache_pathinfo(cct, cpi);
78203c34adc5Sramat 	}
78213c34adc5Sramat }
78223c34adc5Sramat 
78233c34adc5Sramat /*
78243c34adc5Sramat  * Copy the contents of caddrmapnvl to vhci cache.
78253c34adc5Sramat  * caddrmapnvl nvlist contains vhci client address to phci client address
78263c34adc5Sramat  * mappings. See the comment in mainnvl_to_vhcache() for the format of
78273c34adc5Sramat  * this nvlist.
78283c34adc5Sramat  */
78293c34adc5Sramat static void
caddrmapnvl_to_vhcache(mdi_vhci_cache_t * vhcache,nvlist_t * nvl,mdi_vhcache_phci_t * cphci_list[])78303c34adc5Sramat caddrmapnvl_to_vhcache(mdi_vhci_cache_t *vhcache, nvlist_t *nvl,
78313c34adc5Sramat     mdi_vhcache_phci_t *cphci_list[])
78323c34adc5Sramat {
78333c34adc5Sramat 	nvpair_t *nvp = NULL;
78343c34adc5Sramat 	nvlist_t *paddrnvl;
78353c34adc5Sramat 	mdi_vhcache_client_t *cct;
78363c34adc5Sramat 
78373c34adc5Sramat 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
78383c34adc5Sramat 		ASSERT(nvpair_type(nvp) == DATA_TYPE_NVLIST);
78393c34adc5Sramat 		cct = kmem_zalloc(sizeof (*cct), KM_SLEEP);
78403c34adc5Sramat 		cct->cct_name_addr = i_ddi_strdup(nvpair_name(nvp), KM_SLEEP);
78413c34adc5Sramat 		(void) nvpair_value_nvlist(nvp, &paddrnvl);
78423c34adc5Sramat 		paddrnvl_to_vhcache(paddrnvl, cphci_list, cct);
78433c34adc5Sramat 		/* the client must contain at least one path */
78443c34adc5Sramat 		ASSERT(cct->cct_cpi_head != NULL);
78453c34adc5Sramat 
78463c34adc5Sramat 		enqueue_vhcache_client(vhcache, cct);
78473c34adc5Sramat 		(void) mod_hash_insert(vhcache->vhcache_client_hash,
78483c34adc5Sramat 		    (mod_hash_key_t)cct->cct_name_addr, (mod_hash_val_t)cct);
78493c34adc5Sramat 	}
78503c34adc5Sramat }
78513c34adc5Sramat 
78523c34adc5Sramat /*
78533c34adc5Sramat  * Copy the contents of the main nvlist to vhci cache.
78543c34adc5Sramat  *
78553c34adc5Sramat  * VHCI busconfig cached data is stored in the form of a nvlist on the disk.
78563c34adc5Sramat  * The nvlist contains the mappings between the vhci client addresses and
78573c34adc5Sramat  * their corresponding phci client addresses.
78583c34adc5Sramat  *
78593c34adc5Sramat  * The structure of the nvlist is as follows:
78603c34adc5Sramat  *
78613c34adc5Sramat  * Main nvlist:
78623c34adc5Sramat  *	NAME		TYPE		DATA
78633c34adc5Sramat  *	version		int32		version number
78643c34adc5Sramat  *	phcis		string array	array of phci paths
78653c34adc5Sramat  *	clientaddrmap	nvlist_t	c2paddrs_nvl (see below)
78663c34adc5Sramat  *
78673c34adc5Sramat  * structure of c2paddrs_nvl:
78683c34adc5Sramat  *	NAME		TYPE		DATA
78693c34adc5Sramat  *	caddr1		nvlist_t	paddrs_nvl1
78703c34adc5Sramat  *	caddr2		nvlist_t	paddrs_nvl2
78713c34adc5Sramat  *	...
78723c34adc5Sramat  * where caddr1, caddr2, ... are vhci client name and addresses in the
78733c34adc5Sramat  * form of "<clientname>@<clientaddress>".
78743c34adc5Sramat  * (for example: "ssd@2000002037cd9f72");
78753c34adc5Sramat  * paddrs_nvl1, paddrs_nvl2, .. are nvlists that contain path information.
78763c34adc5Sramat  *
78773c34adc5Sramat  * structure of paddrs_nvl:
78783c34adc5Sramat  *	NAME		TYPE		DATA
78793c34adc5Sramat  *	pi_addr1	uint32_array	(phci-id, cpi_flags)
78803c34adc5Sramat  *	pi_addr2	uint32_array	(phci-id, cpi_flags)
78813c34adc5Sramat  *	...
78823c34adc5Sramat  * where pi_addr1, pi_addr2, ... are bus specific addresses of pathinfo nodes
78833c34adc5Sramat  * (so called pi_addrs, for example: "w2100002037cd9f72,0");
78844c06356bSdh  * phci-ids are integers that identify pHCIs to which the
78853c34adc5Sramat  * the bus specific address belongs to. These integers are used as an index
78864c06356bSdh  * into to the phcis string array in the main nvlist to get the pHCI path.
78873c34adc5Sramat  */
78883c34adc5Sramat static int
mainnvl_to_vhcache(mdi_vhci_cache_t * vhcache,nvlist_t * nvl)78893c34adc5Sramat mainnvl_to_vhcache(mdi_vhci_cache_t *vhcache, nvlist_t *nvl)
78903c34adc5Sramat {
78913c34adc5Sramat 	char **phcis, **phci_namep;
78923c34adc5Sramat 	uint_t nphcis;
78933c34adc5Sramat 	mdi_vhcache_phci_t *cphci, **cphci_list;
78943c34adc5Sramat 	nvlist_t *caddrmapnvl;
78953c34adc5Sramat 	int32_t ver;
78963c34adc5Sramat 	int i;
78973c34adc5Sramat 	size_t cphci_list_size;
78983c34adc5Sramat 
78993c34adc5Sramat 	ASSERT(RW_WRITE_HELD(&vhcache->vhcache_lock));
79003c34adc5Sramat 
79013c34adc5Sramat 	if (nvlist_lookup_int32(nvl, MDI_NVPNAME_VERSION, &ver) != 0 ||
79023c34adc5Sramat 	    ver != MDI_VHCI_CACHE_VERSION)
79033c34adc5Sramat 		return (MDI_FAILURE);
79043c34adc5Sramat 
79053c34adc5Sramat 	if (nvlist_lookup_string_array(nvl, MDI_NVPNAME_PHCIS, &phcis,
79063c34adc5Sramat 	    &nphcis) != 0)
79073c34adc5Sramat 		return (MDI_SUCCESS);
79083c34adc5Sramat 
79093c34adc5Sramat 	ASSERT(nphcis > 0);
79103c34adc5Sramat 
79113c34adc5Sramat 	cphci_list_size = sizeof (mdi_vhcache_phci_t *) * nphcis;
79123c34adc5Sramat 	cphci_list = kmem_alloc(cphci_list_size, KM_SLEEP);
79133c34adc5Sramat 	for (i = 0, phci_namep = phcis; i < nphcis; i++, phci_namep++) {
79143c34adc5Sramat 		cphci = kmem_zalloc(sizeof (mdi_vhcache_phci_t), KM_SLEEP);
79153c34adc5Sramat 		cphci->cphci_path = i_ddi_strdup(*phci_namep, KM_SLEEP);
79163c34adc5Sramat 		enqueue_vhcache_phci(vhcache, cphci);
79173c34adc5Sramat 		cphci_list[i] = cphci;
79183c34adc5Sramat 	}
79193c34adc5Sramat 
79203c34adc5Sramat 	ASSERT(vhcache->vhcache_phci_head != NULL);
79213c34adc5Sramat 
79223c34adc5Sramat 	if (nvlist_lookup_nvlist(nvl, MDI_NVPNAME_CTADDRMAP, &caddrmapnvl) == 0)
79233c34adc5Sramat 		caddrmapnvl_to_vhcache(vhcache, caddrmapnvl, cphci_list);
79243c34adc5Sramat 
79253c34adc5Sramat 	kmem_free(cphci_list, cphci_list_size);
79263c34adc5Sramat 	return (MDI_SUCCESS);
79273c34adc5Sramat }
79283c34adc5Sramat 
79293c34adc5Sramat /*
79303c34adc5Sramat  * Build paddrnvl for the specified client using the information in the
79313c34adc5Sramat  * vhci cache and add it to the caddrmapnnvl.
79323c34adc5Sramat  * Returns 0 on success, errno on failure.
79333c34adc5Sramat  */
79343c34adc5Sramat static int
vhcache_to_paddrnvl(mdi_vhci_cache_t * vhcache,mdi_vhcache_client_t * cct,nvlist_t * caddrmapnvl)79353c34adc5Sramat vhcache_to_paddrnvl(mdi_vhci_cache_t *vhcache, mdi_vhcache_client_t *cct,
79363c34adc5Sramat     nvlist_t *caddrmapnvl)
79373c34adc5Sramat {
79383c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
79393c34adc5Sramat 	nvlist_t *nvl;
79403c34adc5Sramat 	int err;
79413c34adc5Sramat 	uint32_t val[2];
79423c34adc5Sramat 
79433c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
79443c34adc5Sramat 
79453c34adc5Sramat 	if ((err = nvlist_alloc(&nvl, 0, KM_SLEEP)) != 0)
79463c34adc5Sramat 		return (err);
79473c34adc5Sramat 
79483c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
79493c34adc5Sramat 		val[0] = cpi->cpi_cphci->cphci_id;
79503c34adc5Sramat 		val[1] = cpi->cpi_flags;
79513c34adc5Sramat 		if ((err = nvlist_add_uint32_array(nvl, cpi->cpi_addr, val, 2))
79523c34adc5Sramat 		    != 0)
79533c34adc5Sramat 			goto out;
79543c34adc5Sramat 	}
79553c34adc5Sramat 
79563c34adc5Sramat 	err = nvlist_add_nvlist(caddrmapnvl, cct->cct_name_addr, nvl);
79573c34adc5Sramat out:
79583c34adc5Sramat 	nvlist_free(nvl);
79593c34adc5Sramat 	return (err);
79603c34adc5Sramat }
79613c34adc5Sramat 
79623c34adc5Sramat /*
79633c34adc5Sramat  * Build caddrmapnvl using the information in the vhci cache
79643c34adc5Sramat  * and add it to the mainnvl.
79653c34adc5Sramat  * Returns 0 on success, errno on failure.
79663c34adc5Sramat  */
79673c34adc5Sramat static int
vhcache_to_caddrmapnvl(mdi_vhci_cache_t * vhcache,nvlist_t * mainnvl)79683c34adc5Sramat vhcache_to_caddrmapnvl(mdi_vhci_cache_t *vhcache, nvlist_t *mainnvl)
79693c34adc5Sramat {
79703c34adc5Sramat 	mdi_vhcache_client_t *cct;
79713c34adc5Sramat 	nvlist_t *nvl;
79723c34adc5Sramat 	int err;
79733c34adc5Sramat 
79743c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
79753c34adc5Sramat 
79763c34adc5Sramat 	if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) != 0)
79773c34adc5Sramat 		return (err);
79783c34adc5Sramat 
79793c34adc5Sramat 	for (cct = vhcache->vhcache_client_head; cct != NULL;
79803c34adc5Sramat 	    cct = cct->cct_next) {
79813c34adc5Sramat 		if ((err = vhcache_to_paddrnvl(vhcache, cct, nvl)) != 0)
79823c34adc5Sramat 			goto out;
79833c34adc5Sramat 	}
79843c34adc5Sramat 
79853c34adc5Sramat 	err = nvlist_add_nvlist(mainnvl, MDI_NVPNAME_CTADDRMAP, nvl);
79863c34adc5Sramat out:
79873c34adc5Sramat 	nvlist_free(nvl);
79883c34adc5Sramat 	return (err);
79893c34adc5Sramat }
79903c34adc5Sramat 
79913c34adc5Sramat /*
79923c34adc5Sramat  * Build nvlist using the information in the vhci cache.
79933c34adc5Sramat  * See the comment in mainnvl_to_vhcache() for the format of the nvlist.
79943c34adc5Sramat  * Returns nvl on success, NULL on failure.
79953c34adc5Sramat  */
79963c34adc5Sramat static nvlist_t *
vhcache_to_mainnvl(mdi_vhci_cache_t * vhcache)79973c34adc5Sramat vhcache_to_mainnvl(mdi_vhci_cache_t *vhcache)
79983c34adc5Sramat {
79993c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
80003c34adc5Sramat 	uint_t phci_count;
80013c34adc5Sramat 	char **phcis;
80023c34adc5Sramat 	nvlist_t *nvl;
80033c34adc5Sramat 	int err, i;
80043c34adc5Sramat 
80053c34adc5Sramat 	if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) != 0) {
80063c34adc5Sramat 		nvl = NULL;
80073c34adc5Sramat 		goto out;
80083c34adc5Sramat 	}
80093c34adc5Sramat 
80103c34adc5Sramat 	if ((err = nvlist_add_int32(nvl, MDI_NVPNAME_VERSION,
80113c34adc5Sramat 	    MDI_VHCI_CACHE_VERSION)) != 0)
80123c34adc5Sramat 		goto out;
80133c34adc5Sramat 
80143c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
80153c34adc5Sramat 	if (vhcache->vhcache_phci_head == NULL) {
80163c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
80173c34adc5Sramat 		return (nvl);
80183c34adc5Sramat 	}
80193c34adc5Sramat 
80203c34adc5Sramat 	phci_count = 0;
80213c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
80223c34adc5Sramat 	    cphci = cphci->cphci_next)
80233c34adc5Sramat 		cphci->cphci_id = phci_count++;
80243c34adc5Sramat 
80253c34adc5Sramat 	/* build phci pathname list */
80263c34adc5Sramat 	phcis = kmem_alloc(sizeof (char *) * phci_count, KM_SLEEP);
80273c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head, i = 0; cphci != NULL;
80283c34adc5Sramat 	    cphci = cphci->cphci_next, i++)
80293c34adc5Sramat 		phcis[i] = i_ddi_strdup(cphci->cphci_path, KM_SLEEP);
80303c34adc5Sramat 
80313c34adc5Sramat 	err = nvlist_add_string_array(nvl, MDI_NVPNAME_PHCIS, phcis,
80323c34adc5Sramat 	    phci_count);
80333c34adc5Sramat 	free_string_array(phcis, phci_count);
80343c34adc5Sramat 
80353c34adc5Sramat 	if (err == 0 &&
80363c34adc5Sramat 	    (err = vhcache_to_caddrmapnvl(vhcache, nvl)) == 0) {
80373c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
80383c34adc5Sramat 		return (nvl);
80393c34adc5Sramat 	}
80403c34adc5Sramat 
80413c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
80423c34adc5Sramat out:
8043aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(nvl);
80443c34adc5Sramat 	return (NULL);
80453c34adc5Sramat }
80463c34adc5Sramat 
80473c34adc5Sramat /*
80483c34adc5Sramat  * Lookup vhcache phci structure for the specified phci path.
80493c34adc5Sramat  */
80503c34adc5Sramat static mdi_vhcache_phci_t *
lookup_vhcache_phci_by_name(mdi_vhci_cache_t * vhcache,char * phci_path)80513c34adc5Sramat lookup_vhcache_phci_by_name(mdi_vhci_cache_t *vhcache, char *phci_path)
80523c34adc5Sramat {
80533c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
80543c34adc5Sramat 
80553c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
80563c34adc5Sramat 
80573c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
80583c34adc5Sramat 	    cphci = cphci->cphci_next) {
80593c34adc5Sramat 		if (strcmp(cphci->cphci_path, phci_path) == 0)
80603c34adc5Sramat 			return (cphci);
80613c34adc5Sramat 	}
80623c34adc5Sramat 
80633c34adc5Sramat 	return (NULL);
80643c34adc5Sramat }
80653c34adc5Sramat 
80663c34adc5Sramat /*
80673c34adc5Sramat  * Lookup vhcache phci structure for the specified phci.
80683c34adc5Sramat  */
80693c34adc5Sramat static mdi_vhcache_phci_t *
lookup_vhcache_phci_by_addr(mdi_vhci_cache_t * vhcache,mdi_phci_t * ph)80703c34adc5Sramat lookup_vhcache_phci_by_addr(mdi_vhci_cache_t *vhcache, mdi_phci_t *ph)
80713c34adc5Sramat {
80723c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
80733c34adc5Sramat 
80743c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
80753c34adc5Sramat 
80763c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
80773c34adc5Sramat 	    cphci = cphci->cphci_next) {
80783c34adc5Sramat 		if (cphci->cphci_phci == ph)
80793c34adc5Sramat 			return (cphci);
80803c34adc5Sramat 	}
80813c34adc5Sramat 
80823c34adc5Sramat 	return (NULL);
80833c34adc5Sramat }
80843c34adc5Sramat 
80853c34adc5Sramat /*
80863c34adc5Sramat  * Add the specified phci to the vhci cache if not already present.
80873c34adc5Sramat  */
80883c34adc5Sramat static void
vhcache_phci_add(mdi_vhci_config_t * vhc,mdi_phci_t * ph)80893c34adc5Sramat vhcache_phci_add(mdi_vhci_config_t *vhc, mdi_phci_t *ph)
80903c34adc5Sramat {
80913c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
80923c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
80933c34adc5Sramat 	char *pathname;
80943c34adc5Sramat 	int cache_updated;
80953c34adc5Sramat 
80963c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
80973c34adc5Sramat 
80983c34adc5Sramat 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
80993c34adc5Sramat 	(void) ddi_pathname(ph->ph_dip, pathname);
81003c34adc5Sramat 	if ((cphci = lookup_vhcache_phci_by_name(vhcache, pathname))
81013c34adc5Sramat 	    != NULL) {
81023c34adc5Sramat 		cphci->cphci_phci = ph;
81033c34adc5Sramat 		cache_updated = 0;
81043c34adc5Sramat 	} else {
81053c34adc5Sramat 		cphci = kmem_zalloc(sizeof (*cphci), KM_SLEEP);
81063c34adc5Sramat 		cphci->cphci_path = i_ddi_strdup(pathname, KM_SLEEP);
81073c34adc5Sramat 		cphci->cphci_phci = ph;
81083c34adc5Sramat 		enqueue_vhcache_phci(vhcache, cphci);
81093c34adc5Sramat 		cache_updated = 1;
81103c34adc5Sramat 	}
811167e56d35Sramat 
81123c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
81133c34adc5Sramat 
811467e56d35Sramat 	/*
811567e56d35Sramat 	 * Since a new phci has been added, reset
811667e56d35Sramat 	 * vhc_path_discovery_cutoff_time to allow for discovery of paths
811767e56d35Sramat 	 * during next vhcache_discover_paths().
811867e56d35Sramat 	 */
811967e56d35Sramat 	mutex_enter(&vhc->vhc_lock);
812067e56d35Sramat 	vhc->vhc_path_discovery_cutoff_time = 0;
812167e56d35Sramat 	mutex_exit(&vhc->vhc_lock);
812267e56d35Sramat 
81233c34adc5Sramat 	kmem_free(pathname, MAXPATHLEN);
81243c34adc5Sramat 	if (cache_updated)
81253c34adc5Sramat 		vhcache_dirty(vhc);
81263c34adc5Sramat }
81273c34adc5Sramat 
81283c34adc5Sramat /*
81293c34adc5Sramat  * Remove the reference to the specified phci from the vhci cache.
81303c34adc5Sramat  */
81313c34adc5Sramat static void
vhcache_phci_remove(mdi_vhci_config_t * vhc,mdi_phci_t * ph)81323c34adc5Sramat vhcache_phci_remove(mdi_vhci_config_t *vhc, mdi_phci_t *ph)
81333c34adc5Sramat {
81343c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
81353c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
81363c34adc5Sramat 
81373c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
81383c34adc5Sramat 	if ((cphci = lookup_vhcache_phci_by_addr(vhcache, ph)) != NULL) {
81393c34adc5Sramat 		/* do not remove the actual mdi_vhcache_phci structure */
81403c34adc5Sramat 		cphci->cphci_phci = NULL;
81413c34adc5Sramat 	}
81423c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
81433c34adc5Sramat }
81443c34adc5Sramat 
81453c34adc5Sramat static void
init_vhcache_lookup_token(mdi_vhcache_lookup_token_t * dst,mdi_vhcache_lookup_token_t * src)81463c34adc5Sramat init_vhcache_lookup_token(mdi_vhcache_lookup_token_t *dst,
81473c34adc5Sramat     mdi_vhcache_lookup_token_t *src)
81483c34adc5Sramat {
81493c34adc5Sramat 	if (src == NULL) {
81503c34adc5Sramat 		dst->lt_cct = NULL;
81513c34adc5Sramat 		dst->lt_cct_lookup_time = 0;
81523c34adc5Sramat 	} else {
81533c34adc5Sramat 		dst->lt_cct = src->lt_cct;
81543c34adc5Sramat 		dst->lt_cct_lookup_time = src->lt_cct_lookup_time;
81553c34adc5Sramat 	}
81563c34adc5Sramat }
81573c34adc5Sramat 
81583c34adc5Sramat /*
81593c34adc5Sramat  * Look up vhcache client for the specified client.
81603c34adc5Sramat  */
81613c34adc5Sramat static mdi_vhcache_client_t *
lookup_vhcache_client(mdi_vhci_cache_t * vhcache,char * ct_name,char * ct_addr,mdi_vhcache_lookup_token_t * token)81623c34adc5Sramat lookup_vhcache_client(mdi_vhci_cache_t *vhcache, char *ct_name, char *ct_addr,
81633c34adc5Sramat     mdi_vhcache_lookup_token_t *token)
81643c34adc5Sramat {
81653c34adc5Sramat 	mod_hash_val_t hv;
81663c34adc5Sramat 	char *name_addr;
81673c34adc5Sramat 	int len;
81683c34adc5Sramat 
81693c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
81703c34adc5Sramat 
81713c34adc5Sramat 	/*
81723c34adc5Sramat 	 * If no vhcache clean occurred since the last lookup, we can
81733c34adc5Sramat 	 * simply return the cct from the last lookup operation.
81743c34adc5Sramat 	 * It works because ccts are never freed except during the vhcache
81753c34adc5Sramat 	 * cleanup operation.
81763c34adc5Sramat 	 */
81773c34adc5Sramat 	if (token != NULL &&
81783c34adc5Sramat 	    vhcache->vhcache_clean_time < token->lt_cct_lookup_time)
81793c34adc5Sramat 		return (token->lt_cct);
81803c34adc5Sramat 
81813c34adc5Sramat 	name_addr = vhcache_mknameaddr(ct_name, ct_addr, &len);
81823c34adc5Sramat 	if (mod_hash_find(vhcache->vhcache_client_hash,
81833c34adc5Sramat 	    (mod_hash_key_t)name_addr, &hv) == 0) {
81843c34adc5Sramat 		if (token) {
81853c34adc5Sramat 			token->lt_cct = (mdi_vhcache_client_t *)hv;
8186d3d50737SRafael Vanoni 			token->lt_cct_lookup_time = ddi_get_lbolt64();
81873c34adc5Sramat 		}
81883c34adc5Sramat 	} else {
81893c34adc5Sramat 		if (token) {
81903c34adc5Sramat 			token->lt_cct = NULL;
81913c34adc5Sramat 			token->lt_cct_lookup_time = 0;
81923c34adc5Sramat 		}
81933c34adc5Sramat 		hv = NULL;
81943c34adc5Sramat 	}
81953c34adc5Sramat 	kmem_free(name_addr, len);
81963c34adc5Sramat 	return ((mdi_vhcache_client_t *)hv);
81973c34adc5Sramat }
81983c34adc5Sramat 
81993c34adc5Sramat /*
82003c34adc5Sramat  * Add the specified path to the vhci cache if not already present.
82013c34adc5Sramat  * Also add the vhcache client for the client corresponding to this path
82023c34adc5Sramat  * if it doesn't already exist.
82033c34adc5Sramat  */
82043c34adc5Sramat static void
vhcache_pi_add(mdi_vhci_config_t * vhc,struct mdi_pathinfo * pip)82053c34adc5Sramat vhcache_pi_add(mdi_vhci_config_t *vhc, struct mdi_pathinfo *pip)
82063c34adc5Sramat {
82073c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
82083c34adc5Sramat 	mdi_vhcache_client_t *cct;
82093c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
82103c34adc5Sramat 	mdi_phci_t *ph = pip->pi_phci;
82113c34adc5Sramat 	mdi_client_t *ct = pip->pi_client;
82123c34adc5Sramat 	int cache_updated = 0;
82133c34adc5Sramat 
82143c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
82153c34adc5Sramat 
82163c34adc5Sramat 	/* if vhcache client for this pip doesn't already exist, add it */
82173c34adc5Sramat 	if ((cct = lookup_vhcache_client(vhcache, ct->ct_drvname, ct->ct_guid,
82183c34adc5Sramat 	    NULL)) == NULL) {
82193c34adc5Sramat 		cct = kmem_zalloc(sizeof (*cct), KM_SLEEP);
82203c34adc5Sramat 		cct->cct_name_addr = vhcache_mknameaddr(ct->ct_drvname,
82213c34adc5Sramat 		    ct->ct_guid, NULL);
82223c34adc5Sramat 		enqueue_vhcache_client(vhcache, cct);
82233c34adc5Sramat 		(void) mod_hash_insert(vhcache->vhcache_client_hash,
82243c34adc5Sramat 		    (mod_hash_key_t)cct->cct_name_addr, (mod_hash_val_t)cct);
82253c34adc5Sramat 		cache_updated = 1;
82263c34adc5Sramat 	}
82273c34adc5Sramat 
82283c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
82293c34adc5Sramat 		if (cpi->cpi_cphci->cphci_phci == ph &&
82303c34adc5Sramat 		    strcmp(cpi->cpi_addr, pip->pi_addr) == 0) {
82313c34adc5Sramat 			cpi->cpi_pip = pip;
82323c34adc5Sramat 			if (cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST) {
82333c34adc5Sramat 				cpi->cpi_flags &=
82343c34adc5Sramat 				    ~MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
82353c34adc5Sramat 				sort_vhcache_paths(cct);
82363c34adc5Sramat 				cache_updated = 1;
82373c34adc5Sramat 			}
82383c34adc5Sramat 			break;
82393c34adc5Sramat 		}
82403c34adc5Sramat 	}
82413c34adc5Sramat 
82423c34adc5Sramat 	if (cpi == NULL) {
82433c34adc5Sramat 		cpi = kmem_zalloc(sizeof (*cpi), KM_SLEEP);
82443c34adc5Sramat 		cpi->cpi_addr = i_ddi_strdup(pip->pi_addr, KM_SLEEP);
82453c34adc5Sramat 		cpi->cpi_cphci = lookup_vhcache_phci_by_addr(vhcache, ph);
82463c34adc5Sramat 		ASSERT(cpi->cpi_cphci != NULL);
82473c34adc5Sramat 		cpi->cpi_pip = pip;
82483c34adc5Sramat 		enqueue_vhcache_pathinfo(cct, cpi);
82493c34adc5Sramat 		cache_updated = 1;
82503c34adc5Sramat 	}
82513c34adc5Sramat 
82523c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
82533c34adc5Sramat 
82543c34adc5Sramat 	if (cache_updated)
82553c34adc5Sramat 		vhcache_dirty(vhc);
82563c34adc5Sramat }
82573c34adc5Sramat 
82583c34adc5Sramat /*
82593c34adc5Sramat  * Remove the reference to the specified path from the vhci cache.
82603c34adc5Sramat  */
82613c34adc5Sramat static void
vhcache_pi_remove(mdi_vhci_config_t * vhc,struct mdi_pathinfo * pip)82623c34adc5Sramat vhcache_pi_remove(mdi_vhci_config_t *vhc, struct mdi_pathinfo *pip)
82633c34adc5Sramat {
82643c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
82653c34adc5Sramat 	mdi_client_t *ct = pip->pi_client;
82663c34adc5Sramat 	mdi_vhcache_client_t *cct;
82673c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
82683c34adc5Sramat 
82693c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
82703c34adc5Sramat 	if ((cct = lookup_vhcache_client(vhcache, ct->ct_drvname, ct->ct_guid,
82713c34adc5Sramat 	    NULL)) != NULL) {
82723c34adc5Sramat 		for (cpi = cct->cct_cpi_head; cpi != NULL;
82733c34adc5Sramat 		    cpi = cpi->cpi_next) {
82743c34adc5Sramat 			if (cpi->cpi_pip == pip) {
82753c34adc5Sramat 				cpi->cpi_pip = NULL;
82763c34adc5Sramat 				break;
82773c34adc5Sramat 			}
82783c34adc5Sramat 		}
82793c34adc5Sramat 	}
82803c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
82813c34adc5Sramat }
82823c34adc5Sramat 
82833c34adc5Sramat /*
82843c34adc5Sramat  * Flush the vhci cache to disk.
82853c34adc5Sramat  * Returns MDI_SUCCESS on success, MDI_FAILURE on failure.
82863c34adc5Sramat  */
82873c34adc5Sramat static int
flush_vhcache(mdi_vhci_config_t * vhc,int force_flag)82883c34adc5Sramat flush_vhcache(mdi_vhci_config_t *vhc, int force_flag)
82893c34adc5Sramat {
82903c34adc5Sramat 	nvlist_t *nvl;
82913c34adc5Sramat 	int err;
82923c34adc5Sramat 	int rv;
82933c34adc5Sramat 
82943c34adc5Sramat 	/*
82953c34adc5Sramat 	 * It is possible that the system may shutdown before
82963c34adc5Sramat 	 * i_ddi_io_initialized (during stmsboot for example). To allow for
82973c34adc5Sramat 	 * flushing the cache in this case do not check for
82983c34adc5Sramat 	 * i_ddi_io_initialized when force flag is set.
82993c34adc5Sramat 	 */
83003c34adc5Sramat 	if (force_flag == 0 && !i_ddi_io_initialized())
83013c34adc5Sramat 		return (MDI_FAILURE);
83023c34adc5Sramat 
83033c34adc5Sramat 	if ((nvl = vhcache_to_mainnvl(&vhc->vhc_vhcache)) != NULL) {
83043c34adc5Sramat 		err = fwrite_nvlist(vhc->vhc_vhcache_filename, nvl);
83053c34adc5Sramat 		nvlist_free(nvl);
83063c34adc5Sramat 	} else
83073c34adc5Sramat 		err = EFAULT;
83083c34adc5Sramat 
83093c34adc5Sramat 	rv = MDI_SUCCESS;
83103c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
83113c34adc5Sramat 	if (err != 0) {
83123c34adc5Sramat 		if (err == EROFS) {
83133c34adc5Sramat 			vhc->vhc_flags |= MDI_VHC_READONLY_FS;
83143c34adc5Sramat 			vhc->vhc_flags &= ~(MDI_VHC_VHCACHE_FLUSH_ERROR |
83153c34adc5Sramat 			    MDI_VHC_VHCACHE_DIRTY);
83163c34adc5Sramat 		} else {
83173c34adc5Sramat 			if (!(vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_ERROR)) {
83183c34adc5Sramat 				cmn_err(CE_CONT, "%s: update failed\n",
83193c34adc5Sramat 				    vhc->vhc_vhcache_filename);
83203c34adc5Sramat 				vhc->vhc_flags |= MDI_VHC_VHCACHE_FLUSH_ERROR;
83213c34adc5Sramat 			}
83223c34adc5Sramat 			rv = MDI_FAILURE;
83233c34adc5Sramat 		}
83243c34adc5Sramat 	} else if (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_ERROR) {
83253c34adc5Sramat 		cmn_err(CE_CONT,
83263c34adc5Sramat 		    "%s: update now ok\n", vhc->vhc_vhcache_filename);
83273c34adc5Sramat 		vhc->vhc_flags &= ~MDI_VHC_VHCACHE_FLUSH_ERROR;
83283c34adc5Sramat 	}
83293c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
83303c34adc5Sramat 
83313c34adc5Sramat 	return (rv);
83323c34adc5Sramat }
83333c34adc5Sramat 
83343c34adc5Sramat /*
83353c34adc5Sramat  * Call flush_vhcache() to flush the vhci cache at the scheduled time.
83363c34adc5Sramat  * Exits itself if left idle for the idle timeout period.
83373c34adc5Sramat  */
83383c34adc5Sramat static void
vhcache_flush_thread(void * arg)83393c34adc5Sramat vhcache_flush_thread(void *arg)
83403c34adc5Sramat {
83413c34adc5Sramat 	mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
83423c34adc5Sramat 	clock_t idle_time, quit_at_ticks;
83433c34adc5Sramat 	callb_cpr_t cprinfo;
83443c34adc5Sramat 
83453c34adc5Sramat 	/* number of seconds to sleep idle before exiting */
83463c34adc5Sramat 	idle_time = mdi_vhcache_flush_daemon_idle_time * TICKS_PER_SECOND;
83473c34adc5Sramat 
83483c34adc5Sramat 	CALLB_CPR_INIT(&cprinfo, &vhc->vhc_lock, callb_generic_cpr,
83493c34adc5Sramat 	    "mdi_vhcache_flush");
83503c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
83513c34adc5Sramat 	for (; ; ) {
83523c34adc5Sramat 		while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
83533c34adc5Sramat 		    (vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY)) {
83543c34adc5Sramat 			if (ddi_get_lbolt() < vhc->vhc_flush_at_ticks) {
83553c34adc5Sramat 				CALLB_CPR_SAFE_BEGIN(&cprinfo);
83563c34adc5Sramat 				(void) cv_timedwait(&vhc->vhc_cv,
83573c34adc5Sramat 				    &vhc->vhc_lock, vhc->vhc_flush_at_ticks);
83583c34adc5Sramat 				CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
83593c34adc5Sramat 			} else {
83603c34adc5Sramat 				vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
83613c34adc5Sramat 				mutex_exit(&vhc->vhc_lock);
83623c34adc5Sramat 
83633c34adc5Sramat 				if (flush_vhcache(vhc, 0) != MDI_SUCCESS)
83643c34adc5Sramat 					vhcache_dirty(vhc);
83653c34adc5Sramat 
83663c34adc5Sramat 				mutex_enter(&vhc->vhc_lock);
83673c34adc5Sramat 			}
83683c34adc5Sramat 		}
83693c34adc5Sramat 
83703c34adc5Sramat 		quit_at_ticks = ddi_get_lbolt() + idle_time;
83713c34adc5Sramat 
83723c34adc5Sramat 		while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
83733c34adc5Sramat 		    !(vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) &&
83743c34adc5Sramat 		    ddi_get_lbolt() < quit_at_ticks) {
83753c34adc5Sramat 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
83763c34adc5Sramat 			(void) cv_timedwait(&vhc->vhc_cv, &vhc->vhc_lock,
83773c34adc5Sramat 			    quit_at_ticks);
83783c34adc5Sramat 			CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
83793c34adc5Sramat 		}
83803c34adc5Sramat 
83813c34adc5Sramat 		if ((vhc->vhc_flags & MDI_VHC_EXIT) ||
83823c34adc5Sramat 		    !(vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY))
83833c34adc5Sramat 			goto out;
83843c34adc5Sramat 	}
83853c34adc5Sramat 
83863c34adc5Sramat out:
83873c34adc5Sramat 	vhc->vhc_flags &= ~MDI_VHC_VHCACHE_FLUSH_THREAD;
83883c34adc5Sramat 	/* CALLB_CPR_EXIT releases the vhc->vhc_lock */
83893c34adc5Sramat 	CALLB_CPR_EXIT(&cprinfo);
83903c34adc5Sramat }
83913c34adc5Sramat 
83923c34adc5Sramat /*
83933c34adc5Sramat  * Make vhci cache dirty and schedule flushing by vhcache flush thread.
83943c34adc5Sramat  */
83953c34adc5Sramat static void
vhcache_dirty(mdi_vhci_config_t * vhc)83963c34adc5Sramat vhcache_dirty(mdi_vhci_config_t *vhc)
83973c34adc5Sramat {
83983c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
83993c34adc5Sramat 	int create_thread;
84003c34adc5Sramat 
84013c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
84023c34adc5Sramat 	/* do not flush cache until the cache is fully built */
84033c34adc5Sramat 	if (!(vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE)) {
84043c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
84053c34adc5Sramat 		return;
84063c34adc5Sramat 	}
84073c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
84083c34adc5Sramat 
84093c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
84103c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_READONLY_FS) {
84113c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
84123c34adc5Sramat 		return;
84133c34adc5Sramat 	}
84143c34adc5Sramat 
84153c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_VHCACHE_DIRTY;
84163c34adc5Sramat 	vhc->vhc_flush_at_ticks = ddi_get_lbolt() +
84173c34adc5Sramat 	    mdi_vhcache_flush_delay * TICKS_PER_SECOND;
84183c34adc5Sramat 	if (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) {
84193c34adc5Sramat 		cv_broadcast(&vhc->vhc_cv);
84203c34adc5Sramat 		create_thread = 0;
84213c34adc5Sramat 	} else {
84223c34adc5Sramat 		vhc->vhc_flags |= MDI_VHC_VHCACHE_FLUSH_THREAD;
84233c34adc5Sramat 		create_thread = 1;
84243c34adc5Sramat 	}
84253c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
84263c34adc5Sramat 
84273c34adc5Sramat 	if (create_thread)
84283c34adc5Sramat 		(void) thread_create(NULL, 0, vhcache_flush_thread, vhc,
84293c34adc5Sramat 		    0, &p0, TS_RUN, minclsyspri);
84303c34adc5Sramat }
84313c34adc5Sramat 
84323c34adc5Sramat /*
84333c34adc5Sramat  * phci bus config structure - one for for each phci bus config operation that
84343c34adc5Sramat  * we initiate on behalf of a vhci.
84353c34adc5Sramat  */
84363c34adc5Sramat typedef struct mdi_phci_bus_config_s {
84373c34adc5Sramat 	char *phbc_phci_path;
84383c34adc5Sramat 	struct mdi_vhci_bus_config_s *phbc_vhbusconfig;	/* vhci bus config */
84393c34adc5Sramat 	struct mdi_phci_bus_config_s *phbc_next;
84403c34adc5Sramat } mdi_phci_bus_config_t;
84413c34adc5Sramat 
84423c34adc5Sramat /* vhci bus config structure - one for each vhci bus config operation */
84433c34adc5Sramat typedef struct mdi_vhci_bus_config_s {
84443c34adc5Sramat 	ddi_bus_config_op_t vhbc_op;	/* bus config op */
84453c34adc5Sramat 	major_t vhbc_op_major;		/* bus config op major */
84463c34adc5Sramat 	uint_t vhbc_op_flags;		/* bus config op flags */
84473c34adc5Sramat 	kmutex_t vhbc_lock;
84483c34adc5Sramat 	kcondvar_t vhbc_cv;
84493c34adc5Sramat 	int vhbc_thr_count;
84503c34adc5Sramat } mdi_vhci_bus_config_t;
84513c34adc5Sramat 
84523c34adc5Sramat /*
84533c34adc5Sramat  * bus config the specified phci
84543c34adc5Sramat  */
84553c34adc5Sramat static void
bus_config_phci(void * arg)84563c34adc5Sramat bus_config_phci(void *arg)
84573c34adc5Sramat {
84583c34adc5Sramat 	mdi_phci_bus_config_t *phbc = (mdi_phci_bus_config_t *)arg;
84593c34adc5Sramat 	mdi_vhci_bus_config_t *vhbc = phbc->phbc_vhbusconfig;
84603c34adc5Sramat 	dev_info_t *ph_dip;
84613c34adc5Sramat 
84623c34adc5Sramat 	/*
84633c34adc5Sramat 	 * first configure all path components upto phci and then configure
84643c34adc5Sramat 	 * the phci children.
84653c34adc5Sramat 	 */
84663c34adc5Sramat 	if ((ph_dip = e_ddi_hold_devi_by_path(phbc->phbc_phci_path, 0))
84673c34adc5Sramat 	    != NULL) {
84683c34adc5Sramat 		if (vhbc->vhbc_op == BUS_CONFIG_DRIVER ||
84693c34adc5Sramat 		    vhbc->vhbc_op == BUS_UNCONFIG_DRIVER) {
84703c34adc5Sramat 			(void) ndi_devi_config_driver(ph_dip,
84713c34adc5Sramat 			    vhbc->vhbc_op_flags,
84723c34adc5Sramat 			    vhbc->vhbc_op_major);
84733c34adc5Sramat 		} else
84743c34adc5Sramat 			(void) ndi_devi_config(ph_dip,
84753c34adc5Sramat 			    vhbc->vhbc_op_flags);
84763c34adc5Sramat 
84773c34adc5Sramat 		/* release the hold that e_ddi_hold_devi_by_path() placed */
84783c34adc5Sramat 		ndi_rele_devi(ph_dip);
84793c34adc5Sramat 	}
84803c34adc5Sramat 
84813c34adc5Sramat 	kmem_free(phbc->phbc_phci_path, strlen(phbc->phbc_phci_path) + 1);
84823c34adc5Sramat 	kmem_free(phbc, sizeof (*phbc));
84833c34adc5Sramat 
84843c34adc5Sramat 	mutex_enter(&vhbc->vhbc_lock);
84853c34adc5Sramat 	vhbc->vhbc_thr_count--;
84863c34adc5Sramat 	if (vhbc->vhbc_thr_count == 0)
84873c34adc5Sramat 		cv_broadcast(&vhbc->vhbc_cv);
84883c34adc5Sramat 	mutex_exit(&vhbc->vhbc_lock);
84893c34adc5Sramat }
84903c34adc5Sramat 
84913c34adc5Sramat /*
84923c34adc5Sramat  * Bus config all phcis associated with the vhci in parallel.
84933c34adc5Sramat  * op must be BUS_CONFIG_DRIVER or BUS_CONFIG_ALL.
84943c34adc5Sramat  */
84953c34adc5Sramat static void
bus_config_all_phcis(mdi_vhci_cache_t * vhcache,uint_t flags,ddi_bus_config_op_t op,major_t maj)84963c34adc5Sramat bus_config_all_phcis(mdi_vhci_cache_t *vhcache, uint_t flags,
84973c34adc5Sramat     ddi_bus_config_op_t op, major_t maj)
84983c34adc5Sramat {
84993c34adc5Sramat 	mdi_phci_bus_config_t *phbc_head = NULL, *phbc, *phbc_next;
85003c34adc5Sramat 	mdi_vhci_bus_config_t *vhbc;
85013c34adc5Sramat 	mdi_vhcache_phci_t *cphci;
85023c34adc5Sramat 
85033c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
85043c34adc5Sramat 	if (vhcache->vhcache_phci_head == NULL) {
85053c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
85063c34adc5Sramat 		return;
85073c34adc5Sramat 	}
85083c34adc5Sramat 
85093c34adc5Sramat 	vhbc = kmem_zalloc(sizeof (*vhbc), KM_SLEEP);
85103c34adc5Sramat 
85113c34adc5Sramat 	for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
85123c34adc5Sramat 	    cphci = cphci->cphci_next) {
8513273f4511Sgp 		/* skip phcis that haven't attached before root is available */
8514273f4511Sgp 		if (!modrootloaded && (cphci->cphci_phci == NULL))
8515273f4511Sgp 			continue;
85163c34adc5Sramat 		phbc = kmem_zalloc(sizeof (*phbc), KM_SLEEP);
85173c34adc5Sramat 		phbc->phbc_phci_path = i_ddi_strdup(cphci->cphci_path,
85183c34adc5Sramat 		    KM_SLEEP);
85193c34adc5Sramat 		phbc->phbc_vhbusconfig = vhbc;
85203c34adc5Sramat 		phbc->phbc_next = phbc_head;
85213c34adc5Sramat 		phbc_head = phbc;
85223c34adc5Sramat 		vhbc->vhbc_thr_count++;
85233c34adc5Sramat 	}
85243c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
85253c34adc5Sramat 
85263c34adc5Sramat 	vhbc->vhbc_op = op;
85273c34adc5Sramat 	vhbc->vhbc_op_major = maj;
85283c34adc5Sramat 	vhbc->vhbc_op_flags = NDI_NO_EVENT |
85293c34adc5Sramat 	    (flags & (NDI_CONFIG_REPROBE | NDI_DRV_CONF_REPROBE));
85303c34adc5Sramat 	mutex_init(&vhbc->vhbc_lock, NULL, MUTEX_DEFAULT, NULL);
85313c34adc5Sramat 	cv_init(&vhbc->vhbc_cv, NULL, CV_DRIVER, NULL);
85323c34adc5Sramat 
85333c34adc5Sramat 	/* now create threads to initiate bus config on all phcis in parallel */
85343c34adc5Sramat 	for (phbc = phbc_head; phbc != NULL; phbc = phbc_next) {
85353c34adc5Sramat 		phbc_next = phbc->phbc_next;
85363c34adc5Sramat 		if (mdi_mtc_off)
85373c34adc5Sramat 			bus_config_phci((void *)phbc);
85383c34adc5Sramat 		else
85393c34adc5Sramat 			(void) thread_create(NULL, 0, bus_config_phci, phbc,
85403c34adc5Sramat 			    0, &p0, TS_RUN, minclsyspri);
85413c34adc5Sramat 	}
85423c34adc5Sramat 
85433c34adc5Sramat 	mutex_enter(&vhbc->vhbc_lock);
85443c34adc5Sramat 	/* wait until all threads exit */
85453c34adc5Sramat 	while (vhbc->vhbc_thr_count > 0)
85463c34adc5Sramat 		cv_wait(&vhbc->vhbc_cv, &vhbc->vhbc_lock);
85473c34adc5Sramat 	mutex_exit(&vhbc->vhbc_lock);
85483c34adc5Sramat 
85493c34adc5Sramat 	mutex_destroy(&vhbc->vhbc_lock);
85503c34adc5Sramat 	cv_destroy(&vhbc->vhbc_cv);
85513c34adc5Sramat 	kmem_free(vhbc, sizeof (*vhbc));
85523c34adc5Sramat }
85533c34adc5Sramat 
855467e56d35Sramat /*
855567e56d35Sramat  * Single threaded version of bus_config_all_phcis()
855667e56d35Sramat  */
855767e56d35Sramat static void
st_bus_config_all_phcis(mdi_vhci_config_t * vhc,uint_t flags,ddi_bus_config_op_t op,major_t maj)855867e56d35Sramat st_bus_config_all_phcis(mdi_vhci_config_t *vhc, uint_t flags,
855967e56d35Sramat     ddi_bus_config_op_t op, major_t maj)
856067e56d35Sramat {
856167e56d35Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
856267e56d35Sramat 
856367e56d35Sramat 	single_threaded_vhconfig_enter(vhc);
856467e56d35Sramat 	bus_config_all_phcis(vhcache, flags, op, maj);
856567e56d35Sramat 	single_threaded_vhconfig_exit(vhc);
856667e56d35Sramat }
856767e56d35Sramat 
85683c34adc5Sramat /*
85693c34adc5Sramat  * Perform BUS_CONFIG_ONE on the specified child of the phci.
85703c34adc5Sramat  * The path includes the child component in addition to the phci path.
85713c34adc5Sramat  */
85723c34adc5Sramat static int
bus_config_one_phci_child(char * path)85733c34adc5Sramat bus_config_one_phci_child(char *path)
85743c34adc5Sramat {
85753c34adc5Sramat 	dev_info_t *ph_dip, *child;
85763c34adc5Sramat 	char *devnm;
85773c34adc5Sramat 	int rv = MDI_FAILURE;
85783c34adc5Sramat 
85793c34adc5Sramat 	/* extract the child component of the phci */
85803c34adc5Sramat 	devnm = strrchr(path, '/');
85813c34adc5Sramat 	*devnm++ = '\0';
85823c34adc5Sramat 
85833c34adc5Sramat 	/*
85843c34adc5Sramat 	 * first configure all path components upto phci and then
85853c34adc5Sramat 	 * configure the phci child.
85863c34adc5Sramat 	 */
85873c34adc5Sramat 	if ((ph_dip = e_ddi_hold_devi_by_path(path, 0)) != NULL) {
85883c34adc5Sramat 		if (ndi_devi_config_one(ph_dip, devnm, &child, NDI_NO_EVENT) ==
85893c34adc5Sramat 		    NDI_SUCCESS) {
85903c34adc5Sramat 			/*
85913c34adc5Sramat 			 * release the hold that ndi_devi_config_one() placed
85923c34adc5Sramat 			 */
85933c34adc5Sramat 			ndi_rele_devi(child);
85943c34adc5Sramat 			rv = MDI_SUCCESS;
85953c34adc5Sramat 		}
85963c34adc5Sramat 
85973c34adc5Sramat 		/* release the hold that e_ddi_hold_devi_by_path() placed */
85983c34adc5Sramat 		ndi_rele_devi(ph_dip);
85993c34adc5Sramat 	}
86003c34adc5Sramat 
86013c34adc5Sramat 	devnm--;
86023c34adc5Sramat 	*devnm = '/';
86033c34adc5Sramat 	return (rv);
86043c34adc5Sramat }
86053c34adc5Sramat 
86063c34adc5Sramat /*
86073c34adc5Sramat  * Build a list of phci client paths for the specified vhci client.
86083c34adc5Sramat  * The list includes only those phci client paths which aren't configured yet.
86093c34adc5Sramat  */
86103c34adc5Sramat static mdi_phys_path_t *
build_phclient_path_list(mdi_vhcache_client_t * cct,char * ct_name)86113c34adc5Sramat build_phclient_path_list(mdi_vhcache_client_t *cct, char *ct_name)
86123c34adc5Sramat {
86133c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
86143c34adc5Sramat 	mdi_phys_path_t *pp_head = NULL, *pp_tail = NULL, *pp;
86153c34adc5Sramat 	int config_path, len;
86163c34adc5Sramat 
86173c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
86183c34adc5Sramat 		/*
86193c34adc5Sramat 		 * include only those paths that aren't configured.
86203c34adc5Sramat 		 */
86213c34adc5Sramat 		config_path = 0;
86223c34adc5Sramat 		if (cpi->cpi_pip == NULL)
86233c34adc5Sramat 			config_path = 1;
86243c34adc5Sramat 		else {
86253c34adc5Sramat 			MDI_PI_LOCK(cpi->cpi_pip);
86263c34adc5Sramat 			if (MDI_PI_IS_INIT(cpi->cpi_pip))
86273c34adc5Sramat 				config_path = 1;
86283c34adc5Sramat 			MDI_PI_UNLOCK(cpi->cpi_pip);
86293c34adc5Sramat 		}
86303c34adc5Sramat 
86313c34adc5Sramat 		if (config_path) {
86323c34adc5Sramat 			pp = kmem_alloc(sizeof (*pp), KM_SLEEP);
86333c34adc5Sramat 			len = strlen(cpi->cpi_cphci->cphci_path) +
86343c34adc5Sramat 			    strlen(ct_name) + strlen(cpi->cpi_addr) + 3;
86353c34adc5Sramat 			pp->phys_path = kmem_alloc(len, KM_SLEEP);
86363c34adc5Sramat 			(void) snprintf(pp->phys_path, len, "%s/%s@%s",
86373c34adc5Sramat 			    cpi->cpi_cphci->cphci_path, ct_name,
86383c34adc5Sramat 			    cpi->cpi_addr);
86393c34adc5Sramat 			pp->phys_path_next = NULL;
86403c34adc5Sramat 
86413c34adc5Sramat 			if (pp_head == NULL)
86423c34adc5Sramat 				pp_head = pp;
86433c34adc5Sramat 			else
86443c34adc5Sramat 				pp_tail->phys_path_next = pp;
86453c34adc5Sramat 			pp_tail = pp;
86463c34adc5Sramat 		}
86473c34adc5Sramat 	}
86483c34adc5Sramat 
86493c34adc5Sramat 	return (pp_head);
86503c34adc5Sramat }
86513c34adc5Sramat 
86523c34adc5Sramat /*
86533c34adc5Sramat  * Free the memory allocated for phci client path list.
86543c34adc5Sramat  */
86553c34adc5Sramat static void
free_phclient_path_list(mdi_phys_path_t * pp_head)86563c34adc5Sramat free_phclient_path_list(mdi_phys_path_t *pp_head)
86573c34adc5Sramat {
86583c34adc5Sramat 	mdi_phys_path_t *pp, *pp_next;
86593c34adc5Sramat 
86603c34adc5Sramat 	for (pp = pp_head; pp != NULL; pp = pp_next) {
86613c34adc5Sramat 		pp_next = pp->phys_path_next;
86623c34adc5Sramat 		kmem_free(pp->phys_path, strlen(pp->phys_path) + 1);
86633c34adc5Sramat 		kmem_free(pp, sizeof (*pp));
86643c34adc5Sramat 	}
86653c34adc5Sramat }
86663c34adc5Sramat 
86673c34adc5Sramat /*
86683c34adc5Sramat  * Allocated async client structure and initialize with the specified values.
86693c34adc5Sramat  */
86703c34adc5Sramat static mdi_async_client_config_t *
alloc_async_client_config(char * ct_name,char * ct_addr,mdi_phys_path_t * pp_head,mdi_vhcache_lookup_token_t * tok)86713c34adc5Sramat alloc_async_client_config(char *ct_name, char *ct_addr,
86723c34adc5Sramat     mdi_phys_path_t *pp_head, mdi_vhcache_lookup_token_t *tok)
86733c34adc5Sramat {
86743c34adc5Sramat 	mdi_async_client_config_t *acc;
86753c34adc5Sramat 
86763c34adc5Sramat 	acc = kmem_alloc(sizeof (*acc), KM_SLEEP);
86773c34adc5Sramat 	acc->acc_ct_name = i_ddi_strdup(ct_name, KM_SLEEP);
86783c34adc5Sramat 	acc->acc_ct_addr = i_ddi_strdup(ct_addr, KM_SLEEP);
86793c34adc5Sramat 	acc->acc_phclient_path_list_head = pp_head;
86803c34adc5Sramat 	init_vhcache_lookup_token(&acc->acc_token, tok);
86813c34adc5Sramat 	acc->acc_next = NULL;
86823c34adc5Sramat 	return (acc);
86833c34adc5Sramat }
86843c34adc5Sramat 
86853c34adc5Sramat /*
86863c34adc5Sramat  * Free the memory allocated for the async client structure and their members.
86873c34adc5Sramat  */
86883c34adc5Sramat static void
free_async_client_config(mdi_async_client_config_t * acc)86893c34adc5Sramat free_async_client_config(mdi_async_client_config_t *acc)
86903c34adc5Sramat {
86913c34adc5Sramat 	if (acc->acc_phclient_path_list_head)
86923c34adc5Sramat 		free_phclient_path_list(acc->acc_phclient_path_list_head);
86933c34adc5Sramat 	kmem_free(acc->acc_ct_name, strlen(acc->acc_ct_name) + 1);
86943c34adc5Sramat 	kmem_free(acc->acc_ct_addr, strlen(acc->acc_ct_addr) + 1);
86953c34adc5Sramat 	kmem_free(acc, sizeof (*acc));
86963c34adc5Sramat }
86973c34adc5Sramat 
86983c34adc5Sramat /*
86993c34adc5Sramat  * Sort vhcache pathinfos (cpis) of the specified client.
87003c34adc5Sramat  * All cpis which do not have MDI_CPI_HINT_PATH_DOES_NOT_EXIST
87013c34adc5Sramat  * flag set come at the beginning of the list. All cpis which have this
87023c34adc5Sramat  * flag set come at the end of the list.
87033c34adc5Sramat  */
87043c34adc5Sramat static void
sort_vhcache_paths(mdi_vhcache_client_t * cct)87053c34adc5Sramat sort_vhcache_paths(mdi_vhcache_client_t *cct)
87063c34adc5Sramat {
87073c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi, *cpi_next, *cpi_head;
87083c34adc5Sramat 
87093c34adc5Sramat 	cpi_head = cct->cct_cpi_head;
87103c34adc5Sramat 	cct->cct_cpi_head = cct->cct_cpi_tail = NULL;
87113c34adc5Sramat 	for (cpi = cpi_head; cpi != NULL; cpi = cpi_next) {
87123c34adc5Sramat 		cpi_next = cpi->cpi_next;
87133c34adc5Sramat 		enqueue_vhcache_pathinfo(cct, cpi);
87143c34adc5Sramat 	}
87153c34adc5Sramat }
87163c34adc5Sramat 
87173c34adc5Sramat /*
87183c34adc5Sramat  * Verify whether MDI_CPI_HINT_PATH_DOES_NOT_EXIST flag setting is correct for
87193c34adc5Sramat  * every vhcache pathinfo of the specified client. If not adjust the flag
87203c34adc5Sramat  * setting appropriately.
87213c34adc5Sramat  *
87223c34adc5Sramat  * Note that MDI_CPI_HINT_PATH_DOES_NOT_EXIST flag is persisted in the
87233c34adc5Sramat  * on-disk vhci cache. So every time this flag is updated the cache must be
87243c34adc5Sramat  * flushed.
87253c34adc5Sramat  */
87263c34adc5Sramat static void
adjust_sort_vhcache_paths(mdi_vhci_config_t * vhc,char * ct_name,char * ct_addr,mdi_vhcache_lookup_token_t * tok)87273c34adc5Sramat adjust_sort_vhcache_paths(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr,
87283c34adc5Sramat     mdi_vhcache_lookup_token_t *tok)
87293c34adc5Sramat {
87303c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
87313c34adc5Sramat 	mdi_vhcache_client_t *cct;
87323c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
87333c34adc5Sramat 
87343c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
87353c34adc5Sramat 	if ((cct = lookup_vhcache_client(vhcache, ct_name, ct_addr, tok))
87363c34adc5Sramat 	    == NULL) {
87373c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
87383c34adc5Sramat 		return;
87393c34adc5Sramat 	}
87403c34adc5Sramat 
87413c34adc5Sramat 	/*
87423c34adc5Sramat 	 * to avoid unnecessary on-disk cache updates, first check if an
87433c34adc5Sramat 	 * update is really needed. If no update is needed simply return.
87443c34adc5Sramat 	 */
87453c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
87463c34adc5Sramat 		if ((cpi->cpi_pip != NULL &&
87473c34adc5Sramat 		    (cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST)) ||
87483c34adc5Sramat 		    (cpi->cpi_pip == NULL &&
87493c34adc5Sramat 		    !(cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST))) {
87503c34adc5Sramat 			break;
87513c34adc5Sramat 		}
87523c34adc5Sramat 	}
87533c34adc5Sramat 	if (cpi == NULL) {
87543c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
87553c34adc5Sramat 		return;
87563c34adc5Sramat 	}
87573c34adc5Sramat 
87583c34adc5Sramat 	if (rw_tryupgrade(&vhcache->vhcache_lock) == 0) {
87593c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
87603c34adc5Sramat 		rw_enter(&vhcache->vhcache_lock, RW_WRITER);
87613c34adc5Sramat 		if ((cct = lookup_vhcache_client(vhcache, ct_name, ct_addr,
87623c34adc5Sramat 		    tok)) == NULL) {
87633c34adc5Sramat 			rw_exit(&vhcache->vhcache_lock);
87643c34adc5Sramat 			return;
87653c34adc5Sramat 		}
87663c34adc5Sramat 	}
87673c34adc5Sramat 
87683c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
87693c34adc5Sramat 		if (cpi->cpi_pip != NULL)
87703c34adc5Sramat 			cpi->cpi_flags &= ~MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
87713c34adc5Sramat 		else
87723c34adc5Sramat 			cpi->cpi_flags |= MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
87733c34adc5Sramat 	}
87743c34adc5Sramat 	sort_vhcache_paths(cct);
87753c34adc5Sramat 
87763c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
87773c34adc5Sramat 	vhcache_dirty(vhc);
87783c34adc5Sramat }
87793c34adc5Sramat 
87803c34adc5Sramat /*
87813c34adc5Sramat  * Configure all specified paths of the client.
87823c34adc5Sramat  */
87833c34adc5Sramat static void
config_client_paths_sync(mdi_vhci_config_t * vhc,char * ct_name,char * ct_addr,mdi_phys_path_t * pp_head,mdi_vhcache_lookup_token_t * tok)87843c34adc5Sramat config_client_paths_sync(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr,
87853c34adc5Sramat     mdi_phys_path_t *pp_head, mdi_vhcache_lookup_token_t *tok)
87863c34adc5Sramat {
87873c34adc5Sramat 	mdi_phys_path_t *pp;
87883c34adc5Sramat 
87893c34adc5Sramat 	for (pp = pp_head; pp != NULL; pp = pp->phys_path_next)
87903c34adc5Sramat 		(void) bus_config_one_phci_child(pp->phys_path);
87913c34adc5Sramat 	adjust_sort_vhcache_paths(vhc, ct_name, ct_addr, tok);
87923c34adc5Sramat }
87933c34adc5Sramat 
87943c34adc5Sramat /*
87953c34adc5Sramat  * Dequeue elements from vhci async client config list and bus configure
87963c34adc5Sramat  * their corresponding phci clients.
87973c34adc5Sramat  */
87983c34adc5Sramat static void
config_client_paths_thread(void * arg)87993c34adc5Sramat config_client_paths_thread(void *arg)
88003c34adc5Sramat {
88013c34adc5Sramat 	mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
88023c34adc5Sramat 	mdi_async_client_config_t *acc;
88033c34adc5Sramat 	clock_t quit_at_ticks;
88043c34adc5Sramat 	clock_t idle_time = mdi_async_config_idle_time * TICKS_PER_SECOND;
88053c34adc5Sramat 	callb_cpr_t cprinfo;
88063c34adc5Sramat 
88073c34adc5Sramat 	CALLB_CPR_INIT(&cprinfo, &vhc->vhc_lock, callb_generic_cpr,
88083c34adc5Sramat 	    "mdi_config_client_paths");
88093c34adc5Sramat 
88103c34adc5Sramat 	for (; ; ) {
88113c34adc5Sramat 		quit_at_ticks = ddi_get_lbolt() + idle_time;
88123c34adc5Sramat 
88133c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
88143c34adc5Sramat 		while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
88153c34adc5Sramat 		    vhc->vhc_acc_list_head == NULL &&
88163c34adc5Sramat 		    ddi_get_lbolt() < quit_at_ticks) {
88173c34adc5Sramat 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
88183c34adc5Sramat 			(void) cv_timedwait(&vhc->vhc_cv, &vhc->vhc_lock,
88193c34adc5Sramat 			    quit_at_ticks);
88203c34adc5Sramat 			CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
88213c34adc5Sramat 		}
88223c34adc5Sramat 
88233c34adc5Sramat 		if ((vhc->vhc_flags & MDI_VHC_EXIT) ||
88243c34adc5Sramat 		    vhc->vhc_acc_list_head == NULL)
88253c34adc5Sramat 			goto out;
88263c34adc5Sramat 
88273c34adc5Sramat 		acc = vhc->vhc_acc_list_head;
88283c34adc5Sramat 		vhc->vhc_acc_list_head = acc->acc_next;
88293c34adc5Sramat 		if (vhc->vhc_acc_list_head == NULL)
88303c34adc5Sramat 			vhc->vhc_acc_list_tail = NULL;
88313c34adc5Sramat 		vhc->vhc_acc_count--;
88323c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
88333c34adc5Sramat 
88343c34adc5Sramat 		config_client_paths_sync(vhc, acc->acc_ct_name,
88353c34adc5Sramat 		    acc->acc_ct_addr, acc->acc_phclient_path_list_head,
88363c34adc5Sramat 		    &acc->acc_token);
88373c34adc5Sramat 
88383c34adc5Sramat 		free_async_client_config(acc);
88393c34adc5Sramat 	}
88403c34adc5Sramat 
88413c34adc5Sramat out:
88423c34adc5Sramat 	vhc->vhc_acc_thrcount--;
88433c34adc5Sramat 	/* CALLB_CPR_EXIT releases the vhc->vhc_lock */
88443c34adc5Sramat 	CALLB_CPR_EXIT(&cprinfo);
88453c34adc5Sramat }
88463c34adc5Sramat 
88473c34adc5Sramat /*
88483c34adc5Sramat  * Arrange for all the phci client paths (pp_head) for the specified client
88493c34adc5Sramat  * to be bus configured asynchronously by a thread.
88503c34adc5Sramat  */
88513c34adc5Sramat static void
config_client_paths_async(mdi_vhci_config_t * vhc,char * ct_name,char * ct_addr,mdi_phys_path_t * pp_head,mdi_vhcache_lookup_token_t * tok)88523c34adc5Sramat config_client_paths_async(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr,
88533c34adc5Sramat     mdi_phys_path_t *pp_head, mdi_vhcache_lookup_token_t *tok)
88543c34adc5Sramat {
88553c34adc5Sramat 	mdi_async_client_config_t *acc, *newacc;
88563c34adc5Sramat 	int create_thread;
88573c34adc5Sramat 
88583c34adc5Sramat 	if (pp_head == NULL)
88593c34adc5Sramat 		return;
88603c34adc5Sramat 
88613c34adc5Sramat 	if (mdi_mtc_off) {
88623c34adc5Sramat 		config_client_paths_sync(vhc, ct_name, ct_addr, pp_head, tok);
88633c34adc5Sramat 		free_phclient_path_list(pp_head);
88643c34adc5Sramat 		return;
88653c34adc5Sramat 	}
88663c34adc5Sramat 
88673c34adc5Sramat 	newacc = alloc_async_client_config(ct_name, ct_addr, pp_head, tok);
88683c34adc5Sramat 	ASSERT(newacc);
88693c34adc5Sramat 
88703c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
88713c34adc5Sramat 	for (acc = vhc->vhc_acc_list_head; acc != NULL; acc = acc->acc_next) {
88723c34adc5Sramat 		if (strcmp(ct_name, acc->acc_ct_name) == 0 &&
88733c34adc5Sramat 		    strcmp(ct_addr, acc->acc_ct_addr) == 0) {
88743c34adc5Sramat 			free_async_client_config(newacc);
88753c34adc5Sramat 			mutex_exit(&vhc->vhc_lock);
88763c34adc5Sramat 			return;
88773c34adc5Sramat 		}
88783c34adc5Sramat 	}
88793c34adc5Sramat 
88803c34adc5Sramat 	if (vhc->vhc_acc_list_head == NULL)
88813c34adc5Sramat 		vhc->vhc_acc_list_head = newacc;
88823c34adc5Sramat 	else
88833c34adc5Sramat 		vhc->vhc_acc_list_tail->acc_next = newacc;
88843c34adc5Sramat 	vhc->vhc_acc_list_tail = newacc;
88853c34adc5Sramat 	vhc->vhc_acc_count++;
88863c34adc5Sramat 	if (vhc->vhc_acc_count <= vhc->vhc_acc_thrcount) {
88873c34adc5Sramat 		cv_broadcast(&vhc->vhc_cv);
88883c34adc5Sramat 		create_thread = 0;
88893c34adc5Sramat 	} else {
88903c34adc5Sramat 		vhc->vhc_acc_thrcount++;
88913c34adc5Sramat 		create_thread = 1;
88923c34adc5Sramat 	}
88933c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
88943c34adc5Sramat 
88953c34adc5Sramat 	if (create_thread)
88963c34adc5Sramat 		(void) thread_create(NULL, 0, config_client_paths_thread, vhc,
88973c34adc5Sramat 		    0, &p0, TS_RUN, minclsyspri);
88983c34adc5Sramat }
88993c34adc5Sramat 
89003c34adc5Sramat /*
89013c34adc5Sramat  * Return number of online paths for the specified client.
89023c34adc5Sramat  */
89033c34adc5Sramat static int
nonline_paths(mdi_vhcache_client_t * cct)89043c34adc5Sramat nonline_paths(mdi_vhcache_client_t *cct)
89053c34adc5Sramat {
89063c34adc5Sramat 	mdi_vhcache_pathinfo_t *cpi;
89073c34adc5Sramat 	int online_count = 0;
89083c34adc5Sramat 
89093c34adc5Sramat 	for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
89103c34adc5Sramat 		if (cpi->cpi_pip != NULL) {
89113c34adc5Sramat 			MDI_PI_LOCK(cpi->cpi_pip);
89123c34adc5Sramat 			if (cpi->cpi_pip->pi_state == MDI_PATHINFO_STATE_ONLINE)
89133c34adc5Sramat 				online_count++;
89143c34adc5Sramat 			MDI_PI_UNLOCK(cpi->cpi_pip);
89153c34adc5Sramat 		}
89163c34adc5Sramat 	}
89173c34adc5Sramat 
89183c34adc5Sramat 	return (online_count);
89193c34adc5Sramat }
89203c34adc5Sramat 
89213c34adc5Sramat /*
89223c34adc5Sramat  * Bus configure all paths for the specified vhci client.
89233c34adc5Sramat  * If at least one path for the client is already online, the remaining paths
89243c34adc5Sramat  * will be configured asynchronously. Otherwise, it synchronously configures
89253c34adc5Sramat  * the paths until at least one path is online and then rest of the paths
89263c34adc5Sramat  * will be configured asynchronously.
89273c34adc5Sramat  */
89283c34adc5Sramat static void
config_client_paths(mdi_vhci_config_t * vhc,char * ct_name,char * ct_addr)89293c34adc5Sramat config_client_paths(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr)
89303c34adc5Sramat {
89313c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
89323c34adc5Sramat 	mdi_phys_path_t *pp_head, *pp;
89333c34adc5Sramat 	mdi_vhcache_client_t *cct;
89343c34adc5Sramat 	mdi_vhcache_lookup_token_t tok;
89353c34adc5Sramat 
89363c34adc5Sramat 	ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
89373c34adc5Sramat 
89383c34adc5Sramat 	init_vhcache_lookup_token(&tok, NULL);
89393c34adc5Sramat 
89403c34adc5Sramat 	if (ct_name == NULL || ct_addr == NULL ||
89413c34adc5Sramat 	    (cct = lookup_vhcache_client(vhcache, ct_name, ct_addr, &tok))
89423c34adc5Sramat 	    == NULL ||
89433c34adc5Sramat 	    (pp_head = build_phclient_path_list(cct, ct_name)) == NULL) {
89443c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
89453c34adc5Sramat 		return;
89463c34adc5Sramat 	}
89473c34adc5Sramat 
89483c34adc5Sramat 	/* if at least one path is online, configure the rest asynchronously */
89493c34adc5Sramat 	if (nonline_paths(cct) > 0) {
89503c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
89513c34adc5Sramat 		config_client_paths_async(vhc, ct_name, ct_addr, pp_head, &tok);
89523c34adc5Sramat 		return;
89533c34adc5Sramat 	}
89543c34adc5Sramat 
89553c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
89563c34adc5Sramat 
89573c34adc5Sramat 	for (pp = pp_head; pp != NULL; pp = pp->phys_path_next) {
89583c34adc5Sramat 		if (bus_config_one_phci_child(pp->phys_path) == MDI_SUCCESS) {
89593c34adc5Sramat 			rw_enter(&vhcache->vhcache_lock, RW_READER);
89603c34adc5Sramat 
89613c34adc5Sramat 			if ((cct = lookup_vhcache_client(vhcache, ct_name,
89623c34adc5Sramat 			    ct_addr, &tok)) == NULL) {
89633c34adc5Sramat 				rw_exit(&vhcache->vhcache_lock);
89643c34adc5Sramat 				goto out;
89653c34adc5Sramat 			}
89663c34adc5Sramat 
89673c34adc5Sramat 			if (nonline_paths(cct) > 0 &&
89683c34adc5Sramat 			    pp->phys_path_next != NULL) {
89693c34adc5Sramat 				rw_exit(&vhcache->vhcache_lock);
89703c34adc5Sramat 				config_client_paths_async(vhc, ct_name, ct_addr,
89713c34adc5Sramat 				    pp->phys_path_next, &tok);
89723c34adc5Sramat 				pp->phys_path_next = NULL;
89733c34adc5Sramat 				goto out;
89743c34adc5Sramat 			}
89753c34adc5Sramat 
89763c34adc5Sramat 			rw_exit(&vhcache->vhcache_lock);
89773c34adc5Sramat 		}
89783c34adc5Sramat 	}
89793c34adc5Sramat 
89803c34adc5Sramat 	adjust_sort_vhcache_paths(vhc, ct_name, ct_addr, &tok);
89813c34adc5Sramat out:
89823c34adc5Sramat 	free_phclient_path_list(pp_head);
89833c34adc5Sramat }
89843c34adc5Sramat 
89853c34adc5Sramat static void
single_threaded_vhconfig_enter(mdi_vhci_config_t * vhc)89863c34adc5Sramat single_threaded_vhconfig_enter(mdi_vhci_config_t *vhc)
89873c34adc5Sramat {
89883c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
89893c34adc5Sramat 	while (vhc->vhc_flags & MDI_VHC_SINGLE_THREADED)
89903c34adc5Sramat 		cv_wait(&vhc->vhc_cv, &vhc->vhc_lock);
89913c34adc5Sramat 	vhc->vhc_flags |= MDI_VHC_SINGLE_THREADED;
89923c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
89933c34adc5Sramat }
89943c34adc5Sramat 
89953c34adc5Sramat static void
single_threaded_vhconfig_exit(mdi_vhci_config_t * vhc)89963c34adc5Sramat single_threaded_vhconfig_exit(mdi_vhci_config_t *vhc)
89973c34adc5Sramat {
89983c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
89993c34adc5Sramat 	vhc->vhc_flags &= ~MDI_VHC_SINGLE_THREADED;
90003c34adc5Sramat 	cv_broadcast(&vhc->vhc_cv);
90013c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
90023c34adc5Sramat }
90033c34adc5Sramat 
900452cac543Sramat typedef struct mdi_phci_driver_info {
900552cac543Sramat 	char	*phdriver_name;	/* name of the phci driver */
900652cac543Sramat 
900752cac543Sramat 	/* set to non zero if the phci driver supports root device */
900852cac543Sramat 	int	phdriver_root_support;
900952cac543Sramat } mdi_phci_driver_info_t;
901052cac543Sramat 
901152cac543Sramat /*
901252cac543Sramat  * vhci class and root support capability of a phci driver can be
901352cac543Sramat  * specified using ddi-vhci-class and ddi-no-root-support properties in the
901452cac543Sramat  * phci driver.conf file. The built-in tables below contain this information
901552cac543Sramat  * for those phci drivers whose driver.conf files don't yet contain this info.
901652cac543Sramat  *
901752cac543Sramat  * All phci drivers expect iscsi have root device support.
901852cac543Sramat  */
901952cac543Sramat static mdi_phci_driver_info_t scsi_phci_driver_list[] = {
902052cac543Sramat 	{ "fp", 1 },
902152cac543Sramat 	{ "iscsi", 0 },
902252cac543Sramat 	{ "ibsrp", 1 }
902352cac543Sramat 	};
902452cac543Sramat 
902552cac543Sramat static mdi_phci_driver_info_t ib_phci_driver_list[] = { "tavor", 1 };
902652cac543Sramat 
902752cac543Sramat static void *
mdi_realloc(void * old_ptr,size_t old_size,size_t new_size)902852cac543Sramat mdi_realloc(void *old_ptr, size_t old_size, size_t new_size)
902952cac543Sramat {
903052cac543Sramat 	void *new_ptr;
903152cac543Sramat 
903252cac543Sramat 	new_ptr = kmem_zalloc(new_size, KM_SLEEP);
903352cac543Sramat 	if (old_ptr) {
9034f7209cf2Spramodbg 		bcopy(old_ptr, new_ptr, MIN(old_size, new_size));
903552cac543Sramat 		kmem_free(old_ptr, old_size);
903652cac543Sramat 	}
903752cac543Sramat 	return (new_ptr);
903852cac543Sramat }
903952cac543Sramat 
904052cac543Sramat static void
add_to_phci_list(char *** driver_list,int ** root_support_list,int * cur_elements,int * max_elements,char * driver_name,int root_support)904152cac543Sramat add_to_phci_list(char ***driver_list, int **root_support_list,
904252cac543Sramat     int *cur_elements, int *max_elements, char *driver_name, int root_support)
904352cac543Sramat {
904452cac543Sramat 	ASSERT(*cur_elements <= *max_elements);
904552cac543Sramat 	if (*cur_elements == *max_elements) {
904652cac543Sramat 		*max_elements += 10;
904752cac543Sramat 		*driver_list = mdi_realloc(*driver_list,
904852cac543Sramat 		    sizeof (char *) * (*cur_elements),
904952cac543Sramat 		    sizeof (char *) * (*max_elements));
905052cac543Sramat 		*root_support_list = mdi_realloc(*root_support_list,
905152cac543Sramat 		    sizeof (int) * (*cur_elements),
905252cac543Sramat 		    sizeof (int) * (*max_elements));
905352cac543Sramat 	}
905452cac543Sramat 	(*driver_list)[*cur_elements] = i_ddi_strdup(driver_name, KM_SLEEP);
905552cac543Sramat 	(*root_support_list)[*cur_elements] = root_support;
905652cac543Sramat 	(*cur_elements)++;
905752cac543Sramat }
905852cac543Sramat 
905952cac543Sramat static void
get_phci_driver_list(char * vhci_class,char *** driver_list,int ** root_support_list,int * cur_elements,int * max_elements)906052cac543Sramat get_phci_driver_list(char *vhci_class, char ***driver_list,
906152cac543Sramat     int **root_support_list, int *cur_elements, int *max_elements)
906252cac543Sramat {
906352cac543Sramat 	mdi_phci_driver_info_t	*st_driver_list, *p;
906452cac543Sramat 	int		st_ndrivers, root_support, i, j, driver_conf_count;
906552cac543Sramat 	major_t		m;
906652cac543Sramat 	struct devnames	*dnp;
906752cac543Sramat 	ddi_prop_t	*propp;
906852cac543Sramat 
906952cac543Sramat 	*driver_list = NULL;
907052cac543Sramat 	*root_support_list = NULL;
907152cac543Sramat 	*cur_elements = 0;
907252cac543Sramat 	*max_elements = 0;
907352cac543Sramat 
907452cac543Sramat 	/* add the phci drivers derived from the phci driver.conf files */
907552cac543Sramat 	for (m = 0; m < devcnt; m++) {
907652cac543Sramat 		dnp = &devnamesp[m];
907752cac543Sramat 
907852cac543Sramat 		if (dnp->dn_flags & DN_PHCI_DRIVER) {
907952cac543Sramat 			LOCK_DEV_OPS(&dnp->dn_lock);
908052cac543Sramat 			if (dnp->dn_global_prop_ptr != NULL &&
908152cac543Sramat 			    (propp = i_ddi_prop_search(DDI_DEV_T_ANY,
908252cac543Sramat 			    DDI_VHCI_CLASS, DDI_PROP_TYPE_STRING,
908352cac543Sramat 			    &dnp->dn_global_prop_ptr->prop_list)) != NULL &&
908452cac543Sramat 			    strcmp(propp->prop_val, vhci_class) == 0) {
908552cac543Sramat 
908652cac543Sramat 				root_support = (i_ddi_prop_search(DDI_DEV_T_ANY,
908752cac543Sramat 				    DDI_NO_ROOT_SUPPORT, DDI_PROP_TYPE_INT,
908852cac543Sramat 				    &dnp->dn_global_prop_ptr->prop_list)
908952cac543Sramat 				    == NULL) ? 1 : 0;
909052cac543Sramat 
909152cac543Sramat 				add_to_phci_list(driver_list, root_support_list,
909252cac543Sramat 				    cur_elements, max_elements, dnp->dn_name,
909352cac543Sramat 				    root_support);
909452cac543Sramat 
909552cac543Sramat 				UNLOCK_DEV_OPS(&dnp->dn_lock);
909652cac543Sramat 			} else
909752cac543Sramat 				UNLOCK_DEV_OPS(&dnp->dn_lock);
909852cac543Sramat 		}
909952cac543Sramat 	}
910052cac543Sramat 
910152cac543Sramat 	driver_conf_count = *cur_elements;
910252cac543Sramat 
910352cac543Sramat 	/* add the phci drivers specified in the built-in tables */
910452cac543Sramat 	if (strcmp(vhci_class, MDI_HCI_CLASS_SCSI) == 0) {
910552cac543Sramat 		st_driver_list = scsi_phci_driver_list;
910652cac543Sramat 		st_ndrivers = sizeof (scsi_phci_driver_list) /
910752cac543Sramat 		    sizeof (mdi_phci_driver_info_t);
910852cac543Sramat 	} else if (strcmp(vhci_class, MDI_HCI_CLASS_IB) == 0) {
910952cac543Sramat 		st_driver_list = ib_phci_driver_list;
911052cac543Sramat 		st_ndrivers = sizeof (ib_phci_driver_list) /
911152cac543Sramat 		    sizeof (mdi_phci_driver_info_t);
911252cac543Sramat 	} else {
911352cac543Sramat 		st_driver_list = NULL;
911452cac543Sramat 		st_ndrivers = 0;
911552cac543Sramat 	}
911652cac543Sramat 
911752cac543Sramat 	for (i = 0, p = st_driver_list; i < st_ndrivers; i++, p++) {
911852cac543Sramat 		/* add this phci driver if not already added before */
911952cac543Sramat 		for (j = 0; j < driver_conf_count; j++) {
912052cac543Sramat 			if (strcmp((*driver_list)[j], p->phdriver_name) == 0)
912152cac543Sramat 				break;
912252cac543Sramat 		}
912352cac543Sramat 		if (j == driver_conf_count) {
912452cac543Sramat 			add_to_phci_list(driver_list, root_support_list,
912552cac543Sramat 			    cur_elements, max_elements, p->phdriver_name,
912652cac543Sramat 			    p->phdriver_root_support);
912752cac543Sramat 		}
912852cac543Sramat 	}
912952cac543Sramat }
913052cac543Sramat 
91313c34adc5Sramat /*
913252cac543Sramat  * Attach the phci driver instances associated with the specified vhci class.
91333c34adc5Sramat  * If root is mounted attach all phci driver instances.
91343c34adc5Sramat  * If root is not mounted, attach the instances of only those phci
91353c34adc5Sramat  * drivers that have the root support.
91363c34adc5Sramat  */
91373c34adc5Sramat static void
attach_phci_drivers(char * vhci_class)913852cac543Sramat attach_phci_drivers(char *vhci_class)
91393c34adc5Sramat {
914052cac543Sramat 	char	**driver_list, **p;
914152cac543Sramat 	int	*root_support_list;
914252cac543Sramat 	int	cur_elements, max_elements, i;
914352cac543Sramat 	major_t	m;
91443c34adc5Sramat 
914552cac543Sramat 	get_phci_driver_list(vhci_class, &driver_list, &root_support_list,
914652cac543Sramat 	    &cur_elements, &max_elements);
91473c34adc5Sramat 
914852cac543Sramat 	for (i = 0; i < cur_elements; i++) {
914952cac543Sramat 		if (modrootloaded || root_support_list[i]) {
915052cac543Sramat 			m = ddi_name_to_major(driver_list[i]);
9151a204de77Scth 			if (m != DDI_MAJOR_T_NONE &&
9152a204de77Scth 			    ddi_hold_installed_driver(m))
91533c34adc5Sramat 				ddi_rele_driver(m);
91543c34adc5Sramat 		}
91553c34adc5Sramat 	}
915652cac543Sramat 
915752cac543Sramat 	if (driver_list) {
915852cac543Sramat 		for (i = 0, p = driver_list; i < cur_elements; i++, p++)
915952cac543Sramat 			kmem_free(*p, strlen(*p) + 1);
916052cac543Sramat 		kmem_free(driver_list, sizeof (char *) * max_elements);
916152cac543Sramat 		kmem_free(root_support_list, sizeof (int) * max_elements);
916252cac543Sramat 	}
91633c34adc5Sramat }
91643c34adc5Sramat 
91653c34adc5Sramat /*
91663c34adc5Sramat  * Build vhci cache:
91673c34adc5Sramat  *
91683c34adc5Sramat  * Attach phci driver instances and then drive BUS_CONFIG_ALL on
91693c34adc5Sramat  * the phci driver instances. During this process the cache gets built.
91703c34adc5Sramat  *
917167e56d35Sramat  * Cache is built fully if the root is mounted.
91723c34adc5Sramat  * If the root is not mounted, phci drivers that do not have root support
91733c34adc5Sramat  * are not attached. As a result the cache is built partially. The entries
91743c34adc5Sramat  * in the cache reflect only those phci drivers that have root support.
91753c34adc5Sramat  */
917667e56d35Sramat static int
build_vhci_cache(mdi_vhci_t * vh)917752cac543Sramat build_vhci_cache(mdi_vhci_t *vh)
91783c34adc5Sramat {
917952cac543Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
91803c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
91813c34adc5Sramat 
918267e56d35Sramat 	single_threaded_vhconfig_enter(vhc);
918367e56d35Sramat 
91843c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
91853c34adc5Sramat 	if (vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE) {
91863c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
918767e56d35Sramat 		single_threaded_vhconfig_exit(vhc);
918867e56d35Sramat 		return (0);
91893c34adc5Sramat 	}
91903c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
91913c34adc5Sramat 
919252cac543Sramat 	attach_phci_drivers(vh->vh_class);
91933c34adc5Sramat 	bus_config_all_phcis(vhcache, NDI_DRV_CONF_REPROBE | NDI_NO_EVENT,
9194a204de77Scth 	    BUS_CONFIG_ALL, DDI_MAJOR_T_NONE);
91953c34adc5Sramat 
919667e56d35Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
919767e56d35Sramat 	vhcache->vhcache_flags |= MDI_VHCI_CACHE_SETUP_DONE;
919867e56d35Sramat 	rw_exit(&vhcache->vhcache_lock);
919967e56d35Sramat 
920067e56d35Sramat 	single_threaded_vhconfig_exit(vhc);
920167e56d35Sramat 	vhcache_dirty(vhc);
920267e56d35Sramat 	return (1);
92033c34adc5Sramat }
92043c34adc5Sramat 
92053c34adc5Sramat /*
920667e56d35Sramat  * Determine if discovery of paths is needed.
92073c34adc5Sramat  */
920867e56d35Sramat static int
vhcache_do_discovery(mdi_vhci_config_t * vhc)920967e56d35Sramat vhcache_do_discovery(mdi_vhci_config_t *vhc)
92103c34adc5Sramat {
921167e56d35Sramat 	int rv = 1;
92123c34adc5Sramat 
92133c34adc5Sramat 	mutex_enter(&vhc->vhc_lock);
921467e56d35Sramat 	if (i_ddi_io_initialized() == 0) {
921567e56d35Sramat 		if (vhc->vhc_path_discovery_boot > 0) {
921667e56d35Sramat 			vhc->vhc_path_discovery_boot--;
921767e56d35Sramat 			goto out;
921867e56d35Sramat 		}
921967e56d35Sramat 	} else {
922067e56d35Sramat 		if (vhc->vhc_path_discovery_postboot > 0) {
922167e56d35Sramat 			vhc->vhc_path_discovery_postboot--;
922267e56d35Sramat 			goto out;
922367e56d35Sramat 		}
92243c34adc5Sramat 	}
92253c34adc5Sramat 
92263c34adc5Sramat 	/*
922767e56d35Sramat 	 * Do full path discovery at most once per mdi_path_discovery_interval.
922867e56d35Sramat 	 * This is to avoid a series of full path discoveries when opening
922967e56d35Sramat 	 * stale /dev/[r]dsk links.
92303c34adc5Sramat 	 */
923167e56d35Sramat 	if (mdi_path_discovery_interval != -1 &&
9232d3d50737SRafael Vanoni 	    ddi_get_lbolt64() >= vhc->vhc_path_discovery_cutoff_time)
923367e56d35Sramat 		goto out;
92343c34adc5Sramat 
923567e56d35Sramat 	rv = 0;
92363c34adc5Sramat out:
92373c34adc5Sramat 	mutex_exit(&vhc->vhc_lock);
923867e56d35Sramat 	return (rv);
92393c34adc5Sramat }
92403c34adc5Sramat 
92413c34adc5Sramat /*
924267e56d35Sramat  * Discover all paths:
92433c34adc5Sramat  *
924467e56d35Sramat  * Attach phci driver instances and then drive BUS_CONFIG_ALL on all the phci
924567e56d35Sramat  * driver instances. During this process all paths will be discovered.
92463c34adc5Sramat  */
92473c34adc5Sramat static int
vhcache_discover_paths(mdi_vhci_t * vh)924852cac543Sramat vhcache_discover_paths(mdi_vhci_t *vh)
92493c34adc5Sramat {
925052cac543Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
925167e56d35Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
925267e56d35Sramat 	int rv = 0;
92533c34adc5Sramat 
92543c34adc5Sramat 	single_threaded_vhconfig_enter(vhc);
92553c34adc5Sramat 
925667e56d35Sramat 	if (vhcache_do_discovery(vhc)) {
925752cac543Sramat 		attach_phci_drivers(vh->vh_class);
925867e56d35Sramat 		bus_config_all_phcis(vhcache, NDI_DRV_CONF_REPROBE |
9259a204de77Scth 		    NDI_NO_EVENT, BUS_CONFIG_ALL, DDI_MAJOR_T_NONE);
92603c34adc5Sramat 
92613c34adc5Sramat 		mutex_enter(&vhc->vhc_lock);
9262d3d50737SRafael Vanoni 		vhc->vhc_path_discovery_cutoff_time = ddi_get_lbolt64() +
926367e56d35Sramat 		    mdi_path_discovery_interval * TICKS_PER_SECOND;
92643c34adc5Sramat 		mutex_exit(&vhc->vhc_lock);
926567e56d35Sramat 		rv = 1;
92663c34adc5Sramat 	}
92673c34adc5Sramat 
92683c34adc5Sramat 	single_threaded_vhconfig_exit(vhc);
92693c34adc5Sramat 	return (rv);
92703c34adc5Sramat }
92713c34adc5Sramat 
92723c34adc5Sramat /*
92733c34adc5Sramat  * Generic vhci bus config implementation:
92747c478bd9Sstevel@tonic-gate  *
92753c34adc5Sramat  * Parameters
92763c34adc5Sramat  *	vdip	vhci dip
92773c34adc5Sramat  *	flags	bus config flags
92783c34adc5Sramat  *	op	bus config operation
92793c34adc5Sramat  *	The remaining parameters are bus config operation specific
92807c478bd9Sstevel@tonic-gate  *
92813c34adc5Sramat  * for BUS_CONFIG_ONE
92823c34adc5Sramat  *	arg	pointer to name@addr
92833c34adc5Sramat  *	child	upon successful return from this function, *child will be
92843c34adc5Sramat  *		set to the configured and held devinfo child node of vdip.
92853c34adc5Sramat  *	ct_addr	pointer to client address (i.e. GUID)
92867c478bd9Sstevel@tonic-gate  *
92873c34adc5Sramat  * for BUS_CONFIG_DRIVER
92883c34adc5Sramat  *	arg	major number of the driver
92893c34adc5Sramat  *	child and ct_addr parameters are ignored
92907c478bd9Sstevel@tonic-gate  *
92913c34adc5Sramat  * for BUS_CONFIG_ALL
92923c34adc5Sramat  *	arg, child, and ct_addr parameters are ignored
92937c478bd9Sstevel@tonic-gate  *
92943c34adc5Sramat  * Note that for the rest of the bus config operations, this function simply
92953c34adc5Sramat  * calls the framework provided default bus config routine.
92967c478bd9Sstevel@tonic-gate  */
92977c478bd9Sstevel@tonic-gate int
mdi_vhci_bus_config(dev_info_t * vdip,uint_t flags,ddi_bus_config_op_t op,void * arg,dev_info_t ** child,char * ct_addr)92987c478bd9Sstevel@tonic-gate mdi_vhci_bus_config(dev_info_t *vdip, uint_t flags, ddi_bus_config_op_t op,
92993c34adc5Sramat     void *arg, dev_info_t **child, char *ct_addr)
93007c478bd9Sstevel@tonic-gate {
93013c34adc5Sramat 	mdi_vhci_t *vh = i_devi_get_vhci(vdip);
93023c34adc5Sramat 	mdi_vhci_config_t *vhc = vh->vh_config;
93033c34adc5Sramat 	mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
930467e56d35Sramat 	int rv = 0;
930567e56d35Sramat 	int params_valid = 0;
93063c34adc5Sramat 	char *cp;
93077c478bd9Sstevel@tonic-gate 
93087c478bd9Sstevel@tonic-gate 	/*
93095e3986cbScth 	 * To bus config vhcis we relay operation, possibly using another
93105e3986cbScth 	 * thread, to phcis. The phci driver then interacts with MDI to cause
93115e3986cbScth 	 * vhci child nodes to be enumerated under the vhci node.  Adding a
93125e3986cbScth 	 * vhci child requires an ndi_devi_enter of the vhci. Since another
93135e3986cbScth 	 * thread may be adding the child, to avoid deadlock we can't wait
93145e3986cbScth 	 * for the relayed operations to complete if we have already entered
93155e3986cbScth 	 * the vhci node.
93167c478bd9Sstevel@tonic-gate 	 */
93177c478bd9Sstevel@tonic-gate 	if (DEVI_BUSY_OWNED(vdip)) {
93184c06356bSdh 		MDI_DEBUG(2, (MDI_NOTE, vdip,
93194c06356bSdh 		    "vhci dip is busy owned %p", (void *)vdip));
93207c478bd9Sstevel@tonic-gate 		goto default_bus_config;
93217c478bd9Sstevel@tonic-gate 	}
93227c478bd9Sstevel@tonic-gate 
93233c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_READER);
93243c34adc5Sramat 	if (!(vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE)) {
93253c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
932652cac543Sramat 		rv = build_vhci_cache(vh);
93273c34adc5Sramat 		rw_enter(&vhcache->vhcache_lock, RW_READER);
93283c34adc5Sramat 	}
93293c34adc5Sramat 
93307c478bd9Sstevel@tonic-gate 	switch (op) {
93317c478bd9Sstevel@tonic-gate 	case BUS_CONFIG_ONE:
933267e56d35Sramat 		if (arg != NULL && ct_addr != NULL) {
933367e56d35Sramat 			/* extract node name */
933467e56d35Sramat 			cp = (char *)arg;
933567e56d35Sramat 			while (*cp != '\0' && *cp != '@')
933667e56d35Sramat 				cp++;
933767e56d35Sramat 			if (*cp == '@') {
933867e56d35Sramat 				params_valid = 1;
933967e56d35Sramat 				*cp = '\0';
934067e56d35Sramat 				config_client_paths(vhc, (char *)arg, ct_addr);
934167e56d35Sramat 				/* config_client_paths() releases cache_lock */
934267e56d35Sramat 				*cp = '@';
934367e56d35Sramat 				break;
934467e56d35Sramat 			}
934567e56d35Sramat 		}
934667e56d35Sramat 
934767e56d35Sramat 		rw_exit(&vhcache->vhcache_lock);
93487c478bd9Sstevel@tonic-gate 		break;
93497c478bd9Sstevel@tonic-gate 
93507c478bd9Sstevel@tonic-gate 	case BUS_CONFIG_DRIVER:
93513c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
935267e56d35Sramat 		if (rv == 0)
935367e56d35Sramat 			st_bus_config_all_phcis(vhc, flags, op,
93543c34adc5Sramat 			    (major_t)(uintptr_t)arg);
93557c478bd9Sstevel@tonic-gate 		break;
93567c478bd9Sstevel@tonic-gate 
93577c478bd9Sstevel@tonic-gate 	case BUS_CONFIG_ALL:
93583c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
935967e56d35Sramat 		if (rv == 0)
936067e56d35Sramat 			st_bus_config_all_phcis(vhc, flags, op, -1);
93617c478bd9Sstevel@tonic-gate 		break;
93627c478bd9Sstevel@tonic-gate 
93637c478bd9Sstevel@tonic-gate 	default:
93643c34adc5Sramat 		rw_exit(&vhcache->vhcache_lock);
93657c478bd9Sstevel@tonic-gate 		break;
93667c478bd9Sstevel@tonic-gate 	}
93677c478bd9Sstevel@tonic-gate 
93683c34adc5Sramat 
93697c478bd9Sstevel@tonic-gate default_bus_config:
93707c478bd9Sstevel@tonic-gate 	/*
93713c34adc5Sramat 	 * All requested child nodes are enumerated under the vhci.
93723c34adc5Sramat 	 * Now configure them.
93737c478bd9Sstevel@tonic-gate 	 */
93743c34adc5Sramat 	if (ndi_busop_bus_config(vdip, flags, op, arg, child, 0) ==
93757c478bd9Sstevel@tonic-gate 	    NDI_SUCCESS) {
93767c478bd9Sstevel@tonic-gate 		return (MDI_SUCCESS);
937767e56d35Sramat 	} else if (op == BUS_CONFIG_ONE && rv == 0 && params_valid) {
937867e56d35Sramat 		/* discover all paths and try configuring again */
937952cac543Sramat 		if (vhcache_discover_paths(vh) &&
938067e56d35Sramat 		    ndi_busop_bus_config(vdip, flags, op, arg, child, 0) ==
938167e56d35Sramat 		    NDI_SUCCESS)
938267e56d35Sramat 			return (MDI_SUCCESS);
93837c478bd9Sstevel@tonic-gate 	}
93847c478bd9Sstevel@tonic-gate 
93857c478bd9Sstevel@tonic-gate 	return (MDI_FAILURE);
93867c478bd9Sstevel@tonic-gate }
93877c478bd9Sstevel@tonic-gate 
93887c478bd9Sstevel@tonic-gate /*
93893c34adc5Sramat  * Read the on-disk vhci cache into an nvlist for the specified vhci class.
93907c478bd9Sstevel@tonic-gate  */
93913c34adc5Sramat static nvlist_t *
read_on_disk_vhci_cache(char * vhci_class)93923c34adc5Sramat read_on_disk_vhci_cache(char *vhci_class)
93937c478bd9Sstevel@tonic-gate {
93943c34adc5Sramat 	nvlist_t *nvl;
93953c34adc5Sramat 	int err;
93963c34adc5Sramat 	char *filename;
93973c34adc5Sramat 
93983c34adc5Sramat 	filename = vhclass2vhcache_filename(vhci_class);
93993c34adc5Sramat 
94003c34adc5Sramat 	if ((err = fread_nvlist(filename, &nvl)) == 0) {
94013c34adc5Sramat 		kmem_free(filename, strlen(filename) + 1);
94023c34adc5Sramat 		return (nvl);
94033c34adc5Sramat 	} else if (err == EIO)
94044c06356bSdh 		cmn_err(CE_WARN, "%s: I/O error, will recreate", filename);
94053c34adc5Sramat 	else if (err == EINVAL)
94063c34adc5Sramat 		cmn_err(CE_WARN,
94074c06356bSdh 		    "%s: data file corrupted, will recreate", filename);
94083c34adc5Sramat 
94093c34adc5Sramat 	kmem_free(filename, strlen(filename) + 1);
94103c34adc5Sramat 	return (NULL);
94117c478bd9Sstevel@tonic-gate }
94127c478bd9Sstevel@tonic-gate 
94137c478bd9Sstevel@tonic-gate /*
94143c34adc5Sramat  * Read on-disk vhci cache into nvlists for all vhci classes.
94153c34adc5Sramat  * Called during booting by i_ddi_read_devices_files().
94167c478bd9Sstevel@tonic-gate  */
94177c478bd9Sstevel@tonic-gate void
mdi_read_devices_files(void)94183c34adc5Sramat mdi_read_devices_files(void)
94197c478bd9Sstevel@tonic-gate {
94203c34adc5Sramat 	int i;
94213c34adc5Sramat 
94223c34adc5Sramat 	for (i = 0; i < N_VHCI_CLASSES; i++)
94233c34adc5Sramat 		vhcache_nvl[i] = read_on_disk_vhci_cache(vhci_class_list[i]);
94247c478bd9Sstevel@tonic-gate }
94257c478bd9Sstevel@tonic-gate 
94267c478bd9Sstevel@tonic-gate /*
94273c34adc5Sramat  * Remove all stale entries from vhci cache.
94287c478bd9Sstevel@tonic-gate  */
94293c34adc5Sramat static void
clean_vhcache(mdi_vhci_config_t * vhc)94303c34adc5Sramat clean_vhcache(mdi_vhci_config_t *vhc)
94313c34adc5Sramat {
943242353512SRandall Ralphs 	mdi_vhci_cache_t	*vhcache = &vhc->vhc_vhcache;
943342353512SRandall Ralphs 	mdi_vhcache_phci_t	*phci, *nxt_phci;
943442353512SRandall Ralphs 	mdi_vhcache_client_t	*client, *nxt_client;
943542353512SRandall Ralphs 	mdi_vhcache_pathinfo_t	*path, *nxt_path;
94363c34adc5Sramat 
94373c34adc5Sramat 	rw_enter(&vhcache->vhcache_lock, RW_WRITER);
94383c34adc5Sramat 
943942353512SRandall Ralphs 	client = vhcache->vhcache_client_head;
94403c34adc5Sramat 	vhcache->vhcache_client_head = vhcache->vhcache_client_tail = NULL;
944142353512SRandall Ralphs 	for ( ; client != NULL; client = nxt_client) {
944242353512SRandall Ralphs 		nxt_client = client->cct_next;
944342353512SRandall Ralphs 
944442353512SRandall Ralphs 		path = client->cct_cpi_head;
944542353512SRandall Ralphs 		client->cct_cpi_head = client->cct_cpi_tail = NULL;
944642353512SRandall Ralphs 		for ( ; path != NULL; path = nxt_path) {
944742353512SRandall Ralphs 			nxt_path = path->cpi_next;
944842353512SRandall Ralphs 			if ((path->cpi_cphci->cphci_phci != NULL) &&
944942353512SRandall Ralphs 			    (path->cpi_pip != NULL)) {
945042353512SRandall Ralphs 				enqueue_tail_vhcache_pathinfo(client, path);
945142353512SRandall Ralphs 			} else if (path->cpi_pip != NULL) {
945242353512SRandall Ralphs 				/* Not valid to have a path without a phci. */
945342353512SRandall Ralphs 				free_vhcache_pathinfo(path);
945442353512SRandall Ralphs 			}
94553c34adc5Sramat 		}
94563c34adc5Sramat 
945742353512SRandall Ralphs 		if (client->cct_cpi_head != NULL)
945842353512SRandall Ralphs 			enqueue_vhcache_client(vhcache, client);
94593c34adc5Sramat 		else {
94603c34adc5Sramat 			(void) mod_hash_destroy(vhcache->vhcache_client_hash,
946142353512SRandall Ralphs 			    (mod_hash_key_t)client->cct_name_addr);
946242353512SRandall Ralphs 			free_vhcache_client(client);
94633c34adc5Sramat 		}
94647c478bd9Sstevel@tonic-gate 	}
94653c34adc5Sramat 
946642353512SRandall Ralphs 	phci = vhcache->vhcache_phci_head;
94673c34adc5Sramat 	vhcache->vhcache_phci_head = vhcache->vhcache_phci_tail = NULL;
946842353512SRandall Ralphs 	for ( ; phci != NULL; phci = nxt_phci) {
946942353512SRandall Ralphs 
947042353512SRandall Ralphs 		nxt_phci = phci->cphci_next;
947142353512SRandall Ralphs 		if (phci->cphci_phci != NULL)
947242353512SRandall Ralphs 			enqueue_vhcache_phci(vhcache, phci);
94733c34adc5Sramat 		else
947442353512SRandall Ralphs 			free_vhcache_phci(phci);
94753c34adc5Sramat 	}
94763c34adc5Sramat 
9477d3d50737SRafael Vanoni 	vhcache->vhcache_clean_time = ddi_get_lbolt64();
94783c34adc5Sramat 	rw_exit(&vhcache->vhcache_lock);
94793c34adc5Sramat 	vhcache_dirty(vhc);
94807c478bd9Sstevel@tonic-gate }
94817c478bd9Sstevel@tonic-gate 
94827c478bd9Sstevel@tonic-gate /*
94833c34adc5Sramat  * Remove all stale entries from vhci cache.
94843c34adc5Sramat  * Called by i_ddi_clean_devices_files() during the execution of devfsadm -C
94857c478bd9Sstevel@tonic-gate  */
94867c478bd9Sstevel@tonic-gate void
mdi_clean_vhcache(void)94873c34adc5Sramat mdi_clean_vhcache(void)
94887c478bd9Sstevel@tonic-gate {
94893c34adc5Sramat 	mdi_vhci_t *vh;
94903c34adc5Sramat 
94913c34adc5Sramat 	mutex_enter(&mdi_mutex);
94923c34adc5Sramat 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
94933c34adc5Sramat 		vh->vh_refcnt++;
94943c34adc5Sramat 		mutex_exit(&mdi_mutex);
94953c34adc5Sramat 		clean_vhcache(vh->vh_config);
94963c34adc5Sramat 		mutex_enter(&mdi_mutex);
94973c34adc5Sramat 		vh->vh_refcnt--;
94987c478bd9Sstevel@tonic-gate 	}
94993c34adc5Sramat 	mutex_exit(&mdi_mutex);
95007c478bd9Sstevel@tonic-gate }
95018c4f8890Srs 
95028c4f8890Srs /*
95038c4f8890Srs  * mdi_vhci_walk_clients():
95048c4f8890Srs  *		Walker routine to traverse client dev_info nodes
95058c4f8890Srs  * ddi_walk_devs(ddi_get_child(vdip), f, arg) returns the entire tree
95068c4f8890Srs  * below the client, including nexus devices, which we dont want.
95078c4f8890Srs  * So we just traverse the immediate siblings, starting from 1st client.
95088c4f8890Srs  */
95098c4f8890Srs void
mdi_vhci_walk_clients(dev_info_t * vdip,int (* f)(dev_info_t *,void *),void * arg)95108c4f8890Srs mdi_vhci_walk_clients(dev_info_t *vdip,
95118c4f8890Srs     int (*f)(dev_info_t *, void *), void *arg)
95128c4f8890Srs {
95135e3986cbScth 	mdi_vhci_t	*vh = i_devi_get_vhci(vdip);
95148c4f8890Srs 	dev_info_t	*cdip;
95158c4f8890Srs 	mdi_client_t	*ct;
95168c4f8890Srs 
95175e3986cbScth 	MDI_VHCI_CLIENT_LOCK(vh);
95188c4f8890Srs 	cdip = ddi_get_child(vdip);
95198c4f8890Srs 	while (cdip) {
95208c4f8890Srs 		ct = i_devi_get_client(cdip);
95218c4f8890Srs 		MDI_CLIENT_LOCK(ct);
95228c4f8890Srs 
95235e3986cbScth 		if (((*f)(cdip, arg)) == DDI_WALK_CONTINUE)
95248c4f8890Srs 			cdip = ddi_get_next_sibling(cdip);
95255e3986cbScth 		else
95265e3986cbScth 			cdip = NULL;
95278c4f8890Srs 
95285e3986cbScth 		MDI_CLIENT_UNLOCK(ct);
95298c4f8890Srs 	}
95305e3986cbScth 	MDI_VHCI_CLIENT_UNLOCK(vh);
95318c4f8890Srs }
95328c4f8890Srs 
95338c4f8890Srs /*
95348c4f8890Srs  * mdi_vhci_walk_phcis():
95358c4f8890Srs  *		Walker routine to traverse phci dev_info nodes
95368c4f8890Srs  */
95378c4f8890Srs void
mdi_vhci_walk_phcis(dev_info_t * vdip,int (* f)(dev_info_t *,void *),void * arg)95388c4f8890Srs mdi_vhci_walk_phcis(dev_info_t *vdip,
95398c4f8890Srs     int (*f)(dev_info_t *, void *), void *arg)
95408c4f8890Srs {
95415e3986cbScth 	mdi_vhci_t	*vh = i_devi_get_vhci(vdip);
95425e3986cbScth 	mdi_phci_t	*ph, *next;
95438c4f8890Srs 
95445e3986cbScth 	MDI_VHCI_PHCI_LOCK(vh);
95458c4f8890Srs 	ph = vh->vh_phci_head;
95468c4f8890Srs 	while (ph) {
95478c4f8890Srs 		MDI_PHCI_LOCK(ph);
95488c4f8890Srs 
95495e3986cbScth 		if (((*f)(ph->ph_dip, arg)) == DDI_WALK_CONTINUE)
95505e3986cbScth 			next = ph->ph_next;
95515e3986cbScth 		else
95525e3986cbScth 			next = NULL;
95538c4f8890Srs 
95545e3986cbScth 		MDI_PHCI_UNLOCK(ph);
95555e3986cbScth 		ph = next;
95568c4f8890Srs 	}
95575e3986cbScth 	MDI_VHCI_PHCI_UNLOCK(vh);
95588c4f8890Srs }
95598c4f8890Srs 
95608c4f8890Srs 
95618c4f8890Srs /*
95628c4f8890Srs  * mdi_walk_vhcis():
95638c4f8890Srs  *		Walker routine to traverse vhci dev_info nodes
95648c4f8890Srs  */
95658c4f8890Srs void
mdi_walk_vhcis(int (* f)(dev_info_t *,void *),void * arg)95668c4f8890Srs mdi_walk_vhcis(int (*f)(dev_info_t *, void *), void *arg)
95678c4f8890Srs {
95688c4f8890Srs 	mdi_vhci_t	*vh = NULL;
95698c4f8890Srs 
95708c4f8890Srs 	mutex_enter(&mdi_mutex);
95718c4f8890Srs 	/*
95728c4f8890Srs 	 * Scan for already registered vhci
95738c4f8890Srs 	 */
95748c4f8890Srs 	for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
95758c4f8890Srs 		vh->vh_refcnt++;
95768c4f8890Srs 		mutex_exit(&mdi_mutex);
95778c4f8890Srs 		if (((*f)(vh->vh_dip, arg)) != DDI_WALK_CONTINUE) {
95788c4f8890Srs 			mutex_enter(&mdi_mutex);
95798c4f8890Srs 			vh->vh_refcnt--;
95808c4f8890Srs 			break;
95818c4f8890Srs 		} else {
95828c4f8890Srs 			mutex_enter(&mdi_mutex);
95838c4f8890Srs 			vh->vh_refcnt--;
95848c4f8890Srs 		}
95858c4f8890Srs 	}
95868c4f8890Srs 
95878c4f8890Srs 	mutex_exit(&mdi_mutex);
95888c4f8890Srs }
95898c4f8890Srs 
95908c4f8890Srs /*
95918c4f8890Srs  * i_mdi_log_sysevent():
95928c4f8890Srs  *		Logs events for pickup by syseventd
95938c4f8890Srs  */
95948c4f8890Srs static void
i_mdi_log_sysevent(dev_info_t * dip,char * ph_vh_class,char * subclass)95958c4f8890Srs i_mdi_log_sysevent(dev_info_t *dip, char *ph_vh_class, char *subclass)
95968c4f8890Srs {
95978c4f8890Srs 	char		*path_name;
95988c4f8890Srs 	nvlist_t	*attr_list;
95998c4f8890Srs 
96008c4f8890Srs 	if (nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE,
96018c4f8890Srs 	    KM_SLEEP) != DDI_SUCCESS) {
96028c4f8890Srs 		goto alloc_failed;
96038c4f8890Srs 	}
96048c4f8890Srs 
96058c4f8890Srs 	path_name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
96068c4f8890Srs 	(void) ddi_pathname(dip, path_name);
96078c4f8890Srs 
96088c4f8890Srs 	if (nvlist_add_string(attr_list, DDI_DRIVER_NAME,
96098c4f8890Srs 	    ddi_driver_name(dip)) != DDI_SUCCESS) {
96108c4f8890Srs 		goto error;
96118c4f8890Srs 	}
96128c4f8890Srs 
96138c4f8890Srs 	if (nvlist_add_int32(attr_list, DDI_DRIVER_MAJOR,
96148c4f8890Srs 	    (int32_t)ddi_driver_major(dip)) != DDI_SUCCESS) {
96158c4f8890Srs 		goto error;
96168c4f8890Srs 	}
96178c4f8890Srs 
96188c4f8890Srs 	if (nvlist_add_int32(attr_list, DDI_INSTANCE,
96198c4f8890Srs 	    (int32_t)ddi_get_instance(dip)) != DDI_SUCCESS) {
96208c4f8890Srs 		goto error;
96218c4f8890Srs 	}
96228c4f8890Srs 
96238c4f8890Srs 	if (nvlist_add_string(attr_list, DDI_PATHNAME,
96248c4f8890Srs 	    path_name) != DDI_SUCCESS) {
96258c4f8890Srs 		goto error;
96268c4f8890Srs 	}
96278c4f8890Srs 
96288c4f8890Srs 	if (nvlist_add_string(attr_list, DDI_CLASS,
96298c4f8890Srs 	    ph_vh_class) != DDI_SUCCESS) {
96308c4f8890Srs 		goto error;
96318c4f8890Srs 	}
96328c4f8890Srs 
96338c4f8890Srs 	(void) ddi_log_sysevent(dip, DDI_VENDOR_SUNW, EC_DDI, subclass,
96348c4f8890Srs 	    attr_list, NULL, DDI_SLEEP);
96358c4f8890Srs 
96368c4f8890Srs error:
96378c4f8890Srs 	kmem_free(path_name, MAXPATHLEN);
96388c4f8890Srs 	nvlist_free(attr_list);
96398c4f8890Srs 	return;
96408c4f8890Srs 
96418c4f8890Srs alloc_failed:
96424c06356bSdh 	MDI_DEBUG(1, (MDI_WARN, dip, "!unable to send sysevent"));
96438c4f8890Srs }
9644f7209cf2Spramodbg 
9645f7209cf2Spramodbg char **
mdi_get_phci_driver_list(char * vhci_class,int * ndrivers)9646f7209cf2Spramodbg mdi_get_phci_driver_list(char *vhci_class, int	*ndrivers)
9647f7209cf2Spramodbg {
9648f7209cf2Spramodbg 	char	**driver_list, **ret_driver_list = NULL;
9649f7209cf2Spramodbg 	int	*root_support_list;
9650f7209cf2Spramodbg 	int	cur_elements, max_elements;
9651f7209cf2Spramodbg 
9652f7209cf2Spramodbg 	get_phci_driver_list(vhci_class, &driver_list, &root_support_list,
9653f7209cf2Spramodbg 	    &cur_elements, &max_elements);
9654f7209cf2Spramodbg 
9655f7209cf2Spramodbg 
9656f7209cf2Spramodbg 	if (driver_list) {
9657f7209cf2Spramodbg 		kmem_free(root_support_list, sizeof (int) * max_elements);
9658f7209cf2Spramodbg 		ret_driver_list = mdi_realloc(driver_list, sizeof (char *)
9659f7209cf2Spramodbg 		    * max_elements, sizeof (char *) * cur_elements);
9660f7209cf2Spramodbg 	}
9661f7209cf2Spramodbg 	*ndrivers = cur_elements;
9662f7209cf2Spramodbg 
9663f7209cf2Spramodbg 	return (ret_driver_list);
9664f7209cf2Spramodbg 
9665f7209cf2Spramodbg }
9666f7209cf2Spramodbg 
9667f7209cf2Spramodbg void
mdi_free_phci_driver_list(char ** driver_list,int ndrivers)9668f7209cf2Spramodbg mdi_free_phci_driver_list(char **driver_list, int ndrivers)
9669f7209cf2Spramodbg {
9670f7209cf2Spramodbg 	char	**p;
9671f7209cf2Spramodbg 	int	i;
9672f7209cf2Spramodbg 
9673f7209cf2Spramodbg 	if (driver_list) {
9674f7209cf2Spramodbg 		for (i = 0, p = driver_list; i < ndrivers; i++, p++)
9675f7209cf2Spramodbg 			kmem_free(*p, strlen(*p) + 1);
9676f7209cf2Spramodbg 		kmem_free(driver_list, sizeof (char *) * ndrivers);
9677f7209cf2Spramodbg 	}
9678f7209cf2Spramodbg }
967955e592a2SRandall Ralphs 
968055e592a2SRandall Ralphs /*
968155e592a2SRandall Ralphs  * mdi_is_dev_supported():
968255e592a2SRandall Ralphs  *		function called by pHCI bus config operation to determine if a
968355e592a2SRandall Ralphs  *		device should be represented as a child of the vHCI or the
968455e592a2SRandall Ralphs  *		pHCI.  This decision is made by the vHCI, using cinfo idenity
968555e592a2SRandall Ralphs  *		information passed by the pHCI - specifics of the cinfo
968655e592a2SRandall Ralphs  *		representation are by agreement between the pHCI and vHCI.
968755e592a2SRandall Ralphs  * Return Values:
968855e592a2SRandall Ralphs  *		MDI_SUCCESS
968955e592a2SRandall Ralphs  *		MDI_FAILURE
969055e592a2SRandall Ralphs  */
969155e592a2SRandall Ralphs int
mdi_is_dev_supported(char * class,dev_info_t * pdip,void * cinfo)969255e592a2SRandall Ralphs mdi_is_dev_supported(char *class, dev_info_t *pdip, void *cinfo)
969355e592a2SRandall Ralphs {
969455e592a2SRandall Ralphs 	mdi_vhci_t	*vh;
969555e592a2SRandall Ralphs 
969655e592a2SRandall Ralphs 	ASSERT(class && pdip);
969755e592a2SRandall Ralphs 
969855e592a2SRandall Ralphs 	/*
969955e592a2SRandall Ralphs 	 * For dev_supported, mdi_phci_register() must have established pdip as
970055e592a2SRandall Ralphs 	 * a pHCI.
970155e592a2SRandall Ralphs 	 *
970255e592a2SRandall Ralphs 	 * NOTE: mdi_phci_register() does "mpxio-disable" processing, and
970355e592a2SRandall Ralphs 	 * MDI_PHCI(pdip) will return false if mpxio is disabled.
970455e592a2SRandall Ralphs 	 */
970555e592a2SRandall Ralphs 	if (!MDI_PHCI(pdip))
970655e592a2SRandall Ralphs 		return (MDI_FAILURE);
970755e592a2SRandall Ralphs 
970855e592a2SRandall Ralphs 	/* Return MDI_FAILURE if vHCI does not support asking the question. */
970955e592a2SRandall Ralphs 	vh = (mdi_vhci_t *)i_mdi_vhci_class2vhci(class);
971055e592a2SRandall Ralphs 	if ((vh == NULL) || (vh->vh_ops->vo_is_dev_supported == NULL)) {
971155e592a2SRandall Ralphs 		return (MDI_FAILURE);
971255e592a2SRandall Ralphs 	}
971355e592a2SRandall Ralphs 
971455e592a2SRandall Ralphs 	/* Return vHCI answer */
971555e592a2SRandall Ralphs 	return (vh->vh_ops->vo_is_dev_supported(vh->vh_dip, pdip, cinfo));
971655e592a2SRandall Ralphs }
971755e592a2SRandall Ralphs 
971855e592a2SRandall Ralphs int
mdi_dc_return_dev_state(mdi_pathinfo_t * pip,struct devctl_iocdata * dcp)971955e592a2SRandall Ralphs mdi_dc_return_dev_state(mdi_pathinfo_t *pip, struct devctl_iocdata *dcp)
972055e592a2SRandall Ralphs {
972155e592a2SRandall Ralphs 	uint_t devstate = 0;
972255e592a2SRandall Ralphs 	dev_info_t *cdip;
972355e592a2SRandall Ralphs 
972455e592a2SRandall Ralphs 	if ((pip == NULL) || (dcp == NULL))
972555e592a2SRandall Ralphs 		return (MDI_FAILURE);
972655e592a2SRandall Ralphs 
972755e592a2SRandall Ralphs 	cdip = mdi_pi_get_client(pip);
972855e592a2SRandall Ralphs 
972955e592a2SRandall Ralphs 	switch (mdi_pi_get_state(pip)) {
973055e592a2SRandall Ralphs 	case MDI_PATHINFO_STATE_INIT:
973155e592a2SRandall Ralphs 		devstate = DEVICE_DOWN;
973255e592a2SRandall Ralphs 		break;
973355e592a2SRandall Ralphs 	case MDI_PATHINFO_STATE_ONLINE:
973455e592a2SRandall Ralphs 		devstate = DEVICE_ONLINE;
973555e592a2SRandall Ralphs 		if ((cdip) && (devi_stillreferenced(cdip) == DEVI_REFERENCED))
973655e592a2SRandall Ralphs 			devstate |= DEVICE_BUSY;
973755e592a2SRandall Ralphs 		break;
973855e592a2SRandall Ralphs 	case MDI_PATHINFO_STATE_STANDBY:
973955e592a2SRandall Ralphs 		devstate = DEVICE_ONLINE;
974055e592a2SRandall Ralphs 		break;
974155e592a2SRandall Ralphs 	case MDI_PATHINFO_STATE_FAULT:
974255e592a2SRandall Ralphs 		devstate = DEVICE_DOWN;
974355e592a2SRandall Ralphs 		break;
974455e592a2SRandall Ralphs 	case MDI_PATHINFO_STATE_OFFLINE:
974555e592a2SRandall Ralphs 		devstate = DEVICE_OFFLINE;
974655e592a2SRandall Ralphs 		break;
974755e592a2SRandall Ralphs 	default:
974855e592a2SRandall Ralphs 		ASSERT(MDI_PI(pip)->pi_state);
974955e592a2SRandall Ralphs 	}
975055e592a2SRandall Ralphs 
975155e592a2SRandall Ralphs 	if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
975255e592a2SRandall Ralphs 		return (MDI_FAILURE);
975355e592a2SRandall Ralphs 
975455e592a2SRandall Ralphs 	return (MDI_SUCCESS);
975555e592a2SRandall Ralphs }
9756