1 /* $NetBSD: aic6360.c,v 1.20 1994/11/30 02:08:01 mycroft Exp $ */ 2 3 /* 4 * Copyright (c) 1994 Charles Hannum. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Charles Hannum. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * Copyright (c) 1994 Jarle Greipsland 21 * All rights reserved. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the above copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. The name of the author may not be used to endorse or promote products 32 * derived from this software without specific prior written permission. 33 * 34 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 35 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 36 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 37 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 38 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 39 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 40 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 42 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 43 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 44 * POSSIBILITY OF SUCH DAMAGE. 45 */ 46 47 /* 48 * Acknowledgements: Many of the algorithms used in this driver are 49 * inspired by the work of Julian Elischer (julian@tfs.com) and 50 * Charles Hannum (mycroft@duality.gnu.ai.mit.edu). Thanks a million! 51 */ 52 53 /* TODO list: 54 * 1) Get the DMA stuff working. 55 * 2) Get the iov/uio stuff working. Is this a good thing ??? 56 * 3) Get the synch stuff working. 57 * 4) Rewrite it to use malloc for the acb structs instead of static alloc.? 58 */ 59 60 /* 61 * A few customizable items: 62 */ 63 64 /* The SCSI ID of the host adapter/computer */ 65 #define AIC_SCSI_HOSTID 7 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 AIC_USE_DWORDS 0 74 75 /* Allow disconnects? Was mainly used in an early phase of the driver when 76 * the message system was very flaky. Should go away soon. 77 */ 78 #define AIC_ALLOW_DISCONNECT 1 79 80 /* Synchronous data transfers? */ 81 #define AIC_USE_SYNCHRONOUS 0 82 #define AIC_SYNC_PERIOD 200 83 #define AIC_SYNC_REQ_ACK_OFS 8 84 85 /* Wide data transfers? */ 86 #define AIC_USE_WIDE 0 87 #define AIC_MAX_WIDTH 0 88 89 /* Max attempts made to transmit a message */ 90 #define AIC_MSG_MAX_ATTEMPT 3 /* Not used now XXX */ 91 92 /* Use DMA (else we do programmed I/O using string instructions) (not yet!)*/ 93 #define AIC_USE_EISA_DMA 0 94 #define AIC_USE_ISA_DMA 0 95 96 /* How to behave on the (E)ISA bus when/if DMAing (on<<4) + off in us */ 97 #define EISA_BRST_TIM ((15<<4) + 1) /* 15us on, 1us off */ 98 99 /* Some spin loop parameters (essentially how long to wait some places) 100 * The problem(?) is that sometimes we expect either to be able to transmit a 101 * byte or to get a new one from the SCSI bus pretty soon. In order to avoid 102 * returning from the interrupt just to get yanked back for the next byte we 103 * may spin in the interrupt routine waiting for this byte to come. How long? 104 * This is really (SCSI) device and processor dependent. Tuneable, I guess. 105 */ 106 #define AIC_MSGI_SPIN 1 /* Will spinwait upto ?ms for a new msg byte */ 107 #define AIC_MSGO_SPIN 1 108 109 /* Include debug functions? At the end of this file there are a bunch of 110 * functions that will print out various information regarding queued SCSI 111 * commands, driver state and chip contents. You can call them from the 112 * kernel debugger. If you set AIC_DEBUG to 0 they are not included (the 113 * kernel uses less memory) but you lose the debugging facilities. 114 */ 115 #define AIC_DEBUG 0 116 117 /* End of customizable parameters */ 118 119 #if AIC_USE_EISA_DMA || AIC_USE_ISA_DMA 120 #error "I said not yet! Start paying attention... grumble" 121 #endif 122 123 #include <sys/types.h> 124 #include <sys/param.h> 125 #include <sys/systm.h> 126 #include <sys/kernel.h> 127 #include <sys/errno.h> 128 #include <sys/ioctl.h> 129 #include <sys/device.h> 130 #include <sys/buf.h> 131 #include <sys/proc.h> 132 #include <sys/user.h> 133 #include <sys/queue.h> 134 135 #include <machine/pio.h> 136 137 #include <scsi/scsi_all.h> 138 #include <scsi/scsiconf.h> 139 140 #include <i386/isa/isavar.h> 141 142 /* Definitions, most of them has turned out to be unneccesary, but here they 143 * are anyway. 144 */ 145 146 /* 147 * Generic SCSI messages. For now we reject most of them. 148 */ 149 /* Messages (1 byte) */ /* I/T M(andatory) or (O)ptional */ 150 #define MSG_CMDCOMPLETE 0x00 /* M/M */ 151 #define MSG_EXTENDED 0x01 /* O/O */ 152 #define MSG_SAVEDATAPOINTER 0x02 /* O/O */ 153 #define MSG_RESTOREPOINTERS 0x03 /* O/O */ 154 #define MSG_DISCONNECT 0x04 /* O/O */ 155 #define MSG_INITIATOR_DET_ERR 0x05 /* M/M */ 156 #define MSG_ABORT 0x06 /* O/M */ 157 #define MSG_MESSAGE_REJECT 0x07 /* M/M */ 158 #define MSG_NOOP 0x08 /* M/M */ 159 #define MSG_PARITY_ERROR 0x09 /* M/M */ 160 #define MSG_LINK_CMD_COMPLETE 0x0a /* O/O */ 161 #define MSG_LINK_CMD_COMPLETEF 0x0b /* O/O */ 162 #define MSG_BUS_DEV_RESET 0x0c /* O/M */ 163 #define MSG_ABORT_TAG 0x0d /* O/O */ 164 #define MSG_CLEAR_QUEUE 0x0e /* O/O */ 165 #define MSG_INIT_RECOVERY 0x0f /* O/O */ 166 #define MSG_REL_RECOVERY 0x10 /* O/O */ 167 #define MSG_TERM_IO_PROC 0x11 /* O/O */ 168 169 /* Messages (2 byte) */ 170 #define MSG_SIMPLE_Q_TAG 0x20 /* O/O */ 171 #define MSG_HEAD_OF_Q_TAG 0x21 /* O/O */ 172 #define MSG_ORDERED_Q_TAG 0x22 /* O/O */ 173 #define MSG_IGN_WIDE_RESIDUE 0x23 /* O/O */ 174 175 /* Identify message */ 176 #define MSG_IDENTIFY(lun) ((AIC_ALLOW_DISCONNECT ? 0xc0 : 0x80)|((lun) & 0x7)) 177 #define MSG_ISIDENT(m) ((m) & 0x80) 178 179 /* Extended messages (opcode) */ 180 #define MSG_EXT_SDTR 0x01 181 #define MSG_EXT_WDTR 0x03 182 183 /* SCSI Status codes */ 184 #define ST_GOOD 0x00 185 #define ST_CHKCOND 0x02 186 #define ST_CONDMET 0x04 187 #define ST_BUSY 0x08 188 #define ST_INTERMED 0x10 189 #define ST_INTERMED_CONDMET 0x14 190 #define ST_RESERVATION_CONFLICT 0x18 191 #define ST_CMD_TERM 0x22 192 #define ST_QUEUE_FULL 0x28 193 194 #define ST_MASK 0x3e /* bit 0,6,7 is reserved */ 195 196 /* AIC6360 definitions */ 197 #define SCSISEQ (iobase + 0x00) /* SCSI sequence control */ 198 #define SXFRCTL0 (iobase + 0x01) /* SCSI transfer control 0 */ 199 #define SXFRCTL1 (iobase + 0x02) /* SCSI transfer control 1 */ 200 #define SCSISIGI (iobase + 0x03) /* SCSI signal in */ 201 #define SCSISIGO (iobase + 0x03) /* SCSI signal out */ 202 #define SCSIRATE (iobase + 0x04) /* SCSI rate control */ 203 #define SCSIID (iobase + 0x05) /* SCSI ID */ 204 #define SELID (iobase + 0x05) /* Selection/Reselection ID */ 205 #define SCSIDAT (iobase + 0x06) /* SCSI Latched Data */ 206 #define SCSIBUS (iobase + 0x07) /* SCSI Data Bus*/ 207 #define STCNT0 (iobase + 0x08) /* SCSI transfer count */ 208 #define STCNT1 (iobase + 0x09) 209 #define STCNT2 (iobase + 0x0a) 210 #define CLRSINT0 (iobase + 0x0b) /* Clear SCSI interrupts 0 */ 211 #define SSTAT0 (iobase + 0x0b) /* SCSI interrupt status 0 */ 212 #define CLRSINT1 (iobase + 0x0c) /* Clear SCSI interrupts 1 */ 213 #define SSTAT1 (iobase + 0x0c) /* SCSI status 1 */ 214 #define SSTAT2 (iobase + 0x0d) /* SCSI status 2 */ 215 #define SCSITEST (iobase + 0x0e) /* SCSI test control */ 216 #define SSTAT3 (iobase + 0x0e) /* SCSI status 3 */ 217 #define CLRSERR (iobase + 0x0f) /* Clear SCSI errors */ 218 #define SSTAT4 (iobase + 0x0f) /* SCSI status 4 */ 219 #define SIMODE0 (iobase + 0x10) /* SCSI interrupt mode 0 */ 220 #define SIMODE1 (iobase + 0x11) /* SCSI interrupt mode 1 */ 221 #define DMACNTRL0 (iobase + 0x12) /* DMA control 0 */ 222 #define DMACNTRL1 (iobase + 0x13) /* DMA control 1 */ 223 #define DMASTAT (iobase + 0x14) /* DMA status */ 224 #define FIFOSTAT (iobase + 0x15) /* FIFO status */ 225 #define DMADATA (iobase + 0x16) /* DMA data */ 226 #define DMADATAL (iobase + 0x16) /* DMA data low byte */ 227 #define DMADATAH (iobase + 0x17) /* DMA data high byte */ 228 #define BRSTCNTRL (iobase + 0x18) /* Burst Control */ 229 #define DMADATALONG (iobase + 0x18) 230 #define PORTA (iobase + 0x1a) /* Port A */ 231 #define PORTB (iobase + 0x1b) /* Port B */ 232 #define REV (iobase + 0x1c) /* Revision (001 for 6360) */ 233 #define STACK (iobase + 0x1d) /* Stack */ 234 #define TEST (iobase + 0x1e) /* Test register */ 235 #define ID (iobase + 0x1f) /* ID register */ 236 237 #define IDSTRING "(C)1991ADAPTECAIC6360 " 238 239 /* What all the bits do */ 240 241 /* SCSISEQ */ 242 #define TEMODEO 0x80 243 #define ENSELO 0x40 244 #define ENSELI 0x20 245 #define ENRESELI 0x10 246 #define ENAUTOATNO 0x08 247 #define ENAUTOATNI 0x04 248 #define ENAUTOATNP 0x02 249 #define SCSIRSTO 0x01 250 251 /* SXFRCTL0 */ 252 #define SCSIEN 0x80 253 #define DMAEN 0x40 254 #define CHEN 0x20 255 #define CLRSTCNT 0x10 256 #define SPIOEN 0x08 257 #define CLRCH 0x02 258 259 /* SXFRCTL1 */ 260 #define BITBUCKET 0x80 261 #define SWRAPEN 0x40 262 #define ENSPCHK 0x20 263 #define STIMESEL1 0x10 264 #define STIMESEL0 0x08 265 #define STIMO_256ms 0x00 266 #define STIMO_128ms 0x08 267 #define STIMO_64ms 0x10 268 #define STIMO_32ms 0x18 269 #define ENSTIMER 0x04 270 #define BYTEALIGN 0x02 271 272 /* SCSISIGI */ 273 #define CDI 0x80 274 #define IOI 0x40 275 #define MSGI 0x20 276 #define ATNI 0x10 277 #define SELI 0x08 278 #define BSYI 0x04 279 #define REQI 0x02 280 #define ACKI 0x01 281 282 /* Important! The 3 most significant bits of this register, in initiator mode, 283 * represents the "expected" SCSI bus phase and can be used to trigger phase 284 * mismatch and phase change interrupts. But more important: If there is a 285 * phase mismatch the chip will not transfer any data! This is actually a nice 286 * feature as it gives us a bit more control over what is happening when we are 287 * bursting data (in) through the FIFOs and the phase suddenly changes from 288 * DATA IN to STATUS or MESSAGE IN. The transfer will stop and wait for the 289 * proper phase to be set in this register instead of dumping the bits into the 290 * FIFOs. 291 */ 292 /* SCSISIGO */ 293 #define CDO 0x80 294 #define CDEXP (CDO) 295 #define IOO 0x40 296 #define IOEXP (IOO) 297 #define MSGO 0x20 298 #define MSGEXP (MSGO) 299 #define ATNO 0x10 300 #define SELO 0x08 301 #define BSYO 0x04 302 #define REQO 0x02 303 #define ACKO 0x01 304 305 /* Information transfer phases */ 306 #define PH_DOUT (0) 307 #define PH_DIN (IOI) 308 #define PH_CMD (CDI) 309 #define PH_STAT (CDI|IOI) 310 #define PH_MSGO (MSGI|CDI) 311 #define PH_MSGI (MSGI|CDI|IOI) 312 313 #define PH_MASK 0xe0 314 315 /* Some pseudo phases for getphase()*/ 316 #define PH_BUSFREE 0x100 /* (Re)Selection no longer valid */ 317 #define PH_INVALID 0x101 /* (Re)Selection valid, but no REQ yet */ 318 #define PH_PSBIT 0x100 /* "pseudo" bit */ 319 320 /* SCSIRATE */ 321 #define SXFR2 0x40 322 #define SXFR1 0x20 323 #define SXFR0 0x10 324 #define SOFS3 0x08 325 #define SOFS2 0x04 326 #define SOFS1 0x02 327 #define SOFS0 0x01 328 329 /* SCSI ID */ 330 #define OID2 0x40 331 #define OID1 0x20 332 #define OID0 0x10 333 #define OID_S 4 /* shift value */ 334 #define TID2 0x04 335 #define TID1 0x02 336 #define TID0 0x01 337 #define SCSI_ID_MASK 0x7 338 339 /* SCSI selection/reselection ID (both target *and* initiator) */ 340 #define SELID7 0x80 341 #define SELID6 0x40 342 #define SELID5 0x20 343 #define SELID4 0x10 344 #define SELID3 0x08 345 #define SELID2 0x04 346 #define SELID1 0x02 347 #define SELID0 0x01 348 349 /* CLRSINT0 Clears what? (interrupt and/or status bit) */ 350 #define SETSDONE 0x80 351 #define CLRSELDO 0x40 /* I */ 352 #define CLRSELDI 0x20 /* I+ */ 353 #define CLRSELINGO 0x10 /* I */ 354 #define CLRSWRAP 0x08 /* I+S */ 355 #define CLRSDONE 0x04 /* I+S */ 356 #define CLRSPIORDY 0x02 /* I */ 357 #define CLRDMADONE 0x01 /* I */ 358 359 /* SSTAT0 Howto clear */ 360 #define TARGET 0x80 361 #define SELDO 0x40 /* Selfclearing */ 362 #define SELDI 0x20 /* Selfclearing when CLRSELDI is set */ 363 #define SELINGO 0x10 /* Selfclearing */ 364 #define SWRAP 0x08 /* CLRSWAP */ 365 #define SDONE 0x04 /* Not used in initiator mode */ 366 #define SPIORDY 0x02 /* Selfclearing (op on SCSIDAT) */ 367 #define DMADONE 0x01 /* Selfclearing (all FIFOs empty & T/C */ 368 369 /* CLRSINT1 Clears what? */ 370 #define CLRSELTIMO 0x80 /* I+S */ 371 #define CLRATNO 0x40 372 #define CLRSCSIRSTI 0x20 /* I+S */ 373 #define CLRBUSFREE 0x08 /* I+S */ 374 #define CLRSCSIPERR 0x04 /* I+S */ 375 #define CLRPHASECHG 0x02 /* I+S */ 376 #define CLRREQINIT 0x01 /* I+S */ 377 378 /* SSTAT1 How to clear? When set?*/ 379 #define SELTO 0x80 /* C select out timeout */ 380 #define ATNTARG 0x40 /* Not used in initiator mode */ 381 #define SCSIRSTI 0x20 /* C RST asserted */ 382 #define PHASEMIS 0x10 /* Selfclearing */ 383 #define BUSFREE 0x08 /* C bus free condition */ 384 #define SCSIPERR 0x04 /* C parity error on inbound data */ 385 #define PHASECHG 0x02 /* C phase in SCSISIGI doesn't match */ 386 #define REQINIT 0x01 /* C or ACK asserting edge of REQ */ 387 388 /* SSTAT2 */ 389 #define SOFFSET 0x20 390 #define SEMPTY 0x10 391 #define SFULL 0x08 392 #define SFCNT2 0x04 393 #define SFCNT1 0x02 394 #define SFCNT0 0x01 395 396 /* SCSITEST */ 397 #define SCTESTU 0x08 398 #define SCTESTD 0x04 399 #define STCTEST 0x01 400 401 /* SSTAT3 */ 402 #define SCSICNT3 0x80 403 #define SCSICNT2 0x40 404 #define SCSICNT1 0x20 405 #define SCSICNT0 0x10 406 #define OFFCNT3 0x08 407 #define OFFCNT2 0x04 408 #define OFFCNT1 0x02 409 #define OFFCNT0 0x01 410 411 /* CLRSERR */ 412 #define CLRSYNCERR 0x04 413 #define CLRFWERR 0x02 414 #define CLRFRERR 0x01 415 416 /* SSTAT4 */ 417 #define SYNCERR 0x04 418 #define FWERR 0x02 419 #define FRERR 0x01 420 421 /* SIMODE0 */ 422 #define ENSELDO 0x40 423 #define ENSELDI 0x20 424 #define ENSELINGO 0x10 425 #define ENSWRAP 0x08 426 #define ENSDONE 0x04 427 #define ENSPIORDY 0x02 428 #define ENDMADONE 0x01 429 430 /* SIMODE1 */ 431 #define ENSELTIMO 0x80 432 #define ENATNTARG 0x40 433 #define ENSCSIRST 0x20 434 #define ENPHASEMIS 0x10 435 #define ENBUSFREE 0x08 436 #define ENSCSIPERR 0x04 437 #define ENPHASECHG 0x02 438 #define ENREQINIT 0x01 439 440 /* DMACNTRL0 */ 441 #define ENDMA 0x80 442 #define B8MODE 0x40 443 #define DMA 0x20 444 #define DWORDPIO 0x10 445 #define WRITE 0x08 446 #define INTEN 0x04 447 #define RSTFIFO 0x02 448 #define SWINT 0x01 449 450 /* DMACNTRL1 */ 451 #define PWRDWN 0x80 452 #define ENSTK32 0x40 453 #define STK4 0x10 454 #define STK3 0x08 455 #define STK2 0x04 456 #define STK1 0x02 457 #define STK0 0x01 458 459 /* DMASTAT */ 460 #define ATDONE 0x80 461 #define WORDRDY 0x40 462 #define INTSTAT 0x20 463 #define DFIFOFULL 0x10 464 #define DFIFOEMP 0x08 465 #define DFIFOHF 0x04 466 #define DWORDRDY 0x02 467 468 /* BRSTCNTRL */ 469 #define BON3 0x80 470 #define BON2 0x40 471 #define BON1 0x20 472 #define BON0 0x10 473 #define BOFF3 0x08 474 #define BOFF2 0x04 475 #define BOFF1 0x02 476 #define BOFF0 0x01 477 478 /* TEST */ 479 #define BOFFTMR 0x40 480 #define BONTMR 0x20 481 #define STCNTH 0x10 482 #define STCNTM 0x08 483 #define STCNTL 0x04 484 #define SCSIBLK 0x02 485 #define DMABLK 0x01 486 487 488 #define orreg(reg, val) outb((reg), inb(reg)| (val)) 489 #define andreg(reg, val) outb((reg), inb(reg)& (val)) 490 #define nandreg(reg, val) outb((reg), inb(reg)&~(val)) 491 492 493 494 /* Grabbed from Julians SCSI aha-drivers */ 495 #ifdef DDB 496 int Debugger(); 497 #else DDB 498 #define Debugger() panic("should call debugger here (aic6360.c)") 499 #endif DDB 500 501 typedef u_long physaddr; 502 503 struct aic_dma_seg { 504 physaddr addr; 505 long len; 506 }; 507 508 extern int delaycount; 509 #define FUDGE(X) ((X)>>1) /* get 1 ms spincount */ 510 #define MINIFUDGE(X) ((X)>>4) /* get (approx) 125us spincount */ 511 #define AIC_NSEG 16 512 #define NUM_CONCURRENT 7 /* Only one per target for now */ 513 514 /* 515 * ACB. Holds additional information for each SCSI command Comments: We 516 * need a separate scsi command block because we may need to overwrite it 517 * with a request sense command. Basicly, we refrain from fiddling with 518 * the scsi_xfer struct (except do the expected updating of return values). 519 * We'll generally update: xs->{flags,resid,error,sense,status} and 520 * occasionally xs->retries. 521 */ 522 struct acb { 523 TAILQ_ENTRY(acb) chain; 524 struct scsi_xfer *xs; /* SCSI xfer ctrl block from above */ 525 int flags; /* Status */ 526 #define ACB_FREE 0x00 527 #define ACB_ACTIVE 0x01 528 #define ACB_DONE 0x04 529 #define ACB_CHKSENSE 0x08 530 /* struct aic_dma_seg dma[AIC_NSEG]; /* Physical addresses+len */ 531 struct scsi_generic cmd; /* SCSI command block */ 532 int clen; 533 u_char *dp; /* Saved data pointer */ 534 int dleft; /* Residue */ 535 int stat; /* SCSI status byte */ 536 }; 537 538 /* 539 * Some info about each (possible) target on the SCSI bus. This should 540 * probably have been a "per target+lunit" structure, but we'll leave it at 541 * this for now. Is there a way to reliably hook it up to sc->fordriver?? 542 */ 543 struct aic_tinfo { 544 int cmds; /* #commands processed */ 545 int dconns; /* #disconnects */ 546 int touts; /* #timeouts */ 547 int perrs; /* #parity errors */ 548 int senses; /* #request sense commands sent */ 549 ushort lubusy; /* What local units/subr. are busy? */ 550 u_char flags; 551 #define DO_SYNC 0x01 /* (Re)Negotiate synchronous options */ 552 #define DO_WIDE 0x02 /* (Re)Negotiate wide options */ 553 u_char period; /* Period suggestion */ 554 u_char offset; /* Offset suggestion */ 555 u_char width; /* Width suggestion */ 556 u_char syncdata; /* True negotiated synch parameters */ 557 } tinfo_t; 558 559 struct aic_softc { /* One of these per adapter */ 560 struct device sc_dev; 561 struct isadev sc_id; 562 struct intrhand sc_ih; 563 564 struct scsi_link sc_link; /* prototype for subdevs */ 565 int id_irq; /* IRQ on the EISA bus */ 566 int id_drq; /* DRQ on the EISA bus */ 567 int iobase; /* Base I/O port */ 568 /* Lists of command blocks */ 569 TAILQ_HEAD(acb_list, acb) free_list, ready_list, nexus_list; 570 struct acb *nexus; /* current command */ 571 /* Command blocks and target info */ 572 struct acb acb[NUM_CONCURRENT]; 573 struct aic_tinfo tinfo[8]; 574 /* Data about the current nexus (updated for every cmd switch) */ 575 u_char *dp; /* Current data pointer */ 576 int dleft; /* Data bytes left to transfer */ 577 u_char *cp; /* Current command pointer */ 578 int cleft; /* Command bytes left to transfer */ 579 /* Adapter state */ 580 u_short prevphase; /* Copy of what bus phase we were in */ 581 u_char state; /* State applicable to the adapter */ 582 #define AIC_IDLE 1 583 #define AIC_SELECTING 2 /* SCSI command is arbiting */ 584 #define AIC_RESELECTED 3 /* Has been reselected */ 585 #define AIC_CONNECTED 4 /* Actively using the SCSI bus */ 586 #define AIC_DISCONNECT 5 /* MSG_DISCONNECT received */ 587 #define AIC_CMDCOMPLETE 6 /* MSG_CMDCOMPLETE received */ 588 #define AIC_CLEANING 7 589 u_char flags; 590 #define AIC_DROP_MSGI 0x01 /* Discard all msgs (parity err detected) */ 591 #define AIC_ABORTING 0x02 /* Bailing out */ 592 #define AIC_DOINGDMA 0x04 /* The FIFO data path is active! */ 593 /* Debugging stuff */ 594 u_char progress; /* Set if interrupt has achieved progress */ 595 /* Message stuff */ 596 u_char msgpriq; /* Messages we want to send */ 597 u_char msgoutq; /* Messages sent during last MESSAGE OUT */ 598 u_char msgout; /* Message last transmitted */ 599 #define SEND_DEV_RESET 0x01 600 #define SEND_PARITY_ERROR 0x02 601 #define SEND_ABORT 0x04 602 #define SEND_REJECT 0x08 603 #define SEND_INIT_DET_ERR 0x10 604 #define SEND_IDENTIFY 0x20 605 #define SEND_SDTR 0x40 606 #define SEND_WDTR 0x80 607 #define AIC_MAX_MSG_LEN 8 608 u_char omess[AIC_MAX_MSG_LEN]; /* Scratch area for messages */ 609 u_char *omp; /* Message pointer (for multibyte messages) */ 610 u_char imess[AIC_MAX_MSG_LEN]; 611 u_char *imp; /* Message pointer (for multibyte messages) */ 612 }; 613 614 #if AIC_DEBUG 615 #define AIC_SHOWACBS 0x01 616 #define AIC_SHOWINTS 0x02 617 #define AIC_SHOWCMDS 0x04 618 #define AIC_SHOWMISC 0x08 619 #define AIC_SHOWTRACE 0x10 620 #define AIC_SHOWSTART 0x20 621 #define AIC_DOBREAK 0x40 622 int aic_debug = 0x00; /* AIC_SHOWSTART|AIC_SHOWMISC|AIC_SHOWTRACE; /**/ 623 #define AIC_PRINT(b, s) do {if ((aic_debug & (b)) != 0) printf s;} while (0) 624 #define AIC_BREAK() do {if ((aic_debug & AIC_DOBREAK) != 0) Debugger();} while (0) 625 #define AIC_ASSERT(x) do {if (x) {} else {printf("aic at line %d: assertion failed\n", __LINE__); Debugger();}} while (0) 626 #else 627 #define AIC_PRINT(b, s) 628 #define AIC_BREAK() 629 #define AIC_ASSERT(x) 630 #endif 631 632 #define AIC_ACBS(s) AIC_PRINT(AIC_SHOWACBS, s) 633 #define AIC_INTS(s) AIC_PRINT(AIC_SHOWINTS, s) 634 #define AIC_CMDS(s) AIC_PRINT(AIC_SHOWCMDS, s) 635 #define AIC_MISC(s) AIC_PRINT(AIC_SHOWMISC, s) 636 #define AIC_TRACE(s) AIC_PRINT(AIC_SHOWTRACE, s) 637 #define AIC_START(s) AIC_PRINT(AIC_SHOWSTART, s) 638 639 int aicprobe __P((struct device *, void *, void *)); 640 void aicattach __P((struct device *, struct device *, void *)); 641 void aic_minphys __P((struct buf *)); 642 u_int aic_adapter_info __P((struct aic_softc *)); 643 int aicintr __P((struct aic_softc *)); 644 void aic_init __P((struct aic_softc *)); 645 void aic_done __P((struct acb *)); 646 int aic_scsi_cmd __P((struct scsi_xfer *)); 647 int aic_poll __P((struct aic_softc *, struct acb *)); 648 void aic_add_timeout __P((struct acb *, int)); 649 void aic_remove_timeout __P((struct acb *)); 650 void aic_timeout __P((void *arg)); 651 int aic_find __P((struct aic_softc *)); 652 void aic_sched __P((struct aic_softc *)); 653 void aic_scsi_reset __P((struct aic_softc *)); 654 #if AIC_DEBUG 655 void aic_print_active_acb(); 656 void aic_dump_driver(); 657 void aic_dump6360(); 658 #endif 659 660 /* Linkup to the rest of the kernel */ 661 struct cfdriver aiccd = { 662 NULL, "aic", aicprobe, aicattach, DV_DULL, sizeof(struct aic_softc) 663 }; 664 665 struct scsi_adapter aic_switch = { 666 aic_scsi_cmd, 667 aic_minphys, 668 0, 669 0, 670 aic_adapter_info, 671 "aic" 672 }; 673 674 struct scsi_device aic_dev = { 675 NULL, /* Use default error handler */ 676 NULL, /* have a queue, served by this */ 677 NULL, /* have no async handler */ 678 NULL, /* Use default 'done' routine */ 679 "aic", 680 0 681 }; 682 683 /* 684 * INITIALIZATION ROUTINES (probe, attach ++) 685 */ 686 687 /* 688 * aicprobe: probe for AIC6360 SCSI-controller 689 * returns non-zero value if a controller is found. 690 */ 691 int 692 aicprobe(parent, match, aux) 693 struct device *parent; 694 void *match, *aux; 695 { 696 struct aic_softc *aic = match; 697 struct isa_attach_args *ia = aux; 698 int i, len, ic; 699 700 #ifdef NEWCONFIG 701 if (ia->ia_iobase == IOBASEUNK) 702 return 0; 703 #endif 704 aic->iobase = ia->ia_iobase; 705 if (aic_find(aic) != 0) 706 return 0; 707 #ifdef NEWCONFIG 708 if (ia->ia_irq == IRQUNK) 709 ia->ia_irq = aic->aic_int; 710 else if (ia->ia_irq != aic->aic_int) { 711 printf("aic%d: irq mismatch, %x != %x\n", 712 aic->sc_dev.dv_unit, ia->ia_irq, aic->aic_int); 713 return 0; 714 } 715 716 if (ia->ia_drq == DRQUNK) 717 ia->ia_drq = aic->aic_dma; 718 else if (ia->ia_drq != aic->aic_dma) { 719 printf("aic%d: drq mismatch, %x != %x\n", 720 aic->sc_dev.dv_unit, ia->ia_drq, aic->aic_dma); 721 return 0; 722 } 723 #endif 724 ia->ia_msize = 0; 725 ia->ia_iosize = 0x20; 726 return 1; 727 } 728 729 /* Do the real search-for-device. 730 * Prerequisite: aic->iobase should be set to the proper value 731 */ 732 int 733 aic_find(aic) 734 struct aic_softc *aic; 735 { 736 int iobase = aic->iobase; 737 char chip_id[sizeof(IDSTRING)]; /* For chips that support it */ 738 char *start; 739 int i; 740 741 /* Remove aic6360 from possible powerdown mode */ 742 outb(DMACNTRL0, 0); 743 744 /* Thanks to mark@aggregate.com for the new method for detecting 745 * whether the chip is present or not. Bonus: may also work for 746 * the AIC-6260! 747 */ 748 AIC_TRACE(("aic: probing for aic-chip at port 0x%x\n",(int)iobase)); 749 /* 750 * Linux also init's the stack to 1-16 and then clears it, 751 * 6260's don't appear to have an ID reg - mpg 752 */ 753 /* Push the sequence 0,1,..,15 on the stack */ 754 #define STSIZE 16 755 outb(DMACNTRL1, 0); /* Reset stack pointer */ 756 for (i = 0; i < STSIZE; i++) 757 outb(STACK, i); 758 759 /* See if we can pull out the same sequence */ 760 outb(DMACNTRL1, 0); 761 for (i = 0; i < STSIZE && inb(STACK) == i; i++) 762 ; 763 if (i != STSIZE) { 764 AIC_START(("STACK futzed at %d.\n", i)); 765 return ENXIO; 766 } 767 768 /* See if we can pull the id string out of the ID register, 769 * now only used for informational purposes. 770 */ 771 bzero(chip_id, sizeof(chip_id)); 772 insb(ID, chip_id, sizeof(IDSTRING)-1); 773 AIC_START(("AIC found at 0x%x ", (int)aic->iobase)); 774 AIC_START(("ID: %s ",chip_id)); 775 AIC_START(("chip revision %d\n",(int)inb(REV))); 776 return 0; 777 } 778 779 int 780 aicprint() 781 { 782 } 783 784 /* 785 * Attach the AIC6360, fill out some high and low level data structures 786 */ 787 void 788 aicattach(parent, self, aux) 789 struct device *parent, *self; 790 void *aux; 791 { 792 struct isa_attach_args *ia = aux; 793 struct aic_softc *aic = (void *)self; 794 795 AIC_TRACE(("aicattach ")); 796 aic->state = 0; 797 aic_init(aic); /* Init chip and driver */ 798 799 /* 800 * Fill in the prototype scsi_link 801 */ 802 aic->sc_link.adapter_softc = aic; 803 aic->sc_link.adapter_targ = AIC_SCSI_HOSTID; 804 aic->sc_link.adapter = &aic_switch; 805 aic->sc_link.device = &aic_dev; 806 printf("\n"); 807 808 #ifdef NEWCONFIG 809 isa_establish(&aic->sc_id, &aic->sc_dev); 810 #endif 811 aic->sc_ih.ih_fun = aicintr; 812 aic->sc_ih.ih_arg = aic; 813 aic->sc_ih.ih_level = IPL_BIO; 814 intr_establish(ia->ia_irq, &aic->sc_ih); 815 816 config_found(self, &aic->sc_link, aicprint); 817 } 818 819 820 /* Initialize AIC6360 chip itself 821 * The following conditions should hold: 822 * aicprobe should have succeeded, i.e. the iobase address in aic_softc must 823 * be valid. 824 */ 825 static void 826 aic6360_reset(aic) 827 struct aic_softc *aic; 828 { 829 int iobase = aic->iobase; 830 831 outb(SCSITEST, 0); /* Doc. recommends to clear these two */ 832 outb(TEST, 0); /* registers before operations commence */ 833 834 /* Reset SCSI-FIFO and abort any transfers */ 835 outb(SXFRCTL0, CHEN|CLRCH|CLRSTCNT); 836 837 /* Reset DMA-FIFO */ 838 outb(DMACNTRL0, RSTFIFO); 839 outb(DMACNTRL1, 0); 840 841 outb(SCSISEQ, 0); /* Disable all selection features */ 842 outb(SXFRCTL1, 0); 843 844 outb(SIMODE0, 0x00); /* Disable some interrupts */ 845 outb(CLRSINT0, 0x7f); /* Clear a slew of interrupts */ 846 847 outb(SIMODE1, 0x00); /* Disable some more interrupts */ 848 outb(CLRSINT1, 0xef); /* Clear another slew of interrupts */ 849 850 outb(SCSIRATE, 0); /* Disable synchronous transfers */ 851 852 outb(CLRSERR, 0x07); /* Haven't seen ant errors (yet) */ 853 854 outb(SCSIID, AIC_SCSI_HOSTID << OID_S); /* Set our SCSI-ID */ 855 outb(BRSTCNTRL, EISA_BRST_TIM); 856 } 857 858 /* Pull the SCSI RST line for 500 us */ 859 void 860 aic_scsi_reset(aic) 861 struct aic_softc *aic; 862 { 863 int iobase = aic->iobase; 864 865 outb(SCSISEQ, SCSIRSTO); 866 delay(500); 867 outb(SCSISEQ, 0); 868 delay(50); 869 } 870 871 /* 872 * Initialize aic SCSI driver, also (conditonally) reset the SCSI bus. 873 * The reinitialization is still buggy (e.g. on SCSI resets). 874 */ 875 void 876 aic_init(aic) 877 struct aic_softc *aic; 878 { 879 int iobase = aic->iobase; 880 struct acb *acb; 881 int r; 882 883 aic_scsi_reset(aic); 884 aic6360_reset(aic); /* Clean up our own hardware */ 885 886 /*XXX*/ /* If not the first time (probably a reset condition), 887 * we should clean queues with active commands 888 */ 889 if (aic->state == 0) { /* First time through */ 890 TAILQ_INIT(&aic->ready_list); 891 TAILQ_INIT(&aic->nexus_list); 892 TAILQ_INIT(&aic->free_list); 893 aic->nexus = NULL; 894 acb = aic->acb; 895 bzero(acb, sizeof(aic->acb)); 896 for (r = 0; r < sizeof(aic->acb) / sizeof(*acb); r++) { 897 TAILQ_INSERT_TAIL(&aic->free_list, acb, chain); 898 acb++; 899 } 900 bzero(&aic->tinfo, sizeof(aic->tinfo)); 901 } else { 902 aic->state = AIC_CLEANING; 903 if ((acb = aic->nexus) != NULL) { 904 acb->xs->error = XS_DRIVER_STUFFUP; 905 untimeout(aic_timeout, acb); 906 aic_done(acb); 907 } 908 while (acb = aic->nexus_list.tqh_first) { 909 acb->xs->error = XS_DRIVER_STUFFUP; 910 untimeout(aic_timeout, acb); 911 aic_done(acb); 912 } 913 } 914 915 aic->prevphase = PH_INVALID; 916 for (r = 0; r < 8; r++) { 917 struct aic_tinfo *ti = &aic->tinfo[r]; 918 919 ti->flags = 0; 920 if (AIC_USE_SYNCHRONOUS) { 921 ti->flags |= DO_SYNC; 922 ti->period = AIC_SYNC_PERIOD; 923 ti->offset = AIC_SYNC_REQ_ACK_OFS; 924 } 925 if (AIC_USE_WIDE) { 926 ti->flags |= DO_WIDE; 927 ti->width = AIC_MAX_WIDTH; 928 } 929 ti->syncdata = 0; 930 } 931 932 aic->state = AIC_IDLE; 933 outb(DMACNTRL0, INTEN); 934 } 935 936 void 937 aic_free_acb(aic, acb, flags) 938 struct aic_softc *aic; 939 struct acb *acb; 940 int flags; 941 { 942 int s; 943 944 if ((flags & SCSI_NOMASK) == 0) 945 s = splbio(); 946 947 acb->flags = ACB_FREE; 948 TAILQ_INSERT_HEAD(&aic->free_list, acb, chain); 949 if (acb->chain.tqe_next == 0) 950 wakeup(&aic->free_list); 951 952 if ((flags & SCSI_NOMASK) == 0) 953 splx(s); 954 } 955 956 struct acb * 957 aic_get_acb(aic, flags) 958 struct aic_softc *aic; 959 int flags; 960 { 961 int s; 962 struct acb *acb; 963 964 /* Get a aic command block */ 965 if ((flags & SCSI_NOMASK) == 0) 966 s = splbio(); 967 968 while ((acb = aic->free_list.tqh_first) == NULL && 969 (flags & SCSI_NOSLEEP) == 0) 970 tsleep(&aic->free_list, PRIBIO, "aicacb", 0); 971 if (acb) { 972 TAILQ_REMOVE(&aic->free_list, acb, chain); 973 acb->flags = ACB_ACTIVE; 974 } 975 976 if ((flags & SCSI_NOMASK) == 0) 977 splx(s); 978 979 return acb; 980 } 981 982 /* 983 * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS 984 */ 985 986 /* 987 * Expected sequence: 988 * 1) Command inserted into ready list 989 * 2) Command selected for execution 990 * 3) Command won arbitration and has selected target device 991 * 4) Send message out (identify message, eventually also sync.negotiations) 992 * 5) Send command 993 * 5a) Receive disconnect message, disconnect. 994 * 5b) Reselected by target 995 * 5c) Receive identify message from target. 996 * 6) Send or receive data 997 * 7) Receive status 998 * 8) Receive message (command complete etc.) 999 * 9) If status == SCSI_CHECK construct a synthetic request sense SCSI cmd. 1000 * Repeat 2-8 (no disconnects please...) 1001 */ 1002 1003 /* 1004 * Start a SCSI-command 1005 * This function is called by the higher level SCSI-driver to queue/run 1006 * SCSI-commands. 1007 */ 1008 int 1009 aic_scsi_cmd(xs) 1010 struct scsi_xfer *xs; 1011 { 1012 struct scsi_link *sc = xs->sc_link; 1013 struct aic_softc *aic = sc->adapter_softc; 1014 struct acb *acb; 1015 int s, flags; 1016 int iobase = aic->iobase; 1017 1018 AIC_TRACE(("aic_scsi_cmd ")); 1019 AIC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen, 1020 sc->target)); 1021 1022 flags = xs->flags; 1023 1024 if ((acb = aic_get_acb(aic, flags)) == NULL) { 1025 xs->error = XS_DRIVER_STUFFUP; 1026 return TRY_AGAIN_LATER; 1027 } 1028 1029 /* Initialize acb */ 1030 acb->xs = xs; 1031 bcopy(xs->cmd, &acb->cmd, xs->cmdlen); 1032 acb->clen = xs->cmdlen; 1033 acb->dp = xs->data; 1034 acb->dleft = xs->datalen; 1035 acb->stat = 0; 1036 1037 if ((flags & SCSI_NOMASK) == 0) 1038 s = splbio(); 1039 1040 TAILQ_INSERT_TAIL(&aic->ready_list, acb, chain); 1041 timeout(aic_timeout, acb, (xs->timeout*hz)/1000); 1042 1043 if (aic->state == AIC_IDLE) 1044 aic_sched(aic); 1045 1046 if ((flags & SCSI_NOMASK) == 0) { /* Almost done. Wait outside */ 1047 splx(s); 1048 AIC_MISC(("SUCCESSFULLY_QUEUED")); 1049 return SUCCESSFULLY_QUEUED; 1050 } 1051 1052 /* Not allowed to use interrupts, use polling instead */ 1053 return aic_poll(aic, acb); 1054 } 1055 1056 /* 1057 * Adjust transfer size in buffer structure 1058 */ 1059 void 1060 aic_minphys(bp) 1061 struct buf *bp; 1062 { 1063 1064 AIC_TRACE(("aic_minphys ")); 1065 if (bp->b_bcount > (AIC_NSEG << PGSHIFT)) 1066 bp->b_bcount = (AIC_NSEG << PGSHIFT); 1067 } 1068 1069 1070 u_int 1071 aic_adapter_info(aic) 1072 struct aic_softc *aic; 1073 { 1074 1075 AIC_TRACE(("aic_adapter_info ")); 1076 return 2; /* One outstanding command per target */ 1077 } 1078 1079 /* 1080 * Used when interrupt driven I/O isn't allowed, e.g. during boot. 1081 */ 1082 int 1083 aic_poll(aic, acb) 1084 struct aic_softc *aic; 1085 struct acb *acb; 1086 { 1087 register int iobase = aic->iobase; 1088 struct scsi_xfer *xs = acb->xs; 1089 int count = xs->timeout * 10; 1090 1091 AIC_TRACE(("aic_poll ")); 1092 while (count) { 1093 if ((inb(DMASTAT) & INTSTAT) != 0) 1094 aicintr(aic); 1095 if ((xs->flags & ITSDONE) != 0) 1096 break; 1097 delay(100); 1098 count--; 1099 } 1100 if (count == 0) { 1101 AIC_MISC(("aic_poll: timeout")); 1102 aic_timeout((caddr_t)acb); 1103 } 1104 if (xs->error) 1105 return HAD_ERROR; 1106 return COMPLETE; 1107 } 1108 1109 /* LOW LEVEL SCSI UTILITIES */ 1110 1111 /* Determine the SCSI bus phase, return either a real SCSI bus phase or some 1112 * pseudo phase we use to detect certain exceptions. This one is a bit tricky. 1113 * The bits we peek at: 1114 * CDI, MSGI and DI is the 3 SCSI signals determining the bus phase. 1115 * These should be qualified by REQI high and ACKI low. 1116 * Also peek at SSTAT0[SELDO|SELDI] to detect a passing BUSFREE condition. 1117 * No longer detect SCSI RESET or PERR here. They are tested for separately 1118 * in the interrupt handler. 1119 * Note: If an exception occur at some critical time during the phase 1120 * determination we'll most likely return something wildly erronous.... 1121 */ 1122 static inline u_short 1123 aicphase(aic) 1124 struct aic_softc *aic; 1125 { 1126 register int iobase = aic->iobase; 1127 register u_char sstat0, sstat1, scsisig; 1128 1129 sstat1 = inb(SSTAT1); /* Look for REQINIT (REQ asserted) */ 1130 scsisig = inb(SCSISIGI); /* Get the SCSI bus signals */ 1131 sstat0 = inb(SSTAT0); /* Get the selection valid status bits */ 1132 1133 if ((sstat0 & (SELDO|SELDI)) == 0) /* Selection became invalid? */ 1134 return PH_BUSFREE; 1135 1136 /* Selection is still valid */ 1137 if ((sstat1 & REQINIT) == 0) /* REQ not asserted ? */ 1138 return PH_INVALID; 1139 1140 /* REQ is asserted, (and ACK is not) */ 1141 return (scsisig & PH_MASK); 1142 } 1143 1144 /* 1145 * Start a selection. This is used by aic_sched() to select an idle target, 1146 * and by aic_done() to immediately reselect a target to get sense information. 1147 */ 1148 void 1149 aic_select(aic, target) 1150 struct aic_softc *aic; 1151 int target; 1152 { 1153 int iobase = aic->iobase; 1154 1155 outb(SCSIID, AIC_SCSI_HOSTID << OID_S | target); 1156 outb(SCSIRATE, aic->tinfo[target].syncdata); 1157 outb(SXFRCTL1, STIMO_256ms|ENSTIMER); 1158 /* Always enable reselections. */ 1159 outb(SIMODE0, ENSELDI|ENSELDO); 1160 outb(SIMODE1, ENSCSIRST|ENSELTIMO); 1161 outb(SCSISEQ, ENRESELI|ENSELO|ENAUTOATNO); 1162 1163 aic->state = AIC_SELECTING; 1164 } 1165 1166 /* 1167 * Schedule a SCSI operation. This has now been pulled out of the interrupt 1168 * handler so that we may call it from aic_scsi_cmd and aic_done. This may 1169 * save us an unecessary interrupt just to get things going. Should only be 1170 * called when state == AIC_IDLE and at bio pl. 1171 */ 1172 void 1173 aic_sched(aic) 1174 register struct aic_softc *aic; 1175 { 1176 int iobase = aic->iobase; 1177 struct acb *acb; 1178 struct scsi_link *sc; 1179 struct aic_tinfo *ti; 1180 1181 /* 1182 * Find first acb in ready queue that is for a target/lunit pair that 1183 * is not busy. 1184 */ 1185 for (acb = aic->ready_list.tqh_first; acb != NULL; 1186 acb = acb->chain.tqe_next) { 1187 sc = acb->xs->sc_link; 1188 ti = &aic->tinfo[sc->target]; 1189 if ((ti->lubusy & (1<<sc->lun)) == 0) { 1190 AIC_MISC(("selecting %d:%d ", sc->target, sc->lun)); 1191 TAILQ_REMOVE(&aic->ready_list, acb, chain); 1192 aic->nexus = acb; 1193 aic_select(aic, sc->target); 1194 return; 1195 } else 1196 AIC_MISC(("%d:%d busy\n", sc->target, sc->lun)); 1197 } 1198 AIC_MISC(("idle ")); 1199 /* Nothing to start; just enable reselections and wait. */ 1200 outb(SIMODE0, ENSELDI); 1201 outb(SIMODE1, ENSCSIRST); 1202 outb(SCSISEQ, ENRESELI); 1203 } 1204 1205 /* 1206 * POST PROCESSING OF SCSI_CMD (usually current) 1207 */ 1208 void 1209 aic_done(acb) 1210 struct acb *acb; 1211 { 1212 struct scsi_xfer *xs = acb->xs; 1213 struct scsi_link *sc = xs->sc_link; 1214 struct aic_softc *aic = sc->adapter_softc; 1215 struct aic_tinfo *ti; 1216 1217 AIC_TRACE(("aic_done ")); 1218 ti = &aic->tinfo[sc->target]; 1219 1220 /* 1221 * Now, if we've come here with no error code, i.e. we've kept the 1222 * initial XS_NOERROR, and the status code signals that we should 1223 * check sense, we'll need to set up a request sense cmd block and 1224 * push the command back into the ready queue *before* any other 1225 * commands for this target/lunit, else we lose the sense info. 1226 * We don't support chk sense conditions for the request sense cmd. 1227 */ 1228 if (xs->error == XS_NOERROR && (acb->flags & ACB_CHKSENSE) == 0) { 1229 if ((acb->stat & ST_MASK) == SCSI_CHECK) { 1230 struct scsi_sense *ss = (void *)&acb->cmd; 1231 1232 AIC_MISC(("requesting sense ")); 1233 /* First, save the return values */ 1234 xs->resid = acb->dleft; 1235 xs->status = acb->stat; 1236 /* Next, setup a request sense command block */ 1237 bzero(ss, sizeof(*ss)); 1238 ss->op_code = REQUEST_SENSE; 1239 ss->byte2 = sc->lun << 5; 1240 ss->length = sizeof(struct scsi_sense_data); 1241 acb->clen = sizeof(*ss); 1242 acb->dp = (char *)&xs->sense; 1243 acb->dleft = sizeof(struct scsi_sense_data); 1244 acb->flags = ACB_ACTIVE|ACB_CHKSENSE; 1245 ti->senses++; 1246 ti->lubusy &= ~(1<<sc->lun); 1247 if (acb == aic->nexus) { 1248 aic_select(aic, sc->target); 1249 } else { 1250 TAILQ_INSERT_HEAD(&aic->ready_list, acb, chain); 1251 } 1252 return; 1253 } 1254 } 1255 1256 if ((xs->flags & SCSI_ERR_OK) != 0) { 1257 xs->resid = 0; 1258 xs->error = XS_NOERROR; 1259 } else if (xs->error == XS_NOERROR && (acb->flags & ACB_CHKSENSE) != 0) { 1260 xs->error = XS_SENSE; 1261 } else { 1262 xs->resid = acb->dleft; 1263 } 1264 xs->flags |= ITSDONE; 1265 1266 #if AIC_DEBUG 1267 if ((aic_debug & AIC_SHOWMISC) != 0) { 1268 if (xs->resid != 0) 1269 printf("resid=%d ", xs->resid); 1270 if (xs->error == XS_SENSE) 1271 printf("sense=0x%02x\n", xs->sense.error_code); 1272 else 1273 printf("error=%d\n", xs->error); 1274 } 1275 #endif 1276 1277 /* 1278 * Remove the ACB from whatever queue it's on. We have to do a bit of 1279 * a hack to figure out which queue it's on. Note that it is *not* 1280 * necessary to cdr down the ready queue, but we must cdr down the 1281 * nexus queue and see if it's there, so we can mark the unit as no 1282 * longer busy. This code is sickening, but it works. 1283 */ 1284 if (acb == aic->nexus) { 1285 ti->lubusy &= ~(1<<sc->lun); 1286 aic->state = AIC_IDLE; 1287 aic->nexus = NULL; 1288 aic_sched(aic); 1289 } else if (aic->ready_list.tqh_last == &acb->chain.tqe_next) { 1290 TAILQ_REMOVE(&aic->ready_list, acb, chain); 1291 } else { 1292 register struct acb *acb2; 1293 for (acb2 = aic->nexus_list.tqh_first; acb2 != NULL; 1294 acb2 = acb2->chain.tqe_next) { 1295 if (acb2 == acb) 1296 break; 1297 } 1298 if (acb2 != NULL) { 1299 TAILQ_REMOVE(&aic->nexus_list, acb, chain); 1300 ti->lubusy &= ~(1<<sc->lun); 1301 } else if (acb->chain.tqe_next) { 1302 TAILQ_REMOVE(&aic->ready_list, acb, chain); 1303 } else { 1304 printf("%s: can't find matching acb\n", 1305 aic->sc_dev.dv_xname); 1306 Debugger(); 1307 } 1308 } 1309 1310 aic_free_acb(aic, acb, xs->flags); 1311 ti->cmds++; 1312 scsi_done(xs); 1313 } 1314 1315 /* 1316 * INTERRUPT/PROTOCOL ENGINE 1317 */ 1318 1319 /* The message system: 1320 * This is a revamped message system that now should easier accomodate new 1321 * messages, if necessary. 1322 * Currently we accept these messages: 1323 * IDENTIFY (when reselecting) 1324 * COMMAND COMPLETE # (expect bus free after messages marked #) 1325 * NOOP 1326 * MESSAGE REJECT 1327 * SYNCHRONOUS DATA TRANSFER REQUEST 1328 * SAVE DATA POINTER 1329 * RESTORE POINTERS 1330 * DISCONNECT # 1331 * 1332 * We may send these messages in prioritized order: 1333 * BUS DEVICE RESET # if SCSI_RESET & xs->flags (or in weird sits.) 1334 * MESSAGE PARITY ERROR par. err. during MSGI 1335 * MESSAGE REJECT If we get a message we don't know how to handle 1336 * ABORT # send on errors 1337 * INITIATOR DETECTED ERROR also on errors (SCSI2) (during info xfer) 1338 * IDENTIFY At the start of each transfer 1339 * SYNCHRONOUS DATA TRANSFER REQUEST if appropriate 1340 * NOOP if nothing else fits the bill ... 1341 */ 1342 1343 #define aic_sched_msgout(m) \ 1344 do { \ 1345 if (aic->msgpriq == 0) \ 1346 orreg(SCSISIGO, ATNO); \ 1347 aic->msgpriq |= (m); \ 1348 } while (0) 1349 1350 #define IS1BYTEMSG(m) (((m) != 0x01 && (m) < 0x20) || (m) >= 0x80) 1351 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20) 1352 #define ISEXTMSG(m) ((m) == 0x01) 1353 /* 1354 * Precondition: 1355 * The SCSI bus is already in the MSGI phase and there is a message byte 1356 * on the bus, along with an asserted REQ signal. 1357 */ 1358 void 1359 aic_msgin(aic) 1360 register struct aic_softc *aic; 1361 { 1362 register int iobase = aic->iobase; 1363 int n; 1364 1365 AIC_TRACE(("aic_msgin ")); 1366 aic->progress = 0; 1367 1368 if (aic->prevphase == PH_MSGI) { 1369 /* This is a continuation of the previous message. */ 1370 n = aic->imp - aic->imess; 1371 goto nextbyte; 1372 } 1373 1374 /* This is a new MESSAGE IN phase. Clean up our state. */ 1375 aic->flags &= ~AIC_DROP_MSGI; 1376 1377 nextmsg: 1378 n = 0; 1379 aic->imp = &aic->imess[n]; 1380 1381 nextbyte: 1382 /* 1383 * Read a whole message, but don't ack the last byte. If we reject the 1384 * message, we have to assert ATN during the message transfer phase 1385 * itself. 1386 */ 1387 for (;;) { 1388 for (;;) { 1389 u_short phase = aicphase(aic); 1390 if (phase == PH_MSGI) 1391 break; 1392 if (phase != PH_INVALID) { 1393 /* 1394 * Target left MESSAGE IN, probably because it 1395 * a) noticed our ATN signal, or 1396 * b) ran out of messages. 1397 */ 1398 goto out; 1399 } 1400 /* Wait for REQINIT. XXX Need timeout. */ 1401 } 1402 1403 /* XXX parity */ 1404 1405 /* Gather incoming message bytes if needed. */ 1406 if ((aic->flags & AIC_DROP_MSGI) == 0) { 1407 if (n >= AIC_MAX_MSG_LEN) { 1408 aic_sched_msgout(SEND_REJECT); 1409 aic->flags |= AIC_DROP_MSGI; 1410 } else { 1411 *aic->imp++ = inb(SCSIDAT); 1412 n++; 1413 /* 1414 * This testing is suboptimal, but most 1415 * messages will be of the one byte variety, so 1416 * it should not affect performance 1417 * significantly. 1418 */ 1419 if (IS1BYTEMSG(aic->imess[0]) && n == 1) 1420 break; 1421 if (IS2BYTEMSG(aic->imess[0]) && n == 2) 1422 break; 1423 if (ISEXTMSG(aic->imess[0]) && n >= 3 && 1424 n == aic->imess[1] + 2) 1425 break; 1426 } 1427 } 1428 1429 /* 1430 * If we reach this spot we're either: 1431 * a) in the middle of a multi-byte message, or 1432 * b) dropping bytes. 1433 */ 1434 outb(SXFRCTL0, CHEN|SPIOEN); 1435 /* Ack the last byte read. */ 1436 (void) inb(SCSIDAT); 1437 outb(SXFRCTL0, CHEN); 1438 } 1439 1440 aic->progress = 1; 1441 AIC_MISC(("n=%d imess=0x%02x ", n, aic->imess[0])); 1442 1443 /* We now have a complete message. Parse it. */ 1444 switch (aic->state) { 1445 struct acb *acb; 1446 struct scsi_link *sc; 1447 struct aic_tinfo *ti; 1448 int period, offset, width; 1449 u_char selid, target, lun; 1450 1451 case AIC_CONNECTED: 1452 AIC_ASSERT(aic->nexus != NULL); 1453 acb = aic->nexus; 1454 ti = &aic->tinfo[acb->xs->sc_link->target]; 1455 1456 switch (aic->imess[0]) { 1457 case MSG_CMDCOMPLETE: 1458 if (aic->dleft < 0) { 1459 sc = acb->xs->sc_link; 1460 printf("%s: %d extra bytes from %d:%d\n", 1461 aic->sc_dev.dv_xname, 1462 -aic->dleft, sc->target, sc->lun); 1463 acb->dleft = 0; 1464 } 1465 acb->xs->resid = acb->dleft = aic->dleft; 1466 aic->state = AIC_CMDCOMPLETE; 1467 break; 1468 1469 case MSG_PARITY_ERROR: 1470 /* Resend the last message. */ 1471 aic_sched_msgout(aic->msgout); 1472 break; 1473 1474 case MSG_MESSAGE_REJECT: 1475 AIC_MISC(("message rejected ")); 1476 switch (aic->msgout) { 1477 case SEND_IDENTIFY: 1478 ti->flags &= ~(DO_SYNC|DO_WIDE); 1479 ti->syncdata = 0; 1480 outb(SCSIRATE, ti->syncdata); 1481 /* ... */ 1482 break; 1483 case SEND_SDTR: 1484 ti->flags &= ~DO_SYNC; 1485 ti->syncdata = 0; 1486 outb(SCSIRATE, ti->syncdata); 1487 break; 1488 case SEND_WDTR: 1489 ti->flags &= ~DO_WIDE; 1490 /* ... */ 1491 break; 1492 case SEND_INIT_DET_ERR: 1493 aic->flags |= AIC_ABORTING; 1494 aic_sched_msgout(SEND_ABORT); 1495 break; 1496 } 1497 break; 1498 1499 case MSG_NOOP: 1500 break; 1501 1502 case MSG_DISCONNECT: 1503 ti->dconns++; 1504 aic->state = AIC_DISCONNECT; 1505 break; 1506 1507 case MSG_SAVEDATAPOINTER: 1508 acb->dp = aic->dp; 1509 acb->dleft = aic->dleft; 1510 break; 1511 1512 case MSG_RESTOREPOINTERS: 1513 aic->dp = acb->dp; 1514 aic->dleft = acb->dleft; 1515 aic->cp = (u_char *)&acb->cmd; 1516 aic->cleft = acb->clen; 1517 break; 1518 1519 case MSG_EXTENDED: 1520 switch (aic->imess[2]) { 1521 case MSG_EXT_SDTR: 1522 if (aic->imess[1] != 3) 1523 goto reject; 1524 period = (aic->imess[3] * 4 + 49)/50 - 2; 1525 offset = aic->imess[4]; 1526 if (offset == 0) { 1527 ti->flags &= ~DO_SYNC; 1528 ti->syncdata = 0; 1529 outb(SCSIRATE, ti->syncdata); 1530 } else if (period > 7) { 1531 /* Too slow for aic6360. Do asynch 1532 * instead. Renegotiate the deal. 1533 */ 1534 ti->period = 0; 1535 ti->offset = 0; 1536 aic_sched_msgout(SEND_SDTR); 1537 } else { 1538 ti->flags &= ~DO_SYNC; 1539 ti->syncdata = period<<4 | offset; 1540 outb(SCSIRATE, ti->syncdata); 1541 } 1542 break; 1543 1544 case MSG_EXT_WDTR: 1545 if (aic->imess[1] != 2) 1546 goto reject; 1547 width = aic->imess[3]; 1548 if (width == 0) { 1549 ti->flags &= ~DO_WIDE; 1550 /* ... */ 1551 } else if (width > AIC_MAX_WIDTH) { 1552 ti->width = 0; 1553 aic_sched_msgout(SEND_WDTR); 1554 } else { 1555 ti->flags &= ~DO_WIDE; 1556 /* ... */ 1557 } 1558 break; 1559 1560 default: 1561 printf("aic at line %d: unrecognized MESSAGE IN; sending REJECT\n", __LINE__); 1562 AIC_BREAK(); 1563 goto reject; 1564 } 1565 break; 1566 1567 default: 1568 printf("aic at line %d: unrecognized MESSAGE IN; sending REJECT\n", __LINE__); 1569 AIC_BREAK(); 1570 reject: 1571 aic_sched_msgout(SEND_REJECT); 1572 break; 1573 } 1574 break; 1575 1576 case AIC_RESELECTED: 1577 if (!MSG_ISIDENT(aic->imess[0])) { 1578 printf("aic at line %d: reselect without IDENTIFY; sending DEVICE RESET\n", __LINE__); 1579 AIC_BREAK(); 1580 goto reset; 1581 } 1582 1583 /* 1584 * The SCSI chip made a snapshot of the data bus while the 1585 * reselection was being negotiated. This enables us to 1586 * determine which target did the reselect. 1587 */ 1588 selid = inb(SELID) & ~(1<<AIC_SCSI_HOSTID); 1589 if (selid & (selid - 1)) { 1590 printf("aic at line %d: reselect with invalid selid %02x; sending DEVICE RESET\n", __LINE__, selid); 1591 AIC_BREAK(); 1592 goto reset; 1593 } 1594 1595 /* Search wait queue for disconnected cmd 1596 * The list should be short, so I haven't bothered with 1597 * any more sophisticated structures than a simple 1598 * singly linked list. 1599 */ 1600 target = ffs(selid) - 1; 1601 lun = aic->imess[0] & 0x07; 1602 for (acb = aic->nexus_list.tqh_first; acb != NULL; 1603 acb = acb->chain.tqe_next) { 1604 sc = acb->xs->sc_link; 1605 if (sc->target == target && sc->lun == lun) 1606 break; 1607 } 1608 if (acb == NULL) { 1609 printf("aic at line %d: reselect from target %d lun %d with no nexus; sending DEVICE RESET\n", __LINE__, target, lun); 1610 AIC_BREAK(); 1611 goto reset; 1612 } 1613 1614 /* Make this nexus active again. */ 1615 TAILQ_REMOVE(&aic->nexus_list, acb, chain); 1616 aic->state = AIC_CONNECTED; 1617 aic->nexus = acb; 1618 ti = &aic->tinfo[sc->target]; 1619 ti->lubusy |= (1<<sc->lun); 1620 outb(SCSIRATE, ti->syncdata); 1621 1622 /* Do an implicit RESTORE POINTERS. */ 1623 aic->dp = acb->dp; 1624 aic->dleft = acb->dleft; 1625 aic->cp = (u_char *)&acb->cmd; 1626 aic->cleft = acb->clen; 1627 break; 1628 1629 default: 1630 printf("aic at line %d: unexpected MESSAGE IN; sending DEVICE RESET\n", __LINE__); 1631 AIC_BREAK(); 1632 reset: 1633 aic->flags |= AIC_ABORTING; 1634 aic_sched_msgout(SEND_DEV_RESET); 1635 break; 1636 } 1637 1638 outb(SXFRCTL0, CHEN|SPIOEN); 1639 /* Ack the last message byte. */ 1640 (void) inb(SCSIDAT); 1641 outb(SXFRCTL0, CHEN); 1642 1643 /* Go get the next message, if any. */ 1644 goto nextmsg; 1645 1646 out: 1647 AIC_MISC(("n=%d imess=0x%02x ", n, aic->imess[0])); 1648 } 1649 1650 1651 /* The message out (and in) stuff is a bit complicated: 1652 * If the target requests another message (sequence) without 1653 * having changed phase in between it really asks for a 1654 * retransmit, probably due to parity error(s). 1655 * The following messages can be sent: 1656 * IDENTIFY @ These 4 stem from SCSI command activity 1657 * SDTR @ 1658 * WDTR @ 1659 * DEV_RESET @ 1660 * REJECT if MSGI doesn't make sense 1661 * PARITY_ERROR if parity error while in MSGI 1662 * INIT_DET_ERR if parity error while not in MSGI 1663 * ABORT if INIT_DET_ERR rejected 1664 * NOOP if asked for a message and there's nothing to send 1665 */ 1666 void 1667 aic_msgout(aic) 1668 register struct aic_softc *aic; 1669 { 1670 register int iobase = aic->iobase; 1671 struct acb *acb; 1672 struct aic_tinfo *ti; 1673 int n; 1674 1675 AIC_TRACE(("aic_msgout ")); 1676 aic->progress = 0; 1677 1678 /* 1679 * Set ATN. If we're just sending a trivial 1-byte message, we'll 1680 * clear ATN later on anyway. 1681 */ 1682 outb(SCSISIGO, PH_MSGO|ATNO); 1683 /* Reset the FIFO. */ 1684 outb(DMACNTRL0, RSTFIFO); 1685 /* Enable REQ/ACK protocol. */ 1686 outb(SXFRCTL0, CHEN|SPIOEN); 1687 1688 if (aic->prevphase == PH_MSGO) { 1689 if (aic->omp == aic->omess) { 1690 /* 1691 * This is a retransmission. 1692 * 1693 * We get here if the target stayed in MESSAGE OUT 1694 * phase. Section 5.1.9.2 of the SCSI 2 spec indicates 1695 * that all of the previously transmitted messages must 1696 * be sent again, in the same order. Therefore, we 1697 * requeue all the previously transmitted messages, and 1698 * start again from the top. Our simple priority 1699 * scheme keeps the messages in the right order. 1700 */ 1701 AIC_MISC(("retransmitting ")); 1702 aic->msgpriq |= aic->msgoutq; 1703 } else { 1704 /* This is a continuation of the previous message. */ 1705 n = aic->omp - aic->omess; 1706 goto nextbyte; 1707 } 1708 } 1709 1710 /* No messages transmitted so far. */ 1711 aic->msgoutq = 0; 1712 1713 nextmsg: 1714 /* Pick up highest priority message. */ 1715 aic->msgout = aic->msgpriq & -aic->msgpriq; 1716 aic->msgpriq &= ~aic->msgout; 1717 aic->msgoutq |= aic->msgout; 1718 1719 /* Build the outgoing message data. */ 1720 switch (aic->msgout) { 1721 case SEND_IDENTIFY: 1722 if (aic->state != AIC_CONNECTED) { 1723 printf("aic at line %d: SEND_IDENTIFY while not connected; sending NOOP\n", __LINE__); 1724 AIC_BREAK(); 1725 goto noop; 1726 } 1727 AIC_ASSERT(aic->nexus != NULL); 1728 acb = aic->nexus; 1729 aic->omess[0] = MSG_IDENTIFY(acb->xs->sc_link->lun); 1730 n = 1; 1731 break; 1732 1733 case SEND_SDTR: 1734 if (aic->state != AIC_CONNECTED) { 1735 printf("aic at line %d: SEND_SDTR while not connected; sending NOOP\n", __LINE__); 1736 AIC_BREAK(); 1737 goto noop; 1738 } 1739 AIC_ASSERT(aic->nexus != NULL); 1740 ti = &aic->tinfo[aic->nexus->xs->sc_link->target]; 1741 aic->omess[4] = MSG_EXTENDED; 1742 aic->omess[3] = 3; 1743 aic->omess[2] = MSG_EXT_SDTR; 1744 aic->omess[1] = ti->period >> 2; 1745 aic->omess[0] = ti->offset; 1746 n = 5; 1747 break; 1748 1749 case SEND_WDTR: 1750 if (aic->state != AIC_CONNECTED) { 1751 printf("aic at line %d: SEND_WDTR while not connected; sending NOOP\n", __LINE__); 1752 AIC_BREAK(); 1753 goto noop; 1754 } 1755 AIC_ASSERT(aic->nexus != NULL); 1756 ti = &aic->tinfo[aic->nexus->xs->sc_link->target]; 1757 aic->omess[3] = MSG_EXTENDED; 1758 aic->omess[2] = 2; 1759 aic->omess[1] = MSG_EXT_WDTR; 1760 aic->omess[0] = ti->width; 1761 n = 4; 1762 break; 1763 1764 case SEND_DEV_RESET: 1765 aic->omess[0] = MSG_BUS_DEV_RESET; 1766 n = 1; 1767 break; 1768 1769 case SEND_REJECT: 1770 aic->omess[0] = MSG_MESSAGE_REJECT; 1771 n = 1; 1772 break; 1773 1774 case SEND_PARITY_ERROR: 1775 aic->omess[0] = MSG_PARITY_ERROR; 1776 n = 1; 1777 break; 1778 1779 case SEND_INIT_DET_ERR: 1780 aic->omess[0] = MSG_INITIATOR_DET_ERR; 1781 n = 1; 1782 break; 1783 1784 case SEND_ABORT: 1785 aic->omess[0] = MSG_ABORT; 1786 n = 1; 1787 break; 1788 1789 case 0: 1790 printf("aic at line %d: unexpected MESSAGE OUT; sending NOOP\n", __LINE__); 1791 noop: 1792 aic->omess[0] = MSG_NOOP; 1793 n = 1; 1794 break; 1795 1796 default: 1797 printf("aic at line %d: weird MESSAGE OUT; sending NOOP\n", __LINE__); 1798 AIC_BREAK(); 1799 goto noop; 1800 } 1801 aic->omp = &aic->omess[n]; 1802 1803 nextbyte: 1804 /* Send message bytes. */ 1805 for (;;) { 1806 for (;;) { 1807 u_short phase = aicphase(aic); 1808 if (phase == PH_MSGO) 1809 break; 1810 if (phase != PH_INVALID) { 1811 /* 1812 * Target left MESSAGE OUT, possibly to reject 1813 * our message. 1814 */ 1815 goto out; 1816 } 1817 /* Wait for REQINIT. XXX Need timeout. */ 1818 } 1819 1820 --n; 1821 1822 /* Clear ATN before last byte if this is the last message. */ 1823 if (n == 0 && aic->msgpriq == 0) 1824 outb(CLRSINT1, CLRATNO); 1825 /* Send message byte. */ 1826 outb(SCSIDAT, *--aic->omp); 1827 /* Wait for ACK to be negated. XXX Need timeout. */ 1828 while ((inb(SCSISIGI) & ACKI) != 0) 1829 ; 1830 1831 if (n == 0) 1832 break; 1833 } 1834 1835 aic->progress = 1; 1836 1837 /* We get here only if the entire message has been transmitted. */ 1838 if (aic->msgpriq != 0) { 1839 /* There are more outgoing messages. */ 1840 goto nextmsg; 1841 } 1842 1843 /* 1844 * The last message has been transmitted. We need to remember the last 1845 * message transmitted (in case the target switches to MESSAGE IN phase 1846 * and sends a MESSAGE REJECT), and the list of messages transmitted 1847 * this time around (in case the target stays in MESSAGE OUT phase to 1848 * request a retransmit). 1849 */ 1850 1851 out: 1852 /* Disable REQ/ACK protocol. */ 1853 outb(SXFRCTL0, CHEN); 1854 } 1855 1856 /* aic_dataout_pio: perform a data transfer using the FIFO datapath in the aic6360 1857 * Precondition: The SCSI bus should be in the DOUT phase, with REQ asserted 1858 * and ACK deasserted (i.e. waiting for a data byte) 1859 * This new revision has been optimized (I tried) to make the common case fast, 1860 * and the rarer cases (as a result) somewhat more comlex 1861 */ 1862 int 1863 aic_dataout_pio(aic, p, n) 1864 register struct aic_softc *aic; 1865 u_char *p; 1866 int n; 1867 { 1868 register int iobase = aic->iobase; 1869 register u_char dmastat; 1870 int out = 0; 1871 #define DOUTAMOUNT 128 /* Full FIFO */ 1872 1873 /* Clear FIFOs and counters. */ 1874 outb(SXFRCTL0, CHEN|CLRSTCNT|CLRCH); 1875 outb(DMACNTRL0, RSTFIFO|WRITE); 1876 /* Enable FIFOs. */ 1877 outb(SXFRCTL0, SCSIEN|DMAEN|CHEN); 1878 outb(DMACNTRL0, ENDMA|DWORDPIO|WRITE); 1879 1880 /* Setup to detect: 1881 * PHASEMIS & PHASECHG: target has left the DOUT phase 1882 * SCSIRST: something just pulled the RST line. 1883 * BUSFREE: target has unexpectedly left the DOUT phase 1884 */ 1885 outb(SIMODE1, ENPHASEMIS|ENSCSIRST|ENBUSFREE|ENPHASECHG); 1886 1887 /* I have tried to make the main loop as tight as possible. This 1888 * means that some of the code following the loop is a bit more 1889 * complex than otherwise. 1890 */ 1891 while (n > 0) { 1892 int xfer; 1893 1894 for (;;) { 1895 dmastat = inb(DMASTAT); 1896 if ((dmastat & DFIFOEMP) != 0) 1897 break; 1898 if ((dmastat & INTSTAT) != 0) 1899 goto phasechange; 1900 } 1901 1902 xfer = min(DOUTAMOUNT, n); 1903 1904 n -= xfer; 1905 out += xfer; 1906 1907 #if AIC_USE_DWORDS 1908 if (xfer >= 12) { 1909 outsl(DMADATALONG, p, xfer>>2); 1910 p += xfer & ~3; 1911 xfer &= 3; 1912 } 1913 #else 1914 if (xfer >= 8) { 1915 outsw(DMADATA, p, xfer>>1); 1916 p += xfer & ~1; 1917 xfer &= 1; 1918 } 1919 #endif 1920 1921 if (xfer > 0) { 1922 outb(DMACNTRL0, ENDMA|B8MODE|WRITE); 1923 outsb(DMADATA, p, xfer); 1924 p += xfer; 1925 outb(DMACNTRL0, ENDMA|DWORDPIO|WRITE); 1926 } 1927 } 1928 1929 /* See the bytes off chip */ 1930 for (;;) { 1931 dmastat = inb(DMASTAT); 1932 if ((dmastat & DFIFOEMP) != 0 && 1933 (inb(SSTAT2) & SEMPTY) != 0) 1934 break; 1935 if ((dmastat & INTSTAT) != 0) 1936 goto phasechange; 1937 } 1938 1939 phasechange: 1940 /* We now have the data off chip. */ 1941 outb(SXFRCTL0, CHEN); 1942 1943 if ((dmastat & INTSTAT) != 0) { 1944 /* Some sort of phase change. */ 1945 register u_char sstat2; 1946 int amount; 1947 1948 /* Stop transfers, do some accounting */ 1949 amount = inb(FIFOSTAT); 1950 sstat2 = inb(SSTAT2); 1951 if ((sstat2 & 7) == 0) 1952 amount += sstat2 & SFULL ? 8 : 0; 1953 else 1954 amount += sstat2 & 7; 1955 out -= amount; 1956 AIC_MISC(("+%d ", amount)); 1957 } 1958 1959 outb(DMACNTRL0, RSTFIFO); 1960 while ((inb(SXFRCTL0) & SCSIEN) != 0) 1961 ; 1962 1963 outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT); 1964 aic->progress = out != 0; 1965 1966 return out; 1967 } 1968 1969 /* aic_datain_pio: perform data transfers using the FIFO datapath in the aic6360 1970 * Precondition: The SCSI bus should be in the DIN phase, with REQ asserted 1971 * and ACK deasserted (i.e. at least one byte is ready). 1972 * For now, uses a pretty dumb algorithm, hangs around until all data has been 1973 * transferred. This, is OK for fast targets, but not so smart for slow 1974 * targets which don't disconnect or for huge transfers. 1975 */ 1976 int 1977 aic_datain_pio(aic, p, n) 1978 register struct aic_softc *aic; 1979 u_char *p; 1980 int n; 1981 { 1982 register int iobase = aic->iobase; 1983 register u_char dmastat; 1984 int in = 0; 1985 #define DINAMOUNT 128 /* Full FIFO */ 1986 1987 /* Clear FIFOs and counters */ 1988 outb(SXFRCTL0, CHEN|CLRSTCNT|CLRCH); 1989 outb(DMACNTRL0, RSTFIFO); 1990 /* Enable FIFOs */ 1991 outb(SXFRCTL0, SCSIEN|DMAEN|CHEN); 1992 outb(DMACNTRL0, ENDMA|DWORDPIO); 1993 1994 /* Setup to detect: 1995 * PHASEMIS & PHASECHG: target has left the DOUT phase 1996 * SCSIRST: something just pulled the RST line. 1997 * BUSFREE: target has unexpectedly left the DOUT phase 1998 */ 1999 outb(SIMODE1, ENPHASEMIS|ENSCSIRST|ENBUSFREE|ENPHASECHG); 2000 2001 /* We leave this loop if one or more of the following is true: 2002 * a) phase != PH_DIN && FIFOs are empty 2003 * b) SCSIRSTI is set (a reset has occurred) or busfree is detected. 2004 */ 2005 while (n > 0) { 2006 int xfer; 2007 2008 /* Wait for fifo half full or phase mismatch */ 2009 for (;;) { 2010 dmastat = inb(DMASTAT); 2011 if ((dmastat & (DFIFOFULL|INTSTAT)) != 0) 2012 break; 2013 } 2014 2015 if ((dmastat & DFIFOFULL) != 0) 2016 xfer = DINAMOUNT; 2017 else { 2018 while ((inb(SSTAT2) & SEMPTY) == 0) 2019 ; 2020 xfer = inb(FIFOSTAT); 2021 } 2022 2023 xfer = min(xfer, n); 2024 2025 n -= xfer; 2026 in += xfer; 2027 2028 #if AIC_USE_DWORDS 2029 if (xfer >= 12) { 2030 insl(DMADATALONG, p, xfer>>2); 2031 p += xfer & ~3; 2032 xfer &= 3; 2033 } 2034 #else 2035 if (xfer >= 8) { 2036 insw(DMADATA, p, xfer>>1); 2037 p += xfer & ~1; 2038 xfer &= 1; 2039 } 2040 #endif 2041 2042 if (xfer > 0) { 2043 outb(DMACNTRL0, ENDMA|B8MODE); 2044 insb(DMADATA, p, xfer); 2045 p += xfer; 2046 outb(DMACNTRL0, ENDMA|DWORDPIO); 2047 } 2048 2049 if ((dmastat & INTSTAT) != 0) 2050 break; 2051 } 2052 2053 #if 0 2054 if (n > 0) 2055 printf("residual %d\n", n); 2056 #endif 2057 2058 /* Some SCSI-devices are rude enough to transfer more data than what 2059 * was requested, e.g. 2048 bytes from a CD-ROM instead of the 2060 * requested 512. Test for progress, i.e. real transfers. If no real 2061 * transfers have been performed (n is probably already zero) and the 2062 * FIFO is not empty, waste some bytes.... 2063 */ 2064 if (in == 0) { 2065 int extra = 0; 2066 2067 for (;;) { 2068 dmastat = inb(DMASTAT); 2069 if ((dmastat & DFIFOEMP) != 0) 2070 break; 2071 (void) inb(DMADATA); /* Throw it away */ 2072 extra++; 2073 } 2074 2075 AIC_MISC(("aic: %d extra bytes\n", extra)); 2076 aic->progress = extra != 0; 2077 } else 2078 aic->progress = 1; 2079 2080 /* Stop the FIFO data path */ 2081 outb(SXFRCTL0, CHEN); 2082 2083 outb(DMACNTRL0, RSTFIFO); 2084 while ((inb(SXFRCTL0) & SCSIEN) != 0) 2085 ; 2086 2087 outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT); 2088 2089 return in; 2090 } 2091 2092 /* 2093 * This is the workhorse routine of the driver. 2094 * Deficiencies (for now): 2095 * 1) always uses programmed I/O 2096 * 2) doesn't support synchronous transfers properly (yet) 2097 */ 2098 int 2099 aicintr(aic) 2100 register struct aic_softc *aic; 2101 { 2102 register int iobase = aic->iobase; 2103 u_char sstat0, sstat1; 2104 u_short phase; 2105 register struct acb *acb; 2106 register struct scsi_link *sc; 2107 struct aic_tinfo *ti; 2108 int n; 2109 2110 /* 2111 * Clear INTEN. We enable it again before returning. This ensures 2112 * that we get another edge on the next `interesting' event. 2113 */ 2114 outb(DMACNTRL0, 0); 2115 AIC_TRACE(("aicintr ")); 2116 2117 /* 2118 * First check for abnormal conditions, such as reset. 2119 */ 2120 sstat1 = inb(SSTAT1); 2121 AIC_MISC(("sstat1:0x%02x ", sstat1)); 2122 if ((sstat1 & SCSIRSTI) != 0) { 2123 printf("aic: reset in -- reinitializing....\n"); 2124 goto reset; 2125 } 2126 if ((sstat1 & SCSIPERR) != 0) { 2127 printf("aic: SCSI bus parity error\n"); 2128 outb(CLRSINT1, CLRSCSIPERR); 2129 if (aic->prevphase == PH_MSGI) { 2130 aic_sched_msgout(SEND_PARITY_ERROR); 2131 aic->flags |= AIC_DROP_MSGI; 2132 } else 2133 aic_sched_msgout(SEND_INIT_DET_ERR); 2134 } 2135 2136 /* 2137 * If we're not already busy doing something test for the following 2138 * conditions: 2139 * 1) We have been reselected by something 2140 * 2) We have selected something successfully 2141 * 3) Our selection process has timed out 2142 * 4) This is really a bus free interrupt just to get a new command 2143 * going? 2144 * 5) Spurious interrupt? 2145 */ 2146 sstat0 = inb(SSTAT0); 2147 AIC_MISC(("sstat0:0x%02x ", sstat0)); 2148 2149 switch (aic->state) { 2150 case AIC_IDLE: 2151 case AIC_SELECTING: 2152 if ((sstat0 & SELDI) != 0) { 2153 AIC_MISC(("reselected ")); 2154 2155 /* 2156 * If we're trying to select a target ourselves, 2157 * push our command back into the ready list. 2158 */ 2159 if (aic->state == AIC_SELECTING) { 2160 AIC_MISC(("backoff selector ")); 2161 AIC_ASSERT(aic->nexus != NULL); 2162 acb = aic->nexus; 2163 aic->nexus = NULL; 2164 TAILQ_INSERT_HEAD(&aic->ready_list, acb, chain); 2165 } 2166 2167 /* 2168 * Turn off selection stuff, and prepare to catch bus 2169 * free interrupts and parity errors. 2170 */ 2171 outb(SXFRCTL1, 0); 2172 outb(SCSISEQ, ENAUTOATNP); 2173 outb(CLRSINT0, CLRSELDI); 2174 outb(CLRSINT1, CLRBUSFREE|CLRSCSIPERR); 2175 outb(SIMODE0, 0); 2176 outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENREQINIT); 2177 2178 aic->state = AIC_RESELECTED; 2179 } else if ((sstat0 & SELDO) != 0) { 2180 AIC_MISC(("selected ")); 2181 2182 /* We have selected a target. Things to do: 2183 * a) Determine what message(s) to send. 2184 * b) Verify that we're still selecting the target. 2185 * c) Mark device as busy. 2186 */ 2187 if (aic->state != AIC_SELECTING) { 2188 printf("aic at line %d: selection out while not selecting; resetting\n", __LINE__); 2189 AIC_BREAK(); 2190 goto reset; 2191 } 2192 AIC_ASSERT(aic->nexus != NULL); 2193 acb = aic->nexus; 2194 2195 sc = acb->xs->sc_link; 2196 ti = &aic->tinfo[sc->target]; 2197 if ((acb->xs->flags & SCSI_RESET) == 0) { 2198 aic->msgpriq = SEND_IDENTIFY; 2199 if ((ti->flags & DO_SYNC) != 0) 2200 aic->msgpriq |= SEND_SDTR; 2201 if ((ti->flags & DO_WIDE) != 0) 2202 aic->msgpriq |= SEND_WDTR; 2203 } else 2204 aic->msgpriq = SEND_DEV_RESET; 2205 2206 /* 2207 * Turn off selection stuff, and prepare to catch bus 2208 * free interrupts and parity errors. 2209 */ 2210 outb(SXFRCTL1, 0); 2211 outb(SCSISEQ, ENAUTOATNP); 2212 outb(CLRSINT0, CLRSELDO); 2213 outb(CLRSINT1, CLRBUSFREE|CLRSCSIPERR); 2214 outb(SIMODE0, 0); 2215 outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENREQINIT); 2216 2217 ti->lubusy |= (1<<sc->lun); 2218 2219 /* Do an implicit RESTORE POINTERS. */ 2220 aic->dp = acb->dp; 2221 aic->dleft = acb->dleft; 2222 aic->cp = (u_char *)&acb->cmd; 2223 aic->cleft = acb->clen; 2224 2225 aic->state = AIC_CONNECTED; 2226 } else if ((sstat1 & SELTO) != 0) { 2227 AIC_MISC(("selection timeout ")); 2228 2229 if (aic->state != AIC_SELECTING) { 2230 printf("aic at line %d: selection timeout while not selecting; resetting\n", __LINE__); 2231 AIC_BREAK(); 2232 goto reset; 2233 } 2234 AIC_ASSERT(aic->nexus != NULL); 2235 acb = aic->nexus; 2236 2237 outb(SXFRCTL1, 0); 2238 outb(CLRSINT1, CLRSELTIMO); 2239 2240 acb->xs->error = XS_TIMEOUT; 2241 untimeout(aic_timeout, acb); 2242 delay(250); 2243 aic_done(acb); 2244 goto out; 2245 } else { 2246 if (aic->state != AIC_IDLE) { 2247 printf("aic at line %d: BUS FREE while not idle; state=%d\n", __LINE__, aic->state); 2248 AIC_BREAK(); 2249 goto out; 2250 } 2251 2252 aic_sched(aic); 2253 goto out; 2254 } 2255 2256 aic->flags = 0; 2257 aic->prevphase = PH_INVALID; 2258 break; 2259 } 2260 2261 phase = aicphase(aic); 2262 AIC_MISC(("phase=0x%02x ", phase)); 2263 if ((phase & PH_PSBIT) == 0) 2264 outb(SCSISIGO, phase); 2265 outb(CLRSINT1, CLRPHASECHG|CLRBUSFREE); 2266 2267 switch (phase) { 2268 case PH_BUSFREE: 2269 switch (aic->state) { 2270 case AIC_RESELECTED: 2271 aic->state = AIC_IDLE; 2272 aic_sched(aic); 2273 goto out; 2274 2275 case AIC_CONNECTED: 2276 if ((aic->flags & AIC_ABORTING) == 0) { 2277 printf("aic at line %d: unexpected BUS FREE; aborting\n", __LINE__); 2278 AIC_BREAK(); 2279 } 2280 AIC_ASSERT(aic->nexus != NULL); 2281 acb = aic->nexus; 2282 acb->xs->error = XS_DRIVER_STUFFUP; 2283 goto finish; 2284 2285 case AIC_DISCONNECT: 2286 AIC_ASSERT(aic->nexus != NULL); 2287 acb = aic->nexus; 2288 aic->state = AIC_IDLE; 2289 aic->nexus = NULL; 2290 TAILQ_INSERT_HEAD(&aic->nexus_list, acb, chain); 2291 aic_sched(aic); 2292 goto out; 2293 2294 case AIC_CMDCOMPLETE: 2295 AIC_ASSERT(aic->nexus != NULL); 2296 acb = aic->nexus; 2297 finish: 2298 untimeout(aic_timeout, acb); 2299 aic_done(acb); 2300 goto out; 2301 } 2302 2303 case PH_INVALID: 2304 /* Wait for REQINIT. */ 2305 goto out; 2306 2307 case PH_MSGO: 2308 /* If aborting, always handle MESSAGE OUT. */ 2309 if ((aic->flags & AIC_ABORTING) == 0) 2310 break; 2311 aic_msgout(aic); 2312 goto nextphase; 2313 } 2314 2315 switch (aic->state) { 2316 case AIC_RESELECTED: 2317 switch (phase) { 2318 case PH_MSGI: 2319 aic_msgin(aic); 2320 goto nextphase; 2321 } 2322 break; 2323 2324 case AIC_CONNECTED: 2325 switch (phase) { 2326 case PH_MSGO: 2327 aic_msgout(aic); 2328 goto nextphase; 2329 2330 case PH_MSGI: 2331 aic_msgin(aic); 2332 goto nextphase; 2333 2334 case PH_CMD: /* CMD phase & REQ asserted */ 2335 #if AIC_DEBUG 2336 if ((aic_debug & AIC_SHOWMISC) != 0) { 2337 AIC_ASSERT(aic->nexus != NULL); 2338 acb = aic->nexus; 2339 printf("cmd=0x%02x+%d ", 2340 acb->cmd.opcode, acb->clen-1); 2341 } 2342 #endif 2343 n = aic_dataout_pio(aic, aic->cp, aic->cleft); 2344 aic->cp += n; 2345 aic->cleft -= n; 2346 goto nextphase; 2347 2348 case PH_DOUT: 2349 AIC_MISC(("dleft=%d ", aic->dleft)); 2350 n = aic_dataout_pio(aic, aic->dp, aic->dleft); 2351 aic->dp += n; 2352 aic->dleft -= n; 2353 goto nextphase; 2354 2355 case PH_DIN: 2356 n = aic_datain_pio(aic, aic->dp, aic->dleft); 2357 aic->dp += n; 2358 aic->dleft -= n; 2359 goto nextphase; 2360 2361 case PH_STAT: 2362 AIC_ASSERT(aic->nexus != NULL); 2363 acb = aic->nexus; 2364 outb(SXFRCTL0, CHEN|SPIOEN); 2365 outb(DMACNTRL0, RSTFIFO); 2366 outb(SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE|ENSCSIPERR); 2367 acb->stat = inb(SCSIDAT); 2368 outb(SXFRCTL0, CHEN); 2369 AIC_MISC(("stat=0x%02x ", acb->stat)); 2370 outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT); 2371 goto nextphase; 2372 } 2373 break; 2374 } 2375 2376 printf("aic at line %d: unexpected bus phase; resetting\n", __LINE__); 2377 AIC_BREAK(); 2378 reset: 2379 aic_init(aic); 2380 return 1; 2381 2382 nextphase: 2383 aic->prevphase = phase; 2384 2385 out: 2386 outb(DMACNTRL0, INTEN); 2387 return 1; 2388 } 2389 2390 void 2391 aic_timeout(arg) 2392 void *arg; 2393 { 2394 struct acb *acb = (struct acb *)arg; 2395 int s; 2396 2397 s = splbio(); 2398 2399 sc_print_addr(acb->xs->sc_link); 2400 printf("timed out\n"); 2401 2402 acb->xs->error = XS_TIMEOUT; 2403 aic_done(acb); 2404 2405 splx(s); 2406 } 2407 2408 #ifdef AIC_DEBUG 2409 /* 2410 * The following functions are mostly used for debugging purposes, either 2411 * directly called from the driver or from the kernel debugger. 2412 */ 2413 2414 void 2415 aic_show_scsi_cmd(acb) 2416 struct acb *acb; 2417 { 2418 u_char *b = (u_char *)&acb->cmd; 2419 struct scsi_link *sc = acb->xs->sc_link; 2420 int i; 2421 2422 sc_print_addr(sc); 2423 if ((acb->xs->flags & SCSI_RESET) == 0) { 2424 for (i = 0; i < acb->clen; i++) { 2425 if (i) 2426 printf(","); 2427 printf("%x", b[i]); 2428 } 2429 printf("\n"); 2430 } else 2431 printf("RESET\n"); 2432 } 2433 2434 void 2435 aic_print_acb(acb) 2436 struct acb *acb; 2437 { 2438 2439 printf("acb@%x xs=%x flags=%x", acb, acb->xs, acb->flags); 2440 printf(" dp=%x dleft=%d stat=%x\n", 2441 (long)acb->dp, acb->dleft, acb->stat); 2442 aic_show_scsi_cmd(acb); 2443 } 2444 2445 void 2446 aic_print_active_acb() 2447 { 2448 struct acb *acb; 2449 struct aic_softc *aic = aiccd.cd_devs[0]; 2450 2451 printf("ready list:\n"); 2452 for (acb = aic->ready_list.tqh_first; acb != NULL; 2453 acb = acb->chain.tqe_next) 2454 aic_print_acb(acb); 2455 printf("nexus:\n"); 2456 if (aic->nexus != NULL) 2457 aic_print_acb(aic->nexus); 2458 printf("nexus list:\n"); 2459 for (acb = aic->nexus_list.tqh_first; acb != NULL; 2460 acb = acb->chain.tqe_next) 2461 aic_print_acb(acb); 2462 } 2463 2464 void 2465 aic_dump6360(aic) 2466 struct aic_softc *aic; 2467 { 2468 int iobase = aic->iobase; 2469 2470 printf("aic6360: SCSISEQ=%x SXFRCTL0=%x SXFRCTL1=%x SCSISIGI=%x\n", 2471 inb(SCSISEQ), inb(SXFRCTL0), inb(SXFRCTL1), inb(SCSISIGI)); 2472 printf(" SSTAT0=%x SSTAT1=%x SSTAT2=%x SSTAT3=%x SSTAT4=%x\n", 2473 inb(SSTAT0), inb(SSTAT1), inb(SSTAT2), inb(SSTAT3), inb(SSTAT4)); 2474 printf(" SIMODE0=%x SIMODE1=%x DMACNTRL0=%x DMACNTRL1=%x DMASTAT=%x\n", 2475 inb(SIMODE0), inb(SIMODE1), inb(DMACNTRL0), inb(DMACNTRL1), 2476 inb(DMASTAT)); 2477 printf(" FIFOSTAT=%d SCSIBUS=0x%x\n", 2478 inb(FIFOSTAT), inb(SCSIBUS)); 2479 } 2480 2481 void 2482 aic_dump_driver(aic) 2483 struct aic_softc *aic; 2484 { 2485 struct aic_tinfo *ti; 2486 int i; 2487 2488 printf("nexus=%x prevphase=%x\n", aic->nexus, aic->prevphase); 2489 printf("state=%x msgin=%x msgpriq=%x msgoutq=%x msgout=%x\n", 2490 aic->state, aic->imess[0], aic->msgpriq, aic->msgoutq, aic->msgout); 2491 for (i = 0; i < 7; i++) { 2492 ti = &aic->tinfo[i]; 2493 printf("tinfo%d: %d cmds %d disconnects %d timeouts", 2494 i, ti->cmds, ti->dconns, ti->touts); 2495 printf(" %d senses flags=%x\n", ti->senses, ti->flags); 2496 } 2497 } 2498 #endif 2499