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