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