Home | History | Annotate | Line # | Download | only in scsipi
scsipiconf.c revision 1.37
      1 /*	$NetBSD: scsipiconf.c,v 1.37 2010/05/30 04:38:04 pgoyette Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum; by Jason R. Thorpe of the Numerical Aerospace
      9  * Simulation Facility, NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Originally written by Julian Elischer (julian (at) tfs.com)
     35  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     36  *
     37  * TRW Financial Systems, in accordance with their agreement with Carnegie
     38  * Mellon University, makes this software available to CMU to distribute
     39  * or use in any manner that they see fit as long as this message is kept with
     40  * the software. For this reason TFS also grants any other persons or
     41  * organisations permission to use or modify this software.
     42  *
     43  * TFS supplies this software to be publicly redistributed
     44  * on the understanding that TFS is not responsible for the correct
     45  * functioning of this software in any circumstances.
     46  *
     47  * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     48  */
     49 
     50 #include <sys/cdefs.h>
     51 __KERNEL_RCSID(0, "$NetBSD: scsipiconf.c,v 1.37 2010/05/30 04:38:04 pgoyette Exp $");
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/malloc.h>
     56 #include <sys/device.h>
     57 #include <sys/proc.h>
     58 
     59 #include <dev/scsipi/scsipi_all.h>
     60 #include <dev/scsipi/scsipiconf.h>
     61 #include <dev/scsipi/scsipi_base.h>
     62 
     63 #define	STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
     64 
     65 /* Function pointers and stub routines for scsiverbose module */
     66 int     (*scsipi_print_sense)(struct scsipi_xfer *, int) =
     67 		scsipi_print_sense_stub;
     68 void    (*scsipi_print_sense_data)(struct scsi_sense_data *, int) =
     69 		scsipi_print_sense_data_stub;
     70 char   *(*scsipi_decode_sense)(void *, int) = scsipi_decode_sense_stub;
     71 
     72 int     scsipi_print_sense_stub(struct scsipi_xfer * xs, int verbosity)
     73 {
     74 	return 0;
     75 }
     76 
     77 void    scsipi_print_sense_data_stub(struct scsi_sense_data *sense,
     78 				     int verbosity)
     79 {
     80 	return;
     81 }
     82 
     83 char   *scsipi_decode_sense_stub(void *sinfo, int flag)
     84 {
     85 	return NULL;
     86 }
     87 
     88 int
     89 scsipi_command(struct scsipi_periph *periph, struct scsipi_generic *cmd,
     90     int cmdlen, u_char *data_addr, int datalen, int retries, int timeout,
     91     struct buf *bp, int flags)
     92 {
     93 	struct scsipi_xfer *xs;
     94 
     95 	xs = scsipi_make_xs(periph, cmd, cmdlen, data_addr, datalen, retries,
     96 	    timeout, bp, flags);
     97 	if (!xs)
     98 		return (ENOMEM);
     99 
    100 	return (scsipi_execute_xs(xs));
    101 }
    102 
    103 /*
    104  * allocate and init a scsipi_periph structure for a new device.
    105  */
    106 struct scsipi_periph *
    107 scsipi_alloc_periph(int malloc_flag)
    108 {
    109 	struct scsipi_periph *periph;
    110 	u_int i;
    111 
    112 	periph = malloc(sizeof(*periph), M_DEVBUF, malloc_flag|M_ZERO);
    113 	if (periph == NULL)
    114 		return NULL;
    115 
    116 	periph->periph_dev = NULL;
    117 
    118 	/*
    119 	 * Start with one command opening.  The periph driver
    120 	 * will grow this if it knows it can take advantage of it.
    121 	 */
    122 	periph->periph_openings = 1;
    123 	periph->periph_active = 0;
    124 
    125 	for (i = 0; i < PERIPH_NTAGWORDS; i++)
    126 		periph->periph_freetags[i] = 0xffffffff;
    127 
    128 	TAILQ_INIT(&periph->periph_xferq);
    129 	callout_init(&periph->periph_callout, 0);
    130 
    131 	return periph;
    132 }
    133 
    134 /*
    135  * Return a priority based on how much of the inquiry data matches
    136  * the patterns for the particular driver.
    137  */
    138 const void *
    139 scsipi_inqmatch(struct scsipi_inquiry_pattern *inqbuf, const void *base,
    140     size_t nmatches, size_t matchsize, int *bestpriority)
    141 {
    142 	u_int8_t type;
    143 	const struct scsipi_inquiry_pattern *bestmatch;
    144 
    145 	/* Include the qualifier to catch vendor-unique types. */
    146 	type = inqbuf->type;
    147 
    148 	for (*bestpriority = 0, bestmatch = 0; nmatches--;
    149 	    base = (const char *)base + matchsize) {
    150 		const struct scsipi_inquiry_pattern *match = base;
    151 		int priority, len;
    152 
    153 		if (type != match->type)
    154 			continue;
    155 		if (inqbuf->removable != match->removable)
    156 			continue;
    157 		priority = 2;
    158 		len = strlen(match->vendor);
    159 		if (memcmp(inqbuf->vendor, match->vendor, len))
    160 			continue;
    161 		priority += len;
    162 		len = strlen(match->product);
    163 		if (memcmp(inqbuf->product, match->product, len))
    164 			continue;
    165 		priority += len;
    166 		len = strlen(match->revision);
    167 		if (memcmp(inqbuf->revision, match->revision, len))
    168 			continue;
    169 		priority += len;
    170 
    171 #ifdef SCSIPI_DEBUG
    172 		printf("scsipi_inqmatch: %d/%d/%d <%s, %s, %s>\n",
    173 		    priority, match->type, match->removable,
    174 		    match->vendor, match->product, match->revision);
    175 #endif
    176 		if (priority > *bestpriority) {
    177 			*bestpriority = priority;
    178 			bestmatch = base;
    179 		}
    180 	}
    181 
    182 	return (bestmatch);
    183 }
    184 
    185 const char *
    186 scsipi_dtype(int type)
    187 {
    188 	const char *dtype;
    189 
    190 	switch (type) {
    191 	case T_DIRECT:
    192 		dtype = "disk";
    193 		break;
    194 	case T_SEQUENTIAL:
    195 		dtype = "tape";
    196 		break;
    197 	case T_PRINTER:
    198 		dtype = "printer";
    199 		break;
    200 	case T_PROCESSOR:
    201 		dtype = "processor";
    202 		break;
    203 	case T_WORM:
    204 		dtype = "worm";
    205 		break;
    206 	case T_CDROM:
    207 		dtype = "cdrom";
    208 		break;
    209 	case T_SCANNER:
    210 		dtype = "scanner";
    211 		break;
    212 	case T_OPTICAL:
    213 		dtype = "optical";
    214 		break;
    215 	case T_CHANGER:
    216 		dtype = "changer";
    217 		break;
    218 	case T_COMM:
    219 		dtype = "communication";
    220 		break;
    221 	case T_IT8_1:
    222 	case T_IT8_2:
    223 		dtype = "graphic arts pre-press";
    224 		break;
    225 	case T_STORARRAY:
    226 		dtype = "storage array";
    227 		break;
    228 	case T_ENCLOSURE:
    229 		dtype = "enclosure services";
    230 		break;
    231 	case T_SIMPLE_DIRECT:
    232 		dtype = "simplified direct";
    233 		break;
    234 	case T_OPTIC_CARD_RW:
    235 		dtype = "optical card r/w";
    236 		break;
    237 	case T_OBJECT_STORED:
    238 		dtype = "object-based storage";
    239 		break;
    240 	case T_NODEVICE:
    241 		panic("scsipi_dtype: impossible device type");
    242 	default:
    243 		dtype = "unknown";
    244 		break;
    245 	}
    246 	return (dtype);
    247 }
    248 
    249 void
    250 scsipi_strvis(u_char *dst, int dlen, const u_char *src, int slen)
    251 {
    252 
    253 	/* Trim leading and trailing blanks and NULs. */
    254 	while (slen > 0 && STRVIS_ISWHITE(src[0]))
    255 		++src, --slen;
    256 	while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
    257 		--slen;
    258 
    259 	while (slen > 0) {
    260 		if (*src < 0x20 || *src >= 0x80) {
    261 			/* non-printable characters */
    262 			dlen -= 4;
    263 			if (dlen < 1)
    264 				break;
    265 			*dst++ = '\\';
    266 			*dst++ = ((*src & 0300) >> 6) + '0';
    267 			*dst++ = ((*src & 0070) >> 3) + '0';
    268 			*dst++ = ((*src & 0007) >> 0) + '0';
    269 		} else if (*src == '\\') {
    270 			/* quote characters */
    271 			dlen -= 2;
    272 			if (dlen < 1)
    273 				break;
    274 			*dst++ = '\\';
    275 			*dst++ = '\\';
    276 		} else {
    277 			/* normal characters */
    278 			if (--dlen < 1)
    279 				break;
    280 			*dst++ = *src;
    281 		}
    282 		++src, --slen;
    283 	}
    284 
    285 	*dst++ = 0;
    286 }
    287