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