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