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