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