18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Adaptec U320 device driver firmware for Linux and FreeBSD.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (c) 1994-2001, 2004 Justin T. Gibbs.
58c2ecf20Sopenharmony_ci * Copyright (c) 2000-2002 Adaptec Inc.
68c2ecf20Sopenharmony_ci * All rights reserved.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
98c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
108c2ecf20Sopenharmony_ci * are met:
118c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
128c2ecf20Sopenharmony_ci *    notice, this list of conditions, and the following disclaimer,
138c2ecf20Sopenharmony_ci *    without modification.
148c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce at minimum a disclaimer
158c2ecf20Sopenharmony_ci *    substantially similar to the "NO WARRANTY" disclaimer below
168c2ecf20Sopenharmony_ci *    ("Disclaimer") and any redistribution must be conditioned upon
178c2ecf20Sopenharmony_ci *    including a substantially similar Disclaimer requirement for further
188c2ecf20Sopenharmony_ci *    binary redistribution.
198c2ecf20Sopenharmony_ci * 3. Neither the names of the above-listed copyright holders nor the names
208c2ecf20Sopenharmony_ci *    of any contributors may be used to endorse or promote products derived
218c2ecf20Sopenharmony_ci *    from this software without specific prior written permission.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the
248c2ecf20Sopenharmony_ci * GNU General Public License ("GPL") version 2 as published by the Free
258c2ecf20Sopenharmony_ci * Software Foundation.
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * NO WARRANTY
288c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
298c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
308c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
318c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
328c2ecf20Sopenharmony_ci * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
338c2ecf20Sopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
348c2ecf20Sopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
358c2ecf20Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
368c2ecf20Sopenharmony_ci * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
378c2ecf20Sopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
388c2ecf20Sopenharmony_ci * POSSIBILITY OF SUCH DAMAGES.
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * $FreeBSD$
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ciVERSION = "$Id: //depot/aic7xxx/aic7xxx/aic79xx.seq#120 $"
448c2ecf20Sopenharmony_ciPATCH_ARG_LIST = "struct ahd_softc *ahd"
458c2ecf20Sopenharmony_ciPREFIX = "ahd_"
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#include "aic79xx.reg"
488c2ecf20Sopenharmony_ci#include "scsi_message.h"
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cirestart:
518c2ecf20Sopenharmony_ciif ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
528c2ecf20Sopenharmony_ci	test	SEQINTCODE, 0xFF jz idle_loop;
538c2ecf20Sopenharmony_ci	SET_SEQINTCODE(NO_SEQINT)
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ciidle_loop:
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
598c2ecf20Sopenharmony_ci		/*
608c2ecf20Sopenharmony_ci		 * Convert ERROR status into a sequencer
618c2ecf20Sopenharmony_ci		 * interrupt to handle the case of an
628c2ecf20Sopenharmony_ci		 * interrupt collision on the hardware
638c2ecf20Sopenharmony_ci		 * setting of HWERR.
648c2ecf20Sopenharmony_ci		 */
658c2ecf20Sopenharmony_ci		test	ERROR, 0xFF jz no_error_set;
668c2ecf20Sopenharmony_ci		SET_SEQINTCODE(SAW_HWERR)
678c2ecf20Sopenharmony_cino_error_set:
688c2ecf20Sopenharmony_ci	}
698c2ecf20Sopenharmony_ci	SET_MODE(M_SCSI, M_SCSI)
708c2ecf20Sopenharmony_ci	test	SCSISEQ0, ENSELO|ENARBO jnz idle_loop_checkbus;
718c2ecf20Sopenharmony_ci	test	SEQ_FLAGS2, SELECTOUT_QFROZEN jz check_waiting_list;
728c2ecf20Sopenharmony_ci	/*
738c2ecf20Sopenharmony_ci	 * If the kernel has caught up with us, thaw the queue.
748c2ecf20Sopenharmony_ci	 */
758c2ecf20Sopenharmony_ci	mov	A, KERNEL_QFREEZE_COUNT;
768c2ecf20Sopenharmony_ci	cmp	QFREEZE_COUNT, A jne check_frozen_completions;
778c2ecf20Sopenharmony_ci	mov	A, KERNEL_QFREEZE_COUNT[1];
788c2ecf20Sopenharmony_ci	cmp	QFREEZE_COUNT[1], A jne check_frozen_completions;
798c2ecf20Sopenharmony_ci	and	SEQ_FLAGS2, ~SELECTOUT_QFROZEN;
808c2ecf20Sopenharmony_ci	jmp	check_waiting_list;
818c2ecf20Sopenharmony_cicheck_frozen_completions:
828c2ecf20Sopenharmony_ci	test	SSTAT0, SELDO|SELINGO jnz idle_loop_checkbus;
838c2ecf20Sopenharmony_ciBEGIN_CRITICAL;
848c2ecf20Sopenharmony_ci	/*
858c2ecf20Sopenharmony_ci	 * If we have completions stalled waiting for the qfreeze
868c2ecf20Sopenharmony_ci	 * to take effect, move them over to the complete_scb list
878c2ecf20Sopenharmony_ci	 * now that no selections are pending.
888c2ecf20Sopenharmony_ci	 */
898c2ecf20Sopenharmony_ci	cmp	COMPLETE_ON_QFREEZE_HEAD[1],SCB_LIST_NULL je idle_loop_checkbus;
908c2ecf20Sopenharmony_ci	/*
918c2ecf20Sopenharmony_ci	 * Find the end of the qfreeze list.  The first element has
928c2ecf20Sopenharmony_ci	 * to be treated specially.
938c2ecf20Sopenharmony_ci	 */
948c2ecf20Sopenharmony_ci	bmov	SCBPTR, COMPLETE_ON_QFREEZE_HEAD, 2;
958c2ecf20Sopenharmony_ci	cmp 	SCB_NEXT_COMPLETE[1], SCB_LIST_NULL je join_lists;
968c2ecf20Sopenharmony_ci	/*
978c2ecf20Sopenharmony_ci	 * Now the normal loop.
988c2ecf20Sopenharmony_ci	 */
998c2ecf20Sopenharmony_ci	bmov	SCBPTR, SCB_NEXT_COMPLETE, 2;
1008c2ecf20Sopenharmony_ci	cmp 	SCB_NEXT_COMPLETE[1], SCB_LIST_NULL jne . - 1;
1018c2ecf20Sopenharmony_cijoin_lists:
1028c2ecf20Sopenharmony_ci	bmov	SCB_NEXT_COMPLETE, COMPLETE_SCB_HEAD, 2;
1038c2ecf20Sopenharmony_ci	bmov	COMPLETE_SCB_HEAD, COMPLETE_ON_QFREEZE_HEAD, 2;
1048c2ecf20Sopenharmony_ci	mvi	COMPLETE_ON_QFREEZE_HEAD[1], SCB_LIST_NULL;
1058c2ecf20Sopenharmony_ci	jmp	idle_loop_checkbus;
1068c2ecf20Sopenharmony_cicheck_waiting_list:
1078c2ecf20Sopenharmony_ci	cmp	WAITING_TID_HEAD[1], SCB_LIST_NULL je idle_loop_checkbus;
1088c2ecf20Sopenharmony_ci	/*
1098c2ecf20Sopenharmony_ci	 * ENSELO is cleared by a SELDO, so we must test for SELDO
1108c2ecf20Sopenharmony_ci	 * one last time.
1118c2ecf20Sopenharmony_ci	 */
1128c2ecf20Sopenharmony_ci	test	SSTAT0, SELDO jnz select_out;
1138c2ecf20Sopenharmony_ci	call	start_selection;
1148c2ecf20Sopenharmony_ciidle_loop_checkbus:
1158c2ecf20Sopenharmony_ci	test	SSTAT0, SELDO jnz select_out;
1168c2ecf20Sopenharmony_ciEND_CRITICAL;
1178c2ecf20Sopenharmony_ci	test	SSTAT0, SELDI jnz select_in;
1188c2ecf20Sopenharmony_ci	test	SCSIPHASE, ~DATA_PHASE_MASK jz idle_loop_check_nonpackreq;
1198c2ecf20Sopenharmony_ci	test	SCSISIGO, ATNO jz idle_loop_check_nonpackreq;
1208c2ecf20Sopenharmony_ci	call	unexpected_nonpkt_phase_find_ctxt;
1218c2ecf20Sopenharmony_ciidle_loop_check_nonpackreq:
1228c2ecf20Sopenharmony_ci	test	SSTAT2, NONPACKREQ jz . + 2;
1238c2ecf20Sopenharmony_ci	call	unexpected_nonpkt_phase_find_ctxt;
1248c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_FAINT_LED_BUG) != 0) {
1258c2ecf20Sopenharmony_ci		/*
1268c2ecf20Sopenharmony_ci		 * On Rev A. hardware, the busy LED is only
1278c2ecf20Sopenharmony_ci		 * turned on automaically during selections
1288c2ecf20Sopenharmony_ci		 * and re-selections.  Make the LED status
1298c2ecf20Sopenharmony_ci		 * more useful by forcing it to be on so
1308c2ecf20Sopenharmony_ci		 * long as one of our data FIFOs is active.
1318c2ecf20Sopenharmony_ci		 */
1328c2ecf20Sopenharmony_ci		and	A, FIFO0FREE|FIFO1FREE, DFFSTAT;
1338c2ecf20Sopenharmony_ci		cmp	A, FIFO0FREE|FIFO1FREE jne . + 3;
1348c2ecf20Sopenharmony_ci		and	SBLKCTL, ~DIAGLEDEN|DIAGLEDON;
1358c2ecf20Sopenharmony_ci		jmp	. + 2;
1368c2ecf20Sopenharmony_ci		or	SBLKCTL, DIAGLEDEN|DIAGLEDON;
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci	call	idle_loop_gsfifo_in_scsi_mode;
1398c2ecf20Sopenharmony_ci	call	idle_loop_service_fifos;
1408c2ecf20Sopenharmony_ci	call	idle_loop_cchan;
1418c2ecf20Sopenharmony_ci	jmp	idle_loop;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ciidle_loop_gsfifo:
1448c2ecf20Sopenharmony_ci	SET_MODE(M_SCSI, M_SCSI)
1458c2ecf20Sopenharmony_ciBEGIN_CRITICAL;
1468c2ecf20Sopenharmony_ciidle_loop_gsfifo_in_scsi_mode:
1478c2ecf20Sopenharmony_ci	test	LQISTAT2, LQIGSAVAIL jz return;
1488c2ecf20Sopenharmony_ci	/*
1498c2ecf20Sopenharmony_ci	 * We have received good status for this transaction.  There may
1508c2ecf20Sopenharmony_ci	 * still be data in our FIFOs draining to the host.  Complete
1518c2ecf20Sopenharmony_ci	 * the SCB only if all data has transferred to the host.
1528c2ecf20Sopenharmony_ci	 */
1538c2ecf20Sopenharmony_cigood_status_IU_done:
1548c2ecf20Sopenharmony_ci	bmov	SCBPTR, GSFIFO, 2;
1558c2ecf20Sopenharmony_ci	clr	SCB_SCSI_STATUS;
1568c2ecf20Sopenharmony_ci	/*
1578c2ecf20Sopenharmony_ci	 * If a command completed before an attempted task management
1588c2ecf20Sopenharmony_ci	 * function completed, notify the host after disabling any
1598c2ecf20Sopenharmony_ci	 * pending select-outs.
1608c2ecf20Sopenharmony_ci	 */
1618c2ecf20Sopenharmony_ci	test	SCB_TASK_MANAGEMENT, 0xFF jz gsfifo_complete_normally;
1628c2ecf20Sopenharmony_ci	test	SSTAT0, SELDO|SELINGO jnz . + 2;
1638c2ecf20Sopenharmony_ci	and	SCSISEQ0, ~ENSELO;
1648c2ecf20Sopenharmony_ci	SET_SEQINTCODE(TASKMGMT_CMD_CMPLT_OKAY)
1658c2ecf20Sopenharmony_cigsfifo_complete_normally:
1668c2ecf20Sopenharmony_ci	or	SCB_CONTROL, STATUS_RCVD;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	/*
1698c2ecf20Sopenharmony_ci	 * Since this status did not consume a FIFO, we have to
1708c2ecf20Sopenharmony_ci	 * be a bit more dilligent in how we check for FIFOs pertaining
1718c2ecf20Sopenharmony_ci	 * to this transaction.  There are two states that a FIFO still
1728c2ecf20Sopenharmony_ci	 * transferring data may be in.
1738c2ecf20Sopenharmony_ci	 *
1748c2ecf20Sopenharmony_ci	 * 1) Configured and draining to the host, with a FIFO handler.
1758c2ecf20Sopenharmony_ci	 * 2) Pending cfg4data, fifo not empty.
1768c2ecf20Sopenharmony_ci	 *
1778c2ecf20Sopenharmony_ci	 * Case 1 can be detected by noticing a non-zero FIFO active
1788c2ecf20Sopenharmony_ci	 * count in the SCB.  In this case, we allow the routine servicing
1798c2ecf20Sopenharmony_ci	 * the FIFO to complete the SCB.
1808c2ecf20Sopenharmony_ci	 * 
1818c2ecf20Sopenharmony_ci	 * Case 2 implies either a pending or yet to occur save data
1828c2ecf20Sopenharmony_ci	 * pointers for this same context in the other FIFO.  So, if
1838c2ecf20Sopenharmony_ci	 * we detect case 1, we will properly defer the post of the SCB
1848c2ecf20Sopenharmony_ci	 * and achieve the desired result.  The pending cfg4data will
1858c2ecf20Sopenharmony_ci	 * notice that status has been received and complete the SCB.
1868c2ecf20Sopenharmony_ci	 */
1878c2ecf20Sopenharmony_ci	test	SCB_FIFO_USE_COUNT, 0xFF jnz idle_loop_gsfifo_in_scsi_mode;
1888c2ecf20Sopenharmony_ci	call	complete;
1898c2ecf20Sopenharmony_ciEND_CRITICAL;
1908c2ecf20Sopenharmony_ci	jmp	idle_loop_gsfifo_in_scsi_mode;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ciidle_loop_service_fifos:
1938c2ecf20Sopenharmony_ci	SET_MODE(M_DFF0, M_DFF0)
1948c2ecf20Sopenharmony_ciBEGIN_CRITICAL;
1958c2ecf20Sopenharmony_ci	test	LONGJMP_ADDR[1], INVALID_ADDR jnz idle_loop_next_fifo;
1968c2ecf20Sopenharmony_ci	call	longjmp;
1978c2ecf20Sopenharmony_ciEND_CRITICAL;
1988c2ecf20Sopenharmony_ciidle_loop_next_fifo:
1998c2ecf20Sopenharmony_ci	SET_MODE(M_DFF1, M_DFF1)
2008c2ecf20Sopenharmony_ciBEGIN_CRITICAL;
2018c2ecf20Sopenharmony_ci	test	LONGJMP_ADDR[1], INVALID_ADDR jz longjmp;
2028c2ecf20Sopenharmony_ciEND_CRITICAL;
2038c2ecf20Sopenharmony_cireturn:
2048c2ecf20Sopenharmony_ci	ret;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ciidle_loop_cchan:
2078c2ecf20Sopenharmony_ci	SET_MODE(M_CCHAN, M_CCHAN)
2088c2ecf20Sopenharmony_ci	test	QOFF_CTLSTA, HS_MAILBOX_ACT jz	hs_mailbox_empty;
2098c2ecf20Sopenharmony_ci	or	QOFF_CTLSTA, HS_MAILBOX_ACT;
2108c2ecf20Sopenharmony_ci	mov	LOCAL_HS_MAILBOX, HS_MAILBOX;
2118c2ecf20Sopenharmony_cihs_mailbox_empty:
2128c2ecf20Sopenharmony_ciBEGIN_CRITICAL;
2138c2ecf20Sopenharmony_ci	test	CCSCBCTL, CCARREN|CCSCBEN jz scbdma_idle;
2148c2ecf20Sopenharmony_ci	test	CCSCBCTL, CCSCBDIR jnz fetch_new_scb_inprog;
2158c2ecf20Sopenharmony_ci	test	CCSCBCTL, CCSCBDONE jz return;
2168c2ecf20Sopenharmony_ci	/* FALLTHROUGH */
2178c2ecf20Sopenharmony_ciscbdma_tohost_done:
2188c2ecf20Sopenharmony_ci	test	CCSCBCTL, CCARREN jz fill_qoutfifo_dmadone;
2198c2ecf20Sopenharmony_ci	/*
2208c2ecf20Sopenharmony_ci	 * An SCB has been successfully uploaded to the host.
2218c2ecf20Sopenharmony_ci	 * If the SCB was uploaded for some reason other than
2228c2ecf20Sopenharmony_ci	 * bad SCSI status (currently only for underruns), we
2238c2ecf20Sopenharmony_ci	 * queue the SCB for normal completion.  Otherwise, we
2248c2ecf20Sopenharmony_ci	 * wait until any select-out activity has halted, and
2258c2ecf20Sopenharmony_ci	 * then queue the completion.
2268c2ecf20Sopenharmony_ci	 */
2278c2ecf20Sopenharmony_ci	and	CCSCBCTL, ~(CCARREN|CCSCBEN);
2288c2ecf20Sopenharmony_ci	bmov	COMPLETE_DMA_SCB_HEAD, SCB_NEXT_COMPLETE, 2;
2298c2ecf20Sopenharmony_ci	cmp	SCB_NEXT_COMPLETE[1], SCB_LIST_NULL jne . + 2;
2308c2ecf20Sopenharmony_ci	mvi	COMPLETE_DMA_SCB_TAIL[1], SCB_LIST_NULL;
2318c2ecf20Sopenharmony_ci	test	SCB_SCSI_STATUS, 0xff jz scbdma_queue_completion;
2328c2ecf20Sopenharmony_ci	bmov	SCB_NEXT_COMPLETE, COMPLETE_ON_QFREEZE_HEAD, 2;
2338c2ecf20Sopenharmony_ci	bmov	COMPLETE_ON_QFREEZE_HEAD, SCBPTR, 2 ret;
2348c2ecf20Sopenharmony_ciscbdma_queue_completion:
2358c2ecf20Sopenharmony_ci	bmov	SCB_NEXT_COMPLETE, COMPLETE_SCB_HEAD, 2;
2368c2ecf20Sopenharmony_ci	bmov	COMPLETE_SCB_HEAD, SCBPTR, 2 ret;
2378c2ecf20Sopenharmony_cifill_qoutfifo_dmadone:
2388c2ecf20Sopenharmony_ci	and	CCSCBCTL, ~(CCARREN|CCSCBEN);
2398c2ecf20Sopenharmony_ci	call	qoutfifo_updated;
2408c2ecf20Sopenharmony_ci	mvi	COMPLETE_SCB_DMAINPROG_HEAD[1], SCB_LIST_NULL;
2418c2ecf20Sopenharmony_ci	bmov	QOUTFIFO_NEXT_ADDR, SCBHADDR, 4;
2428c2ecf20Sopenharmony_ci	test	QOFF_CTLSTA, SDSCB_ROLLOVR jz return;
2438c2ecf20Sopenharmony_ci	bmov	QOUTFIFO_NEXT_ADDR, SHARED_DATA_ADDR, 4;
2448c2ecf20Sopenharmony_ci	xor	QOUTFIFO_ENTRY_VALID_TAG, QOUTFIFO_ENTRY_VALID_TOGGLE ret;
2458c2ecf20Sopenharmony_ciEND_CRITICAL;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ciqoutfifo_updated:
2488c2ecf20Sopenharmony_ci	/*
2498c2ecf20Sopenharmony_ci	 * If there are more commands waiting to be dma'ed
2508c2ecf20Sopenharmony_ci	 * to the host, always coalesce.  Otherwise honor the
2518c2ecf20Sopenharmony_ci	 * host's wishes.
2528c2ecf20Sopenharmony_ci	 */
2538c2ecf20Sopenharmony_ci	cmp	COMPLETE_DMA_SCB_HEAD[1], SCB_LIST_NULL jne coalesce_by_count;
2548c2ecf20Sopenharmony_ci	cmp	COMPLETE_SCB_HEAD[1], SCB_LIST_NULL jne coalesce_by_count;
2558c2ecf20Sopenharmony_ci	test	LOCAL_HS_MAILBOX, ENINT_COALESCE jz issue_cmdcmplt;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	/*
2588c2ecf20Sopenharmony_ci	 * If we have relatively few commands outstanding, don't
2598c2ecf20Sopenharmony_ci	 * bother waiting for another command to complete.
2608c2ecf20Sopenharmony_ci	 */
2618c2ecf20Sopenharmony_ci	test	CMDS_PENDING[1], 0xFF jnz coalesce_by_count;
2628c2ecf20Sopenharmony_ci	/* Add -1 so that jnc means <= not just < */
2638c2ecf20Sopenharmony_ci	add	A, -1, INT_COALESCING_MINCMDS;
2648c2ecf20Sopenharmony_ci	add	NONE, A, CMDS_PENDING;
2658c2ecf20Sopenharmony_ci	jnc	issue_cmdcmplt;
2668c2ecf20Sopenharmony_ci	
2678c2ecf20Sopenharmony_ci	/*
2688c2ecf20Sopenharmony_ci	 * If coalescing, only coalesce up to the limit
2698c2ecf20Sopenharmony_ci	 * provided by the host driver.
2708c2ecf20Sopenharmony_ci	 */
2718c2ecf20Sopenharmony_cicoalesce_by_count:
2728c2ecf20Sopenharmony_ci	mov	A, INT_COALESCING_MAXCMDS;
2738c2ecf20Sopenharmony_ci	add	NONE, A, INT_COALESCING_CMDCOUNT;
2748c2ecf20Sopenharmony_ci	jc	issue_cmdcmplt;
2758c2ecf20Sopenharmony_ci	/*
2768c2ecf20Sopenharmony_ci	 * If the timer is not currently active,
2778c2ecf20Sopenharmony_ci	 * fire it up.
2788c2ecf20Sopenharmony_ci	 */
2798c2ecf20Sopenharmony_ci	test	INTCTL, SWTMINTMASK jz return;
2808c2ecf20Sopenharmony_ci	bmov	SWTIMER, INT_COALESCING_TIMER, 2;
2818c2ecf20Sopenharmony_ci	mvi	CLRSEQINTSTAT, CLRSEQ_SWTMRTO;
2828c2ecf20Sopenharmony_ci	or	INTCTL, SWTMINTEN|SWTIMER_START;
2838c2ecf20Sopenharmony_ci	and	INTCTL, ~SWTMINTMASK ret;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ciissue_cmdcmplt:
2868c2ecf20Sopenharmony_ci	mvi	INTSTAT, CMDCMPLT;
2878c2ecf20Sopenharmony_ci	clr	INT_COALESCING_CMDCOUNT;
2888c2ecf20Sopenharmony_ci	or	INTCTL, SWTMINTMASK ret;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ciBEGIN_CRITICAL;
2918c2ecf20Sopenharmony_cifetch_new_scb_inprog:
2928c2ecf20Sopenharmony_ci	test	CCSCBCTL, ARRDONE jz return;
2938c2ecf20Sopenharmony_cifetch_new_scb_done:
2948c2ecf20Sopenharmony_ci	and	CCSCBCTL, ~(CCARREN|CCSCBEN);
2958c2ecf20Sopenharmony_ci	clr	A;
2968c2ecf20Sopenharmony_ci	add	CMDS_PENDING, 1;
2978c2ecf20Sopenharmony_ci	adc	CMDS_PENDING[1], A;
2988c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
2998c2ecf20Sopenharmony_ci		/*
3008c2ecf20Sopenharmony_ci		 * "Short Luns" are not placed into outgoing LQ
3018c2ecf20Sopenharmony_ci		 * packets in the correct byte order.  Use a full
3028c2ecf20Sopenharmony_ci		 * sized lun field instead and fill it with the
3038c2ecf20Sopenharmony_ci		 * one byte of lun information we support.
3048c2ecf20Sopenharmony_ci		 */
3058c2ecf20Sopenharmony_ci		mov	SCB_PKT_LUN[6], SCB_LUN;
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci	/*
3088c2ecf20Sopenharmony_ci	 * The FIFO use count field is shared with the
3098c2ecf20Sopenharmony_ci	 * tag set by the host so that our SCB dma engine
3108c2ecf20Sopenharmony_ci	 * knows the correct location to store the SCB.
3118c2ecf20Sopenharmony_ci	 * Set it to zero before processing the SCB.
3128c2ecf20Sopenharmony_ci	 */
3138c2ecf20Sopenharmony_ci	clr	SCB_FIFO_USE_COUNT;
3148c2ecf20Sopenharmony_ci	/* Update the next SCB address to download. */
3158c2ecf20Sopenharmony_ci	bmov	NEXT_QUEUED_SCB_ADDR, SCB_NEXT_SCB_BUSADDR, 4;
3168c2ecf20Sopenharmony_ci	/*
3178c2ecf20Sopenharmony_ci	 * NULL out the SCB links since these fields
3188c2ecf20Sopenharmony_ci	 * occupy the same location as SCB_NEXT_SCB_BUSADDR.
3198c2ecf20Sopenharmony_ci	 */
3208c2ecf20Sopenharmony_ci	mvi	SCB_NEXT[1], SCB_LIST_NULL;
3218c2ecf20Sopenharmony_ci	mvi	SCB_NEXT2[1], SCB_LIST_NULL;
3228c2ecf20Sopenharmony_ci	/* Increment our position in the QINFIFO. */
3238c2ecf20Sopenharmony_ci	mov	NONE, SNSCB_QOFF;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	/*
3268c2ecf20Sopenharmony_ci	 * Save SCBID of this SCB in REG0 since
3278c2ecf20Sopenharmony_ci	 * SCBPTR will be clobbered during target
3288c2ecf20Sopenharmony_ci	 * list updates.  We also record the SCB's
3298c2ecf20Sopenharmony_ci	 * flags so that we can refer to them even
3308c2ecf20Sopenharmony_ci	 * after SCBPTR has been changed.
3318c2ecf20Sopenharmony_ci	 */
3328c2ecf20Sopenharmony_ci	bmov	REG0, SCBPTR, 2;
3338c2ecf20Sopenharmony_ci	mov	A, SCB_CONTROL;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	/*
3368c2ecf20Sopenharmony_ci	 * Find the tail SCB of the execution queue
3378c2ecf20Sopenharmony_ci	 * for this target.
3388c2ecf20Sopenharmony_ci	 */
3398c2ecf20Sopenharmony_ci	shr	SINDEX, 3, SCB_SCSIID;
3408c2ecf20Sopenharmony_ci	and	SINDEX, ~0x1;
3418c2ecf20Sopenharmony_ci	mvi	SINDEX[1], (WAITING_SCB_TAILS >> 8);
3428c2ecf20Sopenharmony_ci	bmov	DINDEX, SINDEX, 2;
3438c2ecf20Sopenharmony_ci	bmov	SCBPTR, SINDIR, 2;
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	/*
3468c2ecf20Sopenharmony_ci	 * Update the tail to point to the new SCB.
3478c2ecf20Sopenharmony_ci	 */
3488c2ecf20Sopenharmony_ci	bmov	DINDIR, REG0, 2;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	/*
3518c2ecf20Sopenharmony_ci	 * If the queue was empty, queue this SCB as
3528c2ecf20Sopenharmony_ci	 * the first for this target.
3538c2ecf20Sopenharmony_ci	 */
3548c2ecf20Sopenharmony_ci	cmp	SCBPTR[1], SCB_LIST_NULL je first_new_target_scb;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	/*
3578c2ecf20Sopenharmony_ci	 * SCBs that want to send messages must always be
3588c2ecf20Sopenharmony_ci	 * at the head of their per-target queue so that
3598c2ecf20Sopenharmony_ci	 * ATN can be asserted even if the current
3608c2ecf20Sopenharmony_ci	 * negotiation agreement is packetized.  If the
3618c2ecf20Sopenharmony_ci	 * target queue is empty, the SCB can be queued
3628c2ecf20Sopenharmony_ci	 * immediately.  If the queue is not empty, we must
3638c2ecf20Sopenharmony_ci	 * wait for it to empty before entering this SCB
3648c2ecf20Sopenharmony_ci	 * into the waiting for selection queue.  Otherwise
3658c2ecf20Sopenharmony_ci	 * our batching and round-robin selection scheme 
3668c2ecf20Sopenharmony_ci	 * could allow commands to be queued out of order.
3678c2ecf20Sopenharmony_ci	 * To simplify the implementation, we stop pulling
3688c2ecf20Sopenharmony_ci	 * new commands from the host until the MK_MESSAGE
3698c2ecf20Sopenharmony_ci	 * SCB can be queued to the waiting for selection
3708c2ecf20Sopenharmony_ci	 * list.
3718c2ecf20Sopenharmony_ci	 */
3728c2ecf20Sopenharmony_ci	test	A, MK_MESSAGE jz batch_scb; 
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	/*
3758c2ecf20Sopenharmony_ci	 * If the last SCB is also a MK_MESSAGE SCB, then
3768c2ecf20Sopenharmony_ci	 * order is preserved even if we batch.
3778c2ecf20Sopenharmony_ci	 */
3788c2ecf20Sopenharmony_ci	test	SCB_CONTROL, MK_MESSAGE jz batch_scb; 
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	/*
3818c2ecf20Sopenharmony_ci	 * Defer this SCB and stop fetching new SCBs until
3828c2ecf20Sopenharmony_ci	 * it can be queued.  Since the SCB_SCSIID of the
3838c2ecf20Sopenharmony_ci	 * tail SCB must be the same as that of the newly
3848c2ecf20Sopenharmony_ci	 * queued SCB, there is no need to restore the SCBID
3858c2ecf20Sopenharmony_ci	 * here.
3868c2ecf20Sopenharmony_ci	 */
3878c2ecf20Sopenharmony_ci	or	SEQ_FLAGS2, PENDING_MK_MESSAGE;
3888c2ecf20Sopenharmony_ci	bmov	MK_MESSAGE_SCB, REG0, 2;
3898c2ecf20Sopenharmony_ci	mov	MK_MESSAGE_SCSIID, SCB_SCSIID ret;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cibatch_scb:
3928c2ecf20Sopenharmony_ci	/*
3938c2ecf20Sopenharmony_ci	 * Otherwise just update the previous tail SCB to
3948c2ecf20Sopenharmony_ci	 * point to the new tail.
3958c2ecf20Sopenharmony_ci	 */
3968c2ecf20Sopenharmony_ci	bmov	SCB_NEXT, REG0, 2 ret;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_cifirst_new_target_scb:
3998c2ecf20Sopenharmony_ci	/*
4008c2ecf20Sopenharmony_ci	 * Append SCB to the tail of the waiting for
4018c2ecf20Sopenharmony_ci	 * selection list.
4028c2ecf20Sopenharmony_ci	 */
4038c2ecf20Sopenharmony_ci	cmp	WAITING_TID_HEAD[1], SCB_LIST_NULL je first_new_scb;
4048c2ecf20Sopenharmony_ci	bmov	SCBPTR, WAITING_TID_TAIL, 2;
4058c2ecf20Sopenharmony_ci	bmov	SCB_NEXT2, REG0, 2;
4068c2ecf20Sopenharmony_ci	bmov	WAITING_TID_TAIL, REG0, 2 ret;
4078c2ecf20Sopenharmony_cifirst_new_scb:
4088c2ecf20Sopenharmony_ci	/*
4098c2ecf20Sopenharmony_ci	 * Whole list is empty, so the head of
4108c2ecf20Sopenharmony_ci	 * the list must be initialized too.
4118c2ecf20Sopenharmony_ci	 */
4128c2ecf20Sopenharmony_ci	bmov	WAITING_TID_HEAD, REG0, 2;
4138c2ecf20Sopenharmony_ci	bmov	WAITING_TID_TAIL, REG0, 2 ret;
4148c2ecf20Sopenharmony_ciEND_CRITICAL;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ciscbdma_idle:
4178c2ecf20Sopenharmony_ci	/*
4188c2ecf20Sopenharmony_ci	 * Don't bother downloading new SCBs to execute
4198c2ecf20Sopenharmony_ci	 * if select-outs are currently frozen or we have
4208c2ecf20Sopenharmony_ci	 * a MK_MESSAGE SCB waiting to enter the queue.
4218c2ecf20Sopenharmony_ci	 */
4228c2ecf20Sopenharmony_ci	test	SEQ_FLAGS2, SELECTOUT_QFROZEN|PENDING_MK_MESSAGE
4238c2ecf20Sopenharmony_ci		jnz scbdma_no_new_scbs;
4248c2ecf20Sopenharmony_ciBEGIN_CRITICAL;
4258c2ecf20Sopenharmony_ci	test	QOFF_CTLSTA, NEW_SCB_AVAIL jnz fetch_new_scb;
4268c2ecf20Sopenharmony_ciscbdma_no_new_scbs:
4278c2ecf20Sopenharmony_ci	cmp	COMPLETE_DMA_SCB_HEAD[1], SCB_LIST_NULL jne dma_complete_scb;
4288c2ecf20Sopenharmony_ci	cmp	COMPLETE_SCB_HEAD[1], SCB_LIST_NULL je return;
4298c2ecf20Sopenharmony_ci	/* FALLTHROUGH */
4308c2ecf20Sopenharmony_cifill_qoutfifo:
4318c2ecf20Sopenharmony_ci	/*
4328c2ecf20Sopenharmony_ci	 * Keep track of the SCBs we are dmaing just
4338c2ecf20Sopenharmony_ci	 * in case the DMA fails or is aborted.
4348c2ecf20Sopenharmony_ci	 */
4358c2ecf20Sopenharmony_ci	bmov	COMPLETE_SCB_DMAINPROG_HEAD, COMPLETE_SCB_HEAD, 2;
4368c2ecf20Sopenharmony_ci	mvi	CCSCBCTL, CCSCBRESET;
4378c2ecf20Sopenharmony_ci	bmov	SCBHADDR, QOUTFIFO_NEXT_ADDR, 4;
4388c2ecf20Sopenharmony_ci	mov	A, QOUTFIFO_NEXT_ADDR;
4398c2ecf20Sopenharmony_ci	bmov	SCBPTR, COMPLETE_SCB_HEAD, 2;
4408c2ecf20Sopenharmony_cifill_qoutfifo_loop:
4418c2ecf20Sopenharmony_ci	bmov	CCSCBRAM, SCBPTR, 2;
4428c2ecf20Sopenharmony_ci	mov	CCSCBRAM, SCB_SGPTR[0];
4438c2ecf20Sopenharmony_ci	mov	CCSCBRAM, QOUTFIFO_ENTRY_VALID_TAG;
4448c2ecf20Sopenharmony_ci	mov	NONE, SDSCB_QOFF;
4458c2ecf20Sopenharmony_ci	inc	INT_COALESCING_CMDCOUNT;
4468c2ecf20Sopenharmony_ci	add	CMDS_PENDING, -1;
4478c2ecf20Sopenharmony_ci	adc	CMDS_PENDING[1], -1;
4488c2ecf20Sopenharmony_ci	cmp	SCB_NEXT_COMPLETE[1], SCB_LIST_NULL je fill_qoutfifo_done;
4498c2ecf20Sopenharmony_ci	cmp	CCSCBADDR, CCSCBADDR_MAX je fill_qoutfifo_done;
4508c2ecf20Sopenharmony_ci	test	QOFF_CTLSTA, SDSCB_ROLLOVR jnz fill_qoutfifo_done;
4518c2ecf20Sopenharmony_ci	/*
4528c2ecf20Sopenharmony_ci	 * Don't cross an ADB or Cachline boundary when DMA'ing
4538c2ecf20Sopenharmony_ci	 * completion entries.  In PCI mode, at least in 32/33
4548c2ecf20Sopenharmony_ci	 * configurations, the SCB DMA engine may lose its place
4558c2ecf20Sopenharmony_ci	 * in the data-stream should the target force a retry on
4568c2ecf20Sopenharmony_ci	 * something other than an 8byte aligned boundary. In
4578c2ecf20Sopenharmony_ci	 * PCI-X mode, we do this to avoid split transactions since
4588c2ecf20Sopenharmony_ci	 * many chipsets seem to be unable to format proper split
4598c2ecf20Sopenharmony_ci	 * completions to continue the data transfer.
4608c2ecf20Sopenharmony_ci	 */
4618c2ecf20Sopenharmony_ci	add	SINDEX, A, CCSCBADDR;
4628c2ecf20Sopenharmony_ci	test	SINDEX, CACHELINE_MASK jz fill_qoutfifo_done;
4638c2ecf20Sopenharmony_ci	bmov	SCBPTR, SCB_NEXT_COMPLETE, 2;
4648c2ecf20Sopenharmony_ci	jmp	fill_qoutfifo_loop;
4658c2ecf20Sopenharmony_cifill_qoutfifo_done:
4668c2ecf20Sopenharmony_ci	mov	SCBHCNT, CCSCBADDR;
4678c2ecf20Sopenharmony_ci	mvi	CCSCBCTL, CCSCBEN|CCSCBRESET;
4688c2ecf20Sopenharmony_ci	bmov	COMPLETE_SCB_HEAD, SCB_NEXT_COMPLETE, 2;
4698c2ecf20Sopenharmony_ci	mvi	SCB_NEXT_COMPLETE[1], SCB_LIST_NULL ret;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_cifetch_new_scb:
4728c2ecf20Sopenharmony_ci	bmov	SCBHADDR, NEXT_QUEUED_SCB_ADDR, 4;
4738c2ecf20Sopenharmony_ci	mvi	CCARREN|CCSCBEN|CCSCBDIR|CCSCBRESET jmp dma_scb;
4748c2ecf20Sopenharmony_cidma_complete_scb:
4758c2ecf20Sopenharmony_ci	bmov	SCBPTR, COMPLETE_DMA_SCB_HEAD, 2;
4768c2ecf20Sopenharmony_ci	bmov	SCBHADDR, SCB_BUSADDR, 4;
4778c2ecf20Sopenharmony_ci	mvi	CCARREN|CCSCBEN|CCSCBRESET jmp dma_scb;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci/*
4808c2ecf20Sopenharmony_ci * Either post or fetch an SCB from host memory.  The caller
4818c2ecf20Sopenharmony_ci * is responsible for polling for transfer completion.
4828c2ecf20Sopenharmony_ci *
4838c2ecf20Sopenharmony_ci * Prerequisits: Mode == M_CCHAN
4848c2ecf20Sopenharmony_ci *		 SINDEX contains CCSCBCTL flags
4858c2ecf20Sopenharmony_ci *		 SCBHADDR set to Host SCB address
4868c2ecf20Sopenharmony_ci *		 SCBPTR set to SCB src location on "push" operations
4878c2ecf20Sopenharmony_ci */
4888c2ecf20Sopenharmony_ciSET_SRC_MODE	M_CCHAN;
4898c2ecf20Sopenharmony_ciSET_DST_MODE	M_CCHAN;
4908c2ecf20Sopenharmony_cidma_scb:
4918c2ecf20Sopenharmony_ci	mvi	SCBHCNT, SCB_TRANSFER_SIZE;
4928c2ecf20Sopenharmony_ci	mov	CCSCBCTL, SINDEX ret;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_cisetjmp:
4958c2ecf20Sopenharmony_ci	/*
4968c2ecf20Sopenharmony_ci	 * At least on the A, a return in the same
4978c2ecf20Sopenharmony_ci	 * instruction as the bmov results in a return
4988c2ecf20Sopenharmony_ci	 * to the caller, not to the new address at the
4998c2ecf20Sopenharmony_ci	 * top of the stack.  Since we want the latter
5008c2ecf20Sopenharmony_ci	 * (we use setjmp to register a handler from an
5018c2ecf20Sopenharmony_ci	 * interrupt context but not invoke that handler
5028c2ecf20Sopenharmony_ci	 * until we return to our idle loop), use a
5038c2ecf20Sopenharmony_ci	 * separate ret instruction.
5048c2ecf20Sopenharmony_ci	 */
5058c2ecf20Sopenharmony_ci	bmov	LONGJMP_ADDR, STACK, 2;
5068c2ecf20Sopenharmony_ci	ret;
5078c2ecf20Sopenharmony_cisetjmp_inline:
5088c2ecf20Sopenharmony_ci	bmov	LONGJMP_ADDR, STACK, 2;
5098c2ecf20Sopenharmony_cilongjmp:
5108c2ecf20Sopenharmony_ci	bmov	STACK, LONGJMP_ADDR, 2 ret;
5118c2ecf20Sopenharmony_ciEND_CRITICAL;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci/*************************** Chip Bug Work Arounds ****************************/
5148c2ecf20Sopenharmony_ci/*
5158c2ecf20Sopenharmony_ci * Must disable interrupts when setting the mode pointer
5168c2ecf20Sopenharmony_ci * register as an interrupt occurring mid update will
5178c2ecf20Sopenharmony_ci * fail to store the new mode value for restoration on
5188c2ecf20Sopenharmony_ci * an iret.
5198c2ecf20Sopenharmony_ci */
5208c2ecf20Sopenharmony_ciif ((ahd->bugs & AHD_SET_MODE_BUG) != 0) {
5218c2ecf20Sopenharmony_ciset_mode_work_around:
5228c2ecf20Sopenharmony_ci	mvi	SEQINTCTL, INTVEC1DSL;
5238c2ecf20Sopenharmony_ci	mov	MODE_PTR, SINDEX;
5248c2ecf20Sopenharmony_ci	clr	SEQINTCTL ret;
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ciif ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
5298c2ecf20Sopenharmony_ciset_seqint_work_around:
5308c2ecf20Sopenharmony_ci	mov	SEQINTCODE, SINDEX;
5318c2ecf20Sopenharmony_ci	mvi	SEQINTCODE, NO_SEQINT ret;
5328c2ecf20Sopenharmony_ci}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci/************************ Packetized LongJmp Routines *************************/
5358c2ecf20Sopenharmony_ciSET_SRC_MODE	M_SCSI;
5368c2ecf20Sopenharmony_ciSET_DST_MODE	M_SCSI;
5378c2ecf20Sopenharmony_cistart_selection:
5388c2ecf20Sopenharmony_ciBEGIN_CRITICAL;
5398c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_SENT_SCB_UPDATE_BUG) != 0) {
5408c2ecf20Sopenharmony_ci		/*
5418c2ecf20Sopenharmony_ci		 * Razor #494
5428c2ecf20Sopenharmony_ci		 * Rev A hardware fails to update LAST/CURR/NEXTSCB
5438c2ecf20Sopenharmony_ci		 * correctly after a packetized selection in several
5448c2ecf20Sopenharmony_ci		 * situations:
5458c2ecf20Sopenharmony_ci		 *
5468c2ecf20Sopenharmony_ci		 * 1) If only one command existed in the queue, the
5478c2ecf20Sopenharmony_ci		 *    LAST/CURR/NEXTSCB are unchanged.
5488c2ecf20Sopenharmony_ci		 *
5498c2ecf20Sopenharmony_ci		 * 2) In a non QAS, protocol allowed phase change,
5508c2ecf20Sopenharmony_ci		 *    the queue is shifted 1 too far.  LASTSCB is
5518c2ecf20Sopenharmony_ci		 *    the last SCB that was correctly processed.
5528c2ecf20Sopenharmony_ci		 * 
5538c2ecf20Sopenharmony_ci		 * 3) In the QAS case, if the full list of commands
5548c2ecf20Sopenharmony_ci		 *    was successfully sent, NEXTSCB is NULL and neither
5558c2ecf20Sopenharmony_ci		 *    CURRSCB nor LASTSCB can be trusted.  We must
5568c2ecf20Sopenharmony_ci		 *    manually walk the list counting MAXCMDCNT elements
5578c2ecf20Sopenharmony_ci		 *    to find the last SCB that was sent correctly.
5588c2ecf20Sopenharmony_ci		 *
5598c2ecf20Sopenharmony_ci		 * To simplify the workaround for this bug in SELDO
5608c2ecf20Sopenharmony_ci		 * handling, we initialize LASTSCB prior to enabling
5618c2ecf20Sopenharmony_ci		 * selection so we can rely on it even for case #1 above.
5628c2ecf20Sopenharmony_ci		 */
5638c2ecf20Sopenharmony_ci		bmov	LASTSCB, WAITING_TID_HEAD, 2;
5648c2ecf20Sopenharmony_ci	}
5658c2ecf20Sopenharmony_ci	bmov	CURRSCB, WAITING_TID_HEAD, 2;
5668c2ecf20Sopenharmony_ci	bmov	SCBPTR, WAITING_TID_HEAD, 2;
5678c2ecf20Sopenharmony_ci	shr	SELOID, 4, SCB_SCSIID;
5688c2ecf20Sopenharmony_ci	/*
5698c2ecf20Sopenharmony_ci	 * If we want to send a message to the device, ensure
5708c2ecf20Sopenharmony_ci	 * we are selecting with atn regardless of our packetized
5718c2ecf20Sopenharmony_ci	 * agreement.  Since SPI4 only allows target reset or PPR
5728c2ecf20Sopenharmony_ci	 * messages if this is a packetized connection, the change
5738c2ecf20Sopenharmony_ci	 * to our negotiation table entry for this selection will
5748c2ecf20Sopenharmony_ci	 * be cleared when the message is acted on.
5758c2ecf20Sopenharmony_ci	 */
5768c2ecf20Sopenharmony_ci	test	SCB_CONTROL, MK_MESSAGE jz . + 3;
5778c2ecf20Sopenharmony_ci	mov	NEGOADDR, SELOID;
5788c2ecf20Sopenharmony_ci	or	NEGCONOPTS, ENAUTOATNO;
5798c2ecf20Sopenharmony_ci	or	SCSISEQ0, ENSELO ret;
5808c2ecf20Sopenharmony_ciEND_CRITICAL;
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci/*
5838c2ecf20Sopenharmony_ci * Allocate a FIFO for a non-packetized transaction.
5848c2ecf20Sopenharmony_ci * In RevA hardware, both FIFOs must be free before we
5858c2ecf20Sopenharmony_ci * can allocate a FIFO for a non-packetized transaction.
5868c2ecf20Sopenharmony_ci */
5878c2ecf20Sopenharmony_ciallocate_fifo_loop:
5888c2ecf20Sopenharmony_ci	/*
5898c2ecf20Sopenharmony_ci	 * Do whatever work is required to free a FIFO.
5908c2ecf20Sopenharmony_ci	 */
5918c2ecf20Sopenharmony_ci	call	idle_loop_service_fifos;
5928c2ecf20Sopenharmony_ci	SET_MODE(M_SCSI, M_SCSI)
5938c2ecf20Sopenharmony_ciallocate_fifo:
5948c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_NONPACKFIFO_BUG) != 0) {
5958c2ecf20Sopenharmony_ci		and	A, FIFO0FREE|FIFO1FREE, DFFSTAT;
5968c2ecf20Sopenharmony_ci		cmp	A, FIFO0FREE|FIFO1FREE jne allocate_fifo_loop;
5978c2ecf20Sopenharmony_ci	} else {
5988c2ecf20Sopenharmony_ci		test	DFFSTAT, FIFO1FREE jnz allocate_fifo1;
5998c2ecf20Sopenharmony_ci		test	DFFSTAT, FIFO0FREE jz allocate_fifo_loop;
6008c2ecf20Sopenharmony_ci		mvi	DFFSTAT, B_CURRFIFO_0;
6018c2ecf20Sopenharmony_ci		SET_MODE(M_DFF0, M_DFF0)
6028c2ecf20Sopenharmony_ci		bmov	SCBPTR, ALLOCFIFO_SCBPTR, 2 ret;
6038c2ecf20Sopenharmony_ci	}
6048c2ecf20Sopenharmony_ciSET_SRC_MODE	M_SCSI;
6058c2ecf20Sopenharmony_ciSET_DST_MODE	M_SCSI;
6068c2ecf20Sopenharmony_ciallocate_fifo1:
6078c2ecf20Sopenharmony_ci	mvi	DFFSTAT, CURRFIFO_1;
6088c2ecf20Sopenharmony_ci	SET_MODE(M_DFF1, M_DFF1)
6098c2ecf20Sopenharmony_ci	bmov	SCBPTR, ALLOCFIFO_SCBPTR, 2 ret;
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci/*
6128c2ecf20Sopenharmony_ci * We have been reselected as an initiator
6138c2ecf20Sopenharmony_ci * or selected as a target.
6148c2ecf20Sopenharmony_ci */
6158c2ecf20Sopenharmony_ciSET_SRC_MODE	M_SCSI;
6168c2ecf20Sopenharmony_ciSET_DST_MODE	M_SCSI;
6178c2ecf20Sopenharmony_ciselect_in:
6188c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_FAINT_LED_BUG) != 0) {
6198c2ecf20Sopenharmony_ci		/*
6208c2ecf20Sopenharmony_ci		 * On Rev A. hardware, the busy LED is only
6218c2ecf20Sopenharmony_ci		 * turned on automaically during selections
6228c2ecf20Sopenharmony_ci		 * and re-selections.  Make the LED status
6238c2ecf20Sopenharmony_ci		 * more useful by forcing it to be on from
6248c2ecf20Sopenharmony_ci		 * the point of selection until our idle
6258c2ecf20Sopenharmony_ci		 * loop determines that neither of our FIFOs
6268c2ecf20Sopenharmony_ci		 * are busy.  This handles the non-packetized
6278c2ecf20Sopenharmony_ci		 * case nicely as we will not return to the
6288c2ecf20Sopenharmony_ci		 * idle loop until the busfree at the end of
6298c2ecf20Sopenharmony_ci		 * each transaction.
6308c2ecf20Sopenharmony_ci		 */
6318c2ecf20Sopenharmony_ci		or	SBLKCTL, DIAGLEDEN|DIAGLEDON;
6328c2ecf20Sopenharmony_ci	}
6338c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
6348c2ecf20Sopenharmony_ci		/*
6358c2ecf20Sopenharmony_ci		 * Test to ensure that the bus has not
6368c2ecf20Sopenharmony_ci		 * already gone free prior to clearing
6378c2ecf20Sopenharmony_ci		 * any stale busfree status.  This avoids
6388c2ecf20Sopenharmony_ci		 * a window whereby a busfree just after
6398c2ecf20Sopenharmony_ci		 * a selection could be missed.
6408c2ecf20Sopenharmony_ci		 */
6418c2ecf20Sopenharmony_ci		test	SCSISIGI, BSYI jz . + 2;
6428c2ecf20Sopenharmony_ci		mvi	CLRSINT1,CLRBUSFREE;
6438c2ecf20Sopenharmony_ci		or	SIMODE1, ENBUSFREE;
6448c2ecf20Sopenharmony_ci	}
6458c2ecf20Sopenharmony_ci	or	SXFRCTL0, SPIOEN;
6468c2ecf20Sopenharmony_ci	and	SAVED_SCSIID, SELID_MASK, SELID;
6478c2ecf20Sopenharmony_ci	and	A, OID, IOWNID;
6488c2ecf20Sopenharmony_ci	or	SAVED_SCSIID, A;
6498c2ecf20Sopenharmony_ci	mvi	CLRSINT0, CLRSELDI;
6508c2ecf20Sopenharmony_ci	jmp	ITloop;
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci/*
6538c2ecf20Sopenharmony_ci * We have successfully selected out.
6548c2ecf20Sopenharmony_ci *
6558c2ecf20Sopenharmony_ci * Clear SELDO.
6568c2ecf20Sopenharmony_ci * Dequeue all SCBs sent from the waiting queue
6578c2ecf20Sopenharmony_ci * Requeue all SCBs *not* sent to the tail of the waiting queue
6588c2ecf20Sopenharmony_ci * Take Razor #494 into account for above.
6598c2ecf20Sopenharmony_ci *
6608c2ecf20Sopenharmony_ci * In Packetized Mode:
6618c2ecf20Sopenharmony_ci *	Return to the idle loop.  Our interrupt handler will take
6628c2ecf20Sopenharmony_ci *	care of any incoming L_Qs.
6638c2ecf20Sopenharmony_ci *
6648c2ecf20Sopenharmony_ci * In Non-Packetize Mode:
6658c2ecf20Sopenharmony_ci *	Continue to our normal state machine.
6668c2ecf20Sopenharmony_ci */
6678c2ecf20Sopenharmony_ciSET_SRC_MODE	M_SCSI;
6688c2ecf20Sopenharmony_ciSET_DST_MODE	M_SCSI;
6698c2ecf20Sopenharmony_ciselect_out:
6708c2ecf20Sopenharmony_ciBEGIN_CRITICAL;
6718c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_FAINT_LED_BUG) != 0) {
6728c2ecf20Sopenharmony_ci		/*
6738c2ecf20Sopenharmony_ci		 * On Rev A. hardware, the busy LED is only
6748c2ecf20Sopenharmony_ci		 * turned on automaically during selections
6758c2ecf20Sopenharmony_ci		 * and re-selections.  Make the LED status
6768c2ecf20Sopenharmony_ci		 * more useful by forcing it to be on from
6778c2ecf20Sopenharmony_ci		 * the point of re-selection until our idle
6788c2ecf20Sopenharmony_ci		 * loop determines that neither of our FIFOs
6798c2ecf20Sopenharmony_ci		 * are busy.  This handles the non-packetized
6808c2ecf20Sopenharmony_ci		 * case nicely as we will not return to the
6818c2ecf20Sopenharmony_ci		 * idle loop until the busfree at the end of
6828c2ecf20Sopenharmony_ci		 * each transaction.
6838c2ecf20Sopenharmony_ci		 */
6848c2ecf20Sopenharmony_ci		or	SBLKCTL, DIAGLEDEN|DIAGLEDON;
6858c2ecf20Sopenharmony_ci	}
6868c2ecf20Sopenharmony_ci	/* Clear out all SCBs that have been successfully sent. */
6878c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_SENT_SCB_UPDATE_BUG) != 0) {
6888c2ecf20Sopenharmony_ci		/*
6898c2ecf20Sopenharmony_ci		 * For packetized, the LQO manager clears ENSELO on
6908c2ecf20Sopenharmony_ci		 * the assertion of SELDO.  If we are non-packetized,
6918c2ecf20Sopenharmony_ci		 * LASTSCB and CURRSCB are accurate.
6928c2ecf20Sopenharmony_ci		 */
6938c2ecf20Sopenharmony_ci		test	SCSISEQ0, ENSELO jnz use_lastscb;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci		/*
6968c2ecf20Sopenharmony_ci		 * The update is correct for LQOSTAT1 errors.  All
6978c2ecf20Sopenharmony_ci		 * but LQOBUSFREE are handled by kernel interrupts.
6988c2ecf20Sopenharmony_ci		 * If we see LQOBUSFREE, return to the idle loop.
6998c2ecf20Sopenharmony_ci		 * Once we are out of the select_out critical section,
7008c2ecf20Sopenharmony_ci		 * the kernel will cleanup the LQOBUSFREE and we will
7018c2ecf20Sopenharmony_ci		 * eventually restart the selection if appropriate.
7028c2ecf20Sopenharmony_ci		 */
7038c2ecf20Sopenharmony_ci		test	LQOSTAT1, LQOBUSFREE jnz idle_loop;
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci		/*
7068c2ecf20Sopenharmony_ci		 * On a phase change oustside of packet boundaries,
7078c2ecf20Sopenharmony_ci		 * LASTSCB points to the currently active SCB context
7088c2ecf20Sopenharmony_ci		 * on the bus.
7098c2ecf20Sopenharmony_ci		 */
7108c2ecf20Sopenharmony_ci		test	LQOSTAT2, LQOPHACHGOUTPKT jnz use_lastscb;
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci		/*
7138c2ecf20Sopenharmony_ci		 * If the hardware has traversed the whole list, NEXTSCB
7148c2ecf20Sopenharmony_ci		 * will be NULL, CURRSCB and LASTSCB cannot be trusted,
7158c2ecf20Sopenharmony_ci		 * but MAXCMDCNT is accurate.  If we stop part way through
7168c2ecf20Sopenharmony_ci		 * the list or only had one command to issue, NEXTSCB[1] is
7178c2ecf20Sopenharmony_ci		 * not NULL and LASTSCB is the last command to go out.
7188c2ecf20Sopenharmony_ci		 */
7198c2ecf20Sopenharmony_ci		cmp	NEXTSCB[1], SCB_LIST_NULL jne use_lastscb;
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci		/*
7228c2ecf20Sopenharmony_ci		 * Brute force walk.
7238c2ecf20Sopenharmony_ci		 */
7248c2ecf20Sopenharmony_ci		bmov	SCBPTR, WAITING_TID_HEAD, 2;
7258c2ecf20Sopenharmony_ci		mvi	SEQINTCTL, INTVEC1DSL;
7268c2ecf20Sopenharmony_ci		mvi	MODE_PTR, MK_MODE(M_CFG, M_CFG);
7278c2ecf20Sopenharmony_ci		mov	A, MAXCMDCNT;
7288c2ecf20Sopenharmony_ci		mvi	MODE_PTR, MK_MODE(M_SCSI, M_SCSI);
7298c2ecf20Sopenharmony_ci		clr	SEQINTCTL;
7308c2ecf20Sopenharmony_cifind_lastscb_loop:
7318c2ecf20Sopenharmony_ci		dec	A;
7328c2ecf20Sopenharmony_ci		test	A, 0xFF jz found_last_sent_scb;
7338c2ecf20Sopenharmony_ci		bmov	SCBPTR, SCB_NEXT, 2;
7348c2ecf20Sopenharmony_ci		jmp	find_lastscb_loop;
7358c2ecf20Sopenharmony_ciuse_lastscb:
7368c2ecf20Sopenharmony_ci		bmov	SCBPTR, LASTSCB, 2;
7378c2ecf20Sopenharmony_cifound_last_sent_scb:
7388c2ecf20Sopenharmony_ci		bmov	CURRSCB, SCBPTR, 2;
7398c2ecf20Sopenharmony_cicurscb_ww_done:
7408c2ecf20Sopenharmony_ci	} else {
7418c2ecf20Sopenharmony_ci		bmov	SCBPTR, CURRSCB, 2;
7428c2ecf20Sopenharmony_ci	}
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	/*
7458c2ecf20Sopenharmony_ci	 * The whole list made it.  Clear our tail pointer to indicate
7468c2ecf20Sopenharmony_ci	 * that the per-target selection queue is now empty.
7478c2ecf20Sopenharmony_ci	 */
7488c2ecf20Sopenharmony_ci	cmp	SCB_NEXT[1], SCB_LIST_NULL je select_out_clear_tail;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	/*
7518c2ecf20Sopenharmony_ci	 * Requeue any SCBs not sent, to the tail of the waiting Q.
7528c2ecf20Sopenharmony_ci	 * We know that neither the per-TID list nor the list of
7538c2ecf20Sopenharmony_ci	 * TIDs is empty.  Use this knowledge to our advantage and
7548c2ecf20Sopenharmony_ci	 * queue the remainder to the tail of the global execution
7558c2ecf20Sopenharmony_ci	 * queue.
7568c2ecf20Sopenharmony_ci	 */
7578c2ecf20Sopenharmony_ci	bmov	REG0, SCB_NEXT, 2;
7588c2ecf20Sopenharmony_ciselect_out_queue_remainder:
7598c2ecf20Sopenharmony_ci	bmov	SCBPTR, WAITING_TID_TAIL, 2;
7608c2ecf20Sopenharmony_ci	bmov	SCB_NEXT2, REG0, 2;
7618c2ecf20Sopenharmony_ci	bmov	WAITING_TID_TAIL, REG0, 2;
7628c2ecf20Sopenharmony_ci	jmp	select_out_inc_tid_q;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ciselect_out_clear_tail:
7658c2ecf20Sopenharmony_ci	/*
7668c2ecf20Sopenharmony_ci	 * Queue any pending MK_MESSAGE SCB for this target now
7678c2ecf20Sopenharmony_ci	 * that the queue is empty.
7688c2ecf20Sopenharmony_ci	 */
7698c2ecf20Sopenharmony_ci	test	SEQ_FLAGS2, PENDING_MK_MESSAGE jz select_out_no_mk_message_scb;
7708c2ecf20Sopenharmony_ci	mov	A, MK_MESSAGE_SCSIID;
7718c2ecf20Sopenharmony_ci	cmp	SCB_SCSIID, A jne select_out_no_mk_message_scb;
7728c2ecf20Sopenharmony_ci	and	SEQ_FLAGS2, ~PENDING_MK_MESSAGE;
7738c2ecf20Sopenharmony_ci	bmov	REG0, MK_MESSAGE_SCB, 2;
7748c2ecf20Sopenharmony_ci	jmp select_out_queue_remainder;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ciselect_out_no_mk_message_scb:
7778c2ecf20Sopenharmony_ci	/*
7788c2ecf20Sopenharmony_ci	 * Clear this target's execution tail and increment the queue.
7798c2ecf20Sopenharmony_ci	 */
7808c2ecf20Sopenharmony_ci	shr	DINDEX, 3, SCB_SCSIID;
7818c2ecf20Sopenharmony_ci	or	DINDEX, 1;	/* Want only the second byte */
7828c2ecf20Sopenharmony_ci	mvi	DINDEX[1], ((WAITING_SCB_TAILS) >> 8);
7838c2ecf20Sopenharmony_ci	mvi	DINDIR, SCB_LIST_NULL;
7848c2ecf20Sopenharmony_ciselect_out_inc_tid_q:
7858c2ecf20Sopenharmony_ci	bmov	SCBPTR, WAITING_TID_HEAD, 2;
7868c2ecf20Sopenharmony_ci	bmov	WAITING_TID_HEAD, SCB_NEXT2, 2;
7878c2ecf20Sopenharmony_ci	cmp	WAITING_TID_HEAD[1], SCB_LIST_NULL jne . + 2;
7888c2ecf20Sopenharmony_ci	mvi	WAITING_TID_TAIL[1], SCB_LIST_NULL;
7898c2ecf20Sopenharmony_ci	bmov	SCBPTR, CURRSCB, 2;
7908c2ecf20Sopenharmony_ci	mvi	CLRSINT0, CLRSELDO;
7918c2ecf20Sopenharmony_ci	test	LQOSTAT2, LQOPHACHGOUTPKT jnz unexpected_nonpkt_mode_cleared;
7928c2ecf20Sopenharmony_ci	test	LQOSTAT1, LQOPHACHGINPKT jnz unexpected_nonpkt_mode_cleared;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	/*
7958c2ecf20Sopenharmony_ci	 * If this is a packetized connection, return to our
7968c2ecf20Sopenharmony_ci	 * idle_loop and let our interrupt handler deal with
7978c2ecf20Sopenharmony_ci	 * any connection setup/teardown issues.  The only
7988c2ecf20Sopenharmony_ci	 * exceptions are the case of MK_MESSAGE and task management
7998c2ecf20Sopenharmony_ci	 * SCBs.
8008c2ecf20Sopenharmony_ci	 */
8018c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_LQO_ATNO_BUG) != 0) {
8028c2ecf20Sopenharmony_ci		/*
8038c2ecf20Sopenharmony_ci		 * In the A, the LQO manager transitions to LQOSTOP0 even if
8048c2ecf20Sopenharmony_ci		 * we have selected out with ATN asserted and the target
8058c2ecf20Sopenharmony_ci		 * REQs in a non-packet phase.
8068c2ecf20Sopenharmony_ci		 */
8078c2ecf20Sopenharmony_ci		test 	SCB_CONTROL, MK_MESSAGE jz select_out_no_message;
8088c2ecf20Sopenharmony_ci		test	SCSISIGO, ATNO jnz select_out_non_packetized;
8098c2ecf20Sopenharmony_ciselect_out_no_message:
8108c2ecf20Sopenharmony_ci	}
8118c2ecf20Sopenharmony_ci	test	LQOSTAT2, LQOSTOP0 jz select_out_non_packetized;
8128c2ecf20Sopenharmony_ci	test	SCB_TASK_MANAGEMENT, 0xFF jz idle_loop;
8138c2ecf20Sopenharmony_ci	SET_SEQINTCODE(TASKMGMT_FUNC_COMPLETE)
8148c2ecf20Sopenharmony_ci	jmp	idle_loop;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ciselect_out_non_packetized:
8178c2ecf20Sopenharmony_ci	/* Non packetized request. */
8188c2ecf20Sopenharmony_ci	and     SCSISEQ0, ~ENSELO;
8198c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
8208c2ecf20Sopenharmony_ci		/*
8218c2ecf20Sopenharmony_ci		 * Test to ensure that the bus has not
8228c2ecf20Sopenharmony_ci		 * already gone free prior to clearing
8238c2ecf20Sopenharmony_ci		 * any stale busfree status.  This avoids
8248c2ecf20Sopenharmony_ci		 * a window whereby a busfree just after
8258c2ecf20Sopenharmony_ci		 * a selection could be missed.
8268c2ecf20Sopenharmony_ci		 */
8278c2ecf20Sopenharmony_ci		test	SCSISIGI, BSYI jz . + 2;
8288c2ecf20Sopenharmony_ci		mvi	CLRSINT1,CLRBUSFREE;
8298c2ecf20Sopenharmony_ci		or	SIMODE1, ENBUSFREE;
8308c2ecf20Sopenharmony_ci	}
8318c2ecf20Sopenharmony_ci	mov	SAVED_SCSIID, SCB_SCSIID;
8328c2ecf20Sopenharmony_ci	mov	SAVED_LUN, SCB_LUN;
8338c2ecf20Sopenharmony_ci	mvi	SEQ_FLAGS, NO_CDB_SENT;
8348c2ecf20Sopenharmony_ciEND_CRITICAL;
8358c2ecf20Sopenharmony_ci	or	SXFRCTL0, SPIOEN;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	/*
8388c2ecf20Sopenharmony_ci	 * As soon as we get a successful selection, the target
8398c2ecf20Sopenharmony_ci	 * should go into the message out phase since we have ATN
8408c2ecf20Sopenharmony_ci	 * asserted.
8418c2ecf20Sopenharmony_ci	 */
8428c2ecf20Sopenharmony_ci	mvi	MSG_OUT, MSG_IDENTIFYFLAG;
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	/*
8458c2ecf20Sopenharmony_ci	 * Main loop for information transfer phases.  Wait for the
8468c2ecf20Sopenharmony_ci	 * target to assert REQ before checking MSG, C/D and I/O for
8478c2ecf20Sopenharmony_ci	 * the bus phase.
8488c2ecf20Sopenharmony_ci	 */
8498c2ecf20Sopenharmony_cimesgin_phasemis:
8508c2ecf20Sopenharmony_ciITloop:
8518c2ecf20Sopenharmony_ci	call	phase_lock;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	mov	A, LASTPHASE;
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	test	A, ~P_DATAIN_DT	jz p_data;
8568c2ecf20Sopenharmony_ci	cmp	A,P_COMMAND	je p_command;
8578c2ecf20Sopenharmony_ci	cmp	A,P_MESGOUT	je p_mesgout;
8588c2ecf20Sopenharmony_ci	cmp	A,P_STATUS	je p_status;
8598c2ecf20Sopenharmony_ci	cmp	A,P_MESGIN	je p_mesgin;
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	SET_SEQINTCODE(BAD_PHASE)
8628c2ecf20Sopenharmony_ci	jmp	ITloop;			/* Try reading the bus again. */
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci/*
8658c2ecf20Sopenharmony_ci * Command phase.  Set up the DMA registers and let 'er rip.
8668c2ecf20Sopenharmony_ci */
8678c2ecf20Sopenharmony_cip_command:
8688c2ecf20Sopenharmony_ci	test	SEQ_FLAGS, NOT_IDENTIFIED jz p_command_okay;
8698c2ecf20Sopenharmony_ci	SET_SEQINTCODE(PROTO_VIOLATION)
8708c2ecf20Sopenharmony_cip_command_okay:
8718c2ecf20Sopenharmony_ci	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
8728c2ecf20Sopenharmony_ci		jnz p_command_allocate_fifo;
8738c2ecf20Sopenharmony_ci	/*
8748c2ecf20Sopenharmony_ci	 * Command retry.  Free our current FIFO and
8758c2ecf20Sopenharmony_ci	 * re-allocate a FIFO so transfer state is
8768c2ecf20Sopenharmony_ci	 * reset.
8778c2ecf20Sopenharmony_ci	 */
8788c2ecf20Sopenharmony_ciSET_SRC_MODE	M_DFF1;
8798c2ecf20Sopenharmony_ciSET_DST_MODE	M_DFF1;
8808c2ecf20Sopenharmony_ci	mvi	DFFSXFRCTL, RSTCHN|CLRSHCNT;
8818c2ecf20Sopenharmony_ci	SET_MODE(M_SCSI, M_SCSI)
8828c2ecf20Sopenharmony_cip_command_allocate_fifo:
8838c2ecf20Sopenharmony_ci	bmov	ALLOCFIFO_SCBPTR, SCBPTR, 2;
8848c2ecf20Sopenharmony_ci	call	allocate_fifo;
8858c2ecf20Sopenharmony_ciSET_SRC_MODE	M_DFF1;
8868c2ecf20Sopenharmony_ciSET_DST_MODE	M_DFF1;
8878c2ecf20Sopenharmony_ci	add	NONE, -17, SCB_CDB_LEN;
8888c2ecf20Sopenharmony_ci	jnc	p_command_embedded;
8898c2ecf20Sopenharmony_cip_command_from_host:
8908c2ecf20Sopenharmony_ci	bmov	HADDR[0], SCB_HOST_CDB_PTR, 9;
8918c2ecf20Sopenharmony_ci	mvi	SG_CACHE_PRE, LAST_SEG;
8928c2ecf20Sopenharmony_ci	mvi	DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN);
8938c2ecf20Sopenharmony_ci	jmp	p_command_xfer;
8948c2ecf20Sopenharmony_cip_command_embedded:
8958c2ecf20Sopenharmony_ci	bmov	SHCNT[0], SCB_CDB_LEN,  1;
8968c2ecf20Sopenharmony_ci	bmov	DFDAT, SCB_CDB_STORE, 16; 
8978c2ecf20Sopenharmony_ci	mvi	DFCNTRL, SCSIEN;
8988c2ecf20Sopenharmony_cip_command_xfer:
8998c2ecf20Sopenharmony_ci	and	SEQ_FLAGS, ~NO_CDB_SENT;
9008c2ecf20Sopenharmony_ci	if ((ahd->features & AHD_FAST_CDB_DELIVERY) != 0) {
9018c2ecf20Sopenharmony_ci		/*
9028c2ecf20Sopenharmony_ci		 * To speed up CDB delivery in Rev B, all CDB acks
9038c2ecf20Sopenharmony_ci		 * are "released" to the output sync as soon as the
9048c2ecf20Sopenharmony_ci		 * command phase starts.  There is only one problem
9058c2ecf20Sopenharmony_ci		 * with this approach.  If the target changes phase
9068c2ecf20Sopenharmony_ci		 * before all data are sent, we have left over acks
9078c2ecf20Sopenharmony_ci		 * that can go out on the bus in a data phase.  Due
9088c2ecf20Sopenharmony_ci		 * to other chip contraints, this only happens if
9098c2ecf20Sopenharmony_ci		 * the target goes to data-in, but if the acks go
9108c2ecf20Sopenharmony_ci		 * out before we can test SDONE, we'll think that
9118c2ecf20Sopenharmony_ci		 * the transfer has completed successfully.  Work
9128c2ecf20Sopenharmony_ci		 * around this by taking advantage of the 400ns or
9138c2ecf20Sopenharmony_ci		 * 800ns dead time between command phase and the REQ
9148c2ecf20Sopenharmony_ci		 * of the new phase.  If the transfer has completed
9158c2ecf20Sopenharmony_ci		 * successfully, SCSIEN should fall *long* before we
9168c2ecf20Sopenharmony_ci		 * see a phase change.  We thus treat any phasemiss
9178c2ecf20Sopenharmony_ci		 * that occurs before SCSIEN falls as an incomplete
9188c2ecf20Sopenharmony_ci		 * transfer.
9198c2ecf20Sopenharmony_ci		 */
9208c2ecf20Sopenharmony_ci		test	SSTAT1, PHASEMIS jnz p_command_xfer_failed;
9218c2ecf20Sopenharmony_ci		test	DFCNTRL, SCSIEN jnz . - 1;
9228c2ecf20Sopenharmony_ci	} else {
9238c2ecf20Sopenharmony_ci		test	DFCNTRL, SCSIEN jnz .;
9248c2ecf20Sopenharmony_ci	}
9258c2ecf20Sopenharmony_ci	/*
9268c2ecf20Sopenharmony_ci	 * DMA Channel automatically disabled.
9278c2ecf20Sopenharmony_ci	 * Don't allow a data phase if the command
9288c2ecf20Sopenharmony_ci	 * was not fully transferred.
9298c2ecf20Sopenharmony_ci	 */
9308c2ecf20Sopenharmony_ci	test	SSTAT2, SDONE jnz ITloop;
9318c2ecf20Sopenharmony_cip_command_xfer_failed:
9328c2ecf20Sopenharmony_ci	or	SEQ_FLAGS, NO_CDB_SENT;
9338c2ecf20Sopenharmony_ci	jmp	ITloop;
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci/*
9378c2ecf20Sopenharmony_ci * Status phase.  Wait for the data byte to appear, then read it
9388c2ecf20Sopenharmony_ci * and store it into the SCB.
9398c2ecf20Sopenharmony_ci */
9408c2ecf20Sopenharmony_ciSET_SRC_MODE	M_SCSI;
9418c2ecf20Sopenharmony_ciSET_DST_MODE	M_SCSI;
9428c2ecf20Sopenharmony_cip_status:
9438c2ecf20Sopenharmony_ci	test	SEQ_FLAGS,NOT_IDENTIFIED jnz mesgin_proto_violation;
9448c2ecf20Sopenharmony_cip_status_okay:
9458c2ecf20Sopenharmony_ci	mov	SCB_SCSI_STATUS, SCSIDAT;
9468c2ecf20Sopenharmony_ci	or	SCB_CONTROL, STATUS_RCVD;
9478c2ecf20Sopenharmony_ci	jmp	ITloop;
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci/*
9508c2ecf20Sopenharmony_ci * Message out phase.  If MSG_OUT is MSG_IDENTIFYFLAG, build a full
9518c2ecf20Sopenharmony_ci * indentify message sequence and send it to the target.  The host may
9528c2ecf20Sopenharmony_ci * override this behavior by setting the MK_MESSAGE bit in the SCB
9538c2ecf20Sopenharmony_ci * control byte.  This will cause us to interrupt the host and allow
9548c2ecf20Sopenharmony_ci * it to handle the message phase completely on its own.  If the bit
9558c2ecf20Sopenharmony_ci * associated with this target is set, we will also interrupt the host,
9568c2ecf20Sopenharmony_ci * thereby allowing it to send a message on the next selection regardless
9578c2ecf20Sopenharmony_ci * of the transaction being sent.
9588c2ecf20Sopenharmony_ci * 
9598c2ecf20Sopenharmony_ci * If MSG_OUT is == HOST_MSG, also interrupt the host and take a message.
9608c2ecf20Sopenharmony_ci * This is done to allow the host to send messages outside of an identify
9618c2ecf20Sopenharmony_ci * sequence while protecting the seqencer from testing the MK_MESSAGE bit
9628c2ecf20Sopenharmony_ci * on an SCB that might not be for the current nexus. (For example, a
9638c2ecf20Sopenharmony_ci * BDR message in response to a bad reselection would leave us pointed to
9648c2ecf20Sopenharmony_ci * an SCB that doesn't have anything to do with the current target).
9658c2ecf20Sopenharmony_ci *
9668c2ecf20Sopenharmony_ci * Otherwise, treat MSG_OUT as a 1 byte message to send (abort, abort tag,
9678c2ecf20Sopenharmony_ci * bus device reset).
9688c2ecf20Sopenharmony_ci *
9698c2ecf20Sopenharmony_ci * When there are no messages to send, MSG_OUT should be set to MSG_NOOP,
9708c2ecf20Sopenharmony_ci * in case the target decides to put us in this phase for some strange
9718c2ecf20Sopenharmony_ci * reason.
9728c2ecf20Sopenharmony_ci */
9738c2ecf20Sopenharmony_cip_mesgout_retry:
9748c2ecf20Sopenharmony_ci	/* Turn on ATN for the retry */
9758c2ecf20Sopenharmony_ci	mvi	SCSISIGO, ATNO;
9768c2ecf20Sopenharmony_cip_mesgout:
9778c2ecf20Sopenharmony_ci	mov	SINDEX, MSG_OUT;
9788c2ecf20Sopenharmony_ci	cmp	SINDEX, MSG_IDENTIFYFLAG jne p_mesgout_from_host;
9798c2ecf20Sopenharmony_ci	test	SCB_CONTROL,MK_MESSAGE	jnz host_message_loop;
9808c2ecf20Sopenharmony_cip_mesgout_identify:
9818c2ecf20Sopenharmony_ci	or	SINDEX, MSG_IDENTIFYFLAG|DISCENB, SCB_LUN;
9828c2ecf20Sopenharmony_ci	test	SCB_CONTROL, DISCENB jnz . + 2;
9838c2ecf20Sopenharmony_ci	and	SINDEX, ~DISCENB;
9848c2ecf20Sopenharmony_ci/*
9858c2ecf20Sopenharmony_ci * Send a tag message if TAG_ENB is set in the SCB control block.
9868c2ecf20Sopenharmony_ci * Use SCB_NONPACKET_TAG as the tag value.
9878c2ecf20Sopenharmony_ci */
9888c2ecf20Sopenharmony_cip_mesgout_tag:
9898c2ecf20Sopenharmony_ci	test	SCB_CONTROL,TAG_ENB jz  p_mesgout_onebyte;
9908c2ecf20Sopenharmony_ci	mov	SCSIDAT, SINDEX;	/* Send the identify message */
9918c2ecf20Sopenharmony_ci	call	phase_lock;
9928c2ecf20Sopenharmony_ci	cmp	LASTPHASE, P_MESGOUT	jne p_mesgout_done;
9938c2ecf20Sopenharmony_ci	and	SCSIDAT,TAG_ENB|SCB_TAG_TYPE,SCB_CONTROL;
9948c2ecf20Sopenharmony_ci	call	phase_lock;
9958c2ecf20Sopenharmony_ci	cmp	LASTPHASE, P_MESGOUT	jne p_mesgout_done;
9968c2ecf20Sopenharmony_ci	mov	SCBPTR jmp p_mesgout_onebyte;
9978c2ecf20Sopenharmony_ci/*
9988c2ecf20Sopenharmony_ci * Interrupt the driver, and allow it to handle this message
9998c2ecf20Sopenharmony_ci * phase and any required retries.
10008c2ecf20Sopenharmony_ci */
10018c2ecf20Sopenharmony_cip_mesgout_from_host:
10028c2ecf20Sopenharmony_ci	cmp	SINDEX, HOST_MSG	jne p_mesgout_onebyte;
10038c2ecf20Sopenharmony_ci	jmp	host_message_loop;
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_cip_mesgout_onebyte:
10068c2ecf20Sopenharmony_ci	mvi	CLRSINT1, CLRATNO;
10078c2ecf20Sopenharmony_ci	mov	SCSIDAT, SINDEX;
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci/*
10108c2ecf20Sopenharmony_ci * If the next bus phase after ATN drops is message out, it means
10118c2ecf20Sopenharmony_ci * that the target is requesting that the last message(s) be resent.
10128c2ecf20Sopenharmony_ci */
10138c2ecf20Sopenharmony_ci	call	phase_lock;
10148c2ecf20Sopenharmony_ci	cmp	LASTPHASE, P_MESGOUT	je p_mesgout_retry;
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_cip_mesgout_done:
10178c2ecf20Sopenharmony_ci	mvi	CLRSINT1,CLRATNO;	/* Be sure to turn ATNO off */
10188c2ecf20Sopenharmony_ci	mov	LAST_MSG, MSG_OUT;
10198c2ecf20Sopenharmony_ci	mvi	MSG_OUT, MSG_NOOP;	/* No message left */
10208c2ecf20Sopenharmony_ci	jmp	ITloop;
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci/*
10238c2ecf20Sopenharmony_ci * Message in phase.  Bytes are read using Automatic PIO mode.
10248c2ecf20Sopenharmony_ci */
10258c2ecf20Sopenharmony_cip_mesgin:
10268c2ecf20Sopenharmony_ci	/* read the 1st message byte */
10278c2ecf20Sopenharmony_ci	mvi	ACCUM		call inb_first;
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	test	A,MSG_IDENTIFYFLAG	jnz mesgin_identify;
10308c2ecf20Sopenharmony_ci	cmp	A,MSG_DISCONNECT	je mesgin_disconnect;
10318c2ecf20Sopenharmony_ci	cmp	A,MSG_SAVEDATAPOINTER	je mesgin_sdptrs;
10328c2ecf20Sopenharmony_ci	cmp	ALLZEROS,A		je mesgin_complete;
10338c2ecf20Sopenharmony_ci	cmp	A,MSG_RESTOREPOINTERS	je mesgin_rdptrs;
10348c2ecf20Sopenharmony_ci	cmp	A,MSG_IGN_WIDE_RESIDUE	je mesgin_ign_wide_residue;
10358c2ecf20Sopenharmony_ci	cmp	A,MSG_NOOP		je mesgin_done;
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci/*
10388c2ecf20Sopenharmony_ci * Pushed message loop to allow the kernel to
10398c2ecf20Sopenharmony_ci * run it's own message state engine.  To avoid an
10408c2ecf20Sopenharmony_ci * extra nop instruction after signaling the kernel,
10418c2ecf20Sopenharmony_ci * we perform the phase_lock before checking to see
10428c2ecf20Sopenharmony_ci * if we should exit the loop and skip the phase_lock
10438c2ecf20Sopenharmony_ci * in the ITloop.  Performing back to back phase_locks
10448c2ecf20Sopenharmony_ci * shouldn't hurt, but why do it twice...
10458c2ecf20Sopenharmony_ci */
10468c2ecf20Sopenharmony_cihost_message_loop:
10478c2ecf20Sopenharmony_ci	call	phase_lock;	/* Benign the first time through. */
10488c2ecf20Sopenharmony_ci	SET_SEQINTCODE(HOST_MSG_LOOP)
10498c2ecf20Sopenharmony_ci	cmp	RETURN_1, EXIT_MSG_LOOP	je ITloop;
10508c2ecf20Sopenharmony_ci	cmp	RETURN_1, CONT_MSG_LOOP_WRITE	jne . + 3;
10518c2ecf20Sopenharmony_ci	mov	SCSIDAT, RETURN_2;
10528c2ecf20Sopenharmony_ci	jmp	host_message_loop;
10538c2ecf20Sopenharmony_ci	/* Must be CONT_MSG_LOOP_READ */
10548c2ecf20Sopenharmony_ci	mov	NONE, SCSIDAT;	/* ACK Byte */
10558c2ecf20Sopenharmony_ci	jmp	host_message_loop;
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_cimesgin_ign_wide_residue:
10588c2ecf20Sopenharmony_ci	mov	SAVED_MODE, MODE_PTR;
10598c2ecf20Sopenharmony_ci	SET_MODE(M_SCSI, M_SCSI)
10608c2ecf20Sopenharmony_ci	shr	NEGOADDR, 4, SAVED_SCSIID;
10618c2ecf20Sopenharmony_ci	mov	A, NEGCONOPTS;
10628c2ecf20Sopenharmony_ci	RESTORE_MODE(SAVED_MODE)
10638c2ecf20Sopenharmony_ci	test	A, WIDEXFER jz mesgin_reject;
10648c2ecf20Sopenharmony_ci	/* Pull the residue byte */
10658c2ecf20Sopenharmony_ci	mvi	REG0	call inb_next;
10668c2ecf20Sopenharmony_ci	cmp	REG0, 0x01 jne mesgin_reject;
10678c2ecf20Sopenharmony_ci	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz . + 2;
10688c2ecf20Sopenharmony_ci	test	SCB_TASK_ATTRIBUTE, SCB_XFERLEN_ODD jnz mesgin_done;
10698c2ecf20Sopenharmony_ci	SET_SEQINTCODE(IGN_WIDE_RES)
10708c2ecf20Sopenharmony_ci	jmp	mesgin_done;
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_cimesgin_proto_violation:
10738c2ecf20Sopenharmony_ci	SET_SEQINTCODE(PROTO_VIOLATION)
10748c2ecf20Sopenharmony_ci	jmp	mesgin_done;
10758c2ecf20Sopenharmony_cimesgin_reject:
10768c2ecf20Sopenharmony_ci	mvi	MSG_MESSAGE_REJECT	call mk_mesg;
10778c2ecf20Sopenharmony_cimesgin_done:
10788c2ecf20Sopenharmony_ci	mov	NONE,SCSIDAT;		/*dummy read from latch to ACK*/
10798c2ecf20Sopenharmony_ci	jmp	ITloop;
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci#define INDEX_DISC_LIST(scsiid, lun)					\
10828c2ecf20Sopenharmony_ci	and	A, 0xC0, scsiid;					\
10838c2ecf20Sopenharmony_ci	or	SCBPTR, A, lun;						\
10848c2ecf20Sopenharmony_ci	clr	SCBPTR[1];						\
10858c2ecf20Sopenharmony_ci	and	SINDEX, 0x30, scsiid;					\
10868c2ecf20Sopenharmony_ci	shr	SINDEX, 3;	/* Multiply by 2 */			\
10878c2ecf20Sopenharmony_ci	add	SINDEX, (SCB_DISCONNECTED_LISTS & 0xFF);		\
10888c2ecf20Sopenharmony_ci	mvi	SINDEX[1], ((SCB_DISCONNECTED_LISTS >> 8) & 0xFF)
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_cimesgin_identify:
10918c2ecf20Sopenharmony_ci	/*
10928c2ecf20Sopenharmony_ci	 * Determine whether a target is using tagged or non-tagged
10938c2ecf20Sopenharmony_ci	 * transactions by first looking at the transaction stored in
10948c2ecf20Sopenharmony_ci	 * the per-device, disconnected array.  If there is no untagged
10958c2ecf20Sopenharmony_ci	 * transaction for this target, this must be a tagged transaction.
10968c2ecf20Sopenharmony_ci	 */
10978c2ecf20Sopenharmony_ci	and	SAVED_LUN, MSG_IDENTIFY_LUNMASK, A;
10988c2ecf20Sopenharmony_ci	INDEX_DISC_LIST(SAVED_SCSIID, SAVED_LUN);
10998c2ecf20Sopenharmony_ci	bmov	DINDEX, SINDEX, 2;
11008c2ecf20Sopenharmony_ci	bmov	REG0, SINDIR, 2;
11018c2ecf20Sopenharmony_ci	cmp	REG0[1], SCB_LIST_NULL je snoop_tag;
11028c2ecf20Sopenharmony_ci	/* Untagged.  Clear the busy table entry and setup the SCB. */
11038c2ecf20Sopenharmony_ci	bmov	DINDIR, ALLONES, 2;
11048c2ecf20Sopenharmony_ci	bmov	SCBPTR, REG0, 2;
11058c2ecf20Sopenharmony_ci	jmp	setup_SCB;
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci/*
11088c2ecf20Sopenharmony_ci * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
11098c2ecf20Sopenharmony_ci * If we get one, we use the tag returned to find the proper
11108c2ecf20Sopenharmony_ci * SCB.  After receiving the tag, look for the SCB at SCB locations tag and
11118c2ecf20Sopenharmony_ci * tag + 256.
11128c2ecf20Sopenharmony_ci */
11138c2ecf20Sopenharmony_cisnoop_tag:
11148c2ecf20Sopenharmony_ci	if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
11158c2ecf20Sopenharmony_ci		or	SEQ_FLAGS, 0x80;
11168c2ecf20Sopenharmony_ci	}
11178c2ecf20Sopenharmony_ci	mov	NONE, SCSIDAT;		/* ACK Identify MSG */
11188c2ecf20Sopenharmony_ci	call	phase_lock;
11198c2ecf20Sopenharmony_ci	if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
11208c2ecf20Sopenharmony_ci		or	SEQ_FLAGS, 0x1;
11218c2ecf20Sopenharmony_ci	}
11228c2ecf20Sopenharmony_ci	cmp	LASTPHASE, P_MESGIN	jne not_found_ITloop;
11238c2ecf20Sopenharmony_ci	if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
11248c2ecf20Sopenharmony_ci		or	SEQ_FLAGS, 0x2;
11258c2ecf20Sopenharmony_ci	}
11268c2ecf20Sopenharmony_ci	cmp	SCSIBUS, MSG_SIMPLE_Q_TAG jne not_found;
11278c2ecf20Sopenharmony_ciget_tag:
11288c2ecf20Sopenharmony_ci	clr	SCBPTR[1];
11298c2ecf20Sopenharmony_ci	mvi	SCBPTR	call inb_next;	/* tag value */
11308c2ecf20Sopenharmony_civerify_scb:
11318c2ecf20Sopenharmony_ci	test	SCB_CONTROL,DISCONNECTED jz verify_other_scb;
11328c2ecf20Sopenharmony_ci	mov	A, SAVED_SCSIID;
11338c2ecf20Sopenharmony_ci	cmp	SCB_SCSIID, A jne verify_other_scb;
11348c2ecf20Sopenharmony_ci	mov	A, SAVED_LUN;
11358c2ecf20Sopenharmony_ci	cmp	SCB_LUN, A je setup_SCB_disconnected;
11368c2ecf20Sopenharmony_civerify_other_scb:
11378c2ecf20Sopenharmony_ci	xor	SCBPTR[1], 1;
11388c2ecf20Sopenharmony_ci	test	SCBPTR[1], 0xFF jnz verify_scb;
11398c2ecf20Sopenharmony_ci	jmp	not_found;
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_ci/*
11428c2ecf20Sopenharmony_ci * Ensure that the SCB the tag points to is for
11438c2ecf20Sopenharmony_ci * an SCB transaction to the reconnecting target.
11448c2ecf20Sopenharmony_ci */
11458c2ecf20Sopenharmony_cisetup_SCB:
11468c2ecf20Sopenharmony_ci	if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
11478c2ecf20Sopenharmony_ci		or	SEQ_FLAGS, 0x10;
11488c2ecf20Sopenharmony_ci	}
11498c2ecf20Sopenharmony_ci	test	SCB_CONTROL,DISCONNECTED jz not_found;
11508c2ecf20Sopenharmony_cisetup_SCB_disconnected:
11518c2ecf20Sopenharmony_ci	and	SCB_CONTROL,~DISCONNECTED;
11528c2ecf20Sopenharmony_ci	clr	SEQ_FLAGS;	/* make note of IDENTIFY */
11538c2ecf20Sopenharmony_ci	test	SCB_SGPTR, SG_LIST_NULL jnz . + 3;
11548c2ecf20Sopenharmony_ci	bmov	ALLOCFIFO_SCBPTR, SCBPTR, 2;
11558c2ecf20Sopenharmony_ci	call	allocate_fifo;
11568c2ecf20Sopenharmony_ci	/* See if the host wants to send a message upon reconnection */
11578c2ecf20Sopenharmony_ci	test	SCB_CONTROL, MK_MESSAGE jz mesgin_done;
11588c2ecf20Sopenharmony_ci	mvi	HOST_MSG	call mk_mesg;
11598c2ecf20Sopenharmony_ci	jmp	mesgin_done;
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_cinot_found:
11628c2ecf20Sopenharmony_ci	SET_SEQINTCODE(NO_MATCH)
11638c2ecf20Sopenharmony_ci	jmp	mesgin_done;
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_cinot_found_ITloop:
11668c2ecf20Sopenharmony_ci	SET_SEQINTCODE(NO_MATCH)
11678c2ecf20Sopenharmony_ci	jmp	ITloop;
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_ci/*
11708c2ecf20Sopenharmony_ci * We received a "command complete" message.  Put the SCB on the complete
11718c2ecf20Sopenharmony_ci * queue and trigger a completion interrupt via the idle loop.  Before doing
11728c2ecf20Sopenharmony_ci * so, check to see if there is a residual or the status byte is something
11738c2ecf20Sopenharmony_ci * other than STATUS_GOOD (0).  In either of these conditions, we upload the
11748c2ecf20Sopenharmony_ci * SCB back to the host so it can process this information.
11758c2ecf20Sopenharmony_ci */
11768c2ecf20Sopenharmony_cimesgin_complete:
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci	/*
11798c2ecf20Sopenharmony_ci	 * If ATN is raised, we still want to give the target a message.
11808c2ecf20Sopenharmony_ci	 * Perhaps there was a parity error on this last message byte.
11818c2ecf20Sopenharmony_ci	 * Either way, the target should take us to message out phase
11828c2ecf20Sopenharmony_ci	 * and then attempt to complete the command again.  We should use a
11838c2ecf20Sopenharmony_ci	 * critical section here to guard against a timeout triggering
11848c2ecf20Sopenharmony_ci	 * for this command and setting ATN while we are still processing
11858c2ecf20Sopenharmony_ci	 * the completion.
11868c2ecf20Sopenharmony_ci	test	SCSISIGI, ATNI jnz mesgin_done;
11878c2ecf20Sopenharmony_ci	 */
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	/*
11908c2ecf20Sopenharmony_ci	 * If we are identified and have successfully sent the CDB,
11918c2ecf20Sopenharmony_ci	 * any status will do.  Optimize this fast path.
11928c2ecf20Sopenharmony_ci	 */
11938c2ecf20Sopenharmony_ci	test	SCB_CONTROL, STATUS_RCVD jz mesgin_proto_violation;
11948c2ecf20Sopenharmony_ci	test	SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT jz complete_accepted;
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	/*
11978c2ecf20Sopenharmony_ci	 * If the target never sent an identify message but instead went
11988c2ecf20Sopenharmony_ci	 * to mesgin to give an invalid message, let the host abort us.
11998c2ecf20Sopenharmony_ci	 */
12008c2ecf20Sopenharmony_ci	test	SEQ_FLAGS, NOT_IDENTIFIED jnz mesgin_proto_violation;
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	/*
12038c2ecf20Sopenharmony_ci	 * If we recevied good status but never successfully sent the
12048c2ecf20Sopenharmony_ci	 * cdb, abort the command.
12058c2ecf20Sopenharmony_ci	 */
12068c2ecf20Sopenharmony_ci	test	SCB_SCSI_STATUS,0xff	jnz complete_accepted;
12078c2ecf20Sopenharmony_ci	test	SEQ_FLAGS, NO_CDB_SENT jnz mesgin_proto_violation;
12088c2ecf20Sopenharmony_cicomplete_accepted:
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	/*
12118c2ecf20Sopenharmony_ci	 * See if we attempted to deliver a message but the target ingnored us.
12128c2ecf20Sopenharmony_ci	 */
12138c2ecf20Sopenharmony_ci	test	SCB_CONTROL, MK_MESSAGE jz complete_nomsg;
12148c2ecf20Sopenharmony_ci	SET_SEQINTCODE(MKMSG_FAILED)
12158c2ecf20Sopenharmony_cicomplete_nomsg:
12168c2ecf20Sopenharmony_ci	call	queue_scb_completion;
12178c2ecf20Sopenharmony_ci	jmp	await_busfree;
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ciBEGIN_CRITICAL;
12208c2ecf20Sopenharmony_cifreeze_queue:
12218c2ecf20Sopenharmony_ci	/* Cancel any pending select-out. */
12228c2ecf20Sopenharmony_ci	test	SSTAT0, SELDO|SELINGO jnz . + 2;
12238c2ecf20Sopenharmony_ci	and	SCSISEQ0, ~ENSELO;
12248c2ecf20Sopenharmony_ci	mov	ACCUM_SAVE, A;
12258c2ecf20Sopenharmony_ci	clr	A;
12268c2ecf20Sopenharmony_ci	add	QFREEZE_COUNT, 1;
12278c2ecf20Sopenharmony_ci	adc	QFREEZE_COUNT[1], A;
12288c2ecf20Sopenharmony_ci	or	SEQ_FLAGS2, SELECTOUT_QFROZEN;
12298c2ecf20Sopenharmony_ci	mov	A, ACCUM_SAVE ret;
12308c2ecf20Sopenharmony_ciEND_CRITICAL;
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci/*
12338c2ecf20Sopenharmony_ci * Complete the current FIFO's SCB if data for this same
12348c2ecf20Sopenharmony_ci * SCB is not transferring in the other FIFO.
12358c2ecf20Sopenharmony_ci */
12368c2ecf20Sopenharmony_ciSET_SRC_MODE	M_DFF1;
12378c2ecf20Sopenharmony_ciSET_DST_MODE	M_DFF1;
12388c2ecf20Sopenharmony_cipkt_complete_scb_if_fifos_idle:
12398c2ecf20Sopenharmony_ci	bmov	ARG_1, SCBPTR, 2;
12408c2ecf20Sopenharmony_ci	mvi	DFFSXFRCTL, CLRCHN;
12418c2ecf20Sopenharmony_ci	SET_MODE(M_SCSI, M_SCSI)
12428c2ecf20Sopenharmony_ci	bmov	SCBPTR, ARG_1, 2;
12438c2ecf20Sopenharmony_ci	test	SCB_FIFO_USE_COUNT, 0xFF jnz return;
12448c2ecf20Sopenharmony_ciqueue_scb_completion:
12458c2ecf20Sopenharmony_ci	test	SCB_SCSI_STATUS,0xff	jnz bad_status;
12468c2ecf20Sopenharmony_ci	/*
12478c2ecf20Sopenharmony_ci	 * Check for residuals
12488c2ecf20Sopenharmony_ci	 */
12498c2ecf20Sopenharmony_ci	test	SCB_SGPTR, SG_LIST_NULL jnz complete;	/* No xfer */
12508c2ecf20Sopenharmony_ci	test	SCB_SGPTR, SG_FULL_RESID jnz upload_scb;/* Never xfered */
12518c2ecf20Sopenharmony_ci	test	SCB_RESIDUAL_SGPTR, SG_LIST_NULL jz upload_scb;
12528c2ecf20Sopenharmony_cicomplete:
12538c2ecf20Sopenharmony_ciBEGIN_CRITICAL;
12548c2ecf20Sopenharmony_ci	bmov	SCB_NEXT_COMPLETE, COMPLETE_SCB_HEAD, 2;
12558c2ecf20Sopenharmony_ci	bmov	COMPLETE_SCB_HEAD, SCBPTR, 2 ret;
12568c2ecf20Sopenharmony_ciEND_CRITICAL;
12578c2ecf20Sopenharmony_cibad_status:
12588c2ecf20Sopenharmony_ci	cmp	SCB_SCSI_STATUS, STATUS_PKT_SENSE je upload_scb;
12598c2ecf20Sopenharmony_ci	call	freeze_queue;
12608c2ecf20Sopenharmony_ciupload_scb:
12618c2ecf20Sopenharmony_ci	/*
12628c2ecf20Sopenharmony_ci	 * Restore SCB TAG since we reuse this field
12638c2ecf20Sopenharmony_ci	 * in the sequencer.  We don't want to corrupt
12648c2ecf20Sopenharmony_ci	 * it on the host.
12658c2ecf20Sopenharmony_ci	 */
12668c2ecf20Sopenharmony_ci	bmov	SCB_TAG, SCBPTR, 2;
12678c2ecf20Sopenharmony_ciBEGIN_CRITICAL;
12688c2ecf20Sopenharmony_ci	or	SCB_SGPTR, SG_STATUS_VALID;
12698c2ecf20Sopenharmony_ci	mvi	SCB_NEXT_COMPLETE[1], SCB_LIST_NULL;
12708c2ecf20Sopenharmony_ci	cmp	COMPLETE_DMA_SCB_HEAD[1], SCB_LIST_NULL jne add_dma_scb_tail;
12718c2ecf20Sopenharmony_ci	bmov	COMPLETE_DMA_SCB_HEAD, SCBPTR, 2;
12728c2ecf20Sopenharmony_ci	bmov	COMPLETE_DMA_SCB_TAIL, SCBPTR, 2 ret;
12738c2ecf20Sopenharmony_ciadd_dma_scb_tail:
12748c2ecf20Sopenharmony_ci	bmov	REG0, SCBPTR, 2;
12758c2ecf20Sopenharmony_ci	bmov	SCBPTR, COMPLETE_DMA_SCB_TAIL, 2;
12768c2ecf20Sopenharmony_ci	bmov	SCB_NEXT_COMPLETE, REG0, 2;
12778c2ecf20Sopenharmony_ci	bmov	COMPLETE_DMA_SCB_TAIL, REG0, 2 ret;
12788c2ecf20Sopenharmony_ciEND_CRITICAL;
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci/*
12818c2ecf20Sopenharmony_ci * Is it a disconnect message?  Set a flag in the SCB to remind us
12828c2ecf20Sopenharmony_ci * and await the bus going free.  If this is an untagged transaction
12838c2ecf20Sopenharmony_ci * store the SCB id for it in our untagged target table for lookup on
12848c2ecf20Sopenharmony_ci * a reselection.
12858c2ecf20Sopenharmony_ci */
12868c2ecf20Sopenharmony_cimesgin_disconnect:
12878c2ecf20Sopenharmony_ci	/*
12888c2ecf20Sopenharmony_ci	 * If ATN is raised, we still want to give the target a message.
12898c2ecf20Sopenharmony_ci	 * Perhaps there was a parity error on this last message byte
12908c2ecf20Sopenharmony_ci	 * or we want to abort this command.  Either way, the target
12918c2ecf20Sopenharmony_ci	 * should take us to message out phase and then attempt to
12928c2ecf20Sopenharmony_ci	 * disconnect again.
12938c2ecf20Sopenharmony_ci	 * XXX - Wait for more testing.
12948c2ecf20Sopenharmony_ci	test	SCSISIGI, ATNI jnz mesgin_done;
12958c2ecf20Sopenharmony_ci	 */
12968c2ecf20Sopenharmony_ci	test	SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT
12978c2ecf20Sopenharmony_ci		jnz mesgin_proto_violation;
12988c2ecf20Sopenharmony_ci	or	SCB_CONTROL,DISCONNECTED;
12998c2ecf20Sopenharmony_ci	test	SCB_CONTROL, TAG_ENB jnz await_busfree;
13008c2ecf20Sopenharmony_ciqueue_disc_scb:
13018c2ecf20Sopenharmony_ci	bmov	REG0, SCBPTR, 2;
13028c2ecf20Sopenharmony_ci	INDEX_DISC_LIST(SAVED_SCSIID, SAVED_LUN);
13038c2ecf20Sopenharmony_ci	bmov	DINDEX, SINDEX, 2;
13048c2ecf20Sopenharmony_ci	bmov	DINDIR, REG0, 2;
13058c2ecf20Sopenharmony_ci	bmov	SCBPTR, REG0, 2;
13068c2ecf20Sopenharmony_ci	/* FALLTHROUGH */
13078c2ecf20Sopenharmony_ciawait_busfree:
13088c2ecf20Sopenharmony_ci	and	SIMODE1, ~ENBUSFREE;
13098c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_BUSFREEREV_BUG) == 0) {
13108c2ecf20Sopenharmony_ci		/*
13118c2ecf20Sopenharmony_ci		 * In the BUSFREEREV_BUG case, the
13128c2ecf20Sopenharmony_ci		 * busfree status was cleared at the
13138c2ecf20Sopenharmony_ci		 * beginning of the connection.
13148c2ecf20Sopenharmony_ci		 */
13158c2ecf20Sopenharmony_ci		mvi	CLRSINT1,CLRBUSFREE;
13168c2ecf20Sopenharmony_ci	}
13178c2ecf20Sopenharmony_ci	mov	NONE, SCSIDAT;		/* Ack the last byte */
13188c2ecf20Sopenharmony_ci	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
13198c2ecf20Sopenharmony_ci		jnz await_busfree_not_m_dff;
13208c2ecf20Sopenharmony_ciSET_SRC_MODE	M_DFF1;
13218c2ecf20Sopenharmony_ciSET_DST_MODE	M_DFF1;
13228c2ecf20Sopenharmony_ciawait_busfree_clrchn:
13238c2ecf20Sopenharmony_ci	mvi	DFFSXFRCTL, CLRCHN;
13248c2ecf20Sopenharmony_ciawait_busfree_not_m_dff:
13258c2ecf20Sopenharmony_ci	/* clear target specific flags */
13268c2ecf20Sopenharmony_ci	mvi	SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT;
13278c2ecf20Sopenharmony_ci	test	SSTAT1,REQINIT|BUSFREE	jz .;
13288c2ecf20Sopenharmony_ci	/*
13298c2ecf20Sopenharmony_ci	 * We only set BUSFREE status once either a new
13308c2ecf20Sopenharmony_ci	 * phase has been detected or we are really
13318c2ecf20Sopenharmony_ci	 * BUSFREE.  This allows the driver to know
13328c2ecf20Sopenharmony_ci	 * that we are active on the bus even though
13338c2ecf20Sopenharmony_ci	 * no identified transaction exists should a
13348c2ecf20Sopenharmony_ci	 * timeout occur while awaiting busfree.
13358c2ecf20Sopenharmony_ci	 */
13368c2ecf20Sopenharmony_ci	mvi	LASTPHASE, P_BUSFREE;
13378c2ecf20Sopenharmony_ci	test	SSTAT1, BUSFREE jnz idle_loop;
13388c2ecf20Sopenharmony_ci	SET_SEQINTCODE(MISSED_BUSFREE)
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci/*
13428c2ecf20Sopenharmony_ci * Save data pointers message:
13438c2ecf20Sopenharmony_ci * Copying RAM values back to SCB, for Save Data Pointers message, but
13448c2ecf20Sopenharmony_ci * only if we've actually been into a data phase to change them.  This
13458c2ecf20Sopenharmony_ci * protects against bogus data in scratch ram and the residual counts
13468c2ecf20Sopenharmony_ci * since they are only initialized when we go into data_in or data_out.
13478c2ecf20Sopenharmony_ci * Ack the message as soon as possible.
13488c2ecf20Sopenharmony_ci */
13498c2ecf20Sopenharmony_ciSET_SRC_MODE	M_DFF1;
13508c2ecf20Sopenharmony_ciSET_DST_MODE	M_DFF1;
13518c2ecf20Sopenharmony_cimesgin_sdptrs:
13528c2ecf20Sopenharmony_ci	mov	NONE,SCSIDAT;		/*dummy read from latch to ACK*/
13538c2ecf20Sopenharmony_ci	test	SEQ_FLAGS, DPHASE	jz ITloop;
13548c2ecf20Sopenharmony_ci	call	save_pointers;
13558c2ecf20Sopenharmony_ci	jmp	ITloop;
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_cisave_pointers:
13588c2ecf20Sopenharmony_ci	/*
13598c2ecf20Sopenharmony_ci	 * If we are asked to save our position at the end of the
13608c2ecf20Sopenharmony_ci	 * transfer, just mark us at the end rather than perform a
13618c2ecf20Sopenharmony_ci	 * full save.
13628c2ecf20Sopenharmony_ci	 */
13638c2ecf20Sopenharmony_ci	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz save_pointers_full;
13648c2ecf20Sopenharmony_ci	or	SCB_SGPTR, SG_LIST_NULL ret;
13658c2ecf20Sopenharmony_ci
13668c2ecf20Sopenharmony_cisave_pointers_full:
13678c2ecf20Sopenharmony_ci	/*
13688c2ecf20Sopenharmony_ci	 * The SCB_DATAPTR becomes the current SHADDR.
13698c2ecf20Sopenharmony_ci	 * All other information comes directly from our residual
13708c2ecf20Sopenharmony_ci	 * state.
13718c2ecf20Sopenharmony_ci	 */
13728c2ecf20Sopenharmony_ci	bmov	SCB_DATAPTR, SHADDR, 8;
13738c2ecf20Sopenharmony_ci	bmov	SCB_DATACNT, SCB_RESIDUAL_DATACNT, 8 ret;
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci/*
13768c2ecf20Sopenharmony_ci * Restore pointers message?  Data pointers are recopied from the
13778c2ecf20Sopenharmony_ci * SCB anytime we enter a data phase for the first time, so all
13788c2ecf20Sopenharmony_ci * we need to do is clear the DPHASE flag and let the data phase
13798c2ecf20Sopenharmony_ci * code do the rest.  We also reset/reallocate the FIFO to make
13808c2ecf20Sopenharmony_ci * sure we have a clean start for the next data or command phase.
13818c2ecf20Sopenharmony_ci */
13828c2ecf20Sopenharmony_cimesgin_rdptrs:
13838c2ecf20Sopenharmony_ci	and	SEQ_FLAGS, ~DPHASE;
13848c2ecf20Sopenharmony_ci	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1)) jnz msgin_rdptrs_get_fifo;
13858c2ecf20Sopenharmony_ci	mvi	DFFSXFRCTL, RSTCHN|CLRSHCNT;
13868c2ecf20Sopenharmony_ci	SET_MODE(M_SCSI, M_SCSI)
13878c2ecf20Sopenharmony_cimsgin_rdptrs_get_fifo:
13888c2ecf20Sopenharmony_ci	call	allocate_fifo;
13898c2ecf20Sopenharmony_ci	jmp	mesgin_done;
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_ciphase_lock:     
13928c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_EARLY_REQ_BUG) != 0) {
13938c2ecf20Sopenharmony_ci		/*
13948c2ecf20Sopenharmony_ci		 * Don't ignore persistent REQ assertions just because
13958c2ecf20Sopenharmony_ci		 * they were asserted within the bus settle delay window.
13968c2ecf20Sopenharmony_ci		 * This allows us to tolerate devices like the GEM318
13978c2ecf20Sopenharmony_ci		 * that violate the SCSI spec.  We are careful not to
13988c2ecf20Sopenharmony_ci		 * count REQ while we are waiting for it to fall during
13998c2ecf20Sopenharmony_ci		 * an async phase due to our asserted ACK.  Each
14008c2ecf20Sopenharmony_ci		 * sequencer instruction takes ~25ns, so the REQ must
14018c2ecf20Sopenharmony_ci		 * last at least 100ns in order to be counted as a true
14028c2ecf20Sopenharmony_ci		 * REQ.
14038c2ecf20Sopenharmony_ci		 */
14048c2ecf20Sopenharmony_ci		test	SCSIPHASE, 0xFF jnz phase_locked;
14058c2ecf20Sopenharmony_ci		test	SCSISIGI, ACKI jnz phase_lock;
14068c2ecf20Sopenharmony_ci		test	SCSISIGI, REQI jz phase_lock;
14078c2ecf20Sopenharmony_ci		test	SCSIPHASE, 0xFF jnz phase_locked;
14088c2ecf20Sopenharmony_ci		test	SCSISIGI, ACKI jnz phase_lock;
14098c2ecf20Sopenharmony_ci		test	SCSISIGI, REQI jz phase_lock;
14108c2ecf20Sopenharmony_ciphase_locked:
14118c2ecf20Sopenharmony_ci	} else {
14128c2ecf20Sopenharmony_ci		test	SCSIPHASE, 0xFF jz .;
14138c2ecf20Sopenharmony_ci	}
14148c2ecf20Sopenharmony_ci	test	SSTAT1, SCSIPERR jnz phase_lock;
14158c2ecf20Sopenharmony_ciphase_lock_latch_phase:
14168c2ecf20Sopenharmony_ci	and	LASTPHASE, PHASE_MASK, SCSISIGI ret;
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci/*
14198c2ecf20Sopenharmony_ci * Functions to read data in Automatic PIO mode.
14208c2ecf20Sopenharmony_ci *
14218c2ecf20Sopenharmony_ci * An ACK is not sent on input from the target until SCSIDATL is read from.
14228c2ecf20Sopenharmony_ci * So we wait until SCSIDATL is latched (the usual way), then read the data
14238c2ecf20Sopenharmony_ci * byte directly off the bus using SCSIBUSL.  When we have pulled the ATN
14248c2ecf20Sopenharmony_ci * line, or we just want to acknowledge the byte, then we do a dummy read
14258c2ecf20Sopenharmony_ci * from SCISDATL.  The SCSI spec guarantees that the target will hold the
14268c2ecf20Sopenharmony_ci * data byte on the bus until we send our ACK.
14278c2ecf20Sopenharmony_ci *
14288c2ecf20Sopenharmony_ci * The assumption here is that these are called in a particular sequence,
14298c2ecf20Sopenharmony_ci * and that REQ is already set when inb_first is called.  inb_{first,next}
14308c2ecf20Sopenharmony_ci * use the same calling convention as inb.
14318c2ecf20Sopenharmony_ci */
14328c2ecf20Sopenharmony_ciinb_next:
14338c2ecf20Sopenharmony_ci	mov	NONE,SCSIDAT;		/*dummy read from latch to ACK*/
14348c2ecf20Sopenharmony_ciinb_next_wait:
14358c2ecf20Sopenharmony_ci	/*
14368c2ecf20Sopenharmony_ci	 * If there is a parity error, wait for the kernel to
14378c2ecf20Sopenharmony_ci	 * see the interrupt and prepare our message response
14388c2ecf20Sopenharmony_ci	 * before continuing.
14398c2ecf20Sopenharmony_ci	 */
14408c2ecf20Sopenharmony_ci	test	SCSIPHASE, 0xFF jz .;
14418c2ecf20Sopenharmony_ci	test	SSTAT1, SCSIPERR jnz inb_next_wait;
14428c2ecf20Sopenharmony_ciinb_next_check_phase:
14438c2ecf20Sopenharmony_ci	and	LASTPHASE, PHASE_MASK, SCSISIGI;
14448c2ecf20Sopenharmony_ci	cmp	LASTPHASE, P_MESGIN jne mesgin_phasemis;
14458c2ecf20Sopenharmony_ciinb_first:
14468c2ecf20Sopenharmony_ci	clr	DINDEX[1];
14478c2ecf20Sopenharmony_ci	mov	DINDEX,SINDEX;
14488c2ecf20Sopenharmony_ci	mov	DINDIR,SCSIBUS	ret;		/*read byte directly from bus*/
14498c2ecf20Sopenharmony_ciinb_last:
14508c2ecf20Sopenharmony_ci	mov	NONE,SCSIDAT ret;		/*dummy read from latch to ACK*/
14518c2ecf20Sopenharmony_ci
14528c2ecf20Sopenharmony_cimk_mesg:
14538c2ecf20Sopenharmony_ci	mvi	SCSISIGO, ATNO;
14548c2ecf20Sopenharmony_ci	mov	MSG_OUT,SINDEX ret;
14558c2ecf20Sopenharmony_ci
14568c2ecf20Sopenharmony_ciSET_SRC_MODE	M_DFF1;
14578c2ecf20Sopenharmony_ciSET_DST_MODE	M_DFF1;
14588c2ecf20Sopenharmony_cidisable_ccsgen:
14598c2ecf20Sopenharmony_ci	test	SG_STATE, FETCH_INPROG jz disable_ccsgen_fetch_done;
14608c2ecf20Sopenharmony_ci	clr	CCSGCTL;
14618c2ecf20Sopenharmony_cidisable_ccsgen_fetch_done:
14628c2ecf20Sopenharmony_ci	clr	SG_STATE ret;
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ciservice_fifo:
14658c2ecf20Sopenharmony_ci	/*
14668c2ecf20Sopenharmony_ci	 * Do we have any prefetch left???
14678c2ecf20Sopenharmony_ci	 */
14688c2ecf20Sopenharmony_ci	test	SG_STATE, SEGS_AVAIL jnz idle_sg_avail;
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	/*
14718c2ecf20Sopenharmony_ci	 * Can this FIFO have access to the S/G cache yet?
14728c2ecf20Sopenharmony_ci	 */
14738c2ecf20Sopenharmony_ci	test	CCSGCTL, SG_CACHE_AVAIL jz return;
14748c2ecf20Sopenharmony_ci
14758c2ecf20Sopenharmony_ci	/* Did we just finish fetching segs? */
14768c2ecf20Sopenharmony_ci	test	CCSGCTL, CCSGDONE jnz idle_sgfetch_complete;
14778c2ecf20Sopenharmony_ci
14788c2ecf20Sopenharmony_ci	/* Are we actively fetching segments? */
14798c2ecf20Sopenharmony_ci	test	CCSGCTL, CCSGENACK jnz return;
14808c2ecf20Sopenharmony_ci
14818c2ecf20Sopenharmony_ci	/*
14828c2ecf20Sopenharmony_ci	 * Should the other FIFO get the S/G cache first?  If
14838c2ecf20Sopenharmony_ci	 * both FIFOs have been allocated since we last checked
14848c2ecf20Sopenharmony_ci	 * any FIFO, it is important that we service a FIFO
14858c2ecf20Sopenharmony_ci	 * that is not actively on the bus first.  This guarantees
14868c2ecf20Sopenharmony_ci	 * that a FIFO will be freed to handle snapshot requests for
14878c2ecf20Sopenharmony_ci	 * any FIFO that is still on the bus.  Chips with RTI do not
14888c2ecf20Sopenharmony_ci	 * perform snapshots, so don't bother with this test there.
14898c2ecf20Sopenharmony_ci	 */
14908c2ecf20Sopenharmony_ci	if ((ahd->features & AHD_RTI) == 0) {
14918c2ecf20Sopenharmony_ci		/*
14928c2ecf20Sopenharmony_ci		 * If we're not still receiving SCSI data,
14938c2ecf20Sopenharmony_ci		 * it is safe to allocate the S/G cache to
14948c2ecf20Sopenharmony_ci		 * this FIFO.
14958c2ecf20Sopenharmony_ci		 */
14968c2ecf20Sopenharmony_ci		test	DFCNTRL, SCSIEN jz idle_sgfetch_start;
14978c2ecf20Sopenharmony_ci
14988c2ecf20Sopenharmony_ci		/*
14998c2ecf20Sopenharmony_ci		 * Switch to the other FIFO.  Non-RTI chips
15008c2ecf20Sopenharmony_ci		 * also have the "set mode" bug, so we must
15018c2ecf20Sopenharmony_ci		 * disable interrupts during the switch.
15028c2ecf20Sopenharmony_ci		 */
15038c2ecf20Sopenharmony_ci		mvi	SEQINTCTL, INTVEC1DSL;
15048c2ecf20Sopenharmony_ci		xor	MODE_PTR, MK_MODE(M_DFF1, M_DFF1);
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci		/*
15078c2ecf20Sopenharmony_ci		 * If the other FIFO needs loading, then it
15088c2ecf20Sopenharmony_ci		 * must not have claimed the S/G cache yet
15098c2ecf20Sopenharmony_ci		 * (SG_CACHE_AVAIL would have been cleared in
15108c2ecf20Sopenharmony_ci		 * the original FIFO mode and we test this above).
15118c2ecf20Sopenharmony_ci		 * Return to the idle loop so we can process the
15128c2ecf20Sopenharmony_ci		 * FIFO not currently on the bus first.
15138c2ecf20Sopenharmony_ci		 */
15148c2ecf20Sopenharmony_ci		test	SG_STATE, LOADING_NEEDED jz idle_sgfetch_okay;
15158c2ecf20Sopenharmony_ci		clr	SEQINTCTL ret;
15168c2ecf20Sopenharmony_ciidle_sgfetch_okay:
15178c2ecf20Sopenharmony_ci		xor	MODE_PTR, MK_MODE(M_DFF1, M_DFF1);
15188c2ecf20Sopenharmony_ci		clr	SEQINTCTL;
15198c2ecf20Sopenharmony_ci	}
15208c2ecf20Sopenharmony_ci
15218c2ecf20Sopenharmony_ciidle_sgfetch_start:
15228c2ecf20Sopenharmony_ci	/*
15238c2ecf20Sopenharmony_ci	 * We fetch a "cacheline aligned" and sized amount of data
15248c2ecf20Sopenharmony_ci	 * so we don't end up referencing a non-existent page.
15258c2ecf20Sopenharmony_ci	 * Cacheline aligned is in quotes because the kernel will
15268c2ecf20Sopenharmony_ci	 * set the prefetch amount to a reasonable level if the
15278c2ecf20Sopenharmony_ci	 * cacheline size is unknown.
15288c2ecf20Sopenharmony_ci	 */
15298c2ecf20Sopenharmony_ci	bmov	SGHADDR, SCB_RESIDUAL_SGPTR, 4;
15308c2ecf20Sopenharmony_ci	mvi	SGHCNT, SG_PREFETCH_CNT;
15318c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_REG_SLOW_SETTLE_BUG) != 0) {
15328c2ecf20Sopenharmony_ci		/*
15338c2ecf20Sopenharmony_ci		 * Need two instructions between "touches" of SGHADDR.
15348c2ecf20Sopenharmony_ci		 */
15358c2ecf20Sopenharmony_ci		nop;
15368c2ecf20Sopenharmony_ci	}
15378c2ecf20Sopenharmony_ci	and	SGHADDR[0], SG_PREFETCH_ALIGN_MASK, SCB_RESIDUAL_SGPTR;
15388c2ecf20Sopenharmony_ci	mvi	CCSGCTL, CCSGEN|CCSGRESET;
15398c2ecf20Sopenharmony_ci	or	SG_STATE, FETCH_INPROG ret;
15408c2ecf20Sopenharmony_ciidle_sgfetch_complete:
15418c2ecf20Sopenharmony_ci	/*
15428c2ecf20Sopenharmony_ci	 * Guard against SG_CACHE_AVAIL activating during sg fetch
15438c2ecf20Sopenharmony_ci	 * request in the other FIFO.
15448c2ecf20Sopenharmony_ci	 */
15458c2ecf20Sopenharmony_ci	test	SG_STATE, FETCH_INPROG jz return;
15468c2ecf20Sopenharmony_ci	clr	CCSGCTL;
15478c2ecf20Sopenharmony_ci	and	CCSGADDR, SG_PREFETCH_ADDR_MASK, SCB_RESIDUAL_SGPTR;
15488c2ecf20Sopenharmony_ci	mvi	SG_STATE, SEGS_AVAIL|LOADING_NEEDED;
15498c2ecf20Sopenharmony_ciidle_sg_avail:
15508c2ecf20Sopenharmony_ci	/* Does the hardware have space for another SG entry? */
15518c2ecf20Sopenharmony_ci	test	DFSTATUS, PRELOAD_AVAIL jz return;
15528c2ecf20Sopenharmony_ci	/*
15538c2ecf20Sopenharmony_ci	 * On the A, preloading a segment before HDMAENACK
15548c2ecf20Sopenharmony_ci	 * comes true can clobber the shadow address of the
15558c2ecf20Sopenharmony_ci	 * first segment in the S/G FIFO.  Wait until it is
15568c2ecf20Sopenharmony_ci	 * safe to proceed.
15578c2ecf20Sopenharmony_ci	 */
15588c2ecf20Sopenharmony_ci	if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) == 0) {
15598c2ecf20Sopenharmony_ci		test	DFCNTRL, HDMAENACK jz return;
15608c2ecf20Sopenharmony_ci	}
15618c2ecf20Sopenharmony_ci	if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
15628c2ecf20Sopenharmony_ci		bmov	HADDR, CCSGRAM, 8;
15638c2ecf20Sopenharmony_ci	} else {
15648c2ecf20Sopenharmony_ci		bmov 	HADDR, CCSGRAM, 4;
15658c2ecf20Sopenharmony_ci	}
15668c2ecf20Sopenharmony_ci	bmov	HCNT, CCSGRAM, 3;
15678c2ecf20Sopenharmony_ci	bmov	SCB_RESIDUAL_DATACNT[3], CCSGRAM, 1;
15688c2ecf20Sopenharmony_ci	if ((ahd->flags & AHD_39BIT_ADDRESSING) != 0) {
15698c2ecf20Sopenharmony_ci		and	HADDR[4], SG_HIGH_ADDR_BITS, SCB_RESIDUAL_DATACNT[3];
15708c2ecf20Sopenharmony_ci	}
15718c2ecf20Sopenharmony_ci	if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
15728c2ecf20Sopenharmony_ci		/* Skip 4 bytes of pad. */
15738c2ecf20Sopenharmony_ci		add	CCSGADDR, 4;
15748c2ecf20Sopenharmony_ci	}
15758c2ecf20Sopenharmony_cisg_advance:
15768c2ecf20Sopenharmony_ci	clr	A;			/* add sizeof(struct scatter) */
15778c2ecf20Sopenharmony_ci	add	SCB_RESIDUAL_SGPTR[0],SG_SIZEOF;
15788c2ecf20Sopenharmony_ci	adc	SCB_RESIDUAL_SGPTR[1],A;
15798c2ecf20Sopenharmony_ci	adc	SCB_RESIDUAL_SGPTR[2],A;
15808c2ecf20Sopenharmony_ci	adc	SCB_RESIDUAL_SGPTR[3],A;
15818c2ecf20Sopenharmony_ci	mov	SINDEX, SCB_RESIDUAL_SGPTR[0];
15828c2ecf20Sopenharmony_ci	test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz . + 3;
15838c2ecf20Sopenharmony_ci	or	SINDEX, LAST_SEG;
15848c2ecf20Sopenharmony_ci	clr	SG_STATE;
15858c2ecf20Sopenharmony_ci	mov	SG_CACHE_PRE, SINDEX;
15868c2ecf20Sopenharmony_ci	if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) != 0) {
15878c2ecf20Sopenharmony_ci		/*
15888c2ecf20Sopenharmony_ci		 * Use SCSIENWRDIS so that SCSIEN is never
15898c2ecf20Sopenharmony_ci		 * modified by this operation.
15908c2ecf20Sopenharmony_ci		 */
15918c2ecf20Sopenharmony_ci		or	DFCNTRL, PRELOADEN|HDMAEN|SCSIENWRDIS;
15928c2ecf20Sopenharmony_ci	} else {
15938c2ecf20Sopenharmony_ci		or	DFCNTRL, PRELOADEN|HDMAEN;
15948c2ecf20Sopenharmony_ci	}
15958c2ecf20Sopenharmony_ci	/*
15968c2ecf20Sopenharmony_ci	 * Do we have another segment in the cache?
15978c2ecf20Sopenharmony_ci	 */
15988c2ecf20Sopenharmony_ci	add	NONE, SG_PREFETCH_CNT_LIMIT, CCSGADDR;
15998c2ecf20Sopenharmony_ci	jnc	return;
16008c2ecf20Sopenharmony_ci	and	SG_STATE, ~SEGS_AVAIL ret;
16018c2ecf20Sopenharmony_ci
16028c2ecf20Sopenharmony_ci/*
16038c2ecf20Sopenharmony_ci * Initialize the DMA address and counter from the SCB.
16048c2ecf20Sopenharmony_ci */
16058c2ecf20Sopenharmony_ciload_first_seg:
16068c2ecf20Sopenharmony_ci	bmov	HADDR, SCB_DATAPTR, 11;
16078c2ecf20Sopenharmony_ci	and	REG_ISR, ~SG_FULL_RESID, SCB_SGPTR[0];
16088c2ecf20Sopenharmony_ci	test	SCB_DATACNT[3], SG_LAST_SEG jz . + 2;
16098c2ecf20Sopenharmony_ci	or	REG_ISR, LAST_SEG;
16108c2ecf20Sopenharmony_ci	mov	SG_CACHE_PRE, REG_ISR;
16118c2ecf20Sopenharmony_ci	mvi	DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN);
16128c2ecf20Sopenharmony_ci	/*
16138c2ecf20Sopenharmony_ci	 * Since we've are entering a data phase, we will
16148c2ecf20Sopenharmony_ci	 * rely on the SCB_RESID* fields.  Initialize the
16158c2ecf20Sopenharmony_ci	 * residual and clear the full residual flag.
16168c2ecf20Sopenharmony_ci	 */
16178c2ecf20Sopenharmony_ci	and	SCB_SGPTR[0], ~SG_FULL_RESID;
16188c2ecf20Sopenharmony_ci	bmov	SCB_RESIDUAL_DATACNT[3], SCB_DATACNT[3], 5;
16198c2ecf20Sopenharmony_ci	/* If we need more S/G elements, tell the idle loop */
16208c2ecf20Sopenharmony_ci	test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jnz . + 2;
16218c2ecf20Sopenharmony_ci	mvi	SG_STATE, LOADING_NEEDED ret;
16228c2ecf20Sopenharmony_ci	clr	SG_STATE ret;
16238c2ecf20Sopenharmony_ci
16248c2ecf20Sopenharmony_cip_data_handle_xfer:
16258c2ecf20Sopenharmony_ci	call	setjmp;
16268c2ecf20Sopenharmony_ci	test	SG_STATE, LOADING_NEEDED jnz service_fifo;
16278c2ecf20Sopenharmony_cip_data_clear_handler:
16288c2ecf20Sopenharmony_ci	or	LONGJMP_ADDR[1], INVALID_ADDR ret;
16298c2ecf20Sopenharmony_ci
16308c2ecf20Sopenharmony_cip_data:
16318c2ecf20Sopenharmony_ci	test	SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT	jz p_data_allowed;
16328c2ecf20Sopenharmony_ci	SET_SEQINTCODE(PROTO_VIOLATION)
16338c2ecf20Sopenharmony_cip_data_allowed:
16348c2ecf20Sopenharmony_ci 
16358c2ecf20Sopenharmony_ci	test	SEQ_FLAGS, DPHASE	jz data_phase_initialize;
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_ci	/*
16388c2ecf20Sopenharmony_ci	 * If we re-enter the data phase after going through another
16398c2ecf20Sopenharmony_ci	 * phase, our transfer location has almost certainly been
16408c2ecf20Sopenharmony_ci	 * corrupted by the interveining, non-data, transfers.  Ask
16418c2ecf20Sopenharmony_ci	 * the host driver to fix us up based on the transfer residual
16428c2ecf20Sopenharmony_ci	 * unless we already know that we should be bitbucketing.
16438c2ecf20Sopenharmony_ci	 */
16448c2ecf20Sopenharmony_ci	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jnz p_data_bitbucket;
16458c2ecf20Sopenharmony_ci	SET_SEQINTCODE(PDATA_REINIT)
16468c2ecf20Sopenharmony_ci	jmp	data_phase_inbounds;
16478c2ecf20Sopenharmony_ci
16488c2ecf20Sopenharmony_cip_data_bitbucket:
16498c2ecf20Sopenharmony_ci	/*
16508c2ecf20Sopenharmony_ci	 * Turn on `Bit Bucket' mode, wait until the target takes
16518c2ecf20Sopenharmony_ci	 * us to another phase, and then notify the host.
16528c2ecf20Sopenharmony_ci	 */
16538c2ecf20Sopenharmony_ci	mov	SAVED_MODE, MODE_PTR;
16548c2ecf20Sopenharmony_ci	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
16558c2ecf20Sopenharmony_ci		jnz bitbucket_not_m_dff;
16568c2ecf20Sopenharmony_ci	/*
16578c2ecf20Sopenharmony_ci	 * Ensure that any FIFO contents are cleared out and the
16588c2ecf20Sopenharmony_ci	 * FIFO free'd prior to starting the BITBUCKET.  BITBUCKET
16598c2ecf20Sopenharmony_ci	 * doesn't discard data already in the FIFO.
16608c2ecf20Sopenharmony_ci	 */
16618c2ecf20Sopenharmony_ci	mvi	DFFSXFRCTL, RSTCHN|CLRSHCNT;
16628c2ecf20Sopenharmony_ci	SET_MODE(M_SCSI, M_SCSI)
16638c2ecf20Sopenharmony_cibitbucket_not_m_dff:
16648c2ecf20Sopenharmony_ci	or	SXFRCTL1,BITBUCKET;
16658c2ecf20Sopenharmony_ci	/* Wait for non-data phase. */
16668c2ecf20Sopenharmony_ci	test	SCSIPHASE, ~DATA_PHASE_MASK jz .;
16678c2ecf20Sopenharmony_ci	and	SXFRCTL1, ~BITBUCKET;
16688c2ecf20Sopenharmony_ci	RESTORE_MODE(SAVED_MODE)
16698c2ecf20Sopenharmony_ciSET_SRC_MODE	M_DFF1;
16708c2ecf20Sopenharmony_ciSET_DST_MODE	M_DFF1;
16718c2ecf20Sopenharmony_ci	SET_SEQINTCODE(DATA_OVERRUN)
16728c2ecf20Sopenharmony_ci	jmp	ITloop;
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_cidata_phase_initialize:
16758c2ecf20Sopenharmony_ci	test	SCB_SGPTR[0], SG_LIST_NULL jnz p_data_bitbucket;
16768c2ecf20Sopenharmony_ci	call	load_first_seg;
16778c2ecf20Sopenharmony_cidata_phase_inbounds:
16788c2ecf20Sopenharmony_ci	/* We have seen a data phase at least once. */
16798c2ecf20Sopenharmony_ci	or	SEQ_FLAGS, DPHASE;
16808c2ecf20Sopenharmony_ci	mov	SAVED_MODE, MODE_PTR;
16818c2ecf20Sopenharmony_ci	test	SG_STATE, LOADING_NEEDED jz data_group_dma_loop;
16828c2ecf20Sopenharmony_ci	call	p_data_handle_xfer;
16838c2ecf20Sopenharmony_cidata_group_dma_loop:
16848c2ecf20Sopenharmony_ci	/*
16858c2ecf20Sopenharmony_ci	 * The transfer is complete if either the last segment
16868c2ecf20Sopenharmony_ci	 * completes or the target changes phase.  Both conditions
16878c2ecf20Sopenharmony_ci	 * will clear SCSIEN.
16888c2ecf20Sopenharmony_ci	 */
16898c2ecf20Sopenharmony_ci	call	idle_loop_service_fifos;
16908c2ecf20Sopenharmony_ci	call	idle_loop_cchan;
16918c2ecf20Sopenharmony_ci	call	idle_loop_gsfifo;
16928c2ecf20Sopenharmony_ci	RESTORE_MODE(SAVED_MODE)
16938c2ecf20Sopenharmony_ci	test	DFCNTRL, SCSIEN jnz data_group_dma_loop;
16948c2ecf20Sopenharmony_ci
16958c2ecf20Sopenharmony_cidata_group_dmafinish:
16968c2ecf20Sopenharmony_ci	/*
16978c2ecf20Sopenharmony_ci	 * The transfer has terminated either due to a phase
16988c2ecf20Sopenharmony_ci	 * change, and/or the completion of the last segment.
16998c2ecf20Sopenharmony_ci	 * We have two goals here.  Do as much other work
17008c2ecf20Sopenharmony_ci	 * as possible while the data fifo drains on a read
17018c2ecf20Sopenharmony_ci	 * and respond as quickly as possible to the standard
17028c2ecf20Sopenharmony_ci	 * messages (save data pointers/disconnect and command
17038c2ecf20Sopenharmony_ci	 * complete) that usually follow a data phase.
17048c2ecf20Sopenharmony_ci	 */
17058c2ecf20Sopenharmony_ci	call	calc_residual;
17068c2ecf20Sopenharmony_ci
17078c2ecf20Sopenharmony_ci	/*
17088c2ecf20Sopenharmony_ci	 * Go ahead and shut down the DMA engine now.
17098c2ecf20Sopenharmony_ci	 */
17108c2ecf20Sopenharmony_ci	test	DFCNTRL, DIRECTION jnz data_phase_finish;
17118c2ecf20Sopenharmony_cidata_group_fifoflush:
17128c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
17138c2ecf20Sopenharmony_ci		or	DFCNTRL, FIFOFLUSH;
17148c2ecf20Sopenharmony_ci	}
17158c2ecf20Sopenharmony_ci	/*
17168c2ecf20Sopenharmony_ci	 * We have enabled the auto-ack feature.  This means
17178c2ecf20Sopenharmony_ci	 * that the controller may have already transferred
17188c2ecf20Sopenharmony_ci	 * some overrun bytes into the data FIFO and acked them
17198c2ecf20Sopenharmony_ci	 * on the bus.  The only way to detect this situation is
17208c2ecf20Sopenharmony_ci	 * to wait for LAST_SEG_DONE to come true on a completed
17218c2ecf20Sopenharmony_ci	 * transfer and then test to see if the data FIFO is
17228c2ecf20Sopenharmony_ci	 * non-empty.  We know there is more data yet to transfer
17238c2ecf20Sopenharmony_ci	 * if SG_LIST_NULL is not yet set, thus there cannot be
17248c2ecf20Sopenharmony_ci	 * an overrun.
17258c2ecf20Sopenharmony_ci	 */
17268c2ecf20Sopenharmony_ci	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz data_phase_finish;
17278c2ecf20Sopenharmony_ci	test	SG_CACHE_SHADOW, LAST_SEG_DONE jz .;
17288c2ecf20Sopenharmony_ci	test	DFSTATUS, FIFOEMP jnz data_phase_finish;
17298c2ecf20Sopenharmony_ci	/* Overrun */
17308c2ecf20Sopenharmony_ci	jmp	p_data;
17318c2ecf20Sopenharmony_cidata_phase_finish:
17328c2ecf20Sopenharmony_ci	/*
17338c2ecf20Sopenharmony_ci	 * If the target has left us in data phase, loop through
17348c2ecf20Sopenharmony_ci	 * the dma code again.  We will only loop if there is a
17358c2ecf20Sopenharmony_ci	 * data overrun.  
17368c2ecf20Sopenharmony_ci	 */
17378c2ecf20Sopenharmony_ci	if ((ahd->flags & AHD_TARGETROLE) != 0) {
17388c2ecf20Sopenharmony_ci		test	SSTAT0, TARGET jnz data_phase_done;
17398c2ecf20Sopenharmony_ci	}
17408c2ecf20Sopenharmony_ci	if ((ahd->flags & AHD_INITIATORROLE) != 0) {
17418c2ecf20Sopenharmony_ci		test	SSTAT1, REQINIT jz .;
17428c2ecf20Sopenharmony_ci		test	SCSIPHASE, DATA_PHASE_MASK jnz p_data;
17438c2ecf20Sopenharmony_ci	}
17448c2ecf20Sopenharmony_ci
17458c2ecf20Sopenharmony_cidata_phase_done:
17468c2ecf20Sopenharmony_ci	/* Kill off any pending prefetch */
17478c2ecf20Sopenharmony_ci	call	disable_ccsgen;
17488c2ecf20Sopenharmony_ci	or 	LONGJMP_ADDR[1], INVALID_ADDR;
17498c2ecf20Sopenharmony_ci
17508c2ecf20Sopenharmony_ci	if ((ahd->flags & AHD_TARGETROLE) != 0) {
17518c2ecf20Sopenharmony_ci		test	SEQ_FLAGS, DPHASE_PENDING jz ITloop;
17528c2ecf20Sopenharmony_ci		/*
17538c2ecf20Sopenharmony_ci		and	SEQ_FLAGS, ~DPHASE_PENDING;
17548c2ecf20Sopenharmony_ci		 * For data-in phases, wait for any pending acks from the
17558c2ecf20Sopenharmony_ci		 * initiator before changing phase.  We only need to
17568c2ecf20Sopenharmony_ci		 * send Ignore Wide Residue messages for data-in phases.
17578c2ecf20Sopenharmony_ci		test	DFCNTRL, DIRECTION jz target_ITloop;
17588c2ecf20Sopenharmony_ci		test	SSTAT1, REQINIT	jnz .;
17598c2ecf20Sopenharmony_ci		test	SCB_TASK_ATTRIBUTE, SCB_XFERLEN_ODD jz target_ITloop;
17608c2ecf20Sopenharmony_ci		SET_MODE(M_SCSI, M_SCSI)
17618c2ecf20Sopenharmony_ci		test	NEGCONOPTS, WIDEXFER jz target_ITloop;
17628c2ecf20Sopenharmony_ci		 */
17638c2ecf20Sopenharmony_ci		/*
17648c2ecf20Sopenharmony_ci		 * Issue an Ignore Wide Residue Message.
17658c2ecf20Sopenharmony_ci		mvi	P_MESGIN|BSYO call change_phase;
17668c2ecf20Sopenharmony_ci		mvi	MSG_IGN_WIDE_RESIDUE call target_outb;
17678c2ecf20Sopenharmony_ci		mvi	1 call target_outb;
17688c2ecf20Sopenharmony_ci		jmp	target_ITloop;
17698c2ecf20Sopenharmony_ci		 */
17708c2ecf20Sopenharmony_ci	} else {
17718c2ecf20Sopenharmony_ci		jmp	ITloop;
17728c2ecf20Sopenharmony_ci	}
17738c2ecf20Sopenharmony_ci
17748c2ecf20Sopenharmony_ci/*
17758c2ecf20Sopenharmony_ci * We assume that, even though data may still be
17768c2ecf20Sopenharmony_ci * transferring to the host, that the SCSI side of
17778c2ecf20Sopenharmony_ci * the DMA engine is now in a static state.  This
17788c2ecf20Sopenharmony_ci * allows us to update our notion of where we are
17798c2ecf20Sopenharmony_ci * in this transfer.
17808c2ecf20Sopenharmony_ci *
17818c2ecf20Sopenharmony_ci * If, by chance, we stopped before being able
17828c2ecf20Sopenharmony_ci * to fetch additional segments for this transfer,
17838c2ecf20Sopenharmony_ci * yet the last S/G was completely exhausted,
17848c2ecf20Sopenharmony_ci * call our idle loop until it is able to load
17858c2ecf20Sopenharmony_ci * another segment.  This will allow us to immediately
17868c2ecf20Sopenharmony_ci * pickup on the next segment on the next data phase.
17878c2ecf20Sopenharmony_ci *
17888c2ecf20Sopenharmony_ci * If we happened to stop on the last segment, then
17898c2ecf20Sopenharmony_ci * our residual information is still correct from
17908c2ecf20Sopenharmony_ci * the idle loop and there is no need to perform
17918c2ecf20Sopenharmony_ci * any fixups.
17928c2ecf20Sopenharmony_ci */
17938c2ecf20Sopenharmony_ciresidual_before_last_seg:
17948c2ecf20Sopenharmony_ci	test    MDFFSTAT, SHVALID	jnz sgptr_fixup;
17958c2ecf20Sopenharmony_ci	/*
17968c2ecf20Sopenharmony_ci	 * Can never happen from an interrupt as the packetized
17978c2ecf20Sopenharmony_ci	 * hardware will only interrupt us once SHVALID or
17988c2ecf20Sopenharmony_ci	 * LAST_SEG_DONE.
17998c2ecf20Sopenharmony_ci	 */
18008c2ecf20Sopenharmony_ci	call	idle_loop_service_fifos;
18018c2ecf20Sopenharmony_ci	RESTORE_MODE(SAVED_MODE)
18028c2ecf20Sopenharmony_ci	/* FALLTHROUGH */
18038c2ecf20Sopenharmony_cicalc_residual:
18048c2ecf20Sopenharmony_ci	test	SG_CACHE_SHADOW, LAST_SEG jz residual_before_last_seg;
18058c2ecf20Sopenharmony_ci	/* Record if we've consumed all S/G entries */
18068c2ecf20Sopenharmony_ci	test	MDFFSTAT, SHVALID	jz . + 2;
18078c2ecf20Sopenharmony_ci	bmov	SCB_RESIDUAL_DATACNT, SHCNT, 3 ret;
18088c2ecf20Sopenharmony_ci	or	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL ret;
18098c2ecf20Sopenharmony_ci
18108c2ecf20Sopenharmony_cisgptr_fixup:
18118c2ecf20Sopenharmony_ci	/*
18128c2ecf20Sopenharmony_ci	 * Fixup the residual next S/G pointer.  The S/G preload
18138c2ecf20Sopenharmony_ci	 * feature of the chip allows us to load two elements
18148c2ecf20Sopenharmony_ci	 * in addition to the currently active element.  We
18158c2ecf20Sopenharmony_ci	 * store the bottom byte of the next S/G pointer in
18168c2ecf20Sopenharmony_ci	 * the SG_CACHE_PTR register so we can restore the
18178c2ecf20Sopenharmony_ci	 * correct value when the DMA completes.  If the next
18188c2ecf20Sopenharmony_ci	 * sg ptr value has advanced to the point where higher
18198c2ecf20Sopenharmony_ci	 * bytes in the address have been affected, fix them
18208c2ecf20Sopenharmony_ci	 * too.
18218c2ecf20Sopenharmony_ci	 */
18228c2ecf20Sopenharmony_ci	test	SG_CACHE_SHADOW, 0x80 jz sgptr_fixup_done;
18238c2ecf20Sopenharmony_ci	test	SCB_RESIDUAL_SGPTR[0], 0x80 jnz sgptr_fixup_done;
18248c2ecf20Sopenharmony_ci	add	SCB_RESIDUAL_SGPTR[1], -1;
18258c2ecf20Sopenharmony_ci	adc	SCB_RESIDUAL_SGPTR[2], -1; 
18268c2ecf20Sopenharmony_ci	adc	SCB_RESIDUAL_SGPTR[3], -1;
18278c2ecf20Sopenharmony_cisgptr_fixup_done:
18288c2ecf20Sopenharmony_ci	and	SCB_RESIDUAL_SGPTR[0], SG_ADDR_MASK, SG_CACHE_SHADOW;
18298c2ecf20Sopenharmony_ci	clr	SCB_RESIDUAL_DATACNT[3]; /* We are not the last seg */
18308c2ecf20Sopenharmony_ci	bmov	SCB_RESIDUAL_DATACNT, SHCNT, 3 ret;
18318c2ecf20Sopenharmony_ci
18328c2ecf20Sopenharmony_ciexport timer_isr:
18338c2ecf20Sopenharmony_ci	call	issue_cmdcmplt;
18348c2ecf20Sopenharmony_ci	mvi	CLRSEQINTSTAT, CLRSEQ_SWTMRTO;
18358c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_SET_MODE_BUG) != 0) {
18368c2ecf20Sopenharmony_ci		/*
18378c2ecf20Sopenharmony_ci		 * In H2A4, the mode pointer is not saved
18388c2ecf20Sopenharmony_ci		 * for intvec2, but is restored on iret.
18398c2ecf20Sopenharmony_ci		 * This can lead to the restoration of a
18408c2ecf20Sopenharmony_ci		 * bogus mode ptr.  Manually clear the
18418c2ecf20Sopenharmony_ci		 * intmask bits and do a normal return
18428c2ecf20Sopenharmony_ci		 * to compensate.
18438c2ecf20Sopenharmony_ci		 */
18448c2ecf20Sopenharmony_ci		and	SEQINTCTL, ~(INTMASK2|INTMASK1) ret;
18458c2ecf20Sopenharmony_ci	} else {
18468c2ecf20Sopenharmony_ci		or	SEQINTCTL, IRET ret;
18478c2ecf20Sopenharmony_ci	}
18488c2ecf20Sopenharmony_ci
18498c2ecf20Sopenharmony_ciexport seq_isr:
18508c2ecf20Sopenharmony_ci	if ((ahd->features & AHD_RTI) == 0) {
18518c2ecf20Sopenharmony_ci		/*
18528c2ecf20Sopenharmony_ci		 * On RevA Silicon, if the target returns us to data-out
18538c2ecf20Sopenharmony_ci		 * after we have already trained for data-out, it is
18548c2ecf20Sopenharmony_ci		 * possible for us to transition the free running clock to
18558c2ecf20Sopenharmony_ci		 * data-valid before the required 100ns P1 setup time (8 P1
18568c2ecf20Sopenharmony_ci		 * assertions in fast-160 mode).  This will only happen if
18578c2ecf20Sopenharmony_ci		 * this L-Q is a continuation of a data transfer for which
18588c2ecf20Sopenharmony_ci		 * we have already prefetched data into our FIFO (LQ/Data
18598c2ecf20Sopenharmony_ci		 * followed by LQ/Data for the same write transaction).
18608c2ecf20Sopenharmony_ci		 * This can cause some target implementations to miss the
18618c2ecf20Sopenharmony_ci		 * first few data transfers on the bus.  We detect this
18628c2ecf20Sopenharmony_ci		 * situation by noticing that this is the first data transfer
18638c2ecf20Sopenharmony_ci		 * after an LQ (LQIWORKONLQ true), that the data transfer is
18648c2ecf20Sopenharmony_ci		 * a continuation of a transfer already setup in our FIFO
18658c2ecf20Sopenharmony_ci		 * (SAVEPTRS interrupt), and that the transaction is a write
18668c2ecf20Sopenharmony_ci		 * (DIRECTION set in DFCNTRL). The delay is performed by
18678c2ecf20Sopenharmony_ci		 * disabling SCSIEN until we see the first REQ from the
18688c2ecf20Sopenharmony_ci		 * target.
18698c2ecf20Sopenharmony_ci		 * 
18708c2ecf20Sopenharmony_ci		 * First instruction in an ISR cannot be a branch on
18718c2ecf20Sopenharmony_ci		 * Rev A.  Snapshot LQISTAT2 so the status is not missed
18728c2ecf20Sopenharmony_ci		 * and deffer the test by one instruction.
18738c2ecf20Sopenharmony_ci		 */
18748c2ecf20Sopenharmony_ci		mov	REG_ISR, LQISTAT2;
18758c2ecf20Sopenharmony_ci		test	REG_ISR, LQIWORKONLQ jz main_isr;
18768c2ecf20Sopenharmony_ci		test	SEQINTSRC, SAVEPTRS  jz main_isr;
18778c2ecf20Sopenharmony_ci		test	LONGJMP_ADDR[1], INVALID_ADDR jz saveptr_active_fifo;
18788c2ecf20Sopenharmony_ci		/*
18798c2ecf20Sopenharmony_ci		 * Switch to the active FIFO after clearing the snapshot
18808c2ecf20Sopenharmony_ci		 * savepointer in the current FIFO.  We do this so that
18818c2ecf20Sopenharmony_ci		 * a pending CTXTDONE or SAVEPTR is visible in the active
18828c2ecf20Sopenharmony_ci		 * FIFO.  This status is the only way we can detect if we
18838c2ecf20Sopenharmony_ci		 * have lost the race (e.g. host paused us) and our attempts
18848c2ecf20Sopenharmony_ci		 * to disable the channel occurred after all REQs were
18858c2ecf20Sopenharmony_ci		 * already seen and acked (REQINIT never comes true).
18868c2ecf20Sopenharmony_ci		 */
18878c2ecf20Sopenharmony_ci		mvi	DFFSXFRCTL, CLRCHN;
18888c2ecf20Sopenharmony_ci		xor	MODE_PTR, MK_MODE(M_DFF1, M_DFF1);
18898c2ecf20Sopenharmony_ci		test	DFCNTRL, DIRECTION jz interrupt_return;
18908c2ecf20Sopenharmony_ci		and	DFCNTRL, ~SCSIEN;
18918c2ecf20Sopenharmony_cisnapshot_wait_data_valid:
18928c2ecf20Sopenharmony_ci		test	SEQINTSRC, (CTXTDONE|SAVEPTRS) jnz interrupt_return;
18938c2ecf20Sopenharmony_ci		test	SSTAT1, REQINIT	jz snapshot_wait_data_valid;
18948c2ecf20Sopenharmony_cisnapshot_data_valid:
18958c2ecf20Sopenharmony_ci		or	DFCNTRL, SCSIEN;
18968c2ecf20Sopenharmony_ci		or	SEQINTCTL, IRET ret;
18978c2ecf20Sopenharmony_cisnapshot_saveptr:
18988c2ecf20Sopenharmony_ci		mvi	DFFSXFRCTL, CLRCHN;
18998c2ecf20Sopenharmony_ci		or	SEQINTCTL, IRET ret;
19008c2ecf20Sopenharmony_cimain_isr:
19018c2ecf20Sopenharmony_ci	}
19028c2ecf20Sopenharmony_ci	test	SEQINTSRC, CFG4DATA	jnz cfg4data_intr;
19038c2ecf20Sopenharmony_ci	test	SEQINTSRC, CFG4ISTAT	jnz cfg4istat_intr;
19048c2ecf20Sopenharmony_ci	test	SEQINTSRC, SAVEPTRS	jnz saveptr_intr;
19058c2ecf20Sopenharmony_ci	test	SEQINTSRC, CFG4ICMD	jnz cfg4icmd_intr;
19068c2ecf20Sopenharmony_ci	SET_SEQINTCODE(INVALID_SEQINT)
19078c2ecf20Sopenharmony_ci
19088c2ecf20Sopenharmony_ci/*
19098c2ecf20Sopenharmony_ci * There are two types of save pointers interrupts:
19108c2ecf20Sopenharmony_ci * The first is a snapshot save pointers where the current FIFO is not
19118c2ecf20Sopenharmony_ci * active and contains a snapshot of the current poniter information.
19128c2ecf20Sopenharmony_ci * This happens between packets in a stream for a single L_Q.  Since we
19138c2ecf20Sopenharmony_ci * are not performing a pointer save, we can safely clear the channel
19148c2ecf20Sopenharmony_ci * so it can be used for other transactions.  On RTI capable controllers,
19158c2ecf20Sopenharmony_ci * where snapshots can, and are, disabled, the code to handle this type
19168c2ecf20Sopenharmony_ci * of snapshot is not active.
19178c2ecf20Sopenharmony_ci *
19188c2ecf20Sopenharmony_ci * The second case is a save pointers on an active FIFO which occurs
19198c2ecf20Sopenharmony_ci * if the target changes to a new L_Q or busfrees/QASes and the transfer
19208c2ecf20Sopenharmony_ci * has a residual.  This should occur coincident with a ctxtdone.  We
19218c2ecf20Sopenharmony_ci * disable the interrupt and allow our active routine to handle the
19228c2ecf20Sopenharmony_ci * save.
19238c2ecf20Sopenharmony_ci */
19248c2ecf20Sopenharmony_cisaveptr_intr:
19258c2ecf20Sopenharmony_ci	if ((ahd->features & AHD_RTI) == 0) {
19268c2ecf20Sopenharmony_ci		test	LONGJMP_ADDR[1], INVALID_ADDR jnz snapshot_saveptr;
19278c2ecf20Sopenharmony_ci	}
19288c2ecf20Sopenharmony_cisaveptr_active_fifo:
19298c2ecf20Sopenharmony_ci	and	SEQIMODE, ~ENSAVEPTRS;
19308c2ecf20Sopenharmony_ci	or	SEQINTCTL, IRET ret;
19318c2ecf20Sopenharmony_ci
19328c2ecf20Sopenharmony_cicfg4data_intr:
19338c2ecf20Sopenharmony_ci	test	SCB_SGPTR[0], SG_LIST_NULL jnz pkt_handle_overrun_inc_use_count;
19348c2ecf20Sopenharmony_ci	call	load_first_seg;
19358c2ecf20Sopenharmony_ci	call	pkt_handle_xfer;
19368c2ecf20Sopenharmony_ci	inc	SCB_FIFO_USE_COUNT;
19378c2ecf20Sopenharmony_ciinterrupt_return:
19388c2ecf20Sopenharmony_ci	or	SEQINTCTL, IRET ret;
19398c2ecf20Sopenharmony_ci
19408c2ecf20Sopenharmony_cicfg4istat_intr:
19418c2ecf20Sopenharmony_ci	call	freeze_queue;
19428c2ecf20Sopenharmony_ci	add	NONE, -13, SCB_CDB_LEN;
19438c2ecf20Sopenharmony_ci	jnc	cfg4istat_have_sense_addr;
19448c2ecf20Sopenharmony_ci	test	SCB_CDB_LEN, SCB_CDB_LEN_PTR jnz cfg4istat_have_sense_addr;
19458c2ecf20Sopenharmony_ci	/*
19468c2ecf20Sopenharmony_ci	 * Host sets up address/count and enables transfer.
19478c2ecf20Sopenharmony_ci	 */
19488c2ecf20Sopenharmony_ci	SET_SEQINTCODE(CFG4ISTAT_INTR)
19498c2ecf20Sopenharmony_ci	jmp	cfg4istat_setup_handler;
19508c2ecf20Sopenharmony_cicfg4istat_have_sense_addr:
19518c2ecf20Sopenharmony_ci	bmov	HADDR, SCB_SENSE_BUSADDR, 4;
19528c2ecf20Sopenharmony_ci	mvi	HCNT[1], (AHD_SENSE_BUFSIZE >> 8);
19538c2ecf20Sopenharmony_ci	mvi	SG_CACHE_PRE, LAST_SEG;
19548c2ecf20Sopenharmony_ci	mvi	DFCNTRL, PRELOADEN|SCSIEN|HDMAEN;
19558c2ecf20Sopenharmony_cicfg4istat_setup_handler:
19568c2ecf20Sopenharmony_ci	/*
19578c2ecf20Sopenharmony_ci	 * Status pkt is transferring to host.
19588c2ecf20Sopenharmony_ci	 * Wait in idle loop for transfer to complete.
19598c2ecf20Sopenharmony_ci	 * If a command completed before an attempted
19608c2ecf20Sopenharmony_ci	 * task management function completed, notify the host.
19618c2ecf20Sopenharmony_ci	 */
19628c2ecf20Sopenharmony_ci	test	SCB_TASK_MANAGEMENT, 0xFF jz cfg4istat_no_taskmgmt_func;
19638c2ecf20Sopenharmony_ci	SET_SEQINTCODE(TASKMGMT_CMD_CMPLT_OKAY)
19648c2ecf20Sopenharmony_cicfg4istat_no_taskmgmt_func:
19658c2ecf20Sopenharmony_ci	call	pkt_handle_status;
19668c2ecf20Sopenharmony_ci	or	SEQINTCTL, IRET ret;
19678c2ecf20Sopenharmony_ci
19688c2ecf20Sopenharmony_cicfg4icmd_intr:
19698c2ecf20Sopenharmony_ci	/*
19708c2ecf20Sopenharmony_ci	 * In the case of DMAing a CDB from the host, the normal
19718c2ecf20Sopenharmony_ci	 * CDB buffer is formatted with an 8 byte address followed
19728c2ecf20Sopenharmony_ci	 * by a 1 byte count.
19738c2ecf20Sopenharmony_ci	 */
19748c2ecf20Sopenharmony_ci	bmov	HADDR[0], SCB_HOST_CDB_PTR, 9;
19758c2ecf20Sopenharmony_ci	mvi	SG_CACHE_PRE, LAST_SEG;
19768c2ecf20Sopenharmony_ci	mvi	DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN);
19778c2ecf20Sopenharmony_ci	call	pkt_handle_cdb;
19788c2ecf20Sopenharmony_ci	or	SEQINTCTL, IRET ret;
19798c2ecf20Sopenharmony_ci
19808c2ecf20Sopenharmony_ci/*
19818c2ecf20Sopenharmony_ci * See if the target has gone on in this context creating an
19828c2ecf20Sopenharmony_ci * overrun condition.  For the write case, the hardware cannot
19838c2ecf20Sopenharmony_ci * ack bytes until data are provided.  So, if the target begins
19848c2ecf20Sopenharmony_ci * another  packet without changing contexts, implying we are
19858c2ecf20Sopenharmony_ci * not sitting on a packet boundary, we are in an overrun
19868c2ecf20Sopenharmony_ci * situation.  For the read case, the hardware will continue to
19878c2ecf20Sopenharmony_ci * ack bytes into the FIFO, and may even ack the last overrun packet
19888c2ecf20Sopenharmony_ci * into the FIFO.   If the FIFO should become non-empty, we are in
19898c2ecf20Sopenharmony_ci * a read overrun case.
19908c2ecf20Sopenharmony_ci */
19918c2ecf20Sopenharmony_ci#define check_overrun							\
19928c2ecf20Sopenharmony_ci	/* Not on a packet boundary. */					\
19938c2ecf20Sopenharmony_ci	test 	MDFFSTAT, DLZERO jz pkt_handle_overrun;			\
19948c2ecf20Sopenharmony_ci	test	DFSTATUS, FIFOEMP jz pkt_handle_overrun
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_cipkt_handle_xfer:
19978c2ecf20Sopenharmony_ci	test	SG_STATE, LOADING_NEEDED jz pkt_last_seg;
19988c2ecf20Sopenharmony_ci	call	setjmp;
19998c2ecf20Sopenharmony_ci	test	SEQINTSRC, SAVEPTRS jnz pkt_saveptrs;
20008c2ecf20Sopenharmony_ci	test	SCSIPHASE, ~DATA_PHASE_MASK jz . + 2;
20018c2ecf20Sopenharmony_ci	test	SCSISIGO, ATNO jnz . + 2;
20028c2ecf20Sopenharmony_ci	test	SSTAT2, NONPACKREQ jz pkt_service_fifo;
20038c2ecf20Sopenharmony_ci	/*
20048c2ecf20Sopenharmony_ci	 * Defer handling of this NONPACKREQ until we
20058c2ecf20Sopenharmony_ci	 * can be sure it pertains to this FIFO.  SAVEPTRS
20068c2ecf20Sopenharmony_ci	 * will not be asserted if the NONPACKREQ is for us,
20078c2ecf20Sopenharmony_ci	 * so we must simulate it if shadow is valid.  If
20088c2ecf20Sopenharmony_ci	 * shadow is not valid, keep running this FIFO until we
20098c2ecf20Sopenharmony_ci	 * have satisfied the transfer by loading segments and
20108c2ecf20Sopenharmony_ci	 * waiting for either shadow valid or last_seg_done.
20118c2ecf20Sopenharmony_ci	 */
20128c2ecf20Sopenharmony_ci	test	MDFFSTAT, SHVALID jnz pkt_saveptrs;
20138c2ecf20Sopenharmony_cipkt_service_fifo:
20148c2ecf20Sopenharmony_ci	test	SG_STATE, LOADING_NEEDED jnz service_fifo;
20158c2ecf20Sopenharmony_cipkt_last_seg:
20168c2ecf20Sopenharmony_ci	call	setjmp;
20178c2ecf20Sopenharmony_ci	test	SEQINTSRC, SAVEPTRS jnz pkt_saveptrs;
20188c2ecf20Sopenharmony_ci	test	SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_last_seg_done;
20198c2ecf20Sopenharmony_ci	test	SCSIPHASE, ~DATA_PHASE_MASK jz . + 2;
20208c2ecf20Sopenharmony_ci	test	SCSISIGO, ATNO jnz . + 2;
20218c2ecf20Sopenharmony_ci	test	SSTAT2, NONPACKREQ jz return;
20228c2ecf20Sopenharmony_ci	test	MDFFSTAT, SHVALID jz return;
20238c2ecf20Sopenharmony_ci	/* FALLTHROUGH */
20248c2ecf20Sopenharmony_ci
20258c2ecf20Sopenharmony_ci/*
20268c2ecf20Sopenharmony_ci * Either a SAVEPTRS interrupt condition is pending for this FIFO
20278c2ecf20Sopenharmony_ci * or we have a pending NONPACKREQ for this FIFO.  We differentiate
20288c2ecf20Sopenharmony_ci * between the two by capturing the state of the SAVEPTRS interrupt
20298c2ecf20Sopenharmony_ci * prior to clearing this status and executing the common code for
20308c2ecf20Sopenharmony_ci * these two cases.
20318c2ecf20Sopenharmony_ci */
20328c2ecf20Sopenharmony_cipkt_saveptrs:
20338c2ecf20Sopenharmony_ciBEGIN_CRITICAL;
20348c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
20358c2ecf20Sopenharmony_ci		or	DFCNTRL, FIFOFLUSH;
20368c2ecf20Sopenharmony_ci	}
20378c2ecf20Sopenharmony_ci	mov	REG0, SEQINTSRC;
20388c2ecf20Sopenharmony_ci	call	calc_residual;
20398c2ecf20Sopenharmony_ci	call	save_pointers;
20408c2ecf20Sopenharmony_ci	mvi	CLRSEQINTSRC, CLRSAVEPTRS;
20418c2ecf20Sopenharmony_ci	call	disable_ccsgen;
20428c2ecf20Sopenharmony_ci	or	SEQIMODE, ENSAVEPTRS;
20438c2ecf20Sopenharmony_ci	test	DFCNTRL, DIRECTION jnz pkt_saveptrs_check_status;
20448c2ecf20Sopenharmony_ci	test	DFSTATUS, FIFOEMP jnz pkt_saveptrs_check_status;
20458c2ecf20Sopenharmony_ci	/*
20468c2ecf20Sopenharmony_ci	 * Keep a handler around for this FIFO until it drains
20478c2ecf20Sopenharmony_ci	 * to the host to guarantee that we don't complete the
20488c2ecf20Sopenharmony_ci	 * command to the host before the data arrives.
20498c2ecf20Sopenharmony_ci	 */
20508c2ecf20Sopenharmony_cipkt_saveptrs_wait_fifoemp:
20518c2ecf20Sopenharmony_ci	call	setjmp;
20528c2ecf20Sopenharmony_ci	test	DFSTATUS, FIFOEMP jz return;
20538c2ecf20Sopenharmony_cipkt_saveptrs_check_status:
20548c2ecf20Sopenharmony_ci	or	LONGJMP_ADDR[1], INVALID_ADDR;
20558c2ecf20Sopenharmony_ci	test	REG0, SAVEPTRS jz unexpected_nonpkt_phase;
20568c2ecf20Sopenharmony_ci	dec	SCB_FIFO_USE_COUNT;
20578c2ecf20Sopenharmony_ci	test	SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
20588c2ecf20Sopenharmony_ci	mvi	DFFSXFRCTL, CLRCHN ret;
20598c2ecf20Sopenharmony_ci
20608c2ecf20Sopenharmony_ci/*
20618c2ecf20Sopenharmony_ci * LAST_SEG_DONE status has been seen in the current FIFO.
20628c2ecf20Sopenharmony_ci * This indicates that all of the allowed data for this
20638c2ecf20Sopenharmony_ci * command has transferred across the SCSI and host buses.
20648c2ecf20Sopenharmony_ci * Check for overrun and see if we can complete this command.
20658c2ecf20Sopenharmony_ci */
20668c2ecf20Sopenharmony_cipkt_last_seg_done:
20678c2ecf20Sopenharmony_ci	/*
20688c2ecf20Sopenharmony_ci	 * Mark transfer as completed.
20698c2ecf20Sopenharmony_ci	 */
20708c2ecf20Sopenharmony_ci	or	SCB_SGPTR, SG_LIST_NULL;
20718c2ecf20Sopenharmony_ci
20728c2ecf20Sopenharmony_ci	/*
20738c2ecf20Sopenharmony_ci	 * Wait for the current context to finish to verify that
20748c2ecf20Sopenharmony_ci	 * no overrun condition has occurred.
20758c2ecf20Sopenharmony_ci	 */
20768c2ecf20Sopenharmony_ci	test	SEQINTSRC, CTXTDONE jnz pkt_ctxt_done;
20778c2ecf20Sopenharmony_ci	call	setjmp;
20788c2ecf20Sopenharmony_cipkt_wait_ctxt_done_loop:
20798c2ecf20Sopenharmony_ci	test	SEQINTSRC, CTXTDONE jnz pkt_ctxt_done;
20808c2ecf20Sopenharmony_ci	/*
20818c2ecf20Sopenharmony_ci	 * A sufficiently large overrun or a NONPACKREQ may
20828c2ecf20Sopenharmony_ci	 * prevent CTXTDONE from ever asserting, so we must
20838c2ecf20Sopenharmony_ci	 * poll for these statuses too.
20848c2ecf20Sopenharmony_ci	 */
20858c2ecf20Sopenharmony_ci	check_overrun;
20868c2ecf20Sopenharmony_ci	test	SSTAT2, NONPACKREQ jz return;
20878c2ecf20Sopenharmony_ci	test	SEQINTSRC, CTXTDONE jz unexpected_nonpkt_phase;
20888c2ecf20Sopenharmony_ci	/* FALLTHROUGH */
20898c2ecf20Sopenharmony_ci
20908c2ecf20Sopenharmony_cipkt_ctxt_done:
20918c2ecf20Sopenharmony_ci	check_overrun;
20928c2ecf20Sopenharmony_ci	or	LONGJMP_ADDR[1], INVALID_ADDR;
20938c2ecf20Sopenharmony_ci	/*
20948c2ecf20Sopenharmony_ci	 * If status has been received, it is safe to skip
20958c2ecf20Sopenharmony_ci	 * the check to see if another FIFO is active because
20968c2ecf20Sopenharmony_ci	 * LAST_SEG_DONE has been observed.  However, we check
20978c2ecf20Sopenharmony_ci	 * the FIFO anyway since it costs us only one extra
20988c2ecf20Sopenharmony_ci	 * instruction to leverage common code to perform the
20998c2ecf20Sopenharmony_ci	 * SCB completion.
21008c2ecf20Sopenharmony_ci	 */
21018c2ecf20Sopenharmony_ci	dec	SCB_FIFO_USE_COUNT;
21028c2ecf20Sopenharmony_ci	test	SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
21038c2ecf20Sopenharmony_ci	mvi	DFFSXFRCTL, CLRCHN ret;
21048c2ecf20Sopenharmony_ciEND_CRITICAL;
21058c2ecf20Sopenharmony_ci
21068c2ecf20Sopenharmony_ci/*
21078c2ecf20Sopenharmony_ci * Must wait until CDB xfer is over before issuing the
21088c2ecf20Sopenharmony_ci * clear channel.
21098c2ecf20Sopenharmony_ci */
21108c2ecf20Sopenharmony_cipkt_handle_cdb:
21118c2ecf20Sopenharmony_ci	call	setjmp;
21128c2ecf20Sopenharmony_ci	test	SG_CACHE_SHADOW, LAST_SEG_DONE jz return;
21138c2ecf20Sopenharmony_ci	or	LONGJMP_ADDR[1], INVALID_ADDR;
21148c2ecf20Sopenharmony_ci	mvi	DFFSXFRCTL, CLRCHN ret;
21158c2ecf20Sopenharmony_ci
21168c2ecf20Sopenharmony_ci/*
21178c2ecf20Sopenharmony_ci * Watch over the status transfer.  Our host sense buffer is
21188c2ecf20Sopenharmony_ci * large enough to take the maximum allowed status packet.
21198c2ecf20Sopenharmony_ci * None-the-less, we must still catch and report overruns to
21208c2ecf20Sopenharmony_ci * the host.  Additionally, properly catch unexpected non-packet
21218c2ecf20Sopenharmony_ci * phases that are typically caused by CRC errors in status packet
21228c2ecf20Sopenharmony_ci * transmission.
21238c2ecf20Sopenharmony_ci */
21248c2ecf20Sopenharmony_cipkt_handle_status:
21258c2ecf20Sopenharmony_ci	call	setjmp;
21268c2ecf20Sopenharmony_ci	test	SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_status_check_overrun;
21278c2ecf20Sopenharmony_ci	test	SEQINTSRC, CTXTDONE jz pkt_status_check_nonpackreq;
21288c2ecf20Sopenharmony_ci	test	SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_status_check_overrun;
21298c2ecf20Sopenharmony_cipkt_status_IU_done:
21308c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
21318c2ecf20Sopenharmony_ci		or	DFCNTRL, FIFOFLUSH;
21328c2ecf20Sopenharmony_ci	}
21338c2ecf20Sopenharmony_ci	test	DFSTATUS, FIFOEMP jz return;
21348c2ecf20Sopenharmony_ciBEGIN_CRITICAL;
21358c2ecf20Sopenharmony_ci	or	LONGJMP_ADDR[1], INVALID_ADDR;
21368c2ecf20Sopenharmony_ci	mvi	SCB_SCSI_STATUS, STATUS_PKT_SENSE;
21378c2ecf20Sopenharmony_ci	or	SCB_CONTROL, STATUS_RCVD;
21388c2ecf20Sopenharmony_ci	jmp	pkt_complete_scb_if_fifos_idle;
21398c2ecf20Sopenharmony_ciEND_CRITICAL;
21408c2ecf20Sopenharmony_cipkt_status_check_overrun:
21418c2ecf20Sopenharmony_ci	/*
21428c2ecf20Sopenharmony_ci	 * Status PKT overruns are uncerimoniously recovered with a
21438c2ecf20Sopenharmony_ci	 * bus reset.  If we've overrun, let the host know so that
21448c2ecf20Sopenharmony_ci	 * recovery can be performed.
21458c2ecf20Sopenharmony_ci	 *
21468c2ecf20Sopenharmony_ci	 * LAST_SEG_DONE has been observed.  If either CTXTDONE or
21478c2ecf20Sopenharmony_ci	 * a NONPACKREQ phase change have occurred and the FIFO is
21488c2ecf20Sopenharmony_ci	 * empty, there is no overrun.
21498c2ecf20Sopenharmony_ci	 */
21508c2ecf20Sopenharmony_ci	test	DFSTATUS, FIFOEMP jz pkt_status_report_overrun;
21518c2ecf20Sopenharmony_ci	test	SEQINTSRC, CTXTDONE jz . + 2;
21528c2ecf20Sopenharmony_ci	test	DFSTATUS, FIFOEMP jnz pkt_status_IU_done;
21538c2ecf20Sopenharmony_ci	test	SCSIPHASE, ~DATA_PHASE_MASK jz return;
21548c2ecf20Sopenharmony_ci	test	DFSTATUS, FIFOEMP jnz pkt_status_check_nonpackreq;
21558c2ecf20Sopenharmony_cipkt_status_report_overrun:
21568c2ecf20Sopenharmony_ci	SET_SEQINTCODE(STATUS_OVERRUN)
21578c2ecf20Sopenharmony_ci	/* SEQUENCER RESTARTED */
21588c2ecf20Sopenharmony_cipkt_status_check_nonpackreq:
21598c2ecf20Sopenharmony_ci	/*
21608c2ecf20Sopenharmony_ci	 * CTXTDONE may be held off if a NONPACKREQ is associated with
21618c2ecf20Sopenharmony_ci	 * the current context.  If a NONPACKREQ is observed, decide
21628c2ecf20Sopenharmony_ci	 * if it is for the current context.  If it is for the current
21638c2ecf20Sopenharmony_ci	 * context, we must defer NONPACKREQ processing until all data
21648c2ecf20Sopenharmony_ci	 * has transferred to the host.
21658c2ecf20Sopenharmony_ci	 */
21668c2ecf20Sopenharmony_ci	test	SCSIPHASE, ~DATA_PHASE_MASK jz return;
21678c2ecf20Sopenharmony_ci	test	SCSISIGO, ATNO jnz . + 2;
21688c2ecf20Sopenharmony_ci	test	SSTAT2, NONPACKREQ jz return;
21698c2ecf20Sopenharmony_ci	test	SEQINTSRC, CTXTDONE jnz pkt_status_IU_done;
21708c2ecf20Sopenharmony_ci	test	DFSTATUS, FIFOEMP jz return;
21718c2ecf20Sopenharmony_ci	/*
21728c2ecf20Sopenharmony_ci	 * The unexpected nonpkt phase handler assumes that any
21738c2ecf20Sopenharmony_ci	 * data channel use will have a FIFO reference count.  It
21748c2ecf20Sopenharmony_ci	 * turns out that the status handler doesn't need a references
21758c2ecf20Sopenharmony_ci	 * count since the status received flag, and thus completion
21768c2ecf20Sopenharmony_ci	 * processing, cannot be set until the handler is finished.
21778c2ecf20Sopenharmony_ci	 * We increment the count here to make the nonpkt handler
21788c2ecf20Sopenharmony_ci	 * happy.
21798c2ecf20Sopenharmony_ci	 */
21808c2ecf20Sopenharmony_ci	inc	SCB_FIFO_USE_COUNT;
21818c2ecf20Sopenharmony_ci	/* FALLTHROUGH */
21828c2ecf20Sopenharmony_ci
21838c2ecf20Sopenharmony_ci/*
21848c2ecf20Sopenharmony_ci * Nonpackreq is a polled status.  It can come true in three situations:
21858c2ecf20Sopenharmony_ci * we have received an L_Q, we have sent one or more L_Qs, or there is no
21868c2ecf20Sopenharmony_ci * L_Q context associated with this REQ (REQ occurs immediately after a
21878c2ecf20Sopenharmony_ci * (re)selection).  Routines that know that the context responsible for this
21888c2ecf20Sopenharmony_ci * nonpackreq call directly into unexpected_nonpkt_phase.  In the case of the
21898c2ecf20Sopenharmony_ci * top level idle loop, we exhaust all active contexts prior to determining that
21908c2ecf20Sopenharmony_ci * we simply do not have the full I_T_L_Q for this phase.
21918c2ecf20Sopenharmony_ci */
21928c2ecf20Sopenharmony_ciunexpected_nonpkt_phase_find_ctxt:
21938c2ecf20Sopenharmony_ci	/*
21948c2ecf20Sopenharmony_ci	 * This nonpackreq is most likely associated with one of the tags
21958c2ecf20Sopenharmony_ci	 * in a FIFO or an outgoing LQ.  Only treat it as an I_T only
21968c2ecf20Sopenharmony_ci	 * nonpackreq if we've cleared out the FIFOs and handled any
21978c2ecf20Sopenharmony_ci	 * pending SELDO.
21988c2ecf20Sopenharmony_ci	 */
21998c2ecf20Sopenharmony_ciSET_SRC_MODE	M_SCSI;
22008c2ecf20Sopenharmony_ciSET_DST_MODE	M_SCSI;
22018c2ecf20Sopenharmony_ci	and	A, FIFO1FREE|FIFO0FREE, DFFSTAT;
22028c2ecf20Sopenharmony_ci	cmp	A, FIFO1FREE|FIFO0FREE jne return;
22038c2ecf20Sopenharmony_ci	test	SSTAT0, SELDO jnz return;
22048c2ecf20Sopenharmony_ci	mvi	SCBPTR[1], SCB_LIST_NULL;
22058c2ecf20Sopenharmony_ciunexpected_nonpkt_phase:
22068c2ecf20Sopenharmony_ci	test	MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
22078c2ecf20Sopenharmony_ci		jnz unexpected_nonpkt_mode_cleared;
22088c2ecf20Sopenharmony_ciSET_SRC_MODE	M_DFF0;
22098c2ecf20Sopenharmony_ciSET_DST_MODE	M_DFF0;
22108c2ecf20Sopenharmony_ci	or	LONGJMP_ADDR[1], INVALID_ADDR;
22118c2ecf20Sopenharmony_ci	dec	SCB_FIFO_USE_COUNT;
22128c2ecf20Sopenharmony_ci	mvi	DFFSXFRCTL, CLRCHN;
22138c2ecf20Sopenharmony_ciunexpected_nonpkt_mode_cleared:
22148c2ecf20Sopenharmony_ci	mvi	CLRSINT2, CLRNONPACKREQ;
22158c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
22168c2ecf20Sopenharmony_ci		/*
22178c2ecf20Sopenharmony_ci		 * Test to ensure that the bus has not
22188c2ecf20Sopenharmony_ci		 * already gone free prior to clearing
22198c2ecf20Sopenharmony_ci		 * any stale busfree status.  This avoids
22208c2ecf20Sopenharmony_ci		 * a window whereby a busfree just after
22218c2ecf20Sopenharmony_ci		 * a selection could be missed.
22228c2ecf20Sopenharmony_ci		 */
22238c2ecf20Sopenharmony_ci		test	SCSISIGI, BSYI jz . + 2;
22248c2ecf20Sopenharmony_ci		mvi	CLRSINT1,CLRBUSFREE;
22258c2ecf20Sopenharmony_ci		or	SIMODE1, ENBUSFREE;
22268c2ecf20Sopenharmony_ci	}
22278c2ecf20Sopenharmony_ci	test	SCSIPHASE, ~(MSG_IN_PHASE|MSG_OUT_PHASE) jnz illegal_phase;
22288c2ecf20Sopenharmony_ci	SET_SEQINTCODE(ENTERING_NONPACK)
22298c2ecf20Sopenharmony_ci	jmp	ITloop;
22308c2ecf20Sopenharmony_ci
22318c2ecf20Sopenharmony_ciillegal_phase:
22328c2ecf20Sopenharmony_ci	SET_SEQINTCODE(ILLEGAL_PHASE)
22338c2ecf20Sopenharmony_ci	jmp	ITloop;
22348c2ecf20Sopenharmony_ci
22358c2ecf20Sopenharmony_ci/*
22368c2ecf20Sopenharmony_ci * We have entered an overrun situation.  If we have working
22378c2ecf20Sopenharmony_ci * BITBUCKET, flip that on and let the hardware eat any overrun
22388c2ecf20Sopenharmony_ci * data.  Otherwise use an overrun buffer in the host to simulate
22398c2ecf20Sopenharmony_ci * BITBUCKET.
22408c2ecf20Sopenharmony_ci */
22418c2ecf20Sopenharmony_cipkt_handle_overrun_inc_use_count:
22428c2ecf20Sopenharmony_ci	inc	SCB_FIFO_USE_COUNT;
22438c2ecf20Sopenharmony_cipkt_handle_overrun:
22448c2ecf20Sopenharmony_ci	SET_SEQINTCODE(CFG4OVERRUN)
22458c2ecf20Sopenharmony_ci	call	freeze_queue;
22468c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) == 0) {
22478c2ecf20Sopenharmony_ci		or	DFFSXFRCTL, DFFBITBUCKET;
22488c2ecf20Sopenharmony_ciSET_SRC_MODE	M_DFF1;
22498c2ecf20Sopenharmony_ciSET_DST_MODE	M_DFF1;
22508c2ecf20Sopenharmony_ci	} else {
22518c2ecf20Sopenharmony_ci		call	load_overrun_buf;
22528c2ecf20Sopenharmony_ci		mvi	DFCNTRL, (HDMAEN|SCSIEN|PRELOADEN);
22538c2ecf20Sopenharmony_ci	}
22548c2ecf20Sopenharmony_ci	call	setjmp;
22558c2ecf20Sopenharmony_ci	if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
22568c2ecf20Sopenharmony_ci		test	DFSTATUS, PRELOAD_AVAIL jz overrun_load_done;
22578c2ecf20Sopenharmony_ci		call	load_overrun_buf;
22588c2ecf20Sopenharmony_ci		or	DFCNTRL, PRELOADEN;
22598c2ecf20Sopenharmony_cioverrun_load_done:
22608c2ecf20Sopenharmony_ci		test	SEQINTSRC, CTXTDONE jnz pkt_overrun_end;
22618c2ecf20Sopenharmony_ci	} else {
22628c2ecf20Sopenharmony_ci		test	DFFSXFRCTL, DFFBITBUCKET jz pkt_overrun_end;
22638c2ecf20Sopenharmony_ci	}
22648c2ecf20Sopenharmony_ci	test	SSTAT2, NONPACKREQ jz return;
22658c2ecf20Sopenharmony_cipkt_overrun_end:
22668c2ecf20Sopenharmony_ci	or	SCB_RESIDUAL_SGPTR, SG_OVERRUN_RESID;
22678c2ecf20Sopenharmony_ci	test	SEQINTSRC, CTXTDONE jz unexpected_nonpkt_phase;
22688c2ecf20Sopenharmony_ci	dec	SCB_FIFO_USE_COUNT;
22698c2ecf20Sopenharmony_ci	or	LONGJMP_ADDR[1], INVALID_ADDR;
22708c2ecf20Sopenharmony_ci	test	SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
22718c2ecf20Sopenharmony_ci	mvi	DFFSXFRCTL, CLRCHN ret;
22728c2ecf20Sopenharmony_ci
22738c2ecf20Sopenharmony_ciif ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
22748c2ecf20Sopenharmony_ciload_overrun_buf:
22758c2ecf20Sopenharmony_ci	/*
22768c2ecf20Sopenharmony_ci	 * Load a dummy segment if preload space is available.
22778c2ecf20Sopenharmony_ci	 */
22788c2ecf20Sopenharmony_ci	mov 	HADDR[0], SHARED_DATA_ADDR;
22798c2ecf20Sopenharmony_ci	add	HADDR[1], PKT_OVERRUN_BUFOFFSET, SHARED_DATA_ADDR[1];
22808c2ecf20Sopenharmony_ci	mov	ACCUM_SAVE, A;
22818c2ecf20Sopenharmony_ci	clr	A;
22828c2ecf20Sopenharmony_ci	adc	HADDR[2], A, SHARED_DATA_ADDR[2];
22838c2ecf20Sopenharmony_ci	adc	HADDR[3], A, SHARED_DATA_ADDR[3];
22848c2ecf20Sopenharmony_ci	mov	A, ACCUM_SAVE;
22858c2ecf20Sopenharmony_ci	bmov	HADDR[4], ALLZEROS, 4;
22868c2ecf20Sopenharmony_ci	/* PKT_OVERRUN_BUFSIZE is a multiple of 256 */
22878c2ecf20Sopenharmony_ci	clr	HCNT[0];
22888c2ecf20Sopenharmony_ci	mvi	HCNT[1], ((PKT_OVERRUN_BUFSIZE >> 8) & 0xFF);
22898c2ecf20Sopenharmony_ci	clr	HCNT[2] ret;
22908c2ecf20Sopenharmony_ci}
2291