1 /* $NetBSD: scsipi_base.c,v 1.51 2001/07/18 20:19:24 bouyer Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999, 2000 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; by Jason R. Thorpe of the Numerical Aerospace 9 * Simulation Facility, NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 #include "opt_scsi.h" 41 42 #include <sys/types.h> 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/buf.h> 47 #include <sys/uio.h> 48 #include <sys/malloc.h> 49 #include <sys/pool.h> 50 #include <sys/errno.h> 51 #include <sys/device.h> 52 #include <sys/proc.h> 53 #include <sys/kthread.h> 54 55 #include <dev/scsipi/scsipi_all.h> 56 #include <dev/scsipi/scsipi_disk.h> 57 #include <dev/scsipi/scsipiconf.h> 58 #include <dev/scsipi/scsipi_base.h> 59 60 #include <dev/scsipi/scsi_all.h> 61 #include <dev/scsipi/scsi_message.h> 62 63 int scsipi_complete __P((struct scsipi_xfer *)); 64 void scsipi_request_sense __P((struct scsipi_xfer *)); 65 int scsipi_enqueue __P((struct scsipi_xfer *)); 66 void scsipi_run_queue __P((struct scsipi_channel *chan)); 67 68 void scsipi_completion_thread __P((void *)); 69 70 void scsipi_get_tag __P((struct scsipi_xfer *)); 71 void scsipi_put_tag __P((struct scsipi_xfer *)); 72 73 int scsipi_get_resource __P((struct scsipi_channel *)); 74 void scsipi_put_resource __P((struct scsipi_channel *)); 75 __inline int scsipi_grow_resources __P((struct scsipi_channel *)); 76 77 void scsipi_async_event_max_openings __P((struct scsipi_channel *, 78 struct scsipi_max_openings *)); 79 void scsipi_async_event_xfer_mode __P((struct scsipi_channel *, 80 struct scsipi_xfer_mode *)); 81 void scsipi_async_event_channel_reset __P((struct scsipi_channel *)); 82 83 struct pool scsipi_xfer_pool; 84 85 /* 86 * scsipi_init: 87 * 88 * Called when a scsibus or atapibus is attached to the system 89 * to initialize shared data structures. 90 */ 91 void 92 scsipi_init() 93 { 94 static int scsipi_init_done; 95 96 if (scsipi_init_done) 97 return; 98 scsipi_init_done = 1; 99 100 /* Initialize the scsipi_xfer pool. */ 101 pool_init(&scsipi_xfer_pool, sizeof(struct scsipi_xfer), 0, 102 0, 0, "scxspl", 0, NULL, NULL, M_DEVBUF); 103 } 104 105 /* 106 * scsipi_channel_init: 107 * 108 * Initialize a scsipi_channel when it is attached. 109 */ 110 int 111 scsipi_channel_init(chan) 112 struct scsipi_channel *chan; 113 { 114 size_t nbytes; 115 int i; 116 117 /* Initialize shared data. */ 118 scsipi_init(); 119 120 /* Initialize the queues. */ 121 TAILQ_INIT(&chan->chan_queue); 122 TAILQ_INIT(&chan->chan_complete); 123 124 nbytes = chan->chan_ntargets * sizeof(struct scsipi_periph **); 125 chan->chan_periphs = malloc(nbytes, M_DEVBUF, M_NOWAIT); 126 if (chan->chan_periphs == NULL) 127 return (ENOMEM); 128 129 130 nbytes = chan->chan_nluns * sizeof(struct scsipi_periph *); 131 for (i = 0; i < chan->chan_ntargets; i++) { 132 chan->chan_periphs[i] = malloc(nbytes, M_DEVBUF, M_NOWAIT); 133 if (chan->chan_periphs[i] == NULL) { 134 while (--i >= 0) { 135 free(chan->chan_periphs[i], M_DEVBUF); 136 } 137 return (ENOMEM); 138 } 139 memset(chan->chan_periphs[i], 0, nbytes); 140 } 141 142 /* 143 * Create the asynchronous completion thread. 144 */ 145 kthread_create(scsipi_create_completion_thread, chan); 146 return (0); 147 } 148 149 /* 150 * scsipi_channel_shutdown: 151 * 152 * Shutdown a scsipi_channel. 153 */ 154 void 155 scsipi_channel_shutdown(chan) 156 struct scsipi_channel *chan; 157 { 158 159 /* 160 * Shut down the completion thread. 161 */ 162 chan->chan_flags |= SCSIPI_CHAN_SHUTDOWN; 163 wakeup(&chan->chan_complete); 164 165 /* 166 * Now wait for the thread to exit. 167 */ 168 while (chan->chan_thread != NULL) 169 (void) tsleep(&chan->chan_thread, PRIBIO, "scshut", 0); 170 } 171 172 /* 173 * scsipi_insert_periph: 174 * 175 * Insert a periph into the channel. 176 */ 177 void 178 scsipi_insert_periph(chan, periph) 179 struct scsipi_channel *chan; 180 struct scsipi_periph *periph; 181 { 182 int s; 183 184 s = splbio(); 185 chan->chan_periphs[periph->periph_target][periph->periph_lun] = periph; 186 splx(s); 187 } 188 189 /* 190 * scsipi_remove_periph: 191 * 192 * Remove a periph from the channel. 193 */ 194 void 195 scsipi_remove_periph(chan, periph) 196 struct scsipi_channel *chan; 197 struct scsipi_periph *periph; 198 { 199 int s; 200 201 s = splbio(); 202 chan->chan_periphs[periph->periph_target][periph->periph_lun] = NULL; 203 splx(s); 204 } 205 206 /* 207 * scsipi_lookup_periph: 208 * 209 * Lookup a periph on the specified channel. 210 */ 211 struct scsipi_periph * 212 scsipi_lookup_periph(chan, target, lun) 213 struct scsipi_channel *chan; 214 int target, lun; 215 { 216 struct scsipi_periph *periph; 217 int s; 218 219 if (target >= chan->chan_ntargets || 220 lun >= chan->chan_nluns) 221 return (NULL); 222 223 s = splbio(); 224 periph = chan->chan_periphs[target][lun]; 225 splx(s); 226 227 return (periph); 228 } 229 230 /* 231 * scsipi_get_resource: 232 * 233 * Allocate a single xfer `resource' from the channel. 234 * 235 * NOTE: Must be called at splbio(). 236 */ 237 int 238 scsipi_get_resource(chan) 239 struct scsipi_channel *chan; 240 { 241 struct scsipi_adapter *adapt = chan->chan_adapter; 242 243 if (chan->chan_flags & SCSIPI_CHAN_OPENINGS) { 244 if (chan->chan_openings > 0) { 245 chan->chan_openings--; 246 return (1); 247 } 248 return (0); 249 } 250 251 if (adapt->adapt_openings > 0) { 252 adapt->adapt_openings--; 253 return (1); 254 } 255 return (0); 256 } 257 258 /* 259 * scsipi_grow_resources: 260 * 261 * Attempt to grow resources for a channel. If this succeeds, 262 * we allocate one for our caller. 263 * 264 * NOTE: Must be called at splbio(). 265 */ 266 __inline int 267 scsipi_grow_resources(chan) 268 struct scsipi_channel *chan; 269 { 270 271 if (chan->chan_flags & SCSIPI_CHAN_CANGROW) { 272 scsipi_adapter_request(chan, ADAPTER_REQ_GROW_RESOURCES, NULL); 273 return (scsipi_get_resource(chan)); 274 } 275 276 return (0); 277 } 278 279 /* 280 * scsipi_put_resource: 281 * 282 * Free a single xfer `resource' to the channel. 283 * 284 * NOTE: Must be called at splbio(). 285 */ 286 void 287 scsipi_put_resource(chan) 288 struct scsipi_channel *chan; 289 { 290 struct scsipi_adapter *adapt = chan->chan_adapter; 291 292 if (chan->chan_flags & SCSIPI_CHAN_OPENINGS) 293 chan->chan_openings++; 294 else 295 adapt->adapt_openings++; 296 } 297 298 /* 299 * scsipi_get_tag: 300 * 301 * Get a tag ID for the specified xfer. 302 * 303 * NOTE: Must be called at splbio(). 304 */ 305 void 306 scsipi_get_tag(xs) 307 struct scsipi_xfer *xs; 308 { 309 struct scsipi_periph *periph = xs->xs_periph; 310 int word, bit, tag; 311 312 for (word = 0; word < PERIPH_NTAGWORDS; word++) { 313 bit = ffs(periph->periph_freetags[word]); 314 if (bit != 0) 315 break; 316 } 317 #ifdef DIAGNOSTIC 318 if (word == PERIPH_NTAGWORDS) { 319 scsipi_printaddr(periph); 320 printf("no free tags\n"); 321 panic("scsipi_get_tag"); 322 } 323 #endif 324 325 bit -= 1; 326 periph->periph_freetags[word] &= ~(1 << bit); 327 tag = (word << 5) | bit; 328 329 /* XXX Should eventually disallow this completely. */ 330 if (tag >= periph->periph_openings) { 331 scsipi_printaddr(periph); 332 printf("WARNING: tag %d greater than available openings %d\n", 333 tag, periph->periph_openings); 334 } 335 336 xs->xs_tag_id = tag; 337 } 338 339 /* 340 * scsipi_put_tag: 341 * 342 * Put the tag ID for the specified xfer back into the pool. 343 * 344 * NOTE: Must be called at splbio(). 345 */ 346 void 347 scsipi_put_tag(xs) 348 struct scsipi_xfer *xs; 349 { 350 struct scsipi_periph *periph = xs->xs_periph; 351 int word, bit; 352 353 word = xs->xs_tag_id >> 5; 354 bit = xs->xs_tag_id & 0x1f; 355 356 periph->periph_freetags[word] |= (1 << bit); 357 } 358 359 /* 360 * scsipi_get_xs: 361 * 362 * Allocate an xfer descriptor and associate it with the 363 * specified peripherial. If the peripherial has no more 364 * available command openings, we either block waiting for 365 * one to become available, or fail. 366 */ 367 struct scsipi_xfer * 368 scsipi_get_xs(periph, flags) 369 struct scsipi_periph *periph; 370 int flags; 371 { 372 struct scsipi_xfer *xs; 373 int s; 374 375 SC_DEBUG(periph, SCSIPI_DB3, ("scsipi_get_xs\n")); 376 377 /* 378 * If we're cold, make sure we poll. 379 */ 380 if (cold) 381 flags |= XS_CTL_NOSLEEP | XS_CTL_POLL; 382 383 #ifdef DIAGNOSTIC 384 /* 385 * URGENT commands can never be ASYNC. 386 */ 387 if ((flags & (XS_CTL_URGENT|XS_CTL_ASYNC)) == 388 (XS_CTL_URGENT|XS_CTL_ASYNC)) { 389 scsipi_printaddr(periph); 390 printf("URGENT and ASYNC\n"); 391 panic("scsipi_get_xs"); 392 } 393 #endif 394 395 s = splbio(); 396 /* 397 * Wait for a command opening to become available. Rules: 398 * 399 * - All xfers must wait for an available opening. 400 * Exception: URGENT xfers can proceed when 401 * active == openings, because we use the opening 402 * of the command we're recovering for. 403 * - if the periph has sense pending, only URGENT & REQSENSE 404 * xfers may proceed. 405 * 406 * - If the periph is recovering, only URGENT xfers may 407 * proceed. 408 * 409 * - If the periph is currently executing a recovery 410 * command, URGENT commands must block, because only 411 * one recovery command can execute at a time. 412 */ 413 for (;;) { 414 if (flags & XS_CTL_URGENT) { 415 if (periph->periph_active > periph->periph_openings) 416 goto wait_for_opening; 417 if (periph->periph_flags & PERIPH_SENSE) { 418 if ((flags & XS_CTL_REQSENSE) == 0) 419 goto wait_for_opening; 420 } else { 421 if ((periph->periph_flags & 422 PERIPH_RECOVERY_ACTIVE) != 0) 423 goto wait_for_opening; 424 periph->periph_flags |= PERIPH_RECOVERY_ACTIVE; 425 } 426 break; 427 } 428 if (periph->periph_active >= periph->periph_openings || 429 (periph->periph_flags & PERIPH_RECOVERING) != 0) 430 goto wait_for_opening; 431 periph->periph_active++; 432 break; 433 434 wait_for_opening: 435 if (flags & XS_CTL_NOSLEEP) { 436 splx(s); 437 return (NULL); 438 } 439 SC_DEBUG(periph, SCSIPI_DB3, ("sleeping\n")); 440 periph->periph_flags |= PERIPH_WAITING; 441 (void) tsleep(periph, PRIBIO, "getxs", 0); 442 } 443 SC_DEBUG(periph, SCSIPI_DB3, ("calling pool_get\n")); 444 xs = pool_get(&scsipi_xfer_pool, 445 ((flags & XS_CTL_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK)); 446 if (xs == NULL) { 447 if (flags & XS_CTL_URGENT) { 448 if ((flags & XS_CTL_REQSENSE) == 0) 449 periph->periph_flags &= ~PERIPH_RECOVERY_ACTIVE; 450 } else 451 periph->periph_active--; 452 scsipi_printaddr(periph); 453 printf("unable to allocate %sscsipi_xfer\n", 454 (flags & XS_CTL_URGENT) ? "URGENT " : ""); 455 } 456 splx(s); 457 458 SC_DEBUG(periph, SCSIPI_DB3, ("returning\n")); 459 460 if (xs != NULL) { 461 callout_init(&xs->xs_callout); 462 memset(xs, 0, sizeof(*xs)); 463 xs->xs_periph = periph; 464 xs->xs_control = flags; 465 xs->xs_status = 0; 466 s = splbio(); 467 TAILQ_INSERT_TAIL(&periph->periph_xferq, xs, device_q); 468 splx(s); 469 } 470 return (xs); 471 } 472 473 /* 474 * scsipi_put_xs: 475 * 476 * Release an xfer descriptor, decreasing the outstanding command 477 * count for the peripherial. If there is a thread waiting for 478 * an opening, wake it up. If not, kick any queued I/O the 479 * peripherial may have. 480 * 481 * NOTE: Must be called at splbio(). 482 */ 483 void 484 scsipi_put_xs(xs) 485 struct scsipi_xfer *xs; 486 { 487 struct scsipi_periph *periph = xs->xs_periph; 488 int flags = xs->xs_control; 489 490 SC_DEBUG(periph, SCSIPI_DB3, ("scsipi_free_xs\n")); 491 492 TAILQ_REMOVE(&periph->periph_xferq, xs, device_q); 493 pool_put(&scsipi_xfer_pool, xs); 494 495 #ifdef DIAGNOSTIC 496 if ((periph->periph_flags & PERIPH_RECOVERY_ACTIVE) != 0 && 497 periph->periph_active == 0) { 498 scsipi_printaddr(periph); 499 printf("recovery without a command to recovery for\n"); 500 panic("scsipi_put_xs"); 501 } 502 #endif 503 504 if (flags & XS_CTL_URGENT) { 505 if ((flags & XS_CTL_REQSENSE) == 0) 506 periph->periph_flags &= ~PERIPH_RECOVERY_ACTIVE; 507 } else 508 periph->periph_active--; 509 if (periph->periph_active == 0 && 510 (periph->periph_flags & PERIPH_WAITDRAIN) != 0) { 511 periph->periph_flags &= ~PERIPH_WAITDRAIN; 512 wakeup(&periph->periph_active); 513 } 514 515 if (periph->periph_flags & PERIPH_WAITING) { 516 periph->periph_flags &= ~PERIPH_WAITING; 517 wakeup(periph); 518 } else { 519 if (periph->periph_switch->psw_start != NULL) { 520 SC_DEBUG(periph, SCSIPI_DB2, 521 ("calling private start()\n")); 522 (*periph->periph_switch->psw_start)(periph); 523 } 524 } 525 } 526 527 /* 528 * scsipi_channel_freeze: 529 * 530 * Freeze a channel's xfer queue. 531 */ 532 void 533 scsipi_channel_freeze(chan, count) 534 struct scsipi_channel *chan; 535 int count; 536 { 537 int s; 538 539 s = splbio(); 540 chan->chan_qfreeze += count; 541 splx(s); 542 } 543 544 /* 545 * scsipi_channel_thaw: 546 * 547 * Thaw a channel's xfer queue. 548 */ 549 void 550 scsipi_channel_thaw(chan, count) 551 struct scsipi_channel *chan; 552 int count; 553 { 554 int s; 555 556 s = splbio(); 557 chan->chan_qfreeze -= count; 558 /* 559 * Don't let the freeze count go negative. 560 * 561 * Presumably the adapter driver could keep track of this, 562 * but it might just be easier to do this here so as to allow 563 * multiple callers, including those outside the adapter driver. 564 */ 565 if (chan->chan_qfreeze < 0) { 566 chan->chan_qfreeze = 0; 567 } 568 splx(s); 569 /* 570 * Kick the channel's queue here. Note, we may be running in 571 * interrupt context (softclock or HBA's interrupt), so the adapter 572 * driver had better not sleep. 573 */ 574 if (chan->chan_qfreeze == 0) 575 scsipi_run_queue(chan); 576 } 577 578 /* 579 * scsipi_channel_timed_thaw: 580 * 581 * Thaw a channel after some time has expired. This will also 582 * run the channel's queue if the freeze count has reached 0. 583 */ 584 void 585 scsipi_channel_timed_thaw(arg) 586 void *arg; 587 { 588 struct scsipi_channel *chan = arg; 589 590 scsipi_channel_thaw(chan, 1); 591 } 592 593 /* 594 * scsipi_periph_freeze: 595 * 596 * Freeze a device's xfer queue. 597 */ 598 void 599 scsipi_periph_freeze(periph, count) 600 struct scsipi_periph *periph; 601 int count; 602 { 603 int s; 604 605 s = splbio(); 606 periph->periph_qfreeze += count; 607 splx(s); 608 } 609 610 /* 611 * scsipi_periph_thaw: 612 * 613 * Thaw a device's xfer queue. 614 */ 615 void 616 scsipi_periph_thaw(periph, count) 617 struct scsipi_periph *periph; 618 int count; 619 { 620 int s; 621 622 s = splbio(); 623 periph->periph_qfreeze -= count; 624 if (periph->periph_qfreeze == 0 && 625 (periph->periph_flags & PERIPH_WAITING) != 0) 626 wakeup(periph); 627 splx(s); 628 } 629 630 /* 631 * scsipi_periph_timed_thaw: 632 * 633 * Thaw a device after some time has expired. 634 */ 635 void 636 scsipi_periph_timed_thaw(arg) 637 void *arg; 638 { 639 struct scsipi_periph *periph = arg; 640 641 callout_stop(&periph->periph_callout); 642 scsipi_periph_thaw(periph, 1); 643 644 /* 645 * Kick the channel's queue here. Note, we're running in 646 * interrupt context (softclock), so the adapter driver 647 * had better not sleep. 648 */ 649 scsipi_run_queue(periph->periph_channel); 650 } 651 652 /* 653 * scsipi_wait_drain: 654 * 655 * Wait for a periph's pending xfers to drain. 656 */ 657 void 658 scsipi_wait_drain(periph) 659 struct scsipi_periph *periph; 660 { 661 int s; 662 663 s = splbio(); 664 while (periph->periph_active != 0) { 665 periph->periph_flags |= PERIPH_WAITDRAIN; 666 (void) tsleep(&periph->periph_active, PRIBIO, "sxdrn", 0); 667 } 668 splx(s); 669 } 670 671 /* 672 * scsipi_kill_pending: 673 * 674 * Kill off all pending xfers for a periph. 675 * 676 * NOTE: Must be called at splbio(). 677 */ 678 void 679 scsipi_kill_pending(periph) 680 struct scsipi_periph *periph; 681 { 682 683 (*periph->periph_channel->chan_bustype->bustype_kill_pending)(periph); 684 #ifdef DIAGNOSTIC 685 if (TAILQ_FIRST(&periph->periph_xferq) != NULL) 686 panic("scsipi_kill_pending"); 687 #endif 688 scsipi_wait_drain(periph); 689 } 690 691 /* 692 * scsipi_interpret_sense: 693 * 694 * Look at the returned sense and act on the error, determining 695 * the unix error number to pass back. (0 = report no error) 696 * 697 * NOTE: If we return ERESTART, we are expected to haved 698 * thawed the device! 699 * 700 * THIS IS THE DEFAULT ERROR HANDLER FOR SCSI DEVICES. 701 */ 702 int 703 scsipi_interpret_sense(xs) 704 struct scsipi_xfer *xs; 705 { 706 struct scsipi_sense_data *sense; 707 struct scsipi_periph *periph = xs->xs_periph; 708 u_int8_t key; 709 u_int32_t info; 710 int error; 711 #ifndef SCSIVERBOSE 712 static char *error_mes[] = { 713 "soft error (corrected)", 714 "not ready", "medium error", 715 "non-media hardware failure", "illegal request", 716 "unit attention", "readonly device", 717 "no data found", "vendor unique", 718 "copy aborted", "command aborted", 719 "search returned equal", "volume overflow", 720 "verify miscompare", "unknown error key" 721 }; 722 #endif 723 724 sense = &xs->sense.scsi_sense; 725 #ifdef SCSIPI_DEBUG 726 if (periph->periph_flags & SCSIPI_DB1) { 727 int count; 728 scsipi_printaddr(periph); 729 printf(" sense debug information:\n"); 730 printf("\tcode 0x%x valid 0x%x\n", 731 sense->error_code & SSD_ERRCODE, 732 sense->error_code & SSD_ERRCODE_VALID ? 1 : 0); 733 printf("\tseg 0x%x key 0x%x ili 0x%x eom 0x%x fmark 0x%x\n", 734 sense->segment, 735 sense->flags & SSD_KEY, 736 sense->flags & SSD_ILI ? 1 : 0, 737 sense->flags & SSD_EOM ? 1 : 0, 738 sense->flags & SSD_FILEMARK ? 1 : 0); 739 printf("\ninfo: 0x%x 0x%x 0x%x 0x%x followed by %d " 740 "extra bytes\n", 741 sense->info[0], 742 sense->info[1], 743 sense->info[2], 744 sense->info[3], 745 sense->extra_len); 746 printf("\textra: "); 747 for (count = 0; count < ADD_BYTES_LIM(sense); count++) 748 printf("0x%x ", sense->cmd_spec_info[count]); 749 printf("\n"); 750 } 751 #endif 752 753 /* 754 * If the periph has it's own error handler, call it first. 755 * If it returns a legit error value, return that, otherwise 756 * it wants us to continue with normal error processing. 757 */ 758 if (periph->periph_switch->psw_error != NULL) { 759 SC_DEBUG(periph, SCSIPI_DB2, 760 ("calling private err_handler()\n")); 761 error = (*periph->periph_switch->psw_error)(xs); 762 if (error != EJUSTRETURN) 763 return (error); 764 } 765 /* otherwise use the default */ 766 switch (sense->error_code & SSD_ERRCODE) { 767 /* 768 * If it's code 70, use the extended stuff and 769 * interpret the key 770 */ 771 case 0x71: /* delayed error */ 772 scsipi_printaddr(periph); 773 key = sense->flags & SSD_KEY; 774 printf(" DEFERRED ERROR, key = 0x%x\n", key); 775 /* FALLTHROUGH */ 776 case 0x70: 777 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) 778 info = _4btol(sense->info); 779 else 780 info = 0; 781 key = sense->flags & SSD_KEY; 782 783 switch (key) { 784 case SKEY_NO_SENSE: 785 case SKEY_RECOVERED_ERROR: 786 if (xs->resid == xs->datalen && xs->datalen) { 787 /* 788 * Why is this here? 789 */ 790 xs->resid = 0; /* not short read */ 791 } 792 case SKEY_EQUAL: 793 error = 0; 794 break; 795 case SKEY_NOT_READY: 796 if ((periph->periph_flags & PERIPH_REMOVABLE) != 0) 797 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 798 if ((xs->xs_control & XS_CTL_IGNORE_NOT_READY) != 0) 799 return (0); 800 if (sense->add_sense_code == 0x3A && 801 sense->add_sense_code_qual == 0x00) { 802 error = ENODEV; /* Medium not present */ 803 if (xs->xs_control & XS_CTL_SILENT_NODEV) 804 return (error); 805 } else 806 error = EIO; 807 if ((xs->xs_control & XS_CTL_SILENT) != 0) 808 return (error); 809 break; 810 case SKEY_ILLEGAL_REQUEST: 811 if ((xs->xs_control & 812 XS_CTL_IGNORE_ILLEGAL_REQUEST) != 0) 813 return (0); 814 /* 815 * Handle the case where a device reports 816 * Logical Unit Not Supported during discovery. 817 */ 818 if ((xs->xs_control & XS_CTL_DISCOVERY) != 0 && 819 sense->add_sense_code == 0x25 && 820 sense->add_sense_code_qual == 0x00) 821 return (EINVAL); 822 if ((xs->xs_control & XS_CTL_SILENT) != 0) 823 return (EIO); 824 error = EINVAL; 825 break; 826 case SKEY_UNIT_ATTENTION: 827 if (sense->add_sense_code == 0x29 && 828 sense->add_sense_code_qual == 0x00) { 829 /* device or bus reset */ 830 return (ERESTART); 831 } 832 if ((periph->periph_flags & PERIPH_REMOVABLE) != 0) 833 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 834 if ((xs->xs_control & 835 XS_CTL_IGNORE_MEDIA_CHANGE) != 0 || 836 /* XXX Should reupload any transient state. */ 837 (periph->periph_flags & 838 PERIPH_REMOVABLE) == 0) { 839 return (ERESTART); 840 } 841 if ((xs->xs_control & XS_CTL_SILENT) != 0) 842 return (EIO); 843 error = EIO; 844 break; 845 case SKEY_WRITE_PROTECT: 846 error = EROFS; 847 break; 848 case SKEY_BLANK_CHECK: 849 error = 0; 850 break; 851 case SKEY_ABORTED_COMMAND: 852 error = ERESTART; 853 break; 854 case SKEY_VOLUME_OVERFLOW: 855 error = ENOSPC; 856 break; 857 default: 858 error = EIO; 859 break; 860 } 861 862 #ifdef SCSIVERBOSE 863 if (key && (xs->xs_control & XS_CTL_SILENT) == 0) 864 scsipi_print_sense(xs, 0); 865 #else 866 if (key) { 867 scsipi_printaddr(periph); 868 printf("%s", error_mes[key - 1]); 869 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) { 870 switch (key) { 871 case SKEY_NOT_READY: 872 case SKEY_ILLEGAL_REQUEST: 873 case SKEY_UNIT_ATTENTION: 874 case SKEY_WRITE_PROTECT: 875 break; 876 case SKEY_BLANK_CHECK: 877 printf(", requested size: %d (decimal)", 878 info); 879 break; 880 case SKEY_ABORTED_COMMAND: 881 if (xs->xs_retries) 882 printf(", retrying"); 883 printf(", cmd 0x%x, info 0x%x", 884 xs->cmd->opcode, info); 885 break; 886 default: 887 printf(", info = %d (decimal)", info); 888 } 889 } 890 if (sense->extra_len != 0) { 891 int n; 892 printf(", data ="); 893 for (n = 0; n < sense->extra_len; n++) 894 printf(" %02x", 895 sense->cmd_spec_info[n]); 896 } 897 printf("\n"); 898 } 899 #endif 900 return (error); 901 902 /* 903 * Not code 70, just report it 904 */ 905 default: 906 #if defined(SCSIDEBUG) || defined(DEBUG) 907 { 908 static char *uc = "undecodable sense error"; 909 int i; 910 u_int8_t *cptr = (u_int8_t *) sense; 911 scsipi_printaddr(periph); 912 if (xs->cmd == &xs->cmdstore) { 913 printf("%s for opcode 0x%x, data=", 914 uc, xs->cmdstore.opcode); 915 } else { 916 printf("%s, data=", uc); 917 } 918 for (i = 0; i < sizeof (sense); i++) 919 printf(" 0x%02x", *(cptr++) & 0xff); 920 printf("\n"); 921 } 922 #else 923 924 scsipi_printaddr(periph); 925 printf("Sense Error Code 0x%x", 926 sense->error_code & SSD_ERRCODE); 927 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) { 928 struct scsipi_sense_data_unextended *usense = 929 (struct scsipi_sense_data_unextended *)sense; 930 printf(" at block no. %d (decimal)", 931 _3btol(usense->block)); 932 } 933 printf("\n"); 934 #endif 935 return (EIO); 936 } 937 } 938 939 /* 940 * scsipi_size: 941 * 942 * Find out from the device what its capacity is. 943 */ 944 u_long 945 scsipi_size(periph, flags) 946 struct scsipi_periph *periph; 947 int flags; 948 { 949 struct scsipi_read_cap_data rdcap; 950 struct scsipi_read_capacity scsipi_cmd; 951 952 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 953 scsipi_cmd.opcode = READ_CAPACITY; 954 955 /* 956 * If the command works, interpret the result as a 4 byte 957 * number of blocks 958 */ 959 if (scsipi_command(periph, (struct scsipi_generic *)&scsipi_cmd, 960 sizeof(scsipi_cmd), (u_char *)&rdcap, sizeof(rdcap), 961 SCSIPIRETRIES, 20000, NULL, 962 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK) != 0) { 963 scsipi_printaddr(periph); 964 printf("could not get size\n"); 965 return (0); 966 } 967 968 return (_4btol(rdcap.addr) + 1); 969 } 970 971 /* 972 * scsipi_test_unit_ready: 973 * 974 * Issue a `test unit ready' request. 975 */ 976 int 977 scsipi_test_unit_ready(periph, flags) 978 struct scsipi_periph *periph; 979 int flags; 980 { 981 struct scsipi_test_unit_ready scsipi_cmd; 982 983 /* some ATAPI drives don't support TEST_UNIT_READY. Sigh */ 984 if (periph->periph_quirks & PQUIRK_NOTUR) 985 return (0); 986 987 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 988 scsipi_cmd.opcode = TEST_UNIT_READY; 989 990 return (scsipi_command(periph, 991 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd), 992 0, 0, SCSIPIRETRIES, 10000, NULL, flags)); 993 } 994 995 /* 996 * scsipi_inquire: 997 * 998 * Ask the device about itself. 999 */ 1000 int 1001 scsipi_inquire(periph, inqbuf, flags) 1002 struct scsipi_periph *periph; 1003 struct scsipi_inquiry_data *inqbuf; 1004 int flags; 1005 { 1006 struct scsipi_inquiry scsipi_cmd; 1007 1008 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1009 scsipi_cmd.opcode = INQUIRY; 1010 scsipi_cmd.length = sizeof(struct scsipi_inquiry_data); 1011 1012 return (scsipi_command(periph, 1013 (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd), 1014 (u_char *) inqbuf, sizeof(struct scsipi_inquiry_data), 1015 SCSIPIRETRIES, 10000, NULL, XS_CTL_DATA_IN | flags)); 1016 } 1017 1018 /* 1019 * scsipi_prevent: 1020 * 1021 * Prevent or allow the user to remove the media 1022 */ 1023 int 1024 scsipi_prevent(periph, type, flags) 1025 struct scsipi_periph *periph; 1026 int type, flags; 1027 { 1028 struct scsipi_prevent scsipi_cmd; 1029 1030 if (periph->periph_quirks & PQUIRK_NODOORLOCK) 1031 return (0); 1032 1033 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1034 scsipi_cmd.opcode = PREVENT_ALLOW; 1035 scsipi_cmd.how = type; 1036 1037 return (scsipi_command(periph, 1038 (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd), 1039 0, 0, SCSIPIRETRIES, 5000, NULL, flags)); 1040 } 1041 1042 /* 1043 * scsipi_start: 1044 * 1045 * Send a START UNIT. 1046 */ 1047 int 1048 scsipi_start(periph, type, flags) 1049 struct scsipi_periph *periph; 1050 int type, flags; 1051 { 1052 struct scsipi_start_stop scsipi_cmd; 1053 1054 if (periph->periph_quirks & PQUIRK_NOSTARTUNIT) 1055 return 0; 1056 1057 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1058 scsipi_cmd.opcode = START_STOP; 1059 scsipi_cmd.byte2 = 0x00; 1060 scsipi_cmd.how = type; 1061 1062 return (scsipi_command(periph, 1063 (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd), 1064 0, 0, SCSIPIRETRIES, (type & SSS_START) ? 60000 : 10000, 1065 NULL, flags)); 1066 } 1067 1068 /* 1069 * scsipi_mode_sense, scsipi_mode_sense_big: 1070 * get a sense page from a device 1071 */ 1072 1073 int 1074 scsipi_mode_sense(periph, byte2, page, data, len, flags, retries, timeout) 1075 struct scsipi_periph *periph; 1076 int byte2, page, len, flags, retries, timeout; 1077 struct scsipi_mode_header *data; 1078 { 1079 struct scsipi_mode_sense scsipi_cmd; 1080 int error; 1081 1082 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1083 scsipi_cmd.opcode = MODE_SENSE; 1084 scsipi_cmd.byte2 = byte2; 1085 scsipi_cmd.page = page; 1086 if (scsipi_periph_bustype(periph) == SCSIPI_BUSTYPE_ATAPI) 1087 _lto2b(len, scsipi_cmd.u_len.atapi.length); 1088 else 1089 scsipi_cmd.u_len.scsi.length = len & 0xff; 1090 error = scsipi_command(periph, (struct scsipi_generic *)&scsipi_cmd, 1091 sizeof(scsipi_cmd), (void *)data, len, retries, timeout, NULL, 1092 flags | XS_CTL_DATA_IN); 1093 SC_DEBUG(periph, SCSIPI_DB2, 1094 ("scsipi_mode_sense: error=%d\n", error)); 1095 return (error); 1096 } 1097 1098 int 1099 scsipi_mode_sense_big(periph, byte2, page, data, len, flags, retries, timeout) 1100 struct scsipi_periph *periph; 1101 int byte2, page, len, flags, retries, timeout; 1102 struct scsipi_mode_header_big *data; 1103 { 1104 struct scsipi_mode_sense_big scsipi_cmd; 1105 int error; 1106 1107 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1108 scsipi_cmd.opcode = MODE_SENSE_BIG; 1109 scsipi_cmd.byte2 = byte2; 1110 scsipi_cmd.page = page; 1111 _lto2b(len, scsipi_cmd.length); 1112 error = scsipi_command(periph, (struct scsipi_generic *)&scsipi_cmd, 1113 sizeof(scsipi_cmd), (void *)data, len, retries, timeout, NULL, 1114 flags | XS_CTL_DATA_IN); 1115 SC_DEBUG(periph, SCSIPI_DB2, 1116 ("scsipi_mode_sense_big: error=%d\n", error)); 1117 return (error); 1118 } 1119 1120 int 1121 scsipi_mode_select(periph, byte2, data, len, flags, retries, timeout) 1122 struct scsipi_periph *periph; 1123 int byte2, len, flags, retries, timeout; 1124 struct scsipi_mode_header *data; 1125 { 1126 struct scsipi_mode_select scsipi_cmd; 1127 int error; 1128 1129 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1130 scsipi_cmd.opcode = MODE_SELECT; 1131 scsipi_cmd.byte2 = byte2; 1132 if (scsipi_periph_bustype(periph) == SCSIPI_BUSTYPE_ATAPI) 1133 _lto2b(len, scsipi_cmd.u_len.atapi.length); 1134 else 1135 scsipi_cmd.u_len.scsi.length = len & 0xff; 1136 error = scsipi_command(periph, (struct scsipi_generic *)&scsipi_cmd, 1137 sizeof(scsipi_cmd), (void *)data, len, retries, timeout, NULL, 1138 flags | XS_CTL_DATA_OUT); 1139 SC_DEBUG(periph, SCSIPI_DB2, 1140 ("scsipi_mode_select: error=%d\n", error)); 1141 return (error); 1142 } 1143 1144 int 1145 scsipi_mode_select_big(periph, byte2, data, len, flags, retries, timeout) 1146 struct scsipi_periph *periph; 1147 int byte2, len, flags, retries, timeout; 1148 struct scsipi_mode_header_big *data; 1149 { 1150 struct scsipi_mode_select_big scsipi_cmd; 1151 int error; 1152 1153 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd)); 1154 scsipi_cmd.opcode = MODE_SELECT_BIG; 1155 scsipi_cmd.byte2 = byte2; 1156 _lto2b(len, scsipi_cmd.length); 1157 error = scsipi_command(periph, (struct scsipi_generic *)&scsipi_cmd, 1158 sizeof(scsipi_cmd), (void *)data, len, retries, timeout, NULL, 1159 flags | XS_CTL_DATA_OUT); 1160 SC_DEBUG(periph, SCSIPI_DB2, 1161 ("scsipi_mode_select: error=%d\n", error)); 1162 return (error); 1163 } 1164 1165 /* 1166 * scsipi_done: 1167 * 1168 * This routine is called by an adapter's interrupt handler when 1169 * an xfer is completed. 1170 */ 1171 void 1172 scsipi_done(xs) 1173 struct scsipi_xfer *xs; 1174 { 1175 struct scsipi_periph *periph = xs->xs_periph; 1176 struct scsipi_channel *chan = periph->periph_channel; 1177 int s, freezecnt; 1178 1179 SC_DEBUG(periph, SCSIPI_DB2, ("scsipi_done\n")); 1180 #ifdef SCSIPI_DEBUG 1181 if (periph->periph_dbflags & SCSIPI_DB1) 1182 show_scsipi_cmd(xs); 1183 #endif 1184 1185 s = splbio(); 1186 /* 1187 * The resource this command was using is now free. 1188 */ 1189 scsipi_put_resource(chan); 1190 xs->xs_periph->periph_sent--; 1191 1192 /* 1193 * If the command was tagged, free the tag. 1194 */ 1195 if (XS_CTL_TAGTYPE(xs) != 0) 1196 scsipi_put_tag(xs); 1197 else 1198 periph->periph_flags &= ~PERIPH_UNTAG; 1199 1200 /* Mark the command as `done'. */ 1201 xs->xs_status |= XS_STS_DONE; 1202 1203 #ifdef DIAGNOSTIC 1204 if ((xs->xs_control & (XS_CTL_ASYNC|XS_CTL_POLL)) == 1205 (XS_CTL_ASYNC|XS_CTL_POLL)) 1206 panic("scsipi_done: ASYNC and POLL"); 1207 #endif 1208 1209 /* 1210 * If the xfer had an error of any sort, freeze the 1211 * periph's queue. Freeze it again if we were requested 1212 * to do so in the xfer. 1213 */ 1214 freezecnt = 0; 1215 if (xs->error != XS_NOERROR) 1216 freezecnt++; 1217 if (xs->xs_control & XS_CTL_FREEZE_PERIPH) 1218 freezecnt++; 1219 if (freezecnt != 0) 1220 scsipi_periph_freeze(periph, freezecnt); 1221 1222 /* 1223 * record the xfer with a pending sense, in case a SCSI reset is 1224 * received before the thread is waked up. 1225 */ 1226 if (xs->error == XS_BUSY && xs->status == SCSI_CHECK) { 1227 periph->periph_flags |= PERIPH_SENSE; 1228 periph->periph_xscheck = xs; 1229 } 1230 1231 /* 1232 * If this was an xfer that was not to complete asynchrnously, 1233 * let the requesting thread perform error checking/handling 1234 * in its context. 1235 */ 1236 if ((xs->xs_control & XS_CTL_ASYNC) == 0) { 1237 splx(s); 1238 /* 1239 * If it's a polling job, just return, to unwind the 1240 * call graph. We don't need to restart the queue, 1241 * because pollings jobs are treated specially, and 1242 * are really only used during crash dumps anyway 1243 * (XXX or during boot-time autconfiguration of 1244 * ATAPI devices). 1245 */ 1246 if (xs->xs_control & XS_CTL_POLL) 1247 return; 1248 wakeup(xs); 1249 goto out; 1250 } 1251 1252 /* 1253 * Catch the extremely common case of I/O completing 1254 * without error; no use in taking a context switch 1255 * if we can handle it in interrupt context. 1256 */ 1257 if (xs->error == XS_NOERROR) { 1258 splx(s); 1259 (void) scsipi_complete(xs); 1260 goto out; 1261 } 1262 1263 /* 1264 * There is an error on this xfer. Put it on the channel's 1265 * completion queue, and wake up the completion thread. 1266 */ 1267 TAILQ_INSERT_TAIL(&chan->chan_complete, xs, channel_q); 1268 splx(s); 1269 wakeup(&chan->chan_complete); 1270 1271 out: 1272 /* 1273 * If there are more xfers on the channel's queue, attempt to 1274 * run them. 1275 */ 1276 scsipi_run_queue(chan); 1277 } 1278 1279 /* 1280 * scsipi_complete: 1281 * 1282 * Completion of a scsipi_xfer. This is the guts of scsipi_done(). 1283 * 1284 * NOTE: This routine MUST be called with valid thread context 1285 * except for the case where the following two conditions are 1286 * true: 1287 * 1288 * xs->error == XS_NOERROR 1289 * XS_CTL_ASYNC is set in xs->xs_control 1290 * 1291 * The semantics of this routine can be tricky, so here is an 1292 * explanation: 1293 * 1294 * 0 Xfer completed successfully. 1295 * 1296 * ERESTART Xfer had an error, but was restarted. 1297 * 1298 * anything else Xfer had an error, return value is Unix 1299 * errno. 1300 * 1301 * If the return value is anything but ERESTART: 1302 * 1303 * - If XS_CTL_ASYNC is set, `xs' has been freed back to 1304 * the pool. 1305 * - If there is a buf associated with the xfer, 1306 * it has been biodone()'d. 1307 */ 1308 int 1309 scsipi_complete(xs) 1310 struct scsipi_xfer *xs; 1311 { 1312 struct scsipi_periph *periph = xs->xs_periph; 1313 struct scsipi_channel *chan = periph->periph_channel; 1314 struct buf *bp; 1315 int error, s; 1316 1317 #ifdef DIAGNOSTIC 1318 if ((xs->xs_control & XS_CTL_ASYNC) != 0 && xs->bp == NULL) 1319 panic("scsipi_complete: XS_CTL_ASYNC but no buf"); 1320 #endif 1321 /* 1322 * If command terminated with a CHECK CONDITION, we need to issue a 1323 * REQUEST_SENSE command. Once the REQUEST_SENSE has been processed 1324 * we'll have the real status. 1325 * Must be processed at splbio() to avoid missing a SCSI bus reset 1326 * for this command. 1327 */ 1328 s = splbio(); 1329 if (xs->error == XS_BUSY && xs->status == SCSI_CHECK) { 1330 /* request sense for a request sense ? */ 1331 if (xs->xs_control & XS_CTL_REQSENSE) { 1332 scsipi_printaddr(periph); 1333 printf("request sense for a request sense ?\n"); 1334 /* XXX maybe we should reset the device ? */ 1335 /* we've been frozen because xs->error != XS_NOERROR */ 1336 scsipi_periph_thaw(periph, 1); 1337 splx(s); 1338 if (xs->resid < xs->datalen) { 1339 printf("we read %d bytes of sense anyway:\n", 1340 xs->datalen - xs->resid); 1341 #ifdef SCSIVERBOSE 1342 scsipi_print_sense_data((void *)xs->data, 0); 1343 #endif 1344 } 1345 return EINVAL; 1346 } 1347 scsipi_request_sense(xs); 1348 } 1349 splx(s); 1350 /* 1351 * If it's a user level request, bypass all usual completion 1352 * processing, let the user work it out.. 1353 */ 1354 if ((xs->xs_control & XS_CTL_USERCMD) != 0) { 1355 SC_DEBUG(periph, SCSIPI_DB3, ("calling user done()\n")); 1356 if (xs->error != XS_NOERROR) 1357 scsipi_periph_thaw(periph, 1); 1358 scsipi_user_done(xs); 1359 SC_DEBUG(periph, SCSIPI_DB3, ("returned from user done()\n ")); 1360 return 0; 1361 } 1362 1363 1364 switch (xs->error) { 1365 case XS_NOERROR: 1366 error = 0; 1367 break; 1368 1369 case XS_SENSE: 1370 case XS_SHORTSENSE: 1371 error = (*chan->chan_bustype->bustype_interpret_sense)(xs); 1372 break; 1373 1374 case XS_RESOURCE_SHORTAGE: 1375 /* 1376 * XXX Should freeze channel's queue. 1377 */ 1378 scsipi_printaddr(periph); 1379 printf("adapter resource shortage\n"); 1380 /* FALLTHROUGH */ 1381 1382 case XS_BUSY: 1383 if (xs->error == XS_BUSY && xs->status == SCSI_QUEUE_FULL) { 1384 struct scsipi_max_openings mo; 1385 1386 /* 1387 * We set the openings to active - 1, assuming that 1388 * the command that got us here is the first one that 1389 * can't fit into the device's queue. If that's not 1390 * the case, I guess we'll find out soon enough. 1391 */ 1392 mo.mo_target = periph->periph_target; 1393 mo.mo_lun = periph->periph_lun; 1394 if (periph->periph_active < periph->periph_openings) 1395 mo.mo_openings = periph->periph_active - 1; 1396 else 1397 mo.mo_openings = periph->periph_openings - 1; 1398 #ifdef DIAGNOSTIC 1399 if (mo.mo_openings < 0) { 1400 scsipi_printaddr(periph); 1401 printf("QUEUE FULL resulted in < 0 openings\n"); 1402 panic("scsipi_done"); 1403 } 1404 #endif 1405 if (mo.mo_openings == 0) { 1406 scsipi_printaddr(periph); 1407 printf("QUEUE FULL resulted in 0 openings\n"); 1408 mo.mo_openings = 1; 1409 } 1410 scsipi_async_event(chan, ASYNC_EVENT_MAX_OPENINGS, &mo); 1411 error = ERESTART; 1412 } else if (xs->xs_retries != 0) { 1413 xs->xs_retries--; 1414 /* 1415 * Wait one second, and try again. 1416 */ 1417 if (xs->xs_control & XS_CTL_POLL) 1418 delay(1000000); 1419 else { 1420 scsipi_periph_freeze(periph, 1); 1421 callout_reset(&periph->periph_callout, 1422 hz, scsipi_periph_timed_thaw, periph); 1423 } 1424 error = ERESTART; 1425 } else 1426 error = EBUSY; 1427 break; 1428 1429 case XS_REQUEUE: 1430 error = ERESTART; 1431 break; 1432 1433 case XS_TIMEOUT: 1434 if (xs->xs_retries != 0) { 1435 xs->xs_retries--; 1436 error = ERESTART; 1437 } else 1438 error = EIO; 1439 break; 1440 1441 case XS_SELTIMEOUT: 1442 /* XXX Disable device? */ 1443 error = EIO; 1444 break; 1445 1446 case XS_RESET: 1447 if (xs->xs_control & XS_CTL_REQSENSE) { 1448 /* 1449 * request sense interrupted by reset: signal it 1450 * with EINTR return code. 1451 */ 1452 error = EINTR; 1453 } else { 1454 if (xs->xs_retries != 0) { 1455 xs->xs_retries--; 1456 error = ERESTART; 1457 } else 1458 error = EIO; 1459 } 1460 break; 1461 1462 default: 1463 scsipi_printaddr(periph); 1464 printf("invalid return code from adapter: %d\n", xs->error); 1465 error = EIO; 1466 break; 1467 } 1468 1469 s = splbio(); 1470 if (error == ERESTART) { 1471 /* 1472 * If we get here, the periph has been thawed and frozen 1473 * again if we had to issue recovery commands. Alternatively, 1474 * it may have been frozen again and in a timed thaw. In 1475 * any case, we thaw the periph once we re-enqueue the 1476 * command. Once the periph is fully thawed, it will begin 1477 * operation again. 1478 */ 1479 xs->error = XS_NOERROR; 1480 xs->status = SCSI_OK; 1481 xs->xs_status &= ~XS_STS_DONE; 1482 xs->xs_requeuecnt++; 1483 error = scsipi_enqueue(xs); 1484 if (error == 0) { 1485 scsipi_periph_thaw(periph, 1); 1486 splx(s); 1487 return (ERESTART); 1488 } 1489 } 1490 1491 /* 1492 * scsipi_done() freezes the queue if not XS_NOERROR. 1493 * Thaw it here. 1494 */ 1495 if (xs->error != XS_NOERROR) 1496 scsipi_periph_thaw(periph, 1); 1497 1498 1499 if (periph->periph_switch->psw_done) 1500 periph->periph_switch->psw_done(xs); 1501 if ((bp = xs->bp) != NULL) { 1502 if (error) { 1503 bp->b_error = error; 1504 bp->b_flags |= B_ERROR; 1505 bp->b_resid = bp->b_bcount; 1506 } else { 1507 bp->b_error = 0; 1508 bp->b_resid = xs->resid; 1509 } 1510 biodone(bp); 1511 } 1512 1513 if (xs->xs_control & XS_CTL_ASYNC) 1514 scsipi_put_xs(xs); 1515 splx(s); 1516 1517 return (error); 1518 } 1519 1520 /* 1521 * Issue a request sense for the given scsipi_xfer. Called when the xfer 1522 * returns with a CHECK_CONDITION status. Must be called in valid thread 1523 * context and at splbio(). 1524 */ 1525 1526 void 1527 scsipi_request_sense(xs) 1528 struct scsipi_xfer *xs; 1529 { 1530 struct scsipi_periph *periph = xs->xs_periph; 1531 int flags, error; 1532 struct scsipi_sense cmd; 1533 1534 periph->periph_flags |= PERIPH_SENSE; 1535 1536 /* if command was polling, request sense will too */ 1537 flags = xs->xs_control & XS_CTL_POLL; 1538 /* Polling commands can't sleep */ 1539 if (flags) 1540 flags |= XS_CTL_NOSLEEP; 1541 1542 flags |= XS_CTL_REQSENSE | XS_CTL_URGENT | XS_CTL_DATA_IN | 1543 XS_CTL_THAW_PERIPH | XS_CTL_FREEZE_PERIPH; 1544 1545 memset(&cmd, 0, sizeof(cmd)); 1546 cmd.opcode = REQUEST_SENSE; 1547 cmd.length = sizeof(struct scsipi_sense_data); 1548 1549 error = scsipi_command(periph, 1550 (struct scsipi_generic *) &cmd, sizeof(cmd), 1551 (u_char*)&xs->sense.scsi_sense, sizeof(struct scsipi_sense_data), 1552 0, 1000, NULL, flags); 1553 periph->periph_flags &= ~PERIPH_SENSE; 1554 periph->periph_xscheck = NULL; 1555 switch(error) { 1556 case 0: 1557 /* we have a valid sense */ 1558 xs->error = XS_SENSE; 1559 return; 1560 case EINTR: 1561 /* REQUEST_SENSE interrupted by bus reset. */ 1562 xs->error = XS_RESET; 1563 return; 1564 case EIO: 1565 /* request sense coudn't be performed */ 1566 /* 1567 * XXX this isn't quite rigth but we don't have anything 1568 * better for now 1569 */ 1570 xs->error = XS_DRIVER_STUFFUP; 1571 return; 1572 default: 1573 /* Notify that request sense failed. */ 1574 xs->error = XS_DRIVER_STUFFUP; 1575 scsipi_printaddr(periph); 1576 printf("request sense failed with error %d\n", error); 1577 return; 1578 } 1579 } 1580 1581 /* 1582 * scsipi_enqueue: 1583 * 1584 * Enqueue an xfer on a channel. 1585 */ 1586 int 1587 scsipi_enqueue(xs) 1588 struct scsipi_xfer *xs; 1589 { 1590 struct scsipi_channel *chan = xs->xs_periph->periph_channel; 1591 struct scsipi_xfer *qxs; 1592 int s; 1593 1594 s = splbio(); 1595 1596 /* 1597 * If the xfer is to be polled, and there are already jobs on 1598 * the queue, we can't proceed. 1599 */ 1600 if ((xs->xs_control & XS_CTL_POLL) != 0 && 1601 TAILQ_FIRST(&chan->chan_queue) != NULL) { 1602 splx(s); 1603 xs->error = XS_DRIVER_STUFFUP; 1604 return (EAGAIN); 1605 } 1606 1607 /* 1608 * If we have an URGENT xfer, it's an error recovery command 1609 * and it should just go on the head of the channel's queue. 1610 */ 1611 if (xs->xs_control & XS_CTL_URGENT) { 1612 TAILQ_INSERT_HEAD(&chan->chan_queue, xs, channel_q); 1613 goto out; 1614 } 1615 1616 /* 1617 * If this xfer has already been on the queue before, we 1618 * need to reinsert it in the correct order. That order is: 1619 * 1620 * Immediately before the first xfer for this periph 1621 * with a requeuecnt less than xs->xs_requeuecnt. 1622 * 1623 * Failing that, at the end of the queue. (We'll end up 1624 * there naturally.) 1625 */ 1626 if (xs->xs_requeuecnt != 0) { 1627 for (qxs = TAILQ_FIRST(&chan->chan_queue); qxs != NULL; 1628 qxs = TAILQ_NEXT(qxs, channel_q)) { 1629 if (qxs->xs_periph == xs->xs_periph && 1630 qxs->xs_requeuecnt < xs->xs_requeuecnt) 1631 break; 1632 } 1633 if (qxs != NULL) { 1634 TAILQ_INSERT_AFTER(&chan->chan_queue, qxs, xs, 1635 channel_q); 1636 goto out; 1637 } 1638 } 1639 TAILQ_INSERT_TAIL(&chan->chan_queue, xs, channel_q); 1640 out: 1641 if (xs->xs_control & XS_CTL_THAW_PERIPH) 1642 scsipi_periph_thaw(xs->xs_periph, 1); 1643 splx(s); 1644 return (0); 1645 } 1646 1647 /* 1648 * scsipi_run_queue: 1649 * 1650 * Start as many xfers as possible running on the channel. 1651 */ 1652 void 1653 scsipi_run_queue(chan) 1654 struct scsipi_channel *chan; 1655 { 1656 struct scsipi_xfer *xs; 1657 struct scsipi_periph *periph; 1658 int s; 1659 1660 for (;;) { 1661 s = splbio(); 1662 1663 /* 1664 * If the channel is frozen, we can't do any work right 1665 * now. 1666 */ 1667 if (chan->chan_qfreeze != 0) { 1668 splx(s); 1669 return; 1670 } 1671 1672 /* 1673 * Look for work to do, and make sure we can do it. 1674 */ 1675 for (xs = TAILQ_FIRST(&chan->chan_queue); xs != NULL; 1676 xs = TAILQ_NEXT(xs, channel_q)) { 1677 periph = xs->xs_periph; 1678 1679 if ((periph->periph_sent >= periph->periph_openings) || 1680 periph->periph_qfreeze != 0 || 1681 (periph->periph_flags & PERIPH_UNTAG) != 0) 1682 continue; 1683 1684 if ((periph->periph_flags & 1685 (PERIPH_RECOVERING | PERIPH_SENSE)) != 0 && 1686 (xs->xs_control & XS_CTL_URGENT) == 0) 1687 continue; 1688 1689 /* 1690 * We can issue this xfer! 1691 */ 1692 goto got_one; 1693 } 1694 1695 /* 1696 * Can't find any work to do right now. 1697 */ 1698 splx(s); 1699 return; 1700 1701 got_one: 1702 /* 1703 * Have an xfer to run. Allocate a resource from 1704 * the adapter to run it. If we can't allocate that 1705 * resource, we don't dequeue the xfer. 1706 */ 1707 if (scsipi_get_resource(chan) == 0) { 1708 /* 1709 * Adapter is out of resources. If the adapter 1710 * supports it, attempt to grow them. 1711 */ 1712 if (scsipi_grow_resources(chan) == 0) { 1713 /* 1714 * Wasn't able to grow resources, 1715 * nothing more we can do. 1716 */ 1717 if (xs->xs_control & XS_CTL_POLL) { 1718 scsipi_printaddr(xs->xs_periph); 1719 printf("polling command but no " 1720 "adapter resources"); 1721 /* We'll panic shortly... */ 1722 } 1723 splx(s); 1724 1725 /* 1726 * XXX: We should be able to note that 1727 * XXX: that resources are needed here! 1728 */ 1729 return; 1730 } 1731 /* 1732 * scsipi_grow_resources() allocated the resource 1733 * for us. 1734 */ 1735 } 1736 1737 /* 1738 * We have a resource to run this xfer, do it! 1739 */ 1740 TAILQ_REMOVE(&chan->chan_queue, xs, channel_q); 1741 1742 /* 1743 * If the command is to be tagged, allocate a tag ID 1744 * for it. 1745 */ 1746 if (XS_CTL_TAGTYPE(xs) != 0) 1747 scsipi_get_tag(xs); 1748 else 1749 periph->periph_flags |= PERIPH_UNTAG; 1750 periph->periph_sent++; 1751 splx(s); 1752 1753 scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs); 1754 } 1755 #ifdef DIAGNOSTIC 1756 panic("scsipi_run_queue: impossible"); 1757 #endif 1758 } 1759 1760 /* 1761 * scsipi_execute_xs: 1762 * 1763 * Begin execution of an xfer, waiting for it to complete, if necessary. 1764 */ 1765 int 1766 scsipi_execute_xs(xs) 1767 struct scsipi_xfer *xs; 1768 { 1769 struct scsipi_periph *periph = xs->xs_periph; 1770 struct scsipi_channel *chan = periph->periph_channel; 1771 int async, poll, retries, error, s; 1772 1773 xs->xs_status &= ~XS_STS_DONE; 1774 xs->error = XS_NOERROR; 1775 xs->resid = xs->datalen; 1776 xs->status = SCSI_OK; 1777 1778 #ifdef SCSIPI_DEBUG 1779 if (xs->xs_periph->periph_dbflags & SCSIPI_DB3) { 1780 printf("scsipi_execute_xs: "); 1781 show_scsipi_xs(xs); 1782 printf("\n"); 1783 } 1784 #endif 1785 1786 /* 1787 * Deal with command tagging: 1788 * 1789 * - If the device's current operating mode doesn't 1790 * include tagged queueing, clear the tag mask. 1791 * 1792 * - If the device's current operating mode *does* 1793 * include tagged queueing, set the tag_type in 1794 * the xfer to the appropriate byte for the tag 1795 * message. 1796 */ 1797 if ((PERIPH_XFER_MODE(periph) & PERIPH_CAP_TQING) == 0 || 1798 (xs->xs_control & XS_CTL_REQSENSE)) { 1799 xs->xs_control &= ~XS_CTL_TAGMASK; 1800 xs->xs_tag_type = 0; 1801 } else { 1802 /* 1803 * If the request doesn't specify a tag, give Head 1804 * tags to URGENT operations and Ordered tags to 1805 * everything else. 1806 */ 1807 if (XS_CTL_TAGTYPE(xs) == 0) { 1808 if (xs->xs_control & XS_CTL_URGENT) 1809 xs->xs_control |= XS_CTL_HEAD_TAG; 1810 else 1811 xs->xs_control |= XS_CTL_ORDERED_TAG; 1812 } 1813 1814 switch (XS_CTL_TAGTYPE(xs)) { 1815 case XS_CTL_ORDERED_TAG: 1816 xs->xs_tag_type = MSG_ORDERED_Q_TAG; 1817 break; 1818 1819 case XS_CTL_SIMPLE_TAG: 1820 xs->xs_tag_type = MSG_SIMPLE_Q_TAG; 1821 break; 1822 1823 case XS_CTL_HEAD_TAG: 1824 xs->xs_tag_type = MSG_HEAD_OF_Q_TAG; 1825 break; 1826 1827 default: 1828 scsipi_printaddr(periph); 1829 printf("invalid tag mask 0x%08x\n", 1830 XS_CTL_TAGTYPE(xs)); 1831 panic("scsipi_execute_xs"); 1832 } 1833 } 1834 1835 /* If the adaptor wants us to poll, poll. */ 1836 if (chan->chan_adapter->adapt_flags & SCSIPI_ADAPT_POLL_ONLY) 1837 xs->xs_control |= XS_CTL_POLL; 1838 1839 /* 1840 * If we don't yet have a completion thread, or we are to poll for 1841 * completion, clear the ASYNC flag. 1842 */ 1843 if (chan->chan_thread == NULL || (xs->xs_control & XS_CTL_POLL) != 0) 1844 xs->xs_control &= ~XS_CTL_ASYNC; 1845 1846 async = (xs->xs_control & XS_CTL_ASYNC); 1847 poll = (xs->xs_control & XS_CTL_POLL); 1848 retries = xs->xs_retries; /* for polling commands */ 1849 1850 #ifdef DIAGNOSTIC 1851 if (async != 0 && xs->bp == NULL) 1852 panic("scsipi_execute_xs: XS_CTL_ASYNC but no buf"); 1853 #endif 1854 1855 /* 1856 * Enqueue the transfer. If we're not polling for completion, this 1857 * should ALWAYS return `no error'. 1858 */ 1859 try_again: 1860 error = scsipi_enqueue(xs); 1861 if (error) { 1862 if (poll == 0) { 1863 scsipi_printaddr(periph); 1864 printf("not polling, but enqueue failed with %d\n", 1865 error); 1866 panic("scsipi_execute_xs"); 1867 } 1868 1869 scsipi_printaddr(periph); 1870 printf("failed to enqueue polling command"); 1871 if (retries != 0) { 1872 printf(", retrying...\n"); 1873 delay(1000000); 1874 retries--; 1875 goto try_again; 1876 } 1877 printf("\n"); 1878 goto free_xs; 1879 } 1880 1881 restarted: 1882 scsipi_run_queue(chan); 1883 1884 /* 1885 * The xfer is enqueued, and possibly running. If it's to be 1886 * completed asynchronously, just return now. 1887 */ 1888 if (async) 1889 return (EJUSTRETURN); 1890 1891 /* 1892 * Not an asynchronous command; wait for it to complete. 1893 */ 1894 s = splbio(); 1895 while ((xs->xs_status & XS_STS_DONE) == 0) { 1896 if (poll) { 1897 scsipi_printaddr(periph); 1898 printf("polling command not done\n"); 1899 panic("scsipi_execute_xs"); 1900 } 1901 (void) tsleep(xs, PRIBIO, "xscmd", 0); 1902 } 1903 splx(s); 1904 1905 /* 1906 * Command is complete. scsipi_done() has awakened us to perform 1907 * the error handling. 1908 */ 1909 error = scsipi_complete(xs); 1910 if (error == ERESTART) 1911 goto restarted; 1912 1913 /* 1914 * Command completed successfully or fatal error occurred. Fall 1915 * into.... 1916 */ 1917 free_xs: 1918 s = splbio(); 1919 scsipi_put_xs(xs); 1920 splx(s); 1921 1922 /* 1923 * Kick the queue, keep it running in case it stopped for some 1924 * reason. 1925 */ 1926 scsipi_run_queue(chan); 1927 1928 return (error); 1929 } 1930 1931 /* 1932 * scsipi_completion_thread: 1933 * 1934 * This is the completion thread. We wait for errors on 1935 * asynchronous xfers, and perform the error handling 1936 * function, restarting the command, if necessary. 1937 */ 1938 void 1939 scsipi_completion_thread(arg) 1940 void *arg; 1941 { 1942 struct scsipi_channel *chan = arg; 1943 struct scsipi_xfer *xs; 1944 int s; 1945 1946 for (;;) { 1947 s = splbio(); 1948 xs = TAILQ_FIRST(&chan->chan_complete); 1949 if (xs == NULL && 1950 (chan->chan_flags & 1951 (SCSIPI_CHAN_SHUTDOWN | SCSIPI_CHAN_CALLBACK)) == 0) { 1952 (void) tsleep(&chan->chan_complete, PRIBIO, 1953 "sccomp", 0); 1954 splx(s); 1955 continue; 1956 } 1957 if (chan->chan_flags & SCSIPI_CHAN_CALLBACK) { 1958 /* call chan_callback from thread context */ 1959 chan->chan_flags &= ~SCSIPI_CHAN_CALLBACK; 1960 chan->chan_callback(chan, chan->chan_callback_arg); 1961 splx(s); 1962 continue; 1963 } 1964 if (chan->chan_flags & SCSIPI_CHAN_SHUTDOWN) { 1965 splx(s); 1966 break; 1967 } 1968 if (xs) { 1969 TAILQ_REMOVE(&chan->chan_complete, xs, channel_q); 1970 splx(s); 1971 1972 /* 1973 * Have an xfer with an error; process it. 1974 */ 1975 (void) scsipi_complete(xs); 1976 1977 /* 1978 * Kick the queue; keep it running if it was stopped 1979 * for some reason. 1980 */ 1981 scsipi_run_queue(chan); 1982 } else { 1983 splx(s); 1984 } 1985 } 1986 1987 chan->chan_thread = NULL; 1988 1989 /* In case parent is waiting for us to exit. */ 1990 wakeup(&chan->chan_thread); 1991 1992 kthread_exit(0); 1993 } 1994 1995 /* 1996 * scsipi_create_completion_thread: 1997 * 1998 * Callback to actually create the completion thread. 1999 */ 2000 void 2001 scsipi_create_completion_thread(arg) 2002 void *arg; 2003 { 2004 struct scsipi_channel *chan = arg; 2005 struct scsipi_adapter *adapt = chan->chan_adapter; 2006 2007 if (kthread_create1(scsipi_completion_thread, chan, 2008 &chan->chan_thread, "%s:%d", adapt->adapt_dev->dv_xname, 2009 chan->chan_channel)) { 2010 printf("%s: unable to create completion thread for " 2011 "channel %d\n", adapt->adapt_dev->dv_xname, 2012 chan->chan_channel); 2013 panic("scsipi_create_completion_thread"); 2014 } 2015 } 2016 2017 /* 2018 * scsipi_thread_call_callback: 2019 * 2020 * request to call a callback from the completion thread 2021 */ 2022 int 2023 scsipi_thread_call_callback(chan, callback, arg) 2024 struct scsipi_channel *chan; 2025 void (*callback) __P((struct scsipi_channel *, void *)); 2026 void *arg; 2027 { 2028 int s; 2029 2030 s = splbio(); 2031 if (chan->chan_flags & SCSIPI_CHAN_CALLBACK) { 2032 splx(s); 2033 return EBUSY; 2034 } 2035 scsipi_channel_freeze(chan, 1); 2036 chan->chan_callback = callback; 2037 chan->chan_callback_arg = arg; 2038 chan->chan_flags |= SCSIPI_CHAN_CALLBACK; 2039 wakeup(&chan->chan_complete); 2040 splx(s); 2041 return(0); 2042 } 2043 2044 /* 2045 * scsipi_async_event: 2046 * 2047 * Handle an asynchronous event from an adapter. 2048 */ 2049 void 2050 scsipi_async_event(chan, event, arg) 2051 struct scsipi_channel *chan; 2052 scsipi_async_event_t event; 2053 void *arg; 2054 { 2055 int s; 2056 2057 s = splbio(); 2058 switch (event) { 2059 case ASYNC_EVENT_MAX_OPENINGS: 2060 scsipi_async_event_max_openings(chan, 2061 (struct scsipi_max_openings *)arg); 2062 break; 2063 2064 case ASYNC_EVENT_XFER_MODE: 2065 scsipi_async_event_xfer_mode(chan, 2066 (struct scsipi_xfer_mode *)arg); 2067 break; 2068 case ASYNC_EVENT_RESET: 2069 scsipi_async_event_channel_reset(chan); 2070 break; 2071 } 2072 splx(s); 2073 } 2074 2075 /* 2076 * scsipi_print_xfer_mode: 2077 * 2078 * Print a periph's capabilities. 2079 */ 2080 void 2081 scsipi_print_xfer_mode(periph) 2082 struct scsipi_periph *periph; 2083 { 2084 int period, freq, speed, mbs; 2085 2086 if ((periph->periph_flags & PERIPH_MODE_VALID) == 0) 2087 return; 2088 2089 printf("%s: ", periph->periph_dev->dv_xname); 2090 if (periph->periph_mode & PERIPH_CAP_SYNC) { 2091 period = scsipi_sync_factor_to_period(periph->periph_period); 2092 printf("sync (%d.%dns offset %d)", 2093 period / 10, period % 10, periph->periph_offset); 2094 } else 2095 printf("async"); 2096 2097 if (periph->periph_mode & PERIPH_CAP_WIDE32) 2098 printf(", 32-bit"); 2099 else if (periph->periph_mode & PERIPH_CAP_WIDE16) 2100 printf(", 16-bit"); 2101 else 2102 printf(", 8-bit"); 2103 2104 if (periph->periph_mode & PERIPH_CAP_SYNC) { 2105 freq = scsipi_sync_factor_to_freq(periph->periph_period); 2106 speed = freq; 2107 if (periph->periph_mode & PERIPH_CAP_WIDE32) 2108 speed *= 4; 2109 else if (periph->periph_mode & PERIPH_CAP_WIDE16) 2110 speed *= 2; 2111 mbs = speed / 1000; 2112 if (mbs > 0) 2113 printf(" (%d.%03dMB/s)", mbs, speed % 1000); 2114 else 2115 printf(" (%dKB/s)", speed % 1000); 2116 } 2117 2118 printf(" transfers"); 2119 2120 if (periph->periph_mode & PERIPH_CAP_TQING) 2121 printf(", tagged queueing"); 2122 2123 printf("\n"); 2124 } 2125 2126 /* 2127 * scsipi_async_event_max_openings: 2128 * 2129 * Update the maximum number of outstanding commands a 2130 * device may have. 2131 */ 2132 void 2133 scsipi_async_event_max_openings(chan, mo) 2134 struct scsipi_channel *chan; 2135 struct scsipi_max_openings *mo; 2136 { 2137 struct scsipi_periph *periph; 2138 int minlun, maxlun; 2139 2140 if (mo->mo_lun == -1) { 2141 /* 2142 * Wildcarded; apply it to all LUNs. 2143 */ 2144 minlun = 0; 2145 maxlun = chan->chan_nluns - 1; 2146 } else 2147 minlun = maxlun = mo->mo_lun; 2148 2149 for (; minlun <= maxlun; minlun++) { 2150 periph = scsipi_lookup_periph(chan, mo->mo_target, minlun); 2151 if (periph == NULL) 2152 continue; 2153 2154 if (mo->mo_openings < periph->periph_openings) 2155 periph->periph_openings = mo->mo_openings; 2156 else if (mo->mo_openings > periph->periph_openings && 2157 (periph->periph_flags & PERIPH_GROW_OPENINGS) != 0) 2158 periph->periph_openings = mo->mo_openings; 2159 } 2160 } 2161 2162 /* 2163 * scsipi_async_event_xfer_mode: 2164 * 2165 * Update the xfer mode for all periphs sharing the 2166 * specified I_T Nexus. 2167 */ 2168 void 2169 scsipi_async_event_xfer_mode(chan, xm) 2170 struct scsipi_channel *chan; 2171 struct scsipi_xfer_mode *xm; 2172 { 2173 struct scsipi_periph *periph; 2174 int lun, announce, mode, period, offset; 2175 2176 for (lun = 0; lun < chan->chan_nluns; lun++) { 2177 periph = scsipi_lookup_periph(chan, xm->xm_target, lun); 2178 if (periph == NULL) 2179 continue; 2180 announce = 0; 2181 2182 /* 2183 * Clamp the xfer mode down to this periph's capabilities. 2184 */ 2185 mode = xm->xm_mode & periph->periph_cap; 2186 if (mode & PERIPH_CAP_SYNC) { 2187 period = xm->xm_period; 2188 offset = xm->xm_offset; 2189 } else { 2190 period = 0; 2191 offset = 0; 2192 } 2193 2194 /* 2195 * If we do not have a valid xfer mode yet, or the parameters 2196 * are different, announce them. 2197 */ 2198 if ((periph->periph_flags & PERIPH_MODE_VALID) == 0 || 2199 periph->periph_mode != mode || 2200 periph->periph_period != period || 2201 periph->periph_offset != offset) 2202 announce = 1; 2203 2204 periph->periph_mode = mode; 2205 periph->periph_period = period; 2206 periph->periph_offset = offset; 2207 periph->periph_flags |= PERIPH_MODE_VALID; 2208 2209 if (announce) 2210 scsipi_print_xfer_mode(periph); 2211 } 2212 } 2213 2214 /* 2215 * scsipi_set_xfer_mode: 2216 * 2217 * Set the xfer mode for the specified I_T Nexus. 2218 */ 2219 void 2220 scsipi_set_xfer_mode(chan, target, immed) 2221 struct scsipi_channel *chan; 2222 int target, immed; 2223 { 2224 struct scsipi_xfer_mode xm; 2225 struct scsipi_periph *itperiph; 2226 int lun, s; 2227 2228 /* 2229 * Go to the minimal xfer mode. 2230 */ 2231 xm.xm_target = target; 2232 xm.xm_mode = 0; 2233 xm.xm_period = 0; /* ignored */ 2234 xm.xm_offset = 0; /* ignored */ 2235 2236 /* 2237 * Find the first LUN we know about on this I_T Nexus. 2238 */ 2239 for (lun = 0; lun < chan->chan_nluns; lun++) { 2240 itperiph = scsipi_lookup_periph(chan, target, lun); 2241 if (itperiph != NULL) 2242 break; 2243 } 2244 if (itperiph != NULL) { 2245 xm.xm_mode = itperiph->periph_cap; 2246 /* 2247 * Now issue the request to the adapter. 2248 */ 2249 s = splbio(); 2250 scsipi_adapter_request(chan, ADAPTER_REQ_SET_XFER_MODE, &xm); 2251 splx(s); 2252 /* 2253 * If we want this to happen immediately, issue a dummy 2254 * command, since most adapters can't really negotiate unless 2255 * they're executing a job. 2256 */ 2257 if (immed != 0) { 2258 (void) scsipi_test_unit_ready(itperiph, 2259 XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST | 2260 XS_CTL_IGNORE_NOT_READY | 2261 XS_CTL_IGNORE_MEDIA_CHANGE); 2262 } 2263 } 2264 } 2265 2266 /* 2267 * scsipi_channel_reset: 2268 * 2269 * handle scsi bus reset 2270 * called at splbio 2271 */ 2272 void 2273 scsipi_async_event_channel_reset(chan) 2274 struct scsipi_channel *chan; 2275 { 2276 struct scsipi_xfer *xs, *xs_next; 2277 struct scsipi_periph *periph; 2278 int target, lun; 2279 2280 /* 2281 * Channel has been reset. Also mark as reset pending REQUEST_SENSE 2282 * commands; as the sense is not available any more. 2283 * can't call scsipi_done() from here, as the command has not been 2284 * sent to the adapter yet (this would corrupt accounting). 2285 */ 2286 2287 for (xs = TAILQ_FIRST(&chan->chan_queue); xs != NULL; xs = xs_next) { 2288 xs_next = TAILQ_NEXT(xs, channel_q); 2289 if (xs->xs_control & XS_CTL_REQSENSE) { 2290 TAILQ_REMOVE(&chan->chan_queue, xs, channel_q); 2291 xs->error = XS_RESET; 2292 if ((xs->xs_control & XS_CTL_ASYNC) != 0) 2293 TAILQ_INSERT_TAIL(&chan->chan_complete, xs, 2294 channel_q); 2295 } 2296 } 2297 wakeup(&chan->chan_complete); 2298 /* Catch xs with pending sense which may not have a REQSENSE xs yet */ 2299 for (target = 0; target < chan->chan_ntargets; target++) { 2300 if (target == chan->chan_id) 2301 continue; 2302 for (lun = 0; lun < chan->chan_nluns; lun++) { 2303 periph = chan->chan_periphs[target][lun]; 2304 if (periph) { 2305 xs = periph->periph_xscheck; 2306 if (xs) 2307 xs->error = XS_RESET; 2308 } 2309 } 2310 } 2311 } 2312 2313 /* 2314 * scsipi_target_detach: 2315 * 2316 * detach all periph associated with a I_T 2317 * must be called from valid thread context 2318 */ 2319 int 2320 scsipi_target_detach(chan, target, lun, flags) 2321 struct scsipi_channel *chan; 2322 int target, lun; 2323 int flags; 2324 { 2325 struct scsipi_periph *periph; 2326 int ctarget, mintarget, maxtarget; 2327 int clun, minlun, maxlun; 2328 int error; 2329 2330 if (target == -1) { 2331 mintarget = 0; 2332 maxtarget = chan->chan_ntargets; 2333 } else { 2334 if (target == chan->chan_id) 2335 return EINVAL; 2336 if (target < 0 || target >= chan->chan_ntargets) 2337 return EINVAL; 2338 mintarget = target; 2339 maxtarget = target + 1; 2340 } 2341 2342 if (lun == -1) { 2343 minlun = 0; 2344 maxlun = chan->chan_nluns; 2345 } else { 2346 if (lun < 0 || lun >= chan->chan_nluns) 2347 return EINVAL; 2348 minlun = lun; 2349 maxlun = lun + 1; 2350 } 2351 2352 for (ctarget = 0; ctarget < chan->chan_ntargets; ctarget++) { 2353 if (ctarget == chan->chan_id) 2354 continue; 2355 2356 for (clun = minlun; clun < maxlun; clun++) { 2357 periph = scsipi_lookup_periph(chan, target, clun); 2358 if (periph == NULL) 2359 continue; 2360 error = config_detach(periph->periph_dev, flags); 2361 if (error) 2362 return (error); 2363 scsipi_remove_periph(chan, periph); 2364 free(periph, M_DEVBUF); 2365 } 2366 } 2367 return(0); 2368 } 2369 2370 /* 2371 * scsipi_adapter_addref: 2372 * 2373 * Add a reference to the adapter pointed to by the provided 2374 * link, enabling the adapter if necessary. 2375 */ 2376 int 2377 scsipi_adapter_addref(adapt) 2378 struct scsipi_adapter *adapt; 2379 { 2380 int s, error = 0; 2381 2382 s = splbio(); 2383 if (adapt->adapt_refcnt++ == 0 && adapt->adapt_enable != NULL) { 2384 error = (*adapt->adapt_enable)(adapt->adapt_dev, 1); 2385 if (error) 2386 adapt->adapt_refcnt--; 2387 } 2388 splx(s); 2389 return (error); 2390 } 2391 2392 /* 2393 * scsipi_adapter_delref: 2394 * 2395 * Delete a reference to the adapter pointed to by the provided 2396 * link, disabling the adapter if possible. 2397 */ 2398 void 2399 scsipi_adapter_delref(adapt) 2400 struct scsipi_adapter *adapt; 2401 { 2402 int s; 2403 2404 s = splbio(); 2405 if (adapt->adapt_refcnt-- == 1 && adapt->adapt_enable != NULL) 2406 (void) (*adapt->adapt_enable)(adapt->adapt_dev, 0); 2407 splx(s); 2408 } 2409 2410 struct scsipi_syncparam { 2411 int ss_factor; 2412 int ss_period; /* ns * 10 */ 2413 } scsipi_syncparams[] = { 2414 { 0x0a, 250 }, 2415 { 0x0b, 303 }, 2416 { 0x0c, 500 }, 2417 }; 2418 const int scsipi_nsyncparams = 2419 sizeof(scsipi_syncparams) / sizeof(scsipi_syncparams[0]); 2420 2421 int 2422 scsipi_sync_period_to_factor(period) 2423 int period; /* ns * 10 */ 2424 { 2425 int i; 2426 2427 for (i = 0; i < scsipi_nsyncparams; i++) { 2428 if (period <= scsipi_syncparams[i].ss_period) 2429 return (scsipi_syncparams[i].ss_factor); 2430 } 2431 2432 return ((period / 10) / 4); 2433 } 2434 2435 int 2436 scsipi_sync_factor_to_period(factor) 2437 int factor; 2438 { 2439 int i; 2440 2441 for (i = 0; i < scsipi_nsyncparams; i++) { 2442 if (factor == scsipi_syncparams[i].ss_factor) 2443 return (scsipi_syncparams[i].ss_period); 2444 } 2445 2446 return ((factor * 4) * 10); 2447 } 2448 2449 int 2450 scsipi_sync_factor_to_freq(factor) 2451 int factor; 2452 { 2453 int i; 2454 2455 for (i = 0; i < scsipi_nsyncparams; i++) { 2456 if (factor == scsipi_syncparams[i].ss_factor) 2457 return (10000000 / scsipi_syncparams[i].ss_period); 2458 } 2459 2460 return (10000000 / ((factor * 4) * 10)); 2461 } 2462 2463 #ifdef SCSIPI_DEBUG 2464 /* 2465 * Given a scsipi_xfer, dump the request, in all it's glory 2466 */ 2467 void 2468 show_scsipi_xs(xs) 2469 struct scsipi_xfer *xs; 2470 { 2471 2472 printf("xs(%p): ", xs); 2473 printf("xs_control(0x%08x)", xs->xs_control); 2474 printf("xs_status(0x%08x)", xs->xs_status); 2475 printf("periph(%p)", xs->xs_periph); 2476 printf("retr(0x%x)", xs->xs_retries); 2477 printf("timo(0x%x)", xs->timeout); 2478 printf("cmd(%p)", xs->cmd); 2479 printf("len(0x%x)", xs->cmdlen); 2480 printf("data(%p)", xs->data); 2481 printf("len(0x%x)", xs->datalen); 2482 printf("res(0x%x)", xs->resid); 2483 printf("err(0x%x)", xs->error); 2484 printf("bp(%p)", xs->bp); 2485 show_scsipi_cmd(xs); 2486 } 2487 2488 void 2489 show_scsipi_cmd(xs) 2490 struct scsipi_xfer *xs; 2491 { 2492 u_char *b = (u_char *) xs->cmd; 2493 int i = 0; 2494 2495 scsipi_printaddr(xs->xs_periph); 2496 printf(" command: "); 2497 2498 if ((xs->xs_control & XS_CTL_RESET) == 0) { 2499 while (i < xs->cmdlen) { 2500 if (i) 2501 printf(","); 2502 printf("0x%x", b[i++]); 2503 } 2504 printf("-[%d bytes]\n", xs->datalen); 2505 if (xs->datalen) 2506 show_mem(xs->data, min(64, xs->datalen)); 2507 } else 2508 printf("-RESET-\n"); 2509 } 2510 2511 void 2512 show_mem(address, num) 2513 u_char *address; 2514 int num; 2515 { 2516 int x; 2517 2518 printf("------------------------------"); 2519 for (x = 0; x < num; x++) { 2520 if ((x % 16) == 0) 2521 printf("\n%03d: ", x); 2522 printf("%02x ", *address++); 2523 } 2524 printf("\n------------------------------\n"); 2525 } 2526 #endif /* SCSIPI_DEBUG */ 2527