suntpi.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
3853N/A/*
3853N/A * CDDL HEADER START
3853N/A *
3853N/A * The contents of this file are subject to the terms of the
3853N/A * Common Development and Distribution License, Version 1.0 only
3853N/A * (the "License"). You may not use this file except in compliance
3853N/A * with the License.
3853N/A *
3853N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
3853N/A * or http://www.opensolaris.org/os/licensing.
3853N/A * See the License for the specific language governing permissions
3853N/A * and limitations under the License.
3853N/A *
3853N/A * When distributing Covered Code, include this CDDL HEADER in each
3853N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
3853N/A * If applicable, add the following below this CDDL HEADER, with the
3853N/A * fields enclosed by brackets "[]" replaced with your own identifying
3853N/A * information: Portions Copyright [yyyy] [name of copyright owner]
3853N/A *
3853N/A * CDDL HEADER END
3853N/A */
3853N/A/*
3853N/A * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3853N/A * Use is subject to license terms.
5035N/A */
3853N/A
3853N/A#pragma ident "%Z%%M% %I% %E% SMI"
3853N/A
3853N/A#include <sys/types.h>
3853N/A#include <sys/kmem.h>
4803N/A#include <sys/bitmap.h>
3853N/A#include <sys/stream.h>
3853N/A#include <sys/strsubr.h>
3853N/A#define _SUN_TPI_VERSION 2
4141N/A#include <sys/tihdr.h>
3853N/A#include <sys/suntpi.h>
3853N/A
3853N/A/*
3853N/A * Hash table parameters for tpi_provinfo_table.
3853N/A */
4141N/A#define TPI_HASH_BITS 4
3898N/A#define TPI_NHASH (1 << TPI_HASH_BITS)
3898N/A
4920N/A/*
4518N/A * Use the first element in the key for the hash.
3853N/A */
3853N/A#define TPI_HASH(p) ((((uintptr_t *)p)[0] >> tpi_hashshift) % TPI_NHASH)
3853N/A/*
3853N/A * SAMESTR is a very confusing name. LAST_QUEUE is introduced for readability.
4803N/A */
3853N/A#define LAST_QUEUE(q) (!SAMESTR(q))
3853N/A
3853N/Astatic tpi_provinfo_t *tpi_provinfo_table[TPI_NHASH];
3853N/Astatic kmutex_t tpi_provinfo_lock;
3853N/Astatic int tpi_hashshift;
3853N/A
3853N/A/*
3853N/A * In most cases there is some transport provider (like tcp or udp) below
3853N/A * transport user (like timod or sockets). However, it is possible to construct
3853N/A * stream without transport provider (e.g. by pushing timod into FIFO). It is
3853N/A * hardly of any use, but this condition was observed with sparcv9 abi tests.
3853N/A * To count for such special case, a special tpi_nullprov static data is
3853N/A * provided to cache information about such degenerated null-transport case.
3853N/A */
3853N/Astatic tpi_provinfo_t tpi_nullprov; /* Placeholder for null transport */
4803N/A
4803N/A/*
3853N/A * Initialise the TPI support routines. Called from strinit().
3853N/A */
3853N/Avoid
3853N/Atpi_init()
3853N/A{
3853N/A mutex_init(&tpi_provinfo_lock, NULL, MUTEX_DEFAULT, NULL);
3853N/A
3853N/A /*
4141N/A * Calculate the right shift for hashing a tpi_provinfo_t.
4141N/A */
4141N/A tpi_hashshift = highbit(sizeof (tpi_provinfo_t));
4141N/A}
4141N/A
4141N/A/*
4141N/A * Generate a downstream signature given the write-side queue. It
4141N/A * passes back the size of the generated key in *keylenp. This routine
4141N/A * cannot multithread as it returns a pointer to a static data item.
4141N/A *
4518N/A * There is no way (in the current module loading infrastructure) to
4518N/A * _absolutely_ guarantee that the key below uniquely identifies an
4141N/A * arrangement of modules and drivers. A module _might_ be unloaded and
4141N/A * another module _might_ be loaded such that the qi_minfo is at _exactly_
3853N/A * same kernel address, and then it _might_ be placed in a transport
3853N/A * provider stream in exactly the same configuration (modules above and
4920N/A * below all identical) - but it would take quite a few coincidences
3853N/A * and modules loading and unloading does not usually happen n times a
4500N/A * second...
4500N/A */
4500N/Astatic void *
4500N/Atpi_makekey(queue_t *q, size_t *keylenp)
3853N/A{
3853N/A static uintptr_t *key = NULL;
3853N/A int i;
3853N/A
3853N/A ASSERT(q != NULL);
3853N/A ASSERT(MUTEX_HELD(&tpi_provinfo_lock));
3853N/A
3853N/A /* assert this queue is write queue and qprocson() is called before */
3853N/A ASSERT((q->q_flag & QREADR) == 0);
3853N/A ASSERT(q->q_next != NULL);
3853N/A
3853N/A /*
3853N/A * This can be global because tpi_makekey is called with
3853N/A * tpi_provinfo_lock.
3853N/A */
3853N/A if (key == NULL)
3853N/A key = kmem_alloc((nstrpush + 1) * sizeof (uintptr_t), KM_SLEEP);
3853N/A
3853N/A ASSERT(key != NULL);
3853N/A
3853N/A /*
3853N/A * Go down q_next to the driver, but no further. We use the qi_minfo
3853N/A * because we can find in from the queue and it is a stable part of
4500N/A * any driver/module infrastructure.
4500N/A */
4500N/A for (i = 0; !LAST_QUEUE(q) && (q = q->q_next) != NULL; ++i) {
4500N/A ASSERT(i < nstrpush + 1);
3853N/A key[i] = (uintptr_t)q->q_qinfo->qi_minfo;
3853N/A }
3853N/A
3853N/A /*
3853N/A * Allocate the actual key with the proper length, and pass it
3853N/A * all back.
3853N/A */
3853N/A *keylenp = i * sizeof (uintptr_t);
3853N/A return ((void *)key);
3853N/A}
3853N/A
3853N/A/*
3853N/A * Find an existing provider entry given a queue pointer, or allocate a
3853N/A * new empty entry if not found. Because this routine calls kmem_alloc
3853N/A * with KM_SLEEP, and because it traverses the q_next pointers of a stream
3853N/A * it must be called with a proper user context and within a perimeter
3853N/A * which protects the STREAM e.g. an open routine. This routine always
3853N/A * returns a valid pointer.
3858N/A */
3853N/Atpi_provinfo_t *
3853N/Atpi_findprov(queue_t *q)
3853N/A{
3853N/A void *key;
3853N/A size_t keylen;
3853N/A tpi_provinfo_t **tpp;
3853N/A
3853N/A mutex_enter(&tpi_provinfo_lock);
3858N/A
3853N/A /*
3853N/A * Must hold tpi_provinfo_lock since tpi_makekey() returns a pointer
3853N/A * to static data.
3853N/A */
3853N/A key = tpi_makekey(WR(q), &keylen);
3853N/A
3853N/A if (keylen == 0) {
3853N/A /* there is nothing below us, return special nullprov entry */
3853N/A mutex_exit(&tpi_provinfo_lock);
3853N/A return (&tpi_nullprov);
3853N/A }
3853N/A
3853N/A /*
3853N/A * Look for an existing entry, or the place to put a new one.
3853N/A */
3853N/A for (tpp = &tpi_provinfo_table[TPI_HASH(key)]; *tpp != NULL;
3853N/A tpp = &(*tpp)->tpi_next) {
3853N/A if ((*tpp)->tpi_keylen == keylen &&
3853N/A bcmp((*tpp)->tpi_key, key, keylen) == 0) {
3853N/A mutex_exit(&tpi_provinfo_lock);
3853N/A return (*tpp);
3853N/A }
3853N/A }
4060N/A
4060N/A /*
4060N/A * Allocate and fill in the new tpi_provinfo_t.
4060N/A */
4060N/A *tpp = kmem_zalloc(sizeof (tpi_provinfo_t), KM_SLEEP);
4803N/A (*tpp)->tpi_key = kmem_alloc(keylen, KM_SLEEP);
4060N/A bcopy(key, (*tpp)->tpi_key, keylen);
4060N/A (*tpp)->tpi_keylen = keylen;
4060N/A mutex_init(&(*tpp)->tpi_lock, NULL, MUTEX_DEFAULT, NULL);
4060N/A
4060N/A mutex_exit(&tpi_provinfo_lock);
4060N/A return (*tpp);
4060N/A}
4060N/A
4060N/A/*
4060N/A * Allocate a TPI ACK reusing the old message if possible.
4803N/A */
4060N/Amblk_t *
4060N/Atpi_ack_alloc(mblk_t *mp, size_t size, uchar_t db_type, t_scalar_t prim)
4060N/A{
4060N/A mblk_t *omp = mp;
4060N/A
3853N/A if ((mp = reallocb(mp, size, 0)) == NULL) {
3853N/A freemsg(omp);
3853N/A return (NULL);
4803N/A }
3853N/A if (mp->b_cont != NULL) {
3853N/A freemsg(mp->b_cont);
3853N/A mp->b_cont = NULL;
3853N/A }
3853N/A mp->b_datap->db_type = db_type;
3853N/A mp->b_wptr = mp->b_rptr + size;
3853N/A ((union T_primitives *)mp->b_rptr)->type = prim;
3853N/A return (mp);
4803N/A}
3853N/A