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