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