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