twe.c revision 240137
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 2000 Michael Smith
31541Srgrimes * Copyright (c) 2003 Paul Saab
41541Srgrimes * Copyright (c) 2003 Vinod Kashyap
51541Srgrimes * Copyright (c) 2000 BSDi
61541Srgrimes * All rights reserved.
71541Srgrimes *
81541Srgrimes * Redistribution and use in source and binary forms, with or without
91541Srgrimes * modification, are permitted provided that the following conditions
101541Srgrimes * are met:
111541Srgrimes * 1. Redistributions of source code must retain the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer.
131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer in the
151541Srgrimes *    documentation and/or other materials provided with the distribution.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
291541Srgrimes *	$FreeBSD: head/sys/dev/twe/twe.c 240137 2012-09-05 14:31:58Z jhb $
301541Srgrimes */
311541Srgrimes
321541Srgrimes/*
331541Srgrimes * Driver for the 3ware Escalade family of IDE RAID controllers.
3432350Seivind */
351541Srgrimes
361541Srgrimes#include <dev/twe/twe_compat.h>
371541Srgrimes#include <dev/twe/twereg.h>
381541Srgrimes#include <dev/twe/tweio.h>
391541Srgrimes#include <dev/twe/twevar.h>
401541Srgrimes#define TWE_DEFINE_TABLES
411541Srgrimes#include <dev/twe/twe_tables.h>
421541Srgrimes
4332350Seivind/*
4432350Seivind * Command submission.
451541Srgrimes */
4612693Sphkstatic int	twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result);
4712693Sphkstatic int	twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result);
481541Srgrimesstatic int	twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result);
4912693Sphkstatic void	*twe_get_param(struct twe_softc *sc, int table_id, int parameter_id, size_t size,
501541Srgrimes					       void (* func)(struct twe_request *tr));
5118892Sbde#ifdef TWE_SHUTDOWN_NOTIFICATION
521541Srgrimesstatic int	twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value);
531541Srgrimes#endif
541541Srgrimes#if 0
551541Srgrimesstatic int	twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value);
561541Srgrimesstatic int	twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value);
578426Swollman#endif
581541Srgrimesstatic int	twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size,
591541Srgrimes					      void *data);
601541Srgrimesstatic int	twe_init_connection(struct twe_softc *sc, int mode);
611541Srgrimesstatic int	twe_wait_request(struct twe_request *tr);
621541Srgrimesstatic int	twe_immediate_request(struct twe_request *tr, int usetmp);
631541Srgrimesstatic void	twe_completeio(struct twe_request *tr);
641541Srgrimesstatic void	twe_reset(struct twe_softc *sc);
651541Srgrimesstatic int	twe_add_unit(struct twe_softc *sc, int unit);
6612942Swollmanstatic int	twe_del_unit(struct twe_softc *sc, int unit);
671541Srgrimes
6812693Sphk/*
6912942Swollman * Command I/O to controller.
7012942Swollman */
7112942Swollmanstatic void	twe_done(struct twe_softc *sc, int startio);
721541Srgrimesstatic void	twe_complete(struct twe_softc *sc);
7312942Swollmanstatic int	twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout);
7412942Swollmanstatic int	twe_drain_response_queue(struct twe_softc *sc);
7512942Swollmanstatic int	twe_check_bits(struct twe_softc *sc, u_int32_t status_reg);
7612942Swollmanstatic int	twe_soft_reset(struct twe_softc *sc);
7712942Swollman
7812942Swollman/*
7912693Sphk * Interrupt handling.
801541Srgrimes */
811541Srgrimesstatic void	twe_host_intr(struct twe_softc *sc);
8211225Swollmanstatic void	twe_attention_intr(struct twe_softc *sc);
8311225Swollmanstatic void	twe_command_intr(struct twe_softc *sc);
8411225Swollman
8511225Swollman/*
8611225Swollman * Asynchronous event handling.
8711225Swollman */
8811225Swollmanstatic int	twe_fetch_aen(struct twe_softc *sc);
8912693Sphkstatic void	twe_handle_aen(struct twe_request *tr);
9011225Swollmanstatic void	twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen);
9111225Swollmanstatic u_int16_t	twe_dequeue_aen(struct twe_softc *sc);
921541Srgrimesstatic int	twe_drain_aen_queue(struct twe_softc *sc);
9312820Sphkstatic int	twe_find_aen(struct twe_softc *sc, u_int16_t aen);
941541Srgrimes
9512693Sphk/*
9612942Swollman * Command buffer management.
9712942Swollman */
983282Swollmanstatic int	twe_get_request(struct twe_softc *sc, struct twe_request **tr);
9912942Swollmanstatic void	twe_release_request(struct twe_request *tr);
10012942Swollman
10112942Swollman/*
10212942Swollman * Debugging.
10312942Swollman */
10412942Swollmanstatic char 	*twe_format_aen(struct twe_softc *sc, u_int16_t aen);
10512693Sphkstatic int	twe_report_request(struct twe_request *tr);
10612877Sbdestatic void	twe_panic(struct twe_softc *sc, char *reason);
10712693Sphk
10812693Sphk/********************************************************************************
10912693Sphk ********************************************************************************
11012693Sphk                                                                Public Interfaces
11112693Sphk ********************************************************************************
11212693Sphk ********************************************************************************/
11332350Seivind
11412693Sphk/********************************************************************************
11532350Seivind * Initialise the controller, set up driver data structures.
11612693Sphk */
1171541Srgrimesint
1181541Srgrimestwe_setup(struct twe_softc *sc)
1191541Srgrimes{
1201541Srgrimes    struct twe_request	*tr;
1211541Srgrimes    TWE_Command		*cmd;
1221541Srgrimes    u_int32_t		status_reg;
1231541Srgrimes    int			i;
1241541Srgrimes
1251541Srgrimes    debug_called(4);
12611225Swollman
12711225Swollman    /*
1281541Srgrimes     * Initialise request queues.
1291541Srgrimes     */
13011225Swollman    twe_initq_free(sc);
1311541Srgrimes    twe_initq_bio(sc);
13211225Swollman    twe_initq_ready(sc);
1331541Srgrimes    twe_initq_busy(sc);
13411225Swollman    twe_initq_complete(sc);
1351541Srgrimes    sc->twe_wait_aen = -1;
1361541Srgrimes
1371541Srgrimes    /*
1381541Srgrimes     * Allocate request structures up front.
1391541Srgrimes     */
1401541Srgrimes    for (i = 0; i < TWE_Q_LENGTH; i++) {
1411541Srgrimes	if ((tr = twe_allocate_request(sc, i)) == NULL)
1425196Swollman	    return(ENOMEM);
1431541Srgrimes	/*
1441541Srgrimes	 * Set global defaults that won't change.
1451541Srgrimes	 */
1461541Srgrimes	cmd = TWE_FIND_COMMAND(tr);
1471541Srgrimes	cmd->generic.host_id = sc->twe_host_id;		/* controller-assigned host ID */
1481541Srgrimes	cmd->generic.request_id = i;			/* our index number */
1491541Srgrimes	sc->twe_lookup[i] = tr;
1501541Srgrimes
15112693Sphk	/*
1521541Srgrimes	 * Put command onto the freelist.
1531541Srgrimes	 */
1541541Srgrimes	TWE_IO_LOCK(sc);
15511225Swollman	twe_release_request(tr);
1561541Srgrimes	TWE_IO_UNLOCK(sc);
1571541Srgrimes    }
1581541Srgrimes    TWE_IO_LOCK(sc);
1591541Srgrimes
1601541Srgrimes    /*
1611541Srgrimes     * Check status register for errors, clear them.
1621541Srgrimes     */
1631541Srgrimes    status_reg = TWE_STATUS(sc);
1641541Srgrimes    twe_check_bits(sc, status_reg);
1651541Srgrimes
1661541Srgrimes    /*
1671541Srgrimes     * Wait for the controller to come ready.
1681541Srgrimes     */
1691541Srgrimes    if (twe_wait_status(sc, TWE_STATUS_MICROCONTROLLER_READY, 60)) {
1701541Srgrimes	TWE_IO_UNLOCK(sc);
1711541Srgrimes	twe_printf(sc, "microcontroller not ready\n");
1721541Srgrimes	return(ENXIO);
1731541Srgrimes    }
1741541Srgrimes
1751541Srgrimes    /*
1761541Srgrimes     * Disable interrupts from the card.
1771541Srgrimes     */
1781541Srgrimes    twe_disable_interrupts(sc);
1791541Srgrimes
1801541Srgrimes    /*
1811541Srgrimes     * Soft reset the controller, look for the AEN acknowledging the reset,
1821541Srgrimes     * check for errors, drain the response queue.
1831541Srgrimes     */
1841541Srgrimes    for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
1851541Srgrimes
1861541Srgrimes	if (i > 0)
1871541Srgrimes	    twe_printf(sc, "reset %d failed, trying again\n", i);
1881541Srgrimes
1891541Srgrimes	if (!twe_soft_reset(sc))
1901541Srgrimes	    break;			/* reset process complete */
1911541Srgrimes    }
1921541Srgrimes    TWE_IO_UNLOCK(sc);
1936568Sdg    /* did we give up? */
1941541Srgrimes    if (i >= TWE_MAX_RESET_TRIES) {
1951541Srgrimes	twe_printf(sc, "can't initialise controller, giving up\n");
1961541Srgrimes	return(ENXIO);
1971541Srgrimes    }
1981541Srgrimes
1991541Srgrimes    return(0);
2001541Srgrimes}
2011541Srgrimes
2021541Srgrimesstatic int
2031541Srgrimestwe_add_unit(struct twe_softc *sc, int unit)
2041541Srgrimes{
2051541Srgrimes    struct twe_drive		*dr;
2061541Srgrimes    int				table, error = 0;
2071541Srgrimes    u_int16_t			dsize;
2081541Srgrimes    TWE_Param			*drives = NULL, *param = NULL;
2091541Srgrimes    TWE_Array_Descriptor	*ud;
2101541Srgrimes
2111541Srgrimes    TWE_CONFIG_ASSERT_LOCKED(sc);
2121541Srgrimes    if (unit < 0 || unit > TWE_MAX_UNITS)
2131541Srgrimes	return (EINVAL);
21411225Swollman
21513926Swollman    /*
21632350Seivind     * The controller is in a safe state, so try to find drives attached to it.
21713926Swollman     */
21813926Swollman    TWE_IO_LOCK(sc);
21913926Swollman    if ((drives = twe_get_param(sc, TWE_PARAM_UNITSUMMARY, TWE_PARAM_UNITSUMMARY_Status,
22013926Swollman				TWE_MAX_UNITS, NULL)) == NULL) {
22113926Swollman	TWE_IO_UNLOCK(sc);
22213926Swollman	twe_printf(sc, "can't detect attached units\n");
22313926Swollman	return (EIO);
22413926Swollman    }
22513926Swollman
22616576Speter    dr = &sc->twe_drive[unit];
22713926Swollman    /* check that the drive is online */
22813926Swollman    if (!(drives->data[unit] & TWE_PARAM_UNITSTATUS_Online)) {
22913926Swollman	TWE_IO_UNLOCK(sc);
23013926Swollman	error = ENXIO;
23116576Speter	goto out;
23213926Swollman    }
23332350Seivind
23413926Swollman    table = TWE_PARAM_UNITINFO + unit;
2351541Srgrimes
2361541Srgrimes    if (twe_get_param_4(sc, table, TWE_PARAM_UNITINFO_Capacity, &dr->td_size)) {
2371541Srgrimes	TWE_IO_UNLOCK(sc);
2381541Srgrimes	twe_printf(sc, "error fetching capacity for unit %d\n", unit);
2391541Srgrimes	error = EIO;
2401541Srgrimes	goto out;
2411541Srgrimes    }
2421541Srgrimes    if (twe_get_param_1(sc, table, TWE_PARAM_UNITINFO_Status, &dr->td_state)) {
2431541Srgrimes	TWE_IO_UNLOCK(sc);
2441541Srgrimes	twe_printf(sc, "error fetching state for unit %d\n", unit);
2451541Srgrimes	error = EIO;
2461541Srgrimes	goto out;
2471541Srgrimes    }
2481541Srgrimes    if (twe_get_param_2(sc, table, TWE_PARAM_UNITINFO_DescriptorSize, &dsize)) {
2491541Srgrimes	TWE_IO_UNLOCK(sc);
2501541Srgrimes	twe_printf(sc, "error fetching descriptor size for unit %d\n", unit);
2518090Spst	error = EIO;
2521541Srgrimes	goto out;
2531541Srgrimes    }
2541541Srgrimes    if ((param = twe_get_param(sc, table, TWE_PARAM_UNITINFO_Descriptor, dsize - 3, NULL)) == NULL) {
2551541Srgrimes	TWE_IO_UNLOCK(sc);
2561541Srgrimes	twe_printf(sc, "error fetching descriptor for unit %d\n", unit);
2571541Srgrimes	error = EIO;
2581541Srgrimes	goto out;
2591541Srgrimes    }
26011225Swollman    ud = (TWE_Array_Descriptor *)param->data;
2611541Srgrimes    dr->td_type = ud->configuration;
2621541Srgrimes    dr->td_stripe = ud->stripe_size;
2631541Srgrimes
2641541Srgrimes    /* build synthetic geometry as per controller internal rules */
2651541Srgrimes    if (dr->td_size > 0x200000) {
2661541Srgrimes	dr->td_heads = 255;
2671541Srgrimes	dr->td_sectors = 63;
2681541Srgrimes    } else {
2691541Srgrimes	dr->td_heads = 64;
2701541Srgrimes	dr->td_sectors = 32;
2711541Srgrimes    }
2721541Srgrimes    dr->td_cylinders = dr->td_size / (dr->td_heads * dr->td_sectors);
2731541Srgrimes    dr->td_twe_unit = unit;
2741541Srgrimes    TWE_IO_UNLOCK(sc);
2751541Srgrimes
2761541Srgrimes    error = twe_attach_drive(sc, dr);
2771541Srgrimes
2781541Srgrimesout:
2791541Srgrimes    if (param != NULL)
2801541Srgrimes	free(param, M_DEVBUF);
2811541Srgrimes    if (drives != NULL)
2821541Srgrimes	free(drives, M_DEVBUF);
2831541Srgrimes    return (error);
2841541Srgrimes}
2851541Srgrimes
2861541Srgrimesstatic int
2871541Srgrimestwe_del_unit(struct twe_softc *sc, int unit)
2881541Srgrimes{
2891541Srgrimes    int error;
2901541Srgrimes
2911541Srgrimes    TWE_CONFIG_ASSERT_LOCKED(sc);
2921541Srgrimes    if (unit < 0 || unit >= TWE_MAX_UNITS)
2931541Srgrimes	return (ENXIO);
2948384Sdg
29516341Sdg    if (sc->twe_drive[unit].td_disk == NULL)
2961541Srgrimes	return (ENXIO);
2971541Srgrimes
2981541Srgrimes    error = twe_detach_drive(sc, unit);
2991541Srgrimes    return (error);
3001541Srgrimes}
3018384Sdg
3028384Sdg/********************************************************************************
3038384Sdg * Locate disk devices and attach children to them.
3041541Srgrimes */
3051541Srgrimesvoid
3061541Srgrimestwe_init(struct twe_softc *sc)
3071541Srgrimes{
3081541Srgrimes    int 		i;
3091541Srgrimes
3101541Srgrimes    /*
3111541Srgrimes     * Scan for drives
3121541Srgrimes     */
3131541Srgrimes    TWE_CONFIG_LOCK(sc);
3141541Srgrimes    for (i = 0; i < TWE_MAX_UNITS; i++)
3151541Srgrimes	twe_add_unit(sc, i);
3161541Srgrimes    TWE_CONFIG_UNLOCK(sc);
3171541Srgrimes
3181541Srgrimes    /*
3191541Srgrimes     * Initialise connection with controller.
3203514Swollman     */
3211541Srgrimes    TWE_IO_LOCK(sc);
3221541Srgrimes    twe_init_connection(sc, TWE_INIT_MESSAGE_CREDITS);
3231541Srgrimes
3241541Srgrimes#ifdef TWE_SHUTDOWN_NOTIFICATION
3251541Srgrimes    /*
3263514Swollman     * Tell the controller we support shutdown notification.
3271541Srgrimes     */
3281541Srgrimes    twe_set_param_1(sc, TWE_PARAM_FEATURES, TWE_PARAM_FEATURES_DriverShutdown, 1);
3291541Srgrimes#endif
3301541Srgrimes
3311541Srgrimes    /*
3328384Sdg     * Mark controller up and ready to run.
3331541Srgrimes     */
3341541Srgrimes    sc->twe_state &= ~TWE_STATE_SHUTDOWN;
3351541Srgrimes
3361541Srgrimes    /*
3371541Srgrimes     * Finally enable interrupts.
3381541Srgrimes     */
3391541Srgrimes    twe_enable_interrupts(sc);
3401541Srgrimes    TWE_IO_UNLOCK(sc);
3411541Srgrimes}
3423311Sphk
3433311Sphk/********************************************************************************
3441541Srgrimes * Stop the controller
3451541Srgrimes */
3461541Srgrimesvoid
34714163Sfennertwe_deinit(struct twe_softc *sc)
34814163Sfenner{
3491541Srgrimes    /*
3501541Srgrimes     * Mark the controller as shutting down, and disable any further interrupts.
3511541Srgrimes     */
3521541Srgrimes    TWE_IO_ASSERT_LOCKED(sc);
3531541Srgrimes    sc->twe_state |= TWE_STATE_SHUTDOWN;
3541541Srgrimes    twe_disable_interrupts(sc);
3551541Srgrimes
3561541Srgrimes#ifdef TWE_SHUTDOWN_NOTIFICATION
3571541Srgrimes    /*
3581541Srgrimes     * Disconnect from the controller
35916206Sbde     */
3601541Srgrimes    twe_init_connection(sc, TWE_SHUTDOWN_MESSAGE_CREDITS);
3611541Srgrimes#endif
3621541Srgrimes}
3631541Srgrimes
3641541Srgrimes/*******************************************************************************
3651541Srgrimes * Take an interrupt, or be poked by other code to look for interrupt-worthy
3661541Srgrimes * status.
3671541Srgrimes */
3681541Srgrimesvoid
3691541Srgrimestwe_intr(struct twe_softc *sc)
3701541Srgrimes{
3711541Srgrimes    u_int32_t		status_reg;
3721541Srgrimes
3731541Srgrimes    debug_called(4);
3741541Srgrimes
37514761Sfenner    /*
37616557Sfenner     * Collect current interrupt status.
37714761Sfenner     */
37814761Sfenner    status_reg = TWE_STATUS(sc);
3791541Srgrimes    twe_check_bits(sc, status_reg);
3801541Srgrimes
3811541Srgrimes    /*
3821541Srgrimes     * Dispatch based on interrupt status
3831541Srgrimes     */
3841541Srgrimes    if (status_reg & TWE_STATUS_HOST_INTERRUPT)
3851541Srgrimes	twe_host_intr(sc);
3861541Srgrimes    if (status_reg & TWE_STATUS_ATTENTION_INTERRUPT)
3871541Srgrimes	twe_attention_intr(sc);
3881541Srgrimes    if (status_reg & TWE_STATUS_COMMAND_INTERRUPT)
3891541Srgrimes	twe_command_intr(sc);
3901541Srgrimes    if (status_reg & TWE_STATUS_RESPONSE_INTERRUPT)
3911541Srgrimes	twe_done(sc, 1);
3921541Srgrimes};
3931541Srgrimes
39412693Sphk/********************************************************************************
39531884Sbde * Pull as much work off the softc's work queue as possible and give it to the
3961541Srgrimes * controller.
3971541Srgrimes */
3981541Srgrimesvoid
3991541Srgrimestwe_startio(struct twe_softc *sc)
4001541Srgrimes{
4011541Srgrimes    struct twe_request	*tr;
4021541Srgrimes    TWE_Command		*cmd;
4031541Srgrimes    twe_bio		*bp;
4041541Srgrimes    int			error;
4051541Srgrimes
4061541Srgrimes    debug_called(4);
4071541Srgrimes
4081541Srgrimes    TWE_IO_ASSERT_LOCKED(sc);
4091541Srgrimes    if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN))
4101541Srgrimes	return;
4111541Srgrimes
4121541Srgrimes    /* spin until something prevents us from doing any work */
4131541Srgrimes    for (;;) {
4141541Srgrimes
41532350Seivind	/* try to get a command that's already ready to go */
4161541Srgrimes	tr = twe_dequeue_ready(sc);
4171541Srgrimes
4181541Srgrimes	/* build a command from an outstanding bio */
41932350Seivind	if (tr == NULL) {
4201541Srgrimes
4211541Srgrimes	    /* get a command to handle the bio with */
4221541Srgrimes	    if (twe_get_request(sc, &tr))
4231541Srgrimes		break;
4241541Srgrimes
4258426Swollman	    /* see if there's work to be done */
4268426Swollman	    if ((bp = twe_dequeue_bio(sc)) == NULL) {
42732350Seivind		twe_release_request(tr);
42832350Seivind		break;
4291541Srgrimes	    }
4301541Srgrimes
4311541Srgrimes	    /* connect the bio to the command */
4321541Srgrimes	    tr->tr_complete = twe_completeio;
4331541Srgrimes	    tr->tr_private = bp;
4341541Srgrimes	    tr->tr_data = TWE_BIO_DATA(bp);
4351541Srgrimes	    tr->tr_length = TWE_BIO_LENGTH(bp);
4361541Srgrimes	    cmd = TWE_FIND_COMMAND(tr);
4371541Srgrimes	    if (TWE_BIO_IS_READ(bp)) {
4381541Srgrimes		tr->tr_flags |= TWE_CMD_DATAIN;
4391541Srgrimes		cmd->io.opcode = TWE_OP_READ;
4401541Srgrimes	    } else {
4411541Srgrimes		tr->tr_flags |= TWE_CMD_DATAOUT;
4421541Srgrimes		cmd->io.opcode = TWE_OP_WRITE;
4431541Srgrimes	    }
4441541Srgrimes
4451541Srgrimes	    /* build a suitable I/O command (assumes 512-byte rounded transfers) */
4461541Srgrimes	    cmd->io.size = 3;
4471541Srgrimes	    cmd->io.unit = TWE_BIO_UNIT(bp);
4481541Srgrimes	    cmd->io.block_count = (tr->tr_length + TWE_BLOCK_SIZE - 1) / TWE_BLOCK_SIZE;
4491541Srgrimes	    cmd->io.lba = TWE_BIO_LBA(bp);
4501541Srgrimes	}
4511541Srgrimes
4521541Srgrimes	/* did we find something to do? */
4531541Srgrimes	if (tr == NULL)
4541541Srgrimes	    break;
4551541Srgrimes
4561541Srgrimes	/* try to map and submit the command to controller */
4571541Srgrimes	error = twe_map_request(tr);
4581541Srgrimes
4591541Srgrimes	if (error != 0) {
4608384Sdg	    if (error == EBUSY)
4618384Sdg		break;
46220525Sbde	    tr->tr_status = TWE_CMD_ERROR;
4631541Srgrimes	    if (tr->tr_private != NULL) {
4641541Srgrimes		bp = (twe_bio *)(tr->tr_private);
4651541Srgrimes		TWE_BIO_SET_ERROR(bp, error);
4661541Srgrimes		tr->tr_private = NULL;
4671541Srgrimes		twed_intr(bp);
4681541Srgrimes	        twe_release_request(tr);
46912693Sphk	    } else if (tr->tr_flags & TWE_CMD_SLEEPER)
47012693Sphk		wakeup_one(tr); /* wakeup the sleeping owner */
47112693Sphk	}
47212693Sphk    }
4731541Srgrimes}
4741541Srgrimes
47512693Sphk/********************************************************************************
47612693Sphk * Write blocks from memory to disk, for system crash dumps.
47712693Sphk */
47812693Sphkint
4791541Srgrimestwe_dump_blocks(struct twe_softc *sc, int unit,	u_int32_t lba, void *data, int nblks)
4801541Srgrimes{
4811541Srgrimes    struct twe_request	*tr;
4824069Swollman    TWE_Command		*cmd;
4837088Swollman    int			error;
48412693Sphk
48512693Sphk    if (twe_get_request(sc, &tr))
4861541Srgrimes	return(ENOMEM);
4871541Srgrimes
4881541Srgrimes    tr->tr_data = data;
48919794Sfenner    tr->tr_status = TWE_CMD_SETUP;
49019794Sfenner    tr->tr_length = nblks * TWE_BLOCK_SIZE;
4911541Srgrimes    tr->tr_flags = TWE_CMD_DATAOUT;
4921541Srgrimes
4931541Srgrimes    cmd = TWE_FIND_COMMAND(tr);
4941541Srgrimes    cmd->io.opcode = TWE_OP_WRITE;
4951541Srgrimes    cmd->io.size = 3;
4961541Srgrimes    cmd->io.unit = unit;
4971541Srgrimes    cmd->io.block_count = nblks;
49819794Sfenner    cmd->io.lba = lba;
49919794Sfenner
50019794Sfenner    error = twe_immediate_request(tr, 0);
5018384Sdg    if (error == 0)
5028384Sdg	if (twe_report_request(tr))
5031541Srgrimes	    error = EIO;
5041541Srgrimes    twe_release_request(tr);
5051541Srgrimes    return(error);
5061541Srgrimes}
5071541Srgrimes
5081541Srgrimes/********************************************************************************
5091541Srgrimes * Handle controller-specific control operations.
5101541Srgrimes */
5111541Srgrimesint
5121541Srgrimestwe_ioctl(struct twe_softc *sc, u_long ioctlcmd, void *addr)
5131541Srgrimes{
5141541Srgrimes    struct twe_usercommand	*tu = (struct twe_usercommand *)addr;
5151541Srgrimes    struct twe_paramcommand	*tp = (struct twe_paramcommand *)addr;
5161541Srgrimes    struct twe_drivecommand	*td = (struct twe_drivecommand *)addr;
5171541Srgrimes    union twe_statrequest	*ts = (union twe_statrequest *)addr;
5181541Srgrimes    TWE_Param			*param;
5191541Srgrimes    TWE_Command			*cmd;
5208384Sdg    void			*data;
5218384Sdg    u_int16_t			*aen_code = (u_int16_t *)addr;
5221541Srgrimes    struct twe_request		*tr;
5231541Srgrimes    u_int8_t			srid;
5243282Swollman    int				error;
5253282Swollman    size_t			tr_length;
5263282Swollman
52712693Sphk    error = 0;
52812693Sphk    switch(ioctlcmd) {
52912693Sphk	/* handle a command from userspace */
53012693Sphk    case TWEIO_COMMAND:
5313282Swollman	/*
5323282Swollman	 * if there's a data buffer, allocate and copy it in.
5333282Swollman	 * Must be in multipled of 512 bytes.
5343282Swollman	 */
5353282Swollman	tr_length = roundup2(tu->tu_size, 512);
5363282Swollman	if (tr_length > 0) {
5375101Swollman	    data = malloc(tr_length, M_DEVBUF, M_WAITOK);
53812693Sphk	    error = copyin(tu->tu_data, data, tu->tu_size);
53912693Sphk	    if (error) {
54012693Sphk		free(data, M_DEVBUF);
54112693Sphk		break;
5423282Swollman	    }
5433282Swollman	} else
5443282Swollman	    data = NULL;
5453282Swollman
5463282Swollman	/* get a request */
54712693Sphk	TWE_IO_LOCK(sc);
5483282Swollman	while (twe_get_request(sc, &tr))
54912693Sphk	    mtx_sleep(sc, &sc->twe_io_lock, PPAUSE, "twioctl", hz);
55012693Sphk
5513282Swollman	/*
5528384Sdg	 * Save the command's request ID, copy the user-supplied command in,
5538384Sdg	 * restore the request ID.
5543282Swollman	 */
5554069Swollman	cmd = TWE_FIND_COMMAND(tr);
5568876Srgrimes	srid = cmd->generic.request_id;
5577088Swollman	bcopy(&tu->tu_command, cmd, sizeof(TWE_Command));
5584069Swollman	cmd->generic.request_id = srid;
5593282Swollman
5603282Swollman	tr->tr_length = tr_length;
5618384Sdg	tr->tr_data = data;
5623282Swollman	if (tr->tr_length > 0) {
5638384Sdg	    tr->tr_flags |= TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
5643282Swollman	}
5651541Srgrimes
5661541Srgrimes	/* run the command */
5678384Sdg	error = twe_wait_request(tr);
5688384Sdg	TWE_IO_UNLOCK(sc);
5691541Srgrimes	if (error)
5701541Srgrimes	    goto cmd_done;
5711541Srgrimes
5728384Sdg	/* copy the command out again */
57316341Sdg	bcopy(cmd, &tu->tu_command, sizeof(TWE_Command));
5741541Srgrimes
5751541Srgrimes	/* if there was a data buffer, copy it out */
5761541Srgrimes	if (tr->tr_length > 0)
5771541Srgrimes	    error = copyout(tr->tr_data, tu->tu_data, tu->tu_size);
5781541Srgrimes
57932350Seivind    cmd_done:
5801541Srgrimes	/* free resources */
5811541Srgrimes	if (tr->tr_data != NULL)
5821541Srgrimes	    free(tr->tr_data, M_DEVBUF);
5831541Srgrimes	TWE_IO_LOCK(sc);
5841541Srgrimes	twe_release_request(tr);
5851541Srgrimes	TWE_IO_UNLOCK(sc);
5861541Srgrimes
5871541Srgrimes	break;
5881541Srgrimes
5891541Srgrimes	/* fetch statistics counter */
5901541Srgrimes    case TWEIO_STATS:
5911541Srgrimes	switch (ts->ts_item) {
5921541Srgrimes#ifdef TWE_PERFORMANCE_MONITOR
5931541Srgrimes	case TWEQ_FREE:
5941541Srgrimes	case TWEQ_BIO:
5951541Srgrimes	case TWEQ_READY:
5961541Srgrimes	case TWEQ_BUSY:
5971541Srgrimes	case TWEQ_COMPLETE:
5981541Srgrimes	    TWE_IO_LOCK(sc);
5991541Srgrimes	    bcopy(&sc->twe_qstat[ts->ts_item], &ts->ts_qstat, sizeof(struct twe_qstat));
6001541Srgrimes	    TWE_IO_UNLOCK(sc);
6011541Srgrimes	    break;
6021541Srgrimes#endif
6031541Srgrimes	default:
6041541Srgrimes	    error = ENOENT;
6051541Srgrimes	    break;
6061541Srgrimes	}
6071541Srgrimes	break;
6081541Srgrimes
6091541Srgrimes	/* poll for an AEN */
6101541Srgrimes    case TWEIO_AEN_POLL:
6111541Srgrimes	TWE_IO_LOCK(sc);
6124069Swollman	*aen_code = twe_dequeue_aen(sc);
6131541Srgrimes	TWE_IO_UNLOCK(sc);
6141541Srgrimes	break;
6151541Srgrimes
6165101Swollman	/* wait for another AEN to show up */
6171541Srgrimes    case TWEIO_AEN_WAIT:
6181541Srgrimes	TWE_IO_LOCK(sc);
6191541Srgrimes	while ((*aen_code = twe_dequeue_aen(sc)) == TWE_AEN_QUEUE_EMPTY) {
6204069Swollman	    error = mtx_sleep(&sc->twe_aen_queue, &sc->twe_io_lock, PRIBIO | PCATCH,
62112693Sphk		"tweaen", 0);
6224069Swollman	    if (error == EINTR)
62312693Sphk		break;
6244069Swollman	}
62512693Sphk	TWE_IO_UNLOCK(sc);
6264069Swollman	break;
6274069Swollman
62812693Sphk    case TWEIO_GET_PARAM:
6294069Swollman	TWE_IO_LOCK(sc);
6307088Swollman	param = twe_get_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, NULL);
6314069Swollman	TWE_IO_UNLOCK(sc);
63212693Sphk	if (param == NULL) {
6334069Swollman	    twe_printf(sc, "TWEIO_GET_PARAM failed for 0x%x/0x%x/%d\n",
6341541Srgrimes		       tp->tp_table_id, tp->tp_param_id, tp->tp_size);
6351541Srgrimes	    error = EINVAL;
6361541Srgrimes	} else {
6371541Srgrimes	    if (param->parameter_size_bytes > tp->tp_size) {
6385195Swollman		twe_printf(sc, "TWEIO_GET_PARAM parameter too large (%d > %d)\n",
6395195Swollman			   param->parameter_size_bytes, tp->tp_size);
6405195Swollman		error = EFAULT;
6415195Swollman	    } else {
6425195Swollman		error = copyout(param->data, tp->tp_data, param->parameter_size_bytes);
64325822Stegge	    }
64425822Stegge	    free(param, M_DEVBUF);
64525822Stegge	}
6465195Swollman	break;
6475195Swollman
6485195Swollman    case TWEIO_SET_PARAM:
649	data = malloc(tp->tp_size, M_DEVBUF, M_WAITOK);
650	error = copyin(tp->tp_data, data, tp->tp_size);
651	if (error == 0) {
652	    TWE_IO_LOCK(sc);
653	    error = twe_set_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, data);
654	    TWE_IO_UNLOCK(sc);
655	}
656	free(data, M_DEVBUF);
657	break;
658
659    case TWEIO_RESET:
660	TWE_IO_LOCK(sc);
661	twe_reset(sc);
662	TWE_IO_UNLOCK(sc);
663	break;
664
665    case TWEIO_ADD_UNIT:
666	TWE_CONFIG_LOCK(sc);
667	error = twe_add_unit(sc, td->td_unit);
668	TWE_CONFIG_UNLOCK(sc);
669	break;
670
671    case TWEIO_DEL_UNIT:
672	TWE_CONFIG_LOCK(sc);
673	error = twe_del_unit(sc, td->td_unit);
674	TWE_CONFIG_UNLOCK(sc);
675	break;
676
677	/* XXX implement ATA PASSTHROUGH */
678
679	/* nothing we understand */
680    default:
681	error = ENOTTY;
682    }
683
684    return(error);
685}
686
687/********************************************************************************
688 * Enable the useful interrupts from the controller.
689 */
690void
691twe_enable_interrupts(struct twe_softc *sc)
692{
693    sc->twe_state |= TWE_STATE_INTEN;
694    TWE_CONTROL(sc,
695	       TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT |
696	       TWE_CONTROL_UNMASK_RESPONSE_INTERRUPT |
697	       TWE_CONTROL_ENABLE_INTERRUPTS);
698}
699
700/********************************************************************************
701 * Disable interrupts from the controller.
702 */
703void
704twe_disable_interrupts(struct twe_softc *sc)
705{
706    TWE_CONTROL(sc, TWE_CONTROL_DISABLE_INTERRUPTS);
707    sc->twe_state &= ~TWE_STATE_INTEN;
708}
709
710/********************************************************************************
711 ********************************************************************************
712                                                               Command Submission
713 ********************************************************************************
714 ********************************************************************************/
715
716/********************************************************************************
717 * Read integer parameter table entries.
718 */
719static int
720twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result)
721{
722    TWE_Param	*param;
723
724    if ((param = twe_get_param(sc, table_id, param_id, 1, NULL)) == NULL)
725	return(ENOENT);
726    *result = *(u_int8_t *)param->data;
727    free(param, M_DEVBUF);
728    return(0);
729}
730
731static int
732twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result)
733{
734    TWE_Param	*param;
735
736    if ((param = twe_get_param(sc, table_id, param_id, 2, NULL)) == NULL)
737	return(ENOENT);
738    *result = *(u_int16_t *)param->data;
739    free(param, M_DEVBUF);
740    return(0);
741}
742
743static int
744twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result)
745{
746    TWE_Param	*param;
747
748    if ((param = twe_get_param(sc, table_id, param_id, 4, NULL)) == NULL)
749	return(ENOENT);
750    *result = *(u_int32_t *)param->data;
751    free(param, M_DEVBUF);
752    return(0);
753}
754
755/********************************************************************************
756 * Perform a TWE_OP_GET_PARAM command.  If a callback function is provided, it
757 * will be called with the command when it's completed.  If no callback is
758 * provided, we will wait for the command to complete and then return just the data.
759 * The caller is responsible for freeing the data when done with it.
760 */
761static void *
762twe_get_param(struct twe_softc *sc, int table_id, int param_id, size_t param_size,
763	      void (* func)(struct twe_request *tr))
764{
765    struct twe_request	*tr;
766    TWE_Command		*cmd;
767    TWE_Param		*param;
768    int			error;
769
770    debug_called(4);
771
772    TWE_IO_ASSERT_LOCKED(sc);
773    tr = NULL;
774    param = NULL;
775
776    /* get a command */
777    if (twe_get_request(sc, &tr))
778	goto err;
779
780    /* get a buffer */
781    if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
782	goto err;
783    tr->tr_data = param;
784    tr->tr_length = TWE_SECTOR_SIZE;
785    tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
786
787    /* build the command for the controller */
788    cmd = TWE_FIND_COMMAND(tr);
789    cmd->param.opcode = TWE_OP_GET_PARAM;
790    cmd->param.size = 2;
791    cmd->param.unit = 0;
792    cmd->param.param_count = 1;
793
794    /* fill in the outbound parameter data */
795    param->table_id = table_id;
796    param->parameter_id = param_id;
797    param->parameter_size_bytes = param_size;
798
799    /* submit the command and either wait or let the callback handle it */
800    if (func == NULL) {
801	/* XXX could use twe_wait_request here if interrupts were enabled? */
802	error = twe_immediate_request(tr, 1 /* usetmp */);
803	if (error == 0) {
804	    if (twe_report_request(tr))
805		goto err;
806	} else {
807	    goto err;
808	}
809	twe_release_request(tr);
810	return(param);
811    } else {
812	tr->tr_complete = func;
813	error = twe_map_request(tr);
814	if ((error == 0) || (error == EBUSY))
815	    return(func);
816    }
817
818    /* something failed */
819err:
820    debug(1, "failed");
821    if (tr != NULL)
822	twe_release_request(tr);
823    if (param != NULL)
824	free(param, M_DEVBUF);
825    return(NULL);
826}
827
828/********************************************************************************
829 * Set integer parameter table entries.
830 */
831#ifdef TWE_SHUTDOWN_NOTIFICATION
832static int
833twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value)
834{
835    return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
836}
837#endif
838
839#if 0
840static int
841twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value)
842{
843    return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
844}
845
846static int
847twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value)
848{
849    return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
850}
851#endif
852
853/********************************************************************************
854 * Perform a TWE_OP_SET_PARAM command, returns nonzero on error.
855 */
856static int
857twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size, void *data)
858{
859    struct twe_request	*tr;
860    TWE_Command		*cmd;
861    TWE_Param		*param;
862    int			error;
863
864    debug_called(4);
865
866    TWE_IO_ASSERT_LOCKED(sc);
867    tr = NULL;
868    param = NULL;
869    error = ENOMEM;
870
871    /* get a command */
872    if (twe_get_request(sc, &tr))
873	goto out;
874
875    /* get a buffer */
876    if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
877	goto out;
878    tr->tr_data = param;
879    tr->tr_length = TWE_SECTOR_SIZE;
880    tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
881
882    /* build the command for the controller */
883    cmd = TWE_FIND_COMMAND(tr);
884    cmd->param.opcode = TWE_OP_SET_PARAM;
885    cmd->param.size = 2;
886    cmd->param.unit = 0;
887    cmd->param.param_count = 1;
888
889    /* fill in the outbound parameter data */
890    param->table_id = table_id;
891    param->parameter_id = param_id;
892    param->parameter_size_bytes = param_size;
893    bcopy(data, param->data, param_size);
894
895    /* XXX could use twe_wait_request here if interrupts were enabled? */
896    error = twe_immediate_request(tr, 1 /* usetmp */);
897    if (error == 0) {
898	if (twe_report_request(tr))
899	    error = EIO;
900    }
901
902out:
903    if (tr != NULL)
904	twe_release_request(tr);
905    if (param != NULL)
906	free(param, M_DEVBUF);
907    return(error);
908}
909
910/********************************************************************************
911 * Perform a TWE_OP_INIT_CONNECTION command, returns nonzero on error.
912 *
913 * Typically called with interrupts disabled.
914 */
915static int
916twe_init_connection(struct twe_softc *sc, int mode)
917{
918    struct twe_request	*tr;
919    TWE_Command		*cmd;
920    int			error;
921
922    debug_called(4);
923
924    TWE_IO_ASSERT_LOCKED(sc);
925
926    /* get a command */
927    if (twe_get_request(sc, &tr))
928	return(0);
929
930    /* build the command */
931    cmd = TWE_FIND_COMMAND(tr);
932    cmd->initconnection.opcode = TWE_OP_INIT_CONNECTION;
933    cmd->initconnection.size = 3;
934    cmd->initconnection.host_id = 0;
935    cmd->initconnection.message_credits = mode;
936    cmd->initconnection.response_queue_pointer = 0;
937
938    /* submit the command */
939    error = twe_immediate_request(tr, 0 /* usetmp */);
940    twe_release_request(tr);
941
942    if (mode == TWE_INIT_MESSAGE_CREDITS)
943	sc->twe_host_id = cmd->initconnection.host_id;
944    return(error);
945}
946
947/********************************************************************************
948 * Start the command (tr) and sleep waiting for it to complete.
949 *
950 * Successfully completed commands are dequeued.
951 */
952static int
953twe_wait_request(struct twe_request *tr)
954{
955
956    debug_called(4);
957
958    TWE_IO_ASSERT_LOCKED(tr->tr_sc);
959    tr->tr_flags |= TWE_CMD_SLEEPER;
960    tr->tr_status = TWE_CMD_BUSY;
961    twe_enqueue_ready(tr);
962    twe_startio(tr->tr_sc);
963    while (tr->tr_status == TWE_CMD_BUSY)
964	mtx_sleep(tr, &tr->tr_sc->twe_io_lock, PRIBIO, "twewait", 0);
965
966    return(tr->tr_status != TWE_CMD_COMPLETE);
967}
968
969/********************************************************************************
970 * Start the command (tr) and busy-wait for it to complete.
971 * This should only be used when interrupts are actually disabled (although it
972 * will work if they are not).
973 */
974static int
975twe_immediate_request(struct twe_request *tr, int usetmp)
976{
977    struct twe_softc *sc;
978    int		error;
979    int		count = 0;
980
981    debug_called(4);
982
983    sc = tr->tr_sc;
984
985    if (usetmp && (tr->tr_data != NULL)) {
986	tr->tr_flags |= TWE_CMD_IMMEDIATE;
987	if (tr->tr_length > MAXBSIZE)
988	    return (EINVAL);
989	bcopy(tr->tr_data, sc->twe_immediate, tr->tr_length);
990    }
991    tr->tr_status = TWE_CMD_BUSY;
992    if ((error = twe_map_request(tr)) != 0)
993	if (error != EBUSY)
994	    return(error);
995
996    /* Wait up to 5 seconds for the command to complete */
997    while ((count++ < 5000) && (tr->tr_status == TWE_CMD_BUSY)){
998	DELAY(1000);
999	twe_done(sc, 1);
1000    }
1001    if (usetmp && (tr->tr_data != NULL))
1002	bcopy(sc->twe_immediate, tr->tr_data, tr->tr_length);
1003
1004    return(tr->tr_status != TWE_CMD_COMPLETE);
1005}
1006
1007/********************************************************************************
1008 * Handle completion of an I/O command.
1009 */
1010static void
1011twe_completeio(struct twe_request *tr)
1012{
1013    TWE_Command		*cmd = TWE_FIND_COMMAND(tr);
1014    struct twe_softc	*sc = tr->tr_sc;
1015    twe_bio		*bp = (twe_bio *)tr->tr_private;
1016
1017    debug_called(4);
1018
1019    if (tr->tr_status == TWE_CMD_COMPLETE) {
1020
1021	if (cmd->generic.status)
1022	    if (twe_report_request(tr))
1023		TWE_BIO_SET_ERROR(bp, EIO);
1024
1025    } else {
1026	twe_panic(sc, "twe_completeio on incomplete command");
1027    }
1028    tr->tr_private = NULL;
1029    twed_intr(bp);
1030    twe_release_request(tr);
1031}
1032
1033/********************************************************************************
1034 * Reset the controller and pull all the active commands back onto the ready
1035 * queue.  Used to restart a controller that's exhibiting bad behaviour.
1036 */
1037static void
1038twe_reset(struct twe_softc *sc)
1039{
1040    struct twe_request	*tr;
1041    int			i;
1042
1043    /*
1044     * Sleep for a short period to allow AENs to be signalled.
1045     */
1046    mtx_sleep(sc, &sc->twe_io_lock, PRIBIO, "twereset", hz);
1047
1048    /*
1049     * Disable interrupts from the controller, and mask any accidental entry
1050     * into our interrupt handler.
1051     */
1052    twe_printf(sc, "controller reset in progress...\n");
1053    twe_disable_interrupts(sc);
1054
1055    /*
1056     * Try to soft-reset the controller.
1057     */
1058    for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
1059
1060	if (i > 0)
1061	    twe_printf(sc, "reset %d failed, trying again\n", i);
1062
1063	if (!twe_soft_reset(sc))
1064	    break;			/* reset process complete */
1065    }
1066    /* did we give up? */
1067    if (i >= TWE_MAX_RESET_TRIES) {
1068	twe_printf(sc, "can't reset controller, giving up\n");
1069	goto out;
1070    }
1071
1072    /*
1073     * Move all of the commands that were busy back to the ready queue.
1074     */
1075    i = 0;
1076    while ((tr = twe_dequeue_busy(sc)) != NULL) {
1077	twe_enqueue_ready(tr);
1078	i++;
1079    }
1080
1081    /*
1082     * Kick the controller to start things going again, then re-enable interrupts.
1083     */
1084    twe_startio(sc);
1085    twe_printf(sc, "controller reset done, %d commands restarted\n", i);
1086
1087out:
1088    twe_enable_interrupts(sc);
1089}
1090
1091/********************************************************************************
1092 ********************************************************************************
1093                                                        Command I/O to Controller
1094 ********************************************************************************
1095 ********************************************************************************/
1096
1097/********************************************************************************
1098 * Try to deliver (tr) to the controller.
1099 *
1100 * Can be called at any interrupt level, with or without interrupts enabled.
1101 */
1102int
1103twe_start(struct twe_request *tr)
1104{
1105    struct twe_softc	*sc = tr->tr_sc;
1106    TWE_Command		*cmd;
1107    int			i;
1108    u_int32_t		status_reg;
1109
1110    debug_called(4);
1111
1112    if (!dumping)
1113	TWE_IO_ASSERT_LOCKED(sc);
1114
1115    /* mark the command as currently being processed */
1116    tr->tr_status = TWE_CMD_BUSY;
1117    cmd = TWE_FIND_COMMAND(tr);
1118
1119    /*
1120     * Spin briefly waiting for the controller to come ready
1121     *
1122     * XXX it might be more efficient to return EBUSY immediately
1123     *     and let the command be rescheduled.
1124     */
1125    for (i = 100000; (i > 0); i--) {
1126
1127	/* check to see if we can post a command */
1128	status_reg = TWE_STATUS(sc);
1129	twe_check_bits(sc, status_reg);
1130
1131	if (!(status_reg & TWE_STATUS_COMMAND_QUEUE_FULL)) {
1132	    twe_enqueue_busy(tr);
1133
1134	    TWE_COMMAND_QUEUE(sc, TWE_FIND_COMMANDPHYS(tr));
1135
1136	    /* move command to work queue */
1137#ifdef TWE_DEBUG
1138	    if (tr->tr_complete != NULL) {
1139		debug(3, "queued request %d with callback %p", cmd->generic.request_id, tr->tr_complete);
1140	    } else if (tr->tr_flags & TWE_CMD_SLEEPER) {
1141		debug(3, "queued request %d with wait channel %p", cmd->generic.request_id, tr);
1142	    } else {
1143		debug(3, "queued request %d for polling caller", cmd->generic.request_id);
1144	    }
1145#endif
1146	    return(0);
1147	} else if (!(status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY) && i > 1)
1148	    twe_done(sc, 0);
1149    }
1150
1151    /*
1152     * We couldn't get the controller to take the command; try submitting it again later.
1153     * This should only happen if something is wrong with the controller, or if we have
1154     * overestimated the number of commands it can accept.  (Should we actually reject
1155     * the command at this point?)
1156     */
1157    return(EBUSY);
1158}
1159
1160/********************************************************************************
1161 * Poll the controller (sc) for completed commands.
1162 *
1163 * Can be called at any interrupt level, with or without interrupts enabled.
1164 */
1165static void
1166twe_done(struct twe_softc *sc, int startio)
1167{
1168    TWE_Response_Queue	rq;
1169    TWE_Command		*cmd;
1170    struct twe_request	*tr;
1171    int			found;
1172    u_int32_t		status_reg;
1173
1174    debug_called(5);
1175
1176    /* loop collecting completed commands */
1177    found = 0;
1178    for (;;) {
1179	status_reg = TWE_STATUS(sc);
1180	twe_check_bits(sc, status_reg);		/* XXX should this fail? */
1181
1182	if (!(status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)) {
1183	    found = 1;
1184	    rq = TWE_RESPONSE_QUEUE(sc);
1185	    tr = sc->twe_lookup[rq.u.response_id];	/* find command */
1186	    cmd = TWE_FIND_COMMAND(tr);
1187	    if (tr->tr_status != TWE_CMD_BUSY)
1188		twe_printf(sc, "completion event for nonbusy command\n");
1189	    tr->tr_status = TWE_CMD_COMPLETE;
1190	    debug(3, "completed request id %d with status %d",
1191		  cmd->generic.request_id, cmd->generic.status);
1192	    /* move to completed queue */
1193	    twe_remove_busy(tr);
1194	    twe_enqueue_complete(tr);
1195	    sc->twe_state &= ~TWE_STATE_CTLR_BUSY;
1196	} else {
1197	    break;					/* no response ready */
1198	}
1199    }
1200
1201    /* if we've completed any commands, try posting some more */
1202    if (found && startio)
1203	twe_startio(sc);
1204
1205    /* handle completion and timeouts */
1206    twe_complete(sc);		/* XXX use deferred completion? */
1207}
1208
1209/********************************************************************************
1210 * Perform post-completion processing for commands on (sc).
1211 *
1212 * This is split from twe_done as it can be safely deferred and run at a lower
1213 * priority level should facilities for such a thing become available.
1214 */
1215static void
1216twe_complete(struct twe_softc *sc)
1217{
1218    struct twe_request	*tr;
1219
1220    debug_called(5);
1221
1222    /*
1223     * Pull commands off the completed list, dispatch them appropriately
1224     */
1225    while ((tr = twe_dequeue_complete(sc)) != NULL) {
1226	/* unmap the command's data buffer */
1227	twe_unmap_request(tr);
1228
1229	/* dispatch to suit command originator */
1230	if (tr->tr_complete != NULL) {		/* completion callback */
1231	    debug(2, "call completion handler %p", tr->tr_complete);
1232	    tr->tr_complete(tr);
1233
1234	} else if (tr->tr_flags & TWE_CMD_SLEEPER) {	/* caller is asleep waiting */
1235	    debug(2, "wake up command owner on %p", tr);
1236	    wakeup_one(tr);
1237
1238	} else {					/* caller is polling command */
1239	    debug(2, "command left for owner");
1240	}
1241    }
1242}
1243
1244/********************************************************************************
1245 * Wait for (status) to be set in the controller status register for up to
1246 * (timeout) seconds.  Returns 0 if found, nonzero if we time out.
1247 *
1248 * Note: this busy-waits, rather than sleeping, since we may be called with
1249 * eg. clock interrupts masked.
1250 */
1251static int
1252twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout)
1253{
1254    time_t	expiry;
1255    u_int32_t	status_reg;
1256
1257    debug_called(4);
1258
1259    expiry = time_second + timeout;
1260
1261    do {
1262	status_reg = TWE_STATUS(sc);
1263	if (status_reg & status)	/* got the required bit(s)? */
1264	    return(0);
1265	DELAY(100000);
1266    } while (time_second <= expiry);
1267
1268    return(1);
1269}
1270
1271/********************************************************************************
1272 * Drain the response queue, which may contain responses to commands we know
1273 * nothing about.
1274 */
1275static int
1276twe_drain_response_queue(struct twe_softc *sc)
1277{
1278    TWE_Response_Queue	rq;
1279    u_int32_t		status_reg;
1280
1281    debug_called(4);
1282
1283    for (;;) {				/* XXX give up eventually? */
1284	status_reg = TWE_STATUS(sc);
1285	if (twe_check_bits(sc, status_reg))
1286	    return(1);
1287	if (status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)
1288	    return(0);
1289	rq = TWE_RESPONSE_QUEUE(sc);
1290    }
1291}
1292
1293/********************************************************************************
1294 * Soft-reset the controller
1295 */
1296static int
1297twe_soft_reset(struct twe_softc *sc)
1298{
1299    u_int32_t		status_reg;
1300
1301    debug_called(2);
1302
1303    TWE_IO_ASSERT_LOCKED(sc);
1304    TWE_SOFT_RESET(sc);
1305
1306    if (twe_wait_status(sc, TWE_STATUS_ATTENTION_INTERRUPT, 30)) {
1307	twe_printf(sc, "no attention interrupt\n");
1308	return(1);
1309    }
1310    TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1311    if (twe_drain_aen_queue(sc)) {
1312	twe_printf(sc, "can't drain AEN queue\n");
1313	return(1);
1314    }
1315    if (twe_find_aen(sc, TWE_AEN_SOFT_RESET)) {
1316	twe_printf(sc, "reset not reported\n");
1317	return(1);
1318    }
1319    status_reg = TWE_STATUS(sc);
1320    if (TWE_STATUS_ERRORS(status_reg) || twe_check_bits(sc, status_reg)) {
1321	twe_printf(sc, "controller errors detected\n");
1322	return(1);
1323    }
1324    if (twe_drain_response_queue(sc)) {
1325	twe_printf(sc, "can't drain response queue\n");
1326	return(1);
1327    }
1328    return(0);
1329}
1330
1331/********************************************************************************
1332 ********************************************************************************
1333                                                               Interrupt Handling
1334 ********************************************************************************
1335 ********************************************************************************/
1336
1337/********************************************************************************
1338 * Host interrupt.
1339 *
1340 * XXX what does this mean?
1341 */
1342static void
1343twe_host_intr(struct twe_softc *sc)
1344{
1345    debug_called(4);
1346
1347    twe_printf(sc, "host interrupt\n");
1348    TWE_CONTROL(sc, TWE_CONTROL_CLEAR_HOST_INTERRUPT);
1349}
1350
1351/********************************************************************************
1352 * Attention interrupt.
1353 *
1354 * Signalled when the controller has one or more AENs for us.
1355 */
1356static void
1357twe_attention_intr(struct twe_softc *sc)
1358{
1359    debug_called(4);
1360
1361    /* instigate a poll for AENs */
1362    if (twe_fetch_aen(sc)) {
1363	twe_printf(sc, "error polling for signalled AEN\n");
1364    } else {
1365	TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1366    }
1367}
1368
1369/********************************************************************************
1370 * Command interrupt.
1371 *
1372 * Signalled when the controller can handle more commands.
1373 */
1374static void
1375twe_command_intr(struct twe_softc *sc)
1376{
1377    debug_called(4);
1378
1379    /*
1380     * We don't use this, rather we try to submit commands when we receive
1381     * them, and when other commands have completed.  Mask it so we don't get
1382     * another one.
1383     */
1384    TWE_CONTROL(sc, TWE_CONTROL_MASK_COMMAND_INTERRUPT);
1385}
1386
1387/********************************************************************************
1388 ********************************************************************************
1389                                                      Asynchronous Event Handling
1390 ********************************************************************************
1391 ********************************************************************************/
1392
1393/********************************************************************************
1394 * Request an AEN from the controller.
1395 */
1396static int
1397twe_fetch_aen(struct twe_softc *sc)
1398{
1399
1400    debug_called(4);
1401
1402    if ((twe_get_param(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2, twe_handle_aen)) == NULL)
1403	return(EIO);
1404    return(0);
1405}
1406
1407/********************************************************************************
1408 * Handle an AEN returned by the controller.
1409 */
1410static void
1411twe_handle_aen(struct twe_request *tr)
1412{
1413    struct twe_softc	*sc = tr->tr_sc;
1414    TWE_Param		*param;
1415    u_int16_t		aen;
1416
1417    debug_called(4);
1418
1419    /* XXX check for command success somehow? */
1420
1421    param = (TWE_Param *)tr->tr_data;
1422    aen = *(u_int16_t *)(param->data);
1423
1424    free(tr->tr_data, M_DEVBUF);
1425    twe_release_request(tr);
1426    twe_enqueue_aen(sc, aen);
1427
1428    /* XXX poll for more AENs? */
1429}
1430
1431/********************************************************************************
1432 * Pull AENs out of the controller and park them in the queue, in a context where
1433 * interrupts aren't active.  Return nonzero if we encounter any errors in the
1434 * process of obtaining all the available AENs.
1435 */
1436static int
1437twe_drain_aen_queue(struct twe_softc *sc)
1438{
1439    u_int16_t	aen;
1440
1441    TWE_IO_ASSERT_LOCKED(sc);
1442    for (;;) {
1443	if (twe_get_param_2(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, &aen))
1444	    return(1);
1445	if (aen == TWE_AEN_QUEUE_EMPTY)
1446	    return(0);
1447	twe_enqueue_aen(sc, aen);
1448    }
1449}
1450
1451/********************************************************************************
1452 * Push an AEN that we've received onto the queue.
1453 *
1454 * Note that we have to lock this against reentrance, since it may be called
1455 * from both interrupt and non-interrupt context.
1456 *
1457 * If someone is waiting for the AEN we have, wake them up.
1458 */
1459static void
1460twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen)
1461{
1462    char	*msg;
1463    int		next, nextnext;
1464
1465    debug_called(4);
1466
1467    TWE_IO_ASSERT_LOCKED(sc);
1468    if ((msg = twe_format_aen(sc, aen)) != NULL)
1469	twe_printf(sc, "AEN: <%s>\n", msg);
1470
1471    /* enqueue the AEN */
1472    next = ((sc->twe_aen_head + 1) % TWE_Q_LENGTH);
1473    nextnext = ((sc->twe_aen_head + 2) % TWE_Q_LENGTH);
1474
1475    /* check to see if this is the last free slot, and subvert the AEN if it is */
1476    if (nextnext == sc->twe_aen_tail)
1477	aen = TWE_AEN_QUEUE_FULL;
1478
1479    /* look to see if there's room for this AEN */
1480    if (next != sc->twe_aen_tail) {
1481	sc->twe_aen_queue[sc->twe_aen_head] = aen;
1482	sc->twe_aen_head = next;
1483    }
1484
1485    /* wake up anyone asleep on the queue */
1486    wakeup(&sc->twe_aen_queue);
1487
1488    /* anyone looking for this AEN? */
1489    if (sc->twe_wait_aen == aen) {
1490	sc->twe_wait_aen = -1;
1491	wakeup(&sc->twe_wait_aen);
1492    }
1493}
1494
1495/********************************************************************************
1496 * Pop an AEN off the queue, or return -1 if there are none left.
1497 *
1498 * We are more or less interrupt-safe, so don't block interrupts.
1499 */
1500static u_int16_t
1501twe_dequeue_aen(struct twe_softc *sc)
1502{
1503    u_int16_t	result;
1504
1505    debug_called(4);
1506
1507    TWE_IO_ASSERT_LOCKED(sc);
1508    if (sc->twe_aen_tail == sc->twe_aen_head) {
1509	result = TWE_AEN_QUEUE_EMPTY;
1510    } else {
1511	result = sc->twe_aen_queue[sc->twe_aen_tail];
1512	sc->twe_aen_tail = ((sc->twe_aen_tail + 1) % TWE_Q_LENGTH);
1513    }
1514    return(result);
1515}
1516
1517/********************************************************************************
1518 * Check to see if the requested AEN is in the queue.
1519 *
1520 * XXX we could probably avoid masking interrupts here
1521 */
1522static int
1523twe_find_aen(struct twe_softc *sc, u_int16_t aen)
1524{
1525    int		i, missing;
1526
1527    missing = 1;
1528    for (i = sc->twe_aen_tail; (i != sc->twe_aen_head) && missing; i = (i + 1) % TWE_Q_LENGTH) {
1529	if (sc->twe_aen_queue[i] == aen)
1530	    missing = 0;
1531    }
1532    return(missing);
1533}
1534
1535
1536#if 0	/* currently unused */
1537/********************************************************************************
1538 * Sleep waiting for at least (timeout) seconds until we see (aen) as
1539 * requested.  Returns nonzero on timeout or failure.
1540 *
1541 * XXX: this should not be used in cases where there may be more than one sleeper
1542 *      without a mechanism for registering multiple sleepers.
1543 */
1544static int
1545twe_wait_aen(struct twe_softc *sc, int aen, int timeout)
1546{
1547    time_t	expiry;
1548    int		found;
1549
1550    debug_called(4);
1551
1552    expiry = time_second + timeout;
1553    found = 0;
1554
1555    sc->twe_wait_aen = aen;
1556    do {
1557	twe_fetch_aen(sc);
1558	mtx_sleep(&sc->twe_wait_aen, &sc->twe_io_lock, PZERO, "twewaen", hz);
1559	if (sc->twe_wait_aen == -1)
1560	    found = 1;
1561    } while ((time_second <= expiry) && !found);
1562    return(!found);
1563}
1564#endif
1565
1566/********************************************************************************
1567 ********************************************************************************
1568                                                        Command Buffer Management
1569 ********************************************************************************
1570 ********************************************************************************/
1571
1572/********************************************************************************
1573 * Get a new command buffer.
1574 *
1575 * This will return NULL if all command buffers are in use.
1576 */
1577static int
1578twe_get_request(struct twe_softc *sc, struct twe_request **tr)
1579{
1580    TWE_Command		*cmd;
1581    debug_called(4);
1582
1583    if (!dumping)
1584	TWE_IO_ASSERT_LOCKED(sc);
1585
1586    /* try to reuse an old buffer */
1587    *tr = twe_dequeue_free(sc);
1588
1589    /* initialise some fields to their defaults */
1590    if (*tr != NULL) {
1591	cmd = TWE_FIND_COMMAND(*tr);
1592	(*tr)->tr_data = NULL;
1593	(*tr)->tr_private = NULL;
1594	(*tr)->tr_status = TWE_CMD_SETUP;		/* command is in setup phase */
1595	(*tr)->tr_flags = 0;
1596	(*tr)->tr_complete = NULL;
1597	cmd->generic.status = 0;			/* before submission to controller */
1598	cmd->generic.flags = 0;				/* not used */
1599    }
1600    return(*tr == NULL);
1601}
1602
1603/********************************************************************************
1604 * Release a command buffer for reuse.
1605 *
1606 */
1607static void
1608twe_release_request(struct twe_request *tr)
1609{
1610    debug_called(4);
1611
1612    if (!dumping)
1613	TWE_IO_ASSERT_LOCKED(tr->tr_sc);
1614    if (tr->tr_private != NULL)
1615	twe_panic(tr->tr_sc, "tr_private != NULL");
1616    twe_enqueue_free(tr);
1617}
1618
1619/********************************************************************************
1620 ********************************************************************************
1621                                                                        Debugging
1622 ********************************************************************************
1623 ********************************************************************************/
1624
1625/********************************************************************************
1626 * Print some information about the controller
1627 */
1628void
1629twe_describe_controller(struct twe_softc *sc)
1630{
1631    TWE_Param		*p[6];
1632    u_int8_t		ports;
1633    u_int32_t		size;
1634    int			i;
1635
1636    debug_called(2);
1637
1638    TWE_IO_LOCK(sc);
1639
1640    /* get the port count */
1641    twe_get_param_1(sc, TWE_PARAM_CONTROLLER, TWE_PARAM_CONTROLLER_PortCount, &ports);
1642
1643    /* get version strings */
1644    p[0] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_FW,   16, NULL);
1645    p[1] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_BIOS, 16, NULL);
1646    if (p[0] && p[1])
1647	 twe_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n", ports, p[0]->data, p[1]->data);
1648
1649    if (bootverbose) {
1650	p[2] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_Mon,  16, NULL);
1651	p[3] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCB,  8, NULL);
1652	p[4] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_ATA,  8, NULL);
1653	p[5] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCI,  8, NULL);
1654
1655	if (p[2] && p[3] && p[4] && p[5])
1656	    twe_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n", p[2]->data, p[3]->data,
1657		p[4]->data, p[5]->data);
1658	if (p[2])
1659	    free(p[2], M_DEVBUF);
1660	if (p[3])
1661	    free(p[3], M_DEVBUF);
1662	if (p[4])
1663	    free(p[4], M_DEVBUF);
1664	if (p[5])
1665	    free(p[5], M_DEVBUF);
1666    }
1667    if (p[0])
1668	free(p[0], M_DEVBUF);
1669    if (p[1])
1670	free(p[1], M_DEVBUF);
1671
1672    /* print attached drives */
1673    if (bootverbose) {
1674	p[0] = twe_get_param(sc, TWE_PARAM_DRIVESUMMARY, TWE_PARAM_DRIVESUMMARY_Status, 16, NULL);
1675	for (i = 0; i < ports; i++) {
1676	    if (p[0]->data[i] != TWE_PARAM_DRIVESTATUS_Present)
1677		continue;
1678	    twe_get_param_4(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Size, &size);
1679	    p[1] = twe_get_param(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Model, 40, NULL);
1680	    if (p[1] != NULL) {
1681		twe_printf(sc, "port %d: %.40s %dMB\n", i, p[1]->data, size / 2048);
1682		free(p[1], M_DEVBUF);
1683	    } else {
1684		twe_printf(sc, "port %d, drive status unavailable\n", i);
1685	    }
1686	}
1687	if (p[0])
1688	    free(p[0], M_DEVBUF);
1689    }
1690    TWE_IO_UNLOCK(sc);
1691}
1692
1693/********************************************************************************
1694 * Look up a text description of a numeric code and return a pointer to same.
1695 */
1696char *
1697twe_describe_code(struct twe_code_lookup *table, u_int32_t code)
1698{
1699    int         i;
1700
1701    for (i = 0; table[i].string != NULL; i++)
1702	if (table[i].code == code)
1703	    return(table[i].string);
1704    return(table[i+1].string);
1705}
1706
1707/********************************************************************************
1708 * Complain if the status bits aren't what we're expecting.
1709 *
1710 * Rate-limit the complaints to at most one of each every five seconds, but
1711 * always return the correct status.
1712 */
1713static int
1714twe_check_bits(struct twe_softc *sc, u_int32_t status_reg)
1715{
1716    int			result;
1717    static time_t	lastwarn[2] = {0, 0};
1718
1719    /*
1720     * This can be a little problematic, as twe_panic may call twe_reset if
1721     * TWE_DEBUG is not set, which will call us again as part of the soft reset.
1722     */
1723    if ((status_reg & TWE_STATUS_PANIC_BITS) != 0) {
1724	twe_printf(sc, "FATAL STATUS BIT(S) %b\n", status_reg & TWE_STATUS_PANIC_BITS,
1725		   TWE_STATUS_BITS_DESCRIPTION);
1726	twe_panic(sc, "fatal status bits");
1727    }
1728
1729    result = 0;
1730    if ((status_reg & TWE_STATUS_EXPECTED_BITS) != TWE_STATUS_EXPECTED_BITS) {
1731	if (time_second > (lastwarn[0] + 5)) {
1732	    twe_printf(sc, "missing expected status bit(s) %b\n", ~status_reg & TWE_STATUS_EXPECTED_BITS,
1733		       TWE_STATUS_BITS_DESCRIPTION);
1734	    lastwarn[0] = time_second;
1735	}
1736	result = 1;
1737    }
1738
1739    if ((status_reg & TWE_STATUS_UNEXPECTED_BITS) != 0) {
1740	if (time_second > (lastwarn[1] + 5)) {
1741	    twe_printf(sc, "unexpected status bit(s) %b\n", status_reg & TWE_STATUS_UNEXPECTED_BITS,
1742		       TWE_STATUS_BITS_DESCRIPTION);
1743	    lastwarn[1] = time_second;
1744	}
1745	result = 1;
1746	if (status_reg & TWE_STATUS_PCI_PARITY_ERROR) {
1747	    twe_printf(sc, "PCI parity error: Reseat card, move card or buggy device present.\n");
1748	    twe_clear_pci_parity_error(sc);
1749	}
1750	if (status_reg & TWE_STATUS_PCI_ABORT) {
1751	    twe_printf(sc, "PCI abort, clearing.\n");
1752	    twe_clear_pci_abort(sc);
1753	}
1754    }
1755
1756    return(result);
1757}
1758
1759/********************************************************************************
1760 * Return a string describing (aen).
1761 *
1762 * The low 8 bits of the aen are the code, the high 8 bits give the unit number
1763 * where an AEN is specific to a unit.
1764 *
1765 * Note that we could expand this routine to handle eg. up/downgrading the status
1766 * of a drive if we had some idea of what the drive's initial status was.
1767 */
1768
1769static char *
1770twe_format_aen(struct twe_softc *sc, u_int16_t aen)
1771{
1772    device_t	child;
1773    char	*code, *msg;
1774
1775    code = twe_describe_code(twe_table_aen, TWE_AEN_CODE(aen));
1776    msg = code + 2;
1777
1778    switch (*code) {
1779    case 'q':
1780	if (!bootverbose)
1781	    return(NULL);
1782	/* FALLTHROUGH */
1783    case 'a':
1784	return(msg);
1785
1786    case 'c':
1787	if ((child = sc->twe_drive[TWE_AEN_UNIT(aen)].td_disk) != NULL) {
1788	    snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf), "twed%d: %s",
1789		device_get_unit(child), msg);
1790	} else {
1791	    snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf),
1792		"twe%d: %s for unknown unit %d", device_get_unit(sc->twe_dev),
1793		msg, TWE_AEN_UNIT(aen));
1794	}
1795	return(sc->twe_aen_buf);
1796
1797    case 'p':
1798	snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf),
1799	    "twe%d: port %d: %s", device_get_unit(sc->twe_dev),
1800	    TWE_AEN_UNIT(aen), msg);
1801	return(sc->twe_aen_buf);
1802
1803
1804    case 'x':
1805    default:
1806	break;
1807    }
1808    snprintf(sc->twe_aen_buf, sizeof(sc->twe_aen_buf), "unknown AEN 0x%x", aen);
1809    return(sc->twe_aen_buf);
1810}
1811
1812/********************************************************************************
1813 * Print a diagnostic if the status of the command warrants it, and return
1814 * either zero (command was ok) or nonzero (command failed).
1815 */
1816static int
1817twe_report_request(struct twe_request *tr)
1818{
1819    struct twe_softc	*sc = tr->tr_sc;
1820    TWE_Command		*cmd = TWE_FIND_COMMAND(tr);
1821    int			result = 0;
1822
1823    /*
1824     * Check the command status value and handle accordingly.
1825     */
1826    if (cmd->generic.status == TWE_STATUS_RESET) {
1827	/*
1828	 * The status code 0xff requests a controller reset.
1829	 */
1830	twe_printf(sc, "command returned with controller reset request\n");
1831	twe_reset(sc);
1832	result = 1;
1833    } else if (cmd->generic.status > TWE_STATUS_FATAL) {
1834	/*
1835	 * Fatal errors that don't require controller reset.
1836	 *
1837	 * We know a few special flags values.
1838	 */
1839	switch (cmd->generic.flags) {
1840	case 0x1b:
1841	    device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1842			  "drive timeout");
1843	    break;
1844	case 0x51:
1845	    device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1846			  "unrecoverable drive error");
1847	    break;
1848	default:
1849	    device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1850			  "controller error - %s (flags = 0x%x)\n",
1851			  twe_describe_code(twe_table_status, cmd->generic.status),
1852			  cmd->generic.flags);
1853	    result = 1;
1854	}
1855    } else if (cmd->generic.status > TWE_STATUS_WARNING) {
1856	/*
1857	 * Warning level status.
1858	 */
1859	device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1860		      "warning - %s (flags = 0x%x)\n",
1861		      twe_describe_code(twe_table_status, cmd->generic.status),
1862		      cmd->generic.flags);
1863    } else if (cmd->generic.status > 0x40) {
1864	/*
1865	 * Info level status.
1866	 */
1867	device_printf(sc->twe_drive[cmd->generic.unit].td_disk,
1868		      "attention - %s (flags = 0x%x)\n",
1869		      twe_describe_code(twe_table_status, cmd->generic.status),
1870		      cmd->generic.flags);
1871    }
1872
1873    return(result);
1874}
1875
1876/********************************************************************************
1877 * Print some controller state to aid in debugging error/panic conditions
1878 */
1879void
1880twe_print_controller(struct twe_softc *sc)
1881{
1882    u_int32_t		status_reg;
1883
1884    status_reg = TWE_STATUS(sc);
1885    twe_printf(sc, "status   %b\n", status_reg, TWE_STATUS_BITS_DESCRIPTION);
1886    twe_printf(sc, "          current  max    min\n");
1887    twe_printf(sc, "free      %04d     %04d   %04d\n",
1888	sc->twe_qstat[TWEQ_FREE].q_length, sc->twe_qstat[TWEQ_FREE].q_max, sc->twe_qstat[TWEQ_FREE].q_min);
1889
1890    twe_printf(sc, "ready     %04d     %04d   %04d\n",
1891	sc->twe_qstat[TWEQ_READY].q_length, sc->twe_qstat[TWEQ_READY].q_max, sc->twe_qstat[TWEQ_READY].q_min);
1892
1893    twe_printf(sc, "busy      %04d     %04d   %04d\n",
1894	sc->twe_qstat[TWEQ_BUSY].q_length, sc->twe_qstat[TWEQ_BUSY].q_max, sc->twe_qstat[TWEQ_BUSY].q_min);
1895
1896    twe_printf(sc, "complete  %04d     %04d   %04d\n",
1897	sc->twe_qstat[TWEQ_COMPLETE].q_length, sc->twe_qstat[TWEQ_COMPLETE].q_max, sc->twe_qstat[TWEQ_COMPLETE].q_min);
1898
1899    twe_printf(sc, "bioq      %04d     %04d   %04d\n",
1900	sc->twe_qstat[TWEQ_BIO].q_length, sc->twe_qstat[TWEQ_BIO].q_max, sc->twe_qstat[TWEQ_BIO].q_min);
1901
1902    twe_printf(sc, "AEN queue head %d  tail %d\n", sc->twe_aen_head, sc->twe_aen_tail);
1903}
1904
1905static void
1906twe_panic(struct twe_softc *sc, char *reason)
1907{
1908    twe_print_controller(sc);
1909#ifdef TWE_DEBUG
1910    panic(reason);
1911#else
1912    twe_reset(sc);
1913#endif
1914}
1915
1916#if 0
1917/********************************************************************************
1918 * Print a request/command in human-readable format.
1919 */
1920static void
1921twe_print_request(struct twe_request *tr)
1922{
1923    struct twe_softc	*sc = tr->tr_sc;
1924    TWE_Command	*cmd = TWE_FIND_COMMAND(tr);
1925    int		i;
1926
1927    twe_printf(sc, "CMD: request_id %d  opcode <%s>  size %d  unit %d  host_id %d\n",
1928	       cmd->generic.request_id, twe_describe_code(twe_table_opcode, cmd->generic.opcode), cmd->generic.size,
1929	       cmd->generic.unit, cmd->generic.host_id);
1930    twe_printf(sc, " status %d  flags 0x%x  count %d  sgl_offset %d\n",
1931	       cmd->generic.status, cmd->generic.flags, cmd->generic.count, cmd->generic.sgl_offset);
1932
1933    switch(cmd->generic.opcode) {	/* XXX add more opcodes? */
1934    case TWE_OP_READ:
1935    case TWE_OP_WRITE:
1936	twe_printf(sc, " lba %d\n", cmd->io.lba);
1937	for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->io.sgl[i].length != 0); i++)
1938	    twe_printf(sc, "  %d: 0x%x/%d\n",
1939		       i, cmd->io.sgl[i].address, cmd->io.sgl[i].length);
1940	break;
1941
1942    case TWE_OP_GET_PARAM:
1943    case TWE_OP_SET_PARAM:
1944	for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->param.sgl[i].length != 0); i++)
1945	    twe_printf(sc, "  %d: 0x%x/%d\n",
1946		       i, cmd->param.sgl[i].address, cmd->param.sgl[i].length);
1947	break;
1948
1949    case TWE_OP_INIT_CONNECTION:
1950	twe_printf(sc, " response queue pointer 0x%x\n",
1951		   cmd->initconnection.response_queue_pointer);
1952	break;
1953
1954    default:
1955	break;
1956    }
1957    twe_printf(sc, " tr_command %p/0x%x  tr_data %p/0x%x,%d\n",
1958	       tr, TWE_FIND_COMMANDPHYS(tr), tr->tr_data, tr->tr_dataphys, tr->tr_length);
1959    twe_printf(sc, " tr_status %d  tr_flags 0x%x  tr_complete %p  tr_private %p\n",
1960	       tr->tr_status, tr->tr_flags, tr->tr_complete, tr->tr_private);
1961}
1962
1963#endif
1964