1 /* $NetBSD: ncr5380.c,v 1.1.1.1 1995/03/26 07:12:10 leo 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/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/device.h> 37 #include <sys/buf.h> 38 #include <scsi/scsi_all.h> 39 #include <scsi/scsi_message.h> 40 #include <scsi/scsiconf.h> 41 #include <machine/iomap.h> 42 #include <machine/mfp.h> 43 #include <atari/dev/ncr5380reg.h> 44 45 /* 46 * This is crap, but because the interrupts now run at MFP spl-level (6), 47 * splbio() is not enough at some places. The code should be checked to 48 * find out where splhigh() is needed and where splbio() should be used. 49 * Now that I use this interrupt sceme, the spl values are fake! 50 */ 51 #undef splbio() 52 #define splbio() splhigh() 53 54 /* 55 * SCSI completion status codes, should move to sys/scsi/???? 56 */ 57 #define SCSMASK 0x1e /* status code mask */ 58 #define SCSGOOD 0x00 /* good status */ 59 #define SCSCHKC 0x02 /* check condition */ 60 #define SCSBUSY 0x08 /* busy status */ 61 #define SCSCMET 0x04 /* condition met / good */ 62 63 /********************************************************************/ 64 65 #define NREQ 18 /* Size of issue queue */ 66 #define AUTO_SENSE 1 /* Automatically issue a request-sense */ 67 68 #undef DBG_SEL /* Show the selection process */ 69 #undef DBG_REQ /* Show enqueued/ready requests */ 70 #undef DBG_NOWRITE /* Do not allow writes to the targets */ 71 #undef DBG_PIO /* Show the polled-I/O process */ 72 #undef DBG_INF /* Show information transfer process */ 73 #undef DBG_NOSTATIC /* No static functions, all in DDB trace*/ 74 #define DBG_PID /* Keep track of driver */ 75 #define REAL_DMA /* Use DMA if sensible */ 76 #define REAL_DMA_POLL 0 /* 1: Poll for end of DMA-transfer */ 77 #undef NO_TTRAM_DMA /* Do not use DMA to TT-ram. This */ 78 /* fails on older atari's */ 79 80 #ifdef DBG_NOSTATIC 81 # define static 82 #endif 83 #ifdef DBG_SEL 84 # define DBG_SELPRINT(a,b) printf(a,b) 85 #else 86 # define DBG_SELPRINT(a,b) 87 #endif 88 #ifdef DBG_PIO 89 # define DBG_PIOPRINT(a,b,c) printf(a,b,c) 90 #else 91 # define DBG_PIOPRINT(a,b,c) 92 #endif 93 #ifdef DBG_INF 94 # define DBG_INFPRINT(a,b,c) a(b,c) 95 #else 96 # define DBG_INFPRINT(a,b,c) 97 #endif 98 #ifdef DBG_PID 99 static char *last_hit = NULL; 100 # define PID(a) last_hit = a 101 #else 102 # define PID(a) 103 #endif 104 105 /* 106 * Return values of check_intr() 107 */ 108 #define INTR_SPURIOUS 0 109 #define INTR_RESEL 2 110 #define INTR_DMA 3 111 112 /* 113 * Set bit for target when parity checking must be disabled. 114 * My (LWP) Maxtor 7245S seems to generate parity errors on about 50% 115 * of all transfers while the data is correct!? 116 */ 117 u_char ncr5380_no_parchk = 0; 118 119 /* 120 * This is the default sense-command we send. 121 */ 122 static u_char sense_cmd[] = { 123 REQUEST_SENSE, 0, 0, 0, sizeof(struct scsi_sense), 0 124 }; 125 126 /* 127 * True if the main co-routine is running 128 */ 129 static volatile int main_running = 0; 130 131 /* 132 * True if callback to softint scheduled. 133 */ 134 static volatile int callback_scheduled = 0; 135 136 /* 137 * Mask of targets selected 138 */ 139 u_char busy; 140 141 struct ncr_softc { 142 struct device sc_dev; 143 struct scsi_link sc_link; 144 }; 145 146 static void ncr5380_minphys(struct buf *bp); 147 static int ncr5380_scsi_cmd(struct scsi_xfer *xs); 148 static int ncr5380_show_scsi_cmd(struct scsi_xfer *xs); 149 150 struct scsi_adapter ncr5380_switch = { 151 ncr5380_scsi_cmd, /* scsi_cmd() */ 152 ncr5380_minphys, /* scsi_minphys() */ 153 0, /* open_target_lu() */ 154 0 /* close_target_lu() */ 155 }; 156 157 struct scsi_device ncr5380_dev = { 158 NULL, /* use default error handler */ 159 NULL, /* do not have a start functio */ 160 NULL, /* have no async handler */ 161 NULL /* Use default done routine */ 162 }; 163 164 /* 165 * Max. number of dma-chains per request 166 */ 167 #define MAXDMAIO (MAXPHYS/NBPG + 1) 168 169 /* 170 * Some requests are not contiguous in physical memory. We need to break them 171 * up into contiguous parts for DMA. 172 */ 173 struct dma_chain { 174 u_int dm_count; 175 u_long dm_addr; 176 }; 177 178 /* 179 * Define our issue, free and disconnect queue's. 180 */ 181 typedef struct req_q { 182 struct req_q *next; /* next in free, issue or discon queue */ 183 struct req_q *link; /* next linked command to execute */ 184 struct scsi_xfer *xs; /* request from high-level driver */ 185 u_char dr_flag; /* driver state */ 186 u_char phase; /* current SCSI phase */ 187 u_char msgout; /* message to send when requested */ 188 u_char targ_id; /* target for command */ 189 u_char status; /* returned status byte */ 190 u_char message; /* returned message byte */ 191 struct dma_chain dm_chain[MAXDMAIO]; 192 struct dma_chain *dm_cur; /* current dma-request */ 193 struct dma_chain *dm_last; /* last dma-request */ 194 long xdata_len; /* length of transfer */ 195 u_char *xdata_ptr; /* physical address of transfer */ 196 struct scsi_generic xcmd; /* command to execute */ 197 } SC_REQ; 198 199 /* 200 * Values for dr_flag: 201 */ 202 #define DRIVER_IN_DMA 1 /* Non-polled DMA activated */ 203 #define DRIVER_AUTOSEN 2 /* Doing automatic sense */ 204 #define DRIVER_NOINT 4 /* We are booting: no interrupts */ 205 #define DRIVER_DMAOK 8 /* DMA can be used on this request */ 206 207 208 static SC_REQ req_queue[NREQ]; 209 static SC_REQ *free_head = NULL; /* Free request structures */ 210 static SC_REQ *issue_q = NULL; /* Commands waiting to be issued*/ 211 static SC_REQ *discon_q = NULL; /* Commands disconnected */ 212 static SC_REQ *connected = NULL; /* Command currently connected */ 213 214 /* 215 * Function decls: 216 */ 217 static int transfer_pio __P((u_char *, u_char *, u_long *)); 218 static int wait_req_true __P((void)); 219 static int wait_req_false __P((void)); 220 static int scsi_select __P((SC_REQ *, int)); 221 static int handle_message __P((SC_REQ *, u_int)); 222 static int information_transfer __P((void)); 223 static void reselect __P((void)); 224 static int dma_ready __P((int, int)); 225 static void transfer_dma __P((SC_REQ *, u_int, int)); 226 static int check_autosense __P((SC_REQ *, int)); 227 static int reach_msg_out __P((u_long)); 228 static void timer __P((void)); 229 static int check_intr __P((void)); 230 static void scsi_reset __P((void)); 231 static void scsi_main __P((void)); 232 static int scsi_dmaok __P((SC_REQ *)); 233 static void run_main __P((void)); 234 235 static void show_request __P((SC_REQ *, char *)); 236 static void show_phase __P((SC_REQ *, int)); 237 static void show_signals __P((void)); 238 239 /* 240 * Inline functions: 241 */ 242 extern __inline__ void scsi_ienable() 243 { 244 MFP2->mf_ierb |= IB_SCDM; 245 MFP2->mf_iera |= IA_SCSI; 246 MFP2->mf_imra |= IA_SCSI; 247 } 248 249 extern __inline__ scsi_idisable() 250 { 251 int sps = splbio(); 252 MFP2->mf_ierb &= ~IB_SCDM; 253 MFP2->mf_iera &= ~IA_SCSI; 254 splx(sps); 255 } 256 257 /* 258 * Determine the size of a SCSI command. 259 */ 260 extern __inline__ int command_size(opcode) 261 u_char opcode; 262 { 263 switch((opcode >> 4) & 0xf) { 264 case 0: 265 case 1: 266 return(6); 267 case 2: 268 case 3: 269 return(10); 270 } 271 return(12); 272 } 273 274 275 /* 276 * Wait for request-line to become active. When it doesn't return 0. 277 * Otherwise return != 0. 278 * The timeouts in the 'wait_req_*' functions are arbitrary and rather 279 * large. In 99% of the invocations nearly no timeout is needed but in 280 * some cases (especially when using my tapedrive, a Tandberg 3600) the 281 * device is busy internally and the first SCSI-phase will be delayed. 282 */ 283 extern __inline__ int wait_req_true(void) 284 { 285 int timeout = 25000; 286 287 while(!(SCSI_5380->scsi_idstat & SC_S_REQ) && --timeout) 288 delay(1); 289 return(SCSI_5380->scsi_idstat & SC_S_REQ); 290 } 291 292 /* 293 * Wait for request-line to become inactive. When it doesn't return 0. 294 * Otherwise return != 0. 295 */ 296 extern __inline__ int wait_req_false(void) 297 { 298 int timeout = 25000; 299 300 while((SCSI_5380->scsi_idstat & SC_S_REQ) && --timeout) 301 delay(1); 302 return(!(SCSI_5380->scsi_idstat & SC_S_REQ)); 303 } 304 305 extern __inline__ void finish_req(SC_REQ *reqp) 306 { 307 int sps; 308 struct scsi_xfer *xs = reqp->xs; 309 310 /* 311 * Return request to free-q 312 */ 313 sps = splbio(); 314 reqp->next = free_head; 315 free_head = reqp; 316 splx(sps); 317 318 xs->flags |= ITSDONE; 319 scsi_done(xs); 320 } 321 322 /* 323 * Auto config stuff.... 324 */ 325 int ncr_print __P((void *auxp, char *)); 326 void ncr_attach __P((struct device *, struct device *, void *)); 327 int ncr_match __P((struct device *, struct cfdata *, void *)); 328 329 struct cfdriver ncrscsicd = { 330 NULL, "ncrscsi", (cfmatch_t)ncr_match, ncr_attach, 331 DV_DULL, sizeof(struct ncr_softc), NULL, 0 }; 332 333 int 334 ncr_match(pdp, cdp, auxp) 335 struct device *pdp; 336 struct cfdata *cdp; 337 void *auxp; 338 { 339 if(strcmp(auxp, ncrscsicd.cd_name)) 340 return(0); 341 if(cdp->cf_unit != 0) /* Only one unit */ 342 return(0); 343 return(1); 344 } 345 346 void 347 ncr_attach(pdp, dp, auxp) 348 struct device *pdp, *dp; 349 void *auxp; 350 { 351 struct ncr_softc *sc; 352 int i; 353 354 sc = (struct ncr_softc *)dp; 355 356 sc->sc_link.adapter_softc = sc; 357 sc->sc_link.adapter_target = 7; 358 sc->sc_link.adapter = &ncr5380_switch; 359 sc->sc_link.device = &ncr5380_dev; 360 sc->sc_link.openings = NREQ - 1; 361 362 /* 363 * Enable SCSI-related interrupts 364 */ 365 MFP2->mf_aer |= 0x80; /* SCSI IRQ goes HIGH!!!!! */ 366 367 MFP2->mf_ierb |= IB_SCDM; /* SCSI-dma interrupts */ 368 MFP2->mf_iprb &= ~IB_SCDM; 369 MFP2->mf_imrb |= IB_SCDM; 370 371 MFP2->mf_iera |= IA_SCSI; /* SCSI-5380 interrupts */ 372 MFP2->mf_ipra &= ~IA_SCSI; 373 MFP2->mf_imra |= IA_SCSI; 374 375 /* 376 * Initialize request queue freelist. 377 */ 378 for(i = 0; i < NREQ; i++) { 379 req_queue[i].next = free_head; 380 free_head = &req_queue[i]; 381 } 382 383 /* 384 * Initialize the host adapter 385 */ 386 scsi_idisable(); 387 SCSI_5380->scsi_icom = 0; 388 SCSI_5380->scsi_mode = IMODE_BASE; 389 SCSI_5380->scsi_tcom = 0; 390 SCSI_5380->scsi_idstat = 0; 391 392 printf("\n"); 393 394 395 /* 396 * attach all scsi units on us 397 */ 398 config_found(dp, &sc->sc_link, ncr_print); 399 } 400 401 /* 402 * print diag if name is NULL else just extra 403 */ 404 int 405 ncr_print(auxp, name) 406 void *auxp; 407 char *name; 408 { 409 if(name == NULL) 410 return(UNCONF); 411 return(QUIET); 412 } 413 /* 414 * End of auto config stuff.... 415 */ 416 417 /* 418 * Carry out a request from the high level driver. 419 */ 420 static int 421 ncr5380_scsi_cmd(struct scsi_xfer *xs) 422 { 423 int sps; 424 SC_REQ *reqp; 425 int flags = xs->flags; 426 427 /* 428 * Sanity check on flags... 429 */ 430 if(flags & ITSDONE) { 431 printf("ncr5380_scsi_cmd: command already done.....\n"); 432 xs->flags &= ~ITSDONE; 433 } 434 if(!(flags & INUSE)) { 435 printf("ncr5380_scsi_cmd: command not in use.....\n"); 436 xs->flags |= ~INUSE; 437 } 438 439 /* 440 * LWP: No lun support yet XXX 441 */ 442 if(xs->sc_link->lun != 0) { 443 xs->error = XS_DRIVER_STUFFUP; /* XXX */ 444 return(COMPLETE); 445 } 446 447 /* 448 * We do not queue RESET commands 449 */ 450 if(flags & SCSI_RESET) { 451 scsi_reset(); 452 return(COMPLETE); 453 } 454 455 /* 456 * Get a request block 457 */ 458 sps = splbio(); 459 if((reqp = free_head) == 0) { 460 splx(sps); 461 return(TRY_AGAIN_LATER); 462 } 463 free_head = reqp->next; 464 reqp->next = NULL; 465 splx(sps); 466 467 /* 468 * Initialize our private fields 469 */ 470 reqp->dr_flag = (xs->flags & SCSI_POLL) ? DRIVER_NOINT : 0; 471 reqp->phase = NR_PHASE; 472 reqp->msgout = MSG_NOOP; 473 reqp->status = SCSGOOD; 474 reqp->link = NULL; 475 reqp->xs = xs; 476 reqp->targ_id = xs->sc_link->target; 477 reqp->xdata_ptr = (u_char*)xs->data; 478 reqp->xdata_len = xs->datalen; 479 memcpy(&reqp->xcmd, xs->cmd, sizeof(struct scsi_generic)); 480 481 /* 482 * Check if DMA can be used on this request 483 */ 484 if(!(xs->flags & SCSI_POLL) && scsi_dmaok(reqp)) 485 reqp->dr_flag |= DRIVER_DMAOK; 486 487 /* 488 * Insert the command into the issue queue. Note that 'REQUEST SENSE' 489 * commands are inserted at the head of the queue since any command 490 * will clear the existing contingent allegience condition and the sense 491 * data is only valid while the condition exists. 492 * When possible, link the command to a previous command to the same 493 * target. This is not very sensible when AUTO_SENSE is not defined! 494 * Interrupts are disabled while we are fiddling with the issue-queue. 495 */ 496 sps = splbio(); 497 if((issue_q == NULL) || (reqp->xcmd.opcode == REQUEST_SENSE)) { 498 reqp->next = issue_q; 499 issue_q = reqp; 500 } 501 else { 502 SC_REQ *tmp, *link; 503 504 tmp = issue_q; 505 link = NULL; 506 do { 507 if(!link && (tmp->targ_id == reqp->targ_id) && !tmp->link) 508 link = tmp; 509 } while(tmp->next && (tmp = tmp->next)); 510 tmp->next = reqp; 511 #ifdef AUTO_SENSE 512 if(link) { 513 link->link = reqp; 514 link->xcmd.bytes[link->xs->cmdlen-1] |= 1; 515 } 516 #endif 517 } 518 splx(sps); 519 520 #ifdef DBG_REQ 521 show_request(reqp,(reqp->xcmd.opcode == REQUEST_SENSE) ? "HEAD":"TAIL"); 522 #endif 523 524 run_main(); 525 526 if(xs->flags & SCSI_POLL) 527 return(COMPLETE); /* We're booting */ 528 return(SUCCESSFULLY_QUEUED); 529 } 530 531 #define MIN_PHYS 65536 /*BARF!!!!*/ 532 static void 533 ncr5380_minphys(struct buf *bp) 534 { 535 if(bp->b_bcount > MIN_PHYS) { 536 printf("Uh-oh... ncr5380_minphys setting bp->b_bcount=%x.\n",MIN_PHYS); 537 bp->b_bcount = MIN_PHYS; 538 } 539 } 540 #undef MIN_PHYS 541 542 static int 543 ncr5380_show_scsi_cmd(struct scsi_xfer *xs) 544 { 545 u_char *b = (u_char *) xs->cmd; 546 int i = 0; 547 548 if(!(xs->flags & SCSI_RESET)) { 549 printf("ncr5380(%d:%d:%d,0x%x)-", xs->sc_link->scsibus, 550 xs->sc_link->target, xs->sc_link->lun, xs->sc_link->flags); 551 while(i < xs->cmdlen) { 552 if(i) 553 printf(","); 554 printf("%x",b[i++]); 555 } 556 printf("-\n"); 557 } 558 else { 559 printf("ncr5380(%d:%d:%d)-RESET-\n", 560 xs->sc_link->scsibus,xs->sc_link->target, xs->sc_link->lun); 561 } 562 } 563 564 /* 565 * The body of the driver. 566 */ 567 static void 568 scsi_main() 569 { 570 SC_REQ *req, *prev; 571 int itype; 572 int sps; 573 574 /* 575 * While running in the driver SCSI-interrupts are disabled. 576 */ 577 scsi_idisable(); 578 579 PID(")"); 580 for(;;) { 581 sps = splbio(); 582 if(!connected) { 583 584 /* 585 * Search through the issue-queue for a command 586 * destined for a target that isn't busy. 587 */ 588 prev = NULL; 589 for(req=issue_q; req != NULL; prev = req, req = req->next) { 590 if(!(busy & (1 << req->targ_id))) { 591 /* 592 * Found one, remove it from the issue queue 593 */ 594 if(prev == NULL) 595 issue_q = req->next; 596 else prev->next = req->next; 597 req->next = NULL; 598 break; 599 } 600 } 601 602 /* 603 * When a request has just ended, we get here before an other 604 * device detects that the bus is free and that it can 605 * reconnect. The problem is that when this happens, we always 606 * baffle the device because our (initiator) id is higher. This 607 * can cause a sort of starvation on slow devices. So we check 608 * for a pending reselection here. 609 * Note that 'connected' will be non-null if the reselection 610 * succeeds. 611 */ 612 if((SCSI_5380->scsi_idstat&(SC_S_SEL|SC_S_IO)) 613 == (SC_S_SEL|SC_S_IO)){ 614 if(req != NULL) { 615 req->next = issue_q; 616 issue_q = req; 617 } 618 splx(sps); 619 620 reselect(); 621 SC_CLINT; 622 goto connected; 623 } 624 625 /* 626 * The host is not connected and there is no request 627 * pending, exit. 628 */ 629 if(req == NULL) { 630 PID("{"); 631 goto main_exit; 632 } 633 634 /* 635 * Re-enable interrupts before handling the request. 636 */ 637 splx(sps); 638 639 #ifdef DBG_REQ 640 show_request(req, "TARGET"); 641 #endif 642 /* 643 * We found a request. Try to connect to the target. If the 644 * initiator fails arbitration, the command is put back in the 645 * issue queue. 646 */ 647 if(scsi_select(req, 0)) { 648 sps = splbio(); 649 req->next = issue_q; 650 issue_q = req; 651 splx(sps); 652 #ifdef DBG_REQ 653 printf("Select failed on target %d\n", req->targ_id); 654 #endif 655 } 656 } 657 else splx(sps); 658 connected: 659 if(connected) { 660 /* 661 * If the host is currently connected but a 'real-dma' transfer 662 * is in progress, the 'end-of-dma' interrupt restarts main. 663 * So quit. 664 */ 665 sps = splbio(); 666 if(connected && (connected->dr_flag & DRIVER_IN_DMA)) { 667 PID("["); 668 goto main_exit; 669 } 670 splx(sps); 671 672 /* 673 * Let the target guide us through the bus-phases 674 */ 675 while(information_transfer() == -1) 676 ; 677 } 678 } 679 /* NEVER TO REACH HERE */ 680 panic("TTSCSI: not designed to come here"); 681 682 main_exit: 683 /* 684 * We enter here with interrupts disabled. We are about to exit main 685 * so interrupts should be re-enabled. Because interrupts are edge 686 * triggered, we could already have missed the interrupt. Therefore 687 * we check the IRQ-line here and re-enter when we really missed a 688 * valid interrupt. 689 */ 690 PID("S"); 691 scsi_ienable(); 692 SCSI_5380->scsi_idstat = SC_HOST_ID; 693 if(SCSI_5380->scsi_dmstat & SC_IRQ_SET) { 694 if((itype = check_intr()) != INTR_SPURIOUS) { 695 scsi_idisable(); 696 splx(sps); 697 698 if(itype == INTR_RESEL) 699 reselect(); 700 else dma_ready(0, 0); 701 SC_CLINT; 702 goto connected; 703 } 704 } 705 main_running = 0; 706 splx(sps); 707 PID("R"); 708 } 709 710 /* 711 * The SCSI-DMA interrupt. 712 * This interrupt can only be triggered when running in non-polled DMA 713 * mode. When DMA is not active, it will be silently ignored, it is usually 714 * to late because the EOP interrupt of the controller happens just a tiny 715 * bit earlier. It might become usefull when scatter/gather is implemented, 716 * because in that case only part of the DATAIN/DATAOUT transfer is taken 717 * out of a single buffer. 718 */ 719 scsi_dma(sr) 720 int sr; /* sr at time of interrupt */ 721 { 722 SC_REQ *reqp; 723 int dma_done; 724 725 PID("9"); 726 if((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) { 727 scsi_idisable(); 728 if(!(dma_done = dma_ready(0, 0))) { 729 scsi_ienable(); 730 transfer_dma(reqp, reqp->phase, 0); 731 return; 732 } 733 PID("!"); 734 if(!BASEPRI(sr)) { 735 if(!callback_scheduled++) 736 add_sicallback(run_main, 0, 0); 737 } 738 else { 739 spl1(); 740 run_main(); 741 } 742 } 743 /* PID("#"); */ 744 } 745 746 /* 747 * The SCSI-controller interrupt. This interrupt occurs on reselections and 748 * at the end of non-polled DMA-interrupts. 749 */ 750 scsi_ctrl(sr) 751 int sr; /* sr at time of interrupt */ 752 { 753 int itype; 754 int dma_done; 755 756 /* PID("$"); */ 757 while(SCSI_5380->scsi_dmstat & SC_IRQ_SET) { 758 scsi_idisable(); 759 /* PID("&"); */ 760 if((itype = check_intr()) != INTR_SPURIOUS) { 761 /* PID("o"); */ 762 if(itype == INTR_RESEL) 763 reselect(); 764 else { 765 if(!(dma_done = dma_ready(0, 0))) { 766 scsi_ienable(); 767 transfer_dma(connected, connected->phase, 0); 768 return; 769 } 770 } 771 SC_CLINT; 772 } 773 /* PID("*"); */ 774 if(!BASEPRI(sr)) { 775 if(!callback_scheduled++) 776 add_sicallback(run_main, 0, 0); 777 } 778 else { 779 spl1(); 780 run_main(); 781 } 782 return; 783 } 784 PID("("); 785 } 786 787 /* 788 * Initiate a connection path between the host and the target. The function 789 * first goes into arbitration for the SCSI-bus. When this succeeds, the target 790 * is selected and an 'IDENTIFY' message is send. 791 * Returns -1 when the arbitration failed. Otherwise 0 is returned. When 792 * the target does not respond (to either selection or 'MESSAGE OUT') the 793 * 'done' function is executed. 794 * The result code given by the driver can be influenced by setting 'code' 795 * to a non-zero value. This is the case when 'select' is called by abort. 796 */ 797 static int 798 scsi_select(reqp, code) 799 SC_REQ *reqp; 800 { 801 u_long timeout; 802 u_char tmp[1]; 803 u_char phase; 804 u_long cnt; 805 int sps; 806 807 DBG_SELPRINT ("Starting arbitration\n", 0); 808 PID("T"); 809 810 sps = splbio(); 811 812 /* 813 * Prevent a race condition here. If a reslection interrupt occurred 814 * between the decision to pick a new request and the call to select, 815 * we abort the selection. 816 * Interrupts are lowered when the 5380 is setup to arbitrate for the 817 * bus. 818 */ 819 if(connected || (SCSI_5380->scsi_idstat & SC_S_BSY)) { 820 splx(sps); 821 PID("Y"); 822 return(-1); 823 } 824 825 /* 826 * Set phase bits to 0, otherwise the 5380 won't drive the bus during 827 * selection. 828 */ 829 SCSI_5380->scsi_tcom = 0; 830 SCSI_5380->scsi_icom = 0; 831 832 /* 833 * Arbitrate for the bus. 834 */ 835 SCSI_5380->scsi_data = SC_HOST_ID; 836 SCSI_5380->scsi_mode = SC_ARBIT; 837 838 splx(sps); 839 840 cnt = 10000; 841 while(!(SCSI_5380->scsi_icom & SC_AIP) && --cnt) 842 delay(1); 843 844 if(!(SCSI_5380->scsi_icom & SC_AIP)) { 845 SCSI_5380->scsi_mode = IMODE_BASE; 846 delay(1); 847 PID("U"); 848 849 if(SCSI_5380->scsi_idstat & SC_S_BSY) { 850 /* 851 * Damn, we have a connected target that we don't know 852 * of. Some targets seem to respond to a selection 853 * AFTER the selection-timeout. Try to get the target 854 * into the Message-out phase so we can send an ABORT 855 * message. We try to avoid resetting the SCSI-bus! 856 */ 857 if(!reach_msg_out(sizeof(struct scsi_generic))) { 858 u_long len = 1; 859 u_char phase = PH_MSGOUT; 860 u_char msg = MSG_ABORT; 861 862 transfer_pio(&phase, &msg, &len); 863 } 864 else if(SCSI_5380->scsi_idstat & SC_S_BSY) 865 scsi_reset(); 866 } 867 PID("I"); 868 return(-1); 869 } 870 871 /* The arbitration delay is 2.2 usecs */ 872 delay(3); 873 874 /* 875 * Check the result of the arbitration. If we failed, return -1. 876 */ 877 if(SCSI_5380->scsi_icom & SC_LA) { 878 /* 879 * The spec requires that we should read the data register to 880 * check for higher id's and check the SC_LA again. 881 */ 882 tmp[0] = SCSI_5380->scsi_data; 883 if(SCSI_5380->scsi_icom & SC_LA) { 884 SCSI_5380->scsi_mode = IMODE_BASE; 885 SCSI_5380->scsi_icom = 0; 886 DBG_SELPRINT ("Arbitration lost,deassert SC_ARBIT\n",0); 887 PID("O"); 888 return(-1); 889 } 890 } 891 SCSI_5380->scsi_icom = SC_A_SEL | SC_A_BSY; 892 if(SCSI_5380->scsi_icom & SC_LA) { 893 SCSI_5380->scsi_mode = IMODE_BASE; 894 SCSI_5380->scsi_icom = 0; 895 DBG_SELPRINT ("Arbitration lost, deassert SC_A_SEL\n", 0); 896 PID("P"); 897 return(-1); 898 } 899 /* Bus settle delay + Bus clear delay = 1.2 usecs */ 900 delay(2); 901 DBG_SELPRINT ("Arbitration complete\n", 0); 902 903 /* 904 * Now that we won the arbitration, start the selection. 905 */ 906 SCSI_5380->scsi_data = SC_HOST_ID | (1 << reqp->targ_id); 907 908 /* 909 * Raise ATN while SEL is true before BSY goes false from arbitration, 910 * since this is the only way to guarantee that we'll get a MESSAGE OUT 911 * phase immediately after the selection. 912 */ 913 SCSI_5380->scsi_icom = SC_A_BSY | SC_A_SEL | SC_A_ATN | SC_ADTB; 914 SCSI_5380->scsi_mode = IMODE_BASE; 915 916 /* 917 * Turn off reselection interrupts 918 */ 919 SCSI_5380->scsi_idstat = 0; 920 921 /* 922 * Reset BSY. The delay following it, surpresses a glitch in the 923 * 5380 which causes us to see our own BSY signal instead of that of 924 * the target. 925 */ 926 SCSI_5380->scsi_icom = SC_A_SEL | SC_A_ATN | SC_ADTB; 927 delay(1); 928 929 /* 930 * Wait for the target to react, the specs call for a timeout of 931 * 250 ms. 932 */ 933 cnt = 250000 /* 250 */; 934 while(!(SCSI_5380->scsi_idstat & SC_S_BSY) && --cnt) 935 delay(1); 936 937 if(!(SCSI_5380->scsi_idstat & SC_S_BSY)) { 938 /* 939 * There is no reaction from the target, start the selection 940 * timeout procedure. We release the databus but keep SEL 941 * asserted. After that we wait a 'selection abort time' (200 942 * usecs) and 2 deskew delays (90 ns) and check BSY again. 943 * When BSY is asserted, we assume the selection succeeded, 944 * otherwise we release the bus. 945 */ 946 SCSI_5380->scsi_icom = SC_A_SEL | SC_A_ATN; 947 delay(201); 948 if(!(SCSI_5380->scsi_idstat & SC_S_BSY)) { 949 SCSI_5380->scsi_icom = 0; 950 reqp->xs->error = code ? code : XS_SELTIMEOUT; 951 DBG_SELPRINT ("Target %d not responding to sel\n", 952 reqp->targ_id); 953 finish_req(reqp); 954 PID("A"); 955 return(0); 956 } 957 } 958 SCSI_5380->scsi_icom = SC_A_ATN; 959 960 DBG_SELPRINT ("Target %d responding to select.\n", reqp->targ_id); 961 962 /* 963 * The SCSI-interrupts are disabled while a request is being handled. 964 */ 965 scsi_idisable(); 966 967 /* 968 * Since we followed the SCSI-spec and raised ATN while SEL was true 969 * but before BSY was false during the selection, a 'MESSAGE OUT' 970 * phase should follow. Here we send an 'IDENTIFY' message. 971 * Allow disconnect only when interrups are allowed. 972 */ 973 tmp[0] = MSG_IDENTIFY(0, (reqp->dr_flag & DRIVER_NOINT) ? 0 : 1); 974 cnt = 1; 975 phase = PH_MSGOUT; 976 if(transfer_pio(&phase, tmp, &cnt) || cnt) { 977 DBG_SELPRINT ("Target %d: failed to send identify\n", 978 reqp->targ_id); 979 /* 980 * Try to disconnect from the target. We cannot leave it just 981 * hanging here. 982 */ 983 if(!reach_msg_out(sizeof(struct scsi_generic))) { 984 u_long len = 1; 985 u_char phase = PH_MSGOUT; 986 u_char msg = MSG_ABORT; 987 988 transfer_pio(&phase, &msg, &len); 989 } 990 else scsi_reset(); 991 992 SCSI_5380->scsi_icom = 0; 993 reqp->xs->error = code ? code : XS_DRIVER_STUFFUP; 994 finish_req(reqp); 995 PID("S"); 996 return(0); 997 } 998 reqp->phase = PH_MSGOUT; 999 1000 #ifdef notyet /* LWP: Do we need timeouts in the driver? */ 1001 /* 1002 * Command is connected, start timer ticking. 1003 */ 1004 ccb_p->xtimeout = ccb_p->timeout + Lbolt; 1005 #endif 1006 1007 connected = reqp; 1008 busy |= 1 << reqp->targ_id; 1009 PID("D"); 1010 return(0); 1011 } 1012 1013 /* 1014 * Return codes: 1015 * -1: quit main, trigger on interrupt 1016 * 0: keep on running main. 1017 */ 1018 static int 1019 information_transfer() 1020 { 1021 SC_REQ *reqp = connected; 1022 u_char tmp, phase; 1023 u_long len; 1024 1025 PID("F"); 1026 /* 1027 * Clear pending interrupts from 5380-chip. 1028 */ 1029 SC_CLINT; 1030 1031 /* 1032 * We only have a valid SCSI-phase when REQ is asserted. Something 1033 * is deadly wrong when BSY has dropped. 1034 */ 1035 tmp = SCSI_5380->scsi_idstat; 1036 1037 if(!(tmp & SC_S_BSY)) { 1038 busy &= ~(1 << reqp->targ_id); 1039 connected = NULL; 1040 reqp->xs->error = XS_BUSY; 1041 finish_req(reqp); 1042 PID("G"); 1043 return(0); 1044 } 1045 1046 if(tmp & SC_S_REQ) { 1047 phase = (tmp >> 2) & 7; 1048 if(phase != reqp->phase) { 1049 reqp->phase = phase; 1050 DBG_INFPRINT(show_phase, reqp, phase); 1051 } 1052 } 1053 else return(-1); 1054 1055 switch(phase) { 1056 case PH_DATAOUT: 1057 1058 #ifdef DBG_NOWRITE 1059 printf("NOWRITE set -- write attempt aborted."); 1060 reqp->msgout = MSG_ABORT; 1061 SCSI_5380->scsi_icom = SC_A_ATN; 1062 return(-1); 1063 #endif /* DBG_NOWRITE */ 1064 1065 case PH_DATAIN: 1066 #ifdef REAL_DMA 1067 if(reqp->dr_flag & DRIVER_DMAOK) { 1068 int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT); 1069 transfer_dma(reqp, phase, poll); 1070 if(!poll) 1071 return(0); 1072 } 1073 else 1074 #endif 1075 { 1076 PID("H"); 1077 len = reqp->xdata_len; 1078 transfer_pio(&phase, reqp->xdata_ptr, &len); 1079 reqp->xdata_ptr += reqp->xdata_len - len; 1080 reqp->xdata_len = len; 1081 } 1082 return(-1); 1083 case PH_MSGIN: 1084 /* 1085 * We only expect single byte messages here. 1086 */ 1087 len = 1; 1088 transfer_pio(&phase, &tmp, &len); 1089 reqp->message = tmp; 1090 return(handle_message(reqp, tmp)); 1091 case PH_MSGOUT: 1092 len = 1; 1093 transfer_pio(&phase, &reqp->msgout, &len); 1094 if(reqp->msgout == MSG_ABORT) { 1095 busy &= ~(1 << reqp->targ_id); 1096 connected = NULL; 1097 reqp->xs->error = XS_DRIVER_STUFFUP; 1098 finish_req(reqp); 1099 PID("J"); 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); 1107 PID("K"); 1108 return(-1); 1109 case PH_STATUS: 1110 len = 1; 1111 transfer_pio(&phase, &tmp, &len); 1112 reqp->status = tmp; 1113 PID("L"); 1114 return(-1); 1115 default : 1116 printf("SCSI: unknown phase on target %d\n", reqp->targ_id); 1117 } 1118 PID(":"); 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 1135 PID(";"); 1136 switch(msg) { 1137 /* 1138 * Linking lets us reduce the time required to get 1139 * the next command to the device, skipping the arbitration 1140 * and selection time. In the current implementation, 1141 * we merely have to start the next command pointed 1142 * to by 'next_link'. 1143 */ 1144 case MSG_LINK_CMD_COMPLETE: 1145 case MSG_LINK_CMD_COMPLETEF: 1146 if(reqp->link == NULL) { 1147 printf("SCSI: no link for linked command" 1148 "on target %d\n", reqp->targ_id); 1149 reqp->msgout = MSG_ABORT; 1150 SCSI_5380->scsi_icom = SC_A_ATN; 1151 PID("@"); 1152 return(-1); 1153 } 1154 reqp->xs->error = 0; 1155 1156 #ifdef AUTO_SENSE 1157 if(check_autosense(reqp, 0) == -1) 1158 return(-1); 1159 #endif /* AUTO_SENSE */ 1160 1161 #ifdef DBG_REQ 1162 show_request(reqp->link, "LINK"); 1163 #endif 1164 connected = reqp->link; 1165 finish_req(reqp); 1166 PID("'"); 1167 return(-1); 1168 case MSG_ABORT: 1169 case MSG_CMDCOMPLETE: 1170 #ifdef DBG_REQ 1171 show_request(reqp, "DONE"); 1172 #endif 1173 connected = NULL; 1174 busy &= ~(1 << reqp->targ_id); 1175 if(!(reqp->dr_flag & DRIVER_AUTOSEN)) 1176 reqp->xs->resid = reqp->xdata_len; 1177 reqp->xs->error = 0; 1178 1179 #ifdef AUTO_SENSE 1180 if(check_autosense(reqp, 0) == -1) { 1181 PID("Z"); 1182 return(0); 1183 } 1184 #endif /* AUTO_SENSE */ 1185 1186 finish_req(reqp); 1187 PID("X"); 1188 return(0); 1189 case MSG_MESSAGE_REJECT: 1190 PID("C"); 1191 return(-1); 1192 case MSG_DISCONNECT: 1193 #ifdef DBG_REQ 1194 show_request(reqp, "DISCON"); 1195 #endif 1196 sps = splbio(); 1197 connected = NULL; 1198 reqp->next = discon_q; 1199 discon_q = reqp; 1200 splx(sps); 1201 PID("V"); 1202 return(0); 1203 case MSG_SAVEDATAPOINTER: 1204 case MSG_RESTOREPOINTERS: 1205 /* 1206 * We save pointers implicitely at disconnect. 1207 * So we can ignore these messages. 1208 */ 1209 PID("B"); 1210 return(-1); 1211 default: 1212 printf("SCSI: unkown message %x on target %d\n", msg, 1213 reqp->targ_id); 1214 return(-1); 1215 } 1216 PID("N"); 1217 return(-1); 1218 } 1219 1220 /* 1221 * Handle reselection. If a valid reconnection occurs, connected 1222 * points at the reconnected command. The command is removed from the 1223 * disconnected queue. 1224 */ 1225 static void 1226 reselect() 1227 { 1228 u_char phase; 1229 u_long len; 1230 u_char msg; 1231 u_char target_mask; 1232 int abort = 0; 1233 SC_REQ *tmp, *prev; 1234 1235 PID("M"); 1236 target_mask = SCSI_5380->scsi_data & ~SC_HOST_ID; 1237 1238 /* 1239 * At this point, we have detected that our SCSI-id is on the bus, 1240 * SEL is true and BSY was false for at least one bus settle 1241 * delay (400 ns.). 1242 * We must assert BSY ourselves, until the target drops the SEL signal. 1243 */ 1244 SCSI_5380->scsi_icom = SC_A_BSY; 1245 while(SCSI_5380->scsi_idstat & SC_S_SEL) 1246 ; 1247 1248 SCSI_5380->scsi_icom = 0; 1249 1250 /* 1251 * Get the expected identify message. 1252 */ 1253 phase = PH_MSGIN; 1254 len = 1; 1255 transfer_pio(&phase, &msg, &len); 1256 if(len || !MSG_ISIDENTIFY(msg)) { 1257 printf("SCSI: expecting IDENTIFY, got 0x%x\n", msg); 1258 abort = 1; 1259 } 1260 else { 1261 /* 1262 * Find the command reconnecting 1263 */ 1264 for(tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){ 1265 if(target_mask == (1 << tmp->targ_id)) { 1266 if(prev) 1267 prev->next = tmp->next; 1268 else discon_q = tmp->next; 1269 tmp->next = NULL; 1270 break; 1271 } 1272 } 1273 if(tmp == NULL) { 1274 printf("SCSI: no disconnected job for targetmask %x\n", 1275 target_mask); 1276 abort = 1; 1277 } 1278 } 1279 if(abort) { 1280 msg = MSG_ABORT; 1281 len = 1; 1282 phase = PH_MSGOUT; 1283 1284 SCSI_5380->scsi_icom = SC_A_ATN; 1285 transfer_pio(&phase, &msg, &len); 1286 } 1287 else { 1288 connected = tmp; 1289 #ifdef DBG_REQ 1290 show_request(tmp, "RECON"); 1291 #endif 1292 } 1293 PID("<"); 1294 } 1295 1296 /* 1297 * Transfer data in a given phase using polled I/O. 1298 * Returns -1 when a different phase is entered without transferring the 1299 * maximum number of bytes, 0 if all bytes or exit is in the same 1300 * phase. 1301 */ 1302 static int 1303 transfer_pio(phase, data, len) 1304 u_char *phase; 1305 u_char *data; 1306 u_long *len; 1307 { 1308 u_int cnt = *len; 1309 u_char ph = *phase; 1310 u_char tmp; 1311 1312 DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt); 1313 PID(","); 1314 SCSI_5380->scsi_tcom = ph; 1315 do { 1316 if(!wait_req_true()) { 1317 DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0); 1318 break; 1319 } 1320 if(((SCSI_5380->scsi_idstat >> 2) & 7) != ph) { 1321 DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0); 1322 break; 1323 } 1324 if(PH_IN(ph)) { 1325 *data++ = SCSI_5380->scsi_data; 1326 SCSI_5380->scsi_icom = SC_A_ACK; 1327 } 1328 else { 1329 SCSI_5380->scsi_data = *data++; 1330 1331 /* 1332 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase, 1333 * the initiator should drop ATN on the last byte of the 1334 * message phase after REQ has been asserted for the handshake 1335 * but before the initiator raises ACK. 1336 */ 1337 if(!( (ph == PH_MSGOUT) && (cnt > 1) )) { 1338 SCSI_5380->scsi_icom = SC_ADTB; 1339 SCSI_5380->scsi_icom = SC_ADTB | SC_A_ACK; 1340 } 1341 else { 1342 SCSI_5380->scsi_icom = SC_ADTB | SC_A_ATN; 1343 SCSI_5380->scsi_icom = SC_ADTB | SC_A_ATN | SC_A_ACK; 1344 } 1345 } 1346 if(!wait_req_false()) { 1347 DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0); 1348 break; 1349 } 1350 1351 if(!( (ph == PH_MSGOUT) && (cnt > 1) )) 1352 SCSI_5380->scsi_icom = 0; 1353 else SCSI_5380->scsi_icom = SC_A_ATN; 1354 } while(--cnt); 1355 1356 if((tmp = SCSI_5380->scsi_idstat) & SC_S_REQ) 1357 *phase = (tmp >> 2) & 7; 1358 else *phase = NR_PHASE; 1359 *len = cnt; 1360 DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n", 1361 *phase, cnt); 1362 PID(">"); 1363 if(!cnt || (*phase == ph)) 1364 return(0); 1365 return(-1); 1366 } 1367 1368 /* 1369 * Start a DMA-transfer on the device using the current pointers. 1370 * If 'poll' is true, the function waits until DMA-has completed. 1371 */ 1372 static void 1373 transfer_dma(reqp, phase, poll) 1374 SC_REQ *reqp; 1375 u_int phase; 1376 int poll; 1377 { 1378 int dmastat; 1379 u_char mbase = 0; 1380 int sps; 1381 1382 again: 1383 PID("?"); 1384 /* 1385 * We should be in phase, otherwise we are not allowed to 1386 * drive the bus. 1387 */ 1388 SCSI_5380->scsi_tcom = phase; 1389 1390 /* 1391 * Defer interrupts until DMA is fully running. 1392 */ 1393 sps = splbio(); 1394 1395 /* 1396 * Clear pending interrupts and parity errors. 1397 */ 1398 SCSI_DMA->s_dma_ctrl = 0; 1399 SC_CLINT; 1400 1401 if(!poll) { 1402 /* 1403 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase' 1404 * to the interrupts we want enabled. 1405 */ 1406 scsi_ienable(); 1407 reqp->dr_flag |= DRIVER_IN_DMA; 1408 mbase = SC_E_EOPI | SC_MON_BSY; 1409 } 1410 1411 if(PH_IN(phase)) { 1412 SCSI_DMA->s_dma_ctrl = SD_IN; 1413 set_scsi_dma(&(SCSI_DMA->s_dma_ptr), reqp->dm_cur->dm_addr); 1414 set_scsi_dma(&(SCSI_DMA->s_dma_cnt), reqp->dm_cur->dm_count); 1415 SCSI_5380->scsi_icom = 0; 1416 SCSI_5380->scsi_mode = IMODE_BASE | mbase | SC_M_DMA; 1417 SCSI_DMA->s_dma_ctrl = SD_ENABLE; 1418 SCSI_5380->scsi_ircv = 0; 1419 } 1420 else { 1421 SCSI_DMA->s_dma_ctrl = SD_OUT; 1422 set_scsi_dma(&(SCSI_DMA->s_dma_ptr), reqp->dm_cur->dm_addr); 1423 set_scsi_dma(&(SCSI_DMA->s_dma_cnt), reqp->dm_cur->dm_count); 1424 SCSI_5380->scsi_mode = IMODE_BASE | mbase | SC_M_DMA; 1425 SCSI_5380->scsi_icom = SC_ADTB; 1426 SCSI_5380->scsi_dmstat = 0; 1427 SCSI_DMA->s_dma_ctrl = SD_ENABLE|SD_OUT; 1428 } 1429 splx(sps); 1430 1431 if(poll) { 1432 /* 1433 * We wait here until the DMA has finished. This can be 1434 * achieved by checking the following conditions: 1435 * - 5380: 1436 * - End of DMA flag is set 1437 * - We lost BSY (error!!) 1438 * - A phase mismatch has occured (partial transfer) 1439 * - DMA-controller: 1440 * - A bus error occurred (Kernel error!!) 1441 * - All bytes are transferred 1442 * We one of the terminating conditions was met, we call 1443 * 'dma_ready' to check errors and perform the bookkeeping. 1444 */ 1445 u_char dmstat, dmastat; 1446 int dma_done; 1447 1448 PID("/"); 1449 for(;;) { 1450 dmstat = SCSI_5380->scsi_dmstat; 1451 dmastat = SCSI_DMA->s_dma_ctrl; 1452 if(dmstat & (SC_END_DMA|SC_BSY_ERR|SC_IRQ_SET)) 1453 break; 1454 if(!(dmstat & SC_PHS_MTCH)) 1455 break; 1456 if(dmastat & (SD_BUSERR|SD_ZERO)) 1457 break; 1458 } 1459 PID("q"); 1460 if(dmastat & (SD_BUSERR|SD_ZERO)) 1461 dma_done = dma_ready(1, dmastat); 1462 else dma_done = dma_ready(0, 0); 1463 if(!dma_done) 1464 goto again; 1465 1466 } 1467 PID("w"); 1468 } 1469 1470 /* 1471 * Check results of a DMA data-transfer. 1472 */ 1473 static int 1474 dma_ready(has_dmastat, dmastat) 1475 int has_dmastat, dmastat; 1476 { 1477 int dmstat, phase; 1478 long tmp, bytes_left, bytes_done; 1479 int i; 1480 SC_REQ *reqp = connected; 1481 1482 /* 1483 * Get values of the status registers of the 5380 & DMA-controller. 1484 */ 1485 dmstat = SCSI_5380->scsi_dmstat; 1486 if(!has_dmastat) 1487 dmastat = SCSI_DMA->s_dma_ctrl; 1488 1489 /* 1490 * Check if the call is sensible and not caused by any spurious 1491 * interrupt. 1492 */ 1493 if( !(dmastat & (SD_BUSERR|SD_ZERO)) 1494 && !(dmstat & (SC_END_DMA|SC_BSY_ERR)) 1495 && (dmstat & SC_PHS_MTCH) ) { 1496 printf("dma_ready: spurious call (dma:%x,dm:%x,last_hit: %s)\n", 1497 dmastat, dmstat, last_hit); 1498 return(0); 1499 } 1500 1501 /* 1502 * If end of a DMA transfer, check it's results. 1503 */ 1504 get_scsi_dma(SCSI_DMA->s_dma_cnt, bytes_left); 1505 1506 if(dmastat & SD_BUSERR) { 1507 /* 1508 * The DMA-controller seems to access 8 bytes beyond 1509 * it's limits on output. Therefore check also the byte 1510 * count. If it's zero, ignore the bus error. 1511 */ 1512 if(bytes_left != 0) { 1513 get_scsi_dma(SCSI_DMA->s_dma_ptr, tmp); 1514 printf("SCSI-DMA buserror - accessing 0x%x\n", tmp); 1515 panic("SCSI"); 1516 } 1517 } 1518 1519 if(bytes_left != 0) { 1520 /* 1521 * The byte-count is not zero. This is not always an error,on 1522 * tape drives this usually means EOF. We check that the 1523 * residu is a multiple of 512-bytes, when we know the device 1524 * type it should be included in the check. 1525 * A severe complication is that if a device wants to 1526 * disconnect in the middle of a DMA-transfer, the 5380 has 1527 * already prefetched the next byte from the DMA-controller 1528 * before the phase mismatch occurs. I don't know how this 1529 * fact can be uniquely identified. For the moment, we round 1530 * up the residual if it is odd. 1531 */ 1532 if(PH_OUT(reqp->phase) && (bytes_left & 1)) 1533 bytes_left++; 1534 1535 } 1536 else { 1537 if(PH_IN(reqp->phase)) { 1538 /* 1539 * Check for bytes not transfered. This should never occur 1540 * because the buffer-pool is aligned on a 4-byte boundary. 1541 */ 1542 u_char *p, *q; 1543 1544 if(bytes_left > 3) { 1545 scsi_show(); 1546 printf("SCSI: %d bytes not transferred\n", bytes_left); 1547 panic("SCSI"); 1548 } 1549 p = (u_char*)(bytes_left & ~3); 1550 q = (u_char*)&(SCSI_DMA->s_dma_res); 1551 switch(bytes_left & 3) { 1552 case 3: *p++ = *q++; 1553 case 2: *p++ = *q++; 1554 case 1: *p++ = *q++; 1555 printf("SCSI: dma residue count != 0, "); 1556 printf("Check buffer alignment\n"); 1557 } 1558 } 1559 } 1560 1561 /* 1562 * Update various transfer-pointers/lengths 1563 */ 1564 bytes_done = reqp->dm_cur->dm_count - bytes_left; 1565 1566 reqp->xdata_ptr = &reqp->xdata_ptr[bytes_done]; /* XXX */ 1567 reqp->xdata_len -= bytes_done; /* XXX */ 1568 if((reqp->dm_cur->dm_count -= bytes_done) == 0) 1569 reqp->dm_cur++; 1570 else reqp->dm_cur->dm_addr += bytes_done; 1571 1572 if(PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) { 1573 if(!(ncr5380_no_parchk & (1 << reqp->targ_id))) 1574 /* XXX: Should be parity error ???? */ 1575 reqp->xs->error = XS_DRIVER_STUFFUP; 1576 } 1577 1578 /* 1579 * DMA mode should always be reset even when we will continue with the 1580 * next chain. 1581 */ 1582 SCSI_5380->scsi_mode &= ~SC_M_DMA; 1583 1584 if((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH) 1585 || (reqp->dm_cur > reqp->dm_last)) { 1586 /* 1587 * Tell interrupt functions DMA mode has ended. 1588 */ 1589 reqp->dr_flag &= ~DRIVER_IN_DMA; 1590 1591 /* 1592 * Turn off DMA-mode and clear icom 1593 */ 1594 SCSI_5380->scsi_mode &= 1595 ~(SC_M_DMA|SC_E_EOPI|SC_MON_BSY|SC_E_PARI); 1596 SCSI_5380->scsi_icom = 0; 1597 1598 /* 1599 * Clear all (pending) interrupts. 1600 */ 1601 SCSI_DMA->s_dma_ctrl = 0; 1602 SC_CLINT; 1603 1604 if(dmstat & SC_BSY_ERR) { 1605 if(!reqp->xs->error) 1606 reqp->xs->error = XS_BUSY; 1607 finish_req(reqp); 1608 PID("r"); 1609 return(1); 1610 } 1611 1612 if(reqp->xs->error != 0) { 1613 printf("dma-ready: code = %d\n", reqp->xs->error); /* LWP */ 1614 reqp->msgout = MSG_ABORT; 1615 SCSI_5380->scsi_icom = SC_A_ATN; 1616 } 1617 PID("t"); 1618 return(1); 1619 } 1620 return(0); 1621 } 1622 1623 static int 1624 check_autosense(reqp, linked) 1625 SC_REQ *reqp; 1626 int linked; 1627 { 1628 int sps; 1629 1630 /* 1631 * If we not executing an auto-sense and the status code 1632 * is request-sense, we automatically issue a request 1633 * sense command. 1634 */ 1635 PID("y"); 1636 if(!(reqp->dr_flag & DRIVER_AUTOSEN)) { 1637 if(reqp->status == SCSCHKC) { 1638 memcpy(&reqp->xcmd, sense_cmd, sizeof(sense_cmd)); 1639 reqp->xdata_ptr = (u_char *)&reqp->xs->sense; 1640 reqp->xdata_len = sizeof(reqp->xs->sense); 1641 reqp->dr_flag |= DRIVER_AUTOSEN; 1642 reqp->dr_flag &= ~DRIVER_DMAOK; 1643 if(!linked) { 1644 sps = splbio(); 1645 reqp->next = issue_q; 1646 issue_q = reqp; 1647 splx(sps); 1648 } 1649 else reqp->xcmd.bytes[4] |= 1; 1650 1651 #ifdef DBG_REQ 1652 show_request(reqp, "AUTO-SENSE"); 1653 #endif 1654 PID("u"); 1655 return(-1); 1656 } 1657 } 1658 else { 1659 /* 1660 * An auto-sense has finished 1661 */ 1662 if((reqp->status & SCSMASK) != SCSGOOD) 1663 reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */ 1664 else reqp->xs->error = XS_SENSE; 1665 reqp->status = SCSCHKC; 1666 } 1667 PID("i"); 1668 return(0); 1669 } 1670 1671 static int 1672 reach_msg_out(len) 1673 u_long len; 1674 { 1675 u_char phase; 1676 u_char data; 1677 1678 printf("SCSI: Trying to reach Message-out phase\n"); 1679 if((phase = SCSI_5380->scsi_idstat) & SC_S_REQ) 1680 phase = (phase >> 2) & 7; 1681 else return(-1); 1682 printf("SCSI: Trying to reach Message-out phase, now: %d\n", phase); 1683 if(phase == PH_MSGOUT) 1684 return(0); 1685 1686 SCSI_5380->scsi_tcom = phase; 1687 1688 do { 1689 if(!wait_req_true()) { 1690 break; 1691 } 1692 if(((SCSI_5380->scsi_idstat >> 2) & 7) != phase) { 1693 break; 1694 } 1695 if(PH_IN(phase)) { 1696 data = SCSI_5380->scsi_data; 1697 SCSI_5380->scsi_icom = SC_A_ACK | SC_A_ATN; 1698 } 1699 else { 1700 SCSI_5380->scsi_data = 0; 1701 SCSI_5380->scsi_icom = SC_ADTB | SC_A_ATN | SC_A_ACK; 1702 } 1703 if(!wait_req_false()) { 1704 break; 1705 } 1706 SCSI_5380->scsi_icom = SC_A_ATN; 1707 } while(--len); 1708 1709 if((phase = SCSI_5380->scsi_idstat) & SC_S_REQ) { 1710 phase = (phase >> 2) & 7; 1711 if(phase == PH_MSGOUT) { 1712 printf("SCSI: Message-out phase reached.\n"); 1713 return(0); 1714 } 1715 } 1716 return(-1); 1717 } 1718 1719 static void 1720 scsi_reset() 1721 { 1722 SC_REQ *tmp, *next; 1723 int sps; 1724 1725 printf("SCSI: resetting SCSI-bus\n"); 1726 1727 PID("7"); 1728 sps = splbio(); 1729 SCSI_5380->scsi_icom = SC_A_RST; 1730 delay(1); 1731 SCSI_5380->scsi_icom = 0; 1732 1733 /* 1734 * None of the jobs in the discon_q will ever be reconnected, 1735 * notify this to the higher level code. 1736 */ 1737 for(tmp = discon_q; tmp ;) { 1738 next = tmp->next; 1739 tmp->next = NULL; 1740 tmp->xs->error = XS_TIMEOUT; 1741 busy &= ~(1 << tmp->targ_id); 1742 finish_req(tmp); 1743 tmp = next; 1744 } 1745 discon_q = NULL; 1746 1747 /* 1748 * The current job will never finish either. 1749 * The problem is that we can't finish the job because an instance 1750 * of main is running on it. Our best guess is that the job is currently 1751 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish 1752 * the job because it detects BSY-loss. 1753 */ 1754 if(tmp = connected) { 1755 if(tmp->dr_flag & DRIVER_IN_DMA) { 1756 tmp->xs->error = XS_DRIVER_STUFFUP; 1757 dma_ready(0, 0); 1758 } 1759 } 1760 splx(sps); 1761 PID("8"); 1762 } 1763 1764 /* 1765 * Check validity of the IRQ set by the 5380. If the interrupt is valid, 1766 * the appropriate action is carried out (reselection or DMA ready) and 1767 * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written 1768 * and INTR_SPURIOUS is returned. 1769 */ 1770 static int 1771 check_intr() 1772 { 1773 SC_REQ *reqp; 1774 1775 if((SCSI_5380->scsi_idstat & (SC_S_SEL|SC_S_IO))==(SC_S_SEL|SC_S_IO)) 1776 return(INTR_RESEL); 1777 else { 1778 if((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){ 1779 reqp->dr_flag &= ~DRIVER_IN_DMA; 1780 return(INTR_DMA); 1781 } 1782 } 1783 SC_CLINT; 1784 printf("-->"); 1785 scsi_show(); 1786 printf("SCSI: spurious interrupt.\n"); 1787 return(INTR_SPURIOUS); 1788 } 1789 1790 /* 1791 * Check if DMA can be used for this request. This function also builds 1792 * the dma-chain. 1793 */ 1794 static int 1795 scsi_dmaok(reqp) 1796 SC_REQ *reqp; 1797 { 1798 u_long phy_buf; 1799 u_long phy_len; 1800 void *req_addr; 1801 u_long req_len; 1802 struct dma_chain *dm; 1803 1804 /* 1805 * Initialize locals and requests' DMA-chain. 1806 */ 1807 req_len = reqp->xdata_len; 1808 req_addr = (void*)reqp->xdata_ptr; 1809 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain; 1810 1811 dm->dm_count = dm->dm_addr = 0; 1812 1813 /* 1814 * Do not accept zero length DMA. 1815 */ 1816 if(req_len == 0) 1817 return(0); 1818 1819 /* 1820 * LWP: I think that this restriction is not strictly nessecary. 1821 */ 1822 if((req_len & 0x1) || ((u_int)req_addr & 0x3)) 1823 return(0); 1824 1825 /* 1826 * Build the DMA-chain. 1827 */ 1828 dm->dm_addr = phy_buf = kvtop(req_addr); 1829 while(req_len) { 1830 if(req_len < (phy_len = NBPG - ((u_long)req_addr & PGOFSET))) 1831 phy_len = req_len; 1832 1833 req_addr += phy_len; 1834 req_len -= phy_len; 1835 dm->dm_count += phy_len; 1836 1837 #ifdef NO_TTRAM_DMA 1838 /* 1839 * LWP: DMA transfers to TT-ram causes data to be garbeled 1840 * without notice on some revisons of the TT-mainboard. 1841 * When program's generate misterious Segmentations faults, 1842 * try turning on NO_TTRAM_DMA. 1843 */ 1844 if(dm->dm_addr > 0x1000000) /* XXX: should be a define??? */ 1845 return(0); 1846 #endif 1847 1848 if(req_len) { 1849 u_long tmp = kvtop(req_addr); 1850 1851 if((phy_buf + phy_len) != tmp) { 1852 if(++dm >= &reqp->dm_chain[MAXDMAIO]) { 1853 printf("ncr5380_dmaok: DMA chain too long!\n"); 1854 return(0); 1855 } 1856 dm->dm_count = 0; 1857 dm->dm_addr = tmp; 1858 } 1859 phy_buf = tmp; 1860 } 1861 } 1862 reqp->dm_last = dm; 1863 return(1); 1864 } 1865 1866 static void 1867 run_main(void) 1868 { 1869 int sps = splbio(); 1870 1871 callback_scheduled = 0; 1872 if(!main_running) { 1873 main_running = 1; 1874 splx(sps); 1875 scsi_main(); 1876 } 1877 else splx(sps); 1878 } 1879 1880 /**************************************************************************** 1881 * Start Debugging Functions * 1882 ****************************************************************************/ 1883 static void 1884 show_data_sense(xs) 1885 struct scsi_xfer *xs; 1886 { 1887 u_char *b; 1888 int i; 1889 int sz; 1890 1891 b = (u_char *) xs->cmd; 1892 printf("cmd[%d,%d]: ", xs->cmdlen, sz = command_size(*b)); 1893 for(i = 0; i < sz; i++) 1894 printf("%x ", b[i]); 1895 printf("\nsense: "); 1896 b = (u_char *)&xs->sense; 1897 for(i = 0; i < sizeof(xs->sense); i++) 1898 printf("%x ", b[i]); 1899 printf("\n"); 1900 } 1901 1902 static void 1903 show_request(reqp, qtxt) 1904 SC_REQ *reqp; 1905 char *qtxt; 1906 { 1907 printf("REQ-%s: %d %x[%d] cmd[0]=%x S=%x M=%x R=%x %s\n", qtxt, 1908 reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len, 1909 reqp->xcmd.opcode, reqp->status, reqp->message, 1910 reqp->xs->error, reqp->link ? "L" : ""); 1911 if(reqp->status == SCSCHKC) 1912 show_data_sense(reqp->xs); 1913 } 1914 1915 scsi_show() 1916 { 1917 SC_REQ *tmp; 1918 int sps = splhigh(); 1919 1920 #ifndef DBG_PID 1921 #define last_hit "" 1922 #endif 1923 1924 for(tmp = issue_q; tmp; tmp = tmp->next) 1925 show_request(tmp, "ISSUED"); 1926 for(tmp = discon_q; tmp; tmp = tmp->next) 1927 show_request(tmp, "DISCONNECTED"); 1928 if(connected) 1929 show_request(connected, "CONNECTED"); 1930 /* show_signals(); */ 1931 if(connected) 1932 printf("phase = %d, ", connected->phase); 1933 printf("busy:%x, last_hit:%s, spl:%04x\n", busy, last_hit, sps); 1934 1935 splx(sps); 1936 } 1937