17aec1d6eScindi /*
27aec1d6eScindi  * CDDL HEADER START
37aec1d6eScindi  *
47aec1d6eScindi  * The contents of this file are subject to the terms of the
57aec1d6eScindi  * Common Development and Distribution License (the "License").
67aec1d6eScindi  * You may not use this file except in compliance with the License.
77aec1d6eScindi  *
87aec1d6eScindi  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97aec1d6eScindi  * or http://www.opensolaris.org/os/licensing.
107aec1d6eScindi  * See the License for the specific language governing permissions
117aec1d6eScindi  * and limitations under the License.
127aec1d6eScindi  *
137aec1d6eScindi  * When distributing Covered Code, include this CDDL HEADER in each
147aec1d6eScindi  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157aec1d6eScindi  * If applicable, add the following below this CDDL HEADER, with the
167aec1d6eScindi  * fields enclosed by brackets "[]" replaced with your own identifying
177aec1d6eScindi  * information: Portions Copyright [yyyy] [name of copyright owner]
187aec1d6eScindi  *
197aec1d6eScindi  * CDDL HEADER END
207aec1d6eScindi  */
217aec1d6eScindi 
227aec1d6eScindi /*
237aec1d6eScindi  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247aec1d6eScindi  * Use is subject to license terms.
257aec1d6eScindi  */
267aec1d6eScindi 
277aec1d6eScindi #include <stdio.h>
287aec1d6eScindi #include <stdlib.h>
297aec1d6eScindi #include <assert.h>
307aec1d6eScindi #include <sys/types.h>
317aec1d6eScindi #include <libdevinfo.h>
32*0eb822a1Scindi #include <fm/topo_mod.h>
33*0eb822a1Scindi #include <pcibus.h>
34*0eb822a1Scindi #include <did.h>
357aec1d6eScindi 
367aec1d6eScindi #include "did_impl.h"
377aec1d6eScindi #include "did_props.h"
387aec1d6eScindi 
39*0eb822a1Scindi static did_hash_t *did_hash_create(topo_mod_t *);
40*0eb822a1Scindi static void did_hash_destroy(did_hash_t *);
41*0eb822a1Scindi 
42*0eb822a1Scindi int
did_hash_init(topo_mod_t * hdl)437aec1d6eScindi did_hash_init(topo_mod_t *hdl)
447aec1d6eScindi {
45*0eb822a1Scindi 	did_hash_t *dh = did_hash_create(hdl);
46*0eb822a1Scindi 
47*0eb822a1Scindi 	if (dh != NULL) {
48*0eb822a1Scindi 		topo_mod_setspecific(hdl, (void *) dh);
49*0eb822a1Scindi 		return (0);
50*0eb822a1Scindi 	} else {
51*0eb822a1Scindi 		return (-1);
52*0eb822a1Scindi 	}
537aec1d6eScindi }
547aec1d6eScindi 
557aec1d6eScindi void
did_hash_fini(topo_mod_t * mod)56*0eb822a1Scindi did_hash_fini(topo_mod_t *mod)
577aec1d6eScindi {
58*0eb822a1Scindi 	did_hash_t *dh = (did_hash_t *)topo_mod_getspecific(mod);
59*0eb822a1Scindi 
60*0eb822a1Scindi 	topo_mod_setspecific(mod, NULL);
617aec1d6eScindi 	if (dh == NULL)
627aec1d6eScindi 		return;
637aec1d6eScindi 	did_hash_destroy(dh);
647aec1d6eScindi }
657aec1d6eScindi 
667aec1d6eScindi static uint64_t
did_dnhash(di_node_t key)677aec1d6eScindi did_dnhash(di_node_t key)
687aec1d6eScindi {
697aec1d6eScindi 	static uint64_t key_divisor = 0;
707aec1d6eScindi 	uint64_t keyn;
717aec1d6eScindi 
727aec1d6eScindi 	/*
737aec1d6eScindi 	 * A bit naughty here, we're aware that a di_info_t is a
747aec1d6eScindi 	 * pointer to a struct.  For our hashing, we want use the size
757aec1d6eScindi 	 * of that struct, which we determine here, somewhat
767aec1d6eScindi 	 * impolitely.
777aec1d6eScindi 	 */
787aec1d6eScindi 	if (key_divisor == 0)
797aec1d6eScindi 		key_divisor = sizeof (*key);
807aec1d6eScindi 
8180ab886dSwesolows 	keyn = (uintptr_t)key;
827aec1d6eScindi 
837aec1d6eScindi 	return (keyn / key_divisor);
847aec1d6eScindi }
857aec1d6eScindi 
86*0eb822a1Scindi static did_hash_t *
did_hash_create(topo_mod_t * hdl)877aec1d6eScindi did_hash_create(topo_mod_t *hdl)
887aec1d6eScindi {
897aec1d6eScindi 	did_hash_t *r = topo_mod_zalloc(hdl, sizeof (did_hash_t));
907aec1d6eScindi 
917aec1d6eScindi 	if (r == NULL) {
927aec1d6eScindi 		(void) topo_mod_seterrno(hdl, EMOD_NOMEM);
937aec1d6eScindi 		return (NULL);
947aec1d6eScindi 	}
957aec1d6eScindi 	r->dph_mod = hdl;
967aec1d6eScindi 	r->dph_hashlen = REC_HASHLEN;
977aec1d6eScindi 	r->dph_hash = topo_mod_zalloc(hdl,
987aec1d6eScindi 	    r->dph_hashlen * sizeof (did_t *));
997aec1d6eScindi 	if (r->dph_hash == NULL) {
1007aec1d6eScindi 		topo_mod_free(hdl, r, sizeof (did_hash_t));
1017aec1d6eScindi 		(void) topo_mod_seterrno(hdl, EMOD_NOMEM);
1027aec1d6eScindi 		return (NULL);
1037aec1d6eScindi 	}
1047aec1d6eScindi 	return (r);
1057aec1d6eScindi }
1067aec1d6eScindi 
107*0eb822a1Scindi static void
did_hash_destroy(did_hash_t * ht)1087aec1d6eScindi did_hash_destroy(did_hash_t *ht)
1097aec1d6eScindi {
1107aec1d6eScindi 	did_t *e, *n;
1117aec1d6eScindi 	int idx;
1127aec1d6eScindi 
1137aec1d6eScindi 	if (ht == NULL)
1147aec1d6eScindi 		return;
1157aec1d6eScindi 	for (idx = 0; idx < ht->dph_hashlen; idx++) {
1167aec1d6eScindi 		for (e = ht->dph_hash[idx]; e != NULL; ) {
1177aec1d6eScindi 			n = e->dp_next;
1187aec1d6eScindi 			did_destroy(e);
1197aec1d6eScindi 			e = n;
1207aec1d6eScindi 		}
1217aec1d6eScindi 	}
1227aec1d6eScindi 	topo_mod_free(ht->dph_mod,
1237aec1d6eScindi 	    ht->dph_hash, ht->dph_hashlen * sizeof (did_t *));
1247aec1d6eScindi 	topo_mod_free(ht->dph_mod, ht, sizeof (did_hash_t));
1257aec1d6eScindi }
1267aec1d6eScindi 
1277aec1d6eScindi void
did_hash_insert(topo_mod_t * mp,di_node_t key,did_t * new)128*0eb822a1Scindi did_hash_insert(topo_mod_t *mp, di_node_t key, did_t *new)
1297aec1d6eScindi {
130*0eb822a1Scindi 	did_hash_t *tab = (did_hash_t *)topo_mod_getspecific(mp);
1317aec1d6eScindi 	did_t *assertchk;
1327aec1d6eScindi 	int idx = did_dnhash(key) % tab->dph_hashlen;
1337aec1d6eScindi 
1347aec1d6eScindi 	tab->dph_nelems++;
1357aec1d6eScindi 	did_hold(new);
1367aec1d6eScindi 	topo_mod_dprintf(tab->dph_mod, "Insert [key=%p] into %p, bucket %d\n",
1377aec1d6eScindi 	    key, (void *)tab, idx);
1387aec1d6eScindi 	if (tab->dph_hash[idx] == NULL) {
1397aec1d6eScindi 		tab->dph_hash[idx] = new;
1407aec1d6eScindi 		topo_mod_dprintf(tab->dph_mod, "first entry.\n");
1417aec1d6eScindi 	} else {
1427aec1d6eScindi 		/*
1437aec1d6eScindi 		 * We should not be putting in a duplicate entry
1447aec1d6eScindi 		 */
1457aec1d6eScindi 		for (assertchk = tab->dph_hash[idx];
1467aec1d6eScindi 		    assertchk != NULL;
1477aec1d6eScindi 		    assertchk = assertchk->dp_next)
1487aec1d6eScindi 			assert(assertchk->dp_src != key);
1497aec1d6eScindi 		new->dp_next = tab->dph_hash[idx];
1507aec1d6eScindi 		tab->dph_hash[idx] = new;
1517aec1d6eScindi 	}
1527aec1d6eScindi }
1537aec1d6eScindi 
1547aec1d6eScindi did_t *
did_hash_lookup(topo_mod_t * mp,di_node_t key)155*0eb822a1Scindi did_hash_lookup(topo_mod_t *mp, di_node_t key)
1567aec1d6eScindi {
1577aec1d6eScindi 	did_t *e;
158*0eb822a1Scindi 	did_hash_t *tab = (did_hash_t *)topo_mod_getspecific(mp);
1597aec1d6eScindi 	int idx = did_dnhash(key) % tab->dph_hashlen;
1607aec1d6eScindi 
1617aec1d6eScindi 	e = tab->dph_hash[idx];
1627aec1d6eScindi 	while (e != NULL) {
1637aec1d6eScindi 		if (e->dp_src == key) {
1647aec1d6eScindi 			did_hold(e);
1657aec1d6eScindi 			return (e);
1667aec1d6eScindi 		}
1677aec1d6eScindi 		e = e->dp_next;
1687aec1d6eScindi 	}
1697aec1d6eScindi 	return (NULL);
1707aec1d6eScindi }
171