Home | History | Annotate | Line # | Download | only in ic
dpt.c revision 1.22
      1  1.22       ad /*	$NetBSD: dpt.c,v 1.22 2000/06/13 13:36:42 ad Exp $	*/
      2   1.1       ad 
      3   1.1       ad /*-
      4  1.16       ad  * Copyright (c) 1997, 1998, 1999, 2000 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  * 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 
     57   1.1       ad #include <sys/cdefs.h>
     58  1.22       ad __KERNEL_RCSID(0, "$NetBSD: dpt.c,v 1.22 2000/06/13 13:36:42 ad Exp $");
     59   1.1       ad 
     60   1.1       ad #include <sys/param.h>
     61   1.1       ad #include <sys/systm.h>
     62   1.1       ad #include <sys/kernel.h>
     63   1.1       ad #include <sys/device.h>
     64   1.1       ad #include <sys/queue.h>
     65   1.1       ad #include <sys/proc.h>
     66   1.1       ad #include <sys/buf.h>
     67  1.13       ad #include <sys/endian.h>
     68   1.1       ad 
     69  1.11       ad #include <machine/bswap.h>
     70   1.1       ad #include <machine/bus.h>
     71   1.1       ad 
     72   1.1       ad #include <dev/scsipi/scsi_all.h>
     73   1.1       ad #include <dev/scsipi/scsipi_all.h>
     74   1.1       ad #include <dev/scsipi/scsiconf.h>
     75   1.1       ad 
     76   1.1       ad #include <dev/ic/dptreg.h>
     77   1.1       ad #include <dev/ic/dptvar.h>
     78   1.1       ad 
     79   1.1       ad /* A default for our link struct */
     80   1.1       ad static struct scsipi_device dpt_dev = {
     81   1.1       ad 	NULL,			/* Use default error handler */
     82   1.1       ad 	NULL,			/* have a queue, served by this */
     83   1.1       ad 	NULL,			/* have no async handler */
     84   1.1       ad 	NULL,			/* Use default 'done' routine */
     85   1.1       ad };
     86   1.1       ad 
     87   1.1       ad static char *dpt_cname[] = {
     88   1.1       ad 	"PM3334", "SmartRAID IV",
     89   1.1       ad 	"PM3332", "SmartRAID IV",
     90   1.1       ad 	"PM2144", "SmartCache IV",
     91   1.1       ad 	"PM2044", "SmartCache IV",
     92   1.1       ad 	"PM2142", "SmartCache IV",
     93   1.1       ad 	"PM2042", "SmartCache IV",
     94   1.1       ad 	"PM2041", "SmartCache IV",
     95   1.1       ad 	"PM3224", "SmartRAID III",
     96   1.1       ad 	"PM3222", "SmartRAID III",
     97   1.1       ad 	"PM3021", "SmartRAID III",
     98   1.1       ad 	"PM2124", "SmartCache III",
     99   1.1       ad 	"PM2024", "SmartCache III",
    100   1.1       ad 	"PM2122", "SmartCache III",
    101   1.1       ad 	"PM2022", "SmartCache III",
    102   1.1       ad 	"PM2021", "SmartCache III",
    103   1.1       ad 	"SK2012", "SmartCache Plus",
    104   1.1       ad 	"SK2011", "SmartCache Plus",
    105   1.1       ad 	NULL,     "unknown adapter, please report using send-pr(1)",
    106   1.1       ad };
    107   1.1       ad 
    108  1.19       ad TAILQ_HEAD(, dpt_softc) dpt_hba;	/* tailq of HBA softc's */
    109  1.19       ad void *dpt_sdh;				/* shutdown hook */
    110  1.19       ad 
    111   1.1       ad /*
    112   1.1       ad  * Handle an interrupt from the HBA.
    113   1.1       ad  */
    114   1.1       ad int
    115   1.1       ad dpt_intr(xxx_sc)
    116   1.1       ad 	void *xxx_sc;
    117   1.1       ad {
    118   1.1       ad 	struct dpt_softc *sc;
    119   1.1       ad 	struct dpt_ccb *ccb;
    120   1.1       ad 	struct eata_sp *sp;
    121  1.19       ad 	volatile int junk;
    122   1.1       ad 
    123   1.1       ad 	sc = xxx_sc;
    124  1.19       ad 	sp = sc->sc_stp;
    125   1.1       ad 
    126   1.1       ad #ifdef DEBUG
    127  1.19       ad 	if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) == 0) {
    128   1.1       ad 		printf("%s: spurious intr\n", sc->sc_dv.dv_xname);
    129  1.19       ad 		return (1);
    130  1.19       ad 	}
    131   1.1       ad #endif
    132  1.19       ad 
    133   1.7       ad 	for (;;) {
    134   1.7       ad 		/*
    135   1.7       ad 		 * HBA might have interrupted while we were dealing with the
    136   1.7       ad 		 * last completed command, since we ACK before we deal; keep
    137  1.19       ad 		 * polling.
    138   1.7       ad 		 */
    139  1.19       ad 		if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) == 0)
    140   1.7       ad 			break;
    141   1.7       ad 
    142  1.19       ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_stpoff,
    143   1.1       ad 		    sizeof(struct eata_sp), BUS_DMASYNC_POSTREAD);
    144   1.1       ad 
    145   1.1       ad 		/* Might have looped before HBA can reset HBA_AUX_INTR */
    146   1.1       ad 		if (sp->sp_ccbid == -1) {
    147   1.1       ad 			DELAY(50);
    148  1.19       ad 
    149   1.1       ad 			if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) == 0)
    150   1.1       ad 				return (0);
    151  1.19       ad 
    152  1.19       ad 			printf("%s: no status\n", sc->sc_dv.dv_xname);
    153  1.19       ad 
    154   1.1       ad 			/* Re-sync DMA map */
    155  1.19       ad 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
    156  1.19       ad 			    sc->sc_stpoff, sizeof(struct eata_sp),
    157   1.1       ad 			    BUS_DMASYNC_POSTREAD);
    158   1.1       ad 		}
    159   1.1       ad 
    160   1.1       ad 		/* Make sure CCB ID from status packet is realistic */
    161   1.1       ad 		if (sp->sp_ccbid >= 0 && sp->sp_ccbid < sc->sc_nccbs) {
    162   1.1       ad 			/* Sync up DMA map and cache cmd status */
    163   1.1       ad 			ccb = sc->sc_ccbs + sp->sp_ccbid;
    164   1.1       ad 
    165  1.19       ad 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
    166   1.4       ad 			    CCB_OFF(sc, ccb), sizeof(struct dpt_ccb),
    167   1.1       ad 			    BUS_DMASYNC_POSTWRITE);
    168   1.1       ad 
    169   1.9       ad 			ccb->ccb_hba_status = sp->sp_hba_status & 0x7F;
    170   1.1       ad 			ccb->ccb_scsi_status = sp->sp_scsi_status;
    171   1.1       ad 
    172   1.1       ad 			/*
    173   1.1       ad 			 * Ack the interrupt and process the CCB. If this
    174   1.1       ad 			 * is a private CCB it's up to dpt_poll() to notice.
    175   1.1       ad 			 */
    176   1.1       ad 			sp->sp_ccbid = -1;
    177   1.1       ad 			ccb->ccb_flg |= CCB_INTR;
    178  1.20       ad 			junk = dpt_inb(sc, HA_STATUS);
    179   1.1       ad 			if ((ccb->ccb_flg & CCB_PRIVATE) == 0)
    180   1.1       ad 				dpt_done_ccb(sc, ccb);
    181   1.1       ad 		} else {
    182   1.1       ad 			printf("%s: bogus status (returned CCB id %d)\n",
    183   1.1       ad 			    sc->sc_dv.dv_xname, sp->sp_ccbid);
    184   1.1       ad 
    185   1.1       ad 			/* Ack the interrupt */
    186  1.20       ad 			sp->sp_ccbid = -1;
    187  1.19       ad 			junk = dpt_inb(sc, HA_STATUS);
    188   1.1       ad 		}
    189   1.1       ad 	}
    190   1.1       ad 
    191  1.19       ad 	return (1);
    192   1.1       ad }
    193   1.1       ad 
    194   1.1       ad /*
    195   1.1       ad  * Initialize and attach the HBA. This is the entry point from bus
    196   1.1       ad  * specific probe-and-attach code.
    197   1.1       ad  */
    198   1.1       ad void
    199   1.1       ad dpt_init(sc, intrstr)
    200   1.1       ad 	struct dpt_softc *sc;
    201   1.1       ad 	const char *intrstr;
    202   1.1       ad {
    203   1.1       ad 	struct eata_inquiry_data *ei;
    204  1.19       ad 	int i, j, error, rseg, maxchannel, maxtarget;
    205   1.1       ad 	bus_dma_segment_t seg;
    206   1.4       ad 	struct eata_cfg *ec;
    207  1.19       ad 	struct scsipi_link *link;
    208   1.1       ad 	char model[16];
    209   1.1       ad 
    210   1.4       ad 	ec = &sc->sc_ec;
    211   1.1       ad 
    212   1.1       ad 	/* Allocate the CCB/status packet/scratch DMA map and load */
    213  1.19       ad 	sc->sc_nccbs =
    214  1.19       ad 	    min(be16toh(*(int16_t *)ec->ec_queuedepth), DPT_MAX_CCBS);
    215  1.19       ad 	sc->sc_stpoff = sc->sc_nccbs * sizeof(struct dpt_ccb);
    216  1.19       ad 	sc->sc_scroff = sc->sc_stpoff + sizeof(struct eata_sp);
    217  1.19       ad 	sc->sc_scrlen = DPT_SCRATCH_SIZE;
    218  1.19       ad 	sc->sc_dmamapsize = sc->sc_nccbs * sizeof(struct dpt_ccb) +
    219  1.19       ad 	    sc->sc_scrlen + sizeof(struct eata_sp);
    220   1.1       ad 
    221  1.19       ad 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmamapsize, NBPG, 0,
    222   1.1       ad 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    223   1.1       ad 		printf("%s: unable to allocate CCBs, error = %d\n",
    224   1.1       ad 		    sc->sc_dv.dv_xname, error);
    225   1.1       ad 		return;
    226   1.1       ad 	}
    227   1.1       ad 
    228  1.19       ad 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, sc->sc_dmamapsize,
    229   1.1       ad 	    (caddr_t *)&sc->sc_ccbs, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    230   1.1       ad 		printf("%s: unable to map CCBs, error = %d\n",
    231   1.1       ad 		    sc->sc_dv.dv_xname, error);
    232   1.1       ad 		return;
    233   1.1       ad 	}
    234   1.1       ad 
    235  1.19       ad 	if ((error = bus_dmamap_create(sc->sc_dmat, sc->sc_dmamapsize,
    236  1.19       ad 	    sc->sc_dmamapsize, 1, 0, BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    237   1.1       ad 		printf("%s: unable to create CCB DMA map, error = %d\n",
    238   1.1       ad 		    sc->sc_dv.dv_xname, error);
    239   1.1       ad 		return;
    240   1.1       ad 	}
    241   1.1       ad 
    242  1.19       ad 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
    243  1.19       ad 	    sc->sc_ccbs, sc->sc_dmamapsize, NULL, BUS_DMA_NOWAIT)) != 0) {
    244   1.1       ad 		printf("%s: unable to load CCB DMA map, error = %d\n",
    245   1.1       ad 		    sc->sc_dv.dv_xname, error);
    246   1.1       ad 		return;
    247   1.1       ad 	}
    248   1.1       ad 
    249  1.19       ad 	sc->sc_stp = (struct eata_sp *)((caddr_t)sc->sc_ccbs + sc->sc_stpoff);
    250  1.19       ad 	sc->sc_stppa = sc->sc_dmamap->dm_segs[0].ds_addr + sc->sc_stpoff;
    251   1.1       ad 	sc->sc_scr = (caddr_t)sc->sc_ccbs + sc->sc_scroff;
    252  1.19       ad 	sc->sc_scrpa = sc->sc_dmamap->dm_segs[0].ds_addr + sc->sc_scroff;
    253  1.19       ad 	sc->sc_stp->sp_ccbid = -1;
    254   1.4       ad 
    255   1.1       ad 	/* Initialize the CCBs */
    256   1.1       ad 	TAILQ_INIT(&sc->sc_free_ccb);
    257   1.1       ad 	i = dpt_create_ccbs(sc, sc->sc_ccbs, sc->sc_nccbs);
    258   1.1       ad 
    259   1.1       ad 	if (i == 0) {
    260   1.1       ad 		printf("%s: unable to create CCBs\n", sc->sc_dv.dv_xname);
    261   1.1       ad 		return;
    262   1.1       ad 	} else if (i != sc->sc_nccbs) {
    263   1.1       ad 		printf("%s: %d/%d CCBs created!\n", sc->sc_dv.dv_xname, i,
    264   1.1       ad 		    sc->sc_nccbs);
    265   1.1       ad 		sc->sc_nccbs = i;
    266   1.1       ad 	}
    267   1.1       ad 
    268   1.1       ad 	/* Set shutdownhook before we start any device activity */
    269  1.19       ad 	if (dpt_sdh == NULL) {
    270  1.19       ad 		TAILQ_INIT(&dpt_hba);
    271  1.19       ad 		dpt_sdh = shutdownhook_establish(dpt_shutdown, NULL);
    272  1.19       ad 	}
    273   1.1       ad 
    274   1.1       ad 	/* Get the page 0 inquiry data from the HBA */
    275   1.1       ad 	dpt_hba_inquire(sc, &ei);
    276   1.1       ad 
    277   1.1       ad 	/*
    278   1.1       ad 	 * dpt0 at pci0 dev 12 function 0: DPT SmartRAID III (PM3224A/9X-R)
    279   1.1       ad 	 * dpt0: interrupting at irq 10
    280   1.1       ad 	 * dpt0: 64 queued commands, 1 channel(s), adapter on ID(s) 7
    281   1.1       ad 	 */
    282   1.1       ad 	for (i = 0; ei->ei_vendor[i] != ' ' && i < 8; i++)
    283   1.1       ad 		;
    284   1.1       ad 	ei->ei_vendor[i] = '\0';
    285   1.1       ad 
    286   1.1       ad 	for (i = 0; ei->ei_model[i] != ' ' && i < 7; i++)
    287   1.1       ad 		model[i] = ei->ei_model[i];
    288   1.1       ad 	for (j = 0; ei->ei_suffix[j] != ' ' && j < 7; j++)
    289   1.1       ad 		model[i++] = ei->ei_model[i];
    290   1.1       ad 	model[i] = '\0';
    291   1.1       ad 
    292   1.1       ad 	/* Find the cannonical name for the board */
    293   1.9       ad 	for (i = 0; dpt_cname[i] != NULL; i += 2)
    294   1.1       ad 		if (memcmp(ei->ei_model, dpt_cname[i], 6) == 0)
    295   1.1       ad 			break;
    296   1.1       ad 
    297   1.1       ad 	printf("%s %s (%s)\n", ei->ei_vendor, dpt_cname[i + 1], model);
    298   1.1       ad 
    299   1.1       ad 	if (intrstr != NULL)
    300   1.1       ad 		printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname, intrstr);
    301   1.1       ad 
    302  1.19       ad 	maxchannel = (ec->ec_feat3 & EC_F3_MAX_CHANNEL_MASK) >>
    303  1.19       ad 	    EC_F3_MAX_CHANNEL_SHIFT;
    304  1.19       ad 	maxtarget = (ec->ec_feat3 & EC_F3_MAX_TARGET_MASK) >>
    305  1.19       ad 	    EC_F3_MAX_TARGET_SHIFT;
    306  1.19       ad 
    307   1.1       ad 	printf("%s: %d queued commands, %d channel(s), adapter on ID(s)",
    308  1.19       ad 	    sc->sc_dv.dv_xname, sc->sc_nccbs, maxchannel + 1);
    309   1.1       ad 
    310  1.19       ad 	for (i = 0; i <= maxchannel; i++) {
    311  1.19       ad 		sc->sc_hbaid[i] = ec->ec_hba[3 - i];
    312  1.19       ad 		printf(" %d", sc->sc_hbaid[i]);
    313  1.19       ad 	}
    314   1.1       ad 	printf("\n");
    315   1.1       ad 
    316   1.1       ad 	/* Reset the SCSI bus */
    317   1.4       ad 	if (dpt_cmd(sc, NULL, 0, CP_IMMEDIATE, CPI_BUS_RESET))
    318   1.4       ad 		panic("%s: dpt_cmd failed", sc->sc_dv.dv_xname);
    319   1.1       ad 
    320   1.1       ad 	/* Fill in the adapter, each link and attach in turn */
    321   1.1       ad 	sc->sc_adapter.scsipi_cmd = dpt_scsi_cmd;
    322   1.1       ad 	sc->sc_adapter.scsipi_minphys = dpt_minphys;
    323   1.1       ad 
    324  1.19       ad 	for (i = 0; i <= maxchannel; i++) {
    325   1.1       ad 		link = &sc->sc_link[i];
    326   1.1       ad 		link->scsipi_scsi.channel = i;
    327   1.1       ad 		link->scsipi_scsi.adapter_target = sc->sc_hbaid[i];
    328   1.4       ad 		link->scsipi_scsi.max_lun = ec->ec_maxlun;
    329  1.19       ad 		link->scsipi_scsi.max_target = maxtarget;
    330   1.1       ad 		link->type = BUS_SCSI;
    331   1.1       ad 		link->device = &dpt_dev;
    332   1.1       ad 		link->adapter = &sc->sc_adapter;
    333   1.1       ad 		link->adapter_softc = sc;
    334  1.19       ad 		link->openings = sc->sc_nccbs;	/* XXX */
    335   1.1       ad 		config_found(&sc->sc_dv, link, scsiprint);
    336   1.1       ad 	}
    337  1.19       ad 
    338  1.19       ad 	TAILQ_INSERT_TAIL(&dpt_hba, sc, sc_chain);
    339   1.1       ad }
    340   1.1       ad 
    341   1.1       ad /*
    342   1.1       ad  * Our 'shutdownhook' to cleanly shut down the HBA. The HBA must flush
    343   1.1       ad  * all data from it's cache and mark array groups as clean.
    344   1.1       ad  */
    345   1.1       ad void
    346   1.1       ad dpt_shutdown(xxx_sc)
    347   1.1       ad 	void *xxx_sc;
    348   1.1       ad {
    349   1.1       ad 	struct dpt_softc *sc;
    350   1.1       ad 
    351  1.21       ad 	printf("shutting down dpt devices...");
    352  1.19       ad 
    353  1.19       ad 	for (sc = TAILQ_FIRST(&dpt_hba); sc != NULL;
    354  1.19       ad 	    sc = TAILQ_NEXT(sc, sc_chain))
    355  1.19       ad 		dpt_cmd(sc, NULL, 0, CP_IMMEDIATE, CPI_POWEROFF_WARN);
    356  1.19       ad 
    357   1.1       ad 	DELAY(5000*1000);
    358   1.1       ad 	printf(" done\n");
    359   1.1       ad }
    360   1.1       ad 
    361   1.1       ad /*
    362   1.1       ad  * Send an EATA command to the HBA.
    363   1.1       ad  */
    364   1.4       ad int
    365   1.1       ad dpt_cmd(sc, cp, addr, eatacmd, icmd)
    366   1.1       ad 	struct dpt_softc *sc;
    367   1.1       ad 	struct eata_cp *cp;
    368   1.1       ad 	u_int32_t addr;
    369   1.1       ad 	int eatacmd, icmd;
    370   1.1       ad {
    371   1.1       ad 	int i;
    372   1.1       ad 
    373   1.1       ad 	for (i = 20000; i; i--) {
    374   1.1       ad 		if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_BUSY) == 0)
    375   1.1       ad 			break;
    376   1.1       ad 		DELAY(50);
    377   1.1       ad 	}
    378   1.1       ad 
    379   1.4       ad 	/* Not the most graceful way to handle this */
    380   1.1       ad 	if (i == 0) {
    381   1.4       ad 		printf("%s: HBA timeout on EATA command issue; aborting\n",
    382   1.4       ad 		    sc->sc_dv.dv_xname);
    383   1.4       ad 		return (-1);
    384   1.1       ad 	}
    385   1.1       ad 
    386   1.1       ad 	if (cp == NULL)
    387   1.1       ad 		addr = 0;
    388   1.1       ad 
    389  1.13       ad 	dpt_outl(sc, HA_DMA_BASE, (u_int32_t)addr);
    390   1.1       ad 
    391   1.1       ad 	if (eatacmd == CP_IMMEDIATE) {
    392   1.1       ad 		if (cp == NULL) {
    393   1.1       ad 			/* XXX should really pass meaningful values */
    394   1.1       ad 			dpt_outb(sc, HA_ICMD_CODE2, 0);
    395   1.1       ad 			dpt_outb(sc, HA_ICMD_CODE1, 0);
    396   1.1       ad 		}
    397   1.1       ad 		dpt_outb(sc, HA_ICMD, icmd);
    398   1.1       ad 	}
    399   1.1       ad 
    400  1.19       ad 	dpt_outb(sc, HA_COMMAND, eatacmd);
    401  1.19       ad 	return (0);
    402   1.1       ad }
    403   1.1       ad 
    404   1.1       ad /*
    405  1.14       ad  * Wait for the HBA status register to reach a specific state.
    406   1.1       ad  */
    407   1.1       ad int
    408   1.1       ad dpt_wait(sc, mask, state, ms)
    409  1.19       ad 	struct dpt_softc *sc;
    410  1.19       ad 	u_int8_t mask, state;
    411  1.19       ad 	int ms;
    412   1.1       ad {
    413   1.1       ad 
    414  1.19       ad 	for (ms *= 10; ms; ms--) {
    415  1.19       ad 		if ((dpt_inb(sc, HA_STATUS) & mask) == state)
    416  1.19       ad 			return (0);
    417  1.19       ad 		DELAY(100);
    418  1.19       ad 	}
    419  1.19       ad 	return (-1);
    420   1.1       ad }
    421   1.1       ad 
    422   1.1       ad /*
    423   1.1       ad  * Wait for the specified CCB to finish. This is used when we may not be
    424   1.1       ad  * able to sleep and/or interrupts are disabled (eg autoconfiguration).
    425   1.1       ad  * The timeout value from the CCB is used. This should only be used for
    426   1.1       ad  * CCB_PRIVATE requests; otherwise the CCB will get recycled before we get
    427   1.1       ad  * a look at it.
    428   1.1       ad  */
    429   1.1       ad int
    430   1.1       ad dpt_poll(sc, ccb)
    431  1.19       ad 	struct dpt_softc *sc;
    432  1.19       ad 	struct dpt_ccb *ccb;
    433   1.1       ad {
    434   1.1       ad 	int i;
    435   1.5       ad 
    436   1.5       ad #ifdef DEBUG
    437   1.5       ad 	if ((ccb->ccb_flg & CCB_PRIVATE) == 0)
    438   1.5       ad 		panic("dpt_poll: called for non-CCB_PRIVATE request\n");
    439   1.5       ad #endif
    440   1.1       ad 
    441   1.9       ad  	if ((ccb->ccb_flg & CCB_INTR) != 0)
    442  1.19       ad 		return (0);
    443   1.9       ad 
    444  1.19       ad 	for (i = ccb->ccb_timeout * 20; i; i--) {
    445  1.19       ad 		if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) != 0) {
    446  1.19       ad 			dpt_intr(sc);
    447  1.21       ad 			if ((ccb->ccb_flg & CCB_INTR) != 0)
    448  1.14       ad 				return (0);
    449  1.14       ad 		}
    450  1.14       ad 		DELAY(50);
    451  1.19       ad 	}
    452  1.14       ad 
    453  1.19       ad 	return (-1);
    454   1.1       ad }
    455   1.1       ad 
    456   1.1       ad /*
    457   1.1       ad  * Read the EATA configuration from the HBA and perform some sanity checks.
    458   1.1       ad  */
    459   1.1       ad int
    460   1.4       ad dpt_readcfg(sc)
    461   1.1       ad 	struct dpt_softc *sc;
    462   1.1       ad {
    463   1.4       ad 	struct eata_cfg *ec;
    464   1.1       ad 	int i, j, stat;
    465   1.1       ad 	u_int16_t *p;
    466   1.1       ad 
    467   1.4       ad 	ec = &sc->sc_ec;
    468   1.4       ad 
    469   1.4       ad 	/* Older firmware may puke if we talk to it too soon after reset */
    470   1.4       ad 	dpt_outb(sc, HA_COMMAND, CP_RESET);
    471  1.19       ad 	DELAY(750000);
    472   1.4       ad 
    473   1.4       ad 	for (i = 1000; i; i--) {
    474   1.4       ad 		if ((dpt_inb(sc, HA_STATUS) & HA_ST_READY) != 0)
    475   1.4       ad 			break;
    476   1.4       ad 		DELAY(2000);
    477   1.4       ad 	}
    478   1.4       ad 
    479   1.4       ad 	if (i == 0) {
    480  1.17       ad 		printf("%s: HBA not ready after reset (hba status:%02x)\n",
    481   1.4       ad 		    sc->sc_dv.dv_xname, dpt_inb(sc, HA_STATUS));
    482   1.4       ad 		return (-1);
    483   1.4       ad 	}
    484   1.4       ad 
    485   1.1       ad 	while((((stat = dpt_inb(sc, HA_STATUS))
    486  1.19       ad 	    != (HA_ST_READY|HA_ST_SEEK_COMPLETE))
    487  1.19       ad 	    && (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR))
    488  1.19       ad 	    && (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR|HA_ST_DRQ)))
    489  1.19       ad 	    && dpt_wait(sc, HA_ST_BUSY, 0, 2000)) {
    490  1.19       ad 		/* RAID drives still spinning up? */
    491  1.19       ad 		if((dpt_inb(sc, HA_ERROR) != 'D')
    492  1.19       ad 		    || (dpt_inb(sc, HA_ERROR + 1) != 'P')
    493  1.19       ad 		    || (dpt_inb(sc, HA_ERROR + 2) != 'T')) {
    494  1.19       ad 		    	printf("%s: HBA not ready\n", sc->sc_dv.dv_xname);
    495  1.19       ad 			return (-1);
    496   1.2       ad 		}
    497  1.19       ad 	}
    498   1.1       ad 
    499   1.1       ad 	/*
    500   1.1       ad 	 * Issue the read-config command and wait for the data to appear.
    501   1.1       ad 	 * XXX we shouldn't be doing this with PIO, but it makes it a lot
    502   1.1       ad 	 * easier as no DMA setup is required.
    503   1.1       ad 	 */
    504   1.1       ad 	dpt_outb(sc, HA_COMMAND, CP_PIO_GETCFG);
    505   1.4       ad 	memset(ec, 0, sizeof(*ec));
    506   1.4       ad 	i = ((int)&((struct eata_cfg *)0)->ec_cfglen +
    507   1.4       ad 	    sizeof(ec->ec_cfglen)) >> 1;
    508   1.4       ad 	p = (u_int16_t *)ec;
    509   1.4       ad 
    510   1.4       ad 	if (dpt_wait(sc, 0xFF, HA_ST_DATA_RDY, 2000)) {
    511  1.17       ad 		printf("%s: cfg data didn't appear (hba status:%02x)\n",
    512   1.9       ad 		    sc->sc_dv.dv_xname, dpt_inb(sc, HA_STATUS));
    513   1.4       ad   		return (-1);
    514   1.4       ad   	}
    515   1.1       ad 
    516   1.1       ad 	/* Begin reading */
    517   1.1       ad  	while (i--)
    518   1.1       ad 		*p++ = dpt_inw(sc, HA_DATA);
    519   1.1       ad 
    520  1.19       ad 	if ((i = ec->ec_cfglen) > (sizeof(struct eata_cfg)
    521  1.19       ad 	    - (int)(&(((struct eata_cfg *)0L)->ec_cfglen))
    522  1.19       ad 	    - sizeof(ec->ec_cfglen)))
    523  1.19       ad 		i = sizeof(struct eata_cfg)
    524  1.19       ad 		  - (int)(&(((struct eata_cfg *)0L)->ec_cfglen))
    525  1.19       ad 		  - sizeof(ec->ec_cfglen);
    526  1.19       ad 
    527  1.19       ad 	j = i + (int)(&(((struct eata_cfg *)0L)->ec_cfglen)) +
    528  1.19       ad 	    sizeof(ec->ec_cfglen);
    529  1.19       ad 	i >>= 1;
    530   1.1       ad 
    531   1.1       ad 	while (i--)
    532  1.19       ad 		*p++ = dpt_inw(sc, HA_DATA);
    533  1.19       ad 
    534  1.19       ad 	/* Flush until we have read 512 bytes. */
    535  1.19       ad 	i = (512 - j + 1) >> 1;
    536   1.1       ad 	while (i--)
    537   1.1       ad  		dpt_inw(sc, HA_DATA);
    538  1.19       ad 
    539  1.19       ad 	/* Defaults for older Firmware */
    540   1.4       ad 	if (p <= (u_short *)&ec->ec_hba[DPT_MAX_CHANNELS - 1])
    541   1.4       ad 		ec->ec_hba[DPT_MAX_CHANNELS - 1] = 7;
    542   1.1       ad 
    543  1.19       ad 	if ((dpt_inb(sc, HA_STATUS) & HA_ST_ERROR) != 0) {
    544  1.19       ad 		printf("%s: HBA error\n", sc->sc_dv.dv_xname);
    545  1.19       ad 		return (-1);
    546  1.19       ad 	}
    547  1.19       ad 
    548  1.15       ad 	if (memcmp(ec->ec_eatasig, "EATA", 4) != 0) {
    549  1.15       ad 	        printf("%s: EATA signature mismatch\n", sc->sc_dv.dv_xname);
    550   1.1       ad 		return (-1);
    551   1.1       ad 	}
    552   1.1       ad 
    553  1.19       ad 	if ((ec->ec_feat0 & EC_F0_HBA_VALID) == 0) {
    554  1.19       ad 		printf("%s: ec_hba field invalid\n", sc->sc_dv.dv_xname);
    555   1.1       ad 		return (-1);
    556   1.1       ad 	}
    557   1.1       ad 
    558  1.19       ad 	if ((ec->ec_feat0 & EC_F0_DMA_SUPPORTED) == 0) {
    559   1.1       ad 	        printf("%s: DMA not supported\n", sc->sc_dv.dv_xname);
    560   1.1       ad 		return (-1);
    561   1.1       ad 	}
    562   1.1       ad 
    563   1.1       ad 	return (0);
    564   1.1       ad }
    565   1.1       ad 
    566   1.1       ad /*
    567   1.1       ad  * Adjust the size of each I/O before it passes to the SCSI layer.
    568   1.1       ad  */
    569   1.1       ad void
    570   1.1       ad dpt_minphys(bp)
    571   1.1       ad 	struct buf *bp;
    572   1.1       ad {
    573   1.1       ad 
    574   1.1       ad 	if (bp->b_bcount > DPT_MAX_XFER)
    575   1.1       ad 		bp->b_bcount = DPT_MAX_XFER;
    576   1.1       ad 	minphys(bp);
    577   1.1       ad }
    578   1.1       ad 
    579   1.1       ad /*
    580   1.1       ad  * Put a CCB onto the freelist.
    581   1.1       ad  */
    582   1.1       ad void
    583   1.1       ad dpt_free_ccb(sc, ccb)
    584   1.1       ad 	struct dpt_softc *sc;
    585   1.1       ad 	struct dpt_ccb *ccb;
    586   1.1       ad {
    587   1.1       ad 	int s;
    588   1.1       ad 
    589   1.1       ad 	s = splbio();
    590   1.1       ad 	ccb->ccb_flg = 0;
    591   1.1       ad 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, ccb_chain);
    592   1.1       ad 
    593   1.1       ad 	/* Wake anybody waiting for a free ccb */
    594   1.1       ad 	if (ccb->ccb_chain.tqe_next == 0)
    595   1.1       ad 		wakeup(&sc->sc_free_ccb);
    596   1.1       ad 	splx(s);
    597   1.1       ad }
    598   1.1       ad 
    599   1.1       ad /*
    600   1.1       ad  * Initialize the specified CCB.
    601   1.1       ad  */
    602   1.1       ad int
    603   1.1       ad dpt_init_ccb(sc, ccb)
    604   1.1       ad 	struct dpt_softc *sc;
    605   1.1       ad 	struct dpt_ccb *ccb;
    606   1.1       ad {
    607   1.1       ad 	int error;
    608   1.1       ad 
    609   1.1       ad 	/* Create the DMA map for this CCB's data */
    610   1.1       ad 	error = bus_dmamap_create(sc->sc_dmat, DPT_MAX_XFER, DPT_SG_SIZE,
    611   1.1       ad 	    DPT_MAX_XFER, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    612   1.1       ad 	    &ccb->ccb_dmamap_xfer);
    613   1.1       ad 
    614   1.1       ad 	if (error) {
    615   1.1       ad 		printf("%s: can't create ccb dmamap (%d)\n",
    616  1.21       ad 		    sc->sc_dv.dv_xname, error);
    617   1.1       ad 		return (error);
    618   1.1       ad 	}
    619   1.1       ad 
    620   1.1       ad 	ccb->ccb_flg = 0;
    621  1.19       ad 	ccb->ccb_ccbpa = sc->sc_dmamap->dm_segs[0].ds_addr +
    622   1.4       ad 	    CCB_OFF(sc, ccb);
    623   1.1       ad 	return (0);
    624   1.1       ad }
    625   1.1       ad 
    626   1.1       ad /*
    627   1.1       ad  * Create a set of CCBs and add them to the free list.
    628   1.1       ad  */
    629   1.1       ad int
    630   1.1       ad dpt_create_ccbs(sc, ccbstore, count)
    631   1.1       ad 	struct dpt_softc *sc;
    632   1.1       ad 	struct dpt_ccb *ccbstore;
    633   1.1       ad 	int count;
    634   1.1       ad {
    635   1.1       ad 	struct dpt_ccb *ccb;
    636   1.1       ad 	int i, error;
    637   1.1       ad 
    638   1.1       ad 	memset(ccbstore, 0, sizeof(struct dpt_ccb) * count);
    639   1.1       ad 
    640   1.1       ad 	for (i = 0, ccb = ccbstore; i < count; i++, ccb++) {
    641   1.1       ad 		if ((error = dpt_init_ccb(sc, ccb)) != 0) {
    642   1.1       ad 			printf("%s: unable to init ccb, error = %d\n",
    643   1.1       ad 			    sc->sc_dv.dv_xname, error);
    644   1.1       ad 			break;
    645   1.1       ad 		}
    646   1.1       ad 		ccb->ccb_id = i;
    647   1.1       ad 		TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, ccb_chain);
    648   1.1       ad 	}
    649   1.1       ad 
    650   1.1       ad 	return (i);
    651   1.1       ad }
    652   1.1       ad 
    653   1.1       ad /*
    654   1.9       ad  * Get a free ccb. If there are none, see if we can allocate a new one. If
    655   1.9       ad  * none are available right now and we are permitted to sleep, then wait
    656   1.9       ad  * until one becomes free, otherwise return an error.
    657   1.1       ad  */
    658   1.1       ad struct dpt_ccb *
    659   1.1       ad dpt_alloc_ccb(sc, flg)
    660   1.1       ad 	struct dpt_softc *sc;
    661   1.1       ad 	int flg;
    662   1.1       ad {
    663   1.1       ad 	struct dpt_ccb *ccb;
    664   1.1       ad 	int s;
    665   1.1       ad 
    666   1.1       ad 	s = splbio();
    667   1.1       ad 
    668   1.1       ad 	for (;;) {
    669  1.19       ad 		if ((ccb = TAILQ_FIRST(&sc->sc_free_ccb)) != NULL) {
    670   1.1       ad 			TAILQ_REMOVE(&sc->sc_free_ccb, ccb, ccb_chain);
    671   1.1       ad 			break;
    672   1.1       ad 		}
    673   1.6  thorpej 		if ((flg & XS_CTL_NOSLEEP) != 0) {
    674  1.19       ad 			ccb = NULL;
    675  1.19       ad 			break;
    676   1.1       ad 		}
    677   1.1       ad 		tsleep(&sc->sc_free_ccb, PRIBIO, "dptccb", 0);
    678   1.1       ad 	}
    679   1.1       ad 
    680   1.1       ad 	splx(s);
    681   1.1       ad 	return (ccb);
    682   1.1       ad }
    683   1.1       ad 
    684   1.1       ad /*
    685   1.1       ad  * We have a CCB which has been processed by the HBA, now we look to see how
    686   1.2       ad  * the operation went. CCBs marked with CCB_PRIVATE are not automatically
    687   1.2       ad  * passed here by dpt_intr().
    688   1.1       ad  */
    689   1.1       ad void
    690   1.1       ad dpt_done_ccb(sc, ccb)
    691   1.1       ad 	struct dpt_softc *sc;
    692   1.1       ad 	struct dpt_ccb *ccb;
    693   1.1       ad {
    694   1.1       ad 	struct scsipi_sense_data *s1, *s2;
    695   1.1       ad 	struct scsipi_xfer *xs;
    696   1.1       ad 	bus_dma_tag_t dmat;
    697   1.1       ad 
    698   1.1       ad 	dmat = sc->sc_dmat;
    699   1.1       ad 	xs = ccb->ccb_xs;
    700   1.1       ad 
    701   1.1       ad 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("dpt_done_ccb\n"));
    702   1.1       ad 
    703   1.1       ad 	/*
    704   1.1       ad 	 * If we were a data transfer, unload the map that described the
    705   1.1       ad 	 * data buffer.
    706   1.1       ad 	 */
    707  1.19       ad 	if (xs->datalen != 0) {
    708   1.1       ad 		bus_dmamap_sync(dmat, ccb->ccb_dmamap_xfer, 0,
    709   1.1       ad 		    ccb->ccb_dmamap_xfer->dm_mapsize,
    710   1.6  thorpej 		    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
    711   1.1       ad 		    BUS_DMASYNC_POSTWRITE);
    712   1.1       ad 		bus_dmamap_unload(dmat, ccb->ccb_dmamap_xfer);
    713   1.1       ad 	}
    714   1.1       ad 
    715   1.1       ad 	if (xs->error == XS_NOERROR) {
    716  1.19       ad 		if (ccb->ccb_hba_status != SP_HBA_NO_ERROR) {
    717   1.1       ad 			switch (ccb->ccb_hba_status) {
    718  1.19       ad 			case SP_HBA_ERROR_SEL_TO:
    719   1.1       ad 				xs->error = XS_SELTIMEOUT;
    720   1.1       ad 				break;
    721  1.19       ad 			case SP_HBA_ERROR_RESET:
    722   1.1       ad 				xs->error = XS_RESET;
    723   1.1       ad 				break;
    724   1.1       ad 			default:	/* Other scsi protocol messes */
    725   1.1       ad 				printf("%s: HBA status %x\n",
    726   1.1       ad 				    sc->sc_dv.dv_xname, ccb->ccb_hba_status);
    727   1.1       ad 				xs->error = XS_DRIVER_STUFFUP;
    728   1.1       ad 			}
    729   1.1       ad 		} else if (ccb->ccb_scsi_status != SCSI_OK) {
    730   1.1       ad 			switch (ccb->ccb_scsi_status) {
    731   1.1       ad 			case SCSI_CHECK:
    732   1.1       ad 				s1 = &ccb->ccb_sense;
    733   1.1       ad 				s2 = &xs->sense.scsi_sense;
    734   1.1       ad 				*s2 = *s1;
    735   1.1       ad 				xs->error = XS_SENSE;
    736   1.1       ad 				break;
    737   1.1       ad 			case SCSI_BUSY:
    738   1.1       ad 				xs->error = XS_BUSY;
    739   1.1       ad 				break;
    740   1.1       ad 			default:
    741   1.1       ad 				printf("%s: SCSI status %x\n",
    742   1.1       ad 				    sc->sc_dv.dv_xname, ccb->ccb_scsi_status);
    743   1.1       ad 				xs->error = XS_DRIVER_STUFFUP;
    744   1.1       ad 			}
    745   1.1       ad 		} else
    746   1.1       ad 			xs->resid = 0;
    747   1.7       ad 
    748   1.7       ad 		xs->status = ccb->ccb_scsi_status;
    749   1.1       ad 	}
    750   1.1       ad 
    751   1.1       ad 	/* Free up the CCB and mark the command as done */
    752   1.1       ad 	dpt_free_ccb(sc, ccb);
    753   1.6  thorpej 	xs->xs_status |= XS_STS_DONE;
    754   1.1       ad 	scsipi_done(xs);
    755   1.1       ad 
    756   1.1       ad 	/*
    757   1.9       ad 	 * If there are entries in the software queue, try to run the first
    758   1.9       ad 	 * one. We should be more or less guaranteed to succeed, since we
    759   1.9       ad 	 * just freed an CCB. NOTE: dpt_scsi_cmd() relies on our calling it
    760   1.9       ad 	 * with the first entry in the queue.
    761   1.1       ad 	 */
    762   1.1       ad 	if ((xs = TAILQ_FIRST(&sc->sc_queue)) != NULL)
    763   1.1       ad 		dpt_scsi_cmd(xs);
    764   1.1       ad }
    765   1.1       ad 
    766   1.1       ad /*
    767   1.1       ad  * Start a SCSI command.
    768   1.1       ad  */
    769   1.1       ad int
    770   1.1       ad dpt_scsi_cmd(xs)
    771   1.1       ad 	struct scsipi_xfer *xs;
    772   1.1       ad {
    773  1.18  thorpej 	int error, i, flags, s, fromqueue, dontqueue, nowait;
    774   1.1       ad 	struct scsipi_link *sc_link;
    775   1.1       ad 	struct dpt_softc *sc;
    776   1.1       ad 	struct dpt_ccb *ccb;
    777   1.1       ad 	struct eata_sg *sg;
    778   1.1       ad 	struct eata_cp *cp;
    779   1.1       ad 	bus_dma_tag_t dmat;
    780   1.9       ad 	bus_dmamap_t xfer;
    781   1.1       ad 
    782   1.1       ad 	sc_link = xs->sc_link;
    783   1.6  thorpej 	flags = xs->xs_control;
    784   1.1       ad 	sc = sc_link->adapter_softc;
    785   1.1       ad 	dmat = sc->sc_dmat;
    786   1.1       ad 	fromqueue = 0;
    787   1.1       ad 	dontqueue = 0;
    788  1.18  thorpej 	nowait = 0;
    789   1.1       ad 
    790   1.1       ad 	SC_DEBUG(sc_link, SDEV_DB2, ("dpt_scsi_cmd\n"));
    791   1.1       ad 
    792   1.1       ad 	/* Protect the queue */
    793   1.1       ad 	s = splbio();
    794   1.1       ad 
    795   1.1       ad 	/*
    796   1.1       ad 	 * If we're running the queue from dpt_done_ccb(), we've been called
    797   1.1       ad 	 * with the first queue entry as our argument.
    798   1.1       ad 	 */
    799   1.1       ad 	if (xs == TAILQ_FIRST(&sc->sc_queue)) {
    800   1.1       ad 		TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
    801   1.1       ad 		fromqueue = 1;
    802  1.18  thorpej 		nowait = 1;
    803   1.1       ad 	} else {
    804   1.1       ad 		/* Cmds must be no more than 12 bytes for us */
    805   1.1       ad 		if (xs->cmdlen > 12) {
    806   1.1       ad 			splx(s);
    807   1.1       ad 			xs->error = XS_DRIVER_STUFFUP;
    808   1.1       ad 			return (COMPLETE);
    809   1.1       ad 		}
    810   1.1       ad 
    811  1.19       ad 		/*
    812  1.19       ad 		 * XXX we can't reset devices just yet. Apparently some
    813  1.19       ad 		 * older firmware revisions don't even support it.
    814  1.19       ad 		 */
    815   1.6  thorpej 		if ((flags & XS_CTL_RESET) != 0) {
    816   1.1       ad 			xs->error = XS_DRIVER_STUFFUP;
    817   1.1       ad 			return (COMPLETE);
    818   1.1       ad 		}
    819   1.1       ad 
    820   1.1       ad 		/* Polled requests can't be queued for later */
    821   1.6  thorpej 		dontqueue = flags & XS_CTL_POLL;
    822   1.1       ad 
    823   1.1       ad 		/* If there are jobs in the queue, run them first */
    824   1.1       ad 		if (TAILQ_FIRST(&sc->sc_queue) != NULL) {
    825   1.1       ad 			/*
    826   1.9       ad 			 * If we can't queue we abort, since we must
    827   1.9       ad 			 * preserve the queue order.
    828   1.1       ad 			 */
    829   1.1       ad 			if (dontqueue) {
    830   1.1       ad 				splx(s);
    831   1.1       ad 				xs->error = XS_DRIVER_STUFFUP;
    832   1.1       ad 				return (TRY_AGAIN_LATER);
    833   1.1       ad 			}
    834   1.1       ad 
    835   1.1       ad 			/* Swap with the first queue entry. */
    836   1.1       ad 			TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
    837   1.1       ad 			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 		}
    841   1.1       ad 	}
    842   1.1       ad 
    843   1.9       ad 	/* Get a CCB */
    844  1.18  thorpej 	if (nowait)
    845  1.18  thorpej 		flags |= XS_CTL_NOSLEEP;
    846   1.1       ad 	if ((ccb = dpt_alloc_ccb(sc, flags)) == NULL) {
    847   1.1       ad 		/* If we can't queue, we lose */
    848   1.1       ad 		if (dontqueue) {
    849   1.1       ad 			splx(s);
    850   1.1       ad 			xs->error = XS_DRIVER_STUFFUP;
    851   1.1       ad 			return (TRY_AGAIN_LATER);
    852   1.1       ad 		}
    853   1.3       ad 
    854   1.1       ad 		/*
    855   1.1       ad 		 * Stuff request into the queue, in front if we came off
    856   1.9       ad 		 * it in the first place.
    857   1.1       ad 		 */
    858   1.1       ad 		if (fromqueue)
    859   1.1       ad 			TAILQ_INSERT_HEAD(&sc->sc_queue, xs, adapter_q);
    860   1.1       ad 		else
    861   1.1       ad 			TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
    862   1.1       ad 		splx(s);
    863   1.1       ad 		return (SUCCESSFULLY_QUEUED);
    864   1.1       ad 	}
    865   1.1       ad 
    866   1.1       ad 	splx(s);
    867   1.1       ad 
    868   1.1       ad 	ccb->ccb_xs = xs;
    869   1.1       ad 	ccb->ccb_timeout = xs->timeout;
    870   1.1       ad 
    871   1.1       ad 	cp = &ccb->ccb_eata_cp;
    872  1.19       ad 	memcpy(&cp->cp_cdb_cmd, xs->cmd, xs->cmdlen);
    873   1.1       ad 	cp->cp_ccbid = ccb->ccb_id;
    874   1.1       ad 	cp->cp_senselen = sizeof(ccb->ccb_sense);
    875  1.19       ad 	cp->cp_stataddr = htobe32(sc->sc_stppa);
    876  1.19       ad 	cp->cp_ctl0 = CP_C0_AUTO_SENSE;
    877  1.19       ad 	cp->cp_ctl1 = 0;
    878  1.19       ad 	cp->cp_ctl2 = 0;
    879  1.19       ad 	cp->cp_ctl3 = sc_link->scsipi_scsi.target << CP_C3_ID_SHIFT;
    880  1.19       ad 	cp->cp_ctl3 |= sc_link->scsipi_scsi.channel << CP_C3_CHANNEL_SHIFT;
    881  1.19       ad 	cp->cp_ctl4 = sc_link->scsipi_scsi.lun << CP_C4_LUN_SHIFT;
    882  1.19       ad 	cp->cp_ctl4 |= CP_C4_DIS_PRI | CP_C4_IDENTIFY;
    883  1.19       ad 
    884  1.19       ad 	if ((flags & XS_CTL_DATA_IN) != 0)
    885  1.19       ad 		cp->cp_ctl0 |= CP_C0_DATA_IN;
    886  1.19       ad 	if ((flags & XS_CTL_DATA_OUT) != 0)
    887  1.19       ad 		cp->cp_ctl0 |= CP_C0_DATA_OUT;
    888  1.19       ad 	if (sc->sc_hbaid[sc_link->scsipi_scsi.channel] ==
    889  1.19       ad 	    sc_link->scsipi_scsi.target)
    890  1.19       ad 	    	cp->cp_ctl0 |= CP_C0_INTERPRET;
    891   1.1       ad 
    892   1.9       ad 	/* Synchronous xfers musn't write-back through the cache */
    893   1.9       ad 	if (xs->bp != NULL && (xs->bp->b_flags & (B_ASYNC | B_READ)) == 0)
    894  1.19       ad 		cp->cp_ctl2 |= CP_C2_NO_CACHE;
    895   1.9       ad 
    896  1.19       ad 	cp->cp_senseaddr = htobe32(sc->sc_dmamap->dm_segs[0].ds_addr +
    897   1.4       ad 	    CCB_OFF(sc, ccb) + offsetof(struct dpt_ccb, ccb_sense));
    898   1.1       ad 
    899  1.13       ad 	if (xs->datalen != 0) {
    900   1.9       ad 		xfer = ccb->ccb_dmamap_xfer;
    901  1.19       ad #ifdef TFS
    902   1.9       ad 		if ((flags & XS_CTL_DATA_UIO) != 0) {
    903   1.9       ad 			error = bus_dmamap_load_uio(dmat, xfer,
    904   1.9       ad 			    (struct uio *)xs->data, (flags & XS_CTL_NOSLEEP) ?
    905   1.9       ad 			    BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
    906   1.1       ad 		} else
    907  1.19       ad #endif	/* TFS */
    908   1.1       ad 		{
    909   1.9       ad 			error = bus_dmamap_load(dmat, xfer, xs->data,
    910   1.9       ad 			    xs->datalen, NULL, (flags & XS_CTL_NOSLEEP) ?
    911   1.9       ad 			    BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
    912   1.1       ad 		}
    913   1.1       ad 
    914   1.1       ad 		if (error) {
    915   1.1       ad 			printf("%s: dpt_scsi_cmd: ", sc->sc_dv.dv_xname);
    916   1.1       ad 			if (error == EFBIG)
    917   1.1       ad 				printf("more than %d dma segs\n", DPT_SG_SIZE);
    918   1.1       ad 			else
    919   1.1       ad 				printf("error %d loading dma map\n", error);
    920   1.1       ad 
    921   1.1       ad 			xs->error = XS_DRIVER_STUFFUP;
    922   1.1       ad 			dpt_free_ccb(sc, ccb);
    923   1.1       ad 			return (COMPLETE);
    924   1.1       ad 		}
    925   1.1       ad 
    926   1.9       ad 		bus_dmamap_sync(dmat, xfer, 0, xfer->dm_mapsize,
    927   1.6  thorpej 		    (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
    928   1.1       ad 		    BUS_DMASYNC_PREWRITE);
    929   1.1       ad 
    930   1.9       ad 		/* Don't bother using scatter/gather for just 1 segment */
    931   1.9       ad 		if (xfer->dm_nsegs == 1) {
    932  1.13       ad 			cp->cp_dataaddr = htobe32(xfer->dm_segs[0].ds_addr);
    933  1.13       ad 			cp->cp_datalen = htobe32(xfer->dm_segs[0].ds_len);
    934   1.9       ad 		} else {
    935   1.9       ad 			/*
    936   1.9       ad 			 * Load the hardware scatter/gather map with the
    937   1.9       ad 			 * contents of the DMA map.
    938   1.9       ad 			 */
    939   1.9       ad 			sg = ccb->ccb_sg;
    940   1.9       ad 			for (i = 0; i < xfer->dm_nsegs; i++, sg++) {
    941  1.13       ad 				sg->sg_addr = htobe32(xfer->dm_segs[i].ds_addr);
    942  1.13       ad 				sg->sg_len = htobe32(xfer->dm_segs[i].ds_len);
    943   1.9       ad 			}
    944  1.13       ad 			cp->cp_dataaddr = htobe32(CCB_OFF(sc, ccb) +
    945  1.19       ad 			    sc->sc_dmamap->dm_segs[0].ds_addr +
    946   1.9       ad 			    offsetof(struct dpt_ccb, ccb_sg));
    947  1.13       ad 			cp->cp_datalen = htobe32(i * sizeof(struct eata_sg));
    948  1.19       ad 			cp->cp_ctl0 |= CP_C0_SCATTER;
    949   1.1       ad 		}
    950   1.1       ad 	} else {
    951   1.1       ad 		cp->cp_dataaddr = 0;
    952   1.1       ad 		cp->cp_datalen = 0;
    953   1.1       ad 	}
    954   1.1       ad 
    955   1.1       ad 	/* Sync up CCB and status packet */
    956  1.19       ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, CCB_OFF(sc, ccb),
    957   1.1       ad 	    sizeof(struct dpt_ccb), BUS_DMASYNC_PREWRITE);
    958  1.19       ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_stpoff,
    959   1.1       ad 	    sizeof(struct eata_sp), BUS_DMASYNC_PREREAD);
    960   1.1       ad 
    961   1.1       ad 	/*
    962   1.1       ad 	 * Start the command. If we are polling on completion, mark it
    963   1.1       ad 	 * private so that dpt_intr/dpt_done_ccb don't recycle the CCB
    964   1.1       ad 	 * without us noticing.
    965   1.1       ad 	 */
    966   1.1       ad 	if (dontqueue != 0)
    967   1.1       ad 		ccb->ccb_flg |= CCB_PRIVATE;
    968   1.1       ad 
    969   1.4       ad 	if (dpt_cmd(sc, &ccb->ccb_eata_cp, ccb->ccb_ccbpa, CP_DMA_CMD, 0)) {
    970   1.4       ad 		printf("%s: dpt_cmd failed\n", sc->sc_dv.dv_xname);
    971   1.4       ad 		xs->error = XS_DRIVER_STUFFUP;
    972   1.9       ad 		dpt_free_ccb(sc, ccb);
    973   1.9       ad 		return (TRY_AGAIN_LATER);
    974   1.4       ad 	}
    975   1.1       ad 
    976   1.1       ad 	if (dontqueue == 0)
    977   1.1       ad 		return (SUCCESSFULLY_QUEUED);
    978   1.1       ad 
    979   1.1       ad 	/* Don't wait longer than this single command wants to wait */
    980   1.1       ad 	if (dpt_poll(sc, ccb)) {
    981   1.1       ad 		dpt_timeout(ccb);
    982   1.1       ad 		/* Wait for abort to complete */
    983   1.1       ad 		if (dpt_poll(sc, ccb))
    984   1.1       ad 			dpt_timeout(ccb);
    985   1.1       ad 	}
    986   1.1       ad 
    987   1.1       ad 	dpt_done_ccb(sc, ccb);
    988   1.1       ad 	return (COMPLETE);
    989   1.1       ad }
    990   1.1       ad 
    991   1.1       ad /*
    992   1.1       ad  * Specified CCB has timed out, abort it.
    993   1.1       ad  */
    994   1.1       ad void
    995   1.1       ad dpt_timeout(arg)
    996   1.1       ad 	void *arg;
    997   1.1       ad {
    998   1.1       ad 	struct scsipi_link *sc_link;
    999   1.1       ad 	struct scsipi_xfer *xs;
   1000   1.1       ad 	struct dpt_softc *sc;
   1001   1.1       ad  	struct dpt_ccb *ccb;
   1002   1.1       ad 	int s;
   1003   1.1       ad 
   1004   1.1       ad 	ccb = arg;
   1005   1.1       ad 	xs = ccb->ccb_xs;
   1006   1.1       ad 	sc_link = xs->sc_link;
   1007  1.19       ad 	sc = sc_link->adapter_softc;
   1008   1.1       ad 
   1009   1.1       ad 	scsi_print_addr(sc_link);
   1010   1.1       ad 	printf("timed out (status:%02x aux status:%02x)",
   1011   1.1       ad 	    dpt_inb(sc, HA_STATUS), dpt_inb(sc, HA_AUX_STATUS));
   1012   1.1       ad 
   1013   1.1       ad 	s = splbio();
   1014   1.1       ad 
   1015   1.1       ad 	if ((ccb->ccb_flg & CCB_ABORT) != 0) {
   1016   1.1       ad 		/* Abort timed out, reset the HBA */
   1017   1.1       ad 		printf(" AGAIN, resetting HBA\n");
   1018   1.1       ad 		dpt_outb(sc, HA_COMMAND, CP_RESET);
   1019   1.1       ad 		DELAY(750000);
   1020   1.1       ad 	} else {
   1021   1.1       ad 		/* Abort the operation that has timed out */
   1022   1.1       ad 		printf("\n");
   1023   1.1       ad 		ccb->ccb_xs->error = XS_TIMEOUT;
   1024   1.1       ad 		ccb->ccb_timeout = DPT_ABORT_TIMEOUT;
   1025   1.1       ad 		ccb->ccb_flg |= CCB_ABORT;
   1026   1.1       ad 		/* Start the abort */
   1027   1.4       ad 		if (dpt_cmd(sc, &ccb->ccb_eata_cp, ccb->ccb_ccbpa,
   1028   1.4       ad 		    CP_IMMEDIATE, CPI_SPEC_ABORT))
   1029  1.19       ad 			printf("%s: dpt_cmd failed\n", sc->sc_dv.dv_xname);
   1030   1.1       ad 	}
   1031   1.1       ad 
   1032   1.1       ad 	splx(s);
   1033   1.1       ad }
   1034   1.1       ad 
   1035   1.1       ad /*
   1036   1.1       ad  * Get inquiry data from the adapter.
   1037   1.1       ad  */
   1038   1.1       ad void
   1039   1.1       ad dpt_hba_inquire(sc, ei)
   1040   1.1       ad 	struct dpt_softc *sc;
   1041   1.1       ad 	struct eata_inquiry_data **ei;
   1042   1.1       ad {
   1043   1.1       ad 	struct dpt_ccb *ccb;
   1044   1.1       ad 	struct eata_cp *cp;
   1045   1.1       ad 	bus_dma_tag_t dmat;
   1046   1.1       ad 
   1047   1.1       ad 	*ei = (struct eata_inquiry_data *)sc->sc_scr;
   1048   1.1       ad 	dmat = sc->sc_dmat;
   1049   1.1       ad 
   1050   1.1       ad 	/* Get a CCB and mark as private */
   1051   1.1       ad 	if ((ccb = dpt_alloc_ccb(sc, 0)) == NULL)
   1052   1.1       ad 		panic("%s: no CCB for inquiry", sc->sc_dv.dv_xname);
   1053   1.1       ad 
   1054   1.1       ad 	ccb->ccb_flg |= CCB_PRIVATE;
   1055   1.1       ad 	ccb->ccb_timeout = 200;
   1056   1.1       ad 
   1057   1.1       ad 	/* Put all the arguments into the CCB */
   1058   1.1       ad 	cp = &ccb->ccb_eata_cp;
   1059   1.1       ad 	cp->cp_ccbid = ccb->ccb_id;
   1060   1.1       ad 	cp->cp_senselen = sizeof(ccb->ccb_sense);
   1061  1.19       ad  	cp->cp_senseaddr = 0;
   1062  1.19       ad 	cp->cp_stataddr = htobe32(sc->sc_stppa);
   1063  1.13       ad 	cp->cp_dataaddr = htobe32(sc->sc_scrpa);
   1064  1.13       ad 	cp->cp_datalen = htobe32(sizeof(struct eata_inquiry_data));
   1065  1.19       ad 	cp->cp_ctl0 = CP_C0_DATA_IN | CP_C0_INTERPRET;
   1066  1.19       ad 	cp->cp_ctl1 = 0;
   1067  1.19       ad 	cp->cp_ctl2 = 0;
   1068  1.19       ad 	cp->cp_ctl3 = sc->sc_hbaid[0] << CP_C3_ID_SHIFT;
   1069  1.19       ad 	cp->cp_ctl4 = CP_C4_DIS_PRI | CP_C4_IDENTIFY;
   1070  1.19       ad 
   1071   1.1       ad 	/* Put together the SCSI inquiry command */
   1072  1.19       ad 	memset(&cp->cp_cdb_cmd, 0, 12);	/* XXX */
   1073  1.19       ad 	cp->cp_cdb_cmd = INQUIRY;
   1074  1.19       ad 	cp->cp_cdb_len = sizeof(struct eata_inquiry_data);
   1075   1.1       ad 
   1076   1.1       ad 	/* Sync up CCB, status packet and scratch area */
   1077  1.19       ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, CCB_OFF(sc, ccb),
   1078   1.1       ad 	    sizeof(struct dpt_ccb), BUS_DMASYNC_PREWRITE);
   1079  1.19       ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_stpoff,
   1080   1.1       ad 	    sizeof(struct eata_sp), BUS_DMASYNC_PREREAD);
   1081  1.19       ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_scroff,
   1082   1.1       ad 	    sizeof(struct eata_inquiry_data), BUS_DMASYNC_PREREAD);
   1083   1.1       ad 
   1084   1.1       ad 	/* Start the command and poll on completion */
   1085   1.4       ad 	if (dpt_cmd(sc, &ccb->ccb_eata_cp, ccb->ccb_ccbpa, CP_DMA_CMD, 0))
   1086   1.4       ad 		panic("%s: dpt_cmd failed", sc->sc_dv.dv_xname);
   1087   1.1       ad 
   1088   1.1       ad 	if (dpt_poll(sc, ccb))
   1089   1.1       ad 		panic("%s: inquiry timed out", sc->sc_dv.dv_xname);
   1090   1.1       ad 
   1091  1.19       ad 	if (ccb->ccb_hba_status != SP_HBA_NO_ERROR ||
   1092   1.1       ad 	    ccb->ccb_scsi_status != SCSI_OK)
   1093  1.16       ad 	    	panic("%s: inquiry failed (hba:%02x scsi:%02x)",
   1094   1.1       ad 	    	    sc->sc_dv.dv_xname, ccb->ccb_hba_status,
   1095   1.1       ad 	    	    ccb->ccb_scsi_status);
   1096   1.1       ad 
   1097   1.1       ad 	/* Sync up the DMA map and free CCB, returning */
   1098  1.19       ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_scroff,
   1099   1.1       ad 	    sizeof(struct eata_inquiry_data), BUS_DMASYNC_POSTREAD);
   1100   1.1       ad 	dpt_free_ccb(sc, ccb);
   1101   1.1       ad }
   1102