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