xref: /freebsd/sys/dev/aic7xxx/aic7xxx.seq (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1/*
2 * Adaptec 274x/284x/294x device driver firmware for Linux and FreeBSD.
3 *
4 * Copyright (c) 1994-2001 Justin Gibbs.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions, and the following disclaimer,
12 *    without modification.
13 * 2. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission.
15 *
16 * Alternatively, this software may be distributed under the terms of the
17 * GNU Public License ("GPL").
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * $Id: //depot/src/aic7xxx/aic7xxx.seq#27 $
32 *
33 * $FreeBSD$
34 */
35
36#include "aic7xxx.reg"
37#include "scsi_message.h"
38
39/*
40 * A few words on the waiting SCB list:
41 * After starting the selection hardware, we check for reconnecting targets
42 * as well as for our selection to complete just in case the reselection wins
43 * bus arbitration.  The problem with this is that we must keep track of the
44 * SCB that we've already pulled from the QINFIFO and started the selection
45 * on just in case the reselection wins so that we can retry the selection at
46 * a later time.  This problem cannot be resolved by holding a single entry
47 * in scratch ram since a reconnecting target can request sense and this will
48 * create yet another SCB waiting for selection.  The solution used here is to
49 * use byte 27 of the SCB as a psuedo-next pointer and to thread a list
50 * of SCBs that are awaiting selection.  Since 0-0xfe are valid SCB indexes,
51 * SCB_LIST_NULL is 0xff which is out of range.  An entry is also added to
52 * this list everytime a request sense occurs or after completing a non-tagged
53 * command for which a second SCB has been queued.  The sequencer will
54 * automatically consume the entries.
55 */
56
57bus_free_sel:
58	/*
59	 * Turn off the selection hardware.  We need to reset the
60	 * selection request in order to perform a new selection.
61	 */
62	and	SCSISEQ, TEMODE|ENSELI|ENRSELI|ENAUTOATNP, SCSISEQ;
63	and	SIMODE1, ~ENBUSFREE;
64poll_for_work:
65	call	clear_target_state;
66	and	SXFRCTL0, ~SPIOEN;
67	if ((ahc->features & AHC_ULTRA2) != 0) {
68		clr	SCSIBUSL;
69	}
70	test	SCSISEQ, ENSELO	jnz poll_for_selection;
71	if ((ahc->features & AHC_TWIN) != 0) {
72		xor	SBLKCTL,SELBUSB;	/* Toggle to the other bus */
73		test	SCSISEQ, ENSELO		jnz poll_for_selection;
74	}
75	cmp	WAITING_SCBH,SCB_LIST_NULL jne start_waiting;
76poll_for_work_loop:
77	if ((ahc->features & AHC_TWIN) != 0) {
78		xor	SBLKCTL,SELBUSB;	/* Toggle to the other bus */
79	}
80	test	SSTAT0, SELDO|SELDI	jnz selection;
81test_queue:
82	/* Has the driver posted any work for us? */
83BEGIN_CRITICAL
84	if ((ahc->features & AHC_QUEUE_REGS) != 0) {
85		test	QOFF_CTLSTA, SCB_AVAIL jz poll_for_work_loop;
86	} else {
87		mov	A, QINPOS;
88		cmp	KERNEL_QINPOS, A je poll_for_work_loop;
89	}
90	mov	ARG_1, NEXT_QUEUED_SCB;
91
92	/*
93	 * We have at least one queued SCB now and we don't have any
94	 * SCBs in the list of SCBs awaiting selection.  Allocate a
95	 * card SCB for the host's SCB and get to work on it.
96	 */
97	if ((ahc->flags & AHC_PAGESCBS) != 0) {
98		mov	ALLZEROS	call	get_free_or_disc_scb;
99	} else {
100		/* In the non-paging case, the SCBID == hardware SCB index */
101		mov	SCBPTR, ARG_1;
102	}
103	or	SEQ_FLAGS2, SCB_DMA;
104END_CRITICAL
105dma_queued_scb:
106	/*
107	 * DMA the SCB from host ram into the current SCB location.
108	 */
109	mvi	DMAPARAMS, HDMAEN|DIRECTION|FIFORESET;
110	mov	ARG_1	call dma_scb;
111	/*
112	 * Check one last time to see if this SCB was canceled
113	 * before we completed the DMA operation.  If it was,
114	 * the QINFIFO next pointer will not match our saved
115	 * value.
116	 */
117	mov	A, ARG_1;
118BEGIN_CRITICAL
119	cmp	NEXT_QUEUED_SCB, A jne abort_qinscb;
120	if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
121		cmp	SCB_TAG, A je . + 2;
122		mvi	SCB_MISMATCH call set_seqint;
123	}
124	mov	NEXT_QUEUED_SCB, SCB_NEXT;
125	mov	SCB_NEXT,WAITING_SCBH;
126	mov	WAITING_SCBH, SCBPTR;
127	if ((ahc->features & AHC_QUEUE_REGS) != 0) {
128		mov	NONE, SNSCB_QOFF;
129	} else {
130		inc	QINPOS;
131	}
132	and	SEQ_FLAGS2, ~SCB_DMA;
133END_CRITICAL
134start_waiting:
135	/*
136	 * Start the first entry on the waiting SCB list.
137	 */
138	mov	SCBPTR, WAITING_SCBH;
139	call	start_selection;
140
141poll_for_selection:
142	/*
143	 * Twin channel devices cannot handle things like SELTO
144	 * interrupts on the "background" channel.  So, while
145	 * selecting, keep polling the current channel until
146	 * either a selection or reselection occurs.
147	 */
148	test	SSTAT0, SELDO|SELDI	jz poll_for_selection;
149
150selection:
151	/*
152	 * We aren't expecting a bus free, so interrupt
153	 * the kernel driver if it happens.
154	 */
155	mvi	CLRSINT1,CLRBUSFREE;
156	if ((ahc->features & AHC_DT) == 0) {
157		or	SIMODE1, ENBUSFREE;
158	}
159
160	/*
161	 * Guard against a bus free after (re)selection
162	 * but prior to enabling the busfree interrupt.  SELDI
163	 * and SELDO will be cleared in that case.
164	 */
165	test	SSTAT0, SELDI|SELDO	jz bus_free_sel;
166	test	SSTAT0,SELDO	jnz select_out;
167select_in:
168	if ((ahc->flags & AHC_TARGETROLE) != 0) {
169		if ((ahc->flags & AHC_INITIATORROLE) != 0) {
170			test	SSTAT0, TARGET	jz initiator_reselect;
171		}
172		mvi	CLRSINT0, CLRSELDI;
173
174		/*
175		 * We've just been selected.  Assert BSY and
176		 * setup the phase for receiving messages
177		 * from the target.
178		 *
179		 * If bus reset interrupts have been disabled (from a
180		 * previous reset), re-enable them now.  Resets are only
181		 * of interest when we have outstanding transactions, so
182		 * we can safely defer re-enabling the interrupt until,
183		 * as a target, we start receiving transactions again.
184		 */
185		test	SIMODE1, ENSCSIRST	jnz . + 3;
186		mvi	CLRSINT1, CLRSCSIRSTI;
187		or	SIMODE1, ENSCSIRST;
188		mvi	SCSISIGO, P_MESGOUT|BSYO;
189
190		/*
191		 * Setup the DMA for sending the identify and
192		 * command information.
193		 */
194		or	SEQ_FLAGS, CMDPHASE_PENDING;
195
196		mov     A, TQINPOS;
197		if ((ahc->features & AHC_CMD_CHAN) != 0) {
198			mvi	DINDEX, CCHADDR;
199			mvi	SHARED_DATA_ADDR call set_32byte_addr;
200			mvi	CCSCBCTL, CCSCBRESET;
201		} else {
202			mvi	DINDEX, HADDR;
203			mvi	SHARED_DATA_ADDR call set_32byte_addr;
204			mvi	DFCNTRL, FIFORESET;
205		}
206
207		/* Initiator that selected us */
208		and	SAVED_SCSIID, SELID_MASK, SELID;
209		/* The Target ID we were selected at */
210		if ((ahc->features & AHC_MULTI_TID) != 0) {
211			and	A, OID, TARGIDIN;
212		} else if ((ahc->features & AHC_ULTRA2) != 0) {
213			and	A, OID, SCSIID_ULTRA2;
214		} else {
215			and	A, OID, SCSIID;
216		}
217		or	SAVED_SCSIID, A;
218		if ((ahc->features & AHC_TWIN) != 0) {
219			test 	SBLKCTL, SELBUSB jz . + 2;
220			or	SAVED_SCSIID, TWIN_CHNLB;
221		}
222		if ((ahc->features & AHC_CMD_CHAN) != 0) {
223			mov	CCSCBRAM, SAVED_SCSIID;
224		} else {
225			mov	DFDAT, SAVED_SCSIID;
226		}
227
228		/*
229		 * If ATN isn't asserted, the target isn't interested
230		 * in talking to us.  Go directly to bus free.
231		 * XXX SCSI-1 may require us to assume lun 0 if
232		 * ATN is false.
233		 */
234		test	SCSISIGI, ATNI	jz	target_busfree;
235
236		/*
237		 * Watch ATN closely now as we pull in messages from the
238		 * initiator.  We follow the guidlines from section 6.5
239		 * of the SCSI-2 spec for what messages are allowed when.
240		 */
241		call	target_inb;
242
243		/*
244		 * Our first message must be one of IDENTIFY, ABORT, or
245		 * BUS_DEVICE_RESET.
246		 */
247		test	DINDEX, MSG_IDENTIFYFLAG jz host_target_message_loop;
248		/* Store for host */
249		if ((ahc->features & AHC_CMD_CHAN) != 0) {
250			mov	CCSCBRAM, DINDEX;
251		} else {
252			mov	DFDAT, DINDEX;
253		}
254
255		/* Remember for disconnection decision */
256		test	DINDEX, MSG_IDENTIFY_DISCFLAG jnz . + 2;
257		/* XXX Honor per target settings too */
258		or	SEQ_FLAGS, NO_DISCONNECT;
259
260		test	SCSISIGI, ATNI	jz	ident_messages_done;
261		call	target_inb;
262		/*
263		 * If this is a tagged request, the tagged message must
264		 * immediately follow the identify.  We test for a valid
265		 * tag message by seeing if it is >= MSG_SIMPLE_Q_TAG and
266		 * < MSG_IGN_WIDE_RESIDUE.
267		 */
268		add	A, -MSG_SIMPLE_Q_TAG, DINDEX;
269		jnc	ident_messages_done;
270		add	A, -MSG_IGN_WIDE_RESIDUE, DINDEX;
271		jc	ident_messages_done;
272		/* Store for host */
273		if ((ahc->features & AHC_CMD_CHAN) != 0) {
274			mov	CCSCBRAM, DINDEX;
275		} else {
276			mov	DFDAT, DINDEX;
277		}
278
279		/*
280		 * If the initiator doesn't feel like providing a tag number,
281		 * we've got a failed selection and must transition to bus
282		 * free.
283		 */
284		test	SCSISIGI, ATNI	jz	target_busfree;
285
286		/*
287		 * Store the tag for the host.
288		 */
289		call	target_inb;
290		if ((ahc->features & AHC_CMD_CHAN) != 0) {
291			mov	CCSCBRAM, DINDEX;
292		} else {
293			mov	DFDAT, DINDEX;
294		}
295		mov	INITIATOR_TAG, DINDEX;
296		or	SEQ_FLAGS, TARGET_CMD_IS_TAGGED;
297		test	SCSISIGI, ATNI	jz . + 2;
298		/* Initiator still wants to give us messages */
299		call	target_inb;
300		jmp	ident_messages_done;
301
302		/*
303		 * Pushed message loop to allow the kernel to
304		 * run it's own target mode message state engine.
305		 */
306host_target_message_loop:
307		mvi	HOST_MSG_LOOP call set_seqint;
308		cmp	RETURN_1, EXIT_MSG_LOOP	je target_ITloop;
309		test	SSTAT0, SPIORDY jz .;
310		jmp	host_target_message_loop;
311
312ident_messages_done:
313		/* If ring buffer is full, return busy or queue full */
314		if ((ahc->features & AHC_HS_MAILBOX) != 0) {
315			and	A, HOST_TQINPOS, HS_MAILBOX;
316		} else {
317			mov	A, KERNEL_TQINPOS;
318		}
319		cmp	TQINPOS, A jne tqinfifo_has_space;
320		mvi	P_STATUS|BSYO call change_phase;
321		test	SEQ_FLAGS, TARGET_CMD_IS_TAGGED jz . + 3;
322		mvi	STATUS_QUEUE_FULL call target_outb;
323		jmp	target_busfree_wait;
324		mvi	STATUS_BUSY call target_outb;
325		jmp	target_busfree_wait;
326tqinfifo_has_space:
327		/* Terminate the ident list */
328		if ((ahc->features & AHC_CMD_CHAN) != 0) {
329			mvi	CCSCBRAM, SCB_LIST_NULL;
330		} else {
331			mvi	DFDAT, SCB_LIST_NULL;
332		}
333		or	SEQ_FLAGS, TARG_CMD_PENDING|IDENTIFY_SEEN;
334		test	SCSISIGI, ATNI	jnz target_mesgout_pending;
335		jmp	target_ITloop;
336	}
337
338if ((ahc->flags & AHC_INITIATORROLE) != 0) {
339/*
340 * Reselection has been initiated by a target. Make a note that we've been
341 * reselected, but haven't seen an IDENTIFY message from the target yet.
342 */
343initiator_reselect:
344	/* XXX test for and handle ONE BIT condition */
345	or	SXFRCTL0, SPIOEN|CLRSTCNT|CLRCHN;
346	and	SAVED_SCSIID, SELID_MASK, SELID;
347	if ((ahc->features & AHC_ULTRA2) != 0) {
348		and	A, OID, SCSIID_ULTRA2;
349	} else {
350		and	A, OID, SCSIID;
351	}
352	or	SAVED_SCSIID, A;
353	if ((ahc->features & AHC_TWIN) != 0) {
354		test	SBLKCTL, SELBUSB	jz . + 2;
355		or	SAVED_SCSIID, TWIN_CHNLB;
356	}
357	mvi	CLRSINT0, CLRSELDI;
358	jmp	ITloop;
359}
360
361abort_qinscb:
362	call	add_scb_to_free_list;
363	jmp	poll_for_work_loop;
364
365start_selection:
366	/*
367	 * If bus reset interrupts have been disabled (from a previous
368	 * reset), re-enable them now.  Resets are only of interest
369	 * when we have outstanding transactions, so we can safely
370	 * defer re-enabling the interrupt until, as an initiator,
371	 * we start sending out transactions again.
372	 */
373	test	SIMODE1, ENSCSIRST	jnz . + 3;
374	mvi	CLRSINT1, CLRSCSIRSTI;
375	or	SIMODE1, ENSCSIRST;
376	if ((ahc->features & AHC_TWIN) != 0) {
377		and	SINDEX,~SELBUSB,SBLKCTL;/* Clear channel select bit */
378		test	SCB_SCSIID, TWIN_CHNLB jz . + 2;
379		or	SINDEX, SELBUSB;
380		mov	SBLKCTL,SINDEX;		/* select channel */
381	}
382initialize_scsiid:
383	if ((ahc->features & AHC_ULTRA2) != 0) {
384		mov	SCSIID_ULTRA2, SCB_SCSIID;
385	} else if ((ahc->features & AHC_TWIN) != 0) {
386		and	SCSIID, TWIN_TID|OID, SCB_SCSIID;
387	} else {
388		mov	SCSIID, SCB_SCSIID;
389	}
390	if ((ahc->flags & AHC_TARGETROLE) != 0) {
391		mov	SINDEX, SCSISEQ_TEMPLATE;
392		test	SCB_CONTROL, TARGET_SCB jz . + 2;
393		or	SINDEX, TEMODE;
394		mov	SCSISEQ, SINDEX ret;
395	} else {
396		mov	SCSISEQ, SCSISEQ_TEMPLATE ret;
397	}
398
399/*
400 * Initialize transfer settings and clear the SCSI channel.
401 * SINDEX should contain any additional bit's the client wants
402 * set in SXFRCTL0.  We also assume that the current SCB is
403 * a valid SCB for the target we wish to talk to.
404 */
405initialize_channel:
406	or	SXFRCTL0, SPIOEN|CLRSTCNT|CLRCHN;
407set_transfer_settings:
408	if ((ahc->features & AHC_ULTRA) != 0) {
409		test	SCB_CONTROL, ULTRAENB jz . + 2;
410		or	SXFRCTL0, FAST20;
411	}
412	/*
413	 * Initialize SCSIRATE with the appropriate value for this target.
414	 */
415	if ((ahc->features & AHC_ULTRA2) != 0) {
416		bmov	SCSIRATE, SCB_SCSIRATE, 2 ret;
417	} else {
418		mov	SCSIRATE, SCB_SCSIRATE ret;
419	}
420
421if ((ahc->flags & AHC_TARGETROLE) != 0) {
422/*
423 * We carefully toggle SPIOEN to allow us to return the
424 * message byte we receive so it can be checked prior to
425 * driving REQ on the bus for the next byte.
426 */
427target_inb:
428	/*
429	 * Drive REQ on the bus by enabling SCSI PIO.
430	 */
431	or	SXFRCTL0, SPIOEN;
432	/* Wait for the byte */
433	test	SSTAT0, SPIORDY jz .;
434	/* Prevent our read from triggering another REQ */
435	and	SXFRCTL0, ~SPIOEN;
436	/* Save latched contents */
437	mov	DINDEX, SCSIDATL ret;
438}
439
440/*
441 * After the selection, remove this SCB from the "waiting SCB"
442 * list.  This is achieved by simply moving our "next" pointer into
443 * WAITING_SCBH.  Our next pointer will be set to null the next time this
444 * SCB is used, so don't bother with it now.
445 */
446select_out:
447	/* Turn off the selection hardware */
448	and	SCSISEQ, TEMODE|ENSELI|ENRSELI|ENAUTOATNP, SCSISEQ;
449	mvi	CLRSINT0, CLRSELDO;
450	mov	SCBPTR, WAITING_SCBH;
451	mov	WAITING_SCBH,SCB_NEXT;
452	mov	SAVED_SCSIID, SCB_SCSIID;
453	mov	SAVED_LUN, SCB_LUN;
454	call	initialize_channel;
455	if ((ahc->flags & AHC_TARGETROLE) != 0) {
456		test	SSTAT0, TARGET	jz initiator_select;
457
458		/*
459		 * We've just re-selected an initiator.
460		 * Assert BSY and setup the phase for
461		 * sending our identify messages.
462		 */
463		mvi	P_MESGIN|BSYO call change_phase;
464
465		/*
466		 * Start out with a simple identify message.
467		 */
468		or	SCB_LUN, MSG_IDENTIFYFLAG call target_outb;
469
470		/*
471		 * If we are the result of a tagged command, send
472		 * a simple Q tag and the tag id.
473		 */
474		test	SCB_CONTROL, TAG_ENB	jz . + 3;
475		mvi	MSG_SIMPLE_Q_TAG call target_outb;
476		mov	SCB_TARGET_INFO[SCB_INITIATOR_TAG] call target_outb;
477target_synccmd:
478		/*
479		 * Now determine what phases the host wants us
480		 * to go through.
481		 */
482		mov	SEQ_FLAGS, SCB_TARGET_INFO[SCB_TARGET_PHASES];
483
484		test	SCB_CONTROL, MK_MESSAGE	jz target_ITloop;
485		mvi	P_MESGIN|BSYO call change_phase;
486		jmp	host_target_message_loop;
487target_ITloop:
488		/*
489		 * Start honoring ATN signals now that
490		 * we properly identified ourselves.
491		 */
492		test	SCSISIGI, ATNI			jnz target_mesgout;
493		test	SEQ_FLAGS, CMDPHASE_PENDING	jnz target_cmdphase;
494		test	SEQ_FLAGS, DPHASE_PENDING	jnz target_dphase;
495		test	SEQ_FLAGS, SPHASE_PENDING	jnz target_sphase;
496
497		/*
498		 * No more work to do.  Either disconnect or not depending
499		 * on the state of NO_DISCONNECT.
500		 */
501		test	SEQ_FLAGS, NO_DISCONNECT jz target_disconnect;
502		mov	RETURN_1, ALLZEROS;
503		call	complete_target_cmd;
504		cmp	RETURN_1, CONT_MSG_LOOP jne .;
505		if ((ahc->flags & AHC_PAGESCBS) != 0) {
506			mov	ALLZEROS	call	get_free_or_disc_scb;
507		}
508		mvi	DMAPARAMS, HDMAEN|DIRECTION|FIFORESET;
509		mov	SCB_TAG	 call dma_scb;
510		jmp	target_synccmd;
511
512target_mesgout:
513		mvi	SCSISIGO, P_MESGOUT|BSYO;
514target_mesgout_continue:
515		call	target_inb;
516target_mesgout_pending:
517		/* Local Processing goes here... */
518		jmp	host_target_message_loop;
519
520target_disconnect:
521		mvi	P_MESGIN|BSYO call change_phase;
522		test	SEQ_FLAGS, DPHASE	jz . + 2;
523		mvi	MSG_SAVEDATAPOINTER call target_outb;
524		mvi	MSG_DISCONNECT call target_outb;
525
526target_busfree_wait:
527		/* Wait for preceding I/O session to complete. */
528		test	SCSISIGI, ACKI jnz .;
529target_busfree:
530		and	SIMODE1, ~ENBUSFREE;
531		if ((ahc->features & AHC_ULTRA2) != 0) {
532			clr	SCSIBUSL;
533		}
534		clr	SCSISIGO;
535		mvi	LASTPHASE, P_BUSFREE;
536		call	complete_target_cmd;
537		jmp	poll_for_work;
538
539target_cmdphase:
540		mvi	P_COMMAND|BSYO call change_phase;
541		call	target_inb;
542		mov	A, DINDEX;
543		/* Store for host */
544		if ((ahc->features & AHC_CMD_CHAN) != 0) {
545			mov	CCSCBRAM, A;
546		} else {
547			mov	DFDAT, A;
548		}
549
550		/*
551		 * Determine the number of bytes to read
552		 * based on the command group code via table lookup.
553		 * We reuse the first 8 bytes of the TARG_SCSIRATE
554		 * BIOS array for this table. Count is one less than
555		 * the total for the command since we've already fetched
556		 * the first byte.
557		 */
558		shr	A, CMD_GROUP_CODE_SHIFT;
559		add	SINDEX, CMDSIZE_TABLE, A;
560		mov	A, SINDIR;
561
562		test	A, 0xFF jz command_phase_done;
563		or	SXFRCTL0, SPIOEN;
564command_loop:
565		test	SSTAT0, SPIORDY jz .;
566		cmp	A, 1 jne . + 2;
567		and	SXFRCTL0, ~SPIOEN;	/* Last Byte */
568		if ((ahc->features & AHC_CMD_CHAN) != 0) {
569			mov	CCSCBRAM, SCSIDATL;
570		} else {
571			mov	DFDAT, SCSIDATL;
572		}
573		dec	A;
574		test	A, 0xFF jnz command_loop;
575
576command_phase_done:
577		and	SEQ_FLAGS, ~CMDPHASE_PENDING;
578		jmp	target_ITloop;
579
580target_dphase:
581		/*
582		 * Data phases on the bus are from the
583		 * perspective of the initiator.  The dma
584		 * code looks at LASTPHASE to determine the
585		 * data direction of the DMA.  Toggle it for
586		 * target transfers.
587		 */
588		xor	LASTPHASE, IOI, SCB_TARGET_INFO[SCB_TARGET_DATA_DIR];
589		or	SCB_TARGET_INFO[SCB_TARGET_DATA_DIR], BSYO
590			call change_phase;
591		jmp	p_data;
592
593target_sphase:
594		mvi	P_STATUS|BSYO call change_phase;
595		mvi	LASTPHASE, P_STATUS;
596		mov	SCB_TARGET_INFO[SCB_TARGET_STATUS] call target_outb;
597		/* XXX Watch for ATN or parity errors??? */
598		mvi	SCSISIGO, P_MESGIN|BSYO;
599		/* MSG_CMDCMPLT is 0, but we can't do an immediate of 0 */
600		mov	ALLZEROS call target_outb;
601		jmp	target_busfree_wait;
602
603complete_target_cmd:
604		test	SEQ_FLAGS, TARG_CMD_PENDING	jnz . + 2;
605		mov	SCB_TAG jmp complete_post;
606		if ((ahc->features & AHC_CMD_CHAN) != 0) {
607			/* Set the valid byte */
608			mvi	CCSCBADDR, 24;
609			mov	CCSCBRAM, ALLONES;
610			mvi	CCHCNT, 28;
611			or	CCSCBCTL, CCSCBEN|CCSCBRESET;
612			test	CCSCBCTL, CCSCBDONE jz .;
613			clr	CCSCBCTL;
614		} else {
615			/* Set the valid byte */
616			or	DFCNTRL, FIFORESET;
617			mvi	DFWADDR, 3; /* Third 64bit word or byte 24 */
618			mov	DFDAT, ALLONES;
619			mvi	28	call set_hcnt;
620			or	DFCNTRL, HDMAEN|FIFOFLUSH;
621			call	dma_finish;
622		}
623		inc	TQINPOS;
624		mvi	INTSTAT,CMDCMPLT ret;
625	}
626
627if ((ahc->flags & AHC_INITIATORROLE) != 0) {
628initiator_select:
629	/*
630	 * As soon as we get a successful selection, the target
631	 * should go into the message out phase since we have ATN
632	 * asserted.
633	 */
634	mvi	MSG_OUT, MSG_IDENTIFYFLAG;
635	or	SEQ_FLAGS, IDENTIFY_SEEN;
636
637	/*
638	 * Main loop for information transfer phases.  Wait for the
639	 * target to assert REQ before checking MSG, C/D and I/O for
640	 * the bus phase.
641	 */
642mesgin_phasemis:
643ITloop:
644	call	phase_lock;
645
646	mov	A, LASTPHASE;
647
648	test	A, ~P_DATAIN	jz p_data;
649	cmp	A,P_COMMAND	je p_command;
650	cmp	A,P_MESGOUT	je p_mesgout;
651	cmp	A,P_STATUS	je p_status;
652	cmp	A,P_MESGIN	je p_mesgin;
653
654	mvi	BAD_PHASE call set_seqint;
655	jmp	ITloop;			/* Try reading the bus again. */
656
657await_busfree:
658	and	SIMODE1, ~ENBUSFREE;
659	mov	NONE, SCSIDATL;		/* Ack the last byte */
660	if ((ahc->features & AHC_ULTRA2) != 0) {
661		clr	SCSIBUSL;	/* Prevent bit leakage durint SELTO */
662	}
663	and	SXFRCTL0, ~SPIOEN;
664	test	SSTAT1,REQINIT|BUSFREE	jz .;
665	test	SSTAT1, BUSFREE jnz poll_for_work;
666	mvi	MISSED_BUSFREE call set_seqint;
667}
668
669clear_target_state:
670	/*
671	 * We assume that the kernel driver may reset us
672	 * at any time, even in the middle of a DMA, so
673	 * clear DFCNTRL too.
674	 */
675	clr	DFCNTRL;
676	or	SXFRCTL0, CLRSTCNT|CLRCHN;
677
678	/*
679	 * We don't know the target we will connect to,
680	 * so default to narrow transfers to avoid
681	 * parity problems.
682	 */
683	if ((ahc->features & AHC_ULTRA2) != 0) {
684		bmov	SCSIRATE, ALLZEROS, 2;
685	} else {
686		clr	SCSIRATE;
687		if ((ahc->features & AHC_ULTRA) != 0) {
688			and	SXFRCTL0, ~(FAST20);
689		}
690	}
691	mvi	LASTPHASE, P_BUSFREE;
692	/* clear target specific flags */
693	clr	SEQ_FLAGS ret;
694
695sg_advance:
696	clr	A;			/* add sizeof(struct scatter) */
697	add	SCB_RESIDUAL_SGPTR[0],SG_SIZEOF;
698	adc	SCB_RESIDUAL_SGPTR[1],A;
699	adc	SCB_RESIDUAL_SGPTR[2],A;
700	adc	SCB_RESIDUAL_SGPTR[3],A ret;
701
702if ((ahc->features & AHC_CMD_CHAN) != 0) {
703disable_ccsgen:
704	test	CCSGCTL, CCSGEN jz return;
705	test	CCSGCTL, CCSGDONE jz .;
706disable_ccsgen_fetch_done:
707	clr	CCSGCTL;
708	test	CCSGCTL, CCSGEN jnz .;
709	ret;
710idle_loop:
711	/*
712	 * Do we need any more segments for this transfer?
713	 */
714	test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jnz return;
715
716	/* Did we just finish fetching segs? */
717	cmp	CCSGCTL, CCSGEN|CCSGDONE je idle_sgfetch_complete;
718
719	/* Are we actively fetching segments? */
720	test	CCSGCTL, CCSGEN jnz return;
721
722	/*
723	 * Do we have any prefetch left???
724	 */
725	cmp	CCSGADDR, SG_PREFETCH_CNT jne idle_sg_avail;
726
727	/*
728	 * Need to fetch segments, but we can only do that
729	 * if the command channel is completely idle.  Make
730	 * sure we don't have an SCB prefetch going on.
731	 */
732	test	CCSCBCTL, CCSCBEN jnz return;
733
734	/*
735	 * We fetch a "cacheline aligned" and sized amount of data
736	 * so we don't end up referencing a non-existant page.
737	 * Cacheline aligned is in quotes because the kernel will
738	 * set the prefetch amount to a reasonable level if the
739	 * cacheline size is unknown.
740	 */
741	mvi	CCHCNT, SG_PREFETCH_CNT;
742	and	CCHADDR[0], SG_PREFETCH_ALIGN_MASK, SCB_RESIDUAL_SGPTR;
743	bmov	CCHADDR[1], SCB_RESIDUAL_SGPTR[1], 3;
744	mvi	CCSGCTL, CCSGEN|CCSGRESET ret;
745idle_sgfetch_complete:
746	call	disable_ccsgen_fetch_done;
747	and	CCSGADDR, SG_PREFETCH_ADDR_MASK, SCB_RESIDUAL_SGPTR;
748idle_sg_avail:
749	if ((ahc->features & AHC_ULTRA2) != 0) {
750		/* Does the hardware have space for another SG entry? */
751		test	DFSTATUS, PRELOAD_AVAIL jz return;
752		bmov 	HADDR, CCSGRAM, 4;
753		bmov	SINDEX, CCSGRAM, 1;
754		test	SINDEX, 0x1 jz . + 2;
755		xor	DATA_COUNT_ODD, 0x1;
756		bmov	HCNT[0], SINDEX, 1;
757		bmov	HCNT[1], CCSGRAM, 2;
758		bmov	SCB_RESIDUAL_DATACNT[3], CCSGRAM, 1;
759		call	sg_advance;
760		mov	SINDEX, SCB_RESIDUAL_SGPTR[0];
761		test	DATA_COUNT_ODD, 0x1 jz . + 2;
762		or	SINDEX, ODD_SEG;
763		test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz . + 2;
764		or	SINDEX, LAST_SEG;
765		mov	SG_CACHE_PRE, SINDEX;
766		/* Load the segment */
767		or	DFCNTRL, PRELOADEN;
768	}
769	ret;
770}
771
772if ((ahc->bugs & AHC_PCI_MWI_BUG) != 0 && ahc->pci_cachesize != 0) {
773/*
774 * Calculate the trailing portion of this S/G segment that cannot
775 * be transferred using memory write and invalidate PCI transactions.
776 * XXX Can we optimize this for PCI writes only???
777 */
778calc_mwi_residual:
779	/*
780	 * If the ending address is on a cacheline boundary,
781	 * there is no need for an extra segment.
782	 */
783	mov	A, HCNT[0];
784	add	A, A, HADDR[0];
785	and	A, CACHESIZE_MASK;
786	test	A, 0xFF jz return;
787
788	/*
789	 * If the transfer is less than a cachline,
790	 * there is no need for an extra segment.
791	 */
792	test	HCNT[1], 0xFF	jnz calc_mwi_residual_final;
793	test	HCNT[2], 0xFF	jnz calc_mwi_residual_final;
794	add	NONE, INVERTED_CACHESIZE_MASK, HCNT[0];
795	jnc	return;
796
797calc_mwi_residual_final:
798	mov	MWI_RESIDUAL, A;
799	not	A;
800	inc	A;
801	add	HCNT[0], A;
802	adc	HCNT[1], -1;
803	adc	HCNT[2], -1 ret;
804}
805
806/*
807 * If we re-enter the data phase after going through another phase, the
808 * STCNT may have been cleared, so restore it from the residual field.
809 */
810data_phase_reinit:
811	if ((ahc->features & AHC_ULTRA2) != 0) {
812		/*
813		 * The preload circuitry requires us to
814		 * reload the address too, so pull it from
815		 * the shaddow address.
816		 */
817		bmov	HADDR, SHADDR, 4;
818		bmov	HCNT, SCB_RESIDUAL_DATACNT, 3;
819	} else if ((ahc->features & AHC_CMD_CHAN) != 0) {
820		bmov	STCNT, SCB_RESIDUAL_DATACNT, 3;
821	} else {
822		mvi	DINDEX, STCNT;
823		mvi	SCB_RESIDUAL_DATACNT call bcopy_3;
824	}
825	and	DATA_COUNT_ODD, 0x1, SCB_RESIDUAL_DATACNT[0];
826	jmp	data_phase_loop;
827
828p_data:
829	if ((ahc->features & AHC_ULTRA2) != 0) {
830		mvi	DMAPARAMS, PRELOADEN|SCSIEN|HDMAEN;
831	} else {
832		mvi	DMAPARAMS, WIDEODD|SCSIEN|SDMAEN|HDMAEN|FIFORESET;
833	}
834	test	LASTPHASE, IOI jnz . + 2;
835	or	DMAPARAMS, DIRECTION;
836	call	assert;			/*
837					 * Ensure entering a data
838					 * phase is okay - seen identify, etc.
839					 */
840	if ((ahc->features & AHC_CMD_CHAN) != 0) {
841		/* We don't have any valid S/G elements */
842		mvi	CCSGADDR, SG_PREFETCH_CNT;
843	}
844	test	SEQ_FLAGS, DPHASE	jnz data_phase_reinit;
845
846	/* We have seen a data phase */
847	or	SEQ_FLAGS, DPHASE;
848
849	/*
850	 * Initialize the DMA address and counter from the SCB.
851	 * Also set SCB_RESIDUAL_SGPTR, including the LAST_SEG
852	 * flag in the highest byte of the data count.  We cannot
853	 * modify the saved values in the SCB until we see a save
854	 * data pointers message.
855	 */
856	if ((ahc->features & AHC_CMD_CHAN) != 0) {
857		bmov	HADDR, SCB_DATAPTR, 7;
858		bmov	SCB_RESIDUAL_DATACNT[3], SCB_DATACNT[3], 5;
859	} else {
860		mvi	DINDEX, HADDR;
861		mvi	SCB_DATAPTR	call bcopy_7;
862		mvi	DINDEX, SCB_RESIDUAL_DATACNT + 3;
863		mvi	SCB_DATACNT + 3 call bcopy_5;
864	}
865	if ((ahc->bugs & AHC_PCI_MWI_BUG) != 0 && ahc->pci_cachesize != 0) {
866		call	calc_mwi_residual;
867	}
868	and	SCB_RESIDUAL_SGPTR[0], ~SG_FULL_RESID;
869	and	DATA_COUNT_ODD, 0x1, HCNT[0];
870
871	if ((ahc->features & AHC_ULTRA2) == 0) {
872		if ((ahc->features & AHC_CMD_CHAN) != 0) {
873			bmov	STCNT, HCNT, 3;
874		} else {
875			call	set_stcnt_from_hcnt;
876		}
877	}
878
879data_phase_loop:
880	/* Guard against overruns */
881	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz data_phase_inbounds;
882
883	/*
884	 * Turn on `Bit Bucket' mode, wait until the target takes
885	 * us to another phase, and then notify the host.
886	 */
887	and	DMAPARAMS, DIRECTION;
888	mov	DFCNTRL, DMAPARAMS;
889	or	SXFRCTL1,BITBUCKET;
890	if ((ahc->features & AHC_DT) == 0) {
891		test	SSTAT1,PHASEMIS	jz .;
892	} else {
893		test	SCSIPHASE, DATA_PHASE_MASK jnz .;
894	}
895	and	SXFRCTL1, ~BITBUCKET;
896	mvi	DATA_OVERRUN call set_seqint;
897	jmp	ITloop;
898
899data_phase_inbounds:
900	if ((ahc->features & AHC_ULTRA2) != 0) {
901		mov	SINDEX, SCB_RESIDUAL_SGPTR[0];
902		test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz . + 2;
903		or	SINDEX, LAST_SEG;
904		test	DATA_COUNT_ODD, 0x1 jz . + 2;
905		or	SINDEX, ODD_SEG;
906		mov	SG_CACHE_PRE, SINDEX;
907		mov	DFCNTRL, DMAPARAMS;
908ultra2_dma_loop:
909		call	idle_loop;
910		/*
911		 * The transfer is complete if either the last segment
912		 * completes or the target changes phase.
913		 */
914		test	SG_CACHE_SHADOW, LAST_SEG_DONE jnz ultra2_dmafinish;
915		if ((ahc->features & AHC_DT) == 0) {
916			if ((ahc->flags & AHC_TARGETROLE) != 0) {
917				 /*
918				  * As a target, we control the phases,
919				  * so ignore PHASEMIS.
920				  */
921				test	SSTAT0, TARGET jnz ultra2_dma_loop;
922			}
923			if ((ahc->flags & AHC_INITIATORROLE) != 0) {
924				test	SSTAT1,PHASEMIS	jz ultra2_dma_loop;
925			}
926		} else {
927			test	DFCNTRL, SCSIEN jnz ultra2_dma_loop;
928		}
929
930ultra2_dmafinish:
931		/*
932		 * The transfer has terminated either due to a phase
933		 * change, and/or the completion of the last segment.
934		 * We have two goals here.  Do as much other work
935		 * as possible while the data fifo drains on a read
936		 * and respond as quickly as possible to the standard
937		 * messages (save data pointers/disconnect and command
938		 * complete) that usually follow a data phase.
939		 */
940		if ((ahc->bugs & AHC_AUTOFLUSH_BUG) != 0) {
941			/*
942			 * On chips with broken auto-flush, start
943			 * the flushing process now.  We'll poke
944			 * the chip from time to time to keep the
945			 * flush process going as we complete the
946			 * data phase.
947			 */
948			or	DFCNTRL, FIFOFLUSH;
949		}
950		/*
951		 * We assume that, even though data may still be
952		 * transferring to the host, that the SCSI side of
953		 * the DMA engine is now in a static state.  This
954		 * allows us to update our notion of where we are
955		 * in this transfer.
956		 *
957		 * If, by chance, we stopped before being able
958		 * to fetch additional segments for this transfer,
959		 * yet the last S/G was completely exhausted,
960		 * call our idle loop until it is able to load
961		 * another segment.  This will allow us to immediately
962		 * pickup on the next segment on the next data phase.
963		 *
964		 * If we happened to stop on the last segment, then
965		 * our residual information is still correct from
966		 * the idle loop and there is no need to perform
967		 * any fixups.
968		 */
969ultra2_ensure_sg:
970		test	SG_CACHE_SHADOW, LAST_SEG jz ultra2_shvalid;
971		/* Record if we've consumed all S/G entries */
972		test    SSTAT2, SHVALID	jnz residuals_correct;
973		or	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL;
974		jmp	residuals_correct;
975
976ultra2_shvalid:
977                test    SSTAT2, SHVALID	jnz sgptr_fixup;
978		call	idle_loop;
979		jmp	ultra2_ensure_sg;
980
981sgptr_fixup:
982		/*
983		 * Fixup the residual next S/G pointer.  The S/G preload
984		 * feature of the chip allows us to load two elements
985		 * in addition to the currently active element.  We
986		 * store the bottom byte of the next S/G pointer in
987		 * the SG_CACEPTR register so we can restore the
988		 * correct value when the DMA completes.  If the next
989		 * sg ptr value has advanced to the point where higher
990		 * bytes in the address have been affected, fix them
991		 * too.
992		 */
993		test	SG_CACHE_SHADOW, 0x80 jz sgptr_fixup_done;
994		test	SCB_RESIDUAL_SGPTR[0], 0x80 jnz sgptr_fixup_done;
995		add	SCB_RESIDUAL_SGPTR[1], -1;
996		adc	SCB_RESIDUAL_SGPTR[2], -1;
997		adc	SCB_RESIDUAL_SGPTR[3], -1;
998sgptr_fixup_done:
999		and	SCB_RESIDUAL_SGPTR[0], SG_ADDR_MASK, SG_CACHE_SHADOW;
1000		clr	DATA_COUNT_ODD;
1001		test	SG_CACHE_SHADOW, ODD_SEG jz . + 2;
1002		or	DATA_COUNT_ODD, 0x1;
1003		clr	SCB_RESIDUAL_DATACNT[3]; /* We are not the last seg */
1004residuals_correct:
1005		/*
1006		 * Go ahead and shut down the DMA engine now.
1007		 * In the future, we'll want to handle end of
1008		 * transfer messages prior to doing this, but this
1009		 * requires similar restructuring for pre-ULTRA2
1010		 * controllers.
1011		 */
1012		test	DMAPARAMS, DIRECTION jnz ultra2_fifoempty;
1013ultra2_fifoflush:
1014		if ((ahc->features & AHC_DT) == 0) {
1015			if ((ahc->bugs & AHC_AUTOFLUSH_BUG) != 0) {
1016				/*
1017				 * On Rev A of the aic7890, the autoflush
1018				 * feature doesn't function correctly.
1019				 * Perform an explicit manual flush.  During
1020				 * a manual flush, the FIFOEMP bit becomes
1021				 * true every time the PCI FIFO empties
1022				 * regardless of the state of the SCSI FIFO.
1023				 * It can take up to 4 clock cycles for the
1024				 * SCSI FIFO to get data into the PCI FIFO
1025				 * and for FIFOEMP to de-assert.  Here we
1026				 * guard against this condition by making
1027				 * sure the FIFOEMP bit stays on for 5 full
1028				 * clock cycles.
1029				 */
1030				or	DFCNTRL, FIFOFLUSH;
1031				test	DFSTATUS, FIFOEMP jz ultra2_fifoflush;
1032				test	DFSTATUS, FIFOEMP jz ultra2_fifoflush;
1033				test	DFSTATUS, FIFOEMP jz ultra2_fifoflush;
1034				test	DFSTATUS, FIFOEMP jz ultra2_fifoflush;
1035			}
1036			test	DFSTATUS, FIFOEMP jz ultra2_fifoflush;
1037		} else {
1038			/*
1039			 * We enable the auto-ack feature on DT capable
1040			 * controllers.  This means that the controller may
1041			 * have already transferred some overrun bytes into
1042			 * the data FIFO and acked them on the bus.  The only
1043			 * way to detect this situation is to wait for
1044			 * LAST_SEG_DONE to come true on a completed transfer
1045			 * and then test to see if the data FIFO is non-empty.
1046			 */
1047			test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz . + 4;
1048			test	SG_CACHE_SHADOW, LAST_SEG_DONE jz .;
1049			test	DFSTATUS, FIFOEMP jnz ultra2_fifoempty;
1050			/* Overrun */
1051			jmp	data_phase_loop;
1052			test	DFSTATUS, FIFOEMP jz .;
1053		}
1054ultra2_fifoempty:
1055		/* Don't clobber an inprogress host data transfer */
1056		test	DFSTATUS, MREQPEND	jnz ultra2_fifoempty;
1057ultra2_dmahalt:
1058		and     DFCNTRL, ~(SCSIEN|HDMAEN);
1059		test	DFCNTRL, SCSIEN|HDMAEN jnz .;
1060	} else {
1061		/* If we are the last SG block, tell the hardware. */
1062		if ((ahc->bugs & AHC_PCI_MWI_BUG) != 0
1063		  && ahc->pci_cachesize != 0) {
1064			test	MWI_RESIDUAL, 0xFF jnz dma_mid_sg;
1065		}
1066		test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz dma_mid_sg;
1067		if ((ahc->flags & AHC_TARGETROLE) != 0) {
1068			test	SSTAT0, TARGET jz dma_last_sg;
1069			if ((ahc->flags & AHC_TMODE_WIDEODD_BUG) != 0) {
1070				test	DMAPARAMS, DIRECTION jz dma_mid_sg;
1071			}
1072		}
1073dma_last_sg:
1074		and	DMAPARAMS, ~WIDEODD;
1075dma_mid_sg:
1076		/* Start DMA data transfer. */
1077		mov	DFCNTRL, DMAPARAMS;
1078dma_loop:
1079		if ((ahc->features & AHC_CMD_CHAN) != 0) {
1080			call	idle_loop;
1081		}
1082		test	SSTAT0,DMADONE	jnz dma_dmadone;
1083		test	SSTAT1,PHASEMIS	jz dma_loop;	/* ie. underrun */
1084dma_phasemis:
1085		/*
1086		 * We will be "done" DMAing when the transfer count goes to
1087		 * zero, or the target changes the phase (in light of this,
1088		 * it makes sense that the DMA circuitry doesn't ACK when
1089		 * PHASEMIS is active).  If we are doing a SCSI->Host transfer,
1090		 * the data FIFO should be flushed auto-magically on STCNT=0
1091		 * or a phase change, so just wait for FIFO empty status.
1092		 */
1093dma_checkfifo:
1094		test	DFCNTRL,DIRECTION	jnz dma_fifoempty;
1095dma_fifoflush:
1096		test	DFSTATUS,FIFOEMP	jz dma_fifoflush;
1097dma_fifoempty:
1098		/* Don't clobber an inprogress host data transfer */
1099		test	DFSTATUS, MREQPEND	jnz dma_fifoempty;
1100
1101		/*
1102		 * Now shut off the DMA and make sure that the DMA
1103		 * hardware has actually stopped.  Touching the DMA
1104		 * counters, etc. while a DMA is active will result
1105		 * in an ILLSADDR exception.
1106		 */
1107dma_dmadone:
1108		and	DFCNTRL, ~(SCSIEN|SDMAEN|HDMAEN);
1109dma_halt:
1110		/*
1111		 * Some revisions of the aic78XX have a problem where, if the
1112		 * data fifo is full, but the PCI input latch is not empty,
1113		 * HDMAEN cannot be cleared.  The fix used here is to drain
1114		 * the prefetched but unused data from the data fifo until
1115		 * there is space for the input latch to drain.
1116		 */
1117		if ((ahc->bugs & AHC_PCI_2_1_RETRY_BUG) != 0) {
1118			mov	NONE, DFDAT;
1119		}
1120		test	DFCNTRL, (SCSIEN|SDMAEN|HDMAEN) jnz dma_halt;
1121
1122		/* See if we have completed this last segment */
1123		test	STCNT[0], 0xff	jnz data_phase_finish;
1124		test	STCNT[1], 0xff	jnz data_phase_finish;
1125		test	STCNT[2], 0xff	jnz data_phase_finish;
1126
1127		/*
1128		 * Advance the scatter-gather pointers if needed
1129		 */
1130		if ((ahc->bugs & AHC_PCI_MWI_BUG) != 0
1131		  && ahc->pci_cachesize != 0) {
1132			test	MWI_RESIDUAL, 0xFF jz no_mwi_resid;
1133			/*
1134			 * Reload HADDR from SHADDR and setup the
1135			 * count to be the size of our residual.
1136			 */
1137			if ((ahc->features & AHC_CMD_CHAN) != 0) {
1138				bmov	HADDR, SHADDR, 4;
1139				mov	HCNT, MWI_RESIDUAL;
1140				bmov	HCNT[1], ALLZEROS, 2;
1141			} else {
1142				mvi	DINDEX, HADDR;
1143				mvi	SHADDR call bcopy_4;
1144				mov	MWI_RESIDUAL call set_hcnt;
1145			}
1146			clr	MWI_RESIDUAL;
1147			jmp	sg_load_done;
1148no_mwi_resid:
1149		}
1150		test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz sg_load;
1151		or	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL;
1152		jmp	data_phase_finish;
1153sg_load:
1154		/*
1155		 * Load the next SG element's data address and length
1156		 * into the DMA engine.  If we don't have hardware
1157		 * to perform a prefetch, we'll have to fetch the
1158		 * segment from host memory first.
1159		 */
1160		if ((ahc->features & AHC_CMD_CHAN) != 0) {
1161			/* Wait for the idle loop to complete */
1162			test	CCSGCTL, CCSGEN jz . + 3;
1163			call	idle_loop;
1164			test	CCSGCTL, CCSGEN jnz . - 1;
1165			bmov 	HADDR, CCSGRAM, 7;
1166			test	CCSGRAM, SG_LAST_SEG jz . + 2;
1167			or	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG;
1168		} else {
1169			mvi	DINDEX, HADDR;
1170			mvi	SCB_RESIDUAL_SGPTR	call bcopy_4;
1171
1172			mvi	SG_SIZEOF	call set_hcnt;
1173
1174			or	DFCNTRL, HDMAEN|DIRECTION|FIFORESET;
1175
1176			call	dma_finish;
1177
1178			mvi	DINDEX, HADDR;
1179			call	dfdat_in_7;
1180			mov	SCB_RESIDUAL_DATACNT[3], DFDAT;
1181		}
1182
1183		if ((ahc->bugs & AHC_PCI_MWI_BUG) != 0
1184		  && ahc->pci_cachesize != 0) {
1185			call calc_mwi_residual;
1186		}
1187
1188		/* Point to the new next sg in memory */
1189		call	sg_advance;
1190
1191sg_load_done:
1192		if ((ahc->features & AHC_CMD_CHAN) != 0) {
1193			bmov	STCNT, HCNT, 3;
1194		} else {
1195			call	set_stcnt_from_hcnt;
1196		}
1197		/* Track odd'ness */
1198		test	HCNT[0], 0x1 jz . + 2;
1199		xor	DATA_COUNT_ODD, 0x1;
1200
1201		if ((ahc->flags & AHC_TARGETROLE) != 0) {
1202			test	SSTAT0, TARGET jnz data_phase_loop;
1203		}
1204	}
1205data_phase_finish:
1206	/*
1207	 * If the target has left us in data phase, loop through
1208	 * the dma code again.  In the case of ULTRA2 adapters,
1209	 * we should only loop if there is a data overrun.  For
1210	 * all other adapters, we'll loop after each S/G element
1211	 * is loaded as well as if there is an overrun.
1212	 */
1213	if ((ahc->flags & AHC_TARGETROLE) != 0) {
1214		test	SSTAT0, TARGET jnz data_phase_done;
1215	}
1216	if ((ahc->flags & AHC_INITIATORROLE) != 0) {
1217		test	SSTAT1, REQINIT jz .;
1218		if ((ahc->features & AHC_DT) == 0) {
1219			test	SSTAT1,PHASEMIS	jz data_phase_loop;
1220		} else {
1221			test	SCSIPHASE, DATA_PHASE_MASK jnz data_phase_loop;
1222		}
1223	}
1224
1225data_phase_done:
1226	/*
1227	 * After a DMA finishes, save the SG and STCNT residuals back into
1228	 * the SCB.  We use STCNT instead of HCNT, since it's a reflection
1229	 * of how many bytes were transferred on the SCSI (as opposed to the
1230	 * host) bus.
1231	 */
1232	if ((ahc->features & AHC_CMD_CHAN) != 0) {
1233		/* Kill off any pending prefetch */
1234		call	disable_ccsgen;
1235	}
1236
1237	if ((ahc->bugs & AHC_PCI_MWI_BUG) != 0
1238	  && ahc->pci_cachesize != 0) {
1239		if ((ahc->features & AHC_CMD_CHAN) != 0) {
1240			test	MWI_RESIDUAL, 0xFF jz bmov_resid;
1241		}
1242		mov	A, MWI_RESIDUAL;
1243		add	SCB_RESIDUAL_DATACNT[0], A, STCNT[0];
1244		clr	A;
1245		adc	SCB_RESIDUAL_DATACNT[1], A, STCNT[1];
1246		adc	SCB_RESIDUAL_DATACNT[2], A, STCNT[2];
1247		clr	MWI_RESIDUAL;
1248		if ((ahc->features & AHC_CMD_CHAN) != 0) {
1249			jmp	. + 2;
1250bmov_resid:
1251			bmov	SCB_RESIDUAL_DATACNT, STCNT, 3;
1252		}
1253	} else if ((ahc->features & AHC_CMD_CHAN) != 0) {
1254		bmov	SCB_RESIDUAL_DATACNT, STCNT, 3;
1255	} else {
1256		mov	SCB_RESIDUAL_DATACNT[0], STCNT[0];
1257		mov	SCB_RESIDUAL_DATACNT[1], STCNT[1];
1258		mov	SCB_RESIDUAL_DATACNT[2], STCNT[2];
1259	}
1260
1261	/*
1262	 * Since we've been through a data phase, the SCB_RESID* fields
1263	 * are now initialized.  Clear the full residual flag.
1264	 */
1265	and	SCB_SGPTR[0], ~SG_FULL_RESID;
1266
1267	if ((ahc->features & AHC_ULTRA2) != 0) {
1268		/* Clear the channel in case we return to data phase later */
1269		or	SXFRCTL0, CLRSTCNT|CLRCHN;
1270		or	SXFRCTL0, CLRSTCNT|CLRCHN;
1271	}
1272
1273	if ((ahc->flags & AHC_TARGETROLE) != 0) {
1274		test	SEQ_FLAGS, DPHASE_PENDING jz ITloop;
1275		and	SEQ_FLAGS, ~DPHASE_PENDING;
1276		/*
1277		 * For data-in phases, wait for any pending acks from the
1278		 * initiator before changing phase.
1279		 */
1280		test	DFCNTRL, DIRECTION jz target_ITloop;
1281		test	SSTAT1, REQINIT	jnz .;
1282		jmp	target_ITloop;
1283	} else {
1284		jmp	ITloop;
1285	}
1286
1287if ((ahc->flags & AHC_INITIATORROLE) != 0) {
1288/*
1289 * Command phase.  Set up the DMA registers and let 'er rip.
1290 */
1291p_command:
1292	call	assert;
1293
1294	if ((ahc->features & AHC_ULTRA2) != 0) {
1295		bmov	HCNT[0], SCB_CDB_LEN,  1;
1296		bmov	HCNT[1], ALLZEROS, 2;
1297		mvi	SG_CACHE_PRE, LAST_SEG;
1298	} else if ((ahc->features & AHC_CMD_CHAN) != 0) {
1299		bmov	STCNT[0], SCB_CDB_LEN, 1;
1300		bmov	STCNT[1], ALLZEROS, 2;
1301	} else {
1302		mov	STCNT[0], SCB_CDB_LEN;
1303		clr	STCNT[1];
1304		clr	STCNT[2];
1305	}
1306	add	NONE, -13, SCB_CDB_LEN;
1307	mvi	SCB_CDB_STORE jnc p_command_embedded;
1308p_command_from_host:
1309	if ((ahc->features & AHC_ULTRA2) != 0) {
1310		bmov	HADDR[0], SCB_CDB_PTR, 4;
1311		mvi	DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN|DIRECTION);
1312	} else {
1313		if ((ahc->features & AHC_CMD_CHAN) != 0) {
1314			bmov	HADDR[0], SCB_CDB_PTR, 4;
1315			bmov	HCNT, STCNT, 3;
1316		} else {
1317			mvi	DINDEX, HADDR;
1318			mvi	SCB_CDB_PTR call bcopy_4;
1319			mov	SCB_CDB_LEN call set_hcnt;
1320		}
1321		mvi	DFCNTRL, (SCSIEN|SDMAEN|HDMAEN|DIRECTION|FIFORESET);
1322	}
1323	jmp	p_command_loop;
1324p_command_embedded:
1325	/*
1326	 * The data fifo seems to require 4 byte aligned
1327	 * transfers from the sequencer.  Force this to
1328	 * be the case by clearing HADDR[0] even though
1329	 * we aren't going to touch host memeory.
1330	 */
1331	clr	HADDR[0];
1332	if ((ahc->features & AHC_ULTRA2) != 0) {
1333		mvi	DFCNTRL, (PRELOADEN|SCSIEN|DIRECTION);
1334		bmov	DFDAT, SCB_CDB_STORE, 12;
1335	} else if ((ahc->features & AHC_CMD_CHAN) != 0) {
1336		if ((ahc->flags & AHC_SCB_BTT) != 0) {
1337			/*
1338			 * On the 7895 the data FIFO will
1339			 * get corrupted if you try to dump
1340			 * data from external SCB memory into
1341			 * the FIFO while it is enabled.  So,
1342			 * fill the fifo and then enable SCSI
1343			 * transfers.
1344			 */
1345			mvi	DFCNTRL, (DIRECTION|FIFORESET);
1346		} else {
1347			mvi	DFCNTRL, (SCSIEN|SDMAEN|DIRECTION|FIFORESET);
1348		}
1349		bmov	DFDAT, SCB_CDB_STORE, 12;
1350		if ((ahc->flags & AHC_SCB_BTT) != 0) {
1351			mvi	DFCNTRL, (SCSIEN|SDMAEN|DIRECTION|FIFOFLUSH);
1352		} else {
1353			or	DFCNTRL, FIFOFLUSH;
1354		}
1355	} else {
1356		mvi	DFCNTRL, (SCSIEN|SDMAEN|DIRECTION|FIFORESET);
1357		call	copy_to_fifo_6;
1358		call	copy_to_fifo_6;
1359		or	DFCNTRL, FIFOFLUSH;
1360	}
1361p_command_loop:
1362	if ((ahc->features & AHC_DT) == 0) {
1363		test	SSTAT0, SDONE jnz . + 2;
1364		test    SSTAT1, PHASEMIS jz p_command_loop;
1365		/*
1366		 * Wait for our ACK to go-away on it's own
1367		 * instead of being killed by SCSIEN getting cleared.
1368		 */
1369		test	SCSISIGI, ACKI jnz .;
1370	} else {
1371		test	DFCNTRL, SCSIEN jnz p_command_loop;
1372	}
1373	and	DFCNTRL, ~(SCSIEN|SDMAEN|HDMAEN);
1374	test	DFCNTRL, (SCSIEN|SDMAEN|HDMAEN) jnz .;
1375	if ((ahc->features & AHC_ULTRA2) != 0) {
1376		/* Drop any residual from the S/G Preload queue */
1377		or	SXFRCTL0, CLRSTCNT;
1378	}
1379	jmp	ITloop;
1380
1381/*
1382 * Status phase.  Wait for the data byte to appear, then read it
1383 * and store it into the SCB.
1384 */
1385p_status:
1386	call	assert;
1387
1388	mov	SCB_SCSI_STATUS, SCSIDATL;
1389	jmp	ITloop;
1390
1391/*
1392 * Message out phase.  If MSG_OUT is MSG_IDENTIFYFLAG, build a full
1393 * indentify message sequence and send it to the target.  The host may
1394 * override this behavior by setting the MK_MESSAGE bit in the SCB
1395 * control byte.  This will cause us to interrupt the host and allow
1396 * it to handle the message phase completely on its own.  If the bit
1397 * associated with this target is set, we will also interrupt the host,
1398 * thereby allowing it to send a message on the next selection regardless
1399 * of the transaction being sent.
1400 *
1401 * If MSG_OUT is == HOST_MSG, also interrupt the host and take a message.
1402 * This is done to allow the host to send messages outside of an identify
1403 * sequence while protecting the seqencer from testing the MK_MESSAGE bit
1404 * on an SCB that might not be for the current nexus. (For example, a
1405 * BDR message in responce to a bad reselection would leave us pointed to
1406 * an SCB that doesn't have anything to do with the current target).
1407 *
1408 * Otherwise, treat MSG_OUT as a 1 byte message to send (abort, abort tag,
1409 * bus device reset).
1410 *
1411 * When there are no messages to send, MSG_OUT should be set to MSG_NOOP,
1412 * in case the target decides to put us in this phase for some strange
1413 * reason.
1414 */
1415p_mesgout_retry:
1416	/* Turn on ATN for the retry */
1417	if ((ahc->features & AHC_DT) == 0) {
1418		or	SCSISIGO, ATNO, LASTPHASE;
1419	} else {
1420		mvi	SCSISIGO, ATNO;
1421	}
1422p_mesgout:
1423	mov	SINDEX, MSG_OUT;
1424	cmp	SINDEX, MSG_IDENTIFYFLAG jne p_mesgout_from_host;
1425	test	SCB_CONTROL,MK_MESSAGE	jnz host_message_loop;
1426p_mesgout_identify:
1427	or	SINDEX, MSG_IDENTIFYFLAG|DISCENB, SCB_LUN;
1428	test	SCB_CONTROL, DISCENB jnz . + 2;
1429	and	SINDEX, ~DISCENB;
1430/*
1431 * Send a tag message if TAG_ENB is set in the SCB control block.
1432 * Use SCB_TAG (the position in the kernel's SCB array) as the tag value.
1433 */
1434p_mesgout_tag:
1435	test	SCB_CONTROL,TAG_ENB jz  p_mesgout_onebyte;
1436	mov	SCSIDATL, SINDEX;	/* Send the identify message */
1437	call	phase_lock;
1438	cmp	LASTPHASE, P_MESGOUT	jne p_mesgout_done;
1439	and	SCSIDATL,TAG_ENB|SCB_TAG_TYPE,SCB_CONTROL;
1440	call	phase_lock;
1441	cmp	LASTPHASE, P_MESGOUT	jne p_mesgout_done;
1442	mov	SCB_TAG	jmp p_mesgout_onebyte;
1443/*
1444 * Interrupt the driver, and allow it to handle this message
1445 * phase and any required retries.
1446 */
1447p_mesgout_from_host:
1448	cmp	SINDEX, HOST_MSG	jne p_mesgout_onebyte;
1449	jmp	host_message_loop;
1450
1451p_mesgout_onebyte:
1452	mvi	CLRSINT1, CLRATNO;
1453	mov	SCSIDATL, SINDEX;
1454
1455/*
1456 * If the next bus phase after ATN drops is message out, it means
1457 * that the target is requesting that the last message(s) be resent.
1458 */
1459	call	phase_lock;
1460	cmp	LASTPHASE, P_MESGOUT	je p_mesgout_retry;
1461
1462p_mesgout_done:
1463	mvi	CLRSINT1,CLRATNO;	/* Be sure to turn ATNO off */
1464	mov	LAST_MSG, MSG_OUT;
1465	mvi	MSG_OUT, MSG_NOOP;	/* No message left */
1466	jmp	ITloop;
1467
1468/*
1469 * Message in phase.  Bytes are read using Automatic PIO mode.
1470 */
1471p_mesgin:
1472	mvi	ACCUM		call inb_first;	/* read the 1st message byte */
1473
1474	test	A,MSG_IDENTIFYFLAG	jnz mesgin_identify;
1475	cmp	A,MSG_DISCONNECT	je mesgin_disconnect;
1476	cmp	A,MSG_SAVEDATAPOINTER	je mesgin_sdptrs;
1477	cmp	ALLZEROS,A		je mesgin_complete;
1478	cmp	A,MSG_RESTOREPOINTERS	je mesgin_rdptrs;
1479	cmp	A,MSG_IGN_WIDE_RESIDUE	je mesgin_ign_wide_residue;
1480	cmp	A,MSG_NOOP		je mesgin_done;
1481
1482/*
1483 * Pushed message loop to allow the kernel to
1484 * run it's own message state engine.  To avoid an
1485 * extra nop instruction after signaling the kernel,
1486 * we perform the phase_lock before checking to see
1487 * if we should exit the loop and skip the phase_lock
1488 * in the ITloop.  Performing back to back phase_locks
1489 * shouldn't hurt, but why do it twice...
1490 */
1491host_message_loop:
1492	mvi	HOST_MSG_LOOP call set_seqint;
1493	call	phase_lock;
1494	cmp	RETURN_1, EXIT_MSG_LOOP	je ITloop + 1;
1495	jmp	host_message_loop;
1496
1497mesgin_ign_wide_residue:
1498if ((ahc->features & AHC_WIDE) != 0) {
1499	test	SCSIRATE, WIDEXFER jz mesgin_reject;
1500	/* Pull the residue byte */
1501	mvi	ARG_1	call inb_next;
1502	cmp	ARG_1, 0x01 jne mesgin_reject;
1503	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz . + 2;
1504	test	DATA_COUNT_ODD, 0x1	jz mesgin_done;
1505	mvi	IGN_WIDE_RES call set_seqint;
1506	jmp	mesgin_done;
1507}
1508
1509mesgin_reject:
1510	mvi	MSG_MESSAGE_REJECT	call mk_mesg;
1511mesgin_done:
1512	mov	NONE,SCSIDATL;		/*dummy read from latch to ACK*/
1513	jmp	ITloop;
1514
1515mesgin_complete:
1516/*
1517 * We received a "command complete" message.  Put the SCB_TAG into the QOUTFIFO,
1518 * and trigger a completion interrupt.  Before doing so, check to see if there
1519 * is a residual or the status byte is something other than STATUS_GOOD (0).
1520 * In either of these conditions, we upload the SCB back to the host so it can
1521 * process this information.  In the case of a non zero status byte, we
1522 * additionally interrupt the kernel driver synchronously, allowing it to
1523 * decide if sense should be retrieved.  If the kernel driver wishes to request
1524 * sense, it will fill the kernel SCB with a request sense command, requeue
1525 * it to the QINFIFO and tell us not to post to the QOUTFIFO by setting
1526 * RETURN_1 to SEND_SENSE.
1527 */
1528
1529/*
1530 * If ATN is raised, we still want to give the target a message.
1531 * Perhaps there was a parity error on this last message byte.
1532 * Either way, the target should take us to message out phase
1533 * and then attempt to complete the command again.  We should use a
1534 * critical section here to guard against a timeout triggering
1535 * for this command and setting ATN while we are still processing
1536 * the completion.
1537	test	SCSISIGI, ATNI jnz mesgin_done;
1538 */
1539
1540/*
1541 * See if we attempted to deliver a message but the target ingnored us.
1542 */
1543	test	SCB_CONTROL, MK_MESSAGE jz . + 2;
1544	mvi	MKMSG_FAILED call set_seqint;
1545
1546/*
1547 * Check for residuals
1548 */
1549	test	SCB_SGPTR, SG_LIST_NULL jnz check_status;/* No xfer */
1550	test	SCB_SGPTR, SG_FULL_RESID jnz upload_scb;/* Never xfered */
1551	test	SCB_RESIDUAL_SGPTR, SG_LIST_NULL jz upload_scb;
1552check_status:
1553	test	SCB_SCSI_STATUS,0xff	jz complete;	/* Good Status? */
1554upload_scb:
1555	or	SCB_SGPTR, SG_RESID_VALID;
1556	mvi	DMAPARAMS, FIFORESET;
1557	mov	SCB_TAG		call dma_scb;
1558	test	SCB_SCSI_STATUS, 0xff	jz complete;	/* Just a residual? */
1559	mvi	BAD_STATUS call set_seqint;		/* let driver know */
1560	cmp	RETURN_1, SEND_SENSE	jne complete;
1561	call	add_scb_to_free_list;
1562	jmp	await_busfree;
1563complete:
1564	mov	SCB_TAG call complete_post;
1565	jmp	await_busfree;
1566}
1567
1568complete_post:
1569	/* Post the SCBID in SINDEX and issue an interrupt */
1570	call	add_scb_to_free_list;
1571	mov	ARG_1, SINDEX;
1572	if ((ahc->features & AHC_QUEUE_REGS) != 0) {
1573		mov	A, SDSCB_QOFF;
1574	} else {
1575		mov	A, QOUTPOS;
1576	}
1577	mvi	QOUTFIFO_OFFSET call post_byte_setup;
1578	mov	ARG_1 call post_byte;
1579	if ((ahc->features & AHC_QUEUE_REGS) == 0) {
1580		inc 	QOUTPOS;
1581	}
1582	mvi	INTSTAT,CMDCMPLT ret;
1583
1584if ((ahc->flags & AHC_INITIATORROLE) != 0) {
1585/*
1586 * Is it a disconnect message?  Set a flag in the SCB to remind us
1587 * and await the bus going free.  If this is an untagged transaction
1588 * store the SCB id for it in our untagged target table for lookup on
1589 * a reselction.
1590 */
1591mesgin_disconnect:
1592	/*
1593	 * If ATN is raised, we still want to give the target a message.
1594	 * Perhaps there was a parity error on this last message byte
1595	 * or we want to abort this command.  Either way, the target
1596	 * should take us to message out phase and then attempt to
1597	 * disconnect again.
1598	 * XXX - Wait for more testing.
1599	test	SCSISIGI, ATNI jnz mesgin_done;
1600	 */
1601
1602	or	SCB_CONTROL,DISCONNECTED;
1603	if ((ahc->flags & AHC_PAGESCBS) != 0) {
1604		call	add_scb_to_disc_list;
1605	}
1606	test	SCB_CONTROL, TAG_ENB jnz await_busfree;
1607	mov	ARG_1, SCB_TAG;
1608	mov	SAVED_LUN, SCB_LUN;
1609	mov	SCB_SCSIID	call set_busy_target;
1610	jmp	await_busfree;
1611
1612/*
1613 * Save data pointers message:
1614 * Copying RAM values back to SCB, for Save Data Pointers message, but
1615 * only if we've actually been into a data phase to change them.  This
1616 * protects against bogus data in scratch ram and the residual counts
1617 * since they are only initialized when we go into data_in or data_out.
1618 * Ack the message as soon as possible.  For chips without S/G pipelining,
1619 * we can only ack the message after SHADDR has been saved.  On these
1620 * chips, SHADDR increments with every bus transaction, even PIO.
1621 */
1622mesgin_sdptrs:
1623	if ((ahc->features & AHC_ULTRA2) != 0) {
1624		mov	NONE,SCSIDATL;		/*dummy read from latch to ACK*/
1625		test	SEQ_FLAGS, DPHASE	jz ITloop;
1626	} else {
1627		test	SEQ_FLAGS, DPHASE	jz mesgin_done;
1628	}
1629	/*
1630	 * The SCB_SGPTR becomes the next one we'll download,
1631	 * and the SCB_DATAPTR becomes the current SHADDR.
1632	 * Use the residual number since STCNT is corrupted by
1633	 * any message transfer.
1634	 */
1635	if ((ahc->features & AHC_CMD_CHAN) != 0) {
1636		bmov	SCB_DATAPTR, SHADDR, 4;
1637		if ((ahc->features & AHC_ULTRA2) == 0) {
1638			mov	NONE,SCSIDATL;	/*dummy read from latch to ACK*/
1639		}
1640		bmov	SCB_DATACNT, SCB_RESIDUAL_DATACNT, 8;
1641	} else {
1642		mvi	DINDEX, SCB_DATAPTR;
1643		mvi	SHADDR call bcopy_4;
1644		mov	NONE,SCSIDATL;	/*dummy read from latch to ACK*/
1645		mvi	SCB_RESIDUAL_DATACNT call bcopy_8;
1646	}
1647	jmp	ITloop;
1648
1649/*
1650 * Restore pointers message?  Data pointers are recopied from the
1651 * SCB anytime we enter a data phase for the first time, so all
1652 * we need to do is clear the DPHASE flag and let the data phase
1653 * code do the rest.
1654 */
1655mesgin_rdptrs:
1656	and	SEQ_FLAGS, ~DPHASE;		/*
1657						 * We'll reload them
1658						 * the next time through
1659						 * the dataphase.
1660						 */
1661	jmp	mesgin_done;
1662
1663/*
1664 * Index into our Busy Target table.  SINDEX and DINDEX are modified
1665 * upon return.  SCBPTR may be modified by this action.
1666 */
1667set_busy_target:
1668	shr	DINDEX, 4, SINDEX;
1669	if ((ahc->flags & AHC_SCB_BTT) != 0) {
1670		mov	SCBPTR, SAVED_LUN;
1671		add	DINDEX, SCB_64_BTT;
1672	} else {
1673		add	DINDEX, BUSY_TARGETS;
1674	}
1675	mov	DINDIR, ARG_1 ret;
1676
1677/*
1678 * Identify message?  For a reconnecting target, this tells us the lun
1679 * that the reconnection is for - find the correct SCB and switch to it,
1680 * clearing the "disconnected" bit so we don't "find" it by accident later.
1681 */
1682mesgin_identify:
1683	/*
1684	 * Determine whether a target is using tagged or non-tagged
1685	 * transactions by first looking at the transaction stored in
1686	 * the busy target array.  If there is no untagged transaction
1687	 * for this target or the transaction is for a different lun, then
1688	 * this must be an untagged transaction.
1689	 */
1690	shr	SINDEX, 4, SAVED_SCSIID;
1691	and	SAVED_LUN, MSG_IDENTIFY_LUNMASK, A;
1692	if ((ahc->flags & AHC_SCB_BTT) != 0) {
1693		add	SINDEX, SCB_64_BTT;
1694		mov	SCBPTR, SAVED_LUN;
1695		if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
1696			add	NONE, -SCB_64_BTT, SINDEX;
1697			jc	. + 2;
1698			mvi	INTSTAT, OUT_OF_RANGE;
1699			nop;
1700			add	NONE, -(SCB_64_BTT + 16), SINDEX;
1701			jnc	. + 2;
1702			mvi	INTSTAT, OUT_OF_RANGE;
1703			nop;
1704		}
1705	} else {
1706		add	SINDEX, BUSY_TARGETS;
1707		if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
1708			add	NONE, -BUSY_TARGETS, SINDEX;
1709			jc	. + 2;
1710			mvi	INTSTAT, OUT_OF_RANGE;
1711			nop;
1712			add	NONE, -(BUSY_TARGETS + 16), SINDEX;
1713			jnc	. + 2;
1714			mvi	INTSTAT, OUT_OF_RANGE;
1715			nop;
1716		}
1717	}
1718	mov	ARG_1, SINDIR;
1719	cmp	ARG_1, SCB_LIST_NULL	je snoop_tag;
1720	if ((ahc->flags & AHC_PAGESCBS) != 0) {
1721		mov	ARG_1 call findSCB;
1722	} else {
1723		mov	SCBPTR, ARG_1;
1724	}
1725	if ((ahc->flags & AHC_SCB_BTT) != 0) {
1726		jmp setup_SCB_id_lun_okay;
1727	} else {
1728		/*
1729		 * We only allow one untagged command per-target
1730		 * at a time.  So, if the lun doesn't match, look
1731		 * for a tag message.
1732		 */
1733		mov	A, SCB_LUN;
1734		cmp	SAVED_LUN, A	je setup_SCB_id_lun_okay;
1735		if ((ahc->flags & AHC_PAGESCBS) != 0) {
1736			/*
1737			 * findSCB removes the SCB from the
1738			 * disconnected list, so we must replace
1739			 * it there should this SCB be for another
1740			 * lun.
1741			 */
1742			call	cleanup_scb;
1743		}
1744	}
1745
1746/*
1747 * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
1748 * If we get one, we use the tag returned to find the proper
1749 * SCB.  With SCB paging, we must search for non-tagged
1750 * transactions since the SCB may exist in any slot.  If we're not
1751 * using SCB paging, we can use the tag as the direct index to the
1752 * SCB.
1753 */
1754snoop_tag:
1755	if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
1756		or	SEQ_FLAGS, 0x80;
1757	}
1758	mov	NONE,SCSIDATL;		/* ACK Identify MSG */
1759	call	phase_lock;
1760	if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
1761		or	SEQ_FLAGS, 0x1;
1762	}
1763	cmp	LASTPHASE, P_MESGIN	jne not_found;
1764	if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
1765		or	SEQ_FLAGS, 0x2;
1766	}
1767	cmp	SCSIBUSL,MSG_SIMPLE_Q_TAG jne not_found;
1768get_tag:
1769	if ((ahc->flags & AHC_PAGESCBS) != 0) {
1770		mvi	ARG_1	call inb_next;	/* tag value */
1771		mov	ARG_1	call findSCB;
1772	} else {
1773		mvi	ARG_1	call inb_next;	/* tag value */
1774		mov	SCBPTR, ARG_1;
1775	}
1776
1777/*
1778 * Ensure that the SCB the tag points to is for
1779 * an SCB transaction to the reconnecting target.
1780 */
1781setup_SCB:
1782	if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
1783		or	SEQ_FLAGS, 0x4;
1784	}
1785	mov	A, SCB_SCSIID;
1786	cmp	SAVED_SCSIID, A	jne not_found_cleanup_scb;
1787	if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
1788		or	SEQ_FLAGS, 0x8;
1789	}
1790setup_SCB_id_okay:
1791	mov	A, SCB_LUN;
1792	cmp	SAVED_LUN, A	jne not_found_cleanup_scb;
1793setup_SCB_id_lun_okay:
1794	if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
1795		or	SEQ_FLAGS, 0x10;
1796	}
1797	test	SCB_CONTROL,DISCONNECTED jz not_found_cleanup_scb;
1798	and	SCB_CONTROL,~DISCONNECTED;
1799	test	SCB_CONTROL, TAG_ENB	jnz setup_SCB_tagged;
1800	if ((ahc->flags & AHC_SCB_BTT) != 0) {
1801		mov	A, SCBPTR;
1802	}
1803	mvi	ARG_1, SCB_LIST_NULL;
1804	mov	SAVED_SCSIID	call	set_busy_target;
1805	if ((ahc->flags & AHC_SCB_BTT) != 0) {
1806		mov	SCBPTR, A;
1807	}
1808setup_SCB_tagged:
1809	mvi	SEQ_FLAGS,IDENTIFY_SEEN;	/* make note of IDENTIFY */
1810	call	set_transfer_settings;
1811	/* See if the host wants to send a message upon reconnection */
1812	test	SCB_CONTROL, MK_MESSAGE jz mesgin_done;
1813	mvi	HOST_MSG	call mk_mesg;
1814	jmp	mesgin_done;
1815
1816not_found_cleanup_scb:
1817	if ((ahc->flags & AHC_PAGESCBS) != 0) {
1818		call	cleanup_scb;
1819	}
1820not_found:
1821	mvi	NO_MATCH call set_seqint;
1822	jmp	mesgin_done;
1823
1824mk_mesg:
1825	if ((ahc->features & AHC_DT) == 0) {
1826		or	SCSISIGO, ATNO, LASTPHASE;
1827	} else {
1828		mvi	SCSISIGO, ATNO;
1829	}
1830	mov	MSG_OUT,SINDEX ret;
1831
1832/*
1833 * Functions to read data in Automatic PIO mode.
1834 *
1835 * According to Adaptec's documentation, an ACK is not sent on input from
1836 * the target until SCSIDATL is read from.  So we wait until SCSIDATL is
1837 * latched (the usual way), then read the data byte directly off the bus
1838 * using SCSIBUSL.  When we have pulled the ATN line, or we just want to
1839 * acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
1840 * spec guarantees that the target will hold the data byte on the bus until
1841 * we send our ACK.
1842 *
1843 * The assumption here is that these are called in a particular sequence,
1844 * and that REQ is already set when inb_first is called.  inb_{first,next}
1845 * use the same calling convention as inb.
1846 */
1847inb_next_wait_perr:
1848	mvi	PERR_DETECTED call set_seqint;
1849	jmp	inb_next_wait;
1850inb_next:
1851	mov	NONE,SCSIDATL;		/*dummy read from latch to ACK*/
1852inb_next_wait:
1853	/*
1854	 * If there is a parity error, wait for the kernel to
1855	 * see the interrupt and prepare our message response
1856	 * before continuing.
1857	 */
1858	test	SSTAT1, REQINIT	jz inb_next_wait;
1859	test	SSTAT1, SCSIPERR jnz inb_next_wait_perr;
1860inb_next_check_phase:
1861	and	LASTPHASE, PHASE_MASK, SCSISIGI;
1862	cmp	LASTPHASE, P_MESGIN jne mesgin_phasemis;
1863inb_first:
1864	mov	DINDEX,SINDEX;
1865	mov	DINDIR,SCSIBUSL	ret;		/*read byte directly from bus*/
1866inb_last:
1867	mov	NONE,SCSIDATL ret;		/*dummy read from latch to ACK*/
1868}
1869
1870if ((ahc->flags & AHC_TARGETROLE) != 0) {
1871/*
1872 * Change to a new phase.  If we are changing the state of the I/O signal,
1873 * from out to in, wait an additional data release delay before continuing.
1874 */
1875change_phase:
1876	/* Wait for preceeding I/O session to complete. */
1877	test	SCSISIGI, ACKI jnz .;
1878
1879	/* Change the phase */
1880	and	DINDEX, IOI, SCSISIGI;
1881	mov	SCSISIGO, SINDEX;
1882	and	A, IOI, SINDEX;
1883
1884	/*
1885	 * If the data direction has changed, from
1886	 * out (initiator driving) to in (target driving),
1887	 * we must wait at least a data release delay plus
1888	 * the normal bus settle delay. [SCSI III SPI 10.11.0]
1889	 */
1890	cmp 	DINDEX, A je change_phase_wait;
1891	test	SINDEX, IOI jz change_phase_wait;
1892	call	change_phase_wait;
1893change_phase_wait:
1894	nop;
1895	nop;
1896	nop;
1897	nop ret;
1898
1899/*
1900 * Send a byte to an initiator in Automatic PIO mode.
1901 */
1902target_outb:
1903	or	SXFRCTL0, SPIOEN;
1904	test	SSTAT0, SPIORDY	jz .;
1905	mov	SCSIDATL, SINDEX;
1906	test	SSTAT0, SPIORDY	jz .;
1907	and	SXFRCTL0, ~SPIOEN ret;
1908}
1909
1910
1911/*
1912 * Assert that if we've been reselected, then we've seen an IDENTIFY
1913 * message.
1914 */
1915assert:
1916	test	SEQ_FLAGS,IDENTIFY_SEEN	jnz return;	/* seen IDENTIFY? */
1917
1918	mvi	NO_IDENT jmp set_seqint;	/* no - tell the kernel */
1919
1920/*
1921 * Locate a disconnected SCB by SCBID.  Upon return, SCBPTR and SINDEX will
1922 * be set to the position of the SCB.  If the SCB cannot be found locally,
1923 * it will be paged in from host memory.  RETURN_2 stores the address of the
1924 * preceding SCB in the disconnected list which can be used to speed up
1925 * removal of the found SCB from the disconnected list.
1926 */
1927if ((ahc->flags & AHC_PAGESCBS) != 0) {
1928BEGIN_CRITICAL
1929findSCB:
1930	mov	A, SINDEX;			/* Tag passed in SINDEX */
1931	cmp	DISCONNECTED_SCBH, SCB_LIST_NULL je findSCB_notFound;
1932	mov	SCBPTR, DISCONNECTED_SCBH;	/* Initialize SCBPTR */
1933	mvi	ARG_2, SCB_LIST_NULL;		/* Head of list */
1934	jmp	findSCB_loop;
1935findSCB_next:
1936	cmp	SCB_NEXT, SCB_LIST_NULL je findSCB_notFound;
1937	mov	ARG_2, SCBPTR;
1938	mov	SCBPTR,SCB_NEXT;
1939findSCB_loop:
1940	cmp	SCB_TAG, A	jne findSCB_next;
1941rem_scb_from_disc_list:
1942	cmp	ARG_2, SCB_LIST_NULL	je rHead;
1943	mov	DINDEX, SCB_NEXT;
1944	mov	SINDEX, SCBPTR;
1945	mov	SCBPTR, ARG_2;
1946	mov	SCB_NEXT, DINDEX;
1947	mov	SCBPTR, SINDEX ret;
1948rHead:
1949	mov	DISCONNECTED_SCBH,SCB_NEXT ret;
1950END_CRITICAL
1951findSCB_notFound:
1952	/*
1953	 * We didn't find it.  Page in the SCB.
1954	 */
1955	mov	ARG_1, A; /* Save tag */
1956	mov	ALLZEROS call get_free_or_disc_scb;
1957	mvi	DMAPARAMS, HDMAEN|DIRECTION|FIFORESET;
1958	mov	ARG_1	jmp dma_scb;
1959}
1960
1961/*
1962 * Prepare the hardware to post a byte to host memory given an
1963 * index of (A + (256 * SINDEX)) and a base address of SHARED_DATA_ADDR.
1964 */
1965post_byte_setup:
1966	mov	ARG_2, SINDEX;
1967	if ((ahc->features & AHC_CMD_CHAN) != 0) {
1968		mvi	DINDEX, CCHADDR;
1969		mvi	SHARED_DATA_ADDR call	set_1byte_addr;
1970		mvi	CCHCNT, 1;
1971		mvi	CCSCBCTL, CCSCBRESET ret;
1972	} else {
1973		mvi	DINDEX, HADDR;
1974		mvi	SHARED_DATA_ADDR call	set_1byte_addr;
1975		mvi	1	call set_hcnt;
1976		mvi	DFCNTRL, FIFORESET ret;
1977	}
1978
1979post_byte:
1980	if ((ahc->features & AHC_CMD_CHAN) != 0) {
1981		bmov	CCSCBRAM, SINDEX, 1;
1982		or	CCSCBCTL, CCSCBEN|CCSCBRESET;
1983		test	CCSCBCTL, CCSCBDONE jz .;
1984		clr	CCSCBCTL ret;
1985	} else {
1986		mov	DFDAT, SINDEX;
1987		or	DFCNTRL, HDMAEN|FIFOFLUSH;
1988		jmp	dma_finish;
1989	}
1990
1991phase_lock_perr:
1992	mvi	PERR_DETECTED call set_seqint;
1993phase_lock:
1994	/*
1995	 * If there is a parity error, wait for the kernel to
1996	 * see the interrupt and prepare our message response
1997	 * before continuing.
1998	 */
1999	test	SSTAT1, REQINIT jz phase_lock;
2000	test	SSTAT1, SCSIPERR jnz phase_lock_perr;
2001phase_lock_latch_phase:
2002	if ((ahc->features & AHC_DT) == 0) {
2003		and	SCSISIGO, PHASE_MASK, SCSISIGI;
2004	}
2005	and	LASTPHASE, PHASE_MASK, SCSISIGI ret;
2006
2007if ((ahc->features & AHC_CMD_CHAN) == 0) {
2008set_hcnt:
2009	mov	HCNT[0], SINDEX;
2010clear_hcnt:
2011	clr	HCNT[1];
2012	clr	HCNT[2] ret;
2013
2014set_stcnt_from_hcnt:
2015	mov	STCNT[0], HCNT[0];
2016	mov	STCNT[1], HCNT[1];
2017	mov	STCNT[2], HCNT[2] ret;
2018
2019bcopy_8:
2020	mov	DINDIR, SINDIR;
2021bcopy_7:
2022	mov	DINDIR, SINDIR;
2023	mov	DINDIR, SINDIR;
2024bcopy_5:
2025	mov	DINDIR, SINDIR;
2026bcopy_4:
2027	mov	DINDIR, SINDIR;
2028bcopy_3:
2029	mov	DINDIR, SINDIR;
2030	mov	DINDIR, SINDIR;
2031	mov	DINDIR, SINDIR ret;
2032}
2033
2034if ((ahc->flags & AHC_TARGETROLE) != 0) {
2035/*
2036 * Setup addr assuming that A is an index into
2037 * an array of 32byte objects, SINDEX contains
2038 * the base address of that array, and DINDEX
2039 * contains the base address of the location
2040 * to store the indexed address.
2041 */
2042set_32byte_addr:
2043	shr	ARG_2, 3, A;
2044	shl	A, 5;
2045	jmp	set_1byte_addr;
2046}
2047
2048/*
2049 * Setup addr assuming that A is an index into
2050 * an array of 64byte objects, SINDEX contains
2051 * the base address of that array, and DINDEX
2052 * contains the base address of the location
2053 * to store the indexed address.
2054 */
2055set_64byte_addr:
2056	shr	ARG_2, 2, A;
2057	shl	A, 6;
2058
2059/*
2060 * Setup addr assuming that A + (ARG_2 * 256) is an
2061 * index into an array of 1byte objects, SINDEX contains
2062 * the base address of that array, and DINDEX contains
2063 * the base address of the location to store the computed
2064 * address.
2065 */
2066set_1byte_addr:
2067	add     DINDIR, A, SINDIR;
2068	mov     A, ARG_2;
2069	adc	DINDIR, A, SINDIR;
2070	clr	A;
2071	adc	DINDIR, A, SINDIR;
2072	adc	DINDIR, A, SINDIR ret;
2073
2074/*
2075 * Either post or fetch and SCB from host memory based on the
2076 * DIRECTION bit in DMAPARAMS. The host SCB index is in SINDEX.
2077 */
2078dma_scb:
2079	mov	A, SINDEX;
2080	if ((ahc->features & AHC_CMD_CHAN) != 0) {
2081		mvi	DINDEX, CCHADDR;
2082		mvi	HSCB_ADDR call set_64byte_addr;
2083		mov	CCSCBPTR, SCBPTR;
2084		test	DMAPARAMS, DIRECTION jz dma_scb_tohost;
2085		if ((ahc->flags & AHC_SCB_BTT) != 0) {
2086			mvi	CCHCNT, SCB_DOWNLOAD_SIZE_64;
2087		} else {
2088			mvi	CCHCNT, SCB_DOWNLOAD_SIZE;
2089		}
2090		mvi	CCSCBCTL, CCARREN|CCSCBEN|CCSCBDIR|CCSCBRESET;
2091		cmp	CCSCBCTL, CCSCBDONE|ARRDONE|CCARREN|CCSCBEN|CCSCBDIR jne .;
2092		jmp	dma_scb_finish;
2093dma_scb_tohost:
2094		mvi	CCHCNT, SCB_UPLOAD_SIZE;
2095		if ((ahc->features & AHC_ULTRA2) == 0) {
2096			mvi	CCSCBCTL, CCSCBRESET;
2097			bmov	CCSCBRAM, SCB_BASE, SCB_UPLOAD_SIZE;
2098			or	CCSCBCTL, CCSCBEN|CCSCBRESET;
2099			test	CCSCBCTL, CCSCBDONE jz .;
2100		} else if ((ahc->bugs & AHC_SCBCHAN_UPLOAD_BUG) != 0) {
2101			mvi	CCSCBCTL, CCARREN|CCSCBRESET;
2102			cmp	CCSCBCTL, ARRDONE|CCARREN jne .;
2103			mvi	CCHCNT, SCB_UPLOAD_SIZE;
2104			mvi	CCSCBCTL, CCSCBEN|CCSCBRESET;
2105			cmp	CCSCBCTL, CCSCBDONE|CCSCBEN jne .;
2106		} else {
2107			mvi	CCSCBCTL, CCARREN|CCSCBEN|CCSCBRESET;
2108			cmp	CCSCBCTL, CCSCBDONE|ARRDONE|CCARREN|CCSCBEN jne .;
2109		}
2110dma_scb_finish:
2111		clr	CCSCBCTL;
2112		test	CCSCBCTL, CCARREN|CCSCBEN jnz .;
2113		ret;
2114	} else {
2115		mvi	DINDEX, HADDR;
2116		mvi	HSCB_ADDR call set_64byte_addr;
2117		mvi	SCB_DOWNLOAD_SIZE call set_hcnt;
2118		mov	DFCNTRL, DMAPARAMS;
2119		test	DMAPARAMS, DIRECTION	jnz dma_scb_fromhost;
2120		/* Fill it with the SCB data */
2121copy_scb_tofifo:
2122		mvi	SINDEX, SCB_BASE;
2123		add	A, SCB_DOWNLOAD_SIZE, SINDEX;
2124copy_scb_tofifo_loop:
2125		call	copy_to_fifo_8;
2126		cmp	SINDEX, A jne copy_scb_tofifo_loop;
2127		or	DFCNTRL, HDMAEN|FIFOFLUSH;
2128		jmp	dma_finish;
2129dma_scb_fromhost:
2130		mvi	DINDEX, SCB_BASE;
2131		if ((ahc->bugs & AHC_PCI_2_1_RETRY_BUG) != 0) {
2132			/*
2133			 * The PCI module will only issue a PCI
2134			 * retry if the data FIFO is empty.  If the
2135			 * host disconnects in the middle of a
2136			 * transfer, we must empty the fifo of all
2137			 * available data to force the chip to
2138			 * continue the transfer.  This does not
2139			 * happen for SCSI transfers as the SCSI module
2140			 * will drain the FIFO as data is made available.
2141			 * When the hang occurs, we know that a multiple
2142			 * of 8 bytes are in the FIFO because the PCI
2143			 * module has an 8 byte input latch that only
2144			 * dumps to the FIFO when HCNT == 0 or the
2145			 * latch is full.
2146			 */
2147			clr	A;
2148			/* Wait for at least 8 bytes of data to arrive. */
2149dma_scb_hang_fifo:
2150			test	DFSTATUS, FIFOQWDEMP jnz dma_scb_hang_fifo;
2151dma_scb_hang_wait:
2152			test	DFSTATUS, MREQPEND jnz dma_scb_hang_wait;
2153			test	DFSTATUS, HDONE	jnz dma_scb_hang_dma_done;
2154			test	DFSTATUS, HDONE	jnz dma_scb_hang_dma_done;
2155			test	DFSTATUS, HDONE	jnz dma_scb_hang_dma_done;
2156			/*
2157			 * The PCI module no longer intends to perform
2158			 * a PCI transaction.  Drain the fifo.
2159			 */
2160dma_scb_hang_empty_fifo:
2161			/*
2162			 * Skip lines not yet transfered into the FIFO.
2163			 */
2164			add	SINDEX, 7, HCNT;
2165			shr	SINDEX, 3;
2166
2167			/*
2168			 * Skip lines already copied out of the FIFO.
2169			 */
2170			add	A, A, SINDEX;
2171
2172			call	dma_scb_hang_dma_drain_fifo;
2173
2174			/*
2175			 * Set the lines transferred to all but
2176			 * those yet to reach the FIFO.
2177			 */
2178			not	SINDEX;
2179			add	A, 5, SINDEX;
2180			cmp	A, 4	je dma_finish_nowait;
2181			jmp	dma_scb_hang_fifo;
2182dma_scb_hang_dma_done:
2183			and	DFCNTRL, ~HDMAEN;
2184			test	DFCNTRL, HDMAEN jnz .;
2185dma_scb_hang_dma_drain_fifo:
2186			add	SEQADDR0, A;
2187		} else {
2188			call	dma_finish;
2189		}
2190		/* If we were putting the SCB, we are done */
2191		call	dfdat_in_8;
2192		call	dfdat_in_8;
2193		call	dfdat_in_8;
2194dfdat_in_8:
2195		mov	DINDIR,DFDAT;
2196dfdat_in_7:
2197		mov	DINDIR,DFDAT;
2198		mov	DINDIR,DFDAT;
2199		mov	DINDIR,DFDAT;
2200		mov	DINDIR,DFDAT;
2201		mov	DINDIR,DFDAT;
2202dfdat_in_2:
2203		mov	DINDIR,DFDAT;
2204		mov	DINDIR,DFDAT ret;
2205	}
2206
2207copy_to_fifo_8:
2208	mov	DFDAT,SINDIR;
2209	mov	DFDAT,SINDIR;
2210copy_to_fifo_6:
2211	mov	DFDAT,SINDIR;
2212copy_to_fifo_5:
2213	mov	DFDAT,SINDIR;
2214copy_to_fifo_4:
2215	mov	DFDAT,SINDIR;
2216	mov	DFDAT,SINDIR;
2217	mov	DFDAT,SINDIR;
2218	mov	DFDAT,SINDIR ret;
2219
2220/*
2221 * Wait for DMA from host memory to data FIFO to complete, then disable
2222 * DMA and wait for it to acknowledge that it's off.
2223 */
2224dma_finish:
2225	test	DFSTATUS,HDONE	jz dma_finish;
2226dma_finish_nowait:
2227	/* Turn off DMA */
2228	and	DFCNTRL, ~HDMAEN;
2229	test	DFCNTRL, HDMAEN jnz .;
2230	ret;
2231
2232/*
2233 * Restore an SCB that failed to match an incoming reselection
2234 * to the correct/safe state.  If the SCB is for a disconnected
2235 * transaction, it must be returned to the disconnected list.
2236 * If it is not in the disconnected state, it must be free.
2237 */
2238cleanup_scb:
2239	if ((ahc->flags & AHC_PAGESCBS) != 0) {
2240		test	SCB_CONTROL,DISCONNECTED jnz add_scb_to_disc_list;
2241	}
2242add_scb_to_free_list:
2243	if ((ahc->flags & AHC_PAGESCBS) != 0) {
2244BEGIN_CRITICAL
2245		mov	SCB_NEXT, FREE_SCBH;
2246		mvi	SCB_TAG, SCB_LIST_NULL;
2247		mov	FREE_SCBH, SCBPTR ret;
2248END_CRITICAL
2249	} else {
2250		mvi	SCB_TAG, SCB_LIST_NULL ret;
2251	}
2252
2253if ((ahc->flags & AHC_PAGESCBS) != 0) {
2254get_free_or_disc_scb:
2255BEGIN_CRITICAL
2256	cmp	FREE_SCBH, SCB_LIST_NULL jne dequeue_free_scb;
2257	cmp	DISCONNECTED_SCBH, SCB_LIST_NULL jne dequeue_disc_scb;
2258return_error:
2259	mvi	NO_FREE_SCB call set_seqint;
2260	mvi	SINDEX, SCB_LIST_NULL	ret;
2261dequeue_disc_scb:
2262	mov	SCBPTR, DISCONNECTED_SCBH;
2263	mov	DISCONNECTED_SCBH, SCB_NEXT;
2264END_CRITICAL
2265	mvi	DMAPARAMS, FIFORESET;
2266	mov	SCB_TAG	jmp dma_scb;
2267BEGIN_CRITICAL
2268dequeue_free_scb:
2269	mov	SCBPTR, FREE_SCBH;
2270	mov	FREE_SCBH, SCB_NEXT ret;
2271END_CRITICAL
2272
2273add_scb_to_disc_list:
2274/*
2275 * Link this SCB into the DISCONNECTED list.  This list holds the
2276 * candidates for paging out an SCB if one is needed for a new command.
2277 * Modifying the disconnected list is a critical(pause dissabled) section.
2278 */
2279BEGIN_CRITICAL
2280	mov	SCB_NEXT, DISCONNECTED_SCBH;
2281	mov	DISCONNECTED_SCBH, SCBPTR ret;
2282END_CRITICAL
2283}
2284set_seqint:
2285	mov	INTSTAT, SINDEX;
2286	nop;
2287return:
2288	ret;
2289