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