xref: /titanic_50/usr/src/lib/fm/topo/modules/common/pcibus/did_hash.c (revision 0eb822a1c0c2bea495647510b75f77f0e57633eb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <assert.h>
32 #include <sys/types.h>
33 #include <libdevinfo.h>
34 #include <fm/topo_mod.h>
35 #include <pcibus.h>
36 #include <did.h>
37 
38 #include "did_impl.h"
39 #include "did_props.h"
40 
41 static did_hash_t *did_hash_create(topo_mod_t *);
42 static void did_hash_destroy(did_hash_t *);
43 
44 int
did_hash_init(topo_mod_t * hdl)45 did_hash_init(topo_mod_t *hdl)
46 {
47 	did_hash_t *dh = did_hash_create(hdl);
48 
49 	if (dh != NULL) {
50 		topo_mod_setspecific(hdl, (void *) dh);
51 		return (0);
52 	} else {
53 		return (-1);
54 	}
55 }
56 
57 void
did_hash_fini(topo_mod_t * mod)58 did_hash_fini(topo_mod_t *mod)
59 {
60 	did_hash_t *dh = (did_hash_t *)topo_mod_getspecific(mod);
61 
62 	topo_mod_setspecific(mod, NULL);
63 	if (dh == NULL)
64 		return;
65 	did_hash_destroy(dh);
66 }
67 
68 static uint64_t
did_dnhash(di_node_t key)69 did_dnhash(di_node_t key)
70 {
71 	static uint64_t key_divisor = 0;
72 	uint64_t keyn;
73 
74 	/*
75 	 * A bit naughty here, we're aware that a di_info_t is a
76 	 * pointer to a struct.  For our hashing, we want use the size
77 	 * of that struct, which we determine here, somewhat
78 	 * impolitely.
79 	 */
80 	if (key_divisor == 0)
81 		key_divisor = sizeof (*key);
82 
83 	keyn = (uintptr_t)key;
84 
85 	return (keyn / key_divisor);
86 }
87 
88 static did_hash_t *
did_hash_create(topo_mod_t * hdl)89 did_hash_create(topo_mod_t *hdl)
90 {
91 	did_hash_t *r = topo_mod_zalloc(hdl, sizeof (did_hash_t));
92 
93 	if (r == NULL) {
94 		(void) topo_mod_seterrno(hdl, EMOD_NOMEM);
95 		return (NULL);
96 	}
97 	r->dph_mod = hdl;
98 	r->dph_hashlen = REC_HASHLEN;
99 	r->dph_hash = topo_mod_zalloc(hdl,
100 	    r->dph_hashlen * sizeof (did_t *));
101 	if (r->dph_hash == NULL) {
102 		topo_mod_free(hdl, r, sizeof (did_hash_t));
103 		(void) topo_mod_seterrno(hdl, EMOD_NOMEM);
104 		return (NULL);
105 	}
106 	return (r);
107 }
108 
109 static void
did_hash_destroy(did_hash_t * ht)110 did_hash_destroy(did_hash_t *ht)
111 {
112 	did_t *e, *n;
113 	int idx;
114 
115 	if (ht == NULL)
116 		return;
117 	for (idx = 0; idx < ht->dph_hashlen; idx++) {
118 		for (e = ht->dph_hash[idx]; e != NULL; ) {
119 			n = e->dp_next;
120 			did_destroy(e);
121 			e = n;
122 		}
123 	}
124 	topo_mod_free(ht->dph_mod,
125 	    ht->dph_hash, ht->dph_hashlen * sizeof (did_t *));
126 	topo_mod_free(ht->dph_mod, ht, sizeof (did_hash_t));
127 }
128 
129 void
did_hash_insert(topo_mod_t * mp,di_node_t key,did_t * new)130 did_hash_insert(topo_mod_t *mp, di_node_t key, did_t *new)
131 {
132 	did_hash_t *tab = (did_hash_t *)topo_mod_getspecific(mp);
133 	did_t *assertchk;
134 	int idx = did_dnhash(key) % tab->dph_hashlen;
135 
136 	tab->dph_nelems++;
137 	did_hold(new);
138 	topo_mod_dprintf(tab->dph_mod, "Insert [key=%p] into %p, bucket %d\n",
139 	    key, (void *)tab, idx);
140 	if (tab->dph_hash[idx] == NULL) {
141 		tab->dph_hash[idx] = new;
142 		topo_mod_dprintf(tab->dph_mod, "first entry.\n");
143 	} else {
144 		/*
145 		 * We should not be putting in a duplicate entry
146 		 */
147 		for (assertchk = tab->dph_hash[idx];
148 		    assertchk != NULL;
149 		    assertchk = assertchk->dp_next)
150 			assert(assertchk->dp_src != key);
151 		new->dp_next = tab->dph_hash[idx];
152 		tab->dph_hash[idx] = new;
153 	}
154 }
155 
156 did_t *
did_hash_lookup(topo_mod_t * mp,di_node_t key)157 did_hash_lookup(topo_mod_t *mp, di_node_t key)
158 {
159 	did_t *e;
160 	did_hash_t *tab = (did_hash_t *)topo_mod_getspecific(mp);
161 	int idx = did_dnhash(key) % tab->dph_hashlen;
162 
163 	e = tab->dph_hash[idx];
164 	while (e != NULL) {
165 		if (e->dp_src == key) {
166 			did_hold(e);
167 			return (e);
168 		}
169 		e = e->dp_next;
170 	}
171 	return (NULL);
172 }
173