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