Home | History | Annotate | Line # | Download | only in pci
twe.c revision 1.12.2.14
      1  1.12.2.14   thorpej /*	$NetBSD: twe.c,v 1.12.2.14 2003/01/03 17:08:03 thorpej Exp $	*/
      2        1.1        ad 
      3        1.1        ad /*-
      4   1.12.2.8   nathanw  * Copyright (c) 2000, 2001, 2002 The NetBSD Foundation, Inc.
      5        1.1        ad  * All rights reserved.
      6        1.1        ad  *
      7        1.1        ad  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1        ad  * by Andrew Doran.
      9        1.1        ad  *
     10        1.1        ad  * Redistribution and use in source and binary forms, with or without
     11        1.1        ad  * modification, are permitted provided that the following conditions
     12        1.1        ad  * are met:
     13        1.1        ad  * 1. Redistributions of source code must retain the above copyright
     14        1.1        ad  *    notice, this list of conditions and the following disclaimer.
     15        1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     17        1.1        ad  *    documentation and/or other materials provided with the distribution.
     18        1.1        ad  * 3. All advertising materials mentioning features or use of this software
     19        1.1        ad  *    must display the following acknowledgement:
     20        1.1        ad  *        This product includes software developed by the NetBSD
     21        1.1        ad  *        Foundation, Inc. and its contributors.
     22        1.1        ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23        1.1        ad  *    contributors may be used to endorse or promote products derived
     24        1.1        ad  *    from this software without specific prior written permission.
     25        1.1        ad  *
     26        1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27        1.1        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28        1.1        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29        1.1        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30        1.1        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31        1.1        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32        1.1        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33        1.1        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34        1.1        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35        1.1        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36        1.1        ad  * POSSIBILITY OF SUCH DAMAGE.
     37        1.1        ad  */
     38        1.1        ad 
     39        1.1        ad /*-
     40        1.1        ad  * Copyright (c) 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.12.2.6   nathanw 
     72   1.12.2.6   nathanw #include <sys/cdefs.h>
     73  1.12.2.14   thorpej __KERNEL_RCSID(0, "$NetBSD: twe.c,v 1.12.2.14 2003/01/03 17:08:03 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.12.2.13   thorpej #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.12.2.13   thorpej #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.12.2.13   thorpej     void (*)(struct twe_ccb *, int), void **);
    108  1.12.2.13   thorpej static int	twe_param_set(struct twe_softc *, int, int, size_t, void *);
    109        1.1        ad static void	twe_poll(struct twe_softc *);
    110        1.1        ad static int	twe_print(void *, const char *);
    111        1.1        ad static int	twe_reset(struct twe_softc *);
    112        1.1        ad static int	twe_submatch(struct device *, struct cfdata *, void *);
    113        1.1        ad static int	twe_status_check(struct twe_softc *, u_int);
    114        1.1        ad static int	twe_status_wait(struct twe_softc *, u_int, int);
    115        1.1        ad 
    116   1.12.2.8   nathanw static inline u_int32_t	twe_inl(struct twe_softc *, int);
    117  1.12.2.13   thorpej static inline void twe_outl(struct twe_softc *, int, u_int32_t);
    118  1.12.2.13   thorpej 
    119  1.12.2.13   thorpej dev_type_open(tweopen);
    120  1.12.2.13   thorpej dev_type_close(tweclose);
    121  1.12.2.13   thorpej dev_type_ioctl(tweioctl);
    122  1.12.2.13   thorpej 
    123  1.12.2.13   thorpej const struct cdevsw twe_cdevsw = {
    124  1.12.2.13   thorpej 	tweopen, tweclose, noread, nowrite, tweioctl,
    125  1.12.2.13   thorpej 	nostop, notty, nopoll, nommap,
    126  1.12.2.13   thorpej };
    127  1.12.2.13   thorpej 
    128  1.12.2.13   thorpej extern struct	cfdriver twe_cd;
    129   1.12.2.8   nathanw 
    130  1.12.2.11   nathanw CFATTACH_DECL(twe, sizeof(struct twe_softc),
    131  1.12.2.11   nathanw     twe_match, twe_attach, NULL, NULL);
    132        1.1        ad 
    133        1.1        ad struct {
    134   1.12.2.9   nathanw 	const u_int	aen;	/* High byte indicates type of message */
    135        1.1        ad 	const char	*desc;
    136        1.1        ad } static const twe_aen_names[] = {
    137        1.1        ad 	{ 0x0000, "queue empty" },
    138        1.1        ad 	{ 0x0001, "soft reset" },
    139        1.3        ad 	{ 0x0102, "degraded mirror" },
    140        1.1        ad 	{ 0x0003, "controller error" },
    141        1.3        ad 	{ 0x0104, "rebuild fail" },
    142        1.3        ad 	{ 0x0105, "rebuild done" },
    143        1.3        ad 	{ 0x0106, "incompatible unit" },
    144   1.12.2.9   nathanw 	{ 0x0107, "initialisation done" },
    145   1.12.2.9   nathanw 	{ 0x0108, "unclean shutdown detected" },
    146   1.12.2.9   nathanw 	{ 0x0109, "drive timeout" },
    147        1.3        ad 	{ 0x010a, "drive error" },
    148        1.3        ad 	{ 0x010b, "rebuild started" },
    149   1.12.2.1   nathanw 	{ 0x010c, "init started" },
    150   1.12.2.9   nathanw 	{ 0x010d, "logical unit deleted" },
    151   1.12.2.9   nathanw 	{ 0x020f, "SMART threshold exceeded" },
    152   1.12.2.9   nathanw 	{ 0x0015, "table undefined" },	/* XXX: Not in FreeBSD's table */
    153   1.12.2.9   nathanw 	{ 0x0221, "ATA UDMA downgrade" },
    154   1.12.2.9   nathanw 	{ 0x0222, "ATA UDMA upgrade" },
    155   1.12.2.9   nathanw 	{ 0x0222, "ATA UDMA upgrade" },
    156   1.12.2.9   nathanw 	{ 0x0223, "Sector repair occurred" },
    157   1.12.2.9   nathanw 	{ 0x0024, "SBUF integrity check failure" },
    158   1.12.2.9   nathanw 	{ 0x0225, "lost cached write" },
    159   1.12.2.9   nathanw 	{ 0x0226, "drive ECC error detected" },
    160   1.12.2.9   nathanw 	{ 0x0227, "DCB checksum error" },
    161   1.12.2.9   nathanw 	{ 0x0228, "DCB unsupported version" },
    162   1.12.2.9   nathanw 	{ 0x0129, "verify started" },
    163   1.12.2.9   nathanw 	{ 0x012a, "verify failed" },
    164   1.12.2.9   nathanw 	{ 0x012b, "verify complete" },
    165   1.12.2.9   nathanw 	{ 0x022c, "overwrote bad sector during rebuild" },
    166   1.12.2.9   nathanw 	{ 0x022d, "encountered bad sector during rebuild" },
    167        1.1        ad 	{ 0x00ff, "aen queue full" },
    168        1.1        ad };
    169        1.1        ad 
    170   1.12.2.9   nathanw /*
    171   1.12.2.9   nathanw  * The high byte of the message above determines the format,
    172   1.12.2.9   nathanw  * currently we know about format 0 (no unit/port specific)
    173   1.12.2.9   nathanw  * format 1 (unit specific message), and format 2 (port specific message).
    174   1.12.2.9   nathanw  */
    175   1.12.2.9   nathanw static const char *aenfmt[] = {
    176  1.12.2.10   nathanw 	"",		/* No message */
    177   1.12.2.9   nathanw 	"unit %d: ",	/* Unit message */
    178   1.12.2.9   nathanw 	"port %d: "	/* Port message */
    179   1.12.2.9   nathanw };
    180   1.12.2.9   nathanw 
    181   1.12.2.9   nathanw 
    182   1.12.2.8   nathanw static inline u_int32_t
    183   1.12.2.8   nathanw twe_inl(struct twe_softc *sc, int off)
    184   1.12.2.8   nathanw {
    185   1.12.2.8   nathanw 
    186   1.12.2.8   nathanw 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
    187   1.12.2.8   nathanw 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
    188   1.12.2.8   nathanw 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, off));
    189   1.12.2.8   nathanw }
    190   1.12.2.8   nathanw 
    191   1.12.2.8   nathanw static inline void
    192   1.12.2.8   nathanw twe_outl(struct twe_softc *sc, int off, u_int32_t val)
    193   1.12.2.8   nathanw {
    194   1.12.2.8   nathanw 
    195   1.12.2.8   nathanw 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, off, val);
    196   1.12.2.8   nathanw 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
    197   1.12.2.8   nathanw 	    BUS_SPACE_BARRIER_WRITE);
    198   1.12.2.8   nathanw }
    199   1.12.2.8   nathanw 
    200        1.1        ad /*
    201        1.1        ad  * Match a supported board.
    202        1.1        ad  */
    203        1.1        ad static int
    204        1.1        ad twe_match(struct device *parent, struct cfdata *cfdata, void *aux)
    205        1.1        ad {
    206        1.1        ad 	struct pci_attach_args *pa;
    207        1.1        ad 
    208        1.1        ad 	pa = aux;
    209        1.1        ad 
    210        1.1        ad 	return (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3WARE &&
    211       1.10        ad 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_ESCALADE ||
    212       1.10        ad 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_ESCALADE_ASIC));
    213        1.1        ad }
    214        1.1        ad 
    215        1.1        ad /*
    216        1.1        ad  * Attach a supported board.
    217        1.1        ad  *
    218        1.1        ad  * XXX This doesn't fail gracefully.
    219        1.1        ad  */
    220        1.1        ad static void
    221        1.1        ad twe_attach(struct device *parent, struct device *self, void *aux)
    222        1.1        ad {
    223        1.1        ad 	struct pci_attach_args *pa;
    224        1.1        ad 	struct twe_softc *sc;
    225        1.1        ad 	pci_chipset_tag_t pc;
    226        1.1        ad 	pci_intr_handle_t ih;
    227        1.1        ad 	pcireg_t csr;
    228        1.1        ad 	const char *intrstr;
    229        1.1        ad 	int size, i, rv, rseg;
    230   1.12.2.8   nathanw 	size_t max_segs, max_xfer;
    231        1.1        ad 	struct twe_param *dtp, *ctp;
    232        1.1        ad 	bus_dma_segment_t seg;
    233        1.1        ad 	struct twe_cmd *tc;
    234        1.1        ad 	struct twe_attach_args twea;
    235        1.1        ad 	struct twe_ccb *ccb;
    236        1.1        ad 
    237        1.1        ad 	sc = (struct twe_softc *)self;
    238        1.1        ad 	pa = aux;
    239        1.1        ad 	pc = pa->pa_pc;
    240        1.1        ad 	sc->sc_dmat = pa->pa_dmat;
    241        1.1        ad 	SIMPLEQ_INIT(&sc->sc_ccb_queue);
    242        1.1        ad 	SLIST_INIT(&sc->sc_ccb_freelist);
    243        1.1        ad 
    244        1.3        ad 	printf(": 3ware Escalade\n");
    245        1.1        ad 
    246  1.12.2.13   thorpej 	ccb = malloc(sizeof(*ccb) * TWE_MAX_QUEUECNT, M_DEVBUF, M_NOWAIT);
    247  1.12.2.13   thorpej 	if (ccb == NULL) {
    248  1.12.2.13   thorpej 		printf("%s: unable to allocate memory for ccbs\n",
    249  1.12.2.13   thorpej 		    sc->sc_dv.dv_xname);
    250  1.12.2.13   thorpej 		return;
    251  1.12.2.13   thorpej 	}
    252  1.12.2.13   thorpej 
    253        1.1        ad 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    254        1.1        ad 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    255        1.1        ad 		printf("%s: can't map i/o space\n", sc->sc_dv.dv_xname);
    256        1.1        ad 		return;
    257        1.1        ad 	}
    258        1.1        ad 
    259        1.1        ad 	/* Enable the device. */
    260        1.1        ad 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    261        1.1        ad 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    262        1.1        ad 	    csr | PCI_COMMAND_MASTER_ENABLE);
    263        1.1        ad 
    264        1.1        ad 	/* Map and establish the interrupt. */
    265        1.5  sommerfe 	if (pci_intr_map(pa, &ih)) {
    266        1.1        ad 		printf("%s: can't map interrupt\n", sc->sc_dv.dv_xname);
    267        1.1        ad 		return;
    268        1.1        ad 	}
    269        1.1        ad 	intrstr = pci_intr_string(pc, ih);
    270        1.1        ad 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, twe_intr, sc);
    271        1.1        ad 	if (sc->sc_ih == NULL) {
    272        1.1        ad 		printf("%s: can't establish interrupt", sc->sc_dv.dv_xname);
    273        1.1        ad 		if (intrstr != NULL)
    274        1.1        ad 			printf(" at %s", intrstr);
    275        1.1        ad 		printf("\n");
    276        1.1        ad 		return;
    277        1.1        ad 	}
    278        1.1        ad 	if (intrstr != NULL)
    279        1.1        ad 		printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname, intrstr);
    280        1.1        ad 
    281        1.1        ad 	/*
    282        1.1        ad 	 * Allocate and initialise the command blocks and CCBs.
    283        1.1        ad 	 */
    284        1.7        ad         size = sizeof(struct twe_cmd) * TWE_MAX_QUEUECNT;
    285        1.1        ad 
    286        1.4   thorpej 	if ((rv = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
    287        1.1        ad 	    &rseg, BUS_DMA_NOWAIT)) != 0) {
    288        1.1        ad 		printf("%s: unable to allocate commands, rv = %d\n",
    289        1.1        ad 		    sc->sc_dv.dv_xname, rv);
    290        1.1        ad 		return;
    291        1.1        ad 	}
    292        1.1        ad 
    293        1.1        ad 	if ((rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
    294        1.1        ad 	    (caddr_t *)&sc->sc_cmds,
    295        1.1        ad 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    296        1.1        ad 		printf("%s: unable to map commands, rv = %d\n",
    297        1.1        ad 		    sc->sc_dv.dv_xname, rv);
    298        1.1        ad 		return;
    299        1.1        ad 	}
    300        1.1        ad 
    301        1.1        ad 	if ((rv = bus_dmamap_create(sc->sc_dmat, size, size, 1, 0,
    302        1.1        ad 	    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    303        1.1        ad 		printf("%s: unable to create command DMA map, rv = %d\n",
    304        1.1        ad 		    sc->sc_dv.dv_xname, rv);
    305        1.1        ad 		return;
    306        1.1        ad 	}
    307        1.1        ad 
    308        1.1        ad 	if ((rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_cmds,
    309        1.1        ad 	    size, NULL, BUS_DMA_NOWAIT)) != 0) {
    310        1.1        ad 		printf("%s: unable to load command DMA map, rv = %d\n",
    311        1.1        ad 		    sc->sc_dv.dv_xname, rv);
    312        1.1        ad 		return;
    313        1.1        ad 	}
    314        1.1        ad 
    315        1.1        ad 	sc->sc_cmds_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
    316        1.1        ad 	memset(sc->sc_cmds, 0, size);
    317        1.1        ad 
    318        1.1        ad 	sc->sc_ccbs = ccb;
    319        1.1        ad 	tc = (struct twe_cmd *)sc->sc_cmds;
    320   1.12.2.8   nathanw 	max_segs = twe_get_maxsegs();
    321   1.12.2.8   nathanw 	max_xfer = twe_get_maxxfer(max_segs);
    322        1.1        ad 
    323        1.7        ad 	for (i = 0; i < TWE_MAX_QUEUECNT; i++, tc++, ccb++) {
    324        1.1        ad 		ccb->ccb_cmd = tc;
    325        1.1        ad 		ccb->ccb_cmdid = i;
    326        1.1        ad 		ccb->ccb_flags = 0;
    327   1.12.2.8   nathanw 		rv = bus_dmamap_create(sc->sc_dmat, max_xfer,
    328   1.12.2.8   nathanw 		    max_segs, PAGE_SIZE, 0,
    329        1.4   thorpej 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    330        1.1        ad 		    &ccb->ccb_dmamap_xfer);
    331        1.7        ad 		if (rv != 0) {
    332        1.7        ad 			printf("%s: can't create dmamap, rv = %d\n",
    333        1.7        ad 			    sc->sc_dv.dv_xname, rv);
    334        1.7        ad 			return;
    335        1.7        ad 		}
    336        1.3        ad 		/* Save one CCB for parameter retrieval. */
    337        1.3        ad 		if (i != 0)
    338        1.3        ad 			SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb,
    339        1.3        ad 			    ccb_chain.slist);
    340        1.3        ad 	}
    341        1.1        ad 
    342        1.1        ad 	/* Wait for the controller to become ready. */
    343        1.1        ad 	if (twe_status_wait(sc, TWE_STS_MICROCONTROLLER_READY, 6)) {
    344        1.1        ad 		printf("%s: microcontroller not ready\n", sc->sc_dv.dv_xname);
    345        1.1        ad 		return;
    346        1.1        ad 	}
    347        1.1        ad 
    348   1.12.2.8   nathanw 	twe_outl(sc, TWE_REG_CTL, TWE_CTL_DISABLE_INTRS);
    349        1.1        ad 
    350        1.1        ad 	/* Reset the controller. */
    351        1.1        ad 	if (twe_reset(sc)) {
    352        1.1        ad 		printf("%s: reset failed\n", sc->sc_dv.dv_xname);
    353        1.1        ad 		return;
    354        1.1        ad 	}
    355        1.1        ad 
    356        1.3        ad 	/* Find attached units. */
    357        1.7        ad 	rv = twe_param_get(sc, TWE_PARAM_UNITSUMMARY,
    358  1.12.2.12   thorpej 	    TWE_PARAM_UNITSUMMARY_Status, TWE_MAX_UNITS, NULL, (void *)&dtp);
    359        1.7        ad 	if (rv != 0) {
    360        1.7        ad 		printf("%s: can't detect attached units (%d)\n",
    361        1.7        ad 		    sc->sc_dv.dv_xname, rv);
    362        1.1        ad 		return;
    363        1.1        ad 	}
    364        1.1        ad 
    365        1.1        ad 	/* For each detected unit, collect size and store in an array. */
    366        1.3        ad 	for (i = 0, sc->sc_nunits = 0; i < TWE_MAX_UNITS; i++) {
    367        1.1        ad 		/* Unit present? */
    368        1.3        ad 		if ((dtp->tp_data[i] & TWE_PARAM_UNITSTATUS_Online) == 0) {
    369        1.1        ad 			sc->sc_dsize[i] = 0;
    370        1.1        ad 	   		continue;
    371        1.1        ad 	   	}
    372        1.1        ad 
    373        1.7        ad 		rv = twe_param_get(sc, TWE_PARAM_UNITINFO + i,
    374  1.12.2.12   thorpej 		    TWE_PARAM_UNITINFO_Capacity, 4, NULL, (void *)&ctp);
    375        1.7        ad 		if (rv != 0) {
    376        1.7        ad 			printf("%s: error %d fetching capacity for unit %d\n",
    377        1.7        ad 			    sc->sc_dv.dv_xname, rv, i);
    378        1.1        ad 			continue;
    379        1.1        ad 		}
    380        1.1        ad 
    381        1.1        ad 		sc->sc_dsize[i] = le32toh(*(u_int32_t *)ctp->tp_data);
    382        1.1        ad 		free(ctp, M_DEVBUF);
    383        1.3        ad 		sc->sc_nunits++;
    384        1.1        ad 	}
    385        1.1        ad 	free(dtp, M_DEVBUF);
    386        1.1        ad 
    387        1.1        ad 	/* Initialise connection with controller and enable interrupts. */
    388        1.1        ad 	twe_init_connection(sc);
    389   1.12.2.8   nathanw 	twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR |
    390        1.1        ad 	    TWE_CTL_UNMASK_RESP_INTR |
    391        1.1        ad 	    TWE_CTL_ENABLE_INTRS);
    392        1.1        ad 
    393        1.1        ad 	/* Attach sub-devices. */
    394        1.1        ad 	for (i = 0; i < TWE_MAX_UNITS; i++) {
    395        1.1        ad 		if (sc->sc_dsize[i] == 0)
    396        1.1        ad 			continue;
    397        1.1        ad 		twea.twea_unit = i;
    398        1.1        ad 		config_found_sm(&sc->sc_dv, &twea, twe_print, twe_submatch);
    399        1.1        ad 	}
    400        1.1        ad }
    401        1.1        ad 
    402        1.1        ad /*
    403        1.1        ad  * Reset the controller.  Currently only useful at attach time; must be
    404        1.1        ad  * called with interrupts blocked.
    405        1.1        ad  */
    406        1.1        ad static int
    407        1.1        ad twe_reset(struct twe_softc *sc)
    408        1.1        ad {
    409        1.1        ad 	struct twe_param *tp;
    410        1.1        ad 	u_int aen, status;
    411        1.1        ad 	volatile u_int32_t junk;
    412        1.7        ad 	int got, rv;
    413        1.1        ad 
    414        1.1        ad 	/* Issue a soft reset. */
    415   1.12.2.8   nathanw 	twe_outl(sc, TWE_REG_CTL, TWE_CTL_ISSUE_SOFT_RESET |
    416        1.1        ad 	    TWE_CTL_CLEAR_HOST_INTR |
    417        1.1        ad 	    TWE_CTL_CLEAR_ATTN_INTR |
    418        1.1        ad 	    TWE_CTL_MASK_CMD_INTR |
    419        1.1        ad 	    TWE_CTL_MASK_RESP_INTR |
    420        1.1        ad 	    TWE_CTL_CLEAR_ERROR_STS |
    421        1.1        ad 	    TWE_CTL_DISABLE_INTRS);
    422        1.1        ad 
    423        1.1        ad 	if (twe_status_wait(sc, TWE_STS_ATTN_INTR, 15)) {
    424        1.1        ad 		printf("%s: no attention interrupt\n",
    425        1.1        ad 		    sc->sc_dv.dv_xname);
    426        1.1        ad 		return (-1);
    427        1.1        ad 	}
    428        1.1        ad 
    429        1.1        ad 	/* Pull AENs out of the controller; look for a soft reset AEN. */
    430        1.1        ad 	for (got = 0;;) {
    431        1.7        ad 		rv = twe_param_get(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode,
    432  1.12.2.12   thorpej 		    2, NULL, (void *)&tp);
    433        1.7        ad 		if (rv != 0)
    434        1.7        ad 			printf("%s: error %d while draining response queue\n",
    435        1.7        ad 			    sc->sc_dv.dv_xname, rv);
    436        1.3        ad 		aen = TWE_AEN_CODE(le16toh(*(u_int16_t *)tp->tp_data));
    437        1.1        ad 		free(tp, M_DEVBUF);
    438        1.1        ad 		if (aen == TWE_AEN_QUEUE_EMPTY)
    439        1.1        ad 			break;
    440        1.1        ad 		if (aen == TWE_AEN_SOFT_RESET)
    441        1.1        ad 			got = 1;
    442        1.1        ad 	}
    443        1.1        ad 	if (!got) {
    444        1.1        ad 		printf("%s: reset not reported\n", sc->sc_dv.dv_xname);
    445        1.1        ad 		return (-1);
    446        1.1        ad 	}
    447        1.1        ad 
    448        1.1        ad 	/* Check controller status. */
    449   1.12.2.8   nathanw 	status = twe_inl(sc, TWE_REG_STS);
    450        1.1        ad 	if (twe_status_check(sc, status)) {
    451        1.1        ad 		printf("%s: controller errors detected\n",
    452        1.1        ad 		    sc->sc_dv.dv_xname);
    453        1.1        ad 		return (-1);
    454        1.1        ad 	}
    455        1.1        ad 
    456        1.1        ad 	/* Drain the response queue. */
    457        1.1        ad 	for (;;) {
    458   1.12.2.8   nathanw 		status = twe_inl(sc, TWE_REG_STS);
    459        1.1        ad 		if (twe_status_check(sc, status) != 0) {
    460        1.1        ad 			printf("%s: can't drain response queue\n",
    461        1.1        ad 			    sc->sc_dv.dv_xname);
    462        1.1        ad 			return (-1);
    463        1.1        ad 		}
    464        1.1        ad 		if ((status & TWE_STS_RESP_QUEUE_EMPTY) != 0)
    465        1.1        ad 			break;
    466   1.12.2.8   nathanw 		junk = twe_inl(sc, TWE_REG_RESP_QUEUE);
    467        1.1        ad 	}
    468        1.1        ad 
    469        1.1        ad 	return (0);
    470        1.1        ad }
    471        1.1        ad 
    472        1.1        ad /*
    473        1.1        ad  * Print autoconfiguration message for a sub-device.
    474        1.1        ad  */
    475        1.1        ad static int
    476        1.1        ad twe_print(void *aux, const char *pnp)
    477        1.1        ad {
    478        1.1        ad 	struct twe_attach_args *twea;
    479        1.1        ad 
    480        1.1        ad 	twea = aux;
    481        1.1        ad 
    482        1.1        ad 	if (pnp != NULL)
    483  1.12.2.14   thorpej 		aprint_normal("block device at %s", pnp);
    484  1.12.2.14   thorpej 	aprint_normal(" unit %d", twea->twea_unit);
    485        1.1        ad 	return (UNCONF);
    486        1.1        ad }
    487        1.1        ad 
    488        1.1        ad /*
    489        1.1        ad  * Match a sub-device.
    490        1.1        ad  */
    491        1.1        ad static int
    492        1.1        ad twe_submatch(struct device *parent, struct cfdata *cf, void *aux)
    493        1.1        ad {
    494        1.1        ad 	struct twe_attach_args *twea;
    495        1.1        ad 
    496        1.1        ad 	twea = aux;
    497        1.1        ad 
    498        1.1        ad 	if (cf->tweacf_unit != TWECF_UNIT_DEFAULT &&
    499        1.1        ad 	    cf->tweacf_unit != twea->twea_unit)
    500        1.1        ad 		return (0);
    501        1.1        ad 
    502  1.12.2.11   nathanw 	return (config_match(parent, cf, aux));
    503        1.1        ad }
    504        1.1        ad 
    505        1.1        ad /*
    506        1.1        ad  * Interrupt service routine.
    507        1.1        ad  */
    508        1.1        ad static int
    509        1.1        ad twe_intr(void *arg)
    510        1.1        ad {
    511        1.1        ad 	struct twe_softc *sc;
    512        1.1        ad 	u_int status;
    513        1.7        ad 	int caught, rv;
    514        1.1        ad 
    515        1.1        ad 	sc = arg;
    516        1.1        ad 	caught = 0;
    517   1.12.2.8   nathanw 	status = twe_inl(sc, TWE_REG_STS);
    518        1.1        ad 	twe_status_check(sc, status);
    519        1.1        ad 
    520        1.1        ad 	/* Host interrupts - purpose unknown. */
    521        1.1        ad 	if ((status & TWE_STS_HOST_INTR) != 0) {
    522        1.1        ad #ifdef DIAGNOSTIC
    523        1.1        ad 		printf("%s: host interrupt\n", sc->sc_dv.dv_xname);
    524        1.1        ad #endif
    525   1.12.2.8   nathanw 		twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_HOST_INTR);
    526        1.1        ad 		caught = 1;
    527        1.1        ad 	}
    528        1.1        ad 
    529        1.1        ad 	/*
    530        1.1        ad 	 * Attention interrupts, signalled when a controller or child device
    531   1.12.2.4   nathanw 	 * state change has occurred.
    532        1.1        ad 	 */
    533        1.1        ad 	if ((status & TWE_STS_ATTN_INTR) != 0) {
    534       1.12        ad 		if ((sc->sc_flags & TWEF_AEN) == 0) {
    535       1.12        ad 			rv = twe_param_get(sc, TWE_PARAM_AEN,
    536       1.12        ad 			    TWE_PARAM_AEN_UnitCode, 2, twe_aen_handler,
    537       1.12        ad 			    NULL);
    538       1.12        ad 			if (rv != 0) {
    539       1.12        ad 				printf("%s: unable to retrieve AEN (%d)\n",
    540       1.12        ad 				    sc->sc_dv.dv_xname, rv);
    541   1.12.2.8   nathanw 				twe_outl(sc, TWE_REG_CTL,
    542       1.12        ad 				    TWE_CTL_CLEAR_ATTN_INTR);
    543       1.12        ad 			} else
    544       1.12        ad 				sc->sc_flags |= TWEF_AEN;
    545        1.9        ad 		}
    546        1.1        ad 		caught = 1;
    547        1.1        ad 	}
    548        1.1        ad 
    549        1.1        ad 	/*
    550        1.1        ad 	 * Command interrupts, signalled when the controller can accept more
    551        1.1        ad 	 * commands.  We don't use this; instead, we try to submit commands
    552        1.1        ad 	 * when we receive them, and when other commands have completed.
    553        1.1        ad 	 * Mask it so we don't get another one.
    554        1.1        ad 	 */
    555        1.1        ad 	if ((status & TWE_STS_CMD_INTR) != 0) {
    556        1.1        ad #ifdef DIAGNOSTIC
    557        1.1        ad 		printf("%s: command interrupt\n", sc->sc_dv.dv_xname);
    558        1.1        ad #endif
    559   1.12.2.8   nathanw 		twe_outl(sc, TWE_REG_CTL, TWE_CTL_MASK_CMD_INTR);
    560        1.1        ad 		caught = 1;
    561        1.1        ad 	}
    562        1.1        ad 
    563        1.1        ad 	if ((status & TWE_STS_RESP_INTR) != 0) {
    564        1.1        ad 		twe_poll(sc);
    565        1.1        ad 		caught = 1;
    566        1.1        ad 	}
    567        1.1        ad 
    568        1.1        ad 	return (caught);
    569        1.1        ad }
    570        1.1        ad 
    571        1.1        ad /*
    572        1.1        ad  * Handle an AEN returned by the controller.
    573        1.1        ad  */
    574        1.1        ad static void
    575        1.1        ad twe_aen_handler(struct twe_ccb *ccb, int error)
    576        1.1        ad {
    577        1.1        ad 	struct twe_softc *sc;
    578        1.1        ad 	struct twe_param *tp;
    579        1.1        ad 	const char *str;
    580        1.1        ad 	u_int aen;
    581        1.7        ad 	int i, hu, rv;
    582        1.1        ad 
    583        1.1        ad 	sc = (struct twe_softc *)ccb->ccb_tx.tx_dv;
    584        1.1        ad 	tp = ccb->ccb_tx.tx_context;
    585        1.1        ad 	twe_ccb_unmap(sc, ccb);
    586        1.1        ad 
    587        1.3        ad 	if (error) {
    588        1.1        ad 		printf("%s: error retrieving AEN\n", sc->sc_dv.dv_xname);
    589        1.3        ad 		aen = TWE_AEN_QUEUE_EMPTY;
    590        1.3        ad 	} else
    591        1.1        ad 		aen = le16toh(*(u_int16_t *)tp->tp_data);
    592        1.3        ad 	free(tp, M_DEVBUF);
    593        1.3        ad 	twe_ccb_free(sc, ccb);
    594        1.3        ad 
    595        1.7        ad 	if (TWE_AEN_CODE(aen) == TWE_AEN_QUEUE_EMPTY) {
    596   1.12.2.8   nathanw 		twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR);
    597       1.12        ad 		sc->sc_flags &= ~TWEF_AEN;
    598        1.7        ad 		return;
    599        1.7        ad 	}
    600        1.7        ad 
    601        1.7        ad 	str = "<unknown>";
    602        1.7        ad 	i = 0;
    603        1.7        ad 	hu = 0;
    604        1.3        ad 
    605        1.7        ad 	while (i < sizeof(twe_aen_names) / sizeof(twe_aen_names[0])) {
    606        1.7        ad 		if (TWE_AEN_CODE(twe_aen_names[i].aen) == TWE_AEN_CODE(aen)) {
    607        1.7        ad 			str = twe_aen_names[i].desc;
    608   1.12.2.9   nathanw 			hu = TWE_AEN_UNIT(twe_aen_names[i].aen);
    609        1.7        ad 			break;
    610        1.7        ad 		}
    611        1.7        ad 		i++;
    612        1.7        ad 	}
    613   1.12.2.9   nathanw 	printf("%s: ", sc->sc_dv.dv_xname);
    614   1.12.2.9   nathanw 	printf(aenfmt[hu], TWE_AEN_UNIT(aen));
    615   1.12.2.9   nathanw 	printf("AEN 0x%04x (%s) received\n", TWE_AEN_CODE(aen), str);
    616        1.3        ad 
    617        1.7        ad 	/*
    618        1.7        ad 	 * Chain another retrieval in case interrupts have been
    619        1.7        ad 	 * coalesced.
    620        1.7        ad 	 */
    621        1.7        ad 	rv = twe_param_get(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2,
    622        1.7        ad 	    twe_aen_handler, NULL);
    623        1.7        ad 	if (rv != 0)
    624        1.7        ad 		printf("%s: unable to retrieve AEN (%d)\n",
    625        1.7        ad 		    sc->sc_dv.dv_xname, rv);
    626        1.1        ad }
    627        1.1        ad 
    628        1.1        ad /*
    629        1.1        ad  * Execute a TWE_OP_GET_PARAM command.  If a callback function is provided,
    630        1.1        ad  * it will be called with generated context when the command has completed.
    631        1.1        ad  * If no callback is provided, the command will be executed synchronously
    632        1.3        ad  * and a pointer to a buffer containing the data returned.
    633        1.1        ad  *
    634        1.3        ad  * The caller or callback is responsible for freeing the buffer.
    635        1.1        ad  */
    636        1.7        ad static int
    637        1.1        ad twe_param_get(struct twe_softc *sc, int table_id, int param_id, size_t size,
    638        1.7        ad 	      void (*func)(struct twe_ccb *, int), void **pbuf)
    639        1.1        ad {
    640        1.1        ad 	struct twe_ccb *ccb;
    641        1.1        ad 	struct twe_cmd *tc;
    642        1.1        ad 	struct twe_param *tp;
    643        1.1        ad 	int rv, s;
    644        1.1        ad 
    645  1.12.2.13   thorpej 	tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
    646  1.12.2.13   thorpej 	if (tp == NULL)
    647  1.12.2.13   thorpej 		return ENOMEM;
    648  1.12.2.13   thorpej 
    649        1.7        ad 	rv = twe_ccb_alloc(sc, &ccb,
    650        1.7        ad 	    TWE_CCB_PARAM | TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
    651        1.7        ad 	if (rv != 0)
    652  1.12.2.13   thorpej 		goto done;
    653        1.1        ad 
    654        1.1        ad 	ccb->ccb_data = tp;
    655        1.1        ad 	ccb->ccb_datasize = TWE_SECTOR_SIZE;
    656        1.1        ad 	ccb->ccb_tx.tx_handler = func;
    657        1.1        ad 	ccb->ccb_tx.tx_context = tp;
    658        1.1        ad 	ccb->ccb_tx.tx_dv = &sc->sc_dv;
    659        1.1        ad 
    660        1.1        ad 	tc = ccb->ccb_cmd;
    661        1.1        ad 	tc->tc_size = 2;
    662        1.1        ad 	tc->tc_opcode = TWE_OP_GET_PARAM | (tc->tc_size << 5);
    663        1.1        ad 	tc->tc_unit = 0;
    664        1.1        ad 	tc->tc_count = htole16(1);
    665        1.1        ad 
    666        1.1        ad 	/* Fill in the outbound parameter data. */
    667        1.1        ad 	tp->tp_table_id = htole16(table_id);
    668        1.1        ad 	tp->tp_param_id = param_id;
    669        1.1        ad 	tp->tp_param_size = size;
    670        1.1        ad 
    671        1.1        ad 	/* Map the transfer. */
    672        1.7        ad 	if ((rv = twe_ccb_map(sc, ccb)) != 0) {
    673        1.2        ad 		twe_ccb_free(sc, ccb);
    674  1.12.2.13   thorpej 		goto done;
    675        1.1        ad 	}
    676        1.1        ad 
    677        1.1        ad 	/* Submit the command and either wait or let the callback handle it. */
    678        1.1        ad 	if (func == NULL) {
    679        1.1        ad 		s = splbio();
    680        1.7        ad 		rv = twe_ccb_poll(sc, ccb, 5);
    681        1.1        ad 		twe_ccb_unmap(sc, ccb);
    682        1.2        ad 		twe_ccb_free(sc, ccb);
    683        1.1        ad 		splx(s);
    684        1.1        ad 	} else {
    685  1.12.2.13   thorpej #ifdef DIAGNOSTIC
    686  1.12.2.13   thorpej 		if (pbuf != NULL)
    687  1.12.2.13   thorpej 			panic("both func and pbuf defined");
    688  1.12.2.13   thorpej #endif
    689        1.1        ad 		twe_ccb_enqueue(sc, ccb);
    690  1.12.2.13   thorpej 		return 0;
    691  1.12.2.13   thorpej 	}
    692  1.12.2.13   thorpej 
    693  1.12.2.13   thorpej done:
    694  1.12.2.13   thorpej 	if (pbuf == NULL || rv != 0)
    695  1.12.2.13   thorpej 		free(tp, M_DEVBUF);
    696  1.12.2.13   thorpej 	else if (pbuf != NULL && rv == 0)
    697  1.12.2.13   thorpej 		*pbuf = tp;
    698  1.12.2.13   thorpej 	return rv;
    699  1.12.2.13   thorpej }
    700  1.12.2.13   thorpej 
    701  1.12.2.13   thorpej /*
    702  1.12.2.13   thorpej  * Execute a TWE_OP_SET_PARAM command.
    703  1.12.2.13   thorpej  */
    704  1.12.2.13   thorpej static int
    705  1.12.2.13   thorpej twe_param_set(struct twe_softc *sc, int table_id, int param_id, size_t size,
    706  1.12.2.13   thorpej 	      void *buf)
    707  1.12.2.13   thorpej {
    708  1.12.2.13   thorpej 	struct twe_ccb *ccb;
    709  1.12.2.13   thorpej 	struct twe_cmd *tc;
    710  1.12.2.13   thorpej 	struct twe_param *tp;
    711  1.12.2.13   thorpej 	int rv, s;
    712  1.12.2.13   thorpej 
    713  1.12.2.13   thorpej 	tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
    714  1.12.2.13   thorpej 	if (tp == NULL)
    715  1.12.2.13   thorpej 		return ENOMEM;
    716  1.12.2.13   thorpej 
    717  1.12.2.13   thorpej 	rv = twe_ccb_alloc(sc, &ccb,
    718  1.12.2.13   thorpej 	    TWE_CCB_PARAM | TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
    719  1.12.2.13   thorpej 	if (rv != 0)
    720  1.12.2.13   thorpej 		goto done;
    721  1.12.2.13   thorpej 
    722  1.12.2.13   thorpej 	ccb->ccb_data = tp;
    723  1.12.2.13   thorpej 	ccb->ccb_datasize = TWE_SECTOR_SIZE;
    724  1.12.2.13   thorpej 	ccb->ccb_tx.tx_handler = 0;
    725  1.12.2.13   thorpej 	ccb->ccb_tx.tx_context = tp;
    726  1.12.2.13   thorpej 	ccb->ccb_tx.tx_dv = &sc->sc_dv;
    727  1.12.2.13   thorpej 
    728  1.12.2.13   thorpej 	tc = ccb->ccb_cmd;
    729  1.12.2.13   thorpej 	tc->tc_size = 2;
    730  1.12.2.13   thorpej 	tc->tc_opcode = TWE_OP_SET_PARAM | (tc->tc_size << 5);
    731  1.12.2.13   thorpej 	tc->tc_unit = 0;
    732  1.12.2.13   thorpej 	tc->tc_count = htole16(1);
    733  1.12.2.13   thorpej 
    734  1.12.2.13   thorpej 	/* Fill in the outbound parameter data. */
    735  1.12.2.13   thorpej 	tp->tp_table_id = htole16(table_id);
    736  1.12.2.13   thorpej 	tp->tp_param_id = param_id;
    737  1.12.2.13   thorpej 	tp->tp_param_size = size;
    738  1.12.2.13   thorpej 	memcpy(tp->tp_data, buf, size);
    739  1.12.2.13   thorpej 
    740  1.12.2.13   thorpej 	/* Map the transfer. */
    741  1.12.2.13   thorpej 	if ((rv = twe_ccb_map(sc, ccb)) != 0) {
    742  1.12.2.13   thorpej 		twe_ccb_free(sc, ccb);
    743  1.12.2.13   thorpej 		goto done;
    744        1.1        ad 	}
    745        1.1        ad 
    746  1.12.2.13   thorpej 	/* Submit the command and wait. */
    747  1.12.2.13   thorpej 	s = splbio();
    748  1.12.2.13   thorpej 	rv = twe_ccb_poll(sc, ccb, 5);
    749  1.12.2.13   thorpej 	twe_ccb_unmap(sc, ccb);
    750  1.12.2.13   thorpej 	twe_ccb_free(sc, ccb);
    751  1.12.2.13   thorpej 	splx(s);
    752  1.12.2.13   thorpej done:
    753  1.12.2.13   thorpej 	free(tp, M_DEVBUF);
    754        1.7        ad 	return (rv);
    755        1.1        ad }
    756        1.1        ad 
    757        1.1        ad /*
    758        1.1        ad  * Execute a TWE_OP_INIT_CONNECTION command.  Return non-zero on error.
    759        1.1        ad  * Must be called with interrupts blocked.
    760        1.1        ad  */
    761        1.1        ad static int
    762        1.1        ad twe_init_connection(struct twe_softc *sc)
    763  1.12.2.13   thorpej /*###762 [cc] warning: `twe_init_connection' was used with no prototype before its definition%%%*/
    764  1.12.2.13   thorpej /*###762 [cc] warning: `twe_init_connection' was declared implicitly `extern' and later `static'%%%*/
    765        1.1        ad {
    766        1.1        ad 	struct twe_ccb *ccb;
    767        1.1        ad 	struct twe_cmd *tc;
    768        1.1        ad 	int rv;
    769        1.1        ad 
    770        1.3        ad 	if ((rv = twe_ccb_alloc(sc, &ccb, 0)) != 0)
    771        1.1        ad 		return (rv);
    772        1.1        ad 
    773        1.1        ad 	/* Build the command. */
    774        1.1        ad 	tc = ccb->ccb_cmd;
    775        1.1        ad 	tc->tc_size = 3;
    776        1.1        ad 	tc->tc_opcode = TWE_OP_INIT_CONNECTION;
    777        1.1        ad 	tc->tc_unit = 0;
    778        1.3        ad 	tc->tc_count = htole16(TWE_MAX_CMDS);
    779        1.1        ad 	tc->tc_args.init_connection.response_queue_pointer = 0;
    780        1.1        ad 
    781        1.1        ad 	/* Submit the command for immediate execution. */
    782        1.7        ad 	rv = twe_ccb_poll(sc, ccb, 5);
    783        1.2        ad 	twe_ccb_free(sc, ccb);
    784        1.1        ad 	return (rv);
    785        1.1        ad }
    786        1.1        ad 
    787        1.1        ad /*
    788        1.1        ad  * Poll the controller for completed commands.  Must be called with
    789        1.1        ad  * interrupts blocked.
    790        1.1        ad  */
    791        1.1        ad static void
    792        1.1        ad twe_poll(struct twe_softc *sc)
    793        1.1        ad {
    794        1.1        ad 	struct twe_ccb *ccb;
    795        1.1        ad 	int found;
    796        1.1        ad 	u_int status, cmdid;
    797        1.1        ad 
    798        1.1        ad 	found = 0;
    799        1.1        ad 
    800        1.1        ad 	for (;;) {
    801   1.12.2.8   nathanw 		status = twe_inl(sc, TWE_REG_STS);
    802        1.1        ad 		twe_status_check(sc, status);
    803        1.1        ad 
    804        1.1        ad 		if ((status & TWE_STS_RESP_QUEUE_EMPTY))
    805        1.1        ad 			break;
    806        1.1        ad 
    807        1.1        ad 		found = 1;
    808   1.12.2.8   nathanw 		cmdid = twe_inl(sc, TWE_REG_RESP_QUEUE);
    809        1.1        ad 		cmdid = (cmdid & TWE_RESP_MASK) >> TWE_RESP_SHIFT;
    810        1.7        ad 		if (cmdid >= TWE_MAX_QUEUECNT) {
    811        1.1        ad 			printf("%s: bad completion\n", sc->sc_dv.dv_xname);
    812        1.1        ad 			continue;
    813        1.1        ad 		}
    814        1.1        ad 
    815        1.1        ad 		ccb = sc->sc_ccbs + cmdid;
    816        1.1        ad 		if ((ccb->ccb_flags & TWE_CCB_ACTIVE) == 0) {
    817        1.1        ad 			printf("%s: bad completion (not active)\n",
    818        1.1        ad 			    sc->sc_dv.dv_xname);
    819        1.1        ad 			continue;
    820        1.1        ad 		}
    821        1.1        ad 		ccb->ccb_flags ^= TWE_CCB_COMPLETE | TWE_CCB_ACTIVE;
    822        1.1        ad 
    823        1.1        ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
    824        1.1        ad 		    (caddr_t)ccb->ccb_cmd - sc->sc_cmds,
    825        1.1        ad 		    sizeof(struct twe_cmd),
    826        1.1        ad 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    827        1.1        ad 
    828        1.1        ad 		/* Pass notification to upper layers. */
    829        1.1        ad 		if (ccb->ccb_tx.tx_handler != NULL)
    830        1.1        ad 			(*ccb->ccb_tx.tx_handler)(ccb,
    831        1.1        ad 			    ccb->ccb_cmd->tc_status != 0 ? EIO : 0);
    832        1.1        ad 	}
    833        1.1        ad 
    834        1.1        ad 	/* If any commands have completed, run the software queue. */
    835        1.1        ad 	if (found)
    836        1.1        ad 		twe_ccb_enqueue(sc, NULL);
    837        1.1        ad }
    838        1.1        ad 
    839        1.1        ad /*
    840        1.1        ad  * Wait for `status' to be set in the controller status register.  Return
    841        1.1        ad  * zero if found, non-zero if the operation timed out.
    842        1.1        ad  */
    843        1.1        ad static int
    844        1.1        ad twe_status_wait(struct twe_softc *sc, u_int32_t status, int timo)
    845        1.1        ad {
    846        1.1        ad 
    847       1.11        ad 	for (timo *= 10; timo != 0; timo--) {
    848   1.12.2.8   nathanw 		if ((twe_inl(sc, TWE_REG_STS) & status) == status)
    849        1.1        ad 			break;
    850        1.1        ad 		delay(100000);
    851        1.1        ad 	}
    852        1.1        ad 
    853        1.1        ad 	return (timo == 0);
    854        1.1        ad }
    855        1.1        ad 
    856        1.1        ad /*
    857        1.1        ad  * Complain if the status bits aren't what we expect.
    858        1.1        ad  */
    859        1.1        ad static int
    860        1.1        ad twe_status_check(struct twe_softc *sc, u_int status)
    861        1.1        ad {
    862        1.1        ad 	int rv;
    863        1.1        ad 
    864        1.1        ad 	rv = 0;
    865        1.1        ad 
    866        1.1        ad 	if ((status & TWE_STS_EXPECTED_BITS) != TWE_STS_EXPECTED_BITS) {
    867        1.1        ad 		printf("%s: missing status bits: 0x%08x\n", sc->sc_dv.dv_xname,
    868        1.1        ad 		    status & ~TWE_STS_EXPECTED_BITS);
    869        1.1        ad 		rv = -1;
    870        1.1        ad 	}
    871        1.1        ad 
    872        1.1        ad 	if ((status & TWE_STS_UNEXPECTED_BITS) != 0) {
    873        1.1        ad 		printf("%s: unexpected status bits: 0x%08x\n",
    874        1.1        ad 		    sc->sc_dv.dv_xname, status & TWE_STS_UNEXPECTED_BITS);
    875        1.1        ad 		rv = -1;
    876        1.1        ad 	}
    877        1.1        ad 
    878        1.1        ad 	return (rv);
    879        1.1        ad }
    880        1.1        ad 
    881        1.1        ad /*
    882        1.1        ad  * Allocate and initialise a CCB.
    883        1.1        ad  */
    884        1.1        ad int
    885        1.3        ad twe_ccb_alloc(struct twe_softc *sc, struct twe_ccb **ccbp, int flags)
    886        1.1        ad {
    887        1.1        ad 	struct twe_cmd *tc;
    888        1.1        ad 	struct twe_ccb *ccb;
    889        1.1        ad 	int s;
    890        1.1        ad 
    891        1.7        ad 	s = splbio();
    892        1.3        ad 	if ((flags & TWE_CCB_PARAM) != 0)
    893        1.3        ad 		ccb = sc->sc_ccbs;
    894        1.3        ad 	else {
    895        1.3        ad 		/* Allocate a CCB and command block. */
    896        1.3        ad 		if (SLIST_FIRST(&sc->sc_ccb_freelist) == NULL) {
    897        1.1        ad 			splx(s);
    898        1.1        ad 			return (EAGAIN);
    899        1.1        ad 		}
    900        1.3        ad 		ccb = SLIST_FIRST(&sc->sc_ccb_freelist);
    901        1.3        ad 		SLIST_REMOVE_HEAD(&sc->sc_ccb_freelist, ccb_chain.slist);
    902        1.1        ad 	}
    903        1.3        ad #ifdef DIAGNOSTIC
    904        1.3        ad 	if ((ccb->ccb_flags & TWE_CCB_ALLOCED) != 0)
    905        1.3        ad 		panic("twe_ccb_alloc: CCB already allocated");
    906        1.3        ad 	flags |= TWE_CCB_ALLOCED;
    907        1.3        ad #endif
    908        1.7        ad 	splx(s);
    909        1.1        ad 
    910        1.1        ad 	/* Initialise some fields and return. */
    911        1.1        ad 	ccb->ccb_tx.tx_handler = NULL;
    912        1.3        ad 	ccb->ccb_flags = flags;
    913        1.1        ad 	tc = ccb->ccb_cmd;
    914        1.1        ad 	tc->tc_status = 0;
    915        1.1        ad 	tc->tc_flags = 0;
    916        1.1        ad 	tc->tc_cmdid = ccb->ccb_cmdid;
    917        1.3        ad 	*ccbp = ccb;
    918        1.1        ad 
    919        1.1        ad 	return (0);
    920        1.1        ad }
    921        1.1        ad 
    922        1.1        ad /*
    923        1.3        ad  * Free a CCB.
    924        1.1        ad  */
    925        1.1        ad void
    926        1.2        ad twe_ccb_free(struct twe_softc *sc, struct twe_ccb *ccb)
    927        1.1        ad {
    928        1.1        ad 	int s;
    929        1.1        ad 
    930        1.3        ad 	s = splbio();
    931        1.3        ad 	if ((ccb->ccb_flags & TWE_CCB_PARAM) == 0)
    932        1.3        ad 		SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb, ccb_chain.slist);
    933        1.1        ad 	ccb->ccb_flags = 0;
    934        1.1        ad 	splx(s);
    935        1.1        ad }
    936        1.1        ad 
    937        1.1        ad /*
    938        1.1        ad  * Map the specified CCB's command block and data buffer (if any) into
    939        1.1        ad  * controller visible space.  Perform DMA synchronisation.
    940        1.1        ad  */
    941        1.1        ad int
    942        1.1        ad twe_ccb_map(struct twe_softc *sc, struct twe_ccb *ccb)
    943        1.1        ad {
    944        1.1        ad 	struct twe_cmd *tc;
    945   1.12.2.5   nathanw 	int flags, nsegs, i, s, rv;
    946        1.1        ad 	void *data;
    947        1.1        ad 
    948        1.7        ad 	/*
    949        1.7        ad 	 * The data as a whole must be 512-byte aligned.
    950        1.7        ad 	 */
    951        1.1        ad 	if (((u_long)ccb->ccb_data & (TWE_ALIGNMENT - 1)) != 0) {
    952   1.12.2.5   nathanw 		s = splvm();
    953   1.12.2.5   nathanw 		/* XXX */
    954   1.12.2.5   nathanw 		ccb->ccb_abuf = uvm_km_kmemalloc(kmem_map, NULL,
    955   1.12.2.5   nathanw 		    ccb->ccb_datasize, UVM_KMF_NOWAIT);
    956   1.12.2.5   nathanw 		splx(s);
    957   1.12.2.5   nathanw 		data = (void *)ccb->ccb_abuf;
    958        1.2        ad 		if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
    959        1.2        ad 			memcpy(data, ccb->ccb_data, ccb->ccb_datasize);
    960        1.1        ad 	} else {
    961   1.12.2.5   nathanw 		ccb->ccb_abuf = (vaddr_t)0;
    962        1.1        ad 		data = ccb->ccb_data;
    963        1.1        ad 	}
    964        1.1        ad 
    965        1.7        ad 	/*
    966        1.7        ad 	 * Map the data buffer into bus space and build the S/G list.
    967        1.7        ad 	 */
    968        1.7        ad 	rv = bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer, data,
    969   1.12.2.3   nathanw 	    ccb->ccb_datasize, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
    970   1.12.2.3   nathanw 	    ((ccb->ccb_flags & TWE_CCB_DATA_IN) ?
    971   1.12.2.8   nathanw 	    BUS_DMA_READ : BUS_DMA_WRITE));
    972        1.7        ad 	if (rv != 0) {
    973   1.12.2.5   nathanw 		if (ccb->ccb_abuf != (vaddr_t)0) {
    974   1.12.2.5   nathanw 			s = splvm();
    975   1.12.2.5   nathanw 			/* XXX */
    976   1.12.2.5   nathanw 			uvm_km_free(kmem_map, ccb->ccb_abuf,
    977        1.7        ad 			    ccb->ccb_datasize);
    978   1.12.2.5   nathanw 			splx(s);
    979        1.7        ad 		}
    980        1.7        ad 		return (rv);
    981        1.7        ad 	}
    982        1.1        ad 
    983        1.1        ad 	nsegs = ccb->ccb_dmamap_xfer->dm_nsegs;
    984        1.1        ad 	tc = ccb->ccb_cmd;
    985        1.1        ad 	tc->tc_size += 2 * nsegs;
    986        1.1        ad 
    987        1.1        ad 	/* The location of the S/G list is dependant upon command type. */
    988        1.1        ad 	switch (tc->tc_opcode >> 5) {
    989        1.1        ad 	case 2:
    990        1.1        ad 		for (i = 0; i < nsegs; i++) {
    991        1.1        ad 			tc->tc_args.param.sgl[i].tsg_address =
    992        1.1        ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
    993        1.1        ad 			tc->tc_args.param.sgl[i].tsg_length =
    994        1.1        ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
    995        1.1        ad 		}
    996        1.1        ad 		/* XXX Needed? */
    997        1.1        ad 		for (; i < TWE_SG_SIZE; i++) {
    998        1.1        ad 			tc->tc_args.param.sgl[i].tsg_address = 0;
    999        1.1        ad 			tc->tc_args.param.sgl[i].tsg_length = 0;
   1000        1.1        ad 		}
   1001        1.1        ad 		break;
   1002        1.1        ad 	case 3:
   1003        1.1        ad 		for (i = 0; i < nsegs; i++) {
   1004        1.1        ad 			tc->tc_args.io.sgl[i].tsg_address =
   1005        1.1        ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
   1006        1.1        ad 			tc->tc_args.io.sgl[i].tsg_length =
   1007        1.1        ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
   1008        1.1        ad 		}
   1009        1.1        ad 		/* XXX Needed? */
   1010        1.1        ad 		for (; i < TWE_SG_SIZE; i++) {
   1011        1.1        ad 			tc->tc_args.io.sgl[i].tsg_address = 0;
   1012        1.1        ad 			tc->tc_args.io.sgl[i].tsg_length = 0;
   1013        1.1        ad 		}
   1014        1.1        ad 		break;
   1015        1.1        ad #ifdef DEBUG
   1016        1.1        ad 	default:
   1017        1.1        ad 		panic("twe_ccb_map: oops");
   1018        1.1        ad #endif
   1019        1.1        ad 	}
   1020        1.1        ad 
   1021        1.1        ad 	if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
   1022        1.1        ad 		flags = BUS_DMASYNC_PREREAD;
   1023        1.1        ad 	else
   1024        1.1        ad 		flags = 0;
   1025        1.1        ad 	if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
   1026        1.1        ad 		flags |= BUS_DMASYNC_PREWRITE;
   1027        1.1        ad 
   1028        1.1        ad 	bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
   1029        1.1        ad 	    ccb->ccb_datasize, flags);
   1030        1.1        ad 	return (0);
   1031        1.1        ad }
   1032        1.1        ad 
   1033        1.1        ad /*
   1034        1.1        ad  * Unmap the specified CCB's command block and data buffer (if any) and
   1035        1.1        ad  * perform DMA synchronisation.
   1036        1.1        ad  */
   1037        1.1        ad void
   1038        1.1        ad twe_ccb_unmap(struct twe_softc *sc, struct twe_ccb *ccb)
   1039        1.1        ad {
   1040   1.12.2.5   nathanw 	int flags, s;
   1041        1.1        ad 
   1042        1.1        ad 	if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
   1043        1.1        ad 		flags = BUS_DMASYNC_POSTREAD;
   1044        1.1        ad 	else
   1045        1.1        ad 		flags = 0;
   1046        1.1        ad 	if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
   1047        1.1        ad 		flags |= BUS_DMASYNC_POSTWRITE;
   1048        1.1        ad 
   1049        1.1        ad 	bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
   1050        1.1        ad 	    ccb->ccb_datasize, flags);
   1051        1.1        ad 	bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
   1052   1.12.2.4   nathanw 
   1053   1.12.2.5   nathanw 	if (ccb->ccb_abuf != (vaddr_t)0) {
   1054        1.2        ad 		if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
   1055   1.12.2.5   nathanw 			memcpy(ccb->ccb_data, (void *)ccb->ccb_abuf,
   1056        1.2        ad 			    ccb->ccb_datasize);
   1057   1.12.2.5   nathanw 		s = splvm();
   1058   1.12.2.5   nathanw 		/* XXX */
   1059   1.12.2.5   nathanw 		uvm_km_free(kmem_map, ccb->ccb_abuf, ccb->ccb_datasize);
   1060   1.12.2.5   nathanw 		splx(s);
   1061        1.1        ad 	}
   1062        1.1        ad }
   1063        1.1        ad 
   1064        1.1        ad /*
   1065        1.7        ad  * Submit a command to the controller and poll on completion.  Return
   1066        1.7        ad  * non-zero on timeout (but don't check status, as some command types don't
   1067        1.7        ad  * return status).  Must be called with interrupts blocked.
   1068        1.1        ad  */
   1069        1.1        ad int
   1070        1.1        ad twe_ccb_poll(struct twe_softc *sc, struct twe_ccb *ccb, int timo)
   1071        1.1        ad {
   1072        1.7        ad 	int rv;
   1073        1.7        ad 
   1074        1.7        ad 	if ((rv = twe_ccb_submit(sc, ccb)) != 0)
   1075        1.7        ad 		return (rv);
   1076        1.1        ad 
   1077   1.12.2.2   nathanw 	for (timo *= 1000; timo != 0; timo--) {
   1078        1.1        ad 		twe_poll(sc);
   1079        1.1        ad 		if ((ccb->ccb_flags & TWE_CCB_COMPLETE) != 0)
   1080        1.1        ad 			break;
   1081   1.12.2.2   nathanw 		DELAY(100);
   1082        1.1        ad 	}
   1083        1.1        ad 
   1084        1.1        ad 	return (timo == 0);
   1085        1.1        ad }
   1086        1.1        ad 
   1087        1.1        ad /*
   1088        1.1        ad  * If a CCB is specified, enqueue it.  Pull CCBs off the software queue in
   1089        1.1        ad  * the order that they were enqueued and try to submit their command blocks
   1090        1.1        ad  * to the controller for execution.
   1091        1.1        ad  */
   1092        1.1        ad void
   1093        1.1        ad twe_ccb_enqueue(struct twe_softc *sc, struct twe_ccb *ccb)
   1094        1.1        ad {
   1095        1.1        ad 	int s;
   1096        1.1        ad 
   1097        1.1        ad 	s = splbio();
   1098        1.1        ad 
   1099        1.1        ad 	if (ccb != NULL)
   1100        1.1        ad 		SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain.simpleq);
   1101        1.1        ad 
   1102        1.1        ad 	while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
   1103        1.1        ad 		if (twe_ccb_submit(sc, ccb))
   1104        1.1        ad 			break;
   1105   1.12.2.8   nathanw 		SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb_chain.simpleq);
   1106        1.1        ad 	}
   1107        1.1        ad 
   1108        1.1        ad 	splx(s);
   1109        1.1        ad }
   1110        1.1        ad 
   1111        1.1        ad /*
   1112        1.1        ad  * Submit the command block associated with the specified CCB to the
   1113        1.1        ad  * controller for execution.  Must be called with interrupts blocked.
   1114        1.1        ad  */
   1115        1.1        ad int
   1116        1.1        ad twe_ccb_submit(struct twe_softc *sc, struct twe_ccb *ccb)
   1117        1.1        ad {
   1118        1.1        ad 	bus_addr_t pa;
   1119        1.1        ad 	int rv;
   1120        1.1        ad 	u_int status;
   1121        1.1        ad 
   1122        1.1        ad 	/* Check to see if we can post a command. */
   1123   1.12.2.8   nathanw 	status = twe_inl(sc, TWE_REG_STS);
   1124        1.1        ad 	twe_status_check(sc, status);
   1125        1.1        ad 
   1126        1.1        ad 	if ((status & TWE_STS_CMD_QUEUE_FULL) == 0) {
   1127        1.1        ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1128        1.1        ad 		    (caddr_t)ccb->ccb_cmd - sc->sc_cmds, sizeof(struct twe_cmd),
   1129        1.1        ad 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1130        1.1        ad 		ccb->ccb_flags |= TWE_CCB_ACTIVE;
   1131        1.1        ad 		pa = sc->sc_cmds_paddr +
   1132        1.1        ad 		    ccb->ccb_cmdid * sizeof(struct twe_cmd);
   1133   1.12.2.8   nathanw 		twe_outl(sc, TWE_REG_CMD_QUEUE, (u_int32_t)pa);
   1134        1.1        ad 		rv = 0;
   1135        1.1        ad 	} else
   1136        1.1        ad 		rv = EBUSY;
   1137        1.1        ad 
   1138        1.1        ad 	return (rv);
   1139  1.12.2.13   thorpej }
   1140  1.12.2.13   thorpej 
   1141  1.12.2.13   thorpej 
   1142  1.12.2.13   thorpej /*
   1143  1.12.2.13   thorpej  * Accept an open operation on the control device.
   1144  1.12.2.13   thorpej  */
   1145  1.12.2.13   thorpej int
   1146  1.12.2.13   thorpej tweopen(dev_t dev, int flag, int mode, struct proc *p)
   1147  1.12.2.13   thorpej {
   1148  1.12.2.13   thorpej 	struct twe_softc *twe;
   1149  1.12.2.13   thorpej 
   1150  1.12.2.13   thorpej 	if ((twe = device_lookup(&twe_cd, minor(dev))) == NULL)
   1151  1.12.2.13   thorpej 		return (ENXIO);
   1152  1.12.2.13   thorpej 	if ((twe->sc_flags & TWEF_OPEN) != 0)
   1153  1.12.2.13   thorpej 		return (EBUSY);
   1154  1.12.2.13   thorpej 
   1155  1.12.2.13   thorpej 	twe->sc_flags |= TWEF_OPEN;
   1156  1.12.2.13   thorpej 	return (0);
   1157  1.12.2.13   thorpej }
   1158  1.12.2.13   thorpej 
   1159  1.12.2.13   thorpej /*
   1160  1.12.2.13   thorpej  * Accept the last close on the control device.
   1161  1.12.2.13   thorpej  */
   1162  1.12.2.13   thorpej int
   1163  1.12.2.13   thorpej tweclose(dev_t dev, int flag, int mode, struct proc *p)
   1164  1.12.2.13   thorpej {
   1165  1.12.2.13   thorpej 	struct twe_softc *twe;
   1166  1.12.2.13   thorpej 
   1167  1.12.2.13   thorpej 	twe = device_lookup(&twe_cd, minor(dev));
   1168  1.12.2.13   thorpej 	twe->sc_flags &= ~TWEF_OPEN;
   1169  1.12.2.13   thorpej 	return (0);
   1170  1.12.2.13   thorpej }
   1171  1.12.2.13   thorpej 
   1172  1.12.2.13   thorpej /*
   1173  1.12.2.13   thorpej  * Handle control operations.
   1174  1.12.2.13   thorpej  */
   1175  1.12.2.13   thorpej int
   1176  1.12.2.13   thorpej tweioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
   1177  1.12.2.13   thorpej {
   1178  1.12.2.13   thorpej 	struct twe_softc *twe;
   1179  1.12.2.13   thorpej 	struct twe_ccb *ccb;
   1180  1.12.2.13   thorpej 	struct twe_param *param;
   1181  1.12.2.13   thorpej 	struct twe_usercommand *tu;
   1182  1.12.2.13   thorpej 	struct twe_paramcommand *tp;
   1183  1.12.2.13   thorpej 	union twe_statrequest *ts;
   1184  1.12.2.13   thorpej 	void *pdata = NULL;
   1185  1.12.2.13   thorpej 	int rv, s, error = 0;
   1186  1.12.2.13   thorpej 	u_int8_t cmdid;
   1187  1.12.2.13   thorpej 
   1188  1.12.2.13   thorpej 	if (securelevel >= 2)
   1189  1.12.2.13   thorpej 		return (EPERM);
   1190  1.12.2.13   thorpej 
   1191  1.12.2.13   thorpej 	twe = device_lookup(&twe_cd, minor(dev));
   1192  1.12.2.13   thorpej 	tu = (struct twe_usercommand *)data;
   1193  1.12.2.13   thorpej 	tp = (struct twe_paramcommand *)data;
   1194  1.12.2.13   thorpej 	ts = (union twe_statrequest *)data;
   1195  1.12.2.13   thorpej 
   1196  1.12.2.13   thorpej 	/* Hmm, compatible with FreeBSD */
   1197  1.12.2.13   thorpej 	switch (cmd) {
   1198  1.12.2.13   thorpej 	case TWEIO_COMMAND:
   1199  1.12.2.13   thorpej 		if (tu->tu_size > 0) {
   1200  1.12.2.13   thorpej 			if (tu->tu_size > TWE_SECTOR_SIZE)
   1201  1.12.2.13   thorpej 				return EINVAL;
   1202  1.12.2.13   thorpej 			pdata = malloc(tu->tu_size, M_DEVBUF, M_WAITOK);
   1203  1.12.2.13   thorpej 			error = copyin(tu->tu_data, pdata, tu->tu_size);
   1204  1.12.2.13   thorpej 			if (error != 0)
   1205  1.12.2.13   thorpej 				goto done;
   1206  1.12.2.13   thorpej 			error = twe_ccb_alloc(twe, &ccb, TWE_CCB_PARAM |
   1207  1.12.2.13   thorpej 			    TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
   1208  1.12.2.13   thorpej 		} else {
   1209  1.12.2.13   thorpej 			error = twe_ccb_alloc(twe, &ccb, 0);
   1210  1.12.2.13   thorpej 		}
   1211  1.12.2.13   thorpej 		if (rv != 0)
   1212  1.12.2.13   thorpej 			goto done;
   1213  1.12.2.13   thorpej 		cmdid = ccb->ccb_cmdid;
   1214  1.12.2.13   thorpej 		memcpy(ccb->ccb_cmd, &tu->tu_cmd, sizeof(struct twe_cmd));
   1215  1.12.2.13   thorpej 		ccb->ccb_cmdid = cmdid;
   1216  1.12.2.13   thorpej 		if (ccb->ccb_flags & TWE_CCB_PARAM) {
   1217  1.12.2.13   thorpej 			ccb->ccb_data = pdata;
   1218  1.12.2.13   thorpej 			ccb->ccb_datasize = TWE_SECTOR_SIZE;
   1219  1.12.2.13   thorpej 			ccb->ccb_tx.tx_handler = 0;
   1220  1.12.2.13   thorpej 			ccb->ccb_tx.tx_context = pdata;
   1221  1.12.2.13   thorpej 			ccb->ccb_tx.tx_dv = &twe->sc_dv;
   1222  1.12.2.13   thorpej 		}
   1223  1.12.2.13   thorpej 		/* Map the transfer. */
   1224  1.12.2.13   thorpej 		if ((error = twe_ccb_map(twe, ccb)) != 0) {
   1225  1.12.2.13   thorpej 			twe_ccb_free(twe, ccb);
   1226  1.12.2.13   thorpej 			goto done;
   1227  1.12.2.13   thorpej 		}
   1228  1.12.2.13   thorpej 
   1229  1.12.2.13   thorpej 		/* Submit the command and wait. */
   1230  1.12.2.13   thorpej 		s = splbio();
   1231  1.12.2.13   thorpej 		rv = twe_ccb_poll(twe, ccb, 5);
   1232  1.12.2.13   thorpej 		twe_ccb_unmap(twe, ccb);
   1233  1.12.2.13   thorpej 		twe_ccb_free(twe, ccb);
   1234  1.12.2.13   thorpej 		splx(s);
   1235  1.12.2.13   thorpej 
   1236  1.12.2.13   thorpej 		if (tu->tu_size > 0)
   1237  1.12.2.13   thorpej 			error = copyout(pdata, tu->tu_data, tu->tu_size);
   1238  1.12.2.13   thorpej 		goto done;
   1239  1.12.2.13   thorpej 
   1240  1.12.2.13   thorpej 	case TWEIO_STATS:
   1241  1.12.2.13   thorpej 		return (ENOENT);
   1242  1.12.2.13   thorpej 
   1243  1.12.2.13   thorpej 	case TWEIO_AEN_POLL:
   1244  1.12.2.13   thorpej 		if ((twe->sc_flags & TWEF_AEN) == 0)
   1245  1.12.2.13   thorpej 			return (ENOENT);
   1246  1.12.2.13   thorpej 		return (0);
   1247  1.12.2.13   thorpej 
   1248  1.12.2.13   thorpej 	case TWEIO_AEN_WAIT:
   1249  1.12.2.13   thorpej 		s = splbio();
   1250  1.12.2.13   thorpej 		while ((twe->sc_flags & TWEF_AEN) == 0) {
   1251  1.12.2.13   thorpej 			/* tsleep(); */
   1252  1.12.2.13   thorpej 		}
   1253  1.12.2.13   thorpej 		splx(s);
   1254  1.12.2.13   thorpej 		return (0);
   1255  1.12.2.13   thorpej 
   1256  1.12.2.13   thorpej 	case TWEIO_GET_PARAM:
   1257  1.12.2.13   thorpej 		error = twe_param_get(twe, tp->tp_table_id, tp->tp_param_id,
   1258  1.12.2.13   thorpej 		    tp->tp_size, 0, &pdata);
   1259  1.12.2.13   thorpej 		if (error != 0)
   1260  1.12.2.13   thorpej 			return (error);
   1261  1.12.2.13   thorpej 		param = pdata;
   1262  1.12.2.13   thorpej 		if (param->tp_param_size > tp->tp_size) {
   1263  1.12.2.13   thorpej 			error = EFAULT;
   1264  1.12.2.13   thorpej 			goto done;
   1265  1.12.2.13   thorpej 		}
   1266  1.12.2.13   thorpej 		error = copyout(param->tp_data, tp->tp_data,
   1267  1.12.2.13   thorpej 		    param->tp_param_size);
   1268  1.12.2.13   thorpej 		goto done;
   1269  1.12.2.13   thorpej 
   1270  1.12.2.13   thorpej 	case TWEIO_SET_PARAM:
   1271  1.12.2.13   thorpej 		pdata = malloc(tp->tp_size, M_DEVBUF, M_WAITOK);
   1272  1.12.2.13   thorpej 		if ((error = copyin(tp->tp_data, pdata, tp->tp_size)) != 0)
   1273  1.12.2.13   thorpej 			goto done;
   1274  1.12.2.13   thorpej 		error = twe_param_set(twe, tp->tp_table_id, tp->tp_param_id,
   1275  1.12.2.13   thorpej 		    tp->tp_size, pdata);
   1276  1.12.2.13   thorpej 		goto done;
   1277  1.12.2.13   thorpej 
   1278  1.12.2.13   thorpej 	case TWEIO_RESET:
   1279  1.12.2.13   thorpej 		twe_reset(twe);
   1280  1.12.2.13   thorpej 		return (0);
   1281  1.12.2.13   thorpej 
   1282  1.12.2.13   thorpej 	default:
   1283  1.12.2.13   thorpej 		return EINVAL;
   1284  1.12.2.13   thorpej 	}
   1285  1.12.2.13   thorpej done:
   1286  1.12.2.13   thorpej 	if (pdata)
   1287  1.12.2.13   thorpej 		free(pdata, M_DEVBUF);
   1288  1.12.2.13   thorpej 	return error;
   1289        1.1        ad }
   1290