/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include "sd_xbuf.h"
/*
* xbuf.c: buf(9s) extension facility.
*
* The buf(9S) extension facility is intended to allow block drivers to
* allocate additional memory that is associated with a particular buf(9S)
* struct. It is further intended to help in addressing the usual set of
* problems associated with such allocations, in particular those involving
* recovery from allocation failures, especially in code paths that the
* system relies on to free memory.
*
* CAVEAT: Currently this code is completely private to the sd driver and in
* NO WAY constitutes a public or supported interface of any kind. It is
* envisioned that this may one day migrate into the Solaris DDI, but until
* that time this ought to be considered completely unstable and is subject
* to change without notice. This code may NOT in any way be utilized by
* ANY code outside the sd driver.
*/
static void xbuf_restart_callback(void *arg);
/*
* Note: Should this be exposed to the caller.... do we want to give the
* caller the fexibility of specifying the parameters for the thread pool?
* Note: these values are just estimates at this time, based upon what
* seems reasonable for the sd driver. It may be preferable to make these
* parameters self-scaling in a real (future) implementation.
*/
/*
* Private wrapper for buf cloned via ddi_xbuf_qstrategy()
*/
struct xbuf_brk {
int brkblk;
/* xfer position */
};
/*
* Hack needed in the prototype so buf breakup will work.
* Here we can rely on the sd code not changing the value in
* b_forw.
*/
/* ARGSUSED */
{
/* Future: Allow the caller to specify alignment requirements? */
if (xbuf_refcount == 0) {
/*
* Note: Would be nice if: (1) #threads in the taskq pool (set
* to the value of 'ncpus' at the time the taskq is created)
* could adjust automatically with DR; (2) the taskq
*/
}
/* In this prototype we just always use the global system pool. */
return (xap);
}
DDII void
{
/* Free any xbufs on the reserve list */
while (xap->xa_reserve_count != 0) {
xap->xa_reserve_count--;
}
if (xbuf_refcount == 0) {
}
}
/* ARGSUSED */
DDII void
{
/* Currently a no-op in this prototype */
}
/* ARGSUSED */
DDII void
{
/* Currently a no-op in this prototype */
}
DDII int
{
return (0);
return (1);
}
/*
* Enqueue the given buf and attempt to initiate IO.
* Called from the driver strategy(9E) routine.
*/
DDII int
{
/*
* Breakup buf if necessary. bp->b_private is temporarily
* used to save xbuf_brk
*/
} else {
}
/* Enqueue buf */
} else {
}
xap->xa_pending++;
return (xbuf_iostart(xap));
}
/*
* Drivers call this immediately before calling biodone(9F), to notify the
* framework that the indicated xbuf is no longer being used by the driver.
* May be called under interrupt context.
*/
DDII int
{
int done;
#ifdef SDDEBUG
if (xap->xa_active_limit != 0) {
}
#endif
xap->xa_active_count--;
if (xap->xa_reserve_limit != 0) {
/* Put this xbuf onto the reserve list & exit */
xap->xa_reserve_count++;
goto done;
}
}
done:
done = 0;
} else {
done = 1;
}
} else {
done = 1;
}
if ((xap->xa_active_limit == 0) ||
}
return (done);
}
static int
{
int done;
}
/* All buf segments done */
if (done) {
}
return (0);
}
DDII void
{
if ((xap->xa_active_limit == 0) ||
}
}
/*
* ISSUE: in this prototype we cannot really implement ddi_xbuf_get()
* unless we explicitly hide the xbuf pointer somewhere in the buf
* during allocation, and then rely on the driver never changing it.
* We can probably get away with using b_private for this for now,
* tho it really is kinda gnarly.....
*/
/* ARGSUSED */
{
}
/*
* Initiate IOs for bufs on the queue. Called from kernel thread or taskq
* thread context. May execute concurrently for the same ddi_xbuf_attr_t.
*/
static int
{
/*
* For each request on the queue, attempt to allocate the specified
* xbuf extension area, and call the driver's iostart() routine.
* We process as many requests on the queue as we can, until either
* (1) we run out of requests; or
* (2) we run out of resources; or
* (3) we reach the maximum limit for the given ddi_xbuf_attr_t.
*/
for (;;) {
break; /* queue empty */
}
if ((xap->xa_active_limit != 0) &&
break; /* allocation limit reached */
}
/*
* If the reserve_limit is non-zero then work with the
* reserve else always allocate a new struct.
*/
if (xap->xa_reserve_limit != 0) {
/*
* Don't penalize EVERY I/O by always allocating a new
* struct. for the sake of maintaining and not touching
* a reserve for a pathalogical condition that may never
* happen. Use the reserve entries first, this uses it
* like a local pool rather than a reserve that goes
* untouched. Make sure it's re-populated whenever it
* gets fully depleted just in case it really is needed.
* This is safe because under the pathalogical
* condition, when the system runs out of memory such
* that the below allocs fail, the reserve will still
* be available whether the entries are saved away on
* the queue unused or in-transport somewhere. Thus
* progress can still continue, however slowly.
*/
if (xap->xa_reserve_count != 0) {
/* Grab an xbuf from the reserve */
xap->xa_reserve_count--;
} else {
/*
* Either this is the first time through,
* or the reserve has been totally depleted.
* Re-populate the reserve (pool). Excess
* structs. get released in the done path.
*/
while (xap->xa_reserve_count <
xap->xa_reserve_limit) {
break;
}
xap->xa_reserve_count++;
}
/* And one more to use right now. */
}
} else {
/*
* Try to alloc a new xbuf struct. If this fails just
* exit for now. We'll get back here again either upon
* cmd completion or via the timer handler.
* Question: what if the allocation attempt for the very
* first cmd. fails? There are no outstanding cmds so
* how do we get back here?
* Should look at un_ncmds_in_transport, if it's zero
* then schedule xbuf_restart_callback via the timer.
* Athough that breaks the architecture by bringing
* softstate data into this code.
*/
}
break; /* Can't process a cmd. right now. */
}
/*
* is non-zero which is the typical (and right now only) case.
*/
xap->xa_active_count++;
/* update xfer position */
} else {
/* unlink the buf from the list */
}
} else {
/* unlink the buf from the list */
}
/*
* Hack needed in the prototype so ddi_xbuf_get() will work.
* Here we can rely on the sd code not changing the value in
* b_private (in fact it wants it there). See ddi_get_xbuf()
*/
/* call the driver's iostart routine */
}
xap->xa_pending--;
return (0);
}
/*
* Re-start IO processing if there is anything on the queue, AND if the
*/
static void
{
(xap->xa_pending == 0)) {
/*
* First try to see if we can dispatch the restart function
* immediately, in a taskq thread. If this fails, then
* schedule a timeout(9F) callback to try again later.
*/
/*
* Unable to enqueue the request for the taskq thread,
* try again later. Note that this will keep re-trying
* until taskq_dispatch() succeeds.
*/
} else {
/*
* This indicates that xbuf_iostart() will soon be
* run for this ddi_xbuf_attr_t, and we do not need to
*/
xap->xa_pending++;
}
}
}
/* timeout(9F) callback routine for xbuf restart mechanism. */
static void
{
}
DDII void
{
/*
* If the user-supplied function is non-NULL and returns
* FALSE, then just leave the current bp on the queue.
*/
continue;
}
/* de-queue the bp */
}
} else {
}
}
/* Add the bp to the flush queue */
} else {
}
}
}
}
}