aic7xxx.seq revision 4568
14568Sgibbs# @(#)aic7xxx.seq 1.30 94/11/09 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
204568SgibbsVERSION AIC7XXX_SEQ_VERSION 1.30
214568Sgibbs
224568SgibbsMAXSCB-1	= 0xf
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
674568SgibbsSCSICONF	= 0x5a
684568Sgibbs
694568Sgibbs#  The two reserved bytes at SCBARRAY+1[23] are expected to be set to
704568Sgibbs#  zero, and the reserved bit in SCBARRAY+0 is used as an internal flag
714568Sgibbs#  to indicate whether or not to reload scatter-gather parameters after
724568Sgibbs#  a disconnect.
734568Sgibbs#
744568SgibbsSCBARRAY+0	= 0xa0
754568SgibbsSCBARRAY+1	= 0xa1
764568SgibbsSCBARRAY+2	= 0xa2
774568SgibbsSCBARRAY+3	= 0xa3
784568SgibbsSCBARRAY+7	= 0xa7
794568SgibbsSCBARRAY+11	= 0xab
804568SgibbsSCBARRAY+14	= 0xae
814568SgibbsSCBARRAY+15	= 0xaf
824568SgibbsSCBARRAY+16	= 0xb0
834568SgibbsSCBARRAY+17	= 0xb1
844568SgibbsSCBARRAY+18	= 0xb2
854568SgibbsSCBARRAY+19	= 0xb3
864568SgibbsSCBARRAY+20	= 0xb4
874568SgibbsSCBARRAY+21	= 0xb5
884568SgibbsSCBARRAY+22	= 0xb6
894568SgibbsSCBARRAY+23	= 0xb7
904568SgibbsSCBARRAY+24	= 0xb8
914568SgibbsSCBARRAY+25	= 0xb9
924568Sgibbs
934568SgibbsSIGNAL_0	= 0x01				# unknown scsi bus phase
944568SgibbsSIGNAL_1	= 0x11				# message reject
954568SgibbsSIGNAL_2	= 0x21				# no IDENTIFY after reconnect
964568SgibbsSIGNAL_3	= 0x31				# no cmd match for reconnect
974568SgibbsSIGNAL_4	= 0x41				# SDTR -> SCSIRATE conversion
984568SgibbsSTATUS_ERROR    = 0x51
994568Sgibbs
1004568Sgibbs#  The host adapter card (at least the BIOS) uses 20-2f for SCSI
1014568Sgibbs#  device information, 32-33 and 5a-5f as well.  Since we don't support
1024568Sgibbs#  wide or twin-bus SCSI, 28-2f can be reclaimed.  As it turns out, the
1034568Sgibbs#  BIOS trashes 20-27 anyway, 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
1064568Sgibbs#  SCSIRATE).  This implies, since we can't get the BIOS config values,
1074568Sgibbs#  that all targets will be negotiated with for synchronous transfer.
1084568Sgibbs#  NEEDSDTR has one bit per target indicating if an SDTR message is
1094568Sgibbs#  needed for that device - this will be set initially, as well as
1104568Sgibbs#  after a bus reset condition.
1114568Sgibbs#
1124568Sgibbs#  The high bit of DROPATN is set if ATN should be dropped before the ACK
1134568Sgibbs#  when outb is called.  REJBYTE contains the first byte of a MESSAGE IN
1144568Sgibbs#  message, so the driver can report an intelligible error if a message is
1154568Sgibbs#  rejected.
1164568Sgibbs#
1174568Sgibbs#  RESELECT's high bit is true if we are currently handling a reselect;
1184568Sgibbs#  its next-highest bit is true ONLY IF we've seen an IDENTIFY message
1194568Sgibbs#  from the reselecting target.  If we haven't had IDENTIFY, then we have
1204568Sgibbs#  no idea what the lun is, and we can't select the right SCB register
1214568Sgibbs#  bank, so force a kernel panic if the target attempts a data in/out or
1224568Sgibbs#  command phase instead of corrupting something.
1234568Sgibbs#
1244568Sgibbs#  Note that SG_NEXT occupies four bytes.
1254568Sgibbs#
1264568SgibbsSYNCNEG		= 0x20
1274568SgibbsDISC_DSB_A	= 0x32
1284568Sgibbs
1294568SgibbsDROPATN		= 0x30
1304568SgibbsREJBYTE		= 0x31
1314568SgibbsRESELECT	= 0x34
1324568Sgibbs
1334568SgibbsMSG_FLAGS	= 0x35
1344568SgibbsMSG_LEN		= 0x36
1354568SgibbsMSG_START+0	= 0x37
1364568SgibbsMSG_START+1	= 0x38
1374568SgibbsMSG_START+2	= 0x39
1384568SgibbsMSG_START+3	= 0x3a
1394568SgibbsMSG_START+4	= 0x3b
1404568SgibbsMSG_START+5	= 0x3c
1414568Sgibbs-MSG_START+0	= 0xc9				# 2's complement of MSG_START+0
1424568Sgibbs
1434568SgibbsARG_1		= 0x4c				# sdtr conversion args & return
1444568SgibbsARG_2		= 0x4d
1454568SgibbsRETURN_1	= 0x4c
1464568Sgibbs
1474568SgibbsSIGSTATE	= 0x4e				# value written to SCSISIGO
1484568SgibbsNEEDSDTR	= 0x4f				# send SDTR message, 1 bit/trgt
1494568Sgibbs
1504568SgibbsSG_SIZEOF	= 0x8 				# sizeof(struct scatterlist)
1514568SgibbsSG_NOLOAD	= 0x50				# load SG pointer/length?
1524568SgibbsSG_COUNT	= 0x51				# working value of SG count
1534568SgibbsSG_NEXT		= 0x52				# working value of SG pointer
1544568SgibbsSG_NEXT+0	= 0x52
1554568SgibbsSG_NEXT+1	= 0x53
1564568SgibbsSG_NEXT+2	= 0x54
1574568SgibbsSG_NEXT+3	= 0x55
1584568Sgibbs
1594568SgibbsSCBCOUNT	= 0x56				# the actual number of SCBs
1604568SgibbsACTIVE_A	= 0x57
1614568Sgibbs
1624568Sgibbs#  Poll QINCNT for work - the lower three bits contain
1634568Sgibbs#  the number of entries in the Queue In FIFO.
1644568Sgibbs#
1654568Sgibbsstart:
1664568Sgibbs	test	SCSISIGI,0x4	jnz reselect	# BSYI
1674568Sgibbs	test	QINCNT,MAXSCB-1	jz start
1684568Sgibbs
1694568Sgibbs#  We have at least one queued SCB now.  Set the SCB pointer
1704568Sgibbs#  from the FIFO so we see the right bank of SCB registers,
1714568Sgibbs#  then set SCSI options and set the initiator and target
1724568Sgibbs#  SCSI IDs.
1734568Sgibbs#
1744568Sgibbs	mov	SCBPTR,QINFIFO
1754568Sgibbs
1764568Sgibbs# See if there is not already an active SCB for this target.  This code
1774568Sgibbs# will have to be modified when we add support for dual and wide busses.
1784568Sgibbs
1794568Sgibbs	and	FUNCTION1,0x70,SCBARRAY+1
1804568Sgibbs	mov	A,FUNCTION1
1814568Sgibbs	test	ACTIVE_A,A      jz active  
1824568Sgibbs# Place the currently active back on the queue for later processing
1834568Sgibbs	mov	QINFIFO, SCBPTR
1844568Sgibbs	jmp	start
1854568Sgibbs
1864568Sgibbs# Mark the current target as busy and get working on the SCB
1874568Sgibbsactive:
1884568Sgibbs	or	ACTIVE_A,A
1894568Sgibbs	mov	SCBARRAY+1	call initialize
1904568Sgibbs	clr	SG_NOLOAD
1914568Sgibbs	clr	RESELECT
1924568Sgibbs
1934568Sgibbs#  As soon as we get a successful selection, the target should go
1944568Sgibbs#  into the message out phase since we have ATN asserted.  Prepare
1954568Sgibbs#  the message to send, locking out the device driver.  If the device
1964568Sgibbs#  driver hasn't beaten us with an ABORT or RESET message, then tack
1974568Sgibbs#  on a SDTR negotation if required.
1984568Sgibbs#
1994568Sgibbs#  Messages are stored in scratch RAM starting with a flag byte (high bit
2004568Sgibbs#  set means active message), one length byte, and then the message itself.
2014568Sgibbs#
2024568Sgibbs	mov	SCBARRAY+1	call disconnect	# disconnect ok?
2034568Sgibbs
2044568Sgibbs	and	SINDEX,0x7,SCBARRAY+1		# lun
2054568Sgibbs	or	SINDEX,A			# return value from disconnect
2064568Sgibbs	or	SINDEX,0x80	call mk_mesg	# IDENTIFY message
2074568Sgibbs
2084568Sgibbs	mov	A,SINDEX
2094568Sgibbs	cmp	MSG_START+0,A	jne !message	# did driver beat us?
2104568Sgibbs	mvi	MSG_START+1	call mk_sdtr	# build SDTR message if needed
2114568Sgibbs
2124568Sgibbs!message:
2134568Sgibbs
2144568Sgibbs#  Enable selection phase as an initiator, and do automatic ATN
2154568Sgibbs#  after the selection.
2164568Sgibbs#
2174568Sgibbs	mvi	SCSISEQ,0x48			# ENSELO|ENAUTOATNO
2184568Sgibbs
2194568Sgibbs#  Wait for successful arbitration.  The AIC-7770 documentation says
2204568Sgibbs#  that SELINGO indicates successful arbitration, and that it should
2214568Sgibbs#  be used to look for SELDO.  However, if the sequencer is paused at
2224568Sgibbs#  just the right time - a parallel fsck(8) on two drives did it for
2234568Sgibbs#  me - then SELINGO can flip back to false before we've seen it.  This
2244568Sgibbs#  makes the sequencer sit in the arbitration loop forever.  This is
2254568Sgibbs#  Not Good.
2264568Sgibbs#
2274568Sgibbs#  Therefore, I've added a check in the arbitration loop for SELDO
2284568Sgibbs#  too.  This could arguably be made a critical section by disabling
2294568Sgibbs#  pauses, but I don't want to make a potentially infinite loop a CS.
2304568Sgibbs#  I suppose you could fold it into the select loop, too, but since
2314568Sgibbs#  I've been hunting this bug for four days it's kinda like a trophy.
2324568Sgibbs#
2334568Sgibbsarbitrate:
2344568Sgibbs	test	SSTAT0,0x40	jnz *select	# SELDO
2354568Sgibbs	test	SSTAT0,0x10	jz arbitrate	# SELINGO
2364568Sgibbs
2374568Sgibbs#  Wait for a successful selection.  If the hardware selection
2384568Sgibbs#  timer goes off, then the driver gets the interrupt, so we don't
2394568Sgibbs#  need to worry about it.
2404568Sgibbs#
2414568Sgibbsselect:
2424568Sgibbs	test	SSTAT0,0x40	jz select	# SELDO
2434568Sgibbs	jmp	*select
2444568Sgibbs
2454568Sgibbs#  Reselection is being initiated by a target - we've seen the BSY
2464568Sgibbs#  line driven active, and we didn't do it!  Enable the reselection
2474568Sgibbs#  hardware, and wait for it to finish.  Make a note that we've been
2484568Sgibbs#  reselected, but haven't seen an IDENTIFY message from the target
2494568Sgibbs#  yet.
2504568Sgibbs#
2514568Sgibbsreselect:
2524568Sgibbs	mvi	SCSISEQ,0x10			# ENRSELI
2534568Sgibbs
2544568Sgibbsreselect1:
2554568Sgibbs	test	SSTAT0,0x20	jz reselect1	# SELDI
2564568Sgibbs	mov	SELID		call initialize
2574568Sgibbs
2584568Sgibbs	mvi	RESELECT,0x80			# reselected, no IDENTIFY
2594568Sgibbs
2604568Sgibbs#  After the [re]selection, make sure that the [re]selection enable
2614568Sgibbs#  bit is off.  This chip is flaky enough without extra things
2624568Sgibbs#  turned on.  Also clear the BUSFREE bit in SSTAT1 since we'll be
2634568Sgibbs#  using it shortly.
2644568Sgibbs#
2654568Sgibbs*select:
2664568Sgibbs	clr	SCSISEQ
2674568Sgibbs	mvi	CLRSINT1,0x8			# CLRBUSFREE
2684568Sgibbs
2694568Sgibbs#  Main loop for information transfer phases.  If BSY is false, then
2704568Sgibbs#  we have a bus free condition, expected or not.  Otherwise, wait
2714568Sgibbs#  for the target to assert REQ before checking MSG, C/D and I/O
2724568Sgibbs#  for the bus phase.
2734568Sgibbs#
2744568Sgibbs#  We can't simply look at the values of SCSISIGI here (if we want
2754568Sgibbs#  to do synchronous data transfer), because the target won't assert
2764568Sgibbs#  REQ if it's already sent us some data that we haven't acknowledged
2774568Sgibbs#  yet.
2784568Sgibbs#
2794568SgibbsITloop:
2804568Sgibbs	test	SSTAT1,0x8	jnz p_busfree	# BUSFREE
2814568Sgibbs	test	SSTAT1,0x1	jz ITloop	# REQINIT
2824568Sgibbs
2834568Sgibbs	and	A,0xe0,SCSISIGI			# CDI|IOI|MSGI
2844568Sgibbs
2854568Sgibbs	cmp	ALLZEROS,A	je p_dataout
2864568Sgibbs	cmp	A,0x40		je p_datain
2874568Sgibbs	cmp	A,0x80		je p_command
2884568Sgibbs	cmp	A,0xc0		je p_status
2894568Sgibbs	cmp	A,0xa0		je p_mesgout
2904568Sgibbs	cmp	A,0xe0		je p_mesgin
2914568Sgibbs
2924568Sgibbs	mvi	INTSTAT,SIGNAL_0		# unknown - signal driver
2934568Sgibbs
2944568Sgibbsp_dataout:
2954568Sgibbs	mvi	0		call scsisig	# !CDO|!IOO|!MSGO
2964568Sgibbs	call	assert
2974568Sgibbs	call	sg_load
2984568Sgibbs
2994568Sgibbs	mvi	A,3
3004568Sgibbs	mvi	DINDEX,HCNT
3014568Sgibbs	mvi	SCBARRAY+23	call bcopy
3024568Sgibbs
3034568Sgibbs	mvi	A,3
3044568Sgibbs	mvi	DINDEX,STCNT
3054568Sgibbs	mvi	SCBARRAY+23	call bcopy
3064568Sgibbs
3074568Sgibbs	mvi	A,4
3084568Sgibbs	mvi	DINDEX,HADDR
3094568Sgibbs	mvi	SCBARRAY+19	call bcopy
3104568Sgibbs
3114568Sgibbs	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
3124568Sgibbs						#   DIRECTION|FIFORESET
3134568Sgibbs
3144568Sgibbs#  After a DMA finishes, save the final transfer pointer and count
3154568Sgibbs#  back into the SCB, in case a device disconnects in the middle of
3164568Sgibbs#  a transfer.  Use SHADDR and STCNT instead of HADDR and HCNT, since
3174568Sgibbs#  it's a reflection of how many bytes were transferred on the SCSI
3184568Sgibbs#  (as opposed to the host) bus.
3194568Sgibbs#
3204568Sgibbs	mvi	A,3
3214568Sgibbs	mvi	DINDEX,SCBARRAY+23
3224568Sgibbs	mvi	STCNT		call bcopy
3234568Sgibbs
3244568Sgibbs	mvi	A,4
3254568Sgibbs	mvi	DINDEX,SCBARRAY+19
3264568Sgibbs	mvi	SHADDR		call bcopy
3274568Sgibbs
3284568Sgibbs	call	sg_advance
3294568Sgibbs	mov	SCBARRAY+18,SG_COUNT		# residual S/G count
3304568Sgibbs
3314568Sgibbs	jmp	ITloop
3324568Sgibbs
3334568Sgibbsp_datain:
3344568Sgibbs	mvi	0x40		call scsisig	# !CDO|IOO|!MSGO
3354568Sgibbs	call	assert
3364568Sgibbs	call	sg_load
3374568Sgibbs
3384568Sgibbs	mvi	A,3
3394568Sgibbs	mvi	DINDEX,HCNT
3404568Sgibbs	mvi	SCBARRAY+23	call bcopy
3414568Sgibbs
3424568Sgibbs	mvi	A,3
3434568Sgibbs	mvi	DINDEX,STCNT
3444568Sgibbs	mvi	SCBARRAY+23	call bcopy
3454568Sgibbs
3464568Sgibbs	mvi	A,4
3474568Sgibbs	mvi	DINDEX,HADDR
3484568Sgibbs	mvi	SCBARRAY+19	call bcopy
3494568Sgibbs
3504568Sgibbs	mvi	0x39		call dma	# SCSIEN|SDMAEN|HDMAEN|
3514568Sgibbs						#   !DIRECTION|FIFORESET
3524568Sgibbs	mvi	A,3
3534568Sgibbs	mvi	DINDEX,SCBARRAY+23
3544568Sgibbs	mvi	STCNT		call bcopy
3554568Sgibbs
3564568Sgibbs	mvi	A,4
3574568Sgibbs	mvi	DINDEX,SCBARRAY+19
3584568Sgibbs	mvi	SHADDR		call bcopy
3594568Sgibbs
3604568Sgibbs	call	sg_advance
3614568Sgibbs	mov	SCBARRAY+18,SG_COUNT		# residual S/G count
3624568Sgibbs
3634568Sgibbs	jmp	ITloop
3644568Sgibbs
3654568Sgibbs#  Command phase.  Set up the DMA registers and let 'er rip - the
3664568Sgibbs#  two bytes after the SCB SCSI_cmd_length are zeroed by the driver,
3674568Sgibbs#  so we can copy those three bytes directly into HCNT.
3684568Sgibbs#
3694568Sgibbsp_command:
3704568Sgibbs	mvi	0x80		call scsisig	# CDO|!IOO|!MSGO
3714568Sgibbs	call	assert
3724568Sgibbs
3734568Sgibbs	mvi	A,3
3744568Sgibbs	mvi	DINDEX,HCNT
3754568Sgibbs	mvi	SCBARRAY+11	call bcopy
3764568Sgibbs
3774568Sgibbs	mvi	A,3
3784568Sgibbs	mvi	DINDEX,STCNT
3794568Sgibbs	mvi	SCBARRAY+11	call bcopy
3804568Sgibbs
3814568Sgibbs	mvi	A,4
3824568Sgibbs	mvi	DINDEX,HADDR
3834568Sgibbs	mvi	SCBARRAY+7	call bcopy
3844568Sgibbs
3854568Sgibbs	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
3864568Sgibbs						#   DIRECTION|FIFORESET
3874568Sgibbs	jmp	ITloop
3884568Sgibbs
3894568Sgibbs#  Status phase.  Wait for the data byte to appear, then read it
3904568Sgibbs#  and store it into the SCB.
3914568Sgibbs#
3924568Sgibbsp_status:
3934568Sgibbs	mvi	0xc0		call scsisig	# CDO|IOO|!MSGO
3944568Sgibbs
3954568Sgibbs	mvi	SCBARRAY+14	call inb
3964568Sgibbs	jmp	ITloop
3974568Sgibbs
3984568Sgibbs#  Message out phase.  If there is no active message, but the target
3994568Sgibbs#  took us into this phase anyway, build a no-op message and send it.
4004568Sgibbs#
4014568Sgibbsp_mesgout:
4024568Sgibbs	mvi	0xa0		call scsisig	# CDO|!IOO|MSGO
4034568Sgibbs	mvi	0x8		call mk_mesg	# build NOP message
4044568Sgibbs
4054568Sgibbs#  Set up automatic PIO transfer from MSG_START.  Bit 3 in
4064568Sgibbs#  SXFRCTL0 (SPIOEN) is already on.
4074568Sgibbs#
4084568Sgibbs	mvi	SINDEX,MSG_START+0
4094568Sgibbs	mov	DINDEX,MSG_LEN
4104568Sgibbs	clr	A
4114568Sgibbs
4124568Sgibbs#  When target asks for a byte, drop ATN if it's the last one in
4134568Sgibbs#  the message.  Otherwise, keep going until the message is exhausted.
4144568Sgibbs#  (We can't use outb for this since it wants the input in SINDEX.)
4154568Sgibbs#
4164568Sgibbs#  Keep an eye out for a phase change, in case the target issues
4174568Sgibbs#  a MESSAGE REJECT.
4184568Sgibbs#
4194568Sgibbsp_mesgout2:
4204568Sgibbs	test	SSTAT0,0x2	jz p_mesgout2	# SPIORDY
4214568Sgibbs	test	SSTAT1,0x10	jnz p_mesgout6	# PHASEMIS
4224568Sgibbs
4234568Sgibbs	cmp	DINDEX,1	jne p_mesgout3	# last byte?
4244568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO - drop ATN
4254568Sgibbs
4264568Sgibbs#  Write a byte to the SCSI bus.  The AIC-7770 refuses to automatically
4274568Sgibbs#  send ACKs in automatic PIO or DMA mode unless you make sure that the
4284568Sgibbs#  "expected" bus phase in SCSISIGO matches the actual bus phase.  This
4294568Sgibbs#  behaviour is completely undocumented and caused me several days of
4304568Sgibbs#  grief.
4314568Sgibbs#
4324568Sgibbs#  After plugging in different drives to test with and using a longer
4334568Sgibbs#  SCSI cable, I found that I/O in Automatic PIO mode ceased to function,
4344568Sgibbs#  especially when transferring >1 byte.  It seems to be much more stable
4354568Sgibbs#  if STCNT is set to one before the transfer, and SDONE (in SSTAT0) is
4364568Sgibbs#  polled for transfer completion - for both output _and_ input.  The
4374568Sgibbs#  only theory I have is that SPIORDY doesn't drop right away when SCSIDATL
4384568Sgibbs#  is accessed (like the documentation says it does), and that on a longer
4394568Sgibbs#  cable run, the sequencer code was fast enough to loop back and see
4404568Sgibbs#  an SPIORDY that hadn't dropped yet.
4414568Sgibbs#
4424568Sgibbsp_mesgout3:
4434568Sgibbs	call	one_stcnt
4444568Sgibbs	mov	SCSIDATL,SINDIR
4454568Sgibbs
4464568Sgibbsp_mesgout4:
4474568Sgibbs	test	SSTAT0,0x4	jz p_mesgout4	# SDONE
4484568Sgibbs	dec	DINDEX
4494568Sgibbs	inc	A
4504568Sgibbs	cmp	MSG_LEN,A	jne p_mesgout2
4514568Sgibbs
4524568Sgibbs#  If the next bus phase after ATN drops is a message out, it means
4534568Sgibbs#  that the target is requesting that the last message(s) be resent.
4544568Sgibbs#
4554568Sgibbsp_mesgout5:
4564568Sgibbs	test	SSTAT1,0x8	jnz p_mesgout6	# BUSFREE
4574568Sgibbs	test	SSTAT1,0x1	jz p_mesgout5	# REQINIT
4584568Sgibbs
4594568Sgibbs	and	A,0xe0,SCSISIGI			# CDI|IOI|MSGI
4604568Sgibbs	cmp	A,0xa0		jne p_mesgout6
4614568Sgibbs	mvi	0x10		call scsisig	# ATNO - re-assert ATN
4624568Sgibbs
4634568Sgibbs	jmp	ITloop
4644568Sgibbs
4654568Sgibbsp_mesgout6:
4664568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO - in case of PHASEMIS
4674568Sgibbs	clr	MSG_FLAGS			# no active msg
4684568Sgibbs	jmp	ITloop
4694568Sgibbs
4704568Sgibbs#  Message in phase.  Bytes are read using Automatic PIO mode, but not
4714568Sgibbs#  using inb.  This alleviates a race condition, namely that if ATN had
4724568Sgibbs#  to be asserted under Automatic PIO mode, it had to beat the SCSI
4734568Sgibbs#  circuitry sending an ACK to the target.  This showed up under heavy
4744568Sgibbs#  loads and really confused things, since ABORT commands wouldn't be
4754568Sgibbs#  seen by the drive after an IDENTIFY message in until it had changed
4764568Sgibbs#  to a data I/O phase.
4774568Sgibbs#
4784568Sgibbsp_mesgin:
4794568Sgibbs	mvi	0xe0		call scsisig	# CDO|IOO|MSGO
4804568Sgibbs	mvi	A		call inb_first	# read the 1st message byte
4814568Sgibbs	mvi	REJBYTE,A			# save it for the driver
4824568Sgibbs
4834568Sgibbs	cmp	ALLZEROS,A	jne p_mesgin1
4844568Sgibbs
4854568Sgibbs#  We got a "command complete" message, so put the SCB pointer
4864568Sgibbs#  into the Queue Out, and trigger a completion interrupt.
4874568Sgibbs#  Check status for non zero return and interrupt driver if needed
4884568Sgibbs#  This allows the driver to do a sense command to find out the
4894568Sgibbs#  source of error.  We don't bother to post to the QOUTFIFO in
4904568Sgibbs#  the error case since it would require extra work in the kernel
4914568Sgibbs#  driver to ensure that the entry was removed before the command
4924568Sgibbs#  complete code tried processing it.
4934568Sgibbs
4944568Sgibbs#  First, mark this target as free.
4954568Sgibbs	and     FUNCTION1,0x70,SCBARRAY+1
4964568Sgibbs	mov	A,FUNCTION1
4974568Sgibbs	xor     ACTIVE_A,A 
4984568Sgibbs
4994568Sgibbs	test	SCBARRAY+14,0xff	jz status_ok  # 0 Status?
5004568Sgibbs	call	inb_last			# ack & turn auto PIO back on
5014568Sgibbs	mvi	INTSTAT,STATUS_ERROR		# let driver know
5024568Sgibbs	jmp	ITloop
5034568Sgibbsstatus_ok:
5044568Sgibbs	mov	QOUTFIFO,SCBPTR
5054568Sgibbs	mvi	INTSTAT,0x2			# CMDCMPLT
5064568Sgibbs	jmp	p_mesgin_done
5074568Sgibbs
5084568Sgibbs#  Is it an extended message?  We only support the synchronous data
5094568Sgibbs#  transfer request message, which will probably be in response to
5104568Sgibbs#  an SDTR message out from us.  If it's not an SDTR, reject it -
5114568Sgibbs#  apparently this can be done after any message in byte, according
5124568Sgibbs#  to the SCSI-2 spec.
5134568Sgibbs#
5144568Sgibbs#  XXX - we should really reject this if we didn't initiate the SDTR
5154568Sgibbs#	 negotiation; this may cause problems with unusual devices.
5164568Sgibbs#
5174568Sgibbsp_mesgin1:
5184568Sgibbs	cmp	A,1		jne p_mesgin2	# extended message code?
5194568Sgibbs	
5204568Sgibbs	mvi	A		call inb_next
5214568Sgibbs	cmp	A,3		jne p_mesginN	# extended mesg length = 3
5224568Sgibbs	mvi	A		call inb_next
5234568Sgibbs	cmp	A,1		jne p_mesginN	# SDTR code
5244568Sgibbs
5254568Sgibbs	mvi	ARG_1		call inb_next	# xfer period
5264568Sgibbs	mvi	ARG_2		call inb_next	# REQ/ACK offset
5274568Sgibbs	mvi	INTSTAT,SIGNAL_4		# call driver to convert
5284568Sgibbs
5294568Sgibbs	call	ndx_sdtr			# index sync config for target
5304568Sgibbs	mov	DINDEX,SINDEX
5314568Sgibbs	mov	DINDIR,RETURN_1			# save returned value
5324568Sgibbs
5334568Sgibbs	not	A				# turn off "need sdtr" flag
5344568Sgibbs	and	NEEDSDTR,A
5354568Sgibbs
5364568Sgibbs#  Even though the SCSI-2 specification says that a device responding
5374568Sgibbs#  to our SDTR message should honor our parameters for transmitting
5384568Sgibbs#  to us, it doesn't seem to work too well in real life.  In particular,
5394568Sgibbs#  a lot of CD-ROM and tape units don't function: try using the SDTR
5404568Sgibbs#  parameters the device sent us for both transmitting and receiving.
5414568Sgibbs#
5424568Sgibbs	mov	SCSIRATE,RETURN_1
5434568Sgibbs	jmp	p_mesgin_done
5444568Sgibbs
5454568Sgibbs#  Is it a disconnect message?  Set a flag in the SCB to remind us
5464568Sgibbs#  and await the bus going free.
5474568Sgibbs#
5484568Sgibbsp_mesgin2:
5494568Sgibbs	cmp	A,4		jne p_mesgin3	# disconnect code?
5504568Sgibbs
5514568Sgibbs	or	SCBARRAY+0,0x4			# set "disconnected" bit
5524568Sgibbs	jmp	p_mesgin_done
5534568Sgibbs
5544568Sgibbs#  Save data pointers message?  Copy working values into the SCB,
5554568Sgibbs#  usually in preparation for a disconnect.
5564568Sgibbs#
5574568Sgibbsp_mesgin3:
5584568Sgibbs	cmp	A,2		jne p_mesgin4	# save data pointers code?
5594568Sgibbs
5604568Sgibbs	call	sg_ram2scb
5614568Sgibbs	jmp	p_mesgin_done
5624568Sgibbs
5634568Sgibbs#  Restore pointers message?  Data pointers are recopied from the
5644568Sgibbs#  SCB anyway at the start of any DMA operation, so the only thing
5654568Sgibbs#  to copy is the scatter-gather values.
5664568Sgibbs#
5674568Sgibbsp_mesgin4:
5684568Sgibbs	cmp	A,3		jne p_mesgin5	# restore pointers code?
5694568Sgibbs
5704568Sgibbs	call	sg_scb2ram
5714568Sgibbs	jmp	p_mesgin_done
5724568Sgibbs
5734568Sgibbs#  Identify message?  For a reconnecting target, this tells us the lun
5744568Sgibbs#  that the reconnection is for - find the correct SCB and switch to it,
5754568Sgibbs#  clearing the "disconnected" bit so we don't "find" it by accident later.
5764568Sgibbs#
5774568Sgibbsp_mesgin5:
5784568Sgibbs	test	A,0x80		jz p_mesgin6	# identify message?
5794568Sgibbs
5804568Sgibbs	test	A,0x78		jnz p_mesginN	# !DiscPriv|!LUNTAR|!Reserved
5814568Sgibbs
5824568Sgibbs	mov	A		call findSCB	# switch to correct SCB
5834568Sgibbs
5844568Sgibbs#  If a active message is present after calling findSCB, then either it
5854568Sgibbs#  or the driver is trying to abort the command.  Either way, something
5864568Sgibbs#  untoward has happened and we should just leave it alone.
5874568Sgibbs#
5884568Sgibbs	test	MSG_FLAGS,0x80	jnz p_mesgin_done
5894568Sgibbs
5904568Sgibbs	xor	SCBARRAY+0,0x4			# clear disconnect bit in SCB
5914568Sgibbs	mvi	RESELECT,0xc0			# make note of IDENTIFY
5924568Sgibbs
5934568Sgibbs	call	sg_scb2ram			# implied restore pointers
5944568Sgibbs						#   required on reselect
5954568Sgibbs	jmp	p_mesgin_done
5964568Sgibbs
5974568Sgibbs#  Message reject?  If we have an outstanding SDTR negotiation, assume
5984568Sgibbs#  that it's a response from the target selecting asynchronous transfer,
5994568Sgibbs#  otherwise just ignore it since we have no clue what it pertains to.
6004568Sgibbs#
6014568Sgibbs#  XXX - I don't have a device that responds this way.  Does this code
6024568Sgibbs#	 actually work?
6034568Sgibbs#
6044568Sgibbsp_mesgin6:
6054568Sgibbs	cmp	A,7		jne p_mesgin7	# message reject code?
6064568Sgibbs
6074568Sgibbs	and	FUNCTION1,0x70,SCSIID		# outstanding SDTR message?
6084568Sgibbs	mov	A,FUNCTION1
6094568Sgibbs	test	NEEDSDTR,A	jz p_mesgin_done  # no - ignore rejection
6104568Sgibbs
6114568Sgibbs	call	ndx_sdtr			# note use of asynch xfer
6124568Sgibbs	mov	DINDEX,SINDEX
6134568Sgibbs	clr	DINDIR
6144568Sgibbs
6154568Sgibbs	not	A				# turn off "active sdtr" flag
6164568Sgibbs	and	NEEDSDTR,A
6174568Sgibbs
6184568Sgibbs	clr	SCSIRATE			# select asynch xfer
6194568Sgibbs	jmp	p_mesgin_done
6204568Sgibbs
6214568Sgibbs#  [ ADD MORE MESSAGE HANDLING HERE ]
6224568Sgibbs#
6234568Sgibbsp_mesgin7:
6244568Sgibbs
6254568Sgibbs#  We have no idea what this message in is, and there's no way
6264568Sgibbs#  to pass it up to the kernel, so we issue a message reject and
6274568Sgibbs#  hope for the best.  Since we're now using manual PIO mode to
6284568Sgibbs#  read in the message, there should no longer be a race condition
6294568Sgibbs#  present when we assert ATN.  In any case, rejection should be a
6304568Sgibbs#  rare occurrence - signal the driver when it happens.
6314568Sgibbs#
6324568Sgibbsp_mesginN:
6334568Sgibbs	or	SINDEX,0x10,SIGSTATE		# turn on ATNO
6344568Sgibbs	call	scsisig
6354568Sgibbs	mvi	INTSTAT,SIGNAL_1		# let driver know
6364568Sgibbs
6374568Sgibbs	mvi	0x7		call mk_mesg	# MESSAGE REJECT message
6384568Sgibbs
6394568Sgibbsp_mesgin_done:
6404568Sgibbs	call	inb_last			# ack & turn auto PIO back on
6414568Sgibbs	jmp	ITloop
6424568Sgibbs
6434568Sgibbs#  Bus free phase.  It might be useful to interrupt the device
6444568Sgibbs#  driver if we aren't expecting this.  For now, make sure that
6454568Sgibbs#  ATN isn't being asserted and look for a new command.
6464568Sgibbs#
6474568Sgibbsp_busfree:
6484568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO
6494568Sgibbs	clr	SIGSTATE
6504568Sgibbs	jmp	start
6514568Sgibbs
6524568Sgibbs#  Bcopy: number of bytes to transfer should be in A, DINDEX should
6534568Sgibbs#  contain the destination address, and SINDEX should contain the
6544568Sgibbs#  source address.  All input parameters are trashed on return.
6554568Sgibbs#
6564568Sgibbsbcopy:
6574568Sgibbs	mov	DINDIR,SINDIR
6584568Sgibbs	dec	A
6594568Sgibbs	cmp	ALLZEROS,A	jne bcopy
6604568Sgibbs	ret
6614568Sgibbs
6624568Sgibbs#  Locking the driver out, build a one-byte message passed in SINDEX
6634568Sgibbs#  if there is no active message already.  SINDEX is returned intact.
6644568Sgibbs#
6654568Sgibbsmk_mesg:
6664568Sgibbs	mvi	SEQCTL,0x40			# PAUSEDIS
6674568Sgibbs	test	MSG_FLAGS,0x80	jnz mk_mesg1	# active message?
6684568Sgibbs
6694568Sgibbs	mvi	MSG_FLAGS,0x80			# if not, there is now
6704568Sgibbs	mvi	MSG_LEN,1			# length = 1
6714568Sgibbs	mov	MSG_START+0,SINDEX		# 1-byte message
6724568Sgibbs
6734568Sgibbsmk_mesg1:
6744568Sgibbs	clr	SEQCTL				# !PAUSEDIS
6754568Sgibbs	ret
6764568Sgibbs
6774568Sgibbs#  Input byte in Automatic PIO mode.  The address to store the byte
6784568Sgibbs#  in should be in SINDEX.  DINDEX will be used by this routine.
6794568Sgibbs#
6804568Sgibbsinb:
6814568Sgibbs	test	SSTAT0,0x2	jz inb		# SPIORDY
6824568Sgibbs	mov	DINDEX,SINDEX
6834568Sgibbs	call	one_stcnt			# xfer one byte
6844568Sgibbs	mov	DINDIR,SCSIDATL
6854568Sgibbsinb1:
6864568Sgibbs	test	SSTAT0,0x4	jz inb1		# SDONE - wait to "finish"
6874568Sgibbs	ret
6884568Sgibbs
6894568Sgibbs#  Carefully read data in Automatic PIO mode.  I first tried this using
6904568Sgibbs#  Manual PIO mode, but it gave me continual underrun errors, probably
6914568Sgibbs#  indicating that I did something wrong, but I feel more secure leaving
6924568Sgibbs#  Automatic PIO on all the time.
6934568Sgibbs#
6944568Sgibbs#  According to Adaptec's documentation, an ACK is not sent on input from
6954568Sgibbs#  the target until SCSIDATL is read from.  So we wait until SCSIDATL is
6964568Sgibbs#  latched (the usual way), then read the data byte directly off the bus
6974568Sgibbs#  using SCSIBUSL.  When we have pulled the ATN line, or we just want to
6984568Sgibbs#  acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
6994568Sgibbs#  spec guarantees that the target will hold the data byte on the bus until
7004568Sgibbs#  we send our ACK.
7014568Sgibbs#
7024568Sgibbs#  The assumption here is that these are called in a particular sequence,
7034568Sgibbs#  and that REQ is already set when inb_first is called.  inb_{first,next}
7044568Sgibbs#  use the same calling convention as inb.
7054568Sgibbs#
7064568Sgibbsinb_first:
7074568Sgibbs	mov	DINDEX,SINDEX
7084568Sgibbs	mov	DINDIR,SCSIBUSL	ret		# read byte directly from bus
7094568Sgibbs
7104568Sgibbsinb_next:
7114568Sgibbs	mov	DINDEX,SINDEX			# save SINDEX
7124568Sgibbs
7134568Sgibbs	call	one_stcnt			# xfer one byte
7144568Sgibbs	mov	NONE,SCSIDATL			# dummy read from latch to ACK
7154568Sgibbsinb_next1:
7164568Sgibbs	test	SSTAT0,0x4	jz inb_next1	# SDONE
7174568Sgibbsinb_next2:
7184568Sgibbs	test	SSTAT0,0x2	jz inb_next2	# SPIORDY - wait for next byte
7194568Sgibbs	mov	DINDIR,SCSIBUSL	ret		# read byte directly from bus
7204568Sgibbs
7214568Sgibbsinb_last:
7224568Sgibbs	call	one_stcnt			# ACK with dummy read
7234568Sgibbs	mov	NONE,SCSIDATL
7244568Sgibbsinb_last1:
7254568Sgibbs	test	SSTAT0,0x4	jz inb_last1	# wait for completion
7264568Sgibbs	ret
7274568Sgibbs
7284568Sgibbs#  Output byte in Automatic PIO mode.  The byte to output should be
7294568Sgibbs#  in SINDEX.  If DROPATN's high bit is set, then ATN will be dropped
7304568Sgibbs#  before the byte is output.
7314568Sgibbs#
7324568Sgibbsoutb:
7334568Sgibbs	test	SSTAT0,0x2	jz outb		# SPIORDY
7344568Sgibbs	call	one_stcnt			# xfer one byte
7354568Sgibbs
7364568Sgibbs	test	DROPATN,0x80	jz outb1
7374568Sgibbs	mvi	CLRSINT1,0x40			# CLRATNO
7384568Sgibbs	clr	DROPATN
7394568Sgibbsoutb1:
7404568Sgibbs	mov	SCSIDATL,SINDEX
7414568Sgibbsoutb2:
7424568Sgibbs	test	SSTAT0,0x4	jz outb2	# SDONE
7434568Sgibbs	ret
7444568Sgibbs
7454568Sgibbs#  Write the value "1" into the STCNT registers, for Automatic PIO
7464568Sgibbs#  transfers.
7474568Sgibbs#
7484568Sgibbsone_stcnt:
7494568Sgibbs	clr	STCNT+2
7504568Sgibbs	clr	STCNT+1
7514568Sgibbs	mvi	STCNT+0,1	ret
7524568Sgibbs
7534568Sgibbs#  DMA data transfer.  HADDR and HCNT must be loaded first, and
7544568Sgibbs#  SINDEX should contain the value to load DFCNTRL with - 0x3d for
7554568Sgibbs#  host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
7564568Sgibbs#  during initialization.
7574568Sgibbs#
7584568Sgibbsdma:
7594568Sgibbs	mov	DFCNTRL,SINDEX
7604568Sgibbsdma1:
7614568Sgibbsdma2:
7624568Sgibbs	test	SSTAT0,0x1	jnz dma3	# DMADONE
7634568Sgibbs	test	SSTAT1,0x10	jz dma1		# PHASEMIS, ie. underrun
7644568Sgibbs
7654568Sgibbs#  We will be "done" DMAing when the transfer count goes to zero, or
7664568Sgibbs#  the target changes the phase (in light of this, it makes sense that
7674568Sgibbs#  the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
7684568Sgibbs#  doing a SCSI->Host transfer, flush the data FIFO.
7694568Sgibbs#
7704568Sgibbsdma3:
7714568Sgibbs	test	SINDEX,0x4	jnz dma5	# DIRECTION
7724568Sgibbs	and	SINDEX,0xfe			# mask out FIFORESET
7734568Sgibbs	or	DFCNTRL,0x2,SINDEX		# FIFOFLUSH
7744568Sgibbsdma4:
7754568Sgibbs	test	DFCNTRL,0x2	jnz dma4	# FIFOFLUSHACK
7764568Sgibbs
7774568Sgibbs#  Now shut the DMA enables off, and copy STCNT (ie. the underrun
7784568Sgibbs#  amount, if any) to the SCB registers; SG_COUNT will get copied to
7794568Sgibbs#  the SCB's residual S/G count field after sg_advance is called.  Make
7804568Sgibbs#  sure that the DMA enables are actually off first lest we get an ILLSADDR.
7814568Sgibbs#
7824568Sgibbsdma5:
7834568Sgibbs	clr	DFCNTRL				# disable DMA
7844568Sgibbsdma6:
7854568Sgibbs	test	DFCNTRL,0x38	jnz dma6	# SCSIENACK|SDMAENACK|HDMAENACK
7864568Sgibbs
7874568Sgibbs	mvi	A,3
7884568Sgibbs	mvi	DINDEX,SCBARRAY+15
7894568Sgibbs	mvi	STCNT		call bcopy
7904568Sgibbs
7914568Sgibbs	ret
7924568Sgibbs
7934568Sgibbs#  Common SCSI initialization for selection and reselection.  Expects
7944568Sgibbs#  the target SCSI ID to be in the upper four bits of SINDEX, and A's
7954568Sgibbs#  contents are stomped on return.
7964568Sgibbs#
7974568Sgibbsinitialize:
7984568Sgibbs	clr	SBLKCTL				# channel A, !wide
7994568Sgibbs	and	SCSIID,0xf0,SINDEX		# target ID
8004568Sgibbs	and	A,0x7,SCSICONF			# SCSI_ID_A[210]
8014568Sgibbs	or	SCSIID,A
8024568Sgibbs
8034568Sgibbs#  Esundry initialization.
8044568Sgibbs#
8054568Sgibbs	clr	DROPATN
8064568Sgibbs	clr	SIGSTATE
8074568Sgibbs
8084568Sgibbs#  Turn on Automatic PIO mode now, before we expect to see an REQ
8094568Sgibbs#  from the target.  It shouldn't hurt anything to leave it on.  Set
8104568Sgibbs#  CLRCHN here before the target has entered a data transfer mode -
8114568Sgibbs#  with synchronous SCSI, if you do it later, you blow away some
8124568Sgibbs#  data in the SCSI FIFO that the target has already sent to you.
8134568Sgibbs#
8144568Sgibbs	mvi	SXFRCTL0,0xa			# SPIOEN|CLRCHN
8154568Sgibbs
8164568Sgibbs#  Set SCSI bus parity checking and the selection timeout value,
8174568Sgibbs#  and enable the hardware selection timer.  Set the SELTO interrupt
8184568Sgibbs#  to signal the driver.
8194568Sgibbs#
8204568Sgibbs	and	A,0x38,SCSICONF			# PARITY_ENB_A|SEL_TIM_A[10]
8214568Sgibbs	or	SXFRCTL1,0x4,A			# ENSTIMER
8224568Sgibbs	mvi	SIMODE1,0x84			# ENSELTIMO|ENSCSIPERR
8234568Sgibbs	
8244568Sgibbs#  Initialize scatter-gather pointers by setting up the working copy
8254568Sgibbs#  in scratch RAM.
8264568Sgibbs#
8274568Sgibbs	call	sg_scb2ram
8284568Sgibbs
8294568Sgibbs#  Initialize SCSIRATE with the appropriate value for this target.
8304568Sgibbs#
8314568Sgibbs	call	ndx_sdtr
8324568Sgibbs	mov	SCSIRATE,SINDIR
8334568Sgibbs	ret
8344568Sgibbs
8354568Sgibbs#  Assert that if we've been reselected, then we've seen an IDENTIFY
8364568Sgibbs#  message.
8374568Sgibbs#
8384568Sgibbsassert:
8394568Sgibbs	test	RESELECT,0x80	jz assert1	# reselected?
8404568Sgibbs	test	RESELECT,0x40	jnz assert1	# seen IDENTIFY?
8414568Sgibbs
8424568Sgibbs	mvi	INTSTAT,SIGNAL_2		# no - cause a kernel panic
8434568Sgibbs
8444568Sgibbsassert1:
8454568Sgibbs	ret
8464568Sgibbs
8474568Sgibbs#  Find out if disconnection is ok from the information the BIOS has left
8484568Sgibbs#  us.  The target ID should be in the upper four bits of SINDEX; A will
8494568Sgibbs#  contain either 0x40 (disconnection ok) or 0x00 (diconnection not ok)
8504568Sgibbs#  on exit.
8514568Sgibbs#
8524568Sgibbs#  This is the only place the target ID is limited to three bits, so we
8534568Sgibbs#  can use the FUNCTION1 register.
8544568Sgibbs#
8554568Sgibbsdisconnect:
8564568Sgibbs	and	FUNCTION1,0x70,SINDEX		# strip off extra just in case
8574568Sgibbs	mov	A,FUNCTION1
8584568Sgibbs	test	DISC_DSB_A,A	jz disconnect1	# bit nonzero if DISabled
8594568Sgibbs
8604568Sgibbs	clr	A		ret
8614568Sgibbsdisconnect1:
8624568Sgibbs	mvi	A,0x40		ret
8634568Sgibbs
8644568Sgibbs#  Locate the SCB matching the target ID in SELID and the lun in the lower
8654568Sgibbs#  three bits of SINDEX, and switch the SCB to it.  Have the kernel print
8664568Sgibbs#  a warning message if it can't be found, and generate an ABORT message
8674568Sgibbs#  to the target.  We keep the value of the t/c/l that we are trying to 
8684568Sgibbs#  in DINDEX so it is not overwritten during our check to see if we are
8694568Sgibbs#  at the last SCB.
8704568Sgibbs#
8714568SgibbsfindSCB:
8724568Sgibbs	and	A,0x7,SINDEX		# lun in lower three bits
8734568Sgibbs	or	DINDEX,A,SELID		# can I do this?
8744568Sgibbs	and	DINDEX,0xf7		# only channel A implemented
8754568Sgibbs
8764568Sgibbs	clr	SINDEX
8774568Sgibbs
8784568SgibbsfindSCB1:
8794568Sgibbs	mov	SCBPTR,SINDEX			# switch to new SCB
8804568Sgibbs	mov	A,DINDEX
8814568Sgibbs	cmp	SCBARRAY+1,A	jne findSCB2	# target ID/channel/lun match?
8824568Sgibbs	test	SCBARRAY+0,0x4	jz findSCB2	# should be disconnected
8834568Sgibbs
8844568Sgibbs	ret
8854568Sgibbs
8864568SgibbsfindSCB2:
8874568Sgibbs	inc	SINDEX
8884568Sgibbs	mov	A,SCBCOUNT
8894568Sgibbs	cmp	SINDEX,A	jne findSCB1
8904568Sgibbs
8914568Sgibbs	mvi	INTSTAT,SIGNAL_3		# not found - signal kernel
8924568Sgibbs	mvi	0x6		call mk_mesg	# ABORT message
8934568Sgibbs
8944568Sgibbs	or	SINDEX,0x10,SIGSTATE		# assert ATNO
8954568Sgibbs	call	scsisig
8964568Sgibbs	ret
8974568Sgibbs
8984568Sgibbs#  Make a working copy of the scatter-gather parameters in the SCB.
8994568Sgibbs#
9004568Sgibbssg_scb2ram:
9014568Sgibbs	mov	SG_COUNT,SCBARRAY+2
9024568Sgibbs
9034568Sgibbs	mvi	A,4
9044568Sgibbs	mvi	DINDEX,SG_NEXT
9054568Sgibbs	mvi	SCBARRAY+3	call bcopy
9064568Sgibbs
9074568Sgibbs	mvi	SG_NOLOAD,0x80
9084568Sgibbs	test	SCBARRAY+0,0x10	jnz sg_scb2ram1	# don't reload s/g?
9094568Sgibbs	clr	SG_NOLOAD
9104568Sgibbs
9114568Sgibbssg_scb2ram1:
9124568Sgibbs	ret
9134568Sgibbs
9144568Sgibbs#  Copying RAM values back to SCB, for Save Data Pointers message.
9154568Sgibbs#
9164568Sgibbssg_ram2scb:
9174568Sgibbs	mov	SCBARRAY+2,SG_COUNT
9184568Sgibbs
9194568Sgibbs	mvi	A,4
9204568Sgibbs	mvi	DINDEX,SCBARRAY+3
9214568Sgibbs	mvi	SG_NEXT		call bcopy
9224568Sgibbs
9234568Sgibbs	and	SCBARRAY+0,0xef,SCBARRAY+0
9244568Sgibbs	test	SG_NOLOAD,0x80	jz sg_ram2scb1	# reload s/g?
9254568Sgibbs	or	SCBARRAY+0,0x10
9264568Sgibbs
9274568Sgibbssg_ram2scb1:
9284568Sgibbs	ret
9294568Sgibbs
9304568Sgibbs#  Load a struct scatter if needed and set up the data address and
9314568Sgibbs#  length.  If the working value of the SG count is nonzero, then
9324568Sgibbs#  we need to load a new set of values.
9334568Sgibbs#
9344568Sgibbs#  This, like the above DMA, assumes a little-endian host data storage.
9354568Sgibbs#
9364568Sgibbssg_load:
9374568Sgibbs	test	SG_COUNT,0xff	jz sg_load3	# SG being used?
9384568Sgibbs	test	SG_NOLOAD,0x80	jnz sg_load3	# don't reload s/g?
9394568Sgibbs
9404568Sgibbs	clr	HCNT+2
9414568Sgibbs	clr	HCNT+1
9424568Sgibbs	mvi	HCNT+0,SG_SIZEOF
9434568Sgibbs
9444568Sgibbs	mvi	A,4
9454568Sgibbs	mvi	DINDEX,HADDR
9464568Sgibbs	mvi	SG_NEXT		call bcopy
9474568Sgibbs
9484568Sgibbs	mvi	DFCNTRL,0xd			# HDMAEN|DIRECTION|FIFORESET
9494568Sgibbs
9504568Sgibbs#  Wait for DMA from host memory to data FIFO to complete, then disable
9514568Sgibbs#  DMA and wait for it to acknowledge that it's off.
9524568Sgibbs#
9534568Sgibbssg_load1:
9544568Sgibbs	test	DFSTATUS,0x8	jz sg_load1	# HDONE
9554568Sgibbs
9564568Sgibbs	clr	DFCNTRL				# disable DMA
9574568Sgibbssg_load2:
9584568Sgibbs	test	DFCNTRL,0x8	jnz sg_load2	# HDMAENACK
9594568Sgibbs
9604568Sgibbs#  Copy data from FIFO into SCB data pointer and data count.  This assumes
9614568Sgibbs#  that the struct scatterlist has this structure (this and sizeof(struct
9624568Sgibbs#  scatterlist) == 12 are asserted in aic7xxx.c):
9634568Sgibbs#
9644568Sgibbs#	struct scatterlist {
9654568Sgibbs#		char *address;		/* four bytes, little-endian order */
9664568Sgibbs#		...			/* four bytes, ignored */
9674568Sgibbs#		unsigned short length;	/* two bytes, little-endian order */
9684568Sgibbs#	}
9694568Sgibbs#
9704568Sgibbs
9714568Sgibbs# Not in FreeBSD.  the scatter list is only 8 bytes.
9724568Sgibbs# 
9734568Sgibbs# struct ahc_dma_seg {
9744568Sgibbs#       physaddr addr;                  /* four bytes, little-endian order */
9754568Sgibbs#       long    len;                    /* four bytes, little endian order */   
9764568Sgibbs# };
9774568Sgibbs#
9784568Sgibbs
9794568Sgibbs	mov	SCBARRAY+19,DFDAT		# new data address
9804568Sgibbs	mov	SCBARRAY+20,DFDAT
9814568Sgibbs	mov	SCBARRAY+21,DFDAT
9824568Sgibbs	mov	SCBARRAY+22,DFDAT
9834568Sgibbs
9844568Sgibbs	mov	SCBARRAY+23,DFDAT
9854568Sgibbs	mov	SCBARRAY+24,DFDAT
9864568Sgibbs	mov	SCBARRAY+25,DFDAT
9874568Sgibbs	mov	NONE,DFDAT			#Only support 24 bit length.
9884568Sgibbs
9894568Sgibbssg_load3:
9904568Sgibbs	ret
9914568Sgibbs
9924568Sgibbs#  Advance the scatter-gather pointers only IF NEEDED.  If SG is enabled,
9934568Sgibbs#  and the SCSI transfer count is zero (note that this should be called
9944568Sgibbs#  right after a DMA finishes), then move the working copies of the SG
9954568Sgibbs#  pointer/length along.  If the SCSI transfer count is not zero, then
9964568Sgibbs#  presumably the target is disconnecting - do not reload the SG values
9974568Sgibbs#  next time.
9984568Sgibbs#
9994568Sgibbssg_advance:
10004568Sgibbs	test	SG_COUNT,0xff	jz sg_advance2	# s/g enabled?
10014568Sgibbs
10024568Sgibbs	test	STCNT+0,0xff	jnz sg_advance1	# SCSI transfer count nonzero?
10034568Sgibbs	test	STCNT+1,0xff	jnz sg_advance1
10044568Sgibbs	test	STCNT+2,0xff	jnz sg_advance1
10054568Sgibbs
10064568Sgibbs	clr	SG_NOLOAD			# reload s/g next time
10074568Sgibbs	dec	SG_COUNT			# one less segment to go
10084568Sgibbs
10094568Sgibbs	clr	A				# add sizeof(struct scatter)
10104568Sgibbs	add	SG_NEXT+0,SG_SIZEOF,SG_NEXT+0
10114568Sgibbs	adc	SG_NEXT+1,A,SG_NEXT+1
10124568Sgibbs	adc	SG_NEXT+2,A,SG_NEXT+2
10134568Sgibbs	adc	SG_NEXT+3,A,SG_NEXT+3
10144568Sgibbs
10154568Sgibbs	ret
10164568Sgibbs
10174568Sgibbssg_advance1:
10184568Sgibbs	mvi	SG_NOLOAD,0x80			# don't reload s/g next time
10194568Sgibbssg_advance2:
10204568Sgibbs	ret
10214568Sgibbs
10224568Sgibbs#  Add the array base SYNCNEG to the target offset (the target address
10234568Sgibbs#  is in SCSIID), and return the result in SINDEX.  The accumulator
10244568Sgibbs#  contains the 3->8 decoding of the target ID on return.
10254568Sgibbs#
10264568Sgibbsndx_sdtr:
10274568Sgibbs	shr	A,SCSIID,4
10284568Sgibbs	and	A,0x7
10294568Sgibbs	add	SINDEX,SYNCNEG,A
10304568Sgibbs
10314568Sgibbs	and	FUNCTION1,0x70,SCSIID		# 3-bit target address decode
10324568Sgibbs	mov	A,FUNCTION1	ret
10334568Sgibbs
10344568Sgibbs#  If we need to negotiate transfer parameters, build the SDTR message
10354568Sgibbs#  starting at the address passed in SINDEX.  DINDEX is modified on return.
10364568Sgibbs#
10374568Sgibbsmk_sdtr:
10384568Sgibbs	mov	DINDEX,SINDEX			# save SINDEX
10394568Sgibbs
10404568Sgibbs	call	ndx_sdtr
10414568Sgibbs	test	NEEDSDTR,A	jnz mk_sdtr1	# do we need negotiation?
10424568Sgibbs	ret
10434568Sgibbs
10444568Sgibbsmk_sdtr1:
10454568Sgibbs	mvi	DINDIR,1			# extended message
10464568Sgibbs	mvi	DINDIR,3			# extended message length = 3
10474568Sgibbs	mvi	DINDIR,1			# SDTR code
10484568Sgibbs	mvi	DINDIR,25			# REQ/ACK transfer period
10494568Sgibbs	mvi	DINDIR,15			# REQ/ACK offset
10504568Sgibbs
10514568Sgibbs	add	MSG_LEN,-MSG_START+0,DINDEX	# update message length
10524568Sgibbs	ret
10534568Sgibbs
10544568Sgibbs#  Set SCSI bus control signal state.  This also saves the last-written
10554568Sgibbs#  value into a location where the higher-level driver can read it - if
10564568Sgibbs#  it has to send an ABORT or RESET message, then it needs to know this
10574568Sgibbs#  so it can assert ATN without upsetting SCSISIGO.  The new value is
10584568Sgibbs#  expected in SINDEX.  Change the actual state last to avoid contention
10594568Sgibbs#  from the driver.
10604568Sgibbs#
10614568Sgibbsscsisig:
10624568Sgibbs	mov	SIGSTATE,SINDEX
10634568Sgibbs	mov	SCSISIGO,SINDEX	ret
10644568Sgibbs
1065