Home | History | Annotate | Line # | Download | only in ic
dpt.c revision 1.65
      1  1.65       mrg /*	$NetBSD: dpt.c,v 1.65 2011/07/01 08:38:10 mrg Exp $	*/
      2   1.1        ad 
      3   1.1        ad /*-
      4  1.29    bouyer  * Copyright (c) 1997, 1998, 1999, 2000, 2001 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.22        ad  * by Andrew 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  *
     20   1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1        ad  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1        ad  */
     32   1.1        ad 
     33   1.1        ad /*
     34  1.33        ad  * Copyright (c) 1996-2000 Distributed Processing Technology Corporation
     35  1.33        ad  * Copyright (c) 2000 Adaptec Corporation
     36  1.33        ad  * All rights reserved.
     37  1.33        ad  *
     38  1.33        ad  * TERMS AND CONDITIONS OF USE
     39  1.33        ad  *
     40  1.33        ad  * Redistribution and use in source form, with or without modification, are
     41  1.33        ad  * permitted provided that redistributions of source code must retain the
     42  1.33        ad  * above copyright notice, this list of conditions and the following disclaimer.
     43  1.33        ad  *
     44  1.33        ad  * This software is provided `as is' by Adaptec and any express or implied
     45  1.33        ad  * warranties, including, but not limited to, the implied warranties of
     46  1.33        ad  * merchantability and fitness for a particular purpose, are disclaimed. In no
     47  1.33        ad  * event shall Adaptec be liable for any direct, indirect, incidental, special,
     48  1.33        ad  * exemplary or consequential damages (including, but not limited to,
     49  1.33        ad  * procurement of substitute goods or services; loss of use, data, or profits;
     50  1.33        ad  * or business interruptions) however caused and on any theory of liability,
     51  1.33        ad  * whether in contract, strict liability, or tort (including negligence or
     52  1.33        ad  * otherwise) arising in any way out of the use of this driver software, even
     53  1.33        ad  * if advised of the possibility of such damage.
     54  1.33        ad  */
     55  1.33        ad 
     56  1.33        ad /*
     57   1.1        ad  * Portions of this code fall under the following copyright:
     58   1.1        ad  *
     59   1.1        ad  * Originally written by Julian Elischer (julian (at) tfs.com)
     60   1.1        ad  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     61   1.1        ad  *
     62   1.1        ad  * TRW Financial Systems, in accordance with their agreement with Carnegie
     63   1.1        ad  * Mellon University, makes this software available to CMU to distribute
     64   1.1        ad  * or use in any manner that they see fit as long as this message is kept with
     65   1.1        ad  * the software. For this reason TFS also grants any other persons or
     66   1.1        ad  * organisations permission to use or modify this software.
     67   1.1        ad  *
     68   1.1        ad  * TFS supplies this software to be publicly redistributed
     69   1.1        ad  * on the understanding that TFS is not responsible for the correct
     70   1.1        ad  * functioning of this software in any circumstances.
     71   1.1        ad  */
     72  1.31     lukem 
     73  1.31     lukem #include <sys/cdefs.h>
     74  1.65       mrg __KERNEL_RCSID(0, "$NetBSD: dpt.c,v 1.65 2011/07/01 08:38:10 mrg Exp $");
     75   1.1        ad 
     76   1.1        ad #include <sys/param.h>
     77   1.1        ad #include <sys/systm.h>
     78   1.1        ad #include <sys/device.h>
     79   1.1        ad #include <sys/queue.h>
     80   1.1        ad #include <sys/buf.h>
     81  1.13        ad #include <sys/endian.h>
     82  1.33        ad #include <sys/conf.h>
     83  1.53      elad #include <sys/kauth.h>
     84  1.57        ad #include <sys/proc.h>
     85   1.1        ad 
     86  1.58        ad #include <sys/bus.h>
     87  1.33        ad #ifdef i386
     88  1.33        ad #include <machine/pio.h>
     89  1.61        ad #include <machine/cputypes.h>
     90  1.33        ad #endif
     91   1.1        ad 
     92   1.1        ad #include <dev/scsipi/scsi_all.h>
     93   1.1        ad #include <dev/scsipi/scsipi_all.h>
     94   1.1        ad #include <dev/scsipi/scsiconf.h>
     95   1.1        ad 
     96   1.1        ad #include <dev/ic/dptreg.h>
     97   1.1        ad #include <dev/ic/dptvar.h>
     98   1.1        ad 
     99  1.33        ad #include <dev/i2o/dptivar.h>
    100  1.33        ad 
    101  1.33        ad #ifdef DEBUG
    102  1.33        ad #define	DPRINTF(x)		printf x
    103  1.33        ad #else
    104  1.33        ad #define	DPRINTF(x)
    105  1.33        ad #endif
    106  1.33        ad 
    107  1.29    bouyer #define dpt_inb(x, o)		\
    108  1.29    bouyer     bus_space_read_1((x)->sc_iot, (x)->sc_ioh, (o))
    109  1.29    bouyer #define dpt_outb(x, o, d)	\
    110  1.29    bouyer     bus_space_write_1((x)->sc_iot, (x)->sc_ioh, (o), (d))
    111  1.29    bouyer 
    112  1.29    bouyer static const char * const dpt_cname[] = {
    113  1.29    bouyer 	"3334", "SmartRAID IV",
    114  1.29    bouyer 	"3332", "SmartRAID IV",
    115  1.29    bouyer 	"2144", "SmartCache IV",
    116  1.29    bouyer 	"2044", "SmartCache IV",
    117  1.29    bouyer 	"2142", "SmartCache IV",
    118  1.29    bouyer 	"2042", "SmartCache IV",
    119  1.29    bouyer 	"2041", "SmartCache IV",
    120  1.29    bouyer 	"3224", "SmartRAID III",
    121  1.44     perry 	"3222", "SmartRAID III",
    122  1.29    bouyer 	"3021", "SmartRAID III",
    123  1.29    bouyer 	"2124", "SmartCache III",
    124  1.29    bouyer 	"2024", "SmartCache III",
    125  1.29    bouyer 	"2122", "SmartCache III",
    126  1.29    bouyer 	"2022", "SmartCache III",
    127  1.29    bouyer 	"2021", "SmartCache III",
    128  1.44     perry 	"2012", "SmartCache Plus",
    129  1.29    bouyer 	"2011", "SmartCache Plus",
    130  1.29    bouyer 	NULL,   "<unknown>",
    131   1.1        ad };
    132   1.1        ad 
    133  1.29    bouyer static void	*dpt_sdh;
    134  1.29    bouyer 
    135  1.33        ad dev_type_open(dptopen);
    136  1.33        ad dev_type_ioctl(dptioctl);
    137  1.33        ad 
    138  1.33        ad const struct cdevsw dpt_cdevsw = {
    139  1.33        ad 	dptopen, nullclose, noread, nowrite, dptioctl,
    140  1.50  christos 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
    141  1.33        ad };
    142  1.33        ad 
    143  1.33        ad extern struct cfdriver dpt_cd;
    144  1.33        ad 
    145  1.33        ad static struct dpt_sig dpt_sig = {
    146  1.33        ad 	{ 'd', 'P', 't', 'S', 'i', 'G'},
    147  1.33        ad 	SIG_VERSION,
    148  1.33        ad #if defined(i386)
    149  1.33        ad 	PROC_INTEL,
    150  1.33        ad #elif defined(powerpc)
    151  1.33        ad 	PROC_POWERPC,
    152  1.33        ad #elif defined(alpha)
    153  1.33        ad 	PROC_ALPHA,
    154  1.33        ad #elif defined(__mips__)
    155  1.33        ad 	PROC_MIPS,
    156  1.33        ad #elif defined(sparc64)
    157  1.33        ad 	PROC_ULTRASPARC,
    158  1.35      fvdl #else
    159  1.35      fvdl 	0xff,
    160  1.33        ad #endif
    161  1.33        ad #if defined(i386)
    162  1.33        ad 	PROC_386 | PROC_486 | PROC_PENTIUM | PROC_SEXIUM,
    163  1.33        ad #else
    164  1.33        ad 	0,
    165  1.33        ad #endif
    166  1.33        ad 	FT_HBADRVR,
    167  1.33        ad 	0,
    168  1.33        ad 	OEM_DPT,
    169  1.33        ad 	OS_FREE_BSD,	/* XXX */
    170  1.33        ad 	CAP_ABOVE16MB,
    171  1.33        ad 	DEV_ALL,
    172  1.33        ad 	ADF_ALL_EATA,
    173  1.33        ad 	0,
    174  1.33        ad 	0,
    175  1.33        ad 	DPT_VERSION,
    176  1.33        ad 	DPT_REVISION,
    177  1.33        ad 	DPT_SUBREVISION,
    178  1.33        ad 	DPT_MONTH,
    179  1.33        ad 	DPT_DAY,
    180  1.33        ad 	DPT_YEAR,
    181  1.33        ad 	""		/* Will be filled later */
    182  1.33        ad };
    183  1.33        ad 
    184  1.29    bouyer static void	dpt_ccb_abort(struct dpt_softc *, struct dpt_ccb *);
    185  1.29    bouyer static void	dpt_ccb_done(struct dpt_softc *, struct dpt_ccb *);
    186  1.29    bouyer static int	dpt_ccb_map(struct dpt_softc *, struct dpt_ccb *);
    187  1.29    bouyer static int	dpt_ccb_poll(struct dpt_softc *, struct dpt_ccb *);
    188  1.29    bouyer static void	dpt_ccb_unmap(struct dpt_softc *, struct dpt_ccb *);
    189  1.29    bouyer static int	dpt_cmd(struct dpt_softc *, struct dpt_ccb *, int, int);
    190  1.33        ad static void	dpt_ctlrinfo(struct dpt_softc *, struct dpt_eata_ctlrinfo *);
    191  1.29    bouyer static void	dpt_hba_inquire(struct dpt_softc *, struct eata_inquiry_data **);
    192  1.29    bouyer static void	dpt_minphys(struct buf *);
    193  1.34        ad static int	dpt_passthrough(struct dpt_softc *, struct eata_ucp *,
    194  1.45  christos 				struct lwp *);
    195  1.29    bouyer static void	dpt_scsipi_request(struct scsipi_channel *,
    196  1.29    bouyer 				   scsipi_adapter_req_t, void *);
    197  1.29    bouyer static void	dpt_shutdown(void *);
    198  1.33        ad static void	dpt_sysinfo(struct dpt_softc *, struct dpt_sysinfo *);
    199  1.29    bouyer static int	dpt_wait(struct dpt_softc *, u_int8_t, u_int8_t, int);
    200  1.29    bouyer 
    201  1.46     perry static inline struct dpt_ccb	*dpt_ccb_alloc(struct dpt_softc *);
    202  1.46     perry static inline void	dpt_ccb_free(struct dpt_softc *, struct dpt_ccb *);
    203  1.29    bouyer 
    204  1.46     perry static inline struct dpt_ccb *
    205  1.29    bouyer dpt_ccb_alloc(struct dpt_softc *sc)
    206  1.29    bouyer {
    207  1.29    bouyer 	struct dpt_ccb *ccb;
    208  1.29    bouyer 	int s;
    209  1.29    bouyer 
    210  1.29    bouyer 	s = splbio();
    211  1.29    bouyer 	ccb = SLIST_FIRST(&sc->sc_ccb_free);
    212  1.29    bouyer 	SLIST_REMOVE_HEAD(&sc->sc_ccb_free, ccb_chain);
    213  1.29    bouyer 	splx(s);
    214  1.29    bouyer 
    215  1.29    bouyer 	return (ccb);
    216  1.29    bouyer }
    217  1.29    bouyer 
    218  1.46     perry static inline void
    219  1.29    bouyer dpt_ccb_free(struct dpt_softc *sc, struct dpt_ccb *ccb)
    220  1.29    bouyer {
    221  1.29    bouyer 	int s;
    222   1.1        ad 
    223  1.29    bouyer 	ccb->ccb_flg = 0;
    224  1.33        ad 	ccb->ccb_savesp = NULL;
    225  1.29    bouyer 	s = splbio();
    226  1.29    bouyer 	SLIST_INSERT_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
    227  1.29    bouyer 	splx(s);
    228  1.29    bouyer }
    229  1.19        ad 
    230   1.1        ad /*
    231   1.1        ad  * Handle an interrupt from the HBA.
    232   1.1        ad  */
    233   1.1        ad int
    234  1.29    bouyer dpt_intr(void *cookie)
    235   1.1        ad {
    236   1.1        ad 	struct dpt_softc *sc;
    237   1.1        ad 	struct dpt_ccb *ccb;
    238   1.1        ad 	struct eata_sp *sp;
    239  1.19        ad 	volatile int junk;
    240  1.29    bouyer 	int forus;
    241   1.1        ad 
    242  1.29    bouyer 	sc = cookie;
    243  1.19        ad 	sp = sc->sc_stp;
    244  1.29    bouyer 	forus = 0;
    245  1.19        ad 
    246   1.7        ad 	for (;;) {
    247   1.7        ad 		/*
    248   1.7        ad 		 * HBA might have interrupted while we were dealing with the
    249  1.44     perry 		 * last completed command, since we ACK before we deal; keep
    250  1.19        ad 		 * polling.
    251  1.29    bouyer 		 */
    252  1.19        ad 		if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) == 0)
    253   1.7        ad 			break;
    254  1.29    bouyer 		forus = 1;
    255  1.29    bouyer 
    256  1.19        ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_stpoff,
    257   1.1        ad 		    sizeof(struct eata_sp), BUS_DMASYNC_POSTREAD);
    258   1.1        ad 
    259  1.29    bouyer 		/* Might have looped before HBA can reset HBA_AUX_INTR. */
    260   1.1        ad 		if (sp->sp_ccbid == -1) {
    261   1.1        ad 			DELAY(50);
    262  1.19        ad 
    263   1.1        ad 			if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) == 0)
    264   1.1        ad 				return (0);
    265  1.19        ad 
    266  1.59    cegger 			printf("%s: no status\n", device_xname(&sc->sc_dv));
    267  1.19        ad 
    268   1.1        ad 			/* Re-sync DMA map */
    269  1.29    bouyer 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
    270  1.19        ad 			    sc->sc_stpoff, sizeof(struct eata_sp),
    271   1.1        ad 			    BUS_DMASYNC_POSTREAD);
    272   1.1        ad 		}
    273   1.1        ad 
    274  1.29    bouyer 		/* Make sure CCB ID from status packet is realistic. */
    275  1.29    bouyer 		if ((u_int)sp->sp_ccbid >= sc->sc_nccbs) {
    276  1.44     perry 			printf("%s: bogus status (returned CCB id %d)\n",
    277  1.59    cegger 			    device_xname(&sc->sc_dv), sp->sp_ccbid);
    278   1.1        ad 
    279   1.1        ad 			/* Ack the interrupt */
    280  1.20        ad 			sp->sp_ccbid = -1;
    281  1.19        ad 			junk = dpt_inb(sc, HA_STATUS);
    282  1.29    bouyer 			continue;
    283   1.1        ad 		}
    284  1.29    bouyer 
    285  1.29    bouyer 		/* Sync up DMA map and cache cmd status. */
    286  1.29    bouyer 		ccb = sc->sc_ccbs + sp->sp_ccbid;
    287  1.29    bouyer 
    288  1.29    bouyer 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, CCB_OFF(sc, ccb),
    289  1.29    bouyer 		    sizeof(struct dpt_ccb), BUS_DMASYNC_POSTWRITE);
    290  1.29    bouyer 
    291  1.29    bouyer 		ccb->ccb_hba_status = sp->sp_hba_status & 0x7f;
    292  1.29    bouyer 		ccb->ccb_scsi_status = sp->sp_scsi_status;
    293  1.33        ad 		if (ccb->ccb_savesp != NULL)
    294  1.33        ad 			memcpy(ccb->ccb_savesp, sp, sizeof(*sp));
    295  1.29    bouyer 
    296  1.44     perry 		/*
    297  1.29    bouyer 		 * Ack the interrupt and process the CCB.  If this
    298  1.29    bouyer 		 * is a private CCB it's up to dpt_ccb_poll() to
    299  1.29    bouyer 		 * notice.
    300  1.29    bouyer 		 */
    301  1.29    bouyer 		sp->sp_ccbid = -1;
    302  1.29    bouyer 		ccb->ccb_flg |= CCB_INTR;
    303  1.29    bouyer 		junk = dpt_inb(sc, HA_STATUS);
    304  1.29    bouyer 		if ((ccb->ccb_flg & CCB_PRIVATE) == 0)
    305  1.29    bouyer 			dpt_ccb_done(sc, ccb);
    306  1.33        ad 		else if ((ccb->ccb_flg & CCB_WAIT) != 0)
    307  1.33        ad 			wakeup(ccb);
    308   1.1        ad 	}
    309   1.1        ad 
    310  1.29    bouyer 	return (forus);
    311   1.1        ad }
    312   1.1        ad 
    313   1.1        ad /*
    314  1.29    bouyer  * Initialize and attach the HBA.  This is the entry point from bus
    315   1.1        ad  * specific probe-and-attach code.
    316   1.1        ad  */
    317   1.1        ad void
    318  1.29    bouyer dpt_init(struct dpt_softc *sc, const char *intrstr)
    319   1.1        ad {
    320  1.29    bouyer 	struct scsipi_adapter *adapt;
    321  1.29    bouyer 	struct scsipi_channel *chan;
    322   1.1        ad 	struct eata_inquiry_data *ei;
    323  1.29    bouyer 	int i, j, rv, rseg, maxchannel, maxtarget, mapsize;
    324   1.1        ad 	bus_dma_segment_t seg;
    325   1.4        ad 	struct eata_cfg *ec;
    326  1.29    bouyer 	struct dpt_ccb *ccb;
    327  1.47  christos 	char model[__arraycount(ei->ei_model) + __arraycount(ei->ei_suffix) + 1];
    328  1.47  christos 	char vendor[__arraycount(ei->ei_vendor) + 1];
    329  1.29    bouyer 
    330   1.4        ad 	ec = &sc->sc_ec;
    331  1.43    itojun 	snprintf(dpt_sig.dsDescription, sizeof(dpt_sig.dsDescription),
    332  1.43    itojun 	    "NetBSD %s DPT driver", osrelease);
    333  1.33        ad 
    334  1.29    bouyer 	/*
    335  1.29    bouyer 	 * Allocate the CCB/status packet/scratch DMA map and load.
    336  1.29    bouyer 	 */
    337  1.44     perry 	sc->sc_nccbs =
    338  1.19        ad 	    min(be16toh(*(int16_t *)ec->ec_queuedepth), DPT_MAX_CCBS);
    339  1.19        ad 	sc->sc_stpoff = sc->sc_nccbs * sizeof(struct dpt_ccb);
    340  1.19        ad 	sc->sc_scroff = sc->sc_stpoff + sizeof(struct eata_sp);
    341  1.44     perry 	mapsize = sc->sc_nccbs * sizeof(struct dpt_ccb) +
    342  1.29    bouyer 	    DPT_SCRATCH_SIZE + sizeof(struct eata_sp);
    343  1.29    bouyer 
    344  1.29    bouyer 	if ((rv = bus_dmamem_alloc(sc->sc_dmat, mapsize,
    345  1.24   thorpej 	    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    346  1.59    cegger 		aprint_error_dev(&sc->sc_dv, "unable to allocate CCBs, rv = %d\n", rv);
    347   1.1        ad 		return;
    348   1.1        ad 	}
    349   1.1        ad 
    350  1.29    bouyer 	if ((rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, mapsize,
    351  1.56  christos 	    (void **)&sc->sc_ccbs, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    352  1.59    cegger 		aprint_error_dev(&sc->sc_dv, "unable to map CCBs, rv = %d\n",
    353  1.59    cegger 		    rv);
    354   1.1        ad 		return;
    355   1.1        ad 	}
    356   1.1        ad 
    357  1.29    bouyer 	if ((rv = bus_dmamap_create(sc->sc_dmat, mapsize,
    358  1.29    bouyer 	    mapsize, 1, 0, BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    359  1.59    cegger 		aprint_error_dev(&sc->sc_dv, "unable to create CCB DMA map, rv = %d\n", rv);
    360   1.1        ad 		return;
    361   1.1        ad 	}
    362   1.1        ad 
    363  1.29    bouyer 	if ((rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
    364  1.29    bouyer 	    sc->sc_ccbs, mapsize, NULL, BUS_DMA_NOWAIT)) != 0) {
    365  1.59    cegger 		aprint_error_dev(&sc->sc_dv, "unable to load CCB DMA map, rv = %d\n", rv);
    366   1.1        ad 		return;
    367   1.1        ad 	}
    368   1.1        ad 
    369  1.56  christos 	sc->sc_stp = (struct eata_sp *)((char *)sc->sc_ccbs + sc->sc_stpoff);
    370  1.19        ad 	sc->sc_stppa = sc->sc_dmamap->dm_segs[0].ds_addr + sc->sc_stpoff;
    371  1.56  christos 	sc->sc_scr = (char *)sc->sc_ccbs + sc->sc_scroff;
    372  1.19        ad 	sc->sc_scrpa = sc->sc_dmamap->dm_segs[0].ds_addr + sc->sc_scroff;
    373  1.19        ad 	sc->sc_stp->sp_ccbid = -1;
    374   1.4        ad 
    375  1.29    bouyer 	/*
    376  1.29    bouyer 	 * Create the CCBs.
    377  1.29    bouyer 	 */
    378  1.29    bouyer 	SLIST_INIT(&sc->sc_ccb_free);
    379  1.29    bouyer 	memset(sc->sc_ccbs, 0, sizeof(struct dpt_ccb) * sc->sc_nccbs);
    380  1.29    bouyer 
    381  1.29    bouyer 	for (i = 0, ccb = sc->sc_ccbs; i < sc->sc_nccbs; i++, ccb++) {
    382  1.29    bouyer 		rv = bus_dmamap_create(sc->sc_dmat, DPT_MAX_XFER,
    383  1.29    bouyer 		    DPT_SG_SIZE, DPT_MAX_XFER, 0,
    384  1.29    bouyer 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    385  1.29    bouyer 		    &ccb->ccb_dmamap_xfer);
    386  1.29    bouyer 		if (rv) {
    387  1.59    cegger 			aprint_error_dev(&sc->sc_dv, "can't create ccb dmamap (%d)\n", rv);
    388  1.29    bouyer 			break;
    389  1.29    bouyer 		}
    390  1.29    bouyer 
    391  1.29    bouyer 		ccb->ccb_id = i;
    392  1.29    bouyer 		ccb->ccb_ccbpa = sc->sc_dmamap->dm_segs[0].ds_addr +
    393  1.29    bouyer 		    CCB_OFF(sc, ccb);
    394  1.29    bouyer 		SLIST_INSERT_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
    395  1.29    bouyer 	}
    396   1.1        ad 
    397   1.1        ad 	if (i == 0) {
    398  1.59    cegger 		aprint_error_dev(&sc->sc_dv, "unable to create CCBs\n");
    399   1.1        ad 		return;
    400   1.1        ad 	} else if (i != sc->sc_nccbs) {
    401  1.59    cegger 		aprint_error_dev(&sc->sc_dv, "%d/%d CCBs created!\n",
    402  1.39   thorpej 		    i, sc->sc_nccbs);
    403   1.1        ad 		sc->sc_nccbs = i;
    404   1.1        ad 	}
    405   1.1        ad 
    406  1.29    bouyer 	/* Set shutdownhook before we start any device activity. */
    407  1.23        ad 	if (dpt_sdh == NULL)
    408  1.19        ad 		dpt_sdh = shutdownhook_establish(dpt_shutdown, NULL);
    409   1.1        ad 
    410  1.29    bouyer 	/* Get the inquiry data from the HBA. */
    411   1.1        ad 	dpt_hba_inquire(sc, &ei);
    412   1.1        ad 
    413  1.44     perry 	/*
    414   1.1        ad 	 * dpt0 at pci0 dev 12 function 0: DPT SmartRAID III (PM3224A/9X-R)
    415   1.1        ad 	 * dpt0: interrupting at irq 10
    416   1.1        ad 	 * dpt0: 64 queued commands, 1 channel(s), adapter on ID(s) 7
    417   1.1        ad 	 */
    418  1.47  christos 	for (i = 0; ei->ei_vendor[i] != ' ' && i < __arraycount(ei->ei_vendor);
    419  1.47  christos 	    i++)
    420  1.47  christos 		vendor[i] = ei->ei_vendor[i];
    421  1.47  christos 	vendor[i] = '\0';
    422   1.1        ad 
    423  1.47  christos 	for (i = 0; ei->ei_model[i] != ' ' && i < __arraycount(ei->ei_model);
    424  1.47  christos 	    i++)
    425  1.25       cgd 		model[i] = ei->ei_model[i];
    426  1.47  christos 	for (j = 0; ei->ei_suffix[j] != ' ' && j < __arraycount(ei->ei_suffix);
    427  1.47  christos 	    i++, j++)
    428  1.47  christos 		model[i] = ei->ei_suffix[j];
    429   1.1        ad 	model[i] = '\0';
    430   1.1        ad 
    431  1.29    bouyer 	/* Find the marketing name for the board. */
    432   1.9        ad 	for (i = 0; dpt_cname[i] != NULL; i += 2)
    433  1.29    bouyer 		if (memcmp(ei->ei_model + 2, dpt_cname[i], 4) == 0)
    434   1.1        ad 			break;
    435  1.29    bouyer 
    436  1.47  christos 	aprint_normal("%s %s (%s)\n", vendor, dpt_cname[i + 1], model);
    437   1.1        ad 
    438   1.1        ad 	if (intrstr != NULL)
    439  1.59    cegger 		aprint_normal_dev(&sc->sc_dv, "interrupting at %s\n",
    440  1.29    bouyer 		    intrstr);
    441   1.1        ad 
    442  1.19        ad 	maxchannel = (ec->ec_feat3 & EC_F3_MAX_CHANNEL_MASK) >>
    443  1.19        ad 	    EC_F3_MAX_CHANNEL_SHIFT;
    444  1.19        ad 	maxtarget = (ec->ec_feat3 & EC_F3_MAX_TARGET_MASK) >>
    445  1.19        ad 	    EC_F3_MAX_TARGET_SHIFT;
    446  1.19        ad 
    447  1.59    cegger 	aprint_normal_dev(&sc->sc_dv, "%d queued commands, %d channel(s), adapter on ID(s)",
    448  1.59    cegger 	    sc->sc_nccbs, maxchannel + 1);
    449   1.1        ad 
    450  1.19        ad 	for (i = 0; i <= maxchannel; i++) {
    451  1.19        ad 		sc->sc_hbaid[i] = ec->ec_hba[3 - i];
    452  1.39   thorpej 		aprint_normal(" %d", sc->sc_hbaid[i]);
    453  1.19        ad 	}
    454  1.39   thorpej 	aprint_normal("\n");
    455   1.1        ad 
    456  1.29    bouyer 	/*
    457  1.29    bouyer 	 * Reset the SCSI controller chip(s) and bus.  XXX Do we need to do
    458  1.29    bouyer 	 * this for each bus?
    459  1.29    bouyer 	 */
    460  1.29    bouyer 	if (dpt_cmd(sc, NULL, CP_IMMEDIATE, CPI_BUS_RESET))
    461  1.59    cegger 		panic("%s: dpt_cmd failed", device_xname(&sc->sc_dv));
    462  1.29    bouyer 
    463  1.29    bouyer 	/* Fill in the scsipi_adapter. */
    464  1.29    bouyer 	adapt = &sc->sc_adapt;
    465  1.29    bouyer 	memset(adapt, 0, sizeof(*adapt));
    466  1.29    bouyer 	adapt->adapt_dev = &sc->sc_dv;
    467  1.29    bouyer 	adapt->adapt_nchannels = maxchannel + 1;
    468  1.33        ad 	adapt->adapt_openings = sc->sc_nccbs - 1;
    469  1.33        ad 	adapt->adapt_max_periph = sc->sc_nccbs - 1;
    470  1.29    bouyer 	adapt->adapt_request = dpt_scsipi_request;
    471  1.29    bouyer 	adapt->adapt_minphys = dpt_minphys;
    472   1.1        ad 
    473  1.19        ad 	for (i = 0; i <= maxchannel; i++) {
    474  1.29    bouyer 		/* Fill in the scsipi_channel. */
    475  1.29    bouyer 		chan = &sc->sc_chans[i];
    476  1.29    bouyer 		memset(chan, 0, sizeof(*chan));
    477  1.29    bouyer 		chan->chan_adapter = adapt;
    478  1.29    bouyer 		chan->chan_bustype = &scsi_bustype;
    479  1.29    bouyer 		chan->chan_channel = i;
    480  1.29    bouyer 		chan->chan_ntargets = maxtarget + 1;
    481  1.29    bouyer 		chan->chan_nluns = ec->ec_maxlun + 1;
    482  1.29    bouyer 		chan->chan_id = sc->sc_hbaid[i];
    483  1.29    bouyer 		config_found(&sc->sc_dv, chan, scsiprint);
    484  1.23        ad 	}
    485   1.1        ad }
    486   1.1        ad 
    487   1.1        ad /*
    488   1.1        ad  * Read the EATA configuration from the HBA and perform some sanity checks.
    489   1.1        ad  */
    490   1.1        ad int
    491  1.29    bouyer dpt_readcfg(struct dpt_softc *sc)
    492   1.1        ad {
    493   1.4        ad 	struct eata_cfg *ec;
    494   1.1        ad 	int i, j, stat;
    495   1.1        ad 	u_int16_t *p;
    496   1.1        ad 
    497   1.4        ad 	ec = &sc->sc_ec;
    498   1.4        ad 
    499  1.29    bouyer 	/* Older firmware may puke if we talk to it too soon after reset. */
    500   1.4        ad 	dpt_outb(sc, HA_COMMAND, CP_RESET);
    501  1.19        ad 	DELAY(750000);
    502   1.4        ad 
    503   1.4        ad 	for (i = 1000; i; i--) {
    504   1.4        ad 		if ((dpt_inb(sc, HA_STATUS) & HA_ST_READY) != 0)
    505   1.4        ad 			break;
    506   1.4        ad 		DELAY(2000);
    507   1.4        ad 	}
    508  1.29    bouyer 
    509   1.4        ad 	if (i == 0) {
    510  1.17        ad 		printf("%s: HBA not ready after reset (hba status:%02x)\n",
    511  1.59    cegger 		    device_xname(&sc->sc_dv), dpt_inb(sc, HA_STATUS));
    512   1.4        ad 		return (-1);
    513   1.4        ad 	}
    514   1.4        ad 
    515   1.1        ad 	while((((stat = dpt_inb(sc, HA_STATUS))
    516  1.19        ad 	    != (HA_ST_READY|HA_ST_SEEK_COMPLETE))
    517  1.19        ad 	    && (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR))
    518  1.19        ad 	    && (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR|HA_ST_DRQ)))
    519  1.29    bouyer 	    || (dpt_wait(sc, HA_ST_BUSY, 0, 2000))) {
    520  1.19        ad 		/* RAID drives still spinning up? */
    521  1.29    bouyer 		if(dpt_inb(sc, HA_ERROR) != 'D' ||
    522  1.29    bouyer 		   dpt_inb(sc, HA_ERROR + 1) != 'P' ||
    523  1.29    bouyer 		   dpt_inb(sc, HA_ERROR + 2) != 'T') {
    524  1.59    cegger 			printf("%s: HBA not ready\n", device_xname(&sc->sc_dv));
    525  1.19        ad 			return (-1);
    526   1.2        ad 		}
    527  1.19        ad 	}
    528   1.1        ad 
    529  1.44     perry 	/*
    530  1.44     perry 	 * Issue the read-config command and wait for the data to appear.
    531  1.29    bouyer 	 *
    532  1.29    bouyer 	 * Apparently certian firmware revisions won't DMA later on if we
    533  1.29    bouyer 	 * request the config data using PIO, but it makes it a lot easier
    534  1.29    bouyer 	 * as no DMA setup is required.
    535   1.1        ad 	 */
    536   1.1        ad 	dpt_outb(sc, HA_COMMAND, CP_PIO_GETCFG);
    537   1.4        ad 	memset(ec, 0, sizeof(*ec));
    538  1.65       mrg 	i = ((int)(uintptr_t)&((struct eata_cfg *)0)->ec_cfglen +
    539   1.4        ad 	    sizeof(ec->ec_cfglen)) >> 1;
    540   1.4        ad 	p = (u_int16_t *)ec;
    541  1.44     perry 
    542   1.4        ad 	if (dpt_wait(sc, 0xFF, HA_ST_DATA_RDY, 2000)) {
    543  1.29    bouyer 		printf("%s: cfg data didn't appear (hba status:%02x)\n",
    544  1.59    cegger 		    device_xname(&sc->sc_dv), dpt_inb(sc, HA_STATUS));
    545  1.29    bouyer 		return (-1);
    546  1.29    bouyer 	}
    547   1.1        ad 
    548  1.29    bouyer 	/* Begin reading. */
    549  1.29    bouyer 	while (i--)
    550  1.27    briggs 		*p++ = bus_space_read_stream_2(sc->sc_iot, sc->sc_ioh, HA_DATA);
    551   1.1        ad 
    552  1.19        ad 	if ((i = ec->ec_cfglen) > (sizeof(struct eata_cfg)
    553  1.65       mrg 	    - (int)(uintptr_t)(&(((struct eata_cfg *)0L)->ec_cfglen))
    554  1.19        ad 	    - sizeof(ec->ec_cfglen)))
    555  1.19        ad 		i = sizeof(struct eata_cfg)
    556  1.65       mrg 		  - (int)(uintptr_t)(&(((struct eata_cfg *)0L)->ec_cfglen))
    557  1.19        ad 		  - sizeof(ec->ec_cfglen);
    558  1.19        ad 
    559  1.65       mrg 	j = i + (int)(uintptr_t)(&(((struct eata_cfg *)0L)->ec_cfglen)) +
    560  1.19        ad 	    sizeof(ec->ec_cfglen);
    561  1.19        ad 	i >>= 1;
    562   1.1        ad 
    563   1.1        ad 	while (i--)
    564  1.27    briggs 		*p++ = bus_space_read_stream_2(sc->sc_iot, sc->sc_ioh, HA_DATA);
    565  1.29    bouyer 
    566  1.19        ad 	/* Flush until we have read 512 bytes. */
    567  1.19        ad 	i = (512 - j + 1) >> 1;
    568   1.1        ad 	while (i--)
    569  1.48  christos 		(void)bus_space_read_stream_2(sc->sc_iot, sc->sc_ioh, HA_DATA);
    570  1.44     perry 
    571  1.29    bouyer 	/* Defaults for older firmware... */
    572   1.4        ad 	if (p <= (u_short *)&ec->ec_hba[DPT_MAX_CHANNELS - 1])
    573   1.4        ad 		ec->ec_hba[DPT_MAX_CHANNELS - 1] = 7;
    574   1.1        ad 
    575  1.19        ad 	if ((dpt_inb(sc, HA_STATUS) & HA_ST_ERROR) != 0) {
    576  1.59    cegger 		aprint_error_dev(&sc->sc_dv, "HBA error\n");
    577  1.19        ad 		return (-1);
    578  1.19        ad 	}
    579  1.29    bouyer 
    580  1.15        ad 	if (memcmp(ec->ec_eatasig, "EATA", 4) != 0) {
    581  1.59    cegger 		aprint_error_dev(&sc->sc_dv, "EATA signature mismatch\n");
    582   1.1        ad 		return (-1);
    583   1.1        ad 	}
    584  1.29    bouyer 
    585  1.19        ad 	if ((ec->ec_feat0 & EC_F0_HBA_VALID) == 0) {
    586  1.59    cegger 		aprint_error_dev(&sc->sc_dv, "ec_hba field invalid\n");
    587   1.1        ad 		return (-1);
    588   1.1        ad 	}
    589  1.29    bouyer 
    590  1.19        ad 	if ((ec->ec_feat0 & EC_F0_DMA_SUPPORTED) == 0) {
    591  1.59    cegger 		aprint_error_dev(&sc->sc_dv, "DMA not supported\n");
    592   1.1        ad 		return (-1);
    593   1.1        ad 	}
    594   1.1        ad 
    595   1.1        ad 	return (0);
    596   1.1        ad }
    597   1.1        ad 
    598   1.1        ad /*
    599  1.29    bouyer  * Our `shutdownhook' to cleanly shut down the HBA.  The HBA must flush all
    600  1.29    bouyer  * data from it's cache and mark array groups as clean.
    601  1.29    bouyer  *
    602  1.29    bouyer  * XXX This doesn't always work (i.e., the HBA may still be flushing after
    603  1.29    bouyer  * we tell root that it's safe to power off).
    604   1.1        ad  */
    605  1.29    bouyer static void
    606  1.54  christos dpt_shutdown(void *cookie)
    607   1.1        ad {
    608  1.29    bouyer 	extern struct cfdriver dpt_cd;
    609  1.29    bouyer 	struct dpt_softc *sc;
    610  1.29    bouyer 	int i;
    611  1.29    bouyer 
    612  1.29    bouyer 	printf("shutting down dpt devices...");
    613  1.29    bouyer 
    614  1.29    bouyer 	for (i = 0; i < dpt_cd.cd_ndevs; i++) {
    615  1.62   tsutsui 		if ((sc = device_lookup_private(&dpt_cd, i)) == NULL)
    616  1.29    bouyer 			continue;
    617  1.29    bouyer 		dpt_cmd(sc, NULL, CP_IMMEDIATE, CPI_POWEROFF_WARN);
    618  1.29    bouyer 	}
    619   1.1        ad 
    620  1.29    bouyer 	delay(10000*1000);
    621  1.29    bouyer 	printf(" done\n");
    622   1.1        ad }
    623   1.1        ad 
    624   1.1        ad /*
    625  1.29    bouyer  * Send an EATA command to the HBA.
    626   1.1        ad  */
    627  1.29    bouyer static int
    628  1.29    bouyer dpt_cmd(struct dpt_softc *sc, struct dpt_ccb *ccb, int eatacmd, int icmd)
    629   1.1        ad {
    630  1.29    bouyer 	u_int32_t pa;
    631  1.29    bouyer 	int i, s;
    632   1.1        ad 
    633   1.1        ad 	s = splbio();
    634   1.1        ad 
    635  1.29    bouyer 	for (i = 20000; i != 0; i--) {
    636  1.29    bouyer 		if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_BUSY) == 0)
    637  1.29    bouyer 			break;
    638  1.29    bouyer 		DELAY(50);
    639  1.29    bouyer 	}
    640  1.29    bouyer 	if (i == 0) {
    641  1.29    bouyer 		splx(s);
    642  1.29    bouyer 		return (-1);
    643  1.29    bouyer 	}
    644  1.29    bouyer 
    645  1.29    bouyer 	pa = (ccb != NULL ? ccb->ccb_ccbpa : 0);
    646  1.29    bouyer 	dpt_outb(sc, HA_DMA_BASE + 0, (pa      ) & 0xff);
    647  1.29    bouyer 	dpt_outb(sc, HA_DMA_BASE + 1, (pa >>  8) & 0xff);
    648  1.29    bouyer 	dpt_outb(sc, HA_DMA_BASE + 2, (pa >> 16) & 0xff);
    649  1.29    bouyer 	dpt_outb(sc, HA_DMA_BASE + 3, (pa >> 24) & 0xff);
    650  1.29    bouyer 
    651  1.29    bouyer 	if (eatacmd == CP_IMMEDIATE)
    652  1.29    bouyer 		dpt_outb(sc, HA_ICMD, icmd);
    653   1.1        ad 
    654  1.29    bouyer 	dpt_outb(sc, HA_COMMAND, eatacmd);
    655   1.1        ad 
    656  1.29    bouyer 	splx(s);
    657   1.1        ad 	return (0);
    658   1.1        ad }
    659   1.1        ad 
    660   1.1        ad /*
    661  1.29    bouyer  * Wait for the HBA status register to reach a specific state.
    662   1.1        ad  */
    663  1.29    bouyer static int
    664  1.29    bouyer dpt_wait(struct dpt_softc *sc, u_int8_t mask, u_int8_t state, int ms)
    665   1.1        ad {
    666   1.1        ad 
    667  1.29    bouyer 	for (ms *= 10; ms != 0; ms--) {
    668  1.29    bouyer 		if ((dpt_inb(sc, HA_STATUS) & mask) == state)
    669  1.29    bouyer 			return (0);
    670  1.29    bouyer 		DELAY(100);
    671   1.1        ad 	}
    672   1.1        ad 
    673  1.29    bouyer 	return (-1);
    674   1.1        ad }
    675   1.1        ad 
    676   1.1        ad /*
    677  1.29    bouyer  * Spin waiting for a command to finish.  The timeout value from the CCB is
    678  1.29    bouyer  * used.  The CCB must be marked with CCB_PRIVATE, otherwise it'll will get
    679  1.29    bouyer  * recycled before we get a look at it.
    680   1.1        ad  */
    681  1.29    bouyer static int
    682  1.29    bouyer dpt_ccb_poll(struct dpt_softc *sc, struct dpt_ccb *ccb)
    683   1.1        ad {
    684  1.29    bouyer 	int i, s;
    685  1.29    bouyer 
    686  1.29    bouyer #ifdef DEBUG
    687  1.29    bouyer 	if ((ccb->ccb_flg & CCB_PRIVATE) == 0)
    688  1.32    provos 		panic("dpt_ccb_poll: called for non-CCB_PRIVATE request");
    689  1.29    bouyer #endif
    690   1.1        ad 
    691   1.1        ad 	s = splbio();
    692   1.1        ad 
    693  1.29    bouyer 	if ((ccb->ccb_flg & CCB_INTR) != 0) {
    694  1.29    bouyer 		splx(s);
    695  1.29    bouyer 		return (0);
    696  1.29    bouyer 	}
    697  1.29    bouyer 
    698  1.29    bouyer 	for (i = ccb->ccb_timeout * 20; i != 0; i--) {
    699  1.29    bouyer 		if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) != 0)
    700  1.29    bouyer 			dpt_intr(sc);
    701  1.29    bouyer 		if ((ccb->ccb_flg & CCB_INTR) != 0)
    702   1.1        ad 			break;
    703  1.29    bouyer 		DELAY(50);
    704   1.1        ad 	}
    705   1.1        ad 
    706   1.1        ad 	splx(s);
    707  1.29    bouyer 	return (i == 0);
    708   1.1        ad }
    709   1.1        ad 
    710   1.1        ad /*
    711  1.29    bouyer  * We have a command which has been processed by the HBA, so now we look to
    712  1.29    bouyer  * see how the operation went.  CCBs marked CCB_PRIVATE are not passed here
    713  1.29    bouyer  * by dpt_intr().
    714   1.1        ad  */
    715  1.29    bouyer static void
    716  1.29    bouyer dpt_ccb_done(struct dpt_softc *sc, struct dpt_ccb *ccb)
    717   1.1        ad {
    718   1.1        ad 	struct scsipi_xfer *xs;
    719  1.29    bouyer 
    720   1.1        ad 	xs = ccb->ccb_xs;
    721   1.1        ad 
    722  1.29    bouyer 	SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("dpt_ccb_done\n"));
    723   1.1        ad 
    724   1.1        ad 	/*
    725  1.44     perry 	 * If we were a data transfer, unload the map that described the
    726   1.1        ad 	 * data buffer.
    727   1.1        ad 	 */
    728  1.29    bouyer 	if (xs->datalen != 0)
    729  1.29    bouyer 		dpt_ccb_unmap(sc, ccb);
    730   1.1        ad 
    731   1.1        ad 	if (xs->error == XS_NOERROR) {
    732  1.19        ad 		if (ccb->ccb_hba_status != SP_HBA_NO_ERROR) {
    733   1.1        ad 			switch (ccb->ccb_hba_status) {
    734  1.19        ad 			case SP_HBA_ERROR_SEL_TO:
    735   1.1        ad 				xs->error = XS_SELTIMEOUT;
    736   1.1        ad 				break;
    737  1.19        ad 			case SP_HBA_ERROR_RESET:
    738   1.1        ad 				xs->error = XS_RESET;
    739   1.1        ad 				break;
    740  1.29    bouyer 			default:
    741   1.1        ad 				printf("%s: HBA status %x\n",
    742  1.59    cegger 				    device_xname(&sc->sc_dv), ccb->ccb_hba_status);
    743   1.1        ad 				xs->error = XS_DRIVER_STUFFUP;
    744  1.29    bouyer 				break;
    745   1.1        ad 			}
    746   1.1        ad 		} else if (ccb->ccb_scsi_status != SCSI_OK) {
    747   1.1        ad 			switch (ccb->ccb_scsi_status) {
    748   1.1        ad 			case SCSI_CHECK:
    749  1.29    bouyer 				memcpy(&xs->sense.scsi_sense, &ccb->ccb_sense,
    750  1.29    bouyer 				    sizeof(xs->sense.scsi_sense));
    751   1.1        ad 				xs->error = XS_SENSE;
    752   1.1        ad 				break;
    753   1.1        ad 			case SCSI_BUSY:
    754  1.29    bouyer 			case SCSI_QUEUE_FULL:
    755   1.1        ad 				xs->error = XS_BUSY;
    756   1.1        ad 				break;
    757   1.1        ad 			default:
    758  1.29    bouyer 				scsipi_printaddr(xs->xs_periph);
    759  1.29    bouyer 				printf("SCSI status %x\n",
    760  1.29    bouyer 				    ccb->ccb_scsi_status);
    761   1.1        ad 				xs->error = XS_DRIVER_STUFFUP;
    762  1.29    bouyer 				break;
    763   1.1        ad 			}
    764   1.1        ad 		} else
    765   1.1        ad 			xs->resid = 0;
    766  1.29    bouyer 
    767   1.7        ad 		xs->status = ccb->ccb_scsi_status;
    768   1.1        ad 	}
    769   1.1        ad 
    770  1.29    bouyer 	/* Free up the CCB and mark the command as done. */
    771  1.29    bouyer 	dpt_ccb_free(sc, ccb);
    772   1.1        ad 	scsipi_done(xs);
    773   1.1        ad }
    774   1.1        ad 
    775   1.1        ad /*
    776  1.29    bouyer  * Specified CCB has timed out, abort it.
    777   1.1        ad  */
    778  1.29    bouyer static void
    779  1.29    bouyer dpt_ccb_abort(struct dpt_softc *sc, struct dpt_ccb *ccb)
    780  1.29    bouyer {
    781  1.29    bouyer 	struct scsipi_periph *periph;
    782   1.1        ad 	struct scsipi_xfer *xs;
    783  1.29    bouyer 	int s;
    784   1.1        ad 
    785  1.29    bouyer 	xs = ccb->ccb_xs;
    786  1.29    bouyer 	periph = xs->xs_periph;
    787   1.1        ad 
    788  1.29    bouyer 	scsipi_printaddr(periph);
    789  1.44     perry 	printf("timed out (status:%02x aux status:%02x)",
    790  1.29    bouyer 	    dpt_inb(sc, HA_STATUS), dpt_inb(sc, HA_AUX_STATUS));
    791   1.1        ad 
    792   1.1        ad 	s = splbio();
    793   1.1        ad 
    794  1.29    bouyer 	if ((ccb->ccb_flg & CCB_ABORT) != 0) {
    795  1.29    bouyer 		/* Abort timed out, reset the HBA */
    796  1.29    bouyer 		printf(" AGAIN, resetting HBA\n");
    797  1.29    bouyer 		dpt_outb(sc, HA_COMMAND, CP_RESET);
    798  1.29    bouyer 		DELAY(750000);
    799   1.1        ad 	} else {
    800  1.29    bouyer 		/* Abort the operation that has timed out */
    801  1.29    bouyer 		printf("\n");
    802  1.29    bouyer 		xs->error = XS_TIMEOUT;
    803  1.29    bouyer 		ccb->ccb_timeout = DPT_ABORT_TIMEOUT;
    804  1.29    bouyer 		ccb->ccb_flg |= CCB_ABORT;
    805  1.29    bouyer 		/* Start the abort */
    806  1.29    bouyer 		if (dpt_cmd(sc, ccb, CP_IMMEDIATE, CPI_SPEC_ABORT))
    807  1.59    cegger 			aprint_error_dev(&sc->sc_dv, "dpt_cmd failed\n");
    808  1.29    bouyer 	}
    809  1.29    bouyer 
    810  1.29    bouyer 	splx(s);
    811  1.29    bouyer }
    812   1.1        ad 
    813  1.29    bouyer /*
    814  1.29    bouyer  * Map a data transfer.
    815  1.29    bouyer  */
    816  1.29    bouyer static int
    817  1.29    bouyer dpt_ccb_map(struct dpt_softc *sc, struct dpt_ccb *ccb)
    818  1.29    bouyer {
    819  1.29    bouyer 	struct scsipi_xfer *xs;
    820  1.29    bouyer 	bus_dmamap_t xfer;
    821  1.29    bouyer 	bus_dma_segment_t *ds;
    822  1.29    bouyer 	struct eata_sg *sg;
    823  1.29    bouyer 	struct eata_cp *cp;
    824  1.29    bouyer 	int rv, i;
    825   1.1        ad 
    826  1.29    bouyer 	xs = ccb->ccb_xs;
    827  1.29    bouyer 	xfer = ccb->ccb_dmamap_xfer;
    828  1.29    bouyer 	cp = &ccb->ccb_eata_cp;
    829   1.1        ad 
    830  1.29    bouyer 	rv = bus_dmamap_load(sc->sc_dmat, xfer, xs->data, xs->datalen, NULL,
    831  1.44     perry 	    ((xs->xs_control & XS_CTL_NOSLEEP) != 0 ?
    832  1.30   thorpej 	    BUS_DMA_NOWAIT : BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
    833  1.30   thorpej 	    ((xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMA_READ : BUS_DMA_WRITE));
    834  1.29    bouyer 
    835  1.29    bouyer 	switch (rv) {
    836  1.29    bouyer 	case 0:
    837  1.29    bouyer 		break;
    838  1.29    bouyer 	case ENOMEM:
    839  1.29    bouyer 	case EAGAIN:
    840  1.29    bouyer 		xs->error = XS_RESOURCE_SHORTAGE;
    841  1.29    bouyer 		break;
    842  1.29    bouyer 	default:
    843  1.29    bouyer 		xs->error = XS_DRIVER_STUFFUP;
    844  1.59    cegger 		printf("%s: error %d loading map\n", device_xname(&sc->sc_dv), rv);
    845  1.29    bouyer 		break;
    846  1.29    bouyer 	}
    847   1.1        ad 
    848  1.29    bouyer 	if (xs->error != XS_NOERROR) {
    849  1.29    bouyer 		dpt_ccb_free(sc, ccb);
    850  1.29    bouyer 		scsipi_done(xs);
    851  1.29    bouyer 		return (-1);
    852   1.1        ad 	}
    853   1.1        ad 
    854  1.29    bouyer 	bus_dmamap_sync(sc->sc_dmat, xfer, 0, xfer->dm_mapsize,
    855  1.29    bouyer 	    (xs->xs_control & XS_CTL_DATA_IN) != 0 ? BUS_DMASYNC_PREREAD :
    856  1.29    bouyer 	    BUS_DMASYNC_PREWRITE);
    857  1.29    bouyer 
    858  1.29    bouyer 	/* Don't bother using scatter/gather for just 1 seg */
    859  1.29    bouyer 	if (xfer->dm_nsegs == 1) {
    860  1.29    bouyer 		cp->cp_dataaddr = htobe32(xfer->dm_segs[0].ds_addr);
    861  1.29    bouyer 		cp->cp_datalen = htobe32(xfer->dm_segs[0].ds_len);
    862  1.29    bouyer 	} else {
    863  1.29    bouyer 		/*
    864  1.44     perry 		 * Load the hardware scatter/gather map with
    865  1.29    bouyer 		 * the contents of the DMA map.
    866   1.1        ad 		 */
    867  1.29    bouyer 		sg = ccb->ccb_sg;
    868  1.29    bouyer 		ds = xfer->dm_segs;
    869  1.29    bouyer 		for (i = 0; i < xfer->dm_nsegs; i++, sg++, ds++) {
    870  1.29    bouyer  			sg->sg_addr = htobe32(ds->ds_addr);
    871  1.29    bouyer  			sg->sg_len =  htobe32(ds->ds_len);
    872  1.29    bouyer  		}
    873  1.44     perry 	 	cp->cp_dataaddr = htobe32(CCB_OFF(sc, ccb) +
    874  1.29    bouyer 		    sc->sc_dmamap->dm_segs[0].ds_addr +
    875  1.29    bouyer 		    offsetof(struct dpt_ccb, ccb_sg));
    876  1.29    bouyer 		cp->cp_datalen = htobe32(i * sizeof(struct eata_sg));
    877  1.29    bouyer 		cp->cp_ctl0 |= CP_C0_SCATTER;
    878   1.1        ad 	}
    879   1.1        ad 
    880  1.29    bouyer 	return (0);
    881  1.29    bouyer }
    882  1.29    bouyer 
    883  1.29    bouyer /*
    884  1.29    bouyer  * Unmap a transfer.
    885  1.29    bouyer  */
    886  1.29    bouyer static void
    887  1.29    bouyer dpt_ccb_unmap(struct dpt_softc *sc, struct dpt_ccb *ccb)
    888  1.29    bouyer {
    889  1.29    bouyer 
    890  1.29    bouyer 	bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
    891  1.29    bouyer 	    ccb->ccb_dmamap_xfer->dm_mapsize,
    892  1.29    bouyer 	    (ccb->ccb_eata_cp.cp_ctl0 & CP_C0_DATA_IN) != 0 ?
    893  1.29    bouyer 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    894  1.29    bouyer 	bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
    895  1.29    bouyer }
    896  1.29    bouyer 
    897  1.29    bouyer /*
    898  1.29    bouyer  * Adjust the size of each I/O before it passes to the SCSI layer.
    899  1.29    bouyer  */
    900  1.29    bouyer static void
    901  1.29    bouyer dpt_minphys(struct buf *bp)
    902  1.29    bouyer {
    903  1.29    bouyer 
    904  1.29    bouyer 	if (bp->b_bcount > DPT_MAX_XFER)
    905  1.29    bouyer 		bp->b_bcount = DPT_MAX_XFER;
    906  1.29    bouyer 	minphys(bp);
    907  1.29    bouyer }
    908  1.29    bouyer 
    909  1.29    bouyer /*
    910  1.29    bouyer  * Start a SCSI command.
    911  1.29    bouyer  */
    912  1.29    bouyer static void
    913  1.29    bouyer dpt_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    914  1.29    bouyer 		   void *arg)
    915  1.29    bouyer {
    916  1.29    bouyer 	struct dpt_softc *sc;
    917  1.29    bouyer 	struct scsipi_xfer *xs;
    918  1.29    bouyer 	int flags;
    919  1.29    bouyer 	struct scsipi_periph *periph;
    920  1.29    bouyer 	struct dpt_ccb *ccb;
    921  1.29    bouyer 	struct eata_cp *cp;
    922  1.29    bouyer 
    923  1.29    bouyer 	sc = (struct dpt_softc *)chan->chan_adapter->adapt_dev;
    924   1.1        ad 
    925  1.29    bouyer 	switch (req) {
    926  1.29    bouyer 	case ADAPTER_REQ_RUN_XFER:
    927  1.29    bouyer 		xs = arg;
    928  1.29    bouyer 		periph = xs->xs_periph;
    929  1.29    bouyer 		flags = xs->xs_control;
    930   1.1        ad 
    931  1.29    bouyer #ifdef DIAGNOSTIC
    932  1.29    bouyer 		/* Cmds must be no more than 12 bytes for us. */
    933  1.29    bouyer 		if (xs->cmdlen > 12) {
    934  1.29    bouyer 			xs->error = XS_DRIVER_STUFFUP;
    935  1.29    bouyer 			scsipi_done(xs);
    936  1.29    bouyer 			break;
    937   1.1        ad 		}
    938  1.29    bouyer #endif
    939  1.29    bouyer 		/*
    940  1.29    bouyer 		 * XXX We can't reset devices just yet.  Apparently some
    941  1.29    bouyer 		 * older firmware revisions don't even support it.
    942  1.29    bouyer 		 */
    943  1.29    bouyer 		if ((flags & XS_CTL_RESET) != 0) {
    944   1.1        ad 			xs->error = XS_DRIVER_STUFFUP;
    945  1.29    bouyer 			scsipi_done(xs);
    946  1.29    bouyer 			break;
    947   1.1        ad 		}
    948   1.1        ad 
    949  1.29    bouyer 		/*
    950  1.29    bouyer 		 * Get a CCB and fill it.
    951  1.29    bouyer 		 */
    952  1.29    bouyer 		ccb = dpt_ccb_alloc(sc);
    953  1.29    bouyer 		ccb->ccb_xs = xs;
    954  1.29    bouyer 		ccb->ccb_timeout = xs->timeout;
    955  1.29    bouyer 
    956  1.29    bouyer 		cp = &ccb->ccb_eata_cp;
    957  1.29    bouyer 		memcpy(&cp->cp_cdb_cmd, xs->cmd, xs->cmdlen);
    958  1.29    bouyer 		cp->cp_ccbid = ccb->ccb_id;
    959  1.29    bouyer 		cp->cp_senselen = sizeof(ccb->ccb_sense);
    960  1.29    bouyer 		cp->cp_stataddr = htobe32(sc->sc_stppa);
    961  1.29    bouyer 		cp->cp_ctl0 = CP_C0_AUTO_SENSE;
    962  1.44     perry 		cp->cp_ctl1 = 0;
    963  1.29    bouyer 		cp->cp_ctl2 = 0;
    964  1.29    bouyer 		cp->cp_ctl3 = periph->periph_target << CP_C3_ID_SHIFT;
    965  1.29    bouyer 		cp->cp_ctl3 |= chan->chan_channel << CP_C3_CHANNEL_SHIFT;
    966  1.29    bouyer 		cp->cp_ctl4 = periph->periph_lun << CP_C4_LUN_SHIFT;
    967  1.29    bouyer 		cp->cp_ctl4 |= CP_C4_DIS_PRI | CP_C4_IDENTIFY;
    968  1.29    bouyer 
    969  1.29    bouyer 		if ((flags & XS_CTL_DATA_IN) != 0)
    970  1.29    bouyer 			cp->cp_ctl0 |= CP_C0_DATA_IN;
    971  1.29    bouyer 		if ((flags & XS_CTL_DATA_OUT) != 0)
    972  1.29    bouyer 			cp->cp_ctl0 |= CP_C0_DATA_OUT;
    973  1.29    bouyer 		if (sc->sc_hbaid[chan->chan_channel] == periph->periph_target)
    974  1.29    bouyer 			cp->cp_ctl0 |= CP_C0_INTERPRET;
    975  1.29    bouyer 
    976  1.29    bouyer 		/* Synchronous xfers musn't write-back through the cache. */
    977  1.29    bouyer 		if (xs->bp != NULL)
    978  1.29    bouyer 			if ((xs->bp->b_flags & (B_ASYNC | B_READ)) == 0)
    979  1.29    bouyer 				cp->cp_ctl2 |= CP_C2_NO_CACHE;
    980  1.29    bouyer 
    981  1.29    bouyer 		cp->cp_senseaddr =
    982  1.29    bouyer 		    htobe32(sc->sc_dmamap->dm_segs[0].ds_addr +
    983  1.29    bouyer 		    CCB_OFF(sc, ccb) + offsetof(struct dpt_ccb, ccb_sense));
    984   1.1        ad 
    985  1.29    bouyer 		if (xs->datalen != 0) {
    986  1.29    bouyer 			if (dpt_ccb_map(sc, ccb))
    987  1.29    bouyer 				break;
    988   1.9        ad 		} else {
    989  1.29    bouyer 			cp->cp_dataaddr = 0;
    990  1.29    bouyer 			cp->cp_datalen = 0;
    991   1.1        ad 		}
    992   1.1        ad 
    993  1.29    bouyer 		/* Sync up CCB and status packet. */
    994  1.29    bouyer 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
    995  1.29    bouyer 		    CCB_OFF(sc, ccb), sizeof(struct dpt_ccb),
    996  1.29    bouyer 		    BUS_DMASYNC_PREWRITE);
    997  1.29    bouyer 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_stpoff,
    998  1.29    bouyer 		    sizeof(struct eata_sp), BUS_DMASYNC_PREREAD);
    999   1.1        ad 
   1000  1.44     perry 		/*
   1001  1.29    bouyer 		 * Start the command.
   1002  1.29    bouyer 		 */
   1003  1.29    bouyer 		if ((xs->xs_control & XS_CTL_POLL) != 0)
   1004  1.44     perry 			ccb->ccb_flg |= CCB_PRIVATE;
   1005   1.1        ad 
   1006  1.29    bouyer 		if (dpt_cmd(sc, ccb, CP_DMA_CMD, 0)) {
   1007  1.59    cegger 			aprint_error_dev(&sc->sc_dv, "dpt_cmd failed\n");
   1008  1.29    bouyer 			xs->error = XS_DRIVER_STUFFUP;
   1009  1.29    bouyer 			if (xs->datalen != 0)
   1010  1.29    bouyer 				dpt_ccb_unmap(sc, ccb);
   1011  1.29    bouyer 			dpt_ccb_free(sc, ccb);
   1012  1.29    bouyer 			break;
   1013  1.29    bouyer 		}
   1014   1.1        ad 
   1015  1.29    bouyer 		if ((xs->xs_control & XS_CTL_POLL) == 0)
   1016  1.29    bouyer 			break;
   1017   1.1        ad 
   1018  1.29    bouyer 		if (dpt_ccb_poll(sc, ccb)) {
   1019  1.29    bouyer 			dpt_ccb_abort(sc, ccb);
   1020  1.29    bouyer 			/* Wait for abort to complete... */
   1021  1.29    bouyer 			if (dpt_ccb_poll(sc, ccb))
   1022  1.29    bouyer 				dpt_ccb_abort(sc, ccb);
   1023  1.44     perry 		}
   1024   1.1        ad 
   1025  1.29    bouyer 		dpt_ccb_done(sc, ccb);
   1026  1.29    bouyer 		break;
   1027   1.1        ad 
   1028  1.29    bouyer 	case ADAPTER_REQ_GROW_RESOURCES:
   1029  1.29    bouyer 		/*
   1030  1.29    bouyer 		 * Not supported, since we allocate the maximum number of
   1031  1.29    bouyer 		 * CCBs up front.
   1032  1.29    bouyer 		 */
   1033  1.29    bouyer 		break;
   1034   1.1        ad 
   1035  1.29    bouyer 	case ADAPTER_REQ_SET_XFER_MODE:
   1036  1.29    bouyer 		/*
   1037  1.29    bouyer 		 * This will be handled by the HBA itself, and we can't
   1038  1.29    bouyer 		 * modify that (ditto for tagged queueing).
   1039  1.29    bouyer 		 */
   1040  1.29    bouyer 		break;
   1041   1.1        ad 	}
   1042   1.1        ad }
   1043   1.1        ad 
   1044   1.1        ad /*
   1045   1.1        ad  * Get inquiry data from the adapter.
   1046   1.1        ad  */
   1047  1.29    bouyer static void
   1048  1.29    bouyer dpt_hba_inquire(struct dpt_softc *sc, struct eata_inquiry_data **ei)
   1049   1.1        ad {
   1050   1.1        ad 	struct dpt_ccb *ccb;
   1051   1.1        ad 	struct eata_cp *cp;
   1052  1.29    bouyer 
   1053   1.1        ad 	*ei = (struct eata_inquiry_data *)sc->sc_scr;
   1054   1.1        ad 
   1055   1.1        ad 	/* Get a CCB and mark as private */
   1056  1.29    bouyer 	ccb = dpt_ccb_alloc(sc);
   1057   1.1        ad 	ccb->ccb_flg |= CCB_PRIVATE;
   1058   1.1        ad 	ccb->ccb_timeout = 200;
   1059   1.1        ad 
   1060  1.29    bouyer 	/* Put all the arguments into the CCB. */
   1061   1.1        ad 	cp = &ccb->ccb_eata_cp;
   1062   1.1        ad 	cp->cp_ccbid = ccb->ccb_id;
   1063   1.1        ad 	cp->cp_senselen = sizeof(ccb->ccb_sense);
   1064  1.29    bouyer 	cp->cp_senseaddr = 0;
   1065  1.19        ad 	cp->cp_stataddr = htobe32(sc->sc_stppa);
   1066  1.13        ad 	cp->cp_dataaddr = htobe32(sc->sc_scrpa);
   1067  1.13        ad 	cp->cp_datalen = htobe32(sizeof(struct eata_inquiry_data));
   1068  1.19        ad 	cp->cp_ctl0 = CP_C0_DATA_IN | CP_C0_INTERPRET;
   1069  1.19        ad 	cp->cp_ctl1 = 0;
   1070  1.19        ad 	cp->cp_ctl2 = 0;
   1071  1.19        ad 	cp->cp_ctl3 = sc->sc_hbaid[0] << CP_C3_ID_SHIFT;
   1072  1.19        ad 	cp->cp_ctl4 = CP_C4_DIS_PRI | CP_C4_IDENTIFY;
   1073  1.19        ad 
   1074  1.29    bouyer 	/* Put together the SCSI inquiry command. */
   1075  1.29    bouyer 	memset(&cp->cp_cdb_cmd, 0, 12);
   1076  1.19        ad 	cp->cp_cdb_cmd = INQUIRY;
   1077  1.19        ad 	cp->cp_cdb_len = sizeof(struct eata_inquiry_data);
   1078   1.1        ad 
   1079  1.29    bouyer 	/* Sync up CCB, status packet and scratch area. */
   1080  1.44     perry 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, CCB_OFF(sc, ccb),
   1081   1.1        ad 	    sizeof(struct dpt_ccb), BUS_DMASYNC_PREWRITE);
   1082  1.44     perry 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_stpoff,
   1083   1.1        ad 	    sizeof(struct eata_sp), BUS_DMASYNC_PREREAD);
   1084  1.44     perry 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_scroff,
   1085   1.1        ad 	    sizeof(struct eata_inquiry_data), BUS_DMASYNC_PREREAD);
   1086   1.1        ad 
   1087  1.29    bouyer 	/* Start the command and poll on completion. */
   1088  1.29    bouyer 	if (dpt_cmd(sc, ccb, CP_DMA_CMD, 0))
   1089  1.59    cegger 		panic("%s: dpt_cmd failed", device_xname(&sc->sc_dv));
   1090   1.1        ad 
   1091  1.29    bouyer 	if (dpt_ccb_poll(sc, ccb))
   1092  1.59    cegger 		panic("%s: inquiry timed out", device_xname(&sc->sc_dv));
   1093   1.1        ad 
   1094  1.19        ad 	if (ccb->ccb_hba_status != SP_HBA_NO_ERROR ||
   1095   1.1        ad 	    ccb->ccb_scsi_status != SCSI_OK)
   1096  1.29    bouyer 		panic("%s: inquiry failed (hba:%02x scsi:%02x)",
   1097  1.59    cegger 		    device_xname(&sc->sc_dv), ccb->ccb_hba_status,
   1098  1.29    bouyer 		    ccb->ccb_scsi_status);
   1099  1.29    bouyer 
   1100  1.29    bouyer 	/* Sync up the DMA map and free CCB, returning. */
   1101  1.44     perry 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_scroff,
   1102   1.1        ad 	    sizeof(struct eata_inquiry_data), BUS_DMASYNC_POSTREAD);
   1103  1.29    bouyer 	dpt_ccb_free(sc, ccb);
   1104  1.33        ad }
   1105  1.33        ad 
   1106  1.33        ad int
   1107  1.54  christos dptopen(dev_t dev, int flag, int mode, struct lwp *l)
   1108  1.33        ad {
   1109  1.33        ad 
   1110  1.33        ad 	if (device_lookup(&dpt_cd, minor(dev)) == NULL)
   1111  1.33        ad 		return (ENXIO);
   1112  1.33        ad 
   1113  1.33        ad 	return (0);
   1114  1.33        ad }
   1115  1.33        ad 
   1116  1.33        ad int
   1117  1.56  christos dptioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1118  1.33        ad {
   1119  1.33        ad 	struct dpt_softc *sc;
   1120  1.33        ad 	int rv;
   1121  1.33        ad 
   1122  1.62   tsutsui 	sc = device_lookup_private(&dpt_cd, minor(dev));
   1123  1.33        ad 
   1124  1.33        ad 	switch (cmd & 0xffff) {
   1125  1.33        ad 	case DPT_SIGNATURE:
   1126  1.33        ad 		memcpy(data, &dpt_sig, min(IOCPARM_LEN(cmd), sizeof(dpt_sig)));
   1127  1.33        ad 		break;
   1128  1.33        ad 
   1129  1.33        ad 	case DPT_CTRLINFO:
   1130  1.33        ad 		dpt_ctlrinfo(sc, (struct dpt_eata_ctlrinfo *)data);
   1131  1.33        ad 		break;
   1132  1.33        ad 
   1133  1.33        ad 	case DPT_SYSINFO:
   1134  1.33        ad 		dpt_sysinfo(sc, (struct dpt_sysinfo *)data);
   1135  1.33        ad 		break;
   1136  1.33        ad 
   1137  1.33        ad 	case DPT_BLINKLED:
   1138  1.33        ad 		/*
   1139  1.33        ad 		 * XXX Don't know how to get this from EATA boards.  I think
   1140  1.33        ad 		 * it involves waiting for a "DPT" sequence from HA_ERROR
   1141  1.33        ad 		 * and then reading one of the HA_ICMD registers.
   1142  1.33        ad 		 */
   1143  1.33        ad 		*(int *)data = 0;
   1144  1.33        ad 		break;
   1145  1.33        ad 
   1146  1.33        ad 	case DPT_EATAUSRCMD:
   1147  1.55      elad 		rv = kauth_authorize_device_passthru(l->l_cred, dev,
   1148  1.55      elad 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_ALL, data);
   1149  1.53      elad 		if (rv)
   1150  1.53      elad 			return (rv);
   1151  1.49  christos 
   1152  1.37        ad 		if (IOCPARM_LEN(cmd) < sizeof(struct eata_ucp)) {
   1153  1.38    kleink 			DPRINTF(("%s: ucp %lu vs %lu bytes\n",
   1154  1.59    cegger 			    device_xname(&sc->sc_dv), IOCPARM_LEN(cmd),
   1155  1.38    kleink 			    (unsigned long int)sizeof(struct eata_ucp)));
   1156  1.37        ad 			return (EINVAL);
   1157  1.37        ad 		}
   1158  1.37        ad 
   1159  1.33        ad 		if (sc->sc_uactive++)
   1160  1.33        ad 			tsleep(&sc->sc_uactive, PRIBIO, "dptslp", 0);
   1161  1.33        ad 
   1162  1.45  christos 		rv = dpt_passthrough(sc, (struct eata_ucp *)data, l);
   1163  1.33        ad 
   1164  1.33        ad 		sc->sc_uactive--;
   1165  1.33        ad 		wakeup_one(&sc->sc_uactive);
   1166  1.33        ad 		return (rv);
   1167  1.33        ad 
   1168  1.33        ad 	default:
   1169  1.59    cegger 		DPRINTF(("%s: unknown ioctl %lx\n", device_xname(&sc->sc_dv), cmd));
   1170  1.33        ad 		return (ENOTTY);
   1171  1.33        ad 	}
   1172  1.33        ad 
   1173  1.33        ad 	return (0);
   1174  1.33        ad }
   1175  1.33        ad 
   1176  1.33        ad void
   1177  1.33        ad dpt_ctlrinfo(struct dpt_softc *sc, struct dpt_eata_ctlrinfo *info)
   1178  1.33        ad {
   1179  1.33        ad 
   1180  1.33        ad 	memset(info, 0, sizeof(*info));
   1181  1.33        ad 	info->id = sc->sc_hbaid[0];
   1182  1.33        ad 	info->vect = sc->sc_isairq;
   1183  1.33        ad 	info->base = sc->sc_isaport;
   1184  1.33        ad 	info->qdepth = sc->sc_nccbs;
   1185  1.33        ad 	info->sgsize = DPT_SG_SIZE * sizeof(struct eata_sg);
   1186  1.33        ad 	info->heads = 16;
   1187  1.33        ad 	info->sectors = 63;
   1188  1.33        ad 	info->do_drive32 = 1;
   1189  1.33        ad 	info->primary = 1;
   1190  1.33        ad 	info->cpLength = sizeof(struct eata_cp);
   1191  1.33        ad 	info->spLength = sizeof(struct eata_sp);
   1192  1.33        ad 	info->drqNum = sc->sc_isadrq;
   1193  1.33        ad }
   1194  1.33        ad 
   1195  1.33        ad void
   1196  1.33        ad dpt_sysinfo(struct dpt_softc *sc, struct dpt_sysinfo *info)
   1197  1.33        ad {
   1198  1.33        ad #ifdef i386
   1199  1.33        ad 	int i, j;
   1200  1.33        ad #endif
   1201  1.33        ad 
   1202  1.33        ad 	memset(info, 0, sizeof(*info));
   1203  1.33        ad 
   1204  1.33        ad #ifdef i386
   1205  1.33        ad 	outb (0x70, 0x12);
   1206  1.33        ad 	i = inb(0x71);
   1207  1.33        ad 	j = i >> 4;
   1208  1.33        ad 	if (i == 0x0f) {
   1209  1.33        ad 		outb (0x70, 0x19);
   1210  1.33        ad 		j = inb (0x71);
   1211  1.33        ad 	}
   1212  1.33        ad 	info->drive0CMOS = j;
   1213  1.33        ad 
   1214  1.33        ad 	j = i & 0x0f;
   1215  1.33        ad 	if (i == 0x0f) {
   1216  1.33        ad 		outb (0x70, 0x1a);
   1217  1.33        ad 		j = inb (0x71);
   1218  1.33        ad 	}
   1219  1.33        ad 	info->drive1CMOS = j;
   1220  1.33        ad 	info->processorFamily = dpt_sig.dsProcessorFamily;
   1221  1.33        ad 
   1222  1.33        ad 	/*
   1223  1.33        ad 	 * Get the conventional memory size from CMOS.
   1224  1.33        ad 	 */
   1225  1.33        ad 	outb(0x70, 0x16);
   1226  1.33        ad 	j = inb(0x71);
   1227  1.33        ad 	j <<= 8;
   1228  1.33        ad 	outb(0x70, 0x15);
   1229  1.33        ad 	j |= inb(0x71);
   1230  1.33        ad 	info->conventionalMemSize = j;
   1231  1.33        ad 
   1232  1.33        ad 	/*
   1233  1.33        ad 	 * Get the extended memory size from CMOS.
   1234  1.33        ad 	 */
   1235  1.33        ad 	outb(0x70, 0x31);
   1236  1.33        ad 	j = inb(0x71);
   1237  1.33        ad 	j <<= 8;
   1238  1.33        ad 	outb(0x70, 0x30);
   1239  1.33        ad 	j |= inb(0x71);
   1240  1.33        ad 	info->extendedMemSize = j;
   1241  1.33        ad 
   1242  1.33        ad 	switch (cpu_class) {
   1243  1.33        ad 	case CPUCLASS_386:
   1244  1.33        ad 		info->processorType = PROC_386;
   1245  1.33        ad 		break;
   1246  1.33        ad 	case CPUCLASS_486:
   1247  1.33        ad 		info->processorType = PROC_486;
   1248  1.33        ad 		break;
   1249  1.33        ad 	case CPUCLASS_586:
   1250  1.33        ad 		info->processorType = PROC_PENTIUM;
   1251  1.33        ad 		break;
   1252  1.33        ad 	case CPUCLASS_686:
   1253  1.33        ad 	default:
   1254  1.33        ad 		info->processorType = PROC_SEXIUM;
   1255  1.33        ad 		break;
   1256  1.33        ad 	}
   1257  1.33        ad 
   1258  1.33        ad 	info->flags = SI_CMOS_Valid | SI_BusTypeValid |
   1259  1.33        ad 	    SI_MemorySizeValid | SI_NO_SmartROM;
   1260  1.33        ad #else
   1261  1.33        ad 	info->flags = SI_BusTypeValid | SI_NO_SmartROM;
   1262  1.33        ad #endif
   1263  1.33        ad 
   1264  1.33        ad 	info->busType = sc->sc_bustype;
   1265  1.33        ad }
   1266  1.33        ad 
   1267  1.33        ad int
   1268  1.45  christos dpt_passthrough(struct dpt_softc *sc, struct eata_ucp *ucp, struct lwp *l)
   1269  1.33        ad {
   1270  1.33        ad 	struct dpt_ccb *ccb;
   1271  1.33        ad 	struct eata_sp sp;
   1272  1.33        ad 	struct eata_cp *cp;
   1273  1.33        ad 	struct eata_sg *sg;
   1274  1.42  christos 	bus_dmamap_t xfer = 0; /* XXX: gcc */
   1275  1.33        ad 	bus_dma_segment_t *ds;
   1276  1.42  christos 	int datain = 0, s, rv = 0, i, uslen; /* XXX: gcc */
   1277  1.33        ad 
   1278  1.33        ad 	/*
   1279  1.33        ad 	 * Get a CCB and fill.
   1280  1.33        ad 	 */
   1281  1.33        ad 	ccb = dpt_ccb_alloc(sc);
   1282  1.33        ad 	ccb->ccb_flg |= CCB_PRIVATE | CCB_WAIT;
   1283  1.33        ad 	ccb->ccb_timeout = 0;
   1284  1.33        ad 	ccb->ccb_savesp = &sp;
   1285  1.33        ad 
   1286  1.33        ad 	cp = &ccb->ccb_eata_cp;
   1287  1.34        ad 	memcpy(cp, ucp->ucp_cp, sizeof(ucp->ucp_cp));
   1288  1.34        ad 	uslen = cp->cp_senselen;
   1289  1.33        ad 	cp->cp_ccbid = ccb->ccb_id;
   1290  1.33        ad 	cp->cp_senselen = sizeof(ccb->ccb_sense);
   1291  1.33        ad 	cp->cp_senseaddr = htobe32(sc->sc_dmamap->dm_segs[0].ds_addr +
   1292  1.33        ad 	    CCB_OFF(sc, ccb) + offsetof(struct dpt_ccb, ccb_sense));
   1293  1.33        ad 	cp->cp_stataddr = htobe32(sc->sc_stppa);
   1294  1.33        ad 
   1295  1.33        ad 	/*
   1296  1.33        ad 	 * Map data transfers.
   1297  1.33        ad 	 */
   1298  1.34        ad 	if (ucp->ucp_dataaddr && ucp->ucp_datalen) {
   1299  1.33        ad 		xfer = ccb->ccb_dmamap_xfer;
   1300  1.34        ad 		datain = ((cp->cp_ctl0 & CP_C0_DATA_IN) != 0);
   1301  1.33        ad 
   1302  1.34        ad 		if (ucp->ucp_datalen > DPT_MAX_XFER) {
   1303  1.59    cegger 			DPRINTF(("%s: xfer too big\n", device_xname(&sc->sc_dv)));
   1304  1.33        ad 			dpt_ccb_free(sc, ccb);
   1305  1.33        ad 			return (EFBIG);
   1306  1.33        ad 		}
   1307  1.33        ad 		rv = bus_dmamap_load(sc->sc_dmat, xfer,
   1308  1.45  christos 		    ucp->ucp_dataaddr, ucp->ucp_datalen, l->l_proc,
   1309  1.33        ad 		    BUS_DMA_WAITOK | BUS_DMA_STREAMING |
   1310  1.33        ad 		    (datain ? BUS_DMA_READ : BUS_DMA_WRITE));
   1311  1.33        ad 		if (rv != 0) {
   1312  1.59    cegger 			DPRINTF(("%s: map failed; %d\n", device_xname(&sc->sc_dv),
   1313  1.33        ad 			    rv));
   1314  1.33        ad 			dpt_ccb_free(sc, ccb);
   1315  1.33        ad 			return (rv);
   1316  1.33        ad 		}
   1317  1.33        ad 
   1318  1.33        ad 		bus_dmamap_sync(sc->sc_dmat, xfer, 0, xfer->dm_mapsize,
   1319  1.33        ad 		    (datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
   1320  1.33        ad 
   1321  1.33        ad 		sg = ccb->ccb_sg;
   1322  1.33        ad 		ds = xfer->dm_segs;
   1323  1.33        ad 		for (i = 0; i < xfer->dm_nsegs; i++, sg++, ds++) {
   1324  1.33        ad 	 		sg->sg_addr = htobe32(ds->ds_addr);
   1325  1.34        ad 	 		sg->sg_len = htobe32(ds->ds_len);
   1326  1.33        ad  		}
   1327  1.44     perry 		cp->cp_dataaddr = htobe32(CCB_OFF(sc, ccb) +
   1328  1.33        ad 		    sc->sc_dmamap->dm_segs[0].ds_addr +
   1329  1.33        ad 		    offsetof(struct dpt_ccb, ccb_sg));
   1330  1.33        ad 		cp->cp_datalen = htobe32(i * sizeof(struct eata_sg));
   1331  1.33        ad 		cp->cp_ctl0 |= CP_C0_SCATTER;
   1332  1.33        ad 	} else {
   1333  1.33        ad 		cp->cp_dataaddr = 0;
   1334  1.33        ad 		cp->cp_datalen = 0;
   1335  1.33        ad 	}
   1336  1.33        ad 
   1337  1.33        ad 	/*
   1338  1.33        ad 	 * Start the command and sleep on completion.
   1339  1.33        ad 	 */
   1340  1.44     perry 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, CCB_OFF(sc, ccb),
   1341  1.33        ad 	    sizeof(struct dpt_ccb), BUS_DMASYNC_PREWRITE);
   1342  1.33        ad 	s = splbio();
   1343  1.44     perry 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_stpoff,
   1344  1.33        ad 	    sizeof(struct eata_sp), BUS_DMASYNC_PREREAD);
   1345  1.33        ad 	if (dpt_cmd(sc, ccb, CP_DMA_CMD, 0))
   1346  1.59    cegger 		panic("%s: dpt_cmd failed", device_xname(&sc->sc_dv));
   1347  1.33        ad 	tsleep(ccb, PWAIT, "dptucmd", 0);
   1348  1.33        ad 	splx(s);
   1349  1.33        ad 
   1350  1.33        ad 	/*
   1351  1.33        ad 	 * Sync up the DMA map and copy out results.
   1352  1.33        ad 	 */
   1353  1.44     perry 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, CCB_OFF(sc, ccb),
   1354  1.33        ad 	    sizeof(struct dpt_ccb), BUS_DMASYNC_POSTWRITE);
   1355  1.33        ad 
   1356  1.33        ad 	if (cp->cp_datalen != 0) {
   1357  1.33        ad 		bus_dmamap_sync(sc->sc_dmat, xfer, 0, xfer->dm_mapsize,
   1358  1.33        ad 		    (datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
   1359  1.33        ad 		bus_dmamap_unload(sc->sc_dmat, xfer);
   1360  1.33        ad 	}
   1361  1.33        ad 
   1362  1.34        ad 	if (ucp->ucp_stataddr != NULL) {
   1363  1.34        ad 		rv = copyout(&sp, ucp->ucp_stataddr, sizeof(sp));
   1364  1.51  christos 		if (rv != 0) {
   1365  1.33        ad 			DPRINTF(("%s: sp copyout() failed\n",
   1366  1.59    cegger 			    device_xname(&sc->sc_dv)));
   1367  1.51  christos 		}
   1368  1.33        ad 	}
   1369  1.34        ad 	if (rv == 0 && ucp->ucp_senseaddr != NULL) {
   1370  1.34        ad 		i = min(uslen, sizeof(ccb->ccb_sense));
   1371  1.34        ad 		rv = copyout(&ccb->ccb_sense, ucp->ucp_senseaddr, i);
   1372  1.51  christos 		if (rv != 0) {
   1373  1.33        ad 			DPRINTF(("%s: sense copyout() failed\n",
   1374  1.59    cegger 			    device_xname(&sc->sc_dv)));
   1375  1.51  christos 		}
   1376  1.33        ad 	}
   1377  1.33        ad 
   1378  1.37        ad 	ucp->ucp_hstatus = (u_int8_t)ccb->ccb_hba_status;
   1379  1.37        ad 	ucp->ucp_tstatus = (u_int8_t)ccb->ccb_scsi_status;
   1380  1.33        ad 	dpt_ccb_free(sc, ccb);
   1381  1.33        ad 	return (rv);
   1382   1.1        ad }
   1383