aic7xxx.seq revision 15843
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
4215843SgibbsVERSION AIC7XXX_SEQ_VER "$Id: aic7xxx.seq,v 1.34 1996/05/10 16:11:01 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/*
25715843Sgibbs * Initialize Ultra mode setting.  ndx_dtr has left our target mask
25815843Sgibbs * in A.
25915843Sgibbs */
26015843Sgibbs	and	SINDEX,0xdf,SXFRCTL0		/* default to Ultra disabled */
26115843Sgibbs	test	SCSIID, 0x80	 jnz ultra_b	/* Target ID > 7 */
26215843Sgibbs	test	SBLKCTL, SELBUSB jnz ultra_b	/* Second channel device */
26315843Sgibbs	test	ULTRA_ENB,A	 jz  set_sxfrctl0
26415843Sgibbs	or	SINDEX, ULTRAEN  jmp set_sxfrctl0
26515843Sgibbsultra_b:
26615843Sgibbs	test	ULTRA_ENB_B,A	 jz  set_sxfrctl0
26715843Sgibbs	or	SINDEX, ULTRAEN
26815843Sgibbs
26915843Sgibbsset_sxfrctl0:
27015843Sgibbs	mov	SXFRCTL0,SINDEX
27115843Sgibbs
27213177Sgibbs	mvi	SCSISEQ,ENAUTOATNP		/*
27313177Sgibbs						 * ATN on parity errors
27413177Sgibbs						 * for "in" phases
27513177Sgibbs						 */
27613177Sgibbs	mvi	CLRSINT1,CLRBUSFREE
27713177Sgibbs	mvi	CLRSINT0,0x60			/* CLRSELDI|CLRSELDO */
27813177Sgibbs/*
27913177Sgibbs * Main loop for information transfer phases.  If BSY is false, then
28013177Sgibbs * we have a bus free condition, expected or not.  Otherwise, wait
28113177Sgibbs * for the target to assert REQ before checking MSG, C/D and I/O
28213177Sgibbs * for the bus phase.
28313177Sgibbs *
28413177Sgibbs */
2854568SgibbsITloop:
28613177Sgibbs	test	SSTAT1,BUSFREE	jnz p_busfree
28713177Sgibbs	test	SSTAT1,REQINIT	jz ITloop
2884568Sgibbs
28913177Sgibbs	and	A,PHASE_MASK,SCSISIGI
29013690Sgibbs	mov	LASTPHASE,A
29113690Sgibbs	mov	SCSISIGO,A
2924568Sgibbs
2934568Sgibbs	cmp	ALLZEROS,A	je p_dataout
29413177Sgibbs	cmp	A,P_DATAIN	je p_datain
29513177Sgibbs	cmp	A,P_COMMAND	je p_command
29613177Sgibbs	cmp	A,P_MESGOUT	je p_mesgout
29713177Sgibbs	cmp	A,P_STATUS	je p_status
29813177Sgibbs	cmp	A,P_MESGIN	je p_mesgin
2994568Sgibbs
30013177Sgibbs	mvi	INTSTAT,BAD_PHASE	/* unknown phase - signal driver */
30115843Sgibbs	jmp	ITloop			/* Try reading the bus again. */
3024568Sgibbs
3034568Sgibbsp_dataout:
30413177Sgibbs	mvi	DMAPARAMS,0x7d			/*
30513177Sgibbs						 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
30613177Sgibbs						 * DIRECTION|FIFORESET
30713177Sgibbs						 */
3089928Sgibbs	jmp	data_phase_init
3094568Sgibbs
31013177Sgibbs/*
31113177Sgibbs * If we re-enter the data phase after going through another phase, the
31213177Sgibbs * STCNT may have been cleared, so restore it from the residual field.
31313177Sgibbs */
3149928Sgibbsdata_phase_reinit:
31513690Sgibbs	mov	STCNT0,SCB_RESID_DCNT0
31613690Sgibbs	mov	STCNT1,SCB_RESID_DCNT1
31713690Sgibbs	mov	STCNT2,SCB_RESID_DCNT2
3189928Sgibbs	jmp	data_phase_loop
3194568Sgibbs
3209928Sgibbsp_datain:
32113177Sgibbs	mvi	DMAPARAMS,0x79		/*
32213177Sgibbs					 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
32313177Sgibbs					 * !DIRECTION|FIFORESET
32413177Sgibbs					 */
3259928Sgibbsdata_phase_init:
3269928Sgibbs	call	assert
3275775Sgibbs
3289928Sgibbs	test	FLAGS, DPHASE	jnz data_phase_reinit
3299928Sgibbs	call	sg_scb2ram
33013177Sgibbs	or	FLAGS, DPHASE		/* We have seen a data phase */
3314568Sgibbs
3329928Sgibbsdata_phase_loop:
33313177Sgibbs/* If we are the last SG block, don't set wideodd. */
3349928Sgibbs	cmp	SG_COUNT,0x01 jne data_phase_wideodd
33513177Sgibbs	and	DMAPARAMS, 0xbf		/* Turn off WIDEODD */
3369928Sgibbsdata_phase_wideodd:
3379928Sgibbs	mov	DMAPARAMS  call dma
3384568Sgibbs
33913177Sgibbs/* Exit if we had an underrun */
34013177Sgibbs	test	SSTAT0,SDONE	jz data_phase_finish /* underrun STCNT != 0 */
3417532Sgibbs
34213177Sgibbs/*
34313177Sgibbs * Advance the scatter-gather pointers if needed 
34413177Sgibbs */
3459928Sgibbssg_advance:
34613177Sgibbs	dec	SG_COUNT	/* one less segment to go */
3474568Sgibbs
34813177Sgibbs	test	SG_COUNT, 0xff	jz data_phase_finish /* Are we done? */
3494568Sgibbs
35013177Sgibbs	clr	A			/* add sizeof(struct scatter) */
35113177Sgibbs	add	SG_NEXT0,SG_SIZEOF,SG_NEXT0
35213177Sgibbs	adc	SG_NEXT1,A,SG_NEXT1
3534568Sgibbs
35413177Sgibbs/*
35513177Sgibbs * Load a struct scatter and set up the data address and length.
35613177Sgibbs * If the working value of the SG count is nonzero, then
35713177Sgibbs * we need to load a new set of values.
35813177Sgibbs *
35915328Sgibbs * This, like all DMA's, assumes little-endian host data storage.
36013177Sgibbs */
3619928Sgibbssg_load:
36213177Sgibbs	clr	HCNT2
36313177Sgibbs	clr	HCNT1
36413177Sgibbs	mvi	HCNT0,SG_SIZEOF
3654568Sgibbs
36613690Sgibbs	mov	HADDR0,SG_NEXT0
36713690Sgibbs	mov	HADDR1,SG_NEXT1
36813690Sgibbs	mov	HADDR2,SG_NEXT2
36913690Sgibbs	mov	HADDR3,SG_NEXT3
3704568Sgibbs
37113690Sgibbs	or	DFCNTRL,0xd			/* HDMAEN|DIRECTION|FIFORESET */
3729928Sgibbs
37313177Sgibbs/*
37413177Sgibbs * Wait for DMA from host memory to data FIFO to complete, then disable
37513177Sgibbs * DMA and wait for it to acknowledge that it's off.
37613177Sgibbs */
37713690Sgibbsdma_finish:
37813690Sgibbs	test	DFSTATUS,HDONE	jz dma_finish
37913690Sgibbs	/* Turn off DMA preserving WIDEODD */
38013690Sgibbs	and	DFCNTRL,WIDEODD
38113690Sgibbsdma_finish2:
38213690Sgibbs	test	DFCNTRL,HDMAENACK jnz dma_finish2
3839928Sgibbs
38413177Sgibbs/*
38513177Sgibbs * Copy data from FIFO into SCB data pointer and data count.  This assumes
38613177Sgibbs * that the struct scatterlist has this structure (this and sizeof(struct
38714449Sgibbs * scatterlist) == 12 are asserted in aic7xxx.c for the Linux driver):
38813177Sgibbs *
38913177Sgibbs *	struct scatterlist {
39013177Sgibbs *		char *address;		four bytes, little-endian order
39113177Sgibbs *		...			four bytes, ignored
39213177Sgibbs *		unsigned short length;	two bytes, little-endian order
39313177Sgibbs *	}
39413177Sgibbs *
39513177Sgibbs *
39614449Sgibbs * In FreeBSD, the scatter list entry is only 8 bytes.
39713177Sgibbs * 
39813177Sgibbs * struct ahc_dma_seg {
39913177Sgibbs *       physaddr addr;                  four bytes, little-endian order
40013177Sgibbs *       long    len;                    four bytes, little endian order
40113177Sgibbs * };
40213177Sgibbs */
4039928Sgibbs
40414449Sgibbs	mov	HADDR0,DFDAT
40514449Sgibbs	mov	HADDR1,DFDAT
40614449Sgibbs	mov	HADDR2,DFDAT
40714449Sgibbs	mov	HADDR3,DFDAT
40813177Sgibbs/*
40913177Sgibbs * For Linux, we must throw away four bytes since there is a 32bit gap
41014449Sgibbs * in the middle of a struct scatterlist.
41113177Sgibbs */
41215709Sgibbs#ifdef __linux__
41313177Sgibbs	mov	NONE,DFDAT
41413177Sgibbs	mov	NONE,DFDAT
41513177Sgibbs	mov	NONE,DFDAT
41613177Sgibbs	mov	NONE,DFDAT
41714449Sgibbs#endif
41813690Sgibbs	mov	HCNT0,DFDAT
41913690Sgibbs	mov	HCNT1,DFDAT
42013690Sgibbs	mov	HCNT2,DFDAT
4214568Sgibbs
42213177Sgibbs/* Load STCNT as well.  It is a mirror of HCNT */
42313690Sgibbs	mov	STCNT0,HCNT0
42413690Sgibbs	mov	STCNT1,HCNT1
42513690Sgibbs	mov	STCNT2,HCNT2
4269928Sgibbs        test    SSTAT1,PHASEMIS  jz data_phase_loop
4274568Sgibbs
4289928Sgibbsdata_phase_finish:
42913177Sgibbs/*
43013177Sgibbs * After a DMA finishes, save the SG and STCNT residuals back into the SCB
43113177Sgibbs * We use STCNT instead of HCNT, since it's a reflection of how many bytes 
43213177Sgibbs * were transferred on the SCSI (as opposed to the host) bus.
43313177Sgibbs */
43413690Sgibbs	mov	SCB_RESID_DCNT0,STCNT0
43513690Sgibbs	mov	SCB_RESID_DCNT1,STCNT1
43613690Sgibbs	mov	SCB_RESID_DCNT2,STCNT2
43713177Sgibbs	mov	SCB_RESID_SGCNT, SG_COUNT
4384568Sgibbs	jmp	ITloop
4394568Sgibbs
44013177Sgibbs/*
44115328Sgibbs * Command phase.  Set up the DMA registers and let 'er rip.
44213177Sgibbs */
4434568Sgibbsp_command:
4444568Sgibbs	call	assert
4454568Sgibbs
44613177Sgibbs/*
44715328Sgibbs * Load HADDR and HCNT.
44813177Sgibbs */
44913690Sgibbs	mov	HADDR0, SCB_CMDPTR0
45013690Sgibbs	mov	HADDR1, SCB_CMDPTR1
45113690Sgibbs	mov	HADDR2, SCB_CMDPTR2
45213690Sgibbs	mov	HADDR3, SCB_CMDPTR3
45313690Sgibbs	mov	HCNT0, SCB_CMDLEN
45413690Sgibbs	clr	HCNT1
45513690Sgibbs	clr	HCNT2
4564568Sgibbs
45713690Sgibbs	mov	STCNT0, HCNT0
45813690Sgibbs	mov	STCNT1, HCNT1
45913690Sgibbs	mov	STCNT2, HCNT2
4604568Sgibbs
4614568Sgibbs	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
4624568Sgibbs						#   DIRECTION|FIFORESET
4634568Sgibbs	jmp	ITloop
4644568Sgibbs
46513177Sgibbs/*
46613177Sgibbs * Status phase.  Wait for the data byte to appear, then read it
46713177Sgibbs * and store it into the SCB.
46813177Sgibbs */
4694568Sgibbsp_status:
47013177Sgibbs	mvi	SCB_TARGET_STATUS	call inb_first
4719954Sgibbs	jmp	mesgin_done
4724568Sgibbs
47313177Sgibbs/*
47415328Sgibbs * Message out phase.  If there is not an active message, but the target
47513177Sgibbs * took us into this phase anyway, build a no-op message and send it.
47613177Sgibbs */
4774568Sgibbsp_mesgout:
47813177Sgibbs	test	MSG_LEN, 0xff	jnz  p_mesgout_start
47913177Sgibbs	mvi	MSG_NOP		call mk_mesg	/* build NOP message */
4804568Sgibbs
48113177Sgibbsp_mesgout_start:
48213177Sgibbs/*
48313177Sgibbs * Set up automatic PIO transfer from MSG0.  Bit 3 in
48413177Sgibbs * SXFRCTL0 (SPIOEN) is already on.
48513177Sgibbs */
48613177Sgibbs	mvi	SINDEX,MSG0
4874568Sgibbs	mov	DINDEX,MSG_LEN
4884568Sgibbs
48913177Sgibbs/*
49013177Sgibbs * When target asks for a byte, drop ATN if it's the last one in
49113177Sgibbs * the message.  Otherwise, keep going until the message is exhausted.
49213177Sgibbs *
49313177Sgibbs * Keep an eye out for a phase change, in case the target issues
49413177Sgibbs * a MESSAGE REJECT.
49513177Sgibbs */
49613177Sgibbsp_mesgout_loop:
49713313Sgibbs	test	SSTAT1,PHASEMIS	jnz p_mesgout_phasemis
49813177Sgibbs	test	SSTAT0,SPIORDY	jz p_mesgout_loop
49913177Sgibbs	cmp	DINDEX,1	jne p_mesgout_outb	/* last byte? */
50013177Sgibbs	mvi	CLRSINT1,CLRATNO			/* drop ATN */
50113177Sgibbsp_mesgout_outb:
50213177Sgibbs	dec	DINDEX
50313386Sgibbs	or	CLRSINT0, CLRSPIORDY
5044568Sgibbs	mov	SCSIDATL,SINDIR
50513313Sgibbs	
5064568Sgibbsp_mesgout4:
50713177Sgibbs	test	DINDEX,0xff	jnz p_mesgout_loop
5084568Sgibbs
50913177Sgibbs/*
51013177Sgibbs * If the next bus phase after ATN drops is a message out, it means
51113177Sgibbs * that the target is requesting that the last message(s) be resent.
51213177Sgibbs */
51313177Sgibbsp_mesgout_snoop:
51413177Sgibbs	test	SSTAT1,BUSFREE	jnz p_mesgout_done
51513177Sgibbs	test	SSTAT1,REQINIT	jz p_mesgout_snoop
5164568Sgibbs
51713177Sgibbs	test	SSTAT1,PHASEMIS	jnz p_mesgout_done
5184568Sgibbs
51913690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
52013177Sgibbs
5214568Sgibbs	jmp	ITloop
5224568Sgibbs
52313177Sgibbsp_mesgout_phasemis:
52415328Sgibbs	mvi	CLRSINT1,CLRATNO	/* Be sure to turn ATNO off */
52513177Sgibbsp_mesgout_done:
52613177Sgibbs	clr	MSG_LEN			/* no active msg */
5274568Sgibbs	jmp	ITloop
5284568Sgibbs
52913177Sgibbs/*
53013177Sgibbs * Message in phase.  Bytes are read using Automatic PIO mode.
53113177Sgibbs */
5324568Sgibbsp_mesgin:
53313177Sgibbs	mvi	A		call inb_first	/* read the 1st message byte */
53413177Sgibbs	mov	REJBYTE,A			/* save it for the driver */
5354568Sgibbs
53613177Sgibbs	test	A,MSG_IDENTIFY		jnz mesgin_identify
53713177Sgibbs	cmp	A,MSG_DISCONNECT	je mesgin_disconnect
53813177Sgibbs	cmp	A,MSG_SDPTRS		je mesgin_sdptrs
53913177Sgibbs	cmp	ALLZEROS,A		je mesgin_complete
54013177Sgibbs	cmp	A,MSG_RDPTRS		je mesgin_rdptrs
54113177Sgibbs	cmp	A,MSG_EXTENDED		je mesgin_extended
54213177Sgibbs	cmp	A,MSG_REJECT		je mesgin_reject
5434568Sgibbs
5449954Sgibbsrej_mesgin:
54513177Sgibbs/*
54613177Sgibbs * We have no idea what this message in is, and there's no way
54713177Sgibbs * to pass it up to the kernel, so we issue a message reject and
54813177Sgibbs * hope for the best.  Since we're now using manual PIO mode to
54913177Sgibbs * read in the message, there should no longer be a race condition
55013177Sgibbs * present when we assert ATN.  In any case, rejection should be a
55113177Sgibbs * rare occurrence - signal the driver when it happens.
55213177Sgibbs */
55313690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
55413177Sgibbs	mvi	INTSTAT,SEND_REJECT		/* let driver know */
5559954Sgibbs
55613177Sgibbs	mvi	MSG_REJECT	call mk_mesg
5579954Sgibbs
5589954Sgibbsmesgin_done:
55913177Sgibbs	call	inb_last			/*ack & turn auto PIO back on*/
5609954Sgibbs	jmp	ITloop
5619954Sgibbs
5629954Sgibbs
5639954Sgibbsmesgin_complete:
56413177Sgibbs/*
56515328Sgibbs * We got a "command complete" message, so put the SCB_TAG into QUEUEOUT,
56615328Sgibbs * and trigger a completion interrupt.  Check status for non zero return
56715328Sgibbs * and interrupt driver if needed.  This allows the driver to interpret
56815328Sgibbs * errors only when they occur instead of always uploading the scb.  If
56915328Sgibbs * the status is SCSI_CHECK, the driver will download a new scb requesting
57015328Sgibbs * sense to replace the old one, modify the "waiting for selection" SCB list
57115328Sgibbs * and set RETURN_1 to SEND_SENSE.  If RETURN_1 is set to SEND_SENSE the
57215328Sgibbs * sequencer imediately jumps to main loop where it will run down the waiting
57315328Sgibbs * SCB list and process the sense request.  If the kernel driver does not
57415328Sgibbs * wish to request sense, it need only clear RETURN_1, and the command is
57515328Sgibbs * allowed to complete.  We don't bother to post to the QOUTFIFO in the
57615328Sgibbs * error case since it would require extra work in the kernel driver to
57715328Sgibbs * ensure that the entry was removed before the command complete code tried
57815328Sgibbs * processing it.
57913177Sgibbs *
58013177Sgibbs * First check for residuals
58113177Sgibbs */
58213177Sgibbs	test	SCB_RESID_SGCNT,0xff	jz check_status
58313177Sgibbs/*
58413177Sgibbs * If we have a residual count, interrupt and tell the host.  Other
58513177Sgibbs * alternatives are to pause the sequencer on all command completes (yuck),
58613177Sgibbs * dma the resid directly to the host (slick, we may have space to do it now)
58713177Sgibbs * or have the sequencer pause itself when it encounters a non-zero resid 
58813177Sgibbs * (unecessary pause just to flag the command -yuck-, but takes one instruction
58913177Sgibbs * and since it shouldn't happen that often is good enough for our purposes).  
59013177Sgibbs */
59113177Sgibbsresid:
59213177Sgibbs	mvi	INTSTAT,RESIDUAL
5934568Sgibbs
5947532Sgibbscheck_status:
59513177Sgibbs	test	SCB_TARGET_STATUS,0xff	jz status_ok	/* Good Status? */
59613177Sgibbs	mvi	INTSTAT,BAD_STATUS			/* let driver know */
59713177Sgibbs	cmp	RETURN_1, SEND_SENSE	jne status_ok
5989954Sgibbs	jmp	mesgin_done
5995326Sgibbs
6004568Sgibbsstatus_ok:
60113177Sgibbs/* First, mark this target as free. */
60213177Sgibbs	test	SCB_CONTROL,TAG_ENB jnz test_immediate	/*
60313177Sgibbs							 * Tagged commands
60413177Sgibbs							 * don't busy the
60513177Sgibbs							 * target.
60613177Sgibbs							 */
60713177Sgibbs	mov	FUNCTION1,SCB_TCL
6085326Sgibbs	mov	A,FUNCTION1
60913177Sgibbs	test	SCB_TCL,0x88 jz clear_a
6105326Sgibbs	xor	ACTIVE_B,A
61113177Sgibbs	jmp	test_immediate
6125326Sgibbs
6135326Sgibbsclear_a:
6145326Sgibbs	xor	ACTIVE_A,A
6155326Sgibbs
61613177Sgibbstest_immediate:
61713177Sgibbs	test    SCB_CMDLEN,0xff jnz complete  /* Immediate message complete */
61813177Sgibbs/*
61913177Sgibbs * Pause the sequencer until the driver gets around to handling the command
62013177Sgibbs * complete.  This is so that any action that might require carefull timing
62113177Sgibbs * with the completion of this command can occur.
62213177Sgibbs */
6239810Sgibbs	mvi	INTSTAT,IMMEDDONE
62413177Sgibbs	jmp	start
6255326Sgibbscomplete:
62615328Sgibbs	mov	QOUTFIFO,SCB_TAG
6277532Sgibbs	mvi	INTSTAT,CMDCMPLT
6289954Sgibbs	jmp	mesgin_done
6294568Sgibbs
6307532Sgibbs
63113177Sgibbs/*
63213177Sgibbs * Is it an extended message?  We only support the synchronous and wide data
63313177Sgibbs * transfer request messages, which will probably be in response to
63413177Sgibbs * WDTR or SDTR message outs from us.  If it's not SDTR or WDTR, reject it -
63513177Sgibbs * apparently this can be done after any message in byte, according
63613177Sgibbs * to the SCSI-2 spec.
63713177Sgibbs */
6389954Sgibbsmesgin_extended:
63913177Sgibbs	mvi	ARG_1		call inb_next	/* extended message length */
64015328Sgibbs	mvi	REJBYTE_EXT	call inb_next	/* extended message code */
6414568Sgibbs
64215328Sgibbs	cmp	REJBYTE_EXT,MSG_SDTR	je p_mesginSDTR
64315328Sgibbs	cmp	REJBYTE_EXT,MSG_WDTR	je p_mesginWDTR
6449954Sgibbs	jmp	rej_mesgin
6455562Sgibbs
6465562Sgibbsp_mesginWDTR:
64713177Sgibbs	cmp	ARG_1,2		jne rej_mesgin	/* extended mesg length=2 */
64813177Sgibbs	mvi	ARG_1		call inb_next	/* Width of bus */
64913177Sgibbs	mvi	INTSTAT,WDTR_MSG		/* let driver know */
65013177Sgibbs	test	RETURN_1,0xff jz mesgin_done	/* Do we need to send WDTR? */
65113177Sgibbs	cmp	RETURN_1,SEND_REJ je rej_mesgin /*
65213177Sgibbs						 * Bus width was too large 
65313177Sgibbs						 * Reject it.
65413177Sgibbs						 */
6555562Sgibbs
65613177Sgibbs/* We didn't initiate the wide negotiation, so we must respond to the request */
65713177Sgibbs	and	RETURN_1,0x7f			/* Clear the SEND_WDTR Flag */
65813177Sgibbs	mvi	DINDEX,MSG0
65913177Sgibbs	mvi	MSG0	call mk_wdtr		/* build WDTR message */
66013690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
6619954Sgibbs	jmp	mesgin_done
6625562Sgibbs
6635562Sgibbsp_mesginSDTR:
66413177Sgibbs	cmp	ARG_1,3		jne rej_mesgin	/* extended mesg length=3 */
66513177Sgibbs	mvi	ARG_1		call inb_next	/* xfer period */
66613177Sgibbs	mvi	A		call inb_next	/* REQ/ACK offset */
66713177Sgibbs	mvi	INTSTAT,SDTR_MSG		/* call driver to convert */
6684568Sgibbs
66913177Sgibbs	test	RETURN_1,0xff	jz mesgin_done  /* Do we need to mk_sdtr/rej */
67013177Sgibbs	cmp	RETURN_1,SEND_REJ je rej_mesgin /*
67113177Sgibbs						 * Requested SDTR too small
67213177Sgibbs						 * Reject it.
67313177Sgibbs						 */
67415328Sgibbs	clr	ARG_1				/* Use the scratch ram rate */
67513177Sgibbs	mvi	DINDEX, MSG0
67613177Sgibbs	mvi     MSG0     call mk_sdtr
67713690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
6789954Sgibbs	jmp	mesgin_done
6794568Sgibbs
68013177Sgibbs/*
68113177Sgibbs * Is it a disconnect message?  Set a flag in the SCB to remind us
68213177Sgibbs * and await the bus going free.
68313177Sgibbs */
6849954Sgibbsmesgin_disconnect:
68513177Sgibbs	or	SCB_CONTROL,DISCONNECTED
68615328Sgibbs	test	FLAGS, PAGESCBS jz mesgin_done
68715328Sgibbs/*
68815328Sgibbs * Link this SCB into the DISCONNECTED list.  This list holds the
68915328Sgibbs * candidates for paging out an SCB if one is needed for a new command.
69015328Sgibbs * Modifying the disconnected list is a critical(pause dissabled) section.
69115328Sgibbs */
69215328Sgibbs	mvi	SCB_PREV, SCB_LIST_NULL
69315328Sgibbs	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
69415328Sgibbs	mov	SCB_NEXT, DISCONNECTED_SCBH
69515328Sgibbs	mov	DISCONNECTED_SCBH, SCBPTR
69615328Sgibbs	cmp	SCB_NEXT,SCB_LIST_NULL je linkdone
69715328Sgibbs	mov	SCBPTR,SCB_NEXT
69815328Sgibbs	mov	SCB_PREV,DISCONNECTED_SCBH
69915328Sgibbs	mov	SCBPTR,DISCONNECTED_SCBH
70015328Sgibbslinkdone:
70115328Sgibbs	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
7029954Sgibbs	jmp	mesgin_done
7034568Sgibbs
70413177Sgibbs/*
70513177Sgibbs * Save data pointers message?  Copy working values into the SCB,
70613177Sgibbs * usually in preparation for a disconnect.
70713177Sgibbs */
7089954Sgibbsmesgin_sdptrs:
7094568Sgibbs	call	sg_ram2scb
7109954Sgibbs	jmp	mesgin_done
7114568Sgibbs
71213177Sgibbs/*
71313177Sgibbs * Restore pointers message?  Data pointers are recopied from the
71413177Sgibbs * SCB anytime we enter a data phase for the first time, so all
71513177Sgibbs * we need to do is clear the DPHASE flag and let the data phase
71613177Sgibbs * code do the rest.
71713177Sgibbs */
7189954Sgibbsmesgin_rdptrs:
71915328Sgibbs	and	FLAGS,0xef			/*
72013177Sgibbs						 * !DPHASE we'll reload them
72113177Sgibbs						 * the next time through
72213177Sgibbs						 */
7239954Sgibbs	jmp	mesgin_done
7244568Sgibbs
72513177Sgibbs/*
72613177Sgibbs * Identify message?  For a reconnecting target, this tells us the lun
72713177Sgibbs * that the reconnection is for - find the correct SCB and switch to it,
72813177Sgibbs * clearing the "disconnected" bit so we don't "find" it by accident later.
72913177Sgibbs */
7309954Sgibbsmesgin_identify:
73113177Sgibbs	test	A,0x78	jnz rej_mesgin	/*!DiscPriv|!LUNTAR|!Reserved*/
7324568Sgibbs
73313177Sgibbs	and	A,0x07			/* lun in lower three bits */
7346608Sgibbs	or      SAVED_TCL,A,SELID          
7356608Sgibbs	and     SAVED_TCL,0xf7
73613177Sgibbs	and     A,SELBUSB,SBLKCTL	/* B Channel?? */
7376608Sgibbs	or      SAVED_TCL,A
73813177Sgibbs	call	inb_last		/* ACK */
73913177Sgibbs
74013177Sgibbs/*
74113177Sgibbs * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
74215328Sgibbs * If we get one, we use the tag returned to switch to find the proper
74315328Sgibbs * SCB.  With SCB paging, this requires using findSCB for both tagged
74415328Sgibbs * and non-tagged transactions since the SCB may exist in any slot.
74515328Sgibbs * If we're not using SCB paging, we can use the tag as the direct
74615328Sgibbs * index to the SCB.
74713177Sgibbs */
74815328Sgibbs	mvi	ARG_1,SCB_LIST_NULL	/* Default to no-tag */
74913177Sgibbssnoop_tag_loop:
75013177Sgibbs	test	SSTAT1,BUSFREE	jnz use_findSCB
75113177Sgibbs	test	SSTAT1,REQINIT	jz snoop_tag_loop
75213177Sgibbs	test	SSTAT1,PHASEMIS	jnz use_findSCB
75313177Sgibbs	mvi	A		call inb_first
75415328Sgibbs	cmp	A,MSG_SIMPLE_TAG jne use_findSCB
7556608Sgibbsget_tag:
75613177Sgibbs	mvi	ARG_1	call inb_next	/* tag value */
75713177Sgibbs/*
75813177Sgibbs * See if the tag is in range.  The tag is < SCBCOUNT if we add
75913177Sgibbs * the complement of SCBCOUNT to the incomming tag and there is
76013177Sgibbs * no carry.
76113177Sgibbs */
76213177Sgibbs	mov	A,COMP_SCBCOUNT	
76313177Sgibbs	add	SINDEX,A,ARG_1
76413177Sgibbs	jc	abort_tag
76513177Sgibbs
76613177Sgibbs/*
76715328Sgibbs * Ensure that the SCB the tag points to is for an SCB transaction
76813177Sgibbs * to the reconnecting target.
76913177Sgibbs */
77015328Sgibbs	test	FLAGS, PAGESCBS	jz index_by_tag
77115328Sgibbs	call	inb_last			/* Ack Tag */
77215328Sgibbsuse_findSCB:
77315328Sgibbs	mov	ALLZEROS	call findSCB	  /* Have to search */
77415328Sgibbssetup_SCB:
77515328Sgibbs	and	SCB_CONTROL,0xfb	  /* clear disconnect bit in SCB */
77615328Sgibbs	or	FLAGS,IDENTIFY_SEEN	  /* make note of IDENTIFY */
77715328Sgibbs	jmp	ITloop
77815328Sgibbsindex_by_tag:
77913177Sgibbs	mov	SCBPTR,ARG_1
7806608Sgibbs	mov	A,SAVED_TCL
78113177Sgibbs	cmp	SCB_TCL,A		jne abort_tag
78213177Sgibbs	test	SCB_CONTROL,TAG_ENB	jz  abort_tag
78313177Sgibbs	call	inb_last			/* Ack Successful tag */
78413177Sgibbs	jmp	setup_SCB
78515328Sgibbs
7867700Sgibbsabort_tag:
78713690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
78813177Sgibbs	mvi	INTSTAT,ABORT_TAG 		/* let driver know */
78915328Sgibbs	mvi	MSG_ABORT_TAG	call mk_mesg	/* ABORT TAG message */
79013177Sgibbs	jmp	mesgin_done
7916608Sgibbs
79213177Sgibbs/*
79313177Sgibbs * Message reject?  Let the kernel driver handle this.  If we have an 
79413177Sgibbs * outstanding WDTR or SDTR negotiation, assume that it's a response from 
79513177Sgibbs * the target selecting 8bit or asynchronous transfer, otherwise just ignore 
79613177Sgibbs * it since we have no clue what it pertains to.
79713177Sgibbs */
7989954Sgibbsmesgin_reject:
79913177Sgibbs	mvi	INTSTAT, REJECT_MSG
8009954Sgibbs	jmp	mesgin_done
8015562Sgibbs
80213177Sgibbs/*
80313177Sgibbs * [ ADD MORE MESSAGE HANDLING HERE ]
80413177Sgibbs */
8054568Sgibbs
80613177Sgibbs/*
80713177Sgibbs * Bus free phase.  It might be useful to interrupt the device
80813177Sgibbs * driver if we aren't expecting this.  For now, make sure that
80913177Sgibbs * ATN isn't being asserted and look for a new command.
81013177Sgibbs */
8114568Sgibbsp_busfree:
81213177Sgibbs	mvi	CLRSINT1,CLRATNO
81314449Sgibbs	clr	LASTPHASE
8148567Sdg
81513177Sgibbs/*
81613177Sgibbs * if this is an immediate command, perform a psuedo command complete to
81713177Sgibbs * notify the driver.
81813177Sgibbs */
81913177Sgibbs	test	SCB_CMDLEN,0xff	jz status_ok
82013177Sgibbs	jmp	start
8214568Sgibbs
82213177Sgibbs/*
82313177Sgibbs * Locking the driver out, build a one-byte message passed in SINDEX
82413177Sgibbs * if there is no active message already.  SINDEX is returned intact.
82513177Sgibbs */
8264568Sgibbsmk_mesg:
82713177Sgibbs	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
82813177Sgibbs	test	MSG_LEN,0xff	jz mk_mesg1	/* Should always succeed */
82913177Sgibbs	
83013177Sgibbs	/*
83113177Sgibbs	 * Hmmm.  For some reason the mesg buffer is in use.
83213177Sgibbs	 * Tell the driver.  It should look at SINDEX to find
83313177Sgibbs	 * out what we wanted to use the buffer for and resolve
83413177Sgibbs	 * the conflict.
83513177Sgibbs	 */
83613177Sgibbs	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
83713690Sgibbs	mvi	INTSTAT,MSG_BUFFER_BUSY
8384568Sgibbs
8394568Sgibbsmk_mesg1:
84013177Sgibbs	mvi	MSG_LEN,1		/* length = 1 */
84113177Sgibbs	mov	MSG0,SINDEX		/* 1-byte message */
84213177Sgibbs	mvi	SEQCTL,0x10	ret	/* !PAUSEDIS|FASTMODE */
8434568Sgibbs
84413177Sgibbs/*
84513177Sgibbs * Functions to read data in Automatic PIO mode.
84613177Sgibbs *
84713177Sgibbs * According to Adaptec's documentation, an ACK is not sent on input from
84813177Sgibbs * the target until SCSIDATL is read from.  So we wait until SCSIDATL is
84913177Sgibbs * latched (the usual way), then read the data byte directly off the bus
85013177Sgibbs * using SCSIBUSL.  When we have pulled the ATN line, or we just want to
85113177Sgibbs * acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
85213177Sgibbs * spec guarantees that the target will hold the data byte on the bus until
85313177Sgibbs * we send our ACK.
85413177Sgibbs *
85513177Sgibbs * The assumption here is that these are called in a particular sequence,
85613177Sgibbs * and that REQ is already set when inb_first is called.  inb_{first,next}
85713177Sgibbs * use the same calling convention as inb.
85813177Sgibbs */
85913177Sgibbs
86013177Sgibbsinb_next:
86113386Sgibbs	or	CLRSINT0, CLRSPIORDY
86213360Sgibbs	mov	NONE,SCSIDATL			/*dummy read from latch to ACK*/
86313360Sgibbsinb_next_wait:
86413360Sgibbs	test	SSTAT1,PHASEMIS	jnz mesgin_phasemis
86513360Sgibbs	test	SSTAT0,SPIORDY	jz inb_next_wait /* wait for next byte */
86613313Sgibbsinb_first:
8674568Sgibbs	mov	DINDEX,SINDEX
86813177Sgibbs	mov	DINDIR,SCSIBUSL	ret		/*read byte directly from bus*/
86913177Sgibbsinb_last:
87013360Sgibbs	mov	NONE,SCSIDATL ret		/*dummy read from latch to ACK*/
8714568Sgibbs
87213177Sgibbsmesgin_phasemis:
87313177Sgibbs/*
87413177Sgibbs * We expected to receive another byte, but the target changed phase
87513177Sgibbs */
87613177Sgibbs	mvi	INTSTAT, MSGIN_PHASEMIS
87713177Sgibbs	jmp	ITloop
8784568Sgibbs
87913177Sgibbs/*
88013177Sgibbs * DMA data transfer.  HADDR and HCNT must be loaded first, and
88113177Sgibbs * SINDEX should contain the value to load DFCNTRL with - 0x3d for
88213177Sgibbs * host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
88313177Sgibbs * during initialization.
88413177Sgibbs */
8854568Sgibbsdma:
8864568Sgibbs	mov	DFCNTRL,SINDEX
8874568Sgibbsdma1:
88813177Sgibbs	test	SSTAT0,DMADONE	jnz dma3
88913177Sgibbs	test	SSTAT1,PHASEMIS	jz dma1		/* ie. underrun */
8904568Sgibbs
89113177Sgibbs/*
89213177Sgibbs * We will be "done" DMAing when the transfer count goes to zero, or
89313177Sgibbs * the target changes the phase (in light of this, it makes sense that
89413177Sgibbs * the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
89513177Sgibbs * doing a SCSI->Host transfer, the data FIFO should be flushed auto-
89613177Sgibbs * magically on STCNT=0 or a phase change, so just wait for FIFO empty
89713177Sgibbs * status.
89813177Sgibbs */
8994568Sgibbsdma3:
90013177Sgibbs	test	SINDEX,DIRECTION	jnz dma5
9014568Sgibbsdma4:
90213177Sgibbs	test	DFSTATUS,FIFOEMP	jz dma4
9034568Sgibbs
90413177Sgibbs/*
90513177Sgibbs * Now shut the DMA enables off and make sure that the DMA enables are 
90613177Sgibbs * actually off first lest we get an ILLSADDR.
90713177Sgibbs */
9084568Sgibbsdma5:
90913177Sgibbs	/* disable DMA, but maintain WIDEODD */
91013690Sgibbs	and	DFCNTRL,WIDEODD
9114568Sgibbsdma6:
91213177Sgibbs	test	DFCNTRL,0x38	jnz dma6  /* SCSIENACK|SDMAENACK|HDMAENACK */
9134568Sgibbs
9144568Sgibbs	ret
9154568Sgibbs
91613177Sgibbs/*
91713177Sgibbs * Common SCSI initialization for selection and reselection.  Expects
91813177Sgibbs * the target SCSI ID to be in the upper four bits of SINDEX, and A's
91913177Sgibbs * contents are stomped on return.
92013177Sgibbs */
9218104Sgibbsinitialize_scsiid:
92213177Sgibbs	and	SINDEX,0xf0		/* Get target ID */
9235775Sgibbs	and	A,0x0f,SCSIID
9245775Sgibbs	or	SINDEX,A
9258104Sgibbs	mov	SCSIID,SINDEX ret
9265326Sgibbs
92713177Sgibbs/*
92813177Sgibbs * Assert that if we've been reselected, then we've seen an IDENTIFY
92913177Sgibbs * message.
93013177Sgibbs */
9314568Sgibbsassert:
93213177Sgibbs	test	FLAGS,RESELECTED	jz return	/* reselected? */
93313177Sgibbs	test	FLAGS,IDENTIFY_SEEN	jnz return	/* seen IDENTIFY? */
9344568Sgibbs
93513177Sgibbs	mvi	INTSTAT,NO_IDENT 	ret	/* no - cause a kernel panic */
9364568Sgibbs
93713177Sgibbs/*
93815328Sgibbs * Locate the SCB matching the target ID/channel/lun in SAVED_TCL, and the tag
93915328Sgibbs * value in ARG_1.  If ARG_1 == SCB_LIST_NULL, we're looking for a non-tagged
94015328Sgibbs * SCB.  Have the kernel print a warning message if it can't be found, and
94115328Sgibbs * generate an ABORT/ABORT_TAG message to the target.  SINDEX should be
94213177Sgibbs * cleared on call.
94313177Sgibbs */
9444568SgibbsfindSCB:
9456608Sgibbs	mov	A,SAVED_TCL
94615328Sgibbs	mov	SCBPTR,SINDEX			/* switch to next SCB */
94715328Sgibbs	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
94813177Sgibbs	cmp	SCB_TCL,A	jne findSCB1 /* target ID/channel/lun match? */
94913177Sgibbs	test	SCB_CONTROL,DISCONNECTED jz findSCB1 /*should be disconnected*/
95015328Sgibbs	test	SCB_CONTROL,TAG_ENB jnz findTaggedSCB
95115328Sgibbs	cmp	ARG_1,SCB_LIST_NULL je foundSCB
95215328Sgibbs	jmp	findSCB1
95315328SgibbsfindTaggedSCB:
95415328Sgibbs	mov	A, ARG_1			/* Tag passed in ARG_1 */
95515328Sgibbs	cmp	SCB_TAG,A	jne findSCB1	/* Found it? */
95615328SgibbsfoundSCB:
95715328Sgibbs	test	FLAGS,PAGESCBS	jz foundSCB_ret
95815328Sgibbs/* Remove this SCB from the disconnection list */
95915328Sgibbs	cmp	SCB_NEXT,SCB_LIST_NULL je unlink_prev
96015328Sgibbs	mov	SAVED_LINKPTR, SCB_PREV
96115328Sgibbs	mov	SCBPTR, SCB_NEXT
96215328Sgibbs	mov	SCB_PREV, SAVED_LINKPTR
96315328Sgibbs	mov	SCBPTR, SINDEX
96415328Sgibbsunlink_prev:
96515328Sgibbs	cmp	SCB_PREV,SCB_LIST_NULL	je rHead/* At the head of the list */
96615328Sgibbs	mov	SAVED_LINKPTR, SCB_NEXT
96715328Sgibbs	mov	SCBPTR, SCB_PREV
96815328Sgibbs	mov	SCB_NEXT, SAVED_LINKPTR
96915328Sgibbs	mov	SCBPTR, SINDEX
97015328Sgibbs	mvi	SEQCTL,0x10	ret		/* !PAUSEDIS|FASTMODE */
97115328SgibbsrHead:
97215328Sgibbs	mov	DISCONNECTED_SCBH,SCB_NEXT
97315328SgibbsfoundSCB_ret:
97415328Sgibbs	mvi	SEQCTL,0x10	ret		/* !PAUSEDIS|FASTMODE */
9754568Sgibbs
9766608SgibbsfindSCB1:
97715328Sgibbs	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
9784568Sgibbs	inc	SINDEX
9794568Sgibbs	mov	A,SCBCOUNT
9806608Sgibbs	cmp	SINDEX,A	jne findSCB
9814568Sgibbs
98213177Sgibbs	mvi	INTSTAT,NO_MATCH		/* not found - signal kernel */
98315328Sgibbs	cmp	RETURN_1,SCB_PAGEDIN je return
98415328Sgibbs	or	SCSISIGO,ATNO			/* assert ATNO */
98515328Sgibbs	cmp	ARG_1,SCB_LIST_NULL jne find_abort_tag
98615328Sgibbs	mvi	MSG_ABORT	call mk_mesg
98715328Sgibbs	jmp	ITloop
98815328Sgibbsfind_abort_tag:
98915328Sgibbs	mvi	MSG_ABORT_TAG	call mk_mesg
99015328Sgibbs	jmp	ITloop
9914568Sgibbs
99213177Sgibbs/*
99313177Sgibbs * Make a working copy of the scatter-gather parameters from the SCB.
99413177Sgibbs */
9954568Sgibbssg_scb2ram:
99613690Sgibbs	mov	HADDR0, SCB_DATAPTR0
99713690Sgibbs	mov	HADDR1, SCB_DATAPTR1
99813690Sgibbs	mov	HADDR2, SCB_DATAPTR2
99913690Sgibbs	mov	HADDR3, SCB_DATAPTR3
100013690Sgibbs	mov	HCNT0, SCB_DATACNT0
100113690Sgibbs	mov	HCNT1, SCB_DATACNT1
100213690Sgibbs	mov	HCNT2, SCB_DATACNT2
10039928Sgibbs
100413690Sgibbs	mov	STCNT0, HCNT0
100513690Sgibbs	mov	STCNT1, HCNT1
100613690Sgibbs	mov	STCNT2, HCNT2
10079928Sgibbs
100813177Sgibbs	mov	SG_COUNT,SCB_SGCOUNT
10094568Sgibbs
101013690Sgibbs	mov	SG_NEXT0, SCB_SGPTR0
101113690Sgibbs	mov	SG_NEXT1, SCB_SGPTR1
101213690Sgibbs	mov	SG_NEXT2, SCB_SGPTR2
101313690Sgibbs	mov	SG_NEXT3, SCB_SGPTR3 ret
10144568Sgibbs
101513177Sgibbs/*
101613177Sgibbs * Copying RAM values back to SCB, for Save Data Pointers message, but
101713177Sgibbs * only if we've actually been into a data phase to change them.  This
101813177Sgibbs * protects against bogus data in scratch ram and the residual counts
101913177Sgibbs * since they are only initialized when we go into data_in or data_out.
102013177Sgibbs */
10214568Sgibbssg_ram2scb:
10229928Sgibbs	test	FLAGS, DPHASE	jz return
102313177Sgibbs	mov	SCB_SGCOUNT,SG_COUNT
10244568Sgibbs
102513690Sgibbs	mov	SCB_SGPTR0,SG_NEXT0
102613690Sgibbs	mov	SCB_SGPTR1,SG_NEXT1
102713690Sgibbs	mov	SCB_SGPTR2,SG_NEXT2
102813690Sgibbs	mov	SCB_SGPTR3,SG_NEXT3
10299928Sgibbs	
103013690Sgibbs	mov	SCB_DATAPTR0,SHADDR0
103113690Sgibbs	mov	SCB_DATAPTR1,SHADDR1
103213690Sgibbs	mov	SCB_DATAPTR2,SHADDR2
103313690Sgibbs	mov	SCB_DATAPTR3,SHADDR3
10344568Sgibbs
103513177Sgibbs/*
103613177Sgibbs * Use the residual number since STCNT is corrupted by any message transfer
103713177Sgibbs */
103813690Sgibbs	mov	SCB_DATACNT0,SCB_RESID_DCNT0
103913690Sgibbs	mov	SCB_DATACNT1,SCB_RESID_DCNT1
104013690Sgibbs	mov	SCB_DATACNT2,SCB_RESID_DCNT2 ret
10414568Sgibbs
104213177Sgibbs/*
104313177Sgibbs * Add the array base TARG_SCRATCH to the target offset (the target address
104413177Sgibbs * is in SCSIID), and return the result in SINDEX.  The accumulator
104513177Sgibbs * contains the 3->8 decoding of the target ID on return.
104613177Sgibbs */
10475562Sgibbsndx_dtr:
10484568Sgibbs	shr	A,SCSIID,4
104913177Sgibbs	test	SBLKCTL,SELBUSB	jz ndx_dtr_2
105013177Sgibbs	or	A,0x08		/* Channel B entries add 8 */
10515562Sgibbsndx_dtr_2:
105213690Sgibbs	add	SINDEX,TARG_SCRATCH,A ret
10534568Sgibbs
105413177Sgibbs/*
105513177Sgibbs * If we need to negotiate transfer parameters, build the WDTR or SDTR message
105613177Sgibbs * starting at the address passed in SINDEX.  DINDEX is modified on return.
105713177Sgibbs * The SCSI-II spec requires that Wide negotiation occur first and you can
105813177Sgibbs * only negotiat one or the other at a time otherwise in the event of a message
105913177Sgibbs * reject, you wouldn't be able to tell which message was the culpret.
106013177Sgibbs */
10615562Sgibbsmk_dtr:
106213177Sgibbs	test	SCB_CONTROL,NEEDWDTR jnz  mk_wdtr_16bit
106315328Sgibbs	mvi	ARG_1, MAXOFFSET	/* Force an offset of 15 or 8 if WIDE */
10644568Sgibbs
10657532Sgibbsmk_sdtr:
106613177Sgibbs	mvi	DINDIR,1		/* extended message */
106713177Sgibbs	mvi	DINDIR,3		/* extended message length = 3 */
106813177Sgibbs	mvi	DINDIR,1		/* SDTR code */
10697532Sgibbs	call	sdtr_to_rate
107013177Sgibbs	mov	DINDIR,RETURN_1		/* REQ/ACK transfer period */
107115328Sgibbs	cmp	ARG_1, MAXOFFSET je mk_sdtr_max_offset
107213177Sgibbs	and	DINDIR,0x0f,SINDIR	/* Sync Offset */
10737532Sgibbs
10747532Sgibbsmk_sdtr_done:
107513177Sgibbs	add	MSG_LEN,COMP_MSG0,DINDEX ret	/* update message length */
10767532Sgibbs
10778567Sdgmk_sdtr_max_offset:
107813177Sgibbs/*
107913177Sgibbs * We're initiating sync negotiation, so request the max offset we can (15 or 8)
108013177Sgibbs */
108113177Sgibbs	/* Talking to a WIDE device? */
108213177Sgibbs	test	SCSIRATE, WIDEXFER	jnz wmax_offset	
10838567Sdg	mvi	DINDIR, MAX_OFFSET_8BIT
10847532Sgibbs	jmp	mk_sdtr_done
10857532Sgibbs
10868567Sdgwmax_offset:
108713177Sgibbs	mvi	DINDIR, MAX_OFFSET_16BIT
10888567Sdg	jmp	mk_sdtr_done
10898567Sdg
10905647Sgibbsmk_wdtr_16bit:
10915647Sgibbs	mvi	ARG_1,BUS_16_BIT
10925562Sgibbsmk_wdtr:
109313177Sgibbs	mvi	DINDIR,1		/* extended message */
109413177Sgibbs	mvi	DINDIR,2		/* extended message length = 2 */
109513177Sgibbs	mvi	DINDIR,3		/* WDTR code */
109613177Sgibbs	mov	DINDIR,ARG_1		/* bus width */
10975562Sgibbs
109813177Sgibbs	add	MSG_LEN,COMP_MSG0,DINDEX ret	/* update message length */
10995562Sgibbs	
11005562Sgibbssdtr_to_rate:
110113177Sgibbs	call	ndx_dtr			/* index scratch space for target */
11025562Sgibbs	shr	A,SINDIR,0x4
110313177Sgibbs	dec	SINDEX			/* Preserve SINDEX */
11045562Sgibbs	and	A,0x7
11055562Sgibbs	clr	RETURN_1
11065562Sgibbssdtr_to_rate_loop:
11075562Sgibbs	test	A,0x0f	jz sdtr_to_rate_done
110813177Sgibbs	add	RETURN_1,0x19
11095562Sgibbs	dec	A	
11105562Sgibbs	jmp	sdtr_to_rate_loop
11115562Sgibbssdtr_to_rate_done:
11125562Sgibbs	shr	RETURN_1,0x2
111313177Sgibbs	add	RETURN_1,0x19
111411829Sgibbs	test	SXFRCTL0,ULTRAEN jz return
111511829Sgibbs	shr	RETURN_1,0x1
11167532Sgibbsreturn:
11177532Sgibbs	ret
1118