nxge_serialize.c revision 678453a8ed49104d8adad58f3ba591bdc39883e8
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
extern pri_t maxclsyspri;
extern int servicing_interrupt(void);
#ifdef _KERNEL
static void nxge_onetrack(void *p);
#else
static void *nxge_onetrack(void *p);
#endif
static int nxge_serial_put(nxge_serialize_t *, void *);
static int nxge_freelance(nxge_serialize_t *);
static void nxge_tx_s_end(nxge_serialize_t *);
{
nxge_serialize_t *p;
"==> nxge_serialize_create:"));
p->count = 0;
p->owned = 0;
p->totaltime = 0;
p->totalcount = 0;
/*
* An initial estimate of the avg time spent in the serializer function.
* Any non-zero value is fine. A large value will induce unnecessary
* delays.
*/
p->avg = 1;
if (p->tx_sthread == NULL) {
cv_destroy(&p->serial_cv);
cv_destroy(&p->timecv);
mutex_destroy(&p->lock);
mutex_destroy(&p->serial);
mutex_destroy(&p->timelock);
kmem_free(p, sizeof (nxge_serialize_t));
"<== nxge_serialize_create: (NULL)"));
return (NULL);
}
"<== nxge_serialize_create: s %p thread %p",
p, p->tx_sthread));
return (p);
}
void
{
int n, i;
"==> nxge_serialize_destroy: s %p", p));
if (p == NULL) {
"<== nxge_serialize_destroy:"));
return;
}
mutex_enter(&p->serial);
p->s_state |= NXGE_TX_STHREAD_DESTROY;
while (p->s_state & NXGE_TX_STHREAD_DESTROY) {
"==> nxge_serialize_destroy: s %p state %d",
p, p->s_state));
if (p->s_state & NXGE_TX_STHREAD_EXIT) {
break;
}
}
"==> nxge_serialize_destroy: s %p state %d",
p, p->s_state));
n = nxge_serial_getn(p, &mp, &t);
for (i = 0; i < n; i++) {
"==> nxge_serialize_destroy: s %p mp %p", p, mp));
}
mutex_exit(&p->serial);
cv_destroy(&p->serial_cv);
cv_destroy(&p->timecv);
mutex_destroy(&p->lock);
mutex_destroy(&p->serial);
mutex_destroy(&p->timelock);
kmem_free(p, sizeof (nxge_serialize_t));
"<== nxge_serialize_destroy: s %p", p));
}
/*
* Return values:
* 0 means put succeeded
* 1 means we have exclusive access
*/
static int
{
mblk_t *t;
int r = 0;
int block = 0;
"==> nxge_serial_put: s %p mp %p", p, mp));
mutex_enter(&p->lock);
/*
* If the time required to drain all the queued up packets
* is greater than a tick, we need to block.
*/
/*
* Sanity check that we will sleep only for less than ~a second
*/
p->count--;
mutex_exit(&p->lock);
return (0);
}
block = 1;
}
if (p->owned == 0) {
r = p->owned = 1;
block = 0;
}
} else {
}
mutex_exit(&p->lock);
/*
* Block for the number of ticks required to drain half
* the queued up packets - but only if we are not within
* an interrupt thread.
*/
if (block) {
if (!servicing_interrupt()) {
mutex_enter(&p->timelock);
mutex_exit(&p->timelock);
}
}
return (r);
}
static int
{
int c;
mutex_enter(&p->lock);
if ((c = p->count) != 0) {
p->count = 0;
} else {
p->owned = 0;
}
mutex_exit(&p->lock);
return (c);
}
static void
{
mutex_enter(&p->lock);
} else {
}
p->count += n;
mutex_exit(&p->lock);
}
#ifdef _KERNEL
static void
#else
static void *
#endif
nxge_onetrack(void *s)
{
int k, i;
nxge_serialize_t *p = (nxge_serialize_t *)s;
"==> nxge_onetrack: s %p", s));
(void) nxge_tx_s_begin(p);
mutex_enter(&p->serial);
while (p->s_state & NXGE_TX_STHREAD_RUNNING) {
if (p->s_state & NXGE_TX_STHREAD_DESTROY) {
break;
}
if (p->s_state & NXGE_TX_STHREAD_DESTROY) {
break;
}
&p->serial)
for (i = 0; i < k; i++) {
"==> nxge_onetrack: s %p mp %p", s, mp));
/*
* The queue is full, block and wait for half of
* it to drain.
*/
(void) cv_timedwait(&p->timecv,
if (p->s_state &
"==> nxge_onetrack: s $%p "
"exiting", s));
break;
}
}
mp = n;
}
/*
* Update the total time and count of the serializer
* function and * generate the avg time required to
* process a packet.
*/
p->totalcount += k;
}
}
mutex_exit(&p->serial);
"<== nxge_onetrack: s %p", s));
nxge_tx_s_end(s);
}
/*
* Return values:
* 0 : don't need to signal worker
* 1 : worker needs to be signalled
*/
static int
{
int i, n, c = 0;
"==> nxge_freelance: s %p", s));
while (n = nxge_serial_getn(s, &mp, &t)) {
if ((n > nxge_maxhrs) || ((c += n) > nxge_maxhrs)) {
nxge_serial_ungetn(s, mp, t, n);
return (1);
}
for (i = 0; i < n; i++) {
nxge_serial_ungetn(s, mp, t, n - i);
return (1);
}
"==> nxge_freelance: s %p mp %p", s, mp));
}
}
"<== nxge_freelance: s %p", s));
return (0);
}
void
{
if (nxge_serial_put(s, mp)) {
if (nxge_freelance(s)) {
mutex_enter(&s->serial);
mutex_exit(&s->serial);
}
}
}
static caddr_t
{
callb_generic_cpr, "nxge_tx_serialize");
return (s->cookie);
}
static void
{
"==> nxge_tx_s_end: s %p", s));
mutex_enter(&s->serial);
s->s_state |= NXGE_TX_STHREAD_EXIT;
"<== nxge_tx_s_end: s %p", s));
thread_exit();
}