aic79xx.seq revision 1.6 1 /* $NetBSD: aic79xx.seq,v 1.6 2003/08/29 01:28:53 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.11 2003/05/26 21:26:52 gibbs Exp $
43 */
44
45 VERSION = "Id: //depot/aic7xxx/aic7xxx/aic79xx.seq#94 $"
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 SCB_TASK_ATTRIBUTE, SCB_XFERLEN_ODD jnz mesgin_done;
878 SET_SEQINTCODE(IGN_WIDE_RES)
879 jmp mesgin_done;
880
881 mesgin_proto_violation:
882 SET_SEQINTCODE(PROTO_VIOLATION)
883 jmp mesgin_done;
884 mesgin_reject:
885 mvi MSG_MESSAGE_REJECT call mk_mesg;
886 mesgin_done:
887 mov NONE,SCSIDAT; /*dummy read from latch to ACK*/
888 jmp ITloop;
889
890 #define INDEX_DISC_LIST(scsiid, lun) \
891 and A, 0xC0, scsiid; \
892 or SCBPTR, A, lun; \
893 clr SCBPTR[1]; \
894 and SINDEX, 0x30, scsiid; \
895 shr SINDEX, 3; /* Multiply by 2 */ \
896 add SINDEX, (SCB_DISCONNECTED_LISTS & 0xFF); \
897 mvi SINDEX[1], ((SCB_DISCONNECTED_LISTS >> 8) & 0xFF)
898
899 mesgin_identify:
900 /*
901 * Determine whether a target is using tagged or non-tagged
902 * transactions by first looking at the transaction stored in
903 * the per-device, disconnected array. If there is no untagged
904 * transaction for this target, this must be a tagged transaction.
905 */
906 and SAVED_LUN, MSG_IDENTIFY_LUNMASK, A;
907 INDEX_DISC_LIST(SAVED_SCSIID, SAVED_LUN);
908 bmov DINDEX, SINDEX, 2;
909 bmov REG0, SINDIR, 2;
910 cmp REG0[1], SCB_LIST_NULL je snoop_tag;
911 /* Untagged. Clear the busy table entry and setup the SCB. */
912 bmov DINDIR, ALLONES, 2;
913 bmov SCBPTR, REG0, 2;
914 jmp setup_SCB;
915
916 /*
917 * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
918 * If we get one, we use the tag returned to find the proper
919 * SCB. After receiving the tag, look for the SCB at SCB locations tag and
920 * tag + 256.
921 */
922 snoop_tag:
923 if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
924 or SEQ_FLAGS, 0x80;
925 }
926 mov NONE, SCSIDAT; /* ACK Identify MSG */
927 call phase_lock;
928 if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
929 or SEQ_FLAGS, 0x1;
930 }
931 cmp LASTPHASE, P_MESGIN jne not_found_ITloop;
932 if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
933 or SEQ_FLAGS, 0x2;
934 }
935 cmp SCSIBUS, MSG_SIMPLE_Q_TAG jne not_found;
936 get_tag:
937 clr SCBPTR[1];
938 mvi SCBPTR call inb_next; /* tag value */
939 verify_scb:
940 test SCB_CONTROL,DISCONNECTED jz verify_other_scb;
941 mov A, SAVED_SCSIID;
942 cmp SCB_SCSIID, A jne verify_other_scb;
943 mov A, SAVED_LUN;
944 cmp SCB_LUN, A je setup_SCB_disconnected;
945 verify_other_scb:
946 xor SCBPTR[1], 1;
947 test SCBPTR[1], 0xFF jnz verify_scb;
948 jmp not_found;
949
950 /*
951 * Ensure that the SCB the tag points to is for
952 * an SCB transaction to the reconnecting target.
953 */
954 setup_SCB:
955 if ((ahd->flags & AHD_SEQUENCER_DEBUG) != 0) {
956 or SEQ_FLAGS, 0x10;
957 }
958 test SCB_CONTROL,DISCONNECTED jz not_found;
959 setup_SCB_disconnected:
960 and SCB_CONTROL,~DISCONNECTED;
961 clr SEQ_FLAGS; /* make note of IDENTIFY */
962 test SCB_SGPTR, SG_LIST_NULL jnz . + 3;
963 bmov ALLOCFIFO_SCBPTR, SCBPTR, 2;
964 call allocate_fifo;
965 /* See if the host wants to send a message upon reconnection */
966 test SCB_CONTROL, MK_MESSAGE jz mesgin_done;
967 mvi HOST_MSG call mk_mesg;
968 jmp mesgin_done;
969
970 not_found:
971 SET_SEQINTCODE(NO_MATCH)
972 jmp mesgin_done;
973
974 not_found_ITloop:
975 SET_SEQINTCODE(NO_MATCH)
976 jmp ITloop;
977
978 /*
979 * We received a "command complete" message. Put the SCB on the complete
980 * queue and trigger a completion interrupt via the idle loop. Before doing
981 * so, check to see if there
982 * is a residual or the status byte is something other than STATUS_GOOD (0).
983 * In either of these conditions, we upload the SCB back to the host so it can
984 * process this information. In the case of a non zero status byte, we
985 * additionally interrupt the kernel driver synchronously, allowing it to
986 * decide if sense should be retrieved. If the kernel driver wishes to request
987 * sense, it will fill the kernel SCB with a request sense command, requeue
988 * it to the QINFIFO and tell us not to post to the QOUTFIFO by setting
989 * RETURN_1 to SEND_SENSE.
990 */
991 mesgin_complete:
992
993 /*
994 * If ATN is raised, we still want to give the target a message.
995 * Perhaps there was a parity error on this last message byte.
996 * Either way, the target should take us to message out phase
997 * and then attempt to complete the command again. We should use a
998 * critical section here to guard against a timeout triggering
999 * for this command and setting ATN while we are still processing
1000 * the completion.
1001 test SCSISIGI, ATNI jnz mesgin_done;
1002 */
1003
1004 /*
1005 * If we are identified and have successfully sent the CDB,
1006 * any status will do. Optimize this fast path.
1007 */
1008 test SCB_CONTROL, STATUS_RCVD jz mesgin_proto_violation;
1009 test SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT jz complete_accepted;
1010
1011 /*
1012 * If the target never sent an identify message but instead went
1013 * to mesgin to give an invalid message, let the host abort us.
1014 */
1015 test SEQ_FLAGS, NOT_IDENTIFIED jnz mesgin_proto_violation;
1016
1017 /*
1018 * If we recevied good status but never successfully sent the
1019 * cdb, abort the command.
1020 */
1021 test SCB_SCSI_STATUS,0xff jnz complete_accepted;
1022 test SEQ_FLAGS, NO_CDB_SENT jnz mesgin_proto_violation;
1023 complete_accepted:
1024
1025 /*
1026 * See if we attempted to deliver a message but the target ingnored us.
1027 */
1028 test SCB_CONTROL, MK_MESSAGE jz complete_nomsg;
1029 SET_SEQINTCODE(MKMSG_FAILED)
1030 complete_nomsg:
1031 call queue_scb_completion;
1032 jmp await_busfree;
1033
1034 freeze_queue:
1035 /* Cancel any pending select-out. */
1036 test SSTAT0, SELDO|SELINGO jnz . + 2;
1037 and SCSISEQ0, ~ENSELO;
1038 mov ACCUM_SAVE, A;
1039 clr A;
1040 add QFREEZE_COUNT, 1;
1041 adc QFREEZE_COUNT[1], A;
1042 or SEQ_FLAGS2, SELECTOUT_QFROZEN;
1043 mov A, ACCUM_SAVE ret;
1044
1045 /*
1046 * Complete the current FIFO's SCB if data for this same
1047 * SCB is not transferring in the other FIFO.
1048 */
1049 SET_SRC_MODE M_DFF1;
1050 SET_DST_MODE M_DFF1;
1051 pkt_complete_scb_if_fifos_idle:
1052 bmov ARG_1, SCBPTR, 2;
1053 mvi DFFSXFRCTL, CLRCHN;
1054 SET_MODE(M_SCSI, M_SCSI)
1055 bmov SCBPTR, ARG_1, 2;
1056 test SCB_FIFO_USE_COUNT, 0xFF jnz return;
1057 queue_scb_completion:
1058 test SCB_SCSI_STATUS,0xff jnz bad_status;
1059 /*
1060 * Check for residuals
1061 */
1062 test SCB_SGPTR, SG_LIST_NULL jnz complete; /* No xfer */
1063 test SCB_SGPTR, SG_FULL_RESID jnz upload_scb;/* Never xfered */
1064 test SCB_RESIDUAL_SGPTR, SG_LIST_NULL jz upload_scb;
1065 complete:
1066 bmov SCB_NEXT_COMPLETE, COMPLETE_SCB_HEAD, 2;
1067 bmov COMPLETE_SCB_HEAD, SCBPTR, 2 ret;
1068 bad_status:
1069 cmp SCB_SCSI_STATUS, STATUS_PKT_SENSE je upload_scb;
1070 call freeze_queue;
1071 upload_scb:
1072 /*
1073 * Restore SCB TAG since we reuse this field
1074 * in the sequencer. We don't want to corrupt
1075 * it on the host.
1076 */
1077 bmov SCB_TAG, SCBPTR, 2;
1078 bmov SCB_NEXT_COMPLETE, COMPLETE_DMA_SCB_HEAD, 2;
1079 bmov COMPLETE_DMA_SCB_HEAD, SCBPTR, 2;
1080 or SCB_SGPTR, SG_STATUS_VALID ret;
1081
1082 /*
1083 * Is it a disconnect message? Set a flag in the SCB to remind us
1084 * and await the bus going free. If this is an untagged transaction
1085 * store the SCB id for it in our untagged target table for lookup on
1086 * a reselction.
1087 */
1088 mesgin_disconnect:
1089 /*
1090 * If ATN is raised, we still want to give the target a message.
1091 * Perhaps there was a parity error on this last message byte
1092 * or we want to abort this command. Either way, the target
1093 * should take us to message out phase and then attempt to
1094 * disconnect again.
1095 * XXX - Wait for more testing.
1096 test SCSISIGI, ATNI jnz mesgin_done;
1097 */
1098 test SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT
1099 jnz mesgin_proto_violation;
1100 or SCB_CONTROL,DISCONNECTED;
1101 test SCB_CONTROL, TAG_ENB jnz await_busfree;
1102 queue_disc_scb:
1103 bmov REG0, SCBPTR, 2;
1104 INDEX_DISC_LIST(SAVED_SCSIID, SAVED_LUN);
1105 bmov DINDEX, SINDEX, 2;
1106 bmov DINDIR, REG0, 2;
1107 bmov SCBPTR, REG0, 2;
1108 /* FALLTHROUGH */
1109 await_busfree:
1110 and SIMODE1, ~ENBUSFREE;
1111 if ((ahd->bugs & AHD_BUSFREEREV_BUG) == 0) {
1112 /*
1113 * In the BUSFREEREV_BUG case, the
1114 * busfree status was cleared at the
1115 * beginning of the connection.
1116 */
1117 mvi CLRSINT1,CLRBUSFREE;
1118 }
1119 mov NONE, SCSIDAT; /* Ack the last byte */
1120 test MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
1121 jnz await_busfree_not_m_dff;
1122 SET_SRC_MODE M_DFF1;
1123 SET_DST_MODE M_DFF1;
1124 await_busfree_clrchn:
1125 mvi DFFSXFRCTL, CLRCHN;
1126 await_busfree_not_m_dff:
1127 call clear_target_state;
1128 test SSTAT1,REQINIT|BUSFREE jz .;
1129 test SSTAT1, BUSFREE jnz idle_loop;
1130 SET_SEQINTCODE(MISSED_BUSFREE)
1131
1132
1133 /*
1134 * Save data pointers message:
1135 * Copying RAM values back to SCB, for Save Data Pointers message, but
1136 * only if we've actually been into a data phase to change them. This
1137 * protects against bogus data in scratch ram and the residual counts
1138 * since they are only initialized when we go into data_in or data_out.
1139 * Ack the message as soon as possible.
1140 */
1141 SET_SRC_MODE M_DFF1;
1142 SET_DST_MODE M_DFF1;
1143 mesgin_sdptrs:
1144 mov NONE,SCSIDAT; /*dummy read from latch to ACK*/
1145 test SEQ_FLAGS, DPHASE jz ITloop;
1146 call save_pointers;
1147 jmp ITloop;
1148
1149 save_pointers:
1150 /*
1151 * If we are asked to save our position at the end of the
1152 * transfer, just mark us at the end rather than perform a
1153 * full save.
1154 */
1155 test SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz save_pointers_full;
1156 or SCB_SGPTR, SG_LIST_NULL ret;
1157
1158 save_pointers_full:
1159 /*
1160 * The SCB_DATAPTR becomes the current SHADDR.
1161 * All other information comes directly from our residual
1162 * state.
1163 */
1164 bmov SCB_DATAPTR, SHADDR, 8;
1165 bmov SCB_DATACNT, SCB_RESIDUAL_DATACNT, 8 ret;
1166
1167 /*
1168 * Restore pointers message? Data pointers are recopied from the
1169 * SCB anytime we enter a data phase for the first time, so all
1170 * we need to do is clear the DPHASE flag and let the data phase
1171 * code do the rest. We also reset/reallocate the FIFO to make
1172 * sure we have a clean start for the next data or command phase.
1173 */
1174 mesgin_rdptrs:
1175 and SEQ_FLAGS, ~DPHASE;
1176 test MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1)) jnz msgin_rdptrs_get_fifo;
1177 mvi DFFSXFRCTL, RSTCHN|CLRSHCNT;
1178 SET_MODE(M_SCSI, M_SCSI)
1179 msgin_rdptrs_get_fifo:
1180 call allocate_fifo;
1181 jmp mesgin_done;
1182
1183 clear_target_state:
1184 mvi LASTPHASE, P_BUSFREE;
1185 /* clear target specific flags */
1186 mvi SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT ret;
1187
1188 phase_lock:
1189 if ((ahd->bugs & AHD_EARLY_REQ_BUG) != 0) {
1190 /*
1191 * Don't ignore persistent REQ assertions just because
1192 * they were asserted within the bus settle delay window.
1193 * This allows us to tolerate devices like the GEM318
1194 * that violate the SCSI spec. We are careful not to
1195 * count REQ while we are waiting for it to fall during
1196 * an async phase due to our asserted ACK. Each
1197 * sequencer instruction takes ~25ns, so the REQ must
1198 * last at least 100ns in order to be counted as a true
1199 * REQ.
1200 */
1201 test SCSIPHASE, 0xFF jnz phase_locked;
1202 test SCSISIGI, ACKI jnz phase_lock;
1203 test SCSISIGI, REQI jz phase_lock;
1204 test SCSIPHASE, 0xFF jnz phase_locked;
1205 test SCSISIGI, ACKI jnz phase_lock;
1206 test SCSISIGI, REQI jz phase_lock;
1207 phase_locked:
1208 } else {
1209 test SCSIPHASE, 0xFF jz .;
1210 }
1211 test SSTAT1, SCSIPERR jnz phase_lock;
1212 phase_lock_latch_phase:
1213 and LASTPHASE, PHASE_MASK, SCSISIGI ret;
1214
1215 /*
1216 * Functions to read data in Automatic PIO mode.
1217 *
1218 * An ACK is not sent on input from the target until SCSIDATL is read from.
1219 * So we wait until SCSIDATL is latched (the usual way), then read the data
1220 * byte directly off the bus using SCSIBUSL. When we have pulled the ATN
1221 * line, or we just want to acknowledge the byte, then we do a dummy read
1222 * from SCISDATL. The SCSI spec guarantees that the target will hold the
1223 * data byte on the bus until we send our ACK.
1224 *
1225 * The assumption here is that these are called in a particular sequence,
1226 * and that REQ is already set when inb_first is called. inb_{first,next}
1227 * use the same calling convention as inb.
1228 */
1229 inb_next:
1230 mov NONE,SCSIDAT; /*dummy read from latch to ACK*/
1231 inb_next_wait:
1232 /*
1233 * If there is a parity error, wait for the kernel to
1234 * see the interrupt and prepare our message response
1235 * before continuing.
1236 */
1237 test SCSIPHASE, 0xFF jz .;
1238 test SSTAT1, SCSIPERR jnz inb_next_wait;
1239 inb_next_check_phase:
1240 and LASTPHASE, PHASE_MASK, SCSISIGI;
1241 cmp LASTPHASE, P_MESGIN jne mesgin_phasemis;
1242 inb_first:
1243 clr DINDEX[1];
1244 mov DINDEX,SINDEX;
1245 mov DINDIR,SCSIBUS ret; /*read byte directly from bus*/
1246 inb_last:
1247 mov NONE,SCSIDAT ret; /*dummy read from latch to ACK*/
1248
1249 mk_mesg:
1250 mvi SCSISIGO, ATNO;
1251 mov MSG_OUT,SINDEX ret;
1252
1253 SET_SRC_MODE M_DFF1;
1254 SET_DST_MODE M_DFF1;
1255 disable_ccsgen:
1256 test SG_STATE, FETCH_INPROG jz disable_ccsgen_fetch_done;
1257 clr CCSGCTL;
1258 disable_ccsgen_fetch_done:
1259 clr SG_STATE ret;
1260
1261 service_fifo:
1262 /*
1263 * Do we have any prefetch left???
1264 */
1265 test SG_STATE, SEGS_AVAIL jnz idle_sg_avail;
1266
1267 /*
1268 * Can this FIFO have access to the S/G cache yet?
1269 */
1270 test CCSGCTL, SG_CACHE_AVAIL jz return;
1271
1272 /* Did we just finish fetching segs? */
1273 test CCSGCTL, CCSGDONE jnz idle_sgfetch_complete;
1274
1275 /* Are we actively fetching segments? */
1276 test CCSGCTL, CCSGENACK jnz return;
1277
1278 /*
1279 * We fetch a "cacheline aligned" and sized amount of data
1280 * so we don't end up referencing a non-existant page.
1281 * Cacheline aligned is in quotes because the kernel will
1282 * set the prefetch amount to a reasonable level if the
1283 * cacheline size is unknown.
1284 */
1285 bmov SGHADDR, SCB_RESIDUAL_SGPTR, 4;
1286 mvi SGHCNT, SG_PREFETCH_CNT;
1287 if ((ahd->bugs & AHD_REG_SLOW_SETTLE_BUG) != 0) {
1288 /*
1289 * Need two instruction between "touches" of SGHADDR.
1290 */
1291 nop;
1292 }
1293 and SGHADDR[0], SG_PREFETCH_ALIGN_MASK, SCB_RESIDUAL_SGPTR;
1294 mvi CCSGCTL, CCSGEN|CCSGRESET;
1295 or SG_STATE, FETCH_INPROG ret;
1296 idle_sgfetch_complete:
1297 /*
1298 * Guard against SG_CACHE_AVAIL activating during sg fetch
1299 * request in the other FIFO.
1300 */
1301 test SG_STATE, FETCH_INPROG jz return;
1302 clr CCSGCTL;
1303 and CCSGADDR, SG_PREFETCH_ADDR_MASK, SCB_RESIDUAL_SGPTR;
1304 mvi SG_STATE, SEGS_AVAIL|LOADING_NEEDED;
1305 idle_sg_avail:
1306 /* Does the hardware have space for another SG entry? */
1307 test DFSTATUS, PRELOAD_AVAIL jz return;
1308 /*
1309 * On the A, preloading a segment before HDMAENACK
1310 * comes true can clobber the shaddow address of the
1311 * first segment in the S/G FIFO. Wait until it is
1312 * safe to proceed.
1313 */
1314 if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) == 0) {
1315 test DFCNTRL, HDMAENACK jz return;
1316 }
1317 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
1318 bmov HADDR, CCSGRAM, 8;
1319 } else {
1320 bmov HADDR, CCSGRAM, 4;
1321 }
1322 bmov HCNT, CCSGRAM, 3;
1323 bmov SCB_RESIDUAL_DATACNT[3], CCSGRAM, 1;
1324 if ((ahd->flags & AHD_39BIT_ADDRESSING) != 0) {
1325 and HADDR[4], SG_HIGH_ADDR_BITS, SCB_RESIDUAL_DATACNT[3];
1326 }
1327 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
1328 /* Skip 4 bytes of pad. */
1329 add CCSGADDR, 4;
1330 }
1331 sg_advance:
1332 clr A; /* add sizeof(struct scatter) */
1333 add SCB_RESIDUAL_SGPTR[0],SG_SIZEOF;
1334 adc SCB_RESIDUAL_SGPTR[1],A;
1335 adc SCB_RESIDUAL_SGPTR[2],A;
1336 adc SCB_RESIDUAL_SGPTR[3],A;
1337 mov SINDEX, SCB_RESIDUAL_SGPTR[0];
1338 test SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz . + 3;
1339 or SINDEX, LAST_SEG;
1340 clr SG_STATE;
1341 mov SG_CACHE_PRE, SINDEX;
1342 if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) != 0) {
1343 /*
1344 * Use SCSIENWRDIS so that SCSIEN is never
1345 * modified by this operation.
1346 */
1347 or DFCNTRL, PRELOADEN|HDMAEN|SCSIENWRDIS;
1348 } else {
1349 or DFCNTRL, PRELOADEN|HDMAEN;
1350 }
1351 /*
1352 * Do we have another segment in the cache?
1353 */
1354 add NONE, SG_PREFETCH_CNT_LIMIT, CCSGADDR;
1355 jnc return;
1356 and SG_STATE, ~SEGS_AVAIL ret;
1357
1358 /*
1359 * Initialize the DMA address and counter from the SCB.
1360 */
1361 load_first_seg:
1362 bmov HADDR, SCB_DATAPTR, 11;
1363 and REG_ISR, ~SG_FULL_RESID, SCB_SGPTR[0];
1364 test SCB_DATACNT[3], SG_LAST_SEG jz . + 2;
1365 or REG_ISR, LAST_SEG;
1366 mov SG_CACHE_PRE, REG_ISR;
1367 mvi DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN);
1368 /*
1369 * Since we've are entering a data phase, we will
1370 * rely on the SCB_RESID* fields. Initialize the
1371 * residual and clear the full residual flag.
1372 */
1373 and SCB_SGPTR[0], ~SG_FULL_RESID;
1374 bmov SCB_RESIDUAL_DATACNT[3], SCB_DATACNT[3], 5;
1375 /* If we need more S/G elements, tell the idle loop */
1376 test SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jnz . + 2;
1377 mvi SG_STATE, LOADING_NEEDED ret;
1378 clr SG_STATE ret;
1379
1380 p_data_handle_xfer:
1381 call setjmp;
1382 test SG_STATE, LOADING_NEEDED jnz service_fifo;
1383 p_data_clear_handler:
1384 or LONGJMP_ADDR[1], INVALID_ADDR ret;
1385
1386 p_data:
1387 test SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT jz p_data_allowed;
1388 SET_SEQINTCODE(PROTO_VIOLATION)
1389 p_data_allowed:
1390
1391 test SEQ_FLAGS, DPHASE jz data_phase_initialize;
1392
1393 /*
1394 * If we re-enter the data phase after going through another
1395 * phase, our transfer location has almost certainly been
1396 * corrupted by the interveining, non-data, transfers. Ask
1397 * the host driver to fix us up based on the transfer residual
1398 * unless we already know that we should be bitbucketing.
1399 */
1400 test SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jnz p_data_bitbucket;
1401 SET_SEQINTCODE(PDATA_REINIT)
1402 jmp data_phase_inbounds;
1403
1404 p_data_bitbucket:
1405 /*
1406 * Turn on `Bit Bucket' mode, wait until the target takes
1407 * us to another phase, and then notify the host.
1408 */
1409 mov SAVED_MODE, MODE_PTR;
1410 test MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1))
1411 jnz bitbucket_not_m_dff;
1412 /*
1413 * Ensure that any FIFO contents are cleared out and the
1414 * FIFO free'd prior to starting the BITBUCKET. BITBUCKET
1415 * doesn't discard data already in the FIFO.
1416 */
1417 mvi DFFSXFRCTL, RSTCHN|CLRSHCNT;
1418 SET_MODE(M_SCSI, M_SCSI)
1419 bitbucket_not_m_dff:
1420 or SXFRCTL1,BITBUCKET;
1421 /* Wait for non-data phase. */
1422 test SCSIPHASE, ~DATA_PHASE_MASK jz .;
1423 and SXFRCTL1, ~BITBUCKET;
1424 RESTORE_MODE(SAVED_MODE)
1425 SET_SRC_MODE M_DFF1;
1426 SET_DST_MODE M_DFF1;
1427 SET_SEQINTCODE(DATA_OVERRUN)
1428 jmp ITloop;
1429
1430 data_phase_initialize:
1431 test SCB_SGPTR[0], SG_LIST_NULL jnz p_data_bitbucket;
1432 call load_first_seg;
1433 data_phase_inbounds:
1434 /* We have seen a data phase at least once. */
1435 or SEQ_FLAGS, DPHASE;
1436 mov SAVED_MODE, MODE_PTR;
1437 test SG_STATE, LOADING_NEEDED jz data_group_dma_loop;
1438 call p_data_handle_xfer;
1439 data_group_dma_loop:
1440 /*
1441 * The transfer is complete if either the last segment
1442 * completes or the target changes phase. Both conditions
1443 * will clear SCSIEN.
1444 */
1445 call idle_loop_service_fifos;
1446 call idle_loop_cchan;
1447 call idle_loop_gsfifo;
1448 RESTORE_MODE(SAVED_MODE)
1449 test DFCNTRL, SCSIEN jnz data_group_dma_loop;
1450
1451 data_group_dmafinish:
1452 /*
1453 * The transfer has terminated either due to a phase
1454 * change, and/or the completion of the last segment.
1455 * We have two goals here. Do as much other work
1456 * as possible while the data fifo drains on a read
1457 * and respond as quickly as possible to the standard
1458 * messages (save data pointers/disconnect and command
1459 * complete) that usually follow a data phase.
1460 */
1461 call calc_residual;
1462
1463 /*
1464 * Go ahead and shut down the DMA engine now.
1465 */
1466 test DFCNTRL, DIRECTION jnz data_phase_finish;
1467 data_group_fifoflush:
1468 if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
1469 or DFCNTRL, FIFOFLUSH;
1470 }
1471 /*
1472 * We have enabled the auto-ack feature. This means
1473 * that the controller may have already transferred
1474 * some overrun bytes into the data FIFO and acked them
1475 * on the bus. The only way to detect this situation is
1476 * to wait for LAST_SEG_DONE to come true on a completed
1477 * transfer and then test to see if the data FIFO is
1478 * non-empty. We know there is more data yet to transfer
1479 * if SG_LIST_NULL is not yet set, thus there cannot be
1480 * an overrun.
1481 */
1482 test SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz data_phase_finish;
1483 test SG_CACHE_SHADOW, LAST_SEG_DONE jz .;
1484 test DFSTATUS, FIFOEMP jnz data_phase_finish;
1485 /* Overrun */
1486 jmp p_data;
1487 data_phase_finish:
1488 /*
1489 * If the target has left us in data phase, loop through
1490 * the DMA code again. We will only loop if there is a
1491 * data overrun.
1492 */
1493 if ((ahd->flags & AHD_TARGETROLE) != 0) {
1494 test SSTAT0, TARGET jnz data_phase_done;
1495 }
1496 if ((ahd->flags & AHD_INITIATORROLE) != 0) {
1497 test SSTAT1, REQINIT jz .;
1498 test SCSIPHASE, DATA_PHASE_MASK jnz p_data;
1499 }
1500
1501 data_phase_done:
1502 /* Kill off any pending prefetch */
1503 call disable_ccsgen;
1504 or LONGJMP_ADDR[1], INVALID_ADDR;
1505
1506 if ((ahd->flags & AHD_TARGETROLE) != 0) {
1507 test SEQ_FLAGS, DPHASE_PENDING jz ITloop;
1508 /*
1509 and SEQ_FLAGS, ~DPHASE_PENDING;
1510 * For data-in phases, wait for any pending acks from the
1511 * initiator before changing phase. We only need to
1512 * send Ignore Wide Residue messages for data-in phases.
1513 test DFCNTRL, DIRECTION jz target_ITloop;
1514 test SSTAT1, REQINIT jnz .;
1515 test SCB_TASK_ATTRIBUTE, SCB_XFERLEN_ODD jz target_ITloop;
1516 SET_MODE(M_SCSI, M_SCSI)
1517 test NEGCONOPTS, WIDEXFER jz target_ITloop;
1518 */
1519 /*
1520 * Issue an Ignore Wide Residue Message.
1521 mvi P_MESGIN|BSYO call change_phase;
1522 mvi MSG_IGN_WIDE_RESIDUE call target_outb;
1523 mvi 1 call target_outb;
1524 jmp target_ITloop;
1525 */
1526 } else {
1527 jmp ITloop;
1528 }
1529
1530 /*
1531 * We assume that, even though data may still be
1532 * transferring to the host, that the SCSI side of
1533 * the DMA engine is now in a static state. This
1534 * allows us to update our notion of where we are
1535 * in this transfer.
1536 *
1537 * If, by chance, we stopped before being able
1538 * to fetch additional segments for this transfer,
1539 * yet the last S/G was completely exhausted,
1540 * call our idle loop until it is able to load
1541 * another segment. This will allow us to immediately
1542 * pickup on the next segment on the next data phase.
1543 *
1544 * If we happened to stop on the last segment, then
1545 * our residual information is still correct from
1546 * the idle loop and there is no need to perform
1547 * any fixups.
1548 */
1549 residual_before_last_seg:
1550 test MDFFSTAT, SHVALID jnz sgptr_fixup;
1551 /*
1552 * Can never happen from an interrupt as the packetized
1553 * hardware will only interrupt us once SHVALID or
1554 * LAST_SEG_DONE.
1555 */
1556 call idle_loop_service_fifos;
1557 RESTORE_MODE(SAVED_MODE)
1558 /* FALLTHROUGH */
1559 calc_residual:
1560 test SG_CACHE_SHADOW, LAST_SEG jz residual_before_last_seg;
1561 /* Record if we've consumed all S/G entries */
1562 test MDFFSTAT, SHVALID jz . + 2;
1563 bmov SCB_RESIDUAL_DATACNT, SHCNT, 3 ret;
1564 or SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL ret;
1565
1566 sgptr_fixup:
1567 /*
1568 * Fixup the residual next S/G pointer. The S/G preload
1569 * feature of the chip allows us to load two elements
1570 * in addition to the currently active element. We
1571 * store the bottom byte of the next S/G pointer in
1572 * the SG_CACHE_PTR register so we can restore the
1573 * correct value when the DMA completes. If the next
1574 * sg ptr value has advanced to the point where higher
1575 * bytes in the address have been affected, fix them
1576 * too.
1577 */
1578 test SG_CACHE_SHADOW, 0x80 jz sgptr_fixup_done;
1579 test SCB_RESIDUAL_SGPTR[0], 0x80 jnz sgptr_fixup_done;
1580 add SCB_RESIDUAL_SGPTR[1], -1;
1581 adc SCB_RESIDUAL_SGPTR[2], -1;
1582 adc SCB_RESIDUAL_SGPTR[3], -1;
1583 sgptr_fixup_done:
1584 and SCB_RESIDUAL_SGPTR[0], SG_ADDR_MASK, SG_CACHE_SHADOW;
1585 clr SCB_RESIDUAL_DATACNT[3]; /* We are not the last seg */
1586 bmov SCB_RESIDUAL_DATACNT, SHCNT, 3 ret;
1587
1588 export timer_isr:
1589 call issue_cmdcmplt;
1590 mvi CLRSEQINTSTAT, CLRSEQ_SWTMRTO;
1591 if ((ahd->bugs & AHD_SET_MODE_BUG) != 0) {
1592 /*
1593 * In H2A4, the mode pointer is not saved
1594 * for intvec2, but is restored on iret.
1595 * This can lead to the restoration of a
1596 * bogus mode ptr. Manually clear the
1597 * intmask bits and do a normal return
1598 * to compensate.
1599 */
1600 and SEQINTCTL, ~(INTMASK2|INTMASK1) ret;
1601 } else {
1602 or SEQINTCTL, IRET ret;
1603 }
1604
1605 export seq_isr:
1606 if ((ahd->features & AHD_RTI) == 0) {
1607 /*
1608 * On RevA Silicon, if the target returns us to data-out
1609 * after we have already trained for data-out, it is
1610 * possible for us to transition the free running clock to
1611 * data-valid before the required 100ns P1 setup time (8 P1
1612 * assertions in fast-160 mode). This will only happen if
1613 * this L-Q is a continuation of a data transfer for which
1614 * we have already prefetched data into our FIFO (LQ/Data
1615 * followed by LQ/Data for the same write transaction).
1616 * This can cause some target implementations to miss the
1617 * first few data transfers on the bus. We detect this
1618 * situation by noticing that this is the first data transfer
1619 * after an LQ (LQIWORKONLQ true), that the data transfer is
1620 * a continuation of a transfer already setup in our FIFO
1621 * (SAVEPTRS interrupt), and that the transaction is a write
1622 * (DIRECTION set in DFCNTRL). The delay is performed by
1623 * disabling SCSIEN until we see the first REQ from the
1624 * target.
1625 *
1626 * First instruction in an ISR cannot be a branch on
1627 * Rev A. Snapshot LQISTAT2 so the status is not missed
1628 * and deffer the test by one instruction.
1629 */
1630 mov REG_ISR, LQISTAT2;
1631 test REG_ISR, LQIWORKONLQ jz main_isr;
1632 test SEQINTSRC, SAVEPTRS jz main_isr;
1633 test LONGJMP_ADDR[1], INVALID_ADDR jz saveptr_active_fifo;
1634 /*
1635 * Switch to the active FIFO after clearing the snapshot
1636 * savepointer in the current FIFO. We do this so that
1637 * a pending CTXTDONE or SAVEPTR is visible in the active
1638 * FIFO. This status is the only way we can detect if we
1639 * have lost the race (e.g. host paused us) and our attepts
1640 * to disable the channel occurred after all REQs were
1641 * already seen and acked (REQINIT never comes true).
1642 */
1643 mvi DFFSXFRCTL, CLRCHN;
1644 xor MODE_PTR, MK_MODE(M_DFF1, M_DFF1);
1645 test DFCNTRL, DIRECTION jz interrupt_return;
1646 and DFCNTRL, ~SCSIEN;
1647 snapshot_wait_data_valid:
1648 test SEQINTSRC, (CTXTDONE|SAVEPTRS) jnz snapshot_data_valid;
1649 test SSTAT1, REQINIT jz snapshot_wait_data_valid;
1650 snapshot_data_valid:
1651 or DFCNTRL, SCSIEN;
1652 or SEQINTCTL, IRET ret;
1653 snapshot_saveptr:
1654 mvi DFFSXFRCTL, CLRCHN;
1655 or SEQINTCTL, IRET ret;
1656 main_isr:
1657 }
1658 test SEQINTSRC, CFG4DATA jnz cfg4data_intr;
1659 test SEQINTSRC, CFG4ISTAT jnz cfg4istat_intr;
1660 test SEQINTSRC, SAVEPTRS jnz saveptr_intr;
1661 test SEQINTSRC, CFG4ICMD jnz cfg4icmd_intr;
1662 SET_SEQINTCODE(INVALID_SEQINT)
1663
1664 /*
1665 * There are two types of save pointers interrupts:
1666 * The first is a snapshot save pointers where the current FIFO is not
1667 * active and contains a snapshot of the current poniter information.
1668 * This happens between packets in a stream for a single L_Q. Since we
1669 * are not performing a pointer save, we can safely clear the channel
1670 * so it can be used for other transactions. On RTI capable controllers,
1671 * where snapshots can, and are, disabled, the code to handle this type
1672 * of snapshot is not active.
1673 *
1674 * The second case is a save pointers on an active FIFO which occurs
1675 * if the target changes to a new L_Q or busfrees/QASes and the transfer
1676 * has a residual. This should occur coincident with a ctxtdone. We
1677 * disable the interrupt and allow our active routine to handle the
1678 * save.
1679 */
1680 saveptr_intr:
1681 if ((ahd->features & AHD_RTI) == 0) {
1682 test LONGJMP_ADDR[1], INVALID_ADDR jnz snapshot_saveptr;
1683 }
1684 saveptr_active_fifo:
1685 and SEQIMODE, ~ENSAVEPTRS;
1686 or SEQINTCTL, IRET ret;
1687
1688 cfg4data_intr:
1689 test SCB_SGPTR[0], SG_LIST_NULL jnz pkt_handle_overrun_inc_use_count;
1690 call load_first_seg;
1691 call pkt_handle_xfer;
1692 inc SCB_FIFO_USE_COUNT;
1693 interrupt_return:
1694 or SEQINTCTL, IRET ret;
1695
1696 cfg4istat_intr:
1697 call freeze_queue;
1698 add NONE, -13, SCB_CDB_LEN;
1699 jnc cfg4istat_have_sense_addr;
1700 test SCB_CDB_LEN, SCB_CDB_LEN_PTR jnz cfg4istat_have_sense_addr;
1701 /*
1702 * Host sets up address/count and enables transfer.
1703 */
1704 SET_SEQINTCODE(CFG4ISTAT_INTR)
1705 jmp cfg4istat_setup_handler;
1706 cfg4istat_have_sense_addr:
1707 bmov HADDR, SCB_SENSE_BUSADDR, 4;
1708 mvi HCNT[1], (AHD_SENSE_BUFSIZE >> 8);
1709 mvi SG_CACHE_PRE, LAST_SEG;
1710 mvi DFCNTRL, PRELOADEN|SCSIEN|HDMAEN;
1711 cfg4istat_setup_handler:
1712 /*
1713 * Status pkt is transferring to host.
1714 * Wait in idle loop for transfer to complete.
1715 * If a command completed before an attempted
1716 * task management function completed, notify the host.
1717 */
1718 test SCB_TASK_MANAGEMENT, 0xFF jz cfg4istat_no_taskmgmt_func;
1719 SET_SEQINTCODE(TASKMGMT_CMD_CMPLT_OKAY)
1720 cfg4istat_no_taskmgmt_func:
1721 call pkt_handle_status;
1722 or SEQINTCTL, IRET ret;
1723
1724 cfg4icmd_intr:
1725 /*
1726 * In the case of DMAing a CDB from the host, the normal
1727 * CDB buffer is formatted with an 8 byte address followed
1728 * by a 1 byte count.
1729 */
1730 bmov HADDR[0], SCB_HOST_CDB_PTR, 9;
1731 mvi SG_CACHE_PRE, LAST_SEG;
1732 mvi DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN);
1733 call pkt_handle_cdb;
1734 or SEQINTCTL, IRET ret;
1735
1736 /*
1737 * See if the target has gone on in this context creating an
1738 * overrun condition. For the write case, the hardware cannot
1739 * ack bytes until data are provided. So, if the target begins
1740 * another packet without changing contexts, implying we are
1741 * not sitting on a packet boundary, we are in an overrun
1742 * situation. For the read case, the hardware will continue to
1743 * ack bytes into the FIFO, and may even ack the last overrun packet
1744 * into the FIFO. If the FIFO should become non-empty, we are in
1745 * a read overrun case.
1746 */
1747 #define check_overrun \
1748 /* Not on a packet boundary. */ \
1749 test MDFFSTAT, DLZERO jz pkt_handle_overrun; \
1750 test DFSTATUS, FIFOEMP jz pkt_handle_overrun
1751
1752 pkt_handle_xfer:
1753 test SG_STATE, LOADING_NEEDED jz pkt_last_seg;
1754 call setjmp;
1755 test SEQINTSRC, SAVEPTRS jnz pkt_saveptrs;
1756 test SCSIPHASE, ~DATA_PHASE_MASK jz . + 2;
1757 test SCSISIGO, ATNO jnz . + 2;
1758 test SSTAT2, NONPACKREQ jz pkt_service_fifo;
1759 /*
1760 * Defer handling of this NONPACKREQ until we
1761 * can be sure it pertains to this FIFO. SAVEPTRS
1762 * will not be asserted if the NONPACKREQ is for us,
1763 * so we must simulate it if shaddow is valid. If
1764 * shaddow is not valid, keep running this FIFO until we
1765 * have satisfied the transfer by loading segments and
1766 * waiting for either shaddow valid or last_seg_done.
1767 */
1768 test MDFFSTAT, SHVALID jnz pkt_saveptrs;
1769 pkt_service_fifo:
1770 test SG_STATE, LOADING_NEEDED jnz service_fifo;
1771 pkt_last_seg:
1772 call setjmp;
1773 test SEQINTSRC, SAVEPTRS jnz pkt_saveptrs;
1774 test SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_last_seg_done;
1775 test SCSIPHASE, ~DATA_PHASE_MASK jz . + 2;
1776 test SCSISIGO, ATNO jnz . + 2;
1777 test SSTAT2, NONPACKREQ jz return;
1778 test MDFFSTAT, SHVALID jz return;
1779 /* FALLTHROUGH */
1780
1781 /*
1782 * Either a SAVEPTRS interrupt condition is pending for this FIFO
1783 * or we have a pending NONPACKREQ for this FIFO. We differentiate
1784 * between the two by capturing the state of the SAVEPTRS interrupt
1785 * prior to clearing this status and executing the common code for
1786 * these two cases.
1787 */
1788 pkt_saveptrs:
1789 BEGIN_CRITICAL;
1790 if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
1791 or DFCNTRL, FIFOFLUSH;
1792 }
1793 mov REG0, SEQINTSRC;
1794 call calc_residual;
1795 call save_pointers;
1796 mvi CLRSEQINTSRC, CLRSAVEPTRS;
1797 call disable_ccsgen;
1798 or SEQIMODE, ENSAVEPTRS;
1799 test DFCNTRL, DIRECTION jnz pkt_saveptrs_check_status;
1800 test DFSTATUS, FIFOEMP jnz pkt_saveptrs_check_status;
1801 /*
1802 * Keep a handler around for this FIFO until it drains
1803 * to the host to guarantee that we don't complete the
1804 * command to the host before the data arrives.
1805 */
1806 pkt_saveptrs_wait_fifoemp:
1807 call setjmp;
1808 test DFSTATUS, FIFOEMP jz return;
1809 pkt_saveptrs_check_status:
1810 or LONGJMP_ADDR[1], INVALID_ADDR;
1811 test REG0, SAVEPTRS jz unexpected_nonpkt_phase;
1812 dec SCB_FIFO_USE_COUNT;
1813 test SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
1814 mvi DFFSXFRCTL, CLRCHN ret;
1815 END_CRITICAL;
1816
1817 /*
1818 * LAST_SEG_DONE status has been seen in the current FIFO.
1819 * This indicates that all of the allowed data for this
1820 * command has transferred across the SCSI and host buses.
1821 * Check for overrun and see if we can complete this command.
1822 */
1823 pkt_last_seg_done:
1824 BEGIN_CRITICAL;
1825 /*
1826 * Mark transfer as completed.
1827 */
1828 or SCB_SGPTR, SG_LIST_NULL;
1829
1830 /*
1831 * Wait for the current context to finish to verify that
1832 * no overrun condition has occurred.
1833 */
1834 test SEQINTSRC, CTXTDONE jnz pkt_ctxt_done;
1835 call setjmp;
1836 pkt_wait_ctxt_done_loop:
1837 test SEQINTSRC, CTXTDONE jnz pkt_ctxt_done;
1838 /*
1839 * A sufficiently large overrun or a NONPACKREQ may
1840 * prevent CTXTDONE from ever asserting, so we must
1841 * poll for these statuses too.
1842 */
1843 check_overrun;
1844 test SSTAT2, NONPACKREQ jz return;
1845 test SEQINTSRC, CTXTDONE jz unexpected_nonpkt_phase;
1846 /* FALLTHROUGH */
1847
1848 pkt_ctxt_done:
1849 check_overrun;
1850 or LONGJMP_ADDR[1], INVALID_ADDR;
1851 /*
1852 * If status has been received, it is safe to skip
1853 * the check to see if another FIFO is active because
1854 * LAST_SEG_DONE has been observed. However, we check
1855 * the FIFO anyway since it costs us only one extra
1856 * instruction to leverage common code to perform the
1857 * SCB completion.
1858 */
1859 dec SCB_FIFO_USE_COUNT;
1860 test SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
1861 mvi DFFSXFRCTL, CLRCHN ret;
1862 END_CRITICAL;
1863
1864 /*
1865 * Must wait until CDB xfer is over before issuing the
1866 * clear channel.
1867 */
1868 pkt_handle_cdb:
1869 call setjmp;
1870 test SG_CACHE_SHADOW, LAST_SEG_DONE jz return;
1871 or LONGJMP_ADDR[1], INVALID_ADDR;
1872 mvi DFFSXFRCTL, CLRCHN ret;
1873
1874 /*
1875 * Watch over the status transfer. Our host sense buffer is
1876 * large enough to take the maximum allowed status packet.
1877 * None-the-less, we must still catch and report overruns to
1878 * the host. Additionally, properly catch unexpected non-packet
1879 * phases that are typically caused by CRC errors in status packet
1880 * transmission.
1881 */
1882 pkt_handle_status:
1883 call setjmp;
1884 test SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_status_check_overrun;
1885 test SEQINTSRC, CTXTDONE jz pkt_status_check_nonpackreq;
1886 test SG_CACHE_SHADOW, LAST_SEG_DONE jnz pkt_status_check_overrun;
1887 pkt_status_IU_done:
1888 if ((ahd->bugs & AHD_AUTOFLUSH_BUG) != 0) {
1889 or DFCNTRL, FIFOFLUSH;
1890 }
1891 test DFSTATUS, FIFOEMP jz return;
1892 BEGIN_CRITICAL;
1893 or LONGJMP_ADDR[1], INVALID_ADDR;
1894 mvi SCB_SCSI_STATUS, STATUS_PKT_SENSE;
1895 or SCB_CONTROL, STATUS_RCVD;
1896 jmp pkt_complete_scb_if_fifos_idle;
1897 END_CRITICAL;
1898 pkt_status_check_overrun:
1899 /*
1900 * Status PKT overruns are uncerimoniously recovered with a
1901 * bus reset. If we've overrun, let the host know so that
1902 * recovery can be performed.
1903 *
1904 * LAST_SEG_DONE has been observed. If either CTXTDONE or
1905 * a NONPACKREQ phase change have occurred and the FIFO is
1906 * empty, there is no overrun.
1907 */
1908 test DFSTATUS, FIFOEMP jz pkt_status_report_overrun;
1909 test SEQINTSRC, CTXTDONE jz . + 2;
1910 test DFSTATUS, FIFOEMP jnz pkt_status_IU_done;
1911 test SCSIPHASE, ~DATA_PHASE_MASK jz return;
1912 test DFSTATUS, FIFOEMP jnz pkt_status_check_nonpackreq;
1913 pkt_status_report_overrun:
1914 SET_SEQINTCODE(STATUS_OVERRUN)
1915 /* SEQUENCER RESTARTED */
1916 pkt_status_check_nonpackreq:
1917 /*
1918 * CTXTDONE may be held off if a NONPACKREQ is associated with
1919 * the current context. If a NONPACKREQ is observed, decide
1920 * if it is for the current context. If it is for the current
1921 * context, we must defer NONPACKREQ processing until all data
1922 * has transferred to the host.
1923 */
1924 test SCSIPHASE, ~DATA_PHASE_MASK jz return;
1925 test SCSISIGO, ATNO jnz . + 2;
1926 test SSTAT2, NONPACKREQ jz return;
1927 test SEQINTSRC, CTXTDONE jnz pkt_status_IU_done;
1928 test DFSTATUS, FIFOEMP jz return;
1929 /*
1930 * The unexpected nonpkt phase handler assumes that any
1931 * data channel use will have a FIFO reference count. It
1932 * turns out that the status handler doesn't need a refernce
1933 * count since the status received flag, and thus completion
1934 * processing, cannot be set until the handler is finished.
1935 * We increment the count here to make the nonpkt handler
1936 * happy.
1937 */
1938 inc SCB_FIFO_USE_COUNT;
1939 /* FALLTHROUGH */
1940
1941 /*
1942 * Nonpackreq is a polled status. It can come true in three situations:
1943 * we have received an L_Q, we have sent one or more L_Qs, or there is no
1944 * L_Q context associated with this REQ (REQ occurs immediately after a
1945 * (re)selection). Routines that know that the context responsible for this
1946 * nonpackreq call directly into unexpected_nonpkt_phase. In the case of the
1947 * top level idle loop, we exhaust all active contexts prior to determining that
1948 * we simply do not have the full I_T_L_Q for this phase.
1949 */
1950 unexpected_nonpkt_phase_find_ctxt:
1951 /*
1952 * This nonpackreq is most likely associated with one of the tags
1953 * in a FIFO or an outgoing LQ. Only treat it as an I_T only
1954 * nonpackreq if we've cleared out the FIFOs and handled any
1955 * pending SELDO.
1956 */
1957 SET_SRC_MODE M_SCSI;
1958 SET_DST_MODE M_SCSI;
1959 and A, FIFO1FREE|FIFO0FREE, DFFSTAT;
1960 cmp A, FIFO1FREE|FIFO0FREE jne return;
1961 test SSTAT0, SELDO jnz return;
1962 mvi SCBPTR[1], SCB_LIST_NULL;
1963 unexpected_nonpkt_phase:
1964 test MODE_PTR, ~(MK_MODE(M_DFF1, M_DFF1)) jnz . + 3;
1965 SET_SRC_MODE M_DFF0;
1966 SET_DST_MODE M_DFF0;
1967 or LONGJMP_ADDR[1], INVALID_ADDR;
1968 dec SCB_FIFO_USE_COUNT;
1969 mvi DFFSXFRCTL, CLRCHN;
1970 mvi CLRSINT2, CLRNONPACKREQ;
1971 test SCSIPHASE, ~(MSG_IN_PHASE|MSG_OUT_PHASE) jnz illegal_phase;
1972 SET_SEQINTCODE(ENTERING_NONPACK)
1973 jmp ITloop;
1974
1975 illegal_phase:
1976 SET_SEQINTCODE(ILLEGAL_PHASE)
1977 jmp ITloop;
1978
1979 /*
1980 * We have entered an overrun situation. If we have working
1981 * BITBUCKET, flip that on and let the hardware eat any overrun
1982 * data. Otherwise use an overrun buffer in the host to simulate
1983 * BITBUCKET.
1984 */
1985 pkt_handle_overrun_inc_use_count:
1986 inc SCB_FIFO_USE_COUNT;
1987 pkt_handle_overrun:
1988 SET_SEQINTCODE(CFG4OVERRUN)
1989 call freeze_queue;
1990 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) == 0) {
1991 or DFFSXFRCTL, DFFBITBUCKET;
1992 SET_SRC_MODE M_DFF1;
1993 SET_DST_MODE M_DFF1;
1994 } else {
1995 call load_overrun_buf;
1996 mvi DFCNTRL, (HDMAEN|SCSIEN|PRELOADEN);
1997 }
1998 call setjmp;
1999 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
2000 test DFSTATUS, PRELOAD_AVAIL jz overrun_load_done;
2001 call load_overrun_buf;
2002 or DFCNTRL, PRELOADEN;
2003 overrun_load_done:
2004 test SEQINTSRC, CTXTDONE jnz pkt_overrun_end;
2005 } else {
2006 test DFFSXFRCTL, DFFBITBUCKET jz pkt_overrun_end;
2007 }
2008 test SSTAT2, NONPACKREQ jz return;
2009 pkt_overrun_end:
2010 or SCB_RESIDUAL_SGPTR, SG_OVERRUN_RESID;
2011 test SEQINTSRC, CTXTDONE jz unexpected_nonpkt_phase;
2012 dec SCB_FIFO_USE_COUNT;
2013 or LONGJMP_ADDR[1], INVALID_ADDR;
2014 test SCB_CONTROL, STATUS_RCVD jnz pkt_complete_scb_if_fifos_idle;
2015 mvi DFFSXFRCTL, CLRCHN ret;
2016
2017 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
2018 load_overrun_buf:
2019 /*
2020 * Load a dummy segment if preload space is available.
2021 */
2022 mov HADDR[0], SHARED_DATA_ADDR;
2023 add HADDR[1], PKT_OVERRUN_BUFOFFSET, SHARED_DATA_ADDR[1];
2024 mov ACCUM_SAVE, A;
2025 clr A;
2026 adc HADDR[2], A, SHARED_DATA_ADDR[2];
2027 adc HADDR[3], A, SHARED_DATA_ADDR[3];
2028 mov A, ACCUM_SAVE;
2029 bmov HADDR[4], ALLZEROS, 4;
2030 /* PKT_OVERRUN_BUFSIZE is a multiple of 256 */
2031 clr HCNT[0];
2032 mvi HCNT[1], ((PKT_OVERRUN_BUFSIZE >> 8) & 0xFF);
2033 clr HCNT[2] ret;
2034 }
2035