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