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