aic7xxx.seq revision 15998
113177Sgibbs/*+M***********************************************************************
213177Sgibbs *Adaptec 274x/284x/294x device driver for Linux and FreeBSD.
313177Sgibbs *
413177Sgibbs *Copyright (c) 1994 John Aycock
513177Sgibbs *  The University of Calgary Department of Computer Science.
613177Sgibbs *  All rights reserved.
713177Sgibbs *
815328Sgibbs *FreeBSD, Twin, Wide, 2 command per target support, tagged queuing,
915328Sgibbs *SCB paging and other optimizations:
1015328Sgibbs *Copyright (c) 1994, 1995, 1996 Justin Gibbs. All rights reserved.
1113177Sgibbs *
1213177Sgibbs *Redistribution and use in source and binary forms, with or without
1313177Sgibbs *modification, are permitted provided that the following conditions
1413177Sgibbs *are met:
1513177Sgibbs *1. Redistributions of source code must retain the above copyright
1613177Sgibbs *   notice, this list of conditions, and the following disclaimer.
1713177Sgibbs *2. Redistributions in binary form must reproduce the above copyright
1813177Sgibbs *   notice, this list of conditions and the following disclaimer in the
1913177Sgibbs *   documentation and/or other materials provided with the distribution.
2013177Sgibbs *3. All advertising materials mentioning features or use of this software
2113177Sgibbs *   must display the following acknowledgement:
2213177Sgibbs *     This product includes software developed by the University of Calgary
2313177Sgibbs *     Department of Computer Science and its contributors.
2413177Sgibbs *4. Neither the name of the University nor the names of its contributors
2513177Sgibbs *   may be used to endorse or promote products derived from this software
2613177Sgibbs *   without specific prior written permission.
2713177Sgibbs *
2813177Sgibbs *THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2913177Sgibbs *ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3013177Sgibbs *IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3113177Sgibbs *ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3213177Sgibbs *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3313177Sgibbs *DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3413177Sgibbs *OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3513177Sgibbs *HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3613177Sgibbs *LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3713177Sgibbs *OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3813177Sgibbs *SUCH DAMAGE.
3913177Sgibbs *
4013177Sgibbs *-M************************************************************************/
414568Sgibbs
4215998SgibbsVERSION AIC7XXX_SEQ_VER "$Id: aic7xxx.seq,v 1.37 1996/05/27 23:16:55 gibbs Exp $"
435647Sgibbs
4415998Sgibbs#if defined(__NetBSD__)
4515998Sgibbs#include "../../../../dev/ic/aic7xxxreg.h"
4615998Sgibbs#elif defined(__FreeBSD__)
4713177Sgibbs#include "../../dev/aic7xxx/aic7xxx_reg.h"
4815998Sgibbs#endif
494568Sgibbs
5013177Sgibbs/*
5113177Sgibbs * We can't just use ACCUM in the sequencer code because it
5213177Sgibbs * must be treated specially by the assembler, and it currently
5313690Sgibbs * looks for the symbol 'A'.  This is the only register defined in
5413177Sgibbs * the assembler's symbol space.
5513177Sgibbs */
5613177SgibbsA = ACCUM
574568Sgibbs
5813690Sgibbs/* After starting the selection hardware, we check for reconnecting targets
5913690Sgibbs * as well as for our selection to complete just in case the reselection wins
6013690Sgibbs * bus arbitration.  The problem with this is that we must keep track of the
6113690Sgibbs * SCB that we've already pulled from the QINFIFO and started the selection
6213690Sgibbs * on just in case the reselection wins so that we can retry the selection at
6313690Sgibbs * a later time.  This problem cannot be resolved by holding a single entry
6413690Sgibbs * in scratch ram since a reconnecting target can request sense and this will
6513690Sgibbs * create yet another SCB waiting for selection.  The solution used here is to 
6613690Sgibbs * use byte 27 of the SCB as a psuedo-next pointer and to thread a list
6713177Sgibbs * of SCBs that are awaiting selection.  Since 0-0xfe are valid SCB offsets, 
6813177Sgibbs * SCB_LIST_NULL is 0xff which is out of range.  The kernel driver must
6913177Sgibbs * add an entry to this list everytime a request sense occurs.  The sequencer
7013177Sgibbs * will automatically consume the entries.
7113177Sgibbs */
724568Sgibbs
7313177Sgibbs/*
7414449Sgibbs * We assume that the kernel driver may reset us at any time, even in the
7514449Sgibbs * middle of a DMA, so clear DFCNTRL too.
7613177Sgibbs */
7714449Sgibbsreset:
7814449Sgibbs	clr	DFCNTRL
7914449Sgibbs	clr	SCSISIGO		/* De-assert BSY */
8014449Sgibbs/*
8114449Sgibbs * We jump to start after every bus free.
8214449Sgibbs */
8313177Sgibbsstart:
8415328Sgibbs	and	FLAGS,0x0f		/* clear target specific flags */
8513177Sgibbs	mvi	SCSISEQ,ENRSELI		/* Always allow reselection */
868104Sgibbspoll_for_work:
8713177Sgibbs	/*
8813177Sgibbs	 * Are we a twin channel device?
8913177Sgibbs	 * For fairness, we check the other bus first,
9013177Sgibbs	 * since we just finished a transaction on the
9113177Sgibbs	 * current channel.
9213177Sgibbs	 */
9313177Sgibbs	test	FLAGS,TWIN_BUS	jz start2
9413177Sgibbs	xor	SBLKCTL,SELBUSB			/* Toggle to the other bus */
958104Sgibbs	test	SSTAT0,SELDI	jnz reselect
9613177Sgibbs	xor	SBLKCTL,SELBUSB			/* Toggle to the original bus */
975326Sgibbsstart2:
988104Sgibbs	test	SSTAT0,SELDI	jnz reselect
999395Sgibbs	cmp	WAITING_SCBH,SCB_LIST_NULL jne start_waiting
10014934Sgibbs	mov	A, QCNTMASK
10114934Sgibbs	test	QINCNT,A	jz poll_for_work
1024568Sgibbs
10313690Sgibbs/*
10413690Sgibbs * We have at least one queued SCB now and we don't have any 
10513177Sgibbs * SCBs in the list of SCBs awaiting selection.  Set the SCB
10613177Sgibbs * pointer from the FIFO so we see the right bank of SCB 
10713177Sgibbs * registers.
10813177Sgibbs */
1094568Sgibbs	mov	SCBPTR,QINFIFO
1104568Sgibbs
11113177Sgibbs/*
11213177Sgibbs * See if there is not already an active SCB for this target.  This code
11313177Sgibbs * locks out on a per target basis instead of target/lun.  Although this
11413177Sgibbs * is not ideal for devices that have multiple luns active at the same
11513177Sgibbs * time, it is faster than looping through all SCB's looking for active
11613177Sgibbs * commands.  It may be benificial to make findscb a more general procedure
11713177Sgibbs * to see if the added cost of the search is negligible.  This code also 
11813177Sgibbs * assumes that the kernel driver will clear the active flags on board 
11913177Sgibbs * initialization, board reset, and a target SELTO.  Tagged commands
12013690Sgibbs * don't set the active bits since you can queue more than one command
12113690Sgibbs * at a time.  We do, however, look to see if there are any non-tagged
12213690Sgibbs * I/Os in progress, and requeue the command if there are.  Tagged and
12313690Sgibbs * non-tagged commands cannot be mixed to a single target.
12413177Sgibbs */
1254568Sgibbs
1265647Sgibbstest_busy:
12713177Sgibbs	mov	FUNCTION1,SCB_TCL
1284568Sgibbs	mov	A,FUNCTION1
12913177Sgibbs	test	SCB_TCL,0x88	jz test_a	/* Id < 8 && A channel */
1305326Sgibbs
1315326Sgibbs	test	ACTIVE_B,A	jnz requeue
13213177Sgibbs	test	SCB_CONTROL,TAG_ENB	jnz start_scb
13313177Sgibbs	/* Mark the current target as busy */
13413177Sgibbs	or	ACTIVE_B,A
1355326Sgibbs	jmp	start_scb
1365326Sgibbs
13713177Sgibbs/* Place the currently active SCB back on the queue for later processing */
1385326Sgibbsrequeue:
1394568Sgibbs	mov	QINFIFO, SCBPTR
1408104Sgibbs	jmp	poll_for_work
1414568Sgibbs
14213177Sgibbs/*
14313177Sgibbs * Pull the first entry off of the waiting for selection list
14413177Sgibbs * We don't have to "test_busy" because only transactions that
14513177Sgibbs * have passed that test can be in the waiting_scb list.
14613177Sgibbs */
1478104Sgibbsstart_waiting:
1488104Sgibbs	mov	SCBPTR,WAITING_SCBH
14913177Sgibbs	jmp	start_scb2
1508104Sgibbs
1515326Sgibbstest_a:
15213177Sgibbs	test	ACTIVE_A,A jnz requeue
15313177Sgibbs	test	SCB_CONTROL,TAG_ENB jnz start_scb
15413177Sgibbs	/* Mark the current target as busy */
15513177Sgibbs	or	ACTIVE_A,A
1565326Sgibbs
1575326Sgibbsstart_scb:
15815328Sgibbs	mov	SCB_NEXT,WAITING_SCBH
15913177Sgibbs	mov	WAITING_SCBH, SCBPTR
16013177Sgibbsstart_scb2:
16113177Sgibbs	and	SINDEX,0xf7,SBLKCTL	/* Clear the channel select bit */
16213177Sgibbs	and	A,0x08,SCB_TCL		/* Get new channel bit */
16313177Sgibbs	or	SINDEX,A
16413177Sgibbs	mov	SBLKCTL,SINDEX		/* select channel */
16513177Sgibbs	mov	SCB_TCL	call initialize_scsiid
1668104Sgibbs
16713177Sgibbs/*
16813177Sgibbs * Enable selection phase as an initiator, and do automatic ATN
16913177Sgibbs * after the selection.  We do this now so that we can overlap the
17013177Sgibbs * rest of our work to set up this target with the arbitration and
17113177Sgibbs * selection bus phases.
17213177Sgibbs */
1738104Sgibbsstart_selection:
17413177Sgibbs	mvi	SCSISEQ,0x58		/* ENSELO|ENAUTOATNO|ENRSELI */
1754568Sgibbs
17613177Sgibbs/*
17713177Sgibbs * As soon as we get a successful selection, the target should go
17813177Sgibbs * into the message out phase since we have ATN asserted.  Prepare
17913177Sgibbs * the message to send.
18013177Sgibbs *
18113177Sgibbs * Messages are stored in scratch RAM starting with a length byte
18213177Sgibbs * followed by the message itself.
18313177Sgibbs */
18413177Sgibbs	test	SCB_CMDLEN,0xff jnz mk_identify	/* 0 Length Command? */
1858567Sdg
18613177Sgibbs/*
18713177Sgibbs * The kernel has sent us an SCB with no command attached.  This implies
18813177Sgibbs * that the kernel wants to send a message of some sort to this target,
18913177Sgibbs * so we interrupt the driver, allow it to fill the message buffer, and
19013177Sgibbs * then go back into the arbitration loop
19113177Sgibbs */
1928567Sdg	mvi     INTSTAT,AWAITING_MSG
1939917Sgibbs	jmp     wait_for_selection
1948567Sdg
19513177Sgibbsmk_identify:
19613177Sgibbs	and	A,DISCENB,SCB_CONTROL	/* mask off disconnect privledge */
1974568Sgibbs
19813690Sgibbs	and	MSG0,0x7,SCB_TCL	/* lun */
19913690Sgibbs	or	MSG0,A			/* or in disconnect privledge */
20013690Sgibbs	or	MSG0,MSG_IDENTIFY
20113690Sgibbs	mvi	MSG_LEN, 1
2024568Sgibbs
20313177Sgibbs	test	SCB_CONTROL,0xb0 jz  !message	/* WDTR, SDTR or TAG?? */
20413177Sgibbs/*
20515328Sgibbs * Send a tag message if TAG_ENB is set in the SCB control block.
20615328Sgibbs * Use SCB_TAG (the position in the kernel's SCB array) as the tag value.
20713177Sgibbs */
2084568Sgibbs
2096608Sgibbsmk_tag:
21013177Sgibbs	mvi	DINDEX, MSG1
21113177Sgibbs	test	SCB_CONTROL,TAG_ENB jz mk_tag_done
21215328Sgibbs	and	DINDIR,0x23,SCB_CONTROL
21315328Sgibbs	mov	DINDIR,SCB_TAG
2146608Sgibbs
21513177Sgibbs	add	MSG_LEN,COMP_MSG0,DINDEX	/* update message length */
2166608Sgibbs
2176608Sgibbsmk_tag_done:
2186608Sgibbs
21913690Sgibbs	test	SCB_CONTROL,0x90 jz !message	/* NEEDWDTR|NEEDSDTR */
22013177Sgibbs	mov	DINDEX	call mk_dtr	/* build DTR message if needed */
2216608Sgibbs
2224568Sgibbs!message:
2239917Sgibbswait_for_selection:
22413177Sgibbs	test	SSTAT0,SELDO	jnz select 
22513177Sgibbs	test	SSTAT0,SELDI	jz wait_for_selection
2264568Sgibbs
22713177Sgibbs/*
22813177Sgibbs * Reselection has been initiated by a target. Make a note that we've been
22913177Sgibbs * reselected, but haven't seen an IDENTIFY message from the target
23013177Sgibbs * yet.
23113177Sgibbs */
2324568Sgibbsreselect:
23313177Sgibbs	clr	MSG_LEN		/* Don't have anything in the mesg buffer */
2348104Sgibbs	mov	SELID		call initialize_scsiid
23513177Sgibbs	or	FLAGS,RESELECTED
23613177Sgibbs	jmp	select2
2374568Sgibbs
23813177Sgibbs/*
23913177Sgibbs * After the selection, remove this SCB from the "waiting for selection"
24013177Sgibbs * list.  This is achieved by simply moving our "next" pointer into
24113177Sgibbs * WAITING_SCBH.  Our next pointer will be set to null the next time this
24213177Sgibbs * SCB is used, so don't bother with it now.
24313177Sgibbs */
2448104Sgibbsselect:
24515328Sgibbs	mov	WAITING_SCBH,SCB_NEXT
24615328Sgibbs	or	FLAGS,SELECTED
2478104Sgibbsselect2:
24813177Sgibbs/*
24913177Sgibbs * Set CLRCHN here before the target has entered a data transfer mode -
25013177Sgibbs * with synchronous SCSI, if you do it later, you blow away some
25113177Sgibbs * data in the SCSI FIFO that the target has already sent to you.
25213177Sgibbs */
25313177Sgibbs	or	SXFRCTL0,CLRCHN
25413177Sgibbs/*
25513177Sgibbs * Initialize SCSIRATE with the appropriate value for this target.
25613177Sgibbs */
25713177Sgibbs	call	ndx_dtr
25813177Sgibbs	mov	SCSIRATE,SINDIR
25913177Sgibbs
26015843Sgibbs/*
26115881Sgibbs * Initialize Ultra mode setting.
26215843Sgibbs */
26315881Sgibbs	mov	FUNCTION1,SCSIID
26415881Sgibbs	mov	A,FUNCTION1
26515843Sgibbs	and	SINDEX,0xdf,SXFRCTL0		/* default to Ultra disabled */
26615843Sgibbs	test	SCSIID, 0x80	 jnz ultra_b	/* Target ID > 7 */
26715843Sgibbs	test	SBLKCTL, SELBUSB jnz ultra_b	/* Second channel device */
26815843Sgibbs	test	ULTRA_ENB,A	 jz  set_sxfrctl0
26915843Sgibbs	or	SINDEX, ULTRAEN  jmp set_sxfrctl0
27015843Sgibbsultra_b:
27115843Sgibbs	test	ULTRA_ENB_B,A	 jz  set_sxfrctl0
27215843Sgibbs	or	SINDEX, ULTRAEN
27315843Sgibbs
27415843Sgibbsset_sxfrctl0:
27515843Sgibbs	mov	SXFRCTL0,SINDEX
27615843Sgibbs
27713177Sgibbs	mvi	SCSISEQ,ENAUTOATNP		/*
27813177Sgibbs						 * ATN on parity errors
27913177Sgibbs						 * for "in" phases
28013177Sgibbs						 */
28113177Sgibbs	mvi	CLRSINT1,CLRBUSFREE
28213177Sgibbs	mvi	CLRSINT0,0x60			/* CLRSELDI|CLRSELDO */
28313177Sgibbs/*
28413177Sgibbs * Main loop for information transfer phases.  If BSY is false, then
28513177Sgibbs * we have a bus free condition, expected or not.  Otherwise, wait
28613177Sgibbs * for the target to assert REQ before checking MSG, C/D and I/O
28713177Sgibbs * for the bus phase.
28813177Sgibbs *
28913177Sgibbs */
2904568SgibbsITloop:
29113177Sgibbs	test	SSTAT1,BUSFREE	jnz p_busfree
29213177Sgibbs	test	SSTAT1,REQINIT	jz ITloop
2934568Sgibbs
29413177Sgibbs	and	A,PHASE_MASK,SCSISIGI
29513690Sgibbs	mov	LASTPHASE,A
29613690Sgibbs	mov	SCSISIGO,A
2974568Sgibbs
2984568Sgibbs	cmp	ALLZEROS,A	je p_dataout
29913177Sgibbs	cmp	A,P_DATAIN	je p_datain
30013177Sgibbs	cmp	A,P_COMMAND	je p_command
30113177Sgibbs	cmp	A,P_MESGOUT	je p_mesgout
30213177Sgibbs	cmp	A,P_STATUS	je p_status
30313177Sgibbs	cmp	A,P_MESGIN	je p_mesgin
3044568Sgibbs
30513177Sgibbs	mvi	INTSTAT,BAD_PHASE	/* unknown phase - signal driver */
30615843Sgibbs	jmp	ITloop			/* Try reading the bus again. */
3074568Sgibbs
3084568Sgibbsp_dataout:
30913177Sgibbs	mvi	DMAPARAMS,0x7d			/*
31013177Sgibbs						 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
31113177Sgibbs						 * DIRECTION|FIFORESET
31213177Sgibbs						 */
3139928Sgibbs	jmp	data_phase_init
3144568Sgibbs
31513177Sgibbs/*
31613177Sgibbs * If we re-enter the data phase after going through another phase, the
31713177Sgibbs * STCNT may have been cleared, so restore it from the residual field.
31813177Sgibbs */
3199928Sgibbsdata_phase_reinit:
32013690Sgibbs	mov	STCNT0,SCB_RESID_DCNT0
32113690Sgibbs	mov	STCNT1,SCB_RESID_DCNT1
32213690Sgibbs	mov	STCNT2,SCB_RESID_DCNT2
3239928Sgibbs	jmp	data_phase_loop
3244568Sgibbs
3259928Sgibbsp_datain:
32613177Sgibbs	mvi	DMAPARAMS,0x79		/*
32713177Sgibbs					 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
32813177Sgibbs					 * !DIRECTION|FIFORESET
32913177Sgibbs					 */
3309928Sgibbsdata_phase_init:
3319928Sgibbs	call	assert
3325775Sgibbs
3339928Sgibbs	test	FLAGS, DPHASE	jnz data_phase_reinit
3349928Sgibbs	call	sg_scb2ram
33513177Sgibbs	or	FLAGS, DPHASE		/* We have seen a data phase */
3364568Sgibbs
3379928Sgibbsdata_phase_loop:
33813177Sgibbs/* If we are the last SG block, don't set wideodd. */
3399928Sgibbs	cmp	SG_COUNT,0x01 jne data_phase_wideodd
34013177Sgibbs	and	DMAPARAMS, 0xbf		/* Turn off WIDEODD */
3419928Sgibbsdata_phase_wideodd:
3429928Sgibbs	mov	DMAPARAMS  call dma
3434568Sgibbs
34413177Sgibbs/* Exit if we had an underrun */
34513177Sgibbs	test	SSTAT0,SDONE	jz data_phase_finish /* underrun STCNT != 0 */
3467532Sgibbs
34713177Sgibbs/*
34813177Sgibbs * Advance the scatter-gather pointers if needed 
34913177Sgibbs */
3509928Sgibbssg_advance:
35113177Sgibbs	dec	SG_COUNT	/* one less segment to go */
3524568Sgibbs
35313177Sgibbs	test	SG_COUNT, 0xff	jz data_phase_finish /* Are we done? */
3544568Sgibbs
35513177Sgibbs	clr	A			/* add sizeof(struct scatter) */
35613177Sgibbs	add	SG_NEXT0,SG_SIZEOF,SG_NEXT0
35713177Sgibbs	adc	SG_NEXT1,A,SG_NEXT1
3584568Sgibbs
35913177Sgibbs/*
36013177Sgibbs * Load a struct scatter and set up the data address and length.
36113177Sgibbs * If the working value of the SG count is nonzero, then
36213177Sgibbs * we need to load a new set of values.
36313177Sgibbs *
36415328Sgibbs * This, like all DMA's, assumes little-endian host data storage.
36513177Sgibbs */
3669928Sgibbssg_load:
36713177Sgibbs	clr	HCNT2
36813177Sgibbs	clr	HCNT1
36913177Sgibbs	mvi	HCNT0,SG_SIZEOF
3704568Sgibbs
37113690Sgibbs	mov	HADDR0,SG_NEXT0
37213690Sgibbs	mov	HADDR1,SG_NEXT1
37313690Sgibbs	mov	HADDR2,SG_NEXT2
37413690Sgibbs	mov	HADDR3,SG_NEXT3
3754568Sgibbs
37613690Sgibbs	or	DFCNTRL,0xd			/* HDMAEN|DIRECTION|FIFORESET */
3779928Sgibbs
37813177Sgibbs/*
37913177Sgibbs * Wait for DMA from host memory to data FIFO to complete, then disable
38013177Sgibbs * DMA and wait for it to acknowledge that it's off.
38113177Sgibbs */
38213690Sgibbsdma_finish:
38313690Sgibbs	test	DFSTATUS,HDONE	jz dma_finish
38413690Sgibbs	/* Turn off DMA preserving WIDEODD */
38513690Sgibbs	and	DFCNTRL,WIDEODD
38613690Sgibbsdma_finish2:
38713690Sgibbs	test	DFCNTRL,HDMAENACK jnz dma_finish2
3889928Sgibbs
38913177Sgibbs/*
39013177Sgibbs * Copy data from FIFO into SCB data pointer and data count.  This assumes
39113177Sgibbs * that the struct scatterlist has this structure (this and sizeof(struct
39214449Sgibbs * scatterlist) == 12 are asserted in aic7xxx.c for the Linux driver):
39313177Sgibbs *
39413177Sgibbs *	struct scatterlist {
39513177Sgibbs *		char *address;		four bytes, little-endian order
39613177Sgibbs *		...			four bytes, ignored
39713177Sgibbs *		unsigned short length;	two bytes, little-endian order
39813177Sgibbs *	}
39913177Sgibbs *
40013177Sgibbs *
40114449Sgibbs * In FreeBSD, the scatter list entry is only 8 bytes.
40213177Sgibbs * 
40313177Sgibbs * struct ahc_dma_seg {
40413177Sgibbs *       physaddr addr;                  four bytes, little-endian order
40513177Sgibbs *       long    len;                    four bytes, little endian order
40613177Sgibbs * };
40713177Sgibbs */
4089928Sgibbs
40914449Sgibbs	mov	HADDR0,DFDAT
41014449Sgibbs	mov	HADDR1,DFDAT
41114449Sgibbs	mov	HADDR2,DFDAT
41214449Sgibbs	mov	HADDR3,DFDAT
41313177Sgibbs/*
41413177Sgibbs * For Linux, we must throw away four bytes since there is a 32bit gap
41514449Sgibbs * in the middle of a struct scatterlist.
41613177Sgibbs */
41715709Sgibbs#ifdef __linux__
41813177Sgibbs	mov	NONE,DFDAT
41913177Sgibbs	mov	NONE,DFDAT
42013177Sgibbs	mov	NONE,DFDAT
42113177Sgibbs	mov	NONE,DFDAT
42214449Sgibbs#endif
42313690Sgibbs	mov	HCNT0,DFDAT
42413690Sgibbs	mov	HCNT1,DFDAT
42513690Sgibbs	mov	HCNT2,DFDAT
4264568Sgibbs
42713177Sgibbs/* Load STCNT as well.  It is a mirror of HCNT */
42813690Sgibbs	mov	STCNT0,HCNT0
42913690Sgibbs	mov	STCNT1,HCNT1
43013690Sgibbs	mov	STCNT2,HCNT2
4319928Sgibbs        test    SSTAT1,PHASEMIS  jz data_phase_loop
4324568Sgibbs
4339928Sgibbsdata_phase_finish:
43413177Sgibbs/*
43513177Sgibbs * After a DMA finishes, save the SG and STCNT residuals back into the SCB
43613177Sgibbs * We use STCNT instead of HCNT, since it's a reflection of how many bytes 
43713177Sgibbs * were transferred on the SCSI (as opposed to the host) bus.
43813177Sgibbs */
43913690Sgibbs	mov	SCB_RESID_DCNT0,STCNT0
44013690Sgibbs	mov	SCB_RESID_DCNT1,STCNT1
44113690Sgibbs	mov	SCB_RESID_DCNT2,STCNT2
44213177Sgibbs	mov	SCB_RESID_SGCNT, SG_COUNT
4434568Sgibbs	jmp	ITloop
4444568Sgibbs
44513177Sgibbs/*
44615328Sgibbs * Command phase.  Set up the DMA registers and let 'er rip.
44713177Sgibbs */
4484568Sgibbsp_command:
4494568Sgibbs	call	assert
4504568Sgibbs
45113177Sgibbs/*
45215328Sgibbs * Load HADDR and HCNT.
45313177Sgibbs */
45413690Sgibbs	mov	HADDR0, SCB_CMDPTR0
45513690Sgibbs	mov	HADDR1, SCB_CMDPTR1
45613690Sgibbs	mov	HADDR2, SCB_CMDPTR2
45713690Sgibbs	mov	HADDR3, SCB_CMDPTR3
45813690Sgibbs	mov	HCNT0, SCB_CMDLEN
45913690Sgibbs	clr	HCNT1
46013690Sgibbs	clr	HCNT2
4614568Sgibbs
46213690Sgibbs	mov	STCNT0, HCNT0
46313690Sgibbs	mov	STCNT1, HCNT1
46413690Sgibbs	mov	STCNT2, HCNT2
4654568Sgibbs
4664568Sgibbs	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
4674568Sgibbs						#   DIRECTION|FIFORESET
4684568Sgibbs	jmp	ITloop
4694568Sgibbs
47013177Sgibbs/*
47113177Sgibbs * Status phase.  Wait for the data byte to appear, then read it
47213177Sgibbs * and store it into the SCB.
47313177Sgibbs */
4744568Sgibbsp_status:
47513177Sgibbs	mvi	SCB_TARGET_STATUS	call inb_first
4769954Sgibbs	jmp	mesgin_done
4774568Sgibbs
47813177Sgibbs/*
47915328Sgibbs * Message out phase.  If there is not an active message, but the target
48013177Sgibbs * took us into this phase anyway, build a no-op message and send it.
48113177Sgibbs */
4824568Sgibbsp_mesgout:
48313177Sgibbs	test	MSG_LEN, 0xff	jnz  p_mesgout_start
48413177Sgibbs	mvi	MSG_NOP		call mk_mesg	/* build NOP message */
4854568Sgibbs
48613177Sgibbsp_mesgout_start:
48713177Sgibbs/*
48813177Sgibbs * Set up automatic PIO transfer from MSG0.  Bit 3 in
48913177Sgibbs * SXFRCTL0 (SPIOEN) is already on.
49013177Sgibbs */
49113177Sgibbs	mvi	SINDEX,MSG0
4924568Sgibbs	mov	DINDEX,MSG_LEN
4934568Sgibbs
49413177Sgibbs/*
49513177Sgibbs * When target asks for a byte, drop ATN if it's the last one in
49613177Sgibbs * the message.  Otherwise, keep going until the message is exhausted.
49713177Sgibbs *
49813177Sgibbs * Keep an eye out for a phase change, in case the target issues
49913177Sgibbs * a MESSAGE REJECT.
50013177Sgibbs */
50113177Sgibbsp_mesgout_loop:
50213177Sgibbs	test	SSTAT0,SPIORDY	jz p_mesgout_loop
50315998Sgibbs	test	SSTAT1,PHASEMIS	jnz p_mesgout_phasemis
50413177Sgibbs	cmp	DINDEX,1	jne p_mesgout_outb	/* last byte? */
50513177Sgibbs	mvi	CLRSINT1,CLRATNO			/* drop ATN */
50613177Sgibbsp_mesgout_outb:
50713177Sgibbs	dec	DINDEX
50813386Sgibbs	or	CLRSINT0, CLRSPIORDY
5094568Sgibbs	mov	SCSIDATL,SINDIR
51013313Sgibbs	
5114568Sgibbsp_mesgout4:
51213177Sgibbs	test	DINDEX,0xff	jnz p_mesgout_loop
5134568Sgibbs
51413177Sgibbs/*
51513177Sgibbs * If the next bus phase after ATN drops is a message out, it means
51613177Sgibbs * that the target is requesting that the last message(s) be resent.
51713177Sgibbs */
51813177Sgibbsp_mesgout_snoop:
51913177Sgibbs	test	SSTAT1,BUSFREE	jnz p_mesgout_done
52013177Sgibbs	test	SSTAT1,REQINIT	jz p_mesgout_snoop
5214568Sgibbs
52213177Sgibbs	test	SSTAT1,PHASEMIS	jnz p_mesgout_done
5234568Sgibbs
52413690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
52513177Sgibbs
5264568Sgibbs	jmp	ITloop
5274568Sgibbs
52813177Sgibbsp_mesgout_phasemis:
52915328Sgibbs	mvi	CLRSINT1,CLRATNO	/* Be sure to turn ATNO off */
53013177Sgibbsp_mesgout_done:
53113177Sgibbs	clr	MSG_LEN			/* no active msg */
5324568Sgibbs	jmp	ITloop
5334568Sgibbs
53413177Sgibbs/*
53513177Sgibbs * Message in phase.  Bytes are read using Automatic PIO mode.
53613177Sgibbs */
5374568Sgibbsp_mesgin:
53813177Sgibbs	mvi	A		call inb_first	/* read the 1st message byte */
53913177Sgibbs	mov	REJBYTE,A			/* save it for the driver */
5404568Sgibbs
54113177Sgibbs	test	A,MSG_IDENTIFY		jnz mesgin_identify
54213177Sgibbs	cmp	A,MSG_DISCONNECT	je mesgin_disconnect
54313177Sgibbs	cmp	A,MSG_SDPTRS		je mesgin_sdptrs
54413177Sgibbs	cmp	ALLZEROS,A		je mesgin_complete
54513177Sgibbs	cmp	A,MSG_RDPTRS		je mesgin_rdptrs
54613177Sgibbs	cmp	A,MSG_EXTENDED		je mesgin_extended
54713177Sgibbs	cmp	A,MSG_REJECT		je mesgin_reject
5484568Sgibbs
5499954Sgibbsrej_mesgin:
55013177Sgibbs/*
55113177Sgibbs * We have no idea what this message in is, and there's no way
55213177Sgibbs * to pass it up to the kernel, so we issue a message reject and
55313177Sgibbs * hope for the best.  Since we're now using manual PIO mode to
55413177Sgibbs * read in the message, there should no longer be a race condition
55513177Sgibbs * present when we assert ATN.  In any case, rejection should be a
55613177Sgibbs * rare occurrence - signal the driver when it happens.
55713177Sgibbs */
55813690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
55913177Sgibbs	mvi	INTSTAT,SEND_REJECT		/* let driver know */
5609954Sgibbs
56113177Sgibbs	mvi	MSG_REJECT	call mk_mesg
5629954Sgibbs
5639954Sgibbsmesgin_done:
56413177Sgibbs	call	inb_last			/*ack & turn auto PIO back on*/
5659954Sgibbs	jmp	ITloop
5669954Sgibbs
5679954Sgibbs
5689954Sgibbsmesgin_complete:
56913177Sgibbs/*
57015328Sgibbs * We got a "command complete" message, so put the SCB_TAG into QUEUEOUT,
57115328Sgibbs * and trigger a completion interrupt.  Check status for non zero return
57215328Sgibbs * and interrupt driver if needed.  This allows the driver to interpret
57315328Sgibbs * errors only when they occur instead of always uploading the scb.  If
57415328Sgibbs * the status is SCSI_CHECK, the driver will download a new scb requesting
57515328Sgibbs * sense to replace the old one, modify the "waiting for selection" SCB list
57615328Sgibbs * and set RETURN_1 to SEND_SENSE.  If RETURN_1 is set to SEND_SENSE the
57715328Sgibbs * sequencer imediately jumps to main loop where it will run down the waiting
57815328Sgibbs * SCB list and process the sense request.  If the kernel driver does not
57915328Sgibbs * wish to request sense, it need only clear RETURN_1, and the command is
58015328Sgibbs * allowed to complete.  We don't bother to post to the QOUTFIFO in the
58115328Sgibbs * error case since it would require extra work in the kernel driver to
58215328Sgibbs * ensure that the entry was removed before the command complete code tried
58315328Sgibbs * processing it.
58413177Sgibbs *
58513177Sgibbs * First check for residuals
58613177Sgibbs */
58713177Sgibbs	test	SCB_RESID_SGCNT,0xff	jz check_status
58813177Sgibbs/*
58913177Sgibbs * If we have a residual count, interrupt and tell the host.  Other
59013177Sgibbs * alternatives are to pause the sequencer on all command completes (yuck),
59113177Sgibbs * dma the resid directly to the host (slick, we may have space to do it now)
59213177Sgibbs * or have the sequencer pause itself when it encounters a non-zero resid 
59313177Sgibbs * (unecessary pause just to flag the command -yuck-, but takes one instruction
59413177Sgibbs * and since it shouldn't happen that often is good enough for our purposes).  
59513177Sgibbs */
59613177Sgibbsresid:
59713177Sgibbs	mvi	INTSTAT,RESIDUAL
5984568Sgibbs
5997532Sgibbscheck_status:
60013177Sgibbs	test	SCB_TARGET_STATUS,0xff	jz status_ok	/* Good Status? */
60113177Sgibbs	mvi	INTSTAT,BAD_STATUS			/* let driver know */
60213177Sgibbs	cmp	RETURN_1, SEND_SENSE	jne status_ok
6039954Sgibbs	jmp	mesgin_done
6045326Sgibbs
6054568Sgibbsstatus_ok:
60613177Sgibbs/* First, mark this target as free. */
60713177Sgibbs	test	SCB_CONTROL,TAG_ENB jnz test_immediate	/*
60813177Sgibbs							 * Tagged commands
60913177Sgibbs							 * don't busy the
61013177Sgibbs							 * target.
61113177Sgibbs							 */
61213177Sgibbs	mov	FUNCTION1,SCB_TCL
6135326Sgibbs	mov	A,FUNCTION1
61413177Sgibbs	test	SCB_TCL,0x88 jz clear_a
6155326Sgibbs	xor	ACTIVE_B,A
61613177Sgibbs	jmp	test_immediate
6175326Sgibbs
6185326Sgibbsclear_a:
6195326Sgibbs	xor	ACTIVE_A,A
6205326Sgibbs
62113177Sgibbstest_immediate:
62213177Sgibbs	test    SCB_CMDLEN,0xff jnz complete  /* Immediate message complete */
62313177Sgibbs/*
62413177Sgibbs * Pause the sequencer until the driver gets around to handling the command
62513177Sgibbs * complete.  This is so that any action that might require carefull timing
62613177Sgibbs * with the completion of this command can occur.
62713177Sgibbs */
6289810Sgibbs	mvi	INTSTAT,IMMEDDONE
62913177Sgibbs	jmp	start
6305326Sgibbscomplete:
63115328Sgibbs	mov	QOUTFIFO,SCB_TAG
6327532Sgibbs	mvi	INTSTAT,CMDCMPLT
6339954Sgibbs	jmp	mesgin_done
6344568Sgibbs
6357532Sgibbs
63613177Sgibbs/*
63713177Sgibbs * Is it an extended message?  We only support the synchronous and wide data
63813177Sgibbs * transfer request messages, which will probably be in response to
63913177Sgibbs * WDTR or SDTR message outs from us.  If it's not SDTR or WDTR, reject it -
64013177Sgibbs * apparently this can be done after any message in byte, according
64113177Sgibbs * to the SCSI-2 spec.
64213177Sgibbs */
6439954Sgibbsmesgin_extended:
64413177Sgibbs	mvi	ARG_1		call inb_next	/* extended message length */
64515328Sgibbs	mvi	REJBYTE_EXT	call inb_next	/* extended message code */
6464568Sgibbs
64715328Sgibbs	cmp	REJBYTE_EXT,MSG_SDTR	je p_mesginSDTR
64815328Sgibbs	cmp	REJBYTE_EXT,MSG_WDTR	je p_mesginWDTR
6499954Sgibbs	jmp	rej_mesgin
6505562Sgibbs
6515562Sgibbsp_mesginWDTR:
65213177Sgibbs	cmp	ARG_1,2		jne rej_mesgin	/* extended mesg length=2 */
65313177Sgibbs	mvi	ARG_1		call inb_next	/* Width of bus */
65413177Sgibbs	mvi	INTSTAT,WDTR_MSG		/* let driver know */
65513177Sgibbs	test	RETURN_1,0xff jz mesgin_done	/* Do we need to send WDTR? */
65613177Sgibbs	cmp	RETURN_1,SEND_REJ je rej_mesgin /*
65713177Sgibbs						 * Bus width was too large 
65813177Sgibbs						 * Reject it.
65913177Sgibbs						 */
6605562Sgibbs
66113177Sgibbs/* We didn't initiate the wide negotiation, so we must respond to the request */
66213177Sgibbs	and	RETURN_1,0x7f			/* Clear the SEND_WDTR Flag */
66313177Sgibbs	mvi	DINDEX,MSG0
66413177Sgibbs	mvi	MSG0	call mk_wdtr		/* build WDTR message */
66513690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
6669954Sgibbs	jmp	mesgin_done
6675562Sgibbs
6685562Sgibbsp_mesginSDTR:
66913177Sgibbs	cmp	ARG_1,3		jne rej_mesgin	/* extended mesg length=3 */
67013177Sgibbs	mvi	ARG_1		call inb_next	/* xfer period */
67113177Sgibbs	mvi	A		call inb_next	/* REQ/ACK offset */
67213177Sgibbs	mvi	INTSTAT,SDTR_MSG		/* call driver to convert */
6734568Sgibbs
67413177Sgibbs	test	RETURN_1,0xff	jz mesgin_done  /* Do we need to mk_sdtr/rej */
67513177Sgibbs	cmp	RETURN_1,SEND_REJ je rej_mesgin /*
67613177Sgibbs						 * Requested SDTR too small
67713177Sgibbs						 * Reject it.
67813177Sgibbs						 */
67915328Sgibbs	clr	ARG_1				/* Use the scratch ram rate */
68013177Sgibbs	mvi	DINDEX, MSG0
68113177Sgibbs	mvi     MSG0     call mk_sdtr
68213690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
6839954Sgibbs	jmp	mesgin_done
6844568Sgibbs
68513177Sgibbs/*
68613177Sgibbs * Is it a disconnect message?  Set a flag in the SCB to remind us
68713177Sgibbs * and await the bus going free.
68813177Sgibbs */
6899954Sgibbsmesgin_disconnect:
69013177Sgibbs	or	SCB_CONTROL,DISCONNECTED
69115328Sgibbs	test	FLAGS, PAGESCBS jz mesgin_done
69215328Sgibbs/*
69315328Sgibbs * Link this SCB into the DISCONNECTED list.  This list holds the
69415328Sgibbs * candidates for paging out an SCB if one is needed for a new command.
69515328Sgibbs * Modifying the disconnected list is a critical(pause dissabled) section.
69615328Sgibbs */
69715328Sgibbs	mvi	SCB_PREV, SCB_LIST_NULL
69815328Sgibbs	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
69915328Sgibbs	mov	SCB_NEXT, DISCONNECTED_SCBH
70015328Sgibbs	mov	DISCONNECTED_SCBH, SCBPTR
70115328Sgibbs	cmp	SCB_NEXT,SCB_LIST_NULL je linkdone
70215328Sgibbs	mov	SCBPTR,SCB_NEXT
70315328Sgibbs	mov	SCB_PREV,DISCONNECTED_SCBH
70415328Sgibbs	mov	SCBPTR,DISCONNECTED_SCBH
70515328Sgibbslinkdone:
70615328Sgibbs	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
7079954Sgibbs	jmp	mesgin_done
7084568Sgibbs
70913177Sgibbs/*
71013177Sgibbs * Save data pointers message?  Copy working values into the SCB,
71113177Sgibbs * usually in preparation for a disconnect.
71213177Sgibbs */
7139954Sgibbsmesgin_sdptrs:
7144568Sgibbs	call	sg_ram2scb
7159954Sgibbs	jmp	mesgin_done
7164568Sgibbs
71713177Sgibbs/*
71813177Sgibbs * Restore pointers message?  Data pointers are recopied from the
71913177Sgibbs * SCB anytime we enter a data phase for the first time, so all
72013177Sgibbs * we need to do is clear the DPHASE flag and let the data phase
72113177Sgibbs * code do the rest.
72213177Sgibbs */
7239954Sgibbsmesgin_rdptrs:
72415328Sgibbs	and	FLAGS,0xef			/*
72513177Sgibbs						 * !DPHASE we'll reload them
72613177Sgibbs						 * the next time through
72713177Sgibbs						 */
7289954Sgibbs	jmp	mesgin_done
7294568Sgibbs
73013177Sgibbs/*
73113177Sgibbs * Identify message?  For a reconnecting target, this tells us the lun
73213177Sgibbs * that the reconnection is for - find the correct SCB and switch to it,
73313177Sgibbs * clearing the "disconnected" bit so we don't "find" it by accident later.
73413177Sgibbs */
7359954Sgibbsmesgin_identify:
73613177Sgibbs	test	A,0x78	jnz rej_mesgin	/*!DiscPriv|!LUNTAR|!Reserved*/
7374568Sgibbs
73813177Sgibbs	and	A,0x07			/* lun in lower three bits */
7396608Sgibbs	or      SAVED_TCL,A,SELID          
7406608Sgibbs	and     SAVED_TCL,0xf7
74113177Sgibbs	and     A,SELBUSB,SBLKCTL	/* B Channel?? */
7426608Sgibbs	or      SAVED_TCL,A
74313177Sgibbs	call	inb_last		/* ACK */
74413177Sgibbs
74513177Sgibbs/*
74613177Sgibbs * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
74715328Sgibbs * If we get one, we use the tag returned to switch to find the proper
74815328Sgibbs * SCB.  With SCB paging, this requires using findSCB for both tagged
74915328Sgibbs * and non-tagged transactions since the SCB may exist in any slot.
75015328Sgibbs * If we're not using SCB paging, we can use the tag as the direct
75115328Sgibbs * index to the SCB.
75213177Sgibbs */
75315328Sgibbs	mvi	ARG_1,SCB_LIST_NULL	/* Default to no-tag */
75413177Sgibbssnoop_tag_loop:
75513177Sgibbs	test	SSTAT1,BUSFREE	jnz use_findSCB
75613177Sgibbs	test	SSTAT1,REQINIT	jz snoop_tag_loop
75713177Sgibbs	test	SSTAT1,PHASEMIS	jnz use_findSCB
75813177Sgibbs	mvi	A		call inb_first
75915328Sgibbs	cmp	A,MSG_SIMPLE_TAG jne use_findSCB
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/*
77215328Sgibbs * Ensure that the SCB the tag points to is for an SCB transaction
77313177Sgibbs * to the reconnecting target.
77413177Sgibbs */
77515328Sgibbs	test	FLAGS, PAGESCBS	jz index_by_tag
77615328Sgibbs	call	inb_last			/* Ack Tag */
77715328Sgibbsuse_findSCB:
77815328Sgibbs	mov	ALLZEROS	call findSCB	  /* Have to search */
77915328Sgibbssetup_SCB:
78015328Sgibbs	and	SCB_CONTROL,0xfb	  /* clear disconnect bit in SCB */
78115328Sgibbs	or	FLAGS,IDENTIFY_SEEN	  /* make note of IDENTIFY */
78215328Sgibbs	jmp	ITloop
78315328Sgibbsindex_by_tag:
78413177Sgibbs	mov	SCBPTR,ARG_1
7856608Sgibbs	mov	A,SAVED_TCL
78613177Sgibbs	cmp	SCB_TCL,A		jne abort_tag
78713177Sgibbs	test	SCB_CONTROL,TAG_ENB	jz  abort_tag
78813177Sgibbs	call	inb_last			/* Ack Successful tag */
78913177Sgibbs	jmp	setup_SCB
79015328Sgibbs
7917700Sgibbsabort_tag:
79213690Sgibbs	or	SCSISIGO,ATNO			/* turn on ATNO */
79313177Sgibbs	mvi	INTSTAT,ABORT_TAG 		/* let driver know */
79415328Sgibbs	mvi	MSG_ABORT_TAG	call mk_mesg	/* ABORT TAG message */
79513177Sgibbs	jmp	mesgin_done
7966608Sgibbs
79713177Sgibbs/*
79813177Sgibbs * Message reject?  Let the kernel driver handle this.  If we have an 
79913177Sgibbs * outstanding WDTR or SDTR negotiation, assume that it's a response from 
80013177Sgibbs * the target selecting 8bit or asynchronous transfer, otherwise just ignore 
80113177Sgibbs * it since we have no clue what it pertains to.
80213177Sgibbs */
8039954Sgibbsmesgin_reject:
80413177Sgibbs	mvi	INTSTAT, REJECT_MSG
8059954Sgibbs	jmp	mesgin_done
8065562Sgibbs
80713177Sgibbs/*
80813177Sgibbs * [ ADD MORE MESSAGE HANDLING HERE ]
80913177Sgibbs */
8104568Sgibbs
81113177Sgibbs/*
81213177Sgibbs * Bus free phase.  It might be useful to interrupt the device
81313177Sgibbs * driver if we aren't expecting this.  For now, make sure that
81413177Sgibbs * ATN isn't being asserted and look for a new command.
81513177Sgibbs */
8164568Sgibbsp_busfree:
81713177Sgibbs	mvi	CLRSINT1,CLRATNO
81814449Sgibbs	clr	LASTPHASE
8198567Sdg
82013177Sgibbs/*
82113177Sgibbs * if this is an immediate command, perform a psuedo command complete to
82213177Sgibbs * notify the driver.
82313177Sgibbs */
82413177Sgibbs	test	SCB_CMDLEN,0xff	jz status_ok
82513177Sgibbs	jmp	start
8264568Sgibbs
82713177Sgibbs/*
82813177Sgibbs * Locking the driver out, build a one-byte message passed in SINDEX
82913177Sgibbs * if there is no active message already.  SINDEX is returned intact.
83013177Sgibbs */
8314568Sgibbsmk_mesg:
83213177Sgibbs	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
83313177Sgibbs	test	MSG_LEN,0xff	jz mk_mesg1	/* Should always succeed */
83413177Sgibbs	
83513177Sgibbs	/*
83613177Sgibbs	 * Hmmm.  For some reason the mesg buffer is in use.
83713177Sgibbs	 * Tell the driver.  It should look at SINDEX to find
83813177Sgibbs	 * out what we wanted to use the buffer for and resolve
83913177Sgibbs	 * the conflict.
84013177Sgibbs	 */
84113177Sgibbs	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
84213690Sgibbs	mvi	INTSTAT,MSG_BUFFER_BUSY
8434568Sgibbs
8444568Sgibbsmk_mesg1:
84513177Sgibbs	mvi	MSG_LEN,1		/* length = 1 */
84613177Sgibbs	mov	MSG0,SINDEX		/* 1-byte message */
84713177Sgibbs	mvi	SEQCTL,0x10	ret	/* !PAUSEDIS|FASTMODE */
8484568Sgibbs
84913177Sgibbs/*
85013177Sgibbs * Functions to read data in Automatic PIO mode.
85113177Sgibbs *
85213177Sgibbs * According to Adaptec's documentation, an ACK is not sent on input from
85313177Sgibbs * the target until SCSIDATL is read from.  So we wait until SCSIDATL is
85413177Sgibbs * latched (the usual way), then read the data byte directly off the bus
85513177Sgibbs * using SCSIBUSL.  When we have pulled the ATN line, or we just want to
85613177Sgibbs * acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
85713177Sgibbs * spec guarantees that the target will hold the data byte on the bus until
85813177Sgibbs * we send our ACK.
85913177Sgibbs *
86013177Sgibbs * The assumption here is that these are called in a particular sequence,
86113177Sgibbs * and that REQ is already set when inb_first is called.  inb_{first,next}
86213177Sgibbs * use the same calling convention as inb.
86313177Sgibbs */
86413177Sgibbs
86513177Sgibbsinb_next:
86613386Sgibbs	or	CLRSINT0, CLRSPIORDY
86713360Sgibbs	mov	NONE,SCSIDATL			/*dummy read from latch to ACK*/
86813360Sgibbsinb_next_wait:
86915951Sgibbs	test	SSTAT0,SPIORDY	jz inb_next_wait /* wait for next byte */
87013360Sgibbs	test	SSTAT1,PHASEMIS	jnz mesgin_phasemis
87113313Sgibbsinb_first:
8724568Sgibbs	mov	DINDEX,SINDEX
87313177Sgibbs	mov	DINDIR,SCSIBUSL	ret		/*read byte directly from bus*/
87413177Sgibbsinb_last:
87513360Sgibbs	mov	NONE,SCSIDATL ret		/*dummy read from latch to ACK*/
8764568Sgibbs
87713177Sgibbsmesgin_phasemis:
87813177Sgibbs/*
87913177Sgibbs * We expected to receive another byte, but the target changed phase
88013177Sgibbs */
88113177Sgibbs	mvi	INTSTAT, MSGIN_PHASEMIS
88213177Sgibbs	jmp	ITloop
8834568Sgibbs
88413177Sgibbs/*
88513177Sgibbs * DMA data transfer.  HADDR and HCNT must be loaded first, and
88613177Sgibbs * SINDEX should contain the value to load DFCNTRL with - 0x3d for
88713177Sgibbs * host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
88813177Sgibbs * during initialization.
88913177Sgibbs */
8904568Sgibbsdma:
8914568Sgibbs	mov	DFCNTRL,SINDEX
8924568Sgibbsdma1:
89313177Sgibbs	test	SSTAT0,DMADONE	jnz dma3
89413177Sgibbs	test	SSTAT1,PHASEMIS	jz dma1		/* ie. underrun */
8954568Sgibbs
89613177Sgibbs/*
89713177Sgibbs * We will be "done" DMAing when the transfer count goes to zero, or
89813177Sgibbs * the target changes the phase (in light of this, it makes sense that
89913177Sgibbs * the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
90013177Sgibbs * doing a SCSI->Host transfer, the data FIFO should be flushed auto-
90113177Sgibbs * magically on STCNT=0 or a phase change, so just wait for FIFO empty
90213177Sgibbs * status.
90313177Sgibbs */
9044568Sgibbsdma3:
90513177Sgibbs	test	SINDEX,DIRECTION	jnz dma5
9064568Sgibbsdma4:
90713177Sgibbs	test	DFSTATUS,FIFOEMP	jz dma4
9084568Sgibbs
90913177Sgibbs/*
91013177Sgibbs * Now shut the DMA enables off and make sure that the DMA enables are 
91113177Sgibbs * actually off first lest we get an ILLSADDR.
91213177Sgibbs */
9134568Sgibbsdma5:
91413177Sgibbs	/* disable DMA, but maintain WIDEODD */
91513690Sgibbs	and	DFCNTRL,WIDEODD
9164568Sgibbsdma6:
91713177Sgibbs	test	DFCNTRL,0x38	jnz dma6  /* SCSIENACK|SDMAENACK|HDMAENACK */
9184568Sgibbs
9194568Sgibbs	ret
9204568Sgibbs
92113177Sgibbs/*
92213177Sgibbs * Common SCSI initialization for selection and reselection.  Expects
92313177Sgibbs * the target SCSI ID to be in the upper four bits of SINDEX, and A's
92413177Sgibbs * contents are stomped on return.
92513177Sgibbs */
9268104Sgibbsinitialize_scsiid:
92713177Sgibbs	and	SINDEX,0xf0		/* Get target ID */
9285775Sgibbs	and	A,0x0f,SCSIID
9295775Sgibbs	or	SINDEX,A
9308104Sgibbs	mov	SCSIID,SINDEX ret
9315326Sgibbs
93213177Sgibbs/*
93313177Sgibbs * Assert that if we've been reselected, then we've seen an IDENTIFY
93413177Sgibbs * message.
93513177Sgibbs */
9364568Sgibbsassert:
93713177Sgibbs	test	FLAGS,RESELECTED	jz return	/* reselected? */
93813177Sgibbs	test	FLAGS,IDENTIFY_SEEN	jnz return	/* seen IDENTIFY? */
9394568Sgibbs
94013177Sgibbs	mvi	INTSTAT,NO_IDENT 	ret	/* no - cause a kernel panic */
9414568Sgibbs
94213177Sgibbs/*
94315328Sgibbs * Locate the SCB matching the target ID/channel/lun in SAVED_TCL, and the tag
94415328Sgibbs * value in ARG_1.  If ARG_1 == SCB_LIST_NULL, we're looking for a non-tagged
94515328Sgibbs * SCB.  Have the kernel print a warning message if it can't be found, and
94615328Sgibbs * generate an ABORT/ABORT_TAG message to the target.  SINDEX should be
94713177Sgibbs * cleared on call.
94813177Sgibbs */
9494568SgibbsfindSCB:
9506608Sgibbs	mov	A,SAVED_TCL
95115328Sgibbs	mov	SCBPTR,SINDEX			/* switch to next SCB */
95215328Sgibbs	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
95313177Sgibbs	cmp	SCB_TCL,A	jne findSCB1 /* target ID/channel/lun match? */
95413177Sgibbs	test	SCB_CONTROL,DISCONNECTED jz findSCB1 /*should be disconnected*/
95515328Sgibbs	test	SCB_CONTROL,TAG_ENB jnz findTaggedSCB
95615328Sgibbs	cmp	ARG_1,SCB_LIST_NULL je foundSCB
95715328Sgibbs	jmp	findSCB1
95815328SgibbsfindTaggedSCB:
95915328Sgibbs	mov	A, ARG_1			/* Tag passed in ARG_1 */
96015328Sgibbs	cmp	SCB_TAG,A	jne findSCB1	/* Found it? */
96115328SgibbsfoundSCB:
96215328Sgibbs	test	FLAGS,PAGESCBS	jz foundSCB_ret
96315328Sgibbs/* Remove this SCB from the disconnection list */
96415328Sgibbs	cmp	SCB_NEXT,SCB_LIST_NULL je unlink_prev
96515328Sgibbs	mov	SAVED_LINKPTR, SCB_PREV
96615328Sgibbs	mov	SCBPTR, SCB_NEXT
96715328Sgibbs	mov	SCB_PREV, SAVED_LINKPTR
96815328Sgibbs	mov	SCBPTR, SINDEX
96915328Sgibbsunlink_prev:
97015328Sgibbs	cmp	SCB_PREV,SCB_LIST_NULL	je rHead/* At the head of the list */
97115328Sgibbs	mov	SAVED_LINKPTR, SCB_NEXT
97215328Sgibbs	mov	SCBPTR, SCB_PREV
97315328Sgibbs	mov	SCB_NEXT, SAVED_LINKPTR
97415328Sgibbs	mov	SCBPTR, SINDEX
97515328Sgibbs	mvi	SEQCTL,0x10	ret		/* !PAUSEDIS|FASTMODE */
97615328SgibbsrHead:
97715328Sgibbs	mov	DISCONNECTED_SCBH,SCB_NEXT
97815328SgibbsfoundSCB_ret:
97915328Sgibbs	mvi	SEQCTL,0x10	ret		/* !PAUSEDIS|FASTMODE */
9804568Sgibbs
9816608SgibbsfindSCB1:
98215328Sgibbs	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
9834568Sgibbs	inc	SINDEX
9844568Sgibbs	mov	A,SCBCOUNT
9856608Sgibbs	cmp	SINDEX,A	jne findSCB
9864568Sgibbs
98713177Sgibbs	mvi	INTSTAT,NO_MATCH		/* not found - signal kernel */
98815328Sgibbs	cmp	RETURN_1,SCB_PAGEDIN je return
98915328Sgibbs	or	SCSISIGO,ATNO			/* assert ATNO */
99015328Sgibbs	cmp	ARG_1,SCB_LIST_NULL jne find_abort_tag
99115328Sgibbs	mvi	MSG_ABORT	call mk_mesg
99215328Sgibbs	jmp	ITloop
99315328Sgibbsfind_abort_tag:
99415328Sgibbs	mvi	MSG_ABORT_TAG	call mk_mesg
99515328Sgibbs	jmp	ITloop
9964568Sgibbs
99713177Sgibbs/*
99813177Sgibbs * Make a working copy of the scatter-gather parameters from the SCB.
99913177Sgibbs */
10004568Sgibbssg_scb2ram:
100113690Sgibbs	mov	HADDR0, SCB_DATAPTR0
100213690Sgibbs	mov	HADDR1, SCB_DATAPTR1
100313690Sgibbs	mov	HADDR2, SCB_DATAPTR2
100413690Sgibbs	mov	HADDR3, SCB_DATAPTR3
100513690Sgibbs	mov	HCNT0, SCB_DATACNT0
100613690Sgibbs	mov	HCNT1, SCB_DATACNT1
100713690Sgibbs	mov	HCNT2, SCB_DATACNT2
10089928Sgibbs
100913690Sgibbs	mov	STCNT0, HCNT0
101013690Sgibbs	mov	STCNT1, HCNT1
101113690Sgibbs	mov	STCNT2, HCNT2
10129928Sgibbs
101313177Sgibbs	mov	SG_COUNT,SCB_SGCOUNT
10144568Sgibbs
101513690Sgibbs	mov	SG_NEXT0, SCB_SGPTR0
101613690Sgibbs	mov	SG_NEXT1, SCB_SGPTR1
101713690Sgibbs	mov	SG_NEXT2, SCB_SGPTR2
101813690Sgibbs	mov	SG_NEXT3, SCB_SGPTR3 ret
10194568Sgibbs
102013177Sgibbs/*
102113177Sgibbs * Copying RAM values back to SCB, for Save Data Pointers message, but
102213177Sgibbs * only if we've actually been into a data phase to change them.  This
102313177Sgibbs * protects against bogus data in scratch ram and the residual counts
102413177Sgibbs * since they are only initialized when we go into data_in or data_out.
102513177Sgibbs */
10264568Sgibbssg_ram2scb:
10279928Sgibbs	test	FLAGS, DPHASE	jz return
102813177Sgibbs	mov	SCB_SGCOUNT,SG_COUNT
10294568Sgibbs
103013690Sgibbs	mov	SCB_SGPTR0,SG_NEXT0
103113690Sgibbs	mov	SCB_SGPTR1,SG_NEXT1
103213690Sgibbs	mov	SCB_SGPTR2,SG_NEXT2
103313690Sgibbs	mov	SCB_SGPTR3,SG_NEXT3
10349928Sgibbs	
103513690Sgibbs	mov	SCB_DATAPTR0,SHADDR0
103613690Sgibbs	mov	SCB_DATAPTR1,SHADDR1
103713690Sgibbs	mov	SCB_DATAPTR2,SHADDR2
103813690Sgibbs	mov	SCB_DATAPTR3,SHADDR3
10394568Sgibbs
104013177Sgibbs/*
104113177Sgibbs * Use the residual number since STCNT is corrupted by any message transfer
104213177Sgibbs */
104313690Sgibbs	mov	SCB_DATACNT0,SCB_RESID_DCNT0
104413690Sgibbs	mov	SCB_DATACNT1,SCB_RESID_DCNT1
104513690Sgibbs	mov	SCB_DATACNT2,SCB_RESID_DCNT2 ret
10464568Sgibbs
104713177Sgibbs/*
104813177Sgibbs * Add the array base TARG_SCRATCH to the target offset (the target address
104913177Sgibbs * is in SCSIID), and return the result in SINDEX.  The accumulator
105013177Sgibbs * contains the 3->8 decoding of the target ID on return.
105113177Sgibbs */
10525562Sgibbsndx_dtr:
10534568Sgibbs	shr	A,SCSIID,4
105413177Sgibbs	test	SBLKCTL,SELBUSB	jz ndx_dtr_2
105513177Sgibbs	or	A,0x08		/* Channel B entries add 8 */
10565562Sgibbsndx_dtr_2:
105713690Sgibbs	add	SINDEX,TARG_SCRATCH,A ret
10584568Sgibbs
105913177Sgibbs/*
106013177Sgibbs * If we need to negotiate transfer parameters, build the WDTR or SDTR message
106113177Sgibbs * starting at the address passed in SINDEX.  DINDEX is modified on return.
106213177Sgibbs * The SCSI-II spec requires that Wide negotiation occur first and you can
106313177Sgibbs * only negotiat one or the other at a time otherwise in the event of a message
106413177Sgibbs * reject, you wouldn't be able to tell which message was the culpret.
106513177Sgibbs */
10665562Sgibbsmk_dtr:
106713177Sgibbs	test	SCB_CONTROL,NEEDWDTR jnz  mk_wdtr_16bit
106815328Sgibbs	mvi	ARG_1, MAXOFFSET	/* Force an offset of 15 or 8 if WIDE */
10694568Sgibbs
10707532Sgibbsmk_sdtr:
107113177Sgibbs	mvi	DINDIR,1		/* extended message */
107213177Sgibbs	mvi	DINDIR,3		/* extended message length = 3 */
107313177Sgibbs	mvi	DINDIR,1		/* SDTR code */
10747532Sgibbs	call	sdtr_to_rate
107513177Sgibbs	mov	DINDIR,RETURN_1		/* REQ/ACK transfer period */
107615328Sgibbs	cmp	ARG_1, MAXOFFSET je mk_sdtr_max_offset
107713177Sgibbs	and	DINDIR,0x0f,SINDIR	/* Sync Offset */
10787532Sgibbs
10797532Sgibbsmk_sdtr_done:
108013177Sgibbs	add	MSG_LEN,COMP_MSG0,DINDEX ret	/* update message length */
10817532Sgibbs
10828567Sdgmk_sdtr_max_offset:
108313177Sgibbs/*
108413177Sgibbs * We're initiating sync negotiation, so request the max offset we can (15 or 8)
108513177Sgibbs */
108613177Sgibbs	/* Talking to a WIDE device? */
108713177Sgibbs	test	SCSIRATE, WIDEXFER	jnz wmax_offset	
10888567Sdg	mvi	DINDIR, MAX_OFFSET_8BIT
10897532Sgibbs	jmp	mk_sdtr_done
10907532Sgibbs
10918567Sdgwmax_offset:
109213177Sgibbs	mvi	DINDIR, MAX_OFFSET_16BIT
10938567Sdg	jmp	mk_sdtr_done
10948567Sdg
10955647Sgibbsmk_wdtr_16bit:
10965647Sgibbs	mvi	ARG_1,BUS_16_BIT
10975562Sgibbsmk_wdtr:
109813177Sgibbs	mvi	DINDIR,1		/* extended message */
109913177Sgibbs	mvi	DINDIR,2		/* extended message length = 2 */
110013177Sgibbs	mvi	DINDIR,3		/* WDTR code */
110113177Sgibbs	mov	DINDIR,ARG_1		/* bus width */
11025562Sgibbs
110313177Sgibbs	add	MSG_LEN,COMP_MSG0,DINDEX ret	/* update message length */
11045562Sgibbs	
11055562Sgibbssdtr_to_rate:
110613177Sgibbs	call	ndx_dtr			/* index scratch space for target */
11075562Sgibbs	shr	A,SINDIR,0x4
110813177Sgibbs	dec	SINDEX			/* Preserve SINDEX */
11095562Sgibbs	and	A,0x7
11105562Sgibbs	clr	RETURN_1
11115562Sgibbssdtr_to_rate_loop:
11125562Sgibbs	test	A,0x0f	jz sdtr_to_rate_done
111313177Sgibbs	add	RETURN_1,0x19
11145562Sgibbs	dec	A	
11155562Sgibbs	jmp	sdtr_to_rate_loop
11165562Sgibbssdtr_to_rate_done:
11175562Sgibbs	shr	RETURN_1,0x2
111813177Sgibbs	add	RETURN_1,0x19
111911829Sgibbs	test	SXFRCTL0,ULTRAEN jz return
112011829Sgibbs	shr	RETURN_1,0x1
11217532Sgibbsreturn:
11227532Sgibbs	ret
1123