Home | History | Annotate | Line # | Download | only in ic
aic6360.c revision 1.15
      1 /*	$NetBSD: aic6360.c,v 1.15 1994/11/29 17:56:49 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 (at) tfs.com) and
     50  * Charles Hannum (mycroft (at) 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		1
    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 
    495 /* Grabbed from Julians SCSI aha-drivers */
    496 #ifdef	DDB
    497 int	Debugger();
    498 #else	DDB
    499 #define	Debugger() panic("should call debugger here (aic6360.c)")
    500 #endif	DDB
    501 
    502 typedef u_long physaddr;
    503 
    504 struct aic_dma_seg {
    505 	physaddr	addr;
    506 	long		len;
    507 };
    508 
    509 extern int delaycount;
    510 #define FUDGE(X)	((X)>>1) 	/* get 1 ms spincount */
    511 #define MINIFUDGE(X)	((X)>>4) 	/* get (approx) 125us spincount */
    512 #define AIC_NSEG	16
    513 #define NUM_CONCURRENT	7	/* Only one per target for now */
    514 
    515 /*
    516  * ACB. Holds additional information for each SCSI command Comments: We
    517  * need a separate scsi command block because we may need to overwrite it
    518  * with a request sense command.  Basicly, we refrain from fiddling with
    519  * the scsi_xfer struct (except do the expected updating of return values).
    520  * We'll generally update: xs->{flags,resid,error,sense,status} and
    521  * occasionally xs->retries.
    522  */
    523 struct acb {
    524 	TAILQ_ENTRY(acb) chain;
    525 	struct scsi_xfer *xs;	/* SCSI xfer ctrl block from above */
    526 	int		flags;	/* Status */
    527 #define ACB_FREE	0x00
    528 #define ACB_ACTIVE	0x01
    529 #define ACB_DONE	0x04
    530 #define ACB_CHKSENSE	0x08
    531 /*	struct aic_dma_seg dma[AIC_NSEG]; /* Physical addresses+len */
    532 	struct scsi_generic cmd;  /* SCSI command block */
    533 	int	 clen;
    534 	u_char	*dp;		/* Saved data pointer */
    535 	int	 dleft;		/* Residue */
    536 	int 	 stat;		/* SCSI status byte */
    537 };
    538 
    539 /*
    540  * Some info about each (possible) target on the SCSI bus.  This should
    541  * probably have been a "per target+lunit" structure, but we'll leave it at
    542  * this for now.  Is there a way to reliably hook it up to sc->fordriver??
    543  */
    544 struct aic_tinfo {
    545 	int	cmds;		/* #commands processed */
    546 	int	dconns;		/* #disconnects */
    547 	int	touts;		/* #timeouts */
    548 	int	perrs;		/* #parity errors */
    549 	int	senses;		/* #request sense commands sent */
    550 	ushort	lubusy;		/* What local units/subr. are busy? */
    551 	u_char  flags;
    552 #define DO_SYNC		0x01	/* (Re)Negotiate synchronous options */
    553 #define	DO_WIDE		0x02	/* (Re)Negotiate wide options */
    554 	u_char  period;		/* Period suggestion */
    555 	u_char  offset;		/* Offset suggestion */
    556 	u_char	width;		/* Width suggestion */
    557 	u_char  syncdata;	/* True negotiated synch parameters */
    558 } tinfo_t;
    559 
    560 struct aic_softc { /* One of these per adapter */
    561 	/* Auto config stuff */
    562 	struct device 	sc_dev;	/* This one has to go first! */
    563 	struct isadev	sc_id;
    564 	struct intrhand sc_ih;
    565 	struct scsi_link sc_link;	/* prototype for subdevs */
    566 	int		id_irq;		/* IRQ on the EISA bus */
    567 	int		id_drq;		/* DRQ on the EISA bus */
    568 	int		iobase;		/* Base I/O port */
    569 	/* Lists of command blocks */
    570 	TAILQ_HEAD(acb_list, acb) free_list, ready_list, nexus_list;
    571 	struct acb *nexus;	/* current command */
    572 	/* Command blocks and target info */
    573 	struct acb acb[NUM_CONCURRENT];
    574 	struct aic_tinfo tinfo[8];
    575 	/* Data about the current nexus (updated for every cmd switch) */
    576 	u_char	*dp;		/* Current data pointer */
    577 	int	 dleft;		/* Data bytes left to transfer */
    578 	u_char	*cp;		/* Current command pointer */
    579 	int	 cleft;		/* Command bytes left to transfer */
    580 	/* Adapter state */
    581 	short	 phase;		/* Copy of what bus phase we are in */
    582 	short	 prevphase;	/* Copy of what bus phase we were in */
    583 	short	 state;		/* State applicable to the adapter */
    584 #define AIC_IDLE	1
    585 #define AIC_SELECTING	2	/* SCSI command is arbiting  */
    586 #define AIC_RESELECTED	3	/* Has been reselected */
    587 #define AIC_CONNECTED	4	/* Actively using the SCSI bus */
    588 #define AIC_CLEANING	5
    589 	short	 flags;
    590 #define AIC_DROP_MSGI	0x01	/* Discard all msgs (parity err detected) */
    591 #define AIC_DOINGDMA	0x02	/* The FIFO data path is active! */
    592 #define AIC_BUSFREE_OK	0x04	/* Bus free phase is OK. */
    593 	/* Debugging stuff */
    594 #define AIC_HSIZE 8
    595 	u_char	progress;	/* Set if interrupt has achieved progress */
    596 	/* Message stuff */
    597 	u_char	msgpriq;	/* Messages we want to send */
    598 	u_char	msgoutq;	/* Messages sent during last MESSAGE OUT */
    599 	u_char	msgout;		/* Message last transmitted */
    600 #define SEND_DEV_RESET		0x01
    601 #define SEND_PARITY_ERROR	0x02
    602 #define SEND_ABORT		0x04
    603 #define SEND_REJECT		0x08
    604 #define SEND_INIT_DET_ERR	0x10
    605 #define SEND_IDENTIFY  		0x20
    606 #define SEND_SDTR		0x40
    607 #define	SEND_WDTR		0x80
    608 #define AIC_MAX_MSG_LEN 8
    609 	u_char  omess[AIC_MAX_MSG_LEN];	/* Scratch area for messages */
    610 	u_char	*omp;		/* Message pointer (for multibyte messages) */
    611 	u_char	imess[AIC_MAX_MSG_LEN];
    612 	u_char	*imp;		/* Message pointer (for multibyte messages) */
    613 };
    614 
    615 #if AIC_DEBUG
    616 #define AIC_SHOWACBS	0x01
    617 #define AIC_SHOWINTS	0x02
    618 #define AIC_SHOWCMDS	0x04
    619 #define AIC_SHOWMISC	0x08
    620 #define AIC_SHOWTRACE	0x10
    621 #define AIC_SHOWSTART	0x20
    622 #define AIC_DOBREAK	0x40
    623 int aic_debug = 0x00; /* AIC_SHOWSTART|AIC_SHOWMISC|AIC_SHOWTRACE; /**/
    624 #define	AIC_PRINT(b, s)	do {if ((aic_debug & (b)) != 0) printf s;} while (0)
    625 #define	AIC_BREAK()	do {if ((aic_debug & AIC_DOBREAK) != 0) Debugger();} while (0)
    626 #else
    627 #define	AIC_PRINT(b, s)
    628 #define	AIC_BREAK()
    629 #endif
    630 
    631 #define AIC_ACBS(s)	AIC_PRINT(AIC_SHOWACBS, s)
    632 #define AIC_INTS(s)	AIC_PRINT(AIC_SHOWINTS, s)
    633 #define AIC_CMDS(s)	AIC_PRINT(AIC_SHOWCMDS, s)
    634 #define AIC_MISC(s)	AIC_PRINT(AIC_SHOWMISC, s)
    635 #define AIC_TRACE(s)	AIC_PRINT(AIC_SHOWTRACE, s)
    636 #define AIC_START(s)	AIC_PRINT(AIC_SHOWSTART, s)
    637 
    638 int	aicprobe	__P((struct device *, void *, void *));
    639 void	aicattach	__P((struct device *, struct device *, void *));
    640 void	aic_minphys	__P((struct buf *));
    641 u_int	aic_adapter_info __P((struct aic_softc *));
    642 int	aicintr		__P((struct aic_softc *));
    643 void 	aic_init	__P((struct aic_softc *));
    644 void	aic_done	__P((struct acb *));
    645 int	aic_scsi_cmd	__P((struct scsi_xfer *));
    646 int	aic_poll	__P((struct aic_softc *, struct acb *));
    647 void	aic_add_timeout __P((struct acb *, int));
    648 void	aic_remove_timeout __P((struct acb *));
    649 void	aic_timeout	__P((void *arg));
    650 int	aic_find	__P((struct aic_softc *));
    651 void	aic_sched	__P((struct aic_softc *));
    652 void	aic_scsi_reset	__P((struct aic_softc *));
    653 #if AIC_DEBUG
    654 void	aic_print_active_acb();
    655 void	aic_dump_driver();
    656 void	aic_dump6360();
    657 #endif
    658 
    659 /* Linkup to the rest of the kernel */
    660 struct cfdriver aiccd = {
    661 	NULL, "aic", aicprobe, aicattach, DV_DULL, sizeof(struct aic_softc)
    662 };
    663 
    664 struct scsi_adapter aic_switch = {
    665 	aic_scsi_cmd,
    666 	aic_minphys,
    667 	0,
    668 	0,
    669 	aic_adapter_info,
    670 	"aic"
    671 };
    672 
    673 struct scsi_device aic_dev = {
    674 	NULL,			/* Use default error handler */
    675 	NULL,			/* have a queue, served by this */
    676 	NULL,			/* have no async handler */
    677 	NULL,			/* Use default 'done' routine */
    678 	"aic",
    679 	0
    680 };
    681 
    682 /*
    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 (at) 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 = 0;
    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->phase = 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 /*
    984  * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
    985  */
    986 
    987 /*
    988  * Expected sequence:
    989  * 1) Command inserted into ready list
    990  * 2) Command selected for execution
    991  * 3) Command won arbitration and has selected target device
    992  * 4) Send message out (identify message, eventually also sync.negotiations)
    993  * 5) Send command
    994  * 5a) Receive disconnect message, disconnect.
    995  * 5b) Reselected by target
    996  * 5c) Receive identify message from target.
    997  * 6) Send or receive data
    998  * 7) Receive status
    999  * 8) Receive message (command complete etc.)
   1000  * 9) If status == SCSI_CHECK construct a synthetic request sense SCSI cmd.
   1001  *    Repeat 2-8 (no disconnects please...)
   1002  */
   1003 
   1004 /*
   1005  * Start a SCSI-command
   1006  * This function is called by the higher level SCSI-driver to queue/run
   1007  * SCSI-commands.
   1008  */
   1009 int
   1010 aic_scsi_cmd(xs)
   1011 	struct scsi_xfer *xs;
   1012 {
   1013 	struct scsi_link *sc = xs->sc_link;
   1014 	struct aic_softc *aic = sc->adapter_softc;
   1015 	struct acb 	*acb;
   1016 	int s, flags;
   1017 	int iobase = aic->iobase;
   1018 
   1019 	AIC_TRACE(("aic_scsi_cmd  "));
   1020 	AIC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
   1021 	    sc->target));
   1022 
   1023 	flags = xs->flags;
   1024 
   1025 	if ((acb = aic_get_acb(aic, flags)) == NULL) {
   1026 		xs->error = XS_DRIVER_STUFFUP;
   1027 		return TRY_AGAIN_LATER;
   1028 	}
   1029 
   1030 	/* Initialize acb */
   1031 	acb->xs = xs;
   1032 	bcopy(xs->cmd, &acb->cmd, xs->cmdlen);
   1033 	acb->clen = xs->cmdlen;
   1034 	acb->dp = xs->data;
   1035 	acb->dleft = xs->datalen;
   1036 	acb->stat = 0;
   1037 
   1038 	if ((flags & SCSI_NOMASK) == 0)
   1039 		s = splbio();
   1040 
   1041 	TAILQ_INSERT_TAIL(&aic->ready_list, acb, chain);
   1042 	timeout(aic_timeout, acb, (xs->timeout*hz)/1000);
   1043 
   1044 	if (aic->state == AIC_IDLE)
   1045 		aic_sched(aic);
   1046 
   1047 	if ((flags & SCSI_NOMASK) == 0) { /* Almost done. Wait outside */
   1048 		splx(s);
   1049 		AIC_MISC(("SUCCESSFULLY_QUEUED"));
   1050 		return SUCCESSFULLY_QUEUED;
   1051 	}
   1052 
   1053 	/* Not allowed to use interrupts, use polling instead */
   1054 	return aic_poll(aic, acb);
   1055 }
   1056 
   1057 /*
   1058  * Adjust transfer size in buffer structure
   1059  */
   1060 void
   1061 aic_minphys(bp)
   1062 	struct buf *bp;
   1063 {
   1064 
   1065 	AIC_TRACE(("aic_minphys  "));
   1066 	if (bp->b_bcount > (AIC_NSEG << PGSHIFT))
   1067 		bp->b_bcount = (AIC_NSEG << PGSHIFT);
   1068 }
   1069 
   1070 
   1071 u_int
   1072 aic_adapter_info(aic)
   1073 	struct aic_softc *aic;
   1074 {
   1075 
   1076 	AIC_TRACE(("aic_adapter_info  "));
   1077 	return 2;		/* One outstanding command per target */
   1078 }
   1079 
   1080 /*
   1081  * Used when interrupt driven I/O isn't allowed, e.g. during boot.
   1082  */
   1083 int
   1084 aic_poll(aic, acb)
   1085 	struct aic_softc *aic;
   1086 	struct acb *acb;
   1087 {
   1088 	register int iobase = aic->iobase;
   1089 	struct scsi_xfer *xs = acb->xs;
   1090 	int count = xs->timeout * 10;
   1091 
   1092 	AIC_TRACE(("aic_poll  "));
   1093 	while (count) {
   1094 		if ((inb(DMASTAT) & INTSTAT) != 0)
   1095 			aicintr(aic);
   1096 		if ((xs->flags & ITSDONE) != 0)
   1097 			break;
   1098 		delay(100);
   1099 		count--;
   1100 	}
   1101 	if (count == 0) {
   1102 		AIC_MISC(("aic_poll: timeout"));
   1103 		aic_timeout((caddr_t)acb);
   1104 	}
   1105 	if (xs->error)
   1106 		return HAD_ERROR;
   1107 	return COMPLETE;
   1108 }
   1109 
   1110 /* LOW LEVEL SCSI UTILITIES */
   1112 
   1113 /* Determine the SCSI bus phase, return either a real SCSI bus phase or some
   1114  * pseudo phase we use to detect certain exceptions.  This one is a bit tricky.
   1115  * The bits we peek at:
   1116  * CDI, MSGI and DI is the 3 SCSI signals determining the bus phase.
   1117  * These should be qualified by REQI high and ACKI low.
   1118  * Also peek at SSTAT0[SELDO|SELDI] to detect a passing BUSFREE condition.
   1119  * No longer detect SCSI RESET or PERR here.  They are tested for separately
   1120  * in the interrupt handler.
   1121  * Note: If an exception occur at some critical time during the phase
   1122  * determination we'll most likely return something wildly erronous....
   1123  */
   1124 static inline u_short
   1125 aicphase(aic)
   1126 	struct aic_softc *aic;
   1127 {
   1128 	register int iobase = aic->iobase;
   1129 	register u_char sstat0, sstat1, scsisig;
   1130 
   1131 	sstat1 = inb(SSTAT1);	/* Look for REQINIT (REQ asserted) */
   1132 	scsisig = inb(SCSISIGI); /* Get the SCSI bus signals */
   1133 	sstat0 = inb(SSTAT0);	/* Get the selection valid status bits */
   1134 
   1135 	if ((sstat0 & (SELDO|SELDI)) == 0)	/* Selection became invalid? */
   1136 		return PH_BUSFREE;
   1137 
   1138 	/* Selection is still valid */
   1139 	if ((sstat1 & REQINIT) == 0) 		/* REQ not asserted ? */
   1140 		return PH_INVALID;
   1141 
   1142 	/* REQ is asserted, (and ACK is not) */
   1143 	return (scsisig & PH_MASK);
   1144 }
   1145 
   1146 /*
   1148  * Start a selection.  This is used by aic_sched() to select an idle target,
   1149  * and by aic_done() to immediately reselect a target to get sense information.
   1150  */
   1151 void
   1152 aic_select(aic, target)
   1153 	struct aic_softc *aic;
   1154 	int target;
   1155 {
   1156 	int iobase = aic->iobase;
   1157 
   1158 	outb(SCSIID, AIC_SCSI_HOSTID << OID_S | target);
   1159 	outb(SCSIRATE, aic->tinfo[target].syncdata);
   1160 	outb(SXFRCTL1, STIMO_256ms|ENSTIMER);
   1161 	/* Always enable reselections. */
   1162 	outb(SIMODE0, ENSELDI|ENSELDO);
   1163 	outb(SIMODE1, ENSCSIRST|ENSELTIMO);
   1164 	outb(SCSISEQ, ENRESELI|ENSELO|ENAUTOATNO);
   1165 
   1166 	aic->state = AIC_SELECTING;
   1167 }
   1168 
   1169 /*
   1171  * Schedule a SCSI operation.  This has now been pulled out of the interrupt
   1172  * handler so that we may call it from aic_scsi_cmd and aic_done.  This may
   1173  * save us an unecessary interrupt just to get things going.  Should only be
   1174  * called when state == AIC_IDLE and at bio pl.
   1175  */
   1176 void
   1177 aic_sched(aic)
   1178 	register struct aic_softc *aic;
   1179 {
   1180 	int iobase = aic->iobase;
   1181 	struct acb *acb;
   1182 	struct scsi_link *sc;
   1183 	struct aic_tinfo *ti;
   1184 
   1185 	/*
   1186 	 * Find first acb in ready queue that is for a target/lunit pair that
   1187 	 * is not busy.
   1188 	 */
   1189 	for (acb = aic->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
   1190 		sc = acb->xs->sc_link;
   1191 		ti = &aic->tinfo[sc->target];
   1192 		if ((ti->lubusy & (1<<sc->lun)) == 0) {
   1193 			AIC_MISC(("selecting %d:%d  ", sc->target, sc->lun));
   1194 			TAILQ_REMOVE(&aic->ready_list, acb, chain);
   1195 			aic->nexus = acb;
   1196 			aic_select(aic, sc->target);
   1197 			return;
   1198 		} else
   1199 			AIC_MISC(("%d:%d busy\n", sc->target, sc->lun));
   1200 	}
   1201 	AIC_MISC(("idle  "));
   1202 	/* Nothing to start; just enable reselections and wait. */
   1203 	outb(SIMODE0, ENSELDI);
   1204 	outb(SIMODE1, ENSCSIRST);
   1205 	outb(SCSISEQ, ENRESELI);
   1206 }
   1207 
   1208 /*
   1210  * POST PROCESSING OF SCSI_CMD (usually current)
   1211  */
   1212 void
   1213 aic_done(acb)
   1214 	struct acb *acb;
   1215 {
   1216 	struct scsi_xfer *xs = acb->xs;
   1217 	struct scsi_link *sc = xs->sc_link;
   1218 	struct aic_softc *aic = sc->adapter_softc;
   1219 	struct aic_tinfo *ti;
   1220 
   1221 	AIC_TRACE(("aic_done  "));
   1222 	ti = &aic->tinfo[sc->target];
   1223 
   1224 	/*
   1225 	 * Now, if we've come here with no error code, i.e. we've kept the
   1226 	 * initial XS_NOERROR, and the status code signals that we should
   1227 	 * check sense, we'll need to set up a request sense cmd block and
   1228 	 * push the command back into the ready queue *before* any other
   1229 	 * commands for this target/lunit, else we lose the sense info.
   1230 	 * We don't support chk sense conditions for the request sense cmd.
   1231 	 */
   1232 	if (xs->error == XS_NOERROR && (acb->flags & ACB_CHKSENSE) == 0) {
   1233 		if ((acb->stat & ST_MASK) == SCSI_CHECK) {
   1234 			struct scsi_sense *ss = (void *)&acb->cmd;
   1235 
   1236 			AIC_MISC(("requesting sense  "));
   1237 			/* First, save the return values */
   1238 			xs->resid = acb->dleft;
   1239 			xs->status = acb->stat;
   1240 			/* Next, setup a request sense command block */
   1241 			bzero(ss, sizeof(*ss));
   1242 			ss->op_code = REQUEST_SENSE;
   1243 			ss->byte2 = sc->lun << 5;
   1244 			ss->length = sizeof(struct scsi_sense_data);
   1245 			acb->clen = sizeof(*ss);
   1246 			acb->dp = (char *)&xs->sense;
   1247 			acb->dleft = sizeof(struct scsi_sense_data);
   1248 			acb->flags = ACB_ACTIVE|ACB_CHKSENSE;
   1249 			ti->senses++;
   1250 			if (acb != aic->nexus) {
   1251 				ti->lubusy &= ~(1<<sc->lun);
   1252 				TAILQ_INSERT_HEAD(&aic->ready_list, acb, chain);
   1253 			} else
   1254 				aic_select(aic, sc->target);
   1255 			return;
   1256 		}
   1257 	}
   1258 
   1259 	if ((xs->flags & SCSI_ERR_OK) != 0) {
   1260 		xs->resid = 0;
   1261 		xs->error = XS_NOERROR;
   1262 	} else if (xs->error == XS_NOERROR && (acb->flags & ACB_CHKSENSE) != 0) {
   1263 		xs->error = XS_SENSE;
   1264 	} else {
   1265 		xs->resid = acb->dleft;
   1266 	}
   1267 	xs->flags |= ITSDONE;
   1268 
   1269 #if AIC_DEBUG
   1270 	if ((aic_debug & AIC_SHOWMISC) != 0) {
   1271 		if (xs->resid != 0)
   1272 			printf("resid=%d ", xs->resid);
   1273 		if (xs->error == XS_SENSE)
   1274 			printf("sense=0x%02x\n", xs->sense.error_code);
   1275 		else
   1276 			printf("error=%d\n", xs->error);
   1277 	}
   1278 #endif
   1279 
   1280 	/*
   1281 	 * Remove the ACB from whatever queue it's on.  We have to do a bit of
   1282 	 * a hack to figure out which queue it's on.  Note that it is *not*
   1283 	 * necessary to cdr down the ready queue, but we must cdr down the
   1284 	 * nexus queue and see if it's there, so we can mark the unit as no
   1285 	 * longer busy.  This code is sickening, but it works.
   1286 	 */
   1287 	if (acb == aic->nexus) {
   1288 		ti->lubusy &= ~(1<<sc->lun);
   1289 		aic->state = AIC_IDLE;
   1290 		aic->nexus = NULL;
   1291 		aic_sched(aic);
   1292 	} else if (aic->ready_list.tqh_last == &acb->chain.tqe_next) {
   1293 		TAILQ_REMOVE(&aic->ready_list, acb, chain);
   1294 	} else {
   1295 		register struct acb *acb2;
   1296 		for (acb2 = aic->nexus_list.tqh_first; acb2;
   1297 		    acb2 = acb2->chain.tqe_next)
   1298 			if (acb2 == acb) {
   1299 				TAILQ_REMOVE(&aic->nexus_list, acb, chain);
   1300 				ti->lubusy &= ~(1<<sc->lun);
   1301 				/* XXXX Should we call aic_sched() here? */
   1302 				break;
   1303 			}
   1304 		if (acb2)
   1305 			;
   1306 		else if (acb->chain.tqe_next) {
   1307 			TAILQ_REMOVE(&aic->ready_list, acb, chain);
   1308 		} else {
   1309 			printf("%s: can't find matching acb\n",
   1310 			    aic->sc_dev.dv_xname);
   1311 			Debugger();
   1312 		}
   1313 	}
   1314 
   1315 	aic_free_acb(aic, acb, xs->flags);
   1316 	ti->cmds++;
   1317 	scsi_done(xs);
   1318 }
   1319 
   1320 /*
   1322  * INTERRUPT/PROTOCOL ENGINE
   1323  */
   1324 
   1325 /* The message system:
   1326  * This is a revamped message system that now should easier accomodate new
   1327  * messages, if necessary.
   1328  * Currently we accept these messages:
   1329  * IDENTIFY (when reselecting)
   1330  * COMMAND COMPLETE # (expect bus free after messages marked #)
   1331  * NOOP
   1332  * MESSAGE REJECT
   1333  * SYNCHRONOUS DATA TRANSFER REQUEST
   1334  * SAVE DATA POINTER
   1335  * RESTORE POINTERS
   1336  * DISCONNECT #
   1337  *
   1338  * We may send these messages in prioritized order:
   1339  * BUS DEVICE RESET #		if SCSI_RESET & xs->flags (or in weird sits.)
   1340  * MESSAGE PARITY ERROR		par. err. during MSGI
   1341  * MESSAGE REJECT		If we get a message we don't know how to handle
   1342  * ABORT #			send on errors
   1343  * INITIATOR DETECTED ERROR	also on errors (SCSI2) (during info xfer)
   1344  * IDENTIFY			At the start of each transfer
   1345  * SYNCHRONOUS DATA TRANSFER REQUEST	if appropriate
   1346  * NOOP				if nothing else fits the bill ...
   1347  */
   1348 
   1349 #define aic_sched_msgout(m) \
   1350 	do {					\
   1351 		if (aic->msgpriq == 0)		\
   1352 			orreg(SCSISIGO, ATNO);	\
   1353 		aic->msgpriq |= (m);		\
   1354 	} while (0)
   1355 
   1356 #define IS1BYTEMSG(m) (((m) != 0x01 && (m) < 0x20) || (m) >= 0x80)
   1357 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
   1358 #define ISEXTMSG(m) ((m) == 0x01)
   1359 /*
   1360  * Precondition:
   1361  * The SCSI bus is already in the MSGI phase and there is a message byte
   1362  * on the bus, along with an asserted REQ signal.
   1363  */
   1364 void
   1365 aic_msgin(aic)
   1366 	register struct aic_softc *aic;
   1367 {
   1368 	register int iobase = aic->iobase;
   1369 	int n;
   1370 
   1371 	AIC_TRACE(("aic_msgin  "));
   1372 	aic->progress = 0;
   1373 
   1374 	if (aic->prevphase == PH_MSGI) {
   1375 		/* This is a continuation of the previous message. */
   1376 		n = aic->imp - aic->imess;
   1377 		goto nextbyte;
   1378 	}
   1379 
   1380 	/* This is a new MESSAGE IN phase.  Clean up our state. */
   1381 	aic->flags &= ~AIC_DROP_MSGI;
   1382 
   1383 nextmsg:
   1384 	n = 0;
   1385 	aic->imp = &aic->imess[n];
   1386 
   1387 nextbyte:
   1388 	/*
   1389 	 * Read a whole message, but don't ack the last byte.  If we reject the
   1390 	 * message, we have to assert ATN during the message transfer phase
   1391 	 * itself.
   1392 	 */
   1393 	for (;;) {
   1394 		do {
   1395 			aic->phase = aicphase(aic);
   1396 		} while (aic->phase == PH_INVALID);
   1397 		if (aic->phase != PH_MSGI) {
   1398 			/*
   1399 			 * Target left MESSAGE IN, probably because it
   1400 			 * a) noticed our ATN signal, or
   1401 			 * b) ran out of messages.
   1402 			 */
   1403 			goto out;
   1404 		}
   1405 
   1406 		/* XXX parity */
   1407 
   1408 		/* Gather incoming message bytes if needed. */
   1409 		if ((aic->flags & AIC_DROP_MSGI) == 0) {
   1410 			if (n >= AIC_MAX_MSG_LEN) {
   1411 				aic_sched_msgout(SEND_REJECT);
   1412 				aic->flags |= AIC_DROP_MSGI;
   1413 			} else {
   1414 				*aic->imp++ = inb(SCSIDAT);
   1415 				n++;
   1416 				/*
   1417 				 * This testing is suboptimal, but most
   1418 				 * messages will be of the one byte variety, so
   1419 				 * it should not affect performance
   1420 				 * significantly.
   1421 				 */
   1422 				if (IS1BYTEMSG(aic->imess[0]) && n == 1)
   1423 					break;
   1424 				if (IS2BYTEMSG(aic->imess[0]) && n == 2)
   1425 					break;
   1426 				if (ISEXTMSG(aic->imess[0]) && n >= 3 &&
   1427 				    n == aic->imess[1] + 2)
   1428 					break;
   1429 			}
   1430 		}
   1431 
   1432 		/*
   1433 		 * If we reach this spot we're either:
   1434 		 * a) in the middle of a multi-byte message, or
   1435 		 * b) dropping bytes.
   1436 		 */
   1437 		outb(SXFRCTL0, CHEN|SPIOEN);
   1438 		/* Ack the last byte read. */
   1439 		(void) inb(SCSIDAT);
   1440 		outb(SXFRCTL0, CHEN);
   1441 	}
   1442 
   1443 	aic->progress = 1;
   1444 	AIC_MISC(("n=%d imess=0x%02x  ", n, aic->imess[0]));
   1445 
   1446 	/* We now have a complete message.  Parse it. */
   1447 	switch (aic->state) {
   1448 		struct acb *acb;
   1449 		struct scsi_link *sc;
   1450 		struct aic_tinfo *ti;
   1451 		int period, offset, width;
   1452 		u_char selid, target, lun;
   1453 
   1454 	case AIC_CONNECTED:
   1455 		acb = aic->nexus;
   1456 		sc = acb->xs->sc_link;
   1457 		ti = &aic->tinfo[sc->target];
   1458 
   1459 		switch (aic->imess[0]) {
   1460 		case MSG_CMDCOMPLETE:
   1461 			if (aic->dleft < 0) {
   1462 				printf("aic: %d extra bytes from %d:%d\n",
   1463 				       -aic->dleft, sc->target, sc->lun);
   1464 				acb->dleft = 0;
   1465 			}
   1466 			acb->xs->resid = acb->dleft = aic->dleft;
   1467 			aic->flags |= AIC_BUSFREE_OK;
   1468 #if 1 /* XXX defer till BUS FREE? */
   1469 			untimeout(aic_timeout, acb);
   1470 			delay(250);
   1471 			aic_done(acb);
   1472 #endif
   1473 			break;
   1474 
   1475 		case MSG_PARITY_ERROR:
   1476 			/* Resend the last message. */
   1477 			aic_sched_msgout(aic->msgout);
   1478 			break;
   1479 
   1480 		case MSG_MESSAGE_REJECT:
   1481 			AIC_MISC(("message rejected  "));
   1482 			switch (aic->msgout) {
   1483 			case SEND_IDENTIFY:
   1484 				ti->flags &= ~(DO_SYNC|DO_WIDE);
   1485 				ti->syncdata = 0;
   1486 				outb(SCSIRATE, ti->syncdata);
   1487 				/* ... */
   1488 				break;
   1489 			case SEND_SDTR:
   1490 				ti->flags &= ~DO_SYNC;
   1491 				ti->syncdata = 0;
   1492 				outb(SCSIRATE, ti->syncdata);
   1493 				break;
   1494 			case SEND_WDTR:
   1495 				ti->flags &= ~DO_WIDE;
   1496 				/* ... */
   1497 				break;
   1498 			case SEND_INIT_DET_ERR:
   1499 				aic_sched_msgout(SEND_ABORT);
   1500 				break;
   1501 			}
   1502 			break;
   1503 
   1504 		case MSG_NOOP:
   1505 			break;
   1506 
   1507 		case MSG_DISCONNECT:
   1508 			ti->dconns++;
   1509 			aic->state = AIC_IDLE;
   1510 			aic->nexus = NULL;
   1511 			TAILQ_INSERT_HEAD(&aic->nexus_list, acb, chain);
   1512 			aic->flags |= AIC_BUSFREE_OK;
   1513 			break;
   1514 
   1515 		case MSG_SAVEDATAPOINTER:
   1516 			acb->dp = aic->dp;
   1517 			acb->dleft = aic->dleft;
   1518 			break;
   1519 
   1520 		case MSG_RESTOREPOINTERS:
   1521 			aic->dp = acb->dp;
   1522 			aic->dleft = acb->dleft;
   1523 			aic->cp = (u_char *)&acb->cmd;
   1524 			aic->cleft = acb->clen;
   1525 			break;
   1526 
   1527 		case MSG_EXTENDED:
   1528 			switch (aic->imess[2]) {
   1529 			case MSG_EXT_SDTR:
   1530 				if (aic->imess[1] != 3)
   1531 					goto reject;
   1532 				period = (aic->imess[3] * 4 + 49)/50 - 2;
   1533 				offset = aic->imess[4];
   1534 				if (offset == 0) {
   1535 					ti->flags &= ~DO_SYNC;
   1536 					ti->syncdata = 0;
   1537 					outb(SCSIRATE, ti->syncdata);
   1538 				} else if (period > 7) {
   1539 					/* Too slow for aic6360. Do asynch
   1540 					 * instead.  Renegotiate the deal.
   1541 					 */
   1542 					ti->period = 0;
   1543 					ti->offset = 0;
   1544 					aic_sched_msgout(SEND_SDTR);
   1545 				} else {
   1546 					ti->flags &= ~DO_SYNC;
   1547 					ti->syncdata = period<<4 | offset;
   1548 					outb(SCSIRATE, ti->syncdata);
   1549 				}
   1550 				break;
   1551 
   1552 			case MSG_EXT_WDTR:
   1553 				if (aic->imess[1] != 2)
   1554 					goto reject;
   1555 				width = aic->imess[3];
   1556 				if (width == 0) {
   1557 					ti->flags &= ~DO_WIDE;
   1558 					/* ... */
   1559 				} else if (width > AIC_MAX_WIDTH) {
   1560 					ti->width = 0;
   1561 					aic_sched_msgout(SEND_WDTR);
   1562 				} else {
   1563 					ti->flags &= ~DO_WIDE;
   1564 					/* ... */
   1565 				}
   1566 				break;
   1567 
   1568 			default:
   1569 				printf("aic at line %d: unrecognized MESSAGE IN; sending REJECT\n", __LINE__);
   1570 				AIC_BREAK();
   1571 				goto reject;
   1572 			}
   1573 			break;
   1574 
   1575 		default:
   1576 			printf("aic at line %d: unrecognized MESSAGE IN; sending REJECT\n", __LINE__);
   1577 			AIC_BREAK();
   1578 		reject:
   1579 			aic_sched_msgout(SEND_REJECT);
   1580 			break;
   1581 		}
   1582 		break;
   1583 
   1584 	case AIC_RESELECTED:
   1585 		if (!MSG_ISIDENT(aic->imess[0])) {
   1586 			printf("aic at line %d: reselect without IDENTIFY; sending DEVICE RESET\n", __LINE__);
   1587 			AIC_BREAK();
   1588 			goto reset;
   1589 		}
   1590 
   1591 		/*
   1592 		 * The SCSI chip made a snapshot of the data bus while the
   1593 		 * reselection was being negotiated.  This enables us to
   1594 		 * determine which target did the reselect.
   1595 		 */
   1596 		selid = inb(SELID) & ~(1<<AIC_SCSI_HOSTID);
   1597 		if (selid & (selid - 1)) {
   1598 			printf("aic at line %d: reselect with invalid selid %02x; sending DEVICE RESET\n", __LINE__, selid);
   1599 			AIC_BREAK();
   1600 			goto reset;
   1601 		}
   1602 
   1603 		/* Search wait queue for disconnected cmd
   1604 		 * The list should be short, so I haven't bothered with
   1605 		 * any more sophisticated structures than a simple
   1606 		 * singly linked list.
   1607 		 */
   1608 		target = ffs(selid) - 1;
   1609 		lun = aic->imess[0] & 0x07;
   1610 		for (acb = aic->nexus_list.tqh_first; acb != 0;
   1611 		     acb = acb->chain.tqe_next) {
   1612 			sc = acb->xs->sc_link;
   1613 			if (sc->target == target && sc->lun == lun)
   1614 				break;
   1615 		}
   1616 		if (acb == 0) {
   1617 			printf("aic at line %d: reselect from target %d lun %d with no nexus; sending DEVICE RESET\n", __LINE__, target, lun);
   1618 			AIC_BREAK();
   1619 			goto reset;
   1620 		}
   1621 
   1622 		/* Make this nexus active again. */
   1623 		TAILQ_REMOVE(&aic->nexus_list, acb, chain);
   1624 		aic->state = AIC_CONNECTED;
   1625 		aic->nexus = acb;
   1626 		ti = &aic->tinfo[sc->target];
   1627 		ti->lubusy |= (1<<sc->lun);
   1628 		outb(SCSIRATE, ti->syncdata);
   1629 
   1630 		/* Do an implicit RESTORE POINTERS. */
   1631 		aic->dp = acb->dp;
   1632 		aic->dleft = acb->dleft;
   1633 		aic->cp = (u_char *)&acb->cmd;
   1634 		aic->cleft = acb->clen;
   1635 		break;
   1636 
   1637 	default:
   1638 		printf("aic at line %d: unexpected MESSAGE IN; sending DEVICE RESET\n", __LINE__);
   1639 		AIC_BREAK();
   1640 	reset:
   1641 		aic_sched_msgout(SEND_DEV_RESET);
   1642 		break;
   1643 	}
   1644 
   1645 	outb(SXFRCTL0, CHEN|SPIOEN);
   1646 	/* Ack the last message byte. */
   1647 	(void) inb(SCSIDAT);
   1648 	outb(SXFRCTL0, CHEN);
   1649 
   1650 	/* Go get the next message, if any. */
   1651 	goto nextmsg;
   1652 
   1653 out:
   1654 	AIC_MISC(("n=%d imess=0x%02x  ", n, aic->imess[0]));
   1655 }
   1656 
   1657 
   1658 /* The message out (and in) stuff is a bit complicated:
   1659  * If the target requests another message (sequence) without
   1660  * having changed phase in between it really asks for a
   1661  * retransmit, probably due to parity error(s).
   1662  * The following messages can be sent:
   1663  * IDENTIFY	   @ These 4 stem from SCSI command activity
   1664  * SDTR		   @
   1665  * WDTR		   @
   1666  * DEV_RESET	   @
   1667  * REJECT if MSGI doesn't make sense
   1668  * PARITY_ERROR if parity error while in MSGI
   1669  * INIT_DET_ERR if parity error while not in MSGI
   1670  * ABORT if INIT_DET_ERR rejected
   1671  * NOOP if asked for a message and there's nothing to send
   1672  */
   1673 void
   1674 aic_msgout(aic)
   1675 	register struct aic_softc *aic;
   1676 {
   1677 	register int iobase = aic->iobase;
   1678 	struct acb *acb;
   1679 	struct aic_tinfo *ti;
   1680 	int n;
   1681 
   1682 	AIC_TRACE(("aic_msgout  "));
   1683 	aic->progress = 0;
   1684 
   1685 	/*
   1686 	 * Set ATN.  If we're just sending a trivial 1-byte message, we'll
   1687 	 * clear ATN later on anyway.
   1688 	 */
   1689 	outb(SCSISIGO, PH_MSGO|ATNO);
   1690 	/* Reset the FIFO. */
   1691 	outb(DMACNTRL0, RSTFIFO);
   1692 	/* Enable REQ/ACK protocol. */
   1693 	outb(SXFRCTL0, CHEN|SPIOEN);
   1694 
   1695 	if (aic->prevphase == PH_MSGO) {
   1696 		if (aic->omp == aic->omess) {
   1697 			/*
   1698 			 * This is a retransmission.
   1699 			 *
   1700 			 * We get here if the target stayed in MESSAGE OUT
   1701 			 * phase.  Section 5.1.9.2 of the SCSI 2 spec indicates
   1702 			 * that all of the previously transmitted messages must
   1703 			 * be sent again, in the same order.  Therefore, we
   1704 			 * requeue all the previously transmitted messages, and
   1705 			 * start again from the top.  Our simple priority
   1706 			 * scheme keeps the messages in the right order.
   1707 			 */
   1708 			AIC_MISC(("retransmitting  "));
   1709 			aic->msgpriq |= aic->msgoutq;
   1710 		} else {
   1711 			/* This is a continuation of the previous message. */
   1712 			n = aic->omp - aic->omess;
   1713 			goto nextbyte;
   1714 		}
   1715 	}
   1716 
   1717 	/* No messages transmitted so far. */
   1718 	aic->msgoutq = 0;
   1719 
   1720 nextmsg:
   1721 	/* Pick up highest priority message. */
   1722 	aic->msgout = aic->msgpriq & -aic->msgpriq;
   1723 	aic->msgpriq &= ~aic->msgout;
   1724 	aic->msgoutq |= aic->msgout;
   1725 
   1726 	/* Build the outgoing message data. */
   1727 	switch (aic->msgout) {
   1728 	case SEND_IDENTIFY:
   1729 		if (aic->state != AIC_CONNECTED) {
   1730 			printf("aic at line %d: SEND_IDENTIFY while not connected; sending NOOP\n", __LINE__);
   1731 			AIC_BREAK();
   1732 			goto noop;
   1733 		}
   1734 		acb = aic->nexus;
   1735 		aic->omess[0] = MSG_IDENTIFY(acb->xs->sc_link->lun);
   1736 		n = 1;
   1737 		break;
   1738 
   1739 	case SEND_SDTR:
   1740 		if (aic->state != AIC_CONNECTED) {
   1741 			printf("aic at line %d: SEND_SDTR while not connected; sending NOOP\n", __LINE__);
   1742 			AIC_BREAK();
   1743 			goto noop;
   1744 		}
   1745 		ti = &aic->tinfo[aic->nexus->xs->sc_link->target];
   1746 		aic->omess[4] = MSG_EXTENDED;
   1747 		aic->omess[3] = 3;
   1748 		aic->omess[2] = MSG_EXT_SDTR;
   1749 		aic->omess[1] = ti->period >> 2;
   1750 		aic->omess[0] = ti->offset;
   1751 		n = 5;
   1752 		break;
   1753 
   1754 	case SEND_WDTR:
   1755 		if (aic->state != AIC_CONNECTED) {
   1756 			printf("aic at line %d: SEND_WDTR while not connected; sending NOOP\n", __LINE__);
   1757 			AIC_BREAK();
   1758 			goto noop;
   1759 		}
   1760 		ti = &aic->tinfo[aic->nexus->xs->sc_link->target];
   1761 		aic->omess[3] = MSG_EXTENDED;
   1762 		aic->omess[2] = 2;
   1763 		aic->omess[1] = MSG_EXT_WDTR;
   1764 		aic->omess[0] = ti->width;
   1765 		n = 4;
   1766 		break;
   1767 
   1768 	case SEND_DEV_RESET:
   1769 		aic->omess[0] = MSG_BUS_DEV_RESET;
   1770 		n = 1;
   1771 		aic->flags |= AIC_BUSFREE_OK;
   1772 		break;
   1773 
   1774 	case SEND_REJECT:
   1775 		aic->omess[0] = MSG_MESSAGE_REJECT;
   1776 		n = 1;
   1777 		break;
   1778 
   1779 	case SEND_PARITY_ERROR:
   1780 		aic->omess[0] = MSG_PARITY_ERROR;
   1781 		n = 1;
   1782 		break;
   1783 
   1784 	case SEND_INIT_DET_ERR:
   1785 		aic->omess[0] = MSG_INITIATOR_DET_ERR;
   1786 		n = 1;
   1787 		break;
   1788 
   1789 	case SEND_ABORT:
   1790 		aic->omess[0] = MSG_ABORT;
   1791 		n = 1;
   1792 		aic->flags |= AIC_BUSFREE_OK;
   1793 		break;
   1794 
   1795 	case 0:
   1796 		printf("aic at line %d: unexpected MESSAGE OUT; sending NOOP\n", __LINE__);
   1797 	noop:
   1798 		aic->omess[0] = MSG_NOOP;
   1799 		n = 1;
   1800 		break;
   1801 
   1802 	default:
   1803 		printf("aic at line %d: weird MESSAGE OUT; sending NOOP\n", __LINE__);
   1804 		AIC_BREAK();
   1805 		goto noop;
   1806 	}
   1807 	aic->omp = &aic->omess[n];
   1808 
   1809 nextbyte:
   1810 	/* Send message bytes. */
   1811 	for (; n > 0; n--) {
   1812 		do {
   1813 			aic->phase = aicphase(aic);
   1814 		} while (aic->phase == PH_INVALID);
   1815 		if (aic->phase != PH_MSGO) {
   1816 			/*
   1817 			 * Target left MESSAGE OUT, possibly to reject our
   1818 			 * message.
   1819 			 */
   1820 			goto out;
   1821 		}
   1822 
   1823 		/* Clear ATN before last byte if this is the last message. */
   1824 		if (n == 1 && aic->msgpriq == 0)
   1825 			outb(CLRSINT1, CLRATNO);
   1826 		/* Send message byte. */
   1827 		outb(SCSIDAT, *--aic->omp);
   1828 		/* Wait for ACK to be negated. */
   1829 		while ((inb(SCSISIGI) & ACKI) != 0)
   1830 			;
   1831 	}
   1832 
   1833 	aic->progress = 1;
   1834 
   1835 	/* We get here only if the entire message has been transmitted. */
   1836 	if (aic->msgpriq != 0) {
   1837 		/* There are more outgoing messages. */
   1838 		goto nextmsg;
   1839 	}
   1840 
   1841 	/*
   1842 	 * The last message has been transmitted.  We need to remember the last
   1843 	 * message transmitted (in case the target switches to MESSAGE IN phase
   1844 	 * and sends a MESSAGE REJECT), and the list of messages transmitted
   1845 	 * this time around (in case the target stays in MESSAGE OUT phase to
   1846 	 * request a retransmit).
   1847 	 */
   1848 
   1849 out:
   1850 	/* Disable REQ/ACK protocol. */
   1851 	outb(SXFRCTL0, CHEN);
   1852 }
   1853 
   1854 /* aic_dataout_pio: perform a data transfer using the FIFO datapath in the aic6360
   1856  * Precondition: The SCSI bus should be in the DOUT phase, with REQ asserted
   1857  * and ACK deasserted (i.e. waiting for a data byte)
   1858  * This new revision has been optimized (I tried) to make the common case fast,
   1859  * and the rarer cases (as a result) somewhat more comlex
   1860  */
   1861 int
   1862 aic_dataout_pio(aic, p, n)
   1863 	register struct aic_softc *aic;
   1864 	u_char *p;
   1865 	int n;
   1866 {
   1867 	register int iobase = aic->iobase;
   1868 	register u_char dmastat;
   1869 	int out = 0;
   1870 #define DOUTAMOUNT 128		/* Full FIFO */
   1871 
   1872 	/* Clear FIFOs and counters. */
   1873 	outb(SXFRCTL0, CHEN|CLRSTCNT|CLRCH);
   1874 	outb(DMACNTRL0, RSTFIFO|WRITE);
   1875 	/* Enable FIFOs. */
   1876 	outb(SXFRCTL0, SCSIEN|DMAEN|CHEN);
   1877 	outb(DMACNTRL0, ENDMA|DWORDPIO|WRITE);
   1878 
   1879 	/* Setup to detect:
   1880 	 * PHASEMIS & PHASECHG: target has left the DOUT phase
   1881 	 * SCSIRST: something just pulled the RST line.
   1882 	 * BUSFREE: target has unexpectedly left the DOUT phase
   1883 	 */
   1884 	outb(SIMODE1, ENPHASEMIS|ENSCSIRST|ENBUSFREE|ENPHASECHG);
   1885 
   1886 	/* I have tried to make the main loop as tight as possible.  This
   1887 	 * means that some of the code following the loop is a bit more
   1888 	 * complex than otherwise.
   1889 	 */
   1890 	while (n > 0) {
   1891 		int xfer;
   1892 
   1893 		for (;;) {
   1894 			dmastat = inb(DMASTAT);
   1895 			if ((dmastat & DFIFOEMP) != 0)
   1896 				break;
   1897 			if ((dmastat & INTSTAT) != 0)
   1898 				goto phasechange;
   1899 		}
   1900 
   1901 		xfer = min(DOUTAMOUNT, n);
   1902 
   1903 		n -= xfer;
   1904 		out += xfer;
   1905 
   1906 #if AIC_USE_DWORDS
   1907 		if (xfer >= 12) {
   1908 			outsl(DMADATALONG, p, xfer>>2);
   1909 			p += xfer & ~3;
   1910 			xfer &= 3;
   1911 		}
   1912 #else
   1913 		if (xfer >= 8) {
   1914 			outsw(DMADATA, p, xfer>>1);
   1915 			p += xfer & ~1;
   1916 			xfer &= 1;
   1917 		}
   1918 #endif
   1919 
   1920 		if (xfer > 0) {
   1921 			outb(DMACNTRL0, ENDMA|B8MODE|WRITE);
   1922 			outsb(DMADATA, p, xfer);
   1923 			p += xfer;
   1924 			outb(DMACNTRL0, ENDMA|DWORDPIO|WRITE);
   1925 		}
   1926 	}
   1927 
   1928 	/* See the bytes off chip */
   1929 	for (;;) {
   1930 		dmastat = inb(DMASTAT);
   1931 		if ((dmastat & DFIFOEMP) != 0 &&
   1932 		    (inb(SSTAT2) & SEMPTY) != 0)
   1933 			break;
   1934 		if ((dmastat & INTSTAT) != 0)
   1935 			goto phasechange;
   1936 	}
   1937 
   1938 phasechange:
   1939 	/* We now have the data off chip.  */
   1940 	outb(SXFRCTL0, CHEN);
   1941 
   1942 	if ((dmastat & INTSTAT) != 0) {
   1943 		/* Some sort of phase change. */
   1944 		register u_char sstat2;
   1945 		int amount;
   1946 
   1947 		/* Stop transfers, do some accounting */
   1948 		amount = inb(FIFOSTAT);
   1949 		sstat2 = inb(SSTAT2);
   1950 		if ((sstat2 & 7) == 0)
   1951 			amount += sstat2 & SFULL ? 8 : 0;
   1952 		else
   1953 			amount += sstat2 & 7;
   1954 		out -= amount;
   1955 		AIC_MISC(("+%d ", amount));
   1956 	}
   1957 
   1958 	outb(DMACNTRL0, RSTFIFO);
   1959 	while ((inb(SXFRCTL0) & SCSIEN) != 0)
   1960 		;
   1961 
   1962 	outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT);
   1963 	aic->progress = out != 0;
   1964 
   1965 	return out;
   1966 }
   1967 
   1968 /* 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 /*
   2094  * This is the workhorse routine of the driver.
   2095  * Deficiencies (for now):
   2096  * 1) always uses programmed I/O
   2097  * 2) doesn't support synchronous transfers properly (yet)
   2098  */
   2099 int
   2100 aicintr(aic)
   2101 	register struct aic_softc *aic;
   2102 {
   2103 	register int iobase = aic->iobase;
   2104 	u_char sstat0, sstat1;
   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 				TAILQ_INSERT_HEAD(&aic->ready_list, aic->nexus,
   2162 				    chain);
   2163 				aic->nexus = NULL;
   2164 			}
   2165 
   2166 			/*
   2167 			 * Turn off selection stuff, and prepare to catch bus
   2168 			 * free interrupts and parity errors.
   2169 			 */
   2170 			outb(SXFRCTL1, 0);
   2171 			outb(SCSISEQ, ENAUTOATNP);
   2172 			outb(CLRSINT0, CLRSELDI);
   2173 			outb(CLRSINT1, CLRBUSFREE|CLRSCSIPERR);
   2174 			outb(SIMODE0, 0);
   2175 			outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENREQINIT);
   2176 
   2177 			aic->state = AIC_RESELECTED;
   2178 			goto 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 			acb = aic->nexus;
   2193 
   2194 			sc = acb->xs->sc_link;
   2195 			ti = &aic->tinfo[sc->target];
   2196 			if ((acb->xs->flags & SCSI_RESET) == 0) {
   2197 				aic->msgpriq = SEND_IDENTIFY;
   2198 				if ((ti->flags & DO_SYNC) != 0)
   2199 					aic->msgpriq |= SEND_SDTR;
   2200 				if ((ti->flags & DO_WIDE) != 0)
   2201 					aic->msgpriq |= SEND_WDTR;
   2202 			} else
   2203 				aic->msgpriq = SEND_DEV_RESET;
   2204 
   2205 			/*
   2206 			 * Turn off selection stuff, and prepare to catch bus
   2207 			 * free interrupts and parity errors.
   2208 			 */
   2209 			outb(SXFRCTL1, 0);
   2210 			outb(SCSISEQ, ENAUTOATNP);
   2211 			outb(CLRSINT0, CLRSELDO);
   2212 			outb(CLRSINT1, CLRBUSFREE|CLRSCSIPERR);
   2213 			outb(SIMODE0, 0);
   2214 			outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENREQINIT);
   2215 
   2216 			aic->flags = 0;
   2217 			aic->prevphase = PH_INVALID;
   2218 			ti->lubusy |= (1<<sc->lun);
   2219 
   2220 			/* Do an implicit RESTORE POINTERS. */
   2221 			aic->dp = acb->dp;
   2222 			aic->dleft = acb->dleft;
   2223 			aic->cp = (u_char *)&acb->cmd;
   2224 			aic->cleft = acb->clen;
   2225 
   2226 			aic->state = AIC_CONNECTED;
   2227 			goto connected;
   2228 		} else if ((sstat1 & SELTO) != 0) {
   2229 			AIC_MISC(("selection timeout  "));
   2230 
   2231 			if (aic->state != AIC_SELECTING) {
   2232 				printf("aic at line %d: selection timeout while not selecting; resetting\n", __LINE__);
   2233 				AIC_BREAK();
   2234 				goto reset;
   2235 			}
   2236 			acb = aic->nexus;
   2237 
   2238 			outb(SXFRCTL1, 0);
   2239 			outb(CLRSINT1, CLRSELTIMO);
   2240 
   2241 			acb->xs->error = XS_TIMEOUT;
   2242 			untimeout(aic_timeout, acb);
   2243 			delay(250);
   2244 			aic_done(acb);
   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 			}
   2250 
   2251 			aic_sched(aic);
   2252 		}
   2253 		break;
   2254 
   2255 	case AIC_RESELECTED:
   2256 	reselected:
   2257 		/* Now, we're expecting an IDENTIFY message. */
   2258 		aic->phase = aicphase(aic);
   2259 		AIC_MISC(("phase=0x%02x  ", aic->phase));
   2260 		if ((aic->phase & PH_PSBIT) == 0)
   2261 			outb(SCSISIGO, aic->phase);
   2262 		outb(CLRSINT1, CLRPHASECHG|CLRBUSFREE);
   2263 
   2264 		switch (aic->phase) {
   2265 		case PH_INVALID:
   2266 			break;
   2267 
   2268 		case PH_BUSFREE:
   2269 			aic->state = AIC_IDLE;
   2270 			aic_sched(aic);
   2271 			break;
   2272 
   2273 		case PH_MSGI:
   2274 			aic_msgin(aic);
   2275 			aic->prevphase = PH_MSGI;
   2276 			if (aic->state == AIC_CONNECTED)
   2277 				goto connected;
   2278 			break;
   2279 
   2280 		default:
   2281 			printf("aic at line %d: reselect without MESSAGE IN; resetting\n", __LINE__);
   2282 			AIC_BREAK();
   2283 			goto reset;
   2284 		}
   2285 		break;
   2286 
   2287 	case AIC_CONNECTED:
   2288 	connected:
   2289 		acb = aic->nexus;
   2290 
   2291 		/* What sort of transfer does the bus signal? */
   2292 		aic->phase = aicphase(aic);
   2293 		AIC_MISC(("phase=0x%02x  ", aic->phase));
   2294 		if ((aic->phase & PH_PSBIT) == 0)
   2295 			outb(SCSISIGO, aic->phase);
   2296 		outb(CLRSINT1, CLRPHASECHG|CLRBUSFREE);
   2297 
   2298 		switch (aic->phase) {
   2299 		case PH_INVALID:
   2300 			break;
   2301 
   2302 		case PH_BUSFREE:
   2303 			if ((aic->flags & AIC_BUSFREE_OK) == 0) {
   2304 				printf("aic at line %d: unexpected BUS FREE while connected; aborting\n", __LINE__);
   2305 				AIC_BREAK();
   2306 
   2307 				acb->xs->error = XS_DRIVER_STUFFUP;
   2308 				untimeout(aic_timeout, acb);
   2309 				aic_done(acb);
   2310 				goto out;
   2311 			}
   2312 			aic->flags &= ~AIC_BUSFREE_OK;
   2313 			break;
   2314 
   2315 		case PH_MSGO:
   2316 			aic_msgout(aic);
   2317 			aic->prevphase = PH_MSGO;
   2318 			break;
   2319 
   2320 		case PH_MSGI:
   2321 			aic_msgin(aic);
   2322 			aic->prevphase = PH_MSGI;
   2323 			break;
   2324 
   2325 		case PH_CMD:		/* CMD phase & REQ asserted */
   2326 			AIC_MISC(("cmd=0x%02x+%d  ", acb->cmd.opcode, acb->clen-1));
   2327 			n = aic_dataout_pio(aic, aic->cp, aic->cleft);
   2328 			aic->cp += n;
   2329 			aic->cleft -= n;
   2330 			aic->prevphase = PH_CMD;
   2331 			break;
   2332 
   2333 		case PH_DOUT:
   2334 			AIC_MISC(("dleft=%d  ", aic->dleft));
   2335 			n = aic_dataout_pio(aic, aic->dp, aic->dleft);
   2336 			aic->dp += n;
   2337 			aic->dleft -= n;
   2338 			aic->prevphase = PH_DOUT;
   2339 			break;
   2340 
   2341 		case PH_DIN:
   2342 			n = aic_datain_pio(aic, aic->dp, aic->dleft);
   2343 			aic->dp += n;
   2344 			aic->dleft -= n;
   2345 			aic->prevphase = PH_DIN;
   2346 			break;
   2347 
   2348 		case PH_STAT:
   2349 			outb(SXFRCTL0, CHEN|SPIOEN);
   2350 			outb(DMACNTRL0, RSTFIFO);
   2351 			outb(SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE|ENSCSIPERR);
   2352 			acb->stat = inb(SCSIDAT);
   2353 			outb(SXFRCTL0, CHEN);
   2354 			AIC_MISC(("stat=0x%02x  ", acb->stat));
   2355 			outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT);
   2356 			aic->prevphase = PH_STAT;
   2357 			break;
   2358 
   2359 		default:
   2360 			printf("aic at line %d: bogus phase while connected; resetting\n", __LINE__);
   2361 			AIC_BREAK();
   2362 			goto reset;
   2363 		}
   2364 		break;
   2365 	}
   2366 
   2367 out:
   2368 	outb(DMACNTRL0, INTEN);
   2369 	return 1;
   2370 
   2371 reset:
   2372 	aic_init(aic);
   2373 	return 1;
   2374 }
   2375 
   2376 void
   2377 aic_timeout(arg)
   2378 	void *arg;
   2379 {
   2380 	struct acb *acb = (struct acb *)arg;
   2381 	int s;
   2382 
   2383 	s = splbio();
   2384 
   2385 	sc_print_addr(acb->xs->sc_link);
   2386 	printf("timed out\n");
   2387 
   2388 	acb->xs->error = XS_TIMEOUT;
   2389 	aic_done(acb);
   2390 
   2391 	splx(s);
   2392 }
   2393 
   2394 #ifdef AIC_DEBUG
   2396 /*
   2397  * The following functions are mostly used for debugging purposes, either
   2398  * directly called from the driver or from the kernel debugger.
   2399  */
   2400 
   2401 void
   2402 aic_show_scsi_cmd(acb)
   2403 	struct acb *acb;
   2404 {
   2405 	u_char  *b = (u_char *)&acb->cmd;
   2406 	struct scsi_link *sc = acb->xs->sc_link;
   2407 	int i;
   2408 
   2409 	sc_print_addr(sc);
   2410 	if ((acb->xs->flags & SCSI_RESET) == 0) {
   2411 		for (i = 0; i < acb->clen; i++) {
   2412 			if (i)
   2413 				printf(",");
   2414 			printf("%x", b[i]);
   2415 		}
   2416 		printf("\n");
   2417 	} else
   2418 		printf("RESET\n");
   2419 }
   2420 
   2421 void
   2422 aic_print_acb(acb)
   2423 	struct acb *acb;
   2424 {
   2425 
   2426 	printf("acb@%x xs=%x flags=%x", acb, acb->xs, acb->flags);
   2427 	printf(" dp=%x dleft=%d stat=%x\n",
   2428 	    (long)acb->dp, acb->dleft, acb->stat);
   2429 	aic_show_scsi_cmd(acb);
   2430 }
   2431 
   2432 void
   2433 aic_print_active_acb()
   2434 {
   2435 	struct acb *acb;
   2436 	struct aic_softc *aic = aiccd.cd_devs[0];
   2437 
   2438 	printf("ready list:\n");
   2439 	for (acb = aic->ready_list.tqh_first; acb; acb = acb->chain.tqe_next)
   2440 		aic_print_acb(acb);
   2441 	printf("nexus:\n");
   2442 	if (aic->nexus)
   2443 		aic_print_acb(aic->nexus);
   2444 	printf("nexus list:\n");
   2445 	for (acb = aic->nexus_list.tqh_first; acb; acb = acb->chain.tqe_next)
   2446 		aic_print_acb(acb);
   2447 }
   2448 
   2449 void
   2450 aic_dump6360(aic)
   2451 	struct aic_softc *aic;
   2452 {
   2453 	int iobase = aic->iobase;
   2454 
   2455 	printf("aic6360: SCSISEQ=%x SXFRCTL0=%x SXFRCTL1=%x SCSISIGI=%x\n",
   2456 	    inb(SCSISEQ), inb(SXFRCTL0), inb(SXFRCTL1), inb(SCSISIGI));
   2457 	printf("         SSTAT0=%x SSTAT1=%x SSTAT2=%x SSTAT3=%x SSTAT4=%x\n",
   2458 	    inb(SSTAT0), inb(SSTAT1), inb(SSTAT2), inb(SSTAT3), inb(SSTAT4));
   2459 	printf("         SIMODE0=%x SIMODE1=%x DMACNTRL0=%x DMACNTRL1=%x DMASTAT=%x\n",
   2460 	    inb(SIMODE0), inb(SIMODE1), inb(DMACNTRL0), inb(DMACNTRL1),
   2461 	    inb(DMASTAT));
   2462 	printf("         FIFOSTAT=%d SCSIBUS=0x%x\n",
   2463 	    inb(FIFOSTAT), inb(SCSIBUS));
   2464 }
   2465 
   2466 void
   2467 aic_dump_driver(aic)
   2468 	struct aic_softc *aic;
   2469 {
   2470 	struct aic_tinfo *ti;
   2471 	int i;
   2472 
   2473 	printf("nexus=%x phase=%x prevphase=%x\n", aic->nexus, aic->phase,
   2474 	    aic->prevphase);
   2475 	printf("state=%x msgin=%x msgpriq=%x msgoutq=%x msgout=%x\n",
   2476 	    aic->state, aic->imess[0], aic->msgpriq, aic->msgoutq, aic->msgout);
   2477 	for (i = 0; i < 7; i++) {
   2478 		ti = &aic->tinfo[i];
   2479 		printf("tinfo%d: %d cmds %d disconnects %d timeouts",
   2480 		    i, ti->cmds, ti->dconns, ti->touts);
   2481 		printf(" %d senses flags=%x\n", ti->senses, ti->flags);
   2482 	}
   2483 }
   2484 #endif
   2485