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