scsi_base.c revision 1.31 1 1.31 thorpej /* $NetBSD: scsi_base.c,v 1.31 1996/01/12 22:43:29 thorpej Exp $ */
2 1.14 cgd
3 1.1 mycroft /*
4 1.28 mycroft * Copyright (c) 1994, 1995 Charles Hannum. All rights reserved.
5 1.9 mycroft *
6 1.9 mycroft * Redistribution and use in source and binary forms, with or without
7 1.9 mycroft * modification, are permitted provided that the following conditions
8 1.9 mycroft * are met:
9 1.9 mycroft * 1. Redistributions of source code must retain the above copyright
10 1.9 mycroft * notice, this list of conditions and the following disclaimer.
11 1.9 mycroft * 2. Redistributions in binary form must reproduce the above copyright
12 1.9 mycroft * notice, this list of conditions and the following disclaimer in the
13 1.9 mycroft * documentation and/or other materials provided with the distribution.
14 1.9 mycroft * 3. All advertising materials mentioning features or use of this software
15 1.9 mycroft * must display the following acknowledgement:
16 1.9 mycroft * This product includes software developed by Charles Hannum.
17 1.9 mycroft * 4. The name of the author may not be used to endorse or promote products
18 1.9 mycroft * derived from this software without specific prior written permission.
19 1.9 mycroft *
20 1.9 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.9 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.9 mycroft * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.9 mycroft * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.9 mycroft * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.9 mycroft * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.9 mycroft * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.9 mycroft * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.9 mycroft * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.9 mycroft * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.9 mycroft */
31 1.9 mycroft
32 1.9 mycroft /*
33 1.9 mycroft * Originally written by Julian Elischer (julian (at) dialix.oz.au)
34 1.1 mycroft */
35 1.1 mycroft
36 1.1 mycroft #include <sys/types.h>
37 1.1 mycroft #include <sys/param.h>
38 1.30 thorpej #include <sys/systm.h>
39 1.11 mycroft #include <sys/kernel.h>
40 1.1 mycroft #include <sys/buf.h>
41 1.1 mycroft #include <sys/uio.h>
42 1.1 mycroft #include <sys/malloc.h>
43 1.1 mycroft #include <sys/errno.h>
44 1.1 mycroft #include <sys/device.h>
45 1.2 mycroft
46 1.1 mycroft #include <scsi/scsi_all.h>
47 1.1 mycroft #include <scsi/scsi_disk.h>
48 1.1 mycroft #include <scsi/scsiconf.h>
49 1.1 mycroft
50 1.22 mycroft void scsi_error __P((struct scsi_xfer *, int));
51 1.22 mycroft
52 1.12 mycroft LIST_HEAD(xs_free_list, scsi_xfer) xs_free_list;
53 1.1 mycroft
54 1.1 mycroft /*
55 1.1 mycroft * Get a scsi transfer structure for the caller. Charge the structure
56 1.1 mycroft * to the device that is referenced by the sc_link structure. If the
57 1.1 mycroft * sc_link structure has no 'credits' then the device already has the
58 1.1 mycroft * maximum number or outstanding operations under way. In this stage,
59 1.1 mycroft * wait on the structure so that when one is freed, we are awoken again
60 1.1 mycroft * If the SCSI_NOSLEEP flag is set, then do not wait, but rather, return
61 1.1 mycroft * a NULL pointer, signifying that no slots were available
62 1.1 mycroft * Note in the link structure, that we are waiting on it.
63 1.1 mycroft */
64 1.1 mycroft
65 1.1 mycroft struct scsi_xfer *
66 1.22 mycroft scsi_get_xs(sc_link, flags)
67 1.2 mycroft struct scsi_link *sc_link; /* who to charge the xs to */
68 1.11 mycroft int flags; /* if this call can sleep */
69 1.1 mycroft {
70 1.2 mycroft struct scsi_xfer *xs;
71 1.11 mycroft int s;
72 1.1 mycroft
73 1.22 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("scsi_get_xs\n"));
74 1.1 mycroft s = splbio();
75 1.22 mycroft while (sc_link->openings <= 0) {
76 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("sleeping\n"));
77 1.22 mycroft if ((flags & SCSI_NOSLEEP) != 0) {
78 1.1 mycroft splx(s);
79 1.1 mycroft return 0;
80 1.1 mycroft }
81 1.1 mycroft sc_link->flags |= SDEV_WAITING;
82 1.12 mycroft (void) tsleep(sc_link, PRIBIO, "getxs", 0);
83 1.1 mycroft }
84 1.22 mycroft sc_link->openings--;
85 1.12 mycroft if (xs = xs_free_list.lh_first) {
86 1.12 mycroft LIST_REMOVE(xs, free_list);
87 1.1 mycroft splx(s);
88 1.1 mycroft } else {
89 1.1 mycroft splx(s);
90 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("making\n"));
91 1.12 mycroft xs = malloc(sizeof(*xs), M_DEVBUF,
92 1.22 mycroft ((flags & SCSI_NOSLEEP) != 0 ? M_NOWAIT : M_WAITOK));
93 1.12 mycroft if (!xs) {
94 1.1 mycroft sc_print_addr(sc_link);
95 1.1 mycroft printf("cannot allocate scsi xs\n");
96 1.12 mycroft return 0;
97 1.1 mycroft }
98 1.1 mycroft }
99 1.22 mycroft
100 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("returning\n"));
101 1.22 mycroft xs->flags = INUSE | flags;
102 1.11 mycroft return xs;
103 1.1 mycroft }
104 1.1 mycroft
105 1.1 mycroft /*
106 1.1 mycroft * Given a scsi_xfer struct, and a device (referenced through sc_link)
107 1.1 mycroft * return the struct to the free pool and credit the device with it
108 1.1 mycroft * If another process is waiting for an xs, do a wakeup, let it proceed
109 1.1 mycroft */
110 1.1 mycroft void
111 1.22 mycroft scsi_free_xs(xs, flags)
112 1.1 mycroft struct scsi_xfer *xs;
113 1.11 mycroft int flags;
114 1.1 mycroft {
115 1.22 mycroft struct scsi_link *sc_link = xs->sc_link;
116 1.22 mycroft
117 1.22 mycroft xs->flags &= ~INUSE;
118 1.12 mycroft LIST_INSERT_HEAD(&xs_free_list, xs, free_list);
119 1.1 mycroft
120 1.22 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("scsi_free_xs\n"));
121 1.1 mycroft /* if was 0 and someone waits, wake them up */
122 1.22 mycroft sc_link->openings++;
123 1.22 mycroft if ((sc_link->flags & SDEV_WAITING) != 0) {
124 1.3 mycroft sc_link->flags &= ~SDEV_WAITING;
125 1.22 mycroft wakeup(sc_link);
126 1.3 mycroft } else {
127 1.1 mycroft if (sc_link->device->start) {
128 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("calling private start()\n"));
129 1.17 mycroft (*(sc_link->device->start)) (sc_link->device_softc);
130 1.1 mycroft }
131 1.3 mycroft }
132 1.1 mycroft }
133 1.1 mycroft
134 1.1 mycroft /*
135 1.22 mycroft * Make a scsi_xfer, and return a pointer to it.
136 1.22 mycroft */
137 1.22 mycroft static __inline struct scsi_xfer *
138 1.22 mycroft scsi_make_xs(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
139 1.22 mycroft retries, timeout, bp, flags)
140 1.22 mycroft struct scsi_link *sc_link;
141 1.22 mycroft struct scsi_generic *scsi_cmd;
142 1.22 mycroft int cmdlen;
143 1.22 mycroft u_char *data_addr;
144 1.22 mycroft int datalen;
145 1.22 mycroft int retries;
146 1.22 mycroft int timeout;
147 1.22 mycroft struct buf *bp;
148 1.22 mycroft int flags;
149 1.22 mycroft {
150 1.22 mycroft struct scsi_xfer *xs;
151 1.22 mycroft
152 1.22 mycroft if ((xs = scsi_get_xs(sc_link, flags)) == NULL)
153 1.22 mycroft return NULL;
154 1.22 mycroft
155 1.22 mycroft /*
156 1.22 mycroft * Fill out the scsi_xfer structure. We don't know whose context
157 1.22 mycroft * the cmd is in, so copy it.
158 1.22 mycroft */
159 1.22 mycroft xs->sc_link = sc_link;
160 1.22 mycroft bcopy(scsi_cmd, &xs->cmdstore, cmdlen);
161 1.22 mycroft xs->cmd = &xs->cmdstore;
162 1.22 mycroft xs->cmdlen = cmdlen;
163 1.22 mycroft xs->data = data_addr;
164 1.22 mycroft xs->datalen = datalen;
165 1.22 mycroft xs->retries = retries;
166 1.22 mycroft xs->timeout = timeout;
167 1.22 mycroft xs->bp = bp;
168 1.22 mycroft
169 1.22 mycroft return xs;
170 1.22 mycroft }
171 1.22 mycroft
172 1.22 mycroft /*
173 1.1 mycroft * Find out from the device what its capacity is.
174 1.1 mycroft */
175 1.22 mycroft u_long
176 1.1 mycroft scsi_size(sc_link, flags)
177 1.1 mycroft struct scsi_link *sc_link;
178 1.11 mycroft int flags;
179 1.1 mycroft {
180 1.1 mycroft struct scsi_read_cap_data rdcap;
181 1.1 mycroft struct scsi_read_capacity scsi_cmd;
182 1.22 mycroft u_long size;
183 1.1 mycroft
184 1.1 mycroft /*
185 1.1 mycroft * make up a scsi command and ask the scsi driver to do
186 1.1 mycroft * it for you.
187 1.1 mycroft */
188 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
189 1.22 mycroft scsi_cmd.opcode = READ_CAPACITY;
190 1.1 mycroft
191 1.1 mycroft /*
192 1.1 mycroft * If the command works, interpret the result as a 4 byte
193 1.1 mycroft * number of blocks
194 1.1 mycroft */
195 1.22 mycroft if (scsi_scsi_cmd(sc_link, (struct scsi_generic *)&scsi_cmd,
196 1.22 mycroft sizeof(scsi_cmd), (u_char *)&rdcap, sizeof(rdcap),
197 1.2 mycroft 2, 20000, NULL, flags | SCSI_DATA_IN) != 0) {
198 1.1 mycroft sc_print_addr(sc_link);
199 1.1 mycroft printf("could not get size\n");
200 1.11 mycroft return 0;
201 1.1 mycroft } else {
202 1.1 mycroft size = rdcap.addr_0 + 1;
203 1.1 mycroft size += rdcap.addr_1 << 8;
204 1.1 mycroft size += rdcap.addr_2 << 16;
205 1.1 mycroft size += rdcap.addr_3 << 24;
206 1.1 mycroft }
207 1.11 mycroft return size;
208 1.1 mycroft }
209 1.1 mycroft
210 1.1 mycroft /*
211 1.1 mycroft * Get scsi driver to send a "are you ready?" command
212 1.1 mycroft */
213 1.2 mycroft int
214 1.1 mycroft scsi_test_unit_ready(sc_link, flags)
215 1.1 mycroft struct scsi_link *sc_link;
216 1.11 mycroft int flags;
217 1.1 mycroft {
218 1.1 mycroft struct scsi_test_unit_ready scsi_cmd;
219 1.1 mycroft
220 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
221 1.22 mycroft scsi_cmd.opcode = TEST_UNIT_READY;
222 1.1 mycroft
223 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
224 1.22 mycroft sizeof(scsi_cmd), 0, 0, 2, 10000, NULL, flags);
225 1.1 mycroft }
226 1.1 mycroft
227 1.1 mycroft /*
228 1.1 mycroft * Do a scsi operation, asking a device to run as SCSI-II if it can.
229 1.1 mycroft */
230 1.2 mycroft int
231 1.1 mycroft scsi_change_def(sc_link, flags)
232 1.1 mycroft struct scsi_link *sc_link;
233 1.11 mycroft int flags;
234 1.1 mycroft {
235 1.1 mycroft struct scsi_changedef scsi_cmd;
236 1.1 mycroft
237 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
238 1.22 mycroft scsi_cmd.opcode = CHANGE_DEFINITION;
239 1.1 mycroft scsi_cmd.how = SC_SCSI_2;
240 1.1 mycroft
241 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
242 1.2 mycroft sizeof(scsi_cmd), 0, 0, 2, 100000, NULL, flags);
243 1.1 mycroft }
244 1.1 mycroft
245 1.1 mycroft /*
246 1.1 mycroft * Do a scsi operation asking a device what it is
247 1.1 mycroft * Use the scsi_cmd routine in the switch table.
248 1.1 mycroft */
249 1.2 mycroft int
250 1.1 mycroft scsi_inquire(sc_link, inqbuf, flags)
251 1.1 mycroft struct scsi_link *sc_link;
252 1.1 mycroft struct scsi_inquiry_data *inqbuf;
253 1.11 mycroft int flags;
254 1.1 mycroft {
255 1.1 mycroft struct scsi_inquiry scsi_cmd;
256 1.1 mycroft
257 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
258 1.22 mycroft scsi_cmd.opcode = INQUIRY;
259 1.1 mycroft scsi_cmd.length = sizeof(struct scsi_inquiry_data);
260 1.1 mycroft
261 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
262 1.2 mycroft sizeof(scsi_cmd), (u_char *) inqbuf,
263 1.22 mycroft sizeof(struct scsi_inquiry_data), 2, 10000, NULL,
264 1.2 mycroft SCSI_DATA_IN | flags);
265 1.1 mycroft }
266 1.1 mycroft
267 1.1 mycroft /*
268 1.1 mycroft * Prevent or allow the user to remove the media
269 1.1 mycroft */
270 1.2 mycroft int
271 1.1 mycroft scsi_prevent(sc_link, type, flags)
272 1.1 mycroft struct scsi_link *sc_link;
273 1.11 mycroft int type, flags;
274 1.1 mycroft {
275 1.1 mycroft struct scsi_prevent scsi_cmd;
276 1.1 mycroft
277 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
278 1.22 mycroft scsi_cmd.opcode = PREVENT_ALLOW;
279 1.1 mycroft scsi_cmd.how = type;
280 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
281 1.2 mycroft sizeof(scsi_cmd), 0, 0, 2, 5000, NULL, flags);
282 1.1 mycroft }
283 1.1 mycroft
284 1.1 mycroft /*
285 1.1 mycroft * Get scsi driver to send a "start up" command
286 1.1 mycroft */
287 1.2 mycroft int
288 1.10 mycroft scsi_start(sc_link, type, flags)
289 1.1 mycroft struct scsi_link *sc_link;
290 1.11 mycroft int type, flags;
291 1.1 mycroft {
292 1.1 mycroft struct scsi_start_stop scsi_cmd;
293 1.1 mycroft
294 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
295 1.22 mycroft scsi_cmd.opcode = START_STOP;
296 1.22 mycroft scsi_cmd.byte2 = 0x00;
297 1.10 mycroft scsi_cmd.how = type;
298 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
299 1.10 mycroft sizeof(scsi_cmd), 0, 0, 2,
300 1.22 mycroft type == SSS_START ? 30000 : 10000, NULL, flags);
301 1.1 mycroft }
302 1.1 mycroft
303 1.1 mycroft /*
304 1.1 mycroft * This routine is called by the scsi interrupt when the transfer is complete.
305 1.1 mycroft */
306 1.1 mycroft void
307 1.1 mycroft scsi_done(xs)
308 1.1 mycroft struct scsi_xfer *xs;
309 1.1 mycroft {
310 1.1 mycroft struct scsi_link *sc_link = xs->sc_link;
311 1.11 mycroft int error;
312 1.1 mycroft
313 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("scsi_done\n"));
314 1.1 mycroft #ifdef SCSIDEBUG
315 1.22 mycroft if ((sc_link->flags & SDEV_DB1) != 0)
316 1.1 mycroft show_scsi_cmd(xs);
317 1.22 mycroft #endif /* SCSIDEBUG */
318 1.22 mycroft
319 1.1 mycroft /*
320 1.1 mycroft * If it's a user level request, bypass all usual completion processing,
321 1.1 mycroft * let the user work it out.. We take reponsibility for freeing the
322 1.1 mycroft * xs when the user returns. (and restarting the device's queue).
323 1.1 mycroft */
324 1.22 mycroft if ((xs->flags & SCSI_USER) != 0) {
325 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
326 1.1 mycroft scsi_user_done(xs); /* to take a copy of the sense etc. */
327 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
328 1.19 mycroft
329 1.22 mycroft scsi_free_xs(xs, SCSI_NOSLEEP); /* restarts queue too */
330 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
331 1.1 mycroft return;
332 1.1 mycroft }
333 1.22 mycroft
334 1.1 mycroft /*
335 1.1 mycroft * If the device has it's own done routine, call it first.
336 1.1 mycroft * If it returns a legit error value, return that, otherwise
337 1.1 mycroft * it wants us to continue with normal processing.
338 1.31 thorpej *
339 1.31 thorpej * Make sure the upper-level driver knows that this might not
340 1.31 thorpej * actually be the last time they hear from us. We need to get
341 1.31 thorpej * status back.
342 1.1 mycroft */
343 1.1 mycroft if (sc_link->device->done) {
344 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("calling private done()\n"));
345 1.31 thorpej error = (*sc_link->device->done)(xs, 0);
346 1.22 mycroft if (error == EJUSTRETURN)
347 1.22 mycroft goto done;
348 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("continuing with generic done()\n"));
349 1.1 mycroft }
350 1.22 mycroft if (xs->bp == NULL) {
351 1.1 mycroft /*
352 1.1 mycroft * if it's a normal upper level request, then ask
353 1.1 mycroft * the upper level code to handle error checking
354 1.1 mycroft * rather than doing it here at interrupt time
355 1.1 mycroft */
356 1.1 mycroft wakeup(xs);
357 1.1 mycroft return;
358 1.1 mycroft }
359 1.1 mycroft /*
360 1.1 mycroft * Go and handle errors now.
361 1.22 mycroft * If it returns ERESTART then we should RETRY
362 1.1 mycroft */
363 1.22 mycroft retry:
364 1.22 mycroft if (sc_err1(xs, 1) == ERESTART) {
365 1.22 mycroft switch ((*(sc_link->adapter->scsi_cmd)) (xs)) {
366 1.22 mycroft case SUCCESSFULLY_QUEUED:
367 1.1 mycroft return;
368 1.22 mycroft
369 1.22 mycroft case TRY_AGAIN_LATER:
370 1.22 mycroft xs->error = XS_BUSY;
371 1.22 mycroft case COMPLETE:
372 1.22 mycroft goto retry;
373 1.22 mycroft }
374 1.1 mycroft }
375 1.22 mycroft done:
376 1.31 thorpej if (sc_link->device->done) {
377 1.31 thorpej /*
378 1.31 thorpej * Tell the device the operation is actually complete.
379 1.31 thorpej * No more will happen with this xfer. This for
380 1.31 thorpej * notification of the upper-level driver only; they
381 1.31 thorpej * won't be returning any meaningful information to us.
382 1.31 thorpej */
383 1.31 thorpej (void)(*sc_link->device->done)(xs, 1);
384 1.31 thorpej }
385 1.22 mycroft scsi_free_xs(xs, SCSI_NOSLEEP);
386 1.1 mycroft }
387 1.1 mycroft
388 1.22 mycroft int
389 1.22 mycroft scsi_execute_xs(xs)
390 1.22 mycroft struct scsi_xfer *xs;
391 1.1 mycroft {
392 1.11 mycroft int error;
393 1.11 mycroft int s;
394 1.1 mycroft
395 1.22 mycroft xs->flags &= ~ITSDONE;
396 1.22 mycroft xs->error = XS_NOERROR;
397 1.22 mycroft xs->resid = xs->datalen;
398 1.1 mycroft
399 1.1 mycroft retry:
400 1.1 mycroft /*
401 1.1 mycroft * Do the transfer. If we are polling we will return:
402 1.1 mycroft * COMPLETE, Was poll, and scsi_done has been called
403 1.1 mycroft * TRY_AGAIN_LATER, Adapter short resources, try again
404 1.1 mycroft *
405 1.1 mycroft * if under full steam (interrupts) it will return:
406 1.1 mycroft * SUCCESSFULLY_QUEUED, will do a wakeup when complete
407 1.1 mycroft * TRY_AGAIN_LATER, (as for polling)
408 1.1 mycroft * After the wakeup, we must still check if it succeeded
409 1.1 mycroft *
410 1.1 mycroft * If we have a bp however, all the error proccessing
411 1.1 mycroft * and the buffer code both expect us to return straight
412 1.1 mycroft * to them, so as soon as the command is queued, return
413 1.1 mycroft */
414 1.22 mycroft switch ((*(xs->sc_link->adapter->scsi_cmd)) (xs)) {
415 1.1 mycroft case SUCCESSFULLY_QUEUED:
416 1.22 mycroft if (xs->bp)
417 1.22 mycroft return EJUSTRETURN;
418 1.1 mycroft s = splbio();
419 1.22 mycroft while ((xs->flags & ITSDONE) == 0)
420 1.4 mycroft tsleep(xs, PRIBIO + 1, "scsi_scsi_cmd", 0);
421 1.1 mycroft splx(s);
422 1.1 mycroft case COMPLETE: /* Polling command completed ok */
423 1.22 mycroft if (xs->bp)
424 1.22 mycroft return EJUSTRETURN;
425 1.22 mycroft doit:
426 1.24 mycroft SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
427 1.22 mycroft if ((error = sc_err1(xs, 0)) != ERESTART)
428 1.22 mycroft return error;
429 1.22 mycroft goto retry;
430 1.1 mycroft
431 1.1 mycroft case TRY_AGAIN_LATER: /* adapter resource shortage */
432 1.22 mycroft xs->error = XS_BUSY;
433 1.22 mycroft goto doit;
434 1.22 mycroft
435 1.1 mycroft default:
436 1.22 mycroft panic("scsi_execute_xs: invalid return code");
437 1.1 mycroft }
438 1.22 mycroft
439 1.22 mycroft #ifdef DIAGNOSTIC
440 1.22 mycroft panic("scsi_execute_xs: impossible");
441 1.22 mycroft #endif
442 1.22 mycroft }
443 1.22 mycroft
444 1.22 mycroft /*
445 1.22 mycroft * ask the scsi driver to perform a command for us.
446 1.22 mycroft * tell it where to read/write the data, and how
447 1.22 mycroft * long the data is supposed to be. If we have a buf
448 1.22 mycroft * to associate with the transfer, we need that too.
449 1.22 mycroft */
450 1.22 mycroft int
451 1.22 mycroft scsi_scsi_cmd(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
452 1.22 mycroft retries, timeout, bp, flags)
453 1.22 mycroft struct scsi_link *sc_link;
454 1.22 mycroft struct scsi_generic *scsi_cmd;
455 1.22 mycroft int cmdlen;
456 1.22 mycroft u_char *data_addr;
457 1.22 mycroft int datalen;
458 1.22 mycroft int retries;
459 1.22 mycroft int timeout;
460 1.22 mycroft struct buf *bp;
461 1.22 mycroft int flags;
462 1.22 mycroft {
463 1.22 mycroft struct scsi_xfer *xs;
464 1.22 mycroft int error;
465 1.22 mycroft
466 1.22 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("scsi_cmd\n"));
467 1.22 mycroft
468 1.25 mycroft #ifdef DIAGNOSTIC
469 1.25 mycroft if (bp != 0 && (flags & SCSI_NOSLEEP) == 0)
470 1.25 mycroft panic("scsi_scsi_cmd: buffer without nosleep");
471 1.25 mycroft #endif
472 1.25 mycroft
473 1.22 mycroft if ((xs = scsi_make_xs(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
474 1.22 mycroft retries, timeout, bp, flags)) == NULL)
475 1.22 mycroft return ENOMEM;
476 1.22 mycroft
477 1.22 mycroft if ((error = scsi_execute_xs(xs)) == EJUSTRETURN)
478 1.22 mycroft return 0;
479 1.22 mycroft
480 1.1 mycroft /*
481 1.1 mycroft * we have finished with the xfer stuct, free it and
482 1.1 mycroft * check if anyone else needs to be started up.
483 1.1 mycroft */
484 1.22 mycroft scsi_free_xs(xs, flags);
485 1.11 mycroft return error;
486 1.1 mycroft }
487 1.1 mycroft
488 1.2 mycroft int
489 1.22 mycroft sc_err1(xs, async)
490 1.1 mycroft struct scsi_xfer *xs;
491 1.22 mycroft int async;
492 1.1 mycroft {
493 1.11 mycroft int error;
494 1.1 mycroft
495 1.1 mycroft SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
496 1.22 mycroft
497 1.1 mycroft /*
498 1.1 mycroft * If it has a buf, we might be working with
499 1.1 mycroft * a request from the buffer cache or some other
500 1.1 mycroft * piece of code that requires us to process
501 1.1 mycroft * errors at inetrrupt time. We have probably
502 1.1 mycroft * been called by scsi_done()
503 1.1 mycroft */
504 1.1 mycroft switch (xs->error) {
505 1.1 mycroft case XS_NOERROR: /* nearly always hit this one */
506 1.11 mycroft error = 0;
507 1.1 mycroft break;
508 1.1 mycroft
509 1.1 mycroft case XS_SENSE:
510 1.22 mycroft if ((error = scsi_interpret_sense(xs)) == ERESTART)
511 1.22 mycroft goto retry;
512 1.21 mycroft SC_DEBUG(xs->sc_link, SDEV_DB3,
513 1.21 mycroft ("scsi_interpret_sense returned %d\n", error));
514 1.1 mycroft break;
515 1.1 mycroft
516 1.1 mycroft case XS_BUSY:
517 1.22 mycroft if (xs->retries) {
518 1.22 mycroft if ((xs->flags & SCSI_POLL) != 0)
519 1.22 mycroft delay(1000000);
520 1.22 mycroft else if ((xs->flags & SCSI_NOSLEEP) == 0)
521 1.22 mycroft tsleep(&lbolt, PRIBIO, "scbusy", 0);
522 1.22 mycroft else
523 1.22 mycroft #if 0
524 1.22 mycroft timeout(scsi_requeue, xs, hz);
525 1.22 mycroft #else
526 1.22 mycroft goto lose;
527 1.22 mycroft #endif
528 1.22 mycroft }
529 1.1 mycroft case XS_TIMEOUT:
530 1.22 mycroft retry:
531 1.1 mycroft if (xs->retries--) {
532 1.1 mycroft xs->error = XS_NOERROR;
533 1.1 mycroft xs->flags &= ~ITSDONE;
534 1.22 mycroft return ERESTART;
535 1.1 mycroft }
536 1.1 mycroft case XS_DRIVER_STUFFUP:
537 1.22 mycroft lose:
538 1.22 mycroft error = EIO;
539 1.22 mycroft break;
540 1.22 mycroft
541 1.22 mycroft case XS_SELTIMEOUT:
542 1.22 mycroft /* XXX Disable device? */
543 1.11 mycroft error = EIO;
544 1.1 mycroft break;
545 1.21 mycroft
546 1.1 mycroft default:
547 1.1 mycroft sc_print_addr(xs->sc_link);
548 1.1 mycroft printf("unknown error category from scsi driver\n");
549 1.21 mycroft error = EIO;
550 1.21 mycroft break;
551 1.21 mycroft }
552 1.21 mycroft
553 1.22 mycroft scsi_error(xs, error);
554 1.22 mycroft return error;
555 1.22 mycroft }
556 1.22 mycroft
557 1.22 mycroft void
558 1.22 mycroft scsi_error(xs, error)
559 1.22 mycroft struct scsi_xfer *xs;
560 1.22 mycroft int error;
561 1.22 mycroft {
562 1.22 mycroft struct buf *bp = xs->bp;
563 1.22 mycroft
564 1.21 mycroft if (bp) {
565 1.21 mycroft if (error) {
566 1.22 mycroft bp->b_error = error;
567 1.21 mycroft bp->b_flags |= B_ERROR;
568 1.21 mycroft bp->b_resid = bp->b_bcount;
569 1.21 mycroft } else {
570 1.21 mycroft bp->b_error = 0;
571 1.22 mycroft bp->b_resid = xs->resid;
572 1.21 mycroft }
573 1.22 mycroft biodone(bp);
574 1.1 mycroft }
575 1.1 mycroft }
576 1.1 mycroft
577 1.1 mycroft /*
578 1.1 mycroft * Look at the returned sense and act on the error, determining
579 1.1 mycroft * the unix error number to pass back. (0 = report no error)
580 1.1 mycroft *
581 1.1 mycroft * THIS IS THE DEFAULT ERROR HANDLER
582 1.1 mycroft */
583 1.2 mycroft int
584 1.1 mycroft scsi_interpret_sense(xs)
585 1.1 mycroft struct scsi_xfer *xs;
586 1.1 mycroft {
587 1.1 mycroft struct scsi_sense_data *sense;
588 1.1 mycroft struct scsi_link *sc_link = xs->sc_link;
589 1.22 mycroft u_int8_t key;
590 1.22 mycroft u_int32_t info;
591 1.2 mycroft int error;
592 1.1 mycroft
593 1.22 mycroft static char *error_mes[] = {
594 1.22 mycroft "soft error (corrected)",
595 1.22 mycroft "not ready", "medium error",
596 1.22 mycroft "non-media hardware failure", "illegal request",
597 1.22 mycroft "unit attention", "readonly device",
598 1.22 mycroft "no data found", "vendor unique",
599 1.22 mycroft "copy aborted", "command aborted",
600 1.22 mycroft "search returned equal", "volume overflow",
601 1.22 mycroft "verify miscompare", "unknown error key"
602 1.1 mycroft };
603 1.1 mycroft
604 1.11 mycroft sense = &xs->sense;
605 1.1 mycroft #ifdef SCSIDEBUG
606 1.22 mycroft if ((sc_link->flags & SDEV_DB1) != 0) {
607 1.22 mycroft int count;
608 1.1 mycroft printf("code%x valid%x ",
609 1.1 mycroft sense->error_code & SSD_ERRCODE,
610 1.1 mycroft sense->error_code & SSD_ERRCODE_VALID ? 1 : 0);
611 1.1 mycroft printf("seg%x key%x ili%x eom%x fmark%x\n",
612 1.13 chopps sense->extended_segment,
613 1.13 chopps sense->extended_flags & SSD_KEY,
614 1.13 chopps sense->extended_flags & SSD_ILI ? 1 : 0,
615 1.13 chopps sense->extended_flags & SSD_EOM ? 1 : 0,
616 1.13 chopps sense->extended_flags & SSD_FILEMARK ? 1 : 0);
617 1.1 mycroft printf("info: %x %x %x %x followed by %d extra bytes\n",
618 1.13 chopps sense->extended_info[0],
619 1.13 chopps sense->extended_info[1],
620 1.13 chopps sense->extended_info[2],
621 1.13 chopps sense->extended_info[3],
622 1.13 chopps sense->extended_extra_len);
623 1.1 mycroft printf("extra: ");
624 1.22 mycroft for (count = 0; count < sense->extended_extra_len; count++)
625 1.22 mycroft printf("%x ", sense->extended_extra_bytes[count]);
626 1.1 mycroft printf("\n");
627 1.1 mycroft }
628 1.1 mycroft #endif /*SCSIDEBUG */
629 1.1 mycroft /*
630 1.1 mycroft * If the device has it's own error handler, call it first.
631 1.1 mycroft * If it returns a legit error value, return that, otherwise
632 1.1 mycroft * it wants us to continue with normal error processing.
633 1.1 mycroft */
634 1.1 mycroft if (sc_link->device->err_handler) {
635 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("calling private err_handler()\n"));
636 1.2 mycroft error = (*sc_link->device->err_handler) (xs);
637 1.2 mycroft if (error != -1)
638 1.2 mycroft return error; /* error >= 0 better ? */
639 1.1 mycroft }
640 1.1 mycroft /* otherwise use the default */
641 1.1 mycroft switch (sense->error_code & SSD_ERRCODE) {
642 1.1 mycroft /*
643 1.1 mycroft * If it's code 70, use the extended stuff and interpret the key
644 1.1 mycroft */
645 1.1 mycroft case 0x71: /* delayed error */
646 1.1 mycroft sc_print_addr(sc_link);
647 1.13 chopps key = sense->extended_flags & SSD_KEY;
648 1.1 mycroft printf(" DELAYED ERROR, key = 0x%x\n", key);
649 1.1 mycroft case 0x70:
650 1.22 mycroft if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
651 1.18 deraadt bcopy(sense->extended_info, &info, sizeof info);
652 1.16 deraadt info = ntohl(info);
653 1.16 deraadt } else
654 1.1 mycroft info = 0;
655 1.13 chopps key = sense->extended_flags & SSD_KEY;
656 1.1 mycroft
657 1.22 mycroft switch (key) {
658 1.23 mycroft case 0x0: /* NO SENSE */
659 1.22 mycroft case 0x1: /* RECOVERED ERROR */
660 1.22 mycroft if (xs->resid == xs->datalen)
661 1.22 mycroft xs->resid = 0; /* not short read */
662 1.22 mycroft case 0xc: /* EQUAL */
663 1.22 mycroft error = 0;
664 1.22 mycroft break;
665 1.22 mycroft case 0x2: /* NOT READY */
666 1.22 mycroft if ((sc_link->flags & SDEV_REMOVABLE) != 0)
667 1.22 mycroft sc_link->flags &= ~SDEV_MEDIA_LOADED;
668 1.22 mycroft if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
669 1.22 mycroft return 0;
670 1.22 mycroft if ((xs->flags & SCSI_SILENT) != 0)
671 1.22 mycroft return EIO;
672 1.22 mycroft error = EIO;
673 1.22 mycroft break;
674 1.22 mycroft case 0x5: /* ILLEGAL REQUEST */
675 1.22 mycroft if ((xs->flags & SCSI_IGNORE_ILLEGAL_REQUEST) != 0)
676 1.22 mycroft return 0;
677 1.22 mycroft error = EINVAL;
678 1.22 mycroft break;
679 1.22 mycroft case 0x6: /* UNIT ATTENTION */
680 1.22 mycroft if ((sc_link->flags & SDEV_REMOVABLE) != 0)
681 1.22 mycroft sc_link->flags &= ~SDEV_MEDIA_LOADED;
682 1.22 mycroft if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 ||
683 1.22 mycroft /* XXX Should reupload any transient state. */
684 1.22 mycroft (sc_link->flags & SDEV_REMOVABLE) == 0)
685 1.22 mycroft return ERESTART;
686 1.22 mycroft if ((xs->flags & SCSI_SILENT) != 0)
687 1.22 mycroft return EIO;
688 1.22 mycroft error = EIO;
689 1.22 mycroft break;
690 1.22 mycroft case 0x7: /* DATA PROTECT */
691 1.22 mycroft error = EACCES;
692 1.22 mycroft break;
693 1.22 mycroft case 0x8: /* BLANK CHECK */
694 1.22 mycroft error = 0;
695 1.22 mycroft break;
696 1.22 mycroft case 0xd: /* VOLUME OVERFLOW */
697 1.22 mycroft error = ENOSPC;
698 1.22 mycroft break;
699 1.22 mycroft default:
700 1.22 mycroft error = EIO;
701 1.22 mycroft break;
702 1.22 mycroft }
703 1.22 mycroft
704 1.22 mycroft if (key) {
705 1.1 mycroft sc_print_addr(sc_link);
706 1.1 mycroft printf("%s", error_mes[key - 1]);
707 1.22 mycroft if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
708 1.1 mycroft switch (key) {
709 1.23 mycroft case 0x2: /* NOT READY */
710 1.23 mycroft case 0x5: /* ILLEGAL REQUEST */
711 1.23 mycroft case 0x6: /* UNIT ATTENTION */
712 1.1 mycroft case 0x7: /* DATA PROTECT */
713 1.1 mycroft break;
714 1.1 mycroft case 0x8: /* BLANK CHECK */
715 1.1 mycroft printf(", requested size: %d (decimal)",
716 1.1 mycroft info);
717 1.1 mycroft break;
718 1.1 mycroft default:
719 1.1 mycroft printf(", info = %d (decimal)", info);
720 1.1 mycroft }
721 1.25 mycroft }
722 1.25 mycroft if (sense->extended_extra_len != 0) {
723 1.25 mycroft int n;
724 1.25 mycroft printf(", data =");
725 1.25 mycroft for (n = 0; n < sense->extended_extra_len; n++)
726 1.25 mycroft printf(" %02x", sense->extended_extra_bytes[n]);
727 1.1 mycroft }
728 1.1 mycroft printf("\n");
729 1.1 mycroft }
730 1.22 mycroft return error;
731 1.22 mycroft
732 1.1 mycroft /*
733 1.1 mycroft * Not code 70, just report it
734 1.1 mycroft */
735 1.1 mycroft default:
736 1.22 mycroft sc_print_addr(sc_link);
737 1.22 mycroft printf("error code %d",
738 1.22 mycroft sense->error_code & SSD_ERRCODE);
739 1.22 mycroft if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
740 1.22 mycroft printf(" at block no. %d (decimal)",
741 1.22 mycroft (sense->XXX_unextended_blockhi << 16) +
742 1.22 mycroft (sense->XXX_unextended_blockmed << 8) +
743 1.22 mycroft (sense->XXX_unextended_blocklow));
744 1.1 mycroft }
745 1.22 mycroft printf("\n");
746 1.11 mycroft return EIO;
747 1.1 mycroft }
748 1.1 mycroft }
749 1.1 mycroft
750 1.1 mycroft /*
751 1.1 mycroft * Utility routines often used in SCSI stuff
752 1.1 mycroft */
753 1.1 mycroft
754 1.1 mycroft /*
755 1.1 mycroft * convert a physical address to 3 bytes,
756 1.1 mycroft * MSB at the lowest address,
757 1.1 mycroft * LSB at the highest.
758 1.1 mycroft */
759 1.1 mycroft void
760 1.1 mycroft lto3b(val, bytes)
761 1.22 mycroft u_int32_t val;
762 1.22 mycroft u_int8_t *bytes;
763 1.1 mycroft {
764 1.22 mycroft
765 1.29 mycroft *bytes++ = (val >> 16) & 0xff;
766 1.29 mycroft *bytes++ = (val >> 8) & 0xff;
767 1.1 mycroft *bytes = val & 0xff;
768 1.1 mycroft }
769 1.1 mycroft
770 1.1 mycroft /*
771 1.1 mycroft * The reverse of lto3b
772 1.1 mycroft */
773 1.22 mycroft u_int32_t
774 1.1 mycroft _3btol(bytes)
775 1.22 mycroft u_int8_t *bytes;
776 1.1 mycroft {
777 1.22 mycroft u_int32_t rc;
778 1.22 mycroft
779 1.1 mycroft rc = (*bytes++ << 16);
780 1.1 mycroft rc += (*bytes++ << 8);
781 1.1 mycroft rc += *bytes;
782 1.22 mycroft return (rc);
783 1.1 mycroft }
784 1.1 mycroft
785 1.1 mycroft /*
786 1.1 mycroft * Print out the scsi_link structure's address info.
787 1.1 mycroft */
788 1.1 mycroft void
789 1.1 mycroft sc_print_addr(sc_link)
790 1.22 mycroft struct scsi_link *sc_link;
791 1.1 mycroft {
792 1.1 mycroft
793 1.17 mycroft printf("%s(%s:%d:%d): ",
794 1.22 mycroft sc_link->device_softc ?
795 1.22 mycroft ((struct device *)sc_link->device_softc)->dv_xname : "probe",
796 1.22 mycroft ((struct device *)sc_link->adapter_softc)->dv_xname,
797 1.22 mycroft sc_link->target, sc_link->lun);
798 1.1 mycroft }
799 1.1 mycroft
800 1.1 mycroft #ifdef SCSIDEBUG
801 1.1 mycroft /*
802 1.1 mycroft * Given a scsi_xfer, dump the request, in all it's glory
803 1.1 mycroft */
804 1.1 mycroft void
805 1.1 mycroft show_scsi_xs(xs)
806 1.1 mycroft struct scsi_xfer *xs;
807 1.1 mycroft {
808 1.1 mycroft printf("xs(0x%x): ", xs);
809 1.1 mycroft printf("flg(0x%x)", xs->flags);
810 1.1 mycroft printf("sc_link(0x%x)", xs->sc_link);
811 1.1 mycroft printf("retr(0x%x)", xs->retries);
812 1.1 mycroft printf("timo(0x%x)", xs->timeout);
813 1.1 mycroft printf("cmd(0x%x)", xs->cmd);
814 1.1 mycroft printf("len(0x%x)", xs->cmdlen);
815 1.1 mycroft printf("data(0x%x)", xs->data);
816 1.1 mycroft printf("len(0x%x)", xs->datalen);
817 1.1 mycroft printf("res(0x%x)", xs->resid);
818 1.1 mycroft printf("err(0x%x)", xs->error);
819 1.1 mycroft printf("bp(0x%x)", xs->bp);
820 1.1 mycroft show_scsi_cmd(xs);
821 1.1 mycroft }
822 1.1 mycroft
823 1.1 mycroft void
824 1.15 deraadt show_scsi_cmd(xs)
825 1.15 deraadt struct scsi_xfer *xs;
826 1.1 mycroft {
827 1.1 mycroft u_char *b = (u_char *) xs->cmd;
828 1.1 mycroft int i = 0;
829 1.1 mycroft
830 1.1 mycroft sc_print_addr(xs->sc_link);
831 1.1 mycroft printf("command: ");
832 1.1 mycroft
833 1.22 mycroft if ((xs->flags & SCSI_RESET) == 0) {
834 1.1 mycroft while (i < xs->cmdlen) {
835 1.1 mycroft if (i)
836 1.1 mycroft printf(",");
837 1.1 mycroft printf("%x", b[i++]);
838 1.1 mycroft }
839 1.1 mycroft printf("-[%d bytes]\n", xs->datalen);
840 1.1 mycroft if (xs->datalen)
841 1.1 mycroft show_mem(xs->data, min(64, xs->datalen));
842 1.2 mycroft } else
843 1.1 mycroft printf("-RESET-\n");
844 1.1 mycroft }
845 1.1 mycroft
846 1.1 mycroft void
847 1.1 mycroft show_mem(address, num)
848 1.24 mycroft u_char *address;
849 1.24 mycroft int num;
850 1.1 mycroft {
851 1.24 mycroft int x;
852 1.24 mycroft
853 1.1 mycroft printf("------------------------------");
854 1.24 mycroft for (x = 0; x < num; x++) {
855 1.24 mycroft if ((x % 16) == 0)
856 1.24 mycroft printf("\n%03d: ", x);
857 1.1 mycroft printf("%02x ", *address++);
858 1.1 mycroft }
859 1.1 mycroft printf("\n------------------------------\n");
860 1.1 mycroft }
861 1.1 mycroft #endif /*SCSIDEBUG */
862