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