Home | History | Annotate | Line # | Download | only in aic7xxx
aic7xxx.seq revision 1.6
      1 /*	$NetBSD: aic7xxx.seq,v 1.6 1996/10/08 03:04:06 gibbs Exp $	*/
      2 
      3 /*+M***********************************************************************
      4  *Adaptec 274x/284x/294x device driver for Linux and FreeBSD.
      5  *
      6  *Copyright (c) 1994 John Aycock
      7  *  The University of Calgary Department of Computer Science.
      8  *  All rights reserved.
      9  *
     10  *FreeBSD, Twin, Wide, 2 command per target support, tagged queuing,
     11  *SCB paging and other optimizations:
     12  *Copyright (c) 1994, 1995, 1996 Justin Gibbs. All rights reserved.
     13  *
     14  *Redistribution and use in source and binary forms, with or without
     15  *modification, are permitted provided that the following conditions
     16  *are met:
     17  *1. Redistributions of source code must retain the above copyright
     18  *   notice, this list of conditions, and the following disclaimer.
     19  *2. Redistributions in binary form must reproduce the above copyright
     20  *   notice, this list of conditions and the following disclaimer in the
     21  *   documentation and/or other materials provided with the distribution.
     22  *3. All advertising materials mentioning features or use of this software
     23  *   must display the following acknowledgement:
     24  *     This product includes software developed by the University of Calgary
     25  *     Department of Computer Science and its contributors.
     26  *4. Neither the name of the University nor the names of its contributors
     27  *   may be used to endorse or promote products derived from this software
     28  *   without specific prior written permission.
     29  *
     30  *THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     31  *ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     32  *IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33  *ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     34  *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     35  *DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     36  *OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     37  *HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     38  *LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     39  *OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     40  *SUCH DAMAGE.
     41  *
     42  * from	Id: aic7xxx.seq,v 1.42 1996/06/09 17:29:11 gibbs Exp
     43  *
     44  *-M************************************************************************/
     45 
     46 VERSION AIC7XXX_SEQ_VER "$NetBSD: aic7xxx.seq,v 1.6 1996/10/08 03:04:06 gibbs Exp $"
     47 
     48 #if defined(__NetBSD__)
     49 #include "../../ic/aic7xxxreg.h"
     50 #include "../../../scsi/scsi_message.h"
     51 #elif defined(__FreeBSD__)
     52 #include "../../dev/aic7xxx/aic7xxx_reg.h"
     53 #include "../../scsi/scsi_message.h"
     54 #endif
     55 
     56 /*
     57  * We can't just use ACCUM in the sequencer code because it
     58  * must be treated specially by the assembler, and it currently
     59  * looks for the symbol 'A'.  This is the only register defined in
     60  * the assembler's symbol space.
     61  */
     62 A = ACCUM
     63 
     64 /* After starting the selection hardware, we check for reconnecting targets
     65  * as well as for our selection to complete just in case the reselection wins
     66  * bus arbitration.  The problem with this is that we must keep track of the
     67  * SCB that we've already pulled from the QINFIFO and started the selection
     68  * on just in case the reselection wins so that we can retry the selection at
     69  * a later time.  This problem cannot be resolved by holding a single entry
     70  * in scratch ram since a reconnecting target can request sense and this will
     71  * create yet another SCB waiting for selection.  The solution used here is to
     72  * use byte 27 of the SCB as a psuedo-next pointer and to thread a list
     73  * of SCBs that are awaiting selection.  Since 0-0xfe are valid SCB offsets,
     74  * SCB_LIST_NULL is 0xff which is out of range.  The kernel driver must
     75  * add an entry to this list everytime a request sense occurs.  The sequencer
     76  * will automatically consume the entries.
     77  */
     78 
     79 /*
     80  * We assume that the kernel driver may reset us at any time, even in the
     81  * middle of a DMA, so clear DFCNTRL too.
     82  */
     83 reset:
     84 	clr	DFCNTRL
     85 	clr	SCSISIGO		/* De-assert BSY */
     86 /*
     87  * We jump to start after every bus free.
     88  */
     89 start:
     90 	and	FLAGS,0x0f		/* clear target specific flags */
     91 	mvi	SCSISEQ,ENRSELI		/* Always allow reselection */
     92 	clr	SCSIRATE		/*
     93 					 * We don't know the target we will
     94 					 * connect to, so default to narrow
     95 					 * transfers to avoid parity problems.
     96 					 */
     97 poll_for_work:
     98 	/*
     99 	 * Are we a twin channel device?
    100 	 * For fairness, we check the other bus first,
    101 	 * since we just finished a transaction on the
    102 	 * current channel.
    103 	 */
    104 	test	FLAGS,TWIN_BUS	jz start2
    105 	xor	SBLKCTL,SELBUSB			/* Toggle to the other bus */
    106 	test	SSTAT0,SELDI	jnz reselect
    107 	xor	SBLKCTL,SELBUSB			/* Toggle to the original bus */
    108 start2:
    109 	test	SSTAT0,SELDI	jnz reselect
    110 	cmp	WAITING_SCBH,SCB_LIST_NULL jne start_waiting
    111 	mov	A, QCNTMASK
    112 	test	QINCNT,A	jz poll_for_work
    113 
    114 /*
    115  * We have at least one queued SCB now and we don't have any
    116  * SCBs in the list of SCBs awaiting selection.  Set the SCB
    117  * pointer from the FIFO so we see the right bank of SCB
    118  * registers.
    119  */
    120 	mov	SCBPTR,QINFIFO
    121 
    122 /*
    123  * See if there is not already an active SCB for this target.  This code
    124  * locks out on a per target basis instead of target/lun.  Although this
    125  * is not ideal for devices that have multiple luns active at the same
    126  * time, it is faster than looping through all SCB's looking for active
    127  * commands.  It may be benificial to make findscb a more general procedure
    128  * to see if the added cost of the search is negligible.  This code also
    129  * assumes that the kernel driver will clear the active flags on board
    130  * initialization, board reset, and a target SELTO.  Tagged commands
    131  * don't set the active bits since you can queue more than one command
    132  * at a time.  We do, however, look to see if there are any non-tagged
    133  * I/Os in progress, and requeue the command if there are.  Tagged and
    134  * non-tagged commands cannot be mixed to a single target.
    135  */
    136 
    137 test_busy:
    138 	mov	FUNCTION1,SCB_TCL
    139 	mov	A,FUNCTION1
    140 	test	SCB_TCL,0x88	jz test_a	/* Id < 8 && A channel */
    141 
    142 	test	ACTIVE_B,A	jnz requeue
    143 	test	SCB_CONTROL,TAG_ENB	jnz start_scb
    144 	/* Mark the current target as busy */
    145 	or	ACTIVE_B,A
    146 	jmp	start_scb
    147 
    148 /* Place the currently active SCB back on the queue for later processing */
    149 requeue:
    150 	mov	QINFIFO, SCBPTR
    151 	jmp	poll_for_work
    152 
    153 /*
    154  * Pull the first entry off of the waiting for selection list
    155  * We don't have to "test_busy" because only transactions that
    156  * have passed that test can be in the waiting_scb list.
    157  */
    158 start_waiting:
    159 	mov	SCBPTR,WAITING_SCBH
    160 	jmp	start_scb2
    161 
    162 test_a:
    163 	test	ACTIVE_A,A jnz requeue
    164 	test	SCB_CONTROL,TAG_ENB jnz start_scb
    165 	/* Mark the current target as busy */
    166 	or	ACTIVE_A,A
    167 
    168 start_scb:
    169 	mov	SCB_NEXT,WAITING_SCBH
    170 	mov	WAITING_SCBH, SCBPTR
    171 start_scb2:
    172 	and	SINDEX,0xf7,SBLKCTL	/* Clear the channel select bit */
    173 	and	A,0x08,SCB_TCL		/* Get new channel bit */
    174 	or	SINDEX,A
    175 	mov	SBLKCTL,SINDEX		/* select channel */
    176 	mov	SCB_TCL	call initialize_scsiid
    177 
    178 /*
    179  * Enable selection phase as an initiator, and do automatic ATN
    180  * after the selection.  We do this now so that we can overlap the
    181  * rest of our work to set up this target with the arbitration and
    182  * selection bus phases.
    183  */
    184 start_selection:
    185 	mvi	SCSISEQ,0x58		/* ENSELO|ENAUTOATNO|ENRSELI */
    186 
    187 /*
    188  * As soon as we get a successful selection, the target should go
    189  * into the message out phase since we have ATN asserted.  Prepare
    190  * the message to send.
    191  *
    192  * Messages are stored in scratch RAM starting with a length byte
    193  * followed by the message itself.
    194  */
    195 
    196 mk_identify:
    197 	and	A,DISCENB,SCB_CONTROL	/* mask off disconnect privledge */
    198 
    199 	and	MSG0,0x7,SCB_TCL	/* lun */
    200 	or	MSG0,A			/* or in disconnect privledge */
    201 	or	MSG0,MSG_IDENTIFYFLAG
    202 	mvi	MSG_LEN, 1
    203 
    204 /*
    205  * Send a tag message if TAG_ENB is set in the SCB control block.
    206  * Use SCB_TAG (the position in the kernel's SCB array) as the tag value.
    207  */
    208 mk_tag:
    209 	test	SCB_CONTROL,TAG_ENB jz  mk_message
    210 	mvi	DINDEX, MSG1
    211 	and	DINDIR,0x23,SCB_CONTROL
    212 	mov	DINDIR,SCB_TAG
    213 
    214 	add	MSG_LEN,COMP_MSG0,DINDEX	/* update message length */
    215 
    216 /*
    217  * Interrupt the driver, and allow it to tweak the message buffer
    218  * if it asks.
    219  */
    220 mk_message:
    221 	test	SCB_CONTROL,MK_MESSAGE  jz wait_for_selection
    222 
    223 	mvi     INTSTAT,AWAITING_MSG
    224 
    225 wait_for_selection:
    226 	test	SSTAT0,SELDO	jnz select
    227 	test	SSTAT0,SELDI	jz wait_for_selection
    228 
    229 /*
    230  * Reselection has been initiated by a target. Make a note that we've been
    231  * reselected, but haven't seen an IDENTIFY message from the target
    232  * yet.
    233  */
    234 reselect:
    235 	clr	MSG_LEN		/* Don't have anything in the mesg buffer */
    236 	mov	SELID		call initialize_scsiid
    237 	or	FLAGS,RESELECTED
    238 	jmp	select2
    239 
    240 /*
    241  * After the selection, remove this SCB from the "waiting for selection"
    242  * list.  This is achieved by simply moving our "next" pointer into
    243  * WAITING_SCBH.  Our next pointer will be set to null the next time this
    244  * SCB is used, so don't bother with it now.
    245  */
    246 select:
    247 	mov	WAITING_SCBH,SCB_NEXT
    248 	or	FLAGS,SELECTED
    249 select2:
    250 /*
    251  * Set CLRCHN here before the target has entered a data transfer mode -
    252  * with synchronous SCSI, if you do it later, you blow away some
    253  * data in the SCSI FIFO that the target has already sent to you.
    254  */
    255 	or	SXFRCTL0,CLRCHN
    256 /*
    257  * Initialize SCSIRATE with the appropriate value for this target.
    258  */
    259 	call	ndx_dtr
    260 	mov	SCSIRATE,SINDIR
    261 
    262 /*
    263  * Initialize Ultra mode setting.
    264  */
    265 	mov	FUNCTION1,SCSIID
    266 	mov	A,FUNCTION1
    267 	and	SINDEX,0xdf,SXFRCTL0		/* default to Ultra disabled */
    268 	test	SCSIID, 0x80	 jnz ultra_b	/* Target ID > 7 */
    269 	test	SBLKCTL, SELBUSB jnz ultra_b	/* Second channel device */
    270 	test	ULTRA_ENB,A	 jz  set_sxfrctl0
    271 	or	SINDEX, ULTRAEN  jmp set_sxfrctl0
    272 ultra_b:
    273 	test	ULTRA_ENB_B,A	 jz  set_sxfrctl0
    274 	or	SINDEX, ULTRAEN
    275 
    276 set_sxfrctl0:
    277 	mov	SXFRCTL0,SINDEX
    278 
    279 	mvi	SCSISEQ,ENAUTOATNP		/*
    280 						 * ATN on parity errors
    281 						 * for "in" phases
    282 						 */
    283 	mvi	CLRSINT1,CLRBUSFREE
    284 	mvi	CLRSINT0,0x60			/* CLRSELDI|CLRSELDO */
    285 /*
    286  * Main loop for information transfer phases.  If BSY is false, then
    287  * we have a bus free condition, expected or not.  Otherwise, wait
    288  * for the target to assert REQ before checking MSG, C/D and I/O
    289  * for the bus phase.
    290  *
    291  */
    292 ITloop:
    293 	test	SSTAT1,BUSFREE	jnz p_busfree
    294 	test	SSTAT1,REQINIT	jz ITloop
    295 
    296 	and	A,PHASE_MASK,SCSISIGI
    297 	mov	LASTPHASE,A
    298 	mov	SCSISIGO,A
    299 
    300 	cmp	ALLZEROS,A	je p_dataout
    301 	cmp	A,P_DATAIN	je p_datain
    302 	cmp	A,P_COMMAND	je p_command
    303 	cmp	A,P_MESGOUT	je p_mesgout
    304 	cmp	A,P_STATUS	je p_status
    305 	cmp	A,P_MESGIN	je p_mesgin
    306 
    307 	mvi	INTSTAT,BAD_PHASE	/* unknown phase - signal driver */
    308 	jmp	ITloop			/* Try reading the bus again. */
    309 
    310 p_dataout:
    311 	mvi	DMAPARAMS,0x7d			/*
    312 						 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
    313 						 * DIRECTION|FIFORESET
    314 						 */
    315 	jmp	data_phase_init
    316 
    317 /*
    318  * If we re-enter the data phase after going through another phase, the
    319  * STCNT may have been cleared, so restore it from the residual field.
    320  */
    321 data_phase_reinit:
    322 	mov	STCNT0,SCB_RESID_DCNT0
    323 	mov	STCNT1,SCB_RESID_DCNT1
    324 	mov	STCNT2,SCB_RESID_DCNT2
    325 	jmp	data_phase_loop
    326 
    327 p_datain:
    328 	mvi	DMAPARAMS,0x79		/*
    329 					 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
    330 					 * !DIRECTION|FIFORESET
    331 					 */
    332 data_phase_init:
    333 	call	assert
    334 
    335 	test	FLAGS, DPHASE	jnz data_phase_reinit
    336 	call	sg_scb2ram
    337 	or	FLAGS, DPHASE		/* We have seen a data phase */
    338 
    339 data_phase_loop:
    340 /* Guard against overruns */
    341 	test	SG_COUNT, 0xff jnz data_phase_inbounds
    342 /*
    343  * Turn on 'Bit Bucket' mode, set the transfer count to
    344  * 16meg and let the target run until it changes phase.
    345  * When the transfer completes, notify the host that we
    346  * had an overrun.
    347  */
    348 	or	SXFRCTL1,BITBUCKET
    349 	mvi	STCNT0,0xff
    350 	mvi	STCNT1,0xff
    351 	mvi	STCNT2,0xff
    352 
    353 data_phase_inbounds:
    354 /* If we are the last SG block, don't set wideodd. */
    355 	cmp	SG_COUNT,0x01 jne data_phase_wideodd
    356 	and	DMAPARAMS, 0xbf		/* Turn off WIDEODD */
    357 data_phase_wideodd:
    358 	mov	DMAPARAMS  call dma
    359 
    360 /* Go tell the host about any overruns */
    361 	test	SXFRCTL1,BITBUCKET jnz data_phase_overrun
    362 
    363 /* Exit if we had an underrun */
    364 	test	SSTAT0,SDONE	jz data_phase_finish /* underrun STCNT != 0 */
    365 
    366 /*
    367  * Advance the scatter-gather pointers if needed
    368  */
    369 sg_advance:
    370 	dec	SG_COUNT	/* one less segment to go */
    371 
    372 	test	SG_COUNT, 0xff	jz data_phase_finish /* Are we done? */
    373 
    374 	clr	A			/* add sizeof(struct scatter) */
    375 	add	SG_NEXT0,SG_SIZEOF,SG_NEXT0
    376 	adc	SG_NEXT1,A,SG_NEXT1
    377 
    378 /*
    379  * Load a struct scatter and set up the data address and length.
    380  * If the working value of the SG count is nonzero, then
    381  * we need to load a new set of values.
    382  *
    383  * This, like all DMA's, assumes little-endian host data storage.
    384  */
    385 sg_load:
    386 	clr	HCNT2
    387 	clr	HCNT1
    388 	mvi	HCNT0,SG_SIZEOF
    389 
    390 	mov	HADDR0,SG_NEXT0
    391 	mov	HADDR1,SG_NEXT1
    392 	mov	HADDR2,SG_NEXT2
    393 	mov	HADDR3,SG_NEXT3
    394 
    395 	or	DFCNTRL,0xd			/* HDMAEN|DIRECTION|FIFORESET */
    396 
    397 /*
    398  * Wait for DMA from host memory to data FIFO to complete, then disable
    399  * DMA and wait for it to acknowledge that it's off.
    400  */
    401 dma_finish:
    402 	test	DFSTATUS,HDONE	jz dma_finish
    403 	/* Turn off DMA preserving WIDEODD */
    404 	and	DFCNTRL,WIDEODD
    405 dma_finish2:
    406 	test	DFCNTRL,HDMAENACK jnz dma_finish2
    407 
    408 /*
    409  * Copy data from FIFO into SCB data pointer and data count.  This assumes
    410  * that the struct scatterlist has this structure (this and sizeof(struct
    411  * scatterlist) == 12 are asserted in aic7xxx.c for the Linux driver):
    412  *
    413  *	struct scatterlist {
    414  *		char *address;		four bytes, little-endian order
    415  *		...			four bytes, ignored
    416  *		unsigned short length;	two bytes, little-endian order
    417  *	}
    418  *
    419  *
    420  * In FreeBSD, the scatter list entry is only 8 bytes.
    421  *
    422  * struct ahc_dma_seg {
    423  *       physaddr addr;                  four bytes, little-endian order
    424  *       long    len;                    four bytes, little endian order
    425  * };
    426  */
    427 
    428 	mov	HADDR0,DFDAT
    429 	mov	HADDR1,DFDAT
    430 	mov	HADDR2,DFDAT
    431 	mov	HADDR3,DFDAT
    432 /*
    433  * For Linux, we must throw away four bytes since there is a 32bit gap
    434  * in the middle of a struct scatterlist.
    435  */
    436 #ifdef __linux__
    437 	mov	NONE,DFDAT
    438 	mov	NONE,DFDAT
    439 	mov	NONE,DFDAT
    440 	mov	NONE,DFDAT
    441 #endif
    442 	mov	HCNT0,DFDAT
    443 	mov	HCNT1,DFDAT
    444 	mov	HCNT2,DFDAT
    445 
    446 /* Load STCNT as well.  It is a mirror of HCNT */
    447 	mov	STCNT0,HCNT0
    448 	mov	STCNT1,HCNT1
    449 	mov	STCNT2,HCNT2
    450         test    SSTAT1,PHASEMIS  jz data_phase_loop
    451 
    452 data_phase_finish:
    453 /*
    454  * After a DMA finishes, save the SG and STCNT residuals back into the SCB
    455  * We use STCNT instead of HCNT, since it's a reflection of how many bytes
    456  * were transferred on the SCSI (as opposed to the host) bus.
    457  */
    458 	mov	SCB_RESID_DCNT0,STCNT0
    459 	mov	SCB_RESID_DCNT1,STCNT1
    460 	mov	SCB_RESID_DCNT2,STCNT2
    461 	mov	SCB_RESID_SGCNT, SG_COUNT
    462 	jmp	ITloop
    463 
    464 data_phase_overrun:
    465 /*
    466  * Turn off BITBUCKET mode and notify the host
    467  */
    468 	and	SXFRCTL1,0x7f		/* ~BITBUCKET */
    469 	mvi	INTSTAT,DATA_OVERRUN
    470 	jmp	ITloop
    471 
    472 /*
    473  * Command phase.  Set up the DMA registers and let 'er rip.
    474  */
    475 p_command:
    476 	call	assert
    477 
    478 /*
    479  * Load HADDR and HCNT.
    480  */
    481 	mov	HADDR0, SCB_CMDPTR0
    482 	mov	HADDR1, SCB_CMDPTR1
    483 	mov	HADDR2, SCB_CMDPTR2
    484 	mov	HADDR3, SCB_CMDPTR3
    485 	mov	HCNT0, SCB_CMDLEN
    486 	clr	HCNT1
    487 	clr	HCNT2
    488 
    489 	mov	STCNT0, HCNT0
    490 	mov	STCNT1, HCNT1
    491 	mov	STCNT2, HCNT2
    492 
    493 	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
    494 						#   DIRECTION|FIFORESET
    495 	jmp	ITloop
    496 
    497 /*
    498  * Status phase.  Wait for the data byte to appear, then read it
    499  * and store it into the SCB.
    500  */
    501 p_status:
    502 	mvi	SCB_TARGET_STATUS	call inb_first
    503 	jmp	mesgin_done
    504 
    505 /*
    506  * Message out phase.  If there is not an active message, but the target
    507  * took us into this phase anyway, build a no-op message and send it.
    508  */
    509 p_mesgout:
    510 	test	MSG_LEN, 0xff	jnz  p_mesgout_start
    511 	mvi	MSG_NOOP	call mk_mesg	/* build NOP message */
    512 
    513 p_mesgout_start:
    514 /*
    515  * Set up automatic PIO transfer from MSG0.  Bit 3 in
    516  * SXFRCTL0 (SPIOEN) is already on.
    517  */
    518 	mvi	SINDEX,MSG0
    519 	mov	DINDEX,MSG_LEN
    520 
    521 /*
    522  * When target asks for a byte, drop ATN if it's the last one in
    523  * the message.  Otherwise, keep going until the message is exhausted.
    524  *
    525  * Keep an eye out for a phase change, in case the target issues
    526  * a MESSAGE REJECT.
    527  */
    528 p_mesgout_loop:
    529 	test	SSTAT1,PHASEMIS	jnz p_mesgout_phasemis
    530 	test	SSTAT0,SPIORDY	jz p_mesgout_loop
    531 	test	SSTAT1,PHASEMIS	jnz p_mesgout_phasemis
    532 	cmp	DINDEX,1	jne p_mesgout_outb	/* last byte? */
    533 	mvi	CLRSINT1,CLRATNO			/* drop ATN */
    534 p_mesgout_outb:
    535 	dec	DINDEX
    536 	or	CLRSINT0, CLRSPIORDY
    537 	mov	SCSIDATL,SINDIR
    538 
    539 p_mesgout4:
    540 	test	DINDEX,0xff	jnz p_mesgout_loop
    541 
    542 /*
    543  * If the next bus phase after ATN drops is a message out, it means
    544  * that the target is requesting that the last message(s) be resent.
    545  */
    546 p_mesgout_snoop:
    547 	test	SSTAT1,BUSFREE	jnz p_mesgout_done
    548 	test	SSTAT1,REQINIT	jz p_mesgout_snoop
    549 
    550 	test	SSTAT1,PHASEMIS	jnz p_mesgout_done
    551 
    552 	or	SCSISIGO,ATNO			/* turn on ATNO */
    553 
    554 	jmp	ITloop
    555 
    556 p_mesgout_phasemis:
    557 	mvi	CLRSINT1,CLRATNO	/* Be sure to turn ATNO off */
    558 p_mesgout_done:
    559 	clr	MSG_LEN			/* no active msg */
    560 	jmp	ITloop
    561 
    562 /*
    563  * Message in phase.  Bytes are read using Automatic PIO mode.
    564  */
    565 p_mesgin:
    566 	mvi	A		call inb_first	/* read the 1st message byte */
    567 	mov	REJBYTE,A			/* save it for the driver */
    568 
    569 	test	A,MSG_IDENTIFYFLAG	jnz mesgin_identify
    570 	cmp	A,MSG_DISCONNECT	je mesgin_disconnect
    571 	cmp	A,MSG_SAVEDATAPOINTER	je mesgin_sdptrs
    572 	cmp	ALLZEROS,A		je mesgin_complete
    573 	cmp	A,MSG_RESTOREPOINTERS	je mesgin_rdptrs
    574 	cmp	A,MSG_EXTENDED		je mesgin_extended
    575 	cmp	A,MSG_MESSAGE_REJECT	je mesgin_reject
    576 
    577 rej_mesgin:
    578 /*
    579  * We have no idea what this message in is, and there's no way
    580  * to pass it up to the kernel, so we issue a message reject and
    581  * hope for the best.  Since we're now using manual PIO mode to
    582  * read in the message, there should no longer be a race condition
    583  * present when we assert ATN.  In any case, rejection should be a
    584  * rare occurrence - signal the driver when it happens.
    585  */
    586 	or	SCSISIGO,ATNO			/* turn on ATNO */
    587 	mvi	INTSTAT,SEND_REJECT		/* let driver know */
    588 
    589 	mvi	MSG_MESSAGE_REJECT	call mk_mesg
    590 
    591 mesgin_done:
    592 	call	inb_last			/*ack & turn auto PIO back on*/
    593 	jmp	ITloop
    594 
    595 
    596 mesgin_complete:
    597 /*
    598  * We got a "command complete" message, so put the SCB_TAG into QUEUEOUT,
    599  * and trigger a completion interrupt.  Check status for non zero return
    600  * and interrupt driver if needed.  This allows the driver to interpret
    601  * errors only when they occur instead of always uploading the scb.  If
    602  * the status is SCSI_CHECK, the driver will download a new scb requesting
    603  * sense to replace the old one, modify the "waiting for selection" SCB list
    604  * and set RETURN_1 to SEND_SENSE.  If RETURN_1 is set to SEND_SENSE the
    605  * sequencer imediately jumps to main loop where it will run down the waiting
    606  * SCB list and process the sense request.  If the kernel driver does not
    607  * wish to request sense, it need only clear RETURN_1, and the command is
    608  * allowed to complete.  We don't bother to post to the QOUTFIFO in the
    609  * error case since it would require extra work in the kernel driver to
    610  * ensure that the entry was removed before the command complete code tried
    611  * processing it.
    612  *
    613  * First check for residuals
    614  */
    615 	test	SCB_RESID_SGCNT,0xff	jz check_status
    616 /*
    617  * If we have a residual count, interrupt and tell the host.  Other
    618  * alternatives are to pause the sequencer on all command completes (yuck),
    619  * dma the resid directly to the host (slick, we may have space to do it now)
    620  * or have the sequencer pause itself when it encounters a non-zero resid
    621  * (unecessary pause just to flag the command -yuck-, but takes one instruction
    622  * and since it shouldn't happen that often is good enough for our purposes).
    623  */
    624 resid:
    625 	mvi	INTSTAT,RESIDUAL
    626 
    627 check_status:
    628 	test	SCB_TARGET_STATUS,0xff	jz status_ok	/* Good Status? */
    629 	mvi	INTSTAT,BAD_STATUS			/* let driver know */
    630 	cmp	RETURN_1, SEND_SENSE	jne status_ok
    631 	jmp	mesgin_done
    632 
    633 status_ok:
    634 /* First, mark this target as free. */
    635 	test	SCB_CONTROL,TAG_ENB jnz test_immediate	/*
    636 							 * Tagged commands
    637 							 * don't busy the
    638 							 * target.
    639 							 */
    640 	mov	FUNCTION1,SCB_TCL
    641 	mov	A,FUNCTION1
    642 	test	SCB_TCL,0x88 jz clear_a
    643 	xor	ACTIVE_B,A
    644 	jmp	test_immediate
    645 
    646 clear_a:
    647 	xor	ACTIVE_A,A
    648 
    649 test_immediate:
    650 	test    SCB_CMDLEN,0xff jnz complete  /* Immediate message complete */
    651 /*
    652  * Pause the sequencer until the driver gets around to handling the command
    653  * complete.  This is so that any action that might require carefull timing
    654  * with the completion of this command can occur.
    655  */
    656 	mvi	INTSTAT,IMMEDDONE
    657 	jmp	start
    658 complete:
    659 	mov	QOUTFIFO,SCB_TAG
    660 	mvi	INTSTAT,CMDCMPLT
    661 	jmp	mesgin_done
    662 
    663 
    664 /*
    665  * Is it an extended message?  Copy the message to our message buffer and
    666  * notify the host.  The host will tell us whether to reject this message,
    667  * respond to it with the message that the host placed in our message buffer,
    668  * or simply to do nothing.
    669  */
    670 mesgin_extended:
    671 	mvi	MSGIN_EXT_LEN	 call inb_next
    672 	mvi	MSGIN_EXT_OPCODE call inb_next
    673 	mov	A, MSGIN_EXT_LEN
    674 	dec	A	/* Length counts the op code */
    675 	mvi	SINDEX, MSGIN_EXT_BYTE0
    676 mesgin_extended_loop:
    677 	test	A, 0xFF		jz mesgin_extended_intr
    678 	cmp	SINDEX, MSGIN_EXT_LASTBYTE je mesgin_extended_dump
    679 	call	inb_next
    680 	dec	A
    681 /*
    682  * We pass the arg to inb in SINDEX, but DINDEX is the one incremented
    683  * so update SINDEX with DINDEX's value before looping again.
    684  */
    685 	mov	DINDEX		jmp mesgin_extended_loop
    686 mesgin_extended_dump:
    687 /* We have no more storage space, so dump the rest */
    688 	test	A, 0xFF		jz mesgin_extended_intr
    689 	mvi	NONE		call inb_next
    690 	dec	A
    691 	jmp	mesgin_extended_dump
    692 mesgin_extended_intr:
    693 	mvi	INTSTAT,EXTENDED_MSG		/* let driver know */
    694 	cmp	RETURN_1,SEND_REJ je rej_mesgin
    695 	cmp	RETURN_1,SEND_MSG jne mesgin_done
    696 /* The kernel has setup a message to be sent */
    697 	or	SCSISIGO,ATNO			/* turn on ATNO */
    698 	jmp	mesgin_done
    699 
    700 /*
    701  * Is it a disconnect message?  Set a flag in the SCB to remind us
    702  * and await the bus going free.
    703  */
    704 mesgin_disconnect:
    705 	or	SCB_CONTROL,DISCONNECTED
    706 	test	FLAGS, PAGESCBS jz mesgin_done
    707 /*
    708  * Link this SCB into the DISCONNECTED list.  This list holds the
    709  * candidates for paging out an SCB if one is needed for a new command.
    710  * Modifying the disconnected list is a critical(pause dissabled) section.
    711  */
    712 	mvi	SCB_PREV, SCB_LIST_NULL
    713 	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
    714 	mov	SCB_NEXT, DISCONNECTED_SCBH
    715 	mov	DISCONNECTED_SCBH, SCBPTR
    716 	cmp	SCB_NEXT,SCB_LIST_NULL je linkdone
    717 	mov	SCBPTR,SCB_NEXT
    718 	mov	SCB_PREV,DISCONNECTED_SCBH
    719 	mov	SCBPTR,DISCONNECTED_SCBH
    720 linkdone:
    721 	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
    722 	jmp	mesgin_done
    723 
    724 /*
    725  * Save data pointers message?  Copy working values into the SCB,
    726  * usually in preparation for a disconnect.
    727  */
    728 mesgin_sdptrs:
    729 	call	sg_ram2scb
    730 	jmp	mesgin_done
    731 
    732 /*
    733  * Restore pointers message?  Data pointers are recopied from the
    734  * SCB anytime we enter a data phase for the first time, so all
    735  * we need to do is clear the DPHASE flag and let the data phase
    736  * code do the rest.
    737  */
    738 mesgin_rdptrs:
    739 	and	FLAGS,0xef			/*
    740 						 * !DPHASE we'll reload them
    741 						 * the next time through
    742 						 */
    743 	jmp	mesgin_done
    744 
    745 /*
    746  * Identify message?  For a reconnecting target, this tells us the lun
    747  * that the reconnection is for - find the correct SCB and switch to it,
    748  * clearing the "disconnected" bit so we don't "find" it by accident later.
    749  */
    750 mesgin_identify:
    751 	test	A,0x78	jnz rej_mesgin	/*!DiscPriv|!LUNTAR|!Reserved*/
    752 
    753 	and	A,0x07			/* lun in lower three bits */
    754 	or      SAVED_TCL,A,SELID
    755 	and     SAVED_TCL,0xf7
    756 	and     A,SELBUSB,SBLKCTL	/* B Channel?? */
    757 	or      SAVED_TCL,A
    758 	call	inb_last		/* ACK */
    759 
    760 /*
    761  * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
    762  * If we get one, we use the tag returned to switch to find the proper
    763  * SCB.  With SCB paging, this requires using findSCB for both tagged
    764  * and non-tagged transactions since the SCB may exist in any slot.
    765  * If we're not using SCB paging, we can use the tag as the direct
    766  * index to the SCB.
    767  */
    768 	mvi	ARG_1,SCB_LIST_NULL	/* Default to no-tag */
    769 snoop_tag_loop:
    770 	test	SSTAT1,BUSFREE		jnz use_findSCB
    771 	test	SSTAT1,REQINIT		jz snoop_tag_loop
    772 	test	SSTAT1,PHASEMIS		jnz use_findSCB
    773 	mvi	A			call inb_first
    774 	cmp	A,MSG_SIMPLE_Q_TAG	jne use_findSCB
    775 get_tag:
    776 	mvi	ARG_1	call inb_next	/* tag value */
    777 /*
    778  * See if the tag is in range.  The tag is < SCBCOUNT if we add
    779  * the complement of SCBCOUNT to the incomming tag and there is
    780  * no carry.
    781  */
    782 	mov	A,COMP_SCBCOUNT
    783 	add	SINDEX,A,ARG_1
    784 	jc	abort_tag
    785 
    786 /*
    787  * Ensure that the SCB the tag points to is for an SCB transaction
    788  * to the reconnecting target.
    789  */
    790 	test	FLAGS, PAGESCBS	jz index_by_tag
    791 	call	inb_last			/* Ack Tag */
    792 use_findSCB:
    793 	mov	ALLZEROS	call findSCB	  /* Have to search */
    794 setup_SCB:
    795 	and	SCB_CONTROL,0xfb	  /* clear disconnect bit in SCB */
    796 	or	FLAGS,IDENTIFY_SEEN	  /* make note of IDENTIFY */
    797 	jmp	ITloop
    798 index_by_tag:
    799 	mov	SCBPTR,ARG_1
    800 	mov	A,SAVED_TCL
    801 	cmp	SCB_TCL,A		jne abort_tag
    802 	test	SCB_CONTROL,TAG_ENB	jz  abort_tag
    803 	call	inb_last			/* Ack Successful tag */
    804 	jmp	setup_SCB
    805 
    806 abort_tag:
    807 	or	SCSISIGO,ATNO			/* turn on ATNO */
    808 	mvi	INTSTAT,ABORT_TAG 		/* let driver know */
    809 	mvi	MSG_ABORT_TAG	call mk_mesg	/* ABORT TAG message */
    810 	jmp	mesgin_done
    811 
    812 /*
    813  * Message reject?  Let the kernel driver handle this.  If we have an
    814  * outstanding WDTR or SDTR negotiation, assume that it's a response from
    815  * the target selecting 8bit or asynchronous transfer, otherwise just ignore
    816  * it since we have no clue what it pertains to.
    817  */
    818 mesgin_reject:
    819 	mvi	INTSTAT, REJECT_MSG
    820 	jmp	mesgin_done
    821 
    822 /*
    823  * [ ADD MORE MESSAGE HANDLING HERE ]
    824  */
    825 
    826 /*
    827  * Bus free phase.  It might be useful to interrupt the device
    828  * driver if we aren't expecting this.  For now, make sure that
    829  * ATN isn't being asserted and look for a new command.
    830  */
    831 p_busfree:
    832 	mvi	CLRSINT1,CLRATNO
    833 	clr	LASTPHASE
    834 
    835 /*
    836  * if this is an immediate command, perform a psuedo command complete to
    837  * notify the driver.
    838  */
    839 	test	SCB_CMDLEN,0xff	jz status_ok
    840 	jmp	start
    841 
    842 /*
    843  * Locking the driver out, build a one-byte message passed in SINDEX
    844  * if there is no active message already.  SINDEX is returned intact.
    845  */
    846 mk_mesg:
    847 	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
    848 	test	MSG_LEN,0xff	jz mk_mesg1	/* Should always succeed */
    849 
    850 	/*
    851 	 * Hmmm.  For some reason the mesg buffer is in use.
    852 	 * Tell the driver.  It should look at SINDEX to find
    853 	 * out what we wanted to use the buffer for and resolve
    854 	 * the conflict.
    855 	 */
    856 	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
    857 	mvi	INTSTAT,MSG_BUFFER_BUSY
    858 
    859 mk_mesg1:
    860 	mvi	MSG_LEN,1		/* length = 1 */
    861 	mov	MSG0,SINDEX		/* 1-byte message */
    862 	mvi	SEQCTL,0x10	ret	/* !PAUSEDIS|FASTMODE */
    863 
    864 /*
    865  * Functions to read data in Automatic PIO mode.
    866  *
    867  * According to Adaptec's documentation, an ACK is not sent on input from
    868  * the target until SCSIDATL is read from.  So we wait until SCSIDATL is
    869  * latched (the usual way), then read the data byte directly off the bus
    870  * using SCSIBUSL.  When we have pulled the ATN line, or we just want to
    871  * acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
    872  * spec guarantees that the target will hold the data byte on the bus until
    873  * we send our ACK.
    874  *
    875  * The assumption here is that these are called in a particular sequence,
    876  * and that REQ is already set when inb_first is called.  inb_{first,next}
    877  * use the same calling convention as inb.
    878  */
    879 
    880 inb_next:
    881 	or	CLRSINT0, CLRSPIORDY
    882 	mov	NONE,SCSIDATL			/*dummy read from latch to ACK*/
    883 inb_next_wait:
    884 	test	SSTAT1,PHASEMIS	jnz mesgin_phasemis
    885 	test	SSTAT0,SPIORDY	jz inb_next_wait /* wait for next byte */
    886 inb_first:
    887 	mov	DINDEX,SINDEX
    888 	test	SSTAT1,PHASEMIS	jnz mesgin_phasemis
    889 	mov	DINDIR,SCSIBUSL	ret		/*read byte directly from bus*/
    890 inb_last:
    891 	mov	NONE,SCSIDATL ret		/*dummy read from latch to ACK*/
    892 
    893 mesgin_phasemis:
    894 /*
    895  * We expected to receive another byte, but the target changed phase
    896  */
    897 	mvi	INTSTAT, MSGIN_PHASEMIS
    898 	jmp	ITloop
    899 
    900 /*
    901  * DMA data transfer.  HADDR and HCNT must be loaded first, and
    902  * SINDEX should contain the value to load DFCNTRL with - 0x3d for
    903  * host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
    904  * during initialization.
    905  */
    906 dma:
    907 	mov	DFCNTRL,SINDEX
    908 dma1:
    909 	test	SSTAT0,DMADONE	jnz dma3
    910 	test	SSTAT1,PHASEMIS	jz dma1		/* ie. underrun */
    911 
    912 /*
    913  * We will be "done" DMAing when the transfer count goes to zero, or
    914  * the target changes the phase (in light of this, it makes sense that
    915  * the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
    916  * doing a SCSI->Host transfer, the data FIFO should be flushed auto-
    917  * magically on STCNT=0 or a phase change, so just wait for FIFO empty
    918  * status.
    919  */
    920 dma3:
    921 	test	SINDEX,DIRECTION	jnz dma5
    922 dma4:
    923 	test	DFSTATUS,FIFOEMP	jz dma4
    924 
    925 /*
    926  * Now shut the DMA enables off and make sure that the DMA enables are
    927  * actually off first lest we get an ILLSADDR.
    928  */
    929 dma5:
    930 	/* disable DMA, but maintain WIDEODD */
    931 	and	DFCNTRL,WIDEODD
    932 dma6:
    933 	test	DFCNTRL,0x38	jnz dma6  /* SCSIENACK|SDMAENACK|HDMAENACK */
    934 return:
    935 	ret
    936 
    937 /*
    938  * Common SCSI initialization for selection and reselection.  Expects
    939  * the target SCSI ID to be in the upper four bits of SINDEX, and A's
    940  * contents are stomped on return.
    941  */
    942 initialize_scsiid:
    943 	and	SINDEX,0xf0		/* Get target ID */
    944 	and	A,0x0f,SCSIID
    945 	or	SINDEX,A
    946 	mov	SCSIID,SINDEX ret
    947 
    948 /*
    949  * Assert that if we've been reselected, then we've seen an IDENTIFY
    950  * message.
    951  */
    952 assert:
    953 	test	FLAGS,RESELECTED	jz return	/* reselected? */
    954 	test	FLAGS,IDENTIFY_SEEN	jnz return	/* seen IDENTIFY? */
    955 
    956 	mvi	INTSTAT,NO_IDENT 	ret	/* no - cause a kernel panic */
    957 
    958 /*
    959  * Locate the SCB matching the target ID/channel/lun in SAVED_TCL, and the tag
    960  * value in ARG_1.  If ARG_1 == SCB_LIST_NULL, we're looking for a non-tagged
    961  * SCB.  Have the kernel print a warning message if it can't be found, and
    962  * generate an ABORT/ABORT_TAG message to the target.  SINDEX should be
    963  * cleared on call.
    964  */
    965 findSCB:
    966 	mov	A,SAVED_TCL
    967 	mov	SCBPTR,SINDEX			/* switch to next SCB */
    968 	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
    969 	cmp	SCB_TCL,A	jne findSCB1 /* target ID/channel/lun match? */
    970 	test	SCB_CONTROL,DISCONNECTED jz findSCB1 /*should be disconnected*/
    971 	test	SCB_CONTROL,TAG_ENB jnz findTaggedSCB
    972 	cmp	ARG_1,SCB_LIST_NULL je foundSCB
    973 	jmp	findSCB1
    974 findTaggedSCB:
    975 	mov	A, ARG_1			/* Tag passed in ARG_1 */
    976 	cmp	SCB_TAG,A	jne findSCB1	/* Found it? */
    977 foundSCB:
    978 	test	FLAGS,PAGESCBS	jz foundSCB_ret
    979 /* Remove this SCB from the disconnection list */
    980 	cmp	SCB_NEXT,SCB_LIST_NULL je unlink_prev
    981 	mov	SAVED_LINKPTR, SCB_PREV
    982 	mov	SCBPTR, SCB_NEXT
    983 	mov	SCB_PREV, SAVED_LINKPTR
    984 	mov	SCBPTR, SINDEX
    985 unlink_prev:
    986 	cmp	SCB_PREV,SCB_LIST_NULL	je rHead/* At the head of the list */
    987 	mov	SAVED_LINKPTR, SCB_NEXT
    988 	mov	SCBPTR, SCB_PREV
    989 	mov	SCB_NEXT, SAVED_LINKPTR
    990 	mov	SCBPTR, SINDEX
    991 	mvi	SEQCTL,0x10	ret		/* !PAUSEDIS|FASTMODE */
    992 rHead:
    993 	mov	DISCONNECTED_SCBH,SCB_NEXT
    994 foundSCB_ret:
    995 	mvi	SEQCTL,0x10	ret		/* !PAUSEDIS|FASTMODE */
    996 
    997 findSCB1:
    998 	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
    999 	inc	SINDEX
   1000 	mov	A,SCBCOUNT
   1001 	cmp	SINDEX,A	jne findSCB
   1002 
   1003 	mvi	INTSTAT,NO_MATCH		/* not found - signal kernel */
   1004 	cmp	RETURN_1,SCB_PAGEDIN je return
   1005 	or	SCSISIGO,ATNO			/* assert ATNO */
   1006 	cmp	ARG_1,SCB_LIST_NULL jne find_abort_tag
   1007 	mvi	MSG_ABORT	call mk_mesg
   1008 	jmp	ITloop
   1009 find_abort_tag:
   1010 	mvi	MSG_ABORT_TAG	call mk_mesg
   1011 	jmp	ITloop
   1012 
   1013 /*
   1014  * Make a working copy of the scatter-gather parameters from the SCB.
   1015  */
   1016 sg_scb2ram:
   1017 	mov	HADDR0, SCB_DATAPTR0
   1018 	mov	HADDR1, SCB_DATAPTR1
   1019 	mov	HADDR2, SCB_DATAPTR2
   1020 	mov	HADDR3, SCB_DATAPTR3
   1021 	mov	HCNT0, SCB_DATACNT0
   1022 	mov	HCNT1, SCB_DATACNT1
   1023 	mov	HCNT2, SCB_DATACNT2
   1024 
   1025 	mov	STCNT0, HCNT0
   1026 	mov	STCNT1, HCNT1
   1027 	mov	STCNT2, HCNT2
   1028 
   1029 	mov	SG_COUNT,SCB_SGCOUNT
   1030 
   1031 	mov	SG_NEXT0, SCB_SGPTR0
   1032 	mov	SG_NEXT1, SCB_SGPTR1
   1033 	mov	SG_NEXT2, SCB_SGPTR2
   1034 	mov	SG_NEXT3, SCB_SGPTR3 ret
   1035 
   1036 /*
   1037  * Copying RAM values back to SCB, for Save Data Pointers message, but
   1038  * only if we've actually been into a data phase to change them.  This
   1039  * protects against bogus data in scratch ram and the residual counts
   1040  * since they are only initialized when we go into data_in or data_out.
   1041  */
   1042 sg_ram2scb:
   1043 	test	FLAGS, DPHASE	jz return
   1044 	mov	SCB_SGCOUNT,SG_COUNT
   1045 
   1046 	mov	SCB_SGPTR0,SG_NEXT0
   1047 	mov	SCB_SGPTR1,SG_NEXT1
   1048 	mov	SCB_SGPTR2,SG_NEXT2
   1049 	mov	SCB_SGPTR3,SG_NEXT3
   1050 
   1051 	mov	SCB_DATAPTR0,SHADDR0
   1052 	mov	SCB_DATAPTR1,SHADDR1
   1053 	mov	SCB_DATAPTR2,SHADDR2
   1054 	mov	SCB_DATAPTR3,SHADDR3
   1055 
   1056 /*
   1057  * Use the residual number since STCNT is corrupted by any message transfer
   1058  */
   1059 	mov	SCB_DATACNT0,SCB_RESID_DCNT0
   1060 	mov	SCB_DATACNT1,SCB_RESID_DCNT1
   1061 	mov	SCB_DATACNT2,SCB_RESID_DCNT2 ret
   1062 
   1063 /*
   1064  * Add the array base TARG_SCRATCH to the target offset (the target address
   1065  * is in SCSIID), and return the result in SINDEX.  The accumulator
   1066  * contains the 3->8 decoding of the target ID on return.
   1067  */
   1068 ndx_dtr:
   1069 	shr	A,SCSIID,4
   1070 	test	SBLKCTL,SELBUSB	jz ndx_dtr_2
   1071 	or	A,0x08		/* Channel B entries add 8 */
   1072 ndx_dtr_2:
   1073 	add	SINDEX,TARG_SCRATCH,A ret
   1074