scsipi_base.c revision 1.15 1 /* $NetBSD: scsipi_base.c,v 1.15 1998/12/08 00:17:21 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 TAILQ_INSERT_TAIL(&sc_link->pending_xfers, xs, device_q);
130 bzero(&xs->cmdstore, sizeof(xs->cmdstore));
131 }
132 return (xs);
133 }
134
135 /*
136 * Given a scsipi_xfer struct, and a device (referenced through sc_link)
137 * return the struct to the free pool and credit the device with it
138 * If another process is waiting for an xs, do a wakeup, let it proceed
139 *
140 * MUST BE CALLED AT splbio()!!
141 */
142 void
143 scsipi_free_xs(xs, flags)
144 struct scsipi_xfer *xs;
145 int flags;
146 {
147 struct scsipi_link *sc_link = xs->sc_link;
148
149 TAILQ_REMOVE(&sc_link->pending_xfers, xs, device_q);
150 if (TAILQ_FIRST(&sc_link->pending_xfers) == NULL &&
151 (sc_link->flags & SDEV_WAITDRAIN) != 0)
152 wakeup(&sc_link->pending_xfers);
153 xs->flags &= ~INUSE;
154 pool_put(&scsipi_xfer_pool, xs);
155
156 SC_DEBUG(sc_link, SDEV_DB3, ("scsipi_free_xs\n"));
157 /* if was 0 and someone waits, wake them up */
158 sc_link->openings++;
159 if ((sc_link->flags & SDEV_WAITING) != 0) {
160 sc_link->flags &= ~SDEV_WAITING;
161 wakeup(sc_link);
162 } else {
163 if (sc_link->device->start) {
164 SC_DEBUG(sc_link, SDEV_DB2,
165 ("calling private start()\n"));
166 (*(sc_link->device->start))(sc_link->device_softc);
167 }
168 }
169 }
170
171 /*
172 * Wait for a scsipi_link's pending xfers to drain.
173 */
174 void
175 scsipi_wait_drain(sc_link)
176 struct scsipi_link *sc_link;
177 {
178 int s;
179
180 s = splbio();
181 if (TAILQ_FIRST(&sc_link->pending_xfers) != NULL) {
182 sc_link->flags |= SDEV_WAITDRAIN;
183 (void) tsleep(&sc_link->pending_xfers, PRIBIO, "sxdrn", 0);
184 sc_link->flags &= ~SDEV_WAITDRAIN;
185 }
186 #ifdef DIAGNOSTIC
187 if (TAILQ_FIRST(&sc_link->pending_xfers) != NULL) {
188 (*sc_link->sc_print_addr)(sc_link);
189 printf("still pending xfers after wait\n");
190 panic("scsipi_wait_drain");
191 }
192 #endif
193 splx(s);
194 }
195
196 /*
197 * Look at the returned sense and act on the error, determining
198 * the unix error number to pass back. (0 = report no error)
199 *
200 * THIS IS THE DEFAULT ERROR HANDLER FOR SCSI DEVICES
201 */
202 int
203 scsipi_interpret_sense(xs)
204 struct scsipi_xfer *xs;
205 {
206 struct scsipi_sense_data *sense;
207 struct scsipi_link *sc_link = xs->sc_link;
208 u_int8_t key;
209 u_int32_t info;
210 int error;
211 #ifndef SCSIVERBOSE
212 static char *error_mes[] = {
213 "soft error (corrected)",
214 "not ready", "medium error",
215 "non-media hardware failure", "illegal request",
216 "unit attention", "readonly device",
217 "no data found", "vendor unique",
218 "copy aborted", "command aborted",
219 "search returned equal", "volume overflow",
220 "verify miscompare", "unknown error key"
221 };
222 #endif
223
224 sense = &xs->sense.scsi_sense;
225 #ifdef SCSIDEBUG
226 if ((sc_link->flags & SDEV_DB1) != 0) {
227 int count;
228 printf("code 0x%x valid 0x%x ",
229 sense->error_code & SSD_ERRCODE,
230 sense->error_code & SSD_ERRCODE_VALID ? 1 : 0);
231 printf("seg 0x%x key 0x%x ili 0x%x eom 0x%x fmark 0x%x\n",
232 sense->segment,
233 sense->flags & SSD_KEY,
234 sense->flags & SSD_ILI ? 1 : 0,
235 sense->flags & SSD_EOM ? 1 : 0,
236 sense->flags & SSD_FILEMARK ? 1 : 0);
237 printf("info: 0x%x 0x%x 0x%x 0x%x followed by %d extra bytes\n",
238 sense->info[0],
239 sense->info[1],
240 sense->info[2],
241 sense->info[3],
242 sense->extra_len);
243 printf("extra: ");
244 for (count = 0; count < ADD_BYTES_LIM(sense); count++)
245 printf("0x%x ", sense->cmd_spec_info[count]);
246 printf("\n");
247 }
248 #endif /* SCSIDEBUG */
249 /*
250 * If the device has it's own error handler, call it first.
251 * If it returns a legit error value, return that, otherwise
252 * it wants us to continue with normal error processing.
253 */
254 if (sc_link->device->err_handler) {
255 SC_DEBUG(sc_link, SDEV_DB2,
256 ("calling private err_handler()\n"));
257 error = (*sc_link->device->err_handler)(xs);
258 if (error != SCSIRET_CONTINUE)
259 return (error); /* error >= 0 better ? */
260 }
261 /* otherwise use the default */
262 switch (sense->error_code & SSD_ERRCODE) {
263 /*
264 * If it's code 70, use the extended stuff and
265 * interpret the key
266 */
267 case 0x71: /* delayed error */
268 sc_link->sc_print_addr(sc_link);
269 key = sense->flags & SSD_KEY;
270 printf(" DEFERRED ERROR, key = 0x%x\n", key);
271 /* FALLTHROUGH */
272 case 0x70:
273 if ((sense->error_code & SSD_ERRCODE_VALID) != 0)
274 info = _4btol(sense->info);
275 else
276 info = 0;
277 key = sense->flags & SSD_KEY;
278
279 switch (key) {
280 case SKEY_NO_SENSE:
281 case SKEY_RECOVERED_ERROR:
282 if (xs->resid == xs->datalen && xs->datalen) {
283 /*
284 * Why is this here?
285 */
286 xs->resid = 0; /* not short read */
287 }
288 case SKEY_EQUAL:
289 error = 0;
290 break;
291 case SKEY_NOT_READY:
292 if ((sc_link->flags & SDEV_REMOVABLE) != 0)
293 sc_link->flags &= ~SDEV_MEDIA_LOADED;
294 if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
295 return (0);
296 if ((xs->flags & SCSI_SILENT) != 0)
297 return (EIO);
298 error = EIO;
299 break;
300 case SKEY_ILLEGAL_REQUEST:
301 if ((xs->flags & SCSI_IGNORE_ILLEGAL_REQUEST) != 0)
302 return (0);
303 if ((xs->flags & SCSI_SILENT) != 0)
304 return (EIO);
305 error = EINVAL;
306 break;
307 case SKEY_UNIT_ATTENTION:
308 if ((sc_link->flags & SDEV_REMOVABLE) != 0)
309 sc_link->flags &= ~SDEV_MEDIA_LOADED;
310 if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 ||
311 /* XXX Should reupload any transient state. */
312 (sc_link->flags & SDEV_REMOVABLE) == 0)
313 return (ERESTART);
314 if ((xs->flags & SCSI_SILENT) != 0)
315 return (EIO);
316 error = EIO;
317 break;
318 case SKEY_WRITE_PROTECT:
319 error = EROFS;
320 break;
321 case SKEY_BLANK_CHECK:
322 error = 0;
323 break;
324 case SKEY_ABORTED_COMMAND:
325 error = ERESTART;
326 break;
327 case SKEY_VOLUME_OVERFLOW:
328 error = ENOSPC;
329 break;
330 default:
331 error = EIO;
332 break;
333 }
334
335 #ifdef SCSIVERBOSE
336 if ((xs->flags & SCSI_SILENT) == 0)
337 scsipi_print_sense(xs, 0);
338 #else
339 if (key) {
340 sc_link->sc_print_addr(sc_link);
341 printf("%s", error_mes[key - 1]);
342 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
343 switch (key) {
344 case SKEY_NOT_READY:
345 case SKEY_ILLEGAL_REQUEST:
346 case SKEY_UNIT_ATTENTION:
347 case SKEY_WRITE_PROTECT:
348 break;
349 case SKEY_BLANK_CHECK:
350 printf(", requested size: %d (decimal)",
351 info);
352 break;
353 case SKEY_ABORTED_COMMAND:
354 if (xs->retries)
355 printf(", retrying");
356 printf(", cmd 0x%x, info 0x%x",
357 xs->cmd->opcode, info);
358 break;
359 default:
360 printf(", info = %d (decimal)", info);
361 }
362 }
363 if (sense->extra_len != 0) {
364 int n;
365 printf(", data =");
366 for (n = 0; n < sense->extra_len; n++)
367 printf(" %02x",
368 sense->cmd_spec_info[n]);
369 }
370 printf("\n");
371 }
372 #endif
373 return (error);
374
375 /*
376 * Not code 70, just report it
377 */
378 default:
379 sc_link->sc_print_addr(sc_link);
380 printf("error code %d", sense->error_code & SSD_ERRCODE);
381 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
382 struct scsipi_sense_data_unextended *usense =
383 (struct scsipi_sense_data_unextended *)sense;
384 printf(" at block no. %d (decimal)",
385 _3btol(usense->block));
386 }
387 printf("\n");
388 return (EIO);
389 }
390 }
391
392 /*
393 * Find out from the device what its capacity is.
394 */
395 u_long
396 scsipi_size(sc_link, flags)
397 struct scsipi_link *sc_link;
398 int flags;
399 {
400 struct scsipi_read_cap_data rdcap;
401 struct scsipi_read_capacity scsipi_cmd;
402
403 /*
404 * make up a scsipi command and ask the scsipi driver to do
405 * it for you.
406 */
407 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
408 scsipi_cmd.opcode = READ_CAPACITY;
409
410 /*
411 * If the command works, interpret the result as a 4 byte
412 * number of blocks
413 */
414 if (scsipi_command(sc_link, (struct scsipi_generic *)&scsipi_cmd,
415 sizeof(scsipi_cmd), (u_char *)&rdcap, sizeof(rdcap),
416 2, 20000, NULL, flags | SCSI_DATA_IN) != 0) {
417 sc_link->sc_print_addr(sc_link);
418 printf("could not get size\n");
419 return (0);
420 }
421
422 return (_4btol(rdcap.addr) + 1);
423 }
424
425 /*
426 * Get scsipi driver to send a "are you ready?" command
427 */
428 int
429 scsipi_test_unit_ready(sc_link, flags)
430 struct scsipi_link *sc_link;
431 int flags;
432 {
433 struct scsipi_test_unit_ready scsipi_cmd;
434
435 /* some ATAPI drives don't support TEST_UNIT_READY. Sigh */
436 if (sc_link->quirks & ADEV_NOTUR)
437 return (0);
438
439 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
440 scsipi_cmd.opcode = TEST_UNIT_READY;
441
442 return (scsipi_command(sc_link,
443 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
444 0, 0, 2, 10000, NULL, flags));
445 }
446
447 /*
448 * Do a scsipi operation asking a device what it is
449 * Use the scsipi_cmd routine in the switch table.
450 * XXX actually this is only used for scsi devices, because I have the feeling
451 * that some atapi CDROM may not implement it, althouh it marked as mandatory
452 * in the atapi specs.
453 */
454 int
455 scsipi_inquire(sc_link, inqbuf, flags)
456 struct scsipi_link *sc_link;
457 struct scsipi_inquiry_data *inqbuf;
458 int flags;
459 {
460 struct scsipi_inquiry scsipi_cmd;
461
462 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
463 scsipi_cmd.opcode = INQUIRY;
464 scsipi_cmd.length = sizeof(struct scsipi_inquiry_data);
465
466 return (scsipi_command(sc_link,
467 (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
468 (u_char *) inqbuf, sizeof(struct scsipi_inquiry_data),
469 2, 10000, NULL, SCSI_DATA_IN | flags));
470 }
471
472 /*
473 * Prevent or allow the user to remove the media
474 */
475 int
476 scsipi_prevent(sc_link, type, flags)
477 struct scsipi_link *sc_link;
478 int type, flags;
479 {
480 struct scsipi_prevent scsipi_cmd;
481
482 if (sc_link->quirks & ADEV_NODOORLOCK)
483 return (0);
484
485 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
486 scsipi_cmd.opcode = PREVENT_ALLOW;
487 scsipi_cmd.how = type;
488 return (scsipi_command(sc_link,
489 (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
490 0, 0, 2, 5000, NULL, flags));
491 }
492
493 /*
494 * Get scsipi driver to send a "start up" command
495 */
496 int
497 scsipi_start(sc_link, type, flags)
498 struct scsipi_link *sc_link;
499 int type, flags;
500 {
501 struct scsipi_start_stop scsipi_cmd;
502
503 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
504 scsipi_cmd.opcode = START_STOP;
505 scsipi_cmd.byte2 = 0x00;
506 scsipi_cmd.how = type;
507 return (scsipi_command(sc_link,
508 (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
509 0, 0, 2, type == SSS_START ? 30000 : 10000, NULL, flags));
510 }
511
512 /*
513 * This routine is called by the scsipi interrupt when the transfer is
514 * complete.
515 */
516 void
517 scsipi_done(xs)
518 struct scsipi_xfer *xs;
519 {
520 struct scsipi_link *sc_link = xs->sc_link;
521 struct buf *bp;
522 int error;
523
524 SC_DEBUG(sc_link, SDEV_DB2, ("scsipi_done\n"));
525 #ifdef SCSIDEBUG
526 if ((sc_link->flags & SDEV_DB1) != 0)
527 show_scsipi_cmd(xs);
528 #endif /* SCSIDEBUG */
529
530 /*
531 * If it's a user level request, bypass all usual completion
532 * processing, let the user work it out.. We take
533 * reponsibility for freeing the xs when the user returns.
534 * (and restarting the device's queue).
535 */
536 if ((xs->flags & SCSI_USER) != 0) {
537 SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
538 scsipi_user_done(xs); /* to take a copy of the sense etc. */
539 SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
540
541 /*
542 * If this was an asynchronous operation (i.e. adapter
543 * returned SUCCESSFULLY_QUEUED when the command was
544 * submitted), we need to free the scsipi_xfer here.
545 */
546 if (SCSIPI_XFER_ASYNC(xs))
547 scsipi_free_xs(xs, SCSI_NOSLEEP);
548 SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
549 return;
550 }
551
552 if (!SCSIPI_XFER_ASYNC(xs)) {
553 /*
554 * if it's a normal upper level request, then ask
555 * the upper level code to handle error checking
556 * rather than doing it here at interrupt time
557 */
558 wakeup(xs);
559 return;
560 }
561
562 /*
563 * Go and handle errors now.
564 * If it returns ERESTART then we should RETRY
565 */
566 retry:
567 error = sc_err1(xs, 1);
568 if (error == ERESTART)
569 switch (scsipi_command_direct(xs)) {
570 case SUCCESSFULLY_QUEUED:
571 return;
572
573 case TRY_AGAIN_LATER:
574 xs->error = XS_BUSY;
575 case COMPLETE:
576 goto retry;
577 }
578
579 bp = xs->bp;
580 if (bp) {
581 if (error) {
582 bp->b_error = error;
583 bp->b_flags |= B_ERROR;
584 bp->b_resid = bp->b_bcount;
585 } else {
586 bp->b_error = 0;
587 bp->b_resid = xs->resid;
588 }
589 }
590 if (sc_link->device->done) {
591 /*
592 * Tell the device the operation is actually complete.
593 * No more will happen with this xfer. This for
594 * notification of the upper-level driver only; they
595 * won't be returning any meaningful information to us.
596 */
597 (*sc_link->device->done)(xs);
598 }
599 /*
600 * If this was an asynchronous operation (i.e. adapter
601 * returned SUCCESSFULLY_QUEUED when the command was
602 * submitted), we need to free the scsipi_xfer here.
603 */
604 if (SCSIPI_XFER_ASYNC(xs))
605 scsipi_free_xs(xs, SCSI_NOSLEEP);
606 if (bp)
607 biodone(bp);
608 }
609
610 int
611 scsipi_execute_xs(xs)
612 struct scsipi_xfer *xs;
613 {
614 int async;
615 int error;
616 int s;
617
618 xs->flags &= ~ITSDONE;
619 xs->error = XS_NOERROR;
620 xs->resid = xs->datalen;
621 xs->status = 0;
622
623 retry:
624 /*
625 * Do the transfer. If we are polling we will return:
626 * COMPLETE, Was poll, and scsipi_done has been called
627 * TRY_AGAIN_LATER, Adapter short resources, try again
628 *
629 * if under full steam (interrupts) it will return:
630 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
631 * TRY_AGAIN_LATER, (as for polling)
632 * After the wakeup, we must still check if it succeeded
633 *
634 * If we have a SCSI_NOSLEEP (typically because we have a buf)
635 * we just return. All the error proccessing and the buffer
636 * code both expect us to return straight to them, so as soon
637 * as the command is queued, return.
638 */
639 #ifdef SCSIDEBUG
640 if (xs->sc_link->flags & SDEV_DB3) {
641 printf("scsipi_exec_cmd: ");
642 show_scsipi_xs(xs);
643 printf("\n");
644 }
645 #endif
646 async = SCSIPI_XFER_ASYNC(xs);
647 switch (scsipi_command_direct(xs)) {
648 case SUCCESSFULLY_QUEUED:
649 if (async) {
650 /* scsipi_done() will free the scsipi_xfer. */
651 return (EJUSTRETURN);
652 }
653 #ifdef DIAGNOSTIC
654 if (xs->flags & SCSI_NOSLEEP)
655 panic("scsipi_execute_xs: NOSLEEP and POLL");
656 #endif
657 s = splbio();
658 while ((xs->flags & ITSDONE) == 0)
659 tsleep(xs, PRIBIO + 1, "scsipi_cmd", 0);
660 splx(s);
661 case COMPLETE: /* Polling command completed ok */
662 if (xs->bp)
663 return (0);
664 doit:
665 SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
666 if ((error = sc_err1(xs, 0)) != ERESTART)
667 return (error);
668 goto retry;
669
670 case TRY_AGAIN_LATER: /* adapter resource shortage */
671 xs->error = XS_BUSY;
672 goto doit;
673
674 default:
675 panic("scsipi_execute_xs: invalid return code");
676 }
677
678 #ifdef DIAGNOSTIC
679 panic("scsipi_execute_xs: impossible");
680 #endif
681 return (EINVAL);
682 }
683
684 int
685 sc_err1(xs, async)
686 struct scsipi_xfer *xs;
687 int async;
688 {
689 int error;
690
691 SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
692
693 /*
694 * If it has a buf, we might be working with
695 * a request from the buffer cache or some other
696 * piece of code that requires us to process
697 * errors at inetrrupt time. We have probably
698 * been called by scsipi_done()
699 */
700 switch (xs->error) {
701 case XS_NOERROR: /* nearly always hit this one */
702 error = 0;
703 break;
704
705 case XS_SENSE:
706 case XS_SHORTSENSE:
707 if ((error = (*xs->sc_link->scsipi_interpret_sense)(xs)) ==
708 ERESTART)
709 goto retry;
710 SC_DEBUG(xs->sc_link, SDEV_DB3,
711 ("scsipi_interpret_sense returned %d\n", error));
712 break;
713
714 case XS_BUSY:
715 if (xs->retries) {
716 if ((xs->flags & SCSI_POLL) != 0)
717 delay(1000000);
718 else if ((xs->flags & SCSI_NOSLEEP) == 0)
719 tsleep(&lbolt, PRIBIO, "scbusy", 0);
720 else
721 #if 0
722 timeout(scsipi_requeue, xs, hz);
723 #else
724 goto lose;
725 #endif
726 }
727 case XS_TIMEOUT:
728 retry:
729 if (xs->retries) {
730 xs->retries--;
731 xs->error = XS_NOERROR;
732 xs->flags &= ~ITSDONE;
733 return (ERESTART);
734 }
735 case XS_DRIVER_STUFFUP:
736 lose:
737 error = EIO;
738 break;
739
740 case XS_SELTIMEOUT:
741 /* XXX Disable device? */
742 error = EIO;
743 break;
744
745 case XS_RESET:
746 if (xs->retries) {
747 SC_DEBUG(xs->sc_link, SDEV_DB3,
748 ("restarting command destroyed by reset\n"));
749 goto retry;
750 }
751 error = EIO;
752 break;
753
754 default:
755 (*xs->sc_link->sc_print_addr)(xs->sc_link);
756 printf("unknown error category from scsipi driver\n");
757 error = EIO;
758 break;
759 }
760
761 return (error);
762 }
763
764 /*
765 * Add a reference to the adapter pointed to by the provided
766 * link, enabling the adapter if necessary.
767 */
768 int
769 scsipi_adapter_addref(link)
770 struct scsipi_link *link;
771 {
772 struct scsipi_adapter *adapter = link->adapter;
773 int s, error = 0;
774
775 s = splbio();
776 if (adapter->scsipi_refcnt++ == 0 &&
777 adapter->scsipi_enable != NULL) {
778 error = (*adapter->scsipi_enable)(link->adapter_softc, 1);
779 if (error)
780 adapter->scsipi_refcnt--;
781 }
782 splx(s);
783 return (error);
784 }
785
786 /*
787 * Delete a reference to the adapter pointed to by the provided
788 * link, disabling the adapter if possible.
789 */
790 void
791 scsipi_adapter_delref(link)
792 struct scsipi_link *link;
793 {
794 struct scsipi_adapter *adapter = link->adapter;
795 int s;
796
797 s = splbio();
798 if (adapter->scsipi_refcnt-- == 1 &&
799 adapter->scsipi_enable != NULL)
800 (void) (*adapter->scsipi_enable)(link->adapter_softc, 0);
801 splx(s);
802 }
803
804 #ifdef SCSIDEBUG
805 /*
806 * Given a scsipi_xfer, dump the request, in all it's glory
807 */
808 void
809 show_scsipi_xs(xs)
810 struct scsipi_xfer *xs;
811 {
812
813 printf("xs(%p): ", xs);
814 printf("flg(0x%x)", xs->flags);
815 printf("sc_link(%p)", xs->sc_link);
816 printf("retr(0x%x)", xs->retries);
817 printf("timo(0x%x)", xs->timeout);
818 printf("cmd(%p)", xs->cmd);
819 printf("len(0x%x)", xs->cmdlen);
820 printf("data(%p)", xs->data);
821 printf("len(0x%x)", xs->datalen);
822 printf("res(0x%x)", xs->resid);
823 printf("err(0x%x)", xs->error);
824 printf("bp(%p)", xs->bp);
825 show_scsipi_cmd(xs);
826 }
827
828 void
829 show_scsipi_cmd(xs)
830 struct scsipi_xfer *xs;
831 {
832 u_char *b = (u_char *) xs->cmd;
833 int i = 0;
834
835 (*xs->sc_link->sc_print_addr)(xs->sc_link);
836 printf("command: ");
837
838 if ((xs->flags & SCSI_RESET) == 0) {
839 while (i < xs->cmdlen) {
840 if (i)
841 printf(",");
842 printf("0x%x", b[i++]);
843 }
844 printf("-[%d bytes]\n", xs->datalen);
845 if (xs->datalen)
846 show_mem(xs->data, min(64, xs->datalen));
847 } else
848 printf("-RESET-\n");
849 }
850
851 void
852 show_mem(address, num)
853 u_char *address;
854 int num;
855 {
856 int x;
857
858 printf("------------------------------");
859 for (x = 0; x < num; x++) {
860 if ((x % 16) == 0)
861 printf("\n%03d: ", x);
862 printf("%02x ", *address++);
863 }
864 printf("\n------------------------------\n");
865 }
866 #endif /*SCSIDEBUG */
867