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