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