scsipi_base.c revision 1.24 1 /* $NetBSD: scsipi_base.c,v 1.24 1999/09/30 22:57:54 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 xs->xs_control = flags;
135 TAILQ_INSERT_TAIL(&sc_link->pending_xfers, xs, device_q);
136 bzero(&xs->cmdstore, sizeof(xs->cmdstore));
137 }
138 return (xs);
139 }
140
141 /*
142 * Given a scsipi_xfer struct, and a device (referenced through sc_link)
143 * return the struct to the free pool and credit the device with it
144 * If another process is waiting for an xs, do a wakeup, let it proceed
145 *
146 * MUST BE CALLED AT splbio()!!
147 */
148 void
149 scsipi_free_xs(xs, flags)
150 struct scsipi_xfer *xs;
151 int flags;
152 {
153 struct scsipi_link *sc_link = xs->sc_link;
154
155 TAILQ_REMOVE(&sc_link->pending_xfers, xs, device_q);
156 if (TAILQ_FIRST(&sc_link->pending_xfers) == NULL &&
157 (sc_link->flags & SDEV_WAITDRAIN) != 0) {
158 sc_link->flags &= ~SDEV_WAITDRAIN;
159 wakeup(&sc_link->pending_xfers);
160 }
161 pool_put(&scsipi_xfer_pool, xs);
162
163 SC_DEBUG(sc_link, SDEV_DB3, ("scsipi_free_xs\n"));
164 /* if was 0 and someone waits, wake them up */
165 sc_link->active--;
166 if ((sc_link->flags & SDEV_WAITING) != 0) {
167 sc_link->flags &= ~SDEV_WAITING;
168 wakeup(sc_link);
169 } else {
170 if (sc_link->device->start) {
171 SC_DEBUG(sc_link, SDEV_DB2,
172 ("calling private start()\n"));
173 (*(sc_link->device->start))(sc_link->device_softc);
174 }
175 }
176 }
177
178 /*
179 * Wait for a scsipi_link's pending xfers to drain.
180 */
181 void
182 scsipi_wait_drain(sc_link)
183 struct scsipi_link *sc_link;
184 {
185 int s;
186
187 s = splbio();
188 while (TAILQ_FIRST(&sc_link->pending_xfers) != NULL) {
189 sc_link->flags |= SDEV_WAITDRAIN;
190 (void) tsleep(&sc_link->pending_xfers, PRIBIO, "sxdrn", 0);
191 }
192 splx(s);
193 }
194
195 /*
196 * Kill off all pending xfers for a scsipi_link.
197 *
198 * Must be called at splbio().
199 */
200 void
201 scsipi_kill_pending(sc_link)
202 struct scsipi_link *sc_link;
203 {
204 struct scsipi_xfer *xs;
205
206 while ((xs = TAILQ_FIRST(&sc_link->pending_xfers)) != NULL) {
207 xs->xs_status |= XS_STS_DONE;
208 xs->error = ENODEV;
209 scsipi_done(xs);
210 }
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 sc_link->sc_print_addr(sc_link);
414 printf("Sense Error Code 0x%x",
415 sense->error_code & SSD_ERRCODE);
416 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
417 struct scsipi_sense_data_unextended *usense =
418 (struct scsipi_sense_data_unextended *)sense;
419 printf(" at block no. %d (decimal)",
420 _3btol(usense->block));
421 }
422 printf("\n");
423 return (EIO);
424 }
425 }
426
427 /*
428 * Find out from the device what its capacity is.
429 */
430 u_long
431 scsipi_size(sc_link, flags)
432 struct scsipi_link *sc_link;
433 int flags;
434 {
435 struct scsipi_read_cap_data rdcap;
436 struct scsipi_read_capacity scsipi_cmd;
437
438 /*
439 * make up a scsipi command and ask the scsipi driver to do
440 * it for you.
441 */
442 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
443 scsipi_cmd.opcode = READ_CAPACITY;
444
445 /*
446 * If the command works, interpret the result as a 4 byte
447 * number of blocks
448 */
449 if (scsipi_command(sc_link, (struct scsipi_generic *)&scsipi_cmd,
450 sizeof(scsipi_cmd), (u_char *)&rdcap, sizeof(rdcap),
451 2, 20000, NULL, flags | XS_CTL_DATA_IN) != 0) {
452 sc_link->sc_print_addr(sc_link);
453 printf("could not get size\n");
454 return (0);
455 }
456
457 return (_4btol(rdcap.addr) + 1);
458 }
459
460 /*
461 * Get scsipi driver to send a "are you ready?" command
462 */
463 int
464 scsipi_test_unit_ready(sc_link, flags)
465 struct scsipi_link *sc_link;
466 int flags;
467 {
468 struct scsipi_test_unit_ready scsipi_cmd;
469
470 /* some ATAPI drives don't support TEST_UNIT_READY. Sigh */
471 if (sc_link->quirks & ADEV_NOTUR)
472 return (0);
473
474 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
475 scsipi_cmd.opcode = TEST_UNIT_READY;
476
477 return (scsipi_command(sc_link,
478 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
479 0, 0, 2, 10000, NULL, flags));
480 }
481
482 /*
483 * Do a scsipi operation asking a device what it is
484 * Use the scsipi_cmd routine in the switch table.
485 * XXX actually this is only used for scsi devices, because I have the feeling
486 * that some atapi CDROM may not implement it, althouh it marked as mandatory
487 * in the atapi specs.
488 */
489 int
490 scsipi_inquire(sc_link, inqbuf, flags)
491 struct scsipi_link *sc_link;
492 struct scsipi_inquiry_data *inqbuf;
493 int flags;
494 {
495 struct scsipi_inquiry scsipi_cmd;
496
497 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
498 scsipi_cmd.opcode = INQUIRY;
499 scsipi_cmd.length = sizeof(struct scsipi_inquiry_data);
500
501 return (scsipi_command(sc_link,
502 (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
503 (u_char *) inqbuf, sizeof(struct scsipi_inquiry_data),
504 2, 10000, NULL, XS_CTL_DATA_IN | flags));
505 }
506
507 /*
508 * Prevent or allow the user to remove the media
509 */
510 int
511 scsipi_prevent(sc_link, type, flags)
512 struct scsipi_link *sc_link;
513 int type, flags;
514 {
515 struct scsipi_prevent scsipi_cmd;
516
517 if (sc_link->quirks & ADEV_NODOORLOCK)
518 return (0);
519
520 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
521 scsipi_cmd.opcode = PREVENT_ALLOW;
522 scsipi_cmd.how = type;
523 return (scsipi_command(sc_link,
524 (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
525 0, 0, 2, 5000, NULL, flags));
526 }
527
528 /*
529 * Get scsipi driver to send a "start up" command
530 */
531 int
532 scsipi_start(sc_link, type, flags)
533 struct scsipi_link *sc_link;
534 int type, flags;
535 {
536 struct scsipi_start_stop scsipi_cmd;
537
538 if (sc_link->quirks & SDEV_NOSTARTUNIT)
539 return 0;
540
541 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
542 scsipi_cmd.opcode = START_STOP;
543 scsipi_cmd.byte2 = 0x00;
544 scsipi_cmd.how = type;
545 return (scsipi_command(sc_link,
546 (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
547 0, 0, 2, (type & SSS_START) ? 30000 : 10000, NULL, flags));
548 }
549
550 /*
551 * This routine is called by the scsipi interrupt when the transfer is
552 * complete.
553 */
554 void
555 scsipi_done(xs)
556 struct scsipi_xfer *xs;
557 {
558 struct scsipi_link *sc_link = xs->sc_link;
559 struct buf *bp;
560 int error;
561
562 SC_DEBUG(sc_link, SDEV_DB2, ("scsipi_done\n"));
563 #ifdef SCSIDEBUG
564 if ((sc_link->flags & SDEV_DB1) != 0)
565 show_scsipi_cmd(xs);
566 #endif /* SCSIDEBUG */
567
568 /*
569 * If it's a user level request, bypass all usual completion
570 * processing, let the user work it out.. We take
571 * reponsibility for freeing the xs when the user returns.
572 * (and restarting the device's queue).
573 */
574 if ((xs->xs_control & XS_CTL_USERCMD) != 0) {
575 SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
576 scsipi_user_done(xs); /* to take a copy of the sense etc. */
577 SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
578
579 /*
580 * If this was an asynchronous operation (i.e. adapter
581 * returned SUCCESSFULLY_QUEUED when the command was
582 * submitted), we need to free the scsipi_xfer here.
583 */
584 if (xs->xs_control & XS_CTL_ASYNC)
585 scsipi_free_xs(xs, XS_CTL_NOSLEEP);
586 SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
587 return;
588 }
589
590 if ((xs->xs_control & XS_CTL_ASYNC) == 0) {
591 /*
592 * if it's a normal upper level request, then ask
593 * the upper level code to handle error checking
594 * rather than doing it here at interrupt time
595 */
596 wakeup(xs);
597 return;
598 }
599
600 /*
601 * Go and handle errors now.
602 * If it returns ERESTART then we should RETRY
603 */
604 retry:
605 error = sc_err1(xs, 1);
606 if (error == ERESTART) {
607 switch (scsipi_command_direct(xs)) {
608 case SUCCESSFULLY_QUEUED:
609 return;
610
611 case TRY_AGAIN_LATER:
612 xs->error = XS_BUSY;
613 case COMPLETE:
614 goto retry;
615 }
616 }
617
618 bp = xs->bp;
619 if (bp) {
620 if (error) {
621 bp->b_error = error;
622 bp->b_flags |= B_ERROR;
623 bp->b_resid = bp->b_bcount;
624 } else {
625 bp->b_error = 0;
626 bp->b_resid = xs->resid;
627 }
628 }
629 if (sc_link->device->done) {
630 /*
631 * Tell the device the operation is actually complete.
632 * No more will happen with this xfer. This for
633 * notification of the upper-level driver only; they
634 * won't be returning any meaningful information to us.
635 */
636 (*sc_link->device->done)(xs);
637 }
638 /*
639 * If this was an asynchronous operation (i.e. adapter
640 * returned SUCCESSFULLY_QUEUED when the command was
641 * submitted), we need to free the scsipi_xfer here.
642 */
643 if (xs->xs_control & XS_CTL_ASYNC) {
644 int s = splbio();
645 scsipi_free_xs(xs, XS_CTL_NOSLEEP);
646 splx(s);
647 }
648 if (bp)
649 biodone(bp);
650 }
651
652 int
653 scsipi_execute_xs(xs)
654 struct scsipi_xfer *xs;
655 {
656 int async;
657 int error;
658 int s;
659
660 xs->xs_status &= ~XS_STS_DONE;
661 xs->error = XS_NOERROR;
662 xs->resid = xs->datalen;
663 xs->status = 0;
664
665 retry:
666 /*
667 * Do the transfer. If we are polling we will return:
668 * COMPLETE, Was poll, and scsipi_done has been called
669 * TRY_AGAIN_LATER, Adapter short resources, try again
670 *
671 * if under full steam (interrupts) it will return:
672 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
673 * TRY_AGAIN_LATER, (as for polling)
674 * After the wakeup, we must still check if it succeeded
675 *
676 * If we have a XS_CTL_ASYNC (typically because we have a buf)
677 * we just return. All the error proccessing and the buffer
678 * code both expect us to return straight to them, so as soon
679 * as the command is queued, return.
680 */
681 #ifdef SCSIDEBUG
682 if (xs->sc_link->flags & SDEV_DB3) {
683 printf("scsipi_exec_cmd: ");
684 show_scsipi_xs(xs);
685 printf("\n");
686 }
687 #endif
688 async = (xs->xs_control & XS_CTL_ASYNC);
689 switch (scsipi_command_direct(xs)) {
690 case SUCCESSFULLY_QUEUED:
691 if (async) {
692 /* scsipi_done() will free the scsipi_xfer. */
693 return (EJUSTRETURN);
694 }
695 #ifdef DIAGNOSTIC
696 if (xs->xs_control & XS_CTL_ASYNC)
697 panic("scsipi_execute_xs: ASYNC and POLL");
698 #endif
699 s = splbio();
700 while ((xs->xs_status & XS_STS_DONE) == 0)
701 tsleep(xs, PRIBIO + 1, "scsipi_cmd", 0);
702 splx(s);
703 case COMPLETE: /* Polling command completed ok */
704 if (xs->bp)
705 return (0);
706 doit:
707 SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
708 if ((error = sc_err1(xs, 0)) != ERESTART)
709 return (error);
710 goto retry;
711
712 case TRY_AGAIN_LATER: /* adapter resource shortage */
713 xs->error = XS_BUSY;
714 goto doit;
715
716 default:
717 panic("scsipi_execute_xs: invalid return code");
718 }
719
720 #ifdef DIAGNOSTIC
721 panic("scsipi_execute_xs: impossible");
722 #endif
723 return (EINVAL);
724 }
725
726 int
727 sc_err1(xs, async)
728 struct scsipi_xfer *xs;
729 int async;
730 {
731 int error;
732
733 SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
734
735 /*
736 * If it has a buf, we might be working with
737 * a request from the buffer cache or some other
738 * piece of code that requires us to process
739 * errors at inetrrupt time. We have probably
740 * been called by scsipi_done()
741 */
742 switch (xs->error) {
743 case XS_NOERROR: /* nearly always hit this one */
744 error = 0;
745 break;
746
747 case XS_SENSE:
748 case XS_SHORTSENSE:
749 if ((error = (*xs->sc_link->scsipi_interpret_sense)(xs)) ==
750 ERESTART)
751 goto retry;
752 SC_DEBUG(xs->sc_link, SDEV_DB3,
753 ("scsipi_interpret_sense returned %d\n", error));
754 break;
755
756 case XS_BUSY:
757 if (xs->retries) {
758 if ((xs->xs_control & XS_CTL_POLL) != 0)
759 delay(1000000);
760 else if ((xs->xs_control & XS_CTL_NOSLEEP) == 0)
761 tsleep(&lbolt, PRIBIO, "scbusy", 0);
762 else
763 #if 0
764 timeout(scsipi_requeue, xs, hz);
765 #else
766 goto lose;
767 #endif
768 }
769 case XS_TIMEOUT:
770 retry:
771 if (xs->retries) {
772 xs->retries--;
773 xs->error = XS_NOERROR;
774 xs->xs_status &= ~XS_STS_DONE;
775 return (ERESTART);
776 }
777 case XS_DRIVER_STUFFUP:
778 lose:
779 error = EIO;
780 break;
781
782 case XS_SELTIMEOUT:
783 /* XXX Disable device? */
784 error = EIO;
785 break;
786
787 case XS_RESET:
788 if (xs->retries) {
789 SC_DEBUG(xs->sc_link, SDEV_DB3,
790 ("restarting command destroyed by reset\n"));
791 goto retry;
792 }
793 error = EIO;
794 break;
795
796 default:
797 (*xs->sc_link->sc_print_addr)(xs->sc_link);
798 printf("unknown error category from scsipi driver\n");
799 error = EIO;
800 break;
801 }
802
803 return (error);
804 }
805
806 /*
807 * Add a reference to the adapter pointed to by the provided
808 * link, enabling the adapter if necessary.
809 */
810 int
811 scsipi_adapter_addref(link)
812 struct scsipi_link *link;
813 {
814 struct scsipi_adapter *adapter = link->adapter;
815 int s, error = 0;
816
817 s = splbio();
818 if (adapter->scsipi_refcnt++ == 0 &&
819 adapter->scsipi_enable != NULL) {
820 error = (*adapter->scsipi_enable)(link->adapter_softc, 1);
821 if (error)
822 adapter->scsipi_refcnt--;
823 }
824 splx(s);
825 return (error);
826 }
827
828 /*
829 * Delete a reference to the adapter pointed to by the provided
830 * link, disabling the adapter if possible.
831 */
832 void
833 scsipi_adapter_delref(link)
834 struct scsipi_link *link;
835 {
836 struct scsipi_adapter *adapter = link->adapter;
837 int s;
838
839 s = splbio();
840 if (adapter->scsipi_refcnt-- == 1 &&
841 adapter->scsipi_enable != NULL)
842 (void) (*adapter->scsipi_enable)(link->adapter_softc, 0);
843 splx(s);
844 }
845
846 #ifdef SCSIDEBUG
847 /*
848 * Given a scsipi_xfer, dump the request, in all it's glory
849 */
850 void
851 show_scsipi_xs(xs)
852 struct scsipi_xfer *xs;
853 {
854
855 printf("xs(%p): ", xs);
856 printf("xs_control(0x%08x)", xs->xs_control);
857 printf("xs_status(0x%08x)", xs->xs_status);
858 printf("sc_link(%p)", xs->sc_link);
859 printf("retr(0x%x)", xs->retries);
860 printf("timo(0x%x)", xs->timeout);
861 printf("cmd(%p)", xs->cmd);
862 printf("len(0x%x)", xs->cmdlen);
863 printf("data(%p)", xs->data);
864 printf("len(0x%x)", xs->datalen);
865 printf("res(0x%x)", xs->resid);
866 printf("err(0x%x)", xs->error);
867 printf("bp(%p)", xs->bp);
868 show_scsipi_cmd(xs);
869 }
870
871 void
872 show_scsipi_cmd(xs)
873 struct scsipi_xfer *xs;
874 {
875 u_char *b = (u_char *) xs->cmd;
876 int i = 0;
877
878 (*xs->sc_link->sc_print_addr)(xs->sc_link);
879 printf("command: ");
880
881 if ((xs->xs_control & XS_CTL_RESET) == 0) {
882 while (i < xs->cmdlen) {
883 if (i)
884 printf(",");
885 printf("0x%x", b[i++]);
886 }
887 printf("-[%d bytes]\n", xs->datalen);
888 if (xs->datalen)
889 show_mem(xs->data, min(64, xs->datalen));
890 } else
891 printf("-RESET-\n");
892 }
893
894 void
895 show_mem(address, num)
896 u_char *address;
897 int num;
898 {
899 int x;
900
901 printf("------------------------------");
902 for (x = 0; x < num; x++) {
903 if ((x % 16) == 0)
904 printf("\n%03d: ", x);
905 printf("%02x ", *address++);
906 }
907 printf("\n------------------------------\n");
908 }
909 #endif /*SCSIDEBUG */
910