fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of this file are subject to the terms of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Common Development and Distribution License (the "License").
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You may not use this file except in compliance with the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or http://www.opensolaris.org/os/licensing.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When distributing Covered Code, include this CDDL HEADER in each
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If applicable, add the following below this CDDL HEADER, with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use is subject to license terms.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/types.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/cmn_err.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/errno.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/ksynch.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/kmem.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/ddi.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/varargs.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#if defined(DEBUG) && !defined(DS_DDICT)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/kobj.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/ncall/ncall.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define __NSC_GEN__
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "nsc_gen.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "nsc_mem.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "../nsctl.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef DS_DDICT
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "../contract.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic kcondvar_t _nsc_delay_cv;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic kmutex_t _nsc_delay_mutex;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic nsc_service_t *_nsc_services;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic kmutex_t _nsc_svc_mutex;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int _nsc_rmmap_inuse(nsc_rmmap_t *, ulong_t *, size_t *);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void _nsc_sprint_dec(char **, int, int, int);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void _nsc_sprint_hex(char **, unsigned int, int, int, int, int);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteclock_t HZ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteextern nsc_rmhdr_t *_nsc_rmhdr_ptr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_nsc_init_gen()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte HZ = drv_usectohz(1000000);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_decode_param(nsc_def_t *args, nsc_def_t *def, long *v)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_def_t *dp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (; def && def->name; def++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (dp = args; dp && dp->name; dp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strcmp(dp->name, def->name) == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte v[def->offset] = dp->value;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((!dp || !dp->name) && !v[def->offset])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte v[def->offset] = def->value;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteclock_t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_lbolt()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef _SunOS_5_6
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte clock_t lbolt;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte time_t time;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (drv_getparm(LBOLT, &lbolt) == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (lbolt);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (drv_getparm(TIME, &time) != 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((clock_t)0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte time %= (60 * 60 * 24 * 365);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (clock_t)(time * HZ);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (ddi_get_lbolt());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetime_t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_time()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte time_t time;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (drv_getparm(TIME, &time) != 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((time_t)0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (time);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_node_up(int node)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (node == ncall_self());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * HACK increment nodeid in data parameter
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_nodeid_data()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int data;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((data = nsc_node_id()) == 0 ? 1 : data);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_node_id(void)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (ncall_self());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortechar *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_node_name()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (ncall_nodename(ncall_self()));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * _nsc_rmmap_init (nsc_rmmap_t *map, char *name, int nslot,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * size_t size, ulong_t offset)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Initialise a global resource map.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Calling/Exit State:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Returns TRUE if the map was successfully created. Otherwise
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * returns FALSE.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Initialises a global resource map. If the map already exists
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the arguments are validated against it.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_nsc_rmmap_init(nsc_rmmap_t *map, char *name,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int nslot, size_t size, ulong_t offset)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_rmmap_t *nvmap = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!size)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_enter(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (_nsc_rm_nvmem_base)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nvmap = _nsc_global_nvmemmap_lookup(map);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!map->size)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte map->size = size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!map->inuse)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte map->inuse = nslot;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!map->offset)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte map->offset = offset;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!map->name[0])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) strncpy(map->name, name, _NSC_MAXNAME);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* actually we only need to do this if an update occurred above */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (nvmap) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) nsc_commit_mem(map, nvmap,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sizeof (nsc_rmmap_t), nsc_cm_errhdlr);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strncmp(map->name, name, _NSC_MAXNAME) ||
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (uint32_t)size != map->size || (int32_t)offset != map->offset) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ulong_t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * _nsc_rmmap_alloc (nsc_rmmap_t *map, char *name,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * size_t size, void (*alloc)())
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Allocate entry in a global resource map.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Calling/Exit State:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * On success, returns the base of the allocated area. Otherwise,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * returns NULL. The function 'alloc' will be called if the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * allocated area is not currently in use.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Allocates an entry in the global resource map. If the entry
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * already exists but is a different size an error is returned.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteulong_t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_nsc_rmmap_alloc(nsc_rmmap_t *map, char *name, size_t size, void (*alloc)())
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i, nslot = map[0].inuse;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t want = size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ulong_t offset;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_rmmap_t *nvmap = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!size)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_enter(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (_nsc_rm_nvmem_base)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nvmap = _nsc_global_nvmemmap_lookup(map);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 1; i < nslot; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!map[i].inuse || !map[i].size)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strncmp(map[i].name, name, _NSC_MAXNAME))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((uint32_t)size == map[i].size) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte map[i].inuse |= (1 << nsc_node_id());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (nvmap) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) nsc_commit_mem(&map[i], &nvmap[i],
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sizeof (nsc_rmmap_t), nsc_cm_errhdlr);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (map[i].offset);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte offset = map[0].offset;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while ((int32_t)offset < (map[0].offset + map[0].size)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (_nsc_rmmap_inuse(map, &offset, &want))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (size > want) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte offset += want;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte want = size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 1; i < nslot; i++)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!map[i].inuse || !map[i].size)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (i == nslot)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bzero(&map[i], sizeof (map[i]));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) strncpy(map[i].name, name, _NSC_MAXNAME);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte map[i].size = size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte map[i].offset = offset;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte map[i].inuse = (1 << nsc_node_id());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (nvmap) { /* update the map and hdr dirty bit. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) nsc_commit_mem(&map[i], &nvmap[i],
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sizeof (nsc_rmmap_t), nsc_cm_errhdlr);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (alloc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (*alloc)(offset, size);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (offset);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * _nsc_rmmap_free (nsc_rmmap_t *map, char *name)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Free entry in a global resource map.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Frees an entry in the global resource map.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_nsc_rmmap_free(nsc_rmmap_t *map, char *name, nsc_mem_t *mp)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i, nslot = map[0].inuse;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_rmmap_t *nvmap = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_enter(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (_nsc_rm_nvmem_base)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nvmap = _nsc_global_nvmemmap_lookup(map);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 1; i < nslot; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!map[i].inuse || !map[i].size)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strncmp(map[i].name, name, _NSC_MAXNAME))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte map[i].inuse &= ~(1 << nsc_node_id());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (nvmap) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * if dirty, set the inuse bit so this area
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * will not be _nsc_global_zero'd on restart.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (mp && (mp->type & NSC_MEM_NVDIRTY)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte map[i].inuse |= (1 << nsc_node_id());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) nsc_commit_mem(&map[i], &nvmap[i],
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sizeof (nsc_rmmap_t), nsc_cm_errhdlr);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana cmn_err(CE_WARN, "!nsctl: _nsc_rmmap_free: invalid free");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * size_t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * _nsc_rmmap_size (nsc_rmmap_t *map, char *name)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Find size of area in map.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Calling/Exit State:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Returns the size of the specified area in the map,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or 0 if it is currently unallocated.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesize_t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_nsc_rmmap_size(nsc_rmmap_t *map, char *name)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i, nslot = map[0].inuse;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t size = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_enter(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 1; i < nslot; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!map[i].inuse || !map[i].size)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strncmp(map[i].name, name, _NSC_MAXNAME) == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size = map[i].size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (size);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * size_t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * _nsc_rmmap_avail (nsc_rmmap_t *map)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Find available space in global resource map.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Calling/Exit State:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Returns the size of the largest available area in
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the global resource map.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesize_t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_nsc_rmmap_avail(nsc_rmmap_t *map)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t size, avail = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ulong_t offset;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_enter(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte offset = map[0].offset;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while ((int32_t)offset < (map[0].offset + map[0].size))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!_nsc_rmmap_inuse(map, &offset, &size)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (size > avail)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte avail = size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte offset += size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_global_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (avail);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * static int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * _nsc_rmmap_inuse (nsc_rmmap_t *map, ulong_t *offsetp, size_t *sizep)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Check if a section of the map is in use.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Calling/Exit State:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The global lock must be held across calls to the function.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Returns TRUE if the specified area is currently in use and
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * updates offset to point just past the section that was found
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * to be in use.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Otherwise, returns FALSE and updates size to reflect the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * amount of free space at the specified offset.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Checks the specified global map to determine if any part
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * of the area is in use.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_nsc_rmmap_inuse(nsc_rmmap_t *map, ulong_t *offsetp, size_t *sizep)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t avail, size = (*sizep);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ulong_t offset = (*offsetp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i, nslot;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nslot = map[0].inuse;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte avail = map[0].offset + map[0].size - offset;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 1; i < nslot; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!map[i].size || !map[i].inuse)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((int32_t)(offset + size) > map[i].offset &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (int32_t)offset < (map[i].offset + map[i].size)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (*offsetp) = map[i].offset + map[i].size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (map[i].offset >= (int32_t)offset)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (avail > map[i].offset - offset)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte avail = map[i].offset - offset;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (*sizep) = avail;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * nsc_delay_sig (clock_t tics)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Delay for a number of clock ticks.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Calling/Exit State:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Returns FALSE if the delay was interrupted by a
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * signal, TRUE otherwise.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Delays execution for the specified number of ticks
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or until a signal is received.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_delay_sig(clock_t tics)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte clock_t target, remain, rc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte target = nsc_lbolt() + tics;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_enter(&_nsc_delay_mutex);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* CONSTCOND */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte remain = target - nsc_lbolt();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (remain <= 0 || rc == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* timeout */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = cv_timedwait_sig(&_nsc_delay_cv,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte &_nsc_delay_mutex, target);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (rc == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* signalled */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_delay_mutex);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (FALSE);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_delay_mutex);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (TRUE);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * nsc_sprintf (char *s, char *fmt, ...)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * String printf.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Calling/Exit State:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Builds a NULL terminated string in the buffer
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * pointed to by 's', using the format 'fmt'.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Simple version of sprintf supporting fairly
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * basic formats.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* PRINTFLIKE2 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_sprintf(char *s, char *fmt, ...)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int alt, zero, len;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char c, *cp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_list p;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_start(p, fmt);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* CONSTCOND */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte alt = 0, zero = 0, len = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((c = *fmt++) != '%') {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *s++ = c;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((c = *fmt++) == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *s++ = '%';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte alt = (c == '#');
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (alt && !(c = *fmt++))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte zero = (c == '0');
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (zero && !(c = *fmt++))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while ((len ? '0' : '1') <= c && c <= '9') {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len = (len * 10) + (c - '0');
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!(c = *fmt++))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c == 's') {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cp = (char *)va_arg(p, caddr_t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (*cp)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *s++ = *cp++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c == 'd' || c == 'u') {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte _nsc_sprint_dec(&s, va_arg(p, int), zero, len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c == 'x' || c == 'X') {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte _nsc_sprint_hex(&s, va_arg(p, uint_t),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (c == 'X'), alt, zero, len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *s++ = '%';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (alt)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *s++ = '#';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (zero)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *s++ = '0';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (len)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte _nsc_sprint_dec(&s, len, 0, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *s++ = c;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (alt || zero || len) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *s++ = '%';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (alt)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *s++ = '#';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (zero)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *s++ = '0';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (len)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte _nsc_sprint_dec(&s, len, 0, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte va_end(p);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *s = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * static void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * _nsc_sprint_dec (char **sptr, int n, int zero, int len)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Decimal to string conversion.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Calling/Exit State:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Stores a character representation of 'n' in the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * buffer referenced by 'sptr' and updates the pointer
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * accordingly.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Generates a string representation of a signed decimal
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * integer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_nsc_sprint_dec(char **sptr, int n, int zero, int len)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unsigned int v = (n < 0) ? (-n) : n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char c[20];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0; v; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c[i] = (v % 10) + '0';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte v /= 10;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len -= (i ? i : 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (n < 0 && !zero)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (len--; len > 0; len--)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *(*sptr)++ = ' ';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (n < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *(*sptr)++ = '-';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len--;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (; len > 0; len--)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *(*sptr)++ = (zero ? '0' : ' ');
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!i)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *(*sptr)++ = '0';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (i--)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *(*sptr)++ = c[i];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * static void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * _nsc_sprint_hex (char **sptr, unsigned int v,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * int up, int alt, int zero, int len)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Hexadecimal to string conversion.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Calling/Exit State:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Stores a character representation of 'v' in the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * buffer referenced by 'sptr' and updates the pointer
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * accordingly.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Generates a string representation of an unsigned
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * hexadecimal integer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_nsc_sprint_hex(char **sptr, uint_t v, int up, int alt, int zero, int len)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *str = "0123456789abcdef";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char c[20];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (up)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte str = "0123456789ABCDEF";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0; v; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c[i] = str[(v % 16)];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte v /= 16;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (alt) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *(*sptr)++ = '0';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *(*sptr)++ = (up ? 'X' : 'x');
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (len -= (i ? i : 1); len > 0; len--)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *(*sptr)++ = (zero ? '0' : ' ');
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!i)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *(*sptr)++ = '0';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (i--)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *(*sptr)++ = c[i];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * char *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * nsc_strdup (char *s)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Duplicate string.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Calling/Exit State:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Returns the address of the new string.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Allocates a suitably sized area of memory and
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * copies the string into it. The string should be
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * free'd using nsc_strfree().
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortechar *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_strdup(char *s)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *cp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (s == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cp = nsc_kmem_alloc(strlen(s) + 1, KM_SLEEP, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) strcpy(cp, s);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (cp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * nsc_strfree (char *s)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Free string.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Frees a string previously allocated by nsc_strdup.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_strfree(char *s)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (s)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_kmem_free(s, strlen(s) + 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * nsc_strmatch (char *s, char *pat)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Match string against pattern.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Calling/Exit State:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Returns TRUE if the string matches against the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * pattern, FALSE otherwise.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Compares string against regular expression which
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * can contain '*', '?' and '[]' constructs.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_strmatch(char *s, char *pat)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int neg;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (; *pat; pat++, s++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*pat == '*') {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (*pat == '*')
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pat++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!*pat)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (; *s; s++)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*pat == '[' || *pat == '?' || *pat == *s)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (nsc_strmatch(s, pat))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!*s)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*pat == '[') {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((neg = (*++pat == '^')) != 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pat++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (*pat) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*pat == *s)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pat[1] == '-' && pat[2] != ']') {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*pat <= *s && *s <= pat[2])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pat += 2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*++pat == ']') {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (neg)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto lp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (*pat && *++pat != ']')
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!*pat || neg)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramanalp:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*pat != '?' && *pat != *s)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (!*s);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * uint64_t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * nsc_strhash(char *str)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Calculate a simple hash for the specified string
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Calling/Exit State:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Returns a simple hash of the NULL terminated string, str.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteuint64_t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_strhash(char *str)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint64_t hash = (uint64_t)0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (str == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (hash);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (*str != '\0') {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hash <<= 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hash += (uint64_t)*str;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte str++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (hash);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * nsc_fatal(void)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Fatal error stub function
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Calling/Exit State:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Returns EINVAL (non-DEBUG) or forces a panic.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This is a stub function suitable for default actions in
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * nsctl i/o provider definitions. It should be used when
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * calling the stub would be a programming error. The most
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * common reason for nsc_fatal() being called is that an
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * nsctl client module has called an nsc_fd_t i/o function
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * without the fd already reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The function will display a diagnostic message and when
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * built -DDEBUG will force a panic and display the textual
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * name of the symbol closest to the caller address of this
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * function.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_fatal()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void *caller = nsc_caller();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef DEBUG
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte caddr_t caller_sym = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ulong_t offset = 0UL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifndef DS_DDICT
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte caller_sym = kobj_getsymname((uintptr_t)caller, &offset);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif /* !DS_DDICT */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana cmn_err(CE_WARN, "!nsctl: nsc_fatal called at 0x%p (%s+0x%lx)",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte caller, caller_sym ? caller_sym : "?", offset);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Force TRAP due to NULL pointer dereference
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * - CE_PANIC can result in the stack trace being unreadable
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * by (k)adb.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *(int *)0 = 0x12345678;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else /* !DEBUG */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana cmn_err(CE_WARN, "!nsctl: nsc_fatal called at 0x%p", caller);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif /* DEBUG */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (EINVAL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint nsc_null() { return (0); }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint nsc_true() { return (1); }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint nsc_inval() { return (-1); }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint nsc_ioerr() { return (EIO); }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*ARGSUSED*/
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_commit_mem(void *src, void *dst, size_t len, nsc_mem_err_cb err_action)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int _nsc_nvmem_errs;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* ARGSUSED */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_cm_errhdlr(void *src, void *dst, size_t len, int errval)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte static int _nsc_baddma_already_seen = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!(_nsc_baddma_already_seen % 100)) {
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana cmn_err(CE_WARN, "!nsc_cm_errhdlr: media down, forced_wrthru");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte _nsc_baddma_already_seen += 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (_nsc_baddma_already_seen >= 100) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cmn_err(CE_WARN,
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana "!nsc_cm_errhdlr: this message "
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "displayed every 100 errors");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) nsc_node_hints_set(NSC_FORCED_WRTHRU);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte _nsc_nvmem_errs++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_nsc_init_svc(void)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_init(&_nsc_svc_mutex, NULL, MUTEX_DRIVER, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_init(&_nsc_delay_mutex, NULL, MUTEX_DRIVER, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cv_init(&_nsc_delay_cv, NULL, CV_DRIVER, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_nsc_deinit_svc(void)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (_nsc_services != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cmn_err(CE_PANIC,
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana "nsctl: services registered in _nsc_deinit_svc");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* NOTREACHED */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cv_destroy(&_nsc_delay_cv);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_destroy(&_nsc_delay_mutex);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_destroy(&_nsc_svc_mutex);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_svc_t *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_register_svc(char *name, void (*service_fn)(intptr_t))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_service_t *sp, *new;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_svc_t *svc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new = nsc_kmem_zalloc(sizeof (*new), KM_SLEEP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (new == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte svc = nsc_kmem_zalloc(sizeof (*svc), KM_SLEEP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (svc == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_kmem_free(new, sizeof (*new));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_enter(&_nsc_svc_mutex);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (sp = _nsc_services; sp != NULL; sp = sp->s_next)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strcmp(name, sp->s_name) == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (sp == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sp = new;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sp->s_name = nsc_strdup(name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (sp->s_name == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_svc_mutex);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_kmem_free(new, sizeof (*new));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_kmem_free(svc, sizeof (*svc));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rw_init(&sp->s_rwlock, NULL, RW_DRIVER, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sp->s_next = _nsc_services;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte _nsc_services = sp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rw_enter(&sp->s_rwlock, RW_WRITER);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte svc->svc_fn = service_fn;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte svc->svc_svc = sp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (svc->svc_fn != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte svc->svc_next = sp->s_servers;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sp->s_servers = svc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte svc->svc_next = sp->s_clients;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sp->s_clients = svc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rw_exit(&sp->s_rwlock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_svc_mutex);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (sp != new)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_kmem_free(new, sizeof (*new));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (svc);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_unregister_svc(nsc_svc_t *svc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_service_t *sp, **spp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_svc_t **svcp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (svc == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (EINVAL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sp = svc->svc_svc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (sp == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (EINVAL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_enter(&_nsc_svc_mutex);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rw_enter(&sp->s_rwlock, RW_WRITER);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte svcp = (svc->svc_fn == NULL) ? &sp->s_clients : &sp->s_servers;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (; *svcp; svcp = &((*svcp)->svc_next))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (svc == (*svcp))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*svcp)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (*svcp) = svc->svc_next;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_kmem_free(svc, sizeof (*svc));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (sp->s_servers == NULL && sp->s_clients == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (spp = &_nsc_services; *spp; spp = &((*spp)->s_next))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((*spp) == sp)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*spp)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (*spp) = sp->s_next;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rw_exit(&sp->s_rwlock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_svc_mutex);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rw_destroy(&sp->s_rwlock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_strfree(sp->s_name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_kmem_free(sp, sizeof (*sp));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rw_exit(&sp->s_rwlock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mutex_exit(&_nsc_svc_mutex);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_call_svc(nsc_svc_t *svc, intptr_t arg)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_service_t *sp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nsc_svc_t *svcp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int found;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (svc == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (EINVAL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sp = svc->svc_svc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (sp == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (EINVAL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rw_enter(&sp->s_rwlock, RW_READER);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte found = (sp->s_servers != NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (svcp = sp->s_servers; svcp; svcp = svcp->svc_next)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (*svcp->svc_fn)(arg);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rw_exit(&sp->s_rwlock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (found == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (ENOSYS);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}