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