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