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