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