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