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