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