aic7xxx.seq revision 5326
14866Sgibbs# @(#)aic7xxx.seq 1.31 94/11/25 jda
24568Sgibbs#
34568Sgibbs# Adaptec 274x 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
204866SgibbsVERSION AIC7XXX_SEQ_VERSION 1.31
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
1004568Sgibbs
1014568Sgibbs#  The host adapter card (at least the BIOS) uses 20-2f for SCSI
1025326Sgibbs#  device information, 32-33 and 5a-5f as well. As it turns out, the
1035326Sgibbs#  BIOS trashes 20-2f, writing the synchronous negotiation results
1044568Sgibbs#  on top of the BIOS values, so we re-use those for our per-target
1054568Sgibbs#  scratchspace (actually a value that can be copied directly into
1065326Sgibbs#  SCSIRATE).  The kernel driver will enable synchronous negotiation
1075326Sgibbs#  for all targets that have a value other than 0 in the lower four
1085326Sgibbs#  bits of the target scratch space.  This should work irregardless of
1095326Sgibbs#  whether the bios has been installed. NEEDSDTR has one bit per target 
1105326Sgibbs#  indicating if an SDTR message is needed for that device - this will 
1115326Sgibbs#  be set initially (based on a search through the target scratch space), 
1125326Sgibbs#  as well as after a bus reset condition.
1134568Sgibbs#
1144568Sgibbs#  The high bit of DROPATN is set if ATN should be dropped before the ACK
1154568Sgibbs#  when outb is called.  REJBYTE contains the first byte of a MESSAGE IN
1164568Sgibbs#  message, so the driver can report an intelligible error if a message is
1174568Sgibbs#  rejected.
1184568Sgibbs#
1194568Sgibbs#  RESELECT's high bit is true if we are currently handling a reselect;
1204568Sgibbs#  its next-highest bit is true ONLY IF we've seen an IDENTIFY message
1214568Sgibbs#  from the reselecting target.  If we haven't had IDENTIFY, then we have
1224568Sgibbs#  no idea what the lun is, and we can't select the right SCB register
1234568Sgibbs#  bank, so force a kernel panic if the target attempts a data in/out or
1244568Sgibbs#  command phase instead of corrupting something.
1254568Sgibbs#
1264568Sgibbs#  Note that SG_NEXT occupies four bytes.
1274568Sgibbs#
1284568SgibbsSYNCNEG		= 0x20
1294568Sgibbs
1304568SgibbsDROPATN		= 0x30
1314568SgibbsREJBYTE		= 0x31
1325326SgibbsDISC_DSB_A	= 0x32
1335326SgibbsDISC_DSB_B	= 0x33
1344568SgibbsRESELECT	= 0x34
1354568Sgibbs
1364568SgibbsMSG_FLAGS	= 0x35
1374568SgibbsMSG_LEN		= 0x36
1384568SgibbsMSG_START+0	= 0x37
1394568SgibbsMSG_START+1	= 0x38
1404568SgibbsMSG_START+2	= 0x39
1414568SgibbsMSG_START+3	= 0x3a
1424568SgibbsMSG_START+4	= 0x3b
1434568SgibbsMSG_START+5	= 0x3c
1444568Sgibbs-MSG_START+0	= 0xc9				# 2's complement of MSG_START+0
1454568Sgibbs
1464568SgibbsARG_1		= 0x4c				# sdtr conversion args & return
1474568SgibbsRETURN_1	= 0x4c
1484568Sgibbs
1495326SgibbsSIGSTATE	= 0x4d				# value written to SCSISIGO
1505326SgibbsNEEDSDTR_A	= 0x4e				# send SDTR message, 1 bit/trgt
1515326SgibbsNEEDSDTR_B	= 0x4f
1524568Sgibbs
1534568SgibbsSG_SIZEOF	= 0x8 				# sizeof(struct scatterlist)
1544568SgibbsSG_NOLOAD	= 0x50				# load SG pointer/length?
1554568SgibbsSG_COUNT	= 0x51				# working value of SG count
1564568SgibbsSG_NEXT		= 0x52				# working value of SG pointer
1574568SgibbsSG_NEXT+0	= 0x52
1584568SgibbsSG_NEXT+1	= 0x53
1594568SgibbsSG_NEXT+2	= 0x54
1604568SgibbsSG_NEXT+3	= 0x55
1614568Sgibbs
1624568SgibbsSCBCOUNT	= 0x56				# the actual number of SCBs
1635326SgibbsFLAGS		= 0x57				# Device configuration flags
1645326SgibbsTWIN_BUS	= 0x01
1655326SgibbsWIDE_BUS	= 0x02
1664568Sgibbs
1675326SgibbsACTIVE_A	= 0x58
1685326SgibbsACTIVE_B	= 0x59
1695326Sgibbs
1704866Sgibbs#  Poll QINCNT for work - the lower bits contain
1714568Sgibbs#  the number of entries in the Queue In FIFO.
1724568Sgibbs#
1734568Sgibbsstart:
1745326Sgibbs	test	FLAGS,TWIN_BUS	jz start2	# Are we a twin channel device?
1755326Sgibbs# For fairness, we check the other bus first, since we just finished a 
1765326Sgibbs# transaction on the current channel.
1775326Sgibbs	xor	SBLKCTL,0x08			# Toggle to the other bus
1784568Sgibbs	test	SCSISIGI,0x4	jnz reselect	# BSYI
1795326Sgibbs	xor	SBLKCTL,0x08			# Toggle to the original bus
1805326Sgibbsstart2:
1815326Sgibbs	test	SCSISIGI,0x4	jnz reselect	# BSYI
1824866Sgibbs	test	QINCNT,SCBMASK	jz start
1834568Sgibbs
1844568Sgibbs#  We have at least one queued SCB now.  Set the SCB pointer
1854568Sgibbs#  from the FIFO so we see the right bank of SCB registers,
1864568Sgibbs#  then set SCSI options and set the initiator and target
1874568Sgibbs#  SCSI IDs.
1884568Sgibbs#
1894568Sgibbs	mov	SCBPTR,QINFIFO
1904568Sgibbs
1914568Sgibbs# See if there is not already an active SCB for this target.  This code
1925326Sgibbs# locks out on a per target basis instead of target/lun.  Although this
1935326Sgibbs# is not ideal for devices that have multiple luns active at the same
1945326Sgibbs# time, it is faster than looping through all SCB's looking for active
1955326Sgibbs# commands.  It may be benificial to make findscb a more general procedure
1965326Sgibbs# to see if the added cost of the search is negligible.  This code also 
1975326Sgibbs# assumes that the kernel driver will clear the active flags on board 
1985326Sgibbs# initialization, board reset, and a target's SELTO.
1994568Sgibbs
2004568Sgibbs	and	FUNCTION1,0x70,SCBARRAY+1
2014568Sgibbs	mov	A,FUNCTION1
2025326Sgibbs	test	SCBARRAY+1,0x88	jz test_a	# Id < 8 && A channel
2035326Sgibbs
2045326Sgibbs	test	ACTIVE_B,A	jnz requeue
2055326Sgibbs	or	ACTIVE_B,A	# Mark the current target as busy
2065326Sgibbs	jmp	start_scb
2075326Sgibbs
2084568Sgibbs# Place the currently active back on the queue for later processing
2095326Sgibbsrequeue:
2104568Sgibbs	mov	QINFIFO, SCBPTR
2114568Sgibbs	jmp	start
2124568Sgibbs
2135326Sgibbstest_a:
2145326Sgibbs	test	ACTIVE_A,A	jnz requeue
2155326Sgibbs	or	ACTIVE_A,A	# Mark the current target as busy
2165326Sgibbs
2175326Sgibbsstart_scb:
2185326Sgibbs	and	A,0x08,SCBARRAY+1
2195326Sgibbs	mov	SBLKCTL,A	# select channel, !wide
2204568Sgibbs	mov	SCBARRAY+1	call initialize
2214568Sgibbs	clr	SG_NOLOAD
2224568Sgibbs	clr	RESELECT
2234568Sgibbs
2244568Sgibbs#  As soon as we get a successful selection, the target should go
2254568Sgibbs#  into the message out phase since we have ATN asserted.  Prepare
2264568Sgibbs#  the message to send, locking out the device driver.  If the device
2274568Sgibbs#  driver hasn't beaten us with an ABORT or RESET message, then tack
2284866Sgibbs#  on an SDTR negotiation if required.
2294568Sgibbs#
2304568Sgibbs#  Messages are stored in scratch RAM starting with a flag byte (high bit
2314568Sgibbs#  set means active message), one length byte, and then the message itself.
2324568Sgibbs#
2334568Sgibbs	mov	SCBARRAY+1	call disconnect	# disconnect ok?
2344568Sgibbs
2354568Sgibbs	and	SINDEX,0x7,SCBARRAY+1		# lun
2364568Sgibbs	or	SINDEX,A			# return value from disconnect
2374568Sgibbs	or	SINDEX,0x80	call mk_mesg	# IDENTIFY message
2384568Sgibbs
2394568Sgibbs	mov	A,SINDEX
2404568Sgibbs	cmp	MSG_START+0,A	jne !message	# did driver beat us?
2414568Sgibbs	mvi	MSG_START+1	call mk_sdtr	# build SDTR message if needed
2424568Sgibbs
2434568Sgibbs!message:
2444568Sgibbs
2454568Sgibbs#  Enable selection phase as an initiator, and do automatic ATN
2464568Sgibbs#  after the selection.
2474568Sgibbs#
2484568Sgibbs	mvi	SCSISEQ,0x48			# ENSELO|ENAUTOATNO
2494568Sgibbs
2504568Sgibbs#  Wait for successful arbitration.  The AIC-7770 documentation says
2514568Sgibbs#  that SELINGO indicates successful arbitration, and that it should
2524568Sgibbs#  be used to look for SELDO.  However, if the sequencer is paused at
2534568Sgibbs#  just the right time - a parallel fsck(8) on two drives did it for
2544568Sgibbs#  me - then SELINGO can flip back to false before we've seen it.  This
2554568Sgibbs#  makes the sequencer sit in the arbitration loop forever.  This is
2564568Sgibbs#  Not Good.
2574568Sgibbs#
2584568Sgibbs#  Therefore, I've added a check in the arbitration loop for SELDO
2594568Sgibbs#  too.  This could arguably be made a critical section by disabling
2604568Sgibbs#  pauses, but I don't want to make a potentially infinite loop a CS.
2614568Sgibbs#  I suppose you could fold it into the select loop, too, but since
2624568Sgibbs#  I've been hunting this bug for four days it's kinda like a trophy.
2634568Sgibbs#
2644568Sgibbsarbitrate:
2654568Sgibbs	test	SSTAT0,0x40	jnz *select	# SELDO
2664568Sgibbs	test	SSTAT0,0x10	jz arbitrate	# SELINGO
2674568Sgibbs
2684568Sgibbs#  Wait for a successful selection.  If the hardware selection
2694568Sgibbs#  timer goes off, then the driver gets the interrupt, so we don't
2704568Sgibbs#  need to worry about it.
2714568Sgibbs#
2724568Sgibbsselect:
2734568Sgibbs	test	SSTAT0,0x40	jz select	# SELDO
2744568Sgibbs	jmp	*select
2754568Sgibbs
2764568Sgibbs#  Reselection is being initiated by a target - we've seen the BSY
2774568Sgibbs#  line driven active, and we didn't do it!  Enable the reselection
2784568Sgibbs#  hardware, and wait for it to finish.  Make a note that we've been
2794568Sgibbs#  reselected, but haven't seen an IDENTIFY message from the target
2804568Sgibbs#  yet.
2814568Sgibbs#
2824568Sgibbsreselect:
2834568Sgibbs	mvi	SCSISEQ,0x10			# ENRSELI
2844568Sgibbs
2854568Sgibbsreselect1:
2864568Sgibbs	test	SSTAT0,0x20	jz reselect1	# SELDI
2874568Sgibbs	mov	SELID		call initialize
2884568Sgibbs
2894568Sgibbs	mvi	RESELECT,0x80			# reselected, no IDENTIFY
2904568Sgibbs
2914568Sgibbs#  After the [re]selection, make sure that the [re]selection enable
2924568Sgibbs#  bit is off.  This chip is flaky enough without extra things
2934568Sgibbs#  turned on.  Also clear the BUSFREE bit in SSTAT1 since we'll be
2944568Sgibbs#  using it shortly.
2954568Sgibbs#
2964568Sgibbs*select:
2974568Sgibbs	clr	SCSISEQ
2984568Sgibbs	mvi	CLRSINT1,0x8			# CLRBUSFREE
2994568Sgibbs
3004568Sgibbs#  Main loop for information transfer phases.  If BSY is false, then
3014568Sgibbs#  we have a bus free condition, expected or not.  Otherwise, wait
3024568Sgibbs#  for the target to assert REQ before checking MSG, C/D and I/O
3034568Sgibbs#  for the bus phase.
3044568Sgibbs#
3054568Sgibbs#  We can't simply look at the values of SCSISIGI here (if we want
3064568Sgibbs#  to do synchronous data transfer), because the target won't assert
3074568Sgibbs#  REQ if it's already sent us some data that we haven't acknowledged
3084568Sgibbs#  yet.
3094568Sgibbs#
3104568SgibbsITloop:
3114568Sgibbs	test	SSTAT1,0x8	jnz p_busfree	# BUSFREE
3124568Sgibbs	test	SSTAT1,0x1	jz ITloop	# REQINIT
3134568Sgibbs
3144568Sgibbs	and	A,0xe0,SCSISIGI			# CDI|IOI|MSGI
3154568Sgibbs
3164568Sgibbs	cmp	ALLZEROS,A	je p_dataout
3174568Sgibbs	cmp	A,0x40		je p_datain
3184568Sgibbs	cmp	A,0x80		je p_command
3194568Sgibbs	cmp	A,0xc0		je p_status
3204568Sgibbs	cmp	A,0xa0		je p_mesgout
3214568Sgibbs	cmp	A,0xe0		je p_mesgin
3224568Sgibbs
3234568Sgibbs	mvi	INTSTAT,SIGNAL_0		# unknown - signal driver
3244568Sgibbs
3254568Sgibbsp_dataout:
3264568Sgibbs	mvi	0		call scsisig	# !CDO|!IOO|!MSGO
3274568Sgibbs	call	assert
3284568Sgibbs	call	sg_load
3294568Sgibbs
3304568Sgibbs	mvi	A,3
3314568Sgibbs	mvi	DINDEX,HCNT
3324568Sgibbs	mvi	SCBARRAY+23	call bcopy
3334568Sgibbs
3344568Sgibbs	mvi	A,3
3354568Sgibbs	mvi	DINDEX,STCNT
3364568Sgibbs	mvi	SCBARRAY+23	call bcopy
3374568Sgibbs
3384568Sgibbs	mvi	A,4
3394568Sgibbs	mvi	DINDEX,HADDR
3404568Sgibbs	mvi	SCBARRAY+19	call bcopy
3414568Sgibbs
3424568Sgibbs	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
3434568Sgibbs						#   DIRECTION|FIFORESET
3444568Sgibbs
3454568Sgibbs#  After a DMA finishes, save the final transfer pointer and count
3464568Sgibbs#  back into the SCB, in case a device disconnects in the middle of
3474568Sgibbs#  a transfer.  Use SHADDR and STCNT instead of HADDR and HCNT, since
3484568Sgibbs#  it's a reflection of how many bytes were transferred on the SCSI
3494568Sgibbs#  (as opposed to the host) bus.
3504568Sgibbs#
3514568Sgibbs	mvi	A,3
3524568Sgibbs	mvi	DINDEX,SCBARRAY+23
3534568Sgibbs	mvi	STCNT		call bcopy
3544568Sgibbs
3554568Sgibbs	mvi	A,4
3564568Sgibbs	mvi	DINDEX,SCBARRAY+19
3574568Sgibbs	mvi	SHADDR		call bcopy
3584568Sgibbs
3594568Sgibbs	call	sg_advance
3604568Sgibbs	mov	SCBARRAY+18,SG_COUNT		# residual S/G count
3614568Sgibbs
3624568Sgibbs	jmp	ITloop
3634568Sgibbs
3644568Sgibbsp_datain:
3654568Sgibbs	mvi	0x40		call scsisig	# !CDO|IOO|!MSGO
3664568Sgibbs	call	assert
3674568Sgibbs	call	sg_load
3684568Sgibbs
3694568Sgibbs	mvi	A,3
3704568Sgibbs	mvi	DINDEX,HCNT
3714568Sgibbs	mvi	SCBARRAY+23	call bcopy
3724568Sgibbs
3734568Sgibbs	mvi	A,3
3744568Sgibbs	mvi	DINDEX,STCNT
3754568Sgibbs	mvi	SCBARRAY+23	call bcopy
3764568Sgibbs
3774568Sgibbs	mvi	A,4
3784568Sgibbs	mvi	DINDEX,HADDR
3794568Sgibbs	mvi	SCBARRAY+19	call bcopy
3804568Sgibbs
3814568Sgibbs	mvi	0x39		call dma	# SCSIEN|SDMAEN|HDMAEN|
3824568Sgibbs						#   !DIRECTION|FIFORESET
3834568Sgibbs	mvi	A,3
3844568Sgibbs	mvi	DINDEX,SCBARRAY+23
3854568Sgibbs	mvi	STCNT		call bcopy
3864568Sgibbs
3874568Sgibbs	mvi	A,4
3884568Sgibbs	mvi	DINDEX,SCBARRAY+19
3894568Sgibbs	mvi	SHADDR		call bcopy
3904568Sgibbs
3914568Sgibbs	call	sg_advance
3924568Sgibbs	mov	SCBARRAY+18,SG_COUNT		# residual S/G count
3934568Sgibbs
3944568Sgibbs	jmp	ITloop
3954568Sgibbs
3964568Sgibbs#  Command phase.  Set up the DMA registers and let 'er rip - the
3974568Sgibbs#  two bytes after the SCB SCSI_cmd_length are zeroed by the driver,
3984568Sgibbs#  so we can copy those three bytes directly into HCNT.
3994568Sgibbs#
4004568Sgibbsp_command:
4014568Sgibbs	mvi	0x80		call scsisig	# CDO|!IOO|!MSGO
4024568Sgibbs	call	assert
4034568Sgibbs
4044568Sgibbs	mvi	A,3
4054568Sgibbs	mvi	DINDEX,HCNT
4064568Sgibbs	mvi	SCBARRAY+11	call bcopy
4074568Sgibbs
4084568Sgibbs	mvi	A,3
4094568Sgibbs	mvi	DINDEX,STCNT
4104568Sgibbs	mvi	SCBARRAY+11	call bcopy
4114568Sgibbs
4124568Sgibbs	mvi	A,4
4134568Sgibbs	mvi	DINDEX,HADDR
4144568Sgibbs	mvi	SCBARRAY+7	call bcopy
4154568Sgibbs
4164568Sgibbs	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
4174568Sgibbs						#   DIRECTION|FIFORESET
4184568Sgibbs	jmp	ITloop
4194568Sgibbs
4204568Sgibbs#  Status phase.  Wait for the data byte to appear, then read it
4214568Sgibbs#  and store it into the SCB.
4224568Sgibbs#
4234568Sgibbsp_status:
4244568Sgibbs	mvi	0xc0		call scsisig	# CDO|IOO|!MSGO
4254568Sgibbs
4264568Sgibbs	mvi	SCBARRAY+14	call inb
4274568Sgibbs	jmp	ITloop
4284568Sgibbs
4294568Sgibbs#  Message out phase.  If there is no active message, but the target
4304568Sgibbs#  took us into this phase anyway, build a no-op message and send it.
4314568Sgibbs#
4324568Sgibbsp_mesgout:
4334568Sgibbs	mvi	0xa0		call scsisig	# CDO|!IOO|MSGO
4344568Sgibbs	mvi	0x8		call mk_mesg	# build NOP message
4354568Sgibbs
4364568Sgibbs#  Set up automatic PIO transfer from MSG_START.  Bit 3 in
4374568Sgibbs#  SXFRCTL0 (SPIOEN) is already on.
4384568Sgibbs#
4394568Sgibbs	mvi	SINDEX,MSG_START+0
4404568Sgibbs	mov	DINDEX,MSG_LEN
4414568Sgibbs	clr	A
4424568Sgibbs
4434568Sgibbs#  When target asks for a byte, drop ATN if it's the last one in
4444568Sgibbs#  the message.  Otherwise, keep going until the message is exhausted.
4454568Sgibbs#  (We can't use outb for this since it wants the input in SINDEX.)
4464568Sgibbs#
4474568Sgibbs#  Keep an eye out for a phase change, in case the target issues
4484568Sgibbs#  a MESSAGE REJECT.
4494568Sgibbs#
4504568Sgibbsp_mesgout2:
4514568Sgibbs	test	SSTAT0,0x2	jz p_mesgout2	# SPIORDY
4524568Sgibbs	test	SSTAT1,0x10	jnz p_mesgout6	# PHASEMIS
4534568Sgibbs
4544568Sgibbs	cmp	DINDEX,1	jne p_mesgout3	# last byte?
4554568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO - drop ATN
4564568Sgibbs
4574568Sgibbs#  Write a byte to the SCSI bus.  The AIC-7770 refuses to automatically
4584568Sgibbs#  send ACKs in automatic PIO or DMA mode unless you make sure that the
4594568Sgibbs#  "expected" bus phase in SCSISIGO matches the actual bus phase.  This
4604568Sgibbs#  behaviour is completely undocumented and caused me several days of
4614568Sgibbs#  grief.
4624568Sgibbs#
4634568Sgibbs#  After plugging in different drives to test with and using a longer
4644568Sgibbs#  SCSI cable, I found that I/O in Automatic PIO mode ceased to function,
4654568Sgibbs#  especially when transferring >1 byte.  It seems to be much more stable
4664568Sgibbs#  if STCNT is set to one before the transfer, and SDONE (in SSTAT0) is
4674568Sgibbs#  polled for transfer completion - for both output _and_ input.  The
4684568Sgibbs#  only theory I have is that SPIORDY doesn't drop right away when SCSIDATL
4694568Sgibbs#  is accessed (like the documentation says it does), and that on a longer
4704568Sgibbs#  cable run, the sequencer code was fast enough to loop back and see
4714568Sgibbs#  an SPIORDY that hadn't dropped yet.
4724568Sgibbs#
4734568Sgibbsp_mesgout3:
4744568Sgibbs	call	one_stcnt
4754568Sgibbs	mov	SCSIDATL,SINDIR
4764568Sgibbs
4774568Sgibbsp_mesgout4:
4784568Sgibbs	test	SSTAT0,0x4	jz p_mesgout4	# SDONE
4794568Sgibbs	dec	DINDEX
4804568Sgibbs	inc	A
4814568Sgibbs	cmp	MSG_LEN,A	jne p_mesgout2
4824568Sgibbs
4834568Sgibbs#  If the next bus phase after ATN drops is a message out, it means
4844568Sgibbs#  that the target is requesting that the last message(s) be resent.
4854568Sgibbs#
4864568Sgibbsp_mesgout5:
4874568Sgibbs	test	SSTAT1,0x8	jnz p_mesgout6	# BUSFREE
4884568Sgibbs	test	SSTAT1,0x1	jz p_mesgout5	# REQINIT
4894568Sgibbs
4904568Sgibbs	and	A,0xe0,SCSISIGI			# CDI|IOI|MSGI
4914568Sgibbs	cmp	A,0xa0		jne p_mesgout6
4924568Sgibbs	mvi	0x10		call scsisig	# ATNO - re-assert ATN
4934568Sgibbs
4944568Sgibbs	jmp	ITloop
4954568Sgibbs
4964568Sgibbsp_mesgout6:
4974568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO - in case of PHASEMIS
4984568Sgibbs	clr	MSG_FLAGS			# no active msg
4994568Sgibbs	jmp	ITloop
5004568Sgibbs
5014568Sgibbs#  Message in phase.  Bytes are read using Automatic PIO mode, but not
5024568Sgibbs#  using inb.  This alleviates a race condition, namely that if ATN had
5034568Sgibbs#  to be asserted under Automatic PIO mode, it had to beat the SCSI
5044568Sgibbs#  circuitry sending an ACK to the target.  This showed up under heavy
5054568Sgibbs#  loads and really confused things, since ABORT commands wouldn't be
5064568Sgibbs#  seen by the drive after an IDENTIFY message in until it had changed
5074568Sgibbs#  to a data I/O phase.
5084568Sgibbs#
5094568Sgibbsp_mesgin:
5104568Sgibbs	mvi	0xe0		call scsisig	# CDO|IOO|MSGO
5114568Sgibbs	mvi	A		call inb_first	# read the 1st message byte
5124568Sgibbs	mvi	REJBYTE,A			# save it for the driver
5134568Sgibbs
5144568Sgibbs	cmp	ALLZEROS,A	jne p_mesgin1
5154568Sgibbs
5164568Sgibbs#  We got a "command complete" message, so put the SCB pointer
5174568Sgibbs#  into the Queue Out, and trigger a completion interrupt.
5184568Sgibbs#  Check status for non zero return and interrupt driver if needed
5195326Sgibbs#  This allows the driver to interpret errors only when they occur
5205326Sgibbs#  instead of always uploading the scb.  If the status is SCSI_CHECK,
5215326Sgibbs#  the driver will download a new scb requesting sense, to replace 
5225326Sgibbs#  the old one and the sequencer code will imediately jump to start 
5235326Sgibbs#  working on it.  If the kernel driver does not wish to request sense, 
5245326Sgibbs#  the sequencer program counter is incremented by 1, preventing another run 
5255326Sgibbs#  on the current SCB and the command is allowed to complete.  We don't 
5265326Sgibbs#  bother to post to the QOUTFIFO in the error case since it would require 
5275326Sgibbs#  extra work in the kernel driver to ensure that the entry was removed 
5285326Sgibbs#  before the command complete code tried processing it.
5294568Sgibbs
5304568Sgibbs	test	SCBARRAY+14,0xff	jz status_ok  # 0 Status?
5314568Sgibbs	call	inb_last			# ack & turn auto PIO back on
5324568Sgibbs	mvi	INTSTAT,STATUS_ERROR		# let driver know
5335326Sgibbs	jmp	start_scb
5345326Sgibbs
5354568Sgibbsstatus_ok:
5365326Sgibbs
5375326Sgibbs#  First, mark this target as free.
5385326Sgibbs	and	FUNCTION1,0x70,SCBARRAY+1
5395326Sgibbs	mov	A,FUNCTION1
5405326Sgibbs	test	SCBARRAY+1,0x88 jz clear_a
5415326Sgibbs	xor	ACTIVE_B,A
5425326Sgibbs	jmp	complete
5435326Sgibbs
5445326Sgibbsclear_a:
5455326Sgibbs	xor	ACTIVE_A,A
5465326Sgibbs
5475326Sgibbscomplete:
5484568Sgibbs	mov	QOUTFIFO,SCBPTR
5495326Sgibbs	mvi	INTSTAT,0x02			# CMDCMPLT
5504568Sgibbs	jmp	p_mesgin_done
5514568Sgibbs
5524568Sgibbs#  Is it an extended message?  We only support the synchronous data
5534568Sgibbs#  transfer request message, which will probably be in response to
5544568Sgibbs#  an SDTR message out from us.  If it's not an SDTR, reject it -
5554568Sgibbs#  apparently this can be done after any message in byte, according
5564568Sgibbs#  to the SCSI-2 spec.
5574568Sgibbs#
5584568Sgibbs#  XXX - we should really reject this if we didn't initiate the SDTR
5594568Sgibbs#	 negotiation; this may cause problems with unusual devices.
5604568Sgibbs#
5614568Sgibbsp_mesgin1:
5624568Sgibbs	cmp	A,1		jne p_mesgin2	# extended message code?
5634568Sgibbs	
5644568Sgibbs	mvi	A		call inb_next
5654568Sgibbs	cmp	A,3		jne p_mesginN	# extended mesg length = 3
5664568Sgibbs	mvi	A		call inb_next
5674568Sgibbs	cmp	A,1		jne p_mesginN	# SDTR code
5684568Sgibbs
5694568Sgibbs	mvi	ARG_1		call inb_next	# xfer period
5705326Sgibbs	mvi	A		call inb_next	# REQ/ACK offset
5714568Sgibbs	mvi	INTSTAT,SIGNAL_4		# call driver to convert
5724568Sgibbs
5734568Sgibbs	call	ndx_sdtr			# index sync config for target
5744568Sgibbs	mov	DINDEX,SINDEX
5755326Sgibbs	not	A				# turn off "need sdtr" flag
5765326Sgibbs	test	SBLKCTL,0x08	jnz p_mesgin1_b
5775326Sgibbs	test	SCSIID,0x80	jnz p_mesgin1_b
5785326Sgibbs	and	NEEDSDTR_A,A
5795326Sgibbs	jmp	p_mesgin1_save
5805326Sgibbs
5815326Sgibbsp_mesgin1_b:
5825326Sgibbs	and	NEEDSDTR_B,A
5835326Sgibbs
5845326Sgibbsp_mesgin1_save:
5855326Sgibbs	and	A,0x80,SINDIR			# get the WIDEXFER flag
5865326Sgibbs	or	RETURN_1,A			# Set WIDEXFER if necessary
5874568Sgibbs	mov	DINDIR,RETURN_1			# save returned value
5884568Sgibbs
5894568Sgibbs#  Even though the SCSI-2 specification says that a device responding
5904568Sgibbs#  to our SDTR message should honor our parameters for transmitting
5914568Sgibbs#  to us, it doesn't seem to work too well in real life.  In particular,
5924568Sgibbs#  a lot of CD-ROM and tape units don't function: try using the SDTR
5934568Sgibbs#  parameters the device sent us for both transmitting and receiving.
5944568Sgibbs#
5954568Sgibbs	mov	SCSIRATE,RETURN_1
5964568Sgibbs	jmp	p_mesgin_done
5974568Sgibbs
5984568Sgibbs#  Is it a disconnect message?  Set a flag in the SCB to remind us
5994568Sgibbs#  and await the bus going free.
6004568Sgibbs#
6014568Sgibbsp_mesgin2:
6024568Sgibbs	cmp	A,4		jne p_mesgin3	# disconnect code?
6034568Sgibbs
6044568Sgibbs	or	SCBARRAY+0,0x4			# set "disconnected" bit
6054568Sgibbs	jmp	p_mesgin_done
6064568Sgibbs
6074568Sgibbs#  Save data pointers message?  Copy working values into the SCB,
6084568Sgibbs#  usually in preparation for a disconnect.
6094568Sgibbs#
6104568Sgibbsp_mesgin3:
6114568Sgibbs	cmp	A,2		jne p_mesgin4	# save data pointers code?
6124568Sgibbs
6134568Sgibbs	call	sg_ram2scb
6144568Sgibbs	jmp	p_mesgin_done
6154568Sgibbs
6164568Sgibbs#  Restore pointers message?  Data pointers are recopied from the
6174568Sgibbs#  SCB anyway at the start of any DMA operation, so the only thing
6184568Sgibbs#  to copy is the scatter-gather values.
6194568Sgibbs#
6204568Sgibbsp_mesgin4:
6214568Sgibbs	cmp	A,3		jne p_mesgin5	# restore pointers code?
6224568Sgibbs
6234568Sgibbs	call	sg_scb2ram
6244568Sgibbs	jmp	p_mesgin_done
6254568Sgibbs
6264568Sgibbs#  Identify message?  For a reconnecting target, this tells us the lun
6274568Sgibbs#  that the reconnection is for - find the correct SCB and switch to it,
6284568Sgibbs#  clearing the "disconnected" bit so we don't "find" it by accident later.
6294568Sgibbs#
6304568Sgibbsp_mesgin5:
6314568Sgibbs	test	A,0x80		jz p_mesgin6	# identify message?
6324568Sgibbs
6334568Sgibbs	test	A,0x78		jnz p_mesginN	# !DiscPriv|!LUNTAR|!Reserved
6344568Sgibbs
6354568Sgibbs	mov	A		call findSCB	# switch to correct SCB
6364568Sgibbs
6374568Sgibbs#  If a active message is present after calling findSCB, then either it
6384568Sgibbs#  or the driver is trying to abort the command.  Either way, something
6394568Sgibbs#  untoward has happened and we should just leave it alone.
6404568Sgibbs#
6414568Sgibbs	test	MSG_FLAGS,0x80	jnz p_mesgin_done
6424568Sgibbs
6435326Sgibbs	and	SCBARRAY+0,0xfb			# clear disconnect bit in SCB
6444568Sgibbs	mvi	RESELECT,0xc0			# make note of IDENTIFY
6454568Sgibbs
6464568Sgibbs	call	sg_scb2ram			# implied restore pointers
6474568Sgibbs						#   required on reselect
6484568Sgibbs	jmp	p_mesgin_done
6494568Sgibbs
6504568Sgibbs#  Message reject?  If we have an outstanding SDTR negotiation, assume
6514568Sgibbs#  that it's a response from the target selecting asynchronous transfer,
6524568Sgibbs#  otherwise just ignore it since we have no clue what it pertains to.
6534568Sgibbs#
6544568Sgibbs#  XXX - I don't have a device that responds this way.  Does this code
6554568Sgibbs#	 actually work?
6564568Sgibbs#
6574568Sgibbsp_mesgin6:
6584568Sgibbs	cmp	A,7		jne p_mesgin7	# message reject code?
6594568Sgibbs
6604568Sgibbs	and	FUNCTION1,0x70,SCSIID		# outstanding SDTR message?
6614568Sgibbs	mov	A,FUNCTION1
6625326Sgibbs	
6635326Sgibbs	test	SBLKCTL,0x08	jnz p_mesgin6_b	
6645326Sgibbs	test	SCSIID,0x80	jnz p_mesgin6_b	
6655326Sgibbs	test	NEEDSDTR_A,A	jz p_mesgin_done  # no - ignore rejection
6665326Sgibbs	call	ndx_sdtr			# note use of asynch xfer
6675326Sgibbs	mov	DINDEX,SINDEX
6685326Sgibbs	clr	DINDIR
6694568Sgibbs
6705326Sgibbs	not	A
6715326Sgibbs	and	NEEDSDTR_A,A
6725326Sgibbs	jmp	p_mesgin6_done
6735326Sgibbs
6745326Sgibbsp_mesgin6_b:
6755326Sgibbs	test	NEEDSDTR_B,A	jz p_mesgin_done  # no - ignore rejection
6764568Sgibbs	call	ndx_sdtr			# note use of asynch xfer
6774568Sgibbs	mov	DINDEX,SINDEX
6784568Sgibbs	clr	DINDIR
6794568Sgibbs
6805326Sgibbs	not	A
6815326Sgibbs	and	NEEDSDTR_B,A
6825326Sgibbs	
6835326Sgibbsp_mesgin6_done:
6844568Sgibbs
6854568Sgibbs	clr	SCSIRATE			# select asynch xfer
6864568Sgibbs	jmp	p_mesgin_done
6874568Sgibbs
6884568Sgibbs#  [ ADD MORE MESSAGE HANDLING HERE ]
6894568Sgibbs#
6904568Sgibbsp_mesgin7:
6914568Sgibbs
6924568Sgibbs#  We have no idea what this message in is, and there's no way
6934568Sgibbs#  to pass it up to the kernel, so we issue a message reject and
6944568Sgibbs#  hope for the best.  Since we're now using manual PIO mode to
6954568Sgibbs#  read in the message, there should no longer be a race condition
6964568Sgibbs#  present when we assert ATN.  In any case, rejection should be a
6974568Sgibbs#  rare occurrence - signal the driver when it happens.
6984568Sgibbs#
6994568Sgibbsp_mesginN:
7004568Sgibbs	or	SINDEX,0x10,SIGSTATE		# turn on ATNO
7014568Sgibbs	call	scsisig
7024568Sgibbs	mvi	INTSTAT,SIGNAL_1		# let driver know
7034568Sgibbs
7044568Sgibbs	mvi	0x7		call mk_mesg	# MESSAGE REJECT message
7054568Sgibbs
7064568Sgibbsp_mesgin_done:
7074568Sgibbs	call	inb_last			# ack & turn auto PIO back on
7084568Sgibbs	jmp	ITloop
7094568Sgibbs
7104568Sgibbs#  Bus free phase.  It might be useful to interrupt the device
7114568Sgibbs#  driver if we aren't expecting this.  For now, make sure that
7124568Sgibbs#  ATN isn't being asserted and look for a new command.
7134568Sgibbs#
7144568Sgibbsp_busfree:
7154568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO
7164568Sgibbs	clr	SIGSTATE
7174568Sgibbs	jmp	start
7184568Sgibbs
7194568Sgibbs#  Bcopy: number of bytes to transfer should be in A, DINDEX should
7204568Sgibbs#  contain the destination address, and SINDEX should contain the
7214568Sgibbs#  source address.  All input parameters are trashed on return.
7224568Sgibbs#
7234568Sgibbsbcopy:
7244568Sgibbs	mov	DINDIR,SINDIR
7254568Sgibbs	dec	A
7264568Sgibbs	cmp	ALLZEROS,A	jne bcopy
7274568Sgibbs	ret
7284568Sgibbs
7294568Sgibbs#  Locking the driver out, build a one-byte message passed in SINDEX
7304568Sgibbs#  if there is no active message already.  SINDEX is returned intact.
7314568Sgibbs#
7324568Sgibbsmk_mesg:
7334866Sgibbs	mvi	SEQCTL,0x50			# PAUSEDIS|FASTMODE
7344568Sgibbs	test	MSG_FLAGS,0x80	jnz mk_mesg1	# active message?
7354568Sgibbs
7364568Sgibbs	mvi	MSG_FLAGS,0x80			# if not, there is now
7374568Sgibbs	mvi	MSG_LEN,1			# length = 1
7384568Sgibbs	mov	MSG_START+0,SINDEX		# 1-byte message
7394568Sgibbs
7404568Sgibbsmk_mesg1:
7414866Sgibbs	mvi	SEQCTL,0x10			# !PAUSEDIS|FASTMODE
7424568Sgibbs	ret
7434568Sgibbs
7444568Sgibbs#  Input byte in Automatic PIO mode.  The address to store the byte
7454568Sgibbs#  in should be in SINDEX.  DINDEX will be used by this routine.
7464568Sgibbs#
7474568Sgibbsinb:
7484568Sgibbs	test	SSTAT0,0x2	jz inb		# SPIORDY
7494568Sgibbs	mov	DINDEX,SINDEX
7504568Sgibbs	call	one_stcnt			# xfer one byte
7514568Sgibbs	mov	DINDIR,SCSIDATL
7524568Sgibbsinb1:
7534568Sgibbs	test	SSTAT0,0x4	jz inb1		# SDONE - wait to "finish"
7544568Sgibbs	ret
7554568Sgibbs
7564568Sgibbs#  Carefully read data in Automatic PIO mode.  I first tried this using
7574568Sgibbs#  Manual PIO mode, but it gave me continual underrun errors, probably
7584568Sgibbs#  indicating that I did something wrong, but I feel more secure leaving
7594568Sgibbs#  Automatic PIO on all the time.
7604568Sgibbs#
7614568Sgibbs#  According to Adaptec's documentation, an ACK is not sent on input from
7624568Sgibbs#  the target until SCSIDATL is read from.  So we wait until SCSIDATL is
7634568Sgibbs#  latched (the usual way), then read the data byte directly off the bus
7644568Sgibbs#  using SCSIBUSL.  When we have pulled the ATN line, or we just want to
7654568Sgibbs#  acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
7664568Sgibbs#  spec guarantees that the target will hold the data byte on the bus until
7674568Sgibbs#  we send our ACK.
7684568Sgibbs#
7694568Sgibbs#  The assumption here is that these are called in a particular sequence,
7704568Sgibbs#  and that REQ is already set when inb_first is called.  inb_{first,next}
7714568Sgibbs#  use the same calling convention as inb.
7724568Sgibbs#
7734568Sgibbsinb_first:
7744568Sgibbs	mov	DINDEX,SINDEX
7754568Sgibbs	mov	DINDIR,SCSIBUSL	ret		# read byte directly from bus
7764568Sgibbs
7774568Sgibbsinb_next:
7784568Sgibbs	mov	DINDEX,SINDEX			# save SINDEX
7794568Sgibbs
7804568Sgibbs	call	one_stcnt			# xfer one byte
7814568Sgibbs	mov	NONE,SCSIDATL			# dummy read from latch to ACK
7824568Sgibbsinb_next1:
7834568Sgibbs	test	SSTAT0,0x4	jz inb_next1	# SDONE
7844568Sgibbsinb_next2:
7854568Sgibbs	test	SSTAT0,0x2	jz inb_next2	# SPIORDY - wait for next byte
7864568Sgibbs	mov	DINDIR,SCSIBUSL	ret		# read byte directly from bus
7874568Sgibbs
7884568Sgibbsinb_last:
7894568Sgibbs	call	one_stcnt			# ACK with dummy read
7904568Sgibbs	mov	NONE,SCSIDATL
7914568Sgibbsinb_last1:
7924568Sgibbs	test	SSTAT0,0x4	jz inb_last1	# wait for completion
7934568Sgibbs	ret
7944568Sgibbs
7954568Sgibbs#  Output byte in Automatic PIO mode.  The byte to output should be
7964568Sgibbs#  in SINDEX.  If DROPATN's high bit is set, then ATN will be dropped
7974568Sgibbs#  before the byte is output.
7984568Sgibbs#
7994568Sgibbsoutb:
8004568Sgibbs	test	SSTAT0,0x2	jz outb		# SPIORDY
8014568Sgibbs	call	one_stcnt			# xfer one byte
8024568Sgibbs
8034568Sgibbs	test	DROPATN,0x80	jz outb1
8044568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO
8054568Sgibbs	clr	DROPATN
8064568Sgibbsoutb1:
8074568Sgibbs	mov	SCSIDATL,SINDEX
8084568Sgibbsoutb2:
8094568Sgibbs	test	SSTAT0,0x4	jz outb2	# SDONE
8104568Sgibbs	ret
8114568Sgibbs
8124568Sgibbs#  Write the value "1" into the STCNT registers, for Automatic PIO
8134568Sgibbs#  transfers.
8144568Sgibbs#
8154568Sgibbsone_stcnt:
8164568Sgibbs	clr	STCNT+2
8174568Sgibbs	clr	STCNT+1
8184568Sgibbs	mvi	STCNT+0,1	ret
8194568Sgibbs
8204568Sgibbs#  DMA data transfer.  HADDR and HCNT must be loaded first, and
8214568Sgibbs#  SINDEX should contain the value to load DFCNTRL with - 0x3d for
8224568Sgibbs#  host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
8234568Sgibbs#  during initialization.
8244568Sgibbs#
8254568Sgibbsdma:
8264568Sgibbs	mov	DFCNTRL,SINDEX
8274568Sgibbsdma1:
8284568Sgibbsdma2:
8294568Sgibbs	test	SSTAT0,0x1	jnz dma3	# DMADONE
8304568Sgibbs	test	SSTAT1,0x10	jz dma1		# PHASEMIS, ie. underrun
8314568Sgibbs
8324568Sgibbs#  We will be "done" DMAing when the transfer count goes to zero, or
8334568Sgibbs#  the target changes the phase (in light of this, it makes sense that
8344568Sgibbs#  the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
8354866Sgibbs#  doing a SCSI->Host transfer, the data FIFO should be flushed auto-
8364866Sgibbs#  magically on STCNT=0 or a phase change, so just wait for FIFO empty
8374866Sgibbs#  status.
8384568Sgibbs#
8394568Sgibbsdma3:
8404568Sgibbs	test	SINDEX,0x4	jnz dma5	# DIRECTION
8414568Sgibbsdma4:
8425326Sgibbs	test	DFSTATUS,0x1	jz dma4		# FIFOEMP
8434568Sgibbs
8444568Sgibbs#  Now shut the DMA enables off, and copy STCNT (ie. the underrun
8454568Sgibbs#  amount, if any) to the SCB registers; SG_COUNT will get copied to
8464568Sgibbs#  the SCB's residual S/G count field after sg_advance is called.  Make
8474568Sgibbs#  sure that the DMA enables are actually off first lest we get an ILLSADDR.
8484568Sgibbs#
8494568Sgibbsdma5:
8504568Sgibbs	clr	DFCNTRL				# disable DMA
8514568Sgibbsdma6:
8524568Sgibbs	test	DFCNTRL,0x38	jnz dma6	# SCSIENACK|SDMAENACK|HDMAENACK
8534568Sgibbs
8544568Sgibbs	mvi	A,3
8554568Sgibbs	mvi	DINDEX,SCBARRAY+15
8564568Sgibbs	mvi	STCNT		call bcopy
8574568Sgibbs
8584568Sgibbs	ret
8594568Sgibbs
8604568Sgibbs#  Common SCSI initialization for selection and reselection.  Expects
8614568Sgibbs#  the target SCSI ID to be in the upper four bits of SINDEX, and A's
8624568Sgibbs#  contents are stomped on return.
8634568Sgibbs#
8644568Sgibbsinitialize:
8654568Sgibbs	and	SCSIID,0xf0,SINDEX		# target ID
8665326Sgibbs	test	SBLKCTL,0x08	jnz initialize_b
8675326Sgibbs	mvi	SINDEX,SCSICONF_A
8685326Sgibbs	test	FLAGS,WIDE_BUS	jnz initialize_wide
8695326Sgibbs	and	A,0x7,SCSICONF_A		# SCSI_ID_A[210]
8705326Sgibbs	jmp	initialize_2
8715326Sgibbsinitialize_b:
8725326Sgibbs	and	A,0x7,SCSICONF_B		# SCSI_ID_B[210]
8735326Sgibbs	mvi	SCSICONF_B	 jmp	initialize_2
8745326Sgibbs
8755326Sgibbsinitialize_wide:
8765326Sgibbs	and	A, 0xf,SCSICONF_B
8775326Sgibbs
8785326Sgibbsinitialize_2:
8794568Sgibbs	or	SCSIID,A
8804568Sgibbs
8814568Sgibbs#  Esundry initialization.
8824568Sgibbs#
8834568Sgibbs	clr	DROPATN
8844568Sgibbs	clr	SIGSTATE
8854568Sgibbs
8864568Sgibbs#  Turn on Automatic PIO mode now, before we expect to see an REQ
8874568Sgibbs#  from the target.  It shouldn't hurt anything to leave it on.  Set
8884568Sgibbs#  CLRCHN here before the target has entered a data transfer mode -
8894568Sgibbs#  with synchronous SCSI, if you do it later, you blow away some
8904568Sgibbs#  data in the SCSI FIFO that the target has already sent to you.
8914568Sgibbs#
8924866Sgibbs#  DFON is a 7870 bit enabling digital filtering of REQ and ACK signals.
8934866Sgibbs#
8944866Sgibbs	mvi	SXFRCTL0,0x8a			# DFON|SPIOEN|CLRCHN
8954568Sgibbs
8964568Sgibbs#  Set SCSI bus parity checking and the selection timeout value,
8974568Sgibbs#  and enable the hardware selection timer.  Set the SELTO interrupt
8984568Sgibbs#  to signal the driver.
8994568Sgibbs#
9004866Sgibbs#  STPWEN is 7870-specific, enabling an external termination power source.
9014866Sgibbs#
9025326Sgibbs	and	A,0x38,SINDIR			# PARITY_ENB|SEL_TIM[10]
9035326Sgibbs	or	SXFRCTL1,0x7,A			# ENSTIMER|ACTNEGEN|STPWEN
9044568Sgibbs	mvi	SIMODE1,0x84			# ENSELTIMO|ENSCSIPERR
9054568Sgibbs	
9064568Sgibbs#  Initialize scatter-gather pointers by setting up the working copy
9074568Sgibbs#  in scratch RAM.
9084568Sgibbs#
9094568Sgibbs	call	sg_scb2ram
9104568Sgibbs
9114568Sgibbs#  Initialize SCSIRATE with the appropriate value for this target.
9124568Sgibbs#
9134568Sgibbs	call	ndx_sdtr
9144568Sgibbs	mov	SCSIRATE,SINDIR
9154568Sgibbs	ret
9164568Sgibbs
9174568Sgibbs#  Assert that if we've been reselected, then we've seen an IDENTIFY
9184568Sgibbs#  message.
9194568Sgibbs#
9204568Sgibbsassert:
9214568Sgibbs	test	RESELECT,0x80	jz assert1	# reselected?
9224568Sgibbs	test	RESELECT,0x40	jnz assert1	# seen IDENTIFY?
9234568Sgibbs
9244568Sgibbs	mvi	INTSTAT,SIGNAL_2		# no - cause a kernel panic
9254568Sgibbs
9264568Sgibbsassert1:
9274568Sgibbs	ret
9284568Sgibbs
9294568Sgibbs#  Find out if disconnection is ok from the information the BIOS has left
9305326Sgibbs#  us.  The tcl from SCBARRAY+1 should be in SINDEX; A will
9314866Sgibbs#  contain either 0x40 (disconnection ok) or 0x00 (disconnection not ok)
9324568Sgibbs#  on exit.
9334568Sgibbs#
9345326Sgibbs#  To allow for wide or twin busses, we check the upper bit of the target ID
9355326Sgibbs#  and the channel ID and look at the appropriate disconnect register. 
9364568Sgibbs#
9374568Sgibbsdisconnect:
9384568Sgibbs	and	FUNCTION1,0x70,SINDEX		# strip off extra just in case
9394568Sgibbs	mov	A,FUNCTION1
9405326Sgibbs	test	SINDEX, 0x88	jz disconnect_a
9415326Sgibbs
9425326Sgibbs	test	DISC_DSB_B,A	jz disconnect1	# bit nonzero if DISabled
9435326Sgibbs	clr	A		ret
9445326Sgibbs
9455326Sgibbsdisconnect_a:
9464568Sgibbs	test	DISC_DSB_A,A	jz disconnect1	# bit nonzero if DISabled
9475326Sgibbs	clr	A		ret
9484568Sgibbs
9494568Sgibbsdisconnect1:
9504568Sgibbs	mvi	A,0x40		ret
9514568Sgibbs
9524568Sgibbs#  Locate the SCB matching the target ID in SELID and the lun in the lower
9534568Sgibbs#  three bits of SINDEX, and switch the SCB to it.  Have the kernel print
9544568Sgibbs#  a warning message if it can't be found, and generate an ABORT message
9554866Sgibbs#  to the target.  We keep the value of the t/c/l that we are trying to find
9564568Sgibbs#  in DINDEX so it is not overwritten during our check to see if we are
9574568Sgibbs#  at the last SCB.
9584568Sgibbs#
9594568SgibbsfindSCB:
9604568Sgibbs	and	A,0x7,SINDEX		# lun in lower three bits
9615326Sgibbs	or	DINDEX,A,SELID		
9625326Sgibbs	and	DINDEX,0xf7
9635326Sgibbs	and	A,0x08,SBLKCTL		# B Channel??
9645326Sgibbs	or	DINDEX,A
9654568Sgibbs	clr	SINDEX
9664568Sgibbs
9674568SgibbsfindSCB1:
9684866Sgibbs	mov	A,DINDEX
9694568Sgibbs	mov	SCBPTR,SINDEX			# switch to new SCB
9704568Sgibbs	cmp	SCBARRAY+1,A	jne findSCB2	# target ID/channel/lun match?
9714568Sgibbs	test	SCBARRAY+0,0x4	jz findSCB2	# should be disconnected
9724568Sgibbs	ret
9734568Sgibbs
9744568SgibbsfindSCB2:
9754568Sgibbs	inc	SINDEX
9764568Sgibbs	mov	A,SCBCOUNT
9774568Sgibbs	cmp	SINDEX,A	jne findSCB1
9784568Sgibbs
9794568Sgibbs	mvi	INTSTAT,SIGNAL_3		# not found - signal kernel
9804568Sgibbs	mvi	0x6		call mk_mesg	# ABORT message
9814568Sgibbs
9824568Sgibbs	or	SINDEX,0x10,SIGSTATE		# assert ATNO
9834568Sgibbs	call	scsisig
9844568Sgibbs	ret
9854568Sgibbs
9864568Sgibbs#  Make a working copy of the scatter-gather parameters in the SCB.
9874568Sgibbs#
9884568Sgibbssg_scb2ram:
9894568Sgibbs	mov	SG_COUNT,SCBARRAY+2
9904568Sgibbs
9914568Sgibbs	mvi	A,4
9924568Sgibbs	mvi	DINDEX,SG_NEXT
9934568Sgibbs	mvi	SCBARRAY+3	call bcopy
9944568Sgibbs
9954568Sgibbs	mvi	SG_NOLOAD,0x80
9964568Sgibbs	test	SCBARRAY+0,0x10	jnz sg_scb2ram1	# don't reload s/g?
9974568Sgibbs	clr	SG_NOLOAD
9984568Sgibbs
9994568Sgibbssg_scb2ram1:
10004568Sgibbs	ret
10014568Sgibbs
10024568Sgibbs#  Copying RAM values back to SCB, for Save Data Pointers message.
10034568Sgibbs#
10044568Sgibbssg_ram2scb:
10054568Sgibbs	mov	SCBARRAY+2,SG_COUNT
10064568Sgibbs
10074568Sgibbs	mvi	A,4
10084568Sgibbs	mvi	DINDEX,SCBARRAY+3
10094568Sgibbs	mvi	SG_NEXT		call bcopy
10104568Sgibbs
10114568Sgibbs	and	SCBARRAY+0,0xef,SCBARRAY+0
10124568Sgibbs	test	SG_NOLOAD,0x80	jz sg_ram2scb1	# reload s/g?
10134568Sgibbs	or	SCBARRAY+0,0x10
10144568Sgibbs
10154568Sgibbssg_ram2scb1:
10164568Sgibbs	ret
10174568Sgibbs
10184568Sgibbs#  Load a struct scatter if needed and set up the data address and
10194568Sgibbs#  length.  If the working value of the SG count is nonzero, then
10204568Sgibbs#  we need to load a new set of values.
10214568Sgibbs#
10224568Sgibbs#  This, like the above DMA, assumes a little-endian host data storage.
10234568Sgibbs#
10244568Sgibbssg_load:
10254568Sgibbs	test	SG_COUNT,0xff	jz sg_load3	# SG being used?
10264568Sgibbs	test	SG_NOLOAD,0x80	jnz sg_load3	# don't reload s/g?
10274568Sgibbs
10284568Sgibbs	clr	HCNT+2
10294568Sgibbs	clr	HCNT+1
10304568Sgibbs	mvi	HCNT+0,SG_SIZEOF
10314568Sgibbs
10324568Sgibbs	mvi	A,4
10334568Sgibbs	mvi	DINDEX,HADDR
10344568Sgibbs	mvi	SG_NEXT		call bcopy
10354568Sgibbs
10364568Sgibbs	mvi	DFCNTRL,0xd			# HDMAEN|DIRECTION|FIFORESET
10374568Sgibbs
10384568Sgibbs#  Wait for DMA from host memory to data FIFO to complete, then disable
10394568Sgibbs#  DMA and wait for it to acknowledge that it's off.
10404568Sgibbs#
10414568Sgibbssg_load1:
10424568Sgibbs	test	DFSTATUS,0x8	jz sg_load1	# HDONE
10434568Sgibbs
10444568Sgibbs	clr	DFCNTRL				# disable DMA
10454568Sgibbssg_load2:
10464568Sgibbs	test	DFCNTRL,0x8	jnz sg_load2	# HDMAENACK
10474568Sgibbs
10484568Sgibbs#  Copy data from FIFO into SCB data pointer and data count.  This assumes
10494568Sgibbs#  that the struct scatterlist has this structure (this and sizeof(struct
10504568Sgibbs#  scatterlist) == 12 are asserted in aic7xxx.c):
10514568Sgibbs#
10524568Sgibbs#	struct scatterlist {
10534568Sgibbs#		char *address;		/* four bytes, little-endian order */
10544568Sgibbs#		...			/* four bytes, ignored */
10554568Sgibbs#		unsigned short length;	/* two bytes, little-endian order */
10564568Sgibbs#	}
10574568Sgibbs#
10584568Sgibbs
10594568Sgibbs# Not in FreeBSD.  the scatter list is only 8 bytes.
10604568Sgibbs# 
10614568Sgibbs# struct ahc_dma_seg {
10624568Sgibbs#       physaddr addr;                  /* four bytes, little-endian order */
10634568Sgibbs#       long    len;                    /* four bytes, little endian order */   
10644568Sgibbs# };
10654568Sgibbs#
10664568Sgibbs
10674568Sgibbs	mov	SCBARRAY+19,DFDAT		# new data address
10684568Sgibbs	mov	SCBARRAY+20,DFDAT
10694568Sgibbs	mov	SCBARRAY+21,DFDAT
10704568Sgibbs	mov	SCBARRAY+22,DFDAT
10714568Sgibbs
10724568Sgibbs	mov	SCBARRAY+23,DFDAT
10734568Sgibbs	mov	SCBARRAY+24,DFDAT
10744568Sgibbs	mov	SCBARRAY+25,DFDAT
10754568Sgibbs	mov	NONE,DFDAT			#Only support 24 bit length.
10764568Sgibbs
10774568Sgibbssg_load3:
10784568Sgibbs	ret
10794568Sgibbs
10804568Sgibbs#  Advance the scatter-gather pointers only IF NEEDED.  If SG is enabled,
10814568Sgibbs#  and the SCSI transfer count is zero (note that this should be called
10824568Sgibbs#  right after a DMA finishes), then move the working copies of the SG
10834568Sgibbs#  pointer/length along.  If the SCSI transfer count is not zero, then
10844568Sgibbs#  presumably the target is disconnecting - do not reload the SG values
10854568Sgibbs#  next time.
10864568Sgibbs#
10874568Sgibbssg_advance:
10884568Sgibbs	test	SG_COUNT,0xff	jz sg_advance2	# s/g enabled?
10894568Sgibbs
10904568Sgibbs	test	STCNT+0,0xff	jnz sg_advance1	# SCSI transfer count nonzero?
10914568Sgibbs	test	STCNT+1,0xff	jnz sg_advance1
10924568Sgibbs	test	STCNT+2,0xff	jnz sg_advance1
10934568Sgibbs
10944568Sgibbs	clr	SG_NOLOAD			# reload s/g next time
10954568Sgibbs	dec	SG_COUNT			# one less segment to go
10964568Sgibbs
10974568Sgibbs	clr	A				# add sizeof(struct scatter)
10984568Sgibbs	add	SG_NEXT+0,SG_SIZEOF,SG_NEXT+0
10994568Sgibbs	adc	SG_NEXT+1,A,SG_NEXT+1
11004568Sgibbs	adc	SG_NEXT+2,A,SG_NEXT+2
11014568Sgibbs	adc	SG_NEXT+3,A,SG_NEXT+3
11024568Sgibbs
11034568Sgibbs	ret
11044568Sgibbs
11054568Sgibbssg_advance1:
11064568Sgibbs	mvi	SG_NOLOAD,0x80			# don't reload s/g next time
11074568Sgibbssg_advance2:
11084568Sgibbs	ret
11094568Sgibbs
11104568Sgibbs#  Add the array base SYNCNEG to the target offset (the target address
11114568Sgibbs#  is in SCSIID), and return the result in SINDEX.  The accumulator
11124568Sgibbs#  contains the 3->8 decoding of the target ID on return.
11134568Sgibbs#
11144568Sgibbsndx_sdtr:
11154568Sgibbs	shr	A,SCSIID,4
11165326Sgibbs	test	SBLKCTL,0x08	jz ndx_sdtr_2
11175326Sgibbs	or	A,0x08		# Channel B entries add 8
11185326Sgibbsndx_sdtr_2:
11194568Sgibbs	add	SINDEX,SYNCNEG,A
11204568Sgibbs
11214568Sgibbs	and	FUNCTION1,0x70,SCSIID		# 3-bit target address decode
11224568Sgibbs	mov	A,FUNCTION1	ret
11234568Sgibbs
11244568Sgibbs#  If we need to negotiate transfer parameters, build the SDTR message
11254568Sgibbs#  starting at the address passed in SINDEX.  DINDEX is modified on return.
11264568Sgibbs#
11274568Sgibbsmk_sdtr:
11284568Sgibbs	mov	DINDEX,SINDEX			# save SINDEX
11294568Sgibbs
11304568Sgibbs	call	ndx_sdtr
11315326Sgibbs	test	SCBARRAY+1,0x88	jz  mk_sdtr1_a
11325326Sgibbs	test	NEEDSDTR_B,A	jnz mk_sdtr1	# do we need negotiation?
11334568Sgibbs	ret
11345326Sgibbsmk_sdtr1_a:
11355326Sgibbs	test	NEEDSDTR_A,A	jnz mk_sdtr1	# do we need negotiation?
11365326Sgibbs	ret
11374568Sgibbs
11384568Sgibbsmk_sdtr1:
11394568Sgibbs	mvi	DINDIR,1			# extended message
11404568Sgibbs	mvi	DINDIR,3			# extended message length = 3
11414568Sgibbs	mvi	DINDIR,1			# SDTR code
11424568Sgibbs	mvi	DINDIR,25			# REQ/ACK transfer period
11434568Sgibbs	mvi	DINDIR,15			# REQ/ACK offset
11444568Sgibbs
11454568Sgibbs	add	MSG_LEN,-MSG_START+0,DINDEX	# update message length
11464568Sgibbs	ret
11474568Sgibbs
11484568Sgibbs#  Set SCSI bus control signal state.  This also saves the last-written
11494568Sgibbs#  value into a location where the higher-level driver can read it - if
11504568Sgibbs#  it has to send an ABORT or RESET message, then it needs to know this
11514568Sgibbs#  so it can assert ATN without upsetting SCSISIGO.  The new value is
11524568Sgibbs#  expected in SINDEX.  Change the actual state last to avoid contention
11534568Sgibbs#  from the driver.
11544568Sgibbs#
11554568Sgibbsscsisig:
11564568Sgibbs	mov	SIGSTATE,SINDEX
11574568Sgibbs	mov	SCSISIGO,SINDEX	ret
1158