14c06356bSdh142964 /* 24c06356bSdh142964 * CDDL HEADER START 34c06356bSdh142964 * 44c06356bSdh142964 * The contents of this file are subject to the terms of the 54c06356bSdh142964 * Common Development and Distribution License (the "License"). 64c06356bSdh142964 * You may not use this file except in compliance with the License. 74c06356bSdh142964 * 84c06356bSdh142964 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 94c06356bSdh142964 * or http://www.opensolaris.org/os/licensing. 104c06356bSdh142964 * See the License for the specific language governing permissions 114c06356bSdh142964 * and limitations under the License. 124c06356bSdh142964 * 134c06356bSdh142964 * When distributing Covered Code, include this CDDL HEADER in each 144c06356bSdh142964 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 154c06356bSdh142964 * If applicable, add the following below this CDDL HEADER, with the 164c06356bSdh142964 * fields enclosed by brackets "[]" replaced with your own identifying 174c06356bSdh142964 * information: Portions Copyright [yyyy] [name of copyright owner] 184c06356bSdh142964 * 194c06356bSdh142964 * CDDL HEADER END 204c06356bSdh142964 */ 214c06356bSdh142964 224c06356bSdh142964 /* 23*9aed1621SDavid Hollister * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 244c06356bSdh142964 * Use is subject to license terms. 254c06356bSdh142964 */ 264c06356bSdh142964 274c06356bSdh142964 #include <sys/note.h> 284c06356bSdh142964 #include <sys/types.h> 294c06356bSdh142964 #include <sys/param.h> 304c06356bSdh142964 #include <sys/systm.h> 314c06356bSdh142964 #include <sys/buf.h> 324c06356bSdh142964 #include <sys/kmem.h> 334c06356bSdh142964 #include <sys/cmn_err.h> 344c06356bSdh142964 #include <sys/debug.h> 354c06356bSdh142964 #include <sys/sunndi.h> 364c06356bSdh142964 #include <sys/kstat.h> 374c06356bSdh142964 #include <sys/conf.h> 384c06356bSdh142964 #include <sys/ddi_timer.h> 394c06356bSdh142964 #include <sys/devctl.h> 404c06356bSdh142964 #include <sys/callb.h> 414c06356bSdh142964 #include <sys/sysevent.h> 424c06356bSdh142964 #include <sys/taskq.h> 434c06356bSdh142964 #include <sys/ddi.h> 444c06356bSdh142964 #include <sys/bitset.h> 454c06356bSdh142964 #include <sys/damap.h> 464c06356bSdh142964 #include <sys/damap_impl.h> 474c06356bSdh142964 484c06356bSdh142964 #ifdef DEBUG 494c06356bSdh142964 static int damap_debug = 0; 504c06356bSdh142964 #endif /* DEBUG */ 514c06356bSdh142964 521b115575SJohn Danielson extern taskq_t *system_taskq; 531b115575SJohn Danielson 544c06356bSdh142964 static void dam_addrset_activate(dam_t *, bitset_t *); 551b115575SJohn Danielson static void dam_addrset_deactivate(dam_t *, bitset_t *); 561b115575SJohn Danielson static void dam_stabilize_map(void *); 574c06356bSdh142964 static void dam_addr_stable_cb(void *); 581b115575SJohn Danielson static void dam_addrset_stable_cb(void *); 594c06356bSdh142964 static void dam_sched_tmo(dam_t *, clock_t, void (*tmo_cb)()); 601b115575SJohn Danielson static void dam_addr_report(dam_t *, dam_da_t *, id_t, int); 611b115575SJohn Danielson static void dam_addr_release(dam_t *, id_t); 621b115575SJohn Danielson static void dam_addr_report_release(dam_t *, id_t); 631b115575SJohn Danielson static void dam_addr_deactivate(dam_t *, id_t); 64d189c170SReed static void dam_deact_cleanup(dam_t *, id_t, char *, damap_deact_rsn_t); 654c06356bSdh142964 static id_t dam_get_addrid(dam_t *, char *); 664c06356bSdh142964 static int dam_kstat_create(dam_t *); 671b115575SJohn Danielson static int dam_map_alloc(dam_t *); 684c06356bSdh142964 694c06356bSdh142964 #define DAM_INCR_STAT(mapp, stat) \ 704c06356bSdh142964 if ((mapp)->dam_kstatsp) { \ 714c06356bSdh142964 struct dam_kstats *stp = (mapp)->dam_kstatsp->ks_data; \ 724c06356bSdh142964 stp->stat.value.ui32++; \ 734c06356bSdh142964 } 744c06356bSdh142964 754c06356bSdh142964 #define DAM_SET_STAT(mapp, stat, val) \ 764c06356bSdh142964 if ((mapp)->dam_kstatsp) { \ 774c06356bSdh142964 struct dam_kstats *stp = (mapp)->dam_kstatsp->ks_data; \ 784c06356bSdh142964 stp->stat.value.ui32 = (val); \ 794c06356bSdh142964 } 804c06356bSdh142964 811b115575SJohn Danielson 821b115575SJohn Danielson /* 831b115575SJohn Danielson * increase damap size by 64 entries at a time 841b115575SJohn Danielson */ 851b115575SJohn Danielson #define DAM_SIZE_BUMP 64 861b115575SJohn Danielson 871b115575SJohn Danielson /* 881b115575SJohn Danielson * config/unconfig taskq data 891b115575SJohn Danielson */ 901b115575SJohn Danielson typedef struct { 911b115575SJohn Danielson dam_t *tqd_mapp; 921b115575SJohn Danielson id_t tqd_id; 931b115575SJohn Danielson } cfg_tqd_t; 941b115575SJohn Danielson 951b115575SJohn Danielson extern pri_t maxclsyspri; 961b115575SJohn Danielson 974c06356bSdh142964 /* 984c06356bSdh142964 * Create new device address map 994c06356bSdh142964 * 1001b115575SJohn Danielson * name: map name (kstat unique) 1014c06356bSdh142964 * size: max # of map entries 1021b115575SJohn Danielson * mode: style of address reports: per-address or fullset 1034c06356bSdh142964 * stable_usec: # of quiescent microseconds before report/map is stable 1044c06356bSdh142964 * 1054c06356bSdh142964 * activate_arg: address provider activation-callout private 1064c06356bSdh142964 * activate_cb: address provider activation callback handler 1074c06356bSdh142964 * deactivate_cb: address provider deactivation callback handler 1084c06356bSdh142964 * 1094c06356bSdh142964 * config_arg: configuration-callout private 1104c06356bSdh142964 * config_cb: class configuration callout 1114c06356bSdh142964 * unconfig_cb: class unconfiguration callout 1124c06356bSdh142964 * 1134c06356bSdh142964 * damapp: pointer to map handle (return) 1144c06356bSdh142964 * 1154c06356bSdh142964 * Returns: DAM_SUCCESS 1164c06356bSdh142964 * DAM_EINVAL Invalid argument(s) 1174c06356bSdh142964 * DAM_FAILURE General failure 1184c06356bSdh142964 */ 1194c06356bSdh142964 int 1201b115575SJohn Danielson damap_create(char *name, damap_rptmode_t mode, int map_opts, 1211b115575SJohn Danielson clock_t stable_usec, void *activate_arg, damap_activate_cb_t activate_cb, 1224c06356bSdh142964 damap_deactivate_cb_t deactivate_cb, 1234c06356bSdh142964 void *config_arg, damap_configure_cb_t configure_cb, 1244c06356bSdh142964 damap_unconfig_cb_t unconfig_cb, 1254c06356bSdh142964 damap_t **damapp) 1264c06356bSdh142964 { 1274c06356bSdh142964 dam_t *mapp; 1284c06356bSdh142964 1291b115575SJohn Danielson if (configure_cb == NULL || unconfig_cb == NULL || name == NULL) 1304c06356bSdh142964 return (DAM_EINVAL); 1314c06356bSdh142964 1321b115575SJohn Danielson DTRACE_PROBE3(damap__create, char *, name, 1331b115575SJohn Danielson damap_rptmode_t, mode, clock_t, stable_usec); 1344c06356bSdh142964 1354c06356bSdh142964 mapp = kmem_zalloc(sizeof (*mapp), KM_SLEEP); 1361b115575SJohn Danielson mapp->dam_options = map_opts; 1374c06356bSdh142964 mapp->dam_stabletmo = drv_usectohz(stable_usec); 1381b115575SJohn Danielson mapp->dam_size = 0; 1391b115575SJohn Danielson mapp->dam_rptmode = mode; 1404c06356bSdh142964 mapp->dam_activate_arg = activate_arg; 1414c06356bSdh142964 mapp->dam_activate_cb = (activate_cb_t)activate_cb; 1424c06356bSdh142964 mapp->dam_deactivate_cb = (deactivate_cb_t)deactivate_cb; 1434c06356bSdh142964 mapp->dam_config_arg = config_arg; 1444c06356bSdh142964 mapp->dam_configure_cb = (configure_cb_t)configure_cb; 1454c06356bSdh142964 mapp->dam_unconfig_cb = (unconfig_cb_t)unconfig_cb; 1461b115575SJohn Danielson mapp->dam_name = i_ddi_strdup(name, KM_SLEEP); 1474c06356bSdh142964 mutex_init(&mapp->dam_lock, NULL, MUTEX_DRIVER, NULL); 1484c06356bSdh142964 cv_init(&mapp->dam_cv, NULL, CV_DRIVER, NULL); 1491b115575SJohn Danielson bitset_init(&mapp->dam_active_set); 1501b115575SJohn Danielson bitset_init(&mapp->dam_stable_set); 1511b115575SJohn Danielson bitset_init(&mapp->dam_report_set); 1524c06356bSdh142964 *damapp = (damap_t *)mapp; 1534c06356bSdh142964 return (DAM_SUCCESS); 1544c06356bSdh142964 } 1554c06356bSdh142964 1564c06356bSdh142964 /* 1571b115575SJohn Danielson * Allocate backing resources 1581b115575SJohn Danielson * 1591b115575SJohn Danielson * DAMs are lightly backed on create - major allocations occur 1601b115575SJohn Danielson * at the time a report is made to the map, and are extended on 1611b115575SJohn Danielson * a demand basis. 1621b115575SJohn Danielson */ 1631b115575SJohn Danielson static int 1641b115575SJohn Danielson dam_map_alloc(dam_t *mapp) 1651b115575SJohn Danielson { 1661b115575SJohn Danielson void *softstate_p; 1671b115575SJohn Danielson 1681b115575SJohn Danielson ASSERT(mutex_owned(&mapp->dam_lock)); 1691b115575SJohn Danielson if (mapp->dam_flags & DAM_DESTROYPEND) 1701b115575SJohn Danielson return (DAM_FAILURE); 1711b115575SJohn Danielson 1721b115575SJohn Danielson /* 1731b115575SJohn Danielson * dam_high > 0 signals map allocation complete 1741b115575SJohn Danielson */ 1751b115575SJohn Danielson if (mapp->dam_high) 1761b115575SJohn Danielson return (DAM_SUCCESS); 1771b115575SJohn Danielson 1781b115575SJohn Danielson mapp->dam_size = DAM_SIZE_BUMP; 1791b115575SJohn Danielson if (ddi_soft_state_init(&softstate_p, sizeof (dam_da_t), 1801b115575SJohn Danielson mapp->dam_size) != DDI_SUCCESS) 1811b115575SJohn Danielson return (DAM_FAILURE); 1821b115575SJohn Danielson 1831b115575SJohn Danielson if (ddi_strid_init(&mapp->dam_addr_hash, mapp->dam_size) != 1841b115575SJohn Danielson DDI_SUCCESS) { 1851b115575SJohn Danielson ddi_soft_state_fini(softstate_p); 1861b115575SJohn Danielson return (DAM_FAILURE); 1871b115575SJohn Danielson } 1881b115575SJohn Danielson if (dam_kstat_create(mapp) != DDI_SUCCESS) { 1891b115575SJohn Danielson ddi_soft_state_fini(softstate_p); 1901b115575SJohn Danielson ddi_strid_fini(&mapp->dam_addr_hash); 1911b115575SJohn Danielson return (DAM_FAILURE); 1921b115575SJohn Danielson } 1931b115575SJohn Danielson mapp->dam_da = softstate_p; 1941b115575SJohn Danielson mapp->dam_high = 1; 1951b115575SJohn Danielson bitset_resize(&mapp->dam_active_set, mapp->dam_size); 1961b115575SJohn Danielson bitset_resize(&mapp->dam_stable_set, mapp->dam_size); 1971b115575SJohn Danielson bitset_resize(&mapp->dam_report_set, mapp->dam_size); 1981b115575SJohn Danielson return (DAM_SUCCESS); 1991b115575SJohn Danielson } 2001b115575SJohn Danielson 2011b115575SJohn Danielson /* 2021b115575SJohn Danielson * Destroy address map 2034c06356bSdh142964 * 2044c06356bSdh142964 * damapp: address map 2054c06356bSdh142964 * 2064c06356bSdh142964 * Returns: DAM_SUCCESS 2074c06356bSdh142964 * DAM_EINVAL Invalid argument(s) 2084c06356bSdh142964 * DAM_FAILURE General failure 2094c06356bSdh142964 */ 2104c06356bSdh142964 void 2114c06356bSdh142964 damap_destroy(damap_t *damapp) 2124c06356bSdh142964 { 2134c06356bSdh142964 int i; 2144c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 2154c06356bSdh142964 2164c06356bSdh142964 ASSERT(mapp); 2174c06356bSdh142964 2181b115575SJohn Danielson DTRACE_PROBE1(damap__destroy, char *, mapp->dam_name); 2194c06356bSdh142964 2201b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 2214c06356bSdh142964 2224c06356bSdh142964 /* 2231b115575SJohn Danielson * prevent new reports from being added to the map 2244c06356bSdh142964 */ 2251b115575SJohn Danielson mapp->dam_flags |= DAM_DESTROYPEND; 2264c06356bSdh142964 2271b115575SJohn Danielson if (mapp->dam_high) { 2281b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 2291b115575SJohn Danielson /* 2301b115575SJohn Danielson * wait for outstanding reports to stabilize and cancel 2311b115575SJohn Danielson * the timer for this map 2321b115575SJohn Danielson */ 2331b115575SJohn Danielson (void) damap_sync(damapp); 2341b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 2351b115575SJohn Danielson dam_sched_tmo(mapp, 0, NULL); 2361b115575SJohn Danielson 2371b115575SJohn Danielson /* 2381b115575SJohn Danielson * map is at full stop 2391b115575SJohn Danielson * release the contents of the map, invoking the 2401b115575SJohn Danielson * detactivation protocol as addresses are released 2411b115575SJohn Danielson */ 2421b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 2434c06356bSdh142964 for (i = 1; i < mapp->dam_high; i++) { 2444c06356bSdh142964 if (ddi_get_soft_state(mapp->dam_da, i) == NULL) 2454c06356bSdh142964 continue; 2461b115575SJohn Danielson 2471b115575SJohn Danielson ASSERT(DAM_IN_REPORT(mapp, i) == 0); 2481b115575SJohn Danielson 2491b115575SJohn Danielson if (DAM_IS_STABLE(mapp, i)) { 2501b115575SJohn Danielson dam_addr_deactivate(mapp, i); 2511b115575SJohn Danielson } else { 2524c06356bSdh142964 ddi_strid_free(mapp->dam_addr_hash, i); 2534c06356bSdh142964 ddi_soft_state_free(mapp->dam_da, i); 2544c06356bSdh142964 } 2551b115575SJohn Danielson } 2564c06356bSdh142964 ddi_strid_fini(&mapp->dam_addr_hash); 2574c06356bSdh142964 ddi_soft_state_fini(&mapp->dam_da); 2581b115575SJohn Danielson kstat_delete(mapp->dam_kstatsp); 2591b115575SJohn Danielson } 2604c06356bSdh142964 bitset_fini(&mapp->dam_active_set); 2614c06356bSdh142964 bitset_fini(&mapp->dam_stable_set); 2624c06356bSdh142964 bitset_fini(&mapp->dam_report_set); 2634c06356bSdh142964 mutex_destroy(&mapp->dam_lock); 2644c06356bSdh142964 cv_destroy(&mapp->dam_cv); 2654c06356bSdh142964 if (mapp->dam_name) 2664c06356bSdh142964 kmem_free(mapp->dam_name, strlen(mapp->dam_name) + 1); 2674c06356bSdh142964 kmem_free(mapp, sizeof (*mapp)); 2684c06356bSdh142964 } 2694c06356bSdh142964 2704c06356bSdh142964 /* 2714c06356bSdh142964 * Wait for map stability. 2724c06356bSdh142964 * 2734c06356bSdh142964 * damapp: address map 2744c06356bSdh142964 */ 2754c06356bSdh142964 int 2764c06356bSdh142964 damap_sync(damap_t *damapp) 2774c06356bSdh142964 { 2781b115575SJohn Danielson #define WAITFOR_FLAGS (DAM_SETADD | DAM_SPEND) 2794c06356bSdh142964 2804c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 2814c06356bSdh142964 int none_active; 2824c06356bSdh142964 2834c06356bSdh142964 ASSERT(mapp); 2844c06356bSdh142964 2851b115575SJohn Danielson DTRACE_PROBE2(damap__map__sync__start, char *, mapp->dam_name, 2861b115575SJohn Danielson dam_t *, mapp); 2874c06356bSdh142964 2881b115575SJohn Danielson /* 2891b115575SJohn Danielson * block where waiting for 2901b115575SJohn Danielson * a) stabilization pending or a fullset update pending 2911b115575SJohn Danielson * b) any scheduled timeouts to fire 2921b115575SJohn Danielson * c) the report set to finalize (bitset is null) 2931b115575SJohn Danielson */ 2944c06356bSdh142964 mutex_enter(&mapp->dam_lock); 2954c06356bSdh142964 while ((mapp->dam_flags & WAITFOR_FLAGS) || 2964c06356bSdh142964 (!bitset_is_null(&mapp->dam_report_set)) || (mapp->dam_tid != 0)) { 2971b115575SJohn Danielson DTRACE_PROBE2(damap__map__sync__waiting, char *, mapp->dam_name, 2981b115575SJohn Danielson dam_t *, mapp); 2994c06356bSdh142964 cv_wait(&mapp->dam_cv, &mapp->dam_lock); 3004c06356bSdh142964 } 3014c06356bSdh142964 3024c06356bSdh142964 none_active = bitset_is_null(&mapp->dam_active_set); 3034c06356bSdh142964 3044c06356bSdh142964 mutex_exit(&mapp->dam_lock); 3051b115575SJohn Danielson DTRACE_PROBE3(damap__map__sync__end, char *, mapp->dam_name, int, 3061b115575SJohn Danielson none_active, dam_t *, mapp); 3074c06356bSdh142964 3084c06356bSdh142964 return (none_active); 3094c06356bSdh142964 } 3104c06356bSdh142964 3114c06356bSdh142964 /* 3124c06356bSdh142964 * Get the name of a device address map 3134c06356bSdh142964 * 3144c06356bSdh142964 * damapp: address map 3154c06356bSdh142964 * 3164c06356bSdh142964 * Returns: name 3174c06356bSdh142964 */ 3184c06356bSdh142964 char * 3194c06356bSdh142964 damap_name(damap_t *damapp) 3204c06356bSdh142964 { 3214c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 3224c06356bSdh142964 3234c06356bSdh142964 return (mapp ? mapp->dam_name : "UNKNOWN_damap"); 3244c06356bSdh142964 } 3254c06356bSdh142964 3264c06356bSdh142964 /* 3274c06356bSdh142964 * Report an address to per-address report 3284c06356bSdh142964 * 3294c06356bSdh142964 * damapp: address map handle 3304c06356bSdh142964 * address: address in ascii string representation 3311b115575SJohn Danielson * addridp: address ID 3324c06356bSdh142964 * nvl: optional nvlist of configuration-private data 3334c06356bSdh142964 * addr_priv: optional provider-private (passed to activate/deactivate cb) 3344c06356bSdh142964 * 3354c06356bSdh142964 * Returns: DAM_SUCCESS 3364c06356bSdh142964 * DAM_EINVAL Invalid argument(s) 3374c06356bSdh142964 * DAM_MAPFULL address map exhausted 3384c06356bSdh142964 */ 3394c06356bSdh142964 int 3401b115575SJohn Danielson damap_addr_add(damap_t *damapp, char *address, damap_id_t *addridp, 3411b115575SJohn Danielson nvlist_t *nvl, void *addr_priv) 3424c06356bSdh142964 { 3434c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 3444c06356bSdh142964 id_t addrid; 3454c06356bSdh142964 dam_da_t *passp; 3464c06356bSdh142964 3471b115575SJohn Danielson if (!mapp || !address || (mapp->dam_rptmode != DAMAP_REPORT_PERADDR)) 3484c06356bSdh142964 return (DAM_EINVAL); 3494c06356bSdh142964 3501b115575SJohn Danielson DTRACE_PROBE3(damap__addr__add, char *, mapp->dam_name, 3511b115575SJohn Danielson char *, address, dam_t *, mapp); 3521b115575SJohn Danielson 3531b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 3541b115575SJohn Danielson if ((dam_map_alloc(mapp) != DAM_SUCCESS) || 3551b115575SJohn Danielson ((addrid = dam_get_addrid(mapp, address)) == 0)) { 3561b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 3574c06356bSdh142964 return (DAM_MAPFULL); 3584c06356bSdh142964 } 3594c06356bSdh142964 3604c06356bSdh142964 passp = ddi_get_soft_state(mapp->dam_da, addrid); 3614c06356bSdh142964 ASSERT(passp != NULL); 3624c06356bSdh142964 3634c06356bSdh142964 /* 3644c06356bSdh142964 * If re-reporting the same address (add or remove) clear 3654c06356bSdh142964 * the existing report 3664c06356bSdh142964 */ 3674c06356bSdh142964 if (DAM_IN_REPORT(mapp, addrid)) { 3681b115575SJohn Danielson DTRACE_PROBE3(damap__addr__add__jitter, char *, mapp->dam_name, 3691b115575SJohn Danielson char *, address, dam_t *, mapp); 3701b115575SJohn Danielson DAM_INCR_STAT(mapp, dam_jitter); 3711b115575SJohn Danielson dam_addr_report_release(mapp, addrid); 3724c06356bSdh142964 passp->da_jitter++; 3734c06356bSdh142964 } 3744c06356bSdh142964 passp->da_ppriv_rpt = addr_priv; 3754c06356bSdh142964 if (nvl) 3764c06356bSdh142964 (void) nvlist_dup(nvl, &passp->da_nvl_rpt, KM_SLEEP); 3774c06356bSdh142964 3781b115575SJohn Danielson dam_addr_report(mapp, passp, addrid, RPT_ADDR_ADD); 3791b115575SJohn Danielson if (addridp != NULL) 3801b115575SJohn Danielson *addridp = (damap_id_t)addrid; 3811b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 3824c06356bSdh142964 return (DAM_SUCCESS); 3834c06356bSdh142964 } 3844c06356bSdh142964 3854c06356bSdh142964 /* 3864c06356bSdh142964 * Report removal of address from per-address report 3874c06356bSdh142964 * 3884c06356bSdh142964 * damapp: address map 3894c06356bSdh142964 * address: address in ascii string representation 3904c06356bSdh142964 * 3914c06356bSdh142964 * Returns: DAM_SUCCESS 3924c06356bSdh142964 * DAM_EINVAL Invalid argument(s) 3934c06356bSdh142964 * DAM_FAILURE General failure 3944c06356bSdh142964 */ 3954c06356bSdh142964 int 3964c06356bSdh142964 damap_addr_del(damap_t *damapp, char *address) 3974c06356bSdh142964 { 3984c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 3994c06356bSdh142964 id_t addrid; 4004c06356bSdh142964 dam_da_t *passp; 4014c06356bSdh142964 4021b115575SJohn Danielson if (!mapp || !address || (mapp->dam_rptmode != DAMAP_REPORT_PERADDR)) 4034c06356bSdh142964 return (DAM_EINVAL); 4044c06356bSdh142964 4051b115575SJohn Danielson DTRACE_PROBE3(damap__addr__del, char *, mapp->dam_name, 4061b115575SJohn Danielson char *, address, dam_t *, mapp); 4071b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 4081b115575SJohn Danielson if (dam_map_alloc(mapp) != DAM_SUCCESS) { 4091b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 4101b115575SJohn Danielson return (DAM_MAPFULL); 4111b115575SJohn Danielson } 4121b115575SJohn Danielson 4131b115575SJohn Danielson /* 4141b115575SJohn Danielson * if reporting the removal of an address which is not in the map 4151b115575SJohn Danielson * return success 4161b115575SJohn Danielson */ 4174c06356bSdh142964 if (!(addrid = ddi_strid_str2id(mapp->dam_addr_hash, address))) { 4181b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 4194c06356bSdh142964 return (DAM_SUCCESS); 4204c06356bSdh142964 } 4214c06356bSdh142964 passp = ddi_get_soft_state(mapp->dam_da, addrid); 4224c06356bSdh142964 ASSERT(passp); 4234c06356bSdh142964 if (DAM_IN_REPORT(mapp, addrid)) { 4241b115575SJohn Danielson DTRACE_PROBE3(damap__addr__del__jitter, char *, mapp->dam_name, 4251b115575SJohn Danielson char *, address, dam_t *, mapp); 4261b115575SJohn Danielson DAM_INCR_STAT(mapp, dam_jitter); 4271b115575SJohn Danielson dam_addr_report_release(mapp, addrid); 4284c06356bSdh142964 passp->da_jitter++; 4294c06356bSdh142964 } 4301b115575SJohn Danielson dam_addr_report(mapp, passp, addrid, RPT_ADDR_DEL); 4311b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 4324c06356bSdh142964 return (DAM_SUCCESS); 4334c06356bSdh142964 } 4344c06356bSdh142964 4354c06356bSdh142964 /* 4364c06356bSdh142964 * Initiate full-set report 4374c06356bSdh142964 * 4384c06356bSdh142964 * damapp: address map 4394c06356bSdh142964 * 4404c06356bSdh142964 * Returns: DAM_SUCCESS 4414c06356bSdh142964 * DAM_EINVAL Invalid argument(s) 4424c06356bSdh142964 */ 4434c06356bSdh142964 int 4444c06356bSdh142964 damap_addrset_begin(damap_t *damapp) 4454c06356bSdh142964 { 4464c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 4474c06356bSdh142964 int i; 4484c06356bSdh142964 4491b115575SJohn Danielson if (!mapp || (mapp->dam_rptmode != DAMAP_REPORT_FULLSET)) 4504c06356bSdh142964 return (DAM_EINVAL); 4514c06356bSdh142964 4521b115575SJohn Danielson DTRACE_PROBE2(damap__addrset__begin, char *, mapp->dam_name, dam_t *, 4531b115575SJohn Danielson mapp); 4541b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 4551b115575SJohn Danielson if (dam_map_alloc(mapp) != DAM_SUCCESS) { 4561b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 4571b115575SJohn Danielson return (DAM_MAPFULL); 4581b115575SJohn Danielson } 4594c06356bSdh142964 if (mapp->dam_flags & DAM_SETADD) { 4601b115575SJohn Danielson DTRACE_PROBE2(damap__addrset__begin__reset, char *, 4611b115575SJohn Danielson mapp->dam_name, dam_t *, mapp); 4624c06356bSdh142964 /* 4634c06356bSdh142964 * cancel stabilization timeout 4644c06356bSdh142964 */ 4654c06356bSdh142964 dam_sched_tmo(mapp, 0, NULL); 4661b115575SJohn Danielson DAM_INCR_STAT(mapp, dam_jitter); 4671b115575SJohn Danielson 4681b115575SJohn Danielson /* 4691b115575SJohn Danielson * clear pending reports 4701b115575SJohn Danielson */ 4714c06356bSdh142964 for (i = 1; i < mapp->dam_high; i++) { 4724c06356bSdh142964 if (DAM_IN_REPORT(mapp, i)) 4731b115575SJohn Danielson dam_addr_report_release(mapp, i); 4744c06356bSdh142964 } 4754c06356bSdh142964 } 4764c06356bSdh142964 bitset_zero(&mapp->dam_report_set); 4771b115575SJohn Danielson mapp->dam_flags |= DAM_SETADD; 4781b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 4794c06356bSdh142964 return (DAM_SUCCESS); 4804c06356bSdh142964 } 4814c06356bSdh142964 4824c06356bSdh142964 /* 4834c06356bSdh142964 * Report address to full-set report 4844c06356bSdh142964 * 4854c06356bSdh142964 * damapp: address map handle 4864c06356bSdh142964 * address: address in ascii string representation 4874c06356bSdh142964 * rindx: index if address stabilizes 4884c06356bSdh142964 * nvl: optional nvlist of configuration-private data 4894c06356bSdh142964 * addr_priv: optional provider-private data (passed to activate/release cb) 4904c06356bSdh142964 * 4914c06356bSdh142964 * Returns: DAM_SUCCESS 4924c06356bSdh142964 * DAM_EINVAL Invalid argument(s) 4934c06356bSdh142964 * DAM_MAPFULL address map exhausted 4944c06356bSdh142964 * DAM_FAILURE General failure 4954c06356bSdh142964 */ 4964c06356bSdh142964 int 4974c06356bSdh142964 damap_addrset_add(damap_t *damapp, char *address, damap_id_t *ridx, 4984c06356bSdh142964 nvlist_t *nvl, void *addr_priv) 4994c06356bSdh142964 { 5004c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 5014c06356bSdh142964 id_t addrid; 5024c06356bSdh142964 dam_da_t *passp; 5034c06356bSdh142964 5041b115575SJohn Danielson if (!mapp || !address || (mapp->dam_rptmode != DAMAP_REPORT_FULLSET)) 5054c06356bSdh142964 return (DAM_EINVAL); 5064c06356bSdh142964 5071b115575SJohn Danielson DTRACE_PROBE3(damap__addrset__add, char *, mapp->dam_name, 5081b115575SJohn Danielson char *, address, dam_t *, mapp); 5094c06356bSdh142964 5101b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 5111b115575SJohn Danielson if (!(mapp->dam_flags & DAM_SETADD)) { 5121b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 5131b115575SJohn Danielson return (DAM_FAILURE); 5141b115575SJohn Danielson } 5151b115575SJohn Danielson 5164c06356bSdh142964 if ((addrid = dam_get_addrid(mapp, address)) == 0) { 5171b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 5184c06356bSdh142964 return (DAM_MAPFULL); 5194c06356bSdh142964 } 5204c06356bSdh142964 5214c06356bSdh142964 passp = ddi_get_soft_state(mapp->dam_da, addrid); 5224c06356bSdh142964 ASSERT(passp); 5234c06356bSdh142964 if (DAM_IN_REPORT(mapp, addrid)) { 5241b115575SJohn Danielson DTRACE_PROBE3(damap__addrset__add__jitter, char *, 5251b115575SJohn Danielson mapp->dam_name, char *, address, dam_t *, mapp); 5261b115575SJohn Danielson dam_addr_report_release(mapp, addrid); 5274c06356bSdh142964 passp->da_jitter++; 5284c06356bSdh142964 } 5294c06356bSdh142964 passp->da_ppriv_rpt = addr_priv; 5304c06356bSdh142964 if (nvl) 5314c06356bSdh142964 (void) nvlist_dup(nvl, &passp->da_nvl_rpt, KM_SLEEP); 5324c06356bSdh142964 bitset_add(&mapp->dam_report_set, addrid); 5334c06356bSdh142964 if (ridx) 5344c06356bSdh142964 *ridx = (damap_id_t)addrid; 5351b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 5364c06356bSdh142964 return (DAM_SUCCESS); 5374c06356bSdh142964 } 5384c06356bSdh142964 5394c06356bSdh142964 /* 5404c06356bSdh142964 * Commit full-set report for stabilization 5414c06356bSdh142964 * 5424c06356bSdh142964 * damapp: address map handle 5434c06356bSdh142964 * flags: (currently 0) 5444c06356bSdh142964 * 5454c06356bSdh142964 * Returns: DAM_SUCCESS 5464c06356bSdh142964 * DAM_EINVAL Invalid argument(s) 5474c06356bSdh142964 * DAM_FAILURE General failure 5484c06356bSdh142964 */ 5494c06356bSdh142964 int 5504c06356bSdh142964 damap_addrset_end(damap_t *damapp, int flags) 5514c06356bSdh142964 { 5524c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 5534c06356bSdh142964 int i; 5544c06356bSdh142964 5551b115575SJohn Danielson if (!mapp || (mapp->dam_rptmode != DAMAP_REPORT_FULLSET)) 5564c06356bSdh142964 return (DAM_EINVAL); 5574c06356bSdh142964 5581b115575SJohn Danielson DTRACE_PROBE2(damap__addrset__end, char *, mapp->dam_name, 5591b115575SJohn Danielson dam_t *, mapp); 5604c06356bSdh142964 5611b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 5621b115575SJohn Danielson if (!(mapp->dam_flags & DAM_SETADD)) { 5631b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 5641b115575SJohn Danielson return (DAM_FAILURE); 5651b115575SJohn Danielson } 5661b115575SJohn Danielson 5671b115575SJohn Danielson if (flags & DAMAP_END_RESET) { 5681b115575SJohn Danielson DTRACE_PROBE2(damap__addrset__end__reset, char *, 5691b115575SJohn Danielson mapp->dam_name, dam_t *, mapp); 5704c06356bSdh142964 dam_sched_tmo(mapp, 0, NULL); 5714c06356bSdh142964 for (i = 1; i < mapp->dam_high; i++) 5724c06356bSdh142964 if (DAM_IN_REPORT(mapp, i)) 5731b115575SJohn Danielson dam_addr_report_release(mapp, i); 5744c06356bSdh142964 } else { 5754c06356bSdh142964 mapp->dam_last_update = gethrtime(); 5761b115575SJohn Danielson dam_sched_tmo(mapp, mapp->dam_stabletmo, dam_addrset_stable_cb); 5774c06356bSdh142964 } 5781b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 5794c06356bSdh142964 return (DAM_SUCCESS); 5804c06356bSdh142964 } 5814c06356bSdh142964 5824c06356bSdh142964 /* 5834c06356bSdh142964 * Return nvlist registered with reported address 5844c06356bSdh142964 * 5854c06356bSdh142964 * damapp: address map handle 5861b115575SJohn Danielson * addrid: address ID 5874c06356bSdh142964 * 5884c06356bSdh142964 * Returns: nvlist_t * provider supplied via damap_addr{set}_add()) 5894c06356bSdh142964 * NULL 5904c06356bSdh142964 */ 5914c06356bSdh142964 nvlist_t * 5924c06356bSdh142964 damap_id2nvlist(damap_t *damapp, damap_id_t addrid) 5934c06356bSdh142964 { 5944c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 5954c06356bSdh142964 dam_da_t *pass; 5964c06356bSdh142964 5971b115575SJohn Danielson if (mapp->dam_high && ddi_strid_id2str(mapp->dam_addr_hash, addrid)) { 5981b115575SJohn Danielson if (pass = ddi_get_soft_state(mapp->dam_da, addrid)) 5994c06356bSdh142964 return (pass->da_nvl); 6004c06356bSdh142964 } 6014c06356bSdh142964 return (NULL); 6024c06356bSdh142964 } 6034c06356bSdh142964 6044c06356bSdh142964 /* 6054c06356bSdh142964 * Return address string 6064c06356bSdh142964 * 6074c06356bSdh142964 * damapp: address map handle 6081b115575SJohn Danielson * addrid: address ID 6094c06356bSdh142964 * 6104c06356bSdh142964 * Returns: char * Address string 6114c06356bSdh142964 * NULL 6124c06356bSdh142964 */ 6134c06356bSdh142964 char * 6141b115575SJohn Danielson damap_id2addr(damap_t *damapp, damap_id_t addrid) 6154c06356bSdh142964 { 6164c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 6174c06356bSdh142964 6181b115575SJohn Danielson if (mapp->dam_high) 6191b115575SJohn Danielson return (ddi_strid_id2str(mapp->dam_addr_hash, addrid)); 6201b115575SJohn Danielson else 6211b115575SJohn Danielson return (NULL); 6224c06356bSdh142964 } 6234c06356bSdh142964 6244c06356bSdh142964 /* 6254c06356bSdh142964 * Release address reference in map 6264c06356bSdh142964 * 6274c06356bSdh142964 * damapp: address map handle 6281b115575SJohn Danielson * addrid: address ID 6294c06356bSdh142964 */ 6304c06356bSdh142964 void 6314c06356bSdh142964 damap_id_rele(damap_t *damapp, damap_id_t addrid) 6324c06356bSdh142964 { 6334c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 6341b115575SJohn Danielson dam_da_t *passp; 6351b115575SJohn Danielson char *addr; 6364c06356bSdh142964 6371b115575SJohn Danielson passp = ddi_get_soft_state(mapp->dam_da, (id_t)addrid); 6381b115575SJohn Danielson ASSERT(passp); 6391b115575SJohn Danielson 6401b115575SJohn Danielson addr = damap_id2addr(damapp, addrid); 6411b115575SJohn Danielson DTRACE_PROBE4(damap__id__rele, char *, mapp->dam_name, char *, addr, 6421b115575SJohn Danielson dam_t *, mapp, int, passp->da_ref); 6431b115575SJohn Danielson 6441b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 6451b115575SJohn Danielson 6461b115575SJohn Danielson /* 6471b115575SJohn Danielson * teardown address if last outstanding reference 6481b115575SJohn Danielson */ 6491b115575SJohn Danielson if (--passp->da_ref == 0) 6501b115575SJohn Danielson dam_addr_release(mapp, (id_t)addrid); 6511b115575SJohn Danielson 6521b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 6534c06356bSdh142964 } 6544c06356bSdh142964 6554c06356bSdh142964 /* 6564c06356bSdh142964 * Return current reference count on address reference in map 6574c06356bSdh142964 * 6584c06356bSdh142964 * damapp: address map handle 6591b115575SJohn Danielson * addrid: address ID 6604c06356bSdh142964 * 6614c06356bSdh142964 * Returns: DAM_SUCCESS 6624c06356bSdh142964 * DAM_FAILURE 6634c06356bSdh142964 */ 6644c06356bSdh142964 int 6651b115575SJohn Danielson damap_id_ref(damap_t *damapp, damap_id_t addrid) 6664c06356bSdh142964 { 6674c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 6684c06356bSdh142964 dam_da_t *passp; 6694c06356bSdh142964 int ref = -1; 6704c06356bSdh142964 6711b115575SJohn Danielson passp = ddi_get_soft_state(mapp->dam_da, (id_t)addrid); 6724c06356bSdh142964 if (passp) 6734c06356bSdh142964 ref = passp->da_ref; 6741b115575SJohn Danielson 6754c06356bSdh142964 return (ref); 6764c06356bSdh142964 } 6774c06356bSdh142964 6784c06356bSdh142964 /* 6794c06356bSdh142964 * Return next address ID in list 6804c06356bSdh142964 * 6814c06356bSdh142964 * damapp: address map handle 6824c06356bSdh142964 * damap_list: address ID list passed to config|unconfig 6834c06356bSdh142964 * returned by look by lookup_all 6844c06356bSdh142964 * last: last ID returned, 0 is start of list 6854c06356bSdh142964 * 6864c06356bSdh142964 * Returns: addrid Next ID from the list 6874c06356bSdh142964 * 0 End of the list 6884c06356bSdh142964 */ 6894c06356bSdh142964 damap_id_t 6904c06356bSdh142964 damap_id_next(damap_t *damapp, damap_id_list_t damap_list, damap_id_t last) 6914c06356bSdh142964 { 6924c06356bSdh142964 int i, start; 6934c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 6944c06356bSdh142964 bitset_t *dam_list = (bitset_t *)damap_list; 6954c06356bSdh142964 6964c06356bSdh142964 if (!mapp || !dam_list) 6974c06356bSdh142964 return ((damap_id_t)0); 6984c06356bSdh142964 6994c06356bSdh142964 start = (int)last + 1; 7001b115575SJohn Danielson for (i = start; i < mapp->dam_high; i++) { 7011b115575SJohn Danielson if (bitset_in_set(dam_list, i)) { 7024c06356bSdh142964 return ((damap_id_t)i); 7031b115575SJohn Danielson } 7041b115575SJohn Danielson } 7054c06356bSdh142964 return ((damap_id_t)0); 7064c06356bSdh142964 } 7074c06356bSdh142964 7084c06356bSdh142964 /* 7094c06356bSdh142964 * Set config private data 7104c06356bSdh142964 * 7114c06356bSdh142964 * damapp: address map handle 7121b115575SJohn Danielson * addrid: address ID 7134c06356bSdh142964 * cfg_priv: configuration private data 7144c06356bSdh142964 * 7154c06356bSdh142964 */ 7164c06356bSdh142964 void 7171b115575SJohn Danielson damap_id_priv_set(damap_t *damapp, damap_id_t addrid, void *cfg_priv) 7184c06356bSdh142964 { 7194c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 7204c06356bSdh142964 dam_da_t *passp; 7214c06356bSdh142964 7221b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 7231b115575SJohn Danielson passp = ddi_get_soft_state(mapp->dam_da, (id_t)addrid); 7244c06356bSdh142964 if (!passp) { 7251b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 7264c06356bSdh142964 return; 7274c06356bSdh142964 } 7284c06356bSdh142964 passp->da_cfg_priv = cfg_priv; 7291b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 7304c06356bSdh142964 } 7314c06356bSdh142964 7324c06356bSdh142964 /* 7334c06356bSdh142964 * Get config private data 7344c06356bSdh142964 * 7354c06356bSdh142964 * damapp: address map handle 7361b115575SJohn Danielson * addrid: address ID 7374c06356bSdh142964 * 7384c06356bSdh142964 * Returns: configuration private data 7394c06356bSdh142964 */ 7404c06356bSdh142964 void * 7411b115575SJohn Danielson damap_id_priv_get(damap_t *damapp, damap_id_t addrid) 7424c06356bSdh142964 { 7434c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 7444c06356bSdh142964 dam_da_t *passp; 7454c06356bSdh142964 void *rv; 7464c06356bSdh142964 7471b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 7481b115575SJohn Danielson passp = ddi_get_soft_state(mapp->dam_da, (id_t)addrid); 7494c06356bSdh142964 if (!passp) { 7501b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 7514c06356bSdh142964 return (NULL); 7524c06356bSdh142964 } 7534c06356bSdh142964 rv = passp->da_cfg_priv; 7541b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 7554c06356bSdh142964 return (rv); 7564c06356bSdh142964 } 7574c06356bSdh142964 7584c06356bSdh142964 /* 7594c06356bSdh142964 * Lookup a single address in the active address map 7604c06356bSdh142964 * 7614c06356bSdh142964 * damapp: address map handle 7624c06356bSdh142964 * address: address string 7634c06356bSdh142964 * 7644c06356bSdh142964 * Returns: ID of active/stable address 7654c06356bSdh142964 * 0 Address not in stable set 7664c06356bSdh142964 * 7674c06356bSdh142964 * Future: Allow the caller to wait for stabilize before returning not found. 7684c06356bSdh142964 */ 7694c06356bSdh142964 damap_id_t 7704c06356bSdh142964 damap_lookup(damap_t *damapp, char *address) 7714c06356bSdh142964 { 7724c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 7734c06356bSdh142964 id_t addrid = 0; 7744c06356bSdh142964 dam_da_t *passp = NULL; 7754c06356bSdh142964 7761b115575SJohn Danielson DTRACE_PROBE3(damap__lookup, char *, mapp->dam_name, 7771b115575SJohn Danielson char *, address, dam_t *, mapp); 7781b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 7791b115575SJohn Danielson if (!mapp->dam_high) 7801b115575SJohn Danielson addrid = 0; 7811b115575SJohn Danielson else 7824c06356bSdh142964 addrid = ddi_strid_str2id(mapp->dam_addr_hash, address); 7834c06356bSdh142964 if (addrid) { 7844c06356bSdh142964 if (DAM_IS_STABLE(mapp, addrid)) { 7854c06356bSdh142964 passp = ddi_get_soft_state(mapp->dam_da, addrid); 7864c06356bSdh142964 ASSERT(passp); 7874c06356bSdh142964 if (passp) { 7884c06356bSdh142964 passp->da_ref++; 7894c06356bSdh142964 } else { 7904c06356bSdh142964 addrid = 0; 7914c06356bSdh142964 } 7924c06356bSdh142964 } else { 7934c06356bSdh142964 addrid = 0; 7944c06356bSdh142964 } 7954c06356bSdh142964 } 7961b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 7971b115575SJohn Danielson DTRACE_PROBE4(damap__lookup__return, char *, mapp->dam_name, 7981b115575SJohn Danielson char *, address, dam_t *, mapp, int, addrid); 7994c06356bSdh142964 return ((damap_id_t)addrid); 8004c06356bSdh142964 } 8014c06356bSdh142964 8024c06356bSdh142964 8034c06356bSdh142964 /* 8044c06356bSdh142964 * Return the list of stable addresses in the map 8054c06356bSdh142964 * 8064c06356bSdh142964 * damapp: address map handle 8074c06356bSdh142964 * id_listp: pointer to list of address IDs in stable map (returned) 8084c06356bSdh142964 * 8094c06356bSdh142964 * Returns: # of entries returned in alist 8104c06356bSdh142964 */ 8114c06356bSdh142964 int 8124c06356bSdh142964 damap_lookup_all(damap_t *damapp, damap_id_list_t *id_listp) 8134c06356bSdh142964 { 8144c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 8154c06356bSdh142964 int mapsz = mapp->dam_size; 8164c06356bSdh142964 int n_ids, i; 8174c06356bSdh142964 bitset_t *bsp; 8181b115575SJohn Danielson char *addrp; 8194c06356bSdh142964 dam_da_t *passp; 8204c06356bSdh142964 8211b115575SJohn Danielson DTRACE_PROBE2(damap__lookup__all, char *, mapp->dam_name, 8221b115575SJohn Danielson dam_t *, mapp); 8231b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 8241b115575SJohn Danielson if (!mapp->dam_high) { 8251b115575SJohn Danielson *id_listp = (damap_id_list_t)NULL; 8261b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 8271b115575SJohn Danielson DTRACE_PROBE3(damap__lookup__all__nomap, char *, 8281b115575SJohn Danielson mapp->dam_name, dam_t *, mapp, int, 0); 8291b115575SJohn Danielson return (0); 8301b115575SJohn Danielson } 8314c06356bSdh142964 bsp = kmem_alloc(sizeof (*bsp), KM_SLEEP); 8324c06356bSdh142964 bitset_init(bsp); 8334c06356bSdh142964 bitset_resize(bsp, mapsz); 8344c06356bSdh142964 bitset_copy(&mapp->dam_active_set, bsp); 8354c06356bSdh142964 for (n_ids = 0, i = 1; i < mapsz; i++) { 8364c06356bSdh142964 if (bitset_in_set(bsp, i)) { 8374c06356bSdh142964 passp = ddi_get_soft_state(mapp->dam_da, i); 8384c06356bSdh142964 ASSERT(passp); 8394c06356bSdh142964 if (passp) { 8401b115575SJohn Danielson addrp = damap_id2addr(damapp, i); 8411b115575SJohn Danielson DTRACE_PROBE3(damap__lookup__all__item, char *, 8421b115575SJohn Danielson mapp->dam_name, char *, addrp, dam_t *, 8431b115575SJohn Danielson mapp); 8444c06356bSdh142964 passp->da_ref++; 8454c06356bSdh142964 n_ids++; 8464c06356bSdh142964 } 8474c06356bSdh142964 } 8484c06356bSdh142964 } 8494c06356bSdh142964 if (n_ids) { 8504c06356bSdh142964 *id_listp = (damap_id_list_t)bsp; 8511b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 8524c06356bSdh142964 return (n_ids); 8534c06356bSdh142964 } else { 8544c06356bSdh142964 *id_listp = (damap_id_list_t)NULL; 8554c06356bSdh142964 bitset_fini(bsp); 8564c06356bSdh142964 kmem_free(bsp, sizeof (*bsp)); 8571b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 8584c06356bSdh142964 return (0); 8594c06356bSdh142964 } 8604c06356bSdh142964 } 8614c06356bSdh142964 8624c06356bSdh142964 /* 8634c06356bSdh142964 * Release the address list returned by damap_lookup_all() 8644c06356bSdh142964 * 8654c06356bSdh142964 * mapp: address map handle 8664c06356bSdh142964 * id_list: list of address IDs returned in damap_lookup_all() 8674c06356bSdh142964 */ 8684c06356bSdh142964 void 8694c06356bSdh142964 damap_id_list_rele(damap_t *damapp, damap_id_list_t id_list) 8704c06356bSdh142964 { 8714c06356bSdh142964 dam_t *mapp = (dam_t *)damapp; 8724c06356bSdh142964 int i; 8734c06356bSdh142964 8744c06356bSdh142964 if (id_list == NULL) 8754c06356bSdh142964 return; 8764c06356bSdh142964 8771b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 8784c06356bSdh142964 for (i = 1; i < mapp->dam_high; i++) { 8794c06356bSdh142964 if (bitset_in_set((bitset_t *)id_list, i)) 8801b115575SJohn Danielson (void) dam_addr_release(mapp, i); 8814c06356bSdh142964 } 8821b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 8834c06356bSdh142964 bitset_fini((bitset_t *)id_list); 8844c06356bSdh142964 kmem_free((void *)id_list, sizeof (bitset_t)); 8854c06356bSdh142964 } 8864c06356bSdh142964 8874c06356bSdh142964 /* 8881b115575SJohn Danielson * activate an address that has passed the stabilization interval 8894c06356bSdh142964 */ 8904c06356bSdh142964 static void 8911b115575SJohn Danielson dam_addr_activate(dam_t *mapp, id_t addrid) 8924c06356bSdh142964 { 8934c06356bSdh142964 dam_da_t *passp; 8941b115575SJohn Danielson int config_rv; 8954c06356bSdh142964 char *addrstr; 8964c06356bSdh142964 8971b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 8981b115575SJohn Danielson bitset_add(&mapp->dam_active_set, addrid); 8991b115575SJohn Danielson passp = ddi_get_soft_state(mapp->dam_da, addrid); 9004c06356bSdh142964 ASSERT(passp); 9011b115575SJohn Danielson 9024c06356bSdh142964 /* 9034c06356bSdh142964 * copy the reported nvlist and provider private data 9044c06356bSdh142964 */ 9051b115575SJohn Danielson addrstr = ddi_strid_id2str(mapp->dam_addr_hash, addrid); 9061b115575SJohn Danielson DTRACE_PROBE3(damap__addr__activate__start, char *, mapp->dam_name, 9071b115575SJohn Danielson char *, addrstr, dam_t *, mapp); 9084c06356bSdh142964 passp->da_nvl = passp->da_nvl_rpt; 9094c06356bSdh142964 passp->da_ppriv = passp->da_ppriv_rpt; 9104c06356bSdh142964 passp->da_ppriv_rpt = NULL; 9114c06356bSdh142964 passp->da_nvl_rpt = NULL; 9124c06356bSdh142964 passp->da_last_stable = gethrtime(); 9134c06356bSdh142964 passp->da_stable_cnt++; 9141b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 9151b115575SJohn Danielson if (mapp->dam_activate_cb) { 9161b115575SJohn Danielson (*mapp->dam_activate_cb)(mapp->dam_activate_arg, addrstr, 9171b115575SJohn Danielson addrid, &passp->da_ppriv_rpt); 9184c06356bSdh142964 } 9194c06356bSdh142964 9204c06356bSdh142964 /* 9211b115575SJohn Danielson * call the address-specific configuration action as part of 9221b115575SJohn Danielson * activation. 9234c06356bSdh142964 */ 9241b115575SJohn Danielson config_rv = (*mapp->dam_configure_cb)(mapp->dam_config_arg, mapp, 9251b115575SJohn Danielson addrid); 9261b115575SJohn Danielson if (config_rv != DAM_SUCCESS) { 9271b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 9281b115575SJohn Danielson passp->da_flags |= DA_FAILED_CONFIG; 9291b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 9301b115575SJohn Danielson DTRACE_PROBE3(damap__addr__activate__config__failure, 9311b115575SJohn Danielson char *, mapp->dam_name, char *, addrstr, dam_t *, mapp); 932d189c170SReed dam_deact_cleanup(mapp, addrid, addrstr, 933d189c170SReed DAMAP_DEACT_RSN_CFG_FAIL); 934d189c170SReed } else { 935d189c170SReed DTRACE_PROBE3(damap__addr__activate__end, char *, 936d189c170SReed mapp->dam_name, char *, addrstr, dam_t *, mapp); 9374c06356bSdh142964 } 9384c06356bSdh142964 } 9394c06356bSdh142964 9404c06356bSdh142964 /* 9411b115575SJohn Danielson * deactivate a previously stable address 9424c06356bSdh142964 */ 9434c06356bSdh142964 static void 9441b115575SJohn Danielson dam_addr_deactivate(dam_t *mapp, id_t addrid) 9454c06356bSdh142964 { 9461b115575SJohn Danielson char *addrstr; 9474c06356bSdh142964 9481b115575SJohn Danielson addrstr = ddi_strid_id2str(mapp->dam_addr_hash, addrid); 9491b115575SJohn Danielson DTRACE_PROBE3(damap__addr__deactivate__start, char *, mapp->dam_name, 9501b115575SJohn Danielson char *, addrstr, dam_t *, mapp); 9511b115575SJohn Danielson 9521b115575SJohn Danielson /* 9531b115575SJohn Danielson * call the unconfiguration callback 9541b115575SJohn Danielson */ 9551b115575SJohn Danielson (*mapp->dam_unconfig_cb)(mapp->dam_config_arg, mapp, addrid); 956d189c170SReed dam_deact_cleanup(mapp, addrid, addrstr, DAMAP_DEACT_RSN_GONE); 957d189c170SReed } 958d189c170SReed 959d189c170SReed static void 960d189c170SReed dam_deact_cleanup(dam_t *mapp, id_t addrid, char *addrstr, 961d189c170SReed damap_deact_rsn_t deact_rsn) 962d189c170SReed { 963d189c170SReed dam_da_t *passp; 964d189c170SReed 9651b115575SJohn Danielson passp = ddi_get_soft_state(mapp->dam_da, addrid); 9661b115575SJohn Danielson ASSERT(passp); 9671b115575SJohn Danielson if (mapp->dam_deactivate_cb) 9681b115575SJohn Danielson (*mapp->dam_deactivate_cb)(mapp->dam_activate_arg, 9691b115575SJohn Danielson ddi_strid_id2str(mapp->dam_addr_hash, addrid), 970d189c170SReed addrid, passp->da_ppriv, deact_rsn); 9711b115575SJohn Danielson 9721b115575SJohn Danielson /* 9731b115575SJohn Danielson * clear the active bit and free the backing info for 9741b115575SJohn Danielson * this address 9751b115575SJohn Danielson */ 9761b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 9771b115575SJohn Danielson bitset_del(&mapp->dam_active_set, addrid); 9781b115575SJohn Danielson passp->da_ppriv = NULL; 9791b115575SJohn Danielson if (passp->da_nvl) 9801b115575SJohn Danielson nvlist_free(passp->da_nvl); 9811b115575SJohn Danielson passp->da_nvl = NULL; 9821b115575SJohn Danielson passp->da_ppriv_rpt = NULL; 9831b115575SJohn Danielson if (passp->da_nvl_rpt) 9841b115575SJohn Danielson nvlist_free(passp->da_nvl_rpt); 9851b115575SJohn Danielson passp->da_nvl_rpt = NULL; 9861b115575SJohn Danielson 9871b115575SJohn Danielson DTRACE_PROBE3(damap__addr__deactivate__end, char *, mapp->dam_name, 9881b115575SJohn Danielson char *, addrstr, dam_t *, mapp); 9891b115575SJohn Danielson 9901b115575SJohn Danielson (void) dam_addr_release(mapp, addrid); 9911b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 9921b115575SJohn Danielson } 9931b115575SJohn Danielson 9941b115575SJohn Danielson /* 9951b115575SJohn Danielson * taskq callback for multi-thread activation 9961b115575SJohn Danielson */ 9971b115575SJohn Danielson static void 9981b115575SJohn Danielson dam_tq_config(void *arg) 9991b115575SJohn Danielson { 10001b115575SJohn Danielson cfg_tqd_t *tqd = (cfg_tqd_t *)arg; 10011b115575SJohn Danielson 10021b115575SJohn Danielson dam_addr_activate(tqd->tqd_mapp, tqd->tqd_id); 10031b115575SJohn Danielson kmem_free(tqd, sizeof (*tqd)); 10041b115575SJohn Danielson } 10051b115575SJohn Danielson 10061b115575SJohn Danielson /* 10071b115575SJohn Danielson * taskq callback for multi-thread deactivation 10081b115575SJohn Danielson */ 10091b115575SJohn Danielson static void 10101b115575SJohn Danielson dam_tq_unconfig(void *arg) 10111b115575SJohn Danielson { 10121b115575SJohn Danielson cfg_tqd_t *tqd = (cfg_tqd_t *)arg; 10131b115575SJohn Danielson 10141b115575SJohn Danielson dam_addr_deactivate(tqd->tqd_mapp, tqd->tqd_id); 10151b115575SJohn Danielson kmem_free(tqd, sizeof (*tqd)); 10161b115575SJohn Danielson } 10171b115575SJohn Danielson 10181b115575SJohn Danielson /* 10191b115575SJohn Danielson * Activate a set of stabilized addresses 10201b115575SJohn Danielson */ 10211b115575SJohn Danielson static void 10221b115575SJohn Danielson dam_addrset_activate(dam_t *mapp, bitset_t *activate) 10231b115575SJohn Danielson { 10241b115575SJohn Danielson 10251b115575SJohn Danielson int i, nset; 10261b115575SJohn Danielson taskq_t *tqp = NULL; 10271b115575SJohn Danielson cfg_tqd_t *tqd = NULL; 10281b115575SJohn Danielson char tqn[TASKQ_NAMELEN]; 10291b115575SJohn Danielson extern pri_t maxclsyspri; 10301b115575SJohn Danielson 10311b115575SJohn Danielson if (mapp->dam_options & DAMAP_MTCONFIG) { 10321b115575SJohn Danielson /* 10331b115575SJohn Danielson * calculate the # of taskq threads to create 10341b115575SJohn Danielson */ 10351b115575SJohn Danielson for (i = 1, nset = 0; i < mapp->dam_high; i++) 10361b115575SJohn Danielson if (bitset_in_set(activate, i)) 10371b115575SJohn Danielson nset++; 10381b115575SJohn Danielson ASSERT(nset); 10391b115575SJohn Danielson (void) snprintf(tqn, sizeof (tqn), "actv-%s", mapp->dam_name); 10401b115575SJohn Danielson tqp = taskq_create(tqn, nset, maxclsyspri, 1, 10411b115575SJohn Danielson INT_MAX, TASKQ_PREPOPULATE); 10421b115575SJohn Danielson } 10431b115575SJohn Danielson for (i = 1; i < mapp->dam_high; i++) { 10441b115575SJohn Danielson if (bitset_in_set(activate, i)) { 10451b115575SJohn Danielson if (!tqp) 10461b115575SJohn Danielson dam_addr_activate(mapp, i); 10471b115575SJohn Danielson else { 10481b115575SJohn Danielson /* 10491b115575SJohn Danielson * multi-threaded activation 10501b115575SJohn Danielson */ 10511b115575SJohn Danielson tqd = kmem_alloc(sizeof (*tqd), KM_SLEEP); 10521b115575SJohn Danielson tqd->tqd_mapp = mapp; 10531b115575SJohn Danielson tqd->tqd_id = i; 10541b115575SJohn Danielson (void) taskq_dispatch(tqp, dam_tq_config, 10551b115575SJohn Danielson tqd, KM_SLEEP); 10561b115575SJohn Danielson } 10571b115575SJohn Danielson } 10581b115575SJohn Danielson } 10591b115575SJohn Danielson if (tqp) { 10601b115575SJohn Danielson taskq_wait(tqp); 10611b115575SJohn Danielson taskq_destroy(tqp); 10621b115575SJohn Danielson } 10631b115575SJohn Danielson } 10641b115575SJohn Danielson 10651b115575SJohn Danielson /* 10661b115575SJohn Danielson * Deactivate a set of stabilized addresses 10671b115575SJohn Danielson */ 10681b115575SJohn Danielson static void 10691b115575SJohn Danielson dam_addrset_deactivate(dam_t *mapp, bitset_t *deactivate) 10701b115575SJohn Danielson { 10711b115575SJohn Danielson int i, nset; 10721b115575SJohn Danielson taskq_t *tqp = NULL; 10731b115575SJohn Danielson cfg_tqd_t *tqd = NULL; 10741b115575SJohn Danielson char tqn[TASKQ_NAMELEN]; 10751b115575SJohn Danielson 10761b115575SJohn Danielson DTRACE_PROBE2(damap__addrset__deactivate, char *, mapp->dam_name, 10771b115575SJohn Danielson dam_t *, mapp); 10781b115575SJohn Danielson 10791b115575SJohn Danielson if (mapp->dam_options & DAMAP_MTCONFIG) { 10801b115575SJohn Danielson /* 10811b115575SJohn Danielson * compute the # of taskq threads to dispatch 10821b115575SJohn Danielson */ 10831b115575SJohn Danielson for (i = 1, nset = 0; i < mapp->dam_high; i++) 10841b115575SJohn Danielson if (bitset_in_set(deactivate, i)) 10851b115575SJohn Danielson nset++; 10861b115575SJohn Danielson (void) snprintf(tqn, sizeof (tqn), "deactv-%s", 10871b115575SJohn Danielson mapp->dam_name); 10881b115575SJohn Danielson tqp = taskq_create(tqn, nset, maxclsyspri, 1, 10891b115575SJohn Danielson INT_MAX, TASKQ_PREPOPULATE); 10901b115575SJohn Danielson } 10911b115575SJohn Danielson for (i = 1; i < mapp->dam_high; i++) { 10921b115575SJohn Danielson if (bitset_in_set(deactivate, i)) { 10931b115575SJohn Danielson if (!tqp) { 10941b115575SJohn Danielson dam_addr_deactivate(mapp, i); 10951b115575SJohn Danielson } else { 10961b115575SJohn Danielson tqd = kmem_alloc(sizeof (*tqd), KM_SLEEP); 10971b115575SJohn Danielson tqd->tqd_mapp = mapp; 10981b115575SJohn Danielson tqd->tqd_id = i; 10991b115575SJohn Danielson (void) taskq_dispatch(tqp, 11001b115575SJohn Danielson dam_tq_unconfig, tqd, KM_SLEEP); 11011b115575SJohn Danielson } 11021b115575SJohn Danielson } 11031b115575SJohn Danielson } 11041b115575SJohn Danielson 11051b115575SJohn Danielson if (tqp) { 11061b115575SJohn Danielson taskq_wait(tqp); 11071b115575SJohn Danielson taskq_destroy(tqp); 11081b115575SJohn Danielson } 11091b115575SJohn Danielson } 11101b115575SJohn Danielson 11111b115575SJohn Danielson /* 11121b115575SJohn Danielson * Release a previously activated address 11131b115575SJohn Danielson */ 11141b115575SJohn Danielson static void 11151b115575SJohn Danielson dam_addr_release(dam_t *mapp, id_t addrid) 11161b115575SJohn Danielson { 11171b115575SJohn Danielson dam_da_t *passp; 11181b115575SJohn Danielson char *addrstr; 11191b115575SJohn Danielson 11201b115575SJohn Danielson 11211b115575SJohn Danielson ASSERT(mutex_owned(&mapp->dam_lock)); 11224c06356bSdh142964 passp = ddi_get_soft_state(mapp->dam_da, addrid); 11234c06356bSdh142964 ASSERT(passp); 11244c06356bSdh142964 11251b115575SJohn Danielson addrstr = ddi_strid_id2str(mapp->dam_addr_hash, addrid); 11261b115575SJohn Danielson DTRACE_PROBE3(damap__addr__release, char *, mapp->dam_name, 11271b115575SJohn Danielson char *, addrstr, dam_t *, mapp); 11284c06356bSdh142964 11294c06356bSdh142964 /* 11301b115575SJohn Danielson * defer releasing the address until outstanding references 11311b115575SJohn Danielson * are released 11324c06356bSdh142964 */ 11331b115575SJohn Danielson if (passp->da_ref > 1) { 11341b115575SJohn Danielson DTRACE_PROBE4(damap__addr__release__outstanding__refs, 11351b115575SJohn Danielson char *, mapp->dam_name, char *, addrstr, dam_t *, mapp, 11361b115575SJohn Danielson int, passp->da_ref); 11374c06356bSdh142964 return; 11384c06356bSdh142964 } 11391b115575SJohn Danielson 11401b115575SJohn Danielson /* 11411b115575SJohn Danielson * allow pending reports to stabilize 11421b115575SJohn Danielson */ 11431b115575SJohn Danielson if (DAM_IN_REPORT(mapp, addrid)) { 11441b115575SJohn Danielson DTRACE_PROBE3(damap__addr__release__report__pending, 11451b115575SJohn Danielson char *, mapp->dam_name, char *, addrstr, dam_t *, mapp); 11461b115575SJohn Danielson return; 11471b115575SJohn Danielson } 11481b115575SJohn Danielson 11494c06356bSdh142964 ddi_strid_free(mapp->dam_addr_hash, addrid); 11504c06356bSdh142964 ddi_soft_state_free(mapp->dam_da, addrid); 11514c06356bSdh142964 } 11524c06356bSdh142964 11534c06356bSdh142964 /* 11544c06356bSdh142964 * process stabilized address reports 11554c06356bSdh142964 */ 11564c06356bSdh142964 static void 11571b115575SJohn Danielson dam_stabilize_map(void *arg) 11584c06356bSdh142964 { 11594c06356bSdh142964 dam_t *mapp = (dam_t *)arg; 11604c06356bSdh142964 bitset_t delta; 11614c06356bSdh142964 bitset_t cfg; 11624c06356bSdh142964 bitset_t uncfg; 11634c06356bSdh142964 int has_cfg, has_uncfg; 11641b115575SJohn Danielson uint32_t i, n_active; 11651b115575SJohn Danielson 11661b115575SJohn Danielson DTRACE_PROBE2(damap__stabilize__map, char *, mapp->dam_name, 11671b115575SJohn Danielson dam_t *, mapp); 11684c06356bSdh142964 11694c06356bSdh142964 bitset_init(&delta); 11704c06356bSdh142964 bitset_resize(&delta, mapp->dam_size); 11714c06356bSdh142964 bitset_init(&cfg); 11724c06356bSdh142964 bitset_resize(&cfg, mapp->dam_size); 11734c06356bSdh142964 bitset_init(&uncfg); 11744c06356bSdh142964 bitset_resize(&uncfg, mapp->dam_size); 11754c06356bSdh142964 11761b115575SJohn Danielson /* 11771b115575SJohn Danielson * determine which addresses have changed during 11781b115575SJohn Danielson * this stabilization cycle 11791b115575SJohn Danielson */ 11801b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 11811b115575SJohn Danielson ASSERT(mapp->dam_flags & DAM_SPEND); 11824c06356bSdh142964 if (!bitset_xor(&mapp->dam_active_set, &mapp->dam_stable_set, 11834c06356bSdh142964 &delta)) { 11841b115575SJohn Danielson /* 11851b115575SJohn Danielson * no difference 11861b115575SJohn Danielson */ 11874c06356bSdh142964 bitset_zero(&mapp->dam_stable_set); 11881b115575SJohn Danielson mapp->dam_flags &= ~DAM_SPEND; 11891b115575SJohn Danielson cv_signal(&mapp->dam_cv); 11901b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 11914c06356bSdh142964 bitset_fini(&uncfg); 11924c06356bSdh142964 bitset_fini(&cfg); 11934c06356bSdh142964 bitset_fini(&delta); 11941b115575SJohn Danielson DTRACE_PROBE2(damap__stabilize__map__nochange, char *, 11951b115575SJohn Danielson mapp->dam_name, dam_t *, mapp); 11964c06356bSdh142964 return; 11974c06356bSdh142964 } 11981b115575SJohn Danielson 11991b115575SJohn Danielson /* 12001b115575SJohn Danielson * compute the sets of addresses to be activated and deactivated 12011b115575SJohn Danielson */ 12024c06356bSdh142964 has_cfg = bitset_and(&delta, &mapp->dam_stable_set, &cfg); 12034c06356bSdh142964 has_uncfg = bitset_and(&delta, &mapp->dam_active_set, &uncfg); 12041b115575SJohn Danielson 12051b115575SJohn Danielson /* 12061b115575SJohn Danielson * drop map lock while invoking callouts 12071b115575SJohn Danielson */ 12081b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 12091b115575SJohn Danielson 12101b115575SJohn Danielson /* 12111b115575SJohn Danielson * activate all newly stable addresss 12121b115575SJohn Danielson */ 12131b115575SJohn Danielson if (has_cfg) 12144c06356bSdh142964 dam_addrset_activate(mapp, &cfg); 12151b115575SJohn Danielson 12161b115575SJohn Danielson /* 12171b115575SJohn Danielson * deactivate addresss which are no longer in the map 12181b115575SJohn Danielson */ 12191b115575SJohn Danielson if (has_uncfg) 12201b115575SJohn Danielson dam_addrset_deactivate(mapp, &uncfg); 12211b115575SJohn Danielson 12221b115575SJohn Danielson 12231b115575SJohn Danielson /* 12241b115575SJohn Danielson * timestamp the last stable time and increment the kstat keeping 12251b115575SJohn Danielson * the # of of stable cycles for the map 12261b115575SJohn Danielson */ 12271b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 12284c06356bSdh142964 bitset_zero(&mapp->dam_stable_set); 12294c06356bSdh142964 mapp->dam_last_stable = gethrtime(); 12304c06356bSdh142964 mapp->dam_stable_cnt++; 12311b115575SJohn Danielson DAM_INCR_STAT(mapp, dam_cycles); 12321b115575SJohn Danielson 12331b115575SJohn Danielson /* 12341b115575SJohn Danielson * determine the number of stable addresses 12351b115575SJohn Danielson * and update the n_active kstat for this map 12361b115575SJohn Danielson */ 12371b115575SJohn Danielson for (i = 1, n_active = 0; i < mapp->dam_high; i++) 12381b115575SJohn Danielson if (bitset_in_set(&mapp->dam_active_set, i)) 12391b115575SJohn Danielson n_active++; 12401b115575SJohn Danielson DAM_SET_STAT(mapp, dam_active, n_active); 12411b115575SJohn Danielson 12421b115575SJohn Danielson DTRACE_PROBE3(damap__map__stable__end, char *, mapp->dam_name, 12431b115575SJohn Danielson dam_t *, mapp, int, n_active); 12441b115575SJohn Danielson 12451b115575SJohn Danielson mapp->dam_flags &= ~DAM_SPEND; 12461b115575SJohn Danielson cv_signal(&mapp->dam_cv); 12471b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 12484c06356bSdh142964 bitset_fini(&uncfg); 12494c06356bSdh142964 bitset_fini(&cfg); 12504c06356bSdh142964 bitset_fini(&delta); 12514c06356bSdh142964 } 12524c06356bSdh142964 12534c06356bSdh142964 /* 12544c06356bSdh142964 * per-address stabilization timeout 12554c06356bSdh142964 */ 12564c06356bSdh142964 static void 12574c06356bSdh142964 dam_addr_stable_cb(void *arg) 12584c06356bSdh142964 { 12594c06356bSdh142964 dam_t *mapp = (dam_t *)arg; 12604c06356bSdh142964 int i; 12614c06356bSdh142964 dam_da_t *passp; 12624c06356bSdh142964 int spend = 0; 12634c06356bSdh142964 int tpend = 0; 12644c06356bSdh142964 int64_t next_tmov = mapp->dam_stabletmo; 12654c06356bSdh142964 int64_t tmo_delta; 1266d3d50737SRafael Vanoni int64_t ts = ddi_get_lbolt64(); 12674c06356bSdh142964 12681b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 12694c06356bSdh142964 if (mapp->dam_tid == 0) { 12701b115575SJohn Danielson DTRACE_PROBE2(damap__map__addr__stable__cancelled, char *, 12711b115575SJohn Danielson mapp->dam_name, dam_t *, mapp); 12721b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 12734c06356bSdh142964 return; 12744c06356bSdh142964 } 12754c06356bSdh142964 mapp->dam_tid = 0; 12761b115575SJohn Danielson 12774c06356bSdh142964 /* 12784c06356bSdh142964 * If still under stabilization, reschedule timeout, 12791b115575SJohn Danielson * otherwise dispatch the task to activate and deactivate the 12801b115575SJohn Danielson * new stable address 12814c06356bSdh142964 */ 12824c06356bSdh142964 if (mapp->dam_flags & DAM_SPEND) { 12831b115575SJohn Danielson DAM_INCR_STAT(mapp, dam_overrun); 12844c06356bSdh142964 mapp->dam_stable_overrun++; 12854c06356bSdh142964 dam_sched_tmo(mapp, mapp->dam_stabletmo, dam_addr_stable_cb); 12861b115575SJohn Danielson DTRACE_PROBE2(damap__map__addr__stable__overrun, char *, 12871b115575SJohn Danielson mapp->dam_name, dam_t *, mapp); 12881b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 12894c06356bSdh142964 return; 12904c06356bSdh142964 } 12914c06356bSdh142964 12921b115575SJohn Danielson DAM_SET_STAT(mapp, dam_overrun, 0); 12931b115575SJohn Danielson mapp->dam_stable_overrun = 0; 12941b115575SJohn Danielson 12951b115575SJohn Danielson /* 12961b115575SJohn Danielson * copy the current active set to the stable map 12971b115575SJohn Danielson * for each address being reported, decrement its 12981b115575SJohn Danielson * stabilize deadline, and if stable, add or remove the 12991b115575SJohn Danielson * address from the stable set 13001b115575SJohn Danielson */ 13014c06356bSdh142964 bitset_copy(&mapp->dam_active_set, &mapp->dam_stable_set); 13024c06356bSdh142964 for (i = 1; i < mapp->dam_high; i++) { 13034c06356bSdh142964 if (!bitset_in_set(&mapp->dam_report_set, i)) 13044c06356bSdh142964 continue; 13054c06356bSdh142964 passp = ddi_get_soft_state(mapp->dam_da, i); 13064c06356bSdh142964 ASSERT(passp); 13074c06356bSdh142964 13084c06356bSdh142964 /* report has stabilized */ 13094c06356bSdh142964 if (passp->da_deadline <= ts) { 13104c06356bSdh142964 bitset_del(&mapp->dam_report_set, i); 13111b115575SJohn Danielson if (passp->da_flags & DA_RELE) 13124c06356bSdh142964 bitset_del(&mapp->dam_stable_set, i); 13131b115575SJohn Danielson else 13144c06356bSdh142964 bitset_add(&mapp->dam_stable_set, i); 13154c06356bSdh142964 spend++; 13164c06356bSdh142964 continue; 13174c06356bSdh142964 } 13184c06356bSdh142964 13194c06356bSdh142964 /* 13201b115575SJohn Danielson * not stabilized, determine next map timeout 13214c06356bSdh142964 */ 13224c06356bSdh142964 tpend++; 13234c06356bSdh142964 tmo_delta = passp->da_deadline - ts; 13244c06356bSdh142964 if (tmo_delta < next_tmov) 13254c06356bSdh142964 next_tmov = tmo_delta; 13264c06356bSdh142964 } 13274c06356bSdh142964 13284c06356bSdh142964 /* 13291b115575SJohn Danielson * schedule system_taskq activation of stabilized reports 13304c06356bSdh142964 */ 13314c06356bSdh142964 if (spend) { 13321b115575SJohn Danielson if (taskq_dispatch(system_taskq, dam_stabilize_map, 13331b115575SJohn Danielson mapp, TQ_NOSLEEP)) { 13341b115575SJohn Danielson mapp->dam_flags |= DAM_SPEND; 13351b115575SJohn Danielson DTRACE_PROBE2(damap__map__addr__stable__start, char *, 13361b115575SJohn Danielson mapp->dam_name, dam_t *, mapp); 13371b115575SJohn Danielson } else { 13384c06356bSdh142964 tpend++; 13394c06356bSdh142964 } 13401b115575SJohn Danielson } 13414c06356bSdh142964 13424c06356bSdh142964 /* 13431b115575SJohn Danielson * reschedule the stabilization timer if there are reports 13441b115575SJohn Danielson * still pending 13454c06356bSdh142964 */ 13464c06356bSdh142964 if (tpend) 13474c06356bSdh142964 dam_sched_tmo(mapp, (clock_t)next_tmov, dam_addr_stable_cb); 13481b115575SJohn Danielson 13491b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 13504c06356bSdh142964 } 13514c06356bSdh142964 13524c06356bSdh142964 /* 13531b115575SJohn Danielson * fullset stabilization timeout callback 13544c06356bSdh142964 */ 13554c06356bSdh142964 static void 13561b115575SJohn Danielson dam_addrset_stable_cb(void *arg) 13574c06356bSdh142964 { 13584c06356bSdh142964 dam_t *mapp = (dam_t *)arg; 13594c06356bSdh142964 13601b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 13614c06356bSdh142964 if (mapp->dam_tid == 0) { 13621b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 13631b115575SJohn Danielson DTRACE_PROBE2(damap__map__addrset__stable__cancelled, 13641b115575SJohn Danielson char *, mapp->dam_name, dam_t *, mapp); 13654c06356bSdh142964 return; 13664c06356bSdh142964 } 13674c06356bSdh142964 mapp->dam_tid = 0; 13684c06356bSdh142964 13694c06356bSdh142964 /* 13701b115575SJohn Danielson * If map still underoing stabilization reschedule timeout, 13711b115575SJohn Danielson * else dispatch the task to configure the new stable set of 13721b115575SJohn Danielson * addresses. 13734c06356bSdh142964 */ 13741b115575SJohn Danielson if ((mapp->dam_flags & DAM_SPEND) || (taskq_dispatch(system_taskq, 13751b115575SJohn Danielson dam_stabilize_map, mapp, TQ_NOSLEEP) == NULL)) { 13761b115575SJohn Danielson DAM_INCR_STAT(mapp, dam_overrun); 13774c06356bSdh142964 mapp->dam_stable_overrun++; 13781b115575SJohn Danielson dam_sched_tmo(mapp, mapp->dam_stabletmo, dam_addrset_stable_cb); 13791b115575SJohn Danielson DTRACE_PROBE2(damap__map__addrset__stable__overrun, char *, 13801b115575SJohn Danielson mapp->dam_name, dam_t *, mapp); 13811b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 13821b115575SJohn Danielson return; 13831b115575SJohn Danielson } 13841b115575SJohn Danielson 13851b115575SJohn Danielson DAM_SET_STAT(mapp, dam_overrun, 0); 13861b115575SJohn Danielson mapp->dam_stable_overrun = 0; 13874c06356bSdh142964 bitset_copy(&mapp->dam_report_set, &mapp->dam_stable_set); 13884c06356bSdh142964 bitset_zero(&mapp->dam_report_set); 13891b115575SJohn Danielson mapp->dam_flags |= DAM_SPEND; 13901b115575SJohn Danielson mapp->dam_flags &= ~DAM_SETADD; 13911b115575SJohn Danielson DTRACE_PROBE2(damap__map__addrset__stable__start, char *, 13921b115575SJohn Danielson mapp->dam_name, dam_t *, mapp); 13931b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 13944c06356bSdh142964 } 13954c06356bSdh142964 13964c06356bSdh142964 /* 13971b115575SJohn Danielson * schedule map timeout 'tmo_ms' ticks 13981b115575SJohn Danielson * if map timer is currently running, cancel if tmo_ms == 0 13994c06356bSdh142964 */ 14004c06356bSdh142964 static void 14014c06356bSdh142964 dam_sched_tmo(dam_t *mapp, clock_t tmo_ms, void (*tmo_cb)()) 14024c06356bSdh142964 { 14034c06356bSdh142964 timeout_id_t tid; 14044c06356bSdh142964 14051b115575SJohn Danielson DTRACE_PROBE3(damap__sched__tmo, char *, mapp->dam_name, dam_t *, mapp, 14061b115575SJohn Danielson clock_t, tmo_ms); 14074c06356bSdh142964 14081b115575SJohn Danielson ASSERT(mutex_owned(&mapp->dam_lock)); 14091b115575SJohn Danielson if ((tid = mapp->dam_tid) != 0) { 14101b115575SJohn Danielson if (tmo_ms == 0) { 14111b115575SJohn Danielson mapp->dam_tid = 0; 14121b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 14131b115575SJohn Danielson (void) untimeout(tid); 14141b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 14151b115575SJohn Danielson } 14161b115575SJohn Danielson } else { 14174c06356bSdh142964 if (tmo_cb && (tmo_ms != 0)) 14184c06356bSdh142964 mapp->dam_tid = timeout(tmo_cb, mapp, tmo_ms); 14194c06356bSdh142964 } 14201b115575SJohn Danielson } 14214c06356bSdh142964 14224c06356bSdh142964 /* 14231b115575SJohn Danielson * report addition or removal of an address 14244c06356bSdh142964 */ 14254c06356bSdh142964 static void 14261b115575SJohn Danielson dam_addr_report(dam_t *mapp, dam_da_t *passp, id_t addrid, int rpt_type) 14274c06356bSdh142964 { 14281b115575SJohn Danielson char *addrstr = damap_id2addr((damap_t *)mapp, addrid); 14291b115575SJohn Danielson 14301b115575SJohn Danielson DTRACE_PROBE4(damap__addr__report, char *, mapp->dam_name, 14311b115575SJohn Danielson char *, addrstr, dam_t *, mapp, int, rpt_type); 14321b115575SJohn Danielson 14331b115575SJohn Danielson ASSERT(mutex_owned(&mapp->dam_lock)); 14344c06356bSdh142964 ASSERT(!DAM_IN_REPORT(mapp, addrid)); 14354c06356bSdh142964 passp->da_last_report = gethrtime(); 14364c06356bSdh142964 mapp->dam_last_update = gethrtime(); 14374c06356bSdh142964 passp->da_report_cnt++; 1438d3d50737SRafael Vanoni passp->da_deadline = ddi_get_lbolt64() + mapp->dam_stabletmo; 14391b115575SJohn Danielson if (rpt_type == RPT_ADDR_DEL) 14404c06356bSdh142964 passp->da_flags |= DA_RELE; 14411b115575SJohn Danielson else if (rpt_type == RPT_ADDR_ADD) 14424c06356bSdh142964 passp->da_flags &= ~DA_RELE; 14434c06356bSdh142964 bitset_add(&mapp->dam_report_set, addrid); 14444c06356bSdh142964 dam_sched_tmo(mapp, mapp->dam_stabletmo, dam_addr_stable_cb); 14454c06356bSdh142964 } 14464c06356bSdh142964 14474c06356bSdh142964 /* 14484c06356bSdh142964 * release an address report 14494c06356bSdh142964 */ 14504c06356bSdh142964 static void 14511b115575SJohn Danielson dam_addr_report_release(dam_t *mapp, id_t addrid) 14524c06356bSdh142964 { 14534c06356bSdh142964 dam_da_t *passp; 14541b115575SJohn Danielson char *addrstr = damap_id2addr((damap_t *)mapp, addrid); 14554c06356bSdh142964 14561b115575SJohn Danielson DTRACE_PROBE3(damap__addr__report__release, char *, mapp->dam_name, 14571b115575SJohn Danielson char *, addrstr, dam_t *, mapp); 14581b115575SJohn Danielson 14591b115575SJohn Danielson ASSERT(mutex_owned(&mapp->dam_lock)); 14604c06356bSdh142964 passp = ddi_get_soft_state(mapp->dam_da, addrid); 14614c06356bSdh142964 ASSERT(passp); 14621b115575SJohn Danielson /* 14631b115575SJohn Danielson * clear the report bit 14641b115575SJohn Danielson * if the address has a registered deactivation handler and 1465*9aed1621SDavid Hollister * we are holding a private data pointer and the address has not 1466*9aed1621SDavid Hollister * stabilized, deactivate the address (private data). 14671b115575SJohn Danielson */ 14681b115575SJohn Danielson bitset_del(&mapp->dam_report_set, addrid); 1469*9aed1621SDavid Hollister if (!DAM_IS_STABLE(mapp, addrid) && mapp->dam_deactivate_cb && 1470*9aed1621SDavid Hollister passp->da_ppriv_rpt) { 14711b115575SJohn Danielson mutex_exit(&mapp->dam_lock); 14721b115575SJohn Danielson (*mapp->dam_deactivate_cb)(mapp->dam_activate_arg, 14731b115575SJohn Danielson ddi_strid_id2str(mapp->dam_addr_hash, addrid), 1474*9aed1621SDavid Hollister addrid, passp->da_ppriv_rpt, DAMAP_DEACT_RSN_UNSTBL); 14751b115575SJohn Danielson mutex_enter(&mapp->dam_lock); 14761b115575SJohn Danielson } 14774c06356bSdh142964 passp->da_ppriv_rpt = NULL; 14784c06356bSdh142964 if (passp->da_nvl_rpt) 14794c06356bSdh142964 nvlist_free(passp->da_nvl_rpt); 14804c06356bSdh142964 } 14814c06356bSdh142964 14824c06356bSdh142964 /* 14834c06356bSdh142964 * return the map ID of an address 14844c06356bSdh142964 */ 14854c06356bSdh142964 static id_t 14864c06356bSdh142964 dam_get_addrid(dam_t *mapp, char *address) 14874c06356bSdh142964 { 14884c06356bSdh142964 damap_id_t addrid; 14894c06356bSdh142964 dam_da_t *passp; 14904c06356bSdh142964 14911b115575SJohn Danielson ASSERT(mutex_owned(&mapp->dam_lock)); 14924c06356bSdh142964 if ((addrid = ddi_strid_str2id(mapp->dam_addr_hash, address)) == 0) { 14931b115575SJohn Danielson if ((addrid = ddi_strid_alloc(mapp->dam_addr_hash, 14944c06356bSdh142964 address)) == (damap_id_t)0) { 14954c06356bSdh142964 return (0); 14964c06356bSdh142964 } 14974c06356bSdh142964 if (ddi_soft_state_zalloc(mapp->dam_da, addrid) != 14984c06356bSdh142964 DDI_SUCCESS) { 14994c06356bSdh142964 ddi_strid_free(mapp->dam_addr_hash, addrid); 15004c06356bSdh142964 return (0); 15014c06356bSdh142964 } 15021b115575SJohn Danielson 15034c06356bSdh142964 if (addrid >= mapp->dam_high) 15044c06356bSdh142964 mapp->dam_high = addrid + 1; 15051b115575SJohn Danielson 15061b115575SJohn Danielson /* 15071b115575SJohn Danielson * expand bitmaps if ID has outgrown old map size 15081b115575SJohn Danielson */ 15091b115575SJohn Danielson if (mapp->dam_high > mapp->dam_size) { 15101b115575SJohn Danielson mapp->dam_size = mapp->dam_size + DAM_SIZE_BUMP; 15111b115575SJohn Danielson bitset_resize(&mapp->dam_active_set, mapp->dam_size); 15121b115575SJohn Danielson bitset_resize(&mapp->dam_stable_set, mapp->dam_size); 15131b115575SJohn Danielson bitset_resize(&mapp->dam_report_set, mapp->dam_size); 15144c06356bSdh142964 } 15151b115575SJohn Danielson 15164c06356bSdh142964 passp = ddi_get_soft_state(mapp->dam_da, addrid); 15171b115575SJohn Danielson passp->da_ref = 1; 15181b115575SJohn Danielson passp->da_addr = ddi_strid_id2str(mapp->dam_addr_hash, 15191b115575SJohn Danielson addrid); /* for mdb */ 15201b115575SJohn Danielson } 15214c06356bSdh142964 return (addrid); 15224c06356bSdh142964 } 15234c06356bSdh142964 15244c06356bSdh142964 /* 15254c06356bSdh142964 * create and install map statistics 15264c06356bSdh142964 */ 15274c06356bSdh142964 static int 15284c06356bSdh142964 dam_kstat_create(dam_t *mapp) 15294c06356bSdh142964 { 15304c06356bSdh142964 kstat_t *mapsp; 15314c06356bSdh142964 struct dam_kstats *statsp; 15324c06356bSdh142964 15334c06356bSdh142964 mapsp = kstat_create("dam", 0, mapp->dam_name, "damap", 15344c06356bSdh142964 KSTAT_TYPE_NAMED, 15354c06356bSdh142964 sizeof (struct dam_kstats) / sizeof (kstat_named_t), 0); 15361b115575SJohn Danielson 15371b115575SJohn Danielson if (mapsp == NULL) 15384c06356bSdh142964 return (DDI_FAILURE); 15394c06356bSdh142964 15404c06356bSdh142964 statsp = (struct dam_kstats *)mapsp->ks_data; 15411b115575SJohn Danielson kstat_named_init(&statsp->dam_cycles, "cycles", KSTAT_DATA_UINT32); 15421b115575SJohn Danielson kstat_named_init(&statsp->dam_overrun, "overrun", KSTAT_DATA_UINT32); 15431b115575SJohn Danielson kstat_named_init(&statsp->dam_jitter, "jitter", KSTAT_DATA_UINT32); 15441b115575SJohn Danielson kstat_named_init(&statsp->dam_active, "active", KSTAT_DATA_UINT32); 15454c06356bSdh142964 kstat_install(mapsp); 15464c06356bSdh142964 mapp->dam_kstatsp = mapsp; 15474c06356bSdh142964 return (DDI_SUCCESS); 15484c06356bSdh142964 } 1549