#include <sys/note.h>
#include <sys/types.h>
#include <sys/varargs.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/uio.h>
#include <sys/buf.h>
#include <sys/modctl.h>
#include <sys/open.h>
#include <sys/kmem.h>
#include <sys/poll.h>
#include <sys/conf.h>
#include <sys/bootconf.h>
#include <sys/cmn_err.h>
#include <sys/stat.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/ddipropdefs.h>
#include <sys/sunndi.h>
#include <sys/ndi_impldefs.h>
#include <sys/promif.h>
#include <sys/sunmdi.h>
#include <sys/mdi_impldefs.h>
#include <sys/taskq.h>
#include <sys/epm.h>
#include <sys/sunpm.h>
#include <sys/modhash.h>
#include <sys/disp.h>
#include <sys/autoconf.h>
#include <sys/sysmacros.h>
#ifdef DEBUG
#include <sys/debug.h>
int mdi_debug = 1;
int mdi_debug_logonly = 0;
#define MDI_DEBUG(dbglevel, pargs) if (mdi_debug >= (dbglevel)) i_mdi_log pargs
#define MDI_WARN CE_WARN, __func__
#define MDI_NOTE CE_NOTE, __func__
#define MDI_CONT CE_CONT, __func__
static void i_mdi_log(int, const char *, dev_info_t *, const char *, ...);
#else
#define MDI_DEBUG(dbglevel, pargs)
#endif
int mdi_debug_consoleonly = 0;
int mdi_delay = 3;
extern pri_t minclsyspri;
extern int modrootloaded;
kmutex_t mdi_mutex;
int mdi_vhci_count;
mdi_vhci_t *mdi_vhci_head;
mdi_vhci_t *mdi_vhci_tail;
static int mdi_client_table_size = CLIENT_HASH_TABLE_SIZE;
#define MDI_TASKQ_N_THREADS 8
#define MDI_TASKQ_PRI minclsyspri
#define MDI_TASKQ_MINALLOC (4*mdi_taskq_n_threads)
#define MDI_TASKQ_MAXALLOC (500*mdi_taskq_n_threads)
taskq_t *mdi_taskq;
static uint_t mdi_taskq_n_threads = MDI_TASKQ_N_THREADS;
#define TICKS_PER_SECOND (drv_usectohz(1000000))
static int mdi_vhcache_flush_delay = 10;
static int mdi_vhcache_flush_daemon_idle_time = 60;
static int mdi_path_discovery_boot = 1;
static int mdi_path_discovery_postboot = 2;
static int mdi_path_discovery_interval = 10;
static int mdi_async_config_idle_time = 600;
static int mdi_bus_config_cache_hash_size = 256;
static int mdi_mtc_off = 0;
static uint_t mdi_pathmap_instance = 1;
static int mdi_pathmap_hash_size = 256;
static kmutex_t mdi_pathmap_mutex;
static mod_hash_t *mdi_pathmap_bypath;
static mod_hash_t *mdi_pathmap_byinstance;
static mod_hash_t *mdi_pathmap_sbyinstance;
const char *mdi_component_prop = "mpxio-component";
const char *mdi_component_prop_vhci = "vhci";
const char *mdi_component_prop_phci = "phci";
const char *mdi_component_prop_client = "client";
const char *mdi_client_guid_prop = "client-guid";
const char *mdi_load_balance = "load-balance";
const char *mdi_load_balance_none = "none";
const char *mdi_load_balance_rr = "round-robin";
const char *mdi_load_balance_lba = "logical-block";
const char *mdi_vhci_class_scsi = MDI_HCI_CLASS_SCSI;
static char vhci_greeting[] =
"\tThere already exists one vHCI driver for class %s\n"
"\tOnly one vHCI driver for each class is allowed\n";
static int i_mdi_phci_offline(dev_info_t *, uint_t);
static int i_mdi_client_offline(dev_info_t *, uint_t);
static int i_mdi_phci_pre_detach(dev_info_t *, ddi_detach_cmd_t);
static void i_mdi_phci_post_detach(dev_info_t *,
ddi_detach_cmd_t, int);
static int i_mdi_client_pre_detach(dev_info_t *,
ddi_detach_cmd_t);
static void i_mdi_client_post_detach(dev_info_t *,
ddi_detach_cmd_t, int);
static void i_mdi_pm_hold_pip(mdi_pathinfo_t *);
static void i_mdi_pm_rele_pip(mdi_pathinfo_t *);
static int i_mdi_lba_lb(mdi_client_t *ct,
mdi_pathinfo_t **ret_pip, struct buf *buf);
static void i_mdi_pm_hold_client(mdi_client_t *, int);
static void i_mdi_pm_rele_client(mdi_client_t *, int);
static void i_mdi_pm_reset_client(mdi_client_t *);
static int i_mdi_power_all_phci(mdi_client_t *);
static void i_mdi_log_sysevent(dev_info_t *, char *, char *);
static void i_mdi_pi_kstat_destroy(mdi_pathinfo_t *);
static mdi_vhci_t *i_mdi_vhci_class2vhci(char *);
static mdi_vhci_t *i_devi_get_vhci(dev_info_t *);
static mdi_phci_t *i_devi_get_phci(dev_info_t *);
static void i_mdi_phci_lock(mdi_phci_t *, mdi_pathinfo_t *);
static void i_mdi_phci_unlock(mdi_phci_t *);
static mdi_pathinfo_t *i_mdi_pi_alloc(mdi_phci_t *, char *, mdi_client_t *);
static void i_mdi_phci_add_path(mdi_phci_t *, mdi_pathinfo_t *);
static void i_mdi_client_add_path(mdi_client_t *, mdi_pathinfo_t *);
static void i_mdi_pi_free(mdi_phci_t *ph, mdi_pathinfo_t *,
mdi_client_t *);
static void i_mdi_phci_remove_path(mdi_phci_t *, mdi_pathinfo_t *);
static void i_mdi_client_remove_path(mdi_client_t *,
mdi_pathinfo_t *);
static int i_mdi_pi_state_change(mdi_pathinfo_t *,
mdi_pathinfo_state_t, int);
static int i_mdi_pi_offline(mdi_pathinfo_t *, int);
static dev_info_t *i_mdi_devinfo_create(mdi_vhci_t *, char *, char *,
char **, int);
static dev_info_t *i_mdi_devinfo_find(mdi_vhci_t *, char *, char *);
static int i_mdi_devinfo_remove(dev_info_t *, dev_info_t *, int);
static int i_mdi_is_child_present(dev_info_t *, dev_info_t *);
static mdi_client_t *i_mdi_client_alloc(mdi_vhci_t *, char *, char *);
static void i_mdi_client_enlist_table(mdi_vhci_t *, mdi_client_t *);
static void i_mdi_client_delist_table(mdi_vhci_t *, mdi_client_t *);
static mdi_client_t *i_mdi_client_find(mdi_vhci_t *, char *, char *);
static void i_mdi_client_update_state(mdi_client_t *);
static int i_mdi_client_compute_state(mdi_client_t *,
mdi_phci_t *);
static void i_mdi_client_lock(mdi_client_t *, mdi_pathinfo_t *);
static void i_mdi_client_unlock(mdi_client_t *);
static int i_mdi_client_free(mdi_vhci_t *, mdi_client_t *);
static mdi_client_t *i_devi_get_client(dev_info_t *);
static int i_mdi_pi_enable_disable(dev_info_t *, dev_info_t *,
int, int);
static mdi_pathinfo_t *i_mdi_enable_disable_path(mdi_pathinfo_t *pip,
mdi_vhci_t *vh, int flags, int op);
static int i_mdi_failover(void *);
static int i_mdi_get_hash_key(char *);
static int i_map_nvlist_error_to_mdi(int);
static void i_mdi_report_path_state(mdi_client_t *,
mdi_pathinfo_t *);
static void setup_vhci_cache(mdi_vhci_t *);
static int destroy_vhci_cache(mdi_vhci_t *);
static int stop_vhcache_async_threads(mdi_vhci_config_t *);
static boolean_t stop_vhcache_flush_thread(void *, int);
static void free_string_array(char **, int);
static void free_vhcache_phci(mdi_vhcache_phci_t *);
static void free_vhcache_pathinfo(mdi_vhcache_pathinfo_t *);
static void free_vhcache_client(mdi_vhcache_client_t *);
static int mainnvl_to_vhcache(mdi_vhci_cache_t *, nvlist_t *);
static nvlist_t *vhcache_to_mainnvl(mdi_vhci_cache_t *);
static void vhcache_phci_add(mdi_vhci_config_t *, mdi_phci_t *);
static void vhcache_phci_remove(mdi_vhci_config_t *, mdi_phci_t *);
static void vhcache_pi_add(mdi_vhci_config_t *,
struct mdi_pathinfo *);
static void vhcache_pi_remove(mdi_vhci_config_t *,
struct mdi_pathinfo *);
static void free_phclient_path_list(mdi_phys_path_t *);
static void sort_vhcache_paths(mdi_vhcache_client_t *);
static int flush_vhcache(mdi_vhci_config_t *, int);
static void vhcache_dirty(mdi_vhci_config_t *);
static void free_async_client_config(mdi_async_client_config_t *);
static void single_threaded_vhconfig_enter(mdi_vhci_config_t *);
static void single_threaded_vhconfig_exit(mdi_vhci_config_t *);
static nvlist_t *read_on_disk_vhci_cache(char *);
extern int fread_nvlist(char *, nvlist_t **);
extern int fwrite_nvlist(char *, nvlist_t *);
static void
i_mdi_init()
{
static int initialized = 0;
if (initialized)
return;
initialized = 1;
mutex_init(&mdi_mutex, NULL, MUTEX_DEFAULT, NULL);
mdi_taskq = taskq_create("mdi_taskq", mdi_taskq_n_threads,
MDI_TASKQ_PRI, MDI_TASKQ_MINALLOC, MDI_TASKQ_MAXALLOC,
TASKQ_PREPOPULATE | TASKQ_CPR_SAFE);
ASSERT(mdi_taskq != NULL);
mutex_init(&mdi_pathmap_mutex, NULL, MUTEX_DRIVER, NULL);
mdi_pathmap_bypath = mod_hash_create_strhash(
"mdi_pathmap_bypath", mdi_pathmap_hash_size,
mod_hash_null_valdtor);
mdi_pathmap_byinstance = mod_hash_create_idhash(
"mdi_pathmap_byinstance", mdi_pathmap_hash_size,
mod_hash_null_valdtor);
mdi_pathmap_sbyinstance = mod_hash_create_idhash(
"mdi_pathmap_sbyinstance", mdi_pathmap_hash_size,
mod_hash_null_valdtor);
}
int
mdi_get_component_type(dev_info_t *dip)
{
return (DEVI(dip)->devi_mdi_component);
}
int
mdi_vhci_register(char *class, dev_info_t *vdip, mdi_vhci_ops_t *vops,
int flags)
{
mdi_vhci_t *vh = NULL;
ASSERT(vops->vo_revision <= MDI_VHCI_OPS_REV);
#ifdef DEBUG
if (strcmp(class, MDI_HCI_CLASS_IB) != 0)
ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(vdip)));
#endif
i_mdi_init();
mutex_enter(&mdi_mutex);
for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
if (strcmp(vh->vh_class, class) == 0) {
if (vh->vh_ops != NULL) {
mutex_exit(&mdi_mutex);
cmn_err(CE_NOTE, vhci_greeting, class);
return (MDI_FAILURE);
}
break;
}
}
if (vh == NULL) {
struct client_hash *hash = NULL;
char *load_balance;
vh = kmem_zalloc(sizeof (mdi_vhci_t), KM_SLEEP);
hash = kmem_zalloc(mdi_client_table_size * sizeof (*hash),
KM_SLEEP);
vh->vh_client_table = hash;
vh->vh_class = kmem_zalloc(strlen(class) + 1, KM_SLEEP);
(void) strcpy(vh->vh_class, class);
vh->vh_lb = LOAD_BALANCE_RR;
if (ddi_prop_lookup_string(DDI_DEV_T_ANY, vdip,
0, LOAD_BALANCE_PROP, &load_balance) == DDI_SUCCESS) {
if (strcmp(load_balance, LOAD_BALANCE_PROP_NONE) == 0) {
vh->vh_lb = LOAD_BALANCE_NONE;
} else if (strcmp(load_balance, LOAD_BALANCE_PROP_LBA)
== 0) {
vh->vh_lb = LOAD_BALANCE_LBA;
}
ddi_prop_free(load_balance);
}
mutex_init(&vh->vh_phci_mutex, NULL, MUTEX_DEFAULT, NULL);
mutex_init(&vh->vh_client_mutex, NULL, MUTEX_DEFAULT, NULL);
vh->vh_dip = vdip;
vh->vh_ops = vops;
setup_vhci_cache(vh);
if (mdi_vhci_head == NULL) {
mdi_vhci_head = vh;
}
if (mdi_vhci_tail) {
mdi_vhci_tail->vh_next = vh;
}
mdi_vhci_tail = vh;
mdi_vhci_count++;
}
DEVI(vdip)->devi_mdi_component |= MDI_COMPONENT_VHCI;
DEVI(vdip)->devi_mdi_xhci = (caddr_t)vh;
mutex_exit(&mdi_mutex);
return (MDI_SUCCESS);
}
int
mdi_vhci_unregister(dev_info_t *vdip, int flags)
{
mdi_vhci_t *found, *vh, *prev = NULL;
ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(vdip)));
if ((vh = i_devi_get_vhci(vdip)) == NULL)
return (MDI_FAILURE);
mutex_enter(&mdi_mutex);
for (found = mdi_vhci_head; found != NULL; found = found->vh_next) {
if (found == vh)
break;
prev = found;
}
if (found == NULL) {
mutex_exit(&mdi_mutex);
return (MDI_FAILURE);
}
MDI_VHCI_PHCI_LOCK(vh);
if (vh->vh_refcnt || vh->vh_phci_count || vh->vh_client_count) {
MDI_VHCI_PHCI_UNLOCK(vh);
mutex_exit(&mdi_mutex);
return (MDI_FAILURE);
}
MDI_VHCI_PHCI_UNLOCK(vh);
if (destroy_vhci_cache(vh) != MDI_SUCCESS) {
mutex_exit(&mdi_mutex);
return (MDI_FAILURE);
}
if (vh == mdi_vhci_head) {
mdi_vhci_head = vh->vh_next;
} else {
prev->vh_next = vh->vh_next;
}
if (vh == mdi_vhci_tail) {
mdi_vhci_tail = prev;
}
mdi_vhci_count--;
mutex_exit(&mdi_mutex);
vh->vh_ops = NULL;
DEVI(vdip)->devi_mdi_component &= ~MDI_COMPONENT_VHCI;
DEVI(vdip)->devi_mdi_xhci = NULL;
kmem_free(vh->vh_class, strlen(vh->vh_class)+1);
kmem_free(vh->vh_client_table,
mdi_client_table_size * sizeof (struct client_hash));
mutex_destroy(&vh->vh_phci_mutex);
mutex_destroy(&vh->vh_client_mutex);
kmem_free(vh, sizeof (mdi_vhci_t));
return (MDI_SUCCESS);
}
static mdi_vhci_t *
i_mdi_vhci_class2vhci(char *class)
{
mdi_vhci_t *vh = NULL;
ASSERT(!MUTEX_HELD(&mdi_mutex));
mutex_enter(&mdi_mutex);
for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
if (strcmp(vh->vh_class, class) == 0) {
break;
}
}
mutex_exit(&mdi_mutex);
return (vh);
}
mdi_vhci_t *
i_devi_get_vhci(dev_info_t *vdip)
{
mdi_vhci_t *vh = NULL;
if (MDI_VHCI(vdip)) {
vh = (mdi_vhci_t *)DEVI(vdip)->devi_mdi_xhci;
}
return (vh);
}
int
mdi_phci_register(char *class, dev_info_t *pdip, int flags)
{
mdi_phci_t *ph;
mdi_vhci_t *vh;
char *data;
ASSERT(DEVI_BUSY_CHANGING(ddi_get_parent(pdip)));
if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip, 0, "mpxio-disable",
&data) == DDI_SUCCESS)) {
if (strcmp(data, "yes") == 0) {
MDI_DEBUG(1, (MDI_CONT, pdip,
"?multipath capabilities disabled via %s.conf.",
ddi_driver_name(pdip)));
ddi_prop_free(data);
return (MDI_FAILURE);
}
ddi_prop_free(data);
}
vh = (mdi_vhci_t *)i_mdi_vhci_class2vhci(class);
if (vh == NULL) {
return (MDI_FAILURE);
}
ph = kmem_zalloc(sizeof (mdi_phci_t), KM_SLEEP);
mutex_init(&ph->ph_mutex, NULL, MUTEX_DEFAULT, NULL);
ph->ph_dip = pdip;
ph->ph_vhci = vh;
ph->ph_next = NULL;
ph->ph_unstable = 0;
ph->ph_vprivate = 0;
cv_init(&ph->ph_unstable_cv, NULL, CV_DRIVER, NULL);
MDI_PHCI_LOCK(ph);
MDI_PHCI_SET_POWER_UP(ph);
MDI_PHCI_UNLOCK(ph);
DEVI(pdip)->devi_mdi_component |= MDI_COMPONENT_PHCI;
DEVI(pdip)->devi_mdi_xhci = (caddr_t)ph;
vhcache_phci_add(vh->vh_config, ph);
MDI_VHCI_PHCI_LOCK(vh);
if (vh->vh_phci_head == NULL) {
vh->vh_phci_head = ph;
}
if (vh->vh_phci_tail) {
vh->vh_phci_tail->ph_next = ph;
}
vh->vh_phci_tail = ph;
vh->vh_phci_count++;
MDI_VHCI_PHCI_UNLOCK(vh);
i_mdi_log_sysevent(pdip, class, ESC_DDI_INITIATOR_REGISTER);
return (MDI_SUCCESS);
}
int
mdi_phci_unregister(dev_info_t *pdip, int flags)
{
mdi_vhci_t *vh;
mdi_phci_t *ph;
mdi_phci_t *tmp;
mdi_phci_t *prev = NULL;
mdi_pathinfo_t *pip;
ASSERT(DEVI_BUSY_CHANGING(ddi_get_parent(pdip)));
ph = i_devi_get_phci(pdip);
if (ph == NULL) {
MDI_DEBUG(1, (MDI_WARN, pdip, "!not a valid pHCI"));
return (MDI_FAILURE);
}
vh = ph->ph_vhci;
ASSERT(vh != NULL);
if (vh == NULL) {
MDI_DEBUG(1, (MDI_WARN, pdip, "!not a valid vHCI"));
return (MDI_FAILURE);
}
MDI_VHCI_PHCI_LOCK(vh);
tmp = vh->vh_phci_head;
while (tmp) {
if (tmp == ph) {
break;
}
prev = tmp;
tmp = tmp->ph_next;
}
if (ph == vh->vh_phci_head) {
vh->vh_phci_head = ph->ph_next;
} else {
prev->ph_next = ph->ph_next;
}
if (ph == vh->vh_phci_tail) {
vh->vh_phci_tail = prev;
}
vh->vh_phci_count--;
MDI_VHCI_PHCI_UNLOCK(vh);
MDI_PHCI_LOCK(ph);
for (pip = (mdi_pathinfo_t *)ph->ph_path_head; pip;
pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link)
MDI_PI(pip)->pi_phci = NULL;
MDI_PHCI_UNLOCK(ph);
i_mdi_log_sysevent(pdip, ph->ph_vhci->vh_class,
ESC_DDI_INITIATOR_UNREGISTER);
vhcache_phci_remove(vh->vh_config, ph);
cv_destroy(&ph->ph_unstable_cv);
mutex_destroy(&ph->ph_mutex);
kmem_free(ph, sizeof (mdi_phci_t));
DEVI(pdip)->devi_mdi_component &= ~MDI_COMPONENT_PHCI;
DEVI(pdip)->devi_mdi_xhci = NULL;
return (MDI_SUCCESS);
}
static mdi_phci_t *
i_devi_get_phci(dev_info_t *pdip)
{
mdi_phci_t *ph = NULL;
if (MDI_PHCI(pdip)) {
ph = (mdi_phci_t *)DEVI(pdip)->devi_mdi_xhci;
}
return (ph);
}
void
mdi_devi_enter(dev_info_t *phci_dip, boolean_t *enteredvp)
{
dev_info_t *vdip;
ASSERT3P(enteredvp, !=, NULL);
ASSERT(MDI_PHCI(phci_dip));
vdip = mdi_devi_get_vdip(phci_dip);
ASSERT3P(vdip, !=, NULL);
*enteredvp = B_FALSE;
for (;;) {
if (panicstr != NULL)
return;
if (ndi_devi_tryenter(vdip)) {
*enteredvp = B_TRUE;
if (DEVI_IS_DETACHING(phci_dip)) {
ndi_devi_exit(vdip);
*enteredvp = B_FALSE;
}
break;
} else if (DEVI_IS_DETACHING(phci_dip)) {
*enteredvp = B_FALSE;
break;
} else if (servicing_interrupt()) {
ndi_devi_enter(vdip);
*enteredvp = B_TRUE;
break;
} else {
delay_random(mdi_delay);
}
}
ndi_devi_enter(phci_dip);
}
int
mdi_devi_tryenter(dev_info_t *phci_dip, boolean_t *enteredvp)
{
dev_info_t *vdip;
ASSERT(MDI_PHCI(phci_dip));
vdip = mdi_devi_get_vdip(phci_dip);
ASSERT3P(vdip, !=, NULL);
*enteredvp = B_FALSE;
if (ndi_devi_tryenter(vdip)) {
if (ndi_devi_tryenter(phci_dip)) {
*enteredvp = B_TRUE;
return (1);
}
ndi_devi_exit(vdip);
}
return (0);
}
void
mdi_devi_exit(dev_info_t *phci_dip, boolean_t enteredv)
{
dev_info_t *vdip;
ASSERT(MDI_PHCI(phci_dip));
vdip = mdi_devi_get_vdip(phci_dip);
ASSERT3P(vdip, !=, NULL);
ndi_devi_exit(phci_dip);
if (enteredv)
ndi_devi_exit(vdip);
}
void
mdi_devi_exit_phci(dev_info_t *phci_dip)
{
ASSERT(MDI_PHCI(phci_dip));
ndi_hold_devi(phci_dip);
ndi_devi_exit(phci_dip);
}
void
mdi_devi_enter_phci(dev_info_t *phci_dip)
{
ASSERT(MDI_PHCI(phci_dip));
ndi_devi_enter(phci_dip);
ndi_rele_devi(phci_dip);
}
dev_info_t *
mdi_devi_get_vdip(dev_info_t *pdip)
{
mdi_phci_t *ph;
ph = i_devi_get_phci(pdip);
if (ph && ph->ph_vhci)
return (ph->ph_vhci->vh_dip);
return (NULL);
}
int
mdi_devi_pdip_entered(dev_info_t *vdip)
{
mdi_vhci_t *vh;
mdi_phci_t *ph;
vh = i_devi_get_vhci(vdip);
if (vh == NULL)
return (0);
MDI_VHCI_PHCI_LOCK(vh);
ph = vh->vh_phci_head;
while (ph) {
if (ph->ph_dip && DEVI_BUSY_OWNED(ph->ph_dip)) {
MDI_VHCI_PHCI_UNLOCK(vh);
return (1);
}
ph = ph->ph_next;
}
MDI_VHCI_PHCI_UNLOCK(vh);
return (0);
}
dev_info_t *
mdi_phci_path2devinfo(dev_info_t *vdip, caddr_t pathname)
{
char *temp_pathname;
mdi_vhci_t *vh;
mdi_phci_t *ph;
dev_info_t *pdip = NULL;
vh = i_devi_get_vhci(vdip);
ASSERT(vh != NULL);
if (vh == NULL) {
return (NULL);
}
temp_pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
MDI_VHCI_PHCI_LOCK(vh);
ph = vh->vh_phci_head;
while (ph != NULL) {
pdip = ph->ph_dip;
ASSERT(pdip != NULL);
*temp_pathname = '\0';
(void) ddi_pathname(pdip, temp_pathname);
if (strcmp(temp_pathname, pathname) == 0) {
break;
}
ph = ph->ph_next;
}
if (ph == NULL) {
pdip = NULL;
}
MDI_VHCI_PHCI_UNLOCK(vh);
kmem_free(temp_pathname, MAXPATHLEN);
return (pdip);
}
int
mdi_phci_get_path_count(dev_info_t *pdip)
{
mdi_phci_t *ph;
int count = 0;
ph = i_devi_get_phci(pdip);
if (ph != NULL) {
count = ph->ph_path_count;
}
return (count);
}
static void
i_mdi_phci_lock(mdi_phci_t *ph, mdi_pathinfo_t *pip)
{
if (pip) {
while (MDI_PHCI_TRYLOCK(ph) == 0) {
if (servicing_interrupt()) {
MDI_PI_HOLD(pip);
MDI_PI_UNLOCK(pip);
MDI_PHCI_LOCK(ph);
MDI_PI_LOCK(pip);
MDI_PI_RELE(pip);
break;
} else {
MDI_PI_HOLD(pip);
MDI_PI_UNLOCK(pip);
delay_random(mdi_delay);
MDI_PI_LOCK(pip);
MDI_PI_RELE(pip);
}
}
} else {
MDI_PHCI_LOCK(ph);
}
}
static void
i_mdi_phci_unlock(mdi_phci_t *ph)
{
MDI_PHCI_UNLOCK(ph);
}
static dev_info_t *
i_mdi_devinfo_create(mdi_vhci_t *vh, char *name, char *guid,
char **compatible, int ncompatible)
{
dev_info_t *cdip = NULL;
ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
cdip = i_mdi_devinfo_find(vh, name, guid);
ASSERT(cdip == NULL);
if (cdip) {
cmn_err(CE_WARN,
"i_mdi_devinfo_create: client %s@%s already exists",
name ? name : "", guid ? guid : "");
}
ndi_devi_alloc_sleep(vh->vh_dip, name, DEVI_SID_NODEID, &cdip);
if (cdip == NULL)
goto fail;
if (ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
MDI_CLIENT_GUID_PROP, guid) != DDI_PROP_SUCCESS) {
goto fail;
}
if (compatible &&
(ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip,
"compatible", compatible, ncompatible) != DDI_PROP_SUCCESS)) {
goto fail;
}
return (cdip);
fail:
if (cdip) {
(void) ndi_prop_remove_all(cdip);
(void) ndi_devi_free(cdip);
}
return (NULL);
}
static dev_info_t *
i_mdi_devinfo_find(mdi_vhci_t *vh, caddr_t name, char *guid)
{
char *data;
dev_info_t *cdip = NULL;
dev_info_t *ndip = NULL;
ndi_devi_enter(vh->vh_dip);
ndip = (dev_info_t *)DEVI(vh->vh_dip)->devi_child;
while ((cdip = ndip) != NULL) {
ndip = (dev_info_t *)DEVI(cdip)->devi_sibling;
if (strcmp(DEVI(cdip)->devi_node_name, name)) {
continue;
}
if (ddi_prop_lookup_string(DDI_DEV_T_ANY, cdip,
DDI_PROP_DONTPASS, MDI_CLIENT_GUID_PROP,
&data) != DDI_PROP_SUCCESS) {
continue;
}
if (strcmp(data, guid) != 0) {
ddi_prop_free(data);
continue;
}
ddi_prop_free(data);
break;
}
ndi_devi_exit(vh->vh_dip);
return (cdip);
}
static int
i_mdi_devinfo_remove(dev_info_t *vdip, dev_info_t *cdip, int flags)
{
int rv = MDI_SUCCESS;
if (i_mdi_is_child_present(vdip, cdip) == MDI_SUCCESS ||
(flags & MDI_CLIENT_FLAGS_DEV_NOT_SUPPORTED)) {
rv = ndi_devi_offline(cdip, NDI_DEVFS_CLEAN | NDI_DEVI_REMOVE);
if (rv != NDI_SUCCESS) {
MDI_DEBUG(1, (MDI_NOTE, cdip,
"!failed: cdip %p", (void *)cdip));
}
switch (rv) {
case NDI_SUCCESS:
rv = MDI_SUCCESS;
break;
case NDI_BUSY:
rv = MDI_BUSY;
break;
default:
rv = MDI_FAILURE;
break;
}
}
return (rv);
}
static mdi_client_t *
i_devi_get_client(dev_info_t *cdip)
{
mdi_client_t *ct = NULL;
if (MDI_CLIENT(cdip)) {
ct = (mdi_client_t *)DEVI(cdip)->devi_mdi_client;
}
return (ct);
}
static int
i_mdi_is_child_present(dev_info_t *vdip, dev_info_t *cdip)
{
int rv = MDI_FAILURE;
struct dev_info *dip;
ndi_devi_enter(vdip);
dip = DEVI(vdip)->devi_child;
while (dip) {
if (dip == DEVI(cdip)) {
rv = MDI_SUCCESS;
break;
}
dip = dip->devi_sibling;
}
ndi_devi_exit(vdip);
return (rv);
}
static void
i_mdi_client_lock(mdi_client_t *ct, mdi_pathinfo_t *pip)
{
if (pip) {
while (MDI_CLIENT_TRYLOCK(ct) == 0) {
if (servicing_interrupt()) {
MDI_PI_HOLD(pip);
MDI_PI_UNLOCK(pip);
MDI_CLIENT_LOCK(ct);
MDI_PI_LOCK(pip);
MDI_PI_RELE(pip);
break;
} else {
MDI_PI_HOLD(pip);
MDI_PI_UNLOCK(pip);
delay_random(mdi_delay);
MDI_PI_LOCK(pip);
MDI_PI_RELE(pip);
}
}
} else {
MDI_CLIENT_LOCK(ct);
}
}
static void
i_mdi_client_unlock(mdi_client_t *ct)
{
MDI_CLIENT_UNLOCK(ct);
}
static mdi_client_t *
i_mdi_client_alloc(mdi_vhci_t *vh, char *name, char *lguid)
{
mdi_client_t *ct;
ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
ct = kmem_zalloc(sizeof (*ct), KM_SLEEP);
mutex_init(&ct->ct_mutex, NULL, MUTEX_DEFAULT, NULL);
ct->ct_hnext = NULL;
ct->ct_hprev = NULL;
ct->ct_dip = NULL;
ct->ct_vhci = vh;
ct->ct_drvname = kmem_alloc(strlen(name) + 1, KM_SLEEP);
(void) strcpy(ct->ct_drvname, name);
ct->ct_guid = kmem_alloc(strlen(lguid) + 1, KM_SLEEP);
(void) strcpy(ct->ct_guid, lguid);
ct->ct_cprivate = NULL;
ct->ct_vprivate = NULL;
ct->ct_flags = 0;
ct->ct_state = MDI_CLIENT_STATE_FAILED;
MDI_CLIENT_LOCK(ct);
MDI_CLIENT_SET_OFFLINE(ct);
MDI_CLIENT_SET_DETACH(ct);
MDI_CLIENT_SET_POWER_UP(ct);
MDI_CLIENT_UNLOCK(ct);
ct->ct_failover_flags = 0;
ct->ct_failover_status = 0;
cv_init(&ct->ct_failover_cv, NULL, CV_DRIVER, NULL);
ct->ct_unstable = 0;
cv_init(&ct->ct_unstable_cv, NULL, CV_DRIVER, NULL);
cv_init(&ct->ct_powerchange_cv, NULL, CV_DRIVER, NULL);
ct->ct_lb = vh->vh_lb;
ct->ct_lb_args = kmem_zalloc(sizeof (client_lb_args_t), KM_SLEEP);
ct->ct_lb_args->region_size = LOAD_BALANCE_DEFAULT_REGION_SIZE;
ct->ct_path_count = 0;
ct->ct_path_head = NULL;
ct->ct_path_tail = NULL;
ct->ct_path_last = NULL;
i_mdi_client_enlist_table(vh, ct);
return (ct);
}
static void
i_mdi_client_enlist_table(mdi_vhci_t *vh, mdi_client_t *ct)
{
int index;
struct client_hash *head;
ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
index = i_mdi_get_hash_key(ct->ct_guid);
head = &vh->vh_client_table[index];
ct->ct_hnext = (mdi_client_t *)head->ct_hash_head;
head->ct_hash_head = ct;
head->ct_hash_count++;
vh->vh_client_count++;
}
static void
i_mdi_client_delist_table(mdi_vhci_t *vh, mdi_client_t *ct)
{
int index;
char *guid;
struct client_hash *head;
mdi_client_t *next;
mdi_client_t *last;
ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
guid = ct->ct_guid;
index = i_mdi_get_hash_key(guid);
head = &vh->vh_client_table[index];
last = NULL;
next = (mdi_client_t *)head->ct_hash_head;
while (next != NULL) {
if (next == ct) {
break;
}
last = next;
next = next->ct_hnext;
}
if (next) {
head->ct_hash_count--;
if (last == NULL) {
head->ct_hash_head = ct->ct_hnext;
} else {
last->ct_hnext = ct->ct_hnext;
}
ct->ct_hnext = NULL;
vh->vh_client_count--;
}
}
static int
i_mdi_client_free(mdi_vhci_t *vh, mdi_client_t *ct)
{
int rv = MDI_SUCCESS;
int flags = ct->ct_flags;
dev_info_t *cdip;
dev_info_t *vdip;
ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
vdip = vh->vh_dip;
cdip = ct->ct_dip;
(void) ndi_prop_remove(DDI_DEV_T_NONE, cdip, MDI_CLIENT_GUID_PROP);
DEVI(cdip)->devi_mdi_component &= ~MDI_COMPONENT_CLIENT;
DEVI(cdip)->devi_mdi_client = NULL;
ct->ct_dip = NULL;
i_mdi_client_delist_table(vh, ct);
kmem_free(ct->ct_drvname, strlen(ct->ct_drvname) + 1);
kmem_free(ct->ct_guid, strlen(ct->ct_guid) + 1);
kmem_free(ct->ct_lb_args, sizeof (client_lb_args_t));
cv_destroy(&ct->ct_failover_cv);
cv_destroy(&ct->ct_unstable_cv);
cv_destroy(&ct->ct_powerchange_cv);
mutex_destroy(&ct->ct_mutex);
kmem_free(ct, sizeof (*ct));
MDI_VHCI_CLIENT_UNLOCK(vh);
(void) i_mdi_devinfo_remove(vdip, cdip, flags);
MDI_VHCI_CLIENT_LOCK(vh);
return (rv);
}
static mdi_client_t *
i_mdi_client_find(mdi_vhci_t *vh, char *cname, char *guid)
{
int index;
struct client_hash *head;
mdi_client_t *ct;
ASSERT(MDI_VHCI_CLIENT_LOCKED(vh));
index = i_mdi_get_hash_key(guid);
head = &vh->vh_client_table[index];
ct = head->ct_hash_head;
while (ct != NULL) {
if (strcmp(ct->ct_guid, guid) == 0 &&
(cname == NULL || strcmp(ct->ct_drvname, cname) == 0)) {
break;
}
ct = ct->ct_hnext;
}
return (ct);
}
static void
i_mdi_client_update_state(mdi_client_t *ct)
{
int state;
ASSERT(MDI_CLIENT_LOCKED(ct));
state = i_mdi_client_compute_state(ct, NULL);
MDI_CLIENT_SET_STATE(ct, state);
}
static int
i_mdi_client_compute_state(mdi_client_t *ct, mdi_phci_t *ph)
{
int state;
int online_count = 0;
int standby_count = 0;
mdi_pathinfo_t *pip, *next;
ASSERT(MDI_CLIENT_LOCKED(ct));
pip = ct->ct_path_head;
while (pip != NULL) {
MDI_PI_LOCK(pip);
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
if (MDI_PI(pip)->pi_phci == ph) {
MDI_PI_UNLOCK(pip);
pip = next;
continue;
}
if ((MDI_PI(pip)->pi_state & MDI_PATHINFO_STATE_MASK)
== MDI_PATHINFO_STATE_ONLINE)
online_count++;
else if ((MDI_PI(pip)->pi_state & MDI_PATHINFO_STATE_MASK)
== MDI_PATHINFO_STATE_STANDBY)
standby_count++;
MDI_PI_UNLOCK(pip);
pip = next;
}
if (online_count == 0) {
if (standby_count == 0) {
state = MDI_CLIENT_STATE_FAILED;
MDI_DEBUG(2, (MDI_NOTE, ct->ct_dip,
"client state failed: ct = %p", (void *)ct));
} else if (standby_count == 1) {
state = MDI_CLIENT_STATE_DEGRADED;
} else {
state = MDI_CLIENT_STATE_OPTIMAL;
}
} else if (online_count == 1) {
if (standby_count == 0) {
state = MDI_CLIENT_STATE_DEGRADED;
} else {
state = MDI_CLIENT_STATE_OPTIMAL;
}
} else {
state = MDI_CLIENT_STATE_OPTIMAL;
}
return (state);
}
dev_info_t *
i_mdi_client2devinfo(mdi_client_t *ct)
{
return (ct->ct_dip);
}
dev_info_t *
mdi_client_path2devinfo(dev_info_t *vdip, char *pathname)
{
dev_info_t *cdip = NULL;
dev_info_t *ndip = NULL;
char *temp_pathname;
temp_pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
ndi_devi_enter(vdip);
ndip = (dev_info_t *)DEVI(vdip)->devi_child;
while ((cdip = ndip) != NULL) {
ndip = (dev_info_t *)DEVI(cdip)->devi_sibling;
*temp_pathname = '\0';
(void) ddi_pathname(cdip, temp_pathname);
if (strcmp(temp_pathname, pathname) == 0) {
break;
}
}
ndi_devi_exit(vdip);
kmem_free(temp_pathname, MAXPATHLEN);
return (cdip);
}
int
mdi_client_get_path_count(dev_info_t *cdip)
{
mdi_client_t *ct;
int count = 0;
ct = i_devi_get_client(cdip);
if (ct != NULL) {
count = ct->ct_path_count;
}
return (count);
}
static int
i_mdi_get_hash_key(char *str)
{
uint32_t g, hash = 0;
char *p;
for (p = str; *p != '\0'; p++) {
g = *p;
hash += g;
}
return (hash % (CLIENT_HASH_TABLE_SIZE - 1));
}
client_lb_t
mdi_get_lb_policy(dev_info_t *cdip)
{
client_lb_t lb = LOAD_BALANCE_NONE;
mdi_client_t *ct;
ct = i_devi_get_client(cdip);
if (ct != NULL) {
lb = ct->ct_lb;
}
return (lb);
}
int
mdi_set_lb_region_size(dev_info_t *cdip, int region_size)
{
mdi_client_t *ct;
int rv = MDI_FAILURE;
ct = i_devi_get_client(cdip);
if (ct != NULL && ct->ct_lb_args != NULL) {
ct->ct_lb_args->region_size = region_size;
rv = MDI_SUCCESS;
}
return (rv);
}
int
mdi_set_lb_policy(dev_info_t *cdip, client_lb_t lb)
{
mdi_client_t *ct;
int rv = MDI_FAILURE;
ct = i_devi_get_client(cdip);
if (ct != NULL) {
ct->ct_lb = lb;
rv = MDI_SUCCESS;
}
return (rv);
}
static void
mdi_failover_cb(void *arg)
{
(void)i_mdi_failover(arg);
}
int
mdi_failover(dev_info_t *vdip, dev_info_t *cdip, int flags)
{
int rv;
mdi_client_t *ct;
ct = i_devi_get_client(cdip);
ASSERT(ct != NULL);
if (ct == NULL) {
return (MDI_FAILURE);
}
MDI_CLIENT_LOCK(ct);
if (MDI_CLIENT_IS_PATH_FREE_IN_PROGRESS(ct)) {
MDI_CLIENT_UNLOCK(ct);
return (MDI_BUSY);
}
if (MDI_CLIENT_IS_FAILED(ct)) {
MDI_CLIENT_UNLOCK(ct);
return (MDI_FAILURE);
}
if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
MDI_CLIENT_UNLOCK(ct);
return (MDI_BUSY);
}
if (ct->ct_unstable) {
if (flags == MDI_FAILOVER_ASYNC) {
MDI_CLIENT_UNLOCK(ct);
return (MDI_BUSY);
} else {
while (ct->ct_unstable)
cv_wait(&ct->ct_unstable_cv, &ct->ct_mutex);
}
}
if ((MDI_CLIENT_IS_DETACHED(ct)) || (MDI_CLIENT_IS_FAILED(ct)) ||
(!i_ddi_devi_attached(cdip))) {
MDI_CLIENT_UNLOCK(ct);
return (MDI_FAILURE);
}
MDI_CLIENT_SET_FAILOVER_IN_PROGRESS(ct);
ct->ct_failover_flags = flags;
MDI_CLIENT_UNLOCK(ct);
if (flags == MDI_FAILOVER_ASYNC) {
(void) taskq_dispatch(mdi_taskq, mdi_failover_cb, ct, KM_SLEEP);
return (MDI_ACCEPT);
} else {
rv = i_mdi_failover(ct);
}
return (rv);
}
static int
i_mdi_failover(void *arg)
{
int rv = MDI_SUCCESS;
mdi_client_t *ct = (mdi_client_t *)arg;
mdi_vhci_t *vh = ct->ct_vhci;
ASSERT(!MDI_CLIENT_LOCKED(ct));
if (vh->vh_ops->vo_failover != NULL) {
rv = (*vh->vh_ops->vo_failover)(vh->vh_dip, ct->ct_dip,
ct->ct_failover_flags);
}
MDI_CLIENT_LOCK(ct);
MDI_CLIENT_CLEAR_FAILOVER_IN_PROGRESS(ct);
ct->ct_failover_status = rv;
i_mdi_client_update_state(ct);
cv_broadcast(&ct->ct_failover_cv);
MDI_CLIENT_UNLOCK(ct);
return (rv);
}
static int
i_mdi_lba_lb(mdi_client_t *ct, mdi_pathinfo_t **ret_pip, struct buf *bp)
{
int path_index = -1;
int online_path_count = 0;
int online_nonpref_path_count = 0;
int region_size = ct->ct_lb_args->region_size;
mdi_pathinfo_t *pip;
mdi_pathinfo_t *next;
int preferred, path_cnt;
pip = ct->ct_path_head;
while (pip) {
MDI_PI_LOCK(pip);
if (MDI_PI(pip)->pi_state ==
MDI_PATHINFO_STATE_ONLINE && MDI_PI(pip)->pi_preferred) {
online_path_count++;
} else if (MDI_PI(pip)->pi_state ==
MDI_PATHINFO_STATE_ONLINE && !MDI_PI(pip)->pi_preferred) {
online_nonpref_path_count++;
}
next = (mdi_pathinfo_t *)
MDI_PI(pip)->pi_client_link;
MDI_PI_UNLOCK(pip);
pip = next;
}
if (online_path_count > 0) {
path_cnt = online_path_count;
preferred = 1;
} else if (online_nonpref_path_count > 0) {
path_cnt = online_nonpref_path_count;
preferred = 0;
} else {
path_cnt = 0;
}
if (path_cnt) {
path_index = (bp->b_blkno >> region_size) % path_cnt;
pip = ct->ct_path_head;
while (pip && path_index != -1) {
MDI_PI_LOCK(pip);
if (path_index == 0 &&
(MDI_PI(pip)->pi_state ==
MDI_PATHINFO_STATE_ONLINE) &&
MDI_PI(pip)->pi_preferred == preferred) {
MDI_PI_HOLD(pip);
MDI_PI_UNLOCK(pip);
*ret_pip = pip;
return (MDI_SUCCESS);
}
path_index --;
next = (mdi_pathinfo_t *)
MDI_PI(pip)->pi_client_link;
MDI_PI_UNLOCK(pip);
pip = next;
}
MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
"lba %llx: path %s %p",
bp->b_lblkno, mdi_pi_spathname(pip), (void *)pip));
}
return (MDI_FAILURE);
}
int
mdi_select_path(dev_info_t *cdip, struct buf *bp, int flags,
void *arg, mdi_pathinfo_t **ret_pip)
{
mdi_client_t *ct;
mdi_pathinfo_t *pip;
mdi_pathinfo_t *next;
mdi_pathinfo_t *head;
mdi_pathinfo_t *start;
client_lb_t lbp;
int sb = 1;
int preferred = 1;
int cond, cont = 1;
int retry = 0;
mdi_pathinfo_t *start_pip;
int path_instance;
if (flags & MDI_SELECT_PATH_INSTANCE) {
path_instance = (int)(intptr_t)arg;
start_pip = NULL;
} else {
path_instance = 0;
start_pip = (mdi_pathinfo_t *)arg;
}
if (flags != 0) {
sb = 0;
}
*ret_pip = NULL;
ct = i_devi_get_client(cdip);
if (ct == NULL) {
return (MDI_FAILURE);
}
MDI_CLIENT_LOCK(ct);
if (sb) {
if (MDI_CLIENT_IS_FAILED(ct)) {
MDI_DEBUG(2, (MDI_NOTE, cdip,
"client state offline ct = %p", (void *)ct));
MDI_CLIENT_UNLOCK(ct);
return (MDI_FAILURE);
}
if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
MDI_DEBUG(2, (MDI_NOTE, cdip,
"client failover in progress ct = %p",
(void *)ct));
MDI_CLIENT_UNLOCK(ct);
return (MDI_BUSY);
}
if (MDI_CLIENT_IS_DETACHED(ct) || !i_ddi_devi_attached(cdip)) {
MDI_DEBUG(4, (MDI_NOTE, cdip,
"devi is onlining ct = %p", (void *)ct));
MDI_CLIENT_UNLOCK(ct);
return (MDI_DEVI_ONLINING);
}
}
head = ct->ct_path_head;
if (head == NULL) {
MDI_CLIENT_UNLOCK(ct);
return (MDI_NOPATH);
}
if (path_instance) {
for (pip = head;
pip && (mdi_pi_get_path_instance(pip) != path_instance);
pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link)
;
if (pip == NULL) {
MDI_CLIENT_UNLOCK(ct);
return (MDI_NOPATH);
}
MDI_PI_LOCK(pip);
if ((MDI_PI(pip)->pi_state == MDI_PATHINFO_STATE_INIT) ||
(MDI_PI(pip)->pi_phci == NULL)) {
MDI_PI_UNLOCK(pip);
MDI_CLIENT_UNLOCK(ct);
return (MDI_FAILURE);
}
if (MDI_PI_IS_TRANSIENT(pip)) {
MDI_PI_UNLOCK(pip);
MDI_CLIENT_UNLOCK(ct);
return (MDI_BUSY);
}
MDI_PI_HOLD(pip);
MDI_PI_UNLOCK(pip);
*ret_pip = pip;
MDI_CLIENT_UNLOCK(ct);
return (MDI_SUCCESS);
}
lbp = sb ? ct->ct_lb : LOAD_BALANCE_RR;
switch (lbp) {
case LOAD_BALANCE_NONE:
preferred = 1;
pip = (mdi_pathinfo_t *)ct->ct_path_last;
if (pip == NULL) {
pip = head;
}
start = pip;
do {
MDI_PI_LOCK(pip);
if ((MDI_PI(pip)->pi_state ==
MDI_PATHINFO_STATE_ONLINE) &&
preferred == MDI_PI(pip)->pi_preferred) {
MDI_PI_HOLD(pip);
MDI_PI_UNLOCK(pip);
ct->ct_path_last = pip;
*ret_pip = pip;
MDI_CLIENT_UNLOCK(ct);
return (MDI_SUCCESS);
}
if (MDI_PI_IS_DRV_DISABLE_TRANSIENT(pip) ||
MDI_PI_IS_TRANSIENT(pip))
retry = 1;
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
if (next == NULL) {
next = head;
}
MDI_PI_UNLOCK(pip);
pip = next;
if (start == pip && preferred) {
preferred = 0;
} else if (start == pip && !preferred) {
cont = 0;
}
} while (cont);
break;
case LOAD_BALANCE_LBA:
if ((ct->ct_lb_args != NULL) &&
(ct->ct_lb_args->region_size) && bp &&
(sb || (flags == MDI_SELECT_ONLINE_PATH))) {
if (i_mdi_lba_lb(ct, ret_pip, bp)
== MDI_SUCCESS) {
MDI_CLIENT_UNLOCK(ct);
return (MDI_SUCCESS);
}
}
case LOAD_BALANCE_RR:
if (start_pip != NULL) {
preferred = MDI_PI(start_pip)->pi_preferred;
} else {
preferred = 1;
}
start = sb ? (mdi_pathinfo_t *)ct->ct_path_last : start_pip;
if (start == NULL) {
pip = head;
} else {
pip = (mdi_pathinfo_t *)MDI_PI(start)->pi_client_link;
if (pip == NULL) {
if ( flags & MDI_SELECT_NO_PREFERRED) {
MDI_CLIENT_UNLOCK(ct);
return (MDI_NOPATH);
}
if (!sb) {
if (preferred == 0) {
*ret_pip = NULL;
MDI_CLIENT_UNLOCK(ct);
return (MDI_NOPATH);
} else {
preferred = 0;
}
}
pip = head;
}
}
start = pip;
do {
MDI_PI_LOCK(pip);
if (sb) {
cond = ((MDI_PI(pip)->pi_state ==
MDI_PATHINFO_STATE_ONLINE &&
MDI_PI(pip)->pi_preferred ==
preferred) ? 1 : 0);
} else {
if (flags == MDI_SELECT_ONLINE_PATH) {
cond = ((MDI_PI(pip)->pi_state ==
MDI_PATHINFO_STATE_ONLINE &&
MDI_PI(pip)->pi_preferred ==
preferred) ? 1 : 0);
} else if (flags == MDI_SELECT_STANDBY_PATH) {
cond = ((MDI_PI(pip)->pi_state ==
MDI_PATHINFO_STATE_STANDBY &&
MDI_PI(pip)->pi_preferred ==
preferred) ? 1 : 0);
} else if (flags == (MDI_SELECT_ONLINE_PATH |
MDI_SELECT_STANDBY_PATH)) {
cond = (((MDI_PI(pip)->pi_state ==
MDI_PATHINFO_STATE_ONLINE ||
(MDI_PI(pip)->pi_state ==
MDI_PATHINFO_STATE_STANDBY)) &&
MDI_PI(pip)->pi_preferred ==
preferred) ? 1 : 0);
} else if (flags ==
(MDI_SELECT_STANDBY_PATH |
MDI_SELECT_ONLINE_PATH |
MDI_SELECT_USER_DISABLE_PATH)) {
cond = (((MDI_PI(pip)->pi_state ==
MDI_PATHINFO_STATE_ONLINE ||
(MDI_PI(pip)->pi_state ==
MDI_PATHINFO_STATE_STANDBY) ||
(MDI_PI(pip)->pi_state ==
(MDI_PATHINFO_STATE_ONLINE|
MDI_PATHINFO_STATE_USER_DISABLE)) ||
(MDI_PI(pip)->pi_state ==
(MDI_PATHINFO_STATE_STANDBY |
MDI_PATHINFO_STATE_USER_DISABLE)))&&
MDI_PI(pip)->pi_preferred ==
preferred) ? 1 : 0);
} else if (flags ==
(MDI_SELECT_STANDBY_PATH |
MDI_SELECT_ONLINE_PATH |
MDI_SELECT_NO_PREFERRED)) {
cond = (((MDI_PI(pip)->pi_state ==
MDI_PATHINFO_STATE_ONLINE) ||
(MDI_PI(pip)->pi_state ==
MDI_PATHINFO_STATE_STANDBY))
? 1 : 0);
} else {
cond = 0;
}
}
if (cond) {
MDI_PI_HOLD(pip);
MDI_PI_UNLOCK(pip);
if (sb)
ct->ct_path_last = pip;
*ret_pip = pip;
MDI_CLIENT_UNLOCK(ct);
return (MDI_SUCCESS);
}
if (MDI_PI_IS_DRV_DISABLE_TRANSIENT(pip) ||
MDI_PI_IS_TRANSIENT(pip))
retry = 1;
do_again:
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
if (next == NULL) {
if ( flags & MDI_SELECT_NO_PREFERRED) {
MDI_PI_UNLOCK(pip);
break;
}
if (!sb) {
if (preferred == 1) {
preferred = 0;
next = head;
} else {
cont = 0;
}
} else {
next = head;
}
}
MDI_PI_UNLOCK(pip);
if (cont == 0) {
break;
}
pip = next;
if (!sb) {
if ((start_pip != NULL) && (start_pip == pip) &&
(MDI_PI(start_pip)->pi_preferred
!= preferred)) {
MDI_PI_LOCK(pip);
goto do_again;
}
} else {
if (start == pip && preferred) {
preferred = 0;
} else if (start == pip && !preferred) {
cont = 0;
}
}
} while (cont);
break;
}
MDI_CLIENT_UNLOCK(ct);
if (retry == 1) {
return (MDI_BUSY);
} else {
return (MDI_NOPATH);
}
}
mdi_pathinfo_t *
mdi_get_next_phci_path(dev_info_t *ct_dip, mdi_pathinfo_t *pip)
{
mdi_client_t *ct;
if (!MDI_CLIENT(ct_dip))
return (NULL);
ct = (mdi_client_t *)DEVI(ct_dip)->devi_mdi_client;
ASSERT(ct != NULL);
if (pip == NULL)
return ((mdi_pathinfo_t *)ct->ct_path_head);
return ((mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link);
}
mdi_pathinfo_t *
mdi_get_next_client_path(dev_info_t *ph_dip, mdi_pathinfo_t *pip)
{
mdi_phci_t *ph;
if (!MDI_PHCI(ph_dip))
return (NULL);
ph = (mdi_phci_t *)DEVI(ph_dip)->devi_mdi_xhci;
ASSERT(ph != NULL);
if (pip == NULL)
return ((mdi_pathinfo_t *)ph->ph_path_head);
return ((mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link);
}
void
mdi_hold_path(mdi_pathinfo_t *pip)
{
if (pip) {
MDI_PI_LOCK(pip);
MDI_PI_HOLD(pip);
MDI_PI_UNLOCK(pip);
}
}
void
mdi_rele_path(mdi_pathinfo_t *pip)
{
if (pip) {
MDI_PI_LOCK(pip);
MDI_PI_RELE(pip);
if (MDI_PI(pip)->pi_ref_cnt == 0) {
cv_broadcast(&MDI_PI(pip)->pi_ref_cv);
}
MDI_PI_UNLOCK(pip);
}
}
void
mdi_pi_lock(mdi_pathinfo_t *pip)
{
ASSERT(pip != NULL);
if (pip) {
MDI_PI_LOCK(pip);
}
}
void
mdi_pi_unlock(mdi_pathinfo_t *pip)
{
ASSERT(pip != NULL);
if (pip) {
MDI_PI_UNLOCK(pip);
}
}
mdi_pathinfo_t *
mdi_pi_find(dev_info_t *pdip, char *caddr, char *paddr)
{
mdi_phci_t *ph;
mdi_vhci_t *vh;
mdi_client_t *ct;
mdi_pathinfo_t *pip = NULL;
MDI_DEBUG(2, (MDI_NOTE, pdip,
"caddr@%s paddr@%s", caddr ? caddr : "", paddr ? paddr : ""));
if ((pdip == NULL) || (paddr == NULL)) {
return (NULL);
}
ph = i_devi_get_phci(pdip);
if (ph == NULL) {
MDI_DEBUG(2, (MDI_WARN, pdip, "invalid phci"));
return (NULL);
}
vh = ph->ph_vhci;
if (vh == NULL) {
MDI_DEBUG(2, (MDI_WARN, pdip, "invalid vhci"));
return (NULL);
}
if (caddr == NULL) {
MDI_PHCI_LOCK(ph);
if (MDI_PHCI_IS_OFFLINE(ph)) {
MDI_DEBUG(2, (MDI_WARN, pdip,
"offline phci %p", (void *)ph));
MDI_PHCI_UNLOCK(ph);
return (NULL);
}
pip = (mdi_pathinfo_t *)ph->ph_path_head;
while (pip != NULL) {
if (strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
break;
}
pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
}
MDI_PHCI_UNLOCK(ph);
MDI_DEBUG(2, (MDI_NOTE, pdip,
"found %s %p", mdi_pi_spathname(pip), (void *)pip));
return (pip);
}
MDI_VHCI_CLIENT_LOCK(vh);
ct = i_mdi_client_find(vh, NULL, caddr);
if (ct == NULL) {
MDI_VHCI_CLIENT_UNLOCK(vh);
MDI_DEBUG(2, (MDI_NOTE, pdip,
"client not found for caddr @%s", caddr ? caddr : ""));
return (NULL);
}
MDI_CLIENT_LOCK(ct);
MDI_VHCI_CLIENT_UNLOCK(vh);
pip = (mdi_pathinfo_t *)ct->ct_path_head;
while (pip != NULL) {
if ((MDI_PI(pip)->pi_phci == ph) &&
strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
break;
}
pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
}
MDI_CLIENT_UNLOCK(ct);
MDI_DEBUG(2, (MDI_NOTE, pdip,
"found: %s %p", mdi_pi_spathname(pip), (void *)pip));
return (pip);
}
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)
{
mdi_vhci_t *vh;
mdi_phci_t *ph;
mdi_client_t *ct;
mdi_pathinfo_t *pip = NULL;
dev_info_t *cdip;
int rv = MDI_NOMEM;
int path_allocated = 0;
MDI_DEBUG(2, (MDI_NOTE, pdip,
"cname %s: caddr@%s paddr@%s",
cname ? cname : "", caddr ? caddr : "", paddr ? paddr : ""));
if (pdip == NULL || cname == NULL || caddr == NULL || paddr == NULL ||
ret_pip == NULL) {
return (MDI_FAILURE);
}
*ret_pip = NULL;
if (DEVI_IS_DETACHING(pdip)) {
MDI_DEBUG(1, (MDI_WARN, pdip,
"!detaching pHCI=%p", (void *)pdip));
return (MDI_FAILURE);
}
ph = i_devi_get_phci(pdip);
ASSERT(ph != NULL);
if (ph == NULL) {
MDI_DEBUG(1, (MDI_WARN, pdip,
"!invalid pHCI=%p", (void *)pdip));
return (MDI_FAILURE);
}
MDI_PHCI_LOCK(ph);
vh = ph->ph_vhci;
if (vh == NULL) {
MDI_DEBUG(1, (MDI_WARN, pdip,
"!invalid vHCI=%p", (void *)pdip));
MDI_PHCI_UNLOCK(ph);
return (MDI_FAILURE);
}
if (MDI_PHCI_IS_READY(ph) == 0) {
MDI_DEBUG(1, (MDI_WARN, pdip,
"pHCI=%p is not ready", (void *)ph));
MDI_PHCI_UNLOCK(ph);
return (MDI_BUSY);
}
MDI_PHCI_UNSTABLE(ph);
MDI_PHCI_UNLOCK(ph);
MDI_VHCI_CLIENT_LOCK(vh);
ct = i_mdi_client_find(vh, cname, caddr);
if (ct == NULL) {
ct = i_mdi_client_alloc(vh, cname, caddr);
ASSERT(ct != NULL);
}
if (ct->ct_dip == NULL) {
ct->ct_dip = i_mdi_devinfo_create(vh, cname, caddr,
compatible, ncompatible);
if (ct->ct_dip == NULL) {
(void) i_mdi_client_free(vh, ct);
goto fail;
}
}
cdip = ct->ct_dip;
DEVI(cdip)->devi_mdi_component |= MDI_COMPONENT_CLIENT;
DEVI(cdip)->devi_mdi_client = (caddr_t)ct;
MDI_CLIENT_LOCK(ct);
pip = (mdi_pathinfo_t *)ct->ct_path_head;
while (pip != NULL) {
if ((MDI_PI(pip)->pi_phci == ph) &&
strcmp(MDI_PI(pip)->pi_addr, paddr) == 0) {
break;
}
pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
}
MDI_CLIENT_UNLOCK(ct);
if (pip == NULL) {
pip = i_mdi_pi_alloc(ph, paddr, ct);
ASSERT(pip != NULL);
path_allocated = 1;
}
rv = MDI_SUCCESS;
fail:
MDI_VHCI_CLIENT_UNLOCK(vh);
MDI_PHCI_LOCK(ph);
MDI_PHCI_STABLE(ph);
MDI_PHCI_UNLOCK(ph);
*ret_pip = pip;
MDI_DEBUG(2, (MDI_NOTE, pdip,
"alloc %s %p", mdi_pi_spathname(pip), (void *)pip));
if (path_allocated)
vhcache_pi_add(vh->vh_config, MDI_PI(pip));
return (rv);
}
int
mdi_pi_alloc(dev_info_t *pdip, char *cname, char *caddr, char *paddr,
int flags, mdi_pathinfo_t **ret_pip)
{
return (mdi_pi_alloc_compatible(pdip, cname, caddr, paddr, NULL, 0,
flags, ret_pip));
}
static mdi_pathinfo_t *
i_mdi_pi_alloc(mdi_phci_t *ph, char *paddr, mdi_client_t *ct)
{
mdi_pathinfo_t *pip;
static char path[MAXPATHLEN];
char *path_persistent;
int path_instance;
mod_hash_val_t hv;
ASSERT(MDI_VHCI_CLIENT_LOCKED(ph->ph_vhci));
pip = kmem_zalloc(sizeof (struct mdi_pathinfo), KM_SLEEP);
mutex_init(&MDI_PI(pip)->pi_mutex, NULL, MUTEX_DEFAULT, NULL);
MDI_PI(pip)->pi_state = MDI_PATHINFO_STATE_INIT |
MDI_PATHINFO_STATE_TRANSIENT;
if (MDI_PHCI_IS_USER_DISABLED(ph))
MDI_PI_SET_USER_DISABLE(pip);
if (MDI_PHCI_IS_DRV_DISABLED_TRANSIENT(ph))
MDI_PI_SET_DRV_DISABLE_TRANS(pip);
if (MDI_PHCI_IS_DRV_DISABLED(ph))
MDI_PI_SET_DRV_DISABLE(pip);
MDI_PI(pip)->pi_old_state = MDI_PATHINFO_STATE_INIT;
cv_init(&MDI_PI(pip)->pi_state_cv, NULL, CV_DEFAULT, NULL);
MDI_PI(pip)->pi_client = ct;
MDI_PI(pip)->pi_phci = ph;
MDI_PI(pip)->pi_addr = kmem_alloc(strlen(paddr) + 1, KM_SLEEP);
(void) strcpy(MDI_PI(pip)->pi_addr, paddr);
mutex_enter(&mdi_pathmap_mutex);
(void) ddi_pathname(ph->ph_dip, path);
(void) sprintf(path + strlen(path), "/%s@%s",
mdi_pi_get_node_name(pip), mdi_pi_get_addr(pip));
if (mod_hash_find(mdi_pathmap_bypath, (mod_hash_key_t)path, &hv) == 0) {
path_instance = (uint_t)(intptr_t)hv;
} else {
path_instance = mdi_pathmap_instance++;
path_persistent = i_ddi_strdup(path, KM_SLEEP);
(void) mod_hash_insert(mdi_pathmap_bypath,
(mod_hash_key_t)path_persistent,
(mod_hash_val_t)(intptr_t)path_instance);
(void) mod_hash_insert(mdi_pathmap_byinstance,
(mod_hash_key_t)(intptr_t)path_instance,
(mod_hash_val_t)path_persistent);
(void) snprintf(path, sizeof(path), "%s%d/%s@%s",
ddi_driver_name(ph->ph_dip), ddi_get_instance(ph->ph_dip),
mdi_pi_get_node_name(pip), mdi_pi_get_addr(pip));
path_persistent = i_ddi_strdup(path, KM_SLEEP);
(void) mod_hash_insert(mdi_pathmap_sbyinstance,
(mod_hash_key_t)(intptr_t)path_instance,
(mod_hash_val_t)path_persistent);
}
mutex_exit(&mdi_pathmap_mutex);
MDI_PI(pip)->pi_path_instance = path_instance;
(void) nvlist_alloc(&MDI_PI(pip)->pi_prop, NV_UNIQUE_NAME, KM_SLEEP);
ASSERT(MDI_PI(pip)->pi_prop != NULL);
MDI_PI(pip)->pi_pprivate = NULL;
MDI_PI(pip)->pi_cprivate = NULL;
MDI_PI(pip)->pi_vprivate = NULL;
MDI_PI(pip)->pi_client_link = NULL;
MDI_PI(pip)->pi_phci_link = NULL;
MDI_PI(pip)->pi_ref_cnt = 0;
MDI_PI(pip)->pi_kstats = NULL;
MDI_PI(pip)->pi_preferred = 1;
cv_init(&MDI_PI(pip)->pi_ref_cv, NULL, CV_DEFAULT, NULL);
ndi_devi_enter(ct->ct_dip);
ndi_devi_enter(ph->ph_dip);
i_mdi_phci_add_path(ph, pip);
i_mdi_client_add_path(ct, pip);
ndi_devi_exit(ph->ph_dip);
ndi_devi_exit(ct->ct_dip);
return (pip);
}
char *
mdi_pi_pathname_by_instance(int path_instance)
{
char *path;
mod_hash_val_t hv;
mutex_enter(&mdi_pathmap_mutex);
if (mod_hash_find(mdi_pathmap_byinstance,
(mod_hash_key_t)(intptr_t)path_instance, &hv) == 0)
path = (char *)hv;
else
path = NULL;
mutex_exit(&mdi_pathmap_mutex);
return (path);
}
char *
mdi_pi_spathname_by_instance(int path_instance)
{
char *path;
mod_hash_val_t hv;
mutex_enter(&mdi_pathmap_mutex);
if (mod_hash_find(mdi_pathmap_sbyinstance,
(mod_hash_key_t)(intptr_t)path_instance, &hv) == 0)
path = (char *)hv;
else
path = NULL;
mutex_exit(&mdi_pathmap_mutex);
return (path);
}
static void
i_mdi_phci_add_path(mdi_phci_t *ph, mdi_pathinfo_t *pip)
{
ASSERT(DEVI_BUSY_OWNED(ph->ph_dip));
MDI_PHCI_LOCK(ph);
if (ph->ph_path_head == NULL) {
ph->ph_path_head = pip;
} else {
MDI_PI(ph->ph_path_tail)->pi_phci_link = MDI_PI(pip);
}
ph->ph_path_tail = pip;
ph->ph_path_count++;
MDI_PHCI_UNLOCK(ph);
}
static void
i_mdi_client_add_path(mdi_client_t *ct, mdi_pathinfo_t *pip)
{
ASSERT(DEVI_BUSY_OWNED(ct->ct_dip));
MDI_CLIENT_LOCK(ct);
if (ct->ct_path_head == NULL) {
ct->ct_path_head = pip;
} else {
MDI_PI(ct->ct_path_tail)->pi_client_link = MDI_PI(pip);
}
ct->ct_path_tail = pip;
ct->ct_path_count++;
MDI_CLIENT_UNLOCK(ct);
}
int
mdi_pi_free(mdi_pathinfo_t *pip, int flags)
{
int rv;
mdi_vhci_t *vh;
mdi_phci_t *ph;
mdi_client_t *ct;
int (*f)();
int client_held = 0;
MDI_PI_LOCK(pip);
ph = MDI_PI(pip)->pi_phci;
ASSERT(ph != NULL);
if (ph == NULL) {
MDI_DEBUG(1, (MDI_WARN, NULL,
"!invalid pHCI: pip %s %p",
mdi_pi_spathname(pip), (void *)pip));
MDI_PI_UNLOCK(pip);
return (MDI_FAILURE);
}
vh = ph->ph_vhci;
ASSERT(vh != NULL);
if (vh == NULL) {
MDI_DEBUG(1, (MDI_WARN, ph->ph_dip,
"!invalid vHCI: pip %s %p",
mdi_pi_spathname(pip), (void *)pip));
MDI_PI_UNLOCK(pip);
return (MDI_FAILURE);
}
ct = MDI_PI(pip)->pi_client;
ASSERT(ct != NULL);
if (ct == NULL) {
MDI_DEBUG(1, (MDI_WARN, ph->ph_dip,
"!invalid client: pip %s %p",
mdi_pi_spathname(pip), (void *)pip));
MDI_PI_UNLOCK(pip);
return (MDI_FAILURE);
}
if (!(MDI_PI_IS_OFFLINE(pip) || MDI_PI_IS_INIT(pip) ||
MDI_PI_IS_INITING(pip))) {
MDI_DEBUG(1, (MDI_WARN, ct->ct_dip,
"!busy: pip %s %p", mdi_pi_spathname(pip), (void *)pip));
MDI_PI_UNLOCK(pip);
return (MDI_BUSY);
}
while (MDI_PI(pip)->pi_ref_cnt != 0) {
MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
"!%d cmds still pending on path: %s %p",
MDI_PI(pip)->pi_ref_cnt,
mdi_pi_spathname(pip), (void *)pip));
if (cv_reltimedwait(&MDI_PI(pip)->pi_ref_cv,
&MDI_PI(pip)->pi_mutex, drv_usectohz(60 * 1000000),
TR_CLOCK_TICK) == -1) {
MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
"!Timeout reached on path %s %p without the cond",
mdi_pi_spathname(pip), (void *)pip));
MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
"!%d cmds still pending on path %s %p",
MDI_PI(pip)->pi_ref_cnt,
mdi_pi_spathname(pip), (void *)pip));
MDI_PI_UNLOCK(pip);
return (MDI_BUSY);
}
}
if (MDI_PI(pip)->pi_pm_held) {
client_held = 1;
}
MDI_PI_UNLOCK(pip);
vhcache_pi_remove(vh->vh_config, MDI_PI(pip));
MDI_CLIENT_LOCK(ct);
MDI_CLIENT_SET_PATH_FREE_IN_PROGRESS(ct);
while (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct))
cv_wait(&ct->ct_failover_cv, &ct->ct_mutex);
MDI_CLIENT_UNLOCK(ct);
MDI_VHCI_CLIENT_LOCK(vh);
MDI_CLIENT_LOCK(ct);
MDI_CLIENT_CLEAR_PATH_FREE_IN_PROGRESS(ct);
rv = MDI_SUCCESS;
if (!MDI_PI_IS_INITING(pip)) {
f = vh->vh_ops->vo_pi_uninit;
if (f != NULL) {
rv = (*f)(vh->vh_dip, pip, 0);
}
}
if (rv == MDI_SUCCESS) {
if (client_held) {
MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
"i_mdi_pm_rele_client\n"));
i_mdi_pm_rele_client(ct, 1);
}
i_mdi_pi_free(ph, pip, ct);
if (ct->ct_path_count == 0) {
MDI_CLIENT_UNLOCK(ct);
(void) i_mdi_client_free(ct->ct_vhci, ct);
MDI_VHCI_CLIENT_UNLOCK(vh);
return (rv);
}
}
MDI_CLIENT_UNLOCK(ct);
MDI_VHCI_CLIENT_UNLOCK(vh);
if (rv == MDI_FAILURE)
vhcache_pi_add(vh->vh_config, MDI_PI(pip));
return (rv);
}
static void
i_mdi_pi_free(mdi_phci_t *ph, mdi_pathinfo_t *pip, mdi_client_t *ct)
{
ASSERT(MDI_CLIENT_LOCKED(ct));
i_mdi_pi_kstat_destroy(pip);
ndi_devi_enter(ct->ct_dip);
ndi_devi_enter(ph->ph_dip);
i_mdi_client_remove_path(ct, pip);
i_mdi_phci_remove_path(ph, pip);
ndi_devi_exit(ph->ph_dip);
ndi_devi_exit(ct->ct_dip);
mutex_destroy(&MDI_PI(pip)->pi_mutex);
cv_destroy(&MDI_PI(pip)->pi_state_cv);
cv_destroy(&MDI_PI(pip)->pi_ref_cv);
if (MDI_PI(pip)->pi_addr) {
kmem_free(MDI_PI(pip)->pi_addr,
strlen(MDI_PI(pip)->pi_addr) + 1);
MDI_PI(pip)->pi_addr = NULL;
}
if (MDI_PI(pip)->pi_prop) {
(void) nvlist_free(MDI_PI(pip)->pi_prop);
MDI_PI(pip)->pi_prop = NULL;
}
kmem_free(pip, sizeof (struct mdi_pathinfo));
}
static void
i_mdi_phci_remove_path(mdi_phci_t *ph, mdi_pathinfo_t *pip)
{
mdi_pathinfo_t *prev = NULL;
mdi_pathinfo_t *path = NULL;
ASSERT(DEVI_BUSY_OWNED(ph->ph_dip));
MDI_PHCI_LOCK(ph);
path = ph->ph_path_head;
while (path != NULL) {
if (path == pip) {
break;
}
prev = path;
path = (mdi_pathinfo_t *)MDI_PI(path)->pi_phci_link;
}
if (path) {
ph->ph_path_count--;
if (prev) {
MDI_PI(prev)->pi_phci_link = MDI_PI(path)->pi_phci_link;
} else {
ph->ph_path_head =
(mdi_pathinfo_t *)MDI_PI(path)->pi_phci_link;
}
if (ph->ph_path_tail == path) {
ph->ph_path_tail = prev;
}
}
MDI_PI(pip)->pi_phci_link = NULL;
MDI_PI(pip)->pi_phci = NULL;
MDI_PHCI_UNLOCK(ph);
}
static void
i_mdi_client_remove_path(mdi_client_t *ct, mdi_pathinfo_t *pip)
{
mdi_pathinfo_t *prev = NULL;
mdi_pathinfo_t *path;
ASSERT(DEVI_BUSY_OWNED(ct->ct_dip));
ASSERT(MDI_CLIENT_LOCKED(ct));
path = ct->ct_path_head;
while (path != NULL) {
if (path == pip) {
break;
}
prev = path;
path = (mdi_pathinfo_t *)MDI_PI(path)->pi_client_link;
}
if (path) {
ct->ct_path_count--;
if (prev) {
MDI_PI(prev)->pi_client_link =
MDI_PI(path)->pi_client_link;
} else {
ct->ct_path_head =
(mdi_pathinfo_t *)MDI_PI(path)->pi_client_link;
}
if (ct->ct_path_tail == path) {
ct->ct_path_tail = prev;
}
if (ct->ct_path_last == path) {
ct->ct_path_last = ct->ct_path_head;
}
}
MDI_PI(pip)->pi_client_link = NULL;
MDI_PI(pip)->pi_client = NULL;
}
static int
i_mdi_pi_state_change(mdi_pathinfo_t *pip, mdi_pathinfo_state_t state, int flag)
{
int rv = MDI_SUCCESS;
mdi_vhci_t *vh;
mdi_phci_t *ph;
mdi_client_t *ct;
int (*f)();
dev_info_t *cdip;
MDI_PI_LOCK(pip);
ph = MDI_PI(pip)->pi_phci;
ASSERT(ph);
if (ph == NULL) {
MDI_PI_UNLOCK(pip);
MDI_DEBUG(1, (MDI_WARN, NULL,
"!invalid phci: pip %s %p",
mdi_pi_spathname(pip), (void *)pip));
return (MDI_FAILURE);
}
vh = ph->ph_vhci;
ASSERT(vh);
if (vh == NULL) {
MDI_PI_UNLOCK(pip);
MDI_DEBUG(1, (MDI_WARN, ph->ph_dip,
"!invalid vhci: pip %s %p",
mdi_pi_spathname(pip), (void *)pip));
return (MDI_FAILURE);
}
ct = MDI_PI(pip)->pi_client;
ASSERT(ct != NULL);
if (ct == NULL) {
MDI_PI_UNLOCK(pip);
MDI_DEBUG(1, (MDI_WARN, ph->ph_dip,
"!invalid client: pip %s %p",
mdi_pi_spathname(pip), (void *)pip));
return (MDI_FAILURE);
}
if (MDI_PI_IS_INITING(pip)) {
MDI_PI_UNLOCK(pip);
f = vh->vh_ops->vo_pi_init;
if (f != NULL) {
rv = (*f)(vh->vh_dip, pip, 0);
if (rv != MDI_SUCCESS) {
MDI_DEBUG(1, (MDI_WARN, ct->ct_dip,
"!vo_pi_init failed: vHCI %p, pip %s %p",
(void *)vh, mdi_pi_spathname(pip),
(void *)pip));
return (MDI_FAILURE);
}
}
MDI_PI_LOCK(pip);
MDI_PI_CLEAR_TRANSIENT(pip);
}
i_mdi_phci_lock(ph, pip);
if (MDI_PHCI_IS_READY(ph) == 0) {
MDI_DEBUG(1, (MDI_WARN, ct->ct_dip,
"!pHCI not ready, pHCI=%p", (void *)ph));
MDI_PI_UNLOCK(pip);
i_mdi_phci_unlock(ph);
return (MDI_BUSY);
}
MDI_PHCI_UNSTABLE(ph);
i_mdi_phci_unlock(ph);
if (MDI_PI_IS_TRANSIENT(pip)) {
while (MDI_PI_IS_TRANSIENT(pip)) {
cv_wait(&MDI_PI(pip)->pi_state_cv,
&MDI_PI(pip)->pi_mutex);
}
}
i_mdi_client_lock(ct, pip);
MDI_PI_UNLOCK(pip);
while (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct))
cv_wait(&ct->ct_failover_cv, &ct->ct_mutex);
MDI_PI_LOCK(pip);
switch (state) {
case MDI_PATHINFO_STATE_ONLINE:
MDI_PI_SET_ONLINING(pip);
break;
case MDI_PATHINFO_STATE_STANDBY:
MDI_PI_SET_STANDBYING(pip);
break;
case MDI_PATHINFO_STATE_FAULT:
MDI_PI_SET_FAULTING(pip);
MDI_PI_ERRSTAT(pip, MDI_PI_HARDERR);
break;
case MDI_PATHINFO_STATE_OFFLINE:
MDI_PI_UNLOCK(pip);
cdip = ct->ct_dip;
if ((flag & NDI_USER_REQ) &&
MDI_PI_IS_ONLINE(pip) &&
(MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_DEGRADED)) {
i_mdi_client_unlock(ct);
rv = ndi_devi_offline(cdip, NDI_DEVFS_CLEAN);
if (rv != NDI_SUCCESS) {
switch (rv) {
case NDI_BUSY:
rv = MDI_BUSY;
break;
default:
rv = MDI_FAILURE;
break;
}
goto state_change_exit;
} else {
i_mdi_client_lock(ct, NULL);
}
}
MDI_PI_LOCK(pip);
MDI_PI_SET_OFFLINING(pip);
break;
case MDI_PATHINFO_STATE_INIT:
rv = MDI_FAILURE;
MDI_PI_UNLOCK(pip);
goto state_change_exit;
}
MDI_PI_UNLOCK(pip);
MDI_CLIENT_UNSTABLE(ct);
i_mdi_client_unlock(ct);
f = vh->vh_ops->vo_pi_state_change;
if (f != NULL)
rv = (*f)(vh->vh_dip, pip, state, 0, flag);
MDI_CLIENT_LOCK(ct);
MDI_PI_LOCK(pip);
if (rv == MDI_NOT_SUPPORTED) {
MDI_CLIENT_SET_DEV_NOT_SUPPORTED(ct);
}
if (rv != MDI_SUCCESS) {
MDI_DEBUG(2, (MDI_WARN, ct->ct_dip,
"vo_pi_state_change failed: rv %x", rv));
}
if (MDI_PI_IS_TRANSIENT(pip)) {
if (rv == MDI_SUCCESS) {
MDI_PI_CLEAR_TRANSIENT(pip);
} else {
MDI_PI(pip)->pi_state = MDI_PI_OLD_STATE(pip);
}
}
cv_broadcast(&MDI_PI(pip)->pi_state_cv);
MDI_PI_UNLOCK(pip);
MDI_CLIENT_STABLE(ct);
if (rv == MDI_SUCCESS) {
if (ct->ct_unstable == 0) {
cdip = ct->ct_dip;
rv = NDI_SUCCESS;
i_mdi_client_update_state(ct);
switch (MDI_CLIENT_STATE(ct)) {
case MDI_CLIENT_STATE_OPTIMAL:
case MDI_CLIENT_STATE_DEGRADED:
if (cdip && !i_ddi_devi_attached(cdip) &&
((state == MDI_PATHINFO_STATE_ONLINE) ||
(state == MDI_PATHINFO_STATE_STANDBY))) {
MDI_CLIENT_UNLOCK(ct);
rv = ndi_devi_online(cdip, 0);
MDI_CLIENT_LOCK(ct);
if ((rv != NDI_SUCCESS) &&
(MDI_CLIENT_STATE(ct) ==
MDI_CLIENT_STATE_DEGRADED)) {
MDI_DEBUG(1, (MDI_WARN, cdip,
"!ndi_devi_online failed "
"error %x", rv));
}
rv = NDI_SUCCESS;
}
break;
case MDI_CLIENT_STATE_FAILED:
if (((flag & NDI_USER_REQ) == 0) &&
cdip && (i_ddi_node_state(cdip) >=
DS_INITIALIZED)) {
MDI_CLIENT_UNLOCK(ct);
rv = ndi_devi_offline(cdip,
NDI_DEVFS_CLEAN);
MDI_CLIENT_LOCK(ct);
if (rv != NDI_SUCCESS) {
MDI_DEBUG(1, (MDI_WARN, cdip,
"!ndi_devi_offline failed: "
"error %x", rv));
MDI_CLIENT_SET_ONLINE(ct);
}
}
break;
}
switch (rv) {
case NDI_SUCCESS:
MDI_CLIENT_SET_REPORT_DEV_NEEDED(ct);
i_mdi_report_path_state(ct, pip);
rv = MDI_SUCCESS;
break;
case NDI_BUSY:
rv = MDI_BUSY;
break;
default:
rv = MDI_FAILURE;
break;
}
}
}
MDI_CLIENT_UNLOCK(ct);
state_change_exit:
MDI_PHCI_LOCK(ph);
MDI_PHCI_STABLE(ph);
MDI_PHCI_UNLOCK(ph);
return (rv);
}
int
mdi_pi_online(mdi_pathinfo_t *pip, int flags)
{
mdi_client_t *ct = MDI_PI(pip)->pi_client;
int client_held = 0;
int rv;
ASSERT(ct != NULL);
rv = i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_ONLINE, flags);
if (rv != MDI_SUCCESS)
return (rv);
MDI_PI_LOCK(pip);
if (MDI_PI(pip)->pi_pm_held == 0) {
MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
"i_mdi_pm_hold_pip %p", (void *)pip));
i_mdi_pm_hold_pip(pip);
client_held = 1;
}
MDI_PI_UNLOCK(pip);
if (client_held) {
MDI_CLIENT_LOCK(ct);
if (ct->ct_power_cnt == 0) {
rv = i_mdi_power_all_phci(ct);
}
MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
"i_mdi_pm_hold_client %p", (void *)ct));
i_mdi_pm_hold_client(ct, 1);
MDI_CLIENT_UNLOCK(ct);
}
return (rv);
}
int
mdi_pi_standby(mdi_pathinfo_t *pip, int flags)
{
return (i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_STANDBY, flags));
}
int
mdi_pi_fault(mdi_pathinfo_t *pip, int flags)
{
return (i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_FAULT, flags));
}
int
mdi_pi_offline(mdi_pathinfo_t *pip, int flags)
{
int ret, client_held = 0;
mdi_client_t *ct;
if (flags & NDI_DEVI_REMOVE) {
flags &= ~NDI_DEVI_REMOVE;
flags |= NDI_USER_REQ;
}
ret = i_mdi_pi_state_change(pip, MDI_PATHINFO_STATE_OFFLINE, flags);
if (ret == MDI_SUCCESS) {
MDI_PI_LOCK(pip);
if (MDI_PI(pip)->pi_pm_held) {
client_held = 1;
}
MDI_PI_UNLOCK(pip);
if (client_held) {
ct = MDI_PI(pip)->pi_client;
MDI_CLIENT_LOCK(ct);
MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
"i_mdi_pm_rele_client\n"));
i_mdi_pm_rele_client(ct, 1);
MDI_CLIENT_UNLOCK(ct);
}
}
return (ret);
}
static int
i_mdi_pi_offline(mdi_pathinfo_t *pip, int flags)
{
dev_info_t *vdip = NULL;
mdi_vhci_t *vh = NULL;
mdi_client_t *ct = NULL;
int (*f)();
int rv;
MDI_PI_LOCK(pip);
ct = MDI_PI(pip)->pi_client;
ASSERT(ct != NULL);
while (MDI_PI(pip)->pi_ref_cnt != 0) {
MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
"!%d cmds still pending on path %s %p",
MDI_PI(pip)->pi_ref_cnt, mdi_pi_spathname(pip),
(void *)pip));
if (cv_reltimedwait(&MDI_PI(pip)->pi_ref_cv,
&MDI_PI(pip)->pi_mutex, drv_usectohz(60 * 1000000),
TR_CLOCK_TICK) == -1) {
MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
"!Timeout reached on path %s %p without the cond",
mdi_pi_spathname(pip), (void *)pip));
MDI_DEBUG(1, (MDI_NOTE, ct->ct_dip,
"!%d cmds still pending on path %s %p",
MDI_PI(pip)->pi_ref_cnt,
mdi_pi_spathname(pip), (void *)pip));
}
}
vh = ct->ct_vhci;
vdip = vh->vh_dip;
ASSERT(vh->vh_ops);
f = vh->vh_ops->vo_pi_state_change;
rv = MDI_SUCCESS;
if (f != NULL) {
MDI_PI_UNLOCK(pip);
if ((rv = (*f)(vdip, pip, MDI_PATHINFO_STATE_OFFLINE, 0,
flags)) != MDI_SUCCESS) {
MDI_DEBUG(1, (MDI_WARN, ct->ct_dip,
"!vo_path_offline failed: vdip %s%d %p: path %s %p",
ddi_driver_name(vdip), ddi_get_instance(vdip),
(void *)vdip, mdi_pi_spathname(pip), (void *)pip));
}
MDI_PI_LOCK(pip);
}
MDI_PI_SET_OFFLINE(pip);
cv_broadcast(&MDI_PI(pip)->pi_state_cv);
MDI_PI_UNLOCK(pip);
MDI_CLIENT_LOCK(ct);
if (rv == MDI_SUCCESS) {
if (ct->ct_unstable == 0) {
dev_info_t *cdip = ct->ct_dip;
i_mdi_client_update_state(ct);
rv = NDI_SUCCESS;
if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_FAILED) {
if (cdip &&
(i_ddi_node_state(cdip) >=
DS_INITIALIZED)) {
MDI_CLIENT_UNLOCK(ct);
rv = ndi_devi_offline(cdip,
NDI_DEVFS_CLEAN);
MDI_CLIENT_LOCK(ct);
if (rv != NDI_SUCCESS) {
MDI_DEBUG(4, (MDI_WARN, cdip,
"ndi_devi_offline failed: "
"error %x", rv));
MDI_CLIENT_SET_ONLINE(ct);
}
}
}
switch (rv) {
case NDI_SUCCESS:
rv = MDI_SUCCESS;
break;
case NDI_BUSY:
rv = MDI_BUSY;
break;
default:
rv = MDI_FAILURE;
break;
}
}
MDI_CLIENT_SET_REPORT_DEV_NEEDED(ct);
i_mdi_report_path_state(ct, pip);
}
MDI_CLIENT_UNLOCK(ct);
MDI_DEBUG(2, (MDI_NOTE, ct->ct_dip,
"ct = %p pip = %p", (void *)ct, (void *)pip));
return (rv);
}
static int
i_mdi_pi_online(mdi_pathinfo_t *pip, int flags)
{
mdi_vhci_t *vh = NULL;
mdi_client_t *ct = NULL;
mdi_phci_t *ph;
int (*f)();
int rv;
MDI_PI_LOCK(pip);
ph = MDI_PI(pip)->pi_phci;
vh = ph->ph_vhci;
ct = MDI_PI(pip)->pi_client;
MDI_PI_SET_ONLINING(pip)
MDI_PI_UNLOCK(pip);
f = vh->vh_ops->vo_pi_state_change;
rv = MDI_SUCCESS;
if (f != NULL)
rv = (*f)(vh->vh_dip, pip, MDI_PATHINFO_STATE_ONLINE, 0, flags);
MDI_CLIENT_LOCK(ct);
MDI_PI_LOCK(pip);
cv_broadcast(&MDI_PI(pip)->pi_state_cv);
MDI_PI_UNLOCK(pip);
if (rv == MDI_SUCCESS) {
dev_info_t *cdip = ct->ct_dip;
i_mdi_client_update_state(ct);
if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_OPTIMAL ||
MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_DEGRADED) {
if (cdip && !i_ddi_devi_attached(cdip)) {
MDI_CLIENT_UNLOCK(ct);
rv = ndi_devi_online(cdip, 0);
MDI_CLIENT_LOCK(ct);
if ((rv != NDI_SUCCESS) &&
(MDI_CLIENT_STATE(ct) ==
MDI_CLIENT_STATE_DEGRADED)) {
MDI_CLIENT_SET_OFFLINE(ct);
}
if (rv != NDI_SUCCESS) {
MDI_PI_LOCK(pip);
MDI_PI(pip)->pi_state =
MDI_PI_OLD_STATE(pip);
MDI_PI_UNLOCK(pip);
}
}
}
switch (rv) {
case NDI_SUCCESS:
MDI_CLIENT_SET_REPORT_DEV_NEEDED(ct);
i_mdi_report_path_state(ct, pip);
rv = MDI_SUCCESS;
break;
case NDI_BUSY:
rv = MDI_BUSY;
break;
default:
rv = MDI_FAILURE;
break;
}
} else {
MDI_PI_LOCK(pip);
MDI_PI(pip)->pi_state = MDI_PI_OLD_STATE(pip);
MDI_PI_UNLOCK(pip);
}
MDI_CLIENT_UNLOCK(ct);
return (rv);
}
char *
mdi_pi_get_node_name(mdi_pathinfo_t *pip)
{
mdi_client_t *ct;
if (pip == NULL)
return (NULL);
ct = MDI_PI(pip)->pi_client;
if ((ct == NULL) || (ct->ct_dip == NULL))
return (NULL);
return (ddi_node_name(ct->ct_dip));
}
char *
mdi_pi_get_addr(mdi_pathinfo_t *pip)
{
if (pip == NULL)
return (NULL);
return (MDI_PI(pip)->pi_addr);
}
int
mdi_pi_get_path_instance(mdi_pathinfo_t *pip)
{
if (pip == NULL)
return (0);
return (MDI_PI(pip)->pi_path_instance);
}
char *
mdi_pi_pathname(mdi_pathinfo_t *pip)
{
if (pip == NULL)
return (NULL);
return (mdi_pi_pathname_by_instance(mdi_pi_get_path_instance(pip)));
}
char *
mdi_pi_spathname(mdi_pathinfo_t *pip)
{
char *spath = "";
if (pip) {
spath = mdi_pi_spathname_by_instance(
mdi_pi_get_path_instance(pip));
if (spath == NULL)
spath = "";
}
return (spath);
}
char *
mdi_pi_pathname_obp(mdi_pathinfo_t *pip, char *path)
{
char *obp_path = NULL;
if ((pip == NULL) || (path == NULL))
return (NULL);
if (mdi_prop_lookup_string(pip, "obp-path", &obp_path) == MDI_SUCCESS) {
(void) strcpy(path, obp_path);
(void) mdi_prop_free(obp_path);
} else {
path = NULL;
}
return (path);
}
int
mdi_pi_pathname_obp_set(mdi_pathinfo_t *pip, char *component)
{
dev_info_t *pdip;
char *obp_path = NULL;
int rc = MDI_FAILURE;
if (pip == NULL)
return (MDI_FAILURE);
pdip = mdi_pi_get_phci(pip);
if (pdip == NULL)
return (MDI_FAILURE);
obp_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
if (ddi_pathname_obp(pdip, obp_path) == NULL) {
(void) ddi_pathname(pdip, obp_path);
}
if (component) {
(void) strncat(obp_path, "/", MAXPATHLEN);
(void) strncat(obp_path, component, MAXPATHLEN);
}
rc = mdi_prop_update_string(pip, "obp-path", obp_path);
if (obp_path)
kmem_free(obp_path, MAXPATHLEN);
return (rc);
}
dev_info_t *
mdi_pi_get_client(mdi_pathinfo_t *pip)
{
dev_info_t *dip = NULL;
if (pip) {
dip = MDI_PI(pip)->pi_client->ct_dip;
}
return (dip);
}
dev_info_t *
mdi_pi_get_phci(mdi_pathinfo_t *pip)
{
dev_info_t *dip = NULL;
mdi_phci_t *ph;
if (pip) {
ph = MDI_PI(pip)->pi_phci;
if (ph)
dip = ph->ph_dip;
}
return (dip);
}
void *
mdi_pi_get_client_private(mdi_pathinfo_t *pip)
{
void *cprivate = NULL;
if (pip) {
cprivate = MDI_PI(pip)->pi_cprivate;
}
return (cprivate);
}
void
mdi_pi_set_client_private(mdi_pathinfo_t *pip, void *priv)
{
if (pip) {
MDI_PI(pip)->pi_cprivate = priv;
}
}
caddr_t
mdi_pi_get_phci_private(mdi_pathinfo_t *pip)
{
caddr_t pprivate = NULL;
if (pip) {
pprivate = MDI_PI(pip)->pi_pprivate;
}
return (pprivate);
}
void
mdi_pi_set_phci_private(mdi_pathinfo_t *pip, caddr_t priv)
{
if (pip) {
MDI_PI(pip)->pi_pprivate = priv;
}
}
mdi_pathinfo_state_t
mdi_pi_get_state(mdi_pathinfo_t *pip)
{
mdi_pathinfo_state_t state = MDI_PATHINFO_STATE_INIT;
if (pip) {
if (MDI_PI_IS_TRANSIENT(pip)) {
state = MDI_PI_OLD_STATE(pip);
} else {
state = MDI_PI_STATE(pip);
}
}
return (state);
}
uint_t
mdi_pi_get_flags(mdi_pathinfo_t *pip)
{
return (pip ? MDI_PI(pip)->pi_flags : 0);
}
int
mdi_pi_get_state2(mdi_pathinfo_t *pip, mdi_pathinfo_state_t *state,
uint32_t *ext_state)
{
*state = MDI_PATHINFO_STATE_INIT;
if (pip) {
if (MDI_PI_IS_TRANSIENT(pip)) {
*state = MDI_PI_OLD_STATE(pip);
*ext_state = MDI_PI_OLD_EXT_STATE(pip);
} else {
*state = MDI_PI_STATE(pip);
*ext_state = MDI_PI_EXT_STATE(pip);
}
}
return (MDI_SUCCESS);
}
int
mdi_pi_get_preferred(mdi_pathinfo_t *pip)
{
if (pip) {
return (MDI_PI(pip)->pi_preferred);
}
return (0);
}
void
mdi_pi_set_preferred(mdi_pathinfo_t *pip, int preferred)
{
if (pip) {
MDI_PI(pip)->pi_preferred = preferred;
}
}
void
mdi_pi_set_state(mdi_pathinfo_t *pip, mdi_pathinfo_state_t state)
{
uint32_t ext_state;
if (pip) {
ext_state = MDI_PI(pip)->pi_state & MDI_PATHINFO_EXT_STATE_MASK;
MDI_PI(pip)->pi_state = state;
MDI_PI(pip)->pi_state |= ext_state;
i_ddi_di_cache_invalidate();
}
}
int
i_map_nvlist_error_to_mdi(int val)
{
int rv;
switch (val) {
case 0:
rv = DDI_PROP_SUCCESS;
break;
case EINVAL:
case ENOTSUP:
rv = DDI_PROP_INVAL_ARG;
break;
case ENOMEM:
rv = DDI_PROP_NO_MEMORY;
break;
default:
rv = DDI_PROP_NOT_FOUND;
break;
}
return (rv);
}
nvpair_t *
mdi_pi_get_next_prop(mdi_pathinfo_t *pip, nvpair_t *prev)
{
if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
return (NULL);
}
ASSERT(MDI_PI_LOCKED(pip));
return (nvlist_next_nvpair(MDI_PI(pip)->pi_prop, prev));
}
int
mdi_prop_remove(mdi_pathinfo_t *pip, char *name)
{
if (pip == NULL) {
return (DDI_PROP_NOT_FOUND);
}
ASSERT(!MDI_PI_LOCKED(pip));
MDI_PI_LOCK(pip);
if (MDI_PI(pip)->pi_prop == NULL) {
MDI_PI_UNLOCK(pip);
return (DDI_PROP_NOT_FOUND);
}
if (name) {
(void) nvlist_remove_all(MDI_PI(pip)->pi_prop, name);
} else {
char nvp_name[MAXNAMELEN];
nvpair_t *nvp;
nvp = nvlist_next_nvpair(MDI_PI(pip)->pi_prop, NULL);
while (nvp) {
nvpair_t *next;
next = nvlist_next_nvpair(MDI_PI(pip)->pi_prop, nvp);
(void) snprintf(nvp_name, sizeof(nvp_name), "%s",
nvpair_name(nvp));
(void) nvlist_remove_all(MDI_PI(pip)->pi_prop,
nvp_name);
nvp = next;
}
}
MDI_PI_UNLOCK(pip);
return (DDI_PROP_SUCCESS);
}
int
mdi_prop_size(mdi_pathinfo_t *pip, size_t *buflenp)
{
int rv;
size_t bufsize;
*buflenp = 0;
if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
return (DDI_PROP_NOT_FOUND);
}
ASSERT(MDI_PI_LOCKED(pip));
rv = nvlist_size(MDI_PI(pip)->pi_prop,
&bufsize, NV_ENCODE_NATIVE);
*buflenp = bufsize;
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_pack(mdi_pathinfo_t *pip, char **bufp, uint_t buflen)
{
int rv;
size_t bufsize;
if ((pip == NULL) || MDI_PI(pip)->pi_prop == NULL) {
return (DDI_PROP_NOT_FOUND);
}
ASSERT(MDI_PI_LOCKED(pip));
bufsize = buflen;
rv = nvlist_pack(MDI_PI(pip)->pi_prop, bufp, (size_t *)&bufsize,
NV_ENCODE_NATIVE, KM_SLEEP);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_update_byte(mdi_pathinfo_t *pip, char *name, uchar_t data)
{
int rv;
if (pip == NULL) {
return (DDI_PROP_INVAL_ARG);
}
ASSERT(!MDI_PI_LOCKED(pip));
MDI_PI_LOCK(pip);
if (MDI_PI(pip)->pi_prop == NULL) {
MDI_PI_UNLOCK(pip);
return (DDI_PROP_NOT_FOUND);
}
rv = nvlist_add_byte(MDI_PI(pip)->pi_prop, name, data);
MDI_PI_UNLOCK(pip);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_update_byte_array(mdi_pathinfo_t *pip, char *name, uchar_t *data,
uint_t nelements)
{
int rv;
if (pip == NULL) {
return (DDI_PROP_INVAL_ARG);
}
ASSERT(!MDI_PI_LOCKED(pip));
MDI_PI_LOCK(pip);
if (MDI_PI(pip)->pi_prop == NULL) {
MDI_PI_UNLOCK(pip);
return (DDI_PROP_NOT_FOUND);
}
rv = nvlist_add_byte_array(MDI_PI(pip)->pi_prop, name, data, nelements);
MDI_PI_UNLOCK(pip);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_update_int(mdi_pathinfo_t *pip, char *name, int data)
{
int rv;
if (pip == NULL) {
return (DDI_PROP_INVAL_ARG);
}
ASSERT(!MDI_PI_LOCKED(pip));
MDI_PI_LOCK(pip);
if (MDI_PI(pip)->pi_prop == NULL) {
MDI_PI_UNLOCK(pip);
return (DDI_PROP_NOT_FOUND);
}
rv = nvlist_add_int32(MDI_PI(pip)->pi_prop, name, (int32_t)data);
MDI_PI_UNLOCK(pip);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_update_int64(mdi_pathinfo_t *pip, char *name, int64_t data)
{
int rv;
if (pip == NULL) {
return (DDI_PROP_INVAL_ARG);
}
ASSERT(!MDI_PI_LOCKED(pip));
MDI_PI_LOCK(pip);
if (MDI_PI(pip)->pi_prop == NULL) {
MDI_PI_UNLOCK(pip);
return (DDI_PROP_NOT_FOUND);
}
rv = nvlist_add_int64(MDI_PI(pip)->pi_prop, name, data);
MDI_PI_UNLOCK(pip);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_update_int_array(mdi_pathinfo_t *pip, char *name, int *data,
uint_t nelements)
{
int rv;
if (pip == NULL) {
return (DDI_PROP_INVAL_ARG);
}
ASSERT(!MDI_PI_LOCKED(pip));
MDI_PI_LOCK(pip);
if (MDI_PI(pip)->pi_prop == NULL) {
MDI_PI_UNLOCK(pip);
return (DDI_PROP_NOT_FOUND);
}
rv = nvlist_add_int32_array(MDI_PI(pip)->pi_prop, name, (int32_t *)data,
nelements);
MDI_PI_UNLOCK(pip);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_update_string(mdi_pathinfo_t *pip, char *name, char *data)
{
int rv;
if (pip == NULL) {
return (DDI_PROP_INVAL_ARG);
}
ASSERT(!MDI_PI_LOCKED(pip));
MDI_PI_LOCK(pip);
if (MDI_PI(pip)->pi_prop == NULL) {
MDI_PI_UNLOCK(pip);
return (DDI_PROP_NOT_FOUND);
}
rv = nvlist_add_string(MDI_PI(pip)->pi_prop, name, data);
MDI_PI_UNLOCK(pip);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_update_string_array(mdi_pathinfo_t *pip, char *name, char **data,
uint_t nelements)
{
int rv;
if (pip == NULL) {
return (DDI_PROP_INVAL_ARG);
}
ASSERT(!MDI_PI_LOCKED(pip));
MDI_PI_LOCK(pip);
if (MDI_PI(pip)->pi_prop == NULL) {
MDI_PI_UNLOCK(pip);
return (DDI_PROP_NOT_FOUND);
}
rv = nvlist_add_string_array(MDI_PI(pip)->pi_prop, name, data,
nelements);
MDI_PI_UNLOCK(pip);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_lookup_byte(mdi_pathinfo_t *pip, char *name, uchar_t *data)
{
int rv;
if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
return (DDI_PROP_NOT_FOUND);
}
rv = nvlist_lookup_byte(MDI_PI(pip)->pi_prop, name, data);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_lookup_byte_array(mdi_pathinfo_t *pip, char *name, uchar_t **data,
uint_t *nelements)
{
int rv;
if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
return (DDI_PROP_NOT_FOUND);
}
rv = nvlist_lookup_byte_array(MDI_PI(pip)->pi_prop, name, data,
nelements);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_lookup_int(mdi_pathinfo_t *pip, char *name, int *data)
{
int rv;
if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
return (DDI_PROP_NOT_FOUND);
}
rv = nvlist_lookup_int32(MDI_PI(pip)->pi_prop, name, (int32_t *)data);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_lookup_int64(mdi_pathinfo_t *pip, char *name, int64_t *data)
{
int rv;
if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
return (DDI_PROP_NOT_FOUND);
}
rv = nvlist_lookup_int64(MDI_PI(pip)->pi_prop, name, data);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_lookup_int_array(mdi_pathinfo_t *pip, char *name, int **data,
uint_t *nelements)
{
int rv;
if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
return (DDI_PROP_NOT_FOUND);
}
rv = nvlist_lookup_int32_array(MDI_PI(pip)->pi_prop, name,
(int32_t **)data, nelements);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_lookup_string(mdi_pathinfo_t *pip, char *name, char **data)
{
int rv;
if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
return (DDI_PROP_NOT_FOUND);
}
rv = nvlist_lookup_string(MDI_PI(pip)->pi_prop, name, data);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_lookup_string_array(mdi_pathinfo_t *pip, char *name, char ***data,
uint_t *nelements)
{
int rv;
if ((pip == NULL) || (MDI_PI(pip)->pi_prop == NULL)) {
return (DDI_PROP_NOT_FOUND);
}
rv = nvlist_lookup_string_array(MDI_PI(pip)->pi_prop, name, data,
nelements);
return (i_map_nvlist_error_to_mdi(rv));
}
int
mdi_prop_free(void *data)
{
return (DDI_PROP_SUCCESS);
}
static void
i_mdi_report_path_state(mdi_client_t *ct, mdi_pathinfo_t *pip)
{
char *ct_path;
char *ct_status;
char *status;
dev_info_t *cdip = ct->ct_dip;
char lb_buf[64];
int report_lb_c = 0, report_lb_p = 0;
ASSERT(MDI_CLIENT_LOCKED(ct));
if ((cdip == NULL) || (ddi_get_instance(cdip) == -1) ||
(MDI_CLIENT_IS_REPORT_DEV_NEEDED(ct) == 0)) {
return;
}
if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_OPTIMAL) {
ct_status = "optimal";
report_lb_c = 1;
} else if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_DEGRADED) {
ct_status = "degraded";
} else if (MDI_CLIENT_STATE(ct) == MDI_CLIENT_STATE_FAILED) {
ct_status = "failed";
} else {
ct_status = "unknown";
}
lb_buf[0] = 0;
if (MDI_PI_FLAGS_IS_DEVICE_REMOVED(pip)) {
status = "removed";
} else if (MDI_PI_IS_OFFLINE(pip)) {
status = "offline";
} else if (MDI_PI_IS_ONLINE(pip)) {
status = "online";
report_lb_p = 1;
} else if (MDI_PI_IS_STANDBY(pip)) {
status = "standby";
} else if (MDI_PI_IS_FAULT(pip)) {
status = "faulted";
} else {
status = "unknown";
}
if (cdip) {
ct_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
if (report_lb_c && report_lb_p) {
if (ct->ct_lb == LOAD_BALANCE_LBA) {
(void) snprintf(lb_buf, sizeof (lb_buf),
"%s, region-size: %d", mdi_load_balance_lba,
ct->ct_lb_args->region_size);
} else if (ct->ct_lb == LOAD_BALANCE_NONE) {
(void) snprintf(lb_buf, sizeof (lb_buf),
"%s", mdi_load_balance_none);
} else {
(void) snprintf(lb_buf, sizeof (lb_buf), "%s",
mdi_load_balance_rr);
}
cmn_err(mdi_debug_consoleonly ? CE_NOTE : CE_CONT,
"?%s (%s%d) multipath status: %s: "
"path %d %s is %s: Load balancing: %s\n",
ddi_pathname(cdip, ct_path), ddi_driver_name(cdip),
ddi_get_instance(cdip), ct_status,
mdi_pi_get_path_instance(pip),
mdi_pi_spathname(pip), status, lb_buf);
} else {
cmn_err(mdi_debug_consoleonly ? CE_NOTE : CE_CONT,
"?%s (%s%d) multipath status: %s: "
"path %d %s is %s\n",
ddi_pathname(cdip, ct_path), ddi_driver_name(cdip),
ddi_get_instance(cdip), ct_status,
mdi_pi_get_path_instance(pip),
mdi_pi_spathname(pip), status);
}
kmem_free(ct_path, MAXPATHLEN);
MDI_CLIENT_CLEAR_REPORT_DEV_NEEDED(ct);
}
}
#ifdef DEBUG
static void
i_mdi_log(int level, const char *func, dev_info_t *dip, const char *fmt, ...)
{
char name[MAXNAMELEN];
char buf[512];
char *bp;
va_list ap;
int log_only = 0;
int boot_only = 0;
int console_only = 0;
if (dip) {
(void) snprintf(name, sizeof(name), "%s%d: ",
ddi_driver_name(dip), ddi_get_instance(dip));
} else {
name[0] = 0;
}
va_start(ap, fmt);
(void) vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
switch (buf[0]) {
case '!':
bp = &buf[1];
log_only = 1;
break;
case '?':
bp = &buf[1];
boot_only = 1;
break;
case '^':
bp = &buf[1];
console_only = 1;
break;
default:
if (level >= 2)
log_only = 1;
bp = buf;
break;
}
if (mdi_debug_logonly) {
log_only = 1;
boot_only = 0;
console_only = 0;
}
if (mdi_debug_consoleonly) {
log_only = 0;
boot_only = 0;
console_only = 1;
level = CE_NOTE;
goto console;
}
switch (level) {
case CE_NOTE:
level = CE_CONT;
case CE_CONT:
if (boot_only) {
cmn_err(level, "?mdi: %s%s: %s\n", name, func, bp);
} else if (console_only) {
cmn_err(level, "^mdi: %s%s: %s\n", name, func, bp);
} else if (log_only) {
cmn_err(level, "!mdi: %s%s: %s\n", name, func, bp);
} else {
cmn_err(level, "mdi: %s%s: %s\n", name, func, bp);
}
break;
case CE_WARN:
case CE_PANIC:
console:
if (boot_only) {
cmn_err(level, "?mdi: %s%s: %s", name, func, bp);
} else if (console_only) {
cmn_err(level, "^mdi: %s%s: %s", name, func, bp);
} else if (log_only) {
cmn_err(level, "!mdi: %s%s: %s", name, func, bp);
} else {
cmn_err(level, "mdi: %s%s: %s", name, func, bp);
}
break;
default:
cmn_err(level, "mdi: %s%s", name, bp);
break;
}
}
#endif
void
i_mdi_client_online(dev_info_t *ct_dip)
{
mdi_client_t *ct;
ct = i_devi_get_client(ct_dip);
ASSERT(ct != NULL);
MDI_CLIENT_LOCK(ct);
MDI_CLIENT_SET_ONLINE(ct);
ASSERT((ct->ct_dip == NULL) || (ct->ct_dip == ct_dip));
ct->ct_dip = ct_dip;
if (ct->ct_power_cnt == 0)
(void) i_mdi_power_all_phci(ct);
MDI_DEBUG(4, (MDI_NOTE, ct_dip,
"i_mdi_pm_hold_client %p", (void *)ct));
i_mdi_pm_hold_client(ct, 1);
MDI_CLIENT_UNLOCK(ct);
}
void
i_mdi_phci_online(dev_info_t *ph_dip)
{
mdi_phci_t *ph;
ph = i_devi_get_phci(ph_dip);
ASSERT(ph != NULL);
MDI_PHCI_LOCK(ph);
MDI_PHCI_SET_ONLINE(ph);
MDI_PHCI_UNLOCK(ph);
}
int
mdi_devi_online(dev_info_t *dip, uint_t flags)
{
if (MDI_PHCI(dip)) {
i_mdi_phci_online(dip);
}
if (MDI_CLIENT(dip)) {
i_mdi_client_online(dip);
}
return (NDI_SUCCESS);
}
int
mdi_devi_offline(dev_info_t *dip, uint_t flags)
{
int rv = NDI_SUCCESS;
if (MDI_CLIENT(dip)) {
rv = i_mdi_client_offline(dip, flags);
if (rv != NDI_SUCCESS)
return (rv);
}
if (MDI_PHCI(dip)) {
rv = i_mdi_phci_offline(dip, flags);
if ((rv != NDI_SUCCESS) && MDI_CLIENT(dip)) {
i_mdi_client_online(dip);
}
}
return (rv);
}
static int
i_mdi_phci_offline(dev_info_t *dip, uint_t flags)
{
int rv = NDI_SUCCESS;
mdi_phci_t *ph;
mdi_client_t *ct;
mdi_pathinfo_t *pip;
mdi_pathinfo_t *next;
mdi_pathinfo_t *failed_pip = NULL;
dev_info_t *cdip;
ph = i_devi_get_phci(dip);
MDI_DEBUG(2, (MDI_NOTE, dip,
"called %p %p", (void *)dip, (void *)ph));
if (ph == NULL) {
return (rv);
}
MDI_PHCI_LOCK(ph);
if (MDI_PHCI_IS_OFFLINE(ph)) {
MDI_DEBUG(1, (MDI_WARN, dip,
"!pHCI already offlined: %p", (void *)dip));
MDI_PHCI_UNLOCK(ph);
return (NDI_SUCCESS);
}
if (ph->ph_unstable) {
MDI_DEBUG(1, (MDI_WARN, dip,
"!One or more target devices are in transient state. "
"This device can not be removed at this moment. "
"Please try again later."));
MDI_PHCI_UNLOCK(ph);
return (NDI_BUSY);
}
pip = ph->ph_path_head;
while (pip != NULL) {
MDI_PI_LOCK(pip);
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
ct = MDI_PI(pip)->pi_client;
i_mdi_client_lock(ct, pip);
if ((MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) ||
(ct->ct_unstable)) {
MDI_DEBUG(1, (MDI_WARN, dip,
"!pHCI device is busy. "
"This device can not be removed at this moment. "
"Please try again later."));
MDI_PI_UNLOCK(pip);
i_mdi_client_unlock(ct);
MDI_PHCI_UNLOCK(ph);
return (NDI_BUSY);
}
MDI_PI_UNLOCK(pip);
cdip = ct->ct_dip;
if (cdip && (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
(i_mdi_client_compute_state(ct, ph) ==
MDI_CLIENT_STATE_FAILED)) {
i_mdi_client_unlock(ct);
MDI_PHCI_UNLOCK(ph);
if (ndi_devi_offline(cdip,
NDI_DEVFS_CLEAN) != NDI_SUCCESS) {
MDI_PHCI_LOCK(ph);
MDI_DEBUG(1, (MDI_WARN, dip,
"!pHCI device is busy. "
"This device can not be removed at this "
"moment. Please try again later."));
failed_pip = pip;
break;
} else {
MDI_PHCI_LOCK(ph);
pip = next;
}
} else {
i_mdi_client_unlock(ct);
pip = next;
}
}
if (failed_pip) {
pip = ph->ph_path_head;
while (pip != failed_pip) {
MDI_PI_LOCK(pip);
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
ct = MDI_PI(pip)->pi_client;
i_mdi_client_lock(ct, pip);
cdip = ct->ct_dip;
switch (MDI_CLIENT_STATE(ct)) {
case MDI_CLIENT_STATE_OPTIMAL:
case MDI_CLIENT_STATE_DEGRADED:
if (cdip) {
MDI_PI_UNLOCK(pip);
i_mdi_client_unlock(ct);
MDI_PHCI_UNLOCK(ph);
(void) ndi_devi_online(cdip, 0);
MDI_PHCI_LOCK(ph);
pip = next;
continue;
}
break;
case MDI_CLIENT_STATE_FAILED:
if (cdip) {
MDI_PI_UNLOCK(pip);
i_mdi_client_unlock(ct);
MDI_PHCI_UNLOCK(ph);
(void) ndi_devi_offline(cdip,
NDI_DEVFS_CLEAN);
MDI_PHCI_LOCK(ph);
pip = next;
continue;
}
break;
}
MDI_PI_UNLOCK(pip);
i_mdi_client_unlock(ct);
pip = next;
}
MDI_PHCI_UNLOCK(ph);
return (NDI_BUSY);
}
MDI_PHCI_SET_OFFLINE(ph);
pip = ph->ph_path_head;
while (pip != NULL) {
MDI_PI_LOCK(pip);
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
MDI_PI_SET_OFFLINING(pip);
MDI_PI_UNLOCK(pip);
pip = next;
}
MDI_PHCI_UNLOCK(ph);
delay_random(mdi_delay);
MDI_PHCI_LOCK(ph);
pip = ph->ph_path_head;
while (pip != NULL) {
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
(void) i_mdi_pi_offline(pip, flags);
MDI_PI_LOCK(pip);
ct = MDI_PI(pip)->pi_client;
if (!MDI_PI_IS_OFFLINE(pip)) {
MDI_DEBUG(1, (MDI_WARN, dip,
"!pHCI device is busy. "
"This device can not be removed at this moment. "
"Please try again later."));
MDI_PI_UNLOCK(pip);
MDI_PHCI_SET_ONLINE(ph);
MDI_PHCI_UNLOCK(ph);
return (NDI_BUSY);
}
MDI_PI_UNLOCK(pip);
pip = next;
}
MDI_PHCI_UNLOCK(ph);
return (rv);
}
void
mdi_phci_mark_retiring(dev_info_t *dip, char **cons_array)
{
mdi_phci_t *ph;
mdi_client_t *ct;
mdi_pathinfo_t *pip;
mdi_pathinfo_t *next;
dev_info_t *cdip;
if (!MDI_PHCI(dip))
return;
ph = i_devi_get_phci(dip);
if (ph == NULL) {
return;
}
MDI_PHCI_LOCK(ph);
if (MDI_PHCI_IS_OFFLINE(ph)) {
MDI_PHCI_UNLOCK(ph);
return;
}
pip = ph->ph_path_head;
while (pip != NULL) {
MDI_PI_LOCK(pip);
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
ct = MDI_PI(pip)->pi_client;
i_mdi_client_lock(ct, pip);
MDI_PI_UNLOCK(pip);
cdip = ct->ct_dip;
if (cdip && (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
(i_mdi_client_compute_state(ct, ph) ==
MDI_CLIENT_STATE_FAILED)) {
i_mdi_client_unlock(ct);
MDI_PHCI_UNLOCK(ph);
(void) e_ddi_mark_retiring(cdip, cons_array);
MDI_PHCI_LOCK(ph);
pip = next;
} else {
i_mdi_client_unlock(ct);
pip = next;
}
}
MDI_PHCI_UNLOCK(ph);
return;
}
void
mdi_phci_retire_notify(dev_info_t *dip, int *constraint)
{
mdi_phci_t *ph;
mdi_client_t *ct;
mdi_pathinfo_t *pip;
mdi_pathinfo_t *next;
dev_info_t *cdip;
if (!MDI_PHCI(dip))
return;
ph = i_devi_get_phci(dip);
if (ph == NULL)
return;
MDI_PHCI_LOCK(ph);
if (MDI_PHCI_IS_OFFLINE(ph)) {
MDI_PHCI_UNLOCK(ph);
return;
}
if (ph->ph_unstable) {
MDI_PHCI_UNLOCK(ph);
*constraint = 0;
return;
}
pip = ph->ph_path_head;
while (pip != NULL) {
MDI_PI_LOCK(pip);
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
ct = MDI_PI(pip)->pi_client;
i_mdi_client_lock(ct, pip);
if ((MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) ||
(ct->ct_unstable)) {
MDI_PI_UNLOCK(pip);
i_mdi_client_unlock(ct);
MDI_PHCI_UNLOCK(ph);
*constraint = 0;
return;
}
MDI_PI_UNLOCK(pip);
cdip = ct->ct_dip;
if (cdip && (i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
(i_mdi_client_compute_state(ct, ph) ==
MDI_CLIENT_STATE_FAILED)) {
i_mdi_client_unlock(ct);
MDI_PHCI_UNLOCK(ph);
(void) e_ddi_retire_notify(cdip, constraint);
MDI_PHCI_LOCK(ph);
pip = next;
} else {
i_mdi_client_unlock(ct);
pip = next;
}
}
MDI_PHCI_UNLOCK(ph);
return;
}
void
mdi_phci_retire_finalize(dev_info_t *dip, int phci_only, void *constraint)
{
mdi_phci_t *ph;
mdi_client_t *ct;
mdi_pathinfo_t *pip;
mdi_pathinfo_t *next;
dev_info_t *cdip;
int unstable = 0;
int tmp_constraint;
if (!MDI_PHCI(dip))
return;
ph = i_devi_get_phci(dip);
if (ph == NULL) {
return;
}
MDI_PHCI_LOCK(ph);
if (MDI_PHCI_IS_OFFLINE(ph)) {
MDI_PHCI_UNLOCK(ph);
return;
}
if (ph->ph_unstable) {
unstable = 1;
}
pip = ph->ph_path_head;
while (pip != NULL) {
MDI_PI_LOCK(pip);
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
ct = MDI_PI(pip)->pi_client;
i_mdi_client_lock(ct, pip);
if ((MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) ||
(ct->ct_unstable)) {
unstable = 1;
}
MDI_PI_UNLOCK(pip);
cdip = ct->ct_dip;
if (!phci_only && cdip &&
(i_ddi_node_state(cdip) >= DS_INITIALIZED) &&
(i_mdi_client_compute_state(ct, ph) ==
MDI_CLIENT_STATE_FAILED)) {
i_mdi_client_unlock(ct);
MDI_PHCI_UNLOCK(ph);
tmp_constraint = 0;
(void) e_ddi_retire_finalize(cdip, &tmp_constraint);
MDI_PHCI_LOCK(ph);
pip = next;
} else {
i_mdi_client_unlock(ct);
pip = next;
}
}
if (!phci_only && *((int *)constraint) == 0) {
MDI_PHCI_UNLOCK(ph);
return;
}
if (unstable) {
cmn_err(CE_WARN, "%s%d: mdi_phci_retire_finalize: "
"pHCI in transient state, cannot retire",
ddi_driver_name(dip), ddi_get_instance(dip));
MDI_PHCI_UNLOCK(ph);
return;
}
MDI_PHCI_SET_OFFLINE(ph);
pip = ph->ph_path_head;
while (pip != NULL) {
MDI_PI_LOCK(pip);
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
MDI_PI_SET_OFFLINING(pip);
MDI_PI_UNLOCK(pip);
pip = next;
}
MDI_PHCI_UNLOCK(ph);
delay_random(mdi_delay);
MDI_PHCI_LOCK(ph);
pip = ph->ph_path_head;
while (pip != NULL) {
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
(void) i_mdi_pi_offline(pip, 0);
MDI_PI_LOCK(pip);
ct = MDI_PI(pip)->pi_client;
if (!MDI_PI_IS_OFFLINE(pip)) {
cmn_err(CE_WARN, "mdi_phci_retire_finalize: "
"path %d %s busy, cannot offline",
mdi_pi_get_path_instance(pip),
mdi_pi_spathname(pip));
MDI_PI_UNLOCK(pip);
MDI_PHCI_SET_ONLINE(ph);
MDI_PHCI_UNLOCK(ph);
return;
}
MDI_PI_UNLOCK(pip);
pip = next;
}
MDI_PHCI_UNLOCK(ph);
return;
}
void
mdi_phci_unretire(dev_info_t *dip)
{
mdi_phci_t *ph;
mdi_pathinfo_t *pip;
mdi_pathinfo_t *next;
ASSERT(MDI_PHCI(dip));
i_mdi_phci_online(dip);
ph = i_devi_get_phci(dip);
MDI_PHCI_LOCK(ph);
pip = ph->ph_path_head;
while (pip != NULL) {
MDI_PI_LOCK(pip);
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
MDI_PI_UNLOCK(pip);
(void) i_mdi_pi_online(pip, 0);
pip = next;
}
MDI_PHCI_UNLOCK(ph);
}
static int
i_mdi_client_offline(dev_info_t *dip, uint_t flags)
{
int rv = NDI_SUCCESS;
mdi_client_t *ct;
ct = i_devi_get_client(dip);
MDI_DEBUG(2, (MDI_NOTE, dip,
"called %p %p", (void *)dip, (void *)ct));
if (ct != NULL) {
MDI_CLIENT_LOCK(ct);
if (ct->ct_unstable) {
MDI_DEBUG(1, (MDI_WARN, dip,
"!One or more paths to "
"this device are in transient state. "
"This device can not be removed at this moment. "
"Please try again later."));
MDI_CLIENT_UNLOCK(ct);
return (NDI_BUSY);
}
if (MDI_CLIENT_IS_FAILOVER_IN_PROGRESS(ct)) {
MDI_DEBUG(1, (MDI_WARN, dip,
"!Client device is Busy. "
"This device can not be removed at this moment. "
"Please try again later."));
MDI_CLIENT_UNLOCK(ct);
return (NDI_BUSY);
}
MDI_CLIENT_SET_OFFLINE(ct);
if (flags & NDI_DEVI_REMOVE) {
ct->ct_dip = NULL;
}
MDI_CLIENT_UNLOCK(ct);
}
return (rv);
}
int
mdi_pre_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
if ((DEVI(dip)->devi_mdi_component != MDI_COMPONENT_NONE) &&
(cmd == DDI_PM_RESUME))
return (DDI_FAILURE);
return (DDI_SUCCESS);
}
void
mdi_post_attach(dev_info_t *dip, ddi_attach_cmd_t cmd, int error)
{
mdi_phci_t *ph;
mdi_client_t *ct;
mdi_vhci_t *vh;
if (MDI_PHCI(dip)) {
ph = i_devi_get_phci(dip);
ASSERT(ph != NULL);
MDI_PHCI_LOCK(ph);
switch (cmd) {
case DDI_ATTACH:
MDI_DEBUG(2, (MDI_NOTE, dip,
"phci post_attach called %p", (void *)ph));
if (error == DDI_SUCCESS) {
MDI_PHCI_SET_ATTACH(ph);
} else {
MDI_DEBUG(1, (MDI_NOTE, dip,
"!pHCI post_attach failed: error %d",
error));
MDI_PHCI_SET_DETACH(ph);
}
break;
case DDI_RESUME:
case DDI_PM_RESUME:
MDI_DEBUG(2, (MDI_NOTE, dip,
"pHCI post_resume: called %p", (void *)ph));
if (error == DDI_SUCCESS) {
MDI_PHCI_SET_RESUME(ph);
} else {
MDI_DEBUG(1, (MDI_NOTE, dip,
"!pHCI post_resume failed: error %d",
error));
MDI_PHCI_SET_SUSPEND(ph);
}
break;
}
MDI_PHCI_UNLOCK(ph);
}
if (MDI_CLIENT(dip)) {
ct = i_devi_get_client(dip);
ASSERT(ct != NULL);
MDI_CLIENT_LOCK(ct);
switch (cmd) {
case DDI_ATTACH:
MDI_DEBUG(2, (MDI_NOTE, dip,
"client post_attach called %p", (void *)ct));
if (error != DDI_SUCCESS) {
MDI_DEBUG(1, (MDI_NOTE, dip,
"!client post_attach failed: error %d",
error));
MDI_CLIENT_SET_DETACH(ct);
MDI_DEBUG(4, (MDI_WARN, dip,
"i_mdi_pm_reset_client"));
i_mdi_pm_reset_client(ct);
break;
}
vh = ct->ct_vhci;
if (vh->vh_ops->vo_client_attached)
(*vh->vh_ops->vo_client_attached)(dip);
MDI_CLIENT_SET_ATTACH(ct);
break;
case DDI_RESUME:
case DDI_PM_RESUME:
MDI_DEBUG(2, (MDI_NOTE, dip,
"client post_attach: called %p", (void *)ct));
if (error == DDI_SUCCESS) {
MDI_CLIENT_SET_RESUME(ct);
} else {
MDI_DEBUG(1, (MDI_NOTE, dip,
"!client post_resume failed: error %d",
error));
MDI_CLIENT_SET_SUSPEND(ct);
}
break;
}
MDI_CLIENT_UNLOCK(ct);
}
}
int
mdi_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
int rv = DDI_SUCCESS;
if (MDI_CLIENT(dip)) {
(void) i_mdi_client_pre_detach(dip, cmd);
}
if (MDI_PHCI(dip)) {
rv = i_mdi_phci_pre_detach(dip, cmd);
}
return (rv);
}
static int
i_mdi_phci_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
int rv = DDI_SUCCESS;
mdi_phci_t *ph;
mdi_client_t *ct;
mdi_pathinfo_t *pip;
mdi_pathinfo_t *failed_pip = NULL;
mdi_pathinfo_t *next;
ph = i_devi_get_phci(dip);
if (ph == NULL) {
return (rv);
}
MDI_PHCI_LOCK(ph);
switch (cmd) {
case DDI_DETACH:
MDI_DEBUG(2, (MDI_NOTE, dip,
"pHCI pre_detach: called %p", (void *)ph));
if (!MDI_PHCI_IS_OFFLINE(ph)) {
MDI_DEBUG(2, (MDI_WARN, dip,
"pHCI pre_detach: paths are still attached %p",
(void *)ph));
rv = DDI_FAILURE;
break;
}
MDI_PHCI_SET_DETACH(ph);
break;
case DDI_SUSPEND:
MDI_DEBUG(2, (MDI_NOTE, dip,
"pHCI pre_suspend: called %p", (void *)ph));
pip = ph->ph_path_head;
while (pip != NULL && rv == DDI_SUCCESS) {
dev_info_t *cdip;
MDI_PI_LOCK(pip);
next =
(mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
ct = MDI_PI(pip)->pi_client;
i_mdi_client_lock(ct, pip);
cdip = ct->ct_dip;
MDI_PI_UNLOCK(pip);
if ((MDI_CLIENT_IS_DETACHED(ct) == 0) &&
MDI_CLIENT_IS_SUSPENDED(ct) == 0) {
i_mdi_client_unlock(ct);
if ((rv = devi_detach(cdip, DDI_SUSPEND)) !=
DDI_SUCCESS) {
MDI_DEBUG(1, (MDI_WARN, dip,
"!suspend of device (%s%d) failed.",
ddi_driver_name(cdip),
ddi_get_instance(cdip)));
failed_pip = pip;
break;
}
} else {
i_mdi_client_unlock(ct);
}
pip = next;
}
if (rv == DDI_SUCCESS) {
MDI_PHCI_SET_SUSPEND(ph);
} else {
pip = ph->ph_path_head;
while (pip != failed_pip) {
dev_info_t *cdip;
MDI_PI_LOCK(pip);
next =
(mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
ct = MDI_PI(pip)->pi_client;
i_mdi_client_lock(ct, pip);
cdip = ct->ct_dip;
MDI_PI_UNLOCK(pip);
if (MDI_CLIENT_IS_SUSPENDED(ct)) {
i_mdi_client_unlock(ct);
(void) devi_attach(cdip, DDI_RESUME);
} else {
i_mdi_client_unlock(ct);
}
pip = next;
}
}
break;
default:
rv = DDI_FAILURE;
break;
}
MDI_PHCI_UNLOCK(ph);
return (rv);
}
static int
i_mdi_client_pre_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
int rv = DDI_SUCCESS;
mdi_client_t *ct;
ct = i_devi_get_client(dip);
if (ct == NULL) {
return (rv);
}
MDI_CLIENT_LOCK(ct);
switch (cmd) {
case DDI_DETACH:
MDI_DEBUG(2, (MDI_NOTE, dip,
"client pre_detach: called %p",
(void *)ct));
MDI_CLIENT_SET_DETACH(ct);
break;
case DDI_SUSPEND:
MDI_DEBUG(2, (MDI_NOTE, dip,
"client pre_suspend: called %p",
(void *)ct));
MDI_CLIENT_SET_SUSPEND(ct);
break;
default:
rv = DDI_FAILURE;
break;
}
MDI_CLIENT_UNLOCK(ct);
return (rv);
}
void
mdi_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
{
if (MDI_PHCI(dip))
i_mdi_phci_post_detach(dip, cmd, error);
if (MDI_CLIENT(dip))
i_mdi_client_post_detach(dip, cmd, error);
}
static void
i_mdi_phci_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
{
mdi_phci_t *ph;
ph = i_devi_get_phci(dip);
if (ph == NULL) {
return;
}
MDI_PHCI_LOCK(ph);
switch (cmd) {
case DDI_DETACH:
MDI_DEBUG(2, (MDI_NOTE, dip,
"pHCI post_detach: called %p",
(void *)ph));
if (error != DDI_SUCCESS)
MDI_PHCI_SET_ATTACH(ph);
break;
case DDI_SUSPEND:
case DDI_PM_SUSPEND:
MDI_DEBUG(2, (MDI_NOTE, dip,
"pHCI post_suspend: called %p",
(void *)ph));
if (error != DDI_SUCCESS)
MDI_PHCI_SET_RESUME(ph);
break;
case DDI_HOTPLUG_DETACH:
break;
}
MDI_PHCI_UNLOCK(ph);
}
static void
i_mdi_client_post_detach(dev_info_t *dip, ddi_detach_cmd_t cmd, int error)
{
mdi_client_t *ct;
ct = i_devi_get_client(dip);
if (ct == NULL) {
return;
}
MDI_CLIENT_LOCK(ct);
switch (cmd) {
case DDI_DETACH:
MDI_DEBUG(2, (MDI_NOTE, dip,
"client post_detach: called %p", (void *)ct));
if (DEVI_IS_ATTACHING(dip)) {
MDI_DEBUG(4, (MDI_NOTE, dip,
"i_mdi_pm_rele_client\n"));
i_mdi_pm_rele_client(ct, ct->ct_path_count);
} else {
MDI_DEBUG(4, (MDI_NOTE, dip,
"i_mdi_pm_reset_client\n"));
i_mdi_pm_reset_client(ct);
}
if (error != DDI_SUCCESS)
MDI_CLIENT_SET_ATTACH(ct);
break;
case DDI_SUSPEND:
case DDI_PM_SUSPEND:
MDI_DEBUG(2, (MDI_NOTE, dip,
"called %p", (void *)ct));
if (error != DDI_SUCCESS)
MDI_CLIENT_SET_RESUME(ct);
break;
case DDI_HOTPLUG_DETACH:
break;
}
MDI_CLIENT_UNLOCK(ct);
}
int
mdi_pi_kstat_exists(mdi_pathinfo_t *pip)
{
return (MDI_PI(pip)->pi_kstats ? 1 : 0);
}
int
mdi_pi_kstat_create(mdi_pathinfo_t *pip, char *ksname)
{
kstat_t *kiosp, *kerrsp;
struct pi_errs *nsp;
struct mdi_pi_kstats *mdi_statp;
if (MDI_PI(pip)->pi_kstats != NULL)
return (MDI_SUCCESS);
if ((kiosp = kstat_create("mdi", 0, ksname, "iopath",
KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT)) == NULL) {
return (MDI_FAILURE);
}
(void) strcat(ksname, ",err");
kerrsp = kstat_create("mdi", 0, ksname, "iopath_errors",
KSTAT_TYPE_NAMED,
sizeof (struct pi_errs) / sizeof (kstat_named_t), 0);
if (kerrsp == NULL) {
kstat_delete(kiosp);
return (MDI_FAILURE);
}
nsp = (struct pi_errs *)kerrsp->ks_data;
kstat_named_init(&nsp->pi_softerrs, "Soft Errors", KSTAT_DATA_UINT32);
kstat_named_init(&nsp->pi_harderrs, "Hard Errors", KSTAT_DATA_UINT32);
kstat_named_init(&nsp->pi_transerrs, "Transport Errors",
KSTAT_DATA_UINT32);
kstat_named_init(&nsp->pi_icnt_busy, "Interconnect Busy",
KSTAT_DATA_UINT32);
kstat_named_init(&nsp->pi_icnt_errors, "Interconnect Errors",
KSTAT_DATA_UINT32);
kstat_named_init(&nsp->pi_phci_rsrc, "pHCI No Resources",
KSTAT_DATA_UINT32);
kstat_named_init(&nsp->pi_phci_localerr, "pHCI Local Errors",
KSTAT_DATA_UINT32);
kstat_named_init(&nsp->pi_phci_invstate, "pHCI Invalid State",
KSTAT_DATA_UINT32);
kstat_named_init(&nsp->pi_failedfrom, "Failed From",
KSTAT_DATA_UINT32);
kstat_named_init(&nsp->pi_failedto, "Failed To", KSTAT_DATA_UINT32);
mdi_statp = kmem_alloc(sizeof (*mdi_statp), KM_SLEEP);
mdi_statp->pi_kstat_ref = 1;
mdi_statp->pi_kstat_iostats = kiosp;
mdi_statp->pi_kstat_errstats = kerrsp;
kstat_install(kiosp);
kstat_install(kerrsp);
MDI_PI(pip)->pi_kstats = mdi_statp;
return (MDI_SUCCESS);
}
static void
i_mdi_pi_kstat_destroy(mdi_pathinfo_t *pip)
{
struct mdi_pi_kstats *mdi_statp;
if (MDI_PI(pip)->pi_kstats == NULL)
return;
if ((mdi_statp = MDI_PI(pip)->pi_kstats) == NULL)
return;
MDI_PI(pip)->pi_kstats = NULL;
ASSERT(mdi_statp->pi_kstat_ref > 0);
if (--mdi_statp->pi_kstat_ref != 0)
return;
kstat_delete(mdi_statp->pi_kstat_iostats);
kstat_delete(mdi_statp->pi_kstat_errstats);
kmem_free(mdi_statp, sizeof (*mdi_statp));
}
void
mdi_pi_kstat_iosupdate(mdi_pathinfo_t *pip, struct buf *bp)
{
kstat_t *iostatp;
size_t xfer_cnt;
ASSERT(pip != NULL);
if (bp != NULL && MDI_PI(pip)->pi_kstats != NULL) {
iostatp = MDI_PI(pip)->pi_kstats->pi_kstat_iostats;
xfer_cnt = bp->b_bcount - bp->b_resid;
if (bp->b_flags & B_READ) {
KSTAT_IO_PTR(iostatp)->reads++;
KSTAT_IO_PTR(iostatp)->nread += xfer_cnt;
} else {
KSTAT_IO_PTR(iostatp)->writes++;
KSTAT_IO_PTR(iostatp)->nwritten += xfer_cnt;
}
}
}
int
mdi_pi_enable_path(mdi_pathinfo_t *pip, int flags)
{
mdi_phci_t *ph;
ph = MDI_PI(pip)->pi_phci;
if (ph == NULL) {
MDI_DEBUG(1, (MDI_NOTE, mdi_pi_get_phci(pip),
"!failed: path %s %p: NULL ph",
mdi_pi_spathname(pip), (void *)pip));
return (MDI_FAILURE);
}
(void) i_mdi_enable_disable_path(pip, ph->ph_vhci, flags,
MDI_ENABLE_OP);
MDI_DEBUG(5, (MDI_NOTE, ph->ph_dip,
"!returning success pip = %p. ph = %p",
(void *)pip, (void *)ph));
return (MDI_SUCCESS);
}
int
mdi_pi_disable_path(mdi_pathinfo_t *pip, int flags)
{
mdi_phci_t *ph;
ph = MDI_PI(pip)->pi_phci;
if (ph == NULL) {
MDI_DEBUG(1, (MDI_NOTE, mdi_pi_get_phci(pip),
"!failed: path %s %p: NULL ph",
mdi_pi_spathname(pip), (void *)pip));
return (MDI_FAILURE);
}
(void) i_mdi_enable_disable_path(pip,
ph->ph_vhci, flags, MDI_DISABLE_OP);
MDI_DEBUG(5, (MDI_NOTE, ph->ph_dip,
"!returning success pip = %p. ph = %p",
(void *)pip, (void *)ph));
return (MDI_SUCCESS);
}
int
mdi_pi_disable(dev_info_t *cdip, dev_info_t *pdip, int flags)
{
return (i_mdi_pi_enable_disable(cdip, pdip, flags, MDI_DISABLE_OP));
}
int
mdi_pi_enable(dev_info_t *cdip, dev_info_t *pdip, int flags)
{
return (i_mdi_pi_enable_disable(cdip, pdip, flags, MDI_ENABLE_OP));
}
static mdi_pathinfo_t *
i_mdi_enable_disable_path(mdi_pathinfo_t *pip, mdi_vhci_t *vh, int flags,
int op)
{
int sync_flag = 0;
int rv;
mdi_pathinfo_t *next;
int (*f)() = NULL;
if ((MDI_PI_IS_DISABLE(pip) && op == MDI_DISABLE_OP) ||
(!MDI_PI_IS_DISABLE(pip) && op == MDI_ENABLE_OP)) {
MDI_PI_LOCK(pip);
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
MDI_PI_UNLOCK(pip);
return (next);
}
f = vh->vh_ops->vo_pi_state_change;
sync_flag = (flags << 8) & 0xf00;
rv = MDI_SUCCESS;
if (f != NULL) {
rv = (*f)(vh->vh_dip, pip, 0,
MDI_PI_EXT_STATE(pip),
MDI_EXT_STATE_CHANGE | sync_flag |
op | MDI_BEFORE_STATE_CHANGE);
if (rv != MDI_SUCCESS) {
MDI_DEBUG(2, (MDI_WARN, vh->vh_dip,
"vo_pi_state_change: failed rv = %x", rv));
}
}
MDI_PI_LOCK(pip);
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_phci_link;
switch (flags) {
case USER_DISABLE:
if (op == MDI_DISABLE_OP) {
MDI_PI_SET_USER_DISABLE(pip);
} else {
MDI_PI_SET_USER_ENABLE(pip);
}
break;
case DRIVER_DISABLE:
if (op == MDI_DISABLE_OP) {
MDI_PI_SET_DRV_DISABLE(pip);
} else {
MDI_PI_SET_DRV_ENABLE(pip);
}
break;
case DRIVER_DISABLE_TRANSIENT:
if (op == MDI_DISABLE_OP && rv == MDI_SUCCESS) {
MDI_PI_SET_DRV_DISABLE_TRANS(pip);
} else {
MDI_PI_SET_DRV_ENABLE_TRANS(pip);
}
break;
}
MDI_PI_UNLOCK(pip);
if (f != NULL) {
rv = (*f)(vh->vh_dip, pip, 0,
MDI_PI_EXT_STATE(pip),
MDI_EXT_STATE_CHANGE | sync_flag |
op | MDI_AFTER_STATE_CHANGE);
if (rv != MDI_SUCCESS) {
MDI_DEBUG(2, (MDI_WARN, vh->vh_dip,
"vo_pi_state_change failed: rv = %x", rv));
}
}
return (next);
}
int
i_mdi_pi_enable_disable(dev_info_t *cdip, dev_info_t *pdip, int flags, int op)
{
mdi_phci_t *ph;
mdi_vhci_t *vh = NULL;
mdi_client_t *ct;
mdi_pathinfo_t *next, *pip;
int found_it;
ph = i_devi_get_phci(pdip);
MDI_DEBUG(5, (MDI_NOTE, cdip ? cdip : pdip,
"!op = %d pdip = %p cdip = %p", op, (void *)pdip,
(void *)cdip));
if (ph == NULL) {
MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
"!failed: operation %d: NULL ph", op));
return (MDI_FAILURE);
}
if ((op != MDI_ENABLE_OP) && (op != MDI_DISABLE_OP)) {
MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
"!failed: invalid operation %d", op));
return (MDI_FAILURE);
}
vh = ph->ph_vhci;
if (cdip == NULL) {
MDI_DEBUG(4, (MDI_NOTE, cdip ? cdip : pdip,
"op %d for the phci", op));
MDI_PHCI_LOCK(ph);
switch (flags) {
case USER_DISABLE:
if (op == MDI_DISABLE_OP) {
MDI_PHCI_SET_USER_DISABLE(ph);
} else {
MDI_PHCI_SET_USER_ENABLE(ph);
}
break;
case DRIVER_DISABLE:
if (op == MDI_DISABLE_OP) {
MDI_PHCI_SET_DRV_DISABLE(ph);
} else {
MDI_PHCI_SET_DRV_ENABLE(ph);
}
break;
case DRIVER_DISABLE_TRANSIENT:
if (op == MDI_DISABLE_OP) {
MDI_PHCI_SET_DRV_DISABLE_TRANSIENT(ph);
} else {
MDI_PHCI_SET_DRV_ENABLE_TRANSIENT(ph);
}
break;
default:
MDI_PHCI_UNLOCK(ph);
MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
"!invalid flag argument= %d", flags));
}
pip = ph->ph_path_head;
while (pip != NULL) {
pip = i_mdi_enable_disable_path(pip, vh, flags, op);
}
MDI_PHCI_UNLOCK(ph);
} else {
ct = i_devi_get_client(cdip);
if (ct == NULL) {
MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
"!failed: operation = %d: NULL ct", op));
return (MDI_FAILURE);
}
MDI_CLIENT_LOCK(ct);
pip = ct->ct_path_head;
found_it = 0;
while (pip != NULL) {
MDI_PI_LOCK(pip);
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
if (MDI_PI(pip)->pi_phci == ph) {
MDI_PI_UNLOCK(pip);
found_it = 1;
break;
}
MDI_PI_UNLOCK(pip);
pip = next;
}
MDI_CLIENT_UNLOCK(ct);
if (found_it == 0) {
MDI_DEBUG(1, (MDI_NOTE, cdip ? cdip : pdip,
"!failed. Could not find corresponding pip\n"));
return (MDI_FAILURE);
}
(void) i_mdi_enable_disable_path(pip, vh, flags, op);
}
MDI_DEBUG(5, (MDI_NOTE, cdip ? cdip : pdip,
"!op %d returning success pdip = %p cdip = %p",
op, (void *)pdip, (void *)cdip));
return (MDI_SUCCESS);
}
static void
i_mdi_pm_hold_pip(mdi_pathinfo_t *pip)
{
dev_info_t *ph_dip;
ASSERT(pip != NULL);
ASSERT(MDI_PI_LOCKED(pip));
if (MDI_PI(pip)->pi_pm_held) {
return;
}
ph_dip = mdi_pi_get_phci(pip);
MDI_DEBUG(4, (MDI_NOTE, ph_dip,
"%s %p", mdi_pi_spathname(pip), (void *)pip));
if (ph_dip == NULL) {
return;
}
MDI_PI_UNLOCK(pip);
MDI_DEBUG(4, (MDI_NOTE, ph_dip, "kidsupcnt was %d",
DEVI(ph_dip)->devi_pm_kidsupcnt));
pm_hold_power(ph_dip);
MDI_DEBUG(4, (MDI_NOTE, ph_dip, "kidsupcnt is %d",
DEVI(ph_dip)->devi_pm_kidsupcnt));
MDI_PI_LOCK(pip);
if (DEVI(ph_dip)->devi_pm_info)
MDI_PI(pip)->pi_pm_held = 1;
}
static void
i_mdi_pm_rele_pip(mdi_pathinfo_t *pip)
{
dev_info_t *ph_dip = NULL;
ASSERT(pip != NULL);
ASSERT(MDI_PI_LOCKED(pip));
if (MDI_PI(pip)->pi_pm_held == 0) {
return;
}
ph_dip = mdi_pi_get_phci(pip);
ASSERT(ph_dip != NULL);
MDI_DEBUG(4, (MDI_NOTE, ph_dip,
"%s %p", mdi_pi_spathname(pip), (void *)pip));
MDI_PI_UNLOCK(pip);
MDI_DEBUG(4, (MDI_NOTE, ph_dip,
"kidsupcnt was %d", DEVI(ph_dip)->devi_pm_kidsupcnt));
pm_rele_power(ph_dip);
MDI_DEBUG(4, (MDI_NOTE, ph_dip,
"kidsupcnt is %d", DEVI(ph_dip)->devi_pm_kidsupcnt));
MDI_PI_LOCK(pip);
MDI_PI(pip)->pi_pm_held = 0;
}
static void
i_mdi_pm_hold_client(mdi_client_t *ct, int incr)
{
ASSERT(MDI_CLIENT_LOCKED(ct));
ct->ct_power_cnt += incr;
MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
"%p ct_power_cnt = %d incr = %d",
(void *)ct, ct->ct_power_cnt, incr));
ASSERT(ct->ct_power_cnt >= 0);
}
static void
i_mdi_rele_all_phci(mdi_client_t *ct)
{
mdi_pathinfo_t *pip;
ASSERT(MDI_CLIENT_LOCKED(ct));
pip = (mdi_pathinfo_t *)ct->ct_path_head;
while (pip != NULL) {
mdi_hold_path(pip);
MDI_PI_LOCK(pip);
i_mdi_pm_rele_pip(pip);
MDI_PI_UNLOCK(pip);
mdi_rele_path(pip);
pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
}
}
static void
i_mdi_pm_rele_client(mdi_client_t *ct, int decr)
{
ASSERT(MDI_CLIENT_LOCKED(ct));
if (i_ddi_devi_attached(ct->ct_dip)) {
ct->ct_power_cnt -= decr;
MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
"%p ct_power_cnt = %d decr = %d",
(void *)ct, ct->ct_power_cnt, decr));
}
ASSERT(ct->ct_power_cnt >= 0);
if (ct->ct_power_cnt == 0) {
i_mdi_rele_all_phci(ct);
return;
}
}
static void
i_mdi_pm_reset_client(mdi_client_t *ct)
{
MDI_DEBUG(4, (MDI_NOTE, ct->ct_dip,
"%p ct_power_cnt = %d", (void *)ct, ct->ct_power_cnt));
ASSERT(MDI_CLIENT_LOCKED(ct));
ct->ct_power_cnt = 0;
i_mdi_rele_all_phci(ct);
ct->ct_powercnt_config = 0;
ct->ct_powercnt_unconfig = 0;
ct->ct_powercnt_reset = 1;
}
static int
i_mdi_power_one_phci(mdi_pathinfo_t *pip)
{
int ret;
dev_info_t *ph_dip;
MDI_PI_LOCK(pip);
i_mdi_pm_hold_pip(pip);
ph_dip = mdi_pi_get_phci(pip);
MDI_PI_UNLOCK(pip);
MDI_DEBUG(4, (MDI_NOTE, ph_dip,
"pm_powerup for %s%d %p", ddi_driver_name(ph_dip),
ddi_get_instance(ph_dip), (void *)pip));
ret = pm_powerup(ph_dip);
if (ret == DDI_FAILURE) {
MDI_DEBUG(4, (MDI_NOTE, ph_dip,
"pm_powerup FAILED for %s%d %p",
ddi_driver_name(ph_dip), ddi_get_instance(ph_dip),
(void *)pip));
MDI_PI_LOCK(pip);
i_mdi_pm_rele_pip(pip);
MDI_PI_UNLOCK(pip);
return (MDI_FAILURE);
}
return (MDI_SUCCESS);
}
static int
i_mdi_power_all_phci(mdi_client_t *ct)
{
mdi_pathinfo_t *pip;
int succeeded = 0;
ASSERT(MDI_CLIENT_LOCKED(ct));
pip = (mdi_pathinfo_t *)ct->ct_path_head;
while (pip != NULL) {
if (MDI_PI_IS_INIT(pip) ||
MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip)) {
mdi_hold_path(pip);
MDI_CLIENT_UNLOCK(ct);
if (i_mdi_power_one_phci(pip) == MDI_SUCCESS)
succeeded = 1;
ASSERT(ct == MDI_PI(pip)->pi_client);
MDI_CLIENT_LOCK(ct);
mdi_rele_path(pip);
}
pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
}
return (succeeded ? MDI_SUCCESS : MDI_FAILURE);
}
int
mdi_bus_power(dev_info_t *parent, void *impl_arg, pm_bus_power_op_t op,
void *arg, void *result)
{
int ret = MDI_SUCCESS;
pm_bp_child_pwrchg_t *bpc;
mdi_client_t *ct;
dev_info_t *cdip;
pm_bp_has_changed_t *bphc;
if (op == BUS_POWER_NOINVOL)
return (MDI_FAILURE);
switch (op) {
case BUS_POWER_PRE_NOTIFICATION:
case BUS_POWER_POST_NOTIFICATION:
bpc = (pm_bp_child_pwrchg_t *)arg;
cdip = bpc->bpc_dip;
break;
case BUS_POWER_HAS_CHANGED:
bphc = (pm_bp_has_changed_t *)arg;
cdip = bphc->bphc_dip;
break;
default:
return (pm_busop_bus_power(parent, impl_arg, op, arg, result));
}
ASSERT(MDI_CLIENT(cdip));
ct = i_devi_get_client(cdip);
if (ct == NULL)
return (MDI_FAILURE);
MDI_CLIENT_LOCK(ct);
switch (op) {
case BUS_POWER_PRE_NOTIFICATION:
MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
"BUS_POWER_PRE_NOTIFICATION:"
"%s@%s, olevel=%d, nlevel=%d, comp=%d",
ddi_node_name(bpc->bpc_dip), PM_ADDR(bpc->bpc_dip),
bpc->bpc_olevel, bpc->bpc_nlevel, bpc->bpc_comp));
while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
MDI_CLIENT_SET_POWER_TRANSITION(ct);
if (ct->ct_power_cnt == 0) {
ret = i_mdi_power_all_phci(ct);
}
if (bpc->bpc_nlevel > 0) {
if (!DEVI_IS_ATTACHING(ct->ct_dip)) {
MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
"i_mdi_pm_hold_client\n"));
i_mdi_pm_hold_client(ct, ct->ct_path_count);
}
}
break;
case BUS_POWER_POST_NOTIFICATION:
MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
"BUS_POWER_POST_NOTIFICATION:"
"%s@%s, olevel=%d, nlevel=%d, comp=%d result=%d",
ddi_node_name(bpc->bpc_dip), PM_ADDR(bpc->bpc_dip),
bpc->bpc_olevel, bpc->bpc_nlevel, bpc->bpc_comp,
*(int *)result));
if (*(int *)result == DDI_SUCCESS) {
if (bpc->bpc_nlevel > 0) {
MDI_CLIENT_SET_POWER_UP(ct);
} else {
MDI_CLIENT_SET_POWER_DOWN(ct);
}
}
if (bpc->bpc_nlevel > 0 && (*(int *)result != DDI_SUCCESS) &&
!DEVI_IS_ATTACHING(ct->ct_dip)) {
MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
"i_mdi_pm_rele_client\n"));
i_mdi_pm_rele_client(ct, ct->ct_path_count);
}
if (bpc->bpc_nlevel == 0 && (*(int *)result == DDI_SUCCESS)) {
if (DEVI_IS_ATTACHING(ct->ct_dip)) {
MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
"i_mdi_pm_rele_client\n"));
i_mdi_pm_rele_client(ct, ct->ct_path_count);
} else if (!DEVI_IS_DETACHING(ct->ct_dip)) {
MDI_DEBUG(4, (MDI_NOTE, bpc->bpc_dip,
"i_mdi_pm_reset_client\n"));
i_mdi_pm_reset_client(ct);
}
}
MDI_CLIENT_CLEAR_POWER_TRANSITION(ct);
cv_broadcast(&ct->ct_powerchange_cv);
break;
case BUS_POWER_HAS_CHANGED:
MDI_DEBUG(4, (MDI_NOTE, bphc->bphc_dip,
"BUS_POWER_HAS_CHANGED:"
"%s@%s, olevel=%d, nlevel=%d, comp=%d",
ddi_node_name(bphc->bphc_dip), PM_ADDR(bphc->bphc_dip),
bphc->bphc_olevel, bphc->bphc_nlevel, bphc->bphc_comp));
if (bphc->bphc_nlevel > 0 &&
bphc->bphc_nlevel > bphc->bphc_olevel) {
if (ct->ct_power_cnt == 0) {
ret = i_mdi_power_all_phci(ct);
}
MDI_DEBUG(4, (MDI_NOTE, bphc->bphc_dip,
"i_mdi_pm_hold_client\n"));
i_mdi_pm_hold_client(ct, ct->ct_path_count);
}
if (bphc->bphc_nlevel == 0 && bphc->bphc_olevel != -1) {
MDI_DEBUG(4, (MDI_NOTE, bphc->bphc_dip,
"i_mdi_pm_rele_client\n"));
i_mdi_pm_rele_client(ct, ct->ct_path_count);
}
break;
default:
dev_err(parent, CE_WARN, "!unhandled bus power operation: 0x%x",
op);
break;
}
MDI_CLIENT_UNLOCK(ct);
return (ret);
}
static int
i_mdi_pm_pre_config_one(dev_info_t *child)
{
int ret = MDI_SUCCESS;
mdi_client_t *ct;
ct = i_devi_get_client(child);
if (ct == NULL)
return (MDI_FAILURE);
MDI_CLIENT_LOCK(ct);
while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
if (!MDI_CLIENT_IS_FAILED(ct)) {
MDI_CLIENT_UNLOCK(ct);
MDI_DEBUG(4, (MDI_NOTE, child, "already configured\n"));
return (MDI_SUCCESS);
}
if (ct->ct_powercnt_config) {
MDI_CLIENT_UNLOCK(ct);
MDI_DEBUG(4, (MDI_NOTE, child, "already held\n"));
return (MDI_SUCCESS);
}
if (ct->ct_power_cnt == 0) {
ret = i_mdi_power_all_phci(ct);
}
MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_hold_client\n"));
i_mdi_pm_hold_client(ct, ct->ct_path_count);
ct->ct_powercnt_config = 1;
ct->ct_powercnt_reset = 0;
MDI_CLIENT_UNLOCK(ct);
return (ret);
}
static int
i_mdi_pm_pre_config(dev_info_t *vdip, dev_info_t *child)
{
int ret = MDI_SUCCESS;
dev_info_t *cdip;
ASSERT(MDI_VHCI(vdip));
if (child) {
ASSERT(DEVI_BUSY_OWNED(vdip));
return (i_mdi_pm_pre_config_one(child));
}
ndi_devi_enter(vdip);
cdip = ddi_get_child(vdip);
while (cdip) {
dev_info_t *next = ddi_get_next_sibling(cdip);
ret = i_mdi_pm_pre_config_one(cdip);
if (ret != MDI_SUCCESS)
break;
cdip = next;
}
ndi_devi_exit(vdip);
return (ret);
}
static int
i_mdi_pm_pre_unconfig_one(dev_info_t *child, int *held, int flags)
{
int ret = MDI_SUCCESS;
mdi_client_t *ct;
ct = i_devi_get_client(child);
if (ct == NULL)
return (MDI_FAILURE);
MDI_CLIENT_LOCK(ct);
while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
if (!i_ddi_devi_attached(child)) {
MDI_DEBUG(4, (MDI_NOTE, child, "node detached already\n"));
MDI_CLIENT_UNLOCK(ct);
return (MDI_SUCCESS);
}
if (MDI_CLIENT_IS_POWERED_DOWN(ct) &&
(flags & NDI_AUTODETACH)) {
MDI_DEBUG(4, (MDI_NOTE, child, "auto-modunload\n"));
MDI_CLIENT_UNLOCK(ct);
return (MDI_FAILURE);
}
if (ct->ct_powercnt_unconfig) {
MDI_DEBUG(4, (MDI_NOTE, child, "ct_powercnt_held\n"));
MDI_CLIENT_UNLOCK(ct);
*held = 1;
return (MDI_SUCCESS);
}
if (ct->ct_power_cnt == 0) {
ret = i_mdi_power_all_phci(ct);
}
MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_hold_client\n"));
i_mdi_pm_hold_client(ct, ct->ct_path_count);
ct->ct_powercnt_unconfig = 1;
ct->ct_powercnt_reset = 0;
MDI_CLIENT_UNLOCK(ct);
if (ret == MDI_SUCCESS)
*held = 1;
return (ret);
}
static int
i_mdi_pm_pre_unconfig(dev_info_t *vdip, dev_info_t *child, int *held,
int flags)
{
int ret = MDI_SUCCESS;
dev_info_t *cdip;
ASSERT(MDI_VHCI(vdip));
*held = 0;
if (child) {
ASSERT(DEVI_BUSY_OWNED(vdip));
return (i_mdi_pm_pre_unconfig_one(child, held, flags));
}
ndi_devi_enter(vdip);
cdip = ddi_get_child(vdip);
while (cdip) {
dev_info_t *next = ddi_get_next_sibling(cdip);
ret = i_mdi_pm_pre_unconfig_one(cdip, held, flags);
cdip = next;
}
ndi_devi_exit(vdip);
if (*held)
ret = MDI_SUCCESS;
return (ret);
}
static void
i_mdi_pm_post_config_one(dev_info_t *child)
{
mdi_client_t *ct;
ct = i_devi_get_client(child);
if (ct == NULL)
return;
MDI_CLIENT_LOCK(ct);
while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
if (ct->ct_powercnt_reset || !ct->ct_powercnt_config) {
MDI_DEBUG(4, (MDI_NOTE, child, "not configured\n"));
MDI_CLIENT_UNLOCK(ct);
return;
}
if (MDI_CLIENT_IS_FAILED(ct)) {
MDI_DEBUG(4, (MDI_NOTE, child, "client failed\n"));
MDI_CLIENT_UNLOCK(ct);
return;
}
if ((MDI_CLIENT_IS_POWERED_DOWN(ct) &&
!DEVI_IS_ATTACHING(child)) ||
(!i_ddi_devi_attached(child) &&
!DEVI_IS_ATTACHING(child))) {
MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_reset_client\n"));
i_mdi_pm_reset_client(ct);
} else {
mdi_pathinfo_t *pip, *next;
int valid_path_count = 0;
MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_rele_client\n"));
pip = ct->ct_path_head;
while (pip != NULL) {
MDI_PI_LOCK(pip);
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
if (MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip))
valid_path_count ++;
MDI_PI_UNLOCK(pip);
pip = next;
}
i_mdi_pm_rele_client(ct, valid_path_count);
}
ct->ct_powercnt_config = 0;
MDI_CLIENT_UNLOCK(ct);
}
static void
i_mdi_pm_post_config(dev_info_t *vdip, dev_info_t *child)
{
dev_info_t *cdip;
ASSERT(MDI_VHCI(vdip));
if (child) {
ASSERT(DEVI_BUSY_OWNED(vdip));
i_mdi_pm_post_config_one(child);
return;
}
ndi_devi_enter(vdip);
cdip = ddi_get_child(vdip);
while (cdip) {
dev_info_t *next = ddi_get_next_sibling(cdip);
i_mdi_pm_post_config_one(cdip);
cdip = next;
}
ndi_devi_exit(vdip);
}
static void
i_mdi_pm_post_unconfig_one(dev_info_t *child)
{
mdi_client_t *ct;
ct = i_devi_get_client(child);
if (ct == NULL)
return;
MDI_CLIENT_LOCK(ct);
while (MDI_CLIENT_IS_POWER_TRANSITION(ct))
cv_wait(&ct->ct_powerchange_cv, &ct->ct_mutex);
if (!ct->ct_powercnt_unconfig || ct->ct_powercnt_reset) {
MDI_DEBUG(4, (MDI_NOTE, child, "not held\n"));
MDI_CLIENT_UNLOCK(ct);
return;
}
if ((MDI_CLIENT_IS_POWERED_DOWN(ct) &&
i_ddi_devi_attached(child)) ||
(!i_ddi_devi_attached(child) &&
!DEVI_IS_ATTACHING(child))) {
MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_reset_client\n"));
i_mdi_pm_reset_client(ct);
} else {
mdi_pathinfo_t *pip, *next;
int valid_path_count = 0;
MDI_DEBUG(4, (MDI_NOTE, child, "i_mdi_pm_rele_client\n"));
pip = ct->ct_path_head;
while (pip != NULL) {
MDI_PI_LOCK(pip);
next = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link;
if (MDI_PI_IS_ONLINE(pip) || MDI_PI_IS_STANDBY(pip))
valid_path_count ++;
MDI_PI_UNLOCK(pip);
pip = next;
}
i_mdi_pm_rele_client(ct, valid_path_count);
ct->ct_powercnt_unconfig = 0;
}
MDI_CLIENT_UNLOCK(ct);
}
static void
i_mdi_pm_post_unconfig(dev_info_t *vdip, dev_info_t *child, int held)
{
dev_info_t *cdip;
ASSERT(MDI_VHCI(vdip));
if (!held) {
MDI_DEBUG(4, (MDI_NOTE, vdip, "held = %d", held));
return;
}
if (child) {
ASSERT(DEVI_BUSY_OWNED(vdip));
i_mdi_pm_post_unconfig_one(child);
return;
}
ndi_devi_enter(vdip);
cdip = ddi_get_child(vdip);
while (cdip) {
dev_info_t *next = ddi_get_next_sibling(cdip);
i_mdi_pm_post_unconfig_one(cdip);
cdip = next;
}
ndi_devi_exit(vdip);
}
int
mdi_power(dev_info_t *vdip, mdi_pm_op_t op, void *args, char *devnm, int flags)
{
int ret = MDI_SUCCESS;
dev_info_t *client_dip = NULL;
mdi_client_t *ct;
if (devnm != NULL) {
ndi_devi_enter(vdip);
client_dip = ndi_devi_findchild(vdip, devnm);
}
MDI_DEBUG(4, (MDI_NOTE, vdip,
"op = %d %s %p", op, devnm ? devnm : "", (void *)client_dip));
switch (op) {
case MDI_PM_PRE_CONFIG:
ret = i_mdi_pm_pre_config(vdip, client_dip);
break;
case MDI_PM_PRE_UNCONFIG:
ret = i_mdi_pm_pre_unconfig(vdip, client_dip, (int *)args,
flags);
break;
case MDI_PM_POST_CONFIG:
i_mdi_pm_post_config(vdip, client_dip);
break;
case MDI_PM_POST_UNCONFIG:
i_mdi_pm_post_unconfig(vdip, client_dip, *(int *)args);
break;
case MDI_PM_HOLD_POWER:
case MDI_PM_RELE_POWER:
ASSERT(args);
client_dip = (dev_info_t *)args;
ASSERT(MDI_CLIENT(client_dip));
ct = i_devi_get_client(client_dip);
MDI_CLIENT_LOCK(ct);
if (op == MDI_PM_HOLD_POWER) {
if (ct->ct_power_cnt == 0) {
(void) i_mdi_power_all_phci(ct);
MDI_DEBUG(4, (MDI_NOTE, client_dip,
"i_mdi_pm_hold_client\n"));
i_mdi_pm_hold_client(ct, ct->ct_path_count);
}
} else {
if (DEVI_IS_ATTACHING(client_dip)) {
MDI_DEBUG(4, (MDI_NOTE, client_dip,
"i_mdi_pm_rele_client\n"));
i_mdi_pm_rele_client(ct, ct->ct_path_count);
} else {
MDI_DEBUG(4, (MDI_NOTE, client_dip,
"i_mdi_pm_reset_client\n"));
i_mdi_pm_reset_client(ct);
}
}
MDI_CLIENT_UNLOCK(ct);
break;
default:
break;
}
if (devnm)
ndi_devi_exit(vdip);
return (ret);
}
int
mdi_component_is_vhci(dev_info_t *dip, const char **mdi_class)
{
mdi_vhci_t *vhci;
if (!MDI_VHCI(dip))
return (MDI_FAILURE);
if (mdi_class) {
vhci = DEVI(dip)->devi_mdi_xhci;
ASSERT(vhci);
*mdi_class = vhci->vh_class;
}
return (MDI_SUCCESS);
}
int
mdi_component_is_phci(dev_info_t *dip, const char **mdi_class)
{
mdi_phci_t *phci;
if (!MDI_PHCI(dip))
return (MDI_FAILURE);
if (mdi_class) {
phci = DEVI(dip)->devi_mdi_xhci;
ASSERT(phci);
*mdi_class = phci->ph_vhci->vh_class;
}
return (MDI_SUCCESS);
}
int
mdi_component_is_client(dev_info_t *dip, const char **mdi_class)
{
mdi_client_t *client;
if (!MDI_CLIENT(dip))
return (MDI_FAILURE);
if (mdi_class) {
client = DEVI(dip)->devi_mdi_client;
ASSERT(client);
*mdi_class = client->ct_vhci->vh_class;
}
return (MDI_SUCCESS);
}
void *
mdi_client_get_vhci_private(dev_info_t *dip)
{
ASSERT(mdi_component_is_client(dip, NULL) == MDI_SUCCESS);
if (mdi_component_is_client(dip, NULL) == MDI_SUCCESS) {
mdi_client_t *ct;
ct = i_devi_get_client(dip);
return (ct->ct_vprivate);
}
return (NULL);
}
void
mdi_client_set_vhci_private(dev_info_t *dip, void *data)
{
ASSERT(mdi_component_is_client(dip, NULL) == MDI_SUCCESS);
if (mdi_component_is_client(dip, NULL) == MDI_SUCCESS) {
mdi_client_t *ct;
ct = i_devi_get_client(dip);
ct->ct_vprivate = data;
}
}
void *
mdi_pi_get_vhci_private(mdi_pathinfo_t *pip)
{
caddr_t vprivate = NULL;
if (pip) {
vprivate = MDI_PI(pip)->pi_vprivate;
}
return (vprivate);
}
void
mdi_pi_set_vhci_private(mdi_pathinfo_t *pip, void *priv)
{
if (pip) {
MDI_PI(pip)->pi_vprivate = priv;
}
}
void *
mdi_phci_get_vhci_private(dev_info_t *dip)
{
ASSERT(mdi_component_is_phci(dip, NULL) == MDI_SUCCESS);
if (mdi_component_is_phci(dip, NULL) == MDI_SUCCESS) {
mdi_phci_t *ph;
ph = i_devi_get_phci(dip);
return (ph->ph_vprivate);
}
return (NULL);
}
void
mdi_phci_set_vhci_private(dev_info_t *dip, void *priv)
{
ASSERT(mdi_component_is_phci(dip, NULL) == MDI_SUCCESS);
if (mdi_component_is_phci(dip, NULL) == MDI_SUCCESS) {
mdi_phci_t *ph;
ph = i_devi_get_phci(dip);
ph->ph_vprivate = priv;
}
}
int
mdi_pi_ishidden(mdi_pathinfo_t *pip)
{
return (MDI_PI_FLAGS_IS_HIDDEN(pip));
}
int
mdi_pi_device_isremoved(mdi_pathinfo_t *pip)
{
return (MDI_PI_FLAGS_IS_DEVICE_REMOVED(pip));
}
static int
i_mdi_client_all_devices_removed(mdi_client_t *ct)
{
mdi_pathinfo_t *pip;
int all_devices_removed = 1;
MDI_CLIENT_LOCK(ct);
for (pip = ct->ct_path_head; pip;
pip = (mdi_pathinfo_t *)MDI_PI(pip)->pi_client_link) {
if (!mdi_pi_device_isremoved(pip)) {
all_devices_removed = 0;
break;
}
}
MDI_CLIENT_UNLOCK(ct);
return (all_devices_removed);
}
int
mdi_pi_device_remove(mdi_pathinfo_t *pip)
{
mdi_client_t *ct;
MDI_PI_LOCK(pip);
if (mdi_pi_device_isremoved(pip)) {
MDI_PI_UNLOCK(pip);
return (0);
}
MDI_PI_FLAGS_SET_DEVICE_REMOVED(pip);
MDI_PI_FLAGS_SET_HIDDEN(pip);
MDI_PI_UNLOCK(pip);
ct = MDI_PI(pip)->pi_client;
if (ct && ct->ct_dip && i_mdi_client_all_devices_removed(ct))
(void) ndi_devi_device_remove(ct->ct_dip);
else
i_ddi_di_cache_invalidate();
return (1);
}
int
mdi_pi_device_insert(mdi_pathinfo_t *pip)
{
MDI_PI_LOCK(pip);
if (!mdi_pi_device_isremoved(pip)) {
MDI_PI_UNLOCK(pip);
return (0);
}
MDI_PI_FLAGS_CLR_DEVICE_REMOVED(pip);
MDI_PI_FLAGS_CLR_HIDDEN(pip);
MDI_PI_UNLOCK(pip);
i_ddi_di_cache_invalidate();
return (1);
}
static char *vhci_class_list[] = { MDI_HCI_CLASS_SCSI, MDI_HCI_CLASS_IB };
#define N_VHCI_CLASSES (sizeof (vhci_class_list) / sizeof (char *))
static nvlist_t *vhcache_nvl[N_VHCI_CLASSES];
#define MDI_VHCI_CACHE_VERSION 1
#define MDI_NVPNAME_VERSION "version"
#define MDI_NVPNAME_PHCIS "phcis"
#define MDI_NVPNAME_CTADDRMAP "clientaddrmap"
static char *
vhclass2vhcache_filename(char *vhclass)
{
char *filename;
int len;
static char *fmt = "/etc/devices/mdi_%s_cache";
len = strlen(fmt) + strlen(vhclass) - 1;
filename = kmem_alloc(len, KM_SLEEP);
(void) snprintf(filename, len, fmt, vhclass);
ASSERT(len == (strlen(filename) + 1));
return (filename);
}
static void
setup_vhci_cache(mdi_vhci_t *vh)
{
mdi_vhci_config_t *vhc;
mdi_vhci_cache_t *vhcache;
int i;
nvlist_t *nvl = NULL;
vhc = kmem_zalloc(sizeof (mdi_vhci_config_t), KM_SLEEP);
vh->vh_config = vhc;
vhcache = &vhc->vhc_vhcache;
vhc->vhc_vhcache_filename = vhclass2vhcache_filename(vh->vh_class);
mutex_init(&vhc->vhc_lock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&vhc->vhc_cv, NULL, CV_DRIVER, NULL);
rw_init(&vhcache->vhcache_lock, NULL, RW_DRIVER, NULL);
vhcache->vhcache_client_hash = mod_hash_create_extended(vh->vh_class,
mdi_bus_config_cache_hash_size,
mod_hash_null_keydtor, mod_hash_null_valdtor,
mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
for (i = 0; i < N_VHCI_CLASSES; i++) {
if (strcmp(vhci_class_list[i], vh->vh_class) == 0) {
nvl = vhcache_nvl[i];
vhcache_nvl[i] = NULL;
break;
}
}
if (nvl == NULL && modrootloaded)
nvl = read_on_disk_vhci_cache(vh->vh_class);
if (nvl != NULL) {
rw_enter(&vhcache->vhcache_lock, RW_WRITER);
if (mainnvl_to_vhcache(vhcache, nvl) == MDI_SUCCESS)
vhcache->vhcache_flags |= MDI_VHCI_CACHE_SETUP_DONE;
else {
cmn_err(CE_WARN,
"%s: data file corrupted, will recreate",
vhc->vhc_vhcache_filename);
}
rw_exit(&vhcache->vhcache_lock);
nvlist_free(nvl);
}
vhc->vhc_cbid = callb_add(stop_vhcache_flush_thread, vhc,
CB_CL_UADMIN_PRE_VFS, "mdi_vhcache_flush");
vhc->vhc_path_discovery_boot = mdi_path_discovery_boot;
vhc->vhc_path_discovery_postboot = mdi_path_discovery_postboot;
}
static int
destroy_vhci_cache(mdi_vhci_t *vh)
{
mdi_vhci_config_t *vhc = vh->vh_config;
mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
mdi_vhcache_phci_t *cphci, *cphci_next;
mdi_vhcache_client_t *cct, *cct_next;
mdi_vhcache_pathinfo_t *cpi, *cpi_next;
if (stop_vhcache_async_threads(vhc) != MDI_SUCCESS)
return (MDI_FAILURE);
kmem_free(vhc->vhc_vhcache_filename,
strlen(vhc->vhc_vhcache_filename) + 1);
mod_hash_destroy_strhash(vhcache->vhcache_client_hash);
for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
cphci = cphci_next) {
cphci_next = cphci->cphci_next;
free_vhcache_phci(cphci);
}
for (cct = vhcache->vhcache_client_head; cct != NULL; cct = cct_next) {
cct_next = cct->cct_next;
for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi_next) {
cpi_next = cpi->cpi_next;
free_vhcache_pathinfo(cpi);
}
free_vhcache_client(cct);
}
rw_destroy(&vhcache->vhcache_lock);
mutex_destroy(&vhc->vhc_lock);
cv_destroy(&vhc->vhc_cv);
kmem_free(vhc, sizeof (mdi_vhci_config_t));
return (MDI_SUCCESS);
}
static int
stop_vhcache_async_threads(mdi_vhci_config_t *vhc)
{
mdi_async_client_config_t *acc, *acc_next;
mutex_enter(&vhc->vhc_lock);
vhc->vhc_flags |= MDI_VHC_EXIT;
ASSERT(vhc->vhc_acc_thrcount >= 0);
cv_broadcast(&vhc->vhc_cv);
while ((vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) ||
vhc->vhc_acc_thrcount != 0) {
mutex_exit(&vhc->vhc_lock);
delay_random(mdi_delay);
mutex_enter(&vhc->vhc_lock);
}
vhc->vhc_flags &= ~MDI_VHC_EXIT;
for (acc = vhc->vhc_acc_list_head; acc != NULL; acc = acc_next) {
acc_next = acc->acc_next;
free_async_client_config(acc);
}
vhc->vhc_acc_list_head = NULL;
vhc->vhc_acc_list_tail = NULL;
vhc->vhc_acc_count = 0;
if (vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) {
vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
mutex_exit(&vhc->vhc_lock);
if (flush_vhcache(vhc, 0) != MDI_SUCCESS) {
vhcache_dirty(vhc);
return (MDI_FAILURE);
}
} else
mutex_exit(&vhc->vhc_lock);
if (callb_delete(vhc->vhc_cbid) != 0)
return (MDI_FAILURE);
return (MDI_SUCCESS);
}
static boolean_t
stop_vhcache_flush_thread(void *arg, int code)
{
mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
mutex_enter(&vhc->vhc_lock);
vhc->vhc_flags |= MDI_VHC_EXIT;
cv_broadcast(&vhc->vhc_cv);
while (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) {
mutex_exit(&vhc->vhc_lock);
delay_random(mdi_delay);
mutex_enter(&vhc->vhc_lock);
}
if (vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) {
vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
mutex_exit(&vhc->vhc_lock);
(void) flush_vhcache(vhc, 1);
} else
mutex_exit(&vhc->vhc_lock);
return (B_TRUE);
}
static void
enqueue_vhcache_phci(mdi_vhci_cache_t *vhcache, mdi_vhcache_phci_t *cphci)
{
cphci->cphci_next = NULL;
if (vhcache->vhcache_phci_head == NULL)
vhcache->vhcache_phci_head = cphci;
else
vhcache->vhcache_phci_tail->cphci_next = cphci;
vhcache->vhcache_phci_tail = cphci;
}
static void
enqueue_tail_vhcache_pathinfo(mdi_vhcache_client_t *cct,
mdi_vhcache_pathinfo_t *cpi)
{
cpi->cpi_next = NULL;
if (cct->cct_cpi_head == NULL)
cct->cct_cpi_head = cpi;
else
cct->cct_cpi_tail->cpi_next = cpi;
cct->cct_cpi_tail = cpi;
}
static void
enqueue_vhcache_pathinfo(mdi_vhcache_client_t *cct,
mdi_vhcache_pathinfo_t *newcpi)
{
mdi_vhcache_pathinfo_t *cpi, *prev_cpi;
if (cct->cct_cpi_head == NULL ||
(newcpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST))
enqueue_tail_vhcache_pathinfo(cct, newcpi);
else {
for (cpi = cct->cct_cpi_head, prev_cpi = NULL; cpi != NULL &&
!(cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST);
prev_cpi = cpi, cpi = cpi->cpi_next)
;
if (prev_cpi == NULL)
cct->cct_cpi_head = newcpi;
else
prev_cpi->cpi_next = newcpi;
newcpi->cpi_next = cpi;
if (cpi == NULL)
cct->cct_cpi_tail = newcpi;
}
}
static void
enqueue_vhcache_client(mdi_vhci_cache_t *vhcache,
mdi_vhcache_client_t *cct)
{
cct->cct_next = NULL;
if (vhcache->vhcache_client_head == NULL)
vhcache->vhcache_client_head = cct;
else
vhcache->vhcache_client_tail->cct_next = cct;
vhcache->vhcache_client_tail = cct;
}
static void
free_string_array(char **str, int nelem)
{
int i;
if (str) {
for (i = 0; i < nelem; i++) {
if (str[i])
kmem_free(str[i], strlen(str[i]) + 1);
}
kmem_free(str, sizeof (char *) * nelem);
}
}
static void
free_vhcache_phci(mdi_vhcache_phci_t *cphci)
{
kmem_free(cphci->cphci_path, strlen(cphci->cphci_path) + 1);
kmem_free(cphci, sizeof (*cphci));
}
static void
free_vhcache_pathinfo(mdi_vhcache_pathinfo_t *cpi)
{
kmem_free(cpi->cpi_addr, strlen(cpi->cpi_addr) + 1);
kmem_free(cpi, sizeof (*cpi));
}
static void
free_vhcache_client(mdi_vhcache_client_t *cct)
{
kmem_free(cct->cct_name_addr, strlen(cct->cct_name_addr) + 1);
kmem_free(cct, sizeof (*cct));
}
static char *
vhcache_mknameaddr(char *ct_name, char *ct_addr, int *ret_len)
{
char *name_addr;
int len;
len = strlen(ct_name) + strlen(ct_addr) + 2;
name_addr = kmem_alloc(len, KM_SLEEP);
(void) snprintf(name_addr, len, "%s@%s", ct_name, ct_addr);
if (ret_len)
*ret_len = len;
return (name_addr);
}
static void
paddrnvl_to_vhcache(nvlist_t *nvl, mdi_vhcache_phci_t *cphci_list[],
mdi_vhcache_client_t *cct)
{
nvpair_t *nvp = NULL;
mdi_vhcache_pathinfo_t *cpi;
uint_t nelem;
uint32_t *val;
while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
ASSERT(nvpair_type(nvp) == DATA_TYPE_UINT32_ARRAY);
cpi = kmem_zalloc(sizeof (*cpi), KM_SLEEP);
cpi->cpi_addr = i_ddi_strdup(nvpair_name(nvp), KM_SLEEP);
(void) nvpair_value_uint32_array(nvp, &val, &nelem);
ASSERT(nelem == 2);
cpi->cpi_cphci = cphci_list[val[0]];
cpi->cpi_flags = val[1];
enqueue_tail_vhcache_pathinfo(cct, cpi);
}
}
static void
caddrmapnvl_to_vhcache(mdi_vhci_cache_t *vhcache, nvlist_t *nvl,
mdi_vhcache_phci_t *cphci_list[])
{
nvpair_t *nvp = NULL;
nvlist_t *paddrnvl;
mdi_vhcache_client_t *cct;
while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
ASSERT(nvpair_type(nvp) == DATA_TYPE_NVLIST);
cct = kmem_zalloc(sizeof (*cct), KM_SLEEP);
cct->cct_name_addr = i_ddi_strdup(nvpair_name(nvp), KM_SLEEP);
(void) nvpair_value_nvlist(nvp, &paddrnvl);
paddrnvl_to_vhcache(paddrnvl, cphci_list, cct);
ASSERT(cct->cct_cpi_head != NULL);
enqueue_vhcache_client(vhcache, cct);
(void) mod_hash_insert(vhcache->vhcache_client_hash,
(mod_hash_key_t)cct->cct_name_addr, (mod_hash_val_t)cct);
}
}
static int
mainnvl_to_vhcache(mdi_vhci_cache_t *vhcache, nvlist_t *nvl)
{
char **phcis, **phci_namep;
uint_t nphcis;
mdi_vhcache_phci_t *cphci, **cphci_list;
nvlist_t *caddrmapnvl;
int32_t ver;
int i;
size_t cphci_list_size;
ASSERT(RW_WRITE_HELD(&vhcache->vhcache_lock));
if (nvlist_lookup_int32(nvl, MDI_NVPNAME_VERSION, &ver) != 0 ||
ver != MDI_VHCI_CACHE_VERSION)
return (MDI_FAILURE);
if (nvlist_lookup_string_array(nvl, MDI_NVPNAME_PHCIS, &phcis,
&nphcis) != 0)
return (MDI_SUCCESS);
ASSERT(nphcis > 0);
cphci_list_size = sizeof (mdi_vhcache_phci_t *) * nphcis;
cphci_list = kmem_alloc(cphci_list_size, KM_SLEEP);
for (i = 0, phci_namep = phcis; i < nphcis; i++, phci_namep++) {
cphci = kmem_zalloc(sizeof (mdi_vhcache_phci_t), KM_SLEEP);
cphci->cphci_path = i_ddi_strdup(*phci_namep, KM_SLEEP);
enqueue_vhcache_phci(vhcache, cphci);
cphci_list[i] = cphci;
}
ASSERT(vhcache->vhcache_phci_head != NULL);
if (nvlist_lookup_nvlist(nvl, MDI_NVPNAME_CTADDRMAP, &caddrmapnvl) == 0)
caddrmapnvl_to_vhcache(vhcache, caddrmapnvl, cphci_list);
kmem_free(cphci_list, cphci_list_size);
return (MDI_SUCCESS);
}
static int
vhcache_to_paddrnvl(mdi_vhci_cache_t *vhcache, mdi_vhcache_client_t *cct,
nvlist_t *caddrmapnvl)
{
mdi_vhcache_pathinfo_t *cpi;
nvlist_t *nvl;
int err;
uint32_t val[2];
ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
if ((err = nvlist_alloc(&nvl, 0, KM_SLEEP)) != 0)
return (err);
for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
val[0] = cpi->cpi_cphci->cphci_id;
val[1] = cpi->cpi_flags;
if ((err = nvlist_add_uint32_array(nvl, cpi->cpi_addr, val, 2))
!= 0)
goto out;
}
err = nvlist_add_nvlist(caddrmapnvl, cct->cct_name_addr, nvl);
out:
nvlist_free(nvl);
return (err);
}
static int
vhcache_to_caddrmapnvl(mdi_vhci_cache_t *vhcache, nvlist_t *mainnvl)
{
mdi_vhcache_client_t *cct;
nvlist_t *nvl;
int err;
ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) != 0)
return (err);
for (cct = vhcache->vhcache_client_head; cct != NULL;
cct = cct->cct_next) {
if ((err = vhcache_to_paddrnvl(vhcache, cct, nvl)) != 0)
goto out;
}
err = nvlist_add_nvlist(mainnvl, MDI_NVPNAME_CTADDRMAP, nvl);
out:
nvlist_free(nvl);
return (err);
}
static nvlist_t *
vhcache_to_mainnvl(mdi_vhci_cache_t *vhcache)
{
mdi_vhcache_phci_t *cphci;
uint_t phci_count;
char **phcis;
nvlist_t *nvl;
int err, i;
if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) != 0) {
nvl = NULL;
goto out;
}
if ((err = nvlist_add_int32(nvl, MDI_NVPNAME_VERSION,
MDI_VHCI_CACHE_VERSION)) != 0)
goto out;
rw_enter(&vhcache->vhcache_lock, RW_READER);
if (vhcache->vhcache_phci_head == NULL) {
rw_exit(&vhcache->vhcache_lock);
return (nvl);
}
phci_count = 0;
for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
cphci = cphci->cphci_next)
cphci->cphci_id = phci_count++;
phcis = kmem_alloc(sizeof (char *) * phci_count, KM_SLEEP);
for (cphci = vhcache->vhcache_phci_head, i = 0; cphci != NULL;
cphci = cphci->cphci_next, i++)
phcis[i] = i_ddi_strdup(cphci->cphci_path, KM_SLEEP);
err = nvlist_add_string_array(nvl, MDI_NVPNAME_PHCIS, phcis,
phci_count);
free_string_array(phcis, phci_count);
if (err == 0 &&
(err = vhcache_to_caddrmapnvl(vhcache, nvl)) == 0) {
rw_exit(&vhcache->vhcache_lock);
return (nvl);
}
rw_exit(&vhcache->vhcache_lock);
out:
nvlist_free(nvl);
return (NULL);
}
static mdi_vhcache_phci_t *
lookup_vhcache_phci_by_name(mdi_vhci_cache_t *vhcache, char *phci_path)
{
mdi_vhcache_phci_t *cphci;
ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
cphci = cphci->cphci_next) {
if (strcmp(cphci->cphci_path, phci_path) == 0)
return (cphci);
}
return (NULL);
}
static mdi_vhcache_phci_t *
lookup_vhcache_phci_by_addr(mdi_vhci_cache_t *vhcache, mdi_phci_t *ph)
{
mdi_vhcache_phci_t *cphci;
ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
cphci = cphci->cphci_next) {
if (cphci->cphci_phci == ph)
return (cphci);
}
return (NULL);
}
static void
vhcache_phci_add(mdi_vhci_config_t *vhc, mdi_phci_t *ph)
{
mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
mdi_vhcache_phci_t *cphci;
char *pathname;
int cache_updated;
rw_enter(&vhcache->vhcache_lock, RW_WRITER);
pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(ph->ph_dip, pathname);
if ((cphci = lookup_vhcache_phci_by_name(vhcache, pathname))
!= NULL) {
cphci->cphci_phci = ph;
cache_updated = 0;
} else {
cphci = kmem_zalloc(sizeof (*cphci), KM_SLEEP);
cphci->cphci_path = i_ddi_strdup(pathname, KM_SLEEP);
cphci->cphci_phci = ph;
enqueue_vhcache_phci(vhcache, cphci);
cache_updated = 1;
}
rw_exit(&vhcache->vhcache_lock);
mutex_enter(&vhc->vhc_lock);
vhc->vhc_path_discovery_cutoff_time = 0;
mutex_exit(&vhc->vhc_lock);
kmem_free(pathname, MAXPATHLEN);
if (cache_updated)
vhcache_dirty(vhc);
}
static void
vhcache_phci_remove(mdi_vhci_config_t *vhc, mdi_phci_t *ph)
{
mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
mdi_vhcache_phci_t *cphci;
rw_enter(&vhcache->vhcache_lock, RW_WRITER);
if ((cphci = lookup_vhcache_phci_by_addr(vhcache, ph)) != NULL) {
cphci->cphci_phci = NULL;
}
rw_exit(&vhcache->vhcache_lock);
}
static void
init_vhcache_lookup_token(mdi_vhcache_lookup_token_t *dst,
mdi_vhcache_lookup_token_t *src)
{
if (src == NULL) {
dst->lt_cct = NULL;
dst->lt_cct_lookup_time = 0;
} else {
dst->lt_cct = src->lt_cct;
dst->lt_cct_lookup_time = src->lt_cct_lookup_time;
}
}
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)
{
mod_hash_val_t hv;
char *name_addr;
int len;
ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
if (token != NULL &&
vhcache->vhcache_clean_time < token->lt_cct_lookup_time)
return (token->lt_cct);
name_addr = vhcache_mknameaddr(ct_name, ct_addr, &len);
if (mod_hash_find(vhcache->vhcache_client_hash,
(mod_hash_key_t)name_addr, &hv) == 0) {
if (token) {
token->lt_cct = (mdi_vhcache_client_t *)hv;
token->lt_cct_lookup_time = ddi_get_lbolt64();
}
} else {
if (token) {
token->lt_cct = NULL;
token->lt_cct_lookup_time = 0;
}
hv = NULL;
}
kmem_free(name_addr, len);
return ((mdi_vhcache_client_t *)hv);
}
static void
vhcache_pi_add(mdi_vhci_config_t *vhc, struct mdi_pathinfo *pip)
{
mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
mdi_vhcache_client_t *cct;
mdi_vhcache_pathinfo_t *cpi;
mdi_phci_t *ph = pip->pi_phci;
mdi_client_t *ct = pip->pi_client;
int cache_updated = 0;
rw_enter(&vhcache->vhcache_lock, RW_WRITER);
if ((cct = lookup_vhcache_client(vhcache, ct->ct_drvname, ct->ct_guid,
NULL)) == NULL) {
cct = kmem_zalloc(sizeof (*cct), KM_SLEEP);
cct->cct_name_addr = vhcache_mknameaddr(ct->ct_drvname,
ct->ct_guid, NULL);
enqueue_vhcache_client(vhcache, cct);
(void) mod_hash_insert(vhcache->vhcache_client_hash,
(mod_hash_key_t)cct->cct_name_addr, (mod_hash_val_t)cct);
cache_updated = 1;
}
for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
if (cpi->cpi_cphci->cphci_phci == ph &&
strcmp(cpi->cpi_addr, pip->pi_addr) == 0) {
cpi->cpi_pip = pip;
if (cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST) {
cpi->cpi_flags &=
~MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
sort_vhcache_paths(cct);
cache_updated = 1;
}
break;
}
}
if (cpi == NULL) {
cpi = kmem_zalloc(sizeof (*cpi), KM_SLEEP);
cpi->cpi_addr = i_ddi_strdup(pip->pi_addr, KM_SLEEP);
cpi->cpi_cphci = lookup_vhcache_phci_by_addr(vhcache, ph);
ASSERT(cpi->cpi_cphci != NULL);
cpi->cpi_pip = pip;
enqueue_vhcache_pathinfo(cct, cpi);
cache_updated = 1;
}
rw_exit(&vhcache->vhcache_lock);
if (cache_updated)
vhcache_dirty(vhc);
}
static void
vhcache_pi_remove(mdi_vhci_config_t *vhc, struct mdi_pathinfo *pip)
{
mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
mdi_client_t *ct = pip->pi_client;
mdi_vhcache_client_t *cct;
mdi_vhcache_pathinfo_t *cpi;
rw_enter(&vhcache->vhcache_lock, RW_WRITER);
if ((cct = lookup_vhcache_client(vhcache, ct->ct_drvname, ct->ct_guid,
NULL)) != NULL) {
for (cpi = cct->cct_cpi_head; cpi != NULL;
cpi = cpi->cpi_next) {
if (cpi->cpi_pip == pip) {
cpi->cpi_pip = NULL;
break;
}
}
}
rw_exit(&vhcache->vhcache_lock);
}
static int
flush_vhcache(mdi_vhci_config_t *vhc, int force_flag)
{
nvlist_t *nvl;
int err;
int rv;
if (force_flag == 0 && !i_ddi_io_initialized())
return (MDI_FAILURE);
if ((nvl = vhcache_to_mainnvl(&vhc->vhc_vhcache)) != NULL) {
err = fwrite_nvlist(vhc->vhc_vhcache_filename, nvl);
nvlist_free(nvl);
} else
err = EFAULT;
rv = MDI_SUCCESS;
mutex_enter(&vhc->vhc_lock);
if (err != 0) {
if (err == EROFS) {
vhc->vhc_flags |= MDI_VHC_READONLY_FS;
vhc->vhc_flags &= ~(MDI_VHC_VHCACHE_FLUSH_ERROR |
MDI_VHC_VHCACHE_DIRTY);
} else {
if (!(vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_ERROR)) {
cmn_err(CE_CONT, "%s: update failed\n",
vhc->vhc_vhcache_filename);
vhc->vhc_flags |= MDI_VHC_VHCACHE_FLUSH_ERROR;
}
rv = MDI_FAILURE;
}
} else if (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_ERROR) {
cmn_err(CE_CONT,
"%s: update now ok\n", vhc->vhc_vhcache_filename);
vhc->vhc_flags &= ~MDI_VHC_VHCACHE_FLUSH_ERROR;
}
mutex_exit(&vhc->vhc_lock);
return (rv);
}
static void
vhcache_flush_thread(void *arg)
{
mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
clock_t idle_time, quit_at_ticks;
callb_cpr_t cprinfo;
idle_time = mdi_vhcache_flush_daemon_idle_time * TICKS_PER_SECOND;
CALLB_CPR_INIT(&cprinfo, &vhc->vhc_lock, callb_generic_cpr,
"mdi_vhcache_flush");
mutex_enter(&vhc->vhc_lock);
for (; ; ) {
while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
(vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY)) {
if (ddi_get_lbolt() < vhc->vhc_flush_at_ticks) {
CALLB_CPR_SAFE_BEGIN(&cprinfo);
(void) cv_timedwait(&vhc->vhc_cv,
&vhc->vhc_lock, vhc->vhc_flush_at_ticks);
CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
} else {
vhc->vhc_flags &= ~MDI_VHC_VHCACHE_DIRTY;
mutex_exit(&vhc->vhc_lock);
if (flush_vhcache(vhc, 0) != MDI_SUCCESS)
vhcache_dirty(vhc);
mutex_enter(&vhc->vhc_lock);
}
}
quit_at_ticks = ddi_get_lbolt() + idle_time;
while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
!(vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY) &&
ddi_get_lbolt() < quit_at_ticks) {
CALLB_CPR_SAFE_BEGIN(&cprinfo);
(void) cv_timedwait(&vhc->vhc_cv, &vhc->vhc_lock,
quit_at_ticks);
CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
}
if ((vhc->vhc_flags & MDI_VHC_EXIT) ||
!(vhc->vhc_flags & MDI_VHC_VHCACHE_DIRTY))
goto out;
}
out:
vhc->vhc_flags &= ~MDI_VHC_VHCACHE_FLUSH_THREAD;
CALLB_CPR_EXIT(&cprinfo);
}
static void
vhcache_dirty(mdi_vhci_config_t *vhc)
{
mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
int create_thread;
rw_enter(&vhcache->vhcache_lock, RW_READER);
if (!(vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE)) {
rw_exit(&vhcache->vhcache_lock);
return;
}
rw_exit(&vhcache->vhcache_lock);
mutex_enter(&vhc->vhc_lock);
if (vhc->vhc_flags & MDI_VHC_READONLY_FS) {
mutex_exit(&vhc->vhc_lock);
return;
}
vhc->vhc_flags |= MDI_VHC_VHCACHE_DIRTY;
vhc->vhc_flush_at_ticks = ddi_get_lbolt() +
mdi_vhcache_flush_delay * TICKS_PER_SECOND;
if (vhc->vhc_flags & MDI_VHC_VHCACHE_FLUSH_THREAD) {
cv_broadcast(&vhc->vhc_cv);
create_thread = 0;
} else {
vhc->vhc_flags |= MDI_VHC_VHCACHE_FLUSH_THREAD;
create_thread = 1;
}
mutex_exit(&vhc->vhc_lock);
if (create_thread)
(void) thread_create(NULL, 0, vhcache_flush_thread, vhc,
0, &p0, TS_RUN, minclsyspri);
}
typedef struct mdi_phci_bus_config_s {
char *phbc_phci_path;
struct mdi_vhci_bus_config_s *phbc_vhbusconfig;
struct mdi_phci_bus_config_s *phbc_next;
} mdi_phci_bus_config_t;
typedef struct mdi_vhci_bus_config_s {
ddi_bus_config_op_t vhbc_op;
major_t vhbc_op_major;
uint_t vhbc_op_flags;
kmutex_t vhbc_lock;
kcondvar_t vhbc_cv;
int vhbc_thr_count;
} mdi_vhci_bus_config_t;
static void
bus_config_phci(void *arg)
{
mdi_phci_bus_config_t *phbc = (mdi_phci_bus_config_t *)arg;
mdi_vhci_bus_config_t *vhbc = phbc->phbc_vhbusconfig;
dev_info_t *ph_dip;
if ((ph_dip = e_ddi_hold_devi_by_path(phbc->phbc_phci_path, 0))
!= NULL) {
if (vhbc->vhbc_op == BUS_CONFIG_DRIVER ||
vhbc->vhbc_op == BUS_UNCONFIG_DRIVER) {
(void) ndi_devi_config_driver(ph_dip,
vhbc->vhbc_op_flags,
vhbc->vhbc_op_major);
} else
(void) ndi_devi_config(ph_dip,
vhbc->vhbc_op_flags);
ndi_rele_devi(ph_dip);
}
kmem_free(phbc->phbc_phci_path, strlen(phbc->phbc_phci_path) + 1);
kmem_free(phbc, sizeof (*phbc));
mutex_enter(&vhbc->vhbc_lock);
vhbc->vhbc_thr_count--;
if (vhbc->vhbc_thr_count == 0)
cv_broadcast(&vhbc->vhbc_cv);
mutex_exit(&vhbc->vhbc_lock);
}
static void
bus_config_all_phcis(mdi_vhci_cache_t *vhcache, uint_t flags,
ddi_bus_config_op_t op, major_t maj)
{
mdi_phci_bus_config_t *phbc_head = NULL, *phbc, *phbc_next;
mdi_vhci_bus_config_t *vhbc;
mdi_vhcache_phci_t *cphci;
rw_enter(&vhcache->vhcache_lock, RW_READER);
if (vhcache->vhcache_phci_head == NULL) {
rw_exit(&vhcache->vhcache_lock);
return;
}
vhbc = kmem_zalloc(sizeof (*vhbc), KM_SLEEP);
for (cphci = vhcache->vhcache_phci_head; cphci != NULL;
cphci = cphci->cphci_next) {
if (!modrootloaded && (cphci->cphci_phci == NULL))
continue;
phbc = kmem_zalloc(sizeof (*phbc), KM_SLEEP);
phbc->phbc_phci_path = i_ddi_strdup(cphci->cphci_path,
KM_SLEEP);
phbc->phbc_vhbusconfig = vhbc;
phbc->phbc_next = phbc_head;
phbc_head = phbc;
vhbc->vhbc_thr_count++;
}
rw_exit(&vhcache->vhcache_lock);
vhbc->vhbc_op = op;
vhbc->vhbc_op_major = maj;
vhbc->vhbc_op_flags = NDI_NO_EVENT |
(flags & (NDI_CONFIG_REPROBE | NDI_DRV_CONF_REPROBE));
mutex_init(&vhbc->vhbc_lock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&vhbc->vhbc_cv, NULL, CV_DRIVER, NULL);
for (phbc = phbc_head; phbc != NULL; phbc = phbc_next) {
phbc_next = phbc->phbc_next;
if (mdi_mtc_off)
bus_config_phci((void *)phbc);
else
(void) thread_create(NULL, 0, bus_config_phci, phbc,
0, &p0, TS_RUN, minclsyspri);
}
mutex_enter(&vhbc->vhbc_lock);
while (vhbc->vhbc_thr_count > 0)
cv_wait(&vhbc->vhbc_cv, &vhbc->vhbc_lock);
mutex_exit(&vhbc->vhbc_lock);
mutex_destroy(&vhbc->vhbc_lock);
cv_destroy(&vhbc->vhbc_cv);
kmem_free(vhbc, sizeof (*vhbc));
}
static void
st_bus_config_all_phcis(mdi_vhci_config_t *vhc, uint_t flags,
ddi_bus_config_op_t op, major_t maj)
{
mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
single_threaded_vhconfig_enter(vhc);
bus_config_all_phcis(vhcache, flags, op, maj);
single_threaded_vhconfig_exit(vhc);
}
static int
bus_config_one_phci_child(char *path)
{
dev_info_t *ph_dip, *child;
char *devnm;
int rv = MDI_FAILURE;
devnm = strrchr(path, '/');
*devnm++ = '\0';
if ((ph_dip = e_ddi_hold_devi_by_path(path, 0)) != NULL) {
if (ndi_devi_config_one(ph_dip, devnm, &child, NDI_NO_EVENT) ==
NDI_SUCCESS) {
ndi_rele_devi(child);
rv = MDI_SUCCESS;
}
ndi_rele_devi(ph_dip);
}
devnm--;
*devnm = '/';
return (rv);
}
static mdi_phys_path_t *
build_phclient_path_list(mdi_vhcache_client_t *cct, char *ct_name)
{
mdi_vhcache_pathinfo_t *cpi;
mdi_phys_path_t *pp_head = NULL, *pp_tail = NULL, *pp;
int config_path, len;
for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
config_path = 0;
if (cpi->cpi_pip == NULL)
config_path = 1;
else {
MDI_PI_LOCK(cpi->cpi_pip);
if (MDI_PI_IS_INIT(cpi->cpi_pip))
config_path = 1;
MDI_PI_UNLOCK(cpi->cpi_pip);
}
if (config_path) {
pp = kmem_alloc(sizeof (*pp), KM_SLEEP);
len = strlen(cpi->cpi_cphci->cphci_path) +
strlen(ct_name) + strlen(cpi->cpi_addr) + 3;
pp->phys_path = kmem_alloc(len, KM_SLEEP);
(void) snprintf(pp->phys_path, len, "%s/%s@%s",
cpi->cpi_cphci->cphci_path, ct_name,
cpi->cpi_addr);
pp->phys_path_next = NULL;
if (pp_head == NULL)
pp_head = pp;
else
pp_tail->phys_path_next = pp;
pp_tail = pp;
}
}
return (pp_head);
}
static void
free_phclient_path_list(mdi_phys_path_t *pp_head)
{
mdi_phys_path_t *pp, *pp_next;
for (pp = pp_head; pp != NULL; pp = pp_next) {
pp_next = pp->phys_path_next;
kmem_free(pp->phys_path, strlen(pp->phys_path) + 1);
kmem_free(pp, sizeof (*pp));
}
}
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)
{
mdi_async_client_config_t *acc;
acc = kmem_alloc(sizeof (*acc), KM_SLEEP);
acc->acc_ct_name = i_ddi_strdup(ct_name, KM_SLEEP);
acc->acc_ct_addr = i_ddi_strdup(ct_addr, KM_SLEEP);
acc->acc_phclient_path_list_head = pp_head;
init_vhcache_lookup_token(&acc->acc_token, tok);
acc->acc_next = NULL;
return (acc);
}
static void
free_async_client_config(mdi_async_client_config_t *acc)
{
if (acc->acc_phclient_path_list_head)
free_phclient_path_list(acc->acc_phclient_path_list_head);
kmem_free(acc->acc_ct_name, strlen(acc->acc_ct_name) + 1);
kmem_free(acc->acc_ct_addr, strlen(acc->acc_ct_addr) + 1);
kmem_free(acc, sizeof (*acc));
}
static void
sort_vhcache_paths(mdi_vhcache_client_t *cct)
{
mdi_vhcache_pathinfo_t *cpi, *cpi_next, *cpi_head;
cpi_head = cct->cct_cpi_head;
cct->cct_cpi_head = cct->cct_cpi_tail = NULL;
for (cpi = cpi_head; cpi != NULL; cpi = cpi_next) {
cpi_next = cpi->cpi_next;
enqueue_vhcache_pathinfo(cct, cpi);
}
}
static void
adjust_sort_vhcache_paths(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr,
mdi_vhcache_lookup_token_t *tok)
{
mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
mdi_vhcache_client_t *cct;
mdi_vhcache_pathinfo_t *cpi;
rw_enter(&vhcache->vhcache_lock, RW_READER);
if ((cct = lookup_vhcache_client(vhcache, ct_name, ct_addr, tok))
== NULL) {
rw_exit(&vhcache->vhcache_lock);
return;
}
for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
if ((cpi->cpi_pip != NULL &&
(cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST)) ||
(cpi->cpi_pip == NULL &&
!(cpi->cpi_flags & MDI_CPI_HINT_PATH_DOES_NOT_EXIST))) {
break;
}
}
if (cpi == NULL) {
rw_exit(&vhcache->vhcache_lock);
return;
}
if (rw_tryupgrade(&vhcache->vhcache_lock) == 0) {
rw_exit(&vhcache->vhcache_lock);
rw_enter(&vhcache->vhcache_lock, RW_WRITER);
if ((cct = lookup_vhcache_client(vhcache, ct_name, ct_addr,
tok)) == NULL) {
rw_exit(&vhcache->vhcache_lock);
return;
}
}
for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
if (cpi->cpi_pip != NULL)
cpi->cpi_flags &= ~MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
else
cpi->cpi_flags |= MDI_CPI_HINT_PATH_DOES_NOT_EXIST;
}
sort_vhcache_paths(cct);
rw_exit(&vhcache->vhcache_lock);
vhcache_dirty(vhc);
}
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)
{
mdi_phys_path_t *pp;
for (pp = pp_head; pp != NULL; pp = pp->phys_path_next)
(void) bus_config_one_phci_child(pp->phys_path);
adjust_sort_vhcache_paths(vhc, ct_name, ct_addr, tok);
}
static void
config_client_paths_thread(void *arg)
{
mdi_vhci_config_t *vhc = (mdi_vhci_config_t *)arg;
mdi_async_client_config_t *acc;
clock_t quit_at_ticks;
clock_t idle_time = mdi_async_config_idle_time * TICKS_PER_SECOND;
callb_cpr_t cprinfo;
CALLB_CPR_INIT(&cprinfo, &vhc->vhc_lock, callb_generic_cpr,
"mdi_config_client_paths");
for (; ; ) {
quit_at_ticks = ddi_get_lbolt() + idle_time;
mutex_enter(&vhc->vhc_lock);
while (!(vhc->vhc_flags & MDI_VHC_EXIT) &&
vhc->vhc_acc_list_head == NULL &&
ddi_get_lbolt() < quit_at_ticks) {
CALLB_CPR_SAFE_BEGIN(&cprinfo);
(void) cv_timedwait(&vhc->vhc_cv, &vhc->vhc_lock,
quit_at_ticks);
CALLB_CPR_SAFE_END(&cprinfo, &vhc->vhc_lock);
}
if ((vhc->vhc_flags & MDI_VHC_EXIT) ||
vhc->vhc_acc_list_head == NULL)
goto out;
acc = vhc->vhc_acc_list_head;
vhc->vhc_acc_list_head = acc->acc_next;
if (vhc->vhc_acc_list_head == NULL)
vhc->vhc_acc_list_tail = NULL;
vhc->vhc_acc_count--;
mutex_exit(&vhc->vhc_lock);
config_client_paths_sync(vhc, acc->acc_ct_name,
acc->acc_ct_addr, acc->acc_phclient_path_list_head,
&acc->acc_token);
free_async_client_config(acc);
}
out:
vhc->vhc_acc_thrcount--;
CALLB_CPR_EXIT(&cprinfo);
}
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)
{
mdi_async_client_config_t *acc, *newacc;
int create_thread;
if (pp_head == NULL)
return;
if (mdi_mtc_off) {
config_client_paths_sync(vhc, ct_name, ct_addr, pp_head, tok);
free_phclient_path_list(pp_head);
return;
}
newacc = alloc_async_client_config(ct_name, ct_addr, pp_head, tok);
ASSERT(newacc);
mutex_enter(&vhc->vhc_lock);
for (acc = vhc->vhc_acc_list_head; acc != NULL; acc = acc->acc_next) {
if (strcmp(ct_name, acc->acc_ct_name) == 0 &&
strcmp(ct_addr, acc->acc_ct_addr) == 0) {
free_async_client_config(newacc);
mutex_exit(&vhc->vhc_lock);
return;
}
}
if (vhc->vhc_acc_list_head == NULL)
vhc->vhc_acc_list_head = newacc;
else
vhc->vhc_acc_list_tail->acc_next = newacc;
vhc->vhc_acc_list_tail = newacc;
vhc->vhc_acc_count++;
if (vhc->vhc_acc_count <= vhc->vhc_acc_thrcount) {
cv_broadcast(&vhc->vhc_cv);
create_thread = 0;
} else {
vhc->vhc_acc_thrcount++;
create_thread = 1;
}
mutex_exit(&vhc->vhc_lock);
if (create_thread)
(void) thread_create(NULL, 0, config_client_paths_thread, vhc,
0, &p0, TS_RUN, minclsyspri);
}
static int
nonline_paths(mdi_vhcache_client_t *cct)
{
mdi_vhcache_pathinfo_t *cpi;
int online_count = 0;
for (cpi = cct->cct_cpi_head; cpi != NULL; cpi = cpi->cpi_next) {
if (cpi->cpi_pip != NULL) {
MDI_PI_LOCK(cpi->cpi_pip);
if (cpi->cpi_pip->pi_state == MDI_PATHINFO_STATE_ONLINE)
online_count++;
MDI_PI_UNLOCK(cpi->cpi_pip);
}
}
return (online_count);
}
static void
config_client_paths(mdi_vhci_config_t *vhc, char *ct_name, char *ct_addr)
{
mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
mdi_phys_path_t *pp_head, *pp;
mdi_vhcache_client_t *cct;
mdi_vhcache_lookup_token_t tok;
ASSERT(RW_LOCK_HELD(&vhcache->vhcache_lock));
init_vhcache_lookup_token(&tok, NULL);
if (ct_name == NULL || ct_addr == NULL ||
(cct = lookup_vhcache_client(vhcache, ct_name, ct_addr, &tok))
== NULL ||
(pp_head = build_phclient_path_list(cct, ct_name)) == NULL) {
rw_exit(&vhcache->vhcache_lock);
return;
}
if (nonline_paths(cct) > 0) {
rw_exit(&vhcache->vhcache_lock);
config_client_paths_async(vhc, ct_name, ct_addr, pp_head, &tok);
return;
}
rw_exit(&vhcache->vhcache_lock);
for (pp = pp_head; pp != NULL; pp = pp->phys_path_next) {
if (bus_config_one_phci_child(pp->phys_path) == MDI_SUCCESS) {
rw_enter(&vhcache->vhcache_lock, RW_READER);
if ((cct = lookup_vhcache_client(vhcache, ct_name,
ct_addr, &tok)) == NULL) {
rw_exit(&vhcache->vhcache_lock);
goto out;
}
if (nonline_paths(cct) > 0 &&
pp->phys_path_next != NULL) {
rw_exit(&vhcache->vhcache_lock);
config_client_paths_async(vhc, ct_name, ct_addr,
pp->phys_path_next, &tok);
pp->phys_path_next = NULL;
goto out;
}
rw_exit(&vhcache->vhcache_lock);
}
}
adjust_sort_vhcache_paths(vhc, ct_name, ct_addr, &tok);
out:
free_phclient_path_list(pp_head);
}
static void
single_threaded_vhconfig_enter(mdi_vhci_config_t *vhc)
{
mutex_enter(&vhc->vhc_lock);
while (vhc->vhc_flags & MDI_VHC_SINGLE_THREADED)
cv_wait(&vhc->vhc_cv, &vhc->vhc_lock);
vhc->vhc_flags |= MDI_VHC_SINGLE_THREADED;
mutex_exit(&vhc->vhc_lock);
}
static void
single_threaded_vhconfig_exit(mdi_vhci_config_t *vhc)
{
mutex_enter(&vhc->vhc_lock);
vhc->vhc_flags &= ~MDI_VHC_SINGLE_THREADED;
cv_broadcast(&vhc->vhc_cv);
mutex_exit(&vhc->vhc_lock);
}
typedef struct mdi_phci_driver_info {
char *phdriver_name;
int phdriver_root_support;
} mdi_phci_driver_info_t;
static mdi_phci_driver_info_t scsi_phci_driver_list[] = {
{ "fp", 1 },
{ "iscsi", 0 },
{ "ibsrp", 1 }
};
static mdi_phci_driver_info_t ib_phci_driver_list[] = { "tavor", 1 };
static void *
mdi_realloc(void *old_ptr, size_t old_size, size_t new_size)
{
void *new_ptr;
new_ptr = kmem_zalloc(new_size, KM_SLEEP);
if (old_ptr) {
bcopy(old_ptr, new_ptr, MIN(old_size, new_size));
kmem_free(old_ptr, old_size);
}
return (new_ptr);
}
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)
{
ASSERT(*cur_elements <= *max_elements);
if (*cur_elements == *max_elements) {
*max_elements += 10;
*driver_list = mdi_realloc(*driver_list,
sizeof (char *) * (*cur_elements),
sizeof (char *) * (*max_elements));
*root_support_list = mdi_realloc(*root_support_list,
sizeof (int) * (*cur_elements),
sizeof (int) * (*max_elements));
}
(*driver_list)[*cur_elements] = i_ddi_strdup(driver_name, KM_SLEEP);
(*root_support_list)[*cur_elements] = root_support;
(*cur_elements)++;
}
static void
get_phci_driver_list(char *vhci_class, char ***driver_list,
int **root_support_list, int *cur_elements, int *max_elements)
{
mdi_phci_driver_info_t *st_driver_list, *p;
int st_ndrivers, root_support, i, j, driver_conf_count;
major_t m;
struct devnames *dnp;
ddi_prop_t *propp;
*driver_list = NULL;
*root_support_list = NULL;
*cur_elements = 0;
*max_elements = 0;
for (m = 0; m < devcnt; m++) {
dnp = &devnamesp[m];
if (dnp->dn_flags & DN_PHCI_DRIVER) {
LOCK_DEV_OPS(&dnp->dn_lock);
if (dnp->dn_global_prop_ptr != NULL &&
(propp = i_ddi_prop_search(DDI_DEV_T_ANY,
DDI_VHCI_CLASS, DDI_PROP_TYPE_STRING,
&dnp->dn_global_prop_ptr->prop_list)) != NULL &&
strcmp(propp->prop_val, vhci_class) == 0) {
root_support = (i_ddi_prop_search(DDI_DEV_T_ANY,
DDI_NO_ROOT_SUPPORT, DDI_PROP_TYPE_INT,
&dnp->dn_global_prop_ptr->prop_list)
== NULL) ? 1 : 0;
add_to_phci_list(driver_list, root_support_list,
cur_elements, max_elements, dnp->dn_name,
root_support);
UNLOCK_DEV_OPS(&dnp->dn_lock);
} else
UNLOCK_DEV_OPS(&dnp->dn_lock);
}
}
driver_conf_count = *cur_elements;
if (strcmp(vhci_class, MDI_HCI_CLASS_SCSI) == 0) {
st_driver_list = scsi_phci_driver_list;
st_ndrivers = sizeof (scsi_phci_driver_list) /
sizeof (mdi_phci_driver_info_t);
} else if (strcmp(vhci_class, MDI_HCI_CLASS_IB) == 0) {
st_driver_list = ib_phci_driver_list;
st_ndrivers = sizeof (ib_phci_driver_list) /
sizeof (mdi_phci_driver_info_t);
} else {
st_driver_list = NULL;
st_ndrivers = 0;
}
for (i = 0, p = st_driver_list; i < st_ndrivers; i++, p++) {
for (j = 0; j < driver_conf_count; j++) {
if (strcmp((*driver_list)[j], p->phdriver_name) == 0)
break;
}
if (j == driver_conf_count) {
add_to_phci_list(driver_list, root_support_list,
cur_elements, max_elements, p->phdriver_name,
p->phdriver_root_support);
}
}
}
static void
attach_phci_drivers(char *vhci_class)
{
char **driver_list, **p;
int *root_support_list;
int cur_elements, max_elements, i;
major_t m;
get_phci_driver_list(vhci_class, &driver_list, &root_support_list,
&cur_elements, &max_elements);
for (i = 0; i < cur_elements; i++) {
if (modrootloaded || root_support_list[i]) {
m = ddi_name_to_major(driver_list[i]);
if (m != DDI_MAJOR_T_NONE &&
ddi_hold_installed_driver(m))
ddi_rele_driver(m);
}
}
if (driver_list) {
for (i = 0, p = driver_list; i < cur_elements; i++, p++)
kmem_free(*p, strlen(*p) + 1);
kmem_free(driver_list, sizeof (char *) * max_elements);
kmem_free(root_support_list, sizeof (int) * max_elements);
}
}
static int
build_vhci_cache(mdi_vhci_t *vh)
{
mdi_vhci_config_t *vhc = vh->vh_config;
mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
single_threaded_vhconfig_enter(vhc);
rw_enter(&vhcache->vhcache_lock, RW_READER);
if (vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE) {
rw_exit(&vhcache->vhcache_lock);
single_threaded_vhconfig_exit(vhc);
return (0);
}
rw_exit(&vhcache->vhcache_lock);
attach_phci_drivers(vh->vh_class);
bus_config_all_phcis(vhcache, NDI_DRV_CONF_REPROBE | NDI_NO_EVENT,
BUS_CONFIG_ALL, DDI_MAJOR_T_NONE);
rw_enter(&vhcache->vhcache_lock, RW_WRITER);
vhcache->vhcache_flags |= MDI_VHCI_CACHE_SETUP_DONE;
rw_exit(&vhcache->vhcache_lock);
single_threaded_vhconfig_exit(vhc);
vhcache_dirty(vhc);
return (1);
}
static int
vhcache_do_discovery(mdi_vhci_config_t *vhc)
{
int rv = 1;
mutex_enter(&vhc->vhc_lock);
if (i_ddi_io_initialized() == 0) {
if (vhc->vhc_path_discovery_boot > 0) {
vhc->vhc_path_discovery_boot--;
goto out;
}
} else {
if (vhc->vhc_path_discovery_postboot > 0) {
vhc->vhc_path_discovery_postboot--;
goto out;
}
}
if (mdi_path_discovery_interval != -1 &&
ddi_get_lbolt64() >= vhc->vhc_path_discovery_cutoff_time)
goto out;
rv = 0;
out:
mutex_exit(&vhc->vhc_lock);
return (rv);
}
static int
vhcache_discover_paths(mdi_vhci_t *vh)
{
mdi_vhci_config_t *vhc = vh->vh_config;
mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
int rv = 0;
single_threaded_vhconfig_enter(vhc);
if (vhcache_do_discovery(vhc)) {
attach_phci_drivers(vh->vh_class);
bus_config_all_phcis(vhcache, NDI_DRV_CONF_REPROBE |
NDI_NO_EVENT, BUS_CONFIG_ALL, DDI_MAJOR_T_NONE);
mutex_enter(&vhc->vhc_lock);
vhc->vhc_path_discovery_cutoff_time = ddi_get_lbolt64() +
mdi_path_discovery_interval * TICKS_PER_SECOND;
mutex_exit(&vhc->vhc_lock);
rv = 1;
}
single_threaded_vhconfig_exit(vhc);
return (rv);
}
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)
{
mdi_vhci_t *vh = i_devi_get_vhci(vdip);
mdi_vhci_config_t *vhc = vh->vh_config;
mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
int rv = 0;
int params_valid = 0;
char *cp;
if (DEVI_BUSY_OWNED(vdip)) {
MDI_DEBUG(2, (MDI_NOTE, vdip,
"vhci dip is busy owned %p", (void *)vdip));
goto default_bus_config;
}
rw_enter(&vhcache->vhcache_lock, RW_READER);
if (!(vhcache->vhcache_flags & MDI_VHCI_CACHE_SETUP_DONE)) {
rw_exit(&vhcache->vhcache_lock);
rv = build_vhci_cache(vh);
rw_enter(&vhcache->vhcache_lock, RW_READER);
}
switch (op) {
case BUS_CONFIG_ONE:
if (arg != NULL && ct_addr != NULL) {
cp = (char *)arg;
while (*cp != '\0' && *cp != '@')
cp++;
if (*cp == '@') {
params_valid = 1;
*cp = '\0';
config_client_paths(vhc, (char *)arg, ct_addr);
*cp = '@';
break;
}
}
rw_exit(&vhcache->vhcache_lock);
break;
case BUS_CONFIG_DRIVER:
rw_exit(&vhcache->vhcache_lock);
if (rv == 0)
st_bus_config_all_phcis(vhc, flags, op,
(major_t)(uintptr_t)arg);
break;
case BUS_CONFIG_ALL:
rw_exit(&vhcache->vhcache_lock);
if (rv == 0)
st_bus_config_all_phcis(vhc, flags, op, -1);
break;
default:
rw_exit(&vhcache->vhcache_lock);
break;
}
default_bus_config:
if (ndi_busop_bus_config(vdip, flags, op, arg, child, 0) ==
NDI_SUCCESS) {
return (MDI_SUCCESS);
} else if (op == BUS_CONFIG_ONE && rv == 0 && params_valid) {
if (vhcache_discover_paths(vh) &&
ndi_busop_bus_config(vdip, flags, op, arg, child, 0) ==
NDI_SUCCESS)
return (MDI_SUCCESS);
}
return (MDI_FAILURE);
}
static nvlist_t *
read_on_disk_vhci_cache(char *vhci_class)
{
nvlist_t *nvl;
int err;
char *filename;
filename = vhclass2vhcache_filename(vhci_class);
if ((err = fread_nvlist(filename, &nvl)) == 0) {
kmem_free(filename, strlen(filename) + 1);
return (nvl);
} else if (err == EIO)
cmn_err(CE_WARN, "%s: I/O error, will recreate", filename);
else if (err == EINVAL)
cmn_err(CE_WARN,
"%s: data file corrupted, will recreate", filename);
kmem_free(filename, strlen(filename) + 1);
return (NULL);
}
void
mdi_read_devices_files(void)
{
int i;
for (i = 0; i < N_VHCI_CLASSES; i++)
vhcache_nvl[i] = read_on_disk_vhci_cache(vhci_class_list[i]);
}
static void
clean_vhcache(mdi_vhci_config_t *vhc)
{
mdi_vhci_cache_t *vhcache = &vhc->vhc_vhcache;
mdi_vhcache_phci_t *phci, *nxt_phci;
mdi_vhcache_client_t *client, *nxt_client;
mdi_vhcache_pathinfo_t *path, *nxt_path;
rw_enter(&vhcache->vhcache_lock, RW_WRITER);
client = vhcache->vhcache_client_head;
vhcache->vhcache_client_head = vhcache->vhcache_client_tail = NULL;
for ( ; client != NULL; client = nxt_client) {
nxt_client = client->cct_next;
path = client->cct_cpi_head;
client->cct_cpi_head = client->cct_cpi_tail = NULL;
for ( ; path != NULL; path = nxt_path) {
nxt_path = path->cpi_next;
if ((path->cpi_cphci->cphci_phci != NULL) &&
(path->cpi_pip != NULL)) {
enqueue_tail_vhcache_pathinfo(client, path);
} else if (path->cpi_pip != NULL) {
free_vhcache_pathinfo(path);
}
}
if (client->cct_cpi_head != NULL)
enqueue_vhcache_client(vhcache, client);
else {
(void) mod_hash_destroy(vhcache->vhcache_client_hash,
(mod_hash_key_t)client->cct_name_addr);
free_vhcache_client(client);
}
}
phci = vhcache->vhcache_phci_head;
vhcache->vhcache_phci_head = vhcache->vhcache_phci_tail = NULL;
for ( ; phci != NULL; phci = nxt_phci) {
nxt_phci = phci->cphci_next;
if (phci->cphci_phci != NULL)
enqueue_vhcache_phci(vhcache, phci);
else
free_vhcache_phci(phci);
}
vhcache->vhcache_clean_time = ddi_get_lbolt64();
rw_exit(&vhcache->vhcache_lock);
vhcache_dirty(vhc);
}
void
mdi_clean_vhcache(void)
{
mdi_vhci_t *vh;
mutex_enter(&mdi_mutex);
for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
vh->vh_refcnt++;
mutex_exit(&mdi_mutex);
clean_vhcache(vh->vh_config);
mutex_enter(&mdi_mutex);
vh->vh_refcnt--;
}
mutex_exit(&mdi_mutex);
}
void
mdi_vhci_walk_clients(dev_info_t *vdip,
int (*f)(dev_info_t *, void *), void *arg)
{
mdi_vhci_t *vh = i_devi_get_vhci(vdip);
dev_info_t *cdip;
mdi_client_t *ct;
MDI_VHCI_CLIENT_LOCK(vh);
cdip = ddi_get_child(vdip);
while (cdip) {
ct = i_devi_get_client(cdip);
MDI_CLIENT_LOCK(ct);
if (((*f)(cdip, arg)) == DDI_WALK_CONTINUE)
cdip = ddi_get_next_sibling(cdip);
else
cdip = NULL;
MDI_CLIENT_UNLOCK(ct);
}
MDI_VHCI_CLIENT_UNLOCK(vh);
}
void
mdi_vhci_walk_phcis(dev_info_t *vdip,
int (*f)(dev_info_t *, void *), void *arg)
{
mdi_vhci_t *vh = i_devi_get_vhci(vdip);
mdi_phci_t *ph, *next;
MDI_VHCI_PHCI_LOCK(vh);
ph = vh->vh_phci_head;
while (ph) {
MDI_PHCI_LOCK(ph);
if (((*f)(ph->ph_dip, arg)) == DDI_WALK_CONTINUE)
next = ph->ph_next;
else
next = NULL;
MDI_PHCI_UNLOCK(ph);
ph = next;
}
MDI_VHCI_PHCI_UNLOCK(vh);
}
void
mdi_walk_vhcis(int (*f)(dev_info_t *, void *), void *arg)
{
mdi_vhci_t *vh = NULL;
mutex_enter(&mdi_mutex);
for (vh = mdi_vhci_head; vh != NULL; vh = vh->vh_next) {
vh->vh_refcnt++;
mutex_exit(&mdi_mutex);
if (((*f)(vh->vh_dip, arg)) != DDI_WALK_CONTINUE) {
mutex_enter(&mdi_mutex);
vh->vh_refcnt--;
break;
} else {
mutex_enter(&mdi_mutex);
vh->vh_refcnt--;
}
}
mutex_exit(&mdi_mutex);
}
static void
i_mdi_log_sysevent(dev_info_t *dip, char *ph_vh_class, char *subclass)
{
char *path_name;
nvlist_t *attr_list;
if (nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE,
KM_SLEEP) != DDI_SUCCESS) {
goto alloc_failed;
}
path_name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(dip, path_name);
if (nvlist_add_string(attr_list, DDI_DRIVER_NAME,
ddi_driver_name(dip)) != DDI_SUCCESS) {
goto error;
}
if (nvlist_add_int32(attr_list, DDI_DRIVER_MAJOR,
(int32_t)ddi_driver_major(dip)) != DDI_SUCCESS) {
goto error;
}
if (nvlist_add_int32(attr_list, DDI_INSTANCE,
(int32_t)ddi_get_instance(dip)) != DDI_SUCCESS) {
goto error;
}
if (nvlist_add_string(attr_list, DDI_PATHNAME,
path_name) != DDI_SUCCESS) {
goto error;
}
if (nvlist_add_string(attr_list, DDI_CLASS,
ph_vh_class) != DDI_SUCCESS) {
goto error;
}
(void) ddi_log_sysevent(dip, DDI_VENDOR_SUNW, EC_DDI, subclass,
attr_list, NULL, DDI_SLEEP);
error:
kmem_free(path_name, MAXPATHLEN);
nvlist_free(attr_list);
return;
alloc_failed:
MDI_DEBUG(1, (MDI_WARN, dip, "!unable to send sysevent"));
}
char **
mdi_get_phci_driver_list(char *vhci_class, int *ndrivers)
{
char **driver_list, **ret_driver_list = NULL;
int *root_support_list;
int cur_elements, max_elements;
get_phci_driver_list(vhci_class, &driver_list, &root_support_list,
&cur_elements, &max_elements);
if (driver_list) {
kmem_free(root_support_list, sizeof (int) * max_elements);
ret_driver_list = mdi_realloc(driver_list, sizeof (char *)
* max_elements, sizeof (char *) * cur_elements);
}
*ndrivers = cur_elements;
return (ret_driver_list);
}
void
mdi_free_phci_driver_list(char **driver_list, int ndrivers)
{
char **p;
int i;
if (driver_list) {
for (i = 0, p = driver_list; i < ndrivers; i++, p++)
kmem_free(*p, strlen(*p) + 1);
kmem_free(driver_list, sizeof (char *) * ndrivers);
}
}
int
mdi_is_dev_supported(char *class, dev_info_t *pdip, void *cinfo)
{
mdi_vhci_t *vh;
ASSERT(class && pdip);
if (!MDI_PHCI(pdip))
return (MDI_FAILURE);
vh = (mdi_vhci_t *)i_mdi_vhci_class2vhci(class);
if ((vh == NULL) || (vh->vh_ops->vo_is_dev_supported == NULL)) {
return (MDI_FAILURE);
}
return (vh->vh_ops->vo_is_dev_supported(vh->vh_dip, pdip, cinfo));
}
int
mdi_dc_return_dev_state(mdi_pathinfo_t *pip, struct devctl_iocdata *dcp)
{
uint_t devstate = 0;
dev_info_t *cdip;
if ((pip == NULL) || (dcp == NULL))
return (MDI_FAILURE);
cdip = mdi_pi_get_client(pip);
switch (mdi_pi_get_state(pip)) {
case MDI_PATHINFO_STATE_INIT:
devstate = DEVICE_DOWN;
break;
case MDI_PATHINFO_STATE_ONLINE:
devstate = DEVICE_ONLINE;
if ((cdip) && (devi_stillreferenced(cdip) == DEVI_REFERENCED))
devstate |= DEVICE_BUSY;
break;
case MDI_PATHINFO_STATE_STANDBY:
devstate = DEVICE_ONLINE;
break;
case MDI_PATHINFO_STATE_FAULT:
devstate = DEVICE_DOWN;
break;
case MDI_PATHINFO_STATE_OFFLINE:
devstate = DEVICE_OFFLINE;
break;
default:
ASSERT(MDI_PI(pip)->pi_state);
}
if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
return (MDI_FAILURE);
return (MDI_SUCCESS);
}