aic7xxx.seq revision 13313
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 *Modifications/enhancements:
9 *  Copyright (c) 1994, 1995 Justin Gibbs. All rights reserved.
10 *
11 *Redistribution and use in source and binary forms, with or without
12 *modification, are permitted provided that the following conditions
13 *are met:
14 *1. Redistributions of source code must retain the above copyright
15 *   notice, this list of conditions, and the following disclaimer.
16 *2. Redistributions in binary form must reproduce the above copyright
17 *   notice, this list of conditions and the following disclaimer in the
18 *   documentation and/or other materials provided with the distribution.
19 *3. All advertising materials mentioning features or use of this software
20 *   must display the following acknowledgement:
21 *     This product includes software developed by the University of Calgary
22 *     Department of Computer Science and its contributors.
23 *4. Neither the name of the University nor the names of its contributors
24 *   may be used to endorse or promote products derived from this software
25 *   without specific prior written permission.
26 *
27 *THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28 *ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 *IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 *ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 *DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 *OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 *HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 *LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 *OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 *SUCH DAMAGE.
38 *
39 *FreeBSD, Twin, Wide, 2 command per target support, tagged queuing and other 
40 *optimizations provided by Justin T. Gibbs (gibbs@FreeBSD.org)
41 *
42 *-M************************************************************************/
43
44VERSION AIC7XXX_SEQ_VER "$Id: aic7xxx.seq,v 1.26 1996/01/05 16:11:49 gibbs Exp $"
45
46#include "../../dev/aic7xxx/aic7xxx_reg.h"
47
48/*
49 * We can't just use ACCUM in the sequencer code because it
50 * must be treated specially by the assembler, and it currently
51 * looks for the symbol 'A'.  This is the only register defined
52 * the assembler's symbol space.
53 */
54A = ACCUM
55
56/* After starting the selection hardware, we return to the "poll_for_work"
57 * loop so that we can check for reconnecting targets as well as for our
58 * selection to complete just in case the reselection wins bus arbitration.
59 * The problem with this is that we must keep track of the SCB that we've
60 * already pulled from the QINFIFO and started the selection on just in case
61 * the reselection wins so that we can retry the selection at a later time.
62 * This problem cannot be resolved by holding a single entry in scratch
63 * ram since a reconnecting target can request sense and this will create
64 * yet another SCB waiting for selection.  The solution used here is to 
65 * use byte 31 of the SCB as a psuedo-next pointer and to thread a list
66 * of SCBs that are awaiting selection.  Since 0-0xfe are valid SCB offsets, 
67 * SCB_LIST_NULL is 0xff which is out of range.  The kernel driver must
68 * add an entry to this list everytime a request sense occurs.  The sequencer
69 * will automatically consume the entries.
70 */
71
72/*
73 * Initialize any state idle loop state here.  This code is executed on
74 * startup and after every bus free.
75 */
76start:
77	mvi	SCSISEQ,ENRSELI		/* Always allow reselection */
78poll_for_work:
79	/*
80	 * Are we a twin channel device?
81	 * For fairness, we check the other bus first,
82	 * since we just finished a transaction on the
83	 * current channel.
84	 */
85	test	FLAGS,TWIN_BUS	jz start2
86	xor	SBLKCTL,SELBUSB			/* Toggle to the other bus */
87	test	SSTAT0,SELDI	jnz reselect
88	xor	SBLKCTL,SELBUSB			/* Toggle to the original bus */
89start2:
90	test	SSTAT0,SELDI	jnz reselect
91	cmp	WAITING_SCBH,SCB_LIST_NULL jne start_waiting
92	test	QINCNT,0xff	jz poll_for_work
93
94/* We have at least one queued SCB now and we don't have any 
95 * SCBs in the list of SCBs awaiting selection.  Set the SCB
96 * pointer from the FIFO so we see the right bank of SCB 
97 * registers.
98 */
99	mov	SCBPTR,QINFIFO
100
101/*
102 * If the control byte of this SCB has the NEEDDMA flag set, we have
103 * yet to DMA it from host memory
104 */
105
106test    SCB_CONTROL,NEEDDMA	jz test_busy    
107	clr	HCNT2
108	clr	HCNT1
109	mvi	HCNT0,SCB_SIZEOF
110
111	mvi	DINDEX,HADDR      
112	mvi	SCB_PHYSADDR	call bcopy_4
113        
114	mvi	DFCNTRL,0xd	/* HDMAEN|DIRECTION|FIFORESET */
115
116/*
117 * Wait for DMA from host memory to data FIFO to complete, then disable
118 * DMA and wait for it to acknowledge that it's off.
119 */
120	call	dma_finish
121
122/* Copy the SCB from the FIFO to  the SCBARRAY */
123
124	mvi	DINDEX, SCBARRAY
125	call	bcopy_5_dfdat 
126	call	bcopy_7_dfdat
127	call	bcopy_7_dfdat
128	call	bcopy_7_dfdat   
129
130/*
131 * See if there is not already an active SCB for this target.  This code
132 * locks out on a per target basis instead of target/lun.  Although this
133 * is not ideal for devices that have multiple luns active at the same
134 * time, it is faster than looping through all SCB's looking for active
135 * commands.  It may be benificial to make findscb a more general procedure
136 * to see if the added cost of the search is negligible.  This code also 
137 * assumes that the kernel driver will clear the active flags on board 
138 * initialization, board reset, and a target SELTO.  Tagged commands
139 * don't set the active bits since you can have more than queue more
140 * than one command at a time.  We do, however, look to see if there
141 * are any non-tagged I/Os in progress, and requeue the command if
142 * there are.  Tagged and non-tagged commands cannot be mixed to a 
143 * single target.
144 */
145
146test_busy:
147	mov	FUNCTION1,SCB_TCL
148	mov	A,FUNCTION1
149	test	SCB_TCL,0x88	jz test_a	/* Id < 8 && A channel */
150
151	test	ACTIVE_B,A	jnz requeue
152	test	SCB_CONTROL,TAG_ENB	jnz start_scb
153	/* Mark the current target as busy */
154	or	ACTIVE_B,A
155	jmp	start_scb
156
157/* Place the currently active SCB back on the queue for later processing */
158requeue:
159	mov	QINFIFO, SCBPTR
160	jmp	poll_for_work
161
162/*
163 * Pull the first entry off of the waiting for selection list
164 * We don't have to "test_busy" because only transactions that
165 * have passed that test can be in the waiting_scb list.
166 */
167start_waiting:
168	mov	SCBPTR,WAITING_SCBH
169	jmp	start_scb2
170
171test_a:
172	test	ACTIVE_A,A jnz requeue
173	test	SCB_CONTROL,TAG_ENB jnz start_scb
174	/* Mark the current target as busy */
175	or	ACTIVE_A,A
176
177start_scb:
178	mov	SCB_NEXT_WAITING,WAITING_SCBH
179	mov	WAITING_SCBH, SCBPTR
180start_scb2:
181	and	SINDEX,0xf7,SBLKCTL	/* Clear the channel select bit */
182	and	A,0x08,SCB_TCL		/* Get new channel bit */
183	or	SINDEX,A
184	mov	SBLKCTL,SINDEX		/* select channel */
185	mov	SCB_TCL	call initialize_scsiid
186
187/*
188 * Enable selection phase as an initiator, and do automatic ATN
189 * after the selection.  We do this now so that we can overlap the
190 * rest of our work to set up this target with the arbitration and
191 * selection bus phases.
192 */
193start_selection:
194	mvi	SCSISEQ,0x58		/* ENSELO|ENAUTOATNO|ENRSELI */
195
196/*
197 * As soon as we get a successful selection, the target should go
198 * into the message out phase since we have ATN asserted.  Prepare
199 * the message to send.
200 *
201 * Messages are stored in scratch RAM starting with a length byte
202 * followed by the message itself.
203 */
204	test	SCB_CMDLEN,0xff jnz mk_identify	/* 0 Length Command? */
205
206/*
207 * The kernel has sent us an SCB with no command attached.  This implies
208 * that the kernel wants to send a message of some sort to this target,
209 * so we interrupt the driver, allow it to fill the message buffer, and
210 * then go back into the arbitration loop
211 */
212	mvi     INTSTAT,AWAITING_MSG
213	jmp     wait_for_selection
214
215mk_identify:
216	and	A,DISCENB,SCB_CONTROL	/* mask off disconnect privledge */
217
218	and	SINDEX,0x7,SCB_TCL	/* lun */
219	or	SINDEX,A		/* or in disconnect privledge */
220	or	SINDEX,MSG_IDENTIFY call mk_mesg /* IDENTIFY message */
221
222	test	SCB_CONTROL,0xb0 jz  !message	/* WDTR, SDTR or TAG?? */
223/*
224 * Tag Message if Tag enabled in SCB control block.  Use SCBPTR as the tag
225 * value
226 */
227
228mk_tag:
229	mvi	DINDEX, MSG1
230	test	SCB_CONTROL,TAG_ENB jz mk_tag_done
231	and	A,0x23,SCB_CONTROL
232	mov	DINDIR,A
233	mov	DINDIR,SCBPTR
234
235	add	MSG_LEN,COMP_MSG0,DINDEX	/* update message length */
236
237mk_tag_done:
238
239	mov	DINDEX	call mk_dtr	/* build DTR message if needed */
240
241!message:
242wait_for_selection:
243	test	SSTAT0,SELDO	jnz select 
244	test	SSTAT0,SELDI	jz wait_for_selection
245
246/*
247 * Reselection has been initiated by a target. Make a note that we've been
248 * reselected, but haven't seen an IDENTIFY message from the target
249 * yet.
250 */
251reselect:
252	clr	MSG_LEN		/* Don't have anything in the mesg buffer */
253	mov	SELID		call initialize_scsiid
254	mvi	SAVED_TCL, 0xff	/*
255				 * Fill with an imposible value so we
256				 * don't get false hits for a tag
257				 * without an identify.
258				 */
259	and	FLAGS,0x03	/* clear target specific flags */
260	or	FLAGS,RESELECTED
261	jmp	select2
262
263/*
264 * After the selection, remove this SCB from the "waiting for selection"
265 * list.  This is achieved by simply moving our "next" pointer into
266 * WAITING_SCBH.  Our next pointer will be set to null the next time this
267 * SCB is used, so don't bother with it now.
268 */
269select:
270	and	FLAGS,0x03		/* Clear target flags */
271	or	SCB_CONTROL,NEEDDMA
272
273	/*
274	 * Some drives will issue a simple tag message during
275	 * a tagged selection if they are immediately ready
276	 * to handle the command without a disconnect.  Ensure
277	 * that SAVED_TCL (used in get_tag) is inialized correctly
278	 * during a selection for this reason.
279	 */
280	mov	SAVED_TCL, SCB_TCL
281	mov	WAITING_SCBH,SCB_NEXT_WAITING
282select2:
283/*
284 * Set CLRCHN here before the target has entered a data transfer mode -
285 * with synchronous SCSI, if you do it later, you blow away some
286 * data in the SCSI FIFO that the target has already sent to you.
287 */
288	clr	SIGSTATE 
289
290	or	SXFRCTL0,CLRCHN
291/*
292 * Initialize SCSIRATE with the appropriate value for this target.
293 */
294	call	ndx_dtr
295	mov	SCSIRATE,SINDIR
296
297	mvi	SCSISEQ,ENAUTOATNP		/*
298						 * ATN on parity errors
299						 * for "in" phases
300						 */
301	mvi	CLRSINT1,CLRBUSFREE
302	mvi	CLRSINT0,0x60			/* CLRSELDI|CLRSELDO */
303
304/*
305 * Main loop for information transfer phases.  If BSY is false, then
306 * we have a bus free condition, expected or not.  Otherwise, wait
307 * for the target to assert REQ before checking MSG, C/D and I/O
308 * for the bus phase.
309 *
310 */
311ITloop:
312	test	SSTAT1,BUSFREE	jnz p_busfree
313	test	SSTAT1,REQINIT	jz ITloop
314
315	and	A,PHASE_MASK,SCSISIGI
316
317	mov	A		call scsisig
318	cmp	ALLZEROS,A	je p_dataout
319	cmp	A,P_DATAIN	je p_datain
320	cmp	A,P_COMMAND	je p_command
321	cmp	A,P_MESGOUT	je p_mesgout
322	cmp	A,P_STATUS	je p_status
323	cmp	A,P_MESGIN	je p_mesgin
324
325	mvi	INTSTAT,BAD_PHASE	/* unknown phase - signal driver */
326
327p_dataout:
328	mvi	DMAPARAMS,0x7d			/*
329						 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
330						 * DIRECTION|FIFORESET
331						 */
332	jmp	data_phase_init
333
334/*
335 * If we re-enter the data phase after going through another phase, the
336 * STCNT may have been cleared, so restore it from the residual field.
337 */
338data_phase_reinit:
339	mvi	DINDEX, STCNT
340	mvi	SCB_RESID_DCNT	call bcopy_3
341	jmp	data_phase_loop
342
343p_datain:
344	mvi	DMAPARAMS,0x79		/*
345					 * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
346					 * !DIRECTION|FIFORESET
347					 */
348data_phase_init:
349	call	assert
350
351	test	FLAGS, DPHASE	jnz data_phase_reinit
352	call	sg_scb2ram
353	or	FLAGS, DPHASE		/* We have seen a data phase */
354
355data_phase_loop:
356/* If we are the last SG block, don't set wideodd. */
357	cmp	SG_COUNT,0x01 jne data_phase_wideodd
358	and	DMAPARAMS, 0xbf		/* Turn off WIDEODD */
359data_phase_wideodd:
360	mov	DMAPARAMS  call dma
361
362/* Exit if we had an underrun */
363	test	SSTAT0,SDONE	jz data_phase_finish /* underrun STCNT != 0 */
364
365/*
366 * Advance the scatter-gather pointers if needed 
367 */
368sg_advance:
369	dec	SG_COUNT	/* one less segment to go */
370
371	test	SG_COUNT, 0xff	jz data_phase_finish /* Are we done? */
372
373	clr	A			/* add sizeof(struct scatter) */
374	add	SG_NEXT0,SG_SIZEOF,SG_NEXT0
375	adc	SG_NEXT1,A,SG_NEXT1
376	adc	SG_NEXT2,A,SG_NEXT2
377	adc	SG_NEXT3,A,SG_NEXT3
378
379/*
380 * Load a struct scatter and set up the data address and length.
381 * If the working value of the SG count is nonzero, then
382 * we need to load a new set of values.
383 *
384 * This, like all DMA's, assumes a little-endian host data storage.
385 */
386sg_load:
387	clr	HCNT2
388	clr	HCNT1
389	mvi	HCNT0,SG_SIZEOF
390
391	mvi	DINDEX,HADDR
392	mvi	SG_NEXT		call bcopy_4
393
394	mvi	DFCNTRL,0xd			/* HDMAEN|DIRECTION|FIFORESET */
395
396/*
397 * Wait for DMA from host memory to data FIFO to complete, then disable
398 * DMA and wait for it to acknowledge that it's off.
399 */
400	call	dma_finish
401
402/*
403 * Copy data from FIFO into SCB data pointer and data count.  This assumes
404 * that the struct scatterlist has this structure (this and sizeof(struct
405 * scatterlist) == 12 are asserted in aic7xxx.c):
406 *
407 *	struct scatterlist {
408 *		char *address;		four bytes, little-endian order
409 *		...			four bytes, ignored
410 *		unsigned short length;	two bytes, little-endian order
411 *	}
412 *
413 *
414 * Not in FreeBSD.  the scatter list entry is only 8 bytes.
415 * 
416 * struct ahc_dma_seg {
417 *       physaddr addr;                  four bytes, little-endian order
418 *       long    len;                    four bytes, little endian order
419 * };
420 */
421
422/*
423 * For Linux, we must throw away four bytes since there is a 32bit gap
424 * in the middle of a struct scatterlist
425 */
426#ifdef LINUX
427	call	bcopy_4_dfdat
428	mov	NONE,DFDAT
429	mov	NONE,DFDAT
430	mov	NONE,DFDAT
431	mov	NONE,DFDAT
432	call	bcopy_3_dfdat		/* Only support 24 bit length. */
433#else
434/*
435 * For FreeBSD, just copy it wholesale
436 */
437	mvi	DINDEX,HADDR
438	call	bcopy_7_dfdat
439#endif
440
441/* Load STCNT as well.  It is a mirror of HCNT */
442	mvi	DINDEX,STCNT
443	mvi	HCNT	call bcopy_3
444        test    SSTAT1,PHASEMIS  jz data_phase_loop
445
446data_phase_finish:
447/*
448 * After a DMA finishes, save the SG and STCNT residuals back into the SCB
449 * We use STCNT instead of HCNT, since it's a reflection of how many bytes 
450 * were transferred on the SCSI (as opposed to the host) bus.
451 */
452	mvi	DINDEX,SCB_RESID_DCNT
453	mvi	STCNT	call bcopy_3
454	mov	SCB_RESID_SGCNT, SG_COUNT
455	jmp	ITloop
456
457/*
458 * Command phase.  Set up the DMA registers and let 'er rip - the
459 * two bytes after the SCB SCSI_cmd_length are zeroed by the driver,
460 * so we can copy those three bytes directly into HCNT.
461 */
462p_command:
463	call	assert
464
465/*
466 * Load HADDR and HCNT.  We can do this in one bcopy since they are neighbors
467 */
468	mvi	DINDEX,HADDR
469	mvi	SCB_CMDPTR	call bcopy_7
470
471	mvi	DINDEX,STCNT
472	mvi	SCB_CMDLEN	call bcopy_3
473
474	mvi	0x3d		call dma	# SCSIEN|SDMAEN|HDMAEN|
475						#   DIRECTION|FIFORESET
476	jmp	ITloop
477
478/*
479 * Status phase.  Wait for the data byte to appear, then read it
480 * and store it into the SCB.
481 */
482p_status:
483	mvi	SCB_TARGET_STATUS	call inb_first
484	jmp	mesgin_done
485
486/*
487 * Message out phase.  If there is no active message, but the target
488 * took us into this phase anyway, build a no-op message and send it.
489 */
490p_mesgout:
491	test	MSG_LEN, 0xff	jnz  p_mesgout_start
492	mvi	MSG_NOP		call mk_mesg	/* build NOP message */
493
494p_mesgout_start:
495/*
496 * Set up automatic PIO transfer from MSG0.  Bit 3 in
497 * SXFRCTL0 (SPIOEN) is already on.
498 */
499	mvi	SINDEX,MSG0
500	mov	DINDEX,MSG_LEN
501
502/*
503 * When target asks for a byte, drop ATN if it's the last one in
504 * the message.  Otherwise, keep going until the message is exhausted.
505 *
506 * Keep an eye out for a phase change, in case the target issues
507 * a MESSAGE REJECT.
508 */
509p_mesgout_loop:
510	test	SSTAT1,PHASEMIS	jnz p_mesgout_phasemis
511	test	SSTAT0,SPIORDY	jz p_mesgout_loop
512	cmp	DINDEX,1	jne p_mesgout_outb	/* last byte? */
513	mvi	CLRSINT1,CLRATNO			/* drop ATN */
514p_mesgout_outb:
515	or	SXFRCTL0, CLRSTCNT
516	mvi	STCNT0, 1
517	dec	DINDEX
518	mov	SCSIDATL,SINDIR
519p_mesgout_outb_wait:
520	test	SSTAT0,SDONE	jz p_mesgout_outb_wait
521	
522p_mesgout4:
523	test	DINDEX,0xff	jnz p_mesgout_loop
524
525/*
526 * If the next bus phase after ATN drops is a message out, it means
527 * that the target is requesting that the last message(s) be resent.
528 */
529p_mesgout_snoop:
530	test	SSTAT1,BUSFREE	jnz p_mesgout_done
531	test	SSTAT1,REQINIT	jz p_mesgout_snoop
532
533	test	SSTAT1,PHASEMIS	jnz p_mesgout_done
534
535	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
536	call	scsisig				/* ATNO - re-assert ATN */
537
538	jmp	ITloop
539
540p_mesgout_phasemis:
541	mvi	CLRSINT1,CLRATNO	/* Be sure turn ATNO off */
542p_mesgout_done:
543	clr	MSG_LEN			/* no active msg */
544	jmp	ITloop
545
546/*
547 * Message in phase.  Bytes are read using Automatic PIO mode.
548 */
549p_mesgin:
550	mvi	A		call inb_first	/* read the 1st message byte */
551	mov	REJBYTE,A			/* save it for the driver */
552
553	test	A,MSG_IDENTIFY		jnz mesgin_identify
554	cmp	A,MSG_DISCONNECT	je mesgin_disconnect
555	cmp	A,MSG_SDPTRS		je mesgin_sdptrs
556	cmp	ALLZEROS,A		je mesgin_complete
557	cmp	A,MSG_RDPTRS		je mesgin_rdptrs
558	cmp	A,MSG_EXTENDED		je mesgin_extended
559	cmp	A,MSG_REJECT		je mesgin_reject
560
561rej_mesgin:
562/*
563 * We have no idea what this message in is, and there's no way
564 * to pass it up to the kernel, so we issue a message reject and
565 * hope for the best.  Since we're now using manual PIO mode to
566 * read in the message, there should no longer be a race condition
567 * present when we assert ATN.  In any case, rejection should be a
568 * rare occurrence - signal the driver when it happens.
569 */
570	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
571	call	scsisig
572	mvi	INTSTAT,SEND_REJECT		/* let driver know */
573
574	mvi	MSG_REJECT	call mk_mesg
575
576mesgin_done:
577	call	inb_last			/*ack & turn auto PIO back on*/
578	jmp	ITloop
579
580
581mesgin_complete:
582/*
583 * We got a "command complete" message, so put the SCB pointer
584 * into the Queue Out, and trigger a completion interrupt.
585 * Check status for non zero return and interrupt driver if needed
586 * This allows the driver to interpret errors only when they occur
587 * instead of always uploading the scb.  If the status is SCSI_CHECK,
588 * the driver will download a new scb requesting sense to replace
589 * the old one, modify the "waiting for selection" SCB list and set 
590 * RETURN_1 to 0x80.  If RETURN_1 is set to 0x80 the sequencer imediately
591 * jumps to main loop where it will run down the waiting SCB list.
592 * If the kernel driver does not wish to request sense, it need
593 * only clear RETURN_1, and the command is allowed to complete.  We don't 
594 * bother to post to the QOUTFIFO in the error case since it would require 
595 * extra work in the kernel driver to ensure that the entry was removed 
596 * before the command complete code tried processing it.
597 *
598 * First check for residuals
599 */
600	test	SCB_RESID_SGCNT,0xff	jz check_status
601/*
602 * If we have a residual count, interrupt and tell the host.  Other
603 * alternatives are to pause the sequencer on all command completes (yuck),
604 * dma the resid directly to the host (slick, we may have space to do it now)
605 * or have the sequencer pause itself when it encounters a non-zero resid 
606 * (unecessary pause just to flag the command -yuck-, but takes one instruction
607 * and since it shouldn't happen that often is good enough for our purposes).  
608 */
609resid:
610	mvi	INTSTAT,RESIDUAL
611
612check_status:
613	test	SCB_TARGET_STATUS,0xff	jz status_ok	/* Good Status? */
614	mvi	INTSTAT,BAD_STATUS			/* let driver know */
615	cmp	RETURN_1, SEND_SENSE	jne status_ok
616	jmp	mesgin_done
617
618status_ok:
619/* First, mark this target as free. */
620	test	SCB_CONTROL,TAG_ENB jnz test_immediate	/*
621							 * Tagged commands
622							 * don't busy the
623							 * target.
624							 */
625	mov	FUNCTION1,SCB_TCL
626	mov	A,FUNCTION1
627	test	SCB_TCL,0x88 jz clear_a
628	xor	ACTIVE_B,A
629	jmp	test_immediate
630
631clear_a:
632	xor	ACTIVE_A,A
633
634test_immediate:
635	test    SCB_CMDLEN,0xff jnz complete  /* Immediate message complete */
636/*
637 * Pause the sequencer until the driver gets around to handling the command
638 * complete.  This is so that any action that might require carefull timing
639 * with the completion of this command can occur.
640 */
641	mvi	INTSTAT,IMMEDDONE
642	jmp	start
643complete:
644	mov	QOUTFIFO,SCBPTR
645	mvi	INTSTAT,CMDCMPLT
646	jmp	mesgin_done
647
648
649/*
650 * Is it an extended message?  We only support the synchronous and wide data
651 * transfer request messages, which will probably be in response to
652 * WDTR or SDTR message outs from us.  If it's not SDTR or WDTR, reject it -
653 * apparently this can be done after any message in byte, according
654 * to the SCSI-2 spec.
655 */
656mesgin_extended:
657	mvi	ARG_1		call inb_next	/* extended message length */
658	mvi	A		call inb_next	/* extended message code */
659
660	cmp	A,MSG_SDTR	je p_mesginSDTR
661	cmp	A,MSG_WDTR	je p_mesginWDTR
662	jmp	rej_mesgin
663
664p_mesginWDTR:
665	cmp	ARG_1,2		jne rej_mesgin	/* extended mesg length=2 */
666	mvi	ARG_1		call inb_next	/* Width of bus */
667	mvi	INTSTAT,WDTR_MSG		/* let driver know */
668	test	RETURN_1,0xff jz mesgin_done	/* Do we need to send WDTR? */
669	cmp	RETURN_1,SEND_REJ je rej_mesgin /*
670						 * Bus width was too large 
671						 * Reject it.
672						 */
673
674/* We didn't initiate the wide negotiation, so we must respond to the request */
675	and	RETURN_1,0x7f			/* Clear the SEND_WDTR Flag */
676	mvi	DINDEX,MSG0
677	mvi	MSG0	call mk_wdtr		/* build WDTR message */
678	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
679	call	scsisig
680	jmp	mesgin_done
681
682p_mesginSDTR:
683	cmp	ARG_1,3		jne rej_mesgin	/* extended mesg length=3 */
684	mvi	ARG_1		call inb_next	/* xfer period */
685	mvi	A		call inb_next	/* REQ/ACK offset */
686	mvi	INTSTAT,SDTR_MSG		/* call driver to convert */
687
688	test	RETURN_1,0xff	jz mesgin_done  /* Do we need to mk_sdtr/rej */
689	cmp	RETURN_1,SEND_REJ je rej_mesgin /*
690						 * Requested SDTR too small
691						 * Reject it.
692						 */
693	mvi	DINDEX, MSG0
694	mvi     MSG0     call mk_sdtr
695	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
696	call	scsisig
697	jmp	mesgin_done
698
699/*
700 * Is it a disconnect message?  Set a flag in the SCB to remind us
701 * and await the bus going free.
702 */
703mesgin_disconnect:
704	or	SCB_CONTROL,DISCONNECTED
705	jmp	mesgin_done
706
707/*
708 * Save data pointers message?  Copy working values into the SCB,
709 * usually in preparation for a disconnect.
710 */
711mesgin_sdptrs:
712	call	sg_ram2scb
713	jmp	mesgin_done
714
715/*
716 * Restore pointers message?  Data pointers are recopied from the
717 * SCB anytime we enter a data phase for the first time, so all
718 * we need to do is clear the DPHASE flag and let the data phase
719 * code do the rest.
720 */
721mesgin_rdptrs:
722	and	FLAGS,0xfb			/*
723						 * !DPHASE we'll reload them
724						 * the next time through
725						 */
726	jmp	mesgin_done
727
728/*
729 * Identify message?  For a reconnecting target, this tells us the lun
730 * that the reconnection is for - find the correct SCB and switch to it,
731 * clearing the "disconnected" bit so we don't "find" it by accident later.
732 */
733mesgin_identify:
734	test	A,0x78	jnz rej_mesgin	/*!DiscPriv|!LUNTAR|!Reserved*/
735
736	and	A,0x07			/* lun in lower three bits */
737	or      SAVED_TCL,A,SELID          
738	and     SAVED_TCL,0xf7
739	and     A,SELBUSB,SBLKCTL	/* B Channel?? */
740	or      SAVED_TCL,A
741	call	inb_last		/* ACK */
742
743/*
744 * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
745 * If we get one, we use the tag returned to switch to the proper
746 * SCB.  Otherwise, we just use the findSCB method.
747 */
748snoop_tag_loop:
749	test	SSTAT1,BUSFREE	jnz use_findSCB
750	test	SSTAT1,REQINIT	jz snoop_tag_loop
751	test	SSTAT1,PHASEMIS	jnz use_findSCB
752	mvi	A		call inb_first
753	cmp	A,MSG_SIMPLE_TAG je get_tag
754use_findSCB:
755	mov	ALLZEROS	call findSCB	  /* Have to search */
756setup_SCB:
757	and	SCB_CONTROL,0xfb	  /* clear disconnect bit in SCB */
758	or	FLAGS,IDENTIFY_SEEN	  /* make note of IDENTIFY */
759	jmp	ITloop
760get_tag:
761	mvi	ARG_1	call inb_next	/* tag value */
762/*
763 * See if the tag is in range.  The tag is < SCBCOUNT if we add
764 * the complement of SCBCOUNT to the incomming tag and there is
765 * no carry.
766 */
767	mov	A,COMP_SCBCOUNT	
768	add	SINDEX,A,ARG_1
769	jc	abort_tag
770
771/*
772 * Ensure that the SCB the tag points to is for an SCB transaction
773 * to the reconnecting target.
774 */
775	mov	SCBPTR,ARG_1
776	mov	A,SAVED_TCL
777	cmp	SCB_TCL,A		jne abort_tag
778	test	SCB_CONTROL,TAG_ENB	jz  abort_tag
779	call	inb_last			/* Ack Successful tag */
780	jmp	setup_SCB
781abort_tag:
782	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
783	call	scsisig
784	mvi	INTSTAT,ABORT_TAG 		/* let driver know */
785	mvi	0xd		call mk_mesg	/* ABORT TAG message */
786	jmp	mesgin_done
787
788/*
789 * Message reject?  Let the kernel driver handle this.  If we have an 
790 * outstanding WDTR or SDTR negotiation, assume that it's a response from 
791 * the target selecting 8bit or asynchronous transfer, otherwise just ignore 
792 * it since we have no clue what it pertains to.
793 */
794mesgin_reject:
795	mvi	INTSTAT, REJECT_MSG
796	jmp	mesgin_done
797
798/*
799 * [ ADD MORE MESSAGE HANDLING HERE ]
800 */
801
802/*
803 * Bus free phase.  It might be useful to interrupt the device
804 * driver if we aren't expecting this.  For now, make sure that
805 * ATN isn't being asserted and look for a new command.
806 */
807p_busfree:
808	mvi	CLRSINT1,CLRATNO
809
810/*
811 * if this is an immediate command, perform a psuedo command complete to
812 * notify the driver.
813 */
814	test	SCB_CMDLEN,0xff	jz status_ok
815	jmp	start
816
817/*
818 * Instead of a generic bcopy routine that requires an argument, we unroll
819 * the cases that are actually used, and call them explicitly.  This
820 * not only reduces the overhead of doing a bcopy, but ends up saving space 
821 * in the program since you don't have to put the argument into the accumulator
822 * before the call.  Both functions expect DINDEX to contain the destination 
823 * address and SINDEX to contain the source address.
824 */
825bcopy_7:
826	mov	DINDIR,SINDIR
827	mov	DINDIR,SINDIR
828bcopy_5:
829	mov	DINDIR,SINDIR
830bcopy_4:
831	mov	DINDIR,SINDIR
832bcopy_3:
833	mov	DINDIR,SINDIR
834	mov	DINDIR,SINDIR
835	mov	DINDIR,SINDIR	ret
836	
837bcopy_7_dfdat:
838	mov	DINDIR,DFDAT
839	mov	DINDIR,DFDAT
840bcopy_5_dfdat:
841	mov	DINDIR,DFDAT
842bcopy_4_dfdat:
843	mov	DINDIR,DFDAT
844bcopy_3_dfdat:
845	mov	DINDIR,DFDAT
846	mov	DINDIR,DFDAT
847	mov	DINDIR,DFDAT	ret
848
849/*
850 * Locking the driver out, build a one-byte message passed in SINDEX
851 * if there is no active message already.  SINDEX is returned intact.
852 */
853mk_mesg:
854	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
855	test	MSG_LEN,0xff	jz mk_mesg1	/* Should always succeed */
856	
857	/*
858	 * Hmmm.  For some reason the mesg buffer is in use.
859	 * Tell the driver.  It should look at SINDEX to find
860	 * out what we wanted to use the buffer for and resolve
861	 * the conflict.
862	 */
863	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
864	mvi	INTSTAT,MSG_BUFFER_BUSY ret
865
866mk_mesg1:
867	mvi	MSG_LEN,1		/* length = 1 */
868	mov	MSG0,SINDEX		/* 1-byte message */
869	mvi	SEQCTL,0x10	ret	/* !PAUSEDIS|FASTMODE */
870
871/*
872 * Functions to read data in Automatic PIO mode.
873 *
874 * According to Adaptec's documentation, an ACK is not sent on input from
875 * the target until SCSIDATL is read from.  So we wait until SCSIDATL is
876 * latched (the usual way), then read the data byte directly off the bus
877 * using SCSIBUSL.  When we have pulled the ATN line, or we just want to
878 * acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
879 * spec guarantees that the target will hold the data byte on the bus until
880 * we send our ACK.
881 *
882 * The assumption here is that these are called in a particular sequence,
883 * and that REQ is already set when inb_first is called.  inb_{first,next}
884 * use the same calling convention as inb.
885 */
886
887inb_next:
888	call	inb_last
889inb_first:
890	test	SSTAT1,PHASEMIS	jnz mesgin_phasemis
891	test	SSTAT0,SPIORDY	jz inb_first /* wait for next byte */
892	mov	DINDEX,SINDEX
893	mov	DINDIR,SCSIBUSL	ret		/*read byte directly from bus*/
894inb_last:
895	or	SXFRCTL0,CLRSTCNT
896	mvi	STCNT0,0x01
897	mov	NONE,SCSIDATL			/*dummy read from latch to ACK*/
898inb_last_wait:
899	test	SSTAT0,SDONE	jz inb_last_wait /* Wait for completion */
900	ret
901
902mesgin_phasemis:
903/*
904 * We expected to receive another byte, but the target changed phase
905 */
906	mvi	INTSTAT, MSGIN_PHASEMIS
907	jmp	ITloop
908
909/*
910 * DMA data transfer.  HADDR and HCNT must be loaded first, and
911 * SINDEX should contain the value to load DFCNTRL with - 0x3d for
912 * host->scsi, or 0x39 for scsi->host.  The SCSI channel is cleared
913 * during initialization.
914 */
915dma:
916	mov	DFCNTRL,SINDEX
917dma1:
918	test	SSTAT0,DMADONE	jnz dma3
919	test	SSTAT1,PHASEMIS	jz dma1		/* ie. underrun */
920
921/*
922 * We will be "done" DMAing when the transfer count goes to zero, or
923 * the target changes the phase (in light of this, it makes sense that
924 * the DMA circuitry doesn't ACK when PHASEMIS is active).  If we are
925 * doing a SCSI->Host transfer, the data FIFO should be flushed auto-
926 * magically on STCNT=0 or a phase change, so just wait for FIFO empty
927 * status.
928 */
929dma3:
930	test	SINDEX,DIRECTION	jnz dma5
931dma4:
932	test	DFSTATUS,FIFOEMP	jz dma4
933
934/*
935 * Now shut the DMA enables off and make sure that the DMA enables are 
936 * actually off first lest we get an ILLSADDR.
937 */
938dma5:
939	/* disable DMA, but maintain WIDEODD */
940	and	A, WIDEODD, SINDEX	
941	mov	DFCNTRL, A
942dma6:
943	test	DFCNTRL,0x38	jnz dma6  /* SCSIENACK|SDMAENACK|HDMAENACK */
944
945	ret
946
947dma_finish:
948	test	DFSTATUS,HDONE	jz dma_finish
949
950	clr	DFCNTRL				/* disable DMA */
951dma_finish2:
952	test	DFCNTRL,HDMAENACK jnz dma_finish2
953	ret
954
955/*
956 * Common SCSI initialization for selection and reselection.  Expects
957 * the target SCSI ID to be in the upper four bits of SINDEX, and A's
958 * contents are stomped on return.
959 */
960initialize_scsiid:
961	and	SINDEX,0xf0		/* Get target ID */
962	and	A,0x0f,SCSIID
963	or	SINDEX,A
964	mov	SCSIID,SINDEX ret
965
966/*
967 * Assert that if we've been reselected, then we've seen an IDENTIFY
968 * message.
969 */
970assert:
971	test	FLAGS,RESELECTED	jz return	/* reselected? */
972	test	FLAGS,IDENTIFY_SEEN	jnz return	/* seen IDENTIFY? */
973
974	mvi	INTSTAT,NO_IDENT 	ret	/* no - cause a kernel panic */
975
976/*
977 * Locate the SCB matching the target ID/channel/lun in SAVED_TCL and switch 
978 * the SCB to it.  Have the kernel print a warning message if it can't be 
979 * found, and generate an ABORT message to the target.  SINDEX should be
980 * cleared on call.
981 */
982findSCB:
983	mov	A,SAVED_TCL
984	mov	SCBPTR,SINDEX			/* switch to new SCB */
985	cmp	SCB_TCL,A	jne findSCB1 /* target ID/channel/lun match? */
986	test	SCB_CONTROL,DISCONNECTED jz findSCB1 /*should be disconnected*/
987	ret
988
989findSCB1:
990	inc	SINDEX
991	mov	A,SCBCOUNT
992	cmp	SINDEX,A	jne findSCB
993
994	mvi	INTSTAT,NO_MATCH		/* not found - signal kernel */
995	mvi	MSG_ABORT	call mk_mesg	/* ABORT message */
996
997	or	SINDEX,0x10,SIGSTATE		/* assert ATNO */
998	call	scsisig
999	ret
1000/*
1001 * Make a working copy of the scatter-gather parameters from the SCB.
1002 */
1003sg_scb2ram:
1004	mvi	DINDEX,HADDR
1005	mvi	SCB_DATAPTR	call bcopy_7
1006
1007	mvi	DINDEX,STCNT
1008	mvi	SCB_DATACNT	call bcopy_3
1009
1010	mov	SG_COUNT,SCB_SGCOUNT
1011
1012	mvi	DINDEX,SG_NEXT
1013	mvi	SCB_SGPTR	call bcopy_4
1014	ret
1015
1016/*
1017 * Copying RAM values back to SCB, for Save Data Pointers message, but
1018 * only if we've actually been into a data phase to change them.  This
1019 * protects against bogus data in scratch ram and the residual counts
1020 * since they are only initialized when we go into data_in or data_out.
1021 */
1022sg_ram2scb:
1023	test	FLAGS, DPHASE	jz return
1024	mov	SCB_SGCOUNT,SG_COUNT
1025
1026	mvi	DINDEX,SCB_SGPTR
1027	mvi	SG_NEXT		call bcopy_4
1028	
1029	mvi	DINDEX,SCB_DATAPTR
1030	mvi	SHADDR	call bcopy_4
1031
1032/*
1033 * Use the residual number since STCNT is corrupted by any message transfer
1034 */
1035	mvi	SCB_RESID_DCNT	call bcopy_3
1036	ret
1037
1038/*
1039 * Add the array base TARG_SCRATCH to the target offset (the target address
1040 * is in SCSIID), and return the result in SINDEX.  The accumulator
1041 * contains the 3->8 decoding of the target ID on return.
1042 */
1043ndx_dtr:
1044	shr	A,SCSIID,4
1045	test	SBLKCTL,SELBUSB	jz ndx_dtr_2
1046	or	A,0x08		/* Channel B entries add 8 */
1047ndx_dtr_2:
1048	add	SINDEX,TARG_SCRATCH,A
1049
1050	mov	FUNCTION1,SCSIID	/* 3-bit target address decode */
1051	mov	A,FUNCTION1	ret
1052
1053/*
1054 * If we need to negotiate transfer parameters, build the WDTR or SDTR message
1055 * starting at the address passed in SINDEX.  DINDEX is modified on return.
1056 * The SCSI-II spec requires that Wide negotiation occur first and you can
1057 * only negotiat one or the other at a time otherwise in the event of a message
1058 * reject, you wouldn't be able to tell which message was the culpret.
1059 */
1060mk_dtr:
1061	test	SCB_CONTROL,0x90 jz return	/* NEEDWDTR|NEEDSDTR */
1062	test	SCB_CONTROL,NEEDWDTR jnz  mk_wdtr_16bit
1063	or	FLAGS, MAXOFFSET	/* Force an offset of 15 or 8 if WIDE */
1064
1065mk_sdtr:
1066	mvi	DINDIR,1		/* extended message */
1067	mvi	DINDIR,3		/* extended message length = 3 */
1068	mvi	DINDIR,1		/* SDTR code */
1069	call	sdtr_to_rate
1070	mov	DINDIR,RETURN_1		/* REQ/ACK transfer period */
1071	test	FLAGS, MAXOFFSET jnz mk_sdtr_max_offset
1072	and	DINDIR,0x0f,SINDIR	/* Sync Offset */
1073
1074mk_sdtr_done:
1075	add	MSG_LEN,COMP_MSG0,DINDEX ret	/* update message length */
1076
1077mk_sdtr_max_offset:
1078/*
1079 * We're initiating sync negotiation, so request the max offset we can (15 or 8)
1080 */
1081	xor	FLAGS, MAXOFFSET
1082
1083	/* Talking to a WIDE device? */
1084	test	SCSIRATE, WIDEXFER	jnz wmax_offset	
1085	mvi	DINDIR, MAX_OFFSET_8BIT
1086	jmp	mk_sdtr_done
1087
1088wmax_offset:
1089	mvi	DINDIR, MAX_OFFSET_16BIT
1090	jmp	mk_sdtr_done
1091
1092mk_wdtr_16bit:
1093	mvi	ARG_1,BUS_16_BIT
1094mk_wdtr:
1095	mvi	DINDIR,1		/* extended message */
1096	mvi	DINDIR,2		/* extended message length = 2 */
1097	mvi	DINDIR,3		/* WDTR code */
1098	mov	DINDIR,ARG_1		/* bus width */
1099
1100	add	MSG_LEN,COMP_MSG0,DINDEX ret	/* update message length */
1101	
1102/*
1103 * Set SCSI bus control signal state.  This also saves the last-written
1104 * value into a location where the higher-level driver can read it - if
1105 * it has to send an ABORT or RESET message, then it needs to know this
1106 * so it can assert ATN without upsetting SCSISIGO.  The new value is
1107 * expected in SINDEX.  Change the actual state last to avoid contention
1108 * from the driver.
1109 */
1110scsisig:
1111	mov	SIGSTATE,SINDEX
1112	mov	SCSISIGO,SINDEX	ret
1113
1114sdtr_to_rate:
1115	call	ndx_dtr			/* index scratch space for target */
1116	shr	A,SINDIR,0x4
1117	dec	SINDEX			/* Preserve SINDEX */
1118	and	A,0x7
1119	clr	RETURN_1
1120sdtr_to_rate_loop:
1121	test	A,0x0f	jz sdtr_to_rate_done
1122	add	RETURN_1,0x19
1123	dec	A	
1124	jmp	sdtr_to_rate_loop
1125sdtr_to_rate_done:
1126	shr	RETURN_1,0x2
1127	add	RETURN_1,0x19
1128	test	SXFRCTL0,ULTRAEN jz return
1129	shr	RETURN_1,0x1
1130return:
1131	ret
1132