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