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