aic7xxx.seq revision 5775
1# @(#)aic7xxx.seq 1.32 94/11/29 jda
2#
3# Adaptec 274x/284x/294x device driver for Linux and FreeBSD.
4# Copyright (c) 1994 The University of Calgary Department of Computer Science.
5# 
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10# 
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15# 
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19#
20
21# FreeBSD, Twin, Wide, 2 command per target support, and other optimizations
22# provided by Justin T. Gibbs (gibbs@FreeBSD.org)
23#
24#	$Id: aic7xxx.seq,v 1.5 1995/01/16 16:31:21 gibbs Exp $
25
26VERSION AIC7XXX_SEQ_VERSION 1.5
27
28SCBMASK		= 0x1f
29
30SCSISEQ		= 0x00
31SXFRCTL0	= 0x01
32SXFRCTL1	= 0x02
33SCSISIGI	= 0x03
34SCSISIGO	= 0x03
35SCSIRATE	= 0x04
36SCSIID		= 0x05
37SCSIDATL	= 0x06
38STCNT		= 0x08
39STCNT+0		= 0x08
40STCNT+1		= 0x09
41STCNT+2		= 0x0a
42SSTAT0		= 0x0b
43CLRSINT1	= 0x0c
44SSTAT1		= 0x0c
45SIMODE1		= 0x11
46SCSIBUSL	= 0x12
47SHADDR		= 0x14
48SELID		= 0x19
49SBLKCTL		= 0x1f
50SEQCTL		= 0x60
51A		= 0x64				# == ACCUM
52SINDEX		= 0x65
53DINDEX		= 0x66
54ALLZEROS	= 0x6a
55NONE		= 0x6a
56SINDIR		= 0x6c
57DINDIR		= 0x6d
58FUNCTION1	= 0x6e
59HADDR		= 0x88
60HCNT		= 0x8c
61HCNT+0		= 0x8c
62HCNT+1		= 0x8d
63HCNT+2		= 0x8e
64SCBPTR		= 0x90
65INTSTAT		= 0x91
66DFCNTRL		= 0x93
67DFSTATUS	= 0x94
68DFDAT		= 0x99
69QINFIFO		= 0x9b
70QINCNT		= 0x9c
71QOUTFIFO	= 0x9d
72
73SCSICONF_A	= 0x5a
74SCSICONF_B	= 0x5b
75
76#  The two reserved bytes at SCBARRAY+1[23] are expected to be set to
77#  zero, and the reserved bit in SCBARRAY+0 is used as an internal flag
78#  to indicate whether or not to reload scatter-gather parameters after
79#  a disconnect.  We also use bits 6 & 7 to indicate whether or not to
80#  initiate SDTR or WDTR repectively when starting this command.
81#
82SCBARRAY+0	= 0xa0
83
84DISCONNECTED	= 0x04
85NEEDDMA		= 0x08
86SG_LOAD		= 0x10
87NEEDSDTR	= 0x40
88NEEDWDTR	= 0x80
89
90SCBARRAY+1	= 0xa1
91SCBARRAY+2	= 0xa2
92SCBARRAY+3	= 0xa3
93SCBARRAY+4	= 0xa4
94SCBARRAY+5	= 0xa5
95SCBARRAY+6	= 0xa6
96SCBARRAY+7	= 0xa7
97SCBARRAY+8	= 0xa8
98SCBARRAY+9	= 0xa9
99SCBARRAY+10	= 0xaa
100SCBARRAY+11	= 0xab
101SCBARRAY+12	= 0xac
102SCBARRAY+13	= 0xad
103SCBARRAY+14	= 0xae
104SCBARRAY+15	= 0xaf
105SCBARRAY+16	= 0xb0
106SCBARRAY+17	= 0xb1
107SCBARRAY+18	= 0xb2
108SCBARRAY+19	= 0xb3
109SCBARRAY+20	= 0xb4
110SCBARRAY+21	= 0xb5
111SCBARRAY+22	= 0xb6
112SCBARRAY+23	= 0xb7
113SCBARRAY+24	= 0xb8
114SCBARRAY+25	= 0xb9
115SCBARRAY+26	= 0xba
116
117BAD_PHASE	= 0x01				# unknown scsi bus phase
118SEND_REJECT	= 0x11				# sending a message reject
119NO_IDENT	= 0x21				# no IDENTIFY after reconnect
120NO_MATCH	= 0x31				# no cmd match for reconnect
121MSG_SDTR	= 0x41				# SDTR message recieved
122MSG_WDTR	= 0x51				# WDTR message recieved
123MSG_REJECT	= 0x61				# Reject message recieved
124BAD_STATUS	= 0x71				# Bad status from target
125
126#  The host adapter card (at least the BIOS) uses 20-2f for SCSI
127#  device information, 32-33 and 5a-5f as well. As it turns out, the
128#  BIOS trashes 20-2f, writing the synchronous negotiation results
129#  on top of the BIOS values, so we re-use those for our per-target
130#  scratchspace (actually a value that can be copied directly into
131#  SCSIRATE).  The kernel driver will enable synchronous negotiation
132#  for all targets that have a value other than 0 in the lower four
133#  bits of the target scratch space.  This should work irregardless of
134#  whether the bios has been installed. NEEDWDTR and NEEDSDTR are the top
135#  two bits of the SCB control byte.  The kernel driver will set these
136#  when a WDTR or SDTR message should be sent to the target the SCB's 
137#  command references.
138#
139#  The high bit of DROPATN is set if ATN should be dropped before the ACK
140#  when outb is called.  REJBYTE contains the first byte of a MESSAGE IN
141#  message, so the driver can report an intelligible error if a message is
142#  rejected.
143#
144#  FLAGS's high bit is true if we are currently handling a reselect;
145#  its next-highest bit is true ONLY IF we've seen an IDENTIFY message
146#  from the reselecting target.  If we haven't had IDENTIFY, then we have
147#  no idea what the lun is, and we can't select the right SCB register
148#  bank, so force a kernel panic if the target attempts a data in/out or
149#  command phase instead of corrupting something.
150#
151#  Note that SG_NEXT occupies four bytes.
152#
153SYNCNEG		= 0x20
154
155DROPATN		= 0x30
156REJBYTE		= 0x31
157DISC_DSB_A	= 0x32
158DISC_DSB_B	= 0x33
159
160MSG_LEN		= 0x36
161MSG_START+0	= 0x37
162MSG_START+1	= 0x38
163MSG_START+2	= 0x39
164MSG_START+3	= 0x3a
165MSG_START+4	= 0x3b
166MSG_START+5	= 0x3c
167-MSG_START+0	= 0xc9				# 2's complement of MSG_START+0
168
169ARG_1		= 0x4c				# sdtr conversion args & return
170BUS_16_BIT	= 0x01
171RETURN_1	= 0x4c
172
173SIGSTATE	= 0x4d				# value written to SCSISIGO
174
175# Linux users should use 0xc (12) for SG_SIZEOF
176SG_SIZEOF	= 0x8 				# sizeof(struct ahc_dma)
177#SG_SIZEOF	= 0xc 				# sizeof(struct scatterlist)
178SCB_SIZEOF	= 0x13				# sizeof SCB to DMA (19 bytes)
179
180SG_NOLOAD	= 0x50				# load SG pointer/length?
181SG_COUNT	= 0x51				# working value of SG count
182SG_NEXT		= 0x52				# working value of SG pointer
183SG_NEXT+0	= 0x52
184SG_NEXT+1	= 0x53
185SG_NEXT+2	= 0x54
186SG_NEXT+3	= 0x55
187
188SCBCOUNT	= 0x56				# the actual number of SCBs
189FLAGS		= 0x57				# Device configuration flags
190TWIN_BUS	= 0x01
191WIDE_BUS	= 0x02
192SENSE		= 0x10
193ACTIVE_MSG	= 0x20
194IDENTIFY_SEEN	= 0x40
195RESELECTED	= 0x80
196
197ACTIVE_A	= 0x58
198ACTIVE_B	= 0x59
199
200#  Poll QINCNT for work - the lower bits contain
201#  the number of entries in the Queue In FIFO.
202#
203start:
204	test	FLAGS,SENSE	jnz start_sense
205start_nosense:
206	test	FLAGS,TWIN_BUS	jz start2	# Are we a twin channel device?
207# For fairness, we check the other bus first, since we just finished a 
208# transaction on the current channel.
209	xor	SBLKCTL,0x08			# Toggle to the other bus
210	test	SCSISIGI,0x4	jnz reselect	# BSYI
211	xor	SBLKCTL,0x08			# Toggle to the original bus
212start2:
213	test	SCSISIGI,0x4	jnz reselect	# BSYI
214	test	QINCNT,SCBMASK	jz start_nosense
215
216# We have at least one queued SCB now.  Set the SCB pointer
217# from the FIFO so we see the right bank of SCB registers,
218# then set SCSI options and set the initiator and target
219# SCSI IDs.
220#
221	mov	SCBPTR,QINFIFO
222
223# If the control byte of this SCB has the NEEDDMA flag set, we have
224# yet to DMA it from host memory
225
226test	SCBARRAY+0,NEEDDMA	jz test_busy
227	clr	HCNT+2
228	clr	HCNT+1
229	mvi	HCNT+0,SCB_SIZEOF
230
231	mvi	DINDEX,HADDR
232	mvi	SCBARRAY+26	call bcopy_4
233
234	mvi	DFCNTRL,0xd			# HDMAEN|DIRECTION|FIFORESET
235
236#  Wait for DMA from host memory to data FIFO to complete, then disable
237#  DMA and wait for it to acknowledge that it's off.
238#
239scb_load1:
240	test	DFSTATUS,0x8	jz scb_load1	# HDONE
241
242	clr	DFCNTRL				# disable DMA
243scb_load2:
244	test	DFCNTRL,0x8	jnz scb_load2	# HDMAENACK
245
246# Copy the SCB from the FIFO to  the SCBARRAY
247
248	mov	SCBARRAY+0, DFDAT
249	mov	SCBARRAY+1, DFDAT
250	mov	SCBARRAY+2, DFDAT
251	mov	SCBARRAY+3, DFDAT
252	mov	SCBARRAY+4, DFDAT
253	mov	SCBARRAY+5, DFDAT
254	mov	SCBARRAY+6, DFDAT
255	mov	SCBARRAY+7, DFDAT
256	mov	SCBARRAY+8, DFDAT
257	mov	SCBARRAY+9, DFDAT
258	mov	SCBARRAY+10, DFDAT
259	mov	SCBARRAY+11, DFDAT
260	mov	SCBARRAY+12, DFDAT
261	mov	SCBARRAY+13, DFDAT
262	mov	SCBARRAY+14, DFDAT
263	mov	SCBARRAY+15, DFDAT
264	mov	SCBARRAY+16, DFDAT
265	mov	SCBARRAY+17, DFDAT
266	mov	SCBARRAY+18, DFDAT
267   
268# See if there is not already an active SCB for this target.  This code
269# locks out on a per target basis instead of target/lun.  Although this
270# is not ideal for devices that have multiple luns active at the same
271# time, it is faster than looping through all SCB's looking for active
272# commands.  It may be benificial to make findscb a more general procedure
273# to see if the added cost of the search is negligible.  This code also 
274# assumes that the kernel driver will clear the active flags on board 
275# initialization, board reset, and a target's SELTO.
276
277test_busy:
278	and	FUNCTION1,0x70,SCBARRAY+1
279	mov	A,FUNCTION1
280	test	SCBARRAY+1,0x88	jz test_a	# Id < 8 && A channel
281
282	test	ACTIVE_B,A	jnz requeue
283	or	ACTIVE_B,A	# Mark the current target as busy
284	jmp	start_scb
285
286start_sense:
287# Clear the SENSE flag first, then do a normal start_scb
288	and	FLAGS,0xef
289	jmp	start_scb
290
291# Place the currently active back on the queue for later processing
292requeue:
293	mov	QINFIFO, SCBPTR
294	jmp	start_nosense
295
296test_a:
297	test	ACTIVE_A,A	jnz requeue
298	or	ACTIVE_A,A	# Mark the current target as busy
299
300start_scb:
301	or	SCBARRAY+0,NEEDDMA
302	and	SINDEX,0x08,SCBARRAY+1
303	and	A,WIDE_BUS,FLAGS	# Wide bus?	
304	or	SINDEX,A
305	mov	SBLKCTL,SINDEX	# select channel, bus width
306	mov	SCBARRAY+1	call initialize
307	clr	SG_NOLOAD
308	and	FLAGS,0x3f	# !RESELECTING
309
310#  As soon as we get a successful selection, the target should go
311#  into the message out phase since we have ATN asserted.  Prepare
312#  the message to send, locking out the device driver.  If the device
313#  driver hasn't beaten us with an ABORT or RESET message, then tack
314#  on an SDTR negotiation if required.
315#
316#  Messages are stored in scratch RAM starting with a flag byte (high bit
317#  set means active message), one length byte, and then the message itself.
318#
319	mov	SCBARRAY+1	call disconnect	# disconnect ok?
320
321	and	SINDEX,0x7,SCBARRAY+1		# lun
322	or	SINDEX,A			# return value from disconnect
323	or	SINDEX,0x80	call mk_mesg	# IDENTIFY message
324
325	mov	A,SINDEX
326	test	SCBARRAY+0,0xc0	jz  !message	# WDTR or SDTR??
327	cmp	MSG_START+0,A	jne !message	# did driver beat us?
328	mvi	MSG_START+1	call mk_dtr	# build DTR message if needed
329
330!message:
331
332#  Enable selection phase as an initiator, and do automatic ATN
333#  after the selection.
334#
335	mvi	SCSISEQ,0x48			# ENSELO|ENAUTOATNO
336
337#  Wait for successful arbitration.  The AIC-7770 documentation says
338#  that SELINGO indicates successful arbitration, and that it should
339#  be used to look for SELDO.  However, if the sequencer is paused at
340#  just the right time - a parallel fsck(8) on two drives did it for
341#  me - then SELINGO can flip back to false before we've seen it.  This
342#  makes the sequencer sit in the arbitration loop forever.  This is
343#  Not Good.
344#
345#  Therefore, I've added a check in the arbitration loop for SELDO
346#  too.  This could arguably be made a critical section by disabling
347#  pauses, but I don't want to make a potentially infinite loop a CS.
348#  I suppose you could fold it into the select loop, too, but since
349#  I've been hunting this bug for four days it's kinda like a trophy.
350#
351arbitrate:
352	test	SSTAT0,0x40	jnz *select	# SELDO
353	test	SSTAT0,0x10	jz arbitrate	# SELINGO
354
355#  Wait for a successful selection.  If the hardware selection
356#  timer goes off, then the driver gets the interrupt, so we don't
357#  need to worry about it.
358#
359select:
360	test	SSTAT0,0x40	jz select	# SELDO
361	jmp	*select
362
363#  Reselection is being initiated by a target - we've seen the BSY
364#  line driven active, and we didn't do it!  Enable the reselection
365#  hardware, and wait for it to finish.  Make a note that we've been
366#  reselected, but haven't seen an IDENTIFY message from the target
367#  yet.
368#
369reselect:
370	mvi	SCSISEQ,0x10			# ENRSELI
371
372reselect1:
373	test	SSTAT0,0x20	jz reselect1	# SELDI
374	mov	SELID		call initialize
375
376	and	FLAGS,0x3f			# reselected, no IDENTIFY	
377	or	FLAGS,RESELECTED		
378
379#  After the [re]selection, make sure that the [re]selection enable
380#  bit is off.  This chip is flaky enough without extra things
381#  turned on.  Also clear the BUSFREE bit in SSTAT1 since we'll be
382#  using it shortly.
383#
384*select:
385	clr	SCSISEQ
386	mvi	CLRSINT1,0x8			# CLRBUSFREE
387
388#  Main loop for information transfer phases.  If BSY is false, then
389#  we have a bus free condition, expected or not.  Otherwise, wait
390#  for the target to assert REQ before checking MSG, C/D and I/O
391#  for the bus phase.
392#
393#  We can't simply look at the values of SCSISIGI here (if we want
394#  to do synchronous data transfer), because the target won't assert
395#  REQ if it's already sent us some data that we haven't acknowledged
396#  yet.
397#
398ITloop:
399	test	SSTAT1,0x8	jnz p_busfree	# BUSFREE
400	test	SSTAT1,0x1	jz ITloop	# REQINIT
401
402	and	A,0xe0,SCSISIGI			# CDI|IOI|MSGI
403
404	cmp	ALLZEROS,A	je p_dataout
405	cmp	A,0x40		je p_datain
406	cmp	A,0x80		je p_command
407	cmp	A,0xc0		je p_status
408	cmp	A,0xa0		je p_mesgout
409	cmp	A,0xe0		je p_mesgin
410
411	mvi	INTSTAT,BAD_PHASE		# unknown - signal driver
412
413p_dataout:
414	mvi	0		call scsisig	# !CDO|!IOO|!MSGO
415	call	assert
416	call	sg_load
417
418	mvi	DINDEX,HADDR
419	mvi	SCBARRAY+19	call bcopy_4
420
421#	mvi	DINDEX,HCNT	# implicit since HCNT is next to HADDR
422	mvi	SCBARRAY+23	call bcopy_3
423
424	mvi	DINDEX,STCNT
425	mvi	SCBARRAY+23	call bcopy_3
426
427	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
428						#   DIRECTION|FIFORESET
429
430#  After a DMA finishes, save the final transfer pointer and count
431#  back into the SCB, in case a device disconnects in the middle of
432#  a transfer.  Use SHADDR and STCNT instead of HADDR and HCNT, since
433#  it's a reflection of how many bytes were transferred on the SCSI
434#  (as opposed to the host) bus.
435#
436	mvi	DINDEX,SCBARRAY+23
437	mvi	STCNT		call bcopy_3
438
439	mvi	DINDEX,SCBARRAY+19
440	mvi	SHADDR		call bcopy_4
441
442	call	sg_advance
443	mov	SCBARRAY+18,SG_COUNT		# residual S/G count
444
445	jmp	ITloop
446
447p_datain:
448	mvi	0x40		call scsisig	# !CDO|IOO|!MSGO
449	call	assert
450	call	sg_load
451
452	mvi	DINDEX,HADDR
453	mvi	SCBARRAY+19	call bcopy_4
454
455#	mvi	DINDEX,HCNT	# implicit since HCNT is next to HADDR
456	mvi	SCBARRAY+23	call bcopy_3
457
458	mvi	DINDEX,STCNT
459	mvi	SCBARRAY+23	call bcopy_3
460
461	mvi	0x39		call dma	# SCSIEN|SDMAEN|HDMAEN|
462						#   !DIRECTION|FIFORESET
463	mvi	DINDEX,SCBARRAY+23
464	mvi	STCNT		call bcopy_3
465
466	mvi	DINDEX,SCBARRAY+19
467	mvi	SHADDR		call bcopy_4
468
469	call	sg_advance
470	mov	SCBARRAY+18,SG_COUNT		# residual S/G count
471
472	jmp	ITloop
473
474#  Command phase.  Set up the DMA registers and let 'er rip - the
475#  two bytes after the SCB SCSI_cmd_length are zeroed by the driver,
476#  so we can copy those three bytes directly into HCNT.
477#
478p_command:
479	mvi	0x80		call scsisig	# CDO|!IOO|!MSGO
480	call	assert
481
482	mvi	DINDEX,HADDR
483	mvi	SCBARRAY+7	call bcopy_4
484
485#	mvi	DINDEX,HCNT	# implicit since HCNT is next to HADDR
486	mvi	SCBARRAY+11	call bcopy_3
487
488	mvi	DINDEX,STCNT
489	mvi	SCBARRAY+11	call bcopy_3
490
491	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
492						#   DIRECTION|FIFORESET
493	jmp	ITloop
494
495#  Status phase.  Wait for the data byte to appear, then read it
496#  and store it into the SCB.
497#
498p_status:
499	mvi	0xc0		call scsisig	# CDO|IOO|!MSGO
500
501	mvi	SCBARRAY+14	call inb
502	jmp	ITloop
503
504#  Message out phase.  If there is no active message, but the target
505#  took us into this phase anyway, build a no-op message and send it.
506#
507p_mesgout:
508	mvi	0xa0		call scsisig	# CDO|!IOO|MSGO
509	mvi	0x8		call mk_mesg	# build NOP message
510
511#  Set up automatic PIO transfer from MSG_START.  Bit 3 in
512#  SXFRCTL0 (SPIOEN) is already on.
513#
514	mvi	SINDEX,MSG_START+0
515	mov	DINDEX,MSG_LEN
516	clr	A
517
518#  When target asks for a byte, drop ATN if it's the last one in
519#  the message.  Otherwise, keep going until the message is exhausted.
520#  (We can't use outb for this since it wants the input in SINDEX.)
521#
522#  Keep an eye out for a phase change, in case the target issues
523#  a MESSAGE REJECT.
524#
525p_mesgout2:
526	test	SSTAT0,0x2	jz p_mesgout2	# SPIORDY
527	test	SSTAT1,0x10	jnz p_mesgout6	# PHASEMIS
528
529	cmp	DINDEX,1	jne p_mesgout3	# last byte?
530	mvi	CLRSINT1,0x40			# CLRATNO - drop ATN
531
532#  Write a byte to the SCSI bus.  The AIC-7770 refuses to automatically
533#  send ACKs in automatic PIO or DMA mode unless you make sure that the
534#  "expected" bus phase in SCSISIGO matches the actual bus phase.  This
535#  behaviour is completely undocumented and caused me several days of
536#  grief.
537#
538#  After plugging in different drives to test with and using a longer
539#  SCSI cable, I found that I/O in Automatic PIO mode ceased to function,
540#  especially when transferring >1 byte.  It seems to be much more stable
541#  if STCNT is set to one before the transfer, and SDONE (in SSTAT0) is
542#  polled for transfer completion - for both output _and_ input.  The
543#  only theory I have is that SPIORDY doesn't drop right away when SCSIDATL
544#  is accessed (like the documentation says it does), and that on a longer
545#  cable run, the sequencer code was fast enough to loop back and see
546#  an SPIORDY that hadn't dropped yet.
547#
548p_mesgout3:
549	call	one_stcnt
550	mov	SCSIDATL,SINDIR
551
552p_mesgout4:
553	test	SSTAT0,0x4	jz p_mesgout4	# SDONE
554	dec	DINDEX
555	inc	A
556	cmp	MSG_LEN,A	jne p_mesgout2
557
558#  If the next bus phase after ATN drops is a message out, it means
559#  that the target is requesting that the last message(s) be resent.
560#
561p_mesgout5:
562	test	SSTAT1,0x8	jnz p_mesgout6	# BUSFREE
563	test	SSTAT1,0x1	jz p_mesgout5	# REQINIT
564
565	and	A,0xe0,SCSISIGI			# CDI|IOI|MSGI
566	cmp	A,0xa0		jne p_mesgout6
567	mvi	0x10		call scsisig	# ATNO - re-assert ATN
568
569	jmp	ITloop
570
571p_mesgout6:
572	mvi	CLRSINT1,0x40			# CLRATNO - in case of PHASEMIS
573	and	FLAGS,0xdf			# no active msg
574	jmp	ITloop
575
576#  Message in phase.  Bytes are read using Automatic PIO mode, but not
577#  using inb.  This alleviates a race condition, namely that if ATN had
578#  to be asserted under Automatic PIO mode, it had to beat the SCSI
579#  circuitry sending an ACK to the target.  This showed up under heavy
580#  loads and really confused things, since ABORT commands wouldn't be
581#  seen by the drive after an IDENTIFY message in until it had changed
582#  to a data I/O phase.
583#
584p_mesgin:
585	mvi	0xe0		call scsisig	# CDO|IOO|MSGO
586	mvi	A		call inb_first	# read the 1st message byte
587	mvi	REJBYTE,A			# save it for the driver
588
589	cmp	ALLZEROS,A	jne p_mesgin1
590
591#  We got a "command complete" message, so put the SCB pointer
592#  into the Queue Out, and trigger a completion interrupt.
593#  Check status for non zero return and interrupt driver if needed
594#  This allows the driver to interpret errors only when they occur
595#  instead of always uploading the scb.  If the status is SCSI_CHECK,
596#  the driver will download a new scb requesting sense, to replace 
597#  the old one and the sequencer code will imediately jump to start 
598#  working on it.  If the kernel driver does not wish to request sense, 
599#  the sequencer program counter is incremented by 1, preventing another run 
600#  on the current SCB and the command is allowed to complete.  We don't 
601#  bother to post to the QOUTFIFO in the error case since it would require 
602#  extra work in the kernel driver to ensure that the entry was removed 
603#  before the command complete code tried processing it.
604
605	test	SCBARRAY+14,0xff	jz status_ok	# 0 Status?
606	mvi	INTSTAT,BAD_STATUS			# let driver know
607	test	FLAGS,SENSE	jz status_ok
608	jmp	p_mesgin_done
609
610status_ok:
611
612#  First, mark this target as free.
613	and	FUNCTION1,0x70,SCBARRAY+1
614	mov	A,FUNCTION1
615	test	SCBARRAY+1,0x88 jz clear_a
616	xor	ACTIVE_B,A
617	jmp	complete
618
619clear_a:
620	xor	ACTIVE_A,A
621
622complete:
623	mov	QOUTFIFO,SCBPTR
624	mvi	INTSTAT,0x02			# CMDCMPLT
625	jmp	p_mesgin_done
626
627#  Is it an extended message?  We only support the synchronous and wide data
628#  transfer request messages, which will probably be in response to
629#  WDTR or SDTR message outs from us.  If it's not SDTR or WDTR, reject it -
630#  apparently this can be done after any message in byte, according
631#  to the SCSI-2 spec.
632#
633p_mesgin1:
634	cmp	A,1		jne p_mesgin2	# extended message code?
635	
636	mvi	ARG_1		call inb_next	# extended message length
637	mvi	A		call inb_next	# extended message code
638
639	cmp	A,1		je p_mesginSDTR	# Syncronous negotiation message
640	cmp	A,3		je p_mesginWDTR # Wide negotiation message
641	jmp	p_mesginN
642
643p_mesginWDTR:
644	cmp	ARG_1,2		jne p_mesginN	# extended mesg length = 2
645	mvi	A		call inb_next	# Width of bus
646	mvi	INTSTAT,MSG_WDTR		# let driver know
647	test	RETURN_1,0x80	jz p_mesgin_done# Do we need to send WDTR?
648
649# We didn't initiate the wide negotiation, so we must respond to the request
650	and	RETURN_1,0x7f			# Clear the SEND_WDTR Flag
651	or	FLAGS,ACTIVE_MSG
652	mvi	DINDEX,MSG_START+0
653	mvi	MSG_START+0	call mk_wdtr	# build WDTR message	
654	or	SINDEX,0x10,SIGSTATE		# turn on ATNO
655	call	scsisig
656	jmp	p_mesgin_done
657
658p_mesginSDTR:
659	cmp	ARG_1,3		jne p_mesginN	# extended mesg length = 3
660	mvi	ARG_1		call inb_next	# xfer period
661	mvi	A		call inb_next	# REQ/ACK offset
662	mvi	INTSTAT,MSG_SDTR		# call driver to convert
663
664	test	RETURN_1,0x80	jz p_mesgin_done# Do we need to mk_sdtr?
665
666	or	FLAGS,ACTIVE_MSG
667	mvi	DINDEX, MSG_START+0
668	mvi     MSG_START+0     call mk_sdtr
669	or	SINDEX,0x10,SIGSTATE		# turn on ATNO
670	call	scsisig
671	jmp	p_mesgin_done
672
673#  Is it a disconnect message?  Set a flag in the SCB to remind us
674#  and await the bus going free.
675#
676p_mesgin2:
677	cmp	A,4		jne p_mesgin3	# disconnect code?
678
679	or	SCBARRAY+0,0x4			# set "disconnected" bit
680	jmp	p_mesgin_done
681
682#  Save data pointers message?  Copy working values into the SCB,
683#  usually in preparation for a disconnect.
684#
685p_mesgin3:
686	cmp	A,2		jne p_mesgin4	# save data pointers code?
687
688	call	sg_ram2scb
689	jmp	p_mesgin_done
690
691#  Restore pointers message?  Data pointers are recopied from the
692#  SCB anyway at the start of any DMA operation, so the only thing
693#  to copy is the scatter-gather values.
694#
695p_mesgin4:
696	cmp	A,3		jne p_mesgin5	# restore pointers code?
697
698	call	sg_scb2ram
699	jmp	p_mesgin_done
700
701#  Identify message?  For a reconnecting target, this tells us the lun
702#  that the reconnection is for - find the correct SCB and switch to it,
703#  clearing the "disconnected" bit so we don't "find" it by accident later.
704#
705p_mesgin5:
706	test	A,0x80		jz p_mesgin6	# identify message?
707
708	test	A,0x78		jnz p_mesginN	# !DiscPriv|!LUNTAR|!Reserved
709
710	mov	A		call findSCB	# switch to correct SCB
711
712#  If a active message is present after calling findSCB, then either it
713#  or the driver is trying to abort the command.  Either way, something
714#  untoward has happened and we should just leave it alone.
715#
716	test	FLAGS,ACTIVE_MSG	jnz p_mesgin_done
717
718	and	SCBARRAY+0,0xfb			# clear disconnect bit in SCB
719	or	FLAGS,0xc0			# make note of IDENTIFY
720
721	call	sg_scb2ram			# implied restore pointers
722						#   required on reselect
723	jmp	p_mesgin_done
724
725#  Message reject?  Let the kernel driver handle this.  If we have an 
726#  outstanding WDTR or SDTR negotiation, assume that it's a response from 
727#  the target selecting 8bit or asynchronous transfer, otherwise just ignore 
728#  it since we have no clue what it pertains to.
729#
730p_mesgin6:
731	cmp	A,7		jne p_mesgin7	# message reject code?
732
733	mvi	INTSTAT, MSG_REJECT
734	jmp	p_mesgin_done
735
736#  [ ADD MORE MESSAGE HANDLING HERE ]
737#
738p_mesgin7:
739
740#  We have no idea what this message in is, and there's no way
741#  to pass it up to the kernel, so we issue a message reject and
742#  hope for the best.  Since we're now using manual PIO mode to
743#  read in the message, there should no longer be a race condition
744#  present when we assert ATN.  In any case, rejection should be a
745#  rare occurrence - signal the driver when it happens.
746#
747p_mesginN:
748	or	SINDEX,0x10,SIGSTATE		# turn on ATNO
749	call	scsisig
750	mvi	INTSTAT,SEND_REJECT		# let driver know
751
752	mvi	0x7		call mk_mesg	# MESSAGE REJECT message
753
754p_mesgin_done:
755	call	inb_last			# ack & turn auto PIO back on
756	jmp	ITloop
757
758#  Bus free phase.  It might be useful to interrupt the device
759#  driver if we aren't expecting this.  For now, make sure that
760#  ATN isn't being asserted and look for a new command.
761#
762p_busfree:
763	mvi	CLRSINT1,0x40			# CLRATNO
764	clr	SIGSTATE
765	jmp	start
766
767#  Instead of a generic bcopy routine that requires an argument, we unroll
768#  the two cases that are actually used, and call them explicitly.  This
769#  not only reduces the overhead of doing a bcopy by 2/3rds, but ends up
770#  saving space in the program since you don't have to put the argument 
771#  into the accumulator before the call.  Both functions expect DINDEX to
772#  contain the destination address and SINDEX to contain the source 
773#  address.
774bcopy_3:
775	mov	DINDIR,SINDIR
776	mov	DINDIR,SINDIR
777	mov	DINDIR,SINDIR	ret
778
779bcopy_4:
780	mov	DINDIR,SINDIR
781	mov	DINDIR,SINDIR
782	mov	DINDIR,SINDIR
783	mov	DINDIR,SINDIR	ret
784	
785
786#  Locking the driver out, build a one-byte message passed in SINDEX
787#  if there is no active message already.  SINDEX is returned intact.
788#
789mk_mesg:
790	mvi	SEQCTL,0x50			# PAUSEDIS|FASTMODE
791	test	FLAGS,ACTIVE_MSG jnz mk_mesg1	# active message?
792
793	or	FLAGS,ACTIVE_MSG		# if not, there is now
794	mvi	MSG_LEN,1			# length = 1
795	mov	MSG_START+0,SINDEX		# 1-byte message
796
797mk_mesg1:
798	mvi	SEQCTL,0x10	ret		# !PAUSEDIS|FASTMODE
799
800#  Input byte in Automatic PIO mode.  The address to store the byte
801#  in should be in SINDEX.  DINDEX will be used by this routine.
802#
803inb:
804	test	SSTAT0,0x2	jz inb		# SPIORDY
805	mov	DINDEX,SINDEX
806	call	one_stcnt			# xfer one byte
807	mov	DINDIR,SCSIDATL
808inb1:
809	test	SSTAT0,0x4	jz inb1		# SDONE - wait to "finish"
810	ret
811
812#  Carefully read data in Automatic PIO mode.  I first tried this using
813#  Manual PIO mode, but it gave me continual underrun errors, probably
814#  indicating that I did something wrong, but I feel more secure leaving
815#  Automatic PIO on all the time.
816#
817#  According to Adaptec's documentation, an ACK is not sent on input from
818#  the target until SCSIDATL is read from.  So we wait until SCSIDATL is
819#  latched (the usual way), then read the data byte directly off the bus
820#  using SCSIBUSL.  When we have pulled the ATN line, or we just want to
821#  acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
822#  spec guarantees that the target will hold the data byte on the bus until
823#  we send our ACK.
824#
825#  The assumption here is that these are called in a particular sequence,
826#  and that REQ is already set when inb_first is called.  inb_{first,next}
827#  use the same calling convention as inb.
828#
829inb_first:
830	mov	DINDEX,SINDEX
831	mov	DINDIR,SCSIBUSL	ret		# read byte directly from bus
832
833inb_next:
834	mov	DINDEX,SINDEX			# save SINDEX
835
836	call	one_stcnt			# xfer one byte
837	mov	NONE,SCSIDATL			# dummy read from latch to ACK
838inb_next1:
839	test	SSTAT0,0x4	jz inb_next1	# SDONE
840inb_next2:
841	test	SSTAT0,0x2	jz inb_next2	# SPIORDY - wait for next byte
842	mov	DINDIR,SCSIBUSL	ret		# read byte directly from bus
843
844inb_last:
845	call	one_stcnt			# ACK with dummy read
846	mov	NONE,SCSIDATL
847inb_last1:
848	test	SSTAT0,0x4	jz inb_last1	# wait for completion
849	ret
850
851#  Output byte in Automatic PIO mode.  The byte to output should be
852#  in SINDEX.  If DROPATN's high bit is set, then ATN will be dropped
853#  before the byte is output.
854#
855outb:
856	test	SSTAT0,0x2	jz outb		# SPIORDY
857	call	one_stcnt			# xfer one byte
858
859	test	DROPATN,0x80	jz outb1
860	mvi	CLRSINT1,0x40			# CLRATNO
861	clr	DROPATN
862outb1:
863	mov	SCSIDATL,SINDEX
864outb2:
865	test	SSTAT0,0x4	jz outb2	# SDONE
866	ret
867
868#  Write the value "1" into the STCNT registers, for Automatic PIO
869#  transfers.
870#
871one_stcnt:
872	clr	STCNT+2
873	clr	STCNT+1
874	mvi	STCNT+0,1	ret
875
876#  DMA data transfer.  HADDR and HCNT must be loaded first, and
877#  SINDEX should contain the value to load DFCNTRL with - 0x3d for
878#  host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
879#  during initialization.
880#
881dma:
882	mov	DFCNTRL,SINDEX
883dma1:
884dma2:
885	test	SSTAT0,0x1	jnz dma3	# DMADONE
886	test	SSTAT1,0x10	jz dma1		# PHASEMIS, ie. underrun
887
888#  We will be "done" DMAing when the transfer count goes to zero, or
889#  the target changes the phase (in light of this, it makes sense that
890#  the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
891#  doing a SCSI->Host transfer, the data FIFO should be flushed auto-
892#  magically on STCNT=0 or a phase change, so just wait for FIFO empty
893#  status.
894#
895dma3:
896	test	SINDEX,0x4	jnz dma5	# DIRECTION
897dma4:
898	test	DFSTATUS,0x1	jz dma4		# !FIFOEMP
899
900#  Now shut the DMA enables off, and copy STCNT (ie. the underrun
901#  amount, if any) to the SCB registers; SG_COUNT will get copied to
902#  the SCB's residual S/G count field after sg_advance is called.  Make
903#  sure that the DMA enables are actually off first lest we get an ILLSADDR.
904#
905dma5:
906	clr	DFCNTRL				# disable DMA
907dma6:
908	test	DFCNTRL,0x38	jnz dma6	# SCSIENACK|SDMAENACK|HDMAENACK
909
910	mvi	DINDEX,SCBARRAY+15
911	mvi	STCNT		call bcopy_3
912
913	ret
914
915#  Common SCSI initialization for selection and reselection.  Expects
916#  the target SCSI ID to be in the upper four bits of SINDEX, and A's
917#  contents are stomped on return.
918#
919initialize:
920	and	SINDEX,0xf0		# Get target ID
921	and	A,0x0f,SCSIID
922	or	SINDEX,A
923	mov	SCSIID,SINDEX
924
925#  Esundry initialization.
926#
927	clr	DROPATN
928	clr	SIGSTATE
929
930#  Turn on Automatic PIO mode now, before we expect to see a REQ
931#  from the target.  It shouldn't hurt anything to leave it on.  Set
932#  CLRCHN here before the target has entered a data transfer mode -
933#  with synchronous SCSI, if you do it later, you blow away some
934#  data in the SCSI FIFO that the target has already sent to you.
935#
936	mvi	SXFRCTL0,0x8a			# DFON|SPIOEN|CLRCHN
937
938#  Initialize scatter-gather pointers by setting up the working copy
939#  in scratch RAM.
940#
941	call	sg_scb2ram
942
943#  Initialize SCSIRATE with the appropriate value for this target.
944#
945	call	ndx_dtr
946	mov	SCSIRATE,SINDIR	ret
947
948#  Assert that if we've been reselected, then we've seen an IDENTIFY
949#  message.
950#
951assert:
952	test	FLAGS,RESELECTED	jz assert1	# reselected?
953	test	FLAGS,IDENTIFY_SEEN	jnz assert1	# seen IDENTIFY?
954
955	mvi	INTSTAT,NO_IDENT		# no - cause a kernel panic
956
957assert1:
958	ret
959
960#  Find out if disconnection is ok from the information the BIOS has left
961#  us.  The tcl from SCBARRAY+1 should be in SINDEX; A will
962#  contain either 0x40 (disconnection ok) or 0x00 (disconnection not ok)
963#  on exit.
964#
965#  To allow for wide or twin busses, we check the upper bit of the target ID
966#  and the channel ID and look at the appropriate disconnect register. 
967#
968disconnect:
969	and	FUNCTION1,0x70,SINDEX		# strip off extra just in case
970	mov	A,FUNCTION1
971	test	SINDEX, 0x88	jz disconnect_a
972
973	test	DISC_DSB_B,A	jz disconnect1	# bit nonzero if DISabled
974	clr	A		ret
975
976disconnect_a:
977	test	DISC_DSB_A,A	jz disconnect1	# bit nonzero if DISabled
978	clr	A		ret
979
980disconnect1:
981	mvi	A,0x40		ret
982
983#  Locate the SCB matching the target ID in SELID and the lun in the lower
984#  three bits of SINDEX, and switch the SCB to it.  Have the kernel print
985#  a warning message if it can't be found, and generate an ABORT message
986#  to the target.  We keep the value of the t/c/l that we are trying to find
987#  in DINDEX so it is not overwritten during our check to see if we are
988#  at the last SCB.
989#
990findSCB:
991	and	A,0x7,SINDEX		# lun in lower three bits
992	or	DINDEX,A,SELID		
993	and	DINDEX,0xf7
994	and	A,0x08,SBLKCTL		# B Channel??
995	or	DINDEX,A
996	clr	SINDEX
997
998findSCB1:
999	mov	A,DINDEX
1000	mov	SCBPTR,SINDEX			# switch to new SCB
1001	cmp	SCBARRAY+1,A	jne findSCB2	# target ID/channel/lun match?
1002	test	SCBARRAY+0,0x4	jz findSCB2	# should be disconnected
1003	ret
1004
1005findSCB2:
1006	inc	SINDEX
1007	mov	A,SCBCOUNT
1008	cmp	SINDEX,A	jne findSCB1
1009
1010	mvi	INTSTAT,NO_MATCH		# not found - signal kernel
1011	mvi	0x6		call mk_mesg	# ABORT message
1012
1013	or	SINDEX,0x10,SIGSTATE		# assert ATNO
1014	call	scsisig
1015	ret
1016
1017#  Make a working copy of the scatter-gather parameters in the SCB.
1018#
1019sg_scb2ram:
1020	mov	SG_COUNT,SCBARRAY+2
1021
1022	mvi	DINDEX,SG_NEXT
1023	mvi	SCBARRAY+3	call bcopy_4
1024
1025	mvi	SG_NOLOAD,0x80
1026	test	SCBARRAY+0,0x10	jnz sg_scb2ram1	# don't reload s/g?
1027	clr	SG_NOLOAD
1028
1029sg_scb2ram1:
1030	ret
1031
1032#  Copying RAM values back to SCB, for Save Data Pointers message.
1033#
1034sg_ram2scb:
1035	mov	SCBARRAY+2,SG_COUNT
1036
1037	mvi	DINDEX,SCBARRAY+3
1038	mvi	SG_NEXT		call bcopy_4
1039
1040	and	SCBARRAY+0,0xef,SCBARRAY+0
1041	test	SG_NOLOAD,0x80	jz sg_ram2scb1	# reload s/g?
1042	or	SCBARRAY+0,SG_LOAD
1043
1044sg_ram2scb1:
1045	ret
1046
1047#  Load a struct scatter if needed and set up the data address and
1048#  length.  If the working value of the SG count is nonzero, then
1049#  we need to load a new set of values.
1050#
1051#  This, like the above DMA, assumes a little-endian host data storage.
1052#
1053sg_load:
1054	test	SG_COUNT,0xff	jz sg_load3	# SG being used?
1055	test	SG_NOLOAD,0x80	jnz sg_load3	# don't reload s/g?
1056
1057	clr	HCNT+2
1058	clr	HCNT+1
1059	mvi	HCNT+0,SG_SIZEOF
1060
1061	mvi	DINDEX,HADDR
1062	mvi	SG_NEXT		call bcopy_4
1063
1064	mvi	DFCNTRL,0xd			# HDMAEN|DIRECTION|FIFORESET
1065
1066#  Wait for DMA from host memory to data FIFO to complete, then disable
1067#  DMA and wait for it to acknowledge that it's off.
1068#
1069sg_load1:
1070	test	DFSTATUS,0x8	jz sg_load1	# HDONE
1071
1072	clr	DFCNTRL				# disable DMA
1073sg_load2:
1074	test	DFCNTRL,0x8	jnz sg_load2	# HDMAENACK
1075
1076#  Copy data from FIFO into SCB data pointer and data count.  This assumes
1077#  that the struct scatterlist has this structure (this and sizeof(struct
1078#  scatterlist) == 12 are asserted in aic7xxx.c):
1079#
1080#	struct scatterlist {
1081#		char *address;		/* four bytes, little-endian order */
1082#		...			/* four bytes, ignored */
1083#		unsigned short length;	/* two bytes, little-endian order */
1084#	}
1085#
1086
1087# Not in FreeBSD.  the scatter list is only 8 bytes.
1088# 
1089# struct ahc_dma_seg {
1090#       physaddr addr;                  /* four bytes, little-endian order */
1091#       long    len;                    /* four bytes, little endian order */   
1092# };
1093#
1094
1095	mov	SCBARRAY+19,DFDAT		# new data address
1096	mov	SCBARRAY+20,DFDAT
1097	mov	SCBARRAY+21,DFDAT
1098	mov	SCBARRAY+22,DFDAT
1099
1100# For Linux, we must throw away four bytes since there is a 32bit gap
1101# in the middle of a struct scatterlist
1102#	mov	NONE,DFDAT
1103#	mov	NONE,DFDAT
1104#	mov	NONE,DFDAT
1105#	mov	NONE,DFDAT
1106
1107	mov	SCBARRAY+23,DFDAT
1108	mov	SCBARRAY+24,DFDAT
1109	mov	SCBARRAY+25,DFDAT		#Only support 24 bit length.
1110
1111sg_load3:
1112	ret
1113
1114#  Advance the scatter-gather pointers only IF NEEDED.  If SG is enabled,
1115#  and the SCSI transfer count is zero (note that this should be called
1116#  right after a DMA finishes), then move the working copies of the SG
1117#  pointer/length along.  If the SCSI transfer count is not zero, then
1118#  presumably the target is disconnecting - do not reload the SG values
1119#  next time.
1120#
1121sg_advance:
1122	test	SG_COUNT,0xff	jz sg_advance2	# s/g enabled?
1123
1124	test	STCNT+0,0xff	jnz sg_advance1	# SCSI transfer count nonzero?
1125	test	STCNT+1,0xff	jnz sg_advance1
1126	test	STCNT+2,0xff	jnz sg_advance1
1127
1128	clr	SG_NOLOAD			# reload s/g next time
1129	dec	SG_COUNT			# one less segment to go
1130
1131	clr	A				# add sizeof(struct scatter)
1132	add	SG_NEXT+0,SG_SIZEOF,SG_NEXT+0
1133	adc	SG_NEXT+1,A,SG_NEXT+1
1134	adc	SG_NEXT+2,A,SG_NEXT+2
1135	adc	SG_NEXT+3,A,SG_NEXT+3	ret
1136
1137sg_advance1:
1138	mvi	SG_NOLOAD,0x80			# don't reload s/g next time
1139sg_advance2:
1140	ret
1141
1142#  Add the array base SYNCNEG to the target offset (the target address
1143#  is in SCSIID), and return the result in SINDEX.  The accumulator
1144#  contains the 3->8 decoding of the target ID on return.
1145#
1146ndx_dtr:
1147	shr	A,SCSIID,4
1148	test	SBLKCTL,0x08	jz ndx_dtr_2
1149	or	A,0x08		# Channel B entries add 8
1150ndx_dtr_2:
1151	add	SINDEX,SYNCNEG,A
1152
1153	and	FUNCTION1,0x70,SCSIID		# 3-bit target address decode
1154	mov	A,FUNCTION1	ret
1155
1156#  If we need to negotiate transfer parameters, build the WDTR or SDTR message
1157#  starting at the address passed in SINDEX.  DINDEX is modified on return.
1158#  The SCSI-II spec requires that Wide negotiation occur first and you can
1159#  only negotiat one or the other at a time otherwise in the event of a message
1160#  reject, you wouldn't be able to tell which message was the culpret.
1161#
1162mk_dtr:
1163	mov	DINDEX,SINDEX			# save SINDEX
1164
1165	test	SCBARRAY+0,NEEDWDTR jnz  mk_wdtr_16bit
1166	jmp	mk_sdtr
1167
1168mk_wdtr_16bit:
1169	mvi	ARG_1,BUS_16_BIT
1170mk_wdtr:
1171	mvi	DINDIR,1			# extended message
1172	mvi	DINDIR,2			# extended message length = 2
1173	mvi	DINDIR,3			# WDTR code
1174	mov	DINDIR,ARG_1			# bus width
1175
1176	add	MSG_LEN,-MSG_START+0,DINDEX ret	# update message length
1177	
1178mk_sdtr:
1179	mvi	DINDIR,1			# extended message
1180	mvi	DINDIR,3			# extended message length = 3
1181	mvi	DINDIR,1			# SDTR code
1182	call	sdtr_to_rate
1183	mov	DINDIR,RETURN_1			# REQ/ACK transfer period
1184	and	DINDIR,0xf,SINDIR		# Sync Offset
1185
1186	add	MSG_LEN,-MSG_START+0,DINDEX ret	# update message length
1187
1188# Tag Message if Tag enabled in SCB control block.  Use SCBPTR as the tag
1189# value
1190
1191#mk_tag:
1192#	test	SCBARRAY+0,0x10	jz mk_tag_done	# Tag Enabled?
1193#	and	A,0x03,SCBARRYA+0
1194#	or	A,0x20
1195#	mov	DINDIR,A
1196#	mov	DINDIR,SCBPTR
1197#
1198#	add	MSG_LEN,-MSG_START+0,DINDEX	# update message length
1199
1200#mk_tag_done:
1201#	ret
1202
1203
1204#  Set SCSI bus control signal state.  This also saves the last-written
1205#  value into a location where the higher-level driver can read it - if
1206#  it has to send an ABORT or RESET message, then it needs to know this
1207#  so it can assert ATN without upsetting SCSISIGO.  The new value is
1208#  expected in SINDEX.  Change the actual state last to avoid contention
1209#  from the driver.
1210#
1211scsisig:
1212	mov	SIGSTATE,SINDEX
1213	mov	SCSISIGO,SINDEX	ret
1214
1215sdtr_to_rate:
1216	call	ndx_dtr				# index scratch space for target
1217	shr	A,SINDIR,0x4
1218	dec	SINDEX				#Preserve SINDEX
1219	and	A,0x7
1220	clr	RETURN_1
1221sdtr_to_rate_loop:
1222	test	A,0x0f	jz sdtr_to_rate_done
1223	add	RETURN_1,0x18
1224	dec	A	
1225	jmp	sdtr_to_rate_loop
1226sdtr_to_rate_done:
1227	shr	RETURN_1,0x2
1228	add	RETURN_1,0x18	ret
1229