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