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