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