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