Home | History | Annotate | Line # | Download | only in pci
amr.c revision 1.55.2.3
      1  1.55.2.2       tls /*	$NetBSD: amr.c,v 1.55.2.3 2017/12/03 11:37:07 jdolecek 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  *
     19       1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1        ad  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1        ad  */
     31       1.1        ad 
     32       1.1        ad /*-
     33       1.1        ad  * Copyright (c) 1999,2000 Michael Smith
     34       1.1        ad  * Copyright (c) 2000 BSDi
     35       1.1        ad  * All rights reserved.
     36       1.1        ad  *
     37       1.1        ad  * Redistribution and use in source and binary forms, with or without
     38       1.1        ad  * modification, are permitted provided that the following conditions
     39       1.1        ad  * are met:
     40       1.1        ad  * 1. Redistributions of source code must retain the above copyright
     41       1.1        ad  *    notice, this list of conditions and the following disclaimer.
     42       1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     43       1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     44       1.1        ad  *    documentation and/or other materials provided with the distribution.
     45       1.1        ad  *
     46       1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     47       1.1        ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     48       1.1        ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     49       1.1        ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     50       1.1        ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     51       1.1        ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     52       1.1        ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     53       1.1        ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     54       1.1        ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     55       1.1        ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     56       1.1        ad  * SUCH DAMAGE.
     57       1.1        ad  *
     58       1.1        ad  * from FreeBSD: amr_pci.c,v 1.5 2000/08/30 07:52:40 msmith Exp
     59      1.25     perry  * from FreeBSD: amr.c,v 1.16 2000/08/30 07:52:40 msmith Exp
     60       1.1        ad  */
     61       1.1        ad 
     62       1.1        ad /*
     63       1.1        ad  * Driver for AMI RAID controllers.
     64       1.1        ad  */
     65       1.1        ad 
     66       1.1        ad #include <sys/cdefs.h>
     67  1.55.2.2       tls __KERNEL_RCSID(0, "$NetBSD: amr.c,v 1.55.2.3 2017/12/03 11:37:07 jdolecek Exp $");
     68       1.1        ad 
     69       1.1        ad #include <sys/param.h>
     70       1.1        ad #include <sys/systm.h>
     71       1.1        ad #include <sys/kernel.h>
     72       1.1        ad #include <sys/device.h>
     73       1.1        ad #include <sys/queue.h>
     74       1.1        ad #include <sys/proc.h>
     75       1.1        ad #include <sys/buf.h>
     76       1.1        ad #include <sys/malloc.h>
     77      1.36    bouyer #include <sys/conf.h>
     78       1.9        ad #include <sys/kthread.h>
     79      1.40      elad #include <sys/kauth.h>
     80  1.55.2.3  jdolecek #include <sys/mutex.h>
     81  1.55.2.3  jdolecek #include <sys/condvar.h>
     82  1.55.2.3  jdolecek #include <sys/module.h>
     83       1.1        ad 
     84       1.1        ad #include <machine/endian.h>
     85      1.46        ad #include <sys/bus.h>
     86       1.1        ad 
     87       1.1        ad #include <dev/pci/pcidevs.h>
     88       1.1        ad #include <dev/pci/pcivar.h>
     89       1.1        ad #include <dev/pci/amrreg.h>
     90       1.1        ad #include <dev/pci/amrvar.h>
     91      1.36    bouyer #include <dev/pci/amrio.h>
     92       1.1        ad 
     93      1.22  drochner #include "locators.h"
     94      1.22  drochner 
     95  1.55.2.3  jdolecek #include "ioconf.h"
     96  1.55.2.3  jdolecek 
     97      1.51    cegger static void	amr_attach(device_t, device_t, void *);
     98      1.27   thorpej static void	amr_ccb_dump(struct amr_softc *, struct amr_ccb *);
     99      1.27   thorpej static void	*amr_enquire(struct amr_softc *, u_int8_t, u_int8_t, u_int8_t,
    100      1.27   thorpej 			     void *);
    101      1.27   thorpej static int	amr_init(struct amr_softc *, const char *,
    102       1.1        ad 			 struct pci_attach_args *pa);
    103      1.27   thorpej static int	amr_intr(void *);
    104      1.51    cegger static int	amr_match(device_t, cfdata_t, void *);
    105  1.55.2.3  jdolecek static int	amr_rescan(device_t, const char *, const int *);
    106      1.27   thorpej static int	amr_print(void *, const char *);
    107      1.27   thorpej static void	amr_shutdown(void *);
    108      1.27   thorpej static void	amr_teardown(struct amr_softc *);
    109  1.55.2.3  jdolecek static void	amr_quartz_thread(void *);
    110  1.55.2.3  jdolecek static void	amr_std_thread(void *);
    111      1.27   thorpej 
    112      1.27   thorpej static int	amr_quartz_get_work(struct amr_softc *,
    113      1.27   thorpej 				    struct amr_mailbox_resp *);
    114      1.27   thorpej static int	amr_quartz_submit(struct amr_softc *, struct amr_ccb *);
    115      1.27   thorpej static int	amr_std_get_work(struct amr_softc *, struct amr_mailbox_resp *);
    116      1.27   thorpej static int	amr_std_submit(struct amr_softc *, struct amr_ccb *);
    117       1.1        ad 
    118      1.36    bouyer static dev_type_open(amropen);
    119      1.36    bouyer static dev_type_close(amrclose);
    120      1.36    bouyer static dev_type_ioctl(amrioctl);
    121      1.36    bouyer 
    122  1.55.2.3  jdolecek CFATTACH_DECL3_NEW(amr, sizeof(struct amr_softc),
    123  1.55.2.3  jdolecek     amr_match, amr_attach, NULL, NULL, amr_rescan, NULL, 0);
    124       1.1        ad 
    125      1.36    bouyer const struct cdevsw amr_cdevsw = {
    126  1.55.2.2       tls 	.d_open = amropen,
    127  1.55.2.2       tls 	.d_close = amrclose,
    128  1.55.2.2       tls 	.d_read = noread,
    129  1.55.2.2       tls 	.d_write = nowrite,
    130  1.55.2.2       tls 	.d_ioctl = amrioctl,
    131  1.55.2.2       tls 	.d_stop = nostop,
    132  1.55.2.2       tls 	.d_tty = notty,
    133  1.55.2.2       tls 	.d_poll = nopoll,
    134  1.55.2.2       tls 	.d_mmap = nommap,
    135  1.55.2.2       tls 	.d_kqfilter = nokqfilter,
    136  1.55.2.2       tls 	.d_discard = nodiscard,
    137  1.55.2.2       tls 	.d_flag = D_OTHER
    138  1.55.2.3  jdolecek };
    139      1.36    bouyer 
    140      1.36    bouyer extern struct   cfdriver amr_cd;
    141      1.36    bouyer 
    142       1.1        ad #define AT_QUARTZ	0x01	/* `Quartz' chipset */
    143       1.1        ad #define	AT_SIG		0x02	/* Check for signature */
    144       1.1        ad 
    145      1.38  christos static struct amr_pci_type {
    146       1.1        ad 	u_short	apt_vendor;
    147       1.1        ad 	u_short	apt_product;
    148       1.1        ad 	u_short	apt_flags;
    149      1.38  christos } const amr_pci_type[] = {
    150       1.1        ad 	{ PCI_VENDOR_AMI,   PCI_PRODUCT_AMI_MEGARAID,  0 },
    151       1.1        ad 	{ PCI_VENDOR_AMI,   PCI_PRODUCT_AMI_MEGARAID2, 0 },
    152       1.1        ad 	{ PCI_VENDOR_AMI,   PCI_PRODUCT_AMI_MEGARAID3, AT_QUARTZ },
    153      1.21        he 	{ PCI_VENDOR_SYMBIOS, PCI_PRODUCT_AMI_MEGARAID3, AT_QUARTZ },
    154      1.12      matt 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_AMI_MEGARAID3, AT_QUARTZ | AT_SIG },
    155      1.31  jonathan 	{ PCI_VENDOR_INTEL,  PCI_PRODUCT_SYMBIOS_MEGARAID_320X, AT_QUARTZ },
    156      1.31  jonathan 	{ PCI_VENDOR_INTEL,  PCI_PRODUCT_SYMBIOS_MEGARAID_320E, AT_QUARTZ },
    157      1.31  jonathan 	{ PCI_VENDOR_SYMBIOS,  PCI_PRODUCT_SYMBIOS_MEGARAID_300X, AT_QUARTZ },
    158      1.12      matt 	{ PCI_VENDOR_DELL,  PCI_PRODUCT_DELL_PERC_4DI, AT_QUARTZ },
    159      1.14    martti 	{ PCI_VENDOR_DELL,  PCI_PRODUCT_DELL_PERC_4DI_2, AT_QUARTZ },
    160      1.23    martti 	{ PCI_VENDOR_DELL,  PCI_PRODUCT_DELL_PERC_4ESI, AT_QUARTZ },
    161      1.24    martti 	{ PCI_VENDOR_SYMBIOS,  PCI_PRODUCT_SYMBIOS_PERC_4SC, AT_QUARTZ },
    162      1.31  jonathan 	{ PCI_VENDOR_SYMBIOS,  PCI_PRODUCT_SYMBIOS_MEGARAID_320X, AT_QUARTZ },
    163      1.31  jonathan 	{ PCI_VENDOR_SYMBIOS,  PCI_PRODUCT_SYMBIOS_MEGARAID_320E, AT_QUARTZ },
    164      1.31  jonathan 	{ PCI_VENDOR_SYMBIOS,  PCI_PRODUCT_SYMBIOS_MEGARAID_300X, AT_QUARTZ },
    165       1.1        ad };
    166       1.1        ad 
    167      1.38  christos static struct amr_typestr {
    168       1.1        ad 	const char	*at_str;
    169       1.1        ad 	int		at_sig;
    170      1.38  christos } const amr_typestr[] = {
    171       1.1        ad 	{ "Series 431",			AMR_SIG_431 },
    172       1.1        ad 	{ "Series 438",			AMR_SIG_438 },
    173       1.1        ad 	{ "Series 466",			AMR_SIG_466 },
    174       1.1        ad 	{ "Series 467",			AMR_SIG_467 },
    175       1.1        ad 	{ "Series 490",			AMR_SIG_490 },
    176       1.1        ad 	{ "Series 762",			AMR_SIG_762 },
    177       1.1        ad 	{ "HP NetRAID (T5)",		AMR_SIG_T5 },
    178       1.1        ad 	{ "HP NetRAID (T7)",		AMR_SIG_T7 },
    179       1.1        ad };
    180       1.1        ad 
    181      1.38  christos static struct {
    182       1.9        ad 	const char	*ds_descr;
    183       1.9        ad 	int	ds_happy;
    184      1.38  christos } const amr_dstate[] = {
    185       1.9        ad 	{ "offline",	0 },
    186       1.9        ad 	{ "degraded",	1 },
    187       1.9        ad 	{ "optimal",	1 },
    188       1.9        ad 	{ "online",	1 },
    189       1.9        ad 	{ "failed",	0 },
    190       1.9        ad 	{ "rebuilding",	1 },
    191       1.9        ad 	{ "hotspare",	0 },
    192       1.9        ad };
    193       1.9        ad 
    194      1.27   thorpej static void	*amr_sdh;
    195      1.27   thorpej 
    196  1.55.2.3  jdolecek static kcondvar_t thread_cv;
    197  1.55.2.3  jdolecek static kmutex_t	thread_mutex;
    198  1.55.2.3  jdolecek 
    199      1.27   thorpej static int	amr_max_segs;
    200      1.27   thorpej int		amr_max_xfer;
    201       1.1        ad 
    202       1.1        ad static inline u_int8_t
    203       1.1        ad amr_inb(struct amr_softc *amr, int off)
    204       1.1        ad {
    205       1.1        ad 	bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 1,
    206       1.1        ad 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
    207       1.1        ad 	return (bus_space_read_1(amr->amr_iot, amr->amr_ioh, off));
    208       1.1        ad }
    209       1.1        ad 
    210       1.1        ad static inline u_int32_t
    211       1.1        ad amr_inl(struct amr_softc *amr, int off)
    212       1.1        ad {
    213       1.1        ad 	bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 4,
    214       1.1        ad 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
    215       1.1        ad 	return (bus_space_read_4(amr->amr_iot, amr->amr_ioh, off));
    216       1.1        ad }
    217       1.1        ad 
    218       1.1        ad static inline void
    219       1.1        ad amr_outb(struct amr_softc *amr, int off, u_int8_t val)
    220       1.1        ad {
    221       1.1        ad 	bus_space_write_1(amr->amr_iot, amr->amr_ioh, off, val);
    222       1.1        ad 	bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 1,
    223       1.1        ad 	    BUS_SPACE_BARRIER_WRITE);
    224       1.1        ad }
    225       1.1        ad 
    226       1.1        ad static inline void
    227       1.1        ad amr_outl(struct amr_softc *amr, int off, u_int32_t val)
    228       1.1        ad {
    229       1.1        ad 	bus_space_write_4(amr->amr_iot, amr->amr_ioh, off, val);
    230       1.1        ad 	bus_space_barrier(amr->amr_iot, amr->amr_ioh, off, 4,
    231       1.1        ad 	    BUS_SPACE_BARRIER_WRITE);
    232       1.1        ad }
    233       1.1        ad 
    234       1.1        ad /*
    235       1.1        ad  * Match a supported device.
    236       1.1        ad  */
    237      1.27   thorpej static int
    238      1.51    cegger amr_match(device_t parent, cfdata_t match, void *aux)
    239       1.1        ad {
    240       1.1        ad 	struct pci_attach_args *pa;
    241       1.1        ad 	pcireg_t s;
    242       1.1        ad 	int i;
    243       1.1        ad 
    244       1.1        ad 	pa = (struct pci_attach_args *)aux;
    245       1.1        ad 
    246       1.1        ad 	/*
    247       1.1        ad 	 * Don't match the device if it's operating in I2O mode.  In this
    248       1.1        ad 	 * case it should be handled by the `iop' driver.
    249       1.1        ad 	 */
    250       1.1        ad 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_I2O)
    251       1.1        ad 		return (0);
    252       1.1        ad 
    253       1.1        ad 	for (i = 0; i < sizeof(amr_pci_type) / sizeof(amr_pci_type[0]); i++)
    254      1.25     perry 		if (PCI_VENDOR(pa->pa_id) == amr_pci_type[i].apt_vendor &&
    255       1.1        ad 		    PCI_PRODUCT(pa->pa_id) == amr_pci_type[i].apt_product)
    256       1.1        ad 		    	break;
    257       1.1        ad 
    258       1.1        ad 	if (i == sizeof(amr_pci_type) / sizeof(amr_pci_type[0]))
    259       1.1        ad 		return (0);
    260       1.1        ad 
    261       1.1        ad 	if ((amr_pci_type[i].apt_flags & AT_SIG) == 0)
    262       1.1        ad 		return (1);
    263       1.1        ad 
    264       1.1        ad 	s = pci_conf_read(pa->pa_pc, pa->pa_tag, AMR_QUARTZ_SIG_REG) & 0xffff;
    265       1.1        ad 	return (s == AMR_QUARTZ_SIG0 || s == AMR_QUARTZ_SIG1);
    266       1.1        ad }
    267       1.1        ad 
    268       1.1        ad /*
    269       1.9        ad  * Attach a supported device.
    270       1.1        ad  */
    271      1.27   thorpej static void
    272      1.51    cegger amr_attach(device_t parent, device_t self, void *aux)
    273       1.1        ad {
    274       1.1        ad 	struct pci_attach_args *pa;
    275       1.1        ad 	const struct amr_pci_type *apt;
    276       1.1        ad 	struct amr_softc *amr;
    277       1.1        ad 	pci_chipset_tag_t pc;
    278       1.1        ad 	pci_intr_handle_t ih;
    279       1.1        ad 	const char *intrstr;
    280       1.1        ad 	pcireg_t reg;
    281  1.55.2.3  jdolecek 	int rseg, i, size, rv, memreg, ioreg;
    282      1.36    bouyer 	struct amr_ccb *ac;
    283  1.55.2.2       tls 	char intrbuf[PCI_INTRSTR_LEN];
    284       1.1        ad 
    285       1.8   thorpej 	aprint_naive(": RAID controller\n");
    286       1.8   thorpej 
    287      1.52    cegger 	amr = device_private(self);
    288      1.55  jakllsch 	amr->amr_dv = self;
    289  1.55.2.3  jdolecek 
    290  1.55.2.3  jdolecek 	mutex_init(&amr->amr_mutex, MUTEX_DEFAULT, IPL_BIO);
    291  1.55.2.3  jdolecek 
    292       1.1        ad 	pa = (struct pci_attach_args *)aux;
    293       1.1        ad 	pc = pa->pa_pc;
    294       1.1        ad 
    295       1.1        ad 	for (i = 0; i < sizeof(amr_pci_type) / sizeof(amr_pci_type[0]); i++)
    296       1.1        ad 		if (PCI_VENDOR(pa->pa_id) == amr_pci_type[i].apt_vendor &&
    297       1.1        ad 		    PCI_PRODUCT(pa->pa_id) == amr_pci_type[i].apt_product)
    298       1.1        ad 			break;
    299       1.1        ad 	apt = amr_pci_type + i;
    300       1.1        ad 
    301       1.1        ad 	memreg = ioreg = 0;
    302       1.1        ad 	for (i = 0x10; i <= 0x14; i += 4) {
    303       1.1        ad 		reg = pci_conf_read(pc, pa->pa_tag, i);
    304       1.1        ad 		switch (PCI_MAPREG_TYPE(reg)) {
    305       1.1        ad 		case PCI_MAPREG_TYPE_MEM:
    306      1.19      fvdl 			if (PCI_MAPREG_MEM_SIZE(reg) != 0)
    307      1.19      fvdl 				memreg = i;
    308       1.1        ad 			break;
    309       1.1        ad 		case PCI_MAPREG_TYPE_IO:
    310      1.19      fvdl 			if (PCI_MAPREG_IO_SIZE(reg) != 0)
    311      1.19      fvdl 				ioreg = i;
    312       1.1        ad 			break;
    313       1.1        ad 		}
    314       1.1        ad 	}
    315       1.1        ad 
    316      1.18   mycroft 	if (memreg && pci_mapreg_map(pa, memreg, PCI_MAPREG_TYPE_MEM, 0,
    317      1.18   mycroft 	    &amr->amr_iot, &amr->amr_ioh, NULL, &amr->amr_ios) == 0)
    318      1.18   mycroft 		;
    319      1.18   mycroft 	else if (ioreg && pci_mapreg_map(pa, ioreg, PCI_MAPREG_TYPE_IO, 0,
    320      1.18   mycroft 	    &amr->amr_iot, &amr->amr_ioh, NULL, &amr->amr_ios) == 0)
    321      1.18   mycroft 		;
    322      1.18   mycroft 	else {
    323       1.8   thorpej 		aprint_error("can't map control registers\n");
    324       1.9        ad 		amr_teardown(amr);
    325       1.1        ad 		return;
    326       1.1        ad 	}
    327       1.1        ad 
    328       1.9        ad 	amr->amr_flags |= AMRF_PCI_REGS;
    329       1.1        ad 	amr->amr_dmat = pa->pa_dmat;
    330       1.9        ad 	amr->amr_pc = pa->pa_pc;
    331       1.1        ad 
    332       1.1        ad 	/* Enable the device. */
    333       1.1        ad 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    334       1.1        ad 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    335       1.1        ad 	    reg | PCI_COMMAND_MASTER_ENABLE);
    336       1.1        ad 
    337       1.1        ad 	/* Map and establish the interrupt. */
    338       1.1        ad 	if (pci_intr_map(pa, &ih)) {
    339       1.8   thorpej 		aprint_error("can't map interrupt\n");
    340       1.9        ad 		amr_teardown(amr);
    341       1.1        ad 		return;
    342       1.1        ad 	}
    343  1.55.2.2       tls 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
    344       1.1        ad 	amr->amr_ih = pci_intr_establish(pc, ih, IPL_BIO, amr_intr, amr);
    345       1.1        ad 	if (amr->amr_ih == NULL) {
    346       1.8   thorpej 		aprint_error("can't establish interrupt");
    347       1.1        ad 		if (intrstr != NULL)
    348      1.53     njoly 			aprint_error(" at %s", intrstr);
    349      1.53     njoly 		aprint_error("\n");
    350       1.9        ad 		amr_teardown(amr);
    351       1.1        ad 		return;
    352       1.1        ad 	}
    353       1.9        ad 	amr->amr_flags |= AMRF_PCI_INTR;
    354       1.1        ad 
    355       1.1        ad 	/*
    356       1.1        ad 	 * Allocate space for the mailbox and S/G lists.  Some controllers
    357       1.1        ad 	 * don't like S/G lists to be located below 0x2000, so we allocate
    358       1.1        ad 	 * enough slop to enable us to compensate.
    359       1.1        ad 	 *
    360       1.1        ad 	 * The standard mailbox structure needs to be aligned on a 16-byte
    361       1.1        ad 	 * boundary.  The 64-bit mailbox has one extra field, 4 bytes in
    362      1.42  christos 	 * size, which precedes the standard mailbox.
    363       1.1        ad 	 */
    364       1.1        ad 	size = AMR_SGL_SIZE * AMR_MAX_CMDS + 0x2000;
    365       1.9        ad 	amr->amr_dmasize = size;
    366       1.1        ad 
    367      1.15      fvdl 	if ((rv = bus_dmamem_alloc(amr->amr_dmat, size, PAGE_SIZE, 0,
    368       1.9        ad 	    &amr->amr_dmaseg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    369  1.55.2.3  jdolecek 		aprint_error_dev(amr->amr_dv,
    370  1.55.2.3  jdolecek 		    "unable to allocate buffer, rv = %d\n", rv);
    371       1.9        ad 		amr_teardown(amr);
    372       1.1        ad 		return;
    373       1.1        ad 	}
    374       1.9        ad 	amr->amr_flags |= AMRF_DMA_ALLOC;
    375       1.1        ad 
    376      1.25     perry 	if ((rv = bus_dmamem_map(amr->amr_dmat, &amr->amr_dmaseg, rseg, size,
    377      1.44  christos 	    (void **)&amr->amr_mbox,
    378       1.1        ad 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    379      1.55  jakllsch 		aprint_error_dev(amr->amr_dv, "unable to map buffer, rv = %d\n",
    380      1.47    cegger 		    rv);
    381       1.9        ad 		amr_teardown(amr);
    382       1.1        ad 		return;
    383       1.1        ad 	}
    384       1.9        ad 	amr->amr_flags |= AMRF_DMA_MAP;
    385       1.1        ad 
    386      1.25     perry 	if ((rv = bus_dmamap_create(amr->amr_dmat, size, 1, size, 0,
    387       1.1        ad 	    BUS_DMA_NOWAIT, &amr->amr_dmamap)) != 0) {
    388  1.55.2.3  jdolecek 		aprint_error_dev(amr->amr_dv,
    389  1.55.2.3  jdolecek 		    "unable to create buffer DMA map, rv = %d\n", rv);
    390       1.9        ad 		amr_teardown(amr);
    391       1.1        ad 		return;
    392       1.1        ad 	}
    393       1.9        ad 	amr->amr_flags |= AMRF_DMA_CREATE;
    394       1.1        ad 
    395       1.1        ad 	if ((rv = bus_dmamap_load(amr->amr_dmat, amr->amr_dmamap,
    396       1.1        ad 	    amr->amr_mbox, size, NULL, BUS_DMA_NOWAIT)) != 0) {
    397  1.55.2.3  jdolecek 		aprint_error_dev(amr->amr_dv,
    398  1.55.2.3  jdolecek 		    "unable to load buffer DMA map, rv = %d\n", rv);
    399       1.9        ad 		amr_teardown(amr);
    400       1.1        ad 		return;
    401       1.1        ad 	}
    402       1.9        ad 	amr->amr_flags |= AMRF_DMA_LOAD;
    403       1.1        ad 
    404       1.1        ad 	memset(amr->amr_mbox, 0, size);
    405       1.1        ad 
    406       1.9        ad 	amr->amr_mbox_paddr = amr->amr_dmamap->dm_segs[0].ds_addr;
    407       1.1        ad 	amr->amr_sgls_paddr = (amr->amr_mbox_paddr + 0x1fff) & ~0x1fff;
    408      1.44  christos 	amr->amr_sgls = (struct amr_sgentry *)((char *)amr->amr_mbox +
    409       1.1        ad 	    amr->amr_sgls_paddr - amr->amr_dmamap->dm_segs[0].ds_addr);
    410       1.1        ad 
    411       1.1        ad 	/*
    412       1.1        ad 	 * Allocate and initalise the command control blocks.
    413       1.1        ad 	 */
    414       1.1        ad 	ac = malloc(sizeof(*ac) * AMR_MAX_CMDS, M_DEVBUF, M_NOWAIT | M_ZERO);
    415       1.1        ad 	amr->amr_ccbs = ac;
    416       1.1        ad 	SLIST_INIT(&amr->amr_ccb_freelist);
    417      1.10        ad 	TAILQ_INIT(&amr->amr_ccb_active);
    418       1.9        ad 	amr->amr_flags |= AMRF_CCBS;
    419       1.9        ad 
    420       1.9        ad 	if (amr_max_xfer == 0) {
    421  1.55.2.1       tls 		amr_max_xfer = min(((AMR_MAX_SEGS - 1) * PAGE_SIZE),
    422  1.55.2.1       tls 				   device_maxphys(amr->amr_dv));
    423       1.9        ad 		amr_max_segs = (amr_max_xfer + (PAGE_SIZE * 2) - 1) / PAGE_SIZE;
    424       1.9        ad 	}
    425  1.55.2.1       tls 	amr->amr_dv->dv_maxphys = amr_max_xfer;
    426       1.1        ad 
    427       1.1        ad 	for (i = 0; i < AMR_MAX_CMDS; i++, ac++) {
    428       1.9        ad 		rv = bus_dmamap_create(amr->amr_dmat, amr_max_xfer,
    429       1.9        ad 		    amr_max_segs, amr_max_xfer, 0,
    430       1.9        ad 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ac->ac_xfer_map);
    431       1.1        ad 		if (rv != 0)
    432       1.1        ad 			break;
    433       1.1        ad 
    434       1.1        ad 		ac->ac_ident = i;
    435  1.55.2.3  jdolecek 		cv_init(&ac->ac_cv, "amr1ccb");
    436  1.55.2.3  jdolecek 		mutex_init(&ac->ac_mutex, MUTEX_DEFAULT, IPL_NONE);
    437       1.9        ad 		amr_ccb_free(amr, ac);
    438       1.9        ad 	}
    439       1.9        ad 	if (i != AMR_MAX_CMDS) {
    440      1.55  jakllsch 		aprint_error_dev(amr->amr_dv, "memory exhausted\n");
    441       1.9        ad 		amr_teardown(amr);
    442       1.9        ad 		return;
    443       1.1        ad 	}
    444       1.1        ad 
    445       1.1        ad 	/*
    446       1.1        ad 	 * Take care of model-specific tasks.
    447       1.1        ad 	 */
    448       1.1        ad 	if ((apt->apt_flags & AT_QUARTZ) != 0) {
    449       1.1        ad 		amr->amr_submit = amr_quartz_submit;
    450       1.1        ad 		amr->amr_get_work = amr_quartz_get_work;
    451       1.1        ad 	} else {
    452       1.1        ad 		amr->amr_submit = amr_std_submit;
    453       1.1        ad 		amr->amr_get_work = amr_std_get_work;
    454       1.1        ad 
    455       1.1        ad 		/* Notify the controller of the mailbox location. */
    456       1.9        ad 		amr_outl(amr, AMR_SREG_MBOX, (u_int32_t)amr->amr_mbox_paddr + 16);
    457       1.1        ad 		amr_outb(amr, AMR_SREG_MBOX_ENABLE, AMR_SMBOX_ENABLE_ADDR);
    458       1.1        ad 
    459       1.1        ad 		/* Clear outstanding interrupts and enable interrupts. */
    460       1.1        ad 		amr_outb(amr, AMR_SREG_CMD, AMR_SCMD_ACKINTR);
    461       1.1        ad 		amr_outb(amr, AMR_SREG_TOGL,
    462       1.1        ad 		    amr_inb(amr, AMR_SREG_TOGL) | AMR_STOGL_ENABLE);
    463       1.1        ad 	}
    464       1.1        ad 
    465       1.1        ad 	/*
    466       1.1        ad 	 * Retrieve parameters, and tell the world about us.
    467       1.1        ad 	 */
    468       1.9        ad 	amr->amr_enqbuf = malloc(AMR_ENQUIRY_BUFSIZE, M_DEVBUF, M_NOWAIT);
    469       1.9        ad 	amr->amr_flags |= AMRF_ENQBUF;
    470       1.1        ad 	amr->amr_maxqueuecnt = i;
    471       1.8   thorpej 	aprint_normal(": AMI RAID ");
    472       1.9        ad 	if (amr_init(amr, intrstr, pa) != 0) {
    473       1.9        ad 		amr_teardown(amr);
    474       1.1        ad 		return;
    475       1.9        ad 	}
    476       1.1        ad 
    477      1.25     perry 	/*
    478       1.1        ad 	 * Cap the maximum number of outstanding commands.  AMI's Linux
    479       1.1        ad 	 * driver doesn't trust the controller's reported value, and lockups
    480       1.1        ad 	 * have been seen when we do.
    481       1.1        ad 	 */
    482       1.1        ad 	amr->amr_maxqueuecnt = min(amr->amr_maxqueuecnt, AMR_MAX_CMDS);
    483       1.1        ad 	if (amr->amr_maxqueuecnt > i)
    484       1.1        ad 		amr->amr_maxqueuecnt = i;
    485       1.1        ad 
    486       1.1        ad 	/* Set our `shutdownhook' before we start any device activity. */
    487       1.1        ad 	if (amr_sdh == NULL)
    488       1.1        ad 		amr_sdh = shutdownhook_establish(amr_shutdown, NULL);
    489       1.1        ad 
    490       1.1        ad 	/* Attach sub-devices. */
    491  1.55.2.3  jdolecek 	amr_rescan(self, "amr", 0);
    492  1.55.2.3  jdolecek 
    493  1.55.2.3  jdolecek 	SIMPLEQ_INIT(&amr->amr_ccb_queue);
    494  1.55.2.3  jdolecek 
    495  1.55.2.3  jdolecek 	cv_init(&thread_cv, "amrwdog");
    496  1.55.2.3  jdolecek 	mutex_init(&thread_mutex, MUTEX_DEFAULT, IPL_NONE);
    497  1.55.2.3  jdolecek 
    498  1.55.2.3  jdolecek 	if ((apt->apt_flags & AT_QUARTZ) == 0) {
    499  1.55.2.3  jdolecek 		rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    500  1.55.2.3  jdolecek 				    amr_std_thread, amr, &amr->amr_thread,
    501  1.55.2.3  jdolecek 				    "%s", device_xname(amr->amr_dv));
    502  1.55.2.3  jdolecek 	} else {
    503  1.55.2.3  jdolecek 		rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    504  1.55.2.3  jdolecek 				    amr_quartz_thread, amr, &amr->amr_thread,
    505  1.55.2.3  jdolecek 				    "%s", device_xname(amr->amr_dv));
    506  1.55.2.3  jdolecek 	}
    507  1.55.2.3  jdolecek 	if (rv != 0)
    508  1.55.2.3  jdolecek 		aprint_error_dev(amr->amr_dv, "unable to create thread (%d)",
    509  1.55.2.3  jdolecek  		    rv);
    510  1.55.2.3  jdolecek  	else
    511  1.55.2.3  jdolecek  		amr->amr_flags |= AMRF_THREAD;
    512  1.55.2.3  jdolecek }
    513  1.55.2.3  jdolecek 
    514  1.55.2.3  jdolecek static int
    515  1.55.2.3  jdolecek amr_rescan(device_t self, const char *attr, const int *flags)
    516  1.55.2.3  jdolecek {
    517  1.55.2.3  jdolecek 	int j;
    518  1.55.2.3  jdolecek 	int locs[AMRCF_NLOCS];
    519  1.55.2.3  jdolecek 	struct amr_attach_args amra;
    520  1.55.2.3  jdolecek 	struct amr_softc *amr;
    521  1.55.2.3  jdolecek 
    522  1.55.2.3  jdolecek 	amr = device_private(self);
    523       1.9        ad 	for (j = 0; j < amr->amr_numdrives; j++) {
    524  1.55.2.3  jdolecek 		if (amr->amr_drive[j].al_dv)
    525  1.55.2.3  jdolecek 			continue;
    526       1.9        ad 		if (amr->amr_drive[j].al_size == 0)
    527       1.1        ad 			continue;
    528       1.9        ad 		amra.amra_unit = j;
    529      1.22  drochner 
    530      1.28  drochner 		locs[AMRCF_UNIT] = j;
    531      1.22  drochner 
    532      1.55  jakllsch 		amr->amr_drive[j].al_dv = config_found_sm_loc(amr->amr_dv,
    533  1.55.2.3  jdolecek 			attr, locs, &amra, amr_print, config_stdsubmatch);
    534      1.45        ad 	}
    535  1.55.2.3  jdolecek 	return 0;
    536       1.9        ad }
    537       1.9        ad 
    538       1.9        ad /*
    539       1.9        ad  * Free up resources.
    540       1.9        ad  */
    541      1.27   thorpej static void
    542       1.9        ad amr_teardown(struct amr_softc *amr)
    543       1.9        ad {
    544       1.9        ad 	struct amr_ccb *ac;
    545       1.9        ad 	int fl;
    546       1.9        ad 
    547       1.9        ad 	fl = amr->amr_flags;
    548       1.9        ad 
    549       1.9        ad 	if ((fl & AMRF_THREAD) != 0) {
    550       1.9        ad 		amr->amr_flags |= AMRF_THREAD_EXIT;
    551  1.55.2.3  jdolecek 		mutex_enter(&thread_mutex);
    552  1.55.2.3  jdolecek 		cv_broadcast(&thread_cv);
    553  1.55.2.3  jdolecek 		mutex_exit(&thread_mutex);
    554  1.55.2.3  jdolecek 		while ((amr->amr_flags & AMRF_THREAD_EXIT) != 0) {
    555  1.55.2.3  jdolecek 			mutex_enter(&thread_mutex);
    556  1.55.2.3  jdolecek 			cv_wait(&thread_cv, &thread_mutex);
    557  1.55.2.3  jdolecek 			mutex_exit(&thread_mutex);
    558  1.55.2.3  jdolecek 		}
    559       1.9        ad 	}
    560       1.9        ad 	if ((fl & AMRF_CCBS) != 0) {
    561       1.9        ad 		SLIST_FOREACH(ac, &amr->amr_ccb_freelist, ac_chain.slist) {
    562       1.9        ad 			bus_dmamap_destroy(amr->amr_dmat, ac->ac_xfer_map);
    563       1.9        ad 		}
    564       1.9        ad 		free(amr->amr_ccbs, M_DEVBUF);
    565       1.9        ad 	}
    566       1.9        ad 	if ((fl & AMRF_ENQBUF) != 0)
    567       1.9        ad 		free(amr->amr_enqbuf, M_DEVBUF);
    568       1.9        ad 	if ((fl & AMRF_DMA_LOAD) != 0)
    569       1.9        ad 		bus_dmamap_unload(amr->amr_dmat, amr->amr_dmamap);
    570       1.9        ad 	if ((fl & AMRF_DMA_MAP) != 0)
    571      1.44  christos 		bus_dmamem_unmap(amr->amr_dmat, (void *)amr->amr_mbox,
    572       1.9        ad 		    amr->amr_dmasize);
    573       1.9        ad 	if ((fl & AMRF_DMA_ALLOC) != 0)
    574       1.9        ad 		bus_dmamem_free(amr->amr_dmat, &amr->amr_dmaseg, 1);
    575       1.9        ad 	if ((fl & AMRF_DMA_CREATE) != 0)
    576       1.9        ad 		bus_dmamap_destroy(amr->amr_dmat, amr->amr_dmamap);
    577       1.9        ad 	if ((fl & AMRF_PCI_INTR) != 0)
    578       1.9        ad 		pci_intr_disestablish(amr->amr_pc, amr->amr_ih);
    579       1.9        ad 	if ((fl & AMRF_PCI_REGS) != 0)
    580      1.11      fvdl 		bus_space_unmap(amr->amr_iot, amr->amr_ioh, amr->amr_ios);
    581       1.1        ad }
    582       1.1        ad 
    583       1.1        ad /*
    584       1.1        ad  * Print autoconfiguration message for a sub-device.
    585       1.1        ad  */
    586      1.27   thorpej static int
    587       1.1        ad amr_print(void *aux, const char *pnp)
    588       1.1        ad {
    589       1.1        ad 	struct amr_attach_args *amra;
    590       1.1        ad 
    591       1.1        ad 	amra = (struct amr_attach_args *)aux;
    592       1.1        ad 
    593       1.1        ad 	if (pnp != NULL)
    594       1.7   thorpej 		aprint_normal("block device at %s", pnp);
    595       1.7   thorpej 	aprint_normal(" unit %d", amra->amra_unit);
    596       1.1        ad 	return (UNCONF);
    597       1.1        ad }
    598       1.1        ad 
    599       1.1        ad /*
    600       1.1        ad  * Retrieve operational parameters and describe the controller.
    601       1.1        ad  */
    602      1.27   thorpej static int
    603       1.1        ad amr_init(struct amr_softc *amr, const char *intrstr,
    604       1.1        ad 	 struct pci_attach_args *pa)
    605       1.1        ad {
    606       1.9        ad 	struct amr_adapter_info *aa;
    607       1.1        ad 	struct amr_prodinfo *ap;
    608       1.1        ad 	struct amr_enquiry *ae;
    609       1.1        ad 	struct amr_enquiry3 *aex;
    610       1.1        ad 	const char *prodstr;
    611       1.9        ad 	u_int i, sig, ishp;
    612      1.26  christos 	char sbuf[64];
    613       1.1        ad 
    614       1.1        ad 	/*
    615       1.1        ad 	 * Try to get 40LD product info, which tells us what the card is
    616       1.1        ad 	 * labelled as.
    617       1.1        ad 	 */
    618       1.9        ad 	ap = amr_enquire(amr, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0,
    619       1.9        ad 	    amr->amr_enqbuf);
    620       1.1        ad 	if (ap != NULL) {
    621       1.8   thorpej 		aprint_normal("<%.80s>\n", ap->ap_product);
    622       1.1        ad 		if (intrstr != NULL)
    623      1.55  jakllsch 			aprint_normal_dev(amr->amr_dv, "interrupting at %s\n",
    624      1.47    cegger 			    intrstr);
    625  1.55.2.3  jdolecek 		aprint_normal_dev(amr->amr_dv,
    626  1.55.2.3  jdolecek 		    "firmware %.16s, BIOS %.16s, %dMB RAM\n",
    627  1.55.2.3  jdolecek 		    ap->ap_firmware, ap->ap_bios, le16toh(ap->ap_memsize));
    628       1.1        ad 
    629       1.1        ad 		amr->amr_maxqueuecnt = ap->ap_maxio;
    630       1.1        ad 
    631       1.1        ad 		/*
    632       1.1        ad 		 * Fetch and record state of logical drives.
    633       1.1        ad 		 */
    634       1.1        ad 		aex = amr_enquire(amr, AMR_CMD_CONFIG, AMR_CONFIG_ENQ3,
    635       1.9        ad 		    AMR_CONFIG_ENQ3_SOLICITED_FULL, amr->amr_enqbuf);
    636       1.1        ad 		if (aex == NULL) {
    637      1.55  jakllsch 			aprint_error_dev(amr->amr_dv, "ENQUIRY3 failed\n");
    638       1.1        ad 			return (-1);
    639       1.1        ad 		}
    640       1.1        ad 
    641      1.32  christos 		if (aex->ae_numldrives > __arraycount(aex->ae_drivestate)) {
    642  1.55.2.3  jdolecek 			aprint_error_dev(amr->amr_dv, "Inquiry returned more "
    643  1.55.2.3  jdolecek 			    "drives (%d) than the array can handle (%zu)\n",
    644  1.55.2.3  jdolecek 			    aex->ae_numldrives,
    645  1.55.2.3  jdolecek 			    __arraycount(aex->ae_drivestate));
    646      1.32  christos 			aex->ae_numldrives = __arraycount(aex->ae_drivestate);
    647      1.32  christos 		}
    648       1.1        ad 		if (aex->ae_numldrives > AMR_MAX_UNITS) {
    649      1.55  jakllsch 			aprint_error_dev(amr->amr_dv,
    650  1.55.2.3  jdolecek 			    "adjust AMR_MAX_UNITS to %d (currently %d)\n",
    651  1.55.2.3  jdolecek 			    AMR_MAX_UNITS, amr->amr_numdrives);
    652       1.1        ad 			amr->amr_numdrives = AMR_MAX_UNITS;
    653       1.1        ad 		} else
    654       1.1        ad 			amr->amr_numdrives = aex->ae_numldrives;
    655       1.1        ad 
    656       1.1        ad 		for (i = 0; i < amr->amr_numdrives; i++) {
    657       1.1        ad 			amr->amr_drive[i].al_size =
    658       1.1        ad 			    le32toh(aex->ae_drivesize[i]);
    659       1.1        ad 			amr->amr_drive[i].al_state = aex->ae_drivestate[i];
    660       1.1        ad 			amr->amr_drive[i].al_properties = aex->ae_driveprop[i];
    661       1.1        ad 		}
    662       1.1        ad 
    663       1.1        ad 		return (0);
    664       1.1        ad 	}
    665       1.1        ad 
    666       1.1        ad 	/*
    667       1.1        ad 	 * Try 8LD extended ENQUIRY to get the controller signature.  Once
    668       1.1        ad 	 * found, search for a product description.
    669       1.1        ad 	 */
    670       1.9        ad 	ae = amr_enquire(amr, AMR_CMD_EXT_ENQUIRY2, 0, 0, amr->amr_enqbuf);
    671       1.9        ad 	if (ae != NULL) {
    672       1.1        ad 		i = 0;
    673       1.1        ad 		sig = le32toh(ae->ae_signature);
    674       1.1        ad 
    675       1.1        ad 		while (i < sizeof(amr_typestr) / sizeof(amr_typestr[0])) {
    676       1.1        ad 			if (amr_typestr[i].at_sig == sig)
    677       1.1        ad 				break;
    678       1.1        ad 			i++;
    679       1.1        ad 		}
    680       1.1        ad 		if (i == sizeof(amr_typestr) / sizeof(amr_typestr[0])) {
    681      1.26  christos 			snprintf(sbuf, sizeof(sbuf),
    682      1.20    itojun 			    "unknown ENQUIRY2 sig (0x%08x)", sig);
    683      1.26  christos 			prodstr = sbuf;
    684       1.1        ad 		} else
    685       1.1        ad 			prodstr = amr_typestr[i].at_str;
    686       1.1        ad 	} else {
    687       1.9        ad 		ae = amr_enquire(amr, AMR_CMD_ENQUIRY, 0, 0, amr->amr_enqbuf);
    688       1.9        ad 		if (ae == NULL) {
    689  1.55.2.3  jdolecek 			aprint_error_dev(amr->amr_dv,
    690  1.55.2.3  jdolecek 			    "unsupported controller\n");
    691       1.1        ad 			return (-1);
    692       1.1        ad 		}
    693       1.1        ad 
    694       1.1        ad 		switch (PCI_PRODUCT(pa->pa_id)) {
    695       1.1        ad 		case PCI_PRODUCT_AMI_MEGARAID:
    696       1.1        ad 			prodstr = "Series 428";
    697       1.1        ad 			break;
    698       1.1        ad 		case PCI_PRODUCT_AMI_MEGARAID2:
    699       1.1        ad 			prodstr = "Series 434";
    700       1.1        ad 			break;
    701       1.1        ad 		default:
    702  1.55.2.3  jdolecek 			snprintf(sbuf, sizeof(sbuf),
    703  1.55.2.3  jdolecek 			    "unknown PCI dev (0x%04x)",
    704       1.1        ad 			    PCI_PRODUCT(pa->pa_id));
    705      1.26  christos 			prodstr = sbuf;
    706       1.1        ad 			break;
    707       1.1        ad 		}
    708       1.1        ad 	}
    709       1.1        ad 
    710       1.9        ad 	/*
    711       1.9        ad 	 * HP NetRaid controllers have a special encoding of the firmware
    712       1.9        ad 	 * and BIOS versions.  The AMI version seems to have it as strings
    713       1.9        ad 	 * whereas the HP version does it with a leading uppercase character
    714       1.9        ad 	 * and two binary numbers.
    715       1.9        ad 	*/
    716       1.9        ad 	aa = &ae->ae_adapter;
    717       1.9        ad 
    718       1.9        ad 	if (aa->aa_firmware[2] >= 'A' && aa->aa_firmware[2] <= 'Z' &&
    719       1.9        ad 	    aa->aa_firmware[1] <  ' ' && aa->aa_firmware[0] <  ' ' &&
    720       1.9        ad 	    aa->aa_bios[2] >= 'A' && aa->aa_bios[2] <= 'Z' &&
    721       1.9        ad 	    aa->aa_bios[1] <  ' ' && aa->aa_bios[0] <  ' ') {
    722       1.9        ad 		if (le32toh(ae->ae_signature) == AMR_SIG_438) {
    723       1.9        ad 			/* The AMI 438 is a NetRaid 3si in HP-land. */
    724       1.9        ad 			prodstr = "HP NetRaid 3si";
    725       1.9        ad 		}
    726       1.9        ad 		ishp = 1;
    727       1.9        ad 	} else
    728       1.9        ad 		ishp = 0;
    729       1.9        ad 
    730       1.8   thorpej 	aprint_normal("<%s>\n", prodstr);
    731       1.1        ad 	if (intrstr != NULL)
    732      1.55  jakllsch 		aprint_normal_dev(amr->amr_dv, "interrupting at %s\n",
    733       1.1        ad 		    intrstr);
    734       1.1        ad 
    735       1.9        ad 	if (ishp)
    736  1.55.2.3  jdolecek 		aprint_normal_dev(amr->amr_dv, "firmware <%c.%02d.%02d>, "
    737  1.55.2.3  jdolecek 		    "BIOS <%c.%02d.%02d>, %dMB RAM\n", aa->aa_firmware[2],
    738       1.9        ad 		     aa->aa_firmware[1], aa->aa_firmware[0], aa->aa_bios[2],
    739       1.9        ad 		     aa->aa_bios[1], aa->aa_bios[0], aa->aa_memorysize);
    740       1.9        ad 	else
    741  1.55.2.3  jdolecek 		aprint_normal_dev(amr->amr_dv, "firmware <%.4s>, BIOS <%.4s>, "
    742  1.55.2.3  jdolecek 		    "%dMB RAM\n", aa->aa_firmware, aa->aa_bios,
    743       1.9        ad 		    aa->aa_memorysize);
    744       1.9        ad 
    745       1.9        ad 	amr->amr_maxqueuecnt = aa->aa_maxio;
    746       1.1        ad 
    747       1.1        ad 	/*
    748       1.1        ad 	 * Record state of logical drives.
    749       1.1        ad 	 */
    750      1.32  christos 	if (ae->ae_ldrv.al_numdrives > __arraycount(ae->ae_ldrv.al_size)) {
    751  1.55.2.3  jdolecek 		aprint_error_dev(amr->amr_dv, "Inquiry returned more drives "
    752  1.55.2.3  jdolecek 		    "(%d) than the array can handle (%zu)\n",
    753  1.55.2.3  jdolecek 		    ae->ae_ldrv.al_numdrives,
    754  1.55.2.3  jdolecek 		    __arraycount(ae->ae_ldrv.al_size));
    755      1.32  christos 		ae->ae_ldrv.al_numdrives = __arraycount(ae->ae_ldrv.al_size);
    756      1.32  christos 	}
    757       1.1        ad 	if (ae->ae_ldrv.al_numdrives > AMR_MAX_UNITS) {
    758  1.55.2.3  jdolecek 		aprint_error_dev(amr->amr_dv,
    759  1.55.2.3  jdolecek 		    "adjust AMR_MAX_UNITS to %d (currently %d)\n",
    760  1.55.2.3  jdolecek 		    ae->ae_ldrv.al_numdrives, AMR_MAX_UNITS);
    761       1.1        ad 		amr->amr_numdrives = AMR_MAX_UNITS;
    762       1.1        ad 	} else
    763       1.1        ad 		amr->amr_numdrives = ae->ae_ldrv.al_numdrives;
    764       1.1        ad 
    765      1.32  christos 	for (i = 0; i < amr->amr_numdrives; i++) {
    766       1.1        ad 		amr->amr_drive[i].al_size = le32toh(ae->ae_ldrv.al_size[i]);
    767       1.1        ad 		amr->amr_drive[i].al_state = ae->ae_ldrv.al_state[i];
    768       1.1        ad 		amr->amr_drive[i].al_properties = ae->ae_ldrv.al_properties[i];
    769       1.1        ad 	}
    770       1.1        ad 
    771       1.1        ad 	return (0);
    772       1.1        ad }
    773       1.1        ad 
    774       1.1        ad /*
    775       1.1        ad  * Flush the internal cache on each configured controller.  Called at
    776       1.1        ad  * shutdown time.
    777       1.1        ad  */
    778      1.27   thorpej static void
    779      1.41  christos amr_shutdown(void *cookie)
    780       1.1        ad {
    781      1.36    bouyer 	extern struct cfdriver amr_cd;
    782       1.1        ad 	struct amr_softc *amr;
    783       1.1        ad 	struct amr_ccb *ac;
    784  1.55.2.3  jdolecek 	int i, rv;
    785       1.1        ad 
    786       1.1        ad 	for (i = 0; i < amr_cd.cd_ndevs; i++) {
    787      1.49   tsutsui 		if ((amr = device_lookup_private(&amr_cd, i)) == NULL)
    788       1.1        ad 			continue;
    789       1.1        ad 
    790       1.1        ad 		if ((rv = amr_ccb_alloc(amr, &ac)) == 0) {
    791       1.9        ad 			ac->ac_cmd.mb_command = AMR_CMD_FLUSH;
    792       1.1        ad 			rv = amr_ccb_poll(amr, ac, 30000);
    793       1.1        ad 			amr_ccb_free(amr, ac);
    794       1.1        ad 		}
    795       1.1        ad 		if (rv != 0)
    796  1.55.2.3  jdolecek 			aprint_error_dev(amr->amr_dv,
    797  1.55.2.3  jdolecek 			    "unable to flush cache (%d)\n", rv);
    798       1.1        ad 	}
    799       1.1        ad }
    800       1.1        ad 
    801       1.1        ad /*
    802       1.1        ad  * Interrupt service routine.
    803       1.1        ad  */
    804      1.27   thorpej static int
    805       1.1        ad amr_intr(void *cookie)
    806       1.1        ad {
    807       1.1        ad 	struct amr_softc *amr;
    808       1.1        ad 	struct amr_ccb *ac;
    809       1.9        ad 	struct amr_mailbox_resp mbox;
    810       1.1        ad 	u_int i, forus, idx;
    811       1.1        ad 
    812       1.1        ad 	amr = cookie;
    813       1.1        ad 	forus = 0;
    814       1.1        ad 
    815  1.55.2.3  jdolecek 	mutex_spin_enter(&amr->amr_mutex);
    816  1.55.2.3  jdolecek 
    817       1.1        ad 	while ((*amr->amr_get_work)(amr, &mbox) == 0) {
    818       1.1        ad 		/* Iterate over completed commands in this result. */
    819       1.1        ad 		for (i = 0; i < mbox.mb_nstatus; i++) {
    820       1.1        ad 			idx = mbox.mb_completed[i] - 1;
    821       1.1        ad 			ac = amr->amr_ccbs + idx;
    822       1.1        ad 
    823       1.1        ad 			if (idx >= amr->amr_maxqueuecnt) {
    824       1.1        ad 				printf("%s: bad status (bogus ID: %u=%u)\n",
    825      1.55  jakllsch 				    device_xname(amr->amr_dv), i, idx);
    826       1.1        ad 				continue;
    827       1.1        ad 			}
    828       1.1        ad 
    829       1.1        ad 			if ((ac->ac_flags & AC_ACTIVE) == 0) {
    830       1.1        ad 				printf("%s: bad status (not active; 0x04%x)\n",
    831      1.55  jakllsch 				    device_xname(amr->amr_dv), ac->ac_flags);
    832       1.1        ad 				continue;
    833       1.1        ad 			}
    834       1.1        ad 
    835       1.1        ad 			ac->ac_status = mbox.mb_status;
    836       1.1        ad 			ac->ac_flags = (ac->ac_flags & ~AC_ACTIVE) |
    837       1.1        ad 			    AC_COMPLETE;
    838      1.10        ad 			TAILQ_REMOVE(&amr->amr_ccb_active, ac, ac_chain.tailq);
    839      1.10        ad 
    840      1.10        ad 			if ((ac->ac_flags & AC_MOAN) != 0)
    841      1.10        ad 				printf("%s: ccb %d completed\n",
    842      1.55  jakllsch 				    device_xname(amr->amr_dv), ac->ac_ident);
    843       1.1        ad 
    844       1.1        ad 			/* Pass notification to upper layers. */
    845  1.55.2.3  jdolecek 			mutex_spin_exit(&amr->amr_mutex);
    846  1.55.2.3  jdolecek 			if (ac->ac_handler != NULL) {
    847       1.1        ad 				(*ac->ac_handler)(ac);
    848  1.55.2.3  jdolecek 			} else {
    849  1.55.2.3  jdolecek 				mutex_enter(&ac->ac_mutex);
    850  1.55.2.3  jdolecek 				cv_signal(&ac->ac_cv);
    851  1.55.2.3  jdolecek 				mutex_exit(&ac->ac_mutex);
    852  1.55.2.3  jdolecek 			}
    853  1.55.2.3  jdolecek 			mutex_spin_enter(&amr->amr_mutex);
    854       1.1        ad 		}
    855       1.1        ad 		forus = 1;
    856       1.1        ad 	}
    857       1.1        ad 
    858  1.55.2.3  jdolecek 	mutex_spin_exit(&amr->amr_mutex);
    859  1.55.2.3  jdolecek 
    860       1.1        ad 	if (forus)
    861       1.1        ad 		amr_ccb_enqueue(amr, NULL);
    862       1.9        ad 
    863       1.1        ad 	return (forus);
    864       1.1        ad }
    865       1.1        ad 
    866       1.1        ad /*
    867       1.9        ad  * Watchdog thread.
    868       1.9        ad  */
    869      1.27   thorpej static void
    870  1.55.2.3  jdolecek amr_quartz_thread(void *cookie)
    871  1.55.2.3  jdolecek {
    872  1.55.2.3  jdolecek 	struct amr_softc *amr;
    873  1.55.2.3  jdolecek 	struct amr_ccb *ac;
    874  1.55.2.3  jdolecek 
    875  1.55.2.3  jdolecek 	amr = cookie;
    876  1.55.2.3  jdolecek 
    877  1.55.2.3  jdolecek 	for (;;) {
    878  1.55.2.3  jdolecek 		mutex_enter(&thread_mutex);
    879  1.55.2.3  jdolecek 		cv_timedwait(&thread_cv, &thread_mutex, AMR_WDOG_TICKS);
    880  1.55.2.3  jdolecek 		mutex_exit(&thread_mutex);
    881  1.55.2.3  jdolecek 
    882  1.55.2.3  jdolecek 		if ((amr->amr_flags & AMRF_THREAD_EXIT) != 0) {
    883  1.55.2.3  jdolecek 			amr->amr_flags ^= AMRF_THREAD_EXIT;
    884  1.55.2.3  jdolecek 			mutex_enter(&thread_mutex);
    885  1.55.2.3  jdolecek 			cv_signal(&thread_cv);
    886  1.55.2.3  jdolecek 			mutex_exit(&thread_mutex);
    887  1.55.2.3  jdolecek 			kthread_exit(0);
    888  1.55.2.3  jdolecek 		}
    889  1.55.2.3  jdolecek 
    890  1.55.2.3  jdolecek 		if (amr_intr(amr) == 0)
    891  1.55.2.3  jdolecek 			amr_ccb_enqueue(amr, NULL);
    892  1.55.2.3  jdolecek 
    893  1.55.2.3  jdolecek 		mutex_spin_enter(&amr->amr_mutex);
    894  1.55.2.3  jdolecek 		ac = TAILQ_FIRST(&amr->amr_ccb_active);
    895  1.55.2.3  jdolecek 		while (ac != NULL) {
    896  1.55.2.3  jdolecek 			if (ac->ac_start_time + AMR_TIMEOUT > time_uptime)
    897  1.55.2.3  jdolecek 				break;
    898  1.55.2.3  jdolecek 			if ((ac->ac_flags & AC_MOAN) == 0) {
    899  1.55.2.3  jdolecek 				printf("%s: ccb %d timed out; mailbox:\n",
    900  1.55.2.3  jdolecek 				    device_xname(amr->amr_dv), ac->ac_ident);
    901  1.55.2.3  jdolecek 				amr_ccb_dump(amr, ac);
    902  1.55.2.3  jdolecek 				ac->ac_flags |= AC_MOAN;
    903  1.55.2.3  jdolecek 			}
    904  1.55.2.3  jdolecek 			ac = TAILQ_NEXT(ac, ac_chain.tailq);
    905  1.55.2.3  jdolecek 		}
    906  1.55.2.3  jdolecek 		mutex_spin_exit(&amr->amr_mutex);
    907  1.55.2.3  jdolecek 	}
    908  1.55.2.3  jdolecek }
    909  1.55.2.3  jdolecek 
    910  1.55.2.3  jdolecek static void
    911  1.55.2.3  jdolecek amr_std_thread(void *cookie)
    912       1.9        ad {
    913       1.9        ad 	struct amr_softc *amr;
    914       1.9        ad 	struct amr_ccb *ac;
    915       1.9        ad 	struct amr_logdrive *al;
    916       1.9        ad 	struct amr_enquiry *ae;
    917  1.55.2.3  jdolecek 	int rv, i;
    918       1.9        ad 
    919       1.9        ad 	amr = cookie;
    920       1.9        ad 	ae = amr->amr_enqbuf;
    921       1.9        ad 
    922       1.9        ad 	for (;;) {
    923  1.55.2.3  jdolecek 		mutex_enter(&thread_mutex);
    924  1.55.2.3  jdolecek 		cv_timedwait(&thread_cv, &thread_mutex, AMR_WDOG_TICKS);
    925  1.55.2.3  jdolecek 		mutex_exit(&thread_mutex);
    926       1.9        ad 
    927       1.9        ad 		if ((amr->amr_flags & AMRF_THREAD_EXIT) != 0) {
    928       1.9        ad 			amr->amr_flags ^= AMRF_THREAD_EXIT;
    929  1.55.2.3  jdolecek 			mutex_enter(&thread_mutex);
    930  1.55.2.3  jdolecek 			cv_signal(&thread_cv);
    931  1.55.2.3  jdolecek 			mutex_exit(&thread_mutex);
    932       1.9        ad 			kthread_exit(0);
    933       1.9        ad 		}
    934       1.9        ad 
    935  1.55.2.3  jdolecek 		if (amr_intr(amr) == 0)
    936  1.55.2.3  jdolecek 			amr_ccb_enqueue(amr, NULL);
    937  1.55.2.3  jdolecek 
    938  1.55.2.3  jdolecek 		mutex_spin_enter(&amr->amr_mutex);
    939      1.13        ad 		ac = TAILQ_FIRST(&amr->amr_ccb_active);
    940      1.13        ad 		while (ac != NULL) {
    941      1.35    kardel 			if (ac->ac_start_time + AMR_TIMEOUT > time_uptime)
    942      1.10        ad 				break;
    943      1.10        ad 			if ((ac->ac_flags & AC_MOAN) == 0) {
    944      1.10        ad 				printf("%s: ccb %d timed out; mailbox:\n",
    945      1.55  jakllsch 				    device_xname(amr->amr_dv), ac->ac_ident);
    946      1.10        ad 				amr_ccb_dump(amr, ac);
    947      1.10        ad 				ac->ac_flags |= AC_MOAN;
    948      1.10        ad 			}
    949      1.13        ad 			ac = TAILQ_NEXT(ac, ac_chain.tailq);
    950      1.10        ad 		}
    951  1.55.2.3  jdolecek 		mutex_spin_exit(&amr->amr_mutex);
    952       1.9        ad 
    953       1.9        ad 		if ((rv = amr_ccb_alloc(amr, &ac)) != 0) {
    954       1.9        ad 			printf("%s: ccb_alloc failed (%d)\n",
    955      1.55  jakllsch  			    device_xname(amr->amr_dv), rv);
    956       1.9        ad 			continue;
    957       1.9        ad 		}
    958       1.9        ad 
    959       1.9        ad 		ac->ac_cmd.mb_command = AMR_CMD_ENQUIRY;
    960       1.9        ad 
    961       1.9        ad 		rv = amr_ccb_map(amr, ac, amr->amr_enqbuf,
    962      1.36    bouyer 		    AMR_ENQUIRY_BUFSIZE, AC_XFER_IN);
    963       1.9        ad 		if (rv != 0) {
    964      1.55  jakllsch 			aprint_error_dev(amr->amr_dv, "ccb_map failed (%d)\n",
    965      1.47    cegger  			    rv);
    966       1.9        ad 			amr_ccb_free(amr, ac);
    967       1.9        ad 			continue;
    968       1.9        ad 		}
    969       1.9        ad 
    970       1.9        ad 		rv = amr_ccb_wait(amr, ac);
    971       1.9        ad 		amr_ccb_unmap(amr, ac);
    972       1.9        ad 		if (rv != 0) {
    973  1.55.2.3  jdolecek 			aprint_error_dev(amr->amr_dv,
    974  1.55.2.3  jdolecek 			    "enquiry failed (st=%d)\n", ac->ac_status);
    975       1.9        ad 			continue;
    976       1.9        ad 		}
    977       1.9        ad 		amr_ccb_free(amr, ac);
    978       1.9        ad 
    979       1.9        ad 		al = amr->amr_drive;
    980      1.32  christos 		for (i = 0; i < __arraycount(ae->ae_ldrv.al_state); i++, al++) {
    981       1.9        ad 			if (al->al_dv == NULL)
    982       1.9        ad 				continue;
    983       1.9        ad 			if (al->al_state == ae->ae_ldrv.al_state[i])
    984       1.9        ad 				continue;
    985       1.9        ad 
    986       1.9        ad 			printf("%s: state changed: %s -> %s\n",
    987      1.47    cegger 			    device_xname(al->al_dv),
    988       1.9        ad 			    amr_drive_state(al->al_state, NULL),
    989       1.9        ad 			    amr_drive_state(ae->ae_ldrv.al_state[i], NULL));
    990       1.9        ad 
    991       1.9        ad 			al->al_state = ae->ae_ldrv.al_state[i];
    992       1.9        ad 		}
    993       1.9        ad 	}
    994       1.9        ad }
    995       1.9        ad 
    996       1.9        ad /*
    997       1.9        ad  * Return a text description of a logical drive's current state.
    998       1.9        ad  */
    999       1.9        ad const char *
   1000       1.9        ad amr_drive_state(int state, int *happy)
   1001       1.9        ad {
   1002       1.9        ad 	const char *str;
   1003       1.9        ad 
   1004       1.9        ad 	state = AMR_DRV_CURSTATE(state);
   1005       1.9        ad 	if (state >= sizeof(amr_dstate) / sizeof(amr_dstate[0])) {
   1006       1.9        ad 		if (happy)
   1007       1.9        ad 			*happy = 1;
   1008       1.9        ad 		str = "status unknown";
   1009       1.9        ad 	} else {
   1010       1.9        ad 		if (happy)
   1011       1.9        ad 			*happy = amr_dstate[state].ds_happy;
   1012       1.9        ad 		str = amr_dstate[state].ds_descr;
   1013       1.9        ad 	}
   1014       1.9        ad 
   1015       1.9        ad 	return (str);
   1016       1.9        ad }
   1017       1.9        ad 
   1018       1.9        ad /*
   1019       1.1        ad  * Run a generic enquiry-style command.
   1020       1.1        ad  */
   1021      1.27   thorpej static void *
   1022       1.1        ad amr_enquire(struct amr_softc *amr, u_int8_t cmd, u_int8_t cmdsub,
   1023      1.26  christos 	    u_int8_t cmdqual, void *sbuf)
   1024       1.1        ad {
   1025       1.1        ad 	struct amr_ccb *ac;
   1026       1.1        ad 	u_int8_t *mb;
   1027       1.1        ad 	int rv;
   1028       1.1        ad 
   1029       1.1        ad 	if (amr_ccb_alloc(amr, &ac) != 0)
   1030       1.1        ad 		return (NULL);
   1031       1.1        ad 
   1032       1.1        ad 	/* Build the command proper. */
   1033       1.9        ad 	mb = (u_int8_t *)&ac->ac_cmd;
   1034       1.1        ad 	mb[0] = cmd;
   1035       1.1        ad 	mb[2] = cmdsub;
   1036       1.1        ad 	mb[3] = cmdqual;
   1037       1.1        ad 
   1038      1.36    bouyer 	rv = amr_ccb_map(amr, ac, sbuf, AMR_ENQUIRY_BUFSIZE, AC_XFER_IN);
   1039       1.9        ad 	if (rv == 0) {
   1040       1.1        ad 		rv = amr_ccb_poll(amr, ac, 2000);
   1041       1.1        ad 		amr_ccb_unmap(amr, ac);
   1042       1.1        ad 	}
   1043       1.1        ad 	amr_ccb_free(amr, ac);
   1044       1.1        ad 
   1045      1.26  christos 	return (rv ? NULL : sbuf);
   1046       1.1        ad }
   1047       1.1        ad 
   1048       1.1        ad /*
   1049       1.1        ad  * Allocate and initialise a CCB.
   1050       1.1        ad  */
   1051       1.1        ad int
   1052       1.1        ad amr_ccb_alloc(struct amr_softc *amr, struct amr_ccb **acp)
   1053       1.1        ad {
   1054  1.55.2.3  jdolecek 	mutex_spin_enter(&amr->amr_mutex);
   1055       1.9        ad 	if ((*acp = SLIST_FIRST(&amr->amr_ccb_freelist)) == NULL) {
   1056  1.55.2.3  jdolecek 		mutex_spin_exit(&amr->amr_mutex);
   1057       1.1        ad 		return (EAGAIN);
   1058       1.1        ad 	}
   1059       1.1        ad 	SLIST_REMOVE_HEAD(&amr->amr_ccb_freelist, ac_chain.slist);
   1060  1.55.2.3  jdolecek 	mutex_spin_exit(&amr->amr_mutex);
   1061       1.1        ad 
   1062       1.1        ad 	return (0);
   1063       1.1        ad }
   1064       1.1        ad 
   1065       1.1        ad /*
   1066       1.1        ad  * Free a CCB.
   1067       1.1        ad  */
   1068       1.1        ad void
   1069       1.1        ad amr_ccb_free(struct amr_softc *amr, struct amr_ccb *ac)
   1070       1.1        ad {
   1071       1.9        ad 	memset(&ac->ac_cmd, 0, sizeof(ac->ac_cmd));
   1072       1.9        ad 	ac->ac_cmd.mb_ident = ac->ac_ident + 1;
   1073       1.9        ad 	ac->ac_cmd.mb_busy = 1;
   1074       1.9        ad 	ac->ac_handler = NULL;
   1075       1.1        ad 	ac->ac_flags = 0;
   1076       1.1        ad 
   1077  1.55.2.3  jdolecek 	mutex_spin_enter(&amr->amr_mutex);
   1078       1.1        ad 	SLIST_INSERT_HEAD(&amr->amr_ccb_freelist, ac, ac_chain.slist);
   1079  1.55.2.3  jdolecek 	mutex_spin_exit(&amr->amr_mutex);
   1080       1.1        ad }
   1081       1.1        ad 
   1082       1.1        ad /*
   1083       1.1        ad  * If a CCB is specified, enqueue it.  Pull CCBs off the software queue in
   1084       1.1        ad  * the order that they were enqueued and try to submit their command blocks
   1085       1.1        ad  * to the controller for execution.
   1086       1.1        ad  */
   1087       1.1        ad void
   1088       1.1        ad amr_ccb_enqueue(struct amr_softc *amr, struct amr_ccb *ac)
   1089       1.1        ad {
   1090  1.55.2.3  jdolecek 	if (ac != NULL) {
   1091  1.55.2.3  jdolecek 		mutex_spin_enter(&amr->amr_mutex);
   1092       1.1        ad 		SIMPLEQ_INSERT_TAIL(&amr->amr_ccb_queue, ac, ac_chain.simpleq);
   1093  1.55.2.3  jdolecek 		mutex_spin_exit(&amr->amr_mutex);
   1094       1.1        ad 	}
   1095       1.1        ad 
   1096  1.55.2.3  jdolecek 	while (SIMPLEQ_FIRST(&amr->amr_ccb_queue) != NULL) {
   1097  1.55.2.3  jdolecek 		mutex_spin_enter(&amr->amr_mutex);
   1098  1.55.2.3  jdolecek 		if ((ac = SIMPLEQ_FIRST(&amr->amr_ccb_queue)) != NULL) {
   1099  1.55.2.3  jdolecek 			if ((*amr->amr_submit)(amr, ac) != 0) {
   1100  1.55.2.3  jdolecek 				mutex_spin_exit(&amr->amr_mutex);
   1101  1.55.2.3  jdolecek 				break;
   1102  1.55.2.3  jdolecek 			}
   1103  1.55.2.3  jdolecek 			SIMPLEQ_REMOVE_HEAD(&amr->amr_ccb_queue,
   1104  1.55.2.3  jdolecek 			    ac_chain.simpleq);
   1105  1.55.2.3  jdolecek 			TAILQ_INSERT_TAIL(&amr->amr_ccb_active, ac,
   1106  1.55.2.3  jdolecek 			    ac_chain.tailq);
   1107  1.55.2.3  jdolecek 		}
   1108  1.55.2.3  jdolecek 		mutex_spin_exit(&amr->amr_mutex);
   1109  1.55.2.3  jdolecek 	}
   1110       1.1        ad }
   1111       1.1        ad 
   1112       1.1        ad /*
   1113       1.1        ad  * Map the specified CCB's data buffer onto the bus, and fill the
   1114       1.1        ad  * scatter-gather list.
   1115       1.1        ad  */
   1116       1.1        ad int
   1117       1.1        ad amr_ccb_map(struct amr_softc *amr, struct amr_ccb *ac, void *data, int size,
   1118      1.36    bouyer 	    int tflag)
   1119       1.1        ad {
   1120       1.1        ad 	struct amr_sgentry *sge;
   1121       1.9        ad 	struct amr_mailbox_cmd *mb;
   1122       1.1        ad 	int nsegs, i, rv, sgloff;
   1123       1.1        ad 	bus_dmamap_t xfer;
   1124      1.36    bouyer 	int dmaflag = 0;
   1125       1.1        ad 
   1126       1.1        ad 	xfer = ac->ac_xfer_map;
   1127       1.1        ad 
   1128       1.1        ad 	rv = bus_dmamap_load(amr->amr_dmat, xfer, data, size, NULL,
   1129       1.1        ad 	    BUS_DMA_NOWAIT);
   1130       1.1        ad 	if (rv != 0)
   1131       1.1        ad 		return (rv);
   1132       1.1        ad 
   1133       1.9        ad 	mb = &ac->ac_cmd;
   1134       1.1        ad 	ac->ac_xfer_size = size;
   1135      1.36    bouyer 	ac->ac_flags |= (tflag & (AC_XFER_OUT | AC_XFER_IN));
   1136       1.1        ad 	sgloff = AMR_SGL_SIZE * ac->ac_ident;
   1137       1.1        ad 
   1138      1.36    bouyer 	if (tflag & AC_XFER_OUT)
   1139      1.36    bouyer 		dmaflag |= BUS_DMASYNC_PREWRITE;
   1140      1.36    bouyer 	if (tflag & AC_XFER_IN)
   1141      1.36    bouyer 		dmaflag |= BUS_DMASYNC_PREREAD;
   1142      1.36    bouyer 
   1143       1.1        ad 	/* We don't need to use a scatter/gather list for just 1 segment. */
   1144       1.1        ad 	nsegs = xfer->dm_nsegs;
   1145       1.1        ad 	if (nsegs == 1) {
   1146       1.1        ad 		mb->mb_nsgelem = 0;
   1147       1.1        ad 		mb->mb_physaddr = htole32(xfer->dm_segs[0].ds_addr);
   1148       1.1        ad 		ac->ac_flags |= AC_NOSGL;
   1149       1.1        ad 	} else {
   1150       1.1        ad 		mb->mb_nsgelem = nsegs;
   1151       1.1        ad 		mb->mb_physaddr = htole32(amr->amr_sgls_paddr + sgloff);
   1152       1.1        ad 
   1153      1.44  christos 		sge = (struct amr_sgentry *)((char *)amr->amr_sgls + sgloff);
   1154       1.1        ad 		for (i = 0; i < nsegs; i++, sge++) {
   1155       1.1        ad 			sge->sge_addr = htole32(xfer->dm_segs[i].ds_addr);
   1156       1.1        ad 			sge->sge_count = htole32(xfer->dm_segs[i].ds_len);
   1157       1.1        ad 		}
   1158       1.1        ad 	}
   1159       1.1        ad 
   1160      1.36    bouyer 	bus_dmamap_sync(amr->amr_dmat, xfer, 0, ac->ac_xfer_size, dmaflag);
   1161       1.1        ad 
   1162       1.1        ad 	if ((ac->ac_flags & AC_NOSGL) == 0)
   1163       1.1        ad 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, sgloff,
   1164       1.1        ad 		    AMR_SGL_SIZE, BUS_DMASYNC_PREWRITE);
   1165       1.1        ad 
   1166       1.1        ad 	return (0);
   1167       1.1        ad }
   1168       1.1        ad 
   1169       1.1        ad /*
   1170       1.1        ad  * Unmap the specified CCB's data buffer.
   1171       1.1        ad  */
   1172       1.1        ad void
   1173       1.1        ad amr_ccb_unmap(struct amr_softc *amr, struct amr_ccb *ac)
   1174       1.1        ad {
   1175      1.36    bouyer 	int dmaflag = 0;
   1176      1.36    bouyer 
   1177      1.36    bouyer 	if (ac->ac_flags & AC_XFER_IN)
   1178      1.36    bouyer 		dmaflag |= BUS_DMASYNC_POSTREAD;
   1179      1.36    bouyer 	if (ac->ac_flags & AC_XFER_OUT)
   1180      1.36    bouyer 		dmaflag |= BUS_DMASYNC_POSTWRITE;
   1181       1.1        ad 
   1182       1.1        ad 	if ((ac->ac_flags & AC_NOSGL) == 0)
   1183       1.1        ad 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap,
   1184       1.1        ad 		    AMR_SGL_SIZE * ac->ac_ident, AMR_SGL_SIZE,
   1185       1.1        ad 		    BUS_DMASYNC_POSTWRITE);
   1186       1.1        ad 	bus_dmamap_sync(amr->amr_dmat, ac->ac_xfer_map, 0, ac->ac_xfer_size,
   1187      1.36    bouyer 	    dmaflag);
   1188       1.1        ad 	bus_dmamap_unload(amr->amr_dmat, ac->ac_xfer_map);
   1189       1.1        ad }
   1190       1.1        ad 
   1191       1.1        ad /*
   1192       1.1        ad  * Submit a command to the controller and poll on completion.  Return
   1193  1.55.2.3  jdolecek  * non-zero on timeout or error.
   1194       1.1        ad  */
   1195       1.1        ad int
   1196       1.1        ad amr_ccb_poll(struct amr_softc *amr, struct amr_ccb *ac, int timo)
   1197       1.1        ad {
   1198  1.55.2.3  jdolecek 	int rv, i;
   1199       1.1        ad 
   1200  1.55.2.3  jdolecek 	mutex_spin_enter(&amr->amr_mutex);
   1201  1.55.2.3  jdolecek 	if ((rv = (*amr->amr_submit)(amr, ac)) != 0) {
   1202  1.55.2.3  jdolecek 		mutex_spin_exit(&amr->amr_mutex);
   1203       1.1        ad 		return (rv);
   1204  1.55.2.3  jdolecek 	}
   1205      1.10        ad 	TAILQ_INSERT_TAIL(&amr->amr_ccb_active, ac, ac_chain.tailq);
   1206  1.55.2.3  jdolecek 	mutex_spin_exit(&amr->amr_mutex);
   1207       1.1        ad 
   1208  1.55.2.3  jdolecek 	for (i = timo * 10; i > 0; i--) {
   1209       1.1        ad 		amr_intr(amr);
   1210       1.1        ad 		if ((ac->ac_flags & AC_COMPLETE) != 0)
   1211       1.1        ad 			break;
   1212       1.1        ad 		DELAY(100);
   1213       1.1        ad 	}
   1214       1.1        ad 
   1215  1.55.2.3  jdolecek 	if (i == 0)
   1216  1.55.2.3  jdolecek 		printf("%s: polled operation timed out after %d ms\n",
   1217  1.55.2.3  jdolecek 		       device_xname(amr->amr_dv), timo);
   1218  1.55.2.3  jdolecek 
   1219  1.55.2.3  jdolecek 	return ((i == 0 || ac->ac_status != 0) ? EIO : 0);
   1220       1.1        ad }
   1221       1.1        ad 
   1222       1.1        ad /*
   1223       1.9        ad  * Submit a command to the controller and sleep on completion.  Return
   1224       1.9        ad  * non-zero on error.
   1225       1.9        ad  */
   1226       1.9        ad int
   1227       1.9        ad amr_ccb_wait(struct amr_softc *amr, struct amr_ccb *ac)
   1228       1.9        ad {
   1229       1.9        ad 	amr_ccb_enqueue(amr, ac);
   1230  1.55.2.3  jdolecek 	mutex_enter(&ac->ac_mutex);
   1231  1.55.2.3  jdolecek 	cv_wait(&ac->ac_cv, &ac->ac_mutex);
   1232  1.55.2.3  jdolecek 	mutex_exit(&ac->ac_mutex);
   1233       1.9        ad 
   1234       1.9        ad 	return (ac->ac_status != 0 ? EIO : 0);
   1235       1.9        ad }
   1236       1.9        ad 
   1237      1.27   thorpej #if 0
   1238       1.9        ad /*
   1239       1.1        ad  * Wait for the mailbox to become available.
   1240       1.1        ad  */
   1241      1.27   thorpej static int
   1242       1.1        ad amr_mbox_wait(struct amr_softc *amr)
   1243       1.1        ad {
   1244       1.1        ad 	int timo;
   1245       1.1        ad 
   1246       1.1        ad 	for (timo = 10000; timo != 0; timo--) {
   1247       1.9        ad 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1248       1.9        ad 		    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
   1249       1.9        ad 		if (amr->amr_mbox->mb_cmd.mb_busy == 0)
   1250       1.1        ad 			break;
   1251       1.1        ad 		DELAY(100);
   1252       1.1        ad 	}
   1253       1.1        ad 
   1254       1.9        ad 	if (timo == 0)
   1255      1.55  jakllsch 		printf("%s: controller wedged\n", device_xname(amr->amr_dv));
   1256       1.1        ad 
   1257       1.9        ad 	return (timo != 0 ? 0 : EAGAIN);
   1258       1.1        ad }
   1259      1.27   thorpej #endif
   1260       1.1        ad 
   1261       1.1        ad /*
   1262       1.1        ad  * Tell the controller that the mailbox contains a valid command.  Must be
   1263       1.1        ad  * called with interrupts blocked.
   1264       1.1        ad  */
   1265      1.27   thorpej static int
   1266       1.1        ad amr_quartz_submit(struct amr_softc *amr, struct amr_ccb *ac)
   1267       1.1        ad {
   1268  1.55.2.3  jdolecek 	int i = 0;
   1269       1.1        ad 	u_int32_t v;
   1270       1.1        ad 
   1271       1.9        ad 	amr->amr_mbox->mb_poll = 0;
   1272       1.9        ad 	amr->amr_mbox->mb_ack = 0;
   1273  1.55.2.3  jdolecek 
   1274       1.9        ad 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1275  1.55.2.3  jdolecek 	    sizeof(struct amr_mailbox),
   1276  1.55.2.3  jdolecek 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1277  1.55.2.3  jdolecek 
   1278  1.55.2.3  jdolecek 	v = amr_inl(amr, AMR_QREG_ODB);
   1279       1.9        ad 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1280       1.9        ad 	    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
   1281  1.55.2.3  jdolecek 	while ((amr->amr_mbox->mb_cmd.mb_busy != 0) && (i++ < 10)) {
   1282  1.55.2.3  jdolecek 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1283  1.55.2.3  jdolecek 		    sizeof(struct amr_mailbox), BUS_DMASYNC_PREREAD);
   1284  1.55.2.3  jdolecek 		/* This is a no-op read that flushes pending mailbox updates */
   1285  1.55.2.3  jdolecek 		v = amr_inl(amr, AMR_QREG_ODB);
   1286  1.55.2.3  jdolecek 		DELAY(1);
   1287  1.55.2.3  jdolecek 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1288  1.55.2.3  jdolecek 		    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
   1289  1.55.2.3  jdolecek 	}
   1290  1.55.2.3  jdolecek 
   1291       1.9        ad 	if (amr->amr_mbox->mb_cmd.mb_busy != 0)
   1292       1.9        ad 		return (EAGAIN);
   1293       1.9        ad 
   1294       1.1        ad 	v = amr_inl(amr, AMR_QREG_IDB);
   1295      1.13        ad 	if ((v & AMR_QIDB_SUBMIT) != 0) {
   1296       1.9        ad 		amr->amr_mbox->mb_cmd.mb_busy = 0;
   1297       1.9        ad 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1298       1.9        ad 		    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
   1299  1.55.2.3  jdolecek 		printf("%s: submit failed\n", device_xname(amr->amr_dv));
   1300       1.9        ad 		return (EAGAIN);
   1301       1.9        ad 	}
   1302       1.1        ad 
   1303      1.10        ad 	amr->amr_mbox->mb_segment = 0;
   1304      1.10        ad 	memcpy(&amr->amr_mbox->mb_cmd, &ac->ac_cmd, sizeof(ac->ac_cmd));
   1305      1.10        ad 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1306      1.10        ad 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
   1307      1.10        ad 
   1308      1.35    kardel 	ac->ac_start_time = time_uptime;
   1309       1.1        ad 	ac->ac_flags |= AC_ACTIVE;
   1310  1.55.2.3  jdolecek 
   1311      1.13        ad 	amr_outl(amr, AMR_QREG_IDB,
   1312      1.13        ad 	    (amr->amr_mbox_paddr + 16) | AMR_QIDB_SUBMIT);
   1313  1.55.2.3  jdolecek 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1314  1.55.2.3  jdolecek 	    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTWRITE);
   1315  1.55.2.3  jdolecek 
   1316       1.1        ad 	return (0);
   1317       1.1        ad }
   1318       1.1        ad 
   1319      1.27   thorpej static int
   1320       1.1        ad amr_std_submit(struct amr_softc *amr, struct amr_ccb *ac)
   1321       1.1        ad {
   1322       1.1        ad 
   1323       1.9        ad 	amr->amr_mbox->mb_poll = 0;
   1324       1.9        ad 	amr->amr_mbox->mb_ack = 0;
   1325  1.55.2.3  jdolecek 
   1326       1.9        ad 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1327       1.9        ad 	    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
   1328  1.55.2.3  jdolecek 
   1329       1.9        ad 	if (amr->amr_mbox->mb_cmd.mb_busy != 0)
   1330       1.9        ad 		return (EAGAIN);
   1331       1.9        ad 
   1332       1.9        ad 	if ((amr_inb(amr, AMR_SREG_MBOX_BUSY) & AMR_SMBOX_BUSY_FLAG) != 0) {
   1333       1.9        ad 		amr->amr_mbox->mb_cmd.mb_busy = 0;
   1334       1.9        ad 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1335       1.9        ad 		    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
   1336       1.9        ad 		return (EAGAIN);
   1337       1.9        ad 	}
   1338       1.1        ad 
   1339      1.10        ad 	amr->amr_mbox->mb_segment = 0;
   1340      1.10        ad 	memcpy(&amr->amr_mbox->mb_cmd, &ac->ac_cmd, sizeof(ac->ac_cmd));
   1341  1.55.2.3  jdolecek 
   1342      1.10        ad 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1343      1.10        ad 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREWRITE);
   1344      1.10        ad 
   1345      1.35    kardel 	ac->ac_start_time = time_uptime;
   1346       1.1        ad 	ac->ac_flags |= AC_ACTIVE;
   1347       1.1        ad 	amr_outb(amr, AMR_SREG_CMD, AMR_SCMD_POST);
   1348  1.55.2.3  jdolecek 
   1349  1.55.2.3  jdolecek 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1350  1.55.2.3  jdolecek 	    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTWRITE);
   1351  1.55.2.3  jdolecek 
   1352       1.1        ad 	return (0);
   1353       1.1        ad }
   1354       1.1        ad 
   1355       1.1        ad /*
   1356       1.1        ad  * Claim any work that the controller has completed; acknowledge completion,
   1357       1.1        ad  * save details of the completion in (mbsave).  Must be called with
   1358       1.1        ad  * interrupts blocked.
   1359       1.1        ad  */
   1360      1.27   thorpej static int
   1361       1.9        ad amr_quartz_get_work(struct amr_softc *amr, struct amr_mailbox_resp *mbsave)
   1362       1.1        ad {
   1363  1.55.2.3  jdolecek 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1364  1.55.2.3  jdolecek 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREREAD);
   1365       1.1        ad 
   1366       1.1        ad 	/* Work waiting for us? */
   1367       1.1        ad 	if (amr_inl(amr, AMR_QREG_ODB) != AMR_QODB_READY)
   1368       1.1        ad 		return (-1);
   1369       1.1        ad 
   1370       1.9        ad 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1371       1.9        ad 	    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
   1372       1.9        ad 
   1373       1.1        ad 	/* Save the mailbox, which contains a list of completed commands. */
   1374       1.9        ad 	memcpy(mbsave, &amr->amr_mbox->mb_resp, sizeof(*mbsave));
   1375       1.9        ad 
   1376       1.1        ad 	/* Ack the interrupt and mailbox transfer. */
   1377       1.1        ad 	amr_outl(amr, AMR_QREG_ODB, AMR_QODB_READY);
   1378       1.9        ad 	amr_outl(amr, AMR_QREG_IDB, (amr->amr_mbox_paddr+16) | AMR_QIDB_ACK);
   1379       1.1        ad 
   1380       1.1        ad 	/*
   1381       1.1        ad 	 * This waits for the controller to notice that we've taken the
   1382       1.1        ad 	 * command from it.  It's very inefficient, and we shouldn't do it,
   1383       1.1        ad 	 * but if we remove this code, we stop completing commands under
   1384       1.1        ad 	 * load.
   1385       1.1        ad 	 *
   1386       1.1        ad 	 * Peter J says we shouldn't do this.  The documentation says we
   1387       1.1        ad 	 * should.  Who is right?
   1388       1.1        ad 	 */
   1389       1.1        ad 	while ((amr_inl(amr, AMR_QREG_IDB) & AMR_QIDB_ACK) != 0)
   1390      1.13        ad 		DELAY(10);
   1391       1.1        ad 
   1392       1.1        ad 	return (0);
   1393       1.1        ad }
   1394       1.1        ad 
   1395      1.27   thorpej static int
   1396       1.9        ad amr_std_get_work(struct amr_softc *amr, struct amr_mailbox_resp *mbsave)
   1397       1.1        ad {
   1398       1.1        ad 	u_int8_t istat;
   1399       1.1        ad 
   1400  1.55.2.3  jdolecek 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1401  1.55.2.3  jdolecek 	    sizeof(struct amr_mailbox), BUS_DMASYNC_PREREAD);
   1402  1.55.2.3  jdolecek 
   1403       1.1        ad 	/* Check for valid interrupt status. */
   1404       1.1        ad 	if (((istat = amr_inb(amr, AMR_SREG_INTR)) & AMR_SINTR_VALID) == 0)
   1405       1.1        ad 		return (-1);
   1406       1.1        ad 
   1407       1.1        ad 	/* Ack the interrupt. */
   1408       1.1        ad 	amr_outb(amr, AMR_SREG_INTR, istat);
   1409       1.1        ad 
   1410       1.9        ad 	bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, 0,
   1411       1.9        ad 	    sizeof(struct amr_mailbox), BUS_DMASYNC_POSTREAD);
   1412       1.9        ad 
   1413       1.1        ad 	/* Save mailbox, which contains a list of completed commands. */
   1414       1.9        ad 	memcpy(mbsave, &amr->amr_mbox->mb_resp, sizeof(*mbsave));
   1415       1.9        ad 
   1416       1.1        ad 	/* Ack mailbox transfer. */
   1417       1.1        ad 	amr_outb(amr, AMR_SREG_CMD, AMR_SCMD_ACKINTR);
   1418       1.1        ad 
   1419       1.1        ad 	return (0);
   1420      1.10        ad }
   1421      1.10        ad 
   1422      1.27   thorpej static void
   1423      1.10        ad amr_ccb_dump(struct amr_softc *amr, struct amr_ccb *ac)
   1424      1.10        ad {
   1425      1.10        ad 	int i;
   1426      1.10        ad 
   1427      1.55  jakllsch 	printf("%s: ", device_xname(amr->amr_dv));
   1428      1.10        ad 	for (i = 0; i < 4; i++)
   1429      1.10        ad 		printf("%08x ", ((u_int32_t *)&ac->ac_cmd)[i]);
   1430      1.10        ad 	printf("\n");
   1431       1.1        ad }
   1432      1.36    bouyer 
   1433      1.36    bouyer static int
   1434      1.41  christos amropen(dev_t dev, int flag, int mode, struct lwp *l)
   1435      1.36    bouyer {
   1436      1.36    bouyer 	struct amr_softc *amr;
   1437  1.55.2.3  jdolecek 
   1438      1.49   tsutsui 	if ((amr = device_lookup_private(&amr_cd, minor(dev))) == NULL)
   1439      1.36    bouyer 		return (ENXIO);
   1440      1.36    bouyer 	if ((amr->amr_flags & AMRF_OPEN) != 0)
   1441      1.36    bouyer 		return (EBUSY);
   1442  1.55.2.3  jdolecek 
   1443      1.36    bouyer 	amr->amr_flags |= AMRF_OPEN;
   1444      1.36    bouyer 	return (0);
   1445      1.36    bouyer }
   1446      1.36    bouyer 
   1447      1.36    bouyer static int
   1448      1.41  christos amrclose(dev_t dev, int flag, int mode, struct lwp *l)
   1449      1.36    bouyer {
   1450      1.36    bouyer 	struct amr_softc *amr;
   1451      1.36    bouyer 
   1452      1.49   tsutsui 	amr = device_lookup_private(&amr_cd, minor(dev));
   1453      1.36    bouyer 	amr->amr_flags &= ~AMRF_OPEN;
   1454      1.36    bouyer 	return (0);
   1455      1.36    bouyer }
   1456      1.36    bouyer 
   1457  1.55.2.3  jdolecek /* used below to correct for a firmware bug */
   1458  1.55.2.3  jdolecek static unsigned long
   1459  1.55.2.3  jdolecek amrioctl_buflen(unsigned long len)
   1460  1.55.2.3  jdolecek {
   1461  1.55.2.3  jdolecek 	if (len <= 4 * 1024)
   1462  1.55.2.3  jdolecek 		return (4 * 1024);
   1463  1.55.2.3  jdolecek 	if (len <= 8 * 1024)
   1464  1.55.2.3  jdolecek 		return (8 * 1024);
   1465  1.55.2.3  jdolecek 	if (len <= 32 * 1024)
   1466  1.55.2.3  jdolecek 		return (32 * 1024);
   1467  1.55.2.3  jdolecek 	if (len <= 64 * 1024)
   1468  1.55.2.3  jdolecek 		return (64 * 1024);
   1469  1.55.2.3  jdolecek 	return (len);
   1470  1.55.2.3  jdolecek }
   1471  1.55.2.3  jdolecek 
   1472      1.36    bouyer static int
   1473      1.44  christos amrioctl(dev_t dev, u_long cmd, void *data, int flag,
   1474      1.40      elad     struct lwp *l)
   1475      1.36    bouyer {
   1476      1.36    bouyer 	struct amr_softc *amr;
   1477      1.36    bouyer 	struct amr_user_ioctl *au;
   1478      1.36    bouyer 	struct amr_ccb *ac;
   1479      1.36    bouyer 	struct amr_mailbox_ioctl *mbi;
   1480      1.36    bouyer 	unsigned long au_length;
   1481      1.36    bouyer 	uint8_t *au_cmd;
   1482      1.36    bouyer 	int error;
   1483      1.36    bouyer 	void *dp = NULL, *au_buffer;
   1484      1.36    bouyer 
   1485      1.49   tsutsui 	amr = device_lookup_private(&amr_cd, minor(dev));
   1486      1.36    bouyer 
   1487      1.36    bouyer 	/* This should be compatible with the FreeBSD interface */
   1488      1.36    bouyer 
   1489      1.36    bouyer 	switch (cmd) {
   1490      1.36    bouyer 	case AMR_IO_VERSION:
   1491      1.36    bouyer 		*(int *)data = AMR_IO_VERSION_NUMBER;
   1492      1.36    bouyer 		return 0;
   1493      1.36    bouyer 	case AMR_IO_COMMAND:
   1494      1.43      elad 		error = kauth_authorize_device_passthru(l->l_cred, dev,
   1495      1.43      elad 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_ALL, data);
   1496      1.40      elad 		if (error)
   1497      1.40      elad 			return (error);
   1498      1.37  christos 
   1499      1.36    bouyer 		au = (struct amr_user_ioctl *)data;
   1500      1.36    bouyer 		au_cmd = au->au_cmd;
   1501      1.36    bouyer 		au_buffer = au->au_buffer;
   1502      1.36    bouyer 		au_length = au->au_length;
   1503      1.36    bouyer 		break;
   1504      1.36    bouyer 	default:
   1505      1.36    bouyer 		return ENOTTY;
   1506      1.36    bouyer 	}
   1507      1.36    bouyer 
   1508      1.36    bouyer 	if (au_cmd[0] == AMR_CMD_PASS) {
   1509      1.36    bouyer 		/* not yet */
   1510      1.36    bouyer 		return EOPNOTSUPP;
   1511      1.36    bouyer 	}
   1512      1.36    bouyer 
   1513  1.55.2.1       tls 	if (au_length <= 0 || au_length > device_maxphys(amr->amr_dv) ||
   1514  1.55.2.1       tls 	    au_cmd[0] == 0x06)
   1515      1.36    bouyer 		return (EINVAL);
   1516      1.36    bouyer 
   1517      1.36    bouyer 	/*
   1518      1.36    bouyer 	 * allocate kernel memory for data, doing I/O directly to user
   1519  1.55.2.3  jdolecek 	 * buffer isn't that easy.  Correct allocation size for a bug
   1520  1.55.2.3  jdolecek 	 * in at least some versions of the device firmware, by using
   1521  1.55.2.3  jdolecek 	 * the amrioctl_buflen() function, defined above.
   1522      1.36    bouyer 	 */
   1523  1.55.2.3  jdolecek 	dp = malloc(amrioctl_buflen(au_length), M_DEVBUF, M_WAITOK|M_ZERO);
   1524      1.36    bouyer 	if (dp == NULL)
   1525      1.36    bouyer 		return ENOMEM;
   1526      1.36    bouyer 	if ((error = copyin(au_buffer, dp, au_length)) != 0)
   1527      1.36    bouyer 		goto out;
   1528      1.36    bouyer 
   1529      1.36    bouyer 	/* direct command to controller */
   1530      1.36    bouyer 	while (amr_ccb_alloc(amr, &ac) != 0) {
   1531  1.55.2.3  jdolecek 		mutex_enter(&thread_mutex);
   1532  1.55.2.3  jdolecek 		error = cv_timedwait_sig(&thread_cv, &thread_mutex, hz);
   1533  1.55.2.3  jdolecek 		mutex_exit(&thread_mutex);
   1534      1.36    bouyer 		if (error == EINTR)
   1535      1.36    bouyer 			goto out;
   1536      1.36    bouyer 	}
   1537      1.36    bouyer 
   1538      1.36    bouyer 	mbi = (struct amr_mailbox_ioctl *)&ac->ac_cmd;
   1539      1.36    bouyer 	mbi->mb_command = au_cmd[0];
   1540      1.36    bouyer 	mbi->mb_channel = au_cmd[1];
   1541      1.36    bouyer 	mbi->mb_param = au_cmd[2];
   1542      1.36    bouyer 	mbi->mb_pad[0] = au_cmd[3];
   1543      1.36    bouyer 	mbi->mb_drive = au_cmd[4];
   1544      1.36    bouyer 	error = amr_ccb_map(amr, ac, dp, (int)au_length,
   1545      1.36    bouyer 	    AC_XFER_IN | AC_XFER_OUT);
   1546      1.36    bouyer 	if (error == 0) {
   1547      1.36    bouyer 		error = amr_ccb_wait(amr, ac);
   1548      1.36    bouyer 		amr_ccb_unmap(amr, ac);
   1549      1.36    bouyer 		if (error == 0)
   1550      1.36    bouyer 			error = copyout(dp, au_buffer, au_length);
   1551      1.36    bouyer 
   1552      1.36    bouyer 	}
   1553      1.36    bouyer 	amr_ccb_free(amr, ac);
   1554      1.36    bouyer out:
   1555      1.36    bouyer 	free(dp, M_DEVBUF);
   1556      1.36    bouyer 	return (error);
   1557      1.36    bouyer }
   1558  1.55.2.3  jdolecek 
   1559  1.55.2.3  jdolecek MODULE(MODULE_CLASS_DRIVER, amr, "pci");
   1560  1.55.2.3  jdolecek 
   1561  1.55.2.3  jdolecek #ifdef _MODULE
   1562  1.55.2.3  jdolecek #include "ioconf.c"
   1563  1.55.2.3  jdolecek #endif
   1564  1.55.2.3  jdolecek 
   1565  1.55.2.3  jdolecek static int
   1566  1.55.2.3  jdolecek amr_modcmd(modcmd_t cmd, void *opaque)
   1567  1.55.2.3  jdolecek {
   1568  1.55.2.3  jdolecek 	int error = 0;
   1569  1.55.2.3  jdolecek 
   1570  1.55.2.3  jdolecek #ifdef _MODULE
   1571  1.55.2.3  jdolecek 	switch (cmd) {
   1572  1.55.2.3  jdolecek 	case MODULE_CMD_INIT:
   1573  1.55.2.3  jdolecek 		error = config_init_component(cfdriver_ioconf_amr,
   1574  1.55.2.3  jdolecek 		    cfattach_ioconf_amr, cfdata_ioconf_amr);
   1575  1.55.2.3  jdolecek 		break;
   1576  1.55.2.3  jdolecek 	case MODULE_CMD_FINI:
   1577  1.55.2.3  jdolecek 		error = config_fini_component(cfdriver_ioconf_amr,
   1578  1.55.2.3  jdolecek 		    cfattach_ioconf_amr, cfdata_ioconf_amr);
   1579  1.55.2.3  jdolecek 		break;
   1580  1.55.2.3  jdolecek 	default:
   1581  1.55.2.3  jdolecek 		error = ENOTTY;
   1582  1.55.2.3  jdolecek 		break;
   1583  1.55.2.3  jdolecek 	}
   1584  1.55.2.3  jdolecek #endif
   1585  1.55.2.3  jdolecek 
   1586  1.55.2.3  jdolecek 	return error;
   1587  1.55.2.3  jdolecek }
   1588  1.55.2.3  jdolecek 
   1589