aic7xxx.seq revision 15881
113177Sgibbs/*+M***********************************************************************
213177Sgibbs *Adaptec 274x/284x/294x device driver for Linux and FreeBSD.
313177Sgibbs *
413177Sgibbs *Copyright (c) 1994 John Aycock
513177Sgibbs *  The University of Calgary Department of Computer Science.
613177Sgibbs *  All rights reserved.
713177Sgibbs *
815328Sgibbs *FreeBSD, Twin, Wide, 2 command per target support, tagged queuing,
915328Sgibbs *SCB paging and other optimizations:
1015328Sgibbs *Copyright (c) 1994, 1995, 1996 Justin Gibbs. All rights reserved.
1113177Sgibbs *
1213177Sgibbs *Redistribution and use in source and binary forms, with or without
1313177Sgibbs *modification, are permitted provided that the following conditions
1413177Sgibbs *are met:
1513177Sgibbs *1. Redistributions of source code must retain the above copyright
1613177Sgibbs *   notice, this list of conditions, and the following disclaimer.
1713177Sgibbs *2. Redistributions in binary form must reproduce the above copyright
1813177Sgibbs *   notice, this list of conditions and the following disclaimer in the
1913177Sgibbs *   documentation and/or other materials provided with the distribution.
2013177Sgibbs *3. All advertising materials mentioning features or use of this software
2113177Sgibbs *   must display the following acknowledgement:
2213177Sgibbs *     This product includes software developed by the University of Calgary
2313177Sgibbs *     Department of Computer Science and its contributors.
2413177Sgibbs *4. Neither the name of the University nor the names of its contributors
2513177Sgibbs *   may be used to endorse or promote products derived from this software
2613177Sgibbs *   without specific prior written permission.
2713177Sgibbs *
2813177Sgibbs *THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2913177Sgibbs *ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3013177Sgibbs *IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3113177Sgibbs *ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3213177Sgibbs *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3313177Sgibbs *DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3413177Sgibbs *OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3513177Sgibbs *HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3613177Sgibbs *LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3713177Sgibbs *OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3813177Sgibbs *SUCH DAMAGE.
3913177Sgibbs *
4013177Sgibbs *-M************************************************************************/
414568Sgibbs
4215881SgibbsVERSION AIC7XXX_SEQ_VER "$Id: aic7xxx.seq,v 1.35 1996/05/21 18:32:22 gibbs Exp $"
435647Sgibbs
4413177Sgibbs#include "../../dev/aic7xxx/aic7xxx_reg.h"
454568Sgibbs
4613177Sgibbs/*
4713177Sgibbs * We can't just use ACCUM in the sequencer code because it
4813177Sgibbs * must be treated specially by the assembler, and it currently
4913690Sgibbs * looks for the symbol 'A'.  This is the only register defined in
5013177Sgibbs * the assembler's symbol space.
5113177Sgibbs */
5213177SgibbsA = ACCUM
534568Sgibbs
5413690Sgibbs/* After starting the selection hardware, we check for reconnecting targets
5513690Sgibbs * as well as for our selection to complete just in case the reselection wins
5613690Sgibbs * bus arbitration.  The problem with this is that we must keep track of the
5713690Sgibbs * SCB that we've already pulled from the QINFIFO and started the selection
5813690Sgibbs * on just in case the reselection wins so that we can retry the selection at
5913690Sgibbs * a later time.  This problem cannot be resolved by holding a single entry
6013690Sgibbs * in scratch ram since a reconnecting target can request sense and this will
6113690Sgibbs * create yet another SCB waiting for selection.  The solution used here is to 
6213690Sgibbs * use byte 27 of the SCB as a psuedo-next pointer and to thread a list
6313177Sgibbs * of SCBs that are awaiting selection.  Since 0-0xfe are valid SCB offsets, 
6413177Sgibbs * SCB_LIST_NULL is 0xff which is out of range.  The kernel driver must
6513177Sgibbs * add an entry to this list everytime a request sense occurs.  The sequencer
6613177Sgibbs * will automatically consume the entries.
6713177Sgibbs */
684568Sgibbs
6913177Sgibbs/*
7014449Sgibbs * We assume that the kernel driver may reset us at any time, even in the
7114449Sgibbs * middle of a DMA, so clear DFCNTRL too.
7213177Sgibbs */
7314449Sgibbsreset:
7414449Sgibbs	clr	DFCNTRL
7514449Sgibbs	clr	SCSISIGO		/* De-assert BSY */
7614449Sgibbs/*
7714449Sgibbs * We jump to start after every bus free.
7814449Sgibbs */
7913177Sgibbsstart:
8015328Sgibbs	and	FLAGS,0x0f		/* clear target specific flags */
8113177Sgibbs	mvi	SCSISEQ,ENRSELI		/* Always allow reselection */
828104Sgibbspoll_for_work:
8313177Sgibbs	/*
8413177Sgibbs	 * Are we a twin channel device?
8513177Sgibbs	 * For fairness, we check the other bus first,
8613177Sgibbs	 * since we just finished a transaction on the
8713177Sgibbs	 * current channel.
8813177Sgibbs	 */
8913177Sgibbs	test	FLAGS,TWIN_BUS	jz start2
9013177Sgibbs	xor	SBLKCTL,SELBUSB			/* Toggle to the other bus */
918104Sgibbs	test	SSTAT0,SELDI	jnz reselect
9213177Sgibbs	xor	SBLKCTL,SELBUSB			/* Toggle to the original bus */
935326Sgibbsstart2:
948104Sgibbs	test	SSTAT0,SELDI	jnz reselect
959395Sgibbs	cmp	WAITING_SCBH,SCB_LIST_NULL jne start_waiting
9614934Sgibbs	mov	A, QCNTMASK
9714934Sgibbs	test	QINCNT,A	jz poll_for_work
984568Sgibbs
9913690Sgibbs/*
10013690Sgibbs * We have at least one queued SCB now and we don't have any 
10113177Sgibbs * SCBs in the list of SCBs awaiting selection.  Set the SCB
10213177Sgibbs * pointer from the FIFO so we see the right bank of SCB 
10313177Sgibbs * registers.
10413177Sgibbs */
1054568Sgibbs	mov	SCBPTR,QINFIFO
1064568Sgibbs
10713177Sgibbs/*
10813177Sgibbs * See if there is not already an active SCB for this target.  This code
10913177Sgibbs * locks out on a per target basis instead of target/lun.  Although this
11013177Sgibbs * is not ideal for devices that have multiple luns active at the same
11113177Sgibbs * time, it is faster than looping through all SCB's looking for active
11213177Sgibbs * commands.  It may be benificial to make findscb a more general procedure
11313177Sgibbs * to see if the added cost of the search is negligible.  This code also 
11413177Sgibbs * assumes that the kernel driver will clear the active flags on board 
11513177Sgibbs * initialization, board reset, and a target SELTO.  Tagged commands
11613690Sgibbs * don't set the active bits since you can queue more than one command
11713690Sgibbs * at a time.  We do, however, look to see if there are any non-tagged
11813690Sgibbs * I/Os in progress, and requeue the command if there are.  Tagged and
11913690Sgibbs * non-tagged commands cannot be mixed to a single target.
12013177Sgibbs */
1214568Sgibbs
1225647Sgibbstest_busy:
12313177Sgibbs	mov	FUNCTION1,SCB_TCL
1244568Sgibbs	mov	A,FUNCTION1
12513177Sgibbs	test	SCB_TCL,0x88	jz test_a	/* Id < 8 && A channel */
1265326Sgibbs
1275326Sgibbs	test	ACTIVE_B,A	jnz requeue
12813177Sgibbs	test	SCB_CONTROL,TAG_ENB	jnz start_scb
12913177Sgibbs	/* Mark the current target as busy */
13013177Sgibbs	or	ACTIVE_B,A
1315326Sgibbs	jmp	start_scb
1325326Sgibbs
13313177Sgibbs/* Place the currently active SCB back on the queue for later processing */
1345326Sgibbsrequeue:
1354568Sgibbs	mov	QINFIFO, SCBPTR
1368104Sgibbs	jmp	poll_for_work
1374568Sgibbs
13813177Sgibbs/*
13913177Sgibbs * Pull the first entry off of the waiting for selection list
14013177Sgibbs * We don't have to "test_busy" because only transactions that
14113177Sgibbs * have passed that test can be in the waiting_scb list.
14213177Sgibbs */
1438104Sgibbsstart_waiting:
1448104Sgibbs	mov	SCBPTR,WAITING_SCBH
14513177Sgibbs	jmp	start_scb2
1468104Sgibbs
1475326Sgibbstest_a:
14813177Sgibbs	test	ACTIVE_A,A jnz requeue
14913177Sgibbs	test	SCB_CONTROL,TAG_ENB jnz start_scb
15013177Sgibbs	/* Mark the current target as busy */
15113177Sgibbs	or	ACTIVE_A,A
1525326Sgibbs
1535326Sgibbsstart_scb:
15415328Sgibbs	mov	SCB_NEXT,WAITING_SCBH
15513177Sgibbs	mov	WAITING_SCBH, SCBPTR
15613177Sgibbsstart_scb2:
15713177Sgibbs	and	SINDEX,0xf7,SBLKCTL	/* Clear the channel select bit */
15813177Sgibbs	and	A,0x08,SCB_TCL		/* Get new channel bit */
15913177Sgibbs	or	SINDEX,A
16013177Sgibbs	mov	SBLKCTL,SINDEX		/* select channel */
16113177Sgibbs	mov	SCB_TCL	call initialize_scsiid
1628104Sgibbs
16313177Sgibbs/*
16413177Sgibbs * Enable selection phase as an initiator, and do automatic ATN
16513177Sgibbs * after the selection.  We do this now so that we can overlap the
16613177Sgibbs * rest of our work to set up this target with the arbitration and
16713177Sgibbs * selection bus phases.
16813177Sgibbs */
1698104Sgibbsstart_selection:
17013177Sgibbs	mvi	SCSISEQ,0x58		/* ENSELO|ENAUTOATNO|ENRSELI */
1714568Sgibbs
17213177Sgibbs/*
17313177Sgibbs * As soon as we get a successful selection, the target should go
17413177Sgibbs * into the message out phase since we have ATN asserted.  Prepare
17513177Sgibbs * the message to send.
17613177Sgibbs *
17713177Sgibbs * Messages are stored in scratch RAM starting with a length byte
17813177Sgibbs * followed by the message itself.
17913177Sgibbs */
18013177Sgibbs	test	SCB_CMDLEN,0xff jnz mk_identify	/* 0 Length Command? */
1818567Sdg
18213177Sgibbs/*
18313177Sgibbs * The kernel has sent us an SCB with no command attached.  This implies
18413177Sgibbs * that the kernel wants to send a message of some sort to this target,
18513177Sgibbs * so we interrupt the driver, allow it to fill the message buffer, and
18613177Sgibbs * then go back into the arbitration loop
18713177Sgibbs */
1888567Sdg	mvi     INTSTAT,AWAITING_MSG
1899917Sgibbs	jmp     wait_for_selection
1908567Sdg
19113177Sgibbsmk_identify:
19213177Sgibbs	and	A,DISCENB,SCB_CONTROL	/* mask off disconnect privledge */
1934568Sgibbs
19413690Sgibbs	and	MSG0,0x7,SCB_TCL	/* lun */
19513690Sgibbs	or	MSG0,A			/* or in disconnect privledge */
19613690Sgibbs	or	MSG0,MSG_IDENTIFY
19713690Sgibbs	mvi	MSG_LEN, 1
1984568Sgibbs
19913177Sgibbs	test	SCB_CONTROL,0xb0 jz  !message	/* WDTR, SDTR or TAG?? */
20013177Sgibbs/*
20115328Sgibbs * Send a tag message if TAG_ENB is set in the SCB control block.
20215328Sgibbs * Use SCB_TAG (the position in the kernel's SCB array) as the tag value.
20313177Sgibbs */
2044568Sgibbs
2056608Sgibbsmk_tag:
20613177Sgibbs	mvi	DINDEX, MSG1
20713177Sgibbs	test	SCB_CONTROL,TAG_ENB jz mk_tag_done
20815328Sgibbs	and	DINDIR,0x23,SCB_CONTROL
20915328Sgibbs	mov	DINDIR,SCB_TAG
2106608Sgibbs
21113177Sgibbs	add	MSG_LEN,COMP_MSG0,DINDEX	/* update message length */
2126608Sgibbs
2136608Sgibbsmk_tag_done:
2146608Sgibbs
21513690Sgibbs	test	SCB_CONTROL,0x90 jz !message	/* NEEDWDTR|NEEDSDTR */
21613177Sgibbs	mov	DINDEX	call mk_dtr	/* build DTR message if needed */
2176608Sgibbs
2184568Sgibbs!message:
2199917Sgibbswait_for_selection:
22013177Sgibbs	test	SSTAT0,SELDO	jnz select 
22113177Sgibbs	test	SSTAT0,SELDI	jz wait_for_selection
2224568Sgibbs
22313177Sgibbs/*
22413177Sgibbs * Reselection has been initiated by a target. Make a note that we've been
22513177Sgibbs * reselected, but haven't seen an IDENTIFY message from the target
22613177Sgibbs * yet.
22713177Sgibbs */
2284568Sgibbsreselect:
22913177Sgibbs	clr	MSG_LEN		/* Don't have anything in the mesg buffer */
2308104Sgibbs	mov	SELID		call initialize_scsiid
23113177Sgibbs	or	FLAGS,RESELECTED
23213177Sgibbs	jmp	select2
2334568Sgibbs
23413177Sgibbs/*
23513177Sgibbs * After the selection, remove this SCB from the "waiting for selection"
23613177Sgibbs * list.  This is achieved by simply moving our "next" pointer into
23713177Sgibbs * WAITING_SCBH.  Our next pointer will be set to null the next time this
23813177Sgibbs * SCB is used, so don't bother with it now.
23913177Sgibbs */
2408104Sgibbsselect:
24115328Sgibbs	mov	WAITING_SCBH,SCB_NEXT
24215328Sgibbs	or	FLAGS,SELECTED
2438104Sgibbsselect2:
24413177Sgibbs/*
24513177Sgibbs * Set CLRCHN here before the target has entered a data transfer mode -
24613177Sgibbs * with synchronous SCSI, if you do it later, you blow away some
24713177Sgibbs * data in the SCSI FIFO that the target has already sent to you.
24813177Sgibbs */
24913177Sgibbs	or	SXFRCTL0,CLRCHN
25013177Sgibbs/*
25113177Sgibbs * Initialize SCSIRATE with the appropriate value for this target.
25213177Sgibbs */
25313177Sgibbs	call	ndx_dtr
25413177Sgibbs	mov	SCSIRATE,SINDIR
25513177Sgibbs
25615843Sgibbs/*
25715881Sgibbs * Initialize Ultra mode setting.
25815843Sgibbs */
25915881Sgibbs	mov	FUNCTION1,SCSIID
26015881Sgibbs	mov	A,FUNCTION1
26115843Sgibbs	and	SINDEX,0xdf,SXFRCTL0		/* default to Ultra disabled */
26215843Sgibbs	test	SCSIID, 0x80	 jnz ultra_b	/* Target ID > 7 */
26315843Sgibbs	test	SBLKCTL, SELBUSB jnz ultra_b	/* Second channel device */
26415843Sgibbs	test	ULTRA_ENB,A	 jz  set_sxfrctl0
26515843Sgibbs	or	SINDEX, ULTRAEN  jmp set_sxfrctl0
26615843Sgibbsultra_b:
26715843Sgibbs	test	ULTRA_ENB_B,A	 jz  set_sxfrctl0
26815843Sgibbs	or	SINDEX, ULTRAEN
26915843Sgibbs
27015843Sgibbsset_sxfrctl0:
27115843Sgibbs	mov	SXFRCTL0,SINDEX
27215843Sgibbs
27313177Sgibbs	mvi	SCSISEQ,ENAUTOATNP		/*
27413177Sgibbs						 * ATN on parity errors
27513177Sgibbs						 * for "in" phases
27613177Sgibbs						 */
27713177Sgibbs	mvi	CLRSINT1,CLRBUSFREE
27813177Sgibbs	mvi	CLRSINT0,0x60			/* CLRSELDI|CLRSELDO */
27913177Sgibbs/*
28013177Sgibbs * Main loop for information transfer phases.  If BSY is false, then
28113177Sgibbs * we have a bus free condition, expected or not.  Otherwise, wait
28213177Sgibbs * for the target to assert REQ before checking MSG, C/D and I/O
28313177Sgibbs * for the bus phase.
28413177Sgibbs *
28513177Sgibbs */
2864568SgibbsITloop:
28713177Sgibbs	test	SSTAT1,BUSFREE	jnz p_busfree
28813177Sgibbs	test	SSTAT1,REQINIT	jz ITloop
2894568Sgibbs
29013177Sgibbs	and	A,PHASE_MASK,SCSISIGI
29113690Sgibbs	mov	LASTPHASE,A
29213690Sgibbs	mov	SCSISIGO,A
2934568Sgibbs
2944568Sgibbs	cmp	ALLZEROS,A	je p_dataout
29513177Sgibbs	cmp	A,P_DATAIN	je p_datain
29613177Sgibbs	cmp	A,P_COMMAND	je p_command
29713177Sgibbs	cmp	A,P_MESGOUT	je p_mesgout
29813177Sgibbs	cmp	A,P_STATUS	je p_status
29913177Sgibbs	cmp	A,P_MESGIN	je p_mesgin
3004568Sgibbs
30113177Sgibbs	mvi	INTSTAT,BAD_PHASE	/* unknown phase - signal driver */
30215843Sgibbs	jmp	ITloop			/* Try reading the bus again. */
3034568Sgibbs
3044568Sgibbsp_dataout:
30513177Sgibbs	mvi	DMAPARAMS,0x7d			/*
30613177Sgibbs						 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
30713177Sgibbs						 * DIRECTION|FIFORESET
30813177Sgibbs						 */
3099928Sgibbs	jmp	data_phase_init
3104568Sgibbs
31113177Sgibbs/*
31213177Sgibbs * If we re-enter the data phase after going through another phase, the
31313177Sgibbs * STCNT may have been cleared, so restore it from the residual field.
31413177Sgibbs */
3159928Sgibbsdata_phase_reinit:
31613690Sgibbs	mov	STCNT0,SCB_RESID_DCNT0
31713690Sgibbs	mov	STCNT1,SCB_RESID_DCNT1
31813690Sgibbs	mov	STCNT2,SCB_RESID_DCNT2
3199928Sgibbs	jmp	data_phase_loop
3204568Sgibbs
3219928Sgibbsp_datain:
32213177Sgibbs	mvi	DMAPARAMS,0x79		/*
32313177Sgibbs					 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
32413177Sgibbs					 * !DIRECTION|FIFORESET
32513177Sgibbs					 */
3269928Sgibbsdata_phase_init:
3279928Sgibbs	call	assert
3285775Sgibbs
3299928Sgibbs	test	FLAGS, DPHASE	jnz data_phase_reinit
3309928Sgibbs	call	sg_scb2ram
33113177Sgibbs	or	FLAGS, DPHASE		/* We have seen a data phase */
3324568Sgibbs
3339928Sgibbsdata_phase_loop:
33413177Sgibbs/* If we are the last SG block, don't set wideodd. */
3359928Sgibbs	cmp	SG_COUNT,0x01 jne data_phase_wideodd
33613177Sgibbs	and	DMAPARAMS, 0xbf		/* Turn off WIDEODD */
3379928Sgibbsdata_phase_wideodd:
3389928Sgibbs	mov	DMAPARAMS  call dma
3394568Sgibbs
34013177Sgibbs/* Exit if we had an underrun */
34113177Sgibbs	test	SSTAT0,SDONE	jz data_phase_finish /* underrun STCNT != 0 */
3427532Sgibbs
34313177Sgibbs/*
34413177Sgibbs * Advance the scatter-gather pointers if needed 
34513177Sgibbs */
3469928Sgibbssg_advance:
34713177Sgibbs	dec	SG_COUNT	/* one less segment to go */
3484568Sgibbs
34913177Sgibbs	test	SG_COUNT, 0xff	jz data_phase_finish /* Are we done? */
3504568Sgibbs
35113177Sgibbs	clr	A			/* add sizeof(struct scatter) */
35213177Sgibbs	add	SG_NEXT0,SG_SIZEOF,SG_NEXT0
35313177Sgibbs	adc	SG_NEXT1,A,SG_NEXT1
3544568Sgibbs
35513177Sgibbs/*
35613177Sgibbs * Load a struct scatter and set up the data address and length.
35713177Sgibbs * If the working value of the SG count is nonzero, then
35813177Sgibbs * we need to load a new set of values.
35913177Sgibbs *
36015328Sgibbs * This, like all DMA's, assumes little-endian host data storage.
36113177Sgibbs */
3629928Sgibbssg_load:
36313177Sgibbs	clr	HCNT2
36413177Sgibbs	clr	HCNT1
36513177Sgibbs	mvi	HCNT0,SG_SIZEOF
3664568Sgibbs
36713690Sgibbs	mov	HADDR0,SG_NEXT0
36813690Sgibbs	mov	HADDR1,SG_NEXT1
36913690Sgibbs	mov	HADDR2,SG_NEXT2
37013690Sgibbs	mov	HADDR3,SG_NEXT3
3714568Sgibbs
37213690Sgibbs	or	DFCNTRL,0xd			/* HDMAEN|DIRECTION|FIFORESET */
3739928Sgibbs
37413177Sgibbs/*
37513177Sgibbs * Wait for DMA from host memory to data FIFO to complete, then disable
37613177Sgibbs * DMA and wait for it to acknowledge that it's off.
37713177Sgibbs */
37813690Sgibbsdma_finish:
37913690Sgibbs	test	DFSTATUS,HDONE	jz dma_finish
38013690Sgibbs	/* Turn off DMA preserving WIDEODD */
38113690Sgibbs	and	DFCNTRL,WIDEODD
38213690Sgibbsdma_finish2:
38313690Sgibbs	test	DFCNTRL,HDMAENACK jnz dma_finish2
3849928Sgibbs
38513177Sgibbs/*
38613177Sgibbs * Copy data from FIFO into SCB data pointer and data count.  This assumes
38713177Sgibbs * that the struct scatterlist has this structure (this and sizeof(struct
38814449Sgibbs * scatterlist) == 12 are asserted in aic7xxx.c for the Linux driver):
38913177Sgibbs *
39013177Sgibbs *	struct scatterlist {
39113177Sgibbs *		char *address;		four bytes, little-endian order
39213177Sgibbs *		...			four bytes, ignored
39313177Sgibbs *		unsigned short length;	two bytes, little-endian order
39413177Sgibbs *	}
39513177Sgibbs *
39613177Sgibbs *
39714449Sgibbs * In FreeBSD, the scatter list entry is only 8 bytes.
39813177Sgibbs * 
39913177Sgibbs * struct ahc_dma_seg {
40013177Sgibbs *       physaddr addr;                  four bytes, little-endian order
40113177Sgibbs *       long    len;                    four bytes, little endian order
40213177Sgibbs * };
40313177Sgibbs */
4049928Sgibbs
40514449Sgibbs	mov	HADDR0,DFDAT
40614449Sgibbs	mov	HADDR1,DFDAT
40714449Sgibbs	mov	HADDR2,DFDAT
40814449Sgibbs	mov	HADDR3,DFDAT
40913177Sgibbs/*
41013177Sgibbs * For Linux, we must throw away four bytes since there is a 32bit gap
41114449Sgibbs * in the middle of a struct scatterlist.
41213177Sgibbs */
41315709Sgibbs#ifdef __linux__
41413177Sgibbs	mov	NONE,DFDAT
41513177Sgibbs	mov	NONE,DFDAT
41613177Sgibbs	mov	NONE,DFDAT
41713177Sgibbs	mov	NONE,DFDAT
41814449Sgibbs#endif
41913690Sgibbs	mov	HCNT0,DFDAT
42013690Sgibbs	mov	HCNT1,DFDAT
42113690Sgibbs	mov	HCNT2,DFDAT
4224568Sgibbs
42313177Sgibbs/* Load STCNT as well.  It is a mirror of HCNT */
42413690Sgibbs	mov	STCNT0,HCNT0
42513690Sgibbs	mov	STCNT1,HCNT1
42613690Sgibbs	mov	STCNT2,HCNT2
4279928Sgibbs        test    SSTAT1,PHASEMIS  jz data_phase_loop
4284568Sgibbs
4299928Sgibbsdata_phase_finish:
43013177Sgibbs/*
43113177Sgibbs * After a DMA finishes, save the SG and STCNT residuals back into the SCB
43213177Sgibbs * We use STCNT instead of HCNT, since it's a reflection of how many bytes 
43313177Sgibbs * were transferred on the SCSI (as opposed to the host) bus.
43413177Sgibbs */
43513690Sgibbs	mov	SCB_RESID_DCNT0,STCNT0
43613690Sgibbs	mov	SCB_RESID_DCNT1,STCNT1
43713690Sgibbs	mov	SCB_RESID_DCNT2,STCNT2
43813177Sgibbs	mov	SCB_RESID_SGCNT, SG_COUNT
4394568Sgibbs	jmp	ITloop
4404568Sgibbs
44113177Sgibbs/*
44215328Sgibbs * Command phase.  Set up the DMA registers and let 'er rip.
44313177Sgibbs */
4444568Sgibbsp_command:
4454568Sgibbs	call	assert
4464568Sgibbs
44713177Sgibbs/*
44815328Sgibbs * Load HADDR and HCNT.
44913177Sgibbs */
45013690Sgibbs	mov	HADDR0, SCB_CMDPTR0
45113690Sgibbs	mov	HADDR1, SCB_CMDPTR1
45213690Sgibbs	mov	HADDR2, SCB_CMDPTR2
45313690Sgibbs	mov	HADDR3, SCB_CMDPTR3
45413690Sgibbs	mov	HCNT0, SCB_CMDLEN
45513690Sgibbs	clr	HCNT1
45613690Sgibbs	clr	HCNT2
4574568Sgibbs
45813690Sgibbs	mov	STCNT0, HCNT0
45913690Sgibbs	mov	STCNT1, HCNT1
46013690Sgibbs	mov	STCNT2, HCNT2
4614568Sgibbs
4624568Sgibbs	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
4634568Sgibbs						#   DIRECTION|FIFORESET
4644568Sgibbs	jmp	ITloop
4654568Sgibbs
46613177Sgibbs/*
46713177Sgibbs * Status phase.  Wait for the data byte to appear, then read it
46813177Sgibbs * and store it into the SCB.
46913177Sgibbs */
4704568Sgibbsp_status:
47113177Sgibbs	mvi	SCB_TARGET_STATUS	call inb_first
4729954Sgibbs	jmp	mesgin_done
4734568Sgibbs
47413177Sgibbs/*
47515328Sgibbs * Message out phase.  If there is not an active message, but the target
47613177Sgibbs * took us into this phase anyway, build a no-op message and send it.
47713177Sgibbs */
4784568Sgibbsp_mesgout:
47913177Sgibbs	test	MSG_LEN, 0xff	jnz  p_mesgout_start
48013177Sgibbs	mvi	MSG_NOP		call mk_mesg	/* build NOP message */
4814568Sgibbs
48213177Sgibbsp_mesgout_start:
48313177Sgibbs/*
48413177Sgibbs * Set up automatic PIO transfer from MSG0.  Bit 3 in
48513177Sgibbs * SXFRCTL0 (SPIOEN) is already on.
48613177Sgibbs */
48713177Sgibbs	mvi	SINDEX,MSG0
4884568Sgibbs	mov	DINDEX,MSG_LEN
4894568Sgibbs
49013177Sgibbs/*
49113177Sgibbs * When target asks for a byte, drop ATN if it's the last one in
49213177Sgibbs * the message.  Otherwise, keep going until the message is exhausted.
49313177Sgibbs *
49413177Sgibbs * Keep an eye out for a phase change, in case the target issues
49513177Sgibbs * a MESSAGE REJECT.
49613177Sgibbs */
49713177Sgibbsp_mesgout_loop:
49813313Sgibbs	test	SSTAT1,PHASEMIS	jnz p_mesgout_phasemis
49913177Sgibbs	test	SSTAT0,SPIORDY	jz p_mesgout_loop
50013177Sgibbs	cmp	DINDEX,1	jne p_mesgout_outb	/* last byte? */
50113177Sgibbs	mvi	CLRSINT1,CLRATNO			/* drop ATN */
50213177Sgibbsp_mesgout_outb:
50313177Sgibbs	dec	DINDEX
50413386Sgibbs	or	CLRSINT0, CLRSPIORDY
5054568Sgibbs	mov	SCSIDATL,SINDIR
50613313Sgibbs	
5074568Sgibbsp_mesgout4:
50813177Sgibbs	test	DINDEX,0xff	jnz p_mesgout_loop
5094568Sgibbs
51013177Sgibbs/*
51113177Sgibbs * If the next bus phase after ATN drops is a message out, it means
51213177Sgibbs * that the target is requesting that the last message(s) be resent.
51313177Sgibbs */
51413177Sgibbsp_mesgout_snoop:
51513177Sgibbs	test	SSTAT1,BUSFREE	jnz p_mesgout_done
51613177Sgibbs	test	SSTAT1,REQINIT	jz p_mesgout_snoop
5174568Sgibbs
51813177Sgibbs	test	SSTAT1,PHASEMIS	jnz p_mesgout_done
5194568Sgibbs
52013690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
52113177Sgibbs
5224568Sgibbs	jmp	ITloop
5234568Sgibbs
52413177Sgibbsp_mesgout_phasemis:
52515328Sgibbs	mvi	CLRSINT1,CLRATNO	/* Be sure to turn ATNO off */
52613177Sgibbsp_mesgout_done:
52713177Sgibbs	clr	MSG_LEN			/* no active msg */
5284568Sgibbs	jmp	ITloop
5294568Sgibbs
53013177Sgibbs/*
53113177Sgibbs * Message in phase.  Bytes are read using Automatic PIO mode.
53213177Sgibbs */
5334568Sgibbsp_mesgin:
53413177Sgibbs	mvi	A		call inb_first	/* read the 1st message byte */
53513177Sgibbs	mov	REJBYTE,A			/* save it for the driver */
5364568Sgibbs
53713177Sgibbs	test	A,MSG_IDENTIFY		jnz mesgin_identify
53813177Sgibbs	cmp	A,MSG_DISCONNECT	je mesgin_disconnect
53913177Sgibbs	cmp	A,MSG_SDPTRS		je mesgin_sdptrs
54013177Sgibbs	cmp	ALLZEROS,A		je mesgin_complete
54113177Sgibbs	cmp	A,MSG_RDPTRS		je mesgin_rdptrs
54213177Sgibbs	cmp	A,MSG_EXTENDED		je mesgin_extended
54313177Sgibbs	cmp	A,MSG_REJECT		je mesgin_reject
5444568Sgibbs
5459954Sgibbsrej_mesgin:
54613177Sgibbs/*
54713177Sgibbs * We have no idea what this message in is, and there's no way
54813177Sgibbs * to pass it up to the kernel, so we issue a message reject and
54913177Sgibbs * hope for the best.  Since we're now using manual PIO mode to
55013177Sgibbs * read in the message, there should no longer be a race condition
55113177Sgibbs * present when we assert ATN.  In any case, rejection should be a
55213177Sgibbs * rare occurrence - signal the driver when it happens.
55313177Sgibbs */
55413690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
55513177Sgibbs	mvi	INTSTAT,SEND_REJECT		/* let driver know */
5569954Sgibbs
55713177Sgibbs	mvi	MSG_REJECT	call mk_mesg
5589954Sgibbs
5599954Sgibbsmesgin_done:
56013177Sgibbs	call	inb_last			/*ack & turn auto PIO back on*/
5619954Sgibbs	jmp	ITloop
5629954Sgibbs
5639954Sgibbs
5649954Sgibbsmesgin_complete:
56513177Sgibbs/*
56615328Sgibbs * We got a "command complete" message, so put the SCB_TAG into QUEUEOUT,
56715328Sgibbs * and trigger a completion interrupt.  Check status for non zero return
56815328Sgibbs * and interrupt driver if needed.  This allows the driver to interpret
56915328Sgibbs * errors only when they occur instead of always uploading the scb.  If
57015328Sgibbs * the status is SCSI_CHECK, the driver will download a new scb requesting
57115328Sgibbs * sense to replace the old one, modify the "waiting for selection" SCB list
57215328Sgibbs * and set RETURN_1 to SEND_SENSE.  If RETURN_1 is set to SEND_SENSE the
57315328Sgibbs * sequencer imediately jumps to main loop where it will run down the waiting
57415328Sgibbs * SCB list and process the sense request.  If the kernel driver does not
57515328Sgibbs * wish to request sense, it need only clear RETURN_1, and the command is
57615328Sgibbs * allowed to complete.  We don't bother to post to the QOUTFIFO in the
57715328Sgibbs * error case since it would require extra work in the kernel driver to
57815328Sgibbs * ensure that the entry was removed before the command complete code tried
57915328Sgibbs * processing it.
58013177Sgibbs *
58113177Sgibbs * First check for residuals
58213177Sgibbs */
58313177Sgibbs	test	SCB_RESID_SGCNT,0xff	jz check_status
58413177Sgibbs/*
58513177Sgibbs * If we have a residual count, interrupt and tell the host.  Other
58613177Sgibbs * alternatives are to pause the sequencer on all command completes (yuck),
58713177Sgibbs * dma the resid directly to the host (slick, we may have space to do it now)
58813177Sgibbs * or have the sequencer pause itself when it encounters a non-zero resid 
58913177Sgibbs * (unecessary pause just to flag the command -yuck-, but takes one instruction
59013177Sgibbs * and since it shouldn't happen that often is good enough for our purposes).  
59113177Sgibbs */
59213177Sgibbsresid:
59313177Sgibbs	mvi	INTSTAT,RESIDUAL
5944568Sgibbs
5957532Sgibbscheck_status:
59613177Sgibbs	test	SCB_TARGET_STATUS,0xff	jz status_ok	/* Good Status? */
59713177Sgibbs	mvi	INTSTAT,BAD_STATUS			/* let driver know */
59813177Sgibbs	cmp	RETURN_1, SEND_SENSE	jne status_ok
5999954Sgibbs	jmp	mesgin_done
6005326Sgibbs
6014568Sgibbsstatus_ok:
60213177Sgibbs/* First, mark this target as free. */
60313177Sgibbs	test	SCB_CONTROL,TAG_ENB jnz test_immediate	/*
60413177Sgibbs							 * Tagged commands
60513177Sgibbs							 * don't busy the
60613177Sgibbs							 * target.
60713177Sgibbs							 */
60813177Sgibbs	mov	FUNCTION1,SCB_TCL
6095326Sgibbs	mov	A,FUNCTION1
61013177Sgibbs	test	SCB_TCL,0x88 jz clear_a
6115326Sgibbs	xor	ACTIVE_B,A
61213177Sgibbs	jmp	test_immediate
6135326Sgibbs
6145326Sgibbsclear_a:
6155326Sgibbs	xor	ACTIVE_A,A
6165326Sgibbs
61713177Sgibbstest_immediate:
61813177Sgibbs	test    SCB_CMDLEN,0xff jnz complete  /* Immediate message complete */
61913177Sgibbs/*
62013177Sgibbs * Pause the sequencer until the driver gets around to handling the command
62113177Sgibbs * complete.  This is so that any action that might require carefull timing
62213177Sgibbs * with the completion of this command can occur.
62313177Sgibbs */
6249810Sgibbs	mvi	INTSTAT,IMMEDDONE
62513177Sgibbs	jmp	start
6265326Sgibbscomplete:
62715328Sgibbs	mov	QOUTFIFO,SCB_TAG
6287532Sgibbs	mvi	INTSTAT,CMDCMPLT
6299954Sgibbs	jmp	mesgin_done
6304568Sgibbs
6317532Sgibbs
63213177Sgibbs/*
63313177Sgibbs * Is it an extended message?  We only support the synchronous and wide data
63413177Sgibbs * transfer request messages, which will probably be in response to
63513177Sgibbs * WDTR or SDTR message outs from us.  If it's not SDTR or WDTR, reject it -
63613177Sgibbs * apparently this can be done after any message in byte, according
63713177Sgibbs * to the SCSI-2 spec.
63813177Sgibbs */
6399954Sgibbsmesgin_extended:
64013177Sgibbs	mvi	ARG_1		call inb_next	/* extended message length */
64115328Sgibbs	mvi	REJBYTE_EXT	call inb_next	/* extended message code */
6424568Sgibbs
64315328Sgibbs	cmp	REJBYTE_EXT,MSG_SDTR	je p_mesginSDTR
64415328Sgibbs	cmp	REJBYTE_EXT,MSG_WDTR	je p_mesginWDTR
6459954Sgibbs	jmp	rej_mesgin
6465562Sgibbs
6475562Sgibbsp_mesginWDTR:
64813177Sgibbs	cmp	ARG_1,2		jne rej_mesgin	/* extended mesg length=2 */
64913177Sgibbs	mvi	ARG_1		call inb_next	/* Width of bus */
65013177Sgibbs	mvi	INTSTAT,WDTR_MSG		/* let driver know */
65113177Sgibbs	test	RETURN_1,0xff jz mesgin_done	/* Do we need to send WDTR? */
65213177Sgibbs	cmp	RETURN_1,SEND_REJ je rej_mesgin /*
65313177Sgibbs						 * Bus width was too large 
65413177Sgibbs						 * Reject it.
65513177Sgibbs						 */
6565562Sgibbs
65713177Sgibbs/* We didn't initiate the wide negotiation, so we must respond to the request */
65813177Sgibbs	and	RETURN_1,0x7f			/* Clear the SEND_WDTR Flag */
65913177Sgibbs	mvi	DINDEX,MSG0
66013177Sgibbs	mvi	MSG0	call mk_wdtr		/* build WDTR message */
66113690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
6629954Sgibbs	jmp	mesgin_done
6635562Sgibbs
6645562Sgibbsp_mesginSDTR:
66513177Sgibbs	cmp	ARG_1,3		jne rej_mesgin	/* extended mesg length=3 */
66613177Sgibbs	mvi	ARG_1		call inb_next	/* xfer period */
66713177Sgibbs	mvi	A		call inb_next	/* REQ/ACK offset */
66813177Sgibbs	mvi	INTSTAT,SDTR_MSG		/* call driver to convert */
6694568Sgibbs
67013177Sgibbs	test	RETURN_1,0xff	jz mesgin_done  /* Do we need to mk_sdtr/rej */
67113177Sgibbs	cmp	RETURN_1,SEND_REJ je rej_mesgin /*
67213177Sgibbs						 * Requested SDTR too small
67313177Sgibbs						 * Reject it.
67413177Sgibbs						 */
67515328Sgibbs	clr	ARG_1				/* Use the scratch ram rate */
67613177Sgibbs	mvi	DINDEX, MSG0
67713177Sgibbs	mvi     MSG0     call mk_sdtr
67813690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
6799954Sgibbs	jmp	mesgin_done
6804568Sgibbs
68113177Sgibbs/*
68213177Sgibbs * Is it a disconnect message?  Set a flag in the SCB to remind us
68313177Sgibbs * and await the bus going free.
68413177Sgibbs */
6859954Sgibbsmesgin_disconnect:
68613177Sgibbs	or	SCB_CONTROL,DISCONNECTED
68715328Sgibbs	test	FLAGS, PAGESCBS jz mesgin_done
68815328Sgibbs/*
68915328Sgibbs * Link this SCB into the DISCONNECTED list.  This list holds the
69015328Sgibbs * candidates for paging out an SCB if one is needed for a new command.
69115328Sgibbs * Modifying the disconnected list is a critical(pause dissabled) section.
69215328Sgibbs */
69315328Sgibbs	mvi	SCB_PREV, SCB_LIST_NULL
69415328Sgibbs	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
69515328Sgibbs	mov	SCB_NEXT, DISCONNECTED_SCBH
69615328Sgibbs	mov	DISCONNECTED_SCBH, SCBPTR
69715328Sgibbs	cmp	SCB_NEXT,SCB_LIST_NULL je linkdone
69815328Sgibbs	mov	SCBPTR,SCB_NEXT
69915328Sgibbs	mov	SCB_PREV,DISCONNECTED_SCBH
70015328Sgibbs	mov	SCBPTR,DISCONNECTED_SCBH
70115328Sgibbslinkdone:
70215328Sgibbs	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
7039954Sgibbs	jmp	mesgin_done
7044568Sgibbs
70513177Sgibbs/*
70613177Sgibbs * Save data pointers message?  Copy working values into the SCB,
70713177Sgibbs * usually in preparation for a disconnect.
70813177Sgibbs */
7099954Sgibbsmesgin_sdptrs:
7104568Sgibbs	call	sg_ram2scb
7119954Sgibbs	jmp	mesgin_done
7124568Sgibbs
71313177Sgibbs/*
71413177Sgibbs * Restore pointers message?  Data pointers are recopied from the
71513177Sgibbs * SCB anytime we enter a data phase for the first time, so all
71613177Sgibbs * we need to do is clear the DPHASE flag and let the data phase
71713177Sgibbs * code do the rest.
71813177Sgibbs */
7199954Sgibbsmesgin_rdptrs:
72015328Sgibbs	and	FLAGS,0xef			/*
72113177Sgibbs						 * !DPHASE we'll reload them
72213177Sgibbs						 * the next time through
72313177Sgibbs						 */
7249954Sgibbs	jmp	mesgin_done
7254568Sgibbs
72613177Sgibbs/*
72713177Sgibbs * Identify message?  For a reconnecting target, this tells us the lun
72813177Sgibbs * that the reconnection is for - find the correct SCB and switch to it,
72913177Sgibbs * clearing the "disconnected" bit so we don't "find" it by accident later.
73013177Sgibbs */
7319954Sgibbsmesgin_identify:
73213177Sgibbs	test	A,0x78	jnz rej_mesgin	/*!DiscPriv|!LUNTAR|!Reserved*/
7334568Sgibbs
73413177Sgibbs	and	A,0x07			/* lun in lower three bits */
7356608Sgibbs	or      SAVED_TCL,A,SELID          
7366608Sgibbs	and     SAVED_TCL,0xf7
73713177Sgibbs	and     A,SELBUSB,SBLKCTL	/* B Channel?? */
7386608Sgibbs	or      SAVED_TCL,A
73913177Sgibbs	call	inb_last		/* ACK */
74013177Sgibbs
74113177Sgibbs/*
74213177Sgibbs * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
74315328Sgibbs * If we get one, we use the tag returned to switch to find the proper
74415328Sgibbs * SCB.  With SCB paging, this requires using findSCB for both tagged
74515328Sgibbs * and non-tagged transactions since the SCB may exist in any slot.
74615328Sgibbs * If we're not using SCB paging, we can use the tag as the direct
74715328Sgibbs * index to the SCB.
74813177Sgibbs */
74915328Sgibbs	mvi	ARG_1,SCB_LIST_NULL	/* Default to no-tag */
75013177Sgibbssnoop_tag_loop:
75113177Sgibbs	test	SSTAT1,BUSFREE	jnz use_findSCB
75213177Sgibbs	test	SSTAT1,REQINIT	jz snoop_tag_loop
75313177Sgibbs	test	SSTAT1,PHASEMIS	jnz use_findSCB
75413177Sgibbs	mvi	A		call inb_first
75515328Sgibbs	cmp	A,MSG_SIMPLE_TAG jne use_findSCB
7566608Sgibbsget_tag:
75713177Sgibbs	mvi	ARG_1	call inb_next	/* tag value */
75813177Sgibbs/*
75913177Sgibbs * See if the tag is in range.  The tag is < SCBCOUNT if we add
76013177Sgibbs * the complement of SCBCOUNT to the incomming tag and there is
76113177Sgibbs * no carry.
76213177Sgibbs */
76313177Sgibbs	mov	A,COMP_SCBCOUNT	
76413177Sgibbs	add	SINDEX,A,ARG_1
76513177Sgibbs	jc	abort_tag
76613177Sgibbs
76713177Sgibbs/*
76815328Sgibbs * Ensure that the SCB the tag points to is for an SCB transaction
76913177Sgibbs * to the reconnecting target.
77013177Sgibbs */
77115328Sgibbs	test	FLAGS, PAGESCBS	jz index_by_tag
77215328Sgibbs	call	inb_last			/* Ack Tag */
77315328Sgibbsuse_findSCB:
77415328Sgibbs	mov	ALLZEROS	call findSCB	  /* Have to search */
77515328Sgibbssetup_SCB:
77615328Sgibbs	and	SCB_CONTROL,0xfb	  /* clear disconnect bit in SCB */
77715328Sgibbs	or	FLAGS,IDENTIFY_SEEN	  /* make note of IDENTIFY */
77815328Sgibbs	jmp	ITloop
77915328Sgibbsindex_by_tag:
78013177Sgibbs	mov	SCBPTR,ARG_1
7816608Sgibbs	mov	A,SAVED_TCL
78213177Sgibbs	cmp	SCB_TCL,A		jne abort_tag
78313177Sgibbs	test	SCB_CONTROL,TAG_ENB	jz  abort_tag
78413177Sgibbs	call	inb_last			/* Ack Successful tag */
78513177Sgibbs	jmp	setup_SCB
78615328Sgibbs
7877700Sgibbsabort_tag:
78813690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
78913177Sgibbs	mvi	INTSTAT,ABORT_TAG 		/* let driver know */
79015328Sgibbs	mvi	MSG_ABORT_TAG	call mk_mesg	/* ABORT TAG message */
79113177Sgibbs	jmp	mesgin_done
7926608Sgibbs
79313177Sgibbs/*
79413177Sgibbs * Message reject?  Let the kernel driver handle this.  If we have an 
79513177Sgibbs * outstanding WDTR or SDTR negotiation, assume that it's a response from 
79613177Sgibbs * the target selecting 8bit or asynchronous transfer, otherwise just ignore 
79713177Sgibbs * it since we have no clue what it pertains to.
79813177Sgibbs */
7999954Sgibbsmesgin_reject:
80013177Sgibbs	mvi	INTSTAT, REJECT_MSG
8019954Sgibbs	jmp	mesgin_done
8025562Sgibbs
80313177Sgibbs/*
80413177Sgibbs * [ ADD MORE MESSAGE HANDLING HERE ]
80513177Sgibbs */
8064568Sgibbs
80713177Sgibbs/*
80813177Sgibbs * Bus free phase.  It might be useful to interrupt the device
80913177Sgibbs * driver if we aren't expecting this.  For now, make sure that
81013177Sgibbs * ATN isn't being asserted and look for a new command.
81113177Sgibbs */
8124568Sgibbsp_busfree:
81313177Sgibbs	mvi	CLRSINT1,CLRATNO
81414449Sgibbs	clr	LASTPHASE
8158567Sdg
81613177Sgibbs/*
81713177Sgibbs * if this is an immediate command, perform a psuedo command complete to
81813177Sgibbs * notify the driver.
81913177Sgibbs */
82013177Sgibbs	test	SCB_CMDLEN,0xff	jz status_ok
82113177Sgibbs	jmp	start
8224568Sgibbs
82313177Sgibbs/*
82413177Sgibbs * Locking the driver out, build a one-byte message passed in SINDEX
82513177Sgibbs * if there is no active message already.  SINDEX is returned intact.
82613177Sgibbs */
8274568Sgibbsmk_mesg:
82813177Sgibbs	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
82913177Sgibbs	test	MSG_LEN,0xff	jz mk_mesg1	/* Should always succeed */
83013177Sgibbs	
83113177Sgibbs	/*
83213177Sgibbs	 * Hmmm.  For some reason the mesg buffer is in use.
83313177Sgibbs	 * Tell the driver.  It should look at SINDEX to find
83413177Sgibbs	 * out what we wanted to use the buffer for and resolve
83513177Sgibbs	 * the conflict.
83613177Sgibbs	 */
83713177Sgibbs	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
83813690Sgibbs	mvi	INTSTAT,MSG_BUFFER_BUSY
8394568Sgibbs
8404568Sgibbsmk_mesg1:
84113177Sgibbs	mvi	MSG_LEN,1		/* length = 1 */
84213177Sgibbs	mov	MSG0,SINDEX		/* 1-byte message */
84313177Sgibbs	mvi	SEQCTL,0x10	ret	/* !PAUSEDIS|FASTMODE */
8444568Sgibbs
84513177Sgibbs/*
84613177Sgibbs * Functions to read data in Automatic PIO mode.
84713177Sgibbs *
84813177Sgibbs * According to Adaptec's documentation, an ACK is not sent on input from
84913177Sgibbs * the target until SCSIDATL is read from.  So we wait until SCSIDATL is
85013177Sgibbs * latched (the usual way), then read the data byte directly off the bus
85113177Sgibbs * using SCSIBUSL.  When we have pulled the ATN line, or we just want to
85213177Sgibbs * acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
85313177Sgibbs * spec guarantees that the target will hold the data byte on the bus until
85413177Sgibbs * we send our ACK.
85513177Sgibbs *
85613177Sgibbs * The assumption here is that these are called in a particular sequence,
85713177Sgibbs * and that REQ is already set when inb_first is called.  inb_{first,next}
85813177Sgibbs * use the same calling convention as inb.
85913177Sgibbs */
86013177Sgibbs
86113177Sgibbsinb_next:
86213386Sgibbs	or	CLRSINT0, CLRSPIORDY
86313360Sgibbs	mov	NONE,SCSIDATL			/*dummy read from latch to ACK*/
86413360Sgibbsinb_next_wait:
86513360Sgibbs	test	SSTAT1,PHASEMIS	jnz mesgin_phasemis
86613360Sgibbs	test	SSTAT0,SPIORDY	jz inb_next_wait /* wait for next byte */
86713313Sgibbsinb_first:
8684568Sgibbs	mov	DINDEX,SINDEX
86913177Sgibbs	mov	DINDIR,SCSIBUSL	ret		/*read byte directly from bus*/
87013177Sgibbsinb_last:
87113360Sgibbs	mov	NONE,SCSIDATL ret		/*dummy read from latch to ACK*/
8724568Sgibbs
87313177Sgibbsmesgin_phasemis:
87413177Sgibbs/*
87513177Sgibbs * We expected to receive another byte, but the target changed phase
87613177Sgibbs */
87713177Sgibbs	mvi	INTSTAT, MSGIN_PHASEMIS
87813177Sgibbs	jmp	ITloop
8794568Sgibbs
88013177Sgibbs/*
88113177Sgibbs * DMA data transfer.  HADDR and HCNT must be loaded first, and
88213177Sgibbs * SINDEX should contain the value to load DFCNTRL with - 0x3d for
88313177Sgibbs * host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
88413177Sgibbs * during initialization.
88513177Sgibbs */
8864568Sgibbsdma:
8874568Sgibbs	mov	DFCNTRL,SINDEX
8884568Sgibbsdma1:
88913177Sgibbs	test	SSTAT0,DMADONE	jnz dma3
89013177Sgibbs	test	SSTAT1,PHASEMIS	jz dma1		/* ie. underrun */
8914568Sgibbs
89213177Sgibbs/*
89313177Sgibbs * We will be "done" DMAing when the transfer count goes to zero, or
89413177Sgibbs * the target changes the phase (in light of this, it makes sense that
89513177Sgibbs * the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
89613177Sgibbs * doing a SCSI->Host transfer, the data FIFO should be flushed auto-
89713177Sgibbs * magically on STCNT=0 or a phase change, so just wait for FIFO empty
89813177Sgibbs * status.
89913177Sgibbs */
9004568Sgibbsdma3:
90113177Sgibbs	test	SINDEX,DIRECTION	jnz dma5
9024568Sgibbsdma4:
90313177Sgibbs	test	DFSTATUS,FIFOEMP	jz dma4
9044568Sgibbs
90513177Sgibbs/*
90613177Sgibbs * Now shut the DMA enables off and make sure that the DMA enables are 
90713177Sgibbs * actually off first lest we get an ILLSADDR.
90813177Sgibbs */
9094568Sgibbsdma5:
91013177Sgibbs	/* disable DMA, but maintain WIDEODD */
91113690Sgibbs	and	DFCNTRL,WIDEODD
9124568Sgibbsdma6:
91313177Sgibbs	test	DFCNTRL,0x38	jnz dma6  /* SCSIENACK|SDMAENACK|HDMAENACK */
9144568Sgibbs
9154568Sgibbs	ret
9164568Sgibbs
91713177Sgibbs/*
91813177Sgibbs * Common SCSI initialization for selection and reselection.  Expects
91913177Sgibbs * the target SCSI ID to be in the upper four bits of SINDEX, and A's
92013177Sgibbs * contents are stomped on return.
92113177Sgibbs */
9228104Sgibbsinitialize_scsiid:
92313177Sgibbs	and	SINDEX,0xf0		/* Get target ID */
9245775Sgibbs	and	A,0x0f,SCSIID
9255775Sgibbs	or	SINDEX,A
9268104Sgibbs	mov	SCSIID,SINDEX ret
9275326Sgibbs
92813177Sgibbs/*
92913177Sgibbs * Assert that if we've been reselected, then we've seen an IDENTIFY
93013177Sgibbs * message.
93113177Sgibbs */
9324568Sgibbsassert:
93313177Sgibbs	test	FLAGS,RESELECTED	jz return	/* reselected? */
93413177Sgibbs	test	FLAGS,IDENTIFY_SEEN	jnz return	/* seen IDENTIFY? */
9354568Sgibbs
93613177Sgibbs	mvi	INTSTAT,NO_IDENT 	ret	/* no - cause a kernel panic */
9374568Sgibbs
93813177Sgibbs/*
93915328Sgibbs * Locate the SCB matching the target ID/channel/lun in SAVED_TCL, and the tag
94015328Sgibbs * value in ARG_1.  If ARG_1 == SCB_LIST_NULL, we're looking for a non-tagged
94115328Sgibbs * SCB.  Have the kernel print a warning message if it can't be found, and
94215328Sgibbs * generate an ABORT/ABORT_TAG message to the target.  SINDEX should be
94313177Sgibbs * cleared on call.
94413177Sgibbs */
9454568SgibbsfindSCB:
9466608Sgibbs	mov	A,SAVED_TCL
94715328Sgibbs	mov	SCBPTR,SINDEX			/* switch to next SCB */
94815328Sgibbs	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
94913177Sgibbs	cmp	SCB_TCL,A	jne findSCB1 /* target ID/channel/lun match? */
95013177Sgibbs	test	SCB_CONTROL,DISCONNECTED jz findSCB1 /*should be disconnected*/
95115328Sgibbs	test	SCB_CONTROL,TAG_ENB jnz findTaggedSCB
95215328Sgibbs	cmp	ARG_1,SCB_LIST_NULL je foundSCB
95315328Sgibbs	jmp	findSCB1
95415328SgibbsfindTaggedSCB:
95515328Sgibbs	mov	A, ARG_1			/* Tag passed in ARG_1 */
95615328Sgibbs	cmp	SCB_TAG,A	jne findSCB1	/* Found it? */
95715328SgibbsfoundSCB:
95815328Sgibbs	test	FLAGS,PAGESCBS	jz foundSCB_ret
95915328Sgibbs/* Remove this SCB from the disconnection list */
96015328Sgibbs	cmp	SCB_NEXT,SCB_LIST_NULL je unlink_prev
96115328Sgibbs	mov	SAVED_LINKPTR, SCB_PREV
96215328Sgibbs	mov	SCBPTR, SCB_NEXT
96315328Sgibbs	mov	SCB_PREV, SAVED_LINKPTR
96415328Sgibbs	mov	SCBPTR, SINDEX
96515328Sgibbsunlink_prev:
96615328Sgibbs	cmp	SCB_PREV,SCB_LIST_NULL	je rHead/* At the head of the list */
96715328Sgibbs	mov	SAVED_LINKPTR, SCB_NEXT
96815328Sgibbs	mov	SCBPTR, SCB_PREV
96915328Sgibbs	mov	SCB_NEXT, SAVED_LINKPTR
97015328Sgibbs	mov	SCBPTR, SINDEX
97115328Sgibbs	mvi	SEQCTL,0x10	ret		/* !PAUSEDIS|FASTMODE */
97215328SgibbsrHead:
97315328Sgibbs	mov	DISCONNECTED_SCBH,SCB_NEXT
97415328SgibbsfoundSCB_ret:
97515328Sgibbs	mvi	SEQCTL,0x10	ret		/* !PAUSEDIS|FASTMODE */
9764568Sgibbs
9776608SgibbsfindSCB1:
97815328Sgibbs	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
9794568Sgibbs	inc	SINDEX
9804568Sgibbs	mov	A,SCBCOUNT
9816608Sgibbs	cmp	SINDEX,A	jne findSCB
9824568Sgibbs
98313177Sgibbs	mvi	INTSTAT,NO_MATCH		/* not found - signal kernel */
98415328Sgibbs	cmp	RETURN_1,SCB_PAGEDIN je return
98515328Sgibbs	or	SCSISIGO,ATNO			/* assert ATNO */
98615328Sgibbs	cmp	ARG_1,SCB_LIST_NULL jne find_abort_tag
98715328Sgibbs	mvi	MSG_ABORT	call mk_mesg
98815328Sgibbs	jmp	ITloop
98915328Sgibbsfind_abort_tag:
99015328Sgibbs	mvi	MSG_ABORT_TAG	call mk_mesg
99115328Sgibbs	jmp	ITloop
9924568Sgibbs
99313177Sgibbs/*
99413177Sgibbs * Make a working copy of the scatter-gather parameters from the SCB.
99513177Sgibbs */
9964568Sgibbssg_scb2ram:
99713690Sgibbs	mov	HADDR0, SCB_DATAPTR0
99813690Sgibbs	mov	HADDR1, SCB_DATAPTR1
99913690Sgibbs	mov	HADDR2, SCB_DATAPTR2
100013690Sgibbs	mov	HADDR3, SCB_DATAPTR3
100113690Sgibbs	mov	HCNT0, SCB_DATACNT0
100213690Sgibbs	mov	HCNT1, SCB_DATACNT1
100313690Sgibbs	mov	HCNT2, SCB_DATACNT2
10049928Sgibbs
100513690Sgibbs	mov	STCNT0, HCNT0
100613690Sgibbs	mov	STCNT1, HCNT1
100713690Sgibbs	mov	STCNT2, HCNT2
10089928Sgibbs
100913177Sgibbs	mov	SG_COUNT,SCB_SGCOUNT
10104568Sgibbs
101113690Sgibbs	mov	SG_NEXT0, SCB_SGPTR0
101213690Sgibbs	mov	SG_NEXT1, SCB_SGPTR1
101313690Sgibbs	mov	SG_NEXT2, SCB_SGPTR2
101413690Sgibbs	mov	SG_NEXT3, SCB_SGPTR3 ret
10154568Sgibbs
101613177Sgibbs/*
101713177Sgibbs * Copying RAM values back to SCB, for Save Data Pointers message, but
101813177Sgibbs * only if we've actually been into a data phase to change them.  This
101913177Sgibbs * protects against bogus data in scratch ram and the residual counts
102013177Sgibbs * since they are only initialized when we go into data_in or data_out.
102113177Sgibbs */
10224568Sgibbssg_ram2scb:
10239928Sgibbs	test	FLAGS, DPHASE	jz return
102413177Sgibbs	mov	SCB_SGCOUNT,SG_COUNT
10254568Sgibbs
102613690Sgibbs	mov	SCB_SGPTR0,SG_NEXT0
102713690Sgibbs	mov	SCB_SGPTR1,SG_NEXT1
102813690Sgibbs	mov	SCB_SGPTR2,SG_NEXT2
102913690Sgibbs	mov	SCB_SGPTR3,SG_NEXT3
10309928Sgibbs	
103113690Sgibbs	mov	SCB_DATAPTR0,SHADDR0
103213690Sgibbs	mov	SCB_DATAPTR1,SHADDR1
103313690Sgibbs	mov	SCB_DATAPTR2,SHADDR2
103413690Sgibbs	mov	SCB_DATAPTR3,SHADDR3
10354568Sgibbs
103613177Sgibbs/*
103713177Sgibbs * Use the residual number since STCNT is corrupted by any message transfer
103813177Sgibbs */
103913690Sgibbs	mov	SCB_DATACNT0,SCB_RESID_DCNT0
104013690Sgibbs	mov	SCB_DATACNT1,SCB_RESID_DCNT1
104113690Sgibbs	mov	SCB_DATACNT2,SCB_RESID_DCNT2 ret
10424568Sgibbs
104313177Sgibbs/*
104413177Sgibbs * Add the array base TARG_SCRATCH to the target offset (the target address
104513177Sgibbs * is in SCSIID), and return the result in SINDEX.  The accumulator
104613177Sgibbs * contains the 3->8 decoding of the target ID on return.
104713177Sgibbs */
10485562Sgibbsndx_dtr:
10494568Sgibbs	shr	A,SCSIID,4
105013177Sgibbs	test	SBLKCTL,SELBUSB	jz ndx_dtr_2
105113177Sgibbs	or	A,0x08		/* Channel B entries add 8 */
10525562Sgibbsndx_dtr_2:
105313690Sgibbs	add	SINDEX,TARG_SCRATCH,A ret
10544568Sgibbs
105513177Sgibbs/*
105613177Sgibbs * If we need to negotiate transfer parameters, build the WDTR or SDTR message
105713177Sgibbs * starting at the address passed in SINDEX.  DINDEX is modified on return.
105813177Sgibbs * The SCSI-II spec requires that Wide negotiation occur first and you can
105913177Sgibbs * only negotiat one or the other at a time otherwise in the event of a message
106013177Sgibbs * reject, you wouldn't be able to tell which message was the culpret.
106113177Sgibbs */
10625562Sgibbsmk_dtr:
106313177Sgibbs	test	SCB_CONTROL,NEEDWDTR jnz  mk_wdtr_16bit
106415328Sgibbs	mvi	ARG_1, MAXOFFSET	/* Force an offset of 15 or 8 if WIDE */
10654568Sgibbs
10667532Sgibbsmk_sdtr:
106713177Sgibbs	mvi	DINDIR,1		/* extended message */
106813177Sgibbs	mvi	DINDIR,3		/* extended message length = 3 */
106913177Sgibbs	mvi	DINDIR,1		/* SDTR code */
10707532Sgibbs	call	sdtr_to_rate
107113177Sgibbs	mov	DINDIR,RETURN_1		/* REQ/ACK transfer period */
107215328Sgibbs	cmp	ARG_1, MAXOFFSET je mk_sdtr_max_offset
107313177Sgibbs	and	DINDIR,0x0f,SINDIR	/* Sync Offset */
10747532Sgibbs
10757532Sgibbsmk_sdtr_done:
107613177Sgibbs	add	MSG_LEN,COMP_MSG0,DINDEX ret	/* update message length */
10777532Sgibbs
10788567Sdgmk_sdtr_max_offset:
107913177Sgibbs/*
108013177Sgibbs * We're initiating sync negotiation, so request the max offset we can (15 or 8)
108113177Sgibbs */
108213177Sgibbs	/* Talking to a WIDE device? */
108313177Sgibbs	test	SCSIRATE, WIDEXFER	jnz wmax_offset	
10848567Sdg	mvi	DINDIR, MAX_OFFSET_8BIT
10857532Sgibbs	jmp	mk_sdtr_done
10867532Sgibbs
10878567Sdgwmax_offset:
108813177Sgibbs	mvi	DINDIR, MAX_OFFSET_16BIT
10898567Sdg	jmp	mk_sdtr_done
10908567Sdg
10915647Sgibbsmk_wdtr_16bit:
10925647Sgibbs	mvi	ARG_1,BUS_16_BIT
10935562Sgibbsmk_wdtr:
109413177Sgibbs	mvi	DINDIR,1		/* extended message */
109513177Sgibbs	mvi	DINDIR,2		/* extended message length = 2 */
109613177Sgibbs	mvi	DINDIR,3		/* WDTR code */
109713177Sgibbs	mov	DINDIR,ARG_1		/* bus width */
10985562Sgibbs
109913177Sgibbs	add	MSG_LEN,COMP_MSG0,DINDEX ret	/* update message length */
11005562Sgibbs	
11015562Sgibbssdtr_to_rate:
110213177Sgibbs	call	ndx_dtr			/* index scratch space for target */
11035562Sgibbs	shr	A,SINDIR,0x4
110413177Sgibbs	dec	SINDEX			/* Preserve SINDEX */
11055562Sgibbs	and	A,0x7
11065562Sgibbs	clr	RETURN_1
11075562Sgibbssdtr_to_rate_loop:
11085562Sgibbs	test	A,0x0f	jz sdtr_to_rate_done
110913177Sgibbs	add	RETURN_1,0x19
11105562Sgibbs	dec	A	
11115562Sgibbs	jmp	sdtr_to_rate_loop
11125562Sgibbssdtr_to_rate_done:
11135562Sgibbs	shr	RETURN_1,0x2
111413177Sgibbs	add	RETURN_1,0x19
111511829Sgibbs	test	SXFRCTL0,ULTRAEN jz return
111611829Sgibbs	shr	RETURN_1,0x1
11177532Sgibbsreturn:
11187532Sgibbs	ret
1119