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