aic7xxx.seq revision 5562
15562Sgibbs# @(#)aic7xxx.seq 1.32 94/11/29 jda
24568Sgibbs#
35562Sgibbs# Adaptec 274x/284x/294x device driver for Linux.
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.
194568Sgibbs
205562SgibbsVERSION AIC7XXX_SEQ_VERSION 1.32
214568Sgibbs
224866SgibbsSCBMASK		= 0x1f
234568Sgibbs
244568SgibbsSCSISEQ		= 0x00
254568SgibbsSXFRCTL0	= 0x01
264568SgibbsSXFRCTL1	= 0x02
274568SgibbsSCSISIGI	= 0x03
284568SgibbsSCSISIGO	= 0x03
294568SgibbsSCSIRATE	= 0x04
304568SgibbsSCSIID		= 0x05
314568SgibbsSCSIDATL	= 0x06
324568SgibbsSTCNT		= 0x08
334568SgibbsSTCNT+0		= 0x08
344568SgibbsSTCNT+1		= 0x09
354568SgibbsSTCNT+2		= 0x0a
364568SgibbsSSTAT0		= 0x0b
374568SgibbsCLRSINT1	= 0x0c
384568SgibbsSSTAT1		= 0x0c
394568SgibbsSIMODE1		= 0x11
404568SgibbsSCSIBUSL	= 0x12
414568SgibbsSHADDR		= 0x14
424568SgibbsSELID		= 0x19
434568SgibbsSBLKCTL		= 0x1f
444568SgibbsSEQCTL		= 0x60
454568SgibbsA		= 0x64				# == ACCUM
464568SgibbsSINDEX		= 0x65
474568SgibbsDINDEX		= 0x66
484568SgibbsALLZEROS	= 0x6a
494568SgibbsNONE		= 0x6a
504568SgibbsSINDIR		= 0x6c
514568SgibbsDINDIR		= 0x6d
524568SgibbsFUNCTION1	= 0x6e
534568SgibbsHADDR		= 0x88
544568SgibbsHCNT		= 0x8c
554568SgibbsHCNT+0		= 0x8c
564568SgibbsHCNT+1		= 0x8d
574568SgibbsHCNT+2		= 0x8e
584568SgibbsSCBPTR		= 0x90
594568SgibbsINTSTAT		= 0x91
604568SgibbsDFCNTRL		= 0x93
614568SgibbsDFSTATUS	= 0x94
624568SgibbsDFDAT		= 0x99
634568SgibbsQINFIFO		= 0x9b
644568SgibbsQINCNT		= 0x9c
654568SgibbsQOUTFIFO	= 0x9d
664568Sgibbs
675326SgibbsSCSICONF_A	= 0x5a
685326SgibbsSCSICONF_B	= 0x5b
694568Sgibbs
704568Sgibbs#  The two reserved bytes at SCBARRAY+1[23] are expected to be set to
714568Sgibbs#  zero, and the reserved bit in SCBARRAY+0 is used as an internal flag
724568Sgibbs#  to indicate whether or not to reload scatter-gather parameters after
734568Sgibbs#  a disconnect.
744568Sgibbs#
754568SgibbsSCBARRAY+0	= 0xa0
764568SgibbsSCBARRAY+1	= 0xa1
774568SgibbsSCBARRAY+2	= 0xa2
784568SgibbsSCBARRAY+3	= 0xa3
794568SgibbsSCBARRAY+7	= 0xa7
804568SgibbsSCBARRAY+11	= 0xab
814568SgibbsSCBARRAY+14	= 0xae
824568SgibbsSCBARRAY+15	= 0xaf
834568SgibbsSCBARRAY+16	= 0xb0
844568SgibbsSCBARRAY+17	= 0xb1
854568SgibbsSCBARRAY+18	= 0xb2
864568SgibbsSCBARRAY+19	= 0xb3
874568SgibbsSCBARRAY+20	= 0xb4
884568SgibbsSCBARRAY+21	= 0xb5
894568SgibbsSCBARRAY+22	= 0xb6
904568SgibbsSCBARRAY+23	= 0xb7
914568SgibbsSCBARRAY+24	= 0xb8
924568SgibbsSCBARRAY+25	= 0xb9
934568Sgibbs
944568SgibbsSIGNAL_0	= 0x01				# unknown scsi bus phase
954568SgibbsSIGNAL_1	= 0x11				# message reject
964568SgibbsSIGNAL_2	= 0x21				# no IDENTIFY after reconnect
974568SgibbsSIGNAL_3	= 0x31				# no cmd match for reconnect
984568SgibbsSIGNAL_4	= 0x41				# SDTR -> SCSIRATE conversion
994568SgibbsSTATUS_ERROR    = 0x51
1005562SgibbsSIGNAL_WDTR	= 0x61
1014568Sgibbs
1024568Sgibbs#  The host adapter card (at least the BIOS) uses 20-2f for SCSI
1035326Sgibbs#  device information, 32-33 and 5a-5f as well. As it turns out, the
1045326Sgibbs#  BIOS trashes 20-2f, writing the synchronous negotiation results
1054568Sgibbs#  on top of the BIOS values, so we re-use those for our per-target
1064568Sgibbs#  scratchspace (actually a value that can be copied directly into
1075326Sgibbs#  SCSIRATE).  The kernel driver will enable synchronous negotiation
1085326Sgibbs#  for all targets that have a value other than 0 in the lower four
1095326Sgibbs#  bits of the target scratch space.  This should work irregardless of
1105326Sgibbs#  whether the bios has been installed. NEEDSDTR has one bit per target 
1115326Sgibbs#  indicating if an SDTR message is needed for that device - this will 
1125326Sgibbs#  be set initially (based on a search through the target scratch space), 
1135326Sgibbs#  as well as after a bus reset condition.
1144568Sgibbs#
1154568Sgibbs#  The high bit of DROPATN is set if ATN should be dropped before the ACK
1164568Sgibbs#  when outb is called.  REJBYTE contains the first byte of a MESSAGE IN
1174568Sgibbs#  message, so the driver can report an intelligible error if a message is
1184568Sgibbs#  rejected.
1194568Sgibbs#
1205562Sgibbs#  FLAGS's high bit is true if we are currently handling a reselect;
1214568Sgibbs#  its next-highest bit is true ONLY IF we've seen an IDENTIFY message
1224568Sgibbs#  from the reselecting target.  If we haven't had IDENTIFY, then we have
1234568Sgibbs#  no idea what the lun is, and we can't select the right SCB register
1244568Sgibbs#  bank, so force a kernel panic if the target attempts a data in/out or
1254568Sgibbs#  command phase instead of corrupting something.
1264568Sgibbs#
1274568Sgibbs#  Note that SG_NEXT occupies four bytes.
1284568Sgibbs#
1294568SgibbsSYNCNEG		= 0x20
1304568Sgibbs
1314568SgibbsDROPATN		= 0x30
1324568SgibbsREJBYTE		= 0x31
1335326SgibbsDISC_DSB_A	= 0x32
1345326SgibbsDISC_DSB_B	= 0x33
1354568Sgibbs
1365562SgibbsNEEDWDTR_A	= 0x34
1375562SgibbsNEEDWDTR_B	= 0x35
1385562Sgibbs
1394568SgibbsMSG_LEN		= 0x36
1404568SgibbsMSG_START+0	= 0x37
1414568SgibbsMSG_START+1	= 0x38
1424568SgibbsMSG_START+2	= 0x39
1434568SgibbsMSG_START+3	= 0x3a
1444568SgibbsMSG_START+4	= 0x3b
1454568SgibbsMSG_START+5	= 0x3c
1464568Sgibbs-MSG_START+0	= 0xc9				# 2's complement of MSG_START+0
1474568Sgibbs
1484568SgibbsARG_1		= 0x4c				# sdtr conversion args & return
1494568SgibbsRETURN_1	= 0x4c
1504568Sgibbs
1515326SgibbsSIGSTATE	= 0x4d				# value written to SCSISIGO
1525326SgibbsNEEDSDTR_A	= 0x4e				# send SDTR message, 1 bit/trgt
1535326SgibbsNEEDSDTR_B	= 0x4f
1544568Sgibbs
1554568SgibbsSG_SIZEOF	= 0x8 				# sizeof(struct scatterlist)
1564568SgibbsSG_NOLOAD	= 0x50				# load SG pointer/length?
1574568SgibbsSG_COUNT	= 0x51				# working value of SG count
1584568SgibbsSG_NEXT		= 0x52				# working value of SG pointer
1594568SgibbsSG_NEXT+0	= 0x52
1604568SgibbsSG_NEXT+1	= 0x53
1614568SgibbsSG_NEXT+2	= 0x54
1624568SgibbsSG_NEXT+3	= 0x55
1634568Sgibbs
1644568SgibbsSCBCOUNT	= 0x56				# the actual number of SCBs
1655326SgibbsFLAGS		= 0x57				# Device configuration flags
1665326SgibbsTWIN_BUS	= 0x01
1675326SgibbsWIDE_BUS	= 0x02
1685562SgibbsCHECK_DTR	= 0x08
1695562SgibbsSENSE		= 0x10
1705562SgibbsACTIVE_MSG	= 0x20
1715562SgibbsIDENTIFY_SEEN	= 0x40
1725562SgibbsRESELECTED	= 0x80
1734568Sgibbs
1745326SgibbsACTIVE_A	= 0x58
1755326SgibbsACTIVE_B	= 0x59
1765326Sgibbs
1774866Sgibbs#  Poll QINCNT for work - the lower bits contain
1784568Sgibbs#  the number of entries in the Queue In FIFO.
1794568Sgibbs#
1804568Sgibbsstart:
1815562Sgibbs	test	FLAGS,SENSE	jnz start_sense
1825326Sgibbs	test	FLAGS,TWIN_BUS	jz start2	# Are we a twin channel device?
1835326Sgibbs# For fairness, we check the other bus first, since we just finished a 
1845326Sgibbs# transaction on the current channel.
1855326Sgibbs	xor	SBLKCTL,0x08			# Toggle to the other bus
1864568Sgibbs	test	SCSISIGI,0x4	jnz reselect	# BSYI
1875326Sgibbs	xor	SBLKCTL,0x08			# Toggle to the original bus
1885326Sgibbsstart2:
1895326Sgibbs	test	SCSISIGI,0x4	jnz reselect	# BSYI
1904866Sgibbs	test	QINCNT,SCBMASK	jz start
1914568Sgibbs
1924568Sgibbs#  We have at least one queued SCB now.  Set the SCB pointer
1934568Sgibbs#  from the FIFO so we see the right bank of SCB registers,
1944568Sgibbs#  then set SCSI options and set the initiator and target
1954568Sgibbs#  SCSI IDs.
1964568Sgibbs#
1974568Sgibbs	mov	SCBPTR,QINFIFO
1984568Sgibbs
1994568Sgibbs# See if there is not already an active SCB for this target.  This code
2005326Sgibbs# locks out on a per target basis instead of target/lun.  Although this
2015326Sgibbs# is not ideal for devices that have multiple luns active at the same
2025326Sgibbs# time, it is faster than looping through all SCB's looking for active
2035326Sgibbs# commands.  It may be benificial to make findscb a more general procedure
2045326Sgibbs# to see if the added cost of the search is negligible.  This code also 
2055326Sgibbs# assumes that the kernel driver will clear the active flags on board 
2065326Sgibbs# initialization, board reset, and a target's SELTO.
2074568Sgibbs
2084568Sgibbs	and	FUNCTION1,0x70,SCBARRAY+1
2094568Sgibbs	mov	A,FUNCTION1
2105326Sgibbs	test	SCBARRAY+1,0x88	jz test_a	# Id < 8 && A channel
2115326Sgibbs
2125326Sgibbs	test	ACTIVE_B,A	jnz requeue
2135326Sgibbs	or	ACTIVE_B,A	# Mark the current target as busy
2145326Sgibbs	jmp	start_scb
2155326Sgibbs
2165562Sgibbsstart_sense:
2175562Sgibbs# Clear the SENSE flag first, then do a normal start_scb
2185562Sgibbs	and	FLAGS,0xef
2195562Sgibbs	jmp	start_scb
2205562Sgibbs
2214568Sgibbs# Place the currently active back on the queue for later processing
2225326Sgibbsrequeue:
2234568Sgibbs	mov	QINFIFO, SCBPTR
2244568Sgibbs	jmp	start
2254568Sgibbs
2265326Sgibbstest_a:
2275326Sgibbs	test	ACTIVE_A,A	jnz requeue
2285326Sgibbs	or	ACTIVE_A,A	# Mark the current target as busy
2295326Sgibbs
2305326Sgibbsstart_scb:
2315562Sgibbs	and	SINDEX,0x08,SCBARRAY+1
2325562Sgibbs	and	A,WIDE_BUS,FLAGS	# Wide bus?	
2335562Sgibbs	or	SINDEX,A
2345562Sgibbs	mov	SBLKCTL,SINDEX	# select channel, bus width
2354568Sgibbs	mov	SCBARRAY+1	call initialize
2364568Sgibbs	clr	SG_NOLOAD
2375562Sgibbs	and	FLAGS,0x3f	# !RESELECTING
2384568Sgibbs
2394568Sgibbs#  As soon as we get a successful selection, the target should go
2404568Sgibbs#  into the message out phase since we have ATN asserted.  Prepare
2414568Sgibbs#  the message to send, locking out the device driver.  If the device
2424568Sgibbs#  driver hasn't beaten us with an ABORT or RESET message, then tack
2434866Sgibbs#  on an SDTR negotiation if required.
2444568Sgibbs#
2454568Sgibbs#  Messages are stored in scratch RAM starting with a flag byte (high bit
2464568Sgibbs#  set means active message), one length byte, and then the message itself.
2474568Sgibbs#
2484568Sgibbs	mov	SCBARRAY+1	call disconnect	# disconnect ok?
2494568Sgibbs
2504568Sgibbs	and	SINDEX,0x7,SCBARRAY+1		# lun
2514568Sgibbs	or	SINDEX,A			# return value from disconnect
2524568Sgibbs	or	SINDEX,0x80	call mk_mesg	# IDENTIFY message
2534568Sgibbs
2544568Sgibbs	mov	A,SINDEX
2554568Sgibbs	cmp	MSG_START+0,A	jne !message	# did driver beat us?
2565562Sgibbs	test	FLAGS,CHECK_DTR jz !message	# Don't even bother with DTR?
2575562Sgibbs	mvi	MSG_START+1	call mk_dtr	# build DTR message if needed
2584568Sgibbs
2594568Sgibbs!message:
2604568Sgibbs
2614568Sgibbs#  Enable selection phase as an initiator, and do automatic ATN
2624568Sgibbs#  after the selection.
2634568Sgibbs#
2644568Sgibbs	mvi	SCSISEQ,0x48			# ENSELO|ENAUTOATNO
2654568Sgibbs
2664568Sgibbs#  Wait for successful arbitration.  The AIC-7770 documentation says
2674568Sgibbs#  that SELINGO indicates successful arbitration, and that it should
2684568Sgibbs#  be used to look for SELDO.  However, if the sequencer is paused at
2694568Sgibbs#  just the right time - a parallel fsck(8) on two drives did it for
2704568Sgibbs#  me - then SELINGO can flip back to false before we've seen it.  This
2714568Sgibbs#  makes the sequencer sit in the arbitration loop forever.  This is
2724568Sgibbs#  Not Good.
2734568Sgibbs#
2744568Sgibbs#  Therefore, I've added a check in the arbitration loop for SELDO
2754568Sgibbs#  too.  This could arguably be made a critical section by disabling
2764568Sgibbs#  pauses, but I don't want to make a potentially infinite loop a CS.
2774568Sgibbs#  I suppose you could fold it into the select loop, too, but since
2784568Sgibbs#  I've been hunting this bug for four days it's kinda like a trophy.
2794568Sgibbs#
2804568Sgibbsarbitrate:
2814568Sgibbs	test	SSTAT0,0x40	jnz *select	# SELDO
2824568Sgibbs	test	SSTAT0,0x10	jz arbitrate	# SELINGO
2834568Sgibbs
2844568Sgibbs#  Wait for a successful selection.  If the hardware selection
2854568Sgibbs#  timer goes off, then the driver gets the interrupt, so we don't
2864568Sgibbs#  need to worry about it.
2874568Sgibbs#
2884568Sgibbsselect:
2894568Sgibbs	test	SSTAT0,0x40	jz select	# SELDO
2904568Sgibbs	jmp	*select
2914568Sgibbs
2924568Sgibbs#  Reselection is being initiated by a target - we've seen the BSY
2934568Sgibbs#  line driven active, and we didn't do it!  Enable the reselection
2944568Sgibbs#  hardware, and wait for it to finish.  Make a note that we've been
2954568Sgibbs#  reselected, but haven't seen an IDENTIFY message from the target
2964568Sgibbs#  yet.
2974568Sgibbs#
2984568Sgibbsreselect:
2994568Sgibbs	mvi	SCSISEQ,0x10			# ENRSELI
3004568Sgibbs
3014568Sgibbsreselect1:
3024568Sgibbs	test	SSTAT0,0x20	jz reselect1	# SELDI
3034568Sgibbs	mov	SELID		call initialize
3044568Sgibbs
3055562Sgibbs	and	FLAGS,0x3f			# reselected, no IDENTIFY	
3065562Sgibbs	or	FLAGS,RESELECTED		
3074568Sgibbs
3084568Sgibbs#  After the [re]selection, make sure that the [re]selection enable
3094568Sgibbs#  bit is off.  This chip is flaky enough without extra things
3104568Sgibbs#  turned on.  Also clear the BUSFREE bit in SSTAT1 since we'll be
3114568Sgibbs#  using it shortly.
3124568Sgibbs#
3134568Sgibbs*select:
3144568Sgibbs	clr	SCSISEQ
3154568Sgibbs	mvi	CLRSINT1,0x8			# CLRBUSFREE
3164568Sgibbs
3174568Sgibbs#  Main loop for information transfer phases.  If BSY is false, then
3184568Sgibbs#  we have a bus free condition, expected or not.  Otherwise, wait
3194568Sgibbs#  for the target to assert REQ before checking MSG, C/D and I/O
3204568Sgibbs#  for the bus phase.
3214568Sgibbs#
3224568Sgibbs#  We can't simply look at the values of SCSISIGI here (if we want
3234568Sgibbs#  to do synchronous data transfer), because the target won't assert
3244568Sgibbs#  REQ if it's already sent us some data that we haven't acknowledged
3254568Sgibbs#  yet.
3264568Sgibbs#
3274568SgibbsITloop:
3284568Sgibbs	test	SSTAT1,0x8	jnz p_busfree	# BUSFREE
3294568Sgibbs	test	SSTAT1,0x1	jz ITloop	# REQINIT
3304568Sgibbs
3314568Sgibbs	and	A,0xe0,SCSISIGI			# CDI|IOI|MSGI
3324568Sgibbs
3334568Sgibbs	cmp	ALLZEROS,A	je p_dataout
3344568Sgibbs	cmp	A,0x40		je p_datain
3354568Sgibbs	cmp	A,0x80		je p_command
3364568Sgibbs	cmp	A,0xc0		je p_status
3374568Sgibbs	cmp	A,0xa0		je p_mesgout
3384568Sgibbs	cmp	A,0xe0		je p_mesgin
3394568Sgibbs
3404568Sgibbs	mvi	INTSTAT,SIGNAL_0		# unknown - signal driver
3414568Sgibbs
3424568Sgibbsp_dataout:
3434568Sgibbs	mvi	0		call scsisig	# !CDO|!IOO|!MSGO
3444568Sgibbs	call	assert
3454568Sgibbs	call	sg_load
3464568Sgibbs
3474568Sgibbs	mvi	A,3
3484568Sgibbs	mvi	DINDEX,HCNT
3494568Sgibbs	mvi	SCBARRAY+23	call bcopy
3504568Sgibbs
3514568Sgibbs	mvi	A,3
3524568Sgibbs	mvi	DINDEX,STCNT
3534568Sgibbs	mvi	SCBARRAY+23	call bcopy
3544568Sgibbs
3554568Sgibbs	mvi	A,4
3564568Sgibbs	mvi	DINDEX,HADDR
3574568Sgibbs	mvi	SCBARRAY+19	call bcopy
3584568Sgibbs
3594568Sgibbs	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
3604568Sgibbs						#   DIRECTION|FIFORESET
3614568Sgibbs
3624568Sgibbs#  After a DMA finishes, save the final transfer pointer and count
3634568Sgibbs#  back into the SCB, in case a device disconnects in the middle of
3644568Sgibbs#  a transfer.  Use SHADDR and STCNT instead of HADDR and HCNT, since
3654568Sgibbs#  it's a reflection of how many bytes were transferred on the SCSI
3664568Sgibbs#  (as opposed to the host) bus.
3674568Sgibbs#
3684568Sgibbs	mvi	A,3
3694568Sgibbs	mvi	DINDEX,SCBARRAY+23
3704568Sgibbs	mvi	STCNT		call bcopy
3714568Sgibbs
3724568Sgibbs	mvi	A,4
3734568Sgibbs	mvi	DINDEX,SCBARRAY+19
3744568Sgibbs	mvi	SHADDR		call bcopy
3754568Sgibbs
3764568Sgibbs	call	sg_advance
3774568Sgibbs	mov	SCBARRAY+18,SG_COUNT		# residual S/G count
3784568Sgibbs
3794568Sgibbs	jmp	ITloop
3804568Sgibbs
3814568Sgibbsp_datain:
3824568Sgibbs	mvi	0x40		call scsisig	# !CDO|IOO|!MSGO
3834568Sgibbs	call	assert
3844568Sgibbs	call	sg_load
3854568Sgibbs
3864568Sgibbs	mvi	A,3
3874568Sgibbs	mvi	DINDEX,HCNT
3884568Sgibbs	mvi	SCBARRAY+23	call bcopy
3894568Sgibbs
3904568Sgibbs	mvi	A,3
3914568Sgibbs	mvi	DINDEX,STCNT
3924568Sgibbs	mvi	SCBARRAY+23	call bcopy
3934568Sgibbs
3944568Sgibbs	mvi	A,4
3954568Sgibbs	mvi	DINDEX,HADDR
3964568Sgibbs	mvi	SCBARRAY+19	call bcopy
3974568Sgibbs
3984568Sgibbs	mvi	0x39		call dma	# SCSIEN|SDMAEN|HDMAEN|
3994568Sgibbs						#   !DIRECTION|FIFORESET
4004568Sgibbs	mvi	A,3
4014568Sgibbs	mvi	DINDEX,SCBARRAY+23
4024568Sgibbs	mvi	STCNT		call bcopy
4034568Sgibbs
4044568Sgibbs	mvi	A,4
4054568Sgibbs	mvi	DINDEX,SCBARRAY+19
4064568Sgibbs	mvi	SHADDR		call bcopy
4074568Sgibbs
4084568Sgibbs	call	sg_advance
4094568Sgibbs	mov	SCBARRAY+18,SG_COUNT		# residual S/G count
4104568Sgibbs
4114568Sgibbs	jmp	ITloop
4124568Sgibbs
4134568Sgibbs#  Command phase.  Set up the DMA registers and let 'er rip - the
4144568Sgibbs#  two bytes after the SCB SCSI_cmd_length are zeroed by the driver,
4154568Sgibbs#  so we can copy those three bytes directly into HCNT.
4164568Sgibbs#
4174568Sgibbsp_command:
4184568Sgibbs	mvi	0x80		call scsisig	# CDO|!IOO|!MSGO
4194568Sgibbs	call	assert
4204568Sgibbs
4214568Sgibbs	mvi	A,3
4224568Sgibbs	mvi	DINDEX,HCNT
4234568Sgibbs	mvi	SCBARRAY+11	call bcopy
4244568Sgibbs
4254568Sgibbs	mvi	A,3
4264568Sgibbs	mvi	DINDEX,STCNT
4274568Sgibbs	mvi	SCBARRAY+11	call bcopy
4284568Sgibbs
4294568Sgibbs	mvi	A,4
4304568Sgibbs	mvi	DINDEX,HADDR
4314568Sgibbs	mvi	SCBARRAY+7	call bcopy
4324568Sgibbs
4334568Sgibbs	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
4344568Sgibbs						#   DIRECTION|FIFORESET
4354568Sgibbs	jmp	ITloop
4364568Sgibbs
4374568Sgibbs#  Status phase.  Wait for the data byte to appear, then read it
4384568Sgibbs#  and store it into the SCB.
4394568Sgibbs#
4404568Sgibbsp_status:
4414568Sgibbs	mvi	0xc0		call scsisig	# CDO|IOO|!MSGO
4424568Sgibbs
4434568Sgibbs	mvi	SCBARRAY+14	call inb
4444568Sgibbs	jmp	ITloop
4454568Sgibbs
4464568Sgibbs#  Message out phase.  If there is no active message, but the target
4474568Sgibbs#  took us into this phase anyway, build a no-op message and send it.
4484568Sgibbs#
4494568Sgibbsp_mesgout:
4504568Sgibbs	mvi	0xa0		call scsisig	# CDO|!IOO|MSGO
4514568Sgibbs	mvi	0x8		call mk_mesg	# build NOP message
4524568Sgibbs
4534568Sgibbs#  Set up automatic PIO transfer from MSG_START.  Bit 3 in
4544568Sgibbs#  SXFRCTL0 (SPIOEN) is already on.
4554568Sgibbs#
4564568Sgibbs	mvi	SINDEX,MSG_START+0
4574568Sgibbs	mov	DINDEX,MSG_LEN
4584568Sgibbs	clr	A
4594568Sgibbs
4604568Sgibbs#  When target asks for a byte, drop ATN if it's the last one in
4614568Sgibbs#  the message.  Otherwise, keep going until the message is exhausted.
4624568Sgibbs#  (We can't use outb for this since it wants the input in SINDEX.)
4634568Sgibbs#
4644568Sgibbs#  Keep an eye out for a phase change, in case the target issues
4654568Sgibbs#  a MESSAGE REJECT.
4664568Sgibbs#
4674568Sgibbsp_mesgout2:
4684568Sgibbs	test	SSTAT0,0x2	jz p_mesgout2	# SPIORDY
4694568Sgibbs	test	SSTAT1,0x10	jnz p_mesgout6	# PHASEMIS
4704568Sgibbs
4714568Sgibbs	cmp	DINDEX,1	jne p_mesgout3	# last byte?
4724568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO - drop ATN
4734568Sgibbs
4744568Sgibbs#  Write a byte to the SCSI bus.  The AIC-7770 refuses to automatically
4754568Sgibbs#  send ACKs in automatic PIO or DMA mode unless you make sure that the
4764568Sgibbs#  "expected" bus phase in SCSISIGO matches the actual bus phase.  This
4774568Sgibbs#  behaviour is completely undocumented and caused me several days of
4784568Sgibbs#  grief.
4794568Sgibbs#
4804568Sgibbs#  After plugging in different drives to test with and using a longer
4814568Sgibbs#  SCSI cable, I found that I/O in Automatic PIO mode ceased to function,
4824568Sgibbs#  especially when transferring >1 byte.  It seems to be much more stable
4834568Sgibbs#  if STCNT is set to one before the transfer, and SDONE (in SSTAT0) is
4844568Sgibbs#  polled for transfer completion - for both output _and_ input.  The
4854568Sgibbs#  only theory I have is that SPIORDY doesn't drop right away when SCSIDATL
4864568Sgibbs#  is accessed (like the documentation says it does), and that on a longer
4874568Sgibbs#  cable run, the sequencer code was fast enough to loop back and see
4884568Sgibbs#  an SPIORDY that hadn't dropped yet.
4894568Sgibbs#
4904568Sgibbsp_mesgout3:
4914568Sgibbs	call	one_stcnt
4924568Sgibbs	mov	SCSIDATL,SINDIR
4934568Sgibbs
4944568Sgibbsp_mesgout4:
4954568Sgibbs	test	SSTAT0,0x4	jz p_mesgout4	# SDONE
4964568Sgibbs	dec	DINDEX
4974568Sgibbs	inc	A
4984568Sgibbs	cmp	MSG_LEN,A	jne p_mesgout2
4994568Sgibbs
5004568Sgibbs#  If the next bus phase after ATN drops is a message out, it means
5014568Sgibbs#  that the target is requesting that the last message(s) be resent.
5024568Sgibbs#
5034568Sgibbsp_mesgout5:
5044568Sgibbs	test	SSTAT1,0x8	jnz p_mesgout6	# BUSFREE
5054568Sgibbs	test	SSTAT1,0x1	jz p_mesgout5	# REQINIT
5064568Sgibbs
5074568Sgibbs	and	A,0xe0,SCSISIGI			# CDI|IOI|MSGI
5084568Sgibbs	cmp	A,0xa0		jne p_mesgout6
5094568Sgibbs	mvi	0x10		call scsisig	# ATNO - re-assert ATN
5104568Sgibbs
5114568Sgibbs	jmp	ITloop
5124568Sgibbs
5134568Sgibbsp_mesgout6:
5144568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO - in case of PHASEMIS
5155562Sgibbs	and	FLAGS,0xdf			# no active msg
5164568Sgibbs	jmp	ITloop
5174568Sgibbs
5184568Sgibbs#  Message in phase.  Bytes are read using Automatic PIO mode, but not
5194568Sgibbs#  using inb.  This alleviates a race condition, namely that if ATN had
5204568Sgibbs#  to be asserted under Automatic PIO mode, it had to beat the SCSI
5214568Sgibbs#  circuitry sending an ACK to the target.  This showed up under heavy
5224568Sgibbs#  loads and really confused things, since ABORT commands wouldn't be
5234568Sgibbs#  seen by the drive after an IDENTIFY message in until it had changed
5244568Sgibbs#  to a data I/O phase.
5254568Sgibbs#
5264568Sgibbsp_mesgin:
5274568Sgibbs	mvi	0xe0		call scsisig	# CDO|IOO|MSGO
5284568Sgibbs	mvi	A		call inb_first	# read the 1st message byte
5294568Sgibbs	mvi	REJBYTE,A			# save it for the driver
5304568Sgibbs
5314568Sgibbs	cmp	ALLZEROS,A	jne p_mesgin1
5324568Sgibbs
5334568Sgibbs#  We got a "command complete" message, so put the SCB pointer
5344568Sgibbs#  into the Queue Out, and trigger a completion interrupt.
5354568Sgibbs#  Check status for non zero return and interrupt driver if needed
5365326Sgibbs#  This allows the driver to interpret errors only when they occur
5375326Sgibbs#  instead of always uploading the scb.  If the status is SCSI_CHECK,
5385326Sgibbs#  the driver will download a new scb requesting sense, to replace 
5395326Sgibbs#  the old one and the sequencer code will imediately jump to start 
5405326Sgibbs#  working on it.  If the kernel driver does not wish to request sense, 
5415326Sgibbs#  the sequencer program counter is incremented by 1, preventing another run 
5425326Sgibbs#  on the current SCB and the command is allowed to complete.  We don't 
5435326Sgibbs#  bother to post to the QOUTFIFO in the error case since it would require 
5445326Sgibbs#  extra work in the kernel driver to ensure that the entry was removed 
5455326Sgibbs#  before the command complete code tried processing it.
5464568Sgibbs
5474568Sgibbs	test	SCBARRAY+14,0xff	jz status_ok  # 0 Status?
5484568Sgibbs	mvi	INTSTAT,STATUS_ERROR		# let driver know
5495562Sgibbs	test	FLAGS,SENSE	jz status_ok
5505562Sgibbs	jmp	p_mesgin_done
5515326Sgibbs
5524568Sgibbsstatus_ok:
5535326Sgibbs
5545326Sgibbs#  First, mark this target as free.
5555326Sgibbs	and	FUNCTION1,0x70,SCBARRAY+1
5565326Sgibbs	mov	A,FUNCTION1
5575326Sgibbs	test	SCBARRAY+1,0x88 jz clear_a
5585326Sgibbs	xor	ACTIVE_B,A
5595326Sgibbs	jmp	complete
5605326Sgibbs
5615326Sgibbsclear_a:
5625326Sgibbs	xor	ACTIVE_A,A
5635326Sgibbs
5645326Sgibbscomplete:
5654568Sgibbs	mov	QOUTFIFO,SCBPTR
5665326Sgibbs	mvi	INTSTAT,0x02			# CMDCMPLT
5674568Sgibbs	jmp	p_mesgin_done
5684568Sgibbs
5695562Sgibbs#  Is it an extended message?  We only support the synchronous and wide data
5705562Sgibbs#  transfer request messages, which will probably be in response to
5715562Sgibbs#  WDTR or SDTR message outs from us.  If it's not SDTR or WDTR, reject it -
5724568Sgibbs#  apparently this can be done after any message in byte, according
5734568Sgibbs#  to the SCSI-2 spec.
5744568Sgibbs#
5754568Sgibbsp_mesgin1:
5764568Sgibbs	cmp	A,1		jne p_mesgin2	# extended message code?
5774568Sgibbs	
5785562Sgibbs	mvi	ARG_1		call inb_next	# extended message length
5795562Sgibbs	mvi	A		call inb_next	# extended message code
5804568Sgibbs
5815562Sgibbs	cmp	A,1		je p_mesginSDTR	# Syncronous negotiation message
5825562Sgibbs	cmp	A,3		je p_mesginWDTR # Wide negotiation message
5835562Sgibbs	jmp	p_mesginN
5845562Sgibbs
5855562Sgibbsp_mesginWDTR:
5865562Sgibbs	cmp	ARG_1,2		jne p_mesginN	# extended mesg length = 2
5875562Sgibbs	mvi	A		call inb_next	# Width of bus
5885562Sgibbs	mvi	INTSTAT,SIGNAL_WDTR		# let driver know
5895562Sgibbs	test	RETURN_1,0x80	jz p_mesgin_done# Do we need to send WDTR?
5905562Sgibbs
5915562Sgibbs# We didn't initiate the wide negotiation, so we must respond to the request
5925562Sgibbs	and	RETURN_1,0x7f			# Clear the SEND_WDTR Flag
5935562Sgibbs	or	FLAGS,ACTIVE_MSG
5945562Sgibbs	mvi	MSG_START+0	call mk_wdtr	# build WDTR message	
5955562Sgibbs	or	SINDEX,0x10,SIGSTATE		# turn on ATNO
5965562Sgibbs	call	scsisig
5975562Sgibbs	jmp	p_mesgin_done
5985562Sgibbs
5995562Sgibbsp_mesginSDTR:
6005562Sgibbs	cmp	ARG_1,3		jne p_mesginN	# extended mesg length = 3
6014568Sgibbs	mvi	ARG_1		call inb_next	# xfer period
6025326Sgibbs	mvi	A		call inb_next	# REQ/ACK offset
6034568Sgibbs	mvi	INTSTAT,SIGNAL_4		# call driver to convert
6044568Sgibbs
6055562Sgibbs	test	RETURN_1,0x80	jz p_mesgin_done# Do we need to mk_sdtr?
6065326Sgibbs
6075562Sgibbs	or	FLAGS,ACTIVE_MSG
6085562Sgibbs	mvi     MSG_START+0     call mk_sdtr
6095562Sgibbs	or	SINDEX,0x10,SIGSTATE		# turn on ATNO
6105562Sgibbs	call	scsisig
6114568Sgibbs	jmp	p_mesgin_done
6124568Sgibbs
6134568Sgibbs#  Is it a disconnect message?  Set a flag in the SCB to remind us
6144568Sgibbs#  and await the bus going free.
6154568Sgibbs#
6164568Sgibbsp_mesgin2:
6174568Sgibbs	cmp	A,4		jne p_mesgin3	# disconnect code?
6184568Sgibbs
6194568Sgibbs	or	SCBARRAY+0,0x4			# set "disconnected" bit
6204568Sgibbs	jmp	p_mesgin_done
6214568Sgibbs
6224568Sgibbs#  Save data pointers message?  Copy working values into the SCB,
6234568Sgibbs#  usually in preparation for a disconnect.
6244568Sgibbs#
6254568Sgibbsp_mesgin3:
6264568Sgibbs	cmp	A,2		jne p_mesgin4	# save data pointers code?
6274568Sgibbs
6284568Sgibbs	call	sg_ram2scb
6294568Sgibbs	jmp	p_mesgin_done
6304568Sgibbs
6314568Sgibbs#  Restore pointers message?  Data pointers are recopied from the
6324568Sgibbs#  SCB anyway at the start of any DMA operation, so the only thing
6334568Sgibbs#  to copy is the scatter-gather values.
6344568Sgibbs#
6354568Sgibbsp_mesgin4:
6364568Sgibbs	cmp	A,3		jne p_mesgin5	# restore pointers code?
6374568Sgibbs
6384568Sgibbs	call	sg_scb2ram
6394568Sgibbs	jmp	p_mesgin_done
6404568Sgibbs
6414568Sgibbs#  Identify message?  For a reconnecting target, this tells us the lun
6424568Sgibbs#  that the reconnection is for - find the correct SCB and switch to it,
6434568Sgibbs#  clearing the "disconnected" bit so we don't "find" it by accident later.
6444568Sgibbs#
6454568Sgibbsp_mesgin5:
6464568Sgibbs	test	A,0x80		jz p_mesgin6	# identify message?
6474568Sgibbs
6484568Sgibbs	test	A,0x78		jnz p_mesginN	# !DiscPriv|!LUNTAR|!Reserved
6494568Sgibbs
6504568Sgibbs	mov	A		call findSCB	# switch to correct SCB
6514568Sgibbs
6524568Sgibbs#  If a active message is present after calling findSCB, then either it
6534568Sgibbs#  or the driver is trying to abort the command.  Either way, something
6544568Sgibbs#  untoward has happened and we should just leave it alone.
6554568Sgibbs#
6565562Sgibbs	test	FLAGS,ACTIVE_MSG	jnz p_mesgin_done
6574568Sgibbs
6585326Sgibbs	and	SCBARRAY+0,0xfb			# clear disconnect bit in SCB
6595562Sgibbs	or	FLAGS,0xc0			# make note of IDENTIFY
6604568Sgibbs
6614568Sgibbs	call	sg_scb2ram			# implied restore pointers
6624568Sgibbs						#   required on reselect
6634568Sgibbs	jmp	p_mesgin_done
6644568Sgibbs
6655562Sgibbs#  Message reject?  If we have an outstanding WDTR or SDTR negotiation, assume
6665562Sgibbs#  that it's a response from the target selecting 8bit or asynchronous transfer,
6674568Sgibbs#  otherwise just ignore it since we have no clue what it pertains to.
6684568Sgibbs#
6694568Sgibbs#  XXX - I don't have a device that responds this way.  Does this code
6704568Sgibbs#	 actually work?
6714568Sgibbs#
6724568Sgibbsp_mesgin6:
6734568Sgibbs	cmp	A,7		jne p_mesgin7	# message reject code?
6744568Sgibbs
6754568Sgibbs	and	FUNCTION1,0x70,SCSIID		# outstanding SDTR message?
6764568Sgibbs	mov	A,FUNCTION1
6775326Sgibbs	
6785326Sgibbs	test	SBLKCTL,0x08	jnz p_mesgin6_b	
6795326Sgibbs	test	SCSIID,0x80	jnz p_mesgin6_b	
6805562Sgibbs	test	NEEDWDTR_A,A	jnz clear_wdtr_a
6815562Sgibbs	test	NEEDSDTR_A,A	jnz clear_sdtr_a
6825562Sgibbs	jmp	p_mesgin_done  # no - ignore rejection
6835562Sgibbs
6845562Sgibbsclear_sdtr_a:
6855562Sgibbs	call	ndx_dtr				# note use of asynch xfer
6865326Sgibbs	mov	DINDEX,SINDEX
6875562Sgibbs	and	DINDIR,0x80,SINDIR
6884568Sgibbs
6895326Sgibbs	not	A
6905326Sgibbs	and	NEEDSDTR_A,A
6915562Sgibbs	jmp	clear_sdtr_done
6925326Sgibbs
6935562Sgibbsclear_wdtr_a:
6945562Sgibbs	call	ndx_dtr				# note use of 8bit xfer
6955562Sgibbs	mov	DINDEX,SINDEX
6965562Sgibbs	and	DINDIR,0x7f,SINDIR
6975562Sgibbs
6985562Sgibbs	not	A
6995562Sgibbs	and	NEEDWDTR_A,A
7005562Sgibbs	jmp	clear_wdtr_done
7015562Sgibbs
7025326Sgibbsp_mesgin6_b:
7035562Sgibbs	test	NEEDWDTR_B,A	jnz clear_wdtr_b
7045562Sgibbs	test	NEEDSDTR_B,A	jnz clear_sdtr_b
7055562Sgibbs	jmp	p_mesgin_done	# no - ignore rejection	
7065562Sgibbs
7075562Sgibbsclear_wdtr_b:
7085562Sgibbs	call	ndx_dtr				# note use of 8bit xfer
7094568Sgibbs	mov	DINDEX,SINDEX
7105562Sgibbs	and	DINDIR,0x7f,SINDIR
7114568Sgibbs
7125326Sgibbs	not	A
7135562Sgibbs	and	NEEDWDTR_B,A
7145562Sgibbs
7155562Sgibbsclear_wdtr_done:
7165562Sgibbs	and	SCSIRATE,0x7f			# select 8bit xfer
7175562Sgibbs	jmp	p_mesgin_done
7185562Sgibbs
7195562Sgibbsclear_sdtr_b:
7205562Sgibbs	call	ndx_dtr				# note use of asynch xfer
7215562Sgibbs	mov	DINDEX,SINDEX
7225562Sgibbs	and	DINDIR,0x80,SINDIR
7235562Sgibbs
7245562Sgibbs	not	A
7255326Sgibbs	and	NEEDSDTR_B,A
7265326Sgibbs	
7275562Sgibbsclear_sdtr_done:
7285562Sgibbs	and	SCSIRATE,0xf0			# select asynch xfer
7294568Sgibbs	jmp	p_mesgin_done
7304568Sgibbs
7314568Sgibbs#  [ ADD MORE MESSAGE HANDLING HERE ]
7324568Sgibbs#
7334568Sgibbsp_mesgin7:
7344568Sgibbs
7354568Sgibbs#  We have no idea what this message in is, and there's no way
7364568Sgibbs#  to pass it up to the kernel, so we issue a message reject and
7374568Sgibbs#  hope for the best.  Since we're now using manual PIO mode to
7384568Sgibbs#  read in the message, there should no longer be a race condition
7394568Sgibbs#  present when we assert ATN.  In any case, rejection should be a
7404568Sgibbs#  rare occurrence - signal the driver when it happens.
7414568Sgibbs#
7424568Sgibbsp_mesginN:
7434568Sgibbs	or	SINDEX,0x10,SIGSTATE		# turn on ATNO
7444568Sgibbs	call	scsisig
7454568Sgibbs	mvi	INTSTAT,SIGNAL_1		# let driver know
7464568Sgibbs
7474568Sgibbs	mvi	0x7		call mk_mesg	# MESSAGE REJECT message
7484568Sgibbs
7494568Sgibbsp_mesgin_done:
7504568Sgibbs	call	inb_last			# ack & turn auto PIO back on
7514568Sgibbs	jmp	ITloop
7524568Sgibbs
7534568Sgibbs#  Bus free phase.  It might be useful to interrupt the device
7544568Sgibbs#  driver if we aren't expecting this.  For now, make sure that
7554568Sgibbs#  ATN isn't being asserted and look for a new command.
7564568Sgibbs#
7574568Sgibbsp_busfree:
7584568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO
7594568Sgibbs	clr	SIGSTATE
7604568Sgibbs	jmp	start
7614568Sgibbs
7624568Sgibbs#  Bcopy: number of bytes to transfer should be in A, DINDEX should
7634568Sgibbs#  contain the destination address, and SINDEX should contain the
7644568Sgibbs#  source address.  All input parameters are trashed on return.
7654568Sgibbs#
7664568Sgibbsbcopy:
7674568Sgibbs	mov	DINDIR,SINDIR
7684568Sgibbs	dec	A
7694568Sgibbs	cmp	ALLZEROS,A	jne bcopy
7704568Sgibbs	ret
7714568Sgibbs
7724568Sgibbs#  Locking the driver out, build a one-byte message passed in SINDEX
7734568Sgibbs#  if there is no active message already.  SINDEX is returned intact.
7744568Sgibbs#
7754568Sgibbsmk_mesg:
7764866Sgibbs	mvi	SEQCTL,0x50			# PAUSEDIS|FASTMODE
7775562Sgibbs	test	FLAGS,ACTIVE_MSG jnz mk_mesg1	# active message?
7784568Sgibbs
7795562Sgibbs	or	FLAGS,ACTIVE_MSG		# if not, there is now
7804568Sgibbs	mvi	MSG_LEN,1			# length = 1
7814568Sgibbs	mov	MSG_START+0,SINDEX		# 1-byte message
7824568Sgibbs
7834568Sgibbsmk_mesg1:
7844866Sgibbs	mvi	SEQCTL,0x10			# !PAUSEDIS|FASTMODE
7854568Sgibbs	ret
7864568Sgibbs
7874568Sgibbs#  Input byte in Automatic PIO mode.  The address to store the byte
7884568Sgibbs#  in should be in SINDEX.  DINDEX will be used by this routine.
7894568Sgibbs#
7904568Sgibbsinb:
7914568Sgibbs	test	SSTAT0,0x2	jz inb		# SPIORDY
7924568Sgibbs	mov	DINDEX,SINDEX
7934568Sgibbs	call	one_stcnt			# xfer one byte
7944568Sgibbs	mov	DINDIR,SCSIDATL
7954568Sgibbsinb1:
7964568Sgibbs	test	SSTAT0,0x4	jz inb1		# SDONE - wait to "finish"
7974568Sgibbs	ret
7984568Sgibbs
7994568Sgibbs#  Carefully read data in Automatic PIO mode.  I first tried this using
8004568Sgibbs#  Manual PIO mode, but it gave me continual underrun errors, probably
8014568Sgibbs#  indicating that I did something wrong, but I feel more secure leaving
8024568Sgibbs#  Automatic PIO on all the time.
8034568Sgibbs#
8044568Sgibbs#  According to Adaptec's documentation, an ACK is not sent on input from
8054568Sgibbs#  the target until SCSIDATL is read from.  So we wait until SCSIDATL is
8064568Sgibbs#  latched (the usual way), then read the data byte directly off the bus
8074568Sgibbs#  using SCSIBUSL.  When we have pulled the ATN line, or we just want to
8084568Sgibbs#  acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
8094568Sgibbs#  spec guarantees that the target will hold the data byte on the bus until
8104568Sgibbs#  we send our ACK.
8114568Sgibbs#
8124568Sgibbs#  The assumption here is that these are called in a particular sequence,
8134568Sgibbs#  and that REQ is already set when inb_first is called.  inb_{first,next}
8144568Sgibbs#  use the same calling convention as inb.
8154568Sgibbs#
8164568Sgibbsinb_first:
8174568Sgibbs	mov	DINDEX,SINDEX
8184568Sgibbs	mov	DINDIR,SCSIBUSL	ret		# read byte directly from bus
8194568Sgibbs
8204568Sgibbsinb_next:
8214568Sgibbs	mov	DINDEX,SINDEX			# save SINDEX
8224568Sgibbs
8234568Sgibbs	call	one_stcnt			# xfer one byte
8244568Sgibbs	mov	NONE,SCSIDATL			# dummy read from latch to ACK
8254568Sgibbsinb_next1:
8264568Sgibbs	test	SSTAT0,0x4	jz inb_next1	# SDONE
8274568Sgibbsinb_next2:
8284568Sgibbs	test	SSTAT0,0x2	jz inb_next2	# SPIORDY - wait for next byte
8294568Sgibbs	mov	DINDIR,SCSIBUSL	ret		# read byte directly from bus
8304568Sgibbs
8314568Sgibbsinb_last:
8324568Sgibbs	call	one_stcnt			# ACK with dummy read
8334568Sgibbs	mov	NONE,SCSIDATL
8344568Sgibbsinb_last1:
8354568Sgibbs	test	SSTAT0,0x4	jz inb_last1	# wait for completion
8364568Sgibbs	ret
8374568Sgibbs
8384568Sgibbs#  Output byte in Automatic PIO mode.  The byte to output should be
8394568Sgibbs#  in SINDEX.  If DROPATN's high bit is set, then ATN will be dropped
8404568Sgibbs#  before the byte is output.
8414568Sgibbs#
8424568Sgibbsoutb:
8434568Sgibbs	test	SSTAT0,0x2	jz outb		# SPIORDY
8444568Sgibbs	call	one_stcnt			# xfer one byte
8454568Sgibbs
8464568Sgibbs	test	DROPATN,0x80	jz outb1
8474568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO
8484568Sgibbs	clr	DROPATN
8494568Sgibbsoutb1:
8504568Sgibbs	mov	SCSIDATL,SINDEX
8514568Sgibbsoutb2:
8524568Sgibbs	test	SSTAT0,0x4	jz outb2	# SDONE
8534568Sgibbs	ret
8544568Sgibbs
8554568Sgibbs#  Write the value "1" into the STCNT registers, for Automatic PIO
8564568Sgibbs#  transfers.
8574568Sgibbs#
8584568Sgibbsone_stcnt:
8594568Sgibbs	clr	STCNT+2
8604568Sgibbs	clr	STCNT+1
8614568Sgibbs	mvi	STCNT+0,1	ret
8624568Sgibbs
8634568Sgibbs#  DMA data transfer.  HADDR and HCNT must be loaded first, and
8644568Sgibbs#  SINDEX should contain the value to load DFCNTRL with - 0x3d for
8654568Sgibbs#  host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
8664568Sgibbs#  during initialization.
8674568Sgibbs#
8684568Sgibbsdma:
8694568Sgibbs	mov	DFCNTRL,SINDEX
8704568Sgibbsdma1:
8714568Sgibbsdma2:
8724568Sgibbs	test	SSTAT0,0x1	jnz dma3	# DMADONE
8734568Sgibbs	test	SSTAT1,0x10	jz dma1		# PHASEMIS, ie. underrun
8744568Sgibbs
8754568Sgibbs#  We will be "done" DMAing when the transfer count goes to zero, or
8764568Sgibbs#  the target changes the phase (in light of this, it makes sense that
8774568Sgibbs#  the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
8784866Sgibbs#  doing a SCSI->Host transfer, the data FIFO should be flushed auto-
8794866Sgibbs#  magically on STCNT=0 or a phase change, so just wait for FIFO empty
8804866Sgibbs#  status.
8814568Sgibbs#
8824568Sgibbsdma3:
8834568Sgibbs	test	SINDEX,0x4	jnz dma5	# DIRECTION
8844568Sgibbsdma4:
8855562Sgibbs	test	DFSTATUS,0x1	jz dma4		# !FIFOEMP
8864568Sgibbs
8874568Sgibbs#  Now shut the DMA enables off, and copy STCNT (ie. the underrun
8884568Sgibbs#  amount, if any) to the SCB registers; SG_COUNT will get copied to
8894568Sgibbs#  the SCB's residual S/G count field after sg_advance is called.  Make
8904568Sgibbs#  sure that the DMA enables are actually off first lest we get an ILLSADDR.
8914568Sgibbs#
8924568Sgibbsdma5:
8934568Sgibbs	clr	DFCNTRL				# disable DMA
8944568Sgibbsdma6:
8954568Sgibbs	test	DFCNTRL,0x38	jnz dma6	# SCSIENACK|SDMAENACK|HDMAENACK
8964568Sgibbs
8974568Sgibbs	mvi	A,3
8984568Sgibbs	mvi	DINDEX,SCBARRAY+15
8994568Sgibbs	mvi	STCNT		call bcopy
9004568Sgibbs
9014568Sgibbs	ret
9024568Sgibbs
9034568Sgibbs#  Common SCSI initialization for selection and reselection.  Expects
9044568Sgibbs#  the target SCSI ID to be in the upper four bits of SINDEX, and A's
9054568Sgibbs#  contents are stomped on return.
9064568Sgibbs#
9074568Sgibbsinitialize:
9084568Sgibbs	and	SCSIID,0xf0,SINDEX		# target ID
9095326Sgibbs	test	SBLKCTL,0x08	jnz initialize_b
9105326Sgibbs	mvi	SINDEX,SCSICONF_A
9115326Sgibbs	test	FLAGS,WIDE_BUS	jnz initialize_wide
9125326Sgibbs	and	A,0x7,SCSICONF_A		# SCSI_ID_A[210]
9135326Sgibbs	jmp	initialize_2
9145326Sgibbsinitialize_b:
9155326Sgibbs	and	A,0x7,SCSICONF_B		# SCSI_ID_B[210]
9165326Sgibbs	mvi	SCSICONF_B	 jmp	initialize_2
9175326Sgibbs
9185326Sgibbsinitialize_wide:
9195562Sgibbs	and	A,0x0f,SCSICONF_B		# SCSI_ID_B[210]
9205326Sgibbs
9215326Sgibbsinitialize_2:
9224568Sgibbs	or	SCSIID,A
9234568Sgibbs
9244568Sgibbs#  Esundry initialization.
9254568Sgibbs#
9264568Sgibbs	clr	DROPATN
9274568Sgibbs	clr	SIGSTATE
9284568Sgibbs
9294568Sgibbs#  Turn on Automatic PIO mode now, before we expect to see an REQ
9304568Sgibbs#  from the target.  It shouldn't hurt anything to leave it on.  Set
9314568Sgibbs#  CLRCHN here before the target has entered a data transfer mode -
9324568Sgibbs#  with synchronous SCSI, if you do it later, you blow away some
9334568Sgibbs#  data in the SCSI FIFO that the target has already sent to you.
9344568Sgibbs#
9354866Sgibbs#  DFON is a 7870 bit enabling digital filtering of REQ and ACK signals.
9364866Sgibbs#
9374866Sgibbs	mvi	SXFRCTL0,0x8a			# DFON|SPIOEN|CLRCHN
9384568Sgibbs
9394568Sgibbs#  Set SCSI bus parity checking and the selection timeout value,
9404568Sgibbs#  and enable the hardware selection timer.  Set the SELTO interrupt
9414568Sgibbs#  to signal the driver.
9424568Sgibbs#
9434866Sgibbs#  STPWEN is 7870-specific, enabling an external termination power source.
9444866Sgibbs#
9455326Sgibbs	and	A,0x38,SINDIR			# PARITY_ENB|SEL_TIM[10]
9465562Sgibbs	or	SXFRCTL1,0x7,A			# ENSTIMER|ACTBEGEB|STPWEN
9474568Sgibbs	mvi	SIMODE1,0x84			# ENSELTIMO|ENSCSIPERR
9484568Sgibbs	
9494568Sgibbs#  Initialize scatter-gather pointers by setting up the working copy
9504568Sgibbs#  in scratch RAM.
9514568Sgibbs#
9524568Sgibbs	call	sg_scb2ram
9534568Sgibbs
9544568Sgibbs#  Initialize SCSIRATE with the appropriate value for this target.
9554568Sgibbs#
9565562Sgibbs	call	ndx_dtr
9574568Sgibbs	mov	SCSIRATE,SINDIR
9584568Sgibbs	ret
9594568Sgibbs
9604568Sgibbs#  Assert that if we've been reselected, then we've seen an IDENTIFY
9614568Sgibbs#  message.
9624568Sgibbs#
9634568Sgibbsassert:
9645562Sgibbs	test	FLAGS,RESELECTED	jz assert1	# reselected?
9655562Sgibbs	test	FLAGS,IDENTIFY_SEEN	jnz assert1	# seen IDENTIFY?
9664568Sgibbs
9674568Sgibbs	mvi	INTSTAT,SIGNAL_2		# no - cause a kernel panic
9684568Sgibbs
9694568Sgibbsassert1:
9704568Sgibbs	ret
9714568Sgibbs
9724568Sgibbs#  Find out if disconnection is ok from the information the BIOS has left
9735326Sgibbs#  us.  The tcl from SCBARRAY+1 should be in SINDEX; A will
9744866Sgibbs#  contain either 0x40 (disconnection ok) or 0x00 (disconnection not ok)
9754568Sgibbs#  on exit.
9764568Sgibbs#
9775326Sgibbs#  To allow for wide or twin busses, we check the upper bit of the target ID
9785326Sgibbs#  and the channel ID and look at the appropriate disconnect register. 
9794568Sgibbs#
9804568Sgibbsdisconnect:
9814568Sgibbs	and	FUNCTION1,0x70,SINDEX		# strip off extra just in case
9824568Sgibbs	mov	A,FUNCTION1
9835326Sgibbs	test	SINDEX, 0x88	jz disconnect_a
9845326Sgibbs
9855326Sgibbs	test	DISC_DSB_B,A	jz disconnect1	# bit nonzero if DISabled
9865326Sgibbs	clr	A		ret
9875326Sgibbs
9885326Sgibbsdisconnect_a:
9894568Sgibbs	test	DISC_DSB_A,A	jz disconnect1	# bit nonzero if DISabled
9905326Sgibbs	clr	A		ret
9914568Sgibbs
9924568Sgibbsdisconnect1:
9934568Sgibbs	mvi	A,0x40		ret
9944568Sgibbs
9954568Sgibbs#  Locate the SCB matching the target ID in SELID and the lun in the lower
9964568Sgibbs#  three bits of SINDEX, and switch the SCB to it.  Have the kernel print
9974568Sgibbs#  a warning message if it can't be found, and generate an ABORT message
9984866Sgibbs#  to the target.  We keep the value of the t/c/l that we are trying to find
9994568Sgibbs#  in DINDEX so it is not overwritten during our check to see if we are
10004568Sgibbs#  at the last SCB.
10014568Sgibbs#
10024568SgibbsfindSCB:
10034568Sgibbs	and	A,0x7,SINDEX		# lun in lower three bits
10045326Sgibbs	or	DINDEX,A,SELID		
10055326Sgibbs	and	DINDEX,0xf7
10065326Sgibbs	and	A,0x08,SBLKCTL		# B Channel??
10075326Sgibbs	or	DINDEX,A
10084568Sgibbs	clr	SINDEX
10094568Sgibbs
10104568SgibbsfindSCB1:
10114866Sgibbs	mov	A,DINDEX
10124568Sgibbs	mov	SCBPTR,SINDEX			# switch to new SCB
10134568Sgibbs	cmp	SCBARRAY+1,A	jne findSCB2	# target ID/channel/lun match?
10144568Sgibbs	test	SCBARRAY+0,0x4	jz findSCB2	# should be disconnected
10154568Sgibbs	ret
10164568Sgibbs
10174568SgibbsfindSCB2:
10184568Sgibbs	inc	SINDEX
10194568Sgibbs	mov	A,SCBCOUNT
10204568Sgibbs	cmp	SINDEX,A	jne findSCB1
10214568Sgibbs
10224568Sgibbs	mvi	INTSTAT,SIGNAL_3		# not found - signal kernel
10234568Sgibbs	mvi	0x6		call mk_mesg	# ABORT message
10244568Sgibbs
10254568Sgibbs	or	SINDEX,0x10,SIGSTATE		# assert ATNO
10264568Sgibbs	call	scsisig
10274568Sgibbs	ret
10284568Sgibbs
10294568Sgibbs#  Make a working copy of the scatter-gather parameters in the SCB.
10304568Sgibbs#
10314568Sgibbssg_scb2ram:
10324568Sgibbs	mov	SG_COUNT,SCBARRAY+2
10334568Sgibbs
10344568Sgibbs	mvi	A,4
10354568Sgibbs	mvi	DINDEX,SG_NEXT
10364568Sgibbs	mvi	SCBARRAY+3	call bcopy
10374568Sgibbs
10384568Sgibbs	mvi	SG_NOLOAD,0x80
10394568Sgibbs	test	SCBARRAY+0,0x10	jnz sg_scb2ram1	# don't reload s/g?
10404568Sgibbs	clr	SG_NOLOAD
10414568Sgibbs
10424568Sgibbssg_scb2ram1:
10434568Sgibbs	ret
10444568Sgibbs
10454568Sgibbs#  Copying RAM values back to SCB, for Save Data Pointers message.
10464568Sgibbs#
10474568Sgibbssg_ram2scb:
10484568Sgibbs	mov	SCBARRAY+2,SG_COUNT
10494568Sgibbs
10504568Sgibbs	mvi	A,4
10514568Sgibbs	mvi	DINDEX,SCBARRAY+3
10524568Sgibbs	mvi	SG_NEXT		call bcopy
10534568Sgibbs
10544568Sgibbs	and	SCBARRAY+0,0xef,SCBARRAY+0
10554568Sgibbs	test	SG_NOLOAD,0x80	jz sg_ram2scb1	# reload s/g?
10564568Sgibbs	or	SCBARRAY+0,0x10
10574568Sgibbs
10584568Sgibbssg_ram2scb1:
10594568Sgibbs	ret
10604568Sgibbs
10614568Sgibbs#  Load a struct scatter if needed and set up the data address and
10624568Sgibbs#  length.  If the working value of the SG count is nonzero, then
10634568Sgibbs#  we need to load a new set of values.
10644568Sgibbs#
10654568Sgibbs#  This, like the above DMA, assumes a little-endian host data storage.
10664568Sgibbs#
10674568Sgibbssg_load:
10684568Sgibbs	test	SG_COUNT,0xff	jz sg_load3	# SG being used?
10694568Sgibbs	test	SG_NOLOAD,0x80	jnz sg_load3	# don't reload s/g?
10704568Sgibbs
10714568Sgibbs	clr	HCNT+2
10724568Sgibbs	clr	HCNT+1
10734568Sgibbs	mvi	HCNT+0,SG_SIZEOF
10744568Sgibbs
10754568Sgibbs	mvi	A,4
10764568Sgibbs	mvi	DINDEX,HADDR
10774568Sgibbs	mvi	SG_NEXT		call bcopy
10784568Sgibbs
10794568Sgibbs	mvi	DFCNTRL,0xd			# HDMAEN|DIRECTION|FIFORESET
10804568Sgibbs
10814568Sgibbs#  Wait for DMA from host memory to data FIFO to complete, then disable
10824568Sgibbs#  DMA and wait for it to acknowledge that it's off.
10834568Sgibbs#
10844568Sgibbssg_load1:
10854568Sgibbs	test	DFSTATUS,0x8	jz sg_load1	# HDONE
10864568Sgibbs
10874568Sgibbs	clr	DFCNTRL				# disable DMA
10884568Sgibbssg_load2:
10894568Sgibbs	test	DFCNTRL,0x8	jnz sg_load2	# HDMAENACK
10904568Sgibbs
10914568Sgibbs#  Copy data from FIFO into SCB data pointer and data count.  This assumes
10924568Sgibbs#  that the struct scatterlist has this structure (this and sizeof(struct
10934568Sgibbs#  scatterlist) == 12 are asserted in aic7xxx.c):
10944568Sgibbs#
10954568Sgibbs#	struct scatterlist {
10964568Sgibbs#		char *address;		/* four bytes, little-endian order */
10974568Sgibbs#		...			/* four bytes, ignored */
10984568Sgibbs#		unsigned short length;	/* two bytes, little-endian order */
10994568Sgibbs#	}
11004568Sgibbs#
11014568Sgibbs
11024568Sgibbs# Not in FreeBSD.  the scatter list is only 8 bytes.
11034568Sgibbs# 
11044568Sgibbs# struct ahc_dma_seg {
11054568Sgibbs#       physaddr addr;                  /* four bytes, little-endian order */
11064568Sgibbs#       long    len;                    /* four bytes, little endian order */   
11074568Sgibbs# };
11084568Sgibbs#
11094568Sgibbs
11104568Sgibbs	mov	SCBARRAY+19,DFDAT		# new data address
11114568Sgibbs	mov	SCBARRAY+20,DFDAT
11124568Sgibbs	mov	SCBARRAY+21,DFDAT
11134568Sgibbs	mov	SCBARRAY+22,DFDAT
11144568Sgibbs
11154568Sgibbs	mov	SCBARRAY+23,DFDAT
11164568Sgibbs	mov	SCBARRAY+24,DFDAT
11174568Sgibbs	mov	SCBARRAY+25,DFDAT
11184568Sgibbs	mov	NONE,DFDAT			#Only support 24 bit length.
11194568Sgibbs
11204568Sgibbssg_load3:
11214568Sgibbs	ret
11224568Sgibbs
11234568Sgibbs#  Advance the scatter-gather pointers only IF NEEDED.  If SG is enabled,
11244568Sgibbs#  and the SCSI transfer count is zero (note that this should be called
11254568Sgibbs#  right after a DMA finishes), then move the working copies of the SG
11264568Sgibbs#  pointer/length along.  If the SCSI transfer count is not zero, then
11274568Sgibbs#  presumably the target is disconnecting - do not reload the SG values
11284568Sgibbs#  next time.
11294568Sgibbs#
11304568Sgibbssg_advance:
11314568Sgibbs	test	SG_COUNT,0xff	jz sg_advance2	# s/g enabled?
11324568Sgibbs
11334568Sgibbs	test	STCNT+0,0xff	jnz sg_advance1	# SCSI transfer count nonzero?
11344568Sgibbs	test	STCNT+1,0xff	jnz sg_advance1
11354568Sgibbs	test	STCNT+2,0xff	jnz sg_advance1
11364568Sgibbs
11374568Sgibbs	clr	SG_NOLOAD			# reload s/g next time
11384568Sgibbs	dec	SG_COUNT			# one less segment to go
11394568Sgibbs
11404568Sgibbs	clr	A				# add sizeof(struct scatter)
11414568Sgibbs	add	SG_NEXT+0,SG_SIZEOF,SG_NEXT+0
11424568Sgibbs	adc	SG_NEXT+1,A,SG_NEXT+1
11434568Sgibbs	adc	SG_NEXT+2,A,SG_NEXT+2
11444568Sgibbs	adc	SG_NEXT+3,A,SG_NEXT+3
11454568Sgibbs
11464568Sgibbs	ret
11474568Sgibbs
11484568Sgibbssg_advance1:
11494568Sgibbs	mvi	SG_NOLOAD,0x80			# don't reload s/g next time
11504568Sgibbssg_advance2:
11514568Sgibbs	ret
11524568Sgibbs
11534568Sgibbs#  Add the array base SYNCNEG to the target offset (the target address
11544568Sgibbs#  is in SCSIID), and return the result in SINDEX.  The accumulator
11554568Sgibbs#  contains the 3->8 decoding of the target ID on return.
11564568Sgibbs#
11575562Sgibbsndx_dtr:
11584568Sgibbs	shr	A,SCSIID,4
11595562Sgibbs	test	SBLKCTL,0x08	jz ndx_dtr_2
11605326Sgibbs	or	A,0x08		# Channel B entries add 8
11615562Sgibbsndx_dtr_2:
11624568Sgibbs	add	SINDEX,SYNCNEG,A
11634568Sgibbs
11644568Sgibbs	and	FUNCTION1,0x70,SCSIID		# 3-bit target address decode
11654568Sgibbs	mov	A,FUNCTION1	ret
11664568Sgibbs
11675562Sgibbs#  If we need to negotiate transfer parameters, build the WDTR or SDTR message
11684568Sgibbs#  starting at the address passed in SINDEX.  DINDEX is modified on return.
11695562Sgibbs#  The SCSI-II spec requires that Wide negotiation occur first and you can
11705562Sgibbs#  only negotiat one or the other at a time otherwise in the event of a message
11715562Sgibbs#  reject, you wouldn't be able to tell which message was the culpret.
11724568Sgibbs#
11735562Sgibbsmk_dtr:
11744568Sgibbs	mov	DINDEX,SINDEX			# save SINDEX
11754568Sgibbs
11765562Sgibbs	call	ndx_dtr
11775562Sgibbs	test	SCBARRAY+1,0x88	jz  mk_dtr_a
11785562Sgibbs	test	NEEDWDTR_B,A	jnz mk_wdtr_16	# do we need negotiation?
11795562Sgibbs	test	NEEDSDTR_B,A	jnz mk_sdtr
11804568Sgibbs	ret
11815562Sgibbsmk_dtr_a:
11825562Sgibbs	test	NEEDWDTR_A,A	jnz mk_wdtr_16	# do we need negotiation?
11835562Sgibbs	test	NEEDSDTR_A,A	jnz mk_sdtr
11845326Sgibbs	ret
11854568Sgibbs
11865562Sgibbsmk_wdtr_16:
11875562Sgibbs	mvi	ARG_1,1			# 16bit wide bus
11885562Sgibbs
11895562Sgibbsmk_wdtr:
11904568Sgibbs	mvi	DINDIR,1			# extended message
11915562Sgibbs	mvi	DINDIR,2			# extended message length = 2
11925562Sgibbs	mvi	DINDIR,3			# WDTR code
11935562Sgibbs	mov	DINDIR,ARG_1			# bus width
11945562Sgibbs
11955562Sgibbs	add	MSG_LEN,-MSG_START+0,DINDEX	# update message length
11965562Sgibbs	ret
11975562Sgibbs	
11985562Sgibbsmk_sdtr:
11995562Sgibbs	mvi	DINDIR,1			# extended message
12004568Sgibbs	mvi	DINDIR,3			# extended message length = 3
12014568Sgibbs	mvi	DINDIR,1			# SDTR code
12025562Sgibbs	call	sdtr_to_rate
12035562Sgibbs	mov	DINDIR,RETURN_1			# REQ/ACK transfer period
12045562Sgibbs	and	DINDIR,0xf,SINDIR		# Sync Offset
12054568Sgibbs
12064568Sgibbs	add	MSG_LEN,-MSG_START+0,DINDEX	# update message length
12074568Sgibbs	ret
12084568Sgibbs
12095562Sgibbs# Tag Message if Tag enabled in SCB control block.  Use SCBPTR as the tag
12105562Sgibbs# value
12115562Sgibbs
12125562Sgibbs#mk_tag:
12135562Sgibbs#	test	SCBARRAY+0,0x10	jz mk_tag_done	# Tag Enabled?
12145562Sgibbs#	and	A,0x03,SCBARRYA+0
12155562Sgibbs#	or	A,0x20
12165562Sgibbs#	mov	DINDIR,A
12175562Sgibbs#	mov	DINDIR,SCBPTR
12185562Sgibbs#
12195562Sgibbs#	add	MSG_LEN,-MSG_START+0,DINDEX	# update message length
12205562Sgibbs
12215562Sgibbs#mk_tag_done:
12225562Sgibbs#	ret
12235562Sgibbs
12245562Sgibbs
12254568Sgibbs#  Set SCSI bus control signal state.  This also saves the last-written
12264568Sgibbs#  value into a location where the higher-level driver can read it - if
12274568Sgibbs#  it has to send an ABORT or RESET message, then it needs to know this
12284568Sgibbs#  so it can assert ATN without upsetting SCSISIGO.  The new value is
12294568Sgibbs#  expected in SINDEX.  Change the actual state last to avoid contention
12304568Sgibbs#  from the driver.
12314568Sgibbs#
12324568Sgibbsscsisig:
12334568Sgibbs	mov	SIGSTATE,SINDEX
12344568Sgibbs	mov	SCSISIGO,SINDEX	ret
12355562Sgibbs
12365562Sgibbssdtr_to_rate:
12375562Sgibbs	call	ndx_dtr				# index scratch space for target
12385562Sgibbs	shr	A,SINDIR,0x4
12395562Sgibbs	dec	SINDEX				#Preserve SINDEX
12405562Sgibbs	and	A,0x7
12415562Sgibbs	clr	RETURN_1
12425562Sgibbssdtr_to_rate_loop:
12435562Sgibbs	test	A,0x0f	jz sdtr_to_rate_done
12445562Sgibbs	add	RETURN_1,0x18
12455562Sgibbs	dec	A	
12465562Sgibbs	jmp	sdtr_to_rate_loop
12475562Sgibbssdtr_to_rate_done:
12485562Sgibbs	shr	RETURN_1,0x2
12495562Sgibbs	add	RETURN_1,0x18	ret
1250