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