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