1 /* $NetBSD: adv.c,v 1.45 2010/11/13 13:52:00 uebayasi Exp $ */ 2 3 /* 4 * Generic driver for the Advanced Systems Inc. Narrow SCSI controllers 5 * 6 * Copyright (c) 1998 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * Author: Baldassare Dante Profeta <dante@mclink.it> 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: adv.c,v 1.45 2010/11/13 13:52:00 uebayasi Exp $"); 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/callout.h> 46 #include <sys/kernel.h> 47 #include <sys/errno.h> 48 #include <sys/ioctl.h> 49 #include <sys/device.h> 50 #include <sys/malloc.h> 51 #include <sys/buf.h> 52 #include <sys/proc.h> 53 54 #include <sys/bus.h> 55 #include <sys/intr.h> 56 57 #include <dev/scsipi/scsi_all.h> 58 #include <dev/scsipi/scsipi_all.h> 59 #include <dev/scsipi/scsiconf.h> 60 61 #include <dev/ic/advlib.h> 62 #include <dev/ic/adv.h> 63 64 #ifndef DDB 65 #define Debugger() panic("should call debugger here (adv.c)") 66 #endif /* ! DDB */ 67 68 69 /* #define ASC_DEBUG */ 70 71 /******************************************************************************/ 72 73 74 static int adv_alloc_control_data(ASC_SOFTC *); 75 static void adv_free_control_data(ASC_SOFTC *); 76 static int adv_create_ccbs(ASC_SOFTC *, ADV_CCB *, int); 77 static void adv_free_ccb(ASC_SOFTC *, ADV_CCB *); 78 static void adv_reset_ccb(ADV_CCB *); 79 static int adv_init_ccb(ASC_SOFTC *, ADV_CCB *); 80 static ADV_CCB *adv_get_ccb(ASC_SOFTC *); 81 static void adv_queue_ccb(ASC_SOFTC *, ADV_CCB *); 82 static void adv_start_ccbs(ASC_SOFTC *); 83 84 85 static void adv_scsipi_request(struct scsipi_channel *, 86 scsipi_adapter_req_t, void *); 87 static void advminphys(struct buf *); 88 static void adv_narrow_isr_callback(ASC_SOFTC *, ASC_QDONE_INFO *); 89 90 static int adv_poll(ASC_SOFTC *, struct scsipi_xfer *, int); 91 static void adv_timeout(void *); 92 static void adv_watchdog(void *); 93 94 95 /******************************************************************************/ 96 97 #define ADV_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */ 98 #define ADV_WATCH_TIMEOUT 1000 /* time to wait for watchdog (mSec) */ 99 100 /******************************************************************************/ 101 /* Control Blocks routines */ 102 /******************************************************************************/ 103 104 105 static int 106 adv_alloc_control_data(ASC_SOFTC *sc) 107 { 108 int error; 109 110 /* 111 * Allocate the control blocks. 112 */ 113 if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct adv_control), 114 PAGE_SIZE, 0, &sc->sc_control_seg, 1, 115 &sc->sc_control_nsegs, BUS_DMA_NOWAIT)) != 0) { 116 aprint_error_dev(&sc->sc_dev, "unable to allocate control structures," 117 " error = %d\n", error); 118 return (error); 119 } 120 if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_control_seg, 121 sc->sc_control_nsegs, sizeof(struct adv_control), 122 (void **) & sc->sc_control, 123 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) { 124 aprint_error_dev(&sc->sc_dev, "unable to map control structures, error = %d\n", 125 error); 126 return (error); 127 } 128 /* 129 * Create and load the DMA map used for the control blocks. 130 */ 131 if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct adv_control), 132 1, sizeof(struct adv_control), 0, BUS_DMA_NOWAIT, 133 &sc->sc_dmamap_control)) != 0) { 134 aprint_error_dev(&sc->sc_dev, "unable to create control DMA map, error = %d\n", 135 error); 136 return (error); 137 } 138 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control, 139 sc->sc_control, sizeof(struct adv_control), NULL, 140 BUS_DMA_NOWAIT)) != 0) { 141 aprint_error_dev(&sc->sc_dev, "unable to load control DMA map, error = %d\n", 142 error); 143 return (error); 144 } 145 146 /* 147 * Initialize the overrun_buf address. 148 */ 149 sc->overrun_buf = sc->sc_dmamap_control->dm_segs[0].ds_addr + 150 offsetof(struct adv_control, overrun_buf); 151 152 return (0); 153 } 154 155 static void 156 adv_free_control_data(ASC_SOFTC *sc) 157 { 158 159 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap_control); 160 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap_control); 161 sc->sc_dmamap_control = NULL; 162 163 bus_dmamem_unmap(sc->sc_dmat, (void *) sc->sc_control, 164 sizeof(struct adv_control)); 165 bus_dmamem_free(sc->sc_dmat, &sc->sc_control_seg, 166 sc->sc_control_nsegs); 167 } 168 169 /* 170 * Create a set of ccbs and add them to the free list. Called once 171 * by adv_init(). We return the number of CCBs successfully created. 172 */ 173 static int 174 adv_create_ccbs(ASC_SOFTC *sc, ADV_CCB *ccbstore, int count) 175 { 176 ADV_CCB *ccb; 177 int i, error; 178 179 memset(ccbstore, 0, sizeof(ADV_CCB) * count); 180 for (i = 0; i < count; i++) { 181 ccb = &ccbstore[i]; 182 if ((error = adv_init_ccb(sc, ccb)) != 0) { 183 aprint_error_dev(&sc->sc_dev, "unable to initialize ccb, error = %d\n", 184 error); 185 return (i); 186 } 187 TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain); 188 } 189 190 return (i); 191 } 192 193 194 /* 195 * A ccb is put onto the free list. 196 */ 197 static void 198 adv_free_ccb(ASC_SOFTC *sc, ADV_CCB *ccb) 199 { 200 int s; 201 202 s = splbio(); 203 adv_reset_ccb(ccb); 204 TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain); 205 splx(s); 206 } 207 208 209 static void 210 adv_reset_ccb(ADV_CCB *ccb) 211 { 212 213 ccb->flags = 0; 214 } 215 216 217 static int 218 adv_init_ccb(ASC_SOFTC *sc, ADV_CCB *ccb) 219 { 220 int hashnum, error; 221 222 callout_init(&ccb->ccb_watchdog, 0); 223 224 /* 225 * Create the DMA map for this CCB. 226 */ 227 error = bus_dmamap_create(sc->sc_dmat, 228 (ASC_MAX_SG_LIST - 1) * PAGE_SIZE, 229 ASC_MAX_SG_LIST, (ASC_MAX_SG_LIST - 1) * PAGE_SIZE, 230 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ccb->dmamap_xfer); 231 if (error) { 232 aprint_error_dev(&sc->sc_dev, "unable to create DMA map, error = %d\n", 233 error); 234 return (error); 235 } 236 237 /* 238 * put in the phystokv hash table 239 * Never gets taken out. 240 */ 241 ccb->hashkey = sc->sc_dmamap_control->dm_segs[0].ds_addr + 242 ADV_CCB_OFF(ccb); 243 hashnum = CCB_HASH(ccb->hashkey); 244 ccb->nexthash = sc->sc_ccbhash[hashnum]; 245 sc->sc_ccbhash[hashnum] = ccb; 246 247 adv_reset_ccb(ccb); 248 return (0); 249 } 250 251 252 /* 253 * Get a free ccb 254 * 255 * If there are none, see if we can allocate a new one 256 */ 257 static ADV_CCB * 258 adv_get_ccb(ASC_SOFTC *sc) 259 { 260 ADV_CCB *ccb = 0; 261 int s; 262 263 s = splbio(); 264 ccb = TAILQ_FIRST(&sc->sc_free_ccb); 265 if (ccb != NULL) { 266 TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain); 267 ccb->flags |= CCB_ALLOC; 268 } 269 splx(s); 270 return (ccb); 271 } 272 273 274 /* 275 * Given a physical address, find the ccb that it corresponds to. 276 */ 277 ADV_CCB * 278 adv_ccb_phys_kv(ASC_SOFTC *sc, u_long ccb_phys) 279 { 280 int hashnum = CCB_HASH(ccb_phys); 281 ADV_CCB *ccb = sc->sc_ccbhash[hashnum]; 282 283 while (ccb) { 284 if (ccb->hashkey == ccb_phys) 285 break; 286 ccb = ccb->nexthash; 287 } 288 return (ccb); 289 } 290 291 292 /* 293 * Queue a CCB to be sent to the controller, and send it if possible. 294 */ 295 static void 296 adv_queue_ccb(ASC_SOFTC *sc, ADV_CCB *ccb) 297 { 298 299 TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain); 300 301 adv_start_ccbs(sc); 302 } 303 304 305 static void 306 adv_start_ccbs(ASC_SOFTC *sc) 307 { 308 ADV_CCB *ccb; 309 310 while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) { 311 if (ccb->flags & CCB_WATCHDOG) 312 callout_stop(&ccb->ccb_watchdog); 313 314 if (AscExeScsiQueue(sc, &ccb->scsiq) == ASC_BUSY) { 315 ccb->flags |= CCB_WATCHDOG; 316 callout_reset(&ccb->ccb_watchdog, 317 (ADV_WATCH_TIMEOUT * hz) / 1000, 318 adv_watchdog, ccb); 319 break; 320 } 321 TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain); 322 323 if ((ccb->xs->xs_control & XS_CTL_POLL) == 0) 324 callout_reset(&ccb->xs->xs_callout, 325 mstohz(ccb->timeout), adv_timeout, ccb); 326 } 327 } 328 329 330 /******************************************************************************/ 331 /* SCSI layer interfacing routines */ 332 /******************************************************************************/ 333 334 335 int 336 adv_init(ASC_SOFTC *sc) 337 { 338 int warn; 339 340 if (!AscFindSignature(sc->sc_iot, sc->sc_ioh)) { 341 aprint_error("adv_init: failed to find signature\n"); 342 return (1); 343 } 344 345 /* 346 * Read the board configuration 347 */ 348 AscInitASC_SOFTC(sc); 349 warn = AscInitFromEEP(sc); 350 if (warn) { 351 aprint_error_dev(&sc->sc_dev, "-get: "); 352 switch (warn) { 353 case -1: 354 aprint_normal("Chip is not halted\n"); 355 break; 356 357 case -2: 358 aprint_normal("Couldn't get MicroCode Start" 359 " address\n"); 360 break; 361 362 case ASC_WARN_IO_PORT_ROTATE: 363 aprint_normal("I/O port address modified\n"); 364 break; 365 366 case ASC_WARN_AUTO_CONFIG: 367 aprint_normal("I/O port increment switch enabled\n"); 368 break; 369 370 case ASC_WARN_EEPROM_CHKSUM: 371 aprint_normal("EEPROM checksum error\n"); 372 break; 373 374 case ASC_WARN_IRQ_MODIFIED: 375 aprint_normal("IRQ modified\n"); 376 break; 377 378 case ASC_WARN_CMD_QNG_CONFLICT: 379 aprint_normal("tag queuing enabled w/o disconnects\n"); 380 break; 381 382 default: 383 aprint_normal("unknown warning %d\n", warn); 384 } 385 } 386 if (sc->scsi_reset_wait > ASC_MAX_SCSI_RESET_WAIT) 387 sc->scsi_reset_wait = ASC_MAX_SCSI_RESET_WAIT; 388 389 /* 390 * Modify the board configuration 391 */ 392 warn = AscInitFromASC_SOFTC(sc); 393 if (warn) { 394 aprint_error_dev(&sc->sc_dev, "-set: "); 395 switch (warn) { 396 case ASC_WARN_CMD_QNG_CONFLICT: 397 aprint_normal("tag queuing enabled w/o disconnects\n"); 398 break; 399 400 case ASC_WARN_AUTO_CONFIG: 401 aprint_normal("I/O port increment switch enabled\n"); 402 break; 403 404 default: 405 aprint_normal("unknown warning %d\n", warn); 406 } 407 } 408 sc->isr_callback = (ASC_CALLBACK) adv_narrow_isr_callback; 409 410 return (0); 411 } 412 413 414 void 415 adv_attach(ASC_SOFTC *sc) 416 { 417 struct scsipi_adapter *adapt = &sc->sc_adapter; 418 struct scsipi_channel *chan = &sc->sc_channel; 419 int i, error; 420 421 /* 422 * Initialize board RISC chip and enable interrupts. 423 */ 424 switch (AscInitDriver(sc)) { 425 case 0: 426 /* AllOK */ 427 break; 428 429 case 1: 430 panic("%s: bad signature", device_xname(&sc->sc_dev)); 431 break; 432 433 case 2: 434 panic("%s: unable to load MicroCode", 435 device_xname(&sc->sc_dev)); 436 break; 437 438 case 3: 439 panic("%s: unable to initialize MicroCode", 440 device_xname(&sc->sc_dev)); 441 break; 442 443 default: 444 panic("%s: unable to initialize board RISC chip", 445 device_xname(&sc->sc_dev)); 446 } 447 448 /* 449 * Fill in the scsipi_adapter. 450 */ 451 memset(adapt, 0, sizeof(*adapt)); 452 adapt->adapt_dev = &sc->sc_dev; 453 adapt->adapt_nchannels = 1; 454 /* adapt_openings initialized below */ 455 /* adapt_max_periph initialized below */ 456 adapt->adapt_request = adv_scsipi_request; 457 adapt->adapt_minphys = advminphys; 458 459 /* 460 * Fill in the scsipi_channel. 461 */ 462 memset(chan, 0, sizeof(*chan)); 463 chan->chan_adapter = adapt; 464 chan->chan_bustype = &scsi_bustype; 465 chan->chan_channel = 0; 466 chan->chan_ntargets = 8; 467 chan->chan_nluns = 8; 468 chan->chan_id = sc->chip_scsi_id; 469 470 TAILQ_INIT(&sc->sc_free_ccb); 471 TAILQ_INIT(&sc->sc_waiting_ccb); 472 473 /* 474 * Allocate the Control Blocks and the overrun buffer. 475 */ 476 error = adv_alloc_control_data(sc); 477 if (error) 478 return; /* (error) */ 479 480 /* 481 * Create and initialize the Control Blocks. 482 */ 483 i = adv_create_ccbs(sc, sc->sc_control->ccbs, ADV_MAX_CCB); 484 if (i == 0) { 485 aprint_error_dev(&sc->sc_dev, "unable to create control blocks\n"); 486 return; /* (ENOMEM) */ ; 487 } else if (i != ADV_MAX_CCB) { 488 aprint_error_dev(&sc->sc_dev, 489 "WARNING: only %d of %d control blocks created\n", 490 i, ADV_MAX_CCB); 491 } 492 493 adapt->adapt_openings = i; 494 adapt->adapt_max_periph = adapt->adapt_openings; 495 496 sc->sc_child = config_found(&sc->sc_dev, chan, scsiprint); 497 } 498 499 int 500 adv_detach(ASC_SOFTC *sc, int flags) 501 { 502 int rv = 0; 503 504 if (sc->sc_child != NULL) 505 rv = config_detach(sc->sc_child, flags); 506 507 adv_free_control_data(sc); 508 509 return (rv); 510 } 511 512 static void 513 advminphys(struct buf *bp) 514 { 515 516 if (bp->b_bcount > ((ASC_MAX_SG_LIST - 1) * PAGE_SIZE)) 517 bp->b_bcount = ((ASC_MAX_SG_LIST - 1) * PAGE_SIZE); 518 minphys(bp); 519 } 520 521 522 /* 523 * start a scsi operation given the command and the data address. Also needs 524 * the unit, target and lu. 525 */ 526 527 static void 528 adv_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg) 529 { 530 struct scsipi_xfer *xs; 531 struct scsipi_periph *periph; 532 ASC_SOFTC *sc = (void *)chan->chan_adapter->adapt_dev; 533 bus_dma_tag_t dmat = sc->sc_dmat; 534 ADV_CCB *ccb; 535 int s, flags, error, nsegs; 536 537 switch (req) { 538 case ADAPTER_REQ_RUN_XFER: 539 xs = arg; 540 periph = xs->xs_periph; 541 flags = xs->xs_control; 542 543 /* 544 * Get a CCB to use. 545 */ 546 ccb = adv_get_ccb(sc); 547 #ifdef DIAGNOSTIC 548 /* 549 * This should never happen as we track the resources 550 * in the mid-layer. 551 */ 552 if (ccb == NULL) { 553 scsipi_printaddr(periph); 554 printf("unable to allocate ccb\n"); 555 panic("adv_scsipi_request"); 556 } 557 #endif 558 559 ccb->xs = xs; 560 ccb->timeout = xs->timeout; 561 562 /* 563 * Build up the request 564 */ 565 memset(&ccb->scsiq, 0, sizeof(ASC_SCSI_Q)); 566 567 ccb->scsiq.q2.ccb_ptr = 568 sc->sc_dmamap_control->dm_segs[0].ds_addr + 569 ADV_CCB_OFF(ccb); 570 571 ccb->scsiq.cdbptr = &xs->cmd->opcode; 572 ccb->scsiq.q2.cdb_len = xs->cmdlen; 573 ccb->scsiq.q1.target_id = 574 ASC_TID_TO_TARGET_ID(periph->periph_target); 575 ccb->scsiq.q1.target_lun = periph->periph_lun; 576 ccb->scsiq.q2.target_ix = 577 ASC_TIDLUN_TO_IX(periph->periph_target, 578 periph->periph_lun); 579 ccb->scsiq.q1.sense_addr = 580 sc->sc_dmamap_control->dm_segs[0].ds_addr + 581 ADV_CCB_OFF(ccb) + offsetof(struct adv_ccb, scsi_sense); 582 ccb->scsiq.q1.sense_len = sizeof(struct scsi_sense_data); 583 584 /* 585 * If there are any outstanding requests for the current 586 * target, then every 255th request send an ORDERED request. 587 * This heuristic tries to retain the benefit of request 588 * sorting while preventing request starvation. 255 is the 589 * max number of tags or pending commands a device may have 590 * outstanding. 591 */ 592 sc->reqcnt[periph->periph_target]++; 593 if (((sc->reqcnt[periph->periph_target] > 0) && 594 (sc->reqcnt[periph->periph_target] % 255) == 0) || 595 xs->bp == NULL || (xs->bp->b_flags & B_ASYNC) == 0) { 596 ccb->scsiq.q2.tag_code = M2_QTAG_MSG_ORDERED; 597 } else { 598 ccb->scsiq.q2.tag_code = M2_QTAG_MSG_SIMPLE; 599 } 600 601 if (xs->datalen) { 602 /* 603 * Map the DMA transfer. 604 */ 605 #ifdef TFS 606 if (flags & SCSI_DATA_UIO) { 607 error = bus_dmamap_load_uio(dmat, 608 ccb->dmamap_xfer, (struct uio *) xs->data, 609 ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT : 610 BUS_DMA_WAITOK) | BUS_DMA_STREAMING | 611 ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ : 612 BUS_DMA_WRITE)); 613 } else 614 #endif /* TFS */ 615 { 616 error = bus_dmamap_load(dmat, ccb->dmamap_xfer, 617 xs->data, xs->datalen, NULL, 618 ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT : 619 BUS_DMA_WAITOK) | BUS_DMA_STREAMING | 620 ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ : 621 BUS_DMA_WRITE)); 622 } 623 624 switch (error) { 625 case 0: 626 break; 627 628 629 case ENOMEM: 630 case EAGAIN: 631 xs->error = XS_RESOURCE_SHORTAGE; 632 goto out_bad; 633 634 default: 635 xs->error = XS_DRIVER_STUFFUP; 636 if (error == EFBIG) { 637 aprint_error_dev(&sc->sc_dev, "adv_scsi_cmd, more than %d" 638 " DMA segments\n", 639 ASC_MAX_SG_LIST); 640 } else { 641 aprint_error_dev(&sc->sc_dev, "adv_scsi_cmd, error %d" 642 " loading DMA map\n", 643 error); 644 } 645 646 out_bad: 647 adv_free_ccb(sc, ccb); 648 scsipi_done(xs); 649 return; 650 } 651 bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0, 652 ccb->dmamap_xfer->dm_mapsize, 653 (flags & XS_CTL_DATA_IN) ? 654 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 655 656 memset(&ccb->sghead, 0, sizeof(ASC_SG_HEAD)); 657 658 for (nsegs = 0; 659 nsegs < ccb->dmamap_xfer->dm_nsegs; nsegs++) { 660 ccb->sghead.sg_list[nsegs].addr = 661 ccb->dmamap_xfer->dm_segs[nsegs].ds_addr; 662 ccb->sghead.sg_list[nsegs].bytes = 663 ccb->dmamap_xfer->dm_segs[nsegs].ds_len; 664 } 665 666 ccb->sghead.entry_cnt = ccb->scsiq.q1.sg_queue_cnt = 667 ccb->dmamap_xfer->dm_nsegs; 668 669 ccb->scsiq.q1.cntl |= ASC_QC_SG_HEAD; 670 ccb->scsiq.sg_head = &ccb->sghead; 671 ccb->scsiq.q1.data_addr = 0; 672 ccb->scsiq.q1.data_cnt = 0; 673 } else { 674 /* 675 * No data xfer, use non S/G values. 676 */ 677 ccb->scsiq.q1.data_addr = 0; 678 ccb->scsiq.q1.data_cnt = 0; 679 } 680 681 #ifdef ASC_DEBUG 682 printf("id = %d, lun = %d, cmd = %d, ccb = 0x%lX\n", 683 periph->periph_target, 684 periph->periph_lun, xs->cmd->opcode, 685 (unsigned long)ccb); 686 #endif 687 s = splbio(); 688 adv_queue_ccb(sc, ccb); 689 splx(s); 690 691 if ((flags & XS_CTL_POLL) == 0) 692 return; 693 694 /* Not allowed to use interrupts, poll for completion. */ 695 if (adv_poll(sc, xs, ccb->timeout)) { 696 adv_timeout(ccb); 697 if (adv_poll(sc, xs, ccb->timeout)) 698 adv_timeout(ccb); 699 } 700 return; 701 702 case ADAPTER_REQ_GROW_RESOURCES: 703 /* XXX Not supported. */ 704 return; 705 706 case ADAPTER_REQ_SET_XFER_MODE: 707 { 708 /* 709 * We can't really set the mode, but we know how to 710 * query what the firmware negotiated. 711 */ 712 struct scsipi_xfer_mode *xm = arg; 713 u_int8_t sdtr_data; 714 ASC_SCSI_BIT_ID_TYPE tid_bit; 715 716 tid_bit = ASC_TIX_TO_TARGET_ID(xm->xm_target); 717 718 xm->xm_mode = 0; 719 xm->xm_period = 0; 720 xm->xm_offset = 0; 721 722 if (sc->init_sdtr & tid_bit) { 723 xm->xm_mode |= PERIPH_CAP_SYNC; 724 sdtr_data = sc->sdtr_data[xm->xm_target]; 725 xm->xm_period = 726 sc->sdtr_period_tbl[(sdtr_data >> 4) & 727 (sc->max_sdtr_index - 1)]; 728 xm->xm_offset = sdtr_data & ASC_SYN_MAX_OFFSET; 729 } 730 731 if (sc->use_tagged_qng & tid_bit) 732 xm->xm_mode |= PERIPH_CAP_TQING; 733 734 scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm); 735 return; 736 } 737 } 738 } 739 740 int 741 adv_intr(void *arg) 742 { 743 ASC_SOFTC *sc = arg; 744 745 #ifdef ASC_DEBUG 746 int int_pend = FALSE; 747 748 if(ASC_IS_INT_PENDING(sc->sc_iot, sc->sc_ioh)) 749 { 750 int_pend = TRUE; 751 printf("ISR - "); 752 } 753 #endif 754 AscISR(sc); 755 #ifdef ASC_DEBUG 756 if(int_pend) 757 printf("\n"); 758 #endif 759 760 return (1); 761 } 762 763 764 /* 765 * Poll a particular unit, looking for a particular xs 766 */ 767 static int 768 adv_poll(ASC_SOFTC *sc, struct scsipi_xfer *xs, int count) 769 { 770 771 /* timeouts are in msec, so we loop in 1000 usec cycles */ 772 while (count) { 773 adv_intr(sc); 774 if (xs->xs_status & XS_STS_DONE) 775 return (0); 776 delay(1000); /* only happens in boot so ok */ 777 count--; 778 } 779 return (1); 780 } 781 782 783 static void 784 adv_timeout(void *arg) 785 { 786 ADV_CCB *ccb = arg; 787 struct scsipi_xfer *xs = ccb->xs; 788 struct scsipi_periph *periph = xs->xs_periph; 789 ASC_SOFTC *sc = 790 (void *)periph->periph_channel->chan_adapter->adapt_dev; 791 int s; 792 793 scsipi_printaddr(periph); 794 printf("timed out"); 795 796 s = splbio(); 797 798 /* 799 * If it has been through before, then a previous abort has failed, 800 * don't try abort again, reset the bus instead. 801 */ 802 if (ccb->flags & CCB_ABORT) { 803 /* abort timed out */ 804 printf(" AGAIN. Resetting Bus\n"); 805 /* Lets try resetting the bus! */ 806 if (AscResetBus(sc) == ASC_ERROR) { 807 ccb->timeout = sc->scsi_reset_wait; 808 adv_queue_ccb(sc, ccb); 809 } 810 } else { 811 /* abort the operation that has timed out */ 812 printf("\n"); 813 AscAbortCCB(sc, ccb); 814 ccb->xs->error = XS_TIMEOUT; 815 ccb->timeout = ADV_ABORT_TIMEOUT; 816 ccb->flags |= CCB_ABORT; 817 adv_queue_ccb(sc, ccb); 818 } 819 820 splx(s); 821 } 822 823 824 static void 825 adv_watchdog(void *arg) 826 { 827 ADV_CCB *ccb = arg; 828 struct scsipi_xfer *xs = ccb->xs; 829 struct scsipi_periph *periph = xs->xs_periph; 830 ASC_SOFTC *sc = 831 (void *)periph->periph_channel->chan_adapter->adapt_dev; 832 int s; 833 834 s = splbio(); 835 836 ccb->flags &= ~CCB_WATCHDOG; 837 adv_start_ccbs(sc); 838 839 splx(s); 840 } 841 842 843 /******************************************************************************/ 844 /* NARROW boards Interrupt callbacks */ 845 /******************************************************************************/ 846 847 848 /* 849 * adv_narrow_isr_callback() - Second Level Interrupt Handler called by AscISR() 850 * 851 * Interrupt callback function for the Narrow SCSI Asc Library. 852 */ 853 static void 854 adv_narrow_isr_callback(ASC_SOFTC *sc, ASC_QDONE_INFO *qdonep) 855 { 856 bus_dma_tag_t dmat = sc->sc_dmat; 857 ADV_CCB *ccb; 858 struct scsipi_xfer *xs; 859 struct scsi_sense_data *s1, *s2; 860 861 862 ccb = adv_ccb_phys_kv(sc, qdonep->d2.ccb_ptr); 863 xs = ccb->xs; 864 865 #ifdef ASC_DEBUG 866 printf(" - ccb=0x%lx, id=%d, lun=%d, cmd=%d, ", 867 (unsigned long)ccb, 868 xs->xs_periph->periph_target, 869 xs->xs_periph->periph_lun, xs->cmd->opcode); 870 #endif 871 callout_stop(&ccb->xs->xs_callout); 872 873 /* 874 * If we were a data transfer, unload the map that described 875 * the data buffer. 876 */ 877 if (xs->datalen) { 878 bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0, 879 ccb->dmamap_xfer->dm_mapsize, 880 (xs->xs_control & XS_CTL_DATA_IN) ? 881 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 882 bus_dmamap_unload(dmat, ccb->dmamap_xfer); 883 } 884 if ((ccb->flags & CCB_ALLOC) == 0) { 885 aprint_error_dev(&sc->sc_dev, "exiting ccb not allocated!\n"); 886 Debugger(); 887 return; 888 } 889 /* 890 * 'qdonep' contains the command's ending status. 891 */ 892 #ifdef ASC_DEBUG 893 printf("d_s=%d, h_s=%d", qdonep->d3.done_stat, qdonep->d3.host_stat); 894 #endif 895 switch (qdonep->d3.done_stat) { 896 case ASC_QD_NO_ERROR: 897 switch (qdonep->d3.host_stat) { 898 case ASC_QHSTA_NO_ERROR: 899 xs->error = XS_NOERROR; 900 xs->resid = 0; 901 break; 902 903 default: 904 /* QHSTA error occurred */ 905 xs->error = XS_DRIVER_STUFFUP; 906 break; 907 } 908 909 /* 910 * If an INQUIRY command completed successfully, then call 911 * the AscInquiryHandling() function to patch bugged boards. 912 */ 913 if ((xs->cmd->opcode == SCSICMD_Inquiry) && 914 (xs->xs_periph->periph_lun == 0) && 915 (xs->datalen - qdonep->remain_bytes) >= 8) { 916 AscInquiryHandling(sc, 917 xs->xs_periph->periph_target & 0x7, 918 (ASC_SCSI_INQUIRY *) xs->data); 919 } 920 break; 921 922 case ASC_QD_WITH_ERROR: 923 switch (qdonep->d3.host_stat) { 924 case ASC_QHSTA_NO_ERROR: 925 if (qdonep->d3.scsi_stat == SS_CHK_CONDITION) { 926 s1 = &ccb->scsi_sense; 927 s2 = &xs->sense.scsi_sense; 928 *s2 = *s1; 929 xs->error = XS_SENSE; 930 } else { 931 xs->error = XS_DRIVER_STUFFUP; 932 } 933 break; 934 935 case ASC_QHSTA_M_SEL_TIMEOUT: 936 xs->error = XS_SELTIMEOUT; 937 break; 938 939 default: 940 /* QHSTA error occurred */ 941 xs->error = XS_DRIVER_STUFFUP; 942 break; 943 } 944 break; 945 946 case ASC_QD_ABORTED_BY_HOST: 947 default: 948 xs->error = XS_DRIVER_STUFFUP; 949 break; 950 } 951 952 953 adv_free_ccb(sc, ccb); 954 scsipi_done(xs); 955 } 956