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