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