Home | History | Annotate | Line # | Download | only in aic7xxx
aic79xx.seq revision 1.4
      1 /*	$NetBSD: aic79xx.seq,v 1.4 2003/08/29 00:10:01 thorpej Exp $	*/
      2 
      3 /*
      4  * Adaptec U320 device driver firmware for Linux and FreeBSD.
      5  *
      6  * Copyright (c) 1994-2001 Justin T. Gibbs.
      7  * Copyright (c) 2000-2002 Adaptec Inc.
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions, and the following disclaimer,
     15  *    without modification.
     16  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     17  *    substantially similar to the "NO WARRANTY" disclaimer below
     18  *    ("Disclaimer") and any redistribution must be conditioned upon
     19  *    including a substantially similar Disclaimer requirement for further
     20  *    binary redistribution.
     21  * 3. Neither the names of the above-listed copyright holders nor the names
     22  *    of any contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * Alternatively, this software may be distributed under the terms of the
     26  * GNU General Public License ("GPL") version 2 as published by the Free
     27  * Software Foundation.
     28  *
     29  * NO WARRANTY
     30  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     31  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     32  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     33  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     34  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR 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,
     38  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     39  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     40  * POSSIBILITY OF SUCH DAMAGES.
     41  *
     42  * $FreeBSD: src/sys/dev/aic7xxx/aic79xx.seq,v 1.8 2003/05/04 00:20:07 gibbs Exp $
     43  */
     44 
     45 VERSION = "Id: //depot/aic7xxx/aic7xxx/aic79xx.seq#91 $"
     46 PATCH_ARG_LIST = "struct ahd_softc *ahd"
     47 PREFIX = "ahd_"
     48 
     49 #include <dev/microcode/aic7xxx/aic79xx.reg>
     50 #include <dev/scsipi/scsi_message.h>
     51 
     52 restart:
     53 if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
     54 	test	SEQINTCODE, 0xFF jz idle_loop;
     55 	SET_SEQINTCODE(NO_SEQINT)
     56 }
     57 
     58 idle_loop:
     59 
     60 	if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
     61 		/*
     62 		 * Convert ERROR status into a sequencer
     63 		 * interrupt to handle the case of an
     64 		 * interrupt collision on the hardware
     65 		 * setting of HWERR.
     66 		 */
     67 		test	ERROR, 0xFF jz no_error_set;
     68 		SET_SEQINTCODE(SAW_HWERR)
     69 no_error_set:
     70 	}
     71 	SET_MODE(M_SCSI, M_SCSI)
     72 	test	SCSISEQ0, ENSELO|ENARBO jnz idle_loop_checkbus;
     73 	test	SEQ_FLAGS2, SELECTOUT_QFROZEN jnz idle_loop_checkbus;
     74 	cmp	WAITING_TID_HEAD[1], SCB_LIST_NULL je idle_loop_checkbus;
     75 	/*
     76 	 * ENSELO is cleared by a SELDO, so we must test for SELDO
     77 	 * one last time.
     78 	 */
     79 BEGIN_CRITICAL;
     80 	test	SSTAT0, SELDO jnz select_out;
     81 END_CRITICAL;
     82 	call	start_selection;
     83 idle_loop_checkbus:
     84 BEGIN_CRITICAL;
     85 	test	SSTAT0, SELDO jnz select_out;
     86 END_CRITICAL;
     87 	test	SSTAT0, SELDI jnz select_in;
     88 	test	SCSIPHASE, ~DATA_PHASE_MASK jz idle_loop_check_nonpackreq;
     89 	test	SCSISIGO, ATNO jz idle_loop_check_nonpackreq;
     90 	call	unexpected_nonpkt_phase_find_ctxt;
     91 idle_loop_check_nonpackreq:
     92 	test	SSTAT2, NONPACKREQ jz . + 2;
     93 	call	unexpected_nonpkt_phase_find_ctxt;
     94 	if ((ahd->bugs & AHD_FAINT_LED_BUG) != 0) {
     95 		and	A, FIFO0FREE|FIFO1FREE, DFFSTAT;
     96 		cmp	A, FIFO0FREE|FIFO1FREE jne . + 3;
     97 		and	SBLKCTL, ~DIAGLEDEN|DIAGLEDON;
     98 		jmp	. + 2;
     99 		or	SBLKCTL, DIAGLEDEN|DIAGLEDON;
    100 	}
    101 	call	idle_loop_gsfifo_in_scsi_mode;
    102 	call	idle_loop_service_fifos;
    103 	call	idle_loop_cchan;
    104 	jmp	idle_loop;
    105 
    106 BEGIN_CRITICAL;
    107 idle_loop_gsfifo:
    108 	SET_MODE(M_SCSI, M_SCSI)
    109 idle_loop_gsfifo_in_scsi_mode:
    110 	test	LQISTAT2, LQIGSAVAIL jz return;
    111 	/*
    112 	 * We have received good status for this transaction.  There may
    113 	 * still be data in our FIFOs draining to the host.  Complete
    114 	 * the SCB only if all data has transferred to the host.
    115 	 */
    116 good_status_IU_done:
    117 	bmov	SCBPTR, GSFIFO, 2;
    118 	clr	SCB_SCSI_STATUS;
    119 	/*
    120 	 * If a command completed before an attempted task management
    121 	 * function completed, notify the host after disabling any
    122 	 * pending select-outs.
    123 	 */
    124 	test	SCB_TASK_MANAGEMENT, 0xFF jz gsfifo_complete_normally;
    125 	test	SSTAT0, SELDO|SELINGO jnz . + 2;
    126 	and	SCSISEQ0, ~ENSELO;
    127 	SET_SEQINTCODE(TASKMGMT_CMD_CMPLT_OKAY)
    128 gsfifo_complete_normally:
    129 	or	SCB_CONTROL, STATUS_RCVD;
    130 
    131 	/*
    132 	 * Since this status did not consume a FIFO, we have to
    133 	 * be a bit more dilligent in how we check for FIFOs pertaining
    134 	 * to this transaction.  There are two states that a FIFO still
    135 	 * transferring data may be in.
    136 	 *
    137 	 * 1) Configured and draining to the host, with a FIFO handler.
    138 	 * 2) Pending cfg4data, fifo not empty.
    139 	 *
    140 	 * Case 1 can be detected by noticing a non-zero FIFO active
    141 	 * count in the SCB.  In this case, we allow the routine servicing
    142 	 * the FIFO to complete the SCB.
    143 	 *
    144 	 * Case 2 implies either a pending or yet to occur save data
    145 	 * pointers for this same context in the other FIFO.  So, if
    146 	 * we detect case 1, we will properly defer the post of the SCB
    147 	 * and achieve the desired result.  The pending cfg4data will
    148 	 * notice that status has been received and complete the SCB.
    149 	 */
    150 	test	SCB_FIFO_USE_COUNT, 0xFF jnz idle_loop_gsfifo_in_scsi_mode;
    151 	call	complete;
    152 END_CRITICAL;
    153 	jmp	idle_loop_gsfifo_in_scsi_mode;
    154 
    155 idle_loop_service_fifos:
    156 	SET_MODE(M_DFF0, M_DFF0)
    157 	test	LONGJMP_ADDR[1], INVALID_ADDR jnz idle_loop_next_fifo;
    158 	call	longjmp;
    159 idle_loop_next_fifo:
    160 	SET_MODE(M_DFF1, M_DFF1)
    161 	test	LONGJMP_ADDR[1], INVALID_ADDR jz longjmp;
    162 return:
    163 	ret;
    164 
    165 idle_loop_cchan:
    166 	SET_MODE(M_CCHAN, M_CCHAN)
    167 	test	QOFF_CTLSTA, HS_MAILBOX_ACT jz	hs_mailbox_empty;
    168 	mov	LOCAL_HS_MAILBOX, HS_MAILBOX;
    169 	or	QOFF_CTLSTA, HS_MAILBOX_ACT;
    170 hs_mailbox_empty:
    171 BEGIN_CRITICAL;
    172 	test	CCSCBCTL, CCARREN|CCSCBEN jz scbdma_idle;
    173 	test	CCSCBCTL, CCSCBDIR jnz fetch_new_scb_inprog;
    174 	test	CCSCBCTL, CCSCBDONE jz return;
    175 END_CRITICAL;
    176 	/* FALLTHROUGH */
    177 scbdma_tohost_done:
    178 	test	CCSCBCTL, CCARREN jz fill_qoutfifo_dmadone;
    179 	/*
    180 	 * An SCB has been succesfully uploaded to the host.
    181 	 * If the SCB was uploaded for some reason other than
    182 	 * bad SCSI status (currently only for underruns), we
    183 	 * queue the SCB for normal completion.  Otherwise, we
    184 	 * wait until any select-out activity has halted, and
    185 	 * then notify the host so that the transaction can be
    186 	 * dealt with.
    187 	 */
    188 	test	SCB_SCSI_STATUS, 0xff jnz scbdma_notify_host;
    189 	and	CCSCBCTL, ~(CCARREN|CCSCBEN);
    190 	bmov	COMPLETE_DMA_SCB_HEAD, SCB_NEXT_COMPLETE, 2;
    191 	bmov	SCB_NEXT_COMPLETE, COMPLETE_SCB_HEAD, 2;
    192 	bmov	COMPLETE_SCB_HEAD, SCBPTR, 2 ret;
    193 scbdma_notify_host:
    194 	SET_MODE(M_SCSI, M_SCSI)
    195 	test	SCSISEQ0, ENSELO jnz return;
    196 	test	SSTAT0, (SELDO|SELINGO) jnz return;
    197 	SET_MODE(M_CCHAN, M_CCHAN)
    198 	/*
    199 	 * Remove SCB and notify host.
    200 	 */
    201 	and	CCSCBCTL, ~(CCARREN|CCSCBEN);
    202 	bmov	COMPLETE_DMA_SCB_HEAD, SCB_NEXT_COMPLETE, 2;
    203 	SET_SEQINTCODE(BAD_SCB_STATUS)
    204 	ret;
    205 fill_qoutfifo_dmadone:
    206 	and	CCSCBCTL, ~(CCARREN|CCSCBEN);
    207 	call	qoutfifo_updated;
    208 	mvi	COMPLETE_SCB_DMAINPROG_HEAD[1], SCB_LIST_NULL;
    209 	bmov	QOUTFIFO_NEXT_ADDR, SCBHADDR, 4;
    210 	test	QOFF_CTLSTA, SDSCB_ROLLOVR jz return;
    211 	bmov	QOUTFIFO_NEXT_ADDR, SHARED_DATA_ADDR, 4;
    212 	xor	QOUTFIFO_ENTRY_VALID_TAG, QOUTFIFO_ENTRY_VALID_TOGGLE ret;
    213 
    214 qoutfifo_updated:
    215 	/*
    216 	 * If there are more commands waiting to be DMA'ed
    217 	 * to the host, always coalesce.  Otherwise honor the
    218 	 * host's wishes.
    219 	 */
    220 	cmp	COMPLETE_DMA_SCB_HEAD[1], SCB_LIST_NULL jne coalesce_by_count;
    221 	cmp	COMPLETE_SCB_HEAD[1], SCB_LIST_NULL jne coalesce_by_count;
    222 	test	LOCAL_HS_MAILBOX, ENINT_COALESCE jz issue_cmdcmplt;
    223 
    224 	/*
    225 	 * If we have relatively few commands outstanding, don't
    226 	 * bother waiting for another command to complete.
    227 	 */
    228 	test	CMDS_PENDING[1], 0xFF jnz coalesce_by_count;
    229 	/* Add -1 so that jnc means <= not just < */
    230 	add	A, -1, INT_COALESCING_MINCMDS;
    231 	add	NONE, A, CMDS_PENDING;
    232 	jnc	issue_cmdcmplt;
    233 
    234 	/*
    235 	 * If coalescing, only coalesce up to the limit
    236 	 * provided by the host driver.
    237 	 */
    238 coalesce_by_count:
    239 	mov	A, INT_COALESCING_MAXCMDS;
    240 	add	NONE, A, INT_COALESCING_CMDCOUNT;
    241 	jc	issue_cmdcmplt;
    242 	/*
    243 	 * If the timer is not currently active,
    244 	 * fire it up.
    245 	 */
    246 	test	INTCTL, SWTMINTMASK jz return;
    247 	bmov	SWTIMER, INT_COALESCING_TIMER, 2;
    248 	mvi	CLRSEQINTSTAT, CLRSEQ_SWTMRTO;
    249 	or	INTCTL, SWTMINTEN|SWTIMER_START;
    250 	and	INTCTL, ~SWTMINTMASK ret;
    251 
    252 issue_cmdcmplt:
    253 	mvi	INTSTAT, CMDCMPLT;
    254 	clr	INT_COALESCING_CMDCOUNT;
    255 	or	INTCTL, SWTMINTMASK ret;
    256 
    257 BEGIN_CRITICAL;
    258 fetch_new_scb_inprog:
    259 	test	CCSCBCTL, ARRDONE jz return;
    260 fetch_new_scb_done:
    261 	and	CCSCBCTL, ~(CCARREN|CCSCBEN);
    262 	bmov	REG0, SCBPTR, 2;
    263 	clr	A;
    264 	add	CMDS_PENDING, 1;
    265 	adc	CMDS_PENDING[1], A;
    266 	/*
    267 	 * The FIFO use count field is shared with the
    268 	 * tag set by the host so that our SCB dma engine
    269 	 * knows the correct location to store the SCB.
    270 	 * Set it to zero before processing the SCB.
    271 	 */
    272 	mov	SCB_FIFO_USE_COUNT, ALLZEROS;
    273 	/* Update the next SCB address to download. */
    274 	bmov	NEXT_QUEUED_SCB_ADDR, SCB_NEXT_SCB_BUSADDR, 4;
    275 	mvi	SCB_NEXT[1], SCB_LIST_NULL;
    276 	mvi	SCB_NEXT2[1], SCB_LIST_NULL;
    277 	/* Increment our position in the QINFIFO. */
    278 	mov	NONE, SNSCB_QOFF;
    279 	/*
    280 	 * SCBs that want to send messages are always
    281 	 * queued independently.  This ensures that they
    282 	 * are at the head of the SCB list to select out
    283 	 * to a target and we will see the MK_MESSAGE flag.
    284 	 */
    285 	test	SCB_CONTROL, MK_MESSAGE jnz first_new_target_scb;
    286 	shr	SINDEX, 3, SCB_SCSIID;
    287 	and	SINDEX, ~0x1;
    288 	mvi	SINDEX[1], (WAITING_SCB_TAILS >> 8);
    289 	bmov	DINDEX, SINDEX, 2;
    290 	bmov	SCBPTR, SINDIR, 2;
    291 	bmov	DINDIR, REG0, 2;
    292 	cmp	SCBPTR[1], SCB_LIST_NULL je first_new_target_scb;
    293 	bmov	SCB_NEXT, REG0, 2 ret;
    294 first_new_target_scb:
    295 	cmp	WAITING_TID_HEAD[1], SCB_LIST_NULL je first_new_scb;
    296 	bmov	SCBPTR, WAITING_TID_TAIL, 2;
    297 	bmov	SCB_NEXT2, REG0, 2;
    298 	bmov	WAITING_TID_TAIL, REG0, 2 ret;
    299 first_new_scb:
    300 	bmov	WAITING_TID_HEAD, REG0, 2;
    301 	bmov	WAITING_TID_TAIL, REG0, 2 ret;
    302 END_CRITICAL;
    303 
    304 scbdma_idle:
    305 	/*
    306 	 * Give precedence to downloading new SCBs to execute
    307 	 * unless select-outs are currently frozen.
    308 	 */
    309 	test	SEQ_FLAGS2, SELECTOUT_QFROZEN jnz . + 2;
    310 BEGIN_CRITICAL;
    311 	test	QOFF_CTLSTA, NEW_SCB_AVAIL jnz fetch_new_scb;
    312 	cmp	COMPLETE_DMA_SCB_HEAD[1], SCB_LIST_NULL jne dma_complete_scb;
    313 	cmp	COMPLETE_SCB_HEAD[1], SCB_LIST_NULL je return;
    314 	/* FALLTHROUGH */
    315 fill_qoutfifo:
    316 	/*
    317 	 * Keep track of the SCBs we are DMA'ing just
    318 	 * in case the DMA fails or is aborted.
    319 	 */
    320 	mov	A, QOUTFIFO_ENTRY_VALID_TAG;
    321 	bmov	COMPLETE_SCB_DMAINPROG_HEAD, COMPLETE_SCB_HEAD, 2;
    322 	mvi	CCSCBCTL, CCSCBRESET;
    323 	bmov	SCBHADDR, QOUTFIFO_NEXT_ADDR, 4;
    324 	bmov	SCBPTR, COMPLETE_SCB_HEAD, 2;
    325 fill_qoutfifo_loop:
    326 	mov	CCSCBRAM, SCBPTR;
    327 	or	CCSCBRAM, A, SCBPTR[1];
    328 	mov	NONE, SDSCB_QOFF;
    329 	inc	INT_COALESCING_CMDCOUNT;
    330 	add	CMDS_PENDING, -1;
    331 	adc	CMDS_PENDING[1], -1;
    332 	cmp	SCB_NEXT_COMPLETE[1], SCB_LIST_NULL je fill_qoutfifo_done;
    333 	cmp	CCSCBADDR, CCSCBADDR_MAX je fill_qoutfifo_done;
    334 	test	QOFF_CTLSTA, SDSCB_ROLLOVR jnz fill_qoutfifo_done;
    335 	bmov	SCBPTR, SCB_NEXT_COMPLETE, 2;
    336 	jmp	fill_qoutfifo_loop;
    337 fill_qoutfifo_done:
    338 	mov	SCBHCNT, CCSCBADDR;
    339 	mvi	CCSCBCTL, CCSCBEN|CCSCBRESET;
    340 	bmov	COMPLETE_SCB_HEAD, SCB_NEXT_COMPLETE, 2;
    341 	mvi	SCB_NEXT_COMPLETE[1], SCB_LIST_NULL ret;
    342 
    343 fetch_new_scb:
    344 	bmov	SCBHADDR, NEXT_QUEUED_SCB_ADDR, 4;
    345 	mvi	CCARREN|CCSCBEN|CCSCBDIR|CCSCBRESET jmp dma_scb;
    346 dma_complete_scb:
    347 	bmov	SCBPTR, COMPLETE_DMA_SCB_HEAD, 2;
    348 	bmov	SCBHADDR, SCB_BUSADDR, 4;
    349 	mvi	CCARREN|CCSCBEN|CCSCBRESET jmp dma_scb;
    350 END_CRITICAL;
    351 
    352 /*
    353  * Either post or fetch an SCB from host memory.  The caller
    354  * is responsible for polling for transfer completion.
    355  *
    356  * Prerequisits: Mode == M_CCHAN
    357  *		 SINDEX contains CCSCBCTL flags
    358  *		 SCBHADDR set to Host SCB address
    359  *		 SCBPTR set to SCB src location on "push" operations
    360  */
    361 SET_SRC_MODE	M_CCHAN;
    362 SET_DST_MODE	M_CCHAN;
    363 dma_scb:
    364 	mvi	SCBHCNT, SCB_TRANSFER_SIZE;
    365 	mov	CCSCBCTL, SINDEX ret;
    366 
    367 BEGIN_CRITICAL;
    368 setjmp:
    369 	bmov	LONGJMP_ADDR, STACK, 2 ret;
    370 setjmp_inline:
    371 	bmov	LONGJMP_ADDR, STACK, 2;
    372 longjmp:
    373 	bmov	STACK, LONGJMP_ADDR, 2 ret;
    374 END_CRITICAL;
    375 
    376 /*************************** Chip Bug Work Arounds ****************************/
    377 /*
    378  * Must disable interrupts when setting the mode pointer
    379  * register as an interrupt occurring mid update will
    380  * fail to store the new mode value for restoration on
    381  * an iret.
    382  */
    383 if ((ahd->bugs & AHD_SET_MODE_BUG) != 0) {
    384 set_mode_work_around:
    385 	mvi	SEQINTCTL, INTVEC1DSL;
    386 	mov	MODE_PTR, SINDEX;
    387 	clr	SEQINTCTL ret;
    388 
    389 toggle_dff_mode_work_around:
    390 	mvi	SEQINTCTL, INTVEC1DSL;
    391 	xor	MODE_PTR, MK_MODE(M_DFF1, M_DFF1);
    392 	clr	SEQINTCTL ret;
    393 }
    394 
    395 
    396 if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
    397 set_seqint_work_around:
    398 	mov	SEQINTCODE, SINDEX;
    399 	mvi	SEQINTCODE, NO_SEQINT ret;
    400 }
    401 
    402 /************************ Packetized LongJmp Routines *************************/
    403 SET_SRC_MODE	M_SCSI;
    404 SET_DST_MODE	M_SCSI;
    405 start_selection:
    406 BEGIN_CRITICAL;
    407 	if ((ahd->bugs & AHD_SENT_SCB_UPDATE_BUG) != 0) {
    408 		/*
    409 		 * Razor #494
    410 		 * Rev A hardware fails to update LAST/CURR/NEXTSCB
    411 		 * correctly after a packetized selection in several
    412 		 * situations:
    413 		 *
    414 		 * 1) If only one command existed in the queue, the
    415 		 *    LAST/CURR/NEXTSCB are unchanged.
    416 		 *
    417 		 * 2) In a non QAS, protocol allowed phase change,
    418 		 *    the queue is shifted 1 too far.  LASTSCB is
    419 		 *    the last SCB that was correctly processed.
    420 		 *
    421 		 * 3) In the QAS case, if the full list of commands
    422 		 *    was successfully sent, NEXTSCB is NULL and neither
    423 		 *    CURRSCB nor LASTSCB can be trusted.  We must
    424 		 *    manually walk the list counting MAXCMDCNT elements
    425 		 *    to find the last SCB that was sent correctly.
    426 		 *
    427 		 * To simplify the workaround for this bug in SELDO
    428 		 * handling, we initialize LASTSCB prior to enabling
    429 		 * selection so we can rely on it even for case #1 above.
    430 		 */
    431 		bmov	LASTSCB, WAITING_TID_HEAD, 2;
    432 	}
    433 	bmov	CURRSCB, WAITING_TID_HEAD, 2;
    434 	bmov	SCBPTR, WAITING_TID_HEAD, 2;
    435 	shr	SELOID, 4, SCB_SCSIID;
    436 	/*
    437 	 * If we want to send a message to the device, ensure
    438 	 * we are selecting with atn irregardless of our packetized
    439 	 * agreement.  Since SPI4 only allows target reset or PPR
    440 	 * messages if this is a packetized connection, the change
    441 	 * to our negotiation table entry for this selection will
    442 	 * be cleared when the message is acted on.
    443 	 */
    444 	test	SCB_CONTROL, MK_MESSAGE jz . + 3;
    445 	mov	NEGOADDR, SELOID;
    446 	or	NEGCONOPTS, ENAUTOATNO;
    447 	or	SCSISEQ0, ENSELO ret;
    448 END_CRITICAL;
    449 
    450 /*
    451  * Allocate a FIFO for a non-packetized transaction.
    452  * In RevA hardware, both FIFOs must be free before we
    453  * can allocate a FIFO for a non-packetized transaction.
    454  */
    455 allocate_fifo_loop:
    456 	/*
    457 	 * Do whatever work is required to free a FIFO.
    458 	 */
    459 	call	idle_loop_service_fifos;
    460 	SET_MODE(M_SCSI, M_SCSI)
    461 allocate_fifo:
    462 	if ((ahd->bugs & AHD_NONPACKFIFO_BUG) != 0) {
    463 		and	A, FIFO0FREE|FIFO1FREE, DFFSTAT;
    464 		cmp	A, FIFO0FREE|FIFO1FREE jne allocate_fifo_loop;
    465 	} else {
    466 		test	DFFSTAT, FIFO1FREE jnz allocate_fifo1;
    467 		test	DFFSTAT, FIFO0FREE jz allocate_fifo_loop;
    468 		mvi	DFFSTAT, B_CURRFIFO_0;
    469 		SET_MODE(M_DFF0, M_DFF0)
    470 		bmov	SCBPTR, ALLOCFIFO_SCBPTR, 2 ret;
    471 	}
    472 SET_SRC_MODE	M_SCSI;
    473 SET_DST_MODE	M_SCSI;
    474 allocate_fifo1:
    475 	mvi	DFFSTAT, CURRFIFO_1;
    476 	SET_MODE(M_DFF1, M_DFF1)
    477 	bmov	SCBPTR, ALLOCFIFO_SCBPTR, 2 ret;
    478 
    479 /*
    480  * We have been reselected as an initiator
    481  * or selected as a target.
    482  */
    483 SET_SRC_MODE	M_SCSI;
    484 SET_DST_MODE	M_SCSI;
    485 select_in:
    486 	if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
    487 		/*
    488 		 * This exposes a window whereby a
    489 		 * busfree just after a selection will
    490 		 * be missed, but there is no other safe
    491 		 * way to enable busfree detection if
    492 		 * the busfreerev function is broken.
    493 		 */
    494 		mvi	CLRSINT1,CLRBUSFREE;
    495 		or	SIMODE1, ENBUSFREE;
    496 	}
    497 	or	SXFRCTL0, SPIOEN;
    498 	and	SAVED_SCSIID, SELID_MASK, SELID;
    499 	and	A, OID, IOWNID;
    500 	or	SAVED_SCSIID, A;
    501 	mvi	CLRSINT0, CLRSELDI;
    502 	jmp	ITloop;
    503 
    504 /*
    505  * We have successfully selected out.
    506  *
    507  * Clear SELDO.
    508  * Dequeue all SCBs sent from the waiting queue
    509  * Requeue all SCBs *not* sent to the tail of the waiting queue
    510  * Take Razor #494 into account for above.
    511  *
    512  * In Packetized Mode:
    513  *	Return to the idle loop.  Our interrupt handler will take
    514  *	care of any incoming L_Qs.
    515  *
    516  * In Non-Packetize Mode:
    517  *	Continue to our normal state machine.
    518  */
    519 SET_SRC_MODE	M_SCSI;
    520 SET_DST_MODE	M_SCSI;
    521 select_out:
    522 BEGIN_CRITICAL;
    523 	/* Clear out all SCBs that have been successfully sent. */
    524 	if ((ahd->bugs & AHD_SENT_SCB_UPDATE_BUG) != 0) {
    525 		/*
    526 		 * For packetized, the LQO manager clears ENSELO on
    527 		 * the assertion of SELDO.  If we are non-packetized,
    528 		 * LASTSCB and CURRSCB are accurate.
    529 		 */
    530 		test	SCSISEQ0, ENSELO jnz use_lastscb;
    531 
    532 		/*
    533 		 * The update is correct for LQOSTAT1 errors.  All
    534 		 * but LQOBUSFREE are handled by kernel interrupts.
    535 		 * If we see LQOBUSFREE, return to the idle loop.
    536 		 * Once we are out of the select_out critical section,
    537 		 * the kernel will cleanup the LQOBUSFREE and we will
    538 		 * eventually restart the selection if appropriate.
    539 		 */
    540 		test	LQOSTAT1, LQOBUSFREE jnz idle_loop;
    541 
    542 		/*
    543 		 * On a phase change oustside of packet boundaries,
    544 		 * LASTSCB points to the currently active SCB context
    545 		 * on the bus.
    546 		 */
    547 		test	LQOSTAT2, LQOPHACHGOUTPKT jnz use_lastscb;
    548 
    549 		/*
    550 		 * If the hardware has traversed the whole list, NEXTSCB
    551 		 * will be NULL, CURRSCB and LASTSCB cannot be trusted,
    552 		 * but MAXCMDCNT is accurate.  If we stop part way through
    553 		 * the list or only had one command to issue, NEXTSCB[1] is
    554 		 * not NULL and LASTSCB is the last command to go out.
    555 		 */
    556 		cmp	NEXTSCB[1], SCB_LIST_NULL jne use_lastscb;
    557 
    558 		/*
    559 		 * Brute force walk.
    560 		 */
    561 		bmov	SCBPTR, WAITING_TID_HEAD, 2;
    562 		mvi	SEQINTCTL, INTVEC1DSL;
    563 		mvi	MODE_PTR, MK_MODE(M_CFG, M_CFG);
    564 		mov	A, MAXCMDCNT;
    565 		mvi	MODE_PTR, MK_MODE(M_SCSI, M_SCSI);
    566 		clr	SEQINTCTL;
    567 find_lastscb_loop:
    568 		dec	A;
    569 		test	A, 0xFF jz found_last_sent_scb;
    570 		bmov	SCBPTR, SCB_NEXT, 2;
    571 		jmp	find_lastscb_loop;
    572 use_lastscb:
    573 		bmov	SCBPTR, LASTSCB, 2;
    574 found_last_sent_scb:
    575 		bmov	CURRSCB, SCBPTR, 2;
    576 curscb_ww_done:
    577 	} else {
    578 		/*
    579 		 * Untested - Verify with Rev B.
    580 		 */
    581 		bmov	SCBPTR, CURRSCB, 2;
    582 	}
    583 
    584 	/*
    585 	 * Requeue any SCBs not sent, to the tail of the waiting Q.
    586 	 */
    587 	cmp	SCB_NEXT[1], SCB_LIST_NULL je select_out_list_done;
    588 
    589 	/*
    590 	 * We know that neither the per-TID list nor the list of
    591 	 * TIDs is empty.  Use this knowledge to our advantage.
    592 	 */
    593 	bmov	REG0, SCB_NEXT, 2;
    594 	bmov	SCBPTR, WAITING_TID_TAIL, 2;
    595 	bmov	SCB_NEXT2, REG0, 2;
    596 	bmov	WAITING_TID_TAIL, REG0, 2;
    597 	jmp	select_out_inc_tid_q;
    598 
    599 select_out_list_done:
    600 	/*
    601 	 * The whole list made it.  Just clear our TID's tail pointer
    602 	 * unless we were queued independently due to our need to
    603 	 * send a message.
    604 	 */
    605 	test	SCB_CONTROL, MK_MESSAGE jnz select_out_inc_tid_q;
    606 	shr	DINDEX, 3, SCB_SCSIID;
    607 	or	DINDEX, 1;	/* Want only the second byte */
    608 	mvi	DINDEX[1], ((WAITING_SCB_TAILS) >> 8);
    609 	mvi	DINDIR, SCB_LIST_NULL;
    610 select_out_inc_tid_q:
    611 	bmov	SCBPTR, WAITING_TID_HEAD, 2;
    612 	bmov	WAITING_TID_HEAD, SCB_NEXT2, 2;
    613 	cmp	WAITING_TID_HEAD[1], SCB_LIST_NULL jne . + 2;
    614 	mvi	WAITING_TID_TAIL[1], SCB_LIST_NULL;
    615 	bmov	SCBPTR, CURRSCB, 2;
    616 	mvi	CLRSINT0, CLRSELDO;
    617 	test	LQOSTAT2, LQOPHACHGOUTPKT jnz unexpected_nonpkt_phase;
    618 	test	LQOSTAT1, LQOPHACHGINPKT jnz unexpected_nonpkt_phase;
    619 
    620 	/*
    621 	 * If this is a packetized connection, return to our
    622 	 * idle_loop and let our interrupt handler deal with
    623 	 * any connection setup/teardown issues.  The only
    624 	 * exceptions are the case of MK_MESSAGE and task management
    625 	 * SCBs.
    626 	 */
    627 	if ((ahd->bugs & AHD_LQO_ATNO_BUG) != 0) {
    628 		/*
    629 		 * In the A, the LQO manager transitions to LQOSTOP0 even if
    630 		 * we have selected out with ATN asserted and the target
    631 		 * REQs in a non-packet phase.
    632 		 */
    633 		test 	SCB_CONTROL, MK_MESSAGE jz select_out_no_message;
    634 		test	SCSISIGO, ATNO jnz select_out_non_packetized;
    635 select_out_no_message:
    636 	}
    637 	test	LQOSTAT2, LQOSTOP0 jz select_out_non_packetized;
    638 	test	SCB_TASK_MANAGEMENT, 0xFF jz idle_loop;
    639 	SET_SEQINTCODE(TASKMGMT_FUNC_COMPLETE)
    640 	jmp	idle_loop;
    641 
    642 select_out_non_packetized:
    643 	/* Non packetized request. */
    644 	and     SCSISEQ0, ~ENSELO;
    645 	if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
    646 		/*
    647 		 * This exposes a window whereby a
    648 		 * busfree just after a selection will
    649 		 * be missed, but there is no other safe
    650 		 * way to enable busfree detection if
    651 		 * the busfreerev function is broken.
    652 		 */
    653 		mvi	CLRSINT1,CLRBUSFREE;
    654 		or	SIMODE1, ENBUSFREE;
    655 	}
    656 	mov	SAVED_SCSIID, SCB_SCSIID;
    657 	mov	SAVED_LUN, SCB_LUN;
    658 	mvi	SEQ_FLAGS, NO_CDB_SENT;
    659 END_CRITICAL;
    660 	or	SXFRCTL0, SPIOEN;
    661 
    662 	/*
    663 	 * As soon as we get a successful selection, the target
    664 	 * should go into the message out phase since we have ATN
    665 	 * asserted.
    666 	 */
    667 	mvi	MSG_OUT, MSG_IDENTIFYFLAG;
    668 
    669 	/*
    670 	 * Main loop for information transfer phases.  Wait for the
    671 	 * target to assert REQ before checking MSG, C/D and I/O for
    672 	 * the bus phase.
    673 	 */
    674 mesgin_phasemis:
    675 ITloop:
    676 	call	phase_lock;
    677 
    678 	mov	A, LASTPHASE;
    679 
    680 	test	A, ~P_DATAIN_DT	jz p_data;
    681 	cmp	A,P_COMMAND	je p_command;
    682 	cmp	A,P_MESGOUT	je p_mesgout;
    683 	cmp	A,P_STATUS	je p_status;
    684 	cmp	A,P_MESGIN	je p_mesgin;
    685 
    686 	SET_SEQINTCODE(BAD_PHASE)
    687 	jmp	ITloop;			/* Try reading the bus again. */
    688 
    689 /*
    690  * Command phase.  Set up the DMA registers and let 'er rip.
    691  */
    692 p_command:
    693 	test	SEQ_FLAGS, NOT_IDENTIFIED jz p_command_okay;
    694 	SET_SEQINTCODE(PROTO_VIOLATION)
    695 p_command_okay:
    696 	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
    697 		jnz p_command_allocate_fifo;
    698 	/*
    699 	 * Command retry.  Free our current FIFO and
    700 	 * re-allocate a FIFO so transfer state is
    701 	 * reset.
    702 	 */
    703 SET_SRC_MODE	M_DFF1;
    704 SET_DST_MODE	M_DFF1;
    705 	mvi	DFFSXFRCTL, RSTCHN|CLRSHCNT;
    706 	SET_MODE(M_SCSI, M_SCSI)
    707 p_command_allocate_fifo:
    708 	bmov	ALLOCFIFO_SCBPTR, SCBPTR, 2;
    709 	call	allocate_fifo;
    710 SET_SRC_MODE	M_DFF1;
    711 SET_DST_MODE	M_DFF1;
    712 	add	NONE, -17, SCB_CDB_LEN;
    713 	jnc	p_command_embedded;
    714 p_command_from_host:
    715 	bmov	HADDR[0], SCB_HOST_CDB_PTR, 9;
    716 	mvi	SG_CACHE_PRE, LAST_SEG;
    717 	mvi	DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN);
    718 	jmp	p_command_xfer;
    719 p_command_embedded:
    720 	bmov	SHCNT[0], SCB_CDB_LEN,  1;
    721 	bmov	DFDAT, SCB_CDB_STORE, 16;
    722 	mvi	DFCNTRL, SCSIEN;
    723 p_command_xfer:
    724 	and	SEQ_FLAGS, ~NO_CDB_SENT;
    725 	test	DFCNTRL, SCSIEN jnz .;
    726 	/*
    727 	 * DMA Channel automatically disabled.
    728 	 * Don't allow a data phase if the command
    729 	 * was not fully transferred.
    730 	 */
    731 	test	SSTAT2, SDONE jnz ITloop;
    732 	or	SEQ_FLAGS, NO_CDB_SENT;
    733 	jmp	ITloop;
    734 
    735 
    736 /*
    737  * Status phase.  Wait for the data byte to appear, then read it
    738  * and store it into the SCB.
    739  */
    740 SET_SRC_MODE	M_SCSI;
    741 SET_DST_MODE	M_SCSI;
    742 p_status:
    743 	test	SEQ_FLAGS,NOT_IDENTIFIED jnz mesgin_proto_violation;
    744 p_status_okay:
    745 	mov	SCB_SCSI_STATUS, SCSIDAT;
    746 	or	SCB_CONTROL, STATUS_RCVD;
    747 	jmp	ITloop;
    748 
    749 /*
    750  * Message out phase.  If MSG_OUT is MSG_IDENTIFYFLAG, build a full
    751  * indentify message sequence and send it to the target.  The host may
    752  * override this behavior by setting the MK_MESSAGE bit in the SCB
    753  * control byte.  This will cause us to interrupt the host and allow
    754  * it to handle the message phase completely on its own.  If the bit
    755  * associated with this target is set, we will also interrupt the host,
    756  * thereby allowing it to send a message on the next selection regardless
    757  * of the transaction being sent.
    758  *
    759  * If MSG_OUT is == HOST_MSG, also interrupt the host and take a message.
    760  * This is done to allow the host to send messages outside of an identify
    761  * sequence while protecting the seqencer from testing the MK_MESSAGE bit
    762  * on an SCB that might not be for the current nexus. (For example, a
    763  * BDR message in responce to a bad reselection would leave us pointed to
    764  * an SCB that doesn't have anything to do with the current target).
    765  *
    766  * Otherwise, treat MSG_OUT as a 1 byte message to send (abort, abort tag,
    767  * bus device reset).
    768  *
    769  * When there are no messages to send, MSG_OUT should be set to MSG_NOOP,
    770  * in case the target decides to put us in this phase for some strange
    771  * reason.
    772  */
    773 p_mesgout_retry:
    774 	/* Turn on ATN for the retry */
    775 	mvi	SCSISIGO, ATNO;
    776 p_mesgout:
    777 	mov	SINDEX, MSG_OUT;
    778 	cmp	SINDEX, MSG_IDENTIFYFLAG jne p_mesgout_from_host;
    779 	test	SCB_CONTROL,MK_MESSAGE	jnz host_message_loop;
    780 p_mesgout_identify:
    781 	or	SINDEX, MSG_IDENTIFYFLAG|DISCENB, SCB_LUN;
    782 	test	SCB_CONTROL, DISCENB jnz . + 2;
    783 	and	SINDEX, ~DISCENB;
    784 /*
    785  * Send a tag message if TAG_ENB is set in the SCB control block.
    786  * Use SCB_NONPACKET_TAG as the tag value.
    787  */
    788 p_mesgout_tag:
    789 	test	SCB_CONTROL,TAG_ENB jz  p_mesgout_onebyte;
    790 	mov	SCSIDAT, SINDEX;	/* Send the identify message */
    791 	call	phase_lock;
    792 	cmp	LASTPHASE, P_MESGOUT	jne p_mesgout_done;
    793 	and	SCSIDAT,TAG_ENB|SCB_TAG_TYPE,SCB_CONTROL;
    794 	call	phase_lock;
    795 	cmp	LASTPHASE, P_MESGOUT	jne p_mesgout_done;
    796 	mov	SCBPTR jmp p_mesgout_onebyte;
    797 /*
    798  * Interrupt the driver, and allow it to handle this message
    799  * phase and any required retries.
    800  */
    801 p_mesgout_from_host:
    802 	cmp	SINDEX, HOST_MSG	jne p_mesgout_onebyte;
    803 	jmp	host_message_loop;
    804 
    805 p_mesgout_onebyte:
    806 	mvi	CLRSINT1, CLRATNO;
    807 	mov	SCSIDAT, SINDEX;
    808 
    809 /*
    810  * If the next bus phase after ATN drops is message out, it means
    811  * that the target is requesting that the last message(s) be resent.
    812  */
    813 	call	phase_lock;
    814 	cmp	LASTPHASE, P_MESGOUT	je p_mesgout_retry;
    815 
    816 p_mesgout_done:
    817 	mvi	CLRSINT1,CLRATNO;	/* Be sure to turn ATNO off */
    818 	mov	LAST_MSG, MSG_OUT;
    819 	mvi	MSG_OUT, MSG_NOOP;	/* No message left */
    820 	jmp	ITloop;
    821 
    822 /*
    823  * Message in phase.  Bytes are read using Automatic PIO mode.
    824  */
    825 p_mesgin:
    826 	/* read the 1st message byte */
    827 	mvi	ACCUM		call inb_first;
    828 
    829 	test	A,MSG_IDENTIFYFLAG	jnz mesgin_identify;
    830 	cmp	A,MSG_DISCONNECT	je mesgin_disconnect;
    831 	cmp	A,MSG_SAVEDATAPOINTER	je mesgin_sdptrs;
    832 	cmp	ALLZEROS,A		je mesgin_complete;
    833 	cmp	A,MSG_RESTOREPOINTERS	je mesgin_rdptrs;
    834 	cmp	A,MSG_IGN_WIDE_RESIDUE	je mesgin_ign_wide_residue;
    835 	cmp	A,MSG_NOOP		je mesgin_done;
    836 
    837 /*
    838  * Pushed message loop to allow the kernel to
    839  * run it's own message state engine.  To avoid an
    840  * extra nop instruction after signaling the kernel,
    841  * we perform the phase_lock before checking to see
    842  * if we should exit the loop and skip the phase_lock
    843  * in the ITloop.  Performing back to back phase_locks
    844  * shouldn't hurt, but why do it twice...
    845  */
    846 host_message_loop:
    847 	call	phase_lock;	/* Benign the first time through. */
    848 	SET_SEQINTCODE(HOST_MSG_LOOP)
    849 	cmp	RETURN_1, EXIT_MSG_LOOP	je ITloop;
    850 	cmp	RETURN_1, CONT_MSG_LOOP_WRITE	jne . + 3;
    851 	mov	SCSIDAT, RETURN_2;
    852 	jmp	host_message_loop;
    853 	/* Must be CONT_MSG_LOOP_READ */
    854 	mov	NONE, SCSIDAT;	/* ACK Byte */
    855 	jmp	host_message_loop;
    856 
    857 mesgin_ign_wide_residue:
    858 	mov	SAVED_MODE, MODE_PTR;
    859 	SET_MODE(M_SCSI, M_SCSI)
    860 	shr	NEGOADDR, 4, SAVED_SCSIID;
    861 	mov	A, NEGCONOPTS;
    862 	RESTORE_MODE(SAVED_MODE)
    863 	test	A, WIDEXFER jz mesgin_reject;
    864 	/* Pull the residue byte */
    865 	mvi	REG0	call inb_next;
    866 	cmp	REG0, 0x01 jne mesgin_reject;
    867 	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz . + 2;
    868 	test	DATA_COUNT_ODD, 0x1	jz mesgin_done;
    869 	jmp	mesgin_done;
    870 
    871 mesgin_proto_violation:
    872 	SET_SEQINTCODE(PROTO_VIOLATION)
    873 	jmp	mesgin_done;
    874 mesgin_reject:
    875 	mvi	MSG_MESSAGE_REJECT	call mk_mesg;
    876 mesgin_done:
    877 	mov	NONE,SCSIDAT;		/*dummy read from latch to ACK*/
    878 	jmp	ITloop;
    879 
    880 #define INDEX_DISC_LIST(scsiid, lun)					\
    881 	and	A, 0xC0, scsiid;					\
    882 	or	SCBPTR, A, lun;						\
    883 	clr	SCBPTR[1];						\
    884 	and	SINDEX, 0x30, scsiid;					\
    885 	shr	SINDEX, 3;	/* Multiply by 2 */			\
    886 	add	SINDEX, (SCB_DISCONNECTED_LISTS & 0xFF);		\
    887 	mvi	SINDEX[1], ((SCB_DISCONNECTED_LISTS >> 8) & 0xFF)
    888 
    889 mesgin_identify:
    890 	/*
    891 	 * Determine whether a target is using tagged or non-tagged
    892 	 * transactions by first looking at the transaction stored in
    893 	 * the per-device, disconnected array.  If there is no untagged
    894 	 * transaction for this target, this must be a tagged transaction.
    895 	 */
    896 	and	SAVED_LUN, MSG_IDENTIFY_LUNMASK, A;
    897 	INDEX_DISC_LIST(SAVED_SCSIID, SAVED_LUN);
    898 	bmov	DINDEX, SINDEX, 2;
    899 	bmov	REG0, SINDIR, 2;
    900 	cmp	REG0[1], SCB_LIST_NULL je snoop_tag;
    901 	/* Untagged.  Clear the busy table entry and setup the SCB. */
    902 	bmov	DINDIR, ALLONES, 2;
    903 	bmov	SCBPTR, REG0, 2;
    904 	jmp	setup_SCB;
    905 
    906 /*
    907  * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
    908  * If we get one, we use the tag returned to find the proper
    909  * SCB.  After receiving the tag, look for the SCB at SCB locations tag and
    910  * tag + 256.
    911  */
    912 snoop_tag:
    913 	if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
    914 		or	SEQ_FLAGS, 0x80;
    915 	}
    916 	mov	NONE, SCSIDAT;		/* ACK Identify MSG */
    917 	call	phase_lock;
    918 	if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
    919 		or	SEQ_FLAGS, 0x1;
    920 	}
    921 	cmp	LASTPHASE, P_MESGIN	jne not_found_ITloop;
    922 	if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
    923 		or	SEQ_FLAGS, 0x2;
    924 	}
    925 	cmp	SCSIBUS, MSG_SIMPLE_Q_TAG jne not_found;
    926 get_tag:
    927 	clr	SCBPTR[1];
    928 	mvi	SCBPTR	call inb_next;	/* tag value */
    929 verify_scb:
    930 	test	SCB_CONTROL,DISCONNECTED jz verify_other_scb;
    931 	mov	A, SAVED_SCSIID;
    932 	cmp	SCB_SCSIID, A jne verify_other_scb;
    933 	mov	A, SAVED_LUN;
    934 	cmp	SCB_LUN, A je setup_SCB_disconnected;
    935 verify_other_scb:
    936 	xor	SCBPTR[1], 1;
    937 	test	SCBPTR[1], 0xFF jnz verify_scb;
    938 	jmp	not_found;
    939 
    940 /*
    941  * Ensure that the SCB the tag points to is for
    942  * an SCB transaction to the reconnecting target.
    943  */
    944 setup_SCB:
    945 	if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
    946 		or	SEQ_FLAGS, 0x10;
    947 	}
    948 	test	SCB_CONTROL,DISCONNECTED jz not_found;
    949 setup_SCB_disconnected:
    950 	and	SCB_CONTROL,~DISCONNECTED;
    951 	clr	SEQ_FLAGS;	/* make note of IDENTIFY */
    952 	test	SCB_SGPTR, SG_LIST_NULL jnz . + 3;
    953 	bmov	ALLOCFIFO_SCBPTR, SCBPTR, 2;
    954 	call	allocate_fifo;
    955 	/* See if the host wants to send a message upon reconnection */
    956 	test	SCB_CONTROL, MK_MESSAGE jz mesgin_done;
    957 	mvi	HOST_MSG	call mk_mesg;
    958 	jmp	mesgin_done;
    959 
    960 not_found:
    961 	SET_SEQINTCODE(NO_MATCH)
    962 	jmp	mesgin_done;
    963 
    964 not_found_ITloop:
    965 	SET_SEQINTCODE(NO_MATCH)
    966 	jmp	ITloop;
    967 
    968 /*
    969  * We received a "command complete" message.  Put the SCB on the complete
    970  * queue and trigger a completion interrupt via the idle loop.  Before doing
    971  * so, check to see if there
    972  * is a residual or the status byte is something other than STATUS_GOOD (0).
    973  * In either of these conditions, we upload the SCB back to the host so it can
    974  * process this information.  In the case of a non zero status byte, we
    975  * additionally interrupt the kernel driver synchronously, allowing it to
    976  * decide if sense should be retrieved.  If the kernel driver wishes to request
    977  * sense, it will fill the kernel SCB with a request sense command, requeue
    978  * it to the QINFIFO and tell us not to post to the QOUTFIFO by setting
    979  * RETURN_1 to SEND_SENSE.
    980  */
    981 mesgin_complete:
    982 
    983 	/*
    984 	 * If ATN is raised, we still want to give the target a message.
    985 	 * Perhaps there was a parity error on this last message byte.
    986 	 * Either way, the target should take us to message out phase
    987 	 * and then attempt to complete the command again.  We should use a
    988 	 * critical section here to guard against a timeout triggering
    989 	 * for this command and setting ATN while we are still processing
    990 	 * the completion.
    991 	test	SCSISIGI, ATNI jnz mesgin_done;
    992 	 */
    993 
    994 	/*
    995 	 * If we are identified and have successfully sent the CDB,
    996 	 * any status will do.  Optimize this fast path.
    997 	 */
    998 	test	SCB_CONTROL, STATUS_RCVD jz mesgin_proto_violation;
    999 	test	SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT jz complete_accepted;
   1000 
   1001 	/*
   1002 	 * If the target never sent an identify message but instead went
   1003 	 * to mesgin to give an invalid message, let the host abort us.
   1004 	 */
   1005 	test	SEQ_FLAGS, NOT_IDENTIFIED jnz mesgin_proto_violation;
   1006 
   1007 	/*
   1008 	 * If we recevied good status but never successfully sent the
   1009 	 * cdb, abort the command.
   1010 	 */
   1011 	test	SCB_SCSI_STATUS,0xff	jnz complete_accepted;
   1012 	test	SEQ_FLAGS, NO_CDB_SENT jnz mesgin_proto_violation;
   1013 complete_accepted:
   1014 
   1015 	/*
   1016 	 * See if we attempted to deliver a message but the target ingnored us.
   1017 	 */
   1018 	test	SCB_CONTROL, MK_MESSAGE jz complete_nomsg;
   1019 	SET_SEQINTCODE(MKMSG_FAILED)
   1020 complete_nomsg:
   1021 	call	queue_scb_completion;
   1022 	jmp	await_busfree;
   1023 
   1024 freeze_queue:
   1025 	/* Cancel any pending select-out. */
   1026 	test	SSTAT0, SELDO|SELINGO jnz . + 2;
   1027 	and	SCSISEQ0, ~ENSELO;
   1028 	mov	ACCUM_SAVE, A;
   1029 	clr	A;
   1030 	add	QFREEZE_COUNT, 1;
   1031 	adc	QFREEZE_COUNT[1], A;
   1032 	or	SEQ_FLAGS2, SELECTOUT_QFROZEN;
   1033 	mov	A, ACCUM_SAVE ret;
   1034 
   1035 /*
   1036  * Complete the current FIFO's SCB if data for this same
   1037  * SCB is not transferring in the other FIFO.
   1038  */
   1039 SET_SRC_MODE	M_DFF1;
   1040 SET_DST_MODE	M_DFF1;
   1041 pkt_complete_scb_if_fifos_idle:
   1042 	bmov	ARG_1, SCBPTR, 2;
   1043 	mvi	DFFSXFRCTL, CLRCHN;
   1044 	SET_MODE(M_SCSI, M_SCSI)
   1045 	bmov	SCBPTR, ARG_1, 2;
   1046 	test	SCB_FIFO_USE_COUNT, 0xFF jnz return;
   1047 queue_scb_completion:
   1048 	test	SCB_SCSI_STATUS,0xff	jnz bad_status;
   1049 	/*
   1050 	 * Check for residuals
   1051 	 */
   1052 	test	SCB_SGPTR, SG_LIST_NULL jnz complete;	/* No xfer */
   1053 	test	SCB_SGPTR, SG_FULL_RESID jnz upload_scb;/* Never xfered */
   1054 	test	SCB_RESIDUAL_SGPTR, SG_LIST_NULL jz upload_scb;
   1055 complete:
   1056 	bmov	SCB_NEXT_COMPLETE, COMPLETE_SCB_HEAD, 2;
   1057 	bmov	COMPLETE_SCB_HEAD, SCBPTR, 2 ret;
   1058 bad_status:
   1059 	cmp	SCB_SCSI_STATUS, STATUS_PKT_SENSE je upload_scb;
   1060 	call	freeze_queue;
   1061 upload_scb:
   1062 	/*
   1063 	 * Restore SCB TAG since we reuse this field
   1064 	 * in the sequencer.  We don't want to corrupt
   1065 	 * it on the host.
   1066 	 */
   1067 	bmov	SCB_TAG, SCBPTR, 2;
   1068 	bmov	SCB_NEXT_COMPLETE, COMPLETE_DMA_SCB_HEAD, 2;
   1069 	bmov	COMPLETE_DMA_SCB_HEAD, SCBPTR, 2;
   1070 	or	SCB_SGPTR, SG_STATUS_VALID ret;
   1071 
   1072 /*
   1073  * Is it a disconnect message?  Set a flag in the SCB to remind us
   1074  * and await the bus going free.  If this is an untagged transaction
   1075  * store the SCB id for it in our untagged target table for lookup on
   1076  * a reselction.
   1077  */
   1078 mesgin_disconnect:
   1079 	/*
   1080 	 * If ATN is raised, we still want to give the target a message.
   1081 	 * Perhaps there was a parity error on this last message byte
   1082 	 * or we want to abort this command.  Either way, the target
   1083 	 * should take us to message out phase and then attempt to
   1084 	 * disconnect again.
   1085 	 * XXX - Wait for more testing.
   1086 	test	SCSISIGI, ATNI jnz mesgin_done;
   1087 	 */
   1088 	test	SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT
   1089 		jnz mesgin_proto_violation;
   1090 	or	SCB_CONTROL,DISCONNECTED;
   1091 	test	SCB_CONTROL, TAG_ENB jnz await_busfree;
   1092 queue_disc_scb:
   1093 	bmov	REG0, SCBPTR, 2;
   1094 	INDEX_DISC_LIST(SAVED_SCSIID, SAVED_LUN);
   1095 	bmov	DINDEX, SINDEX, 2;
   1096 	bmov	DINDIR, REG0, 2;
   1097 	bmov	SCBPTR, REG0, 2;
   1098 	/* FALLTHROUGH */
   1099 await_busfree:
   1100 	and	SIMODE1, ~ENBUSFREE;
   1101 	if ((ahd->bugs & AHD_BUSFREEREV_BUG) == 0) {
   1102 		/*
   1103 		 * In the BUSFREEREV_BUG case, the
   1104 		 * busfree status was cleared at the
   1105 		 * beginning of the connection.
   1106 		 */
   1107 		mvi	CLRSINT1,CLRBUSFREE;
   1108 	}
   1109 	mov	NONE, SCSIDAT;		/* Ack the last byte */
   1110 	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
   1111 		jnz await_busfree_not_m_dff;
   1112 SET_SRC_MODE	M_DFF1;
   1113 SET_DST_MODE	M_DFF1;
   1114 await_busfree_clrchn:
   1115 	mvi	DFFSXFRCTL, CLRCHN;
   1116 await_busfree_not_m_dff:
   1117 	call	clear_target_state;
   1118 	test	SSTAT1,REQINIT|BUSFREE	jz .;
   1119 	test	SSTAT1, BUSFREE jnz idle_loop;
   1120 	SET_SEQINTCODE(MISSED_BUSFREE)
   1121 
   1122 
   1123 /*
   1124  * Save data pointers message:
   1125  * Copying RAM values back to SCB, for Save Data Pointers message, but
   1126  * only if we've actually been into a data phase to change them.  This
   1127  * protects against bogus data in scratch ram and the residual counts
   1128  * since they are only initialized when we go into data_in or data_out.
   1129  * Ack the message as soon as possible.
   1130  */
   1131 SET_SRC_MODE	M_DFF1;
   1132 SET_DST_MODE	M_DFF1;
   1133 mesgin_sdptrs:
   1134 	mov	NONE,SCSIDAT;		/*dummy read from latch to ACK*/
   1135 	test	SEQ_FLAGS, DPHASE	jz ITloop;
   1136 	call	save_pointers;
   1137 	jmp	ITloop;
   1138 
   1139 save_pointers:
   1140 	/*
   1141 	 * If we are asked to save our position at the end of the
   1142 	 * transfer, just mark us at the end rather than perform a
   1143 	 * full save.
   1144 	 */
   1145 	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz save_pointers_full;
   1146 	or	SCB_SGPTR, SG_LIST_NULL ret;
   1147 
   1148 save_pointers_full:
   1149 	/*
   1150 	 * The SCB_DATAPTR becomes the current SHADDR.
   1151 	 * All other information comes directly from our residual
   1152 	 * state.
   1153 	 */
   1154 	bmov	SCB_DATAPTR, SHADDR, 8;
   1155 	bmov	SCB_DATACNT, SCB_RESIDUAL_DATACNT, 8 ret;
   1156 
   1157 /*
   1158  * Restore pointers message?  Data pointers are recopied from the
   1159  * SCB anytime we enter a data phase for the first time, so all
   1160  * we need to do is clear the DPHASE flag and let the data phase
   1161  * code do the rest.  We also reset/reallocate the FIFO to make
   1162  * sure we have a clean start for the next data or command phase.
   1163  */
   1164 mesgin_rdptrs:
   1165 	and	SEQ_FLAGS, ~DPHASE;
   1166 	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1)) jnz msgin_rdptrs_get_fifo;
   1167 	mvi	DFFSXFRCTL, RSTCHN|CLRSHCNT;
   1168 	SET_MODE(M_SCSI, M_SCSI)
   1169 msgin_rdptrs_get_fifo:
   1170 	call	allocate_fifo;
   1171 	jmp	mesgin_done;
   1172 
   1173 clear_target_state:
   1174 	mvi	LASTPHASE, P_BUSFREE;
   1175 	/* clear target specific flags */
   1176 	mvi	SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT ret;
   1177 
   1178 phase_lock:
   1179 	if ((ahd->bugs & AHD_EARLY_REQ_BUG) != 0) {
   1180 		/*
   1181 		 * Don't ignore persistent REQ assertions just because
   1182 		 * they were asserted within the bus settle delay window.
   1183 		 * This allows us to tolerate devices like the GEM318
   1184 		 * that violate the SCSI spec.  We are careful not to
   1185 		 * count REQ while we are waiting for it to fall during
   1186 		 * an async phase due to our asserted ACK.  Each
   1187 		 * sequencer instruction takes ~25ns, so the REQ must
   1188 		 * last at least 100ns in order to be counted as a true
   1189 		 * REQ.
   1190 		 */
   1191 		test	SCSIPHASE, 0xFF jnz phase_locked;
   1192 		test	SCSISIGI, ACKI jnz phase_lock;
   1193 		test	SCSISIGI, REQI jz phase_lock;
   1194 		test	SCSIPHASE, 0xFF jnz phase_locked;
   1195 		test	SCSISIGI, ACKI jnz phase_lock;
   1196 		test	SCSISIGI, REQI jz phase_lock;
   1197 phase_locked:
   1198 	} else {
   1199 		test	SCSIPHASE, 0xFF jz .;
   1200 	}
   1201 	test	SSTAT1, SCSIPERR jnz phase_lock;
   1202 phase_lock_latch_phase:
   1203 	and	LASTPHASE, PHASE_MASK, SCSISIGI ret;
   1204 
   1205 /*
   1206  * Functions to read data in Automatic PIO mode.
   1207  *
   1208  * An ACK is not sent on input from the target until SCSIDATL is read from.
   1209  * So we wait until SCSIDATL is latched (the usual way), then read the data
   1210  * byte directly off the bus using SCSIBUSL.  When we have pulled the ATN
   1211  * line, or we just want to acknowledge the byte, then we do a dummy read
   1212  * from SCISDATL.  The SCSI spec guarantees that the target will hold the
   1213  * data byte on the bus until we send our ACK.
   1214  *
   1215  * The assumption here is that these are called in a particular sequence,
   1216  * and that REQ is already set when inb_first is called.  inb_{first,next}
   1217  * use the same calling convention as inb.
   1218  */
   1219 inb_next:
   1220 	mov	NONE,SCSIDAT;		/*dummy read from latch to ACK*/
   1221 inb_next_wait:
   1222 	/*
   1223 	 * If there is a parity error, wait for the kernel to
   1224 	 * see the interrupt and prepare our message response
   1225 	 * before continuing.
   1226 	 */
   1227 	test	SCSIPHASE, 0xFF jz .;
   1228 	test	SSTAT1, SCSIPERR jnz inb_next_wait;
   1229 inb_next_check_phase:
   1230 	and	LASTPHASE, PHASE_MASK, SCSISIGI;
   1231 	cmp	LASTPHASE, P_MESGIN jne mesgin_phasemis;
   1232 inb_first:
   1233 	clr	DINDEX[1];
   1234 	mov	DINDEX,SINDEX;
   1235 	mov	DINDIR,SCSIBUS	ret;		/*read byte directly from bus*/
   1236 inb_last:
   1237 	mov	NONE,SCSIDAT ret;		/*dummy read from latch to ACK*/
   1238 
   1239 mk_mesg:
   1240 	mvi	SCSISIGO, ATNO;
   1241 	mov	MSG_OUT,SINDEX ret;
   1242 
   1243 SET_SRC_MODE	M_DFF1;
   1244 SET_DST_MODE	M_DFF1;
   1245 disable_ccsgen:
   1246 	test	SG_STATE, FETCH_INPROG jz disable_ccsgen_fetch_done;
   1247 	clr	CCSGCTL;
   1248 disable_ccsgen_fetch_done:
   1249 	clr	SG_STATE ret;
   1250 
   1251 service_fifo:
   1252 	/*
   1253 	 * Do we have any prefetch left???
   1254 	 */
   1255 	test	SG_STATE, SEGS_AVAIL jnz idle_sg_avail;
   1256 
   1257 	/*
   1258 	 * Can this FIFO have access to the S/G cache yet?
   1259 	 */
   1260 	test	CCSGCTL, SG_CACHE_AVAIL jz return;
   1261 
   1262 	/* Did we just finish fetching segs? */
   1263 	test	CCSGCTL, CCSGDONE jnz idle_sgfetch_complete;
   1264 
   1265 	/* Are we actively fetching segments? */
   1266 	test	CCSGCTL, CCSGENACK jnz return;
   1267 
   1268 	/*
   1269 	 * We fetch a "cacheline aligned" and sized amount of data
   1270 	 * so we don't end up referencing a non-existant page.
   1271 	 * Cacheline aligned is in quotes because the kernel will
   1272 	 * set the prefetch amount to a reasonable level if the
   1273 	 * cacheline size is unknown.
   1274 	 */
   1275 	bmov	SGHADDR, SCB_RESIDUAL_SGPTR, 4;
   1276 	mvi	SGHCNT, SG_PREFETCH_CNT;
   1277 	if ((ahd->bugs & AHD_REG_SLOW_SETTLE_BUG) != 0) {
   1278 		/*
   1279 		 * Need two instruction between "touches" of SGHADDR.
   1280 		 */
   1281 		nop;
   1282 	}
   1283 	and	SGHADDR[0], SG_PREFETCH_ALIGN_MASK, SCB_RESIDUAL_SGPTR;
   1284 	mvi	CCSGCTL, CCSGEN|CCSGRESET;
   1285 	or	SG_STATE, FETCH_INPROG ret;
   1286 idle_sgfetch_complete:
   1287 	/*
   1288 	 * Guard against SG_CACHE_AVAIL activating during sg fetch
   1289 	 * request in the other FIFO.
   1290 	 */
   1291 	test	SG_STATE, FETCH_INPROG jz return;
   1292 	clr	CCSGCTL;
   1293 	and	CCSGADDR, SG_PREFETCH_ADDR_MASK, SCB_RESIDUAL_SGPTR;
   1294 	mvi	SG_STATE, SEGS_AVAIL|LOADING_NEEDED;
   1295 idle_sg_avail:
   1296 	/* Does the hardware have space for another SG entry? */
   1297 	test	DFSTATUS, PRELOAD_AVAIL jz return;
   1298 	/*
   1299 	 * On the A, preloading a segment before HDMAENACK
   1300 	 * comes true can clobber the shaddow address of the
   1301 	 * first segment in the S/G FIFO.  Wait until it is
   1302 	 * safe to proceed.
   1303 	 */
   1304 	if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) == 0) {
   1305 		test	DFCNTRL, HDMAENACK jz return;
   1306 	}
   1307 	if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
   1308 		bmov	HADDR, CCSGRAM, 8;
   1309 	} else {
   1310 		bmov 	HADDR, CCSGRAM, 4;
   1311 	}
   1312 	bmov	HCNT, CCSGRAM, 3;
   1313 	test	HCNT[0], 0x1 jz . + 2;
   1314 	xor	DATA_COUNT_ODD, 0x1;
   1315 	bmov	SCB_RESIDUAL_DATACNT[3], CCSGRAM, 1;
   1316 	if ((ahd->flags & AHD_39BIT_ADDRESSING) != 0) {
   1317 		and	HADDR[4], SG_HIGH_ADDR_BITS, SCB_RESIDUAL_DATACNT[3];
   1318 	}
   1319 	if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
   1320 		/* Skip 4 bytes of pad. */
   1321 		add	CCSGADDR, 4;
   1322 	}
   1323 sg_advance:
   1324 	clr	A;			/* add sizeof(struct scatter) */
   1325 	add	SCB_RESIDUAL_SGPTR[0],SG_SIZEOF;
   1326 	adc	SCB_RESIDUAL_SGPTR[1],A;
   1327 	adc	SCB_RESIDUAL_SGPTR[2],A;
   1328 	adc	SCB_RESIDUAL_SGPTR[3],A;
   1329 	mov	SINDEX, SCB_RESIDUAL_SGPTR[0];
   1330 	test	DATA_COUNT_ODD, 0x1 jz . + 2;
   1331 	or	SINDEX, ODD_SEG;
   1332 	test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz . + 3;
   1333 	or	SINDEX, LAST_SEG;
   1334 	clr	SG_STATE;
   1335 	mov	SG_CACHE_PRE, SINDEX;
   1336 	if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) != 0) {
   1337 		/*
   1338 		 * Use SCSIENWRDIS so that SCSIEN is never
   1339 		 * modified by this operation.
   1340 		 */
   1341 		or	DFCNTRL, PRELOADEN|HDMAEN|SCSIENWRDIS;
   1342 	} else {
   1343 		or	DFCNTRL, PRELOADEN|HDMAEN;
   1344 	}
   1345 	/*
   1346 	 * Do we have another segment in the cache?
   1347 	 */
   1348 	add	NONE, SG_PREFETCH_CNT_LIMIT, CCSGADDR;
   1349 	jnc	return;
   1350 	and	SG_STATE, ~SEGS_AVAIL ret;
   1351 
   1352 /*
   1353  * Initialize the DMA address and counter from the SCB.
   1354  */
   1355 load_first_seg:
   1356 	bmov	HADDR, SCB_DATAPTR, 11;
   1357 	and	DATA_COUNT_ODD, 0x1, SCB_DATACNT[0];
   1358 	and	REG_ISR, ~SG_FULL_RESID, SCB_SGPTR[0];
   1359 	test	SCB_DATACNT[3], SG_LAST_SEG jz . + 2;
   1360 	or	REG_ISR, LAST_SEG;
   1361 	test	DATA_COUNT_ODD, 0x1 jz . + 2;
   1362 	or	REG_ISR, ODD_SEG;
   1363 	mov	SG_CACHE_PRE, REG_ISR;
   1364 	mvi	DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN);
   1365 	/*
   1366 	 * Since we've are entering a data phase, we will
   1367 	 * rely on the SCB_RESID* fields.  Initialize the
   1368 	 * residual and clear the full residual flag.
   1369 	 */
   1370 	and	SCB_SGPTR[0], ~SG_FULL_RESID;
   1371 	bmov	SCB_RESIDUAL_DATACNT[3], SCB_DATACNT[3], 5;
   1372 	/* If we need more S/G elements, tell the idle loop */
   1373 	test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jnz . + 2;
   1374 	mvi	SG_STATE, LOADING_NEEDED ret;
   1375 	clr	SG_STATE ret;
   1376 
   1377 p_data_handle_xfer:
   1378 	call	setjmp;
   1379 	test	SG_STATE, LOADING_NEEDED jnz service_fifo;
   1380 p_data_clear_handler:
   1381 	or	LONGJMP_ADDR[1], INVALID_ADDR ret;
   1382 
   1383 p_data:
   1384 	test	SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT	jz p_data_allowed;
   1385 	SET_SEQINTCODE(PROTO_VIOLATION)
   1386 p_data_allowed:
   1387 
   1388 	test	SEQ_FLAGS, DPHASE	jz data_phase_initialize;
   1389 
   1390 	/*
   1391 	 * If we re-enter the data phase after going through another
   1392 	 * phase, our transfer location has almost certainly been
   1393 	 * corrupted by the interveining, non-data, transfers.  Ask
   1394 	 * the host driver to fix us up based on the transfer residual
   1395 	 * unless we already know that we should be bitbucketing.
   1396 	 */
   1397 	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jnz p_data_bitbucket;
   1398 	SET_SEQINTCODE(PDATA_REINIT)
   1399 	jmp	data_phase_inbounds;
   1400 
   1401 p_data_bitbucket:
   1402 	/*
   1403 	 * Turn on `Bit Bucket' mode, wait until the target takes
   1404 	 * us to another phase, and then notify the host.
   1405 	 */
   1406 	mov	SAVED_MODE, MODE_PTR;
   1407 	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
   1408 		jnz bitbucket_not_m_dff;
   1409 	/*
   1410 	 * Ensure that any FIFO contents are cleared out and the
   1411 	 * FIFO free'd prior to starting the BITBUCKET.  BITBUCKET
   1412 	 * doesn't discard data already in the FIFO.
   1413 	 */
   1414 	mvi	DFFSXFRCTL, RSTCHN|CLRSHCNT;
   1415 	SET_MODE(M_SCSI, M_SCSI)
   1416 bitbucket_not_m_dff:
   1417 	or	SXFRCTL1,BITBUCKET;
   1418 	/* Wait for non-data phase. */
   1419 	test	SCSIPHASE, ~DATA_PHASE_MASK jz .;
   1420 	and	SXFRCTL1, ~BITBUCKET;
   1421 	RESTORE_MODE(SAVED_MODE)
   1422 SET_SRC_MODE	M_DFF1;
   1423 SET_DST_MODE	M_DFF1;
   1424 	SET_SEQINTCODE(DATA_OVERRUN)
   1425 	jmp	ITloop;
   1426 
   1427 data_phase_initialize:
   1428 	test	SCB_SGPTR[0], SG_LIST_NULL jnz p_data_bitbucket;
   1429 	call	load_first_seg;
   1430 data_phase_inbounds:
   1431 	/* We have seen a data phase at least once. */
   1432 	or	SEQ_FLAGS, DPHASE;
   1433 	mov	SAVED_MODE, MODE_PTR;
   1434 	test	SG_STATE, LOADING_NEEDED jz data_group_dma_loop;
   1435 	call	p_data_handle_xfer;
   1436 data_group_dma_loop:
   1437 	/*
   1438 	 * The transfer is complete if either the last segment
   1439 	 * completes or the target changes phase.  Both conditions
   1440 	 * will clear SCSIEN.
   1441 	 */
   1442 	call	idle_loop_service_fifos;
   1443 	call	idle_loop_cchan;
   1444 	call	idle_loop_gsfifo;
   1445 	RESTORE_MODE(SAVED_MODE)
   1446 	test	DFCNTRL, SCSIEN jnz data_group_dma_loop;
   1447 
   1448 data_group_dmafinish:
   1449 	/*
   1450 	 * The transfer has terminated either due to a phase
   1451 	 * change, and/or the completion of the last segment.
   1452 	 * We have two goals here.  Do as much other work
   1453 	 * as possible while the data fifo drains on a read
   1454 	 * and respond as quickly as possible to the standard
   1455 	 * messages (save data pointers/disconnect and command
   1456 	 * complete) that usually follow a data phase.
   1457 	 */
   1458 	call	calc_residual;
   1459 
   1460 	/*
   1461 	 * Go ahead and shut down the DMA engine now.
   1462 	 */
   1463 	test	DFCNTRL, DIRECTION jnz data_phase_finish;
   1464 data_group_fifoflush:
   1465 	if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
   1466 		or	DFCNTRL, FIFOFLUSH;
   1467 	}
   1468 	/*
   1469 	 * We have enabled the auto-ack feature.  This means
   1470 	 * that the controller may have already transferred
   1471 	 * some overrun bytes into the data FIFO and acked them
   1472 	 * on the bus.  The only way to detect this situation is
   1473 	 * to wait for LAST_SEG_DONE to come true on a completed
   1474 	 * transfer and then test to see if the data FIFO is
   1475 	 * non-empty.  We know there is more data yet to transfer
   1476 	 * if SG_LIST_NULL is not yet set, thus there cannot be
   1477 	 * an overrun.
   1478 	 */
   1479 	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz data_phase_finish;
   1480 	test	SG_CACHE_SHADOW, LAST_SEG_DONE jz .;
   1481 	test	DFSTATUS, FIFOEMP jnz data_phase_finish;
   1482 	/* Overrun */
   1483 	jmp	p_data;
   1484 data_phase_finish:
   1485 	/*
   1486 	 * If the target has left us in data phase, loop through
   1487 	 * the DMA code again.  We will only loop if there is a
   1488 	 * data overrun.
   1489 	 */
   1490 	if ((ahd->flags & AHD_TARGETROLE) != 0) {
   1491 		test	SSTAT0, TARGET jnz data_phase_done;
   1492 	}
   1493 	if ((ahd->flags & AHD_INITIATORROLE) != 0) {
   1494 		test	SSTAT1, REQINIT jz .;
   1495 		test	SCSIPHASE, DATA_PHASE_MASK jnz p_data;
   1496 	}
   1497 
   1498 data_phase_done:
   1499 	/* Kill off any pending prefetch */
   1500 	call	disable_ccsgen;
   1501 	or 	LONGJMP_ADDR[1], INVALID_ADDR;
   1502 
   1503 	if ((ahd->flags & AHD_TARGETROLE) != 0) {
   1504 		test	SEQ_FLAGS, DPHASE_PENDING jz ITloop;
   1505 		/*
   1506 		and	SEQ_FLAGS, ~DPHASE_PENDING;
   1507 		 * For data-in phases, wait for any pending acks from the
   1508 		 * initiator before changing phase.  We only need to
   1509 		 * send Ignore Wide Residue messages for data-in phases.
   1510 		test	DFCNTRL, DIRECTION jz target_ITloop;
   1511 		test	SSTAT1, REQINIT	jnz .;
   1512 		test	DATA_COUNT_ODD, 0x1 jz target_ITloop;
   1513 		SET_MODE(M_SCSI, M_SCSI)
   1514 		test	NEGCONOPTS, WIDEXFER jz target_ITloop;
   1515 		 */
   1516 		/*
   1517 		 * Issue an Ignore Wide Residue Message.
   1518 		mvi	P_MESGIN|BSYO call change_phase;
   1519 		mvi	MSG_IGN_WIDE_RESIDUE call target_outb;
   1520 		mvi	1 call target_outb;
   1521 		jmp	target_ITloop;
   1522 		 */
   1523 	} else {
   1524 		jmp	ITloop;
   1525 	}
   1526 
   1527 /*
   1528  * We assume that, even though data may still be
   1529  * transferring to the host, that the SCSI side of
   1530  * the DMA engine is now in a static state.  This
   1531  * allows us to update our notion of where we are
   1532  * in this transfer.
   1533  *
   1534  * If, by chance, we stopped before being able
   1535  * to fetch additional segments for this transfer,
   1536  * yet the last S/G was completely exhausted,
   1537  * call our idle loop until it is able to load
   1538  * another segment.  This will allow us to immediately
   1539  * pickup on the next segment on the next data phase.
   1540  *
   1541  * If we happened to stop on the last segment, then
   1542  * our residual information is still correct from
   1543  * the idle loop and there is no need to perform
   1544  * any fixups.
   1545  */
   1546 residual_before_last_seg:
   1547 	test    MDFFSTAT, SHVALID	jnz sgptr_fixup;
   1548 	/*
   1549 	 * Can never happen from an interrupt as the packetized
   1550 	 * hardware will only interrupt us once SHVALID or
   1551 	 * LAST_SEG_DONE.
   1552 	 */
   1553 	call	idle_loop_service_fifos;
   1554 	RESTORE_MODE(SAVED_MODE)
   1555 	/* FALLTHROUGH */
   1556 calc_residual:
   1557 	test	SG_CACHE_SHADOW, LAST_SEG jz residual_before_last_seg;
   1558 	/* Record if we've consumed all S/G entries */
   1559 	test	MDFFSTAT, SHVALID	jz . + 2;
   1560 	bmov	SCB_RESIDUAL_DATACNT, SHCNT, 3 ret;
   1561 	or	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL ret;
   1562 
   1563 sgptr_fixup:
   1564 	/*
   1565 	 * Fixup the residual next S/G pointer.  The S/G preload
   1566 	 * feature of the chip allows us to load two elements
   1567 	 * in addition to the currently active element.  We
   1568 	 * store the bottom byte of the next S/G pointer in
   1569 	 * the SG_CACHE_PTR register so we can restore the
   1570 	 * correct value when the DMA completes.  If the next
   1571 	 * sg ptr value has advanced to the point where higher
   1572 	 * bytes in the address have been affected, fix them
   1573 	 * too.
   1574 	 */
   1575 	test	SG_CACHE_SHADOW, 0x80 jz sgptr_fixup_done;
   1576 	test	SCB_RESIDUAL_SGPTR[0], 0x80 jnz sgptr_fixup_done;
   1577 	add	SCB_RESIDUAL_SGPTR[1], -1;
   1578 	adc	SCB_RESIDUAL_SGPTR[2], -1;
   1579 	adc	SCB_RESIDUAL_SGPTR[3], -1;
   1580 sgptr_fixup_done:
   1581 	and	SCB_RESIDUAL_SGPTR[0], SG_ADDR_MASK, SG_CACHE_SHADOW;
   1582 	clr	DATA_COUNT_ODD;
   1583 	test	SG_CACHE_SHADOW, ODD_SEG jz . + 2;
   1584 	or	DATA_COUNT_ODD, 0x1;
   1585 	clr	SCB_RESIDUAL_DATACNT[3]; /* We are not the last seg */
   1586 	bmov	SCB_RESIDUAL_DATACNT, SHCNT, 3 ret;
   1587 
   1588 export timer_isr:
   1589 	call	issue_cmdcmplt;
   1590 	mvi	CLRSEQINTSTAT, CLRSEQ_SWTMRTO;
   1591 	if ((ahd->bugs & AHD_SET_MODE_BUG) != 0) {
   1592 		/*
   1593 		 * In H2A4, the mode pointer is not saved
   1594 		 * for intvec2, but is restored on iret.
   1595 		 * This can lead to the restoration of a
   1596 		 * bogus mode ptr.  Manually clear the
   1597 		 * intmask bits and do a normal return
   1598 		 * to compensate.
   1599 		 */
   1600 		and	SEQINTCTL, ~(INTMASK2|INTMASK1) ret;
   1601 	} else {
   1602 		or	SEQINTCTL, IRET ret;
   1603 	}
   1604 
   1605 export seq_isr:
   1606 	if ((ahd->features & AHD_RTI) == 0) {
   1607 		/*
   1608 		 * On RevA Silicon, if the target returns us to data-out
   1609 		 * after we have already trained for data-out, it is
   1610 		 * possible for us to transition the free running clock to
   1611 		 * data-valid before the required 100ns P1 setup time (8 P1
   1612 		 * assertions in fast-160 mode).  This will only happen if
   1613 		 * this L-Q is a continuation of a data transfer for which
   1614 		 * we have already prefetched data into our FIFO (LQ/Data
   1615 		 * followed by LQ/Data for the same write transaction).
   1616 		 * This can cause some target implementations to miss the
   1617 		 * first few data transfers on the bus.  We detect this
   1618 		 * situation by noticing that this is the first data transfer
   1619 		 * after an LQ (LQIWORKONLQ true), that the data transfer is
   1620 		 * a continuation of a transfer already setup in our FIFO
   1621 		 * (SAVEPTRS interrupt), and that the transaction is a write
   1622 		 * (DIRECTION set in DFCNTRL). The delay is performed by
   1623 		 * disabling SCSIEN until we see the first REQ from the
   1624 		 * target.
   1625 		 *
   1626 		 * First instruction in an ISR cannot be a branch on
   1627 		 * Rev A.  Snapshot LQISTAT2 so the status is not missed
   1628 		 * and deffer the test by one instruction.
   1629 		 */
   1630 		mov	REG_ISR, LQISTAT2;
   1631 		test	REG_ISR, LQIWORKONLQ jz main_isr;
   1632 		test	SEQINTSRC, SAVEPTRS  jz main_isr;
   1633 		test	LONGJMP_ADDR[1], INVALID_ADDR jz saveptr_active_fifo;
   1634 		/*
   1635 		 * Switch to the active FIFO after clearing the snapshot
   1636 		 * savepointer in the current FIFO.  We do this so that
   1637 		 * a pending CTXTDONE or SAVEPTR is visible in the active
   1638 		 * FIFO.  This status is the only way we can detect if we
   1639 		 * have lost the race (e.g. host paused us) and our attepts
   1640 		 * to disable the channel occurred after all REQs were
   1641 		 * already seen and acked (REQINIT never comes true).
   1642 		 */
   1643 		mvi	DFFSXFRCTL, CLRCHN;
   1644 		xor	MODE_PTR, MK_MODE(M_DFF1, M_DFF1);
   1645 		test	DFCNTRL, DIRECTION jz interrupt_return;
   1646 		and	DFCNTRL, ~SCSIEN;
   1647 snapshot_wait_data_valid:
   1648 		test	SEQINTSRC, (CTXTDONE|SAVEPTRS) jnz snapshot_data_valid;
   1649 		test	SSTAT1, REQINIT	jz snapshot_wait_data_valid;
   1650 snapshot_data_valid:
   1651 		or	DFCNTRL, SCSIEN;
   1652 		or	SEQINTCTL, IRET ret;
   1653 snapshot_saveptr:
   1654 		mvi	DFFSXFRCTL, CLRCHN;
   1655 		or	SEQINTCTL, IRET ret;
   1656 main_isr:
   1657 	}
   1658 	test	SEQINTSRC, CFG4DATA	jnz cfg4data_intr;
   1659 	test	SEQINTSRC, CFG4ISTAT	jnz cfg4istat_intr;
   1660 	test	SEQINTSRC, SAVEPTRS	jnz saveptr_intr;
   1661 	test	SEQINTSRC, CFG4ICMD	jnz cfg4icmd_intr;
   1662 	SET_SEQINTCODE(INVALID_SEQINT)
   1663 
   1664 /*
   1665  * There are two types of save pointers interrupts:
   1666  * The first is a snapshot save pointers where the current FIFO is not
   1667  * active and contains a snapshot of the current poniter information.
   1668  * This happens between packets in a stream for a single L_Q.  Since we
   1669  * are not performing a pointer save, we can safely clear the channel
   1670  * so it can be used for other transactions.  On RTI capable controllers,
   1671  * where snapshots can, and are, disabled, the code to handle this type
   1672  * of snapshot is not active.
   1673  *
   1674  * The second case is a save pointers on an active FIFO which occurs
   1675  * if the target changes to a new L_Q or busfrees/QASes and the transfer
   1676  * has a residual.  This should occur coincident with a ctxtdone.  We
   1677  * disable the interrupt and allow our active routine to handle the
   1678  * save.
   1679  */
   1680 saveptr_intr:
   1681 	if ((ahd->features & AHD_RTI) == 0) {
   1682 		test	LONGJMP_ADDR[1], INVALID_ADDR jnz snapshot_saveptr;
   1683 	}
   1684 saveptr_active_fifo:
   1685 	and	SEQIMODE, ~ENSAVEPTRS;
   1686 	or	SEQINTCTL, IRET ret;
   1687 
   1688 cfg4data_intr:
   1689 	test	SCB_SGPTR[0], SG_LIST_NULL jnz pkt_handle_overrun_inc_use_count;
   1690 	call	load_first_seg;
   1691 	call	pkt_handle_xfer;
   1692 	inc	SCB_FIFO_USE_COUNT;
   1693 interrupt_return:
   1694 	or	SEQINTCTL, IRET ret;
   1695 
   1696 cfg4istat_intr:
   1697 	call	freeze_queue;
   1698 	add	NONE, -13, SCB_CDB_LEN;
   1699 	jnc	cfg4istat_have_sense_addr;
   1700 	test	SCB_CDB_LEN, SCB_CDB_LEN_PTR jnz cfg4istat_have_sense_addr;
   1701 	/*
   1702 	 * Host sets up address/count and enables transfer.
   1703 	 */
   1704 	SET_SEQINTCODE(CFG4ISTAT_INTR)
   1705 	jmp	cfg4istat_setup_handler;
   1706 cfg4istat_have_sense_addr:
   1707 	bmov	HADDR, SCB_SENSE_BUSADDR, 4;
   1708 	mvi	HCNT[1], (AHD_SENSE_BUFSIZE >> 8);
   1709 	mvi	SG_CACHE_PRE, LAST_SEG;
   1710 	mvi	DFCNTRL, PRELOADEN|SCSIEN|HDMAEN;
   1711 cfg4istat_setup_handler:
   1712 	/*
   1713 	 * Status pkt is transferring to host.
   1714 	 * Wait in idle loop for transfer to complete.
   1715 	 * If a command completed before an attempted
   1716 	 * task management function completed, notify the host.
   1717 	 */
   1718 	test	SCB_TASK_MANAGEMENT, 0xFF jz cfg4istat_no_taskmgmt_func;
   1719 	SET_SEQINTCODE(TASKMGMT_CMD_CMPLT_OKAY)
   1720 cfg4istat_no_taskmgmt_func:
   1721 	call	pkt_handle_status;
   1722 	or	SEQINTCTL, IRET ret;
   1723 
   1724 cfg4icmd_intr:
   1725 	/*
   1726 	 * In the case of DMAing a CDB from the host, the normal
   1727 	 * CDB buffer is formatted with an 8 byte address followed
   1728 	 * by a 1 byte count.
   1729 	 */
   1730 	bmov	HADDR[0], SCB_HOST_CDB_PTR, 9;
   1731 	mvi	SG_CACHE_PRE, LAST_SEG;
   1732 	mvi	DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN);
   1733 	call	pkt_handle_cdb;
   1734 	or	SEQINTCTL, IRET ret;
   1735 
   1736 /*
   1737  * See if the target has gone on in this context creating an
   1738  * overrun condition.  For the write case, the hardware cannot
   1739  * ack bytes until data are provided.  So, if the target begins
   1740  * another  packet without changing contexts, implying we are
   1741  * not sitting on a packet boundary, we are in an overrun
   1742  * situation.  For the read case, the hardware will continue to
   1743  * ack bytes into the FIFO, and may even ack the last overrun packet
   1744  * into the FIFO.   If the FIFO should become non-empty, we are in
   1745  * a read overrun case.
   1746  */
   1747 #define check_overrun							\
   1748 	/* Not on a packet boundary. */					\
   1749 	test 	MDFFSTAT, DLZERO jz pkt_handle_overrun;			\
   1750 	test	DFSTATUS, FIFOEMP jz pkt_handle_overrun
   1751 
   1752 pkt_handle_xfer:
   1753 	test	SG_STATE, LOADING_NEEDED jz pkt_last_seg;
   1754 	call	setjmp;
   1755 	test	SEQINTSRC, SAVEPTRS jnz pkt_saveptrs;
   1756 	test	SCSIPHASE, ~DATA_PHASE_MASK jz . + 2;
   1757 	test	SCSISIGO, ATNO jnz . + 2;
   1758 	test	SSTAT2, NONPACKREQ jz pkt_service_fifo;
   1759 	/*
   1760 	 * Defer handling of this NONPACKREQ until we
   1761 	 * can be sure it pertains to this FIFO.  SAVEPTRS
   1762 	 * will not be asserted if the NONPACKREQ is for us,
   1763 	 * so we must simulate it if shaddow is valid.  If
   1764 	 * shaddow is not valid, keep running this FIFO until we
   1765 	 * have satisfied the transfer by loading segments and
   1766 	 * waiting for either shaddow valid or last_seg_done.
   1767 	 */
   1768 	test	MDFFSTAT, SHVALID jnz pkt_saveptrs;
   1769 pkt_service_fifo:
   1770 	test	SG_STATE, LOADING_NEEDED jnz service_fifo;
   1771 pkt_last_seg:
   1772 	call	setjmp;
   1773 	test	SEQINTSRC, SAVEPTRS jnz pkt_saveptrs;
   1774 	test	SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_last_seg_done;
   1775 	test	SCSIPHASE, ~DATA_PHASE_MASK jz . + 2;
   1776 	test	SCSISIGO, ATNO jnz . + 2;
   1777 	test	SSTAT2, NONPACKREQ jz return;
   1778 	test	MDFFSTAT, SHVALID jz return;
   1779 	/* FALLTHROUGH */
   1780 
   1781 /*
   1782  * Either a SAVEPTRS interrupt condition is pending for this FIFO
   1783  * or we have a pending NONPACKREQ for this FIFO.  We differentiate
   1784  * between the two by capturing the state of the SAVEPTRS interrupt
   1785  * prior to clearing this status and executing the common code for
   1786  * these two cases.
   1787  */
   1788 pkt_saveptrs:
   1789 BEGIN_CRITICAL;
   1790 	if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
   1791 		or	DFCNTRL, FIFOFLUSH;
   1792 	}
   1793 	mov	REG0, SEQINTSRC;
   1794 	call	calc_residual;
   1795 	call	save_pointers;
   1796 	mvi	CLRSEQINTSRC, CLRSAVEPTRS;
   1797 	call	disable_ccsgen;
   1798 	or	SEQIMODE, ENSAVEPTRS;
   1799 	test	DFCNTRL, DIRECTION jnz pkt_saveptrs_check_status;
   1800 	test	DFSTATUS, FIFOEMP jnz pkt_saveptrs_check_status;
   1801 	/*
   1802 	 * Keep a handler around for this FIFO until it drains
   1803 	 * to the host to guarantee that we don't complete the
   1804 	 * command to the host before the data arrives.
   1805 	 */
   1806 pkt_saveptrs_wait_fifoemp:
   1807 	call	setjmp;
   1808 	test	DFSTATUS, FIFOEMP jz return;
   1809 pkt_saveptrs_check_status:
   1810 	or	LONGJMP_ADDR[1], INVALID_ADDR;
   1811 	test	REG0, SAVEPTRS jz unexpected_nonpkt_phase;
   1812 	dec	SCB_FIFO_USE_COUNT;
   1813 	test	SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
   1814 	mvi	DFFSXFRCTL, CLRCHN ret;
   1815 END_CRITICAL;
   1816 
   1817 /*
   1818  * LAST_SEG_DONE status has been seen in the current FIFO.
   1819  * This indicates that all of the allowed data for this
   1820  * command has transferred across the SCSI and host buses.
   1821  * Check for overrun and see if we can complete this command.
   1822  */
   1823 pkt_last_seg_done:
   1824 BEGIN_CRITICAL;
   1825 	/*
   1826 	 * Mark transfer as completed.
   1827 	 */
   1828 	or	SCB_SGPTR, SG_LIST_NULL;
   1829 
   1830 	/*
   1831 	 * Wait for the current context to finish to verify that
   1832 	 * no overrun condition has occurred.
   1833 	 */
   1834 	test	SEQINTSRC, CTXTDONE jnz pkt_ctxt_done;
   1835 	call	setjmp;
   1836 pkt_wait_ctxt_done_loop:
   1837 	test	SEQINTSRC, CTXTDONE jnz pkt_ctxt_done;
   1838 	/*
   1839 	 * A sufficiently large overrun or a NONPACKREQ may
   1840 	 * prevent CTXTDONE from ever asserting, so we must
   1841 	 * poll for these statuses too.
   1842 	 */
   1843 	check_overrun;
   1844 	test	SSTAT2, NONPACKREQ jz return;
   1845 	test	SEQINTSRC, CTXTDONE jz unexpected_nonpkt_phase;
   1846 	/* FALLTHROUGH */
   1847 
   1848 pkt_ctxt_done:
   1849 	check_overrun;
   1850 	or	LONGJMP_ADDR[1], INVALID_ADDR;
   1851 	/*
   1852 	 * If status has been received, it is safe to skip
   1853 	 * the check to see if another FIFO is active because
   1854 	 * LAST_SEG_DONE has been observed.  However, we check
   1855 	 * the FIFO anyway since it costs us only one extra
   1856 	 * instruction to leverage common code to perform the
   1857 	 * SCB completion.
   1858 	 */
   1859 	dec	SCB_FIFO_USE_COUNT;
   1860 	test	SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
   1861 	mvi	DFFSXFRCTL, CLRCHN ret;
   1862 END_CRITICAL;
   1863 
   1864 /*
   1865  * Must wait until CDB xfer is over before issuing the
   1866  * clear channel.
   1867  */
   1868 pkt_handle_cdb:
   1869 	call	setjmp;
   1870 	test	SG_CACHE_SHADOW, LAST_SEG_DONE jz return;
   1871 	or	LONGJMP_ADDR[1], INVALID_ADDR;
   1872 	mvi	DFFSXFRCTL, CLRCHN ret;
   1873 
   1874 /*
   1875  * Watch over the status transfer.  Our host sense buffer is
   1876  * large enough to take the maximum allowed status packet.
   1877  * None-the-less, we must still catch and report overruns to
   1878  * the host.  Additionally, properly catch unexpected non-packet
   1879  * phases that are typically caused by CRC errors in status packet
   1880  * transmission.
   1881  */
   1882 pkt_handle_status:
   1883 	call	setjmp;
   1884 	test	SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_status_check_overrun;
   1885 	test	SEQINTSRC, CTXTDONE jz pkt_status_check_nonpackreq;
   1886 	test	SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_status_check_overrun;
   1887 pkt_status_IU_done:
   1888 	if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
   1889 		or	DFCNTRL, FIFOFLUSH;
   1890 	}
   1891 	test	DFSTATUS, FIFOEMP jz return;
   1892 BEGIN_CRITICAL;
   1893 	or	LONGJMP_ADDR[1], INVALID_ADDR;
   1894 	mvi	SCB_SCSI_STATUS, STATUS_PKT_SENSE;
   1895 	or	SCB_CONTROL, STATUS_RCVD;
   1896 	jmp	pkt_complete_scb_if_fifos_idle;
   1897 END_CRITICAL;
   1898 pkt_status_check_overrun:
   1899 	/*
   1900 	 * Status PKT overruns are uncerimoniously recovered with a
   1901 	 * bus reset.  If we've overrun, let the host know so that
   1902 	 * recovery can be performed.
   1903 	 *
   1904 	 * LAST_SEG_DONE has been observed.  If either CTXTDONE or
   1905 	 * a NONPACKREQ phase change have occurred and the FIFO is
   1906 	 * empty, there is no overrun.
   1907 	 */
   1908 	test	DFSTATUS, FIFOEMP jz pkt_status_report_overrun;
   1909 	test	SEQINTSRC, CTXTDONE jz . + 2;
   1910 	test	DFSTATUS, FIFOEMP jnz pkt_status_IU_done;
   1911 	test	SCSIPHASE, ~DATA_PHASE_MASK jz return;
   1912 	test	DFSTATUS, FIFOEMP jnz pkt_status_check_nonpackreq;
   1913 pkt_status_report_overrun:
   1914 	SET_SEQINTCODE(STATUS_OVERRUN)
   1915 	/* SEQUENCER RESTARTED */
   1916 pkt_status_check_nonpackreq:
   1917 	/*
   1918 	 * CTXTDONE may be held off if a NONPACKREQ is associated with
   1919 	 * the current context.  If a NONPACKREQ is observed, decide
   1920 	 * if it is for the current context.  If it is for the current
   1921 	 * context, we must defer NONPACKREQ processing until all data
   1922 	 * has transferred to the host.
   1923 	 */
   1924 	test	SCSIPHASE, ~DATA_PHASE_MASK jz return;
   1925 	test	SCSISIGO, ATNO jnz . + 2;
   1926 	test	SSTAT2, NONPACKREQ jz return;
   1927 	test	SEQINTSRC, CTXTDONE jnz pkt_status_IU_done;
   1928 	test	DFSTATUS, FIFOEMP jz return;
   1929 	/*
   1930 	 * The unexpected nonpkt phase handler assumes that any
   1931 	 * data channel use will have a FIFO reference count.  It
   1932 	 * turns out that the status handler doesn't need a refernce
   1933 	 * count since the status received flag, and thus completion
   1934 	 * processing, cannot be set until the handler is finished.
   1935 	 * We increment the count here to make the nonpkt handler
   1936 	 * happy.
   1937 	 */
   1938 	inc	SCB_FIFO_USE_COUNT;
   1939 	/* FALLTHROUGH */
   1940 
   1941 /*
   1942  * Nonpackreq is a polled status.  It can come true in three situations:
   1943  * we have received an L_Q, we have sent one or more L_Qs, or there is no
   1944  * L_Q context associated with this REQ (REQ occurs immediately after a
   1945  * (re)selection).  Routines that know that the context responsible for this
   1946  * nonpackreq call directly into unexpected_nonpkt_phase.  In the case of the
   1947  * top level idle loop, we exhaust all active contexts prior to determining that
   1948  * we simply do not have the full I_T_L_Q for this phase.
   1949  */
   1950 unexpected_nonpkt_phase_find_ctxt:
   1951 	/*
   1952 	 * This nonpackreq is most likely associated with one of the tags
   1953 	 * in a FIFO or an outgoing LQ.  Only treat it as an I_T only
   1954 	 * nonpackreq if we've cleared out the FIFOs and handled any
   1955 	 * pending SELDO.
   1956 	 */
   1957 SET_SRC_MODE	M_SCSI;
   1958 SET_DST_MODE	M_SCSI;
   1959 	and	A, FIFO1FREE|FIFO0FREE, DFFSTAT;
   1960 	cmp	A, FIFO1FREE|FIFO0FREE jne return;
   1961 	test	SSTAT0, SELDO jnz return;
   1962 	mvi	SCBPTR[1], SCB_LIST_NULL;
   1963 unexpected_nonpkt_phase:
   1964 	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1)) jnz . + 3;
   1965 SET_SRC_MODE	M_DFF0;
   1966 SET_DST_MODE	M_DFF0;
   1967 	or	LONGJMP_ADDR[1], INVALID_ADDR;
   1968 	dec	SCB_FIFO_USE_COUNT;
   1969 	mvi	DFFSXFRCTL, CLRCHN;
   1970 	mvi	CLRSINT2, CLRNONPACKREQ;
   1971 	test	SCSIPHASE, ~(MSG_IN_PHASE|MSG_OUT_PHASE) jnz illegal_phase;
   1972 	SET_SEQINTCODE(ENTERING_NONPACK)
   1973 	jmp	ITloop;
   1974 
   1975 illegal_phase:
   1976 	SET_SEQINTCODE(ILLEGAL_PHASE)
   1977 	jmp	ITloop;
   1978 
   1979 /*
   1980  * We have entered an overrun situation.  If we have working
   1981  * BITBUCKET, flip that on and let the hardware eat any overrun
   1982  * data.  Otherwise use an overrun buffer in the host to simulate
   1983  * BITBUCKET.
   1984  */
   1985 pkt_handle_overrun_inc_use_count:
   1986 	inc	SCB_FIFO_USE_COUNT;
   1987 pkt_handle_overrun:
   1988 	SET_SEQINTCODE(CFG4OVERRUN)
   1989 	call	freeze_queue;
   1990 	if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) == 0) {
   1991 		or	DFFSXFRCTL, DFFBITBUCKET;
   1992 SET_SRC_MODE	M_DFF1;
   1993 SET_DST_MODE	M_DFF1;
   1994 	} else {
   1995 		call	load_overrun_buf;
   1996 		mvi	DFCNTRL, (HDMAEN|SCSIEN|PRELOADEN);
   1997 	}
   1998 	call	setjmp;
   1999 	if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
   2000 		test	DFSTATUS, PRELOAD_AVAIL jz overrun_load_done;
   2001 		call	load_overrun_buf;
   2002 		or	DFCNTRL, PRELOADEN;
   2003 overrun_load_done:
   2004 		test	SEQINTSRC, CTXTDONE jnz pkt_overrun_end;
   2005 	} else {
   2006 		test	DFFSXFRCTL, DFFBITBUCKET jz pkt_overrun_end;
   2007 	}
   2008 	test	SSTAT2, NONPACKREQ jz return;
   2009 pkt_overrun_end:
   2010 	or	SCB_RESIDUAL_SGPTR, SG_OVERRUN_RESID;
   2011 	test	SEQINTSRC, CTXTDONE jz unexpected_nonpkt_phase;
   2012 	dec	SCB_FIFO_USE_COUNT;
   2013 	or	LONGJMP_ADDR[1], INVALID_ADDR;
   2014 	test	SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
   2015 	mvi	DFFSXFRCTL, CLRCHN ret;
   2016 
   2017 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
   2018 load_overrun_buf:
   2019 	/*
   2020 	 * Load a dummy segment if preload space is available.
   2021 	 */
   2022 	mov 	HADDR[0], SHARED_DATA_ADDR;
   2023 	add	HADDR[1], PKT_OVERRUN_BUFOFFSET, SHARED_DATA_ADDR[1];
   2024 	mov	ACCUM_SAVE, A;
   2025 	clr	A;
   2026 	adc	HADDR[2], A, SHARED_DATA_ADDR[2];
   2027 	adc	HADDR[3], A, SHARED_DATA_ADDR[3];
   2028 	mov	A, ACCUM_SAVE;
   2029 	bmov	HADDR[4], ALLZEROS, 4;
   2030 	/* PKT_OVERRUN_BUFSIZE is a multiple of 256 */
   2031 	clr	HCNT[0];
   2032 	mvi	HCNT[1], ((PKT_OVERRUN_BUFSIZE >> 8) & 0xFF);
   2033 	clr	HCNT[2] ret;
   2034 }
   2035