aic7xxx.seq revision 13386
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.28 1996/01/09 16:14:03 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	mvi	DINDEX,HADDR
423/*
424 * For Linux, we must throw away four bytes since there is a 32bit gap
425 * in the middle of a struct scatterlist
426 */
427#ifdef linux
428	call	bcopy_4_dfdat
429	mov	NONE,DFDAT
430	mov	NONE,DFDAT
431	mov	NONE,DFDAT
432	mov	NONE,DFDAT
433	call	bcopy_3_dfdat		/* Only support 24 bit length. */
434#else
435/*
436 * For FreeBSD, just copy it wholesale
437 */
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	dec	DINDEX
516	or	CLRSINT0, CLRSPIORDY
517	mov	SCSIDATL,SINDIR
518	
519p_mesgout4:
520	test	DINDEX,0xff	jnz p_mesgout_loop
521
522/*
523 * If the next bus phase after ATN drops is a message out, it means
524 * that the target is requesting that the last message(s) be resent.
525 */
526p_mesgout_snoop:
527	test	SSTAT1,BUSFREE	jnz p_mesgout_done
528	test	SSTAT1,REQINIT	jz p_mesgout_snoop
529
530	test	SSTAT1,PHASEMIS	jnz p_mesgout_done
531
532	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
533	call	scsisig				/* ATNO - re-assert ATN */
534
535	jmp	ITloop
536
537p_mesgout_phasemis:
538	mvi	CLRSINT1,CLRATNO	/* Be sure turn ATNO off */
539p_mesgout_done:
540	clr	MSG_LEN			/* no active msg */
541	jmp	ITloop
542
543/*
544 * Message in phase.  Bytes are read using Automatic PIO mode.
545 */
546p_mesgin:
547	mvi	A		call inb_first	/* read the 1st message byte */
548	mov	REJBYTE,A			/* save it for the driver */
549
550	test	A,MSG_IDENTIFY		jnz mesgin_identify
551	cmp	A,MSG_DISCONNECT	je mesgin_disconnect
552	cmp	A,MSG_SDPTRS		je mesgin_sdptrs
553	cmp	ALLZEROS,A		je mesgin_complete
554	cmp	A,MSG_RDPTRS		je mesgin_rdptrs
555	cmp	A,MSG_EXTENDED		je mesgin_extended
556	cmp	A,MSG_REJECT		je mesgin_reject
557
558rej_mesgin:
559/*
560 * We have no idea what this message in is, and there's no way
561 * to pass it up to the kernel, so we issue a message reject and
562 * hope for the best.  Since we're now using manual PIO mode to
563 * read in the message, there should no longer be a race condition
564 * present when we assert ATN.  In any case, rejection should be a
565 * rare occurrence - signal the driver when it happens.
566 */
567	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
568	call	scsisig
569	mvi	INTSTAT,SEND_REJECT		/* let driver know */
570
571	mvi	MSG_REJECT	call mk_mesg
572
573mesgin_done:
574	call	inb_last			/*ack & turn auto PIO back on*/
575	jmp	ITloop
576
577
578mesgin_complete:
579/*
580 * We got a "command complete" message, so put the SCB pointer
581 * into the Queue Out, and trigger a completion interrupt.
582 * Check status for non zero return and interrupt driver if needed
583 * This allows the driver to interpret errors only when they occur
584 * instead of always uploading the scb.  If the status is SCSI_CHECK,
585 * the driver will download a new scb requesting sense to replace
586 * the old one, modify the "waiting for selection" SCB list and set 
587 * RETURN_1 to 0x80.  If RETURN_1 is set to 0x80 the sequencer imediately
588 * jumps to main loop where it will run down the waiting SCB list.
589 * If the kernel driver does not wish to request sense, it need
590 * only clear RETURN_1, and the command is allowed to complete.  We don't 
591 * bother to post to the QOUTFIFO in the error case since it would require 
592 * extra work in the kernel driver to ensure that the entry was removed 
593 * before the command complete code tried processing it.
594 *
595 * First check for residuals
596 */
597	test	SCB_RESID_SGCNT,0xff	jz check_status
598/*
599 * If we have a residual count, interrupt and tell the host.  Other
600 * alternatives are to pause the sequencer on all command completes (yuck),
601 * dma the resid directly to the host (slick, we may have space to do it now)
602 * or have the sequencer pause itself when it encounters a non-zero resid 
603 * (unecessary pause just to flag the command -yuck-, but takes one instruction
604 * and since it shouldn't happen that often is good enough for our purposes).  
605 */
606resid:
607	mvi	INTSTAT,RESIDUAL
608
609check_status:
610	test	SCB_TARGET_STATUS,0xff	jz status_ok	/* Good Status? */
611	mvi	INTSTAT,BAD_STATUS			/* let driver know */
612	cmp	RETURN_1, SEND_SENSE	jne status_ok
613	jmp	mesgin_done
614
615status_ok:
616/* First, mark this target as free. */
617	test	SCB_CONTROL,TAG_ENB jnz test_immediate	/*
618							 * Tagged commands
619							 * don't busy the
620							 * target.
621							 */
622	mov	FUNCTION1,SCB_TCL
623	mov	A,FUNCTION1
624	test	SCB_TCL,0x88 jz clear_a
625	xor	ACTIVE_B,A
626	jmp	test_immediate
627
628clear_a:
629	xor	ACTIVE_A,A
630
631test_immediate:
632	test    SCB_CMDLEN,0xff jnz complete  /* Immediate message complete */
633/*
634 * Pause the sequencer until the driver gets around to handling the command
635 * complete.  This is so that any action that might require carefull timing
636 * with the completion of this command can occur.
637 */
638	mvi	INTSTAT,IMMEDDONE
639	jmp	start
640complete:
641	mov	QOUTFIFO,SCBPTR
642	mvi	INTSTAT,CMDCMPLT
643	jmp	mesgin_done
644
645
646/*
647 * Is it an extended message?  We only support the synchronous and wide data
648 * transfer request messages, which will probably be in response to
649 * WDTR or SDTR message outs from us.  If it's not SDTR or WDTR, reject it -
650 * apparently this can be done after any message in byte, according
651 * to the SCSI-2 spec.
652 */
653mesgin_extended:
654	mvi	ARG_1		call inb_next	/* extended message length */
655	mvi	A		call inb_next	/* extended message code */
656
657	cmp	A,MSG_SDTR	je p_mesginSDTR
658	cmp	A,MSG_WDTR	je p_mesginWDTR
659	jmp	rej_mesgin
660
661p_mesginWDTR:
662	cmp	ARG_1,2		jne rej_mesgin	/* extended mesg length=2 */
663	mvi	ARG_1		call inb_next	/* Width of bus */
664	mvi	INTSTAT,WDTR_MSG		/* let driver know */
665	test	RETURN_1,0xff jz mesgin_done	/* Do we need to send WDTR? */
666	cmp	RETURN_1,SEND_REJ je rej_mesgin /*
667						 * Bus width was too large 
668						 * Reject it.
669						 */
670
671/* We didn't initiate the wide negotiation, so we must respond to the request */
672	and	RETURN_1,0x7f			/* Clear the SEND_WDTR Flag */
673	mvi	DINDEX,MSG0
674	mvi	MSG0	call mk_wdtr		/* build WDTR message */
675	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
676	call	scsisig
677	jmp	mesgin_done
678
679p_mesginSDTR:
680	cmp	ARG_1,3		jne rej_mesgin	/* extended mesg length=3 */
681	mvi	ARG_1		call inb_next	/* xfer period */
682	mvi	A		call inb_next	/* REQ/ACK offset */
683	mvi	INTSTAT,SDTR_MSG		/* call driver to convert */
684
685	test	RETURN_1,0xff	jz mesgin_done  /* Do we need to mk_sdtr/rej */
686	cmp	RETURN_1,SEND_REJ je rej_mesgin /*
687						 * Requested SDTR too small
688						 * Reject it.
689						 */
690	mvi	DINDEX, MSG0
691	mvi     MSG0     call mk_sdtr
692	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
693	call	scsisig
694	jmp	mesgin_done
695
696/*
697 * Is it a disconnect message?  Set a flag in the SCB to remind us
698 * and await the bus going free.
699 */
700mesgin_disconnect:
701	or	SCB_CONTROL,DISCONNECTED
702	jmp	mesgin_done
703
704/*
705 * Save data pointers message?  Copy working values into the SCB,
706 * usually in preparation for a disconnect.
707 */
708mesgin_sdptrs:
709	call	sg_ram2scb
710	jmp	mesgin_done
711
712/*
713 * Restore pointers message?  Data pointers are recopied from the
714 * SCB anytime we enter a data phase for the first time, so all
715 * we need to do is clear the DPHASE flag and let the data phase
716 * code do the rest.
717 */
718mesgin_rdptrs:
719	and	FLAGS,0xfb			/*
720						 * !DPHASE we'll reload them
721						 * the next time through
722						 */
723	jmp	mesgin_done
724
725/*
726 * Identify message?  For a reconnecting target, this tells us the lun
727 * that the reconnection is for - find the correct SCB and switch to it,
728 * clearing the "disconnected" bit so we don't "find" it by accident later.
729 */
730mesgin_identify:
731	test	A,0x78	jnz rej_mesgin	/*!DiscPriv|!LUNTAR|!Reserved*/
732
733	and	A,0x07			/* lun in lower three bits */
734	or      SAVED_TCL,A,SELID          
735	and     SAVED_TCL,0xf7
736	and     A,SELBUSB,SBLKCTL	/* B Channel?? */
737	or      SAVED_TCL,A
738	call	inb_last		/* ACK */
739
740/*
741 * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
742 * If we get one, we use the tag returned to switch to the proper
743 * SCB.  Otherwise, we just use the findSCB method.
744 */
745snoop_tag_loop:
746	test	SSTAT1,BUSFREE	jnz use_findSCB
747	test	SSTAT1,REQINIT	jz snoop_tag_loop
748	test	SSTAT1,PHASEMIS	jnz use_findSCB
749	mvi	A		call inb_first
750	cmp	A,MSG_SIMPLE_TAG je get_tag
751use_findSCB:
752	mov	ALLZEROS	call findSCB	  /* Have to search */
753setup_SCB:
754	and	SCB_CONTROL,0xfb	  /* clear disconnect bit in SCB */
755	or	FLAGS,IDENTIFY_SEEN	  /* make note of IDENTIFY */
756	jmp	ITloop
757get_tag:
758	mvi	ARG_1	call inb_next	/* tag value */
759/*
760 * See if the tag is in range.  The tag is < SCBCOUNT if we add
761 * the complement of SCBCOUNT to the incomming tag and there is
762 * no carry.
763 */
764	mov	A,COMP_SCBCOUNT	
765	add	SINDEX,A,ARG_1
766	jc	abort_tag
767
768/*
769 * Ensure that the SCB the tag points to is for an SCB transaction
770 * to the reconnecting target.
771 */
772	mov	SCBPTR,ARG_1
773	mov	A,SAVED_TCL
774	cmp	SCB_TCL,A		jne abort_tag
775	test	SCB_CONTROL,TAG_ENB	jz  abort_tag
776	call	inb_last			/* Ack Successful tag */
777	jmp	setup_SCB
778abort_tag:
779	or	SINDEX,0x10,SIGSTATE		/* turn on ATNO */
780	call	scsisig
781	mvi	INTSTAT,ABORT_TAG 		/* let driver know */
782	mvi	0xd		call mk_mesg	/* ABORT TAG message */
783	jmp	mesgin_done
784
785/*
786 * Message reject?  Let the kernel driver handle this.  If we have an 
787 * outstanding WDTR or SDTR negotiation, assume that it's a response from 
788 * the target selecting 8bit or asynchronous transfer, otherwise just ignore 
789 * it since we have no clue what it pertains to.
790 */
791mesgin_reject:
792	mvi	INTSTAT, REJECT_MSG
793	jmp	mesgin_done
794
795/*
796 * [ ADD MORE MESSAGE HANDLING HERE ]
797 */
798
799/*
800 * Bus free phase.  It might be useful to interrupt the device
801 * driver if we aren't expecting this.  For now, make sure that
802 * ATN isn't being asserted and look for a new command.
803 */
804p_busfree:
805	mvi	CLRSINT1,CLRATNO
806
807/*
808 * if this is an immediate command, perform a psuedo command complete to
809 * notify the driver.
810 */
811	test	SCB_CMDLEN,0xff	jz status_ok
812	jmp	start
813
814/*
815 * Instead of a generic bcopy routine that requires an argument, we unroll
816 * the cases that are actually used, and call them explicitly.  This
817 * not only reduces the overhead of doing a bcopy, but ends up saving space 
818 * in the program since you don't have to put the argument into the accumulator
819 * before the call.  Both functions expect DINDEX to contain the destination 
820 * address and SINDEX to contain the source address.
821 */
822bcopy_7:
823	mov	DINDIR,SINDIR
824	mov	DINDIR,SINDIR
825bcopy_5:
826	mov	DINDIR,SINDIR
827bcopy_4:
828	mov	DINDIR,SINDIR
829bcopy_3:
830	mov	DINDIR,SINDIR
831	mov	DINDIR,SINDIR
832	mov	DINDIR,SINDIR	ret
833	
834bcopy_7_dfdat:
835	mov	DINDIR,DFDAT
836	mov	DINDIR,DFDAT
837bcopy_5_dfdat:
838	mov	DINDIR,DFDAT
839bcopy_4_dfdat:
840	mov	DINDIR,DFDAT
841bcopy_3_dfdat:
842	mov	DINDIR,DFDAT
843	mov	DINDIR,DFDAT
844	mov	DINDIR,DFDAT	ret
845
846/*
847 * Locking the driver out, build a one-byte message passed in SINDEX
848 * if there is no active message already.  SINDEX is returned intact.
849 */
850mk_mesg:
851	mvi	SEQCTL,0x50			/* PAUSEDIS|FASTMODE */
852	test	MSG_LEN,0xff	jz mk_mesg1	/* Should always succeed */
853	
854	/*
855	 * Hmmm.  For some reason the mesg buffer is in use.
856	 * Tell the driver.  It should look at SINDEX to find
857	 * out what we wanted to use the buffer for and resolve
858	 * the conflict.
859	 */
860	mvi	SEQCTL,0x10			/* !PAUSEDIS|FASTMODE */
861	mvi	INTSTAT,MSG_BUFFER_BUSY ret
862
863mk_mesg1:
864	mvi	MSG_LEN,1		/* length = 1 */
865	mov	MSG0,SINDEX		/* 1-byte message */
866	mvi	SEQCTL,0x10	ret	/* !PAUSEDIS|FASTMODE */
867
868/*
869 * Functions to read data in Automatic PIO mode.
870 *
871 * According to Adaptec's documentation, an ACK is not sent on input from
872 * the target until SCSIDATL is read from.  So we wait until SCSIDATL is
873 * latched (the usual way), then read the data byte directly off the bus
874 * using SCSIBUSL.  When we have pulled the ATN line, or we just want to
875 * acknowledge the byte, then we do a dummy read from SCISDATL.  The SCSI
876 * spec guarantees that the target will hold the data byte on the bus until
877 * we send our ACK.
878 *
879 * The assumption here is that these are called in a particular sequence,
880 * and that REQ is already set when inb_first is called.  inb_{first,next}
881 * use the same calling convention as inb.
882 */
883
884inb_next:
885	or	CLRSINT0, CLRSPIORDY
886	mov	NONE,SCSIDATL			/*dummy read from latch to ACK*/
887inb_next_wait:
888	test	SSTAT1,PHASEMIS	jnz mesgin_phasemis
889	test	SSTAT0,SPIORDY	jz inb_next_wait /* wait for next byte */
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	/* disable DMA, but maintain WIDEODD */
934	and	A, WIDEODD, SINDEX	
935	mov	DFCNTRL, A
936dma6:
937	test	DFCNTRL,0x38	jnz dma6  /* SCSIENACK|SDMAENACK|HDMAENACK */
938
939	ret
940
941dma_finish:
942	test	DFSTATUS,HDONE	jz dma_finish
943
944	clr	DFCNTRL				/* disable DMA */
945dma_finish2:
946	test	DFCNTRL,HDMAENACK jnz dma_finish2
947	ret
948
949/*
950 * Common SCSI initialization for selection and reselection.  Expects
951 * the target SCSI ID to be in the upper four bits of SINDEX, and A's
952 * contents are stomped on return.
953 */
954initialize_scsiid:
955	and	SINDEX,0xf0		/* Get target ID */
956	and	A,0x0f,SCSIID
957	or	SINDEX,A
958	mov	SCSIID,SINDEX ret
959
960/*
961 * Assert that if we've been reselected, then we've seen an IDENTIFY
962 * message.
963 */
964assert:
965	test	FLAGS,RESELECTED	jz return	/* reselected? */
966	test	FLAGS,IDENTIFY_SEEN	jnz return	/* seen IDENTIFY? */
967
968	mvi	INTSTAT,NO_IDENT 	ret	/* no - cause a kernel panic */
969
970/*
971 * Locate the SCB matching the target ID/channel/lun in SAVED_TCL and switch 
972 * the SCB to it.  Have the kernel print a warning message if it can't be 
973 * found, and generate an ABORT message to the target.  SINDEX should be
974 * cleared on call.
975 */
976findSCB:
977	mov	A,SAVED_TCL
978	mov	SCBPTR,SINDEX			/* switch to new SCB */
979	cmp	SCB_TCL,A	jne findSCB1 /* target ID/channel/lun match? */
980	test	SCB_CONTROL,DISCONNECTED jz findSCB1 /*should be disconnected*/
981	ret
982
983findSCB1:
984	inc	SINDEX
985	mov	A,SCBCOUNT
986	cmp	SINDEX,A	jne findSCB
987
988	mvi	INTSTAT,NO_MATCH		/* not found - signal kernel */
989	mvi	MSG_ABORT	call mk_mesg	/* ABORT message */
990
991	or	SINDEX,0x10,SIGSTATE		/* assert ATNO */
992	call	scsisig
993	ret
994/*
995 * Make a working copy of the scatter-gather parameters from the SCB.
996 */
997sg_scb2ram:
998	mvi	DINDEX,HADDR
999	mvi	SCB_DATAPTR	call bcopy_7
1000
1001	mvi	DINDEX,STCNT
1002	mvi	SCB_DATACNT	call bcopy_3
1003
1004	mov	SG_COUNT,SCB_SGCOUNT
1005
1006	mvi	DINDEX,SG_NEXT
1007	mvi	SCB_SGPTR	call bcopy_4
1008	ret
1009
1010/*
1011 * Copying RAM values back to SCB, for Save Data Pointers message, but
1012 * only if we've actually been into a data phase to change them.  This
1013 * protects against bogus data in scratch ram and the residual counts
1014 * since they are only initialized when we go into data_in or data_out.
1015 */
1016sg_ram2scb:
1017	test	FLAGS, DPHASE	jz return
1018	mov	SCB_SGCOUNT,SG_COUNT
1019
1020	mvi	DINDEX,SCB_SGPTR
1021	mvi	SG_NEXT		call bcopy_4
1022	
1023	mvi	DINDEX,SCB_DATAPTR
1024	mvi	SHADDR	call bcopy_4
1025
1026/*
1027 * Use the residual number since STCNT is corrupted by any message transfer
1028 */
1029	mvi	SCB_RESID_DCNT	call bcopy_3
1030	ret
1031
1032/*
1033 * Add the array base TARG_SCRATCH to the target offset (the target address
1034 * is in SCSIID), and return the result in SINDEX.  The accumulator
1035 * contains the 3->8 decoding of the target ID on return.
1036 */
1037ndx_dtr:
1038	shr	A,SCSIID,4
1039	test	SBLKCTL,SELBUSB	jz ndx_dtr_2
1040	or	A,0x08		/* Channel B entries add 8 */
1041ndx_dtr_2:
1042	add	SINDEX,TARG_SCRATCH,A
1043
1044	mov	FUNCTION1,SCSIID	/* 3-bit target address decode */
1045	mov	A,FUNCTION1	ret
1046
1047/*
1048 * If we need to negotiate transfer parameters, build the WDTR or SDTR message
1049 * starting at the address passed in SINDEX.  DINDEX is modified on return.
1050 * The SCSI-II spec requires that Wide negotiation occur first and you can
1051 * only negotiat one or the other at a time otherwise in the event of a message
1052 * reject, you wouldn't be able to tell which message was the culpret.
1053 */
1054mk_dtr:
1055	test	SCB_CONTROL,0x90 jz return	/* NEEDWDTR|NEEDSDTR */
1056	test	SCB_CONTROL,NEEDWDTR jnz  mk_wdtr_16bit
1057	or	FLAGS, MAXOFFSET	/* Force an offset of 15 or 8 if WIDE */
1058
1059mk_sdtr:
1060	mvi	DINDIR,1		/* extended message */
1061	mvi	DINDIR,3		/* extended message length = 3 */
1062	mvi	DINDIR,1		/* SDTR code */
1063	call	sdtr_to_rate
1064	mov	DINDIR,RETURN_1		/* REQ/ACK transfer period */
1065	test	FLAGS, MAXOFFSET jnz mk_sdtr_max_offset
1066	and	DINDIR,0x0f,SINDIR	/* Sync Offset */
1067
1068mk_sdtr_done:
1069	add	MSG_LEN,COMP_MSG0,DINDEX ret	/* update message length */
1070
1071mk_sdtr_max_offset:
1072/*
1073 * We're initiating sync negotiation, so request the max offset we can (15 or 8)
1074 */
1075	xor	FLAGS, MAXOFFSET
1076
1077	/* Talking to a WIDE device? */
1078	test	SCSIRATE, WIDEXFER	jnz wmax_offset	
1079	mvi	DINDIR, MAX_OFFSET_8BIT
1080	jmp	mk_sdtr_done
1081
1082wmax_offset:
1083	mvi	DINDIR, MAX_OFFSET_16BIT
1084	jmp	mk_sdtr_done
1085
1086mk_wdtr_16bit:
1087	mvi	ARG_1,BUS_16_BIT
1088mk_wdtr:
1089	mvi	DINDIR,1		/* extended message */
1090	mvi	DINDIR,2		/* extended message length = 2 */
1091	mvi	DINDIR,3		/* WDTR code */
1092	mov	DINDIR,ARG_1		/* bus width */
1093
1094	add	MSG_LEN,COMP_MSG0,DINDEX ret	/* update message length */
1095	
1096/*
1097 * Set SCSI bus control signal state.  This also saves the last-written
1098 * value into a location where the higher-level driver can read it - if
1099 * it has to send an ABORT or RESET message, then it needs to know this
1100 * so it can assert ATN without upsetting SCSISIGO.  The new value is
1101 * expected in SINDEX.  Change the actual state last to avoid contention
1102 * from the driver.
1103 */
1104scsisig:
1105	mov	SIGSTATE,SINDEX
1106	mov	SCSISIGO,SINDEX	ret
1107
1108sdtr_to_rate:
1109	call	ndx_dtr			/* index scratch space for target */
1110	shr	A,SINDIR,0x4
1111	dec	SINDEX			/* Preserve SINDEX */
1112	and	A,0x7
1113	clr	RETURN_1
1114sdtr_to_rate_loop:
1115	test	A,0x0f	jz sdtr_to_rate_done
1116	add	RETURN_1,0x19
1117	dec	A	
1118	jmp	sdtr_to_rate_loop
1119sdtr_to_rate_done:
1120	shr	RETURN_1,0x2
1121	add	RETURN_1,0x19
1122	test	SXFRCTL0,ULTRAEN jz return
1123	shr	RETURN_1,0x1
1124return:
1125	ret
1126