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