aic7xxx.seq revision 22078
1/*+M***********************************************************************
2 *Adaptec 274x/284x/294x device driver for Linux and FreeBSD.
3 *
4 *Copyright (c) 1994 John Aycock
5 *  The University of Calgary Department of Computer Science.
6 *  All rights reserved.
7 *
8 *FreeBSD, Twin, Wide, 2 command per target support, tagged queuing,
9 *SCB paging and other optimizations:
10 *Copyright (c) 1994, 1995, 1996, 1997 Justin Gibbs. All rights reserved.
11 *
12 *Redistribution and use in source and binary forms, with or without
13 *modification, are permitted provided that the following conditions
14 *are met:
15 *1. Redistributions of source code must retain the above copyright
16 *   notice, this list of conditions, and the following disclaimer.
17 *2. Redistributions in binary form must reproduce the above copyright
18 *   notice, this list of conditions and the following disclaimer in the
19 *   documentation and/or other materials provided with the distribution.
20 *3. All advertising materials mentioning features or use of this software
21 *   must display the following acknowledgement:
22 *     This product includes software developed by the University of Calgary
23 *     Department of Computer Science and its contributors.
24 *4. Neither the name of the University nor the names of its contributors
25 *   may be used to endorse or promote products derived from this software
26 *   without specific prior written permission.
27 *
28 *THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29 *ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 *IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 *ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32 *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 *DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 *OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 *HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 *LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 *OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 *SUCH DAMAGE.
39 *
40 *-M************************************************************************/
41
42VERSION AIC7XXX_SEQ_VER "$FreeBSD: head/sys/dev/aic7xxx/aic7xxx.seq 22078 1997-01-29 05:19:46Z gibbs $"
43
44#if defined(__NetBSD__)
45#include "../../../../dev/ic/aic7xxxreg.h"
46#include "../../../../scsi/scsi_message.h"
47#elif defined(__FreeBSD__)
48#include "../../dev/aic7xxx/aic7xxx_reg.h"
49#include "../../scsi/scsi_message.h"
50#endif
51
52/*
53 * We can't just use ACCUM in the sequencer code because it
54 * must be treated specially by the assembler, and it currently
55 * looks for the symbol 'A'.  This is the only register defined in
56 * the assembler's symbol space.
57 */
58A = ACCUM
59
60/*
61 * A few words on the waiting SCB list:
62 * After starting the selection hardware, we check for reconnecting targets
63 * as well as for our selection to complete just in case the reselection wins
64 * bus arbitration.  The problem with this is that we must keep track of the
65 * SCB that we've already pulled from the QINFIFO and started the selection
66 * on just in case the reselection wins so that we can retry the selection at
67 * a later time.  This problem cannot be resolved by holding a single entry
68 * in scratch ram since a reconnecting target can request sense and this will
69 * create yet another SCB waiting for selection.  The solution used here is to 
70 * use byte 27 of the SCB as a psuedo-next pointer and to thread a list
71 * of SCBs that are awaiting selection.  Since 0-0xfe are valid SCB indexes, 
72 * SCB_LIST_NULL is 0xff which is out of range.  An entry is also added to
73 * this list everytime a request sense occurs or after completing a non-tagged
74 * command for which a second SCB has been queued.  The sequencer will
75 * automatically consume the entries.
76 */
77
78/*
79 * We assume that the kernel driver may reset us at any time, even in the
80 * middle of a DMA, so clear DFCNTRL too.
81 */
82reset:
83	clr	DFCNTRL
84	clr	SCSISIGO		/* De-assert BSY */
85
86p_busfree:
87	or	SXFRCTL0, CLRCHN
88	clr	SCSIRATE		/*
89					 * We don't know the target we will
90					 * connect to, so default to narrow
91					 * transfers to avoid parity problems.
92					 */
93	mvi	SCSISEQ,ENRSELI		/* Always allow reselection */
94	mvi	LASTPHASE, P_BUSFREE
95	and	FLAGS,0x07		/* clear target specific flags */
96poll_for_work:
97	/*
98	 * Are we a twin channel device?
99	 * For fairness, we check the other bus first,
100	 * since we just finished a transaction on the
101	 * current channel.
102	 */
103	test	FLAGS,TWIN_BUS	jz start2
104	xor	SBLKCTL,SELBUSB			/* Toggle to the other bus */
105	test	SSTAT0,SELDI	jnz reselect
106	xor	SBLKCTL,SELBUSB			/* Toggle to the original bus */
107start2:
108	test	SSTAT0,SELDI	jnz reselect
109	cmp	WAITING_SCBH,SCB_LIST_NULL je test_queue
110start_waiting:
111	/*
112	 * Pull the first entry off of the waiting SCB list
113	 * We don't have to "test_busy" because only transactions that
114	 * have passed that test can be in the WAITING_SCB list.
115	 */
116	mov	SCBPTR,WAITING_SCBH
117	jmp	start_scb2
118test_queue:
119	/* Has the driver posted any work for us? */
120	mov	A, QCNTMASK
121	test	QINCNT,A	jz poll_for_work
122
123/*
124 * We have at least one queued SCB now and we don't have any 
125 * SCBs in the list of SCBs awaiting selection.  If we have
126 * any SCBs availible for use, pull the tag from the QINFIFO
127 * and get to work on it.
128 */
129	test	FLAGS, PAGESCBS	jz	dequeue_scb
130	mov	ALLZEROS	call	get_free_or_disc_scb
131	cmp	SINDEX, SCB_LIST_NULL	je poll_for_work
132dequeue_scb:
133	mov	CUR_SCBID,QINFIFO
134	test	FLAGS, PAGESCBS jnz dma_queued_scb
135	/* In the non-paging case, the SCBID == hardware SCB index */
136	mov	SCBPTR, CUR_SCBID
137dma_queued_scb:
138/*
139 * DMA the SCB from host ram into the current SCB location.
140 */
141	mvi	DMAPARAMS, 0xd	/* HDMAEN|DIRECTION|FIFORESET */
142	mov	CUR_SCBID	call dma_scb
143
144/*
145 * See if there is not already an active SCB for this target.  This code
146 * locks out on a per target basis instead of target/lun.  Although this
147 * is not ideal for devices that have multiple luns active at the same
148 * time, it is faster than looping through all SCB's looking for active
149 * commands.  We also don't have enough spare SCB space for us to store the
150 * SCBID of the currently busy transaction for each target/lun making it
151 * impossible to link up the SCBs.
152 */
153test_busy:
154	test	SCB_CONTROL, TAG_ENB	jnz start_scb
155	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
156	mov	SAVED_SCBPTR, SCBPTR
157	mov	SCB_TCL		call	index_untagged_scb
158	mov	ARG_1, SINDIR			/*
159						 * ARG_1 should
160						 * now have the SCB ID of
161						 * any active, non-tagged,
162						 * command for this target.
163						 */
164	cmp	ARG_1, SCB_LIST_NULL je make_busy
165	test	FLAGS, PAGESCBS jz simple_busy_link
166	/*
167	 * Put this SCB back onto the free list.  It
168	 * may be necessary to satisfy the search for
169	 * the active SCB.
170	 */
171	mov	SCBPTR, SAVED_SCBPTR
172	call	add_scb_to_free_list
173	/* Find the active SCB */
174	mov	ALLZEROS	call findSCB
175	/*
176	 * If we couldn't find it, tell the kernel.  This should
177	 * never happen.
178	 */
179	cmp	SINDEX, SCB_LIST_NULL	jne paged_busy_link
180	mvi	INTSTAT, NO_MATCH_BUSY
181paged_busy_link:
182	/* Link us in */
183	mov	SCB_LINKED_NEXT, CUR_SCBID 
184	/* Put it back on the disconnected list */
185	call	add_scb_to_disc_list
186	mvi	SEQCTL,0x10			/* FASTMODE */
187	jmp	poll_for_work
188simple_busy_link:
189	mov	SCBPTR, ARG_1
190	mov	SCB_LINKED_NEXT, CUR_SCBID 
191	mvi	SEQCTL,0x10			/* FASTMODE */
192	jmp	poll_for_work
193make_busy:
194	mov	DINDIR, CUR_SCBID
195	mov	SCBPTR, SAVED_SCBPTR
196	mvi	SEQCTL,0x10			/* FASTMODE */
197
198start_scb:
199	/*
200	 * Place us on the waiting list in case our selection
201	 * doesn't win during bus arbitration.
202	 */
203	mov	SCB_NEXT,WAITING_SCBH
204	mov	WAITING_SCBH, SCBPTR
205start_scb2:
206	and	SINDEX,0xf7,SBLKCTL	/* Clear the channel select bit */
207	and	A,0x08,SCB_TCL		/* Get new channel bit */
208	or	SINDEX,A
209	mov	SBLKCTL,SINDEX		/* select channel */
210	mov	SCB_TCL	call initialize_scsiid
211
212/*
213 * Enable selection phase as an initiator, and do automatic ATN
214 * after the selection.  We do this now so that we can overlap the
215 * rest of our work to set up this target with the arbitration and
216 * selection bus phases.
217 */
218start_selection:
219	mvi	SCSISEQ,0x58		/* ENSELO|ENAUTOATNO|ENRSELI */
220
221/*
222 * As soon as we get a successful selection, the target should go
223 * into the message out phase since we have ATN asserted.  Prepare
224 * the message to send.
225 *
226 * Messages are stored in scratch RAM starting with a length byte
227 * followed by the message itself.
228 */
229
230mk_identify:
231	and	MSG0,0x7,SCB_TCL	/* lun */
232	and	A,DISCENB,SCB_CONTROL	/* mask off disconnect privledge */
233	or	MSG0,A			/* or in disconnect privledge */
234	or	MSG0,MSG_IDENTIFYFLAG
235	mvi	MSG_LEN, 1
236
237/*
238 * Send a tag message if TAG_ENB is set in the SCB control block.
239 * Use SCB_TAG (the position in the kernel's SCB array) as the tag value.
240 */
241mk_tag:
242	test	SCB_CONTROL,TAG_ENB jz  mk_message
243	and	MSG1,0x23,SCB_CONTROL
244	mov	MSG2,SCB_TAG
245	add	MSG_LEN,2	/* update message length */
246
247/*
248 * Interrupt the driver, and allow it to tweak the message buffer
249 * if it asks.
250 */
251mk_message:
252	test	SCB_CONTROL,MK_MESSAGE  jz wait_for_selection
253
254	mvi     INTSTAT,AWAITING_MSG
255
256wait_for_selection:
257	test	SSTAT0,SELDO	jnz select 
258	test	SSTAT0,SELDI	jz wait_for_selection
259
260/*
261 * Reselection has been initiated by a target. Make a note that we've been
262 * reselected, but haven't seen an IDENTIFY message from the target yet.
263 */
264reselect:
265	clr	MSG_LEN		/* Don't have anything in the mesg buffer */
266	/* XXX test for and handle ONE BIT condition */
267	and	SAVED_TCL, 0xf0, SELID
268	or	FLAGS,RESELECTED
269	jmp	select2
270
271/*
272 * After the selection, remove this SCB from the "waiting SCB"
273 * list.  This is achieved by simply moving our "next" pointer into
274 * WAITING_SCBH.  Our next pointer will be set to null the next time this
275 * SCB is used, so don't bother with it now.
276 */
277select:
278	mov	WAITING_SCBH,SCB_NEXT
279select2:
280	/* Turn off the selection hardware */
281	mvi	SCSISEQ,ENAUTOATNP		/*
282						 * ATN on parity errors
283						 * for "in" phases
284						 */
285	mvi	CLRSINT0,0x60			/* CLRSELDI|CLRSELDO */
286	mvi	CLRSINT1,CLRBUSFREE
287	or	SIMODE1, ENBUSFREE		/*
288						 * We aren't expecting a
289						 * bus free, so interrupt
290						 * the kernel driver if it
291						 * happens.
292						 */
293/*
294 * Initialize SCSIRATE with the appropriate value for this target.
295 * The SCSIRATE settings for each target are stored in an array
296 * based at TARG_SCRATCH.
297 */
298ndx_dtr:
299	shr	A,SAVED_TCL,4
300	test	SBLKCTL,SELBUSB	jz ndx_dtr_2
301	or	SAVED_TCL, SELBUSB /* Add the channel bit while we're here */
302	or	A,0x08		/* Channel B entries add 8 */
303ndx_dtr_2:
304	add	SINDEX,TARG_SCRATCH,A
305	mov	SCSIRATE,SINDIR
306
307/*
308 * Initialize Ultra mode setting and clear the SCSI channel.
309 */
310ultra:
311	and	DINDEX,0xdf,SXFRCTL0            /* default to Ultra disabled */
312	mvi	SINDEX, ULTRA_ENB_B
313	test	SAVED_TCL, 0x80     jnz ultra_2     /* Target ID > 7 */
314	test	SBLKCTL, SELBUSB jnz ultra_2     /* Second channel device */
315	dec	SINDEX
316ultra_2:
317	mov     FUNCTION1,SAVED_TCL
318	mov     A,FUNCTION1
319	test	SINDIR, A	jz set_sxfrctl0
320	or	DINDEX, ULTRAEN
321 
322set_sxfrctl0:
323	mov	SXFRCTL0,DINDEX
324
325/*
326 * Main loop for information transfer phases.  If BSY is false, then
327 * we have a bus free condition, expected or not.  Otherwise, wait
328 * for the target to assert REQ before checking MSG, C/D and I/O
329 * for the bus phase.
330 *
331 */
332ITloop:
333	test	SSTAT1,BUSFREE	jnz p_busfree
334	test	SSTAT1,REQINIT	jz ITloop
335
336	and	A,PHASE_MASK,SCSISIGI
337	mov	LASTPHASE,A
338	mov	SCSISIGO,A
339
340	cmp	ALLZEROS,A	je p_dataout
341	cmp	A,P_DATAIN	je p_datain
342	cmp	A,P_COMMAND	je p_command
343	cmp	A,P_MESGOUT	je p_mesgout
344	cmp	A,P_STATUS	je p_status
345	cmp	A,P_MESGIN	je p_mesgin
346
347	mvi	INTSTAT,BAD_PHASE	/* unknown phase - signal driver */
348	jmp	ITloop			/* Try reading the bus again. */
349
350p_dataout:
351	mvi	DMAPARAMS,0x7d			/*
352						 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
353						 * DIRECTION|FIFORESET
354						 */
355	jmp	data_phase_init
356
357/*
358 * If we re-enter the data phase after going through another phase, the
359 * STCNT may have been cleared, so restore it from the residual field.
360 */
361data_phase_reinit:
362	mvi	DINDEX, STCNT0
363	mvi	SCB_RESID_DCNT0	call bcopy_3
364	jmp	data_phase_loop
365
366p_datain:
367	mvi	DMAPARAMS,0x79		/*
368					 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
369					 * !DIRECTION|FIFORESET
370					 */
371data_phase_init:
372	call	assert			/*
373					 * Ensure entering a data
374					 * phase is okay - seen identify, etc.
375					 */
376
377	test	FLAGS, DPHASE	jnz data_phase_reinit
378
379	/*
380	 * Initialize the DMA address and counter from the SCB.
381	 * Also set SG_COUNT and SG_NEXT in memory since we cannot
382	 * modify the values in the SCB itself until we see a
383	 * save data pointers message.
384	 */
385	mvi	DINDEX, HADDR0
386	mvi	SCB_DATAPTR	call bcopy_7
387
388	call	set_stcnt_from_hcnt
389
390	mov	SG_COUNT,SCB_SGCOUNT
391
392	mvi	DINDEX, SG_NEXT
393	mvi	SCB_SGPTR	call bcopy_4
394
395	/* We have seen a data phase */
396	or	FLAGS, DPHASE
397
398data_phase_loop:
399/* Guard against overruns */
400	test	SG_COUNT, 0xff jnz data_phase_inbounds
401/*
402 * Turn on 'Bit Bucket' mode, set the transfer count to
403 * 16meg and let the target run until it changes phase.
404 * When the transfer completes, notify the host that we
405 * had an overrun.
406 */
407	or	SXFRCTL1,BITBUCKET
408	and	DMAPARAMS, 0xf7		/* Turn off HDMAEN */
409	mvi	STCNT0,0xff
410	mvi	STCNT1,0xff
411	mvi	STCNT2,0xff
412
413data_phase_inbounds:
414/* If we are the last SG block, ensure wideodd is off. */
415	cmp	SG_COUNT,0x01 jne data_phase_wideodd
416	and	DMAPARAMS, 0xbf		/* Turn off WIDEODD */
417data_phase_wideodd:
418	mov	DMAPARAMS  call dma
419
420/* Go tell the host about any overruns */
421	test	SXFRCTL1,BITBUCKET jnz data_phase_overrun
422
423/* Exit if we had an underrun */
424	test	SSTAT0,SDONE	jz data_phase_finish /* underrun STCNT != 0 */
425
426/*
427 * Advance the scatter-gather pointers if needed 
428 */
429sg_advance:
430	dec	SG_COUNT	/* one less segment to go */
431
432	test	SG_COUNT, 0xff	jz data_phase_finish /* Are we done? */
433
434	clr	A			/* add sizeof(struct scatter) */
435	add	SG_NEXT0,SG_SIZEOF,SG_NEXT0
436	adc	SG_NEXT1,A,SG_NEXT1
437
438/*
439 * Load a struct scatter and set up the data address and length.
440 * If the working value of the SG count is nonzero, then
441 * we need to load a new set of values.
442 *
443 * This, like all DMA's, assumes little-endian host data storage.
444 */
445sg_load:
446	clr	HCNT2
447	clr	HCNT1
448	mvi	HCNT0,SG_SIZEOF
449
450	mvi	DINDEX, HADDR0
451	mvi	SG_NEXT0	call bcopy_4
452
453	or	DFCNTRL,0xd			/* HDMAEN|DIRECTION|FIFORESET */
454
455	call	dma_finish
456
457/*
458 * Copy data from FIFO into SCB data pointer and data count.  This assumes
459 * that the SG segments are of the form:
460 *
461 * struct ahc_dma_seg {
462 *	u_int32_t	addr;		four bytes, little-endian order
463 *	u_int32_t	len;		four bytes, little endian order
464 * };
465 */
466	mvi	HADDR0	call dfdat_in_7
467
468/* Load STCNT as well.  It is a mirror of HCNT */
469	call	set_stcnt_from_hcnt
470	test	SSTAT1,PHASEMIS  jz data_phase_loop
471
472data_phase_finish:
473/*
474 * After a DMA finishes, save the SG and STCNT residuals back into the SCB
475 * We use STCNT instead of HCNT, since it's a reflection of how many bytes 
476 * were transferred on the SCSI (as opposed to the host) bus.
477 */
478	mov	SCB_RESID_DCNT0,STCNT0
479	mov	SCB_RESID_DCNT1,STCNT1
480	mov	SCB_RESID_DCNT2,STCNT2
481	mov	SCB_RESID_SGCNT, SG_COUNT
482	jmp	ITloop
483
484data_phase_overrun:
485/*
486 * Turn off BITBUCKET mode and notify the host
487 */
488	and	SXFRCTL1,0x7f		/* ~BITBUCKET */
489	mvi	INTSTAT,DATA_OVERRUN
490	jmp	ITloop
491
492/*
493 * Command phase.  Set up the DMA registers and let 'er rip.
494 */
495p_command:
496	call	assert
497
498/*
499 * Load HADDR and HCNT.
500 */
501	mvi	DINDEX, HADDR0
502	mvi	SCB_CMDPTR	call bcopy_5
503	clr	HCNT1
504	clr	HCNT2
505
506	call	set_stcnt_from_hcnt
507
508	mvi	0x3d		call dma	/*  SCSIEN|SDMAEN|HDMAEN
509						 * |DIRECTION|FIFORESET
510						 */
511	jmp	ITloop
512
513/*
514 * Status phase.  Wait for the data byte to appear, then read it
515 * and store it into the SCB.
516 */
517p_status:
518	call	assert
519
520	mov	SCB_TARGET_STATUS, SCSIDATL
521	jmp	ITloop
522
523/*
524 * Message out phase.  If there is not an active message, but the target
525 * took us into this phase anyway, build a no-op message and send it.
526 */
527p_mesgout:
528	test	MSG_LEN, 0xff	jnz  p_mesgout_start
529	mvi	MSG_NOOP	call mk_mesg	/* build NOP message */
530p_mesgout_start:
531/*
532 * Set up automatic PIO transfer from MSG0.  Bit 3 in
533 * SXFRCTL0 (SPIOEN) is already on.
534 */
535	mvi	SINDEX,MSG0
536	mov	DINDEX,MSG_LEN
537
538/*
539 * When target asks for a byte, drop ATN if it's the last one in
540 * the message.  Otherwise, keep going until the message is exhausted.
541 *
542 * Keep an eye out for a phase change, in case the target issues
543 * a MESSAGE REJECT.
544 */
545p_mesgout_loop:
546	/*
547	 * If there is a parity error, wait for the kernel to
548	 * see the interrupt and "update" our message response
549	 * before continuing.
550	 */
551	test	SSTAT1, REQINIT	jz p_mesgout_loop
552	test	SSTAT1, SCSIPERR jnz p_mesgout_loop
553	and	LASTPHASE, PHASE_MASK, SCSISIGI
554	cmp	LASTPHASE, P_MESGOUT jne p_mesgout_done
555/*
556 * If the next bus phase after ATN drops is a message out, it means
557 * that the target is requesting that the last message(s) be resent.
558 */
559p_mesgout_testretry:
560	test	DINDEX,0xff	jnz p_mesgout_dropatn
561	or	SCSISIGO,ATNO,LASTPHASE	/* turn on ATN for the retry */
562	jmp	p_mesgout_start
563p_mesgout_dropatn:
564	cmp	DINDEX,1	jne p_mesgout_outb	/* last byte? */
565	mvi	CLRSINT1,CLRATNO			/* drop ATN */
566p_mesgout_outb:
567	dec	DINDEX
568	mvi	CLRSINT0, CLRSPIORDY
569	mov	SCSIDATL,SINDIR
570	jmp	p_mesgout_loop
571
572p_mesgout_done:
573	mvi	CLRSINT1,CLRATNO	/* Be sure to turn ATNO off */
574	clr	MSG_LEN			/* no active msg */
575	jmp	ITloop
576
577/*
578 * Message in phase.  Bytes are read using Automatic PIO mode.
579 */
580p_mesgin:
581	mvi	A		call inb_first	/* read the 1st message byte */
582	mov	REJBYTE,A			/* save it for the driver */
583
584	test	A,MSG_IDENTIFYFLAG	jnz mesgin_identify
585	cmp	A,MSG_DISCONNECT	je mesgin_disconnect
586	cmp	A,MSG_SAVEDATAPOINTER	je mesgin_sdptrs
587	cmp	ALLZEROS,A		je mesgin_complete
588	cmp	A,MSG_RESTOREPOINTERS	je mesgin_rdptrs
589	cmp	A,MSG_EXTENDED		je mesgin_extended
590	cmp	A,MSG_MESSAGE_REJECT	je mesgin_reject
591
592rej_mesgin:
593/*
594 * We have no idea what this message in is, so we issue a message reject
595 * and hope for the best.  In any case, rejection should be a rare
596 * occurrence - signal the driver when it happens.
597 */
598	mvi	INTSTAT,SEND_REJECT		/* let driver know */
599
600	mvi	MSG_MESSAGE_REJECT	call mk_mesg
601
602mesgin_done:
603	call	inb_last			/*ack & turn auto PIO back on*/
604	jmp	ITloop
605
606
607mesgin_complete:
608/*
609 * We got a "command complete" message, so put the SCB_TAG into the QOUTFIFO,
610 * and trigger a completion interrupt.  Before doing so, check to see if there
611 * is a residual or the status byte is something other than NO_ERROR (0).  In
612 * either of these conditions, we upload the SCB back to the host so it can
613 * process this information.  In the case of a non zero status byte, we 
614 * additionally interrupt the kernel driver synchronously, allowing it to
615 * decide if sense should be retrieved.  If the kernel driver wishes to request
616 * sense, it will fill the kernel SCB with a request sense command and set
617 * RETURN_1 to SEND_SENSE.  If RETURN_1 is set to SEND_SENSE we redownload
618 * the SCB, and process it as the next command by adding it to the waiting list.
619 * If the kernel driver does not wish to request sense, it need only clear
620 * RETURN_1, and the command is allowed to complete normally.  We don't bother
621 * to post to the QOUTFIFO in the error cases since it would require extra
622 * work in the kernel driver to ensure that the entry was removed before the
623 * command complete code tried processing it.
624 */
625
626/*
627 * We expect to go to bus free after this message.
628 */
629	and	SIMODE1, 0xf7		/* ~ENBUSFREE */
630/*
631 * First check for residuals
632 */
633	test	SCB_RESID_SGCNT,0xff	jnz upload_scb
634	test	SCB_TARGET_STATUS,0xff	jz status_ok	/* Good Status? */
635upload_scb:
636	mvi	DMAPARAMS, 0x9	/* HDMAEN | FIFORESET*/
637	mov	SCB_TAG		call dma_scb
638check_status:
639	test	SCB_TARGET_STATUS,0xff	jz status_ok	/* Just a residual? */
640	mvi	INTSTAT,BAD_STATUS			/* let driver know */
641	cmp	RETURN_1, SEND_SENSE	jne status_ok
642	/* This SCB becomes the next to execute as it will retrieve sense */
643	mov	SCB_LINKED_NEXT, SCB_TAG
644	jmp	dma_next_scb
645
646status_ok:
647/* First, mark this target as free. */
648	test	SCB_CONTROL,TAG_ENB jnz complete	/*
649							 * Tagged commands
650							 * don't busy the
651							 * target.
652							 */
653	mov	SAVED_SCBPTR, SCBPTR
654	mov	SAVED_LINKPTR, SCB_LINKED_NEXT
655	mov	SCB_TCL		call	index_untagged_scb
656	mov	DINDIR, SAVED_LINKPTR
657	mov	SCBPTR, SAVED_SCBPTR
658
659complete:
660	/* Post the SCB and issue an interrupt */
661	mov	QOUTFIFO,SCB_TAG
662	mvi	INTSTAT,CMDCMPLT
663
664dma_next_scb:
665	cmp	SCB_LINKED_NEXT, SCB_LIST_NULL	je add_to_free_list
666	test	FLAGS, PAGESCBS jnz dma_next_scb2
667	/* Only DMA on top of ourselves if we are the SCB to download */
668	mov	A, SCB_LINKED_NEXT
669	cmp	SCB_TAG, A	je dma_next_scb2
670	mov	SCBPTR, A
671	jmp	add_to_waiting_list
672dma_next_scb2:
673	mvi	DMAPARAMS, 0xd	/* HDMAEN|DIRECTION|FIFORESET */
674	mov	SCB_LINKED_NEXT		call dma_scb
675add_to_waiting_list:
676	mov	SCB_NEXT,WAITING_SCBH
677	mov	WAITING_SCBH, SCBPTR
678	jmp	mesgin_done
679add_to_free_list:
680	call	add_scb_to_free_list
681	jmp	mesgin_done
682
683/*
684 * Is it an extended message?  Copy the message to our message buffer and
685 * notify the host.  The host will tell us whether to reject this message,
686 * respond to it with the message that the host placed in our message buffer,
687 * or simply to do nothing.
688 */
689mesgin_extended:
690	mvi	MSGIN_EXT_LEN	 call inb_next
691	mov	A, MSGIN_EXT_LEN
692mesgin_extended_loop:
693	mov	DINDEX	call	inb_next
694	dec	A
695	cmp	DINDEX, MSGIN_EXT_LASTBYTE jne mesgin_extended_loop_test
696	dec	DINDEX		/* dump by repeatedly filling the last byte */
697mesgin_extended_loop_test:
698	test	A, 0xFF		jnz mesgin_extended_loop
699mesgin_extended_intr:
700	mvi	INTSTAT,EXTENDED_MSG		/* let driver know */
701	cmp	RETURN_1,SEND_REJ je rej_mesgin
702	cmp	RETURN_1,SEND_MSG jne mesgin_done
703/* The kernel has setup a message to be sent */
704	or	SCSISIGO,ATNO,LASTPHASE		/* turn on ATNO */
705	jmp	mesgin_done
706
707/*
708 * Is it a disconnect message?  Set a flag in the SCB to remind us
709 * and await the bus going free.
710 */
711mesgin_disconnect:
712	and	SIMODE1, 0xf7		/* ~ENBUSFREE */
713	or	SCB_CONTROL,DISCONNECTED
714	test	FLAGS, PAGESCBS jz mesgin_done
715	call	add_scb_to_disc_list
716	jmp	mesgin_done
717
718/*
719 * Save data pointers message:
720 * Copying RAM values back to SCB, for Save Data Pointers message, but
721 * only if we've actually been into a data phase to change them.  This
722 * protects against bogus data in scratch ram and the residual counts
723 * since they are only initialized when we go into data_in or data_out.
724 */
725mesgin_sdptrs:
726	test	FLAGS, DPHASE	jz mesgin_done
727	mov	SCB_SGCOUNT,SG_COUNT
728
729	/* The SCB SGPTR becomes the next one we'll download */
730	mvi	DINDEX, SCB_SGPTR
731	mvi	SG_NEXT0	call bcopy_4
732	
733	/* The SCB DATAPTR0 becomes the current SHADDR */
734	mvi	DINDEX, SCB_DATAPTR0
735	mvi	SHADDR0		call bcopy_4
736
737/*
738 * Use the residual number since STCNT is corrupted by any message transfer.
739 */
740	mvi	SCB_RESID_DCNT0	call	bcopy_3
741
742	jmp	mesgin_done
743
744/*
745 * Restore pointers message?  Data pointers are recopied from the
746 * SCB anytime we enter a data phase for the first time, so all
747 * we need to do is clear the DPHASE flag and let the data phase
748 * code do the rest.
749 */
750mesgin_rdptrs:
751	and	FLAGS,0xef			/*
752						 * !DPHASE we'll reload them
753						 * the next time through
754						 */
755	jmp	mesgin_done
756
757/*
758 * Identify message?  For a reconnecting target, this tells us the lun
759 * that the reconnection is for - find the correct SCB and switch to it,
760 * clearing the "disconnected" bit so we don't "find" it by accident later.
761 */
762mesgin_identify:
763	test	A,0x78	jnz rej_mesgin	/*!DiscPriv|!LUNTAR|!Reserved*/
764	and	A,0x07			/* lun in lower three bits */
765	or      SAVED_TCL,A		/* SAVED_TCL should be complete now */
766	call	inb_last		/* ACK */
767
768/*
769 * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
770 * If we get one, we use the tag returned to switch to find the proper
771 * SCB.  With SCB paging, this requires using findSCB for both tagged
772 * and non-tagged transactions since the SCB may exist in any slot.
773 * If we're not using SCB paging, we can use the tag as the direct
774 * index to the SCB.
775 */
776	mvi	ARG_1,SCB_LIST_NULL	/* Default to no-tag */
777snoop_tag_loop:
778	test	SSTAT1,REQINIT		jz snoop_tag_loop
779	and	LASTPHASE, PHASE_MASK, SCSISIGI
780	cmp	LASTPHASE, P_MESGIN, jne use_findSCB
781	mvi	A			call inb_first
782	cmp	A,MSG_SIMPLE_Q_TAG	jne use_findSCB
783get_tag:
784	or	FLAGS, TAGGED_SCB
785	mvi	ARG_1	call inb_next	/* tag value */
786/*
787 * See if the tag is in range.  The tag is < SCBCOUNT if we add
788 * the complement of SCBCOUNT to the incomming tag and there is
789 * no carry.
790 */
791	mov	A,COMP_SCBCOUNT	
792	add	SINDEX,A,ARG_1
793	jc	send_abort_msg
794
795/*
796 * Ensure that the SCB the tag points to is for an SCB transaction
797 * to the reconnecting target.
798 */
799	test	FLAGS, PAGESCBS	jz index_by_tag
800use_findSCB:
801	mov	ALLZEROS	call findSCB	  /* Have to search */
802	cmp	SINDEX, SCB_LIST_NULL, je not_found
803setup_SCB:
804	and	SCB_CONTROL,0xfb	  /* clear disconnect bit in SCB */
805	or	FLAGS,IDENTIFY_SEEN	  /* make note of IDENTIFY */
806	test	SCB_CONTROL,TAG_ENB	jnz  mesgin_done /* Ack Tag */
807	jmp	ITloop
808index_by_tag:
809	mov	SCBPTR,ARG_1
810	mov	A, SAVED_TCL
811	cmp	SCB_TCL,A		jne send_abort_msg
812	test	SCB_CONTROL,TAG_ENB	jz  send_abort_msg
813	jmp	setup_SCB
814
815not_found:
816	mvi	INTSTAT, NO_MATCH
817send_abort_msg:
818	test	FLAGS, TAGGED_SCB jnz abort_tag_msg
819	mvi	MSG_ABORT	call mk_mesg
820	jmp	mesgin_done
821abort_tag_msg:
822	mvi	MSG_ABORT_TAG	call mk_mesg	/* ABORT TAG message */
823	jmp	mesgin_done
824
825/*
826 * Message reject?  Let the kernel driver handle this.  If we have an 
827 * outstanding WDTR or SDTR negotiation, assume that it's a response from 
828 * the target selecting 8bit or asynchronous transfer, otherwise just ignore 
829 * it since we have no clue what it pertains to.
830 */
831mesgin_reject:
832	mvi	INTSTAT, REJECT_MSG
833	jmp	mesgin_done
834
835/*
836 * [ ADD MORE MESSAGE HANDLING HERE ]
837 */
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	MSG_LEN,0xff	jz mk_mesg1	/* Should always succeed */
846	
847	/*
848	 * Hmmm.  For some reason the mesg buffer is in use.
849	 * Tell the driver.  It should look at SINDEX to find
850	 * out what we wanted to use the buffer for and resolve
851	 * the conflict.
852	 */
853	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
854	mvi	INTSTAT,MSG_BUFFER_BUSY
855
856mk_mesg1:
857	or	SCSISIGO,ATNO,LASTPHASE	/* turn on ATNO */
858	mvi	MSG_LEN,1		/* length = 1 */
859	mov	MSG0,SINDEX		/* 1-byte message */
860	mvi	SEQCTL,0x10	ret	/* !PAUSEDIS|FASTMODE */
861
862/*
863 * Functions to read data in Automatic PIO mode.
864 *
865 * According to Adaptec's documentation, an ACK is not sent on input from
866 * the target until SCSIDATL is read from.  So we wait until SCSIDATL is
867 * latched (the usual way), then read the data byte directly off the bus
868 * using SCSIBUSL.  When we have pulled the ATN line, or we just want to
869 * acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
870 * spec guarantees that the target will hold the data byte on the bus until
871 * we send our ACK.
872 *
873 * The assumption here is that these are called in a particular sequence,
874 * and that REQ is already set when inb_first is called.  inb_{first,next}
875 * use the same calling convention as inb.
876 */
877
878inb_next:
879	mov	NONE,SCSIDATL			/*dummy read from latch to ACK*/
880inb_next_wait:
881	/*
882	 * If there is a parity error, wait for the kernel to
883	 * see the interrupt and prepare our message response
884	 * before continuing.
885	 */
886	test	SSTAT1, REQINIT	jz inb_next_wait
887	test	SSTAT1, SCSIPERR jnz inb_next_wait
888	and	LASTPHASE, PHASE_MASK, SCSISIGI
889	cmp	LASTPHASE, P_MESGIN jne mesgin_phasemis
890inb_first:
891	mov	DINDEX,SINDEX
892	mov	DINDIR,SCSIBUSL	ret		/*read byte directly from bus*/
893inb_last:
894	mov	NONE,SCSIDATL ret		/*dummy read from latch to ACK*/
895
896mesgin_phasemis:
897/*
898 * We expected to receive another byte, but the target changed phase
899 */
900	mvi	INTSTAT, MSGIN_PHASEMIS
901	jmp	ITloop
902
903/*
904 * DMA data transfer.  HADDR and HCNT must be loaded first, and
905 * SINDEX should contain the value to load DFCNTRL with - 0x3d for
906 * host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
907 * during initialization.
908 */
909dma:
910	mov	DFCNTRL,SINDEX
911dma1:
912	test	SSTAT0,DMADONE	jnz dma3
913	test	SSTAT1,PHASEMIS	jz dma1		/* ie. underrun */
914
915/*
916 * We will be "done" DMAing when the transfer count goes to zero, or
917 * the target changes the phase (in light of this, it makes sense that
918 * the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
919 * doing a SCSI->Host transfer, the data FIFO should be flushed auto-
920 * magically on STCNT=0 or a phase change, so just wait for FIFO empty
921 * status.
922 */
923dma3:
924	test	SINDEX,DIRECTION	jnz dma5
925dma4:
926	test	DFSTATUS,FIFOEMP	jz dma4
927
928/*
929 * Now shut the DMA enables off and make sure that the DMA enables are 
930 * actually off first lest we get an ILLSADDR.
931 */
932dma5:
933	/* Don't clobber an inprogress host data transfer */
934	test	DFSTATUS, MREQPEND	jnz dma5
935	/* disable DMA */
936	and	DFCNTRL, 0xc7		/* ~(SCSIEN|SDMAEN|HDMAEN|DIRECTION) */
937dma6:
938	test	DFCNTRL, 0x38	jnz dma6  /* (SCSIEN|SDMAEN|HDMAEN|DIRECTION) */
939return:
940	ret
941
942/*
943 * Common SCSI initialization for selection and reselection.  Expects
944 * the target SCSI ID to be in the upper four bits of SINDEX, and A's
945 * contents are stomped on return.
946 */
947initialize_scsiid:
948	and	SINDEX,0xf0		/* Get target ID */
949	mov	SAVED_TCL, SINDEX	/* Update the target portion of this */
950	and	A,0x0f,SCSIID
951	or	SINDEX,A
952	mov	SCSIID,SINDEX ret
953
954/*
955 * Assert that if we've been reselected, then we've seen an IDENTIFY
956 * message.
957 */
958assert:
959	test	FLAGS,RESELECTED	jz return	/* reselected? */
960	test	FLAGS,IDENTIFY_SEEN	jnz return	/* seen IDENTIFY? */
961
962	mvi	INTSTAT,NO_IDENT 	ret	/* no - tell the kernel */
963
964/*
965 * Locate a disconnected SCB either by SAVED_TCL (ARG_1 is SCB_LIST_NULL)
966 * or by the SCBIDn ARG_1.  The search begins at the SCB index passed in
967 * via SINDEX.  If the SCB cannot be found, SINDEX will be SCB_LIST_NULL,
968 * otherwise, SCBPTR is set to the proper SCB.
969 */
970findSCB:
971	mov	SCBPTR,SINDEX			/* switch to next SCB */
972	test	SCB_CONTROL,DISCONNECTED jz findSCB1 /*should be disconnected*/
973	cmp	ARG_1, SCB_LIST_NULL	jne findBySCBID
974	mov	A, SAVED_TCL
975	cmp	SCB_TCL,A	je foundSCB /* target ID/channel/lun match? */
976findSCB1:
977	inc	SINDEX
978	mov	A,SCBCOUNT
979	cmp	SINDEX,A	jne findSCB
980/*
981 * We didn't find it.  If we're paging, pull an SCB and DMA down the
982 * one we want.  If we aren't paging or the SCB we dma down has the
983 * abort flag set, return not found.
984 */
985	test	FLAGS, PAGESCBS	jz find_error
986	mov	ALLZEROS	call	get_free_or_disc_scb
987	cmp	ARG_1, SCB_LIST_NULL jne find_dma_scb
988	mov	SAVED_TCL	call	index_untagged_scb
989	mov	ARG_1, SINDIR	/* SCBID of SCB to fetch */
990find_dma_scb:
991	mvi	DMAPARAMS, 0xd	/* HDMAEN|DIRECTION|FIFORESET */
992	mov	ARG_1	call dma_scb
993	test	SCB_CONTROL, ABORT_SCB jz return
994	call	add_scb_to_free_list
995find_error:
996	mvi	SINDEX, SCB_LIST_NULL ret
997findBySCBID:
998	mov	A, ARG_1			/* Tag passed in ARG_1 */
999	cmp	SCB_TAG,A	jne findSCB1	/* Found it? */
1000foundSCB:
1001	test	SCB_CONTROL, ABORT_SCB jnz find_error
1002	test	FLAGS,PAGESCBS	jz return
1003rem_scb_from_disc_list:
1004/* Remove this SCB from the disconnection list */
1005	cmp	SCB_NEXT,SCB_LIST_NULL je unlink_prev
1006	mov	SAVED_LINKPTR, SCB_PREV
1007	mov	SCBPTR, SCB_NEXT
1008	mov	SCB_PREV, SAVED_LINKPTR
1009	mov	SCBPTR, SINDEX
1010unlink_prev:
1011	cmp	SCB_PREV,SCB_LIST_NULL	je rHead/* At the head of the list */
1012	mov	SAVED_LINKPTR, SCB_NEXT
1013	mov	SCBPTR, SCB_PREV
1014	mov	SCB_NEXT, SAVED_LINKPTR
1015	mov	SCBPTR, SINDEX ret
1016rHead:
1017	mov	DISCONNECTED_SCBH,SCB_NEXT ret
1018
1019set_stcnt_from_hcnt:
1020	mov	STCNT0, HCNT0
1021	mov	STCNT1, HCNT1
1022	mov	STCNT2, HCNT2 ret
1023
1024bcopy_7:
1025	mov	DINDIR, SINDIR
1026	mov	DINDIR, SINDIR
1027bcopy_5:
1028	mov	DINDIR, SINDIR
1029bcopy_4:
1030	mov	DINDIR, SINDIR
1031bcopy_3:
1032	mov	DINDIR, SINDIR
1033	mov	DINDIR, SINDIR
1034	mov	DINDIR, SINDIR ret
1035
1036dma_scb:
1037	/*
1038	 * SCB index is in SINDEX.  Determine the physical address in
1039	 * the host where this SCB is located and load HADDR with it.
1040	 */
1041	shr	DINDEX, SINDEX, 3
1042	shl	A, SINDEX, 5
1043	add	HADDR0, A, HSCB_ADDR0
1044	mov	A, DINDEX
1045	adc	HADDR1, A, HSCB_ADDR1
1046	clr	A
1047	adc	HADDR2, A, HSCB_ADDR2
1048	adc	HADDR3, A, HSCB_ADDR3
1049	/* Setup Count */
1050	mvi	HCNT0, 28
1051	clr	HCNT1
1052	clr	HCNT2
1053	mov	DFCNTRL, DMAPARAMS
1054	test	DMAPARAMS, DIRECTION	jnz dma_scb_fromhost
1055	/* Fill it with the SCB data */
1056	call	copy_scb_tofifo
1057	mvi	DFCNTRL, 0xa		/* HDMAEN | FIFOFLUSH */
1058dma_scb_fromhost:
1059	call	dma_finish
1060	/* If we were putting the SCB, we are done */
1061	test	DMAPARAMS, DIRECTION	jz	return
1062	mvi	SCBARRAY  call dfdat_in_7
1063	call	dfdat_in_7_continued
1064	call	dfdat_in_7_continued
1065	jmp	dfdat_in_7_continued
1066dfdat_in_7:
1067	mov     DINDEX,SINDEX
1068dfdat_in_7_continued:
1069	mov	DINDIR,DFDAT
1070	mov	DINDIR,DFDAT
1071	mov	DINDIR,DFDAT
1072	mov	DINDIR,DFDAT
1073	mov	DINDIR,DFDAT
1074	mov	DINDIR,DFDAT
1075	mov	DINDIR,DFDAT ret
1076
1077copy_scb_tofifo:
1078	mvi	SCBARRAY  call dfdat_out_7
1079	call	dfdat_out_7
1080	call	dfdat_out_7
1081dfdat_out_7:
1082	mov	DFDAT,SINDIR
1083	mov	DFDAT,SINDIR
1084	mov	DFDAT,SINDIR
1085	mov	DFDAT,SINDIR
1086	mov	DFDAT,SINDIR
1087	mov	DFDAT,SINDIR
1088	mov	DFDAT,SINDIR ret
1089
1090/*
1091 * Wait for DMA from host memory to data FIFO to complete, then disable
1092 * DMA and wait for it to acknowledge that it's off.
1093 */
1094dma_finish:
1095	test	DFSTATUS,HDONE	jz dma_finish
1096	/* Turn off DMA preserving WIDEODD */
1097	and	DFCNTRL,WIDEODD
1098dma_finish2:
1099	test	DFCNTRL,HDMAENACK jnz dma_finish2
1100	ret
1101
1102index_untagged_scb:
1103	mov	DINDEX, SINDEX
1104	shr	DINDEX, 4
1105	and	DINDEX, 0x03			/* Bottom two bits of tid */
1106	add	DINDEX, SCB_ACTIVE0
1107	shr	A, SINDEX, 6			/* Target ID divided by 4 */
1108	test	SINDEX, SELBUSB jz index_untagged_scb2
1109	add	A, 2				/* Add 2 positions */
1110index_untagged_scb2:
1111	mov	SCBPTR, A			/*
1112						 * Select the SCB with this 
1113						 * target's information.
1114						 */
1115	mov	SINDEX, DINDEX	ret
1116
1117
1118get_free_or_disc_scb:
1119	cmp	FREE_SCBH, SCB_LIST_NULL jne dequeue_free_scb
1120	cmp	DISCONNECTED_SCBH, SCB_LIST_NULL jne dequeue_disc_scb
1121return_error:
1122	mvi	SINDEX, SCB_LIST_NULL	ret
1123dequeue_disc_scb:
1124	mov	SCBPTR, DISCONNECTED_SCBH
1125/*
1126 * If we have a residual, then we are in the middle of some I/O
1127 * and we have to send this SCB back up to the kernel so that the
1128 * saved data pointers and residual information isn't lost.
1129 */
1130	test	SCB_RESID_SGCNT,0xff	jz unlink_disc_scb
1131	mvi	DMAPARAMS, 0x9	/* HDMAEN | FIFORESET*/
1132	mov	SCB_TAG		call dma_scb
1133unlink_disc_scb:
1134	/* jmp instead of call since we want to return anyway */
1135	mov	SCBPTR	jmp rem_scb_from_disc_list
1136dequeue_free_scb:
1137	mov	SCBPTR, FREE_SCBH
1138	mov	FREE_SCBH, SCB_NEXT ret
1139
1140add_scb_to_free_list:
1141	mov	SCB_NEXT, FREE_SCBH
1142	mov	FREE_SCBH, SCBPTR ret
1143
1144add_scb_to_disc_list:
1145/*
1146 * Link this SCB into the DISCONNECTED list.  This list holds the
1147 * candidates for paging out an SCB if one is needed for a new command.
1148 * Modifying the disconnected list is a critical(pause dissabled) section.
1149 */
1150	mvi	SCB_PREV, SCB_LIST_NULL
1151	mov	SCB_NEXT, DISCONNECTED_SCBH
1152	mov	DISCONNECTED_SCBH, SCBPTR
1153	cmp	SCB_NEXT,SCB_LIST_NULL je return
1154	mov	SCBPTR,SCB_NEXT
1155	mov	SCB_PREV,DISCONNECTED_SCBH
1156	mov	SCBPTR,DISCONNECTED_SCBH ret
1157
1158