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