xref: /freebsd/sys/dev/aic7xxx/aic7xxx.seq (revision b601c69bdbe8755d26570261d7fd4c02ee4eff74)
1/*
2 * Adaptec 274x/284x/294x device driver firmware for Linux and FreeBSD.
3 *
4 * Copyright (c) 1994-2000 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 * $FreeBSD$
32 */
33
34#include <dev/aic7xxx/aic7xxx.reg>
35#include <cam/scsi/scsi_message.h>
36
37/*
38 * A few words on the waiting SCB list:
39 * After starting the selection hardware, we check for reconnecting targets
40 * as well as for our selection to complete just in case the reselection wins
41 * bus arbitration.  The problem with this is that we must keep track of the
42 * SCB that we've already pulled from the QINFIFO and started the selection
43 * on just in case the reselection wins so that we can retry the selection at
44 * a later time.  This problem cannot be resolved by holding a single entry
45 * in scratch ram since a reconnecting target can request sense and this will
46 * create yet another SCB waiting for selection.  The solution used here is to
47 * use byte 27 of the SCB as a psuedo-next pointer and to thread a list
48 * of SCBs that are awaiting selection.  Since 0-0xfe are valid SCB indexes,
49 * SCB_LIST_NULL is 0xff which is out of range.  An entry is also added to
50 * this list everytime a request sense occurs or after completing a non-tagged
51 * command for which a second SCB has been queued.  The sequencer will
52 * automatically consume the entries.
53 */
54
55reset:
56	clr	SCSISIGO;		/* De-assert BSY */
57	mvi	MSG_OUT, MSG_NOOP;	/* No message to send */
58	and	SXFRCTL1, ~BITBUCKET;
59	/* Always allow reselection */
60	and	SCSISEQ, ENSELI|ENRSELI|ENAUTOATNP, SCSISEQ_TEMPLATE;
61	if ((ahc->features & AHC_CMD_CHAN) != 0) {
62		/* Ensure that no DMA operations are in progress */
63		clr	CCSGCTL;
64		clr	CCSCBCTL;
65	}
66
67poll_for_work:
68	call	clear_target_state;
69	and	SXFRCTL0, ~SPIOEN;
70	if ((ahc->features & AHC_QUEUE_REGS) == 0) {
71		mov	A, QINPOS;
72	}
73poll_for_work_loop:
74	test	SSTAT0, SELDO|SELDI	jnz selection;
75	test	SCSISEQ, ENSELO	jnz poll_for_work_loop;
76	if ((ahc->features & AHC_TWIN) != 0) {
77		/*
78		 * Twin channel devices cannot handle things like SELTO
79		 * interrupts on the "background" channel.  So, if we
80		 * are selecting, keep polling the current channel util
81		 * either a selection or reselection occurs.
82		 */
83		xor	SBLKCTL,SELBUSB;	/* Toggle to the other bus */
84		test	SSTAT0, SELDO|SELDI	jnz selection;
85		test	SCSISEQ, ENSELO	jnz poll_for_work_loop;
86	}
87	cmp	WAITING_SCBH,SCB_LIST_NULL jne start_waiting;
88test_queue:
89	/* Has the driver posted any work for us? */
90	if ((ahc->features & AHC_QUEUE_REGS) != 0) {
91		test	QOFF_CTLSTA, SCB_AVAIL jz poll_for_work_loop;
92		mov	NONE, SNSCB_QOFF;
93		inc	QINPOS;
94	} else {
95		cmp	KERNEL_QINPOS, A je poll_for_work_loop;
96		inc	QINPOS;
97	}
98
99	/*
100	 * We have at least one queued SCB now and we don't have any
101	 * SCBs in the list of SCBs awaiting selection.  Pull the tag
102	 * from the QINFIFO and get to work on it.
103	 */
104	if ((ahc->flags & AHC_PAGESCBS) != 0) {
105		mov	ALLZEROS	call	get_free_or_disc_scb;
106	}
107
108dequeue_scb:
109	add	A, -1, QINPOS;
110	mvi	QINFIFO_OFFSET call fetch_byte;
111
112	if ((ahc->flags & AHC_PAGESCBS) == 0) {
113		/* In the non-paging case, the SCBID == hardware SCB index */
114		mov	SCBPTR, RETURN_2;
115	}
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	RETURN_2	 call dma_scb;
122
123start_scb:
124	/*
125	 * Place us on the waiting list in case our selection
126	 * doesn't win during bus arbitration.
127	 */
128	mov	SCB_NEXT,WAITING_SCBH;
129	mov	WAITING_SCBH, SCBPTR;
130start_waiting:
131	/*
132	 * Start the first entry on the waiting SCB list.
133	 */
134	mov	SCBPTR, WAITING_SCBH;
135	call	start_selection;
136	jmp	poll_for_work;
137
138start_selection:
139	if ((ahc->features & AHC_TWIN) != 0) {
140		and	SINDEX,~SELBUSB,SBLKCTL;/* Clear channel select bit */
141		test	SCB_SCSIID, TWIN_CHNLB jz . + 2;
142		or	SINDEX, SELBUSB;
143		mov	SBLKCTL,SINDEX;		/* select channel */
144	}
145initialize_scsiid:
146	if ((ahc->features & AHC_ULTRA2) != 0) {
147		mov	SCSIID_ULTRA2, SCB_SCSIID;
148	} else if ((ahc->features & AHC_TWIN) != 0) {
149		and	SCSIID, TWIN_TID|OID, SCB_SCSIID;
150	} else {
151		mov	SCSIID, SCB_SCSIID;
152	}
153	if ((ahc->flags & AHC_TARGETMODE) != 0) {
154		mov	SINDEX, SCSISEQ_TEMPLATE;
155		test	SCB_CONTROL, TARGET_SCB jz . + 2;
156		or	SINDEX, TEMODE;
157		mov	SCSISEQ, SINDEX ret;
158	} else {
159		mov	SCSISEQ, SCSISEQ_TEMPLATE ret;
160	}
161
162/*
163 * Initialize transfer settings and clear the SCSI channel.
164 * SINDEX should contain any additional bit's the client wants
165 * set in SXFRCTL0.  We also assume that the current SCB is
166 * a valid SCB for the target we wish to talk to.
167 */
168initialize_channel:
169	or	SXFRCTL0, CLRSTCNT|CLRCHN, SINDEX;
170set_transfer_settings:
171	if ((ahc->features & AHC_ULTRA) != 0) {
172		test	SCB_CONTROL, ULTRAENB jz . + 2;
173		or	SXFRCTL0, FAST20;
174	}
175	/*
176	 * Initialize SCSIRATE with the appropriate value for this target.
177	 */
178	if ((ahc->features & AHC_ULTRA2) != 0) {
179		bmov	SCSIRATE, SCB_SCSIRATE, 2 ret;
180	} else {
181		mov	SCSIRATE, SCB_SCSIRATE ret;
182	}
183
184selection:
185	/*
186	 * We aren't expecting a bus free, so interrupt
187	 * the kernel driver if it happens.
188	 */
189	mvi	CLRSINT1,CLRBUSFREE;
190	or	SIMODE1, ENBUSFREE;
191
192	test	SSTAT0,SELDO	jnz select_out;
193	mvi	CLRSINT0, CLRSELDI;
194select_in:
195	if ((ahc->flags & AHC_TARGETMODE) != 0) {
196		if ((ahc->flags & AHC_INITIATORMODE) != 0) {
197			test	SSTAT0, TARGET	jz initiator_reselect;
198		}
199
200		/*
201		 * We've just been selected.  Assert BSY and
202		 * setup the phase for receiving messages
203		 * from the target.
204		 */
205		mvi	SCSISIGO, P_MESGOUT|BSYO;
206
207		/*
208		 * Setup the DMA for sending the identify and
209		 * command information.
210		 */
211		or	SEQ_FLAGS, CMDPHASE_PENDING;
212
213		mov     A, TQINPOS;
214		if ((ahc->features & AHC_CMD_CHAN) != 0) {
215			mvi	DINDEX, CCHADDR;
216			mvi	SHARED_DATA_ADDR call set_32byte_addr;
217			mvi	CCSCBCTL, CCSCBRESET;
218		} else {
219			mvi	DINDEX, HADDR;
220			mvi	SHARED_DATA_ADDR call set_32byte_addr;
221			mvi	DFCNTRL, FIFORESET;
222		}
223
224		/* Initiator that selected us */
225		and	SAVED_SCSIID, SELID_MASK, SELID;
226		/* The Target ID we were selected at */
227		if ((ahc->features & AHC_MULTI_TID) != 0) {
228			and	A, OID, TARGIDIN;
229		} else if ((ahc->features & AHC_ULTRA2) != 0) {
230			and	A, OID, SCSIID_ULTRA2;
231		} else {
232			and	A, OID, SCSIID;
233		}
234		or	SAVED_SCSIID, A;
235		if ((ahc->features & AHC_TWIN) != 0) {
236			test 	SBLKCTL, SELBUSB jz . + 2;
237			or	SAVED_SCSIID, TWIN_CHNLB;
238		}
239		if ((ahc->features & AHC_CMD_CHAN) != 0) {
240			mov	CCSCBRAM, SAVED_SCSIID;
241		} else {
242			mov	DFDAT, SAVED_SCSIID;
243		}
244
245		/*
246		 * If ATN isn't asserted, the target isn't interested
247		 * in talking to us.  Go directly to bus free.
248		 * XXX SCSI-1 may require us to assume lun 0 if
249		 * ATN is false.
250		 */
251		test	SCSISIGI, ATNI	jz	target_busfree;
252
253		/*
254		 * Watch ATN closely now as we pull in messages from the
255		 * initiator.  We follow the guidlines from section 6.5
256		 * of the SCSI-2 spec for what messages are allowed when.
257		 */
258		call	target_inb;
259
260		/*
261		 * Our first message must be one of IDENTIFY, ABORT, or
262		 * BUS_DEVICE_RESET.
263		 */
264		test	DINDEX, MSG_IDENTIFYFLAG jz host_target_message_loop;
265		/* Store for host */
266		if ((ahc->features & AHC_CMD_CHAN) != 0) {
267			mov	CCSCBRAM, DINDEX;
268		} else {
269			mov	DFDAT, DINDEX;
270		}
271
272		/* Remember for disconnection decision */
273		test	DINDEX, MSG_IDENTIFY_DISCFLAG jnz . + 2;
274		/* XXX Honor per target settings too */
275		or	SEQ_FLAGS, NO_DISCONNECT;
276
277		test	SCSISIGI, ATNI	jz	ident_messages_done;
278		call	target_inb;
279		/*
280		 * If this is a tagged request, the tagged message must
281		 * immediately follow the identify.  We test for a valid
282		 * tag message by seeing if it is >= MSG_SIMPLE_Q_TAG and
283		 * < MSG_IGN_WIDE_RESIDUE.
284		 */
285		add	A, -MSG_SIMPLE_Q_TAG, DINDEX;
286		jnc	ident_messages_done;
287		add	A, -MSG_IGN_WIDE_RESIDUE, DINDEX;
288		jc	ident_messages_done;
289		/* Store for host */
290		if ((ahc->features & AHC_CMD_CHAN) != 0) {
291			mov	CCSCBRAM, DINDEX;
292		} else {
293			mov	DFDAT, DINDEX;
294		}
295
296		/*
297		 * If the initiator doesn't feel like providing a tag number,
298		 * we've got a failed selection and must transition to bus
299		 * free.
300		 */
301		test	SCSISIGI, ATNI	jz	target_busfree;
302
303		/*
304		 * Store the tag for the host.
305		 */
306		call	target_inb;
307		if ((ahc->features & AHC_CMD_CHAN) != 0) {
308			mov	CCSCBRAM, DINDEX;
309		} else {
310			mov	DFDAT, DINDEX;
311		}
312		mov	INITIATOR_TAG, DINDEX;
313		or	SEQ_FLAGS, TARGET_CMD_IS_TAGGED;
314		test	SCSISIGI, ATNI	jz . + 2;
315		/* Initiator still wants to give us messages */
316		call	target_inb;
317		jmp	ident_messages_done;
318
319		/*
320		 * Pushed message loop to allow the kernel to
321		 * run it's own target mode message state engine.
322		 */
323host_target_message_loop:
324		mvi	INTSTAT, HOST_MSG_LOOP;
325		nop;
326		cmp	RETURN_1, EXIT_MSG_LOOP	je target_ITloop;
327		test	SSTAT0, SPIORDY jz .;
328		jmp	host_target_message_loop;
329
330ident_messages_done:
331		/* If ring buffer is full, return busy or queue full */
332		if ((ahc->features & AHC_HS_MAILBOX) != 0) {
333			and	A, HOST_TQINPOS, HS_MAILBOX;
334		} else {
335			mov	A, KERNEL_TQINPOS;
336		}
337		cmp	TQINPOS, A jne tqinfifo_has_space;
338		mvi	P_STATUS|BSYO call change_phase;
339		test	SEQ_FLAGS, TARGET_CMD_IS_TAGGED jnz . + 3;
340		mvi	STATUS_QUEUE_FULL call target_outb;
341		jmp	target_busfree_wait;
342		mvi	STATUS_BUSY call target_outb;
343		jmp	target_busfree_wait;
344tqinfifo_has_space:
345		/* Terminate the ident list */
346		if ((ahc->features & AHC_CMD_CHAN) != 0) {
347			mvi	CCSCBRAM, SCB_LIST_NULL;
348		} else {
349			mvi	DFDAT, SCB_LIST_NULL;
350		}
351		or	SEQ_FLAGS, TARG_CMD_PENDING|IDENTIFY_SEEN;
352		test	SCSISIGI, ATNI	jnz target_mesgout_pending;
353		jmp	target_ITloop;
354
355/*
356 * We carefully toggle SPIOEN to allow us to return the
357 * message byte we receive so it can be checked prior to
358 * driving REQ on the bus for the next byte.
359 */
360target_inb:
361		/*
362		 * Drive REQ on the bus by enabling SCSI PIO.
363		 */
364		or	SXFRCTL0, SPIOEN;
365		/* Wait for the byte */
366		test	SSTAT0, SPIORDY jz .;
367		/* Prevent our read from triggering another REQ */
368		and	SXFRCTL0, ~SPIOEN;
369		/* Save latched contents */
370		mov	DINDEX, SCSIDATL ret;
371	}
372
373if ((ahc->flags & AHC_INITIATORMODE) != 0) {
374/*
375 * Reselection has been initiated by a target. Make a note that we've been
376 * reselected, but haven't seen an IDENTIFY message from the target yet.
377 */
378initiator_reselect:
379	/* XXX test for and handle ONE BIT condition */
380	and	SAVED_SCSIID, SELID_MASK, SELID;
381	if ((ahc->features & AHC_ULTRA2) != 0) {
382		and	A, OID, SCSIID_ULTRA2;
383	} else {
384		and	A, OID, SCSIID;
385	}
386	or	SAVED_SCSIID, A;
387	if ((ahc->features & AHC_TWIN) != 0) {
388		test	SBLKCTL, SELBUSB	jz . + 2;
389		or	SAVED_SCSIID, TWIN_CHNLB;
390	}
391	or	SXFRCTL0, SPIOEN|CLRSTCNT|CLRCHN;
392	jmp	ITloop;
393}
394
395/*
396 * After the selection, remove this SCB from the "waiting SCB"
397 * list.  This is achieved by simply moving our "next" pointer into
398 * WAITING_SCBH.  Our next pointer will be set to null the next time this
399 * SCB is used, so don't bother with it now.
400 */
401select_out:
402	/* Turn off the selection hardware */
403	and	SCSISEQ, TEMODE|ENSELI|ENRSELI|ENAUTOATNP, SCSISEQ;
404	mvi	CLRSINT0, CLRSELDO;
405	mov	SCBPTR, WAITING_SCBH;
406	mov	WAITING_SCBH,SCB_NEXT;
407	mov	SAVED_SCSIID, SCB_SCSIID;
408	mov	SAVED_LUN, SCB_LUN;
409	mvi	SPIOEN call	initialize_channel;
410	if ((ahc->flags & AHC_TARGETMODE) != 0) {
411		test	SSTAT0, TARGET	jz initiator_select;
412
413		/*
414		 * We've just re-selected an initiator.
415		 * Assert BSY and setup the phase for
416		 * sending our identify messages.
417		 */
418		mvi	P_MESGIN|BSYO call change_phase;
419
420		/*
421		 * Start out with a simple identify message.
422		 */
423		or	SCB_LUN, MSG_IDENTIFYFLAG call target_outb;
424
425		/*
426		 * If we are the result of a tagged command, send
427		 * a simple Q tag and the tag id.
428		 */
429		test	SCB_CONTROL, TAG_ENB	jz . + 3;
430		mvi	MSG_SIMPLE_Q_TAG call target_outb;
431		mov	SCB_TARGET_INFO[SCB_INITIATOR_TAG] call target_outb;
432target_synccmd:
433		/*
434		 * Now determine what phases the host wants us
435		 * to go through.
436		 */
437		mov	SEQ_FLAGS, SCB_TARGET_INFO[SCB_TARGET_PHASES];
438
439target_ITloop:
440		/*
441		 * Start honoring ATN signals now that
442		 * we properly identified ourselves.
443		 */
444		test	SCSISIGI, ATNI			jnz target_mesgout;
445		test	SEQ_FLAGS, CMDPHASE_PENDING	jnz target_cmdphase;
446		test	SEQ_FLAGS, DPHASE_PENDING	jnz target_dphase;
447		test	SEQ_FLAGS, SPHASE_PENDING	jnz target_sphase;
448
449		/*
450		 * No more work to do.  Either disconnect or not depending
451		 * on the state of NO_DISCONNECT.
452		 */
453		test	SEQ_FLAGS, NO_DISCONNECT jz target_disconnect;
454		if ((ahc->flags & AHC_PAGESCBS) != 0) {
455			mov	ALLZEROS	call	get_free_or_disc_scb;
456		}
457		mov	RETURN_1, ALLZEROS;
458		call	complete_target_cmd;
459		cmp	RETURN_1, CONT_MSG_LOOP jne .;
460		mvi	DMAPARAMS, HDMAEN|DIRECTION|FIFORESET;
461		mov	SCB_TAG	 call dma_scb;
462		jmp	target_synccmd;
463
464target_mesgout:
465		mvi	SCSISIGO, P_MESGOUT|BSYO;
466target_mesgout_continue:
467		call	target_inb;
468target_mesgout_pending:
469		/* Local Processing goes here... */
470		jmp	host_target_message_loop;
471
472target_disconnect:
473		mvi	P_MESGIN|BSYO call change_phase;
474		test	SEQ_FLAGS, DPHASE	jz . + 2;
475		mvi	MSG_SAVEDATAPOINTER call target_outb;
476		mvi	MSG_DISCONNECT call target_outb;
477
478target_busfree_wait:
479		/* Wait for preceeding I/O session to complete. */
480		test	SCSISIGI, ACKI jnz .;
481target_busfree:
482		and	SIMODE1, ~ENBUSFREE;
483		clr	SCSISIGO;
484		mvi	LASTPHASE, P_BUSFREE;
485		call	complete_target_cmd;
486		jmp	poll_for_work;
487
488target_cmdphase:
489		mvi	P_COMMAND|BSYO call change_phase;
490		call	target_inb;
491		mov	A, DINDEX;
492		/* Store for host */
493		if ((ahc->features & AHC_CMD_CHAN) != 0) {
494			mov	CCSCBRAM, A;
495		} else {
496			mov	DFDAT, A;
497		}
498
499		/*
500		 * Determine the number of bytes to read
501		 * based on the command group code via table lookup.
502		 * We reuse the first 8 bytes of the TARG_SCSIRATE
503		 * BIOS array for this table. Count is one less than
504		 * the total for the command since we've already fetched
505		 * the first byte.
506		 */
507		shr	A, CMD_GROUP_CODE_SHIFT;
508		add	SINDEX, TARG_SCSIRATE, A;
509		mov	A, SINDIR;
510
511		test	A, 0xFF jz command_phase_done;
512command_loop:
513		or	SXFRCTL0, SPIOEN;
514		test	SSTAT0, SPIORDY jz .;
515		cmp	A, 1 jne . + 2;
516		and	SXFRCTL0, ~SPIOEN;	/* Last Byte */
517		if ((ahc->features & AHC_CMD_CHAN) != 0) {
518			mov	CCSCBRAM, SCSIDATL;
519		} else {
520			mov	DFDAT, SCSIDATL;
521		}
522		dec	A;
523		test	A, 0xFF jnz command_loop;
524
525command_phase_done:
526		and	SEQ_FLAGS, ~CMDPHASE_PENDING;
527		jmp	target_ITloop;
528
529target_dphase:
530		/*
531		 * Data phases on the bus are from the
532		 * perspective of the initiator.  The dma
533		 * code looks at LASTPHASE to determine the
534		 * data direction of the DMA.  Toggle it for
535		 * target transfers.
536		 */
537		xor	LASTPHASE, IOI, SCB_TARGET_INFO[SCB_TARGET_DATA_DIR];
538		or	SCB_TARGET_INFO[SCB_TARGET_DATA_DIR], BSYO
539			call change_phase;
540		jmp	p_data;
541
542target_sphase:
543		mvi	P_STATUS|BSYO call change_phase;
544		mvi	LASTPHASE, P_STATUS;
545		mov	SCB_TARGET_INFO[SCB_TARGET_STATUS] call target_outb;
546		/* XXX Watch for ATN or parity errors??? */
547		mvi	SCSISIGO, P_MESGIN|BSYO;
548		/* MSG_CMDCMPLT is 0, but we can't do an immediate of 0 */
549		mov	ALLZEROS call target_outb;
550		jmp	target_busfree_wait;
551
552complete_target_cmd:
553		test	SEQ_FLAGS, TARG_CMD_PENDING	jnz . + 2;
554		mov	SCB_TAG jmp complete_post;
555		if ((ahc->features & AHC_CMD_CHAN) != 0) {
556			/* Set the valid byte */
557			mvi	CCSCBADDR, 24;
558			mov	CCSCBRAM, ALLONES;
559			mvi	CCHCNT, 28;
560			or	CCSCBCTL, CCSCBEN|CCSCBRESET;
561			test	CCSCBCTL, CCSCBDONE jz .;
562			clr	CCSCBCTL;
563		} else {
564			/* Set the valid byte */
565			or	DFCNTRL, FIFORESET;
566			mvi	DFWADDR, 3; /* Third 64bit word or byte 24 */
567			mov	DFDAT, ALLONES;
568			mvi	28	call set_hcnt;
569			or	DFCNTRL, HDMAEN|FIFOFLUSH;
570			call	dma_finish;
571		}
572		inc	TQINPOS;
573		mvi	INTSTAT,CMDCMPLT ret;
574	}
575
576if ((ahc->flags & AHC_INITIATORMODE) != 0) {
577initiator_select:
578	/*
579	 * As soon as we get a successful selection, the target
580	 * should go into the message out phase since we have ATN
581	 * asserted.
582	 */
583	mvi	MSG_OUT, MSG_IDENTIFYFLAG;
584	or	SEQ_FLAGS, IDENTIFY_SEEN;
585
586	/*
587	 * Main loop for information transfer phases.  Wait for the
588	 * target to assert REQ before checking MSG, C/D and I/O for
589	 * the bus phase.
590	 */
591mesgin_phasemis:
592ITloop:
593	call	phase_lock;
594
595	mov	A, LASTPHASE;
596
597	test	A, ~P_DATAIN	jz p_data;
598	cmp	A,P_COMMAND	je p_command;
599	cmp	A,P_MESGOUT	je p_mesgout;
600	cmp	A,P_STATUS	je p_status;
601	cmp	A,P_MESGIN	je p_mesgin;
602
603	mvi	INTSTAT,BAD_PHASE;
604	jmp	ITloop;			/* Try reading the bus again. */
605
606await_busfree:
607	and	SIMODE1, ~ENBUSFREE;
608	mov	NONE, SCSIDATL;		/* Ack the last byte */
609	and	SXFRCTL0, ~SPIOEN;
610	test	SSTAT1,REQINIT|BUSFREE	jz .;
611	test	SSTAT1, BUSFREE jnz poll_for_work;
612	mvi	INTSTAT, BAD_PHASE;
613}
614
615clear_target_state:
616	/*
617	 * We assume that the kernel driver may reset us
618	 * at any time, even in the middle of a DMA, so
619	 * clear DFCNTRL too.
620	 */
621	clr	DFCNTRL;
622	mvi	SXFRCTL0, CLRSTCNT|CLRCHN;
623
624	/*
625	 * We don't know the target we will connect to,
626	 * so default to narrow transfers to avoid
627	 * parity problems.
628	 */
629	if ((ahc->features & AHC_ULTRA2) != 0) {
630		bmov	SCSIRATE, ALLZEROS, 2;
631	} else {
632		clr	SCSIRATE;
633		if ((ahc->features & AHC_ULTRA) != 0) {
634			and	SXFRCTL0, ~(FAST20);
635		}
636	}
637	mvi	LASTPHASE, P_BUSFREE;
638	/* clear target specific flags */
639	clr	SEQ_FLAGS ret;
640
641sg_advance:
642	clr	A;			/* add sizeof(struct scatter) */
643	add	SCB_RESIDUAL_SGPTR[0],SG_SIZEOF;
644	adc	SCB_RESIDUAL_SGPTR[1],A;
645	adc	SCB_RESIDUAL_SGPTR[2],A;
646	adc	SCB_RESIDUAL_SGPTR[3],A ret;
647
648idle_loop:
649	if ((ahc->features & AHC_CMD_CHAN) != 0) {
650		/* Did we just finish fetching segs? */
651		cmp	CCSGCTL, CCSGEN|CCSGDONE je idle_sgfetch_complete;
652
653		/* Are we actively fetching segments? */
654		test	CCSGCTL, CCSGEN jnz return;
655
656		/*
657		 * Do we need any more segments?
658		 */
659		test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jnz return;
660
661		/*
662		 * Do we have any prefetch left???
663		 */
664		cmp	CCSGADDR, CCSGADDR_MAX jne idle_sg_avail;
665
666		/*
667		 * Need to fetch segments, but we can only do that
668		 * if the command channel is completely idle.  Make
669		 * sure we don't have an SCB prefetch going on.
670		 */
671		test	CCSCBCTL, CCSCBEN jnz return;
672
673		/*
674		 * The kernel allocates S/G space so that it is 128 byte
675		 * aligned and ends on a 128 byte boundary.  We fetch
676		 * up to the next 128 byte boundary so we don't attempt
677		 * to read a non-existent page.
678		 */
679		mvi	CCHCNT, CCSGADDR_MAX;
680		and	CCHADDR[0], ~(CCSGADDR_MAX - 1), SCB_RESIDUAL_SGPTR;
681		bmov	CCHADDR[1], SCB_RESIDUAL_SGPTR[1], 3;
682		mvi	CCSGCTL, CCSGEN|CCSGRESET ret;
683idle_sgfetch_complete:
684		mvi	CCSGCTL, CCSGRESET;
685		and	CCSGADDR, (CCSGADDR_MAX - 1), SCB_RESIDUAL_SGPTR;
686idle_sg_avail:
687		if ((ahc->features & AHC_ULTRA2) != 0) {
688			/* Does the hardware have space for another SG entry? */
689			test	DFSTATUS, PRELOAD_AVAIL jz return;
690			bmov 	HADDR, CCSGRAM, 4;
691			bmov	SINDEX, CCSGRAM, 1;
692			test	SINDEX, 0x1 jz . + 2;
693			xor	DATA_COUNT_ODD, 0x1;
694			bmov	HCNT[0], SINDEX, 1;
695			bmov	HCNT[1], CCSGRAM, 2;
696			bmov	SCB_RESIDUAL_DATACNT[3], CCSGRAM, 1;
697			call	sg_advance;
698			mov	SINDEX, SCB_RESIDUAL_SGPTR[0];
699			test	DATA_COUNT_ODD, 0x1 jz . + 2;
700			or	SINDEX, ODD_SEG;
701			test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz . + 2;
702			or	SINDEX, LAST_SEG;
703			mov	SG_CACHE_PRE, SINDEX;
704			/* Load the segment by writing DFCNTRL again */
705			mov	DFCNTRL, DMAPARAMS;
706		}
707		ret;
708	}
709
710/*
711 * If we re-enter the data phase after going through another phase, the
712 * STCNT may have been cleared, so restore it from the residual field.
713 */
714data_phase_reinit:
715	if ((ahc->features & AHC_ULTRA2) != 0) {
716		/*
717		 * The preload circuitry requires us to
718		 * reload the address too, so pull it from
719		 * the shaddow address.
720		 */
721		bmov	HADDR, SHADDR, 4;
722		bmov	HCNT, SCB_RESIDUAL_DATACNT, 3;
723	} else if ((ahc->features & AHC_CMD_CHAN) != 0) {
724		bmov	STCNT, SCB_RESIDUAL_DATACNT, 3;
725	} else {
726		mvi	DINDEX, STCNT;
727		mvi	SCB_RESIDUAL_DATACNT call bcopy_3;
728	}
729	and	DATA_COUNT_ODD, 0x1, SCB_RESIDUAL_DATACNT[0];
730	jmp	data_phase_loop;
731
732p_data:
733	if ((ahc->features & AHC_ULTRA2) != 0) {
734		mvi	DMAPARAMS, PRELOADEN|SCSIEN|HDMAEN;
735	} else {
736		mvi	DMAPARAMS, WIDEODD|SCSIEN|SDMAEN|HDMAEN|FIFORESET;
737	}
738	test	LASTPHASE, IOI jnz . + 2;
739	or	DMAPARAMS, DIRECTION;
740	call	assert;			/*
741					 * Ensure entering a data
742					 * phase is okay - seen identify, etc.
743					 */
744	if ((ahc->features & AHC_CMD_CHAN) != 0) {
745		/* We don't have any valid S/G elements */
746		mvi	CCSGADDR, CCSGADDR_MAX;
747	}
748	test	SEQ_FLAGS, DPHASE	jnz data_phase_reinit;
749
750	/* We have seen a data phase */
751	or	SEQ_FLAGS, DPHASE;
752
753	/*
754	 * Initialize the DMA address and counter from the SCB.
755	 * Also set SCB_RESIDUAL_SGPTR, including the LAST_SEG
756	 * flag in the highest byte of the data count.  We cannot
757	 * modify the saved values in the SCB until we see a save
758	 * data pointers message.
759	 */
760	if ((ahc->features & AHC_CMD_CHAN) != 0) {
761		bmov	HADDR, SCB_DATAPTR, 7;
762		bmov	SCB_RESIDUAL_DATACNT[3], SCB_DATACNT[3], 5;
763	} else {
764		mvi	DINDEX, HADDR;
765		mvi	SCB_DATAPTR	call bcopy_7;
766		mvi	DINDEX, SCB_RESIDUAL_DATACNT + 3;
767		mvi	SCB_DATACNT + 3 call bcopy_5;
768	}
769	and	SCB_RESIDUAL_SGPTR[0], ~SG_FULL_RESID;
770	and	DATA_COUNT_ODD, 0x1, SCB_DATACNT[0];
771
772	if ((ahc->features & AHC_ULTRA2) == 0) {
773		if ((ahc->features & AHC_CMD_CHAN) != 0) {
774			bmov	STCNT, HCNT, 3;
775		} else {
776			call	set_stcnt_from_hcnt;
777		}
778	}
779
780data_phase_loop:
781	/* Guard against overruns */
782	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz data_phase_inbounds;
783
784	/*
785	 * Turn on 'Bit Bucket' mode, set the transfer count to
786	 * 16meg and let the target run until it changes phase.
787	 * When the transfer completes, notify the host that we
788	 * had an overrun.
789	 */
790	or	SXFRCTL1,BITBUCKET;
791	and	DMAPARAMS, ~(HDMAEN|SDMAEN);
792	/* Keep our idle loop from mucking with SG segments */
793	or	SCB_RESIDUAL_DATACNT[0], SG_LAST_SEG;
794	if ((ahc->features & AHC_ULTRA2) != 0) {
795		bmov	HCNT, ALLONES, 3;
796	} else if ((ahc->features & AHC_CMD_CHAN) != 0) {
797		bmov	STCNT, ALLONES, 3;
798	} else {
799		/* XXX Use bcopy? */
800		mvi	STCNT[0], 0xFF;
801		mvi	STCNT[1], 0xFF;
802		mvi	STCNT[2], 0xFF;
803	}
804data_phase_inbounds:
805	if ((ahc->features & AHC_ULTRA2) != 0) {
806		mov	SINDEX, SCB_RESIDUAL_SGPTR[0];
807		test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz . + 2;
808		or	SINDEX, LAST_SEG;
809		test	DATA_COUNT_ODD, 0x1 jz . + 2;
810		or	SINDEX, ODD_SEG;
811		mov	SG_CACHE_PRE, SINDEX;
812		mov	DFCNTRL, DMAPARAMS;
813ultra2_dma_loop:
814		call	idle_loop;
815		/*
816		 * The transfer is complete if either the last segment
817		 * completes or the target changes phase.
818		 */
819		test	SG_CACHE_SHADOW, LAST_SEG_DONE jnz ultra2_dmafinish;
820		test	SSTAT1,PHASEMIS	jz ultra2_dma_loop;
821
822ultra2_dmafinish:
823		test	DFCNTRL, DIRECTION jnz ultra2_dmafifoempty;
824		and	DFCNTRL, ~SCSIEN;
825		test	DFCNTRL, SCSIEN jnz .;
826ultra2_dmafifoflush:
827		if ((ahc->bugs & AHC_AUTOFLUSH_BUG) != 0) {
828			/*
829			 * On Rev A of the aic7890, the autoflush
830			 * features doesn't function correctly.
831			 * Perform an explicit manual flush.  During
832			 * a manual flush, the FIFOEMP bit becomes
833			 * true every time the PCI FIFO empties
834			 * regardless of the state of the SCSI FIFO.
835			 * It can take up to 4 clock cycles for the
836			 * SCSI FIFO to get data into the PCI FIFO
837			 * and for FIFOEMP to de-assert.  Here we
838			 * guard against this condition by making
839			 * sure the FIFOEMP bit stays on for 5 full
840			 * clock cycles.
841			 */
842			or	DFCNTRL, FIFOFLUSH;
843			test	DFSTATUS, FIFOEMP jz ultra2_dmafifoflush;
844			test	DFSTATUS, FIFOEMP jz ultra2_dmafifoflush;
845			test	DFSTATUS, FIFOEMP jz ultra2_dmafifoflush;
846			test	DFSTATUS, FIFOEMP jz ultra2_dmafifoflush;
847		}
848		test	DFSTATUS, FIFOEMP jz ultra2_dmafifoflush;
849ultra2_dmafifoempty:
850		/* Don't clobber an inprogress host data transfer */
851		test	DFSTATUS, MREQPEND	jnz ultra2_dmafifoempty;
852ultra2_dmahalt:
853		and     DFCNTRL, ~(SCSIEN|HDMAEN);
854		test	DFCNTRL, HDMAEN jnz .;
855
856		test	SXFRCTL1,BITBUCKET jnz data_phase_finish;
857		/*
858		 * Fixup the residual next S/G pointer.  The S/G preload
859		 * feature of the chip allows us to load two elements
860		 * in addition to the currently active element.  We
861		 * store the bottom byte of the next S/G pointer in
862		 * the SG_CACEPTR register so we can restore the
863		 * correct value when the DMA completes.  If the next
864		 * sg ptr value has advanced to the point where higher
865		 * bytes in the address have been affected, fix them
866		 * too.
867		 */
868		test	SG_CACHE_SHADOW, 0x80 jz sgptr_fixup_done;
869		test	SCB_RESIDUAL_SGPTR[0], 0x80 jnz sgptr_fixup_done;
870		add	SCB_RESIDUAL_SGPTR[1], -1;
871		adc	SCB_RESIDUAL_SGPTR[2], -1;
872		adc	SCB_RESIDUAL_SGPTR[3], -1;
873sgptr_fixup_done:
874		and	SCB_RESIDUAL_SGPTR[0], SG_ADDR_MASK, SG_CACHE_SHADOW;
875		clr	DATA_COUNT_ODD;
876		test	SG_CACHE_SHADOW, ODD_SEG jz . + 2;
877		or	DATA_COUNT_ODD, 0x1;
878		clr	SCB_RESIDUAL_DATACNT[3];
879		test	SG_CACHE_SHADOW, LAST_SEG jz data_phase_finish;
880		or	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG;
881		/* Record if we've consumed all S/G entries */
882		test	SG_CACHE_SHADOW, LAST_SEG_DONE jz . + 2;
883		or	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL;
884	} else {
885		/* If we are the last SG block, tell the hardware. */
886		test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz dma_mid_sg;
887		if ((ahc->flags & AHC_TARGETMODE) != 0) {
888			test	SSTAT0, TARGET jz . + 2;
889			test	DMAPARAMS, DIRECTION jz dma_mid_sg;
890		}
891		and	DMAPARAMS, ~WIDEODD;
892dma_mid_sg:
893		/* Start DMA data transfer. */
894		mov	DFCNTRL, DMAPARAMS;
895dma_loop:
896		if ((ahc->features & AHC_CMD_CHAN) != 0) {
897			call	idle_loop;
898		}
899		test	SSTAT0,DMADONE	jnz dma_dmadone;
900		test	SSTAT1,PHASEMIS	jz dma_loop;	/* ie. underrun */
901dma_phasemis:
902		/*
903		 * We will be "done" DMAing when the transfer count goes to
904		 * zero, or the target changes the phase (in light of this,
905		 * it makes sense that the DMA circuitry doesn't ACK when
906		 * PHASEMIS is active).  If we are doing a SCSI->Host transfer,
907		 * the data FIFO should be flushed auto-magically on STCNT=0
908		 * or a phase change, so just wait for FIFO empty status.
909		 */
910dma_checkfifo:
911		test	DFCNTRL,DIRECTION	jnz dma_fifoempty;
912dma_fifoflush:
913		test	DFSTATUS,FIFOEMP	jz dma_fifoflush;
914dma_fifoempty:
915		/* Don't clobber an inprogress host data transfer */
916		test	DFSTATUS, MREQPEND	jnz dma_fifoempty;
917
918		/*
919		 * Now shut off the DMA and make sure that the DMA
920		 * hardware has actually stopped.  Touching the DMA
921		 * counters, etc. while a DMA is active will result
922		 * in an ILLSADDR exception.
923		 */
924dma_dmadone:
925		and	DFCNTRL, ~(SCSIEN|SDMAEN|HDMAEN);
926dma_halt:
927		/*
928		 * Some revisions of the aic7880 have a problem where, if the
929		 * data fifo is full, but the PCI input latch is not empty,
930		 * HDMAEN cannot be cleared.  The fix used here is to drain
931		 * the prefetched but unused data from the data fifo until
932		 * there is space for the input latch to drain.
933		 */
934		mov	NONE, DFDAT;
935		test	DFCNTRL, (SCSIEN|SDMAEN|HDMAEN) jnz dma_halt;
936
937		/* See if we have completed this last segment */
938		test	STCNT[0], 0xff	jnz data_phase_finish;
939		test	STCNT[1], 0xff	jnz data_phase_finish;
940		test	STCNT[2], 0xff	jnz data_phase_finish;
941
942		/*
943		 * Advance the scatter-gather pointers if needed
944		 */
945		test	SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz sg_load;
946		or	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL;
947		jmp	data_phase_finish;
948sg_load:
949		/*
950		 * Load the next SG element's data address and length
951		 * into the DMA engine.  If we don't have hardware
952		 * to perform a prefetch, we'll have to fetch the
953		 * segment from host memory first.
954		 */
955		if ((ahc->features & AHC_CMD_CHAN) != 0) {
956			/* Wait for the idle loop to complete */
957			test	CCSGCTL, CCSGEN jz . + 3;
958			call	idle_loop;
959			test	CCSGCTL, CCSGEN jnz . - 1;
960			bmov 	HADDR, CCSGRAM, 7;
961			bmov	SCB_RESIDUAL_DATACNT[3], CCSGRAM, 1;
962			bmov	STCNT, HCNT, 3;
963		} else {
964			mvi	DINDEX, HADDR;
965			mvi	SCB_RESIDUAL_SGPTR	call bcopy_4;
966
967			mvi	SG_SIZEOF	call set_hcnt;
968
969			or	DFCNTRL, HDMAEN|DIRECTION|FIFORESET;
970
971			call	dma_finish;
972
973			mvi	HADDR	call dfdat_in_7;
974			mov	SCB_RESIDUAL_DATACNT[3], DFDAT;
975			call	set_stcnt_from_hcnt;
976		}
977
978		/* Track odd'ness */
979		test	HCNT[0], 0x1 jz . + 2;
980		xor	DATA_COUNT_ODD, 0x1;
981
982		/* Point to the new next sg in memory */
983		call	sg_advance;
984
985		if ((ahc->flags & AHC_TARGETMODE) != 0) {
986			test	SSTAT0, TARGET jnz data_phase_loop;
987		}
988	}
989data_phase_finish:
990	/*
991	 * If the target has left us in data phase, loop through
992	 * the dma code again.  In the case of ULTRA2 adapters,
993	 * we should only loop if there is a data overrun.  For
994	 * all other adapters, we'll loop after each S/G element
995	 * is loaded as well as if there is an overrun.
996	 */
997	if ((ahc->flags & AHC_TARGETMODE) != 0) {
998		test	SSTAT0, TARGET jnz data_phase_done;
999	}
1000	if ((ahc->flags & AHC_INITIATORMODE) != 0) {
1001		test	SSTAT1, REQINIT jz .;
1002		test	SSTAT1,PHASEMIS	jz data_phase_loop;
1003
1004		if ((ahc->features & AHC_CMD_CHAN) != 0) {
1005			/* Kill off any pending prefetch */
1006			clr	CCSGCTL;
1007			test	CCSGCTL, CCSGEN jnz .;
1008		}
1009
1010		/*
1011		 * Turn off BITBUCKET mode and notify the host
1012		 * in the event of an overrun.
1013		 */
1014		test	SXFRCTL1,BITBUCKET jz data_phase_done;
1015		and	SXFRCTL1, ~BITBUCKET;
1016		mvi	INTSTAT,DATA_OVERRUN;
1017		jmp	ITloop;
1018	}
1019
1020data_phase_done:
1021	/*
1022	 * After a DMA finishes, save the SG and STCNT residuals back into
1023	 * the SCB.  We use STCNT instead of HCNT, since it's a reflection
1024	 * of how many bytes were transferred on the SCSI (as opposed to the
1025	 * host) bus.
1026	 */
1027	if ((ahc->features & AHC_CMD_CHAN) != 0) {
1028		/* Kill off any pending prefetch */
1029		clr	CCSGCTL;
1030		test	CCSGCTL, CCSGEN jnz .;
1031
1032		bmov	SCB_RESIDUAL_DATACNT, STCNT, 3;
1033	} else {
1034		mov	SCB_RESIDUAL_DATACNT[0],STCNT[0];
1035		mov	SCB_RESIDUAL_DATACNT[1],STCNT[1];
1036		mov	SCB_RESIDUAL_DATACNT[2],STCNT[2];
1037	}
1038
1039	/*
1040	 * Since we've been through a data phase, the SCB_RESID* fields
1041	 * are now initialized.  Clear the full residual flag.
1042	 */
1043	and	SCB_SGPTR[0], ~SG_FULL_RESID;
1044
1045	if ((ahc->features & AHC_ULTRA2) != 0) {
1046		/* Clear the channel in case we return to data phase later */
1047		or	SXFRCTL0, CLRSTCNT|CLRCHN;
1048	}
1049
1050	if ((ahc->flags & AHC_TARGETMODE) != 0) {
1051		test	SEQ_FLAGS, DPHASE_PENDING jz ITloop;
1052		and	SEQ_FLAGS, ~DPHASE_PENDING;
1053		/*
1054		 * For data-in phases, wait for any pending acks from the
1055		 * initiator before changing phase.
1056		 */
1057		test	DFCNTRL, DIRECTION jz target_ITloop;
1058		test	SSTAT1, REQINIT	jnz .;
1059		jmp	target_ITloop;
1060	} else {
1061		jmp	ITloop;
1062	}
1063
1064if ((ahc->flags & AHC_INITIATORMODE) != 0) {
1065/*
1066 * Command phase.  Set up the DMA registers and let 'er rip.
1067 */
1068p_command:
1069	call	assert;
1070
1071	if ((ahc->features & AHC_ULTRA2) != 0) {
1072		bmov	HCNT[0], SCB_CDB_LEN,  1;
1073		bmov	HCNT[1], ALLZEROS, 2;
1074	} else if ((ahc->features & AHC_CMD_CHAN) != 0) {
1075		bmov	STCNT[0], SCB_CDB_LEN, 1;
1076		bmov	STCNT[1], ALLZEROS, 2;
1077	} else {
1078		mov	STCNT[0], SCB_CDB_LEN;
1079		clr	STCNT[1];
1080		clr	STCNT[2];
1081	}
1082	add	NONE, -13, SCB_CDB_LEN;
1083	mvi	SCB_CDB_STORE	jnc p_command_embedded;
1084p_command_from_host:
1085	if ((ahc->features & AHC_ULTRA2) != 0) {
1086		bmov	HADDR[0], SCB_CDB_PTR, 4;
1087		mvi	DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN|DIRECTION);
1088	} else {
1089		if ((ahc->features & AHC_CMD_CHAN) != 0) {
1090			bmov	HADDR[0], SCB_CDB_PTR, 4;
1091			bmov	HCNT[0], STCNT[0], 3;
1092		} else {
1093			mvi	DINDEX, HADDR;
1094			mvi	SCB_CDB_PTR	call bcopy_5;
1095			call	clear_hcnt;
1096		}
1097		mvi	DFCNTRL, (SCSIEN|SDMAEN|HDMAEN|DIRECTION|FIFORESET);
1098	}
1099	jmp	p_command_loop;
1100p_command_embedded:
1101	/*
1102	 * The data fifo seems to require 4 byte alligned
1103	 * transfers from the sequencer.  Force this to
1104	 * be the case by clearing HADDR[0] even though
1105	 * we aren't going to touch host memeory.
1106	 */
1107	clr	HADDR[0];
1108	if ((ahc->features & AHC_ULTRA2) != 0) {
1109		mvi	DFCNTRL, (PRELOADEN|SCSIEN|DIRECTION);
1110	} else {
1111		mvi	DFCNTRL, (SCSIEN|SDMAEN|DIRECTION|FIFORESET);
1112	}
1113	if ((ahc->features & AHC_CMD_CHAN) != 0) {
1114		bmov	DFDAT, SCB_CDB_STORE, 12;
1115	} else {
1116		/*
1117		 * At most 12 bytes, but we copy 16 to fill
1118		 * the 64bit words in the FIFO
1119		 */
1120		call	copy_to_fifo_8;
1121		call	copy_to_fifo_8;
1122	}
1123p_command_loop:
1124	test	SSTAT0, SDONE jnz . + 2;
1125	test    SSTAT1, PHASEMIS jz p_command_loop;
1126	/*
1127	 * Wait for our ACK to go-away on it's own
1128	 * instead of being killed by SCSIEN getting cleared.
1129	 */
1130	test	SCSISIGI, ACKI jnz .;
1131	and	DFCNTRL, ~(SCSIEN|SDMAEN|HDMAEN);
1132	test	DFCNTRL, (SCSIEN|SDMAEN|HDMAEN) jnz .;
1133	jmp	ITloop;
1134
1135/*
1136 * Status phase.  Wait for the data byte to appear, then read it
1137 * and store it into the SCB.
1138 */
1139p_status:
1140	call	assert;
1141
1142	mov	SCB_SCSI_STATUS, SCSIDATL;
1143	jmp	ITloop;
1144
1145/*
1146 * Message out phase.  If MSG_OUT is MSG_IDENTIFYFLAG, build a full
1147 * indentify message sequence and send it to the target.  The host may
1148 * override this behavior by setting the MK_MESSAGE bit in the SCB
1149 * control byte.  This will cause us to interrupt the host and allow
1150 * it to handle the message phase completely on its own.  If the bit
1151 * associated with this target is set, we will also interrupt the host,
1152 * thereby allowing it to send a message on the next selection regardless
1153 * of the transaction being sent.
1154 *
1155 * If MSG_OUT is == HOST_MSG, also interrupt the host and take a message.
1156 * This is done to allow the host to send messages outside of an identify
1157 * sequence while protecting the seqencer from testing the MK_MESSAGE bit
1158 * on an SCB that might not be for the current nexus. (For example, a
1159 * BDR message in responce to a bad reselection would leave us pointed to
1160 * an SCB that doesn't have anything to do with the current target).
1161 *
1162 * Otherwise, treat MSG_OUT as a 1 byte message to send (abort, abort tag,
1163 * bus device reset).
1164 *
1165 * When there are no messages to send, MSG_OUT should be set to MSG_NOOP,
1166 * in case the target decides to put us in this phase for some strange
1167 * reason.
1168 */
1169p_mesgout_retry:
1170	or	SCSISIGO,ATNO,LASTPHASE;/* turn on ATN for the retry */
1171p_mesgout:
1172	mov	SINDEX, MSG_OUT;
1173	cmp	SINDEX, MSG_IDENTIFYFLAG jne p_mesgout_from_host;
1174	test	SCB_CONTROL,MK_MESSAGE	jnz host_message_loop;
1175	mov	FUNCTION1, SCB_SCSIID;
1176	mov	A, FUNCTION1;
1177	mov	SINDEX, TARGET_MSG_REQUEST[0];
1178	if ((ahc->features & AHC_TWIN) != 0) {
1179		/* Second Channel uses high byte bits */
1180		test	SCB_SCSIID, TWIN_CHNLB jz . + 2;
1181		mov	SINDEX, TARGET_MSG_REQUEST[1];
1182	} else if ((ahc->features & AHC_WIDE) != 0) {
1183		test	SCB_SCSIID, 0x80	jz . + 2; /* target > 7 */
1184		mov	SINDEX, TARGET_MSG_REQUEST[1];
1185	}
1186	test	SINDEX, A	jnz host_message_loop;
1187p_mesgout_identify:
1188	or	SINDEX, MSG_IDENTIFYFLAG|DISCENB, SCB_LUN;
1189	test	SCB_CONTROL, DISCENB jnz . + 2;
1190	and	SINDEX, ~DISCENB;
1191/*
1192 * Send a tag message if TAG_ENB is set in the SCB control block.
1193 * Use SCB_TAG (the position in the kernel's SCB array) as the tag value.
1194 */
1195p_mesgout_tag:
1196	test	SCB_CONTROL,TAG_ENB jz  p_mesgout_onebyte;
1197	mov	SCSIDATL, SINDEX;	/* Send the identify message */
1198	call	phase_lock;
1199	cmp	LASTPHASE, P_MESGOUT	jne p_mesgout_done;
1200	and	SCSIDATL,TAG_ENB|SCB_TAG_TYPE,SCB_CONTROL;
1201	call	phase_lock;
1202	cmp	LASTPHASE, P_MESGOUT	jne p_mesgout_done;
1203	mov	SCB_TAG	jmp p_mesgout_onebyte;
1204/*
1205 * Interrupt the driver, and allow it to handle this message
1206 * phase and any required retries.
1207 */
1208p_mesgout_from_host:
1209	cmp	SINDEX, HOST_MSG	jne p_mesgout_onebyte;
1210	jmp	host_message_loop;
1211
1212p_mesgout_onebyte:
1213	mvi	CLRSINT1, CLRATNO;
1214	mov	SCSIDATL, SINDEX;
1215
1216/*
1217 * If the next bus phase after ATN drops is message out, it means
1218 * that the target is requesting that the last message(s) be resent.
1219 */
1220	call	phase_lock;
1221	cmp	LASTPHASE, P_MESGOUT	je p_mesgout_retry;
1222
1223p_mesgout_done:
1224	mvi	CLRSINT1,CLRATNO;	/* Be sure to turn ATNO off */
1225	mov	LAST_MSG, MSG_OUT;
1226	mvi	MSG_OUT, MSG_NOOP;	/* No message left */
1227	jmp	ITloop;
1228
1229/*
1230 * Message in phase.  Bytes are read using Automatic PIO mode.
1231 */
1232p_mesgin:
1233	mvi	ACCUM		call inb_first;	/* read the 1st message byte */
1234
1235	test	A,MSG_IDENTIFYFLAG	jnz mesgin_identify;
1236	cmp	A,MSG_DISCONNECT	je mesgin_disconnect;
1237	cmp	A,MSG_SAVEDATAPOINTER	je mesgin_sdptrs;
1238	cmp	ALLZEROS,A		je mesgin_complete;
1239	cmp	A,MSG_RESTOREPOINTERS	je mesgin_rdptrs;
1240	cmp	A,MSG_IGN_WIDE_RESIDUE	je mesgin_ign_wide_residue;
1241	cmp	A,MSG_NOOP		je mesgin_done;
1242
1243/*
1244 * Pushed message loop to allow the kernel to
1245 * run it's own message state engine.  To avoid an
1246 * extra nop instruction after signaling the kernel,
1247 * we perform the phase_lock before checking to see
1248 * if we should exit the loop and skip the phase_lock
1249 * in the ITloop.  Performing back to back phase_locks
1250 * shouldn't hurt, but why do it twice...
1251 */
1252host_message_loop:
1253	nop;
1254	mvi	INTSTAT, HOST_MSG_LOOP;
1255	call	phase_lock;
1256	cmp	RETURN_1, EXIT_MSG_LOOP	je ITloop + 1;
1257	jmp	host_message_loop;
1258
1259mesgin_ign_wide_residue:
1260if ((ahc->features & AHC_WIDE) != 0) {
1261	test	SCSIRATE, WIDEXFER jz mesgin_reject;
1262	/* Pull the residue byte */
1263	mvi	ARG_1	call inb_next;
1264	cmp	ARG_1, 0x01 jne mesgin_reject;
1265	test	SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz . + 2;
1266	test	DATA_COUNT_ODD, 0x1	jz mesgin_done;
1267	mvi	INTSTAT, IGN_WIDE_RES;
1268	jmp	mesgin_done;
1269}
1270
1271mesgin_reject:
1272	mvi	MSG_MESSAGE_REJECT	call mk_mesg;
1273mesgin_done:
1274	mov	NONE,SCSIDATL;		/*dummy read from latch to ACK*/
1275	jmp	ITloop;
1276
1277mesgin_complete:
1278/*
1279 * We received a "command complete" message.  Put the SCB_TAG into the QOUTFIFO,
1280 * and trigger a completion interrupt.  Before doing so, check to see if there
1281 * is a residual or the status byte is something other than STATUS_GOOD (0).
1282 * In either of these conditions, we upload the SCB back to the host so it can
1283 * process this information.  In the case of a non zero status byte, we
1284 * additionally interrupt the kernel driver synchronously, allowing it to
1285 * decide if sense should be retrieved.  If the kernel driver wishes to request
1286 * sense, it will fill the kernel SCB with a request sense command and set
1287 * RETURN_1 to SEND_SENSE.  If RETURN_1 is set to SEND_SENSE we redownload
1288 * the SCB, and process it as the next command by adding it to the waiting list.
1289 * If the kernel driver does not wish to request sense, it need only clear
1290 * RETURN_1, and the command is allowed to complete normally.  We don't bother
1291 * to post to the QOUTFIFO in the error cases since it would require extra
1292 * work in the kernel driver to ensure that the entry was removed before the
1293 * command complete code tried processing it.
1294 */
1295
1296/*
1297 * First check for residuals
1298 */
1299	test	SCB_SGPTR, SG_LIST_NULL jnz check_status;/* No xfer */
1300	test	SCB_SGPTR, SG_FULL_RESID jnz upload_scb;/* Never xfered */
1301	test	SCB_RESIDUAL_SGPTR, SG_LIST_NULL jz upload_scb;
1302check_status:
1303	test	SCB_SCSI_STATUS,0xff	jz complete;	/* Good Status? */
1304upload_scb:
1305	or	SCB_SGPTR, SG_RESID_VALID;
1306	mvi	DMAPARAMS, FIFORESET;
1307	mov	SCB_TAG		call dma_scb;
1308	test	SCB_SCSI_STATUS, 0xff	jz complete;	/* Just a residual? */
1309	mvi	INTSTAT, BAD_STATUS;			/* let driver know */
1310	/*
1311	 * Prepare to DMA this SCB in case we are told to retrieve sense.
1312	 * Fills a delay slot after the INTSTAT as well.
1313	 */
1314	mvi	DMAPARAMS, HDMAEN|DIRECTION|FIFORESET;
1315	cmp	RETURN_1, SEND_SENSE	jne complete;
1316	mov	SCB_TAG		call dma_scb;
1317add_to_waiting_list:
1318	mov	SCB_NEXT,WAITING_SCBH;
1319	mov	WAITING_SCBH, SCBPTR;
1320	/*
1321	 * Prepare our selection hardware before the busfree so we have a
1322	 * high probability of winning arbitration.
1323	 */
1324	call	start_selection;
1325	jmp	await_busfree;
1326
1327complete:
1328	mov	SCB_TAG call complete_post;
1329	jmp	await_busfree;
1330}
1331
1332complete_post:
1333	/* Post the SCBID in SINDEX and issue an interrupt */
1334	call	add_scb_to_free_list;
1335	mov	ARG_1, SINDEX;
1336	if ((ahc->features & AHC_QUEUE_REGS) != 0) {
1337		mov	A, SDSCB_QOFF;
1338	} else {
1339		mov	A, QOUTPOS;
1340	}
1341	mvi	QOUTFIFO_OFFSET call post_byte_setup;
1342	mov	ARG_1 call post_byte;
1343	if ((ahc->features & AHC_QUEUE_REGS) == 0) {
1344		inc 	QOUTPOS;
1345	}
1346	mvi	INTSTAT,CMDCMPLT ret;
1347
1348if ((ahc->flags & AHC_INITIATORMODE) != 0) {
1349/*
1350 * Is it a disconnect message?  Set a flag in the SCB to remind us
1351 * and await the bus going free.  If this is an untagged transaction
1352 * store the SCB id for it in our untagged target table for lookup on
1353 * a reselction.
1354 */
1355mesgin_disconnect:
1356	or	SCB_CONTROL,DISCONNECTED;
1357	if ((ahc->flags & AHC_PAGESCBS) != 0) {
1358		call	add_scb_to_disc_list;
1359	}
1360	test	SCB_CONTROL, TAG_ENB jnz await_busfree;
1361	mov	ARG_1, SCB_TAG;
1362	mov	SAVED_LUN, SCB_LUN;
1363	mov	SCB_SCSIID	call index_busy_target;
1364	mov	DINDIR, ARG_1;
1365	jmp	await_busfree;
1366
1367/*
1368 * Save data pointers message:
1369 * Copying RAM values back to SCB, for Save Data Pointers message, but
1370 * only if we've actually been into a data phase to change them.  This
1371 * protects against bogus data in scratch ram and the residual counts
1372 * since they are only initialized when we go into data_in or data_out.
1373 */
1374mesgin_sdptrs:
1375	test	SEQ_FLAGS, DPHASE	jz mesgin_done;
1376
1377	/*
1378	 * The SCB_SGPTR becomes the next one we'll download,
1379	 * and the SCB_DATAPTR becomes the current SHADDR.
1380	 * Use the residual number since STCNT is corrupted by
1381	 * any message transfer.
1382	 */
1383	if ((ahc->features & AHC_CMD_CHAN) != 0) {
1384		bmov	SCB_DATAPTR, SHADDR, 4;
1385		bmov	SCB_DATACNT, SCB_RESIDUAL_DATACNT, 8;
1386	} else {
1387		mvi	DINDEX, SCB_DATAPTR;
1388		mvi	SHADDR call bcopy_4;
1389		mvi	SCB_RESIDUAL_DATACNT call bcopy_8;
1390	}
1391	jmp	mesgin_done;
1392
1393/*
1394 * Restore pointers message?  Data pointers are recopied from the
1395 * SCB anytime we enter a data phase for the first time, so all
1396 * we need to do is clear the DPHASE flag and let the data phase
1397 * code do the rest.
1398 */
1399mesgin_rdptrs:
1400	and	SEQ_FLAGS, ~DPHASE;		/*
1401						 * We'll reload them
1402						 * the next time through
1403						 * the dataphase.
1404						 */
1405	jmp	mesgin_done;
1406
1407/*
1408 * Index into our Busy Target table.  SINDEX and DINDEX are modified
1409 * upon return.  SCBPTR may be modified by this action.
1410 */
1411index_busy_target:
1412	if ((ahc->features & AHC_SCB_BTT) != 0) {
1413		mov	SCBPTR, SAVED_LUN;
1414		add	SINDEX, SCB_64_BTT;
1415	} else {
1416		shr	SINDEX, 4;
1417		add	SINDEX, BUSY_TARGETS;
1418	}
1419	mov	DINDEX, SINDEX ret;
1420
1421/*
1422 * Identify message?  For a reconnecting target, this tells us the lun
1423 * that the reconnection is for - find the correct SCB and switch to it,
1424 * clearing the "disconnected" bit so we don't "find" it by accident later.
1425 */
1426mesgin_identify:
1427	and	SAVED_LUN, MSG_IDENTIFY_LUNMASK, A;
1428	/*
1429	 * Determine whether a target is using tagged or non-tagged
1430	 * transactions by first looking at the transaction stored in
1431	 * the busy target array.  If there is no untagged transaction
1432	 * for this target or the transaction is for a different lun, then
1433	 * this must be an untagged transaction.
1434	 */
1435	mov	SAVED_SCSIID	call	index_busy_target;
1436	mov	A, SINDIR;
1437	cmp	A, SCB_LIST_NULL	je snoop_tag;
1438	if ((ahc->flags & AHC_PAGESCBS) != 0) {
1439		mov	A	call findSCB;
1440	} else {
1441		mov	SCBPTR, A;
1442	}
1443	if ((ahc->features & AHC_SCB_BTT) != 0) {
1444		jmp setup_SCB_id_lun_okay;
1445	} else {
1446		mov	A, SCB_LUN;
1447		cmp	SAVED_LUN, A		je setup_SCB_id_lun_okay;
1448	}
1449
1450/*
1451 * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
1452 * If we get one, we use the tag returned to find the proper
1453 * SCB.  With SCB paging, we must search for non-tagged
1454 * transactions since the SCB may exist in any slot.  If we're not
1455 * using SCB paging, we can use the tag as the direct index to the
1456 * SCB.
1457 */
1458snoop_tag:
1459	mov	NONE,SCSIDATL;		/* ACK Identify MSG */
1460snoop_tag_loop:
1461	call	phase_lock;
1462	cmp	LASTPHASE, P_MESGIN	jne not_found;
1463	cmp	SCSIBUSL,MSG_SIMPLE_Q_TAG jne not_found;
1464get_tag:
1465	if ((ahc->flags & AHC_PAGESCBS) != 0) {
1466		mvi	ARG_1	call inb_next;	/* tag value */
1467		mov	ARG_1	call findSCB;
1468	} else {
1469		mvi	SCBPTR	call inb_next;	/* tag value */
1470	}
1471
1472/*
1473 * Ensure that the SCB the tag points to is for
1474 * an SCB transaction to the reconnecting target.
1475 */
1476setup_SCB:
1477	mov	A, SAVED_SCSIID;
1478	cmp	SCB_SCSIID, A	jne not_found;
1479	mov	A, SAVED_LUN;
1480	cmp	SCB_LUN, A	jne not_found;
1481setup_SCB_id_lun_okay:
1482	test	SCB_CONTROL,DISCONNECTED jz not_found;
1483	if ((ahc->flags & AHC_PAGESCBS) != 0) {
1484		mov	SCBPTR	call	rem_scb_from_disc_list;
1485	}
1486	and	SCB_CONTROL,~DISCONNECTED;
1487	or	SEQ_FLAGS,IDENTIFY_SEEN;	/* make note of IDENTIFY */
1488	test	SCB_CONTROL, TAG_ENB	jnz setup_SCB_tagged;
1489	mov	A, SCBPTR;
1490	mov	SAVED_SCSIID	call	index_busy_target;
1491	mvi	DINDIR, SCB_LIST_NULL;
1492	mov	SCBPTR, A;
1493setup_SCB_tagged:
1494	call	set_transfer_settings;
1495	/* See if the host wants to send a message upon reconnection */
1496	test	SCB_CONTROL, MK_MESSAGE jz mesgin_done;
1497	and	SCB_CONTROL, ~MK_MESSAGE;
1498	mvi	HOST_MSG	call mk_mesg;
1499	jmp	mesgin_done;
1500
1501not_found:
1502	mvi	INTSTAT, NO_MATCH;
1503	jmp	mesgin_done;
1504
1505mk_mesg:
1506	or	SCSISIGO,ATNO,LASTPHASE;/* turn on ATNO */
1507	mov	MSG_OUT,SINDEX ret;
1508
1509/*
1510 * Functions to read data in Automatic PIO mode.
1511 *
1512 * According to Adaptec's documentation, an ACK is not sent on input from
1513 * the target until SCSIDATL is read from.  So we wait until SCSIDATL is
1514 * latched (the usual way), then read the data byte directly off the bus
1515 * using SCSIBUSL.  When we have pulled the ATN line, or we just want to
1516 * acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
1517 * spec guarantees that the target will hold the data byte on the bus until
1518 * we send our ACK.
1519 *
1520 * The assumption here is that these are called in a particular sequence,
1521 * and that REQ is already set when inb_first is called.  inb_{first,next}
1522 * use the same calling convention as inb.
1523 */
1524inb_next_wait_perr:
1525	mvi	INTSTAT, PERR_DETECTED;
1526	jmp	inb_next_wait;
1527inb_next:
1528	mov	NONE,SCSIDATL;		/*dummy read from latch to ACK*/
1529inb_next_wait:
1530	/*
1531	 * If there is a parity error, wait for the kernel to
1532	 * see the interrupt and prepare our message response
1533	 * before continuing.
1534	 */
1535	test	SSTAT1, REQINIT	jz inb_next_wait;
1536	test	SSTAT1, SCSIPERR jnz inb_next_wait_perr;
1537inb_next_check_phase:
1538	and	LASTPHASE, PHASE_MASK, SCSISIGI;
1539	cmp	LASTPHASE, P_MESGIN jne mesgin_phasemis;
1540inb_first:
1541	mov	DINDEX,SINDEX;
1542	mov	DINDIR,SCSIBUSL	ret;		/*read byte directly from bus*/
1543inb_last:
1544	mov	NONE,SCSIDATL ret;		/*dummy read from latch to ACK*/
1545}
1546
1547if ((ahc->flags & AHC_TARGETMODE) != 0) {
1548/*
1549 * Change to a new phase.  If we are changing the state of the I/O signal,
1550 * from out to in, wait an additional data release delay before continuing.
1551 */
1552change_phase:
1553	/* Wait for preceeding I/O session to complete. */
1554	test	SCSISIGI, ACKI jnz .;
1555
1556	/* Change the phase */
1557	and	DINDEX, IOI, SCSISIGI;
1558	mov	SCSISIGO, SINDEX;
1559	and	A, IOI, SINDEX;
1560
1561	/*
1562	 * If the data direction has changed, from
1563	 * out (initiator driving) to in (target driving),
1564	 * we must wait at least a data release delay plus
1565	 * the normal bus settle delay. [SCSI III SPI 10.11.0]
1566	 */
1567	cmp 	DINDEX, A je change_phase_wait;
1568	test	SINDEX, IOI jz change_phase_wait;
1569	call	change_phase_wait;
1570change_phase_wait:
1571	nop;
1572	nop;
1573	nop;
1574	nop ret;
1575
1576/*
1577 * Send a byte to an initiator in Automatic PIO mode.
1578 */
1579target_outb:
1580	or	SXFRCTL0, SPIOEN;
1581	test	SSTAT0, SPIORDY	jz .;
1582	mov	SCSIDATL, SINDEX;
1583	test	SSTAT0, SPIORDY	jz .;
1584	and	SXFRCTL0, ~SPIOEN ret;
1585}
1586
1587
1588/*
1589 * Assert that if we've been reselected, then we've seen an IDENTIFY
1590 * message.
1591 */
1592assert:
1593	test	SEQ_FLAGS,IDENTIFY_SEEN	jnz return;	/* seen IDENTIFY? */
1594
1595	mvi	INTSTAT,NO_IDENT 	ret;	/* no - tell the kernel */
1596
1597/*
1598 * Locate a disconnected SCB by SCBID.  Upon return, SCBPTR and SINDEX will
1599 * be set to the position of the SCB.  If the SCB cannot be found locally,
1600 * it will be paged in from host memory.  RETURN_2 stores the address of the
1601 * preceding SCB in the disconnected list which can be used to speed up
1602 * removal of the found SCB from the disconnected list.
1603 */
1604findSCB:
1605	mov	SCBPTR, DISCONNECTED_SCBH;	/* Initialize SCBPTR */
1606	mov	A, SINDEX;			/* Tag passed in SINDEX */
1607	mvi	RETURN_2, SCB_LIST_NULL;	/* Head of list */
1608	jmp	findSCB_loop;
1609findSCB_next:
1610	mov	RETURN_2, SCBPTR;
1611	cmp	SCB_NEXT, SCB_LIST_NULL je findSCB_notFound;
1612	mov	SCBPTR,SCB_NEXT;
1613findSCB_loop:
1614	cmp	SCB_TAG, A	jne findSCB_next;
1615	mov	SINDEX, SCBPTR 	ret;
1616findSCB_notFound:
1617	/*
1618	 * We didn't find it.  Page in the SCB and make it look
1619	 * like it was at the head of the appropriate scb list.
1620	 */
1621	mov	ARG_1, A; /* Save tag */
1622	mov	ALLZEROS call get_free_or_disc_scb;
1623	mvi	DMAPARAMS, HDMAEN|DIRECTION|FIFORESET;
1624	mov	ARG_1	call dma_scb;
1625	mvi	RETURN_2, SCB_LIST_NULL;	/* Head of list */
1626	/* Jump instead of call as we want to return anyway */
1627	test	SCB_CONTROL, DISCONNECTED jnz add_scb_to_disc_list;
1628	jmp	add_scb_to_free_list;
1629
1630/*
1631 * This routine expects SINDEX to contain the index of the SCB to be
1632 * removed, SCBPTR to be pointing to that SCB, and ARG_2 to be the
1633 * SCBID of the SCB just previous to this one in the list or SCB_LIST_NULL
1634 * if it is at the head.
1635 */
1636rem_scb_from_disc_list:
1637/* Remove this SCB from the disconnection list */
1638	cmp	ARG_2, SCB_LIST_NULL	je rHead;
1639	mov	DINDEX, SCB_NEXT;
1640	mov	SCBPTR, ARG_2;
1641	mov	SCB_NEXT, DINDEX;
1642	mov	SCBPTR, SINDEX ret;
1643rHead:
1644	mov	DISCONNECTED_SCBH,SCB_NEXT ret;
1645
1646/*
1647 * Fetch a byte from host memory given an index of (A + (256 * SINDEX))
1648 * and a base address of SHARED_DATA_ADDR.  The byte is returned in RETURN_2.
1649 */
1650fetch_byte:
1651	mov	ARG_2, SINDEX;
1652	if ((ahc->features & AHC_CMD_CHAN) != 0) {
1653		mvi	DINDEX, CCHADDR;
1654		mvi	SHARED_DATA_ADDR call set_1byte_addr;
1655		mvi	CCHCNT, 1;
1656		mvi	CCSGCTL, CCSGEN|CCSGRESET;
1657		test	CCSGCTL, CCSGDONE jz .;
1658		mvi	CCSGCTL, CCSGRESET;
1659		bmov	RETURN_2, CCSGRAM, 1 ret;
1660	} else {
1661		mvi	DINDEX, HADDR;
1662		mvi	SHARED_DATA_ADDR call set_1byte_addr;
1663		mvi	1	call set_hcnt;
1664		mvi	DFCNTRL, HDMAEN|DIRECTION|FIFORESET;
1665		call	dma_finish;
1666		mov	RETURN_2, DFDAT ret;
1667	}
1668
1669/*
1670 * Prepare the hardware to post a byte to host memory given an
1671 * index of (A + (256 * SINDEX)) and a base address of SHARED_DATA_ADDR.
1672 */
1673post_byte_setup:
1674	mov	ARG_2, SINDEX;
1675	if ((ahc->features & AHC_CMD_CHAN) != 0) {
1676		mvi	DINDEX, CCHADDR;
1677		mvi	SHARED_DATA_ADDR call	set_1byte_addr;
1678		mvi	CCHCNT, 1;
1679		mvi	CCSCBCTL, CCSCBRESET ret;
1680	} else {
1681		mvi	DINDEX, HADDR;
1682		mvi	SHARED_DATA_ADDR call	set_1byte_addr;
1683		mvi	1	call set_hcnt;
1684		mvi	DFCNTRL, FIFORESET ret;
1685	}
1686
1687post_byte:
1688	if ((ahc->features & AHC_CMD_CHAN) != 0) {
1689		bmov	CCSCBRAM, SINDEX, 1;
1690		or	CCSCBCTL, CCSCBEN|CCSCBRESET;
1691		test	CCSCBCTL, CCSCBDONE jz .;
1692		clr	CCSCBCTL ret;
1693	} else {
1694		mov	DFDAT, SINDEX;
1695		or	DFCNTRL, HDMAEN|FIFOFLUSH;
1696		jmp	dma_finish;
1697	}
1698
1699get_SCBID_from_host:
1700	mov	A, SAVED_LUN;
1701	shr	SINDEX, 4, SAVED_SCSIID;
1702	call	fetch_byte;
1703	mov	RETURN_1,  RETURN_2 ret;
1704
1705phase_lock_perr:
1706	mvi	INTSTAT, PERR_DETECTED;
1707phase_lock:
1708	/*
1709	 * If there is a parity error, wait for the kernel to
1710	 * see the interrupt and prepare our message response
1711	 * before continuing.
1712	 */
1713	test	SSTAT1, REQINIT jz phase_lock;
1714	test	SSTAT1, SCSIPERR jnz phase_lock_perr;
1715phase_lock_latch_phase:
1716	and	SCSISIGO, PHASE_MASK, SCSISIGI;
1717	and	LASTPHASE, PHASE_MASK, SCSISIGI ret;
1718
1719if ((ahc->features & AHC_CMD_CHAN) == 0) {
1720set_hcnt:
1721	mov	HCNT[0], SINDEX;
1722clear_hcnt:
1723	clr	HCNT[1];
1724	clr	HCNT[2] ret;
1725
1726set_stcnt_from_hcnt:
1727	mov	STCNT[0], HCNT[0];
1728	mov	STCNT[1], HCNT[1];
1729	mov	STCNT[2], HCNT[2] ret;
1730
1731bcopy_8:
1732	mov	DINDIR, SINDIR;
1733bcopy_7:
1734	mov	DINDIR, SINDIR;
1735	mov	DINDIR, SINDIR;
1736bcopy_5:
1737	mov	DINDIR, SINDIR;
1738bcopy_4:
1739	mov	DINDIR, SINDIR;
1740bcopy_3:
1741	mov	DINDIR, SINDIR;
1742	mov	DINDIR, SINDIR;
1743	mov	DINDIR, SINDIR ret;
1744}
1745
1746if ((ahc->flags & AHC_TARGETMODE) != 0) {
1747/*
1748 * Setup addr assuming that A is an index into
1749 * an array of 32byte objects, SINDEX contains
1750 * the base address of that array, and DINDEX
1751 * contains the base address of the location
1752 * to store the indexed address.
1753 */
1754set_32byte_addr:
1755	shr	ARG_2, 3, A;
1756	shl	A, 5;
1757	jmp	set_1byte_addr;
1758}
1759
1760/*
1761 * Setup addr assuming that A is an index into
1762 * an array of 64byte objects, SINDEX contains
1763 * the base address of that array, and DINDEX
1764 * contains the base address of the location
1765 * to store the indexed address.
1766 */
1767set_64byte_addr:
1768	shr	ARG_2, 2, A;
1769	shl	A, 6;
1770
1771/*
1772 * Setup addr assuming that A + (ARG_2 * 256) is an
1773 * index into an array of 1byte objects, SINDEX contains
1774 * the base address of that array, and DINDEX contains
1775 * the base address of the location to store the computed
1776 * address.
1777 */
1778set_1byte_addr:
1779	add     DINDIR, A, SINDIR;
1780	mov     A, ARG_2;
1781	adc	DINDIR, A, SINDIR;
1782	clr	A;
1783	adc	DINDIR, A, SINDIR;
1784	adc	DINDIR, A, SINDIR ret;
1785
1786/*
1787 * Either post or fetch and SCB from host memory based on the
1788 * DIRECTION bit in DMAPARAMS. The host SCB index is in SINDEX.
1789 */
1790dma_scb:
1791	mov	A, SINDEX;
1792	if ((ahc->features & AHC_CMD_CHAN) != 0) {
1793		mvi	DINDEX, CCHADDR;
1794		mvi	HSCB_ADDR call set_64byte_addr;
1795		mov	CCSCBPTR, SCBPTR;
1796		test	DMAPARAMS, DIRECTION jz dma_scb_tohost;
1797		mvi	CCHCNT, SCB_64BYTE_SIZE;
1798		mvi	CCSCBCTL, CCARREN|CCSCBEN|CCSCBDIR|CCSCBRESET;
1799		cmp	CCSCBCTL, CCSCBDONE|ARRDONE|CCARREN|CCSCBEN|CCSCBDIR jne .;
1800		jmp	dma_scb_finish;
1801dma_scb_tohost:
1802		mvi	CCHCNT, SCB_32BYTE_SIZE;
1803		if ((ahc->chip & AHC_CHIPID_MASK) == AHC_AIC7895) {
1804			mvi	CCSCBCTL, CCSCBRESET;
1805			bmov	CCSCBRAM, SCB_CONTROL, SCB_32BYTE_SIZE;
1806			or	CCSCBCTL, CCSCBEN|CCSCBRESET;
1807			test	CCSCBCTL, CCSCBDONE jz .;
1808		} else {
1809			mvi	CCSCBCTL, CCARREN|CCSCBEN|CCSCBRESET;
1810			cmp	CCSCBCTL, CCSCBDONE|ARRDONE|CCARREN|CCSCBEN jne .;
1811		}
1812dma_scb_finish:
1813		clr	CCSCBCTL;
1814		test	CCSCBCTL, CCARREN|CCSCBEN jnz .;
1815		ret;
1816	} else {
1817		mvi	DINDEX, HADDR;
1818		mvi	HSCB_ADDR call set_64byte_addr;
1819		mvi	SCB_32BYTE_SIZE	call set_hcnt;
1820		mov	DFCNTRL, DMAPARAMS;
1821		test	DMAPARAMS, DIRECTION	jnz dma_scb_fromhost;
1822		/* Fill it with the SCB data */
1823copy_scb_tofifo:
1824		mvi	SINDEX, SCB_CONTROL;
1825		add	A, SCB_32BYTE_SIZE, SINDEX;
1826copy_scb_tofifo_loop:
1827		call	copy_to_fifo_6;
1828		cmp	SINDEX, A jne copy_scb_tofifo_loop;
1829		or	DFCNTRL, HDMAEN|FIFOFLUSH;
1830dma_scb_fromhost:
1831		call	dma_finish;
1832		/* If we were putting the SCB, we are done */
1833		test	DMAPARAMS, DIRECTION	jz	return;
1834		mvi	SCB_CONTROL  call dfdat_in_7;
1835		call	dfdat_in_7_continued;
1836		call	dfdat_in_7_continued;
1837		call	dfdat_in_7_continued;
1838		jmp	dfdat_in_2_continued;
1839dfdat_in_7:
1840		mov     DINDEX,SINDEX;
1841dfdat_in_7_continued:
1842		mov	DINDIR,DFDAT;
1843		mov	DINDIR,DFDAT;
1844		mov	DINDIR,DFDAT;
1845		mov	DINDIR,DFDAT;
1846		mov	DINDIR,DFDAT;
1847dfdat_in_2_continued:
1848		mov	DINDIR,DFDAT;
1849		mov	DINDIR,DFDAT ret;
1850	}
1851
1852copy_to_fifo_8:
1853	mov	DFDAT,SINDIR;
1854copy_to_fifo_7:
1855	mov	DFDAT,SINDIR;
1856copy_to_fifo_6:
1857	mov	DFDAT,SINDIR;
1858copy_to_fifo_5:
1859	mov	DFDAT,SINDIR;
1860copy_to_fifo_4:
1861	mov	DFDAT,SINDIR;
1862	mov	DFDAT,SINDIR;
1863	mov	DFDAT,SINDIR;
1864	mov	DFDAT,SINDIR ret;
1865
1866/*
1867 * Wait for DMA from host memory to data FIFO to complete, then disable
1868 * DMA and wait for it to acknowledge that it's off.
1869 */
1870dma_finish:
1871	test	DFSTATUS,HDONE	jz dma_finish;
1872	/* Turn off DMA */
1873	and	DFCNTRL, ~HDMAEN;
1874	test	DFCNTRL, HDMAEN jnz .;
1875	ret;
1876
1877add_scb_to_free_list:
1878	if ((ahc->flags & AHC_PAGESCBS) != 0) {
1879		mov	SCB_NEXT, FREE_SCBH;
1880		mvi	SCB_TAG, SCB_LIST_NULL;
1881		mov	FREE_SCBH, SCBPTR ret;
1882	} else {
1883		mvi	SCB_TAG, SCB_LIST_NULL ret;
1884	}
1885
1886if ((ahc->flags & AHC_PAGESCBS) != 0) {
1887get_free_or_disc_scb:
1888	cmp	FREE_SCBH, SCB_LIST_NULL jne dequeue_free_scb;
1889	cmp	DISCONNECTED_SCBH, SCB_LIST_NULL jne dequeue_disc_scb;
1890return_error:
1891	mvi	SINDEX, SCB_LIST_NULL	ret;
1892dequeue_disc_scb:
1893	mov	SCBPTR, DISCONNECTED_SCBH;
1894dma_up_scb:
1895	mvi	DMAPARAMS, FIFORESET;
1896	mov	SCB_TAG		call dma_scb;
1897unlink_disc_scb:
1898	mov	DISCONNECTED_SCBH, SCB_NEXT ret;
1899dequeue_free_scb:
1900	mov	SCBPTR, FREE_SCBH;
1901	mov	FREE_SCBH, SCB_NEXT ret;
1902}
1903
1904add_scb_to_disc_list:
1905/*
1906 * Link this SCB into the DISCONNECTED list.  This list holds the
1907 * candidates for paging out an SCB if one is needed for a new command.
1908 * Modifying the disconnected list is a critical(pause dissabled) section.
1909 */
1910	mov	SCB_NEXT, DISCONNECTED_SCBH;
1911	mov	DISCONNECTED_SCBH, SCBPTR ret;
1912return:
1913	ret;
1914