xref: /freebsd/sys/dev/aic7xxx/aic79xx.seq (revision b971c51a4d9498c26079fd8229f7af6ef1585020)
1098ca2bdSWarner Losh/*-
28fdc1261SJustin T. Gibbs * Adaptec U320 device driver firmware for Linux and FreeBSD.
317d24755SJustin T. Gibbs *
4789902c3SJustin T. Gibbs * Copyright (c) 1994-2001, 2004 Justin T. Gibbs.
58fdc1261SJustin T. Gibbs * Copyright (c) 2000-2002 Adaptec Inc.
617d24755SJustin T. Gibbs * All rights reserved.
717d24755SJustin T. Gibbs *
817d24755SJustin T. Gibbs * Redistribution and use in source and binary forms, with or without
917d24755SJustin T. Gibbs * modification, are permitted provided that the following conditions
1017d24755SJustin T. Gibbs * are met:
1117d24755SJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright
1217d24755SJustin T. Gibbs *    notice, this list of conditions, and the following disclaimer,
1317d24755SJustin T. Gibbs *    without modification.
1417d24755SJustin T. Gibbs * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1517d24755SJustin T. Gibbs *    substantially similar to the "NO WARRANTY" disclaimer below
1617d24755SJustin T. Gibbs *    ("Disclaimer") and any redistribution must be conditioned upon
1717d24755SJustin T. Gibbs *    including a substantially similar Disclaimer requirement for further
1817d24755SJustin T. Gibbs *    binary redistribution.
1917d24755SJustin T. Gibbs * 3. Neither the names of the above-listed copyright holders nor the names
2017d24755SJustin T. Gibbs *    of any contributors may be used to endorse or promote products derived
2117d24755SJustin T. Gibbs *    from this software without specific prior written permission.
2217d24755SJustin T. Gibbs *
2317d24755SJustin T. Gibbs * Alternatively, this software may be distributed under the terms of the
2417d24755SJustin T. Gibbs * GNU General Public License ("GPL") version 2 as published by the Free
2517d24755SJustin T. Gibbs * Software Foundation.
2617d24755SJustin T. Gibbs *
2717d24755SJustin T. Gibbs * NO WARRANTY
2817d24755SJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2917d24755SJustin T. Gibbs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3017d24755SJustin T. Gibbs * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
3117d24755SJustin T. Gibbs * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3217d24755SJustin T. Gibbs * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3317d24755SJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3417d24755SJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3517d24755SJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3617d24755SJustin T. Gibbs * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
3717d24755SJustin T. Gibbs * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3817d24755SJustin T. Gibbs * POSSIBILITY OF SUCH DAMAGES.
3917d24755SJustin T. Gibbs */
4017d24755SJustin T. Gibbs
4122dbd4c6SJustin T. GibbsVERSION = "$Id: //depot/aic7xxx/aic7xxx/aic79xx.seq#119 $"
4217d24755SJustin T. GibbsPATCH_ARG_LIST = "struct ahd_softc *ahd"
438fdc1261SJustin T. GibbsPREFIX = "ahd_"
4417d24755SJustin T. Gibbs
4517d24755SJustin T. Gibbs#include "aic79xx.reg"
4617d24755SJustin T. Gibbs#include "scsi_message.h"
4717d24755SJustin T. Gibbs
480c5aa4c5SScott Longrestart:
490c5aa4c5SScott Longif ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
500c5aa4c5SScott Long	test	SEQINTCODE, 0xFF jz idle_loop;
510c5aa4c5SScott Long	SET_SEQINTCODE(NO_SEQINT)
520c5aa4c5SScott Long}
530c5aa4c5SScott Long
5417d24755SJustin T. Gibbsidle_loop:
550c5aa4c5SScott Long
560c5aa4c5SScott Long	if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
570c5aa4c5SScott Long		/*
580c5aa4c5SScott Long		 * Convert ERROR status into a sequencer
590c5aa4c5SScott Long		 * interrupt to handle the case of an
600c5aa4c5SScott Long		 * interrupt collision on the hardware
610c5aa4c5SScott Long		 * setting of HWERR.
620c5aa4c5SScott Long		 */
638528a712SScott Long		test	ERROR, 0xFF jz no_error_set;
640c5aa4c5SScott Long		SET_SEQINTCODE(SAW_HWERR)
658528a712SScott Longno_error_set:
660c5aa4c5SScott Long	}
67c59c8a72SJustin T. Gibbs	SET_MODE(M_SCSI, M_SCSI)
6817d24755SJustin T. Gibbs	test	SCSISEQ0, ENSELO|ENARBO jnz idle_loop_checkbus;
694164174aSJustin T. Gibbs	test	SEQ_FLAGS2, SELECTOUT_QFROZEN jz check_waiting_list;
704164174aSJustin T. Gibbs	/*
714164174aSJustin T. Gibbs	 * If the kernel has caught up with us, thaw the queue.
724164174aSJustin T. Gibbs	 */
734164174aSJustin T. Gibbs	mov	A, KERNEL_QFREEZE_COUNT;
744164174aSJustin T. Gibbs	cmp	QFREEZE_COUNT, A jne check_frozen_completions;
754164174aSJustin T. Gibbs	mov	A, KERNEL_QFREEZE_COUNT[1];
764164174aSJustin T. Gibbs	cmp	QFREEZE_COUNT[1], A jne check_frozen_completions;
774164174aSJustin T. Gibbs	and	SEQ_FLAGS2, ~SELECTOUT_QFROZEN;
784164174aSJustin T. Gibbs	jmp	check_waiting_list;
794164174aSJustin T. Gibbscheck_frozen_completions:
804164174aSJustin T. Gibbs	test	SSTAT0, SELDO|SELINGO jnz idle_loop_checkbus;
814164174aSJustin T. GibbsBEGIN_CRITICAL;
824164174aSJustin T. Gibbs	/*
834164174aSJustin T. Gibbs	 * If we have completions stalled waiting for the qfreeze
844164174aSJustin T. Gibbs	 * to take effect, move them over to the complete_scb list
854164174aSJustin T. Gibbs	 * now that no selections are pending.
864164174aSJustin T. Gibbs	 */
874164174aSJustin T. Gibbs	cmp	COMPLETE_ON_QFREEZE_HEAD[1],SCB_LIST_NULL je idle_loop_checkbus;
884164174aSJustin T. Gibbs	/*
894164174aSJustin T. Gibbs	 * Find the end of the qfreeze list.  The first element has
904164174aSJustin T. Gibbs	 * to be treated specially.
914164174aSJustin T. Gibbs	 */
924164174aSJustin T. Gibbs	bmov	SCBPTR, COMPLETE_ON_QFREEZE_HEAD, 2;
934164174aSJustin T. Gibbs	cmp 	SCB_NEXT_COMPLETE[1], SCB_LIST_NULL je join_lists;
944164174aSJustin T. Gibbs	/*
954164174aSJustin T. Gibbs	 * Now the normal loop.
964164174aSJustin T. Gibbs	 */
974164174aSJustin T. Gibbs	bmov	SCBPTR, SCB_NEXT_COMPLETE, 2;
984164174aSJustin T. Gibbs	cmp 	SCB_NEXT_COMPLETE[1], SCB_LIST_NULL jne . - 1;
994164174aSJustin T. Gibbsjoin_lists:
1004164174aSJustin T. Gibbs	bmov	SCB_NEXT_COMPLETE, COMPLETE_SCB_HEAD, 2;
1014164174aSJustin T. Gibbs	bmov	COMPLETE_SCB_HEAD, COMPLETE_ON_QFREEZE_HEAD, 2;
1024164174aSJustin T. Gibbs	mvi	COMPLETE_ON_QFREEZE_HEAD[1], SCB_LIST_NULL;
1034164174aSJustin T. Gibbs	jmp	idle_loop_checkbus;
1044164174aSJustin T. Gibbscheck_waiting_list:
10517d24755SJustin T. Gibbs	cmp	WAITING_TID_HEAD[1], SCB_LIST_NULL je idle_loop_checkbus;
10617d24755SJustin T. Gibbs	/*
10717d24755SJustin T. Gibbs	 * ENSELO is cleared by a SELDO, so we must test for SELDO
10817d24755SJustin T. Gibbs	 * one last time.
10917d24755SJustin T. Gibbs	 */
11017d24755SJustin T. Gibbs	test	SSTAT0, SELDO jnz select_out;
11117d24755SJustin T. Gibbs	call	start_selection;
11217d24755SJustin T. Gibbsidle_loop_checkbus:
11317d24755SJustin T. Gibbs	test	SSTAT0, SELDO jnz select_out;
11417d24755SJustin T. GibbsEND_CRITICAL;
11517d24755SJustin T. Gibbs	test	SSTAT0, SELDI jnz select_in;
11617d24755SJustin T. Gibbs	test	SCSIPHASE, ~DATA_PHASE_MASK jz idle_loop_check_nonpackreq;
11717d24755SJustin T. Gibbs	test	SCSISIGO, ATNO jz idle_loop_check_nonpackreq;
11817d24755SJustin T. Gibbs	call	unexpected_nonpkt_phase_find_ctxt;
11917d24755SJustin T. Gibbsidle_loop_check_nonpackreq:
1200c5aa4c5SScott Long	test	SSTAT2, NONPACKREQ jz . + 2;
12117d24755SJustin T. Gibbs	call	unexpected_nonpkt_phase_find_ctxt;
122d7cff4abSJustin T. Gibbs	if ((ahd->bugs & AHD_FAINT_LED_BUG) != 0) {
123b3b25f2cSJustin T. Gibbs		/*
124b3b25f2cSJustin T. Gibbs		 * On Rev A. hardware, the busy LED is only
125b3b25f2cSJustin T. Gibbs		 * turned on automaically during selections
126b3b25f2cSJustin T. Gibbs		 * and re-selections.  Make the LED status
127b3b25f2cSJustin T. Gibbs		 * more useful by forcing it to be on so
128b3b25f2cSJustin T. Gibbs		 * long as one of our data FIFOs is active.
129b3b25f2cSJustin T. Gibbs		 */
130d7cff4abSJustin T. Gibbs		and	A, FIFO0FREE|FIFO1FREE, DFFSTAT;
131d7cff4abSJustin T. Gibbs		cmp	A, FIFO0FREE|FIFO1FREE jne . + 3;
132d7cff4abSJustin T. Gibbs		and	SBLKCTL, ~DIAGLEDEN|DIAGLEDON;
133d7cff4abSJustin T. Gibbs		jmp	. + 2;
134d7cff4abSJustin T. Gibbs		or	SBLKCTL, DIAGLEDEN|DIAGLEDON;
135d7cff4abSJustin T. Gibbs	}
1360c5aa4c5SScott Long	call	idle_loop_gsfifo_in_scsi_mode;
1370c5aa4c5SScott Long	call	idle_loop_service_fifos;
1380c5aa4c5SScott Long	call	idle_loop_cchan;
1390c5aa4c5SScott Long	jmp	idle_loop;
1400c5aa4c5SScott Long
1410c5aa4c5SScott Longidle_loop_gsfifo:
1420c5aa4c5SScott Long	SET_MODE(M_SCSI, M_SCSI)
143b3b25f2cSJustin T. GibbsBEGIN_CRITICAL;
1440c5aa4c5SScott Longidle_loop_gsfifo_in_scsi_mode:
1450c5aa4c5SScott Long	test	LQISTAT2, LQIGSAVAIL jz return;
14617d24755SJustin T. Gibbs	/*
14717d24755SJustin T. Gibbs	 * We have received good status for this transaction.  There may
148d7cff4abSJustin T. Gibbs	 * still be data in our FIFOs draining to the host.  Complete
149d7cff4abSJustin T. Gibbs	 * the SCB only if all data has transferred to the host.
15017d24755SJustin T. Gibbs	 */
15117d24755SJustin T. Gibbsgood_status_IU_done:
15217d24755SJustin T. Gibbs	bmov	SCBPTR, GSFIFO, 2;
15317d24755SJustin T. Gibbs	clr	SCB_SCSI_STATUS;
1540794987dSJustin T. Gibbs	/*
1550794987dSJustin T. Gibbs	 * If a command completed before an attempted task management
1560794987dSJustin T. Gibbs	 * function completed, notify the host after disabling any
1570794987dSJustin T. Gibbs	 * pending select-outs.
1580794987dSJustin T. Gibbs	 */
1590794987dSJustin T. Gibbs	test	SCB_TASK_MANAGEMENT, 0xFF jz gsfifo_complete_normally;
1600794987dSJustin T. Gibbs	test	SSTAT0, SELDO|SELINGO jnz . + 2;
1610794987dSJustin T. Gibbs	and	SCSISEQ0, ~ENSELO;
1620794987dSJustin T. Gibbs	SET_SEQINTCODE(TASKMGMT_CMD_CMPLT_OKAY)
1630794987dSJustin T. Gibbsgsfifo_complete_normally:
16417d24755SJustin T. Gibbs	or	SCB_CONTROL, STATUS_RCVD;
16517d24755SJustin T. Gibbs
16617d24755SJustin T. Gibbs	/*
16717d24755SJustin T. Gibbs	 * Since this status did not consume a FIFO, we have to
16817d24755SJustin T. Gibbs	 * be a bit more dilligent in how we check for FIFOs pertaining
169acae33b0SJustin T. Gibbs	 * to this transaction.  There are two states that a FIFO still
17017d24755SJustin T. Gibbs	 * transferring data may be in.
17117d24755SJustin T. Gibbs	 *
172acae33b0SJustin T. Gibbs	 * 1) Configured and draining to the host, with a FIFO handler.
173acae33b0SJustin T. Gibbs	 * 2) Pending cfg4data, fifo not empty.
17417d24755SJustin T. Gibbs	 *
175d7cff4abSJustin T. Gibbs	 * Case 1 can be detected by noticing a non-zero FIFO active
176d7cff4abSJustin T. Gibbs	 * count in the SCB.  In this case, we allow the routine servicing
177d7cff4abSJustin T. Gibbs	 * the FIFO to complete the SCB.
17817d24755SJustin T. Gibbs	 *
179acae33b0SJustin T. Gibbs	 * Case 2 implies either a pending or yet to occur save data
18017d24755SJustin T. Gibbs	 * pointers for this same context in the other FIFO.  So, if
181acae33b0SJustin T. Gibbs	 * we detect case 1, we will properly defer the post of the SCB
18217d24755SJustin T. Gibbs	 * and achieve the desired result.  The pending cfg4data will
18317d24755SJustin T. Gibbs	 * notice that status has been received and complete the SCB.
18417d24755SJustin T. Gibbs	 */
185d7cff4abSJustin T. Gibbs	test	SCB_FIFO_USE_COUNT, 0xFF jnz idle_loop_gsfifo_in_scsi_mode;
18617d24755SJustin T. Gibbs	call	complete;
18717d24755SJustin T. GibbsEND_CRITICAL;
1880c5aa4c5SScott Long	jmp	idle_loop_gsfifo_in_scsi_mode;
1890c5aa4c5SScott Long
19017d24755SJustin T. Gibbsidle_loop_service_fifos:
191c59c8a72SJustin T. Gibbs	SET_MODE(M_DFF0, M_DFF0)
192b3b25f2cSJustin T. GibbsBEGIN_CRITICAL;
19317d24755SJustin T. Gibbs	test	LONGJMP_ADDR[1], INVALID_ADDR jnz idle_loop_next_fifo;
19417d24755SJustin T. Gibbs	call	longjmp;
195b3b25f2cSJustin T. GibbsEND_CRITICAL;
19617d24755SJustin T. Gibbsidle_loop_next_fifo:
197c59c8a72SJustin T. Gibbs	SET_MODE(M_DFF1, M_DFF1)
198b3b25f2cSJustin T. GibbsBEGIN_CRITICAL;
1990c5aa4c5SScott Long	test	LONGJMP_ADDR[1], INVALID_ADDR jz longjmp;
200b3b25f2cSJustin T. GibbsEND_CRITICAL;
201d7cff4abSJustin T. Gibbsreturn:
2020c5aa4c5SScott Long	ret;
20317d24755SJustin T. Gibbs
20417d24755SJustin T. Gibbsidle_loop_cchan:
205c59c8a72SJustin T. Gibbs	SET_MODE(M_CCHAN, M_CCHAN)
2060794987dSJustin T. Gibbs	test	QOFF_CTLSTA, HS_MAILBOX_ACT jz	hs_mailbox_empty;
2070794987dSJustin T. Gibbs	or	QOFF_CTLSTA, HS_MAILBOX_ACT;
208b3b25f2cSJustin T. Gibbs	mov	LOCAL_HS_MAILBOX, HS_MAILBOX;
2090794987dSJustin T. Gibbshs_mailbox_empty:
210c59c8a72SJustin T. GibbsBEGIN_CRITICAL;
21117d24755SJustin T. Gibbs	test	CCSCBCTL, CCARREN|CCSCBEN jz scbdma_idle;
21217d24755SJustin T. Gibbs	test	CCSCBCTL, CCSCBDIR jnz fetch_new_scb_inprog;
21317d24755SJustin T. Gibbs	test	CCSCBCTL, CCSCBDONE jz return;
21417d24755SJustin T. Gibbs	/* FALLTHROUGH */
21517d24755SJustin T. Gibbsscbdma_tohost_done:
21617d24755SJustin T. Gibbs	test	CCSCBCTL, CCARREN jz fill_qoutfifo_dmadone;
21717d24755SJustin T. Gibbs	/*
218594c945aSPedro F. Giffuni	 * An SCB has been successfully uploaded to the host.
219d7cff4abSJustin T. Gibbs	 * If the SCB was uploaded for some reason other than
220d7cff4abSJustin T. Gibbs	 * bad SCSI status (currently only for underruns), we
221d7cff4abSJustin T. Gibbs	 * queue the SCB for normal completion.  Otherwise, we
222d7cff4abSJustin T. Gibbs	 * wait until any select-out activity has halted, and
223b3b25f2cSJustin T. Gibbs	 * then queue the completion.
22417d24755SJustin T. Gibbs	 */
225d7cff4abSJustin T. Gibbs	and	CCSCBCTL, ~(CCARREN|CCSCBEN);
226d7cff4abSJustin T. Gibbs	bmov	COMPLETE_DMA_SCB_HEAD, SCB_NEXT_COMPLETE, 2;
2274164174aSJustin T. Gibbs	cmp	SCB_NEXT_COMPLETE[1], SCB_LIST_NULL jne . + 2;
2284164174aSJustin T. Gibbs	mvi	COMPLETE_DMA_SCB_TAIL[1], SCB_LIST_NULL;
2294164174aSJustin T. Gibbs	test	SCB_SCSI_STATUS, 0xff jz scbdma_queue_completion;
2304164174aSJustin T. Gibbs	bmov	SCB_NEXT_COMPLETE, COMPLETE_ON_QFREEZE_HEAD, 2;
2314164174aSJustin T. Gibbs	bmov	COMPLETE_ON_QFREEZE_HEAD, SCBPTR, 2 ret;
2324164174aSJustin T. Gibbsscbdma_queue_completion:
233b3b25f2cSJustin T. Gibbs	bmov	SCB_NEXT_COMPLETE, COMPLETE_SCB_HEAD, 2;
234b3b25f2cSJustin T. Gibbs	bmov	COMPLETE_SCB_HEAD, SCBPTR, 2 ret;
23517d24755SJustin T. Gibbsfill_qoutfifo_dmadone:
23617d24755SJustin T. Gibbs	and	CCSCBCTL, ~(CCARREN|CCSCBEN);
2370794987dSJustin T. Gibbs	call	qoutfifo_updated;
23817d24755SJustin T. Gibbs	mvi	COMPLETE_SCB_DMAINPROG_HEAD[1], SCB_LIST_NULL;
23917d24755SJustin T. Gibbs	bmov	QOUTFIFO_NEXT_ADDR, SCBHADDR, 4;
24017d24755SJustin T. Gibbs	test	QOFF_CTLSTA, SDSCB_ROLLOVR jz return;
2418fdc1261SJustin T. Gibbs	bmov	QOUTFIFO_NEXT_ADDR, SHARED_DATA_ADDR, 4;
2428fdc1261SJustin T. Gibbs	xor	QOUTFIFO_ENTRY_VALID_TAG, QOUTFIFO_ENTRY_VALID_TOGGLE ret;
243b3b25f2cSJustin T. GibbsEND_CRITICAL;
24417d24755SJustin T. Gibbs
2450794987dSJustin T. Gibbsqoutfifo_updated:
2460794987dSJustin T. Gibbs	/*
2470794987dSJustin T. Gibbs	 * If there are more commands waiting to be dma'ed
2488089f0f0SJustin T. Gibbs	 * to the host, always coalesce.  Otherwise honor the
2490794987dSJustin T. Gibbs	 * host's wishes.
2500794987dSJustin T. Gibbs	 */
2518089f0f0SJustin T. Gibbs	cmp	COMPLETE_DMA_SCB_HEAD[1], SCB_LIST_NULL jne coalesce_by_count;
2528089f0f0SJustin T. Gibbs	cmp	COMPLETE_SCB_HEAD[1], SCB_LIST_NULL jne coalesce_by_count;
2538089f0f0SJustin T. Gibbs	test	LOCAL_HS_MAILBOX, ENINT_COALESCE jz issue_cmdcmplt;
2540794987dSJustin T. Gibbs
2550794987dSJustin T. Gibbs	/*
2560794987dSJustin T. Gibbs	 * If we have relatively few commands outstanding, don't
2570794987dSJustin T. Gibbs	 * bother waiting for another command to complete.
2580794987dSJustin T. Gibbs	 */
2598089f0f0SJustin T. Gibbs	test	CMDS_PENDING[1], 0xFF jnz coalesce_by_count;
2600794987dSJustin T. Gibbs	/* Add -1 so that jnc means <= not just < */
2618089f0f0SJustin T. Gibbs	add	A, -1, INT_COALESCING_MINCMDS;
2620794987dSJustin T. Gibbs	add	NONE, A, CMDS_PENDING;
2630794987dSJustin T. Gibbs	jnc	issue_cmdcmplt;
2640794987dSJustin T. Gibbs
2650794987dSJustin T. Gibbs	/*
2668089f0f0SJustin T. Gibbs	 * If coalescing, only coalesce up to the limit
2670794987dSJustin T. Gibbs	 * provided by the host driver.
2680794987dSJustin T. Gibbs	 */
2698089f0f0SJustin T. Gibbscoalesce_by_count:
2708089f0f0SJustin T. Gibbs	mov	A, INT_COALESCING_MAXCMDS;
2718089f0f0SJustin T. Gibbs	add	NONE, A, INT_COALESCING_CMDCOUNT;
2720794987dSJustin T. Gibbs	jc	issue_cmdcmplt;
2730794987dSJustin T. Gibbs	/*
2740794987dSJustin T. Gibbs	 * If the timer is not currently active,
2750794987dSJustin T. Gibbs	 * fire it up.
2760794987dSJustin T. Gibbs	 */
2770794987dSJustin T. Gibbs	test	INTCTL, SWTMINTMASK jz return;
2788089f0f0SJustin T. Gibbs	bmov	SWTIMER, INT_COALESCING_TIMER, 2;
2790794987dSJustin T. Gibbs	mvi	CLRSEQINTSTAT, CLRSEQ_SWTMRTO;
2800794987dSJustin T. Gibbs	or	INTCTL, SWTMINTEN|SWTIMER_START;
2810794987dSJustin T. Gibbs	and	INTCTL, ~SWTMINTMASK ret;
2820794987dSJustin T. Gibbs
2830794987dSJustin T. Gibbsissue_cmdcmplt:
2840794987dSJustin T. Gibbs	mvi	INTSTAT, CMDCMPLT;
2858089f0f0SJustin T. Gibbs	clr	INT_COALESCING_CMDCOUNT;
2860794987dSJustin T. Gibbs	or	INTCTL, SWTMINTMASK ret;
2870794987dSJustin T. Gibbs
288c59c8a72SJustin T. GibbsBEGIN_CRITICAL;
28917d24755SJustin T. Gibbsfetch_new_scb_inprog:
29017d24755SJustin T. Gibbs	test	CCSCBCTL, ARRDONE jz return;
29117d24755SJustin T. Gibbsfetch_new_scb_done:
29217d24755SJustin T. Gibbs	and	CCSCBCTL, ~(CCARREN|CCSCBEN);
2930794987dSJustin T. Gibbs	clr	A;
2940794987dSJustin T. Gibbs	add	CMDS_PENDING, 1;
2950794987dSJustin T. Gibbs	adc	CMDS_PENDING[1], A;
296a3f571b8SJustin T. Gibbs	if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
297a3f571b8SJustin T. Gibbs		/*
298a3f571b8SJustin T. Gibbs		 * "Short Luns" are not placed into outgoing LQ
299a3f571b8SJustin T. Gibbs		 * packets in the correct byte order.  Use a full
300a3f571b8SJustin T. Gibbs		 * sized lun field instead and fill it with the
301a3f571b8SJustin T. Gibbs		 * one byte of lun information we support.
302a3f571b8SJustin T. Gibbs		 */
303a3f571b8SJustin T. Gibbs		mov	SCB_PKT_LUN[6], SCB_LUN;
304a3f571b8SJustin T. Gibbs	}
305d7cff4abSJustin T. Gibbs	/*
306d7cff4abSJustin T. Gibbs	 * The FIFO use count field is shared with the
307d7cff4abSJustin T. Gibbs	 * tag set by the host so that our SCB dma engine
308d7cff4abSJustin T. Gibbs	 * knows the correct location to store the SCB.
309d7cff4abSJustin T. Gibbs	 * Set it to zero before processing the SCB.
310d7cff4abSJustin T. Gibbs	 */
31183441ad3SJustin T. Gibbs	clr	SCB_FIFO_USE_COUNT;
31217d24755SJustin T. Gibbs	/* Update the next SCB address to download. */
31317d24755SJustin T. Gibbs	bmov	NEXT_QUEUED_SCB_ADDR, SCB_NEXT_SCB_BUSADDR, 4;
314789902c3SJustin T. Gibbs	/*
315789902c3SJustin T. Gibbs	 * NULL out the SCB links since these fields
316789902c3SJustin T. Gibbs	 * occupy the same location as SCB_NEXT_SCB_BUSADDR.
317789902c3SJustin T. Gibbs	 */
31817d24755SJustin T. Gibbs	mvi	SCB_NEXT[1], SCB_LIST_NULL;
31917d24755SJustin T. Gibbs	mvi	SCB_NEXT2[1], SCB_LIST_NULL;
3200c5aa4c5SScott Long	/* Increment our position in the QINFIFO. */
3210c5aa4c5SScott Long	mov	NONE, SNSCB_QOFF;
322789902c3SJustin T. Gibbs
32317d24755SJustin T. Gibbs	/*
324789902c3SJustin T. Gibbs	 * Save SCBID of this SCB in REG0 since
325789902c3SJustin T. Gibbs	 * SCBPTR will be clobbered during target
326789902c3SJustin T. Gibbs	 * list updates.  We also record the SCB's
327789902c3SJustin T. Gibbs	 * flags so that we can refer to them even
328789902c3SJustin T. Gibbs	 * after SCBPTR has been changed.
32917d24755SJustin T. Gibbs	 */
330789902c3SJustin T. Gibbs	bmov	REG0, SCBPTR, 2;
331789902c3SJustin T. Gibbs	mov	A, SCB_CONTROL;
332789902c3SJustin T. Gibbs
333789902c3SJustin T. Gibbs	/*
334789902c3SJustin T. Gibbs	 * Find the tail SCB of the execution queue
335789902c3SJustin T. Gibbs	 * for this target.
336789902c3SJustin T. Gibbs	 */
33717d24755SJustin T. Gibbs	shr	SINDEX, 3, SCB_SCSIID;
33817d24755SJustin T. Gibbs	and	SINDEX, ~0x1;
33917d24755SJustin T. Gibbs	mvi	SINDEX[1], (WAITING_SCB_TAILS >> 8);
34017d24755SJustin T. Gibbs	bmov	DINDEX, SINDEX, 2;
34117d24755SJustin T. Gibbs	bmov	SCBPTR, SINDIR, 2;
342789902c3SJustin T. Gibbs
343789902c3SJustin T. Gibbs	/*
344789902c3SJustin T. Gibbs	 * Update the tail to point to the new SCB.
345789902c3SJustin T. Gibbs	 */
34617d24755SJustin T. Gibbs	bmov	DINDIR, REG0, 2;
347789902c3SJustin T. Gibbs
348789902c3SJustin T. Gibbs	/*
349789902c3SJustin T. Gibbs	 * If the queue was empty, queue this SCB as
350789902c3SJustin T. Gibbs	 * the first for this target.
351789902c3SJustin T. Gibbs	 */
35217d24755SJustin T. Gibbs	cmp	SCBPTR[1], SCB_LIST_NULL je first_new_target_scb;
353789902c3SJustin T. Gibbs
354789902c3SJustin T. Gibbs	/*
355789902c3SJustin T. Gibbs	 * SCBs that want to send messages must always be
356789902c3SJustin T. Gibbs	 * at the head of their per-target queue so that
357789902c3SJustin T. Gibbs	 * ATN can be asserted even if the current
358789902c3SJustin T. Gibbs	 * negotiation agreement is packetized.  If the
359789902c3SJustin T. Gibbs	 * target queue is empty, the SCB can be queued
360789902c3SJustin T. Gibbs	 * immediately.  If the queue is not empty, we must
361789902c3SJustin T. Gibbs	 * wait for it to empty before entering this SCB
362789902c3SJustin T. Gibbs	 * into the waiting for selection queue.  Otherwise
363789902c3SJustin T. Gibbs	 * our batching and round-robin selection scheme
364789902c3SJustin T. Gibbs	 * could allow commands to be queued out of order.
365789902c3SJustin T. Gibbs	 * To simplify the implementation, we stop pulling
366789902c3SJustin T. Gibbs	 * new commands from the host until the MK_MESSAGE
367789902c3SJustin T. Gibbs	 * SCB can be queued to the waiting for selection
368789902c3SJustin T. Gibbs	 * list.
369789902c3SJustin T. Gibbs	 */
370789902c3SJustin T. Gibbs	test	A, MK_MESSAGE jz batch_scb;
371789902c3SJustin T. Gibbs
372789902c3SJustin T. Gibbs	/*
373789902c3SJustin T. Gibbs	 * If the last SCB is also a MK_MESSAGE SCB, then
374789902c3SJustin T. Gibbs	 * order is preserved even if we batch.
375789902c3SJustin T. Gibbs	 */
376789902c3SJustin T. Gibbs	test	SCB_CONTROL, MK_MESSAGE jz batch_scb;
377789902c3SJustin T. Gibbs
378789902c3SJustin T. Gibbs	/*
379789902c3SJustin T. Gibbs	 * Defer this SCB and stop fetching new SCBs until
380789902c3SJustin T. Gibbs	 * it can be queued.  Since the SCB_SCSIID of the
381789902c3SJustin T. Gibbs	 * tail SCB must be the same as that of the newly
382789902c3SJustin T. Gibbs	 * queued SCB, there is no need to restore the SCBID
383789902c3SJustin T. Gibbs	 * here.
384789902c3SJustin T. Gibbs	 */
385789902c3SJustin T. Gibbs	or	SEQ_FLAGS2, PENDING_MK_MESSAGE;
386789902c3SJustin T. Gibbs	bmov	MK_MESSAGE_SCB, REG0, 2;
387789902c3SJustin T. Gibbs	mov	MK_MESSAGE_SCSIID, SCB_SCSIID ret;
388789902c3SJustin T. Gibbs
389789902c3SJustin T. Gibbsbatch_scb:
390789902c3SJustin T. Gibbs	/*
391789902c3SJustin T. Gibbs	 * Otherwise just update the previous tail SCB to
392789902c3SJustin T. Gibbs	 * point to the new tail.
393789902c3SJustin T. Gibbs	 */
3940c5aa4c5SScott Long	bmov	SCB_NEXT, REG0, 2 ret;
395789902c3SJustin T. Gibbs
39617d24755SJustin T. Gibbsfirst_new_target_scb:
397789902c3SJustin T. Gibbs	/*
398789902c3SJustin T. Gibbs	 * Append SCB to the tail of the waiting for
399789902c3SJustin T. Gibbs	 * selection list.
400789902c3SJustin T. Gibbs	 */
40117d24755SJustin T. Gibbs	cmp	WAITING_TID_HEAD[1], SCB_LIST_NULL je first_new_scb;
40217d24755SJustin T. Gibbs	bmov	SCBPTR, WAITING_TID_TAIL, 2;
40317d24755SJustin T. Gibbs	bmov	SCB_NEXT2, REG0, 2;
4040c5aa4c5SScott Long	bmov	WAITING_TID_TAIL, REG0, 2 ret;
40517d24755SJustin T. Gibbsfirst_new_scb:
406789902c3SJustin T. Gibbs	/*
407789902c3SJustin T. Gibbs	 * Whole list is empty, so the head of
408789902c3SJustin T. Gibbs	 * the list must be initialized too.
409789902c3SJustin T. Gibbs	 */
41017d24755SJustin T. Gibbs	bmov	WAITING_TID_HEAD, REG0, 2;
4110c5aa4c5SScott Long	bmov	WAITING_TID_TAIL, REG0, 2 ret;
412c59c8a72SJustin T. GibbsEND_CRITICAL;
41317d24755SJustin T. Gibbs
41417d24755SJustin T. Gibbsscbdma_idle:
41517d24755SJustin T. Gibbs	/*
416789902c3SJustin T. Gibbs	 * Don't bother downloading new SCBs to execute
417789902c3SJustin T. Gibbs	 * if select-outs are currently frozen or we have
418789902c3SJustin T. Gibbs	 * a MK_MESSAGE SCB waiting to enter the queue.
41917d24755SJustin T. Gibbs	 */
420789902c3SJustin T. Gibbs	test	SEQ_FLAGS2, SELECTOUT_QFROZEN|PENDING_MK_MESSAGE
421789902c3SJustin T. Gibbs		jnz scbdma_no_new_scbs;
42217d24755SJustin T. GibbsBEGIN_CRITICAL;
42317d24755SJustin T. Gibbs	test	QOFF_CTLSTA, NEW_SCB_AVAIL jnz fetch_new_scb;
424789902c3SJustin T. Gibbsscbdma_no_new_scbs:
42517d24755SJustin T. Gibbs	cmp	COMPLETE_DMA_SCB_HEAD[1], SCB_LIST_NULL jne dma_complete_scb;
42617d24755SJustin T. Gibbs	cmp	COMPLETE_SCB_HEAD[1], SCB_LIST_NULL je return;
42717d24755SJustin T. Gibbs	/* FALLTHROUGH */
42817d24755SJustin T. Gibbsfill_qoutfifo:
42917d24755SJustin T. Gibbs	/*
43017d24755SJustin T. Gibbs	 * Keep track of the SCBs we are dmaing just
43117d24755SJustin T. Gibbs	 * in case the DMA fails or is aborted.
43217d24755SJustin T. Gibbs	 */
43317d24755SJustin T. Gibbs	bmov	COMPLETE_SCB_DMAINPROG_HEAD, COMPLETE_SCB_HEAD, 2;
43417d24755SJustin T. Gibbs	mvi	CCSCBCTL, CCSCBRESET;
43517d24755SJustin T. Gibbs	bmov	SCBHADDR, QOUTFIFO_NEXT_ADDR, 4;
43622dbd4c6SJustin T. Gibbs	mov	A, QOUTFIFO_NEXT_ADDR;
43717d24755SJustin T. Gibbs	bmov	SCBPTR, COMPLETE_SCB_HEAD, 2;
43817d24755SJustin T. Gibbsfill_qoutfifo_loop:
4394164174aSJustin T. Gibbs	bmov	CCSCBRAM, SCBPTR, 2;
4404164174aSJustin T. Gibbs	mov	CCSCBRAM, SCB_SGPTR[0];
4414164174aSJustin T. Gibbs	mov	CCSCBRAM, QOUTFIFO_ENTRY_VALID_TAG;
44217d24755SJustin T. Gibbs	mov	NONE, SDSCB_QOFF;
4438089f0f0SJustin T. Gibbs	inc	INT_COALESCING_CMDCOUNT;
4440794987dSJustin T. Gibbs	add	CMDS_PENDING, -1;
4450794987dSJustin T. Gibbs	adc	CMDS_PENDING[1], -1;
44617d24755SJustin T. Gibbs	cmp	SCB_NEXT_COMPLETE[1], SCB_LIST_NULL je fill_qoutfifo_done;
44717d24755SJustin T. Gibbs	cmp	CCSCBADDR, CCSCBADDR_MAX je fill_qoutfifo_done;
4480c5aa4c5SScott Long	test	QOFF_CTLSTA, SDSCB_ROLLOVR jnz fill_qoutfifo_done;
44922dbd4c6SJustin T. Gibbs	/*
45022dbd4c6SJustin T. Gibbs	 * Don't cross an ADB or Cachline boundary when DMA'ing
45122dbd4c6SJustin T. Gibbs	 * completion entries.  In PCI mode, at least in 32/33
45222dbd4c6SJustin T. Gibbs	 * configurations, the SCB DMA engine may lose its place
45322dbd4c6SJustin T. Gibbs	 * in the data-stream should the target force a retry on
45422dbd4c6SJustin T. Gibbs	 * something other than an 8byte aligned boundary. In
45522dbd4c6SJustin T. Gibbs	 * PCI-X mode, we do this to avoid split transactions since
45622dbd4c6SJustin T. Gibbs	 * many chipsets seem to be unable to format proper split
45722dbd4c6SJustin T. Gibbs	 * completions to continue the data transfer.
45822dbd4c6SJustin T. Gibbs	 */
45922dbd4c6SJustin T. Gibbs	add	SINDEX, A, CCSCBADDR;
46022dbd4c6SJustin T. Gibbs	test	SINDEX, CACHELINE_MASK jz fill_qoutfifo_done;
4610c5aa4c5SScott Long	bmov	SCBPTR, SCB_NEXT_COMPLETE, 2;
4620c5aa4c5SScott Long	jmp	fill_qoutfifo_loop;
46317d24755SJustin T. Gibbsfill_qoutfifo_done:
46417d24755SJustin T. Gibbs	mov	SCBHCNT, CCSCBADDR;
46517d24755SJustin T. Gibbs	mvi	CCSCBCTL, CCSCBEN|CCSCBRESET;
46617d24755SJustin T. Gibbs	bmov	COMPLETE_SCB_HEAD, SCB_NEXT_COMPLETE, 2;
46717d24755SJustin T. Gibbs	mvi	SCB_NEXT_COMPLETE[1], SCB_LIST_NULL ret;
46817d24755SJustin T. Gibbs
46917d24755SJustin T. Gibbsfetch_new_scb:
47017d24755SJustin T. Gibbs	bmov	SCBHADDR, NEXT_QUEUED_SCB_ADDR, 4;
47117d24755SJustin T. Gibbs	mvi	CCARREN|CCSCBEN|CCSCBDIR|CCSCBRESET jmp dma_scb;
47217d24755SJustin T. Gibbsdma_complete_scb:
47317d24755SJustin T. Gibbs	bmov	SCBPTR, COMPLETE_DMA_SCB_HEAD, 2;
47417d24755SJustin T. Gibbs	bmov	SCBHADDR, SCB_BUSADDR, 4;
475d7cff4abSJustin T. Gibbs	mvi	CCARREN|CCSCBEN|CCSCBRESET jmp dma_scb;
47617d24755SJustin T. Gibbs
47717d24755SJustin T. Gibbs/*
47817d24755SJustin T. Gibbs * Either post or fetch an SCB from host memory.  The caller
47917d24755SJustin T. Gibbs * is responsible for polling for transfer completion.
48017d24755SJustin T. Gibbs *
48117d24755SJustin T. Gibbs * Prerequisits: Mode == M_CCHAN
48217d24755SJustin T. Gibbs *		 SINDEX contains CCSCBCTL flags
48317d24755SJustin T. Gibbs *		 SCBHADDR set to Host SCB address
48417d24755SJustin T. Gibbs *		 SCBPTR set to SCB src location on "push" operations
48517d24755SJustin T. Gibbs */
48617d24755SJustin T. GibbsSET_SRC_MODE	M_CCHAN;
48717d24755SJustin T. GibbsSET_DST_MODE	M_CCHAN;
48817d24755SJustin T. Gibbsdma_scb:
48917d24755SJustin T. Gibbs	mvi	SCBHCNT, SCB_TRANSFER_SIZE;
490c59c8a72SJustin T. Gibbs	mov	CCSCBCTL, SINDEX ret;
49117d24755SJustin T. Gibbs
49217d24755SJustin T. Gibbssetjmp:
4934164174aSJustin T. Gibbs	/*
4944164174aSJustin T. Gibbs	 * At least on the A, a return in the same
4954164174aSJustin T. Gibbs	 * instruction as the bmov results in a return
4964164174aSJustin T. Gibbs	 * to the caller, not to the new address at the
4974164174aSJustin T. Gibbs	 * top of the stack.  Since we want the latter
4984164174aSJustin T. Gibbs	 * (we use setjmp to register a handler from an
4994164174aSJustin T. Gibbs	 * interrupt context but not invoke that handler
5004164174aSJustin T. Gibbs	 * until we return to our idle loop), use a
5014164174aSJustin T. Gibbs	 * separate ret instruction.
5024164174aSJustin T. Gibbs	 */
5034164174aSJustin T. Gibbs	bmov	LONGJMP_ADDR, STACK, 2;
5044164174aSJustin T. Gibbs	ret;
50517d24755SJustin T. Gibbssetjmp_inline:
50617d24755SJustin T. Gibbs	bmov	LONGJMP_ADDR, STACK, 2;
50717d24755SJustin T. Gibbslongjmp:
50817d24755SJustin T. Gibbs	bmov	STACK, LONGJMP_ADDR, 2 ret;
50917d24755SJustin T. GibbsEND_CRITICAL;
51017d24755SJustin T. Gibbs
5110c5aa4c5SScott Long/*************************** Chip Bug Work Arounds ****************************/
51217d24755SJustin T. Gibbs/*
51317d24755SJustin T. Gibbs * Must disable interrupts when setting the mode pointer
51417d24755SJustin T. Gibbs * register as an interrupt occurring mid update will
51517d24755SJustin T. Gibbs * fail to store the new mode value for restoration on
51617d24755SJustin T. Gibbs * an iret.
51717d24755SJustin T. Gibbs */
518c59c8a72SJustin T. Gibbsif ((ahd->bugs & AHD_SET_MODE_BUG) != 0) {
51917d24755SJustin T. Gibbsset_mode_work_around:
52017d24755SJustin T. Gibbs	mvi	SEQINTCTL, INTVEC1DSL;
52117d24755SJustin T. Gibbs	mov	MODE_PTR, SINDEX;
52217d24755SJustin T. Gibbs	clr	SEQINTCTL ret;
523c59c8a72SJustin T. Gibbs}
524c59c8a72SJustin T. Gibbs
5250c5aa4c5SScott Long
5260c5aa4c5SScott Longif ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
5270c5aa4c5SScott Longset_seqint_work_around:
5280c5aa4c5SScott Long	mov	SEQINTCODE, SINDEX;
5290c5aa4c5SScott Long	mvi	SEQINTCODE, NO_SEQINT ret;
5300c5aa4c5SScott Long}
5310c5aa4c5SScott Long
5320c5aa4c5SScott Long/************************ Packetized LongJmp Routines *************************/
53317d24755SJustin T. GibbsSET_SRC_MODE	M_SCSI;
53417d24755SJustin T. GibbsSET_DST_MODE	M_SCSI;
53517d24755SJustin T. Gibbsstart_selection:
53617d24755SJustin T. GibbsBEGIN_CRITICAL;
53717d24755SJustin T. Gibbs	if ((ahd->bugs & AHD_SENT_SCB_UPDATE_BUG) != 0) {
53817d24755SJustin T. Gibbs		/*
53917d24755SJustin T. Gibbs		 * Razor #494
54017d24755SJustin T. Gibbs		 * Rev A hardware fails to update LAST/CURR/NEXTSCB
54117d24755SJustin T. Gibbs		 * correctly after a packetized selection in several
54217d24755SJustin T. Gibbs		 * situations:
54317d24755SJustin T. Gibbs		 *
5448fdc1261SJustin T. Gibbs		 * 1) If only one command existed in the queue, the
54517d24755SJustin T. Gibbs		 *    LAST/CURR/NEXTSCB are unchanged.
54617d24755SJustin T. Gibbs		 *
54717d24755SJustin T. Gibbs		 * 2) In a non QAS, protocol allowed phase change,
54817d24755SJustin T. Gibbs		 *    the queue is shifted 1 too far.  LASTSCB is
54917d24755SJustin T. Gibbs		 *    the last SCB that was correctly processed.
55017d24755SJustin T. Gibbs		 *
55117d24755SJustin T. Gibbs		 * 3) In the QAS case, if the full list of commands
55217d24755SJustin T. Gibbs		 *    was successfully sent, NEXTSCB is NULL and neither
55317d24755SJustin T. Gibbs		 *    CURRSCB nor LASTSCB can be trusted.  We must
55417d24755SJustin T. Gibbs		 *    manually walk the list counting MAXCMDCNT elements
55517d24755SJustin T. Gibbs		 *    to find the last SCB that was sent correctly.
55617d24755SJustin T. Gibbs		 *
55717d24755SJustin T. Gibbs		 * To simplify the workaround for this bug in SELDO
55817d24755SJustin T. Gibbs		 * handling, we initialize LASTSCB prior to enabling
55917d24755SJustin T. Gibbs		 * selection so we can rely on it even for case #1 above.
56017d24755SJustin T. Gibbs		 */
56117d24755SJustin T. Gibbs		bmov	LASTSCB, WAITING_TID_HEAD, 2;
56217d24755SJustin T. Gibbs	}
56317d24755SJustin T. Gibbs	bmov	CURRSCB, WAITING_TID_HEAD, 2;
56417d24755SJustin T. Gibbs	bmov	SCBPTR, WAITING_TID_HEAD, 2;
56517d24755SJustin T. Gibbs	shr	SELOID, 4, SCB_SCSIID;
56617d24755SJustin T. Gibbs	/*
56717d24755SJustin T. Gibbs	 * If we want to send a message to the device, ensure
56817d24755SJustin T. Gibbs	 * we are selecting with atn irregardless of our packetized
56917d24755SJustin T. Gibbs	 * agreement.  Since SPI4 only allows target reset or PPR
57017d24755SJustin T. Gibbs	 * messages if this is a packetized connection, the change
57117d24755SJustin T. Gibbs	 * to our negotiation table entry for this selection will
57217d24755SJustin T. Gibbs	 * be cleared when the message is acted on.
57317d24755SJustin T. Gibbs	 */
57417d24755SJustin T. Gibbs	test	SCB_CONTROL, MK_MESSAGE jz . + 3;
57517d24755SJustin T. Gibbs	mov	NEGOADDR, SELOID;
57617d24755SJustin T. Gibbs	or	NEGCONOPTS, ENAUTOATNO;
57717d24755SJustin T. Gibbs	or	SCSISEQ0, ENSELO ret;
57817d24755SJustin T. GibbsEND_CRITICAL;
57917d24755SJustin T. Gibbs
58017d24755SJustin T. Gibbs/*
58117d24755SJustin T. Gibbs * Allocate a FIFO for a non-packetized transaction.
5820c5aa4c5SScott Long * In RevA hardware, both FIFOs must be free before we
5830c5aa4c5SScott Long * can allocate a FIFO for a non-packetized transaction.
58417d24755SJustin T. Gibbs */
58517d24755SJustin T. Gibbsallocate_fifo_loop:
58617d24755SJustin T. Gibbs	/*
58717d24755SJustin T. Gibbs	 * Do whatever work is required to free a FIFO.
58817d24755SJustin T. Gibbs	 */
5890c5aa4c5SScott Long	call	idle_loop_service_fifos;
590c59c8a72SJustin T. Gibbs	SET_MODE(M_SCSI, M_SCSI)
59117d24755SJustin T. Gibbsallocate_fifo:
5920c5aa4c5SScott Long	if ((ahd->bugs & AHD_NONPACKFIFO_BUG) != 0) {
59317d24755SJustin T. Gibbs		and	A, FIFO0FREE|FIFO1FREE, DFFSTAT;
59417d24755SJustin T. Gibbs		cmp	A, FIFO0FREE|FIFO1FREE jne allocate_fifo_loop;
5950c5aa4c5SScott Long	} else {
5960c5aa4c5SScott Long		test	DFFSTAT, FIFO1FREE jnz allocate_fifo1;
5970c5aa4c5SScott Long		test	DFFSTAT, FIFO0FREE jz allocate_fifo_loop;
5980c5aa4c5SScott Long		mvi	DFFSTAT, B_CURRFIFO_0;
5990c5aa4c5SScott Long		SET_MODE(M_DFF0, M_DFF0)
6000c5aa4c5SScott Long		bmov	SCBPTR, ALLOCFIFO_SCBPTR, 2 ret;
6010c5aa4c5SScott Long	}
6020c5aa4c5SScott LongSET_SRC_MODE	M_SCSI;
6030c5aa4c5SScott LongSET_DST_MODE	M_SCSI;
6040c5aa4c5SScott Longallocate_fifo1:
6050c5aa4c5SScott Long	mvi	DFFSTAT, CURRFIFO_1;
606c59c8a72SJustin T. Gibbs	SET_MODE(M_DFF1, M_DFF1)
607c59c8a72SJustin T. Gibbs	bmov	SCBPTR, ALLOCFIFO_SCBPTR, 2 ret;
60817d24755SJustin T. Gibbs
60917d24755SJustin T. Gibbs/*
61017d24755SJustin T. Gibbs * We have been reselected as an initiator
61117d24755SJustin T. Gibbs * or selected as a target.
61217d24755SJustin T. Gibbs */
61317d24755SJustin T. GibbsSET_SRC_MODE	M_SCSI;
61417d24755SJustin T. GibbsSET_DST_MODE	M_SCSI;
61517d24755SJustin T. Gibbsselect_in:
616b3b25f2cSJustin T. Gibbs	if ((ahd->bugs & AHD_FAINT_LED_BUG) != 0) {
617b3b25f2cSJustin T. Gibbs		/*
618b3b25f2cSJustin T. Gibbs		 * On Rev A. hardware, the busy LED is only
619b3b25f2cSJustin T. Gibbs		 * turned on automaically during selections
620b3b25f2cSJustin T. Gibbs		 * and re-selections.  Make the LED status
621b3b25f2cSJustin T. Gibbs		 * more useful by forcing it to be on from
622b3b25f2cSJustin T. Gibbs		 * the point of selection until our idle
623b3b25f2cSJustin T. Gibbs		 * loop determines that neither of our FIFOs
624b3b25f2cSJustin T. Gibbs		 * are busy.  This handles the non-packetized
625b3b25f2cSJustin T. Gibbs		 * case nicely as we will not return to the
626b3b25f2cSJustin T. Gibbs		 * idle loop until the busfree at the end of
627b3b25f2cSJustin T. Gibbs		 * each transaction.
628b3b25f2cSJustin T. Gibbs		 */
629b3b25f2cSJustin T. Gibbs		or	SBLKCTL, DIAGLEDEN|DIAGLEDON;
630b3b25f2cSJustin T. Gibbs	}
631c59c8a72SJustin T. Gibbs	if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
632c59c8a72SJustin T. Gibbs		/*
633b3b25f2cSJustin T. Gibbs		 * Test to ensure that the bus has not
634b3b25f2cSJustin T. Gibbs		 * already gone free prior to clearing
635b3b25f2cSJustin T. Gibbs		 * any stale busfree status.  This avoids
636b3b25f2cSJustin T. Gibbs		 * a window whereby a busfree just after
637b3b25f2cSJustin T. Gibbs		 * a selection could be missed.
638c59c8a72SJustin T. Gibbs		 */
639b3b25f2cSJustin T. Gibbs		test	SCSISIGI, BSYI jz . + 2;
640c59c8a72SJustin T. Gibbs		mvi	CLRSINT1,CLRBUSFREE;
641c59c8a72SJustin T. Gibbs		or	SIMODE1, ENBUSFREE;
642c59c8a72SJustin T. Gibbs	}
64317d24755SJustin T. Gibbs	or	SXFRCTL0, SPIOEN;
64417d24755SJustin T. Gibbs	and	SAVED_SCSIID, SELID_MASK, SELID;
64517d24755SJustin T. Gibbs	and	A, OID, IOWNID;
64617d24755SJustin T. Gibbs	or	SAVED_SCSIID, A;
64717d24755SJustin T. Gibbs	mvi	CLRSINT0, CLRSELDI;
64817d24755SJustin T. Gibbs	jmp	ITloop;
64917d24755SJustin T. Gibbs
65017d24755SJustin T. Gibbs/*
65117d24755SJustin T. Gibbs * We have successfully selected out.
65217d24755SJustin T. Gibbs *
65317d24755SJustin T. Gibbs * Clear SELDO.
65417d24755SJustin T. Gibbs * Dequeue all SCBs sent from the waiting queue
65517d24755SJustin T. Gibbs * Requeue all SCBs *not* sent to the tail of the waiting queue
65617d24755SJustin T. Gibbs * Take Razor #494 into account for above.
65717d24755SJustin T. Gibbs *
65817d24755SJustin T. Gibbs * In Packetized Mode:
65917d24755SJustin T. Gibbs *	Return to the idle loop.  Our interrupt handler will take
66017d24755SJustin T. Gibbs *	care of any incoming L_Qs.
66117d24755SJustin T. Gibbs *
66217d24755SJustin T. Gibbs * In Non-Packetize Mode:
66317d24755SJustin T. Gibbs *	Continue to our normal state machine.
66417d24755SJustin T. Gibbs */
66517d24755SJustin T. GibbsSET_SRC_MODE	M_SCSI;
66617d24755SJustin T. GibbsSET_DST_MODE	M_SCSI;
66717d24755SJustin T. Gibbsselect_out:
66817d24755SJustin T. GibbsBEGIN_CRITICAL;
669b3b25f2cSJustin T. Gibbs	if ((ahd->bugs & AHD_FAINT_LED_BUG) != 0) {
670b3b25f2cSJustin T. Gibbs		/*
671b3b25f2cSJustin T. Gibbs		 * On Rev A. hardware, the busy LED is only
672b3b25f2cSJustin T. Gibbs		 * turned on automaically during selections
673b3b25f2cSJustin T. Gibbs		 * and re-selections.  Make the LED status
674b3b25f2cSJustin T. Gibbs		 * more useful by forcing it to be on from
675b3b25f2cSJustin T. Gibbs		 * the point of re-selection until our idle
676b3b25f2cSJustin T. Gibbs		 * loop determines that neither of our FIFOs
677b3b25f2cSJustin T. Gibbs		 * are busy.  This handles the non-packetized
678b3b25f2cSJustin T. Gibbs		 * case nicely as we will not return to the
679b3b25f2cSJustin T. Gibbs		 * idle loop until the busfree at the end of
680b3b25f2cSJustin T. Gibbs		 * each transaction.
681b3b25f2cSJustin T. Gibbs		 */
682b3b25f2cSJustin T. Gibbs		or	SBLKCTL, DIAGLEDEN|DIAGLEDON;
683b3b25f2cSJustin T. Gibbs	}
68417d24755SJustin T. Gibbs	/* Clear out all SCBs that have been successfully sent. */
68517d24755SJustin T. Gibbs	if ((ahd->bugs & AHD_SENT_SCB_UPDATE_BUG) != 0) {
68617d24755SJustin T. Gibbs		/*
68717d24755SJustin T. Gibbs		 * For packetized, the LQO manager clears ENSELO on
68817d24755SJustin T. Gibbs		 * the assertion of SELDO.  If we are non-packetized,
689acae33b0SJustin T. Gibbs		 * LASTSCB and CURRSCB are accurate.
69017d24755SJustin T. Gibbs		 */
69117d24755SJustin T. Gibbs		test	SCSISEQ0, ENSELO jnz use_lastscb;
69217d24755SJustin T. Gibbs
69317d24755SJustin T. Gibbs		/*
69417d24755SJustin T. Gibbs		 * The update is correct for LQOSTAT1 errors.  All
69517d24755SJustin T. Gibbs		 * but LQOBUSFREE are handled by kernel interrupts.
69617d24755SJustin T. Gibbs		 * If we see LQOBUSFREE, return to the idle loop.
69717d24755SJustin T. Gibbs		 * Once we are out of the select_out critical section,
69817d24755SJustin T. Gibbs		 * the kernel will cleanup the LQOBUSFREE and we will
69917d24755SJustin T. Gibbs		 * eventually restart the selection if appropriate.
70017d24755SJustin T. Gibbs		 */
70117d24755SJustin T. Gibbs		test	LQOSTAT1, LQOBUSFREE jnz idle_loop;
70217d24755SJustin T. Gibbs
70317d24755SJustin T. Gibbs		/*
70417d24755SJustin T. Gibbs		 * On a phase change oustside of packet boundaries,
70517d24755SJustin T. Gibbs		 * LASTSCB points to the currently active SCB context
70617d24755SJustin T. Gibbs		 * on the bus.
70717d24755SJustin T. Gibbs		 */
70817d24755SJustin T. Gibbs		test	LQOSTAT2, LQOPHACHGOUTPKT jnz use_lastscb;
70917d24755SJustin T. Gibbs
71017d24755SJustin T. Gibbs		/*
71117d24755SJustin T. Gibbs		 * If the hardware has traversed the whole list, NEXTSCB
71217d24755SJustin T. Gibbs		 * will be NULL, CURRSCB and LASTSCB cannot be trusted,
71317d24755SJustin T. Gibbs		 * but MAXCMDCNT is accurate.  If we stop part way through
71417d24755SJustin T. Gibbs		 * the list or only had one command to issue, NEXTSCB[1] is
71517d24755SJustin T. Gibbs		 * not NULL and LASTSCB is the last command to go out.
71617d24755SJustin T. Gibbs		 */
71717d24755SJustin T. Gibbs		cmp	NEXTSCB[1], SCB_LIST_NULL jne use_lastscb;
71817d24755SJustin T. Gibbs
71917d24755SJustin T. Gibbs		/*
72017d24755SJustin T. Gibbs		 * Brute force walk.
72117d24755SJustin T. Gibbs		 */
72217d24755SJustin T. Gibbs		bmov	SCBPTR, WAITING_TID_HEAD, 2;
72317d24755SJustin T. Gibbs		mvi	SEQINTCTL, INTVEC1DSL;
72417d24755SJustin T. Gibbs		mvi	MODE_PTR, MK_MODE(M_CFG, M_CFG);
72517d24755SJustin T. Gibbs		mov	A, MAXCMDCNT;
72617d24755SJustin T. Gibbs		mvi	MODE_PTR, MK_MODE(M_SCSI, M_SCSI);
72717d24755SJustin T. Gibbs		clr	SEQINTCTL;
72817d24755SJustin T. Gibbsfind_lastscb_loop:
72917d24755SJustin T. Gibbs		dec	A;
73017d24755SJustin T. Gibbs		test	A, 0xFF jz found_last_sent_scb;
73117d24755SJustin T. Gibbs		bmov	SCBPTR, SCB_NEXT, 2;
73217d24755SJustin T. Gibbs		jmp	find_lastscb_loop;
73317d24755SJustin T. Gibbsuse_lastscb:
73417d24755SJustin T. Gibbs		bmov	SCBPTR, LASTSCB, 2;
73517d24755SJustin T. Gibbsfound_last_sent_scb:
73617d24755SJustin T. Gibbs		bmov	CURRSCB, SCBPTR, 2;
73717d24755SJustin T. Gibbscurscb_ww_done:
73817d24755SJustin T. Gibbs	} else {
73917d24755SJustin T. Gibbs		bmov	SCBPTR, CURRSCB, 2;
74017d24755SJustin T. Gibbs	}
7410c5aa4c5SScott Long
74217d24755SJustin T. Gibbs	/*
743789902c3SJustin T. Gibbs	 * The whole list made it.  Clear our tail pointer to indicate
744789902c3SJustin T. Gibbs	 * that the per-target selection queue is now empty.
74517d24755SJustin T. Gibbs	 */
746789902c3SJustin T. Gibbs	cmp	SCB_NEXT[1], SCB_LIST_NULL je select_out_clear_tail;
74717d24755SJustin T. Gibbs
74817d24755SJustin T. Gibbs	/*
749789902c3SJustin T. Gibbs	 * Requeue any SCBs not sent, to the tail of the waiting Q.
75017d24755SJustin T. Gibbs	 * We know that neither the per-TID list nor the list of
751789902c3SJustin T. Gibbs	 * TIDs is empty.  Use this knowledge to our advantage and
752789902c3SJustin T. Gibbs	 * queue the remainder to the tail of the global execution
753789902c3SJustin T. Gibbs	 * queue.
75417d24755SJustin T. Gibbs	 */
75517d24755SJustin T. Gibbs	bmov	REG0, SCB_NEXT, 2;
756789902c3SJustin T. Gibbsselect_out_queue_remainder:
75717d24755SJustin T. Gibbs	bmov	SCBPTR, WAITING_TID_TAIL, 2;
75817d24755SJustin T. Gibbs	bmov	SCB_NEXT2, REG0, 2;
75917d24755SJustin T. Gibbs	bmov	WAITING_TID_TAIL, REG0, 2;
76017d24755SJustin T. Gibbs	jmp	select_out_inc_tid_q;
76117d24755SJustin T. Gibbs
762789902c3SJustin T. Gibbsselect_out_clear_tail:
76317d24755SJustin T. Gibbs	/*
764789902c3SJustin T. Gibbs	 * Queue any pending MK_MESSAGE SCB for this target now
765789902c3SJustin T. Gibbs	 * that the queue is empty.
76617d24755SJustin T. Gibbs	 */
767789902c3SJustin T. Gibbs	test	SEQ_FLAGS2, PENDING_MK_MESSAGE jz select_out_no_mk_message_scb;
768789902c3SJustin T. Gibbs	mov	A, MK_MESSAGE_SCSIID;
769789902c3SJustin T. Gibbs	cmp	SCB_SCSIID, A jne select_out_no_mk_message_scb;
770789902c3SJustin T. Gibbs	and	SEQ_FLAGS2, ~PENDING_MK_MESSAGE;
771789902c3SJustin T. Gibbs	bmov	REG0, MK_MESSAGE_SCB, 2;
772789902c3SJustin T. Gibbs	jmp select_out_queue_remainder;
773789902c3SJustin T. Gibbs
774789902c3SJustin T. Gibbsselect_out_no_mk_message_scb:
775789902c3SJustin T. Gibbs	/*
776789902c3SJustin T. Gibbs	 * Clear this target's execution tail and increment the queue.
777789902c3SJustin T. Gibbs	 */
77817d24755SJustin T. Gibbs	shr	DINDEX, 3, SCB_SCSIID;
77917d24755SJustin T. Gibbs	or	DINDEX, 1;	/* Want only the second byte */
78017d24755SJustin T. Gibbs	mvi	DINDEX[1], ((WAITING_SCB_TAILS) >> 8);
78117d24755SJustin T. Gibbs	mvi	DINDIR, SCB_LIST_NULL;
78217d24755SJustin T. Gibbsselect_out_inc_tid_q:
78317d24755SJustin T. Gibbs	bmov	SCBPTR, WAITING_TID_HEAD, 2;
78417d24755SJustin T. Gibbs	bmov	WAITING_TID_HEAD, SCB_NEXT2, 2;
78517d24755SJustin T. Gibbs	cmp	WAITING_TID_HEAD[1], SCB_LIST_NULL jne . + 2;
78617d24755SJustin T. Gibbs	mvi	WAITING_TID_TAIL[1], SCB_LIST_NULL;
78717d24755SJustin T. Gibbs	bmov	SCBPTR, CURRSCB, 2;
78817d24755SJustin T. Gibbs	mvi	CLRSINT0, CLRSELDO;
789789902c3SJustin T. Gibbs	test	LQOSTAT2, LQOPHACHGOUTPKT jnz unexpected_nonpkt_mode_cleared;
790789902c3SJustin T. Gibbs	test	LQOSTAT1, LQOPHACHGINPKT jnz unexpected_nonpkt_mode_cleared;
79117d24755SJustin T. Gibbs
79217d24755SJustin T. Gibbs	/*
79317d24755SJustin T. Gibbs	 * If this is a packetized connection, return to our
79417d24755SJustin T. Gibbs	 * idle_loop and let our interrupt handler deal with
79517d24755SJustin T. Gibbs	 * any connection setup/teardown issues.  The only
7960794987dSJustin T. Gibbs	 * exceptions are the case of MK_MESSAGE and task management
7970794987dSJustin T. Gibbs	 * SCBs.
7980794987dSJustin T. Gibbs	 */
7990794987dSJustin T. Gibbs	if ((ahd->bugs & AHD_LQO_ATNO_BUG) != 0) {
8000794987dSJustin T. Gibbs		/*
8010794987dSJustin T. Gibbs		 * In the A, the LQO manager transitions to LQOSTOP0 even if
80217d24755SJustin T. Gibbs		 * we have selected out with ATN asserted and the target
80317d24755SJustin T. Gibbs		 * REQs in a non-packet phase.
80417d24755SJustin T. Gibbs		 */
80517d24755SJustin T. Gibbs		test 	SCB_CONTROL, MK_MESSAGE jz select_out_no_message;
80617d24755SJustin T. Gibbs		test	SCSISIGO, ATNO jnz select_out_non_packetized;
80717d24755SJustin T. Gibbsselect_out_no_message:
80817d24755SJustin T. Gibbs	}
8090794987dSJustin T. Gibbs	test	LQOSTAT2, LQOSTOP0 jz select_out_non_packetized;
8100794987dSJustin T. Gibbs	test	SCB_TASK_MANAGEMENT, 0xFF jz idle_loop;
8110794987dSJustin T. Gibbs	SET_SEQINTCODE(TASKMGMT_FUNC_COMPLETE)
8120794987dSJustin T. Gibbs	jmp	idle_loop;
81317d24755SJustin T. Gibbs
81417d24755SJustin T. Gibbsselect_out_non_packetized:
81517d24755SJustin T. Gibbs	/* Non packetized request. */
81617d24755SJustin T. Gibbs	and     SCSISEQ0, ~ENSELO;
817c59c8a72SJustin T. Gibbs	if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
818c59c8a72SJustin T. Gibbs		/*
819b3b25f2cSJustin T. Gibbs		 * Test to ensure that the bus has not
820b3b25f2cSJustin T. Gibbs		 * already gone free prior to clearing
821b3b25f2cSJustin T. Gibbs		 * any stale busfree status.  This avoids
822b3b25f2cSJustin T. Gibbs		 * a window whereby a busfree just after
823b3b25f2cSJustin T. Gibbs		 * a selection could be missed.
824c59c8a72SJustin T. Gibbs		 */
825b3b25f2cSJustin T. Gibbs		test	SCSISIGI, BSYI jz . + 2;
826c59c8a72SJustin T. Gibbs		mvi	CLRSINT1,CLRBUSFREE;
827c59c8a72SJustin T. Gibbs		or	SIMODE1, ENBUSFREE;
828c59c8a72SJustin T. Gibbs	}
82917d24755SJustin T. Gibbs	mov	SAVED_SCSIID, SCB_SCSIID;
83017d24755SJustin T. Gibbs	mov	SAVED_LUN, SCB_LUN;
8310794987dSJustin T. Gibbs	mvi	SEQ_FLAGS, NO_CDB_SENT;
8320794987dSJustin T. GibbsEND_CRITICAL;
83317d24755SJustin T. Gibbs	or	SXFRCTL0, SPIOEN;
83417d24755SJustin T. Gibbs
83517d24755SJustin T. Gibbs	/*
83617d24755SJustin T. Gibbs	 * As soon as we get a successful selection, the target
83717d24755SJustin T. Gibbs	 * should go into the message out phase since we have ATN
83817d24755SJustin T. Gibbs	 * asserted.
83917d24755SJustin T. Gibbs	 */
84017d24755SJustin T. Gibbs	mvi	MSG_OUT, MSG_IDENTIFYFLAG;
84117d24755SJustin T. Gibbs
84217d24755SJustin T. Gibbs	/*
84317d24755SJustin T. Gibbs	 * Main loop for information transfer phases.  Wait for the
84417d24755SJustin T. Gibbs	 * target to assert REQ before checking MSG, C/D and I/O for
84517d24755SJustin T. Gibbs	 * the bus phase.
84617d24755SJustin T. Gibbs	 */
84717d24755SJustin T. Gibbsmesgin_phasemis:
84817d24755SJustin T. GibbsITloop:
84917d24755SJustin T. Gibbs	call	phase_lock;
85017d24755SJustin T. Gibbs
85117d24755SJustin T. Gibbs	mov	A, LASTPHASE;
85217d24755SJustin T. Gibbs
85317d24755SJustin T. Gibbs	test	A, ~P_DATAIN_DT	jz p_data;
85417d24755SJustin T. Gibbs	cmp	A,P_COMMAND	je p_command;
85517d24755SJustin T. Gibbs	cmp	A,P_MESGOUT	je p_mesgout;
85617d24755SJustin T. Gibbs	cmp	A,P_STATUS	je p_status;
85717d24755SJustin T. Gibbs	cmp	A,P_MESGIN	je p_mesgin;
85817d24755SJustin T. Gibbs
8590c5aa4c5SScott Long	SET_SEQINTCODE(BAD_PHASE)
86017d24755SJustin T. Gibbs	jmp	ITloop;			/* Try reading the bus again. */
86117d24755SJustin T. Gibbs
86217d24755SJustin T. Gibbs/*
86317d24755SJustin T. Gibbs * Command phase.  Set up the DMA registers and let 'er rip.
86417d24755SJustin T. Gibbs */
86517d24755SJustin T. Gibbsp_command:
8660c5aa4c5SScott Long	test	SEQ_FLAGS, NOT_IDENTIFIED jz p_command_okay;
8670c5aa4c5SScott Long	SET_SEQINTCODE(PROTO_VIOLATION)
8680c5aa4c5SScott Longp_command_okay:
86917d24755SJustin T. Gibbs	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
87017d24755SJustin T. Gibbs		jnz p_command_allocate_fifo;
87117d24755SJustin T. Gibbs	/*
87217d24755SJustin T. Gibbs	 * Command retry.  Free our current FIFO and
87317d24755SJustin T. Gibbs	 * re-allocate a FIFO so transfer state is
87417d24755SJustin T. Gibbs	 * reset.
87517d24755SJustin T. Gibbs	 */
8760c5aa4c5SScott LongSET_SRC_MODE	M_DFF1;
8770c5aa4c5SScott LongSET_DST_MODE	M_DFF1;
87817d24755SJustin T. Gibbs	mvi	DFFSXFRCTL, RSTCHN|CLRSHCNT;
8790c5aa4c5SScott Long	SET_MODE(M_SCSI, M_SCSI)
88017d24755SJustin T. Gibbsp_command_allocate_fifo:
881c59c8a72SJustin T. Gibbs	bmov	ALLOCFIFO_SCBPTR, SCBPTR, 2;
88217d24755SJustin T. Gibbs	call	allocate_fifo;
8830c5aa4c5SScott LongSET_SRC_MODE	M_DFF1;
8840c5aa4c5SScott LongSET_DST_MODE	M_DFF1;
88517d24755SJustin T. Gibbs	add	NONE, -17, SCB_CDB_LEN;
88617d24755SJustin T. Gibbs	jnc	p_command_embedded;
88717d24755SJustin T. Gibbsp_command_from_host:
888acae33b0SJustin T. Gibbs	bmov	HADDR[0], SCB_HOST_CDB_PTR, 9;
88917d24755SJustin T. Gibbs	mvi	SG_CACHE_PRE, LAST_SEG;
89017d24755SJustin T. Gibbs	mvi	DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN);
8910c5aa4c5SScott Long	jmp	p_command_xfer;
89217d24755SJustin T. Gibbsp_command_embedded:
89317d24755SJustin T. Gibbs	bmov	SHCNT[0], SCB_CDB_LEN,  1;
89417d24755SJustin T. Gibbs	bmov	DFDAT, SCB_CDB_STORE, 16;
89517d24755SJustin T. Gibbs	mvi	DFCNTRL, SCSIEN;
8960c5aa4c5SScott Longp_command_xfer:
8970c5aa4c5SScott Long	and	SEQ_FLAGS, ~NO_CDB_SENT;
898b3b25f2cSJustin T. Gibbs	if ((ahd->features & AHD_FAST_CDB_DELIVERY) != 0) {
899b3b25f2cSJustin T. Gibbs		/*
900b3b25f2cSJustin T. Gibbs		 * To speed up CDB delivery in Rev B, all CDB acks
901b3b25f2cSJustin T. Gibbs		 * are "released" to the output sync as soon as the
902b3b25f2cSJustin T. Gibbs		 * command phase starts.  There is only one problem
903b3b25f2cSJustin T. Gibbs		 * with this approach.  If the target changes phase
904b3b25f2cSJustin T. Gibbs		 * before all data are sent, we have left over acks
905b3b25f2cSJustin T. Gibbs		 * that can go out on the bus in a data phase.  Due
906b3b25f2cSJustin T. Gibbs		 * to other chip contraints, this only happens if
907b3b25f2cSJustin T. Gibbs		 * the target goes to data-in, but if the acks go
908b3b25f2cSJustin T. Gibbs		 * out before we can test SDONE, we'll think that
909b3b25f2cSJustin T. Gibbs		 * the transfer has completed successfully.  Work
910b3b25f2cSJustin T. Gibbs		 * around this by taking advantage of the 400ns or
911b3b25f2cSJustin T. Gibbs		 * 800ns dead time between command phase and the REQ
912b3b25f2cSJustin T. Gibbs		 * of the new phase.  If the transfer has completed
913b3b25f2cSJustin T. Gibbs		 * successfully, SCSIEN should fall *long* before we
914b3b25f2cSJustin T. Gibbs		 * see a phase change.  We thus treat any phasemiss
915b3b25f2cSJustin T. Gibbs		 * that occurs before SCSIEN falls as an incomplete
916b3b25f2cSJustin T. Gibbs		 * transfer.
917b3b25f2cSJustin T. Gibbs		 */
918b3b25f2cSJustin T. Gibbs		test	SSTAT1, PHASEMIS jnz p_command_xfer_failed;
919b3b25f2cSJustin T. Gibbs		test	DFCNTRL, SCSIEN jnz . - 1;
920b3b25f2cSJustin T. Gibbs	} else {
9210c5aa4c5SScott Long		test	DFCNTRL, SCSIEN jnz .;
922b3b25f2cSJustin T. Gibbs	}
92317d24755SJustin T. Gibbs	/*
92417d24755SJustin T. Gibbs	 * DMA Channel automatically disabled.
92517d24755SJustin T. Gibbs	 * Don't allow a data phase if the command
9260c5aa4c5SScott Long	 * was not fully transferred.
92717d24755SJustin T. Gibbs	 */
92817d24755SJustin T. Gibbs	test	SSTAT2, SDONE jnz ITloop;
929b3b25f2cSJustin T. Gibbsp_command_xfer_failed:
93017d24755SJustin T. Gibbs	or	SEQ_FLAGS, NO_CDB_SENT;
93117d24755SJustin T. Gibbs	jmp	ITloop;
93217d24755SJustin T. Gibbs
93317d24755SJustin T. Gibbs
93417d24755SJustin T. Gibbs/*
93517d24755SJustin T. Gibbs * Status phase.  Wait for the data byte to appear, then read it
93617d24755SJustin T. Gibbs * and store it into the SCB.
93717d24755SJustin T. Gibbs */
93817d24755SJustin T. GibbsSET_SRC_MODE	M_SCSI;
93917d24755SJustin T. GibbsSET_DST_MODE	M_SCSI;
94017d24755SJustin T. Gibbsp_status:
9410c5aa4c5SScott Long	test	SEQ_FLAGS,NOT_IDENTIFIED jnz mesgin_proto_violation;
94217d24755SJustin T. Gibbsp_status_okay:
94317d24755SJustin T. Gibbs	mov	SCB_SCSI_STATUS, SCSIDAT;
94417d24755SJustin T. Gibbs	or	SCB_CONTROL, STATUS_RCVD;
94517d24755SJustin T. Gibbs	jmp	ITloop;
94617d24755SJustin T. Gibbs
94717d24755SJustin T. Gibbs/*
94817d24755SJustin T. Gibbs * Message out phase.  If MSG_OUT is MSG_IDENTIFYFLAG, build a full
94917d24755SJustin T. Gibbs * indentify message sequence and send it to the target.  The host may
95017d24755SJustin T. Gibbs * override this behavior by setting the MK_MESSAGE bit in the SCB
95117d24755SJustin T. Gibbs * control byte.  This will cause us to interrupt the host and allow
95217d24755SJustin T. Gibbs * it to handle the message phase completely on its own.  If the bit
95317d24755SJustin T. Gibbs * associated with this target is set, we will also interrupt the host,
95417d24755SJustin T. Gibbs * thereby allowing it to send a message on the next selection regardless
95517d24755SJustin T. Gibbs * of the transaction being sent.
95617d24755SJustin T. Gibbs *
95717d24755SJustin T. Gibbs * If MSG_OUT is == HOST_MSG, also interrupt the host and take a message.
95817d24755SJustin T. Gibbs * This is done to allow the host to send messages outside of an identify
95917d24755SJustin T. Gibbs * sequence while protecting the seqencer from testing the MK_MESSAGE bit
96017d24755SJustin T. Gibbs * on an SCB that might not be for the current nexus. (For example, a
961594c945aSPedro F. Giffuni * BDR message in response to a bad reselection would leave us pointed to
96217d24755SJustin T. Gibbs * an SCB that doesn't have anything to do with the current target).
96317d24755SJustin T. Gibbs *
96417d24755SJustin T. Gibbs * Otherwise, treat MSG_OUT as a 1 byte message to send (abort, abort tag,
96517d24755SJustin T. Gibbs * bus device reset).
96617d24755SJustin T. Gibbs *
96717d24755SJustin T. Gibbs * When there are no messages to send, MSG_OUT should be set to MSG_NOOP,
96817d24755SJustin T. Gibbs * in case the target decides to put us in this phase for some strange
96917d24755SJustin T. Gibbs * reason.
97017d24755SJustin T. Gibbs */
97117d24755SJustin T. Gibbsp_mesgout_retry:
97217d24755SJustin T. Gibbs	/* Turn on ATN for the retry */
97317d24755SJustin T. Gibbs	mvi	SCSISIGO, ATNO;
97417d24755SJustin T. Gibbsp_mesgout:
97517d24755SJustin T. Gibbs	mov	SINDEX, MSG_OUT;
97617d24755SJustin T. Gibbs	cmp	SINDEX, MSG_IDENTIFYFLAG jne p_mesgout_from_host;
97717d24755SJustin T. Gibbs	test	SCB_CONTROL,MK_MESSAGE	jnz host_message_loop;
97817d24755SJustin T. Gibbsp_mesgout_identify:
97917d24755SJustin T. Gibbs	or	SINDEX, MSG_IDENTIFYFLAG|DISCENB, SCB_LUN;
98017d24755SJustin T. Gibbs	test	SCB_CONTROL, DISCENB jnz . + 2;
98117d24755SJustin T. Gibbs	and	SINDEX, ~DISCENB;
98217d24755SJustin T. Gibbs/*
98317d24755SJustin T. Gibbs * Send a tag message if TAG_ENB is set in the SCB control block.
98417d24755SJustin T. Gibbs * Use SCB_NONPACKET_TAG as the tag value.
98517d24755SJustin T. Gibbs */
98617d24755SJustin T. Gibbsp_mesgout_tag:
98717d24755SJustin T. Gibbs	test	SCB_CONTROL,TAG_ENB jz  p_mesgout_onebyte;
98817d24755SJustin T. Gibbs	mov	SCSIDAT, SINDEX;	/* Send the identify message */
98917d24755SJustin T. Gibbs	call	phase_lock;
99017d24755SJustin T. Gibbs	cmp	LASTPHASE, P_MESGOUT	jne p_mesgout_done;
99117d24755SJustin T. Gibbs	and	SCSIDAT,TAG_ENB|SCB_TAG_TYPE,SCB_CONTROL;
99217d24755SJustin T. Gibbs	call	phase_lock;
99317d24755SJustin T. Gibbs	cmp	LASTPHASE, P_MESGOUT	jne p_mesgout_done;
9948fdc1261SJustin T. Gibbs	mov	SCBPTR jmp p_mesgout_onebyte;
99517d24755SJustin T. Gibbs/*
99617d24755SJustin T. Gibbs * Interrupt the driver, and allow it to handle this message
99717d24755SJustin T. Gibbs * phase and any required retries.
99817d24755SJustin T. Gibbs */
99917d24755SJustin T. Gibbsp_mesgout_from_host:
100017d24755SJustin T. Gibbs	cmp	SINDEX, HOST_MSG	jne p_mesgout_onebyte;
100117d24755SJustin T. Gibbs	jmp	host_message_loop;
100217d24755SJustin T. Gibbs
100317d24755SJustin T. Gibbsp_mesgout_onebyte:
100417d24755SJustin T. Gibbs	mvi	CLRSINT1, CLRATNO;
100517d24755SJustin T. Gibbs	mov	SCSIDAT, SINDEX;
100617d24755SJustin T. Gibbs
100717d24755SJustin T. Gibbs/*
100817d24755SJustin T. Gibbs * If the next bus phase after ATN drops is message out, it means
100917d24755SJustin T. Gibbs * that the target is requesting that the last message(s) be resent.
101017d24755SJustin T. Gibbs */
101117d24755SJustin T. Gibbs	call	phase_lock;
101217d24755SJustin T. Gibbs	cmp	LASTPHASE, P_MESGOUT	je p_mesgout_retry;
101317d24755SJustin T. Gibbs
101417d24755SJustin T. Gibbsp_mesgout_done:
101517d24755SJustin T. Gibbs	mvi	CLRSINT1,CLRATNO;	/* Be sure to turn ATNO off */
101617d24755SJustin T. Gibbs	mov	LAST_MSG, MSG_OUT;
101717d24755SJustin T. Gibbs	mvi	MSG_OUT, MSG_NOOP;	/* No message left */
101817d24755SJustin T. Gibbs	jmp	ITloop;
101917d24755SJustin T. Gibbs
102017d24755SJustin T. Gibbs/*
102117d24755SJustin T. Gibbs * Message in phase.  Bytes are read using Automatic PIO mode.
102217d24755SJustin T. Gibbs */
102317d24755SJustin T. Gibbsp_mesgin:
102417d24755SJustin T. Gibbs	/* read the 1st message byte */
102517d24755SJustin T. Gibbs	mvi	ACCUM		call inb_first;
102617d24755SJustin T. Gibbs
102717d24755SJustin T. Gibbs	test	A,MSG_IDENTIFYFLAG	jnz mesgin_identify;
102817d24755SJustin T. Gibbs	cmp	A,MSG_DISCONNECT	je mesgin_disconnect;
102917d24755SJustin T. Gibbs	cmp	A,MSG_SAVEDATAPOINTER	je mesgin_sdptrs;
103017d24755SJustin T. Gibbs	cmp	ALLZEROS,A		je mesgin_complete;
103117d24755SJustin T. Gibbs	cmp	A,MSG_RESTOREPOINTERS	je mesgin_rdptrs;
103217d24755SJustin T. Gibbs	cmp	A,MSG_IGN_WIDE_RESIDUE	je mesgin_ign_wide_residue;
103317d24755SJustin T. Gibbs	cmp	A,MSG_NOOP		je mesgin_done;
103417d24755SJustin T. Gibbs
103517d24755SJustin T. Gibbs/*
103617d24755SJustin T. Gibbs * Pushed message loop to allow the kernel to
103717d24755SJustin T. Gibbs * run it's own message state engine.  To avoid an
103817d24755SJustin T. Gibbs * extra nop instruction after signaling the kernel,
103917d24755SJustin T. Gibbs * we perform the phase_lock before checking to see
104017d24755SJustin T. Gibbs * if we should exit the loop and skip the phase_lock
104117d24755SJustin T. Gibbs * in the ITloop.  Performing back to back phase_locks
104217d24755SJustin T. Gibbs * shouldn't hurt, but why do it twice...
104317d24755SJustin T. Gibbs */
104417d24755SJustin T. Gibbshost_message_loop:
104517d24755SJustin T. Gibbs	call	phase_lock;	/* Benign the first time through. */
10460c5aa4c5SScott Long	SET_SEQINTCODE(HOST_MSG_LOOP)
104717d24755SJustin T. Gibbs	cmp	RETURN_1, EXIT_MSG_LOOP	je ITloop;
104817d24755SJustin T. Gibbs	cmp	RETURN_1, CONT_MSG_LOOP_WRITE	jne . + 3;
104917d24755SJustin T. Gibbs	mov	SCSIDAT, RETURN_2;
105017d24755SJustin T. Gibbs	jmp	host_message_loop;
105117d24755SJustin T. Gibbs	/* Must be CONT_MSG_LOOP_READ */
105217d24755SJustin T. Gibbs	mov	NONE, SCSIDAT;	/* ACK Byte */
105317d24755SJustin T. Gibbs	jmp	host_message_loop;
105417d24755SJustin T. Gibbs
105517d24755SJustin T. Gibbsmesgin_ign_wide_residue:
10560794987dSJustin T. Gibbs	mov	SAVED_MODE, MODE_PTR;
10570794987dSJustin T. Gibbs	SET_MODE(M_SCSI, M_SCSI)
105817d24755SJustin T. Gibbs	shr	NEGOADDR, 4, SAVED_SCSIID;
10590794987dSJustin T. Gibbs	mov	A, NEGCONOPTS;
10600794987dSJustin T. Gibbs	RESTORE_MODE(SAVED_MODE)
10610794987dSJustin T. Gibbs	test	A, WIDEXFER jz mesgin_reject;
106217d24755SJustin T. Gibbs	/* Pull the residue byte */
106317d24755SJustin T. Gibbs	mvi	REG0	call inb_next;
106417d24755SJustin T. Gibbs	cmp	REG0, 0x01 jne mesgin_reject;
106517d24755SJustin T. Gibbs	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz . + 2;
1066333f04d9SJustin T. Gibbs	test	SCB_TASK_ATTRIBUTE, SCB_XFERLEN_ODD jnz mesgin_done;
1067333f04d9SJustin T. Gibbs	SET_SEQINTCODE(IGN_WIDE_RES)
106817d24755SJustin T. Gibbs	jmp	mesgin_done;
106917d24755SJustin T. Gibbs
10700c5aa4c5SScott Longmesgin_proto_violation:
10710c5aa4c5SScott Long	SET_SEQINTCODE(PROTO_VIOLATION)
10720c5aa4c5SScott Long	jmp	mesgin_done;
107317d24755SJustin T. Gibbsmesgin_reject:
107417d24755SJustin T. Gibbs	mvi	MSG_MESSAGE_REJECT	call mk_mesg;
107517d24755SJustin T. Gibbsmesgin_done:
107617d24755SJustin T. Gibbs	mov	NONE,SCSIDAT;		/*dummy read from latch to ACK*/
107717d24755SJustin T. Gibbs	jmp	ITloop;
107817d24755SJustin T. Gibbs
1079c59c8a72SJustin T. Gibbs#define INDEX_DISC_LIST(scsiid, lun)					\
10808fdc1261SJustin T. Gibbs	and	A, 0xC0, scsiid;					\
10818fdc1261SJustin T. Gibbs	or	SCBPTR, A, lun;						\
1082c59c8a72SJustin T. Gibbs	clr	SCBPTR[1];						\
10838fdc1261SJustin T. Gibbs	and	SINDEX, 0x30, scsiid;					\
10848fdc1261SJustin T. Gibbs	shr	SINDEX, 3;	/* Multiply by 2 */			\
108517d24755SJustin T. Gibbs	add	SINDEX, (SCB_DISCONNECTED_LISTS & 0xFF);		\
108617d24755SJustin T. Gibbs	mvi	SINDEX[1], ((SCB_DISCONNECTED_LISTS >> 8) & 0xFF)
108717d24755SJustin T. Gibbs
108817d24755SJustin T. Gibbsmesgin_identify:
108917d24755SJustin T. Gibbs	/*
109017d24755SJustin T. Gibbs	 * Determine whether a target is using tagged or non-tagged
109117d24755SJustin T. Gibbs	 * transactions by first looking at the transaction stored in
109217d24755SJustin T. Gibbs	 * the per-device, disconnected array.  If there is no untagged
10938fdc1261SJustin T. Gibbs	 * transaction for this target, this must be a tagged transaction.
109417d24755SJustin T. Gibbs	 */
109517d24755SJustin T. Gibbs	and	SAVED_LUN, MSG_IDENTIFY_LUNMASK, A;
10968fdc1261SJustin T. Gibbs	INDEX_DISC_LIST(SAVED_SCSIID, SAVED_LUN);
109717d24755SJustin T. Gibbs	bmov	DINDEX, SINDEX, 2;
10988fdc1261SJustin T. Gibbs	bmov	REG0, SINDIR, 2;
10998fdc1261SJustin T. Gibbs	cmp	REG0[1], SCB_LIST_NULL je snoop_tag;
11008fdc1261SJustin T. Gibbs	/* Untagged.  Clear the busy table entry and setup the SCB. */
11018fdc1261SJustin T. Gibbs	bmov	DINDIR, ALLONES, 2;
11028fdc1261SJustin T. Gibbs	bmov	SCBPTR, REG0, 2;
11038fdc1261SJustin T. Gibbs	jmp	setup_SCB;
110417d24755SJustin T. Gibbs
110517d24755SJustin T. Gibbs/*
110617d24755SJustin T. Gibbs * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
110717d24755SJustin T. Gibbs * If we get one, we use the tag returned to find the proper
11088fdc1261SJustin T. Gibbs * SCB.  After receiving the tag, look for the SCB at SCB locations tag and
11098fdc1261SJustin T. Gibbs * tag + 256.
111017d24755SJustin T. Gibbs */
111117d24755SJustin T. Gibbssnoop_tag:
111217d24755SJustin T. Gibbs	if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
111317d24755SJustin T. Gibbs		or	SEQ_FLAGS, 0x80;
111417d24755SJustin T. Gibbs	}
111517d24755SJustin T. Gibbs	mov	NONE, SCSIDAT;		/* ACK Identify MSG */
111617d24755SJustin T. Gibbs	call	phase_lock;
111717d24755SJustin T. Gibbs	if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
111817d24755SJustin T. Gibbs		or	SEQ_FLAGS, 0x1;
111917d24755SJustin T. Gibbs	}
112017d24755SJustin T. Gibbs	cmp	LASTPHASE, P_MESGIN	jne not_found_ITloop;
112117d24755SJustin T. Gibbs	if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
112217d24755SJustin T. Gibbs		or	SEQ_FLAGS, 0x2;
112317d24755SJustin T. Gibbs	}
112417d24755SJustin T. Gibbs	cmp	SCSIBUS, MSG_SIMPLE_Q_TAG jne not_found;
112517d24755SJustin T. Gibbsget_tag:
112617d24755SJustin T. Gibbs	clr	SCBPTR[1];
112717d24755SJustin T. Gibbs	mvi	SCBPTR	call inb_next;	/* tag value */
112817d24755SJustin T. Gibbsverify_scb:
11298fdc1261SJustin T. Gibbs	test	SCB_CONTROL,DISCONNECTED jz verify_other_scb;
113017d24755SJustin T. Gibbs	mov	A, SAVED_SCSIID;
113117d24755SJustin T. Gibbs	cmp	SCB_SCSIID, A jne verify_other_scb;
113217d24755SJustin T. Gibbs	mov	A, SAVED_LUN;
11338fdc1261SJustin T. Gibbs	cmp	SCB_LUN, A je setup_SCB_disconnected;
113417d24755SJustin T. Gibbsverify_other_scb:
113517d24755SJustin T. Gibbs	xor	SCBPTR[1], 1;
113617d24755SJustin T. Gibbs	test	SCBPTR[1], 0xFF jnz verify_scb;
113717d24755SJustin T. Gibbs	jmp	not_found;
113817d24755SJustin T. Gibbs
113917d24755SJustin T. Gibbs/*
114017d24755SJustin T. Gibbs * Ensure that the SCB the tag points to is for
114117d24755SJustin T. Gibbs * an SCB transaction to the reconnecting target.
114217d24755SJustin T. Gibbs */
114317d24755SJustin T. Gibbssetup_SCB:
114417d24755SJustin T. Gibbs	if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
114517d24755SJustin T. Gibbs		or	SEQ_FLAGS, 0x10;
114617d24755SJustin T. Gibbs	}
114717d24755SJustin T. Gibbs	test	SCB_CONTROL,DISCONNECTED jz not_found;
11488fdc1261SJustin T. Gibbssetup_SCB_disconnected:
114917d24755SJustin T. Gibbs	and	SCB_CONTROL,~DISCONNECTED;
115017d24755SJustin T. Gibbs	clr	SEQ_FLAGS;	/* make note of IDENTIFY */
1151c59c8a72SJustin T. Gibbs	test	SCB_SGPTR, SG_LIST_NULL jnz . + 3;
1152c59c8a72SJustin T. Gibbs	bmov	ALLOCFIFO_SCBPTR, SCBPTR, 2;
115317d24755SJustin T. Gibbs	call	allocate_fifo;
115417d24755SJustin T. Gibbs	/* See if the host wants to send a message upon reconnection */
115517d24755SJustin T. Gibbs	test	SCB_CONTROL, MK_MESSAGE jz mesgin_done;
115617d24755SJustin T. Gibbs	mvi	HOST_MSG	call mk_mesg;
115717d24755SJustin T. Gibbs	jmp	mesgin_done;
115817d24755SJustin T. Gibbs
115917d24755SJustin T. Gibbsnot_found:
11600c5aa4c5SScott Long	SET_SEQINTCODE(NO_MATCH)
116117d24755SJustin T. Gibbs	jmp	mesgin_done;
116217d24755SJustin T. Gibbs
116317d24755SJustin T. Gibbsnot_found_ITloop:
11640c5aa4c5SScott Long	SET_SEQINTCODE(NO_MATCH)
116517d24755SJustin T. Gibbs	jmp	ITloop;
116617d24755SJustin T. Gibbs
116717d24755SJustin T. Gibbs/*
116817d24755SJustin T. Gibbs * We received a "command complete" message.  Put the SCB on the complete
116917d24755SJustin T. Gibbs * queue and trigger a completion interrupt via the idle loop.  Before doing
11704164174aSJustin T. Gibbs * so, check to see if there is a residual or the status byte is something
11714164174aSJustin T. Gibbs * other than STATUS_GOOD (0).  In either of these conditions, we upload the
11724164174aSJustin T. Gibbs * SCB back to the host so it can process this information.
117317d24755SJustin T. Gibbs */
117417d24755SJustin T. Gibbsmesgin_complete:
117517d24755SJustin T. Gibbs
117617d24755SJustin T. Gibbs	/*
117717d24755SJustin T. Gibbs	 * If ATN is raised, we still want to give the target a message.
117817d24755SJustin T. Gibbs	 * Perhaps there was a parity error on this last message byte.
117917d24755SJustin T. Gibbs	 * Either way, the target should take us to message out phase
118017d24755SJustin T. Gibbs	 * and then attempt to complete the command again.  We should use a
118117d24755SJustin T. Gibbs	 * critical section here to guard against a timeout triggering
118217d24755SJustin T. Gibbs	 * for this command and setting ATN while we are still processing
118317d24755SJustin T. Gibbs	 * the completion.
118417d24755SJustin T. Gibbs	test	SCSISIGI, ATNI jnz mesgin_done;
118517d24755SJustin T. Gibbs	 */
118617d24755SJustin T. Gibbs
118717d24755SJustin T. Gibbs	/*
11888fdc1261SJustin T. Gibbs	 * If we are identified and have successfully sent the CDB,
11898fdc1261SJustin T. Gibbs	 * any status will do.  Optimize this fast path.
11908fdc1261SJustin T. Gibbs	 */
11910c5aa4c5SScott Long	test	SCB_CONTROL, STATUS_RCVD jz mesgin_proto_violation;
11920c5aa4c5SScott Long	test	SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT jz complete_accepted;
11938fdc1261SJustin T. Gibbs
11948fdc1261SJustin T. Gibbs	/*
119517d24755SJustin T. Gibbs	 * If the target never sent an identify message but instead went
119617d24755SJustin T. Gibbs	 * to mesgin to give an invalid message, let the host abort us.
119717d24755SJustin T. Gibbs	 */
11980c5aa4c5SScott Long	test	SEQ_FLAGS, NOT_IDENTIFIED jnz mesgin_proto_violation;
11998fdc1261SJustin T. Gibbs
12008fdc1261SJustin T. Gibbs	/*
1201*b971c51aSGordon Bergling	 * If we received good status but never successfully sent the
12028fdc1261SJustin T. Gibbs	 * cdb, abort the command.
12038fdc1261SJustin T. Gibbs	 */
12048fdc1261SJustin T. Gibbs	test	SCB_SCSI_STATUS,0xff	jnz complete_accepted;
12050c5aa4c5SScott Long	test	SEQ_FLAGS, NO_CDB_SENT jnz mesgin_proto_violation;
12068fdc1261SJustin T. Gibbscomplete_accepted:
120717d24755SJustin T. Gibbs
120817d24755SJustin T. Gibbs	/*
12099bfefceaSGordon Bergling	 * See if we attempted to deliver a message but the target ignored us.
121017d24755SJustin T. Gibbs	 */
12118528a712SScott Long	test	SCB_CONTROL, MK_MESSAGE jz complete_nomsg;
12120c5aa4c5SScott Long	SET_SEQINTCODE(MKMSG_FAILED)
12138528a712SScott Longcomplete_nomsg:
121417d24755SJustin T. Gibbs	call	queue_scb_completion;
121517d24755SJustin T. Gibbs	jmp	await_busfree;
121617d24755SJustin T. Gibbs
12174164174aSJustin T. GibbsBEGIN_CRITICAL;
121817d24755SJustin T. Gibbsfreeze_queue:
121917d24755SJustin T. Gibbs	/* Cancel any pending select-out. */
12200794987dSJustin T. Gibbs	test	SSTAT0, SELDO|SELINGO jnz . + 2;
122117d24755SJustin T. Gibbs	and	SCSISEQ0, ~ENSELO;
122217d24755SJustin T. Gibbs	mov	ACCUM_SAVE, A;
122317d24755SJustin T. Gibbs	clr	A;
122417d24755SJustin T. Gibbs	add	QFREEZE_COUNT, 1;
122517d24755SJustin T. Gibbs	adc	QFREEZE_COUNT[1], A;
122617d24755SJustin T. Gibbs	or	SEQ_FLAGS2, SELECTOUT_QFROZEN;
122717d24755SJustin T. Gibbs	mov	A, ACCUM_SAVE ret;
12284164174aSJustin T. GibbsEND_CRITICAL;
122917d24755SJustin T. Gibbs
1230d7cff4abSJustin T. Gibbs/*
1231d7cff4abSJustin T. Gibbs * Complete the current FIFO's SCB if data for this same
1232d7cff4abSJustin T. Gibbs * SCB is not transferring in the other FIFO.
1233d7cff4abSJustin T. Gibbs */
1234d7cff4abSJustin T. GibbsSET_SRC_MODE	M_DFF1;
1235d7cff4abSJustin T. GibbsSET_DST_MODE	M_DFF1;
1236d7cff4abSJustin T. Gibbspkt_complete_scb_if_fifos_idle:
1237d7cff4abSJustin T. Gibbs	bmov	ARG_1, SCBPTR, 2;
1238d7cff4abSJustin T. Gibbs	mvi	DFFSXFRCTL, CLRCHN;
1239c59c8a72SJustin T. Gibbs	SET_MODE(M_SCSI, M_SCSI)
124017d24755SJustin T. Gibbs	bmov	SCBPTR, ARG_1, 2;
1241d7cff4abSJustin T. Gibbs	test	SCB_FIFO_USE_COUNT, 0xFF jnz return;
124217d24755SJustin T. Gibbsqueue_scb_completion:
124317d24755SJustin T. Gibbs	test	SCB_SCSI_STATUS,0xff	jnz bad_status;
124417d24755SJustin T. Gibbs	/*
124517d24755SJustin T. Gibbs	 * Check for residuals
124617d24755SJustin T. Gibbs	 */
124717d24755SJustin T. Gibbs	test	SCB_SGPTR, SG_LIST_NULL jnz complete;	/* No xfer */
124817d24755SJustin T. Gibbs	test	SCB_SGPTR, SG_FULL_RESID jnz upload_scb;/* Never xfered */
124917d24755SJustin T. Gibbs	test	SCB_RESIDUAL_SGPTR, SG_LIST_NULL jz upload_scb;
125017d24755SJustin T. Gibbscomplete:
1251b3b25f2cSJustin T. GibbsBEGIN_CRITICAL;
125217d24755SJustin T. Gibbs	bmov	SCB_NEXT_COMPLETE, COMPLETE_SCB_HEAD, 2;
125317d24755SJustin T. Gibbs	bmov	COMPLETE_SCB_HEAD, SCBPTR, 2 ret;
1254b3b25f2cSJustin T. GibbsEND_CRITICAL;
125517d24755SJustin T. Gibbsbad_status:
125617d24755SJustin T. Gibbs	cmp	SCB_SCSI_STATUS, STATUS_PKT_SENSE je upload_scb;
125717d24755SJustin T. Gibbs	call	freeze_queue;
125817d24755SJustin T. Gibbsupload_scb:
1259d7cff4abSJustin T. Gibbs	/*
1260d7cff4abSJustin T. Gibbs	 * Restore SCB TAG since we reuse this field
1261d7cff4abSJustin T. Gibbs	 * in the sequencer.  We don't want to corrupt
1262d7cff4abSJustin T. Gibbs	 * it on the host.
1263d7cff4abSJustin T. Gibbs	 */
1264d7cff4abSJustin T. Gibbs	bmov	SCB_TAG, SCBPTR, 2;
1265b3b25f2cSJustin T. GibbsBEGIN_CRITICAL;
12664164174aSJustin T. Gibbs	or	SCB_SGPTR, SG_STATUS_VALID;
12674164174aSJustin T. Gibbs	mvi	SCB_NEXT_COMPLETE[1], SCB_LIST_NULL;
12684164174aSJustin T. Gibbs	cmp	COMPLETE_DMA_SCB_HEAD[1], SCB_LIST_NULL jne add_dma_scb_tail;
126917d24755SJustin T. Gibbs	bmov	COMPLETE_DMA_SCB_HEAD, SCBPTR, 2;
12704164174aSJustin T. Gibbs	bmov	COMPLETE_DMA_SCB_TAIL, SCBPTR, 2 ret;
12714164174aSJustin T. Gibbsadd_dma_scb_tail:
12724164174aSJustin T. Gibbs	bmov	REG0, SCBPTR, 2;
12734164174aSJustin T. Gibbs	bmov	SCBPTR, COMPLETE_DMA_SCB_TAIL, 2;
12744164174aSJustin T. Gibbs	bmov	SCB_NEXT_COMPLETE, REG0, 2;
12754164174aSJustin T. Gibbs	bmov	COMPLETE_DMA_SCB_TAIL, REG0, 2 ret;
1276b3b25f2cSJustin T. GibbsEND_CRITICAL;
127717d24755SJustin T. Gibbs
127817d24755SJustin T. Gibbs/*
127917d24755SJustin T. Gibbs * Is it a disconnect message?  Set a flag in the SCB to remind us
128017d24755SJustin T. Gibbs * and await the bus going free.  If this is an untagged transaction
128117d24755SJustin T. Gibbs * store the SCB id for it in our untagged target table for lookup on
128217d24755SJustin T. Gibbs * a reselction.
128317d24755SJustin T. Gibbs */
128417d24755SJustin T. Gibbsmesgin_disconnect:
128517d24755SJustin T. Gibbs	/*
128617d24755SJustin T. Gibbs	 * If ATN is raised, we still want to give the target a message.
128717d24755SJustin T. Gibbs	 * Perhaps there was a parity error on this last message byte
128817d24755SJustin T. Gibbs	 * or we want to abort this command.  Either way, the target
128917d24755SJustin T. Gibbs	 * should take us to message out phase and then attempt to
129017d24755SJustin T. Gibbs	 * disconnect again.
129117d24755SJustin T. Gibbs	 * XXX - Wait for more testing.
129217d24755SJustin T. Gibbs	test	SCSISIGI, ATNI jnz mesgin_done;
129317d24755SJustin T. Gibbs	 */
12940c5aa4c5SScott Long	test	SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT
12950c5aa4c5SScott Long		jnz mesgin_proto_violation;
129617d24755SJustin T. Gibbs	or	SCB_CONTROL,DISCONNECTED;
12978fdc1261SJustin T. Gibbs	test	SCB_CONTROL, TAG_ENB jnz await_busfree;
129817d24755SJustin T. Gibbsqueue_disc_scb:
129917d24755SJustin T. Gibbs	bmov	REG0, SCBPTR, 2;
13008fdc1261SJustin T. Gibbs	INDEX_DISC_LIST(SAVED_SCSIID, SAVED_LUN);
130117d24755SJustin T. Gibbs	bmov	DINDEX, SINDEX, 2;
130217d24755SJustin T. Gibbs	bmov	DINDIR, REG0, 2;
130317d24755SJustin T. Gibbs	bmov	SCBPTR, REG0, 2;
130417d24755SJustin T. Gibbs	/* FALLTHROUGH */
130517d24755SJustin T. Gibbsawait_busfree:
130617d24755SJustin T. Gibbs	and	SIMODE1, ~ENBUSFREE;
1307c59c8a72SJustin T. Gibbs	if ((ahd->bugs & AHD_BUSFREEREV_BUG) == 0) {
1308c59c8a72SJustin T. Gibbs		/*
1309c59c8a72SJustin T. Gibbs		 * In the BUSFREEREV_BUG case, the
1310c59c8a72SJustin T. Gibbs		 * busfree status was cleared at the
1311c59c8a72SJustin T. Gibbs		 * beginning of the connection.
1312c59c8a72SJustin T. Gibbs		 */
1313c59c8a72SJustin T. Gibbs		mvi	CLRSINT1,CLRBUSFREE;
1314c59c8a72SJustin T. Gibbs	}
131517d24755SJustin T. Gibbs	mov	NONE, SCSIDAT;		/* Ack the last byte */
131617d24755SJustin T. Gibbs	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
131717d24755SJustin T. Gibbs		jnz await_busfree_not_m_dff;
131817d24755SJustin T. GibbsSET_SRC_MODE	M_DFF1;
131917d24755SJustin T. GibbsSET_DST_MODE	M_DFF1;
132017d24755SJustin T. Gibbsawait_busfree_clrchn:
132117d24755SJustin T. Gibbs	mvi	DFFSXFRCTL, CLRCHN;
132217d24755SJustin T. Gibbsawait_busfree_not_m_dff:
1323b3b25f2cSJustin T. Gibbs	/* clear target specific flags */
1324b3b25f2cSJustin T. Gibbs	mvi	SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT;
132517d24755SJustin T. Gibbs	test	SSTAT1,REQINIT|BUSFREE	jz .;
1326b3b25f2cSJustin T. Gibbs	/*
1327b3b25f2cSJustin T. Gibbs	 * We only set BUSFREE status once either a new
1328b3b25f2cSJustin T. Gibbs	 * phase has been detected or we are really
1329b3b25f2cSJustin T. Gibbs	 * BUSFREE.  This allows the driver to know
1330b3b25f2cSJustin T. Gibbs	 * that we are active on the bus even though
1331b3b25f2cSJustin T. Gibbs	 * no identified transaction exists should a
1332b3b25f2cSJustin T. Gibbs	 * timeout occur while awaiting busfree.
1333b3b25f2cSJustin T. Gibbs	 */
1334b3b25f2cSJustin T. Gibbs	mvi	LASTPHASE, P_BUSFREE;
133517d24755SJustin T. Gibbs	test	SSTAT1, BUSFREE jnz idle_loop;
13360c5aa4c5SScott Long	SET_SEQINTCODE(MISSED_BUSFREE)
133717d24755SJustin T. Gibbs
133817d24755SJustin T. Gibbs
133917d24755SJustin T. Gibbs/*
134017d24755SJustin T. Gibbs * Save data pointers message:
134117d24755SJustin T. Gibbs * Copying RAM values back to SCB, for Save Data Pointers message, but
134217d24755SJustin T. Gibbs * only if we've actually been into a data phase to change them.  This
134317d24755SJustin T. Gibbs * protects against bogus data in scratch ram and the residual counts
134417d24755SJustin T. Gibbs * since they are only initialized when we go into data_in or data_out.
13458fdc1261SJustin T. Gibbs * Ack the message as soon as possible.
134617d24755SJustin T. Gibbs */
134717d24755SJustin T. GibbsSET_SRC_MODE	M_DFF1;
134817d24755SJustin T. GibbsSET_DST_MODE	M_DFF1;
134917d24755SJustin T. Gibbsmesgin_sdptrs:
135017d24755SJustin T. Gibbs	mov	NONE,SCSIDAT;		/*dummy read from latch to ACK*/
135117d24755SJustin T. Gibbs	test	SEQ_FLAGS, DPHASE	jz ITloop;
135217d24755SJustin T. Gibbs	call	save_pointers;
135317d24755SJustin T. Gibbs	jmp	ITloop;
135417d24755SJustin T. Gibbs
135517d24755SJustin T. Gibbssave_pointers:
135617d24755SJustin T. Gibbs	/*
135717d24755SJustin T. Gibbs	 * If we are asked to save our position at the end of the
135817d24755SJustin T. Gibbs	 * transfer, just mark us at the end rather than perform a
135917d24755SJustin T. Gibbs	 * full save.
136017d24755SJustin T. Gibbs	 */
136117d24755SJustin T. Gibbs	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz save_pointers_full;
136217d24755SJustin T. Gibbs	or	SCB_SGPTR, SG_LIST_NULL ret;
136317d24755SJustin T. Gibbs
136417d24755SJustin T. Gibbssave_pointers_full:
136517d24755SJustin T. Gibbs	/*
136617d24755SJustin T. Gibbs	 * The SCB_DATAPTR becomes the current SHADDR.
136717d24755SJustin T. Gibbs	 * All other information comes directly from our residual
136817d24755SJustin T. Gibbs	 * state.
136917d24755SJustin T. Gibbs	 */
137017d24755SJustin T. Gibbs	bmov	SCB_DATAPTR, SHADDR, 8;
137117d24755SJustin T. Gibbs	bmov	SCB_DATACNT, SCB_RESIDUAL_DATACNT, 8 ret;
137217d24755SJustin T. Gibbs
137317d24755SJustin T. Gibbs/*
137417d24755SJustin T. Gibbs * Restore pointers message?  Data pointers are recopied from the
137517d24755SJustin T. Gibbs * SCB anytime we enter a data phase for the first time, so all
137617d24755SJustin T. Gibbs * we need to do is clear the DPHASE flag and let the data phase
137717d24755SJustin T. Gibbs * code do the rest.  We also reset/reallocate the FIFO to make
13780c5aa4c5SScott Long * sure we have a clean start for the next data or command phase.
137917d24755SJustin T. Gibbs */
138017d24755SJustin T. Gibbsmesgin_rdptrs:
138117d24755SJustin T. Gibbs	and	SEQ_FLAGS, ~DPHASE;
138217d24755SJustin T. Gibbs	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1)) jnz msgin_rdptrs_get_fifo;
138317d24755SJustin T. Gibbs	mvi	DFFSXFRCTL, RSTCHN|CLRSHCNT;
13840c5aa4c5SScott Long	SET_MODE(M_SCSI, M_SCSI)
138517d24755SJustin T. Gibbsmsgin_rdptrs_get_fifo:
138617d24755SJustin T. Gibbs	call	allocate_fifo;
138717d24755SJustin T. Gibbs	jmp	mesgin_done;
138817d24755SJustin T. Gibbs
138917d24755SJustin T. Gibbsphase_lock:
1390acae33b0SJustin T. Gibbs	if ((ahd->bugs & AHD_EARLY_REQ_BUG) != 0) {
1391acae33b0SJustin T. Gibbs		/*
1392acae33b0SJustin T. Gibbs		 * Don't ignore persistent REQ assertions just because
1393acae33b0SJustin T. Gibbs		 * they were asserted within the bus settle delay window.
1394acae33b0SJustin T. Gibbs		 * This allows us to tolerate devices like the GEM318
1395acae33b0SJustin T. Gibbs		 * that violate the SCSI spec.  We are careful not to
1396acae33b0SJustin T. Gibbs		 * count REQ while we are waiting for it to fall during
1397acae33b0SJustin T. Gibbs		 * an async phase due to our asserted ACK.  Each
1398acae33b0SJustin T. Gibbs		 * sequencer instruction takes ~25ns, so the REQ must
1399acae33b0SJustin T. Gibbs		 * last at least 100ns in order to be counted as a true
1400acae33b0SJustin T. Gibbs		 * REQ.
1401acae33b0SJustin T. Gibbs		 */
1402acae33b0SJustin T. Gibbs		test	SCSIPHASE, 0xFF jnz phase_locked;
1403acae33b0SJustin T. Gibbs		test	SCSISIGI, ACKI jnz phase_lock;
1404acae33b0SJustin T. Gibbs		test	SCSISIGI, REQI jz phase_lock;
1405acae33b0SJustin T. Gibbs		test	SCSIPHASE, 0xFF jnz phase_locked;
1406acae33b0SJustin T. Gibbs		test	SCSISIGI, ACKI jnz phase_lock;
1407acae33b0SJustin T. Gibbs		test	SCSISIGI, REQI jz phase_lock;
1408acae33b0SJustin T. Gibbsphase_locked:
1409acae33b0SJustin T. Gibbs	} else {
141017d24755SJustin T. Gibbs		test	SCSIPHASE, 0xFF jz .;
1411acae33b0SJustin T. Gibbs	}
14120c5aa4c5SScott Long	test	SSTAT1, SCSIPERR jnz phase_lock;
141317d24755SJustin T. Gibbsphase_lock_latch_phase:
141417d24755SJustin T. Gibbs	and	LASTPHASE, PHASE_MASK, SCSISIGI ret;
141517d24755SJustin T. Gibbs
141617d24755SJustin T. Gibbs/*
141717d24755SJustin T. Gibbs * Functions to read data in Automatic PIO mode.
141817d24755SJustin T. Gibbs *
141917d24755SJustin T. Gibbs * An ACK is not sent on input from the target until SCSIDATL is read from.
142017d24755SJustin T. Gibbs * So we wait until SCSIDATL is latched (the usual way), then read the data
142117d24755SJustin T. Gibbs * byte directly off the bus using SCSIBUSL.  When we have pulled the ATN
142217d24755SJustin T. Gibbs * line, or we just want to acknowledge the byte, then we do a dummy read
142317d24755SJustin T. Gibbs * from SCISDATL.  The SCSI spec guarantees that the target will hold the
142417d24755SJustin T. Gibbs * data byte on the bus until we send our ACK.
142517d24755SJustin T. Gibbs *
142617d24755SJustin T. Gibbs * The assumption here is that these are called in a particular sequence,
142717d24755SJustin T. Gibbs * and that REQ is already set when inb_first is called.  inb_{first,next}
142817d24755SJustin T. Gibbs * use the same calling convention as inb.
142917d24755SJustin T. Gibbs */
143017d24755SJustin T. Gibbsinb_next:
143117d24755SJustin T. Gibbs	mov	NONE,SCSIDAT;		/*dummy read from latch to ACK*/
143217d24755SJustin T. Gibbsinb_next_wait:
143317d24755SJustin T. Gibbs	/*
143417d24755SJustin T. Gibbs	 * If there is a parity error, wait for the kernel to
143517d24755SJustin T. Gibbs	 * see the interrupt and prepare our message response
143617d24755SJustin T. Gibbs	 * before continuing.
143717d24755SJustin T. Gibbs	 */
143817d24755SJustin T. Gibbs	test	SCSIPHASE, 0xFF jz .;
14390c5aa4c5SScott Long	test	SSTAT1, SCSIPERR jnz inb_next_wait;
144017d24755SJustin T. Gibbsinb_next_check_phase:
144117d24755SJustin T. Gibbs	and	LASTPHASE, PHASE_MASK, SCSISIGI;
144217d24755SJustin T. Gibbs	cmp	LASTPHASE, P_MESGIN jne mesgin_phasemis;
144317d24755SJustin T. Gibbsinb_first:
144417d24755SJustin T. Gibbs	clr	DINDEX[1];
144517d24755SJustin T. Gibbs	mov	DINDEX,SINDEX;
144617d24755SJustin T. Gibbs	mov	DINDIR,SCSIBUS	ret;		/*read byte directly from bus*/
144717d24755SJustin T. Gibbsinb_last:
144817d24755SJustin T. Gibbs	mov	NONE,SCSIDAT ret;		/*dummy read from latch to ACK*/
144917d24755SJustin T. Gibbs
145017d24755SJustin T. Gibbsmk_mesg:
145117d24755SJustin T. Gibbs	mvi	SCSISIGO, ATNO;
145217d24755SJustin T. Gibbs	mov	MSG_OUT,SINDEX ret;
145317d24755SJustin T. Gibbs
145417d24755SJustin T. GibbsSET_SRC_MODE	M_DFF1;
145517d24755SJustin T. GibbsSET_DST_MODE	M_DFF1;
145617d24755SJustin T. Gibbsdisable_ccsgen:
14578fdc1261SJustin T. Gibbs	test	SG_STATE, FETCH_INPROG jz disable_ccsgen_fetch_done;
14588fdc1261SJustin T. Gibbs	clr	CCSGCTL;
145917d24755SJustin T. Gibbsdisable_ccsgen_fetch_done:
14608fdc1261SJustin T. Gibbs	clr	SG_STATE ret;
146117d24755SJustin T. Gibbs
146217d24755SJustin T. Gibbsservice_fifo:
146317d24755SJustin T. Gibbs	/*
146417d24755SJustin T. Gibbs	 * Do we have any prefetch left???
146517d24755SJustin T. Gibbs	 */
146617d24755SJustin T. Gibbs	test	SG_STATE, SEGS_AVAIL jnz idle_sg_avail;
146717d24755SJustin T. Gibbs
146817d24755SJustin T. Gibbs	/*
146917d24755SJustin T. Gibbs	 * Can this FIFO have access to the S/G cache yet?
147017d24755SJustin T. Gibbs	 */
147117d24755SJustin T. Gibbs	test	CCSGCTL, SG_CACHE_AVAIL jz return;
147217d24755SJustin T. Gibbs
147317d24755SJustin T. Gibbs	/* Did we just finish fetching segs? */
14740c5aa4c5SScott Long	test	CCSGCTL, CCSGDONE jnz idle_sgfetch_complete;
147517d24755SJustin T. Gibbs
147617d24755SJustin T. Gibbs	/* Are we actively fetching segments? */
14770c5aa4c5SScott Long	test	CCSGCTL, CCSGENACK jnz return;
147817d24755SJustin T. Gibbs
147917d24755SJustin T. Gibbs	/*
14804164174aSJustin T. Gibbs	 * Should the other FIFO get the S/G cache first?  If
14814164174aSJustin T. Gibbs	 * both FIFOs have been allocated since we last checked
14824164174aSJustin T. Gibbs	 * any FIFO, it is important that we service a FIFO
14834164174aSJustin T. Gibbs	 * that is not actively on the bus first.  This guarantees
14844164174aSJustin T. Gibbs	 * that a FIFO will be freed to handle snapshot requests for
14854164174aSJustin T. Gibbs	 * any FIFO that is still on the bus.  Chips with RTI do not
14864164174aSJustin T. Gibbs	 * perform snapshots, so don't bother with this test there.
14874164174aSJustin T. Gibbs	 */
14884164174aSJustin T. Gibbs	if ((ahd->features & AHD_RTI) == 0) {
14894164174aSJustin T. Gibbs		/*
14904164174aSJustin T. Gibbs		 * If we're not still receiving SCSI data,
14914164174aSJustin T. Gibbs		 * it is safe to allocate the S/G cache to
14924164174aSJustin T. Gibbs		 * this FIFO.
14934164174aSJustin T. Gibbs		 */
14944164174aSJustin T. Gibbs		test	DFCNTRL, SCSIEN jz idle_sgfetch_start;
14954164174aSJustin T. Gibbs
14964164174aSJustin T. Gibbs		/*
14974164174aSJustin T. Gibbs		 * Switch to the other FIFO.  Non-RTI chips
14984164174aSJustin T. Gibbs		 * also have the "set mode" bug, so we must
14994164174aSJustin T. Gibbs		 * disable interrupts during the switch.
15004164174aSJustin T. Gibbs		 */
15014164174aSJustin T. Gibbs		mvi	SEQINTCTL, INTVEC1DSL;
15024164174aSJustin T. Gibbs		xor	MODE_PTR, MK_MODE(M_DFF1, M_DFF1);
15034164174aSJustin T. Gibbs
15044164174aSJustin T. Gibbs		/*
15054164174aSJustin T. Gibbs		 * If the other FIFO needs loading, then it
15064164174aSJustin T. Gibbs		 * must not have claimed the S/G cache yet
15074164174aSJustin T. Gibbs		 * (SG_CACHE_AVAIL would have been cleared in
1508594c945aSPedro F. Giffuni		 * the original FIFO mode and we test this above).
15094164174aSJustin T. Gibbs		 * Return to the idle loop so we can process the
15104164174aSJustin T. Gibbs		 * FIFO not currently on the bus first.
15114164174aSJustin T. Gibbs		 */
15124164174aSJustin T. Gibbs		test	SG_STATE, LOADING_NEEDED jz idle_sgfetch_okay;
15134164174aSJustin T. Gibbs		clr	SEQINTCTL ret;
15144164174aSJustin T. Gibbsidle_sgfetch_okay:
15154164174aSJustin T. Gibbs		xor	MODE_PTR, MK_MODE(M_DFF1, M_DFF1);
15164164174aSJustin T. Gibbs		clr	SEQINTCTL;
15174164174aSJustin T. Gibbs	}
15184164174aSJustin T. Gibbs
15194164174aSJustin T. Gibbsidle_sgfetch_start:
15204164174aSJustin T. Gibbs	/*
152117d24755SJustin T. Gibbs	 * We fetch a "cacheline aligned" and sized amount of data
152217d24755SJustin T. Gibbs	 * so we don't end up referencing a non-existant page.
152317d24755SJustin T. Gibbs	 * Cacheline aligned is in quotes because the kernel will
152417d24755SJustin T. Gibbs	 * set the prefetch amount to a reasonable level if the
152517d24755SJustin T. Gibbs	 * cacheline size is unknown.
152617d24755SJustin T. Gibbs	 */
15270c5aa4c5SScott Long	bmov	SGHADDR, SCB_RESIDUAL_SGPTR, 4;
1528c59c8a72SJustin T. Gibbs	mvi	SGHCNT, SG_PREFETCH_CNT;
1529c59c8a72SJustin T. Gibbs	if ((ahd->bugs & AHD_REG_SLOW_SETTLE_BUG) != 0) {
1530c59c8a72SJustin T. Gibbs		/*
15314164174aSJustin T. Gibbs		 * Need two instructions between "touches" of SGHADDR.
1532c59c8a72SJustin T. Gibbs		 */
1533c59c8a72SJustin T. Gibbs		nop;
1534c59c8a72SJustin T. Gibbs	}
15350c5aa4c5SScott Long	and	SGHADDR[0], SG_PREFETCH_ALIGN_MASK, SCB_RESIDUAL_SGPTR;
1536acae33b0SJustin T. Gibbs	mvi	CCSGCTL, CCSGEN|CCSGRESET;
153717d24755SJustin T. Gibbs	or	SG_STATE, FETCH_INPROG ret;
153817d24755SJustin T. Gibbsidle_sgfetch_complete:
153917d24755SJustin T. Gibbs	/*
154017d24755SJustin T. Gibbs	 * Guard against SG_CACHE_AVAIL activating during sg fetch
154117d24755SJustin T. Gibbs	 * request in the other FIFO.
154217d24755SJustin T. Gibbs	 */
154317d24755SJustin T. Gibbs	test	SG_STATE, FETCH_INPROG jz return;
15448fdc1261SJustin T. Gibbs	clr	CCSGCTL;
154517d24755SJustin T. Gibbs	and	CCSGADDR, SG_PREFETCH_ADDR_MASK, SCB_RESIDUAL_SGPTR;
154617d24755SJustin T. Gibbs	mvi	SG_STATE, SEGS_AVAIL|LOADING_NEEDED;
154717d24755SJustin T. Gibbsidle_sg_avail:
154817d24755SJustin T. Gibbs	/* Does the hardware have space for another SG entry? */
154917d24755SJustin T. Gibbs	test	DFSTATUS, PRELOAD_AVAIL jz return;
1550acae33b0SJustin T. Gibbs	/*
1551acae33b0SJustin T. Gibbs	 * On the A, preloading a segment before HDMAENACK
1552594c945aSPedro F. Giffuni	 * comes true can clobber the shadow address of the
1553acae33b0SJustin T. Gibbs	 * first segment in the S/G FIFO.  Wait until it is
1554acae33b0SJustin T. Gibbs	 * safe to proceed.
1555acae33b0SJustin T. Gibbs	 */
1556acae33b0SJustin T. Gibbs	if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) == 0) {
1557acae33b0SJustin T. Gibbs		test	DFCNTRL, HDMAENACK jz return;
1558acae33b0SJustin T. Gibbs	}
155917d24755SJustin T. Gibbs	if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
156017d24755SJustin T. Gibbs		bmov	HADDR, CCSGRAM, 8;
156117d24755SJustin T. Gibbs	} else {
156217d24755SJustin T. Gibbs		bmov 	HADDR, CCSGRAM, 4;
156317d24755SJustin T. Gibbs	}
156417d24755SJustin T. Gibbs	bmov	HCNT, CCSGRAM, 3;
156517d24755SJustin T. Gibbs	bmov	SCB_RESIDUAL_DATACNT[3], CCSGRAM, 1;
156617d24755SJustin T. Gibbs	if ((ahd->flags & AHD_39BIT_ADDRESSING) != 0) {
156717d24755SJustin T. Gibbs		and	HADDR[4], SG_HIGH_ADDR_BITS, SCB_RESIDUAL_DATACNT[3];
156817d24755SJustin T. Gibbs	}
156917d24755SJustin T. Gibbs	if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
157017d24755SJustin T. Gibbs		/* Skip 4 bytes of pad. */
157117d24755SJustin T. Gibbs		add	CCSGADDR, 4;
157217d24755SJustin T. Gibbs	}
157317d24755SJustin T. Gibbssg_advance:
157417d24755SJustin T. Gibbs	clr	A;			/* add sizeof(struct scatter) */
157517d24755SJustin T. Gibbs	add	SCB_RESIDUAL_SGPTR[0],SG_SIZEOF;
157617d24755SJustin T. Gibbs	adc	SCB_RESIDUAL_SGPTR[1],A;
157717d24755SJustin T. Gibbs	adc	SCB_RESIDUAL_SGPTR[2],A;
157817d24755SJustin T. Gibbs	adc	SCB_RESIDUAL_SGPTR[3],A;
157917d24755SJustin T. Gibbs	mov	SINDEX, SCB_RESIDUAL_SGPTR[0];
158017d24755SJustin T. Gibbs	test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz . + 3;
158117d24755SJustin T. Gibbs	or	SINDEX, LAST_SEG;
158217d24755SJustin T. Gibbs	clr	SG_STATE;
158317d24755SJustin T. Gibbs	mov	SG_CACHE_PRE, SINDEX;
15840c5aa4c5SScott Long	if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) != 0) {
15850c5aa4c5SScott Long		/*
15860c5aa4c5SScott Long		 * Use SCSIENWRDIS so that SCSIEN is never
15870c5aa4c5SScott Long		 * modified by this operation.
15880c5aa4c5SScott Long		 */
1589acae33b0SJustin T. Gibbs		or	DFCNTRL, PRELOADEN|HDMAEN|SCSIENWRDIS;
15900c5aa4c5SScott Long	} else {
159117d24755SJustin T. Gibbs		or	DFCNTRL, PRELOADEN|HDMAEN;
15920c5aa4c5SScott Long	}
159317d24755SJustin T. Gibbs	/*
159417d24755SJustin T. Gibbs	 * Do we have another segment in the cache?
159517d24755SJustin T. Gibbs	 */
159617d24755SJustin T. Gibbs	add	NONE, SG_PREFETCH_CNT_LIMIT, CCSGADDR;
159717d24755SJustin T. Gibbs	jnc	return;
159817d24755SJustin T. Gibbs	and	SG_STATE, ~SEGS_AVAIL ret;
159917d24755SJustin T. Gibbs
160017d24755SJustin T. Gibbs/*
160117d24755SJustin T. Gibbs * Initialize the DMA address and counter from the SCB.
160217d24755SJustin T. Gibbs */
160317d24755SJustin T. Gibbsload_first_seg:
160417d24755SJustin T. Gibbs	bmov	HADDR, SCB_DATAPTR, 11;
1605c59c8a72SJustin T. Gibbs	and	REG_ISR, ~SG_FULL_RESID, SCB_SGPTR[0];
160617d24755SJustin T. Gibbs	test	SCB_DATACNT[3], SG_LAST_SEG jz . + 2;
1607c59c8a72SJustin T. Gibbs	or	REG_ISR, LAST_SEG;
1608c59c8a72SJustin T. Gibbs	mov	SG_CACHE_PRE, REG_ISR;
160917d24755SJustin T. Gibbs	mvi	DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN);
161017d24755SJustin T. Gibbs	/*
161117d24755SJustin T. Gibbs	 * Since we've are entering a data phase, we will
161217d24755SJustin T. Gibbs	 * rely on the SCB_RESID* fields.  Initialize the
161317d24755SJustin T. Gibbs	 * residual and clear the full residual flag.
161417d24755SJustin T. Gibbs	 */
161517d24755SJustin T. Gibbs	and	SCB_SGPTR[0], ~SG_FULL_RESID;
161617d24755SJustin T. Gibbs	bmov	SCB_RESIDUAL_DATACNT[3], SCB_DATACNT[3], 5;
161717d24755SJustin T. Gibbs	/* If we need more S/G elements, tell the idle loop */
161817d24755SJustin T. Gibbs	test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jnz . + 2;
161917d24755SJustin T. Gibbs	mvi	SG_STATE, LOADING_NEEDED ret;
162017d24755SJustin T. Gibbs	clr	SG_STATE ret;
162117d24755SJustin T. Gibbs
16220c5aa4c5SScott Longp_data_handle_xfer:
1623d7cff4abSJustin T. Gibbs	call	setjmp;
16240c5aa4c5SScott Long	test	SG_STATE, LOADING_NEEDED jnz service_fifo;
16250c5aa4c5SScott Longp_data_clear_handler:
16260c5aa4c5SScott Long	or	LONGJMP_ADDR[1], INVALID_ADDR ret;
16270c5aa4c5SScott Long
162817d24755SJustin T. Gibbsp_data:
162917d24755SJustin T. Gibbs	test	SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT	jz p_data_allowed;
16300c5aa4c5SScott Long	SET_SEQINTCODE(PROTO_VIOLATION)
163117d24755SJustin T. Gibbsp_data_allowed:
163217d24755SJustin T. Gibbs
163317d24755SJustin T. Gibbs	test	SEQ_FLAGS, DPHASE	jz data_phase_initialize;
163417d24755SJustin T. Gibbs
163517d24755SJustin T. Gibbs	/*
163617d24755SJustin T. Gibbs	 * If we re-enter the data phase after going through another
163717d24755SJustin T. Gibbs	 * phase, our transfer location has almost certainly been
163817d24755SJustin T. Gibbs	 * corrupted by the interveining, non-data, transfers.  Ask
163917d24755SJustin T. Gibbs	 * the host driver to fix us up based on the transfer residual
164017d24755SJustin T. Gibbs	 * unless we already know that we should be bitbucketing.
164117d24755SJustin T. Gibbs	 */
164217d24755SJustin T. Gibbs	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jnz p_data_bitbucket;
16430c5aa4c5SScott Long	SET_SEQINTCODE(PDATA_REINIT)
16440c5aa4c5SScott Long	jmp	data_phase_inbounds;
164517d24755SJustin T. Gibbs
164617d24755SJustin T. Gibbsp_data_bitbucket:
164717d24755SJustin T. Gibbs	/*
164817d24755SJustin T. Gibbs	 * Turn on `Bit Bucket' mode, wait until the target takes
164917d24755SJustin T. Gibbs	 * us to another phase, and then notify the host.
165017d24755SJustin T. Gibbs	 */
16510c5aa4c5SScott Long	mov	SAVED_MODE, MODE_PTR;
165217d24755SJustin T. Gibbs	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
165317d24755SJustin T. Gibbs		jnz bitbucket_not_m_dff;
165417d24755SJustin T. Gibbs	/*
165517d24755SJustin T. Gibbs	 * Ensure that any FIFO contents are cleared out and the
165617d24755SJustin T. Gibbs	 * FIFO free'd prior to starting the BITBUCKET.  BITBUCKET
165717d24755SJustin T. Gibbs	 * doesn't discard data already in the FIFO.
165817d24755SJustin T. Gibbs	 */
165917d24755SJustin T. Gibbs	mvi	DFFSXFRCTL, RSTCHN|CLRSHCNT;
1660c59c8a72SJustin T. Gibbs	SET_MODE(M_SCSI, M_SCSI)
166117d24755SJustin T. Gibbsbitbucket_not_m_dff:
166217d24755SJustin T. Gibbs	or	SXFRCTL1,BITBUCKET;
1663c59c8a72SJustin T. Gibbs	/* Wait for non-data phase. */
1664c59c8a72SJustin T. Gibbs	test	SCSIPHASE, ~DATA_PHASE_MASK jz .;
166517d24755SJustin T. Gibbs	and	SXFRCTL1, ~BITBUCKET;
16660c5aa4c5SScott Long	RESTORE_MODE(SAVED_MODE)
16670c5aa4c5SScott LongSET_SRC_MODE	M_DFF1;
16680c5aa4c5SScott LongSET_DST_MODE	M_DFF1;
16690c5aa4c5SScott Long	SET_SEQINTCODE(DATA_OVERRUN)
167017d24755SJustin T. Gibbs	jmp	ITloop;
167117d24755SJustin T. Gibbs
167217d24755SJustin T. Gibbsdata_phase_initialize:
167317d24755SJustin T. Gibbs	test	SCB_SGPTR[0], SG_LIST_NULL jnz p_data_bitbucket;
167417d24755SJustin T. Gibbs	call	load_first_seg;
167517d24755SJustin T. Gibbsdata_phase_inbounds:
167617d24755SJustin T. Gibbs	/* We have seen a data phase at least once. */
167717d24755SJustin T. Gibbs	or	SEQ_FLAGS, DPHASE;
16780c5aa4c5SScott Long	mov	SAVED_MODE, MODE_PTR;
16790c5aa4c5SScott Long	test	SG_STATE, LOADING_NEEDED jz data_group_dma_loop;
16800c5aa4c5SScott Long	call	p_data_handle_xfer;
168117d24755SJustin T. Gibbsdata_group_dma_loop:
168217d24755SJustin T. Gibbs	/*
168317d24755SJustin T. Gibbs	 * The transfer is complete if either the last segment
168417d24755SJustin T. Gibbs	 * completes or the target changes phase.  Both conditions
16858fdc1261SJustin T. Gibbs	 * will clear SCSIEN.
168617d24755SJustin T. Gibbs	 */
16870c5aa4c5SScott Long	call	idle_loop_service_fifos;
16880c5aa4c5SScott Long	call	idle_loop_cchan;
16890c5aa4c5SScott Long	call	idle_loop_gsfifo;
16900c5aa4c5SScott Long	RESTORE_MODE(SAVED_MODE)
169117d24755SJustin T. Gibbs	test	DFCNTRL, SCSIEN jnz data_group_dma_loop;
169217d24755SJustin T. Gibbs
169317d24755SJustin T. Gibbsdata_group_dmafinish:
169417d24755SJustin T. Gibbs	/*
169517d24755SJustin T. Gibbs	 * The transfer has terminated either due to a phase
169617d24755SJustin T. Gibbs	 * change, and/or the completion of the last segment.
169717d24755SJustin T. Gibbs	 * We have two goals here.  Do as much other work
169817d24755SJustin T. Gibbs	 * as possible while the data fifo drains on a read
169917d24755SJustin T. Gibbs	 * and respond as quickly as possible to the standard
170017d24755SJustin T. Gibbs	 * messages (save data pointers/disconnect and command
170117d24755SJustin T. Gibbs	 * complete) that usually follow a data phase.
170217d24755SJustin T. Gibbs	 */
170317d24755SJustin T. Gibbs	call	calc_residual;
170417d24755SJustin T. Gibbs
170517d24755SJustin T. Gibbs	/*
170617d24755SJustin T. Gibbs	 * Go ahead and shut down the DMA engine now.
170717d24755SJustin T. Gibbs	 */
170817d24755SJustin T. Gibbs	test	DFCNTRL, DIRECTION jnz data_phase_finish;
170917d24755SJustin T. Gibbsdata_group_fifoflush:
171017d24755SJustin T. Gibbs	if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
171117d24755SJustin T. Gibbs		or	DFCNTRL, FIFOFLUSH;
171217d24755SJustin T. Gibbs	}
171317d24755SJustin T. Gibbs	/*
171417d24755SJustin T. Gibbs	 * We have enabled the auto-ack feature.  This means
171517d24755SJustin T. Gibbs	 * that the controller may have already transferred
171617d24755SJustin T. Gibbs	 * some overrun bytes into the data FIFO and acked them
171717d24755SJustin T. Gibbs	 * on the bus.  The only way to detect this situation is
171817d24755SJustin T. Gibbs	 * to wait for LAST_SEG_DONE to come true on a completed
171917d24755SJustin T. Gibbs	 * transfer and then test to see if the data FIFO is
172017d24755SJustin T. Gibbs	 * non-empty.  We know there is more data yet to transfer
172117d24755SJustin T. Gibbs	 * if SG_LIST_NULL is not yet set, thus there cannot be
172217d24755SJustin T. Gibbs	 * an overrun.
172317d24755SJustin T. Gibbs	 */
172417d24755SJustin T. Gibbs	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz data_phase_finish;
172517d24755SJustin T. Gibbs	test	SG_CACHE_SHADOW, LAST_SEG_DONE jz .;
172617d24755SJustin T. Gibbs	test	DFSTATUS, FIFOEMP jnz data_phase_finish;
172717d24755SJustin T. Gibbs	/* Overrun */
172817d24755SJustin T. Gibbs	jmp	p_data;
172917d24755SJustin T. Gibbsdata_phase_finish:
173017d24755SJustin T. Gibbs	/*
173117d24755SJustin T. Gibbs	 * If the target has left us in data phase, loop through
173217d24755SJustin T. Gibbs	 * the dma code again.  We will only loop if there is a
173317d24755SJustin T. Gibbs	 * data overrun.
173417d24755SJustin T. Gibbs	 */
173517d24755SJustin T. Gibbs	if ((ahd->flags & AHD_TARGETROLE) != 0) {
173617d24755SJustin T. Gibbs		test	SSTAT0, TARGET jnz data_phase_done;
173717d24755SJustin T. Gibbs	}
173817d24755SJustin T. Gibbs	if ((ahd->flags & AHD_INITIATORROLE) != 0) {
173917d24755SJustin T. Gibbs		test	SSTAT1, REQINIT jz .;
174017d24755SJustin T. Gibbs		test	SCSIPHASE, DATA_PHASE_MASK jnz p_data;
174117d24755SJustin T. Gibbs	}
174217d24755SJustin T. Gibbs
174317d24755SJustin T. Gibbsdata_phase_done:
174417d24755SJustin T. Gibbs	/* Kill off any pending prefetch */
174517d24755SJustin T. Gibbs	call	disable_ccsgen;
17460c5aa4c5SScott Long	or 	LONGJMP_ADDR[1], INVALID_ADDR;
174717d24755SJustin T. Gibbs
174817d24755SJustin T. Gibbs	if ((ahd->flags & AHD_TARGETROLE) != 0) {
174917d24755SJustin T. Gibbs		test	SEQ_FLAGS, DPHASE_PENDING jz ITloop;
175017d24755SJustin T. Gibbs		/*
175117d24755SJustin T. Gibbs		and	SEQ_FLAGS, ~DPHASE_PENDING;
175217d24755SJustin T. Gibbs		 * For data-in phases, wait for any pending acks from the
175317d24755SJustin T. Gibbs		 * initiator before changing phase.  We only need to
175417d24755SJustin T. Gibbs		 * send Ignore Wide Residue messages for data-in phases.
175517d24755SJustin T. Gibbs		test	DFCNTRL, DIRECTION jz target_ITloop;
175617d24755SJustin T. Gibbs		test	SSTAT1, REQINIT	jnz .;
1757333f04d9SJustin T. Gibbs		test	SCB_TASK_ATTRIBUTE, SCB_XFERLEN_ODD jz target_ITloop;
1758c59c8a72SJustin T. Gibbs		SET_MODE(M_SCSI, M_SCSI)
175917d24755SJustin T. Gibbs		test	NEGCONOPTS, WIDEXFER jz target_ITloop;
176017d24755SJustin T. Gibbs		 */
176117d24755SJustin T. Gibbs		/*
176217d24755SJustin T. Gibbs		 * Issue an Ignore Wide Residue Message.
176317d24755SJustin T. Gibbs		mvi	P_MESGIN|BSYO call change_phase;
176417d24755SJustin T. Gibbs		mvi	MSG_IGN_WIDE_RESIDUE call target_outb;
176517d24755SJustin T. Gibbs		mvi	1 call target_outb;
176617d24755SJustin T. Gibbs		jmp	target_ITloop;
176717d24755SJustin T. Gibbs		 */
176817d24755SJustin T. Gibbs	} else {
176917d24755SJustin T. Gibbs		jmp	ITloop;
177017d24755SJustin T. Gibbs	}
177117d24755SJustin T. Gibbs
177217d24755SJustin T. Gibbs/*
177317d24755SJustin T. Gibbs * We assume that, even though data may still be
177417d24755SJustin T. Gibbs * transferring to the host, that the SCSI side of
177517d24755SJustin T. Gibbs * the DMA engine is now in a static state.  This
177617d24755SJustin T. Gibbs * allows us to update our notion of where we are
177717d24755SJustin T. Gibbs * in this transfer.
177817d24755SJustin T. Gibbs *
177917d24755SJustin T. Gibbs * If, by chance, we stopped before being able
178017d24755SJustin T. Gibbs * to fetch additional segments for this transfer,
178117d24755SJustin T. Gibbs * yet the last S/G was completely exhausted,
178217d24755SJustin T. Gibbs * call our idle loop until it is able to load
178317d24755SJustin T. Gibbs * another segment.  This will allow us to immediately
178417d24755SJustin T. Gibbs * pickup on the next segment on the next data phase.
178517d24755SJustin T. Gibbs *
178617d24755SJustin T. Gibbs * If we happened to stop on the last segment, then
178717d24755SJustin T. Gibbs * our residual information is still correct from
178817d24755SJustin T. Gibbs * the idle loop and there is no need to perform
178917d24755SJustin T. Gibbs * any fixups.
179017d24755SJustin T. Gibbs */
179117d24755SJustin T. Gibbsresidual_before_last_seg:
179217d24755SJustin T. Gibbs	test    MDFFSTAT, SHVALID	jnz sgptr_fixup;
179317d24755SJustin T. Gibbs	/*
179417d24755SJustin T. Gibbs	 * Can never happen from an interrupt as the packetized
179517d24755SJustin T. Gibbs	 * hardware will only interrupt us once SHVALID or
179617d24755SJustin T. Gibbs	 * LAST_SEG_DONE.
179717d24755SJustin T. Gibbs	 */
17980c5aa4c5SScott Long	call	idle_loop_service_fifos;
17990c5aa4c5SScott Long	RESTORE_MODE(SAVED_MODE)
1800acae33b0SJustin T. Gibbs	/* FALLTHROUGH */
1801acae33b0SJustin T. Gibbscalc_residual:
1802acae33b0SJustin T. Gibbs	test	SG_CACHE_SHADOW, LAST_SEG jz residual_before_last_seg;
1803acae33b0SJustin T. Gibbs	/* Record if we've consumed all S/G entries */
1804acae33b0SJustin T. Gibbs	test	MDFFSTAT, SHVALID	jz . + 2;
1805acae33b0SJustin T. Gibbs	bmov	SCB_RESIDUAL_DATACNT, SHCNT, 3 ret;
1806acae33b0SJustin T. Gibbs	or	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL ret;
180717d24755SJustin T. Gibbs
180817d24755SJustin T. Gibbssgptr_fixup:
180917d24755SJustin T. Gibbs	/*
181017d24755SJustin T. Gibbs	 * Fixup the residual next S/G pointer.  The S/G preload
181117d24755SJustin T. Gibbs	 * feature of the chip allows us to load two elements
181217d24755SJustin T. Gibbs	 * in addition to the currently active element.  We
181317d24755SJustin T. Gibbs	 * store the bottom byte of the next S/G pointer in
181417d24755SJustin T. Gibbs	 * the SG_CACHE_PTR register so we can restore the
181517d24755SJustin T. Gibbs	 * correct value when the DMA completes.  If the next
181617d24755SJustin T. Gibbs	 * sg ptr value has advanced to the point where higher
181717d24755SJustin T. Gibbs	 * bytes in the address have been affected, fix them
181817d24755SJustin T. Gibbs	 * too.
181917d24755SJustin T. Gibbs	 */
182017d24755SJustin T. Gibbs	test	SG_CACHE_SHADOW, 0x80 jz sgptr_fixup_done;
182117d24755SJustin T. Gibbs	test	SCB_RESIDUAL_SGPTR[0], 0x80 jnz sgptr_fixup_done;
182217d24755SJustin T. Gibbs	add	SCB_RESIDUAL_SGPTR[1], -1;
182317d24755SJustin T. Gibbs	adc	SCB_RESIDUAL_SGPTR[2], -1;
182417d24755SJustin T. Gibbs	adc	SCB_RESIDUAL_SGPTR[3], -1;
182517d24755SJustin T. Gibbssgptr_fixup_done:
182617d24755SJustin T. Gibbs	and	SCB_RESIDUAL_SGPTR[0], SG_ADDR_MASK, SG_CACHE_SHADOW;
182717d24755SJustin T. Gibbs	clr	SCB_RESIDUAL_DATACNT[3]; /* We are not the last seg */
182817d24755SJustin T. Gibbs	bmov	SCB_RESIDUAL_DATACNT, SHCNT, 3 ret;
182917d24755SJustin T. Gibbs
18300794987dSJustin T. Gibbsexport timer_isr:
18310794987dSJustin T. Gibbs	call	issue_cmdcmplt;
18320794987dSJustin T. Gibbs	mvi	CLRSEQINTSTAT, CLRSEQ_SWTMRTO;
18330794987dSJustin T. Gibbs	if ((ahd->bugs & AHD_SET_MODE_BUG) != 0) {
18340794987dSJustin T. Gibbs		/*
18350794987dSJustin T. Gibbs		 * In H2A4, the mode pointer is not saved
18360794987dSJustin T. Gibbs		 * for intvec2, but is restored on iret.
18370794987dSJustin T. Gibbs		 * This can lead to the restoration of a
18380794987dSJustin T. Gibbs		 * bogus mode ptr.  Manually clear the
18390794987dSJustin T. Gibbs		 * intmask bits and do a normal return
18400794987dSJustin T. Gibbs		 * to compensate.
18410794987dSJustin T. Gibbs		 */
18420794987dSJustin T. Gibbs		and	SEQINTCTL, ~(INTMASK2|INTMASK1) ret;
18430794987dSJustin T. Gibbs	} else {
18440794987dSJustin T. Gibbs		or	SEQINTCTL, IRET ret;
18450794987dSJustin T. Gibbs	}
18460794987dSJustin T. Gibbs
184717d24755SJustin T. Gibbsexport seq_isr:
1848acae33b0SJustin T. Gibbs	if ((ahd->features & AHD_RTI) == 0) {
1849acae33b0SJustin T. Gibbs		/*
1850acae33b0SJustin T. Gibbs		 * On RevA Silicon, if the target returns us to data-out
1851acae33b0SJustin T. Gibbs		 * after we have already trained for data-out, it is
1852acae33b0SJustin T. Gibbs		 * possible for us to transition the free running clock to
1853acae33b0SJustin T. Gibbs		 * data-valid before the required 100ns P1 setup time (8 P1
1854acae33b0SJustin T. Gibbs		 * assertions in fast-160 mode).  This will only happen if
1855acae33b0SJustin T. Gibbs		 * this L-Q is a continuation of a data transfer for which
1856acae33b0SJustin T. Gibbs		 * we have already prefetched data into our FIFO (LQ/Data
1857acae33b0SJustin T. Gibbs		 * followed by LQ/Data for the same write transaction).
1858acae33b0SJustin T. Gibbs		 * This can cause some target implementations to miss the
1859acae33b0SJustin T. Gibbs		 * first few data transfers on the bus.  We detect this
1860acae33b0SJustin T. Gibbs		 * situation by noticing that this is the first data transfer
1861acae33b0SJustin T. Gibbs		 * after an LQ (LQIWORKONLQ true), that the data transfer is
1862acae33b0SJustin T. Gibbs		 * a continuation of a transfer already setup in our FIFO
1863acae33b0SJustin T. Gibbs		 * (SAVEPTRS interrupt), and that the transaction is a write
1864acae33b0SJustin T. Gibbs		 * (DIRECTION set in DFCNTRL). The delay is performed by
1865acae33b0SJustin T. Gibbs		 * disabling SCSIEN until we see the first REQ from the
1866acae33b0SJustin T. Gibbs		 * target.
1867acae33b0SJustin T. Gibbs		 *
1868acae33b0SJustin T. Gibbs		 * First instruction in an ISR cannot be a branch on
1869acae33b0SJustin T. Gibbs		 * Rev A.  Snapshot LQISTAT2 so the status is not missed
1870acae33b0SJustin T. Gibbs		 * and deffer the test by one instruction.
1871acae33b0SJustin T. Gibbs		 */
1872acae33b0SJustin T. Gibbs		mov	REG_ISR, LQISTAT2;
1873d7cff4abSJustin T. Gibbs		test	REG_ISR, LQIWORKONLQ jz main_isr;
1874d7cff4abSJustin T. Gibbs		test	SEQINTSRC, SAVEPTRS  jz main_isr;
1875acae33b0SJustin T. Gibbs		test	LONGJMP_ADDR[1], INVALID_ADDR jz saveptr_active_fifo;
1876acae33b0SJustin T. Gibbs		/*
1877d7cff4abSJustin T. Gibbs		 * Switch to the active FIFO after clearing the snapshot
1878d7cff4abSJustin T. Gibbs		 * savepointer in the current FIFO.  We do this so that
1879d7cff4abSJustin T. Gibbs		 * a pending CTXTDONE or SAVEPTR is visible in the active
1880d7cff4abSJustin T. Gibbs		 * FIFO.  This status is the only way we can detect if we
1881b3b25f2cSJustin T. Gibbs		 * have lost the race (e.g. host paused us) and our attempts
1882d7cff4abSJustin T. Gibbs		 * to disable the channel occurred after all REQs were
1883d7cff4abSJustin T. Gibbs		 * already seen and acked (REQINIT never comes true).
1884acae33b0SJustin T. Gibbs		 */
1885d7cff4abSJustin T. Gibbs		mvi	DFFSXFRCTL, CLRCHN;
1886acae33b0SJustin T. Gibbs		xor	MODE_PTR, MK_MODE(M_DFF1, M_DFF1);
1887d7cff4abSJustin T. Gibbs		test	DFCNTRL, DIRECTION jz interrupt_return;
1888acae33b0SJustin T. Gibbs		and	DFCNTRL, ~SCSIEN;
1889d7cff4abSJustin T. Gibbssnapshot_wait_data_valid:
1890b3b25f2cSJustin T. Gibbs		test	SEQINTSRC, (CTXTDONE|SAVEPTRS) jnz interrupt_return;
1891d7cff4abSJustin T. Gibbs		test	SSTAT1, REQINIT	jz snapshot_wait_data_valid;
1892d7cff4abSJustin T. Gibbssnapshot_data_valid:
1893acae33b0SJustin T. Gibbs		or	DFCNTRL, SCSIEN;
1894d7cff4abSJustin T. Gibbs		or	SEQINTCTL, IRET ret;
1895acae33b0SJustin T. Gibbssnapshot_saveptr:
1896acae33b0SJustin T. Gibbs		mvi	DFFSXFRCTL, CLRCHN;
1897acae33b0SJustin T. Gibbs		or	SEQINTCTL, IRET ret;
1898d7cff4abSJustin T. Gibbsmain_isr:
1899acae33b0SJustin T. Gibbs	}
190017d24755SJustin T. Gibbs	test	SEQINTSRC, CFG4DATA	jnz cfg4data_intr;
190117d24755SJustin T. Gibbs	test	SEQINTSRC, CFG4ISTAT	jnz cfg4istat_intr;
19020794987dSJustin T. Gibbs	test	SEQINTSRC, SAVEPTRS	jnz saveptr_intr;
190317d24755SJustin T. Gibbs	test	SEQINTSRC, CFG4ICMD	jnz cfg4icmd_intr;
19040c5aa4c5SScott Long	SET_SEQINTCODE(INVALID_SEQINT)
190517d24755SJustin T. Gibbs
190617d24755SJustin T. Gibbs/*
190717d24755SJustin T. Gibbs * There are two types of save pointers interrupts:
190817d24755SJustin T. Gibbs * The first is a snapshot save pointers where the current FIFO is not
190917d24755SJustin T. Gibbs * active and contains a snapshot of the current poniter information.
191017d24755SJustin T. Gibbs * This happens between packets in a stream for a single L_Q.  Since we
191117d24755SJustin T. Gibbs * are not performing a pointer save, we can safely clear the channel
1912acae33b0SJustin T. Gibbs * so it can be used for other transactions.  On RTI capable controllers,
1913acae33b0SJustin T. Gibbs * where snapshots can, and are, disabled, the code to handle this type
1914acae33b0SJustin T. Gibbs * of snapshot is not active.
191517d24755SJustin T. Gibbs *
191617d24755SJustin T. Gibbs * The second case is a save pointers on an active FIFO which occurs
1917acae33b0SJustin T. Gibbs * if the target changes to a new L_Q or busfrees/QASes and the transfer
191817d24755SJustin T. Gibbs * has a residual.  This should occur coincident with a ctxtdone.  We
191917d24755SJustin T. Gibbs * disable the interrupt and allow our active routine to handle the
192017d24755SJustin T. Gibbs * save.
192117d24755SJustin T. Gibbs */
192217d24755SJustin T. Gibbssaveptr_intr:
1923acae33b0SJustin T. Gibbs	if ((ahd->features & AHD_RTI) == 0) {
1924acae33b0SJustin T. Gibbs		test	LONGJMP_ADDR[1], INVALID_ADDR jnz snapshot_saveptr;
1925acae33b0SJustin T. Gibbs	}
1926acae33b0SJustin T. Gibbssaveptr_active_fifo:
192717d24755SJustin T. Gibbs	and	SEQIMODE, ~ENSAVEPTRS;
192817d24755SJustin T. Gibbs	or	SEQINTCTL, IRET ret;
192917d24755SJustin T. Gibbs
193017d24755SJustin T. Gibbscfg4data_intr:
1931d7cff4abSJustin T. Gibbs	test	SCB_SGPTR[0], SG_LIST_NULL jnz pkt_handle_overrun_inc_use_count;
193217d24755SJustin T. Gibbs	call	load_first_seg;
193317d24755SJustin T. Gibbs	call	pkt_handle_xfer;
1934d7cff4abSJustin T. Gibbs	inc	SCB_FIFO_USE_COUNT;
1935d7cff4abSJustin T. Gibbsinterrupt_return:
193617d24755SJustin T. Gibbs	or	SEQINTCTL, IRET ret;
193717d24755SJustin T. Gibbs
193817d24755SJustin T. Gibbscfg4istat_intr:
193917d24755SJustin T. Gibbs	call	freeze_queue;
194017d24755SJustin T. Gibbs	add	NONE, -13, SCB_CDB_LEN;
194117d24755SJustin T. Gibbs	jnc	cfg4istat_have_sense_addr;
194217d24755SJustin T. Gibbs	test	SCB_CDB_LEN, SCB_CDB_LEN_PTR jnz cfg4istat_have_sense_addr;
194317d24755SJustin T. Gibbs	/*
194417d24755SJustin T. Gibbs	 * Host sets up address/count and enables transfer.
194517d24755SJustin T. Gibbs	 */
19460c5aa4c5SScott Long	SET_SEQINTCODE(CFG4ISTAT_INTR)
194717d24755SJustin T. Gibbs	jmp	cfg4istat_setup_handler;
194817d24755SJustin T. Gibbscfg4istat_have_sense_addr:
194917d24755SJustin T. Gibbs	bmov	HADDR, SCB_SENSE_BUSADDR, 4;
195017d24755SJustin T. Gibbs	mvi	HCNT[1], (AHD_SENSE_BUFSIZE >> 8);
195117d24755SJustin T. Gibbs	mvi	SG_CACHE_PRE, LAST_SEG;
195217d24755SJustin T. Gibbs	mvi	DFCNTRL, PRELOADEN|SCSIEN|HDMAEN;
195317d24755SJustin T. Gibbscfg4istat_setup_handler:
195417d24755SJustin T. Gibbs	/*
195517d24755SJustin T. Gibbs	 * Status pkt is transferring to host.
195617d24755SJustin T. Gibbs	 * Wait in idle loop for transfer to complete.
19570794987dSJustin T. Gibbs	 * If a command completed before an attempted
19580794987dSJustin T. Gibbs	 * task management function completed, notify the host.
195917d24755SJustin T. Gibbs	 */
19600794987dSJustin T. Gibbs	test	SCB_TASK_MANAGEMENT, 0xFF jz cfg4istat_no_taskmgmt_func;
19610794987dSJustin T. Gibbs	SET_SEQINTCODE(TASKMGMT_CMD_CMPLT_OKAY)
19620794987dSJustin T. Gibbscfg4istat_no_taskmgmt_func:
196317d24755SJustin T. Gibbs	call	pkt_handle_status;
196417d24755SJustin T. Gibbs	or	SEQINTCTL, IRET ret;
196517d24755SJustin T. Gibbs
1966acae33b0SJustin T. Gibbscfg4icmd_intr:
1967acae33b0SJustin T. Gibbs	/*
1968acae33b0SJustin T. Gibbs	 * In the case of DMAing a CDB from the host, the normal
1969acae33b0SJustin T. Gibbs	 * CDB buffer is formatted with an 8 byte address followed
1970acae33b0SJustin T. Gibbs	 * by a 1 byte count.
1971acae33b0SJustin T. Gibbs	 */
1972acae33b0SJustin T. Gibbs	bmov	HADDR[0], SCB_HOST_CDB_PTR, 9;
1973acae33b0SJustin T. Gibbs	mvi	SG_CACHE_PRE, LAST_SEG;
1974acae33b0SJustin T. Gibbs	mvi	DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN);
1975acae33b0SJustin T. Gibbs	call	pkt_handle_cdb;
1976acae33b0SJustin T. Gibbs	or	SEQINTCTL, IRET ret;
1977acae33b0SJustin T. Gibbs
197817d24755SJustin T. Gibbs/*
197917d24755SJustin T. Gibbs * See if the target has gone on in this context creating an
198017d24755SJustin T. Gibbs * overrun condition.  For the write case, the hardware cannot
1981c59c8a72SJustin T. Gibbs * ack bytes until data are provided.  So, if the target begins
198217d24755SJustin T. Gibbs * another  packet without changing contexts, implying we are
198317d24755SJustin T. Gibbs * not sitting on a packet boundary, we are in an overrun
198417d24755SJustin T. Gibbs * situation.  For the read case, the hardware will continue to
198517d24755SJustin T. Gibbs * ack bytes into the FIFO, and may even ack the last overrun packet
198617d24755SJustin T. Gibbs * into the FIFO.   If the FIFO should become non-empty, we are in
198717d24755SJustin T. Gibbs * a read overrun case.
198817d24755SJustin T. Gibbs */
198917d24755SJustin T. Gibbs#define check_overrun							\
199017d24755SJustin T. Gibbs	/* Not on a packet boundary. */					\
199117d24755SJustin T. Gibbs	test 	MDFFSTAT, DLZERO jz pkt_handle_overrun;			\
199217d24755SJustin T. Gibbs	test	DFSTATUS, FIFOEMP jz pkt_handle_overrun
199317d24755SJustin T. Gibbs
199417d24755SJustin T. Gibbspkt_handle_xfer:
199517d24755SJustin T. Gibbs	test	SG_STATE, LOADING_NEEDED jz pkt_last_seg;
199617d24755SJustin T. Gibbs	call	setjmp;
199717d24755SJustin T. Gibbs	test	SEQINTSRC, SAVEPTRS jnz pkt_saveptrs;
199817d24755SJustin T. Gibbs	test	SCSIPHASE, ~DATA_PHASE_MASK jz . + 2;
199917d24755SJustin T. Gibbs	test	SCSISIGO, ATNO jnz . + 2;
200017d24755SJustin T. Gibbs	test	SSTAT2, NONPACKREQ jz pkt_service_fifo;
200117d24755SJustin T. Gibbs	/*
200217d24755SJustin T. Gibbs	 * Defer handling of this NONPACKREQ until we
200317d24755SJustin T. Gibbs	 * can be sure it pertains to this FIFO.  SAVEPTRS
200417d24755SJustin T. Gibbs	 * will not be asserted if the NONPACKREQ is for us,
2005594c945aSPedro F. Giffuni	 * so we must simulate it if shadow is valid.  If
2006594c945aSPedro F. Giffuni	 * shadow is not valid, keep running this FIFO until we
200717d24755SJustin T. Gibbs	 * have satisfied the transfer by loading segments and
2008594c945aSPedro F. Giffuni	 * waiting for either shadow valid or last_seg_done.
200917d24755SJustin T. Gibbs	 */
201017d24755SJustin T. Gibbs	test	MDFFSTAT, SHVALID jnz pkt_saveptrs;
201117d24755SJustin T. Gibbspkt_service_fifo:
201217d24755SJustin T. Gibbs	test	SG_STATE, LOADING_NEEDED jnz service_fifo;
201317d24755SJustin T. Gibbspkt_last_seg:
201417d24755SJustin T. Gibbs	call	setjmp;
201517d24755SJustin T. Gibbs	test	SEQINTSRC, SAVEPTRS jnz pkt_saveptrs;
2016d7cff4abSJustin T. Gibbs	test	SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_last_seg_done;
201717d24755SJustin T. Gibbs	test	SCSIPHASE, ~DATA_PHASE_MASK jz . + 2;
201817d24755SJustin T. Gibbs	test	SCSISIGO, ATNO jnz . + 2;
201917d24755SJustin T. Gibbs	test	SSTAT2, NONPACKREQ jz return;
2020c59c8a72SJustin T. Gibbs	test	MDFFSTAT, SHVALID jz return;
2021c59c8a72SJustin T. Gibbs	/* FALLTHROUGH */
2022c59c8a72SJustin T. Gibbs
2023c59c8a72SJustin T. Gibbs/*
2024c59c8a72SJustin T. Gibbs * Either a SAVEPTRS interrupt condition is pending for this FIFO
2025d7cff4abSJustin T. Gibbs * or we have a pending NONPACKREQ for this FIFO.  We differentiate
2026c59c8a72SJustin T. Gibbs * between the two by capturing the state of the SAVEPTRS interrupt
2027c59c8a72SJustin T. Gibbs * prior to clearing this status and executing the common code for
2028c59c8a72SJustin T. Gibbs * these two cases.
2029c59c8a72SJustin T. Gibbs */
2030c59c8a72SJustin T. Gibbspkt_saveptrs:
2031c59c8a72SJustin T. GibbsBEGIN_CRITICAL;
2032c59c8a72SJustin T. Gibbs	if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
2033c59c8a72SJustin T. Gibbs		or	DFCNTRL, FIFOFLUSH;
2034c59c8a72SJustin T. Gibbs	}
2035c59c8a72SJustin T. Gibbs	mov	REG0, SEQINTSRC;
2036c59c8a72SJustin T. Gibbs	call	calc_residual;
2037c59c8a72SJustin T. Gibbs	call	save_pointers;
2038c59c8a72SJustin T. Gibbs	mvi	CLRSEQINTSRC, CLRSAVEPTRS;
2039c59c8a72SJustin T. Gibbs	call	disable_ccsgen;
2040c59c8a72SJustin T. Gibbs	or	SEQIMODE, ENSAVEPTRS;
2041c59c8a72SJustin T. Gibbs	test	DFCNTRL, DIRECTION jnz pkt_saveptrs_check_status;
2042c59c8a72SJustin T. Gibbs	test	DFSTATUS, FIFOEMP jnz pkt_saveptrs_check_status;
2043c59c8a72SJustin T. Gibbs	/*
2044c59c8a72SJustin T. Gibbs	 * Keep a handler around for this FIFO until it drains
2045c59c8a72SJustin T. Gibbs	 * to the host to guarantee that we don't complete the
2046c59c8a72SJustin T. Gibbs	 * command to the host before the data arrives.
2047c59c8a72SJustin T. Gibbs	 */
2048c59c8a72SJustin T. Gibbspkt_saveptrs_wait_fifoemp:
2049c59c8a72SJustin T. Gibbs	call	setjmp;
2050c59c8a72SJustin T. Gibbs	test	DFSTATUS, FIFOEMP jz return;
2051c59c8a72SJustin T. Gibbspkt_saveptrs_check_status:
2052c59c8a72SJustin T. Gibbs	or	LONGJMP_ADDR[1], INVALID_ADDR;
2053c59c8a72SJustin T. Gibbs	test	REG0, SAVEPTRS jz unexpected_nonpkt_phase;
2054d7cff4abSJustin T. Gibbs	dec	SCB_FIFO_USE_COUNT;
2055d7cff4abSJustin T. Gibbs	test	SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
2056c59c8a72SJustin T. Gibbs	mvi	DFFSXFRCTL, CLRCHN ret;
2057c59c8a72SJustin T. Gibbs
205817d24755SJustin T. Gibbs/*
2059d7cff4abSJustin T. Gibbs * LAST_SEG_DONE status has been seen in the current FIFO.
2060d7cff4abSJustin T. Gibbs * This indicates that all of the allowed data for this
2061d7cff4abSJustin T. Gibbs * command has transferred across the SCSI and host buses.
2062d7cff4abSJustin T. Gibbs * Check for overrun and see if we can complete this command.
206317d24755SJustin T. Gibbs */
2064d7cff4abSJustin T. Gibbspkt_last_seg_done:
2065d7cff4abSJustin T. Gibbs	/*
2066d7cff4abSJustin T. Gibbs	 * Mark transfer as completed.
2067d7cff4abSJustin T. Gibbs	 */
2068d7cff4abSJustin T. Gibbs	or	SCB_SGPTR, SG_LIST_NULL;
206917d24755SJustin T. Gibbs
2070d7cff4abSJustin T. Gibbs	/*
2071d7cff4abSJustin T. Gibbs	 * Wait for the current context to finish to verify that
2072d7cff4abSJustin T. Gibbs	 * no overrun condition has occurred.
2073d7cff4abSJustin T. Gibbs	 */
2074d7cff4abSJustin T. Gibbs	test	SEQINTSRC, CTXTDONE jnz pkt_ctxt_done;
207517d24755SJustin T. Gibbs	call	setjmp;
2076d7cff4abSJustin T. Gibbspkt_wait_ctxt_done_loop:
2077d7cff4abSJustin T. Gibbs	test	SEQINTSRC, CTXTDONE jnz pkt_ctxt_done;
2078d7cff4abSJustin T. Gibbs	/*
2079d7cff4abSJustin T. Gibbs	 * A sufficiently large overrun or a NONPACKREQ may
2080d7cff4abSJustin T. Gibbs	 * prevent CTXTDONE from ever asserting, so we must
2081d7cff4abSJustin T. Gibbs	 * poll for these statuses too.
2082d7cff4abSJustin T. Gibbs	 */
208317d24755SJustin T. Gibbs	check_overrun;
208417d24755SJustin T. Gibbs	test	SSTAT2, NONPACKREQ jz return;
208517d24755SJustin T. Gibbs	test	SEQINTSRC, CTXTDONE jz unexpected_nonpkt_phase;
2086d7cff4abSJustin T. Gibbs	/* FALLTHROUGH */
2087d7cff4abSJustin T. Gibbs
2088d7cff4abSJustin T. Gibbspkt_ctxt_done:
208917d24755SJustin T. Gibbs	check_overrun;
209017d24755SJustin T. Gibbs	or	LONGJMP_ADDR[1], INVALID_ADDR;
2091d7cff4abSJustin T. Gibbs	/*
2092d7cff4abSJustin T. Gibbs	 * If status has been received, it is safe to skip
2093d7cff4abSJustin T. Gibbs	 * the check to see if another FIFO is active because
2094d7cff4abSJustin T. Gibbs	 * LAST_SEG_DONE has been observed.  However, we check
2095d7cff4abSJustin T. Gibbs	 * the FIFO anyway since it costs us only one extra
2096d7cff4abSJustin T. Gibbs	 * instruction to leverage common code to perform the
2097d7cff4abSJustin T. Gibbs	 * SCB completion.
2098d7cff4abSJustin T. Gibbs	 */
2099d7cff4abSJustin T. Gibbs	dec	SCB_FIFO_USE_COUNT;
2100d7cff4abSJustin T. Gibbs	test	SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
210117d24755SJustin T. Gibbs	mvi	DFFSXFRCTL, CLRCHN ret;
210217d24755SJustin T. GibbsEND_CRITICAL;
210317d24755SJustin T. Gibbs
2104acae33b0SJustin T. Gibbs/*
2105acae33b0SJustin T. Gibbs * Must wait until CDB xfer is over before issuing the
2106acae33b0SJustin T. Gibbs * clear channel.
2107acae33b0SJustin T. Gibbs */
2108acae33b0SJustin T. Gibbspkt_handle_cdb:
2109d7cff4abSJustin T. Gibbs	call	setjmp;
2110acae33b0SJustin T. Gibbs	test	SG_CACHE_SHADOW, LAST_SEG_DONE jz return;
2111acae33b0SJustin T. Gibbs	or	LONGJMP_ADDR[1], INVALID_ADDR;
2112acae33b0SJustin T. Gibbs	mvi	DFFSXFRCTL, CLRCHN ret;
2113acae33b0SJustin T. Gibbs
2114acae33b0SJustin T. Gibbs/*
2115d7cff4abSJustin T. Gibbs * Watch over the status transfer.  Our host sense buffer is
2116d7cff4abSJustin T. Gibbs * large enough to take the maximum allowed status packet.
2117d7cff4abSJustin T. Gibbs * None-the-less, we must still catch and report overruns to
2118d7cff4abSJustin T. Gibbs * the host.  Additionally, properly catch unexpected non-packet
2119d7cff4abSJustin T. Gibbs * phases that are typically caused by CRC errors in status packet
2120d7cff4abSJustin T. Gibbs * transmission.
2121d7cff4abSJustin T. Gibbs */
2122d7cff4abSJustin T. Gibbspkt_handle_status:
2123d7cff4abSJustin T. Gibbs	call	setjmp;
2124d7cff4abSJustin T. Gibbs	test	SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_status_check_overrun;
2125d7cff4abSJustin T. Gibbs	test	SEQINTSRC, CTXTDONE jz pkt_status_check_nonpackreq;
2126d7cff4abSJustin T. Gibbs	test	SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_status_check_overrun;
2127d7cff4abSJustin T. Gibbspkt_status_IU_done:
2128d7cff4abSJustin T. Gibbs	if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
2129d7cff4abSJustin T. Gibbs		or	DFCNTRL, FIFOFLUSH;
2130d7cff4abSJustin T. Gibbs	}
2131d7cff4abSJustin T. Gibbs	test	DFSTATUS, FIFOEMP jz return;
2132d7cff4abSJustin T. GibbsBEGIN_CRITICAL;
2133d7cff4abSJustin T. Gibbs	or	LONGJMP_ADDR[1], INVALID_ADDR;
2134d7cff4abSJustin T. Gibbs	mvi	SCB_SCSI_STATUS, STATUS_PKT_SENSE;
2135d7cff4abSJustin T. Gibbs	or	SCB_CONTROL, STATUS_RCVD;
2136d7cff4abSJustin T. Gibbs	jmp	pkt_complete_scb_if_fifos_idle;
2137d7cff4abSJustin T. GibbsEND_CRITICAL;
2138d7cff4abSJustin T. Gibbspkt_status_check_overrun:
2139d7cff4abSJustin T. Gibbs	/*
2140e1854a84SCeri Davies	 * Status PKT overruns are unceremoniously recovered with a
2141d7cff4abSJustin T. Gibbs	 * bus reset.  If we've overrun, let the host know so that
2142d7cff4abSJustin T. Gibbs	 * recovery can be performed.
2143d7cff4abSJustin T. Gibbs	 *
2144d7cff4abSJustin T. Gibbs	 * LAST_SEG_DONE has been observed.  If either CTXTDONE or
2145d7cff4abSJustin T. Gibbs	 * a NONPACKREQ phase change have occurred and the FIFO is
2146d7cff4abSJustin T. Gibbs	 * empty, there is no overrun.
2147d7cff4abSJustin T. Gibbs	 */
2148d7cff4abSJustin T. Gibbs	test	DFSTATUS, FIFOEMP jz pkt_status_report_overrun;
2149d7cff4abSJustin T. Gibbs	test	SEQINTSRC, CTXTDONE jz . + 2;
2150d7cff4abSJustin T. Gibbs	test	DFSTATUS, FIFOEMP jnz pkt_status_IU_done;
2151d7cff4abSJustin T. Gibbs	test	SCSIPHASE, ~DATA_PHASE_MASK jz return;
2152d7cff4abSJustin T. Gibbs	test	DFSTATUS, FIFOEMP jnz pkt_status_check_nonpackreq;
2153d7cff4abSJustin T. Gibbspkt_status_report_overrun:
2154d7cff4abSJustin T. Gibbs	SET_SEQINTCODE(STATUS_OVERRUN)
2155d7cff4abSJustin T. Gibbs	/* SEQUENCER RESTARTED */
2156d7cff4abSJustin T. Gibbspkt_status_check_nonpackreq:
2157d7cff4abSJustin T. Gibbs	/*
2158d7cff4abSJustin T. Gibbs	 * CTXTDONE may be held off if a NONPACKREQ is associated with
2159d7cff4abSJustin T. Gibbs	 * the current context.  If a NONPACKREQ is observed, decide
2160d7cff4abSJustin T. Gibbs	 * if it is for the current context.  If it is for the current
2161d7cff4abSJustin T. Gibbs	 * context, we must defer NONPACKREQ processing until all data
2162d7cff4abSJustin T. Gibbs	 * has transferred to the host.
2163d7cff4abSJustin T. Gibbs	 */
2164d7cff4abSJustin T. Gibbs	test	SCSIPHASE, ~DATA_PHASE_MASK jz return;
2165d7cff4abSJustin T. Gibbs	test	SCSISIGO, ATNO jnz . + 2;
2166d7cff4abSJustin T. Gibbs	test	SSTAT2, NONPACKREQ jz return;
2167d7cff4abSJustin T. Gibbs	test	SEQINTSRC, CTXTDONE jnz pkt_status_IU_done;
2168d7cff4abSJustin T. Gibbs	test	DFSTATUS, FIFOEMP jz return;
2169d7cff4abSJustin T. Gibbs	/*
2170d7cff4abSJustin T. Gibbs	 * The unexpected nonpkt phase handler assumes that any
2171d7cff4abSJustin T. Gibbs	 * data channel use will have a FIFO reference count.  It
2172594c945aSPedro F. Giffuni	 * turns out that the status handler doesn't need a references
2173d7cff4abSJustin T. Gibbs	 * count since the status received flag, and thus completion
2174d7cff4abSJustin T. Gibbs	 * processing, cannot be set until the handler is finished.
2175d7cff4abSJustin T. Gibbs	 * We increment the count here to make the nonpkt handler
2176d7cff4abSJustin T. Gibbs	 * happy.
2177d7cff4abSJustin T. Gibbs	 */
2178d7cff4abSJustin T. Gibbs	inc	SCB_FIFO_USE_COUNT;
2179d7cff4abSJustin T. Gibbs	/* FALLTHROUGH */
2180d7cff4abSJustin T. Gibbs
2181d7cff4abSJustin T. Gibbs/*
218217d24755SJustin T. Gibbs * Nonpackreq is a polled status.  It can come true in three situations:
218317d24755SJustin T. Gibbs * we have received an L_Q, we have sent one or more L_Qs, or there is no
218417d24755SJustin T. Gibbs * L_Q context associated with this REQ (REQ occurs immediately after a
218517d24755SJustin T. Gibbs * (re)selection).  Routines that know that the context responsible for this
218617d24755SJustin T. Gibbs * nonpackreq call directly into unexpected_nonpkt_phase.  In the case of the
218717d24755SJustin T. Gibbs * top level idle loop, we exhaust all active contexts prior to determining that
218817d24755SJustin T. Gibbs * we simply do not have the full I_T_L_Q for this phase.
218917d24755SJustin T. Gibbs */
219017d24755SJustin T. Gibbsunexpected_nonpkt_phase_find_ctxt:
219117d24755SJustin T. Gibbs	/*
219217d24755SJustin T. Gibbs	 * This nonpackreq is most likely associated with one of the tags
219317d24755SJustin T. Gibbs	 * in a FIFO or an outgoing LQ.  Only treat it as an I_T only
219417d24755SJustin T. Gibbs	 * nonpackreq if we've cleared out the FIFOs and handled any
219517d24755SJustin T. Gibbs	 * pending SELDO.
219617d24755SJustin T. Gibbs	 */
219717d24755SJustin T. GibbsSET_SRC_MODE	M_SCSI;
219817d24755SJustin T. GibbsSET_DST_MODE	M_SCSI;
219917d24755SJustin T. Gibbs	and	A, FIFO1FREE|FIFO0FREE, DFFSTAT;
220017d24755SJustin T. Gibbs	cmp	A, FIFO1FREE|FIFO0FREE jne return;
220117d24755SJustin T. Gibbs	test	SSTAT0, SELDO jnz return;
220217d24755SJustin T. Gibbs	mvi	SCBPTR[1], SCB_LIST_NULL;
220317d24755SJustin T. Gibbsunexpected_nonpkt_phase:
2204e46bab9aSJustin T. Gibbs	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
2205e46bab9aSJustin T. Gibbs		jnz unexpected_nonpkt_mode_cleared;
220617d24755SJustin T. GibbsSET_SRC_MODE	M_DFF0;
220717d24755SJustin T. GibbsSET_DST_MODE	M_DFF0;
220817d24755SJustin T. Gibbs	or	LONGJMP_ADDR[1], INVALID_ADDR;
2209d7cff4abSJustin T. Gibbs	dec	SCB_FIFO_USE_COUNT;
221017d24755SJustin T. Gibbs	mvi	DFFSXFRCTL, CLRCHN;
2211e46bab9aSJustin T. Gibbsunexpected_nonpkt_mode_cleared:
221217d24755SJustin T. Gibbs	mvi	CLRSINT2, CLRNONPACKREQ;
2213789902c3SJustin T. Gibbs	if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
2214789902c3SJustin T. Gibbs		/*
2215789902c3SJustin T. Gibbs		 * Test to ensure that the bus has not
2216789902c3SJustin T. Gibbs		 * already gone free prior to clearing
2217789902c3SJustin T. Gibbs		 * any stale busfree status.  This avoids
2218789902c3SJustin T. Gibbs		 * a window whereby a busfree just after
2219789902c3SJustin T. Gibbs		 * a selection could be missed.
2220789902c3SJustin T. Gibbs		 */
2221789902c3SJustin T. Gibbs		test	SCSISIGI, BSYI jz . + 2;
2222789902c3SJustin T. Gibbs		mvi	CLRSINT1,CLRBUSFREE;
2223789902c3SJustin T. Gibbs		or	SIMODE1, ENBUSFREE;
2224789902c3SJustin T. Gibbs	}
222517d24755SJustin T. Gibbs	test	SCSIPHASE, ~(MSG_IN_PHASE|MSG_OUT_PHASE) jnz illegal_phase;
22260c5aa4c5SScott Long	SET_SEQINTCODE(ENTERING_NONPACK)
222717d24755SJustin T. Gibbs	jmp	ITloop;
222817d24755SJustin T. Gibbs
222917d24755SJustin T. Gibbsillegal_phase:
22300c5aa4c5SScott Long	SET_SEQINTCODE(ILLEGAL_PHASE)
223117d24755SJustin T. Gibbs	jmp	ITloop;
223217d24755SJustin T. Gibbs
223317d24755SJustin T. Gibbs/*
223417d24755SJustin T. Gibbs * We have entered an overrun situation.  If we have working
223517d24755SJustin T. Gibbs * BITBUCKET, flip that on and let the hardware eat any overrun
223617d24755SJustin T. Gibbs * data.  Otherwise use an overrun buffer in the host to simulate
223717d24755SJustin T. Gibbs * BITBUCKET.
223817d24755SJustin T. Gibbs */
2239d7cff4abSJustin T. Gibbspkt_handle_overrun_inc_use_count:
2240d7cff4abSJustin T. Gibbs	inc	SCB_FIFO_USE_COUNT;
224117d24755SJustin T. Gibbspkt_handle_overrun:
22420c5aa4c5SScott Long	SET_SEQINTCODE(CFG4OVERRUN)
224317d24755SJustin T. Gibbs	call	freeze_queue;
224417d24755SJustin T. Gibbs	if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) == 0) {
22450c5aa4c5SScott Long		or	DFFSXFRCTL, DFFBITBUCKET;
224617d24755SJustin T. GibbsSET_SRC_MODE	M_DFF1;
224717d24755SJustin T. GibbsSET_DST_MODE	M_DFF1;
224817d24755SJustin T. Gibbs	} else {
224917d24755SJustin T. Gibbs		call	load_overrun_buf;
225017d24755SJustin T. Gibbs		mvi	DFCNTRL, (HDMAEN|SCSIEN|PRELOADEN);
225117d24755SJustin T. Gibbs	}
225217d24755SJustin T. Gibbs	call	setjmp;
225317d24755SJustin T. Gibbs	if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
22540c5aa4c5SScott Long		test	DFSTATUS, PRELOAD_AVAIL jz overrun_load_done;
225517d24755SJustin T. Gibbs		call	load_overrun_buf;
225617d24755SJustin T. Gibbs		or	DFCNTRL, PRELOADEN;
225717d24755SJustin T. Gibbsoverrun_load_done:
225817d24755SJustin T. Gibbs		test	SEQINTSRC, CTXTDONE jnz pkt_overrun_end;
22590c5aa4c5SScott Long	} else {
22600c5aa4c5SScott Long		test	DFFSXFRCTL, DFFBITBUCKET jz pkt_overrun_end;
22610c5aa4c5SScott Long	}
226217d24755SJustin T. Gibbs	test	SSTAT2, NONPACKREQ jz return;
226317d24755SJustin T. Gibbspkt_overrun_end:
226417d24755SJustin T. Gibbs	or	SCB_RESIDUAL_SGPTR, SG_OVERRUN_RESID;
226517d24755SJustin T. Gibbs	test	SEQINTSRC, CTXTDONE jz unexpected_nonpkt_phase;
2266d7cff4abSJustin T. Gibbs	dec	SCB_FIFO_USE_COUNT;
2267acae33b0SJustin T. Gibbs	or	LONGJMP_ADDR[1], INVALID_ADDR;
2268d7cff4abSJustin T. Gibbs	test	SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
226917d24755SJustin T. Gibbs	mvi	DFFSXFRCTL, CLRCHN ret;
227017d24755SJustin T. Gibbs
227117d24755SJustin T. Gibbsif ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
227217d24755SJustin T. Gibbsload_overrun_buf:
227317d24755SJustin T. Gibbs	/*
227417d24755SJustin T. Gibbs	 * Load a dummy segment if preload space is available.
227517d24755SJustin T. Gibbs	 */
227617d24755SJustin T. Gibbs	mov 	HADDR[0], SHARED_DATA_ADDR;
227717d24755SJustin T. Gibbs	add	HADDR[1], PKT_OVERRUN_BUFOFFSET, SHARED_DATA_ADDR[1];
227817d24755SJustin T. Gibbs	mov	ACCUM_SAVE, A;
227917d24755SJustin T. Gibbs	clr	A;
228017d24755SJustin T. Gibbs	adc	HADDR[2], A, SHARED_DATA_ADDR[2];
228117d24755SJustin T. Gibbs	adc	HADDR[3], A, SHARED_DATA_ADDR[3];
228217d24755SJustin T. Gibbs	mov	A, ACCUM_SAVE;
228317d24755SJustin T. Gibbs	bmov	HADDR[4], ALLZEROS, 4;
228417d24755SJustin T. Gibbs	/* PKT_OVERRUN_BUFSIZE is a multiple of 256 */
228517d24755SJustin T. Gibbs	clr	HCNT[0];
228617d24755SJustin T. Gibbs	mvi	HCNT[1], ((PKT_OVERRUN_BUFSIZE >> 8) & 0xFF);
22870794987dSJustin T. Gibbs	clr	HCNT[2] ret;
228817d24755SJustin T. Gibbs}
2289