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