1 /* $NetBSD: mb89352.c,v 1.12 2002/04/05 18:27:52 bouyer Exp $ */ 2 /* NecBSD: mb89352.c,v 1.4 1998/03/14 07:31:20 kmatsuda Exp */ 3 4 /*- 5 * Copyright (c) 1996,97,98,99 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Charles M. Hannum, Masaru Oki and Kouichi Matsuda. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by Charles M. Hannum. 22 * 4. The name of the author may not be used to endorse or promote products 23 * derived from this software without specific prior written permission. 24 * 25 * Copyright (c) 1994 Jarle Greipsland 26 * All rights reserved. 27 * 28 * Redistribution and use in source and binary forms, with or without 29 * modification, are permitted provided that the following conditions 30 * are met: 31 * 1. Redistributions of source code must retain the above copyright 32 * notice, this list of conditions and the following disclaimer. 33 * 2. Redistributions in binary form must reproduce the above copyright 34 * notice, this list of conditions and the following disclaimer in the 35 * documentation and/or other materials provided with the distribution. 36 * 3. The name of the author may not be used to endorse or promote products 37 * derived from this software without specific prior written permission. 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 40 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 41 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 42 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 43 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 44 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 45 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 48 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 49 * POSSIBILITY OF SUCH DAMAGE. 50 */ 51 /* 52 * [NetBSD for NEC PC-98 series] 53 * Copyright (c) 1996, 1997, 1998 54 * NetBSD/pc98 porting staff. All rights reserved. 55 * Copyright (c) 1996, 1997, 1998 56 * Kouichi Matsuda. All rights reserved. 57 */ 58 59 /* 60 * Acknowledgements: Many of the algorithms used in this driver are 61 * inspired by the work of Julian Elischer (julian@tfs.com) and 62 * Charles Hannum (mycroft@duality.gnu.ai.mit.edu). Thanks a million! 63 */ 64 65 /* TODO list: 66 * 1) Get the DMA stuff working. 67 * 2) Get the iov/uio stuff working. Is this a good thing ??? 68 * 3) Get the synch stuff working. 69 * 4) Rewrite it to use malloc for the acb structs instead of static alloc.? 70 */ 71 72 #include <sys/cdefs.h> 73 __KERNEL_RCSID(0, "$NetBSD: mb89352.c,v 1.12 2002/04/05 18:27:52 bouyer Exp $"); 74 75 #ifdef DDB 76 #define integrate 77 #else 78 #define integrate __inline static 79 #endif 80 81 /* 82 * A few customizable items: 83 */ 84 85 /* Use doubleword transfers to/from SCSI chip. Note: This requires 86 * motherboard support. Basicly, some motherboard chipsets are able to 87 * split a 32 bit I/O operation into two 16 bit I/O operations, 88 * transparently to the processor. This speeds up some things, notably long 89 * data transfers. 90 */ 91 #define SPC_USE_DWORDS 0 92 93 /* Synchronous data transfers? */ 94 #define SPC_USE_SYNCHRONOUS 0 95 #define SPC_SYNC_REQ_ACK_OFS 8 96 97 /* Wide data transfers? */ 98 #define SPC_USE_WIDE 0 99 #define SPC_MAX_WIDTH 0 100 101 /* Max attempts made to transmit a message */ 102 #define SPC_MSG_MAX_ATTEMPT 3 /* Not used now XXX */ 103 104 /* 105 * Some spin loop parameters (essentially how long to wait some places) 106 * The problem(?) is that sometimes we expect either to be able to transmit a 107 * byte or to get a new one from the SCSI bus pretty soon. In order to avoid 108 * returning from the interrupt just to get yanked back for the next byte we 109 * may spin in the interrupt routine waiting for this byte to come. How long? 110 * This is really (SCSI) device and processor dependent. Tuneable, I guess. 111 */ 112 #define SPC_MSGIN_SPIN 1 /* Will spinwait upto ?ms for a new msg byte */ 113 #define SPC_MSGOUT_SPIN 1 114 115 /* Include debug functions? At the end of this file there are a bunch of 116 * functions that will print out various information regarding queued SCSI 117 * commands, driver state and chip contents. You can call them from the 118 * kernel debugger. If you set SPC_DEBUG to 0 they are not included (the 119 * kernel uses less memory) but you lose the debugging facilities. 120 */ 121 #define SPC_DEBUG 1 122 123 #define SPC_ABORT_TIMEOUT 2000 /* time to wait for abort */ 124 125 /* End of customizable parameters */ 126 127 /* 128 * MB89352 SCSI Protocol Controller (SPC) routines. 129 */ 130 131 #include "opt_ddb.h" 132 133 #include <sys/param.h> 134 #include <sys/systm.h> 135 #include <sys/kernel.h> 136 #include <sys/errno.h> 137 #include <sys/ioctl.h> 138 #include <sys/device.h> 139 #include <sys/buf.h> 140 #include <sys/proc.h> 141 #include <sys/user.h> 142 #include <sys/queue.h> 143 144 #include <machine/intr.h> 145 #include <machine/bus.h> 146 147 #include <dev/scsipi/scsi_all.h> 148 #include <dev/scsipi/scsipi_all.h> 149 #include <dev/scsipi/scsi_message.h> 150 #include <dev/scsipi/scsiconf.h> 151 152 #include <dev/ic/mb89352reg.h> 153 #include <dev/ic/mb89352var.h> 154 155 #ifndef DDB 156 #define Debugger() panic("should call debugger here (mb89352.c)") 157 #endif /* ! DDB */ 158 159 #if SPC_DEBUG 160 int spc_debug = 0x00; /* SPC_SHOWSTART|SPC_SHOWMISC|SPC_SHOWTRACE; */ 161 #endif 162 163 void spc_minphys __P((struct buf *)); 164 void spc_done __P((struct spc_softc *, struct spc_acb *)); 165 void spc_dequeue __P((struct spc_softc *, struct spc_acb *)); 166 void spc_scsipi_request __P((struct scsipi_channel *, 167 scsipi_adapter_req_t, void *)); 168 int spc_poll __P((struct spc_softc *, struct scsipi_xfer *, int)); 169 integrate void spc_sched_msgout __P((struct spc_softc *, u_char)); 170 integrate void spc_setsync __P((struct spc_softc *, struct spc_tinfo *)); 171 void spc_select __P((struct spc_softc *, struct spc_acb *)); 172 void spc_timeout __P((void *)); 173 void spc_scsi_reset __P((struct spc_softc *)); 174 void spc_reset __P((struct spc_softc *)); 175 void spc_free_acb __P((struct spc_softc *, struct spc_acb *, int)); 176 struct spc_acb* spc_get_acb __P((struct spc_softc *)); 177 int spc_reselect __P((struct spc_softc *, int)); 178 void spc_msgin __P((struct spc_softc *)); 179 void spc_abort __P((struct spc_softc *, struct spc_acb *)); 180 void spc_msgout __P((struct spc_softc *)); 181 int spc_dataout_pio __P((struct spc_softc *, u_char *, int)); 182 int spc_datain_pio __P((struct spc_softc *, u_char *, int)); 183 #if SPC_DEBUG 184 void spc_print_acb __P((struct spc_acb *)); 185 void spc_dump_driver __P((struct spc_softc *)); 186 void spc_dump89352 __P((struct spc_softc *)); 187 void spc_show_scsi_cmd __P((struct spc_acb *)); 188 void spc_print_active_acb __P((void)); 189 #endif 190 191 extern struct cfdriver spc_cd; 192 193 194 /* 195 * INITIALIZATION ROUTINES (probe, attach ++) 196 */ 197 198 /* 199 * Do the real search-for-device. 200 * Prerequisite: sc->sc_iobase should be set to the proper value 201 */ 202 int 203 spc_find(iot, ioh, bdid) 204 bus_space_tag_t iot; 205 bus_space_handle_t ioh; 206 int bdid; 207 { 208 long timeout = SPC_ABORT_TIMEOUT; 209 210 SPC_TRACE(("spc: probing for spc-chip\n")); 211 /* 212 * Disable interrupts then reset the FUJITSU chip. 213 */ 214 bus_space_write_1(iot, ioh, SCTL, SCTL_DISABLE | SCTL_CTRLRST); 215 bus_space_write_1(iot, ioh, SCMD, 0); 216 bus_space_write_1(iot, ioh, PCTL, 0); 217 bus_space_write_1(iot, ioh, TEMP, 0); 218 bus_space_write_1(iot, ioh, TCH, 0); 219 bus_space_write_1(iot, ioh, TCM, 0); 220 bus_space_write_1(iot, ioh, TCL, 0); 221 bus_space_write_1(iot, ioh, INTS, 0); 222 bus_space_write_1(iot, ioh, SCTL, SCTL_DISABLE | SCTL_ABRT_ENAB | SCTL_PARITY_ENAB | SCTL_RESEL_ENAB); 223 bus_space_write_1(iot, ioh, BDID, bdid); 224 delay(400); 225 bus_space_write_1(iot, ioh, SCTL, bus_space_read_1(iot, ioh, SCTL) & ~SCTL_DISABLE); 226 227 /* The following detection is derived from spc.c 228 * (by Takahide Matsutsuka) in FreeBSD/pccard-test. 229 */ 230 while (bus_space_read_1(iot, ioh, PSNS) && timeout) 231 timeout--; 232 if (!timeout) { 233 printf("spc: find failed\n"); 234 return 0; 235 } 236 237 SPC_START(("SPC found")); 238 return 1; 239 } 240 241 void 242 spcattach(sc) 243 struct spc_softc *sc; 244 { 245 246 SPC_TRACE(("spcattach ")); 247 sc->sc_state = SPC_INIT; 248 249 sc->sc_freq = 20; /* XXXX Assume 20 MHz. */ 250 251 #if SPC_USE_SYNCHRONOUS 252 /* 253 * These are the bounds of the sync period, based on the frequency of 254 * the chip's clock input and the size and offset of the sync period 255 * register. 256 * 257 * For a 20Mhz clock, this gives us 25, or 100nS, or 10MB/s, as a 258 * maximum transfer rate, and 112.5, or 450nS, or 2.22MB/s, as a 259 * minimum transfer rate. 260 */ 261 sc->sc_minsync = (2 * 250) / sc->sc_freq; 262 sc->sc_maxsync = (9 * 250) / sc->sc_freq; 263 #endif 264 265 spc_init(sc); /* Init chip and driver */ 266 267 /* 268 * Fill in the adapter. 269 */ 270 sc->sc_adapter.adapt_dev = &sc->sc_dev; 271 sc->sc_adapter.adapt_nchannels = 1; 272 sc->sc_adapter.adapt_openings = 7; 273 sc->sc_adapter.adapt_max_periph = 1; 274 sc->sc_adapter.adapt_minphys = spc_minphys; 275 sc->sc_adapter.adapt_request = spc_scsipi_request; 276 277 sc->sc_channel.chan_adapter = &sc->sc_adapter; 278 sc->sc_channel.chan_bustype = &scsi_bustype; 279 sc->sc_channel.chan_channel = 0; 280 sc->sc_channel.chan_ntargets = 8; 281 sc->sc_channel.chan_nluns = 8; 282 sc->sc_channel.chan_id = sc->sc_initiator; 283 284 /* 285 * ask the adapter what subunits are present 286 */ 287 config_found((struct device*)sc, &sc->sc_channel, scsiprint); 288 } 289 290 /* 291 * Initialize MB89352 chip itself 292 * The following conditions should hold: 293 * spc_isa_probe should have succeeded, i.e. the iobase address in spc_softc 294 * must be valid. 295 */ 296 void 297 spc_reset(sc) 298 struct spc_softc *sc; 299 { 300 bus_space_tag_t iot = sc->sc_iot; 301 bus_space_handle_t ioh = sc->sc_ioh; 302 303 SPC_TRACE(("spc_reset ")); 304 /* 305 * Disable interrupts then reset the FUJITSU chip. 306 */ 307 bus_space_write_1(iot, ioh, SCTL, SCTL_DISABLE | SCTL_CTRLRST); 308 bus_space_write_1(iot, ioh, SCMD, 0); 309 bus_space_write_1(iot, ioh, PCTL, 0); 310 bus_space_write_1(iot, ioh, TEMP, 0); 311 bus_space_write_1(iot, ioh, TCH, 0); 312 bus_space_write_1(iot, ioh, TCM, 0); 313 bus_space_write_1(iot, ioh, TCL, 0); 314 bus_space_write_1(iot, ioh, INTS, 0); 315 bus_space_write_1(iot, ioh, SCTL, SCTL_DISABLE | SCTL_ABRT_ENAB | SCTL_PARITY_ENAB | SCTL_RESEL_ENAB); 316 bus_space_write_1(iot, ioh, BDID, sc->sc_initiator); 317 delay(400); 318 bus_space_write_1(iot, ioh, SCTL, bus_space_read_1(iot, ioh, SCTL) & ~SCTL_DISABLE); 319 } 320 321 322 /* 323 * Pull the SCSI RST line for 500us. 324 */ 325 void 326 spc_scsi_reset(sc) 327 struct spc_softc *sc; 328 { 329 bus_space_tag_t iot = sc->sc_iot; 330 bus_space_handle_t ioh = sc->sc_ioh; 331 332 SPC_TRACE(("spc_scsi_reset ")); 333 bus_space_write_1(iot, ioh, SCMD, bus_space_read_1(iot, ioh, SCMD) | SCMD_RST); 334 delay(500); 335 bus_space_write_1(iot, ioh, SCMD, bus_space_read_1(iot, ioh, SCMD) & ~SCMD_RST); 336 delay(50); 337 } 338 339 /* 340 * Initialize spc SCSI driver. 341 */ 342 void 343 spc_init(sc) 344 struct spc_softc *sc; 345 { 346 struct spc_acb *acb; 347 int r; 348 349 SPC_TRACE(("spc_init ")); 350 spc_reset(sc); 351 spc_scsi_reset(sc); 352 spc_reset(sc); 353 354 if (sc->sc_state == SPC_INIT) { 355 /* First time through; initialize. */ 356 TAILQ_INIT(&sc->ready_list); 357 TAILQ_INIT(&sc->nexus_list); 358 TAILQ_INIT(&sc->free_list); 359 sc->sc_nexus = NULL; 360 acb = sc->sc_acb; 361 memset(acb, 0, sizeof(sc->sc_acb)); 362 for (r = 0; r < sizeof(sc->sc_acb) / sizeof(*acb); r++) { 363 TAILQ_INSERT_TAIL(&sc->free_list, acb, chain); 364 acb++; 365 } 366 memset(&sc->sc_tinfo, 0, sizeof(sc->sc_tinfo)); 367 } else { 368 /* Cancel any active commands. */ 369 sc->sc_state = SPC_CLEANING; 370 if ((acb = sc->sc_nexus) != NULL) { 371 acb->xs->error = XS_DRIVER_STUFFUP; 372 callout_stop(&acb->xs->xs_callout); 373 spc_done(sc, acb); 374 } 375 while ((acb = sc->nexus_list.tqh_first) != NULL) { 376 acb->xs->error = XS_DRIVER_STUFFUP; 377 callout_stop(&acb->xs->xs_callout); 378 spc_done(sc, acb); 379 } 380 } 381 382 sc->sc_prevphase = PH_INVALID; 383 for (r = 0; r < 8; r++) { 384 struct spc_tinfo *ti = &sc->sc_tinfo[r]; 385 386 ti->flags = 0; 387 #if SPC_USE_SYNCHRONOUS 388 ti->flags |= DO_SYNC; 389 ti->period = sc->sc_minsync; 390 ti->offset = SPC_SYNC_REQ_ACK_OFS; 391 #else 392 ti->period = ti->offset = 0; 393 #endif 394 #if SPC_USE_WIDE 395 ti->flags |= DO_WIDE; 396 ti->width = SPC_MAX_WIDTH; 397 #else 398 ti->width = 0; 399 #endif 400 } 401 402 sc->sc_state = SPC_IDLE; 403 bus_space_write_1(sc->sc_iot, sc->sc_ioh, SCTL, 404 bus_space_read_1(sc->sc_iot, sc->sc_ioh, SCTL) | SCTL_INTR_ENAB); 405 } 406 407 void 408 spc_free_acb(sc, acb, flags) 409 struct spc_softc *sc; 410 struct spc_acb *acb; 411 int flags; 412 { 413 int s; 414 415 SPC_TRACE(("spc_free_acb ")); 416 s = splbio(); 417 418 acb->flags = 0; 419 TAILQ_INSERT_HEAD(&sc->free_list, acb, chain); 420 splx(s); 421 } 422 423 struct spc_acb * 424 spc_get_acb(sc) 425 struct spc_softc *sc; 426 { 427 struct spc_acb *acb; 428 int s; 429 430 SPC_TRACE(("spc_get_acb ")); 431 s = splbio(); 432 acb = TAILQ_FIRST(&sc->free_list); 433 if (acb != NULL) { 434 TAILQ_REMOVE(&sc->free_list, acb, chain); 435 acb->flags |= ACB_ALLOC; 436 } 437 splx(s); 438 return acb; 439 } 440 441 /* 442 * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS 443 */ 444 445 /* 446 * Expected sequence: 447 * 1) Command inserted into ready list 448 * 2) Command selected for execution 449 * 3) Command won arbitration and has selected target device 450 * 4) Send message out (identify message, eventually also sync.negotiations) 451 * 5) Send command 452 * 5a) Receive disconnect message, disconnect. 453 * 5b) Reselected by target 454 * 5c) Receive identify message from target. 455 * 6) Send or receive data 456 * 7) Receive status 457 * 8) Receive message (command complete etc.) 458 */ 459 460 /* 461 * Start a SCSI-command 462 * This function is called by the higher level SCSI-driver to queue/run 463 * SCSI-commands. 464 */ 465 void 466 spc_scsipi_request(chan, req, arg) 467 struct scsipi_channel *chan; 468 scsipi_adapter_req_t req; 469 void *arg; 470 { 471 struct scsipi_xfer *xs; 472 struct scsipi_periph *periph; 473 struct spc_softc *sc = (void *)chan->chan_adapter->adapt_dev; 474 struct spc_acb *acb; 475 int s, flags; 476 477 switch (req) { 478 case ADAPTER_REQ_RUN_XFER: 479 xs = arg; 480 periph = xs->xs_periph; 481 SPC_TRACE(("spc_scsipi_request ")); 482 SPC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen, 483 periph->periph_target)); 484 485 flags = xs->xs_control; 486 if ((acb = spc_get_acb(sc)) == NULL) { 487 xs->error = XS_DRIVER_STUFFUP; 488 scsipi_done(xs); 489 return; 490 } 491 492 /* Initialize acb */ 493 acb->xs = xs; 494 acb->timeout = xs->timeout; 495 496 if (xs->xs_control & XS_CTL_RESET) { 497 acb->flags |= ACB_RESET; 498 acb->scsipi_cmd_length = 0; 499 acb->data_length = 0; 500 } else { 501 memcpy(&acb->scsipi_cmd, xs->cmd, xs->cmdlen); 502 #if 1 503 acb->scsipi_cmd.bytes[0] |= periph->periph_lun << 5; /* XXX? */ 504 #endif 505 acb->scsipi_cmd_length = xs->cmdlen; 506 acb->data_addr = xs->data; 507 acb->data_length = xs->datalen; 508 } 509 acb->target_stat = 0; 510 511 s = splbio(); 512 513 TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain); 514 /* 515 * Start scheduling unless a queue process is in progress. 516 */ 517 if (sc->sc_state == SPC_IDLE) 518 spc_sched(sc); 519 /* 520 * After successful sending, check if we should return just now. 521 * If so, return SUCCESSFULLY_QUEUED. 522 */ 523 524 splx(s); 525 526 if ((flags & XS_CTL_POLL) == 0) 527 return; 528 529 /* Not allowed to use interrupts, use polling instead */ 530 s = splbio(); 531 if (spc_poll(sc, xs, acb->timeout)) { 532 spc_timeout(acb); 533 if (spc_poll(sc, xs, acb->timeout)) 534 spc_timeout(acb); 535 } 536 splx(s); 537 return; 538 case ADAPTER_REQ_GROW_RESOURCES: 539 /* XXX Not supported. */ 540 return; 541 case ADAPTER_REQ_SET_XFER_MODE: 542 /* XXX Not supported. */ 543 return; 544 } 545 } 546 547 /* 548 * Adjust transfer size in buffer structure 549 */ 550 void 551 spc_minphys(bp) 552 struct buf *bp; 553 { 554 555 SPC_TRACE(("spc_minphys ")); 556 minphys(bp); 557 } 558 559 /* 560 * Used when interrupt driven I/O isn't allowed, e.g. during boot. 561 */ 562 int 563 spc_poll(sc, xs, count) 564 struct spc_softc *sc; 565 struct scsipi_xfer *xs; 566 int count; 567 { 568 bus_space_tag_t iot = sc->sc_iot; 569 bus_space_handle_t ioh = sc->sc_ioh; 570 571 SPC_TRACE(("spc_poll ")); 572 while (count) { 573 /* 574 * If we had interrupts enabled, would we 575 * have got an interrupt? 576 */ 577 if (bus_space_read_1(iot, ioh, INTS) != 0) 578 spcintr(sc); 579 if ((xs->xs_status & XS_STS_DONE) != 0) 580 return 0; 581 delay(1000); 582 count--; 583 } 584 return 1; 585 } 586 587 /* 588 * LOW LEVEL SCSI UTILITIES 589 */ 590 591 integrate void 592 spc_sched_msgout(sc, m) 593 struct spc_softc *sc; 594 u_char m; 595 { 596 bus_space_tag_t iot = sc->sc_iot; 597 bus_space_handle_t ioh = sc->sc_ioh; 598 599 SPC_TRACE(("spc_sched_msgout ")); 600 if (sc->sc_msgpriq == 0) 601 bus_space_write_1(iot, ioh, SCMD, SCMD_SET_ATN); 602 sc->sc_msgpriq |= m; 603 } 604 605 /* 606 * Set synchronous transfer offset and period. 607 */ 608 integrate void 609 spc_setsync(sc, ti) 610 struct spc_softc *sc; 611 struct spc_tinfo *ti; 612 { 613 #if SPC_USE_SYNCHRONOUS 614 bus_space_tag_t iot = sc->sc_iot; 615 bus_space_handle_t ioh = sc->sc_ioh; 616 617 SPC_TRACE(("spc_setsync ")); 618 if (ti->offset != 0) 619 bus_space_write_1(iot, ioh, TMOD, 620 ((ti->period * sc->sc_freq) / 250 - 2) << 4 | ti->offset); 621 else 622 bus_space_write_1(iot, ioh, TMOD, 0); 623 #endif 624 } 625 626 /* 627 * Start a selection. This is used by spc_sched() to select an idle target. 628 */ 629 void 630 spc_select(sc, acb) 631 struct spc_softc *sc; 632 struct spc_acb *acb; 633 { 634 struct scsipi_periph *periph = acb->xs->xs_periph; 635 int target = periph->periph_target; 636 struct spc_tinfo *ti = &sc->sc_tinfo[target]; 637 bus_space_tag_t iot = sc->sc_iot; 638 bus_space_handle_t ioh = sc->sc_ioh; 639 640 SPC_TRACE(("spc_select ")); 641 spc_setsync(sc, ti); 642 643 #if 0 644 bus_space_write_1(iot, ioh, SCMD, SCMD_SET_ATN); 645 #endif 646 #ifdef x68k /* XXX? */ 647 do { 648 asm ("nop"); 649 } while (bus_space_read_1(iot, ioh, SSTS) & 650 (SSTS_ACTIVE|SSTS_TARGET|SSTS_BUSY)); 651 #endif 652 653 bus_space_write_1(iot, ioh, PCTL, 0); 654 bus_space_write_1(iot, ioh, TEMP, (1 << sc->sc_initiator) | (1 << target)); 655 /* 656 * Setup BSY timeout (selection timeout). 657 * 250ms according to the SCSI specification. 658 * T = (X * 256 + 15) * Tclf * 2 (Tclf = 200ns on x68k) 659 * To setup 256ms timeout, 660 * 128000ns/200ns = X * 256 + 15 661 * 640 - 15 = X * 256 662 * X = 625 / 256 663 * X = 2 + 113 / 256 664 * ==> tch = 2, tcm = 113 (correct?) 665 */ 666 bus_space_write_1(iot, ioh, TCH, 2); 667 bus_space_write_1(iot, ioh, TCM, 113); 668 /* Time to the information transfer phase start. */ 669 bus_space_write_1(iot, ioh, TCL, 3); 670 bus_space_write_1(iot, ioh, SCMD, SCMD_SELECT); 671 672 sc->sc_state = SPC_SELECTING; 673 } 674 675 int 676 spc_reselect(sc, message) 677 struct spc_softc *sc; 678 int message; 679 { 680 u_char selid, target, lun; 681 struct spc_acb *acb; 682 struct scsipi_periph *periph; 683 struct spc_tinfo *ti; 684 685 SPC_TRACE(("spc_reselect ")); 686 /* 687 * The SCSI chip made a snapshot of the data bus while the reselection 688 * was being negotiated. This enables us to determine which target did 689 * the reselect. 690 */ 691 selid = sc->sc_selid & ~(1 << sc->sc_initiator); 692 if (selid & (selid - 1)) { 693 printf("%s: reselect with invalid selid %02x; sending DEVICE RESET\n", 694 sc->sc_dev.dv_xname, selid); 695 SPC_BREAK(); 696 goto reset; 697 } 698 699 /* 700 * Search wait queue for disconnected cmd 701 * The list should be short, so I haven't bothered with 702 * any more sophisticated structures than a simple 703 * singly linked list. 704 */ 705 target = ffs(selid) - 1; 706 lun = message & 0x07; 707 for (acb = sc->nexus_list.tqh_first; acb != NULL; 708 acb = acb->chain.tqe_next) { 709 periph = acb->xs->xs_periph; 710 if (periph->periph_target == target && 711 periph->periph_lun == lun) 712 break; 713 } 714 if (acb == NULL) { 715 printf("%s: reselect from target %d lun %d with no nexus; sending ABORT\n", 716 sc->sc_dev.dv_xname, target, lun); 717 SPC_BREAK(); 718 goto abort; 719 } 720 721 /* Make this nexus active again. */ 722 TAILQ_REMOVE(&sc->nexus_list, acb, chain); 723 sc->sc_state = SPC_CONNECTED; 724 sc->sc_nexus = acb; 725 ti = &sc->sc_tinfo[target]; 726 ti->lubusy |= (1 << lun); 727 spc_setsync(sc, ti); 728 729 if (acb->flags & ACB_RESET) 730 spc_sched_msgout(sc, SEND_DEV_RESET); 731 else if (acb->flags & ACB_ABORT) 732 spc_sched_msgout(sc, SEND_ABORT); 733 734 /* Do an implicit RESTORE POINTERS. */ 735 sc->sc_dp = acb->data_addr; 736 sc->sc_dleft = acb->data_length; 737 sc->sc_cp = (u_char *)&acb->scsipi_cmd; 738 sc->sc_cleft = acb->scsipi_cmd_length; 739 740 return (0); 741 742 reset: 743 spc_sched_msgout(sc, SEND_DEV_RESET); 744 return (1); 745 746 abort: 747 spc_sched_msgout(sc, SEND_ABORT); 748 return (1); 749 } 750 751 /* 752 * Schedule a SCSI operation. This has now been pulled out of the interrupt 753 * handler so that we may call it from spc_scsi_cmd and spc_done. This may 754 * save us an unecessary interrupt just to get things going. Should only be 755 * called when state == SPC_IDLE and at bio pl. 756 */ 757 void 758 spc_sched(sc) 759 struct spc_softc *sc; 760 { 761 struct spc_acb *acb; 762 struct scsipi_periph *periph; 763 struct spc_tinfo *ti; 764 765 /* missing the hw, just return and wait for our hw */ 766 if (sc->sc_flags & SPC_INACTIVE) 767 return; 768 SPC_TRACE(("spc_sched ")); 769 /* 770 * Find first acb in ready queue that is for a target/lunit pair that 771 * is not busy. 772 */ 773 for (acb = sc->ready_list.tqh_first; acb != NULL; 774 acb = acb->chain.tqe_next) { 775 periph = acb->xs->xs_periph; 776 ti = &sc->sc_tinfo[periph->periph_target]; 777 if ((ti->lubusy & (1 << periph->periph_lun)) == 0) { 778 SPC_MISC(("selecting %d:%d ", 779 periph->periph_target, periph->periph_lun)); 780 TAILQ_REMOVE(&sc->ready_list, acb, chain); 781 sc->sc_nexus = acb; 782 spc_select(sc, acb); 783 return; 784 } else 785 SPC_MISC(("%d:%d busy\n", 786 periph->periph_target, periph->periph_lun)); 787 } 788 SPC_MISC(("idle ")); 789 /* Nothing to start; just enable reselections and wait. */ 790 } 791 792 /* 793 * POST PROCESSING OF SCSI_CMD (usually current) 794 */ 795 void 796 spc_done(sc, acb) 797 struct spc_softc *sc; 798 struct spc_acb *acb; 799 { 800 struct scsipi_xfer *xs = acb->xs; 801 struct scsipi_periph *periph = xs->xs_periph; 802 struct spc_tinfo *ti = &sc->sc_tinfo[periph->periph_target]; 803 804 SPC_TRACE(("spc_done ")); 805 806 if (xs->error == XS_NOERROR) { 807 if (acb->flags & ACB_ABORT) { 808 xs->error = XS_DRIVER_STUFFUP; 809 } else { 810 switch (acb->target_stat) { 811 case SCSI_CHECK: 812 /* First, save the return values */ 813 xs->resid = acb->data_length; 814 /* FALLBACK */ 815 case SCSI_BUSY: 816 xs->status = acb->target_stat; 817 xs->error = XS_BUSY; 818 break; 819 case SCSI_OK: 820 xs->resid = acb->data_length; 821 break; 822 default: 823 xs->error = XS_DRIVER_STUFFUP; 824 #if SPC_DEBUG 825 printf("%s: spc_done: bad stat 0x%x\n", 826 sc->sc_dev.dv_xname, acb->target_stat); 827 #endif 828 break; 829 } 830 } 831 } 832 833 #if SPC_DEBUG 834 if ((spc_debug & SPC_SHOWMISC) != 0) { 835 if (xs->resid != 0) 836 printf("resid=%d ", xs->resid); 837 else 838 printf("error=%d\n", xs->error); 839 } 840 #endif 841 842 /* 843 * Remove the ACB from whatever queue it happens to be on. 844 */ 845 if (acb->flags & ACB_NEXUS) 846 ti->lubusy &= ~(1 << periph->periph_lun); 847 if (acb == sc->sc_nexus) { 848 sc->sc_nexus = NULL; 849 sc->sc_state = SPC_IDLE; 850 spc_sched(sc); 851 } else 852 spc_dequeue(sc, acb); 853 854 spc_free_acb(sc, acb, xs->xs_control); 855 ti->cmds++; 856 scsipi_done(xs); 857 } 858 859 void 860 spc_dequeue(sc, acb) 861 struct spc_softc *sc; 862 struct spc_acb *acb; 863 { 864 865 SPC_TRACE(("spc_dequeue ")); 866 if (acb->flags & ACB_NEXUS) { 867 TAILQ_REMOVE(&sc->nexus_list, acb, chain); 868 } else { 869 TAILQ_REMOVE(&sc->ready_list, acb, chain); 870 } 871 } 872 873 /* 874 * INTERRUPT/PROTOCOL ENGINE 875 */ 876 877 /* 878 * Precondition: 879 * The SCSI bus is already in the MSGI phase and there is a message byte 880 * on the bus, along with an asserted REQ signal. 881 */ 882 void 883 spc_msgin(sc) 884 struct spc_softc *sc; 885 { 886 bus_space_tag_t iot = sc->sc_iot; 887 bus_space_handle_t ioh = sc->sc_ioh; 888 int n; 889 890 SPC_TRACE(("spc_msgin ")); 891 892 if (sc->sc_prevphase == PH_MSGIN) { 893 /* This is a continuation of the previous message. */ 894 n = sc->sc_imp - sc->sc_imess; 895 goto nextbyte; 896 } 897 898 /* This is a new MESSAGE IN phase. Clean up our state. */ 899 sc->sc_flags &= ~SPC_DROP_MSGIN; 900 901 nextmsg: 902 n = 0; 903 sc->sc_imp = &sc->sc_imess[n]; 904 905 nextbyte: 906 /* 907 * Read a whole message, but don't ack the last byte. If we reject the 908 * message, we have to assert ATN during the message transfer phase 909 * itself. 910 */ 911 for (;;) { 912 #if 0 913 for (;;) { 914 if ((bus_space_read_1(iot, ioh, PSNS) & PSNS_REQ) != 0) 915 break; 916 /* Wait for REQINIT. XXX Need timeout. */ 917 } 918 #endif 919 if (bus_space_read_1(iot, ioh, INTS) != 0) { 920 /* 921 * Target left MESSAGE IN, probably because it 922 * a) noticed our ATN signal, or 923 * b) ran out of messages. 924 */ 925 goto out; 926 } 927 928 /* If parity error, just dump everything on the floor. */ 929 if ((bus_space_read_1(iot, ioh, SERR) & 930 (SERR_SCSI_PAR|SERR_SPC_PAR)) != 0) { 931 sc->sc_flags |= SPC_DROP_MSGIN; 932 spc_sched_msgout(sc, SEND_PARITY_ERROR); 933 } 934 935 /* send TRANSFER command. */ 936 bus_space_write_1(iot, ioh, TCH, 0); 937 bus_space_write_1(iot, ioh, TCM, 0); 938 bus_space_write_1(iot, ioh, TCL, 1); 939 bus_space_write_1(iot, ioh, PCTL, 940 sc->sc_phase | PCTL_BFINT_ENAB); 941 #ifdef x68k 942 bus_space_write_1(iot, ioh, SCMD, SCMD_XFR); /* | SCMD_PROG_XFR */ 943 #else 944 bus_space_write_1(iot, ioh, SCMD, SCMD_XFR | SCMD_PROG_XFR); /* XXX */ 945 #endif 946 for (;;) { 947 /*if ((bus_space_read_1(iot, ioh, SSTS) & SSTS_BUSY) != 0 948 && (bus_space_read_1(iot, ioh, SSTS) & SSTS_DREG_EMPTY) != 0)*/ 949 if ((bus_space_read_1(iot, ioh, SSTS) & SSTS_DREG_EMPTY) == 0) 950 break; 951 if (bus_space_read_1(iot, ioh, INTS) != 0) 952 goto out; 953 } 954 955 /* Gather incoming message bytes if needed. */ 956 if ((sc->sc_flags & SPC_DROP_MSGIN) == 0) { 957 if (n >= SPC_MAX_MSG_LEN) { 958 (void) bus_space_read_1(iot, ioh, DREG); 959 sc->sc_flags |= SPC_DROP_MSGIN; 960 spc_sched_msgout(sc, SEND_REJECT); 961 } else { 962 *sc->sc_imp++ = bus_space_read_1(iot, ioh, DREG); 963 n++; 964 /* 965 * This testing is suboptimal, but most 966 * messages will be of the one byte variety, so 967 * it should not affect performance 968 * significantly. 969 */ 970 if (n == 1 && MSG_IS1BYTE(sc->sc_imess[0])) 971 break; 972 if (n == 2 && MSG_IS2BYTE(sc->sc_imess[0])) 973 break; 974 if (n >= 3 && MSG_ISEXTENDED(sc->sc_imess[0]) && 975 n == sc->sc_imess[1] + 2) 976 break; 977 } 978 } else 979 (void) bus_space_read_1(iot, ioh, DREG); 980 981 /* 982 * If we reach this spot we're either: 983 * a) in the middle of a multi-byte message, or 984 * b) dropping bytes. 985 */ 986 #if 0 987 /* Ack the last byte read. */ 988 /*(void) bus_space_read_1(iot, ioh, DREG);*/ 989 while ((bus_space_read_1(iot, ioh, PSNS) & ACKI) != 0) 990 ; 991 #endif 992 } 993 994 SPC_MISC(("n=%d imess=0x%02x ", n, sc->sc_imess[0])); 995 996 /* We now have a complete message. Parse it. */ 997 switch (sc->sc_state) { 998 struct spc_acb *acb; 999 struct scsipi_periph *periph; 1000 struct spc_tinfo *ti; 1001 1002 case SPC_CONNECTED: 1003 SPC_ASSERT(sc->sc_nexus != NULL); 1004 acb = sc->sc_nexus; 1005 ti = &sc->sc_tinfo[acb->xs->xs_periph->periph_target]; 1006 1007 switch (sc->sc_imess[0]) { 1008 case MSG_CMDCOMPLETE: 1009 if (sc->sc_dleft < 0) { 1010 periph = acb->xs->xs_periph; 1011 printf("%s: %d extra bytes from %d:%d\n", 1012 sc->sc_dev.dv_xname, -sc->sc_dleft, 1013 periph->periph_target, periph->periph_lun); 1014 acb->data_length = 0; 1015 } 1016 acb->xs->resid = acb->data_length = sc->sc_dleft; 1017 sc->sc_state = SPC_CMDCOMPLETE; 1018 break; 1019 1020 case MSG_PARITY_ERROR: 1021 /* Resend the last message. */ 1022 spc_sched_msgout(sc, sc->sc_lastmsg); 1023 break; 1024 1025 case MSG_MESSAGE_REJECT: 1026 SPC_MISC(("message rejected %02x ", sc->sc_lastmsg)); 1027 switch (sc->sc_lastmsg) { 1028 #if SPC_USE_SYNCHRONOUS + SPC_USE_WIDE 1029 case SEND_IDENTIFY: 1030 ti->flags &= ~(DO_SYNC | DO_WIDE); 1031 ti->period = ti->offset = 0; 1032 spc_setsync(sc, ti); 1033 ti->width = 0; 1034 break; 1035 #endif 1036 #if SPC_USE_SYNCHRONOUS 1037 case SEND_SDTR: 1038 ti->flags &= ~DO_SYNC; 1039 ti->period = ti->offset = 0; 1040 spc_setsync(sc, ti); 1041 break; 1042 #endif 1043 #if SPC_USE_WIDE 1044 case SEND_WDTR: 1045 ti->flags &= ~DO_WIDE; 1046 ti->width = 0; 1047 break; 1048 #endif 1049 case SEND_INIT_DET_ERR: 1050 spc_sched_msgout(sc, SEND_ABORT); 1051 break; 1052 } 1053 break; 1054 1055 case MSG_NOOP: 1056 break; 1057 1058 case MSG_DISCONNECT: 1059 ti->dconns++; 1060 sc->sc_state = SPC_DISCONNECT; 1061 break; 1062 1063 case MSG_SAVEDATAPOINTER: 1064 acb->data_addr = sc->sc_dp; 1065 acb->data_length = sc->sc_dleft; 1066 break; 1067 1068 case MSG_RESTOREPOINTERS: 1069 sc->sc_dp = acb->data_addr; 1070 sc->sc_dleft = acb->data_length; 1071 sc->sc_cp = (u_char *)&acb->scsipi_cmd; 1072 sc->sc_cleft = acb->scsipi_cmd_length; 1073 break; 1074 1075 case MSG_EXTENDED: 1076 switch (sc->sc_imess[2]) { 1077 #if SPC_USE_SYNCHRONOUS 1078 case MSG_EXT_SDTR: 1079 if (sc->sc_imess[1] != 3) 1080 goto reject; 1081 ti->period = sc->sc_imess[3]; 1082 ti->offset = sc->sc_imess[4]; 1083 ti->flags &= ~DO_SYNC; 1084 if (ti->offset == 0) { 1085 } else if (ti->period < sc->sc_minsync || 1086 ti->period > sc->sc_maxsync || 1087 ti->offset > 8) { 1088 ti->period = ti->offset = 0; 1089 spc_sched_msgout(sc, SEND_SDTR); 1090 } else { 1091 scsipi_printaddr(acb->xs->xs_periph); 1092 printf("sync, offset %d, period %dnsec\n", 1093 ti->offset, ti->period * 4); 1094 } 1095 spc_setsync(sc, ti); 1096 break; 1097 #endif 1098 1099 #if SPC_USE_WIDE 1100 case MSG_EXT_WDTR: 1101 if (sc->sc_imess[1] != 2) 1102 goto reject; 1103 ti->width = sc->sc_imess[3]; 1104 ti->flags &= ~DO_WIDE; 1105 if (ti->width == 0) { 1106 } else if (ti->width > SPC_MAX_WIDTH) { 1107 ti->width = 0; 1108 spc_sched_msgout(sc, SEND_WDTR); 1109 } else { 1110 scsipi_printaddr(acb->xs->xs_periph); 1111 printf("wide, width %d\n", 1112 1 << (3 + ti->width)); 1113 } 1114 break; 1115 #endif 1116 1117 default: 1118 printf("%s: unrecognized MESSAGE EXTENDED; sending REJECT\n", 1119 sc->sc_dev.dv_xname); 1120 SPC_BREAK(); 1121 goto reject; 1122 } 1123 break; 1124 1125 default: 1126 printf("%s: unrecognized MESSAGE; sending REJECT\n", 1127 sc->sc_dev.dv_xname); 1128 SPC_BREAK(); 1129 reject: 1130 spc_sched_msgout(sc, SEND_REJECT); 1131 break; 1132 } 1133 break; 1134 1135 case SPC_RESELECTED: 1136 if (!MSG_ISIDENTIFY(sc->sc_imess[0])) { 1137 printf("%s: reselect without IDENTIFY; sending DEVICE RESET\n", 1138 sc->sc_dev.dv_xname); 1139 SPC_BREAK(); 1140 goto reset; 1141 } 1142 1143 (void) spc_reselect(sc, sc->sc_imess[0]); 1144 break; 1145 1146 default: 1147 printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n", 1148 sc->sc_dev.dv_xname); 1149 SPC_BREAK(); 1150 reset: 1151 spc_sched_msgout(sc, SEND_DEV_RESET); 1152 break; 1153 1154 #ifdef notdef 1155 abort: 1156 spc_sched_msgout(sc, SEND_ABORT); 1157 break; 1158 #endif 1159 } 1160 1161 /* Ack the last message byte. */ 1162 #if 0 /* XXX? */ 1163 (void) bus_space_read_1(iot, ioh, DREG); 1164 while ((bus_space_read_1(iot, ioh, PSNS) & ACKI) != 0) 1165 ; 1166 #endif 1167 1168 /* Go get the next message, if any. */ 1169 goto nextmsg; 1170 1171 out: 1172 bus_space_write_1(iot, ioh, SCMD, SCMD_RST_ACK); 1173 SPC_MISC(("n=%d imess=0x%02x ", n, sc->sc_imess[0])); 1174 } 1175 1176 /* 1177 * Send the highest priority, scheduled message. 1178 */ 1179 void 1180 spc_msgout(sc) 1181 struct spc_softc *sc; 1182 { 1183 bus_space_tag_t iot = sc->sc_iot; 1184 bus_space_handle_t ioh = sc->sc_ioh; 1185 #if SPC_USE_SYNCHRONOUS 1186 struct spc_tinfo *ti; 1187 #endif 1188 int n; 1189 1190 SPC_TRACE(("spc_msgout ")); 1191 1192 if (sc->sc_prevphase == PH_MSGOUT) { 1193 if (sc->sc_omp == sc->sc_omess) { 1194 /* 1195 * This is a retransmission. 1196 * 1197 * We get here if the target stayed in MESSAGE OUT 1198 * phase. Section 5.1.9.2 of the SCSI 2 spec indicates 1199 * that all of the previously transmitted messages must 1200 * be sent again, in the same order. Therefore, we 1201 * requeue all the previously transmitted messages, and 1202 * start again from the top. Our simple priority 1203 * scheme keeps the messages in the right order. 1204 */ 1205 SPC_MISC(("retransmitting ")); 1206 sc->sc_msgpriq |= sc->sc_msgoutq; 1207 /* 1208 * Set ATN. If we're just sending a trivial 1-byte 1209 * message, we'll clear ATN later on anyway. 1210 */ 1211 bus_space_write_1(iot, ioh, SCMD, SCMD_SET_ATN); /* XXX? */ 1212 } else { 1213 /* This is a continuation of the previous message. */ 1214 n = sc->sc_omp - sc->sc_omess; 1215 goto nextbyte; 1216 } 1217 } 1218 1219 /* No messages transmitted so far. */ 1220 sc->sc_msgoutq = 0; 1221 sc->sc_lastmsg = 0; 1222 1223 nextmsg: 1224 /* Pick up highest priority message. */ 1225 sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq; 1226 sc->sc_msgpriq &= ~sc->sc_currmsg; 1227 sc->sc_msgoutq |= sc->sc_currmsg; 1228 1229 /* Build the outgoing message data. */ 1230 switch (sc->sc_currmsg) { 1231 case SEND_IDENTIFY: 1232 SPC_ASSERT(sc->sc_nexus != NULL); 1233 sc->sc_omess[0] = 1234 MSG_IDENTIFY(sc->sc_nexus->xs->xs_periph->periph_lun, 1); 1235 n = 1; 1236 break; 1237 1238 #if SPC_USE_SYNCHRONOUS 1239 case SEND_SDTR: 1240 SPC_ASSERT(sc->sc_nexus != NULL); 1241 ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target]; 1242 sc->sc_omess[4] = MSG_EXTENDED; 1243 sc->sc_omess[3] = 3; 1244 sc->sc_omess[2] = MSG_EXT_SDTR; 1245 sc->sc_omess[1] = ti->period >> 2; 1246 sc->sc_omess[0] = ti->offset; 1247 n = 5; 1248 break; 1249 #endif 1250 1251 #if SPC_USE_WIDE 1252 case SEND_WDTR: 1253 SPC_ASSERT(sc->sc_nexus != NULL); 1254 ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target]; 1255 sc->sc_omess[3] = MSG_EXTENDED; 1256 sc->sc_omess[2] = 2; 1257 sc->sc_omess[1] = MSG_EXT_WDTR; 1258 sc->sc_omess[0] = ti->width; 1259 n = 4; 1260 break; 1261 #endif 1262 1263 case SEND_DEV_RESET: 1264 sc->sc_flags |= SPC_ABORTING; 1265 sc->sc_omess[0] = MSG_BUS_DEV_RESET; 1266 n = 1; 1267 break; 1268 1269 case SEND_REJECT: 1270 sc->sc_omess[0] = MSG_MESSAGE_REJECT; 1271 n = 1; 1272 break; 1273 1274 case SEND_PARITY_ERROR: 1275 sc->sc_omess[0] = MSG_PARITY_ERROR; 1276 n = 1; 1277 break; 1278 1279 case SEND_INIT_DET_ERR: 1280 sc->sc_omess[0] = MSG_INITIATOR_DET_ERR; 1281 n = 1; 1282 break; 1283 1284 case SEND_ABORT: 1285 sc->sc_flags |= SPC_ABORTING; 1286 sc->sc_omess[0] = MSG_ABORT; 1287 n = 1; 1288 break; 1289 1290 default: 1291 printf("%s: unexpected MESSAGE OUT; sending NOOP\n", 1292 sc->sc_dev.dv_xname); 1293 SPC_BREAK(); 1294 sc->sc_omess[0] = MSG_NOOP; 1295 n = 1; 1296 break; 1297 } 1298 sc->sc_omp = &sc->sc_omess[n]; 1299 1300 nextbyte: 1301 /* Send message bytes. */ 1302 /* send TRANSFER command. */ 1303 bus_space_write_1(iot, ioh, TCH, n >> 16); 1304 bus_space_write_1(iot, ioh, TCM, n >> 8); 1305 bus_space_write_1(iot, ioh, TCL, n); 1306 bus_space_write_1(iot, ioh, PCTL, sc->sc_phase | PCTL_BFINT_ENAB); 1307 #ifdef x68k 1308 bus_space_write_1(iot, ioh, SCMD, SCMD_XFR); /* XXX */ 1309 #else 1310 bus_space_write_1(iot, ioh, SCMD, SCMD_XFR | SCMD_PROG_XFR | SCMD_ICPT_XFR); 1311 #endif 1312 for (;;) { 1313 if ((bus_space_read_1(iot, ioh, SSTS) & SSTS_BUSY) != 0) 1314 break; 1315 if (bus_space_read_1(iot, ioh, INTS) != 0) 1316 goto out; 1317 } 1318 for (;;) { 1319 #if 0 1320 for (;;) { 1321 if ((bus_space_read_1(iot, ioh, PSNS) & PSNS_REQ) != 0) 1322 break; 1323 /* Wait for REQINIT. XXX Need timeout. */ 1324 } 1325 #endif 1326 if (bus_space_read_1(iot, ioh, INTS) != 0) { 1327 /* 1328 * Target left MESSAGE OUT, possibly to reject 1329 * our message. 1330 * 1331 * If this is the last message being sent, then we 1332 * deassert ATN, since either the target is going to 1333 * ignore this message, or it's going to ask for a 1334 * retransmission via MESSAGE PARITY ERROR (in which 1335 * case we reassert ATN anyway). 1336 */ 1337 #if 0 1338 if (sc->sc_msgpriq == 0) 1339 bus_space_write_1(iot, ioh, SCMD, SCMD_RST_ATN); 1340 #endif 1341 goto out; 1342 } 1343 1344 #if 0 1345 /* Clear ATN before last byte if this is the last message. */ 1346 if (n == 1 && sc->sc_msgpriq == 0) 1347 bus_space_write_1(iot, ioh, SCMD, SCMD_RST_ATN); 1348 #endif 1349 1350 while ((bus_space_read_1(iot, ioh, SSTS) & SSTS_DREG_FULL) != 0) 1351 ; 1352 /* Send message byte. */ 1353 bus_space_write_1(iot, ioh, DREG, *--sc->sc_omp); 1354 --n; 1355 /* Keep track of the last message we've sent any bytes of. */ 1356 sc->sc_lastmsg = sc->sc_currmsg; 1357 #if 0 1358 /* Wait for ACK to be negated. XXX Need timeout. */ 1359 while ((bus_space_read_1(iot, ioh, PSNS) & ACKI) != 0) 1360 ; 1361 #endif 1362 1363 if (n == 0) 1364 break; 1365 } 1366 1367 /* We get here only if the entire message has been transmitted. */ 1368 if (sc->sc_msgpriq != 0) { 1369 /* There are more outgoing messages. */ 1370 goto nextmsg; 1371 } 1372 1373 /* 1374 * The last message has been transmitted. We need to remember the last 1375 * message transmitted (in case the target switches to MESSAGE IN phase 1376 * and sends a MESSAGE REJECT), and the list of messages transmitted 1377 * this time around (in case the target stays in MESSAGE OUT phase to 1378 * request a retransmit). 1379 */ 1380 1381 out: 1382 /* Disable REQ/ACK protocol. */ 1383 } 1384 1385 /* 1386 * spc_dataout_pio: perform a data transfer using the FIFO datapath in the spc 1387 * Precondition: The SCSI bus should be in the DOUT phase, with REQ asserted 1388 * and ACK deasserted (i.e. waiting for a data byte) 1389 * 1390 * This new revision has been optimized (I tried) to make the common case fast, 1391 * and the rarer cases (as a result) somewhat more comlex 1392 */ 1393 int 1394 spc_dataout_pio(sc, p, n) 1395 struct spc_softc *sc; 1396 u_char *p; 1397 int n; 1398 { 1399 bus_space_tag_t iot = sc->sc_iot; 1400 bus_space_handle_t ioh = sc->sc_ioh; 1401 u_char intstat = 0; 1402 int out = 0; 1403 #define DOUTAMOUNT 8 /* Full FIFO */ 1404 1405 SPC_TRACE(("spc_dataout_pio ")); 1406 /* send TRANSFER command. */ 1407 bus_space_write_1(iot, ioh, TCH, n >> 16); 1408 bus_space_write_1(iot, ioh, TCM, n >> 8); 1409 bus_space_write_1(iot, ioh, TCL, n); 1410 bus_space_write_1(iot, ioh, PCTL, sc->sc_phase | PCTL_BFINT_ENAB); 1411 #ifdef x68k 1412 bus_space_write_1(iot, ioh, SCMD, SCMD_XFR); /* XXX */ 1413 #else 1414 bus_space_write_1(iot, ioh, SCMD, SCMD_XFR | SCMD_PROG_XFR | SCMD_ICPT_XFR); /* XXX */ 1415 #endif 1416 for (;;) { 1417 if ((bus_space_read_1(iot, ioh, SSTS) & SSTS_BUSY) != 0) 1418 break; 1419 if (bus_space_read_1(iot, ioh, INTS) != 0) 1420 break; 1421 } 1422 1423 /* 1424 * I have tried to make the main loop as tight as possible. This 1425 * means that some of the code following the loop is a bit more 1426 * complex than otherwise. 1427 */ 1428 while (n > 0) { 1429 int xfer; 1430 1431 for (;;) { 1432 intstat = bus_space_read_1(iot, ioh, INTS); 1433 /* Wait till buffer is empty. */ 1434 if ((bus_space_read_1(iot, ioh, SSTS) & SSTS_DREG_EMPTY) != 0) 1435 break; 1436 /* Break on interrupt. */ 1437 if (intstat != 0) 1438 goto phasechange; 1439 } 1440 1441 xfer = min(DOUTAMOUNT, n); 1442 1443 SPC_MISC(("%d> ", xfer)); 1444 1445 n -= xfer; 1446 out += xfer; 1447 1448 while (xfer-- > 0) { 1449 bus_space_write_1(iot, ioh, DREG, *p++); 1450 } 1451 } 1452 1453 if (out == 0) { 1454 for (;;) { 1455 if (bus_space_read_1(iot, ioh, INTS) != 0) 1456 break; 1457 } 1458 SPC_MISC(("extra data ")); 1459 } else { 1460 /* See the bytes off chip */ 1461 for (;;) { 1462 /* Wait till buffer is empty. */ 1463 if ((bus_space_read_1(iot, ioh, SSTS) & SSTS_DREG_EMPTY) != 0) 1464 break; 1465 intstat = bus_space_read_1(iot, ioh, INTS); 1466 /* Break on interrupt. */ 1467 if (intstat != 0) 1468 goto phasechange; 1469 } 1470 } 1471 1472 phasechange: 1473 /* Stop the FIFO data path. */ 1474 1475 if (intstat != 0) { 1476 /* Some sort of phase change. */ 1477 int amount; 1478 1479 amount = ((bus_space_read_1(iot, ioh, TCH) << 16) | 1480 (bus_space_read_1(iot, ioh, TCM) << 8) | 1481 bus_space_read_1(iot, ioh, TCL)); 1482 if (amount > 0) { 1483 out -= amount; 1484 SPC_MISC(("+%d ", amount)); 1485 } 1486 } 1487 1488 /* Turn on ENREQINIT again. */ 1489 1490 return out; 1491 } 1492 1493 /* 1494 * spc_datain_pio: perform data transfers using the FIFO datapath in the spc 1495 * Precondition: The SCSI bus should be in the DIN phase, with REQ asserted 1496 * and ACK deasserted (i.e. at least one byte is ready). 1497 * 1498 * For now, uses a pretty dumb algorithm, hangs around until all data has been 1499 * transferred. This, is OK for fast targets, but not so smart for slow 1500 * targets which don't disconnect or for huge transfers. 1501 */ 1502 int 1503 spc_datain_pio(sc, p, n) 1504 struct spc_softc *sc; 1505 u_char *p; 1506 int n; 1507 { 1508 bus_space_tag_t iot = sc->sc_iot; 1509 bus_space_handle_t ioh = sc->sc_ioh; 1510 u_short intstat; 1511 int in = 0; 1512 #define DINAMOUNT 8 /* Full FIFO */ 1513 1514 SPC_TRACE(("spc_datain_pio ")); 1515 /* send TRANSFER command. */ 1516 bus_space_write_1(iot, ioh, TCH, n >> 16); 1517 bus_space_write_1(iot, ioh, TCM, n >> 8); 1518 bus_space_write_1(iot, ioh, TCL, n); 1519 bus_space_write_1(iot, ioh, PCTL, sc->sc_phase | PCTL_BFINT_ENAB); 1520 #ifdef x68k 1521 bus_space_write_1(iot, ioh, SCMD, SCMD_XFR); /* XXX */ 1522 #else 1523 bus_space_write_1(iot, ioh, SCMD, SCMD_XFR | SCMD_PROG_XFR); /* XXX */ 1524 #endif 1525 for (;;) { 1526 if ((bus_space_read_1(iot, ioh, SSTS) & SSTS_BUSY) != 0) 1527 break; 1528 if (bus_space_read_1(iot, ioh, INTS) != 0) 1529 goto phasechange; 1530 } 1531 1532 /* 1533 * We leave this loop if one or more of the following is true: 1534 * a) phase != PH_DATAIN && FIFOs are empty 1535 * b) reset has occurred or busfree is detected. 1536 */ 1537 while (n > 0) { 1538 int xfer; 1539 1540 #define INTSMASK 0xff 1541 /* Wait for fifo half full or phase mismatch */ 1542 for (;;) { 1543 intstat = ((bus_space_read_1(iot, ioh, SSTS) << 8) | 1544 bus_space_read_1(iot, ioh, INTS)); 1545 if ((intstat & (INTSMASK | (SSTS_DREG_FULL << 8))) != 1546 0) 1547 break; 1548 if ((intstat & (SSTS_DREG_EMPTY << 8)) == 0) 1549 break; 1550 } 1551 1552 #if 1 1553 if ((intstat & INTSMASK) != 0) 1554 goto phasechange; 1555 #else 1556 if ((intstat & INTSMASK) != 0 && 1557 (intstat & (SSTS_DREG_EMPTY << 8))) 1558 goto phasechange; 1559 #endif 1560 if ((intstat & (SSTS_DREG_FULL << 8)) != 0) 1561 xfer = min(DINAMOUNT, n); 1562 else 1563 xfer = min(1, n); 1564 1565 SPC_MISC((">%d ", xfer)); 1566 1567 n -= xfer; 1568 in += xfer; 1569 1570 while (xfer-- > 0) { 1571 *p++ = bus_space_read_1(iot, ioh, DREG); 1572 } 1573 1574 if ((intstat & INTSMASK) != 0) 1575 goto phasechange; 1576 } 1577 1578 /* 1579 * Some SCSI-devices are rude enough to transfer more data than what 1580 * was requested, e.g. 2048 bytes from a CD-ROM instead of the 1581 * requested 512. Test for progress, i.e. real transfers. If no real 1582 * transfers have been performed (n is probably already zero) and the 1583 * FIFO is not empty, waste some bytes.... 1584 */ 1585 if (in == 0) { 1586 for (;;) { 1587 if (bus_space_read_1(iot, ioh, INTS) != 0) 1588 break; 1589 } 1590 SPC_MISC(("extra data ")); 1591 } 1592 1593 phasechange: 1594 /* Stop the FIFO data path. */ 1595 1596 /* Turn on ENREQINIT again. */ 1597 1598 return in; 1599 } 1600 1601 /* 1602 * Catch an interrupt from the adaptor 1603 */ 1604 /* 1605 * This is the workhorse routine of the driver. 1606 * Deficiencies (for now): 1607 * 1) always uses programmed I/O 1608 */ 1609 int 1610 spcintr(arg) 1611 void *arg; 1612 { 1613 struct spc_softc *sc = arg; 1614 bus_space_tag_t iot = sc->sc_iot; 1615 bus_space_handle_t ioh = sc->sc_ioh; 1616 u_char ints; 1617 struct spc_acb *acb; 1618 struct scsipi_periph *periph; 1619 struct spc_tinfo *ti; 1620 int n; 1621 1622 /* 1623 * Disable interrupt. 1624 */ 1625 bus_space_write_1(iot, ioh, SCTL, bus_space_read_1(iot, ioh, SCTL) & ~SCTL_INTR_ENAB); 1626 1627 SPC_TRACE(("spcintr ")); 1628 1629 loop: 1630 /* 1631 * Loop until transfer completion. 1632 */ 1633 /* 1634 * First check for abnormal conditions, such as reset. 1635 */ 1636 #ifdef x68k /* XXX? */ 1637 while ((ints = bus_space_read_1(iot, ioh, INTS)) == 0) 1638 delay(1); 1639 SPC_MISC(("ints = 0x%x ", ints)); 1640 #else 1641 ints = bus_space_read_1(iot, ioh, INTS); 1642 SPC_MISC(("ints = 0x%x ", ints)); 1643 #endif 1644 1645 if ((ints & INTS_RST) != 0) { 1646 printf("%s: SCSI bus reset\n", sc->sc_dev.dv_xname); 1647 goto reset; 1648 } 1649 1650 /* 1651 * Check for less serious errors. 1652 */ 1653 if ((bus_space_read_1(iot, ioh, SERR) & (SERR_SCSI_PAR|SERR_SPC_PAR)) != 0) { 1654 printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname); 1655 if (sc->sc_prevphase == PH_MSGIN) { 1656 sc->sc_flags |= SPC_DROP_MSGIN; 1657 spc_sched_msgout(sc, SEND_PARITY_ERROR); 1658 } else 1659 spc_sched_msgout(sc, SEND_INIT_DET_ERR); 1660 } 1661 1662 /* 1663 * If we're not already busy doing something test for the following 1664 * conditions: 1665 * 1) We have been reselected by something 1666 * 2) We have selected something successfully 1667 * 3) Our selection process has timed out 1668 * 4) This is really a bus free interrupt just to get a new command 1669 * going? 1670 * 5) Spurious interrupt? 1671 */ 1672 switch (sc->sc_state) { 1673 case SPC_IDLE: 1674 case SPC_SELECTING: 1675 SPC_MISC(("ints:0x%02x ", ints)); 1676 1677 if ((ints & INTS_SEL) != 0) { 1678 /* 1679 * We don't currently support target mode. 1680 */ 1681 printf("%s: target mode selected; going to BUS FREE\n", 1682 sc->sc_dev.dv_xname); 1683 1684 goto sched; 1685 } else if ((ints & INTS_RESEL) != 0) { 1686 SPC_MISC(("reselected ")); 1687 1688 /* 1689 * If we're trying to select a target ourselves, 1690 * push our command back into the ready list. 1691 */ 1692 if (sc->sc_state == SPC_SELECTING) { 1693 SPC_MISC(("backoff selector ")); 1694 SPC_ASSERT(sc->sc_nexus != NULL); 1695 acb = sc->sc_nexus; 1696 sc->sc_nexus = NULL; 1697 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain); 1698 } 1699 1700 /* Save reselection ID. */ 1701 sc->sc_selid = bus_space_read_1(iot, ioh, TEMP); 1702 1703 sc->sc_state = SPC_RESELECTED; 1704 } else if ((ints & INTS_CMD_DONE) != 0) { 1705 SPC_MISC(("selected ")); 1706 1707 /* 1708 * We have selected a target. Things to do: 1709 * a) Determine what message(s) to send. 1710 * b) Verify that we're still selecting the target. 1711 * c) Mark device as busy. 1712 */ 1713 if (sc->sc_state != SPC_SELECTING) { 1714 printf("%s: selection out while idle; resetting\n", 1715 sc->sc_dev.dv_xname); 1716 SPC_BREAK(); 1717 goto reset; 1718 } 1719 SPC_ASSERT(sc->sc_nexus != NULL); 1720 acb = sc->sc_nexus; 1721 periph = acb->xs->xs_periph; 1722 ti = &sc->sc_tinfo[periph->periph_target]; 1723 1724 sc->sc_msgpriq = SEND_IDENTIFY; 1725 if (acb->flags & ACB_RESET) 1726 sc->sc_msgpriq |= SEND_DEV_RESET; 1727 else if (acb->flags & ACB_ABORT) 1728 sc->sc_msgpriq |= SEND_ABORT; 1729 else { 1730 #if SPC_USE_SYNCHRONOUS 1731 if ((ti->flags & DO_SYNC) != 0) 1732 sc->sc_msgpriq |= SEND_SDTR; 1733 #endif 1734 #if SPC_USE_WIDE 1735 if ((ti->flags & DO_WIDE) != 0) 1736 sc->sc_msgpriq |= SEND_WDTR; 1737 #endif 1738 } 1739 1740 acb->flags |= ACB_NEXUS; 1741 ti->lubusy |= (1 << periph->periph_lun); 1742 1743 /* Do an implicit RESTORE POINTERS. */ 1744 sc->sc_dp = acb->data_addr; 1745 sc->sc_dleft = acb->data_length; 1746 sc->sc_cp = (u_char *)&acb->scsipi_cmd; 1747 sc->sc_cleft = acb->scsipi_cmd_length; 1748 1749 /* On our first connection, schedule a timeout. */ 1750 if ((acb->xs->xs_control & XS_CTL_POLL) == 0) 1751 callout_reset(&acb->xs->xs_callout, 1752 mstohz(acb->timeout), spc_timeout, acb); 1753 1754 sc->sc_state = SPC_CONNECTED; 1755 } else if ((ints & INTS_TIMEOUT) != 0) { 1756 SPC_MISC(("selection timeout ")); 1757 1758 if (sc->sc_state != SPC_SELECTING) { 1759 printf("%s: selection timeout while idle; resetting\n", 1760 sc->sc_dev.dv_xname); 1761 SPC_BREAK(); 1762 goto reset; 1763 } 1764 SPC_ASSERT(sc->sc_nexus != NULL); 1765 acb = sc->sc_nexus; 1766 1767 delay(250); 1768 1769 acb->xs->error = XS_SELTIMEOUT; 1770 goto finish; 1771 } else { 1772 if (sc->sc_state != SPC_IDLE) { 1773 printf("%s: BUS FREE while not idle; state=%d\n", 1774 sc->sc_dev.dv_xname, sc->sc_state); 1775 SPC_BREAK(); 1776 goto out; 1777 } 1778 1779 goto sched; 1780 } 1781 1782 /* 1783 * Turn off selection stuff, and prepare to catch bus free 1784 * interrupts, parity errors, and phase changes. 1785 */ 1786 1787 sc->sc_flags = 0; 1788 sc->sc_prevphase = PH_INVALID; 1789 goto dophase; 1790 } 1791 1792 if ((ints & INTS_DISCON) != 0) { 1793 /* We've gone to BUS FREE phase. */ 1794 bus_space_write_1(iot, ioh, PCTL, 1795 bus_space_read_1(iot, ioh, PCTL) & ~PCTL_BFINT_ENAB); 1796 /* disable disconnect interrupt */ 1797 bus_space_write_1(iot, ioh, INTS, ints); 1798 /* XXX reset interrput */ 1799 1800 switch (sc->sc_state) { 1801 case SPC_RESELECTED: 1802 goto sched; 1803 1804 case SPC_CONNECTED: 1805 SPC_ASSERT(sc->sc_nexus != NULL); 1806 acb = sc->sc_nexus; 1807 1808 #if SPC_USE_SYNCHRONOUS + SPC_USE_WIDE 1809 if (sc->sc_prevphase == PH_MSGOUT) { 1810 /* 1811 * If the target went to BUS FREE phase during 1812 * or immediately after sending a SDTR or WDTR 1813 * message, disable negotiation. 1814 */ 1815 periph = acb->xs->xs_periph; 1816 ti = &sc->sc_tinfo[periph->periph_target]; 1817 switch (sc->sc_lastmsg) { 1818 #if SPC_USE_SYNCHRONOUS 1819 case SEND_SDTR: 1820 ti->flags &= ~DO_SYNC; 1821 ti->period = ti->offset = 0; 1822 break; 1823 #endif 1824 #if SPC_USE_WIDE 1825 case SEND_WDTR: 1826 ti->flags &= ~DO_WIDE; 1827 ti->width = 0; 1828 break; 1829 #endif 1830 } 1831 } 1832 #endif 1833 1834 if ((sc->sc_flags & SPC_ABORTING) == 0) { 1835 /* 1836 * Section 5.1.1 of the SCSI 2 spec suggests 1837 * issuing a REQUEST SENSE following an 1838 * unexpected disconnect. Some devices go into 1839 * a contingent allegiance condition when 1840 * disconnecting, and this is necessary to 1841 * clean up their state. 1842 */ 1843 printf("%s: unexpected disconnect; sending REQUEST SENSE\n", 1844 sc->sc_dev.dv_xname); 1845 SPC_BREAK(); 1846 acb->target_stat = SCSI_CHECK; 1847 acb->xs->error = XS_NOERROR; 1848 goto finish; 1849 } 1850 1851 acb->xs->error = XS_DRIVER_STUFFUP; 1852 goto finish; 1853 1854 case SPC_DISCONNECT: 1855 SPC_ASSERT(sc->sc_nexus != NULL); 1856 acb = sc->sc_nexus; 1857 TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain); 1858 sc->sc_nexus = NULL; 1859 goto sched; 1860 1861 case SPC_CMDCOMPLETE: 1862 SPC_ASSERT(sc->sc_nexus != NULL); 1863 acb = sc->sc_nexus; 1864 goto finish; 1865 } 1866 } 1867 else if ((ints & INTS_CMD_DONE) != 0 && 1868 sc->sc_prevphase == PH_MSGIN && sc->sc_state != SPC_CONNECTED) 1869 goto out; 1870 1871 dophase: 1872 #if 0 1873 if ((bus_space_read_1(iot, ioh, PSNS) & PSNS_REQ) == 0) { 1874 /* Wait for REQINIT. */ 1875 goto out; 1876 } 1877 #else 1878 bus_space_write_1(iot, ioh, INTS, ints); 1879 ints = 0; 1880 while ((bus_space_read_1(iot, ioh, PSNS) & PSNS_REQ) == 0) 1881 delay(1); /* need timeout XXX */ 1882 #endif 1883 1884 /* 1885 * State transition. 1886 */ 1887 sc->sc_phase = bus_space_read_1(iot, ioh, PSNS) & PH_MASK; 1888 /* bus_space_write_1(iot, ioh, PCTL, sc->sc_phase);*/ 1889 1890 SPC_MISC(("phase=%d\n", sc->sc_phase)); 1891 switch (sc->sc_phase) { 1892 case PH_MSGOUT: 1893 if (sc->sc_state != SPC_CONNECTED && 1894 sc->sc_state != SPC_RESELECTED) 1895 break; 1896 spc_msgout(sc); 1897 sc->sc_prevphase = PH_MSGOUT; 1898 goto loop; 1899 1900 case PH_MSGIN: 1901 if (sc->sc_state != SPC_CONNECTED && 1902 sc->sc_state != SPC_RESELECTED) 1903 break; 1904 spc_msgin(sc); 1905 sc->sc_prevphase = PH_MSGIN; 1906 goto loop; 1907 1908 case PH_CMD: 1909 if (sc->sc_state != SPC_CONNECTED) 1910 break; 1911 #if SPC_DEBUG 1912 if ((spc_debug & SPC_SHOWMISC) != 0) { 1913 SPC_ASSERT(sc->sc_nexus != NULL); 1914 acb = sc->sc_nexus; 1915 printf("cmd=0x%02x+%d ", 1916 acb->scsipi_cmd.opcode, acb->scsipi_cmd_length-1); 1917 } 1918 #endif 1919 n = spc_dataout_pio(sc, sc->sc_cp, sc->sc_cleft); 1920 sc->sc_cp += n; 1921 sc->sc_cleft -= n; 1922 sc->sc_prevphase = PH_CMD; 1923 goto loop; 1924 1925 case PH_DATAOUT: 1926 if (sc->sc_state != SPC_CONNECTED) 1927 break; 1928 SPC_MISC(("dataout dleft=%d ", sc->sc_dleft)); 1929 n = spc_dataout_pio(sc, sc->sc_dp, sc->sc_dleft); 1930 sc->sc_dp += n; 1931 sc->sc_dleft -= n; 1932 sc->sc_prevphase = PH_DATAOUT; 1933 goto loop; 1934 1935 case PH_DATAIN: 1936 if (sc->sc_state != SPC_CONNECTED) 1937 break; 1938 SPC_MISC(("datain ")); 1939 n = spc_datain_pio(sc, sc->sc_dp, sc->sc_dleft); 1940 sc->sc_dp += n; 1941 sc->sc_dleft -= n; 1942 sc->sc_prevphase = PH_DATAIN; 1943 goto loop; 1944 1945 case PH_STAT: 1946 if (sc->sc_state != SPC_CONNECTED) 1947 break; 1948 SPC_ASSERT(sc->sc_nexus != NULL); 1949 acb = sc->sc_nexus; 1950 /*acb->target_stat = bus_space_read_1(iot, ioh, DREG);*/ 1951 spc_datain_pio(sc, &acb->target_stat, 1); 1952 SPC_MISC(("target_stat=0x%02x ", acb->target_stat)); 1953 sc->sc_prevphase = PH_STAT; 1954 goto loop; 1955 } 1956 1957 printf("%s: unexpected bus phase; resetting\n", sc->sc_dev.dv_xname); 1958 SPC_BREAK(); 1959 reset: 1960 spc_init(sc); 1961 return 1; 1962 1963 finish: 1964 callout_stop(&acb->xs->xs_callout); 1965 bus_space_write_1(iot, ioh, INTS, ints); 1966 ints = 0; 1967 spc_done(sc, acb); 1968 goto out; 1969 1970 sched: 1971 sc->sc_state = SPC_IDLE; 1972 spc_sched(sc); 1973 goto out; 1974 1975 out: 1976 if (ints) 1977 bus_space_write_1(iot, ioh, INTS, ints); 1978 bus_space_write_1(iot, ioh, SCTL, 1979 bus_space_read_1(iot, ioh, SCTL) | SCTL_INTR_ENAB); 1980 return 1; 1981 } 1982 1983 void 1984 spc_abort(sc, acb) 1985 struct spc_softc *sc; 1986 struct spc_acb *acb; 1987 { 1988 1989 /* 2 secs for the abort */ 1990 acb->timeout = SPC_ABORT_TIMEOUT; 1991 acb->flags |= ACB_ABORT; 1992 1993 if (acb == sc->sc_nexus) { 1994 /* 1995 * If we're still selecting, the message will be scheduled 1996 * after selection is complete. 1997 */ 1998 if (sc->sc_state == SPC_CONNECTED) 1999 spc_sched_msgout(sc, SEND_ABORT); 2000 } else { 2001 spc_dequeue(sc, acb); 2002 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain); 2003 if (sc->sc_state == SPC_IDLE) 2004 spc_sched(sc); 2005 } 2006 } 2007 2008 void 2009 spc_timeout(arg) 2010 void *arg; 2011 { 2012 struct spc_acb *acb = arg; 2013 struct scsipi_xfer *xs = acb->xs; 2014 struct scsipi_periph *periph = xs->xs_periph; 2015 struct spc_softc *sc = (void*)periph->periph_channel->chan_adapter->adapt_dev; 2016 int s; 2017 2018 scsipi_printaddr(periph); 2019 printf("timed out"); 2020 2021 s = splbio(); 2022 2023 if (acb->flags & ACB_ABORT) { 2024 /* abort timed out */ 2025 printf(" AGAIN\n"); 2026 /* XXX Must reset! */ 2027 } else { 2028 /* abort the operation that has timed out */ 2029 printf("\n"); 2030 acb->xs->error = XS_TIMEOUT; 2031 spc_abort(sc, acb); 2032 } 2033 2034 splx(s); 2035 } 2036 2037 #ifdef SPC_DEBUG 2038 /* 2039 * The following functions are mostly used for debugging purposes, either 2040 * directly called from the driver or from the kernel debugger. 2041 */ 2042 2043 void 2044 spc_show_scsi_cmd(acb) 2045 struct spc_acb *acb; 2046 { 2047 u_char *b = (u_char *)&acb->scsipi_cmd; 2048 int i; 2049 2050 scsipi_printaddr(acb->xs->xs_periph); 2051 if ((acb->xs->xs_control & XS_CTL_RESET) == 0) { 2052 for (i = 0; i < acb->scsipi_cmd_length; i++) { 2053 if (i) 2054 printf(","); 2055 printf("%x", b[i]); 2056 } 2057 printf("\n"); 2058 } else 2059 printf("RESET\n"); 2060 } 2061 2062 void 2063 spc_print_acb(acb) 2064 struct spc_acb *acb; 2065 { 2066 2067 printf("acb@%p xs=%p flags=%x", acb, acb->xs, acb->flags); 2068 printf(" dp=%p dleft=%d target_stat=%x\n", 2069 acb->data_addr, acb->data_length, acb->target_stat); 2070 spc_show_scsi_cmd(acb); 2071 } 2072 2073 void 2074 spc_print_active_acb() 2075 { 2076 struct spc_acb *acb; 2077 struct spc_softc *sc = spc_cd.cd_devs[0]; /* XXX */ 2078 2079 printf("ready list:\n"); 2080 for (acb = sc->ready_list.tqh_first; acb != NULL; 2081 acb = acb->chain.tqe_next) 2082 spc_print_acb(acb); 2083 printf("nexus:\n"); 2084 if (sc->sc_nexus != NULL) 2085 spc_print_acb(sc->sc_nexus); 2086 printf("nexus list:\n"); 2087 for (acb = sc->nexus_list.tqh_first; acb != NULL; 2088 acb = acb->chain.tqe_next) 2089 spc_print_acb(acb); 2090 } 2091 2092 void 2093 spc_dump89352(sc) 2094 struct spc_softc *sc; 2095 { 2096 bus_space_tag_t iot = sc->sc_iot; 2097 bus_space_handle_t ioh = sc->sc_ioh; 2098 2099 printf("mb89352: BDID=%x SCTL=%x SCMD=%x TMOD=%x\n", 2100 bus_space_read_1(iot, ioh, BDID), 2101 bus_space_read_1(iot, ioh, SCTL), 2102 bus_space_read_1(iot, ioh, SCMD), 2103 bus_space_read_1(iot, ioh, TMOD)); 2104 printf(" INTS=%x PSNS=%x SSTS=%x SERR=%x PCTL=%x\n", 2105 bus_space_read_1(iot, ioh, INTS), 2106 bus_space_read_1(iot, ioh, PSNS), 2107 bus_space_read_1(iot, ioh, SSTS), 2108 bus_space_read_1(iot, ioh, SERR), 2109 bus_space_read_1(iot, ioh, PCTL)); 2110 printf(" MBC=%x DREG=%x TEMP=%x TCH=%x TCM=%x\n", 2111 bus_space_read_1(iot, ioh, MBC), 2112 #if 0 2113 bus_space_read_1(iot, ioh, DREG), 2114 #else 2115 0, 2116 #endif 2117 bus_space_read_1(iot, ioh, TEMP), 2118 bus_space_read_1(iot, ioh, TCH), 2119 bus_space_read_1(iot, ioh, TCM)); 2120 printf(" TCL=%x EXBF=%x\n", 2121 bus_space_read_1(iot, ioh, TCL), 2122 bus_space_read_1(iot, ioh, EXBF)); 2123 } 2124 2125 void 2126 spc_dump_driver(sc) 2127 struct spc_softc *sc; 2128 { 2129 struct spc_tinfo *ti; 2130 int i; 2131 2132 printf("nexus=%p prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase); 2133 printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x currmsg=%x\n", 2134 sc->sc_state, sc->sc_imess[0], 2135 sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg); 2136 for (i = 0; i < 7; i++) { 2137 ti = &sc->sc_tinfo[i]; 2138 printf("tinfo%d: %d cmds %d disconnects %d timeouts", 2139 i, ti->cmds, ti->dconns, ti->touts); 2140 printf(" %d senses flags=%x\n", ti->senses, ti->flags); 2141 } 2142 } 2143 #endif 2144