Home | History | Annotate | Line # | Download | only in aic7xxx
aic7xxx.seq revision 1.7
      1 /*	$NetBSD: aic7xxx.seq,v 1.7 1998/03/11 20:57:03 leo 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.7 1998/03/11 20:57:03 leo Exp $"
     47 
     48 #if defined(__NetBSD__)
     49 #include "../../ic/aic7xxxreg.h"
     50 #include "../../scsipi/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 #if BIG_ENDIAN_CPU
    429 	mov	HADDR3,DFDAT
    430 	mov	HADDR2,DFDAT
    431 	mov	HADDR1,DFDAT
    432 	mov	HADDR0,DFDAT
    433 #else
    434 	mov	HADDR0,DFDAT
    435 	mov	HADDR1,DFDAT
    436 	mov	HADDR2,DFDAT
    437 	mov	HADDR3,DFDAT
    438 #endif
    439 /*
    440  * For Linux, we must throw away four bytes since there is a 32bit gap
    441  * in the middle of a struct scatterlist.
    442  */
    443 #ifdef __linux__
    444 	mov	NONE,DFDAT
    445 	mov	NONE,DFDAT
    446 	mov	NONE,DFDAT
    447 	mov	NONE,DFDAT
    448 #endif
    449 #if BIG_ENDIAN_CPU
    450 	mov	HCNT2,DFDAT	/* LEO: MSB unused */
    451 	mov	HCNT2,DFDAT
    452 	mov	HCNT1,DFDAT
    453 	mov	HCNT0,DFDAT
    454 #else
    455 	mov	HCNT0,DFDAT
    456 	mov	HCNT1,DFDAT
    457 	mov	HCNT2,DFDAT
    458 #endif
    459 
    460 /* Load STCNT as well.  It is a mirror of HCNT */
    461 	mov	STCNT0,HCNT0
    462 	mov	STCNT1,HCNT1
    463 	mov	STCNT2,HCNT2
    464         test    SSTAT1,PHASEMIS  jz data_phase_loop
    465 
    466 data_phase_finish:
    467 /*
    468  * After a DMA finishes, save the SG and STCNT residuals back into the SCB
    469  * We use STCNT instead of HCNT, since it's a reflection of how many bytes
    470  * were transferred on the SCSI (as opposed to the host) bus.
    471  */
    472 	mov	SCB_RESID_DCNT0,STCNT0
    473 	mov	SCB_RESID_DCNT1,STCNT1
    474 	mov	SCB_RESID_DCNT2,STCNT2
    475 	mov	SCB_RESID_SGCNT, SG_COUNT
    476 	jmp	ITloop
    477 
    478 data_phase_overrun:
    479 /*
    480  * Turn off BITBUCKET mode and notify the host
    481  */
    482 	and	SXFRCTL1,0x7f		/* ~BITBUCKET */
    483 	mvi	INTSTAT,DATA_OVERRUN
    484 	jmp	ITloop
    485 
    486 /*
    487  * Command phase.  Set up the DMA registers and let 'er rip.
    488  */
    489 p_command:
    490 	call	assert
    491 
    492 /*
    493  * Load HADDR and HCNT.
    494  */
    495 #if BIG_ENDIAN_CPU
    496 	mov	HADDR3, SCB_CMDPTR0
    497 	mov	HADDR2, SCB_CMDPTR1
    498 	mov	HADDR1, SCB_CMDPTR2
    499 	mov	HADDR0, SCB_CMDPTR3
    500 #else
    501 	mov	HADDR0, SCB_CMDPTR0
    502 	mov	HADDR1, SCB_CMDPTR1
    503 	mov	HADDR2, SCB_CMDPTR2
    504 	mov	HADDR3, SCB_CMDPTR3
    505 #endif
    506 	mov	HCNT0, SCB_CMDLEN
    507 	clr	HCNT1
    508 	clr	HCNT2
    509 
    510 	mov	STCNT0, HCNT0
    511 	mov	STCNT1, HCNT1
    512 	mov	STCNT2, HCNT2
    513 
    514 	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
    515 						#   DIRECTION|FIFORESET
    516 	jmp	ITloop
    517 
    518 /*
    519  * Status phase.  Wait for the data byte to appear, then read it
    520  * and store it into the SCB.
    521  */
    522 p_status:
    523 	mvi	SCB_TARGET_STATUS	call inb_first
    524 	jmp	mesgin_done
    525 
    526 /*
    527  * Message out phase.  If there is not an active message, but the target
    528  * took us into this phase anyway, build a no-op message and send it.
    529  */
    530 p_mesgout:
    531 	test	MSG_LEN, 0xff	jnz  p_mesgout_start
    532 	mvi	MSG_NOOP	call mk_mesg	/* build NOP message */
    533 
    534 p_mesgout_start:
    535 /*
    536  * Set up automatic PIO transfer from MSG0.  Bit 3 in
    537  * SXFRCTL0 (SPIOEN) is already on.
    538  */
    539 	mvi	SINDEX,MSG0
    540 	mov	DINDEX,MSG_LEN
    541 
    542 /*
    543  * When target asks for a byte, drop ATN if it's the last one in
    544  * the message.  Otherwise, keep going until the message is exhausted.
    545  *
    546  * Keep an eye out for a phase change, in case the target issues
    547  * a MESSAGE REJECT.
    548  */
    549 p_mesgout_loop:
    550 	test	SSTAT1,PHASEMIS	jnz p_mesgout_phasemis
    551 	test	SSTAT0,SPIORDY	jz p_mesgout_loop
    552 	test	SSTAT1,PHASEMIS	jnz p_mesgout_phasemis
    553 	cmp	DINDEX,1	jne p_mesgout_outb	/* last byte? */
    554 	mvi	CLRSINT1,CLRATNO			/* drop ATN */
    555 p_mesgout_outb:
    556 	dec	DINDEX
    557 	or	CLRSINT0, CLRSPIORDY
    558 	mov	SCSIDATL,SINDIR
    559 
    560 p_mesgout4:
    561 	test	DINDEX,0xff	jnz p_mesgout_loop
    562 
    563 /*
    564  * If the next bus phase after ATN drops is a message out, it means
    565  * that the target is requesting that the last message(s) be resent.
    566  */
    567 p_mesgout_snoop:
    568 	test	SSTAT1,BUSFREE	jnz p_mesgout_done
    569 	test	SSTAT1,REQINIT	jz p_mesgout_snoop
    570 
    571 	test	SSTAT1,PHASEMIS	jnz p_mesgout_done
    572 
    573 	or	SCSISIGO,ATNO			/* turn on ATNO */
    574 
    575 	jmp	ITloop
    576 
    577 p_mesgout_phasemis:
    578 	mvi	CLRSINT1,CLRATNO	/* Be sure to turn ATNO off */
    579 p_mesgout_done:
    580 	clr	MSG_LEN			/* no active msg */
    581 	jmp	ITloop
    582 
    583 /*
    584  * Message in phase.  Bytes are read using Automatic PIO mode.
    585  */
    586 p_mesgin:
    587 	mvi	A		call inb_first	/* read the 1st message byte */
    588 	mov	REJBYTE,A			/* save it for the driver */
    589 
    590 	test	A,MSG_IDENTIFYFLAG	jnz mesgin_identify
    591 	cmp	A,MSG_DISCONNECT	je mesgin_disconnect
    592 	cmp	A,MSG_SAVEDATAPOINTER	je mesgin_sdptrs
    593 	cmp	ALLZEROS,A		je mesgin_complete
    594 	cmp	A,MSG_RESTOREPOINTERS	je mesgin_rdptrs
    595 	cmp	A,MSG_EXTENDED		je mesgin_extended
    596 	cmp	A,MSG_MESSAGE_REJECT	je mesgin_reject
    597 
    598 rej_mesgin:
    599 /*
    600  * We have no idea what this message in is, and there's no way
    601  * to pass it up to the kernel, so we issue a message reject and
    602  * hope for the best.  Since we're now using manual PIO mode to
    603  * read in the message, there should no longer be a race condition
    604  * present when we assert ATN.  In any case, rejection should be a
    605  * rare occurrence - signal the driver when it happens.
    606  */
    607 	or	SCSISIGO,ATNO			/* turn on ATNO */
    608 	mvi	INTSTAT,SEND_REJECT		/* let driver know */
    609 
    610 	mvi	MSG_MESSAGE_REJECT	call mk_mesg
    611 
    612 mesgin_done:
    613 	call	inb_last			/*ack & turn auto PIO back on*/
    614 	jmp	ITloop
    615 
    616 
    617 mesgin_complete:
    618 /*
    619  * We got a "command complete" message, so put the SCB_TAG into QUEUEOUT,
    620  * and trigger a completion interrupt.  Check status for non zero return
    621  * and interrupt driver if needed.  This allows the driver to interpret
    622  * errors only when they occur instead of always uploading the scb.  If
    623  * the status is SCSI_CHECK, the driver will download a new scb requesting
    624  * sense to replace the old one, modify the "waiting for selection" SCB list
    625  * and set RETURN_1 to SEND_SENSE.  If RETURN_1 is set to SEND_SENSE the
    626  * sequencer imediately jumps to main loop where it will run down the waiting
    627  * SCB list and process the sense request.  If the kernel driver does not
    628  * wish to request sense, it need only clear RETURN_1, and the command is
    629  * allowed to complete.  We don't bother to post to the QOUTFIFO in the
    630  * error case since it would require extra work in the kernel driver to
    631  * ensure that the entry was removed before the command complete code tried
    632  * processing it.
    633  *
    634  * First check for residuals
    635  */
    636 	test	SCB_RESID_SGCNT,0xff	jz check_status
    637 /*
    638  * If we have a residual count, interrupt and tell the host.  Other
    639  * alternatives are to pause the sequencer on all command completes (yuck),
    640  * dma the resid directly to the host (slick, we may have space to do it now)
    641  * or have the sequencer pause itself when it encounters a non-zero resid
    642  * (unecessary pause just to flag the command -yuck-, but takes one instruction
    643  * and since it shouldn't happen that often is good enough for our purposes).
    644  */
    645 resid:
    646 	mvi	INTSTAT,RESIDUAL
    647 
    648 check_status:
    649 	test	SCB_TARGET_STATUS,0xff	jz status_ok	/* Good Status? */
    650 	mvi	INTSTAT,BAD_STATUS			/* let driver know */
    651 	cmp	RETURN_1, SEND_SENSE	jne status_ok
    652 	jmp	mesgin_done
    653 
    654 status_ok:
    655 /* First, mark this target as free. */
    656 	test	SCB_CONTROL,TAG_ENB jnz test_immediate	/*
    657 							 * Tagged commands
    658 							 * don't busy the
    659 							 * target.
    660 							 */
    661 	mov	FUNCTION1,SCB_TCL
    662 	mov	A,FUNCTION1
    663 	test	SCB_TCL,0x88 jz clear_a
    664 	xor	ACTIVE_B,A
    665 	jmp	test_immediate
    666 
    667 clear_a:
    668 	xor	ACTIVE_A,A
    669 
    670 test_immediate:
    671 	test    SCB_CMDLEN,0xff jnz complete  /* Immediate message complete */
    672 /*
    673  * Pause the sequencer until the driver gets around to handling the command
    674  * complete.  This is so that any action that might require carefull timing
    675  * with the completion of this command can occur.
    676  */
    677 	mvi	INTSTAT,IMMEDDONE
    678 	jmp	start
    679 complete:
    680 	mov	QOUTFIFO,SCB_TAG
    681 	mvi	INTSTAT,CMDCMPLT
    682 	jmp	mesgin_done
    683 
    684 
    685 /*
    686  * Is it an extended message?  Copy the message to our message buffer and
    687  * notify the host.  The host will tell us whether to reject this message,
    688  * respond to it with the message that the host placed in our message buffer,
    689  * or simply to do nothing.
    690  */
    691 mesgin_extended:
    692 	mvi	MSGIN_EXT_LEN	 call inb_next
    693 	mvi	MSGIN_EXT_OPCODE call inb_next
    694 	mov	A, MSGIN_EXT_LEN
    695 	dec	A	/* Length counts the op code */
    696 	mvi	SINDEX, MSGIN_EXT_BYTE0
    697 mesgin_extended_loop:
    698 	test	A, 0xFF		jz mesgin_extended_intr
    699 	cmp	SINDEX, MSGIN_EXT_LASTBYTE je mesgin_extended_dump
    700 	call	inb_next
    701 	dec	A
    702 /*
    703  * We pass the arg to inb in SINDEX, but DINDEX is the one incremented
    704  * so update SINDEX with DINDEX's value before looping again.
    705  */
    706 	mov	DINDEX		jmp mesgin_extended_loop
    707 mesgin_extended_dump:
    708 /* We have no more storage space, so dump the rest */
    709 	test	A, 0xFF		jz mesgin_extended_intr
    710 	mvi	NONE		call inb_next
    711 	dec	A
    712 	jmp	mesgin_extended_dump
    713 mesgin_extended_intr:
    714 	mvi	INTSTAT,EXTENDED_MSG		/* let driver know */
    715 	cmp	RETURN_1,SEND_REJ je rej_mesgin
    716 	cmp	RETURN_1,SEND_MSG jne mesgin_done
    717 /* The kernel has setup a message to be sent */
    718 	or	SCSISIGO,ATNO			/* turn on ATNO */
    719 	jmp	mesgin_done
    720 
    721 /*
    722  * Is it a disconnect message?  Set a flag in the SCB to remind us
    723  * and await the bus going free.
    724  */
    725 mesgin_disconnect:
    726 	or	SCB_CONTROL,DISCONNECTED
    727 	test	FLAGS, PAGESCBS jz mesgin_done
    728 /*
    729  * Link this SCB into the DISCONNECTED list.  This list holds the
    730  * candidates for paging out an SCB if one is needed for a new command.
    731  * Modifying the disconnected list is a critical(pause dissabled) section.
    732  */
    733 	mvi	SCB_PREV, SCB_LIST_NULL
    734 	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
    735 	mov	SCB_NEXT, DISCONNECTED_SCBH
    736 	mov	DISCONNECTED_SCBH, SCBPTR
    737 	cmp	SCB_NEXT,SCB_LIST_NULL je linkdone
    738 	mov	SCBPTR,SCB_NEXT
    739 	mov	SCB_PREV,DISCONNECTED_SCBH
    740 	mov	SCBPTR,DISCONNECTED_SCBH
    741 linkdone:
    742 	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
    743 	jmp	mesgin_done
    744 
    745 /*
    746  * Save data pointers message?  Copy working values into the SCB,
    747  * usually in preparation for a disconnect.
    748  */
    749 mesgin_sdptrs:
    750 	call	sg_ram2scb
    751 	jmp	mesgin_done
    752 
    753 /*
    754  * Restore pointers message?  Data pointers are recopied from the
    755  * SCB anytime we enter a data phase for the first time, so all
    756  * we need to do is clear the DPHASE flag and let the data phase
    757  * code do the rest.
    758  */
    759 mesgin_rdptrs:
    760 	and	FLAGS,0xef			/*
    761 						 * !DPHASE we'll reload them
    762 						 * the next time through
    763 						 */
    764 	jmp	mesgin_done
    765 
    766 /*
    767  * Identify message?  For a reconnecting target, this tells us the lun
    768  * that the reconnection is for - find the correct SCB and switch to it,
    769  * clearing the "disconnected" bit so we don't "find" it by accident later.
    770  */
    771 mesgin_identify:
    772 	test	A,0x78	jnz rej_mesgin	/*!DiscPriv|!LUNTAR|!Reserved*/
    773 
    774 	and	A,0x07			/* lun in lower three bits */
    775 	or      SAVED_TCL,A,SELID
    776 	and     SAVED_TCL,0xf7
    777 	and     A,SELBUSB,SBLKCTL	/* B Channel?? */
    778 	or      SAVED_TCL,A
    779 	call	inb_last		/* ACK */
    780 
    781 /*
    782  * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
    783  * If we get one, we use the tag returned to switch to find the proper
    784  * SCB.  With SCB paging, this requires using findSCB for both tagged
    785  * and non-tagged transactions since the SCB may exist in any slot.
    786  * If we're not using SCB paging, we can use the tag as the direct
    787  * index to the SCB.
    788  */
    789 	mvi	ARG_1,SCB_LIST_NULL	/* Default to no-tag */
    790 snoop_tag_loop:
    791 	test	SSTAT1,BUSFREE		jnz use_findSCB
    792 	test	SSTAT1,REQINIT		jz snoop_tag_loop
    793 	test	SSTAT1,PHASEMIS		jnz use_findSCB
    794 	mvi	A			call inb_first
    795 	cmp	A,MSG_SIMPLE_Q_TAG	jne use_findSCB
    796 get_tag:
    797 	mvi	ARG_1	call inb_next	/* tag value */
    798 /*
    799  * See if the tag is in range.  The tag is < SCBCOUNT if we add
    800  * the complement of SCBCOUNT to the incomming tag and there is
    801  * no carry.
    802  */
    803 	mov	A,COMP_SCBCOUNT
    804 	add	SINDEX,A,ARG_1
    805 	jc	abort_tag
    806 
    807 /*
    808  * Ensure that the SCB the tag points to is for an SCB transaction
    809  * to the reconnecting target.
    810  */
    811 	test	FLAGS, PAGESCBS	jz index_by_tag
    812 	call	inb_last			/* Ack Tag */
    813 use_findSCB:
    814 	mov	ALLZEROS	call findSCB	  /* Have to search */
    815 setup_SCB:
    816 	and	SCB_CONTROL,0xfb	  /* clear disconnect bit in SCB */
    817 	or	FLAGS,IDENTIFY_SEEN	  /* make note of IDENTIFY */
    818 	jmp	ITloop
    819 index_by_tag:
    820 	mov	SCBPTR,ARG_1
    821 	mov	A,SAVED_TCL
    822 	cmp	SCB_TCL,A		jne abort_tag
    823 	test	SCB_CONTROL,TAG_ENB	jz  abort_tag
    824 	call	inb_last			/* Ack Successful tag */
    825 	jmp	setup_SCB
    826 
    827 abort_tag:
    828 	or	SCSISIGO,ATNO			/* turn on ATNO */
    829 	mvi	INTSTAT,ABORT_TAG 		/* let driver know */
    830 	mvi	MSG_ABORT_TAG	call mk_mesg	/* ABORT TAG message */
    831 	jmp	mesgin_done
    832 
    833 /*
    834  * Message reject?  Let the kernel driver handle this.  If we have an
    835  * outstanding WDTR or SDTR negotiation, assume that it's a response from
    836  * the target selecting 8bit or asynchronous transfer, otherwise just ignore
    837  * it since we have no clue what it pertains to.
    838  */
    839 mesgin_reject:
    840 	mvi	INTSTAT, REJECT_MSG
    841 	jmp	mesgin_done
    842 
    843 /*
    844  * [ ADD MORE MESSAGE HANDLING HERE ]
    845  */
    846 
    847 /*
    848  * Bus free phase.  It might be useful to interrupt the device
    849  * driver if we aren't expecting this.  For now, make sure that
    850  * ATN isn't being asserted and look for a new command.
    851  */
    852 p_busfree:
    853 	mvi	CLRSINT1,CLRATNO
    854 	clr	LASTPHASE
    855 
    856 /*
    857  * if this is an immediate command, perform a psuedo command complete to
    858  * notify the driver.
    859  */
    860 	test	SCB_CMDLEN,0xff	jz status_ok
    861 	jmp	start
    862 
    863 /*
    864  * Locking the driver out, build a one-byte message passed in SINDEX
    865  * if there is no active message already.  SINDEX is returned intact.
    866  */
    867 mk_mesg:
    868 	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
    869 	test	MSG_LEN,0xff	jz mk_mesg1	/* Should always succeed */
    870 
    871 	/*
    872 	 * Hmmm.  For some reason the mesg buffer is in use.
    873 	 * Tell the driver.  It should look at SINDEX to find
    874 	 * out what we wanted to use the buffer for and resolve
    875 	 * the conflict.
    876 	 */
    877 	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
    878 	mvi	INTSTAT,MSG_BUFFER_BUSY
    879 
    880 mk_mesg1:
    881 	mvi	MSG_LEN,1		/* length = 1 */
    882 	mov	MSG0,SINDEX		/* 1-byte message */
    883 	mvi	SEQCTL,0x10	ret	/* !PAUSEDIS|FASTMODE */
    884 
    885 /*
    886  * Functions to read data in Automatic PIO mode.
    887  *
    888  * According to Adaptec's documentation, an ACK is not sent on input from
    889  * the target until SCSIDATL is read from.  So we wait until SCSIDATL is
    890  * latched (the usual way), then read the data byte directly off the bus
    891  * using SCSIBUSL.  When we have pulled the ATN line, or we just want to
    892  * acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
    893  * spec guarantees that the target will hold the data byte on the bus until
    894  * we send our ACK.
    895  *
    896  * The assumption here is that these are called in a particular sequence,
    897  * and that REQ is already set when inb_first is called.  inb_{first,next}
    898  * use the same calling convention as inb.
    899  */
    900 
    901 inb_next:
    902 	or	CLRSINT0, CLRSPIORDY
    903 	mov	NONE,SCSIDATL			/*dummy read from latch to ACK*/
    904 inb_next_wait:
    905 	test	SSTAT1,PHASEMIS	jnz mesgin_phasemis
    906 	test	SSTAT0,SPIORDY	jz inb_next_wait /* wait for next byte */
    907 inb_first:
    908 	mov	DINDEX,SINDEX
    909 	test	SSTAT1,PHASEMIS	jnz mesgin_phasemis
    910 	mov	DINDIR,SCSIBUSL	ret		/*read byte directly from bus*/
    911 inb_last:
    912 	mov	NONE,SCSIDATL ret		/*dummy read from latch to ACK*/
    913 
    914 mesgin_phasemis:
    915 /*
    916  * We expected to receive another byte, but the target changed phase
    917  */
    918 	mvi	INTSTAT, MSGIN_PHASEMIS
    919 	jmp	ITloop
    920 
    921 /*
    922  * DMA data transfer.  HADDR and HCNT must be loaded first, and
    923  * SINDEX should contain the value to load DFCNTRL with - 0x3d for
    924  * host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
    925  * during initialization.
    926  */
    927 dma:
    928 	mov	DFCNTRL,SINDEX
    929 dma1:
    930 	test	SSTAT0,DMADONE	jnz dma3
    931 	test	SSTAT1,PHASEMIS	jz dma1		/* ie. underrun */
    932 
    933 /*
    934  * We will be "done" DMAing when the transfer count goes to zero, or
    935  * the target changes the phase (in light of this, it makes sense that
    936  * the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
    937  * doing a SCSI->Host transfer, the data FIFO should be flushed auto-
    938  * magically on STCNT=0 or a phase change, so just wait for FIFO empty
    939  * status.
    940  */
    941 dma3:
    942 	test	SINDEX,DIRECTION	jnz dma5
    943 dma4:
    944 	test	DFSTATUS,FIFOEMP	jz dma4
    945 
    946 /*
    947  * Now shut the DMA enables off and make sure that the DMA enables are
    948  * actually off first lest we get an ILLSADDR.
    949  */
    950 dma5:
    951 	/* disable DMA, but maintain WIDEODD */
    952 	and	DFCNTRL,WIDEODD
    953 dma6:
    954 	test	DFCNTRL,0x38	jnz dma6  /* SCSIENACK|SDMAENACK|HDMAENACK */
    955 return:
    956 	ret
    957 
    958 /*
    959  * Common SCSI initialization for selection and reselection.  Expects
    960  * the target SCSI ID to be in the upper four bits of SINDEX, and A's
    961  * contents are stomped on return.
    962  */
    963 initialize_scsiid:
    964 	and	SINDEX,0xf0		/* Get target ID */
    965 	and	A,0x0f,SCSIID
    966 	or	SINDEX,A
    967 	mov	SCSIID,SINDEX ret
    968 
    969 /*
    970  * Assert that if we've been reselected, then we've seen an IDENTIFY
    971  * message.
    972  */
    973 assert:
    974 	test	FLAGS,RESELECTED	jz return	/* reselected? */
    975 	test	FLAGS,IDENTIFY_SEEN	jnz return	/* seen IDENTIFY? */
    976 
    977 	mvi	INTSTAT,NO_IDENT 	ret	/* no - cause a kernel panic */
    978 
    979 /*
    980  * Locate the SCB matching the target ID/channel/lun in SAVED_TCL, and the tag
    981  * value in ARG_1.  If ARG_1 == SCB_LIST_NULL, we're looking for a non-tagged
    982  * SCB.  Have the kernel print a warning message if it can't be found, and
    983  * generate an ABORT/ABORT_TAG message to the target.  SINDEX should be
    984  * cleared on call.
    985  */
    986 findSCB:
    987 	mov	A,SAVED_TCL
    988 	mov	SCBPTR,SINDEX			/* switch to next SCB */
    989 	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
    990 	cmp	SCB_TCL,A	jne findSCB1 /* target ID/channel/lun match? */
    991 	test	SCB_CONTROL,DISCONNECTED jz findSCB1 /*should be disconnected*/
    992 	test	SCB_CONTROL,TAG_ENB jnz findTaggedSCB
    993 	cmp	ARG_1,SCB_LIST_NULL je foundSCB
    994 	jmp	findSCB1
    995 findTaggedSCB:
    996 	mov	A, ARG_1			/* Tag passed in ARG_1 */
    997 	cmp	SCB_TAG,A	jne findSCB1	/* Found it? */
    998 foundSCB:
    999 	test	FLAGS,PAGESCBS	jz foundSCB_ret
   1000 /* Remove this SCB from the disconnection list */
   1001 	cmp	SCB_NEXT,SCB_LIST_NULL je unlink_prev
   1002 	mov	SAVED_LINKPTR, SCB_PREV
   1003 	mov	SCBPTR, SCB_NEXT
   1004 	mov	SCB_PREV, SAVED_LINKPTR
   1005 	mov	SCBPTR, SINDEX
   1006 unlink_prev:
   1007 	cmp	SCB_PREV,SCB_LIST_NULL	je rHead/* At the head of the list */
   1008 	mov	SAVED_LINKPTR, SCB_NEXT
   1009 	mov	SCBPTR, SCB_PREV
   1010 	mov	SCB_NEXT, SAVED_LINKPTR
   1011 	mov	SCBPTR, SINDEX
   1012 	mvi	SEQCTL,0x10	ret		/* !PAUSEDIS|FASTMODE */
   1013 rHead:
   1014 	mov	DISCONNECTED_SCBH,SCB_NEXT
   1015 foundSCB_ret:
   1016 	mvi	SEQCTL,0x10	ret		/* !PAUSEDIS|FASTMODE */
   1017 
   1018 findSCB1:
   1019 	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
   1020 	inc	SINDEX
   1021 	mov	A,SCBCOUNT
   1022 	cmp	SINDEX,A	jne findSCB
   1023 
   1024 	mvi	INTSTAT,NO_MATCH		/* not found - signal kernel */
   1025 	cmp	RETURN_1,SCB_PAGEDIN je return
   1026 	or	SCSISIGO,ATNO			/* assert ATNO */
   1027 	cmp	ARG_1,SCB_LIST_NULL jne find_abort_tag
   1028 	mvi	MSG_ABORT	call mk_mesg
   1029 	jmp	ITloop
   1030 find_abort_tag:
   1031 	mvi	MSG_ABORT_TAG	call mk_mesg
   1032 	jmp	ITloop
   1033 
   1034 /*
   1035  * Make a working copy of the scatter-gather parameters from the SCB.
   1036  */
   1037 sg_scb2ram:
   1038 #if BIG_ENDIAN_CPU
   1039 	mov	HADDR3, SCB_DATAPTR0
   1040 	mov	HADDR2, SCB_DATAPTR1
   1041 	mov	HADDR1, SCB_DATAPTR2
   1042 	mov	HADDR0, SCB_DATAPTR3
   1043 	mov	HCNT0, SCB_DATACNT3
   1044 	mov	HCNT1, SCB_DATACNT2
   1045 	mov	HCNT2, SCB_DATACNT1
   1046 #else
   1047 	mov	HADDR0, SCB_DATAPTR0
   1048 	mov	HADDR1, SCB_DATAPTR1
   1049 	mov	HADDR2, SCB_DATAPTR2
   1050 	mov	HADDR3, SCB_DATAPTR3
   1051 	mov	HCNT0, SCB_DATACNT0
   1052 	mov	HCNT1, SCB_DATACNT1
   1053 	mov	HCNT2, SCB_DATACNT2
   1054 #endif
   1055 
   1056 	mov	STCNT0, HCNT0
   1057 	mov	STCNT1, HCNT1
   1058 	mov	STCNT2, HCNT2
   1059 
   1060 	mov	SG_COUNT,SCB_SGCOUNT
   1061 
   1062 #if BIG_ENDIAN_CPU
   1063 	mov	SG_NEXT3, SCB_SGPTR0
   1064 	mov	SG_NEXT2, SCB_SGPTR1
   1065 	mov	SG_NEXT1, SCB_SGPTR2
   1066 	mov	SG_NEXT0, SCB_SGPTR3 ret
   1067 #else
   1068 	mov	SG_NEXT0, SCB_SGPTR0
   1069 	mov	SG_NEXT1, SCB_SGPTR1
   1070 	mov	SG_NEXT2, SCB_SGPTR2
   1071 	mov	SG_NEXT3, SCB_SGPTR3 ret
   1072 #endif
   1073 
   1074 /*
   1075  * Copying RAM values back to SCB, for Save Data Pointers message, but
   1076  * only if we've actually been into a data phase to change them.  This
   1077  * protects against bogus data in scratch ram and the residual counts
   1078  * since they are only initialized when we go into data_in or data_out.
   1079  */
   1080 sg_ram2scb:
   1081 	test	FLAGS, DPHASE	jz return
   1082 	mov	SCB_SGCOUNT,SG_COUNT
   1083 
   1084 #if BIG_ENDIAN_CPU
   1085 	mov	SCB_SGPTR3,SG_NEXT0
   1086 	mov	SCB_SGPTR2,SG_NEXT1
   1087 	mov	SCB_SGPTR1,SG_NEXT2
   1088 	mov	SCB_SGPTR0,SG_NEXT3
   1089 	mov	SCB_DATAPTR3,SHADDR0
   1090 	mov	SCB_DATAPTR2,SHADDR1
   1091 	mov	SCB_DATAPTR1,SHADDR2
   1092 	mov	SCB_DATAPTR0,SHADDR3
   1093 #else
   1094 	mov	SCB_SGPTR0,SG_NEXT0
   1095 	mov	SCB_SGPTR1,SG_NEXT1
   1096 	mov	SCB_SGPTR2,SG_NEXT2
   1097 	mov	SCB_SGPTR3,SG_NEXT3
   1098 	mov	SCB_DATAPTR0,SHADDR0
   1099 	mov	SCB_DATAPTR1,SHADDR1
   1100 	mov	SCB_DATAPTR2,SHADDR2
   1101 	mov	SCB_DATAPTR3,SHADDR3
   1102 #endif
   1103 
   1104 
   1105 /*
   1106  * Use the residual number since STCNT is corrupted by any message transfer
   1107  */
   1108 #if BIG_ENDIAN_CPU
   1109 	mov	SCB_DATACNT3,SCB_RESID_DCNT0
   1110 	mov	SCB_DATACNT2,SCB_RESID_DCNT1
   1111 	mov	SCB_DATACNT1,SCB_RESID_DCNT2 ret
   1112 #else
   1113 	mov	SCB_DATACNT0,SCB_RESID_DCNT0
   1114 	mov	SCB_DATACNT1,SCB_RESID_DCNT1
   1115 	mov	SCB_DATACNT2,SCB_RESID_DCNT2 ret
   1116 #endif
   1117 
   1118 /*
   1119  * Add the array base TARG_SCRATCH to the target offset (the target address
   1120  * is in SCSIID), and return the result in SINDEX.  The accumulator
   1121  * contains the 3->8 decoding of the target ID on return.
   1122  */
   1123 ndx_dtr:
   1124 	shr	A,SCSIID,4
   1125 	test	SBLKCTL,SELBUSB	jz ndx_dtr_2
   1126 	or	A,0x08		/* Channel B entries add 8 */
   1127 ndx_dtr_2:
   1128 	add	SINDEX,TARG_SCRATCH,A ret
   1129