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