1 /* $NetBSD: esc.c,v 1.3 2001/11/27 00:53:12 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Scott Stevens 5 * Copyright (c) 1995 Daniel Widenfalk 6 * Copyright (c) 1994 Christian E. Hopps 7 * Copyright (c) 1990 The Regents of the University of California. 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * Van Jacobson of Lawrence Berkeley Laboratory. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)scsi.c 7.5 (Berkeley) 5/4/91 42 */ 43 44 /* 45 * AMD AM53CF94 scsi adaptor driver 46 * 47 * Functionally compatible with the FAS216 48 * 49 * Apart from a very small patch to set up control register 4 50 */ 51 52 /* 53 * Modified for NetBSD/arm32 by Scott Stevens 54 */ 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/device.h> 59 #include <sys/buf.h> 60 #include <sys/proc.h> 61 #include <dev/scsipi/scsi_all.h> 62 #include <dev/scsipi/scsipi_all.h> 63 #include <dev/scsipi/scsiconf.h> 64 65 #include <uvm/uvm_extern.h> 66 67 #include <machine/pmap.h> 68 #include <machine/cpu.h> 69 #include <machine/io.h> 70 #include <machine/intr.h> 71 #include <arm/arm32/katelib.h> 72 #include <acorn32/podulebus/podulebus.h> 73 #include <acorn32/podulebus/escreg.h> 74 #include <acorn32/podulebus/escvar.h> 75 76 /* Externs */ 77 extern pt_entry_t *pmap_pte __P((pmap_t, vm_offset_t)); 78 79 void escinitialize __P((struct esc_softc *)); 80 void esc_minphys __P((struct buf *bp)); 81 void esc_scsi_request __P((struct scsipi_channel *, 82 scsipi_adapter_req_t, void *)); 83 void esc_donextcmd __P((struct esc_softc *dev, struct esc_pending *pendp)); 84 void esc_scsidone __P((struct esc_softc *dev, struct scsipi_xfer *xs, 85 int stat)); 86 void escintr __P((struct esc_softc *dev)); 87 void esciwait __P((struct esc_softc *dev)); 88 void escreset __P((struct esc_softc *dev, int how)); 89 int escselect __P((struct esc_softc *dev, struct esc_pending *pendp, 90 unsigned char *cbuf, int clen, 91 unsigned char *buf, int len, int mode)); 92 void escicmd __P((struct esc_softc *dev, struct esc_pending *pendp)); 93 int escgo __P((struct esc_softc *dev, struct esc_pending *pendp)); 94 95 /* 96 * Initialize these to make 'em patchable. Defaults to enable sync and discon. 97 */ 98 u_char esc_inhibit_sync[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 99 u_char esc_inhibit_disc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 100 101 #define DEBUG 102 #ifdef DEBUG 103 #define QPRINTF(a) if (esc_debug > 1) printf a 104 int esc_debug = 2; 105 #else 106 #define QPRINTF 107 #endif 108 109 /* 110 * default minphys routine for esc based controllers 111 */ 112 void 113 esc_minphys(bp) 114 struct buf *bp; 115 { 116 117 /* 118 * No max transfer at this level. 119 */ 120 minphys(bp); 121 } 122 123 /* 124 * Initialize the nexus structs. 125 */ 126 void 127 esc_init_nexus(dev, nexus) 128 struct esc_softc *dev; 129 struct nexus *nexus; 130 { 131 bzero(nexus, sizeof(struct nexus)); 132 133 nexus->state = ESC_NS_IDLE; 134 nexus->period = 200; 135 nexus->offset = 0; 136 nexus->syncper = 5; 137 nexus->syncoff = 0; 138 nexus->config3 = dev->sc_config3 & ~ESC_CFG3_FASTSCSI; 139 } 140 141 void 142 escinitialize(dev) 143 struct esc_softc *dev; 144 { 145 u_int *pte; 146 int i; 147 148 dev->sc_led_status = 0; 149 150 TAILQ_INIT(&dev->sc_xs_pending); 151 TAILQ_INIT(&dev->sc_xs_free); 152 153 /* 154 * Initialize the esc_pending structs and link them into the free list. We 155 * have to set vm_link_data.pages to 0 or the vm FIX won't work. 156 */ 157 for(i=0; i<MAXPENDING; i++) { 158 TAILQ_INSERT_TAIL(&dev->sc_xs_free, &dev->sc_xs_store[i], 159 link); 160 } 161 162 /* 163 * Calculate the correct clock conversion factor 2 <= factor <= 8, i.e. set 164 * the factor to clock_freq / 5 (int). 165 */ 166 if (dev->sc_clock_freq <= 10) 167 dev->sc_clock_conv_fact = 2; 168 if (dev->sc_clock_freq <= 40) 169 dev->sc_clock_conv_fact = 2+((dev->sc_clock_freq-10)/5); 170 else 171 panic("escinitialize: Clock frequence too high"); 172 173 /* Setup and save the basic configuration registers */ 174 dev->sc_config1 = (dev->sc_host_id & ESC_CFG1_BUS_ID_MASK); 175 dev->sc_config2 = ESC_CFG2_FEATURES_ENABLE; 176 dev->sc_config3 = (dev->sc_clock_freq > 25 ? ESC_CFG3_FASTCLK : 0); 177 178 /* Precalculate timeout value and clock period. */ 179 /* Ekkk ... floating point in the kernel !!!! */ 180 /* dev->sc_timeout_val = 1+dev->sc_timeout*dev->sc_clock_freq/ 181 (7.682*dev->sc_clock_conv_fact);*/ 182 dev->sc_timeout_val = 1+dev->sc_timeout*dev->sc_clock_freq/ 183 ((7682*dev->sc_clock_conv_fact)/1000); 184 dev->sc_clock_period = 1000/dev->sc_clock_freq; 185 186 escreset(dev, 1 | 2); /* Reset Chip and Bus */ 187 188 dev->sc_units_disconnected = 0; 189 dev->sc_msg_in_len = 0; 190 dev->sc_msg_out_len = 0; 191 192 dev->sc_flags = 0; 193 194 for(i=0; i<8; i++) 195 esc_init_nexus(dev, &dev->sc_nexus[i]); 196 197 /* 198 * Setup bump buffer. 199 */ 200 dev->sc_bump_va = (u_char *)uvm_km_zalloc(kernel_map, dev->sc_bump_sz); 201 (void) pmap_extract(pmap_kernel(), (vaddr_t)dev->sc_bump_va, 202 (paddr_t *)&dev->sc_bump_pa); 203 204 /* 205 * Setup pages to noncachable, that way we don't have to flush the cache 206 * every time we need "bumped" transfer. 207 */ 208 pte = pmap_pte(pmap_kernel(), (vm_offset_t)dev->sc_bump_va); 209 *pte &= ~PT_C; 210 cpu_tlb_flushD(); 211 cpu_cache_purgeD_rng((vm_offset_t)dev->sc_bump_va, NBPG); 212 213 printf(" dmabuf V0x%08x P0x%08x", (u_int)dev->sc_bump_va, (u_int)dev->sc_bump_pa); 214 } 215 216 217 /* 218 * used by specific esc controller 219 */ 220 void 221 esc_scsi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, 222 void *arg) 223 { 224 struct scsipi_xfer *xs; 225 struct esc_softc *dev = (void *)chan->chan_adapter->adapt_dev; 226 struct scsipi_periph *periph; 227 struct esc_pending *pendp; 228 int flags, s, target; 229 230 switch (req) { 231 case ADAPTER_REQ_RUN_XFER: 232 xs = arg; 233 periph = xs->xs_periph; 234 flags = xs->xs_control; 235 target = periph->periph_target; 236 237 if (flags & XS_CTL_DATA_UIO) 238 panic("esc: scsi data uio requested"); 239 240 if ((flags & XS_CTL_POLL) && (dev->sc_flags & ESC_ACTIVE)) 241 panic("esc_scsicmd: busy"); 242 243 /* Get hold of a esc_pending block. */ 244 s = splbio(); 245 pendp = dev->sc_xs_free.tqh_first; 246 if (pendp == NULL) { 247 splx(s); 248 xs->error = XS_RESOURCE_SHORTAGE; 249 scsipi_done(xs); 250 return; 251 } 252 TAILQ_REMOVE(&dev->sc_xs_free, pendp, link); 253 pendp->xs = xs; 254 splx(s); 255 256 257 /* If the chip if busy OR the unit is busy, we have to wait for out turn. */ 258 if ((dev->sc_flags & ESC_ACTIVE) || 259 (dev->sc_nexus[target].flags & ESC_NF_UNIT_BUSY)) { 260 s = splbio(); 261 TAILQ_INSERT_TAIL(&dev->sc_xs_pending, pendp, link); 262 splx(s); 263 } else 264 esc_donextcmd(dev, pendp); 265 266 return; 267 case ADAPTER_REQ_GROW_RESOURCES: 268 case ADAPTER_REQ_SET_XFER_MODE: 269 /* XXX Not supported. */ 270 return; 271 } 272 273 } 274 275 /* 276 * Actually select the unit, whereby the whole scsi-process is started. 277 */ 278 void 279 esc_donextcmd(dev, pendp) 280 struct esc_softc *dev; 281 struct esc_pending *pendp; 282 { 283 int s; 284 285 /* 286 * Special case for scsi unit reset. I think this is waterproof. We first 287 * select the unit during splbio. We then cycle through the generated 288 * interrupts until the interrupt routine signals that the unit has 289 * acknowledged the reset. After that we have to wait a reset to select 290 * delay before anything else can happend. 291 */ 292 if (pendp->xs->xs_control & XS_CTL_RESET) { 293 struct nexus *nexus; 294 295 s = splbio(); 296 while(!escselect(dev, pendp, 0, 0, 0, 0, ESC_SELECT_K)) { 297 splx(s); 298 delay(10); 299 s = splbio(); 300 } 301 302 nexus = dev->sc_cur_nexus; 303 while(nexus->flags & ESC_NF_UNIT_BUSY) { 304 esciwait(dev); 305 escintr(dev); 306 } 307 308 nexus->flags |= ESC_NF_UNIT_BUSY; 309 splx(s); 310 311 escreset(dev, 0); 312 313 s = splbio(); 314 nexus->flags &= ~ESC_NF_UNIT_BUSY; 315 splx(s); 316 } 317 318 /* 319 * If we are polling, go to splbio and perform the command, else we poke 320 * the scsi-bus via escgo to get the interrupt machine going. 321 */ 322 if (pendp->xs->xs_control & XS_CTL_POLL) { 323 s = splbio(); 324 escicmd(dev, pendp); 325 TAILQ_INSERT_TAIL(&dev->sc_xs_free, pendp, link); 326 splx(s); 327 } else { 328 escgo(dev, pendp); 329 return; 330 } 331 } 332 333 void 334 esc_scsidone(dev, xs, stat) 335 struct esc_softc *dev; 336 struct scsipi_xfer *xs; 337 int stat; 338 { 339 struct esc_pending *pendp; 340 int s; 341 342 xs->status = stat; 343 344 if (stat == 0) 345 xs->resid = 0; 346 else { 347 switch(stat) { 348 case SCSI_CHECK: 349 case SCSI_BUSY: 350 xs->error = XS_BUSY; 351 break; 352 case -1: 353 xs->error = XS_DRIVER_STUFFUP; 354 QPRINTF(("esc_scsicmd() bad %x\n", stat)); 355 break; 356 default: 357 xs->error = XS_TIMEOUT; 358 break; 359 } 360 } 361 362 /* Steal the next command from the queue so that one unit can't hog the bus. */ 363 s = splbio(); 364 pendp = dev->sc_xs_pending.tqh_first; 365 while(pendp) { 366 if (!(dev->sc_nexus[pendp->xs->xs_periph->periph_target].flags & 367 ESC_NF_UNIT_BUSY)) 368 break; 369 pendp = pendp->link.tqe_next; 370 } 371 372 if (pendp != NULL) { 373 TAILQ_REMOVE(&dev->sc_xs_pending, pendp, link); 374 } 375 376 splx(s); 377 scsipi_done(xs); 378 379 if (pendp) 380 esc_donextcmd(dev, pendp); 381 } 382 383 /* 384 * There are two kinds of reset: 385 * 1) CHIP-bus reset. This also implies a SCSI-bus reset. 386 * 2) SCSI-bus reset. 387 * After the appropriate resets have been performed we wait a reset to select 388 * delay time. 389 */ 390 void 391 escreset(dev, how) 392 struct esc_softc *dev; 393 int how; 394 { 395 esc_regmap_p rp; 396 int i, s; 397 398 rp = dev->sc_esc; 399 400 if (how & 1) { 401 for(i=0; i<8; i++) 402 esc_init_nexus(dev, &dev->sc_nexus[i]); 403 404 *rp->esc_command = ESC_CMD_RESET_CHIP; 405 delay(1); 406 *rp->esc_command = ESC_CMD_NOP; 407 408 *rp->esc_config1 = dev->sc_config1; 409 *rp->esc_config2 = dev->sc_config2; 410 *rp->esc_config3 = dev->sc_config3; 411 *rp->esc_config4 = dev->sc_config4; 412 *rp->esc_timeout = dev->sc_timeout_val; 413 *rp->esc_clkconv = dev->sc_clock_conv_fact & 414 ESC_CLOCK_CONVERSION_MASK; 415 } 416 417 if (how & 2) { 418 for(i=0; i<8; i++) 419 esc_init_nexus(dev, &dev->sc_nexus[i]); 420 421 s = splbio(); 422 423 *rp->esc_command = ESC_CMD_RESET_SCSI_BUS; 424 delay(100); 425 426 /* Skip interrupt generated by RESET_SCSI_BUS */ 427 while(*rp->esc_status & ESC_STAT_INTERRUPT_PENDING) { 428 dev->sc_status = *rp->esc_status; 429 dev->sc_interrupt = *rp->esc_interrupt; 430 431 delay(100); 432 } 433 434 dev->sc_status = *rp->esc_status; 435 dev->sc_interrupt = *rp->esc_interrupt; 436 437 splx(s); 438 } 439 440 if (dev->sc_config_flags & ESC_SLOW_START) 441 delay(4*250000); /* RESET to SELECT DELAY*4 for slow devices */ 442 else 443 delay(250000); /* RESET to SELECT DELAY */ 444 } 445 446 /* 447 * Save active data pointers to the nexus block currently active. 448 */ 449 void 450 esc_save_pointers(dev) 451 struct esc_softc *dev; 452 { 453 struct nexus *nx; 454 455 nx = dev->sc_cur_nexus; 456 if (nx) { 457 nx->cur_link = dev->sc_cur_link; 458 nx->max_link = dev->sc_max_link; 459 nx->buf = dev->sc_buf; 460 nx->len = dev->sc_len; 461 nx->dma_len = dev->sc_dma_len; 462 nx->dma_buf = dev->sc_dma_buf; 463 nx->dma_blk_flg = dev->sc_dma_blk_flg; 464 nx->dma_blk_len = dev->sc_dma_blk_len; 465 nx->dma_blk_ptr = dev->sc_dma_blk_ptr; 466 } 467 } 468 469 /* 470 * Restore data pointers from the currently active nexus block. 471 */ 472 void 473 esc_restore_pointers(dev) 474 struct esc_softc *dev; 475 { 476 struct nexus *nx; 477 478 nx = dev->sc_cur_nexus; 479 if (nx) { 480 dev->sc_cur_link = nx->cur_link; 481 dev->sc_max_link = nx->max_link; 482 dev->sc_buf = nx->buf; 483 dev->sc_len = nx->len; 484 dev->sc_dma_len = nx->dma_len; 485 dev->sc_dma_buf = nx->dma_buf; 486 dev->sc_dma_blk_flg = nx->dma_blk_flg; 487 dev->sc_dma_blk_len = nx->dma_blk_len; 488 dev->sc_dma_blk_ptr = nx->dma_blk_ptr; 489 dev->sc_chain = nx->dma; 490 dev->sc_unit = (nx->lun_unit & 0x0F); 491 dev->sc_lun = (nx->lun_unit & 0xF0) >> 4; 492 } 493 } 494 495 /* 496 * esciwait is used during interrupt and polled IO to wait for an event from 497 * the FAS chip. This function MUST NOT BE CALLED without interrupt disabled. 498 */ 499 void 500 esciwait(dev) 501 struct esc_softc *dev; 502 { 503 esc_regmap_p rp; 504 505 /* 506 * If ESC_DONT_WAIT is set, we have already grabbed the interrupt info 507 * elsewhere. So we don't have to wait for it. 508 */ 509 if (dev->sc_flags & ESC_DONT_WAIT) { 510 dev->sc_flags &= ~ESC_DONT_WAIT; 511 return; 512 } 513 514 rp = dev->sc_esc; 515 516 /* Wait for FAS chip to signal an interrupt. */ 517 while(!(*rp->esc_status & ESC_STAT_INTERRUPT_PENDING)); 518 /* delay(1);*/ 519 520 /* Grab interrupt info from chip. */ 521 dev->sc_status = *rp->esc_status; 522 dev->sc_interrupt = *rp->esc_interrupt; 523 if (dev->sc_interrupt & ESC_INT_RESELECTED) { 524 dev->sc_resel[0] = *rp->esc_fifo; 525 dev->sc_resel[1] = *rp->esc_fifo; 526 } 527 } 528 529 #if 0 530 /* 531 * Transfer info to/from device. esc_ixfer uses polled IO+esciwait so the 532 * rules that apply to esciwait also applies here. 533 */ 534 void 535 esc_ixfer(dev) 536 struct esc_softc *dev; 537 { 538 esc_regmap_p rp; 539 u_char *buf; 540 int len, mode, phase; 541 542 rp = dev->sc_esc; 543 buf = dev->sc_buf; 544 len = dev->sc_len; 545 546 /* 547 * Decode the scsi phase to determine whether we are reading or writing. 548 * mode == 1 => READ, mode == 0 => WRITE 549 */ 550 phase = dev->sc_status & ESC_STAT_PHASE_MASK; 551 mode = (phase == ESC_PHASE_DATA_IN); 552 553 while(len && ((dev->sc_status & ESC_STAT_PHASE_MASK) == phase)) 554 if (mode) { 555 *rp->esc_command = ESC_CMD_TRANSFER_INFO; 556 557 esciwait(dev); 558 559 *buf++ = *rp->esc_fifo; 560 len--; 561 } else { 562 len--; 563 *rp->esc_fifo = *buf++; 564 *rp->esc_command = ESC_CMD_TRANSFER_INFO; 565 566 esciwait(dev); 567 } 568 569 /* Update buffer pointers to reflect the sent/received data. */ 570 dev->sc_buf = buf; 571 dev->sc_len = len; 572 573 /* 574 * Since the last esciwait will be a phase-change, we can't wait for it 575 * again later, so we have to signal that. 576 */ 577 578 dev->sc_flags |= ESC_DONT_WAIT; 579 } 580 #else 581 /* 582 * Transfer info to/from device. esc_ixfer uses polled IO+esciwait so the 583 * rules that apply to esciwait also applies here. 584 */ 585 void 586 esc_ixfer(dev) 587 struct esc_softc *dev; 588 { 589 esc_regmap_p rp; 590 vu_char *esc_status; 591 vu_char *esc_command; 592 vu_char *esc_interrupt; 593 vu_char *esc_fifo; 594 u_char *buf; 595 int len, mode, phase; 596 597 rp = dev->sc_esc; 598 buf = dev->sc_buf; 599 len = dev->sc_len; 600 601 /* Use discrete variables for better optimisation */ 602 603 esc_status = rp->esc_status; 604 esc_command = rp->esc_command; 605 esc_interrupt = rp->esc_interrupt; 606 esc_fifo = rp->esc_fifo; 607 608 /* 609 * Decode the scsi phase to determine whether we are reading or writing. 610 * mode == 1 => READ, mode == 0 => WRITE 611 */ 612 phase = dev->sc_status & ESC_STAT_PHASE_MASK; 613 mode = (phase == ESC_PHASE_DATA_IN); 614 615 if (mode) { 616 while(len && ((dev->sc_status & ESC_STAT_PHASE_MASK) == phase)) { 617 *esc_command = ESC_CMD_TRANSFER_INFO; 618 619 /* Wait for FAS chip to signal an interrupt. */ 620 while(!(*esc_status & ESC_STAT_INTERRUPT_PENDING)); 621 /* delay(1);*/ 622 623 /* Grab interrupt info from chip. */ 624 dev->sc_status = *esc_status; 625 dev->sc_interrupt = *esc_interrupt; 626 627 *buf++ = *esc_fifo; 628 len--; 629 } 630 } else { 631 while(len && ((dev->sc_status & ESC_STAT_PHASE_MASK) == phase)) { 632 len--; 633 *esc_fifo = *buf++; 634 *esc_command = ESC_CMD_TRANSFER_INFO; 635 636 /* Wait for FAS chip to signal an interrupt. */ 637 while(!(*esc_status & ESC_STAT_INTERRUPT_PENDING)); 638 /* delay(1);*/ 639 640 /* Grab interrupt info from chip. */ 641 dev->sc_status = *esc_status; 642 dev->sc_interrupt = *esc_interrupt; 643 } 644 } 645 646 /* Update buffer pointers to reflect the sent/received data. */ 647 dev->sc_buf = buf; 648 dev->sc_len = len; 649 650 /* 651 * Since the last esciwait will be a phase-change, we can't wait for it 652 * again later, so we have to signal that. 653 */ 654 655 dev->sc_flags |= ESC_DONT_WAIT; 656 } 657 #endif 658 659 /* 660 * Build a Synchronous Data Transfer Request message 661 */ 662 void 663 esc_build_sdtrm(dev, period, offset) 664 struct esc_softc *dev; 665 int period; 666 int offset; 667 { 668 dev->sc_msg_out[0] = 0x01; 669 dev->sc_msg_out[1] = 0x03; 670 dev->sc_msg_out[2] = 0x01; 671 dev->sc_msg_out[3] = period/4; 672 dev->sc_msg_out[4] = offset; 673 dev->sc_msg_out_len= 5; 674 } 675 676 /* 677 * Arbitate the scsi bus and select the unit 678 */ 679 int 680 esc_select_unit(dev, target) 681 struct esc_softc *dev; 682 short target; 683 { 684 esc_regmap_p rp; 685 struct nexus *nexus; 686 int s, retcode, i; 687 u_char cmd; 688 689 s = splbio(); /* Do this at splbio so that we won't be disturbed. */ 690 691 retcode = 0; 692 693 nexus = &dev->sc_nexus[target]; 694 695 /* 696 * Check if the chip is busy. If not the we mark it as so and hope that nobody 697 * reselects us until we have grabbed the bus. 698 */ 699 if (!(dev->sc_flags & ESC_ACTIVE) && !dev->sc_sel_nexus) { 700 dev->sc_flags |= ESC_ACTIVE; 701 702 rp = dev->sc_esc; 703 704 *rp->esc_syncper = nexus->syncper; 705 *rp->esc_syncoff = nexus->syncoff; 706 *rp->esc_config3 = nexus->config3; 707 708 *rp->esc_config1 = dev->sc_config1; 709 *rp->esc_timeout = dev->sc_timeout_val; 710 *rp->esc_dest_id = target; 711 712 /* If nobody has stolen the bus, we can send a select command to the chip. */ 713 if (!(*rp->esc_status & ESC_STAT_INTERRUPT_PENDING)) { 714 *rp->esc_fifo = nexus->ID; 715 if ((nexus->flags & (ESC_NF_DO_SDTR | ESC_NF_RESET)) 716 || (dev->sc_msg_out_len != 0)) 717 cmd = ESC_CMD_SEL_ATN_STOP; 718 else { 719 for(i=0; i<nexus->clen; i++) 720 *rp->esc_fifo = nexus->cbuf[i]; 721 722 cmd = ESC_CMD_SEL_ATN; 723 } 724 725 dev->sc_sel_nexus = nexus; 726 727 *rp->esc_command = cmd; 728 retcode = 1; 729 nexus->flags &= ~ESC_NF_RETRY_SELECT; 730 } else 731 nexus->flags |= ESC_NF_RETRY_SELECT; 732 } else 733 nexus->flags |= ESC_NF_RETRY_SELECT; 734 735 splx(s); 736 return(retcode); 737 } 738 739 /* 740 * Grab the nexus if available else return 0. 741 */ 742 struct nexus * 743 esc_arbitate_target(dev, target) 744 struct esc_softc *dev; 745 int target; 746 { 747 struct nexus *nexus; 748 int s; 749 750 /* 751 * This is realy simple. Raise interrupt level to splbio. Grab the nexus and 752 * leave. 753 */ 754 nexus = &dev->sc_nexus[target]; 755 756 s = splbio(); 757 758 if (nexus->flags & ESC_NF_UNIT_BUSY) 759 nexus = 0; 760 else 761 nexus->flags |= ESC_NF_UNIT_BUSY; 762 763 splx(s); 764 return(nexus); 765 } 766 767 /* 768 * Setup a nexus for use. Initializes command, buffer pointers and dma chain. 769 */ 770 void 771 esc_setup_nexus(dev, nexus, pendp, cbuf, clen, buf, len, mode) 772 struct esc_softc *dev; 773 struct nexus *nexus; 774 struct esc_pending *pendp; 775 unsigned char *cbuf; 776 int clen; 777 unsigned char *buf; 778 int len; 779 int mode; 780 { 781 int sync, target, lun; 782 783 target = pendp->xs->xs_periph->periph_target; 784 lun = pendp->xs->xs_periph->periph_lun; 785 786 /* 787 * Adopt mode to reflect the config flags. 788 * If we can't use DMA we can't use synch transfer. Also check the 789 * esc_inhibit_xxx[target] flags. 790 */ 791 if ((dev->sc_config_flags & (ESC_NO_SYNCH | ESC_NO_DMA)) || 792 esc_inhibit_sync[(int)target]) 793 mode &= ~ESC_SELECT_S; 794 795 if ((dev->sc_config_flags & ESC_NO_RESELECT) || 796 esc_inhibit_disc[(int)target]) 797 mode &= ~ESC_SELECT_R; 798 799 nexus->xs = pendp->xs; 800 801 /* Setup the nexus struct. */ 802 nexus->ID = ((mode & ESC_SELECT_R) ? 0xC0 : 0x80) | lun; 803 nexus->clen = clen; 804 bcopy(cbuf, nexus->cbuf, nexus->clen); 805 nexus->cbuf[1] |= lun << 5; /* Fix the lun bits */ 806 nexus->cur_link = 0; 807 nexus->dma_len = 0; 808 nexus->dma_buf = 0; 809 nexus->dma_blk_len = 0; 810 nexus->dma_blk_ptr = 0; 811 nexus->len = len; 812 nexus->buf = buf; 813 nexus->lun_unit = (lun << 4) | target; 814 nexus->state = ESC_NS_SELECTED; 815 816 /* We must keep these flags. All else must be zero. */ 817 nexus->flags &= ESC_NF_UNIT_BUSY 818 | ESC_NF_SYNC_TESTED | ESC_NF_SELECT_ME; 819 820 if (mode & ESC_SELECT_I) 821 nexus->flags |= ESC_NF_IMMEDIATE; 822 if (mode & ESC_SELECT_K) 823 nexus->flags |= ESC_NF_RESET; 824 825 sync = ((mode & ESC_SELECT_S) ? 1 : 0); 826 827 /* We can't use sync during polled IO. */ 828 if (sync && (mode & ESC_SELECT_I)) 829 sync = 0; 830 831 if (!sync && 832 ((nexus->flags & ESC_NF_SYNC_TESTED) && (nexus->offset != 0))) { 833 /* 834 * If the scsi unit is set to synch transfer and we don't want 835 * that, we have to renegotiate. 836 */ 837 838 nexus->flags |= ESC_NF_DO_SDTR; 839 nexus->period = 200; 840 nexus->offset = 0; 841 } else if (sync && !(nexus->flags & ESC_NF_SYNC_TESTED)) { 842 /* 843 * If the scsi unit is not set to synch transfer and we want 844 * that, we have to negotiate. This should realy base the 845 * period on the clock frequence rather than just check if 846 * >25Mhz 847 */ 848 849 nexus->flags |= ESC_NF_DO_SDTR; 850 nexus->period = ((dev->sc_clock_freq>25) ? 100 : 200); 851 nexus->offset = 8; 852 853 /* If the user has a long cable, we want to limit the period */ 854 if ((nexus->period == 100) && 855 (dev->sc_config_flags & ESC_SLOW_CABLE)) 856 nexus->period = 200; 857 } 858 859 /* 860 * Fake a dma-block for polled IO. This way we can use the same code to handle 861 * reselection. Much nicer this way. 862 */ 863 if ((mode & ESC_SELECT_I) || (dev->sc_config_flags & ESC_NO_DMA)) { 864 nexus->dma[0].ptr = (vm_offset_t)buf; 865 nexus->dma[0].len = len; 866 nexus->dma[0].flg = ESC_CHAIN_PRG; 867 nexus->max_link = 1; 868 } else { 869 nexus->max_link = dev->sc_build_dma_chain(dev, nexus->dma, 870 buf, len); 871 } 872 873 /* Flush the caches. */ 874 875 if (len && !(mode & ESC_SELECT_I)) 876 cpu_cache_purgeD_rng((vm_offset_t)buf, len); 877 } 878 879 int 880 escselect(dev, pendp, cbuf, clen, buf, len, mode) 881 struct esc_softc *dev; 882 struct esc_pending *pendp; 883 unsigned char *cbuf; 884 int clen; 885 unsigned char *buf; 886 int len; 887 int mode; 888 { 889 struct nexus *nexus; 890 891 /* Get the nexus struct. */ 892 nexus = esc_arbitate_target(dev, pendp->xs->xs_periph->periph_target); 893 if (nexus == NULL) 894 return(0); 895 896 /* Setup the nexus struct. */ 897 esc_setup_nexus(dev, nexus, pendp, cbuf, clen, buf, len, mode); 898 899 /* Post it to the interrupt machine. */ 900 esc_select_unit(dev, pendp->xs->xs_periph->periph_target); 901 902 return(1); 903 } 904 905 int 906 escgo(dev, pendp) 907 struct esc_softc *dev; 908 struct esc_pending *pendp; 909 { 910 int s; 911 char *buf; 912 913 buf = pendp->xs->data; 914 915 if (escselect(dev, pendp, (char *)pendp->xs->cmd, pendp->xs->cmdlen, 916 buf, pendp->xs->datalen, ESC_SELECT_RS)) { 917 /* 918 * We got the command going so the esc_pending struct is now 919 * free to reuse. 920 */ 921 922 s = splbio(); 923 TAILQ_INSERT_TAIL(&dev->sc_xs_free, pendp, link); 924 splx(s); 925 } else { 926 /* 927 * We couldn't make the command fly so we have to wait. The 928 * struct MUST be inserted at the head to keep the order of 929 * the commands. 930 */ 931 932 s = splbio(); 933 TAILQ_INSERT_HEAD(&dev->sc_xs_pending, pendp, link); 934 splx(s); 935 } 936 937 return(0); 938 } 939 940 /* 941 * Part one of the interrupt machine. Error checks and reselection test. 942 * We don't know if we have an active nexus here! 943 */ 944 int 945 esc_pretests(dev, rp) 946 struct esc_softc *dev; 947 esc_regmap_p rp; 948 { 949 struct nexus *nexus; 950 int i, s; 951 952 if (dev->sc_interrupt & ESC_INT_SCSI_RESET_DETECTED) { 953 /* 954 * Cleanup and notify user. Lets hope that this is all we 955 * have to do 956 */ 957 958 for(i=0; i<8; i++) { 959 if (dev->sc_nexus[i].xs) 960 esc_scsidone(dev, dev->sc_nexus[i].xs, -2); 961 962 esc_init_nexus(dev, &dev->sc_nexus[i]); 963 } 964 printf("escintr: SCSI-RESET detected!"); 965 return(-1); 966 } 967 968 if (dev->sc_interrupt & ESC_INT_ILLEGAL_COMMAND) { 969 /* Something went terrible wrong! Dump some data and panic! */ 970 971 printf("FIFO:"); 972 while(*rp->esc_fifo_flags & ESC_FIFO_COUNT_MASK) 973 printf(" %x", *rp->esc_fifo); 974 printf("\n"); 975 976 printf("CMD: %x\n", *rp->esc_command); 977 panic("escintr: ILLEGAL COMMAND!"); 978 } 979 980 if (dev->sc_interrupt & ESC_INT_RESELECTED) { 981 /* We were reselected. Set the chip as busy */ 982 983 s = splbio(); 984 dev->sc_flags |= ESC_ACTIVE; 985 if (dev->sc_sel_nexus) { 986 dev->sc_sel_nexus->flags |= ESC_NF_SELECT_ME; 987 dev->sc_sel_nexus = 0; 988 } 989 splx(s); 990 991 if (dev->sc_units_disconnected) { 992 /* Find out who reselected us. */ 993 994 dev->sc_resel[0] &= ~(1<<dev->sc_host_id); 995 996 for(i=0; i<8; i++) 997 if (dev->sc_resel[0] & (1<<i)) 998 break; 999 1000 if (i == 8) 1001 panic("Illegal reselection!"); 1002 1003 if (dev->sc_nexus[i].state == ESC_NS_DISCONNECTED) { 1004 /* 1005 * This unit had disconnected, so we reconnect 1006 * it. 1007 */ 1008 1009 dev->sc_cur_nexus = &dev->sc_nexus[i]; 1010 nexus = dev->sc_cur_nexus; 1011 1012 *rp->esc_syncper = nexus->syncper; 1013 *rp->esc_syncoff = nexus->syncoff; 1014 *rp->esc_config3 = nexus->config3; 1015 1016 *rp->esc_dest_id = i & 7; 1017 1018 dev->sc_units_disconnected--; 1019 dev->sc_msg_in_len= 0; 1020 1021 /* Restore active pointers. */ 1022 esc_restore_pointers(dev); 1023 1024 nexus->state = ESC_NS_RESELECTED; 1025 1026 *rp->esc_command = ESC_CMD_MESSAGE_ACCEPTED; 1027 1028 return(1); 1029 } 1030 } 1031 1032 /* Somehow we got an illegal reselection. Dump and panic. */ 1033 printf("escintr: resel[0] %x resel[1] %x disconnected %d\n", 1034 dev->sc_resel[0], dev->sc_resel[1], 1035 dev->sc_units_disconnected); 1036 panic("escintr: Unexpected reselection!"); 1037 } 1038 1039 return(0); 1040 } 1041 1042 /* 1043 * Part two of the interrupt machine. Handle disconnection and post command 1044 * processing. We know that we have an active nexus here. 1045 */ 1046 int 1047 esc_midaction(dev, rp, nexus) 1048 struct esc_softc *dev; 1049 esc_regmap_p rp; 1050 struct nexus *nexus; 1051 { 1052 int i, left, len, s; 1053 u_char status, msg; 1054 1055 if (dev->sc_interrupt & ESC_INT_DISCONNECT) { 1056 s = splbio(); 1057 dev->sc_cur_nexus = 0; 1058 1059 /* Mark chip as busy and clean up the chip FIFO. */ 1060 dev->sc_flags &= ~ESC_ACTIVE; 1061 *rp->esc_command = ESC_CMD_FLUSH_FIFO; 1062 1063 /* Let the nexus state reflect what we have to do. */ 1064 switch(nexus->state) { 1065 case ESC_NS_SELECTED: 1066 dev->sc_sel_nexus = 0; 1067 nexus->flags &= ~ESC_NF_SELECT_ME; 1068 1069 /* 1070 * We were trying to select the unit. Probably no unit 1071 * at this ID. 1072 */ 1073 nexus->xs->resid = dev->sc_len; 1074 1075 nexus->status = -2; 1076 nexus->flags &= ~ESC_NF_UNIT_BUSY; 1077 nexus->state = ESC_NS_FINISHED; 1078 break; 1079 1080 case ESC_NS_DONE: 1081 /* All done. */ 1082 nexus->xs->resid = dev->sc_len; 1083 1084 nexus->flags &= ~ESC_NF_UNIT_BUSY; 1085 nexus->state = ESC_NS_FINISHED; 1086 dev->sc_led(dev, 0); 1087 break; 1088 1089 case ESC_NS_DISCONNECTING: 1090 /* 1091 * We have received a DISCONNECT message, so we are 1092 * doing a normal disconnection. 1093 */ 1094 nexus->state = ESC_NS_DISCONNECTED; 1095 1096 dev->sc_units_disconnected++; 1097 break; 1098 1099 case ESC_NS_RESET: 1100 /* 1101 * We were reseting this SCSI-unit. Clean up the 1102 * nexus struct. 1103 */ 1104 dev->sc_led(dev, 0); 1105 esc_init_nexus(dev, nexus); 1106 break; 1107 1108 default: 1109 /* 1110 * Unexpected disconnection! Cleanup and exit. This 1111 * shouldn't cause any problems. 1112 */ 1113 printf("escintr: Unexpected disconnection\n"); 1114 printf("escintr: u %x s %d p %d f %x c %x\n", 1115 nexus->lun_unit, nexus->state, 1116 dev->sc_status & ESC_STAT_PHASE_MASK, 1117 nexus->flags, nexus->cbuf[0]); 1118 1119 nexus->xs->resid = dev->sc_len; 1120 1121 nexus->flags &= ~ESC_NF_UNIT_BUSY; 1122 nexus->state = ESC_NS_FINISHED; 1123 nexus->status = -3; 1124 1125 dev->sc_led(dev, 0); 1126 break; 1127 } 1128 1129 /* 1130 * If we have disconnected units, we MUST enable reselection 1131 * within 250ms. 1132 */ 1133 if (dev->sc_units_disconnected && 1134 !(dev->sc_flags & ESC_ACTIVE)) 1135 *rp->esc_command = ESC_CMD_ENABLE_RESEL; 1136 1137 splx(s); 1138 1139 /* Select the first pre-initialized nexus we find. */ 1140 for(i=0; i<8; i++) 1141 if (dev->sc_nexus[i].flags & (ESC_NF_SELECT_ME | ESC_NF_RETRY_SELECT)) 1142 if (esc_select_unit(dev, i) == 2) 1143 break; 1144 1145 /* We are done with this nexus! */ 1146 if (nexus->state == ESC_NS_FINISHED) 1147 esc_scsidone(dev, nexus->xs, nexus->status); 1148 1149 return(1); 1150 } 1151 1152 switch(nexus->state) { 1153 case ESC_NS_SELECTED: 1154 dev->sc_cur_nexus = nexus; 1155 dev->sc_sel_nexus = 0; 1156 1157 nexus->flags &= ~ESC_NF_SELECT_ME; 1158 1159 /* 1160 * We have selected a unit. Setup chip, restore pointers and 1161 * light the led. 1162 */ 1163 *rp->esc_syncper = nexus->syncper; 1164 *rp->esc_syncoff = nexus->syncoff; 1165 *rp->esc_config3 = nexus->config3; 1166 1167 esc_restore_pointers(dev); 1168 1169 nexus->status = 0xFF; 1170 dev->sc_msg_in[0] = 0xFF; 1171 dev->sc_msg_in_len= 0; 1172 1173 dev->sc_led(dev, 1); 1174 1175 break; 1176 1177 case ESC_NS_DATA_IN: 1178 case ESC_NS_DATA_OUT: 1179 /* We have transfered data. */ 1180 if (dev->sc_dma_len) 1181 if (dev->sc_cur_link < dev->sc_max_link) { 1182 /* 1183 * Clean up dma and at the same time get how 1184 * many bytes that were NOT transfered. 1185 */ 1186 left = dev->sc_setup_dma(dev, 0, 0, ESC_DMA_CLEAR); 1187 len = dev->sc_dma_len; 1188 1189 if (nexus->state == ESC_NS_DATA_IN) { 1190 /* 1191 * If we were bumping we may have had an odd length 1192 * which means that there may be bytes left in the 1193 * fifo. We also need to move the data from the 1194 * bump buffer to the actual memory. 1195 */ 1196 if (dev->sc_dma_buf == dev->sc_bump_pa) 1197 { 1198 while((*rp->esc_fifo_flags&ESC_FIFO_COUNT_MASK) 1199 && left) 1200 dev->sc_bump_va[len-(left--)] = *rp->esc_fifo; 1201 1202 bcopy(dev->sc_bump_va, dev->sc_buf, len-left); 1203 } 1204 } else { 1205 /* Count any unsent bytes and flush them. */ 1206 left+= *rp->esc_fifo_flags & ESC_FIFO_COUNT_MASK; 1207 *rp->esc_command = ESC_CMD_FLUSH_FIFO; 1208 } 1209 1210 /* 1211 * Update pointers/length to reflect the transfered 1212 * data. 1213 */ 1214 dev->sc_len -= len-left; 1215 dev->sc_buf += len-left; 1216 1217 dev->sc_dma_buf += len-left; 1218 dev->sc_dma_len = left; 1219 1220 dev->sc_dma_blk_ptr += len-left; 1221 dev->sc_dma_blk_len -= len-left; 1222 1223 /* 1224 * If it was the end of a dma block, we select the 1225 * next to begin with. 1226 */ 1227 if (!dev->sc_dma_blk_len) 1228 dev->sc_cur_link++; 1229 } 1230 break; 1231 1232 case ESC_NS_STATUS: 1233 /* 1234 * If we were not sensing, grab the status byte. If we were 1235 * sensing and we got a bad status, let the user know. 1236 */ 1237 1238 status = *rp->esc_fifo; 1239 msg = *rp->esc_fifo; 1240 1241 nexus->status = status; 1242 if (status != 0) 1243 nexus->status = -1; 1244 1245 /* 1246 * Preload the command complete message. Handeled in 1247 * esc_postaction. 1248 */ 1249 dev->sc_msg_in[0] = msg; 1250 dev->sc_msg_in_len = 1; 1251 nexus->flags |= ESC_NF_HAS_MSG; 1252 break; 1253 1254 default: 1255 break; 1256 } 1257 1258 return(0); 1259 } 1260 1261 /* 1262 * Part three of the interrupt machine. Handle phase changes (and repeated 1263 * phase passes). We know that we have an active nexus here. 1264 */ 1265 int 1266 esc_postaction(dev, rp, nexus) 1267 struct esc_softc *dev; 1268 esc_regmap_p rp; 1269 struct nexus *nexus; 1270 { 1271 int i, len; 1272 u_char cmd; 1273 short offset, period; 1274 1275 cmd = 0; 1276 1277 switch(dev->sc_status & ESC_STAT_PHASE_MASK) { 1278 case ESC_PHASE_DATA_OUT: 1279 case ESC_PHASE_DATA_IN: 1280 if ((dev->sc_status & ESC_STAT_PHASE_MASK) == 1281 ESC_PHASE_DATA_OUT) 1282 nexus->state = ESC_NS_DATA_OUT; 1283 else 1284 nexus->state = ESC_NS_DATA_IN; 1285 1286 /* Make DMA ready to accept new data. Load active pointers 1287 * from the DMA block. */ 1288 dev->sc_setup_dma(dev, 0, 0, ESC_DMA_CLEAR); 1289 if (dev->sc_cur_link < dev->sc_max_link) { 1290 if (!dev->sc_dma_blk_len) { 1291 dev->sc_dma_blk_ptr = dev->sc_chain[dev->sc_cur_link].ptr; 1292 dev->sc_dma_blk_len = dev->sc_chain[dev->sc_cur_link].len; 1293 dev->sc_dma_blk_flg = dev->sc_chain[dev->sc_cur_link].flg; 1294 } 1295 1296 /* We should use polled IO here. */ 1297 if (dev->sc_dma_blk_flg == ESC_CHAIN_PRG) { 1298 esc_ixfer(dev/*, nexus->xs->xs_control & XS_CTL_POLL*/); 1299 dev->sc_cur_link++; 1300 dev->sc_dma_len = 0; 1301 break; 1302 } 1303 else if (dev->sc_dma_blk_flg == ESC_CHAIN_BUMP) 1304 len = dev->sc_dma_blk_len; 1305 else 1306 len = dev->sc_need_bump(dev, dev->sc_dma_blk_ptr, 1307 dev->sc_dma_blk_len); 1308 1309 /* 1310 * If len != 0 we must bump the data, else we just DMA it 1311 * straight into memory. 1312 */ 1313 if (len) { 1314 dev->sc_dma_buf = dev->sc_bump_pa; 1315 dev->sc_dma_len = len; 1316 1317 if (nexus->state == ESC_NS_DATA_OUT) 1318 bcopy(dev->sc_buf, dev->sc_bump_va, dev->sc_dma_len); 1319 } else { 1320 dev->sc_dma_buf = dev->sc_dma_blk_ptr; 1321 dev->sc_dma_len = dev->sc_dma_blk_len; 1322 } 1323 1324 /* Load DMA with adress and length of transfer. */ 1325 dev->sc_setup_dma(dev, dev->sc_dma_buf, dev->sc_dma_len, 1326 ((nexus->state == ESC_NS_DATA_OUT) ? 1327 ESC_DMA_WRITE : ESC_DMA_READ)); 1328 1329 printf("Using DMA !!!!\n"); 1330 cmd = ESC_CMD_TRANSFER_INFO | ESC_CMD_DMA; 1331 } else { 1332 /* 1333 * Hmmm, the unit wants more info than we have or has 1334 * more than we want. Let the chip handle that. 1335 */ 1336 1337 *rp->esc_tc_low = 0; 1338 *rp->esc_tc_mid = 1; 1339 *rp->esc_tc_high = 0; 1340 cmd = ESC_CMD_TRANSFER_PAD; 1341 } 1342 break; 1343 1344 case ESC_PHASE_COMMAND: 1345 /* The scsi unit wants the command, send it. */ 1346 nexus->state = ESC_NS_SVC; 1347 1348 *rp->esc_command = ESC_CMD_FLUSH_FIFO; 1349 for(i=0; i<5; i++); 1350 1351 for(i=0; i<nexus->clen; i++) 1352 *rp->esc_fifo = nexus->cbuf[i]; 1353 cmd = ESC_CMD_TRANSFER_INFO; 1354 break; 1355 1356 case ESC_PHASE_STATUS: 1357 /* 1358 * We've got status phase. Request status and command 1359 * complete message. 1360 */ 1361 nexus->state = ESC_NS_STATUS; 1362 cmd = ESC_CMD_COMMAND_COMPLETE; 1363 break; 1364 1365 case ESC_PHASE_MESSAGE_OUT: 1366 /* 1367 * Either the scsi unit wants us to send a message or we have 1368 * asked for it by seting the ATN bit. 1369 */ 1370 nexus->state = ESC_NS_MSG_OUT; 1371 1372 *rp->esc_command = ESC_CMD_FLUSH_FIFO; 1373 1374 if (nexus->flags & ESC_NF_DO_SDTR) { 1375 /* Send a Synchronous Data Transfer Request. */ 1376 1377 esc_build_sdtrm(dev, nexus->period, nexus->offset); 1378 nexus->flags |= ESC_NF_SDTR_SENT; 1379 nexus->flags &= ~ESC_NF_DO_SDTR; 1380 } else if (nexus->flags & ESC_NF_RESET) { 1381 /* Send a reset scsi unit message. */ 1382 1383 dev->sc_msg_out[0] = 0x0C; 1384 dev->sc_msg_out_len = 1; 1385 nexus->state = ESC_NS_RESET; 1386 nexus->flags &= ~ESC_NF_RESET; 1387 } else if (dev->sc_msg_out_len == 0) { 1388 /* Don't know what to send so we send a NOP message. */ 1389 1390 dev->sc_msg_out[0] = 0x08; 1391 dev->sc_msg_out_len = 1; 1392 } 1393 1394 cmd = ESC_CMD_TRANSFER_INFO; 1395 1396 for(i=0; i<dev->sc_msg_out_len; i++) 1397 *rp->esc_fifo = dev->sc_msg_out[i]; 1398 dev->sc_msg_out_len = 0; 1399 1400 break; 1401 1402 case ESC_PHASE_MESSAGE_IN: 1403 /* Receive a message from the scsi unit. */ 1404 nexus->state = ESC_NS_MSG_IN; 1405 1406 while(!(nexus->flags & ESC_NF_HAS_MSG)) { 1407 *rp->esc_command = ESC_CMD_TRANSFER_INFO; 1408 esciwait(dev); 1409 1410 dev->sc_msg_in[dev->sc_msg_in_len++] = *rp->esc_fifo; 1411 1412 /* Check if we got all the bytes in the message. */ 1413 if (dev->sc_msg_in[0] >= 0x80) ; 1414 else if (dev->sc_msg_in[0] >= 0x30) ; 1415 else if (((dev->sc_msg_in[0] >= 0x20) && 1416 (dev->sc_msg_in_len == 2)) || 1417 ((dev->sc_msg_in[0] != 0x01) && 1418 (dev->sc_msg_in_len == 1))) { 1419 nexus->flags |= ESC_NF_HAS_MSG; 1420 break; 1421 } else { 1422 if (dev->sc_msg_in_len >= 2) 1423 if ((dev->sc_msg_in[1]+2) == dev->sc_msg_in_len) { 1424 nexus->flags |= ESC_NF_HAS_MSG; 1425 break; 1426 } 1427 } 1428 1429 *rp->esc_command = ESC_CMD_MESSAGE_ACCEPTED; 1430 esciwait(dev); 1431 1432 if ((dev->sc_status & ESC_STAT_PHASE_MASK) != 1433 ESC_PHASE_MESSAGE_IN) 1434 break; 1435 } 1436 1437 cmd = ESC_CMD_MESSAGE_ACCEPTED; 1438 if (nexus->flags & ESC_NF_HAS_MSG) { 1439 /* We have a message. Decode it. */ 1440 1441 switch(dev->sc_msg_in[0]) { 1442 case 0x00: /* COMMAND COMPLETE */ 1443 nexus->state = ESC_NS_DONE; 1444 case 0x04: /* DISCONNECT */ 1445 nexus->state = ESC_NS_DISCONNECTING; 1446 break; 1447 case 0x02: /* SAVE DATA POINTER */ 1448 esc_save_pointers(dev); 1449 break; 1450 case 0x03: /* RESTORE DATA POINTERS */ 1451 esc_restore_pointers(dev); 1452 break; 1453 case 0x07: /* MESSAGE REJECT */ 1454 /* 1455 * If we had sent a SDTR and we got a message 1456 * reject, the scsi docs say that we must go 1457 * to async transfer. 1458 */ 1459 if (nexus->flags & ESC_NF_SDTR_SENT) { 1460 nexus->flags &= ~ESC_NF_SDTR_SENT; 1461 1462 nexus->config3 &= ~ESC_CFG3_FASTSCSI; 1463 nexus->syncper = 5; 1464 nexus->syncoff = 0; 1465 1466 *rp->esc_syncper = nexus->syncper; 1467 *rp->esc_syncoff = nexus->syncoff; 1468 *rp->esc_config3 = nexus->config3; 1469 } else 1470 /* 1471 * Something was rejected but we don't know 1472 * what! PANIC! 1473 */ 1474 panic("escintr: Unknown message rejected!"); 1475 break; 1476 case 0x08: /* MO OPERATION */ 1477 break; 1478 case 0x01: /* EXTENDED MESSAGE */ 1479 switch(dev->sc_msg_in[2]) { 1480 case 0x01:/* SYNC. DATA TRANSFER REQUEST */ 1481 /* Decode the SDTR message. */ 1482 period = 4*dev->sc_msg_in[3]; 1483 offset = dev->sc_msg_in[4]; 1484 1485 /* 1486 * Make sure that the specs are within 1487 * chip limits. Note that if we 1488 * initiated the negotiation the specs 1489 * WILL be withing chip limits. If it 1490 * was the scsi unit that initiated 1491 * the negotiation, the specs may be 1492 * to high. 1493 */ 1494 if (offset > 16) 1495 offset = 16; 1496 if ((period < 200) && 1497 (dev->sc_clock_freq <= 25)) 1498 period = 200; 1499 1500 if (offset == 0) 1501 period = 5*dev->sc_clock_period; 1502 1503 nexus->syncper = period/ 1504 dev->sc_clock_period; 1505 nexus->syncoff = offset; 1506 1507 if (period < 200) 1508 nexus->config3 |= ESC_CFG3_FASTSCSI; 1509 else 1510 nexus->config3 &=~ESC_CFG3_FASTSCSI; 1511 1512 nexus->flags |= ESC_NF_SYNC_TESTED; 1513 1514 *rp->esc_syncper = nexus->syncper; 1515 *rp->esc_syncoff = nexus->syncoff; 1516 *rp->esc_config3 = nexus->config3; 1517 1518 /* 1519 * Hmmm, it seems that the scsi unit 1520 * initiated sync negotiation, so lets 1521 * reply acording to scsi-2 standard. 1522 */ 1523 if (!(nexus->flags& ESC_NF_SDTR_SENT)) 1524 { 1525 if ((dev->sc_config_flags & 1526 ESC_NO_SYNCH) || 1527 (dev->sc_config_flags & 1528 ESC_NO_DMA) || 1529 esc_inhibit_sync[ 1530 nexus->lun_unit & 7]) { 1531 period = 200; 1532 offset = 0; 1533 } 1534 1535 nexus->offset = offset; 1536 nexus->period = period; 1537 nexus->flags |= ESC_NF_DO_SDTR; 1538 *rp->esc_command = ESC_CMD_SET_ATN; 1539 } 1540 1541 nexus->flags &= ~ESC_NF_SDTR_SENT; 1542 break; 1543 1544 case 0x00: /* MODIFY DATA POINTERS */ 1545 case 0x02: /* EXTENDED IDENTIFY (SCSI-1) */ 1546 case 0x03: /* WIDE DATA TRANSFER REQUEST */ 1547 default: 1548 /* Reject any unhandeled messages. */ 1549 1550 dev->sc_msg_out[0] = 0x07; 1551 dev->sc_msg_out_len = 1; 1552 *rp->esc_command = ESC_CMD_SET_ATN; 1553 cmd = ESC_CMD_MESSAGE_ACCEPTED; 1554 break; 1555 } 1556 break; 1557 1558 default: 1559 /* Reject any unhandeled messages. */ 1560 1561 dev->sc_msg_out[0] = 0x07; 1562 dev->sc_msg_out_len = 1; 1563 *rp->esc_command = ESC_CMD_SET_ATN; 1564 cmd = ESC_CMD_MESSAGE_ACCEPTED; 1565 break; 1566 } 1567 nexus->flags &= ~ESC_NF_HAS_MSG; 1568 dev->sc_msg_in_len = 0; 1569 } 1570 break; 1571 default: 1572 printf("ESCINTR: UNKNOWN PHASE! phase: %d\n", 1573 dev->sc_status & ESC_STAT_PHASE_MASK); 1574 dev->sc_led(dev, 0); 1575 esc_scsidone(dev, nexus->xs, -4); 1576 1577 return(-1); 1578 } 1579 1580 if (cmd) 1581 *rp->esc_command = cmd; 1582 1583 return(0); 1584 } 1585 1586 /* 1587 * Stub for interrupt machine. 1588 */ 1589 void 1590 escintr(dev) 1591 struct esc_softc *dev; 1592 { 1593 esc_regmap_p rp; 1594 struct nexus *nexus; 1595 1596 rp = dev->sc_esc; 1597 1598 if (!esc_pretests(dev, rp)) { 1599 1600 nexus = dev->sc_cur_nexus; 1601 if (nexus == NULL) 1602 nexus = dev->sc_sel_nexus; 1603 1604 if (nexus) 1605 if (!esc_midaction(dev, rp, nexus)) 1606 esc_postaction(dev, rp, nexus); 1607 } 1608 } 1609 1610 /* 1611 * escicmd is used to perform IO when we can't use interrupts. escicmd 1612 * emulates the normal environment by waiting for the chip and calling 1613 * escintr. 1614 */ 1615 void 1616 escicmd(dev, pendp) 1617 struct esc_softc *dev; 1618 struct esc_pending *pendp; 1619 { 1620 esc_regmap_p rp; 1621 struct nexus *nexus; 1622 1623 nexus = &dev->sc_nexus[pendp->xs->xs_periph->periph_target]; 1624 rp = dev->sc_esc; 1625 1626 if (!escselect(dev, pendp, (char *)pendp->xs->cmd, pendp->xs->cmdlen, 1627 (char *)pendp->xs->data, pendp->xs->datalen, 1628 ESC_SELECT_I)) 1629 panic("escicmd: Couldn't select unit"); 1630 1631 while(nexus->state != ESC_NS_FINISHED) { 1632 esciwait(dev); 1633 escintr(dev); 1634 } 1635 1636 nexus->flags &= ~ESC_NF_SYNC_TESTED; 1637 } 1638