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