2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <assert.h>
2N/A#include <sys/types.h>
2N/A#include <libdevinfo.h>
2N/A#include <fm/topo_mod.h>
2N/A#include <pcibus.h>
2N/A#include <did.h>
2N/A
2N/A#include "did_impl.h"
2N/A#include "did_props.h"
2N/A
2N/Astatic did_hash_t *did_hash_create(topo_mod_t *);
2N/Astatic void did_hash_destroy(did_hash_t *);
2N/A
2N/Aint
2N/Adid_hash_init(topo_mod_t *hdl)
2N/A{
2N/A did_hash_t *dh = did_hash_create(hdl);
2N/A
2N/A if (dh != NULL) {
2N/A topo_mod_setspecific(hdl, (void *) dh);
2N/A return (0);
2N/A } else {
2N/A return (-1);
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Adid_hash_fini(topo_mod_t *mod)
2N/A{
2N/A did_hash_t *dh = (did_hash_t *)topo_mod_getspecific(mod);
2N/A
2N/A topo_mod_setspecific(mod, NULL);
2N/A if (dh == NULL)
2N/A return;
2N/A did_hash_destroy(dh);
2N/A}
2N/A
2N/Astatic uint64_t
2N/Adid_dnhash(di_node_t key)
2N/A{
2N/A static uint64_t key_divisor = 0;
2N/A uint64_t keyn;
2N/A
2N/A /*
2N/A * A bit naughty here, we're aware that a di_info_t is a
2N/A * pointer to a struct. For our hashing, we want use the size
2N/A * of that struct, which we determine here, somewhat
2N/A * impolitely.
2N/A */
2N/A if (key_divisor == 0)
2N/A key_divisor = sizeof (*key);
2N/A
2N/A keyn = (uintptr_t)key;
2N/A
2N/A return (keyn / key_divisor);
2N/A}
2N/A
2N/Astatic did_hash_t *
2N/Adid_hash_create(topo_mod_t *hdl)
2N/A{
2N/A did_hash_t *r = topo_mod_zalloc(hdl, sizeof (did_hash_t));
2N/A
2N/A if (r == NULL) {
2N/A (void) topo_mod_seterrno(hdl, EMOD_NOMEM);
2N/A return (NULL);
2N/A }
2N/A r->dph_mod = hdl;
2N/A r->dph_hashlen = REC_HASHLEN;
2N/A r->dph_hash = topo_mod_zalloc(hdl,
2N/A r->dph_hashlen * sizeof (did_t *));
2N/A if (r->dph_hash == NULL) {
2N/A topo_mod_free(hdl, r, sizeof (did_hash_t));
2N/A (void) topo_mod_seterrno(hdl, EMOD_NOMEM);
2N/A return (NULL);
2N/A }
2N/A return (r);
2N/A}
2N/A
2N/Astatic void
2N/Adid_hash_destroy(did_hash_t *ht)
2N/A{
2N/A did_t *e, *n;
2N/A int idx;
2N/A
2N/A if (ht == NULL)
2N/A return;
2N/A for (idx = 0; idx < ht->dph_hashlen; idx++) {
2N/A for (e = ht->dph_hash[idx]; e != NULL; ) {
2N/A n = e->dp_next;
2N/A did_destroy(e);
2N/A e = n;
2N/A }
2N/A }
2N/A topo_mod_free(ht->dph_mod,
2N/A ht->dph_hash, ht->dph_hashlen * sizeof (did_t *));
2N/A topo_mod_free(ht->dph_mod, ht, sizeof (did_hash_t));
2N/A}
2N/A
2N/Avoid
2N/Adid_hash_insert(topo_mod_t *mp, di_node_t key, did_t *new)
2N/A{
2N/A did_hash_t *tab = (did_hash_t *)topo_mod_getspecific(mp);
2N/A did_t *assertchk;
2N/A int idx = did_dnhash(key) % tab->dph_hashlen;
2N/A
2N/A tab->dph_nelems++;
2N/A did_hold(new);
2N/A topo_mod_dprintf(tab->dph_mod, "Insert [key=%p] into %p, bucket %d\n",
2N/A key, (void *)tab, idx);
2N/A if (tab->dph_hash[idx] == NULL) {
2N/A tab->dph_hash[idx] = new;
2N/A topo_mod_dprintf(tab->dph_mod, "first entry.\n");
2N/A } else {
2N/A /*
2N/A * We should not be putting in a duplicate entry
2N/A */
2N/A for (assertchk = tab->dph_hash[idx];
2N/A assertchk != NULL;
2N/A assertchk = assertchk->dp_next)
2N/A assert(assertchk->dp_src != key);
2N/A new->dp_next = tab->dph_hash[idx];
2N/A tab->dph_hash[idx] = new;
2N/A }
2N/A}
2N/A
2N/Adid_t *
2N/Adid_hash_lookup(topo_mod_t *mp, di_node_t key)
2N/A{
2N/A did_t *e;
2N/A did_hash_t *tab = (did_hash_t *)topo_mod_getspecific(mp);
2N/A int idx = did_dnhash(key) % tab->dph_hashlen;
2N/A
2N/A e = tab->dph_hash[idx];
2N/A while (e != NULL) {
2N/A if (e->dp_src == key) {
2N/A did_hold(e);
2N/A return (e);
2N/A }
2N/A e = e->dp_next;
2N/A }
2N/A return (NULL);
2N/A}