Home | History | Annotate | Line # | Download | only in pci
twe.c revision 1.42
      1  1.42   thorpej /*	$NetBSD: twe.c,v 1.42 2003/09/21 19:20:18 thorpej Exp $	*/
      2   1.1        ad 
      3   1.1        ad /*-
      4  1.42   thorpej  * Copyright (c) 2000, 2001, 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.42   thorpej  * by Andrew Doran; and by Jason R. Thorpe of Wasabi Systems, Inc.
      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) 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: twe.c,v 1.1 2000/05/24 23:35:23 msmith Exp
     66   1.1        ad  */
     67   1.1        ad 
     68   1.1        ad /*
     69   1.1        ad  * Driver for the 3ware Escalade family of RAID controllers.
     70   1.1        ad  */
     71  1.21     lukem 
     72  1.21     lukem #include <sys/cdefs.h>
     73  1.42   thorpej __KERNEL_RCSID(0, "$NetBSD: twe.c,v 1.42 2003/09/21 19:20:18 thorpej Exp $");
     74   1.1        ad 
     75   1.1        ad #include <sys/param.h>
     76   1.1        ad #include <sys/systm.h>
     77   1.1        ad #include <sys/kernel.h>
     78   1.1        ad #include <sys/device.h>
     79   1.1        ad #include <sys/queue.h>
     80   1.1        ad #include <sys/proc.h>
     81   1.1        ad #include <sys/buf.h>
     82   1.1        ad #include <sys/endian.h>
     83   1.1        ad #include <sys/malloc.h>
     84  1.33  christos #include <sys/conf.h>
     85   1.1        ad #include <sys/disk.h>
     86   1.1        ad 
     87   1.1        ad #include <uvm/uvm_extern.h>
     88   1.1        ad 
     89   1.1        ad #include <machine/bswap.h>
     90   1.1        ad #include <machine/bus.h>
     91   1.1        ad 
     92   1.1        ad #include <dev/pci/pcireg.h>
     93   1.1        ad #include <dev/pci/pcivar.h>
     94   1.1        ad #include <dev/pci/pcidevs.h>
     95   1.1        ad #include <dev/pci/twereg.h>
     96   1.1        ad #include <dev/pci/twevar.h>
     97  1.33  christos #include <dev/pci/tweio.h>
     98   1.1        ad 
     99   1.1        ad #define	PCI_CBIO	0x10
    100   1.1        ad 
    101   1.1        ad static void	twe_aen_handler(struct twe_ccb *, int);
    102   1.1        ad static void	twe_attach(struct device *, struct device *, void *);
    103   1.1        ad static int	twe_init_connection(struct twe_softc *);
    104   1.1        ad static int	twe_intr(void *);
    105   1.1        ad static int	twe_match(struct device *, struct cfdata *, void *);
    106   1.7        ad static int	twe_param_get(struct twe_softc *, int, int, size_t,
    107  1.41   thorpej 		    void (*)(struct twe_ccb *, int), struct twe_param **);
    108  1.41   thorpej static int	twe_param_get_1(struct twe_softc *, int, int, uint8_t *);
    109  1.41   thorpej static int	twe_param_get_2(struct twe_softc *, int, int, uint16_t *);
    110  1.41   thorpej static int	twe_param_get_4(struct twe_softc *, int, int, uint32_t *);
    111  1.33  christos static int	twe_param_set(struct twe_softc *, int, int, size_t, void *);
    112   1.1        ad static void	twe_poll(struct twe_softc *);
    113   1.1        ad static int	twe_print(void *, const char *);
    114   1.1        ad static int	twe_reset(struct twe_softc *);
    115   1.1        ad static int	twe_submatch(struct device *, struct cfdata *, void *);
    116   1.1        ad static int	twe_status_check(struct twe_softc *, u_int);
    117   1.1        ad static int	twe_status_wait(struct twe_softc *, u_int, int);
    118  1.38  jdolecek static void	twe_describe_controller(struct twe_softc *);
    119   1.1        ad 
    120  1.22        ad static inline u_int32_t	twe_inl(struct twe_softc *, int);
    121  1.33  christos static inline void twe_outl(struct twe_softc *, int, u_int32_t);
    122  1.33  christos 
    123  1.33  christos dev_type_open(tweopen);
    124  1.33  christos dev_type_close(tweclose);
    125  1.33  christos dev_type_ioctl(tweioctl);
    126  1.33  christos 
    127  1.33  christos const struct cdevsw twe_cdevsw = {
    128  1.33  christos 	tweopen, tweclose, noread, nowrite, tweioctl,
    129  1.33  christos 	nostop, notty, nopoll, nommap,
    130  1.33  christos };
    131  1.33  christos 
    132  1.33  christos extern struct	cfdriver twe_cd;
    133  1.22        ad 
    134  1.30   thorpej CFATTACH_DECL(twe, sizeof(struct twe_softc),
    135  1.31   thorpej     twe_match, twe_attach, NULL, NULL);
    136   1.1        ad 
    137  1.40   thorpej /*
    138  1.40   thorpej  * Tables to convert numeric codes to strings.
    139  1.40   thorpej  */
    140  1.40   thorpej const struct twe_code_table twe_table_status[] = {
    141  1.40   thorpej 	{ 0x00,	"successful completion" },
    142  1.40   thorpej 
    143  1.40   thorpej 	/* info */
    144  1.40   thorpej 	{ 0x42,	"command in progress" },
    145  1.40   thorpej 	{ 0x6c,	"retrying interface CRC error from UDMA command" },
    146  1.40   thorpej 
    147  1.40   thorpej 	/* warning */
    148  1.40   thorpej 	{ 0x81,	"redundant/inconsequential request ignored" },
    149  1.40   thorpej 	{ 0x8e,	"failed to write zeroes to LBA 0" },
    150  1.40   thorpej 	{ 0x8f,	"failed to profile TwinStor zones" },
    151  1.40   thorpej 
    152  1.40   thorpej 	/* fatal */
    153  1.40   thorpej 	{ 0xc1,	"aborted due to system command or reconfiguration" },
    154  1.40   thorpej 	{ 0xc4,	"aborted" },
    155  1.40   thorpej 	{ 0xc5,	"access error" },
    156  1.40   thorpej 	{ 0xc6,	"access violation" },
    157  1.40   thorpej 	{ 0xc7,	"device failure" },	/* high byte may be port # */
    158  1.40   thorpej 	{ 0xc8,	"controller error" },
    159  1.40   thorpej 	{ 0xc9,	"timed out" },
    160  1.40   thorpej 	{ 0xcb,	"invalid unit number" },
    161  1.40   thorpej 	{ 0xcf,	"unit not available" },
    162  1.40   thorpej 	{ 0xd2,	"undefined opcode" },
    163  1.40   thorpej 	{ 0xdb,	"request incompatible with unit" },
    164  1.40   thorpej 	{ 0xdc,	"invalid request" },
    165  1.40   thorpej 	{ 0xff,	"firmware error, reset requested" },
    166  1.40   thorpej 
    167  1.40   thorpej 	{ 0,	NULL }
    168  1.40   thorpej };
    169  1.40   thorpej 
    170  1.40   thorpej const struct twe_code_table twe_table_unitstate[] = {
    171  1.40   thorpej 	{ TWE_PARAM_UNITSTATUS_Normal,		"Normal" },
    172  1.40   thorpej 	{ TWE_PARAM_UNITSTATUS_Initialising,	"Initializing" },
    173  1.40   thorpej 	{ TWE_PARAM_UNITSTATUS_Degraded,	"Degraded" },
    174  1.40   thorpej 	{ TWE_PARAM_UNITSTATUS_Rebuilding,	"Rebuilding" },
    175  1.40   thorpej 	{ TWE_PARAM_UNITSTATUS_Verifying,	"Verifying" },
    176  1.40   thorpej 	{ TWE_PARAM_UNITSTATUS_Corrupt,		"Corrupt" },
    177  1.40   thorpej 	{ TWE_PARAM_UNITSTATUS_Missing,		"Missing" },
    178  1.40   thorpej 
    179  1.40   thorpej 	{ 0,					NULL }
    180  1.40   thorpej };
    181  1.40   thorpej 
    182  1.40   thorpej const struct twe_code_table twe_table_unittype[] = {
    183  1.40   thorpej 	/* array descriptor configuration */
    184  1.40   thorpej 	{ TWE_AD_CONFIG_RAID0,			"RAID0" },
    185  1.40   thorpej 	{ TWE_AD_CONFIG_RAID1,			"RAID1" },
    186  1.40   thorpej 	{ TWE_AD_CONFIG_TwinStor,		"TwinStor" },
    187  1.40   thorpej 	{ TWE_AD_CONFIG_RAID5,			"RAID5" },
    188  1.40   thorpej 	{ TWE_AD_CONFIG_RAID10,			"RAID10" },
    189  1.40   thorpej 
    190  1.40   thorpej 	{ 0,					NULL }
    191  1.40   thorpej };
    192  1.40   thorpej 
    193  1.40   thorpej const struct twe_code_table twe_table_stripedepth[] = {
    194  1.40   thorpej 	{ TWE_AD_STRIPE_4k,			"4K" },
    195  1.40   thorpej 	{ TWE_AD_STRIPE_8k,			"8K" },
    196  1.40   thorpej 	{ TWE_AD_STRIPE_16k,			"16K" },
    197  1.40   thorpej 	{ TWE_AD_STRIPE_32k,			"32K" },
    198  1.40   thorpej 	{ TWE_AD_STRIPE_64k,			"64K" },
    199  1.40   thorpej 
    200  1.40   thorpej 	{ 0,					NULL }
    201  1.40   thorpej };
    202  1.40   thorpej 
    203  1.40   thorpej const char *
    204  1.40   thorpej twe_describe_code(const struct twe_code_table *table, uint32_t code)
    205  1.40   thorpej {
    206  1.40   thorpej 
    207  1.40   thorpej 	for (; table->string != NULL; table++) {
    208  1.40   thorpej 		if (table->code == code)
    209  1.40   thorpej 			return (table->string);
    210  1.40   thorpej 	}
    211  1.40   thorpej 	return (NULL);
    212  1.40   thorpej }
    213  1.40   thorpej 
    214   1.1        ad struct {
    215  1.26  christos 	const u_int	aen;	/* High byte indicates type of message */
    216   1.1        ad 	const char	*desc;
    217   1.1        ad } static const twe_aen_names[] = {
    218   1.1        ad 	{ 0x0000, "queue empty" },
    219   1.1        ad 	{ 0x0001, "soft reset" },
    220   1.3        ad 	{ 0x0102, "degraded mirror" },
    221   1.1        ad 	{ 0x0003, "controller error" },
    222   1.3        ad 	{ 0x0104, "rebuild fail" },
    223   1.3        ad 	{ 0x0105, "rebuild done" },
    224   1.3        ad 	{ 0x0106, "incompatible unit" },
    225  1.26  christos 	{ 0x0107, "initialisation done" },
    226  1.26  christos 	{ 0x0108, "unclean shutdown detected" },
    227  1.26  christos 	{ 0x0109, "drive timeout" },
    228   1.3        ad 	{ 0x010a, "drive error" },
    229   1.3        ad 	{ 0x010b, "rebuild started" },
    230  1.14        ad 	{ 0x010c, "init started" },
    231  1.26  christos 	{ 0x010d, "logical unit deleted" },
    232  1.26  christos 	{ 0x020f, "SMART threshold exceeded" },
    233  1.26  christos 	{ 0x0015, "table undefined" },	/* XXX: Not in FreeBSD's table */
    234  1.26  christos 	{ 0x0221, "ATA UDMA downgrade" },
    235  1.26  christos 	{ 0x0222, "ATA UDMA upgrade" },
    236  1.26  christos 	{ 0x0222, "ATA UDMA upgrade" },
    237  1.26  christos 	{ 0x0223, "Sector repair occurred" },
    238  1.26  christos 	{ 0x0024, "SBUF integrity check failure" },
    239  1.26  christos 	{ 0x0225, "lost cached write" },
    240  1.26  christos 	{ 0x0226, "drive ECC error detected" },
    241  1.26  christos 	{ 0x0227, "DCB checksum error" },
    242  1.26  christos 	{ 0x0228, "DCB unsupported version" },
    243  1.26  christos 	{ 0x0129, "verify started" },
    244  1.26  christos 	{ 0x012a, "verify failed" },
    245  1.26  christos 	{ 0x012b, "verify complete" },
    246  1.26  christos 	{ 0x022c, "overwrote bad sector during rebuild" },
    247  1.26  christos 	{ 0x022d, "encountered bad sector during rebuild" },
    248   1.1        ad 	{ 0x00ff, "aen queue full" },
    249   1.1        ad };
    250   1.1        ad 
    251  1.26  christos /*
    252  1.26  christos  * The high byte of the message above determines the format,
    253  1.26  christos  * currently we know about format 0 (no unit/port specific)
    254  1.26  christos  * format 1 (unit specific message), and format 2 (port specific message).
    255  1.26  christos  */
    256  1.38  jdolecek static const char * const aenfmt[] = {
    257  1.27       kim 	"",		/* No message */
    258  1.26  christos 	"unit %d: ",	/* Unit message */
    259  1.26  christos 	"port %d: "	/* Port message */
    260  1.26  christos };
    261  1.26  christos 
    262  1.26  christos 
    263  1.22        ad static inline u_int32_t
    264  1.22        ad twe_inl(struct twe_softc *sc, int off)
    265  1.22        ad {
    266  1.22        ad 
    267  1.22        ad 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
    268  1.22        ad 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
    269  1.22        ad 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, off));
    270  1.22        ad }
    271  1.22        ad 
    272  1.22        ad static inline void
    273  1.22        ad twe_outl(struct twe_softc *sc, int off, u_int32_t val)
    274  1.22        ad {
    275  1.22        ad 
    276  1.22        ad 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, off, val);
    277  1.22        ad 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
    278  1.22        ad 	    BUS_SPACE_BARRIER_WRITE);
    279  1.22        ad }
    280  1.22        ad 
    281   1.1        ad /*
    282   1.1        ad  * Match a supported board.
    283   1.1        ad  */
    284   1.1        ad static int
    285   1.1        ad twe_match(struct device *parent, struct cfdata *cfdata, void *aux)
    286   1.1        ad {
    287   1.1        ad 	struct pci_attach_args *pa;
    288   1.1        ad 
    289   1.1        ad 	pa = aux;
    290   1.1        ad 
    291   1.1        ad 	return (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3WARE &&
    292  1.10        ad 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_ESCALADE ||
    293  1.10        ad 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_ESCALADE_ASIC));
    294   1.1        ad }
    295   1.1        ad 
    296   1.1        ad /*
    297   1.1        ad  * Attach a supported board.
    298   1.1        ad  *
    299   1.1        ad  * XXX This doesn't fail gracefully.
    300   1.1        ad  */
    301   1.1        ad static void
    302   1.1        ad twe_attach(struct device *parent, struct device *self, void *aux)
    303   1.1        ad {
    304   1.1        ad 	struct pci_attach_args *pa;
    305   1.1        ad 	struct twe_softc *sc;
    306   1.1        ad 	pci_chipset_tag_t pc;
    307   1.1        ad 	pci_intr_handle_t ih;
    308   1.1        ad 	pcireg_t csr;
    309   1.1        ad 	const char *intrstr;
    310   1.1        ad 	int size, i, rv, rseg;
    311  1.23  christos 	size_t max_segs, max_xfer;
    312  1.42   thorpej 	struct twe_param *dtp, *atp;
    313  1.42   thorpej 	struct twe_array_descriptor *ad;
    314  1.42   thorpej 	struct twe_drive *td;
    315   1.1        ad 	bus_dma_segment_t seg;
    316   1.1        ad 	struct twe_cmd *tc;
    317   1.1        ad 	struct twe_attach_args twea;
    318   1.1        ad 	struct twe_ccb *ccb;
    319  1.42   thorpej 	uint16_t dsize;
    320   1.1        ad 
    321   1.1        ad 	sc = (struct twe_softc *)self;
    322   1.1        ad 	pa = aux;
    323   1.1        ad 	pc = pa->pa_pc;
    324   1.1        ad 	sc->sc_dmat = pa->pa_dmat;
    325   1.1        ad 	SIMPLEQ_INIT(&sc->sc_ccb_queue);
    326   1.1        ad 	SLIST_INIT(&sc->sc_ccb_freelist);
    327   1.1        ad 
    328  1.40   thorpej 	aprint_naive(": RAID controller\n");
    329  1.38  jdolecek 	aprint_normal(": 3ware Escalade\n");
    330   1.1        ad 
    331  1.33  christos 	ccb = malloc(sizeof(*ccb) * TWE_MAX_QUEUECNT, M_DEVBUF, M_NOWAIT);
    332  1.33  christos 	if (ccb == NULL) {
    333  1.38  jdolecek 		aprint_error("%s: unable to allocate memory for ccbs\n",
    334  1.33  christos 		    sc->sc_dv.dv_xname);
    335  1.33  christos 		return;
    336  1.33  christos 	}
    337  1.33  christos 
    338   1.1        ad 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    339   1.1        ad 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    340  1.38  jdolecek 		aprint_error("%s: can't map i/o space\n", sc->sc_dv.dv_xname);
    341   1.1        ad 		return;
    342   1.1        ad 	}
    343   1.1        ad 
    344   1.1        ad 	/* Enable the device. */
    345   1.1        ad 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    346   1.1        ad 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    347   1.1        ad 	    csr | PCI_COMMAND_MASTER_ENABLE);
    348   1.1        ad 
    349   1.1        ad 	/* Map and establish the interrupt. */
    350   1.5  sommerfe 	if (pci_intr_map(pa, &ih)) {
    351  1.38  jdolecek 		aprint_error("%s: can't map interrupt\n", sc->sc_dv.dv_xname);
    352   1.1        ad 		return;
    353   1.1        ad 	}
    354  1.38  jdolecek 
    355   1.1        ad 	intrstr = pci_intr_string(pc, ih);
    356   1.1        ad 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, twe_intr, sc);
    357   1.1        ad 	if (sc->sc_ih == NULL) {
    358  1.38  jdolecek 		aprint_error("%s: can't establish interrupt%s%s\n",
    359  1.38  jdolecek 			sc->sc_dv.dv_xname,
    360  1.38  jdolecek 			(intrstr) ? " at " : "",
    361  1.38  jdolecek 			(intrstr) ? intrstr : "");
    362   1.1        ad 		return;
    363   1.1        ad 	}
    364  1.38  jdolecek 
    365   1.1        ad 	if (intrstr != NULL)
    366  1.38  jdolecek 		aprint_normal("%s: interrupting at %s\n",
    367  1.38  jdolecek 			sc->sc_dv.dv_xname, intrstr);
    368   1.1        ad 
    369   1.1        ad 	/*
    370   1.1        ad 	 * Allocate and initialise the command blocks and CCBs.
    371   1.1        ad 	 */
    372   1.7        ad         size = sizeof(struct twe_cmd) * TWE_MAX_QUEUECNT;
    373   1.1        ad 
    374   1.4   thorpej 	if ((rv = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
    375   1.1        ad 	    &rseg, BUS_DMA_NOWAIT)) != 0) {
    376  1.38  jdolecek 		aprint_error("%s: unable to allocate commands, rv = %d\n",
    377   1.1        ad 		    sc->sc_dv.dv_xname, rv);
    378   1.1        ad 		return;
    379   1.1        ad 	}
    380   1.1        ad 
    381   1.1        ad 	if ((rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
    382   1.1        ad 	    (caddr_t *)&sc->sc_cmds,
    383   1.1        ad 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    384  1.38  jdolecek 		aprint_error("%s: unable to map commands, rv = %d\n",
    385   1.1        ad 		    sc->sc_dv.dv_xname, rv);
    386   1.1        ad 		return;
    387   1.1        ad 	}
    388   1.1        ad 
    389   1.1        ad 	if ((rv = bus_dmamap_create(sc->sc_dmat, size, size, 1, 0,
    390   1.1        ad 	    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    391  1.38  jdolecek 		aprint_error("%s: unable to create command DMA map, rv = %d\n",
    392   1.1        ad 		    sc->sc_dv.dv_xname, rv);
    393   1.1        ad 		return;
    394   1.1        ad 	}
    395   1.1        ad 
    396   1.1        ad 	if ((rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_cmds,
    397   1.1        ad 	    size, NULL, BUS_DMA_NOWAIT)) != 0) {
    398  1.38  jdolecek 		aprint_error("%s: unable to load command DMA map, rv = %d\n",
    399   1.1        ad 		    sc->sc_dv.dv_xname, rv);
    400   1.1        ad 		return;
    401   1.1        ad 	}
    402   1.1        ad 
    403   1.1        ad 	sc->sc_cmds_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
    404   1.1        ad 	memset(sc->sc_cmds, 0, size);
    405   1.1        ad 
    406   1.1        ad 	sc->sc_ccbs = ccb;
    407   1.1        ad 	tc = (struct twe_cmd *)sc->sc_cmds;
    408  1.24  christos 	max_segs = twe_get_maxsegs();
    409  1.24  christos 	max_xfer = twe_get_maxxfer(max_segs);
    410   1.1        ad 
    411   1.7        ad 	for (i = 0; i < TWE_MAX_QUEUECNT; i++, tc++, ccb++) {
    412   1.1        ad 		ccb->ccb_cmd = tc;
    413   1.1        ad 		ccb->ccb_cmdid = i;
    414   1.1        ad 		ccb->ccb_flags = 0;
    415  1.23  christos 		rv = bus_dmamap_create(sc->sc_dmat, max_xfer,
    416  1.23  christos 		    max_segs, PAGE_SIZE, 0,
    417   1.4   thorpej 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    418   1.1        ad 		    &ccb->ccb_dmamap_xfer);
    419   1.7        ad 		if (rv != 0) {
    420  1.38  jdolecek 			aprint_error("%s: can't create dmamap, rv = %d\n",
    421   1.7        ad 			    sc->sc_dv.dv_xname, rv);
    422   1.7        ad 			return;
    423   1.7        ad 		}
    424   1.3        ad 		/* Save one CCB for parameter retrieval. */
    425   1.3        ad 		if (i != 0)
    426   1.3        ad 			SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb,
    427   1.3        ad 			    ccb_chain.slist);
    428   1.3        ad 	}
    429   1.1        ad 
    430   1.1        ad 	/* Wait for the controller to become ready. */
    431   1.1        ad 	if (twe_status_wait(sc, TWE_STS_MICROCONTROLLER_READY, 6)) {
    432  1.38  jdolecek 		aprint_error("%s: microcontroller not ready\n",
    433  1.38  jdolecek 			sc->sc_dv.dv_xname);
    434   1.1        ad 		return;
    435   1.1        ad 	}
    436   1.1        ad 
    437  1.22        ad 	twe_outl(sc, TWE_REG_CTL, TWE_CTL_DISABLE_INTRS);
    438   1.1        ad 
    439   1.1        ad 	/* Reset the controller. */
    440   1.1        ad 	if (twe_reset(sc)) {
    441  1.38  jdolecek 		aprint_error("%s: reset failed\n", sc->sc_dv.dv_xname);
    442   1.1        ad 		return;
    443   1.1        ad 	}
    444   1.1        ad 
    445   1.3        ad 	/* Find attached units. */
    446   1.7        ad 	rv = twe_param_get(sc, TWE_PARAM_UNITSUMMARY,
    447  1.38  jdolecek 	    TWE_PARAM_UNITSUMMARY_Status, TWE_MAX_UNITS, NULL, &dtp);
    448   1.7        ad 	if (rv != 0) {
    449  1.38  jdolecek 		aprint_error("%s: can't detect attached units (%d)\n",
    450   1.7        ad 		    sc->sc_dv.dv_xname, rv);
    451   1.1        ad 		return;
    452   1.1        ad 	}
    453   1.1        ad 
    454   1.1        ad 	/* For each detected unit, collect size and store in an array. */
    455   1.3        ad 	for (i = 0, sc->sc_nunits = 0; i < TWE_MAX_UNITS; i++) {
    456  1.42   thorpej 		td = &sc->sc_units[i];
    457  1.42   thorpej 
    458   1.1        ad 		/* Unit present? */
    459   1.3        ad 		if ((dtp->tp_data[i] & TWE_PARAM_UNITSTATUS_Online) == 0) {
    460  1.42   thorpej 			td->td_size = 0;
    461  1.42   thorpej 			td->td_type = 0;
    462  1.42   thorpej 			td->td_stripe = 0;
    463   1.1        ad 	   		continue;
    464   1.1        ad 	   	}
    465   1.1        ad 
    466  1.42   thorpej 		rv = twe_param_get_2(sc, TWE_PARAM_UNITINFO + i,
    467  1.42   thorpej 		    TWE_PARAM_UNITINFO_DescriptorSize, &dsize);
    468  1.42   thorpej 		if (rv != 0) {
    469  1.42   thorpej 			aprint_error("%s: error %d fetching descriptor size "
    470  1.42   thorpej 			    "for unit %d\n", sc->sc_dv.dv_xname, rv, i);
    471  1.42   thorpej 			td->td_size = 0;
    472  1.42   thorpej 			td->td_type = 0;
    473  1.42   thorpej 			td->td_stripe = 0;
    474  1.42   thorpej 			continue;
    475  1.42   thorpej 		}
    476  1.42   thorpej 
    477  1.42   thorpej 		rv = twe_param_get(sc, TWE_PARAM_UNITINFO + i,
    478  1.42   thorpej 		    TWE_PARAM_UNITINFO_Descriptor, dsize - 3, NULL, &atp);
    479  1.42   thorpej 		if (rv != 0) {
    480  1.42   thorpej 			aprint_error("%s: error %d fetching array descriptor "
    481  1.42   thorpej 			    "for unit %d\n", sc->sc_dv.dv_xname, rv, i);
    482  1.42   thorpej 			td->td_size = 0;
    483  1.42   thorpej 			td->td_type = 0;
    484  1.42   thorpej 			td->td_stripe = 0;
    485  1.42   thorpej 			continue;
    486  1.42   thorpej 		}
    487  1.42   thorpej 		ad = (struct twe_array_descriptor *)atp->tp_data;
    488  1.42   thorpej 		td->td_type = ad->configuration;
    489  1.42   thorpej 		td->td_stripe = ad->stripe_size;
    490  1.42   thorpej 		free(atp, M_DEVBUF);
    491  1.42   thorpej 
    492  1.41   thorpej 		rv = twe_param_get_4(sc, TWE_PARAM_UNITINFO + i,
    493  1.42   thorpej 		    TWE_PARAM_UNITINFO_Capacity, &td->td_size);
    494   1.7        ad 		if (rv != 0) {
    495  1.42   thorpej 			aprint_error(
    496  1.42   thorpej 			    "%s: error %d fetching capacity for unit %d\n",
    497   1.7        ad 			    sc->sc_dv.dv_xname, rv, i);
    498  1.42   thorpej 			td->td_size = 0;
    499  1.42   thorpej 			td->td_type = 0;
    500  1.42   thorpej 			td->td_stripe = 0;
    501   1.1        ad 			continue;
    502   1.1        ad 		}
    503   1.1        ad 
    504   1.3        ad 		sc->sc_nunits++;
    505   1.1        ad 	}
    506   1.1        ad 	free(dtp, M_DEVBUF);
    507   1.1        ad 
    508   1.1        ad 	/* Initialise connection with controller and enable interrupts. */
    509   1.1        ad 	twe_init_connection(sc);
    510  1.22        ad 	twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR |
    511   1.1        ad 	    TWE_CTL_UNMASK_RESP_INTR |
    512   1.1        ad 	    TWE_CTL_ENABLE_INTRS);
    513   1.1        ad 
    514  1.38  jdolecek 	twe_describe_controller(sc);
    515  1.38  jdolecek 
    516   1.1        ad 	/* Attach sub-devices. */
    517   1.1        ad 	for (i = 0; i < TWE_MAX_UNITS; i++) {
    518  1.42   thorpej 		td = &sc->sc_units[i];
    519  1.42   thorpej 		if (td->td_size == 0)
    520   1.1        ad 			continue;
    521   1.1        ad 		twea.twea_unit = i;
    522  1.42   thorpej 		td->td_dev = config_found_sm(&sc->sc_dv, &twea, twe_print,
    523  1.42   thorpej 		    twe_submatch);
    524   1.1        ad 	}
    525   1.1        ad }
    526   1.1        ad 
    527   1.1        ad /*
    528   1.1        ad  * Reset the controller.  Currently only useful at attach time; must be
    529   1.1        ad  * called with interrupts blocked.
    530   1.1        ad  */
    531   1.1        ad static int
    532   1.1        ad twe_reset(struct twe_softc *sc)
    533   1.1        ad {
    534  1.41   thorpej 	uint16_t aen;
    535  1.41   thorpej 	u_int status;
    536   1.1        ad 	volatile u_int32_t junk;
    537   1.7        ad 	int got, rv;
    538   1.1        ad 
    539   1.1        ad 	/* Issue a soft reset. */
    540  1.22        ad 	twe_outl(sc, TWE_REG_CTL, TWE_CTL_ISSUE_SOFT_RESET |
    541   1.1        ad 	    TWE_CTL_CLEAR_HOST_INTR |
    542   1.1        ad 	    TWE_CTL_CLEAR_ATTN_INTR |
    543   1.1        ad 	    TWE_CTL_MASK_CMD_INTR |
    544   1.1        ad 	    TWE_CTL_MASK_RESP_INTR |
    545   1.1        ad 	    TWE_CTL_CLEAR_ERROR_STS |
    546   1.1        ad 	    TWE_CTL_DISABLE_INTRS);
    547   1.1        ad 
    548   1.1        ad 	if (twe_status_wait(sc, TWE_STS_ATTN_INTR, 15)) {
    549   1.1        ad 		printf("%s: no attention interrupt\n",
    550   1.1        ad 		    sc->sc_dv.dv_xname);
    551   1.1        ad 		return (-1);
    552   1.1        ad 	}
    553   1.1        ad 
    554   1.1        ad 	/* Pull AENs out of the controller; look for a soft reset AEN. */
    555   1.1        ad 	for (got = 0;;) {
    556  1.41   thorpej 		rv = twe_param_get_2(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode,
    557  1.41   thorpej 		    &aen);
    558   1.7        ad 		if (rv != 0)
    559   1.7        ad 			printf("%s: error %d while draining response queue\n",
    560   1.7        ad 			    sc->sc_dv.dv_xname, rv);
    561  1.41   thorpej 		if (TWE_AEN_CODE(aen) == TWE_AEN_QUEUE_EMPTY)
    562   1.1        ad 			break;
    563  1.41   thorpej 		if (TWE_AEN_CODE(aen) == TWE_AEN_SOFT_RESET)
    564   1.1        ad 			got = 1;
    565   1.1        ad 	}
    566   1.1        ad 	if (!got) {
    567   1.1        ad 		printf("%s: reset not reported\n", sc->sc_dv.dv_xname);
    568   1.1        ad 		return (-1);
    569   1.1        ad 	}
    570   1.1        ad 
    571   1.1        ad 	/* Check controller status. */
    572  1.22        ad 	status = twe_inl(sc, TWE_REG_STS);
    573   1.1        ad 	if (twe_status_check(sc, status)) {
    574   1.1        ad 		printf("%s: controller errors detected\n",
    575   1.1        ad 		    sc->sc_dv.dv_xname);
    576   1.1        ad 		return (-1);
    577   1.1        ad 	}
    578   1.1        ad 
    579   1.1        ad 	/* Drain the response queue. */
    580   1.1        ad 	for (;;) {
    581  1.22        ad 		status = twe_inl(sc, TWE_REG_STS);
    582   1.1        ad 		if (twe_status_check(sc, status) != 0) {
    583   1.1        ad 			printf("%s: can't drain response queue\n",
    584   1.1        ad 			    sc->sc_dv.dv_xname);
    585   1.1        ad 			return (-1);
    586   1.1        ad 		}
    587   1.1        ad 		if ((status & TWE_STS_RESP_QUEUE_EMPTY) != 0)
    588   1.1        ad 			break;
    589  1.22        ad 		junk = twe_inl(sc, TWE_REG_RESP_QUEUE);
    590   1.1        ad 	}
    591   1.1        ad 
    592   1.1        ad 	return (0);
    593   1.1        ad }
    594   1.1        ad 
    595   1.1        ad /*
    596   1.1        ad  * Print autoconfiguration message for a sub-device.
    597   1.1        ad  */
    598   1.1        ad static int
    599   1.1        ad twe_print(void *aux, const char *pnp)
    600   1.1        ad {
    601   1.1        ad 	struct twe_attach_args *twea;
    602   1.1        ad 
    603   1.1        ad 	twea = aux;
    604   1.1        ad 
    605   1.1        ad 	if (pnp != NULL)
    606  1.35   thorpej 		aprint_normal("block device at %s", pnp);
    607  1.35   thorpej 	aprint_normal(" unit %d", twea->twea_unit);
    608   1.1        ad 	return (UNCONF);
    609   1.1        ad }
    610   1.1        ad 
    611   1.1        ad /*
    612   1.1        ad  * Match a sub-device.
    613   1.1        ad  */
    614   1.1        ad static int
    615   1.1        ad twe_submatch(struct device *parent, struct cfdata *cf, void *aux)
    616   1.1        ad {
    617   1.1        ad 	struct twe_attach_args *twea;
    618   1.1        ad 
    619   1.1        ad 	twea = aux;
    620   1.1        ad 
    621   1.1        ad 	if (cf->tweacf_unit != TWECF_UNIT_DEFAULT &&
    622   1.1        ad 	    cf->tweacf_unit != twea->twea_unit)
    623   1.1        ad 		return (0);
    624   1.1        ad 
    625  1.28   thorpej 	return (config_match(parent, cf, aux));
    626   1.1        ad }
    627   1.1        ad 
    628   1.1        ad /*
    629   1.1        ad  * Interrupt service routine.
    630   1.1        ad  */
    631   1.1        ad static int
    632   1.1        ad twe_intr(void *arg)
    633   1.1        ad {
    634   1.1        ad 	struct twe_softc *sc;
    635   1.1        ad 	u_int status;
    636   1.7        ad 	int caught, rv;
    637   1.1        ad 
    638   1.1        ad 	sc = arg;
    639   1.1        ad 	caught = 0;
    640  1.22        ad 	status = twe_inl(sc, TWE_REG_STS);
    641   1.1        ad 	twe_status_check(sc, status);
    642   1.1        ad 
    643   1.1        ad 	/* Host interrupts - purpose unknown. */
    644   1.1        ad 	if ((status & TWE_STS_HOST_INTR) != 0) {
    645  1.38  jdolecek #ifdef DEBUG
    646   1.1        ad 		printf("%s: host interrupt\n", sc->sc_dv.dv_xname);
    647   1.1        ad #endif
    648  1.22        ad 		twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_HOST_INTR);
    649   1.1        ad 		caught = 1;
    650   1.1        ad 	}
    651   1.1        ad 
    652   1.1        ad 	/*
    653   1.1        ad 	 * Attention interrupts, signalled when a controller or child device
    654  1.18       wiz 	 * state change has occurred.
    655   1.1        ad 	 */
    656   1.1        ad 	if ((status & TWE_STS_ATTN_INTR) != 0) {
    657  1.12        ad 		if ((sc->sc_flags & TWEF_AEN) == 0) {
    658  1.12        ad 			rv = twe_param_get(sc, TWE_PARAM_AEN,
    659  1.12        ad 			    TWE_PARAM_AEN_UnitCode, 2, twe_aen_handler,
    660  1.12        ad 			    NULL);
    661  1.12        ad 			if (rv != 0) {
    662  1.12        ad 				printf("%s: unable to retrieve AEN (%d)\n",
    663  1.12        ad 				    sc->sc_dv.dv_xname, rv);
    664  1.22        ad 				twe_outl(sc, TWE_REG_CTL,
    665  1.12        ad 				    TWE_CTL_CLEAR_ATTN_INTR);
    666  1.12        ad 			} else
    667  1.12        ad 				sc->sc_flags |= TWEF_AEN;
    668   1.9        ad 		}
    669   1.1        ad 		caught = 1;
    670   1.1        ad 	}
    671   1.1        ad 
    672   1.1        ad 	/*
    673   1.1        ad 	 * Command interrupts, signalled when the controller can accept more
    674   1.1        ad 	 * commands.  We don't use this; instead, we try to submit commands
    675   1.1        ad 	 * when we receive them, and when other commands have completed.
    676   1.1        ad 	 * Mask it so we don't get another one.
    677   1.1        ad 	 */
    678   1.1        ad 	if ((status & TWE_STS_CMD_INTR) != 0) {
    679  1.38  jdolecek #ifdef DEBUG
    680   1.1        ad 		printf("%s: command interrupt\n", sc->sc_dv.dv_xname);
    681   1.1        ad #endif
    682  1.22        ad 		twe_outl(sc, TWE_REG_CTL, TWE_CTL_MASK_CMD_INTR);
    683   1.1        ad 		caught = 1;
    684   1.1        ad 	}
    685   1.1        ad 
    686   1.1        ad 	if ((status & TWE_STS_RESP_INTR) != 0) {
    687   1.1        ad 		twe_poll(sc);
    688   1.1        ad 		caught = 1;
    689   1.1        ad 	}
    690   1.1        ad 
    691   1.1        ad 	return (caught);
    692   1.1        ad }
    693   1.1        ad 
    694   1.1        ad /*
    695   1.1        ad  * Handle an AEN returned by the controller.
    696   1.1        ad  */
    697   1.1        ad static void
    698   1.1        ad twe_aen_handler(struct twe_ccb *ccb, int error)
    699   1.1        ad {
    700   1.1        ad 	struct twe_softc *sc;
    701   1.1        ad 	struct twe_param *tp;
    702   1.1        ad 	const char *str;
    703   1.1        ad 	u_int aen;
    704   1.7        ad 	int i, hu, rv;
    705   1.1        ad 
    706   1.1        ad 	sc = (struct twe_softc *)ccb->ccb_tx.tx_dv;
    707   1.1        ad 	tp = ccb->ccb_tx.tx_context;
    708   1.1        ad 	twe_ccb_unmap(sc, ccb);
    709   1.1        ad 
    710   1.3        ad 	if (error) {
    711   1.1        ad 		printf("%s: error retrieving AEN\n", sc->sc_dv.dv_xname);
    712   1.3        ad 		aen = TWE_AEN_QUEUE_EMPTY;
    713   1.3        ad 	} else
    714   1.1        ad 		aen = le16toh(*(u_int16_t *)tp->tp_data);
    715   1.3        ad 	free(tp, M_DEVBUF);
    716   1.3        ad 	twe_ccb_free(sc, ccb);
    717   1.3        ad 
    718   1.7        ad 	if (TWE_AEN_CODE(aen) == TWE_AEN_QUEUE_EMPTY) {
    719  1.22        ad 		twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR);
    720  1.12        ad 		sc->sc_flags &= ~TWEF_AEN;
    721   1.7        ad 		return;
    722   1.7        ad 	}
    723   1.7        ad 
    724   1.7        ad 	str = "<unknown>";
    725   1.7        ad 	i = 0;
    726   1.7        ad 	hu = 0;
    727   1.3        ad 
    728   1.7        ad 	while (i < sizeof(twe_aen_names) / sizeof(twe_aen_names[0])) {
    729   1.7        ad 		if (TWE_AEN_CODE(twe_aen_names[i].aen) == TWE_AEN_CODE(aen)) {
    730   1.7        ad 			str = twe_aen_names[i].desc;
    731  1.26  christos 			hu = TWE_AEN_UNIT(twe_aen_names[i].aen);
    732   1.7        ad 			break;
    733   1.7        ad 		}
    734   1.7        ad 		i++;
    735   1.7        ad 	}
    736  1.26  christos 	printf("%s: ", sc->sc_dv.dv_xname);
    737  1.26  christos 	printf(aenfmt[hu], TWE_AEN_UNIT(aen));
    738  1.26  christos 	printf("AEN 0x%04x (%s) received\n", TWE_AEN_CODE(aen), str);
    739   1.3        ad 
    740   1.7        ad 	/*
    741   1.7        ad 	 * Chain another retrieval in case interrupts have been
    742   1.7        ad 	 * coalesced.
    743   1.7        ad 	 */
    744   1.7        ad 	rv = twe_param_get(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2,
    745   1.7        ad 	    twe_aen_handler, NULL);
    746   1.7        ad 	if (rv != 0)
    747   1.7        ad 		printf("%s: unable to retrieve AEN (%d)\n",
    748   1.7        ad 		    sc->sc_dv.dv_xname, rv);
    749   1.1        ad }
    750   1.1        ad 
    751   1.1        ad /*
    752  1.41   thorpej  * These are short-hand functions that execute TWE_OP_GET_PARAM to
    753  1.41   thorpej  * fetch 1, 2, and 4 byte parameter values, respectively.
    754  1.41   thorpej  */
    755  1.41   thorpej static int
    756  1.41   thorpej twe_param_get_1(struct twe_softc *sc, int table_id, int param_id,
    757  1.41   thorpej     uint8_t *valp)
    758  1.41   thorpej {
    759  1.41   thorpej 	struct twe_param *tp;
    760  1.41   thorpej 	int rv;
    761  1.41   thorpej 
    762  1.41   thorpej 	rv = twe_param_get(sc, table_id, param_id, 1, NULL, &tp);
    763  1.41   thorpej 	if (rv != 0)
    764  1.41   thorpej 		return (rv);
    765  1.41   thorpej 	*valp = *(uint8_t *)tp->tp_data;
    766  1.41   thorpej 	free(tp, M_DEVBUF);
    767  1.41   thorpej 	return (0);
    768  1.41   thorpej }
    769  1.41   thorpej 
    770  1.41   thorpej static int
    771  1.41   thorpej twe_param_get_2(struct twe_softc *sc, int table_id, int param_id,
    772  1.41   thorpej     uint16_t *valp)
    773  1.41   thorpej {
    774  1.41   thorpej 	struct twe_param *tp;
    775  1.41   thorpej 	int rv;
    776  1.41   thorpej 
    777  1.41   thorpej 	rv = twe_param_get(sc, table_id, param_id, 2, NULL, &tp);
    778  1.41   thorpej 	if (rv != 0)
    779  1.41   thorpej 		return (rv);
    780  1.41   thorpej 	*valp = le16toh(*(uint16_t *)tp->tp_data);
    781  1.41   thorpej 	free(tp, M_DEVBUF);
    782  1.41   thorpej 	return (0);
    783  1.41   thorpej }
    784  1.41   thorpej 
    785  1.41   thorpej static int
    786  1.41   thorpej twe_param_get_4(struct twe_softc *sc, int table_id, int param_id,
    787  1.41   thorpej     uint32_t *valp)
    788  1.41   thorpej {
    789  1.41   thorpej 	struct twe_param *tp;
    790  1.41   thorpej 	int rv;
    791  1.41   thorpej 
    792  1.41   thorpej 	rv = twe_param_get(sc, table_id, param_id, 4, NULL, &tp);
    793  1.41   thorpej 	if (rv != 0)
    794  1.41   thorpej 		return (rv);
    795  1.41   thorpej 	*valp = le32toh(*(uint32_t *)tp->tp_data);
    796  1.41   thorpej 	free(tp, M_DEVBUF);
    797  1.41   thorpej 	return (0);
    798  1.41   thorpej }
    799  1.41   thorpej 
    800  1.41   thorpej /*
    801   1.1        ad  * Execute a TWE_OP_GET_PARAM command.  If a callback function is provided,
    802   1.1        ad  * it will be called with generated context when the command has completed.
    803   1.1        ad  * If no callback is provided, the command will be executed synchronously
    804   1.3        ad  * and a pointer to a buffer containing the data returned.
    805   1.1        ad  *
    806   1.3        ad  * The caller or callback is responsible for freeing the buffer.
    807   1.1        ad  */
    808   1.7        ad static int
    809   1.1        ad twe_param_get(struct twe_softc *sc, int table_id, int param_id, size_t size,
    810  1.38  jdolecek 	      void (*func)(struct twe_ccb *, int), struct twe_param **pbuf)
    811   1.1        ad {
    812   1.1        ad 	struct twe_ccb *ccb;
    813   1.1        ad 	struct twe_cmd *tc;
    814   1.1        ad 	struct twe_param *tp;
    815   1.1        ad 	int rv, s;
    816   1.1        ad 
    817  1.33  christos 	tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
    818  1.33  christos 	if (tp == NULL)
    819  1.33  christos 		return ENOMEM;
    820  1.33  christos 
    821   1.7        ad 	rv = twe_ccb_alloc(sc, &ccb,
    822   1.7        ad 	    TWE_CCB_PARAM | TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
    823   1.7        ad 	if (rv != 0)
    824  1.33  christos 		goto done;
    825   1.1        ad 
    826   1.1        ad 	ccb->ccb_data = tp;
    827   1.1        ad 	ccb->ccb_datasize = TWE_SECTOR_SIZE;
    828   1.1        ad 	ccb->ccb_tx.tx_handler = func;
    829   1.1        ad 	ccb->ccb_tx.tx_context = tp;
    830   1.1        ad 	ccb->ccb_tx.tx_dv = &sc->sc_dv;
    831   1.1        ad 
    832   1.1        ad 	tc = ccb->ccb_cmd;
    833   1.1        ad 	tc->tc_size = 2;
    834   1.1        ad 	tc->tc_opcode = TWE_OP_GET_PARAM | (tc->tc_size << 5);
    835   1.1        ad 	tc->tc_unit = 0;
    836   1.1        ad 	tc->tc_count = htole16(1);
    837   1.1        ad 
    838   1.1        ad 	/* Fill in the outbound parameter data. */
    839   1.1        ad 	tp->tp_table_id = htole16(table_id);
    840   1.1        ad 	tp->tp_param_id = param_id;
    841   1.1        ad 	tp->tp_param_size = size;
    842   1.1        ad 
    843   1.1        ad 	/* Map the transfer. */
    844   1.7        ad 	if ((rv = twe_ccb_map(sc, ccb)) != 0) {
    845   1.2        ad 		twe_ccb_free(sc, ccb);
    846  1.33  christos 		goto done;
    847   1.1        ad 	}
    848   1.1        ad 
    849   1.1        ad 	/* Submit the command and either wait or let the callback handle it. */
    850   1.1        ad 	if (func == NULL) {
    851   1.1        ad 		s = splbio();
    852   1.7        ad 		rv = twe_ccb_poll(sc, ccb, 5);
    853   1.1        ad 		twe_ccb_unmap(sc, ccb);
    854   1.2        ad 		twe_ccb_free(sc, ccb);
    855   1.1        ad 		splx(s);
    856   1.1        ad 	} else {
    857  1.38  jdolecek #ifdef DEBUG
    858  1.33  christos 		if (pbuf != NULL)
    859  1.33  christos 			panic("both func and pbuf defined");
    860  1.33  christos #endif
    861   1.1        ad 		twe_ccb_enqueue(sc, ccb);
    862  1.33  christos 		return 0;
    863  1.33  christos 	}
    864  1.33  christos 
    865  1.33  christos done:
    866  1.33  christos 	if (pbuf == NULL || rv != 0)
    867  1.33  christos 		free(tp, M_DEVBUF);
    868  1.33  christos 	else if (pbuf != NULL && rv == 0)
    869  1.33  christos 		*pbuf = tp;
    870  1.33  christos 	return rv;
    871  1.33  christos }
    872  1.33  christos 
    873  1.33  christos /*
    874  1.33  christos  * Execute a TWE_OP_SET_PARAM command.
    875  1.33  christos  */
    876  1.33  christos static int
    877  1.33  christos twe_param_set(struct twe_softc *sc, int table_id, int param_id, size_t size,
    878  1.33  christos 	      void *buf)
    879  1.33  christos {
    880  1.33  christos 	struct twe_ccb *ccb;
    881  1.33  christos 	struct twe_cmd *tc;
    882  1.33  christos 	struct twe_param *tp;
    883  1.33  christos 	int rv, s;
    884  1.33  christos 
    885  1.33  christos 	tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
    886  1.33  christos 	if (tp == NULL)
    887  1.33  christos 		return ENOMEM;
    888  1.33  christos 
    889  1.33  christos 	rv = twe_ccb_alloc(sc, &ccb,
    890  1.33  christos 	    TWE_CCB_PARAM | TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
    891  1.33  christos 	if (rv != 0)
    892  1.33  christos 		goto done;
    893  1.33  christos 
    894  1.33  christos 	ccb->ccb_data = tp;
    895  1.33  christos 	ccb->ccb_datasize = TWE_SECTOR_SIZE;
    896  1.33  christos 	ccb->ccb_tx.tx_handler = 0;
    897  1.33  christos 	ccb->ccb_tx.tx_context = tp;
    898  1.33  christos 	ccb->ccb_tx.tx_dv = &sc->sc_dv;
    899  1.33  christos 
    900  1.33  christos 	tc = ccb->ccb_cmd;
    901  1.33  christos 	tc->tc_size = 2;
    902  1.33  christos 	tc->tc_opcode = TWE_OP_SET_PARAM | (tc->tc_size << 5);
    903  1.33  christos 	tc->tc_unit = 0;
    904  1.33  christos 	tc->tc_count = htole16(1);
    905  1.33  christos 
    906  1.33  christos 	/* Fill in the outbound parameter data. */
    907  1.33  christos 	tp->tp_table_id = htole16(table_id);
    908  1.33  christos 	tp->tp_param_id = param_id;
    909  1.33  christos 	tp->tp_param_size = size;
    910  1.33  christos 	memcpy(tp->tp_data, buf, size);
    911  1.33  christos 
    912  1.33  christos 	/* Map the transfer. */
    913  1.33  christos 	if ((rv = twe_ccb_map(sc, ccb)) != 0) {
    914  1.33  christos 		twe_ccb_free(sc, ccb);
    915  1.33  christos 		goto done;
    916   1.1        ad 	}
    917   1.1        ad 
    918  1.33  christos 	/* Submit the command and wait. */
    919  1.33  christos 	s = splbio();
    920  1.33  christos 	rv = twe_ccb_poll(sc, ccb, 5);
    921  1.33  christos 	twe_ccb_unmap(sc, ccb);
    922  1.33  christos 	twe_ccb_free(sc, ccb);
    923  1.33  christos 	splx(s);
    924  1.33  christos done:
    925  1.33  christos 	free(tp, M_DEVBUF);
    926   1.7        ad 	return (rv);
    927   1.1        ad }
    928   1.1        ad 
    929   1.1        ad /*
    930   1.1        ad  * Execute a TWE_OP_INIT_CONNECTION command.  Return non-zero on error.
    931   1.1        ad  * Must be called with interrupts blocked.
    932   1.1        ad  */
    933   1.1        ad static int
    934   1.1        ad twe_init_connection(struct twe_softc *sc)
    935  1.33  christos /*###762 [cc] warning: `twe_init_connection' was used with no prototype before its definition%%%*/
    936  1.33  christos /*###762 [cc] warning: `twe_init_connection' was declared implicitly `extern' and later `static'%%%*/
    937   1.1        ad {
    938   1.1        ad 	struct twe_ccb *ccb;
    939   1.1        ad 	struct twe_cmd *tc;
    940   1.1        ad 	int rv;
    941   1.1        ad 
    942   1.3        ad 	if ((rv = twe_ccb_alloc(sc, &ccb, 0)) != 0)
    943   1.1        ad 		return (rv);
    944   1.1        ad 
    945   1.1        ad 	/* Build the command. */
    946   1.1        ad 	tc = ccb->ccb_cmd;
    947   1.1        ad 	tc->tc_size = 3;
    948   1.1        ad 	tc->tc_opcode = TWE_OP_INIT_CONNECTION;
    949   1.1        ad 	tc->tc_unit = 0;
    950   1.3        ad 	tc->tc_count = htole16(TWE_MAX_CMDS);
    951   1.1        ad 	tc->tc_args.init_connection.response_queue_pointer = 0;
    952   1.1        ad 
    953   1.1        ad 	/* Submit the command for immediate execution. */
    954   1.7        ad 	rv = twe_ccb_poll(sc, ccb, 5);
    955   1.2        ad 	twe_ccb_free(sc, ccb);
    956   1.1        ad 	return (rv);
    957   1.1        ad }
    958   1.1        ad 
    959   1.1        ad /*
    960   1.1        ad  * Poll the controller for completed commands.  Must be called with
    961   1.1        ad  * interrupts blocked.
    962   1.1        ad  */
    963   1.1        ad static void
    964   1.1        ad twe_poll(struct twe_softc *sc)
    965   1.1        ad {
    966   1.1        ad 	struct twe_ccb *ccb;
    967   1.1        ad 	int found;
    968   1.1        ad 	u_int status, cmdid;
    969   1.1        ad 
    970   1.1        ad 	found = 0;
    971   1.1        ad 
    972   1.1        ad 	for (;;) {
    973  1.22        ad 		status = twe_inl(sc, TWE_REG_STS);
    974   1.1        ad 		twe_status_check(sc, status);
    975   1.1        ad 
    976   1.1        ad 		if ((status & TWE_STS_RESP_QUEUE_EMPTY))
    977   1.1        ad 			break;
    978   1.1        ad 
    979   1.1        ad 		found = 1;
    980  1.22        ad 		cmdid = twe_inl(sc, TWE_REG_RESP_QUEUE);
    981   1.1        ad 		cmdid = (cmdid & TWE_RESP_MASK) >> TWE_RESP_SHIFT;
    982   1.7        ad 		if (cmdid >= TWE_MAX_QUEUECNT) {
    983   1.1        ad 			printf("%s: bad completion\n", sc->sc_dv.dv_xname);
    984   1.1        ad 			continue;
    985   1.1        ad 		}
    986   1.1        ad 
    987   1.1        ad 		ccb = sc->sc_ccbs + cmdid;
    988   1.1        ad 		if ((ccb->ccb_flags & TWE_CCB_ACTIVE) == 0) {
    989   1.1        ad 			printf("%s: bad completion (not active)\n",
    990   1.1        ad 			    sc->sc_dv.dv_xname);
    991   1.1        ad 			continue;
    992   1.1        ad 		}
    993   1.1        ad 		ccb->ccb_flags ^= TWE_CCB_COMPLETE | TWE_CCB_ACTIVE;
    994   1.1        ad 
    995   1.1        ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
    996   1.1        ad 		    (caddr_t)ccb->ccb_cmd - sc->sc_cmds,
    997   1.1        ad 		    sizeof(struct twe_cmd),
    998   1.1        ad 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    999   1.1        ad 
   1000   1.1        ad 		/* Pass notification to upper layers. */
   1001   1.1        ad 		if (ccb->ccb_tx.tx_handler != NULL)
   1002   1.1        ad 			(*ccb->ccb_tx.tx_handler)(ccb,
   1003   1.1        ad 			    ccb->ccb_cmd->tc_status != 0 ? EIO : 0);
   1004   1.1        ad 	}
   1005   1.1        ad 
   1006   1.1        ad 	/* If any commands have completed, run the software queue. */
   1007   1.1        ad 	if (found)
   1008   1.1        ad 		twe_ccb_enqueue(sc, NULL);
   1009   1.1        ad }
   1010   1.1        ad 
   1011   1.1        ad /*
   1012   1.1        ad  * Wait for `status' to be set in the controller status register.  Return
   1013   1.1        ad  * zero if found, non-zero if the operation timed out.
   1014   1.1        ad  */
   1015   1.1        ad static int
   1016   1.1        ad twe_status_wait(struct twe_softc *sc, u_int32_t status, int timo)
   1017   1.1        ad {
   1018   1.1        ad 
   1019  1.11        ad 	for (timo *= 10; timo != 0; timo--) {
   1020  1.22        ad 		if ((twe_inl(sc, TWE_REG_STS) & status) == status)
   1021   1.1        ad 			break;
   1022   1.1        ad 		delay(100000);
   1023   1.1        ad 	}
   1024   1.1        ad 
   1025   1.1        ad 	return (timo == 0);
   1026   1.1        ad }
   1027   1.1        ad 
   1028   1.1        ad /*
   1029   1.1        ad  * Complain if the status bits aren't what we expect.
   1030   1.1        ad  */
   1031   1.1        ad static int
   1032   1.1        ad twe_status_check(struct twe_softc *sc, u_int status)
   1033   1.1        ad {
   1034   1.1        ad 	int rv;
   1035   1.1        ad 
   1036   1.1        ad 	rv = 0;
   1037   1.1        ad 
   1038   1.1        ad 	if ((status & TWE_STS_EXPECTED_BITS) != TWE_STS_EXPECTED_BITS) {
   1039   1.1        ad 		printf("%s: missing status bits: 0x%08x\n", sc->sc_dv.dv_xname,
   1040   1.1        ad 		    status & ~TWE_STS_EXPECTED_BITS);
   1041   1.1        ad 		rv = -1;
   1042   1.1        ad 	}
   1043   1.1        ad 
   1044   1.1        ad 	if ((status & TWE_STS_UNEXPECTED_BITS) != 0) {
   1045   1.1        ad 		printf("%s: unexpected status bits: 0x%08x\n",
   1046   1.1        ad 		    sc->sc_dv.dv_xname, status & TWE_STS_UNEXPECTED_BITS);
   1047   1.1        ad 		rv = -1;
   1048   1.1        ad 	}
   1049   1.1        ad 
   1050   1.1        ad 	return (rv);
   1051   1.1        ad }
   1052   1.1        ad 
   1053   1.1        ad /*
   1054   1.1        ad  * Allocate and initialise a CCB.
   1055   1.1        ad  */
   1056   1.1        ad int
   1057   1.3        ad twe_ccb_alloc(struct twe_softc *sc, struct twe_ccb **ccbp, int flags)
   1058   1.1        ad {
   1059   1.1        ad 	struct twe_cmd *tc;
   1060   1.1        ad 	struct twe_ccb *ccb;
   1061   1.1        ad 	int s;
   1062   1.1        ad 
   1063   1.7        ad 	s = splbio();
   1064   1.3        ad 	if ((flags & TWE_CCB_PARAM) != 0)
   1065   1.3        ad 		ccb = sc->sc_ccbs;
   1066   1.3        ad 	else {
   1067   1.3        ad 		/* Allocate a CCB and command block. */
   1068   1.3        ad 		if (SLIST_FIRST(&sc->sc_ccb_freelist) == NULL) {
   1069   1.1        ad 			splx(s);
   1070   1.1        ad 			return (EAGAIN);
   1071   1.1        ad 		}
   1072   1.3        ad 		ccb = SLIST_FIRST(&sc->sc_ccb_freelist);
   1073   1.3        ad 		SLIST_REMOVE_HEAD(&sc->sc_ccb_freelist, ccb_chain.slist);
   1074   1.1        ad 	}
   1075   1.3        ad #ifdef DIAGNOSTIC
   1076   1.3        ad 	if ((ccb->ccb_flags & TWE_CCB_ALLOCED) != 0)
   1077   1.3        ad 		panic("twe_ccb_alloc: CCB already allocated");
   1078   1.3        ad 	flags |= TWE_CCB_ALLOCED;
   1079   1.3        ad #endif
   1080   1.7        ad 	splx(s);
   1081   1.1        ad 
   1082   1.1        ad 	/* Initialise some fields and return. */
   1083   1.1        ad 	ccb->ccb_tx.tx_handler = NULL;
   1084   1.3        ad 	ccb->ccb_flags = flags;
   1085   1.1        ad 	tc = ccb->ccb_cmd;
   1086   1.1        ad 	tc->tc_status = 0;
   1087   1.1        ad 	tc->tc_flags = 0;
   1088   1.1        ad 	tc->tc_cmdid = ccb->ccb_cmdid;
   1089   1.3        ad 	*ccbp = ccb;
   1090   1.1        ad 
   1091   1.1        ad 	return (0);
   1092   1.1        ad }
   1093   1.1        ad 
   1094   1.1        ad /*
   1095   1.3        ad  * Free a CCB.
   1096   1.1        ad  */
   1097   1.1        ad void
   1098   1.2        ad twe_ccb_free(struct twe_softc *sc, struct twe_ccb *ccb)
   1099   1.1        ad {
   1100   1.1        ad 	int s;
   1101   1.1        ad 
   1102   1.3        ad 	s = splbio();
   1103   1.3        ad 	if ((ccb->ccb_flags & TWE_CCB_PARAM) == 0)
   1104   1.3        ad 		SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb, ccb_chain.slist);
   1105   1.1        ad 	ccb->ccb_flags = 0;
   1106   1.1        ad 	splx(s);
   1107   1.1        ad }
   1108   1.1        ad 
   1109   1.1        ad /*
   1110   1.1        ad  * Map the specified CCB's command block and data buffer (if any) into
   1111   1.1        ad  * controller visible space.  Perform DMA synchronisation.
   1112   1.1        ad  */
   1113   1.1        ad int
   1114   1.1        ad twe_ccb_map(struct twe_softc *sc, struct twe_ccb *ccb)
   1115   1.1        ad {
   1116   1.1        ad 	struct twe_cmd *tc;
   1117  1.20        ad 	int flags, nsegs, i, s, rv;
   1118   1.1        ad 	void *data;
   1119   1.1        ad 
   1120   1.7        ad 	/*
   1121   1.7        ad 	 * The data as a whole must be 512-byte aligned.
   1122   1.7        ad 	 */
   1123   1.1        ad 	if (((u_long)ccb->ccb_data & (TWE_ALIGNMENT - 1)) != 0) {
   1124  1.20        ad 		s = splvm();
   1125  1.20        ad 		/* XXX */
   1126  1.20        ad 		ccb->ccb_abuf = uvm_km_kmemalloc(kmem_map, NULL,
   1127  1.20        ad 		    ccb->ccb_datasize, UVM_KMF_NOWAIT);
   1128  1.20        ad 		splx(s);
   1129  1.20        ad 		data = (void *)ccb->ccb_abuf;
   1130   1.2        ad 		if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
   1131   1.2        ad 			memcpy(data, ccb->ccb_data, ccb->ccb_datasize);
   1132   1.1        ad 	} else {
   1133  1.20        ad 		ccb->ccb_abuf = (vaddr_t)0;
   1134   1.1        ad 		data = ccb->ccb_data;
   1135   1.1        ad 	}
   1136   1.1        ad 
   1137   1.7        ad 	/*
   1138   1.7        ad 	 * Map the data buffer into bus space and build the S/G list.
   1139   1.7        ad 	 */
   1140   1.7        ad 	rv = bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer, data,
   1141  1.16   thorpej 	    ccb->ccb_datasize, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
   1142  1.16   thorpej 	    ((ccb->ccb_flags & TWE_CCB_DATA_IN) ?
   1143  1.22        ad 	    BUS_DMA_READ : BUS_DMA_WRITE));
   1144   1.7        ad 	if (rv != 0) {
   1145  1.20        ad 		if (ccb->ccb_abuf != (vaddr_t)0) {
   1146  1.20        ad 			s = splvm();
   1147  1.20        ad 			/* XXX */
   1148  1.20        ad 			uvm_km_free(kmem_map, ccb->ccb_abuf,
   1149   1.7        ad 			    ccb->ccb_datasize);
   1150  1.20        ad 			splx(s);
   1151   1.7        ad 		}
   1152   1.7        ad 		return (rv);
   1153   1.7        ad 	}
   1154   1.1        ad 
   1155   1.1        ad 	nsegs = ccb->ccb_dmamap_xfer->dm_nsegs;
   1156   1.1        ad 	tc = ccb->ccb_cmd;
   1157   1.1        ad 	tc->tc_size += 2 * nsegs;
   1158   1.1        ad 
   1159   1.1        ad 	/* The location of the S/G list is dependant upon command type. */
   1160   1.1        ad 	switch (tc->tc_opcode >> 5) {
   1161   1.1        ad 	case 2:
   1162   1.1        ad 		for (i = 0; i < nsegs; i++) {
   1163   1.1        ad 			tc->tc_args.param.sgl[i].tsg_address =
   1164   1.1        ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
   1165   1.1        ad 			tc->tc_args.param.sgl[i].tsg_length =
   1166   1.1        ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
   1167   1.1        ad 		}
   1168   1.1        ad 		/* XXX Needed? */
   1169   1.1        ad 		for (; i < TWE_SG_SIZE; i++) {
   1170   1.1        ad 			tc->tc_args.param.sgl[i].tsg_address = 0;
   1171   1.1        ad 			tc->tc_args.param.sgl[i].tsg_length = 0;
   1172   1.1        ad 		}
   1173   1.1        ad 		break;
   1174   1.1        ad 	case 3:
   1175   1.1        ad 		for (i = 0; i < nsegs; i++) {
   1176   1.1        ad 			tc->tc_args.io.sgl[i].tsg_address =
   1177   1.1        ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
   1178   1.1        ad 			tc->tc_args.io.sgl[i].tsg_length =
   1179   1.1        ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
   1180   1.1        ad 		}
   1181   1.1        ad 		/* XXX Needed? */
   1182   1.1        ad 		for (; i < TWE_SG_SIZE; i++) {
   1183   1.1        ad 			tc->tc_args.io.sgl[i].tsg_address = 0;
   1184   1.1        ad 			tc->tc_args.io.sgl[i].tsg_length = 0;
   1185   1.1        ad 		}
   1186   1.1        ad 		break;
   1187   1.1        ad #ifdef DEBUG
   1188   1.1        ad 	default:
   1189   1.1        ad 		panic("twe_ccb_map: oops");
   1190   1.1        ad #endif
   1191   1.1        ad 	}
   1192   1.1        ad 
   1193   1.1        ad 	if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
   1194   1.1        ad 		flags = BUS_DMASYNC_PREREAD;
   1195   1.1        ad 	else
   1196   1.1        ad 		flags = 0;
   1197   1.1        ad 	if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
   1198   1.1        ad 		flags |= BUS_DMASYNC_PREWRITE;
   1199   1.1        ad 
   1200   1.1        ad 	bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
   1201   1.1        ad 	    ccb->ccb_datasize, flags);
   1202   1.1        ad 	return (0);
   1203   1.1        ad }
   1204   1.1        ad 
   1205   1.1        ad /*
   1206   1.1        ad  * Unmap the specified CCB's command block and data buffer (if any) and
   1207   1.1        ad  * perform DMA synchronisation.
   1208   1.1        ad  */
   1209   1.1        ad void
   1210   1.1        ad twe_ccb_unmap(struct twe_softc *sc, struct twe_ccb *ccb)
   1211   1.1        ad {
   1212  1.20        ad 	int flags, s;
   1213   1.1        ad 
   1214   1.1        ad 	if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
   1215   1.1        ad 		flags = BUS_DMASYNC_POSTREAD;
   1216   1.1        ad 	else
   1217   1.1        ad 		flags = 0;
   1218   1.1        ad 	if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
   1219   1.1        ad 		flags |= BUS_DMASYNC_POSTWRITE;
   1220   1.1        ad 
   1221   1.1        ad 	bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
   1222   1.1        ad 	    ccb->ccb_datasize, flags);
   1223   1.1        ad 	bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
   1224   1.1        ad 
   1225  1.20        ad 	if (ccb->ccb_abuf != (vaddr_t)0) {
   1226   1.2        ad 		if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
   1227  1.20        ad 			memcpy(ccb->ccb_data, (void *)ccb->ccb_abuf,
   1228   1.2        ad 			    ccb->ccb_datasize);
   1229  1.20        ad 		s = splvm();
   1230  1.20        ad 		/* XXX */
   1231  1.20        ad 		uvm_km_free(kmem_map, ccb->ccb_abuf, ccb->ccb_datasize);
   1232  1.20        ad 		splx(s);
   1233   1.1        ad 	}
   1234   1.1        ad }
   1235   1.1        ad 
   1236   1.1        ad /*
   1237   1.7        ad  * Submit a command to the controller and poll on completion.  Return
   1238   1.7        ad  * non-zero on timeout (but don't check status, as some command types don't
   1239   1.7        ad  * return status).  Must be called with interrupts blocked.
   1240   1.1        ad  */
   1241   1.1        ad int
   1242   1.1        ad twe_ccb_poll(struct twe_softc *sc, struct twe_ccb *ccb, int timo)
   1243   1.1        ad {
   1244   1.7        ad 	int rv;
   1245   1.7        ad 
   1246   1.7        ad 	if ((rv = twe_ccb_submit(sc, ccb)) != 0)
   1247   1.7        ad 		return (rv);
   1248   1.1        ad 
   1249  1.15        ad 	for (timo *= 1000; timo != 0; timo--) {
   1250   1.1        ad 		twe_poll(sc);
   1251   1.1        ad 		if ((ccb->ccb_flags & TWE_CCB_COMPLETE) != 0)
   1252   1.1        ad 			break;
   1253  1.15        ad 		DELAY(100);
   1254   1.1        ad 	}
   1255   1.1        ad 
   1256   1.1        ad 	return (timo == 0);
   1257   1.1        ad }
   1258   1.1        ad 
   1259   1.1        ad /*
   1260   1.1        ad  * If a CCB is specified, enqueue it.  Pull CCBs off the software queue in
   1261   1.1        ad  * the order that they were enqueued and try to submit their command blocks
   1262   1.1        ad  * to the controller for execution.
   1263   1.1        ad  */
   1264   1.1        ad void
   1265   1.1        ad twe_ccb_enqueue(struct twe_softc *sc, struct twe_ccb *ccb)
   1266   1.1        ad {
   1267   1.1        ad 	int s;
   1268   1.1        ad 
   1269   1.1        ad 	s = splbio();
   1270   1.1        ad 
   1271   1.1        ad 	if (ccb != NULL)
   1272   1.1        ad 		SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain.simpleq);
   1273   1.1        ad 
   1274   1.1        ad 	while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
   1275   1.1        ad 		if (twe_ccb_submit(sc, ccb))
   1276   1.1        ad 			break;
   1277  1.25     lukem 		SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb_chain.simpleq);
   1278   1.1        ad 	}
   1279   1.1        ad 
   1280   1.1        ad 	splx(s);
   1281   1.1        ad }
   1282   1.1        ad 
   1283   1.1        ad /*
   1284   1.1        ad  * Submit the command block associated with the specified CCB to the
   1285   1.1        ad  * controller for execution.  Must be called with interrupts blocked.
   1286   1.1        ad  */
   1287   1.1        ad int
   1288   1.1        ad twe_ccb_submit(struct twe_softc *sc, struct twe_ccb *ccb)
   1289   1.1        ad {
   1290   1.1        ad 	bus_addr_t pa;
   1291   1.1        ad 	int rv;
   1292   1.1        ad 	u_int status;
   1293   1.1        ad 
   1294   1.1        ad 	/* Check to see if we can post a command. */
   1295  1.22        ad 	status = twe_inl(sc, TWE_REG_STS);
   1296   1.1        ad 	twe_status_check(sc, status);
   1297   1.1        ad 
   1298   1.1        ad 	if ((status & TWE_STS_CMD_QUEUE_FULL) == 0) {
   1299   1.1        ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1300   1.1        ad 		    (caddr_t)ccb->ccb_cmd - sc->sc_cmds, sizeof(struct twe_cmd),
   1301   1.1        ad 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1302   1.1        ad 		ccb->ccb_flags |= TWE_CCB_ACTIVE;
   1303   1.1        ad 		pa = sc->sc_cmds_paddr +
   1304   1.1        ad 		    ccb->ccb_cmdid * sizeof(struct twe_cmd);
   1305  1.22        ad 		twe_outl(sc, TWE_REG_CMD_QUEUE, (u_int32_t)pa);
   1306   1.1        ad 		rv = 0;
   1307   1.1        ad 	} else
   1308   1.1        ad 		rv = EBUSY;
   1309   1.1        ad 
   1310   1.1        ad 	return (rv);
   1311  1.33  christos }
   1312  1.33  christos 
   1313  1.33  christos 
   1314  1.33  christos /*
   1315  1.33  christos  * Accept an open operation on the control device.
   1316  1.33  christos  */
   1317  1.33  christos int
   1318  1.37      fvdl tweopen(dev_t dev, int flag, int mode, struct proc *p)
   1319  1.33  christos {
   1320  1.33  christos 	struct twe_softc *twe;
   1321  1.33  christos 
   1322  1.33  christos 	if ((twe = device_lookup(&twe_cd, minor(dev))) == NULL)
   1323  1.33  christos 		return (ENXIO);
   1324  1.33  christos 	if ((twe->sc_flags & TWEF_OPEN) != 0)
   1325  1.33  christos 		return (EBUSY);
   1326  1.33  christos 
   1327  1.33  christos 	twe->sc_flags |= TWEF_OPEN;
   1328  1.33  christos 	return (0);
   1329  1.33  christos }
   1330  1.33  christos 
   1331  1.33  christos /*
   1332  1.33  christos  * Accept the last close on the control device.
   1333  1.33  christos  */
   1334  1.33  christos int
   1335  1.37      fvdl tweclose(dev_t dev, int flag, int mode, struct proc *p)
   1336  1.33  christos {
   1337  1.33  christos 	struct twe_softc *twe;
   1338  1.33  christos 
   1339  1.33  christos 	twe = device_lookup(&twe_cd, minor(dev));
   1340  1.33  christos 	twe->sc_flags &= ~TWEF_OPEN;
   1341  1.33  christos 	return (0);
   1342  1.33  christos }
   1343  1.33  christos 
   1344  1.33  christos /*
   1345  1.33  christos  * Handle control operations.
   1346  1.33  christos  */
   1347  1.33  christos int
   1348  1.37      fvdl tweioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
   1349  1.33  christos {
   1350  1.33  christos 	struct twe_softc *twe;
   1351  1.33  christos 	struct twe_ccb *ccb;
   1352  1.33  christos 	struct twe_param *param;
   1353  1.33  christos 	struct twe_usercommand *tu;
   1354  1.33  christos 	struct twe_paramcommand *tp;
   1355  1.33  christos 	union twe_statrequest *ts;
   1356  1.33  christos 	void *pdata = NULL;
   1357  1.33  christos 	int rv, s, error = 0;
   1358  1.33  christos 	u_int8_t cmdid;
   1359  1.33  christos 
   1360  1.33  christos 	if (securelevel >= 2)
   1361  1.33  christos 		return (EPERM);
   1362  1.33  christos 
   1363  1.33  christos 	twe = device_lookup(&twe_cd, minor(dev));
   1364  1.33  christos 	tu = (struct twe_usercommand *)data;
   1365  1.33  christos 	tp = (struct twe_paramcommand *)data;
   1366  1.33  christos 	ts = (union twe_statrequest *)data;
   1367  1.33  christos 
   1368  1.33  christos 	/* Hmm, compatible with FreeBSD */
   1369  1.33  christos 	switch (cmd) {
   1370  1.33  christos 	case TWEIO_COMMAND:
   1371  1.33  christos 		if (tu->tu_size > 0) {
   1372  1.33  christos 			if (tu->tu_size > TWE_SECTOR_SIZE)
   1373  1.33  christos 				return EINVAL;
   1374  1.33  christos 			pdata = malloc(tu->tu_size, M_DEVBUF, M_WAITOK);
   1375  1.33  christos 			error = copyin(tu->tu_data, pdata, tu->tu_size);
   1376  1.33  christos 			if (error != 0)
   1377  1.33  christos 				goto done;
   1378  1.33  christos 			error = twe_ccb_alloc(twe, &ccb, TWE_CCB_PARAM |
   1379  1.33  christos 			    TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
   1380  1.33  christos 		} else {
   1381  1.33  christos 			error = twe_ccb_alloc(twe, &ccb, 0);
   1382  1.33  christos 		}
   1383  1.33  christos 		if (rv != 0)
   1384  1.33  christos 			goto done;
   1385  1.33  christos 		cmdid = ccb->ccb_cmdid;
   1386  1.33  christos 		memcpy(ccb->ccb_cmd, &tu->tu_cmd, sizeof(struct twe_cmd));
   1387  1.33  christos 		ccb->ccb_cmdid = cmdid;
   1388  1.33  christos 		if (ccb->ccb_flags & TWE_CCB_PARAM) {
   1389  1.33  christos 			ccb->ccb_data = pdata;
   1390  1.33  christos 			ccb->ccb_datasize = TWE_SECTOR_SIZE;
   1391  1.33  christos 			ccb->ccb_tx.tx_handler = 0;
   1392  1.33  christos 			ccb->ccb_tx.tx_context = pdata;
   1393  1.33  christos 			ccb->ccb_tx.tx_dv = &twe->sc_dv;
   1394  1.33  christos 		}
   1395  1.33  christos 		/* Map the transfer. */
   1396  1.33  christos 		if ((error = twe_ccb_map(twe, ccb)) != 0) {
   1397  1.33  christos 			twe_ccb_free(twe, ccb);
   1398  1.33  christos 			goto done;
   1399  1.33  christos 		}
   1400  1.33  christos 
   1401  1.33  christos 		/* Submit the command and wait. */
   1402  1.33  christos 		s = splbio();
   1403  1.33  christos 		rv = twe_ccb_poll(twe, ccb, 5);
   1404  1.33  christos 		twe_ccb_unmap(twe, ccb);
   1405  1.33  christos 		twe_ccb_free(twe, ccb);
   1406  1.33  christos 		splx(s);
   1407  1.33  christos 
   1408  1.33  christos 		if (tu->tu_size > 0)
   1409  1.33  christos 			error = copyout(pdata, tu->tu_data, tu->tu_size);
   1410  1.33  christos 		goto done;
   1411  1.33  christos 
   1412  1.33  christos 	case TWEIO_STATS:
   1413  1.33  christos 		return (ENOENT);
   1414  1.33  christos 
   1415  1.33  christos 	case TWEIO_AEN_POLL:
   1416  1.33  christos 		if ((twe->sc_flags & TWEF_AEN) == 0)
   1417  1.33  christos 			return (ENOENT);
   1418  1.33  christos 		return (0);
   1419  1.33  christos 
   1420  1.33  christos 	case TWEIO_AEN_WAIT:
   1421  1.33  christos 		s = splbio();
   1422  1.33  christos 		while ((twe->sc_flags & TWEF_AEN) == 0) {
   1423  1.33  christos 			/* tsleep(); */
   1424  1.33  christos 		}
   1425  1.33  christos 		splx(s);
   1426  1.33  christos 		return (0);
   1427  1.33  christos 
   1428  1.33  christos 	case TWEIO_GET_PARAM:
   1429  1.33  christos 		error = twe_param_get(twe, tp->tp_table_id, tp->tp_param_id,
   1430  1.39  jdolecek 		    tp->tp_size, 0, &param);
   1431  1.33  christos 		if (error != 0)
   1432  1.33  christos 			return (error);
   1433  1.33  christos 		if (param->tp_param_size > tp->tp_size) {
   1434  1.33  christos 			error = EFAULT;
   1435  1.33  christos 			goto done;
   1436  1.33  christos 		}
   1437  1.33  christos 		error = copyout(param->tp_data, tp->tp_data,
   1438  1.33  christos 		    param->tp_param_size);
   1439  1.33  christos 		goto done;
   1440  1.33  christos 
   1441  1.33  christos 	case TWEIO_SET_PARAM:
   1442  1.33  christos 		pdata = malloc(tp->tp_size, M_DEVBUF, M_WAITOK);
   1443  1.33  christos 		if ((error = copyin(tp->tp_data, pdata, tp->tp_size)) != 0)
   1444  1.33  christos 			goto done;
   1445  1.33  christos 		error = twe_param_set(twe, tp->tp_table_id, tp->tp_param_id,
   1446  1.33  christos 		    tp->tp_size, pdata);
   1447  1.33  christos 		goto done;
   1448  1.33  christos 
   1449  1.33  christos 	case TWEIO_RESET:
   1450  1.33  christos 		twe_reset(twe);
   1451  1.33  christos 		return (0);
   1452  1.33  christos 
   1453  1.33  christos 	default:
   1454  1.33  christos 		return EINVAL;
   1455  1.33  christos 	}
   1456  1.33  christos done:
   1457  1.33  christos 	if (pdata)
   1458  1.33  christos 		free(pdata, M_DEVBUF);
   1459  1.33  christos 	return error;
   1460  1.38  jdolecek }
   1461  1.38  jdolecek 
   1462  1.38  jdolecek /*
   1463  1.38  jdolecek  * Print some information about the controller
   1464  1.38  jdolecek  */
   1465  1.38  jdolecek static void
   1466  1.38  jdolecek twe_describe_controller(struct twe_softc *sc)
   1467  1.38  jdolecek {
   1468  1.41   thorpej 	struct twe_param *p[6];
   1469  1.38  jdolecek 	int rv = 0;
   1470  1.41   thorpej 	uint8_t ports;
   1471  1.41   thorpej 
   1472  1.38  jdolecek 	/* get the port count */
   1473  1.41   thorpej 	rv |= twe_param_get_1(sc, TWE_PARAM_CONTROLLER,
   1474  1.41   thorpej 		TWE_PARAM_CONTROLLER_PortCount, &ports);
   1475  1.38  jdolecek 
   1476  1.38  jdolecek 	/* get version strings */
   1477  1.38  jdolecek 	rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_Mon,
   1478  1.41   thorpej 		16, NULL, &p[0]);
   1479  1.41   thorpej 	rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_FW,
   1480  1.38  jdolecek 		16, NULL, &p[1]);
   1481  1.41   thorpej 	rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_BIOS,
   1482  1.38  jdolecek 		16, NULL, &p[2]);
   1483  1.38  jdolecek 	rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCB,
   1484  1.41   thorpej 		8, NULL, &p[3]);
   1485  1.41   thorpej 	rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_ATA,
   1486  1.38  jdolecek 		8, NULL, &p[4]);
   1487  1.41   thorpej 	rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCI,
   1488  1.38  jdolecek 		8, NULL, &p[5]);
   1489  1.38  jdolecek 
   1490  1.38  jdolecek 	if (rv) {
   1491  1.38  jdolecek 		/* some error occurred */
   1492  1.38  jdolecek 		aprint_error("%s: failed to fetch version information\n",
   1493  1.38  jdolecek 			sc->sc_dv.dv_xname);
   1494  1.38  jdolecek 		return;
   1495  1.38  jdolecek 	}
   1496  1.38  jdolecek 
   1497  1.38  jdolecek 	aprint_normal("%s: %d ports, Firmware %.16s, BIOS %.16s\n",
   1498  1.41   thorpej 		sc->sc_dv.dv_xname, ports,
   1499  1.41   thorpej 		p[1]->tp_data, p[2]->tp_data);
   1500  1.38  jdolecek 
   1501  1.38  jdolecek 	aprint_verbose("%s: Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n",
   1502  1.38  jdolecek 		sc->sc_dv.dv_xname,
   1503  1.41   thorpej 		p[0]->tp_data, p[3]->tp_data,
   1504  1.41   thorpej 		p[4]->tp_data, p[5]->tp_data);
   1505  1.38  jdolecek 
   1506  1.38  jdolecek 	free(p[0], M_DEVBUF);
   1507  1.38  jdolecek 	free(p[1], M_DEVBUF);
   1508  1.38  jdolecek 	free(p[2], M_DEVBUF);
   1509  1.38  jdolecek 	free(p[3], M_DEVBUF);
   1510  1.38  jdolecek 	free(p[4], M_DEVBUF);
   1511  1.38  jdolecek 	free(p[5], M_DEVBUF);
   1512   1.1        ad }
   1513