1 /* $NetBSD: ncr5380.c,v 1.47 2004/12/07 21:12:42 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Leo Weppelman. 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 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Leo Weppelman. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: ncr5380.c,v 1.47 2004/12/07 21:12:42 thorpej Exp $"); 35 36 /* 37 * Bit mask of targets you want debugging to be shown 38 */ 39 u_char dbg_target_mask = 0x7f; 40 41 /* 42 * Set bit for target when parity checking must be disabled. 43 * My (LWP) Maxtor 7245S seems to generate parity errors on about 50% 44 * of all transfers while the data is correct!? 45 */ 46 u_char ncr5380_no_parchk = 0xff; 47 48 #ifdef AUTO_SENSE 49 50 /* 51 * Bit masks of targets that accept linked commands, and those 52 * that we've already checked out. Some devices will report 53 * that they support linked commands when they have problems with 54 * them. By default, don't try them on any devices. Allow an 55 * option to override. 56 */ 57 #ifdef TRY_SCSI_LINKED_COMMANDS 58 u_char ncr_test_link = ((~TRY_SCSI_LINKED_COMMANDS) & 0x7f); 59 #else 60 u_char ncr_test_link = 0x7f; 61 #endif 62 u_char ncr_will_link = 0x00; 63 64 #endif /* AUTO_SENSE */ 65 66 /* 67 * This is the default sense-command we send. 68 */ 69 static u_char sense_cmd[] = { 70 REQUEST_SENSE, 0, 0, 0, sizeof(struct scsipi_sense_data), 0 71 }; 72 73 /* 74 * True if the main co-routine is running 75 */ 76 static volatile int main_running = 0; 77 78 /* 79 * Mask of targets selected 80 */ 81 static u_char busy; 82 83 static void ncr5380_minphys __P((struct buf *bp)); 84 static void ncr5380_scsi_request __P((struct scsipi_channel *, 85 scsipi_adapter_req_t, void *)); 86 static void ncr5380_show_scsi_cmd __P((struct scsipi_xfer *xs)); 87 88 static SC_REQ req_queue[NREQ]; 89 static SC_REQ *free_head = NULL; /* Free request structures */ 90 91 92 /* 93 * Inline functions: 94 */ 95 96 /* 97 * Wait for request-line to become active. When it doesn't return 0. 98 * Otherwise return != 0. 99 * The timeouts in the 'wait_req_*' functions are arbitrary and rather 100 * large. In 99% of the invocations nearly no timeout is needed but in 101 * some cases (especially when using my tapedrive, a Tandberg 3600) the 102 * device is busy internally and the first SCSI-phase will be delayed. 103 */ 104 extern __inline__ int wait_req_true(void) 105 { 106 int timeout = 250000; 107 108 while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout) 109 delay(1); 110 return (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ); 111 } 112 113 /* 114 * Wait for request-line to become inactive. When it doesn't return 0. 115 * Otherwise return != 0. 116 */ 117 extern __inline__ int wait_req_false(void) 118 { 119 int timeout = 250000; 120 121 while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout) 122 delay(1); 123 return (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ)); 124 } 125 126 extern __inline__ void ack_message() 127 { 128 SET_5380_REG(NCR5380_ICOM, 0); 129 } 130 131 extern __inline__ void nack_message(SC_REQ *reqp, u_char msg) 132 { 133 SET_5380_REG(NCR5380_ICOM, SC_A_ATN); 134 reqp->msgout = msg; 135 } 136 137 extern __inline__ void finish_req(SC_REQ *reqp) 138 { 139 int sps; 140 struct scsipi_xfer *xs = reqp->xs; 141 142 #ifdef REAL_DMA 143 /* 144 * If we bounced, free the bounce buffer 145 */ 146 if (reqp->dr_flag & DRIVER_BOUNCING) 147 free_bounceb(reqp->bounceb); 148 #endif /* REAL_DMA */ 149 #ifdef DBG_REQ 150 if (dbg_target_mask & (1 << reqp->targ_id)) 151 show_request(reqp, "DONE"); 152 #endif 153 #ifdef DBG_ERR_RET 154 if ((dbg_target_mask & (1 << reqp->targ_id)) && (reqp->xs->error != 0)) 155 show_request(reqp, "ERR_RET"); 156 #endif 157 /* 158 * Return request to free-q 159 */ 160 sps = splbio(); 161 reqp->next = free_head; 162 free_head = reqp; 163 splx(sps); 164 165 xs->xs_status |= XS_STS_DONE; 166 if (!(reqp->dr_flag & DRIVER_LINKCHK)) 167 scsipi_done(xs); 168 } 169 170 /* 171 * Auto config stuff.... 172 */ 173 void ncr_attach __P((struct device *, struct device *, void *)); 174 int ncr_match __P((struct device *, struct cfdata *, void *)); 175 176 /* 177 * Tricks to make driver-name configurable 178 */ 179 #define CFNAME(n) __CONCAT(n,_cd) 180 #define CANAME(n) __CONCAT(n,_ca) 181 #define CFSTRING(n) __STRING(n) 182 #define CFDRNAME(n) n 183 184 CFATTACH_DECL(CFDRNAME(DRNAME), sizeof(struct ncr_softc), 185 ncr_match, ncr_attach, NULL, NULL); 186 187 extern struct cfdriver CFNAME(DRNAME); 188 189 int 190 ncr_match(pdp, cfp, auxp) 191 struct device *pdp; 192 struct cfdata *cfp; 193 void *auxp; 194 { 195 return (machine_match(pdp, cfp, auxp, &CFNAME(DRNAME))); 196 } 197 198 void 199 ncr_attach(pdp, dp, auxp) 200 struct device *pdp, *dp; 201 void *auxp; 202 { 203 struct ncr_softc *sc; 204 int i; 205 206 sc = (struct ncr_softc *)dp; 207 208 sc->sc_adapter.adapt_dev = &sc->sc_dev; 209 sc->sc_adapter.adapt_openings = 7; 210 sc->sc_adapter.adapt_max_periph = 1; 211 sc->sc_adapter.adapt_ioctl = NULL; 212 sc->sc_adapter.adapt_minphys = ncr5380_minphys; 213 sc->sc_adapter.adapt_request = ncr5380_scsi_request; 214 215 sc->sc_channel.chan_adapter = &sc->sc_adapter; 216 sc->sc_channel.chan_bustype = &scsi_bustype; 217 sc->sc_channel.chan_channel = 0; 218 sc->sc_channel.chan_ntargets = 8; 219 sc->sc_channel.chan_nluns = 8; 220 sc->sc_channel.chan_id = 7; 221 222 /* 223 * bitmasks 224 */ 225 sc->sc_noselatn = 0; 226 sc->sc_selected = 0; 227 228 /* 229 * Initialize machine-type specific things... 230 */ 231 scsi_mach_init(sc); 232 printf("\n"); 233 234 /* 235 * Initialize request queue freelist. 236 */ 237 for (i = 0; i < NREQ; i++) { 238 req_queue[i].next = free_head; 239 free_head = &req_queue[i]; 240 } 241 242 /* 243 * Initialize the host adapter 244 */ 245 scsi_idisable(); 246 ENABLE_NCR5380(sc); 247 SET_5380_REG(NCR5380_ICOM, 0); 248 SET_5380_REG(NCR5380_MODE, IMODE_BASE); 249 SET_5380_REG(NCR5380_TCOM, 0); 250 SET_5380_REG(NCR5380_IDSTAT, 0); 251 scsi_ienable(); 252 253 /* 254 * attach all scsi units on us 255 */ 256 config_found(dp, &sc->sc_channel, scsiprint); 257 } 258 259 /* 260 * End of auto config stuff.... 261 */ 262 263 /* 264 * Carry out a request from the high level driver. 265 */ 266 static void 267 ncr5380_scsi_request(chan, req, arg) 268 struct scsipi_channel *chan; 269 scsipi_adapter_req_t req; 270 void *arg; 271 { 272 struct scsipi_xfer *xs; 273 struct scsipi_periph *periph; 274 struct ncr_softc *sc = (void *)chan->chan_adapter->adapt_dev; 275 int sps; 276 SC_REQ *reqp, *link, *tmp; 277 int flags; 278 279 switch (req) { 280 case ADAPTER_REQ_RUN_XFER: 281 xs = arg; 282 periph = xs->xs_periph; 283 284 /* 285 * We do not queue RESET commands 286 */ 287 flags = xs->xs_control; 288 if (flags & XS_CTL_RESET) { 289 scsi_reset_verbose(sc, "Got reset-command"); 290 scsipi_done(xs); 291 return; 292 } 293 294 /* 295 * Get a request block 296 */ 297 sps = splbio(); 298 if ((reqp = free_head) == 0) { 299 xs->error = XS_RESOURCE_SHORTAGE; 300 scsipi_done(xs); 301 splx(sps); 302 return; 303 } 304 free_head = reqp->next; 305 reqp->next = NULL; 306 splx(sps); 307 308 /* 309 * Initialize our private fields 310 */ 311 reqp->dr_flag = (flags & XS_CTL_POLL) ? DRIVER_NOINT : 0; 312 reqp->phase = NR_PHASE; 313 reqp->msgout = MSG_NOOP; 314 reqp->status = SCSGOOD; 315 reqp->message = 0xff; 316 reqp->link = NULL; 317 reqp->xs = xs; 318 reqp->targ_id = xs->xs_periph->periph_target; 319 reqp->targ_lun = xs->xs_periph->periph_lun; 320 reqp->xdata_ptr = (u_char*)xs->data; 321 reqp->xdata_len = xs->datalen; 322 memcpy(&reqp->xcmd, xs->cmd, xs->cmdlen); 323 reqp->xcmd_len = xs->cmdlen; 324 reqp->xcmd.bytes[0] |= reqp->targ_lun << 5; 325 326 #ifdef REAL_DMA 327 /* 328 * Check if DMA can be used on this request 329 */ 330 if (scsi_dmaok(reqp)) 331 reqp->dr_flag |= DRIVER_DMAOK; 332 #endif /* REAL_DMA */ 333 334 /* 335 * Insert the command into the issue queue. Note that 336 * 'REQUEST SENSE' commands are inserted at the head of the 337 * queue since any command will clear the existing contingent 338 * allegience condition and the sense data is only valid while 339 * the condition exists. When possible, link the command to a 340 * previous command to the same target. This is not very 341 * sensible when AUTO_SENSE is not defined! Interrupts are 342 * disabled while we are fiddling with the issue-queue. 343 */ 344 sps = splbio(); 345 link = NULL; 346 if ((issue_q == NULL) || (reqp->xcmd.opcode == REQUEST_SENSE)) { 347 reqp->next = issue_q; 348 issue_q = reqp; 349 } 350 else { 351 tmp = issue_q; 352 do { 353 if (!link && (tmp->targ_id == reqp->targ_id) && !tmp->link) 354 link = tmp; 355 } while (tmp->next && (tmp = tmp->next)); 356 tmp->next = reqp; 357 #ifdef AUTO_SENSE 358 if (link && (ncr_will_link & (1<<reqp->targ_id))) { 359 link->link = reqp; 360 link->xcmd.bytes[link->xs->cmdlen-2] |= 1; 361 } 362 #endif 363 } 364 #ifdef AUTO_SENSE 365 /* 366 * If we haven't already, check the target for link support. 367 * Do this by prefixing the current command with a dummy 368 * Request_Sense command, link the dummy to the current 369 * command, and insert the dummy command at the head of the 370 * issue queue. Set the DRIVER_LINKCHK flag so that we'll 371 * ignore the results of the dummy command, since we only 372 * care about whether it was accepted or not. 373 */ 374 if (!link && !(ncr_test_link & (1<<reqp->targ_id)) && 375 (tmp = free_head) && !(reqp->dr_flag & DRIVER_NOINT)) { 376 free_head = tmp->next; 377 tmp->dr_flag = (reqp->dr_flag & ~DRIVER_DMAOK) | DRIVER_LINKCHK; 378 tmp->phase = NR_PHASE; 379 tmp->msgout = MSG_NOOP; 380 tmp->status = SCSGOOD; 381 tmp->xs = reqp->xs; 382 tmp->targ_id = reqp->targ_id; 383 tmp->targ_lun = reqp->targ_lun; 384 bcopy(sense_cmd, &tmp->xcmd, sizeof(sense_cmd)); 385 tmp->xcmd_len = sizeof(sense_cmd); 386 tmp->xdata_ptr = (u_char *)&tmp->xs->sense.scsi_sense; 387 tmp->xdata_len = sizeof(tmp->xs->sense.scsi_sense); 388 ncr_test_link |= 1<<tmp->targ_id; 389 tmp->link = reqp; 390 tmp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1; 391 tmp->next = issue_q; 392 issue_q = tmp; 393 #ifdef DBG_REQ 394 if (dbg_target_mask & (1 << tmp->targ_id)) 395 show_request(tmp, "LINKCHK"); 396 #endif 397 } 398 #endif 399 splx(sps); 400 401 #ifdef DBG_REQ 402 if (dbg_target_mask & (1 << reqp->targ_id)) 403 show_request(reqp, (reqp->xcmd.opcode == REQUEST_SENSE) ? 404 "HEAD":"TAIL"); 405 #endif 406 407 run_main(sc); 408 return; 409 410 case ADAPTER_REQ_GROW_RESOURCES: 411 /* XXX Not supported. */ 412 return; 413 414 case ADAPTER_REQ_SET_XFER_MODE: 415 /* XXX Not supported. */ 416 return; 417 } 418 } 419 420 static void 421 ncr5380_minphys(struct buf *bp) 422 { 423 if (bp->b_bcount > MIN_PHYS) 424 bp->b_bcount = MIN_PHYS; 425 minphys(bp); 426 } 427 #undef MIN_PHYS 428 429 static void 430 ncr5380_show_scsi_cmd(struct scsipi_xfer *xs) 431 { 432 u_char *b = (u_char *) xs->cmd; 433 int i = 0; 434 435 scsipi_printaddr(xs->xs_periph); 436 if (!(xs->xs_control & XS_CTL_RESET)) { 437 while (i < xs->cmdlen) { 438 if (i) 439 printf(","); 440 printf("%x",b[i++]); 441 } 442 printf("-\n"); 443 } 444 else { 445 446 printf("-RESET-\n"); 447 } 448 } 449 450 /* 451 * The body of the driver. 452 */ 453 static void 454 scsi_main(sc) 455 struct ncr_softc *sc; 456 { 457 SC_REQ *req, *prev; 458 int itype; 459 int sps; 460 461 /* 462 * While running in the driver SCSI-interrupts are disabled. 463 */ 464 scsi_idisable(); 465 ENABLE_NCR5380(sc); 466 467 PID("scsi_main1"); 468 for (;;) { 469 sps = splbio(); 470 if (!connected) { 471 /* 472 * Check if it is fair keep any exclusive access to DMA 473 * claimed. If not, stop queueing new jobs so the discon_q 474 * will be eventually drained and DMA can be given up. 475 */ 476 if (!fair_to_keep_dma()) 477 goto main_exit; 478 479 /* 480 * Search through the issue-queue for a command 481 * destined for a target that isn't busy. 482 */ 483 prev = NULL; 484 for (req=issue_q; req != NULL; prev = req, req = req->next) { 485 if (!(busy & (1 << req->targ_id))) { 486 /* 487 * Found one, remove it from the issue queue 488 */ 489 if (prev == NULL) 490 issue_q = req->next; 491 else prev->next = req->next; 492 req->next = NULL; 493 break; 494 } 495 } 496 497 /* 498 * When a request has just ended, we get here before an other 499 * device detects that the bus is free and that it can 500 * reconnect. The problem is that when this happens, we always 501 * baffle the device because our (initiator) id is higher. This 502 * can cause a sort of starvation on slow devices. So we check 503 * for a pending reselection here. 504 * Note that 'connected' will be non-null if the reselection 505 * succeeds. 506 */ 507 if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO)) 508 == (SC_S_SEL|SC_S_IO)){ 509 if (req != NULL) { 510 req->next = issue_q; 511 issue_q = req; 512 } 513 splx(sps); 514 515 reselect(sc); 516 scsi_clr_ipend(); 517 goto connected; 518 } 519 520 /* 521 * The host is not connected and there is no request 522 * pending, exit. 523 */ 524 if (req == NULL) { 525 PID("scsi_main2"); 526 goto main_exit; 527 } 528 529 /* 530 * Re-enable interrupts before handling the request. 531 */ 532 splx(sps); 533 534 #ifdef DBG_REQ 535 if (dbg_target_mask & (1 << req->targ_id)) 536 show_request(req, "TARGET"); 537 #endif 538 /* 539 * We found a request. Try to connect to the target. If the 540 * initiator fails arbitration, the command is put back in the 541 * issue queue. 542 */ 543 if (scsi_select(req, 0)) { 544 sps = splbio(); 545 req->next = issue_q; 546 issue_q = req; 547 splx(sps); 548 #ifdef DBG_REQ 549 if (dbg_target_mask & (1 << req->targ_id)) 550 ncr_tprint(req, "Select failed\n"); 551 #endif 552 } 553 } 554 else splx(sps); 555 connected: 556 if (connected) { 557 /* 558 * If the host is currently connected but a 'real-DMA' transfer 559 * is in progress, the 'end-of-DMA' interrupt restarts main. 560 * So quit. 561 */ 562 sps = splbio(); 563 if (connected && (connected->dr_flag & DRIVER_IN_DMA)) { 564 PID("scsi_main3"); 565 goto main_exit; 566 } 567 splx(sps); 568 569 /* 570 * Let the target guide us through the bus-phases 571 */ 572 while (information_transfer(sc) == -1) 573 ; 574 } 575 } 576 /* NEVER TO REACH HERE */ 577 show_phase(NULL, 0); /* XXX: Get rid of not used warning */ 578 panic("ncr5380-SCSI: not designed to come here"); 579 580 main_exit: 581 /* 582 * We enter here with interrupts disabled. We are about to exit main 583 * so interrupts should be re-enabled. Because interrupts are edge 584 * triggered, we could already have missed the interrupt. Therefore 585 * we check the IRQ-line here and re-enter when we really missed a 586 * valid interrupt. 587 */ 588 PID("scsi_main4"); 589 scsi_ienable(); 590 591 /* 592 * If we're not currently connected, enable reselection 593 * interrupts. 594 */ 595 if (!connected) 596 SET_5380_REG(NCR5380_IDSTAT, SC_HOST_ID); 597 598 if (scsi_ipending()) { 599 if ((itype = check_intr(sc)) != INTR_SPURIOUS) { 600 scsi_idisable(); 601 scsi_clr_ipend(); 602 splx(sps); 603 604 if (itype == INTR_RESEL) 605 reselect(sc); 606 #ifdef REAL_DMA 607 else dma_ready(); 608 #else 609 else { 610 if (pdma_ready()) 611 goto connected; 612 panic("Got DMA interrupt without DMA"); 613 } 614 #endif 615 goto connected; 616 } 617 } 618 reconsider_dma(); 619 main_running = 0; 620 splx(sps); 621 PID("scsi_main5"); 622 } 623 624 #ifdef REAL_DMA 625 /* 626 * The SCSI-DMA interrupt. 627 * This interrupt can only be triggered when running in non-polled DMA 628 * mode. When DMA is not active, it will be silently ignored, it is usually 629 * to late because the EOP interrupt of the controller happens just a tiny 630 * bit earlier. It might become usefull when scatter/gather is implemented, 631 * because in that case only part of the DATAIN/DATAOUT transfer is taken 632 * out of a single buffer. 633 */ 634 static void 635 ncr_dma_intr(sc) 636 struct ncr_softc *sc; 637 { 638 SC_REQ *reqp; 639 int dma_done; 640 641 PID("ncr_dma_intr"); 642 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) { 643 scsi_idisable(); 644 if (!(dma_done = dma_ready())) { 645 transfer_dma(reqp, reqp->phase, 0); 646 return; 647 } 648 run_main(sc); 649 } 650 } 651 #endif /* REAL_DMA */ 652 653 /* 654 * The SCSI-controller interrupt. This interrupt occurs on reselections and 655 * at the end of non-polled DMA-interrupts. It is assumed to be called from 656 * the machine-dependent hardware interrupt. 657 */ 658 static void 659 ncr_ctrl_intr(sc) 660 struct ncr_softc *sc; 661 { 662 int itype; 663 664 if (main_running) 665 return; /* scsi_main() should handle this one */ 666 667 while (scsi_ipending()) { 668 scsi_idisable(); 669 if ((itype = check_intr(sc)) != INTR_SPURIOUS) { 670 if (itype == INTR_RESEL) 671 reselect(sc); 672 else { 673 #ifdef REAL_DMA 674 int dma_done; 675 if (!(dma_done = dma_ready())) { 676 transfer_dma(connected, connected->phase, 0); 677 return; 678 } 679 #else 680 if (pdma_ready()) 681 return; 682 panic("Got DMA interrupt without DMA"); 683 #endif 684 } 685 scsi_clr_ipend(); 686 } 687 run_main(sc); 688 return; 689 } 690 PID("ncr_ctrl_intr1"); 691 } 692 693 /* 694 * Initiate a connection path between the host and the target. The function 695 * first goes into arbitration for the SCSI-bus. When this succeeds, the target 696 * is selected and an 'IDENTIFY' message is send. 697 * Returns -1 when the arbitration failed. Otherwise 0 is returned. When 698 * the target does not respond (to either selection or 'MESSAGE OUT') the 699 * 'done' function is executed. 700 * The result code given by the driver can be influenced by setting 'code' 701 * to a non-zero value. This is the case when 'select' is called by abort. 702 */ 703 static int 704 scsi_select(reqp, code) 705 SC_REQ *reqp; 706 int code; 707 { 708 u_char tmp[1]; 709 u_char phase; 710 u_long cnt; 711 int sps; 712 u_int8_t atn_flag; 713 u_int8_t targ_bit; 714 struct ncr_softc *sc; 715 716 sc = (void*)reqp->xs->xs_periph->periph_channel->chan_adapter->adapt_dev; 717 DBG_SELPRINT ("Starting arbitration\n", 0); 718 PID("scsi_select1"); 719 720 sps = splbio(); 721 722 /* 723 * Prevent a race condition here. If a reslection interrupt occurred 724 * between the decision to pick a new request and the call to select, 725 * we abort the selection. 726 * Interrupts are lowered when the 5380 is setup to arbitrate for the 727 * bus. 728 */ 729 if (connected) { 730 splx(sps); 731 PID("scsi_select2"); 732 return (-1); 733 } 734 735 /* 736 * Set phase bits to 0, otherwise the 5380 won't drive the bus during 737 * selection. 738 */ 739 SET_5380_REG(NCR5380_TCOM, 0); 740 SET_5380_REG(NCR5380_ICOM, 0); 741 742 /* 743 * Arbitrate for the bus. 744 */ 745 SET_5380_REG(NCR5380_DATA, SC_HOST_ID); 746 SET_5380_REG(NCR5380_MODE, SC_ARBIT); 747 748 splx(sps); 749 750 cnt = 10; 751 while (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP) && --cnt) 752 delay(1); 753 754 if (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP)) { 755 SET_5380_REG(NCR5380_MODE, IMODE_BASE); 756 SET_5380_REG(NCR5380_ICOM, 0); 757 DBG_SELPRINT ("Arbitration lost, bus not free\n",0); 758 PID("scsi_select3"); 759 return (-1); 760 } 761 762 /* The arbitration delay is 2.2 usecs */ 763 delay(3); 764 765 /* 766 * Check the result of the arbitration. If we failed, return -1. 767 */ 768 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) { 769 SET_5380_REG(NCR5380_MODE, IMODE_BASE); 770 SET_5380_REG(NCR5380_ICOM, 0); 771 PID("scsi_select4"); 772 return (-1); 773 } 774 775 /* 776 * The spec requires that we should read the data register to 777 * check for higher id's and check the SC_LA again. 778 */ 779 tmp[0] = GET_5380_REG(NCR5380_DATA); 780 if (tmp[0] & ~((SC_HOST_ID << 1) - 1)) { 781 SET_5380_REG(NCR5380_MODE, IMODE_BASE); 782 SET_5380_REG(NCR5380_ICOM, 0); 783 DBG_SELPRINT ("Arbitration lost, higher id present\n",0); 784 PID("scsi_select5"); 785 return (-1); 786 } 787 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) { 788 SET_5380_REG(NCR5380_MODE, IMODE_BASE); 789 SET_5380_REG(NCR5380_ICOM, 0); 790 DBG_SELPRINT ("Arbitration lost,deassert SC_ARBIT\n",0); 791 PID("scsi_select6"); 792 return (-1); 793 } 794 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_BSY); 795 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) { 796 SET_5380_REG(NCR5380_MODE, IMODE_BASE); 797 SET_5380_REG(NCR5380_ICOM, 0); 798 DBG_SELPRINT ("Arbitration lost, deassert SC_A_SEL\n", 0); 799 PID("scsi_select7"); 800 return (-1); 801 } 802 /* Bus settle delay + Bus clear delay = 1.2 usecs */ 803 delay(2); 804 DBG_SELPRINT ("Arbitration complete\n", 0); 805 806 /* 807 * Now that we won the arbitration, start the selection. 808 */ 809 targ_bit = 1 << reqp->targ_id; 810 SET_5380_REG(NCR5380_DATA, SC_HOST_ID | targ_bit); 811 812 if (sc->sc_noselatn & targ_bit) 813 atn_flag = 0; 814 else 815 atn_flag = SC_A_ATN; 816 817 /* 818 * Raise ATN while SEL is true before BSY goes false from arbitration, 819 * since this is the only way to guarantee that we'll get a MESSAGE OUT 820 * phase immediately after the selection. 821 */ 822 SET_5380_REG(NCR5380_ICOM, SC_A_BSY | SC_A_SEL | atn_flag | SC_ADTB); 823 SET_5380_REG(NCR5380_MODE, IMODE_BASE); 824 825 /* 826 * Turn off reselection interrupts 827 */ 828 SET_5380_REG(NCR5380_IDSTAT, 0); 829 830 /* 831 * Reset BSY. The delay following it, surpresses a glitch in the 832 * 5380 which causes us to see our own BSY signal instead of that of 833 * the target. 834 */ 835 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag | SC_ADTB); 836 delay(1); 837 838 /* 839 * Wait for the target to react, the specs call for a timeout of 840 * 250 ms. 841 */ 842 cnt = 25000; 843 while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) && --cnt) 844 delay(10); 845 846 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) { 847 /* 848 * There is no reaction from the target, start the selection 849 * timeout procedure. We release the databus but keep SEL 850 * asserted. After that we wait a 'selection abort time' (200 851 * usecs) and 2 deskew delays (90 ns) and check BSY again. 852 * When BSY is asserted, we assume the selection succeeded, 853 * otherwise we release the bus. 854 */ 855 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag); 856 delay(201); 857 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) { 858 SET_5380_REG(NCR5380_ICOM, 0); 859 reqp->xs->error = code ? code : XS_SELTIMEOUT; 860 DBG_SELPRINT ("Target %d not responding to sel\n", 861 reqp->targ_id); 862 if (reqp->dr_flag & DRIVER_LINKCHK) 863 ncr_test_link &= ~(1<<reqp->targ_id); 864 finish_req(reqp); 865 PID("scsi_select8"); 866 return (0); 867 } 868 } 869 SET_5380_REG(NCR5380_ICOM, atn_flag); 870 871 DBG_SELPRINT ("Target %d responding to select.\n", reqp->targ_id); 872 873 /* 874 * The SCSI-interrupts are disabled while a request is being handled. 875 */ 876 scsi_idisable(); 877 878 /* 879 * If we did not request ATN, then don't try to send IDENTIFY. 880 */ 881 if (atn_flag == 0) { 882 reqp->phase = PH_CMD; 883 goto identify_failed; 884 } 885 886 /* 887 * Here we prepare to send an 'IDENTIFY' message. 888 * Allow disconnect only when interrups are allowed. 889 */ 890 tmp[0] = MSG_IDENTIFY(reqp->targ_lun, 891 (reqp->dr_flag & DRIVER_NOINT) ? 0 : 1); 892 cnt = 1; 893 phase = PH_MSGOUT; 894 895 /* 896 * Since we followed the SCSI-spec and raised ATN while SEL was true 897 * but before BSY was false during the selection, a 'MESSAGE OUT' 898 * phase should follow. Unfortunately, this does not happen on 899 * all targets (Asante ethernet devices, for example), so we must 900 * check the actual mode if the message transfer fails--if the 901 * new phase is PH_CMD and has never been successfully selected 902 * w/ATN in the past, then we assume that it is an old device 903 * that doesn't support select w/ATN. 904 */ 905 if (transfer_pio(&phase, tmp, &cnt, 0) || cnt) { 906 907 if ((phase == PH_CMD) && !(sc->sc_selected & targ_bit)) { 908 DBG_SELPRINT ("Target %d: not responding to ATN.\n", 909 reqp->targ_id); 910 sc->sc_noselatn |= targ_bit; 911 reqp->phase = PH_CMD; 912 goto identify_failed; 913 } 914 915 DBG_SELPRINT ("Target %d: failed to send identify\n", 916 reqp->targ_id); 917 /* 918 * Try to disconnect from the target. We cannot leave 919 * it just hanging here. 920 */ 921 if (!reach_msg_out(sc, sizeof(struct scsipi_generic))) { 922 u_long len = 1; 923 u_char phase = PH_MSGOUT; 924 u_char msg = MSG_ABORT; 925 926 transfer_pio(&phase, &msg, &len, 0); 927 } 928 else scsi_reset_verbose(sc, "Connected to unidentified target"); 929 930 SET_5380_REG(NCR5380_ICOM, 0); 931 reqp->xs->error = code ? code : XS_DRIVER_STUFFUP; 932 finish_req(reqp); 933 PID("scsi_select9"); 934 return (0); 935 } 936 reqp->phase = PH_MSGOUT; 937 938 identify_failed: 939 sc->sc_selected |= targ_bit; 940 941 #ifdef notyet /* LWP: Do we need timeouts in the driver? */ 942 /* 943 * Command is connected, start timer ticking. 944 */ 945 ccb_p->xtimeout = ccb_p->timeout + Lbolt; 946 #endif 947 948 connected = reqp; 949 busy |= targ_bit; 950 PID("scsi_select10"); 951 return (0); 952 } 953 954 /* 955 * Return codes: 956 * 0: Job has finished or disconnected, find something else 957 * -1: keep on calling information_transfer() from scsi_main() 958 */ 959 static int 960 information_transfer(sc) 961 struct ncr_softc *sc; 962 { 963 SC_REQ *reqp = connected; 964 u_char tmp, phase; 965 u_long len; 966 967 PID("info_transf1"); 968 /* 969 * Clear pending interrupts from 5380-chip. 970 */ 971 scsi_clr_ipend(); 972 973 /* 974 * The SCSI-spec requires BSY to be true while connected to a target, 975 * loosing it means we lost the target... 976 * Also REQ needs to be asserted here to indicate that the bus-phase 977 * is valid. When the target does not supply REQ within a 'reasonable' 978 * amount of time, it's probably lost in it's own maze of twisting 979 * passages, we have to reset the bus to free it. 980 */ 981 if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) 982 wait_req_true(); 983 tmp = GET_5380_REG(NCR5380_IDSTAT); 984 985 986 if ((tmp & (SC_S_BSY|SC_S_REQ)) != (SC_S_BSY|SC_S_REQ)) { 987 busy &= ~(1 << reqp->targ_id); 988 connected = NULL; 989 reqp->xs->error = XS_TIMEOUT; 990 finish_req(reqp); 991 if (!(tmp & SC_S_REQ)) 992 scsi_reset_verbose(sc, 993 "Timeout waiting for phase-change"); 994 PID("info_transf2"); 995 return (0); 996 } 997 998 phase = (tmp >> 2) & 7; 999 if (phase != reqp->phase) { 1000 reqp->phase = phase; 1001 #ifdef DBG_INF 1002 if (dbg_target_mask & (1 << reqp->targ_id)) 1003 DBG_INFPRINT(show_phase, reqp, phase); 1004 #endif 1005 } 1006 else { 1007 /* 1008 * Same data-phase. If same error give up 1009 */ 1010 if ((reqp->msgout == MSG_ABORT) 1011 && ((phase == PH_DATAOUT) || (phase == PH_DATAIN))) { 1012 busy &= ~(1 << reqp->targ_id); 1013 connected = NULL; 1014 reqp->xs->error = XS_TIMEOUT; 1015 finish_req(reqp); 1016 scsi_reset_verbose(sc, "Failure to abort command"); 1017 return (0); 1018 } 1019 } 1020 1021 switch (phase) { 1022 case PH_DATAOUT: 1023 #ifdef DBG_NOWRITE 1024 ncr_tprint(reqp, "NOWRITE set -- write attempt aborted."); 1025 reqp->msgout = MSG_ABORT; 1026 SET_5380_REG(NCR5380_ICOM, SC_A_ATN); 1027 return (-1); 1028 #endif /* DBG_NOWRITE */ 1029 /* 1030 * If this is the first write using DMA, fill 1031 * the bounce buffer. 1032 */ 1033 if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */ 1034 if (reqp->dr_flag & DRIVER_BOUNCING) 1035 bcopy(reqp->xdata_ptr, reqp->bounceb, reqp->xdata_len); 1036 } 1037 1038 case PH_DATAIN: 1039 if (reqp->xdata_len <= 0) { 1040 /* 1041 * Target keeps requesting data. Try to get into 1042 * message-out phase by feeding/taking 100 byte. 1043 */ 1044 ncr_tprint(reqp, "Target requests too much data\n"); 1045 reqp->msgout = MSG_ABORT; 1046 SET_5380_REG(NCR5380_ICOM, SC_A_ATN); 1047 reach_msg_out(sc, 100); 1048 return (-1); 1049 } 1050 #ifdef REAL_DMA 1051 if (reqp->dr_flag & DRIVER_DMAOK) { 1052 int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT); 1053 transfer_dma(reqp, phase, poll); 1054 if (!poll) 1055 return (0); 1056 } 1057 else 1058 #endif 1059 { 1060 PID("info_transf3"); 1061 len = reqp->xdata_len; 1062 #ifdef USE_PDMA 1063 if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0) 1064 return (0); 1065 #else 1066 transfer_pio(&phase, reqp->xdata_ptr, &len, 0); 1067 #endif 1068 reqp->xdata_ptr += reqp->xdata_len - len; 1069 reqp->xdata_len = len; 1070 } 1071 return (-1); 1072 case PH_MSGIN: 1073 /* 1074 * We only expect single byte messages here. 1075 */ 1076 len = 1; 1077 transfer_pio(&phase, &tmp, &len, 1); 1078 reqp->message = tmp; 1079 return (handle_message(reqp, tmp)); 1080 case PH_MSGOUT: 1081 len = 1; 1082 transfer_pio(&phase, &reqp->msgout, &len, 0); 1083 if (reqp->msgout == MSG_ABORT) { 1084 busy &= ~(1 << reqp->targ_id); 1085 connected = NULL; 1086 if (!reqp->xs->error) 1087 reqp->xs->error = XS_DRIVER_STUFFUP; 1088 finish_req(reqp); 1089 PID("info_transf4"); 1090 return (0); 1091 } 1092 reqp->msgout = MSG_NOOP; 1093 return (-1); 1094 case PH_CMD : 1095 len = reqp->xcmd_len; 1096 transfer_pio(&phase, (u_char *)&reqp->xcmd, &len, 0); 1097 PID("info_transf5"); 1098 return (-1); 1099 case PH_STATUS: 1100 len = 1; 1101 transfer_pio(&phase, &tmp, &len, 0); 1102 reqp->status = tmp; 1103 PID("info_transf6"); 1104 return (-1); 1105 default : 1106 ncr_tprint(reqp, "Unknown phase\n"); 1107 } 1108 PID("info_transf7"); 1109 return (-1); 1110 } 1111 1112 /* 1113 * Handle the message 'msg' send to us by the target. 1114 * Return values: 1115 * 0 : The current command has completed. 1116 * -1 : Get on to the next phase. 1117 */ 1118 static int 1119 handle_message(reqp, msg) 1120 SC_REQ *reqp; 1121 u_int msg; 1122 { 1123 int sps; 1124 SC_REQ *prev, *req; 1125 1126 PID("hmessage1"); 1127 switch (msg) { 1128 /* 1129 * Linking lets us reduce the time required to get 1130 * the next command to the device, skipping the arbitration 1131 * and selection time. In the current implementation, 1132 * we merely have to start the next command pointed 1133 * to by 'next_link'. 1134 */ 1135 case MSG_LINK_CMD_COMPLETE: 1136 case MSG_LINK_CMD_COMPLETEF: 1137 if (reqp->link == NULL) { 1138 ncr_tprint(reqp, "No link for linked command"); 1139 nack_message(reqp, MSG_ABORT); 1140 PID("hmessage2"); 1141 return (-1); 1142 } 1143 ack_message(); 1144 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) { 1145 reqp->xs->resid = reqp->xdata_len; 1146 reqp->xs->error = 0; 1147 } 1148 1149 #ifdef AUTO_SENSE 1150 if (check_autosense(reqp, 1) == -1) 1151 return (-1); 1152 #endif /* AUTO_SENSE */ 1153 1154 #ifdef DBG_REQ 1155 if (dbg_target_mask & (1 << reqp->targ_id)) 1156 show_request(reqp->link, "LINK"); 1157 #endif 1158 connected = reqp->link; 1159 1160 /* 1161 * Unlink the 'linked' request from the issue_q 1162 */ 1163 sps = splbio(); 1164 prev = NULL; 1165 req = issue_q; 1166 for (; req != NULL; prev = req, req = req->next) { 1167 if (req == connected) 1168 break; 1169 } 1170 if (req == NULL) 1171 panic("Inconsistent issue_q"); 1172 if (prev == NULL) 1173 issue_q = req->next; 1174 else prev->next = req->next; 1175 req->next = NULL; 1176 splx(sps); 1177 1178 finish_req(reqp); 1179 PID("hmessage3"); 1180 return (-1); 1181 case MSG_ABORT: 1182 case MSG_CMDCOMPLETE: 1183 ack_message(); 1184 connected = NULL; 1185 busy &= ~(1 << reqp->targ_id); 1186 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) { 1187 reqp->xs->resid = reqp->xdata_len; 1188 reqp->xs->error = 0; 1189 } 1190 1191 #ifdef AUTO_SENSE 1192 if (check_autosense(reqp, 0) == -1) { 1193 PID("hmessage4"); 1194 return (0); 1195 } 1196 #endif /* AUTO_SENSE */ 1197 1198 finish_req(reqp); 1199 PID("hmessage5"); 1200 return (0); 1201 case MSG_MESSAGE_REJECT: 1202 ack_message(); 1203 PID("hmessage6"); 1204 return (-1); 1205 case MSG_DISCONNECT: 1206 ack_message(); 1207 #ifdef DBG_REQ 1208 if (dbg_target_mask & (1 << reqp->targ_id)) 1209 show_request(reqp, "DISCON"); 1210 #endif 1211 sps = splbio(); 1212 connected = NULL; 1213 reqp->next = discon_q; 1214 discon_q = reqp; 1215 splx(sps); 1216 PID("hmessage7"); 1217 return (0); 1218 case MSG_SAVEDATAPOINTER: 1219 case MSG_RESTOREPOINTERS: 1220 /* 1221 * We save pointers implicitely at disconnect. 1222 * So we can ignore these messages. 1223 */ 1224 ack_message(); 1225 PID("hmessage8"); 1226 return (-1); 1227 case MSG_EXTENDED: 1228 nack_message(reqp, MSG_MESSAGE_REJECT); 1229 PID("hmessage9"); 1230 return (-1); 1231 default: 1232 if ((msg & 0x80) && !(msg & 0x18)) { /* IDENTIFY */ 1233 PID("hmessage10"); 1234 ack_message(); 1235 return (0); 1236 } else { 1237 ncr_tprint(reqp, 1238 "Unknown message %x. Rejecting.\n", 1239 msg); 1240 nack_message(reqp, MSG_MESSAGE_REJECT); 1241 } 1242 return (-1); 1243 } 1244 PID("hmessage11"); 1245 return (-1); 1246 } 1247 1248 /* 1249 * Handle reselection. If a valid reconnection occurs, connected 1250 * points at the reconnected command. The command is removed from the 1251 * disconnected queue. 1252 */ 1253 static void 1254 reselect(sc) 1255 struct ncr_softc *sc; 1256 { 1257 u_char phase; 1258 u_long len; 1259 u_char msg; 1260 u_char target_mask; 1261 int abort = 0; 1262 SC_REQ *tmp, *prev; 1263 1264 PID("reselect1"); 1265 target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID; 1266 1267 /* 1268 * At this point, we have detected that our SCSI-id is on the bus, 1269 * SEL is true and BSY was false for at least one bus settle 1270 * delay (400 ns.). 1271 * We must assert BSY ourselves, until the target drops the SEL signal. 1272 * The SCSI-spec specifies no maximum time for this, so we have to 1273 * choose something long enough to suit all targets. 1274 */ 1275 SET_5380_REG(NCR5380_ICOM, SC_A_BSY); 1276 len = 250000; 1277 while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) { 1278 delay(1); 1279 len--; 1280 } 1281 if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) { 1282 /* Damn SEL isn't dropping */ 1283 scsi_reset_verbose(sc, "Target won't drop SEL during Reselect"); 1284 return; 1285 } 1286 1287 SET_5380_REG(NCR5380_ICOM, 0); 1288 1289 /* 1290 * Check if the reselection is still valid. Check twice because 1291 * of possible line glitches - cheaper than delay(1) and we need 1292 * only a few nanoseconds. 1293 */ 1294 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) { 1295 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) { 1296 ncr_aprint(sc, "Stepped into the reselection timeout\n"); 1297 return; 1298 } 1299 } 1300 1301 /* 1302 * Get the expected identify message. 1303 */ 1304 phase = PH_MSGIN; 1305 len = 1; 1306 transfer_pio(&phase, &msg, &len, 0); 1307 if (len || !MSG_ISIDENTIFY(msg)) { 1308 ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg); 1309 abort = 1; 1310 tmp = NULL; 1311 } 1312 else { 1313 /* 1314 * Find the command reconnecting 1315 */ 1316 for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){ 1317 if (target_mask == (1 << tmp->targ_id)) { 1318 if (prev) 1319 prev->next = tmp->next; 1320 else discon_q = tmp->next; 1321 tmp->next = NULL; 1322 break; 1323 } 1324 } 1325 if (tmp == NULL) { 1326 ncr_aprint(sc, "No disconnected job for targetmask %x\n", 1327 target_mask); 1328 abort = 1; 1329 } 1330 } 1331 if (abort) { 1332 msg = MSG_ABORT; 1333 len = 1; 1334 phase = PH_MSGOUT; 1335 1336 SET_5380_REG(NCR5380_ICOM, SC_A_ATN); 1337 if (transfer_pio(&phase, &msg, &len, 0) || len) 1338 scsi_reset_verbose(sc, "Failure to abort reselection"); 1339 } 1340 else { 1341 connected = tmp; 1342 #ifdef DBG_REQ 1343 if (dbg_target_mask & (1 << tmp->targ_id)) 1344 show_request(tmp, "RECON"); 1345 #endif 1346 } 1347 PID("reselect2"); 1348 } 1349 1350 /* 1351 * Transfer data in a given phase using programmed I/O. 1352 * Returns -1 when a different phase is entered without transferring the 1353 * maximum number of bytes, 0 if all bytes transferred or exit is in the same 1354 * phase. 1355 */ 1356 static int 1357 transfer_pio(phase, data, len, dont_drop_ack) 1358 u_char *phase; 1359 u_char *data; 1360 u_long *len; 1361 int dont_drop_ack; 1362 { 1363 u_int cnt = *len; 1364 u_char ph = *phase; 1365 u_char tmp, new_icom; 1366 1367 DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt); 1368 PID("tpio1"); 1369 SET_5380_REG(NCR5380_TCOM, ph); 1370 do { 1371 if (!wait_req_true()) { 1372 DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0); 1373 break; 1374 } 1375 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) { 1376 DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0); 1377 break; 1378 } 1379 if (PH_IN(ph)) { 1380 *data++ = GET_5380_REG(NCR5380_DATA); 1381 SET_5380_REG(NCR5380_ICOM, SC_A_ACK); 1382 if ((cnt == 1) && dont_drop_ack) 1383 new_icom = SC_A_ACK; 1384 else new_icom = 0; 1385 } 1386 else { 1387 SET_5380_REG(NCR5380_DATA, *data++); 1388 1389 /* 1390 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase, 1391 * the initiator should drop ATN on the last byte of the 1392 * message phase after REQ has been asserted for the handshake 1393 * but before the initiator raises ACK. 1394 */ 1395 if (!( (ph == PH_MSGOUT) && (cnt > 1) )) { 1396 SET_5380_REG(NCR5380_ICOM, SC_ADTB); 1397 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK); 1398 new_icom = 0; 1399 } 1400 else { 1401 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN); 1402 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK); 1403 new_icom = SC_A_ATN; 1404 } 1405 } 1406 if (!wait_req_false()) { 1407 DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0); 1408 break; 1409 } 1410 SET_5380_REG(NCR5380_ICOM, new_icom); 1411 1412 } while (--cnt); 1413 1414 if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) 1415 *phase = (tmp >> 2) & 7; 1416 else *phase = NR_PHASE; 1417 *len = cnt; 1418 DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n", 1419 *phase, cnt); 1420 PID("tpio2"); 1421 if (!cnt || (*phase == ph)) 1422 return (0); 1423 return (-1); 1424 } 1425 1426 #ifdef REAL_DMA 1427 1428 /* 1429 * Start a DMA-transfer on the device using the current pointers. 1430 * If 'poll' is true, the function busy-waits until DMA has completed. 1431 */ 1432 static void 1433 transfer_dma(reqp, phase, poll) 1434 SC_REQ *reqp; 1435 u_int phase; 1436 int poll; 1437 { 1438 int dma_done; 1439 u_char mbase = 0; 1440 int sps; 1441 1442 again: 1443 PID("tdma1"); 1444 1445 /* 1446 * We should be in phase, otherwise we are not allowed to 1447 * drive the bus. 1448 */ 1449 SET_5380_REG(NCR5380_TCOM, phase); 1450 1451 /* 1452 * Defer interrupts until DMA is fully running. 1453 */ 1454 sps = splbio(); 1455 1456 /* 1457 * Clear pending interrupts and parity errors. 1458 */ 1459 scsi_clr_ipend(); 1460 1461 if (!poll) { 1462 /* 1463 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase' 1464 * to the interrupts we want enabled. 1465 */ 1466 scsi_ienable(); 1467 reqp->dr_flag |= DRIVER_IN_DMA; 1468 mbase = SC_E_EOPI | SC_MON_BSY; 1469 } 1470 else scsi_idisable(); 1471 mbase |= IMODE_BASE | SC_M_DMA; 1472 scsi_dma_setup(reqp, phase, mbase); 1473 1474 splx(sps); 1475 1476 if (poll) { 1477 /* 1478 * On polled-DMA transfers, we wait here until the 1479 * 'end-of-DMA' condition occurs. 1480 */ 1481 poll_edma(reqp); 1482 if (!(dma_done = dma_ready())) 1483 goto again; 1484 } 1485 PID("tdma2"); 1486 } 1487 1488 /* 1489 * Check results of a DMA data-transfer. 1490 */ 1491 static int 1492 dma_ready() 1493 { 1494 SC_REQ *reqp = connected; 1495 int dmstat, is_edma; 1496 long bytes_left, bytes_done; 1497 1498 is_edma = get_dma_result(reqp, &bytes_left); 1499 dmstat = GET_5380_REG(NCR5380_DMSTAT); 1500 1501 /* 1502 * Check if the call is sensible and not caused by any spurious 1503 * interrupt. 1504 */ 1505 if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR)) 1506 && (dmstat & SC_PHS_MTCH) ) { 1507 ncr_tprint(reqp, "dma_ready: spurious call " 1508 "(dm:%x,last_hit: %s)\n", 1509 #ifdef DBG_PID 1510 dmstat, last_hit[DBG_PID-1]); 1511 #else 1512 dmstat, "unknown"); 1513 #endif 1514 return (0); 1515 } 1516 1517 /* 1518 * Clear all (pending) interrupts. 1519 */ 1520 scsi_clr_ipend(); 1521 1522 /* 1523 * Update various transfer-pointers/lengths 1524 */ 1525 bytes_done = reqp->dm_cur->dm_count - bytes_left; 1526 1527 if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) { 1528 /* 1529 * Copy the bytes read until now from the bounce buffer 1530 * to the 'real' destination. Flush the data-cache 1531 * before copying. 1532 */ 1533 PCIA(); 1534 bcopy(reqp->bouncerp, reqp->xdata_ptr, bytes_done); 1535 reqp->bouncerp += bytes_done; 1536 } 1537 1538 reqp->xdata_ptr = &reqp->xdata_ptr[bytes_done]; /* XXX */ 1539 reqp->xdata_len -= bytes_done; /* XXX */ 1540 if ((reqp->dm_cur->dm_count -= bytes_done) == 0) 1541 reqp->dm_cur++; 1542 else reqp->dm_cur->dm_addr += bytes_done; 1543 1544 if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) { 1545 if (!(ncr5380_no_parchk & (1 << reqp->targ_id))) { 1546 ncr_tprint(reqp, "parity error in data-phase\n"); 1547 reqp->xs->error = XS_TIMEOUT; 1548 } 1549 } 1550 1551 /* 1552 * DMA mode should always be reset even when we will continue with the 1553 * next chain. It is also essential to clear the MON_BUSY because 1554 * when LOST_BUSY is unexpectedly set, we will not be able to drive 1555 * the bus.... 1556 */ 1557 SET_5380_REG(NCR5380_MODE, IMODE_BASE); 1558 1559 1560 if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH) 1561 || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) { 1562 1563 /* 1564 * Tell interrupt functions DMA mode has ended. 1565 */ 1566 reqp->dr_flag &= ~DRIVER_IN_DMA; 1567 1568 /* 1569 * Clear mode and icom 1570 */ 1571 SET_5380_REG(NCR5380_MODE, IMODE_BASE); 1572 SET_5380_REG(NCR5380_ICOM, 0); 1573 1574 if (dmstat & SC_BSY_ERR) { 1575 if (!reqp->xs->error) 1576 reqp->xs->error = XS_TIMEOUT; 1577 finish_req(reqp); 1578 PID("dma_ready1"); 1579 return (1); 1580 } 1581 1582 if (reqp->xs->error != 0) { 1583 ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */ 1584 reqp->msgout = MSG_ABORT; 1585 SET_5380_REG(NCR5380_ICOM, SC_A_ATN); 1586 } 1587 PID("dma_ready2"); 1588 return (1); 1589 } 1590 return (0); 1591 } 1592 #endif /* REAL_DMA */ 1593 1594 static int 1595 check_autosense(reqp, linked) 1596 SC_REQ *reqp; 1597 int linked; 1598 { 1599 int sps; 1600 1601 /* 1602 * If this is the driver's Link Check for this target, ignore 1603 * the results of the command. All we care about is whether we 1604 * got here from a LINK_CMD_COMPLETE or CMD_COMPLETE message. 1605 */ 1606 PID("linkcheck"); 1607 if (reqp->dr_flag & DRIVER_LINKCHK) { 1608 if (linked) 1609 ncr_will_link |= 1<<reqp->targ_id; 1610 else ncr_tprint(reqp, "Does not support linked commands\n"); 1611 return (0); 1612 } 1613 /* 1614 * If we not executing an auto-sense and the status code 1615 * is request-sense, we automatically issue a request 1616 * sense command. 1617 */ 1618 PID("cautos1"); 1619 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) { 1620 switch (reqp->status & SCSMASK) { 1621 case SCSCHKC: 1622 bcopy(sense_cmd, &reqp->xcmd, sizeof(sense_cmd)); 1623 reqp->xcmd_len = sizeof(sense_cmd); 1624 reqp->xdata_ptr = (u_char *)&reqp->xs->sense.scsi_sense; 1625 reqp->xdata_len = sizeof(reqp->xs->sense.scsi_sense); 1626 reqp->dr_flag |= DRIVER_AUTOSEN; 1627 reqp->dr_flag &= ~DRIVER_DMAOK; 1628 if (!linked) { 1629 sps = splbio(); 1630 reqp->next = issue_q; 1631 issue_q = reqp; 1632 splx(sps); 1633 } 1634 else reqp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1; 1635 1636 #ifdef DBG_REQ 1637 bzero(reqp->xdata_ptr, reqp->xdata_len); 1638 if (dbg_target_mask & (1 << reqp->targ_id)) 1639 show_request(reqp, "AUTO-SENSE"); 1640 #endif 1641 PID("cautos2"); 1642 return (-1); 1643 case SCSBUSY: 1644 reqp->xs->error = XS_BUSY; 1645 return (0); 1646 } 1647 } 1648 else { 1649 /* 1650 * An auto-sense has finished 1651 */ 1652 if ((reqp->status & SCSMASK) != SCSGOOD) 1653 reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */ 1654 else reqp->xs->error = XS_SENSE; 1655 reqp->status = SCSCHKC; 1656 } 1657 PID("cautos3"); 1658 return (0); 1659 } 1660 1661 static int 1662 reach_msg_out(sc, len) 1663 struct ncr_softc *sc; 1664 u_long len; 1665 { 1666 u_char phase; 1667 u_char data; 1668 u_long n = len; 1669 1670 ncr_aprint(sc, "Trying to reach Message-out phase\n"); 1671 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) 1672 phase = (phase >> 2) & 7; 1673 else return (-1); 1674 ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase); 1675 if (phase == PH_MSGOUT) 1676 return (0); 1677 1678 SET_5380_REG(NCR5380_TCOM, phase); 1679 1680 do { 1681 if (!wait_req_true()) 1682 break; 1683 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase) 1684 break; 1685 if (PH_IN(phase)) { 1686 data = GET_5380_REG(NCR5380_DATA); 1687 SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN); 1688 } 1689 else { 1690 SET_5380_REG(NCR5380_DATA, 0); 1691 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN); 1692 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN); 1693 } 1694 if (!wait_req_false()) 1695 break; 1696 SET_5380_REG(NCR5380_ICOM, SC_A_ATN); 1697 } while (--n); 1698 1699 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) { 1700 phase = (phase >> 2) & 7; 1701 if (phase == PH_MSGOUT) { 1702 ncr_aprint(sc, "Message-out phase reached after " 1703 "%ld bytes.\n", len - n); 1704 return (0); 1705 } 1706 ncr_aprint(sc, "Phase now: %d after %ld bytes.\n", phase, 1707 len - n); 1708 1709 } 1710 return (-1); 1711 } 1712 1713 void 1714 scsi_reset() 1715 { 1716 SC_REQ *tmp, *next; 1717 int sps; 1718 1719 PID("scsi_reset1"); 1720 sps = splbio(); 1721 SET_5380_REG(NCR5380_ICOM, SC_A_RST); 1722 delay(100); 1723 SET_5380_REG(NCR5380_ICOM, 0); 1724 scsi_clr_ipend(); 1725 1726 /* 1727 * None of the jobs in the discon_q will ever be reconnected, 1728 * notify this to the higher level code. 1729 */ 1730 for (tmp = discon_q; tmp ;) { 1731 next = tmp->next; 1732 tmp->next = NULL; 1733 tmp->xs->error = XS_TIMEOUT; 1734 busy &= ~(1 << tmp->targ_id); 1735 finish_req(tmp); 1736 tmp = next; 1737 } 1738 discon_q = NULL; 1739 1740 /* 1741 * The current job will never finish either. 1742 * The problem is that we can't finish the job because an instance 1743 * of main is running on it. Our best guess is that the job is currently 1744 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish 1745 * the job because it detects BSY-loss. 1746 */ 1747 if ((tmp = connected) != NULL) { 1748 if (tmp->dr_flag & DRIVER_IN_DMA) { 1749 tmp->xs->error = XS_DRIVER_STUFFUP; 1750 #ifdef REAL_DMA 1751 dma_ready(); 1752 #endif 1753 } 1754 } 1755 splx(sps); 1756 PID("scsi_reset2"); 1757 1758 /* 1759 * Give the attached devices some time to handle the reset. This 1760 * value is arbitrary but should be relatively long. 1761 */ 1762 delay(100000); 1763 } 1764 1765 static void 1766 scsi_reset_verbose(sc, why) 1767 struct ncr_softc *sc; 1768 const char *why; 1769 { 1770 ncr_aprint(sc, "Resetting SCSI-bus (%s)\n", why); 1771 1772 scsi_reset(); 1773 } 1774 1775 /* 1776 * Check validity of the IRQ set by the 5380. If the interrupt is valid, 1777 * the appropriate action is carried out (reselection or DMA ready) and 1778 * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written 1779 * and INTR_SPURIOUS is returned. 1780 */ 1781 static int 1782 check_intr(sc) 1783 struct ncr_softc *sc; 1784 { 1785 SC_REQ *reqp; 1786 1787 if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO)) 1788 ==(SC_S_SEL|SC_S_IO)) 1789 return (INTR_RESEL); 1790 else { 1791 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){ 1792 reqp->dr_flag &= ~DRIVER_IN_DMA; 1793 return (INTR_DMA); 1794 } 1795 } 1796 printf("-->"); 1797 scsi_show(); 1798 scsi_clr_ipend(); 1799 ncr_aprint(sc, "Spurious interrupt.\n"); 1800 return (INTR_SPURIOUS); 1801 } 1802 1803 #ifdef REAL_DMA 1804 /* 1805 * Check if DMA can be used for this request. This function also builds 1806 * the DMA-chain. 1807 */ 1808 static int 1809 scsi_dmaok(reqp) 1810 SC_REQ *reqp; 1811 { 1812 u_long phy_buf; 1813 u_long phy_len; 1814 caddr_t req_addr; 1815 u_long req_len; 1816 struct dma_chain *dm; 1817 1818 /* 1819 * Initialize locals and requests' DMA-chain. 1820 */ 1821 req_len = reqp->xdata_len; 1822 req_addr = (caddr_t)reqp->xdata_ptr; 1823 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain; 1824 dm->dm_count = dm->dm_addr = 0; 1825 reqp->dr_flag &= ~DRIVER_BOUNCING; 1826 1827 /* 1828 * Do not accept zero length DMA. 1829 */ 1830 if (req_len == 0) 1831 return (0); 1832 1833 /* 1834 * If DMA is emulated in software, we don't have to breakup the 1835 * request. Just build a chain with a single element and stash in 1836 * the KVA and not the KPA. 1837 */ 1838 if (emulated_dma()) { 1839 dm->dm_addr = (u_long)req_addr; 1840 dm->dm_count = req_len; 1841 return (1); 1842 } 1843 1844 /* 1845 * LWP: I think that this restriction is not strictly nessecary. 1846 */ 1847 if ((req_len & 0x1) || ((u_int)req_addr & 0x3)) 1848 return (0); 1849 1850 /* 1851 * Build the DMA-chain. 1852 */ 1853 dm->dm_addr = phy_buf = kvtop(req_addr); 1854 while (req_len) { 1855 if (req_len < 1856 (phy_len = PAGE_SIZE - ((u_long)req_addr & PGOFSET))) 1857 phy_len = req_len; 1858 1859 req_addr += phy_len; 1860 req_len -= phy_len; 1861 dm->dm_count += phy_len; 1862 1863 if (req_len) { 1864 u_long tmp = kvtop(req_addr); 1865 1866 if ((phy_buf + phy_len) != tmp) { 1867 if (wrong_dma_range(reqp, dm)) { 1868 if (reqp->dr_flag & DRIVER_BOUNCING) 1869 goto bounceit; 1870 return (0); 1871 } 1872 1873 if (++dm >= &reqp->dm_chain[MAXDMAIO]) { 1874 ncr_tprint(reqp,"dmaok: DMA chain too long!\n"); 1875 return (0); 1876 } 1877 dm->dm_count = 0; 1878 dm->dm_addr = tmp; 1879 } 1880 phy_buf = tmp; 1881 } 1882 } 1883 if (wrong_dma_range(reqp, dm)) { 1884 if (reqp->dr_flag & DRIVER_BOUNCING) 1885 goto bounceit; 1886 return (0); 1887 } 1888 reqp->dm_last = dm; 1889 return (1); 1890 1891 bounceit: 1892 if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) { 1893 /* 1894 * If we can't get a bounce buffer, forget DMA 1895 */ 1896 reqp->dr_flag &= ~DRIVER_BOUNCING; 1897 return(0); 1898 } 1899 /* 1900 * Initialize a single DMA-range containing the bounced request 1901 */ 1902 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain; 1903 dm->dm_addr = kvtop(reqp->bounceb); 1904 dm->dm_count = reqp->xdata_len; 1905 reqp->bouncerp = reqp->bounceb; 1906 1907 return (1); 1908 } 1909 #endif /* REAL_DMA */ 1910 1911 static void 1912 run_main(sc) 1913 struct ncr_softc *sc; 1914 { 1915 int sps = splbio(); 1916 1917 if (!main_running) { 1918 /* 1919 * If shared resources are required, claim them 1920 * before entering 'scsi_main'. If we can't get them 1921 * now, assume 'run_main' will be called when the resource 1922 * becomes available. 1923 */ 1924 if (!claimed_dma()) { 1925 splx(sps); 1926 return; 1927 } 1928 main_running = 1; 1929 splx(sps); 1930 scsi_main(sc); 1931 } 1932 else splx(sps); 1933 } 1934 1935 /* 1936 * Prefix message with full target info. 1937 */ 1938 static void 1939 ncr_tprint(SC_REQ *reqp, char *fmt, ...) 1940 { 1941 va_list ap; 1942 1943 va_start(ap, fmt); 1944 scsipi_printaddr(reqp->xs->xs_periph); 1945 vprintf(fmt, ap); 1946 va_end(ap); 1947 } 1948 1949 /* 1950 * Prefix message with adapter info. 1951 */ 1952 static void 1953 ncr_aprint(struct ncr_softc *sc, char *fmt, ...) 1954 { 1955 va_list ap; 1956 char buf[256]; 1957 1958 va_start(ap, fmt); 1959 vsnprintf(buf, sizeof(buf), fmt, ap); 1960 va_end(ap); 1961 1962 printf("%s: %s", sc->sc_dev.dv_xname, buf); 1963 } 1964 /**************************************************************************** 1965 * Start Debugging Functions * 1966 ****************************************************************************/ 1967 static char *phase_names[] = { 1968 "DATA_OUT", "DATA_IN", "COMMAND", "STATUS", "NONE", "NONE", "MSG_OUT", 1969 "MSG_IN" 1970 }; 1971 1972 static void 1973 show_phase(reqp, phase) 1974 SC_REQ *reqp; 1975 int phase; 1976 { 1977 printf("INFTRANS: %d Phase = %s\n", reqp->targ_id, phase_names[phase]); 1978 } 1979 1980 static void 1981 show_data_sense(xs) 1982 struct scsipi_xfer *xs; 1983 { 1984 u_char *p1, *p2; 1985 int i; 1986 1987 p1 = (u_char *) xs->cmd; 1988 p2 = (u_char *)&xs->sense.scsi_sense; 1989 if(*p2 == 0) 1990 return; /* No(n)sense */ 1991 printf("cmd[%d]: ", xs->cmdlen); 1992 for (i = 0; i < xs->cmdlen; i++) 1993 printf("%x ", p1[i]); 1994 printf("\nsense: "); 1995 for (i = 0; i < sizeof(xs->sense.scsi_sense); i++) 1996 printf("%x ", p2[i]); 1997 printf("\n"); 1998 } 1999 2000 static void 2001 show_request(reqp, qtxt) 2002 SC_REQ *reqp; 2003 char *qtxt; 2004 { 2005 printf("REQ-%s: %d %p[%ld] cmd[0]=%x S=%x M=%x R=%x resid=%d dr_flag=%x %s\n", 2006 qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len, 2007 reqp->xcmd.opcode, reqp->status, reqp->message, 2008 reqp->xs->error, reqp->xs->resid, reqp->dr_flag, 2009 reqp->link ? "L":""); 2010 if (reqp->status == SCSCHKC) 2011 show_data_sense(reqp->xs); 2012 } 2013 2014 static char *sig_names[] = { 2015 "PAR", "SEL", "I/O", "C/D", "MSG", "REQ", "BSY", "RST", 2016 "ACK", "ATN", "LBSY", "PMATCH", "IRQ", "EPAR", "DREQ", "EDMA" 2017 }; 2018 2019 static void 2020 show_signals(dmstat, idstat) 2021 u_char dmstat, idstat; 2022 { 2023 u_short tmp, mask; 2024 int j, need_pipe; 2025 2026 tmp = idstat | ((dmstat & 3) << 8); 2027 printf("Bus signals (%02x/%02x): ", idstat, dmstat & 3); 2028 for (mask = 1, j = need_pipe = 0; mask <= tmp; mask <<= 1, j++) { 2029 if (tmp & mask) 2030 printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]); 2031 } 2032 printf("\nDma status (%02x): ", dmstat); 2033 for (mask = 4, j = 10, need_pipe = 0; mask <= dmstat; mask <<= 1, j++) { 2034 if (dmstat & mask) 2035 printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]); 2036 } 2037 printf("\n"); 2038 } 2039 2040 void 2041 scsi_show() 2042 { 2043 SC_REQ *tmp; 2044 int sps = splhigh(); 2045 u_char idstat, dmstat; 2046 int i; 2047 2048 printf("scsi_show: scsi_main is%s running\n", 2049 main_running ? "" : " not"); 2050 for (tmp = issue_q; tmp; tmp = tmp->next) 2051 show_request(tmp, "ISSUED"); 2052 for (tmp = discon_q; tmp; tmp = tmp->next) 2053 show_request(tmp, "DISCONNECTED"); 2054 if (connected) 2055 show_request(connected, "CONNECTED"); 2056 if (can_access_5380()) { 2057 idstat = GET_5380_REG(NCR5380_IDSTAT); 2058 dmstat = GET_5380_REG(NCR5380_DMSTAT); 2059 show_signals(dmstat, idstat); 2060 } 2061 2062 if (connected) 2063 printf("phase = %d, ", connected->phase); 2064 printf("busy:%x, spl:%04x\n", busy, sps); 2065 #ifdef DBG_PID 2066 for (i=0; i<DBG_PID; i++) 2067 printf("\t%d\t%s\n", i, last_hit[i]); 2068 #endif 2069 2070 splx(sps); 2071 } 2072