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