Home | History | Annotate | Line # | Download | only in scsipi
scsipiconf.c revision 1.40
      1 /*	$NetBSD: scsipiconf.c,v 1.40 2010/08/21 13:18:36 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.40 2010/08/21 13:18:36 pgoyette Exp $");
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/malloc.h>
     56 #include <sys/module.h>
     57 #include <sys/device.h>
     58 #include <sys/proc.h>
     59 
     60 #include <dev/scsipi/scsipi_all.h>
     61 #include <dev/scsipi/scsipiconf.h>
     62 #include <dev/scsipi/scsipi_base.h>
     63 
     64 #define	STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
     65 
     66 /* Function pointers and stub routines for scsiverbose module */
     67 int (*scsipi_print_sense)(struct scsipi_xfer *, int) = scsipi_print_sense_stub;
     68 void (*scsipi_print_sense_data)(struct scsi_sense_data *, int) =
     69 		scsipi_print_sense_data_stub;
     70 
     71 int scsi_verbose_loaded = 0;
     72 
     73 int scsipi_print_sense_stub(struct scsipi_xfer * xs, int verbosity)
     74 {
     75 	scsipi_load_verbose();
     76 	if (scsi_verbose_loaded)
     77 		return scsipi_print_sense(xs, verbosity);
     78 	else
     79 		return 0;
     80 }
     81 
     82 void scsipi_print_sense_data_stub(struct scsi_sense_data *sense, int verbosity)
     83 {
     84 	scsipi_load_verbose();
     85 	if (scsi_verbose_loaded)
     86 		scsipi_print_sense_data(sense, verbosity);
     87 }
     88 
     89 int
     90 scsipi_command(struct scsipi_periph *periph, struct scsipi_generic *cmd,
     91     int cmdlen, u_char *data_addr, int datalen, int retries, int timeout,
     92     struct buf *bp, int flags)
     93 {
     94 	struct scsipi_xfer *xs;
     95 
     96 	xs = scsipi_make_xs(periph, cmd, cmdlen, data_addr, datalen, retries,
     97 	    timeout, bp, flags);
     98 	if (!xs)
     99 		return (ENOMEM);
    100 
    101 	return (scsipi_execute_xs(xs));
    102 }
    103 
    104 /*
    105  * Load the scsiverbose module
    106  */
    107 void
    108 scsipi_load_verbose(void)
    109 {
    110 	if (scsi_verbose_loaded == 0)
    111 		module_autoload("scsiverbose", MODULE_CLASS_MISC);
    112 }
    113 
    114 /*
    115  * allocate and init a scsipi_periph structure for a new device.
    116  */
    117 struct scsipi_periph *
    118 scsipi_alloc_periph(int malloc_flag)
    119 {
    120 	struct scsipi_periph *periph;
    121 	u_int i;
    122 
    123 	periph = malloc(sizeof(*periph), M_DEVBUF, malloc_flag|M_ZERO);
    124 	if (periph == NULL)
    125 		return NULL;
    126 
    127 	periph->periph_dev = NULL;
    128 
    129 	/*
    130 	 * Start with one command opening.  The periph driver
    131 	 * will grow this if it knows it can take advantage of it.
    132 	 */
    133 	periph->periph_openings = 1;
    134 	periph->periph_active = 0;
    135 
    136 	for (i = 0; i < PERIPH_NTAGWORDS; i++)
    137 		periph->periph_freetags[i] = 0xffffffff;
    138 
    139 	TAILQ_INIT(&periph->periph_xferq);
    140 	callout_init(&periph->periph_callout, 0);
    141 
    142 	return periph;
    143 }
    144 
    145 /*
    146  * Return a priority based on how much of the inquiry data matches
    147  * the patterns for the particular driver.
    148  */
    149 const void *
    150 scsipi_inqmatch(struct scsipi_inquiry_pattern *inqbuf, const void *base,
    151     size_t nmatches, size_t matchsize, int *bestpriority)
    152 {
    153 	u_int8_t type;
    154 	const struct scsipi_inquiry_pattern *bestmatch;
    155 
    156 	/* Include the qualifier to catch vendor-unique types. */
    157 	type = inqbuf->type;
    158 
    159 	for (*bestpriority = 0, bestmatch = 0; nmatches--;
    160 	    base = (const char *)base + matchsize) {
    161 		const struct scsipi_inquiry_pattern *match = base;
    162 		int priority, len;
    163 
    164 		if (type != match->type)
    165 			continue;
    166 		if (inqbuf->removable != match->removable)
    167 			continue;
    168 		priority = 2;
    169 		len = strlen(match->vendor);
    170 		if (memcmp(inqbuf->vendor, match->vendor, len))
    171 			continue;
    172 		priority += len;
    173 		len = strlen(match->product);
    174 		if (memcmp(inqbuf->product, match->product, len))
    175 			continue;
    176 		priority += len;
    177 		len = strlen(match->revision);
    178 		if (memcmp(inqbuf->revision, match->revision, len))
    179 			continue;
    180 		priority += len;
    181 
    182 #ifdef SCSIPI_DEBUG
    183 		printf("scsipi_inqmatch: %d/%d/%d <%s, %s, %s>\n",
    184 		    priority, match->type, match->removable,
    185 		    match->vendor, match->product, match->revision);
    186 #endif
    187 		if (priority > *bestpriority) {
    188 			*bestpriority = priority;
    189 			bestmatch = base;
    190 		}
    191 	}
    192 
    193 	return (bestmatch);
    194 }
    195 
    196 const char *
    197 scsipi_dtype(int type)
    198 {
    199 	const char *dtype;
    200 
    201 	switch (type) {
    202 	case T_DIRECT:
    203 		dtype = "disk";
    204 		break;
    205 	case T_SEQUENTIAL:
    206 		dtype = "tape";
    207 		break;
    208 	case T_PRINTER:
    209 		dtype = "printer";
    210 		break;
    211 	case T_PROCESSOR:
    212 		dtype = "processor";
    213 		break;
    214 	case T_WORM:
    215 		dtype = "worm";
    216 		break;
    217 	case T_CDROM:
    218 		dtype = "cdrom";
    219 		break;
    220 	case T_SCANNER:
    221 		dtype = "scanner";
    222 		break;
    223 	case T_OPTICAL:
    224 		dtype = "optical";
    225 		break;
    226 	case T_CHANGER:
    227 		dtype = "changer";
    228 		break;
    229 	case T_COMM:
    230 		dtype = "communication";
    231 		break;
    232 	case T_IT8_1:
    233 	case T_IT8_2:
    234 		dtype = "graphic arts pre-press";
    235 		break;
    236 	case T_STORARRAY:
    237 		dtype = "storage array";
    238 		break;
    239 	case T_ENCLOSURE:
    240 		dtype = "enclosure services";
    241 		break;
    242 	case T_SIMPLE_DIRECT:
    243 		dtype = "simplified direct";
    244 		break;
    245 	case T_OPTIC_CARD_RW:
    246 		dtype = "optical card r/w";
    247 		break;
    248 	case T_OBJECT_STORED:
    249 		dtype = "object-based storage";
    250 		break;
    251 	case T_NODEVICE:
    252 		panic("scsipi_dtype: impossible device type");
    253 	default:
    254 		dtype = "unknown";
    255 		break;
    256 	}
    257 	return (dtype);
    258 }
    259 
    260 void
    261 scsipi_strvis(u_char *dst, int dlen, const u_char *src, int slen)
    262 {
    263 
    264 	/* Trim leading and trailing blanks and NULs. */
    265 	while (slen > 0 && STRVIS_ISWHITE(src[0]))
    266 		++src, --slen;
    267 	while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
    268 		--slen;
    269 
    270 	while (slen > 0) {
    271 		if (*src < 0x20 || *src >= 0x80) {
    272 			/* non-printable characters */
    273 			dlen -= 4;
    274 			if (dlen < 1)
    275 				break;
    276 			*dst++ = '\\';
    277 			*dst++ = ((*src & 0300) >> 6) + '0';
    278 			*dst++ = ((*src & 0070) >> 3) + '0';
    279 			*dst++ = ((*src & 0007) >> 0) + '0';
    280 		} else if (*src == '\\') {
    281 			/* quote characters */
    282 			dlen -= 2;
    283 			if (dlen < 1)
    284 				break;
    285 			*dst++ = '\\';
    286 			*dst++ = '\\';
    287 		} else {
    288 			/* normal characters */
    289 			if (--dlen < 1)
    290 				break;
    291 			*dst++ = *src;
    292 		}
    293 		++src, --slen;
    294 	}
    295 
    296 	*dst++ = 0;
    297 }
    298