aic7xxx.seq revision 13313
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 *
813177Sgibbs *Modifications/enhancements:
913177Sgibbs *  Copyright (c) 1994, 1995 Justin Gibbs. All rights reserved.
1013177Sgibbs *
1113177Sgibbs *Redistribution and use in source and binary forms, with or without
1213177Sgibbs *modification, are permitted provided that the following conditions
1313177Sgibbs *are met:
1413177Sgibbs *1. Redistributions of source code must retain the above copyright
1513177Sgibbs *   notice, this list of conditions, and the following disclaimer.
1613177Sgibbs *2. Redistributions in binary form must reproduce the above copyright
1713177Sgibbs *   notice, this list of conditions and the following disclaimer in the
1813177Sgibbs *   documentation and/or other materials provided with the distribution.
1913177Sgibbs *3. All advertising materials mentioning features or use of this software
2013177Sgibbs *   must display the following acknowledgement:
2113177Sgibbs *     This product includes software developed by the University of Calgary
2213177Sgibbs *     Department of Computer Science and its contributors.
2313177Sgibbs *4. Neither the name of the University nor the names of its contributors
2413177Sgibbs *   may be used to endorse or promote products derived from this software
2513177Sgibbs *   without specific prior written permission.
2613177Sgibbs *
2713177Sgibbs *THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2813177Sgibbs *ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2913177Sgibbs *IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3013177Sgibbs *ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3113177Sgibbs *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3213177Sgibbs *DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3313177Sgibbs *OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3413177Sgibbs *HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3513177Sgibbs *LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3613177Sgibbs *OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3713177Sgibbs *SUCH DAMAGE.
3813177Sgibbs *
3913177Sgibbs *FreeBSD, Twin, Wide, 2 command per target support, tagged queuing and other 
4013177Sgibbs *optimizations provided by Justin T. Gibbs (gibbs@FreeBSD.org)
4113177Sgibbs *
4213177Sgibbs *-M************************************************************************/
434568Sgibbs
4413313SgibbsVERSION AIC7XXX_SEQ_VER "$Id: aic7xxx.seq,v 1.26 1996/01/05 16:11:49 gibbs Exp $"
455647Sgibbs
4613177Sgibbs#include "../../dev/aic7xxx/aic7xxx_reg.h"
474568Sgibbs
4813177Sgibbs/*
4913177Sgibbs * We can't just use ACCUM in the sequencer code because it
5013177Sgibbs * must be treated specially by the assembler, and it currently
5113177Sgibbs * looks for the symbol 'A'.  This is the only register defined
5213177Sgibbs * the assembler's symbol space.
5313177Sgibbs */
5413177SgibbsA = ACCUM
554568Sgibbs
5613177Sgibbs/* After starting the selection hardware, we return to the "poll_for_work"
5713177Sgibbs * loop so that we can check for reconnecting targets as well as for our
5813177Sgibbs * selection to complete just in case the reselection wins bus arbitration.
5913177Sgibbs * The problem with this is that we must keep track of the SCB that we've
6013177Sgibbs * already pulled from the QINFIFO and started the selection on just in case
6113177Sgibbs * the reselection wins so that we can retry the selection at a later time.
6213177Sgibbs * This problem cannot be resolved by holding a single entry in scratch
6313177Sgibbs * ram since a reconnecting target can request sense and this will create
6413177Sgibbs * yet another SCB waiting for selection.  The solution used here is to 
6513177Sgibbs * use byte 31 of the SCB as a psuedo-next pointer and to thread a list
6613177Sgibbs * of SCBs that are awaiting selection.  Since 0-0xfe are valid SCB offsets, 
6713177Sgibbs * SCB_LIST_NULL is 0xff which is out of range.  The kernel driver must
6813177Sgibbs * add an entry to this list everytime a request sense occurs.  The sequencer
6913177Sgibbs * will automatically consume the entries.
7013177Sgibbs */
714568Sgibbs
7213177Sgibbs/*
7313177Sgibbs * Initialize any state idle loop state here.  This code is executed on
7413177Sgibbs * startup and after every bus free.
7513177Sgibbs */
7613177Sgibbsstart:
7713177Sgibbs	mvi	SCSISEQ,ENRSELI		/* Always allow reselection */
788104Sgibbspoll_for_work:
7913177Sgibbs	/*
8013177Sgibbs	 * Are we a twin channel device?
8113177Sgibbs	 * For fairness, we check the other bus first,
8213177Sgibbs	 * since we just finished a transaction on the
8313177Sgibbs	 * current channel.
8413177Sgibbs	 */
8513177Sgibbs	test	FLAGS,TWIN_BUS	jz start2
8613177Sgibbs	xor	SBLKCTL,SELBUSB			/* Toggle to the other bus */
878104Sgibbs	test	SSTAT0,SELDI	jnz reselect
8813177Sgibbs	xor	SBLKCTL,SELBUSB			/* Toggle to the original bus */
895326Sgibbsstart2:
908104Sgibbs	test	SSTAT0,SELDI	jnz reselect
919395Sgibbs	cmp	WAITING_SCBH,SCB_LIST_NULL jne start_waiting
9213177Sgibbs	test	QINCNT,0xff	jz poll_for_work
934568Sgibbs
9413177Sgibbs/* We have at least one queued SCB now and we don't have any 
9513177Sgibbs * SCBs in the list of SCBs awaiting selection.  Set the SCB
9613177Sgibbs * pointer from the FIFO so we see the right bank of SCB 
9713177Sgibbs * registers.
9813177Sgibbs */
994568Sgibbs	mov	SCBPTR,QINFIFO
1004568Sgibbs
10113177Sgibbs/*
10213177Sgibbs * If the control byte of this SCB has the NEEDDMA flag set, we have
10313177Sgibbs * yet to DMA it from host memory
10413177Sgibbs */
1055647Sgibbs
10613177Sgibbstest    SCB_CONTROL,NEEDDMA	jz test_busy    
10713177Sgibbs	clr	HCNT2
10813177Sgibbs	clr	HCNT1
10913177Sgibbs	mvi	HCNT0,SCB_SIZEOF
1105647Sgibbs
1118104Sgibbs	mvi	DINDEX,HADDR      
11213177Sgibbs	mvi	SCB_PHYSADDR	call bcopy_4
1138104Sgibbs        
11413177Sgibbs	mvi	DFCNTRL,0xd	/* HDMAEN|DIRECTION|FIFORESET */
1155647Sgibbs
11613177Sgibbs/*
11713177Sgibbs * Wait for DMA from host memory to data FIFO to complete, then disable
11813177Sgibbs * DMA and wait for it to acknowledge that it's off.
11913177Sgibbs */
1207532Sgibbs	call	dma_finish
1215647Sgibbs
12213177Sgibbs/* Copy the SCB from the FIFO to  the SCBARRAY */
1235647Sgibbs
12413177Sgibbs	mvi	DINDEX, SCBARRAY
1259928Sgibbs	call	bcopy_5_dfdat 
1269928Sgibbs	call	bcopy_7_dfdat
1279928Sgibbs	call	bcopy_7_dfdat
1289928Sgibbs	call	bcopy_7_dfdat   
1298104Sgibbs
13013177Sgibbs/*
13113177Sgibbs * See if there is not already an active SCB for this target.  This code
13213177Sgibbs * locks out on a per target basis instead of target/lun.  Although this
13313177Sgibbs * is not ideal for devices that have multiple luns active at the same
13413177Sgibbs * time, it is faster than looping through all SCB's looking for active
13513177Sgibbs * commands.  It may be benificial to make findscb a more general procedure
13613177Sgibbs * to see if the added cost of the search is negligible.  This code also 
13713177Sgibbs * assumes that the kernel driver will clear the active flags on board 
13813177Sgibbs * initialization, board reset, and a target SELTO.  Tagged commands
13913177Sgibbs * don't set the active bits since you can have more than queue more
14013177Sgibbs * than one command at a time.  We do, however, look to see if there
14113177Sgibbs * are any non-tagged I/Os in progress, and requeue the command if
14213177Sgibbs * there are.  Tagged and non-tagged commands cannot be mixed to a 
14313177Sgibbs * single target.
14413177Sgibbs */
1454568Sgibbs
1465647Sgibbstest_busy:
14713177Sgibbs	mov	FUNCTION1,SCB_TCL
1484568Sgibbs	mov	A,FUNCTION1
14913177Sgibbs	test	SCB_TCL,0x88	jz test_a	/* Id < 8 && A channel */
1505326Sgibbs
1515326Sgibbs	test	ACTIVE_B,A	jnz requeue
15213177Sgibbs	test	SCB_CONTROL,TAG_ENB	jnz start_scb
15313177Sgibbs	/* Mark the current target as busy */
15413177Sgibbs	or	ACTIVE_B,A
1555326Sgibbs	jmp	start_scb
1565326Sgibbs
15713177Sgibbs/* Place the currently active SCB back on the queue for later processing */
1585326Sgibbsrequeue:
1594568Sgibbs	mov	QINFIFO, SCBPTR
1608104Sgibbs	jmp	poll_for_work
1614568Sgibbs
16213177Sgibbs/*
16313177Sgibbs * Pull the first entry off of the waiting for selection list
16413177Sgibbs * We don't have to "test_busy" because only transactions that
16513177Sgibbs * have passed that test can be in the waiting_scb list.
16613177Sgibbs */
1678104Sgibbsstart_waiting:
1688104Sgibbs	mov	SCBPTR,WAITING_SCBH
16913177Sgibbs	jmp	start_scb2
1708104Sgibbs
1715326Sgibbstest_a:
17213177Sgibbs	test	ACTIVE_A,A jnz requeue
17313177Sgibbs	test	SCB_CONTROL,TAG_ENB jnz start_scb
17413177Sgibbs	/* Mark the current target as busy */
17513177Sgibbs	or	ACTIVE_A,A
1765326Sgibbs
1775326Sgibbsstart_scb:
17813177Sgibbs	mov	SCB_NEXT_WAITING,WAITING_SCBH
17913177Sgibbs	mov	WAITING_SCBH, SCBPTR
18013177Sgibbsstart_scb2:
18113177Sgibbs	and	SINDEX,0xf7,SBLKCTL	/* Clear the channel select bit */
18213177Sgibbs	and	A,0x08,SCB_TCL		/* Get new channel bit */
18313177Sgibbs	or	SINDEX,A
18413177Sgibbs	mov	SBLKCTL,SINDEX		/* select channel */
18513177Sgibbs	mov	SCB_TCL	call initialize_scsiid
1868104Sgibbs
18713177Sgibbs/*
18813177Sgibbs * Enable selection phase as an initiator, and do automatic ATN
18913177Sgibbs * after the selection.  We do this now so that we can overlap the
19013177Sgibbs * rest of our work to set up this target with the arbitration and
19113177Sgibbs * selection bus phases.
19213177Sgibbs */
1938104Sgibbsstart_selection:
19413177Sgibbs	mvi	SCSISEQ,0x58		/* ENSELO|ENAUTOATNO|ENRSELI */
1954568Sgibbs
19613177Sgibbs/*
19713177Sgibbs * As soon as we get a successful selection, the target should go
19813177Sgibbs * into the message out phase since we have ATN asserted.  Prepare
19913177Sgibbs * the message to send.
20013177Sgibbs *
20113177Sgibbs * Messages are stored in scratch RAM starting with a length byte
20213177Sgibbs * followed by the message itself.
20313177Sgibbs */
20413177Sgibbs	test	SCB_CMDLEN,0xff jnz mk_identify	/* 0 Length Command? */
2058567Sdg
20613177Sgibbs/*
20713177Sgibbs * The kernel has sent us an SCB with no command attached.  This implies
20813177Sgibbs * that the kernel wants to send a message of some sort to this target,
20913177Sgibbs * so we interrupt the driver, allow it to fill the message buffer, and
21013177Sgibbs * then go back into the arbitration loop
21113177Sgibbs */
2128567Sdg	mvi     INTSTAT,AWAITING_MSG
2139917Sgibbs	jmp     wait_for_selection
2148567Sdg
21513177Sgibbsmk_identify:
21613177Sgibbs	and	A,DISCENB,SCB_CONTROL	/* mask off disconnect privledge */
2174568Sgibbs
21813177Sgibbs	and	SINDEX,0x7,SCB_TCL	/* lun */
21913177Sgibbs	or	SINDEX,A		/* or in disconnect privledge */
22013177Sgibbs	or	SINDEX,MSG_IDENTIFY call mk_mesg /* IDENTIFY message */
2214568Sgibbs
22213177Sgibbs	test	SCB_CONTROL,0xb0 jz  !message	/* WDTR, SDTR or TAG?? */
22313177Sgibbs/*
22413177Sgibbs * Tag Message if Tag enabled in SCB control block.  Use SCBPTR as the tag
22513177Sgibbs * value
22613177Sgibbs */
2274568Sgibbs
2286608Sgibbsmk_tag:
22913177Sgibbs	mvi	DINDEX, MSG1
23013177Sgibbs	test	SCB_CONTROL,TAG_ENB jz mk_tag_done
23113177Sgibbs	and	A,0x23,SCB_CONTROL
2326608Sgibbs	mov	DINDIR,A
2336608Sgibbs	mov	DINDIR,SCBPTR
2346608Sgibbs
23513177Sgibbs	add	MSG_LEN,COMP_MSG0,DINDEX	/* update message length */
2366608Sgibbs
2376608Sgibbsmk_tag_done:
2386608Sgibbs
23913177Sgibbs	mov	DINDEX	call mk_dtr	/* build DTR message if needed */
2406608Sgibbs
2414568Sgibbs!message:
2429917Sgibbswait_for_selection:
24313177Sgibbs	test	SSTAT0,SELDO	jnz select 
24413177Sgibbs	test	SSTAT0,SELDI	jz wait_for_selection
2454568Sgibbs
24613177Sgibbs/*
24713177Sgibbs * Reselection has been initiated by a target. Make a note that we've been
24813177Sgibbs * reselected, but haven't seen an IDENTIFY message from the target
24913177Sgibbs * yet.
25013177Sgibbs */
2514568Sgibbsreselect:
25213177Sgibbs	clr	MSG_LEN		/* Don't have anything in the mesg buffer */
2538104Sgibbs	mov	SELID		call initialize_scsiid
25413177Sgibbs	mvi	SAVED_TCL, 0xff	/*
25513177Sgibbs				 * Fill with an imposible value so we
25613177Sgibbs				 * don't get false hits for a tag
25713177Sgibbs				 * without an identify.
25813177Sgibbs				 */
25913177Sgibbs	and	FLAGS,0x03	/* clear target specific flags */
26013177Sgibbs	or	FLAGS,RESELECTED
26113177Sgibbs	jmp	select2
2624568Sgibbs
26313177Sgibbs/*
26413177Sgibbs * After the selection, remove this SCB from the "waiting for selection"
26513177Sgibbs * list.  This is achieved by simply moving our "next" pointer into
26613177Sgibbs * WAITING_SCBH.  Our next pointer will be set to null the next time this
26713177Sgibbs * SCB is used, so don't bother with it now.
26813177Sgibbs */
2698104Sgibbsselect:
27013177Sgibbs	and	FLAGS,0x03		/* Clear target flags */
27113177Sgibbs	or	SCB_CONTROL,NEEDDMA
27213177Sgibbs
27313177Sgibbs	/*
27413177Sgibbs	 * Some drives will issue a simple tag message during
27513177Sgibbs	 * a tagged selection if they are immediately ready
27613177Sgibbs	 * to handle the command without a disconnect.  Ensure
27713177Sgibbs	 * that SAVED_TCL (used in get_tag) is inialized correctly
27813177Sgibbs	 * during a selection for this reason.
27913177Sgibbs	 */
28013177Sgibbs	mov	SAVED_TCL, SCB_TCL
28113177Sgibbs	mov	WAITING_SCBH,SCB_NEXT_WAITING
2828104Sgibbsselect2:
28313177Sgibbs/*
28413177Sgibbs * Set CLRCHN here before the target has entered a data transfer mode -
28513177Sgibbs * with synchronous SCSI, if you do it later, you blow away some
28613177Sgibbs * data in the SCSI FIFO that the target has already sent to you.
28713177Sgibbs */
28813177Sgibbs	clr	SIGSTATE 
2894568Sgibbs
29013177Sgibbs	or	SXFRCTL0,CLRCHN
29113177Sgibbs/*
29213177Sgibbs * Initialize SCSIRATE with the appropriate value for this target.
29313177Sgibbs */
29413177Sgibbs	call	ndx_dtr
29513177Sgibbs	mov	SCSIRATE,SINDIR
29613177Sgibbs
29713177Sgibbs	mvi	SCSISEQ,ENAUTOATNP		/*
29813177Sgibbs						 * ATN on parity errors
29913177Sgibbs						 * for "in" phases
30013177Sgibbs						 */
30113177Sgibbs	mvi	CLRSINT1,CLRBUSFREE
30213177Sgibbs	mvi	CLRSINT0,0x60			/* CLRSELDI|CLRSELDO */
30313177Sgibbs
30413177Sgibbs/*
30513177Sgibbs * Main loop for information transfer phases.  If BSY is false, then
30613177Sgibbs * we have a bus free condition, expected or not.  Otherwise, wait
30713177Sgibbs * for the target to assert REQ before checking MSG, C/D and I/O
30813177Sgibbs * for the bus phase.
30913177Sgibbs *
31013177Sgibbs */
3114568SgibbsITloop:
31213177Sgibbs	test	SSTAT1,BUSFREE	jnz p_busfree
31313177Sgibbs	test	SSTAT1,REQINIT	jz ITloop
3144568Sgibbs
31513177Sgibbs	and	A,PHASE_MASK,SCSISIGI
3164568Sgibbs
31711829Sgibbs	mov	A		call scsisig
3184568Sgibbs	cmp	ALLZEROS,A	je p_dataout
31913177Sgibbs	cmp	A,P_DATAIN	je p_datain
32013177Sgibbs	cmp	A,P_COMMAND	je p_command
32113177Sgibbs	cmp	A,P_MESGOUT	je p_mesgout
32213177Sgibbs	cmp	A,P_STATUS	je p_status
32313177Sgibbs	cmp	A,P_MESGIN	je p_mesgin
3244568Sgibbs
32513177Sgibbs	mvi	INTSTAT,BAD_PHASE	/* unknown phase - signal driver */
3264568Sgibbs
3274568Sgibbsp_dataout:
32813177Sgibbs	mvi	DMAPARAMS,0x7d			/*
32913177Sgibbs						 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
33013177Sgibbs						 * DIRECTION|FIFORESET
33113177Sgibbs						 */
3329928Sgibbs	jmp	data_phase_init
3334568Sgibbs
33413177Sgibbs/*
33513177Sgibbs * If we re-enter the data phase after going through another phase, the
33613177Sgibbs * STCNT may have been cleared, so restore it from the residual field.
33713177Sgibbs */
3389928Sgibbsdata_phase_reinit:
3399928Sgibbs	mvi	DINDEX, STCNT
34013177Sgibbs	mvi	SCB_RESID_DCNT	call bcopy_3
3419928Sgibbs	jmp	data_phase_loop
3424568Sgibbs
3439928Sgibbsp_datain:
34413177Sgibbs	mvi	DMAPARAMS,0x79		/*
34513177Sgibbs					 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
34613177Sgibbs					 * !DIRECTION|FIFORESET
34713177Sgibbs					 */
3489928Sgibbsdata_phase_init:
3499928Sgibbs	call	assert
3505775Sgibbs
3519928Sgibbs	test	FLAGS, DPHASE	jnz data_phase_reinit
3529928Sgibbs	call	sg_scb2ram
35313177Sgibbs	or	FLAGS, DPHASE		/* We have seen a data phase */
3544568Sgibbs
3559928Sgibbsdata_phase_loop:
35613177Sgibbs/* If we are the last SG block, don't set wideodd. */
3579928Sgibbs	cmp	SG_COUNT,0x01 jne data_phase_wideodd
35813177Sgibbs	and	DMAPARAMS, 0xbf		/* Turn off WIDEODD */
3599928Sgibbsdata_phase_wideodd:
3609928Sgibbs	mov	DMAPARAMS  call dma
3614568Sgibbs
36213177Sgibbs/* Exit if we had an underrun */
36313177Sgibbs	test	SSTAT0,SDONE	jz data_phase_finish /* underrun STCNT != 0 */
3647532Sgibbs
36513177Sgibbs/*
36613177Sgibbs * Advance the scatter-gather pointers if needed 
36713177Sgibbs */
3689928Sgibbssg_advance:
36913177Sgibbs	dec	SG_COUNT	/* one less segment to go */
3704568Sgibbs
37113177Sgibbs	test	SG_COUNT, 0xff	jz data_phase_finish /* Are we done? */
3724568Sgibbs
37313177Sgibbs	clr	A			/* add sizeof(struct scatter) */
37413177Sgibbs	add	SG_NEXT0,SG_SIZEOF,SG_NEXT0
37513177Sgibbs	adc	SG_NEXT1,A,SG_NEXT1
37613177Sgibbs	adc	SG_NEXT2,A,SG_NEXT2
37713177Sgibbs	adc	SG_NEXT3,A,SG_NEXT3
3784568Sgibbs
37913177Sgibbs/*
38013177Sgibbs * Load a struct scatter and set up the data address and length.
38113177Sgibbs * If the working value of the SG count is nonzero, then
38213177Sgibbs * we need to load a new set of values.
38313177Sgibbs *
38413177Sgibbs * This, like all DMA's, assumes a little-endian host data storage.
38513177Sgibbs */
3869928Sgibbssg_load:
38713177Sgibbs	clr	HCNT2
38813177Sgibbs	clr	HCNT1
38913177Sgibbs	mvi	HCNT0,SG_SIZEOF
3904568Sgibbs
3919928Sgibbs	mvi	DINDEX,HADDR
3929928Sgibbs	mvi	SG_NEXT		call bcopy_4
3934568Sgibbs
39413177Sgibbs	mvi	DFCNTRL,0xd			/* HDMAEN|DIRECTION|FIFORESET */
3959928Sgibbs
39613177Sgibbs/*
39713177Sgibbs * Wait for DMA from host memory to data FIFO to complete, then disable
39813177Sgibbs * DMA and wait for it to acknowledge that it's off.
39913177Sgibbs */
4009928Sgibbs	call	dma_finish
4019928Sgibbs
40213177Sgibbs/*
40313177Sgibbs * Copy data from FIFO into SCB data pointer and data count.  This assumes
40413177Sgibbs * that the struct scatterlist has this structure (this and sizeof(struct
40513177Sgibbs * scatterlist) == 12 are asserted in aic7xxx.c):
40613177Sgibbs *
40713177Sgibbs *	struct scatterlist {
40813177Sgibbs *		char *address;		four bytes, little-endian order
40913177Sgibbs *		...			four bytes, ignored
41013177Sgibbs *		unsigned short length;	two bytes, little-endian order
41113177Sgibbs *	}
41213177Sgibbs *
41313177Sgibbs *
41413177Sgibbs * Not in FreeBSD.  the scatter list entry is only 8 bytes.
41513177Sgibbs * 
41613177Sgibbs * struct ahc_dma_seg {
41713177Sgibbs *       physaddr addr;                  four bytes, little-endian order
41813177Sgibbs *       long    len;                    four bytes, little endian order
41913177Sgibbs * };
42013177Sgibbs */
4219928Sgibbs
42213177Sgibbs/*
42313177Sgibbs * For Linux, we must throw away four bytes since there is a 32bit gap
42413177Sgibbs * in the middle of a struct scatterlist
42513177Sgibbs */
42613177Sgibbs#ifdef LINUX
42713177Sgibbs	call	bcopy_4_dfdat
42813177Sgibbs	mov	NONE,DFDAT
42913177Sgibbs	mov	NONE,DFDAT
43013177Sgibbs	mov	NONE,DFDAT
43113177Sgibbs	mov	NONE,DFDAT
43213177Sgibbs	call	bcopy_3_dfdat		/* Only support 24 bit length. */
43313177Sgibbs#else
43413177Sgibbs/*
43513177Sgibbs * For FreeBSD, just copy it wholesale
43613177Sgibbs */
4375775Sgibbs	mvi	DINDEX,HADDR
4389928Sgibbs	call	bcopy_7_dfdat
43913177Sgibbs#endif
4404568Sgibbs
44113177Sgibbs/* Load STCNT as well.  It is a mirror of HCNT */
4424568Sgibbs	mvi	DINDEX,STCNT
4439928Sgibbs	mvi	HCNT	call bcopy_3
4449928Sgibbs        test    SSTAT1,PHASEMIS  jz data_phase_loop
4454568Sgibbs
4469928Sgibbsdata_phase_finish:
44713177Sgibbs/*
44813177Sgibbs * After a DMA finishes, save the SG and STCNT residuals back into the SCB
44913177Sgibbs * We use STCNT instead of HCNT, since it's a reflection of how many bytes 
45013177Sgibbs * were transferred on the SCSI (as opposed to the host) bus.
45113177Sgibbs */
45213177Sgibbs	mvi	DINDEX,SCB_RESID_DCNT
45313177Sgibbs	mvi	STCNT	call bcopy_3
45413177Sgibbs	mov	SCB_RESID_SGCNT, SG_COUNT
4554568Sgibbs	jmp	ITloop
4564568Sgibbs
45713177Sgibbs/*
45813177Sgibbs * Command phase.  Set up the DMA registers and let 'er rip - the
45913177Sgibbs * two bytes after the SCB SCSI_cmd_length are zeroed by the driver,
46013177Sgibbs * so we can copy those three bytes directly into HCNT.
46113177Sgibbs */
4624568Sgibbsp_command:
4634568Sgibbs	call	assert
4644568Sgibbs
46513177Sgibbs/*
46613177Sgibbs * Load HADDR and HCNT.  We can do this in one bcopy since they are neighbors
46713177Sgibbs */
4685775Sgibbs	mvi	DINDEX,HADDR
46913177Sgibbs	mvi	SCB_CMDPTR	call bcopy_7
4704568Sgibbs
4714568Sgibbs	mvi	DINDEX,STCNT
47213177Sgibbs	mvi	SCB_CMDLEN	call bcopy_3
4734568Sgibbs
4744568Sgibbs	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
4754568Sgibbs						#   DIRECTION|FIFORESET
4764568Sgibbs	jmp	ITloop
4774568Sgibbs
47813177Sgibbs/*
47913177Sgibbs * Status phase.  Wait for the data byte to appear, then read it
48013177Sgibbs * and store it into the SCB.
48113177Sgibbs */
4824568Sgibbsp_status:
48313177Sgibbs	mvi	SCB_TARGET_STATUS	call inb_first
4849954Sgibbs	jmp	mesgin_done
4854568Sgibbs
48613177Sgibbs/*
48713177Sgibbs * Message out phase.  If there is no active message, but the target
48813177Sgibbs * took us into this phase anyway, build a no-op message and send it.
48913177Sgibbs */
4904568Sgibbsp_mesgout:
49113177Sgibbs	test	MSG_LEN, 0xff	jnz  p_mesgout_start
49213177Sgibbs	mvi	MSG_NOP		call mk_mesg	/* build NOP message */
4934568Sgibbs
49413177Sgibbsp_mesgout_start:
49513177Sgibbs/*
49613177Sgibbs * Set up automatic PIO transfer from MSG0.  Bit 3 in
49713177Sgibbs * SXFRCTL0 (SPIOEN) is already on.
49813177Sgibbs */
49913177Sgibbs	mvi	SINDEX,MSG0
5004568Sgibbs	mov	DINDEX,MSG_LEN
5014568Sgibbs
50213177Sgibbs/*
50313177Sgibbs * When target asks for a byte, drop ATN if it's the last one in
50413177Sgibbs * the message.  Otherwise, keep going until the message is exhausted.
50513177Sgibbs *
50613177Sgibbs * Keep an eye out for a phase change, in case the target issues
50713177Sgibbs * a MESSAGE REJECT.
50813177Sgibbs */
50913177Sgibbsp_mesgout_loop:
51013313Sgibbs	test	SSTAT1,PHASEMIS	jnz p_mesgout_phasemis
51113177Sgibbs	test	SSTAT0,SPIORDY	jz p_mesgout_loop
51213177Sgibbs	cmp	DINDEX,1	jne p_mesgout_outb	/* last byte? */
51313177Sgibbs	mvi	CLRSINT1,CLRATNO			/* drop ATN */
51413177Sgibbsp_mesgout_outb:
51513313Sgibbs	or	SXFRCTL0, CLRSTCNT
51613313Sgibbs	mvi	STCNT0, 1
51713177Sgibbs	dec	DINDEX
5184568Sgibbs	mov	SCSIDATL,SINDIR
51913313Sgibbsp_mesgout_outb_wait:
52013313Sgibbs	test	SSTAT0,SDONE	jz p_mesgout_outb_wait
52113313Sgibbs	
5224568Sgibbsp_mesgout4:
52313177Sgibbs	test	DINDEX,0xff	jnz p_mesgout_loop
5244568Sgibbs
52513177Sgibbs/*
52613177Sgibbs * If the next bus phase after ATN drops is a message out, it means
52713177Sgibbs * that the target is requesting that the last message(s) be resent.
52813177Sgibbs */
52913177Sgibbsp_mesgout_snoop:
53013177Sgibbs	test	SSTAT1,BUSFREE	jnz p_mesgout_done
53113177Sgibbs	test	SSTAT1,REQINIT	jz p_mesgout_snoop
5324568Sgibbs
53313177Sgibbs	test	SSTAT1,PHASEMIS	jnz p_mesgout_done
5344568Sgibbs
53513177Sgibbs	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
53613177Sgibbs	call	scsisig				/* ATNO - re-assert ATN */
53713177Sgibbs
5384568Sgibbs	jmp	ITloop
5394568Sgibbs
54013177Sgibbsp_mesgout_phasemis:
54113177Sgibbs	mvi	CLRSINT1,CLRATNO	/* Be sure turn ATNO off */
54213177Sgibbsp_mesgout_done:
54313177Sgibbs	clr	MSG_LEN			/* no active msg */
5444568Sgibbs	jmp	ITloop
5454568Sgibbs
54613177Sgibbs/*
54713177Sgibbs * Message in phase.  Bytes are read using Automatic PIO mode.
54813177Sgibbs */
5494568Sgibbsp_mesgin:
55013177Sgibbs	mvi	A		call inb_first	/* read the 1st message byte */
55113177Sgibbs	mov	REJBYTE,A			/* save it for the driver */
5524568Sgibbs
55313177Sgibbs	test	A,MSG_IDENTIFY		jnz mesgin_identify
55413177Sgibbs	cmp	A,MSG_DISCONNECT	je mesgin_disconnect
55513177Sgibbs	cmp	A,MSG_SDPTRS		je mesgin_sdptrs
55613177Sgibbs	cmp	ALLZEROS,A		je mesgin_complete
55713177Sgibbs	cmp	A,MSG_RDPTRS		je mesgin_rdptrs
55813177Sgibbs	cmp	A,MSG_EXTENDED		je mesgin_extended
55913177Sgibbs	cmp	A,MSG_REJECT		je mesgin_reject
5604568Sgibbs
5619954Sgibbsrej_mesgin:
56213177Sgibbs/*
56313177Sgibbs * We have no idea what this message in is, and there's no way
56413177Sgibbs * to pass it up to the kernel, so we issue a message reject and
56513177Sgibbs * hope for the best.  Since we're now using manual PIO mode to
56613177Sgibbs * read in the message, there should no longer be a race condition
56713177Sgibbs * present when we assert ATN.  In any case, rejection should be a
56813177Sgibbs * rare occurrence - signal the driver when it happens.
56913177Sgibbs */
57013177Sgibbs	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
5719954Sgibbs	call	scsisig
57213177Sgibbs	mvi	INTSTAT,SEND_REJECT		/* let driver know */
5739954Sgibbs
57413177Sgibbs	mvi	MSG_REJECT	call mk_mesg
5759954Sgibbs
5769954Sgibbsmesgin_done:
57713177Sgibbs	call	inb_last			/*ack & turn auto PIO back on*/
5789954Sgibbs	jmp	ITloop
5799954Sgibbs
5809954Sgibbs
5819954Sgibbsmesgin_complete:
58213177Sgibbs/*
58313177Sgibbs * We got a "command complete" message, so put the SCB pointer
58413177Sgibbs * into the Queue Out, and trigger a completion interrupt.
58513177Sgibbs * Check status for non zero return and interrupt driver if needed
58613177Sgibbs * This allows the driver to interpret errors only when they occur
58713177Sgibbs * instead of always uploading the scb.  If the status is SCSI_CHECK,
58813177Sgibbs * the driver will download a new scb requesting sense to replace
58913177Sgibbs * the old one, modify the "waiting for selection" SCB list and set 
59013177Sgibbs * RETURN_1 to 0x80.  If RETURN_1 is set to 0x80 the sequencer imediately
59113177Sgibbs * jumps to main loop where it will run down the waiting SCB list.
59213177Sgibbs * If the kernel driver does not wish to request sense, it need
59313177Sgibbs * only clear RETURN_1, and the command is allowed to complete.  We don't 
59413177Sgibbs * bother to post to the QOUTFIFO in the error case since it would require 
59513177Sgibbs * extra work in the kernel driver to ensure that the entry was removed 
59613177Sgibbs * before the command complete code tried processing it.
59713177Sgibbs *
59813177Sgibbs * First check for residuals
59913177Sgibbs */
60013177Sgibbs	test	SCB_RESID_SGCNT,0xff	jz check_status
60113177Sgibbs/*
60213177Sgibbs * If we have a residual count, interrupt and tell the host.  Other
60313177Sgibbs * alternatives are to pause the sequencer on all command completes (yuck),
60413177Sgibbs * dma the resid directly to the host (slick, we may have space to do it now)
60513177Sgibbs * or have the sequencer pause itself when it encounters a non-zero resid 
60613177Sgibbs * (unecessary pause just to flag the command -yuck-, but takes one instruction
60713177Sgibbs * and since it shouldn't happen that often is good enough for our purposes).  
60813177Sgibbs */
60913177Sgibbsresid:
61013177Sgibbs	mvi	INTSTAT,RESIDUAL
6114568Sgibbs
6127532Sgibbscheck_status:
61313177Sgibbs	test	SCB_TARGET_STATUS,0xff	jz status_ok	/* Good Status? */
61413177Sgibbs	mvi	INTSTAT,BAD_STATUS			/* let driver know */
61513177Sgibbs	cmp	RETURN_1, SEND_SENSE	jne status_ok
6169954Sgibbs	jmp	mesgin_done
6175326Sgibbs
6184568Sgibbsstatus_ok:
61913177Sgibbs/* First, mark this target as free. */
62013177Sgibbs	test	SCB_CONTROL,TAG_ENB jnz test_immediate	/*
62113177Sgibbs							 * Tagged commands
62213177Sgibbs							 * don't busy the
62313177Sgibbs							 * target.
62413177Sgibbs							 */
62513177Sgibbs	mov	FUNCTION1,SCB_TCL
6265326Sgibbs	mov	A,FUNCTION1
62713177Sgibbs	test	SCB_TCL,0x88 jz clear_a
6285326Sgibbs	xor	ACTIVE_B,A
62913177Sgibbs	jmp	test_immediate
6305326Sgibbs
6315326Sgibbsclear_a:
6325326Sgibbs	xor	ACTIVE_A,A
6335326Sgibbs
63413177Sgibbstest_immediate:
63513177Sgibbs	test    SCB_CMDLEN,0xff jnz complete  /* Immediate message complete */
63613177Sgibbs/*
63713177Sgibbs * Pause the sequencer until the driver gets around to handling the command
63813177Sgibbs * complete.  This is so that any action that might require carefull timing
63913177Sgibbs * with the completion of this command can occur.
64013177Sgibbs */
6419810Sgibbs	mvi	INTSTAT,IMMEDDONE
64213177Sgibbs	jmp	start
6435326Sgibbscomplete:
6444568Sgibbs	mov	QOUTFIFO,SCBPTR
6457532Sgibbs	mvi	INTSTAT,CMDCMPLT
6469954Sgibbs	jmp	mesgin_done
6474568Sgibbs
6487532Sgibbs
64913177Sgibbs/*
65013177Sgibbs * Is it an extended message?  We only support the synchronous and wide data
65113177Sgibbs * transfer request messages, which will probably be in response to
65213177Sgibbs * WDTR or SDTR message outs from us.  If it's not SDTR or WDTR, reject it -
65313177Sgibbs * apparently this can be done after any message in byte, according
65413177Sgibbs * to the SCSI-2 spec.
65513177Sgibbs */
6569954Sgibbsmesgin_extended:
65713177Sgibbs	mvi	ARG_1		call inb_next	/* extended message length */
65813177Sgibbs	mvi	A		call inb_next	/* extended message code */
6594568Sgibbs
66013177Sgibbs	cmp	A,MSG_SDTR	je p_mesginSDTR
66113177Sgibbs	cmp	A,MSG_WDTR	je p_mesginWDTR
6629954Sgibbs	jmp	rej_mesgin
6635562Sgibbs
6645562Sgibbsp_mesginWDTR:
66513177Sgibbs	cmp	ARG_1,2		jne rej_mesgin	/* extended mesg length=2 */
66613177Sgibbs	mvi	ARG_1		call inb_next	/* Width of bus */
66713177Sgibbs	mvi	INTSTAT,WDTR_MSG		/* let driver know */
66813177Sgibbs	test	RETURN_1,0xff jz mesgin_done	/* Do we need to send WDTR? */
66913177Sgibbs	cmp	RETURN_1,SEND_REJ je rej_mesgin /*
67013177Sgibbs						 * Bus width was too large 
67113177Sgibbs						 * Reject it.
67213177Sgibbs						 */
6735562Sgibbs
67413177Sgibbs/* We didn't initiate the wide negotiation, so we must respond to the request */
67513177Sgibbs	and	RETURN_1,0x7f			/* Clear the SEND_WDTR Flag */
67613177Sgibbs	mvi	DINDEX,MSG0
67713177Sgibbs	mvi	MSG0	call mk_wdtr		/* build WDTR message */
67813177Sgibbs	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
6795562Sgibbs	call	scsisig
6809954Sgibbs	jmp	mesgin_done
6815562Sgibbs
6825562Sgibbsp_mesginSDTR:
68313177Sgibbs	cmp	ARG_1,3		jne rej_mesgin	/* extended mesg length=3 */
68413177Sgibbs	mvi	ARG_1		call inb_next	/* xfer period */
68513177Sgibbs	mvi	A		call inb_next	/* REQ/ACK offset */
68613177Sgibbs	mvi	INTSTAT,SDTR_MSG		/* call driver to convert */
6874568Sgibbs
68813177Sgibbs	test	RETURN_1,0xff	jz mesgin_done  /* Do we need to mk_sdtr/rej */
68913177Sgibbs	cmp	RETURN_1,SEND_REJ je rej_mesgin /*
69013177Sgibbs						 * Requested SDTR too small
69113177Sgibbs						 * Reject it.
69213177Sgibbs						 */
69313177Sgibbs	mvi	DINDEX, MSG0
69413177Sgibbs	mvi     MSG0     call mk_sdtr
69513177Sgibbs	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
6965562Sgibbs	call	scsisig
6979954Sgibbs	jmp	mesgin_done
6984568Sgibbs
69913177Sgibbs/*
70013177Sgibbs * Is it a disconnect message?  Set a flag in the SCB to remind us
70113177Sgibbs * and await the bus going free.
70213177Sgibbs */
7039954Sgibbsmesgin_disconnect:
70413177Sgibbs	or	SCB_CONTROL,DISCONNECTED
7059954Sgibbs	jmp	mesgin_done
7064568Sgibbs
70713177Sgibbs/*
70813177Sgibbs * Save data pointers message?  Copy working values into the SCB,
70913177Sgibbs * usually in preparation for a disconnect.
71013177Sgibbs */
7119954Sgibbsmesgin_sdptrs:
7124568Sgibbs	call	sg_ram2scb
7139954Sgibbs	jmp	mesgin_done
7144568Sgibbs
71513177Sgibbs/*
71613177Sgibbs * Restore pointers message?  Data pointers are recopied from the
71713177Sgibbs * SCB anytime we enter a data phase for the first time, so all
71813177Sgibbs * we need to do is clear the DPHASE flag and let the data phase
71913177Sgibbs * code do the rest.
72013177Sgibbs */
7219954Sgibbsmesgin_rdptrs:
72213177Sgibbs	and	FLAGS,0xfb			/*
72313177Sgibbs						 * !DPHASE we'll reload them
72413177Sgibbs						 * the next time through
72513177Sgibbs						 */
7269954Sgibbs	jmp	mesgin_done
7274568Sgibbs
72813177Sgibbs/*
72913177Sgibbs * Identify message?  For a reconnecting target, this tells us the lun
73013177Sgibbs * that the reconnection is for - find the correct SCB and switch to it,
73113177Sgibbs * clearing the "disconnected" bit so we don't "find" it by accident later.
73213177Sgibbs */
7339954Sgibbsmesgin_identify:
73413177Sgibbs	test	A,0x78	jnz rej_mesgin	/*!DiscPriv|!LUNTAR|!Reserved*/
7354568Sgibbs
73613177Sgibbs	and	A,0x07			/* lun in lower three bits */
7376608Sgibbs	or      SAVED_TCL,A,SELID          
7386608Sgibbs	and     SAVED_TCL,0xf7
73913177Sgibbs	and     A,SELBUSB,SBLKCTL	/* B Channel?? */
7406608Sgibbs	or      SAVED_TCL,A
74113177Sgibbs	call	inb_last		/* ACK */
74213177Sgibbs
74313177Sgibbs/*
74413177Sgibbs * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
74513177Sgibbs * If we get one, we use the tag returned to switch to the proper
74613177Sgibbs * SCB.  Otherwise, we just use the findSCB method.
74713177Sgibbs */
74813177Sgibbssnoop_tag_loop:
74913177Sgibbs	test	SSTAT1,BUSFREE	jnz use_findSCB
75013177Sgibbs	test	SSTAT1,REQINIT	jz snoop_tag_loop
75113177Sgibbs	test	SSTAT1,PHASEMIS	jnz use_findSCB
75213177Sgibbs	mvi	A		call inb_first
75313177Sgibbs	cmp	A,MSG_SIMPLE_TAG je get_tag
75413177Sgibbsuse_findSCB:
75513177Sgibbs	mov	ALLZEROS	call findSCB	  /* Have to search */
7566608Sgibbssetup_SCB:
75713177Sgibbs	and	SCB_CONTROL,0xfb	  /* clear disconnect bit in SCB */
75813177Sgibbs	or	FLAGS,IDENTIFY_SEEN	  /* make note of IDENTIFY */
7596608Sgibbs	jmp	ITloop
7606608Sgibbsget_tag:
76113177Sgibbs	mvi	ARG_1	call inb_next	/* tag value */
76213177Sgibbs/*
76313177Sgibbs * See if the tag is in range.  The tag is < SCBCOUNT if we add
76413177Sgibbs * the complement of SCBCOUNT to the incomming tag and there is
76513177Sgibbs * no carry.
76613177Sgibbs */
76713177Sgibbs	mov	A,COMP_SCBCOUNT	
76813177Sgibbs	add	SINDEX,A,ARG_1
76913177Sgibbs	jc	abort_tag
77013177Sgibbs
77113177Sgibbs/*
77213177Sgibbs * Ensure that the SCB the tag points to is for an SCB transaction
77313177Sgibbs * to the reconnecting target.
77413177Sgibbs */
77513177Sgibbs	mov	SCBPTR,ARG_1
7766608Sgibbs	mov	A,SAVED_TCL
77713177Sgibbs	cmp	SCB_TCL,A		jne abort_tag
77813177Sgibbs	test	SCB_CONTROL,TAG_ENB	jz  abort_tag
77913177Sgibbs	call	inb_last			/* Ack Successful tag */
78013177Sgibbs	jmp	setup_SCB
7817700Sgibbsabort_tag:
78213177Sgibbs	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
7837700Sgibbs	call	scsisig
78413177Sgibbs	mvi	INTSTAT,ABORT_TAG 		/* let driver know */
78513177Sgibbs	mvi	0xd		call mk_mesg	/* ABORT TAG message */
78613177Sgibbs	jmp	mesgin_done
7876608Sgibbs
78813177Sgibbs/*
78913177Sgibbs * Message reject?  Let the kernel driver handle this.  If we have an 
79013177Sgibbs * outstanding WDTR or SDTR negotiation, assume that it's a response from 
79113177Sgibbs * the target selecting 8bit or asynchronous transfer, otherwise just ignore 
79213177Sgibbs * it since we have no clue what it pertains to.
79313177Sgibbs */
7949954Sgibbsmesgin_reject:
79513177Sgibbs	mvi	INTSTAT, REJECT_MSG
7969954Sgibbs	jmp	mesgin_done
7975562Sgibbs
79813177Sgibbs/*
79913177Sgibbs * [ ADD MORE MESSAGE HANDLING HERE ]
80013177Sgibbs */
8014568Sgibbs
80213177Sgibbs/*
80313177Sgibbs * Bus free phase.  It might be useful to interrupt the device
80413177Sgibbs * driver if we aren't expecting this.  For now, make sure that
80513177Sgibbs * ATN isn't being asserted and look for a new command.
80613177Sgibbs */
8074568Sgibbsp_busfree:
80813177Sgibbs	mvi	CLRSINT1,CLRATNO
8098567Sdg
81013177Sgibbs/*
81113177Sgibbs * if this is an immediate command, perform a psuedo command complete to
81213177Sgibbs * notify the driver.
81313177Sgibbs */
81413177Sgibbs	test	SCB_CMDLEN,0xff	jz status_ok
81513177Sgibbs	jmp	start
8164568Sgibbs
81713177Sgibbs/*
81813177Sgibbs * Instead of a generic bcopy routine that requires an argument, we unroll
81913177Sgibbs * the cases that are actually used, and call them explicitly.  This
82013177Sgibbs * not only reduces the overhead of doing a bcopy, but ends up saving space 
82113177Sgibbs * in the program since you don't have to put the argument into the accumulator
82213177Sgibbs * before the call.  Both functions expect DINDEX to contain the destination 
82313177Sgibbs * address and SINDEX to contain the source address.
82413177Sgibbs */
8259928Sgibbsbcopy_7:
8264568Sgibbs	mov	DINDIR,SINDIR
8275775Sgibbs	mov	DINDIR,SINDIR
8289928Sgibbsbcopy_5:
8299928Sgibbs	mov	DINDIR,SINDIR
8305775Sgibbsbcopy_4:
8315775Sgibbs	mov	DINDIR,SINDIR
8329928Sgibbsbcopy_3:
8335775Sgibbs	mov	DINDIR,SINDIR
8345775Sgibbs	mov	DINDIR,SINDIR
8355775Sgibbs	mov	DINDIR,SINDIR	ret
8365775Sgibbs	
8379928Sgibbsbcopy_7_dfdat:
8387532Sgibbs	mov	DINDIR,DFDAT
8397532Sgibbs	mov	DINDIR,DFDAT
8409928Sgibbsbcopy_5_dfdat:
8419928Sgibbs	mov	DINDIR,DFDAT
8427532Sgibbsbcopy_4_dfdat:
8437532Sgibbs	mov	DINDIR,DFDAT
8449928Sgibbsbcopy_3_dfdat:
8457532Sgibbs	mov	DINDIR,DFDAT
8467532Sgibbs	mov	DINDIR,DFDAT
8477532Sgibbs	mov	DINDIR,DFDAT	ret
8487532Sgibbs
84913177Sgibbs/*
85013177Sgibbs * Locking the driver out, build a one-byte message passed in SINDEX
85113177Sgibbs * if there is no active message already.  SINDEX is returned intact.
85213177Sgibbs */
8534568Sgibbsmk_mesg:
85413177Sgibbs	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
85513177Sgibbs	test	MSG_LEN,0xff	jz mk_mesg1	/* Should always succeed */
85613177Sgibbs	
85713177Sgibbs	/*
85813177Sgibbs	 * Hmmm.  For some reason the mesg buffer is in use.
85913177Sgibbs	 * Tell the driver.  It should look at SINDEX to find
86013177Sgibbs	 * out what we wanted to use the buffer for and resolve
86113177Sgibbs	 * the conflict.
86213177Sgibbs	 */
86313177Sgibbs	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
86413177Sgibbs	mvi	INTSTAT,MSG_BUFFER_BUSY ret
8654568Sgibbs
8664568Sgibbsmk_mesg1:
86713177Sgibbs	mvi	MSG_LEN,1		/* length = 1 */
86813177Sgibbs	mov	MSG0,SINDEX		/* 1-byte message */
86913177Sgibbs	mvi	SEQCTL,0x10	ret	/* !PAUSEDIS|FASTMODE */
8704568Sgibbs
87113177Sgibbs/*
87213177Sgibbs * Functions to read data in Automatic PIO mode.
87313177Sgibbs *
87413177Sgibbs * According to Adaptec's documentation, an ACK is not sent on input from
87513177Sgibbs * the target until SCSIDATL is read from.  So we wait until SCSIDATL is
87613177Sgibbs * latched (the usual way), then read the data byte directly off the bus
87713177Sgibbs * using SCSIBUSL.  When we have pulled the ATN line, or we just want to
87813177Sgibbs * acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
87913177Sgibbs * spec guarantees that the target will hold the data byte on the bus until
88013177Sgibbs * we send our ACK.
88113177Sgibbs *
88213177Sgibbs * The assumption here is that these are called in a particular sequence,
88313177Sgibbs * and that REQ is already set when inb_first is called.  inb_{first,next}
88413177Sgibbs * use the same calling convention as inb.
88513177Sgibbs */
88613177Sgibbs
88713177Sgibbsinb_next:
88813251Sgibbs	call	inb_last
88913313Sgibbsinb_first:
89013251Sgibbs	test	SSTAT1,PHASEMIS	jnz mesgin_phasemis
89113313Sgibbs	test	SSTAT0,SPIORDY	jz inb_first /* wait for next byte */
8924568Sgibbs	mov	DINDEX,SINDEX
89313177Sgibbs	mov	DINDIR,SCSIBUSL	ret		/*read byte directly from bus*/
89413177Sgibbsinb_last:
89513313Sgibbs	or	SXFRCTL0,CLRSTCNT
89613251Sgibbs	mvi	STCNT0,0x01
89713251Sgibbs	mov	NONE,SCSIDATL			/*dummy read from latch to ACK*/
89813251Sgibbsinb_last_wait:
89913251Sgibbs	test	SSTAT0,SDONE	jz inb_last_wait /* Wait for completion */
90013251Sgibbs	ret
9014568Sgibbs
90213177Sgibbsmesgin_phasemis:
90313177Sgibbs/*
90413177Sgibbs * We expected to receive another byte, but the target changed phase
90513177Sgibbs */
90613177Sgibbs	mvi	INTSTAT, MSGIN_PHASEMIS
90713177Sgibbs	jmp	ITloop
9084568Sgibbs
90913177Sgibbs/*
91013177Sgibbs * DMA data transfer.  HADDR and HCNT must be loaded first, and
91113177Sgibbs * SINDEX should contain the value to load DFCNTRL with - 0x3d for
91213177Sgibbs * host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
91313177Sgibbs * during initialization.
91413177Sgibbs */
9154568Sgibbsdma:
9164568Sgibbs	mov	DFCNTRL,SINDEX
9174568Sgibbsdma1:
91813177Sgibbs	test	SSTAT0,DMADONE	jnz dma3
91913177Sgibbs	test	SSTAT1,PHASEMIS	jz dma1		/* ie. underrun */
9204568Sgibbs
92113177Sgibbs/*
92213177Sgibbs * We will be "done" DMAing when the transfer count goes to zero, or
92313177Sgibbs * the target changes the phase (in light of this, it makes sense that
92413177Sgibbs * the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
92513177Sgibbs * doing a SCSI->Host transfer, the data FIFO should be flushed auto-
92613177Sgibbs * magically on STCNT=0 or a phase change, so just wait for FIFO empty
92713177Sgibbs * status.
92813177Sgibbs */
9294568Sgibbsdma3:
93013177Sgibbs	test	SINDEX,DIRECTION	jnz dma5
9314568Sgibbsdma4:
93213177Sgibbs	test	DFSTATUS,FIFOEMP	jz dma4
9334568Sgibbs
93413177Sgibbs/*
93513177Sgibbs * Now shut the DMA enables off and make sure that the DMA enables are 
93613177Sgibbs * actually off first lest we get an ILLSADDR.
93713177Sgibbs */
9384568Sgibbsdma5:
93913177Sgibbs	/* disable DMA, but maintain WIDEODD */
94013177Sgibbs	and	A, WIDEODD, SINDEX	
94113177Sgibbs	mov	DFCNTRL, A
9424568Sgibbsdma6:
94313177Sgibbs	test	DFCNTRL,0x38	jnz dma6  /* SCSIENACK|SDMAENACK|HDMAENACK */
9444568Sgibbs
9454568Sgibbs	ret
9464568Sgibbs
9477532Sgibbsdma_finish:
94813177Sgibbs	test	DFSTATUS,HDONE	jz dma_finish
9497532Sgibbs
95013177Sgibbs	clr	DFCNTRL				/* disable DMA */
9517532Sgibbsdma_finish2:
95213177Sgibbs	test	DFCNTRL,HDMAENACK jnz dma_finish2
9537532Sgibbs	ret
9547532Sgibbs
95513177Sgibbs/*
95613177Sgibbs * Common SCSI initialization for selection and reselection.  Expects
95713177Sgibbs * the target SCSI ID to be in the upper four bits of SINDEX, and A's
95813177Sgibbs * contents are stomped on return.
95913177Sgibbs */
9608104Sgibbsinitialize_scsiid:
96113177Sgibbs	and	SINDEX,0xf0		/* Get target ID */
9625775Sgibbs	and	A,0x0f,SCSIID
9635775Sgibbs	or	SINDEX,A
9648104Sgibbs	mov	SCSIID,SINDEX ret
9655326Sgibbs
96613177Sgibbs/*
96713177Sgibbs * Assert that if we've been reselected, then we've seen an IDENTIFY
96813177Sgibbs * message.
96913177Sgibbs */
9704568Sgibbsassert:
97113177Sgibbs	test	FLAGS,RESELECTED	jz return	/* reselected? */
97213177Sgibbs	test	FLAGS,IDENTIFY_SEEN	jnz return	/* seen IDENTIFY? */
9734568Sgibbs
97413177Sgibbs	mvi	INTSTAT,NO_IDENT 	ret	/* no - cause a kernel panic */
9754568Sgibbs
97613177Sgibbs/*
97713177Sgibbs * Locate the SCB matching the target ID/channel/lun in SAVED_TCL and switch 
97813177Sgibbs * the SCB to it.  Have the kernel print a warning message if it can't be 
97913177Sgibbs * found, and generate an ABORT message to the target.  SINDEX should be
98013177Sgibbs * cleared on call.
98113177Sgibbs */
9824568SgibbsfindSCB:
9836608Sgibbs	mov	A,SAVED_TCL
98413177Sgibbs	mov	SCBPTR,SINDEX			/* switch to new SCB */
98513177Sgibbs	cmp	SCB_TCL,A	jne findSCB1 /* target ID/channel/lun match? */
98613177Sgibbs	test	SCB_CONTROL,DISCONNECTED jz findSCB1 /*should be disconnected*/
9874568Sgibbs	ret
9884568Sgibbs
9896608SgibbsfindSCB1:
9904568Sgibbs	inc	SINDEX
9914568Sgibbs	mov	A,SCBCOUNT
9926608Sgibbs	cmp	SINDEX,A	jne findSCB
9934568Sgibbs
99413177Sgibbs	mvi	INTSTAT,NO_MATCH		/* not found - signal kernel */
99513177Sgibbs	mvi	MSG_ABORT	call mk_mesg	/* ABORT message */
9964568Sgibbs
99713177Sgibbs	or	SINDEX,0x10,SIGSTATE		/* assert ATNO */
9984568Sgibbs	call	scsisig
9994568Sgibbs	ret
100013177Sgibbs/*
100113177Sgibbs * Make a working copy of the scatter-gather parameters from the SCB.
100213177Sgibbs */
10034568Sgibbssg_scb2ram:
10049928Sgibbs	mvi	DINDEX,HADDR
100513177Sgibbs	mvi	SCB_DATAPTR	call bcopy_7
10069928Sgibbs
10079928Sgibbs	mvi	DINDEX,STCNT
100813177Sgibbs	mvi	SCB_DATACNT	call bcopy_3
10099928Sgibbs
101013177Sgibbs	mov	SG_COUNT,SCB_SGCOUNT
10114568Sgibbs
10124568Sgibbs	mvi	DINDEX,SG_NEXT
101313177Sgibbs	mvi	SCB_SGPTR	call bcopy_4
10149928Sgibbs	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
102613177Sgibbs	mvi	DINDEX,SCB_SGPTR
10275775Sgibbs	mvi	SG_NEXT		call bcopy_4
10289928Sgibbs	
102913177Sgibbs	mvi	DINDEX,SCB_DATAPTR
10309928Sgibbs	mvi	SHADDR	call bcopy_4
10314568Sgibbs
103213177Sgibbs/*
103313177Sgibbs * Use the residual number since STCNT is corrupted by any message transfer
103413177Sgibbs */
103513177Sgibbs	mvi	SCB_RESID_DCNT	call bcopy_3
10364568Sgibbs	ret
10374568Sgibbs
103813177Sgibbs/*
103913177Sgibbs * Add the array base TARG_SCRATCH to the target offset (the target address
104013177Sgibbs * is in SCSIID), and return the result in SINDEX.  The accumulator
104113177Sgibbs * contains the 3->8 decoding of the target ID on return.
104213177Sgibbs */
10435562Sgibbsndx_dtr:
10444568Sgibbs	shr	A,SCSIID,4
104513177Sgibbs	test	SBLKCTL,SELBUSB	jz ndx_dtr_2
104613177Sgibbs	or	A,0x08		/* Channel B entries add 8 */
10475562Sgibbsndx_dtr_2:
104813177Sgibbs	add	SINDEX,TARG_SCRATCH,A
10494568Sgibbs
105013177Sgibbs	mov	FUNCTION1,SCSIID	/* 3-bit target address decode */
10514568Sgibbs	mov	A,FUNCTION1	ret
10524568Sgibbs
105313177Sgibbs/*
105413177Sgibbs * If we need to negotiate transfer parameters, build the WDTR or SDTR message
105513177Sgibbs * starting at the address passed in SINDEX.  DINDEX is modified on return.
105613177Sgibbs * The SCSI-II spec requires that Wide negotiation occur first and you can
105713177Sgibbs * only negotiat one or the other at a time otherwise in the event of a message
105813177Sgibbs * reject, you wouldn't be able to tell which message was the culpret.
105913177Sgibbs */
10605562Sgibbsmk_dtr:
106113177Sgibbs	test	SCB_CONTROL,0x90 jz return	/* NEEDWDTR|NEEDSDTR */
106213177Sgibbs	test	SCB_CONTROL,NEEDWDTR jnz  mk_wdtr_16bit
106313177Sgibbs	or	FLAGS, 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 */
107113177Sgibbs	test	FLAGS, MAXOFFSET jnz 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	xor	FLAGS, MAXOFFSET
108213177Sgibbs
108313177Sgibbs	/* Talking to a WIDE device? */
108413177Sgibbs	test	SCSIRATE, WIDEXFER	jnz wmax_offset	
10858567Sdg	mvi	DINDIR, MAX_OFFSET_8BIT
10867532Sgibbs	jmp	mk_sdtr_done
10877532Sgibbs
10888567Sdgwmax_offset:
108913177Sgibbs	mvi	DINDIR, MAX_OFFSET_16BIT
10908567Sdg	jmp	mk_sdtr_done
10918567Sdg
10925647Sgibbsmk_wdtr_16bit:
10935647Sgibbs	mvi	ARG_1,BUS_16_BIT
10945562Sgibbsmk_wdtr:
109513177Sgibbs	mvi	DINDIR,1		/* extended message */
109613177Sgibbs	mvi	DINDIR,2		/* extended message length = 2 */
109713177Sgibbs	mvi	DINDIR,3		/* WDTR code */
109813177Sgibbs	mov	DINDIR,ARG_1		/* bus width */
10995562Sgibbs
110013177Sgibbs	add	MSG_LEN,COMP_MSG0,DINDEX ret	/* update message length */
11015562Sgibbs	
110213177Sgibbs/*
110313177Sgibbs * Set SCSI bus control signal state.  This also saves the last-written
110413177Sgibbs * value into a location where the higher-level driver can read it - if
110513177Sgibbs * it has to send an ABORT or RESET message, then it needs to know this
110613177Sgibbs * so it can assert ATN without upsetting SCSISIGO.  The new value is
110713177Sgibbs * expected in SINDEX.  Change the actual state last to avoid contention
110813177Sgibbs * from the driver.
110913177Sgibbs */
11104568Sgibbsscsisig:
11114568Sgibbs	mov	SIGSTATE,SINDEX
11124568Sgibbs	mov	SCSISIGO,SINDEX	ret
11135562Sgibbs
11145562Sgibbssdtr_to_rate:
111513177Sgibbs	call	ndx_dtr			/* index scratch space for target */
11165562Sgibbs	shr	A,SINDIR,0x4
111713177Sgibbs	dec	SINDEX			/* Preserve SINDEX */
11185562Sgibbs	and	A,0x7
11195562Sgibbs	clr	RETURN_1
11205562Sgibbssdtr_to_rate_loop:
11215562Sgibbs	test	A,0x0f	jz sdtr_to_rate_done
112213177Sgibbs	add	RETURN_1,0x19
11235562Sgibbs	dec	A	
11245562Sgibbs	jmp	sdtr_to_rate_loop
11255562Sgibbssdtr_to_rate_done:
11265562Sgibbs	shr	RETURN_1,0x2
112713177Sgibbs	add	RETURN_1,0x19
112811829Sgibbs	test	SXFRCTL0,ULTRAEN jz return
112911829Sgibbs	shr	RETURN_1,0x1
11307532Sgibbsreturn:
11317532Sgibbs	ret
1132