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