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