Home | History | Annotate | Line # | Download | only in pci
twe.c revision 1.7
      1 /*	$NetBSD: twe.c,v 1.7 2001/01/22 17:32:11 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 2000 Michael Smith
     41  * Copyright (c) 2000 BSDi
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  * from FreeBSD: twe.c,v 1.1 2000/05/24 23:35:23 msmith Exp
     66  */
     67 
     68 /*
     69  * Driver for the 3ware Escalade family of RAID controllers.
     70  */
     71 
     72 #include "opt_twe.h"
     73 
     74 #include <sys/param.h>
     75 #include <sys/systm.h>
     76 #include <sys/kernel.h>
     77 #include <sys/device.h>
     78 #include <sys/queue.h>
     79 #include <sys/proc.h>
     80 #include <sys/buf.h>
     81 #include <sys/endian.h>
     82 #include <sys/malloc.h>
     83 #include <sys/disk.h>
     84 
     85 #include <uvm/uvm_extern.h>
     86 
     87 #include <machine/bswap.h>
     88 #include <machine/bus.h>
     89 
     90 #include <dev/pci/pcireg.h>
     91 #include <dev/pci/pcivar.h>
     92 #include <dev/pci/pcidevs.h>
     93 #include <dev/pci/twereg.h>
     94 #include <dev/pci/twevar.h>
     95 
     96 #define	TWE_INL(sc, port) \
     97     bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, port)
     98 #define	TWE_OUTL(sc, port, val) \
     99     bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, port, val)
    100 
    101 #define	PCI_CBIO	0x10
    102 
    103 static void	twe_aen_handler(struct twe_ccb *, int);
    104 static void	twe_attach(struct device *, struct device *, void *);
    105 static int	twe_init_connection(struct twe_softc *);
    106 static int	twe_intr(void *);
    107 static int	twe_match(struct device *, struct cfdata *, void *);
    108 static int	twe_param_get(struct twe_softc *, int, int, size_t,
    109 			      void (*)(struct twe_ccb *, int), void **);
    110 static void	twe_poll(struct twe_softc *);
    111 static int	twe_print(void *, const char *);
    112 static int	twe_reset(struct twe_softc *);
    113 static int	twe_submatch(struct device *, struct cfdata *, void *);
    114 static int	twe_status_check(struct twe_softc *, u_int);
    115 static int	twe_status_wait(struct twe_softc *, u_int, int);
    116 
    117 struct cfattach twe_ca = {
    118 	sizeof(struct twe_softc), twe_match, twe_attach
    119 };
    120 
    121 struct {
    122 	const u_int	aen;		/* High byte non-zero if w/unit */
    123 	const char	*desc;
    124 } static const twe_aen_names[] = {
    125 	{ 0x0000, "queue empty" },
    126 	{ 0x0001, "soft reset" },
    127 	{ 0x0102, "degraded mirror" },
    128 	{ 0x0003, "controller error" },
    129 	{ 0x0104, "rebuild fail" },
    130 	{ 0x0105, "rebuild done" },
    131 	{ 0x0106, "incompatible unit" },
    132 	{ 0x0107, "init done" },
    133 	{ 0x0108, "unclean shutdown" },
    134 	{ 0x0109, "aport timeout" },
    135 	{ 0x010a, "drive error" },
    136 	{ 0x010b, "rebuild started" },
    137 	{ 0x0015, "table undefined" },
    138 	{ 0x00ff, "aen queue full" },
    139 };
    140 
    141 /*
    142  * Match a supported board.
    143  */
    144 static int
    145 twe_match(struct device *parent, struct cfdata *cfdata, void *aux)
    146 {
    147 	struct pci_attach_args *pa;
    148 
    149 	pa = aux;
    150 
    151 	return (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3WARE &&
    152 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_ESCALADE);
    153 }
    154 
    155 /*
    156  * Attach a supported board.
    157  *
    158  * XXX This doesn't fail gracefully.
    159  */
    160 static void
    161 twe_attach(struct device *parent, struct device *self, void *aux)
    162 {
    163 	struct pci_attach_args *pa;
    164 	struct twe_softc *sc;
    165 	pci_chipset_tag_t pc;
    166 	pci_intr_handle_t ih;
    167 	pcireg_t csr;
    168 	const char *intrstr;
    169 	int size, i, rv, rseg;
    170 	struct twe_param *dtp, *ctp;
    171 	bus_dma_segment_t seg;
    172 	struct twe_cmd *tc;
    173 	struct twe_attach_args twea;
    174 	struct twe_ccb *ccb;
    175 
    176 	sc = (struct twe_softc *)self;
    177 	pa = aux;
    178 	pc = pa->pa_pc;
    179 	sc->sc_dmat = pa->pa_dmat;
    180 	SIMPLEQ_INIT(&sc->sc_ccb_queue);
    181 	SLIST_INIT(&sc->sc_ccb_freelist);
    182 
    183 	printf(": 3ware Escalade\n");
    184 
    185 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    186 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    187 		printf("%s: can't map i/o space\n", sc->sc_dv.dv_xname);
    188 		return;
    189 	}
    190 
    191 	/* Enable the device. */
    192 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    193 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    194 	    csr | PCI_COMMAND_MASTER_ENABLE);
    195 
    196 	/* Map and establish the interrupt. */
    197 	if (pci_intr_map(pa, &ih)) {
    198 		printf("%s: can't map interrupt\n", sc->sc_dv.dv_xname);
    199 		return;
    200 	}
    201 	intrstr = pci_intr_string(pc, ih);
    202 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, twe_intr, sc);
    203 	if (sc->sc_ih == NULL) {
    204 		printf("%s: can't establish interrupt", sc->sc_dv.dv_xname);
    205 		if (intrstr != NULL)
    206 			printf(" at %s", intrstr);
    207 		printf("\n");
    208 		return;
    209 	}
    210 	if (intrstr != NULL)
    211 		printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname, intrstr);
    212 
    213 	/*
    214 	 * Allocate and initialise the command blocks and CCBs.
    215 	 */
    216         size = sizeof(struct twe_cmd) * TWE_MAX_QUEUECNT;
    217 
    218 	if ((rv = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
    219 	    &rseg, BUS_DMA_NOWAIT)) != 0) {
    220 		printf("%s: unable to allocate commands, rv = %d\n",
    221 		    sc->sc_dv.dv_xname, rv);
    222 		return;
    223 	}
    224 
    225 	if ((rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
    226 	    (caddr_t *)&sc->sc_cmds,
    227 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    228 		printf("%s: unable to map commands, rv = %d\n",
    229 		    sc->sc_dv.dv_xname, rv);
    230 		return;
    231 	}
    232 
    233 	if ((rv = bus_dmamap_create(sc->sc_dmat, size, size, 1, 0,
    234 	    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    235 		printf("%s: unable to create command DMA map, rv = %d\n",
    236 		    sc->sc_dv.dv_xname, rv);
    237 		return;
    238 	}
    239 
    240 	if ((rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_cmds,
    241 	    size, NULL, BUS_DMA_NOWAIT)) != 0) {
    242 		printf("%s: unable to load command DMA map, rv = %d\n",
    243 		    sc->sc_dv.dv_xname, rv);
    244 		return;
    245 	}
    246 
    247 	sc->sc_cmds_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
    248 	memset(sc->sc_cmds, 0, size);
    249 
    250 	ccb = malloc(sizeof(*ccb) * TWE_MAX_QUEUECNT, M_DEVBUF, M_NOWAIT);
    251 	sc->sc_ccbs = ccb;
    252 	tc = (struct twe_cmd *)sc->sc_cmds;
    253 
    254 	for (i = 0; i < TWE_MAX_QUEUECNT; i++, tc++, ccb++) {
    255 		ccb->ccb_cmd = tc;
    256 		ccb->ccb_cmdid = i;
    257 		ccb->ccb_flags = 0;
    258 		rv = bus_dmamap_create(sc->sc_dmat, TWE_MAX_XFER,
    259 		    TWE_MAX_SEGS, PAGE_SIZE, 0,
    260 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    261 		    &ccb->ccb_dmamap_xfer);
    262 		if (rv != 0) {
    263 			printf("%s: can't create dmamap, rv = %d\n",
    264 			    sc->sc_dv.dv_xname, rv);
    265 			return;
    266 		}
    267 		/* Save one CCB for parameter retrieval. */
    268 		if (i != 0)
    269 			SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb,
    270 			    ccb_chain.slist);
    271 	}
    272 
    273 	/* Wait for the controller to become ready. */
    274 	if (twe_status_wait(sc, TWE_STS_MICROCONTROLLER_READY, 6)) {
    275 		printf("%s: microcontroller not ready\n", sc->sc_dv.dv_xname);
    276 		return;
    277 	}
    278 
    279 	TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_DISABLE_INTRS);
    280 
    281 	/* Reset the controller. */
    282 	if (twe_reset(sc)) {
    283 		printf("%s: reset failed\n", sc->sc_dv.dv_xname);
    284 		return;
    285 	}
    286 
    287 	/* Find attached units. */
    288 	rv = twe_param_get(sc, TWE_PARAM_UNITSUMMARY,
    289 	    TWE_PARAM_UNITSUMMARY_Status, TWE_MAX_UNITS, NULL, (void **)&dtp);
    290 	if (rv != 0) {
    291 		printf("%s: can't detect attached units (%d)\n",
    292 		    sc->sc_dv.dv_xname, rv);
    293 		return;
    294 	}
    295 
    296 	/* For each detected unit, collect size and store in an array. */
    297 	for (i = 0, sc->sc_nunits = 0; i < TWE_MAX_UNITS; i++) {
    298 		/* Unit present? */
    299 		if ((dtp->tp_data[i] & TWE_PARAM_UNITSTATUS_Online) == 0) {
    300 			sc->sc_dsize[i] = 0;
    301 	   		continue;
    302 	   	}
    303 
    304 		rv = twe_param_get(sc, TWE_PARAM_UNITINFO + i,
    305 		    TWE_PARAM_UNITINFO_Capacity, 4, NULL, (void **)&ctp);
    306 		if (rv != 0) {
    307 			printf("%s: error %d fetching capacity for unit %d\n",
    308 			    sc->sc_dv.dv_xname, rv, i);
    309 			continue;
    310 		}
    311 
    312 		sc->sc_dsize[i] = le32toh(*(u_int32_t *)ctp->tp_data);
    313 		free(ctp, M_DEVBUF);
    314 		sc->sc_nunits++;
    315 	}
    316 	free(dtp, M_DEVBUF);
    317 
    318 	/* Initialise connection with controller and enable interrupts. */
    319 	twe_init_connection(sc);
    320 	TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR |
    321 	    TWE_CTL_UNMASK_RESP_INTR |
    322 	    TWE_CTL_ENABLE_INTRS);
    323 
    324 	/* Attach sub-devices. */
    325 	for (i = 0; i < TWE_MAX_UNITS; i++) {
    326 		if (sc->sc_dsize[i] == 0)
    327 			continue;
    328 		twea.twea_unit = i;
    329 		config_found_sm(&sc->sc_dv, &twea, twe_print, twe_submatch);
    330 	}
    331 }
    332 
    333 /*
    334  * Reset the controller.  Currently only useful at attach time; must be
    335  * called with interrupts blocked.
    336  */
    337 static int
    338 twe_reset(struct twe_softc *sc)
    339 {
    340 	struct twe_param *tp;
    341 	u_int aen, status;
    342 	volatile u_int32_t junk;
    343 	int got, rv;
    344 
    345 	/* Issue a soft reset. */
    346 	TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_ISSUE_SOFT_RESET |
    347 	    TWE_CTL_CLEAR_HOST_INTR |
    348 	    TWE_CTL_CLEAR_ATTN_INTR |
    349 	    TWE_CTL_MASK_CMD_INTR |
    350 	    TWE_CTL_MASK_RESP_INTR |
    351 	    TWE_CTL_CLEAR_ERROR_STS |
    352 	    TWE_CTL_DISABLE_INTRS);
    353 
    354 	if (twe_status_wait(sc, TWE_STS_ATTN_INTR, 15)) {
    355 		printf("%s: no attention interrupt\n",
    356 		    sc->sc_dv.dv_xname);
    357 		return (-1);
    358 	}
    359 
    360 	/* Pull AENs out of the controller; look for a soft reset AEN. */
    361 	for (got = 0;;) {
    362 		rv = twe_param_get(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode,
    363 		    2, NULL, (void **)&tp);
    364 		if (rv != 0)
    365 			printf("%s: error %d while draining response queue\n",
    366 			    sc->sc_dv.dv_xname, rv);
    367 		aen = TWE_AEN_CODE(le16toh(*(u_int16_t *)tp->tp_data));
    368 		free(tp, M_DEVBUF);
    369 		if (aen == TWE_AEN_QUEUE_EMPTY)
    370 			break;
    371 		if (aen == TWE_AEN_SOFT_RESET)
    372 			got = 1;
    373 	}
    374 	if (!got) {
    375 		printf("%s: reset not reported\n", sc->sc_dv.dv_xname);
    376 		return (-1);
    377 	}
    378 
    379 	/* Check controller status. */
    380 	status = TWE_INL(sc, TWE_REG_STS);
    381 	if (twe_status_check(sc, status)) {
    382 		printf("%s: controller errors detected\n",
    383 		    sc->sc_dv.dv_xname);
    384 		return (-1);
    385 	}
    386 
    387 	/* Drain the response queue. */
    388 	for (;;) {
    389 		status = TWE_INL(sc, TWE_REG_STS);
    390 		if (twe_status_check(sc, status) != 0) {
    391 			printf("%s: can't drain response queue\n",
    392 			    sc->sc_dv.dv_xname);
    393 			return (-1);
    394 		}
    395 		if ((status & TWE_STS_RESP_QUEUE_EMPTY) != 0)
    396 			break;
    397 		junk = TWE_INL(sc, TWE_REG_RESP_QUEUE);
    398 	}
    399 
    400 	return (0);
    401 }
    402 
    403 /*
    404  * Print autoconfiguration message for a sub-device.
    405  */
    406 static int
    407 twe_print(void *aux, const char *pnp)
    408 {
    409 	struct twe_attach_args *twea;
    410 
    411 	twea = aux;
    412 
    413 	if (pnp != NULL)
    414 		printf("block device at %s", pnp);
    415 	printf(" unit %d", twea->twea_unit);
    416 	return (UNCONF);
    417 }
    418 
    419 /*
    420  * Match a sub-device.
    421  */
    422 static int
    423 twe_submatch(struct device *parent, struct cfdata *cf, void *aux)
    424 {
    425 	struct twe_attach_args *twea;
    426 
    427 	twea = aux;
    428 
    429 	if (cf->tweacf_unit != TWECF_UNIT_DEFAULT &&
    430 	    cf->tweacf_unit != twea->twea_unit)
    431 		return (0);
    432 
    433 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    434 }
    435 
    436 /*
    437  * Interrupt service routine.
    438  */
    439 static int
    440 twe_intr(void *arg)
    441 {
    442 	struct twe_softc *sc;
    443 	u_int status;
    444 	int caught, rv;
    445 
    446 	sc = arg;
    447 	caught = 0;
    448 	status = TWE_INL(sc, TWE_REG_STS);
    449 	twe_status_check(sc, status);
    450 
    451 	/* Host interrupts - purpose unknown. */
    452 	if ((status & TWE_STS_HOST_INTR) != 0) {
    453 #ifdef DIAGNOSTIC
    454 		printf("%s: host interrupt\n", sc->sc_dv.dv_xname);
    455 #endif
    456 		TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_CLEAR_HOST_INTR);
    457 		caught = 1;
    458 	}
    459 
    460 	/*
    461 	 * Attention interrupts, signalled when a controller or child device
    462 	 * state change has occured.
    463 	 */
    464 	if ((status & TWE_STS_ATTN_INTR) != 0) {
    465 		rv = twe_param_get(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode,
    466 		    2, twe_aen_handler, NULL);
    467 		if (rv != 0)
    468 			printf("%s: unable to retrieve AEN (%d)\n",
    469 			    sc->sc_dv.dv_xname, rv);
    470 		caught = 1;
    471 	}
    472 
    473 	/*
    474 	 * Command interrupts, signalled when the controller can accept more
    475 	 * commands.  We don't use this; instead, we try to submit commands
    476 	 * when we receive them, and when other commands have completed.
    477 	 * Mask it so we don't get another one.
    478 	 */
    479 	if ((status & TWE_STS_CMD_INTR) != 0) {
    480 #ifdef DIAGNOSTIC
    481 		printf("%s: command interrupt\n", sc->sc_dv.dv_xname);
    482 #endif
    483 		TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_MASK_CMD_INTR);
    484 		caught = 1;
    485 	}
    486 
    487 	if ((status & TWE_STS_RESP_INTR) != 0) {
    488 		twe_poll(sc);
    489 		caught = 1;
    490 	}
    491 
    492 	return (caught);
    493 }
    494 
    495 /*
    496  * Handle an AEN returned by the controller.
    497  */
    498 static void
    499 twe_aen_handler(struct twe_ccb *ccb, int error)
    500 {
    501 	struct twe_softc *sc;
    502 	struct twe_param *tp;
    503 	const char *str;
    504 	u_int aen;
    505 	int i, hu, rv;
    506 
    507 	sc = (struct twe_softc *)ccb->ccb_tx.tx_dv;
    508 	tp = ccb->ccb_tx.tx_context;
    509 	twe_ccb_unmap(sc, ccb);
    510 
    511 	if (error) {
    512 		printf("%s: error retrieving AEN\n", sc->sc_dv.dv_xname);
    513 		aen = TWE_AEN_QUEUE_EMPTY;
    514 	} else
    515 		aen = le16toh(*(u_int16_t *)tp->tp_data);
    516 	free(tp, M_DEVBUF);
    517 	twe_ccb_free(sc, ccb);
    518 
    519 	if (TWE_AEN_CODE(aen) == TWE_AEN_QUEUE_EMPTY) {
    520 		TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR);
    521 		return;
    522 	}
    523 
    524 	str = "<unknown>";
    525 	i = 0;
    526 	hu = 0;
    527 
    528 	while (i < sizeof(twe_aen_names) / sizeof(twe_aen_names[0])) {
    529 		if (TWE_AEN_CODE(twe_aen_names[i].aen) == TWE_AEN_CODE(aen)) {
    530 			str = twe_aen_names[i].desc;
    531 			hu = (TWE_AEN_UNIT(twe_aen_names[i].aen) != 0);
    532 			break;
    533 		}
    534 		i++;
    535 	}
    536 	printf("%s: AEN 0x%04x (%s) received", sc->sc_dv.dv_xname,
    537 	    TWE_AEN_CODE(aen), str);
    538 	if (hu != 0)
    539 		printf(" for unit %d", TWE_AEN_UNIT(aen));
    540 	printf("\n");
    541 
    542 	/*
    543 	 * Chain another retrieval in case interrupts have been
    544 	 * coalesced.
    545 	 */
    546 	rv = twe_param_get(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2,
    547 	    twe_aen_handler, NULL);
    548 	if (rv != 0)
    549 		printf("%s: unable to retrieve AEN (%d)\n",
    550 		    sc->sc_dv.dv_xname, rv);
    551 }
    552 
    553 /*
    554  * Execute a TWE_OP_GET_PARAM command.  If a callback function is provided,
    555  * it will be called with generated context when the command has completed.
    556  * If no callback is provided, the command will be executed synchronously
    557  * and a pointer to a buffer containing the data returned.
    558  *
    559  * The caller or callback is responsible for freeing the buffer.
    560  */
    561 static int
    562 twe_param_get(struct twe_softc *sc, int table_id, int param_id, size_t size,
    563 	      void (*func)(struct twe_ccb *, int), void **pbuf)
    564 {
    565 	struct twe_ccb *ccb;
    566 	struct twe_cmd *tc;
    567 	struct twe_param *tp;
    568 	int rv, s;
    569 
    570 	rv = twe_ccb_alloc(sc, &ccb,
    571 	    TWE_CCB_PARAM | TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
    572 	if (rv != 0)
    573 		return (rv);
    574 
    575 	tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
    576 	if (pbuf != NULL)
    577 		*pbuf = tp;
    578 
    579 	ccb->ccb_data = tp;
    580 	ccb->ccb_datasize = TWE_SECTOR_SIZE;
    581 	ccb->ccb_tx.tx_handler = func;
    582 	ccb->ccb_tx.tx_context = tp;
    583 	ccb->ccb_tx.tx_dv = &sc->sc_dv;
    584 
    585 	tc = ccb->ccb_cmd;
    586 	tc->tc_size = 2;
    587 	tc->tc_opcode = TWE_OP_GET_PARAM | (tc->tc_size << 5);
    588 	tc->tc_unit = 0;
    589 	tc->tc_count = htole16(1);
    590 
    591 	/* Fill in the outbound parameter data. */
    592 	tp->tp_table_id = htole16(table_id);
    593 	tp->tp_param_id = param_id;
    594 	tp->tp_param_size = size;
    595 
    596 	/* Map the transfer. */
    597 	if ((rv = twe_ccb_map(sc, ccb)) != 0) {
    598 		twe_ccb_free(sc, ccb);
    599 		free(tp, M_DEVBUF);
    600 		return (rv);
    601 	}
    602 
    603 	/* Submit the command and either wait or let the callback handle it. */
    604 	if (func == NULL) {
    605 		s = splbio();
    606 		rv = twe_ccb_poll(sc, ccb, 5);
    607 		twe_ccb_unmap(sc, ccb);
    608 		twe_ccb_free(sc, ccb);
    609 		splx(s);
    610 		if (rv != 0)
    611 			free(tp, M_DEVBUF);
    612 	} else {
    613 		twe_ccb_enqueue(sc, ccb);
    614 		rv = 0;
    615 	}
    616 
    617 	return (rv);
    618 }
    619 
    620 /*
    621  * Execute a TWE_OP_INIT_CONNECTION command.  Return non-zero on error.
    622  * Must be called with interrupts blocked.
    623  */
    624 static int
    625 twe_init_connection(struct twe_softc *sc)
    626 {
    627 	struct twe_ccb *ccb;
    628 	struct twe_cmd *tc;
    629 	int rv;
    630 
    631 	if ((rv = twe_ccb_alloc(sc, &ccb, 0)) != 0)
    632 		return (rv);
    633 
    634 	/* Build the command. */
    635 	tc = ccb->ccb_cmd;
    636 	tc->tc_size = 3;
    637 	tc->tc_opcode = TWE_OP_INIT_CONNECTION;
    638 	tc->tc_unit = 0;
    639 	tc->tc_count = htole16(TWE_MAX_CMDS);
    640 	tc->tc_args.init_connection.response_queue_pointer = 0;
    641 
    642 	/* Submit the command for immediate execution. */
    643 	rv = twe_ccb_poll(sc, ccb, 5);
    644 	twe_ccb_free(sc, ccb);
    645 	return (rv);
    646 }
    647 
    648 /*
    649  * Poll the controller for completed commands.  Must be called with
    650  * interrupts blocked.
    651  */
    652 static void
    653 twe_poll(struct twe_softc *sc)
    654 {
    655 	struct twe_ccb *ccb;
    656 	int found;
    657 	u_int status, cmdid;
    658 
    659 	found = 0;
    660 
    661 	for (;;) {
    662 		status = TWE_INL(sc, TWE_REG_STS);
    663 		twe_status_check(sc, status);
    664 
    665 		if ((status & TWE_STS_RESP_QUEUE_EMPTY))
    666 			break;
    667 
    668 		found = 1;
    669 		cmdid = TWE_INL(sc, TWE_REG_RESP_QUEUE);
    670 		cmdid = (cmdid & TWE_RESP_MASK) >> TWE_RESP_SHIFT;
    671 		if (cmdid >= TWE_MAX_QUEUECNT) {
    672 			printf("%s: bad completion\n", sc->sc_dv.dv_xname);
    673 			continue;
    674 		}
    675 
    676 		ccb = sc->sc_ccbs + cmdid;
    677 		if ((ccb->ccb_flags & TWE_CCB_ACTIVE) == 0) {
    678 			printf("%s: bad completion (not active)\n",
    679 			    sc->sc_dv.dv_xname);
    680 			continue;
    681 		}
    682 		ccb->ccb_flags ^= TWE_CCB_COMPLETE | TWE_CCB_ACTIVE;
    683 
    684 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
    685 		    (caddr_t)ccb->ccb_cmd - sc->sc_cmds,
    686 		    sizeof(struct twe_cmd),
    687 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    688 
    689 		/* Pass notification to upper layers. */
    690 		if (ccb->ccb_tx.tx_handler != NULL)
    691 			(*ccb->ccb_tx.tx_handler)(ccb,
    692 			    ccb->ccb_cmd->tc_status != 0 ? EIO : 0);
    693 	}
    694 
    695 	/* If any commands have completed, run the software queue. */
    696 	if (found)
    697 		twe_ccb_enqueue(sc, NULL);
    698 }
    699 
    700 /*
    701  * Wait for `status' to be set in the controller status register.  Return
    702  * zero if found, non-zero if the operation timed out.
    703  */
    704 static int
    705 twe_status_wait(struct twe_softc *sc, u_int32_t status, int timo)
    706 {
    707 
    708 	for (; timo != 0; timo--) {
    709 		if ((TWE_INL(sc, TWE_REG_STS) & status) == status)
    710 			break;
    711 		delay(100000);
    712 	}
    713 
    714 	return (timo == 0);
    715 }
    716 
    717 /*
    718  * Complain if the status bits aren't what we expect.
    719  */
    720 static int
    721 twe_status_check(struct twe_softc *sc, u_int status)
    722 {
    723 	int rv;
    724 
    725 	rv = 0;
    726 
    727 	if ((status & TWE_STS_EXPECTED_BITS) != TWE_STS_EXPECTED_BITS) {
    728 		printf("%s: missing status bits: 0x%08x\n", sc->sc_dv.dv_xname,
    729 		    status & ~TWE_STS_EXPECTED_BITS);
    730 		rv = -1;
    731 	}
    732 
    733 	if ((status & TWE_STS_UNEXPECTED_BITS) != 0) {
    734 		printf("%s: unexpected status bits: 0x%08x\n",
    735 		    sc->sc_dv.dv_xname, status & TWE_STS_UNEXPECTED_BITS);
    736 		rv = -1;
    737 	}
    738 
    739 	return (rv);
    740 }
    741 
    742 /*
    743  * Allocate and initialise a CCB.
    744  */
    745 int
    746 twe_ccb_alloc(struct twe_softc *sc, struct twe_ccb **ccbp, int flags)
    747 {
    748 	struct twe_cmd *tc;
    749 	struct twe_ccb *ccb;
    750 	int s;
    751 
    752 	s = splbio();
    753 	if ((flags & TWE_CCB_PARAM) != 0)
    754 		ccb = sc->sc_ccbs;
    755 	else {
    756 		/* Allocate a CCB and command block. */
    757 		if (SLIST_FIRST(&sc->sc_ccb_freelist) == NULL) {
    758 			splx(s);
    759 			return (EAGAIN);
    760 		}
    761 		ccb = SLIST_FIRST(&sc->sc_ccb_freelist);
    762 		SLIST_REMOVE_HEAD(&sc->sc_ccb_freelist, ccb_chain.slist);
    763 	}
    764 #ifdef DIAGNOSTIC
    765 	if ((ccb->ccb_flags & TWE_CCB_ALLOCED) != 0)
    766 		panic("twe_ccb_alloc: CCB already allocated");
    767 	flags |= TWE_CCB_ALLOCED;
    768 #endif
    769 	splx(s);
    770 
    771 	/* Initialise some fields and return. */
    772 	ccb->ccb_tx.tx_handler = NULL;
    773 	ccb->ccb_flags = flags;
    774 	tc = ccb->ccb_cmd;
    775 	tc->tc_status = 0;
    776 	tc->tc_flags = 0;
    777 	tc->tc_cmdid = ccb->ccb_cmdid;
    778 	*ccbp = ccb;
    779 
    780 	return (0);
    781 }
    782 
    783 /*
    784  * Free a CCB.
    785  */
    786 void
    787 twe_ccb_free(struct twe_softc *sc, struct twe_ccb *ccb)
    788 {
    789 	int s;
    790 
    791 	s = splbio();
    792 	if ((ccb->ccb_flags & TWE_CCB_PARAM) == 0)
    793 		SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb, ccb_chain.slist);
    794 	ccb->ccb_flags = 0;
    795 	splx(s);
    796 }
    797 
    798 /*
    799  * Map the specified CCB's command block and data buffer (if any) into
    800  * controller visible space.  Perform DMA synchronisation.
    801  */
    802 int
    803 twe_ccb_map(struct twe_softc *sc, struct twe_ccb *ccb)
    804 {
    805 	struct twe_cmd *tc;
    806 	int flags, nsegs, i, s, rv;
    807 	void *data;
    808 
    809 	/*
    810 	 * The data as a whole must be 512-byte aligned.
    811 	 */
    812 	if (((u_long)ccb->ccb_data & (TWE_ALIGNMENT - 1)) != 0) {
    813 		s = splvm();
    814 		/* XXX */
    815 		ccb->ccb_abuf = uvm_km_kmemalloc(kmem_map, uvmexp.kmem_object,
    816 		    ccb->ccb_datasize, UVM_KMF_NOWAIT);
    817 		splx(s);
    818 		data = (void *)ccb->ccb_abuf;
    819 		if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
    820 			memcpy(data, ccb->ccb_data, ccb->ccb_datasize);
    821 	} else {
    822 		ccb->ccb_abuf = (vaddr_t)0;
    823 		data = ccb->ccb_data;
    824 	}
    825 
    826 	/*
    827 	 * Map the data buffer into bus space and build the S/G list.
    828 	 */
    829 	rv = bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer, data,
    830 	    ccb->ccb_datasize, NULL, BUS_DMA_NOWAIT);
    831 	if (rv != 0) {
    832 		if (ccb->ccb_abuf != (vaddr_t)0) {
    833 			s = splvm();
    834 			/* XXX */
    835 			uvm_km_free(kmem_map, ccb->ccb_abuf,
    836 			    ccb->ccb_datasize);
    837 			splx(s);
    838 		}
    839 		return (rv);
    840 	}
    841 
    842 	nsegs = ccb->ccb_dmamap_xfer->dm_nsegs;
    843 	tc = ccb->ccb_cmd;
    844 	tc->tc_size += 2 * nsegs;
    845 
    846 	/* The location of the S/G list is dependant upon command type. */
    847 	switch (tc->tc_opcode >> 5) {
    848 	case 2:
    849 		for (i = 0; i < nsegs; i++) {
    850 			tc->tc_args.param.sgl[i].tsg_address =
    851 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
    852 			tc->tc_args.param.sgl[i].tsg_length =
    853 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
    854 		}
    855 		/* XXX Needed? */
    856 		for (; i < TWE_SG_SIZE; i++) {
    857 			tc->tc_args.param.sgl[i].tsg_address = 0;
    858 			tc->tc_args.param.sgl[i].tsg_length = 0;
    859 		}
    860 		break;
    861 	case 3:
    862 		for (i = 0; i < nsegs; i++) {
    863 			tc->tc_args.io.sgl[i].tsg_address =
    864 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
    865 			tc->tc_args.io.sgl[i].tsg_length =
    866 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
    867 		}
    868 		/* XXX Needed? */
    869 		for (; i < TWE_SG_SIZE; i++) {
    870 			tc->tc_args.io.sgl[i].tsg_address = 0;
    871 			tc->tc_args.io.sgl[i].tsg_length = 0;
    872 		}
    873 		break;
    874 #ifdef DEBUG
    875 	default:
    876 		panic("twe_ccb_map: oops");
    877 #endif
    878 	}
    879 
    880 	if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
    881 		flags = BUS_DMASYNC_PREREAD;
    882 	else
    883 		flags = 0;
    884 	if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
    885 		flags |= BUS_DMASYNC_PREWRITE;
    886 
    887 	bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
    888 	    ccb->ccb_datasize, flags);
    889 	return (0);
    890 }
    891 
    892 /*
    893  * Unmap the specified CCB's command block and data buffer (if any) and
    894  * perform DMA synchronisation.
    895  */
    896 void
    897 twe_ccb_unmap(struct twe_softc *sc, struct twe_ccb *ccb)
    898 {
    899 	int flags, s;
    900 
    901 	if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
    902 		flags = BUS_DMASYNC_POSTREAD;
    903 	else
    904 		flags = 0;
    905 	if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
    906 		flags |= BUS_DMASYNC_POSTWRITE;
    907 
    908 	bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
    909 	    ccb->ccb_datasize, flags);
    910 	bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
    911 
    912 	if (ccb->ccb_abuf != (vaddr_t)0) {
    913 		if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
    914 			memcpy(ccb->ccb_data, (void *)ccb->ccb_abuf,
    915 			    ccb->ccb_datasize);
    916 		s = splvm();
    917 		/* XXX */
    918 		uvm_km_free(kmem_map, ccb->ccb_abuf, ccb->ccb_datasize);
    919 		splx(s);
    920 	}
    921 }
    922 
    923 /*
    924  * Submit a command to the controller and poll on completion.  Return
    925  * non-zero on timeout (but don't check status, as some command types don't
    926  * return status).  Must be called with interrupts blocked.
    927  */
    928 int
    929 twe_ccb_poll(struct twe_softc *sc, struct twe_ccb *ccb, int timo)
    930 {
    931 	int rv;
    932 
    933 	if ((rv = twe_ccb_submit(sc, ccb)) != 0)
    934 		return (rv);
    935 
    936 	for (; timo != 0; timo--) {
    937 		twe_poll(sc);
    938 		if ((ccb->ccb_flags & TWE_CCB_COMPLETE) != 0)
    939 			break;
    940 		DELAY(100000);
    941 	}
    942 
    943 	return (timo == 0);
    944 }
    945 
    946 /*
    947  * If a CCB is specified, enqueue it.  Pull CCBs off the software queue in
    948  * the order that they were enqueued and try to submit their command blocks
    949  * to the controller for execution.
    950  */
    951 void
    952 twe_ccb_enqueue(struct twe_softc *sc, struct twe_ccb *ccb)
    953 {
    954 	int s;
    955 
    956 	s = splbio();
    957 
    958 	if (ccb != NULL)
    959 		SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain.simpleq);
    960 
    961 	while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
    962 		if (twe_ccb_submit(sc, ccb))
    963 			break;
    964 		SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb, ccb_chain.simpleq);
    965 	}
    966 
    967 	splx(s);
    968 }
    969 
    970 /*
    971  * Submit the command block associated with the specified CCB to the
    972  * controller for execution.  Must be called with interrupts blocked.
    973  */
    974 int
    975 twe_ccb_submit(struct twe_softc *sc, struct twe_ccb *ccb)
    976 {
    977 	bus_addr_t pa;
    978 	int rv;
    979 	u_int status;
    980 
    981 	/* Check to see if we can post a command. */
    982 	status = TWE_INL(sc, TWE_REG_STS);
    983 	twe_status_check(sc, status);
    984 
    985 	if ((status & TWE_STS_CMD_QUEUE_FULL) == 0) {
    986 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
    987 		    (caddr_t)ccb->ccb_cmd - sc->sc_cmds, sizeof(struct twe_cmd),
    988 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    989 		ccb->ccb_flags |= TWE_CCB_ACTIVE;
    990 		pa = sc->sc_cmds_paddr +
    991 		    ccb->ccb_cmdid * sizeof(struct twe_cmd);
    992 		TWE_OUTL(sc, TWE_REG_CMD_QUEUE, (u_int32_t)pa);
    993 		rv = 0;
    994 	} else
    995 		rv = EBUSY;
    996 
    997 	return (rv);
    998 }
    999