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