amr.c revision 65763
151974Smsmith/*-
265245Smsmith * Copyright (c) 1999,2000 Michael Smith
365245Smsmith * Copyright (c) 2000 BSDi
451974Smsmith * All rights reserved.
551974Smsmith *
651974Smsmith * Redistribution and use in source and binary forms, with or without
751974Smsmith * modification, are permitted provided that the following conditions
851974Smsmith * are met:
951974Smsmith * 1. Redistributions of source code must retain the above copyright
1051974Smsmith *    notice, this list of conditions and the following disclaimer.
1151974Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1251974Smsmith *    notice, this list of conditions and the following disclaimer in the
1351974Smsmith *    documentation and/or other materials provided with the distribution.
1451974Smsmith *
1551974Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1651974Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1751974Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1851974Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1951974Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2051974Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2151974Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2251974Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2351974Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2451974Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2551974Smsmith * SUCH DAMAGE.
2651974Smsmith *
2751974Smsmith *	$FreeBSD: head/sys/dev/amr/amr.c 65763 2000-09-11 23:19:13Z msmith $
2851974Smsmith */
2951974Smsmith
3051974Smsmith/*
3165245Smsmith * Driver for the AMI MegaRaid family of controllers.
3251974Smsmith */
3351974Smsmith
3451974Smsmith#include <sys/param.h>
3551974Smsmith#include <sys/systm.h>
3651974Smsmith#include <sys/malloc.h>
3751974Smsmith#include <sys/kernel.h>
3851974Smsmith
3965245Smsmith#include <dev/amr/amr_compat.h>
4051974Smsmith#include <sys/bus.h>
4151974Smsmith#include <sys/conf.h>
4251974Smsmith#include <sys/devicestat.h>
4351974Smsmith#include <sys/disk.h>
4465245Smsmith#include <sys/stat.h>
4551974Smsmith
4665245Smsmith#include <machine/bus_memio.h>
4765245Smsmith#include <machine/bus_pio.h>
4865245Smsmith#include <machine/bus.h>
4951974Smsmith#include <machine/resource.h>
5051974Smsmith#include <machine/clock.h>
5151974Smsmith#include <sys/rman.h>
5251974Smsmith
5365245Smsmith#include <pci/pcireg.h>
5465245Smsmith#include <pci/pcivar.h>
5565245Smsmith
5651974Smsmith#include <dev/amr/amrio.h>
5751974Smsmith#include <dev/amr/amrreg.h>
5851974Smsmith#include <dev/amr/amrvar.h>
5965245Smsmith#define AMR_DEFINE_TABLES
6065245Smsmith#include <dev/amr/amr_tables.h>
6151974Smsmith
6251974Smsmith#define AMR_CDEV_MAJOR	132
6351974Smsmith
6465245Smsmithstatic d_open_t         amr_open;
6565245Smsmithstatic d_close_t        amr_close;
6665245Smsmithstatic d_ioctl_t        amr_ioctl;
6765245Smsmith
6851974Smsmithstatic struct cdevsw amr_cdevsw = {
6951974Smsmith		/* open */	amr_open,
7051974Smsmith		/* close */	amr_close,
7151974Smsmith		/* read */	noread,
7251974Smsmith		/* write */	nowrite,
7351974Smsmith		/* ioctl */	amr_ioctl,
7451974Smsmith		/* poll */	nopoll,
7551974Smsmith		/* mmap */	nommap,
7651974Smsmith		/* strategy */	nostrategy,
7751974Smsmith		/* name */ 	"amr",
7851974Smsmith		/* maj */	AMR_CDEV_MAJOR,
7951974Smsmith		/* dump */	nodump,
8051974Smsmith		/* psize */ 	nopsize,
8151974Smsmith		/* flags */	0,
8251974Smsmith		/* bmaj */	254	/* XXX magic no-bdev */
8351974Smsmith};
8451974Smsmith
8565245Smsmith/*
8665245Smsmith * Initialisation, bus interface.
8765245Smsmith */
8865245Smsmithstatic void	amr_startup(void *arg);
8951974Smsmith
9051974Smsmith/*
9151974Smsmith * Command wrappers
9251974Smsmith */
9365245Smsmithstatic int	amr_query_controller(struct amr_softc *sc);
9465245Smsmithstatic void	*amr_enquiry(struct amr_softc *sc, size_t bufsize,
9565245Smsmith			     u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual);
9665245Smsmithstatic void	amr_completeio(struct amr_command *ac);
9751974Smsmith
9851974Smsmith/*
9965245Smsmith * Command buffer allocation.
10051974Smsmith */
10165245Smsmithstatic void	amr_alloccmd_cluster(struct amr_softc *sc);
10265245Smsmithstatic void	amr_freecmd_cluster(struct amr_command_cluster *acc);
10351974Smsmith
10451974Smsmith/*
10565245Smsmith * Command processing.
10651974Smsmith */
10765245Smsmithstatic int	amr_bio_command(struct amr_softc *sc, struct amr_command **acp);
10865245Smsmithstatic int	amr_wait_command(struct amr_command *ac);
10965245Smsmithstatic int	amr_poll_command(struct amr_command *ac);
11065245Smsmithstatic int	amr_getslot(struct amr_command *ac);
11165245Smsmithstatic void	amr_mapcmd(struct amr_command *ac);
11265245Smsmithstatic void	amr_unmapcmd(struct amr_command *ac);
11365245Smsmithstatic int	amr_start(struct amr_command *ac);
11465245Smsmithstatic void	amr_complete(void *context, int pending);
11551974Smsmith
11651974Smsmith/*
11758883Smsmith * Status monitoring
11858883Smsmith */
11965245Smsmithstatic void	amr_periodic(void *data);
12058883Smsmith
12158883Smsmith/*
12251974Smsmith * Interface-specific shims
12351974Smsmith */
12465245Smsmithstatic int	amr_quartz_submit_command(struct amr_softc *sc);
12565245Smsmithstatic int	amr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave);
12651974Smsmith
12765245Smsmithstatic int	amr_std_submit_command(struct amr_softc *sc);
12865245Smsmithstatic int	amr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave);
12965245Smsmithstatic void	amr_std_attach_mailbox(struct amr_softc *sc);
13051974Smsmith
13165245Smsmith#ifdef AMR_BOARD_INIT
13265245Smsmithstatic int	amr_quartz_init(struct amr_softc *sc);
13365245Smsmithstatic int	amr_std_init(struct amr_softc *sc);
13465245Smsmith#endif
13565245Smsmith
13651974Smsmith/*
13751974Smsmith * Debugging
13851974Smsmith */
13965245Smsmithstatic void	amr_describe_controller(struct amr_softc *sc);
14065245Smsmith#ifdef AMR_DEBUG
14165245Smsmithstatic void	amr_printcommand(struct amr_command *ac);
14265245Smsmith#endif
14351974Smsmith
14451974Smsmith/********************************************************************************
14551974Smsmith ********************************************************************************
14665245Smsmith                                                                      Inline Glue
14751974Smsmith ********************************************************************************
14851974Smsmith ********************************************************************************/
14951974Smsmith
15051974Smsmith/********************************************************************************
15165245Smsmith ********************************************************************************
15265245Smsmith                                                                Public Interfaces
15365245Smsmith ********************************************************************************
15465245Smsmith ********************************************************************************/
15551974Smsmith
15651974Smsmith/********************************************************************************
15751974Smsmith * Initialise the controller and softc.
15851974Smsmith */
15951974Smsmithint
16051974Smsmithamr_attach(struct amr_softc *sc)
16151974Smsmith{
16251974Smsmith
16365245Smsmith    debug_called(1);
16465245Smsmith
16551974Smsmith    /*
16651974Smsmith     * Initialise per-controller queues.
16751974Smsmith     */
16865245Smsmith    TAILQ_INIT(&sc->amr_completed);
16951974Smsmith    TAILQ_INIT(&sc->amr_freecmds);
17065245Smsmith    TAILQ_INIT(&sc->amr_cmd_clusters);
17165245Smsmith    TAILQ_INIT(&sc->amr_ready);
17259249Sphk    bioq_init(&sc->amr_bioq);
17351974Smsmith
17465245Smsmith#if __FreeBSD_version >= 500005
17551974Smsmith    /*
17665245Smsmith     * Initialise command-completion task.
17765245Smsmith     */
17865245Smsmith    TASK_INIT(&sc->amr_task_complete, 0, amr_complete, sc);
17965245Smsmith#endif
18065245Smsmith
18165245Smsmith    debug(2, "queue init done");
18265245Smsmith
18365245Smsmith    /*
18451974Smsmith     * Configure for this controller type.
18551974Smsmith     */
18665245Smsmith    if (AMR_IS_QUARTZ(sc)) {
18751974Smsmith	sc->amr_submit_command = amr_quartz_submit_command;
18851974Smsmith	sc->amr_get_work       = amr_quartz_get_work;
18951974Smsmith    } else {
19051974Smsmith	sc->amr_submit_command = amr_std_submit_command;
19151974Smsmith	sc->amr_get_work       = amr_std_get_work;
19265245Smsmith	amr_std_attach_mailbox(sc);;
19351974Smsmith    }
19451974Smsmith
19565245Smsmith#ifdef AMR_BOARD_INIT
19665245Smsmith    if ((AMR_IS_QUARTZ(sc) ? amr_quartz_init(sc) : amr_std_init(sc))))
19765245Smsmith	return(ENXIO);
19865245Smsmith#endif
19951974Smsmith
20051974Smsmith    /*
20165245Smsmith     * Quiz controller for features and limits.
20251974Smsmith     */
20365245Smsmith    if (amr_query_controller(sc))
20465245Smsmith	return(ENXIO);
20551974Smsmith
20665245Smsmith    debug(2, "controller query complete");
20751974Smsmith
20865245Smsmith#ifdef AMR_SCSI_PASSTHROUGH
20951974Smsmith    /*
21065245Smsmith     * Attach our 'real' SCSI channels to CAM.
21151974Smsmith     */
21265245Smsmith    if (amr_cam_attach(sc))
21351974Smsmith	return(ENXIO);
21465245Smsmith    debug(2, "CAM attach done");
21565245Smsmith#endif
21651974Smsmith
21751974Smsmith    /*
21865245Smsmith     * Create the control device.
21951974Smsmith     */
22065245Smsmith    sc->amr_dev_t = make_dev(&amr_cdevsw, device_get_unit(sc->amr_dev), UID_ROOT, GID_OPERATOR,
22165245Smsmith			     S_IRUSR | S_IWUSR, "amr%d", device_get_unit(sc->amr_dev));
22265245Smsmith    sc->amr_dev_t->si_drv1 = sc;
22351974Smsmith
22451974Smsmith    /*
22565245Smsmith     * Schedule ourselves to bring the controller up once interrupts are
22665245Smsmith     * available.
22751974Smsmith     */
22865245Smsmith    bzero(&sc->amr_ich, sizeof(struct intr_config_hook));
22965245Smsmith    sc->amr_ich.ich_func = amr_startup;
23065245Smsmith    sc->amr_ich.ich_arg = sc;
23165245Smsmith    if (config_intrhook_establish(&sc->amr_ich) != 0) {
23265245Smsmith	device_printf(sc->amr_dev, "can't establish configuration hook\n");
23365245Smsmith	return(ENOMEM);
23465245Smsmith    }
23551974Smsmith
23658883Smsmith    /*
23765245Smsmith     * Print a little information about the controller.
23858883Smsmith     */
23965245Smsmith    amr_describe_controller(sc);
24058883Smsmith
24165245Smsmith    debug(2, "attach complete");
24251974Smsmith    return(0);
24351974Smsmith}
24451974Smsmith
24551974Smsmith/********************************************************************************
24651974Smsmith * Locate disk resources and attach children to them.
24751974Smsmith */
24865245Smsmithstatic void
24965245Smsmithamr_startup(void *arg)
25051974Smsmith{
25165245Smsmith    struct amr_softc	*sc = (struct amr_softc *)arg;
25251974Smsmith    struct amr_logdrive	*dr;
25351974Smsmith    int			i, error;
25451974Smsmith
25565245Smsmith    debug_called(1);
25651974Smsmith
25765245Smsmith    /* pull ourselves off the intrhook chain */
25865245Smsmith    config_intrhook_disestablish(&sc->amr_ich);
25965245Smsmith
26051974Smsmith    /* get up-to-date drive information */
26151974Smsmith    if (amr_query_controller(sc)) {
26265245Smsmith	device_printf(sc->amr_dev, "can't scan controller for drives\n");
26351974Smsmith	return;
26451974Smsmith    }
26551974Smsmith
26651974Smsmith    /* iterate over available drives */
26751974Smsmith    for (i = 0, dr = &sc->amr_drive[0]; (i < AMR_MAXLD) && (dr->al_size != 0xffffffff); i++, dr++) {
26851974Smsmith	/* are we already attached to this drive? */
26951974Smsmith	if (dr->al_disk == 0) {
27051974Smsmith	    /* generate geometry information */
27151974Smsmith	    if (dr->al_size > 0x200000) {	/* extended translation? */
27251974Smsmith		dr->al_heads = 255;
27351974Smsmith		dr->al_sectors = 63;
27451974Smsmith	    } else {
27551974Smsmith		dr->al_heads = 64;
27651974Smsmith		dr->al_sectors = 32;
27751974Smsmith	    }
27851974Smsmith	    dr->al_cylinders = dr->al_size / (dr->al_heads * dr->al_sectors);
27951974Smsmith
28054073Smdodd	    dr->al_disk = device_add_child(sc->amr_dev, NULL, -1);
28151974Smsmith	    if (dr->al_disk == 0)
28251974Smsmith		device_printf(sc->amr_dev, "device_add_child failed\n");
28354073Smdodd	    device_set_ivars(dr->al_disk, dr);
28451974Smsmith	}
28551974Smsmith    }
28651974Smsmith
28751974Smsmith    if ((error = bus_generic_attach(sc->amr_dev)) != 0)
28851974Smsmith	device_printf(sc->amr_dev, "bus_generic_attach returned %d\n", error);
28951974Smsmith
29051974Smsmith    /* mark controller back up */
29151974Smsmith    sc->amr_state &= ~AMR_STATE_SHUTDOWN;
29251974Smsmith
29351974Smsmith    /* interrupts will be enabled before we do anything more */
29451974Smsmith    sc->amr_state |= AMR_STATE_INTEN;
29551974Smsmith
29651974Smsmith    /*
29765245Smsmith     * Start the timeout routine.
29851974Smsmith     */
29965245Smsmith/*    sc->amr_timeout = timeout(amr_periodic, sc, hz);*/
30051974Smsmith
30165245Smsmith    return;
30251974Smsmith}
30351974Smsmith
30465245Smsmith/*******************************************************************************
30565245Smsmith * Free resources associated with a controller instance
30651974Smsmith */
30765245Smsmithvoid
30865245Smsmithamr_free(struct amr_softc *sc)
30951974Smsmith{
31065245Smsmith    struct amr_command_cluster	*acc;
31151974Smsmith
31265245Smsmith#ifdef AMR_SCSI_PASSTHROUGH
31365245Smsmith    /* detach from CAM */
31465245Smsmith    amr_cam_detach(sc);
31565245Smsmith#endif
31651974Smsmith
31765245Smsmith    /* cancel status timeout */
31865245Smsmith    untimeout(amr_periodic, sc, sc->amr_timeout);
31951974Smsmith
32065245Smsmith    /* throw away any command buffers */
32165245Smsmith    while ((acc = TAILQ_FIRST(&sc->amr_cmd_clusters)) != NULL) {
32265245Smsmith	TAILQ_REMOVE(&sc->amr_cmd_clusters, acc, acc_link);
32365245Smsmith	amr_freecmd_cluster(acc);
32451974Smsmith    }
32551974Smsmith}
32651974Smsmith
32751974Smsmith/*******************************************************************************
32865245Smsmith * Receive a bio structure from a child device and queue it on a particular
32951974Smsmith * disk resource, then poke the disk resource to start as much work as it can.
33051974Smsmith */
33151974Smsmithint
33265245Smsmithamr_submit_bio(struct amr_softc *sc, struct bio *bio)
33351974Smsmith{
33465245Smsmith    debug_called(2);
33552543Smsmith
33665245Smsmith    amr_enqueue_bio(sc, bio);
33751974Smsmith    amr_startio(sc);
33851974Smsmith    return(0);
33951974Smsmith}
34051974Smsmith
34151974Smsmith/********************************************************************************
34251974Smsmith * Accept an open operation on the control device.
34351974Smsmith */
34451974Smsmithint
34551974Smsmithamr_open(dev_t dev, int flags, int fmt, struct proc *p)
34651974Smsmith{
34751974Smsmith    int			unit = minor(dev);
34851974Smsmith    struct amr_softc	*sc = devclass_get_softc(amr_devclass, unit);
34951974Smsmith
35065245Smsmith    debug_called(1);
35165245Smsmith
35251974Smsmith    sc->amr_state |= AMR_STATE_OPEN;
35351974Smsmith    return(0);
35451974Smsmith}
35551974Smsmith
35651974Smsmith/********************************************************************************
35751974Smsmith * Accept the last close on the control device.
35851974Smsmith */
35951974Smsmithint
36051974Smsmithamr_close(dev_t dev, int flags, int fmt, struct proc *p)
36151974Smsmith{
36251974Smsmith    int			unit = minor(dev);
36351974Smsmith    struct amr_softc	*sc = devclass_get_softc(amr_devclass, unit);
36451974Smsmith
36565245Smsmith    debug_called(1);
36665245Smsmith
36751974Smsmith    sc->amr_state &= ~AMR_STATE_OPEN;
36851974Smsmith    return (0);
36951974Smsmith}
37051974Smsmith
37151974Smsmith/********************************************************************************
37251974Smsmith * Handle controller-specific control operations.
37351974Smsmith */
37451974Smsmithint
37551974Smsmithamr_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
37651974Smsmith{
37765245Smsmith    struct amr_softc		*sc = (struct amr_softc *)dev->si_drv1;
37865245Smsmith    int				*arg = (int *)addr;
37965245Smsmith    struct amr_user_ioctl	*au = (struct amr_user_ioctl *)addr;
38065245Smsmith    struct amr_command		*ac;
38165245Smsmith    struct amr_mailbox_ioctl	*mbi;
38265245Smsmith    struct amr_passthrough	*ap;
38365245Smsmith    void			*dp;
38465245Smsmith    int				error;
38565245Smsmith
38665245Smsmith    debug_called(1);
38765245Smsmith
38865245Smsmith    error = 0;
38965245Smsmith    dp = NULL;
39065245Smsmith    ap = NULL;
39165245Smsmith    ac = NULL;
39251974Smsmith    switch(cmd) {
39365245Smsmith
39465245Smsmith    case AMR_IO_VERSION:
39565245Smsmith	debug(1, "AMR_IO_VERSION");
39665245Smsmith	*arg = AMR_IO_VERSION_NUMBER;
39765245Smsmith	break;
39865245Smsmith
39965245Smsmith    case AMR_IO_COMMAND:
40065245Smsmith	debug(1, "AMR_IO_COMMAND");
40165245Smsmith	/* handle inbound data buffer */
40265245Smsmith	if (au->au_length != 0) {
40365245Smsmith	    if ((dp = malloc(au->au_length, M_DEVBUF, M_WAITOK)) == NULL) {
40465245Smsmith		error = ENOMEM;
40565245Smsmith		break;
40665245Smsmith	    }
40765245Smsmith	    if ((error = copyin(au->au_buffer, dp, au->au_length)) != 0)
40865245Smsmith		break;
40965245Smsmith	}
41065245Smsmith
41165245Smsmith	if ((ac = amr_alloccmd(sc)) == NULL) {
41265245Smsmith	    error = ENOMEM;
41365245Smsmith	    break;
41465245Smsmith	}
41565245Smsmith
41665245Smsmith	/* handle SCSI passthrough command */
41765245Smsmith	if (au->au_cmd[0] == AMR_CMD_PASS) {
41865245Smsmith	    if ((ap = malloc(sizeof(*ap), M_DEVBUF, M_WAITOK)) == NULL) {
41965245Smsmith		error = ENOMEM;
42065245Smsmith		break;
42165245Smsmith	    }
42265245Smsmith	    bzero(ap, sizeof(*ap));
42365245Smsmith
42465245Smsmith	    /* copy cdb */
42565245Smsmith	    ap->ap_cdb_length = au->au_cmd[2];
42665245Smsmith	    bcopy(&au->au_cmd[3], &ap->ap_cdb[0], ap->ap_cdb_length);
42765245Smsmith
42865245Smsmith	    /* build passthrough */
42965245Smsmith	    ap->ap_timeout		= au->au_cmd[ap->ap_cdb_length + 3] & 0x07;
43065245Smsmith	    ap->ap_ars			= (au->au_cmd[ap->ap_cdb_length + 3] & 0x08) ? 1 : 0;
43165245Smsmith	    ap->ap_islogical		= (au->au_cmd[ap->ap_cdb_length + 3] & 0x80) ? 1 : 0;
43265245Smsmith	    ap->ap_logical_drive_no	= au->au_cmd[ap->ap_cdb_length + 4];
43365245Smsmith	    ap->ap_channel		= au->au_cmd[ap->ap_cdb_length + 5];
43465245Smsmith	    ap->ap_scsi_id 		= au->au_cmd[ap->ap_cdb_length + 6];
43565245Smsmith	    ap->ap_request_sense_length	= 14;
43665245Smsmith	    /* XXX what about the request-sense area? does the caller want it? */
43765245Smsmith
43865245Smsmith	    /* build command */
43965245Smsmith	    ac->ac_data = ap;
44065245Smsmith	    ac->ac_length = sizeof(*ap);
44165245Smsmith	    ac->ac_flags |= AMR_CMD_DATAOUT;
44265245Smsmith	    ac->ac_ccb_data = dp;
44365245Smsmith	    ac->ac_ccb_length = au->au_length;
44465245Smsmith	    if (au->au_direction & AMR_IO_READ)
44565245Smsmith		ac->ac_flags |= AMR_CMD_CCB_DATAIN;
44665245Smsmith	    if (au->au_direction & AMR_IO_WRITE)
44765245Smsmith		ac->ac_flags |= AMR_CMD_CCB_DATAOUT;
44865245Smsmith
44965245Smsmith	    ac->ac_mailbox.mb_command = AMR_CMD_PASS;
45065245Smsmith
45165245Smsmith	} else {
45265245Smsmith	    /* direct command to controller */
45365245Smsmith	    mbi = (struct amr_mailbox_ioctl *)&ac->ac_mailbox;
45465245Smsmith
45565245Smsmith	    /* copy pertinent mailbox items */
45665245Smsmith	    mbi->mb_command = au->au_cmd[0];
45765245Smsmith	    mbi->mb_channel = au->au_cmd[1];
45865245Smsmith	    mbi->mb_param = au->au_cmd[2];
45965245Smsmith	    mbi->mb_pad[0] = au->au_cmd[3];
46065245Smsmith	    mbi->mb_drive = au->au_cmd[4];
46165245Smsmith
46265245Smsmith	    /* build the command */
46365245Smsmith	    ac->ac_data = dp;
46465245Smsmith	    ac->ac_length = au->au_length;
46565245Smsmith	    if (au->au_direction & AMR_IO_READ)
46665245Smsmith		ac->ac_flags |= AMR_CMD_DATAIN;
46765245Smsmith	    if (au->au_direction & AMR_IO_WRITE)
46865245Smsmith		ac->ac_flags |= AMR_CMD_DATAOUT;
46965245Smsmith	}
47065245Smsmith
47165245Smsmith	/* run the command */
47265245Smsmith	if ((error = amr_wait_command(ac)) != 0)
47365245Smsmith	    break;
47465245Smsmith
47565245Smsmith	/* copy out data and set status */
47665245Smsmith	if (au->au_length != 0)
47765245Smsmith	    error = copyout(dp, au->au_buffer, au->au_length);
47865245Smsmith	au->au_status = ac->ac_status;
47965245Smsmith	break;
48065245Smsmith
48165245Smsmith    default:
48265245Smsmith	debug(1, "unknown ioctl 0x%lx", cmd);
48365245Smsmith	error = ENOIOCTL;
48465245Smsmith	break;
48551974Smsmith    }
48651974Smsmith
48765245Smsmith    if (dp != NULL)
48865245Smsmith	free(dp, M_DEVBUF);
48965245Smsmith    if (ap != NULL)
49065245Smsmith	free(ap, M_DEVBUF);
49165245Smsmith    if (ac != NULL)
49265245Smsmith	amr_releasecmd(ac);
49365245Smsmith    return(error);
49451974Smsmith}
49551974Smsmith
49651974Smsmith/********************************************************************************
49751974Smsmith ********************************************************************************
49858883Smsmith                                                                Status Monitoring
49958883Smsmith ********************************************************************************
50058883Smsmith ********************************************************************************/
50158883Smsmith
50258883Smsmith/********************************************************************************
50358883Smsmith * Perform a periodic check of the controller status
50458883Smsmith */
50558883Smsmithstatic void
50658883Smsmithamr_periodic(void *data)
50758883Smsmith{
50858883Smsmith    struct amr_softc	*sc = (struct amr_softc *)data;
50958883Smsmith
51065245Smsmith    debug_called(2);
51158883Smsmith
51265245Smsmith    /* XXX perform periodic status checks here */
51358883Smsmith
51465245Smsmith    /* compensate for missed interrupts */
51565245Smsmith    amr_done(sc);
51665245Smsmith
51758883Smsmith    /* reschedule */
51858883Smsmith    sc->amr_timeout = timeout(amr_periodic, sc, hz);
51958883Smsmith}
52058883Smsmith
52158883Smsmith/********************************************************************************
52258883Smsmith ********************************************************************************
52351974Smsmith                                                                 Command Wrappers
52451974Smsmith ********************************************************************************
52551974Smsmith ********************************************************************************/
52651974Smsmith
52751974Smsmith/********************************************************************************
52851974Smsmith * Interrogate the controller for the operational parameters we require.
52951974Smsmith */
53051974Smsmithstatic int
53151974Smsmithamr_query_controller(struct amr_softc *sc)
53251974Smsmith{
53365245Smsmith    struct amr_enquiry3	*aex;
53465245Smsmith    struct amr_prodinfo	*ap;
53565245Smsmith    struct amr_enquiry	*ae;
53665245Smsmith    int			ldrv;
53751974Smsmith
53865245Smsmith    /*
53965245Smsmith     * If we haven't found the real limit yet, let us have a couple of commands in
54065245Smsmith     * order to be able to probe.
54165245Smsmith     */
54265245Smsmith    if (sc->amr_maxio == 0)
54365245Smsmith	sc->amr_maxio = 2;
54451974Smsmith
54565245Smsmith    /*
54665245Smsmith     * Try to issue an ENQUIRY3 command
54765245Smsmith     */
54865245Smsmith    if ((aex = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_ENQ3,
54965245Smsmith			   AMR_CONFIG_ENQ3_SOLICITED_FULL)) != NULL) {
55051974Smsmith
55165245Smsmith	/*
55265245Smsmith	 * Fetch current state of logical drives.
55365245Smsmith	 */
55465245Smsmith	for (ldrv = 0; ldrv < aex->ae_numldrives; ldrv++) {
55565245Smsmith	    sc->amr_drive[ldrv].al_size       = aex->ae_drivesize[ldrv];
55665245Smsmith	    sc->amr_drive[ldrv].al_state      = aex->ae_drivestate[ldrv];
55765245Smsmith	    sc->amr_drive[ldrv].al_properties = aex->ae_driveprop[ldrv];
55865245Smsmith	    debug(2, "  drive %d: %d state %x properties %x\n", ldrv, sc->amr_drive[ldrv].al_size,
55965245Smsmith		  sc->amr_drive[ldrv].al_state, sc->amr_drive[ldrv].al_properties);
56051974Smsmith	}
56165245Smsmith	free(aex, M_DEVBUF);
56258883Smsmith
56365245Smsmith	/*
56465245Smsmith	 * Get product info for channel count.
56558883Smsmith	 */
56665245Smsmith	if ((ap = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0)) == NULL) {
56765245Smsmith	    device_printf(sc->amr_dev, "can't obtain product data from controller\n");
56865245Smsmith	    return(1);
56965245Smsmith	}
57065245Smsmith	sc->amr_maxdrives = 40;
57165245Smsmith	sc->amr_maxchan = ap->ap_nschan;
57265245Smsmith	sc->amr_maxio = ap->ap_maxio;
57365245Smsmith	sc->amr_type |= AMR_TYPE_40LD;
57465245Smsmith	free(ap, M_DEVBUF);
57558883Smsmith
57665245Smsmith    } else {
57765245Smsmith
57865245Smsmith	/* failed, try the 8LD ENQUIRY commands */
57965245Smsmith	if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_EXT_ENQUIRY2, 0, 0)) == NULL) {
58065245Smsmith	    if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0)) == NULL) {
58165245Smsmith		device_printf(sc->amr_dev, "can't obtain configuration data from controller\n");
58265245Smsmith		return(1);
58365245Smsmith	    }
58465245Smsmith	    ae->ae_signature = 0;
58551974Smsmith	}
58665245Smsmith
58758883Smsmith	/*
58865245Smsmith	 * Fetch current state of logical drives.
58958883Smsmith	 */
59065245Smsmith	for (ldrv = 0; ldrv < ae->ae_ldrv.al_numdrives; ldrv++) {
59165245Smsmith	    sc->amr_drive[ldrv].al_size       = ae->ae_ldrv.al_size[ldrv];
59265245Smsmith	    sc->amr_drive[ldrv].al_state      = ae->ae_ldrv.al_state[ldrv];
59365245Smsmith	    sc->amr_drive[ldrv].al_properties = ae->ae_ldrv.al_properties[ldrv];
59465245Smsmith	    debug(2, "  drive %d: %d state %x properties %x\n", ldrv, sc->amr_drive[ldrv].al_size,
59565245Smsmith		  sc->amr_drive[ldrv].al_state, sc->amr_drive[ldrv].al_properties);
59651974Smsmith	}
59765245Smsmith
59865245Smsmith	sc->amr_maxdrives = 8;
59965245Smsmith	sc->amr_maxchan = ae->ae_adapter.aa_channels;
60065245Smsmith	sc->amr_maxio = ae->ae_adapter.aa_maxio;
60165245Smsmith	free(ae, M_DEVBUF);
60251974Smsmith    }
60365245Smsmith
60465245Smsmith    /*
60565245Smsmith     * Mark remaining drives as unused.
60665245Smsmith     */
60765245Smsmith    for (; ldrv < AMR_MAXLD; ldrv++)
60865245Smsmith	sc->amr_drive[ldrv].al_size = 0xffffffff;
60965245Smsmith
61065245Smsmith    /*
61165245Smsmith     * Cap the maximum number of outstanding I/Os.  AMI's Linux driver doesn't trust
61265245Smsmith     * the controller's reported value, and lockups have been seen when we do.
61365245Smsmith     */
61465245Smsmith    sc->amr_maxio = imin(sc->amr_maxio, AMR_LIMITCMD);
61565245Smsmith
61651974Smsmith    return(0);
61751974Smsmith}
61851974Smsmith
61951974Smsmith/********************************************************************************
62051974Smsmith * Run a generic enquiry-style command.
62151974Smsmith */
62251974Smsmithstatic void *
62351974Smsmithamr_enquiry(struct amr_softc *sc, size_t bufsize, u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual)
62451974Smsmith{
62551974Smsmith    struct amr_command	*ac;
62651974Smsmith    void		*result;
62751974Smsmith    u_int8_t		*mbox;
62851974Smsmith    int			error;
62951974Smsmith
63065245Smsmith    debug_called(1);
63151974Smsmith
63251974Smsmith    error = 1;
63351974Smsmith    result = NULL;
63451974Smsmith
63551974Smsmith    /* get ourselves a command buffer */
63651974Smsmith    if ((ac = amr_alloccmd(sc)) == NULL)
63751974Smsmith	goto out;
63851974Smsmith    /* allocate the response structure */
63951974Smsmith    if ((result = malloc(bufsize, M_DEVBUF, M_NOWAIT)) == NULL)
64051974Smsmith	goto out;
64165245Smsmith    /* set command flags */
64251974Smsmith    ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
64351974Smsmith
64465245Smsmith    /* point the command at our data */
64551974Smsmith    ac->ac_data = result;
64651974Smsmith    ac->ac_length = bufsize;
64751974Smsmith
64851974Smsmith    /* build the command proper */
64951974Smsmith    mbox = (u_int8_t *)&ac->ac_mailbox;		/* XXX want a real structure for this? */
65051974Smsmith    mbox[0] = cmd;
65151974Smsmith    mbox[2] = cmdsub;
65251974Smsmith    mbox[3] = cmdqual;
65351974Smsmith
65458883Smsmith    /* can't assume that interrupts are going to work here, so play it safe */
65558883Smsmith    if (amr_poll_command(ac))
65651974Smsmith	goto out;
65751974Smsmith    error = ac->ac_status;
65851974Smsmith
65951974Smsmith out:
66051974Smsmith    if (ac != NULL)
66151974Smsmith	amr_releasecmd(ac);
66251974Smsmith    if ((error != 0) && (result != NULL)) {
66351974Smsmith	free(result, M_DEVBUF);
66451974Smsmith	result = NULL;
66551974Smsmith    }
66651974Smsmith    return(result);
66751974Smsmith}
66851974Smsmith
66951974Smsmith/********************************************************************************
67051974Smsmith * Flush the controller's internal cache, return status.
67151974Smsmith */
67265245Smsmithint
67351974Smsmithamr_flush(struct amr_softc *sc)
67451974Smsmith{
67551974Smsmith    struct amr_command	*ac;
67651974Smsmith    int			error;
67751974Smsmith
67851974Smsmith    /* get ourselves a command buffer */
67951974Smsmith    error = 1;
68051974Smsmith    if ((ac = amr_alloccmd(sc)) == NULL)
68151974Smsmith	goto out;
68265245Smsmith    /* set command flags */
68351974Smsmith    ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
68451974Smsmith
68551974Smsmith    /* build the command proper */
68651974Smsmith    ac->ac_mailbox.mb_command = AMR_CMD_FLUSH;
68751974Smsmith
68858883Smsmith    /* we have to poll, as the system may be going down or otherwise damaged */
68958883Smsmith    if (amr_poll_command(ac))
69051974Smsmith	goto out;
69151974Smsmith    error = ac->ac_status;
69251974Smsmith
69351974Smsmith out:
69451974Smsmith    if (ac != NULL)
69551974Smsmith	amr_releasecmd(ac);
69651974Smsmith    return(error);
69751974Smsmith}
69851974Smsmith
69951974Smsmith/********************************************************************************
70065245Smsmith * Try to find I/O work for the controller from one or more of the work queues.
70151974Smsmith *
70265245Smsmith * We make the assumption that if the controller is not ready to take a command
70365245Smsmith * at some given time, it will generate an interrupt at some later time when
70465245Smsmith * it is.
70551974Smsmith */
70665245Smsmithvoid
70751974Smsmithamr_startio(struct amr_softc *sc)
70851974Smsmith{
70951974Smsmith    struct amr_command	*ac;
71051974Smsmith
71151974Smsmith    /* spin until something prevents us from doing any work */
71251974Smsmith    for (;;) {
71351974Smsmith
71465245Smsmith	/* try to get a ready command */
71565245Smsmith	ac = amr_dequeue_ready(sc);
71651974Smsmith
71765245Smsmith	/* if that failed, build a command from a bio */
71865245Smsmith	if (ac == NULL)
71965245Smsmith	    (void)amr_bio_command(sc, &ac);
72051974Smsmith
72165245Smsmith#ifdef AMR_SCSI_PASSTHROUGH
72265245Smsmith	/* if that failed, build a command from a ccb */
72365245Smsmith	if (ac == NULL)
72465245Smsmith	    (void)amr_cam_command(sc, &ac);
72565245Smsmith#endif
72665245Smsmith
72765245Smsmith	/* if we don't have anything to do, give up */
72865245Smsmith	if (ac == NULL)
72965245Smsmith	    break;
73051974Smsmith
73165245Smsmith	/* try to give the command to the controller; if this fails save it for later and give up */
73265245Smsmith	if (amr_start(ac)) {
73365245Smsmith	    debug(2, "controller busy, command deferred");
73465245Smsmith	    amr_requeue_ready(ac);	/* XXX schedule retry very soon? */
73565245Smsmith	    break;
73651974Smsmith	}
73751974Smsmith    }
73851974Smsmith}
73951974Smsmith
74051974Smsmith/********************************************************************************
74151974Smsmith * Handle completion of an I/O command.
74251974Smsmith */
74351974Smsmithstatic void
74451974Smsmithamr_completeio(struct amr_command *ac)
74551974Smsmith{
74651974Smsmith    struct amr_softc	*sc = ac->ac_sc;
74751974Smsmith
74851974Smsmith    if (ac->ac_status != AMR_STATUS_SUCCESS) {	/* could be more verbose here? */
74965245Smsmith	ac->ac_bio->bio_error = EIO;
75065245Smsmith	ac->ac_bio->bio_flags |= BIO_ERROR;
75151974Smsmith
75265245Smsmith	device_printf(sc->amr_dev, "I/O error - 0x%x\n", ac->ac_status);
75365245Smsmith/*	amr_printcommand(ac);*/
75451974Smsmith    }
75565245Smsmith    amrd_intr(ac->ac_bio);
75665245Smsmith    amr_releasecmd(ac);
75751974Smsmith}
75851974Smsmith
75951974Smsmith/********************************************************************************
76051974Smsmith ********************************************************************************
76151974Smsmith                                                               Command Processing
76251974Smsmith ********************************************************************************
76351974Smsmith ********************************************************************************/
76451974Smsmith
76551974Smsmith/********************************************************************************
76665245Smsmith * Convert a bio off the top of the bio queue into a command.
76765245Smsmith */
76865245Smsmithstatic int
76965245Smsmithamr_bio_command(struct amr_softc *sc, struct amr_command **acp)
77065245Smsmith{
77165245Smsmith    struct amr_command	*ac;
77265245Smsmith    struct amrd_softc	*amrd;
77365245Smsmith    struct bio		*bio;
77465245Smsmith    int			error;
77565245Smsmith    int			blkcount;
77665245Smsmith    int			driveno;
77765245Smsmith    int			cmd;
77865245Smsmith
77965245Smsmith    ac = NULL;
78065245Smsmith    error = 0;
78165245Smsmith
78265245Smsmith    /* get a bio to work on */
78365245Smsmith    if ((bio = amr_dequeue_bio(sc)) == NULL)
78465245Smsmith	goto out;
78565245Smsmith
78665245Smsmith    /* get a command */
78765245Smsmith    if ((ac = amr_alloccmd(sc)) == NULL) {
78865245Smsmith	error = ENOMEM;
78965245Smsmith	goto out;
79065245Smsmith    }
79165245Smsmith
79265245Smsmith    /* connect the bio to the command */
79365245Smsmith    ac->ac_complete = amr_completeio;
79465245Smsmith    ac->ac_bio = bio;
79565245Smsmith    ac->ac_data = bio->bio_data;
79665245Smsmith    ac->ac_length = bio->bio_bcount;
79765245Smsmith    if (BIO_IS_READ(bio)) {
79865245Smsmith	ac->ac_flags |= AMR_CMD_DATAIN;
79965245Smsmith	cmd = AMR_CMD_LREAD;
80065245Smsmith    } else {
80165245Smsmith	ac->ac_flags |= AMR_CMD_DATAOUT;
80265245Smsmith	cmd = AMR_CMD_LWRITE;
80365245Smsmith    }
80465245Smsmith    amrd = (struct amrd_softc *)bio->bio_dev->si_drv1;
80565245Smsmith    driveno = amrd->amrd_drive - sc->amr_drive;
80665245Smsmith    blkcount = (bio->bio_bcount + AMR_BLKSIZE - 1) / AMR_BLKSIZE;
80765245Smsmith
80865245Smsmith    ac->ac_mailbox.mb_command = cmd;
80965245Smsmith    ac->ac_mailbox.mb_blkcount = blkcount;
81065245Smsmith    ac->ac_mailbox.mb_lba = bio->bio_pblkno;
81165245Smsmith    ac->ac_mailbox.mb_drive = driveno;
81265245Smsmith    /* we fill in the s/g related data when the command is mapped */
81365245Smsmith
81465245Smsmith    if ((bio->bio_pblkno + blkcount) > sc->amr_drive[driveno].al_size)
81565245Smsmith	device_printf(sc->amr_dev, "I/O beyond end of unit (%u,%d > %u)\n",
81665245Smsmith		      bio->bio_pblkno, blkcount, sc->amr_drive[driveno].al_size);
81765245Smsmith
81865245Smsmithout:
81965245Smsmith    if (error != 0) {
82065245Smsmith	if (ac != NULL)
82165245Smsmith	    amr_releasecmd(ac);
82265245Smsmith	if (bio != NULL)			/* this breaks ordering... */
82365245Smsmith	    amr_enqueue_bio(sc, bio);
82465245Smsmith    }
82565245Smsmith    *acp = ac;
82665245Smsmith    return(error);
82765245Smsmith}
82865245Smsmith
82965245Smsmith/********************************************************************************
83051974Smsmith * Take a command, submit it to the controller and sleep until it completes
83151974Smsmith * or fails.  Interrupts must be enabled, returns nonzero on error.
83251974Smsmith */
83351974Smsmithstatic int
83451974Smsmithamr_wait_command(struct amr_command *ac)
83551974Smsmith{
83651974Smsmith    struct amr_softc	*sc = ac->ac_sc;
83751974Smsmith    int			error, count;
83851974Smsmith
83965245Smsmith    debug_called(1);
84051974Smsmith
84151974Smsmith    ac->ac_complete = NULL;
84265245Smsmith    ac->ac_flags |= AMR_CMD_SLEEP;
84351974Smsmith    if ((error = amr_start(ac)) != 0)
84451974Smsmith	return(error);
84551974Smsmith
84651974Smsmith    count = 0;
84751974Smsmith    /* XXX better timeout? */
84865245Smsmith    while ((ac->ac_flags & AMR_CMD_BUSY) && (count < 30)) {
84965245Smsmith	tsleep(ac, PRIBIO | PCATCH, "amrwcmd", hz);
85051974Smsmith    }
85151974Smsmith
85251974Smsmith    if (ac->ac_status != 0) {
85365245Smsmith	device_printf(sc->amr_dev, "I/O error - 0x%x\n", ac->ac_status);
85451974Smsmith	return(EIO);
85551974Smsmith    }
85651974Smsmith    return(0);
85751974Smsmith}
85851974Smsmith
85951974Smsmith/********************************************************************************
86051974Smsmith * Take a command, submit it to the controller and busy-wait for it to return.
86151974Smsmith * Returns nonzero on error.  Can be safely called with interrupts enabled.
86251974Smsmith */
86351974Smsmithstatic int
86451974Smsmithamr_poll_command(struct amr_command *ac)
86551974Smsmith{
86651974Smsmith    struct amr_softc	*sc = ac->ac_sc;
86765245Smsmith    int			error, count;
86851974Smsmith
86965245Smsmith    debug_called(2);
87051974Smsmith
87151974Smsmith    ac->ac_complete = NULL;
87251974Smsmith    if ((error = amr_start(ac)) != 0)
87351974Smsmith	return(error);
87451974Smsmith
87551974Smsmith    count = 0;
87651974Smsmith    do {
87751974Smsmith	/*
87851974Smsmith	 * Poll for completion, although the interrupt handler may beat us to it.
87951974Smsmith	 * Note that the timeout here is somewhat arbitrary.
88051974Smsmith	 */
88151974Smsmith	amr_done(sc);
88265245Smsmith	DELAY(1000);
88365245Smsmith    } while ((ac->ac_flags & AMR_CMD_BUSY) && (count++ < 1000));
88465245Smsmith    if (!(ac->ac_flags & AMR_CMD_BUSY)) {
88551974Smsmith	error = 0;
88651974Smsmith    } else {
88765245Smsmith	/* XXX the slot is now marked permanently busy */
88851974Smsmith	error = EIO;
88965245Smsmith	device_printf(sc->amr_dev, "polled command timeout\n");
89051974Smsmith    }
89151974Smsmith    return(error);
89251974Smsmith}
89351974Smsmith
89451974Smsmith/********************************************************************************
89565245Smsmith * Get a free command slot for a command if it doesn't already have one.
89665245Smsmith *
89765245Smsmith * May be safely called multiple times for a given command.
89851974Smsmith */
89951974Smsmithstatic int
90051974Smsmithamr_getslot(struct amr_command *ac)
90151974Smsmith{
90251974Smsmith    struct amr_softc	*sc = ac->ac_sc;
90365245Smsmith    int			s, slot, limit, error;
90451974Smsmith
90565245Smsmith    debug_called(3);
90665245Smsmith
90765245Smsmith    /* if the command already has a slot, don't try to give it another one */
90865245Smsmith    if (ac->ac_slot != 0)
90965245Smsmith	return(0);
91065245Smsmith
91151974Smsmith    /* enforce slot usage limit */
91251974Smsmith    limit = (ac->ac_flags & AMR_CMD_PRIORITY) ? sc->amr_maxio : sc->amr_maxio - 4;
91365245Smsmith    if (sc->amr_busyslots > limit)
91451974Smsmith	return(EBUSY);
91551974Smsmith
91651974Smsmith    /*
91765245Smsmith     * Allocate a slot.  XXX linear scan is slow
91851974Smsmith     */
91965245Smsmith    error = EBUSY;
92051974Smsmith    s = splbio();
92151974Smsmith    for (slot = 0; slot < sc->amr_maxio; slot++) {
92265245Smsmith	if (sc->amr_busycmd[slot] == NULL) {
92365245Smsmith	    sc->amr_busycmd[slot] = ac;
92465245Smsmith	    sc->amr_busyslots++;
92565245Smsmith	    ac->ac_slot = slot;
92665245Smsmith	    error = 0;
92751974Smsmith	    break;
92865245Smsmith	}
92951974Smsmith    }
93051974Smsmith    splx(s);
93151974Smsmith
93265245Smsmith    return(error);
93351974Smsmith}
93451974Smsmith
93551974Smsmith/********************************************************************************
93665245Smsmith * Map/unmap (ac)'s data in the controller's addressable space as required.
93765245Smsmith *
93865245Smsmith * These functions may be safely called multiple times on a given command.
93951974Smsmith */
94051974Smsmithstatic void
94151974Smsmithamr_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
94251974Smsmith{
94351974Smsmith    struct amr_command	*ac = (struct amr_command *)arg;
94451974Smsmith    struct amr_softc	*sc = ac->ac_sc;
94551974Smsmith    struct amr_sgentry	*sg;
94651974Smsmith    int			i;
94751974Smsmith
94865245Smsmith    debug_called(3);
94951974Smsmith
95051974Smsmith    /* get base address of s/g table */
95151974Smsmith    sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
95251974Smsmith
95365245Smsmith    /* save data physical address */
95451974Smsmith    ac->ac_dataphys = segs[0].ds_addr;
95551974Smsmith
95665245Smsmith    /* decide whether we need to populate the s/g table */
95765245Smsmith    if (nsegments < 2) {
95865245Smsmith	ac->ac_mailbox.mb_nsgelem = 0;
95965245Smsmith	ac->ac_mailbox.mb_physaddr = ac->ac_dataphys;
96065245Smsmith    } else {
96165245Smsmith	ac->ac_mailbox.mb_nsgelem = nsegments;
96265245Smsmith	ac->ac_mailbox.mb_physaddr = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry));
96365245Smsmith	for (i = 0; i < nsegments; i++, sg++) {
96465245Smsmith	    sg->sg_addr = segs[i].ds_addr;
96565245Smsmith	    sg->sg_count = segs[i].ds_len;
96665245Smsmith	}
96765245Smsmith    }
96865245Smsmith}
96965245Smsmith
97065245Smsmithstatic void
97165245Smsmithamr_setup_ccbmap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
97265245Smsmith{
97365245Smsmith    struct amr_command		*ac = (struct amr_command *)arg;
97465245Smsmith    struct amr_softc		*sc = ac->ac_sc;
97565245Smsmith    struct amr_sgentry		*sg;
97665245Smsmith    struct amr_passthrough	*ap = (struct amr_passthrough *)ac->ac_data;
97765245Smsmith    int				i;
97865245Smsmith
97965245Smsmith    /* get base address of s/g table */
98065245Smsmith    sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
98165245Smsmith
98265245Smsmith    /* save s/g table information in passthrough */
98365245Smsmith    ap->ap_no_sg_elements = nsegments;
98465245Smsmith    ap->ap_data_transfer_address = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry));
98565245Smsmith
98665245Smsmith    /* save pointer to passthrough in command   XXX is this already done above? */
98765245Smsmith    ac->ac_mailbox.mb_physaddr = ac->ac_dataphys;
98865245Smsmith
98965245Smsmith    debug(2, "slot %d  %d segments at 0x%x, passthrough at 0x%x", ac->ac_slot,
99065245Smsmith	   ap->ap_no_sg_elements, ap->ap_data_transfer_address, ac->ac_dataphys);
99165245Smsmith
99265245Smsmith    /* populate s/g table (overwrites previous call which mapped the passthrough) */
99351974Smsmith    for (i = 0; i < nsegments; i++, sg++) {
99451974Smsmith	sg->sg_addr = segs[i].ds_addr;
99551974Smsmith	sg->sg_count = segs[i].ds_len;
99665245Smsmith	debug(2, " %d: 0x%x/%d", i, sg->sg_addr, sg->sg_count);
99751974Smsmith    }
99851974Smsmith}
99951974Smsmith
100051974Smsmithstatic void
100151974Smsmithamr_mapcmd(struct amr_command *ac)
100251974Smsmith{
100351974Smsmith    struct amr_softc	*sc = ac->ac_sc;
100451974Smsmith
100565245Smsmith    debug_called(2);
100651974Smsmith
100765245Smsmith    /* if the command involves data at all, and hasn't been mapped */
100865245Smsmith    if (!(ac->ac_flags & AMR_CMD_MAPPED)) {
100965245Smsmith
101065245Smsmith	if (ac->ac_data != NULL) {
101165245Smsmith	    /* map the data buffers into bus space and build the s/g list */
101265245Smsmith	    bus_dmamap_load(sc->amr_buffer_dmat, ac->ac_dmamap, ac->ac_data, ac->ac_length,
101365245Smsmith			    amr_setup_dmamap, ac, 0);
101465245Smsmith	    if (ac->ac_flags & AMR_CMD_DATAIN)
101565245Smsmith		bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_PREREAD);
101665245Smsmith	    if (ac->ac_flags & AMR_CMD_DATAOUT)
101765245Smsmith		bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_PREWRITE);
101865245Smsmith	}
101965245Smsmith
102065245Smsmith	if (ac->ac_ccb_data != NULL) {
102165245Smsmith	    bus_dmamap_load(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, ac->ac_ccb_data, ac->ac_ccb_length,
102265245Smsmith			    amr_setup_ccbmap, ac, 0);
102365245Smsmith	    if (ac->ac_flags & AMR_CMD_CCB_DATAIN)
102465245Smsmith		bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_PREREAD);
102565245Smsmith	    if (ac->ac_flags & AMR_CMD_CCB_DATAOUT)
102665245Smsmith		bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_PREWRITE);
102765245Smsmith	}
102865245Smsmith	ac->ac_flags |= AMR_CMD_MAPPED;
102951974Smsmith    }
103051974Smsmith}
103151974Smsmith
103251974Smsmithstatic void
103351974Smsmithamr_unmapcmd(struct amr_command *ac)
103451974Smsmith{
103551974Smsmith    struct amr_softc	*sc = ac->ac_sc;
103651974Smsmith
103765245Smsmith    debug_called(2);
103851974Smsmith
103965245Smsmith    /* if the command involved data at all and was mapped */
104065245Smsmith    if (ac->ac_flags & AMR_CMD_MAPPED) {
104151974Smsmith
104265245Smsmith	if (ac->ac_data != NULL) {
104365245Smsmith	    if (ac->ac_flags & AMR_CMD_DATAIN)
104465245Smsmith		bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_POSTREAD);
104565245Smsmith	    if (ac->ac_flags & AMR_CMD_DATAOUT)
104665245Smsmith		bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_POSTWRITE);
104765245Smsmith	    bus_dmamap_unload(sc->amr_buffer_dmat, ac->ac_dmamap);
104865245Smsmith	}
104965245Smsmith
105065245Smsmith	if (ac->ac_ccb_data != NULL) {
105165245Smsmith	    if (ac->ac_flags & AMR_CMD_CCB_DATAIN)
105265245Smsmith		bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_POSTREAD);
105365245Smsmith	    if (ac->ac_flags & AMR_CMD_CCB_DATAOUT)
105465245Smsmith		bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_POSTWRITE);
105565245Smsmith	    bus_dmamap_unload(sc->amr_buffer_dmat, ac->ac_ccb_dmamap);
105665245Smsmith	}
105765245Smsmith	ac->ac_flags &= ~AMR_CMD_MAPPED;
105851974Smsmith    }
105951974Smsmith}
106051974Smsmith
106151974Smsmith/********************************************************************************
106265245Smsmith * Take a command and give it to the controller, returns 0 if successful, or
106365245Smsmith * EBUSY if the command should be retried later.
106451974Smsmith */
106551974Smsmithstatic int
106651974Smsmithamr_start(struct amr_command *ac)
106751974Smsmith{
106851974Smsmith    struct amr_softc	*sc = ac->ac_sc;
106958883Smsmith    int			done, s, i;
107051974Smsmith
107165245Smsmith    debug_called(2);
107251974Smsmith
107365245Smsmith    /* mark command as busy so that polling consumer can tell */
107465245Smsmith    ac->ac_flags |= AMR_CMD_BUSY;
107565245Smsmith
107665245Smsmith    /* get a command slot (freed in amr_done) */
107765245Smsmith    if (amr_getslot(ac))
107865245Smsmith	return(EBUSY);
107965245Smsmith
108065245Smsmith    /* now we have a slot, we can map the command (unmapped in amr_complete) */
108165245Smsmith    amr_mapcmd(ac);
108265245Smsmith
108365245Smsmith    /* mark the new mailbox we are going to copy in as busy */
108465245Smsmith    ac->ac_mailbox.mb_busy = 1;
108565245Smsmith
108665245Smsmith    /* clear the poll/ack fields in the mailbox */
108765245Smsmith    sc->amr_mailbox->mb_poll = 0;
108865245Smsmith    sc->amr_mailbox->mb_ack = 0;
108965245Smsmith
109051974Smsmith    /*
109151974Smsmith     * Save the slot number so that we can locate this command when complete.
109251974Smsmith     * Note that ident = 0 seems to be special, so we don't use it.
109351974Smsmith     */
109451974Smsmith    ac->ac_mailbox.mb_ident = ac->ac_slot + 1;
109551974Smsmith
109658883Smsmith    /*
109765245Smsmith     * Spin waiting for the mailbox, give up after ~1 second.  We expect the
109865245Smsmith     * controller to be able to handle our I/O.
109965245Smsmith     *
110065245Smsmith     * XXX perhaps we should wait for less time, and count on the deferred command
110165245Smsmith     * handling to deal with retries?
110258883Smsmith     */
110365245Smsmith    debug(2, "wait for mailbox");
110458883Smsmith    for (i = 10000, done = 0; (i > 0) && !done; i--) {
110551974Smsmith	s = splbio();
110651974Smsmith
110751974Smsmith	/* is the mailbox free? */
110851974Smsmith	if (sc->amr_mailbox->mb_busy == 0) {
110965245Smsmith	    debug(2, "got mailbox");
111051974Smsmith	    sc->amr_mailbox64->mb64_segment = 0;
111165245Smsmith	    bcopy(&ac->ac_mailbox, (void *)(uintptr_t)(volatile void *)sc->amr_mailbox, AMR_MBOX_CMDSIZE);
111251974Smsmith	    done = 1;
111351974Smsmith
111465245Smsmith	    /* not free, spin waiting */
111551974Smsmith	} else {
111665245Smsmith	    debug(3, "busy flag %x\n", sc->amr_mailbox->mb_busy);
111758883Smsmith	    /* this is somewhat ugly */
111858883Smsmith	    DELAY(100);
111951974Smsmith	}
112058883Smsmith	splx(s);	/* drop spl to allow completion interrupts */
112151974Smsmith    }
112265245Smsmith
112365245Smsmith    /*
112465245Smsmith     * Now give the command to the controller
112565245Smsmith     */
112651974Smsmith    if (done) {
112765245Smsmith	if (sc->amr_submit_command(sc)) {
112865245Smsmith	    /* the controller wasn't ready to take the command, forget that we tried to post it */
112965245Smsmith	    sc->amr_mailbox->mb_busy = 0;
113065245Smsmith	    return(EBUSY);
113165245Smsmith	}
113265245Smsmith	debug(2, "posted command");
113351974Smsmith	return(0);
113451974Smsmith    }
113551974Smsmith
113651974Smsmith    /*
113765245Smsmith     * The controller wouldn't take the command.  Return the command as busy
113865245Smsmith     * so that it is retried later.
113951974Smsmith     */
114065245Smsmith    return(EBUSY);
114151974Smsmith}
114251974Smsmith
114351974Smsmith/********************************************************************************
114451974Smsmith * Extract one or more completed commands from the controller (sc)
114551974Smsmith *
114652543Smsmith * Returns nonzero if any commands on the work queue were marked as completed.
114751974Smsmith */
114865245Smsmithint
114951974Smsmithamr_done(struct amr_softc *sc)
115051974Smsmith{
115151974Smsmith    struct amr_command	*ac;
115251974Smsmith    struct amr_mailbox	mbox;
115365245Smsmith    int			i, idx, result;
115451974Smsmith
115565245Smsmith    debug_called(2);
115651974Smsmith
115751974Smsmith    /* See if there's anything for us to do */
115851974Smsmith    result = 0;
115951974Smsmith
116058883Smsmith    /* loop collecting completed commands */
116158883Smsmith    for (;;) {
116258883Smsmith	/* poll for a completed command's identifier and status */
116358883Smsmith	if (sc->amr_get_work(sc, &mbox)) {
116458883Smsmith	    result = 1;
116558883Smsmith
116658883Smsmith	    /* iterate over completed commands in this result */
116758883Smsmith	    for (i = 0; i < mbox.mb_nstatus; i++) {
116858883Smsmith		/* get pointer to busy command */
116958883Smsmith		idx = mbox.mb_completed[i] - 1;
117058883Smsmith		ac = sc->amr_busycmd[idx];
117151974Smsmith
117258883Smsmith		/* really a busy command? */
117358883Smsmith		if (ac != NULL) {
117458883Smsmith
117558883Smsmith		    /* pull the command from the busy index */
117658883Smsmith		    sc->amr_busycmd[idx] = NULL;
117765245Smsmith		    sc->amr_busyslots--;
117851974Smsmith
117965245Smsmith		    /* save status for later use */
118065245Smsmith		    ac->ac_status = mbox.mb_status;
118165245Smsmith		    amr_enqueue_completed(ac);
118265245Smsmith		    debug(3, "completed command with status %x", mbox.mb_status);
118365245Smsmith		} else {
118465245Smsmith		    device_printf(sc->amr_dev, "bad slot %d completed\n", idx);
118551974Smsmith		}
118651974Smsmith	    }
118758883Smsmith	} else {
118865245Smsmith	    break;	/* no work */
118951974Smsmith	}
119051974Smsmith    }
119158883Smsmith
119258883Smsmith    /* if we've completed any commands, try posting some more */
119358883Smsmith    if (result)
119458883Smsmith	amr_startio(sc);
119558883Smsmith
119658883Smsmith    /* handle completion and timeouts */
119765245Smsmith#if __FreeBSD_version >= 500005
119865245Smsmith    if (sc->amr_state & AMR_STATE_INTEN)
119965245Smsmith	taskqueue_enqueue(taskqueue_swi, &sc->amr_task_complete);
120065245Smsmith    else
120165245Smsmith#endif
120265245Smsmith	amr_complete(sc, 0);
120358883Smsmith
120451974Smsmith    return(result);
120551974Smsmith}
120651974Smsmith
120751974Smsmith/********************************************************************************
120851974Smsmith * Do completion processing on done commands on (sc)
120951974Smsmith */
121051974Smsmithstatic void
121165245Smsmithamr_complete(void *context, int pending)
121251974Smsmith{
121365245Smsmith    struct amr_softc	*sc = (struct amr_softc *)context;
121465245Smsmith    struct amr_command	*ac;
121551974Smsmith
121665245Smsmith    debug_called(2);
121751974Smsmith
121865245Smsmith    /* pull completed commands off the queue */
121965245Smsmith    for (;;) {
122065245Smsmith	ac = amr_dequeue_completed(sc);
122165245Smsmith	if (ac == NULL)
122265245Smsmith	    break;
122358883Smsmith
122465245Smsmith	/* unmap the command's data buffer */
122565245Smsmith	amr_unmapcmd(ac);
122651974Smsmith
122765245Smsmith	/* unbusy the command */
122865245Smsmith	ac->ac_flags &= ~AMR_CMD_BUSY;
122951974Smsmith
123065245Smsmith	/*
123165245Smsmith	 * Is there a completion handler?
123265245Smsmith	 */
123365245Smsmith	if (ac->ac_complete != NULL) {
123465245Smsmith	    ac->ac_complete(ac);
123565245Smsmith
123651974Smsmith	    /*
123765245Smsmith	     * Is someone sleeping on this one?
123851974Smsmith	     */
123965245Smsmith	} else if (ac->ac_flags & AMR_CMD_SLEEP) {
124065245Smsmith	    wakeup(ac);
124151974Smsmith	}
124251974Smsmith    }
124351974Smsmith}
124451974Smsmith
124551974Smsmith/********************************************************************************
124651974Smsmith ********************************************************************************
124751974Smsmith                                                        Command Buffer Management
124851974Smsmith ********************************************************************************
124951974Smsmith ********************************************************************************/
125051974Smsmith
125151974Smsmith/********************************************************************************
125251974Smsmith * Get a new command buffer.
125351974Smsmith *
125451974Smsmith * This may return NULL in low-memory cases.
125551974Smsmith *
125651974Smsmith * If possible, we recycle a command buffer that's been used before.
125751974Smsmith */
125865245Smsmithstruct amr_command *
125951974Smsmithamr_alloccmd(struct amr_softc *sc)
126051974Smsmith{
126151974Smsmith    struct amr_command	*ac;
126251974Smsmith
126365245Smsmith    debug_called(3);
126451974Smsmith
126565245Smsmith    ac = amr_dequeue_free(sc);
126651974Smsmith    if (ac == NULL) {
126765245Smsmith	amr_alloccmd_cluster(sc);
126865245Smsmith	ac = amr_dequeue_free(sc);
126951974Smsmith    }
127065245Smsmith    if (ac == NULL)
127165245Smsmith	return(NULL);
127265245Smsmith
127365245Smsmith    /* clear out significant fields */
127465245Smsmith    ac->ac_slot = 0;
127565245Smsmith    ac->ac_status = 0;
127651974Smsmith    bzero(&ac->ac_mailbox, sizeof(struct amr_mailbox));
127765245Smsmith    ac->ac_flags = 0;
127865245Smsmith    ac->ac_bio = NULL;
127965245Smsmith    ac->ac_data = NULL;
128065245Smsmith    ac->ac_ccb_data = NULL;
128165245Smsmith    ac->ac_complete = NULL;
128251974Smsmith    return(ac);
128351974Smsmith}
128451974Smsmith
128551974Smsmith/********************************************************************************
128651974Smsmith * Release a command buffer for recycling.
128751974Smsmith */
128865245Smsmithvoid
128951974Smsmithamr_releasecmd(struct amr_command *ac)
129051974Smsmith{
129165245Smsmith    debug_called(3);
129251974Smsmith
129365245Smsmith    amr_enqueue_free(ac);
129451974Smsmith}
129551974Smsmith
129651974Smsmith/********************************************************************************
129765245Smsmith * Allocate a new command cluster and initialise it.
129851974Smsmith */
129965245Smsmithvoid
130065245Smsmithamr_alloccmd_cluster(struct amr_softc *sc)
130151974Smsmith{
130265245Smsmith    struct amr_command_cluster	*acc;
130365245Smsmith    struct amr_command		*ac;
130465245Smsmith    int				s, i;
130551974Smsmith
130665245Smsmith    acc = malloc(AMR_CMD_CLUSTERSIZE, M_DEVBUF, M_NOWAIT);
130765245Smsmith    if (acc != NULL) {
130865245Smsmith	s = splbio();
130965245Smsmith	TAILQ_INSERT_TAIL(&sc->amr_cmd_clusters, acc, acc_link);
131065245Smsmith	splx(s);
131165245Smsmith	for (i = 0; i < AMR_CMD_CLUSTERCOUNT; i++) {
131265245Smsmith	    ac = &acc->acc_command[i];
131365245Smsmith	    bzero(ac, sizeof(*ac));
131465245Smsmith	    ac->ac_sc = sc;
131565245Smsmith	    if (!bus_dmamap_create(sc->amr_buffer_dmat, 0, &ac->ac_dmamap) &&
131665245Smsmith		!bus_dmamap_create(sc->amr_buffer_dmat, 0, &ac->ac_ccb_dmamap))
131765245Smsmith		amr_releasecmd(ac);
131865245Smsmith	}
131965245Smsmith    }
132051974Smsmith}
132151974Smsmith
132251974Smsmith/********************************************************************************
132365245Smsmith * Free a command cluster
132465245Smsmith */
132565245Smsmithvoid
132665245Smsmithamr_freecmd_cluster(struct amr_command_cluster *acc)
132765245Smsmith{
132865245Smsmith    struct amr_softc	*sc = acc->acc_command[0].ac_sc;
132965245Smsmith    int			i;
133065245Smsmith
133165245Smsmith    for (i = 0; i < AMR_CMD_CLUSTERCOUNT; i++)
133265245Smsmith	bus_dmamap_destroy(sc->amr_buffer_dmat, acc->acc_command[i].ac_dmamap);
133365245Smsmith    free(acc, M_DEVBUF);
133465245Smsmith}
133565245Smsmith
133665245Smsmith/********************************************************************************
133751974Smsmith ********************************************************************************
133851974Smsmith                                                         Interface-specific Shims
133951974Smsmith ********************************************************************************
134051974Smsmith ********************************************************************************/
134151974Smsmith
134251974Smsmith/********************************************************************************
134351974Smsmith * Tell the controller that the mailbox contains a valid command
134451974Smsmith */
134565245Smsmithstatic int
134651974Smsmithamr_quartz_submit_command(struct amr_softc *sc)
134751974Smsmith{
134865245Smsmith    debug_called(3);
134951974Smsmith
135065245Smsmith    if (AMR_QGET_IDB(sc) & AMR_QIDB_SUBMIT)
135165245Smsmith	return(EBUSY);
135251974Smsmith    AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_SUBMIT);
135365245Smsmith    return(0);
135451974Smsmith}
135551974Smsmith
135665245Smsmithstatic int
135751974Smsmithamr_std_submit_command(struct amr_softc *sc)
135851974Smsmith{
135965245Smsmith    debug_called(3);
136051974Smsmith
136165245Smsmith    if (AMR_SGET_MBSTAT(sc) & AMR_SMBOX_BUSYFLAG)
136265245Smsmith	return(EBUSY);
136351974Smsmith    AMR_SPOST_COMMAND(sc);
136465245Smsmith    return(0);
136551974Smsmith}
136651974Smsmith
136751974Smsmith/********************************************************************************
136851974Smsmith * Claim any work that the controller has completed; acknowledge completion,
136951974Smsmith * save details of the completion in (mbsave)
137051974Smsmith */
137151974Smsmithstatic int
137251974Smsmithamr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave)
137351974Smsmith{
137451974Smsmith    int		s, worked;
137551974Smsmith    u_int32_t	outd;
137651974Smsmith
137765245Smsmith    debug_called(3);
137865245Smsmith
137951974Smsmith    worked = 0;
138051974Smsmith    s = splbio();
138151974Smsmith
138251974Smsmith    /* work waiting for us? */
138351974Smsmith    if ((outd = AMR_QGET_ODB(sc)) == AMR_QODB_READY) {
138451974Smsmith
138551974Smsmith	/* save mailbox, which contains a list of completed commands */
138665245Smsmith	bcopy((void *)(uintptr_t)(volatile void *)sc->amr_mailbox, mbsave, sizeof(*mbsave));
138751974Smsmith
138865245Smsmith	/* acknowledge interrupt */
138965245Smsmith	AMR_QPUT_ODB(sc, AMR_QODB_READY);
139065245Smsmith
139151974Smsmith	/* acknowledge that we have the commands */
139251974Smsmith	AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_ACK);
139365245Smsmith
139465763Smsmith#ifndef AMR_QUARTZ_GOFASTER
139565245Smsmith	/*
139665245Smsmith	 * This waits for the controller to notice that we've taken the
139765245Smsmith	 * command from it.  It's very inefficient, and we shouldn't do it,
139865245Smsmith	 * but if we remove this code, we stop completing commands under
139965245Smsmith	 * load.
140065245Smsmith	 *
140165245Smsmith	 * Peter J says we shouldn't do this.  The documentation says we
140265245Smsmith	 * should.  Who is right?
140365245Smsmith	 */
140451974Smsmith	while(AMR_QGET_IDB(sc) & AMR_QIDB_ACK)
140551974Smsmith	    ;				/* XXX aiee! what if it dies? */
140665245Smsmith#endif
140765245Smsmith
140851974Smsmith	worked = 1;			/* got some work */
140951974Smsmith    }
141051974Smsmith
141151974Smsmith    splx(s);
141251974Smsmith    return(worked);
141351974Smsmith}
141451974Smsmith
141551974Smsmithstatic int
141651974Smsmithamr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave)
141751974Smsmith{
141851974Smsmith    int		s, worked;
141951974Smsmith    u_int8_t	istat;
142051974Smsmith
142165245Smsmith    debug_called(3);
142251974Smsmith
142351974Smsmith    worked = 0;
142451974Smsmith    s = splbio();
142551974Smsmith
142651974Smsmith    /* check for valid interrupt status */
142751974Smsmith    istat = AMR_SGET_ISTAT(sc);
142851974Smsmith    if ((istat & AMR_SINTR_VALID) != 0) {
142951974Smsmith	AMR_SPUT_ISTAT(sc, istat);	/* ack interrupt status */
143051974Smsmith
143151974Smsmith	/* save mailbox, which contains a list of completed commands */
143265245Smsmith	bcopy((void *)(uintptr_t)(volatile void *)sc->amr_mailbox, mbsave, sizeof(*mbsave));
143351974Smsmith
143451974Smsmith	AMR_SACK_INTERRUPT(sc);		/* acknowledge we have the mailbox */
143551974Smsmith	worked = 1;
143651974Smsmith    }
143751974Smsmith
143851974Smsmith    splx(s);
143951974Smsmith    return(worked);
144051974Smsmith}
144151974Smsmith
144251974Smsmith/********************************************************************************
144351974Smsmith * Notify the controller of the mailbox location.
144451974Smsmith */
144551974Smsmithstatic void
144651974Smsmithamr_std_attach_mailbox(struct amr_softc *sc)
144751974Smsmith{
144851974Smsmith
144951974Smsmith    /* program the mailbox physical address */
145051974Smsmith    AMR_SBYTE_SET(sc, AMR_SMBOX_0, sc->amr_mailboxphys         & 0xff);
145151974Smsmith    AMR_SBYTE_SET(sc, AMR_SMBOX_1, (sc->amr_mailboxphys >>  8) & 0xff);
145251974Smsmith    AMR_SBYTE_SET(sc, AMR_SMBOX_2, (sc->amr_mailboxphys >> 16) & 0xff);
145351974Smsmith    AMR_SBYTE_SET(sc, AMR_SMBOX_3, (sc->amr_mailboxphys >> 24) & 0xff);
145451974Smsmith    AMR_SBYTE_SET(sc, AMR_SMBOX_ENABLE, AMR_SMBOX_ADDR);
145551974Smsmith
145651974Smsmith    /* clear any outstanding interrupt and enable interrupts proper */
145751974Smsmith    AMR_SACK_INTERRUPT(sc);
145851974Smsmith    AMR_SENABLE_INTR(sc);
145951974Smsmith}
146051974Smsmith
146165245Smsmith#ifdef AMR_BOARD_INIT
146251974Smsmith/********************************************************************************
146365245Smsmith * Initialise the controller
146465245Smsmith */
146565245Smsmithstatic int
146665245Smsmithamr_quartz_init(struct amr_softc *sc)
146765245Smsmith{
146865245Smsmith    int		status, ostatus;
146965245Smsmith
147065245Smsmith    device_printf(sc->amr_dev, "initial init status %x\n", AMR_QGET_INITSTATUS(sc));
147165245Smsmith
147265245Smsmith    AMR_QRESET(sc);
147365245Smsmith
147465245Smsmith    ostatus = 0xff;
147565245Smsmith    while ((status = AMR_QGET_INITSTATUS(sc)) != AMR_QINIT_DONE) {
147665245Smsmith	if (status != ostatus) {
147765245Smsmith	    device_printf(sc->amr_dev, "(%x) %s\n", status, amr_describe_code(amr_table_qinit, status));
147865245Smsmith	    ostatus = status;
147965245Smsmith	}
148065245Smsmith	switch (status) {
148165245Smsmith	case AMR_QINIT_NOMEM:
148265245Smsmith	    return(ENOMEM);
148365245Smsmith
148465245Smsmith	case AMR_QINIT_SCAN:
148565245Smsmith	    /* XXX we could print channel/target here */
148665245Smsmith	    break;
148765245Smsmith	}
148865245Smsmith    }
148965245Smsmith    return(0);
149065245Smsmith}
149165245Smsmith
149265245Smsmithstatic int
149365245Smsmithamr_std_init(struct amr_softc *sc)
149465245Smsmith{
149565245Smsmith    int		status, ostatus;
149665245Smsmith
149765245Smsmith    device_printf(sc->amr_dev, "initial init status %x\n", AMR_SGET_INITSTATUS(sc));
149865245Smsmith
149965245Smsmith    AMR_SRESET(sc);
150065245Smsmith
150165245Smsmith    ostatus = 0xff;
150265245Smsmith    while ((status = AMR_SGET_INITSTATUS(sc)) != AMR_SINIT_DONE) {
150365245Smsmith	if (status != ostatus) {
150465245Smsmith	    device_printf(sc->amr_dev, "(%x) %s\n", status, amr_describe_code(amr_table_sinit, status));
150565245Smsmith	    ostatus = status;
150665245Smsmith	}
150765245Smsmith	switch (status) {
150865245Smsmith	case AMR_SINIT_NOMEM:
150965245Smsmith	    return(ENOMEM);
151065245Smsmith
151165245Smsmith	case AMR_SINIT_INPROG:
151265245Smsmith	    /* XXX we could print channel/target here? */
151365245Smsmith	    break;
151465245Smsmith	}
151565245Smsmith    }
151665245Smsmith    return(0);
151765245Smsmith}
151865245Smsmith#endif
151965245Smsmith
152065245Smsmith/********************************************************************************
152151974Smsmith ********************************************************************************
152251974Smsmith                                                                        Debugging
152351974Smsmith ********************************************************************************
152451974Smsmith ********************************************************************************/
152551974Smsmith
152651974Smsmith/********************************************************************************
152765245Smsmith * Identify the controller and print some information about it.
152865245Smsmith */
152965245Smsmithstatic void
153065245Smsmithamr_describe_controller(struct amr_softc *sc)
153165245Smsmith{
153265245Smsmith    struct amr_prodinfo	*ap;
153365245Smsmith    struct amr_enquiry	*ae;
153465245Smsmith    char		*prod;
153565245Smsmith
153665245Smsmith    /*
153765245Smsmith     * Try to get 40LD product info, which tells us what the card is labelled as.
153865245Smsmith     */
153965245Smsmith    if ((ap = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0)) != NULL) {
154065245Smsmith	device_printf(sc->amr_dev, "<%.80s> Firmware %.16s, BIOS %.16s, %dMB RAM\n",
154165245Smsmith		      ap->ap_product, ap->ap_firmware, ap->ap_bios,
154265245Smsmith		      ap->ap_memsize);
154365245Smsmith
154465245Smsmith	free(ap, M_DEVBUF);
154565245Smsmith	return;
154665245Smsmith    }
154765245Smsmith
154865245Smsmith    /*
154965245Smsmith     * Try 8LD extended ENQUIRY to get controller signature, and use lookup table.
155065245Smsmith     */
155165245Smsmith    if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_EXT_ENQUIRY2, 0, 0)) != NULL) {
155265245Smsmith	prod = amr_describe_code(amr_table_adaptertype, ae->ae_signature);
155365245Smsmith
155465245Smsmith    } else if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0)) != NULL) {
155565245Smsmith
155665245Smsmith	/*
155765245Smsmith	 * Try to work it out based on the PCI signatures.
155865245Smsmith	 */
155965245Smsmith	switch (pci_get_device(sc->amr_dev)) {
156065245Smsmith	case 0x9010:
156165245Smsmith	    prod = "Series 428";
156265245Smsmith	    break;
156365245Smsmith	case 0x9060:
156465245Smsmith	    prod = "Series 434";
156565245Smsmith	    break;
156665245Smsmith	default:
156765245Smsmith	    prod = "unknown controller";
156865245Smsmith	    break;
156965245Smsmith	}
157065245Smsmith    } else {
157165245Smsmith	prod = "unsupported controller";
157265245Smsmith    }
157365245Smsmith    device_printf(sc->amr_dev, "<%s> Firmware %.4s, BIOS %.4s, %dMB RAM\n",
157465245Smsmith		  prod, ae->ae_adapter.aa_firmware, ae->ae_adapter.aa_bios,
157565245Smsmith		  ae->ae_adapter.aa_memorysize);
157665245Smsmith    free(ae, M_DEVBUF);
157765245Smsmith}
157865245Smsmith
157965245Smsmith#ifdef AMR_DEBUG
158065245Smsmith/********************************************************************************
158151974Smsmith * Print the command (ac) in human-readable format
158251974Smsmith */
158351974Smsmithstatic void
158451974Smsmithamr_printcommand(struct amr_command *ac)
158551974Smsmith{
158651974Smsmith    struct amr_softc	*sc = ac->ac_sc;
158751974Smsmith    struct amr_sgentry	*sg;
158851974Smsmith    int			i;
158951974Smsmith
159051974Smsmith    device_printf(sc->amr_dev, "cmd %x  ident %d  drive %d\n",
159151974Smsmith		  ac->ac_mailbox.mb_command, ac->ac_mailbox.mb_ident, ac->ac_mailbox.mb_drive);
159251974Smsmith    device_printf(sc->amr_dev, "blkcount %d  lba %d\n",
159351974Smsmith		  ac->ac_mailbox.mb_blkcount, ac->ac_mailbox.mb_lba);
159454512Speter    device_printf(sc->amr_dev, "virtaddr %p  length %lu\n", ac->ac_data, (unsigned long)ac->ac_length);
159558883Smsmith    device_printf(sc->amr_dev, "sg physaddr %08x  nsg %d\n",
159651974Smsmith		  ac->ac_mailbox.mb_physaddr, ac->ac_mailbox.mb_nsgelem);
159765245Smsmith    device_printf(sc->amr_dev, "ccb %p  bio %p\n", ac->ac_ccb_data, ac->ac_bio);
159851974Smsmith
159951974Smsmith    /* get base address of s/g table */
160051974Smsmith    sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
160151974Smsmith    for (i = 0; i < ac->ac_mailbox.mb_nsgelem; i++, sg++)
160251974Smsmith	device_printf(sc->amr_dev, "  %x/%d\n", sg->sg_addr, sg->sg_count);
160351974Smsmith}
160465245Smsmith#endif
1605