07c1c6220ad999a5e87ae763e07f54902d0dae12
[dragonfly.git] / sys / dev / disk / aic7xxx / aic79xx.seq
1 /*
2  * Adaptec U320 device driver firmware for Linux and FreeBSD.
3  *
4  * Copyright (c) 1994-2001 Justin T. Gibbs.
5  * Copyright (c) 2000-2002 Adaptec Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification.
14  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15  *    substantially similar to the "NO WARRANTY" disclaimer below
16  *    ("Disclaimer") and any redistribution must be conditioned upon
17  *    including a substantially similar Disclaimer requirement for further
18  *    binary redistribution.
19  * 3. Neither the names of the above-listed copyright holders nor the names
20  *    of any contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * Alternatively, this software may be distributed under the terms of the
24  * GNU General Public License ("GPL") version 2 as published by the Free
25  * Software Foundation.
26  *
27  * NO WARRANTY
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR 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,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
37  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGES.
39  *
40  * $FreeBSD: src/sys/dev/aic7xxx/aic79xx.seq,v 1.2.2.5 2003/06/10 03:26:07 gibbs Exp $
41  * $DragonFly: src/sys/dev/disk/aic7xxx/aic79xx.seq,v 1.2 2003/06/17 04:28:21 dillon Exp $
42  */
43
44 VERSION = "$Id: //depot/aic7xxx/aic7xxx/aic79xx.seq#94 $"
45 PATCH_ARG_LIST = "struct ahd_softc *ahd"
46 PREFIX = "ahd_"
47
48 #include "aic79xx.reg"
49 #include "scsi_message.h"
50
51 restart:
52 if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
53         test    SEQINTCODE, 0xFF jz idle_loop;
54         SET_SEQINTCODE(NO_SEQINT)
55 }
56
57 idle_loop:
58
59         if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
60                 /*
61                  * Convert ERROR status into a sequencer
62                  * interrupt to handle the case of an
63                  * interrupt collision on the hardware
64                  * setting of HWERR.
65                  */
66                 test    ERROR, 0xFF jz no_error_set;
67                 SET_SEQINTCODE(SAW_HWERR)
68 no_error_set:
69         }
70         SET_MODE(M_SCSI, M_SCSI)
71         test    SCSISEQ0, ENSELO|ENARBO jnz idle_loop_checkbus;
72         test    SEQ_FLAGS2, SELECTOUT_QFROZEN jnz idle_loop_checkbus;
73         cmp     WAITING_TID_HEAD[1], SCB_LIST_NULL je idle_loop_checkbus;
74         /*
75          * ENSELO is cleared by a SELDO, so we must test for SELDO
76          * one last time.
77          */
78 BEGIN_CRITICAL;
79         test    SSTAT0, SELDO jnz select_out;
80 END_CRITICAL;
81         call    start_selection;
82 idle_loop_checkbus:
83 BEGIN_CRITICAL;
84         test    SSTAT0, SELDO jnz select_out;
85 END_CRITICAL;
86         test    SSTAT0, SELDI jnz select_in;
87         test    SCSIPHASE, ~DATA_PHASE_MASK jz idle_loop_check_nonpackreq;
88         test    SCSISIGO, ATNO jz idle_loop_check_nonpackreq;
89         call    unexpected_nonpkt_phase_find_ctxt;
90 idle_loop_check_nonpackreq:
91         test    SSTAT2, NONPACKREQ jz . + 2;
92         call    unexpected_nonpkt_phase_find_ctxt;
93         if ((ahd->bugs & AHD_FAINT_LED_BUG) != 0) {
94                 and     A, FIFO0FREE|FIFO1FREE, DFFSTAT;
95                 cmp     A, FIFO0FREE|FIFO1FREE jne . + 3;
96                 and     SBLKCTL, ~DIAGLEDEN|DIAGLEDON;
97                 jmp     . + 2;
98                 or      SBLKCTL, DIAGLEDEN|DIAGLEDON;
99         }
100         call    idle_loop_gsfifo_in_scsi_mode;
101         call    idle_loop_service_fifos;
102         call    idle_loop_cchan;
103         jmp     idle_loop;
104
105 BEGIN_CRITICAL;
106 idle_loop_gsfifo:
107         SET_MODE(M_SCSI, M_SCSI)
108 idle_loop_gsfifo_in_scsi_mode:
109         test    LQISTAT2, LQIGSAVAIL jz return;
110         /*
111          * We have received good status for this transaction.  There may
112          * still be data in our FIFOs draining to the host.  Complete
113          * the SCB only if all data has transferred to the host.
114          */
115 good_status_IU_done:
116         bmov    SCBPTR, GSFIFO, 2;
117         clr     SCB_SCSI_STATUS;
118         /*
119          * If a command completed before an attempted task management
120          * function completed, notify the host after disabling any
121          * pending select-outs.
122          */
123         test    SCB_TASK_MANAGEMENT, 0xFF jz gsfifo_complete_normally;
124         test    SSTAT0, SELDO|SELINGO jnz . + 2;
125         and     SCSISEQ0, ~ENSELO;
126         SET_SEQINTCODE(TASKMGMT_CMD_CMPLT_OKAY)
127 gsfifo_complete_normally:
128         or      SCB_CONTROL, STATUS_RCVD;
129
130         /*
131          * Since this status did not consume a FIFO, we have to
132          * be a bit more dilligent in how we check for FIFOs pertaining
133          * to this transaction.  There are two states that a FIFO still
134          * transferring data may be in.
135          *
136          * 1) Configured and draining to the host, with a FIFO handler.
137          * 2) Pending cfg4data, fifo not empty.
138          *
139          * Case 1 can be detected by noticing a non-zero FIFO active
140          * count in the SCB.  In this case, we allow the routine servicing
141          * the FIFO to complete the SCB.
142          * 
143          * Case 2 implies either a pending or yet to occur save data
144          * pointers for this same context in the other FIFO.  So, if
145          * we detect case 1, we will properly defer the post of the SCB
146          * and achieve the desired result.  The pending cfg4data will
147          * notice that status has been received and complete the SCB.
148          */
149         test    SCB_FIFO_USE_COUNT, 0xFF jnz idle_loop_gsfifo_in_scsi_mode;
150         call    complete;
151 END_CRITICAL;
152         jmp     idle_loop_gsfifo_in_scsi_mode;
153
154 idle_loop_service_fifos:
155         SET_MODE(M_DFF0, M_DFF0)
156         test    LONGJMP_ADDR[1], INVALID_ADDR jnz idle_loop_next_fifo;
157         call    longjmp;
158 idle_loop_next_fifo:
159         SET_MODE(M_DFF1, M_DFF1)
160         test    LONGJMP_ADDR[1], INVALID_ADDR jz longjmp;
161 return:
162         ret;
163
164 idle_loop_cchan:
165         SET_MODE(M_CCHAN, M_CCHAN)
166         test    QOFF_CTLSTA, HS_MAILBOX_ACT jz  hs_mailbox_empty;
167         mov     LOCAL_HS_MAILBOX, HS_MAILBOX;
168         or      QOFF_CTLSTA, HS_MAILBOX_ACT;
169 hs_mailbox_empty:
170 BEGIN_CRITICAL;
171         test    CCSCBCTL, CCARREN|CCSCBEN jz scbdma_idle;
172         test    CCSCBCTL, CCSCBDIR jnz fetch_new_scb_inprog;
173         test    CCSCBCTL, CCSCBDONE jz return;
174 END_CRITICAL;
175         /* FALLTHROUGH */
176 scbdma_tohost_done:
177         test    CCSCBCTL, CCARREN jz fill_qoutfifo_dmadone;
178         /*
179          * An SCB has been succesfully uploaded to the host.
180          * If the SCB was uploaded for some reason other than
181          * bad SCSI status (currently only for underruns), we
182          * queue the SCB for normal completion.  Otherwise, we
183          * wait until any select-out activity has halted, and
184          * then notify the host so that the transaction can be
185          * dealt with.
186          */
187         test    SCB_SCSI_STATUS, 0xff jnz scbdma_notify_host;
188         and     CCSCBCTL, ~(CCARREN|CCSCBEN);
189         bmov    COMPLETE_DMA_SCB_HEAD, SCB_NEXT_COMPLETE, 2;
190         bmov    SCB_NEXT_COMPLETE, COMPLETE_SCB_HEAD, 2;
191         bmov    COMPLETE_SCB_HEAD, SCBPTR, 2 ret;
192 scbdma_notify_host:
193         SET_MODE(M_SCSI, M_SCSI)
194         test    SCSISEQ0, ENSELO jnz return;
195         test    SSTAT0, (SELDO|SELINGO) jnz return;
196         SET_MODE(M_CCHAN, M_CCHAN)
197         /*
198          * Remove SCB and notify host.
199          */
200         and     CCSCBCTL, ~(CCARREN|CCSCBEN);
201         bmov    COMPLETE_DMA_SCB_HEAD, SCB_NEXT_COMPLETE, 2;
202         SET_SEQINTCODE(BAD_SCB_STATUS)
203         ret;
204 fill_qoutfifo_dmadone:
205         and     CCSCBCTL, ~(CCARREN|CCSCBEN);
206         call    qoutfifo_updated;
207         mvi     COMPLETE_SCB_DMAINPROG_HEAD[1], SCB_LIST_NULL;
208         bmov    QOUTFIFO_NEXT_ADDR, SCBHADDR, 4;
209         test    QOFF_CTLSTA, SDSCB_ROLLOVR jz return;
210         bmov    QOUTFIFO_NEXT_ADDR, SHARED_DATA_ADDR, 4;
211         xor     QOUTFIFO_ENTRY_VALID_TAG, QOUTFIFO_ENTRY_VALID_TOGGLE ret;
212
213 qoutfifo_updated:
214         /*
215          * If there are more commands waiting to be dma'ed
216          * to the host, always coalesce.  Otherwise honor the
217          * host's wishes.
218          */
219         cmp     COMPLETE_DMA_SCB_HEAD[1], SCB_LIST_NULL jne coalesce_by_count;
220         cmp     COMPLETE_SCB_HEAD[1], SCB_LIST_NULL jne coalesce_by_count;
221         test    LOCAL_HS_MAILBOX, ENINT_COALESCE jz issue_cmdcmplt;
222
223         /*
224          * If we have relatively few commands outstanding, don't
225          * bother waiting for another command to complete.
226          */
227         test    CMDS_PENDING[1], 0xFF jnz coalesce_by_count;
228         /* Add -1 so that jnc means <= not just < */
229         add     A, -1, INT_COALESCING_MINCMDS;
230         add     NONE, A, CMDS_PENDING;
231         jnc     issue_cmdcmplt;
232         
233         /*
234          * If coalescing, only coalesce up to the limit
235          * provided by the host driver.
236          */
237 coalesce_by_count:
238         mov     A, INT_COALESCING_MAXCMDS;
239         add     NONE, A, INT_COALESCING_CMDCOUNT;
240         jc      issue_cmdcmplt;
241         /*
242          * If the timer is not currently active,
243          * fire it up.
244          */
245         test    INTCTL, SWTMINTMASK jz return;
246         bmov    SWTIMER, INT_COALESCING_TIMER, 2;
247         mvi     CLRSEQINTSTAT, CLRSEQ_SWTMRTO;
248         or      INTCTL, SWTMINTEN|SWTIMER_START;
249         and     INTCTL, ~SWTMINTMASK ret;
250
251 issue_cmdcmplt:
252         mvi     INTSTAT, CMDCMPLT;
253         clr     INT_COALESCING_CMDCOUNT;
254         or      INTCTL, SWTMINTMASK ret;
255
256 BEGIN_CRITICAL;
257 fetch_new_scb_inprog:
258         test    CCSCBCTL, ARRDONE jz return;
259 fetch_new_scb_done:
260         and     CCSCBCTL, ~(CCARREN|CCSCBEN);
261         bmov    REG0, SCBPTR, 2;
262         clr     A;
263         add     CMDS_PENDING, 1;
264         adc     CMDS_PENDING[1], A;
265         if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
266                 /*
267                  * "Short Luns" are not placed into outgoing LQ
268                  * packets in the correct byte order.  Use a full
269                  * sized lun field instead and fill it with the
270                  * one byte of lun information we support.
271                  */
272                 mov     SCB_PKT_LUN[6], SCB_LUN;
273         }
274         /*
275          * The FIFO use count field is shared with the
276          * tag set by the host so that our SCB dma engine
277          * knows the correct location to store the SCB.
278          * Set it to zero before processing the SCB.
279          */
280         mov     SCB_FIFO_USE_COUNT, ALLZEROS;
281         /* Update the next SCB address to download. */
282         bmov    NEXT_QUEUED_SCB_ADDR, SCB_NEXT_SCB_BUSADDR, 4;
283         mvi     SCB_NEXT[1], SCB_LIST_NULL;
284         mvi     SCB_NEXT2[1], SCB_LIST_NULL;
285         /* Increment our position in the QINFIFO. */
286         mov     NONE, SNSCB_QOFF;
287         /*
288          * SCBs that want to send messages are always
289          * queued independently.  This ensures that they
290          * are at the head of the SCB list to select out
291          * to a target and we will see the MK_MESSAGE flag.
292          */
293         test    SCB_CONTROL, MK_MESSAGE jnz first_new_target_scb;
294         shr     SINDEX, 3, SCB_SCSIID;
295         and     SINDEX, ~0x1;
296         mvi     SINDEX[1], (WAITING_SCB_TAILS >> 8);
297         bmov    DINDEX, SINDEX, 2;
298         bmov    SCBPTR, SINDIR, 2;
299         bmov    DINDIR, REG0, 2;
300         cmp     SCBPTR[1], SCB_LIST_NULL je first_new_target_scb;
301         bmov    SCB_NEXT, REG0, 2 ret;
302 first_new_target_scb:
303         cmp     WAITING_TID_HEAD[1], SCB_LIST_NULL je first_new_scb;
304         bmov    SCBPTR, WAITING_TID_TAIL, 2;
305         bmov    SCB_NEXT2, REG0, 2;
306         bmov    WAITING_TID_TAIL, REG0, 2 ret;
307 first_new_scb:
308         bmov    WAITING_TID_HEAD, REG0, 2;
309         bmov    WAITING_TID_TAIL, REG0, 2 ret;
310 END_CRITICAL;
311
312 scbdma_idle:
313         /*
314          * Give precedence to downloading new SCBs to execute
315          * unless select-outs are currently frozen.
316          */
317         test    SEQ_FLAGS2, SELECTOUT_QFROZEN jnz . + 2;
318 BEGIN_CRITICAL;
319         test    QOFF_CTLSTA, NEW_SCB_AVAIL jnz fetch_new_scb;
320         cmp     COMPLETE_DMA_SCB_HEAD[1], SCB_LIST_NULL jne dma_complete_scb;
321         cmp     COMPLETE_SCB_HEAD[1], SCB_LIST_NULL je return;
322         /* FALLTHROUGH */
323 fill_qoutfifo:
324         /*
325          * Keep track of the SCBs we are dmaing just
326          * in case the DMA fails or is aborted.
327          */
328         mov     A, QOUTFIFO_ENTRY_VALID_TAG;
329         bmov    COMPLETE_SCB_DMAINPROG_HEAD, COMPLETE_SCB_HEAD, 2;
330         mvi     CCSCBCTL, CCSCBRESET;
331         bmov    SCBHADDR, QOUTFIFO_NEXT_ADDR, 4;
332         bmov    SCBPTR, COMPLETE_SCB_HEAD, 2;
333 fill_qoutfifo_loop:
334         mov     CCSCBRAM, SCBPTR;
335         or      CCSCBRAM, A, SCBPTR[1];
336         mov     NONE, SDSCB_QOFF;
337         inc     INT_COALESCING_CMDCOUNT;
338         add     CMDS_PENDING, -1;
339         adc     CMDS_PENDING[1], -1;
340         cmp     SCB_NEXT_COMPLETE[1], SCB_LIST_NULL je fill_qoutfifo_done;
341         cmp     CCSCBADDR, CCSCBADDR_MAX je fill_qoutfifo_done;
342         test    QOFF_CTLSTA, SDSCB_ROLLOVR jnz fill_qoutfifo_done;
343         bmov    SCBPTR, SCB_NEXT_COMPLETE, 2;
344         jmp     fill_qoutfifo_loop;
345 fill_qoutfifo_done:
346         mov     SCBHCNT, CCSCBADDR;
347         mvi     CCSCBCTL, CCSCBEN|CCSCBRESET;
348         bmov    COMPLETE_SCB_HEAD, SCB_NEXT_COMPLETE, 2;
349         mvi     SCB_NEXT_COMPLETE[1], SCB_LIST_NULL ret;
350
351 fetch_new_scb:
352         bmov    SCBHADDR, NEXT_QUEUED_SCB_ADDR, 4;
353         mvi     CCARREN|CCSCBEN|CCSCBDIR|CCSCBRESET jmp dma_scb;
354 dma_complete_scb:
355         bmov    SCBPTR, COMPLETE_DMA_SCB_HEAD, 2;
356         bmov    SCBHADDR, SCB_BUSADDR, 4;
357         mvi     CCARREN|CCSCBEN|CCSCBRESET jmp dma_scb;
358 END_CRITICAL;
359
360 /*
361  * Either post or fetch an SCB from host memory.  The caller
362  * is responsible for polling for transfer completion.
363  *
364  * Prerequisits: Mode == M_CCHAN
365  *               SINDEX contains CCSCBCTL flags
366  *               SCBHADDR set to Host SCB address
367  *               SCBPTR set to SCB src location on "push" operations
368  */
369 SET_SRC_MODE    M_CCHAN;
370 SET_DST_MODE    M_CCHAN;
371 dma_scb:
372         mvi     SCBHCNT, SCB_TRANSFER_SIZE;
373         mov     CCSCBCTL, SINDEX ret;
374
375 BEGIN_CRITICAL;
376 setjmp:
377         bmov    LONGJMP_ADDR, STACK, 2 ret;
378 setjmp_inline:
379         bmov    LONGJMP_ADDR, STACK, 2;
380 longjmp:
381         bmov    STACK, LONGJMP_ADDR, 2 ret;
382 END_CRITICAL;
383
384 /*************************** Chip Bug Work Arounds ****************************/
385 /*
386  * Must disable interrupts when setting the mode pointer
387  * register as an interrupt occurring mid update will
388  * fail to store the new mode value for restoration on
389  * an iret.
390  */
391 if ((ahd->bugs & AHD_SET_MODE_BUG) != 0) {
392 set_mode_work_around:
393         mvi     SEQINTCTL, INTVEC1DSL;
394         mov     MODE_PTR, SINDEX;
395         clr     SEQINTCTL ret;
396
397 toggle_dff_mode_work_around:
398         mvi     SEQINTCTL, INTVEC1DSL;
399         xor     MODE_PTR, MK_MODE(M_DFF1, M_DFF1);
400         clr     SEQINTCTL ret;
401 }
402
403
404 if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
405 set_seqint_work_around:
406         mov     SEQINTCODE, SINDEX;
407         mvi     SEQINTCODE, NO_SEQINT ret;
408 }
409
410 /************************ Packetized LongJmp Routines *************************/
411 SET_SRC_MODE    M_SCSI;
412 SET_DST_MODE    M_SCSI;
413 start_selection:
414 BEGIN_CRITICAL;
415         if ((ahd->bugs & AHD_SENT_SCB_UPDATE_BUG) != 0) {
416                 /*
417                  * Razor #494
418                  * Rev A hardware fails to update LAST/CURR/NEXTSCB
419                  * correctly after a packetized selection in several
420                  * situations:
421                  *
422                  * 1) If only one command existed in the queue, the
423                  *    LAST/CURR/NEXTSCB are unchanged.
424                  *
425                  * 2) In a non QAS, protocol allowed phase change,
426                  *    the queue is shifted 1 too far.  LASTSCB is
427                  *    the last SCB that was correctly processed.
428                  * 
429                  * 3) In the QAS case, if the full list of commands
430                  *    was successfully sent, NEXTSCB is NULL and neither
431                  *    CURRSCB nor LASTSCB can be trusted.  We must
432                  *    manually walk the list counting MAXCMDCNT elements
433                  *    to find the last SCB that was sent correctly.
434                  *
435                  * To simplify the workaround for this bug in SELDO
436                  * handling, we initialize LASTSCB prior to enabling
437                  * selection so we can rely on it even for case #1 above.
438                  */
439                 bmov    LASTSCB, WAITING_TID_HEAD, 2;
440         }
441         bmov    CURRSCB, WAITING_TID_HEAD, 2;
442         bmov    SCBPTR, WAITING_TID_HEAD, 2;
443         shr     SELOID, 4, SCB_SCSIID;
444         /*
445          * If we want to send a message to the device, ensure
446          * we are selecting with atn irregardless of our packetized
447          * agreement.  Since SPI4 only allows target reset or PPR
448          * messages if this is a packetized connection, the change
449          * to our negotiation table entry for this selection will
450          * be cleared when the message is acted on.
451          */
452         test    SCB_CONTROL, MK_MESSAGE jz . + 3;
453         mov     NEGOADDR, SELOID;
454         or      NEGCONOPTS, ENAUTOATNO;
455         or      SCSISEQ0, ENSELO ret;
456 END_CRITICAL;
457
458 /*
459  * Allocate a FIFO for a non-packetized transaction.
460  * In RevA hardware, both FIFOs must be free before we
461  * can allocate a FIFO for a non-packetized transaction.
462  */
463 allocate_fifo_loop:
464         /*
465          * Do whatever work is required to free a FIFO.
466          */
467         call    idle_loop_service_fifos;
468         SET_MODE(M_SCSI, M_SCSI)
469 allocate_fifo:
470         if ((ahd->bugs & AHD_NONPACKFIFO_BUG) != 0) {
471                 and     A, FIFO0FREE|FIFO1FREE, DFFSTAT;
472                 cmp     A, FIFO0FREE|FIFO1FREE jne allocate_fifo_loop;
473         } else {
474                 test    DFFSTAT, FIFO1FREE jnz allocate_fifo1;
475                 test    DFFSTAT, FIFO0FREE jz allocate_fifo_loop;
476                 mvi     DFFSTAT, B_CURRFIFO_0;
477                 SET_MODE(M_DFF0, M_DFF0)
478                 bmov    SCBPTR, ALLOCFIFO_SCBPTR, 2 ret;
479         }
480 SET_SRC_MODE    M_SCSI;
481 SET_DST_MODE    M_SCSI;
482 allocate_fifo1:
483         mvi     DFFSTAT, CURRFIFO_1;
484         SET_MODE(M_DFF1, M_DFF1)
485         bmov    SCBPTR, ALLOCFIFO_SCBPTR, 2 ret;
486
487 /*
488  * We have been reselected as an initiator
489  * or selected as a target.
490  */
491 SET_SRC_MODE    M_SCSI;
492 SET_DST_MODE    M_SCSI;
493 select_in:
494         if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
495                 /*
496                  * This exposes a window whereby a
497                  * busfree just after a selection will
498                  * be missed, but there is no other safe
499                  * way to enable busfree detection if
500                  * the busfreerev function is broken.
501                  */
502                 mvi     CLRSINT1,CLRBUSFREE;
503                 or      SIMODE1, ENBUSFREE;
504         }
505         or      SXFRCTL0, SPIOEN;
506         and     SAVED_SCSIID, SELID_MASK, SELID;
507         and     A, OID, IOWNID;
508         or      SAVED_SCSIID, A;
509         mvi     CLRSINT0, CLRSELDI;
510         jmp     ITloop;
511
512 /*
513  * We have successfully selected out.
514  *
515  * Clear SELDO.
516  * Dequeue all SCBs sent from the waiting queue
517  * Requeue all SCBs *not* sent to the tail of the waiting queue
518  * Take Razor #494 into account for above.
519  *
520  * In Packetized Mode:
521  *      Return to the idle loop.  Our interrupt handler will take
522  *      care of any incoming L_Qs.
523  *
524  * In Non-Packetize Mode:
525  *      Continue to our normal state machine.
526  */
527 SET_SRC_MODE    M_SCSI;
528 SET_DST_MODE    M_SCSI;
529 select_out:
530 BEGIN_CRITICAL;
531         /* Clear out all SCBs that have been successfully sent. */
532         if ((ahd->bugs & AHD_SENT_SCB_UPDATE_BUG) != 0) {
533                 /*
534                  * For packetized, the LQO manager clears ENSELO on
535                  * the assertion of SELDO.  If we are non-packetized,
536                  * LASTSCB and CURRSCB are accurate.
537                  */
538                 test    SCSISEQ0, ENSELO jnz use_lastscb;
539
540                 /*
541                  * The update is correct for LQOSTAT1 errors.  All
542                  * but LQOBUSFREE are handled by kernel interrupts.
543                  * If we see LQOBUSFREE, return to the idle loop.
544                  * Once we are out of the select_out critical section,
545                  * the kernel will cleanup the LQOBUSFREE and we will
546                  * eventually restart the selection if appropriate.
547                  */
548                 test    LQOSTAT1, LQOBUSFREE jnz idle_loop;
549
550                 /*
551                  * On a phase change oustside of packet boundaries,
552                  * LASTSCB points to the currently active SCB context
553                  * on the bus.
554                  */
555                 test    LQOSTAT2, LQOPHACHGOUTPKT jnz use_lastscb;
556
557                 /*
558                  * If the hardware has traversed the whole list, NEXTSCB
559                  * will be NULL, CURRSCB and LASTSCB cannot be trusted,
560                  * but MAXCMDCNT is accurate.  If we stop part way through
561                  * the list or only had one command to issue, NEXTSCB[1] is
562                  * not NULL and LASTSCB is the last command to go out.
563                  */
564                 cmp     NEXTSCB[1], SCB_LIST_NULL jne use_lastscb;
565
566                 /*
567                  * Brute force walk.
568                  */
569                 bmov    SCBPTR, WAITING_TID_HEAD, 2;
570                 mvi     SEQINTCTL, INTVEC1DSL;
571                 mvi     MODE_PTR, MK_MODE(M_CFG, M_CFG);
572                 mov     A, MAXCMDCNT;
573                 mvi     MODE_PTR, MK_MODE(M_SCSI, M_SCSI);
574                 clr     SEQINTCTL;
575 find_lastscb_loop:
576                 dec     A;
577                 test    A, 0xFF jz found_last_sent_scb;
578                 bmov    SCBPTR, SCB_NEXT, 2;
579                 jmp     find_lastscb_loop;
580 use_lastscb:
581                 bmov    SCBPTR, LASTSCB, 2;
582 found_last_sent_scb:
583                 bmov    CURRSCB, SCBPTR, 2;
584 curscb_ww_done:
585         } else {
586                 /*
587                  * Untested - Verify with Rev B.
588                  */
589                 bmov    SCBPTR, CURRSCB, 2;
590         }
591
592         /*
593          * Requeue any SCBs not sent, to the tail of the waiting Q.
594          */
595         cmp     SCB_NEXT[1], SCB_LIST_NULL je select_out_list_done;
596
597         /*
598          * We know that neither the per-TID list nor the list of
599          * TIDs is empty.  Use this knowledge to our advantage.
600          */
601         bmov    REG0, SCB_NEXT, 2;
602         bmov    SCBPTR, WAITING_TID_TAIL, 2;
603         bmov    SCB_NEXT2, REG0, 2;
604         bmov    WAITING_TID_TAIL, REG0, 2;
605         jmp     select_out_inc_tid_q;
606
607 select_out_list_done:
608         /*
609          * The whole list made it.  Just clear our TID's tail pointer
610          * unless we were queued independently due to our need to
611          * send a message.
612          */
613         test    SCB_CONTROL, MK_MESSAGE jnz select_out_inc_tid_q;
614         shr     DINDEX, 3, SCB_SCSIID;
615         or      DINDEX, 1;      /* Want only the second byte */
616         mvi     DINDEX[1], ((WAITING_SCB_TAILS) >> 8);
617         mvi     DINDIR, SCB_LIST_NULL;
618 select_out_inc_tid_q:
619         bmov    SCBPTR, WAITING_TID_HEAD, 2;
620         bmov    WAITING_TID_HEAD, SCB_NEXT2, 2;
621         cmp     WAITING_TID_HEAD[1], SCB_LIST_NULL jne . + 2;
622         mvi     WAITING_TID_TAIL[1], SCB_LIST_NULL;
623         bmov    SCBPTR, CURRSCB, 2;
624         mvi     CLRSINT0, CLRSELDO;
625         test    LQOSTAT2, LQOPHACHGOUTPKT jnz unexpected_nonpkt_phase;
626         test    LQOSTAT1, LQOPHACHGINPKT jnz unexpected_nonpkt_phase;
627
628         /*
629          * If this is a packetized connection, return to our
630          * idle_loop and let our interrupt handler deal with
631          * any connection setup/teardown issues.  The only
632          * exceptions are the case of MK_MESSAGE and task management
633          * SCBs.
634          */
635         if ((ahd->bugs & AHD_LQO_ATNO_BUG) != 0) {
636                 /*
637                  * In the A, the LQO manager transitions to LQOSTOP0 even if
638                  * we have selected out with ATN asserted and the target
639                  * REQs in a non-packet phase.
640                  */
641                 test    SCB_CONTROL, MK_MESSAGE jz select_out_no_message;
642                 test    SCSISIGO, ATNO jnz select_out_non_packetized;
643 select_out_no_message:
644         }
645         test    LQOSTAT2, LQOSTOP0 jz select_out_non_packetized;
646         test    SCB_TASK_MANAGEMENT, 0xFF jz idle_loop;
647         SET_SEQINTCODE(TASKMGMT_FUNC_COMPLETE)
648         jmp     idle_loop;
649
650 select_out_non_packetized:
651         /* Non packetized request. */
652         and     SCSISEQ0, ~ENSELO;
653         if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
654                 /*
655                  * This exposes a window whereby a
656                  * busfree just after a selection will
657                  * be missed, but there is no other safe
658                  * way to enable busfree detection if
659                  * the busfreerev function is broken.
660                  */
661                 mvi     CLRSINT1,CLRBUSFREE;
662                 or      SIMODE1, ENBUSFREE;
663         }
664         mov     SAVED_SCSIID, SCB_SCSIID;
665         mov     SAVED_LUN, SCB_LUN;
666         mvi     SEQ_FLAGS, NO_CDB_SENT;
667 END_CRITICAL;
668         or      SXFRCTL0, SPIOEN;
669
670         /*
671          * As soon as we get a successful selection, the target
672          * should go into the message out phase since we have ATN
673          * asserted.
674          */
675         mvi     MSG_OUT, MSG_IDENTIFYFLAG;
676
677         /*
678          * Main loop for information transfer phases.  Wait for the
679          * target to assert REQ before checking MSG, C/D and I/O for
680          * the bus phase.
681          */
682 mesgin_phasemis:
683 ITloop:
684         call    phase_lock;
685
686         mov     A, LASTPHASE;
687
688         test    A, ~P_DATAIN_DT jz p_data;
689         cmp     A,P_COMMAND     je p_command;
690         cmp     A,P_MESGOUT     je p_mesgout;
691         cmp     A,P_STATUS      je p_status;
692         cmp     A,P_MESGIN      je p_mesgin;
693
694         SET_SEQINTCODE(BAD_PHASE)
695         jmp     ITloop;                 /* Try reading the bus again. */
696
697 /*
698  * Command phase.  Set up the DMA registers and let 'er rip.
699  */
700 p_command:
701         test    SEQ_FLAGS, NOT_IDENTIFIED jz p_command_okay;
702         SET_SEQINTCODE(PROTO_VIOLATION)
703 p_command_okay:
704         test    MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
705                 jnz p_command_allocate_fifo;
706         /*
707          * Command retry.  Free our current FIFO and
708          * re-allocate a FIFO so transfer state is
709          * reset.
710          */
711 SET_SRC_MODE    M_DFF1;
712 SET_DST_MODE    M_DFF1;
713         mvi     DFFSXFRCTL, RSTCHN|CLRSHCNT;
714         SET_MODE(M_SCSI, M_SCSI)
715 p_command_allocate_fifo:
716         bmov    ALLOCFIFO_SCBPTR, SCBPTR, 2;
717         call    allocate_fifo;
718 SET_SRC_MODE    M_DFF1;
719 SET_DST_MODE    M_DFF1;
720         add     NONE, -17, SCB_CDB_LEN;
721         jnc     p_command_embedded;
722 p_command_from_host:
723         bmov    HADDR[0], SCB_HOST_CDB_PTR, 9;
724         mvi     SG_CACHE_PRE, LAST_SEG;
725         mvi     DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN);
726         jmp     p_command_xfer;
727 p_command_embedded:
728         bmov    SHCNT[0], SCB_CDB_LEN,  1;
729         bmov    DFDAT, SCB_CDB_STORE, 16; 
730         mvi     DFCNTRL, SCSIEN;
731 p_command_xfer:
732         and     SEQ_FLAGS, ~NO_CDB_SENT;
733         test    DFCNTRL, SCSIEN jnz .;
734         /*
735          * DMA Channel automatically disabled.
736          * Don't allow a data phase if the command
737          * was not fully transferred.
738          */
739         test    SSTAT2, SDONE jnz ITloop;
740         or      SEQ_FLAGS, NO_CDB_SENT;
741         jmp     ITloop;
742
743
744 /*
745  * Status phase.  Wait for the data byte to appear, then read it
746  * and store it into the SCB.
747  */
748 SET_SRC_MODE    M_SCSI;
749 SET_DST_MODE    M_SCSI;
750 p_status:
751         test    SEQ_FLAGS,NOT_IDENTIFIED jnz mesgin_proto_violation;
752 p_status_okay:
753         mov     SCB_SCSI_STATUS, SCSIDAT;
754         or      SCB_CONTROL, STATUS_RCVD;
755         jmp     ITloop;
756
757 /*
758  * Message out phase.  If MSG_OUT is MSG_IDENTIFYFLAG, build a full
759  * indentify message sequence and send it to the target.  The host may
760  * override this behavior by setting the MK_MESSAGE bit in the SCB
761  * control byte.  This will cause us to interrupt the host and allow
762  * it to handle the message phase completely on its own.  If the bit
763  * associated with this target is set, we will also interrupt the host,
764  * thereby allowing it to send a message on the next selection regardless
765  * of the transaction being sent.
766  * 
767  * If MSG_OUT is == HOST_MSG, also interrupt the host and take a message.
768  * This is done to allow the host to send messages outside of an identify
769  * sequence while protecting the seqencer from testing the MK_MESSAGE bit
770  * on an SCB that might not be for the current nexus. (For example, a
771  * BDR message in responce to a bad reselection would leave us pointed to
772  * an SCB that doesn't have anything to do with the current target).
773  *
774  * Otherwise, treat MSG_OUT as a 1 byte message to send (abort, abort tag,
775  * bus device reset).
776  *
777  * When there are no messages to send, MSG_OUT should be set to MSG_NOOP,
778  * in case the target decides to put us in this phase for some strange
779  * reason.
780  */
781 p_mesgout_retry:
782         /* Turn on ATN for the retry */
783         mvi     SCSISIGO, ATNO;
784 p_mesgout:
785         mov     SINDEX, MSG_OUT;
786         cmp     SINDEX, MSG_IDENTIFYFLAG jne p_mesgout_from_host;
787         test    SCB_CONTROL,MK_MESSAGE  jnz host_message_loop;
788 p_mesgout_identify:
789         or      SINDEX, MSG_IDENTIFYFLAG|DISCENB, SCB_LUN;
790         test    SCB_CONTROL, DISCENB jnz . + 2;
791         and     SINDEX, ~DISCENB;
792 /*
793  * Send a tag message if TAG_ENB is set in the SCB control block.
794  * Use SCB_NONPACKET_TAG as the tag value.
795  */
796 p_mesgout_tag:
797         test    SCB_CONTROL,TAG_ENB jz  p_mesgout_onebyte;
798         mov     SCSIDAT, SINDEX;        /* Send the identify message */
799         call    phase_lock;
800         cmp     LASTPHASE, P_MESGOUT    jne p_mesgout_done;
801         and     SCSIDAT,TAG_ENB|SCB_TAG_TYPE,SCB_CONTROL;
802         call    phase_lock;
803         cmp     LASTPHASE, P_MESGOUT    jne p_mesgout_done;
804         mov     SCBPTR jmp p_mesgout_onebyte;
805 /*
806  * Interrupt the driver, and allow it to handle this message
807  * phase and any required retries.
808  */
809 p_mesgout_from_host:
810         cmp     SINDEX, HOST_MSG        jne p_mesgout_onebyte;
811         jmp     host_message_loop;
812
813 p_mesgout_onebyte:
814         mvi     CLRSINT1, CLRATNO;
815         mov     SCSIDAT, SINDEX;
816
817 /*
818  * If the next bus phase after ATN drops is message out, it means
819  * that the target is requesting that the last message(s) be resent.
820  */
821         call    phase_lock;
822         cmp     LASTPHASE, P_MESGOUT    je p_mesgout_retry;
823
824 p_mesgout_done:
825         mvi     CLRSINT1,CLRATNO;       /* Be sure to turn ATNO off */
826         mov     LAST_MSG, MSG_OUT;
827         mvi     MSG_OUT, MSG_NOOP;      /* No message left */
828         jmp     ITloop;
829
830 /*
831  * Message in phase.  Bytes are read using Automatic PIO mode.
832  */
833 p_mesgin:
834         /* read the 1st message byte */
835         mvi     ACCUM           call inb_first;
836
837         test    A,MSG_IDENTIFYFLAG      jnz mesgin_identify;
838         cmp     A,MSG_DISCONNECT        je mesgin_disconnect;
839         cmp     A,MSG_SAVEDATAPOINTER   je mesgin_sdptrs;
840         cmp     ALLZEROS,A              je mesgin_complete;
841         cmp     A,MSG_RESTOREPOINTERS   je mesgin_rdptrs;
842         cmp     A,MSG_IGN_WIDE_RESIDUE  je mesgin_ign_wide_residue;
843         cmp     A,MSG_NOOP              je mesgin_done;
844
845 /*
846  * Pushed message loop to allow the kernel to
847  * run it's own message state engine.  To avoid an
848  * extra nop instruction after signaling the kernel,
849  * we perform the phase_lock before checking to see
850  * if we should exit the loop and skip the phase_lock
851  * in the ITloop.  Performing back to back phase_locks
852  * shouldn't hurt, but why do it twice...
853  */
854 host_message_loop:
855         call    phase_lock;     /* Benign the first time through. */
856         SET_SEQINTCODE(HOST_MSG_LOOP)
857         cmp     RETURN_1, EXIT_MSG_LOOP je ITloop;
858         cmp     RETURN_1, CONT_MSG_LOOP_WRITE   jne . + 3;
859         mov     SCSIDAT, RETURN_2;
860         jmp     host_message_loop;
861         /* Must be CONT_MSG_LOOP_READ */
862         mov     NONE, SCSIDAT;  /* ACK Byte */
863         jmp     host_message_loop;
864
865 mesgin_ign_wide_residue:
866         mov     SAVED_MODE, MODE_PTR;
867         SET_MODE(M_SCSI, M_SCSI)
868         shr     NEGOADDR, 4, SAVED_SCSIID;
869         mov     A, NEGCONOPTS;
870         RESTORE_MODE(SAVED_MODE)
871         test    A, WIDEXFER jz mesgin_reject;
872         /* Pull the residue byte */
873         mvi     REG0    call inb_next;
874         cmp     REG0, 0x01 jne mesgin_reject;
875         test    SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz . + 2;
876         test    SCB_TASK_ATTRIBUTE, SCB_XFERLEN_ODD jnz mesgin_done;
877         SET_SEQINTCODE(IGN_WIDE_RES)
878         jmp     mesgin_done;
879
880 mesgin_proto_violation:
881         SET_SEQINTCODE(PROTO_VIOLATION)
882         jmp     mesgin_done;
883 mesgin_reject:
884         mvi     MSG_MESSAGE_REJECT      call mk_mesg;
885 mesgin_done:
886         mov     NONE,SCSIDAT;           /*dummy read from latch to ACK*/
887         jmp     ITloop;
888
889 #define INDEX_DISC_LIST(scsiid, lun)                                    \
890         and     A, 0xC0, scsiid;                                        \
891         or      SCBPTR, A, lun;                                         \
892         clr     SCBPTR[1];                                              \
893         and     SINDEX, 0x30, scsiid;                                   \
894         shr     SINDEX, 3;      /* Multiply by 2 */                     \
895         add     SINDEX, (SCB_DISCONNECTED_LISTS & 0xFF);                \
896         mvi     SINDEX[1], ((SCB_DISCONNECTED_LISTS >> 8) & 0xFF)
897
898 mesgin_identify:
899         /*
900          * Determine whether a target is using tagged or non-tagged
901          * transactions by first looking at the transaction stored in
902          * the per-device, disconnected array.  If there is no untagged
903          * transaction for this target, this must be a tagged transaction.
904          */
905         and     SAVED_LUN, MSG_IDENTIFY_LUNMASK, A;
906         INDEX_DISC_LIST(SAVED_SCSIID, SAVED_LUN);
907         bmov    DINDEX, SINDEX, 2;
908         bmov    REG0, SINDIR, 2;
909         cmp     REG0[1], SCB_LIST_NULL je snoop_tag;
910         /* Untagged.  Clear the busy table entry and setup the SCB. */
911         bmov    DINDIR, ALLONES, 2;
912         bmov    SCBPTR, REG0, 2;
913         jmp     setup_SCB;
914
915 /*
916  * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
917  * If we get one, we use the tag returned to find the proper
918  * SCB.  After receiving the tag, look for the SCB at SCB locations tag and
919  * tag + 256.
920  */
921 snoop_tag:
922         if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
923                 or      SEQ_FLAGS, 0x80;
924         }
925         mov     NONE, SCSIDAT;          /* ACK Identify MSG */
926         call    phase_lock;
927         if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
928                 or      SEQ_FLAGS, 0x1;
929         }
930         cmp     LASTPHASE, P_MESGIN     jne not_found_ITloop;
931         if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
932                 or      SEQ_FLAGS, 0x2;
933         }
934         cmp     SCSIBUS, MSG_SIMPLE_Q_TAG jne not_found;
935 get_tag:
936         clr     SCBPTR[1];
937         mvi     SCBPTR  call inb_next;  /* tag value */
938 verify_scb:
939         test    SCB_CONTROL,DISCONNECTED jz verify_other_scb;
940         mov     A, SAVED_SCSIID;
941         cmp     SCB_SCSIID, A jne verify_other_scb;
942         mov     A, SAVED_LUN;
943         cmp     SCB_LUN, A je setup_SCB_disconnected;
944 verify_other_scb:
945         xor     SCBPTR[1], 1;
946         test    SCBPTR[1], 0xFF jnz verify_scb;
947         jmp     not_found;
948
949 /*
950  * Ensure that the SCB the tag points to is for
951  * an SCB transaction to the reconnecting target.
952  */
953 setup_SCB:
954         if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
955                 or      SEQ_FLAGS, 0x10;
956         }
957         test    SCB_CONTROL,DISCONNECTED jz not_found;
958 setup_SCB_disconnected:
959         and     SCB_CONTROL,~DISCONNECTED;
960         clr     SEQ_FLAGS;      /* make note of IDENTIFY */
961         test    SCB_SGPTR, SG_LIST_NULL jnz . + 3;
962         bmov    ALLOCFIFO_SCBPTR, SCBPTR, 2;
963         call    allocate_fifo;
964         /* See if the host wants to send a message upon reconnection */
965         test    SCB_CONTROL, MK_MESSAGE jz mesgin_done;
966         mvi     HOST_MSG        call mk_mesg;
967         jmp     mesgin_done;
968
969 not_found:
970         SET_SEQINTCODE(NO_MATCH)
971         jmp     mesgin_done;
972
973 not_found_ITloop:
974         SET_SEQINTCODE(NO_MATCH)
975         jmp     ITloop;
976
977 /*
978  * We received a "command complete" message.  Put the SCB on the complete
979  * queue and trigger a completion interrupt via the idle loop.  Before doing
980  * so, check to see if there
981  * is a residual or the status byte is something other than STATUS_GOOD (0).
982  * In either of these conditions, we upload the SCB back to the host so it can
983  * process this information.  In the case of a non zero status byte, we 
984  * additionally interrupt the kernel driver synchronously, allowing it to
985  * decide if sense should be retrieved.  If the kernel driver wishes to request
986  * sense, it will fill the kernel SCB with a request sense command, requeue
987  * it to the QINFIFO and tell us not to post to the QOUTFIFO by setting 
988  * RETURN_1 to SEND_SENSE.
989  */
990 mesgin_complete:
991
992         /*
993          * If ATN is raised, we still want to give the target a message.
994          * Perhaps there was a parity error on this last message byte.
995          * Either way, the target should take us to message out phase
996          * and then attempt to complete the command again.  We should use a
997          * critical section here to guard against a timeout triggering
998          * for this command and setting ATN while we are still processing
999          * the completion.
1000         test    SCSISIGI, ATNI jnz mesgin_done;
1001          */
1002
1003         /*
1004          * If we are identified and have successfully sent the CDB,
1005          * any status will do.  Optimize this fast path.
1006          */
1007         test    SCB_CONTROL, STATUS_RCVD jz mesgin_proto_violation;
1008         test    SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT jz complete_accepted;
1009
1010         /*
1011          * If the target never sent an identify message but instead went
1012          * to mesgin to give an invalid message, let the host abort us.
1013          */
1014         test    SEQ_FLAGS, NOT_IDENTIFIED jnz mesgin_proto_violation;
1015
1016         /*
1017          * If we recevied good status but never successfully sent the
1018          * cdb, abort the command.
1019          */
1020         test    SCB_SCSI_STATUS,0xff    jnz complete_accepted;
1021         test    SEQ_FLAGS, NO_CDB_SENT jnz mesgin_proto_violation;
1022 complete_accepted:
1023
1024         /*
1025          * See if we attempted to deliver a message but the target ingnored us.
1026          */
1027         test    SCB_CONTROL, MK_MESSAGE jz complete_nomsg;
1028         SET_SEQINTCODE(MKMSG_FAILED)
1029 complete_nomsg:
1030         call    queue_scb_completion;
1031         jmp     await_busfree;
1032
1033 freeze_queue:
1034         /* Cancel any pending select-out. */
1035         test    SSTAT0, SELDO|SELINGO jnz . + 2;
1036         and     SCSISEQ0, ~ENSELO;
1037         mov     ACCUM_SAVE, A;
1038         clr     A;
1039         add     QFREEZE_COUNT, 1;
1040         adc     QFREEZE_COUNT[1], A;
1041         or      SEQ_FLAGS2, SELECTOUT_QFROZEN;
1042         mov     A, ACCUM_SAVE ret;
1043
1044 /*
1045  * Complete the current FIFO's SCB if data for this same
1046  * SCB is not transferring in the other FIFO.
1047  */
1048 SET_SRC_MODE    M_DFF1;
1049 SET_DST_MODE    M_DFF1;
1050 pkt_complete_scb_if_fifos_idle:
1051         bmov    ARG_1, SCBPTR, 2;
1052         mvi     DFFSXFRCTL, CLRCHN;
1053         SET_MODE(M_SCSI, M_SCSI)
1054         bmov    SCBPTR, ARG_1, 2;
1055         test    SCB_FIFO_USE_COUNT, 0xFF jnz return;
1056 queue_scb_completion:
1057         test    SCB_SCSI_STATUS,0xff    jnz bad_status;
1058         /*
1059          * Check for residuals
1060          */
1061         test    SCB_SGPTR, SG_LIST_NULL jnz complete;   /* No xfer */
1062         test    SCB_SGPTR, SG_FULL_RESID jnz upload_scb;/* Never xfered */
1063         test    SCB_RESIDUAL_SGPTR, SG_LIST_NULL jz upload_scb;
1064 complete:
1065         bmov    SCB_NEXT_COMPLETE, COMPLETE_SCB_HEAD, 2;
1066         bmov    COMPLETE_SCB_HEAD, SCBPTR, 2 ret;
1067 bad_status:
1068         cmp     SCB_SCSI_STATUS, STATUS_PKT_SENSE je upload_scb;
1069         call    freeze_queue;
1070 upload_scb:
1071         /*
1072          * Restore SCB TAG since we reuse this field
1073          * in the sequencer.  We don't want to corrupt
1074          * it on the host.
1075          */
1076         bmov    SCB_TAG, SCBPTR, 2;
1077         bmov    SCB_NEXT_COMPLETE, COMPLETE_DMA_SCB_HEAD, 2;
1078         bmov    COMPLETE_DMA_SCB_HEAD, SCBPTR, 2;
1079         or      SCB_SGPTR, SG_STATUS_VALID ret;
1080
1081 /*
1082  * Is it a disconnect message?  Set a flag in the SCB to remind us
1083  * and await the bus going free.  If this is an untagged transaction
1084  * store the SCB id for it in our untagged target table for lookup on
1085  * a reselction.
1086  */
1087 mesgin_disconnect:
1088         /*
1089          * If ATN is raised, we still want to give the target a message.
1090          * Perhaps there was a parity error on this last message byte
1091          * or we want to abort this command.  Either way, the target
1092          * should take us to message out phase and then attempt to
1093          * disconnect again.
1094          * XXX - Wait for more testing.
1095         test    SCSISIGI, ATNI jnz mesgin_done;
1096          */
1097         test    SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT
1098                 jnz mesgin_proto_violation;
1099         or      SCB_CONTROL,DISCONNECTED;
1100         test    SCB_CONTROL, TAG_ENB jnz await_busfree;
1101 queue_disc_scb:
1102         bmov    REG0, SCBPTR, 2;
1103         INDEX_DISC_LIST(SAVED_SCSIID, SAVED_LUN);
1104         bmov    DINDEX, SINDEX, 2;
1105         bmov    DINDIR, REG0, 2;
1106         bmov    SCBPTR, REG0, 2;
1107         /* FALLTHROUGH */
1108 await_busfree:
1109         and     SIMODE1, ~ENBUSFREE;
1110         if ((ahd->bugs & AHD_BUSFREEREV_BUG) == 0) {
1111                 /*
1112                  * In the BUSFREEREV_BUG case, the
1113                  * busfree status was cleared at the
1114                  * beginning of the connection.
1115                  */
1116                 mvi     CLRSINT1,CLRBUSFREE;
1117         }
1118         mov     NONE, SCSIDAT;          /* Ack the last byte */
1119         test    MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
1120                 jnz await_busfree_not_m_dff;
1121 SET_SRC_MODE    M_DFF1;
1122 SET_DST_MODE    M_DFF1;
1123 await_busfree_clrchn:
1124         mvi     DFFSXFRCTL, CLRCHN;
1125 await_busfree_not_m_dff:
1126         call    clear_target_state;
1127         test    SSTAT1,REQINIT|BUSFREE  jz .;
1128         test    SSTAT1, BUSFREE jnz idle_loop;
1129         SET_SEQINTCODE(MISSED_BUSFREE)
1130
1131
1132 /*
1133  * Save data pointers message:
1134  * Copying RAM values back to SCB, for Save Data Pointers message, but
1135  * only if we've actually been into a data phase to change them.  This
1136  * protects against bogus data in scratch ram and the residual counts
1137  * since they are only initialized when we go into data_in or data_out.
1138  * Ack the message as soon as possible.
1139  */
1140 SET_SRC_MODE    M_DFF1;
1141 SET_DST_MODE    M_DFF1;
1142 mesgin_sdptrs:
1143         mov     NONE,SCSIDAT;           /*dummy read from latch to ACK*/
1144         test    SEQ_FLAGS, DPHASE       jz ITloop;
1145         call    save_pointers;
1146         jmp     ITloop;
1147
1148 save_pointers:
1149         /*
1150          * If we are asked to save our position at the end of the
1151          * transfer, just mark us at the end rather than perform a
1152          * full save.
1153          */
1154         test    SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz save_pointers_full;
1155         or      SCB_SGPTR, SG_LIST_NULL ret;
1156
1157 save_pointers_full:
1158         /*
1159          * The SCB_DATAPTR becomes the current SHADDR.
1160          * All other information comes directly from our residual
1161          * state.
1162          */
1163         bmov    SCB_DATAPTR, SHADDR, 8;
1164         bmov    SCB_DATACNT, SCB_RESIDUAL_DATACNT, 8 ret;
1165
1166 /*
1167  * Restore pointers message?  Data pointers are recopied from the
1168  * SCB anytime we enter a data phase for the first time, so all
1169  * we need to do is clear the DPHASE flag and let the data phase
1170  * code do the rest.  We also reset/reallocate the FIFO to make
1171  * sure we have a clean start for the next data or command phase.
1172  */
1173 mesgin_rdptrs:
1174         and     SEQ_FLAGS, ~DPHASE;
1175         test    MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1)) jnz msgin_rdptrs_get_fifo;
1176         mvi     DFFSXFRCTL, RSTCHN|CLRSHCNT;
1177         SET_MODE(M_SCSI, M_SCSI)
1178 msgin_rdptrs_get_fifo:
1179         call    allocate_fifo;
1180         jmp     mesgin_done;
1181
1182 clear_target_state:
1183         mvi     LASTPHASE, P_BUSFREE;
1184         /* clear target specific flags */
1185         mvi     SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT ret;
1186
1187 phase_lock:     
1188         if ((ahd->bugs & AHD_EARLY_REQ_BUG) != 0) {
1189                 /*
1190                  * Don't ignore persistent REQ assertions just because
1191                  * they were asserted within the bus settle delay window.
1192                  * This allows us to tolerate devices like the GEM318
1193                  * that violate the SCSI spec.  We are careful not to
1194                  * count REQ while we are waiting for it to fall during
1195                  * an async phase due to our asserted ACK.  Each
1196                  * sequencer instruction takes ~25ns, so the REQ must
1197                  * last at least 100ns in order to be counted as a true
1198                  * REQ.
1199                  */
1200                 test    SCSIPHASE, 0xFF jnz phase_locked;
1201                 test    SCSISIGI, ACKI jnz phase_lock;
1202                 test    SCSISIGI, REQI jz phase_lock;
1203                 test    SCSIPHASE, 0xFF jnz phase_locked;
1204                 test    SCSISIGI, ACKI jnz phase_lock;
1205                 test    SCSISIGI, REQI jz phase_lock;
1206 phase_locked:
1207         } else {
1208                 test    SCSIPHASE, 0xFF jz .;
1209         }
1210         test    SSTAT1, SCSIPERR jnz phase_lock;
1211 phase_lock_latch_phase:
1212         and     LASTPHASE, PHASE_MASK, SCSISIGI ret;
1213
1214 /*
1215  * Functions to read data in Automatic PIO mode.
1216  *
1217  * An ACK is not sent on input from the target until SCSIDATL is read from.
1218  * So we wait until SCSIDATL is latched (the usual way), then read the data
1219  * byte directly off the bus using SCSIBUSL.  When we have pulled the ATN
1220  * line, or we just want to acknowledge the byte, then we do a dummy read
1221  * from SCISDATL.  The SCSI spec guarantees that the target will hold the
1222  * data byte on the bus until we send our ACK.
1223  *
1224  * The assumption here is that these are called in a particular sequence,
1225  * and that REQ is already set when inb_first is called.  inb_{first,next}
1226  * use the same calling convention as inb.
1227  */
1228 inb_next:
1229         mov     NONE,SCSIDAT;           /*dummy read from latch to ACK*/
1230 inb_next_wait:
1231         /*
1232          * If there is a parity error, wait for the kernel to
1233          * see the interrupt and prepare our message response
1234          * before continuing.
1235          */
1236         test    SCSIPHASE, 0xFF jz .;
1237         test    SSTAT1, SCSIPERR jnz inb_next_wait;
1238 inb_next_check_phase:
1239         and     LASTPHASE, PHASE_MASK, SCSISIGI;
1240         cmp     LASTPHASE, P_MESGIN jne mesgin_phasemis;
1241 inb_first:
1242         clr     DINDEX[1];
1243         mov     DINDEX,SINDEX;
1244         mov     DINDIR,SCSIBUS  ret;            /*read byte directly from bus*/
1245 inb_last:
1246         mov     NONE,SCSIDAT ret;               /*dummy read from latch to ACK*/
1247
1248 mk_mesg:
1249         mvi     SCSISIGO, ATNO;
1250         mov     MSG_OUT,SINDEX ret;
1251
1252 SET_SRC_MODE    M_DFF1;
1253 SET_DST_MODE    M_DFF1;
1254 disable_ccsgen:
1255         test    SG_STATE, FETCH_INPROG jz disable_ccsgen_fetch_done;
1256         clr     CCSGCTL;
1257 disable_ccsgen_fetch_done:
1258         clr     SG_STATE ret;
1259
1260 service_fifo:
1261         /*
1262          * Do we have any prefetch left???
1263          */
1264         test    SG_STATE, SEGS_AVAIL jnz idle_sg_avail;
1265
1266         /*
1267          * Can this FIFO have access to the S/G cache yet?
1268          */
1269         test    CCSGCTL, SG_CACHE_AVAIL jz return;
1270
1271         /* Did we just finish fetching segs? */
1272         test    CCSGCTL, CCSGDONE jnz idle_sgfetch_complete;
1273
1274         /* Are we actively fetching segments? */
1275         test    CCSGCTL, CCSGENACK jnz return;
1276
1277         /*
1278          * We fetch a "cacheline aligned" and sized amount of data
1279          * so we don't end up referencing a non-existant page.
1280          * Cacheline aligned is in quotes because the kernel will
1281          * set the prefetch amount to a reasonable level if the
1282          * cacheline size is unknown.
1283          */
1284         bmov    SGHADDR, SCB_RESIDUAL_SGPTR, 4;
1285         mvi     SGHCNT, SG_PREFETCH_CNT;
1286         if ((ahd->bugs & AHD_REG_SLOW_SETTLE_BUG) != 0) {
1287                 /*
1288                  * Need two instruction between "touches" of SGHADDR.
1289                  */
1290                 nop;
1291         }
1292         and     SGHADDR[0], SG_PREFETCH_ALIGN_MASK, SCB_RESIDUAL_SGPTR;
1293         mvi     CCSGCTL, CCSGEN|CCSGRESET;
1294         or      SG_STATE, FETCH_INPROG ret;
1295 idle_sgfetch_complete:
1296         /*
1297          * Guard against SG_CACHE_AVAIL activating during sg fetch
1298          * request in the other FIFO.
1299          */
1300         test    SG_STATE, FETCH_INPROG jz return;
1301         clr     CCSGCTL;
1302         and     CCSGADDR, SG_PREFETCH_ADDR_MASK, SCB_RESIDUAL_SGPTR;
1303         mvi     SG_STATE, SEGS_AVAIL|LOADING_NEEDED;
1304 idle_sg_avail:
1305         /* Does the hardware have space for another SG entry? */
1306         test    DFSTATUS, PRELOAD_AVAIL jz return;
1307         /*
1308          * On the A, preloading a segment before HDMAENACK
1309          * comes true can clobber the shaddow address of the
1310          * first segment in the S/G FIFO.  Wait until it is
1311          * safe to proceed.
1312          */
1313         if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) == 0) {
1314                 test    DFCNTRL, HDMAENACK jz return;
1315         }
1316         if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
1317                 bmov    HADDR, CCSGRAM, 8;
1318         } else {
1319                 bmov    HADDR, CCSGRAM, 4;
1320         }
1321         bmov    HCNT, CCSGRAM, 3;
1322         bmov    SCB_RESIDUAL_DATACNT[3], CCSGRAM, 1;
1323         if ((ahd->flags & AHD_39BIT_ADDRESSING) != 0) {
1324                 and     HADDR[4], SG_HIGH_ADDR_BITS, SCB_RESIDUAL_DATACNT[3];
1325         }
1326         if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
1327                 /* Skip 4 bytes of pad. */
1328                 add     CCSGADDR, 4;
1329         }
1330 sg_advance:
1331         clr     A;                      /* add sizeof(struct scatter) */
1332         add     SCB_RESIDUAL_SGPTR[0],SG_SIZEOF;
1333         adc     SCB_RESIDUAL_SGPTR[1],A;
1334         adc     SCB_RESIDUAL_SGPTR[2],A;
1335         adc     SCB_RESIDUAL_SGPTR[3],A;
1336         mov     SINDEX, SCB_RESIDUAL_SGPTR[0];
1337         test    SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz . + 3;
1338         or      SINDEX, LAST_SEG;
1339         clr     SG_STATE;
1340         mov     SG_CACHE_PRE, SINDEX;
1341         if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) != 0) {
1342                 /*
1343                  * Use SCSIENWRDIS so that SCSIEN is never
1344                  * modified by this operation.
1345                  */
1346                 or      DFCNTRL, PRELOADEN|HDMAEN|SCSIENWRDIS;
1347         } else {
1348                 or      DFCNTRL, PRELOADEN|HDMAEN;
1349         }
1350         /*
1351          * Do we have another segment in the cache?
1352          */
1353         add     NONE, SG_PREFETCH_CNT_LIMIT, CCSGADDR;
1354         jnc     return;
1355         and     SG_STATE, ~SEGS_AVAIL ret;
1356
1357 /*
1358  * Initialize the DMA address and counter from the SCB.
1359  */
1360 load_first_seg:
1361         bmov    HADDR, SCB_DATAPTR, 11;
1362         and     REG_ISR, ~SG_FULL_RESID, SCB_SGPTR[0];
1363         test    SCB_DATACNT[3], SG_LAST_SEG jz . + 2;
1364         or      REG_ISR, LAST_SEG;
1365         mov     SG_CACHE_PRE, REG_ISR;
1366         mvi     DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN);
1367         /*
1368          * Since we've are entering a data phase, we will
1369          * rely on the SCB_RESID* fields.  Initialize the
1370          * residual and clear the full residual flag.
1371          */
1372         and     SCB_SGPTR[0], ~SG_FULL_RESID;
1373         bmov    SCB_RESIDUAL_DATACNT[3], SCB_DATACNT[3], 5;
1374         /* If we need more S/G elements, tell the idle loop */
1375         test    SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jnz . + 2;
1376         mvi     SG_STATE, LOADING_NEEDED ret;
1377         clr     SG_STATE ret;
1378
1379 p_data_handle_xfer:
1380         call    setjmp;
1381         test    SG_STATE, LOADING_NEEDED jnz service_fifo;
1382 p_data_clear_handler:
1383         or      LONGJMP_ADDR[1], INVALID_ADDR ret;
1384
1385 p_data:
1386         test    SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT   jz p_data_allowed;
1387         SET_SEQINTCODE(PROTO_VIOLATION)
1388 p_data_allowed:
1389  
1390         test    SEQ_FLAGS, DPHASE       jz data_phase_initialize;
1391
1392         /*
1393          * If we re-enter the data phase after going through another
1394          * phase, our transfer location has almost certainly been
1395          * corrupted by the interveining, non-data, transfers.  Ask
1396          * the host driver to fix us up based on the transfer residual
1397          * unless we already know that we should be bitbucketing.
1398          */
1399         test    SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jnz p_data_bitbucket;
1400         SET_SEQINTCODE(PDATA_REINIT)
1401         jmp     data_phase_inbounds;
1402
1403 p_data_bitbucket:
1404         /*
1405          * Turn on `Bit Bucket' mode, wait until the target takes
1406          * us to another phase, and then notify the host.
1407          */
1408         mov     SAVED_MODE, MODE_PTR;
1409         test    MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
1410                 jnz bitbucket_not_m_dff;
1411         /*
1412          * Ensure that any FIFO contents are cleared out and the
1413          * FIFO free'd prior to starting the BITBUCKET.  BITBUCKET
1414          * doesn't discard data already in the FIFO.
1415          */
1416         mvi     DFFSXFRCTL, RSTCHN|CLRSHCNT;
1417         SET_MODE(M_SCSI, M_SCSI)
1418 bitbucket_not_m_dff:
1419         or      SXFRCTL1,BITBUCKET;
1420         /* Wait for non-data phase. */
1421         test    SCSIPHASE, ~DATA_PHASE_MASK jz .;
1422         and     SXFRCTL1, ~BITBUCKET;
1423         RESTORE_MODE(SAVED_MODE)
1424 SET_SRC_MODE    M_DFF1;
1425 SET_DST_MODE    M_DFF1;
1426         SET_SEQINTCODE(DATA_OVERRUN)
1427         jmp     ITloop;
1428
1429 data_phase_initialize:
1430         test    SCB_SGPTR[0], SG_LIST_NULL jnz p_data_bitbucket;
1431         call    load_first_seg;
1432 data_phase_inbounds:
1433         /* We have seen a data phase at least once. */
1434         or      SEQ_FLAGS, DPHASE;
1435         mov     SAVED_MODE, MODE_PTR;
1436         test    SG_STATE, LOADING_NEEDED jz data_group_dma_loop;
1437         call    p_data_handle_xfer;
1438 data_group_dma_loop:
1439         /*
1440          * The transfer is complete if either the last segment
1441          * completes or the target changes phase.  Both conditions
1442          * will clear SCSIEN.
1443          */
1444         call    idle_loop_service_fifos;
1445         call    idle_loop_cchan;
1446         call    idle_loop_gsfifo;
1447         RESTORE_MODE(SAVED_MODE)
1448         test    DFCNTRL, SCSIEN jnz data_group_dma_loop;
1449
1450 data_group_dmafinish:
1451         /*
1452          * The transfer has terminated either due to a phase
1453          * change, and/or the completion of the last segment.
1454          * We have two goals here.  Do as much other work
1455          * as possible while the data fifo drains on a read
1456          * and respond as quickly as possible to the standard
1457          * messages (save data pointers/disconnect and command
1458          * complete) that usually follow a data phase.
1459          */
1460         call    calc_residual;
1461
1462         /*
1463          * Go ahead and shut down the DMA engine now.
1464          */
1465         test    DFCNTRL, DIRECTION jnz data_phase_finish;
1466 data_group_fifoflush:
1467         if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
1468                 or      DFCNTRL, FIFOFLUSH;
1469         }
1470         /*
1471          * We have enabled the auto-ack feature.  This means
1472          * that the controller may have already transferred
1473          * some overrun bytes into the data FIFO and acked them
1474          * on the bus.  The only way to detect this situation is
1475          * to wait for LAST_SEG_DONE to come true on a completed
1476          * transfer and then test to see if the data FIFO is
1477          * non-empty.  We know there is more data yet to transfer
1478          * if SG_LIST_NULL is not yet set, thus there cannot be
1479          * an overrun.
1480          */
1481         test    SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz data_phase_finish;
1482         test    SG_CACHE_SHADOW, LAST_SEG_DONE jz .;
1483         test    DFSTATUS, FIFOEMP jnz data_phase_finish;
1484         /* Overrun */
1485         jmp     p_data;
1486 data_phase_finish:
1487         /*
1488          * If the target has left us in data phase, loop through
1489          * the dma code again.  We will only loop if there is a
1490          * data overrun.  
1491          */
1492         if ((ahd->flags & AHD_TARGETROLE) != 0) {
1493                 test    SSTAT0, TARGET jnz data_phase_done;
1494         }
1495         if ((ahd->flags & AHD_INITIATORROLE) != 0) {
1496                 test    SSTAT1, REQINIT jz .;
1497                 test    SCSIPHASE, DATA_PHASE_MASK jnz p_data;
1498         }
1499
1500 data_phase_done:
1501         /* Kill off any pending prefetch */
1502         call    disable_ccsgen;
1503         or      LONGJMP_ADDR[1], INVALID_ADDR;
1504
1505         if ((ahd->flags & AHD_TARGETROLE) != 0) {
1506                 test    SEQ_FLAGS, DPHASE_PENDING jz ITloop;
1507                 /*
1508                 and     SEQ_FLAGS, ~DPHASE_PENDING;
1509                  * For data-in phases, wait for any pending acks from the
1510                  * initiator before changing phase.  We only need to
1511                  * send Ignore Wide Residue messages for data-in phases.
1512                 test    DFCNTRL, DIRECTION jz target_ITloop;
1513                 test    SSTAT1, REQINIT jnz .;
1514                 test    SCB_TASK_ATTRIBUTE, SCB_XFERLEN_ODD jz target_ITloop;
1515                 SET_MODE(M_SCSI, M_SCSI)
1516                 test    NEGCONOPTS, WIDEXFER jz target_ITloop;
1517                  */
1518                 /*
1519                  * Issue an Ignore Wide Residue Message.
1520                 mvi     P_MESGIN|BSYO call change_phase;
1521                 mvi     MSG_IGN_WIDE_RESIDUE call target_outb;
1522                 mvi     1 call target_outb;
1523                 jmp     target_ITloop;
1524                  */
1525         } else {
1526                 jmp     ITloop;
1527         }
1528
1529 /*
1530  * We assume that, even though data may still be
1531  * transferring to the host, that the SCSI side of
1532  * the DMA engine is now in a static state.  This
1533  * allows us to update our notion of where we are
1534  * in this transfer.
1535  *
1536  * If, by chance, we stopped before being able
1537  * to fetch additional segments for this transfer,
1538  * yet the last S/G was completely exhausted,
1539  * call our idle loop until it is able to load
1540  * another segment.  This will allow us to immediately
1541  * pickup on the next segment on the next data phase.
1542  *
1543  * If we happened to stop on the last segment, then
1544  * our residual information is still correct from
1545  * the idle loop and there is no need to perform
1546  * any fixups.
1547  */
1548 residual_before_last_seg:
1549         test    MDFFSTAT, SHVALID       jnz sgptr_fixup;
1550         /*
1551          * Can never happen from an interrupt as the packetized
1552          * hardware will only interrupt us once SHVALID or
1553          * LAST_SEG_DONE.
1554          */
1555         call    idle_loop_service_fifos;
1556         RESTORE_MODE(SAVED_MODE)
1557         /* FALLTHROUGH */
1558 calc_residual:
1559         test    SG_CACHE_SHADOW, LAST_SEG jz residual_before_last_seg;
1560         /* Record if we've consumed all S/G entries */
1561         test    MDFFSTAT, SHVALID       jz . + 2;
1562         bmov    SCB_RESIDUAL_DATACNT, SHCNT, 3 ret;
1563         or      SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL ret;
1564
1565 sgptr_fixup:
1566         /*
1567          * Fixup the residual next S/G pointer.  The S/G preload
1568          * feature of the chip allows us to load two elements
1569          * in addition to the currently active element.  We
1570          * store the bottom byte of the next S/G pointer in
1571          * the SG_CACHE_PTR register so we can restore the
1572          * correct value when the DMA completes.  If the next
1573          * sg ptr value has advanced to the point where higher
1574          * bytes in the address have been affected, fix them
1575          * too.
1576          */
1577         test    SG_CACHE_SHADOW, 0x80 jz sgptr_fixup_done;
1578         test    SCB_RESIDUAL_SGPTR[0], 0x80 jnz sgptr_fixup_done;
1579         add     SCB_RESIDUAL_SGPTR[1], -1;
1580         adc     SCB_RESIDUAL_SGPTR[2], -1; 
1581         adc     SCB_RESIDUAL_SGPTR[3], -1;
1582 sgptr_fixup_done:
1583         and     SCB_RESIDUAL_SGPTR[0], SG_ADDR_MASK, SG_CACHE_SHADOW;
1584         clr     SCB_RESIDUAL_DATACNT[3]; /* We are not the last seg */
1585         bmov    SCB_RESIDUAL_DATACNT, SHCNT, 3 ret;
1586
1587 export timer_isr:
1588         call    issue_cmdcmplt;
1589         mvi     CLRSEQINTSTAT, CLRSEQ_SWTMRTO;
1590         if ((ahd->bugs & AHD_SET_MODE_BUG) != 0) {
1591                 /*
1592                  * In H2A4, the mode pointer is not saved
1593                  * for intvec2, but is restored on iret.
1594                  * This can lead to the restoration of a
1595                  * bogus mode ptr.  Manually clear the
1596                  * intmask bits and do a normal return
1597                  * to compensate.
1598                  */
1599                 and     SEQINTCTL, ~(INTMASK2|INTMASK1) ret;
1600         } else {
1601                 or      SEQINTCTL, IRET ret;
1602         }
1603
1604 export seq_isr:
1605         if ((ahd->features & AHD_RTI) == 0) {
1606                 /*
1607                  * On RevA Silicon, if the target returns us to data-out
1608                  * after we have already trained for data-out, it is
1609                  * possible for us to transition the free running clock to
1610                  * data-valid before the required 100ns P1 setup time (8 P1
1611                  * assertions in fast-160 mode).  This will only happen if
1612                  * this L-Q is a continuation of a data transfer for which
1613                  * we have already prefetched data into our FIFO (LQ/Data
1614                  * followed by LQ/Data for the same write transaction).
1615                  * This can cause some target implementations to miss the
1616                  * first few data transfers on the bus.  We detect this
1617                  * situation by noticing that this is the first data transfer
1618                  * after an LQ (LQIWORKONLQ true), that the data transfer is
1619                  * a continuation of a transfer already setup in our FIFO
1620                  * (SAVEPTRS interrupt), and that the transaction is a write
1621                  * (DIRECTION set in DFCNTRL). The delay is performed by
1622                  * disabling SCSIEN until we see the first REQ from the
1623                  * target.
1624                  * 
1625                  * First instruction in an ISR cannot be a branch on
1626                  * Rev A.  Snapshot LQISTAT2 so the status is not missed
1627                  * and deffer the test by one instruction.
1628                  */
1629                 mov     REG_ISR, LQISTAT2;
1630                 test    REG_ISR, LQIWORKONLQ jz main_isr;
1631                 test    SEQINTSRC, SAVEPTRS  jz main_isr;
1632                 test    LONGJMP_ADDR[1], INVALID_ADDR jz saveptr_active_fifo;
1633                 /*
1634                  * Switch to the active FIFO after clearing the snapshot
1635                  * savepointer in the current FIFO.  We do this so that
1636                  * a pending CTXTDONE or SAVEPTR is visible in the active
1637                  * FIFO.  This status is the only way we can detect if we
1638                  * have lost the race (e.g. host paused us) and our attepts
1639                  * to disable the channel occurred after all REQs were
1640                  * already seen and acked (REQINIT never comes true).
1641                  */
1642                 mvi     DFFSXFRCTL, CLRCHN;
1643                 xor     MODE_PTR, MK_MODE(M_DFF1, M_DFF1);
1644                 test    DFCNTRL, DIRECTION jz interrupt_return;
1645                 and     DFCNTRL, ~SCSIEN;
1646 snapshot_wait_data_valid:
1647                 test    SEQINTSRC, (CTXTDONE|SAVEPTRS) jnz snapshot_data_valid;
1648                 test    SSTAT1, REQINIT jz snapshot_wait_data_valid;
1649 snapshot_data_valid:
1650                 or      DFCNTRL, SCSIEN;
1651                 or      SEQINTCTL, IRET ret;
1652 snapshot_saveptr:
1653                 mvi     DFFSXFRCTL, CLRCHN;
1654                 or      SEQINTCTL, IRET ret;
1655 main_isr:
1656         }
1657         test    SEQINTSRC, CFG4DATA     jnz cfg4data_intr;
1658         test    SEQINTSRC, CFG4ISTAT    jnz cfg4istat_intr;
1659         test    SEQINTSRC, SAVEPTRS     jnz saveptr_intr;
1660         test    SEQINTSRC, CFG4ICMD     jnz cfg4icmd_intr;
1661         SET_SEQINTCODE(INVALID_SEQINT)
1662
1663 /*
1664  * There are two types of save pointers interrupts:
1665  * The first is a snapshot save pointers where the current FIFO is not
1666  * active and contains a snapshot of the current poniter information.
1667  * This happens between packets in a stream for a single L_Q.  Since we
1668  * are not performing a pointer save, we can safely clear the channel
1669  * so it can be used for other transactions.  On RTI capable controllers,
1670  * where snapshots can, and are, disabled, the code to handle this type
1671  * of snapshot is not active.
1672  *
1673  * The second case is a save pointers on an active FIFO which occurs
1674  * if the target changes to a new L_Q or busfrees/QASes and the transfer
1675  * has a residual.  This should occur coincident with a ctxtdone.  We
1676  * disable the interrupt and allow our active routine to handle the
1677  * save.
1678  */
1679 saveptr_intr:
1680         if ((ahd->features & AHD_RTI) == 0) {
1681                 test    LONGJMP_ADDR[1], INVALID_ADDR jnz snapshot_saveptr;
1682         }
1683 saveptr_active_fifo:
1684         and     SEQIMODE, ~ENSAVEPTRS;
1685         or      SEQINTCTL, IRET ret;
1686
1687 cfg4data_intr:
1688         test    SCB_SGPTR[0], SG_LIST_NULL jnz pkt_handle_overrun_inc_use_count;
1689         call    load_first_seg;
1690         call    pkt_handle_xfer;
1691         inc     SCB_FIFO_USE_COUNT;
1692 interrupt_return:
1693         or      SEQINTCTL, IRET ret;
1694
1695 cfg4istat_intr:
1696         call    freeze_queue;
1697         add     NONE, -13, SCB_CDB_LEN;
1698         jnc     cfg4istat_have_sense_addr;
1699         test    SCB_CDB_LEN, SCB_CDB_LEN_PTR jnz cfg4istat_have_sense_addr;
1700         /*
1701          * Host sets up address/count and enables transfer.
1702          */
1703         SET_SEQINTCODE(CFG4ISTAT_INTR)
1704         jmp     cfg4istat_setup_handler;
1705 cfg4istat_have_sense_addr:
1706         bmov    HADDR, SCB_SENSE_BUSADDR, 4;
1707         mvi     HCNT[1], (AHD_SENSE_BUFSIZE >> 8);
1708         mvi     SG_CACHE_PRE, LAST_SEG;
1709         mvi     DFCNTRL, PRELOADEN|SCSIEN|HDMAEN;
1710 cfg4istat_setup_handler:
1711         /*
1712          * Status pkt is transferring to host.
1713          * Wait in idle loop for transfer to complete.
1714          * If a command completed before an attempted
1715          * task management function completed, notify the host.
1716          */
1717         test    SCB_TASK_MANAGEMENT, 0xFF jz cfg4istat_no_taskmgmt_func;
1718         SET_SEQINTCODE(TASKMGMT_CMD_CMPLT_OKAY)
1719 cfg4istat_no_taskmgmt_func:
1720         call    pkt_handle_status;
1721         or      SEQINTCTL, IRET ret;
1722
1723 cfg4icmd_intr:
1724         /*
1725          * In the case of DMAing a CDB from the host, the normal
1726          * CDB buffer is formatted with an 8 byte address followed
1727          * by a 1 byte count.
1728          */
1729         bmov    HADDR[0], SCB_HOST_CDB_PTR, 9;
1730         mvi     SG_CACHE_PRE, LAST_SEG;
1731         mvi     DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN);
1732         call    pkt_handle_cdb;
1733         or      SEQINTCTL, IRET ret;
1734
1735 /*
1736  * See if the target has gone on in this context creating an
1737  * overrun condition.  For the write case, the hardware cannot
1738  * ack bytes until data are provided.  So, if the target begins
1739  * another  packet without changing contexts, implying we are
1740  * not sitting on a packet boundary, we are in an overrun
1741  * situation.  For the read case, the hardware will continue to
1742  * ack bytes into the FIFO, and may even ack the last overrun packet
1743  * into the FIFO.   If the FIFO should become non-empty, we are in
1744  * a read overrun case.
1745  */
1746 #define check_overrun                                                   \
1747         /* Not on a packet boundary. */                                 \
1748         test    MDFFSTAT, DLZERO jz pkt_handle_overrun;                 \
1749         test    DFSTATUS, FIFOEMP jz pkt_handle_overrun
1750
1751 pkt_handle_xfer:
1752         test    SG_STATE, LOADING_NEEDED jz pkt_last_seg;
1753         call    setjmp;
1754         test    SEQINTSRC, SAVEPTRS jnz pkt_saveptrs;
1755         test    SCSIPHASE, ~DATA_PHASE_MASK jz . + 2;
1756         test    SCSISIGO, ATNO jnz . + 2;
1757         test    SSTAT2, NONPACKREQ jz pkt_service_fifo;
1758         /*
1759          * Defer handling of this NONPACKREQ until we
1760          * can be sure it pertains to this FIFO.  SAVEPTRS
1761          * will not be asserted if the NONPACKREQ is for us,
1762          * so we must simulate it if shaddow is valid.  If
1763          * shaddow is not valid, keep running this FIFO until we
1764          * have satisfied the transfer by loading segments and
1765          * waiting for either shaddow valid or last_seg_done.
1766          */
1767         test    MDFFSTAT, SHVALID jnz pkt_saveptrs;
1768 pkt_service_fifo:
1769         test    SG_STATE, LOADING_NEEDED jnz service_fifo;
1770 pkt_last_seg:
1771         call    setjmp;
1772         test    SEQINTSRC, SAVEPTRS jnz pkt_saveptrs;
1773         test    SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_last_seg_done;
1774         test    SCSIPHASE, ~DATA_PHASE_MASK jz . + 2;
1775         test    SCSISIGO, ATNO jnz . + 2;
1776         test    SSTAT2, NONPACKREQ jz return;
1777         test    MDFFSTAT, SHVALID jz return;
1778         /* FALLTHROUGH */
1779
1780 /*
1781  * Either a SAVEPTRS interrupt condition is pending for this FIFO
1782  * or we have a pending NONPACKREQ for this FIFO.  We differentiate
1783  * between the two by capturing the state of the SAVEPTRS interrupt
1784  * prior to clearing this status and executing the common code for
1785  * these two cases.
1786  */
1787 pkt_saveptrs:
1788 BEGIN_CRITICAL;
1789         if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
1790                 or      DFCNTRL, FIFOFLUSH;
1791         }
1792         mov     REG0, SEQINTSRC;
1793         call    calc_residual;
1794         call    save_pointers;
1795         mvi     CLRSEQINTSRC, CLRSAVEPTRS;
1796         call    disable_ccsgen;
1797         or      SEQIMODE, ENSAVEPTRS;
1798         test    DFCNTRL, DIRECTION jnz pkt_saveptrs_check_status;
1799         test    DFSTATUS, FIFOEMP jnz pkt_saveptrs_check_status;
1800         /*
1801          * Keep a handler around for this FIFO until it drains
1802          * to the host to guarantee that we don't complete the
1803          * command to the host before the data arrives.
1804          */
1805 pkt_saveptrs_wait_fifoemp:
1806         call    setjmp;
1807         test    DFSTATUS, FIFOEMP jz return;
1808 pkt_saveptrs_check_status:
1809         or      LONGJMP_ADDR[1], INVALID_ADDR;
1810         test    REG0, SAVEPTRS jz unexpected_nonpkt_phase;
1811         dec     SCB_FIFO_USE_COUNT;
1812         test    SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
1813         mvi     DFFSXFRCTL, CLRCHN ret;
1814 END_CRITICAL;
1815
1816 /*
1817  * LAST_SEG_DONE status has been seen in the current FIFO.
1818  * This indicates that all of the allowed data for this
1819  * command has transferred across the SCSI and host buses.
1820  * Check for overrun and see if we can complete this command.
1821  */
1822 pkt_last_seg_done:
1823 BEGIN_CRITICAL;
1824         /*
1825          * Mark transfer as completed.
1826          */
1827         or      SCB_SGPTR, SG_LIST_NULL;
1828
1829         /*
1830          * Wait for the current context to finish to verify that
1831          * no overrun condition has occurred.
1832          */
1833         test    SEQINTSRC, CTXTDONE jnz pkt_ctxt_done;
1834         call    setjmp;
1835 pkt_wait_ctxt_done_loop:
1836         test    SEQINTSRC, CTXTDONE jnz pkt_ctxt_done;
1837         /*
1838          * A sufficiently large overrun or a NONPACKREQ may
1839          * prevent CTXTDONE from ever asserting, so we must
1840          * poll for these statuses too.
1841          */
1842         check_overrun;
1843         test    SSTAT2, NONPACKREQ jz return;
1844         test    SEQINTSRC, CTXTDONE jz unexpected_nonpkt_phase;
1845         /* FALLTHROUGH */
1846
1847 pkt_ctxt_done:
1848         check_overrun;
1849         or      LONGJMP_ADDR[1], INVALID_ADDR;
1850         /*
1851          * If status has been received, it is safe to skip
1852          * the check to see if another FIFO is active because
1853          * LAST_SEG_DONE has been observed.  However, we check
1854          * the FIFO anyway since it costs us only one extra
1855          * instruction to leverage common code to perform the
1856          * SCB completion.
1857          */
1858         dec     SCB_FIFO_USE_COUNT;
1859         test    SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
1860         mvi     DFFSXFRCTL, CLRCHN ret;
1861 END_CRITICAL;
1862
1863 /*
1864  * Must wait until CDB xfer is over before issuing the
1865  * clear channel.
1866  */
1867 pkt_handle_cdb:
1868         call    setjmp;
1869         test    SG_CACHE_SHADOW, LAST_SEG_DONE jz return;
1870         or      LONGJMP_ADDR[1], INVALID_ADDR;
1871         mvi     DFFSXFRCTL, CLRCHN ret;
1872
1873 /*
1874  * Watch over the status transfer.  Our host sense buffer is
1875  * large enough to take the maximum allowed status packet.
1876  * None-the-less, we must still catch and report overruns to
1877  * the host.  Additionally, properly catch unexpected non-packet
1878  * phases that are typically caused by CRC errors in status packet
1879  * transmission.
1880  */
1881 pkt_handle_status:
1882         call    setjmp;
1883         test    SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_status_check_overrun;
1884         test    SEQINTSRC, CTXTDONE jz pkt_status_check_nonpackreq;
1885         test    SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_status_check_overrun;
1886 pkt_status_IU_done:
1887         if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
1888                 or      DFCNTRL, FIFOFLUSH;
1889         }
1890         test    DFSTATUS, FIFOEMP jz return;
1891 BEGIN_CRITICAL;
1892         or      LONGJMP_ADDR[1], INVALID_ADDR;
1893         mvi     SCB_SCSI_STATUS, STATUS_PKT_SENSE;
1894         or      SCB_CONTROL, STATUS_RCVD;
1895         jmp     pkt_complete_scb_if_fifos_idle;
1896 END_CRITICAL;
1897 pkt_status_check_overrun:
1898         /*
1899          * Status PKT overruns are uncerimoniously recovered with a
1900          * bus reset.  If we've overrun, let the host know so that
1901          * recovery can be performed.
1902          *
1903          * LAST_SEG_DONE has been observed.  If either CTXTDONE or
1904          * a NONPACKREQ phase change have occurred and the FIFO is
1905          * empty, there is no overrun.
1906          */
1907         test    DFSTATUS, FIFOEMP jz pkt_status_report_overrun;
1908         test    SEQINTSRC, CTXTDONE jz . + 2;
1909         test    DFSTATUS, FIFOEMP jnz pkt_status_IU_done;
1910         test    SCSIPHASE, ~DATA_PHASE_MASK jz return;
1911         test    DFSTATUS, FIFOEMP jnz pkt_status_check_nonpackreq;
1912 pkt_status_report_overrun:
1913         SET_SEQINTCODE(STATUS_OVERRUN)
1914         /* SEQUENCER RESTARTED */
1915 pkt_status_check_nonpackreq:
1916         /*
1917          * CTXTDONE may be held off if a NONPACKREQ is associated with
1918          * the current context.  If a NONPACKREQ is observed, decide
1919          * if it is for the current context.  If it is for the current
1920          * context, we must defer NONPACKREQ processing until all data
1921          * has transferred to the host.
1922          */
1923         test    SCSIPHASE, ~DATA_PHASE_MASK jz return;
1924         test    SCSISIGO, ATNO jnz . + 2;
1925         test    SSTAT2, NONPACKREQ jz return;
1926         test    SEQINTSRC, CTXTDONE jnz pkt_status_IU_done;
1927         test    DFSTATUS, FIFOEMP jz return;
1928         /*
1929          * The unexpected nonpkt phase handler assumes that any
1930          * data channel use will have a FIFO reference count.  It
1931          * turns out that the status handler doesn't need a refernce
1932          * count since the status received flag, and thus completion
1933          * processing, cannot be set until the handler is finished.
1934          * We increment the count here to make the nonpkt handler
1935          * happy.
1936          */
1937         inc     SCB_FIFO_USE_COUNT;
1938         /* FALLTHROUGH */
1939
1940 /*
1941  * Nonpackreq is a polled status.  It can come true in three situations:
1942  * we have received an L_Q, we have sent one or more L_Qs, or there is no
1943  * L_Q context associated with this REQ (REQ occurs immediately after a
1944  * (re)selection).  Routines that know that the context responsible for this
1945  * nonpackreq call directly into unexpected_nonpkt_phase.  In the case of the
1946  * top level idle loop, we exhaust all active contexts prior to determining that
1947  * we simply do not have the full I_T_L_Q for this phase.
1948  */
1949 unexpected_nonpkt_phase_find_ctxt:
1950         /*
1951          * This nonpackreq is most likely associated with one of the tags
1952          * in a FIFO or an outgoing LQ.  Only treat it as an I_T only
1953          * nonpackreq if we've cleared out the FIFOs and handled any
1954          * pending SELDO.
1955          */
1956 SET_SRC_MODE    M_SCSI;
1957 SET_DST_MODE    M_SCSI;
1958         and     A, FIFO1FREE|FIFO0FREE, DFFSTAT;
1959         cmp     A, FIFO1FREE|FIFO0FREE jne return;
1960         test    SSTAT0, SELDO jnz return;
1961         mvi     SCBPTR[1], SCB_LIST_NULL;
1962 unexpected_nonpkt_phase:
1963         test    MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1)) jnz . + 3;
1964 SET_SRC_MODE    M_DFF0;
1965 SET_DST_MODE    M_DFF0;
1966         or      LONGJMP_ADDR[1], INVALID_ADDR;
1967         dec     SCB_FIFO_USE_COUNT;
1968         mvi     DFFSXFRCTL, CLRCHN;
1969         mvi     CLRSINT2, CLRNONPACKREQ;
1970         test    SCSIPHASE, ~(MSG_IN_PHASE|MSG_OUT_PHASE) jnz illegal_phase;
1971         SET_SEQINTCODE(ENTERING_NONPACK)
1972         jmp     ITloop;
1973
1974 illegal_phase:
1975         SET_SEQINTCODE(ILLEGAL_PHASE)
1976         jmp     ITloop;
1977
1978 /*
1979  * We have entered an overrun situation.  If we have working
1980  * BITBUCKET, flip that on and let the hardware eat any overrun
1981  * data.  Otherwise use an overrun buffer in the host to simulate
1982  * BITBUCKET.
1983  */
1984 pkt_handle_overrun_inc_use_count:
1985         inc     SCB_FIFO_USE_COUNT;
1986 pkt_handle_overrun:
1987         SET_SEQINTCODE(CFG4OVERRUN)
1988         call    freeze_queue;
1989         if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) == 0) {
1990                 or      DFFSXFRCTL, DFFBITBUCKET;
1991 SET_SRC_MODE    M_DFF1;
1992 SET_DST_MODE    M_DFF1;
1993         } else {
1994                 call    load_overrun_buf;
1995                 mvi     DFCNTRL, (HDMAEN|SCSIEN|PRELOADEN);
1996         }
1997         call    setjmp;
1998         if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
1999                 test    DFSTATUS, PRELOAD_AVAIL jz overrun_load_done;
2000                 call    load_overrun_buf;
2001                 or      DFCNTRL, PRELOADEN;
2002 overrun_load_done:
2003                 test    SEQINTSRC, CTXTDONE jnz pkt_overrun_end;
2004         } else {
2005                 test    DFFSXFRCTL, DFFBITBUCKET jz pkt_overrun_end;
2006         }
2007         test    SSTAT2, NONPACKREQ jz return;
2008 pkt_overrun_end:
2009         or      SCB_RESIDUAL_SGPTR, SG_OVERRUN_RESID;
2010         test    SEQINTSRC, CTXTDONE jz unexpected_nonpkt_phase;
2011         dec     SCB_FIFO_USE_COUNT;
2012         or      LONGJMP_ADDR[1], INVALID_ADDR;
2013         test    SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
2014         mvi     DFFSXFRCTL, CLRCHN ret;
2015
2016 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
2017 load_overrun_buf:
2018         /*
2019          * Load a dummy segment if preload space is available.
2020          */
2021         mov     HADDR[0], SHARED_DATA_ADDR;
2022         add     HADDR[1], PKT_OVERRUN_BUFOFFSET, SHARED_DATA_ADDR[1];
2023         mov     ACCUM_SAVE, A;
2024         clr     A;
2025         adc     HADDR[2], A, SHARED_DATA_ADDR[2];
2026         adc     HADDR[3], A, SHARED_DATA_ADDR[3];
2027         mov     A, ACCUM_SAVE;
2028         bmov    HADDR[4], ALLZEROS, 4;
2029         /* PKT_OVERRUN_BUFSIZE is a multiple of 256 */
2030         clr     HCNT[0];
2031         mvi     HCNT[1], ((PKT_OVERRUN_BUFSIZE >> 8) & 0xFF);
2032         clr     HCNT[2] ret;
2033 }