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