Home | History | Annotate | Line # | Download | only in ic
dpt.c revision 1.2
      1  1.2  ad /*	$NetBSD: dpt.c,v 1.2 1999/09/28 09:18:00 ad Exp $	*/
      2  1.1  ad 
      3  1.1  ad /*-
      4  1.1  ad  * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
      5  1.1  ad  * All rights reserved.
      6  1.1  ad  *
      7  1.1  ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  ad  * by Andy Doran, Charles M. Hannum and by Jason R. Thorpe of the Numerical
      9  1.1  ad  * Aerospace Simulation Facility, NASA Ames Research Center.
     10  1.1  ad  *
     11  1.1  ad  * Redistribution and use in source and binary forms, with or without
     12  1.1  ad  * modification, are permitted provided that the following conditions
     13  1.1  ad  * are met:
     14  1.1  ad  * 1. Redistributions of source code must retain the above copyright
     15  1.1  ad  *    notice, this list of conditions and the following disclaimer.
     16  1.1  ad  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  ad  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  ad  *    documentation and/or other materials provided with the distribution.
     19  1.1  ad  * 3. All advertising materials mentioning features or use of this software
     20  1.1  ad  *    must display the following acknowledgement:
     21  1.1  ad  *	This product includes software developed by the NetBSD
     22  1.1  ad  *	Foundation, Inc. and its contributors.
     23  1.1  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1  ad  *    contributors may be used to endorse or promote products derived
     25  1.1  ad  *    from this software without specific prior written permission.
     26  1.1  ad  *
     27  1.1  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1  ad  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1  ad  */
     39  1.1  ad 
     40  1.1  ad /*
     41  1.1  ad  * Portions of this code fall under the following copyright:
     42  1.1  ad  *
     43  1.1  ad  * Originally written by Julian Elischer (julian (at) tfs.com)
     44  1.1  ad  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     45  1.1  ad  *
     46  1.1  ad  * TRW Financial Systems, in accordance with their agreement with Carnegie
     47  1.1  ad  * Mellon University, makes this software available to CMU to distribute
     48  1.1  ad  * or use in any manner that they see fit as long as this message is kept with
     49  1.1  ad  * the software. For this reason TFS also grants any other persons or
     50  1.1  ad  * organisations permission to use or modify this software.
     51  1.1  ad  *
     52  1.1  ad  * TFS supplies this software to be publicly redistributed
     53  1.1  ad  * on the understanding that TFS is not responsible for the correct
     54  1.1  ad  * functioning of this software in any circumstances.
     55  1.1  ad  *
     56  1.1  ad  * commenced: Sun Sep 27 18:14:01 PDT 1992
     57  1.1  ad  * slight mod to make work with 34F as well: Wed Jun  2 18:05:48 WST 1993
     58  1.1  ad  */
     59  1.1  ad 
     60  1.1  ad /*
     61  1.1  ad  * Driver for DPT EATA SCSI adapters.
     62  1.1  ad  *
     63  1.1  ad  * TODO:
     64  1.1  ad  *
     65  1.1  ad  * o Occasionally, dpt_readcfg() will fail while waiting for the HBA - fix.
     66  1.1  ad  * o Need a front-end for EISA boards.
     67  1.1  ad  * o Need a front-end for (newer) ISA boards.
     68  1.1  ad  * o Handle older firmware better.
     69  1.1  ad  * o Find a bunch of different firmware EEPROMs and try them out.
     70  1.1  ad  * o Test with a bunch of different boards.
     71  1.1  ad  * o dpt_readcfg() should not be using CP_PIO_GETCFG.
     72  1.1  ad  * o An interface to userland applications.
     73  1.1  ad  * o A port of DPT Storage Manager included in the base system would be nice.
     74  1.1  ad  * o Some sysctls or a utility (eg dptctl(8)) to control parameters.
     75  1.1  ad  * o Commit the manpage.
     76  1.1  ad  * o Code needs KNF in places and sanity in others.
     77  1.1  ad  */
     78  1.1  ad 
     79  1.1  ad #include <sys/cdefs.h>
     80  1.2  ad __KERNEL_RCSID(0, "$NetBSD: dpt.c,v 1.2 1999/09/28 09:18:00 ad Exp $");
     81  1.1  ad 
     82  1.1  ad #include <sys/param.h>
     83  1.1  ad #include <sys/systm.h>
     84  1.1  ad #include <sys/kernel.h>
     85  1.1  ad #include <sys/device.h>
     86  1.1  ad #include <sys/queue.h>
     87  1.1  ad #include <sys/proc.h>
     88  1.1  ad #include <sys/buf.h>
     89  1.1  ad 
     90  1.1  ad #include <machine/endian.h>
     91  1.1  ad #include <machine/bus.h>
     92  1.1  ad 
     93  1.1  ad #include <dev/scsipi/scsi_all.h>
     94  1.1  ad #include <dev/scsipi/scsipi_all.h>
     95  1.1  ad #include <dev/scsipi/scsiconf.h>
     96  1.1  ad 
     97  1.1  ad #include <dev/ic/dptreg.h>
     98  1.1  ad #include <dev/ic/dptvar.h>
     99  1.1  ad 
    100  1.1  ad /* A default for our link struct */
    101  1.1  ad static struct scsipi_device dpt_dev = {
    102  1.1  ad 	NULL,			/* Use default error handler */
    103  1.1  ad 	NULL,			/* have a queue, served by this */
    104  1.1  ad 	NULL,			/* have no async handler */
    105  1.1  ad 	NULL,			/* Use default 'done' routine */
    106  1.1  ad };
    107  1.1  ad 
    108  1.1  ad static char *dpt_cname[] = {
    109  1.1  ad #ifdef notdef
    110  1.1  ad 	"PM3755", "SmartRAID V",
    111  1.1  ad 	"PM3754", "SmartRAID V",
    112  1.1  ad 	"PM2654", "SmartRAID V",
    113  1.1  ad 	"PM2554", "SmartRAID V",
    114  1.1  ad 	"PM1554", "SmartRAID V",
    115  1.1  ad #endif
    116  1.1  ad 	"PM3334", "SmartRAID IV",
    117  1.1  ad 	"PM3332", "SmartRAID IV",
    118  1.1  ad 	"PM2144", "SmartCache IV",
    119  1.1  ad 	"PM2044", "SmartCache IV",
    120  1.1  ad 	"PM2142", "SmartCache IV",
    121  1.1  ad 	"PM2042", "SmartCache IV",
    122  1.1  ad 	"PM2041", "SmartCache IV",
    123  1.1  ad 	"PM3224", "SmartRAID III",
    124  1.1  ad 	"PM3222", "SmartRAID III",
    125  1.1  ad 	"PM3021", "SmartRAID III",
    126  1.1  ad 	"PM2124", "SmartCache III",
    127  1.1  ad 	"PM2024", "SmartCache III",
    128  1.1  ad 	"PM2122", "SmartCache III",
    129  1.1  ad 	"PM2022", "SmartCache III",
    130  1.1  ad 	"PM2021", "SmartCache III",
    131  1.1  ad 	"SK2012", "SmartCache Plus",
    132  1.1  ad 	"SK2011", "SmartCache Plus",
    133  1.1  ad 	NULL,     "unknown adapter, please report using send-pr(1)",
    134  1.1  ad };
    135  1.1  ad 
    136  1.1  ad void	dpt_shutdown __P((void *));
    137  1.1  ad void	dpt_timeout __P((void *));
    138  1.1  ad void	dpt_minphys __P((struct buf *));
    139  1.1  ad int	dpt_readcfg __P((struct dpt_softc *, struct eata_cfg *));
    140  1.1  ad int	dpt_scsi_cmd __P((struct scsipi_xfer *));
    141  1.1  ad int	dpt_wait __P((struct dpt_softc *, u_int8_t, u_int8_t, int));
    142  1.1  ad int	dpt_poll __P((struct dpt_softc *, struct dpt_ccb *));
    143  1.1  ad void	dpt_cmd __P((struct dpt_softc *, struct eata_cp *, u_int32_t, int, int));
    144  1.1  ad void	dpt_hba_inquire __P((struct dpt_softc *, struct eata_inquiry_data **));
    145  1.1  ad 
    146  1.1  ad void	dpt_reset_ccb __P((struct dpt_softc *, struct dpt_ccb *));
    147  1.1  ad void	dpt_free_ccb __P((struct dpt_softc *, struct dpt_ccb *));
    148  1.1  ad void	dpt_done_ccb __P((struct dpt_softc *, struct dpt_ccb *));
    149  1.1  ad int	dpt_init_ccb __P((struct dpt_softc *, struct dpt_ccb *));
    150  1.1  ad int	dpt_create_ccbs __P((struct dpt_softc *, struct dpt_ccb *, int));
    151  1.1  ad 
    152  1.1  ad struct dpt_ccb	*dpt_alloc_ccb __P((struct dpt_softc *, int));
    153  1.1  ad 
    154  1.1  ad #if 0 && defined(DEBUG)
    155  1.1  ad static void	dpt_dump_sp __P((struct eata_sp *));
    156  1.1  ad #endif
    157  1.1  ad 
    158  1.1  ad /*
    159  1.1  ad  * Handle an interrupt from the HBA.
    160  1.1  ad  */
    161  1.1  ad int
    162  1.1  ad dpt_intr(xxx_sc)
    163  1.1  ad 	void *xxx_sc;
    164  1.1  ad {
    165  1.1  ad 	struct dpt_softc *sc;
    166  1.1  ad 	struct dpt_ccb *ccb;
    167  1.1  ad 	struct eata_sp *sp;
    168  1.1  ad 	volatile int junk;
    169  1.1  ad 
    170  1.1  ad 	sc = xxx_sc;
    171  1.1  ad 	sp = sc->sc_sp;
    172  1.1  ad 
    173  1.1  ad #ifdef DEBUG
    174  1.1  ad 	if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) == 0)
    175  1.1  ad 		printf("%s: spurious intr\n", sc->sc_dv.dv_xname);
    176  1.1  ad #endif
    177  1.1  ad 
    178  1.1  ad 	/*
    179  1.1  ad 	 * HBA might have interrupted while we were dealing with the last
    180  1.1  ad 	 * completed command, since we ACK before we deal; keep polling.
    181  1.1  ad 	 */
    182  1.1  ad 	while ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) != 0) {
    183  1.1  ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb, sc->sc_spoff,
    184  1.1  ad 		    sizeof(struct eata_sp), BUS_DMASYNC_POSTREAD);
    185  1.1  ad 
    186  1.1  ad 		/* Might have looped before HBA can reset HBA_AUX_INTR */
    187  1.1  ad 		if (sp->sp_ccbid == -1) {
    188  1.1  ad 			DELAY(50);
    189  1.1  ad #ifdef DIAGNOSTIC
    190  1.1  ad 			printf("%s: slow reset of HA_AUX_STATUS?",
    191  1.1  ad 			    sc->sc_dv.dv_xname);
    192  1.1  ad #endif
    193  1.1  ad 			if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) == 0)
    194  1.1  ad 				return (0);
    195  1.1  ad #ifdef DIAGNOSTIC
    196  1.1  ad 			printf("%s: was a slow reset of HA_AUX_STATUS",
    197  1.1  ad 			    sc->sc_dv.dv_xname);
    198  1.1  ad #endif
    199  1.1  ad 			/* Re-sync DMA map */
    200  1.1  ad 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb,
    201  1.1  ad 			    sc->sc_spoff, sizeof(struct eata_sp),
    202  1.1  ad 			    BUS_DMASYNC_POSTREAD);
    203  1.1  ad 		}
    204  1.1  ad 
    205  1.1  ad 		/* Make sure CCB ID from status packet is realistic */
    206  1.1  ad 		if (sp->sp_ccbid >= 0 && sp->sp_ccbid < sc->sc_nccbs) {
    207  1.1  ad 			/* Sync up DMA map and cache cmd status */
    208  1.1  ad 			ccb = sc->sc_ccbs + sp->sp_ccbid;
    209  1.1  ad 
    210  1.1  ad 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb,
    211  1.1  ad 			    DPT_CCB_OFF(sc, ccb), sizeof(struct dpt_ccb),
    212  1.1  ad 			    BUS_DMASYNC_POSTWRITE);
    213  1.1  ad 
    214  1.1  ad 			ccb->ccb_hba_status = sp->sp_hba_status;
    215  1.1  ad 			ccb->ccb_scsi_status = sp->sp_scsi_status;
    216  1.1  ad 
    217  1.1  ad 			/*
    218  1.1  ad 			 * Ack the interrupt and process the CCB. If this
    219  1.1  ad 			 * is a private CCB it's up to dpt_poll() to notice.
    220  1.1  ad 			 */
    221  1.1  ad 			sp->sp_ccbid = -1;
    222  1.1  ad 			ccb->ccb_flg |= CCB_INTR;
    223  1.1  ad 			junk = dpt_inb(sc, HA_STATUS);
    224  1.1  ad 			if ((ccb->ccb_flg & CCB_PRIVATE) == 0)
    225  1.1  ad 				dpt_done_ccb(sc, ccb);
    226  1.1  ad 		} else {
    227  1.1  ad 			printf("%s: bogus status (returned CCB id %d)\n",
    228  1.1  ad 			    sc->sc_dv.dv_xname, sp->sp_ccbid);
    229  1.1  ad 
    230  1.1  ad 			/* Ack the interrupt */
    231  1.1  ad 			sp->sp_ccbid = -1;
    232  1.1  ad 			junk = dpt_inb(sc, HA_STATUS);
    233  1.1  ad 		}
    234  1.1  ad 	}
    235  1.1  ad 
    236  1.1  ad 	return (0);
    237  1.1  ad }
    238  1.1  ad 
    239  1.1  ad /*
    240  1.1  ad  * Initialize and attach the HBA. This is the entry point from bus
    241  1.1  ad  * specific probe-and-attach code.
    242  1.1  ad  */
    243  1.1  ad void
    244  1.1  ad dpt_init(sc, intrstr)
    245  1.1  ad 	struct dpt_softc *sc;
    246  1.1  ad 	const char *intrstr;
    247  1.1  ad {
    248  1.1  ad 	struct eata_inquiry_data *ei;
    249  1.1  ad 	int i, j, error, rseg, mapsize;
    250  1.1  ad 	bus_dma_segment_t seg;
    251  1.1  ad 	struct eata_cfg dc;
    252  1.1  ad 	char model[16];
    253  1.1  ad 
    254  1.1  ad 	/* Older firmware may puke if we talk to it too soon after reset */
    255  1.1  ad 	dpt_outb(sc, HA_COMMAND, CP_RESET);
    256  1.1  ad         DELAY(750000);
    257  1.1  ad 
    258  1.1  ad 	for (i = 1000; i; i--) {
    259  1.1  ad 		if ((dpt_inb(sc, HA_STATUS) & HA_ST_READY) != 0)
    260  1.1  ad 			break;
    261  1.1  ad 		DELAY(2000);
    262  1.1  ad 	}
    263  1.1  ad 
    264  1.1  ad 	if (i == 0) {
    265  1.1  ad 		printf("%s: HBA not ready after reset: %02x\n",
    266  1.1  ad 		    sc->sc_dv.dv_xname, dpt_inb(sc, HA_STATUS));
    267  1.1  ad 		return;
    268  1.1  ad 	}
    269  1.1  ad 
    270  1.1  ad 	if (dpt_readcfg(sc, &dc)) {
    271  1.1  ad 		printf("%s: readcfg failed - see dpt(4)\n",
    272  1.1  ad 		    sc->sc_dv.dv_xname);
    273  1.1  ad 		return;
    274  1.1  ad 	}
    275  1.1  ad 
    276  1.1  ad 	/* Allocate the CCB/status packet/scratch DMA map and load */
    277  1.1  ad 	sc->sc_nccbs = min(SWAP16(*(int16_t *)dc.dc_queuedepth), DPT_MAX_CCBS);
    278  1.1  ad 	sc->sc_spoff = sc->sc_nccbs * sizeof(struct dpt_ccb);
    279  1.1  ad 	sc->sc_scroff = sc->sc_spoff + sizeof(struct eata_sp);
    280  1.1  ad 	sc->sc_scrlen = 256; /* XXX */
    281  1.1  ad 	mapsize = sc->sc_nccbs * sizeof(struct dpt_ccb) + sc->sc_scrlen +
    282  1.1  ad 	    sizeof(struct eata_sp);
    283  1.1  ad 
    284  1.1  ad 	if ((error = bus_dmamem_alloc(sc->sc_dmat, mapsize, NBPG, 0,
    285  1.1  ad 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    286  1.1  ad 		printf("%s: unable to allocate CCBs, error = %d\n",
    287  1.1  ad 		    sc->sc_dv.dv_xname, error);
    288  1.1  ad 		return;
    289  1.1  ad 	}
    290  1.1  ad 
    291  1.1  ad 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, mapsize,
    292  1.1  ad 	    (caddr_t *)&sc->sc_ccbs, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    293  1.1  ad 		printf("%s: unable to map CCBs, error = %d\n",
    294  1.1  ad 		    sc->sc_dv.dv_xname, error);
    295  1.1  ad 		return;
    296  1.1  ad 	}
    297  1.1  ad 
    298  1.1  ad 	if ((error = bus_dmamap_create(sc->sc_dmat, mapsize, mapsize, 1, 0,
    299  1.1  ad 	    BUS_DMA_NOWAIT, &sc->sc_dmamap_ccb)) != 0) {
    300  1.1  ad 		printf("%s: unable to create CCB DMA map, error = %d\n",
    301  1.1  ad 		    sc->sc_dv.dv_xname, error);
    302  1.1  ad 		return;
    303  1.1  ad 	}
    304  1.1  ad 
    305  1.1  ad 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_ccb,
    306  1.1  ad 	    sc->sc_ccbs, mapsize, NULL, BUS_DMA_NOWAIT)) != 0) {
    307  1.1  ad 		printf("%s: unable to load CCB DMA map, error = %d\n",
    308  1.1  ad 		    sc->sc_dv.dv_xname, error);
    309  1.1  ad 		return;
    310  1.1  ad 	}
    311  1.1  ad 
    312  1.1  ad 	sc->sc_sp = (struct eata_sp *)((caddr_t)sc->sc_ccbs + sc->sc_spoff);
    313  1.1  ad 	sc->sc_sppa = sc->sc_dmamap_ccb->dm_segs[0].ds_addr + sc->sc_spoff;
    314  1.1  ad 	sc->sc_scr = (caddr_t)sc->sc_ccbs + sc->sc_scroff;
    315  1.1  ad 	sc->sc_scrpa = sc->sc_dmamap_ccb->dm_segs[0].ds_addr + sc->sc_scroff;
    316  1.1  ad 	sc->sc_sp->sp_ccbid = -1;
    317  1.1  ad #ifdef notdef
    318  1.1  ad 	sc->sc_pending = 0;
    319  1.1  ad #endif
    320  1.1  ad 	/* Initialize the CCBs */
    321  1.1  ad 	TAILQ_INIT(&sc->sc_free_ccb);
    322  1.1  ad 	i = dpt_create_ccbs(sc, sc->sc_ccbs, sc->sc_nccbs);
    323  1.1  ad 
    324  1.1  ad 	if (i == 0) {
    325  1.1  ad 		printf("%s: unable to create CCBs\n", sc->sc_dv.dv_xname);
    326  1.1  ad 		return;
    327  1.1  ad 	} else if (i != sc->sc_nccbs) {
    328  1.1  ad 		printf("%s: %d/%d CCBs created!\n", sc->sc_dv.dv_xname, i,
    329  1.1  ad 		    sc->sc_nccbs);
    330  1.1  ad 		sc->sc_nccbs = i;
    331  1.1  ad 	}
    332  1.1  ad 
    333  1.1  ad 	/* Set shutdownhook before we start any device activity */
    334  1.1  ad 	sc->sc_sdh = shutdownhook_establish(dpt_shutdown, sc);
    335  1.1  ad 
    336  1.1  ad 	/* Get the page 0 inquiry data from the HBA */
    337  1.1  ad 	dpt_hba_inquire(sc, &ei);
    338  1.1  ad 
    339  1.1  ad 	/*
    340  1.1  ad 	 * dpt0 at pci0 dev 12 function 0: DPT SmartRAID III (PM3224A/9X-R)
    341  1.1  ad 	 * dpt0: interrupting at irq 10
    342  1.1  ad 	 * dpt0: 64 queued commands, 1 channel(s), adapter on ID(s) 7
    343  1.1  ad 	 */
    344  1.1  ad 	for (i = 0; ei->ei_vendor[i] != ' ' && i < 8; i++)
    345  1.1  ad 		;
    346  1.1  ad 	ei->ei_vendor[i] = '\0';
    347  1.1  ad 
    348  1.1  ad 	for (i = 0; ei->ei_model[i] != ' ' && i < 7; i++)
    349  1.1  ad 		model[i] = ei->ei_model[i];
    350  1.1  ad 	for (j = 0; ei->ei_suffix[j] != ' ' && j < 7; j++)
    351  1.1  ad 		model[i++] = ei->ei_model[i];
    352  1.1  ad 	model[i] = '\0';
    353  1.1  ad 
    354  1.1  ad 	/* Find the cannonical name for the board */
    355  1.1  ad 	for (i = 0; dpt_cname[i]; i += 2)
    356  1.1  ad 		if (memcmp(ei->ei_model, dpt_cname[i], 6) == 0)
    357  1.1  ad 			break;
    358  1.1  ad 
    359  1.1  ad 	printf("%s %s (%s)\n", ei->ei_vendor, dpt_cname[i + 1], model);
    360  1.1  ad 
    361  1.1  ad 	if (intrstr != NULL)
    362  1.1  ad 		printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname, intrstr);
    363  1.1  ad 
    364  1.1  ad 	printf("%s: %d queued commands, %d channel(s), adapter on ID(s)",
    365  1.1  ad 	    sc->sc_dv.dv_xname, sc->sc_nccbs, dc.dc_maxchannel + 1);
    366  1.1  ad 
    367  1.1  ad 	for (i = 0; i <= dc.dc_maxchannel; i++)
    368  1.1  ad 		printf(" %d", dc.dc_hba[3 - i]);
    369  1.1  ad 	printf("\n");
    370  1.1  ad 
    371  1.1  ad 	/* Reset the SCSI bus */
    372  1.1  ad 	dpt_cmd(sc, NULL, 0, CP_IMMEDIATE, CPI_BUS_RESET);
    373  1.1  ad         DELAY(20000);
    374  1.1  ad 
    375  1.1  ad 	/* Fill in the adapter, each link and attach in turn */
    376  1.1  ad 	sc->sc_adapter.scsipi_cmd = dpt_scsi_cmd;
    377  1.1  ad 	sc->sc_adapter.scsipi_minphys = dpt_minphys;
    378  1.1  ad 
    379  1.1  ad 	for (i = 0; i <= dc.dc_maxchannel; i++) {
    380  1.1  ad 		struct scsipi_link *link;
    381  1.1  ad 
    382  1.1  ad 		sc->sc_hbaid[i] = dc.dc_hba[3 - i];
    383  1.1  ad 		link = &sc->sc_link[i];
    384  1.1  ad 		link->scsipi_scsi.channel = i;
    385  1.1  ad 		link->scsipi_scsi.adapter_target = sc->sc_hbaid[i];
    386  1.1  ad 		link->scsipi_scsi.max_lun = dc.dc_maxlun;
    387  1.1  ad 		link->scsipi_scsi.max_target = dc.dc_maxtarget;
    388  1.1  ad 		link->type = BUS_SCSI;
    389  1.1  ad 		link->device = &dpt_dev;
    390  1.1  ad 		link->adapter = &sc->sc_adapter;
    391  1.1  ad 		link->adapter_softc = sc;
    392  1.1  ad 		link->openings = sc->sc_nccbs;
    393  1.1  ad 		config_found(&sc->sc_dv, link, scsiprint);
    394  1.1  ad 	}
    395  1.1  ad }
    396  1.1  ad 
    397  1.1  ad /*
    398  1.1  ad  * Our 'shutdownhook' to cleanly shut down the HBA. The HBA must flush
    399  1.1  ad  * all data from it's cache and mark array groups as clean.
    400  1.1  ad  */
    401  1.1  ad void
    402  1.1  ad dpt_shutdown(xxx_sc)
    403  1.1  ad 	void *xxx_sc;
    404  1.1  ad {
    405  1.1  ad 	struct dpt_softc *sc;
    406  1.1  ad 
    407  1.1  ad 	sc = xxx_sc;
    408  1.1  ad 	printf("shutting down %s...", sc->sc_dv.dv_xname);
    409  1.1  ad 	dpt_cmd(sc, NULL, 0, CP_IMMEDIATE, CPI_POWEROFF_WARN);
    410  1.1  ad 	DELAY(5000*1000);
    411  1.1  ad 	printf(" done\n");
    412  1.1  ad }
    413  1.1  ad 
    414  1.1  ad /*
    415  1.1  ad  * Send an EATA command to the HBA.
    416  1.1  ad  */
    417  1.1  ad void
    418  1.1  ad dpt_cmd(sc, cp, addr, eatacmd, icmd)
    419  1.1  ad 	struct dpt_softc *sc;
    420  1.1  ad 	struct eata_cp *cp;
    421  1.1  ad 	u_int32_t addr;
    422  1.1  ad 	int eatacmd, icmd;
    423  1.1  ad {
    424  1.1  ad 	int i;
    425  1.1  ad 
    426  1.1  ad 	for (i = 20000; i; i--) {
    427  1.1  ad 		if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_BUSY) == 0)
    428  1.1  ad 			break;
    429  1.1  ad 		DELAY(50);
    430  1.1  ad 	}
    431  1.1  ad 
    432  1.1  ad 	if (i == 0) {
    433  1.1  ad 		/* XXX what to do here? */
    434  1.1  ad 		printf("%s: dpt_cmd failed\n", sc->sc_dv.dv_xname);
    435  1.1  ad 	}
    436  1.1  ad 
    437  1.1  ad 	if (cp == NULL)
    438  1.1  ad 		addr = 0;
    439  1.1  ad 
    440  1.1  ad 	dpt_outb(sc, HA_DMA_BASE + 0, (u_int32_t)addr);
    441  1.1  ad 	dpt_outb(sc, HA_DMA_BASE + 1, (u_int32_t)addr >> 8);
    442  1.1  ad 	dpt_outb(sc, HA_DMA_BASE + 2, (u_int32_t)addr >> 16);
    443  1.1  ad 	dpt_outb(sc, HA_DMA_BASE + 3, (u_int32_t)addr >> 24);
    444  1.1  ad 
    445  1.1  ad 	if (eatacmd == CP_IMMEDIATE) {
    446  1.1  ad 		if (cp == NULL) {
    447  1.1  ad 			/* XXX should really pass meaningful values */
    448  1.1  ad 			dpt_outb(sc, HA_ICMD_CODE2, 0);
    449  1.1  ad 			dpt_outb(sc, HA_ICMD_CODE1, 0);
    450  1.1  ad 		}
    451  1.1  ad 		dpt_outb(sc, HA_ICMD, icmd);
    452  1.1  ad 	}
    453  1.1  ad 
    454  1.1  ad         dpt_outb(sc, HA_COMMAND, eatacmd);
    455  1.1  ad }
    456  1.1  ad 
    457  1.1  ad /*
    458  1.1  ad  * Wait for the HBA to reach an arbitrary state.
    459  1.1  ad  */
    460  1.1  ad int
    461  1.1  ad dpt_wait(sc, mask, state, ms)
    462  1.1  ad         struct dpt_softc *sc;
    463  1.1  ad         u_int8_t mask, state;
    464  1.1  ad         int ms;
    465  1.1  ad {
    466  1.1  ad 
    467  1.1  ad         for (ms *= 10; ms; ms--) {
    468  1.1  ad                 if ((dpt_inb(sc, HA_STATUS) & mask) == state)
    469  1.1  ad                 	return (0);
    470  1.1  ad                 DELAY(100);
    471  1.1  ad         }
    472  1.1  ad         return (-1);
    473  1.1  ad }
    474  1.1  ad 
    475  1.1  ad /*
    476  1.1  ad  * Wait for the specified CCB to finish. This is used when we may not be
    477  1.1  ad  * able to sleep and/or interrupts are disabled (eg autoconfiguration).
    478  1.1  ad  * The timeout value from the CCB is used. This should only be used for
    479  1.1  ad  * CCB_PRIVATE requests; otherwise the CCB will get recycled before we get
    480  1.1  ad  * a look at it.
    481  1.1  ad  */
    482  1.1  ad int
    483  1.1  ad dpt_poll(sc, ccb)
    484  1.1  ad         struct dpt_softc *sc;
    485  1.1  ad         struct dpt_ccb *ccb;
    486  1.1  ad {
    487  1.1  ad 	int i;
    488  1.1  ad 
    489  1.1  ad         for (i = ccb->ccb_timeout * 20; i; i--) {
    490  1.1  ad                 if ((ccb->ccb_flg & CCB_INTR) != 0)
    491  1.1  ad                 	return (0);
    492  1.1  ad                 if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) != 0)
    493  1.1  ad                 	dpt_intr(sc);
    494  1.1  ad                 if ((ccb->ccb_flg & CCB_INTR) != 0)
    495  1.1  ad                 	return (0);
    496  1.1  ad                 DELAY(50);
    497  1.1  ad         }
    498  1.1  ad         return (-1);
    499  1.1  ad }
    500  1.1  ad 
    501  1.1  ad /*
    502  1.1  ad  * Read the EATA configuration from the HBA and perform some sanity checks.
    503  1.1  ad  */
    504  1.1  ad int
    505  1.1  ad dpt_readcfg(sc, dc)
    506  1.1  ad 	struct dpt_softc *sc;
    507  1.1  ad 	struct eata_cfg *dc;
    508  1.1  ad {
    509  1.1  ad 	int i, j, stat;
    510  1.1  ad 	u_int16_t *p;
    511  1.1  ad 
    512  1.1  ad 	while((((stat = dpt_inb(sc, HA_STATUS))
    513  1.1  ad             != (HA_ST_READY|HA_ST_SEEK_COMPLETE))
    514  1.1  ad             && (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR))
    515  1.1  ad             && (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR|HA_ST_DRQ)))
    516  1.2  ad             || (dpt_wait(sc, HA_ST_BUSY, 0, 2000))) {
    517  1.1  ad         	/* RAID drives still spinning up? */
    518  1.1  ad                 if((dpt_inb(sc, HA_ERROR) != 'D')
    519  1.1  ad                     || (dpt_inb(sc, HA_ERROR + 1) != 'P')
    520  1.2  ad                     || (dpt_inb(sc, HA_ERROR + 2) != 'T')) {
    521  1.2  ad                     	printf("%s: HBA not ready\n", sc->sc_dv.dv_xname);
    522  1.1  ad                         return (-1);
    523  1.2  ad 		}
    524  1.1  ad         }
    525  1.1  ad 
    526  1.1  ad 	/*
    527  1.1  ad 	 * Issue the read-config command and wait for the data to appear.
    528  1.1  ad 	 * XXX we shouldn't be doing this with PIO, but it makes it a lot
    529  1.1  ad 	 * easier as no DMA setup is required.
    530  1.1  ad 	 */
    531  1.1  ad 	dpt_outb(sc, HA_COMMAND, CP_PIO_GETCFG);
    532  1.1  ad 	memset(dc, 0, sizeof(*dc));
    533  1.1  ad 	i = ((int)&((struct eata_cfg *)0)->dc_cfglen +
    534  1.1  ad 	    sizeof(dc->dc_cfglen)) >> 1;
    535  1.1  ad 	p = (u_int16_t *)dc;
    536  1.1  ad 
    537  1.1  ad 	if (dpt_wait(sc, 0xFF, HA_ST_DATA_RDY, 2000))
    538  1.1  ad 		return (-1);
    539  1.1  ad 
    540  1.1  ad 	/* Begin reading */
    541  1.1  ad  	while (i--)
    542  1.1  ad 		*p++ = dpt_inw(sc, HA_DATA);
    543  1.1  ad 
    544  1.1  ad         if ((i = dc->dc_cfglen) > (sizeof(struct eata_cfg)
    545  1.1  ad             - (int)(&(((struct eata_cfg *)0L)->dc_cfglen))
    546  1.1  ad             - sizeof(dc->dc_cfglen)))
    547  1.1  ad                 i = sizeof(struct eata_cfg)
    548  1.1  ad                   - (int)(&(((struct eata_cfg *)0L)->dc_cfglen))
    549  1.1  ad                   - sizeof(dc->dc_cfglen);
    550  1.1  ad 
    551  1.1  ad         j = i + (int)(&(((struct eata_cfg *)0L)->dc_cfglen)) +
    552  1.1  ad             sizeof(dc->dc_cfglen);
    553  1.1  ad         i >>= 1;
    554  1.1  ad 
    555  1.1  ad 	while (i--)
    556  1.1  ad                 *p++ = dpt_inw(sc, HA_DATA);
    557  1.1  ad 
    558  1.1  ad         /* Flush until we have read 512 bytes. */
    559  1.1  ad         i = (512 - j + 1) >> 1;
    560  1.1  ad 	while (i--)
    561  1.1  ad  		dpt_inw(sc, HA_DATA);
    562  1.1  ad 
    563  1.1  ad         /* Defaults for older Firmware */
    564  1.1  ad 	if (p <= (u_short *)&dc->dc_hba[DPT_MAX_CHANNELS - 1])
    565  1.1  ad 		dc->dc_hba[DPT_MAX_CHANNELS - 1] = 7;
    566  1.1  ad 
    567  1.1  ad         if ((dpt_inb(sc, HA_STATUS) & HA_ST_ERROR) != 0) {
    568  1.1  ad         	printf("%s: HBA error\n", sc->sc_dv.dv_xname);
    569  1.1  ad         	return (-1);
    570  1.1  ad         }
    571  1.1  ad 
    572  1.1  ad         if (!dc->dc_hbavalid) {
    573  1.1  ad                 printf("%s: dc.dc_hba field invalid\n", sc->sc_dv.dv_xname);
    574  1.1  ad 		return (-1);
    575  1.1  ad 	}
    576  1.1  ad 
    577  1.1  ad 	if (memcmp(dc->dc_eatasig, "EATA", 4) != 0) {
    578  1.1  ad 	        printf("%s: EATA signature mismatch\n", sc->sc_dv.dv_xname);
    579  1.1  ad 		return (-1);
    580  1.1  ad 	}
    581  1.1  ad 
    582  1.1  ad 	if (!dc->dc_dmasupported) {
    583  1.1  ad 	        printf("%s: DMA not supported\n", sc->sc_dv.dv_xname);
    584  1.1  ad 		return (-1);
    585  1.1  ad 	}
    586  1.1  ad 
    587  1.1  ad 	return (0);
    588  1.1  ad }
    589  1.1  ad 
    590  1.1  ad /*
    591  1.1  ad  * Adjust the size of each I/O before it passes to the SCSI layer.
    592  1.1  ad  */
    593  1.1  ad void
    594  1.1  ad dpt_minphys(bp)
    595  1.1  ad 	struct buf *bp;
    596  1.1  ad {
    597  1.1  ad 
    598  1.1  ad 	if (bp->b_bcount > DPT_MAX_XFER)
    599  1.1  ad 		bp->b_bcount = DPT_MAX_XFER;
    600  1.1  ad 	minphys(bp);
    601  1.1  ad }
    602  1.1  ad 
    603  1.1  ad /*
    604  1.1  ad  * Put a CCB onto the freelist.
    605  1.1  ad  */
    606  1.1  ad void
    607  1.1  ad dpt_free_ccb(sc, ccb)
    608  1.1  ad 	struct dpt_softc *sc;
    609  1.1  ad 	struct dpt_ccb *ccb;
    610  1.1  ad {
    611  1.1  ad 	int s;
    612  1.1  ad 
    613  1.1  ad 	s = splbio();
    614  1.1  ad 	ccb->ccb_flg = 0;
    615  1.1  ad #ifdef notdef
    616  1.1  ad 	sc->sc_pending--;
    617  1.1  ad #endif
    618  1.1  ad 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, ccb_chain);
    619  1.1  ad 
    620  1.1  ad 	/* Wake anybody waiting for a free ccb */
    621  1.1  ad 	if (ccb->ccb_chain.tqe_next == 0)
    622  1.1  ad 		wakeup(&sc->sc_free_ccb);
    623  1.1  ad 
    624  1.1  ad 	splx(s);
    625  1.1  ad }
    626  1.1  ad 
    627  1.1  ad /*
    628  1.1  ad  * Initialize the specified CCB.
    629  1.1  ad  */
    630  1.1  ad int
    631  1.1  ad dpt_init_ccb(sc, ccb)
    632  1.1  ad 	struct dpt_softc *sc;
    633  1.1  ad 	struct dpt_ccb *ccb;
    634  1.1  ad {
    635  1.1  ad 	int error;
    636  1.1  ad 
    637  1.1  ad 	/* Create the DMA map for this CCB's data */
    638  1.1  ad 	error = bus_dmamap_create(sc->sc_dmat, DPT_MAX_XFER, DPT_SG_SIZE,
    639  1.1  ad 	    DPT_MAX_XFER, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    640  1.1  ad 	    &ccb->ccb_dmamap_xfer);
    641  1.1  ad 
    642  1.1  ad 	if (error) {
    643  1.1  ad 		printf("%s: can't create ccb dmamap (%d)\n",
    644  1.1  ad 		   sc->sc_dv.dv_xname, error);
    645  1.1  ad 		return (error);
    646  1.1  ad 	}
    647  1.1  ad 
    648  1.1  ad 	ccb->ccb_flg = 0;
    649  1.1  ad 	ccb->ccb_ccbpa = sc->sc_dmamap_ccb->dm_segs[0].ds_addr +
    650  1.1  ad 	    DPT_CCB_OFF(sc, ccb);
    651  1.1  ad 	return (0);
    652  1.1  ad }
    653  1.1  ad 
    654  1.1  ad /*
    655  1.1  ad  * Create a set of CCBs and add them to the free list.
    656  1.1  ad  */
    657  1.1  ad int
    658  1.1  ad dpt_create_ccbs(sc, ccbstore, count)
    659  1.1  ad 	struct dpt_softc *sc;
    660  1.1  ad 	struct dpt_ccb *ccbstore;
    661  1.1  ad 	int count;
    662  1.1  ad {
    663  1.1  ad 	struct dpt_ccb *ccb;
    664  1.1  ad 	int i, error;
    665  1.1  ad 
    666  1.1  ad 	memset(ccbstore, 0, sizeof(struct dpt_ccb) * count);
    667  1.1  ad 
    668  1.1  ad 	for (i = 0, ccb = ccbstore; i < count; i++, ccb++) {
    669  1.1  ad 		if ((error = dpt_init_ccb(sc, ccb)) != 0) {
    670  1.1  ad 			printf("%s: unable to init ccb, error = %d\n",
    671  1.1  ad 			    sc->sc_dv.dv_xname, error);
    672  1.1  ad 			break;
    673  1.1  ad 		}
    674  1.1  ad 		ccb->ccb_id = i;
    675  1.1  ad 		TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, ccb_chain);
    676  1.1  ad 	}
    677  1.1  ad 
    678  1.1  ad 	return (i);
    679  1.1  ad }
    680  1.1  ad 
    681  1.1  ad /*
    682  1.1  ad  * Get a free ccb. If there are none, see if we can allocate a new one.
    683  1.1  ad  * Otherwise either return an error or if we are permitted to, sleep until
    684  1.1  ad  * one becomes free.
    685  1.1  ad  */
    686  1.1  ad struct dpt_ccb *
    687  1.1  ad dpt_alloc_ccb(sc, flg)
    688  1.1  ad 	struct dpt_softc *sc;
    689  1.1  ad 	int flg;
    690  1.1  ad {
    691  1.1  ad 	struct dpt_ccb *ccb;
    692  1.1  ad 	int s;
    693  1.1  ad 
    694  1.1  ad 	s = splbio();
    695  1.1  ad 
    696  1.1  ad 	for (;;) {
    697  1.1  ad 		ccb = sc->sc_free_ccb.tqh_first;
    698  1.1  ad 		if (ccb) {
    699  1.1  ad 			TAILQ_REMOVE(&sc->sc_free_ccb, ccb, ccb_chain);
    700  1.1  ad 			break;
    701  1.1  ad 		}
    702  1.1  ad 		if ((flg & SCSI_NOSLEEP) != 0) {
    703  1.1  ad 			splx(s);
    704  1.1  ad 			return (NULL);
    705  1.1  ad 		}
    706  1.1  ad 		tsleep(&sc->sc_free_ccb, PRIBIO, "dptccb", 0);
    707  1.1  ad 	}
    708  1.1  ad 
    709  1.1  ad 	ccb->ccb_flg |= CCB_ALLOC;
    710  1.1  ad #ifdef notdef
    711  1.1  ad 	sc->sc_pending++;
    712  1.1  ad #endif
    713  1.1  ad 	splx(s);
    714  1.1  ad 	return (ccb);
    715  1.1  ad }
    716  1.1  ad 
    717  1.1  ad /*
    718  1.1  ad  * We have a CCB which has been processed by the HBA, now we look to see how
    719  1.2  ad  * the operation went. CCBs marked with CCB_PRIVATE are not automatically
    720  1.2  ad  * passed here by dpt_intr().
    721  1.1  ad  */
    722  1.1  ad void
    723  1.1  ad dpt_done_ccb(sc, ccb)
    724  1.1  ad 	struct dpt_softc *sc;
    725  1.1  ad 	struct dpt_ccb *ccb;
    726  1.1  ad {
    727  1.1  ad 	struct scsipi_sense_data *s1, *s2;
    728  1.1  ad 	struct scsipi_xfer *xs;
    729  1.1  ad 	bus_dma_tag_t dmat;
    730  1.1  ad 
    731  1.1  ad 	dmat = sc->sc_dmat;
    732  1.1  ad 	xs = ccb->ccb_xs;
    733  1.1  ad 
    734  1.1  ad 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("dpt_done_ccb\n"));
    735  1.1  ad 
    736  1.1  ad 	/*
    737  1.1  ad 	 * If we were a data transfer, unload the map that described the
    738  1.1  ad 	 * data buffer.
    739  1.1  ad 	 */
    740  1.1  ad 	if (xs->datalen) {
    741  1.1  ad 		bus_dmamap_sync(dmat, ccb->ccb_dmamap_xfer, 0,
    742  1.1  ad 		    ccb->ccb_dmamap_xfer->dm_mapsize,
    743  1.1  ad 		    (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
    744  1.1  ad 		    BUS_DMASYNC_POSTWRITE);
    745  1.1  ad 		bus_dmamap_unload(dmat, ccb->ccb_dmamap_xfer);
    746  1.1  ad 	}
    747  1.1  ad 
    748  1.1  ad 	/*
    749  1.1  ad 	 * Otherwise, put the results of the operation into the xfer and
    750  1.1  ad 	 * call whoever started it.
    751  1.1  ad 	 */
    752  1.1  ad 	if ((ccb->ccb_flg & CCB_ALLOC) == 0) {
    753  1.1  ad 		panic("%s: done ccb not allocated!\n", sc->sc_dv.dv_xname);
    754  1.1  ad 		return;
    755  1.1  ad 	}
    756  1.1  ad 
    757  1.1  ad 	if (xs->error == XS_NOERROR) {
    758  1.1  ad 		if (ccb->ccb_hba_status != HA_NO_ERROR) {
    759  1.1  ad 			switch (ccb->ccb_hba_status) {
    760  1.1  ad 			case HA_ERROR_SEL_TO:
    761  1.1  ad 				xs->error = XS_SELTIMEOUT;
    762  1.1  ad 				break;
    763  1.1  ad 			case HA_ERROR_RESET:
    764  1.1  ad 				xs->error = XS_RESET;
    765  1.1  ad 				break;
    766  1.1  ad 			default:	/* Other scsi protocol messes */
    767  1.1  ad 				printf("%s: HBA status %x\n",
    768  1.1  ad 				    sc->sc_dv.dv_xname, ccb->ccb_hba_status);
    769  1.1  ad 				xs->error = XS_DRIVER_STUFFUP;
    770  1.1  ad 			}
    771  1.1  ad 		} else if (ccb->ccb_scsi_status != SCSI_OK) {
    772  1.1  ad 			switch (ccb->ccb_scsi_status) {
    773  1.1  ad 			case SCSI_CHECK:
    774  1.1  ad 				s1 = &ccb->ccb_sense;
    775  1.1  ad 				s2 = &xs->sense.scsi_sense;
    776  1.1  ad 				*s2 = *s1;
    777  1.1  ad 				xs->error = XS_SENSE;
    778  1.1  ad 				break;
    779  1.1  ad 			case SCSI_BUSY:
    780  1.1  ad 				xs->error = XS_BUSY;
    781  1.1  ad 				break;
    782  1.1  ad 			default:
    783  1.1  ad 				printf("%s: SCSI status %x\n",
    784  1.1  ad 				    sc->sc_dv.dv_xname, ccb->ccb_scsi_status);
    785  1.1  ad 				xs->error = XS_DRIVER_STUFFUP;
    786  1.1  ad 			}
    787  1.1  ad 		} else
    788  1.1  ad 			xs->resid = 0;
    789  1.1  ad 	}
    790  1.1  ad 
    791  1.1  ad 	/* Free up the CCB and mark the command as done */
    792  1.1  ad 	dpt_free_ccb(sc, ccb);
    793  1.1  ad 	xs->flags |= ITSDONE;
    794  1.1  ad 	scsipi_done(xs);
    795  1.1  ad 
    796  1.1  ad 	/*
    797  1.1  ad 	 * If there are queue entries in the software queue, try to run the
    798  1.1  ad 	 * first one.  We should be more or less guaranteed to succeed, since
    799  1.1  ad 	 * we just freed an CCB. NOTE: dpt_scsi_cmd() relies on our calling
    800  1.1  ad 	 * it with the first entry in the queue.
    801  1.1  ad 	 */
    802  1.1  ad 	if ((xs = TAILQ_FIRST(&sc->sc_queue)) != NULL)
    803  1.1  ad 		dpt_scsi_cmd(xs);
    804  1.1  ad }
    805  1.1  ad 
    806  1.1  ad /*
    807  1.1  ad  * Start a SCSI command.
    808  1.1  ad  */
    809  1.1  ad int
    810  1.1  ad dpt_scsi_cmd(xs)
    811  1.1  ad 	struct scsipi_xfer *xs;
    812  1.1  ad {
    813  1.1  ad 	int error, seg, flags, s, fromqueue, dontqueue;
    814  1.1  ad 	struct scsipi_link *sc_link;
    815  1.1  ad 	struct dpt_softc *sc;
    816  1.1  ad 	struct dpt_ccb *ccb;
    817  1.1  ad 	struct eata_sg *sg;
    818  1.1  ad 	struct eata_cp *cp;
    819  1.1  ad 	bus_dma_tag_t dmat;
    820  1.1  ad 
    821  1.1  ad 	sc_link = xs->sc_link;
    822  1.1  ad 	flags = xs->flags;
    823  1.1  ad 	sc = sc_link->adapter_softc;
    824  1.1  ad 	dmat = sc->sc_dmat;
    825  1.1  ad 	fromqueue = 0;
    826  1.1  ad 	dontqueue = 0;
    827  1.1  ad 
    828  1.1  ad 	SC_DEBUG(sc_link, SDEV_DB2, ("dpt_scsi_cmd\n"));
    829  1.1  ad 
    830  1.1  ad 	/* Protect the queue */
    831  1.1  ad 	s = splbio();
    832  1.1  ad 
    833  1.1  ad 	/*
    834  1.1  ad 	 * If we're running the queue from dpt_done_ccb(), we've been called
    835  1.1  ad 	 * with the first queue entry as our argument.
    836  1.1  ad 	 */
    837  1.1  ad 	if (xs == TAILQ_FIRST(&sc->sc_queue)) {
    838  1.1  ad 		TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
    839  1.1  ad 		fromqueue = 1;
    840  1.1  ad 	} else {
    841  1.1  ad 		/* Cmds must be no more than 12 bytes for us */
    842  1.1  ad 		if (xs->cmdlen > 12) {
    843  1.1  ad 			splx(s);
    844  1.1  ad 			xs->error = XS_DRIVER_STUFFUP;
    845  1.1  ad 			return (COMPLETE);
    846  1.1  ad 		}
    847  1.1  ad 
    848  1.1  ad 		/* XXX we can't reset devices just yet */
    849  1.1  ad 		if ((flags & SCSI_RESET) != 0) {
    850  1.1  ad 			xs->error = XS_DRIVER_STUFFUP;
    851  1.1  ad 			return (COMPLETE);
    852  1.1  ad 		}
    853  1.1  ad 
    854  1.1  ad 		/* Polled requests can't be queued for later */
    855  1.1  ad 		dontqueue = flags & SCSI_POLL;
    856  1.1  ad 
    857  1.1  ad 		/* If there are jobs in the queue, run them first */
    858  1.1  ad 		if (TAILQ_FIRST(&sc->sc_queue) != NULL) {
    859  1.1  ad 			/*
    860  1.1  ad 			 * If we can't queue, we have to abort, since we have
    861  1.1  ad 			 * to preserve the queue order.
    862  1.1  ad 			 */
    863  1.1  ad 			if (dontqueue) {
    864  1.1  ad 				splx(s);
    865  1.1  ad 				xs->error = XS_DRIVER_STUFFUP;
    866  1.1  ad 				return (TRY_AGAIN_LATER);
    867  1.1  ad 			}
    868  1.1  ad 
    869  1.1  ad 			/* Swap with the first queue entry. */
    870  1.1  ad 			TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
    871  1.1  ad 			xs = TAILQ_FIRST(&sc->sc_queue);
    872  1.1  ad 			TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
    873  1.1  ad 			fromqueue = 1;
    874  1.1  ad 		}
    875  1.1  ad 	}
    876  1.1  ad 
    877  1.1  ad 	/*
    878  1.1  ad 	 * Get a CCB. If the transfer is from a buf (possibly from interrupt
    879  1.1  ad 	 * time) then we can't allow it to sleep.
    880  1.1  ad 	 */
    881  1.1  ad 	if ((ccb = dpt_alloc_ccb(sc, flags)) == NULL) {
    882  1.1  ad 		/* If we can't queue, we lose */
    883  1.1  ad 		if (dontqueue) {
    884  1.1  ad 			splx(s);
    885  1.1  ad 			xs->error = XS_DRIVER_STUFFUP;
    886  1.1  ad 			return (TRY_AGAIN_LATER);
    887  1.1  ad 		}
    888  1.1  ad 
    889  1.1  ad 		/*
    890  1.1  ad 		 * Stuff request into the queue, in front if we came off
    891  1.1  ad 		 * in the first place.
    892  1.1  ad 		 */
    893  1.1  ad 		if (fromqueue)
    894  1.1  ad 			TAILQ_INSERT_HEAD(&sc->sc_queue, xs, adapter_q);
    895  1.1  ad 		else
    896  1.1  ad 			TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
    897  1.1  ad #ifdef notdef
    898  1.1  ad 		sc->sc_pending++;
    899  1.1  ad #endif
    900  1.1  ad 		splx(s);
    901  1.1  ad 		return (SUCCESSFULLY_QUEUED);
    902  1.1  ad 	}
    903  1.1  ad 
    904  1.1  ad #ifdef notdef
    905  1.1  ad 	/*
    906  1.1  ad 	 * Request has been shifted from pending command queue to executing
    907  1.1  ad 	 * CCB queue. Bump sc_pending to compensate.
    908  1.1  ad 	 */
    909  1.1  ad 	sc->sc_pending--;
    910  1.1  ad #endif
    911  1.1  ad 	splx(s);
    912  1.1  ad 
    913  1.1  ad 	ccb->ccb_xs = xs;
    914  1.1  ad 	ccb->ccb_timeout = xs->timeout;
    915  1.1  ad 
    916  1.1  ad 	cp = &ccb->ccb_eata_cp;
    917  1.1  ad 	memcpy(&cp->cp_scsi_cmd, xs->cmd, xs->cmdlen);
    918  1.1  ad 	cp->cp_ccbid = ccb->ccb_id;
    919  1.1  ad 	cp->cp_id = sc_link->scsipi_scsi.target;
    920  1.1  ad 	cp->cp_lun = sc_link->scsipi_scsi.lun;
    921  1.1  ad 	cp->cp_channel = sc_link->scsipi_scsi.channel;
    922  1.1  ad 	cp->cp_senselen = sizeof(ccb->ccb_sense);
    923  1.1  ad 	cp->cp_stataddr = SWAP32(sc->sc_sppa);
    924  1.1  ad 	cp->cp_dispri = 1;
    925  1.1  ad 	cp->cp_identify = 1;
    926  1.1  ad 	cp->cp_autosense = 1;
    927  1.1  ad 	cp->cp_datain = ((flags & SCSI_DATA_IN) != 0);
    928  1.1  ad 	cp->cp_dataout = ((flags & SCSI_DATA_OUT) != 0);
    929  1.1  ad 	cp->cp_interpret = (sc->sc_hbaid[sc_link->scsipi_scsi.channel] ==
    930  1.1  ad 	    sc_link->scsipi_scsi.target);
    931  1.1  ad 
    932  1.1  ad 	cp->cp_senseaddr = SWAP32(sc->sc_dmamap_ccb->dm_segs[0].ds_addr +
    933  1.1  ad 	    DPT_CCB_OFF(sc, ccb) + offsetof(struct dpt_ccb, ccb_sense));
    934  1.1  ad 
    935  1.1  ad 	if (xs->datalen) {
    936  1.1  ad 		sg = ccb->ccb_sg;
    937  1.1  ad 		seg = 0;
    938  1.1  ad #ifdef	TFS
    939  1.1  ad 		if (flags & SCSI_DATA_UIO) {
    940  1.1  ad 			error = bus_dmamap_load_uio(dmat,
    941  1.1  ad 			    ccb->ccb_dmamap_xfer, (struct uio *)xs->data,
    942  1.1  ad 			    (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
    943  1.1  ad 			    BUS_DMA_WAITOK);
    944  1.1  ad 		} else
    945  1.1  ad #endif /*TFS */
    946  1.1  ad 		{
    947  1.1  ad 			error = bus_dmamap_load(dmat,
    948  1.1  ad 			    ccb->ccb_dmamap_xfer,
    949  1.1  ad 			    xs->data, xs->datalen, NULL,
    950  1.1  ad 			    (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
    951  1.1  ad 			    BUS_DMA_WAITOK);
    952  1.1  ad 		}
    953  1.1  ad 
    954  1.1  ad 		if (error) {
    955  1.1  ad 			printf("%s: dpt_scsi_cmd: ", sc->sc_dv.dv_xname);
    956  1.1  ad 			if (error == EFBIG)
    957  1.1  ad 				printf("more than %d dma segs\n", DPT_SG_SIZE);
    958  1.1  ad 			else
    959  1.1  ad 				printf("error %d loading dma map\n", error);
    960  1.1  ad 
    961  1.1  ad 			xs->error = XS_DRIVER_STUFFUP;
    962  1.1  ad 			dpt_free_ccb(sc, ccb);
    963  1.1  ad 			return (COMPLETE);
    964  1.1  ad 		}
    965  1.1  ad 
    966  1.1  ad 		bus_dmamap_sync(dmat, ccb->ccb_dmamap_xfer, 0,
    967  1.1  ad 		    ccb->ccb_dmamap_xfer->dm_mapsize,
    968  1.1  ad 		    (flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
    969  1.1  ad 		    BUS_DMASYNC_PREWRITE);
    970  1.1  ad 
    971  1.1  ad 		/*
    972  1.1  ad 		 * Load the hardware scatter/gather map with the
    973  1.1  ad 		 * contents of the DMA map.
    974  1.1  ad 		 */
    975  1.1  ad 		for (seg = 0; seg < ccb->ccb_dmamap_xfer->dm_nsegs; seg++) {
    976  1.1  ad 			ccb->ccb_sg[seg].sg_addr =
    977  1.1  ad 			    SWAP32(ccb->ccb_dmamap_xfer->dm_segs[seg].ds_addr);
    978  1.1  ad 			ccb->ccb_sg[seg].sg_len =
    979  1.1  ad 			    SWAP32(ccb->ccb_dmamap_xfer->dm_segs[seg].ds_len);
    980  1.1  ad 		}
    981  1.1  ad 
    982  1.1  ad 		cp->cp_dataaddr = SWAP32(sc->sc_dmamap_ccb->dm_segs[0].ds_addr
    983  1.1  ad 		    + DPT_CCB_OFF(sc, ccb) + offsetof(struct dpt_ccb, ccb_sg));
    984  1.1  ad 		cp->cp_datalen = SWAP32(seg * sizeof(struct eata_sg));
    985  1.1  ad 		cp->cp_scatter = 1;
    986  1.1  ad 	} else {
    987  1.1  ad 		cp->cp_dataaddr = 0;
    988  1.1  ad 		cp->cp_datalen = 0;
    989  1.1  ad 		cp->cp_scatter = 0;
    990  1.1  ad 	}
    991  1.1  ad 
    992  1.1  ad 	/* Sync up CCB and status packet */
    993  1.1  ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb, DPT_CCB_OFF(sc, ccb),
    994  1.1  ad 	    sizeof(struct dpt_ccb), BUS_DMASYNC_PREWRITE);
    995  1.1  ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb, sc->sc_spoff,
    996  1.1  ad 	    sizeof(struct eata_sp), BUS_DMASYNC_PREREAD);
    997  1.1  ad 
    998  1.1  ad 	/*
    999  1.1  ad 	 * Start the command. If we are polling on completion, mark it
   1000  1.1  ad 	 * private so that dpt_intr/dpt_done_ccb don't recycle the CCB
   1001  1.1  ad 	 * without us noticing.
   1002  1.1  ad 	 */
   1003  1.1  ad 	if (dontqueue != 0)
   1004  1.1  ad 		ccb->ccb_flg |= CCB_PRIVATE;
   1005  1.1  ad 
   1006  1.1  ad 	dpt_cmd(sc, &ccb->ccb_eata_cp, ccb->ccb_ccbpa, CP_DMA_CMD, 0);
   1007  1.1  ad 
   1008  1.1  ad 	if (dontqueue == 0)
   1009  1.1  ad 		return (SUCCESSFULLY_QUEUED);
   1010  1.1  ad 
   1011  1.1  ad 	/* Don't wait longer than this single command wants to wait */
   1012  1.1  ad 	if (dpt_poll(sc, ccb)) {
   1013  1.1  ad 		dpt_timeout(ccb);
   1014  1.1  ad 		/* Wait for abort to complete */
   1015  1.1  ad 		if (dpt_poll(sc, ccb))
   1016  1.1  ad 			dpt_timeout(ccb);
   1017  1.1  ad 	}
   1018  1.1  ad 
   1019  1.1  ad 	dpt_done_ccb(sc, ccb);
   1020  1.1  ad 	return (COMPLETE);
   1021  1.1  ad }
   1022  1.1  ad 
   1023  1.1  ad /*
   1024  1.1  ad  * Specified CCB has timed out, abort it.
   1025  1.1  ad  */
   1026  1.1  ad void
   1027  1.1  ad dpt_timeout(arg)
   1028  1.1  ad 	void *arg;
   1029  1.1  ad {
   1030  1.1  ad 	struct scsipi_link *sc_link;
   1031  1.1  ad 	struct scsipi_xfer *xs;
   1032  1.1  ad 	struct dpt_softc *sc;
   1033  1.1  ad  	struct dpt_ccb *ccb;
   1034  1.1  ad 	int s;
   1035  1.1  ad 
   1036  1.1  ad 	ccb = arg;
   1037  1.1  ad 	xs = ccb->ccb_xs;
   1038  1.1  ad 	sc_link = xs->sc_link;
   1039  1.1  ad 	sc  = sc_link->adapter_softc;
   1040  1.1  ad 
   1041  1.1  ad 	scsi_print_addr(sc_link);
   1042  1.1  ad 	printf("timed out (status:%02x aux status:%02x)",
   1043  1.1  ad 	    dpt_inb(sc, HA_STATUS), dpt_inb(sc, HA_AUX_STATUS));
   1044  1.1  ad 
   1045  1.1  ad 	s = splbio();
   1046  1.1  ad 
   1047  1.1  ad 	if ((ccb->ccb_flg & CCB_ABORT) != 0) {
   1048  1.1  ad 		/* Abort timed out, reset the HBA */
   1049  1.1  ad 		printf(" AGAIN, resetting HBA\n");
   1050  1.1  ad 		dpt_outb(sc, HA_COMMAND, CP_RESET);
   1051  1.1  ad 		DELAY(750000);
   1052  1.1  ad 	} else {
   1053  1.1  ad 		/* Abort the operation that has timed out */
   1054  1.1  ad 		printf("\n");
   1055  1.1  ad 		ccb->ccb_xs->error = XS_TIMEOUT;
   1056  1.1  ad 		ccb->ccb_timeout = DPT_ABORT_TIMEOUT;
   1057  1.1  ad 		ccb->ccb_flg |= CCB_ABORT;
   1058  1.1  ad 		/* Start the abort */
   1059  1.1  ad 		dpt_cmd(sc, &ccb->ccb_eata_cp, ccb->ccb_ccbpa, CP_IMMEDIATE,
   1060  1.1  ad 		   CPI_SPEC_ABORT);
   1061  1.1  ad 	}
   1062  1.1  ad 
   1063  1.1  ad 	splx(s);
   1064  1.1  ad }
   1065  1.1  ad 
   1066  1.1  ad #if 0 && defined(DEBUG)
   1067  1.1  ad /*
   1068  1.1  ad  * Dump the contents of an EATA status packet.
   1069  1.1  ad  */
   1070  1.1  ad static void
   1071  1.1  ad dpt_dump_sp(sp)
   1072  1.1  ad 	struct eata_sp *sp;
   1073  1.1  ad {
   1074  1.1  ad 	int i;
   1075  1.1  ad 
   1076  1.1  ad 	printf("\thba_status\t%02x\n", sp->sp_hba_status);
   1077  1.1  ad 	printf("\teoc\t\t%d\n", sp->sp_eoc);
   1078  1.1  ad 	printf("\tscsi_status\t%02x\n", sp->sp_scsi_status);
   1079  1.1  ad 	printf("\tinv_residue\t%d\n", sp->sp_inv_residue);
   1080  1.1  ad 	printf("\tccbid\t\t%d\n", sp->sp_ccbid);
   1081  1.1  ad 	printf("\tid_message\t%d\n", sp->sp_id_message);
   1082  1.1  ad 	printf("\tque_message\t%d\n", sp->sp_que_message);
   1083  1.1  ad 	printf("\ttag_message\t%d\n", sp->sp_tag_message);
   1084  1.1  ad 	printf("\tmessages\t");
   1085  1.1  ad 
   1086  1.1  ad 	for (i = 0; i < 9; i++)
   1087  1.1  ad 		printf("%d ", sp->sp_messages[i]);
   1088  1.1  ad 
   1089  1.1  ad 	printf("\n");
   1090  1.1  ad }
   1091  1.1  ad #endif	/* DEBUG */
   1092  1.1  ad 
   1093  1.1  ad /*
   1094  1.1  ad  * Get inquiry data from the adapter.
   1095  1.1  ad  */
   1096  1.1  ad void
   1097  1.1  ad dpt_hba_inquire(sc, ei)
   1098  1.1  ad 	struct dpt_softc *sc;
   1099  1.1  ad 	struct eata_inquiry_data **ei;
   1100  1.1  ad {
   1101  1.1  ad 	struct dpt_ccb *ccb;
   1102  1.1  ad 	struct eata_cp *cp;
   1103  1.1  ad 	bus_dma_tag_t dmat;
   1104  1.1  ad 
   1105  1.1  ad 	*ei = (struct eata_inquiry_data *)sc->sc_scr;
   1106  1.1  ad 	dmat = sc->sc_dmat;
   1107  1.1  ad 
   1108  1.1  ad 	/* Get a CCB and mark as private */
   1109  1.1  ad 	if ((ccb = dpt_alloc_ccb(sc, 0)) == NULL)
   1110  1.1  ad 		panic("%s: no CCB for inquiry", sc->sc_dv.dv_xname);
   1111  1.1  ad 
   1112  1.1  ad 	ccb->ccb_flg |= CCB_PRIVATE;
   1113  1.1  ad 	ccb->ccb_timeout = 200;
   1114  1.1  ad 
   1115  1.1  ad 	/* Put all the arguments into the CCB */
   1116  1.1  ad 	cp = &ccb->ccb_eata_cp;
   1117  1.1  ad 	cp->cp_ccbid = ccb->ccb_id;
   1118  1.1  ad 	cp->cp_id = sc->sc_hbaid[0];
   1119  1.1  ad 	cp->cp_lun = 0;
   1120  1.1  ad 	cp->cp_channel = 0;
   1121  1.1  ad 	cp->cp_senselen = sizeof(ccb->ccb_sense);
   1122  1.1  ad 	cp->cp_stataddr = SWAP32(sc->sc_sppa);
   1123  1.1  ad 	cp->cp_dispri = 1;
   1124  1.1  ad 	cp->cp_identify = 1;
   1125  1.1  ad 	cp->cp_autosense = 0;
   1126  1.1  ad 	cp->cp_interpret = 1;
   1127  1.1  ad 	cp->cp_datain = 1;
   1128  1.1  ad 	cp->cp_dataout = 0;
   1129  1.1  ad 	cp->cp_senseaddr = 0;
   1130  1.1  ad 	cp->cp_dataaddr = SWAP32(sc->sc_scrpa);
   1131  1.1  ad 	cp->cp_datalen = SWAP32(sizeof(struct eata_inquiry_data));
   1132  1.1  ad 	cp->cp_scatter = 0;
   1133  1.1  ad 
   1134  1.1  ad 	/* Put together the SCSI inquiry command */
   1135  1.1  ad 	memset(&cp->cp_scsi_cmd, 0, 12);	/* XXX */
   1136  1.1  ad 	cp->cp_scsi_cmd = INQUIRY;
   1137  1.1  ad 	cp->cp_len = sizeof(struct eata_inquiry_data);
   1138  1.1  ad 
   1139  1.1  ad 	/* Sync up CCB, status packet and scratch area */
   1140  1.1  ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb, DPT_CCB_OFF(sc, ccb),
   1141  1.1  ad 	    sizeof(struct dpt_ccb), BUS_DMASYNC_PREWRITE);
   1142  1.1  ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb, sc->sc_spoff,
   1143  1.1  ad 	    sizeof(struct eata_sp), BUS_DMASYNC_PREREAD);
   1144  1.1  ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb, sc->sc_scroff,
   1145  1.1  ad 	    sizeof(struct eata_inquiry_data), BUS_DMASYNC_PREREAD);
   1146  1.1  ad 
   1147  1.1  ad 	/* Start the command and poll on completion */
   1148  1.1  ad 	dpt_cmd(sc, &ccb->ccb_eata_cp, ccb->ccb_ccbpa, CP_DMA_CMD, 0);
   1149  1.1  ad 
   1150  1.1  ad 	if (dpt_poll(sc, ccb))
   1151  1.1  ad 		panic("%s: inquiry timed out", sc->sc_dv.dv_xname);
   1152  1.1  ad 
   1153  1.1  ad 	if (ccb->ccb_hba_status != HA_NO_ERROR ||
   1154  1.1  ad 	    ccb->ccb_scsi_status != SCSI_OK)
   1155  1.1  ad 	    	panic("%s: inquiry failed (hba:%02x scsi:%02x",
   1156  1.1  ad 	    	    sc->sc_dv.dv_xname, ccb->ccb_hba_status,
   1157  1.1  ad 	    	    ccb->ccb_scsi_status);
   1158  1.1  ad 
   1159  1.1  ad 	/* Sync up the DMA map and free CCB, returning */
   1160  1.1  ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb, sc->sc_scroff,
   1161  1.1  ad 	    sizeof(struct eata_inquiry_data), BUS_DMASYNC_POSTREAD);
   1162  1.1  ad 	dpt_free_ccb(sc, ccb);
   1163  1.1  ad }
   1164