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