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