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