scsi_base.c revision 1.26 1 /* $NetBSD: scsi_base.c,v 1.26 1995/01/16 21:34:10 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 for (; n; 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 break;
349 }
350 if (!silent) {
351 sc_print_addr(sc_link);
352 printf("%sready\n", n ? "" : "not ");
353 }
354 out:
355 scsi_free_xs(xs, SCSI_NOSLEEP);
356 return error;
357 }
358
359 /*
360 * This routine is called by the scsi interrupt when the transfer is complete.
361 */
362 void
363 scsi_done(xs)
364 struct scsi_xfer *xs;
365 {
366 struct scsi_link *sc_link = xs->sc_link;
367 int error;
368
369 SC_DEBUG(sc_link, SDEV_DB2, ("scsi_done\n"));
370 #ifdef SCSIDEBUG
371 if ((sc_link->flags & SDEV_DB1) != 0)
372 show_scsi_cmd(xs);
373 #endif /* SCSIDEBUG */
374
375 /*
376 * If it's a user level request, bypass all usual completion processing,
377 * let the user work it out.. We take reponsibility for freeing the
378 * xs when the user returns. (and restarting the device's queue).
379 */
380 if ((xs->flags & SCSI_USER) != 0) {
381 SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
382 scsi_user_done(xs); /* to take a copy of the sense etc. */
383 SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
384
385 scsi_free_xs(xs, SCSI_NOSLEEP); /* restarts queue too */
386 SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
387 return;
388 }
389
390 /*
391 * If the device has it's own done routine, call it first.
392 * If it returns a legit error value, return that, otherwise
393 * it wants us to continue with normal processing.
394 */
395 if (sc_link->device->done) {
396 SC_DEBUG(sc_link, SDEV_DB2, ("calling private done()\n"));
397 error = (*sc_link->device->done) (xs);
398 if (error == EJUSTRETURN)
399 goto done;
400 SC_DEBUG(sc_link, SDEV_DB3, ("continuing with generic done()\n"));
401 }
402 if (xs->bp == NULL) {
403 /*
404 * if it's a normal upper level request, then ask
405 * the upper level code to handle error checking
406 * rather than doing it here at interrupt time
407 */
408 wakeup(xs);
409 return;
410 }
411 /*
412 * Go and handle errors now.
413 * If it returns ERESTART then we should RETRY
414 */
415 retry:
416 if (sc_err1(xs, 1) == ERESTART) {
417 switch ((*(sc_link->adapter->scsi_cmd)) (xs)) {
418 case SUCCESSFULLY_QUEUED:
419 return;
420
421 case TRY_AGAIN_LATER:
422 xs->error = XS_BUSY;
423 case COMPLETE:
424 goto retry;
425 }
426 }
427 done:
428 scsi_free_xs(xs, SCSI_NOSLEEP);
429 }
430
431 int
432 scsi_execute_xs(xs)
433 struct scsi_xfer *xs;
434 {
435 int error;
436 int s;
437
438 xs->flags &= ~ITSDONE;
439 xs->error = XS_NOERROR;
440 xs->resid = xs->datalen;
441
442 retry:
443 /*
444 * Do the transfer. If we are polling we will return:
445 * COMPLETE, Was poll, and scsi_done has been called
446 * TRY_AGAIN_LATER, Adapter short resources, try again
447 *
448 * if under full steam (interrupts) it will return:
449 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
450 * TRY_AGAIN_LATER, (as for polling)
451 * After the wakeup, we must still check if it succeeded
452 *
453 * If we have a bp however, all the error proccessing
454 * and the buffer code both expect us to return straight
455 * to them, so as soon as the command is queued, return
456 */
457 switch ((*(xs->sc_link->adapter->scsi_cmd)) (xs)) {
458 case SUCCESSFULLY_QUEUED:
459 if (xs->bp)
460 return EJUSTRETURN;
461 s = splbio();
462 while ((xs->flags & ITSDONE) == 0)
463 tsleep(xs, PRIBIO + 1, "scsi_scsi_cmd", 0);
464 splx(s);
465 case COMPLETE: /* Polling command completed ok */
466 if (xs->bp)
467 return EJUSTRETURN;
468 doit:
469 SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
470 if ((error = sc_err1(xs, 0)) != ERESTART)
471 return error;
472 goto retry;
473
474 case TRY_AGAIN_LATER: /* adapter resource shortage */
475 xs->error = XS_BUSY;
476 goto doit;
477
478 default:
479 panic("scsi_execute_xs: invalid return code");
480 }
481
482 #ifdef DIAGNOSTIC
483 panic("scsi_execute_xs: impossible");
484 #endif
485 }
486
487 /*
488 * ask the scsi driver to perform a command for us.
489 * tell it where to read/write the data, and how
490 * long the data is supposed to be. If we have a buf
491 * to associate with the transfer, we need that too.
492 */
493 int
494 scsi_scsi_cmd(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
495 retries, timeout, bp, flags)
496 struct scsi_link *sc_link;
497 struct scsi_generic *scsi_cmd;
498 int cmdlen;
499 u_char *data_addr;
500 int datalen;
501 int retries;
502 int timeout;
503 struct buf *bp;
504 int flags;
505 {
506 struct scsi_xfer *xs;
507 int error;
508
509 SC_DEBUG(sc_link, SDEV_DB2, ("scsi_cmd\n"));
510
511 #ifdef DIAGNOSTIC
512 if (bp != 0 && (flags & SCSI_NOSLEEP) == 0)
513 panic("scsi_scsi_cmd: buffer without nosleep");
514 #endif
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 if (sense->extended_extra_len != 0) {
766 int n;
767 printf(", data =");
768 for (n = 0; n < sense->extended_extra_len; n++)
769 printf(" %02x", sense->extended_extra_bytes[n]);
770 }
771 printf("\n");
772 }
773 return error;
774
775 /*
776 * Not code 70, just report it
777 */
778 default:
779 sc_print_addr(sc_link);
780 printf("error code %d",
781 sense->error_code & SSD_ERRCODE);
782 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
783 printf(" at block no. %d (decimal)",
784 (sense->XXX_unextended_blockhi << 16) +
785 (sense->XXX_unextended_blockmed << 8) +
786 (sense->XXX_unextended_blocklow));
787 }
788 printf("\n");
789 return EIO;
790 }
791 }
792
793 /*
794 * Utility routines often used in SCSI stuff
795 */
796
797 /*
798 * convert a physical address to 3 bytes,
799 * MSB at the lowest address,
800 * LSB at the highest.
801 */
802 void
803 lto3b(val, bytes)
804 u_int32_t val;
805 u_int8_t *bytes;
806 {
807
808 *bytes++ = (val & 0xff0000) >> 16;
809 *bytes++ = (val & 0xff00) >> 8;
810 *bytes = val & 0xff;
811 }
812
813 /*
814 * The reverse of lto3b
815 */
816 u_int32_t
817 _3btol(bytes)
818 u_int8_t *bytes;
819 {
820 u_int32_t rc;
821
822 rc = (*bytes++ << 16);
823 rc += (*bytes++ << 8);
824 rc += *bytes;
825 return (rc);
826 }
827
828 /*
829 * Print out the scsi_link structure's address info.
830 */
831 void
832 sc_print_addr(sc_link)
833 struct scsi_link *sc_link;
834 {
835
836 printf("%s(%s:%d:%d): ",
837 sc_link->device_softc ?
838 ((struct device *)sc_link->device_softc)->dv_xname : "probe",
839 ((struct device *)sc_link->adapter_softc)->dv_xname,
840 sc_link->target, sc_link->lun);
841 }
842
843 #ifdef SCSIDEBUG
844 /*
845 * Given a scsi_xfer, dump the request, in all it's glory
846 */
847 void
848 show_scsi_xs(xs)
849 struct scsi_xfer *xs;
850 {
851 printf("xs(0x%x): ", xs);
852 printf("flg(0x%x)", xs->flags);
853 printf("sc_link(0x%x)", xs->sc_link);
854 printf("retr(0x%x)", xs->retries);
855 printf("timo(0x%x)", xs->timeout);
856 printf("cmd(0x%x)", xs->cmd);
857 printf("len(0x%x)", xs->cmdlen);
858 printf("data(0x%x)", xs->data);
859 printf("len(0x%x)", xs->datalen);
860 printf("res(0x%x)", xs->resid);
861 printf("err(0x%x)", xs->error);
862 printf("bp(0x%x)", xs->bp);
863 show_scsi_cmd(xs);
864 }
865
866 void
867 show_scsi_cmd(xs)
868 struct scsi_xfer *xs;
869 {
870 u_char *b = (u_char *) xs->cmd;
871 int i = 0;
872
873 sc_print_addr(xs->sc_link);
874 printf("command: ");
875
876 if ((xs->flags & SCSI_RESET) == 0) {
877 while (i < xs->cmdlen) {
878 if (i)
879 printf(",");
880 printf("%x", b[i++]);
881 }
882 printf("-[%d bytes]\n", xs->datalen);
883 if (xs->datalen)
884 show_mem(xs->data, min(64, xs->datalen));
885 } else
886 printf("-RESET-\n");
887 }
888
889 void
890 show_mem(address, num)
891 u_char *address;
892 int num;
893 {
894 int x;
895
896 printf("------------------------------");
897 for (x = 0; x < num; x++) {
898 if ((x % 16) == 0)
899 printf("\n%03d: ", x);
900 printf("%02x ", *address++);
901 }
902 printf("\n------------------------------\n");
903 }
904 #endif /*SCSIDEBUG */
905