Home | History | Annotate | Line # | Download | only in ic
adv.c revision 1.23
      1 /*	$NetBSD: adv.c,v 1.23 2001/03/27 17:24:03 bouyer 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 (at) 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/types.h>
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/callout.h>
     44 #include <sys/kernel.h>
     45 #include <sys/errno.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/device.h>
     48 #include <sys/malloc.h>
     49 #include <sys/buf.h>
     50 #include <sys/proc.h>
     51 #include <sys/user.h>
     52 
     53 #include <machine/bus.h>
     54 #include <machine/intr.h>
     55 
     56 #include <uvm/uvm_extern.h>
     57 
     58 #include <dev/scsipi/scsi_all.h>
     59 #include <dev/scsipi/scsipi_all.h>
     60 #include <dev/scsipi/scsiconf.h>
     61 
     62 #include <dev/ic/advlib.h>
     63 #include <dev/ic/adv.h>
     64 
     65 #ifndef DDB
     66 #define	Debugger()	panic("should call debugger here (adv.c)")
     67 #endif /* ! DDB */
     68 
     69 
     70 /* #define ASC_DEBUG */
     71 
     72 /******************************************************************************/
     73 
     74 
     75 static int adv_alloc_control_data __P((ASC_SOFTC *));
     76 static void adv_free_control_data __P((ASC_SOFTC *));
     77 static int adv_create_ccbs __P((ASC_SOFTC *, ADV_CCB *, int));
     78 static void adv_free_ccb __P((ASC_SOFTC *, ADV_CCB *));
     79 static void adv_reset_ccb __P((ADV_CCB *));
     80 static int adv_init_ccb __P((ASC_SOFTC *, ADV_CCB *));
     81 static ADV_CCB *adv_get_ccb __P((ASC_SOFTC *, int));
     82 static void adv_queue_ccb __P((ASC_SOFTC *, ADV_CCB *));
     83 static void adv_start_ccbs __P((ASC_SOFTC *));
     84 
     85 
     86 static int adv_scsi_cmd __P((struct scsipi_xfer *));
     87 static void advminphys __P((struct buf *));
     88 static void adv_narrow_isr_callback __P((ASC_SOFTC *, ASC_QDONE_INFO *));
     89 
     90 static int adv_poll __P((ASC_SOFTC *, struct scsipi_xfer *, int));
     91 static void adv_timeout __P((void *));
     92 static void adv_watchdog __P((void *));
     93 
     94 
     95 /******************************************************************************/
     96 
     97 
     98 /* the below structure is so we have a default dev struct for out link struct */
     99 struct scsipi_device adv_dev =
    100 {
    101 	NULL,			/* Use default error handler */
    102 	NULL,			/* have a queue, served by this */
    103 	NULL,			/* have no async handler */
    104 	NULL,			/* Use default 'done' routine */
    105 };
    106 
    107 
    108 #define ADV_ABORT_TIMEOUT       2000	/* time to wait for abort (mSec) */
    109 #define ADV_WATCH_TIMEOUT       1000	/* time to wait for watchdog (mSec) */
    110 
    111 
    112 /******************************************************************************/
    113 /*                             Control Blocks routines                        */
    114 /******************************************************************************/
    115 
    116 
    117 static int
    118 adv_alloc_control_data(sc)
    119 	ASC_SOFTC      *sc;
    120 {
    121 	int error;
    122 
    123 	/*
    124          * Allocate the control blocks.
    125          */
    126 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct adv_control),
    127 			   PAGE_SIZE, 0, &sc->sc_control_seg, 1,
    128 			   &sc->sc_control_nsegs, BUS_DMA_NOWAIT)) != 0) {
    129 		printf("%s: unable to allocate control structures,"
    130 		       " error = %d\n", sc->sc_dev.dv_xname, error);
    131 		return (error);
    132 	}
    133 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_control_seg,
    134 			   sc->sc_control_nsegs, sizeof(struct adv_control),
    135 			   (caddr_t *) & sc->sc_control,
    136 			   BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    137 		printf("%s: unable to map control structures, error = %d\n",
    138 		       sc->sc_dev.dv_xname, error);
    139 		return (error);
    140 	}
    141 	/*
    142          * Create and load the DMA map used for the control blocks.
    143          */
    144 	if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct adv_control),
    145 			   1, sizeof(struct adv_control), 0, BUS_DMA_NOWAIT,
    146 				       &sc->sc_dmamap_control)) != 0) {
    147 		printf("%s: unable to create control DMA map, error = %d\n",
    148 		       sc->sc_dev.dv_xname, error);
    149 		return (error);
    150 	}
    151 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control,
    152 			   sc->sc_control, sizeof(struct adv_control), NULL,
    153 				     BUS_DMA_NOWAIT)) != 0) {
    154 		printf("%s: unable to load control DMA map, error = %d\n",
    155 		       sc->sc_dev.dv_xname, error);
    156 		return (error);
    157 	}
    158 
    159 	/*
    160 	 * Initialize the overrun_buf address.
    161 	 */
    162 	sc->overrun_buf = sc->sc_dmamap_control->dm_segs[0].ds_addr +
    163 	    offsetof(struct adv_control, overrun_buf);
    164 
    165 	return (0);
    166 }
    167 
    168 static void
    169 adv_free_control_data(sc)
    170 	ASC_SOFTC *sc;
    171 {
    172 
    173 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap_control);
    174 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap_control);
    175 	sc->sc_dmamap_control = NULL;
    176 
    177 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t) sc->sc_control,
    178 	    sizeof(struct adv_control));
    179 	bus_dmamem_free(sc->sc_dmat, &sc->sc_control_seg,
    180 	    sc->sc_control_nsegs);
    181 }
    182 
    183 /*
    184  * Create a set of ccbs and add them to the free list.  Called once
    185  * by adv_init().  We return the number of CCBs successfully created.
    186  */
    187 static int
    188 adv_create_ccbs(sc, ccbstore, count)
    189 	ASC_SOFTC      *sc;
    190 	ADV_CCB        *ccbstore;
    191 	int             count;
    192 {
    193 	ADV_CCB        *ccb;
    194 	int             i, error;
    195 
    196 	bzero(ccbstore, sizeof(ADV_CCB) * count);
    197 	for (i = 0; i < count; i++) {
    198 		ccb = &ccbstore[i];
    199 		if ((error = adv_init_ccb(sc, ccb)) != 0) {
    200 			printf("%s: unable to initialize ccb, error = %d\n",
    201 			       sc->sc_dev.dv_xname, error);
    202 			return (i);
    203 		}
    204 		TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain);
    205 	}
    206 
    207 	return (i);
    208 }
    209 
    210 
    211 /*
    212  * A ccb is put onto the free list.
    213  */
    214 static void
    215 adv_free_ccb(sc, ccb)
    216 	ASC_SOFTC      *sc;
    217 	ADV_CCB        *ccb;
    218 {
    219 	int             s;
    220 
    221 	s = splbio();
    222 
    223 	adv_reset_ccb(ccb);
    224 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
    225 
    226 	/*
    227          * If there were none, wake anybody waiting for one to come free,
    228          * starting with queued entries.
    229          */
    230 	if (ccb->chain.tqe_next == 0)
    231 		wakeup(&sc->sc_free_ccb);
    232 
    233 	splx(s);
    234 }
    235 
    236 
    237 static void
    238 adv_reset_ccb(ccb)
    239 	ADV_CCB        *ccb;
    240 {
    241 
    242 	ccb->flags = 0;
    243 }
    244 
    245 
    246 static int
    247 adv_init_ccb(sc, ccb)
    248 	ASC_SOFTC      *sc;
    249 	ADV_CCB        *ccb;
    250 {
    251 	int	hashnum, error;
    252 
    253 	callout_init(&ccb->ccb_watchdog);
    254 
    255 	/*
    256          * Create the DMA map for this CCB.
    257          */
    258 	error = bus_dmamap_create(sc->sc_dmat,
    259 				  (ASC_MAX_SG_LIST - 1) * PAGE_SIZE,
    260 			 ASC_MAX_SG_LIST, (ASC_MAX_SG_LIST - 1) * PAGE_SIZE,
    261 		   0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ccb->dmamap_xfer);
    262 	if (error) {
    263 		printf("%s: unable to create DMA map, error = %d\n",
    264 		       sc->sc_dev.dv_xname, error);
    265 		return (error);
    266 	}
    267 
    268 	/*
    269 	 * put in the phystokv hash table
    270 	 * Never gets taken out.
    271 	 */
    272 	ccb->hashkey = sc->sc_dmamap_control->dm_segs[0].ds_addr +
    273 	    ADV_CCB_OFF(ccb);
    274 	hashnum = CCB_HASH(ccb->hashkey);
    275 	ccb->nexthash = sc->sc_ccbhash[hashnum];
    276 	sc->sc_ccbhash[hashnum] = ccb;
    277 
    278 	adv_reset_ccb(ccb);
    279 	return (0);
    280 }
    281 
    282 
    283 /*
    284  * Get a free ccb
    285  *
    286  * If there are none, see if we can allocate a new one
    287  */
    288 static ADV_CCB *
    289 adv_get_ccb(sc, flags)
    290 	ASC_SOFTC      *sc;
    291 	int             flags;
    292 {
    293 	ADV_CCB        *ccb = 0;
    294 	int             s;
    295 
    296 	s = splbio();
    297 
    298 	/*
    299          * If we can and have to, sleep waiting for one to come free
    300          * but only if we can't allocate a new one.
    301          */
    302 	for (;;) {
    303 		ccb = sc->sc_free_ccb.tqh_first;
    304 		if (ccb) {
    305 			TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
    306 			break;
    307 		}
    308 		if ((flags & XS_CTL_NOSLEEP) != 0)
    309 			goto out;
    310 
    311 		tsleep(&sc->sc_free_ccb, PRIBIO, "advccb", 0);
    312 	}
    313 
    314 	ccb->flags |= CCB_ALLOC;
    315 
    316 out:
    317 	splx(s);
    318 	return (ccb);
    319 }
    320 
    321 
    322 /*
    323  * Given a physical address, find the ccb that it corresponds to.
    324  */
    325 ADV_CCB *
    326 adv_ccb_phys_kv(sc, ccb_phys)
    327 	ASC_SOFTC	*sc;
    328 	u_long		ccb_phys;
    329 {
    330 	int hashnum = CCB_HASH(ccb_phys);
    331 	ADV_CCB *ccb = sc->sc_ccbhash[hashnum];
    332 
    333 	while (ccb) {
    334 		if (ccb->hashkey == ccb_phys)
    335 			break;
    336 		ccb = ccb->nexthash;
    337 	}
    338 	return (ccb);
    339 }
    340 
    341 
    342 /*
    343  * Queue a CCB to be sent to the controller, and send it if possible.
    344  */
    345 static void
    346 adv_queue_ccb(sc, ccb)
    347 	ASC_SOFTC      *sc;
    348 	ADV_CCB        *ccb;
    349 {
    350 
    351 	TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
    352 
    353 	adv_start_ccbs(sc);
    354 }
    355 
    356 
    357 static void
    358 adv_start_ccbs(sc)
    359 	ASC_SOFTC      *sc;
    360 {
    361 	ADV_CCB        *ccb;
    362 
    363 	while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
    364 		if (ccb->flags & CCB_WATCHDOG)
    365 			callout_stop(&ccb->ccb_watchdog);
    366 
    367 		if (AscExeScsiQueue(sc, &ccb->scsiq) == ASC_BUSY) {
    368 			ccb->flags |= CCB_WATCHDOG;
    369 			callout_reset(&ccb->ccb_watchdog,
    370 			    (ADV_WATCH_TIMEOUT * hz) / 1000,
    371 			    adv_watchdog, ccb);
    372 			break;
    373 		}
    374 		TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
    375 
    376 		if ((ccb->xs->xs_control & XS_CTL_POLL) == 0)
    377 			callout_reset(&ccb->xs->xs_callout,
    378 			    ((u_int64_t)ccb->timeout * (u_int64_t)hz) / 1000,
    379 			    adv_timeout, ccb);
    380 	}
    381 }
    382 
    383 
    384 /******************************************************************************/
    385 /*                         SCSI layer interfacing routines                    */
    386 /******************************************************************************/
    387 
    388 
    389 int
    390 adv_init(sc)
    391 	ASC_SOFTC      *sc;
    392 {
    393 	int             warn;
    394 
    395 	if (!AscFindSignature(sc->sc_iot, sc->sc_ioh)) {
    396 		printf("adv_init: failed to find signature\n");
    397 		return (1);
    398 	}
    399 
    400 	/*
    401          * Read the board configuration
    402          */
    403 	AscInitASC_SOFTC(sc);
    404 	warn = AscInitFromEEP(sc);
    405 	if (warn) {
    406 		printf("%s -get: ", sc->sc_dev.dv_xname);
    407 		switch (warn) {
    408 		case -1:
    409 			printf("Chip is not halted\n");
    410 			break;
    411 
    412 		case -2:
    413 			printf("Couldn't get MicroCode Start"
    414 			       " address\n");
    415 			break;
    416 
    417 		case ASC_WARN_IO_PORT_ROTATE:
    418 			printf("I/O port address modified\n");
    419 			break;
    420 
    421 		case ASC_WARN_AUTO_CONFIG:
    422 			printf("I/O port increment switch enabled\n");
    423 			break;
    424 
    425 		case ASC_WARN_EEPROM_CHKSUM:
    426 			printf("EEPROM checksum error\n");
    427 			break;
    428 
    429 		case ASC_WARN_IRQ_MODIFIED:
    430 			printf("IRQ modified\n");
    431 			break;
    432 
    433 		case ASC_WARN_CMD_QNG_CONFLICT:
    434 			printf("tag queuing enabled w/o disconnects\n");
    435 			break;
    436 
    437 		default:
    438 			printf("unknown warning %d\n", warn);
    439 		}
    440 	}
    441 	if (sc->scsi_reset_wait > ASC_MAX_SCSI_RESET_WAIT)
    442 		sc->scsi_reset_wait = ASC_MAX_SCSI_RESET_WAIT;
    443 
    444 	/*
    445          * Modify the board configuration
    446          */
    447 	warn = AscInitFromASC_SOFTC(sc);
    448 	if (warn) {
    449 		printf("%s -set: ", sc->sc_dev.dv_xname);
    450 		switch (warn) {
    451 		case ASC_WARN_CMD_QNG_CONFLICT:
    452 			printf("tag queuing enabled w/o disconnects\n");
    453 			break;
    454 
    455 		case ASC_WARN_AUTO_CONFIG:
    456 			printf("I/O port increment switch enabled\n");
    457 			break;
    458 
    459 		default:
    460 			printf("unknown warning %d\n", warn);
    461 		}
    462 	}
    463 	sc->isr_callback = (ASC_CALLBACK) adv_narrow_isr_callback;
    464 
    465 	return (0);
    466 }
    467 
    468 
    469 void
    470 adv_attach(sc)
    471 	ASC_SOFTC      *sc;
    472 {
    473 	int             i, error;
    474 
    475 	/*
    476          * Initialize board RISC chip and enable interrupts.
    477          */
    478 	switch (AscInitDriver(sc)) {
    479 	case 0:
    480 		/* AllOK */
    481 		break;
    482 
    483 	case 1:
    484 		panic("%s: bad signature", sc->sc_dev.dv_xname);
    485 		break;
    486 
    487 	case 2:
    488 		panic("%s: unable to load MicroCode",
    489 		      sc->sc_dev.dv_xname);
    490 		break;
    491 
    492 	case 3:
    493 		panic("%s: unable to initialize MicroCode",
    494 		      sc->sc_dev.dv_xname);
    495 		break;
    496 
    497 	default:
    498 		panic("%s: unable to initialize board RISC chip",
    499 		      sc->sc_dev.dv_xname);
    500 	}
    501 
    502 	/*
    503 	 * Fill in the adapter.
    504 	 */
    505 	sc->sc_adapter.scsipi_cmd = adv_scsi_cmd;
    506 	sc->sc_adapter.scsipi_minphys = advminphys;
    507 
    508 	/*
    509          * fill in the prototype scsipi_link.
    510          */
    511 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    512 	sc->sc_link.adapter_softc = sc;
    513 	sc->sc_link.scsipi_scsi.adapter_target = sc->chip_scsi_id;
    514 	sc->sc_link.adapter = &sc->sc_adapter;
    515 	sc->sc_link.device = &adv_dev;
    516 	sc->sc_link.openings = 4;
    517 	sc->sc_link.scsipi_scsi.max_target = 7;
    518 	sc->sc_link.scsipi_scsi.max_lun = 7;
    519 	sc->sc_link.type = BUS_SCSI;
    520 
    521 
    522 	TAILQ_INIT(&sc->sc_free_ccb);
    523 	TAILQ_INIT(&sc->sc_waiting_ccb);
    524 	TAILQ_INIT(&sc->sc_queue);
    525 
    526 
    527 	/*
    528          * Allocate the Control Blocks and the overrun buffer.
    529          */
    530 	error = adv_alloc_control_data(sc);
    531 	if (error)
    532 		return; /* (error) */
    533 
    534 	/*
    535          * Create and initialize the Control Blocks.
    536          */
    537 	i = adv_create_ccbs(sc, sc->sc_control->ccbs, ADV_MAX_CCB);
    538 	if (i == 0) {
    539 		printf("%s: unable to create control blocks\n",
    540 		       sc->sc_dev.dv_xname);
    541 		return; /* (ENOMEM) */ ;
    542 	} else if (i != ADV_MAX_CCB) {
    543 		printf("%s: WARNING: only %d of %d control blocks created\n",
    544 		       sc->sc_dev.dv_xname, i, ADV_MAX_CCB);
    545 	}
    546 	sc->sc_child = config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
    547 }
    548 
    549 int
    550 adv_detach(sc, flags)
    551 	ASC_SOFTC *sc;
    552 	int flags;
    553 {
    554 	int rv = 0;
    555 
    556 	if (sc->sc_child != NULL)
    557 		rv = config_detach(sc->sc_child, flags);
    558 
    559 	adv_free_control_data(sc);
    560 
    561 	return (rv);
    562 }
    563 
    564 static void
    565 advminphys(bp)
    566 	struct buf     *bp;
    567 {
    568 
    569 	if (bp->b_bcount > ((ASC_MAX_SG_LIST - 1) * PAGE_SIZE))
    570 		bp->b_bcount = ((ASC_MAX_SG_LIST - 1) * PAGE_SIZE);
    571 	minphys(bp);
    572 }
    573 
    574 
    575 /*
    576  * start a scsi operation given the command and the data address.  Also needs
    577  * the unit, target and lu.
    578  */
    579 static int
    580 adv_scsi_cmd(xs)
    581 	struct scsipi_xfer *xs;
    582 {
    583 	struct scsipi_link *sc_link = xs->sc_link;
    584 	ASC_SOFTC      *sc = sc_link->adapter_softc;
    585 	bus_dma_tag_t   dmat = sc->sc_dmat;
    586 	ADV_CCB        *ccb;
    587 	int             s, flags, error, nsegs;
    588 	int             fromqueue = 0, dontqueue = 0, nowait = 0;
    589 
    590 
    591 	s = splbio();		/* protect the queue */
    592 
    593 	/*
    594          * If we're running the queue from adv_done(), we've been
    595          * called with the first queue entry as our argument.
    596          */
    597 	if (xs == TAILQ_FIRST(&sc->sc_queue)) {
    598 		TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
    599 		fromqueue = 1;
    600 		nowait = 1;
    601 	} else {
    602 
    603 		/* Polled requests can't be queued for later. */
    604 		dontqueue = xs->xs_control & XS_CTL_POLL;
    605 
    606 		/*
    607                  * If there are jobs in the queue, run them first.
    608                  */
    609 		if (TAILQ_FIRST(&sc->sc_queue) != NULL) {
    610 			/*
    611                          * If we can't queue, we have to abort, since
    612                          * we have to preserve order.
    613                          */
    614 			if (dontqueue) {
    615 				splx(s);
    616 				xs->error = XS_DRIVER_STUFFUP;
    617 				return (TRY_AGAIN_LATER);
    618 			}
    619 			/*
    620                          * Swap with the first queue entry.
    621                          */
    622 			TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
    623 			xs = TAILQ_FIRST(&sc->sc_queue);
    624 			TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
    625 			fromqueue = 1;
    626 		}
    627 	}
    628 
    629 
    630 	/*
    631          * get a ccb to use. If the transfer
    632          * is from a buf (possibly from interrupt time)
    633          * then we can't allow it to sleep
    634          */
    635 
    636 	flags = xs->xs_control;
    637 	if (nowait)
    638 		flags |= XS_CTL_NOSLEEP;
    639 	if ((ccb = adv_get_ccb(sc, flags)) == NULL) {
    640 		/*
    641                  * If we can't queue, we lose.
    642                  */
    643 		if (dontqueue) {
    644 			splx(s);
    645 			xs->error = XS_DRIVER_STUFFUP;
    646 			return (TRY_AGAIN_LATER);
    647 		}
    648 		/*
    649                  * Stuff ourselves into the queue, in front
    650                  * if we came off in the first place.
    651                  */
    652 		if (fromqueue)
    653 			TAILQ_INSERT_HEAD(&sc->sc_queue, xs, adapter_q);
    654 		else
    655 			TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
    656 		splx(s);
    657 		return (SUCCESSFULLY_QUEUED);
    658 	}
    659 	splx(s);		/* done playing with the queue */
    660 
    661 	ccb->xs = xs;
    662 	ccb->timeout = xs->timeout;
    663 
    664 	/*
    665          * Build up the request
    666          */
    667 	memset(&ccb->scsiq, 0, sizeof(ASC_SCSI_Q));
    668 
    669 	ccb->scsiq.q2.ccb_ptr = sc->sc_dmamap_control->dm_segs[0].ds_addr +
    670 		    ADV_CCB_OFF(ccb);
    671 
    672 	ccb->scsiq.cdbptr = &xs->cmd->opcode;
    673 	ccb->scsiq.q2.cdb_len = xs->cmdlen;
    674 	ccb->scsiq.q1.target_id = ASC_TID_TO_TARGET_ID(sc_link->scsipi_scsi.target);
    675 	ccb->scsiq.q1.target_lun = sc_link->scsipi_scsi.lun;
    676 	ccb->scsiq.q2.target_ix = ASC_TIDLUN_TO_IX(sc_link->scsipi_scsi.target,
    677 						   sc_link->scsipi_scsi.lun);
    678 	ccb->scsiq.q1.sense_addr = sc->sc_dmamap_control->dm_segs[0].ds_addr +
    679 		ADV_CCB_OFF(ccb) + offsetof(struct adv_ccb, scsi_sense);
    680 	ccb->scsiq.q1.sense_len = sizeof(struct scsipi_sense_data);
    681 
    682 	/*
    683          * If  there  are  any  outstanding  requests  for  the  current target,
    684          * then  every  255th request  send an  ORDERED request.  This heuristic
    685          * tries  to  retain  the  benefit  of request  sorting while preventing
    686          * request starvation. 255 is the max number of tags or pending commands
    687          * a device may have outstanding.
    688          */
    689 	sc->reqcnt[sc_link->scsipi_scsi.target]++;
    690 	if ((sc->reqcnt[sc_link->scsipi_scsi.target] > 0) &&
    691 	    (sc->reqcnt[sc_link->scsipi_scsi.target] % 255) == 0) {
    692 		ccb->scsiq.q2.tag_code = M2_QTAG_MSG_ORDERED;
    693 	} else {
    694 		if((xs->bp != NULL) && xs->bp->b_flags & B_ASYNC)
    695 			ccb->scsiq.q2.tag_code = M2_QTAG_MSG_SIMPLE;
    696 		else
    697 			ccb->scsiq.q2.tag_code = M2_QTAG_MSG_ORDERED;
    698 	}
    699 
    700 
    701 	if (xs->datalen) {
    702 		/*
    703                  * Map the DMA transfer.
    704                  */
    705 #ifdef TFS
    706 		if (flags & SCSI_DATA_UIO) {
    707 			error = bus_dmamap_load_uio(dmat, ccb->dmamap_xfer,
    708 			    (struct uio *) xs->data,
    709 			    ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
    710 			     BUS_DMA_WAITOK) | BUS_DMA_STREAMING);
    711 		} else
    712 #endif				/* TFS */
    713 		{
    714 			error = bus_dmamap_load(dmat, ccb->dmamap_xfer,
    715 			    xs->data, xs->datalen, NULL,
    716 			    ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
    717 			     BUS_DMA_WAITOK) | BUS_DMA_STREAMING);
    718 		}
    719 
    720 		if (error) {
    721 			if (error == EFBIG) {
    722 				printf("%s: adv_scsi_cmd, more than %d dma"
    723 				       " segments\n",
    724 				       sc->sc_dev.dv_xname, ASC_MAX_SG_LIST);
    725 			} else {
    726 				printf("%s: adv_scsi_cmd, error %d loading"
    727 				       " dma map\n",
    728 				       sc->sc_dev.dv_xname, error);
    729 			}
    730 
    731 			xs->error = XS_DRIVER_STUFFUP;
    732 			adv_free_ccb(sc, ccb);
    733 			return (COMPLETE);
    734 		}
    735 		bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
    736 				ccb->dmamap_xfer->dm_mapsize,
    737 			      (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
    738 				BUS_DMASYNC_PREWRITE);
    739 
    740 
    741 		memset(&ccb->sghead, 0, sizeof(ASC_SG_HEAD));
    742 
    743 		for (nsegs = 0; nsegs < ccb->dmamap_xfer->dm_nsegs; nsegs++) {
    744 
    745 			ccb->sghead.sg_list[nsegs].addr =
    746 				ccb->dmamap_xfer->dm_segs[nsegs].ds_addr;
    747 			ccb->sghead.sg_list[nsegs].bytes =
    748 				ccb->dmamap_xfer->dm_segs[nsegs].ds_len;
    749 		}
    750 
    751 		ccb->sghead.entry_cnt = ccb->scsiq.q1.sg_queue_cnt =
    752 			ccb->dmamap_xfer->dm_nsegs;
    753 
    754 		ccb->scsiq.q1.cntl |= ASC_QC_SG_HEAD;
    755 		ccb->scsiq.sg_head = &ccb->sghead;
    756 		ccb->scsiq.q1.data_addr = 0;
    757 		ccb->scsiq.q1.data_cnt = 0;
    758 	} else {
    759 		/*
    760                  * No data xfer, use non S/G values.
    761                  */
    762 		ccb->scsiq.q1.data_addr = 0;
    763 		ccb->scsiq.q1.data_cnt = 0;
    764 	}
    765 
    766 #ifdef ASC_DEBUG
    767 	printf("id = %d, lun = %d, cmd = %d, ccb = 0x%lX \n",
    768 			sc_link->scsipi_scsi.target,
    769 			sc_link->scsipi_scsi.lun, xs->cmd->opcode,
    770 			(unsigned long)ccb);
    771 #endif
    772 	s = splbio();
    773 	adv_queue_ccb(sc, ccb);
    774 	splx(s);
    775 
    776 	/*
    777          * Usually return SUCCESSFULLY QUEUED
    778          */
    779 	if ((flags & XS_CTL_POLL) == 0)
    780 		return (SUCCESSFULLY_QUEUED);
    781 
    782 	/*
    783          * If we can't use interrupts, poll on completion
    784          */
    785 	if (adv_poll(sc, xs, ccb->timeout)) {
    786 		adv_timeout(ccb);
    787 		if (adv_poll(sc, xs, ccb->timeout))
    788 			adv_timeout(ccb);
    789 	}
    790 	return (COMPLETE);
    791 }
    792 
    793 
    794 int
    795 adv_intr(arg)
    796 	void           *arg;
    797 {
    798 	ASC_SOFTC      *sc = arg;
    799 	struct scsipi_xfer *xs;
    800 
    801 #ifdef ASC_DEBUG
    802 	int int_pend = FALSE;
    803 
    804 	if(ASC_IS_INT_PENDING(sc->sc_iot, sc->sc_ioh))
    805 	{
    806 		int_pend = TRUE;
    807 		printf("ISR - ");
    808 	}
    809 #endif
    810 	AscISR(sc);
    811 #ifdef ASC_DEBUG
    812 	if(int_pend)
    813 		printf("\n");
    814 #endif
    815 
    816 	/*
    817          * If there are queue entries in the software queue, try to
    818          * run the first one.  We should be more or less guaranteed
    819          * to succeed, since we just freed a CCB.
    820          *
    821          * NOTE: adv_scsi_cmd() relies on our calling it with
    822          * the first entry in the queue.
    823          */
    824 	if ((xs = TAILQ_FIRST(&sc->sc_queue)) != NULL)
    825 		(void) adv_scsi_cmd(xs);
    826 
    827 	return (1);
    828 }
    829 
    830 
    831 /*
    832  * Poll a particular unit, looking for a particular xs
    833  */
    834 static int
    835 adv_poll(sc, xs, count)
    836 	ASC_SOFTC      *sc;
    837 	struct scsipi_xfer *xs;
    838 	int             count;
    839 {
    840 
    841 	/* timeouts are in msec, so we loop in 1000 usec cycles */
    842 	while (count) {
    843 		adv_intr(sc);
    844 		if (xs->xs_status & XS_STS_DONE)
    845 			return (0);
    846 		delay(1000);	/* only happens in boot so ok */
    847 		count--;
    848 	}
    849 	return (1);
    850 }
    851 
    852 
    853 static void
    854 adv_timeout(arg)
    855 	void           *arg;
    856 {
    857 	ADV_CCB        *ccb = arg;
    858 	struct scsipi_xfer *xs = ccb->xs;
    859 	struct scsipi_link *sc_link = xs->sc_link;
    860 	ASC_SOFTC      *sc = sc_link->adapter_softc;
    861 	int             s;
    862 
    863 	scsi_print_addr(sc_link);
    864 	printf("timed out");
    865 
    866 	s = splbio();
    867 
    868 	/*
    869          * If it has been through before, then a previous abort has failed,
    870          * don't try abort again, reset the bus instead.
    871          */
    872 	if (ccb->flags & CCB_ABORT) {
    873 		/* abort timed out */
    874 		printf(" AGAIN. Resetting Bus\n");
    875 		/* Lets try resetting the bus! */
    876 		if (AscResetBus(sc) == ASC_ERROR) {
    877 			ccb->timeout = sc->scsi_reset_wait;
    878 			adv_queue_ccb(sc, ccb);
    879 		}
    880 	} else {
    881 		/* abort the operation that has timed out */
    882 		printf("\n");
    883 		AscAbortCCB(sc, ccb);
    884 		ccb->xs->error = XS_TIMEOUT;
    885 		ccb->timeout = ADV_ABORT_TIMEOUT;
    886 		ccb->flags |= CCB_ABORT;
    887 		adv_queue_ccb(sc, ccb);
    888 	}
    889 
    890 	splx(s);
    891 }
    892 
    893 
    894 static void
    895 adv_watchdog(arg)
    896 	void           *arg;
    897 {
    898 	ADV_CCB        *ccb = arg;
    899 	struct scsipi_xfer *xs = ccb->xs;
    900 	struct scsipi_link *sc_link = xs->sc_link;
    901 	ASC_SOFTC      *sc = sc_link->adapter_softc;
    902 	int             s;
    903 
    904 	s = splbio();
    905 
    906 	ccb->flags &= ~CCB_WATCHDOG;
    907 	adv_start_ccbs(sc);
    908 
    909 	splx(s);
    910 }
    911 
    912 
    913 /******************************************************************************/
    914 /*                      NARROW boards Interrupt callbacks                     */
    915 /******************************************************************************/
    916 
    917 
    918 /*
    919  * adv_narrow_isr_callback() - Second Level Interrupt Handler called by AscISR()
    920  *
    921  * Interrupt callback function for the Narrow SCSI Asc Library.
    922  */
    923 static void
    924 adv_narrow_isr_callback(sc, qdonep)
    925 	ASC_SOFTC      *sc;
    926 	ASC_QDONE_INFO *qdonep;
    927 {
    928 	bus_dma_tag_t   dmat = sc->sc_dmat;
    929 	ADV_CCB        *ccb;
    930 	struct scsipi_xfer *xs;
    931 	struct scsipi_sense_data *s1, *s2;
    932 
    933 
    934 	ccb = adv_ccb_phys_kv(sc, qdonep->d2.ccb_ptr);
    935 	xs = ccb->xs;
    936 
    937 #ifdef ASC_DEBUG
    938 	printf(" - ccb=0x%lx, id=%d, lun=%d, cmd=%d, ",
    939 			(unsigned long)ccb,
    940 			xs->sc_link->scsipi_scsi.target,
    941 			xs->sc_link->scsipi_scsi.lun, xs->cmd->opcode);
    942 #endif
    943 	callout_stop(&ccb->xs->xs_callout);
    944 
    945 	/*
    946          * If we were a data transfer, unload the map that described
    947          * the data buffer.
    948          */
    949 	if (xs->datalen) {
    950 		bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
    951 				ccb->dmamap_xfer->dm_mapsize,
    952 			 (xs->xs_control & XS_CTL_DATA_IN) ?
    953 			 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    954 		bus_dmamap_unload(dmat, ccb->dmamap_xfer);
    955 	}
    956 	if ((ccb->flags & CCB_ALLOC) == 0) {
    957 		printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
    958 		Debugger();
    959 		return;
    960 	}
    961 	/*
    962          * 'qdonep' contains the command's ending status.
    963          */
    964 #ifdef ASC_DEBUG
    965 	printf("d_s=%d, h_s=%d", qdonep->d3.done_stat, qdonep->d3.host_stat);
    966 #endif
    967 	switch (qdonep->d3.done_stat) {
    968 	case ASC_QD_NO_ERROR:
    969 		switch (qdonep->d3.host_stat) {
    970 		case ASC_QHSTA_NO_ERROR:
    971 			xs->error = XS_NOERROR;
    972 			xs->resid = 0;
    973 			break;
    974 
    975 		default:
    976 			/* QHSTA error occurred */
    977 			xs->error = XS_DRIVER_STUFFUP;
    978 			break;
    979 		}
    980 
    981 		/*
    982                  * If an INQUIRY command completed successfully, then call
    983                  * the AscInquiryHandling() function to patch bugged boards.
    984                  */
    985 		if ((xs->cmd->opcode == SCSICMD_Inquiry) &&
    986 		    (xs->sc_link->scsipi_scsi.lun == 0) &&
    987 		    (xs->datalen - qdonep->remain_bytes) >= 8) {
    988 			AscInquiryHandling(sc,
    989 				      xs->sc_link->scsipi_scsi.target & 0x7,
    990 					   (ASC_SCSI_INQUIRY *) xs->data);
    991 		}
    992 		break;
    993 
    994 	case ASC_QD_WITH_ERROR:
    995 		switch (qdonep->d3.host_stat) {
    996 		case ASC_QHSTA_NO_ERROR:
    997 			if (qdonep->d3.scsi_stat == SS_CHK_CONDITION) {
    998 				s1 = &ccb->scsi_sense;
    999 				s2 = &xs->sense.scsi_sense;
   1000 				*s2 = *s1;
   1001 				xs->error = XS_SENSE;
   1002 			} else {
   1003 				xs->error = XS_DRIVER_STUFFUP;
   1004 			}
   1005 			break;
   1006 
   1007 		default:
   1008 			/* QHSTA error occurred */
   1009 			xs->error = XS_DRIVER_STUFFUP;
   1010 			break;
   1011 		}
   1012 		break;
   1013 
   1014 	case ASC_QD_ABORTED_BY_HOST:
   1015 	default:
   1016 		xs->error = XS_DRIVER_STUFFUP;
   1017 		break;
   1018 	}
   1019 
   1020 
   1021 	adv_free_ccb(sc, ccb);
   1022 	xs->xs_status |= XS_STS_DONE;
   1023 	scsipi_done(xs);
   1024 }
   1025