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