Home | History | Annotate | Line # | Download | only in ic
adw.c revision 1.30
      1 /* $NetBSD: adw.c,v 1.30 2001/04/30 02:55:08 lukem Exp $	 */
      2 
      3 /*
      4  * Generic driver for the Advanced Systems Inc. SCSI controllers
      5  *
      6  * Copyright (c) 1998, 1999, 2000 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/adwlib.h>
     63 #include <dev/ic/adwmcode.h>
     64 #include <dev/ic/adw.h>
     65 
     66 #ifndef DDB
     67 #define	Debugger()	panic("should call debugger here (adw.c)")
     68 #endif				/* ! DDB */
     69 
     70 /******************************************************************************/
     71 
     72 
     73 static int adw_alloc_controls(ADW_SOFTC *);
     74 static int adw_alloc_carriers(ADW_SOFTC *);
     75 static int adw_create_ccbs(ADW_SOFTC *, ADW_CCB *, int);
     76 static void adw_free_ccb(ADW_SOFTC *, ADW_CCB *);
     77 static void adw_reset_ccb(ADW_CCB *);
     78 static int adw_init_ccb(ADW_SOFTC *, ADW_CCB *);
     79 static ADW_CCB *adw_get_ccb(ADW_SOFTC *);
     80 static int adw_queue_ccb(ADW_SOFTC *, ADW_CCB *);
     81 
     82 static void adw_scsipi_request(struct scsipi_channel *,
     83 	scsipi_adapter_req_t, void *);
     84 static int adw_build_req(ADW_SOFTC *, ADW_CCB *);
     85 static void adw_build_sglist(ADW_CCB *, ADW_SCSI_REQ_Q *, ADW_SG_BLOCK *);
     86 static void adwminphys(struct buf *);
     87 static void adw_isr_callback(ADW_SOFTC *, ADW_SCSI_REQ_Q *);
     88 static void adw_async_callback(ADW_SOFTC *, u_int8_t);
     89 
     90 static void adw_print_info(ADW_SOFTC *, int);
     91 
     92 static int adw_poll(ADW_SOFTC *, struct scsipi_xfer *, int);
     93 static void adw_timeout(void *);
     94 static void adw_reset_bus(ADW_SOFTC *);
     95 
     96 
     97 /******************************************************************************/
     98 /*                       DMA Mapping for Control Blocks                       */
     99 /******************************************************************************/
    100 
    101 
    102 static int
    103 adw_alloc_controls(ADW_SOFTC *sc)
    104 {
    105 	bus_dma_segment_t seg;
    106 	int             error, rseg;
    107 
    108 	/*
    109          * Allocate the control structure.
    110          */
    111 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct adw_control),
    112 			   PAGE_SIZE, 0, &seg, 1, &rseg,
    113 			   BUS_DMA_NOWAIT)) != 0) {
    114 		printf("%s: unable to allocate control structures,"
    115 		       " error = %d\n", sc->sc_dev.dv_xname, error);
    116 		return (error);
    117 	}
    118 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    119 		   sizeof(struct adw_control), (caddr_t *) & sc->sc_control,
    120 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    121 		printf("%s: unable to map control structures, error = %d\n",
    122 		       sc->sc_dev.dv_xname, error);
    123 		return (error);
    124 	}
    125 
    126 	/*
    127          * Create and load the DMA map used for the control blocks.
    128          */
    129 	if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct adw_control),
    130 			   1, sizeof(struct adw_control), 0, BUS_DMA_NOWAIT,
    131 				       &sc->sc_dmamap_control)) != 0) {
    132 		printf("%s: unable to create control DMA map, error = %d\n",
    133 		       sc->sc_dev.dv_xname, error);
    134 		return (error);
    135 	}
    136 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control,
    137 			   sc->sc_control, sizeof(struct adw_control), NULL,
    138 				     BUS_DMA_NOWAIT)) != 0) {
    139 		printf("%s: unable to load control DMA map, error = %d\n",
    140 		       sc->sc_dev.dv_xname, error);
    141 		return (error);
    142 	}
    143 
    144 	return (0);
    145 }
    146 
    147 
    148 static int
    149 adw_alloc_carriers(ADW_SOFTC *sc)
    150 {
    151 	bus_dma_segment_t seg;
    152 	int             error, rseg;
    153 
    154 	/*
    155          * Allocate the control structure.
    156          */
    157 	sc->sc_control->carriers = malloc(sizeof(ADW_CARRIER) * ADW_MAX_CARRIER,
    158 			M_DEVBUF, M_WAITOK);
    159 	if(!sc->sc_control->carriers) {
    160 		printf("%s: malloc() failed in allocating carrier structures\n",
    161 		       sc->sc_dev.dv_xname);
    162 		return (ENOMEM);
    163 	}
    164 
    165 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    166 			sizeof(ADW_CARRIER) * ADW_MAX_CARRIER,
    167 			0x10, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    168 		printf("%s: unable to allocate carrier structures,"
    169 		       " error = %d\n", sc->sc_dev.dv_xname, error);
    170 		return (error);
    171 	}
    172 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    173 			sizeof(ADW_CARRIER) * ADW_MAX_CARRIER,
    174 			(caddr_t *) &sc->sc_control->carriers,
    175 			BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    176 		printf("%s: unable to map carrier structures,"
    177 			" error = %d\n", sc->sc_dev.dv_xname, error);
    178 		return (error);
    179 	}
    180 
    181 	/*
    182          * Create and load the DMA map used for the control blocks.
    183          */
    184 	if ((error = bus_dmamap_create(sc->sc_dmat,
    185 			sizeof(ADW_CARRIER) * ADW_MAX_CARRIER, 1,
    186 			sizeof(ADW_CARRIER) * ADW_MAX_CARRIER, 0,BUS_DMA_NOWAIT,
    187 			&sc->sc_dmamap_carrier)) != 0) {
    188 		printf("%s: unable to create carriers DMA map,"
    189 			" error = %d\n", sc->sc_dev.dv_xname, error);
    190 		return (error);
    191 	}
    192 	if ((error = bus_dmamap_load(sc->sc_dmat,
    193 			sc->sc_dmamap_carrier, sc->sc_control->carriers,
    194 			sizeof(ADW_CARRIER) * ADW_MAX_CARRIER, NULL,
    195 			BUS_DMA_NOWAIT)) != 0) {
    196 		printf("%s: unable to load carriers DMA map,"
    197 			" error = %d\n", sc->sc_dev.dv_xname, error);
    198 		return (error);
    199 	}
    200 
    201 	return (0);
    202 }
    203 
    204 
    205 /******************************************************************************/
    206 /*                           Control Blocks routines                          */
    207 /******************************************************************************/
    208 
    209 
    210 /*
    211  * Create a set of ccbs and add them to the free list.  Called once
    212  * by adw_init().  We return the number of CCBs successfully created.
    213  */
    214 static int
    215 adw_create_ccbs(ADW_SOFTC *sc, ADW_CCB *ccbstore, int count)
    216 {
    217 	ADW_CCB        *ccb;
    218 	int             i, error;
    219 
    220 	for (i = 0; i < count; i++) {
    221 		ccb = &ccbstore[i];
    222 		if ((error = adw_init_ccb(sc, ccb)) != 0) {
    223 			printf("%s: unable to initialize ccb, error = %d\n",
    224 			       sc->sc_dev.dv_xname, error);
    225 			return (i);
    226 		}
    227 		TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain);
    228 	}
    229 
    230 	return (i);
    231 }
    232 
    233 
    234 /*
    235  * A ccb is put onto the free list.
    236  */
    237 static void
    238 adw_free_ccb(ADW_SOFTC *sc, ADW_CCB *ccb)
    239 {
    240 	int             s;
    241 
    242 	s = splbio();
    243 
    244 	adw_reset_ccb(ccb);
    245 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
    246 
    247 	splx(s);
    248 }
    249 
    250 
    251 static void
    252 adw_reset_ccb(ADW_CCB *ccb)
    253 {
    254 
    255 	ccb->flags = 0;
    256 }
    257 
    258 
    259 static int
    260 adw_init_ccb(ADW_SOFTC *sc, ADW_CCB *ccb)
    261 {
    262 	int	hashnum, error;
    263 
    264 	/*
    265          * Create the DMA map for this CCB.
    266          */
    267 	error = bus_dmamap_create(sc->sc_dmat,
    268 				  (ADW_MAX_SG_LIST - 1) * PAGE_SIZE,
    269 			 ADW_MAX_SG_LIST, (ADW_MAX_SG_LIST - 1) * PAGE_SIZE,
    270 		   0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ccb->dmamap_xfer);
    271 	if (error) {
    272 		printf("%s: unable to create CCB DMA map, error = %d\n",
    273 		       sc->sc_dev.dv_xname, error);
    274 		return (error);
    275 	}
    276 
    277 	/*
    278 	 * put in the phystokv hash table
    279 	 * Never gets taken out.
    280 	 */
    281 	ccb->hashkey = sc->sc_dmamap_control->dm_segs[0].ds_addr +
    282 	    ADW_CCB_OFF(ccb);
    283 	hashnum = CCB_HASH(ccb->hashkey);
    284 	ccb->nexthash = sc->sc_ccbhash[hashnum];
    285 	sc->sc_ccbhash[hashnum] = ccb;
    286 	adw_reset_ccb(ccb);
    287 	return (0);
    288 }
    289 
    290 
    291 /*
    292  * Get a free ccb
    293  *
    294  * If there are none, see if we can allocate a new one
    295  */
    296 static ADW_CCB *
    297 adw_get_ccb(ADW_SOFTC *sc)
    298 {
    299 	ADW_CCB        *ccb = 0;
    300 	int             s;
    301 
    302 	s = splbio();
    303 
    304 	ccb = sc->sc_free_ccb.tqh_first;
    305 	if (ccb != NULL) {
    306 		TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
    307 		ccb->flags |= CCB_ALLOC;
    308 	}
    309 	splx(s);
    310 	return (ccb);
    311 }
    312 
    313 
    314 /*
    315  * Given a physical address, find the ccb that it corresponds to.
    316  */
    317 ADW_CCB *
    318 adw_ccb_phys_kv(ADW_SOFTC *sc, u_int32_t ccb_phys)
    319 {
    320 	int hashnum = CCB_HASH(ccb_phys);
    321 	ADW_CCB *ccb = sc->sc_ccbhash[hashnum];
    322 
    323 	while (ccb) {
    324 		if (ccb->hashkey == ccb_phys)
    325 			break;
    326 		ccb = ccb->nexthash;
    327 	}
    328 	return (ccb);
    329 }
    330 
    331 
    332 /*
    333  * Queue a CCB to be sent to the controller, and send it if possible.
    334  */
    335 static int
    336 adw_queue_ccb(ADW_SOFTC *sc, ADW_CCB *ccb)
    337 {
    338 	int		errcode = ADW_SUCCESS;
    339 
    340 	TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
    341 
    342 	while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
    343 
    344 		TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
    345 		errcode = AdwExeScsiQueue(sc, &ccb->scsiq);
    346 		switch(errcode) {
    347 		case ADW_SUCCESS:
    348 			break;
    349 
    350 		case ADW_BUSY:
    351 			printf("ADW_BUSY\n");
    352 			return(ADW_BUSY);
    353 
    354 		case ADW_ERROR:
    355 			printf("ADW_ERROR\n");
    356 			return(ADW_ERROR);
    357 		}
    358 
    359 		TAILQ_INSERT_TAIL(&sc->sc_pending_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, adw_timeout, ccb);
    364 	}
    365 
    366 	return(errcode);
    367 }
    368 
    369 
    370 /******************************************************************************/
    371 /*                       SCSI layer interfacing routines                      */
    372 /******************************************************************************/
    373 
    374 
    375 int
    376 adw_init(ADW_SOFTC *sc)
    377 {
    378 	u_int16_t       warn_code;
    379 
    380 
    381 	sc->cfg.lib_version = (ADW_LIB_VERSION_MAJOR << 8) |
    382 		ADW_LIB_VERSION_MINOR;
    383 	sc->cfg.chip_version =
    384 		ADW_GET_CHIP_VERSION(sc->sc_iot, sc->sc_ioh, sc->bus_type);
    385 
    386 	/*
    387 	 * Reset the chip to start and allow register writes.
    388 	 */
    389 	if (ADW_FIND_SIGNATURE(sc->sc_iot, sc->sc_ioh) == 0) {
    390 		panic("adw_init: adw_find_signature failed");
    391 	} else {
    392 		AdwResetChip(sc->sc_iot, sc->sc_ioh);
    393 
    394 		warn_code = AdwInitFromEEPROM(sc);
    395 
    396 		if (warn_code & ADW_WARN_EEPROM_CHKSUM)
    397 			printf("%s: Bad checksum found. "
    398 			       "Setting default values\n",
    399 			       sc->sc_dev.dv_xname);
    400 		if (warn_code & ADW_WARN_EEPROM_TERMINATION)
    401 			printf("%s: Bad bus termination setting."
    402 			       "Using automatic termination.\n",
    403 			       sc->sc_dev.dv_xname);
    404 	}
    405 
    406 	sc->isr_callback = (ADW_CALLBACK) adw_isr_callback;
    407 	sc->async_callback = (ADW_CALLBACK) adw_async_callback;
    408 
    409 	return 0;
    410 }
    411 
    412 
    413 void
    414 adw_attach(ADW_SOFTC *sc)
    415 {
    416 	struct scsipi_adapter *adapt = &sc->sc_adapter;
    417 	struct scsipi_channel *chan = &sc->sc_channel;
    418 	int             ncontrols, error;
    419 
    420 	TAILQ_INIT(&sc->sc_free_ccb);
    421 	TAILQ_INIT(&sc->sc_waiting_ccb);
    422 	TAILQ_INIT(&sc->sc_pending_ccb);
    423 
    424 	/*
    425          * Allocate the Control Blocks.
    426          */
    427 	error = adw_alloc_controls(sc);
    428 	if (error)
    429 		return; /* (error) */ ;
    430 
    431 	bzero(sc->sc_control, sizeof(struct adw_control));
    432 
    433 	/*
    434 	 * Create and initialize the Control Blocks.
    435 	 */
    436 	ncontrols = adw_create_ccbs(sc, sc->sc_control->ccbs, ADW_MAX_CCB);
    437 	if (ncontrols == 0) {
    438 		printf("%s: unable to create Control Blocks\n",
    439 		       sc->sc_dev.dv_xname);
    440 		return; /* (ENOMEM) */ ;
    441 	} else if (ncontrols != ADW_MAX_CCB) {
    442 		printf("%s: WARNING: only %d of %d Control Blocks"
    443 		       " created\n",
    444 		       sc->sc_dev.dv_xname, ncontrols, ADW_MAX_CCB);
    445 	}
    446 
    447 	/*
    448 	 * Create and initialize the Carriers.
    449 	 */
    450 	error = adw_alloc_carriers(sc);
    451 	if (error)
    452 		return; /* (error) */ ;
    453 
    454 	/*
    455 	 * Zero's the freeze_device status
    456 	 */
    457 	 bzero(sc->sc_freeze_dev, sizeof(sc->sc_freeze_dev));
    458 
    459 	/*
    460 	 * Initialize the adapter
    461 	 */
    462 	switch (AdwInitDriver(sc)) {
    463 	case ADW_IERR_BIST_PRE_TEST:
    464 		panic("%s: BIST pre-test error",
    465 		      sc->sc_dev.dv_xname);
    466 		break;
    467 
    468 	case ADW_IERR_BIST_RAM_TEST:
    469 		panic("%s: BIST RAM test error",
    470 		      sc->sc_dev.dv_xname);
    471 		break;
    472 
    473 	case ADW_IERR_MCODE_CHKSUM:
    474 		panic("%s: Microcode checksum error",
    475 		      sc->sc_dev.dv_xname);
    476 		break;
    477 
    478 	case ADW_IERR_ILLEGAL_CONNECTION:
    479 		panic("%s: All three connectors are in use",
    480 		      sc->sc_dev.dv_xname);
    481 		break;
    482 
    483 	case ADW_IERR_REVERSED_CABLE:
    484 		panic("%s: Cable is reversed",
    485 		      sc->sc_dev.dv_xname);
    486 		break;
    487 
    488 	case ADW_IERR_HVD_DEVICE:
    489 		panic("%s: HVD attached to LVD connector",
    490 		      sc->sc_dev.dv_xname);
    491 		break;
    492 
    493 	case ADW_IERR_SINGLE_END_DEVICE:
    494 		panic("%s: single-ended device is attached to"
    495 		      " one of the connectors",
    496 		      sc->sc_dev.dv_xname);
    497 		break;
    498 
    499 	case ADW_IERR_NO_CARRIER:
    500 		panic("%s: unable to create Carriers",
    501 		      sc->sc_dev.dv_xname);
    502 		break;
    503 
    504 	case ADW_WARN_BUSRESET_ERROR:
    505 		printf("%s: WARNING: Bus Reset Error\n",
    506 		      sc->sc_dev.dv_xname);
    507 		break;
    508 	}
    509 
    510 	/*
    511 	 * Fill in the scsipi_adapter.
    512 	 */
    513 	memset(adapt, 0, sizeof(*adapt));
    514 	adapt->adapt_dev = &sc->sc_dev;
    515 	adapt->adapt_nchannels = 1;
    516 	adapt->adapt_openings = ncontrols;
    517 	adapt->adapt_max_periph = adapt->adapt_openings;
    518 	adapt->adapt_request = adw_scsipi_request;
    519 	adapt->adapt_minphys = adwminphys;
    520 
    521 	/*
    522 	 * Fill in the scsipi_channel.
    523 	 */
    524 	memset(chan, 0, sizeof(*chan));
    525 	chan->chan_adapter = adapt;
    526 	chan->chan_bustype = &scsi_bustype;
    527 	chan->chan_channel = 0;
    528 	chan->chan_ntargets = ADW_MAX_TID + 1;
    529 	chan->chan_nluns = 7;
    530 	chan->chan_id = sc->chip_scsi_id;
    531 
    532 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    533 }
    534 
    535 
    536 static void
    537 adwminphys(struct buf *bp)
    538 {
    539 
    540 	if (bp->b_bcount > ((ADW_MAX_SG_LIST - 1) * PAGE_SIZE))
    541 		bp->b_bcount = ((ADW_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.
    548  * Also needs the unit, target and lu.
    549  */
    550 static void
    551 adw_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    552 	void *arg)
    553 {
    554 	struct scsipi_xfer *xs;
    555 	ADW_SOFTC      *sc = (void *)chan->chan_adapter->adapt_dev;
    556 	ADW_CCB        *ccb;
    557 	int            s, retry;
    558 
    559 	switch (req) {
    560 	case ADAPTER_REQ_RUN_XFER:
    561 		xs = arg;
    562 
    563 		/*
    564 		 * get a ccb to use. If the transfer
    565 		 * is from a buf (possibly from interrupt time)
    566 		 * then we can't allow it to sleep
    567 		 */
    568 
    569 		ccb = adw_get_ccb(sc);
    570 #ifdef DIAGNOSTIC
    571 		/*
    572                  * This should never happen as we track the resources
    573 		 * in the mid-layer.
    574                  */
    575 		if (ccb == NULL) {
    576 			scsipi_printaddr(xs->xs_periph);
    577 			printf("unable to allocate ccb\n");
    578 			panic("adw_scsipi_request");
    579 		}
    580 #endif
    581 
    582 		ccb->xs = xs;
    583 		ccb->timeout = xs->timeout;
    584 
    585 		if (adw_build_req(sc, ccb)) {
    586 			s = splbio();
    587 			retry = adw_queue_ccb(sc, ccb);
    588 			splx(s);
    589 
    590 			switch(retry) {
    591 			case ADW_BUSY:
    592 				xs->error = XS_RESOURCE_SHORTAGE;
    593 				adw_free_ccb(sc, ccb);
    594 				scsipi_done(xs);
    595 				return;
    596 
    597 			case ADW_ERROR:
    598 				xs->error = XS_DRIVER_STUFFUP;
    599 				adw_free_ccb(sc, ccb);
    600 				scsipi_done(xs);
    601 				return;
    602 			}
    603 			if ((xs->xs_control & XS_CTL_POLL) == 0)
    604 				return;
    605 			/*
    606 			 * Not allowed to use interrupts, poll for completion.
    607 			 */
    608 			if (adw_poll(sc, xs, ccb->timeout)) {
    609 				adw_timeout(ccb);
    610 				if (adw_poll(sc, xs, ccb->timeout))
    611 					adw_timeout(ccb);
    612 			}
    613 		}
    614 		return;
    615 
    616 	case ADAPTER_REQ_GROW_RESOURCES:
    617 		/* XXX Not supported. */
    618 		return;
    619 
    620 	case ADAPTER_REQ_SET_XFER_MODE:
    621 		/* XXX XXX XXX */
    622 		return;
    623 	}
    624 }
    625 
    626 
    627 /*
    628  * Build a request structure for the Wide Boards.
    629  */
    630 static int
    631 adw_build_req(ADW_SOFTC *sc, ADW_CCB *ccb)
    632 {
    633 	struct scsipi_xfer *xs = ccb->xs;
    634 	struct scsipi_periph *periph = xs->xs_periph;
    635 	bus_dma_tag_t   dmat = sc->sc_dmat;
    636 	ADW_SCSI_REQ_Q *scsiqp;
    637 	int             error;
    638 
    639 	scsiqp = &ccb->scsiq;
    640 	bzero(scsiqp, sizeof(ADW_SCSI_REQ_Q));
    641 
    642 	/*
    643 	 * Set the ADW_SCSI_REQ_Q 'ccb_ptr' to point to the
    644 	 * physical CCB structure.
    645 	 */
    646 	scsiqp->ccb_ptr = ccb->hashkey;
    647 
    648 	/*
    649 	 * Build the ADW_SCSI_REQ_Q request.
    650 	 */
    651 
    652 	/*
    653 	 * Set CDB length and copy it to the request structure.
    654 	 * For wide  boards a CDB length maximum of 16 bytes
    655 	 * is supported.
    656 	 */
    657 	bcopy(xs->cmd, &scsiqp->cdb, ((scsiqp->cdb_len = xs->cmdlen) <= 12)?
    658 			xs->cmdlen : 12 );
    659 	if(xs->cmdlen > 12)
    660 		bcopy(&(xs->cmd[12]),  &scsiqp->cdb16, xs->cmdlen - 12);
    661 
    662 	scsiqp->target_id = periph->periph_target;
    663 	scsiqp->target_lun = periph->periph_lun;
    664 
    665 	scsiqp->vsense_addr = &ccb->scsi_sense;
    666 	scsiqp->sense_addr = sc->sc_dmamap_control->dm_segs[0].ds_addr +
    667 			ADW_CCB_OFF(ccb) + offsetof(struct adw_ccb, scsi_sense);
    668 	scsiqp->sense_len = sizeof(struct scsipi_sense_data);
    669 
    670 	/*
    671 	 * Build ADW_SCSI_REQ_Q for a scatter-gather buffer command.
    672 	 */
    673 	if (xs->datalen) {
    674 		/*
    675                  * Map the DMA transfer.
    676                  */
    677 #ifdef TFS
    678 		if (xs->xs_control & SCSI_DATA_UIO) {
    679 			error = bus_dmamap_load_uio(dmat,
    680 				ccb->dmamap_xfer, (struct uio *) xs->data,
    681 			        ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
    682 			         BUS_DMA_WAITOK) | BUS_DMA_STREAMING);
    683 		} else
    684 #endif		/* TFS */
    685 		{
    686 			error = bus_dmamap_load(dmat,
    687 			      ccb->dmamap_xfer, xs->data, xs->datalen, NULL,
    688 			      ((xs->xs_control & XS_CTL_NOSLEEP) ?
    689 			       BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
    690 			       BUS_DMA_STREAMING);
    691 		}
    692 
    693 		switch (error) {
    694 		case 0:
    695 			break;
    696 		case ENOMEM:
    697 		case EAGAIN:
    698 			xs->error = XS_RESOURCE_SHORTAGE;
    699 			goto out_bad;
    700 
    701 		default:
    702 			xs->error = XS_DRIVER_STUFFUP;
    703 			printf("%s: error %d loading DMA map\n",
    704 			    sc->sc_dev.dv_xname, error);
    705 out_bad:
    706 			adw_free_ccb(sc, ccb);
    707 			scsipi_done(xs);
    708 			return(0);
    709 		}
    710 
    711 		bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
    712 		    ccb->dmamap_xfer->dm_mapsize,
    713 		    (xs->xs_control & XS_CTL_DATA_IN) ?
    714 		    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    715 
    716 		/*
    717 		 * Build scatter-gather list.
    718 		 */
    719 		scsiqp->data_cnt = xs->datalen;
    720 		scsiqp->vdata_addr = xs->data;
    721 		scsiqp->data_addr = ccb->dmamap_xfer->dm_segs[0].ds_addr;
    722 		bzero(ccb->sg_block, sizeof(ADW_SG_BLOCK) * ADW_NUM_SG_BLOCK);
    723 		adw_build_sglist(ccb, scsiqp, ccb->sg_block);
    724 	} else {
    725 		/*
    726                  * No data xfer, use non S/G values.
    727                  */
    728 		scsiqp->data_cnt = 0;
    729 		scsiqp->vdata_addr = 0;
    730 		scsiqp->data_addr = 0;
    731 	}
    732 
    733 	return (1);
    734 }
    735 
    736 
    737 /*
    738  * Build scatter-gather list for Wide Boards.
    739  */
    740 static void
    741 adw_build_sglist(ADW_CCB *ccb, ADW_SCSI_REQ_Q *scsiqp, ADW_SG_BLOCK *sg_block)
    742 {
    743 	u_long          sg_block_next_addr;	/* block and its next */
    744 	u_int32_t       sg_block_physical_addr;
    745 	int             i;	/* how many SG entries */
    746 	bus_dma_segment_t *sg_list = &ccb->dmamap_xfer->dm_segs[0];
    747 	int             sg_elem_cnt = ccb->dmamap_xfer->dm_nsegs;
    748 
    749 
    750 	sg_block_next_addr = (u_long) sg_block;	/* allow math operation */
    751 	sg_block_physical_addr = ccb->hashkey +
    752 	    offsetof(struct adw_ccb, sg_block[0]);
    753 	scsiqp->sg_real_addr = sg_block_physical_addr;
    754 
    755 	/*
    756 	 * If there are more than NO_OF_SG_PER_BLOCK dma segments (hw sg-list)
    757 	 * then split the request into multiple sg-list blocks.
    758 	 */
    759 
    760 	do {
    761 		for (i = 0; i < NO_OF_SG_PER_BLOCK; i++) {
    762 			sg_block->sg_list[i].sg_addr = sg_list->ds_addr;
    763 			sg_block->sg_list[i].sg_count = sg_list->ds_len;
    764 
    765 			if (--sg_elem_cnt == 0) {
    766 				/* last entry, get out */
    767 				sg_block->sg_cnt = i + 1;
    768 				sg_block->sg_ptr = NULL; /* next link = NULL */
    769 				return;
    770 			}
    771 			sg_list++;
    772 		}
    773 		sg_block_next_addr += sizeof(ADW_SG_BLOCK);
    774 		sg_block_physical_addr += sizeof(ADW_SG_BLOCK);
    775 
    776 		sg_block->sg_cnt = NO_OF_SG_PER_BLOCK;
    777 		sg_block->sg_ptr = sg_block_physical_addr;
    778 		sg_block = (ADW_SG_BLOCK *) sg_block_next_addr;	/* virt. addr */
    779 	} while (1);
    780 }
    781 
    782 
    783 /******************************************************************************/
    784 /*                       Interrupts and TimeOut routines                      */
    785 /******************************************************************************/
    786 
    787 
    788 int
    789 adw_intr(void *arg)
    790 {
    791 	ADW_SOFTC      *sc = arg;
    792 
    793 
    794 	if(AdwISR(sc) != ADW_FALSE) {
    795 		return (1);
    796 	}
    797 
    798 	return (0);
    799 }
    800 
    801 
    802 /*
    803  * Poll a particular unit, looking for a particular xs
    804  */
    805 static int
    806 adw_poll(ADW_SOFTC *sc, struct scsipi_xfer *xs, int count)
    807 {
    808 
    809 	/* timeouts are in msec, so we loop in 1000 usec cycles */
    810 	while (count) {
    811 		adw_intr(sc);
    812 		if (xs->xs_status & XS_STS_DONE)
    813 			return (0);
    814 		delay(1000);	/* only happens in boot so ok */
    815 		count--;
    816 	}
    817 	return (1);
    818 }
    819 
    820 
    821 static void
    822 adw_timeout(void *arg)
    823 {
    824 	ADW_CCB        *ccb = arg;
    825 	struct scsipi_xfer *xs = ccb->xs;
    826 	struct scsipi_periph *periph = xs->xs_periph;
    827 	ADW_SOFTC      *sc =
    828 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
    829 	int             s;
    830 
    831 	scsipi_printaddr(periph);
    832 	printf("timed out");
    833 
    834 	s = splbio();
    835 
    836 	if (ccb->flags & CCB_ABORTED) {
    837 	/*
    838 	 * Abort Timed Out
    839 	 *
    840 	 * No more opportunities. Lets try resetting the bus and
    841 	 * reinitialize the host adapter.
    842 	 */
    843 		callout_stop(&xs->xs_callout);
    844 		printf(" AGAIN. Resetting SCSI Bus\n");
    845 		adw_reset_bus(sc);
    846 		splx(s);
    847 		return;
    848 	} else if (ccb->flags & CCB_ABORTING) {
    849 	/*
    850 	 * Abort the operation that has timed out.
    851 	 *
    852 	 * Second opportunity.
    853 	 */
    854 		printf("\n");
    855 		xs->error = XS_TIMEOUT;
    856 		ccb->flags |= CCB_ABORTED;
    857 #if 0
    858 		/*
    859 		 * - XXX - 3.3a microcode is BROKEN!!!
    860 		 *
    861 		 * We cannot abort a CCB, so we can only hope the command
    862 		 * get completed before the next timeout, otherwise a
    863 		 * Bus Reset will arrive inexorably.
    864 		 */
    865 		/*
    866 		 * ADW_ABORT_CCB() makes the board to generate an interrupt
    867 		 *
    868 		 * - XXX - The above assertion MUST be verified (and this
    869 		 *         code changed as well [callout_*()]), when the
    870 		 *         ADW_ABORT_CCB will be working again
    871 		 */
    872 		ADW_ABORT_CCB(sc, ccb);
    873 #endif
    874 		/*
    875 		 * waiting for multishot callout_reset() let's restart it
    876 		 * by hand so the next time a timeout event will occour
    877 		 * we will reset the bus.
    878 		 */
    879 		callout_reset(&xs->xs_callout,
    880 			    (ccb->timeout * hz) / 1000, adw_timeout, ccb);
    881 	} else {
    882 	/*
    883 	 * Abort the operation that has timed out.
    884 	 *
    885 	 * First opportunity.
    886 	 */
    887 		printf("\n");
    888 		xs->error = XS_TIMEOUT;
    889 		ccb->flags |= CCB_ABORTING;
    890 #if 0
    891 		/*
    892 		 * - XXX - 3.3a microcode is BROKEN!!!
    893 		 *
    894 		 * We cannot abort a CCB, so we can only hope the command
    895 		 * get completed before the next 2 timeout, otherwise a
    896 		 * Bus Reset will arrive inexorably.
    897 		 */
    898 		/*
    899 		 * ADW_ABORT_CCB() makes the board to generate an interrupt
    900 		 *
    901 		 * - XXX - The above assertion MUST be verified (and this
    902 		 *         code changed as well [callout_*()]), when the
    903 		 *         ADW_ABORT_CCB will be working again
    904 		 */
    905 		ADW_ABORT_CCB(sc, ccb);
    906 #endif
    907 		/*
    908 		 * waiting for multishot callout_reset() let's restart it
    909 		 * by hand so to give a second opportunity to the command
    910 		 * which timed-out.
    911 		 */
    912 		callout_reset(&xs->xs_callout,
    913 			    (ccb->timeout * hz) / 1000, adw_timeout, ccb);
    914 	}
    915 
    916 	splx(s);
    917 }
    918 
    919 
    920 static void
    921 adw_reset_bus(ADW_SOFTC *sc)
    922 {
    923 	ADW_CCB	*ccb;
    924 	int	 s;
    925 	struct scsipi_xfer *xs;
    926 
    927 	s = splbio();
    928 	AdwResetSCSIBus(sc);
    929 	while((ccb = TAILQ_LAST(&sc->sc_pending_ccb,
    930 			adw_pending_ccb)) != NULL) {
    931 		callout_stop(&ccb->xs->xs_callout);
    932 		TAILQ_REMOVE(&sc->sc_pending_ccb, ccb, chain);
    933 		xs = ccb->xs;
    934 		adw_free_ccb(sc, ccb);
    935 		xs->error = XS_RESOURCE_SHORTAGE;
    936 		scsipi_done(xs);
    937 	}
    938 	splx(s);
    939 }
    940 
    941 
    942 /******************************************************************************/
    943 /*              Host Adapter and Peripherals Information Routines             */
    944 /******************************************************************************/
    945 
    946 
    947 static void
    948 adw_print_info(ADW_SOFTC *sc, int tid)
    949 {
    950 	bus_space_tag_t iot = sc->sc_iot;
    951 	bus_space_handle_t ioh = sc->sc_ioh;
    952 	u_int16_t wdtr_able, wdtr_done, wdtr;
    953     	u_int16_t sdtr_able, sdtr_done, sdtr, period;
    954 	static int wdtr_reneg = 0, sdtr_reneg = 0;
    955 
    956 	if (tid == 0){
    957 		wdtr_reneg = sdtr_reneg = 0;
    958 	}
    959 
    960 	printf("%s: target %d ", sc->sc_dev.dv_xname, tid);
    961 
    962 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, wdtr_able);
    963 	if(wdtr_able & ADW_TID_TO_TIDMASK(tid)) {
    964 		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_DONE, wdtr_done);
    965 		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_DEVICE_HSHK_CFG_TABLE +
    966 			(2 * tid), wdtr);
    967 		printf("using %d-bits wide, ", (wdtr & 0x8000)? 16 : 8);
    968 		if((wdtr_done & ADW_TID_TO_TIDMASK(tid)) == 0)
    969 			wdtr_reneg = 1;
    970 	} else {
    971 		printf("wide transfers disabled, ");
    972 	}
    973 
    974 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, sdtr_able);
    975 	if(sdtr_able & ADW_TID_TO_TIDMASK(tid)) {
    976 		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_DONE, sdtr_done);
    977 		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_DEVICE_HSHK_CFG_TABLE +
    978 			(2 * tid), sdtr);
    979 		sdtr &=  ~0x8000;
    980 		if((sdtr & 0x1F) != 0) {
    981 			if((sdtr & 0x1F00) == 0x1100){
    982 				printf("80.0 MHz");
    983 			} else if((sdtr & 0x1F00) == 0x1000){
    984 				printf("40.0 MHz");
    985 			} else {
    986 				/* <= 20.0 MHz */
    987 				period = (((sdtr >> 8) * 25) + 50)/4;
    988 				if(period == 0) {
    989 					/* Should never happen. */
    990 					printf("? MHz");
    991 				} else {
    992 					printf("%d.%d MHz", 250/period,
    993 						ADW_TENTHS(250, period));
    994 				}
    995 			}
    996 			printf(" synchronous transfers\n");
    997 		} else {
    998 			printf("asynchronous transfers\n");
    999 		}
   1000 		if((sdtr_done & ADW_TID_TO_TIDMASK(tid)) == 0)
   1001 			sdtr_reneg = 1;
   1002 	} else {
   1003 		printf("synchronous transfers disabled\n");
   1004 	}
   1005 
   1006 	if(wdtr_reneg || sdtr_reneg) {
   1007 		printf("%s: target %d %s", sc->sc_dev.dv_xname, tid,
   1008 			(wdtr_reneg)? ((sdtr_reneg)? "wide/sync" : "wide") :
   1009 			((sdtr_reneg)? "sync" : "") );
   1010 		printf(" renegotiation pending before next command.\n");
   1011 	}
   1012 }
   1013 
   1014 
   1015 /******************************************************************************/
   1016 /*                        WIDE boards Interrupt callbacks                     */
   1017 /******************************************************************************/
   1018 
   1019 
   1020 /*
   1021  * adw_isr_callback() - Second Level Interrupt Handler called by AdwISR()
   1022  *
   1023  * Interrupt callback function for the Wide SCSI Adv Library.
   1024  *
   1025  * Notice:
   1026  * Interrupts are disabled by the caller (AdwISR() function), and will be
   1027  * enabled at the end of the caller.
   1028  */
   1029 static void
   1030 adw_isr_callback(ADW_SOFTC *sc, ADW_SCSI_REQ_Q *scsiq)
   1031 {
   1032 	bus_dma_tag_t   dmat = sc->sc_dmat;
   1033 	ADW_CCB        *ccb;
   1034 	struct scsipi_xfer *xs;
   1035 	struct scsipi_sense_data *s1, *s2;
   1036 
   1037 
   1038 	ccb = adw_ccb_phys_kv(sc, scsiq->ccb_ptr);
   1039 
   1040 	callout_stop(&ccb->xs->xs_callout);
   1041 
   1042 	xs = ccb->xs;
   1043 
   1044 	/*
   1045          * If we were a data transfer, unload the map that described
   1046          * the data buffer.
   1047          */
   1048 	if (xs->datalen) {
   1049 		bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
   1050 				ccb->dmamap_xfer->dm_mapsize,
   1051 			 (xs->xs_control & XS_CTL_DATA_IN) ?
   1052 			 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1053 		bus_dmamap_unload(dmat, ccb->dmamap_xfer);
   1054 	}
   1055 
   1056 	if ((ccb->flags & CCB_ALLOC) == 0) {
   1057 		printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
   1058 		Debugger();
   1059 		return;
   1060 	}
   1061 
   1062 	/*
   1063 	 * 'done_status' contains the command's ending status.
   1064 	 * 'host_status' conatins the host adapter status.
   1065 	 * 'scsi_status' contains the scsi peripheral status.
   1066 	 */
   1067 	if ((scsiq->host_status == QHSTA_NO_ERROR) &&
   1068 	   ((scsiq->done_status == QD_NO_ERROR) ||
   1069 	    (scsiq->done_status == QD_WITH_ERROR))) {
   1070 		switch (scsiq->host_status) {
   1071 		case SCSI_STATUS_GOOD:
   1072 			if ((scsiq->cdb[0] == INQUIRY) &&
   1073 			    (scsiq->target_lun == 0)) {
   1074 				adw_print_info(sc, scsiq->target_id);
   1075 			}
   1076 			xs->error = XS_NOERROR;
   1077 			xs->resid = scsiq->data_cnt;
   1078 			sc->sc_freeze_dev[scsiq->target_id] = 0;
   1079 			break;
   1080 
   1081 		case SCSI_STATUS_CHECK_CONDITION:
   1082 		case SCSI_STATUS_CMD_TERMINATED:
   1083 			s1 = &ccb->scsi_sense;
   1084 			s2 = &xs->sense.scsi_sense;
   1085 			*s2 = *s1;
   1086 			xs->error = XS_SENSE;
   1087 			sc->sc_freeze_dev[scsiq->target_id] = 1;
   1088 			break;
   1089 
   1090 		default:
   1091 			xs->error = XS_BUSY;
   1092 			sc->sc_freeze_dev[scsiq->target_id] = 1;
   1093 			break;
   1094 		}
   1095 	} else if (scsiq->done_status == QD_ABORTED_BY_HOST) {
   1096 		xs->error = XS_DRIVER_STUFFUP;
   1097 	} else {
   1098 		switch (scsiq->host_status) {
   1099 		case QHSTA_M_SEL_TIMEOUT:
   1100 			xs->error = XS_SELTIMEOUT;
   1101 			break;
   1102 
   1103 		case QHSTA_M_SXFR_OFF_UFLW:
   1104 		case QHSTA_M_SXFR_OFF_OFLW:
   1105 		case QHSTA_M_DATA_OVER_RUN:
   1106 			printf("%s: Overrun/Overflow/Underflow condition\n",
   1107 				sc->sc_dev.dv_xname);
   1108 			xs->error = XS_DRIVER_STUFFUP;
   1109 			break;
   1110 
   1111 		case QHSTA_M_SXFR_DESELECTED:
   1112 		case QHSTA_M_UNEXPECTED_BUS_FREE:
   1113 			printf("%s: Unexpected BUS free\n",sc->sc_dev.dv_xname);
   1114 			xs->error = XS_DRIVER_STUFFUP;
   1115 			break;
   1116 
   1117 		case QHSTA_M_SCSI_BUS_RESET:
   1118 		case QHSTA_M_SCSI_BUS_RESET_UNSOL:
   1119 			printf("%s: BUS Reset\n", sc->sc_dev.dv_xname);
   1120 			xs->error = XS_DRIVER_STUFFUP;
   1121 			break;
   1122 
   1123 		case QHSTA_M_BUS_DEVICE_RESET:
   1124 			printf("%s: Device Reset\n", sc->sc_dev.dv_xname);
   1125 			xs->error = XS_DRIVER_STUFFUP;
   1126 			break;
   1127 
   1128 		case QHSTA_M_QUEUE_ABORTED:
   1129 			printf("%s: Queue Aborted\n", sc->sc_dev.dv_xname);
   1130 			xs->error = XS_DRIVER_STUFFUP;
   1131 			break;
   1132 
   1133 		case QHSTA_M_SXFR_SDMA_ERR:
   1134 		case QHSTA_M_SXFR_SXFR_PERR:
   1135 		case QHSTA_M_RDMA_PERR:
   1136 			/*
   1137 			 * DMA Error. This should *NEVER* happen!
   1138 			 *
   1139 			 * Lets try resetting the bus and reinitialize
   1140 			 * the host adapter.
   1141 			 */
   1142 			printf("%s: DMA Error. Reseting bus\n",
   1143 				sc->sc_dev.dv_xname);
   1144 			TAILQ_REMOVE(&sc->sc_pending_ccb, ccb, chain);
   1145 			adw_reset_bus(sc);
   1146 			xs->error = XS_BUSY;
   1147 			goto done;
   1148 
   1149 		case QHSTA_M_WTM_TIMEOUT:
   1150 		case QHSTA_M_SXFR_WD_TMO:
   1151 			/* The SCSI bus hung in a phase */
   1152 			printf("%s: Watch Dog timer expired. Reseting bus\n",
   1153 				sc->sc_dev.dv_xname);
   1154 			TAILQ_REMOVE(&sc->sc_pending_ccb, ccb, chain);
   1155 			adw_reset_bus(sc);
   1156 			xs->error = XS_BUSY;
   1157 			goto done;
   1158 
   1159 		case QHSTA_M_SXFR_XFR_PH_ERR:
   1160 			printf("%s: Transfer Error\n", sc->sc_dev.dv_xname);
   1161 			xs->error = XS_DRIVER_STUFFUP;
   1162 			break;
   1163 
   1164 		case QHSTA_M_BAD_CMPL_STATUS_IN:
   1165 			/* No command complete after a status message */
   1166 			printf("%s: Bad Completion Status\n",
   1167 				sc->sc_dev.dv_xname);
   1168 			xs->error = XS_DRIVER_STUFFUP;
   1169 			break;
   1170 
   1171 		case QHSTA_M_AUTO_REQ_SENSE_FAIL:
   1172 			printf("%s: Auto Sense Failed\n", sc->sc_dev.dv_xname);
   1173 			xs->error = XS_DRIVER_STUFFUP;
   1174 			break;
   1175 
   1176 		case QHSTA_M_INVALID_DEVICE:
   1177 			printf("%s: Invalid Device\n", sc->sc_dev.dv_xname);
   1178 			xs->error = XS_DRIVER_STUFFUP;
   1179 			break;
   1180 
   1181 		case QHSTA_M_NO_AUTO_REQ_SENSE:
   1182 			/*
   1183 			 * User didn't request sense, but we got a
   1184 			 * check condition.
   1185 			 */
   1186 			printf("%s: Unexpected Check Condition\n",
   1187 					sc->sc_dev.dv_xname);
   1188 			xs->error = XS_DRIVER_STUFFUP;
   1189 			break;
   1190 
   1191 		case QHSTA_M_SXFR_UNKNOWN_ERROR:
   1192 			printf("%s: Unknown Error\n", sc->sc_dev.dv_xname);
   1193 			xs->error = XS_DRIVER_STUFFUP;
   1194 			break;
   1195 
   1196 		default:
   1197 			panic("%s: Unhandled Host Status Error %x",
   1198 			      sc->sc_dev.dv_xname, scsiq->host_status);
   1199 		}
   1200 	}
   1201 
   1202 	TAILQ_REMOVE(&sc->sc_pending_ccb, ccb, chain);
   1203 done:	adw_free_ccb(sc, ccb);
   1204 	scsipi_done(xs);
   1205 }
   1206 
   1207 
   1208 /*
   1209  * adw_async_callback() - Adv Library asynchronous event callback function.
   1210  */
   1211 static void
   1212 adw_async_callback(ADW_SOFTC *sc, u_int8_t code)
   1213 {
   1214 	switch (code) {
   1215 	case ADV_ASYNC_SCSI_BUS_RESET_DET:
   1216 		/* The firmware detected a SCSI Bus reset. */
   1217 		printf("%s: SCSI Bus reset detected\n", sc->sc_dev.dv_xname);
   1218 		break;
   1219 
   1220 	case ADV_ASYNC_RDMA_FAILURE:
   1221 		/*
   1222 		 * Handle RDMA failure by resetting the SCSI Bus and
   1223 		 * possibly the chip if it is unresponsive.
   1224 		 */
   1225 		printf("%s: RDMA failure. Resetting the SCSI Bus and"
   1226 				" the adapter\n", sc->sc_dev.dv_xname);
   1227 		AdwResetSCSIBus(sc);
   1228 		break;
   1229 
   1230 	case ADV_HOST_SCSI_BUS_RESET:
   1231 		/* Host generated SCSI bus reset occurred. */
   1232 		printf("%s: Host generated SCSI bus reset occurred\n",
   1233 				sc->sc_dev.dv_xname);
   1234 		break;
   1235 
   1236 	case ADV_ASYNC_CARRIER_READY_FAILURE:
   1237 		/* Carrier Ready failure. */
   1238 		printf("%s: Carrier Ready failure!\n", sc->sc_dev.dv_xname);
   1239 		break;
   1240 
   1241 	default:
   1242 		break;
   1243 	}
   1244 }
   1245