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