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