Home | History | Annotate | Line # | Download | only in ic
aic6360.c revision 1.37
      1 /*	$NetBSD: aic6360.c,v 1.37 1995/11/30 00:59:51 jtc 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 <dev/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 #ifndef DDB
    425 #define	Debugger() panic("should call debugger here (aic6360.c)")
    426 #endif /* ! DDB */
    427 
    428 typedef u_long physaddr;
    429 typedef u_long physlen;
    430 
    431 struct aic_dma_seg {
    432 	physaddr seg_addr;
    433 	physlen seg_len;
    434 };
    435 
    436 #define AIC_NSEG	16
    437 
    438 /*
    439  * ACB. Holds additional information for each SCSI command Comments: We
    440  * need a separate scsi command block because we may need to overwrite it
    441  * with a request sense command.  Basicly, we refrain from fiddling with
    442  * the scsi_xfer struct (except do the expected updating of return values).
    443  * We'll generally update: xs->{flags,resid,error,sense,status} and
    444  * occasionally xs->retries.
    445  */
    446 struct aic_acb {
    447 	struct scsi_generic scsi_cmd;
    448 	int scsi_cmd_length;
    449 	u_char *data_addr;		/* Saved data pointer */
    450 	int data_length;		/* Residue */
    451 
    452 	u_char target_stat;		/* SCSI status byte */
    453 
    454 /*	struct aic_dma_seg dma[AIC_NSEG]; /* Physical addresses+len */
    455 
    456 	TAILQ_ENTRY(aic_acb) chain;
    457 	struct scsi_xfer *xs;	/* SCSI xfer ctrl block from above */
    458 	int flags;
    459 #define ACB_FREE	0
    460 #define ACB_ACTIVE	1
    461 #define ACB_CHKSENSE	2
    462 #define	ACB_ABORTED	3
    463 };
    464 
    465 /*
    466  * Some info about each (possible) target on the SCSI bus.  This should
    467  * probably have been a "per target+lunit" structure, but we'll leave it at
    468  * this for now.
    469  */
    470 struct aic_tinfo {
    471 	int	cmds;		/* #commands processed */
    472 	int	dconns;		/* #disconnects */
    473 	int	touts;		/* #timeouts */
    474 	int	perrs;		/* #parity errors */
    475 	int	senses;		/* #request sense commands sent */
    476 	ushort	lubusy;		/* What local units/subr. are busy? */
    477 	u_char  flags;
    478 #define DO_SYNC		0x01	/* (Re)Negotiate synchronous options */
    479 #define	DO_WIDE		0x02	/* (Re)Negotiate wide options */
    480 	u_char  period;		/* Period suggestion */
    481 	u_char  offset;		/* Offset suggestion */
    482 	u_char	width;		/* Width suggestion */
    483 } tinfo_t;
    484 
    485 struct aic_softc {
    486 	struct device sc_dev;
    487 	struct isadev sc_id;
    488 	void *sc_ih;
    489 
    490 	int sc_iobase;
    491 	int sc_irq, sc_drq;
    492 
    493 	struct scsi_link sc_link;	/* prototype for subdevs */
    494 
    495 	TAILQ_HEAD(, aic_acb) free_list, ready_list, nexus_list;
    496 	struct aic_acb *sc_nexus;	/* current command */
    497 	struct aic_acb sc_acb[8];
    498 	struct aic_tinfo sc_tinfo[8];
    499 
    500 	/* Data about the current nexus (updated for every cmd switch) */
    501 	u_char	*sc_dp;		/* Current data pointer */
    502 	size_t	sc_dleft;	/* Data bytes left to transfer */
    503 	u_char	*sc_cp;		/* Current command pointer */
    504 	size_t	sc_cleft;	/* Command bytes left to transfer */
    505 
    506 	/* Adapter state */
    507 	u_char	 sc_phase;	/* Current bus phase */
    508 	u_char	 sc_prevphase;	/* Previous bus phase */
    509 	u_char	 sc_state;	/* State applicable to the adapter */
    510 #define AIC_IDLE	0x01
    511 #define AIC_SELECTING	0x02	/* SCSI command is arbiting  */
    512 #define AIC_RESELECTED	0x04	/* Has been reselected */
    513 #define AIC_CONNECTED	0x08	/* Actively using the SCSI bus */
    514 #define	AIC_DISCONNECT	0x10	/* MSG_DISCONNECT received */
    515 #define	AIC_CMDCOMPLETE	0x20	/* MSG_CMDCOMPLETE received */
    516 #define AIC_CLEANING	0x40
    517 	u_char	 sc_flags;
    518 #define AIC_DROP_MSGIN	0x01	/* Discard all msgs (parity err detected) */
    519 #define	AIC_ABORTING	0x02	/* Bailing out */
    520 #define AIC_DOINGDMA	0x04	/* The FIFO data path is active! */
    521 	u_char	sc_selid;	/* Reselection ID */
    522 
    523 	/* Message stuff */
    524 	u_char	sc_msgpriq;	/* Messages we want to send */
    525 	u_char	sc_msgoutq;	/* Messages sent during last MESSAGE OUT */
    526 	u_char	sc_lastmsg;	/* Message last transmitted */
    527 	u_char	sc_currmsg;	/* Message currently ready to transmit */
    528 #define SEND_DEV_RESET		0x01
    529 #define SEND_PARITY_ERROR	0x02
    530 #define SEND_ABORT		0x04
    531 #define SEND_REJECT		0x08
    532 #define SEND_INIT_DET_ERR	0x10
    533 #define SEND_IDENTIFY  		0x20
    534 #define SEND_SDTR		0x40
    535 #define	SEND_WDTR		0x80
    536 #define AIC_MAX_MSG_LEN 8
    537 	u_char  sc_omess[AIC_MAX_MSG_LEN];
    538 	u_char	*sc_omp;		/* Outgoing message pointer */
    539 	u_char	sc_imess[AIC_MAX_MSG_LEN];
    540 	u_char	*sc_imp;		/* Incoming message pointer */
    541 
    542 	/* Hardware stuff */
    543 	int	sc_initiator;		/* Our scsi id */
    544 	int	sc_freq;		/* Clock frequency in MHz */
    545 	int	sc_minsync;		/* Minimum sync period / 4 */
    546 	int	sc_maxsync;		/* Maximum sync period / 4 */
    547 };
    548 
    549 #if AIC_DEBUG
    550 #define AIC_SHOWACBS	0x01
    551 #define AIC_SHOWINTS	0x02
    552 #define AIC_SHOWCMDS	0x04
    553 #define AIC_SHOWMISC	0x08
    554 #define AIC_SHOWTRACE	0x10
    555 #define AIC_SHOWSTART	0x20
    556 #define AIC_DOBREAK	0x40
    557 int aic_debug = 0x00; /* AIC_SHOWSTART|AIC_SHOWMISC|AIC_SHOWTRACE; /**/
    558 #define	AIC_PRINT(b, s)	do {if ((aic_debug & (b)) != 0) printf s;} while (0)
    559 #define	AIC_BREAK()	do {if ((aic_debug & AIC_DOBREAK) != 0) Debugger();} while (0)
    560 #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)
    561 #else
    562 #define	AIC_PRINT(b, s)
    563 #define	AIC_BREAK()
    564 #define	AIC_ASSERT(x)
    565 #endif
    566 
    567 #define AIC_ACBS(s)	AIC_PRINT(AIC_SHOWACBS, s)
    568 #define AIC_INTS(s)	AIC_PRINT(AIC_SHOWINTS, s)
    569 #define AIC_CMDS(s)	AIC_PRINT(AIC_SHOWCMDS, s)
    570 #define AIC_MISC(s)	AIC_PRINT(AIC_SHOWMISC, s)
    571 #define AIC_TRACE(s)	AIC_PRINT(AIC_SHOWTRACE, s)
    572 #define AIC_START(s)	AIC_PRINT(AIC_SHOWSTART, s)
    573 
    574 int	aicprobe	__P((struct device *, void *, void *));
    575 void	aicattach	__P((struct device *, struct device *, void *));
    576 int	aicprint	__P((void *, char *));
    577 void	aic_minphys	__P((struct buf *));
    578 int	aicintr		__P((void *));
    579 void 	aic_init	__P((struct aic_softc *));
    580 void	aic_done	__P((struct aic_softc *, struct aic_acb *));
    581 void	aic_dequeue	__P((struct aic_softc *, struct aic_acb *));
    582 int	aic_scsi_cmd	__P((struct scsi_xfer *));
    583 int	aic_poll	__P((struct aic_softc *, struct scsi_xfer *, int));
    584 void	aic_select	__P((struct aic_softc *, struct aic_acb *));
    585 void	aic_timeout	__P((void *));
    586 int	aic_find	__P((struct aic_softc *));
    587 void	aic_sched	__P((struct aic_softc *));
    588 void	aic_scsi_reset	__P((struct aic_softc *));
    589 void	aic_reset	__P((struct aic_softc *));
    590 #if AIC_DEBUG
    591 void	aic_print_active_acb();
    592 void	aic_dump_driver();
    593 void	aic_dump6360();
    594 #endif
    595 
    596 struct cfdriver aiccd = {
    597 	NULL, "aic", aicprobe, aicattach, DV_DULL, sizeof(struct aic_softc)
    598 };
    599 
    600 struct scsi_adapter aic_switch = {
    601 	aic_scsi_cmd,
    602 	aic_minphys,
    603 	0,
    604 	0,
    605 };
    606 
    607 struct scsi_device aic_dev = {
    608 	NULL,			/* Use default error handler */
    609 	NULL,			/* have a queue, served by this */
    610 	NULL,			/* have no async handler */
    611 	NULL,			/* Use default 'done' routine */
    612 };
    613 
    614 /*
    616  * INITIALIZATION ROUTINES (probe, attach ++)
    617  */
    618 
    619 /*
    620  * aicprobe: probe for AIC6360 SCSI-controller
    621  * returns non-zero value if a controller is found.
    622  */
    623 int
    624 aicprobe(parent, match, aux)
    625 	struct device *parent;
    626 	void *match, *aux;
    627 {
    628 	struct aic_softc *sc = match;
    629 	struct isa_attach_args *ia = aux;
    630 	int i, len, ic;
    631 
    632 #ifdef NEWCONFIG
    633 	if (ia->ia_iobase == IOBASEUNK)
    634 		return 0;
    635 #endif
    636 
    637 	sc->sc_iobase = ia->ia_iobase;
    638 	if (aic_find(sc) != 0)
    639 		return 0;
    640 
    641 #ifdef NEWCONFIG
    642 	if (ia->ia_irq != IRQUNK) {
    643 		if (ia->ia_irq != sc->sc_irq) {
    644 			printf("%s: irq mismatch; kernel configured %d != board configured %d\n",
    645 			    sc->sc_dev.dv_xname, ia->ia_irq, sc->sc_irq);
    646 			return 0;
    647 		}
    648 	} else
    649 		ia->ia_irq = sc->sc_irq;
    650 
    651 	if (ia->ia_drq != DRQUNK) {
    652 		if (ia->ia_drq != sc->sc_drq) {
    653 			printf("%s: drq mismatch; kernel configured %d != board configured %d\n",
    654 			    sc->sc_dev.dv_xname, ia->ia_drq, sc->sc_drq);
    655 			return 0;
    656 		}
    657 	} else
    658 		ia->ia_drq = sc->sc_drq;
    659 #endif
    660 
    661 	ia->ia_msize = 0;
    662 	ia->ia_iosize = 0x20;
    663 	return 1;
    664 }
    665 
    666 /* Do the real search-for-device.
    667  * Prerequisite: sc->sc_iobase should be set to the proper value
    668  */
    669 int
    670 aic_find(sc)
    671 	struct aic_softc *sc;
    672 {
    673 	char chip_id[sizeof(IDSTRING)];	/* For chips that support it */
    674 	char *start;
    675 	int i;
    676 
    677 	/* Remove aic6360 from possible powerdown mode */
    678 	outb(DMACNTRL0, 0);
    679 
    680 	/* Thanks to mark (at) aggregate.com for the new method for detecting
    681 	 * whether the chip is present or not.  Bonus: may also work for
    682 	 * the AIC-6260!
    683  	 */
    684 	AIC_TRACE(("aic: probing for aic-chip at port 0x%x\n",
    685 	    sc->sc_iobase));
    686  	/*
    687  	 * Linux also init's the stack to 1-16 and then clears it,
    688      	 *  6260's don't appear to have an ID reg - mpg
    689  	 */
    690 	/* Push the sequence 0,1,..,15 on the stack */
    691 #define STSIZE 16
    692 	outb(DMACNTRL1, 0);	/* Reset stack pointer */
    693 	for (i = 0; i < STSIZE; i++)
    694 		outb(STACK, i);
    695 
    696 	/* See if we can pull out the same sequence */
    697 	outb(DMACNTRL1, 0);
    698  	for (i = 0; i < STSIZE && inb(STACK) == i; i++)
    699 		;
    700 	if (i != STSIZE) {
    701 		AIC_START(("STACK futzed at %d.\n", i));
    702 		return ENXIO;
    703 	}
    704 
    705 	/* See if we can pull the id string out of the ID register,
    706 	 * now only used for informational purposes.
    707 	 */
    708 	bzero(chip_id, sizeof(chip_id));
    709 	insb(ID, chip_id, sizeof(IDSTRING)-1);
    710 	AIC_START(("AIC found at 0x%x ", sc->sc_iobase));
    711 	AIC_START(("ID: %s ",chip_id));
    712 	AIC_START(("chip revision %d\n",(int)inb(REV)));
    713 
    714 	sc->sc_initiator = 7;
    715 	sc->sc_freq = 20;	/* XXXX Assume 20 MHz. */
    716 
    717 	/*
    718 	 * These are the bounds of the sync period, based on the frequency of
    719 	 * the chip's clock input and the size and offset of the sync period
    720 	 * register.
    721 	 *
    722 	 * For a 20Mhz clock, this gives us 25, or 100nS, or 10MB/s, as a
    723 	 * maximum transfer rate, and 112.5, or 450nS, or 2.22MB/s, as a
    724 	 * minimum transfer rate.
    725 	 */
    726 	sc->sc_minsync = (2 * 250) / sc->sc_freq;
    727 	sc->sc_maxsync = (9 * 250) / sc->sc_freq;
    728 
    729 	return 0;
    730 }
    731 
    732 int
    733 aicprint(aux, name)
    734 	void *aux;
    735 	char *name;
    736 {
    737 	if (name != NULL)
    738 		printf("%s: scsibus ", name);
    739 	return UNCONF;
    740 }
    741 
    742 /*
    743  * Attach the AIC6360, fill out some high and low level data structures
    744  */
    745 void
    746 aicattach(parent, self, aux)
    747 	struct device *parent, *self;
    748 	void *aux;
    749 {
    750 	struct isa_attach_args *ia = aux;
    751 	struct aic_softc *sc = (void *)self;
    752 
    753 	AIC_TRACE(("aicattach  "));
    754 	sc->sc_state = 0;
    755 	aic_init(sc);	/* Init chip and driver */
    756 
    757 	/*
    758 	 * Fill in the prototype scsi_link
    759 	 */
    760 	sc->sc_link.adapter_softc = sc;
    761 	sc->sc_link.adapter_target = sc->sc_initiator;
    762 	sc->sc_link.adapter = &aic_switch;
    763 	sc->sc_link.device = &aic_dev;
    764 	sc->sc_link.openings = 2;
    765 
    766 	printf("\n");
    767 
    768 #ifdef NEWCONFIG
    769 	isa_establish(&sc->sc_id, &sc->sc_dev);
    770 #endif
    771 	sc->sc_ih = isa_intr_establish(ia->ia_irq, ISA_IST_EDGE, ISA_IPL_BIO,
    772 	    aicintr, sc);
    773 
    774 	config_found(self, &sc->sc_link, aicprint);
    775 }
    776 
    777 
    778 /* Initialize AIC6360 chip itself
    779  * The following conditions should hold:
    780  * aicprobe should have succeeded, i.e. the iobase address in aic_softc must
    781  * be valid.
    782  */
    783 void
    784 aic_reset(sc)
    785 	struct aic_softc *sc;
    786 {
    787 
    788 	outb(SCSITEST, 0);	/* Doc. recommends to clear these two */
    789 	outb(TEST, 0);		/* registers before operations commence */
    790 
    791 	/* Reset SCSI-FIFO and abort any transfers */
    792 	outb(SXFRCTL0, CHEN|CLRCH|CLRSTCNT);
    793 
    794 	/* Reset DMA-FIFO */
    795 	outb(DMACNTRL0, RSTFIFO);
    796 	outb(DMACNTRL1, 0);
    797 
    798 	outb(SCSISEQ, 0);	/* Disable all selection features */
    799 	outb(SXFRCTL1, 0);
    800 
    801 	outb(SIMODE0, 0x00);		/* Disable some interrupts */
    802 	outb(CLRSINT0, 0x7f);	/* Clear a slew of interrupts */
    803 
    804 	outb(SIMODE1, 0x00);		/* Disable some more interrupts */
    805 	outb(CLRSINT1, 0xef);	/* Clear another slew of interrupts */
    806 
    807 	outb(SCSIRATE, 0);	/* Disable synchronous transfers */
    808 
    809 	outb(CLRSERR, 0x07);	/* Haven't seen ant errors (yet) */
    810 
    811 	outb(SCSIID, sc->sc_initiator << OID_S); /* Set our SCSI-ID */
    812 	outb(BRSTCNTRL, EISA_BRST_TIM);
    813 }
    814 
    815 /* Pull the SCSI RST line for 500 us */
    816 void
    817 aic_scsi_reset(sc)
    818 	struct aic_softc *sc;
    819 {
    820 
    821 	outb(SCSISEQ, SCSIRSTO);
    822 	delay(500);
    823 	outb(SCSISEQ, 0);
    824 	delay(50);
    825 }
    826 
    827 /*
    828  * Initialize aic SCSI driver.
    829  */
    830 void
    831 aic_init(sc)
    832 	struct aic_softc *sc;
    833 {
    834 	struct aic_acb *acb;
    835 	int r;
    836 
    837 	aic_reset(sc);
    838 	aic_scsi_reset(sc);
    839 	aic_reset(sc);
    840 
    841 	if (sc->sc_state == 0) {
    842 		/* First time through; initialize. */
    843 		TAILQ_INIT(&sc->ready_list);
    844 		TAILQ_INIT(&sc->nexus_list);
    845 		TAILQ_INIT(&sc->free_list);
    846 		sc->sc_nexus = NULL;
    847 		acb = sc->sc_acb;
    848 		bzero(acb, sizeof(sc->sc_acb));
    849 		for (r = 0; r < sizeof(sc->sc_acb) / sizeof(*acb); r++) {
    850 			TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
    851 			acb++;
    852 		}
    853 		bzero(&sc->sc_tinfo, sizeof(sc->sc_tinfo));
    854 	} else {
    855 		/* Cancel any active commands. */
    856 		sc->sc_state = AIC_CLEANING;
    857 		if ((acb = sc->sc_nexus) != NULL) {
    858 			acb->xs->error = XS_DRIVER_STUFFUP;
    859 			untimeout(aic_timeout, acb);
    860 			aic_done(sc, acb);
    861 		}
    862 		while (acb = sc->nexus_list.tqh_first) {
    863 			acb->xs->error = XS_DRIVER_STUFFUP;
    864 			untimeout(aic_timeout, acb);
    865 			aic_done(sc, acb);
    866 		}
    867 	}
    868 
    869 	sc->sc_prevphase = PH_INVALID;
    870 	for (r = 0; r < 8; r++) {
    871 		struct aic_tinfo *ti = &sc->sc_tinfo[r];
    872 
    873 		ti->flags = 0;
    874 #if AIC_USE_SYNCHRONOUS
    875 		ti->flags |= DO_SYNC;
    876 		ti->period = sc->sc_minsync;
    877 		ti->offset = AIC_SYNC_REQ_ACK_OFS;
    878 #else
    879 		ti->period = ti->offset = 0;
    880 #endif
    881 #if AIC_USE_WIDE
    882 		ti->flags |= DO_WIDE;
    883 		ti->width = AIC_MAX_WIDTH;
    884 #else
    885 		ti->width = 0;
    886 #endif
    887 	}
    888 
    889 	sc->sc_state = AIC_IDLE;
    890 	outb(DMACNTRL0, INTEN);
    891 }
    892 
    893 void
    894 aic_free_acb(sc, acb, flags)
    895 	struct aic_softc *sc;
    896 	struct aic_acb *acb;
    897 	int flags;
    898 {
    899 	int s;
    900 
    901 	s = splbio();
    902 
    903 	acb->flags = ACB_FREE;
    904 	TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
    905 
    906 	/*
    907 	 * If there were none, wake anybody waiting for one to come free,
    908 	 * starting with queued entries.
    909 	 */
    910 	if (acb->chain.tqe_next == 0)
    911 		wakeup(&sc->free_list);
    912 
    913 	splx(s);
    914 }
    915 
    916 struct aic_acb *
    917 aic_get_acb(sc, flags)
    918 	struct aic_softc *sc;
    919 	int flags;
    920 {
    921 	struct aic_acb *acb;
    922 	int s;
    923 
    924 	s = splbio();
    925 
    926 	while ((acb = sc->free_list.tqh_first) == NULL &&
    927 	       (flags & SCSI_NOSLEEP) == 0)
    928 		tsleep(&sc->free_list, PRIBIO, "aicacb", 0);
    929 	if (acb) {
    930 		TAILQ_REMOVE(&sc->free_list, acb, chain);
    931 		acb->flags = ACB_ACTIVE;
    932 	}
    933 
    934 	splx(s);
    935 	return acb;
    936 }
    937 
    938 /*
    940  * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
    941  */
    942 
    943 /*
    944  * Expected sequence:
    945  * 1) Command inserted into ready list
    946  * 2) Command selected for execution
    947  * 3) Command won arbitration and has selected target device
    948  * 4) Send message out (identify message, eventually also sync.negotiations)
    949  * 5) Send command
    950  * 5a) Receive disconnect message, disconnect.
    951  * 5b) Reselected by target
    952  * 5c) Receive identify message from target.
    953  * 6) Send or receive data
    954  * 7) Receive status
    955  * 8) Receive message (command complete etc.)
    956  * 9) If status == SCSI_CHECK construct a synthetic request sense SCSI cmd.
    957  *    Repeat 2-8 (no disconnects please...)
    958  */
    959 
    960 /*
    961  * Start a SCSI-command
    962  * This function is called by the higher level SCSI-driver to queue/run
    963  * SCSI-commands.
    964  */
    965 int
    966 aic_scsi_cmd(xs)
    967 	struct scsi_xfer *xs;
    968 {
    969 	struct scsi_link *sc_link = xs->sc_link;
    970 	struct aic_softc *sc = sc_link->adapter_softc;
    971 	struct aic_acb *acb;
    972 	int s, flags;
    973 
    974 	AIC_TRACE(("aic_scsi_cmd  "));
    975 	AIC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
    976 	    sc_link->target));
    977 
    978 	flags = xs->flags;
    979 	if ((flags & (ITSDONE|INUSE)) != INUSE) {
    980 		printf("%s: done or not in use?\n", sc->sc_dev.dv_xname);
    981 		xs->flags &= ~ITSDONE;
    982 		xs->flags |= INUSE;
    983 	}
    984 
    985 	if ((acb = aic_get_acb(sc, flags)) == NULL) {
    986 		xs->error = XS_DRIVER_STUFFUP;
    987 		return TRY_AGAIN_LATER;
    988 	}
    989 
    990 	/* Initialize acb */
    991 	acb->xs = xs;
    992 	bcopy(xs->cmd, &acb->scsi_cmd, xs->cmdlen);
    993 	acb->scsi_cmd_length = xs->cmdlen;
    994 	acb->data_addr = xs->data;
    995 	acb->data_length = xs->datalen;
    996 	acb->target_stat = 0;
    997 
    998 	s = splbio();
    999 
   1000 	TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
   1001 	if (sc->sc_state == AIC_IDLE)
   1002 		aic_sched(sc);
   1003 
   1004 	if ((flags & SCSI_POLL) == 0) { /* Almost done. Wait outside */
   1005 		timeout(aic_timeout, acb, (xs->timeout * hz) / 1000);
   1006 		splx(s);
   1007 		return SUCCESSFULLY_QUEUED;
   1008 	}
   1009 
   1010 	splx(s);
   1011 
   1012 	/* Not allowed to use interrupts, use polling instead */
   1013 	if (aic_poll(sc, xs, xs->timeout)) {
   1014 		aic_timeout(acb);
   1015 		if (aic_poll(sc, xs, 2000))
   1016 			aic_timeout(acb);
   1017 	}
   1018 	return COMPLETE;
   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  "));
   1030 	if (bp->b_bcount > (AIC_NSEG << PGSHIFT))
   1031 		bp->b_bcount = (AIC_NSEG << PGSHIFT);
   1032 	minphys(bp);
   1033 }
   1034 
   1035 /*
   1036  * Used when interrupt driven I/O isn't allowed, e.g. during boot.
   1037  */
   1038 int
   1039 aic_poll(sc, xs, count)
   1040 	struct aic_softc *sc;
   1041 	struct scsi_xfer *xs;
   1042 	int count;
   1043 {
   1044 
   1045 	AIC_TRACE(("aic_poll  "));
   1046 	while (count) {
   1047 		/*
   1048 		 * If we had interrupts enabled, would we
   1049 		 * have got an interrupt?
   1050 		 */
   1051 		if ((inb(DMASTAT) & INTSTAT) != 0)
   1052 			aicintr(sc);
   1053 		if ((xs->flags & ITSDONE) != 0)
   1054 			return 0;
   1055 		delay(1000);
   1056 		count--;
   1057 	}
   1058 	return 1;
   1059 }
   1060 
   1061 /*
   1063  * LOW LEVEL SCSI UTILITIES
   1064  */
   1065 
   1066 #define aic_sched_msgout(m) \
   1067 	do {							\
   1068 		if (sc->sc_msgpriq == 0)			\
   1069 			outb(SCSISIG, sc->sc_phase|ATNO);	\
   1070 		sc->sc_msgpriq |= (m);				\
   1071 	} while (0)
   1072 
   1073 #if AIC_USE_SYNCHRONOUS
   1074 /*
   1075  * Set synchronous transfer offset and period.
   1076  */
   1077 static inline void
   1078 aic_setsync(sc, ti)
   1079 	struct aic_softc *sc;
   1080 	struct aic_tinfo *ti;
   1081 {
   1082 
   1083 	if (ti->offset != 0)
   1084 		outb(SCSIRATE,
   1085 		    ((ti->period * sc->sc_freq) / 250 - 2) << 4 | ti->offset);
   1086 	else
   1087 		outb(SCSIRATE, 0);
   1088 }
   1089 #else
   1090 #define	aic_setsync(sc, ti)
   1091 #endif
   1092 
   1093 /*
   1094  * Start a selection.  This is used by aic_sched() to select an idle target,
   1095  * and by aic_done() to immediately reselect a target to get sense information.
   1096  */
   1097 void
   1098 aic_select(sc, acb)
   1099 	struct aic_softc *sc;
   1100 	struct aic_acb *acb;
   1101 {
   1102 	struct scsi_link *sc_link = acb->xs->sc_link;
   1103 	int target = sc_link->target;
   1104 	struct aic_tinfo *ti = &sc->sc_tinfo[target];
   1105 
   1106 	outb(SCSIID, sc->sc_initiator << OID_S | target);
   1107 	aic_setsync(sc, ti);
   1108 	outb(SXFRCTL1, STIMO_256ms|ENSTIMER);
   1109 
   1110 	/* Always enable reselections. */
   1111 	outb(SIMODE0, ENSELDI|ENSELDO);
   1112 	outb(SIMODE1, ENSCSIRST|ENSELTIMO);
   1113 	outb(SCSISEQ, ENRESELI|ENSELO|ENAUTOATNO);
   1114 
   1115 	sc->sc_state = AIC_SELECTING;
   1116 }
   1117 
   1118 int
   1119 aic_reselect(sc, message)
   1120 	struct aic_softc *sc;
   1121 	u_char message;
   1122 {
   1123 	u_char selid, target, lun;
   1124 	struct aic_acb *acb;
   1125 	struct scsi_link *sc_link;
   1126 	struct aic_tinfo *ti;
   1127 
   1128 	/*
   1129 	 * The SCSI chip made a snapshot of the data bus while the reselection
   1130 	 * was being negotiated.  This enables us to determine which target did
   1131 	 * the reselect.
   1132 	 */
   1133 	selid = sc->sc_selid & ~(1 << sc->sc_initiator);
   1134 	if (selid & (selid - 1)) {
   1135 		printf("%s: reselect with invalid selid %02x; sending DEVICE RESET\n",
   1136 		    sc->sc_dev.dv_xname, selid);
   1137 		AIC_BREAK();
   1138 		goto reset;
   1139 	}
   1140 
   1141 	/* Search wait queue for disconnected cmd
   1142 	 * The list should be short, so I haven't bothered with
   1143 	 * any more sophisticated structures than a simple
   1144 	 * singly linked list.
   1145 	 */
   1146 	target = ffs(selid) - 1;
   1147 	lun = message & 0x07;
   1148 	for (acb = sc->nexus_list.tqh_first; acb != NULL;
   1149 	     acb = acb->chain.tqe_next) {
   1150 		sc_link = acb->xs->sc_link;
   1151 		if (sc_link->target == target && sc_link->lun == lun)
   1152 			break;
   1153 	}
   1154 	if (acb == NULL) {
   1155 		printf("%s: reselect from target %d lun %d with no nexus; sending ABORT\n",
   1156 		    sc->sc_dev.dv_xname, target, lun);
   1157 		AIC_BREAK();
   1158 		goto abort;
   1159 	}
   1160 
   1161 	/* Make this nexus active again. */
   1162 	TAILQ_REMOVE(&sc->nexus_list, acb, chain);
   1163 	sc->sc_state = AIC_CONNECTED;
   1164 	sc->sc_nexus = acb;
   1165 	ti = &sc->sc_tinfo[target];
   1166 	ti->lubusy |= (1 << lun);
   1167 	aic_setsync(sc, ti);
   1168 
   1169 	/* Do an implicit RESTORE POINTERS. */
   1170 	sc->sc_dp = acb->data_addr;
   1171 	sc->sc_dleft = acb->data_length;
   1172 	sc->sc_cp = (u_char *)&acb->scsi_cmd;
   1173 	sc->sc_cleft = acb->scsi_cmd_length;
   1174 
   1175 	return (0);
   1176 
   1177 reset:
   1178 	sc->sc_flags |= AIC_ABORTING;
   1179 	aic_sched_msgout(SEND_DEV_RESET);
   1180 	return (1);
   1181 
   1182 abort:
   1183 	sc->sc_flags |= AIC_ABORTING;
   1184 	aic_sched_msgout(SEND_ABORT);
   1185 	return (1);
   1186 }
   1187 
   1188 /*
   1190  * Schedule a SCSI operation.  This has now been pulled out of the interrupt
   1191  * handler so that we may call it from aic_scsi_cmd and aic_done.  This may
   1192  * save us an unecessary interrupt just to get things going.  Should only be
   1193  * called when state == AIC_IDLE and at bio pl.
   1194  */
   1195 void
   1196 aic_sched(sc)
   1197 	register struct aic_softc *sc;
   1198 {
   1199 	struct aic_acb *acb;
   1200 	struct scsi_link *sc_link;
   1201 	struct aic_tinfo *ti;
   1202 
   1203 	/*
   1204 	 * Find first acb in ready queue that is for a target/lunit pair that
   1205 	 * is not busy.
   1206 	 */
   1207 	outb(CLRSINT1, CLRSELTIMO|CLRBUSFREE|CLRSCSIPERR);
   1208 	for (acb = sc->ready_list.tqh_first; acb != NULL;
   1209 	    acb = acb->chain.tqe_next) {
   1210 		sc_link = acb->xs->sc_link;
   1211 		ti = &sc->sc_tinfo[sc_link->target];
   1212 		if ((ti->lubusy & (1 << sc_link->lun)) == 0) {
   1213 			AIC_MISC(("selecting %d:%d  ",
   1214 			    sc_link->target, sc_link->lun));
   1215 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
   1216 			sc->sc_nexus = acb;
   1217 			aic_select(sc, acb);
   1218 			return;
   1219 		} else
   1220 			AIC_MISC(("%d:%d busy\n",
   1221 			    sc_link->target, sc_link->lun));
   1222 	}
   1223 	AIC_MISC(("idle  "));
   1224 	/* Nothing to start; just enable reselections and wait. */
   1225 	outb(SIMODE0, ENSELDI);
   1226 	outb(SIMODE1, ENSCSIRST);
   1227 	outb(SCSISEQ, ENRESELI);
   1228 }
   1229 
   1230 /*
   1232  * POST PROCESSING OF SCSI_CMD (usually current)
   1233  */
   1234 void
   1235 aic_done(sc, acb)
   1236 	struct aic_softc *sc;
   1237 	struct aic_acb *acb;
   1238 {
   1239 	struct scsi_xfer *xs = acb->xs;
   1240 	struct scsi_link *sc_link = xs->sc_link;
   1241 	struct aic_tinfo *ti = &sc->sc_tinfo[sc_link->target];
   1242 
   1243 	AIC_TRACE(("aic_done  "));
   1244 
   1245 	/*
   1246 	 * Now, if we've come here with no error code, i.e. we've kept the
   1247 	 * initial XS_NOERROR, and the status code signals that we should
   1248 	 * check sense, we'll need to set up a request sense cmd block and
   1249 	 * push the command back into the ready queue *before* any other
   1250 	 * commands for this target/lunit, else we lose the sense info.
   1251 	 * We don't support chk sense conditions for the request sense cmd.
   1252 	 */
   1253 	if (xs->error == XS_NOERROR) {
   1254 		if (acb->flags == ACB_ABORTED) {
   1255 			xs->error = XS_DRIVER_STUFFUP;
   1256 		} else if (acb->flags == ACB_CHKSENSE) {
   1257 			xs->error = XS_SENSE;
   1258 		} else if (acb->target_stat == SCSI_CHECK) {
   1259 			struct scsi_sense *ss = (void *)&acb->scsi_cmd;
   1260 
   1261 			AIC_MISC(("requesting sense  "));
   1262 			/* First, save the return values */
   1263 			xs->resid = acb->data_length;
   1264 			xs->status = acb->target_stat;
   1265 			/* Next, setup a request sense command block */
   1266 			bzero(ss, sizeof(*ss));
   1267 			ss->opcode = REQUEST_SENSE;
   1268 			ss->byte2 = sc_link->lun << 5;
   1269 			ss->length = sizeof(struct scsi_sense_data);
   1270 			acb->scsi_cmd_length = sizeof(*ss);
   1271 			acb->data_addr = (char *)&xs->sense;
   1272 			acb->data_length = sizeof(struct scsi_sense_data);
   1273 			acb->flags = ACB_CHKSENSE;
   1274 			ti->senses++;
   1275 			ti->lubusy &= ~(1<<sc_link->lun);
   1276 			if (acb == sc->sc_nexus) {
   1277 				aic_select(sc, acb);
   1278 			} else {
   1279 				TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
   1280 			}
   1281 			return;
   1282 		} else {
   1283 			xs->resid = acb->data_length;
   1284 		}
   1285 	}
   1286 
   1287 	xs->flags |= ITSDONE;
   1288 
   1289 #if AIC_DEBUG
   1290 	if ((aic_debug & AIC_SHOWMISC) != 0) {
   1291 		if (xs->resid != 0)
   1292 			printf("resid=%d ", xs->resid);
   1293 		if (xs->error == XS_SENSE)
   1294 			printf("sense=0x%02x\n", xs->sense.error_code);
   1295 		else
   1296 			printf("error=%d\n", xs->error);
   1297 	}
   1298 #endif
   1299 
   1300 	/*
   1301 	 * Remove the ACB from whatever queue it's on.  We have to do a bit of
   1302 	 * a hack to figure out which queue it's on.  Note that it is *not*
   1303 	 * necessary to cdr down the ready queue, but we must cdr down the
   1304 	 * nexus queue and see if it's there, so we can mark the unit as no
   1305 	 * longer busy.  This code is sickening, but it works.
   1306 	 */
   1307 	if (acb == sc->sc_nexus) {
   1308 		ti->lubusy &= ~(1 << sc_link->lun);
   1309 		sc->sc_state = AIC_IDLE;
   1310 		sc->sc_nexus = NULL;
   1311 		aic_sched(sc);
   1312 	} else
   1313 		aic_dequeue(sc, acb);
   1314 
   1315 	aic_free_acb(sc, acb, xs->flags);
   1316 	ti->cmds++;
   1317 	scsi_done(xs);
   1318 }
   1319 
   1320 void
   1321 aic_dequeue(sc, acb)
   1322 	struct aic_softc *sc;
   1323 	struct aic_acb *acb;
   1324 {
   1325 	struct scsi_link *sc_link = acb->xs->sc_link;
   1326 	struct aic_tinfo *ti = &sc->sc_tinfo[sc_link->target];
   1327 
   1328 	if (sc->ready_list.tqh_last == &acb->chain.tqe_next) {
   1329 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
   1330 	} else {
   1331 		register struct aic_acb *acb2;
   1332 		for (acb2 = sc->nexus_list.tqh_first; acb2 != NULL;
   1333 		    acb2 = acb2->chain.tqe_next) {
   1334 			if (acb2 == acb)
   1335 				break;
   1336 		}
   1337 		if (acb2 != NULL) {
   1338 			TAILQ_REMOVE(&sc->nexus_list, acb, chain);
   1339 			ti->lubusy &= ~(1 << sc_link->lun);
   1340 		} else if (acb->chain.tqe_next) {
   1341 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
   1342 		} else {
   1343 			printf("%s: can't find matching acb\n",
   1344 			    sc->sc_dev.dv_xname);
   1345 			Debugger();
   1346 		}
   1347 	}
   1348 }
   1349 
   1350 /*
   1352  * INTERRUPT/PROTOCOL ENGINE
   1353  */
   1354 
   1355 #define IS1BYTEMSG(m) (((m) != 0x01 && (m) < 0x20) || (m) >= 0x80)
   1356 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
   1357 #define ISEXTMSG(m) ((m) == 0x01)
   1358 
   1359 /*
   1360  * Precondition:
   1361  * The SCSI bus is already in the MSGI phase and there is a message byte
   1362  * on the bus, along with an asserted REQ signal.
   1363  */
   1364 int
   1365 aic_msgin(sc)
   1366 	register struct aic_softc *sc;
   1367 {
   1368 	u_char sstat1;
   1369 	int n;
   1370 
   1371 	AIC_TRACE(("aic_msgin  "));
   1372 
   1373 	if (sc->sc_prevphase == PH_MSGIN) {
   1374 		/* This is a continuation of the previous message. */
   1375 		n = sc->sc_imp - sc->sc_imess;
   1376 		goto nextbyte;
   1377 	}
   1378 
   1379 	/* This is a new MESSAGE IN phase.  Clean up our state. */
   1380 	sc->sc_flags &= ~AIC_DROP_MSGIN;
   1381 
   1382 nextmsg:
   1383 	n = 0;
   1384 	sc->sc_imp = &sc->sc_imess[n];
   1385 
   1386 nextbyte:
   1387 	/*
   1388 	 * Read a whole message, but don't ack the last byte.  If we reject the
   1389 	 * message, we have to assert ATN during the message transfer phase
   1390 	 * itself.
   1391 	 */
   1392 	for (;;) {
   1393 		for (;;) {
   1394 			sstat1 = inb(SSTAT1);
   1395 			if ((sstat1 & (REQINIT|BUSFREE)) != 0)
   1396 				break;
   1397 			/* Wait for REQINIT.  XXX Need timeout. */
   1398 		}
   1399 		if ((sstat1 & (PHASECHG|BUSFREE)) != 0) {
   1400 			/*
   1401 			 * Target left MESSAGE IN, probably because it
   1402 			 * a) noticed our ATN signal, or
   1403 			 * b) ran out of messages.
   1404 			 */
   1405 			return (1);
   1406 		}
   1407 
   1408 		/* If parity error, just dump everything on the floor. */
   1409 		if ((sstat1 & SCSIPERR) != 0) {
   1410 			aic_sched_msgout(SEND_PARITY_ERROR);
   1411 			sc->sc_flags |= AIC_DROP_MSGIN;
   1412 		}
   1413 
   1414 		/* Gather incoming message bytes if needed. */
   1415 		if ((sc->sc_flags & AIC_DROP_MSGIN) == 0) {
   1416 			if (n >= AIC_MAX_MSG_LEN) {
   1417 				(void) inb(SCSIDAT);
   1418 				aic_sched_msgout(SEND_REJECT);
   1419 				sc->sc_flags |= AIC_DROP_MSGIN;
   1420 			} else {
   1421 				*sc->sc_imp++ = inb(SCSIDAT);
   1422 				n++;
   1423 				/*
   1424 				 * This testing is suboptimal, but most
   1425 				 * messages will be of the one byte variety, so
   1426 				 * it should not affect performance
   1427 				 * significantly.
   1428 				 */
   1429 				if (n == 1 && IS1BYTEMSG(sc->sc_imess[0]))
   1430 					break;
   1431 				if (n == 2 && IS2BYTEMSG(sc->sc_imess[0]))
   1432 					break;
   1433 				if (n >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
   1434 				    n == sc->sc_imess[1] + 2)
   1435 					break;
   1436 			}
   1437 		} else
   1438 			(void) inb(SCSIDAT);
   1439 
   1440 		/*
   1441 		 * If we reach this spot we're either:
   1442 		 * a) in the middle of a multi-byte message, or
   1443 		 * b) dropping bytes.
   1444 		 */
   1445 		outb(SXFRCTL0, CHEN|SPIOEN);
   1446 		/* Ack the last byte read. */
   1447 		(void) inb(SCSIDAT);
   1448 		outb(SXFRCTL0, CHEN);
   1449 		while ((inb(SCSISIG) & ACKI) != 0)
   1450 			;
   1451 	}
   1452 
   1453 	AIC_MISC(("n=%d imess=0x%02x  ", n, sc->sc_imess[0]));
   1454 
   1455 	/* We now have a complete message.  Parse it. */
   1456 	switch (sc->sc_state) {
   1457 		struct aic_acb *acb;
   1458 		struct scsi_link *sc_link;
   1459 		struct aic_tinfo *ti;
   1460 
   1461 	case AIC_CONNECTED:
   1462 		AIC_ASSERT(sc->sc_nexus != NULL);
   1463 		acb = sc->sc_nexus;
   1464 		ti = &sc->sc_tinfo[acb->xs->sc_link->target];
   1465 
   1466 		switch (sc->sc_imess[0]) {
   1467 		case MSG_CMDCOMPLETE:
   1468 			if (sc->sc_dleft < 0) {
   1469 				sc_link = acb->xs->sc_link;
   1470 				printf("%s: %d extra bytes from %d:%d\n",
   1471 				    sc->sc_dev.dv_xname, -sc->sc_dleft,
   1472 				    sc_link->target, sc_link->lun);
   1473 				acb->data_length = 0;
   1474 			}
   1475 			acb->xs->resid = acb->data_length = sc->sc_dleft;
   1476 			sc->sc_state = AIC_CMDCOMPLETE;
   1477 			break;
   1478 
   1479 		case MSG_PARITY_ERROR:
   1480 			/* Resend the last message. */
   1481 			aic_sched_msgout(sc->sc_lastmsg);
   1482 			break;
   1483 
   1484 		case MSG_MESSAGE_REJECT:
   1485 			AIC_MISC(("message rejected %02x  ", sc->sc_lastmsg));
   1486 			switch (sc->sc_lastmsg) {
   1487 #if AIC_USE_SYNCHRONOUS + AIC_USE_WIDE
   1488 			case SEND_IDENTIFY:
   1489 				ti->flags &= ~(DO_SYNC|DO_WIDE);
   1490 				ti->period = ti->offset = 0;
   1491 				aic_setsync(sc, ti);
   1492 				ti->width = 0;
   1493 				break;
   1494 #endif
   1495 #if AIC_USE_SYNCHRONOUS
   1496 			case SEND_SDTR:
   1497 				ti->flags &= ~DO_SYNC;
   1498 				ti->period = ti->offset = 0;
   1499 				aic_setsync(sc, ti);
   1500 				break;
   1501 #endif
   1502 #if AIC_USE_WIDE
   1503 			case SEND_WDTR:
   1504 				ti->flags &= ~DO_WIDE;
   1505 				ti->width = 0;
   1506 				break;
   1507 #endif
   1508 			case SEND_INIT_DET_ERR:
   1509 				sc->sc_flags |= AIC_ABORTING;
   1510 				aic_sched_msgout(SEND_ABORT);
   1511 				break;
   1512 			}
   1513 			break;
   1514 
   1515 		case MSG_NOOP:
   1516 			break;
   1517 
   1518 		case MSG_DISCONNECT:
   1519 			ti->dconns++;
   1520 			sc->sc_state = AIC_DISCONNECT;
   1521 			break;
   1522 
   1523 		case MSG_SAVEDATAPOINTER:
   1524 			acb->data_addr = sc->sc_dp;
   1525 			acb->data_length = sc->sc_dleft;
   1526 			break;
   1527 
   1528 		case MSG_RESTOREPOINTERS:
   1529 			sc->sc_dp = acb->data_addr;
   1530 			sc->sc_dleft = acb->data_length;
   1531 			sc->sc_cp = (u_char *)&acb->scsi_cmd;
   1532 			sc->sc_cleft = acb->scsi_cmd_length;
   1533 			break;
   1534 
   1535 		case MSG_EXTENDED:
   1536 			switch (sc->sc_imess[2]) {
   1537 #if AIC_USE_SYNCHRONOUS
   1538 			case MSG_EXT_SDTR:
   1539 				if (sc->sc_imess[1] != 3)
   1540 					goto reject;
   1541 				ti->period = sc->sc_imess[3];
   1542 				ti->offset = sc->sc_imess[4];
   1543 				ti->flags &= ~DO_SYNC;
   1544 				if (ti->offset == 0) {
   1545 				} else if (ti->period < sc->sc_minsync ||
   1546 					   ti->period > sc->sc_maxsync ||
   1547 					   ti->offset > 8) {
   1548 					ti->period = ti->offset = 0;
   1549 					aic_sched_msgout(SEND_SDTR);
   1550 				} else {
   1551 					sc_print_addr(acb->xs->sc_link);
   1552 					printf("sync, offset %d, period %dnsec\n",
   1553 					    ti->offset, ti->period * 4);
   1554 				}
   1555 				aic_setsync(sc, ti);
   1556 				break;
   1557 #endif
   1558 
   1559 #if AIC_USE_WIDE
   1560 			case MSG_EXT_WDTR:
   1561 				if (sc->sc_imess[1] != 2)
   1562 					goto reject;
   1563 				ti->width = sc->sc_imess[3];
   1564 				ti->flags &= ~DO_WIDE;
   1565 				if (ti->width == 0) {
   1566 				} else if (ti->width > AIC_MAX_WIDTH) {
   1567 					ti->width = 0;
   1568 					aic_sched_msgout(SEND_WDTR);
   1569 				} else {
   1570 					sc_print_addr(acb->xs->sc_link);
   1571 					printf("wide, width %d\n",
   1572 					    1 << (3 + ti->width));
   1573 				}
   1574 				break;
   1575 #endif
   1576 
   1577 			default:
   1578 				printf("%s: unrecognized MESSAGE EXTENDED; sending REJECT\n",
   1579 				    sc->sc_dev.dv_xname);
   1580 				AIC_BREAK();
   1581 				goto reject;
   1582 			}
   1583 			break;
   1584 
   1585 		default:
   1586 			printf("%s: unrecognized MESSAGE; sending REJECT\n",
   1587 			    sc->sc_dev.dv_xname);
   1588 			AIC_BREAK();
   1589 		reject:
   1590 			aic_sched_msgout(SEND_REJECT);
   1591 			break;
   1592 		}
   1593 		break;
   1594 
   1595 	case AIC_RESELECTED:
   1596 		if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
   1597 			printf("%s: reselect without IDENTIFY; sending DEVICE RESET\n",
   1598 			    sc->sc_dev.dv_xname);
   1599 			AIC_BREAK();
   1600 			goto reset;
   1601 		}
   1602 
   1603 		(void) aic_reselect(sc, sc->sc_imess[0]);
   1604 		break;
   1605 
   1606 	default:
   1607 		printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
   1608 		    sc->sc_dev.dv_xname);
   1609 		AIC_BREAK();
   1610 	reset:
   1611 		sc->sc_flags |= AIC_ABORTING;
   1612 		aic_sched_msgout(SEND_DEV_RESET);
   1613 		break;
   1614 
   1615 	abort:
   1616 		sc->sc_flags |= AIC_ABORTING;
   1617 		aic_sched_msgout(SEND_ABORT);
   1618 		break;
   1619 	}
   1620 
   1621 	outb(SXFRCTL0, CHEN|SPIOEN);
   1622 	/* Ack the last message byte. */
   1623 	(void) inb(SCSIDAT);
   1624 	outb(SXFRCTL0, CHEN);
   1625 	while ((inb(SCSISIG) & ACKI) != 0)
   1626 		;
   1627 
   1628 	/* Go get the next message, if any. */
   1629 	goto nextmsg;
   1630 
   1631 out:
   1632 	AIC_MISC(("n=%d imess=0x%02x  ", n, sc->sc_imess[0]));
   1633 	return (0);
   1634 }
   1635 
   1636 /*
   1637  * Send the highest priority, scheduled message.
   1638  */
   1639 void
   1640 aic_msgout(sc)
   1641 	register struct aic_softc *sc;
   1642 {
   1643 	struct aic_acb *acb;
   1644 	struct aic_tinfo *ti;
   1645 	u_char sstat1;
   1646 	int n;
   1647 
   1648 	AIC_TRACE(("aic_msgout  "));
   1649 
   1650 	/*
   1651 	 * Set ATN.  If we're just sending a trivial 1-byte message, we'll
   1652 	 * clear ATN later on anyway.
   1653 	 */
   1654 	outb(SCSISIG, PH_MSGOUT|ATNO);
   1655 	/* Reset the FIFO. */
   1656 	outb(DMACNTRL0, RSTFIFO);
   1657 	/* Enable REQ/ACK protocol. */
   1658 	outb(SXFRCTL0, CHEN|SPIOEN);
   1659 
   1660 	if (sc->sc_prevphase == PH_MSGOUT) {
   1661 		if (sc->sc_omp == sc->sc_omess) {
   1662 			/*
   1663 			 * This is a retransmission.
   1664 			 *
   1665 			 * We get here if the target stayed in MESSAGE OUT
   1666 			 * phase.  Section 5.1.9.2 of the SCSI 2 spec indicates
   1667 			 * that all of the previously transmitted messages must
   1668 			 * be sent again, in the same order.  Therefore, we
   1669 			 * requeue all the previously transmitted messages, and
   1670 			 * start again from the top.  Our simple priority
   1671 			 * scheme keeps the messages in the right order.
   1672 			 */
   1673 			AIC_MISC(("retransmitting  "));
   1674 			sc->sc_msgpriq |= sc->sc_msgoutq;
   1675 		} else {
   1676 			/* This is a continuation of the previous message. */
   1677 			n = sc->sc_omp - sc->sc_omess;
   1678 			goto nextbyte;
   1679 		}
   1680 	}
   1681 
   1682 	/* No messages transmitted so far. */
   1683 	sc->sc_msgoutq = 0;
   1684 	sc->sc_lastmsg = 0;
   1685 
   1686 nextmsg:
   1687 	/* Pick up highest priority message. */
   1688 	sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
   1689 	sc->sc_msgpriq &= ~sc->sc_currmsg;
   1690 	sc->sc_msgoutq |= sc->sc_currmsg;
   1691 
   1692 	/* Build the outgoing message data. */
   1693 	switch (sc->sc_currmsg) {
   1694 	case SEND_IDENTIFY:
   1695 		if (sc->sc_state != AIC_CONNECTED) {
   1696 			printf("%s: SEND_IDENTIFY while not connected; sending NOOP\n",
   1697 			    sc->sc_dev.dv_xname);
   1698 			AIC_BREAK();
   1699 			goto noop;
   1700 		}
   1701 		AIC_ASSERT(sc->sc_nexus != NULL);
   1702 		acb = sc->sc_nexus;
   1703 		sc->sc_omess[0] = MSG_IDENTIFY(acb->xs->sc_link->lun, 1);
   1704 		n = 1;
   1705 		break;
   1706 
   1707 #if AIC_USE_SYNCHRONOUS
   1708 	case SEND_SDTR:
   1709 		if (sc->sc_state != AIC_CONNECTED) {
   1710 			printf("%s: SEND_SDTR while not connected; sending NOOP\n",
   1711 			    sc->sc_dev.dv_xname);
   1712 			AIC_BREAK();
   1713 			goto noop;
   1714 		}
   1715 		AIC_ASSERT(sc->sc_nexus != NULL);
   1716 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->target];
   1717 		sc->sc_omess[4] = MSG_EXTENDED;
   1718 		sc->sc_omess[3] = 3;
   1719 		sc->sc_omess[2] = MSG_EXT_SDTR;
   1720 		sc->sc_omess[1] = ti->period >> 2;
   1721 		sc->sc_omess[0] = ti->offset;
   1722 		n = 5;
   1723 		break;
   1724 #endif
   1725 
   1726 #if AIC_USE_WIDE
   1727 	case SEND_WDTR:
   1728 		if (sc->sc_state != AIC_CONNECTED) {
   1729 			printf("%s: SEND_WDTR while not connected; sending NOOP\n",
   1730 			    sc->sc_dev.dv_xname);
   1731 			AIC_BREAK();
   1732 			goto noop;
   1733 		}
   1734 		AIC_ASSERT(sc->sc_nexus != NULL);
   1735 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->target];
   1736 		sc->sc_omess[3] = MSG_EXTENDED;
   1737 		sc->sc_omess[2] = 2;
   1738 		sc->sc_omess[1] = MSG_EXT_WDTR;
   1739 		sc->sc_omess[0] = ti->width;
   1740 		n = 4;
   1741 		break;
   1742 #endif
   1743 
   1744 	case SEND_DEV_RESET:
   1745 		sc->sc_omess[0] = MSG_BUS_DEV_RESET;
   1746 		n = 1;
   1747 		break;
   1748 
   1749 	case SEND_REJECT:
   1750 		sc->sc_omess[0] = MSG_MESSAGE_REJECT;
   1751 		n = 1;
   1752 		break;
   1753 
   1754 	case SEND_PARITY_ERROR:
   1755 		sc->sc_omess[0] = MSG_PARITY_ERROR;
   1756 		n = 1;
   1757 		break;
   1758 
   1759 	case SEND_INIT_DET_ERR:
   1760 		sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
   1761 		n = 1;
   1762 		break;
   1763 
   1764 	case SEND_ABORT:
   1765 		sc->sc_omess[0] = MSG_ABORT;
   1766 		n = 1;
   1767 		break;
   1768 
   1769 	case 0:
   1770 #ifdef AIC_PICKY
   1771 		printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
   1772 		    sc->sc_dev.dv_xname);
   1773 		AIC_BREAK();
   1774 #endif
   1775 	noop:
   1776 		sc->sc_omess[0] = MSG_NOOP;
   1777 		n = 1;
   1778 		break;
   1779 
   1780 	default:
   1781 		printf("%s: weird MESSAGE OUT; sending NOOP\n",
   1782 		    sc->sc_dev.dv_xname);
   1783 		AIC_BREAK();
   1784 		goto noop;
   1785 	}
   1786 	sc->sc_omp = &sc->sc_omess[n];
   1787 
   1788 nextbyte:
   1789 	/* Send message bytes. */
   1790 	for (;;) {
   1791 		for (;;) {
   1792 			sstat1 = inb(SSTAT1);
   1793 			if ((sstat1 & (REQINIT|BUSFREE)) != 0)
   1794 				break;
   1795 			/* Wait for REQINIT.  XXX Need timeout. */
   1796 		}
   1797 		if ((sstat1 & (PHASECHG|BUSFREE)) != 0) {
   1798 			/*
   1799 			 * Target left MESSAGE OUT, possibly to reject
   1800 			 * our message.
   1801 			 */
   1802 			goto out;
   1803 		}
   1804 
   1805 		/* Clear ATN before last byte if this is the last message. */
   1806 		if (n == 1 && sc->sc_msgpriq == 0)
   1807 			outb(CLRSINT1, CLRATNO);
   1808 		/* Send message byte. */
   1809 		outb(SCSIDAT, *--sc->sc_omp);
   1810 		--n;
   1811 		/* Keep track of the last message we've sent any bytes of. */
   1812 		sc->sc_lastmsg = sc->sc_currmsg;
   1813 		/* Wait for ACK to be negated.  XXX Need timeout. */
   1814 		while ((inb(SCSISIG) & ACKI) != 0)
   1815 			;
   1816 
   1817 		if (n == 0)
   1818 			break;
   1819 	}
   1820 
   1821 	/* We get here only if the entire message has been transmitted. */
   1822 	if (sc->sc_msgpriq != 0) {
   1823 		/* There are more outgoing messages. */
   1824 		goto nextmsg;
   1825 	}
   1826 
   1827 	/*
   1828 	 * The last message has been transmitted.  We need to remember the last
   1829 	 * message transmitted (in case the target switches to MESSAGE IN phase
   1830 	 * and sends a MESSAGE REJECT), and the list of messages transmitted
   1831 	 * this time around (in case the target stays in MESSAGE OUT phase to
   1832 	 * request a retransmit).
   1833 	 */
   1834 
   1835 out:
   1836 	/* Disable REQ/ACK protocol. */
   1837 	outb(SXFRCTL0, CHEN);
   1838 }
   1839 
   1840 /* aic_dataout_pio: perform a data transfer using the FIFO datapath in the aic6360
   1842  * Precondition: The SCSI bus should be in the DOUT phase, with REQ asserted
   1843  * and ACK deasserted (i.e. waiting for a data byte)
   1844  * This new revision has been optimized (I tried) to make the common case fast,
   1845  * and the rarer cases (as a result) somewhat more comlex
   1846  */
   1847 int
   1848 aic_dataout_pio(sc, p, n)
   1849 	register struct aic_softc *sc;
   1850 	u_char *p;
   1851 	int n;
   1852 {
   1853 	register u_char dmastat;
   1854 	int out = 0;
   1855 #define DOUTAMOUNT 128		/* Full FIFO */
   1856 
   1857 	/* Clear host FIFO and counter. */
   1858 	outb(DMACNTRL0, RSTFIFO|WRITE);
   1859 	/* Enable FIFOs. */
   1860 	outb(SXFRCTL0, SCSIEN|DMAEN|CHEN);
   1861 	outb(DMACNTRL0, ENDMA|DWORDPIO|WRITE);
   1862 
   1863 	/* Turn off ENREQINIT for now. */
   1864 	outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENPHASECHG);
   1865 
   1866 	/* I have tried to make the main loop as tight as possible.  This
   1867 	 * means that some of the code following the loop is a bit more
   1868 	 * complex than otherwise.
   1869 	 */
   1870 	while (n > 0) {
   1871 		int xfer;
   1872 
   1873 		for (;;) {
   1874 			dmastat = inb(DMASTAT);
   1875 			if ((dmastat & DFIFOEMP) != 0)
   1876 				break;
   1877 			if ((dmastat & INTSTAT) != 0)
   1878 				goto phasechange;
   1879 		}
   1880 
   1881 		xfer = min(DOUTAMOUNT, n);
   1882 
   1883 		AIC_MISC(("%d> ", xfer));
   1884 
   1885 		n -= xfer;
   1886 		out += xfer;
   1887 
   1888 #if AIC_USE_DWORDS
   1889 		if (xfer >= 12) {
   1890 			outsl(DMADATALONG, p, xfer>>2);
   1891 			p += xfer & ~3;
   1892 			xfer &= 3;
   1893 		}
   1894 #else
   1895 		if (xfer >= 8) {
   1896 			outsw(DMADATA, p, xfer>>1);
   1897 			p += xfer & ~1;
   1898 			xfer &= 1;
   1899 		}
   1900 #endif
   1901 
   1902 		if (xfer > 0) {
   1903 			outb(DMACNTRL0, ENDMA|B8MODE|WRITE);
   1904 			outsb(DMADATA, p, xfer);
   1905 			p += xfer;
   1906 			outb(DMACNTRL0, ENDMA|DWORDPIO|WRITE);
   1907 		}
   1908 	}
   1909 
   1910 	if (out == 0) {
   1911 		outb(SXFRCTL1, BITBUCKET);
   1912 		for (;;) {
   1913 			if ((inb(DMASTAT) & INTSTAT) != 0)
   1914 				break;
   1915 		}
   1916 		outb(SXFRCTL1, 0);
   1917 		AIC_MISC(("extra data  "));
   1918 	} else {
   1919 		/* See the bytes off chip */
   1920 		for (;;) {
   1921 			dmastat = inb(DMASTAT);
   1922 			if ((dmastat & DFIFOEMP) != 0 &&
   1923 			    (inb(SSTAT2) & SEMPTY) != 0)
   1924 				break;
   1925 			if ((dmastat & INTSTAT) != 0)
   1926 				goto phasechange;
   1927 		}
   1928 	}
   1929 
   1930 phasechange:
   1931 	/* Stop the FIFO data path. */
   1932 	outb(SXFRCTL0, CHEN);
   1933 	while ((inb(SXFRCTL0) & SCSIEN) != 0)
   1934 		;
   1935 
   1936 	if ((dmastat & INTSTAT) != 0) {
   1937 		/* Some sort of phase change. */
   1938 		int amount;
   1939 
   1940 		/* Stop transfers, do some accounting */
   1941 		amount = inb(FIFOSTAT) + (inb(SSTAT2) & 15);
   1942 		if (amount > 0) {
   1943 			out -= amount;
   1944 			outb(SXFRCTL0, CHEN|CLRSTCNT|CLRCH);
   1945 			AIC_MISC(("+%d ", amount));
   1946 		}
   1947 	}
   1948 
   1949 	/* Turn on ENREQINIT again. */
   1950 	outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENREQINIT|ENPHASECHG);
   1951 
   1952 	return out;
   1953 }
   1954 
   1955 /* aic_datain_pio: perform data transfers using the FIFO datapath in the aic6360
   1957  * Precondition: The SCSI bus should be in the DIN phase, with REQ asserted
   1958  * and ACK deasserted (i.e. at least one byte is ready).
   1959  * For now, uses a pretty dumb algorithm, hangs around until all data has been
   1960  * transferred.  This, is OK for fast targets, but not so smart for slow
   1961  * targets which don't disconnect or for huge transfers.
   1962  */
   1963 int
   1964 aic_datain_pio(sc, p, n)
   1965 	register struct aic_softc *sc;
   1966 	u_char *p;
   1967 	int n;
   1968 {
   1969 	register u_char dmastat;
   1970 	int in = 0;
   1971 #define DINAMOUNT 128		/* Full FIFO */
   1972 
   1973 	/* Clear host FIFO and counter. */
   1974 	outb(DMACNTRL0, RSTFIFO);
   1975 	/* Enable FIFOs */
   1976 	outb(SXFRCTL0, SCSIEN|DMAEN|CHEN);
   1977 	outb(DMACNTRL0, ENDMA|DWORDPIO);
   1978 
   1979 	/* Turn off ENREQINIT for now. */
   1980 	outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENPHASECHG);
   1981 
   1982 	/* We leave this loop if one or more of the following is true:
   1983 	 * a) phase != PH_DATAIN && FIFOs are empty
   1984 	 * b) SCSIRSTI is set (a reset has occurred) or busfree is detected.
   1985 	 */
   1986 	while (n > 0) {
   1987 		int xfer;
   1988 
   1989 		/* Wait for fifo half full or phase mismatch */
   1990 		for (;;) {
   1991 			dmastat = inb(DMASTAT);
   1992 			if ((dmastat & (DFIFOFULL|INTSTAT)) != 0)
   1993 				break;
   1994 		}
   1995 
   1996 		if ((dmastat & DFIFOFULL) != 0)
   1997 			xfer = min(DINAMOUNT, n);
   1998 		else
   1999 			xfer = min(inb(FIFOSTAT), n);
   2000 
   2001 		AIC_MISC((">%d ", xfer));
   2002 
   2003 		n -= xfer;
   2004 		in += xfer;
   2005 
   2006 #if AIC_USE_DWORDS
   2007 		if (xfer >= 12) {
   2008 			insl(DMADATALONG, p, xfer>>2);
   2009 			p += xfer & ~3;
   2010 			xfer &= 3;
   2011 		}
   2012 #else
   2013 		if (xfer >= 8) {
   2014 			insw(DMADATA, p, xfer>>1);
   2015 			p += xfer & ~1;
   2016 			xfer &= 1;
   2017 		}
   2018 #endif
   2019 
   2020 		if (xfer > 0) {
   2021 			outb(DMACNTRL0, ENDMA|B8MODE);
   2022 			insb(DMADATA, p, xfer);
   2023 			p += xfer;
   2024 			outb(DMACNTRL0, ENDMA|DWORDPIO);
   2025 		}
   2026 
   2027 		if ((dmastat & INTSTAT) != 0)
   2028 			goto phasechange;
   2029 	}
   2030 
   2031 	/* Some SCSI-devices are rude enough to transfer more data than what
   2032 	 * was requested, e.g. 2048 bytes from a CD-ROM instead of the
   2033 	 * requested 512.  Test for progress, i.e. real transfers.  If no real
   2034 	 * transfers have been performed (n is probably already zero) and the
   2035 	 * FIFO is not empty, waste some bytes....
   2036 	 */
   2037 	if (in == 0) {
   2038 		outb(SXFRCTL1, BITBUCKET);
   2039 		for (;;) {
   2040 			if ((inb(DMASTAT) & INTSTAT) != 0)
   2041 				break;
   2042 		}
   2043 		outb(SXFRCTL1, 0);
   2044 		AIC_MISC(("extra data  "));
   2045 	}
   2046 
   2047 phasechange:
   2048 	/* Stop the FIFO data path. */
   2049 	outb(SXFRCTL0, CHEN);
   2050 	while ((inb(SXFRCTL0) & SCSIEN) != 0)
   2051 		;
   2052 
   2053 	/* Turn on ENREQINIT again. */
   2054 	outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENREQINIT|ENPHASECHG);
   2055 
   2056 	return in;
   2057 }
   2058 
   2059 /*
   2061  * This is the workhorse routine of the driver.
   2062  * Deficiencies (for now):
   2063  * 1) always uses programmed I/O
   2064  */
   2065 int
   2066 aicintr(arg)
   2067 	void *arg;
   2068 {
   2069 	register struct aic_softc *sc = arg;
   2070 	u_char sstat0, sstat1;
   2071 	register struct aic_acb *acb;
   2072 	register struct scsi_link *sc_link;
   2073 	struct aic_tinfo *ti;
   2074 	int n;
   2075 
   2076 	/*
   2077 	 * Clear INTEN.  We enable it again before returning.  This makes the
   2078 	 * interrupt esssentially level-triggered.
   2079 	 */
   2080 	outb(DMACNTRL0, 0);
   2081 
   2082 	AIC_TRACE(("aicintr  "));
   2083 
   2084 loop:
   2085 gotintr:
   2086 	/*
   2087 	 * First check for abnormal conditions, such as reset.
   2088 	 */
   2089 	sstat1 = inb(SSTAT1);
   2090 	AIC_MISC(("sstat1:0x%02x ", sstat1));
   2091 
   2092 	if ((sstat1 & SCSIRSTI) != 0) {
   2093 		printf("%s: SCSI bus reset\n", sc->sc_dev.dv_xname);
   2094 		goto reset;
   2095 	}
   2096 
   2097 	/*
   2098 	 * Check for less serious errors.
   2099 	 */
   2100 	if ((sstat1 & SCSIPERR) != 0) {
   2101 		printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname);
   2102 		outb(CLRSINT1, CLRSCSIPERR);
   2103 		if (sc->sc_prevphase == PH_MSGIN) {
   2104 			aic_sched_msgout(SEND_PARITY_ERROR);
   2105 			sc->sc_flags |= AIC_DROP_MSGIN;
   2106 		} else
   2107 			aic_sched_msgout(SEND_INIT_DET_ERR);
   2108 	}
   2109 
   2110 	/*
   2111 	 * If we're not already busy doing something test for the following
   2112 	 * conditions:
   2113 	 * 1) We have been reselected by something
   2114 	 * 2) We have selected something successfully
   2115 	 * 3) Our selection process has timed out
   2116 	 * 4) This is really a bus free interrupt just to get a new command
   2117 	 *    going?
   2118 	 * 5) Spurious interrupt?
   2119 	 */
   2120 	switch (sc->sc_state) {
   2121 	case AIC_IDLE:
   2122 	case AIC_SELECTING:
   2123 		sstat0 = inb(SSTAT0);
   2124 		AIC_MISC(("sstat0:0x%02x ", sstat0));
   2125 
   2126 		if ((sstat0 & TARGET) != 0) {
   2127 			/*
   2128 			 * We don't currently support target mode.
   2129 			 */
   2130 			printf("%s: target mode selected; going to bus free\n",
   2131 			    sc->sc_dev.dv_xname);
   2132 			outb(SCSISIG, 0);
   2133 
   2134 			sc->sc_state = AIC_IDLE;
   2135 			aic_sched(sc);
   2136 			goto out;
   2137 		} else if ((sstat0 & SELDI) != 0) {
   2138 			AIC_MISC(("reselected  "));
   2139 
   2140 			/*
   2141 			 * If we're trying to select a target ourselves,
   2142 			 * push our command back into the ready list.
   2143 			 */
   2144 			if (sc->sc_state == AIC_SELECTING) {
   2145 				AIC_MISC(("backoff selector  "));
   2146 				AIC_ASSERT(sc->sc_nexus != NULL);
   2147 				acb = sc->sc_nexus;
   2148 				sc->sc_nexus = NULL;
   2149 				TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
   2150 			}
   2151 
   2152 			/* Save reselection ID. */
   2153 			sc->sc_selid = inb(SELID);
   2154 
   2155 			sc->sc_state = AIC_RESELECTED;
   2156 		} else if ((sstat0 & SELDO) != 0) {
   2157 			AIC_MISC(("selected  "));
   2158 
   2159 			/* We have selected a target. Things to do:
   2160 			 * a) Determine what message(s) to send.
   2161 			 * b) Verify that we're still selecting the target.
   2162 			 * c) Mark device as busy.
   2163 			 */
   2164 			if (sc->sc_state != AIC_SELECTING) {
   2165 				printf("%s: selection out while idle; resetting\n",
   2166 				    sc->sc_dev.dv_xname);
   2167 				AIC_BREAK();
   2168 				goto reset;
   2169 			}
   2170 			AIC_ASSERT(sc->sc_nexus != NULL);
   2171 			acb = sc->sc_nexus;
   2172 
   2173 			sc_link = acb->xs->sc_link;
   2174 			ti = &sc->sc_tinfo[sc_link->target];
   2175 			if ((acb->xs->flags & SCSI_RESET) == 0) {
   2176 				sc->sc_msgpriq = SEND_IDENTIFY;
   2177 				if (acb->flags != ACB_ABORTED) {
   2178 #if AIC_USE_SYNCHRONOUS
   2179 					if ((ti->flags & DO_SYNC) != 0)
   2180 						sc->sc_msgpriq |= SEND_SDTR;
   2181 #endif
   2182 #if AIC_USE_WIDE
   2183 					if ((ti->flags & DO_WIDE) != 0)
   2184 						sc->sc_msgpriq |= SEND_WDTR;
   2185 #endif
   2186 				} else {
   2187 					sc->sc_flags |= AIC_ABORTING;
   2188 					sc->sc_msgpriq |= SEND_ABORT;
   2189 				}
   2190 			} else
   2191 				sc->sc_msgpriq = SEND_DEV_RESET;
   2192 
   2193 			ti->lubusy |= (1 << sc_link->lun);
   2194 
   2195 			/* Do an implicit RESTORE POINTERS. */
   2196 			sc->sc_dp = acb->data_addr;
   2197 			sc->sc_dleft = acb->data_length;
   2198 			sc->sc_cp = (u_char *)&acb->scsi_cmd;
   2199 			sc->sc_cleft = acb->scsi_cmd_length;
   2200 
   2201 			sc->sc_state = AIC_CONNECTED;
   2202 		} else if ((sstat1 & SELTO) != 0) {
   2203 			AIC_MISC(("selection timeout  "));
   2204 
   2205 			if (sc->sc_state != AIC_SELECTING) {
   2206 				printf("%s: selection timeout while idle; resetting\n",
   2207 				    sc->sc_dev.dv_xname);
   2208 				AIC_BREAK();
   2209 				goto reset;
   2210 			}
   2211 			AIC_ASSERT(sc->sc_nexus != NULL);
   2212 			acb = sc->sc_nexus;
   2213 
   2214 			outb(SXFRCTL1, 0);
   2215 			outb(SCSISEQ, ENRESELI);
   2216 			outb(CLRSINT1, CLRSELTIMO);
   2217 
   2218 			acb->xs->error = XS_SELTIMEOUT;
   2219 			untimeout(aic_timeout, acb);
   2220 			delay(250);
   2221 			aic_done(sc, acb);
   2222 			goto out;
   2223 		} else {
   2224 #ifdef AIC_PICKY
   2225 			if (sc->sc_state != AIC_IDLE) {
   2226 				printf("%s: BUS FREE while not idle; state=%d\n",
   2227 				    sc->sc_dev.dv_xname, sc->sc_state);
   2228 				AIC_BREAK();
   2229 				goto out;
   2230 			}
   2231 #endif
   2232 
   2233 			aic_sched(sc);
   2234 			goto out;
   2235 		}
   2236 
   2237 		/*
   2238 		 * Turn off selection stuff, and prepare to catch bus free
   2239 		 * interrupts, parity errors, and phase changes.
   2240 		 */
   2241 		outb(SXFRCTL1, 0);
   2242 		outb(SCSISEQ, ENAUTOATNP);
   2243 		outb(CLRSINT0, CLRSELDI|CLRSELDO);
   2244 		outb(CLRSINT1, CLRBUSFREE|CLRPHASECHG);
   2245 		outb(SIMODE0, 0);
   2246 		outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENREQINIT|ENPHASECHG);
   2247 
   2248 		sc->sc_flags = 0;
   2249 		sc->sc_prevphase = PH_INVALID;
   2250 		goto dophase;
   2251 	}
   2252 
   2253 	outb(CLRSINT1, CLRPHASECHG);
   2254 
   2255 	if ((sstat1 & BUSFREE) != 0) {
   2256 		/* We've gone to BUS FREE phase. */
   2257 		outb(CLRSINT1, CLRBUSFREE);
   2258 
   2259 		switch (sc->sc_state) {
   2260 		case AIC_RESELECTED:
   2261 			sc->sc_state = AIC_IDLE;
   2262 			aic_sched(sc);
   2263 			break;
   2264 
   2265 		case AIC_CONNECTED:
   2266 			if ((sc->sc_flags & AIC_ABORTING) == 0) {
   2267 				printf("%s: unexpected BUS FREE; aborting\n",
   2268 				    sc->sc_dev.dv_xname);
   2269 				AIC_BREAK();
   2270 			}
   2271 			AIC_ASSERT(sc->sc_nexus != NULL);
   2272 			acb = sc->sc_nexus;
   2273 			acb->xs->error = XS_DRIVER_STUFFUP;
   2274 			goto finish;
   2275 
   2276 		case AIC_DISCONNECT:
   2277 			AIC_ASSERT(sc->sc_nexus != NULL);
   2278 			acb = sc->sc_nexus;
   2279 			sc->sc_state = AIC_IDLE;
   2280 			sc->sc_nexus = NULL;
   2281 			TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
   2282 			aic_sched(sc);
   2283 			break;
   2284 
   2285 		case AIC_CMDCOMPLETE:
   2286 			AIC_ASSERT(sc->sc_nexus != NULL);
   2287 			acb = sc->sc_nexus;
   2288 		finish:
   2289 			untimeout(aic_timeout, acb);
   2290 			aic_done(sc, acb);
   2291 			break;
   2292 		}
   2293 		goto out;
   2294 	}
   2295 
   2296 dophase:
   2297 	if ((sstat1 & REQINIT) == 0) {
   2298 		/* Wait for REQINIT. */
   2299 		goto out;
   2300 	}
   2301 
   2302 	sc->sc_phase = inb(SCSISIG) & PH_MASK;
   2303 	outb(SCSISIG, sc->sc_phase);
   2304 
   2305 	switch (sc->sc_phase) {
   2306 	case PH_MSGOUT:
   2307 		/* If aborting, always handle MESSAGE OUT. */
   2308 		if ((sc->sc_state & AIC_CONNECTED) == 0 &&
   2309 		    (sc->sc_flags & AIC_ABORTING) == 0)
   2310 			break;
   2311 		aic_msgout(sc);
   2312 		sc->sc_prevphase = PH_MSGOUT;
   2313 		goto loop;
   2314 
   2315 	case PH_MSGIN:
   2316 		if ((sc->sc_state & (AIC_CONNECTED|AIC_RESELECTED)) == 0)
   2317 			break;
   2318 		if (aic_msgin(sc)) {
   2319 			sc->sc_prevphase = PH_MSGIN;
   2320 			goto gotintr;
   2321 		}
   2322 		sc->sc_prevphase = PH_MSGIN;
   2323 		goto loop;
   2324 
   2325 	case PH_CMD:
   2326 		if ((sc->sc_state & AIC_CONNECTED) == 0)
   2327 			break;
   2328 #if AIC_DEBUG
   2329 		if ((aic_debug & AIC_SHOWMISC) != 0) {
   2330 			AIC_ASSERT(sc->sc_nexus != NULL);
   2331 			acb = sc->sc_nexus;
   2332 			printf("cmd=0x%02x+%d  ",
   2333 			    acb->scsi_cmd.opcode, acb->scsi_cmd_length-1);
   2334 		}
   2335 #endif
   2336 		n = aic_dataout_pio(sc, sc->sc_cp, sc->sc_cleft);
   2337 		sc->sc_cp += n;
   2338 		sc->sc_cleft -= n;
   2339 		sc->sc_prevphase = PH_CMD;
   2340 		goto loop;
   2341 
   2342 	case PH_DATAOUT:
   2343 		if ((sc->sc_state & AIC_CONNECTED) == 0)
   2344 			break;
   2345 		AIC_MISC(("dataout dleft=%d  ", sc->sc_dleft));
   2346 		n = aic_dataout_pio(sc, sc->sc_dp, sc->sc_dleft);
   2347 		sc->sc_dp += n;
   2348 		sc->sc_dleft -= n;
   2349 		sc->sc_prevphase = PH_DATAOUT;
   2350 		goto loop;
   2351 
   2352 	case PH_DATAIN:
   2353 		if ((sc->sc_state & AIC_CONNECTED) == 0)
   2354 			break;
   2355 		AIC_MISC(("datain  "));
   2356 		n = aic_datain_pio(sc, sc->sc_dp, sc->sc_dleft);
   2357 		sc->sc_dp += n;
   2358 		sc->sc_dleft -= n;
   2359 		sc->sc_prevphase = PH_DATAIN;
   2360 		goto loop;
   2361 
   2362 	case PH_STAT:
   2363 		if ((sc->sc_state & AIC_CONNECTED) == 0)
   2364 			break;
   2365 		AIC_ASSERT(sc->sc_nexus != NULL);
   2366 		acb = sc->sc_nexus;
   2367 		/* XXXX Don't clear FIFO.  Wait for byte to come in. */
   2368 		outb(SXFRCTL0, CHEN|SPIOEN);
   2369 		outb(DMACNTRL0, RSTFIFO);
   2370 		acb->target_stat = inb(SCSIDAT);
   2371 		outb(SXFRCTL0, CHEN);
   2372 		outb(DMACNTRL0, RSTFIFO);
   2373 		while ((inb(SXFRCTL0) & SCSIEN) != 0)
   2374 			;
   2375 		AIC_MISC(("target_stat=0x%02x  ", acb->target_stat));
   2376 		sc->sc_prevphase = PH_STAT;
   2377 		goto loop;
   2378 	}
   2379 
   2380 	printf("%s: unexpected bus phase; resetting\n", sc->sc_dev.dv_xname);
   2381 	AIC_BREAK();
   2382 reset:
   2383 	aic_init(sc);
   2384 	return 1;
   2385 
   2386 out:
   2387 	outb(DMACNTRL0, INTEN);
   2388 	return 1;
   2389 }
   2390 
   2391 void
   2392 aic_abort(sc, acb)
   2393 	struct aic_softc *sc;
   2394 	struct aic_acb *acb;
   2395 {
   2396 
   2397 	if (sc->sc_nexus == acb) {
   2398 		if (sc->sc_state == AIC_CONNECTED) {
   2399 			sc->sc_flags |= AIC_ABORTING;
   2400 			aic_sched_msgout(SEND_ABORT);
   2401 		}
   2402 	} else {
   2403 		aic_dequeue(sc, acb);
   2404 		TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
   2405 		if (sc->sc_state == AIC_IDLE)
   2406 			aic_sched(sc);
   2407 	}
   2408 }
   2409 
   2410 void
   2411 aic_timeout(arg)
   2412 	void *arg;
   2413 {
   2414 	struct aic_acb *acb = arg;
   2415 	struct scsi_xfer *xs = acb->xs;
   2416 	struct scsi_link *sc_link = xs->sc_link;
   2417 	struct aic_softc *sc = sc_link->adapter_softc;
   2418 	int s;
   2419 
   2420 	sc_print_addr(sc_link);
   2421 	printf("timed out");
   2422 
   2423 	s = splbio();
   2424 
   2425 	if (acb->flags == ACB_ABORTED) {
   2426 		/* abort timed out */
   2427 		printf(" AGAIN\n");
   2428 		acb->xs->retries = 0;
   2429 		aic_done(sc, acb);
   2430 	} else {
   2431 		/* abort the operation that has timed out */
   2432 		printf("\n");
   2433 		acb->xs->error = XS_TIMEOUT;
   2434 		acb->flags = ACB_ABORTED;
   2435 		aic_abort(sc, acb);
   2436 		/* 2 secs for the abort */
   2437 		if ((xs->flags & SCSI_POLL) == 0)
   2438 			timeout(aic_timeout, acb, 2 * hz);
   2439 	}
   2440 
   2441 	splx(s);
   2442 }
   2443 
   2444 #ifdef AIC_DEBUG
   2446 /*
   2447  * The following functions are mostly used for debugging purposes, either
   2448  * directly called from the driver or from the kernel debugger.
   2449  */
   2450 
   2451 void
   2452 aic_show_scsi_cmd(acb)
   2453 	struct aic_acb *acb;
   2454 {
   2455 	u_char  *b = (u_char *)&acb->scsi_cmd;
   2456 	struct scsi_link *sc_link = acb->xs->sc_link;
   2457 	int i;
   2458 
   2459 	sc_print_addr(sc_link);
   2460 	if ((acb->xs->flags & SCSI_RESET) == 0) {
   2461 		for (i = 0; i < acb->scsi_cmd_length; i++) {
   2462 			if (i)
   2463 				printf(",");
   2464 			printf("%x", b[i]);
   2465 		}
   2466 		printf("\n");
   2467 	} else
   2468 		printf("RESET\n");
   2469 }
   2470 
   2471 void
   2472 aic_print_acb(acb)
   2473 	struct aic_acb *acb;
   2474 {
   2475 
   2476 	printf("acb@%x xs=%x flags=%x", acb, acb->xs, acb->flags);
   2477 	printf(" dp=%x dleft=%d target_stat=%x\n",
   2478 	    (long)acb->data_addr, acb->data_length, acb->target_stat);
   2479 	aic_show_scsi_cmd(acb);
   2480 }
   2481 
   2482 void
   2483 aic_print_active_acb()
   2484 {
   2485 	struct aic_acb *acb;
   2486 	struct aic_softc *sc = aiccd.cd_devs[0];
   2487 
   2488 	printf("ready list:\n");
   2489 	for (acb = sc->ready_list.tqh_first; acb != NULL;
   2490 	    acb = acb->chain.tqe_next)
   2491 		aic_print_acb(acb);
   2492 	printf("nexus:\n");
   2493 	if (sc->sc_nexus != NULL)
   2494 		aic_print_acb(sc->sc_nexus);
   2495 	printf("nexus list:\n");
   2496 	for (acb = sc->nexus_list.tqh_first; acb != NULL;
   2497 	    acb = acb->chain.tqe_next)
   2498 		aic_print_acb(acb);
   2499 }
   2500 
   2501 void
   2502 aic_dump6360(sc)
   2503 	struct aic_softc *sc;
   2504 {
   2505 
   2506 	printf("aic6360: SCSISEQ=%x SXFRCTL0=%x SXFRCTL1=%x SCSISIG=%x\n",
   2507 	    inb(SCSISEQ), inb(SXFRCTL0), inb(SXFRCTL1), inb(SCSISIG));
   2508 	printf("         SSTAT0=%x SSTAT1=%x SSTAT2=%x SSTAT3=%x SSTAT4=%x\n",
   2509 	    inb(SSTAT0), inb(SSTAT1), inb(SSTAT2), inb(SSTAT3), inb(SSTAT4));
   2510 	printf("         SIMODE0=%x SIMODE1=%x DMACNTRL0=%x DMACNTRL1=%x DMASTAT=%x\n",
   2511 	    inb(SIMODE0), inb(SIMODE1), inb(DMACNTRL0), inb(DMACNTRL1),
   2512 	    inb(DMASTAT));
   2513 	printf("         FIFOSTAT=%d SCSIBUS=0x%x\n",
   2514 	    inb(FIFOSTAT), inb(SCSIBUS));
   2515 }
   2516 
   2517 void
   2518 aic_dump_driver(sc)
   2519 	struct aic_softc *sc;
   2520 {
   2521 	struct aic_tinfo *ti;
   2522 	int i;
   2523 
   2524 	printf("nexus=%x prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
   2525 	printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x currmsg=%x\n",
   2526 	    sc->sc_state, sc->sc_imess[0],
   2527 	    sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
   2528 	for (i = 0; i < 7; i++) {
   2529 		ti = &sc->sc_tinfo[i];
   2530 		printf("tinfo%d: %d cmds %d disconnects %d timeouts",
   2531 		    i, ti->cmds, ti->dconns, ti->touts);
   2532 		printf(" %d senses flags=%x\n", ti->senses, ti->flags);
   2533 	}
   2534 }
   2535 #endif
   2536