mirror of
https://git.FreeBSD.org/src.git
synced 2024-12-20 11:11:24 +00:00
3393f8daa3
Some of the major changes include: - The SCSI error handling portion of cam_periph_error() has been broken out into a number of subfunctions to better modularize the code that handles the hierarchy of SCSI errors. As a result, the code is now much easier to read. - String handling and error printing has been significantly revamped. We now use sbufs to do string formatting instead of using printfs (for the kernel) and snprintf/strncat (for userland) as before. There is a new catchall error printing routine, cam_error_print() and its string-based counterpart, cam_error_string() that allow the kernel and userland applications to pass in a CCB and have errors printed out properly, whether or not they're SCSI errors. Among other things, this helped eliminate a fair amount of duplicate code in camcontrol. We now print out more information than before, including the CAM status and SCSI status and the error recovery action taken to remedy the problem. - sbufs are now available in userland, via libsbuf. This change was necessary since most of the error printing code is shared between libcam and the kernel. - A new transfer settings interface is included in this checkin. This code is #ifdef'ed out, and is primarily intended to aid discussion with HBA driver authors on the final form the interface should take. There is example code in the ahc(4) driver that implements the HBA driver side of the new interface. The new transfer settings code won't be enabled until we're ready to switch all HBA drivers over to the new interface. src/Makefile.inc1, lib/Makefile: Add libsbuf. It must be built before libcam, since libcam uses sbuf routines. libcam/Makefile: libcam now depends on libsbuf. libsbuf/Makefile: Add a makefile for libsbuf. This pulls in the sbuf sources from sys/kern. bsd.libnames.mk: Add LIBSBUF. camcontrol/Makefile: Add -lsbuf. Since camcontrol is statically linked, we can't depend on the dynamic linker to pull in libsbuf. camcontrol.c: Use cam_error_print() instead of checking for CAM_SCSI_STATUS_ERROR on every failed CCB. sbuf.9: Change the prototypes for sbuf_cat() and sbuf_cpy() so that the source string is now a const char *. This is more in line wth the standard system string functions, and helps eliminate warnings when dealing with a const source buffer. Fix a typo. cam.c: Add description strings for the various CAM error status values, as well as routines to look up those strings. Add new cam_error_string() and cam_error_print() routines for userland and the kernel. cam.h: Add a new CAM flag, CAM_RETRY_SELTO. Add enumerated types for the various options available with cam_error_print() and cam_error_string(). cam_ccb.h: Add new transfer negotiation structures/types. Change inq_len in the ccb_getdev structure to be "reserved". This field has never been filled in, and will be removed when we next bump the CAM version. cam_debug.h: Fix typo. cam_periph.c: Modularize cam_periph_error(). The SCSI error handling part of cam_periph_error() is now in camperiphscsistatuserror() and camperiphscsisenseerror(). In cam_periph_lock(), increase the reference count on the periph while we wait for our lock attempt to succeed so that the periph won't go away while we're sleeping. cam_xpt.c: Add new transfer negotiation code. (ifdefed out) Add a new function, xpt_path_string(). This is a string/sbuf analog to xpt_print_path(). scsi_all.c: Revamp string handing and error printing code. We now use sbufs for much of the string formatting code. More of that code is shared between userland the kernel. scsi_all.h: Get rid of SS_TURSTART, it wasn't terribly useful in the first place. Add a new error action, SS_REQSENSE. (Send a request sense and then retry the command.) This is useful when the controller hasn't performed autosense for some reason. Change the default actions around a bit. scsi_cd.c, scsi_da.c, scsi_pt.c, scsi_ses.c: SF_RETRY_SELTO -> CAM_RETRY_SELTO. Selection timeouts shouldn't be covered by a sense flag. scsi_pass.[ch]: SF_RETRY_SELTO -> CAM_RETRY_SELTO. Get rid of the last vestiges of a read/write interface. libkern/bsearch.c, sys/libkern.h, conf/files: Add bsearch.c, which is needed for some of the new table lookup routines. aic7xxx_freebsd.c: Define AHC_NEW_TRAN_SETTINGS if CAM_NEW_TRAN_CODE is defined. sbuf.h, subr_sbuf.c: Add the appropriate #ifdefs so sbufs can compile and run in userland. Change sbuf_printf() to use vsnprintf() instead of kvprintf(), which is only available in the kernel. Change the source string for sbuf_cpy() and sbuf_cat() to be a const char *. Add __BEGIN_DECLS and __END_DECLS around function prototypes since they're now exported to userland. kdump/mkioctls: Include stdio.h before cam.h since cam.h now includes a function with a FILE * argument. Submitted by: gibbs (mostly) Reviewed by: jdp, marcel (libsbuf makefile changes) Reviewed by: des (sbuf changes) Reviewed by: ken
319 lines
7.5 KiB
C
319 lines
7.5 KiB
C
/*-
|
|
* Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Coïdan Smørgrav
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer
|
|
* in this position and unchanged.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
* derived from this software without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
* $FreeBSD$
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
#include <sys/sbuf.h>
|
|
|
|
#ifdef _KERNEL
|
|
#include <sys/kernel.h>
|
|
#include <sys/malloc.h>
|
|
#include <sys/systm.h>
|
|
#include <machine/stdarg.h>
|
|
#else /* _KERNEL */
|
|
#include <stdarg.h>
|
|
#endif /* _KERNEL */
|
|
|
|
#ifdef _KERNEL
|
|
MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers");
|
|
#define SBMALLOC(size) malloc(size, M_SBUF, M_WAITOK)
|
|
#define SBFREE(buf) free(buf, M_SBUF)
|
|
#else /* _KERNEL */
|
|
#define KASSERT(e, m)
|
|
#define SBMALLOC(size) malloc(size)
|
|
#define SBFREE(buf) free(buf)
|
|
#define min(x,y) MIN(x,y)
|
|
#endif /* _KERNEL */
|
|
|
|
/*
|
|
* Predicates
|
|
*/
|
|
#define SBUF_ISDYNAMIC(s) ((s)->s_flags & SBUF_DYNAMIC)
|
|
#define SBUF_ISFINISHED(s) ((s)->s_flags & SBUF_FINISHED)
|
|
#define SBUF_HASOVERFLOWED(s) ((s)->s_flags & SBUF_OVERFLOWED)
|
|
#define SBUF_HASROOM(s) ((s)->s_len < (s)->s_size - 1)
|
|
|
|
/*
|
|
* Set / clear flags
|
|
*/
|
|
#define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0)
|
|
#define SBUF_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0)
|
|
|
|
/*
|
|
* Debugging support
|
|
*/
|
|
#if defined(_KERNEL) && defined(INVARIANTS)
|
|
static void
|
|
_assert_sbuf_integrity(char *fun, struct sbuf *s)
|
|
{
|
|
KASSERT(s != NULL,
|
|
("%s called with a NULL sbuf pointer", fun));
|
|
KASSERT(s->s_buf != NULL,
|
|
("%s called with unitialized or corrupt sbuf", fun));
|
|
KASSERT(s->s_len < s->s_size,
|
|
("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
|
|
}
|
|
|
|
static void
|
|
_assert_sbuf_state(char *fun, struct sbuf *s, int state)
|
|
{
|
|
KASSERT((s->s_flags & SBUF_FINISHED) == state,
|
|
("%s called with %sfinished or corrupt sbuf", fun,
|
|
(state ? "un" : "")));
|
|
}
|
|
#define assert_sbuf_integrity(s) _assert_sbuf_integrity(__FUNCTION__, (s))
|
|
#define assert_sbuf_state(s, i) _assert_sbuf_state(__FUNCTION__, (s), (i))
|
|
#else /* _KERNEL && INVARIANTS */
|
|
#define assert_sbuf_integrity(s) do { } while (0)
|
|
#define assert_sbuf_state(s, i) do { } while (0)
|
|
#endif /* _KERNEL && INVARIANTS */
|
|
|
|
/*
|
|
* Initialize an sbuf.
|
|
* If buf is non-NULL, it points to a static or already-allocated string
|
|
* big enough to hold at least length characters.
|
|
*/
|
|
int
|
|
sbuf_new(struct sbuf *s, char *buf, int length, int flags)
|
|
{
|
|
KASSERT(length >= 0,
|
|
("attempt to create an sbuf of negative length (%d)", length));
|
|
KASSERT(flags == 0,
|
|
(__FUNCTION__ " called with non-zero flags"));
|
|
KASSERT(s != NULL,
|
|
(__FUNCTION__ " called with a NULL sbuf pointer"));
|
|
|
|
bzero(s, sizeof *s);
|
|
s->s_size = length;
|
|
if (buf) {
|
|
s->s_buf = buf;
|
|
return (0);
|
|
}
|
|
s->s_buf = (char *)SBMALLOC(s->s_size);
|
|
if (s->s_buf == NULL)
|
|
return (-1);
|
|
SBUF_SETFLAG(s, SBUF_DYNAMIC);
|
|
return (0);
|
|
}
|
|
|
|
/*
|
|
* Clear an sbuf and reset its position
|
|
*/
|
|
void
|
|
sbuf_clear(struct sbuf *s)
|
|
{
|
|
assert_sbuf_integrity(s);
|
|
/* don't care if it's finished or not */
|
|
|
|
SBUF_CLEARFLAG(s, SBUF_FINISHED);
|
|
SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
|
|
s->s_len = 0;
|
|
}
|
|
|
|
/*
|
|
* Set the sbuf's position to an arbitrary value
|
|
*/
|
|
int
|
|
sbuf_setpos(struct sbuf *s, int pos)
|
|
{
|
|
assert_sbuf_integrity(s);
|
|
assert_sbuf_state(s, 0);
|
|
|
|
KASSERT(pos >= 0,
|
|
("attempt to seek to a negative position (%d)", pos));
|
|
KASSERT(pos < s->s_size,
|
|
("attempt to seek past end of sbuf (%d >= %d)", pos, s->s_size));
|
|
|
|
if (pos < 0 || pos > s->s_len)
|
|
return (-1);
|
|
s->s_len = pos;
|
|
return (0);
|
|
}
|
|
|
|
/*
|
|
* Append a string to an sbuf.
|
|
*/
|
|
int
|
|
sbuf_cat(struct sbuf *s, const char *str)
|
|
{
|
|
assert_sbuf_integrity(s);
|
|
assert_sbuf_state(s, 0);
|
|
|
|
if (SBUF_HASOVERFLOWED(s))
|
|
return (-1);
|
|
|
|
while (*str && SBUF_HASROOM(s))
|
|
s->s_buf[s->s_len++] = *str++;
|
|
if (*str) {
|
|
SBUF_SETFLAG(s, SBUF_OVERFLOWED);
|
|
return (-1);
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
/*
|
|
* Copy a string into an sbuf.
|
|
*/
|
|
int
|
|
sbuf_cpy(struct sbuf *s, const char *str)
|
|
{
|
|
assert_sbuf_integrity(s);
|
|
assert_sbuf_state(s, 0);
|
|
|
|
sbuf_clear(s);
|
|
return (sbuf_cat(s, str));
|
|
}
|
|
|
|
/*
|
|
* Format the given arguments and append the resulting string to an sbuf.
|
|
*/
|
|
int
|
|
sbuf_printf(struct sbuf *s, char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
int len;
|
|
|
|
assert_sbuf_integrity(s);
|
|
assert_sbuf_state(s, 0);
|
|
|
|
KASSERT(fmt != NULL,
|
|
(__FUNCTION__ " called with a NULL format string"));
|
|
|
|
if (SBUF_HASOVERFLOWED(s))
|
|
return (-1);
|
|
|
|
va_start(ap, fmt);
|
|
len = vsnprintf(&s->s_buf[s->s_len], s->s_size - s->s_len, fmt, ap);
|
|
va_end(ap);
|
|
|
|
/*
|
|
* s->s_len is the length of the string, without the terminating nul.
|
|
* When updating s->s_len, we must subtract 1 from the length that
|
|
* we passed into vsnprintf() because that length includes the
|
|
* terminating nul.
|
|
*
|
|
* vsnprintf() returns the amount that would have been copied,
|
|
* given sufficient space, hence the min() calculation below.
|
|
*/
|
|
s->s_len += min(len, s->s_size - s->s_len - 1);
|
|
if (!SBUF_HASROOM(s))
|
|
SBUF_SETFLAG(s, SBUF_OVERFLOWED);
|
|
|
|
KASSERT(s->s_len < s->s_size,
|
|
("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
|
|
|
|
if (SBUF_HASOVERFLOWED(s))
|
|
return (-1);
|
|
return (0);
|
|
}
|
|
|
|
/*
|
|
* Append a character to an sbuf.
|
|
*/
|
|
int
|
|
sbuf_putc(struct sbuf *s, int c)
|
|
{
|
|
assert_sbuf_integrity(s);
|
|
assert_sbuf_state(s, 0);
|
|
|
|
if (SBUF_HASOVERFLOWED(s))
|
|
return (-1);
|
|
|
|
if (!SBUF_HASROOM(s)) {
|
|
SBUF_SETFLAG(s, SBUF_OVERFLOWED);
|
|
return (-1);
|
|
}
|
|
if (c != '\0')
|
|
s->s_buf[s->s_len++] = c;
|
|
return (0);
|
|
}
|
|
|
|
/*
|
|
* Check if an sbuf overflowed
|
|
*/
|
|
int
|
|
sbuf_overflowed(struct sbuf *s)
|
|
{
|
|
return SBUF_HASOVERFLOWED(s);
|
|
}
|
|
|
|
/*
|
|
* Finish off an sbuf.
|
|
*/
|
|
void
|
|
sbuf_finish(struct sbuf *s)
|
|
{
|
|
assert_sbuf_integrity(s);
|
|
assert_sbuf_state(s, 0);
|
|
|
|
s->s_buf[s->s_len] = '\0';
|
|
SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
|
|
SBUF_SETFLAG(s, SBUF_FINISHED);
|
|
}
|
|
|
|
/*
|
|
* Return a pointer to the sbuf data.
|
|
*/
|
|
char *
|
|
sbuf_data(struct sbuf *s)
|
|
{
|
|
assert_sbuf_integrity(s);
|
|
assert_sbuf_state(s, SBUF_FINISHED);
|
|
|
|
return s->s_buf;
|
|
}
|
|
|
|
/*
|
|
* Return the length of the sbuf data.
|
|
*/
|
|
int
|
|
sbuf_len(struct sbuf *s)
|
|
{
|
|
assert_sbuf_integrity(s);
|
|
/* don't care if it's finished or not */
|
|
|
|
if (SBUF_HASOVERFLOWED(s))
|
|
return (-1);
|
|
return s->s_len;
|
|
}
|
|
|
|
/*
|
|
* Clear an sbuf, free its buffer if necessary.
|
|
*/
|
|
void
|
|
sbuf_delete(struct sbuf *s)
|
|
{
|
|
assert_sbuf_integrity(s);
|
|
/* don't care if it's finished or not */
|
|
|
|
if (SBUF_ISDYNAMIC(s))
|
|
SBFREE(s->s_buf);
|
|
bzero(s, sizeof *s);
|
|
}
|