Home | History | Annotate | Line # | Download | only in ic
aha.c revision 1.32
      1  1.32    bouyer /*	$NetBSD: aha.c,v 1.32 2001/05/02 10:31:41 bouyer Exp $	*/
      2  1.17  jonathan 
      3  1.17  jonathan #include "opt_ddb.h"
      4   1.1   mycroft 
      5   1.1   mycroft #undef AHADIAG
      6   1.1   mycroft #ifdef DDB
      7   1.1   mycroft #define	integrate
      8   1.1   mycroft #else
      9   1.1   mycroft #define	integrate	static inline
     10   1.1   mycroft #endif
     11   1.1   mycroft 
     12   1.5   thorpej /*-
     13  1.14   thorpej  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
     14   1.5   thorpej  * All rights reserved.
     15   1.5   thorpej  *
     16   1.5   thorpej  * This code is derived from software contributed to The NetBSD Foundation
     17  1.18   mycroft  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
     18  1.18   mycroft  * Simulation Facility, NASA Ames Research Center.
     19   1.5   thorpej  *
     20   1.5   thorpej  * Redistribution and use in source and binary forms, with or without
     21   1.5   thorpej  * modification, are permitted provided that the following conditions
     22   1.5   thorpej  * are met:
     23   1.5   thorpej  * 1. Redistributions of source code must retain the above copyright
     24   1.5   thorpej  *    notice, this list of conditions and the following disclaimer.
     25   1.5   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     26   1.5   thorpej  *    notice, this list of conditions and the following disclaimer in the
     27   1.5   thorpej  *    documentation and/or other materials provided with the distribution.
     28   1.5   thorpej  * 3. All advertising materials mentioning features or use of this software
     29   1.5   thorpej  *    must display the following acknowledgement:
     30   1.5   thorpej  *	This product includes software developed by the NetBSD
     31   1.5   thorpej  *	Foundation, Inc. and its contributors.
     32   1.5   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     33   1.5   thorpej  *    contributors may be used to endorse or promote products derived
     34   1.5   thorpej  *    from this software without specific prior written permission.
     35   1.5   thorpej  *
     36   1.5   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     37   1.5   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     38   1.5   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     39   1.5   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     40   1.5   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     41   1.5   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     42   1.5   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     43   1.5   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     44   1.5   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     45   1.5   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     46   1.5   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     47   1.1   mycroft  */
     48   1.1   mycroft 
     49   1.1   mycroft /*
     50   1.1   mycroft  * Originally written by Julian Elischer (julian (at) tfs.com)
     51   1.1   mycroft  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     52   1.1   mycroft  *
     53   1.1   mycroft  * TRW Financial Systems, in accordance with their agreement with Carnegie
     54   1.1   mycroft  * Mellon University, makes this software available to CMU to distribute
     55   1.1   mycroft  * or use in any manner that they see fit as long as this message is kept with
     56   1.1   mycroft  * the software. For this reason TFS also grants any other persons or
     57   1.1   mycroft  * organisations permission to use or modify this software.
     58   1.1   mycroft  *
     59   1.1   mycroft  * TFS supplies this software to be publicly redistributed
     60   1.1   mycroft  * on the understanding that TFS is not responsible for the correct
     61   1.1   mycroft  * functioning of this software in any circumstances.
     62   1.1   mycroft  */
     63   1.1   mycroft 
     64   1.1   mycroft #include <sys/types.h>
     65   1.1   mycroft #include <sys/param.h>
     66   1.1   mycroft #include <sys/systm.h>
     67  1.26   thorpej #include <sys/callout.h>
     68   1.1   mycroft #include <sys/kernel.h>
     69   1.1   mycroft #include <sys/errno.h>
     70   1.1   mycroft #include <sys/ioctl.h>
     71   1.1   mycroft #include <sys/device.h>
     72   1.1   mycroft #include <sys/malloc.h>
     73   1.1   mycroft #include <sys/buf.h>
     74   1.1   mycroft #include <sys/proc.h>
     75   1.1   mycroft #include <sys/user.h>
     76   1.1   mycroft 
     77  1.29   thorpej #include <uvm/uvm_extern.h>
     78  1.29   thorpej 
     79   1.1   mycroft #include <machine/bus.h>
     80   1.1   mycroft #include <machine/intr.h>
     81   1.1   mycroft 
     82   1.7    bouyer #include <dev/scsipi/scsi_all.h>
     83   1.7    bouyer #include <dev/scsipi/scsipi_all.h>
     84   1.7    bouyer #include <dev/scsipi/scsiconf.h>
     85   1.1   mycroft 
     86   1.1   mycroft #include <dev/ic/ahareg.h>
     87   1.1   mycroft #include <dev/ic/ahavar.h>
     88   1.1   mycroft 
     89   1.1   mycroft #ifndef DDB
     90   1.1   mycroft #define Debugger() panic("should call debugger here (aha1542.c)")
     91   1.1   mycroft #endif /* ! DDB */
     92   1.1   mycroft 
     93   1.5   thorpej #define	AHA_MAXXFER	((AHA_NSEG - 1) << PGSHIFT)
     94   1.1   mycroft 
     95   1.1   mycroft #ifdef AHADEBUG
     96   1.1   mycroft int	aha_debug = 1;
     97   1.1   mycroft #endif /* AHADEBUG */
     98   1.1   mycroft 
     99   1.1   mycroft int aha_cmd __P((bus_space_tag_t, bus_space_handle_t, struct aha_softc *, int,
    100   1.1   mycroft     u_char *, int, u_char *));
    101   1.1   mycroft integrate void aha_finish_ccbs __P((struct aha_softc *));
    102   1.1   mycroft integrate void aha_reset_ccb __P((struct aha_softc *, struct aha_ccb *));
    103   1.1   mycroft void aha_free_ccb __P((struct aha_softc *, struct aha_ccb *));
    104   1.9   thorpej integrate int aha_init_ccb __P((struct aha_softc *, struct aha_ccb *));
    105  1.31    bouyer struct aha_ccb *aha_get_ccb __P((struct aha_softc *));
    106   1.1   mycroft struct aha_ccb *aha_ccb_phys_kv __P((struct aha_softc *, u_long));
    107   1.1   mycroft void aha_queue_ccb __P((struct aha_softc *, struct aha_ccb *));
    108   1.1   mycroft void aha_collect_mbo __P((struct aha_softc *));
    109   1.1   mycroft void aha_start_ccbs __P((struct aha_softc *));
    110   1.1   mycroft void aha_done __P((struct aha_softc *, struct aha_ccb *));
    111  1.15   thorpej int aha_init __P((struct aha_softc *));
    112   1.1   mycroft void aha_inquire_setup_information __P((struct aha_softc *));
    113   1.1   mycroft void ahaminphys __P((struct buf *));
    114  1.31    bouyer void aha_scsipi_request __P((struct scsipi_channel *,
    115  1.31    bouyer     scsipi_adapter_req_t, void *));
    116   1.7    bouyer int aha_poll __P((struct aha_softc *, struct scsipi_xfer *, int));
    117   1.1   mycroft void aha_timeout __P((void *arg));
    118  1.15   thorpej int aha_create_ccbs __P((struct aha_softc *, struct aha_ccb *, int));
    119   1.1   mycroft 
    120   1.1   mycroft #define AHA_RESET_TIMEOUT	2000	/* time to wait for reset (mSec) */
    121   1.1   mycroft #define	AHA_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
    122   1.5   thorpej 
    123   1.1   mycroft /*
    124   1.1   mycroft  * aha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
    125   1.1   mycroft  *
    126   1.1   mycroft  * Activate Adapter command
    127   1.1   mycroft  *    icnt:   number of args (outbound bytes including opcode)
    128   1.1   mycroft  *    ibuf:   argument buffer
    129   1.1   mycroft  *    ocnt:   number of expected returned bytes
    130   1.1   mycroft  *    obuf:   result buffer
    131   1.1   mycroft  *    wait:   number of seconds to wait for response
    132   1.1   mycroft  *
    133   1.1   mycroft  * Performs an adapter command through the ports.  Not to be confused with a
    134   1.1   mycroft  * scsi command, which is read in via the dma; one of the adapter commands
    135   1.1   mycroft  * tells it to read in a scsi command.
    136   1.1   mycroft  */
    137   1.1   mycroft int
    138   1.1   mycroft aha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
    139   1.1   mycroft 	bus_space_tag_t iot;
    140   1.1   mycroft 	bus_space_handle_t ioh;
    141   1.1   mycroft 	struct aha_softc *sc;
    142   1.1   mycroft 	int icnt, ocnt;
    143   1.1   mycroft 	u_char *ibuf, *obuf;
    144   1.1   mycroft {
    145   1.1   mycroft 	const char *name;
    146  1.28  augustss 	int i;
    147   1.1   mycroft 	int wait;
    148   1.1   mycroft 	u_char sts;
    149   1.1   mycroft 	u_char opcode = ibuf[0];
    150   1.1   mycroft 
    151   1.1   mycroft 	if (sc != NULL)
    152   1.1   mycroft 		name = sc->sc_dev.dv_xname;
    153   1.1   mycroft 	else
    154   1.1   mycroft 		name = "(aha probe)";
    155   1.1   mycroft 
    156   1.1   mycroft 	/*
    157   1.1   mycroft 	 * Calculate a reasonable timeout for the command.
    158   1.1   mycroft 	 */
    159   1.1   mycroft 	switch (opcode) {
    160   1.1   mycroft 	case AHA_INQUIRE_DEVICES:
    161   1.1   mycroft 		wait = 90 * 20000;
    162   1.1   mycroft 		break;
    163   1.1   mycroft 	default:
    164   1.1   mycroft 		wait = 1 * 20000;
    165   1.1   mycroft 		break;
    166   1.1   mycroft 	}
    167   1.1   mycroft 
    168   1.1   mycroft 	/*
    169   1.1   mycroft 	 * Wait for the adapter to go idle, unless it's one of
    170   1.1   mycroft 	 * the commands which don't need this
    171   1.1   mycroft 	 */
    172   1.1   mycroft 	if (opcode != AHA_MBO_INTR_EN) {
    173   1.1   mycroft 		for (i = 20000; i; i--) {	/* 1 sec? */
    174   1.1   mycroft 			sts = bus_space_read_1(iot, ioh, AHA_STAT_PORT);
    175   1.1   mycroft 			if (sts & AHA_STAT_IDLE)
    176   1.1   mycroft 				break;
    177   1.1   mycroft 			delay(50);
    178   1.1   mycroft 		}
    179   1.1   mycroft 		if (!i) {
    180   1.1   mycroft 			printf("%s: aha_cmd, host not idle(0x%x)\n",
    181   1.1   mycroft 			    name, sts);
    182   1.1   mycroft 			return (1);
    183   1.1   mycroft 		}
    184   1.1   mycroft 	}
    185   1.1   mycroft 	/*
    186   1.1   mycroft 	 * Now that it is idle, if we expect output, preflush the
    187   1.1   mycroft 	 * queue feeding to us.
    188   1.1   mycroft 	 */
    189   1.1   mycroft 	if (ocnt) {
    190   1.1   mycroft 		while ((bus_space_read_1(iot, ioh, AHA_STAT_PORT)) & AHA_STAT_DF)
    191   1.1   mycroft 			bus_space_read_1(iot, ioh, AHA_DATA_PORT);
    192   1.1   mycroft 	}
    193   1.1   mycroft 	/*
    194   1.1   mycroft 	 * Output the command and the number of arguments given
    195   1.1   mycroft 	 * for each byte, first check the port is empty.
    196   1.1   mycroft 	 */
    197   1.1   mycroft 	while (icnt--) {
    198   1.1   mycroft 		for (i = wait; i; i--) {
    199   1.1   mycroft 			sts = bus_space_read_1(iot, ioh, AHA_STAT_PORT);
    200   1.1   mycroft 			if (!(sts & AHA_STAT_CDF))
    201   1.1   mycroft 				break;
    202   1.1   mycroft 			delay(50);
    203   1.1   mycroft 		}
    204   1.1   mycroft 		if (!i) {
    205   1.1   mycroft 			if (opcode != AHA_INQUIRE_REVISION)
    206   1.1   mycroft 				printf("%s: aha_cmd, cmd/data port full\n", name);
    207   1.1   mycroft 			bus_space_write_1(iot, ioh, AHA_CTRL_PORT, AHA_CTRL_SRST);
    208   1.1   mycroft 			return (1);
    209   1.1   mycroft 		}
    210   1.1   mycroft 		bus_space_write_1(iot, ioh, AHA_CMD_PORT, *ibuf++);
    211   1.1   mycroft 	}
    212   1.1   mycroft 	/*
    213   1.1   mycroft 	 * If we expect input, loop that many times, each time,
    214   1.1   mycroft 	 * looking for the data register to have valid data
    215   1.1   mycroft 	 */
    216   1.1   mycroft 	while (ocnt--) {
    217   1.1   mycroft 		for (i = wait; i; i--) {
    218   1.1   mycroft 			sts = bus_space_read_1(iot, ioh, AHA_STAT_PORT);
    219   1.1   mycroft 			if (sts & AHA_STAT_DF)
    220   1.1   mycroft 				break;
    221   1.1   mycroft 			delay(50);
    222   1.1   mycroft 		}
    223   1.1   mycroft 		if (!i) {
    224   1.1   mycroft 			if (opcode != AHA_INQUIRE_REVISION)
    225   1.1   mycroft 				printf("%s: aha_cmd, cmd/data port empty %d\n",
    226   1.1   mycroft 				    name, ocnt);
    227   1.1   mycroft 			bus_space_write_1(iot, ioh, AHA_CTRL_PORT, AHA_CTRL_SRST);
    228   1.1   mycroft 			return (1);
    229   1.1   mycroft 		}
    230   1.1   mycroft 		*obuf++ = bus_space_read_1(iot, ioh, AHA_DATA_PORT);
    231   1.1   mycroft 	}
    232   1.1   mycroft 	/*
    233   1.1   mycroft 	 * Wait for the board to report a finished instruction.
    234   1.1   mycroft 	 * We may get an extra interrupt for the HACC signal, but this is
    235   1.1   mycroft 	 * unimportant.
    236   1.1   mycroft 	 */
    237   1.1   mycroft 	if (opcode != AHA_MBO_INTR_EN) {
    238   1.1   mycroft 		for (i = 20000; i; i--) {	/* 1 sec? */
    239   1.1   mycroft 			sts = bus_space_read_1(iot, ioh, AHA_INTR_PORT);
    240   1.1   mycroft 			/* XXX Need to save this in the interrupt handler? */
    241   1.1   mycroft 			if (sts & AHA_INTR_HACC)
    242   1.1   mycroft 				break;
    243   1.1   mycroft 			delay(50);
    244   1.1   mycroft 		}
    245   1.1   mycroft 		if (!i) {
    246   1.1   mycroft 			printf("%s: aha_cmd, host not finished(0x%x)\n",
    247   1.1   mycroft 			    name, sts);
    248   1.1   mycroft 			return (1);
    249   1.1   mycroft 		}
    250   1.1   mycroft 	}
    251   1.1   mycroft 	bus_space_write_1(iot, ioh, AHA_CTRL_PORT, AHA_CTRL_IRST);
    252   1.1   mycroft 	return (0);
    253   1.1   mycroft }
    254   1.1   mycroft 
    255   1.1   mycroft void
    256   1.4   mycroft aha_attach(sc, apd)
    257   1.1   mycroft 	struct aha_softc *sc;
    258   1.4   mycroft 	struct aha_probe_data *apd;
    259   1.1   mycroft {
    260  1.31    bouyer 	struct scsipi_adapter *adapt = &sc->sc_adapter;
    261  1.31    bouyer 	struct scsipi_channel *chan = &sc->sc_channel;
    262   1.1   mycroft 
    263   1.5   thorpej 	TAILQ_INIT(&sc->sc_free_ccb);
    264   1.5   thorpej 	TAILQ_INIT(&sc->sc_waiting_ccb);
    265  1.20   thorpej 
    266  1.20   thorpej 	/*
    267  1.31    bouyer 	 * Fill in the scsipi_adapter.
    268   1.1   mycroft 	 */
    269  1.31    bouyer 	memset(adapt, 0, sizeof(*adapt));
    270  1.31    bouyer 	adapt->adapt_dev = &sc->sc_dev;
    271  1.31    bouyer 	adapt->adapt_nchannels = 1;
    272  1.31    bouyer 	/* adapt_openings initialized below */
    273  1.31    bouyer 	/* adapt_max_periph initialized below */
    274  1.31    bouyer 	adapt->adapt_request = aha_scsipi_request;
    275  1.31    bouyer 	adapt->adapt_minphys = ahaminphys;
    276  1.31    bouyer 
    277  1.31    bouyer 	/*
    278  1.31    bouyer 	 * Fill in the scsipi_channel.
    279  1.31    bouyer 	 */
    280  1.31    bouyer 	memset(chan, 0, sizeof(*chan));
    281  1.31    bouyer 	chan->chan_adapter = adapt;
    282  1.32    bouyer 	chan->chan_bustype = &scsi_bustype;
    283  1.31    bouyer 	chan->chan_channel = 0;
    284  1.31    bouyer 	chan->chan_ntargets = 8;
    285  1.31    bouyer 	chan->chan_nluns = 8;
    286  1.31    bouyer 	chan->chan_id = apd->sc_scsi_dev;
    287   1.1   mycroft 
    288   1.6   hannken 	aha_inquire_setup_information(sc);
    289  1.15   thorpej 	if (aha_init(sc) != 0) {
    290  1.15   thorpej 		/* Error during initialization! */
    291  1.15   thorpej 		return;
    292  1.15   thorpej 	}
    293   1.6   hannken 
    294   1.1   mycroft 	/*
    295   1.1   mycroft 	 * ask the adapter what subunits are present
    296   1.1   mycroft 	 */
    297  1.31    bouyer 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    298   1.1   mycroft }
    299   1.1   mycroft 
    300   1.1   mycroft integrate void
    301   1.1   mycroft aha_finish_ccbs(sc)
    302   1.1   mycroft 	struct aha_softc *sc;
    303   1.1   mycroft {
    304   1.1   mycroft 	struct aha_mbx_in *wmbi;
    305   1.1   mycroft 	struct aha_ccb *ccb;
    306   1.1   mycroft 	int i;
    307   1.1   mycroft 
    308   1.1   mycroft 	wmbi = wmbx->tmbi;
    309   1.1   mycroft 
    310  1.15   thorpej 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    311  1.15   thorpej 	    AHA_MBI_OFF(wmbi), sizeof(struct aha_mbx_in),
    312  1.15   thorpej 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    313  1.15   thorpej 
    314   1.1   mycroft 	if (wmbi->stat == AHA_MBI_FREE) {
    315   1.1   mycroft 		for (i = 0; i < AHA_MBX_SIZE; i++) {
    316   1.1   mycroft 			if (wmbi->stat != AHA_MBI_FREE) {
    317   1.1   mycroft 				printf("%s: mbi not in round-robin order\n",
    318   1.1   mycroft 				    sc->sc_dev.dv_xname);
    319   1.1   mycroft 				goto AGAIN;
    320   1.1   mycroft 			}
    321   1.1   mycroft 			aha_nextmbx(wmbi, wmbx, mbi);
    322  1.15   thorpej 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    323  1.15   thorpej 			    AHA_MBI_OFF(wmbi), sizeof(struct aha_mbx_in),
    324  1.15   thorpej 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    325   1.1   mycroft 		}
    326   1.1   mycroft #ifdef AHADIAGnot
    327   1.1   mycroft 		printf("%s: mbi interrupt with no full mailboxes\n",
    328   1.1   mycroft 		    sc->sc_dev.dv_xname);
    329   1.1   mycroft #endif
    330   1.1   mycroft 		return;
    331   1.1   mycroft 	}
    332   1.1   mycroft 
    333   1.1   mycroft AGAIN:
    334   1.1   mycroft 	do {
    335   1.1   mycroft 		ccb = aha_ccb_phys_kv(sc, phystol(wmbi->ccb_addr));
    336   1.1   mycroft 		if (!ccb) {
    337   1.1   mycroft 			printf("%s: bad mbi ccb pointer; skipping\n",
    338   1.1   mycroft 			    sc->sc_dev.dv_xname);
    339   1.1   mycroft 			goto next;
    340   1.1   mycroft 		}
    341   1.1   mycroft 
    342  1.15   thorpej 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    343  1.15   thorpej 		    AHA_CCB_OFF(ccb), sizeof(struct aha_ccb),
    344  1.15   thorpej 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    345  1.15   thorpej 
    346   1.1   mycroft #ifdef AHADEBUG
    347   1.1   mycroft 		if (aha_debug) {
    348   1.1   mycroft 			u_char *cp = &ccb->scsi_cmd;
    349   1.1   mycroft 			printf("op=%x %x %x %x %x %x\n",
    350   1.1   mycroft 			    cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
    351   1.1   mycroft 			printf("stat %x for mbi addr = 0x%08x, ",
    352   1.1   mycroft 			    wmbi->stat, wmbi);
    353   1.1   mycroft 			printf("ccb addr = 0x%x\n", ccb);
    354   1.1   mycroft 		}
    355   1.1   mycroft #endif /* AHADEBUG */
    356   1.1   mycroft 
    357   1.1   mycroft 		switch (wmbi->stat) {
    358   1.1   mycroft 		case AHA_MBI_OK:
    359   1.1   mycroft 		case AHA_MBI_ERROR:
    360   1.1   mycroft 			if ((ccb->flags & CCB_ABORT) != 0) {
    361   1.1   mycroft 				/*
    362   1.1   mycroft 				 * If we already started an abort, wait for it
    363   1.1   mycroft 				 * to complete before clearing the CCB.  We
    364   1.1   mycroft 				 * could instead just clear CCB_SENDING, but
    365   1.1   mycroft 				 * what if the mailbox was already received?
    366   1.1   mycroft 				 * The worst that happens here is that we clear
    367   1.1   mycroft 				 * the CCB a bit later than we need to.  BFD.
    368   1.1   mycroft 				 */
    369   1.1   mycroft 				goto next;
    370   1.1   mycroft 			}
    371   1.1   mycroft 			break;
    372   1.1   mycroft 
    373   1.1   mycroft 		case AHA_MBI_ABORT:
    374   1.1   mycroft 		case AHA_MBI_UNKNOWN:
    375   1.1   mycroft 			/*
    376   1.1   mycroft 			 * Even if the CCB wasn't found, we clear it anyway.
    377   1.1   mycroft 			 * See preceeding comment.
    378   1.1   mycroft 			 */
    379   1.1   mycroft 			break;
    380   1.1   mycroft 
    381   1.1   mycroft 		default:
    382   1.1   mycroft 			printf("%s: bad mbi status %02x; skipping\n",
    383   1.1   mycroft 			    sc->sc_dev.dv_xname, wmbi->stat);
    384   1.1   mycroft 			goto next;
    385   1.1   mycroft 		}
    386   1.1   mycroft 
    387  1.26   thorpej 		callout_stop(&ccb->xs->xs_callout);
    388   1.1   mycroft 		aha_done(sc, ccb);
    389   1.1   mycroft 
    390   1.1   mycroft 	next:
    391   1.1   mycroft 		wmbi->stat = AHA_MBI_FREE;
    392  1.15   thorpej 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    393  1.15   thorpej 		    AHA_MBI_OFF(wmbi), sizeof(struct aha_mbx_in),
    394  1.15   thorpej 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    395   1.1   mycroft 		aha_nextmbx(wmbi, wmbx, mbi);
    396  1.15   thorpej 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    397  1.15   thorpej 		    AHA_MBI_OFF(wmbi), sizeof(struct aha_mbx_in),
    398  1.15   thorpej 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    399   1.1   mycroft 	} while (wmbi->stat != AHA_MBI_FREE);
    400   1.1   mycroft 
    401   1.1   mycroft 	wmbx->tmbi = wmbi;
    402   1.1   mycroft }
    403   1.1   mycroft 
    404   1.1   mycroft /*
    405   1.1   mycroft  * Catch an interrupt from the adaptor
    406   1.1   mycroft  */
    407   1.1   mycroft int
    408   1.1   mycroft aha_intr(arg)
    409   1.1   mycroft 	void *arg;
    410   1.1   mycroft {
    411   1.1   mycroft 	struct aha_softc *sc = arg;
    412   1.1   mycroft 	bus_space_tag_t iot = sc->sc_iot;
    413   1.1   mycroft 	bus_space_handle_t ioh = sc->sc_ioh;
    414   1.1   mycroft 	u_char sts;
    415   1.1   mycroft 
    416   1.1   mycroft #ifdef AHADEBUG
    417   1.1   mycroft 	printf("%s: aha_intr ", sc->sc_dev.dv_xname);
    418   1.1   mycroft #endif /*AHADEBUG */
    419   1.1   mycroft 
    420   1.1   mycroft 	/*
    421   1.1   mycroft 	 * First acknowlege the interrupt, Then if it's not telling about
    422   1.1   mycroft 	 * a completed operation just return.
    423   1.1   mycroft 	 */
    424   1.1   mycroft 	sts = bus_space_read_1(iot, ioh, AHA_INTR_PORT);
    425   1.1   mycroft 	if ((sts & AHA_INTR_ANYINTR) == 0)
    426   1.1   mycroft 		return (0);
    427   1.1   mycroft 	bus_space_write_1(iot, ioh, AHA_CTRL_PORT, AHA_CTRL_IRST);
    428   1.1   mycroft 
    429   1.1   mycroft #ifdef AHADIAG
    430   1.1   mycroft 	/* Make sure we clear CCB_SENDING before finishing a CCB. */
    431   1.1   mycroft 	aha_collect_mbo(sc);
    432   1.1   mycroft #endif
    433   1.1   mycroft 
    434   1.1   mycroft 	/* Mail box out empty? */
    435   1.1   mycroft 	if (sts & AHA_INTR_MBOA) {
    436   1.1   mycroft 		struct aha_toggle toggle;
    437   1.1   mycroft 
    438   1.1   mycroft 		toggle.cmd.opcode = AHA_MBO_INTR_EN;
    439   1.1   mycroft 		toggle.cmd.enable = 0;
    440   1.1   mycroft 		aha_cmd(iot, ioh, sc,
    441   1.1   mycroft 		    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
    442   1.1   mycroft 		    0, (u_char *)0);
    443   1.1   mycroft 		aha_start_ccbs(sc);
    444   1.1   mycroft 	}
    445   1.1   mycroft 
    446   1.1   mycroft 	/* Mail box in full? */
    447   1.1   mycroft 	if (sts & AHA_INTR_MBIF)
    448   1.1   mycroft 		aha_finish_ccbs(sc);
    449   1.1   mycroft 
    450   1.1   mycroft 	return (1);
    451   1.1   mycroft }
    452   1.1   mycroft 
    453   1.1   mycroft integrate void
    454   1.1   mycroft aha_reset_ccb(sc, ccb)
    455   1.1   mycroft 	struct aha_softc *sc;
    456   1.1   mycroft 	struct aha_ccb *ccb;
    457   1.1   mycroft {
    458   1.1   mycroft 
    459   1.1   mycroft 	ccb->flags = 0;
    460   1.1   mycroft }
    461   1.1   mycroft 
    462   1.1   mycroft /*
    463   1.1   mycroft  * A ccb is put onto the free list.
    464   1.1   mycroft  */
    465   1.1   mycroft void
    466   1.1   mycroft aha_free_ccb(sc, ccb)
    467   1.1   mycroft 	struct aha_softc *sc;
    468   1.1   mycroft 	struct aha_ccb *ccb;
    469   1.1   mycroft {
    470   1.1   mycroft 	int s;
    471   1.1   mycroft 
    472   1.1   mycroft 	s = splbio();
    473   1.1   mycroft 	aha_reset_ccb(sc, ccb);
    474   1.1   mycroft 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
    475   1.1   mycroft 	splx(s);
    476   1.1   mycroft }
    477   1.1   mycroft 
    478   1.9   thorpej integrate int
    479   1.1   mycroft aha_init_ccb(sc, ccb)
    480   1.1   mycroft 	struct aha_softc *sc;
    481   1.1   mycroft 	struct aha_ccb *ccb;
    482   1.1   mycroft {
    483   1.5   thorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
    484   1.9   thorpej 	int hashnum, error;
    485  1.26   thorpej 
    486   1.5   thorpej 	/*
    487  1.15   thorpej 	 * Create the DMA map for this CCB.
    488   1.5   thorpej 	 */
    489   1.9   thorpej 	error = bus_dmamap_create(dmat, AHA_MAXXFER, AHA_NSEG, AHA_MAXXFER,
    490   1.9   thorpej 	    0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &ccb->dmamap_xfer);
    491   1.9   thorpej 	if (error) {
    492  1.15   thorpej 		printf("%s: unable to create ccb DMA map, error = %d\n",
    493  1.15   thorpej 		    sc->sc_dev.dv_xname, error);
    494   1.9   thorpej 		return (error);
    495   1.9   thorpej 	}
    496   1.5   thorpej 
    497   1.1   mycroft 	/*
    498   1.1   mycroft 	 * put in the phystokv hash table
    499   1.1   mycroft 	 * Never gets taken out.
    500   1.1   mycroft 	 */
    501  1.15   thorpej 	ccb->hashkey = sc->sc_dmamap_control->dm_segs[0].ds_addr +
    502  1.15   thorpej 	    AHA_CCB_OFF(ccb);
    503   1.1   mycroft 	hashnum = CCB_HASH(ccb->hashkey);
    504   1.1   mycroft 	ccb->nexthash = sc->sc_ccbhash[hashnum];
    505   1.1   mycroft 	sc->sc_ccbhash[hashnum] = ccb;
    506   1.1   mycroft 	aha_reset_ccb(sc, ccb);
    507   1.9   thorpej 	return (0);
    508   1.1   mycroft }
    509   1.1   mycroft 
    510   1.1   mycroft /*
    511  1.15   thorpej  * Create a set of ccbs and add them to the free list.  Called once
    512  1.15   thorpej  * by aha_init().  We return the number of CCBs successfully created.
    513   1.5   thorpej  */
    514   1.5   thorpej int
    515  1.15   thorpej aha_create_ccbs(sc, ccbstore, count)
    516   1.5   thorpej 	struct aha_softc *sc;
    517  1.15   thorpej 	struct aha_ccb *ccbstore;
    518  1.15   thorpej 	int count;
    519   1.5   thorpej {
    520   1.5   thorpej 	struct aha_ccb *ccb;
    521  1.15   thorpej 	int i, error;
    522   1.5   thorpej 
    523  1.15   thorpej 	bzero(ccbstore, sizeof(struct aha_ccb) * count);
    524  1.15   thorpej 	for (i = 0; i < count; i++) {
    525  1.15   thorpej 		ccb = &ccbstore[i];
    526  1.15   thorpej 		if ((error = aha_init_ccb(sc, ccb)) != 0) {
    527  1.15   thorpej 			printf("%s: unable to initialize ccb, error = %d\n",
    528  1.15   thorpej 			    sc->sc_dev.dv_xname, error);
    529  1.15   thorpej 			goto out;
    530   1.9   thorpej 		}
    531   1.5   thorpej 		TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain);
    532   1.5   thorpej 	}
    533  1.15   thorpej  out:
    534  1.15   thorpej 	return (i);
    535   1.5   thorpej }
    536   1.5   thorpej 
    537   1.5   thorpej /*
    538   1.1   mycroft  * Get a free ccb
    539   1.1   mycroft  *
    540   1.1   mycroft  * If there are none, see if we can allocate a new one.  If so, put it in
    541   1.1   mycroft  * the hash table too otherwise either return an error or sleep.
    542   1.1   mycroft  */
    543   1.1   mycroft struct aha_ccb *
    544  1.31    bouyer aha_get_ccb(sc)
    545   1.1   mycroft 	struct aha_softc *sc;
    546   1.1   mycroft {
    547   1.1   mycroft 	struct aha_ccb *ccb;
    548   1.1   mycroft 	int s;
    549   1.1   mycroft 
    550   1.1   mycroft 	s = splbio();
    551  1.31    bouyer 	ccb = TAILQ_FIRST(&sc->sc_free_ccb);
    552  1.31    bouyer 	if (ccb != NULL) {
    553  1.31    bouyer 		TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
    554  1.31    bouyer 		ccb->flags |= CCB_ALLOC;
    555   1.1   mycroft 	}
    556   1.1   mycroft 	splx(s);
    557   1.1   mycroft 	return (ccb);
    558   1.1   mycroft }
    559   1.1   mycroft 
    560   1.1   mycroft /*
    561   1.1   mycroft  * Given a physical address, find the ccb that it corresponds to.
    562   1.1   mycroft  */
    563   1.1   mycroft struct aha_ccb *
    564   1.1   mycroft aha_ccb_phys_kv(sc, ccb_phys)
    565   1.1   mycroft 	struct aha_softc *sc;
    566   1.1   mycroft 	u_long ccb_phys;
    567   1.1   mycroft {
    568   1.1   mycroft 	int hashnum = CCB_HASH(ccb_phys);
    569   1.1   mycroft 	struct aha_ccb *ccb = sc->sc_ccbhash[hashnum];
    570   1.1   mycroft 
    571   1.1   mycroft 	while (ccb) {
    572   1.1   mycroft 		if (ccb->hashkey == ccb_phys)
    573   1.1   mycroft 			break;
    574   1.1   mycroft 		ccb = ccb->nexthash;
    575   1.1   mycroft 	}
    576   1.1   mycroft 	return (ccb);
    577   1.1   mycroft }
    578   1.1   mycroft 
    579   1.1   mycroft /*
    580   1.1   mycroft  * Queue a CCB to be sent to the controller, and send it if possible.
    581   1.1   mycroft  */
    582   1.1   mycroft void
    583   1.1   mycroft aha_queue_ccb(sc, ccb)
    584   1.1   mycroft 	struct aha_softc *sc;
    585   1.1   mycroft 	struct aha_ccb *ccb;
    586   1.1   mycroft {
    587   1.1   mycroft 
    588   1.1   mycroft 	TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
    589   1.1   mycroft 	aha_start_ccbs(sc);
    590   1.1   mycroft }
    591   1.1   mycroft 
    592   1.1   mycroft /*
    593   1.1   mycroft  * Garbage collect mailboxes that are no longer in use.
    594   1.1   mycroft  */
    595   1.1   mycroft void
    596   1.1   mycroft aha_collect_mbo(sc)
    597   1.1   mycroft 	struct aha_softc *sc;
    598   1.1   mycroft {
    599   1.1   mycroft 	struct aha_mbx_out *wmbo;	/* Mail Box Out pointer */
    600   1.1   mycroft #ifdef AHADIAG
    601   1.1   mycroft 	struct aha_ccb *ccb;
    602   1.1   mycroft #endif
    603   1.1   mycroft 
    604   1.1   mycroft 	wmbo = wmbx->cmbo;
    605   1.1   mycroft 
    606   1.1   mycroft 	while (sc->sc_mbofull > 0) {
    607  1.15   thorpej 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    608  1.15   thorpej 		    AHA_MBO_OFF(wmbo), sizeof(struct aha_mbx_out),
    609  1.15   thorpej 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    610   1.1   mycroft 		if (wmbo->cmd != AHA_MBO_FREE)
    611   1.1   mycroft 			break;
    612   1.1   mycroft 
    613   1.1   mycroft #ifdef AHADIAG
    614   1.1   mycroft 		ccb = aha_ccb_phys_kv(sc, phystol(wmbo->ccb_addr));
    615   1.1   mycroft 		ccb->flags &= ~CCB_SENDING;
    616   1.1   mycroft #endif
    617   1.1   mycroft 
    618   1.1   mycroft 		--sc->sc_mbofull;
    619   1.1   mycroft 		aha_nextmbx(wmbo, wmbx, mbo);
    620   1.1   mycroft 	}
    621   1.1   mycroft 
    622   1.1   mycroft 	wmbx->cmbo = wmbo;
    623   1.1   mycroft }
    624   1.1   mycroft 
    625   1.1   mycroft /*
    626   1.1   mycroft  * Send as many CCBs as we have empty mailboxes for.
    627   1.1   mycroft  */
    628   1.1   mycroft void
    629   1.1   mycroft aha_start_ccbs(sc)
    630   1.1   mycroft 	struct aha_softc *sc;
    631   1.1   mycroft {
    632   1.1   mycroft 	bus_space_tag_t iot = sc->sc_iot;
    633   1.1   mycroft 	bus_space_handle_t ioh = sc->sc_ioh;
    634   1.1   mycroft 	struct aha_mbx_out *wmbo;	/* Mail Box Out pointer */
    635   1.1   mycroft 	struct aha_ccb *ccb;
    636   1.1   mycroft 
    637   1.1   mycroft 	wmbo = wmbx->tmbo;
    638   1.1   mycroft 
    639   1.1   mycroft 	while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
    640   1.1   mycroft 		if (sc->sc_mbofull >= AHA_MBX_SIZE) {
    641   1.1   mycroft 			aha_collect_mbo(sc);
    642   1.1   mycroft 			if (sc->sc_mbofull >= AHA_MBX_SIZE) {
    643   1.1   mycroft 				struct aha_toggle toggle;
    644   1.1   mycroft 
    645   1.1   mycroft 				toggle.cmd.opcode = AHA_MBO_INTR_EN;
    646   1.1   mycroft 				toggle.cmd.enable = 1;
    647   1.1   mycroft 				aha_cmd(iot, ioh, sc,
    648   1.1   mycroft 				    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
    649   1.1   mycroft 				    0, (u_char *)0);
    650   1.1   mycroft 				break;
    651   1.1   mycroft 			}
    652   1.1   mycroft 		}
    653   1.1   mycroft 
    654   1.1   mycroft 		TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
    655   1.1   mycroft #ifdef AHADIAG
    656   1.1   mycroft 		ccb->flags |= CCB_SENDING;
    657   1.1   mycroft #endif
    658   1.1   mycroft 
    659   1.1   mycroft 		/* Link ccb to mbo. */
    660  1.15   thorpej 		ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
    661  1.15   thorpej 		    AHA_CCB_OFF(ccb), wmbo->ccb_addr);
    662   1.1   mycroft 		if (ccb->flags & CCB_ABORT)
    663   1.1   mycroft 			wmbo->cmd = AHA_MBO_ABORT;
    664   1.1   mycroft 		else
    665   1.1   mycroft 			wmbo->cmd = AHA_MBO_START;
    666   1.1   mycroft 
    667  1.15   thorpej 		 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    668  1.15   thorpej 		     AHA_MBO_OFF(wmbo), sizeof(struct aha_mbx_out),
    669  1.15   thorpej 		     BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    670  1.15   thorpej 
    671   1.1   mycroft 		/* Tell the card to poll immediately. */
    672   1.1   mycroft 		bus_space_write_1(iot, ioh, AHA_CMD_PORT, AHA_START_SCSI);
    673   1.1   mycroft 
    674  1.24   thorpej 		if ((ccb->xs->xs_control & XS_CTL_POLL) == 0)
    675  1.26   thorpej 			callout_reset(&ccb->xs->xs_callout,
    676  1.26   thorpej 			    (ccb->timeout * hz) / 1000, aha_timeout, ccb);
    677   1.1   mycroft 
    678   1.1   mycroft 		++sc->sc_mbofull;
    679   1.1   mycroft 		aha_nextmbx(wmbo, wmbx, mbo);
    680   1.1   mycroft 	}
    681   1.1   mycroft 
    682   1.1   mycroft 	wmbx->tmbo = wmbo;
    683   1.1   mycroft }
    684   1.1   mycroft 
    685   1.1   mycroft /*
    686   1.1   mycroft  * We have a ccb which has been processed by the
    687   1.1   mycroft  * adaptor, now we look to see how the operation
    688   1.1   mycroft  * went. Wake up the owner if waiting
    689   1.1   mycroft  */
    690   1.1   mycroft void
    691   1.1   mycroft aha_done(sc, ccb)
    692   1.1   mycroft 	struct aha_softc *sc;
    693   1.1   mycroft 	struct aha_ccb *ccb;
    694   1.1   mycroft {
    695   1.5   thorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
    696   1.7    bouyer 	struct scsipi_sense_data *s1, *s2;
    697   1.7    bouyer 	struct scsipi_xfer *xs = ccb->xs;
    698   1.1   mycroft 
    699  1.31    bouyer 	SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("aha_done\n"));
    700   1.5   thorpej 
    701   1.5   thorpej 	/*
    702   1.5   thorpej 	 * If we were a data transfer, unload the map that described
    703   1.5   thorpej 	 * the data buffer.
    704   1.5   thorpej 	 */
    705   1.5   thorpej 	if (xs->datalen) {
    706  1.14   thorpej 		bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
    707  1.14   thorpej 		    ccb->dmamap_xfer->dm_mapsize,
    708  1.24   thorpej 		    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
    709   1.5   thorpej 		    BUS_DMASYNC_POSTWRITE);
    710   1.5   thorpej 		bus_dmamap_unload(dmat, ccb->dmamap_xfer);
    711   1.5   thorpej 	}
    712   1.5   thorpej 
    713   1.1   mycroft 	/*
    714   1.1   mycroft 	 * Otherwise, put the results of the operation
    715   1.1   mycroft 	 * into the xfer and call whoever started it
    716   1.1   mycroft 	 */
    717   1.1   mycroft #ifdef AHADIAG
    718   1.1   mycroft 	if (ccb->flags & CCB_SENDING) {
    719   1.1   mycroft 		printf("%s: exiting ccb still in transit!\n", sc->sc_dev.dv_xname);
    720   1.1   mycroft 		Debugger();
    721   1.1   mycroft 		return;
    722   1.1   mycroft 	}
    723   1.1   mycroft #endif
    724   1.1   mycroft 	if ((ccb->flags & CCB_ALLOC) == 0) {
    725   1.1   mycroft 		printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
    726   1.1   mycroft 		Debugger();
    727   1.1   mycroft 		return;
    728   1.1   mycroft 	}
    729   1.1   mycroft 	if (xs->error == XS_NOERROR) {
    730   1.1   mycroft 		if (ccb->host_stat != AHA_OK) {
    731   1.1   mycroft 			switch (ccb->host_stat) {
    732   1.1   mycroft 			case AHA_SEL_TIMEOUT:	/* No response */
    733   1.1   mycroft 				xs->error = XS_SELTIMEOUT;
    734   1.1   mycroft 				break;
    735   1.1   mycroft 			default:	/* Other scsi protocol messes */
    736   1.1   mycroft 				printf("%s: host_stat %x\n",
    737   1.1   mycroft 				    sc->sc_dev.dv_xname, ccb->host_stat);
    738   1.1   mycroft 				xs->error = XS_DRIVER_STUFFUP;
    739   1.1   mycroft 				break;
    740   1.1   mycroft 			}
    741   1.1   mycroft 		} else if (ccb->target_stat != SCSI_OK) {
    742   1.1   mycroft 			switch (ccb->target_stat) {
    743   1.1   mycroft 			case SCSI_CHECK:
    744   1.7    bouyer 				s1 = (struct scsipi_sense_data *) (((char *) (&ccb->scsi_cmd)) +
    745   1.1   mycroft 				    ccb->scsi_cmd_length);
    746   1.7    bouyer 				s2 = &xs->sense.scsi_sense;
    747   1.1   mycroft 				*s2 = *s1;
    748   1.1   mycroft 				xs->error = XS_SENSE;
    749   1.1   mycroft 				break;
    750   1.1   mycroft 			case SCSI_BUSY:
    751   1.1   mycroft 				xs->error = XS_BUSY;
    752   1.1   mycroft 				break;
    753   1.1   mycroft 			default:
    754   1.1   mycroft 				printf("%s: target_stat %x\n",
    755   1.1   mycroft 				    sc->sc_dev.dv_xname, ccb->target_stat);
    756   1.1   mycroft 				xs->error = XS_DRIVER_STUFFUP;
    757   1.1   mycroft 				break;
    758   1.1   mycroft 			}
    759   1.1   mycroft 		} else
    760   1.1   mycroft 			xs->resid = 0;
    761   1.1   mycroft 	}
    762   1.1   mycroft 	aha_free_ccb(sc, ccb);
    763   1.7    bouyer 	scsipi_done(xs);
    764   1.1   mycroft }
    765   1.1   mycroft 
    766   1.1   mycroft /*
    767   1.1   mycroft  * Find the board and find its irq/drq
    768   1.1   mycroft  */
    769   1.1   mycroft int
    770   1.1   mycroft aha_find(iot, ioh, sc)
    771   1.1   mycroft 	bus_space_tag_t iot;
    772   1.1   mycroft 	bus_space_handle_t ioh;
    773   1.4   mycroft 	struct aha_probe_data *sc;
    774   1.1   mycroft {
    775   1.1   mycroft 	int i;
    776   1.1   mycroft 	u_char sts;
    777   1.1   mycroft 	struct aha_config config;
    778   1.1   mycroft 	int irq, drq;
    779   1.1   mycroft 
    780   1.1   mycroft 	/*
    781   1.1   mycroft 	 * reset board, If it doesn't respond, assume
    782   1.1   mycroft 	 * that it's not there.. good for the probe
    783   1.1   mycroft 	 */
    784   1.1   mycroft 
    785   1.1   mycroft 	bus_space_write_1(iot, ioh, AHA_CTRL_PORT, AHA_CTRL_HRST | AHA_CTRL_SRST);
    786   1.1   mycroft 
    787   1.1   mycroft 	delay(100);
    788   1.1   mycroft 	for (i = AHA_RESET_TIMEOUT; i; i--) {
    789   1.1   mycroft 		sts = bus_space_read_1(iot, ioh, AHA_STAT_PORT);
    790   1.1   mycroft 		if (sts == (AHA_STAT_IDLE | AHA_STAT_INIT))
    791   1.1   mycroft 			break;
    792   1.1   mycroft 		delay(1000);	/* calibrated in msec */
    793   1.1   mycroft 	}
    794   1.1   mycroft 	if (!i) {
    795   1.1   mycroft #ifdef AHADEBUG
    796   1.1   mycroft 		if (aha_debug)
    797   1.1   mycroft 			printf("aha_find: No answer from adaptec board\n");
    798   1.1   mycroft #endif /* AHADEBUG */
    799   1.1   mycroft 		return (0);
    800   1.1   mycroft 	}
    801   1.1   mycroft 
    802   1.1   mycroft 	/*
    803   1.1   mycroft 	 * setup dma channel from jumpers and save int
    804   1.1   mycroft 	 * level
    805   1.1   mycroft 	 */
    806   1.1   mycroft 	delay(1000);		/* for Bustek 545 */
    807   1.1   mycroft 	config.cmd.opcode = AHA_INQUIRE_CONFIG;
    808   1.4   mycroft 	aha_cmd(iot, ioh, (struct aha_softc *)0,
    809   1.1   mycroft 	    sizeof(config.cmd), (u_char *)&config.cmd,
    810   1.1   mycroft 	    sizeof(config.reply), (u_char *)&config.reply);
    811   1.1   mycroft 	switch (config.reply.chan) {
    812   1.1   mycroft 	case EISADMA:
    813   1.1   mycroft 		drq = -1;
    814   1.1   mycroft 		break;
    815   1.1   mycroft 	case CHAN0:
    816   1.1   mycroft 		drq = 0;
    817   1.1   mycroft 		break;
    818   1.1   mycroft 	case CHAN5:
    819   1.1   mycroft 		drq = 5;
    820   1.1   mycroft 		break;
    821   1.1   mycroft 	case CHAN6:
    822   1.1   mycroft 		drq = 6;
    823   1.1   mycroft 		break;
    824   1.1   mycroft 	case CHAN7:
    825   1.1   mycroft 		drq = 7;
    826   1.1   mycroft 		break;
    827   1.1   mycroft 	default:
    828   1.1   mycroft 		printf("aha_find: illegal drq setting %x\n", config.reply.chan);
    829   1.1   mycroft 		return (0);
    830   1.1   mycroft 	}
    831   1.1   mycroft 
    832   1.1   mycroft 	switch (config.reply.intr) {
    833   1.1   mycroft 	case INT9:
    834   1.1   mycroft 		irq = 9;
    835   1.1   mycroft 		break;
    836   1.1   mycroft 	case INT10:
    837   1.1   mycroft 		irq = 10;
    838   1.1   mycroft 		break;
    839   1.1   mycroft 	case INT11:
    840   1.1   mycroft 		irq = 11;
    841   1.1   mycroft 		break;
    842   1.1   mycroft 	case INT12:
    843   1.1   mycroft 		irq = 12;
    844   1.1   mycroft 		break;
    845   1.1   mycroft 	case INT14:
    846   1.1   mycroft 		irq = 14;
    847   1.1   mycroft 		break;
    848   1.1   mycroft 	case INT15:
    849   1.1   mycroft 		irq = 15;
    850   1.1   mycroft 		break;
    851   1.1   mycroft 	default:
    852   1.1   mycroft 		printf("aha_find: illegal irq setting %x\n", config.reply.intr);
    853   1.1   mycroft 		return (0);
    854   1.1   mycroft 	}
    855   1.1   mycroft 
    856   1.4   mycroft 	if (sc) {
    857   1.1   mycroft 		sc->sc_irq = irq;
    858   1.1   mycroft 		sc->sc_drq = drq;
    859   1.1   mycroft 		sc->sc_scsi_dev = config.reply.scsi_dev;
    860   1.1   mycroft 	}
    861   1.1   mycroft 
    862   1.1   mycroft 	return (1);
    863   1.1   mycroft }
    864   1.1   mycroft 
    865   1.1   mycroft /*
    866   1.1   mycroft  * Start the board, ready for normal operation
    867   1.1   mycroft  */
    868  1.15   thorpej int
    869   1.1   mycroft aha_init(sc)
    870   1.1   mycroft 	struct aha_softc *sc;
    871   1.1   mycroft {
    872   1.1   mycroft 	bus_space_tag_t iot = sc->sc_iot;
    873   1.1   mycroft 	bus_space_handle_t ioh = sc->sc_ioh;
    874   1.5   thorpej 	bus_dma_segment_t seg;
    875   1.1   mycroft 	struct aha_devices devices;
    876   1.1   mycroft 	struct aha_setup setup;
    877   1.1   mycroft 	struct aha_mailbox mailbox;
    878  1.15   thorpej 	int error, i, j, initial_ccbs, rseg;
    879   1.1   mycroft 
    880   1.1   mycroft 	/*
    881   1.1   mycroft 	 * XXX
    882   1.1   mycroft 	 * If we are a 1542C or later, disable the extended BIOS so that the
    883   1.1   mycroft 	 * mailbox interface is unlocked.
    884   1.1   mycroft 	 * No need to check the extended BIOS flags as some of the
    885   1.1   mycroft 	 * extensions that cause us problems are not flagged in that byte.
    886   1.1   mycroft 	 */
    887   1.1   mycroft 	if (!strncmp(sc->sc_model, "1542C", 5)) {
    888   1.1   mycroft 		struct aha_extbios extbios;
    889   1.1   mycroft 		struct aha_unlock unlock;
    890   1.1   mycroft 
    891   1.1   mycroft 		printf("%s: unlocking mailbox interface\n", sc->sc_dev.dv_xname);
    892   1.1   mycroft 		extbios.cmd.opcode = AHA_EXT_BIOS;
    893   1.1   mycroft 		aha_cmd(iot, ioh, sc,
    894   1.1   mycroft 		    sizeof(extbios.cmd), (u_char *)&extbios.cmd,
    895   1.1   mycroft 		    sizeof(extbios.reply), (u_char *)&extbios.reply);
    896   1.1   mycroft 
    897   1.1   mycroft #ifdef AHADEBUG
    898   1.1   mycroft 		printf("%s: flags=%02x, mailboxlock=%02x\n",
    899   1.1   mycroft 		    sc->sc_dev.dv_xname,
    900   1.1   mycroft 		    extbios.reply.flags, extbios.reply.mailboxlock);
    901   1.1   mycroft #endif /* AHADEBUG */
    902   1.1   mycroft 
    903   1.1   mycroft 		unlock.cmd.opcode = AHA_MBX_ENABLE;
    904   1.1   mycroft 		unlock.cmd.junk = 0;
    905   1.1   mycroft 		unlock.cmd.magic = extbios.reply.mailboxlock;
    906   1.1   mycroft 		aha_cmd(iot, ioh, sc,
    907   1.1   mycroft 		    sizeof(unlock.cmd), (u_char *)&unlock.cmd,
    908   1.1   mycroft 		    0, (u_char *)0);
    909   1.1   mycroft 	}
    910   1.1   mycroft 
    911   1.1   mycroft #if 0
    912   1.1   mycroft 	/*
    913   1.1   mycroft 	 * Change the bus on/off times to not clash with other dma users.
    914   1.1   mycroft 	 */
    915   1.1   mycroft 	aha_cmd(iot, ioh, 1, 0, 0, 0, AHA_BUS_ON_TIME_SET, 7);
    916   1.1   mycroft 	aha_cmd(iot, ioh, 1, 0, 0, 0, AHA_BUS_OFF_TIME_SET, 4);
    917   1.1   mycroft #endif
    918   1.1   mycroft 
    919   1.1   mycroft 	/* Inquire Installed Devices (to force synchronous negotiation). */
    920   1.1   mycroft 	devices.cmd.opcode = AHA_INQUIRE_DEVICES;
    921   1.1   mycroft 	aha_cmd(iot, ioh, sc,
    922   1.1   mycroft 	    sizeof(devices.cmd), (u_char *)&devices.cmd,
    923   1.1   mycroft 	    sizeof(devices.reply), (u_char *)&devices.reply);
    924   1.1   mycroft 
    925   1.6   hannken 	/* Count installed units */
    926   1.6   hannken 	initial_ccbs = 0;
    927   1.6   hannken 	for (i = 0; i < 8; i++) {
    928   1.6   hannken 		for (j = 0; j < 8; j++) {
    929   1.6   hannken 			if (((devices.reply.lun_map[i] >> j) & 1) == 1)
    930   1.6   hannken 				initial_ccbs += 1;
    931   1.6   hannken 		}
    932   1.6   hannken 	}
    933  1.31    bouyer 	initial_ccbs *= 2;
    934  1.15   thorpej 	if (initial_ccbs > AHA_CCB_MAX)
    935  1.15   thorpej 		initial_ccbs = AHA_CCB_MAX;
    936  1.16   thorpej 	if (initial_ccbs == 0)	/* yes, this can happen */
    937  1.31    bouyer 		initial_ccbs = 2;
    938   1.6   hannken 
    939   1.1   mycroft 	/* Obtain setup information from. */
    940   1.1   mycroft 	setup.cmd.opcode = AHA_INQUIRE_SETUP;
    941   1.1   mycroft 	setup.cmd.len = sizeof(setup.reply);
    942   1.1   mycroft 	aha_cmd(iot, ioh, sc,
    943   1.1   mycroft 	    sizeof(setup.cmd), (u_char *)&setup.cmd,
    944   1.1   mycroft 	    sizeof(setup.reply), (u_char *)&setup.reply);
    945   1.1   mycroft 
    946   1.1   mycroft 	printf("%s: %s, %s\n",
    947   1.1   mycroft 	    sc->sc_dev.dv_xname,
    948   1.1   mycroft 	    setup.reply.sync_neg ? "sync" : "async",
    949   1.1   mycroft 	    setup.reply.parity ? "parity" : "no parity");
    950   1.1   mycroft 
    951   1.1   mycroft 	for (i = 0; i < 8; i++) {
    952   1.1   mycroft 		if (!setup.reply.sync[i].valid ||
    953   1.1   mycroft 		    (!setup.reply.sync[i].offset && !setup.reply.sync[i].period))
    954   1.1   mycroft 			continue;
    955   1.1   mycroft 		printf("%s targ %d: sync, offset %d, period %dnsec\n",
    956   1.1   mycroft 		    sc->sc_dev.dv_xname, i,
    957   1.1   mycroft 		    setup.reply.sync[i].offset, setup.reply.sync[i].period * 50 + 200);
    958   1.1   mycroft 	}
    959   1.1   mycroft 
    960   1.1   mycroft 	/*
    961  1.15   thorpej 	 * Allocate the mailbox and control blocks.
    962   1.5   thorpej 	 */
    963  1.15   thorpej 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct aha_control),
    964  1.29   thorpej 	    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    965  1.15   thorpej 		printf("%s: unable to allocate control structures, "
    966  1.15   thorpej 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    967  1.15   thorpej 		return (error);
    968  1.15   thorpej 	}
    969  1.15   thorpej 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    970  1.15   thorpej 	    sizeof(struct aha_control), (caddr_t *)&sc->sc_control,
    971  1.15   thorpej 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    972  1.15   thorpej 		printf("%s: unable to map control structures, error = %d\n",
    973  1.15   thorpej 		    sc->sc_dev.dv_xname, error);
    974  1.15   thorpej 		return (error);
    975  1.15   thorpej 	}
    976   1.5   thorpej 
    977   1.5   thorpej 	/*
    978  1.15   thorpej 	 * Create and load the DMA map used for the mailbox and
    979  1.15   thorpej 	 * control blocks.
    980   1.5   thorpej 	 */
    981  1.15   thorpej 	if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct aha_control),
    982  1.15   thorpej 	    1, sizeof(struct aha_control), 0, BUS_DMA_NOWAIT,
    983  1.15   thorpej 	    &sc->sc_dmamap_control)) != 0) {
    984  1.15   thorpej 		printf("%s: unable to create control DMA map, error = %d\n",
    985  1.15   thorpej 		    sc->sc_dev.dv_xname, error);
    986  1.15   thorpej 		return (error);
    987  1.15   thorpej 	}
    988  1.15   thorpej 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control,
    989  1.15   thorpej 	    sc->sc_control, sizeof(struct aha_control), NULL,
    990  1.15   thorpej 	    BUS_DMA_NOWAIT)) != 0) {
    991  1.15   thorpej 		printf("%s: unable to load control DMA map, error = %d\n",
    992  1.15   thorpej 		    sc->sc_dev.dv_xname, error);
    993  1.15   thorpej 		return (error);
    994  1.15   thorpej 	}
    995   1.5   thorpej 
    996   1.5   thorpej 	/*
    997  1.15   thorpej 	 * Initialize the control blocks.
    998   1.5   thorpej 	 */
    999  1.15   thorpej 	i = aha_create_ccbs(sc, sc->sc_control->ac_ccbs, initial_ccbs);
   1000  1.15   thorpej 	if (i == 0) {
   1001  1.15   thorpej 		printf("%s: unable to create control blocks\n",
   1002  1.15   thorpej 		    sc->sc_dev.dv_xname);
   1003  1.15   thorpej 		return (ENOMEM);
   1004  1.15   thorpej 	} else if (i != initial_ccbs) {
   1005  1.15   thorpej 		printf("%s: WARNING: only %d of %d control blocks created\n",
   1006  1.15   thorpej 		    sc->sc_dev.dv_xname, i, initial_ccbs);
   1007  1.15   thorpej 	}
   1008   1.5   thorpej 
   1009  1.31    bouyer 	sc->sc_adapter.adapt_openings = i;
   1010  1.31    bouyer 	sc->sc_adapter.adapt_max_periph = sc->sc_adapter.adapt_openings;
   1011  1.31    bouyer 
   1012   1.5   thorpej 	/*
   1013   1.1   mycroft 	 * Set up initial mail box for round-robin operation.
   1014   1.1   mycroft 	 */
   1015   1.1   mycroft 	for (i = 0; i < AHA_MBX_SIZE; i++) {
   1016   1.1   mycroft 		wmbx->mbo[i].cmd = AHA_MBO_FREE;
   1017  1.15   thorpej 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
   1018  1.15   thorpej 		    AHA_MBO_OFF(&wmbx->mbo[i]), sizeof(struct aha_mbx_out),
   1019  1.15   thorpej 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1020   1.1   mycroft 		wmbx->mbi[i].stat = AHA_MBI_FREE;
   1021  1.15   thorpej 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
   1022  1.15   thorpej 		    AHA_MBI_OFF(&wmbx->mbi[i]), sizeof(struct aha_mbx_in),
   1023  1.15   thorpej 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1024   1.1   mycroft 	}
   1025   1.1   mycroft 	wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
   1026   1.1   mycroft 	wmbx->tmbi = &wmbx->mbi[0];
   1027   1.1   mycroft 	sc->sc_mbofull = 0;
   1028   1.1   mycroft 
   1029   1.1   mycroft 	/* Initialize mail box. */
   1030   1.1   mycroft 	mailbox.cmd.opcode = AHA_MBX_INIT;
   1031   1.1   mycroft 	mailbox.cmd.nmbx = AHA_MBX_SIZE;
   1032  1.15   thorpej 	ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
   1033  1.15   thorpej 	    offsetof(struct aha_control, ac_mbx), mailbox.cmd.addr);
   1034   1.1   mycroft 	aha_cmd(iot, ioh, sc,
   1035   1.1   mycroft 	    sizeof(mailbox.cmd), (u_char *)&mailbox.cmd,
   1036   1.1   mycroft 	    0, (u_char *)0);
   1037  1.15   thorpej 	return (0);
   1038   1.1   mycroft }
   1039   1.1   mycroft 
   1040   1.1   mycroft void
   1041   1.1   mycroft aha_inquire_setup_information(sc)
   1042   1.1   mycroft 	struct aha_softc *sc;
   1043   1.1   mycroft {
   1044   1.1   mycroft 	bus_space_tag_t iot = sc->sc_iot;
   1045   1.1   mycroft 	bus_space_handle_t ioh = sc->sc_ioh;
   1046   1.1   mycroft 	struct aha_revision revision;
   1047   1.1   mycroft 	u_char sts;
   1048   1.1   mycroft 	int i;
   1049   1.1   mycroft 	char *p;
   1050   1.1   mycroft 
   1051   1.1   mycroft 	strcpy(sc->sc_model, "unknown");
   1052   1.1   mycroft 
   1053   1.1   mycroft 	/*
   1054   1.1   mycroft 	 * Assume we have a board at this stage, do an adapter inquire
   1055   1.1   mycroft 	 * to find out what type of controller it is.  If the command
   1056   1.1   mycroft 	 * fails, we assume it's either a crusty board or an old 1542
   1057   1.1   mycroft 	 * clone, and skip the board-specific stuff.
   1058   1.1   mycroft 	 */
   1059   1.1   mycroft 	revision.cmd.opcode = AHA_INQUIRE_REVISION;
   1060   1.1   mycroft 	if (aha_cmd(iot, ioh, sc,
   1061   1.1   mycroft 	    sizeof(revision.cmd), (u_char *)&revision.cmd,
   1062   1.1   mycroft 	    sizeof(revision.reply), (u_char *)&revision.reply)) {
   1063   1.1   mycroft 		/*
   1064   1.1   mycroft 		 * aha_cmd() already started the reset.  It's not clear we
   1065   1.1   mycroft 		 * even need to bother here.
   1066   1.1   mycroft 		 */
   1067   1.1   mycroft 		for (i = AHA_RESET_TIMEOUT; i; i--) {
   1068   1.1   mycroft 			sts = bus_space_read_1(iot, ioh, AHA_STAT_PORT);
   1069   1.1   mycroft 			if (sts == (AHA_STAT_IDLE | AHA_STAT_INIT))
   1070   1.1   mycroft 				break;
   1071   1.1   mycroft 			delay(1000);
   1072   1.1   mycroft 		}
   1073   1.1   mycroft 		if (!i) {
   1074   1.1   mycroft #ifdef AHADEBUG
   1075   1.1   mycroft 			printf("aha_init: soft reset failed\n");
   1076   1.1   mycroft #endif /* AHADEBUG */
   1077   1.1   mycroft 			return;
   1078   1.1   mycroft 		}
   1079   1.1   mycroft #ifdef AHADEBUG
   1080   1.1   mycroft 		printf("aha_init: inquire command failed\n");
   1081   1.1   mycroft #endif /* AHADEBUG */
   1082   1.1   mycroft 		goto noinquire;
   1083   1.1   mycroft 	}
   1084   1.1   mycroft 
   1085   1.1   mycroft #ifdef AHADEBUG
   1086   1.1   mycroft 	printf("%s: inquire %x, %x, %x, %x\n",
   1087   1.1   mycroft 	    sc->sc_dev.dv_xname,
   1088   1.1   mycroft 	    revision.reply.boardid, revision.reply.spec_opts,
   1089   1.1   mycroft 	    revision.reply.revision_1, revision.reply.revision_2);
   1090   1.1   mycroft #endif /* AHADEBUG */
   1091   1.1   mycroft 
   1092   1.1   mycroft 	switch (revision.reply.boardid) {
   1093  1.23       mjl 	case BOARD_1540_16HEAD_BIOS:
   1094  1.23       mjl 	case BOARD_1540_64HEAD_BIOS:
   1095  1.23       mjl 	case BOARD_1540:
   1096   1.1   mycroft 		strcpy(sc->sc_model, "1540");
   1097   1.1   mycroft 		break;
   1098  1.23       mjl 	case BOARD_1542:
   1099   1.1   mycroft 		strcpy(sc->sc_model, "1540A/1542A/1542B");
   1100   1.1   mycroft 		break;
   1101  1.23       mjl 	case BOARD_1640:
   1102   1.1   mycroft 		strcpy(sc->sc_model, "1640");
   1103   1.1   mycroft 		break;
   1104  1.23       mjl 	case BOARD_1740:
   1105  1.23       mjl 		strcpy(sc->sc_model, "1740");
   1106  1.23       mjl 		break;
   1107  1.23       mjl 	case BOARD_1542C:
   1108   1.1   mycroft 		strcpy(sc->sc_model, "1542C");
   1109   1.1   mycroft 		break;
   1110  1.23       mjl 	case BOARD_1542CF:
   1111   1.1   mycroft 		strcpy(sc->sc_model, "1542CF");
   1112   1.1   mycroft 		break;
   1113  1.23       mjl 	case BOARD_1542CP:
   1114   1.1   mycroft 		strcpy(sc->sc_model, "1542CP");
   1115   1.1   mycroft 		break;
   1116   1.1   mycroft 	}
   1117   1.1   mycroft 
   1118   1.1   mycroft 	p = sc->sc_firmware;
   1119   1.1   mycroft 	*p++ = revision.reply.revision_1;
   1120   1.1   mycroft 	*p++ = '.';
   1121   1.1   mycroft 	*p++ = revision.reply.revision_2;
   1122   1.1   mycroft 	*p = '\0';
   1123   1.1   mycroft 
   1124   1.1   mycroft noinquire:
   1125   1.2  sommerfe 	printf("%s: model AHA-%s, firmware %s\n",
   1126   1.2  sommerfe 	       sc->sc_dev.dv_xname,
   1127   1.2  sommerfe 	       sc->sc_model, sc->sc_firmware);
   1128   1.1   mycroft }
   1129   1.1   mycroft 
   1130   1.1   mycroft void
   1131   1.1   mycroft ahaminphys(bp)
   1132   1.1   mycroft 	struct buf *bp;
   1133   1.1   mycroft {
   1134   1.1   mycroft 
   1135   1.5   thorpej 	if (bp->b_bcount > AHA_MAXXFER)
   1136   1.5   thorpej 		bp->b_bcount = AHA_MAXXFER;
   1137   1.1   mycroft 	minphys(bp);
   1138   1.1   mycroft }
   1139   1.1   mycroft 
   1140   1.1   mycroft /*
   1141   1.1   mycroft  * start a scsi operation given the command and the data address. Also needs
   1142   1.1   mycroft  * the unit, target and lu.
   1143   1.1   mycroft  */
   1144  1.31    bouyer 
   1145  1.31    bouyer void
   1146  1.31    bouyer aha_scsipi_request(chan, req, arg)
   1147  1.31    bouyer 	struct scsipi_channel *chan;
   1148  1.31    bouyer 	scsipi_adapter_req_t req;
   1149  1.31    bouyer 	void *arg;
   1150  1.31    bouyer {
   1151   1.7    bouyer 	struct scsipi_xfer *xs;
   1152  1.31    bouyer 	struct scsipi_periph *periph;
   1153  1.31    bouyer 	struct aha_softc *sc = (void *)chan->chan_adapter->adapt_dev;
   1154   1.5   thorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
   1155   1.1   mycroft 	struct aha_ccb *ccb;
   1156   1.5   thorpej 	int error, seg, flags, s;
   1157  1.10   thorpej 
   1158  1.10   thorpej 
   1159  1.31    bouyer 	switch (req) {
   1160  1.31    bouyer 	case ADAPTER_REQ_RUN_XFER:
   1161  1.31    bouyer 		xs = arg;
   1162  1.31    bouyer 		periph = xs->xs_periph;
   1163  1.31    bouyer 		flags = xs->xs_control;
   1164  1.31    bouyer 
   1165  1.31    bouyer 		SC_DEBUG(periph, SCSIPI_DB2, ("aha_scsipi_request\n"));
   1166  1.31    bouyer 
   1167  1.31    bouyer 		/* Get a CCB to use. */
   1168  1.31    bouyer 		ccb = aha_get_ccb(sc);
   1169  1.31    bouyer #ifdef DIAGNOSTIC
   1170  1.10   thorpej 		/*
   1171  1.31    bouyer 		 * This should never happen as we track the resources
   1172  1.31    bouyer 		 * in the mid-layer.
   1173  1.10   thorpej 		 */
   1174  1.31    bouyer 		if (ccb == NULL) {
   1175  1.31    bouyer 			scsipi_printaddr(periph);
   1176  1.31    bouyer 			printf("unable to allocate ccb\n");
   1177  1.31    bouyer 			panic("aha_scsipi_request");
   1178  1.10   thorpej 		}
   1179  1.31    bouyer #endif
   1180  1.31    bouyer 
   1181  1.31    bouyer 		ccb->xs = xs;
   1182  1.31    bouyer 		ccb->timeout = xs->timeout;
   1183  1.10   thorpej 
   1184  1.10   thorpej 		/*
   1185  1.31    bouyer 		 * Put all the arguments for the xfer in the ccb
   1186  1.10   thorpej 		 */
   1187  1.31    bouyer 		if (flags & XS_CTL_RESET) {
   1188  1.31    bouyer 			ccb->opcode = AHA_RESET_CCB;
   1189  1.31    bouyer 			ccb->scsi_cmd_length = 0;
   1190  1.31    bouyer 		} else {
   1191  1.31    bouyer 			/* can't use S/G if zero length */
   1192  1.31    bouyer 			ccb->opcode = (xs->datalen ? AHA_INIT_SCAT_GATH_CCB
   1193  1.31    bouyer 						   : AHA_INITIATOR_CCB);
   1194  1.31    bouyer 			bcopy(xs->cmd, &ccb->scsi_cmd,
   1195  1.31    bouyer 			    ccb->scsi_cmd_length = xs->cmdlen);
   1196  1.10   thorpej 		}
   1197  1.10   thorpej 
   1198  1.31    bouyer 		if (xs->datalen) {
   1199  1.31    bouyer 			/*
   1200  1.31    bouyer 			 * Map the DMA transfer.
   1201  1.31    bouyer 			 */
   1202  1.31    bouyer #ifdef TFS
   1203  1.31    bouyer 			if (flags & XS_CTL_DATA_UIO) {
   1204  1.31    bouyer 				error = bus_dmamap_load_uio(dmat,
   1205  1.31    bouyer 				    ccb->dmamap_xfer, (struct uio *)xs->data,
   1206  1.31    bouyer 				    ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
   1207  1.31    bouyer 				     BUS_DMA_WAITOK) | BUS_DMA_STREAMING);
   1208  1.31    bouyer 			} else
   1209  1.31    bouyer #endif
   1210  1.31    bouyer 			{
   1211  1.31    bouyer 				error = bus_dmamap_load(dmat,
   1212  1.31    bouyer 				    ccb->dmamap_xfer, xs->data, xs->datalen,
   1213  1.31    bouyer 				    NULL,
   1214  1.31    bouyer 				    ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
   1215  1.31    bouyer 				     BUS_DMA_WAITOK) | BUS_DMA_STREAMING);
   1216  1.31    bouyer 			}
   1217  1.31    bouyer 
   1218  1.31    bouyer 			switch (error) {
   1219  1.31    bouyer 			case 0:
   1220  1.31    bouyer 				break;
   1221  1.10   thorpej 
   1222  1.31    bouyer 			case ENOMEM:
   1223  1.31    bouyer 			case EAGAIN:
   1224  1.31    bouyer 				xs->error = XS_RESOURCE_SHORTAGE;
   1225  1.31    bouyer 				goto out_bad;
   1226  1.10   thorpej 
   1227  1.31    bouyer 			default:
   1228  1.31    bouyer 				xs->error = XS_DRIVER_STUFFUP;
   1229  1.31    bouyer 				if (error == EFBIG) {
   1230  1.31    bouyer 					printf("%s: aha_scsi_cmd, more than %d"
   1231  1.31    bouyer 					    " dma segments\n",
   1232  1.31    bouyer 					    sc->sc_dev.dv_xname, AHA_NSEG);
   1233  1.31    bouyer 				} else {
   1234  1.31    bouyer 					printf("%s: error %d loading DMA map\n",
   1235  1.31    bouyer 					    sc->sc_dev.dv_xname, error);
   1236  1.31    bouyer 				}
   1237  1.31    bouyer out_bad:
   1238  1.31    bouyer 				aha_free_ccb(sc, ccb);
   1239  1.31    bouyer 				scsipi_done(xs);
   1240  1.31    bouyer 				return;
   1241  1.31    bouyer 			}
   1242   1.1   mycroft 
   1243  1.31    bouyer 			bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
   1244  1.31    bouyer 			    ccb->dmamap_xfer->dm_mapsize,
   1245  1.31    bouyer 			    (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
   1246  1.31    bouyer 			    BUS_DMASYNC_PREWRITE);
   1247   1.1   mycroft 
   1248  1.31    bouyer 			/*
   1249  1.31    bouyer 			 * Load the hardware scatter/gather map with the
   1250  1.31    bouyer 			 * contents of the DMA map.
   1251  1.31    bouyer 			 */
   1252  1.31    bouyer 			for (seg = 0; seg < ccb->dmamap_xfer->dm_nsegs; seg++) {
   1253  1.31    bouyer 				ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_addr,
   1254  1.31    bouyer 				    ccb->scat_gath[seg].seg_addr);
   1255  1.31    bouyer 				ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_len,
   1256  1.31    bouyer 				    ccb->scat_gath[seg].seg_len);
   1257   1.1   mycroft 			}
   1258  1.31    bouyer 
   1259  1.31    bouyer 			ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
   1260  1.31    bouyer 			    AHA_CCB_OFF(ccb) +
   1261  1.31    bouyer 			    offsetof(struct aha_ccb, scat_gath),
   1262  1.31    bouyer 			    ccb->data_addr);
   1263  1.31    bouyer 			ltophys(ccb->dmamap_xfer->dm_nsegs *
   1264  1.31    bouyer 			    sizeof(struct aha_scat_gath), ccb->data_length);
   1265  1.31    bouyer 		} else {
   1266  1.31    bouyer 			/*
   1267  1.31    bouyer 			 * No data xfer, use non S/G values.
   1268  1.31    bouyer 			 */
   1269  1.31    bouyer 			ltophys(0, ccb->data_addr);
   1270  1.31    bouyer 			ltophys(0, ccb->data_length);
   1271   1.1   mycroft 		}
   1272   1.5   thorpej 
   1273  1.31    bouyer 		ccb->data_out = 0;
   1274  1.31    bouyer 		ccb->data_in = 0;
   1275  1.31    bouyer 		ccb->target = periph->periph_target;
   1276  1.31    bouyer 		ccb->lun = periph->periph_lun;
   1277  1.31    bouyer 		ccb->req_sense_length = sizeof(ccb->scsi_sense);
   1278  1.31    bouyer 		ccb->host_stat = 0x00;
   1279  1.31    bouyer 		ccb->target_stat = 0x00;
   1280  1.31    bouyer 		ccb->link_id = 0;
   1281  1.31    bouyer 		ltophys(0, ccb->link_addr);
   1282   1.5   thorpej 
   1283  1.31    bouyer 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
   1284  1.31    bouyer 		    AHA_CCB_OFF(ccb), sizeof(struct aha_ccb),
   1285  1.31    bouyer 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1286   1.5   thorpej 
   1287  1.31    bouyer 		s = splbio();
   1288  1.31    bouyer 		aha_queue_ccb(sc, ccb);
   1289  1.31    bouyer 		splx(s);
   1290   1.1   mycroft 
   1291  1.31    bouyer 		SC_DEBUG(periph, SCSIPI_DB3, ("cmd_sent\n"));
   1292  1.31    bouyer 		if ((flags & XS_CTL_POLL) == 0)
   1293  1.31    bouyer 			return;
   1294  1.15   thorpej 
   1295  1.31    bouyer 		/* Not allowed to use interrupts, poll for completion. */
   1296  1.31    bouyer 		if (aha_poll(sc, xs, ccb->timeout)) {
   1297  1.31    bouyer 			aha_timeout(ccb);
   1298  1.31    bouyer 			if (aha_poll(sc, xs, ccb->timeout))
   1299  1.31    bouyer 				aha_timeout(ccb);
   1300  1.31    bouyer 		}
   1301  1.31    bouyer 		return;
   1302   1.1   mycroft 
   1303  1.31    bouyer 	case ADAPTER_REQ_GROW_RESOURCES:
   1304  1.31    bouyer 		/* XXX Not supported. */
   1305  1.31    bouyer 		return;
   1306   1.1   mycroft 
   1307  1.31    bouyer 	case ADAPTER_REQ_SET_XFER_MODE:
   1308  1.31    bouyer 		/*
   1309  1.31    bouyer 		 * Can't really do this on the Adaptec; it has
   1310  1.31    bouyer 		 * its own config mechanism, but we do know how
   1311  1.31    bouyer 		 * to query what the firmware negotiated.
   1312  1.31    bouyer 		 */
   1313  1.31    bouyer 		/* XXX XXX XXX */
   1314  1.31    bouyer 		return;
   1315   1.1   mycroft 	}
   1316   1.1   mycroft }
   1317   1.1   mycroft 
   1318   1.1   mycroft /*
   1319   1.1   mycroft  * Poll a particular unit, looking for a particular xs
   1320   1.1   mycroft  */
   1321   1.1   mycroft int
   1322   1.1   mycroft aha_poll(sc, xs, count)
   1323   1.1   mycroft 	struct aha_softc *sc;
   1324   1.7    bouyer 	struct scsipi_xfer *xs;
   1325   1.1   mycroft 	int count;
   1326   1.1   mycroft {
   1327   1.1   mycroft 	bus_space_tag_t iot = sc->sc_iot;
   1328   1.1   mycroft 	bus_space_handle_t ioh = sc->sc_ioh;
   1329   1.1   mycroft 
   1330   1.1   mycroft 	/* timeouts are in msec, so we loop in 1000 usec cycles */
   1331   1.1   mycroft 	while (count) {
   1332   1.1   mycroft 		/*
   1333   1.1   mycroft 		 * If we had interrupts enabled, would we
   1334   1.1   mycroft 		 * have got an interrupt?
   1335   1.1   mycroft 		 */
   1336   1.1   mycroft 		if (bus_space_read_1(iot, ioh, AHA_INTR_PORT) & AHA_INTR_ANYINTR)
   1337   1.1   mycroft 			aha_intr(sc);
   1338  1.24   thorpej 		if (xs->xs_status & XS_STS_DONE)
   1339   1.1   mycroft 			return (0);
   1340   1.1   mycroft 		delay(1000);	/* only happens in boot so ok */
   1341   1.1   mycroft 		count--;
   1342   1.1   mycroft 	}
   1343   1.1   mycroft 	return (1);
   1344   1.1   mycroft }
   1345   1.1   mycroft 
   1346   1.1   mycroft void
   1347   1.1   mycroft aha_timeout(arg)
   1348   1.1   mycroft 	void *arg;
   1349   1.1   mycroft {
   1350   1.1   mycroft 	struct aha_ccb *ccb = arg;
   1351   1.7    bouyer 	struct scsipi_xfer *xs = ccb->xs;
   1352  1.31    bouyer 	struct scsipi_periph *periph = xs->xs_periph;
   1353  1.31    bouyer 	struct aha_softc *sc =
   1354  1.31    bouyer 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
   1355   1.1   mycroft 	int s;
   1356   1.1   mycroft 
   1357  1.31    bouyer 	scsipi_printaddr(periph);
   1358   1.1   mycroft 	printf("timed out");
   1359   1.1   mycroft 
   1360   1.1   mycroft 	s = splbio();
   1361   1.1   mycroft 
   1362   1.1   mycroft #ifdef AHADIAG
   1363   1.1   mycroft 	/*
   1364   1.1   mycroft 	 * If The ccb's mbx is not free, then the board has gone south?
   1365   1.1   mycroft 	 */
   1366   1.1   mycroft 	aha_collect_mbo(sc);
   1367   1.1   mycroft 	if (ccb->flags & CCB_SENDING) {
   1368   1.1   mycroft 		printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
   1369   1.1   mycroft 		Debugger();
   1370   1.1   mycroft 	}
   1371   1.1   mycroft #endif
   1372   1.1   mycroft 
   1373   1.1   mycroft 	/*
   1374   1.1   mycroft 	 * If it has been through before, then
   1375   1.1   mycroft 	 * a previous abort has failed, don't
   1376   1.1   mycroft 	 * try abort again
   1377   1.1   mycroft 	 */
   1378   1.1   mycroft 	if (ccb->flags & CCB_ABORT) {
   1379   1.1   mycroft 		/* abort timed out */
   1380   1.1   mycroft 		printf(" AGAIN\n");
   1381   1.1   mycroft 		/* XXX Must reset! */
   1382   1.1   mycroft 	} else {
   1383   1.1   mycroft 		/* abort the operation that has timed out */
   1384   1.1   mycroft 		printf("\n");
   1385   1.1   mycroft 		ccb->xs->error = XS_TIMEOUT;
   1386   1.1   mycroft 		ccb->timeout = AHA_ABORT_TIMEOUT;
   1387   1.1   mycroft 		ccb->flags |= CCB_ABORT;
   1388   1.1   mycroft 		aha_queue_ccb(sc, ccb);
   1389   1.1   mycroft 	}
   1390   1.1   mycroft 
   1391   1.1   mycroft 	splx(s);
   1392   1.1   mycroft }
   1393