Home | History | Annotate | Line # | Download | only in pci
amr.c revision 1.28
      1 /*	$NetBSD: amr.c,v 1.28 2005/08/25 18:35:39 drochner Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 1999,2000 Michael Smith
     41  * Copyright (c) 2000 BSDi
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  * from FreeBSD: amr_pci.c,v 1.5 2000/08/30 07:52:40 msmith Exp
     66  * from FreeBSD: amr.c,v 1.16 2000/08/30 07:52:40 msmith Exp
     67  */
     68 
     69 /*
     70  * Driver for AMI RAID controllers.
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: amr.c,v 1.28 2005/08/25 18:35:39 drochner Exp $");
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/kernel.h>
     79 #include <sys/device.h>
     80 #include <sys/queue.h>
     81 #include <sys/proc.h>
     82 #include <sys/buf.h>
     83 #include <sys/malloc.h>
     84 #include <sys/kthread.h>
     85 
     86 #include <uvm/uvm_extern.h>
     87 
     88 #include <machine/endian.h>
     89 #include <machine/bus.h>
     90 
     91 #include <dev/pci/pcidevs.h>
     92 #include <dev/pci/pcivar.h>
     93 #include <dev/pci/amrreg.h>
     94 #include <dev/pci/amrvar.h>
     95 
     96 #include "locators.h"
     97 
     98 static void	amr_attach(struct device *, struct device *, void *);
     99 static void	amr_ccb_dump(struct amr_softc *, struct amr_ccb *);
    100 static void	*amr_enquire(struct amr_softc *, u_int8_t, u_int8_t, u_int8_t,
    101 			     void *);
    102 static int	amr_init(struct amr_softc *, const char *,
    103 			 struct pci_attach_args *pa);
    104 static int	amr_intr(void *);
    105 static int	amr_match(struct device *, struct cfdata *, void *);
    106 static int	amr_print(void *, const char *);
    107 static void	amr_shutdown(void *);
    108 static int	amr_submatch(struct device *, struct cfdata *,
    109 			     const locdesc_t *, void *);
    110 static void	amr_teardown(struct amr_softc *);
    111 static void	amr_thread(void *);
    112 static void	amr_thread_create(void *);
    113 
    114 static int	amr_quartz_get_work(struct amr_softc *,
    115 				    struct amr_mailbox_resp *);
    116 static int	amr_quartz_submit(struct amr_softc *, struct amr_ccb *);
    117 static int	amr_std_get_work(struct amr_softc *, struct amr_mailbox_resp *);
    118 static int	amr_std_submit(struct amr_softc *, struct amr_ccb *);
    119 
    120 CFATTACH_DECL(amr, sizeof(struct amr_softc),
    121     amr_match, amr_attach, NULL, NULL);
    122 
    123 #define AT_QUARTZ	0x01	/* `Quartz' chipset */
    124 #define	AT_SIG		0x02	/* Check for signature */
    125 
    126 struct amr_pci_type {
    127 	u_short	apt_vendor;
    128 	u_short	apt_product;
    129 	u_short	apt_flags;
    130 } static const amr_pci_type[] = {
    131 	{ PCI_VENDOR_AMI,   PCI_PRODUCT_AMI_MEGARAID,  0 },
    132 	{ PCI_VENDOR_AMI,   PCI_PRODUCT_AMI_MEGARAID2, 0 },
    133 	{ PCI_VENDOR_AMI,   PCI_PRODUCT_AMI_MEGARAID3, AT_QUARTZ },
    134 	{ PCI_VENDOR_SYMBIOS, PCI_PRODUCT_AMI_MEGARAID3, AT_QUARTZ },
    135 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_AMI_MEGARAID3, AT_QUARTZ | AT_SIG },
    136 	{ PCI_VENDOR_DELL,  PCI_PRODUCT_DELL_PERC_4DI, AT_QUARTZ },
    137 	{ PCI_VENDOR_DELL,  PCI_PRODUCT_DELL_PERC_4DI_2, AT_QUARTZ },
    138 	{ PCI_VENDOR_DELL,  PCI_PRODUCT_DELL_PERC_4ESI, AT_QUARTZ },
    139 	{ PCI_VENDOR_SYMBIOS,  PCI_PRODUCT_SYMBIOS_PERC_4SC, AT_QUARTZ },
    140 };
    141 
    142 struct amr_typestr {
    143 	const char	*at_str;
    144 	int		at_sig;
    145 } static const amr_typestr[] = {
    146 	{ "Series 431",			AMR_SIG_431 },
    147 	{ "Series 438",			AMR_SIG_438 },
    148 	{ "Series 466",			AMR_SIG_466 },
    149 	{ "Series 467",			AMR_SIG_467 },
    150 	{ "Series 490",			AMR_SIG_490 },
    151 	{ "Series 762",			AMR_SIG_762 },
    152 	{ "HP NetRAID (T5)",		AMR_SIG_T5 },
    153 	{ "HP NetRAID (T7)",		AMR_SIG_T7 },
    154 };
    155 
    156 struct {
    157 	const char	*ds_descr;
    158 	int	ds_happy;
    159 } static const amr_dstate[] = {
    160 	{ "offline",	0 },
    161 	{ "degraded",	1 },
    162 	{ "optimal",	1 },
    163 	{ "online",	1 },
    164 	{ "failed",	0 },
    165 	{ "rebuilding",	1 },
    166 	{ "hotspare",	0 },
    167 };
    168 
    169 static void	*amr_sdh;
    170 
    171 static int	amr_max_segs;
    172 int		amr_max_xfer;
    173 
    174 static inline u_int8_t
    175 amr_inb(struct amr_softc *amr, int off)
    176 {
    177 
    178 	bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 1,
    179 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
    180 	return (bus_space_read_1(amr->amr_iot, amr->amr_ioh, off));
    181 }
    182 
    183 static inline u_int32_t
    184 amr_inl(struct amr_softc *amr, int off)
    185 {
    186 
    187 	bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 4,
    188 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
    189 	return (bus_space_read_4(amr->amr_iot, amr->amr_ioh, off));
    190 }
    191 
    192 static inline void
    193 amr_outb(struct amr_softc *amr, int off, u_int8_t val)
    194 {
    195 
    196 	bus_space_write_1(amr->amr_iot, amr->amr_ioh, off, val);
    197 	bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 1,
    198 	    BUS_SPACE_BARRIER_WRITE);
    199 }
    200 
    201 static inline void
    202 amr_outl(struct amr_softc *amr, int off, u_int32_t val)
    203 {
    204 
    205 	bus_space_write_4(amr->amr_iot, amr->amr_ioh, off, val);
    206 	bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 4,
    207 	    BUS_SPACE_BARRIER_WRITE);
    208 }
    209 
    210 /*
    211  * Match a supported device.
    212  */
    213 static int
    214 amr_match(struct device *parent, struct cfdata *match, void *aux)
    215 {
    216 	struct pci_attach_args *pa;
    217 	pcireg_t s;
    218 	int i;
    219 
    220 	pa = (struct pci_attach_args *)aux;
    221 
    222 	/*
    223 	 * Don't match the device if it's operating in I2O mode.  In this
    224 	 * case it should be handled by the `iop' driver.
    225 	 */
    226 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_I2O)
    227 		return (0);
    228 
    229 	for (i = 0; i < sizeof(amr_pci_type) / sizeof(amr_pci_type[0]); i++)
    230 		if (PCI_VENDOR(pa->pa_id) == amr_pci_type[i].apt_vendor &&
    231 		    PCI_PRODUCT(pa->pa_id) == amr_pci_type[i].apt_product)
    232 		    	break;
    233 
    234 	if (i == sizeof(amr_pci_type) / sizeof(amr_pci_type[0]))
    235 		return (0);
    236 
    237 	if ((amr_pci_type[i].apt_flags & AT_SIG) == 0)
    238 		return (1);
    239 
    240 	s = pci_conf_read(pa->pa_pc, pa->pa_tag, AMR_QUARTZ_SIG_REG) & 0xffff;
    241 	return (s == AMR_QUARTZ_SIG0 || s == AMR_QUARTZ_SIG1);
    242 }
    243 
    244 /*
    245  * Attach a supported device.
    246  */
    247 static void
    248 amr_attach(struct device *parent, struct device *self, void *aux)
    249 {
    250 	struct pci_attach_args *pa;
    251 	struct amr_attach_args amra;
    252 	const struct amr_pci_type *apt;
    253 	struct amr_softc *amr;
    254 	pci_chipset_tag_t pc;
    255 	pci_intr_handle_t ih;
    256 	const char *intrstr;
    257 	pcireg_t reg;
    258 	int rseg, i, j, size, rv, memreg, ioreg;
    259         struct amr_ccb *ac;
    260 	int locs[AMRCF_NLOCS];
    261 
    262 	aprint_naive(": RAID controller\n");
    263 
    264 	amr = (struct amr_softc *)self;
    265 	pa = (struct pci_attach_args *)aux;
    266 	pc = pa->pa_pc;
    267 
    268 	for (i = 0; i < sizeof(amr_pci_type) / sizeof(amr_pci_type[0]); i++)
    269 		if (PCI_VENDOR(pa->pa_id) == amr_pci_type[i].apt_vendor &&
    270 		    PCI_PRODUCT(pa->pa_id) == amr_pci_type[i].apt_product)
    271 			break;
    272 	apt = amr_pci_type + i;
    273 
    274 	memreg = ioreg = 0;
    275 	for (i = 0x10; i <= 0x14; i += 4) {
    276 		reg = pci_conf_read(pc, pa->pa_tag, i);
    277 		switch (PCI_MAPREG_TYPE(reg)) {
    278 		case PCI_MAPREG_TYPE_MEM:
    279 			if (PCI_MAPREG_MEM_SIZE(reg) != 0)
    280 				memreg = i;
    281 			break;
    282 		case PCI_MAPREG_TYPE_IO:
    283 			if (PCI_MAPREG_IO_SIZE(reg) != 0)
    284 				ioreg = i;
    285 			break;
    286 
    287 		}
    288 	}
    289 
    290 	if (memreg && pci_mapreg_map(pa, memreg, PCI_MAPREG_TYPE_MEM, 0,
    291 	    &amr->amr_iot, &amr->amr_ioh, NULL, &amr->amr_ios) == 0)
    292 		;
    293 	else if (ioreg && pci_mapreg_map(pa, ioreg, PCI_MAPREG_TYPE_IO, 0,
    294 	    &amr->amr_iot, &amr->amr_ioh, NULL, &amr->amr_ios) == 0)
    295 		;
    296 	else {
    297 		aprint_error("can't map control registers\n");
    298 		amr_teardown(amr);
    299 		return;
    300 	}
    301 
    302 	amr->amr_flags |= AMRF_PCI_REGS;
    303 	amr->amr_dmat = pa->pa_dmat;
    304 	amr->amr_pc = pa->pa_pc;
    305 
    306 	/* Enable the device. */
    307 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    308 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    309 	    reg | PCI_COMMAND_MASTER_ENABLE);
    310 
    311 	/* Map and establish the interrupt. */
    312 	if (pci_intr_map(pa, &ih)) {
    313 		aprint_error("can't map interrupt\n");
    314 		amr_teardown(amr);
    315 		return;
    316 	}
    317 	intrstr = pci_intr_string(pc, ih);
    318 	amr->amr_ih = pci_intr_establish(pc, ih, IPL_BIO, amr_intr, amr);
    319 	if (amr->amr_ih == NULL) {
    320 		aprint_error("can't establish interrupt");
    321 		if (intrstr != NULL)
    322 			aprint_normal(" at %s", intrstr);
    323 		aprint_normal("\n");
    324 		amr_teardown(amr);
    325 		return;
    326 	}
    327 	amr->amr_flags |= AMRF_PCI_INTR;
    328 
    329 	/*
    330 	 * Allocate space for the mailbox and S/G lists.  Some controllers
    331 	 * don't like S/G lists to be located below 0x2000, so we allocate
    332 	 * enough slop to enable us to compensate.
    333 	 *
    334 	 * The standard mailbox structure needs to be aligned on a 16-byte
    335 	 * boundary.  The 64-bit mailbox has one extra field, 4 bytes in
    336 	 * size, which preceeds the standard mailbox.
    337 	 */
    338 	size = AMR_SGL_SIZE * AMR_MAX_CMDS + 0x2000;
    339 	amr->amr_dmasize = size;
    340 
    341 	if ((rv = bus_dmamem_alloc(amr->amr_dmat, size, PAGE_SIZE, 0,
    342 	    &amr->amr_dmaseg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    343 		aprint_error("%s: unable to allocate buffer, rv = %d\n",
    344 		    amr->amr_dv.dv_xname, rv);
    345 		amr_teardown(amr);
    346 		return;
    347 	}
    348 	amr->amr_flags |= AMRF_DMA_ALLOC;
    349 
    350 	if ((rv = bus_dmamem_map(amr->amr_dmat, &amr->amr_dmaseg, rseg, size,
    351 	    (caddr_t *)&amr->amr_mbox,
    352 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    353 		aprint_error("%s: unable to map buffer, rv = %d\n",
    354 		    amr->amr_dv.dv_xname, rv);
    355 		amr_teardown(amr);
    356 		return;
    357 	}
    358 	amr->amr_flags |= AMRF_DMA_MAP;
    359 
    360 	if ((rv = bus_dmamap_create(amr->amr_dmat, size, 1, size, 0,
    361 	    BUS_DMA_NOWAIT, &amr->amr_dmamap)) != 0) {
    362 		aprint_error("%s: unable to create buffer DMA map, rv = %d\n",
    363 		    amr->amr_dv.dv_xname, rv);
    364 		amr_teardown(amr);
    365 		return;
    366 	}
    367 	amr->amr_flags |= AMRF_DMA_CREATE;
    368 
    369 	if ((rv = bus_dmamap_load(amr->amr_dmat, amr->amr_dmamap,
    370 	    amr->amr_mbox, size, NULL, BUS_DMA_NOWAIT)) != 0) {
    371 		aprint_error("%s: unable to load buffer DMA map, rv = %d\n",
    372 		    amr->amr_dv.dv_xname, rv);
    373 		amr_teardown(amr);
    374 		return;
    375 	}
    376 	amr->amr_flags |= AMRF_DMA_LOAD;
    377 
    378 	memset(amr->amr_mbox, 0, size);
    379 
    380 	amr->amr_mbox_paddr = amr->amr_dmamap->dm_segs[0].ds_addr;
    381 	amr->amr_sgls_paddr = (amr->amr_mbox_paddr + 0x1fff) & ~0x1fff;
    382 	amr->amr_sgls = (struct amr_sgentry *)((caddr_t)amr->amr_mbox +
    383 	    amr->amr_sgls_paddr - amr->amr_dmamap->dm_segs[0].ds_addr);
    384 
    385 	/*
    386 	 * Allocate and initalise the command control blocks.
    387 	 */
    388 	ac = malloc(sizeof(*ac) * AMR_MAX_CMDS, M_DEVBUF, M_NOWAIT | M_ZERO);
    389 	amr->amr_ccbs = ac;
    390 	SLIST_INIT(&amr->amr_ccb_freelist);
    391 	TAILQ_INIT(&amr->amr_ccb_active);
    392 	amr->amr_flags |= AMRF_CCBS;
    393 
    394 	if (amr_max_xfer == 0) {
    395 		amr_max_xfer = min(((AMR_MAX_SEGS - 1) * PAGE_SIZE), MAXPHYS);
    396 		amr_max_segs = (amr_max_xfer + (PAGE_SIZE * 2) - 1) / PAGE_SIZE;
    397 	}
    398 
    399 	for (i = 0; i < AMR_MAX_CMDS; i++, ac++) {
    400 		rv = bus_dmamap_create(amr->amr_dmat, amr_max_xfer,
    401 		    amr_max_segs, amr_max_xfer, 0,
    402 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ac->ac_xfer_map);
    403 		if (rv != 0)
    404 			break;
    405 
    406 		ac->ac_ident = i;
    407 		amr_ccb_free(amr, ac);
    408 	}
    409 	if (i != AMR_MAX_CMDS) {
    410 		aprint_error("%s: memory exhausted\n", amr->amr_dv.dv_xname);
    411 		amr_teardown(amr);
    412 		return;
    413 	}
    414 
    415 	/*
    416 	 * Take care of model-specific tasks.
    417 	 */
    418 	if ((apt->apt_flags & AT_QUARTZ) != 0) {
    419 		amr->amr_submit = amr_quartz_submit;
    420 		amr->amr_get_work = amr_quartz_get_work;
    421 	} else {
    422 		amr->amr_submit = amr_std_submit;
    423 		amr->amr_get_work = amr_std_get_work;
    424 
    425 		/* Notify the controller of the mailbox location. */
    426 		amr_outl(amr, AMR_SREG_MBOX, (u_int32_t)amr->amr_mbox_paddr + 16);
    427 		amr_outb(amr, AMR_SREG_MBOX_ENABLE, AMR_SMBOX_ENABLE_ADDR);
    428 
    429 		/* Clear outstanding interrupts and enable interrupts. */
    430 		amr_outb(amr, AMR_SREG_CMD, AMR_SCMD_ACKINTR);
    431 		amr_outb(amr, AMR_SREG_TOGL,
    432 		    amr_inb(amr, AMR_SREG_TOGL) | AMR_STOGL_ENABLE);
    433 	}
    434 
    435 	/*
    436 	 * Retrieve parameters, and tell the world about us.
    437 	 */
    438 	amr->amr_enqbuf = malloc(AMR_ENQUIRY_BUFSIZE, M_DEVBUF, M_NOWAIT);
    439 	amr->amr_flags |= AMRF_ENQBUF;
    440 	amr->amr_maxqueuecnt = i;
    441 	aprint_normal(": AMI RAID ");
    442 	if (amr_init(amr, intrstr, pa) != 0) {
    443 		amr_teardown(amr);
    444 		return;
    445 	}
    446 
    447 	/*
    448 	 * Cap the maximum number of outstanding commands.  AMI's Linux
    449 	 * driver doesn't trust the controller's reported value, and lockups
    450 	 * have been seen when we do.
    451 	 */
    452 	amr->amr_maxqueuecnt = min(amr->amr_maxqueuecnt, AMR_MAX_CMDS);
    453 	if (amr->amr_maxqueuecnt > i)
    454 		amr->amr_maxqueuecnt = i;
    455 
    456 	/* Set our `shutdownhook' before we start any device activity. */
    457 	if (amr_sdh == NULL)
    458 		amr_sdh = shutdownhook_establish(amr_shutdown, NULL);
    459 
    460 	/* Attach sub-devices. */
    461 	for (j = 0; j < amr->amr_numdrives; j++) {
    462 		if (amr->amr_drive[j].al_size == 0)
    463 			continue;
    464 		amra.amra_unit = j;
    465 
    466 		locs[AMRCF_UNIT] = j;
    467 
    468 		amr->amr_drive[j].al_dv = config_found_sm_loc(&amr->amr_dv,
    469 			"amr", locs, &amra, amr_print, amr_submatch);
    470 	}
    471 
    472 	SIMPLEQ_INIT(&amr->amr_ccb_queue);
    473 
    474 	/* XXX This doesn't work for newer boards yet. */
    475 	if ((apt->apt_flags & AT_QUARTZ) == 0)
    476 		kthread_create(amr_thread_create, amr);
    477 }
    478 
    479 /*
    480  * Free up resources.
    481  */
    482 static void
    483 amr_teardown(struct amr_softc *amr)
    484 {
    485 	struct amr_ccb *ac;
    486 	int fl;
    487 
    488 	fl = amr->amr_flags;
    489 
    490 	if ((fl & AMRF_THREAD) != 0) {
    491 		amr->amr_flags |= AMRF_THREAD_EXIT;
    492 		wakeup(amr_thread);
    493 		while ((amr->amr_flags & AMRF_THREAD_EXIT) != 0)
    494 			tsleep(&amr->amr_flags, PWAIT, "amrexit", 0);
    495 	}
    496 	if ((fl & AMRF_CCBS) != 0) {
    497 		SLIST_FOREACH(ac, &amr->amr_ccb_freelist, ac_chain.slist) {
    498 			bus_dmamap_destroy(amr->amr_dmat, ac->ac_xfer_map);
    499 		}
    500 		free(amr->amr_ccbs, M_DEVBUF);
    501 	}
    502 	if ((fl & AMRF_ENQBUF) != 0)
    503 		free(amr->amr_enqbuf, M_DEVBUF);
    504 	if ((fl & AMRF_DMA_LOAD) != 0)
    505 		bus_dmamap_unload(amr->amr_dmat, amr->amr_dmamap);
    506 	if ((fl & AMRF_DMA_MAP) != 0)
    507 		bus_dmamem_unmap(amr->amr_dmat, (caddr_t)amr->amr_mbox,
    508 		    amr->amr_dmasize);
    509 	if ((fl & AMRF_DMA_ALLOC) != 0)
    510 		bus_dmamem_free(amr->amr_dmat, &amr->amr_dmaseg, 1);
    511 	if ((fl & AMRF_DMA_CREATE) != 0)
    512 		bus_dmamap_destroy(amr->amr_dmat, amr->amr_dmamap);
    513 	if ((fl & AMRF_PCI_INTR) != 0)
    514 		pci_intr_disestablish(amr->amr_pc, amr->amr_ih);
    515 	if ((fl & AMRF_PCI_REGS) != 0)
    516 		bus_space_unmap(amr->amr_iot, amr->amr_ioh, amr->amr_ios);
    517 }
    518 
    519 /*
    520  * Print autoconfiguration message for a sub-device.
    521  */
    522 static int
    523 amr_print(void *aux, const char *pnp)
    524 {
    525 	struct amr_attach_args *amra;
    526 
    527 	amra = (struct amr_attach_args *)aux;
    528 
    529 	if (pnp != NULL)
    530 		aprint_normal("block device at %s", pnp);
    531 	aprint_normal(" unit %d", amra->amra_unit);
    532 	return (UNCONF);
    533 }
    534 
    535 /*
    536  * Match a sub-device.
    537  */
    538 static int
    539 amr_submatch(struct device *parent, struct cfdata *cf,
    540 	     const locdesc_t *locs, void *aux)
    541 {
    542 	struct amr_attach_args *amra;
    543 
    544 	amra = (struct amr_attach_args *)aux;
    545 
    546 	if (cf->cf_loc[AMRCF_UNIT] != AMRCF_UNIT_DEFAULT &&
    547 	    cf->cf_loc[AMRCF_UNIT] != locs[AMRCF_UNIT])
    548 		return (0);
    549 
    550 	return (config_match(parent, cf, aux));
    551 }
    552 
    553 /*
    554  * Retrieve operational parameters and describe the controller.
    555  */
    556 static int
    557 amr_init(struct amr_softc *amr, const char *intrstr,
    558 	 struct pci_attach_args *pa)
    559 {
    560 	struct amr_adapter_info *aa;
    561 	struct amr_prodinfo *ap;
    562 	struct amr_enquiry *ae;
    563 	struct amr_enquiry3 *aex;
    564 	const char *prodstr;
    565 	u_int i, sig, ishp;
    566 	char sbuf[64];
    567 
    568 	/*
    569 	 * Try to get 40LD product info, which tells us what the card is
    570 	 * labelled as.
    571 	 */
    572 	ap = amr_enquire(amr, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0,
    573 	    amr->amr_enqbuf);
    574 	if (ap != NULL) {
    575 		aprint_normal("<%.80s>\n", ap->ap_product);
    576 		if (intrstr != NULL)
    577 			aprint_normal("%s: interrupting at %s\n",
    578 			    amr->amr_dv.dv_xname, intrstr);
    579 		aprint_normal("%s: firmware %.16s, BIOS %.16s, %dMB RAM\n",
    580 		    amr->amr_dv.dv_xname, ap->ap_firmware, ap->ap_bios,
    581 		    le16toh(ap->ap_memsize));
    582 
    583 		amr->amr_maxqueuecnt = ap->ap_maxio;
    584 
    585 		/*
    586 		 * Fetch and record state of logical drives.
    587 		 */
    588 		aex = amr_enquire(amr, AMR_CMD_CONFIG, AMR_CONFIG_ENQ3,
    589 		    AMR_CONFIG_ENQ3_SOLICITED_FULL, amr->amr_enqbuf);
    590 		if (aex == NULL) {
    591 			aprint_error("%s ENQUIRY3 failed\n",
    592 			    amr->amr_dv.dv_xname);
    593 			return (-1);
    594 		}
    595 
    596 		if (aex->ae_numldrives > AMR_MAX_UNITS) {
    597 			aprint_error(
    598 			    "%s: adjust AMR_MAX_UNITS to %d (currently %d)"
    599 			    "\n", amr->amr_dv.dv_xname, AMR_MAX_UNITS,
    600 			    amr->amr_numdrives);
    601 			amr->amr_numdrives = AMR_MAX_UNITS;
    602 		} else
    603 			amr->amr_numdrives = aex->ae_numldrives;
    604 
    605 		for (i = 0; i < amr->amr_numdrives; i++) {
    606 			amr->amr_drive[i].al_size =
    607 			    le32toh(aex->ae_drivesize[i]);
    608 			amr->amr_drive[i].al_state = aex->ae_drivestate[i];
    609 			amr->amr_drive[i].al_properties = aex->ae_driveprop[i];
    610 		}
    611 
    612 		return (0);
    613 	}
    614 
    615 	/*
    616 	 * Try 8LD extended ENQUIRY to get the controller signature.  Once
    617 	 * found, search for a product description.
    618 	 */
    619 	ae = amr_enquire(amr, AMR_CMD_EXT_ENQUIRY2, 0, 0, amr->amr_enqbuf);
    620 	if (ae != NULL) {
    621 		i = 0;
    622 		sig = le32toh(ae->ae_signature);
    623 
    624 		while (i < sizeof(amr_typestr) / sizeof(amr_typestr[0])) {
    625 			if (amr_typestr[i].at_sig == sig)
    626 				break;
    627 			i++;
    628 		}
    629 		if (i == sizeof(amr_typestr) / sizeof(amr_typestr[0])) {
    630 			snprintf(sbuf, sizeof(sbuf),
    631 			    "unknown ENQUIRY2 sig (0x%08x)", sig);
    632 			prodstr = sbuf;
    633 		} else
    634 			prodstr = amr_typestr[i].at_str;
    635 	} else {
    636 		ae = amr_enquire(amr, AMR_CMD_ENQUIRY, 0, 0, amr->amr_enqbuf);
    637 		if (ae == NULL) {
    638 			aprint_error("%s: unsupported controller\n",
    639 			    amr->amr_dv.dv_xname);
    640 			return (-1);
    641 		}
    642 
    643 		switch (PCI_PRODUCT(pa->pa_id)) {
    644 		case PCI_PRODUCT_AMI_MEGARAID:
    645 			prodstr = "Series 428";
    646 			break;
    647 		case PCI_PRODUCT_AMI_MEGARAID2:
    648 			prodstr = "Series 434";
    649 			break;
    650 		default:
    651 			snprintf(sbuf, sizeof(sbuf), "unknown PCI dev (0x%04x)",
    652 			    PCI_PRODUCT(pa->pa_id));
    653 			prodstr = sbuf;
    654 			break;
    655 		}
    656 	}
    657 
    658 	/*
    659 	 * HP NetRaid controllers have a special encoding of the firmware
    660 	 * and BIOS versions.  The AMI version seems to have it as strings
    661 	 * whereas the HP version does it with a leading uppercase character
    662 	 * and two binary numbers.
    663 	*/
    664 	aa = &ae->ae_adapter;
    665 
    666 	if (aa->aa_firmware[2] >= 'A' && aa->aa_firmware[2] <= 'Z' &&
    667 	    aa->aa_firmware[1] <  ' ' && aa->aa_firmware[0] <  ' ' &&
    668 	    aa->aa_bios[2] >= 'A' && aa->aa_bios[2] <= 'Z' &&
    669 	    aa->aa_bios[1] <  ' ' && aa->aa_bios[0] <  ' ') {
    670 		if (le32toh(ae->ae_signature) == AMR_SIG_438) {
    671 			/* The AMI 438 is a NetRaid 3si in HP-land. */
    672 			prodstr = "HP NetRaid 3si";
    673 		}
    674 		ishp = 1;
    675 	} else
    676 		ishp = 0;
    677 
    678 	aprint_normal("<%s>\n", prodstr);
    679 	if (intrstr != NULL)
    680 		aprint_normal("%s: interrupting at %s\n", amr->amr_dv.dv_xname,
    681 		    intrstr);
    682 
    683 	if (ishp)
    684 		aprint_normal("%s: firmware <%c.%02d.%02d>, BIOS <%c.%02d.%02d>"
    685 		    ", %dMB RAM\n", amr->amr_dv.dv_xname, aa->aa_firmware[2],
    686 		     aa->aa_firmware[1], aa->aa_firmware[0], aa->aa_bios[2],
    687 		     aa->aa_bios[1], aa->aa_bios[0], aa->aa_memorysize);
    688 	else
    689 		aprint_normal("%s: firmware <%.4s>, BIOS <%.4s>, %dMB RAM\n",
    690 		    amr->amr_dv.dv_xname, aa->aa_firmware, aa->aa_bios,
    691 		    aa->aa_memorysize);
    692 
    693 	amr->amr_maxqueuecnt = aa->aa_maxio;
    694 
    695 	/*
    696 	 * Record state of logical drives.
    697 	 */
    698 	if (ae->ae_ldrv.al_numdrives > AMR_MAX_UNITS) {
    699 		aprint_error("%s: adjust AMR_MAX_UNITS to %d (currently %d)\n",
    700 		    amr->amr_dv.dv_xname, ae->ae_ldrv.al_numdrives,
    701 		    AMR_MAX_UNITS);
    702 		amr->amr_numdrives = AMR_MAX_UNITS;
    703 	} else
    704 		amr->amr_numdrives = ae->ae_ldrv.al_numdrives;
    705 
    706 	for (i = 0; i < AMR_MAX_UNITS; i++) {
    707 		amr->amr_drive[i].al_size = le32toh(ae->ae_ldrv.al_size[i]);
    708 		amr->amr_drive[i].al_state = ae->ae_ldrv.al_state[i];
    709 		amr->amr_drive[i].al_properties = ae->ae_ldrv.al_properties[i];
    710 	}
    711 
    712 	return (0);
    713 }
    714 
    715 /*
    716  * Flush the internal cache on each configured controller.  Called at
    717  * shutdown time.
    718  */
    719 static void
    720 amr_shutdown(void *cookie)
    721 {
    722         extern struct cfdriver amr_cd;
    723 	struct amr_softc *amr;
    724 	struct amr_ccb *ac;
    725 	int i, rv, s;
    726 
    727 	for (i = 0; i < amr_cd.cd_ndevs; i++) {
    728 		if ((amr = device_lookup(&amr_cd, i)) == NULL)
    729 			continue;
    730 
    731 		if ((rv = amr_ccb_alloc(amr, &ac)) == 0) {
    732 			ac->ac_cmd.mb_command = AMR_CMD_FLUSH;
    733 			s = splbio();
    734 			rv = amr_ccb_poll(amr, ac, 30000);
    735 			splx(s);
    736 			amr_ccb_free(amr, ac);
    737 		}
    738 		if (rv != 0)
    739 			printf("%s: unable to flush cache (%d)\n",
    740 			    amr->amr_dv.dv_xname, rv);
    741 	}
    742 }
    743 
    744 /*
    745  * Interrupt service routine.
    746  */
    747 static int
    748 amr_intr(void *cookie)
    749 {
    750 	struct amr_softc *amr;
    751 	struct amr_ccb *ac;
    752 	struct amr_mailbox_resp mbox;
    753 	u_int i, forus, idx;
    754 
    755 	amr = cookie;
    756 	forus = 0;
    757 
    758 	while ((*amr->amr_get_work)(amr, &mbox) == 0) {
    759 		/* Iterate over completed commands in this result. */
    760 		for (i = 0; i < mbox.mb_nstatus; i++) {
    761 			idx = mbox.mb_completed[i] - 1;
    762 			ac = amr->amr_ccbs + idx;
    763 
    764 			if (idx >= amr->amr_maxqueuecnt) {
    765 				printf("%s: bad status (bogus ID: %u=%u)\n",
    766 				    amr->amr_dv.dv_xname, i, idx);
    767 				continue;
    768 			}
    769 
    770 			if ((ac->ac_flags & AC_ACTIVE) == 0) {
    771 				printf("%s: bad status (not active; 0x04%x)\n",
    772 				    amr->amr_dv.dv_xname, ac->ac_flags);
    773 				continue;
    774 			}
    775 
    776 			ac->ac_status = mbox.mb_status;
    777 			ac->ac_flags = (ac->ac_flags & ~AC_ACTIVE) |
    778 			    AC_COMPLETE;
    779 			TAILQ_REMOVE(&amr->amr_ccb_active, ac, ac_chain.tailq);
    780 
    781 			if ((ac->ac_flags & AC_MOAN) != 0)
    782 				printf("%s: ccb %d completed\n",
    783 				    amr->amr_dv.dv_xname, ac->ac_ident);
    784 
    785 			/* Pass notification to upper layers. */
    786 			if (ac->ac_handler != NULL)
    787 				(*ac->ac_handler)(ac);
    788 			else
    789 				wakeup(ac);
    790 		}
    791 		forus = 1;
    792 	}
    793 
    794 	if (forus)
    795 		amr_ccb_enqueue(amr, NULL);
    796 
    797 	return (forus);
    798 }
    799 
    800 /*
    801  * Create the watchdog thread.
    802  */
    803 static void
    804 amr_thread_create(void *cookie)
    805 {
    806 	struct amr_softc *amr;
    807 	int rv;
    808 
    809 	amr = cookie;
    810 
    811 	if ((amr->amr_flags & AMRF_THREAD_EXIT) != 0) {
    812 		amr->amr_flags ^= AMRF_THREAD_EXIT;
    813 		wakeup(&amr->amr_flags);
    814 		return;
    815 	}
    816 
    817 	rv = kthread_create1(amr_thread, amr, &amr->amr_thread, "%s",
    818 	    amr->amr_dv.dv_xname);
    819  	if (rv != 0)
    820 		aprint_error("%s: unable to create thread (%d)",
    821  		    amr->amr_dv.dv_xname, rv);
    822  	else
    823  		amr->amr_flags |= AMRF_THREAD;
    824 }
    825 
    826 /*
    827  * Watchdog thread.
    828  */
    829 static void
    830 amr_thread(void *cookie)
    831 {
    832 	struct amr_softc *amr;
    833 	struct amr_ccb *ac;
    834 	struct amr_logdrive *al;
    835 	struct amr_enquiry *ae;
    836 	time_t curtime;
    837 	int rv, i, s;
    838 
    839 	amr = cookie;
    840 	ae = amr->amr_enqbuf;
    841 
    842 	for (;;) {
    843 		tsleep(amr_thread, PWAIT, "amrwdog", AMR_WDOG_TICKS);
    844 
    845 		if ((amr->amr_flags & AMRF_THREAD_EXIT) != 0) {
    846 			amr->amr_flags ^= AMRF_THREAD_EXIT;
    847 			wakeup(&amr->amr_flags);
    848 			kthread_exit(0);
    849 		}
    850 
    851 		s = splbio();
    852 		amr_intr(cookie);
    853 		curtime = (time_t)mono_time.tv_sec;
    854 		ac = TAILQ_FIRST(&amr->amr_ccb_active);
    855 		while (ac != NULL) {
    856 			if (ac->ac_start_time + AMR_TIMEOUT > curtime)
    857 				break;
    858 			if ((ac->ac_flags & AC_MOAN) == 0) {
    859 				printf("%s: ccb %d timed out; mailbox:\n",
    860 				    amr->amr_dv.dv_xname, ac->ac_ident);
    861 				amr_ccb_dump(amr, ac);
    862 				ac->ac_flags |= AC_MOAN;
    863 			}
    864 			ac = TAILQ_NEXT(ac, ac_chain.tailq);
    865 		}
    866 		splx(s);
    867 
    868 		if ((rv = amr_ccb_alloc(amr, &ac)) != 0) {
    869 			printf("%s: ccb_alloc failed (%d)\n",
    870  			    amr->amr_dv.dv_xname, rv);
    871 			continue;
    872 		}
    873 
    874 		ac->ac_cmd.mb_command = AMR_CMD_ENQUIRY;
    875 
    876 		rv = amr_ccb_map(amr, ac, amr->amr_enqbuf,
    877 		    AMR_ENQUIRY_BUFSIZE, 0);
    878 		if (rv != 0) {
    879 			printf("%s: ccb_map failed (%d)\n",
    880  			    amr->amr_dv.dv_xname, rv);
    881 			amr_ccb_free(amr, ac);
    882 			continue;
    883 		}
    884 
    885 		rv = amr_ccb_wait(amr, ac);
    886 		amr_ccb_unmap(amr, ac);
    887 		if (rv != 0) {
    888 			printf("%s: enquiry failed (st=%d)\n",
    889  			    amr->amr_dv.dv_xname, ac->ac_status);
    890 			continue;
    891 		}
    892 		amr_ccb_free(amr, ac);
    893 
    894 		al = amr->amr_drive;
    895 		for (i = 0; i < AMR_MAX_UNITS; i++, al++) {
    896 			if (al->al_dv == NULL)
    897 				continue;
    898 			if (al->al_state == ae->ae_ldrv.al_state[i])
    899 				continue;
    900 
    901 			printf("%s: state changed: %s -> %s\n",
    902 			    al->al_dv->dv_xname,
    903 			    amr_drive_state(al->al_state, NULL),
    904 			    amr_drive_state(ae->ae_ldrv.al_state[i], NULL));
    905 
    906 			al->al_state = ae->ae_ldrv.al_state[i];
    907 		}
    908 	}
    909 }
    910 
    911 /*
    912  * Return a text description of a logical drive's current state.
    913  */
    914 const char *
    915 amr_drive_state(int state, int *happy)
    916 {
    917 	const char *str;
    918 
    919 	state = AMR_DRV_CURSTATE(state);
    920 	if (state >= sizeof(amr_dstate) / sizeof(amr_dstate[0])) {
    921 		if (happy)
    922 			*happy = 1;
    923 		str = "status unknown";
    924 	} else {
    925 		if (happy)
    926 			*happy = amr_dstate[state].ds_happy;
    927 		str = amr_dstate[state].ds_descr;
    928 	}
    929 
    930 	return (str);
    931 }
    932 
    933 /*
    934  * Run a generic enquiry-style command.
    935  */
    936 static void *
    937 amr_enquire(struct amr_softc *amr, u_int8_t cmd, u_int8_t cmdsub,
    938 	    u_int8_t cmdqual, void *sbuf)
    939 {
    940 	struct amr_ccb *ac;
    941 	u_int8_t *mb;
    942 	int rv;
    943 
    944 	if (amr_ccb_alloc(amr, &ac) != 0)
    945 		return (NULL);
    946 
    947 	/* Build the command proper. */
    948 	mb = (u_int8_t *)&ac->ac_cmd;
    949 	mb[0] = cmd;
    950 	mb[2] = cmdsub;
    951 	mb[3] = cmdqual;
    952 
    953 	rv = amr_ccb_map(amr, ac, sbuf, AMR_ENQUIRY_BUFSIZE, 0);
    954 	if (rv == 0) {
    955 		rv = amr_ccb_poll(amr, ac, 2000);
    956 		amr_ccb_unmap(amr, ac);
    957 	}
    958 	amr_ccb_free(amr, ac);
    959 
    960 	return (rv ? NULL : sbuf);
    961 }
    962 
    963 /*
    964  * Allocate and initialise a CCB.
    965  */
    966 int
    967 amr_ccb_alloc(struct amr_softc *amr, struct amr_ccb **acp)
    968 {
    969 	int s;
    970 
    971 	s = splbio();
    972 	if ((*acp = SLIST_FIRST(&amr->amr_ccb_freelist)) == NULL) {
    973 		splx(s);
    974 		return (EAGAIN);
    975 	}
    976 	SLIST_REMOVE_HEAD(&amr->amr_ccb_freelist, ac_chain.slist);
    977 	splx(s);
    978 
    979 	return (0);
    980 }
    981 
    982 /*
    983  * Free a CCB.
    984  */
    985 void
    986 amr_ccb_free(struct amr_softc *amr, struct amr_ccb *ac)
    987 {
    988 	int s;
    989 
    990 	memset(&ac->ac_cmd, 0, sizeof(ac->ac_cmd));
    991 	ac->ac_cmd.mb_ident = ac->ac_ident + 1;
    992 	ac->ac_cmd.mb_busy = 1;
    993 	ac->ac_handler = NULL;
    994 	ac->ac_flags = 0;
    995 
    996 	s = splbio();
    997 	SLIST_INSERT_HEAD(&amr->amr_ccb_freelist, ac, ac_chain.slist);
    998 	splx(s);
    999 }
   1000 
   1001 /*
   1002  * If a CCB is specified, enqueue it.  Pull CCBs off the software queue in
   1003  * the order that they were enqueued and try to submit their command blocks
   1004  * to the controller for execution.
   1005  */
   1006 void
   1007 amr_ccb_enqueue(struct amr_softc *amr, struct amr_ccb *ac)
   1008 {
   1009 	int s;
   1010 
   1011 	s = splbio();
   1012 
   1013 	if (ac != NULL)
   1014 		SIMPLEQ_INSERT_TAIL(&amr->amr_ccb_queue, ac, ac_chain.simpleq);
   1015 
   1016 	while ((ac = SIMPLEQ_FIRST(&amr->amr_ccb_queue)) != NULL) {
   1017 		if ((*amr->amr_submit)(amr, ac) != 0)
   1018 			break;
   1019 		SIMPLEQ_REMOVE_HEAD(&amr->amr_ccb_queue, ac_chain.simpleq);
   1020 		TAILQ_INSERT_TAIL(&amr->amr_ccb_active, ac, ac_chain.tailq);
   1021 	}
   1022 
   1023 	splx(s);
   1024 }
   1025 
   1026 /*
   1027  * Map the specified CCB's data buffer onto the bus, and fill the
   1028  * scatter-gather list.
   1029  */
   1030 int
   1031 amr_ccb_map(struct amr_softc *amr, struct amr_ccb *ac, void *data, int size,
   1032 	    int out)
   1033 {
   1034 	struct amr_sgentry *sge;
   1035 	struct amr_mailbox_cmd *mb;
   1036 	int nsegs, i, rv, sgloff;
   1037 	bus_dmamap_t xfer;
   1038 
   1039 	xfer = ac->ac_xfer_map;
   1040 
   1041 	rv = bus_dmamap_load(amr->amr_dmat, xfer, data, size, NULL,
   1042 	    BUS_DMA_NOWAIT);
   1043 	if (rv != 0)
   1044 		return (rv);
   1045 
   1046 	mb = &ac->ac_cmd;
   1047 	ac->ac_xfer_size = size;
   1048 	ac->ac_flags |= (out ? AC_XFER_OUT : AC_XFER_IN);
   1049 	sgloff = AMR_SGL_SIZE * ac->ac_ident;
   1050 
   1051 	/* We don't need to use a scatter/gather list for just 1 segment. */
   1052 	nsegs = xfer->dm_nsegs;
   1053 	if (nsegs == 1) {
   1054 		mb->mb_nsgelem = 0;
   1055 		mb->mb_physaddr = htole32(xfer->dm_segs[0].ds_addr);
   1056 		ac->ac_flags |= AC_NOSGL;
   1057 	} else {
   1058 		mb->mb_nsgelem = nsegs;
   1059 		mb->mb_physaddr = htole32(amr->amr_sgls_paddr + sgloff);
   1060 
   1061 		sge = (struct amr_sgentry *)((caddr_t)amr->amr_sgls + sgloff);
   1062 		for (i = 0; i < nsegs; i++, sge++) {
   1063 			sge->sge_addr = htole32(xfer->dm_segs[i].ds_addr);
   1064 			sge->sge_count = htole32(xfer->dm_segs[i].ds_len);
   1065 		}
   1066 	}
   1067 
   1068 	bus_dmamap_sync(amr->amr_dmat, xfer, 0, ac->ac_xfer_size,
   1069 	    out ? BUS_DMASYNC_PREWRITE : BUS_DMASYNC_PREREAD);
   1070 
   1071 	if ((ac->ac_flags & AC_NOSGL) == 0)
   1072 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, sgloff,
   1073 		    AMR_SGL_SIZE, BUS_DMASYNC_PREWRITE);
   1074 
   1075 	return (0);
   1076 }
   1077 
   1078 /*
   1079  * Unmap the specified CCB's data buffer.
   1080  */
   1081 void
   1082 amr_ccb_unmap(struct amr_softc *amr, struct amr_ccb *ac)
   1083 {
   1084 
   1085 	if ((ac->ac_flags & AC_NOSGL) == 0)
   1086 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap,
   1087 		    AMR_SGL_SIZE * ac->ac_ident, AMR_SGL_SIZE,
   1088 		    BUS_DMASYNC_POSTWRITE);
   1089 	bus_dmamap_sync(amr->amr_dmat, ac->ac_xfer_map, 0, ac->ac_xfer_size,
   1090 	    (ac->ac_flags & AC_XFER_IN) != 0 ?
   1091 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1092 	bus_dmamap_unload(amr->amr_dmat, ac->ac_xfer_map);
   1093 }
   1094 
   1095 /*
   1096  * Submit a command to the controller and poll on completion.  Return
   1097  * non-zero on timeout or error.  Must be called with interrupts blocked.
   1098  */
   1099 int
   1100 amr_ccb_poll(struct amr_softc *amr, struct amr_ccb *ac, int timo)
   1101 {
   1102 	int rv;
   1103 
   1104 	if ((rv = (*amr->amr_submit)(amr, ac)) != 0)
   1105 		return (rv);
   1106 	TAILQ_INSERT_TAIL(&amr->amr_ccb_active, ac, ac_chain.tailq);
   1107 
   1108 	for (timo *= 10; timo != 0; timo--) {
   1109 		amr_intr(amr);
   1110 		if ((ac->ac_flags & AC_COMPLETE) != 0)
   1111 			break;
   1112 		DELAY(100);
   1113 	}
   1114 
   1115 	return (timo == 0 || ac->ac_status != 0 ? EIO : 0);
   1116 }
   1117 
   1118 /*
   1119  * Submit a command to the controller and sleep on completion.  Return
   1120  * non-zero on error.
   1121  */
   1122 int
   1123 amr_ccb_wait(struct amr_softc *amr, struct amr_ccb *ac)
   1124 {
   1125 	int s;
   1126 
   1127 	s = splbio();
   1128 	amr_ccb_enqueue(amr, ac);
   1129 	tsleep(ac, PRIBIO, "amrcmd", 0);
   1130 	splx(s);
   1131 
   1132 	return (ac->ac_status != 0 ? EIO : 0);
   1133 }
   1134 
   1135 #if 0
   1136 /*
   1137  * Wait for the mailbox to become available.
   1138  */
   1139 static int
   1140 amr_mbox_wait(struct amr_softc *amr)
   1141 {
   1142 	int timo;
   1143 
   1144 	for (timo = 10000; timo != 0; timo--) {
   1145 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1146 		    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
   1147 		if (amr->amr_mbox->mb_cmd.mb_busy == 0)
   1148 			break;
   1149 		DELAY(100);
   1150 	}
   1151 
   1152 	if (timo == 0)
   1153 		printf("%s: controller wedged\n", amr->amr_dv.dv_xname);
   1154 
   1155 	return (timo != 0 ? 0 : EAGAIN);
   1156 }
   1157 #endif
   1158 
   1159 /*
   1160  * Tell the controller that the mailbox contains a valid command.  Must be
   1161  * called with interrupts blocked.
   1162  */
   1163 static int
   1164 amr_quartz_submit(struct amr_softc *amr, struct amr_ccb *ac)
   1165 {
   1166 	u_int32_t v;
   1167 
   1168 	amr->amr_mbox->mb_poll = 0;
   1169 	amr->amr_mbox->mb_ack = 0;
   1170 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1171 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
   1172 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1173 	    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
   1174 	if (amr->amr_mbox->mb_cmd.mb_busy != 0)
   1175 		return (EAGAIN);
   1176 
   1177 	v = amr_inl(amr, AMR_QREG_IDB);
   1178 	if ((v & AMR_QIDB_SUBMIT) != 0) {
   1179 		amr->amr_mbox->mb_cmd.mb_busy = 0;
   1180 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1181 		    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
   1182 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1183 		    sizeof(struct amr_mailbox), BUS_DMASYNC_PREREAD);
   1184 		return (EAGAIN);
   1185 	}
   1186 
   1187 	amr->amr_mbox->mb_segment = 0;
   1188 	memcpy(&amr->amr_mbox->mb_cmd, &ac->ac_cmd, sizeof(ac->ac_cmd));
   1189 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1190 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
   1191 
   1192 	ac->ac_start_time = (time_t)mono_time.tv_sec;
   1193 	ac->ac_flags |= AC_ACTIVE;
   1194 	amr_outl(amr, AMR_QREG_IDB,
   1195 	    (amr->amr_mbox_paddr + 16) | AMR_QIDB_SUBMIT);
   1196 	return (0);
   1197 }
   1198 
   1199 static int
   1200 amr_std_submit(struct amr_softc *amr, struct amr_ccb *ac)
   1201 {
   1202 
   1203 	amr->amr_mbox->mb_poll = 0;
   1204 	amr->amr_mbox->mb_ack = 0;
   1205 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1206 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
   1207 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1208 	    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
   1209 	if (amr->amr_mbox->mb_cmd.mb_busy != 0)
   1210 		return (EAGAIN);
   1211 
   1212 	if ((amr_inb(amr, AMR_SREG_MBOX_BUSY) & AMR_SMBOX_BUSY_FLAG) != 0) {
   1213 		amr->amr_mbox->mb_cmd.mb_busy = 0;
   1214 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1215 		    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
   1216 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1217 		    sizeof(struct amr_mailbox), BUS_DMASYNC_PREREAD);
   1218 		return (EAGAIN);
   1219 	}
   1220 
   1221 	amr->amr_mbox->mb_segment = 0;
   1222 	memcpy(&amr->amr_mbox->mb_cmd, &ac->ac_cmd, sizeof(ac->ac_cmd));
   1223 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1224 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
   1225 
   1226 	ac->ac_start_time = (time_t)mono_time.tv_sec;
   1227 	ac->ac_flags |= AC_ACTIVE;
   1228 	amr_outb(amr, AMR_SREG_CMD, AMR_SCMD_POST);
   1229 	return (0);
   1230 }
   1231 
   1232 /*
   1233  * Claim any work that the controller has completed; acknowledge completion,
   1234  * save details of the completion in (mbsave).  Must be called with
   1235  * interrupts blocked.
   1236  */
   1237 static int
   1238 amr_quartz_get_work(struct amr_softc *amr, struct amr_mailbox_resp *mbsave)
   1239 {
   1240 
   1241 	/* Work waiting for us? */
   1242 	if (amr_inl(amr, AMR_QREG_ODB) != AMR_QODB_READY)
   1243 		return (-1);
   1244 
   1245 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1246 	    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
   1247 
   1248 	/* Save the mailbox, which contains a list of completed commands. */
   1249 	memcpy(mbsave, &amr->amr_mbox->mb_resp, sizeof(*mbsave));
   1250 
   1251 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1252 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREREAD);
   1253 
   1254 	/* Ack the interrupt and mailbox transfer. */
   1255 	amr_outl(amr, AMR_QREG_ODB, AMR_QODB_READY);
   1256 	amr_outl(amr, AMR_QREG_IDB, (amr->amr_mbox_paddr+16) | AMR_QIDB_ACK);
   1257 
   1258 	/*
   1259 	 * This waits for the controller to notice that we've taken the
   1260 	 * command from it.  It's very inefficient, and we shouldn't do it,
   1261 	 * but if we remove this code, we stop completing commands under
   1262 	 * load.
   1263 	 *
   1264 	 * Peter J says we shouldn't do this.  The documentation says we
   1265 	 * should.  Who is right?
   1266 	 */
   1267 	while ((amr_inl(amr, AMR_QREG_IDB) & AMR_QIDB_ACK) != 0)
   1268 		DELAY(10);
   1269 
   1270 	return (0);
   1271 }
   1272 
   1273 static int
   1274 amr_std_get_work(struct amr_softc *amr, struct amr_mailbox_resp *mbsave)
   1275 {
   1276 	u_int8_t istat;
   1277 
   1278 	/* Check for valid interrupt status. */
   1279 	if (((istat = amr_inb(amr, AMR_SREG_INTR)) & AMR_SINTR_VALID) == 0)
   1280 		return (-1);
   1281 
   1282 	/* Ack the interrupt. */
   1283 	amr_outb(amr, AMR_SREG_INTR, istat);
   1284 
   1285 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1286 	    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
   1287 
   1288 	/* Save mailbox, which contains a list of completed commands. */
   1289 	memcpy(mbsave, &amr->amr_mbox->mb_resp, sizeof(*mbsave));
   1290 
   1291 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1292 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREREAD);
   1293 
   1294 	/* Ack mailbox transfer. */
   1295 	amr_outb(amr, AMR_SREG_CMD, AMR_SCMD_ACKINTR);
   1296 
   1297 	return (0);
   1298 }
   1299 
   1300 static void
   1301 amr_ccb_dump(struct amr_softc *amr, struct amr_ccb *ac)
   1302 {
   1303 	int i;
   1304 
   1305 	printf("%s: ", amr->amr_dv.dv_xname);
   1306 	for (i = 0; i < 4; i++)
   1307 		printf("%08x ", ((u_int32_t *)&ac->ac_cmd)[i]);
   1308 	printf("\n");
   1309 }
   1310