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