aic7xxx.seq revision 13386
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
4413386SgibbsVERSION AIC7XXX_SEQ_VER "$Id: aic7xxx.seq,v 1.28 1996/01/09 16:14:03 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
42213386Sgibbs	mvi	DINDEX,HADDR
42313177Sgibbs/*
42413177Sgibbs * For Linux, we must throw away four bytes since there is a 32bit gap
42513177Sgibbs * in the middle of a struct scatterlist
42613177Sgibbs */
42713386Sgibbs#ifdef linux
42813177Sgibbs	call	bcopy_4_dfdat
42913177Sgibbs	mov	NONE,DFDAT
43013177Sgibbs	mov	NONE,DFDAT
43113177Sgibbs	mov	NONE,DFDAT
43213177Sgibbs	mov	NONE,DFDAT
43313177Sgibbs	call	bcopy_3_dfdat		/* Only support 24 bit length. */
43413177Sgibbs#else
43513177Sgibbs/*
43613177Sgibbs * For FreeBSD, just copy it wholesale
43713177Sgibbs */
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:
51513177Sgibbs	dec	DINDEX
51613386Sgibbs	or	CLRSINT0, CLRSPIORDY
5174568Sgibbs	mov	SCSIDATL,SINDIR
51813313Sgibbs	
5194568Sgibbsp_mesgout4:
52013177Sgibbs	test	DINDEX,0xff	jnz p_mesgout_loop
5214568Sgibbs
52213177Sgibbs/*
52313177Sgibbs * If the next bus phase after ATN drops is a message out, it means
52413177Sgibbs * that the target is requesting that the last message(s) be resent.
52513177Sgibbs */
52613177Sgibbsp_mesgout_snoop:
52713177Sgibbs	test	SSTAT1,BUSFREE	jnz p_mesgout_done
52813177Sgibbs	test	SSTAT1,REQINIT	jz p_mesgout_snoop
5294568Sgibbs
53013177Sgibbs	test	SSTAT1,PHASEMIS	jnz p_mesgout_done
5314568Sgibbs
53213177Sgibbs	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
53313177Sgibbs	call	scsisig				/* ATNO - re-assert ATN */
53413177Sgibbs
5354568Sgibbs	jmp	ITloop
5364568Sgibbs
53713177Sgibbsp_mesgout_phasemis:
53813177Sgibbs	mvi	CLRSINT1,CLRATNO	/* Be sure turn ATNO off */
53913177Sgibbsp_mesgout_done:
54013177Sgibbs	clr	MSG_LEN			/* no active msg */
5414568Sgibbs	jmp	ITloop
5424568Sgibbs
54313177Sgibbs/*
54413177Sgibbs * Message in phase.  Bytes are read using Automatic PIO mode.
54513177Sgibbs */
5464568Sgibbsp_mesgin:
54713177Sgibbs	mvi	A		call inb_first	/* read the 1st message byte */
54813177Sgibbs	mov	REJBYTE,A			/* save it for the driver */
5494568Sgibbs
55013177Sgibbs	test	A,MSG_IDENTIFY		jnz mesgin_identify
55113177Sgibbs	cmp	A,MSG_DISCONNECT	je mesgin_disconnect
55213177Sgibbs	cmp	A,MSG_SDPTRS		je mesgin_sdptrs
55313177Sgibbs	cmp	ALLZEROS,A		je mesgin_complete
55413177Sgibbs	cmp	A,MSG_RDPTRS		je mesgin_rdptrs
55513177Sgibbs	cmp	A,MSG_EXTENDED		je mesgin_extended
55613177Sgibbs	cmp	A,MSG_REJECT		je mesgin_reject
5574568Sgibbs
5589954Sgibbsrej_mesgin:
55913177Sgibbs/*
56013177Sgibbs * We have no idea what this message in is, and there's no way
56113177Sgibbs * to pass it up to the kernel, so we issue a message reject and
56213177Sgibbs * hope for the best.  Since we're now using manual PIO mode to
56313177Sgibbs * read in the message, there should no longer be a race condition
56413177Sgibbs * present when we assert ATN.  In any case, rejection should be a
56513177Sgibbs * rare occurrence - signal the driver when it happens.
56613177Sgibbs */
56713177Sgibbs	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
5689954Sgibbs	call	scsisig
56913177Sgibbs	mvi	INTSTAT,SEND_REJECT		/* let driver know */
5709954Sgibbs
57113177Sgibbs	mvi	MSG_REJECT	call mk_mesg
5729954Sgibbs
5739954Sgibbsmesgin_done:
57413177Sgibbs	call	inb_last			/*ack & turn auto PIO back on*/
5759954Sgibbs	jmp	ITloop
5769954Sgibbs
5779954Sgibbs
5789954Sgibbsmesgin_complete:
57913177Sgibbs/*
58013177Sgibbs * We got a "command complete" message, so put the SCB pointer
58113177Sgibbs * into the Queue Out, and trigger a completion interrupt.
58213177Sgibbs * Check status for non zero return and interrupt driver if needed
58313177Sgibbs * This allows the driver to interpret errors only when they occur
58413177Sgibbs * instead of always uploading the scb.  If the status is SCSI_CHECK,
58513177Sgibbs * the driver will download a new scb requesting sense to replace
58613177Sgibbs * the old one, modify the "waiting for selection" SCB list and set 
58713177Sgibbs * RETURN_1 to 0x80.  If RETURN_1 is set to 0x80 the sequencer imediately
58813177Sgibbs * jumps to main loop where it will run down the waiting SCB list.
58913177Sgibbs * If the kernel driver does not wish to request sense, it need
59013177Sgibbs * only clear RETURN_1, and the command is allowed to complete.  We don't 
59113177Sgibbs * bother to post to the QOUTFIFO in the error case since it would require 
59213177Sgibbs * extra work in the kernel driver to ensure that the entry was removed 
59313177Sgibbs * before the command complete code tried processing it.
59413177Sgibbs *
59513177Sgibbs * First check for residuals
59613177Sgibbs */
59713177Sgibbs	test	SCB_RESID_SGCNT,0xff	jz check_status
59813177Sgibbs/*
59913177Sgibbs * If we have a residual count, interrupt and tell the host.  Other
60013177Sgibbs * alternatives are to pause the sequencer on all command completes (yuck),
60113177Sgibbs * dma the resid directly to the host (slick, we may have space to do it now)
60213177Sgibbs * or have the sequencer pause itself when it encounters a non-zero resid 
60313177Sgibbs * (unecessary pause just to flag the command -yuck-, but takes one instruction
60413177Sgibbs * and since it shouldn't happen that often is good enough for our purposes).  
60513177Sgibbs */
60613177Sgibbsresid:
60713177Sgibbs	mvi	INTSTAT,RESIDUAL
6084568Sgibbs
6097532Sgibbscheck_status:
61013177Sgibbs	test	SCB_TARGET_STATUS,0xff	jz status_ok	/* Good Status? */
61113177Sgibbs	mvi	INTSTAT,BAD_STATUS			/* let driver know */
61213177Sgibbs	cmp	RETURN_1, SEND_SENSE	jne status_ok
6139954Sgibbs	jmp	mesgin_done
6145326Sgibbs
6154568Sgibbsstatus_ok:
61613177Sgibbs/* First, mark this target as free. */
61713177Sgibbs	test	SCB_CONTROL,TAG_ENB jnz test_immediate	/*
61813177Sgibbs							 * Tagged commands
61913177Sgibbs							 * don't busy the
62013177Sgibbs							 * target.
62113177Sgibbs							 */
62213177Sgibbs	mov	FUNCTION1,SCB_TCL
6235326Sgibbs	mov	A,FUNCTION1
62413177Sgibbs	test	SCB_TCL,0x88 jz clear_a
6255326Sgibbs	xor	ACTIVE_B,A
62613177Sgibbs	jmp	test_immediate
6275326Sgibbs
6285326Sgibbsclear_a:
6295326Sgibbs	xor	ACTIVE_A,A
6305326Sgibbs
63113177Sgibbstest_immediate:
63213177Sgibbs	test    SCB_CMDLEN,0xff jnz complete  /* Immediate message complete */
63313177Sgibbs/*
63413177Sgibbs * Pause the sequencer until the driver gets around to handling the command
63513177Sgibbs * complete.  This is so that any action that might require carefull timing
63613177Sgibbs * with the completion of this command can occur.
63713177Sgibbs */
6389810Sgibbs	mvi	INTSTAT,IMMEDDONE
63913177Sgibbs	jmp	start
6405326Sgibbscomplete:
6414568Sgibbs	mov	QOUTFIFO,SCBPTR
6427532Sgibbs	mvi	INTSTAT,CMDCMPLT
6439954Sgibbs	jmp	mesgin_done
6444568Sgibbs
6457532Sgibbs
64613177Sgibbs/*
64713177Sgibbs * Is it an extended message?  We only support the synchronous and wide data
64813177Sgibbs * transfer request messages, which will probably be in response to
64913177Sgibbs * WDTR or SDTR message outs from us.  If it's not SDTR or WDTR, reject it -
65013177Sgibbs * apparently this can be done after any message in byte, according
65113177Sgibbs * to the SCSI-2 spec.
65213177Sgibbs */
6539954Sgibbsmesgin_extended:
65413177Sgibbs	mvi	ARG_1		call inb_next	/* extended message length */
65513177Sgibbs	mvi	A		call inb_next	/* extended message code */
6564568Sgibbs
65713177Sgibbs	cmp	A,MSG_SDTR	je p_mesginSDTR
65813177Sgibbs	cmp	A,MSG_WDTR	je p_mesginWDTR
6599954Sgibbs	jmp	rej_mesgin
6605562Sgibbs
6615562Sgibbsp_mesginWDTR:
66213177Sgibbs	cmp	ARG_1,2		jne rej_mesgin	/* extended mesg length=2 */
66313177Sgibbs	mvi	ARG_1		call inb_next	/* Width of bus */
66413177Sgibbs	mvi	INTSTAT,WDTR_MSG		/* let driver know */
66513177Sgibbs	test	RETURN_1,0xff jz mesgin_done	/* Do we need to send WDTR? */
66613177Sgibbs	cmp	RETURN_1,SEND_REJ je rej_mesgin /*
66713177Sgibbs						 * Bus width was too large 
66813177Sgibbs						 * Reject it.
66913177Sgibbs						 */
6705562Sgibbs
67113177Sgibbs/* We didn't initiate the wide negotiation, so we must respond to the request */
67213177Sgibbs	and	RETURN_1,0x7f			/* Clear the SEND_WDTR Flag */
67313177Sgibbs	mvi	DINDEX,MSG0
67413177Sgibbs	mvi	MSG0	call mk_wdtr		/* build WDTR message */
67513177Sgibbs	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
6765562Sgibbs	call	scsisig
6779954Sgibbs	jmp	mesgin_done
6785562Sgibbs
6795562Sgibbsp_mesginSDTR:
68013177Sgibbs	cmp	ARG_1,3		jne rej_mesgin	/* extended mesg length=3 */
68113177Sgibbs	mvi	ARG_1		call inb_next	/* xfer period */
68213177Sgibbs	mvi	A		call inb_next	/* REQ/ACK offset */
68313177Sgibbs	mvi	INTSTAT,SDTR_MSG		/* call driver to convert */
6844568Sgibbs
68513177Sgibbs	test	RETURN_1,0xff	jz mesgin_done  /* Do we need to mk_sdtr/rej */
68613177Sgibbs	cmp	RETURN_1,SEND_REJ je rej_mesgin /*
68713177Sgibbs						 * Requested SDTR too small
68813177Sgibbs						 * Reject it.
68913177Sgibbs						 */
69013177Sgibbs	mvi	DINDEX, MSG0
69113177Sgibbs	mvi     MSG0     call mk_sdtr
69213177Sgibbs	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
6935562Sgibbs	call	scsisig
6949954Sgibbs	jmp	mesgin_done
6954568Sgibbs
69613177Sgibbs/*
69713177Sgibbs * Is it a disconnect message?  Set a flag in the SCB to remind us
69813177Sgibbs * and await the bus going free.
69913177Sgibbs */
7009954Sgibbsmesgin_disconnect:
70113177Sgibbs	or	SCB_CONTROL,DISCONNECTED
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:
71913177Sgibbs	and	FLAGS,0xfb			/*
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.
74213177Sgibbs * If we get one, we use the tag returned to switch to the proper
74313177Sgibbs * SCB.  Otherwise, we just use the findSCB method.
74413177Sgibbs */
74513177Sgibbssnoop_tag_loop:
74613177Sgibbs	test	SSTAT1,BUSFREE	jnz use_findSCB
74713177Sgibbs	test	SSTAT1,REQINIT	jz snoop_tag_loop
74813177Sgibbs	test	SSTAT1,PHASEMIS	jnz use_findSCB
74913177Sgibbs	mvi	A		call inb_first
75013177Sgibbs	cmp	A,MSG_SIMPLE_TAG je get_tag
75113177Sgibbsuse_findSCB:
75213177Sgibbs	mov	ALLZEROS	call findSCB	  /* Have to search */
7536608Sgibbssetup_SCB:
75413177Sgibbs	and	SCB_CONTROL,0xfb	  /* clear disconnect bit in SCB */
75513177Sgibbs	or	FLAGS,IDENTIFY_SEEN	  /* make note of IDENTIFY */
7566608Sgibbs	jmp	ITloop
7576608Sgibbsget_tag:
75813177Sgibbs	mvi	ARG_1	call inb_next	/* tag value */
75913177Sgibbs/*
76013177Sgibbs * See if the tag is in range.  The tag is < SCBCOUNT if we add
76113177Sgibbs * the complement of SCBCOUNT to the incomming tag and there is
76213177Sgibbs * no carry.
76313177Sgibbs */
76413177Sgibbs	mov	A,COMP_SCBCOUNT	
76513177Sgibbs	add	SINDEX,A,ARG_1
76613177Sgibbs	jc	abort_tag
76713177Sgibbs
76813177Sgibbs/*
76913177Sgibbs * Ensure that the SCB the tag points to is for an SCB transaction
77013177Sgibbs * to the reconnecting target.
77113177Sgibbs */
77213177Sgibbs	mov	SCBPTR,ARG_1
7736608Sgibbs	mov	A,SAVED_TCL
77413177Sgibbs	cmp	SCB_TCL,A		jne abort_tag
77513177Sgibbs	test	SCB_CONTROL,TAG_ENB	jz  abort_tag
77613177Sgibbs	call	inb_last			/* Ack Successful tag */
77713177Sgibbs	jmp	setup_SCB
7787700Sgibbsabort_tag:
77913177Sgibbs	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
7807700Sgibbs	call	scsisig
78113177Sgibbs	mvi	INTSTAT,ABORT_TAG 		/* let driver know */
78213177Sgibbs	mvi	0xd		call mk_mesg	/* ABORT TAG message */
78313177Sgibbs	jmp	mesgin_done
7846608Sgibbs
78513177Sgibbs/*
78613177Sgibbs * Message reject?  Let the kernel driver handle this.  If we have an 
78713177Sgibbs * outstanding WDTR or SDTR negotiation, assume that it's a response from 
78813177Sgibbs * the target selecting 8bit or asynchronous transfer, otherwise just ignore 
78913177Sgibbs * it since we have no clue what it pertains to.
79013177Sgibbs */
7919954Sgibbsmesgin_reject:
79213177Sgibbs	mvi	INTSTAT, REJECT_MSG
7939954Sgibbs	jmp	mesgin_done
7945562Sgibbs
79513177Sgibbs/*
79613177Sgibbs * [ ADD MORE MESSAGE HANDLING HERE ]
79713177Sgibbs */
7984568Sgibbs
79913177Sgibbs/*
80013177Sgibbs * Bus free phase.  It might be useful to interrupt the device
80113177Sgibbs * driver if we aren't expecting this.  For now, make sure that
80213177Sgibbs * ATN isn't being asserted and look for a new command.
80313177Sgibbs */
8044568Sgibbsp_busfree:
80513177Sgibbs	mvi	CLRSINT1,CLRATNO
8068567Sdg
80713177Sgibbs/*
80813177Sgibbs * if this is an immediate command, perform a psuedo command complete to
80913177Sgibbs * notify the driver.
81013177Sgibbs */
81113177Sgibbs	test	SCB_CMDLEN,0xff	jz status_ok
81213177Sgibbs	jmp	start
8134568Sgibbs
81413177Sgibbs/*
81513177Sgibbs * Instead of a generic bcopy routine that requires an argument, we unroll
81613177Sgibbs * the cases that are actually used, and call them explicitly.  This
81713177Sgibbs * not only reduces the overhead of doing a bcopy, but ends up saving space 
81813177Sgibbs * in the program since you don't have to put the argument into the accumulator
81913177Sgibbs * before the call.  Both functions expect DINDEX to contain the destination 
82013177Sgibbs * address and SINDEX to contain the source address.
82113177Sgibbs */
8229928Sgibbsbcopy_7:
8234568Sgibbs	mov	DINDIR,SINDIR
8245775Sgibbs	mov	DINDIR,SINDIR
8259928Sgibbsbcopy_5:
8269928Sgibbs	mov	DINDIR,SINDIR
8275775Sgibbsbcopy_4:
8285775Sgibbs	mov	DINDIR,SINDIR
8299928Sgibbsbcopy_3:
8305775Sgibbs	mov	DINDIR,SINDIR
8315775Sgibbs	mov	DINDIR,SINDIR
8325775Sgibbs	mov	DINDIR,SINDIR	ret
8335775Sgibbs	
8349928Sgibbsbcopy_7_dfdat:
8357532Sgibbs	mov	DINDIR,DFDAT
8367532Sgibbs	mov	DINDIR,DFDAT
8379928Sgibbsbcopy_5_dfdat:
8389928Sgibbs	mov	DINDIR,DFDAT
8397532Sgibbsbcopy_4_dfdat:
8407532Sgibbs	mov	DINDIR,DFDAT
8419928Sgibbsbcopy_3_dfdat:
8427532Sgibbs	mov	DINDIR,DFDAT
8437532Sgibbs	mov	DINDIR,DFDAT
8447532Sgibbs	mov	DINDIR,DFDAT	ret
8457532Sgibbs
84613177Sgibbs/*
84713177Sgibbs * Locking the driver out, build a one-byte message passed in SINDEX
84813177Sgibbs * if there is no active message already.  SINDEX is returned intact.
84913177Sgibbs */
8504568Sgibbsmk_mesg:
85113177Sgibbs	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
85213177Sgibbs	test	MSG_LEN,0xff	jz mk_mesg1	/* Should always succeed */
85313177Sgibbs	
85413177Sgibbs	/*
85513177Sgibbs	 * Hmmm.  For some reason the mesg buffer is in use.
85613177Sgibbs	 * Tell the driver.  It should look at SINDEX to find
85713177Sgibbs	 * out what we wanted to use the buffer for and resolve
85813177Sgibbs	 * the conflict.
85913177Sgibbs	 */
86013177Sgibbs	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
86113177Sgibbs	mvi	INTSTAT,MSG_BUFFER_BUSY ret
8624568Sgibbs
8634568Sgibbsmk_mesg1:
86413177Sgibbs	mvi	MSG_LEN,1		/* length = 1 */
86513177Sgibbs	mov	MSG0,SINDEX		/* 1-byte message */
86613177Sgibbs	mvi	SEQCTL,0x10	ret	/* !PAUSEDIS|FASTMODE */
8674568Sgibbs
86813177Sgibbs/*
86913177Sgibbs * Functions to read data in Automatic PIO mode.
87013177Sgibbs *
87113177Sgibbs * According to Adaptec's documentation, an ACK is not sent on input from
87213177Sgibbs * the target until SCSIDATL is read from.  So we wait until SCSIDATL is
87313177Sgibbs * latched (the usual way), then read the data byte directly off the bus
87413177Sgibbs * using SCSIBUSL.  When we have pulled the ATN line, or we just want to
87513177Sgibbs * acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
87613177Sgibbs * spec guarantees that the target will hold the data byte on the bus until
87713177Sgibbs * we send our ACK.
87813177Sgibbs *
87913177Sgibbs * The assumption here is that these are called in a particular sequence,
88013177Sgibbs * and that REQ is already set when inb_first is called.  inb_{first,next}
88113177Sgibbs * use the same calling convention as inb.
88213177Sgibbs */
88313177Sgibbs
88413177Sgibbsinb_next:
88513386Sgibbs	or	CLRSINT0, CLRSPIORDY
88613360Sgibbs	mov	NONE,SCSIDATL			/*dummy read from latch to ACK*/
88713360Sgibbsinb_next_wait:
88813360Sgibbs	test	SSTAT1,PHASEMIS	jnz mesgin_phasemis
88913360Sgibbs	test	SSTAT0,SPIORDY	jz inb_next_wait /* wait for next byte */
89013313Sgibbsinb_first:
8914568Sgibbs	mov	DINDEX,SINDEX
89213177Sgibbs	mov	DINDIR,SCSIBUSL	ret		/*read byte directly from bus*/
89313177Sgibbsinb_last:
89413360Sgibbs	mov	NONE,SCSIDATL ret		/*dummy read from latch to ACK*/
8954568Sgibbs
89613177Sgibbsmesgin_phasemis:
89713177Sgibbs/*
89813177Sgibbs * We expected to receive another byte, but the target changed phase
89913177Sgibbs */
90013177Sgibbs	mvi	INTSTAT, MSGIN_PHASEMIS
90113177Sgibbs	jmp	ITloop
9024568Sgibbs
90313177Sgibbs/*
90413177Sgibbs * DMA data transfer.  HADDR and HCNT must be loaded first, and
90513177Sgibbs * SINDEX should contain the value to load DFCNTRL with - 0x3d for
90613177Sgibbs * host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
90713177Sgibbs * during initialization.
90813177Sgibbs */
9094568Sgibbsdma:
9104568Sgibbs	mov	DFCNTRL,SINDEX
9114568Sgibbsdma1:
91213177Sgibbs	test	SSTAT0,DMADONE	jnz dma3
91313177Sgibbs	test	SSTAT1,PHASEMIS	jz dma1		/* ie. underrun */
9144568Sgibbs
91513177Sgibbs/*
91613177Sgibbs * We will be "done" DMAing when the transfer count goes to zero, or
91713177Sgibbs * the target changes the phase (in light of this, it makes sense that
91813177Sgibbs * the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
91913177Sgibbs * doing a SCSI->Host transfer, the data FIFO should be flushed auto-
92013177Sgibbs * magically on STCNT=0 or a phase change, so just wait for FIFO empty
92113177Sgibbs * status.
92213177Sgibbs */
9234568Sgibbsdma3:
92413177Sgibbs	test	SINDEX,DIRECTION	jnz dma5
9254568Sgibbsdma4:
92613177Sgibbs	test	DFSTATUS,FIFOEMP	jz dma4
9274568Sgibbs
92813177Sgibbs/*
92913177Sgibbs * Now shut the DMA enables off and make sure that the DMA enables are 
93013177Sgibbs * actually off first lest we get an ILLSADDR.
93113177Sgibbs */
9324568Sgibbsdma5:
93313177Sgibbs	/* disable DMA, but maintain WIDEODD */
93413177Sgibbs	and	A, WIDEODD, SINDEX	
93513177Sgibbs	mov	DFCNTRL, A
9364568Sgibbsdma6:
93713177Sgibbs	test	DFCNTRL,0x38	jnz dma6  /* SCSIENACK|SDMAENACK|HDMAENACK */
9384568Sgibbs
9394568Sgibbs	ret
9404568Sgibbs
9417532Sgibbsdma_finish:
94213177Sgibbs	test	DFSTATUS,HDONE	jz dma_finish
9437532Sgibbs
94413177Sgibbs	clr	DFCNTRL				/* disable DMA */
9457532Sgibbsdma_finish2:
94613177Sgibbs	test	DFCNTRL,HDMAENACK jnz dma_finish2
9477532Sgibbs	ret
9487532Sgibbs
94913177Sgibbs/*
95013177Sgibbs * Common SCSI initialization for selection and reselection.  Expects
95113177Sgibbs * the target SCSI ID to be in the upper four bits of SINDEX, and A's
95213177Sgibbs * contents are stomped on return.
95313177Sgibbs */
9548104Sgibbsinitialize_scsiid:
95513177Sgibbs	and	SINDEX,0xf0		/* Get target ID */
9565775Sgibbs	and	A,0x0f,SCSIID
9575775Sgibbs	or	SINDEX,A
9588104Sgibbs	mov	SCSIID,SINDEX ret
9595326Sgibbs
96013177Sgibbs/*
96113177Sgibbs * Assert that if we've been reselected, then we've seen an IDENTIFY
96213177Sgibbs * message.
96313177Sgibbs */
9644568Sgibbsassert:
96513177Sgibbs	test	FLAGS,RESELECTED	jz return	/* reselected? */
96613177Sgibbs	test	FLAGS,IDENTIFY_SEEN	jnz return	/* seen IDENTIFY? */
9674568Sgibbs
96813177Sgibbs	mvi	INTSTAT,NO_IDENT 	ret	/* no - cause a kernel panic */
9694568Sgibbs
97013177Sgibbs/*
97113177Sgibbs * Locate the SCB matching the target ID/channel/lun in SAVED_TCL and switch 
97213177Sgibbs * the SCB to it.  Have the kernel print a warning message if it can't be 
97313177Sgibbs * found, and generate an ABORT message to the target.  SINDEX should be
97413177Sgibbs * cleared on call.
97513177Sgibbs */
9764568SgibbsfindSCB:
9776608Sgibbs	mov	A,SAVED_TCL
97813177Sgibbs	mov	SCBPTR,SINDEX			/* switch to new SCB */
97913177Sgibbs	cmp	SCB_TCL,A	jne findSCB1 /* target ID/channel/lun match? */
98013177Sgibbs	test	SCB_CONTROL,DISCONNECTED jz findSCB1 /*should be disconnected*/
9814568Sgibbs	ret
9824568Sgibbs
9836608SgibbsfindSCB1:
9844568Sgibbs	inc	SINDEX
9854568Sgibbs	mov	A,SCBCOUNT
9866608Sgibbs	cmp	SINDEX,A	jne findSCB
9874568Sgibbs
98813177Sgibbs	mvi	INTSTAT,NO_MATCH		/* not found - signal kernel */
98913177Sgibbs	mvi	MSG_ABORT	call mk_mesg	/* ABORT message */
9904568Sgibbs
99113177Sgibbs	or	SINDEX,0x10,SIGSTATE		/* assert ATNO */
9924568Sgibbs	call	scsisig
9934568Sgibbs	ret
99413177Sgibbs/*
99513177Sgibbs * Make a working copy of the scatter-gather parameters from the SCB.
99613177Sgibbs */
9974568Sgibbssg_scb2ram:
9989928Sgibbs	mvi	DINDEX,HADDR
99913177Sgibbs	mvi	SCB_DATAPTR	call bcopy_7
10009928Sgibbs
10019928Sgibbs	mvi	DINDEX,STCNT
100213177Sgibbs	mvi	SCB_DATACNT	call bcopy_3
10039928Sgibbs
100413177Sgibbs	mov	SG_COUNT,SCB_SGCOUNT
10054568Sgibbs
10064568Sgibbs	mvi	DINDEX,SG_NEXT
100713177Sgibbs	mvi	SCB_SGPTR	call bcopy_4
10089928Sgibbs	ret
10094568Sgibbs
101013177Sgibbs/*
101113177Sgibbs * Copying RAM values back to SCB, for Save Data Pointers message, but
101213177Sgibbs * only if we've actually been into a data phase to change them.  This
101313177Sgibbs * protects against bogus data in scratch ram and the residual counts
101413177Sgibbs * since they are only initialized when we go into data_in or data_out.
101513177Sgibbs */
10164568Sgibbssg_ram2scb:
10179928Sgibbs	test	FLAGS, DPHASE	jz return
101813177Sgibbs	mov	SCB_SGCOUNT,SG_COUNT
10194568Sgibbs
102013177Sgibbs	mvi	DINDEX,SCB_SGPTR
10215775Sgibbs	mvi	SG_NEXT		call bcopy_4
10229928Sgibbs	
102313177Sgibbs	mvi	DINDEX,SCB_DATAPTR
10249928Sgibbs	mvi	SHADDR	call bcopy_4
10254568Sgibbs
102613177Sgibbs/*
102713177Sgibbs * Use the residual number since STCNT is corrupted by any message transfer
102813177Sgibbs */
102913177Sgibbs	mvi	SCB_RESID_DCNT	call bcopy_3
10304568Sgibbs	ret
10314568Sgibbs
103213177Sgibbs/*
103313177Sgibbs * Add the array base TARG_SCRATCH to the target offset (the target address
103413177Sgibbs * is in SCSIID), and return the result in SINDEX.  The accumulator
103513177Sgibbs * contains the 3->8 decoding of the target ID on return.
103613177Sgibbs */
10375562Sgibbsndx_dtr:
10384568Sgibbs	shr	A,SCSIID,4
103913177Sgibbs	test	SBLKCTL,SELBUSB	jz ndx_dtr_2
104013177Sgibbs	or	A,0x08		/* Channel B entries add 8 */
10415562Sgibbsndx_dtr_2:
104213177Sgibbs	add	SINDEX,TARG_SCRATCH,A
10434568Sgibbs
104413177Sgibbs	mov	FUNCTION1,SCSIID	/* 3-bit target address decode */
10454568Sgibbs	mov	A,FUNCTION1	ret
10464568Sgibbs
104713177Sgibbs/*
104813177Sgibbs * If we need to negotiate transfer parameters, build the WDTR or SDTR message
104913177Sgibbs * starting at the address passed in SINDEX.  DINDEX is modified on return.
105013177Sgibbs * The SCSI-II spec requires that Wide negotiation occur first and you can
105113177Sgibbs * only negotiat one or the other at a time otherwise in the event of a message
105213177Sgibbs * reject, you wouldn't be able to tell which message was the culpret.
105313177Sgibbs */
10545562Sgibbsmk_dtr:
105513177Sgibbs	test	SCB_CONTROL,0x90 jz return	/* NEEDWDTR|NEEDSDTR */
105613177Sgibbs	test	SCB_CONTROL,NEEDWDTR jnz  mk_wdtr_16bit
105713177Sgibbs	or	FLAGS, MAXOFFSET	/* Force an offset of 15 or 8 if WIDE */
10584568Sgibbs
10597532Sgibbsmk_sdtr:
106013177Sgibbs	mvi	DINDIR,1		/* extended message */
106113177Sgibbs	mvi	DINDIR,3		/* extended message length = 3 */
106213177Sgibbs	mvi	DINDIR,1		/* SDTR code */
10637532Sgibbs	call	sdtr_to_rate
106413177Sgibbs	mov	DINDIR,RETURN_1		/* REQ/ACK transfer period */
106513177Sgibbs	test	FLAGS, MAXOFFSET jnz mk_sdtr_max_offset
106613177Sgibbs	and	DINDIR,0x0f,SINDIR	/* Sync Offset */
10677532Sgibbs
10687532Sgibbsmk_sdtr_done:
106913177Sgibbs	add	MSG_LEN,COMP_MSG0,DINDEX ret	/* update message length */
10707532Sgibbs
10718567Sdgmk_sdtr_max_offset:
107213177Sgibbs/*
107313177Sgibbs * We're initiating sync negotiation, so request the max offset we can (15 or 8)
107413177Sgibbs */
107513177Sgibbs	xor	FLAGS, MAXOFFSET
107613177Sgibbs
107713177Sgibbs	/* Talking to a WIDE device? */
107813177Sgibbs	test	SCSIRATE, WIDEXFER	jnz wmax_offset	
10798567Sdg	mvi	DINDIR, MAX_OFFSET_8BIT
10807532Sgibbs	jmp	mk_sdtr_done
10817532Sgibbs
10828567Sdgwmax_offset:
108313177Sgibbs	mvi	DINDIR, MAX_OFFSET_16BIT
10848567Sdg	jmp	mk_sdtr_done
10858567Sdg
10865647Sgibbsmk_wdtr_16bit:
10875647Sgibbs	mvi	ARG_1,BUS_16_BIT
10885562Sgibbsmk_wdtr:
108913177Sgibbs	mvi	DINDIR,1		/* extended message */
109013177Sgibbs	mvi	DINDIR,2		/* extended message length = 2 */
109113177Sgibbs	mvi	DINDIR,3		/* WDTR code */
109213177Sgibbs	mov	DINDIR,ARG_1		/* bus width */
10935562Sgibbs
109413177Sgibbs	add	MSG_LEN,COMP_MSG0,DINDEX ret	/* update message length */
10955562Sgibbs	
109613177Sgibbs/*
109713177Sgibbs * Set SCSI bus control signal state.  This also saves the last-written
109813177Sgibbs * value into a location where the higher-level driver can read it - if
109913177Sgibbs * it has to send an ABORT or RESET message, then it needs to know this
110013177Sgibbs * so it can assert ATN without upsetting SCSISIGO.  The new value is
110113177Sgibbs * expected in SINDEX.  Change the actual state last to avoid contention
110213177Sgibbs * from the driver.
110313177Sgibbs */
11044568Sgibbsscsisig:
11054568Sgibbs	mov	SIGSTATE,SINDEX
11064568Sgibbs	mov	SCSISIGO,SINDEX	ret
11075562Sgibbs
11085562Sgibbssdtr_to_rate:
110913177Sgibbs	call	ndx_dtr			/* index scratch space for target */
11105562Sgibbs	shr	A,SINDIR,0x4
111113177Sgibbs	dec	SINDEX			/* Preserve SINDEX */
11125562Sgibbs	and	A,0x7
11135562Sgibbs	clr	RETURN_1
11145562Sgibbssdtr_to_rate_loop:
11155562Sgibbs	test	A,0x0f	jz sdtr_to_rate_done
111613177Sgibbs	add	RETURN_1,0x19
11175562Sgibbs	dec	A	
11185562Sgibbs	jmp	sdtr_to_rate_loop
11195562Sgibbssdtr_to_rate_done:
11205562Sgibbs	shr	RETURN_1,0x2
112113177Sgibbs	add	RETURN_1,0x19
112211829Sgibbs	test	SXFRCTL0,ULTRAEN jz return
112311829Sgibbs	shr	RETURN_1,0x1
11247532Sgibbsreturn:
11257532Sgibbs	ret
1126