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