aic7xxx.seq revision 21982
1130268Sdwmalone/*+M***********************************************************************
278064Sume *Adaptec 274x/284x/294x device driver for Linux and FreeBSD.
355163Sshin *
455163Sshin *Copyright (c) 1994 John Aycock
555163Sshin *  The University of Calgary Department of Computer Science.
655163Sshin *  All rights reserved.
755163Sshin *
855163Sshin *FreeBSD, Twin, Wide, 2 command per target support, tagged queuing,
955163Sshin *SCB paging and other optimizations:
1055163Sshin *Copyright (c) 1994, 1995, 1996 Justin Gibbs. All rights reserved.
1155163Sshin *
1255163Sshin *Redistribution and use in source and binary forms, with or without
1355163Sshin *modification, are permitted provided that the following conditions
1455163Sshin *are met:
1555163Sshin *1. Redistributions of source code must retain the above copyright
1655163Sshin *   notice, this list of conditions, and the following disclaimer.
1755163Sshin *2. Redistributions in binary form must reproduce the above copyright
1855163Sshin *   notice, this list of conditions and the following disclaimer in the
1955163Sshin *   documentation and/or other materials provided with the distribution.
2055163Sshin *3. All advertising materials mentioning features or use of this software
2155163Sshin *   must display the following acknowledgement:
2255163Sshin *     This product includes software developed by the University of Calgary
2355163Sshin *     Department of Computer Science and its contributors.
2455163Sshin *4. Neither the name of the University nor the names of its contributors
2555163Sshin *   may be used to endorse or promote products derived from this software
2655163Sshin *   without specific prior written permission.
2755163Sshin *
2855163Sshin *THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2955163Sshin *ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30141580Sru *IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3155163Sshin *ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32196475Sume *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3355163Sshin *DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3459849Sphantom *OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3555163Sshin *HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3655163Sshin *LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3755163Sshin *OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38130268Sdwmalone *SUCH DAMAGE.
3955163Sshin *
4055163Sshin *-M************************************************************************/
4168965Sru
4262637SkrisVERSION AIC7XXX_SEQ_VER "$FreeBSD: head/sys/dev/aic7xxx/aic7xxx.seq 21982 1997-01-24 21:57:47Z gibbs $"
43196475Sume
4462637Skris#if defined(__NetBSD__)
4562637Skris#include "../../../../dev/ic/aic7xxxreg.h"
4662637Skris#include "../../../../scsi/scsi_message.h"
4762637Skris#elif defined(__FreeBSD__)
4862637Skris#include "../../dev/aic7xxx/aic7xxx_reg.h"
4962637Skris#include "../../scsi/scsi_message.h"
5062637Skris#endif
5162637Skris
5255163Sshin/*
5362637Skris * We can't just use ACCUM in the sequencer code because it
5462637Skris * must be treated specially by the assembler, and it currently
5555163Sshin * looks for the symbol 'A'.  This is the only register defined in
5662637Skris * the assembler's symbol space.
5762637Skris */
5855163SshinA = ACCUM
5962637Skris
6062637Skris/*
6155163Sshin * A few words on the waiting SCB list:
6262637Skris * After starting the selection hardware, we check for reconnecting targets
6362637Skris * as well as for our selection to complete just in case the reselection wins
6455163Sshin * bus arbitration.  The problem with this is that we must keep track of the
6562637Skris * SCB that we've already pulled from the QINFIFO and started the selection
6662637Skris * on just in case the reselection wins so that we can retry the selection at
67196475Sume * a later time.  This problem cannot be resolved by holding a single entry
68196475Sume * in scratch ram since a reconnecting target can request sense and this will
69196475Sume * create yet another SCB waiting for selection.  The solution used here is to 
7062637Skris * use byte 27 of the SCB as a psuedo-next pointer and to thread a list
7162637Skris * of SCBs that are awaiting selection.  Since 0-0xfe are valid SCB indexes, 
7262637Skris * SCB_LIST_NULL is 0xff which is out of range.  An entry is also added to
7355163Sshin * this list everytime a request sense occurs or after completing a non-tagged
7455163Sshin * command for which a second SCB has been queued.  The sequencer will
75131754Sru * automatically consume the entries.
76130268Sdwmalone */
77131754Sru
78130268Sdwmalone/*
79130268Sdwmalone * We assume that the kernel driver may reset us at any time, even in the
80130268Sdwmalone * middle of a DMA, so clear DFCNTRL too.
81130268Sdwmalone */
82130268Sdwmalonereset:
83130268Sdwmalone	clr	DFCNTRL
84131754Sru	clr	SCSISIGO		/* De-assert BSY */
85131754Sru
86131754Srup_busfree:
87130268Sdwmalone	or	SXFRCTL0, CLRCHN
88130268Sdwmalone	clr	SCSIRATE		/*
8955163Sshin					 * We don't know the target we will
90196475Sume					 * connect to, so default to narrow
91196475Sume					 * transfers to avoid parity problems.
92196475Sume					 */
93196475Sume	mvi	SCSISEQ,ENRSELI		/* Always allow reselection */
9455163Sshin	mvi	LASTPHASE, P_BUSFREE
9555163Sshin	and	FLAGS,0x07		/* clear target specific flags */
9662637Skrispoll_for_work:
9762637Skris	/*
9862637Skris	 * Are we a twin channel device?
9962637Skris	 * For fairness, we check the other bus first,
100130135Sru	 * since we just finished a transaction on the
101130135Sru	 * current channel.
102122574Sume	 */
103122574Sume	test	FLAGS,TWIN_BUS	jz start2
10455163Sshin	xor	SBLKCTL,SELBUSB			/* Toggle to the other bus */
10555163Sshin	test	SSTAT0,SELDI	jnz reselect
10655163Sshin	xor	SBLKCTL,SELBUSB			/* Toggle to the original bus */
10755163Sshinstart2:
10855163Sshin	test	SSTAT0,SELDI	jnz reselect
10955163Sshin	cmp	WAITING_SCBH,SCB_LIST_NULL je test_queue
11055163Sshinstart_waiting:
11155163Sshin	/*
11255163Sshin	 * Pull the first entry off of the waiting SCB list
113154190Spav	 * We don't have to "test_busy" because only transactions that
114154190Spav	 * have passed that test can be in the WAITING_SCB list.
115154190Spav	 */
11655163Sshin	mov	SCBPTR,WAITING_SCBH
11755163Sshin	jmp	start_scb2
118176154Sdwmalonetest_queue:
119176154Sdwmalone	/* Has the driver posted any work for us? */
120176154Sdwmalone	mov	A, QCNTMASK
12155163Sshin	test	QINCNT,A	jz poll_for_work
12255163Sshin
12355163Sshin/*
12455163Sshin * We have at least one queued SCB now and we don't have any 
12555163Sshin * SCBs in the list of SCBs awaiting selection.  If we have
12655163Sshin * any SCBs availible for use, pull the tag from the QINFIFO
12755163Sshin * and get to work on it.
128153638Sdd */
129153638Sdd	test	FLAGS, PAGESCBS	jz	dequeue_scb
130153638Sdd	mov	ALLZEROS	call	get_free_or_disc_scb
131153638Sdd	cmp	SINDEX, SCB_LIST_NULL	je poll_for_work
132153638Sdddequeue_scb:
133153638Sdd	mov	CUR_SCBID,QINFIFO
134153638Sdd	test	FLAGS, PAGESCBS jnz dma_queued_scb
135153638Sdd	/* In the non-paging case, the SCBID == hardware SCB index */
136153638Sdd	mov	SCBPTR, CUR_SCBID
137153638Sdddma_queued_scb:
13855163Sshin/*
13955163Sshin * DMA the SCB from host ram into the current SCB location.
14055163Sshin */
141176154Sdwmalone	mvi	DMAPARAMS, 0xd	/* HDMAEN|DIRECTION|FIFORESET */
142176154Sdwmalone	mov	CUR_SCBID	call dma_scb
143176154Sdwmalone
14455163Sshin/*
14555163Sshin * See if there is not already an active SCB for this target.  This code
14655163Sshin * locks out on a per target basis instead of target/lun.  Although this
14755163Sshin * is not ideal for devices that have multiple luns active at the same
14855163Sshin * time, it is faster than looping through all SCB's looking for active
149130268Sdwmalone * commands.  We also don't have enough spare SCB space for us to store the
150131754Sru * SCBID of the currently busy transaction for each target/lun making it
151130268Sdwmalone * impossible to link up the SCBs.
152130268Sdwmalone */
153130268Sdwmalonetest_busy:
154130268Sdwmalone	test	SCB_CONTROL, TAG_ENB	jnz start_scb
155131754Sru	mov	SAVED_SCBPTR, SCBPTR
156130268Sdwmalone	mov	SCB_TCL		call	index_untagged_scb
157131754Sru	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
158130268Sdwmalone	mov	ARG_1, SINDIR			/*
159131754Sru						 * ARG_1 should
160130268Sdwmalone						 * now have the SCB ID of
161131754Sru						 * any active, non-tagged,
162130268Sdwmalone						 * command for this target.
163131754Sru						 */
164130268Sdwmalone	cmp	ARG_1, SCB_LIST_NULL je make_busy
165130268Sdwmalone	test	FLAGS, PAGESCBS jz simple_busy_link
166130268Sdwmalone	/*
167130268Sdwmalone	 * Put this SCB back onto the free list.  It
168130268Sdwmalone	 * may be necessary to satisfy the search for
169130268Sdwmalone	 * the active SCB.
170130268Sdwmalone	 */
17155163Sshin	mov	SCBPTR, SAVED_SCBPTR
172186845Sdanger	call	add_scb_to_free_list
17359849Sphantom	/* Find the active SCB */
17459849Sphantom	mov	ALLZEROS	call findSCB
175130135Sru	/*
17655163Sshin	 * If we couldn't find it, tell the kernel.  This should
17755163Sshin	 * never happen.
17855163Sshin	 */
17955163Sshin	cmp	SINDEX, SCB_LIST_NULL	jne paged_busy_link
18055163Sshin	mvi	INTSTAT, NO_MATCH_BUSY
18155163Sshinpaged_busy_link:
18255163Sshin	/* Link us in */
18355163Sshin	mov	SCB_LINKED_NEXT, CUR_SCBID 
18455163Sshin	/* Put it back on the disconnected list */
185130135Sru	call	add_scb_to_disc_list
186	mvi	SEQCTL,0x10			/* FASTMODE */
187	jmp	poll_for_work
188simple_busy_link:
189	mov	SCBPTR, ARG_1
190	mov	SCB_LINKED_NEXT, CUR_SCBID 
191	mvi	SEQCTL,0x10			/* FASTMODE */
192	jmp	poll_for_work
193make_busy:
194	mvi	SEQCTL,0x10			/* FASTMODE */
195	mov	DINDIR, CUR_SCBID
196	mov	SCBPTR, SAVED_SCBPTR
197
198start_scb:
199	/*
200	 * Place us on the waiting list in case our selection
201	 * doesn't win during bus arbitration.
202	 */
203	mov	SCB_NEXT,WAITING_SCBH
204	mov	WAITING_SCBH, SCBPTR
205start_scb2:
206	and	SINDEX,0xf7,SBLKCTL	/* Clear the channel select bit */
207	and	A,0x08,SCB_TCL		/* Get new channel bit */
208	or	SINDEX,A
209	mov	SBLKCTL,SINDEX		/* select channel */
210	mov	SCB_TCL	call initialize_scsiid
211
212/*
213 * Enable selection phase as an initiator, and do automatic ATN
214 * after the selection.  We do this now so that we can overlap the
215 * rest of our work to set up this target with the arbitration and
216 * selection bus phases.
217 */
218start_selection:
219	mvi	SCSISEQ,0x58		/* ENSELO|ENAUTOATNO|ENRSELI */
220
221/*
222 * As soon as we get a successful selection, the target should go
223 * into the message out phase since we have ATN asserted.  Prepare
224 * the message to send.
225 *
226 * Messages are stored in scratch RAM starting with a length byte
227 * followed by the message itself.
228 */
229
230mk_identify:
231	and	MSG0,0x7,SCB_TCL	/* lun */
232	and	A,DISCENB,SCB_CONTROL	/* mask off disconnect privledge */
233	or	MSG0,A			/* or in disconnect privledge */
234	or	MSG0,MSG_IDENTIFYFLAG
235	mvi	MSG_LEN, 1
236
237/*
238 * Send a tag message if TAG_ENB is set in the SCB control block.
239 * Use SCB_TAG (the position in the kernel's SCB array) as the tag value.
240 */
241mk_tag:
242	test	SCB_CONTROL,TAG_ENB jz  mk_message
243	and	MSG1,0x23,SCB_CONTROL
244	mov	MSG2,SCB_TAG
245	add	MSG_LEN,2	/* update message length */
246
247/*
248 * Interrupt the driver, and allow it to tweak the message buffer
249 * if it asks.
250 */
251mk_message:
252	test	SCB_CONTROL,MK_MESSAGE  jz wait_for_selection
253
254	mvi     INTSTAT,AWAITING_MSG
255
256wait_for_selection:
257	test	SSTAT0,SELDO	jnz select 
258	test	SSTAT0,SELDI	jz wait_for_selection
259
260/*
261 * Reselection has been initiated by a target. Make a note that we've been
262 * reselected, but haven't seen an IDENTIFY message from the target yet.
263 */
264reselect:
265	clr	MSG_LEN		/* Don't have anything in the mesg buffer */
266	/* XXX test for and handle ONE BIT condition */
267	and	SAVED_TCL, 0xf0, SELID
268	or	FLAGS,RESELECTED
269	jmp	select2
270
271/*
272 * After the selection, remove this SCB from the "waiting SCB"
273 * list.  This is achieved by simply moving our "next" pointer into
274 * WAITING_SCBH.  Our next pointer will be set to null the next time this
275 * SCB is used, so don't bother with it now.
276 */
277select:
278	mov	WAITING_SCBH,SCB_NEXT
279select2:
280	/* Turn off the selection hardware */
281	mvi	SCSISEQ,ENAUTOATNP		/*
282						 * ATN on parity errors
283						 * for "in" phases
284						 */
285	mvi	CLRSINT0,0x60			/* CLRSELDI|CLRSELDO */
286	mvi	CLRSINT1,CLRBUSFREE
287	or	SIMODE1, ENBUSFREE		/*
288						 * We aren't expecting a
289						 * bus free, so interrupt
290						 * the kernel driver if it
291						 * happens.
292						 */
293/*
294 * Initialize SCSIRATE with the appropriate value for this target.
295 * The SCSIRATE settings for each target are stored in an array
296 * based at TARG_SCRATCH.
297 */
298ndx_dtr:
299	shr	A,SAVED_TCL,4
300	test	SBLKCTL,SELBUSB	jz ndx_dtr_2
301	or	SAVED_TCL, SELBUSB /* Add the channel bit while we're here */
302	or	A,0x08		/* Channel B entries add 8 */
303ndx_dtr_2:
304	add	SINDEX,TARG_SCRATCH,A
305	mov	SCSIRATE,SINDIR
306
307/*
308 * Initialize Ultra mode setting and clear the SCSI channel.
309 */
310ultra:
311	and	DINDEX,0xdf,SXFRCTL0            /* default to Ultra disabled */
312	mvi	SINDEX, ULTRA_ENB_B
313	test	SAVED_TCL, 0x80     jnz ultra_2     /* Target ID > 7 */
314	test	SBLKCTL, SELBUSB jnz ultra_2     /* Second channel device */
315	dec	SINDEX
316ultra_2:
317	mov     FUNCTION1,SAVED_TCL
318	mov     A,FUNCTION1
319	test	SINDIR, A	jz set_sxfrctl0
320	or	DINDEX, ULTRAEN
321 
322set_sxfrctl0:
323	mov	SXFRCTL0,DINDEX
324
325/*
326 * Main loop for information transfer phases.  If BSY is false, then
327 * we have a bus free condition, expected or not.  Otherwise, wait
328 * for the target to assert REQ before checking MSG, C/D and I/O
329 * for the bus phase.
330 *
331 */
332ITloop:
333	test	SSTAT1,BUSFREE	jnz p_busfree
334	test	SSTAT1,REQINIT	jz ITloop
335
336	and	A,PHASE_MASK,SCSISIGI
337	mov	LASTPHASE,A
338	mov	SCSISIGO,A
339
340	cmp	ALLZEROS,A	je p_dataout
341	cmp	A,P_DATAIN	je p_datain
342	cmp	A,P_COMMAND	je p_command
343	cmp	A,P_MESGOUT	je p_mesgout
344	cmp	A,P_STATUS	je p_status
345	cmp	A,P_MESGIN	je p_mesgin
346
347	mvi	INTSTAT,BAD_PHASE	/* unknown phase - signal driver */
348	jmp	ITloop			/* Try reading the bus again. */
349
350p_dataout:
351	mvi	DMAPARAMS,0x7d			/*
352						 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
353						 * DIRECTION|FIFORESET
354						 */
355	jmp	data_phase_init
356
357/*
358 * If we re-enter the data phase after going through another phase, the
359 * STCNT may have been cleared, so restore it from the residual field.
360 */
361data_phase_reinit:
362	mvi	DINDEX, STCNT0
363	mvi	SCB_RESID_DCNT0	call bcopy_3
364	jmp	data_phase_loop
365
366p_datain:
367	mvi	DMAPARAMS,0x79		/*
368					 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
369					 * !DIRECTION|FIFORESET
370					 */
371data_phase_init:
372	call	assert			/*
373					 * Ensure entering a data
374					 * phase is okay - seen identify, etc.
375					 */
376
377	test	FLAGS, DPHASE	jnz data_phase_reinit
378
379	/*
380	 * Initialize the DMA address and counter from the SCB.
381	 * Also set SG_COUNT and SG_NEXT in memory since we cannot
382	 * modify the values in the SCB itself until we see a
383	 * save data pointers message.
384	 */
385	mvi	DINDEX, HADDR0
386	mvi	SCB_DATAPTR	call bcopy_7
387
388	call	set_stcnt_from_hcnt
389
390	mov	SG_COUNT,SCB_SGCOUNT
391
392	mvi	DINDEX, SG_NEXT
393	mvi	SCB_SGPTR	call bcopy_4
394
395	/* We have seen a data phase */
396	or	FLAGS, DPHASE
397
398data_phase_loop:
399/* Guard against overruns */
400	test	SG_COUNT, 0xff jnz data_phase_inbounds
401/*
402 * Turn on 'Bit Bucket' mode, set the transfer count to
403 * 16meg and let the target run until it changes phase.
404 * When the transfer completes, notify the host that we
405 * had an overrun.
406 */
407	or	SXFRCTL1,BITBUCKET
408	and	DMAPARAMS, 0xf7		/* Turn off HDMAEN */
409	mvi	STCNT0,0xff
410	mvi	STCNT1,0xff
411	mvi	STCNT2,0xff
412
413data_phase_inbounds:
414/* If we are the last SG block, ensure wideodd is off. */
415	cmp	SG_COUNT,0x01 jne data_phase_wideodd
416	and	DMAPARAMS, 0xbf		/* Turn off WIDEODD */
417data_phase_wideodd:
418	mov	DMAPARAMS  call dma
419
420/* Go tell the host about any overruns */
421	test	SXFRCTL1,BITBUCKET jnz data_phase_overrun
422
423/* Exit if we had an underrun */
424	test	SSTAT0,SDONE	jz data_phase_finish /* underrun STCNT != 0 */
425
426/*
427 * Advance the scatter-gather pointers if needed 
428 */
429sg_advance:
430	dec	SG_COUNT	/* one less segment to go */
431
432	test	SG_COUNT, 0xff	jz data_phase_finish /* Are we done? */
433
434	clr	A			/* add sizeof(struct scatter) */
435	add	SG_NEXT0,SG_SIZEOF,SG_NEXT0
436	adc	SG_NEXT1,A,SG_NEXT1
437
438/*
439 * Load a struct scatter and set up the data address and length.
440 * If the working value of the SG count is nonzero, then
441 * we need to load a new set of values.
442 *
443 * This, like all DMA's, assumes little-endian host data storage.
444 */
445sg_load:
446	clr	HCNT2
447	clr	HCNT1
448	mvi	HCNT0,SG_SIZEOF
449
450	mvi	DINDEX, HADDR0
451	mvi	SG_NEXT0	call bcopy_4
452
453	or	DFCNTRL,0xd			/* HDMAEN|DIRECTION|FIFORESET */
454
455	call	dma_finish
456
457/*
458 * Copy data from FIFO into SCB data pointer and data count.  This assumes
459 * that the SG segments are of the form:
460 *
461 * struct ahc_dma_seg {
462 *	u_int32_t	addr;		four bytes, little-endian order
463 *	u_int32_t	len;		four bytes, little endian order
464 * };
465 */
466	mvi	HADDR0	call dfdat_in_7
467
468/* Load STCNT as well.  It is a mirror of HCNT */
469	call	set_stcnt_from_hcnt
470	test	SSTAT1,PHASEMIS  jz data_phase_loop
471
472data_phase_finish:
473/*
474 * After a DMA finishes, save the SG and STCNT residuals back into the SCB
475 * We use STCNT instead of HCNT, since it's a reflection of how many bytes 
476 * were transferred on the SCSI (as opposed to the host) bus.
477 */
478	mov	SCB_RESID_DCNT0,STCNT0
479	mov	SCB_RESID_DCNT1,STCNT1
480	mov	SCB_RESID_DCNT2,STCNT2
481	mov	SCB_RESID_SGCNT, SG_COUNT
482	jmp	ITloop
483
484data_phase_overrun:
485/*
486 * Turn off BITBUCKET mode and notify the host
487 */
488	and	SXFRCTL1,0x7f		/* ~BITBUCKET */
489	mvi	INTSTAT,DATA_OVERRUN
490	jmp	ITloop
491
492/*
493 * Command phase.  Set up the DMA registers and let 'er rip.
494 */
495p_command:
496	call	assert
497
498/*
499 * Load HADDR and HCNT.
500 */
501	mvi	DINDEX, HADDR0
502	mvi	SCB_CMDPTR	call bcopy_5
503	clr	HCNT1
504	clr	HCNT2
505
506	call	set_stcnt_from_hcnt
507
508	mvi	DFCNTRL, 0x3d			/*  SCSIEN|SDMAEN|HDMAEN
509						 * |DIRECTION|FIFORESET
510						 */
511p_command_dma:
512	test	SSTAT0, SDONE		jnz p_command_dma_done
513	test	SSTAT1, PHASEMIS	jz  p_command_dma
514	test	SSTAT0, SDONE		jnz p_command_dma_done
515	clr	DFCNTRL
516	jmp	ITloop
517p_command_dma_done:
518	and	DFCNTRL, 0xc7		/* ~(SCSIEN|SDMAEN|HDMAEN) */
519p_command_dma_clear:
520	test	DFCNTRL, 0x38		jnz p_command_dma_clear
521	jmp	ITloop
522
523/*
524 * Status phase.  Wait for the data byte to appear, then read it
525 * and store it into the SCB.
526 */
527p_status:
528	call	assert
529
530	mov	SCB_TARGET_STATUS, SCSIDATL
531	jmp	ITloop
532
533/*
534 * Message out phase.  If there is not an active message, but the target
535 * took us into this phase anyway, build a no-op message and send it.
536 */
537p_mesgout:
538	test	MSG_LEN, 0xff	jnz  p_mesgout_start
539	mvi	MSG_NOOP	call mk_mesg	/* build NOP message */
540p_mesgout_start:
541/*
542 * Set up automatic PIO transfer from MSG0.  Bit 3 in
543 * SXFRCTL0 (SPIOEN) is already on.
544 */
545	mvi	SINDEX,MSG0
546	mov	DINDEX,MSG_LEN
547
548/*
549 * When target asks for a byte, drop ATN if it's the last one in
550 * the message.  Otherwise, keep going until the message is exhausted.
551 *
552 * Keep an eye out for a phase change, in case the target issues
553 * a MESSAGE REJECT.
554 */
555p_mesgout_loop:
556	/*
557	 * If there is a parity error, wait for the kernel to
558	 * see the interrupt and "update" our message response
559	 * before continuing.
560	 */
561	test	SSTAT1, REQINIT	jz p_mesgout_loop
562	test	SSTAT1, SCSIPERR jnz p_mesgout_loop
563	and	LASTPHASE, PHASE_MASK, SCSISIGI
564	cmp	LASTPHASE, P_MESGOUT jne p_mesgout_done
565/*
566 * If the next bus phase after ATN drops is a message out, it means
567 * that the target is requesting that the last message(s) be resent.
568 */
569p_mesgout_testretry:
570	test	DINDEX,0xff	jnz p_mesgout_dropatn
571	or	SCSISIGO,ATNO,LASTPHASE	/* turn on ATN for the retry */
572	jmp	p_mesgout_start
573p_mesgout_dropatn:
574	cmp	DINDEX,1	jne p_mesgout_outb	/* last byte? */
575	mvi	CLRSINT1,CLRATNO			/* drop ATN */
576p_mesgout_outb:
577	dec	DINDEX
578	mvi	CLRSINT0, CLRSPIORDY
579	mov	SCSIDATL,SINDIR
580	jmp	p_mesgout_loop
581
582p_mesgout_done:
583	mvi	CLRSINT1,CLRATNO	/* Be sure to turn ATNO off */
584	clr	MSG_LEN			/* no active msg */
585	jmp	ITloop
586
587/*
588 * Message in phase.  Bytes are read using Automatic PIO mode.
589 */
590p_mesgin:
591	mvi	A		call inb_first	/* read the 1st message byte */
592	mov	REJBYTE,A			/* save it for the driver */
593
594	test	A,MSG_IDENTIFYFLAG	jnz mesgin_identify
595	cmp	A,MSG_DISCONNECT	je mesgin_disconnect
596	cmp	A,MSG_SAVEDATAPOINTER	je mesgin_sdptrs
597	cmp	ALLZEROS,A		je mesgin_complete
598	cmp	A,MSG_RESTOREPOINTERS	je mesgin_rdptrs
599	cmp	A,MSG_EXTENDED		je mesgin_extended
600	cmp	A,MSG_MESSAGE_REJECT	je mesgin_reject
601
602rej_mesgin:
603/*
604 * We have no idea what this message in is, so we issue a message reject
605 * and hope for the best.  In any case, rejection should be a rare
606 * occurrence - signal the driver when it happens.
607 */
608	mvi	INTSTAT,SEND_REJECT		/* let driver know */
609
610	mvi	MSG_MESSAGE_REJECT	call mk_mesg
611
612mesgin_done:
613	call	inb_last			/*ack & turn auto PIO back on*/
614	jmp	ITloop
615
616
617mesgin_complete:
618/*
619 * We got a "command complete" message, so put the SCB_TAG into the QOUTFIFO,
620 * and trigger a completion interrupt.  Before doing so, check to see if there
621 * is a residual or the status byte is something other than NO_ERROR (0).  In
622 * either of these conditions, we upload the SCB back to the host so it can
623 * process this information.  In the case of a non zero status byte, we 
624 * additionally interrupt the kernel driver synchronously, allowing it to
625 * decide if sense should be retrieved.  If the kernel driver wishes to request
626 * sense, it will fill the kernel SCB with a request sense command and set
627 * RETURN_1 to SEND_SENSE.  If RETURN_1 is set to SEND_SENSE we redownload
628 * the SCB, and process it as the next command by adding it to the waiting list.
629 * If the kernel driver does not wish to request sense, it need only clear
630 * RETURN_1, and the command is allowed to complete normally.  We don't bother
631 * to post to the QOUTFIFO in the error cases since it would require extra
632 * work in the kernel driver to ensure that the entry was removed before the
633 * command complete code tried processing it.
634 */
635
636/*
637 * We expect to go to bus free after this message.
638 */
639	and	SIMODE1, 0xf7		/* ~ENBUSFREE */
640/*
641 * First check for residuals
642 */
643	test	SCB_RESID_SGCNT,0xff	jnz upload_scb
644	test	SCB_TARGET_STATUS,0xff	jz status_ok	/* Good Status? */
645upload_scb:
646	mvi	DMAPARAMS, 0x9	/* HDMAEN | FIFORESET*/
647	mov	SCB_TAG		call dma_scb
648check_status:
649	test	SCB_TARGET_STATUS,0xff	jz status_ok	/* Just a residual? */
650	mvi	INTSTAT,BAD_STATUS			/* let driver know */
651	cmp	RETURN_1, SEND_SENSE	jne status_ok
652	/* This SCB becomes the next to execute as it will retrieve sense */
653	mov	SCB_LINKED_NEXT, SCB_TAG
654	jmp	dma_next_scb
655
656status_ok:
657/* First, mark this target as free. */
658	test	SCB_CONTROL,TAG_ENB jnz complete	/*
659							 * Tagged commands
660							 * don't busy the
661							 * target.
662							 */
663	mov	SAVED_SCBPTR, SCBPTR
664	mov	SAVED_LINKPTR, SCB_LINKED_NEXT
665	mov	SCB_TCL		call	index_untagged_scb
666	mov	DINDIR, SAVED_LINKPTR
667	mov	SCBPTR, SAVED_SCBPTR
668
669complete:
670	/* Post the SCB and issue an interrupt */
671	mov	QOUTFIFO,SCB_TAG
672	mvi	INTSTAT,CMDCMPLT
673
674dma_next_scb:
675	cmp	SCB_LINKED_NEXT, SCB_LIST_NULL	je add_to_free_list
676	test	FLAGS, PAGESCBS jnz dma_next_scb2
677	/* Only DMA on top of ourselves if we are the SCB to download */
678	mov	A, SCB_LINKED_NEXT
679	cmp	SCB_TAG, A	je dma_next_scb2
680	mov	SCBPTR, A
681	jmp	add_to_waiting_list
682dma_next_scb2:
683	mvi	DMAPARAMS, 0xd	/* HDMAEN|DIRECTION|FIFORESET */
684	mov	SCB_LINKED_NEXT		call dma_scb
685add_to_waiting_list:
686	mov	SCB_NEXT,WAITING_SCBH
687	mov	WAITING_SCBH, SCBPTR
688	jmp	mesgin_done
689add_to_free_list:
690	call	add_scb_to_free_list
691	jmp	mesgin_done
692
693/*
694 * Is it an extended message?  Copy the message to our message buffer and
695 * notify the host.  The host will tell us whether to reject this message,
696 * respond to it with the message that the host placed in our message buffer,
697 * or simply to do nothing.
698 */
699mesgin_extended:
700	mvi	MSGIN_EXT_LEN	 call inb_next
701	mov	A, MSGIN_EXT_LEN
702mesgin_extended_loop:
703	mov	DINDEX	call	inb_next
704	dec	A
705	cmp	DINDEX, MSGIN_EXT_LASTBYTE jne mesgin_extended_loop_test
706	dec	DINDEX		/* dump by repeatedly filling the last byte */
707mesgin_extended_loop_test:
708	test	A, 0xFF		jnz mesgin_extended_loop
709mesgin_extended_intr:
710	mvi	INTSTAT,EXTENDED_MSG		/* let driver know */
711	cmp	RETURN_1,SEND_REJ je rej_mesgin
712	cmp	RETURN_1,SEND_MSG jne mesgin_done
713/* The kernel has setup a message to be sent */
714	or	SCSISIGO,ATNO,LASTPHASE		/* turn on ATNO */
715	jmp	mesgin_done
716
717/*
718 * Is it a disconnect message?  Set a flag in the SCB to remind us
719 * and await the bus going free.
720 */
721mesgin_disconnect:
722	and	SIMODE1, 0xf7		/* ~ENBUSFREE */
723	or	SCB_CONTROL,DISCONNECTED
724	test	FLAGS, PAGESCBS jz mesgin_done
725	call	add_scb_to_disc_list
726	jmp	mesgin_done
727
728/*
729 * Save data pointers message:
730 * Copying RAM values back to SCB, for Save Data Pointers message, but
731 * only if we've actually been into a data phase to change them.  This
732 * protects against bogus data in scratch ram and the residual counts
733 * since they are only initialized when we go into data_in or data_out.
734 */
735mesgin_sdptrs:
736	test	FLAGS, DPHASE	jz mesgin_done
737	mov	SCB_SGCOUNT,SG_COUNT
738
739	/* The SCB SGPTR becomes the next one we'll download */
740	mvi	DINDEX, SCB_SGPTR
741	mvi	SG_NEXT0	call bcopy_4
742	
743	/* The SCB DATAPTR0 becomes the current SHADDR */
744	mvi	DINDEX, SCB_DATAPTR0
745	mvi	SHADDR0		call bcopy_4
746
747/*
748 * Use the residual number since STCNT is corrupted by any message transfer.
749 */
750	mvi	SCB_RESID_DCNT0	call	bcopy_3
751
752	jmp	mesgin_done
753
754/*
755 * Restore pointers message?  Data pointers are recopied from the
756 * SCB anytime we enter a data phase for the first time, so all
757 * we need to do is clear the DPHASE flag and let the data phase
758 * code do the rest.
759 */
760mesgin_rdptrs:
761	and	FLAGS,0xef			/*
762						 * !DPHASE we'll reload them
763						 * the next time through
764						 */
765	jmp	mesgin_done
766
767/*
768 * Identify message?  For a reconnecting target, this tells us the lun
769 * that the reconnection is for - find the correct SCB and switch to it,
770 * clearing the "disconnected" bit so we don't "find" it by accident later.
771 */
772mesgin_identify:
773	test	A,0x78	jnz rej_mesgin	/*!DiscPriv|!LUNTAR|!Reserved*/
774	and	A,0x07			/* lun in lower three bits */
775	or      SAVED_TCL,A		/* SAVED_TCL should be complete now */
776	call	inb_last		/* ACK */
777
778/*
779 * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
780 * If we get one, we use the tag returned to switch to find the proper
781 * SCB.  With SCB paging, this requires using findSCB for both tagged
782 * and non-tagged transactions since the SCB may exist in any slot.
783 * If we're not using SCB paging, we can use the tag as the direct
784 * index to the SCB.
785 */
786	mvi	ARG_1,SCB_LIST_NULL	/* Default to no-tag */
787snoop_tag_loop:
788	test	SSTAT1,REQINIT		jz snoop_tag_loop
789	and	LASTPHASE, PHASE_MASK, SCSISIGI
790	cmp	LASTPHASE, P_MESGIN, jne use_findSCB
791	mvi	A			call inb_first
792	cmp	A,MSG_SIMPLE_Q_TAG	jne use_findSCB
793get_tag:
794	or	FLAGS, TAGGED_SCB
795	mvi	ARG_1	call inb_next	/* tag value */
796/*
797 * See if the tag is in range.  The tag is < SCBCOUNT if we add
798 * the complement of SCBCOUNT to the incomming tag and there is
799 * no carry.
800 */
801	mov	A,COMP_SCBCOUNT	
802	add	SINDEX,A,ARG_1
803	jc	send_abort_msg
804
805/*
806 * Ensure that the SCB the tag points to is for an SCB transaction
807 * to the reconnecting target.
808 */
809	test	FLAGS, PAGESCBS	jz index_by_tag
810use_findSCB:
811	mov	ALLZEROS	call findSCB	  /* Have to search */
812	cmp	SINDEX, SCB_LIST_NULL, je not_found
813setup_SCB:
814	and	SCB_CONTROL,0xfb	  /* clear disconnect bit in SCB */
815	or	FLAGS,IDENTIFY_SEEN	  /* make note of IDENTIFY */
816	test	SCB_CONTROL,TAG_ENB	jnz  mesgin_done /* Ack Tag */
817	jmp	ITloop
818index_by_tag:
819	mov	SCBPTR,ARG_1
820	mov	A, SAVED_TCL
821	cmp	SCB_TCL,A		jne send_abort_msg
822	test	SCB_CONTROL,TAG_ENB	jz  send_abort_msg
823	jmp	setup_SCB
824
825not_found:
826	mvi	INTSTAT, NO_MATCH
827send_abort_msg:
828	test	FLAGS, TAGGED_SCB jnz abort_tag_msg
829	mvi	MSG_ABORT	call mk_mesg
830	jmp	mesgin_done
831abort_tag_msg:
832	mvi	MSG_ABORT_TAG	call mk_mesg	/* ABORT TAG message */
833	jmp	mesgin_done
834
835/*
836 * Message reject?  Let the kernel driver handle this.  If we have an 
837 * outstanding WDTR or SDTR negotiation, assume that it's a response from 
838 * the target selecting 8bit or asynchronous transfer, otherwise just ignore 
839 * it since we have no clue what it pertains to.
840 */
841mesgin_reject:
842	mvi	INTSTAT, REJECT_MSG
843	jmp	mesgin_done
844
845/*
846 * [ ADD MORE MESSAGE HANDLING HERE ]
847 */
848
849/*
850 * Locking the driver out, build a one-byte message passed in SINDEX
851 * if there is no active message already.  SINDEX is returned intact.
852 */
853mk_mesg:
854	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
855	test	MSG_LEN,0xff	jz mk_mesg1	/* Should always succeed */
856	
857	/*
858	 * Hmmm.  For some reason the mesg buffer is in use.
859	 * Tell the driver.  It should look at SINDEX to find
860	 * out what we wanted to use the buffer for and resolve
861	 * the conflict.
862	 */
863	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
864	mvi	INTSTAT,MSG_BUFFER_BUSY
865
866mk_mesg1:
867	or	SCSISIGO,ATNO,LASTPHASE	/* turn on ATNO */
868	mvi	MSG_LEN,1		/* length = 1 */
869	mov	MSG0,SINDEX		/* 1-byte message */
870	mvi	SEQCTL,0x10	ret	/* !PAUSEDIS|FASTMODE */
871
872/*
873 * Functions to read data in Automatic PIO mode.
874 *
875 * According to Adaptec's documentation, an ACK is not sent on input from
876 * the target until SCSIDATL is read from.  So we wait until SCSIDATL is
877 * latched (the usual way), then read the data byte directly off the bus
878 * using SCSIBUSL.  When we have pulled the ATN line, or we just want to
879 * acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
880 * spec guarantees that the target will hold the data byte on the bus until
881 * we send our ACK.
882 *
883 * The assumption here is that these are called in a particular sequence,
884 * and that REQ is already set when inb_first is called.  inb_{first,next}
885 * use the same calling convention as inb.
886 */
887
888inb_next:
889	mov	NONE,SCSIDATL			/*dummy read from latch to ACK*/
890inb_next_wait:
891	/*
892	 * If there is a parity error, wait for the kernel to
893	 * see the interrupt and prepare our message response
894	 * before continuing.
895	 */
896	test	SSTAT1, REQINIT	jz inb_next_wait
897	test	SSTAT1, SCSIPERR jnz inb_next_wait
898	and	LASTPHASE, PHASE_MASK, SCSISIGI
899	cmp	LASTPHASE, P_MESGIN jne mesgin_phasemis
900inb_first:
901	mov	DINDEX,SINDEX
902	mov	DINDIR,SCSIBUSL	ret		/*read byte directly from bus*/
903inb_last:
904	mov	NONE,SCSIDATL ret		/*dummy read from latch to ACK*/
905
906mesgin_phasemis:
907/*
908 * We expected to receive another byte, but the target changed phase
909 */
910	mvi	INTSTAT, MSGIN_PHASEMIS
911	jmp	ITloop
912
913/*
914 * DMA data transfer.  HADDR and HCNT must be loaded first, and
915 * SINDEX should contain the value to load DFCNTRL with - 0x3d for
916 * host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
917 * during initialization.
918 */
919dma:
920	mov	DFCNTRL,SINDEX
921dma1:
922	test	SSTAT0,DMADONE	jnz dma3
923	test	SSTAT1,PHASEMIS	jz dma1		/* ie. underrun */
924
925/*
926 * We will be "done" DMAing when the transfer count goes to zero, or
927 * the target changes the phase (in light of this, it makes sense that
928 * the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
929 * doing a SCSI->Host transfer, the data FIFO should be flushed auto-
930 * magically on STCNT=0 or a phase change, so just wait for FIFO empty
931 * status.
932 */
933dma3:
934	test	SINDEX,DIRECTION	jnz dma5
935dma4:
936	test	DFSTATUS,FIFOEMP	jz dma4
937
938/*
939 * Now shut the DMA enables off and make sure that the DMA enables are 
940 * actually off first lest we get an ILLSADDR.
941 */
942dma5:
943	/* Don't clobber an inprogress host data transfer */
944	test	DFSTATUS, MREQPEND	jnz dma5
945	/* disable DMA */
946	and	DFCNTRL, 0xc7		/* ~(SCSIEN|SDMAEN|HDMAEN|DIRECTION) */
947dma6:
948	test	DFCNTRL, HDMAENACK	jnz dma6 
949return:
950	ret
951
952/*
953 * Common SCSI initialization for selection and reselection.  Expects
954 * the target SCSI ID to be in the upper four bits of SINDEX, and A's
955 * contents are stomped on return.
956 */
957initialize_scsiid:
958	and	SINDEX,0xf0		/* Get target ID */
959	mov	SAVED_TCL, SINDEX	/* Update the target portion of this */
960	and	A,0x0f,SCSIID
961	or	SINDEX,A
962	mov	SCSIID,SINDEX ret
963
964/*
965 * Assert that if we've been reselected, then we've seen an IDENTIFY
966 * message.
967 */
968assert:
969	test	FLAGS,RESELECTED	jz return	/* reselected? */
970	test	FLAGS,IDENTIFY_SEEN	jnz return	/* seen IDENTIFY? */
971
972	mvi	INTSTAT,NO_IDENT 	ret	/* no - tell the kernel */
973
974/*
975 * Locate a disconnected SCB either by SAVED_TCL (ARG_1 is SCB_LIST_NULL)
976 * or by the SCBIDn ARG_1.  The search begins at the SCB index passed in
977 * via SINDEX.  If the SCB cannot be found, SINDEX will be SCB_LIST_NULL,
978 * otherwise, SCBPTR is set to the proper SCB.
979 */
980findSCB:
981	mov	SCBPTR,SINDEX			/* switch to next SCB */
982	test	SCB_CONTROL,DISCONNECTED jz findSCB1 /*should be disconnected*/
983	cmp	ARG_1, SCB_LIST_NULL	jne findBySCBID
984	mov	A, SAVED_TCL
985	cmp	SCB_TCL,A	je foundSCB /* target ID/channel/lun match? */
986findSCB1:
987	inc	SINDEX
988	mov	A,SCBCOUNT
989	cmp	SINDEX,A	jne findSCB
990/*
991 * We didn't find it.  If we're paging, pull an SCB and DMA down the
992 * one we want.  If we aren't paging or the SCB we dma down has the
993 * abort flag set, return not found.
994 */
995	test	FLAGS, PAGESCBS	jz find_error
996	mov	ALLZEROS	call	get_free_or_disc_scb
997	cmp	ARG_1, SCB_LIST_NULL jne find_dma_scb
998	mov	SAVED_TCL	call	index_untagged_scb
999	mov	ARG_1, SINDIR	/* SCBID of SCB to fetch */
1000find_dma_scb:
1001	mvi	DMAPARAMS, 0xd	/* HDMAEN|DIRECTION|FIFORESET */
1002	mov	ARG_1	call dma_scb
1003	test	SCB_CONTROL, ABORT_SCB jz return
1004find_error:
1005	mvi	SINDEX, SCB_LIST_NULL ret
1006findBySCBID:
1007	mov	A, ARG_1			/* Tag passed in ARG_1 */
1008	cmp	SCB_TAG,A	jne findSCB1	/* Found it? */
1009foundSCB:
1010	test	SCB_CONTROL, ABORT_SCB jnz find_error
1011	test	FLAGS,PAGESCBS	jz return
1012rem_scb_from_disc_list:
1013/* Remove this SCB from the disconnection list */
1014	cmp	SCB_NEXT,SCB_LIST_NULL je unlink_prev
1015	mov	SAVED_LINKPTR, SCB_PREV
1016	mov	SCBPTR, SCB_NEXT
1017	mov	SCB_PREV, SAVED_LINKPTR
1018	mov	SCBPTR, SINDEX
1019unlink_prev:
1020	cmp	SCB_PREV,SCB_LIST_NULL	je rHead/* At the head of the list */
1021	mov	SAVED_LINKPTR, SCB_NEXT
1022	mov	SCBPTR, SCB_PREV
1023	mov	SCB_NEXT, SAVED_LINKPTR
1024	mov	SCBPTR, SINDEX ret
1025rHead:
1026	mov	DISCONNECTED_SCBH,SCB_NEXT ret
1027
1028set_stcnt_from_hcnt:
1029	mov	STCNT0, HCNT0
1030	mov	STCNT1, HCNT1
1031	mov	STCNT2, HCNT2 ret
1032
1033bcopy_7:
1034	mov	DINDIR, SINDIR
1035	mov	DINDIR, SINDIR
1036bcopy_5:
1037	mov	DINDIR, SINDIR
1038bcopy_4:
1039	mov	DINDIR, SINDIR
1040bcopy_3:
1041	mov	DINDIR, SINDIR
1042	mov	DINDIR, SINDIR
1043	mov	DINDIR, SINDIR ret
1044
1045dma_scb:
1046	/*
1047	 * SCB index is in SINDEX.  Determine the physical address in
1048	 * the host where this SCB is located and load HADDR with it.
1049	 */
1050	shr	DINDEX, SINDEX, 3
1051	shl	A, SINDEX, 5
1052	add	HADDR0, A, HSCB_ADDR0
1053	mov	A, DINDEX
1054	adc	HADDR1, A, HSCB_ADDR1
1055	clr	A
1056	adc	HADDR2, A, HSCB_ADDR2
1057	adc	HADDR3, A, HSCB_ADDR3
1058	/* Setup Count */
1059	mvi	HCNT0, 28
1060	clr	HCNT1
1061	clr	HCNT2
1062	mov	DFCNTRL, DMAPARAMS
1063	test	DMAPARAMS, DIRECTION	jnz dma_scb_fromhost
1064	/* Fill it with the SCB data */
1065	call	copy_scb_tofifo
1066	mvi	DFCNTRL, 0xa		/* HDMAEN | FIFOFLUSH */
1067dma_scb_fromhost:
1068	call	dma_finish
1069	/* If we were putting the SCB, we are done */
1070	test	DMAPARAMS, DIRECTION	jz	return
1071	mvi	SCBARRAY  call dfdat_in_7
1072	call	dfdat_in_7_continued
1073	call	dfdat_in_7_continued
1074	jmp	dfdat_in_7_continued
1075dfdat_in_7:
1076	mov     DINDEX,SINDEX
1077dfdat_in_7_continued:
1078	mov	DINDIR,DFDAT
1079	mov	DINDIR,DFDAT
1080	mov	DINDIR,DFDAT
1081	mov	DINDIR,DFDAT
1082	mov	DINDIR,DFDAT
1083	mov	DINDIR,DFDAT
1084	mov	DINDIR,DFDAT ret
1085
1086copy_scb_tofifo:
1087	mvi	SCBARRAY  call dfdat_out_7
1088	call	dfdat_out_7
1089	call	dfdat_out_7
1090dfdat_out_7:
1091	mov	DFDAT,SINDIR
1092	mov	DFDAT,SINDIR
1093	mov	DFDAT,SINDIR
1094	mov	DFDAT,SINDIR
1095	mov	DFDAT,SINDIR
1096	mov	DFDAT,SINDIR
1097	mov	DFDAT,SINDIR ret
1098
1099/*
1100 * Wait for DMA from host memory to data FIFO to complete, then disable
1101 * DMA and wait for it to acknowledge that it's off.
1102 */
1103dma_finish:
1104	test	DFSTATUS,HDONE	jz dma_finish
1105	/* Turn off DMA preserving WIDEODD */
1106	and	DFCNTRL,WIDEODD
1107dma_finish2:
1108	test	DFCNTRL,HDMAENACK jnz dma_finish2
1109	ret
1110
1111index_untagged_scb:
1112	mov	DINDEX, SINDEX
1113	shr	DINDEX, 4
1114	and	DINDEX, 0x03			/* Bottom two bits of tid */
1115	add	DINDEX, SCB_ACTIVE0
1116	shr	A, SINDEX, 6			/* Target ID divided by 4 */
1117	test	SINDEX, SELBUSB jz index_untagged_scb2
1118	add	A, 2				/* Add 2 positions */
1119index_untagged_scb2:
1120	mov	SCBPTR, A			/*
1121						 * Select the SCB with this 
1122						 * target's information.
1123						 */
1124	mov	SINDEX, DINDEX	ret
1125
1126
1127get_free_or_disc_scb:
1128	cmp	FREE_SCBH, SCB_LIST_NULL jne dequeue_free_scb
1129	cmp	DISCONNECTED_SCBH, SCB_LIST_NULL jne dequeue_disc_scb
1130return_error:
1131	mvi	SINDEX, SCB_LIST_NULL	ret
1132dequeue_disc_scb:
1133	mov	SCBPTR, DISCONNECTED_SCBH
1134/*
1135 * If we have a residual, then we are in the middle of some I/O
1136 * and we have to send this SCB back up to the kernel so that the
1137 * saved data pointers and residual information isn't lost.
1138 */
1139	test	SCB_RESID_SGCNT,0xff	jz unlink_disc_scb
1140	mvi	DMAPARAMS, 0x9	/* HDMAEN | FIFORESET*/
1141	mov	SCB_TAG		call dma_scb
1142unlink_disc_scb:
1143	/* jmp instead of call since we want to return anyway */
1144	mov	SCBPTR	jmp rem_scb_from_disc_list
1145dequeue_free_scb:
1146	mov	SCBPTR, FREE_SCBH
1147	mov	FREE_SCBH, SCB_NEXT ret
1148
1149add_scb_to_free_list:
1150	mov	SCB_NEXT, FREE_SCBH
1151	mov	FREE_SCBH, SCBPTR ret
1152
1153add_scb_to_disc_list:
1154/*
1155 * Link this SCB into the DISCONNECTED list.  This list holds the
1156 * candidates for paging out an SCB if one is needed for a new command.
1157 * Modifying the disconnected list is a critical(pause dissabled) section.
1158 */
1159	mvi	SCB_PREV, SCB_LIST_NULL
1160	mov	SCB_NEXT, DISCONNECTED_SCBH
1161	mov	DISCONNECTED_SCBH, SCBPTR
1162	cmp	SCB_NEXT,SCB_LIST_NULL je return
1163	mov	SCBPTR,SCB_NEXT
1164	mov	SCB_PREV,DISCONNECTED_SCBH
1165	mov	SCBPTR,DISCONNECTED_SCBH ret
1166
1167