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