2010-07-25 17:49:58 +04:00
|
|
|
/* $NetBSD: scsipiconf.c,v 1.39 2010/07/25 13:49:58 pgoyette Exp $ */
|
1997-08-27 15:22:52 +04:00
|
|
|
|
1998-08-17 04:49:01 +04:00
|
|
|
/*-
|
2004-09-18 03:30:22 +04:00
|
|
|
* Copyright (c) 1998, 1999, 2004 The NetBSD Foundation, Inc.
|
1998-08-17 04:49:01 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2001-04-25 21:53:04 +04:00
|
|
|
* by Charles M. Hannum; by Jason R. Thorpe of the Numerical Aerospace
|
|
|
|
* Simulation Facility, NASA Ames Research Center.
|
1997-08-27 15:22:52 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
*
|
1998-08-17 04:49:01 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``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 FOUNDATION OR CONTRIBUTORS
|
|
|
|
* 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.
|
1997-08-27 15:22:52 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Originally written by Julian Elischer (julian@tfs.com)
|
|
|
|
* for TRW Financial Systems for use under the MACH(2.5) operating system.
|
|
|
|
*
|
|
|
|
* TRW Financial Systems, in accordance with their agreement with Carnegie
|
|
|
|
* Mellon University, makes this software available to CMU to distribute
|
|
|
|
* or use in any manner that they see fit as long as this message is kept with
|
|
|
|
* the software. For this reason TFS also grants any other persons or
|
|
|
|
* organisations permission to use or modify this software.
|
|
|
|
*
|
|
|
|
* TFS supplies this software to be publicly redistributed
|
|
|
|
* on the understanding that TFS is not responsible for the correct
|
|
|
|
* functioning of this software in any circumstances.
|
|
|
|
*
|
|
|
|
* Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
|
|
|
|
*/
|
|
|
|
|
2001-11-13 09:54:32 +03:00
|
|
|
#include <sys/cdefs.h>
|
2010-07-25 17:49:58 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: scsipiconf.c,v 1.39 2010/07/25 13:49:58 pgoyette Exp $");
|
2001-11-13 09:54:32 +03:00
|
|
|
|
1997-08-27 15:22:52 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
2010-06-07 05:41:39 +04:00
|
|
|
#include <sys/module.h>
|
1997-08-27 15:22:52 +04:00
|
|
|
#include <sys/device.h>
|
2000-06-09 12:54:19 +04:00
|
|
|
#include <sys/proc.h>
|
|
|
|
|
1997-08-27 15:22:52 +04:00
|
|
|
#include <dev/scsipi/scsipi_all.h>
|
|
|
|
#include <dev/scsipi/scsipiconf.h>
|
2004-09-18 03:30:22 +04:00
|
|
|
#include <dev/scsipi/scsipi_base.h>
|
1997-08-27 15:22:52 +04:00
|
|
|
|
2005-02-01 03:19:34 +03:00
|
|
|
#define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
|
2000-12-31 20:54:29 +03:00
|
|
|
|
2010-05-30 08:38:03 +04:00
|
|
|
/* Function pointers and stub routines for scsiverbose module */
|
2010-06-07 05:41:39 +04:00
|
|
|
int (*scsipi_print_sense)(struct scsipi_xfer *, int) = scsipi_print_sense_stub;
|
|
|
|
void (*scsipi_print_sense_data)(struct scsi_sense_data *, int) =
|
2010-05-30 08:38:03 +04:00
|
|
|
scsipi_print_sense_data_stub;
|
|
|
|
|
2010-06-07 05:41:39 +04:00
|
|
|
int scsi_verbose_loaded = 0;
|
2010-05-30 08:38:03 +04:00
|
|
|
|
2010-06-07 05:41:39 +04:00
|
|
|
int scsipi_print_sense_stub(struct scsipi_xfer * xs, int verbosity)
|
2010-05-30 08:38:03 +04:00
|
|
|
{
|
2010-06-07 05:41:39 +04:00
|
|
|
scsipi_load_verbose();
|
|
|
|
if (scsi_verbose_loaded)
|
|
|
|
return scsipi_print_sense(xs, verbosity);
|
|
|
|
else
|
|
|
|
return 0;
|
2010-05-30 08:38:03 +04:00
|
|
|
}
|
|
|
|
|
2010-06-07 05:41:39 +04:00
|
|
|
void scsipi_print_sense_data_stub(struct scsi_sense_data *sense, int verbosity)
|
2010-05-30 08:38:03 +04:00
|
|
|
{
|
2010-06-07 05:41:39 +04:00
|
|
|
scsipi_load_verbose();
|
|
|
|
if (scsi_verbose_loaded)
|
|
|
|
scsipi_print_sense_data(sense, verbosity);
|
2010-05-30 08:38:03 +04:00
|
|
|
}
|
|
|
|
|
2000-06-09 12:54:19 +04:00
|
|
|
int
|
2004-09-18 03:43:17 +04:00
|
|
|
scsipi_command(struct scsipi_periph *periph, struct scsipi_generic *cmd,
|
2005-02-01 03:19:34 +03:00
|
|
|
int cmdlen, u_char *data_addr, int datalen, int retries, int timeout,
|
2004-09-18 03:43:17 +04:00
|
|
|
struct buf *bp, int flags)
|
2000-06-09 12:54:19 +04:00
|
|
|
{
|
2004-09-18 03:43:17 +04:00
|
|
|
struct scsipi_xfer *xs;
|
2005-02-27 03:26:58 +03:00
|
|
|
|
2004-09-18 03:43:17 +04:00
|
|
|
xs = scsipi_make_xs(periph, cmd, cmdlen, data_addr, datalen, retries,
|
|
|
|
timeout, bp, flags);
|
|
|
|
if (!xs)
|
|
|
|
return (ENOMEM);
|
2004-09-18 03:30:22 +04:00
|
|
|
|
|
|
|
return (scsipi_execute_xs(xs));
|
2000-06-09 12:54:19 +04:00
|
|
|
}
|
|
|
|
|
2010-06-07 05:41:39 +04:00
|
|
|
/*
|
|
|
|
* Load the scsiverbose module
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
scsipi_load_verbose(void)
|
|
|
|
{
|
2010-07-25 17:49:58 +04:00
|
|
|
if (scsi_verbose_loaded == 0) {
|
|
|
|
mutex_enter(&module_lock);
|
|
|
|
module_autoload("scsiverbose", MODULE_CLASS_MISC);
|
|
|
|
mutex_exit(&module_lock);
|
|
|
|
}
|
2010-06-07 05:41:39 +04:00
|
|
|
}
|
|
|
|
|
2001-04-25 21:53:04 +04:00
|
|
|
/*
|
|
|
|
* allocate and init a scsipi_periph structure for a new device.
|
|
|
|
*/
|
|
|
|
struct scsipi_periph *
|
2004-08-22 01:29:39 +04:00
|
|
|
scsipi_alloc_periph(int malloc_flag)
|
2001-04-25 21:53:04 +04:00
|
|
|
{
|
|
|
|
struct scsipi_periph *periph;
|
2005-02-01 03:19:34 +03:00
|
|
|
u_int i;
|
2001-04-25 21:53:04 +04:00
|
|
|
|
2002-01-12 19:29:30 +03:00
|
|
|
periph = malloc(sizeof(*periph), M_DEVBUF, malloc_flag|M_ZERO);
|
2001-04-25 21:53:04 +04:00
|
|
|
if (periph == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
periph->periph_dev = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start with one command opening. The periph driver
|
|
|
|
* will grow this if it knows it can take advantage of it.
|
|
|
|
*/
|
2005-02-27 03:26:58 +03:00
|
|
|
periph->periph_openings = 1;
|
|
|
|
periph->periph_active = 0;
|
2001-04-25 21:53:04 +04:00
|
|
|
|
|
|
|
for (i = 0; i < PERIPH_NTAGWORDS; i++)
|
|
|
|
periph->periph_freetags[i] = 0xffffffff;
|
|
|
|
|
|
|
|
TAILQ_INIT(&periph->periph_xferq);
|
2007-07-10 00:51:58 +04:00
|
|
|
callout_init(&periph->periph_callout, 0);
|
2001-04-25 21:53:04 +04:00
|
|
|
|
|
|
|
return periph;
|
|
|
|
}
|
|
|
|
|
1997-08-27 15:22:52 +04:00
|
|
|
/*
|
|
|
|
* Return a priority based on how much of the inquiry data matches
|
|
|
|
* the patterns for the particular driver.
|
|
|
|
*/
|
2005-05-30 02:00:50 +04:00
|
|
|
const void *
|
|
|
|
scsipi_inqmatch(struct scsipi_inquiry_pattern *inqbuf, const void *base,
|
|
|
|
size_t nmatches, size_t matchsize, int *bestpriority)
|
1997-08-27 15:22:52 +04:00
|
|
|
{
|
2005-02-01 03:19:34 +03:00
|
|
|
u_int8_t type;
|
2005-05-30 02:00:50 +04:00
|
|
|
const struct scsipi_inquiry_pattern *bestmatch;
|
1997-08-27 15:22:52 +04:00
|
|
|
|
|
|
|
/* Include the qualifier to catch vendor-unique types. */
|
|
|
|
type = inqbuf->type;
|
|
|
|
|
2005-05-30 02:00:50 +04:00
|
|
|
for (*bestpriority = 0, bestmatch = 0; nmatches--;
|
|
|
|
base = (const char *)base + matchsize) {
|
|
|
|
const struct scsipi_inquiry_pattern *match = base;
|
1997-08-27 15:22:52 +04:00
|
|
|
int priority, len;
|
|
|
|
|
|
|
|
if (type != match->type)
|
|
|
|
continue;
|
|
|
|
if (inqbuf->removable != match->removable)
|
|
|
|
continue;
|
|
|
|
priority = 2;
|
|
|
|
len = strlen(match->vendor);
|
2001-07-18 22:27:08 +04:00
|
|
|
if (memcmp(inqbuf->vendor, match->vendor, len))
|
1997-08-27 15:22:52 +04:00
|
|
|
continue;
|
|
|
|
priority += len;
|
|
|
|
len = strlen(match->product);
|
2001-07-18 22:27:08 +04:00
|
|
|
if (memcmp(inqbuf->product, match->product, len))
|
1997-08-27 15:22:52 +04:00
|
|
|
continue;
|
|
|
|
priority += len;
|
|
|
|
len = strlen(match->revision);
|
2001-07-18 22:27:08 +04:00
|
|
|
if (memcmp(inqbuf->revision, match->revision, len))
|
1997-08-27 15:22:52 +04:00
|
|
|
continue;
|
|
|
|
priority += len;
|
|
|
|
|
2001-04-25 21:53:04 +04:00
|
|
|
#ifdef SCSIPI_DEBUG
|
1997-08-27 15:22:52 +04:00
|
|
|
printf("scsipi_inqmatch: %d/%d/%d <%s, %s, %s>\n",
|
|
|
|
priority, match->type, match->removable,
|
|
|
|
match->vendor, match->product, match->revision);
|
|
|
|
#endif
|
|
|
|
if (priority > *bestpriority) {
|
|
|
|
*bestpriority = priority;
|
|
|
|
bestmatch = base;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (bestmatch);
|
|
|
|
}
|
|
|
|
|
2004-08-22 01:30:29 +04:00
|
|
|
const char *
|
2004-08-22 01:29:39 +04:00
|
|
|
scsipi_dtype(int type)
|
1997-08-27 15:22:52 +04:00
|
|
|
{
|
2004-08-22 01:30:58 +04:00
|
|
|
const char *dtype;
|
1997-10-01 05:18:38 +04:00
|
|
|
|
1997-08-27 15:22:52 +04:00
|
|
|
switch (type) {
|
|
|
|
case T_DIRECT:
|
2002-10-04 21:53:33 +04:00
|
|
|
dtype = "disk";
|
1997-10-03 06:04:17 +04:00
|
|
|
break;
|
1997-08-27 15:22:52 +04:00
|
|
|
case T_SEQUENTIAL:
|
2002-10-04 21:53:33 +04:00
|
|
|
dtype = "tape";
|
1997-08-27 15:22:52 +04:00
|
|
|
break;
|
|
|
|
case T_PRINTER:
|
|
|
|
dtype = "printer";
|
|
|
|
break;
|
|
|
|
case T_PROCESSOR:
|
|
|
|
dtype = "processor";
|
|
|
|
break;
|
|
|
|
case T_WORM:
|
|
|
|
dtype = "worm";
|
|
|
|
break;
|
1997-10-03 06:04:17 +04:00
|
|
|
case T_CDROM:
|
|
|
|
dtype = "cdrom";
|
|
|
|
break;
|
1997-08-27 15:22:52 +04:00
|
|
|
case T_SCANNER:
|
|
|
|
dtype = "scanner";
|
|
|
|
break;
|
|
|
|
case T_OPTICAL:
|
|
|
|
dtype = "optical";
|
|
|
|
break;
|
|
|
|
case T_CHANGER:
|
|
|
|
dtype = "changer";
|
|
|
|
break;
|
|
|
|
case T_COMM:
|
|
|
|
dtype = "communication";
|
|
|
|
break;
|
1997-10-03 06:04:17 +04:00
|
|
|
case T_IT8_1:
|
|
|
|
case T_IT8_2:
|
2000-05-15 20:35:49 +04:00
|
|
|
dtype = "graphic arts pre-press";
|
1997-10-03 06:04:17 +04:00
|
|
|
break;
|
|
|
|
case T_STORARRAY:
|
|
|
|
dtype = "storage array";
|
|
|
|
break;
|
|
|
|
case T_ENCLOSURE:
|
|
|
|
dtype = "enclosure services";
|
|
|
|
break;
|
2000-05-15 20:35:49 +04:00
|
|
|
case T_SIMPLE_DIRECT:
|
|
|
|
dtype = "simplified direct";
|
|
|
|
break;
|
|
|
|
case T_OPTIC_CARD_RW:
|
|
|
|
dtype = "optical card r/w";
|
|
|
|
break;
|
|
|
|
case T_OBJECT_STORED:
|
|
|
|
dtype = "object-based storage";
|
|
|
|
break;
|
1997-08-27 15:22:52 +04:00
|
|
|
case T_NODEVICE:
|
|
|
|
panic("scsipi_dtype: impossible device type");
|
|
|
|
default:
|
|
|
|
dtype = "unknown";
|
|
|
|
break;
|
|
|
|
}
|
1997-10-01 05:18:38 +04:00
|
|
|
return (dtype);
|
1997-08-27 15:22:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-05-30 08:25:32 +04:00
|
|
|
scsipi_strvis(u_char *dst, int dlen, const u_char *src, int slen)
|
1997-08-27 15:22:52 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
/* Trim leading and trailing blanks and NULs. */
|
2000-12-31 20:54:29 +03:00
|
|
|
while (slen > 0 && STRVIS_ISWHITE(src[0]))
|
1998-08-05 20:29:04 +04:00
|
|
|
++src, --slen;
|
2000-12-31 20:54:29 +03:00
|
|
|
while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
|
1998-08-05 20:29:04 +04:00
|
|
|
--slen;
|
1997-08-27 15:22:52 +04:00
|
|
|
|
1998-08-05 20:29:04 +04:00
|
|
|
while (slen > 0) {
|
1997-08-27 15:22:52 +04:00
|
|
|
if (*src < 0x20 || *src >= 0x80) {
|
|
|
|
/* non-printable characters */
|
1998-08-05 20:29:04 +04:00
|
|
|
dlen -= 4;
|
|
|
|
if (dlen < 1)
|
|
|
|
break;
|
1997-08-27 15:22:52 +04:00
|
|
|
*dst++ = '\\';
|
|
|
|
*dst++ = ((*src & 0300) >> 6) + '0';
|
|
|
|
*dst++ = ((*src & 0070) >> 3) + '0';
|
|
|
|
*dst++ = ((*src & 0007) >> 0) + '0';
|
|
|
|
} else if (*src == '\\') {
|
|
|
|
/* quote characters */
|
1998-08-05 20:29:04 +04:00
|
|
|
dlen -= 2;
|
|
|
|
if (dlen < 1)
|
|
|
|
break;
|
1997-08-27 15:22:52 +04:00
|
|
|
*dst++ = '\\';
|
|
|
|
*dst++ = '\\';
|
|
|
|
} else {
|
|
|
|
/* normal characters */
|
1998-08-05 20:29:04 +04:00
|
|
|
if (--dlen < 1)
|
|
|
|
break;
|
1997-08-27 15:22:52 +04:00
|
|
|
*dst++ = *src;
|
|
|
|
}
|
1998-08-05 20:29:04 +04:00
|
|
|
++src, --slen;
|
1997-08-27 15:22:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
*dst++ = 0;
|
|
|
|
}
|