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