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