aic7xxx.seq revision 7532
15562Sgibbs# @(#)aic7xxx.seq 1.32 94/11/29 jda
24568Sgibbs#
35647Sgibbs# Adaptec 274x/284x/294x device driver for Linux and FreeBSD.
44568Sgibbs# Copyright (c) 1994 The University of Calgary Department of Computer Science.
54568Sgibbs# 
64568Sgibbs# This program is free software; you can redistribute it and/or modify
74568Sgibbs# it under the terms of the GNU General Public License as published by
84568Sgibbs# the Free Software Foundation; either version 2 of the License, or
94568Sgibbs# (at your option) any later version.
104568Sgibbs# 
114568Sgibbs# This program is distributed in the hope that it will be useful,
124568Sgibbs# but WITHOUT ANY WARRANTY; without even the implied warranty of
134568Sgibbs# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
144568Sgibbs# GNU General Public License for more details.
154568Sgibbs# 
164568Sgibbs# You should have received a copy of the GNU General Public License
174568Sgibbs# along with this program; if not, write to the Free Software
184568Sgibbs# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
195647Sgibbs#
204568Sgibbs
216608Sgibbs# FreeBSD, Twin, Wide, 2 command per target support, tagged queuing and other 
226608Sgibbs# optimizations provided by Justin T. Gibbs (gibbs@FreeBSD.org)
235647Sgibbs#
244568Sgibbs
257532SgibbsVERSION AIC7XXX_SEQ_VER "$Id: aic7xxx.seq,v 1.10 1995/03/17 23:54:17 gibbs Exp $"
265647Sgibbs
274866SgibbsSCBMASK		= 0x1f
284568Sgibbs
294568SgibbsSCSISEQ		= 0x00
304568SgibbsSXFRCTL0	= 0x01
314568SgibbsSXFRCTL1	= 0x02
324568SgibbsSCSISIGI	= 0x03
334568SgibbsSCSISIGO	= 0x03
344568SgibbsSCSIRATE	= 0x04
354568SgibbsSCSIID		= 0x05
364568SgibbsSCSIDATL	= 0x06
374568SgibbsSTCNT		= 0x08
384568SgibbsSTCNT+0		= 0x08
394568SgibbsSTCNT+1		= 0x09
404568SgibbsSTCNT+2		= 0x0a
414568SgibbsSSTAT0		= 0x0b
424568SgibbsCLRSINT1	= 0x0c
434568SgibbsSSTAT1		= 0x0c
444568SgibbsSIMODE1		= 0x11
454568SgibbsSCSIBUSL	= 0x12
464568SgibbsSHADDR		= 0x14
474568SgibbsSELID		= 0x19
484568SgibbsSBLKCTL		= 0x1f
494568SgibbsSEQCTL		= 0x60
504568SgibbsA		= 0x64				# == ACCUM
514568SgibbsSINDEX		= 0x65
524568SgibbsDINDEX		= 0x66
534568SgibbsALLZEROS	= 0x6a
544568SgibbsNONE		= 0x6a
554568SgibbsSINDIR		= 0x6c
564568SgibbsDINDIR		= 0x6d
574568SgibbsFUNCTION1	= 0x6e
584568SgibbsHADDR		= 0x88
597532SgibbsHADDR+1		= 0x89
607532SgibbsHADDR+2		= 0x8a
617532SgibbsHADDR+3		= 0x8b
624568SgibbsHCNT		= 0x8c
634568SgibbsHCNT+0		= 0x8c
644568SgibbsHCNT+1		= 0x8d
654568SgibbsHCNT+2		= 0x8e
664568SgibbsSCBPTR		= 0x90
674568SgibbsINTSTAT		= 0x91
684568SgibbsDFCNTRL		= 0x93
694568SgibbsDFSTATUS	= 0x94
704568SgibbsDFDAT		= 0x99
714568SgibbsQINFIFO		= 0x9b
724568SgibbsQINCNT		= 0x9c
734568SgibbsQOUTFIFO	= 0x9d
744568Sgibbs
755326SgibbsSCSICONF_A	= 0x5a
765326SgibbsSCSICONF_B	= 0x5b
774568Sgibbs
784568Sgibbs#  The two reserved bytes at SCBARRAY+1[23] are expected to be set to
794568Sgibbs#  zero, and the reserved bit in SCBARRAY+0 is used as an internal flag
804568Sgibbs#  to indicate whether or not to reload scatter-gather parameters after
815647Sgibbs#  a disconnect.  We also use bits 6 & 7 to indicate whether or not to
825647Sgibbs#  initiate SDTR or WDTR repectively when starting this command.
834568Sgibbs#
844568SgibbsSCBARRAY+0	= 0xa0
855647Sgibbs
865647SgibbsDISCONNECTED	= 0x04
875647SgibbsNEEDDMA		= 0x08
885647SgibbsSG_LOAD		= 0x10
896608SgibbsTAG_ENB		= 0x20
905647SgibbsNEEDSDTR	= 0x40
915647SgibbsNEEDWDTR	= 0x80
925647Sgibbs
934568SgibbsSCBARRAY+1	= 0xa1
944568SgibbsSCBARRAY+2	= 0xa2
954568SgibbsSCBARRAY+3	= 0xa3
965647SgibbsSCBARRAY+4	= 0xa4
975647SgibbsSCBARRAY+5	= 0xa5
985647SgibbsSCBARRAY+6	= 0xa6
994568SgibbsSCBARRAY+7	= 0xa7
1005647SgibbsSCBARRAY+8	= 0xa8
1015647SgibbsSCBARRAY+9	= 0xa9
1025647SgibbsSCBARRAY+10	= 0xaa
1034568SgibbsSCBARRAY+11	= 0xab
1045647SgibbsSCBARRAY+12	= 0xac
1055647SgibbsSCBARRAY+13	= 0xad
1064568SgibbsSCBARRAY+14	= 0xae
1074568SgibbsSCBARRAY+15	= 0xaf
1084568SgibbsSCBARRAY+16	= 0xb0
1094568SgibbsSCBARRAY+17	= 0xb1
1104568SgibbsSCBARRAY+18	= 0xb2
1114568SgibbsSCBARRAY+19	= 0xb3
1124568SgibbsSCBARRAY+20	= 0xb4
1134568SgibbsSCBARRAY+21	= 0xb5
1144568SgibbsSCBARRAY+22	= 0xb6
1154568SgibbsSCBARRAY+23	= 0xb7
1164568SgibbsSCBARRAY+24	= 0xb8
1174568SgibbsSCBARRAY+25	= 0xb9
1185647SgibbsSCBARRAY+26	= 0xba
1197532SgibbsSCBARRAY+27	= 0xbb
1207532SgibbsSCBARRAY+28	= 0xbc
1217532SgibbsSCBARRAY+29	= 0xbd
1224568Sgibbs
1235647SgibbsBAD_PHASE	= 0x01				# unknown scsi bus phase
1247532SgibbsCMDCMPLT	= 0x02
1255647SgibbsSEND_REJECT	= 0x11				# sending a message reject
1265647SgibbsNO_IDENT	= 0x21				# no IDENTIFY after reconnect
1275647SgibbsNO_MATCH	= 0x31				# no cmd match for reconnect
1285647SgibbsMSG_SDTR	= 0x41				# SDTR message recieved
1295647SgibbsMSG_WDTR	= 0x51				# WDTR message recieved
1305647SgibbsMSG_REJECT	= 0x61				# Reject message recieved
1315647SgibbsBAD_STATUS	= 0x71				# Bad status from target
1327532SgibbsRESIDUAL	= 0x81				# Residual byte count != 0
1334568Sgibbs
1344568Sgibbs#  The host adapter card (at least the BIOS) uses 20-2f for SCSI
1355326Sgibbs#  device information, 32-33 and 5a-5f as well. As it turns out, the
1365326Sgibbs#  BIOS trashes 20-2f, writing the synchronous negotiation results
1374568Sgibbs#  on top of the BIOS values, so we re-use those for our per-target
1384568Sgibbs#  scratchspace (actually a value that can be copied directly into
1395326Sgibbs#  SCSIRATE).  The kernel driver will enable synchronous negotiation
1405326Sgibbs#  for all targets that have a value other than 0 in the lower four
1415326Sgibbs#  bits of the target scratch space.  This should work irregardless of
1425647Sgibbs#  whether the bios has been installed. NEEDWDTR and NEEDSDTR are the top
1435647Sgibbs#  two bits of the SCB control byte.  The kernel driver will set these
1445647Sgibbs#  when a WDTR or SDTR message should be sent to the target the SCB's 
1455647Sgibbs#  command references.
1464568Sgibbs#
1474568Sgibbs#  The high bit of DROPATN is set if ATN should be dropped before the ACK
1484568Sgibbs#  when outb is called.  REJBYTE contains the first byte of a MESSAGE IN
1494568Sgibbs#  message, so the driver can report an intelligible error if a message is
1504568Sgibbs#  rejected.
1514568Sgibbs#
1525562Sgibbs#  FLAGS's high bit is true if we are currently handling a reselect;
1534568Sgibbs#  its next-highest bit is true ONLY IF we've seen an IDENTIFY message
1544568Sgibbs#  from the reselecting target.  If we haven't had IDENTIFY, then we have
1554568Sgibbs#  no idea what the lun is, and we can't select the right SCB register
1564568Sgibbs#  bank, so force a kernel panic if the target attempts a data in/out or
1574568Sgibbs#  command phase instead of corrupting something.
1584568Sgibbs#
1594568Sgibbs#  Note that SG_NEXT occupies four bytes.
1604568Sgibbs#
1614568SgibbsSYNCNEG		= 0x20
1624568Sgibbs
1634568SgibbsDROPATN		= 0x30
1644568SgibbsREJBYTE		= 0x31
1655326SgibbsDISC_DSB_A	= 0x32
1665326SgibbsDISC_DSB_B	= 0x33
1674568Sgibbs
1686608SgibbsMSG_LEN		= 0x34
1696608SgibbsMSG_START+0	= 0x35
1706608SgibbsMSG_START+1	= 0x36
1716608SgibbsMSG_START+2	= 0x37
1726608SgibbsMSG_START+3	= 0x38
1736608SgibbsMSG_START+4	= 0x39
1746608SgibbsMSG_START+5	= 0x3a
1757532Sgibbs-MSG_START+0	= 0xcc				# 2's complement of MSG_START+0
1764568Sgibbs
1776608SgibbsARG_1		= 0x4a				# sdtr conversion args & return
1785647SgibbsBUS_16_BIT	= 0x01
1796608SgibbsRETURN_1	= 0x4a
1804568Sgibbs
1816608SgibbsSIGSTATE	= 0x4b				# value written to SCSISIGO
1824568Sgibbs
1835647Sgibbs# Linux users should use 0xc (12) for SG_SIZEOF
1845647SgibbsSG_SIZEOF	= 0x8 				# sizeof(struct ahc_dma)
1855647Sgibbs#SG_SIZEOF	= 0xc 				# sizeof(struct scatterlist)
1865647SgibbsSCB_SIZEOF	= 0x13				# sizeof SCB to DMA (19 bytes)
1875647Sgibbs
1886608SgibbsSG_NOLOAD	= 0x4c				# load SG pointer/length?
1896608SgibbsSG_COUNT	= 0x4d				# working value of SG count
1906608SgibbsSG_NEXT		= 0x4e				# working value of SG pointer
1916608SgibbsSG_NEXT+0	= 0x4e
1926608SgibbsSG_NEXT+1	= 0x4f
1936608SgibbsSG_NEXT+2	= 0x50
1946608SgibbsSG_NEXT+3	= 0x51
1954568Sgibbs
1966608SgibbsSCBCOUNT	= 0x52				# the actual number of SCBs
1976608SgibbsFLAGS		= 0x53				# Device configuration flags
1985326SgibbsTWIN_BUS	= 0x01
1995326SgibbsWIDE_BUS	= 0x02
2007532SgibbsMAX_SYNC	= 0x08
2015562SgibbsSENSE		= 0x10
2025562SgibbsACTIVE_MSG	= 0x20
2035562SgibbsIDENTIFY_SEEN	= 0x40
2045562SgibbsRESELECTED	= 0x80
2054568Sgibbs
2066608SgibbsACTIVE_A	= 0x54
2076608SgibbsACTIVE_B	= 0x55
2086608SgibbsSAVED_TCL	= 0x56
2094866Sgibbs#  Poll QINCNT for work - the lower bits contain
2104568Sgibbs#  the number of entries in the Queue In FIFO.
2114568Sgibbs#
2124568Sgibbsstart:
2135562Sgibbs	test	FLAGS,SENSE	jnz start_sense
2145775Sgibbsstart_nosense:
2155326Sgibbs	test	FLAGS,TWIN_BUS	jz start2	# Are we a twin channel device?
2165326Sgibbs# For fairness, we check the other bus first, since we just finished a 
2175326Sgibbs# transaction on the current channel.
2185326Sgibbs	xor	SBLKCTL,0x08			# Toggle to the other bus
2194568Sgibbs	test	SCSISIGI,0x4	jnz reselect	# BSYI
2205326Sgibbs	xor	SBLKCTL,0x08			# Toggle to the original bus
2215326Sgibbsstart2:
2225326Sgibbs	test	SCSISIGI,0x4	jnz reselect	# BSYI
2235775Sgibbs	test	QINCNT,SCBMASK	jz start_nosense
2244568Sgibbs
2255647Sgibbs# We have at least one queued SCB now.  Set the SCB pointer
2265647Sgibbs# from the FIFO so we see the right bank of SCB registers,
2275647Sgibbs# then set SCSI options and set the initiator and target
2285647Sgibbs# SCSI IDs.
2294568Sgibbs#
2304568Sgibbs	mov	SCBPTR,QINFIFO
2314568Sgibbs
2325647Sgibbs# If the control byte of this SCB has the NEEDDMA flag set, we have
2335647Sgibbs# yet to DMA it from host memory
2345647Sgibbs
2355647Sgibbstest	SCBARRAY+0,NEEDDMA	jz test_busy
2365647Sgibbs	clr	HCNT+2
2375647Sgibbs	clr	HCNT+1
2385647Sgibbs	mvi	HCNT+0,SCB_SIZEOF
2395647Sgibbs
2405647Sgibbs	mvi	DINDEX,HADDR
2415775Sgibbs	mvi	SCBARRAY+26	call bcopy_4
2425647Sgibbs
2435647Sgibbs	mvi	DFCNTRL,0xd			# HDMAEN|DIRECTION|FIFORESET
2445647Sgibbs
2455647Sgibbs#  Wait for DMA from host memory to data FIFO to complete, then disable
2465647Sgibbs#  DMA and wait for it to acknowledge that it's off.
2475647Sgibbs#
2487532Sgibbs	call	dma_finish
2495647Sgibbs
2505647Sgibbs# Copy the SCB from the FIFO to  the SCBARRAY
2515647Sgibbs
2527532Sgibbs	mvi	DINDEX, SCBARRAY+0
2537532Sgibbs	call	bcopy_3_dfdat
2547532Sgibbs	call	bcopy_4_dfdat
2557532Sgibbs	call	bcopy_4_dfdat
2567532Sgibbs	call	bcopy_4_dfdat
2577532Sgibbs	call	bcopy_4_dfdat
2585647Sgibbs   
2594568Sgibbs# See if there is not already an active SCB for this target.  This code
2605326Sgibbs# locks out on a per target basis instead of target/lun.  Although this
2615326Sgibbs# is not ideal for devices that have multiple luns active at the same
2625326Sgibbs# time, it is faster than looping through all SCB's looking for active
2635326Sgibbs# commands.  It may be benificial to make findscb a more general procedure
2645326Sgibbs# to see if the added cost of the search is negligible.  This code also 
2655326Sgibbs# assumes that the kernel driver will clear the active flags on board 
2665326Sgibbs# initialization, board reset, and a target's SELTO.
2674568Sgibbs
2685647Sgibbstest_busy:
2696608Sgibbs	test	SCBARRAY+0,0x20	jnz start_scb
2704568Sgibbs	and	FUNCTION1,0x70,SCBARRAY+1
2714568Sgibbs	mov	A,FUNCTION1
2725326Sgibbs	test	SCBARRAY+1,0x88	jz test_a	# Id < 8 && A channel
2735326Sgibbs
2745326Sgibbs	test	ACTIVE_B,A	jnz requeue
2755326Sgibbs	or	ACTIVE_B,A	# Mark the current target as busy
2765326Sgibbs	jmp	start_scb
2775326Sgibbs
2785562Sgibbsstart_sense:
2795562Sgibbs# Clear the SENSE flag first, then do a normal start_scb
2805562Sgibbs	and	FLAGS,0xef
2815562Sgibbs	jmp	start_scb
2825562Sgibbs
2834568Sgibbs# Place the currently active back on the queue for later processing
2845326Sgibbsrequeue:
2854568Sgibbs	mov	QINFIFO, SCBPTR
2865775Sgibbs	jmp	start_nosense
2874568Sgibbs
2885326Sgibbstest_a:
2895326Sgibbs	test	ACTIVE_A,A	jnz requeue
2905326Sgibbs	or	ACTIVE_A,A	# Mark the current target as busy
2915326Sgibbs
2925326Sgibbsstart_scb:
2935647Sgibbs	or	SCBARRAY+0,NEEDDMA
2946940Sgibbs	and	SINDEX,0xf7,SBLKCTL  #Clear the channel select bit
2956940Sgibbs	and	A,0x08,SCBARRAY+1    #Get new channel bit
2966940Sgibbs	or	SINDEX,A	     
2976940Sgibbs	mov	SBLKCTL,SINDEX	# select channel
2984568Sgibbs	mov	SCBARRAY+1	call initialize
2994568Sgibbs	clr	SG_NOLOAD
3005562Sgibbs	and	FLAGS,0x3f	# !RESELECTING
3014568Sgibbs
3024568Sgibbs#  As soon as we get a successful selection, the target should go
3034568Sgibbs#  into the message out phase since we have ATN asserted.  Prepare
3044568Sgibbs#  the message to send, locking out the device driver.  If the device
3054568Sgibbs#  driver hasn't beaten us with an ABORT or RESET message, then tack
3064866Sgibbs#  on an SDTR negotiation if required.
3074568Sgibbs#
3084568Sgibbs#  Messages are stored in scratch RAM starting with a flag byte (high bit
3094568Sgibbs#  set means active message), one length byte, and then the message itself.
3104568Sgibbs#
3114568Sgibbs	mov	SCBARRAY+1	call disconnect	# disconnect ok?
3124568Sgibbs
3134568Sgibbs	and	SINDEX,0x7,SCBARRAY+1		# lun
3144568Sgibbs	or	SINDEX,A			# return value from disconnect
3154568Sgibbs	or	SINDEX,0x80	call mk_mesg	# IDENTIFY message
3164568Sgibbs
3174568Sgibbs	mov	A,SINDEX
3186608Sgibbs	test	SCBARRAY+0,0xe0	jz  !message	# WDTR, SDTR or TAG??
3194568Sgibbs	cmp	MSG_START+0,A	jne !message	# did driver beat us?
3204568Sgibbs
3216608Sgibbs# Tag Message if Tag enabled in SCB control block.  Use SCBPTR as the tag
3226608Sgibbs# value
3236608Sgibbs
3246608Sgibbsmk_tag:
3256608Sgibbs	mvi	DINDEX, MSG_START+1
3266608Sgibbs	test	SCBARRAY+0,TAG_ENB jz mk_tag_done
3276608Sgibbs	and	A,0x23,SCBARRAY+0
3286608Sgibbs	mov	DINDIR,A
3296608Sgibbs	mov	DINDIR,SCBPTR
3306608Sgibbs
3316608Sgibbs	add	MSG_LEN,-MSG_START+0,DINDEX	# update message length
3326608Sgibbs
3336608Sgibbsmk_tag_done:
3346608Sgibbs
3356608Sgibbs	mov	DINDEX	call mk_dtr	# build DTR message if needed
3366608Sgibbs
3374568Sgibbs!message:
3384568Sgibbs
3394568Sgibbs#  Enable selection phase as an initiator, and do automatic ATN
3404568Sgibbs#  after the selection.
3414568Sgibbs#
3424568Sgibbs	mvi	SCSISEQ,0x48			# ENSELO|ENAUTOATNO
3434568Sgibbs
3444568Sgibbs#  Wait for successful arbitration.  The AIC-7770 documentation says
3454568Sgibbs#  that SELINGO indicates successful arbitration, and that it should
3464568Sgibbs#  be used to look for SELDO.  However, if the sequencer is paused at
3474568Sgibbs#  just the right time - a parallel fsck(8) on two drives did it for
3484568Sgibbs#  me - then SELINGO can flip back to false before we've seen it.  This
3494568Sgibbs#  makes the sequencer sit in the arbitration loop forever.  This is
3504568Sgibbs#  Not Good.
3514568Sgibbs#
3524568Sgibbs#  Therefore, I've added a check in the arbitration loop for SELDO
3534568Sgibbs#  too.  This could arguably be made a critical section by disabling
3544568Sgibbs#  pauses, but I don't want to make a potentially infinite loop a CS.
3554568Sgibbs#  I suppose you could fold it into the select loop, too, but since
3564568Sgibbs#  I've been hunting this bug for four days it's kinda like a trophy.
3574568Sgibbs#
3584568Sgibbsarbitrate:
3594568Sgibbs	test	SSTAT0,0x40	jnz *select	# SELDO
3604568Sgibbs	test	SSTAT0,0x10	jz arbitrate	# SELINGO
3614568Sgibbs
3624568Sgibbs#  Wait for a successful selection.  If the hardware selection
3634568Sgibbs#  timer goes off, then the driver gets the interrupt, so we don't
3644568Sgibbs#  need to worry about it.
3654568Sgibbs#
3664568Sgibbsselect:
3674568Sgibbs	test	SSTAT0,0x40	jz select	# SELDO
3684568Sgibbs	jmp	*select
3694568Sgibbs
3704568Sgibbs#  Reselection is being initiated by a target - we've seen the BSY
3714568Sgibbs#  line driven active, and we didn't do it!  Enable the reselection
3724568Sgibbs#  hardware, and wait for it to finish.  Make a note that we've been
3734568Sgibbs#  reselected, but haven't seen an IDENTIFY message from the target
3744568Sgibbs#  yet.
3754568Sgibbs#
3764568Sgibbsreselect:
3774568Sgibbs	mvi	SCSISEQ,0x10			# ENRSELI
3784568Sgibbs
3794568Sgibbsreselect1:
3804568Sgibbs	test	SSTAT0,0x20	jz reselect1	# SELDI
3814568Sgibbs	mov	SELID		call initialize
3824568Sgibbs
3835562Sgibbs	and	FLAGS,0x3f			# reselected, no IDENTIFY	
3845562Sgibbs	or	FLAGS,RESELECTED		
3854568Sgibbs
3864568Sgibbs#  After the [re]selection, make sure that the [re]selection enable
3874568Sgibbs#  bit is off.  This chip is flaky enough without extra things
3884568Sgibbs#  turned on.  Also clear the BUSFREE bit in SSTAT1 since we'll be
3894568Sgibbs#  using it shortly.
3904568Sgibbs#
3914568Sgibbs*select:
3924568Sgibbs	clr	SCSISEQ
3934568Sgibbs	mvi	CLRSINT1,0x8			# CLRBUSFREE
3944568Sgibbs
3954568Sgibbs#  Main loop for information transfer phases.  If BSY is false, then
3964568Sgibbs#  we have a bus free condition, expected or not.  Otherwise, wait
3974568Sgibbs#  for the target to assert REQ before checking MSG, C/D and I/O
3984568Sgibbs#  for the bus phase.
3994568Sgibbs#
4004568Sgibbs#  We can't simply look at the values of SCSISIGI here (if we want
4014568Sgibbs#  to do synchronous data transfer), because the target won't assert
4024568Sgibbs#  REQ if it's already sent us some data that we haven't acknowledged
4034568Sgibbs#  yet.
4044568Sgibbs#
4054568SgibbsITloop:
4064568Sgibbs	test	SSTAT1,0x8	jnz p_busfree	# BUSFREE
4074568Sgibbs	test	SSTAT1,0x1	jz ITloop	# REQINIT
4084568Sgibbs
4094568Sgibbs	and	A,0xe0,SCSISIGI			# CDI|IOI|MSGI
4104568Sgibbs
4114568Sgibbs	cmp	ALLZEROS,A	je p_dataout
4124568Sgibbs	cmp	A,0x40		je p_datain
4134568Sgibbs	cmp	A,0x80		je p_command
4144568Sgibbs	cmp	A,0xc0		je p_status
4154568Sgibbs	cmp	A,0xa0		je p_mesgout
4164568Sgibbs	cmp	A,0xe0		je p_mesgin
4174568Sgibbs
4185647Sgibbs	mvi	INTSTAT,BAD_PHASE		# unknown - signal driver
4194568Sgibbs
4204568Sgibbsp_dataout:
4214568Sgibbs	mvi	0		call scsisig	# !CDO|!IOO|!MSGO
4224568Sgibbs	call	assert
4234568Sgibbs	call	sg_load
4244568Sgibbs
4255775Sgibbs	mvi	DINDEX,HADDR
4265775Sgibbs	mvi	SCBARRAY+19	call bcopy_4
4274568Sgibbs
4285775Sgibbs#	mvi	DINDEX,HCNT	# implicit since HCNT is next to HADDR
4295775Sgibbs	mvi	SCBARRAY+23	call bcopy_3
4305775Sgibbs
4314568Sgibbs	mvi	DINDEX,STCNT
4325775Sgibbs	mvi	SCBARRAY+23	call bcopy_3
4334568Sgibbs
4347532Sgibbs# If we are the last SG block, don't set wideodd.
4357532Sgibbs	test    SCBARRAY+18,0xff jnz p_dataout_wideodd
4364568Sgibbs	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
4374568Sgibbs						#   DIRECTION|FIFORESET
4387532Sgibbs	jmp	p_dataout_rest
4394568Sgibbs
4407532Sgibbsp_dataout_wideodd:
4417532Sgibbs	mvi	0xbd		call dma	# WIDEODD|SCSIEN|SDMAEN|HDMAEN|
4427532Sgibbs						#   DIRECTION|FIFORESET
4437532Sgibbs
4447532Sgibbsp_dataout_rest:
4454568Sgibbs#  After a DMA finishes, save the final transfer pointer and count
4464568Sgibbs#  back into the SCB, in case a device disconnects in the middle of
4474568Sgibbs#  a transfer.  Use SHADDR and STCNT instead of HADDR and HCNT, since
4484568Sgibbs#  it's a reflection of how many bytes were transferred on the SCSI
4494568Sgibbs#  (as opposed to the host) bus.
4504568Sgibbs#
4514568Sgibbs	mvi	DINDEX,SCBARRAY+23
4525775Sgibbs	mvi	STCNT		call bcopy_3
4534568Sgibbs
4544568Sgibbs	mvi	DINDEX,SCBARRAY+19
4555775Sgibbs	mvi	SHADDR		call bcopy_4
4564568Sgibbs
4574568Sgibbs	call	sg_advance
4584568Sgibbs	mov	SCBARRAY+18,SG_COUNT		# residual S/G count
4594568Sgibbs
4604568Sgibbs	jmp	ITloop
4614568Sgibbs
4624568Sgibbsp_datain:
4634568Sgibbs	mvi	0x40		call scsisig	# !CDO|IOO|!MSGO
4644568Sgibbs	call	assert
4654568Sgibbs	call	sg_load
4664568Sgibbs
4675775Sgibbs	mvi	DINDEX,HADDR
4685775Sgibbs	mvi	SCBARRAY+19	call bcopy_4
4694568Sgibbs
4705775Sgibbs#	mvi	DINDEX,HCNT	# implicit since HCNT is next to HADDR
4715775Sgibbs	mvi	SCBARRAY+23	call bcopy_3
4725775Sgibbs
4734568Sgibbs	mvi	DINDEX,STCNT
4745775Sgibbs	mvi	SCBARRAY+23	call bcopy_3
4754568Sgibbs
4767532Sgibbs# If we are the last SG block, don't set wideodd.
4777532Sgibbs	test	SCBARRAY+18,0xff jnz p_datain_wideodd
4784568Sgibbs	mvi	0x39		call dma	# SCSIEN|SDMAEN|HDMAEN|
4794568Sgibbs						#   !DIRECTION|FIFORESET
4807532Sgibbs	jmp	p_datain_rest
4817532Sgibbsp_datain_wideodd:
4827532Sgibbs	mvi	0xb9		call dma	# WIDEODD|SCSIEN|SDMAEN|HDMAEN|
4837532Sgibbs						#   !DIRECTION|FIFORESET
4847532Sgibbsp_datain_rest:
4854568Sgibbs	mvi	DINDEX,SCBARRAY+23
4865775Sgibbs	mvi	STCNT		call bcopy_3
4874568Sgibbs
4884568Sgibbs	mvi	DINDEX,SCBARRAY+19
4895775Sgibbs	mvi	SHADDR		call bcopy_4
4904568Sgibbs
4914568Sgibbs	call	sg_advance
4924568Sgibbs	mov	SCBARRAY+18,SG_COUNT		# residual S/G count
4934568Sgibbs
4944568Sgibbs	jmp	ITloop
4954568Sgibbs
4964568Sgibbs#  Command phase.  Set up the DMA registers and let 'er rip - the
4974568Sgibbs#  two bytes after the SCB SCSI_cmd_length are zeroed by the driver,
4984568Sgibbs#  so we can copy those three bytes directly into HCNT.
4994568Sgibbs#
5004568Sgibbsp_command:
5014568Sgibbs	mvi	0x80		call scsisig	# CDO|!IOO|!MSGO
5024568Sgibbs	call	assert
5034568Sgibbs
5045775Sgibbs	mvi	DINDEX,HADDR
5055775Sgibbs	mvi	SCBARRAY+7	call bcopy_4
5064568Sgibbs
5075775Sgibbs#	mvi	DINDEX,HCNT	# implicit since HCNT is next to HADDR
5085775Sgibbs	mvi	SCBARRAY+11	call bcopy_3
5095775Sgibbs
5104568Sgibbs	mvi	DINDEX,STCNT
5115775Sgibbs	mvi	SCBARRAY+11	call bcopy_3
5124568Sgibbs
5134568Sgibbs	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
5144568Sgibbs						#   DIRECTION|FIFORESET
5154568Sgibbs	jmp	ITloop
5164568Sgibbs
5174568Sgibbs#  Status phase.  Wait for the data byte to appear, then read it
5184568Sgibbs#  and store it into the SCB.
5194568Sgibbs#
5204568Sgibbsp_status:
5214568Sgibbs	mvi	0xc0		call scsisig	# CDO|IOO|!MSGO
5224568Sgibbs
5234568Sgibbs	mvi	SCBARRAY+14	call inb
5244568Sgibbs	jmp	ITloop
5254568Sgibbs
5264568Sgibbs#  Message out phase.  If there is no active message, but the target
5274568Sgibbs#  took us into this phase anyway, build a no-op message and send it.
5284568Sgibbs#
5294568Sgibbsp_mesgout:
5304568Sgibbs	mvi	0xa0		call scsisig	# CDO|!IOO|MSGO
5314568Sgibbs	mvi	0x8		call mk_mesg	# build NOP message
5324568Sgibbs
5334568Sgibbs#  Set up automatic PIO transfer from MSG_START.  Bit 3 in
5344568Sgibbs#  SXFRCTL0 (SPIOEN) is already on.
5354568Sgibbs#
5364568Sgibbs	mvi	SINDEX,MSG_START+0
5374568Sgibbs	mov	DINDEX,MSG_LEN
5384568Sgibbs	clr	A
5394568Sgibbs
5404568Sgibbs#  When target asks for a byte, drop ATN if it's the last one in
5414568Sgibbs#  the message.  Otherwise, keep going until the message is exhausted.
5424568Sgibbs#  (We can't use outb for this since it wants the input in SINDEX.)
5434568Sgibbs#
5444568Sgibbs#  Keep an eye out for a phase change, in case the target issues
5454568Sgibbs#  a MESSAGE REJECT.
5464568Sgibbs#
5474568Sgibbsp_mesgout2:
5484568Sgibbs	test	SSTAT0,0x2	jz p_mesgout2	# SPIORDY
5494568Sgibbs	test	SSTAT1,0x10	jnz p_mesgout6	# PHASEMIS
5504568Sgibbs
5514568Sgibbs	cmp	DINDEX,1	jne p_mesgout3	# last byte?
5524568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO - drop ATN
5534568Sgibbs
5544568Sgibbs#  Write a byte to the SCSI bus.  The AIC-7770 refuses to automatically
5554568Sgibbs#  send ACKs in automatic PIO or DMA mode unless you make sure that the
5564568Sgibbs#  "expected" bus phase in SCSISIGO matches the actual bus phase.  This
5574568Sgibbs#  behaviour is completely undocumented and caused me several days of
5584568Sgibbs#  grief.
5594568Sgibbs#
5604568Sgibbs#  After plugging in different drives to test with and using a longer
5614568Sgibbs#  SCSI cable, I found that I/O in Automatic PIO mode ceased to function,
5624568Sgibbs#  especially when transferring >1 byte.  It seems to be much more stable
5634568Sgibbs#  if STCNT is set to one before the transfer, and SDONE (in SSTAT0) is
5644568Sgibbs#  polled for transfer completion - for both output _and_ input.  The
5654568Sgibbs#  only theory I have is that SPIORDY doesn't drop right away when SCSIDATL
5664568Sgibbs#  is accessed (like the documentation says it does), and that on a longer
5674568Sgibbs#  cable run, the sequencer code was fast enough to loop back and see
5684568Sgibbs#  an SPIORDY that hadn't dropped yet.
5694568Sgibbs#
5704568Sgibbsp_mesgout3:
5714568Sgibbs	call	one_stcnt
5724568Sgibbs	mov	SCSIDATL,SINDIR
5734568Sgibbs
5744568Sgibbsp_mesgout4:
5754568Sgibbs	test	SSTAT0,0x4	jz p_mesgout4	# SDONE
5764568Sgibbs	dec	DINDEX
5774568Sgibbs	inc	A
5784568Sgibbs	cmp	MSG_LEN,A	jne p_mesgout2
5794568Sgibbs
5804568Sgibbs#  If the next bus phase after ATN drops is a message out, it means
5814568Sgibbs#  that the target is requesting that the last message(s) be resent.
5824568Sgibbs#
5834568Sgibbsp_mesgout5:
5844568Sgibbs	test	SSTAT1,0x8	jnz p_mesgout6	# BUSFREE
5854568Sgibbs	test	SSTAT1,0x1	jz p_mesgout5	# REQINIT
5864568Sgibbs
5874568Sgibbs	and	A,0xe0,SCSISIGI			# CDI|IOI|MSGI
5884568Sgibbs	cmp	A,0xa0		jne p_mesgout6
5894568Sgibbs	mvi	0x10		call scsisig	# ATNO - re-assert ATN
5904568Sgibbs
5914568Sgibbs	jmp	ITloop
5924568Sgibbs
5934568Sgibbsp_mesgout6:
5944568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO - in case of PHASEMIS
5955562Sgibbs	and	FLAGS,0xdf			# no active msg
5964568Sgibbs	jmp	ITloop
5974568Sgibbs
5984568Sgibbs#  Message in phase.  Bytes are read using Automatic PIO mode, but not
5994568Sgibbs#  using inb.  This alleviates a race condition, namely that if ATN had
6004568Sgibbs#  to be asserted under Automatic PIO mode, it had to beat the SCSI
6014568Sgibbs#  circuitry sending an ACK to the target.  This showed up under heavy
6024568Sgibbs#  loads and really confused things, since ABORT commands wouldn't be
6034568Sgibbs#  seen by the drive after an IDENTIFY message in until it had changed
6044568Sgibbs#  to a data I/O phase.
6054568Sgibbs#
6064568Sgibbsp_mesgin:
6074568Sgibbs	mvi	0xe0		call scsisig	# CDO|IOO|MSGO
6084568Sgibbs	mvi	A		call inb_first	# read the 1st message byte
6094568Sgibbs	mvi	REJBYTE,A			# save it for the driver
6104568Sgibbs
6114568Sgibbs	cmp	ALLZEROS,A	jne p_mesgin1
6124568Sgibbs
6134568Sgibbs#  We got a "command complete" message, so put the SCB pointer
6144568Sgibbs#  into the Queue Out, and trigger a completion interrupt.
6154568Sgibbs#  Check status for non zero return and interrupt driver if needed
6165326Sgibbs#  This allows the driver to interpret errors only when they occur
6175326Sgibbs#  instead of always uploading the scb.  If the status is SCSI_CHECK,
6185326Sgibbs#  the driver will download a new scb requesting sense, to replace 
6197532Sgibbs#  the old one and set the SENSE sequencer flag.  If the sense flag is
6207532Sgibbs#  set, the sequencer imediately jumps to start working on the sense
6217532Sgibbs#  command.  If the kernel driver does not wish to request sense, it need
6227532Sgibbs#  do nothing, and the command is allowed to complete.  We don't 
6235326Sgibbs#  bother to post to the QOUTFIFO in the error case since it would require 
6245326Sgibbs#  extra work in the kernel driver to ensure that the entry was removed 
6255326Sgibbs#  before the command complete code tried processing it.
6264568Sgibbs
6277532Sgibbs	test	SCBARRAY+15,0xff	jnz resid
6287532Sgibbs	test	SCBARRAY+16,0xff	jnz resid
6297532Sgibbs	test	SCBARRAY+17,0xff	jnz resid
6307532Sgibbs
6317532Sgibbscheck_status:
6325647Sgibbs	test	SCBARRAY+14,0xff	jz status_ok	# 0 Status?
6335647Sgibbs	mvi	INTSTAT,BAD_STATUS			# let driver know
6345562Sgibbs	test	FLAGS,SENSE	jz status_ok
6355562Sgibbs	jmp	p_mesgin_done
6365326Sgibbs
6374568Sgibbsstatus_ok:
6385326Sgibbs#  First, mark this target as free.
6396608Sgibbs	test	SCBARRAY+0,0x20	jnz complete		# Tagged command
6405326Sgibbs	and	FUNCTION1,0x70,SCBARRAY+1
6415326Sgibbs	mov	A,FUNCTION1
6425326Sgibbs	test	SCBARRAY+1,0x88 jz clear_a
6435326Sgibbs	xor	ACTIVE_B,A
6445326Sgibbs	jmp	complete
6455326Sgibbs
6465326Sgibbsclear_a:
6475326Sgibbs	xor	ACTIVE_A,A
6485326Sgibbs
6495326Sgibbscomplete:
6504568Sgibbs	mov	QOUTFIFO,SCBPTR
6517532Sgibbs	mvi	INTSTAT,CMDCMPLT
6524568Sgibbs	jmp	p_mesgin_done
6534568Sgibbs
6547532Sgibbs# If we have a residual count, interrupt and tell the host.  Other
6557532Sgibbs# alternatives are to pause the sequencer on all command completes (yuck),
6567532Sgibbs# dma the resid directly to the host (slick, but a ton of instructions), or
6577532Sgibbs# have the sequencer pause itself when it encounters a non-zero resid 
6587532Sgibbs# (unecessary pause just to flag the command -- yuck, but takes few instructions
6597532Sgibbs# and since it shouldn't happen that offten is good enough for our purposes).  
6607532Sgibbs
6617532Sgibbsresid:
6627532Sgibbs	mvi	INTSTAT,RESIDUAL
6637532Sgibbs	jmp	check_status
6647532Sgibbs
6655562Sgibbs#  Is it an extended message?  We only support the synchronous and wide data
6665562Sgibbs#  transfer request messages, which will probably be in response to
6675562Sgibbs#  WDTR or SDTR message outs from us.  If it's not SDTR or WDTR, reject it -
6684568Sgibbs#  apparently this can be done after any message in byte, according
6694568Sgibbs#  to the SCSI-2 spec.
6704568Sgibbs#
6714568Sgibbsp_mesgin1:
6724568Sgibbs	cmp	A,1		jne p_mesgin2	# extended message code?
6734568Sgibbs	
6745562Sgibbs	mvi	ARG_1		call inb_next	# extended message length
6755562Sgibbs	mvi	A		call inb_next	# extended message code
6764568Sgibbs
6775562Sgibbs	cmp	A,1		je p_mesginSDTR	# Syncronous negotiation message
6785562Sgibbs	cmp	A,3		je p_mesginWDTR # Wide negotiation message
6795562Sgibbs	jmp	p_mesginN
6805562Sgibbs
6815562Sgibbsp_mesginWDTR:
6825562Sgibbs	cmp	ARG_1,2		jne p_mesginN	# extended mesg length = 2
6835562Sgibbs	mvi	A		call inb_next	# Width of bus
6845647Sgibbs	mvi	INTSTAT,MSG_WDTR		# let driver know
6855562Sgibbs	test	RETURN_1,0x80	jz p_mesgin_done# Do we need to send WDTR?
6865562Sgibbs
6875562Sgibbs# We didn't initiate the wide negotiation, so we must respond to the request
6885562Sgibbs	and	RETURN_1,0x7f			# Clear the SEND_WDTR Flag
6895562Sgibbs	or	FLAGS,ACTIVE_MSG
6905647Sgibbs	mvi	DINDEX,MSG_START+0
6915562Sgibbs	mvi	MSG_START+0	call mk_wdtr	# build WDTR message	
6925562Sgibbs	or	SINDEX,0x10,SIGSTATE		# turn on ATNO
6935562Sgibbs	call	scsisig
6945562Sgibbs	jmp	p_mesgin_done
6955562Sgibbs
6965562Sgibbsp_mesginSDTR:
6975562Sgibbs	cmp	ARG_1,3		jne p_mesginN	# extended mesg length = 3
6984568Sgibbs	mvi	ARG_1		call inb_next	# xfer period
6995326Sgibbs	mvi	A		call inb_next	# REQ/ACK offset
7005647Sgibbs	mvi	INTSTAT,MSG_SDTR		# call driver to convert
7014568Sgibbs
7025562Sgibbs	test	RETURN_1,0x80	jz p_mesgin_done# Do we need to mk_sdtr?
7035326Sgibbs
7045562Sgibbs	or	FLAGS,ACTIVE_MSG
7055647Sgibbs	mvi	DINDEX, MSG_START+0
7065562Sgibbs	mvi     MSG_START+0     call mk_sdtr
7075562Sgibbs	or	SINDEX,0x10,SIGSTATE		# turn on ATNO
7085562Sgibbs	call	scsisig
7094568Sgibbs	jmp	p_mesgin_done
7104568Sgibbs
7114568Sgibbs#  Is it a disconnect message?  Set a flag in the SCB to remind us
7124568Sgibbs#  and await the bus going free.
7134568Sgibbs#
7144568Sgibbsp_mesgin2:
7154568Sgibbs	cmp	A,4		jne p_mesgin3	# disconnect code?
7164568Sgibbs
7174568Sgibbs	or	SCBARRAY+0,0x4			# set "disconnected" bit
7184568Sgibbs	jmp	p_mesgin_done
7194568Sgibbs
7204568Sgibbs#  Save data pointers message?  Copy working values into the SCB,
7214568Sgibbs#  usually in preparation for a disconnect.
7224568Sgibbs#
7234568Sgibbsp_mesgin3:
7244568Sgibbs	cmp	A,2		jne p_mesgin4	# save data pointers code?
7254568Sgibbs
7264568Sgibbs	call	sg_ram2scb
7274568Sgibbs	jmp	p_mesgin_done
7284568Sgibbs
7294568Sgibbs#  Restore pointers message?  Data pointers are recopied from the
7304568Sgibbs#  SCB anyway at the start of any DMA operation, so the only thing
7314568Sgibbs#  to copy is the scatter-gather values.
7324568Sgibbs#
7334568Sgibbsp_mesgin4:
7344568Sgibbs	cmp	A,3		jne p_mesgin5	# restore pointers code?
7354568Sgibbs
7364568Sgibbs	call	sg_scb2ram
7374568Sgibbs	jmp	p_mesgin_done
7384568Sgibbs
7394568Sgibbs#  Identify message?  For a reconnecting target, this tells us the lun
7404568Sgibbs#  that the reconnection is for - find the correct SCB and switch to it,
7414568Sgibbs#  clearing the "disconnected" bit so we don't "find" it by accident later.
7424568Sgibbs#
7434568Sgibbsp_mesgin5:
7444568Sgibbs	test	A,0x80		jz p_mesgin6	# identify message?
7454568Sgibbs
7464568Sgibbs	test	A,0x78		jnz p_mesginN	# !DiscPriv|!LUNTAR|!Reserved
7474568Sgibbs
7487118Sgibbs	and	A,0x07				# lun in lower three bits
7496608Sgibbs	or      SAVED_TCL,A,SELID          
7506608Sgibbs	and     SAVED_TCL,0xf7
7516608Sgibbs	and     A,0x08,SBLKCTL			# B Channel??
7526608Sgibbs	or      SAVED_TCL,A
7536608Sgibbs	call	inb_last			# Ack
7544568Sgibbs
7556608Sgibbs# Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
7566608Sgibbs# If we get one, we use the tag returned to switch to the proper
7576608Sgibbs# SCB.  Otherwise, we just use the findSCB method.
7586608Sgibbsp_mesgin5_loop:
7597118Sgibbs	test	SSTAT1,0x08	jnz use_findSCB	  # BUSFREE
7607118Sgibbs	test	SSTAT1,0x01	jz p_mesgin5_loop # REQINIT
7616608Sgibbs	and	A,0xe0,SCSISIGI			# CDI|IOI|MSGI
7626608Sgibbs	cmp	A,0xe0		jne use_findSCB # Still p_mesgin?
7636608Sgibbs	mvi	A	call inb_first
7646608Sgibbs	cmp	A,0x20  je get_tag		# Simple Tag message?
7656608Sgibbsuse_findSCB:
7666608Sgibbs	mov	ALLZEROS	call findSCB    # Have to search
7676608Sgibbs
7684568Sgibbs#  If a active message is present after calling findSCB, then either it
7694568Sgibbs#  or the driver is trying to abort the command.  Either way, something
7704568Sgibbs#  untoward has happened and we should just leave it alone.
7714568Sgibbs#
7726608Sgibbssetup_SCB:
7735562Sgibbs	test	FLAGS,ACTIVE_MSG	jnz p_mesgin_done
7744568Sgibbs
7755326Sgibbs	and	SCBARRAY+0,0xfb			# clear disconnect bit in SCB
7765562Sgibbs	or	FLAGS,0xc0			# make note of IDENTIFY
7774568Sgibbs
7784568Sgibbs	call	sg_scb2ram			# implied restore pointers
7794568Sgibbs						#   required on reselect
7806608Sgibbs	jmp	ITloop
7814568Sgibbs
7826608Sgibbsget_tag:
7836608Sgibbs	mvi	A		call inb_next
7846608Sgibbs	test	A,0xf0		jnz abort_tag	# Tag in range?
7856608Sgibbs	mov	SCBPTR,A
7866608Sgibbs	mov	A,SAVED_TCL
7876608Sgibbs	cmp	SCBARRAY+1,A		jne abort_tag
7886608Sgibbs	test	SCBARRAY+0,TAG_ENB	jz  abort_tag
7896608Sgibbs	call	inb_last
7906608Sgibbs	jmp	setup_SCB
7916608Sgibbs
7925647Sgibbs#  Message reject?  Let the kernel driver handle this.  If we have an 
7935647Sgibbs#  outstanding WDTR or SDTR negotiation, assume that it's a response from 
7945647Sgibbs#  the target selecting 8bit or asynchronous transfer, otherwise just ignore 
7955647Sgibbs#  it since we have no clue what it pertains to.
7964568Sgibbs#
7974568Sgibbsp_mesgin6:
7984568Sgibbs	cmp	A,7		jne p_mesgin7	# message reject code?
7994568Sgibbs
8005647Sgibbs	mvi	INTSTAT, MSG_REJECT
8015562Sgibbs	jmp	p_mesgin_done
8025562Sgibbs
8034568Sgibbs#  [ ADD MORE MESSAGE HANDLING HERE ]
8044568Sgibbs#
8054568Sgibbsp_mesgin7:
8064568Sgibbs
8074568Sgibbs#  We have no idea what this message in is, and there's no way
8084568Sgibbs#  to pass it up to the kernel, so we issue a message reject and
8094568Sgibbs#  hope for the best.  Since we're now using manual PIO mode to
8104568Sgibbs#  read in the message, there should no longer be a race condition
8114568Sgibbs#  present when we assert ATN.  In any case, rejection should be a
8124568Sgibbs#  rare occurrence - signal the driver when it happens.
8134568Sgibbs#
8144568Sgibbsp_mesginN:
8154568Sgibbs	or	SINDEX,0x10,SIGSTATE		# turn on ATNO
8164568Sgibbs	call	scsisig
8175647Sgibbs	mvi	INTSTAT,SEND_REJECT		# let driver know
8184568Sgibbs
8194568Sgibbs	mvi	0x7		call mk_mesg	# MESSAGE REJECT message
8204568Sgibbs
8214568Sgibbsp_mesgin_done:
8224568Sgibbs	call	inb_last			# ack & turn auto PIO back on
8234568Sgibbs	jmp	ITloop
8244568Sgibbs
8256608Sgibbsabort_tag:
8266608Sgibbs	or	SINDEX,0x10,SIGSTATE		# turn on ATNO
8276608Sgibbs	call	scsisig
8286608Sgibbs#	mvi	INTSTAT,ABORT_TAG 		# let driver know
8296608Sgibbs	mvi	0xd		call mk_mesg	# ABORT TAG message
8306608Sgibbs	jmp	p_mesgin_done
8316608Sgibbs
8324568Sgibbs#  Bus free phase.  It might be useful to interrupt the device
8334568Sgibbs#  driver if we aren't expecting this.  For now, make sure that
8344568Sgibbs#  ATN isn't being asserted and look for a new command.
8354568Sgibbs#
8364568Sgibbsp_busfree:
8374568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO
8384568Sgibbs	clr	SIGSTATE
8394568Sgibbs	jmp	start
8404568Sgibbs
8415775Sgibbs#  Instead of a generic bcopy routine that requires an argument, we unroll
8425775Sgibbs#  the two cases that are actually used, and call them explicitly.  This
8435775Sgibbs#  not only reduces the overhead of doing a bcopy by 2/3rds, but ends up
8445775Sgibbs#  saving space in the program since you don't have to put the argument 
8455775Sgibbs#  into the accumulator before the call.  Both functions expect DINDEX to
8465775Sgibbs#  contain the destination address and SINDEX to contain the source 
8475775Sgibbs#  address.
8485775Sgibbsbcopy_3:
8494568Sgibbs	mov	DINDIR,SINDIR
8505775Sgibbs	mov	DINDIR,SINDIR
8515775Sgibbs	mov	DINDIR,SINDIR	ret
8524568Sgibbs
8535775Sgibbsbcopy_4:
8545775Sgibbs	mov	DINDIR,SINDIR
8555775Sgibbs	mov	DINDIR,SINDIR
8565775Sgibbs	mov	DINDIR,SINDIR
8575775Sgibbs	mov	DINDIR,SINDIR	ret
8585775Sgibbs	
8597532Sgibbsbcopy_3_dfdat:
8607532Sgibbs	mov	DINDIR,DFDAT
8617532Sgibbs	mov	DINDIR,DFDAT
8627532Sgibbs	mov	DINDIR,DFDAT	ret
8635775Sgibbs
8647532Sgibbsbcopy_4_dfdat:
8657532Sgibbs	mov	DINDIR,DFDAT
8667532Sgibbs	mov	DINDIR,DFDAT
8677532Sgibbs	mov	DINDIR,DFDAT
8687532Sgibbs	mov	DINDIR,DFDAT	ret
8697532Sgibbs
8704568Sgibbs#  Locking the driver out, build a one-byte message passed in SINDEX
8714568Sgibbs#  if there is no active message already.  SINDEX is returned intact.
8724568Sgibbs#
8734568Sgibbsmk_mesg:
8744866Sgibbs	mvi	SEQCTL,0x50			# PAUSEDIS|FASTMODE
8755562Sgibbs	test	FLAGS,ACTIVE_MSG jnz mk_mesg1	# active message?
8764568Sgibbs
8775562Sgibbs	or	FLAGS,ACTIVE_MSG		# if not, there is now
8784568Sgibbs	mvi	MSG_LEN,1			# length = 1
8794568Sgibbs	mov	MSG_START+0,SINDEX		# 1-byte message
8804568Sgibbs
8814568Sgibbsmk_mesg1:
8825775Sgibbs	mvi	SEQCTL,0x10	ret		# !PAUSEDIS|FASTMODE
8834568Sgibbs
8844568Sgibbs#  Input byte in Automatic PIO mode.  The address to store the byte
8854568Sgibbs#  in should be in SINDEX.  DINDEX will be used by this routine.
8864568Sgibbs#
8874568Sgibbsinb:
8884568Sgibbs	test	SSTAT0,0x2	jz inb		# SPIORDY
8894568Sgibbs	mov	DINDEX,SINDEX
8904568Sgibbs	call	one_stcnt			# xfer one byte
8914568Sgibbs	mov	DINDIR,SCSIDATL
8924568Sgibbsinb1:
8934568Sgibbs	test	SSTAT0,0x4	jz inb1		# SDONE - wait to "finish"
8944568Sgibbs	ret
8954568Sgibbs
8964568Sgibbs#  Carefully read data in Automatic PIO mode.  I first tried this using
8974568Sgibbs#  Manual PIO mode, but it gave me continual underrun errors, probably
8984568Sgibbs#  indicating that I did something wrong, but I feel more secure leaving
8994568Sgibbs#  Automatic PIO on all the time.
9004568Sgibbs#
9014568Sgibbs#  According to Adaptec's documentation, an ACK is not sent on input from
9024568Sgibbs#  the target until SCSIDATL is read from.  So we wait until SCSIDATL is
9034568Sgibbs#  latched (the usual way), then read the data byte directly off the bus
9044568Sgibbs#  using SCSIBUSL.  When we have pulled the ATN line, or we just want to
9054568Sgibbs#  acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
9064568Sgibbs#  spec guarantees that the target will hold the data byte on the bus until
9074568Sgibbs#  we send our ACK.
9084568Sgibbs#
9094568Sgibbs#  The assumption here is that these are called in a particular sequence,
9104568Sgibbs#  and that REQ is already set when inb_first is called.  inb_{first,next}
9114568Sgibbs#  use the same calling convention as inb.
9124568Sgibbs#
9134568Sgibbsinb_first:
9144568Sgibbs	mov	DINDEX,SINDEX
9154568Sgibbs	mov	DINDIR,SCSIBUSL	ret		# read byte directly from bus
9164568Sgibbs
9174568Sgibbsinb_next:
9184568Sgibbs	mov	DINDEX,SINDEX			# save SINDEX
9194568Sgibbs
9204568Sgibbs	call	one_stcnt			# xfer one byte
9214568Sgibbs	mov	NONE,SCSIDATL			# dummy read from latch to ACK
9224568Sgibbsinb_next1:
9234568Sgibbs	test	SSTAT0,0x4	jz inb_next1	# SDONE
9244568Sgibbsinb_next2:
9254568Sgibbs	test	SSTAT0,0x2	jz inb_next2	# SPIORDY - wait for next byte
9264568Sgibbs	mov	DINDIR,SCSIBUSL	ret		# read byte directly from bus
9274568Sgibbs
9284568Sgibbsinb_last:
9294568Sgibbs	call	one_stcnt			# ACK with dummy read
9304568Sgibbs	mov	NONE,SCSIDATL
9314568Sgibbsinb_last1:
9324568Sgibbs	test	SSTAT0,0x4	jz inb_last1	# wait for completion
9334568Sgibbs	ret
9344568Sgibbs
9354568Sgibbs#  Output byte in Automatic PIO mode.  The byte to output should be
9364568Sgibbs#  in SINDEX.  If DROPATN's high bit is set, then ATN will be dropped
9374568Sgibbs#  before the byte is output.
9384568Sgibbs#
9394568Sgibbsoutb:
9404568Sgibbs	test	SSTAT0,0x2	jz outb		# SPIORDY
9414568Sgibbs	call	one_stcnt			# xfer one byte
9424568Sgibbs
9434568Sgibbs	test	DROPATN,0x80	jz outb1
9444568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO
9454568Sgibbs	clr	DROPATN
9464568Sgibbsoutb1:
9474568Sgibbs	mov	SCSIDATL,SINDEX
9484568Sgibbsoutb2:
9494568Sgibbs	test	SSTAT0,0x4	jz outb2	# SDONE
9504568Sgibbs	ret
9514568Sgibbs
9524568Sgibbs#  Write the value "1" into the STCNT registers, for Automatic PIO
9534568Sgibbs#  transfers.
9544568Sgibbs#
9554568Sgibbsone_stcnt:
9564568Sgibbs	clr	STCNT+2
9574568Sgibbs	clr	STCNT+1
9584568Sgibbs	mvi	STCNT+0,1	ret
9594568Sgibbs
9604568Sgibbs#  DMA data transfer.  HADDR and HCNT must be loaded first, and
9614568Sgibbs#  SINDEX should contain the value to load DFCNTRL with - 0x3d for
9624568Sgibbs#  host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
9634568Sgibbs#  during initialization.
9644568Sgibbs#
9654568Sgibbsdma:
9664568Sgibbs	mov	DFCNTRL,SINDEX
9674568Sgibbsdma1:
9684568Sgibbsdma2:
9694568Sgibbs	test	SSTAT0,0x1	jnz dma3	# DMADONE
9704568Sgibbs	test	SSTAT1,0x10	jz dma1		# PHASEMIS, ie. underrun
9714568Sgibbs
9724568Sgibbs#  We will be "done" DMAing when the transfer count goes to zero, or
9734568Sgibbs#  the target changes the phase (in light of this, it makes sense that
9744568Sgibbs#  the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
9754866Sgibbs#  doing a SCSI->Host transfer, the data FIFO should be flushed auto-
9764866Sgibbs#  magically on STCNT=0 or a phase change, so just wait for FIFO empty
9774866Sgibbs#  status.
9784568Sgibbs#
9794568Sgibbsdma3:
9804568Sgibbs	test	SINDEX,0x4	jnz dma5	# DIRECTION
9814568Sgibbsdma4:
9825562Sgibbs	test	DFSTATUS,0x1	jz dma4		# !FIFOEMP
9834568Sgibbs
9844568Sgibbs#  Now shut the DMA enables off, and copy STCNT (ie. the underrun
9854568Sgibbs#  amount, if any) to the SCB registers; SG_COUNT will get copied to
9864568Sgibbs#  the SCB's residual S/G count field after sg_advance is called.  Make
9874568Sgibbs#  sure that the DMA enables are actually off first lest we get an ILLSADDR.
9884568Sgibbs#
9894568Sgibbsdma5:
9904568Sgibbs	clr	DFCNTRL				# disable DMA
9914568Sgibbsdma6:
9924568Sgibbs	test	DFCNTRL,0x38	jnz dma6	# SCSIENACK|SDMAENACK|HDMAENACK
9934568Sgibbs
9944568Sgibbs	mvi	DINDEX,SCBARRAY+15
9955775Sgibbs	mvi	STCNT		call bcopy_3
9964568Sgibbs
9974568Sgibbs	ret
9984568Sgibbs
9997532Sgibbsdma_finish:
10007532Sgibbs	test	DFSTATUS,0x8	jz dma_finish	# HDONE
10017532Sgibbs
10027532Sgibbs	clr	DFCNTRL				# disable DMA
10037532Sgibbsdma_finish2:
10047532Sgibbs	test	DFCNTRL,0x8	jnz dma_finish2	# HDMAENACK
10057532Sgibbs	ret
10067532Sgibbs
10074568Sgibbs#  Common SCSI initialization for selection and reselection.  Expects
10084568Sgibbs#  the target SCSI ID to be in the upper four bits of SINDEX, and A's
10094568Sgibbs#  contents are stomped on return.
10104568Sgibbs#
10114568Sgibbsinitialize:
10125775Sgibbs	and	SINDEX,0xf0		# Get target ID
10135775Sgibbs	and	A,0x0f,SCSIID
10145775Sgibbs	or	SINDEX,A
10155775Sgibbs	mov	SCSIID,SINDEX
10165326Sgibbs
10174568Sgibbs#  Esundry initialization.
10184568Sgibbs#
10194568Sgibbs	clr	DROPATN
10204568Sgibbs	clr	SIGSTATE
10214568Sgibbs
10225775Sgibbs#  Turn on Automatic PIO mode now, before we expect to see a REQ
10234568Sgibbs#  from the target.  It shouldn't hurt anything to leave it on.  Set
10244568Sgibbs#  CLRCHN here before the target has entered a data transfer mode -
10254568Sgibbs#  with synchronous SCSI, if you do it later, you blow away some
10264568Sgibbs#  data in the SCSI FIFO that the target has already sent to you.
10274568Sgibbs#
10284866Sgibbs	mvi	SXFRCTL0,0x8a			# DFON|SPIOEN|CLRCHN
10294568Sgibbs
10304568Sgibbs#  Initialize scatter-gather pointers by setting up the working copy
10314568Sgibbs#  in scratch RAM.
10324568Sgibbs#
10334568Sgibbs	call	sg_scb2ram
10344568Sgibbs
10354568Sgibbs#  Initialize SCSIRATE with the appropriate value for this target.
10364568Sgibbs#
10375562Sgibbs	call	ndx_dtr
10385775Sgibbs	mov	SCSIRATE,SINDIR	ret
10394568Sgibbs
10404568Sgibbs#  Assert that if we've been reselected, then we've seen an IDENTIFY
10414568Sgibbs#  message.
10424568Sgibbs#
10434568Sgibbsassert:
10447532Sgibbs	test	FLAGS,RESELECTED	jz return	# reselected?
10457532Sgibbs	test	FLAGS,IDENTIFY_SEEN	jnz return	# seen IDENTIFY?
10464568Sgibbs
10477532Sgibbs	mvi	INTSTAT,NO_IDENT 	ret	# no - cause a kernel panic
10484568Sgibbs
10494568Sgibbs#  Find out if disconnection is ok from the information the BIOS has left
10505326Sgibbs#  us.  The tcl from SCBARRAY+1 should be in SINDEX; A will
10514866Sgibbs#  contain either 0x40 (disconnection ok) or 0x00 (disconnection not ok)
10524568Sgibbs#  on exit.
10534568Sgibbs#
10545326Sgibbs#  To allow for wide or twin busses, we check the upper bit of the target ID
10555326Sgibbs#  and the channel ID and look at the appropriate disconnect register. 
10564568Sgibbs#
10574568Sgibbsdisconnect:
10584568Sgibbs	and	FUNCTION1,0x70,SINDEX		# strip off extra just in case
10594568Sgibbs	mov	A,FUNCTION1
10605326Sgibbs	test	SINDEX, 0x88	jz disconnect_a
10615326Sgibbs
10625326Sgibbs	test	DISC_DSB_B,A	jz disconnect1	# bit nonzero if DISabled
10635326Sgibbs	clr	A		ret
10645326Sgibbs
10655326Sgibbsdisconnect_a:
10664568Sgibbs	test	DISC_DSB_A,A	jz disconnect1	# bit nonzero if DISabled
10675326Sgibbs	clr	A		ret
10684568Sgibbs
10694568Sgibbsdisconnect1:
10704568Sgibbs	mvi	A,0x40		ret
10714568Sgibbs
10726608Sgibbs#  Locate the SCB matching the target ID/channel/lun in SAVED_TCL and switch 
10736608Sgibbs#  the SCB to it.  Have the kernel print a warning message if it can't be 
10746608Sgibbs#  found, and generate an ABORT message to the target.  SINDEX should be
10756608Sgibbs#  cleared on call.
10764568Sgibbs#
10774568SgibbsfindSCB:
10786608Sgibbs	mov	A,SAVED_TCL
10794568Sgibbs	mov	SCBPTR,SINDEX			# switch to new SCB
10806608Sgibbs	cmp	SCBARRAY+1,A	jne findSCB1	# target ID/channel/lun match?
10816608Sgibbs	test	SCBARRAY+0,0x4	jz findSCB1	# should be disconnected
10824568Sgibbs	ret
10834568Sgibbs
10846608SgibbsfindSCB1:
10854568Sgibbs	inc	SINDEX
10864568Sgibbs	mov	A,SCBCOUNT
10876608Sgibbs	cmp	SINDEX,A	jne findSCB
10884568Sgibbs
10895647Sgibbs	mvi	INTSTAT,NO_MATCH		# not found - signal kernel
10904568Sgibbs	mvi	0x6		call mk_mesg	# ABORT message
10914568Sgibbs
10924568Sgibbs	or	SINDEX,0x10,SIGSTATE		# assert ATNO
10934568Sgibbs	call	scsisig
10944568Sgibbs	ret
10954568Sgibbs
10964568Sgibbs#  Make a working copy of the scatter-gather parameters in the SCB.
10974568Sgibbs#
10984568Sgibbssg_scb2ram:
10994568Sgibbs	mov	SG_COUNT,SCBARRAY+2
11004568Sgibbs
11014568Sgibbs	mvi	DINDEX,SG_NEXT
11025775Sgibbs	mvi	SCBARRAY+3	call bcopy_4
11034568Sgibbs
11044568Sgibbs	mvi	SG_NOLOAD,0x80
11057532Sgibbs	test	SCBARRAY+0,0x10	jnz return	# don't reload s/g?
11067532Sgibbs	clr	SG_NOLOAD	 ret
11074568Sgibbs
11084568Sgibbs#  Copying RAM values back to SCB, for Save Data Pointers message.
11094568Sgibbs#
11104568Sgibbssg_ram2scb:
11114568Sgibbs	mov	SCBARRAY+2,SG_COUNT
11124568Sgibbs
11134568Sgibbs	mvi	DINDEX,SCBARRAY+3
11145775Sgibbs	mvi	SG_NEXT		call bcopy_4
11154568Sgibbs
11164568Sgibbs	and	SCBARRAY+0,0xef,SCBARRAY+0
11177532Sgibbs	test	SG_NOLOAD,0x80	jz return	# reload s/g?
11187532Sgibbs	or	SCBARRAY+0,SG_LOAD	 ret
11194568Sgibbs
11204568Sgibbs#  Load a struct scatter if needed and set up the data address and
11214568Sgibbs#  length.  If the working value of the SG count is nonzero, then
11224568Sgibbs#  we need to load a new set of values.
11234568Sgibbs#
11244568Sgibbs#  This, like the above DMA, assumes a little-endian host data storage.
11254568Sgibbs#
11264568Sgibbssg_load:
11277532Sgibbs	test	SG_COUNT,0xff	jz return	# SG being used?
11287532Sgibbs	test	SG_NOLOAD,0x80	jnz return	# don't reload s/g?
11294568Sgibbs
11304568Sgibbs	clr	HCNT+2
11314568Sgibbs	clr	HCNT+1
11324568Sgibbs	mvi	HCNT+0,SG_SIZEOF
11334568Sgibbs
11344568Sgibbs	mvi	DINDEX,HADDR
11355775Sgibbs	mvi	SG_NEXT		call bcopy_4
11364568Sgibbs
11374568Sgibbs	mvi	DFCNTRL,0xd			# HDMAEN|DIRECTION|FIFORESET
11384568Sgibbs
11394568Sgibbs#  Wait for DMA from host memory to data FIFO to complete, then disable
11404568Sgibbs#  DMA and wait for it to acknowledge that it's off.
11414568Sgibbs#
11424568Sgibbs
11437532Sgibbs	call	dma_finish
11444568Sgibbs
11454568Sgibbs#  Copy data from FIFO into SCB data pointer and data count.  This assumes
11464568Sgibbs#  that the struct scatterlist has this structure (this and sizeof(struct
11474568Sgibbs#  scatterlist) == 12 are asserted in aic7xxx.c):
11484568Sgibbs#
11494568Sgibbs#	struct scatterlist {
11504568Sgibbs#		char *address;		/* four bytes, little-endian order */
11514568Sgibbs#		...			/* four bytes, ignored */
11524568Sgibbs#		unsigned short length;	/* two bytes, little-endian order */
11534568Sgibbs#	}
11544568Sgibbs#
11554568Sgibbs
11567532Sgibbs# Not in FreeBSD.  the scatter list entry is only 8 bytes.
11574568Sgibbs# 
11584568Sgibbs# struct ahc_dma_seg {
11594568Sgibbs#       physaddr addr;                  /* four bytes, little-endian order */
11604568Sgibbs#       long    len;                    /* four bytes, little endian order */   
11614568Sgibbs# };
11624568Sgibbs#
11634568Sgibbs
11647532Sgibbs	mvi	DINDEX, SCBARRAY+19
11657532Sgibbs	call	bcopy_4_dfdat
11664568Sgibbs
11675647Sgibbs# For Linux, we must throw away four bytes since there is a 32bit gap
11685647Sgibbs# in the middle of a struct scatterlist
11695647Sgibbs#	mov	NONE,DFDAT
11705647Sgibbs#	mov	NONE,DFDAT
11715647Sgibbs#	mov	NONE,DFDAT
11725647Sgibbs#	mov	NONE,DFDAT
11735647Sgibbs
11747532Sgibbs	call	bcopy_3_dfdat		#Only support 24 bit length.
11754568Sgibbs	ret
11764568Sgibbs
11774568Sgibbs#  Advance the scatter-gather pointers only IF NEEDED.  If SG is enabled,
11784568Sgibbs#  and the SCSI transfer count is zero (note that this should be called
11794568Sgibbs#  right after a DMA finishes), then move the working copies of the SG
11804568Sgibbs#  pointer/length along.  If the SCSI transfer count is not zero, then
11814568Sgibbs#  presumably the target is disconnecting - do not reload the SG values
11824568Sgibbs#  next time.
11834568Sgibbs#
11844568Sgibbssg_advance:
11857532Sgibbs	test	SG_COUNT,0xff	jz return	# s/g enabled?
11864568Sgibbs
11874568Sgibbs	test	STCNT+0,0xff	jnz sg_advance1	# SCSI transfer count nonzero?
11884568Sgibbs	test	STCNT+1,0xff	jnz sg_advance1
11894568Sgibbs	test	STCNT+2,0xff	jnz sg_advance1
11904568Sgibbs
11914568Sgibbs	clr	SG_NOLOAD			# reload s/g next time
11924568Sgibbs	dec	SG_COUNT			# one less segment to go
11934568Sgibbs
11944568Sgibbs	clr	A				# add sizeof(struct scatter)
11954568Sgibbs	add	SG_NEXT+0,SG_SIZEOF,SG_NEXT+0
11964568Sgibbs	adc	SG_NEXT+1,A,SG_NEXT+1
11974568Sgibbs	adc	SG_NEXT+2,A,SG_NEXT+2
11985775Sgibbs	adc	SG_NEXT+3,A,SG_NEXT+3	ret
11994568Sgibbs
12004568Sgibbssg_advance1:
12017532Sgibbs	mvi	SG_NOLOAD,0x80	ret		# don't reload s/g next time
12024568Sgibbs
12034568Sgibbs#  Add the array base SYNCNEG to the target offset (the target address
12044568Sgibbs#  is in SCSIID), and return the result in SINDEX.  The accumulator
12054568Sgibbs#  contains the 3->8 decoding of the target ID on return.
12064568Sgibbs#
12075562Sgibbsndx_dtr:
12084568Sgibbs	shr	A,SCSIID,4
12095562Sgibbs	test	SBLKCTL,0x08	jz ndx_dtr_2
12105326Sgibbs	or	A,0x08		# Channel B entries add 8
12115562Sgibbsndx_dtr_2:
12124568Sgibbs	add	SINDEX,SYNCNEG,A
12134568Sgibbs
12144568Sgibbs	and	FUNCTION1,0x70,SCSIID		# 3-bit target address decode
12154568Sgibbs	mov	A,FUNCTION1	ret
12164568Sgibbs
12175562Sgibbs#  If we need to negotiate transfer parameters, build the WDTR or SDTR message
12184568Sgibbs#  starting at the address passed in SINDEX.  DINDEX is modified on return.
12195562Sgibbs#  The SCSI-II spec requires that Wide negotiation occur first and you can
12205562Sgibbs#  only negotiat one or the other at a time otherwise in the event of a message
12215562Sgibbs#  reject, you wouldn't be able to tell which message was the culpret.
12224568Sgibbs#
12235562Sgibbsmk_dtr:
12247532Sgibbs	test	SCBARRAY+0,0xc0 jz return	# NEEDWDTR|NEEDSDTR
12255647Sgibbs	test	SCBARRAY+0,NEEDWDTR jnz  mk_wdtr_16bit
12267532Sgibbs	or	FLAGS, MAX_SYNC		 # Force an offset of 15
12274568Sgibbs
12287532Sgibbsmk_sdtr:
12297532Sgibbs	mvi	DINDIR,1			# extended message
12307532Sgibbs	mvi	DINDIR,3			# extended message length = 3
12317532Sgibbs	mvi	DINDIR,1			# SDTR code
12327532Sgibbs	call	sdtr_to_rate
12337532Sgibbs	mov	DINDIR,RETURN_1			# REQ/ACK transfer period
12347532Sgibbs	test	FLAGS, MAX_SYNC	jnz mk_sdtr_max_sync
12357532Sgibbs	and	DINDIR,0xf,SINDIR		# Sync Offset
12367532Sgibbs
12377532Sgibbsmk_sdtr_done:
12387532Sgibbs	add	MSG_LEN,-MSG_START+0,DINDEX ret	# update message length
12397532Sgibbs
12407532Sgibbsmk_sdtr_max_sync:
12417532Sgibbs# We're initiating sync negotiation, so request the max offset we can (15)
12427532Sgibbs	mvi	DINDIR, 0x0f
12437532Sgibbs	xor	FLAGS, MAX_SYNC
12447532Sgibbs	jmp	mk_sdtr_done
12457532Sgibbs
12465647Sgibbsmk_wdtr_16bit:
12475647Sgibbs	mvi	ARG_1,BUS_16_BIT
12485562Sgibbsmk_wdtr:
12494568Sgibbs	mvi	DINDIR,1			# extended message
12505562Sgibbs	mvi	DINDIR,2			# extended message length = 2
12515562Sgibbs	mvi	DINDIR,3			# WDTR code
12525562Sgibbs	mov	DINDIR,ARG_1			# bus width
12535562Sgibbs
12545775Sgibbs	add	MSG_LEN,-MSG_START+0,DINDEX ret	# update message length
12555562Sgibbs	
12564568Sgibbs#  Set SCSI bus control signal state.  This also saves the last-written
12574568Sgibbs#  value into a location where the higher-level driver can read it - if
12584568Sgibbs#  it has to send an ABORT or RESET message, then it needs to know this
12594568Sgibbs#  so it can assert ATN without upsetting SCSISIGO.  The new value is
12604568Sgibbs#  expected in SINDEX.  Change the actual state last to avoid contention
12614568Sgibbs#  from the driver.
12624568Sgibbs#
12634568Sgibbsscsisig:
12644568Sgibbs	mov	SIGSTATE,SINDEX
12654568Sgibbs	mov	SCSISIGO,SINDEX	ret
12665562Sgibbs
12675562Sgibbssdtr_to_rate:
12685562Sgibbs	call	ndx_dtr				# index scratch space for target
12695562Sgibbs	shr	A,SINDIR,0x4
12705562Sgibbs	dec	SINDEX				#Preserve SINDEX
12715562Sgibbs	and	A,0x7
12725562Sgibbs	clr	RETURN_1
12735562Sgibbssdtr_to_rate_loop:
12745562Sgibbs	test	A,0x0f	jz sdtr_to_rate_done
12755562Sgibbs	add	RETURN_1,0x18
12765562Sgibbs	dec	A	
12775562Sgibbs	jmp	sdtr_to_rate_loop
12785562Sgibbssdtr_to_rate_done:
12795562Sgibbs	shr	RETURN_1,0x2
12805562Sgibbs	add	RETURN_1,0x18	ret
12817532Sgibbs
12827532Sgibbsreturn:
12837532Sgibbs	ret
1284