1 /* $OpenBSD: adw.c,v 1.60 2020/02/18 20:24:52 krw Exp $ */ 2 /* $NetBSD: adw.c,v 1.23 2000/05/27 18:24:50 dante Exp $ */ 3 4 /* 5 * Generic driver for the Advanced Systems Inc. SCSI controllers 6 * 7 * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc. 8 * All rights reserved. 9 * 10 * Author: Baldassare Dante Profeta <dante@mclink.it> 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/errno.h> 38 #include <sys/device.h> 39 #include <sys/malloc.h> 40 #include <sys/buf.h> 41 #include <sys/timeout.h> 42 43 #include <machine/bus.h> 44 #include <machine/intr.h> 45 46 #include <scsi/scsi_all.h> 47 #include <scsi/scsiconf.h> 48 49 #include <dev/ic/adwlib.h> 50 #include <dev/microcode/adw/adwmcode.h> 51 #include <dev/ic/adw.h> 52 53 /******************************************************************************/ 54 55 56 int adw_alloc_controls(ADW_SOFTC *); 57 int adw_alloc_carriers(ADW_SOFTC *); 58 int adw_create_ccbs(ADW_SOFTC *, ADW_CCB *, int); 59 void adw_ccb_free(void *, void *); 60 void adw_reset_ccb(ADW_CCB *); 61 int adw_init_ccb(ADW_SOFTC *, ADW_CCB *); 62 void *adw_ccb_alloc(void *); 63 int adw_queue_ccb(ADW_SOFTC *, ADW_CCB *, int); 64 65 void adw_scsi_cmd(struct scsi_xfer *); 66 int adw_build_req(struct scsi_xfer *, ADW_CCB *, int); 67 void adw_build_sglist(ADW_CCB *, ADW_SCSI_REQ_Q *, ADW_SG_BLOCK *); 68 void adw_isr_callback(ADW_SOFTC *, ADW_SCSI_REQ_Q *); 69 void adw_async_callback(ADW_SOFTC *, u_int8_t); 70 71 void adw_print_info(ADW_SOFTC *, int); 72 73 int adw_poll(ADW_SOFTC *, struct scsi_xfer *, int); 74 void adw_timeout(void *); 75 void adw_reset_bus(ADW_SOFTC *); 76 77 78 /******************************************************************************/ 79 80 81 struct cfdriver adw_cd = { 82 NULL, "adw", DV_DULL 83 }; 84 85 struct scsi_adapter adw_switch = { 86 adw_scsi_cmd, NULL, NULL, NULL, NULL 87 }; 88 89 /******************************************************************************/ 90 /* DMA Mapping for Control Blocks */ 91 /******************************************************************************/ 92 93 94 int 95 adw_alloc_controls(ADW_SOFTC *sc) 96 { 97 bus_dma_segment_t seg; 98 int error, rseg; 99 100 /* 101 * Allocate the control structure. 102 */ 103 if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct adw_control), 104 NBPG, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT | BUS_DMA_ZERO)) != 0) { 105 printf("%s: unable to allocate control structures," 106 " error = %d\n", sc->sc_dev.dv_xname, error); 107 return (error); 108 } 109 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, 110 sizeof(struct adw_control), (caddr_t *) & sc->sc_control, 111 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) { 112 printf("%s: unable to map control structures, error = %d\n", 113 sc->sc_dev.dv_xname, error); 114 return (error); 115 } 116 117 /* 118 * Create and load the DMA map used for the control blocks. 119 */ 120 if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct adw_control), 121 1, sizeof(struct adw_control), 0, BUS_DMA_NOWAIT, 122 &sc->sc_dmamap_control)) != 0) { 123 printf("%s: unable to create control DMA map, error = %d\n", 124 sc->sc_dev.dv_xname, error); 125 return (error); 126 } 127 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control, 128 sc->sc_control, sizeof(struct adw_control), NULL, 129 BUS_DMA_NOWAIT)) != 0) { 130 printf("%s: unable to load control DMA map, error = %d\n", 131 sc->sc_dev.dv_xname, error); 132 return (error); 133 } 134 135 return (0); 136 } 137 138 139 int 140 adw_alloc_carriers(ADW_SOFTC *sc) 141 { 142 bus_dma_segment_t seg; 143 int error, rseg; 144 145 /* 146 * Allocate the control structure. 147 */ 148 sc->sc_control->carriers = 149 malloc(ADW_MAX_CARRIER * sizeof(ADW_CARRIER), M_DEVBUF, 150 M_NOWAIT); 151 if (sc->sc_control->carriers == NULL) 152 return (ENOMEM); 153 154 155 if ((error = bus_dmamem_alloc(sc->sc_dmat, 156 sizeof(ADW_CARRIER) * ADW_MAX_CARRIER, 157 0x10, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) { 158 printf("%s: unable to allocate carrier structures," 159 " error = %d\n", sc->sc_dev.dv_xname, error); 160 return (error); 161 } 162 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, 163 sizeof(ADW_CARRIER) * ADW_MAX_CARRIER, 164 (caddr_t *) &sc->sc_control->carriers, 165 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) { 166 printf("%s: unable to map carrier structures," 167 " error = %d\n", sc->sc_dev.dv_xname, error); 168 return (error); 169 } 170 171 /* 172 * Create and load the DMA map used for the control blocks. 173 */ 174 if ((error = bus_dmamap_create(sc->sc_dmat, 175 sizeof(ADW_CARRIER) * ADW_MAX_CARRIER, 1, 176 sizeof(ADW_CARRIER) * ADW_MAX_CARRIER, 0,BUS_DMA_NOWAIT, 177 &sc->sc_dmamap_carrier)) != 0) { 178 printf("%s: unable to create carriers DMA map," 179 " error = %d\n", sc->sc_dev.dv_xname, error); 180 return (error); 181 } 182 if ((error = bus_dmamap_load(sc->sc_dmat, 183 sc->sc_dmamap_carrier, sc->sc_control->carriers, 184 sizeof(ADW_CARRIER) * ADW_MAX_CARRIER, NULL, 185 BUS_DMA_NOWAIT)) != 0) { 186 printf("%s: unable to load carriers DMA map," 187 " error = %d\n", sc->sc_dev.dv_xname, error); 188 return (error); 189 } 190 191 return (0); 192 } 193 194 195 /******************************************************************************/ 196 /* Control Blocks routines */ 197 /******************************************************************************/ 198 199 200 /* 201 * Create a set of ccbs and add them to the free list. Called once 202 * by adw_init(). We return the number of CCBs successfully created. 203 */ 204 int 205 adw_create_ccbs(ADW_SOFTC *sc, ADW_CCB *ccbstore, int count) 206 { 207 ADW_CCB *ccb; 208 int i, error; 209 210 for (i = 0; i < count; i++) { 211 ccb = &ccbstore[i]; 212 if ((error = adw_init_ccb(sc, ccb)) != 0) { 213 printf("%s: unable to initialize ccb, error = %d\n", 214 sc->sc_dev.dv_xname, error); 215 return (i); 216 } 217 TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain); 218 } 219 220 return (i); 221 } 222 223 224 /* 225 * A ccb is put onto the free list. 226 */ 227 void 228 adw_ccb_free(void *xsc, void *xccb) 229 { 230 ADW_SOFTC *sc = xsc; 231 ADW_CCB *ccb = xccb; 232 233 adw_reset_ccb(ccb); 234 235 mtx_enter(&sc->sc_ccb_mtx); 236 TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain); 237 mtx_leave(&sc->sc_ccb_mtx); 238 } 239 240 241 void 242 adw_reset_ccb(ADW_CCB *ccb) 243 { 244 245 ccb->flags = 0; 246 } 247 248 249 int 250 adw_init_ccb(ADW_SOFTC *sc, ADW_CCB *ccb) 251 { 252 int hashnum, error; 253 254 /* 255 * Create the DMA map for this CCB. 256 */ 257 error = bus_dmamap_create(sc->sc_dmat, 258 (ADW_MAX_SG_LIST - 1) * PAGE_SIZE, 259 ADW_MAX_SG_LIST, (ADW_MAX_SG_LIST - 1) * PAGE_SIZE, 260 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ccb->dmamap_xfer); 261 if (error) { 262 printf("%s: unable to create CCB DMA map, error = %d\n", 263 sc->sc_dev.dv_xname, error); 264 return (error); 265 } 266 267 /* 268 * put in the phystokv hash table 269 * Never gets taken out. 270 */ 271 ccb->hashkey = sc->sc_dmamap_control->dm_segs[0].ds_addr + 272 ADW_CCB_OFF(ccb); 273 hashnum = CCB_HASH(ccb->hashkey); 274 ccb->nexthash = sc->sc_ccbhash[hashnum]; 275 sc->sc_ccbhash[hashnum] = ccb; 276 adw_reset_ccb(ccb); 277 return (0); 278 } 279 280 281 /* 282 * Get a free ccb 283 * 284 * If there are none, see if we can allocate a new one 285 */ 286 void * 287 adw_ccb_alloc(void *xsc) 288 { 289 ADW_SOFTC *sc = xsc; 290 ADW_CCB *ccb; 291 292 mtx_enter(&sc->sc_ccb_mtx); 293 ccb = TAILQ_FIRST(&sc->sc_free_ccb); 294 if (ccb) { 295 TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain); 296 ccb->flags |= CCB_ALLOC; 297 } 298 mtx_leave(&sc->sc_ccb_mtx); 299 300 return (ccb); 301 } 302 303 304 /* 305 * Given a physical address, find the ccb that it corresponds to. 306 */ 307 ADW_CCB * 308 adw_ccb_phys_kv(ADW_SOFTC *sc, u_int32_t ccb_phys) 309 { 310 int hashnum = CCB_HASH(ccb_phys); 311 ADW_CCB *ccb = sc->sc_ccbhash[hashnum]; 312 313 while (ccb) { 314 if (ccb->hashkey == ccb_phys) 315 break; 316 ccb = ccb->nexthash; 317 } 318 return (ccb); 319 } 320 321 322 /* 323 * Queue a CCB to be sent to the controller, and send it if possible. 324 */ 325 int 326 adw_queue_ccb(ADW_SOFTC *sc, ADW_CCB *ccb, int retry) 327 { 328 int errcode = ADW_SUCCESS; 329 330 if(!retry) { 331 TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain); 332 } 333 334 while ((ccb = TAILQ_FIRST(&sc->sc_waiting_ccb)) != NULL) { 335 336 errcode = AdwExeScsiQueue(sc, &ccb->scsiq); 337 switch(errcode) { 338 case ADW_SUCCESS: 339 break; 340 341 case ADW_BUSY: 342 printf("ADW_BUSY\n"); 343 return(ADW_BUSY); 344 345 case ADW_ERROR: 346 printf("ADW_ERROR\n"); 347 TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain); 348 return(ADW_ERROR); 349 } 350 351 TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain); 352 TAILQ_INSERT_TAIL(&sc->sc_pending_ccb, ccb, chain); 353 354 /* ALWAYS initialize stimeout, lest it contain garbage! */ 355 timeout_set(&ccb->xs->stimeout, adw_timeout, ccb); 356 if ((ccb->xs->flags & SCSI_POLL) == 0) 357 timeout_add_msec(&ccb->xs->stimeout, ccb->timeout); 358 } 359 360 return(errcode); 361 } 362 363 364 /******************************************************************************/ 365 /* SCSI layer interfacing routines */ 366 /******************************************************************************/ 367 368 369 int 370 adw_init(ADW_SOFTC *sc) 371 { 372 u_int16_t warn_code; 373 374 375 sc->cfg.lib_version = (ADW_LIB_VERSION_MAJOR << 8) | 376 ADW_LIB_VERSION_MINOR; 377 sc->cfg.chip_version = 378 ADW_GET_CHIP_VERSION(sc->sc_iot, sc->sc_ioh, sc->bus_type); 379 380 /* 381 * Reset the chip to start and allow register writes. 382 */ 383 if (ADW_FIND_SIGNATURE(sc->sc_iot, sc->sc_ioh) == 0) { 384 panic("adw_init: adw_find_signature failed"); 385 } else { 386 AdwResetChip(sc->sc_iot, sc->sc_ioh); 387 388 warn_code = AdwInitFromEEPROM(sc); 389 390 if (warn_code & ADW_WARN_EEPROM_CHKSUM) 391 printf("%s: Bad checksum found. " 392 "Setting default values\n", 393 sc->sc_dev.dv_xname); 394 if (warn_code & ADW_WARN_EEPROM_TERMINATION) 395 printf("%s: Bad bus termination setting." 396 "Using automatic termination.\n", 397 sc->sc_dev.dv_xname); 398 } 399 400 sc->isr_callback = (ADW_CALLBACK) adw_isr_callback; 401 sc->async_callback = (ADW_CALLBACK) adw_async_callback; 402 403 return 0; 404 } 405 406 407 void 408 adw_attach(ADW_SOFTC *sc) 409 { 410 struct scsibus_attach_args saa; 411 int i, error; 412 413 414 TAILQ_INIT(&sc->sc_free_ccb); 415 TAILQ_INIT(&sc->sc_waiting_ccb); 416 TAILQ_INIT(&sc->sc_pending_ccb); 417 418 mtx_init(&sc->sc_ccb_mtx, IPL_BIO); 419 scsi_iopool_init(&sc->sc_iopool, sc, adw_ccb_alloc, adw_ccb_free); 420 421 /* 422 * Allocate the Control Blocks. 423 */ 424 error = adw_alloc_controls(sc); 425 if (error) 426 return; /* (error) */ ; 427 428 /* 429 * Create and initialize the Control Blocks. 430 */ 431 i = adw_create_ccbs(sc, sc->sc_control->ccbs, ADW_MAX_CCB); 432 if (i == 0) { 433 printf("%s: unable to create Control Blocks\n", 434 sc->sc_dev.dv_xname); 435 return; /* (ENOMEM) */ ; 436 } else if (i != ADW_MAX_CCB) { 437 printf("%s: WARNING: only %d of %d Control Blocks" 438 " created\n", 439 sc->sc_dev.dv_xname, i, ADW_MAX_CCB); 440 } 441 442 /* 443 * Create and initialize the Carriers. 444 */ 445 error = adw_alloc_carriers(sc); 446 if (error) 447 return; /* (error) */ ; 448 449 /* 450 * Zero's the freeze_device status 451 */ 452 bzero(sc->sc_freeze_dev, sizeof(sc->sc_freeze_dev)); 453 454 /* 455 * Initialize the adapter 456 */ 457 switch (AdwInitDriver(sc)) { 458 case ADW_IERR_BIST_PRE_TEST: 459 panic("%s: BIST pre-test error", 460 sc->sc_dev.dv_xname); 461 break; 462 463 case ADW_IERR_BIST_RAM_TEST: 464 panic("%s: BIST RAM test error", 465 sc->sc_dev.dv_xname); 466 break; 467 468 case ADW_IERR_MCODE_CHKSUM: 469 panic("%s: Microcode checksum error", 470 sc->sc_dev.dv_xname); 471 break; 472 473 case ADW_IERR_ILLEGAL_CONNECTION: 474 panic("%s: All three connectors are in use", 475 sc->sc_dev.dv_xname); 476 break; 477 478 case ADW_IERR_REVERSED_CABLE: 479 panic("%s: Cable is reversed", 480 sc->sc_dev.dv_xname); 481 break; 482 483 case ADW_IERR_HVD_DEVICE: 484 panic("%s: HVD attached to LVD connector", 485 sc->sc_dev.dv_xname); 486 break; 487 488 case ADW_IERR_SINGLE_END_DEVICE: 489 panic("%s: single-ended device is attached to" 490 " one of the connectors", 491 sc->sc_dev.dv_xname); 492 break; 493 494 case ADW_IERR_NO_CARRIER: 495 panic("%s: unable to create Carriers", 496 sc->sc_dev.dv_xname); 497 break; 498 499 case ADW_WARN_BUSRESET_ERROR: 500 printf("%s: WARNING: Bus Reset Error\n", 501 sc->sc_dev.dv_xname); 502 break; 503 } 504 505 /* 506 * fill in the prototype scsi_link. 507 */ 508 sc->sc_link.adapter_softc = sc; 509 sc->sc_link.adapter_target = sc->chip_scsi_id; 510 sc->sc_link.adapter = &adw_switch; 511 sc->sc_link.openings = 4; 512 sc->sc_link.adapter_buswidth = ADW_MAX_TID+1; 513 sc->sc_link.pool = &sc->sc_iopool; 514 515 bzero(&saa, sizeof(saa)); 516 saa.saa_sc_link = &sc->sc_link; 517 518 config_found(&sc->sc_dev, &saa, scsiprint); 519 } 520 521 522 /* 523 * start a scsi operation given the command and the data address. 524 * Also needs the unit, target and lu. 525 */ 526 void 527 adw_scsi_cmd(struct scsi_xfer *xs) 528 { 529 struct scsi_link *sc_link = xs->sc_link; 530 ADW_SOFTC *sc = sc_link->adapter_softc; 531 ADW_CCB *ccb; 532 int s, nowait = 0, retry = 0; 533 int flags; 534 535 /* 536 * get a ccb to use. If the transfer 537 * is from a buf (possibly from interrupt time) 538 * then we can't allow it to sleep 539 */ 540 541 flags = xs->flags; 542 if (nowait) 543 flags |= SCSI_NOSLEEP; 544 ccb = xs->io; 545 546 ccb->xs = xs; 547 ccb->timeout = xs->timeout; 548 549 if (adw_build_req(xs, ccb, flags)) { 550 retryagain: 551 s = splbio(); 552 retry = adw_queue_ccb(sc, ccb, retry); 553 splx(s); 554 555 switch(retry) { 556 case ADW_BUSY: 557 goto retryagain; 558 559 case ADW_ERROR: 560 xs->error = XS_DRIVER_STUFFUP; 561 scsi_done(xs); 562 return; 563 } 564 565 /* 566 * Usually return SUCCESSFULLY QUEUED 567 */ 568 if ((xs->flags & SCSI_POLL) == 0) 569 return; 570 571 /* 572 * If we can't use interrupts, poll on completion 573 */ 574 if (adw_poll(sc, xs, ccb->timeout)) { 575 adw_timeout(ccb); 576 if (adw_poll(sc, xs, ccb->timeout)) 577 adw_timeout(ccb); 578 } 579 } else { 580 /* adw_build_req() has set xs->error already */ 581 scsi_done(xs); 582 } 583 } 584 585 586 /* 587 * Build a request structure for the Wide Boards. 588 */ 589 int 590 adw_build_req(struct scsi_xfer *xs, ADW_CCB *ccb, int flags) 591 { 592 struct scsi_link *sc_link = xs->sc_link; 593 ADW_SOFTC *sc = sc_link->adapter_softc; 594 bus_dma_tag_t dmat = sc->sc_dmat; 595 ADW_SCSI_REQ_Q *scsiqp; 596 int error; 597 598 scsiqp = &ccb->scsiq; 599 bzero(scsiqp, sizeof(ADW_SCSI_REQ_Q)); 600 601 /* 602 * Set the ADW_SCSI_REQ_Q 'ccb_ptr' to point to the 603 * physical CCB structure. 604 */ 605 scsiqp->ccb_ptr = ccb->hashkey; 606 607 /* 608 * Build the ADW_SCSI_REQ_Q request. 609 */ 610 611 /* 612 * Set CDB length and copy it to the request structure. 613 * For wide boards a CDB length maximum of 16 bytes 614 * is supported. 615 */ 616 scsiqp->cdb_len = xs->cmdlen; 617 bcopy((caddr_t)xs->cmd, &scsiqp->cdb, 12); 618 bcopy((caddr_t)xs->cmd + 12, &scsiqp->cdb16, 4); 619 620 scsiqp->target_id = sc_link->target; 621 scsiqp->target_lun = sc_link->lun; 622 623 scsiqp->vsense_addr = &ccb->scsi_sense; 624 scsiqp->sense_addr = sc->sc_dmamap_control->dm_segs[0].ds_addr + 625 ADW_CCB_OFF(ccb) + offsetof(struct adw_ccb, scsi_sense); 626 scsiqp->sense_len = sizeof(struct scsi_sense_data); 627 628 /* 629 * Build ADW_SCSI_REQ_Q for a scatter-gather buffer command. 630 */ 631 if (xs->datalen) { 632 /* 633 * Map the DMA transfer. 634 */ 635 error = bus_dmamap_load(dmat, 636 ccb->dmamap_xfer, xs->data, xs->datalen, NULL, 637 (flags & SCSI_NOSLEEP) ? 638 BUS_DMA_NOWAIT : BUS_DMA_WAITOK); 639 640 if (error) { 641 if (error == EFBIG) { 642 printf("%s: adw_scsi_cmd, more than %d dma" 643 " segments\n", 644 sc->sc_dev.dv_xname, ADW_MAX_SG_LIST); 645 } else { 646 printf("%s: adw_scsi_cmd, error %d loading" 647 " dma map\n", 648 sc->sc_dev.dv_xname, error); 649 } 650 651 xs->error = XS_DRIVER_STUFFUP; 652 return (0); 653 } 654 bus_dmamap_sync(dmat, ccb->dmamap_xfer, 655 0, ccb->dmamap_xfer->dm_mapsize, 656 (xs->flags & SCSI_DATA_IN) ? 657 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 658 659 /* 660 * Build scatter-gather list. 661 */ 662 scsiqp->data_cnt = xs->datalen; 663 scsiqp->vdata_addr = xs->data; 664 scsiqp->data_addr = ccb->dmamap_xfer->dm_segs[0].ds_addr; 665 bzero(ccb->sg_block, sizeof(ADW_SG_BLOCK) * ADW_NUM_SG_BLOCK); 666 adw_build_sglist(ccb, scsiqp, ccb->sg_block); 667 } else { 668 /* 669 * No data xfer, use non S/G values. 670 */ 671 scsiqp->data_cnt = 0; 672 scsiqp->vdata_addr = 0; 673 scsiqp->data_addr = 0; 674 } 675 676 return (1); 677 } 678 679 680 /* 681 * Build scatter-gather list for Wide Boards. 682 */ 683 void 684 adw_build_sglist(ADW_CCB *ccb, ADW_SCSI_REQ_Q *scsiqp, ADW_SG_BLOCK *sg_block) 685 { 686 u_long sg_block_next_addr; /* block and its next */ 687 u_int32_t sg_block_physical_addr; 688 int i; /* how many SG entries */ 689 bus_dma_segment_t *sg_list = &ccb->dmamap_xfer->dm_segs[0]; 690 int sg_elem_cnt = ccb->dmamap_xfer->dm_nsegs; 691 692 693 sg_block_next_addr = (u_long) sg_block; /* allow math operation */ 694 sg_block_physical_addr = ccb->hashkey + 695 offsetof(struct adw_ccb, sg_block[0]); 696 scsiqp->sg_real_addr = sg_block_physical_addr; 697 698 /* 699 * If there are more than NO_OF_SG_PER_BLOCK dma segments (hw sg-list) 700 * then split the request into multiple sg-list blocks. 701 */ 702 703 do { 704 for (i = 0; i < NO_OF_SG_PER_BLOCK; i++) { 705 sg_block->sg_list[i].sg_addr = sg_list->ds_addr; 706 sg_block->sg_list[i].sg_count = sg_list->ds_len; 707 708 if (--sg_elem_cnt == 0) { 709 /* last entry, get out */ 710 sg_block->sg_cnt = i + 1; 711 sg_block->sg_ptr = 0; /* next link = NULL */ 712 return; 713 } 714 sg_list++; 715 } 716 sg_block_next_addr += sizeof(ADW_SG_BLOCK); 717 sg_block_physical_addr += sizeof(ADW_SG_BLOCK); 718 719 sg_block->sg_cnt = NO_OF_SG_PER_BLOCK; 720 sg_block->sg_ptr = sg_block_physical_addr; 721 sg_block = (ADW_SG_BLOCK *) sg_block_next_addr; /* virt. addr */ 722 } while (1); 723 } 724 725 726 /******************************************************************************/ 727 /* Interrupts and TimeOut routines */ 728 /******************************************************************************/ 729 730 731 int 732 adw_intr(void *arg) 733 { 734 ADW_SOFTC *sc = arg; 735 736 737 if(AdwISR(sc) != ADW_FALSE) { 738 return (1); 739 } 740 741 return (0); 742 } 743 744 745 /* 746 * Poll a particular unit, looking for a particular xs 747 */ 748 int 749 adw_poll(ADW_SOFTC *sc, struct scsi_xfer *xs, int count) 750 { 751 int s; 752 753 /* timeouts are in msec, so we loop in 1000 usec cycles */ 754 while (count > 0) { 755 s = splbio(); 756 adw_intr(sc); 757 splx(s); 758 if (xs->flags & ITSDONE) { 759 if ((xs->cmd->opcode == INQUIRY) 760 && (xs->sc_link->lun == 0) 761 && (xs->error == XS_NOERROR)) 762 adw_print_info(sc, xs->sc_link->target); 763 return (0); 764 } 765 delay(1000); /* only happens in boot so ok */ 766 count--; 767 } 768 return (1); 769 } 770 771 772 void 773 adw_timeout(void *arg) 774 { 775 ADW_CCB *ccb = arg; 776 struct scsi_xfer *xs = ccb->xs; 777 struct scsi_link *sc_link = xs->sc_link; 778 ADW_SOFTC *sc = sc_link->adapter_softc; 779 int s; 780 781 sc_print_addr(sc_link); 782 printf("timed out"); 783 784 s = splbio(); 785 786 if (ccb->flags & CCB_ABORTED) { 787 /* 788 * Abort Timed Out 789 * 790 * No more opportunities. Lets try resetting the bus and 791 * reinitialize the host adapter. 792 */ 793 timeout_del(&xs->stimeout); 794 printf(" AGAIN. Resetting SCSI Bus\n"); 795 adw_reset_bus(sc); 796 splx(s); 797 return; 798 } else if (ccb->flags & CCB_ABORTING) { 799 /* 800 * Abort the operation that has timed out. 801 * 802 * Second opportunity. 803 */ 804 printf("\n"); 805 xs->error = XS_TIMEOUT; 806 ccb->flags |= CCB_ABORTED; 807 #if 0 808 /* 809 * - XXX - 3.3a microcode is BROKEN!!! 810 * 811 * We cannot abort a CCB, so we can only hope the command 812 * get completed before the next timeout, otherwise a 813 * Bus Reset will arrive inexorably. 814 */ 815 /* 816 * ADW_ABORT_CCB() makes the board to generate an interrupt 817 * 818 * - XXX - The above assertion MUST be verified (and this 819 * code changed as well [callout_*()]), when the 820 * ADW_ABORT_CCB will be working again 821 */ 822 ADW_ABORT_CCB(sc, ccb); 823 #endif 824 /* 825 * waiting for multishot callout_reset() let's restart it 826 * by hand so the next time a timeout event will occur 827 * we will reset the bus. 828 */ 829 timeout_add_msec(&xs->stimeout, ccb->timeout); 830 } else { 831 /* 832 * Abort the operation that has timed out. 833 * 834 * First opportunity. 835 */ 836 printf("\n"); 837 xs->error = XS_TIMEOUT; 838 ccb->flags |= CCB_ABORTING; 839 #if 0 840 /* 841 * - XXX - 3.3a microcode is BROKEN!!! 842 * 843 * We cannot abort a CCB, so we can only hope the command 844 * get completed before the next 2 timeout, otherwise a 845 * Bus Reset will arrive inexorably. 846 */ 847 /* 848 * ADW_ABORT_CCB() makes the board to generate an interrupt 849 * 850 * - XXX - The above assertion MUST be verified (and this 851 * code changed as well [callout_*()]), when the 852 * ADW_ABORT_CCB will be working again 853 */ 854 ADW_ABORT_CCB(sc, ccb); 855 #endif 856 /* 857 * waiting for multishot callout_reset() let's restart it 858 * by hand so to give a second opportunity to the command 859 * which timed-out. 860 */ 861 timeout_add_msec(&xs->stimeout, ccb->timeout); 862 } 863 864 splx(s); 865 } 866 867 868 void 869 adw_reset_bus(ADW_SOFTC *sc) 870 { 871 ADW_CCB *ccb; 872 int s; 873 874 s = splbio(); 875 AdwResetSCSIBus(sc); /* XXX - should check return value? */ 876 while((ccb = TAILQ_LAST(&sc->sc_pending_ccb, 877 adw_pending_ccb)) != NULL) { 878 timeout_del(&ccb->xs->stimeout); 879 TAILQ_REMOVE(&sc->sc_pending_ccb, ccb, chain); 880 TAILQ_INSERT_HEAD(&sc->sc_waiting_ccb, ccb, chain); 881 } 882 883 bzero(sc->sc_freeze_dev, sizeof(sc->sc_freeze_dev)); 884 adw_queue_ccb(sc, TAILQ_FIRST(&sc->sc_waiting_ccb), 1); 885 886 splx(s); 887 } 888 889 890 /******************************************************************************/ 891 /* Host Adapter and Peripherals Information Routines */ 892 /******************************************************************************/ 893 894 895 void 896 adw_print_info(ADW_SOFTC *sc, int tid) 897 { 898 bus_space_handle_t ioh = sc->sc_ioh; 899 bus_space_tag_t iot = sc->sc_iot; 900 u_int16_t hshk_cfg, able_mask, period = 0; 901 902 /* hshk/HSHK means 'handskake' */ 903 904 ADW_READ_WORD_LRAM(iot, ioh, 905 ADW_MC_DEVICE_HSHK_CFG_TABLE + (2 * tid), hshk_cfg); 906 907 ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE, able_mask); 908 if ((able_mask & ADW_TID_TO_TIDMASK(tid)) == 0) 909 hshk_cfg &= ~HSHK_CFG_WIDE_XFR; 910 911 ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, able_mask); 912 if ((able_mask & ADW_TID_TO_TIDMASK(tid)) == 0) 913 hshk_cfg &= ~HSHK_CFG_OFFSET; 914 915 printf("%s: target %d using %d bit ", sc->sc_dev.dv_xname, tid, 916 (hshk_cfg & HSHK_CFG_WIDE_XFR) ? 16 : 8); 917 918 if ((hshk_cfg & HSHK_CFG_OFFSET) == 0) 919 printf("async "); 920 else { 921 period = (hshk_cfg & 0x1f00) >> 8; 922 switch (period) { 923 case 0x11: 924 printf("80.0 "); 925 break; 926 case 0x10: 927 printf("40.0 "); 928 break; 929 default: 930 period = (period * 25) + 50; 931 printf("%d.%d ", 1000/period, ADW_TENTHS(1000, period)); 932 break; 933 } 934 printf("MHz %d REQ/ACK offset ", hshk_cfg & HSHK_CFG_OFFSET); 935 } 936 937 printf("xfers\n"); 938 } 939 940 941 /******************************************************************************/ 942 /* WIDE boards Interrupt callbacks */ 943 /******************************************************************************/ 944 945 946 /* 947 * adw_isr_callback() - Second Level Interrupt Handler called by AdwISR() 948 * 949 * Interrupt callback function for the Wide SCSI Adw Library. 950 * 951 * Notice: 952 * Interrupts are disabled by the caller (AdwISR() function), and will be 953 * enabled at the end of the caller. 954 */ 955 void 956 adw_isr_callback(ADW_SOFTC *sc, ADW_SCSI_REQ_Q *scsiq) 957 { 958 bus_dma_tag_t dmat; 959 ADW_CCB *ccb; 960 struct scsi_xfer *xs; 961 struct scsi_sense_data *s1, *s2; 962 963 964 ccb = adw_ccb_phys_kv(sc, scsiq->ccb_ptr); 965 TAILQ_REMOVE(&sc->sc_pending_ccb, ccb, chain); 966 967 if ((ccb->flags & CCB_ALLOC) == 0) { 968 panic("%s: unallocated ccb found on pending list!", 969 sc->sc_dev.dv_xname); 970 return; 971 } 972 973 xs = ccb->xs; 974 timeout_del(&xs->stimeout); 975 976 /* 977 * If we were a data transfer, unload the map that described 978 * the data buffer. 979 */ 980 dmat = sc->sc_dmat; 981 if (xs->datalen) { 982 bus_dmamap_sync(dmat, ccb->dmamap_xfer, 983 0, ccb->dmamap_xfer->dm_mapsize, 984 ((xs->flags & SCSI_DATA_IN) ? 985 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE)); 986 bus_dmamap_unload(dmat, ccb->dmamap_xfer); 987 } 988 989 /* 990 * 'done_status' contains the command's ending status. 991 * 'host_status' contains the host adapter status. 992 * 'scsi_status' contains the scsi peripheral status. 993 */ 994 995 sc->sc_freeze_dev[scsiq->target_id] = 0; 996 xs->status = scsiq->scsi_status; 997 998 switch (scsiq->done_status) { 999 case QD_NO_ERROR: /* (scsi_status == 0) && (host_status == 0) */ 1000 NO_ERROR: 1001 xs->resid = scsiq->data_cnt; 1002 xs->error = XS_NOERROR; 1003 break; 1004 1005 case QD_WITH_ERROR: 1006 switch (scsiq->host_status) { 1007 case QHSTA_NO_ERROR: 1008 switch (scsiq->scsi_status) { 1009 case SCSI_COND_MET: 1010 case SCSI_INTERM: 1011 case SCSI_INTERM_COND_MET: 1012 /* 1013 * These non-zero status values are 1014 * not really error conditions. 1015 * 1016 * XXX - would it be too paranoid to 1017 * add SCSI_OK here in 1018 * case the docs are wrong re 1019 * QD_NO_ERROR? 1020 */ 1021 goto NO_ERROR; 1022 1023 case SCSI_CHECK: 1024 case SCSI_TERMINATED: 1025 case SCSI_ACA_ACTIVE: 1026 s1 = &ccb->scsi_sense; 1027 s2 = &xs->sense; 1028 *s2 = *s1; 1029 xs->error = XS_SENSE; 1030 break; 1031 1032 case SCSI_BUSY: 1033 case SCSI_QUEUE_FULL: 1034 case SCSI_RESV_CONFLICT: 1035 sc->sc_freeze_dev[scsiq->target_id] = 1; 1036 xs->error = XS_BUSY; 1037 break; 1038 1039 default: /* scsiq->scsi_status value */ 1040 printf("%s: bad scsi_status: 0x%02x.\n" 1041 ,sc->sc_dev.dv_xname 1042 ,scsiq->scsi_status); 1043 xs->error = XS_DRIVER_STUFFUP; 1044 break; 1045 } 1046 break; 1047 1048 case QHSTA_M_SEL_TIMEOUT: 1049 xs->error = XS_SELTIMEOUT; 1050 break; 1051 1052 case QHSTA_M_DIRECTION_ERR: 1053 case QHSTA_M_SXFR_OFF_UFLW: 1054 case QHSTA_M_SXFR_OFF_OFLW: 1055 case QHSTA_M_SXFR_XFR_OFLW: 1056 case QHSTA_M_QUEUE_ABORTED: 1057 case QHSTA_M_INVALID_DEVICE: 1058 case QHSTA_M_SGBACKUP_ERROR: 1059 case QHSTA_M_SXFR_DESELECTED: 1060 case QHSTA_M_SXFR_XFR_PH_ERR: 1061 case QHSTA_M_BUS_DEVICE_RESET: 1062 case QHSTA_M_NO_AUTO_REQ_SENSE: 1063 case QHSTA_M_BAD_CMPL_STATUS_IN: 1064 case QHSTA_M_SXFR_UNKNOWN_ERROR: 1065 case QHSTA_M_AUTO_REQ_SENSE_FAIL: 1066 case QHSTA_M_UNEXPECTED_BUS_FREE: 1067 printf("%s: host adapter error 0x%02x." 1068 " See adw(4).\n" 1069 ,sc->sc_dev.dv_xname, scsiq->host_status); 1070 xs->error = XS_DRIVER_STUFFUP; 1071 break; 1072 1073 case QHSTA_M_RDMA_PERR: 1074 case QHSTA_M_SXFR_WD_TMO: 1075 case QHSTA_M_WTM_TIMEOUT: 1076 case QHSTA_M_FROZEN_TIDQ: 1077 case QHSTA_M_SXFR_SDMA_ERR: 1078 case QHSTA_M_SXFR_SXFR_PERR: 1079 case QHSTA_M_SCSI_BUS_RESET: 1080 case QHSTA_M_DIRECTION_ERR_HUNG: 1081 case QHSTA_M_SCSI_BUS_RESET_UNSOL: 1082 /* 1083 * XXX - are all these cases really asking 1084 * for a card reset? _BUS_RESET and 1085 * _BUS_RESET_UNSOL added just to make 1086 * sure the pending queue is cleared out 1087 * in case card has lost track of them. 1088 */ 1089 printf("%s: host adapter error 0x%02x," 1090 " resetting bus. See adw(4).\n" 1091 ,sc->sc_dev.dv_xname, scsiq->host_status); 1092 adw_reset_bus(sc); 1093 xs->error = XS_RESET; 1094 break; 1095 1096 default: /* scsiq->host_status value */ 1097 /* 1098 * XXX - is a panic really appropriate here? If 1099 * not, would it be better to make the 1100 * XS_DRIVER_STUFFUP case above the 1101 * default behaviour? Or XS_RESET? 1102 */ 1103 panic("%s: bad host_status: 0x%02x" 1104 ,sc->sc_dev.dv_xname, scsiq->host_status); 1105 break; 1106 } 1107 break; 1108 1109 case QD_ABORTED_BY_HOST: 1110 xs->error = XS_DRIVER_STUFFUP; 1111 break; 1112 1113 default: /* scsiq->done_status value */ 1114 /* 1115 * XXX - would QD_NO_STATUS really mean the I/O is not 1116 * done? and would that mean it should somehow be 1117 * put back as a pending I/O? 1118 */ 1119 printf("%s: bad done_status: 0x%02x" 1120 " (host_status: 0x%02x, scsi_status: 0x%02x)\n" 1121 ,sc->sc_dev.dv_xname 1122 ,scsiq->done_status 1123 ,scsiq->host_status 1124 ,scsiq->scsi_status); 1125 xs->error = XS_DRIVER_STUFFUP; 1126 break; 1127 } 1128 1129 scsi_done(xs); 1130 } 1131 1132 1133 /* 1134 * adw_async_callback() - Adw Library asynchronous event callback function. 1135 */ 1136 void 1137 adw_async_callback(ADW_SOFTC *sc, u_int8_t code) 1138 { 1139 switch (code) { 1140 case ADW_ASYNC_SCSI_BUS_RESET_DET: 1141 /* The firmware detected a SCSI Bus reset. */ 1142 printf("%s: SCSI Bus reset detected\n", sc->sc_dev.dv_xname); 1143 break; 1144 1145 case ADW_ASYNC_RDMA_FAILURE: 1146 /* 1147 * Handle RDMA failure by resetting the SCSI Bus and 1148 * possibly the chip if it is unresponsive. 1149 */ 1150 printf("%s: RDMA failure. Resetting the SCSI Bus and" 1151 " the adapter\n", sc->sc_dev.dv_xname); 1152 adw_reset_bus(sc); 1153 break; 1154 1155 case ADW_HOST_SCSI_BUS_RESET: 1156 /* Host generated SCSI bus reset occurred. */ 1157 printf("%s: Host generated SCSI bus reset occurred\n", 1158 sc->sc_dev.dv_xname); 1159 break; 1160 1161 1162 case ADW_ASYNC_CARRIER_READY_FAILURE: 1163 /* 1164 * Carrier Ready failure. 1165 * 1166 * A warning only - RISC too busy to realize it's been 1167 * tickled. Occurs in normal operation under heavy 1168 * load, so a message is printed only when ADW_DEBUG'ing 1169 */ 1170 #ifdef ADW_DEBUG 1171 printf("%s: Carrier Ready failure!\n", sc->sc_dev.dv_xname); 1172 #endif 1173 break; 1174 1175 default: 1176 printf("%s: Unknown Async callback code (ignored): 0x%02x\n", 1177 sc->sc_dev.dv_xname, code); 1178 break; 1179 } 1180 } 1181