1 | /* $NetBSD: scsipiconf.c,v 1.42 2016/11/20 15:37:19 mlelstv 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 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
24 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
30 | * POSSIBILITY OF SUCH DAMAGE. |
31 | */ |
32 | |
33 | /* |
34 | * Originally written by Julian Elischer (julian@tfs.com) |
35 | * for TRW Financial Systems for use under the MACH(2.5) operating system. |
36 | * |
37 | * TRW Financial Systems, in accordance with their agreement with Carnegie |
38 | * Mellon University, makes this software available to CMU to distribute |
39 | * or use in any manner that they see fit as long as this message is kept with |
40 | * the software. For this reason TFS also grants any other persons or |
41 | * organisations permission to use or modify this software. |
42 | * |
43 | * TFS supplies this software to be publicly redistributed |
44 | * on the understanding that TFS is not responsible for the correct |
45 | * functioning of this software in any circumstances. |
46 | * |
47 | * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 |
48 | */ |
49 | |
50 | #include <sys/cdefs.h> |
51 | __KERNEL_RCSID(0, "$NetBSD: scsipiconf.c,v 1.42 2016/11/20 15:37:19 mlelstv Exp $" ); |
52 | |
53 | #include <sys/param.h> |
54 | #include <sys/systm.h> |
55 | #include <sys/malloc.h> |
56 | #include <sys/module.h> |
57 | #include <sys/device.h> |
58 | #include <sys/proc.h> |
59 | |
60 | #include <dev/scsipi/scsipi_all.h> |
61 | #include <dev/scsipi/scsipiconf.h> |
62 | #include <dev/scsipi/scsipi_base.h> |
63 | |
64 | |
65 | /* Function pointers and stub routines for scsiverbose module */ |
66 | int (*scsipi_print_sense)(struct scsipi_xfer *, int) = scsipi_print_sense_stub; |
67 | void (*scsipi_print_sense_data)(struct scsi_sense_data *, int) = |
68 | scsipi_print_sense_data_stub; |
69 | |
70 | int scsi_verbose_loaded = 0; |
71 | |
72 | int |
73 | scsipi_print_sense_stub(struct scsipi_xfer * xs, int verbosity) |
74 | { |
75 | scsipi_load_verbose(); |
76 | if (scsi_verbose_loaded) |
77 | return scsipi_print_sense(xs, verbosity); |
78 | else |
79 | return 0; |
80 | } |
81 | |
82 | void |
83 | scsipi_print_sense_data_stub(struct scsi_sense_data *sense, int verbosity) |
84 | { |
85 | scsipi_load_verbose(); |
86 | if (scsi_verbose_loaded) |
87 | scsipi_print_sense_data(sense, verbosity); |
88 | } |
89 | |
90 | int |
91 | scsipi_command(struct scsipi_periph *periph, struct scsipi_generic *cmd, |
92 | int cmdlen, u_char *data_addr, int datalen, int retries, int timeout, |
93 | struct buf *bp, int flags) |
94 | { |
95 | struct scsipi_xfer *xs; |
96 | int rc; |
97 | |
98 | /* |
99 | * execute unlocked to allow waiting for memory |
100 | */ |
101 | xs = scsipi_make_xs_unlocked(periph, cmd, cmdlen, data_addr, datalen, retries, |
102 | timeout, bp, flags); |
103 | if (!xs) |
104 | return (ENOMEM); |
105 | |
106 | mutex_enter(chan_mtx(periph->periph_channel)); |
107 | rc = scsipi_execute_xs(xs); |
108 | mutex_exit(chan_mtx(periph->periph_channel)); |
109 | |
110 | return rc; |
111 | } |
112 | |
113 | /* |
114 | * Load the scsiverbose module |
115 | */ |
116 | void |
117 | scsipi_load_verbose(void) |
118 | { |
119 | if (scsi_verbose_loaded == 0) |
120 | module_autoload("scsiverbose" , MODULE_CLASS_MISC); |
121 | } |
122 | |
123 | /* |
124 | * allocate and init a scsipi_periph structure for a new device. |
125 | */ |
126 | struct scsipi_periph * |
127 | scsipi_alloc_periph(int malloc_flag) |
128 | { |
129 | struct scsipi_periph *periph; |
130 | u_int i; |
131 | |
132 | periph = malloc(sizeof(*periph), M_DEVBUF, malloc_flag|M_ZERO); |
133 | if (periph == NULL) |
134 | return NULL; |
135 | |
136 | periph->periph_dev = NULL; |
137 | |
138 | /* |
139 | * Start with one command opening. The periph driver |
140 | * will grow this if it knows it can take advantage of it. |
141 | */ |
142 | periph->periph_openings = 1; |
143 | periph->periph_active = 0; |
144 | |
145 | for (i = 0; i < PERIPH_NTAGWORDS; i++) |
146 | periph->periph_freetags[i] = 0xffffffff; |
147 | |
148 | TAILQ_INIT(&periph->periph_xferq); |
149 | callout_init(&periph->periph_callout, 0); |
150 | cv_init(&periph->periph_cv, "periph" ); |
151 | |
152 | return periph; |
153 | } |
154 | |
155 | /* |
156 | * cleanup and free scsipi_periph structure |
157 | */ |
158 | void |
159 | scsipi_free_periph(struct scsipi_periph *periph) |
160 | { |
161 | cv_destroy(&periph->periph_cv); |
162 | free(periph, M_DEVBUF); |
163 | } |
164 | |
165 | /* |
166 | * Return a priority based on how much of the inquiry data matches |
167 | * the patterns for the particular driver. |
168 | */ |
169 | const void * |
170 | scsipi_inqmatch(struct scsipi_inquiry_pattern *inqbuf, const void *base, |
171 | size_t nmatches, size_t matchsize, int *bestpriority) |
172 | { |
173 | u_int8_t type; |
174 | const struct scsipi_inquiry_pattern *bestmatch; |
175 | |
176 | /* Include the qualifier to catch vendor-unique types. */ |
177 | type = inqbuf->type; |
178 | |
179 | for (*bestpriority = 0, bestmatch = 0; nmatches--; |
180 | base = (const char *)base + matchsize) { |
181 | const struct scsipi_inquiry_pattern *match = base; |
182 | int priority, len; |
183 | |
184 | if (type != match->type) |
185 | continue; |
186 | if (inqbuf->removable != match->removable) |
187 | continue; |
188 | priority = 2; |
189 | len = strlen(match->vendor); |
190 | if (memcmp(inqbuf->vendor, match->vendor, len)) |
191 | continue; |
192 | priority += len; |
193 | len = strlen(match->product); |
194 | if (memcmp(inqbuf->product, match->product, len)) |
195 | continue; |
196 | priority += len; |
197 | len = strlen(match->revision); |
198 | if (memcmp(inqbuf->revision, match->revision, len)) |
199 | continue; |
200 | priority += len; |
201 | |
202 | #ifdef SCSIPI_DEBUG |
203 | printf("scsipi_inqmatch: %d/%d/%d <%s, %s, %s>\n" , |
204 | priority, match->type, match->removable, |
205 | match->vendor, match->product, match->revision); |
206 | #endif |
207 | if (priority > *bestpriority) { |
208 | *bestpriority = priority; |
209 | bestmatch = base; |
210 | } |
211 | } |
212 | |
213 | return (bestmatch); |
214 | } |
215 | |
216 | const char * |
217 | scsipi_dtype(int type) |
218 | { |
219 | const char *dtype; |
220 | |
221 | switch (type) { |
222 | case T_DIRECT: |
223 | dtype = "disk" ; |
224 | break; |
225 | case T_SEQUENTIAL: |
226 | dtype = "tape" ; |
227 | break; |
228 | case T_PRINTER: |
229 | dtype = "printer" ; |
230 | break; |
231 | case T_PROCESSOR: |
232 | dtype = "processor" ; |
233 | break; |
234 | case T_WORM: |
235 | dtype = "worm" ; |
236 | break; |
237 | case T_CDROM: |
238 | dtype = "cdrom" ; |
239 | break; |
240 | case T_SCANNER: |
241 | dtype = "scanner" ; |
242 | break; |
243 | case T_OPTICAL: |
244 | dtype = "optical" ; |
245 | break; |
246 | case T_CHANGER: |
247 | dtype = "changer" ; |
248 | break; |
249 | case T_COMM: |
250 | dtype = "communication" ; |
251 | break; |
252 | case T_IT8_1: |
253 | case T_IT8_2: |
254 | dtype = "graphic arts pre-press" ; |
255 | break; |
256 | case T_STORARRAY: |
257 | dtype = "storage array" ; |
258 | break; |
259 | case T_ENCLOSURE: |
260 | dtype = "enclosure services" ; |
261 | break; |
262 | case T_SIMPLE_DIRECT: |
263 | dtype = "simplified direct" ; |
264 | break; |
265 | case T_OPTIC_CARD_RW: |
266 | dtype = "optical card r/w" ; |
267 | break; |
268 | case T_OBJECT_STORED: |
269 | dtype = "object-based storage" ; |
270 | break; |
271 | case T_NODEVICE: |
272 | panic("scsipi_dtype: impossible device type" ); |
273 | default: |
274 | dtype = "unknown" ; |
275 | break; |
276 | } |
277 | return (dtype); |
278 | } |
279 | |