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