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