xref: /freebsd/sys/dev/mlx/mlx.c (revision b04e4c122bad756dc4fa92b36eb8e6366e3d9141)
11ac4b82bSMike Smith /*-
21ac4b82bSMike Smith  * Copyright (c) 1999 Michael Smith
31ac4b82bSMike Smith  * All rights reserved.
41ac4b82bSMike Smith  *
51ac4b82bSMike Smith  * Redistribution and use in source and binary forms, with or without
61ac4b82bSMike Smith  * modification, are permitted provided that the following conditions
71ac4b82bSMike Smith  * are met:
81ac4b82bSMike Smith  * 1. Redistributions of source code must retain the above copyright
91ac4b82bSMike Smith  *    notice, this list of conditions and the following disclaimer.
101ac4b82bSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
111ac4b82bSMike Smith  *    notice, this list of conditions and the following disclaimer in the
121ac4b82bSMike Smith  *    documentation and/or other materials provided with the distribution.
131ac4b82bSMike Smith  *
141ac4b82bSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151ac4b82bSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161ac4b82bSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171ac4b82bSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181ac4b82bSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191ac4b82bSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201ac4b82bSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211ac4b82bSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221ac4b82bSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231ac4b82bSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241ac4b82bSMike Smith  * SUCH DAMAGE.
251ac4b82bSMike Smith  *
261ac4b82bSMike Smith  *	$FreeBSD$
271ac4b82bSMike Smith  */
281ac4b82bSMike Smith 
291ac4b82bSMike Smith /*
301ac4b82bSMike Smith  * Driver for the Mylex DAC960 family of RAID controllers.
311ac4b82bSMike Smith  */
321ac4b82bSMike Smith 
331ac4b82bSMike Smith #include <sys/param.h>
341ac4b82bSMike Smith #include <sys/systm.h>
35*b04e4c12SJohn Baldwin #include <sys/bio.h>
360fca6f8bSJohn Baldwin #include <sys/lock.h>
371ac4b82bSMike Smith #include <sys/malloc.h>
380fca6f8bSJohn Baldwin #include <sys/mutex.h>
391ac4b82bSMike Smith #include <sys/kernel.h>
400fca6f8bSJohn Baldwin #include <sys/sx.h>
411ac4b82bSMike Smith 
421ac4b82bSMike Smith #include <sys/bus.h>
431ac4b82bSMike Smith #include <sys/conf.h>
44da8bb3a3SMike Smith #include <sys/stat.h>
451ac4b82bSMike Smith 
461ac4b82bSMike Smith #include <machine/resource.h>
471ac4b82bSMike Smith #include <machine/bus.h>
481ac4b82bSMike Smith #include <machine/clock.h>
491ac4b82bSMike Smith #include <sys/rman.h>
501ac4b82bSMike Smith 
51891619a6SPoul-Henning Kamp #include <geom/geom_disk.h>
52891619a6SPoul-Henning Kamp 
531ac4b82bSMike Smith #include <dev/mlx/mlxio.h>
541ac4b82bSMike Smith #include <dev/mlx/mlxvar.h>
551ac4b82bSMike Smith #include <dev/mlx/mlxreg.h>
561ac4b82bSMike Smith 
571ac4b82bSMike Smith static struct cdevsw mlx_cdevsw = {
58dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
597ac40f5fSPoul-Henning Kamp 	.d_open =	mlx_open,
607ac40f5fSPoul-Henning Kamp 	.d_close =	mlx_close,
617ac40f5fSPoul-Henning Kamp 	.d_ioctl =	mlx_ioctl,
627ac40f5fSPoul-Henning Kamp 	.d_name =	"mlx",
631ac4b82bSMike Smith };
641ac4b82bSMike Smith 
651ac4b82bSMike Smith devclass_t	mlx_devclass;
661ac4b82bSMike Smith 
671ac4b82bSMike Smith /*
681ac4b82bSMike Smith  * Per-interface accessor methods
691ac4b82bSMike Smith  */
701ac4b82bSMike Smith static int			mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
711ac4b82bSMike Smith static int			mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
721ac4b82bSMike Smith static void			mlx_v3_intaction(struct mlx_softc *sc, int action);
730fca6f8bSJohn Baldwin static int			mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first);
741ac4b82bSMike Smith 
75f6b84b08SMike Smith static int			mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
76f6b84b08SMike Smith static int			mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
77f6b84b08SMike Smith static void			mlx_v4_intaction(struct mlx_softc *sc, int action);
780fca6f8bSJohn Baldwin static int			mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first);
79f6b84b08SMike Smith 
805792b7feSMike Smith static int			mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
815792b7feSMike Smith static int			mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
825792b7feSMike Smith static void			mlx_v5_intaction(struct mlx_softc *sc, int action);
830fca6f8bSJohn Baldwin static int			mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first);
845792b7feSMike Smith 
851ac4b82bSMike Smith /*
861ac4b82bSMike Smith  * Status monitoring
871ac4b82bSMike Smith  */
881ac4b82bSMike Smith static void			mlx_periodic(void *data);
891ac4b82bSMike Smith static void			mlx_periodic_enquiry(struct mlx_command *mc);
901ac4b82bSMike Smith static void			mlx_periodic_eventlog_poll(struct mlx_softc *sc);
911ac4b82bSMike Smith static void			mlx_periodic_eventlog_respond(struct mlx_command *mc);
921ac4b82bSMike Smith static void			mlx_periodic_rebuild(struct mlx_command *mc);
931ac4b82bSMike Smith 
941ac4b82bSMike Smith /*
951ac4b82bSMike Smith  * Channel Pause
961ac4b82bSMike Smith  */
971ac4b82bSMike Smith static void			mlx_pause_action(struct mlx_softc *sc);
981ac4b82bSMike Smith static void			mlx_pause_done(struct mlx_command *mc);
991ac4b82bSMike Smith 
1001ac4b82bSMike Smith /*
1011ac4b82bSMike Smith  * Command submission.
1021ac4b82bSMike Smith  */
1031ac4b82bSMike Smith static void			*mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize,
1041ac4b82bSMike Smith 					     void (*complete)(struct mlx_command *mc));
1051ac4b82bSMike Smith static int			mlx_flush(struct mlx_softc *sc);
106421f2f7dSMike Smith static int			mlx_check(struct mlx_softc *sc, int drive);
1071ac4b82bSMike Smith static int			mlx_rebuild(struct mlx_softc *sc, int channel, int target);
1081ac4b82bSMike Smith static int			mlx_wait_command(struct mlx_command *mc);
1091ac4b82bSMike Smith static int			mlx_poll_command(struct mlx_command *mc);
1101b4404f9SScott Long void				mlx_startio_cb(void *arg,
1111b4404f9SScott Long 					       bus_dma_segment_t *segs,
1121b4404f9SScott Long 					       int nsegments, int error);
1131ac4b82bSMike Smith static void			mlx_startio(struct mlx_softc *sc);
1141ac4b82bSMike Smith static void			mlx_completeio(struct mlx_command *mc);
1151b4404f9SScott Long static int			mlx_user_command(struct mlx_softc *sc,
1161b4404f9SScott Long 						 struct mlx_usercommand *mu);
1171b4404f9SScott Long void				mlx_user_cb(void *arg, bus_dma_segment_t *segs,
1181b4404f9SScott Long 					    int nsegments, int error);
1191ac4b82bSMike Smith 
1201ac4b82bSMike Smith /*
1211ac4b82bSMike Smith  * Command buffer allocation.
1221ac4b82bSMike Smith  */
1231ac4b82bSMike Smith static struct mlx_command	*mlx_alloccmd(struct mlx_softc *sc);
1241ac4b82bSMike Smith static void			mlx_releasecmd(struct mlx_command *mc);
1251ac4b82bSMike Smith static void			mlx_freecmd(struct mlx_command *mc);
1261ac4b82bSMike Smith 
1271ac4b82bSMike Smith /*
1281ac4b82bSMike Smith  * Command management.
1291ac4b82bSMike Smith  */
1301ac4b82bSMike Smith static int			mlx_getslot(struct mlx_command *mc);
1311b4404f9SScott Long static void			mlx_setup_dmamap(struct mlx_command *mc,
1321b4404f9SScott Long 						 bus_dma_segment_t *segs,
1331b4404f9SScott Long 						 int nsegments, int error);
1341ac4b82bSMike Smith static void			mlx_unmapcmd(struct mlx_command *mc);
1350fca6f8bSJohn Baldwin static int			mlx_shutdown_locked(struct mlx_softc *sc);
1361ac4b82bSMike Smith static int			mlx_start(struct mlx_command *mc);
1370fca6f8bSJohn Baldwin static int			mlx_done(struct mlx_softc *sc, int startio);
1381ac4b82bSMike Smith static void			mlx_complete(struct mlx_softc *sc);
1391ac4b82bSMike Smith 
1401ac4b82bSMike Smith /*
1411ac4b82bSMike Smith  * Debugging.
1421ac4b82bSMike Smith  */
1431ac4b82bSMike Smith static char			*mlx_diagnose_command(struct mlx_command *mc);
1449eee27f1SMike Smith static void			mlx_describe_controller(struct mlx_softc *sc);
145da8bb3a3SMike Smith static int			mlx_fw_message(struct mlx_softc *sc, int status, int param1, int param2);
1461ac4b82bSMike Smith 
1471ac4b82bSMike Smith /*
1481ac4b82bSMike Smith  * Utility functions.
1491ac4b82bSMike Smith  */
1501ac4b82bSMike Smith static struct mlx_sysdrive	*mlx_findunit(struct mlx_softc *sc, int unit);
1511ac4b82bSMike Smith 
1521ac4b82bSMike Smith /********************************************************************************
1531ac4b82bSMike Smith  ********************************************************************************
1541ac4b82bSMike Smith                                                                 Public Interfaces
1551ac4b82bSMike Smith  ********************************************************************************
1561ac4b82bSMike Smith  ********************************************************************************/
1571ac4b82bSMike Smith 
1581ac4b82bSMike Smith /********************************************************************************
1591ac4b82bSMike Smith  * Free all of the resources associated with (sc)
1601ac4b82bSMike Smith  *
1611ac4b82bSMike Smith  * Should not be called if the controller is active.
1621ac4b82bSMike Smith  */
1631ac4b82bSMike Smith void
1641ac4b82bSMike Smith mlx_free(struct mlx_softc *sc)
1651ac4b82bSMike Smith {
1661ac4b82bSMike Smith     struct mlx_command	*mc;
1671ac4b82bSMike Smith 
168da8bb3a3SMike Smith     debug_called(1);
1691ac4b82bSMike Smith 
1700fca6f8bSJohn Baldwin     /* destroy control device */
1710fca6f8bSJohn Baldwin     if (sc->mlx_dev_t != NULL)
1720fca6f8bSJohn Baldwin 	destroy_dev(sc->mlx_dev_t);
1730fca6f8bSJohn Baldwin 
1740fca6f8bSJohn Baldwin     if (sc->mlx_intr)
1750fca6f8bSJohn Baldwin 	bus_teardown_intr(sc->mlx_dev, sc->mlx_irq, sc->mlx_intr);
1760fca6f8bSJohn Baldwin 
1771ac4b82bSMike Smith     /* cancel status timeout */
1780fca6f8bSJohn Baldwin     MLX_IO_LOCK(sc);
1790fca6f8bSJohn Baldwin     callout_stop(&sc->mlx_timeout);
1801ac4b82bSMike Smith 
1811ac4b82bSMike Smith     /* throw away any command buffers */
1821ac4b82bSMike Smith     while ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL) {
1831ac4b82bSMike Smith 	TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
1841ac4b82bSMike Smith 	mlx_freecmd(mc);
1851ac4b82bSMike Smith     }
1860fca6f8bSJohn Baldwin     MLX_IO_UNLOCK(sc);
1870fca6f8bSJohn Baldwin     callout_drain(&sc->mlx_timeout);
1881ac4b82bSMike Smith 
1891ac4b82bSMike Smith     /* destroy data-transfer DMA tag */
1901ac4b82bSMike Smith     if (sc->mlx_buffer_dmat)
1911ac4b82bSMike Smith 	bus_dma_tag_destroy(sc->mlx_buffer_dmat);
1921ac4b82bSMike Smith 
1931ac4b82bSMike Smith     /* free and destroy DMA memory and tag for s/g lists */
1941ac4b82bSMike Smith     if (sc->mlx_sgtable)
1951ac4b82bSMike Smith 	bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
1961ac4b82bSMike Smith     if (sc->mlx_sg_dmat)
1971ac4b82bSMike Smith 	bus_dma_tag_destroy(sc->mlx_sg_dmat);
1981ac4b82bSMike Smith 
1991ac4b82bSMike Smith     /* disconnect the interrupt handler */
2001ac4b82bSMike Smith     if (sc->mlx_irq != NULL)
2011ac4b82bSMike Smith 	bus_release_resource(sc->mlx_dev, SYS_RES_IRQ, 0, sc->mlx_irq);
2021ac4b82bSMike Smith 
2031ac4b82bSMike Smith     /* destroy the parent DMA tag */
2041ac4b82bSMike Smith     if (sc->mlx_parent_dmat)
2051ac4b82bSMike Smith 	bus_dma_tag_destroy(sc->mlx_parent_dmat);
2061ac4b82bSMike Smith 
2071ac4b82bSMike Smith     /* release the register window mapping */
2081ac4b82bSMike Smith     if (sc->mlx_mem != NULL)
2099b11c7baSMatthew N. Dodd 	bus_release_resource(sc->mlx_dev, sc->mlx_mem_type, sc->mlx_mem_rid, sc->mlx_mem);
2109eee27f1SMike Smith 
2119eee27f1SMike Smith     /* free controller enquiry data */
2129eee27f1SMike Smith     if (sc->mlx_enq2 != NULL)
2139eee27f1SMike Smith 	free(sc->mlx_enq2, M_DEVBUF);
214da8bb3a3SMike Smith 
2150fca6f8bSJohn Baldwin     sx_destroy(&sc->mlx_config_lock);
2160fca6f8bSJohn Baldwin     mtx_destroy(&sc->mlx_io_lock);
2171ac4b82bSMike Smith }
2181ac4b82bSMike Smith 
2191ac4b82bSMike Smith /********************************************************************************
2201ac4b82bSMike Smith  * Map the scatter/gather table into bus space
2211ac4b82bSMike Smith  */
2221ac4b82bSMike Smith static void
2231ac4b82bSMike Smith mlx_dma_map_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2241ac4b82bSMike Smith {
2251ac4b82bSMike Smith     struct mlx_softc	*sc = (struct mlx_softc *)arg;
2261ac4b82bSMike Smith 
227da8bb3a3SMike Smith     debug_called(1);
2281ac4b82bSMike Smith 
2291ac4b82bSMike Smith     /* save base of s/g table's address in bus space */
2301ac4b82bSMike Smith     sc->mlx_sgbusaddr = segs->ds_addr;
2311ac4b82bSMike Smith }
2321ac4b82bSMike Smith 
2331ac4b82bSMike Smith static int
2341ac4b82bSMike Smith mlx_sglist_map(struct mlx_softc *sc)
2351ac4b82bSMike Smith {
2361ac4b82bSMike Smith     size_t	segsize;
237baff09dbSMike Smith     int		error, ncmd;
2381ac4b82bSMike Smith 
239da8bb3a3SMike Smith     debug_called(1);
2401ac4b82bSMike Smith 
2411ac4b82bSMike Smith     /* destroy any existing mappings */
2421ac4b82bSMike Smith     if (sc->mlx_sgtable)
2431ac4b82bSMike Smith 	bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
2441ac4b82bSMike Smith     if (sc->mlx_sg_dmat)
2451ac4b82bSMike Smith 	bus_dma_tag_destroy(sc->mlx_sg_dmat);
2461ac4b82bSMike Smith 
2471ac4b82bSMike Smith     /*
2481ac4b82bSMike Smith      * Create a single tag describing a region large enough to hold all of
249baff09dbSMike Smith      * the s/g lists we will need.  If we're called early on, we don't know how
250baff09dbSMike Smith      * many commands we're going to be asked to support, so only allocate enough
251baff09dbSMike Smith      * for a couple.
2521ac4b82bSMike Smith      */
253baff09dbSMike Smith     if (sc->mlx_enq2 == NULL) {
254baff09dbSMike Smith 	ncmd = 2;
255baff09dbSMike Smith     } else {
256baff09dbSMike Smith 	ncmd = sc->mlx_enq2->me_max_commands;
257baff09dbSMike Smith     }
258baff09dbSMike Smith     segsize = sizeof(struct mlx_sgentry) * MLX_NSEG * ncmd;
2591ac4b82bSMike Smith     error = bus_dma_tag_create(sc->mlx_parent_dmat, 	/* parent */
2601ac4b82bSMike Smith 			       1, 0, 			/* alignment,boundary */
2611ac4b82bSMike Smith 			       BUS_SPACE_MAXADDR,	/* lowaddr */
2621ac4b82bSMike Smith 			       BUS_SPACE_MAXADDR, 	/* highaddr */
2631ac4b82bSMike Smith 			       NULL, NULL, 		/* filter, filterarg */
2641ac4b82bSMike Smith 			       segsize, 1,		/* maxsize, nsegments */
2651ac4b82bSMike Smith 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
2661ac4b82bSMike Smith 			       0,			/* flags */
267fc3e87b3SScott Long 			       NULL, NULL,		/* lockfunc, lockarg */
2681ac4b82bSMike Smith 			       &sc->mlx_sg_dmat);
2691ac4b82bSMike Smith     if (error != 0) {
2701ac4b82bSMike Smith 	device_printf(sc->mlx_dev, "can't allocate scatter/gather DMA tag\n");
2711ac4b82bSMike Smith 	return(ENOMEM);
2721ac4b82bSMike Smith     }
2731ac4b82bSMike Smith 
2741ac4b82bSMike Smith     /*
2751ac4b82bSMike Smith      * Allocate enough s/g maps for all commands and permanently map them into
2761ac4b82bSMike Smith      * controller-visible space.
2771ac4b82bSMike Smith      *
2781ac4b82bSMike Smith      * XXX this assumes we can get enough space for all the s/g maps in one
279fc3e87b3SScott Long      * contiguous slab.  We may need to switch to a more complex arrangement
280fc3e87b3SScott Long      * where we allocate in smaller chunks and keep a lookup table from slot
281fc3e87b3SScott Long      * to bus address.
2821ac4b82bSMike Smith      */
283fc3e87b3SScott Long     error = bus_dmamem_alloc(sc->mlx_sg_dmat, (void **)&sc->mlx_sgtable,
284fc3e87b3SScott Long 			     BUS_DMA_NOWAIT, &sc->mlx_sg_dmamap);
2851ac4b82bSMike Smith     if (error) {
2861ac4b82bSMike Smith 	device_printf(sc->mlx_dev, "can't allocate s/g table\n");
2871ac4b82bSMike Smith 	return(ENOMEM);
2881ac4b82bSMike Smith     }
2891b4404f9SScott Long     (void)bus_dmamap_load(sc->mlx_sg_dmat, sc->mlx_sg_dmamap, sc->mlx_sgtable,
290fc3e87b3SScott Long 			  segsize, mlx_dma_map_sg, sc, 0);
2911ac4b82bSMike Smith     return(0);
2921ac4b82bSMike Smith }
2931ac4b82bSMike Smith 
2941ac4b82bSMike Smith /********************************************************************************
2951ac4b82bSMike Smith  * Initialise the controller and softc
2961ac4b82bSMike Smith  */
2971ac4b82bSMike Smith int
2981ac4b82bSMike Smith mlx_attach(struct mlx_softc *sc)
2991ac4b82bSMike Smith {
300da8bb3a3SMike Smith     struct mlx_enquiry_old	*meo;
301da8bb3a3SMike Smith     int				rid, error, fwminor, hscode, hserror, hsparam1, hsparam2, hsmsg;
3021ac4b82bSMike Smith 
303da8bb3a3SMike Smith     debug_called(1);
3041ac4b82bSMike Smith 
3051ac4b82bSMike Smith     /*
3061ac4b82bSMike Smith      * Initialise per-controller queues.
3071ac4b82bSMike Smith      */
3084b006d7bSMike Smith     TAILQ_INIT(&sc->mlx_work);
3091ac4b82bSMike Smith     TAILQ_INIT(&sc->mlx_freecmds);
310*b04e4c12SJohn Baldwin     bioq_init(&sc->mlx_bioq);
3111ac4b82bSMike Smith 
3121ac4b82bSMike Smith     /*
3131ac4b82bSMike Smith      * Select accessor methods based on controller interface type.
3141ac4b82bSMike Smith      */
3151ac4b82bSMike Smith     switch(sc->mlx_iftype) {
316da8bb3a3SMike Smith     case MLX_IFTYPE_2:
3171ac4b82bSMike Smith     case MLX_IFTYPE_3:
3181ac4b82bSMike Smith 	sc->mlx_tryqueue	= mlx_v3_tryqueue;
3191ac4b82bSMike Smith 	sc->mlx_findcomplete	= mlx_v3_findcomplete;
3201ac4b82bSMike Smith 	sc->mlx_intaction	= mlx_v3_intaction;
321da8bb3a3SMike Smith 	sc->mlx_fw_handshake	= mlx_v3_fw_handshake;
3221ac4b82bSMike Smith 	break;
323f6b84b08SMike Smith     case MLX_IFTYPE_4:
324f6b84b08SMike Smith 	sc->mlx_tryqueue	= mlx_v4_tryqueue;
325f6b84b08SMike Smith 	sc->mlx_findcomplete	= mlx_v4_findcomplete;
326f6b84b08SMike Smith 	sc->mlx_intaction	= mlx_v4_intaction;
327da8bb3a3SMike Smith 	sc->mlx_fw_handshake	= mlx_v4_fw_handshake;
328f6b84b08SMike Smith 	break;
3295792b7feSMike Smith     case MLX_IFTYPE_5:
3305792b7feSMike Smith 	sc->mlx_tryqueue	= mlx_v5_tryqueue;
3315792b7feSMike Smith 	sc->mlx_findcomplete	= mlx_v5_findcomplete;
3325792b7feSMike Smith 	sc->mlx_intaction	= mlx_v5_intaction;
333da8bb3a3SMike Smith 	sc->mlx_fw_handshake	= mlx_v5_fw_handshake;
3345792b7feSMike Smith 	break;
3351ac4b82bSMike Smith     default:
3361ac4b82bSMike Smith 	return(ENXIO);		/* should never happen */
3371ac4b82bSMike Smith     }
3381ac4b82bSMike Smith 
3391ac4b82bSMike Smith     /* disable interrupts before we start talking to the controller */
3400fca6f8bSJohn Baldwin     MLX_IO_LOCK(sc);
3411ac4b82bSMike Smith     sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
3420fca6f8bSJohn Baldwin     MLX_IO_UNLOCK(sc);
3431ac4b82bSMike Smith 
3441ac4b82bSMike Smith     /*
345da8bb3a3SMike Smith      * Wait for the controller to come ready, handshake with the firmware if required.
346da8bb3a3SMike Smith      * This is typically only necessary on platforms where the controller BIOS does not
347da8bb3a3SMike Smith      * run.
348da8bb3a3SMike Smith      */
349da8bb3a3SMike Smith     hsmsg = 0;
350da8bb3a3SMike Smith     DELAY(1000);
3510fca6f8bSJohn Baldwin     while ((hscode = sc->mlx_fw_handshake(sc, &hserror, &hsparam1, &hsparam2,
3520fca6f8bSJohn Baldwin 	hsmsg == 0)) != 0) {
353da8bb3a3SMike Smith 	/* report first time around... */
354da8bb3a3SMike Smith 	if (hsmsg == 0) {
355da8bb3a3SMike Smith 	    device_printf(sc->mlx_dev, "controller initialisation in progress...\n");
356da8bb3a3SMike Smith 	    hsmsg = 1;
357da8bb3a3SMike Smith 	}
358da8bb3a3SMike Smith 	/* did we get a real message? */
359da8bb3a3SMike Smith 	if (hscode == 2) {
360da8bb3a3SMike Smith 	    hscode = mlx_fw_message(sc, hserror, hsparam1, hsparam2);
361da8bb3a3SMike Smith 	    /* fatal initialisation error? */
362da8bb3a3SMike Smith 	    if (hscode != 0) {
363da8bb3a3SMike Smith 		return(ENXIO);
364da8bb3a3SMike Smith 	    }
365da8bb3a3SMike Smith 	}
366da8bb3a3SMike Smith     }
367da8bb3a3SMike Smith     if (hsmsg == 1)
368da8bb3a3SMike Smith 	device_printf(sc->mlx_dev, "initialisation complete.\n");
369da8bb3a3SMike Smith 
370da8bb3a3SMike Smith     /*
3711ac4b82bSMike Smith      * Allocate and connect our interrupt.
3721ac4b82bSMike Smith      */
3731ac4b82bSMike Smith     rid = 0;
3745f96beb9SNate Lawson     sc->mlx_irq = bus_alloc_resource_any(sc->mlx_dev, SYS_RES_IRQ, &rid,
3755f96beb9SNate Lawson         RF_SHAREABLE | RF_ACTIVE);
3761ac4b82bSMike Smith     if (sc->mlx_irq == NULL) {
377421f2f7dSMike Smith 	device_printf(sc->mlx_dev, "can't allocate interrupt\n");
3781ac4b82bSMike Smith 	return(ENXIO);
3791ac4b82bSMike Smith     }
3800fca6f8bSJohn Baldwin     error = bus_setup_intr(sc->mlx_dev, sc->mlx_irq, INTR_TYPE_BIO |
3810fca6f8bSJohn Baldwin 	INTR_ENTROPY | INTR_MPSAFE, NULL, mlx_intr, sc, &sc->mlx_intr);
3821ac4b82bSMike Smith     if (error) {
383421f2f7dSMike Smith 	device_printf(sc->mlx_dev, "can't set up interrupt\n");
3841ac4b82bSMike Smith 	return(ENXIO);
3851ac4b82bSMike Smith     }
3861ac4b82bSMike Smith 
3871ac4b82bSMike Smith     /*
3881ac4b82bSMike Smith      * Create DMA tag for mapping buffers into controller-addressable space.
3891ac4b82bSMike Smith      */
3901ac4b82bSMike Smith     error = bus_dma_tag_create(sc->mlx_parent_dmat, 	/* parent */
3911b4404f9SScott Long 			       1, 0, 			/* align, boundary */
3921ac4b82bSMike Smith 			       BUS_SPACE_MAXADDR,	/* lowaddr */
3931ac4b82bSMike Smith 			       BUS_SPACE_MAXADDR, 	/* highaddr */
3941ac4b82bSMike Smith 			       NULL, NULL, 		/* filter, filterarg */
395baff09dbSMike Smith 			       MAXBSIZE, MLX_NSEG,	/* maxsize, nsegments */
3961ac4b82bSMike Smith 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
3971ac4b82bSMike Smith 			       0,			/* flags */
398f6b1c44dSScott Long 			       busdma_lock_mutex,	/* lockfunc */
3990fca6f8bSJohn Baldwin 			       &sc->mlx_io_lock,	/* lockarg */
4001ac4b82bSMike Smith 			       &sc->mlx_buffer_dmat);
4011ac4b82bSMike Smith     if (error != 0) {
4021ac4b82bSMike Smith 	device_printf(sc->mlx_dev, "can't allocate buffer DMA tag\n");
4031ac4b82bSMike Smith 	return(ENOMEM);
4041ac4b82bSMike Smith     }
4051ac4b82bSMike Smith 
4061ac4b82bSMike Smith     /*
4071b4404f9SScott Long      * Create some initial scatter/gather mappings so we can run the probe
4081b4404f9SScott Long      * commands.
4091ac4b82bSMike Smith      */
4101ac4b82bSMike Smith     error = mlx_sglist_map(sc);
4111ac4b82bSMike Smith     if (error != 0) {
412421f2f7dSMike Smith 	device_printf(sc->mlx_dev, "can't make initial s/g list mapping\n");
4131ac4b82bSMike Smith 	return(error);
4141ac4b82bSMike Smith     }
4151ac4b82bSMike Smith 
416baff09dbSMike Smith     /*
417baff09dbSMike Smith      * We don't (yet) know where the event log is up to.
418baff09dbSMike Smith      */
419baff09dbSMike Smith     sc->mlx_currevent = -1;
420baff09dbSMike Smith 
421baff09dbSMike Smith     /*
422baff09dbSMike Smith      * Obtain controller feature information
423baff09dbSMike Smith      */
4240fca6f8bSJohn Baldwin     MLX_IO_LOCK(sc);
4259eee27f1SMike Smith     if ((sc->mlx_enq2 = mlx_enquire(sc, MLX_CMD_ENQUIRY2, sizeof(struct mlx_enquiry2), NULL)) == NULL) {
4260fca6f8bSJohn Baldwin 	MLX_IO_UNLOCK(sc);
4271ac4b82bSMike Smith 	device_printf(sc->mlx_dev, "ENQUIRY2 failed\n");
4281ac4b82bSMike Smith 	return(ENXIO);
4291ac4b82bSMike Smith     }
4301ac4b82bSMike Smith 
4319eee27f1SMike Smith     /*
4321ac4b82bSMike Smith      * Do quirk/feature related things.
4331ac4b82bSMike Smith      */
4349eee27f1SMike Smith     fwminor = (sc->mlx_enq2->me_firmware_id >> 8) & 0xff;
4351ac4b82bSMike Smith     switch(sc->mlx_iftype) {
436da8bb3a3SMike Smith     case MLX_IFTYPE_2:
437da8bb3a3SMike Smith 	/* These controllers don't report the firmware version in the ENQUIRY2 response */
438da8bb3a3SMike Smith 	if ((meo = mlx_enquire(sc, MLX_CMD_ENQUIRY_OLD, sizeof(struct mlx_enquiry_old), NULL)) == NULL) {
4390fca6f8bSJohn Baldwin 	    MLX_IO_UNLOCK(sc);
440da8bb3a3SMike Smith 	    device_printf(sc->mlx_dev, "ENQUIRY_OLD failed\n");
441da8bb3a3SMike Smith 	    return(ENXIO);
442da8bb3a3SMike Smith 	}
443da8bb3a3SMike Smith 	sc->mlx_enq2->me_firmware_id = ('0' << 24) | (0 << 16) | (meo->me_fwminor << 8) | meo->me_fwmajor;
444da8bb3a3SMike Smith 
445da8bb3a3SMike Smith 	/* XXX require 2.42 or better (PCI) or 2.14 or better (EISA) */
446da8bb3a3SMike Smith 	if (meo->me_fwminor < 42) {
447da8bb3a3SMike Smith 	    device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
448da8bb3a3SMike Smith 	    device_printf(sc->mlx_dev, " *** WARNING *** Use revision 2.42 or later\n");
449da8bb3a3SMike Smith 	}
450caa32ef5SColin Percival 	free(meo, M_DEVBUF);
451da8bb3a3SMike Smith 	break;
4521ac4b82bSMike Smith     case MLX_IFTYPE_3:
453f6b84b08SMike Smith 	/* XXX certify 3.52? */
4549eee27f1SMike Smith 	if (fwminor < 51) {
4554b006d7bSMike Smith 	    device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
4564b006d7bSMike Smith 	    device_printf(sc->mlx_dev, " *** WARNING *** Use revision 3.51 or later\n");
4571ac4b82bSMike Smith 	}
4581ac4b82bSMike Smith 	break;
459f6b84b08SMike Smith     case MLX_IFTYPE_4:
460f6b84b08SMike Smith 	/* XXX certify firmware versions? */
4619eee27f1SMike Smith 	if (fwminor < 6) {
4624b006d7bSMike Smith 	    device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
4634b006d7bSMike Smith 	    device_printf(sc->mlx_dev, " *** WARNING *** Use revision 4.06 or later\n");
464f6b84b08SMike Smith 	}
465f6b84b08SMike Smith 	break;
4665792b7feSMike Smith     case MLX_IFTYPE_5:
4679eee27f1SMike Smith 	if (fwminor < 7) {
4685792b7feSMike Smith 	    device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
4695792b7feSMike Smith 	    device_printf(sc->mlx_dev, " *** WARNING *** Use revision 5.07 or later\n");
4705792b7feSMike Smith 	}
4715792b7feSMike Smith 	break;
4721ac4b82bSMike Smith     default:
4730fca6f8bSJohn Baldwin 	MLX_IO_UNLOCK(sc);
4741ac4b82bSMike Smith 	return(ENXIO);		/* should never happen */
4751ac4b82bSMike Smith     }
4760fca6f8bSJohn Baldwin     MLX_IO_UNLOCK(sc);
4771ac4b82bSMike Smith 
4781ac4b82bSMike Smith     /*
479baff09dbSMike Smith      * Create the final scatter/gather mappings now that we have characterised the controller.
4801ac4b82bSMike Smith      */
4811ac4b82bSMike Smith     error = mlx_sglist_map(sc);
4821ac4b82bSMike Smith     if (error != 0) {
483baff09dbSMike Smith 	device_printf(sc->mlx_dev, "can't make final s/g list mapping\n");
4841ac4b82bSMike Smith 	return(error);
4851ac4b82bSMike Smith     }
4861ac4b82bSMike Smith 
4871ac4b82bSMike Smith     /*
488421f2f7dSMike Smith      * No user-requested background operation is in progress.
4891ac4b82bSMike Smith      */
490421f2f7dSMike Smith     sc->mlx_background = 0;
491421f2f7dSMike Smith     sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
4921ac4b82bSMike Smith 
4931ac4b82bSMike Smith     /*
494da8bb3a3SMike Smith      * Create the control device.
4951ac4b82bSMike Smith      */
496e78a5a3fSEd Schouten     sc->mlx_dev_t = make_dev(&mlx_cdevsw, 0, UID_ROOT, GID_OPERATOR,
497da8bb3a3SMike Smith 			     S_IRUSR | S_IWUSR, "mlx%d", device_get_unit(sc->mlx_dev));
498e78a5a3fSEd Schouten     sc->mlx_dev_t->si_drv1 = sc;
4991ac4b82bSMike Smith 
5001ac4b82bSMike Smith     /*
5011ac4b82bSMike Smith      * Start the timeout routine.
5021ac4b82bSMike Smith      */
5030fca6f8bSJohn Baldwin     callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
5041ac4b82bSMike Smith 
505da8bb3a3SMike Smith     /* print a little information about the controller */
506da8bb3a3SMike Smith     mlx_describe_controller(sc);
507da8bb3a3SMike Smith 
5081ac4b82bSMike Smith     return(0);
5091ac4b82bSMike Smith }
5101ac4b82bSMike Smith 
5111ac4b82bSMike Smith /********************************************************************************
5121ac4b82bSMike Smith  * Locate disk resources and attach children to them.
5131ac4b82bSMike Smith  */
5141ac4b82bSMike Smith void
5151ac4b82bSMike Smith mlx_startup(struct mlx_softc *sc)
5161ac4b82bSMike Smith {
5171ac4b82bSMike Smith     struct mlx_enq_sys_drive	*mes;
5181ac4b82bSMike Smith     struct mlx_sysdrive		*dr;
5191ac4b82bSMike Smith     int				i, error;
5201ac4b82bSMike Smith 
521da8bb3a3SMike Smith     debug_called(1);
5221ac4b82bSMike Smith 
5231ac4b82bSMike Smith     /*
5241ac4b82bSMike Smith      * Scan all the system drives and attach children for those that
5251ac4b82bSMike Smith      * don't currently have them.
5261ac4b82bSMike Smith      */
5270fca6f8bSJohn Baldwin     MLX_IO_LOCK(sc);
5281ac4b82bSMike Smith     mes = mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(*mes) * MLX_MAXDRIVES, NULL);
5290fca6f8bSJohn Baldwin     MLX_IO_UNLOCK(sc);
5301ac4b82bSMike Smith     if (mes == NULL) {
5319eee27f1SMike Smith 	device_printf(sc->mlx_dev, "error fetching drive status\n");
5321ac4b82bSMike Smith 	return;
5331ac4b82bSMike Smith     }
5341ac4b82bSMike Smith 
5351ac4b82bSMike Smith     /* iterate over drives returned */
5360fca6f8bSJohn Baldwin     MLX_CONFIG_LOCK(sc);
5371ac4b82bSMike Smith     for (i = 0, dr = &sc->mlx_sysdrive[0];
5381ac4b82bSMike Smith 	 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
5391ac4b82bSMike Smith 	 i++, dr++) {
5401ac4b82bSMike Smith 	/* are we already attached to this drive? */
5411ac4b82bSMike Smith     	if (dr->ms_disk == 0) {
5421ac4b82bSMike Smith 	    /* pick up drive information */
5431ac4b82bSMike Smith 	    dr->ms_size = mes[i].sd_size;
544f6b84b08SMike Smith 	    dr->ms_raidlevel = mes[i].sd_raidlevel & 0xf;
5451ac4b82bSMike Smith 	    dr->ms_state = mes[i].sd_state;
5461ac4b82bSMike Smith 
5471ac4b82bSMike Smith 	    /* generate geometry information */
5481ac4b82bSMike Smith 	    if (sc->mlx_geom == MLX_GEOM_128_32) {
5491ac4b82bSMike Smith 		dr->ms_heads = 128;
5501ac4b82bSMike Smith 		dr->ms_sectors = 32;
5511ac4b82bSMike Smith 		dr->ms_cylinders = dr->ms_size / (128 * 32);
5521ac4b82bSMike Smith 	    } else {        /* MLX_GEOM_255/63 */
5531ac4b82bSMike Smith 		dr->ms_heads = 255;
5541ac4b82bSMike Smith 		dr->ms_sectors = 63;
5551ac4b82bSMike Smith 		dr->ms_cylinders = dr->ms_size / (255 * 63);
5561ac4b82bSMike Smith 	    }
557fe0d4089SMatthew N. Dodd 	    dr->ms_disk =  device_add_child(sc->mlx_dev, /*"mlxd"*/NULL, -1);
5581ac4b82bSMike Smith 	    if (dr->ms_disk == 0)
5591ac4b82bSMike Smith 		device_printf(sc->mlx_dev, "device_add_child failed\n");
560fe0d4089SMatthew N. Dodd 	    device_set_ivars(dr->ms_disk, dr);
5611ac4b82bSMike Smith 	}
5621ac4b82bSMike Smith     }
5631ac4b82bSMike Smith     free(mes, M_DEVBUF);
5641ac4b82bSMike Smith     if ((error = bus_generic_attach(sc->mlx_dev)) != 0)
5651ac4b82bSMike Smith 	device_printf(sc->mlx_dev, "bus_generic_attach returned %d", error);
5661ac4b82bSMike Smith 
5671ac4b82bSMike Smith     /* mark controller back up */
5680fca6f8bSJohn Baldwin     MLX_IO_LOCK(sc);
5691ac4b82bSMike Smith     sc->mlx_state &= ~MLX_STATE_SHUTDOWN;
5701ac4b82bSMike Smith 
5711ac4b82bSMike Smith     /* enable interrupts */
5721ac4b82bSMike Smith     sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
5730fca6f8bSJohn Baldwin     MLX_IO_UNLOCK(sc);
5740fca6f8bSJohn Baldwin     MLX_CONFIG_UNLOCK(sc);
5751ac4b82bSMike Smith }
5761ac4b82bSMike Smith 
5771ac4b82bSMike Smith /********************************************************************************
5781ac4b82bSMike Smith  * Disconnect from the controller completely, in preparation for unload.
5791ac4b82bSMike Smith  */
5801ac4b82bSMike Smith int
5811ac4b82bSMike Smith mlx_detach(device_t dev)
5821ac4b82bSMike Smith {
5831ac4b82bSMike Smith     struct mlx_softc	*sc = device_get_softc(dev);
5845792b7feSMike Smith     struct mlxd_softc	*mlxd;
5850fca6f8bSJohn Baldwin     int			i, error;
5861ac4b82bSMike Smith 
587da8bb3a3SMike Smith     debug_called(1);
5881ac4b82bSMike Smith 
5895792b7feSMike Smith     error = EBUSY;
5900fca6f8bSJohn Baldwin     MLX_CONFIG_LOCK(sc);
5911ac4b82bSMike Smith     if (sc->mlx_state & MLX_STATE_OPEN)
5925792b7feSMike Smith 	goto out;
5931ac4b82bSMike Smith 
5945792b7feSMike Smith     for (i = 0; i < MLX_MAXDRIVES; i++) {
5955792b7feSMike Smith 	if (sc->mlx_sysdrive[i].ms_disk != 0) {
5965792b7feSMike Smith 	    mlxd = device_get_softc(sc->mlx_sysdrive[i].ms_disk);
5975792b7feSMike Smith 	    if (mlxd->mlxd_flags & MLXD_OPEN) {		/* drive is mounted, abort detach */
5985792b7feSMike Smith 		device_printf(sc->mlx_sysdrive[i].ms_disk, "still open, can't detach\n");
5995792b7feSMike Smith 		goto out;
6005792b7feSMike Smith 	    }
6015792b7feSMike Smith 	}
6025792b7feSMike Smith     }
6031ac4b82bSMike Smith     if ((error = mlx_shutdown(dev)))
6045792b7feSMike Smith 	goto out;
6050fca6f8bSJohn Baldwin     MLX_CONFIG_UNLOCK(sc);
6061ac4b82bSMike Smith 
6071ac4b82bSMike Smith     mlx_free(sc);
6081ac4b82bSMike Smith 
6090fca6f8bSJohn Baldwin     return (0);
6105792b7feSMike Smith  out:
6110fca6f8bSJohn Baldwin     MLX_CONFIG_UNLOCK(sc);
6125792b7feSMike Smith     return(error);
6131ac4b82bSMike Smith }
6141ac4b82bSMike Smith 
6151ac4b82bSMike Smith /********************************************************************************
6161ac4b82bSMike Smith  * Bring the controller down to a dormant state and detach all child devices.
6171ac4b82bSMike Smith  *
6181ac4b82bSMike Smith  * This function is called before detach, system shutdown, or before performing
6191ac4b82bSMike Smith  * an operation which may add or delete system disks.  (Call mlx_startup to
6201ac4b82bSMike Smith  * resume normal operation.)
6211ac4b82bSMike Smith  *
6228177437dSPoul-Henning Kamp  * Note that we can assume that the bioq on the controller is empty, as we won't
6231ac4b82bSMike Smith  * allow shutdown if any device is open.
6241ac4b82bSMike Smith  */
6251ac4b82bSMike Smith int
6261ac4b82bSMike Smith mlx_shutdown(device_t dev)
6271ac4b82bSMike Smith {
6281ac4b82bSMike Smith     struct mlx_softc	*sc = device_get_softc(dev);
6290fca6f8bSJohn Baldwin     int			error;
6300fca6f8bSJohn Baldwin 
6310fca6f8bSJohn Baldwin     MLX_CONFIG_LOCK(sc);
6320fca6f8bSJohn Baldwin     error = mlx_shutdown_locked(sc);
6330fca6f8bSJohn Baldwin     MLX_CONFIG_UNLOCK(sc);
6340fca6f8bSJohn Baldwin     return (error);
6350fca6f8bSJohn Baldwin }
6360fca6f8bSJohn Baldwin 
6370fca6f8bSJohn Baldwin static int
6380fca6f8bSJohn Baldwin mlx_shutdown_locked(struct mlx_softc *sc)
6390fca6f8bSJohn Baldwin {
6400fca6f8bSJohn Baldwin     int			i, error;
6411ac4b82bSMike Smith 
642da8bb3a3SMike Smith     debug_called(1);
6431ac4b82bSMike Smith 
6440fca6f8bSJohn Baldwin     MLX_CONFIG_ASSERT_LOCKED(sc);
6451ac4b82bSMike Smith 
6460fca6f8bSJohn Baldwin     MLX_IO_LOCK(sc);
6471ac4b82bSMike Smith     sc->mlx_state |= MLX_STATE_SHUTDOWN;
6485792b7feSMike Smith     sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
6491ac4b82bSMike Smith 
6501ac4b82bSMike Smith     /* flush controller */
6511ac4b82bSMike Smith     device_printf(sc->mlx_dev, "flushing cache...");
6521ac4b82bSMike Smith     if (mlx_flush(sc)) {
6531ac4b82bSMike Smith 	printf("failed\n");
6541ac4b82bSMike Smith     } else {
6551ac4b82bSMike Smith 	printf("done\n");
6561ac4b82bSMike Smith     }
6570fca6f8bSJohn Baldwin     MLX_IO_UNLOCK(sc);
6581ac4b82bSMike Smith 
6591ac4b82bSMike Smith     /* delete all our child devices */
6601ac4b82bSMike Smith     for (i = 0; i < MLX_MAXDRIVES; i++) {
6611ac4b82bSMike Smith 	if (sc->mlx_sysdrive[i].ms_disk != 0) {
6621ac4b82bSMike Smith 	    if ((error = device_delete_child(sc->mlx_dev, sc->mlx_sysdrive[i].ms_disk)) != 0)
6630fca6f8bSJohn Baldwin 		return (error);
6641ac4b82bSMike Smith 	    sc->mlx_sysdrive[i].ms_disk = 0;
6651ac4b82bSMike Smith 	}
6661ac4b82bSMike Smith     }
6671ac4b82bSMike Smith 
6680fca6f8bSJohn Baldwin     return (0);
6691ac4b82bSMike Smith }
6701ac4b82bSMike Smith 
6711ac4b82bSMike Smith /********************************************************************************
6721ac4b82bSMike Smith  * Bring the controller to a quiescent state, ready for system suspend.
6731ac4b82bSMike Smith  */
6741ac4b82bSMike Smith int
6751ac4b82bSMike Smith mlx_suspend(device_t dev)
6761ac4b82bSMike Smith {
6771ac4b82bSMike Smith     struct mlx_softc	*sc = device_get_softc(dev);
6781ac4b82bSMike Smith 
679da8bb3a3SMike Smith     debug_called(1);
6801ac4b82bSMike Smith 
6810fca6f8bSJohn Baldwin     MLX_IO_LOCK(sc);
6821ac4b82bSMike Smith     sc->mlx_state |= MLX_STATE_SUSPEND;
6831ac4b82bSMike Smith 
6841ac4b82bSMike Smith     /* flush controller */
6851ac4b82bSMike Smith     device_printf(sc->mlx_dev, "flushing cache...");
6861ac4b82bSMike Smith     printf("%s\n", mlx_flush(sc) ? "failed" : "done");
6871ac4b82bSMike Smith 
6881ac4b82bSMike Smith     sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
6890fca6f8bSJohn Baldwin     MLX_IO_UNLOCK(sc);
6901ac4b82bSMike Smith 
6911ac4b82bSMike Smith     return(0);
6921ac4b82bSMike Smith }
6931ac4b82bSMike Smith 
6941ac4b82bSMike Smith /********************************************************************************
6951ac4b82bSMike Smith  * Bring the controller back to a state ready for operation.
6961ac4b82bSMike Smith  */
6971ac4b82bSMike Smith int
6981ac4b82bSMike Smith mlx_resume(device_t dev)
6991ac4b82bSMike Smith {
7001ac4b82bSMike Smith     struct mlx_softc	*sc = device_get_softc(dev);
7011ac4b82bSMike Smith 
702da8bb3a3SMike Smith     debug_called(1);
7031ac4b82bSMike Smith 
7040fca6f8bSJohn Baldwin     MLX_IO_LOCK(sc);
7051ac4b82bSMike Smith     sc->mlx_state &= ~MLX_STATE_SUSPEND;
7061ac4b82bSMike Smith     sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
7070fca6f8bSJohn Baldwin     MLX_IO_UNLOCK(sc);
7081ac4b82bSMike Smith 
7091ac4b82bSMike Smith     return(0);
7101ac4b82bSMike Smith }
7111ac4b82bSMike Smith 
7121ac4b82bSMike Smith /*******************************************************************************
7131ac4b82bSMike Smith  * Take an interrupt, or be poked by other code to look for interrupt-worthy
7141ac4b82bSMike Smith  * status.
7151ac4b82bSMike Smith  */
7161ac4b82bSMike Smith void
7171ac4b82bSMike Smith mlx_intr(void *arg)
7181ac4b82bSMike Smith {
7191ac4b82bSMike Smith     struct mlx_softc	*sc = (struct mlx_softc *)arg;
7201ac4b82bSMike Smith 
721da8bb3a3SMike Smith     debug_called(1);
7221ac4b82bSMike Smith 
7235792b7feSMike Smith     /* collect finished commands, queue anything waiting */
7240fca6f8bSJohn Baldwin     MLX_IO_LOCK(sc);
7250fca6f8bSJohn Baldwin     mlx_done(sc, 1);
7260fca6f8bSJohn Baldwin     MLX_IO_UNLOCK(sc);
7271ac4b82bSMike Smith };
7281ac4b82bSMike Smith 
7291ac4b82bSMike Smith /*******************************************************************************
7301ac4b82bSMike Smith  * Receive a buf structure from a child device and queue it on a particular
7311ac4b82bSMike Smith  * disk resource, then poke the disk resource to start as much work as it can.
7321ac4b82bSMike Smith  */
7331ac4b82bSMike Smith int
734*b04e4c12SJohn Baldwin mlx_submit_buf(struct mlx_softc *sc, struct bio *bp)
7351ac4b82bSMike Smith {
7364b006d7bSMike Smith 
737da8bb3a3SMike Smith     debug_called(1);
7381ac4b82bSMike Smith 
7390fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
740*b04e4c12SJohn Baldwin     bioq_insert_tail(&sc->mlx_bioq, bp);
7411ac4b82bSMike Smith     sc->mlx_waitbufs++;
7421ac4b82bSMike Smith     mlx_startio(sc);
7431ac4b82bSMike Smith     return(0);
7441ac4b82bSMike Smith }
7451ac4b82bSMike Smith 
7461ac4b82bSMike Smith /********************************************************************************
7471ac4b82bSMike Smith  * Accept an open operation on the control device.
7481ac4b82bSMike Smith  */
7491ac4b82bSMike Smith int
75089c9c53dSPoul-Henning Kamp mlx_open(struct cdev *dev, int flags, int fmt, struct thread *td)
7511ac4b82bSMike Smith {
752e78a5a3fSEd Schouten     struct mlx_softc	*sc = dev->si_drv1;
7531ac4b82bSMike Smith 
7540fca6f8bSJohn Baldwin     MLX_CONFIG_LOCK(sc);
7550fca6f8bSJohn Baldwin     MLX_IO_LOCK(sc);
7561ac4b82bSMike Smith     sc->mlx_state |= MLX_STATE_OPEN;
7570fca6f8bSJohn Baldwin     MLX_IO_UNLOCK(sc);
7580fca6f8bSJohn Baldwin     MLX_CONFIG_UNLOCK(sc);
7591ac4b82bSMike Smith     return(0);
7601ac4b82bSMike Smith }
7611ac4b82bSMike Smith 
7621ac4b82bSMike Smith /********************************************************************************
7631ac4b82bSMike Smith  * Accept the last close on the control device.
7641ac4b82bSMike Smith  */
7651ac4b82bSMike Smith int
76689c9c53dSPoul-Henning Kamp mlx_close(struct cdev *dev, int flags, int fmt, struct thread *td)
7671ac4b82bSMike Smith {
768e78a5a3fSEd Schouten     struct mlx_softc	*sc = dev->si_drv1;
7691ac4b82bSMike Smith 
7700fca6f8bSJohn Baldwin     MLX_CONFIG_LOCK(sc);
7710fca6f8bSJohn Baldwin     MLX_IO_LOCK(sc);
7721ac4b82bSMike Smith     sc->mlx_state &= ~MLX_STATE_OPEN;
7730fca6f8bSJohn Baldwin     MLX_IO_UNLOCK(sc);
7740fca6f8bSJohn Baldwin     MLX_CONFIG_UNLOCK(sc);
7751ac4b82bSMike Smith     return (0);
7761ac4b82bSMike Smith }
7771ac4b82bSMike Smith 
7781ac4b82bSMike Smith /********************************************************************************
7791ac4b82bSMike Smith  * Handle controller-specific control operations.
7801ac4b82bSMike Smith  */
7811ac4b82bSMike Smith int
78289c9c53dSPoul-Henning Kamp mlx_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
7831ac4b82bSMike Smith {
784e78a5a3fSEd Schouten     struct mlx_softc		*sc = dev->si_drv1;
785421f2f7dSMike Smith     struct mlx_rebuild_request	*rb = (struct mlx_rebuild_request *)addr;
786421f2f7dSMike Smith     struct mlx_rebuild_status	*rs = (struct mlx_rebuild_status *)addr;
7871ac4b82bSMike Smith     int				*arg = (int *)addr;
7881ac4b82bSMike Smith     struct mlx_pause		*mp;
7891ac4b82bSMike Smith     struct mlx_sysdrive		*dr;
7901ac4b82bSMike Smith     struct mlxd_softc		*mlxd;
7911ac4b82bSMike Smith     int				i, error;
7921ac4b82bSMike Smith 
7931ac4b82bSMike Smith     switch(cmd) {
7941ac4b82bSMike Smith 	/*
7951ac4b82bSMike Smith 	 * Enumerate connected system drives; returns the first system drive's
7961ac4b82bSMike Smith 	 * unit number if *arg is -1, or the next unit after *arg if it's
7971ac4b82bSMike Smith 	 * a valid unit on this controller.
7981ac4b82bSMike Smith 	 */
7991ac4b82bSMike Smith     case MLX_NEXT_CHILD:
8001ac4b82bSMike Smith 	/* search system drives */
8010fca6f8bSJohn Baldwin 	MLX_CONFIG_LOCK(sc);
8021ac4b82bSMike Smith 	for (i = 0; i < MLX_MAXDRIVES; i++) {
8031ac4b82bSMike Smith 	    /* is this one attached? */
8041ac4b82bSMike Smith 	    if (sc->mlx_sysdrive[i].ms_disk != 0) {
8051ac4b82bSMike Smith 		/* looking for the next one we come across? */
8061ac4b82bSMike Smith 		if (*arg == -1) {
80729e6fa3aSStephane E. Potvin 		    *arg = device_get_unit(sc->mlx_sysdrive[i].ms_disk);
8080fca6f8bSJohn Baldwin 		    MLX_CONFIG_UNLOCK(sc);
8091ac4b82bSMike Smith 		    return(0);
8101ac4b82bSMike Smith 		}
8111ac4b82bSMike Smith 		/* we want the one after this one */
8121ac4b82bSMike Smith 		if (*arg == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
8131ac4b82bSMike Smith 		    *arg = -1;
8141ac4b82bSMike Smith 	    }
8151ac4b82bSMike Smith 	}
8160fca6f8bSJohn Baldwin 	MLX_CONFIG_UNLOCK(sc);
8171ac4b82bSMike Smith 	return(ENOENT);
8181ac4b82bSMike Smith 
8191ac4b82bSMike Smith 	/*
8201ac4b82bSMike Smith 	 * Scan the controller to see whether new drives have appeared.
8211ac4b82bSMike Smith 	 */
8221ac4b82bSMike Smith     case MLX_RESCAN_DRIVES:
8230fca6f8bSJohn Baldwin 	mtx_lock(&Giant);
8241ac4b82bSMike Smith 	mlx_startup(sc);
8250fca6f8bSJohn Baldwin 	mtx_unlock(&Giant);
8261ac4b82bSMike Smith 	return(0);
8271ac4b82bSMike Smith 
8281ac4b82bSMike Smith 	/*
8291ac4b82bSMike Smith 	 * Disconnect from the specified drive; it may be about to go
8301ac4b82bSMike Smith 	 * away.
8311ac4b82bSMike Smith 	 */
8321ac4b82bSMike Smith     case MLX_DETACH_DRIVE:			/* detach one drive */
8330fca6f8bSJohn Baldwin 	MLX_CONFIG_LOCK(sc);
8341ac4b82bSMike Smith 	if (((dr = mlx_findunit(sc, *arg)) == NULL) ||
8350fca6f8bSJohn Baldwin 	    ((mlxd = device_get_softc(dr->ms_disk)) == NULL)) {
8360fca6f8bSJohn Baldwin 	    MLX_CONFIG_UNLOCK(sc);
8371ac4b82bSMike Smith 	    return(ENOENT);
8380fca6f8bSJohn Baldwin 	}
8391ac4b82bSMike Smith 
8401ac4b82bSMike Smith 	device_printf(dr->ms_disk, "detaching...");
8411ac4b82bSMike Smith 	error = 0;
8421ac4b82bSMike Smith 	if (mlxd->mlxd_flags & MLXD_OPEN) {
8431ac4b82bSMike Smith 	    error = EBUSY;
8441ac4b82bSMike Smith 	    goto detach_out;
8451ac4b82bSMike Smith 	}
8461ac4b82bSMike Smith 
8471ac4b82bSMike Smith 	/* flush controller */
8480fca6f8bSJohn Baldwin 	MLX_IO_LOCK(sc);
8491ac4b82bSMike Smith 	if (mlx_flush(sc)) {
8500fca6f8bSJohn Baldwin 	    MLX_IO_UNLOCK(sc);
8511ac4b82bSMike Smith 	    error = EBUSY;
8521ac4b82bSMike Smith 	    goto detach_out;
8531ac4b82bSMike Smith 	}
8540fca6f8bSJohn Baldwin 	MLX_IO_UNLOCK(sc);
8551ac4b82bSMike Smith 
8561ac4b82bSMike Smith 	/* nuke drive */
8571ac4b82bSMike Smith 	if ((error = device_delete_child(sc->mlx_dev, dr->ms_disk)) != 0)
8581ac4b82bSMike Smith 	    goto detach_out;
8591ac4b82bSMike Smith 	dr->ms_disk = 0;
8601ac4b82bSMike Smith 
8611ac4b82bSMike Smith     detach_out:
8620fca6f8bSJohn Baldwin 	MLX_CONFIG_UNLOCK(sc);
8631ac4b82bSMike Smith 	if (error) {
8641ac4b82bSMike Smith 	    printf("failed\n");
8651ac4b82bSMike Smith 	} else {
8661ac4b82bSMike Smith 	    printf("done\n");
8671ac4b82bSMike Smith 	}
8681ac4b82bSMike Smith 	return(error);
8691ac4b82bSMike Smith 
8701ac4b82bSMike Smith 	/*
8711ac4b82bSMike Smith 	 * Pause one or more SCSI channels for a period of time, to assist
8721ac4b82bSMike Smith 	 * in the process of hot-swapping devices.
8731ac4b82bSMike Smith 	 *
8741ac4b82bSMike Smith 	 * Note that at least the 3.51 firmware on the DAC960PL doesn't seem
8751ac4b82bSMike Smith 	 * to do this right.
8761ac4b82bSMike Smith 	 */
8771ac4b82bSMike Smith     case MLX_PAUSE_CHANNEL:			/* schedule a channel pause */
8781ac4b82bSMike Smith 	/* Does this command work on this firmware? */
8791ac4b82bSMike Smith 	if (!(sc->mlx_feature & MLX_FEAT_PAUSEWORKS))
8801ac4b82bSMike Smith 	    return(EOPNOTSUPP);
8811ac4b82bSMike Smith 
8820fca6f8bSJohn Baldwin 	/* check time values */
8831ac4b82bSMike Smith 	mp = (struct mlx_pause *)addr;
8840fca6f8bSJohn Baldwin 	if ((mp->mp_when < 0) || (mp->mp_when > 3600))
8850fca6f8bSJohn Baldwin 	    return(EINVAL);
8860fca6f8bSJohn Baldwin 	if ((mp->mp_howlong < 1) || (mp->mp_howlong > (0xf * 30)))
8870fca6f8bSJohn Baldwin 	    return(EINVAL);
8880fca6f8bSJohn Baldwin 
8890fca6f8bSJohn Baldwin 	MLX_IO_LOCK(sc);
8901ac4b82bSMike Smith 	if ((mp->mp_which == MLX_PAUSE_CANCEL) && (sc->mlx_pause.mp_when != 0)) {
8911ac4b82bSMike Smith 	    /* cancel a pending pause operation */
8921ac4b82bSMike Smith 	    sc->mlx_pause.mp_which = 0;
8931ac4b82bSMike Smith 	} else {
8941ac4b82bSMike Smith 	    /* fix for legal channels */
8959eee27f1SMike Smith 	    mp->mp_which &= ((1 << sc->mlx_enq2->me_actual_channels) -1);
8961ac4b82bSMike Smith 
8971ac4b82bSMike Smith 	    /* check for a pause currently running */
8980fca6f8bSJohn Baldwin 	    if ((sc->mlx_pause.mp_which != 0) && (sc->mlx_pause.mp_when == 0)) {
8990fca6f8bSJohn Baldwin 		MLX_IO_UNLOCK(sc);
9001ac4b82bSMike Smith 		return(EBUSY);
9010fca6f8bSJohn Baldwin 	    }
9021ac4b82bSMike Smith 
9031ac4b82bSMike Smith 	    /* looks ok, go with it */
9041ac4b82bSMike Smith 	    sc->mlx_pause.mp_which = mp->mp_which;
9051ac4b82bSMike Smith 	    sc->mlx_pause.mp_when = time_second + mp->mp_when;
9061ac4b82bSMike Smith 	    sc->mlx_pause.mp_howlong = sc->mlx_pause.mp_when + mp->mp_howlong;
9071ac4b82bSMike Smith 	}
9080fca6f8bSJohn Baldwin 	MLX_IO_UNLOCK(sc);
9091ac4b82bSMike Smith 	return(0);
9101ac4b82bSMike Smith 
9111ac4b82bSMike Smith 	/*
9121ac4b82bSMike Smith 	 * Accept a command passthrough-style.
9131ac4b82bSMike Smith 	 */
9141ac4b82bSMike Smith     case MLX_COMMAND:
9151ac4b82bSMike Smith 	return(mlx_user_command(sc, (struct mlx_usercommand *)addr));
9161ac4b82bSMike Smith 
917421f2f7dSMike Smith 	/*
918421f2f7dSMike Smith 	 * Start a rebuild on a given SCSI disk
919421f2f7dSMike Smith 	 */
920421f2f7dSMike Smith     case MLX_REBUILDASYNC:
9210fca6f8bSJohn Baldwin 	MLX_IO_LOCK(sc);
922421f2f7dSMike Smith 	if (sc->mlx_background != 0) {
9230fca6f8bSJohn Baldwin 	    MLX_IO_UNLOCK(sc);
924421f2f7dSMike Smith 	    rb->rr_status = 0x0106;
925421f2f7dSMike Smith 	    return(EBUSY);
926421f2f7dSMike Smith 	}
927421f2f7dSMike Smith 	rb->rr_status = mlx_rebuild(sc, rb->rr_channel, rb->rr_target);
928421f2f7dSMike Smith 	switch (rb->rr_status) {
929421f2f7dSMike Smith 	case 0:
930421f2f7dSMike Smith 	    error = 0;
931421f2f7dSMike Smith 	    break;
932421f2f7dSMike Smith 	case 0x10000:
933421f2f7dSMike Smith 	    error = ENOMEM;		/* couldn't set up the command */
934421f2f7dSMike Smith 	    break;
935421f2f7dSMike Smith 	case 0x0002:
936421f2f7dSMike Smith 	    error = EBUSY;
937421f2f7dSMike Smith 	    break;
938421f2f7dSMike Smith 	case 0x0104:
939421f2f7dSMike Smith 	    error = EIO;
940421f2f7dSMike Smith 	    break;
941421f2f7dSMike Smith 	case 0x0105:
942421f2f7dSMike Smith 	    error = ERANGE;
943421f2f7dSMike Smith 	    break;
944421f2f7dSMike Smith 	case 0x0106:
945421f2f7dSMike Smith 	    error = EBUSY;
946421f2f7dSMike Smith 	    break;
947421f2f7dSMike Smith 	default:
948421f2f7dSMike Smith 	    error = EINVAL;
949421f2f7dSMike Smith 	    break;
950421f2f7dSMike Smith 	}
951421f2f7dSMike Smith 	if (error == 0)
952421f2f7dSMike Smith 	    sc->mlx_background = MLX_BACKGROUND_REBUILD;
9530fca6f8bSJohn Baldwin 	MLX_IO_UNLOCK(sc);
954421f2f7dSMike Smith 	return(error);
955421f2f7dSMike Smith 
956421f2f7dSMike Smith 	/*
957421f2f7dSMike Smith 	 * Get the status of the current rebuild or consistency check.
958421f2f7dSMike Smith 	 */
959421f2f7dSMike Smith     case MLX_REBUILDSTAT:
9600fca6f8bSJohn Baldwin 	MLX_IO_LOCK(sc);
961421f2f7dSMike Smith 	*rs = sc->mlx_rebuildstat;
9620fca6f8bSJohn Baldwin 	MLX_IO_UNLOCK(sc);
963421f2f7dSMike Smith 	return(0);
964421f2f7dSMike Smith 
965421f2f7dSMike Smith 	/*
966421f2f7dSMike Smith 	 * Return the per-controller system drive number matching the
967421f2f7dSMike Smith 	 * disk device number in (arg), if it happens to belong to us.
968421f2f7dSMike Smith 	 */
969421f2f7dSMike Smith     case MLX_GET_SYSDRIVE:
970421f2f7dSMike Smith 	error = ENOENT;
9710fca6f8bSJohn Baldwin 	MLX_CONFIG_LOCK(sc);
9720fca6f8bSJohn Baldwin 	mtx_lock(&Giant);
973421f2f7dSMike Smith 	mlxd = (struct mlxd_softc *)devclass_get_softc(mlxd_devclass, *arg);
9740fca6f8bSJohn Baldwin 	mtx_unlock(&Giant);
975421f2f7dSMike Smith 	if ((mlxd != NULL) && (mlxd->mlxd_drive >= sc->mlx_sysdrive) &&
976421f2f7dSMike Smith 	    (mlxd->mlxd_drive < (sc->mlx_sysdrive + MLX_MAXDRIVES))) {
977421f2f7dSMike Smith 	    error = 0;
978421f2f7dSMike Smith 	    *arg = mlxd->mlxd_drive - sc->mlx_sysdrive;
979421f2f7dSMike Smith 	}
9800fca6f8bSJohn Baldwin 	MLX_CONFIG_UNLOCK(sc);
981421f2f7dSMike Smith 	return(error);
982421f2f7dSMike Smith 
9831ac4b82bSMike Smith     default:
9841ac4b82bSMike Smith 	return(ENOTTY);
9851ac4b82bSMike Smith     }
9861ac4b82bSMike Smith }
9871ac4b82bSMike Smith 
9881ac4b82bSMike Smith /********************************************************************************
9891ac4b82bSMike Smith  * Handle operations requested by a System Drive connected to this controller.
9901ac4b82bSMike Smith  */
9911ac4b82bSMike Smith int
9921ac4b82bSMike Smith mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd,
993b40ce416SJulian Elischer 		caddr_t addr, int32_t flag, struct thread *td)
9941ac4b82bSMike Smith {
9951ac4b82bSMike Smith     int				*arg = (int *)addr;
996421f2f7dSMike Smith     int				error, result;
9971ac4b82bSMike Smith 
9981ac4b82bSMike Smith     switch(cmd) {
9991ac4b82bSMike Smith 	/*
10001ac4b82bSMike Smith 	 * Return the current status of this drive.
10011ac4b82bSMike Smith 	 */
10021ac4b82bSMike Smith     case MLXD_STATUS:
10030fca6f8bSJohn Baldwin 	MLX_IO_LOCK(sc);
10041ac4b82bSMike Smith 	*arg = drive->ms_state;
10050fca6f8bSJohn Baldwin 	MLX_IO_UNLOCK(sc);
10061ac4b82bSMike Smith 	return(0);
10071ac4b82bSMike Smith 
10081ac4b82bSMike Smith 	/*
1009421f2f7dSMike Smith 	 * Start a background consistency check on this drive.
10101ac4b82bSMike Smith 	 */
1011421f2f7dSMike Smith     case MLXD_CHECKASYNC:		/* start a background consistency check */
10120fca6f8bSJohn Baldwin 	MLX_IO_LOCK(sc);
1013421f2f7dSMike Smith 	if (sc->mlx_background != 0) {
10140fca6f8bSJohn Baldwin 	    MLX_IO_UNLOCK(sc);
1015421f2f7dSMike Smith 	    *arg = 0x0106;
10161ac4b82bSMike Smith 	    return(EBUSY);
1017421f2f7dSMike Smith 	}
1018421f2f7dSMike Smith 	result = mlx_check(sc, drive - &sc->mlx_sysdrive[0]);
1019421f2f7dSMike Smith 	switch (result) {
10201ac4b82bSMike Smith 	case 0:
10211ac4b82bSMike Smith 	    error = 0;
10221ac4b82bSMike Smith 	    break;
10231ac4b82bSMike Smith 	case 0x10000:
10241ac4b82bSMike Smith 	    error = ENOMEM;		/* couldn't set up the command */
10251ac4b82bSMike Smith 	    break;
10261ac4b82bSMike Smith 	case 0x0002:
10271ac4b82bSMike Smith 	    error = EIO;
10281ac4b82bSMike Smith 	    break;
10291ac4b82bSMike Smith 	case 0x0105:
10301ac4b82bSMike Smith 	    error = ERANGE;
10311ac4b82bSMike Smith 	    break;
1032421f2f7dSMike Smith 	case 0x0106:
1033421f2f7dSMike Smith 	    error = EBUSY;
1034421f2f7dSMike Smith 	    break;
10351ac4b82bSMike Smith 	default:
10361ac4b82bSMike Smith 	    error = EINVAL;
10371ac4b82bSMike Smith 	    break;
10381ac4b82bSMike Smith 	}
1039421f2f7dSMike Smith 	if (error == 0)
1040421f2f7dSMike Smith 	    sc->mlx_background = MLX_BACKGROUND_CHECK;
10410fca6f8bSJohn Baldwin 	MLX_IO_UNLOCK(sc);
1042421f2f7dSMike Smith 	*arg = result;
10431ac4b82bSMike Smith 	return(error);
10441ac4b82bSMike Smith 
10451ac4b82bSMike Smith     }
10461ac4b82bSMike Smith     return(ENOIOCTL);
10471ac4b82bSMike Smith }
10481ac4b82bSMike Smith 
10491ac4b82bSMike Smith 
10501ac4b82bSMike Smith /********************************************************************************
10511ac4b82bSMike Smith  ********************************************************************************
10521ac4b82bSMike Smith                                                                 Status Monitoring
10531ac4b82bSMike Smith  ********************************************************************************
10541ac4b82bSMike Smith  ********************************************************************************/
10551ac4b82bSMike Smith 
10561ac4b82bSMike Smith /********************************************************************************
10571ac4b82bSMike Smith  * Fire off commands to periodically check the status of connected drives.
10581ac4b82bSMike Smith  */
10591ac4b82bSMike Smith static void
10601ac4b82bSMike Smith mlx_periodic(void *data)
10611ac4b82bSMike Smith {
10621ac4b82bSMike Smith     struct mlx_softc *sc = (struct mlx_softc *)data;
10631ac4b82bSMike Smith 
1064da8bb3a3SMike Smith     debug_called(1);
10650fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
10661ac4b82bSMike Smith 
10671ac4b82bSMike Smith     /*
10681ac4b82bSMike Smith      * Run a bus pause?
10691ac4b82bSMike Smith      */
10701ac4b82bSMike Smith     if ((sc->mlx_pause.mp_which != 0) &&
10711ac4b82bSMike Smith 	(sc->mlx_pause.mp_when > 0) &&
10721ac4b82bSMike Smith 	(time_second >= sc->mlx_pause.mp_when)){
10731ac4b82bSMike Smith 
10741ac4b82bSMike Smith 	mlx_pause_action(sc);		/* pause is running */
10751ac4b82bSMike Smith 	sc->mlx_pause.mp_when = 0;
10761ac4b82bSMike Smith 	sysbeep(500, hz);
10771ac4b82bSMike Smith 
10781ac4b82bSMike Smith 	/*
10791ac4b82bSMike Smith 	 * Bus pause still running?
10801ac4b82bSMike Smith 	 */
10811ac4b82bSMike Smith     } else if ((sc->mlx_pause.mp_which != 0) &&
10821ac4b82bSMike Smith 	       (sc->mlx_pause.mp_when == 0)) {
10831ac4b82bSMike Smith 
10841ac4b82bSMike Smith 	/* time to stop bus pause? */
10851ac4b82bSMike Smith 	if (time_second >= sc->mlx_pause.mp_howlong) {
10861ac4b82bSMike Smith 	    mlx_pause_action(sc);
10871ac4b82bSMike Smith 	    sc->mlx_pause.mp_which = 0;	/* pause is complete */
10881ac4b82bSMike Smith 	    sysbeep(500, hz);
10891ac4b82bSMike Smith 	} else {
10901ac4b82bSMike Smith 	    sysbeep((time_second % 5) * 100 + 500, hz/8);
10911ac4b82bSMike Smith 	}
10921ac4b82bSMike Smith 
10931ac4b82bSMike Smith 	/*
10941ac4b82bSMike Smith 	 * Run normal periodic activities?
10951ac4b82bSMike Smith 	 */
10965792b7feSMike Smith     } else if (time_second > (sc->mlx_lastpoll + 10)) {
10971ac4b82bSMike Smith 	sc->mlx_lastpoll = time_second;
10981ac4b82bSMike Smith 
10991ac4b82bSMike Smith 	/*
11001ac4b82bSMike Smith 	 * Check controller status.
11015792b7feSMike Smith 	 *
11025792b7feSMike Smith 	 * XXX Note that this may not actually launch a command in situations of high load.
11031ac4b82bSMike Smith 	 */
1104da8bb3a3SMike Smith 	mlx_enquire(sc, (sc->mlx_iftype == MLX_IFTYPE_2) ? MLX_CMD_ENQUIRY_OLD : MLX_CMD_ENQUIRY,
1105da8bb3a3SMike Smith 		    imax(sizeof(struct mlx_enquiry), sizeof(struct mlx_enquiry_old)), mlx_periodic_enquiry);
11061ac4b82bSMike Smith 
11071ac4b82bSMike Smith 	/*
11081ac4b82bSMike Smith 	 * Check system drive status.
11091ac4b82bSMike Smith 	 *
11101ac4b82bSMike Smith 	 * XXX This might be better left to event-driven detection, eg. I/O to an offline
11111ac4b82bSMike Smith 	 *     drive will detect it's offline, rebuilds etc. should detect the drive is back
11121ac4b82bSMike Smith 	 *     online.
11131ac4b82bSMike Smith 	 */
11141ac4b82bSMike Smith 	mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(struct mlx_enq_sys_drive) * MLX_MAXDRIVES,
11151ac4b82bSMike Smith 			mlx_periodic_enquiry);
11161ac4b82bSMike Smith 
11171ac4b82bSMike Smith     }
11181ac4b82bSMike Smith 
1119421f2f7dSMike Smith     /* get drive rebuild/check status */
1120421f2f7dSMike Smith     /* XXX should check sc->mlx_background if this is only valid while in progress */
1121421f2f7dSMike Smith     mlx_enquire(sc, MLX_CMD_REBUILDSTAT, sizeof(struct mlx_rebuild_stat), mlx_periodic_rebuild);
1122421f2f7dSMike Smith 
11235792b7feSMike Smith     /* deal with possibly-missed interrupts and timed-out commands */
11240fca6f8bSJohn Baldwin     mlx_done(sc, 1);
11251ac4b82bSMike Smith 
11261ac4b82bSMike Smith     /* reschedule another poll next second or so */
11270fca6f8bSJohn Baldwin     callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
11281ac4b82bSMike Smith }
11291ac4b82bSMike Smith 
11301ac4b82bSMike Smith /********************************************************************************
11311ac4b82bSMike Smith  * Handle the result of an ENQUIRY command instigated by periodic status polling.
11321ac4b82bSMike Smith  */
11331ac4b82bSMike Smith static void
11341ac4b82bSMike Smith mlx_periodic_enquiry(struct mlx_command *mc)
11351ac4b82bSMike Smith {
11361ac4b82bSMike Smith     struct mlx_softc		*sc = mc->mc_sc;
11371ac4b82bSMike Smith 
1138da8bb3a3SMike Smith     debug_called(1);
11390fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
11401ac4b82bSMike Smith 
11411ac4b82bSMike Smith     /* Command completed OK? */
11421ac4b82bSMike Smith     if (mc->mc_status != 0) {
1143da8bb3a3SMike Smith 	device_printf(sc->mlx_dev, "periodic enquiry failed - %s\n", mlx_diagnose_command(mc));
11441ac4b82bSMike Smith 	goto out;
11451ac4b82bSMike Smith     }
11461ac4b82bSMike Smith 
11471ac4b82bSMike Smith     /* respond to command */
11481ac4b82bSMike Smith     switch(mc->mc_mailbox[0]) {
11491ac4b82bSMike Smith 	/*
1150da8bb3a3SMike Smith 	 * This is currently a bit fruitless, as we don't know how to extract the eventlog
1151da8bb3a3SMike Smith 	 * pointer yet.
1152da8bb3a3SMike Smith 	 */
1153da8bb3a3SMike Smith     case MLX_CMD_ENQUIRY_OLD:
1154da8bb3a3SMike Smith     {
1155da8bb3a3SMike Smith 	struct mlx_enquiry		*me = (struct mlx_enquiry *)mc->mc_data;
1156da8bb3a3SMike Smith 	struct mlx_enquiry_old		*meo = (struct mlx_enquiry_old *)mc->mc_data;
1157da8bb3a3SMike Smith 	int				i;
1158da8bb3a3SMike Smith 
1159da8bb3a3SMike Smith 	/* convert data in-place to new format */
1160da8bb3a3SMike Smith 	for (i = (sizeof(me->me_dead) / sizeof(me->me_dead[0])) - 1; i >= 0; i--) {
1161da8bb3a3SMike Smith 	    me->me_dead[i].dd_chan = meo->me_dead[i].dd_chan;
1162da8bb3a3SMike Smith 	    me->me_dead[i].dd_targ = meo->me_dead[i].dd_targ;
1163da8bb3a3SMike Smith 	}
1164da8bb3a3SMike Smith 	me->me_misc_flags        = 0;
1165da8bb3a3SMike Smith 	me->me_rebuild_count     = meo->me_rebuild_count;
1166da8bb3a3SMike Smith 	me->me_dead_count        = meo->me_dead_count;
1167da8bb3a3SMike Smith 	me->me_critical_sd_count = meo->me_critical_sd_count;
1168da8bb3a3SMike Smith 	me->me_event_log_seq_num = 0;
1169da8bb3a3SMike Smith 	me->me_offline_sd_count  = meo->me_offline_sd_count;
1170da8bb3a3SMike Smith 	me->me_max_commands      = meo->me_max_commands;
1171da8bb3a3SMike Smith 	me->me_rebuild_flag      = meo->me_rebuild_flag;
1172da8bb3a3SMike Smith 	me->me_fwmajor           = meo->me_fwmajor;
1173da8bb3a3SMike Smith 	me->me_fwminor           = meo->me_fwminor;
1174da8bb3a3SMike Smith 	me->me_status_flags      = meo->me_status_flags;
1175da8bb3a3SMike Smith 	me->me_flash_age         = meo->me_flash_age;
1176da8bb3a3SMike Smith 	for (i = (sizeof(me->me_drvsize) / sizeof(me->me_drvsize[0])) - 1; i >= 0; i--) {
1177da8bb3a3SMike Smith 	    if (i > ((sizeof(meo->me_drvsize) / sizeof(meo->me_drvsize[0])) - 1)) {
1178da8bb3a3SMike Smith 		me->me_drvsize[i] = 0;		/* drive beyond supported range */
1179da8bb3a3SMike Smith 	    } else {
1180da8bb3a3SMike Smith 		me->me_drvsize[i] = meo->me_drvsize[i];
1181da8bb3a3SMike Smith 	    }
1182da8bb3a3SMike Smith 	}
1183da8bb3a3SMike Smith 	me->me_num_sys_drvs = meo->me_num_sys_drvs;
1184da8bb3a3SMike Smith     }
1185da8bb3a3SMike Smith     /* FALLTHROUGH */
1186da8bb3a3SMike Smith 
1187da8bb3a3SMike Smith 	/*
11881ac4b82bSMike Smith 	 * Generic controller status update.  We could do more with this than just
11891ac4b82bSMike Smith 	 * checking the event log.
11901ac4b82bSMike Smith 	 */
11911ac4b82bSMike Smith     case MLX_CMD_ENQUIRY:
11921ac4b82bSMike Smith     {
11931ac4b82bSMike Smith 	struct mlx_enquiry		*me = (struct mlx_enquiry *)mc->mc_data;
11941ac4b82bSMike Smith 
1195421f2f7dSMike Smith 	if (sc->mlx_currevent == -1) {
11969eee27f1SMike Smith 	    /* initialise our view of the event log */
11979eee27f1SMike Smith 	    sc->mlx_currevent = sc->mlx_lastevent = me->me_event_log_seq_num;
11985d278f5cSMike Smith 	} else if ((me->me_event_log_seq_num != sc->mlx_lastevent) && !(sc->mlx_flags & MLX_EVENTLOG_BUSY)) {
11991ac4b82bSMike Smith 	    /* record where current events are up to */
12001ac4b82bSMike Smith 	    sc->mlx_currevent = me->me_event_log_seq_num;
1201da8bb3a3SMike Smith 	    debug(1, "event log pointer was %d, now %d\n", sc->mlx_lastevent, sc->mlx_currevent);
12021ac4b82bSMike Smith 
12035d278f5cSMike Smith 	    /* mark the event log as busy */
12040fca6f8bSJohn Baldwin 	    sc->mlx_flags |= MLX_EVENTLOG_BUSY;
12055d278f5cSMike Smith 
12069eee27f1SMike Smith 	    /* drain new eventlog entries */
12071ac4b82bSMike Smith 	    mlx_periodic_eventlog_poll(sc);
12081ac4b82bSMike Smith 	}
12091ac4b82bSMike Smith 	break;
12101ac4b82bSMike Smith     }
12111ac4b82bSMike Smith     case MLX_CMD_ENQSYSDRIVE:
12121ac4b82bSMike Smith     {
12131ac4b82bSMike Smith 	struct mlx_enq_sys_drive	*mes = (struct mlx_enq_sys_drive *)mc->mc_data;
12141ac4b82bSMike Smith 	struct mlx_sysdrive		*dr;
12151ac4b82bSMike Smith 	int				i;
12161ac4b82bSMike Smith 
12171ac4b82bSMike Smith 	for (i = 0, dr = &sc->mlx_sysdrive[0];
12181ac4b82bSMike Smith 	     (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
12191ac4b82bSMike Smith 	     i++) {
12201ac4b82bSMike Smith 
12211ac4b82bSMike Smith 	    /* has state been changed by controller? */
12221ac4b82bSMike Smith 	    if (dr->ms_state != mes[i].sd_state) {
12231ac4b82bSMike Smith 		switch(mes[i].sd_state) {
12241ac4b82bSMike Smith 		case MLX_SYSD_OFFLINE:
12251ac4b82bSMike Smith 		    device_printf(dr->ms_disk, "drive offline\n");
12261ac4b82bSMike Smith 		    break;
12271ac4b82bSMike Smith 		case MLX_SYSD_ONLINE:
12281ac4b82bSMike Smith 		    device_printf(dr->ms_disk, "drive online\n");
12291ac4b82bSMike Smith 		    break;
12301ac4b82bSMike Smith 		case MLX_SYSD_CRITICAL:
12311ac4b82bSMike Smith 		    device_printf(dr->ms_disk, "drive critical\n");
12321ac4b82bSMike Smith 		    break;
12331ac4b82bSMike Smith 		}
12341ac4b82bSMike Smith 		/* save new state */
12351ac4b82bSMike Smith 		dr->ms_state = mes[i].sd_state;
12361ac4b82bSMike Smith 	    }
12371ac4b82bSMike Smith 	}
12381ac4b82bSMike Smith 	break;
12391ac4b82bSMike Smith     }
12401ac4b82bSMike Smith     default:
12416e551fb6SDavid E. O'Brien 	device_printf(sc->mlx_dev, "%s: unknown command 0x%x", __func__, mc->mc_mailbox[0]);
12421ac4b82bSMike Smith 	break;
12431ac4b82bSMike Smith     }
12441ac4b82bSMike Smith 
12451ac4b82bSMike Smith  out:
12461ac4b82bSMike Smith     free(mc->mc_data, M_DEVBUF);
12471ac4b82bSMike Smith     mlx_releasecmd(mc);
12481ac4b82bSMike Smith }
12491ac4b82bSMike Smith 
12501b4404f9SScott Long static void
12511b4404f9SScott Long mlx_eventlog_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
12521b4404f9SScott Long {
12531b4404f9SScott Long     struct mlx_command *mc;
12541b4404f9SScott Long 
12551b4404f9SScott Long     mc = (struct mlx_command *)arg;
12561b4404f9SScott Long     mlx_setup_dmamap(mc, segs, nsegments, error);
12571b4404f9SScott Long 
12581b4404f9SScott Long     /* build the command to get one entry */
12591b4404f9SScott Long     mlx_make_type3(mc, MLX_CMD_LOGOP, MLX_LOGOP_GET, 1,
12601b4404f9SScott Long 		   mc->mc_sc->mlx_lastevent, 0, 0, mc->mc_dataphys, 0);
12611b4404f9SScott Long     mc->mc_complete = mlx_periodic_eventlog_respond;
12621b4404f9SScott Long     mc->mc_private = mc;
12631b4404f9SScott Long 
12641b4404f9SScott Long     /* start the command */
12651b4404f9SScott Long     if (mlx_start(mc) != 0) {
12661b4404f9SScott Long 	mlx_releasecmd(mc);
12671b4404f9SScott Long 	free(mc->mc_data, M_DEVBUF);
12681b4404f9SScott Long 	mc->mc_data = NULL;
12691b4404f9SScott Long     }
12701b4404f9SScott Long 
12711b4404f9SScott Long }
12721b4404f9SScott Long 
12731ac4b82bSMike Smith /********************************************************************************
12741ac4b82bSMike Smith  * Instigate a poll for one event log message on (sc).
12751ac4b82bSMike Smith  * We only poll for one message at a time, to keep our command usage down.
12761ac4b82bSMike Smith  */
12771ac4b82bSMike Smith static void
12781ac4b82bSMike Smith mlx_periodic_eventlog_poll(struct mlx_softc *sc)
12791ac4b82bSMike Smith {
12801ac4b82bSMike Smith     struct mlx_command	*mc;
12811ac4b82bSMike Smith     void		*result = NULL;
12821b4404f9SScott Long     int			error = 0;
12831ac4b82bSMike Smith 
1284da8bb3a3SMike Smith     debug_called(1);
12850fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
12861ac4b82bSMike Smith 
12871ac4b82bSMike Smith     /* get ourselves a command buffer */
12881ac4b82bSMike Smith     error = 1;
12891ac4b82bSMike Smith     if ((mc = mlx_alloccmd(sc)) == NULL)
12901ac4b82bSMike Smith 	goto out;
12911b4404f9SScott Long 
12921ac4b82bSMike Smith     /* allocate the response structure */
12931b4404f9SScott Long     if ((result = malloc(/*sizeof(struct mlx_eventlog_entry)*/1024, M_DEVBUF,
12941b4404f9SScott Long 			 M_NOWAIT)) == NULL)
12951ac4b82bSMike Smith 	goto out;
12961b4404f9SScott Long 
12971ac4b82bSMike Smith     /* get a command slot */
12981ac4b82bSMike Smith     if (mlx_getslot(mc))
12991ac4b82bSMike Smith 	goto out;
13001ac4b82bSMike Smith 
13011ac4b82bSMike Smith     /* map the command so the controller can see it */
13021ac4b82bSMike Smith     mc->mc_data = result;
130333c8cb18SMike Smith     mc->mc_length = /*sizeof(struct mlx_eventlog_entry)*/1024;
13041b4404f9SScott Long     error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
13051b4404f9SScott Long 			    mc->mc_length, mlx_eventlog_cb, mc, BUS_DMA_NOWAIT);
13061ac4b82bSMike Smith 
13071ac4b82bSMike Smith  out:
130833c8cb18SMike Smith     if (error != 0) {
13091ac4b82bSMike Smith 	if (mc != NULL)
13101ac4b82bSMike Smith 	    mlx_releasecmd(mc);
13111b4404f9SScott Long 	if ((result != NULL) && (mc->mc_data != NULL))
13121ac4b82bSMike Smith 	    free(result, M_DEVBUF);
13131ac4b82bSMike Smith     }
131433c8cb18SMike Smith }
13151ac4b82bSMike Smith 
13161ac4b82bSMike Smith /********************************************************************************
13171ac4b82bSMike Smith  * Handle the result of polling for a log message, generate diagnostic output.
13181ac4b82bSMike Smith  * If this wasn't the last message waiting for us, we'll go collect another.
13191ac4b82bSMike Smith  */
13201ac4b82bSMike Smith static char *mlx_sense_messages[] = {
13211ac4b82bSMike Smith     "because write recovery failed",
13221ac4b82bSMike Smith     "because of SCSI bus reset failure",
13231ac4b82bSMike Smith     "because of double check condition",
13241ac4b82bSMike Smith     "because it was removed",
13251ac4b82bSMike Smith     "because of gross error on SCSI chip",
13261ac4b82bSMike Smith     "because of bad tag returned from drive",
13271ac4b82bSMike Smith     "because of timeout on SCSI command",
13281ac4b82bSMike Smith     "because of reset SCSI command issued from system",
13291ac4b82bSMike Smith     "because busy or parity error count exceeded limit",
13301ac4b82bSMike Smith     "because of 'kill drive' command from system",
13311ac4b82bSMike Smith     "because of selection timeout",
13321ac4b82bSMike Smith     "due to SCSI phase sequence error",
13331ac4b82bSMike Smith     "due to unknown status"
13341ac4b82bSMike Smith };
13351ac4b82bSMike Smith 
13361ac4b82bSMike Smith static void
13371ac4b82bSMike Smith mlx_periodic_eventlog_respond(struct mlx_command *mc)
13381ac4b82bSMike Smith {
13391ac4b82bSMike Smith     struct mlx_softc		*sc = mc->mc_sc;
13401ac4b82bSMike Smith     struct mlx_eventlog_entry	*el = (struct mlx_eventlog_entry *)mc->mc_data;
13411ac4b82bSMike Smith     char			*reason;
13421ac4b82bSMike Smith 
1343da8bb3a3SMike Smith     debug_called(1);
13440fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
13451ac4b82bSMike Smith 
13465792b7feSMike Smith     sc->mlx_lastevent++;		/* next message... */
13471ac4b82bSMike Smith     if (mc->mc_status == 0) {
13481ac4b82bSMike Smith 
13491ac4b82bSMike Smith 	/* handle event log message */
13501ac4b82bSMike Smith 	switch(el->el_type) {
13511ac4b82bSMike Smith 	    /*
13521ac4b82bSMike Smith 	     * This is the only sort of message we understand at the moment.
13531ac4b82bSMike Smith 	     * The tests here are probably incomplete.
13541ac4b82bSMike Smith 	     */
13551ac4b82bSMike Smith 	case MLX_LOGMSG_SENSE:	/* sense data */
13561ac4b82bSMike Smith 	    /* Mylex vendor-specific message indicating a drive was killed? */
13571ac4b82bSMike Smith 	    if ((el->el_sensekey == 9) &&
13581ac4b82bSMike Smith 		(el->el_asc == 0x80)) {
13591ac4b82bSMike Smith 		if (el->el_asq < (sizeof(mlx_sense_messages) / sizeof(mlx_sense_messages[0]))) {
13601ac4b82bSMike Smith 		    reason = mlx_sense_messages[el->el_asq];
13611ac4b82bSMike Smith 		} else {
13621ac4b82bSMike Smith 		    reason = "for unknown reason";
13631ac4b82bSMike Smith 		}
13641ac4b82bSMike Smith 		device_printf(sc->mlx_dev, "physical drive %d:%d killed %s\n",
13651ac4b82bSMike Smith 			      el->el_channel, el->el_target, reason);
13661ac4b82bSMike Smith 	    }
13671ac4b82bSMike Smith 	    /* SCSI drive was reset? */
13681ac4b82bSMike Smith 	    if ((el->el_sensekey == 6) && (el->el_asc == 0x29)) {
13691ac4b82bSMike Smith 		device_printf(sc->mlx_dev, "physical drive %d:%d reset\n",
13701ac4b82bSMike Smith 			      el->el_channel, el->el_target);
13711ac4b82bSMike Smith 	    }
13721ac4b82bSMike Smith 	    /* SCSI drive error? */
13731ac4b82bSMike Smith 	    if (!((el->el_sensekey == 0) ||
13741ac4b82bSMike Smith 		  ((el->el_sensekey == 2) &&
13751ac4b82bSMike Smith 		   (el->el_asc == 0x04) &&
13761ac4b82bSMike Smith 		   ((el->el_asq == 0x01) ||
13771ac4b82bSMike Smith 		    (el->el_asq == 0x02))))) {
13781ac4b82bSMike Smith 		device_printf(sc->mlx_dev, "physical drive %d:%d error log: sense = %d asc = %x asq = %x\n",
13791ac4b82bSMike Smith 			      el->el_channel, el->el_target, el->el_sensekey, el->el_asc, el->el_asq);
13801ac4b82bSMike Smith 		device_printf(sc->mlx_dev, "  info %4D csi %4D\n", el->el_information, ":", el->el_csi, ":");
13811ac4b82bSMike Smith 	    }
13821ac4b82bSMike Smith 	    break;
13831ac4b82bSMike Smith 
13841ac4b82bSMike Smith 	default:
13851ac4b82bSMike Smith 	    device_printf(sc->mlx_dev, "unknown log message type 0x%x\n", el->el_type);
13861ac4b82bSMike Smith 	    break;
13871ac4b82bSMike Smith 	}
13881ac4b82bSMike Smith     } else {
13891ac4b82bSMike Smith 	device_printf(sc->mlx_dev, "error reading message log - %s\n", mlx_diagnose_command(mc));
13905d278f5cSMike Smith 	/* give up on all the outstanding messages, as we may have come unsynched */
13915d278f5cSMike Smith 	sc->mlx_lastevent = sc->mlx_currevent;
13921ac4b82bSMike Smith     }
13931ac4b82bSMike Smith 
13941ac4b82bSMike Smith     /* dispose of command and data */
13951ac4b82bSMike Smith     free(mc->mc_data, M_DEVBUF);
13961ac4b82bSMike Smith     mlx_releasecmd(mc);
13971ac4b82bSMike Smith 
13981ac4b82bSMike Smith     /* is there another message to obtain? */
13995d278f5cSMike Smith     if (sc->mlx_lastevent != sc->mlx_currevent) {
14001ac4b82bSMike Smith 	mlx_periodic_eventlog_poll(sc);
14015d278f5cSMike Smith     } else {
14025d278f5cSMike Smith 	/* clear log-busy status */
14030fca6f8bSJohn Baldwin 	sc->mlx_flags &= ~MLX_EVENTLOG_BUSY;
14045d278f5cSMike Smith     }
14051ac4b82bSMike Smith }
14061ac4b82bSMike Smith 
14071ac4b82bSMike Smith /********************************************************************************
1408421f2f7dSMike Smith  * Handle check/rebuild operations in progress.
14091ac4b82bSMike Smith  */
14101ac4b82bSMike Smith static void
14111ac4b82bSMike Smith mlx_periodic_rebuild(struct mlx_command *mc)
14121ac4b82bSMike Smith {
14131ac4b82bSMike Smith     struct mlx_softc		*sc = mc->mc_sc;
1414421f2f7dSMike Smith     struct mlx_rebuild_status	*mr = (struct mlx_rebuild_status *)mc->mc_data;
14151ac4b82bSMike Smith 
14160fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
14171ac4b82bSMike Smith     switch(mc->mc_status) {
1418421f2f7dSMike Smith     case 0:				/* operation running, update stats */
1419421f2f7dSMike Smith 	sc->mlx_rebuildstat = *mr;
1420421f2f7dSMike Smith 
1421421f2f7dSMike Smith 	/* spontaneous rebuild/check? */
1422421f2f7dSMike Smith 	if (sc->mlx_background == 0) {
1423421f2f7dSMike Smith 	    sc->mlx_background = MLX_BACKGROUND_SPONTANEOUS;
1424421f2f7dSMike Smith 	    device_printf(sc->mlx_dev, "background check/rebuild operation started\n");
1425421f2f7dSMike Smith 	}
14261ac4b82bSMike Smith 	break;
14271ac4b82bSMike Smith 
1428421f2f7dSMike Smith     case 0x0105:			/* nothing running, finalise stats and report */
1429421f2f7dSMike Smith 	switch(sc->mlx_background) {
1430421f2f7dSMike Smith 	case MLX_BACKGROUND_CHECK:
1431421f2f7dSMike Smith 	    device_printf(sc->mlx_dev, "consistency check completed\n");	/* XXX print drive? */
1432421f2f7dSMike Smith 	    break;
1433421f2f7dSMike Smith 	case MLX_BACKGROUND_REBUILD:
1434421f2f7dSMike Smith 	    device_printf(sc->mlx_dev, "drive rebuild completed\n");	/* XXX print channel/target? */
1435421f2f7dSMike Smith 	    break;
1436421f2f7dSMike Smith 	case MLX_BACKGROUND_SPONTANEOUS:
1437421f2f7dSMike Smith 	default:
1438421f2f7dSMike Smith 	    /* if we have previously been non-idle, report the transition */
1439421f2f7dSMike Smith 	    if (sc->mlx_rebuildstat.rs_code != MLX_REBUILDSTAT_IDLE) {
1440421f2f7dSMike Smith 		device_printf(sc->mlx_dev, "background check/rebuild operation completed\n");
14411ac4b82bSMike Smith 	    }
1442421f2f7dSMike Smith 	}
1443421f2f7dSMike Smith 	sc->mlx_background = 0;
1444421f2f7dSMike Smith 	sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
14451ac4b82bSMike Smith 	break;
14461ac4b82bSMike Smith     }
14471ac4b82bSMike Smith     free(mc->mc_data, M_DEVBUF);
14481ac4b82bSMike Smith     mlx_releasecmd(mc);
14491ac4b82bSMike Smith }
14501ac4b82bSMike Smith 
14511ac4b82bSMike Smith /********************************************************************************
14521ac4b82bSMike Smith  ********************************************************************************
14531ac4b82bSMike Smith                                                                     Channel Pause
14541ac4b82bSMike Smith  ********************************************************************************
14551ac4b82bSMike Smith  ********************************************************************************/
14561ac4b82bSMike Smith 
14571ac4b82bSMike Smith /********************************************************************************
14581ac4b82bSMike Smith  * It's time to perform a channel pause action for (sc), either start or stop
14591ac4b82bSMike Smith  * the pause.
14601ac4b82bSMike Smith  */
14611ac4b82bSMike Smith static void
14621ac4b82bSMike Smith mlx_pause_action(struct mlx_softc *sc)
14631ac4b82bSMike Smith {
14641ac4b82bSMike Smith     struct mlx_command	*mc;
14651ac4b82bSMike Smith     int			failsafe, i, command;
14661ac4b82bSMike Smith 
14670fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
14680fca6f8bSJohn Baldwin 
14691ac4b82bSMike Smith     /* What are we doing here? */
14701ac4b82bSMike Smith     if (sc->mlx_pause.mp_when == 0) {
14711ac4b82bSMike Smith 	command = MLX_CMD_STARTCHANNEL;
14721ac4b82bSMike Smith 	failsafe = 0;
14731ac4b82bSMike Smith 
14741ac4b82bSMike Smith     } else {
14751ac4b82bSMike Smith 	command = MLX_CMD_STOPCHANNEL;
14761ac4b82bSMike Smith 
14771ac4b82bSMike Smith 	/*
14781ac4b82bSMike Smith 	 * Channels will always start again after the failsafe period,
14791ac4b82bSMike Smith 	 * which is specified in multiples of 30 seconds.
14801ac4b82bSMike Smith 	 * This constrains us to a maximum pause of 450 seconds.
14811ac4b82bSMike Smith 	 */
14821ac4b82bSMike Smith 	failsafe = ((sc->mlx_pause.mp_howlong - time_second) + 5) / 30;
14831ac4b82bSMike Smith 	if (failsafe > 0xf) {
14841ac4b82bSMike Smith 	    failsafe = 0xf;
14851ac4b82bSMike Smith 	    sc->mlx_pause.mp_howlong = time_second + (0xf * 30) - 5;
14861ac4b82bSMike Smith 	}
14871ac4b82bSMike Smith     }
14881ac4b82bSMike Smith 
14891ac4b82bSMike Smith     /* build commands for every channel requested */
14909eee27f1SMike Smith     for (i = 0; i < sc->mlx_enq2->me_actual_channels; i++) {
14911ac4b82bSMike Smith 	if ((1 << i) & sc->mlx_pause.mp_which) {
14921ac4b82bSMike Smith 
14931ac4b82bSMike Smith 	    /* get ourselves a command buffer */
14941ac4b82bSMike Smith 	    if ((mc = mlx_alloccmd(sc)) == NULL)
14951ac4b82bSMike Smith 		goto fail;
14961ac4b82bSMike Smith 	    /* get a command slot */
14971ac4b82bSMike Smith 	    mc->mc_flags |= MLX_CMD_PRIORITY;
14981ac4b82bSMike Smith 	    if (mlx_getslot(mc))
14991ac4b82bSMike Smith 		goto fail;
15001ac4b82bSMike Smith 
15011ac4b82bSMike Smith 	    /* build the command */
15021ac4b82bSMike Smith 	    mlx_make_type2(mc, command, (failsafe << 4) | i, 0, 0, 0, 0, 0, 0, 0);
15031ac4b82bSMike Smith 	    mc->mc_complete = mlx_pause_done;
15041ac4b82bSMike Smith 	    mc->mc_private = sc;		/* XXX not needed */
15051ac4b82bSMike Smith 	    if (mlx_start(mc))
15061ac4b82bSMike Smith 		goto fail;
15071ac4b82bSMike Smith 	    /* command submitted OK */
15081ac4b82bSMike Smith 	    return;
15091ac4b82bSMike Smith 
15101ac4b82bSMike Smith 	fail:
15111ac4b82bSMike Smith 	    device_printf(sc->mlx_dev, "%s failed for channel %d\n",
15121ac4b82bSMike Smith 			  command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", i);
15131ac4b82bSMike Smith 	    if (mc != NULL)
15141ac4b82bSMike Smith 		mlx_releasecmd(mc);
15151ac4b82bSMike Smith 	}
15161ac4b82bSMike Smith     }
15171ac4b82bSMike Smith }
15181ac4b82bSMike Smith 
15191ac4b82bSMike Smith static void
15201ac4b82bSMike Smith mlx_pause_done(struct mlx_command *mc)
15211ac4b82bSMike Smith {
15221ac4b82bSMike Smith     struct mlx_softc	*sc = mc->mc_sc;
15231ac4b82bSMike Smith     int			command = mc->mc_mailbox[0];
15241ac4b82bSMike Smith     int			channel = mc->mc_mailbox[2] & 0xf;
15251ac4b82bSMike Smith 
15260fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
15271ac4b82bSMike Smith     if (mc->mc_status != 0) {
15281ac4b82bSMike Smith 	device_printf(sc->mlx_dev, "%s command failed - %s\n",
15291ac4b82bSMike Smith 		      command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", mlx_diagnose_command(mc));
15301ac4b82bSMike Smith     } else if (command == MLX_CMD_STOPCHANNEL) {
15311ac4b82bSMike Smith 	device_printf(sc->mlx_dev, "channel %d pausing for %ld seconds\n",
153272c10febSPeter Wemm 		      channel, (long)(sc->mlx_pause.mp_howlong - time_second));
15331ac4b82bSMike Smith     } else {
15341ac4b82bSMike Smith 	device_printf(sc->mlx_dev, "channel %d resuming\n", channel);
15351ac4b82bSMike Smith     }
15361ac4b82bSMike Smith     mlx_releasecmd(mc);
15371ac4b82bSMike Smith }
15381ac4b82bSMike Smith 
15391ac4b82bSMike Smith /********************************************************************************
15401ac4b82bSMike Smith  ********************************************************************************
15411ac4b82bSMike Smith                                                                Command Submission
15421ac4b82bSMike Smith  ********************************************************************************
15431ac4b82bSMike Smith  ********************************************************************************/
15441ac4b82bSMike Smith 
15451b4404f9SScott Long static void
15461b4404f9SScott Long mlx_enquire_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
15471b4404f9SScott Long {
15481b4404f9SScott Long     struct mlx_softc *sc;
15491b4404f9SScott Long     struct mlx_command *mc;
15501b4404f9SScott Long 
15511b4404f9SScott Long     mc = (struct mlx_command *)arg;
15521b4404f9SScott Long     if (error)
15531b4404f9SScott Long 	return;
15541b4404f9SScott Long 
15551b4404f9SScott Long     mlx_setup_dmamap(mc, segs, nsegments, error);
15561b4404f9SScott Long 
15571b4404f9SScott Long     /* build an enquiry command */
15581b4404f9SScott Long     sc = mc->mc_sc;
15591b4404f9SScott Long     mlx_make_type2(mc, mc->mc_command, 0, 0, 0, 0, 0, 0, mc->mc_dataphys, 0);
15601b4404f9SScott Long 
15611b4404f9SScott Long     /* do we want a completion callback? */
15621b4404f9SScott Long     if (mc->mc_complete != NULL) {
15631b4404f9SScott Long 	if ((error = mlx_start(mc)) != 0)
15641b4404f9SScott Long 	    return;
15651b4404f9SScott Long     } else {
15661b4404f9SScott Long 	/* run the command in either polled or wait mode */
15671b4404f9SScott Long 	if ((sc->mlx_state & MLX_STATE_INTEN) ? mlx_wait_command(mc) :
15681b4404f9SScott Long 						mlx_poll_command(mc))
15691b4404f9SScott Long 	    return;
15701b4404f9SScott Long 
15711b4404f9SScott Long 	/* command completed OK? */
15721b4404f9SScott Long 	if (mc->mc_status != 0) {
15731b4404f9SScott Long 	    device_printf(sc->mlx_dev, "ENQUIRY failed - %s\n",
15741b4404f9SScott Long 			  mlx_diagnose_command(mc));
15751b4404f9SScott Long 	    return;
15761b4404f9SScott Long 	}
15771b4404f9SScott Long     }
15781b4404f9SScott Long }
15791b4404f9SScott Long 
15801ac4b82bSMike Smith /********************************************************************************
15811ac4b82bSMike Smith  * Perform an Enquiry command using a type-3 command buffer and a return a single
15821ac4b82bSMike Smith  * linear result buffer.  If the completion function is specified, it will
15831ac4b82bSMike Smith  * be called with the completed command (and the result response will not be
15841ac4b82bSMike Smith  * valid until that point).  Otherwise, the command will either be busy-waited
15851ac4b82bSMike Smith  * for (interrupts not enabled), or slept for.
15861ac4b82bSMike Smith  */
15871ac4b82bSMike Smith static void *
15881ac4b82bSMike Smith mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize, void (* complete)(struct mlx_command *mc))
15891ac4b82bSMike Smith {
15901ac4b82bSMike Smith     struct mlx_command	*mc;
15911ac4b82bSMike Smith     void		*result;
15921ac4b82bSMike Smith     int			error;
15931ac4b82bSMike Smith 
1594da8bb3a3SMike Smith     debug_called(1);
15950fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
15961ac4b82bSMike Smith 
15971ac4b82bSMike Smith     /* get ourselves a command buffer */
15981ac4b82bSMike Smith     error = 1;
15991ac4b82bSMike Smith     result = NULL;
16001ac4b82bSMike Smith     if ((mc = mlx_alloccmd(sc)) == NULL)
16011ac4b82bSMike Smith 	goto out;
16021ac4b82bSMike Smith     /* allocate the response structure */
16031ac4b82bSMike Smith     if ((result = malloc(bufsize, M_DEVBUF, M_NOWAIT)) == NULL)
16041ac4b82bSMike Smith 	goto out;
16051ac4b82bSMike Smith     /* get a command slot */
16061ac4b82bSMike Smith     mc->mc_flags |= MLX_CMD_PRIORITY | MLX_CMD_DATAOUT;
16071ac4b82bSMike Smith     if (mlx_getslot(mc))
16081ac4b82bSMike Smith 	goto out;
16091ac4b82bSMike Smith 
16101ac4b82bSMike Smith     /* map the command so the controller can see it */
16111ac4b82bSMike Smith     mc->mc_data = result;
16121ac4b82bSMike Smith     mc->mc_length = bufsize;
16131b4404f9SScott Long     mc->mc_command = command;
16141ac4b82bSMike Smith 
16151ac4b82bSMike Smith     if (complete != NULL) {
16161ac4b82bSMike Smith 	mc->mc_complete = complete;
16171ac4b82bSMike Smith 	mc->mc_private = mc;
16181b4404f9SScott Long     }
16191ac4b82bSMike Smith 
16201b4404f9SScott Long     error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
16211b4404f9SScott Long 			    mc->mc_length, mlx_enquire_cb, mc, BUS_DMA_NOWAIT);
16221b4404f9SScott Long 
16231ac4b82bSMike Smith  out:
16241ac4b82bSMike Smith     /* we got a command, but nobody else will free it */
162523691262SSam Leffler     if ((mc != NULL) && (mc->mc_complete == NULL))
16261ac4b82bSMike Smith 	mlx_releasecmd(mc);
162733c8cb18SMike Smith     /* we got an error, and we allocated a result */
1628d4881905SScott Long     if ((error != 0) && (result != NULL)) {
1629d4881905SScott Long 	free(result, M_DEVBUF);
1630a7700303SScott Long 	result = NULL;
16311ac4b82bSMike Smith     }
16321ac4b82bSMike Smith     return(result);
16331ac4b82bSMike Smith }
16341ac4b82bSMike Smith 
16351ac4b82bSMike Smith 
16361ac4b82bSMike Smith /********************************************************************************
16371ac4b82bSMike Smith  * Perform a Flush command on the nominated controller.
16381ac4b82bSMike Smith  *
16391ac4b82bSMike Smith  * May be called with interrupts enabled or disabled; will not return until
16401ac4b82bSMike Smith  * the flush operation completes or fails.
16411ac4b82bSMike Smith  */
16421ac4b82bSMike Smith static int
16431ac4b82bSMike Smith mlx_flush(struct mlx_softc *sc)
16441ac4b82bSMike Smith {
16451ac4b82bSMike Smith     struct mlx_command	*mc;
16461ac4b82bSMike Smith     int			error;
16471ac4b82bSMike Smith 
1648da8bb3a3SMike Smith     debug_called(1);
16490fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
16501ac4b82bSMike Smith 
16511ac4b82bSMike Smith     /* get ourselves a command buffer */
16521ac4b82bSMike Smith     error = 1;
16531ac4b82bSMike Smith     if ((mc = mlx_alloccmd(sc)) == NULL)
16541ac4b82bSMike Smith 	goto out;
16551ac4b82bSMike Smith     /* get a command slot */
16561ac4b82bSMike Smith     if (mlx_getslot(mc))
16571ac4b82bSMike Smith 	goto out;
16581ac4b82bSMike Smith 
16591ac4b82bSMike Smith     /* build a flush command */
16601ac4b82bSMike Smith     mlx_make_type2(mc, MLX_CMD_FLUSH, 0, 0, 0, 0, 0, 0, 0, 0);
16611ac4b82bSMike Smith 
16625792b7feSMike Smith     /* can't assume that interrupts are going to work here, so play it safe */
16635792b7feSMike Smith     if (mlx_poll_command(mc))
16641ac4b82bSMike Smith 	goto out;
16651ac4b82bSMike Smith 
16661ac4b82bSMike Smith     /* command completed OK? */
16671ac4b82bSMike Smith     if (mc->mc_status != 0) {
16681ac4b82bSMike Smith 	device_printf(sc->mlx_dev, "FLUSH failed - %s\n", mlx_diagnose_command(mc));
16691ac4b82bSMike Smith 	goto out;
16701ac4b82bSMike Smith     }
16711ac4b82bSMike Smith 
16721ac4b82bSMike Smith     error = 0;			/* success */
16731ac4b82bSMike Smith  out:
16741ac4b82bSMike Smith     if (mc != NULL)
16751ac4b82bSMike Smith 	mlx_releasecmd(mc);
16761ac4b82bSMike Smith     return(error);
16771ac4b82bSMike Smith }
16781ac4b82bSMike Smith 
16791ac4b82bSMike Smith /********************************************************************************
1680421f2f7dSMike Smith  * Start a background consistency check on (drive).
1681421f2f7dSMike Smith  *
1682421f2f7dSMike Smith  * May be called with interrupts enabled or disabled; will return as soon as the
1683421f2f7dSMike Smith  * operation has started or been refused.
1684421f2f7dSMike Smith  */
1685421f2f7dSMike Smith static int
1686421f2f7dSMike Smith mlx_check(struct mlx_softc *sc, int drive)
1687421f2f7dSMike Smith {
1688421f2f7dSMike Smith     struct mlx_command	*mc;
1689421f2f7dSMike Smith     int			error;
1690421f2f7dSMike Smith 
1691421f2f7dSMike Smith     debug_called(1);
16920fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
1693421f2f7dSMike Smith 
1694421f2f7dSMike Smith     /* get ourselves a command buffer */
1695421f2f7dSMike Smith     error = 0x10000;
1696421f2f7dSMike Smith     if ((mc = mlx_alloccmd(sc)) == NULL)
1697421f2f7dSMike Smith 	goto out;
1698421f2f7dSMike Smith     /* get a command slot */
1699421f2f7dSMike Smith     if (mlx_getslot(mc))
1700421f2f7dSMike Smith 	goto out;
1701421f2f7dSMike Smith 
1702421f2f7dSMike Smith     /* build a checkasync command, set the "fix it" flag */
1703421f2f7dSMike Smith     mlx_make_type2(mc, MLX_CMD_CHECKASYNC, 0, 0, 0, 0, 0, drive | 0x80, 0, 0);
1704421f2f7dSMike Smith 
1705421f2f7dSMike Smith     /* start the command and wait for it to be returned */
1706421f2f7dSMike Smith     if (mlx_wait_command(mc))
1707421f2f7dSMike Smith 	goto out;
1708421f2f7dSMike Smith 
1709421f2f7dSMike Smith     /* command completed OK? */
1710421f2f7dSMike Smith     if (mc->mc_status != 0) {
1711421f2f7dSMike Smith 	device_printf(sc->mlx_dev, "CHECK ASYNC failed - %s\n", mlx_diagnose_command(mc));
1712421f2f7dSMike Smith     } else {
1713421f2f7dSMike Smith 	device_printf(sc->mlx_sysdrive[drive].ms_disk, "consistency check started");
1714421f2f7dSMike Smith     }
1715421f2f7dSMike Smith     error = mc->mc_status;
1716421f2f7dSMike Smith 
1717421f2f7dSMike Smith  out:
1718421f2f7dSMike Smith     if (mc != NULL)
1719421f2f7dSMike Smith 	mlx_releasecmd(mc);
1720421f2f7dSMike Smith     return(error);
1721421f2f7dSMike Smith }
1722421f2f7dSMike Smith 
1723421f2f7dSMike Smith /********************************************************************************
1724421f2f7dSMike Smith  * Start a background rebuild of the physical drive at (channel),(target).
17251ac4b82bSMike Smith  *
17261ac4b82bSMike Smith  * May be called with interrupts enabled or disabled; will return as soon as the
17271ac4b82bSMike Smith  * operation has started or been refused.
17281ac4b82bSMike Smith  */
17291ac4b82bSMike Smith static int
17301ac4b82bSMike Smith mlx_rebuild(struct mlx_softc *sc, int channel, int target)
17311ac4b82bSMike Smith {
17321ac4b82bSMike Smith     struct mlx_command	*mc;
17331ac4b82bSMike Smith     int			error;
17341ac4b82bSMike Smith 
1735da8bb3a3SMike Smith     debug_called(1);
17360fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
17371ac4b82bSMike Smith 
17381ac4b82bSMike Smith     /* get ourselves a command buffer */
17391ac4b82bSMike Smith     error = 0x10000;
17401ac4b82bSMike Smith     if ((mc = mlx_alloccmd(sc)) == NULL)
17411ac4b82bSMike Smith 	goto out;
17421ac4b82bSMike Smith     /* get a command slot */
17431ac4b82bSMike Smith     if (mlx_getslot(mc))
17441ac4b82bSMike Smith 	goto out;
17451ac4b82bSMike Smith 
1746421f2f7dSMike Smith     /* build a checkasync command, set the "fix it" flag */
17471ac4b82bSMike Smith     mlx_make_type2(mc, MLX_CMD_REBUILDASYNC, channel, target, 0, 0, 0, 0, 0, 0);
17481ac4b82bSMike Smith 
1749421f2f7dSMike Smith     /* start the command and wait for it to be returned */
1750421f2f7dSMike Smith     if (mlx_wait_command(mc))
17511ac4b82bSMike Smith 	goto out;
17521ac4b82bSMike Smith 
17531ac4b82bSMike Smith     /* command completed OK? */
17541ac4b82bSMike Smith     if (mc->mc_status != 0) {
17551ac4b82bSMike Smith 	device_printf(sc->mlx_dev, "REBUILD ASYNC failed - %s\n", mlx_diagnose_command(mc));
17561ac4b82bSMike Smith     } else {
1757421f2f7dSMike Smith 	device_printf(sc->mlx_dev, "drive rebuild started for %d:%d\n", channel, target);
17581ac4b82bSMike Smith     }
17591ac4b82bSMike Smith     error = mc->mc_status;
17601ac4b82bSMike Smith 
17611ac4b82bSMike Smith  out:
17621ac4b82bSMike Smith     if (mc != NULL)
17631ac4b82bSMike Smith 	mlx_releasecmd(mc);
17641ac4b82bSMike Smith     return(error);
17651ac4b82bSMike Smith }
17661ac4b82bSMike Smith 
17671ac4b82bSMike Smith /********************************************************************************
17681ac4b82bSMike Smith  * Run the command (mc) and return when it completes.
17691ac4b82bSMike Smith  *
17701ac4b82bSMike Smith  * Interrupts need to be enabled; returns nonzero on error.
17711ac4b82bSMike Smith  */
17721ac4b82bSMike Smith static int
17731ac4b82bSMike Smith mlx_wait_command(struct mlx_command *mc)
17741ac4b82bSMike Smith {
17751ac4b82bSMike Smith     struct mlx_softc	*sc = mc->mc_sc;
17761ac4b82bSMike Smith     int			error, count;
17771ac4b82bSMike Smith 
1778da8bb3a3SMike Smith     debug_called(1);
17790fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
17801ac4b82bSMike Smith 
17811ac4b82bSMike Smith     mc->mc_complete = NULL;
17821ac4b82bSMike Smith     mc->mc_private = mc;		/* wake us when you're done */
17831ac4b82bSMike Smith     if ((error = mlx_start(mc)) != 0)
17841ac4b82bSMike Smith 	return(error);
17851ac4b82bSMike Smith 
17861ac4b82bSMike Smith     count = 0;
17871ac4b82bSMike Smith     /* XXX better timeout? */
17881ac4b82bSMike Smith     while ((mc->mc_status == MLX_STATUS_BUSY) && (count < 30)) {
17890fca6f8bSJohn Baldwin 	mtx_sleep(mc->mc_private, &sc->mlx_io_lock, PRIBIO | PCATCH, "mlxwcmd", hz);
17901ac4b82bSMike Smith     }
17911ac4b82bSMike Smith 
17921ac4b82bSMike Smith     if (mc->mc_status != 0) {
1793da8bb3a3SMike Smith 	device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
17941ac4b82bSMike Smith 	return(EIO);
17951ac4b82bSMike Smith     }
17961ac4b82bSMike Smith     return(0);
17971ac4b82bSMike Smith }
17981ac4b82bSMike Smith 
17991ac4b82bSMike Smith 
18001ac4b82bSMike Smith /********************************************************************************
18011ac4b82bSMike Smith  * Start the command (mc) and busy-wait for it to complete.
18021ac4b82bSMike Smith  *
1803da8bb3a3SMike Smith  * Should only be used when interrupts can't be relied upon. Returns 0 on
18041ac4b82bSMike Smith  * success, nonzero on error.
18051ac4b82bSMike Smith  * Successfully completed commands are dequeued.
18061ac4b82bSMike Smith  */
18071ac4b82bSMike Smith static int
18081ac4b82bSMike Smith mlx_poll_command(struct mlx_command *mc)
18091ac4b82bSMike Smith {
18101ac4b82bSMike Smith     struct mlx_softc	*sc = mc->mc_sc;
18110fca6f8bSJohn Baldwin     int			error, count;
18121ac4b82bSMike Smith 
1813da8bb3a3SMike Smith     debug_called(1);
18140fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
18151ac4b82bSMike Smith 
18161ac4b82bSMike Smith     mc->mc_complete = NULL;
18171ac4b82bSMike Smith     mc->mc_private = NULL;	/* we will poll for it */
18181ac4b82bSMike Smith     if ((error = mlx_start(mc)) != 0)
18191ac4b82bSMike Smith 	return(error);
18201ac4b82bSMike Smith 
18211ac4b82bSMike Smith     count = 0;
18221ac4b82bSMike Smith     do {
18231ac4b82bSMike Smith 	/* poll for completion */
18240fca6f8bSJohn Baldwin 	mlx_done(mc->mc_sc, 1);
1825da8bb3a3SMike Smith 
1826da8bb3a3SMike Smith     } while ((mc->mc_status == MLX_STATUS_BUSY) && (count++ < 15000000));
18271ac4b82bSMike Smith     if (mc->mc_status != MLX_STATUS_BUSY) {
18284b006d7bSMike Smith 	TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
18291ac4b82bSMike Smith 	return(0);
18301ac4b82bSMike Smith     }
1831421f2f7dSMike Smith     device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
18321ac4b82bSMike Smith     return(EIO);
18331ac4b82bSMike Smith }
18341ac4b82bSMike Smith 
18351b4404f9SScott Long void
18361b4404f9SScott Long mlx_startio_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
18371b4404f9SScott Long {
18381b4404f9SScott Long     struct mlx_command	*mc;
18391b4404f9SScott Long     struct mlxd_softc	*mlxd;
18401b4404f9SScott Long     struct mlx_softc	*sc;
1841*b04e4c12SJohn Baldwin     struct bio		*bp;
18421b4404f9SScott Long     int			blkcount;
18431b4404f9SScott Long     int			driveno;
18441b4404f9SScott Long     int			cmd;
18451b4404f9SScott Long 
18461b4404f9SScott Long     mc = (struct mlx_command *)arg;
18471b4404f9SScott Long     mlx_setup_dmamap(mc, segs, nsegments, error);
18481b4404f9SScott Long 
18491b4404f9SScott Long     sc = mc->mc_sc;
18501b4404f9SScott Long     bp = mc->mc_private;
18511b4404f9SScott Long 
1852*b04e4c12SJohn Baldwin     if (bp->bio_cmd == BIO_READ) {
18531b4404f9SScott Long 	mc->mc_flags |= MLX_CMD_DATAIN;
18541b4404f9SScott Long 	cmd = MLX_CMD_READSG;
18551b4404f9SScott Long     } else {
18561b4404f9SScott Long 	mc->mc_flags |= MLX_CMD_DATAOUT;
18571b4404f9SScott Long 	cmd = MLX_CMD_WRITESG;
18581b4404f9SScott Long     }
18591b4404f9SScott Long 
18601b4404f9SScott Long     /* build a suitable I/O command (assumes 512-byte rounded transfers) */
1861*b04e4c12SJohn Baldwin     mlxd = bp->bio_disk->d_drv1;
18621b4404f9SScott Long     driveno = mlxd->mlxd_drive - sc->mlx_sysdrive;
1863*b04e4c12SJohn Baldwin     blkcount = (bp->bio_bcount + MLX_BLKSIZE - 1) / MLX_BLKSIZE;
18641b4404f9SScott Long 
1865*b04e4c12SJohn Baldwin     if ((bp->bio_pblkno + blkcount) > sc->mlx_sysdrive[driveno].ms_size)
18661b4404f9SScott Long 	device_printf(sc->mlx_dev,
18671b4404f9SScott Long 		      "I/O beyond end of unit (%lld,%d > %lu)\n",
1868*b04e4c12SJohn Baldwin 		      (long long)bp->bio_pblkno, blkcount,
18691b4404f9SScott Long 		      (u_long)sc->mlx_sysdrive[driveno].ms_size);
18701b4404f9SScott Long 
18711b4404f9SScott Long     /*
18721b4404f9SScott Long      * Build the I/O command.  Note that the SG list type bits are set to zero,
18731b4404f9SScott Long      * denoting the format of SG list that we are using.
18741b4404f9SScott Long      */
18751b4404f9SScott Long     if (sc->mlx_iftype == MLX_IFTYPE_2) {
18761b4404f9SScott Long 	mlx_make_type1(mc, (cmd == MLX_CMD_WRITESG) ? MLX_CMD_WRITESG_OLD :
18771b4404f9SScott Long 						      MLX_CMD_READSG_OLD,
18781b4404f9SScott Long 		       blkcount & 0xff, 	/* xfer length low byte */
1879*b04e4c12SJohn Baldwin 		       bp->bio_pblkno,		/* physical block number */
18801b4404f9SScott Long 		       driveno,			/* target drive number */
18811b4404f9SScott Long 		       mc->mc_sgphys,		/* location of SG list */
18821b4404f9SScott Long 		       mc->mc_nsgent & 0x3f);	/* size of SG list */
18831b4404f9SScott Long 	} else {
18841b4404f9SScott Long 	mlx_make_type5(mc, cmd,
18851b4404f9SScott Long 		       blkcount & 0xff, 	/* xfer length low byte */
18861b4404f9SScott Long 		       (driveno << 3) | ((blkcount >> 8) & 0x07),
18871b4404f9SScott Long 						/* target+length high 3 bits */
1888*b04e4c12SJohn Baldwin 		       bp->bio_pblkno,		/* physical block number */
18891b4404f9SScott Long 		       mc->mc_sgphys,		/* location of SG list */
18901b4404f9SScott Long 		       mc->mc_nsgent & 0x3f);	/* size of SG list */
18911b4404f9SScott Long     }
18921b4404f9SScott Long 
18931b4404f9SScott Long     /* try to give command to controller */
18941b4404f9SScott Long     if (mlx_start(mc) != 0) {
18951b4404f9SScott Long 	/* fail the command */
18961b4404f9SScott Long 	mc->mc_status = MLX_STATUS_WEDGED;
18971b4404f9SScott Long 	mlx_completeio(mc);
18981b4404f9SScott Long     }
18990fca6f8bSJohn Baldwin 
19000fca6f8bSJohn Baldwin     sc->mlx_state &= ~MLX_STATE_QFROZEN;
19011b4404f9SScott Long }
19021b4404f9SScott Long 
19031ac4b82bSMike Smith /********************************************************************************
19041ac4b82bSMike Smith  * Pull as much work off the softc's work queue as possible and give it to the
19051ac4b82bSMike Smith  * controller.  Leave a couple of slots free for emergencies.
19061ac4b82bSMike Smith  */
19071ac4b82bSMike Smith static void
19081ac4b82bSMike Smith mlx_startio(struct mlx_softc *sc)
19091ac4b82bSMike Smith {
19101ac4b82bSMike Smith     struct mlx_command	*mc;
1911*b04e4c12SJohn Baldwin     struct bio		*bp;
19121b4404f9SScott Long     int			error;
19131ac4b82bSMike Smith 
19140fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
19155792b7feSMike Smith 
19161ac4b82bSMike Smith     /* spin until something prevents us from doing any work */
19171ac4b82bSMike Smith     for (;;) {
19180fca6f8bSJohn Baldwin 	if (sc->mlx_state & MLX_STATE_QFROZEN)
19190fca6f8bSJohn Baldwin 	    break;
19201ac4b82bSMike Smith 
19211ac4b82bSMike Smith 	/* see if there's work to be done */
1922*b04e4c12SJohn Baldwin 	if ((bp = bioq_first(&sc->mlx_bioq)) == NULL)
19231ac4b82bSMike Smith 	    break;
19241ac4b82bSMike Smith 	/* get a command */
19251ac4b82bSMike Smith 	if ((mc = mlx_alloccmd(sc)) == NULL)
19261ac4b82bSMike Smith 	    break;
19271ac4b82bSMike Smith 	/* get a slot for the command */
19281ac4b82bSMike Smith 	if (mlx_getslot(mc) != 0) {
19291ac4b82bSMike Smith 	    mlx_releasecmd(mc);
19301ac4b82bSMike Smith 	    break;
19311ac4b82bSMike Smith 	}
19321ac4b82bSMike Smith 	/* get the buf containing our work */
1933*b04e4c12SJohn Baldwin 	bioq_remove(&sc->mlx_bioq, bp);
19341ac4b82bSMike Smith 	sc->mlx_waitbufs--;
19351ac4b82bSMike Smith 
19361ac4b82bSMike Smith 	/* connect the buf to the command */
19371ac4b82bSMike Smith 	mc->mc_complete = mlx_completeio;
19381ac4b82bSMike Smith 	mc->mc_private = bp;
1939*b04e4c12SJohn Baldwin 	mc->mc_data = bp->bio_data;
1940*b04e4c12SJohn Baldwin 	mc->mc_length = bp->bio_bcount;
19411ac4b82bSMike Smith 
19421ac4b82bSMike Smith 	/* map the command so the controller can work with it */
19431b4404f9SScott Long 	error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
19441b4404f9SScott Long 				mc->mc_length, mlx_startio_cb, mc, 0);
19451b4404f9SScott Long 	if (error == EINPROGRESS) {
19460fca6f8bSJohn Baldwin 	    sc->mlx_state |= MLX_STATE_QFROZEN;
19471b4404f9SScott Long 	    break;
1948da8bb3a3SMike Smith 	}
19491ac4b82bSMike Smith     }
19501ac4b82bSMike Smith }
19511ac4b82bSMike Smith 
19521ac4b82bSMike Smith /********************************************************************************
19531ac4b82bSMike Smith  * Handle completion of an I/O command.
19541ac4b82bSMike Smith  */
19551ac4b82bSMike Smith static void
19561ac4b82bSMike Smith mlx_completeio(struct mlx_command *mc)
19571ac4b82bSMike Smith {
19581ac4b82bSMike Smith     struct mlx_softc	*sc = mc->mc_sc;
1959*b04e4c12SJohn Baldwin     struct bio		*bp = mc->mc_private;
1960*b04e4c12SJohn Baldwin     struct mlxd_softc	*mlxd = bp->bio_disk->d_drv1;
19611ac4b82bSMike Smith 
19620fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
19631ac4b82bSMike Smith     if (mc->mc_status != MLX_STATUS_OK) {	/* could be more verbose here? */
1964*b04e4c12SJohn Baldwin 	bp->bio_error = EIO;
1965*b04e4c12SJohn Baldwin 	bp->bio_flags |= BIO_ERROR;
19661ac4b82bSMike Smith 
19671ac4b82bSMike Smith 	switch(mc->mc_status) {
19681ac4b82bSMike Smith 	case MLX_STATUS_RDWROFFLINE:		/* system drive has gone offline */
19691ac4b82bSMike Smith 	    device_printf(mlxd->mlxd_dev, "drive offline\n");
1970f6b84b08SMike Smith 	    /* should signal this with a return code */
19711ac4b82bSMike Smith 	    mlxd->mlxd_drive->ms_state = MLX_SYSD_OFFLINE;
19721ac4b82bSMike Smith 	    break;
19731ac4b82bSMike Smith 
19741ac4b82bSMike Smith 	default:				/* other I/O error */
19751ac4b82bSMike Smith 	    device_printf(sc->mlx_dev, "I/O error - %s\n", mlx_diagnose_command(mc));
19761ac4b82bSMike Smith #if 0
1977cd4ace0cSMike Smith 	    device_printf(sc->mlx_dev, "  b_bcount %ld  blkcount %ld  b_pblkno %d\n",
1978*b04e4c12SJohn Baldwin 			  bp->bio_bcount, bp->bio_bcount / MLX_BLKSIZE, bp->bio_pblkno);
19791ac4b82bSMike Smith 	    device_printf(sc->mlx_dev, "  %13D\n", mc->mc_mailbox, " ");
19801ac4b82bSMike Smith #endif
19811ac4b82bSMike Smith 	    break;
19821ac4b82bSMike Smith 	}
19831ac4b82bSMike Smith     }
19841ac4b82bSMike Smith     mlx_releasecmd(mc);
19851ac4b82bSMike Smith     mlxd_intr(bp);
19861ac4b82bSMike Smith }
19871ac4b82bSMike Smith 
19881b4404f9SScott Long void
19891b4404f9SScott Long mlx_user_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
19901b4404f9SScott Long {
19911b4404f9SScott Long     struct mlx_usercommand *mu;
19921b4404f9SScott Long     struct mlx_command *mc;
19931b4404f9SScott Long     struct mlx_dcdb	*dcdb;
19941b4404f9SScott Long 
19951b4404f9SScott Long     mc = (struct mlx_command *)arg;
19961b4404f9SScott Long     if (error)
19971b4404f9SScott Long 	return;
19981b4404f9SScott Long 
19991b4404f9SScott Long     mlx_setup_dmamap(mc, segs, nsegments, error);
20001b4404f9SScott Long 
20011b4404f9SScott Long     mu = (struct mlx_usercommand *)mc->mc_private;
20021b4404f9SScott Long     dcdb = NULL;
20031b4404f9SScott Long 
20041b4404f9SScott Long     /*
20051b4404f9SScott Long      * If this is a passthrough SCSI command, the DCDB is packed at the
20061b4404f9SScott Long      * beginning of the data area.  Fix up the DCDB to point to the correct
20071b4404f9SScott Long      * physical address and override any bufptr supplied by the caller since
20081b4404f9SScott Long      * we know what it's meant to be.
20091b4404f9SScott Long      */
20101b4404f9SScott Long     if (mc->mc_mailbox[0] == MLX_CMD_DIRECT_CDB) {
20111b4404f9SScott Long 	dcdb = (struct mlx_dcdb *)mc->mc_data;
20121b4404f9SScott Long 	dcdb->dcdb_physaddr = mc->mc_dataphys + sizeof(*dcdb);
20131b4404f9SScott Long 	mu->mu_bufptr = 8;
20141b4404f9SScott Long     }
20151b4404f9SScott Long 
20161b4404f9SScott Long     /*
20171b4404f9SScott Long      * If there's a data buffer, fix up the command's buffer pointer.
20181b4404f9SScott Long      */
20191b4404f9SScott Long     if (mu->mu_datasize > 0) {
20201b4404f9SScott Long 	mc->mc_mailbox[mu->mu_bufptr    ] =  mc->mc_dataphys        & 0xff;
20211b4404f9SScott Long 	mc->mc_mailbox[mu->mu_bufptr + 1] = (mc->mc_dataphys >> 8)  & 0xff;
20221b4404f9SScott Long 	mc->mc_mailbox[mu->mu_bufptr + 2] = (mc->mc_dataphys >> 16) & 0xff;
20231b4404f9SScott Long 	mc->mc_mailbox[mu->mu_bufptr + 3] = (mc->mc_dataphys >> 24) & 0xff;
20241b4404f9SScott Long     }
20251b4404f9SScott Long     debug(0, "command fixup");
20261b4404f9SScott Long 
20271b4404f9SScott Long     /* submit the command and wait */
20281b4404f9SScott Long     if (mlx_wait_command(mc) != 0)
20291b4404f9SScott Long 	return;
20301b4404f9SScott Long 
20311b4404f9SScott Long }
20321b4404f9SScott Long 
20331ac4b82bSMike Smith /********************************************************************************
20341ac4b82bSMike Smith  * Take a command from user-space and try to run it.
2035da8bb3a3SMike Smith  *
2036da8bb3a3SMike Smith  * XXX Note that this can't perform very much in the way of error checking, and
2037da8bb3a3SMike Smith  *     as such, applications _must_ be considered trustworthy.
2038da8bb3a3SMike Smith  * XXX Commands using S/G for data are not supported.
20391ac4b82bSMike Smith  */
20401ac4b82bSMike Smith static int
20411ac4b82bSMike Smith mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu)
20421ac4b82bSMike Smith {
20431ac4b82bSMike Smith     struct mlx_command	*mc;
20441ac4b82bSMike Smith     void		*kbuf;
20451ac4b82bSMike Smith     int			error;
20461ac4b82bSMike Smith 
2047da8bb3a3SMike Smith     debug_called(0);
2048da8bb3a3SMike Smith 
20491ac4b82bSMike Smith     kbuf = NULL;
20501ac4b82bSMike Smith     mc = NULL;
20511ac4b82bSMike Smith     error = ENOMEM;
2052da8bb3a3SMike Smith 
2053da8bb3a3SMike Smith     /* get ourselves a command and copy in from user space */
20540fca6f8bSJohn Baldwin     MLX_IO_LOCK(sc);
20550fca6f8bSJohn Baldwin     if ((mc = mlx_alloccmd(sc)) == NULL) {
20560fca6f8bSJohn Baldwin 	MLX_IO_UNLOCK(sc);
2057001ea8fbSSam Leffler 	return(error);
20580fca6f8bSJohn Baldwin     }
20591ac4b82bSMike Smith     bcopy(mu->mu_command, mc->mc_mailbox, sizeof(mc->mc_mailbox));
2060da8bb3a3SMike Smith     debug(0, "got command buffer");
2061da8bb3a3SMike Smith 
20621b4404f9SScott Long     /*
20631b4404f9SScott Long      * if we need a buffer for data transfer, allocate one and copy in its
20641b4404f9SScott Long      * initial contents
20651b4404f9SScott Long      */
2066da8bb3a3SMike Smith     if (mu->mu_datasize > 0) {
206752c9ce25SScott Long 	if (mu->mu_datasize > MLX_MAXPHYS) {
2068aa083c3dSSam Leffler 	    error = EINVAL;
2069aa083c3dSSam Leffler 	    goto out;
2070aa083c3dSSam Leffler 	}
20710fca6f8bSJohn Baldwin 	MLX_IO_UNLOCK(sc);
2072a163d034SWarner Losh 	if (((kbuf = malloc(mu->mu_datasize, M_DEVBUF, M_WAITOK)) == NULL) ||
20730fca6f8bSJohn Baldwin 	    (error = copyin(mu->mu_buf, kbuf, mu->mu_datasize))) {
20740fca6f8bSJohn Baldwin 	    MLX_IO_LOCK(sc);
20751ac4b82bSMike Smith 	    goto out;
20760fca6f8bSJohn Baldwin 	}
20770fca6f8bSJohn Baldwin 	MLX_IO_LOCK(sc);
2078da8bb3a3SMike Smith 	debug(0, "got kernel buffer");
2079da8bb3a3SMike Smith     }
20801ac4b82bSMike Smith 
20811ac4b82bSMike Smith     /* get a command slot */
20821ac4b82bSMike Smith     if (mlx_getslot(mc))
20831ac4b82bSMike Smith 	goto out;
2084da8bb3a3SMike Smith     debug(0, "got a slot");
20851ac4b82bSMike Smith 
2086da8bb3a3SMike Smith     if (mu->mu_datasize > 0) {
2087da8bb3a3SMike Smith 
2088da8bb3a3SMike Smith 	/* range check the pointer to physical buffer address */
20891b4404f9SScott Long 	if ((mu->mu_bufptr < 0) || (mu->mu_bufptr > (sizeof(mu->mu_command) -
20901b4404f9SScott Long 						     sizeof(u_int32_t)))) {
2091da8bb3a3SMike Smith 	    error = EINVAL;
2092da8bb3a3SMike Smith 	    goto out;
2093da8bb3a3SMike Smith 	}
2094da8bb3a3SMike Smith     }
2095da8bb3a3SMike Smith 
20961b4404f9SScott Long     /* map the command so the controller can see it */
20971b4404f9SScott Long     mc->mc_data = kbuf;
20981b4404f9SScott Long     mc->mc_length = mu->mu_datasize;
20991b4404f9SScott Long     mc->mc_private = mu;
21001b4404f9SScott Long     error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
21011b4404f9SScott Long 			    mc->mc_length, mlx_user_cb, mc, BUS_DMA_NOWAIT);
21020fca6f8bSJohn Baldwin     if (error)
21030fca6f8bSJohn Baldwin 	goto out;
21041ac4b82bSMike Smith 
21051ac4b82bSMike Smith     /* copy out status and data */
21061ac4b82bSMike Smith     mu->mu_status = mc->mc_status;
21070fca6f8bSJohn Baldwin     if (mu->mu_datasize > 0) {
21080fca6f8bSJohn Baldwin 	MLX_IO_UNLOCK(sc);
21090fca6f8bSJohn Baldwin 	error = copyout(kbuf, mu->mu_buf, mu->mu_datasize);
21100fca6f8bSJohn Baldwin 	MLX_IO_LOCK(sc);
21110fca6f8bSJohn Baldwin     }
21121ac4b82bSMike Smith 
21131ac4b82bSMike Smith  out:
21141ac4b82bSMike Smith     mlx_releasecmd(mc);
21150fca6f8bSJohn Baldwin     MLX_IO_UNLOCK(sc);
21161ac4b82bSMike Smith     if (kbuf != NULL)
21171ac4b82bSMike Smith 	free(kbuf, M_DEVBUF);
21181ac4b82bSMike Smith     return(error);
21191ac4b82bSMike Smith }
21201ac4b82bSMike Smith 
21211ac4b82bSMike Smith /********************************************************************************
21221ac4b82bSMike Smith  ********************************************************************************
21231ac4b82bSMike Smith                                                         Command I/O to Controller
21241ac4b82bSMike Smith  ********************************************************************************
21251ac4b82bSMike Smith  ********************************************************************************/
21261ac4b82bSMike Smith 
21271ac4b82bSMike Smith /********************************************************************************
21281ac4b82bSMike Smith  * Find a free command slot for (mc).
21291ac4b82bSMike Smith  *
21301ac4b82bSMike Smith  * Don't hand out a slot to a normal-priority command unless there are at least
21311ac4b82bSMike Smith  * 4 slots free for priority commands.
21321ac4b82bSMike Smith  */
21331ac4b82bSMike Smith static int
21341ac4b82bSMike Smith mlx_getslot(struct mlx_command *mc)
21351ac4b82bSMike Smith {
21361ac4b82bSMike Smith     struct mlx_softc	*sc = mc->mc_sc;
21370fca6f8bSJohn Baldwin     int			slot, limit;
21381ac4b82bSMike Smith 
2139da8bb3a3SMike Smith     debug_called(1);
21401ac4b82bSMike Smith 
21410fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
21420fca6f8bSJohn Baldwin 
2143baff09dbSMike Smith     /*
2144baff09dbSMike Smith      * Enforce slot-usage limit, if we have the required information.
2145baff09dbSMike Smith      */
2146baff09dbSMike Smith     if (sc->mlx_enq2 != NULL) {
2147baff09dbSMike Smith 	limit = sc->mlx_enq2->me_max_commands;
2148baff09dbSMike Smith     } else {
2149baff09dbSMike Smith 	limit = 2;
2150baff09dbSMike Smith     }
2151baff09dbSMike Smith     if (sc->mlx_busycmds >= ((mc->mc_flags & MLX_CMD_PRIORITY) ? limit : limit - 4))
21521ac4b82bSMike Smith 	return(EBUSY);
21531ac4b82bSMike Smith 
21541ac4b82bSMike Smith     /*
21551ac4b82bSMike Smith      * Allocate an outstanding command slot
21561ac4b82bSMike Smith      *
21571ac4b82bSMike Smith      * XXX linear search is slow
21581ac4b82bSMike Smith      */
2159baff09dbSMike Smith     for (slot = 0; slot < limit; slot++) {
2160da8bb3a3SMike Smith 	debug(2, "try slot %d", slot);
21611ac4b82bSMike Smith 	if (sc->mlx_busycmd[slot] == NULL)
21621ac4b82bSMike Smith 	    break;
21631ac4b82bSMike Smith     }
2164baff09dbSMike Smith     if (slot < limit) {
21651ac4b82bSMike Smith 	sc->mlx_busycmd[slot] = mc;
21661ac4b82bSMike Smith 	sc->mlx_busycmds++;
21671ac4b82bSMike Smith     }
21681ac4b82bSMike Smith 
21691ac4b82bSMike Smith     /* out of slots? */
2170baff09dbSMike Smith     if (slot >= limit)
21711ac4b82bSMike Smith 	return(EBUSY);
21721ac4b82bSMike Smith 
2173da8bb3a3SMike Smith     debug(2, "got slot %d", slot);
21741ac4b82bSMike Smith     mc->mc_slot = slot;
21751ac4b82bSMike Smith     return(0);
21761ac4b82bSMike Smith }
21771ac4b82bSMike Smith 
21781ac4b82bSMike Smith /********************************************************************************
21791ac4b82bSMike Smith  * Map/unmap (mc)'s data in the controller's addressable space.
21801ac4b82bSMike Smith  */
21811ac4b82bSMike Smith static void
21821b4404f9SScott Long mlx_setup_dmamap(struct mlx_command *mc, bus_dma_segment_t *segs, int nsegments,
21831b4404f9SScott Long 		 int error)
21841ac4b82bSMike Smith {
21851ac4b82bSMike Smith     struct mlx_softc	*sc = mc->mc_sc;
21861ac4b82bSMike Smith     struct mlx_sgentry	*sg;
21871ac4b82bSMike Smith     int			i;
21881ac4b82bSMike Smith 
2189da8bb3a3SMike Smith     debug_called(1);
21901ac4b82bSMike Smith 
2191baff09dbSMike Smith     /* XXX should be unnecessary */
2192baff09dbSMike Smith     if (sc->mlx_enq2 && (nsegments > sc->mlx_enq2->me_max_sg))
21931b4404f9SScott Long 	panic("MLX: too many s/g segments (%d, max %d)", nsegments,
21941b4404f9SScott Long 	      sc->mlx_enq2->me_max_sg);
2195baff09dbSMike Smith 
21961ac4b82bSMike Smith     /* get base address of s/g table */
2197baff09dbSMike Smith     sg = sc->mlx_sgtable + (mc->mc_slot * MLX_NSEG);
21981ac4b82bSMike Smith 
21991ac4b82bSMike Smith     /* save s/g table information in command */
22001ac4b82bSMike Smith     mc->mc_nsgent = nsegments;
22011b4404f9SScott Long     mc->mc_sgphys = sc->mlx_sgbusaddr +
22021b4404f9SScott Long 		   (mc->mc_slot * MLX_NSEG * sizeof(struct mlx_sgentry));
22031ac4b82bSMike Smith     mc->mc_dataphys = segs[0].ds_addr;
22041ac4b82bSMike Smith 
22051ac4b82bSMike Smith     /* populate s/g table */
22061ac4b82bSMike Smith     for (i = 0; i < nsegments; i++, sg++) {
22071ac4b82bSMike Smith 	sg->sg_addr = segs[i].ds_addr;
22081ac4b82bSMike Smith 	sg->sg_count = segs[i].ds_len;
22091ac4b82bSMike Smith     }
22101ac4b82bSMike Smith 
22111b4404f9SScott Long     /* Make sure the buffers are visible on the bus. */
22121ac4b82bSMike Smith     if (mc->mc_flags & MLX_CMD_DATAIN)
22131b4404f9SScott Long 	bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap,
22141b4404f9SScott Long 			BUS_DMASYNC_PREREAD);
22151ac4b82bSMike Smith     if (mc->mc_flags & MLX_CMD_DATAOUT)
22161b4404f9SScott Long 	bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap,
22171b4404f9SScott Long 			BUS_DMASYNC_PREWRITE);
22181ac4b82bSMike Smith }
22191ac4b82bSMike Smith 
22201ac4b82bSMike Smith static void
22211ac4b82bSMike Smith mlx_unmapcmd(struct mlx_command *mc)
22221ac4b82bSMike Smith {
22231ac4b82bSMike Smith     struct mlx_softc	*sc = mc->mc_sc;
22241ac4b82bSMike Smith 
2225da8bb3a3SMike Smith     debug_called(1);
22261ac4b82bSMike Smith 
22271ac4b82bSMike Smith     /* if the command involved data at all */
22281ac4b82bSMike Smith     if (mc->mc_data != NULL) {
22291ac4b82bSMike Smith 
22301ac4b82bSMike Smith 	if (mc->mc_flags & MLX_CMD_DATAIN)
22311ac4b82bSMike Smith 	    bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTREAD);
22321ac4b82bSMike Smith 	if (mc->mc_flags & MLX_CMD_DATAOUT)
22331ac4b82bSMike Smith 	    bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTWRITE);
22341ac4b82bSMike Smith 
22351ac4b82bSMike Smith 	bus_dmamap_unload(sc->mlx_buffer_dmat, mc->mc_dmamap);
22361ac4b82bSMike Smith     }
22371ac4b82bSMike Smith }
22381ac4b82bSMike Smith 
22391ac4b82bSMike Smith /********************************************************************************
22405792b7feSMike Smith  * Try to deliver (mc) to the controller.
22411ac4b82bSMike Smith  *
22421ac4b82bSMike Smith  * Can be called at any interrupt level, with or without interrupts enabled.
22431ac4b82bSMike Smith  */
22441ac4b82bSMike Smith static int
22451ac4b82bSMike Smith mlx_start(struct mlx_command *mc)
22461ac4b82bSMike Smith {
22471ac4b82bSMike Smith     struct mlx_softc	*sc = mc->mc_sc;
22480fca6f8bSJohn Baldwin     int			i;
22491ac4b82bSMike Smith 
2250da8bb3a3SMike Smith     debug_called(1);
22511ac4b82bSMike Smith 
22521ac4b82bSMike Smith     /* save the slot number as ident so we can handle this command when complete */
22531ac4b82bSMike Smith     mc->mc_mailbox[0x1] = mc->mc_slot;
22541ac4b82bSMike Smith 
22554b006d7bSMike Smith     /* mark the command as currently being processed */
22561ac4b82bSMike Smith     mc->mc_status = MLX_STATUS_BUSY;
22571ac4b82bSMike Smith 
22585792b7feSMike Smith     /* set a default 60-second timeout  XXX tunable?  XXX not currently used */
22595792b7feSMike Smith     mc->mc_timeout = time_second + 60;
22601ac4b82bSMike Smith 
22611ac4b82bSMike Smith     /* spin waiting for the mailbox */
22620fca6f8bSJohn Baldwin     for (i = 100000; i > 0; i--) {
22634b006d7bSMike Smith 	if (sc->mlx_tryqueue(sc, mc)) {
22644b006d7bSMike Smith 	    /* move command to work queue */
22654b006d7bSMike Smith 	    TAILQ_INSERT_TAIL(&sc->mlx_work, mc, mc_link);
22661ac4b82bSMike Smith 	    return (0);
22670fca6f8bSJohn Baldwin 	} else if (i > 1)
22680fca6f8bSJohn Baldwin 	    mlx_done(sc, 0);
22690fca6f8bSJohn Baldwin     }
22701ac4b82bSMike Smith 
22711ac4b82bSMike Smith     /*
22721ac4b82bSMike Smith      * We couldn't get the controller to take the command.  Revoke the slot
22731ac4b82bSMike Smith      * that the command was given and return it with a bad status.
22741ac4b82bSMike Smith      */
22751ac4b82bSMike Smith     sc->mlx_busycmd[mc->mc_slot] = NULL;
22761ac4b82bSMike Smith     device_printf(sc->mlx_dev, "controller wedged (not taking commands)\n");
22771ac4b82bSMike Smith     mc->mc_status = MLX_STATUS_WEDGED;
22785792b7feSMike Smith     mlx_complete(sc);
22791ac4b82bSMike Smith     return(EIO);
22801ac4b82bSMike Smith }
22811ac4b82bSMike Smith 
22821ac4b82bSMike Smith /********************************************************************************
22835792b7feSMike Smith  * Poll the controller (sc) for completed commands.
22845792b7feSMike Smith  * Update command status and free slots for reuse.  If any slots were freed,
22855792b7feSMike Smith  * new commands may be posted.
22861ac4b82bSMike Smith  *
22875792b7feSMike Smith  * Returns nonzero if one or more commands were completed.
22881ac4b82bSMike Smith  */
22891ac4b82bSMike Smith static int
22900fca6f8bSJohn Baldwin mlx_done(struct mlx_softc *sc, int startio)
22911ac4b82bSMike Smith {
22921ac4b82bSMike Smith     struct mlx_command	*mc;
22930fca6f8bSJohn Baldwin     int			result;
22941ac4b82bSMike Smith     u_int8_t		slot;
22951ac4b82bSMike Smith     u_int16_t		status;
22961ac4b82bSMike Smith 
2297da8bb3a3SMike Smith     debug_called(2);
22980fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
22991ac4b82bSMike Smith 
23005792b7feSMike Smith     result = 0;
23011ac4b82bSMike Smith 
23025792b7feSMike Smith     /* loop collecting completed commands */
23035792b7feSMike Smith     for (;;) {
23045792b7feSMike Smith 	/* poll for a completed command's identifier and status */
23051ac4b82bSMike Smith 	if (sc->mlx_findcomplete(sc, &slot, &status)) {
23065792b7feSMike Smith 	    result = 1;
23071ac4b82bSMike Smith 	    mc = sc->mlx_busycmd[slot];			/* find command */
23081ac4b82bSMike Smith 	    if (mc != NULL) {				/* paranoia */
23091ac4b82bSMike Smith 		if (mc->mc_status == MLX_STATUS_BUSY) {
23101ac4b82bSMike Smith 		    mc->mc_status = status;		/* save status */
23111ac4b82bSMike Smith 
23121ac4b82bSMike Smith 		    /* free slot for reuse */
23131ac4b82bSMike Smith 		    sc->mlx_busycmd[slot] = NULL;
23141ac4b82bSMike Smith 		    sc->mlx_busycmds--;
23151ac4b82bSMike Smith 		} else {
23161ac4b82bSMike Smith 		    device_printf(sc->mlx_dev, "duplicate done event for slot %d\n", slot);
23171ac4b82bSMike Smith 		}
23181ac4b82bSMike Smith 	    } else {
23191ac4b82bSMike Smith 		device_printf(sc->mlx_dev, "done event for nonbusy slot %d\n", slot);
23201ac4b82bSMike Smith 	    }
23215792b7feSMike Smith 	} else {
23225792b7feSMike Smith 	    break;
23231ac4b82bSMike Smith 	}
23245792b7feSMike Smith     }
23251ac4b82bSMike Smith 
23265792b7feSMike Smith     /* if we've completed any commands, try posting some more */
23270fca6f8bSJohn Baldwin     if (result && startio)
23285792b7feSMike Smith 	mlx_startio(sc);
23295792b7feSMike Smith 
23305792b7feSMike Smith     /* handle completion and timeouts */
23315792b7feSMike Smith     mlx_complete(sc);
23325792b7feSMike Smith 
23335792b7feSMike Smith     return(result);
23341ac4b82bSMike Smith }
23351ac4b82bSMike Smith 
23361ac4b82bSMike Smith /********************************************************************************
23375792b7feSMike Smith  * Perform post-completion processing for commands on (sc).
23381ac4b82bSMike Smith  */
23391ac4b82bSMike Smith static void
23401ac4b82bSMike Smith mlx_complete(struct mlx_softc *sc)
23411ac4b82bSMike Smith {
23421ac4b82bSMike Smith     struct mlx_command	*mc, *nc;
23431ac4b82bSMike Smith 
2344da8bb3a3SMike Smith     debug_called(2);
23450fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
23461ac4b82bSMike Smith 
23475792b7feSMike Smith     /* scan the list of busy/done commands */
23484b006d7bSMike Smith     mc = TAILQ_FIRST(&sc->mlx_work);
23491ac4b82bSMike Smith     while (mc != NULL) {
23501ac4b82bSMike Smith 	nc = TAILQ_NEXT(mc, mc_link);
23511ac4b82bSMike Smith 
23525792b7feSMike Smith 	/* Command has been completed in some fashion */
23534b006d7bSMike Smith 	if (mc->mc_status != MLX_STATUS_BUSY) {
23544b006d7bSMike Smith 
23555792b7feSMike Smith 	    /* unmap the command's data buffer */
23565792b7feSMike Smith 	    mlx_unmapcmd(mc);
23571ac4b82bSMike Smith 	    /*
23581ac4b82bSMike Smith 	     * Does the command have a completion handler?
23591ac4b82bSMike Smith 	     */
23601ac4b82bSMike Smith 	    if (mc->mc_complete != NULL) {
23611ac4b82bSMike Smith 		/* remove from list and give to handler */
23624b006d7bSMike Smith 		TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
23631ac4b82bSMike Smith 		mc->mc_complete(mc);
23641ac4b82bSMike Smith 
23651ac4b82bSMike Smith 		/*
23661ac4b82bSMike Smith 		 * Is there a sleeper waiting on this command?
23671ac4b82bSMike Smith 		 */
23681ac4b82bSMike Smith 	    } else if (mc->mc_private != NULL) {	/* sleeping caller wants to know about it */
23691ac4b82bSMike Smith 
23701ac4b82bSMike Smith 		/* remove from list and wake up sleeper */
23714b006d7bSMike Smith 		TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
23721ac4b82bSMike Smith 		wakeup_one(mc->mc_private);
23731ac4b82bSMike Smith 
23741ac4b82bSMike Smith 		/*
23751ac4b82bSMike Smith 		 * Leave the command for a caller that's polling for it.
23761ac4b82bSMike Smith 		 */
23771ac4b82bSMike Smith 	    } else {
23781ac4b82bSMike Smith 	    }
23794b006d7bSMike Smith 	}
23801ac4b82bSMike Smith 	mc = nc;
23811ac4b82bSMike Smith     }
23821ac4b82bSMike Smith }
23831ac4b82bSMike Smith 
23841ac4b82bSMike Smith /********************************************************************************
23851ac4b82bSMike Smith  ********************************************************************************
23861ac4b82bSMike Smith                                                         Command Buffer Management
23871ac4b82bSMike Smith  ********************************************************************************
23881ac4b82bSMike Smith  ********************************************************************************/
23891ac4b82bSMike Smith 
23901ac4b82bSMike Smith /********************************************************************************
23911ac4b82bSMike Smith  * Get a new command buffer.
23921ac4b82bSMike Smith  *
23931ac4b82bSMike Smith  * This may return NULL in low-memory cases.
23941ac4b82bSMike Smith  *
23951ac4b82bSMike Smith  * Note that using malloc() is expensive (the command buffer is << 1 page) but
23961ac4b82bSMike Smith  * necessary if we are to be a loadable module before the zone allocator is fixed.
23971ac4b82bSMike Smith  *
23981ac4b82bSMike Smith  * If possible, we recycle a command buffer that's been used before.
23991ac4b82bSMike Smith  *
24001ac4b82bSMike Smith  * XXX Note that command buffers are not cleaned out - it is the caller's
24011ac4b82bSMike Smith  *     responsibility to ensure that all required fields are filled in before
24021ac4b82bSMike Smith  *     using a buffer.
24031ac4b82bSMike Smith  */
24041ac4b82bSMike Smith static struct mlx_command *
24051ac4b82bSMike Smith mlx_alloccmd(struct mlx_softc *sc)
24061ac4b82bSMike Smith {
24071ac4b82bSMike Smith     struct mlx_command	*mc;
24081ac4b82bSMike Smith     int			error;
24091ac4b82bSMike Smith 
2410da8bb3a3SMike Smith     debug_called(1);
24111ac4b82bSMike Smith 
24120fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
24131ac4b82bSMike Smith     if ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL)
24141ac4b82bSMike Smith 	TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
24151ac4b82bSMike Smith 
24161ac4b82bSMike Smith     /* allocate a new command buffer? */
24171ac4b82bSMike Smith     if (mc == NULL) {
2418ca89ee27SDavid Malone 	mc = (struct mlx_command *)malloc(sizeof(*mc), M_DEVBUF, M_NOWAIT | M_ZERO);
24191ac4b82bSMike Smith 	if (mc != NULL) {
24201ac4b82bSMike Smith 	    mc->mc_sc = sc;
24211ac4b82bSMike Smith 	    error = bus_dmamap_create(sc->mlx_buffer_dmat, 0, &mc->mc_dmamap);
24221ac4b82bSMike Smith 	    if (error) {
24231ac4b82bSMike Smith 		free(mc, M_DEVBUF);
24241ac4b82bSMike Smith 		return(NULL);
24251ac4b82bSMike Smith 	    }
24261ac4b82bSMike Smith 	}
24271ac4b82bSMike Smith     }
24281ac4b82bSMike Smith     return(mc);
24291ac4b82bSMike Smith }
24301ac4b82bSMike Smith 
24311ac4b82bSMike Smith /********************************************************************************
24321ac4b82bSMike Smith  * Release a command buffer for recycling.
24331ac4b82bSMike Smith  *
24341ac4b82bSMike Smith  * XXX It might be a good idea to limit the number of commands we save for reuse
24351ac4b82bSMike Smith  *     if it's shown that this list bloats out massively.
24361ac4b82bSMike Smith  */
24371ac4b82bSMike Smith static void
24381ac4b82bSMike Smith mlx_releasecmd(struct mlx_command *mc)
24391ac4b82bSMike Smith {
24401ac4b82bSMike Smith 
2441da8bb3a3SMike Smith     debug_called(1);
24421ac4b82bSMike Smith 
24430fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(mc->mc_sc);
24441ac4b82bSMike Smith     TAILQ_INSERT_HEAD(&mc->mc_sc->mlx_freecmds, mc, mc_link);
24451ac4b82bSMike Smith }
24461ac4b82bSMike Smith 
24471ac4b82bSMike Smith /********************************************************************************
24481ac4b82bSMike Smith  * Permanently discard a command buffer.
24491ac4b82bSMike Smith  */
24501ac4b82bSMike Smith static void
24511ac4b82bSMike Smith mlx_freecmd(struct mlx_command *mc)
24521ac4b82bSMike Smith {
24531ac4b82bSMike Smith     struct mlx_softc	*sc = mc->mc_sc;
24541ac4b82bSMike Smith 
2455da8bb3a3SMike Smith     debug_called(1);
24561ac4b82bSMike Smith     bus_dmamap_destroy(sc->mlx_buffer_dmat, mc->mc_dmamap);
24571ac4b82bSMike Smith     free(mc, M_DEVBUF);
24581ac4b82bSMike Smith }
24591ac4b82bSMike Smith 
24601ac4b82bSMike Smith 
24611ac4b82bSMike Smith /********************************************************************************
24621ac4b82bSMike Smith  ********************************************************************************
24631ac4b82bSMike Smith                                                 Type 3 interface accessor methods
24641ac4b82bSMike Smith  ********************************************************************************
24651ac4b82bSMike Smith  ********************************************************************************/
24661ac4b82bSMike Smith 
24671ac4b82bSMike Smith /********************************************************************************
24681ac4b82bSMike Smith  * Try to give (mc) to the controller.  Returns 1 if successful, 0 on failure
24691ac4b82bSMike Smith  * (the controller is not ready to take a command).
24701ac4b82bSMike Smith  */
24711ac4b82bSMike Smith static int
24721ac4b82bSMike Smith mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
24731ac4b82bSMike Smith {
24741ac4b82bSMike Smith     int		i;
24751ac4b82bSMike Smith 
2476da8bb3a3SMike Smith     debug_called(2);
24770fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
24781ac4b82bSMike Smith 
24791ac4b82bSMike Smith     /* ready for our command? */
24801ac4b82bSMike Smith     if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_FULL)) {
24811ac4b82bSMike Smith 	/* copy mailbox data to window */
24821ac4b82bSMike Smith 	for (i = 0; i < 13; i++)
24831ac4b82bSMike Smith 	    MLX_V3_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
24841ac4b82bSMike Smith 
24851ac4b82bSMike Smith 	/* post command */
2486f6b84b08SMike Smith 	MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_FULL);
24871ac4b82bSMike Smith 	return(1);
24881ac4b82bSMike Smith     }
24891ac4b82bSMike Smith     return(0);
24901ac4b82bSMike Smith }
24911ac4b82bSMike Smith 
24921ac4b82bSMike Smith /********************************************************************************
24931ac4b82bSMike Smith  * See if a command has been completed, if so acknowledge its completion
24941ac4b82bSMike Smith  * and recover the slot number and status code.
24951ac4b82bSMike Smith  */
24961ac4b82bSMike Smith static int
24971ac4b82bSMike Smith mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
24981ac4b82bSMike Smith {
24991ac4b82bSMike Smith 
2500da8bb3a3SMike Smith     debug_called(2);
25010fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
25021ac4b82bSMike Smith 
25031ac4b82bSMike Smith     /* status available? */
25041ac4b82bSMike Smith     if (MLX_V3_GET_ODBR(sc) & MLX_V3_ODB_SAVAIL) {
25051ac4b82bSMike Smith 	*slot = MLX_V3_GET_STATUS_IDENT(sc);		/* get command identifier */
25061ac4b82bSMike Smith 	*status = MLX_V3_GET_STATUS(sc);		/* get status */
25071ac4b82bSMike Smith 
25081ac4b82bSMike Smith 	/* acknowledge completion */
2509f6b84b08SMike Smith 	MLX_V3_PUT_ODBR(sc, MLX_V3_ODB_SAVAIL);
2510f6b84b08SMike Smith 	MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
25111ac4b82bSMike Smith 	return(1);
25121ac4b82bSMike Smith     }
25131ac4b82bSMike Smith     return(0);
25141ac4b82bSMike Smith }
25151ac4b82bSMike Smith 
25161ac4b82bSMike Smith /********************************************************************************
25171ac4b82bSMike Smith  * Enable/disable interrupts as requested. (No acknowledge required)
25181ac4b82bSMike Smith  */
25191ac4b82bSMike Smith static void
25201ac4b82bSMike Smith mlx_v3_intaction(struct mlx_softc *sc, int action)
25211ac4b82bSMike Smith {
2522da8bb3a3SMike Smith     debug_called(1);
25230fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
25241ac4b82bSMike Smith 
25251ac4b82bSMike Smith     switch(action) {
25261ac4b82bSMike Smith     case MLX_INTACTION_DISABLE:
25271ac4b82bSMike Smith 	MLX_V3_PUT_IER(sc, 0);
25281ac4b82bSMike Smith 	sc->mlx_state &= ~MLX_STATE_INTEN;
25291ac4b82bSMike Smith 	break;
25301ac4b82bSMike Smith     case MLX_INTACTION_ENABLE:
25311ac4b82bSMike Smith 	MLX_V3_PUT_IER(sc, 1);
25321ac4b82bSMike Smith 	sc->mlx_state |= MLX_STATE_INTEN;
25331ac4b82bSMike Smith 	break;
25341ac4b82bSMike Smith     }
25351ac4b82bSMike Smith }
25361ac4b82bSMike Smith 
2537da8bb3a3SMike Smith /********************************************************************************
2538da8bb3a3SMike Smith  * Poll for firmware error codes during controller initialisation.
2539da8bb3a3SMike Smith  * Returns 0 if initialisation is complete, 1 if still in progress but no
2540da8bb3a3SMike Smith  * error has been fetched, 2 if an error has been retrieved.
2541da8bb3a3SMike Smith  */
2542da8bb3a3SMike Smith static int
25430fca6f8bSJohn Baldwin mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
25440fca6f8bSJohn Baldwin     int first)
2545da8bb3a3SMike Smith {
2546da8bb3a3SMike Smith     u_int8_t	fwerror;
2547da8bb3a3SMike Smith 
2548da8bb3a3SMike Smith     debug_called(2);
2549da8bb3a3SMike Smith 
2550da8bb3a3SMike Smith     /* first time around, clear any hardware completion status */
25510fca6f8bSJohn Baldwin     if (first) {
2552da8bb3a3SMike Smith 	MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2553da8bb3a3SMike Smith 	DELAY(1000);
2554da8bb3a3SMike Smith     }
2555da8bb3a3SMike Smith 
2556da8bb3a3SMike Smith     /* init in progress? */
2557da8bb3a3SMike Smith     if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_INIT_BUSY))
2558da8bb3a3SMike Smith 	return(0);
2559da8bb3a3SMike Smith 
2560da8bb3a3SMike Smith     /* test error value */
2561da8bb3a3SMike Smith     fwerror = MLX_V3_GET_FWERROR(sc);
2562da8bb3a3SMike Smith     if (!(fwerror & MLX_V3_FWERROR_PEND))
2563da8bb3a3SMike Smith 	return(1);
2564da8bb3a3SMike Smith 
2565da8bb3a3SMike Smith     /* mask status pending bit, fetch status */
2566da8bb3a3SMike Smith     *error = fwerror & ~MLX_V3_FWERROR_PEND;
2567da8bb3a3SMike Smith     *param1 = MLX_V3_GET_FWERROR_PARAM1(sc);
2568da8bb3a3SMike Smith     *param2 = MLX_V3_GET_FWERROR_PARAM2(sc);
2569da8bb3a3SMike Smith 
2570da8bb3a3SMike Smith     /* acknowledge */
2571da8bb3a3SMike Smith     MLX_V3_PUT_FWERROR(sc, 0);
2572da8bb3a3SMike Smith 
2573da8bb3a3SMike Smith     return(2);
2574da8bb3a3SMike Smith }
25751ac4b82bSMike Smith 
25761ac4b82bSMike Smith /********************************************************************************
25771ac4b82bSMike Smith  ********************************************************************************
2578f6b84b08SMike Smith                                                 Type 4 interface accessor methods
2579f6b84b08SMike Smith  ********************************************************************************
2580f6b84b08SMike Smith  ********************************************************************************/
2581f6b84b08SMike Smith 
2582f6b84b08SMike Smith /********************************************************************************
2583f6b84b08SMike Smith  * Try to give (mc) to the controller.  Returns 1 if successful, 0 on failure
2584f6b84b08SMike Smith  * (the controller is not ready to take a command).
2585f6b84b08SMike Smith  */
2586f6b84b08SMike Smith static int
2587f6b84b08SMike Smith mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2588f6b84b08SMike Smith {
2589f6b84b08SMike Smith     int		i;
2590f6b84b08SMike Smith 
2591da8bb3a3SMike Smith     debug_called(2);
25920fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
2593f6b84b08SMike Smith 
2594f6b84b08SMike Smith     /* ready for our command? */
2595f6b84b08SMike Smith     if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_FULL)) {
2596f6b84b08SMike Smith 	/* copy mailbox data to window */
2597f6b84b08SMike Smith 	for (i = 0; i < 13; i++)
2598f6b84b08SMike Smith 	    MLX_V4_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2599f6b84b08SMike Smith 
2600da8bb3a3SMike Smith 	/* memory-mapped controller, so issue a write barrier to ensure the mailbox is filled */
26010fca6f8bSJohn Baldwin 	bus_barrier(sc->mlx_mem, MLX_V4_MAILBOX, MLX_V4_MAILBOX_LENGTH,
2602da8bb3a3SMike Smith 			  BUS_SPACE_BARRIER_WRITE);
2603da8bb3a3SMike Smith 
2604f6b84b08SMike Smith 	/* post command */
2605f6b84b08SMike Smith 	MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_HWMBOX_CMD);
2606f6b84b08SMike Smith 	return(1);
2607f6b84b08SMike Smith     }
2608f6b84b08SMike Smith     return(0);
2609f6b84b08SMike Smith }
2610f6b84b08SMike Smith 
2611f6b84b08SMike Smith /********************************************************************************
2612f6b84b08SMike Smith  * See if a command has been completed, if so acknowledge its completion
2613f6b84b08SMike Smith  * and recover the slot number and status code.
2614f6b84b08SMike Smith  */
2615f6b84b08SMike Smith static int
2616f6b84b08SMike Smith mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2617f6b84b08SMike Smith {
2618f6b84b08SMike Smith 
2619da8bb3a3SMike Smith     debug_called(2);
26200fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
2621f6b84b08SMike Smith 
2622f6b84b08SMike Smith     /* status available? */
2623f6b84b08SMike Smith     if (MLX_V4_GET_ODBR(sc) & MLX_V4_ODB_HWSAVAIL) {
2624f6b84b08SMike Smith 	*slot = MLX_V4_GET_STATUS_IDENT(sc);		/* get command identifier */
2625f6b84b08SMike Smith 	*status = MLX_V4_GET_STATUS(sc);		/* get status */
2626f6b84b08SMike Smith 
2627f6b84b08SMike Smith 	/* acknowledge completion */
2628f6b84b08SMike Smith 	MLX_V4_PUT_ODBR(sc, MLX_V4_ODB_HWMBOX_ACK);
2629f6b84b08SMike Smith 	MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2630f6b84b08SMike Smith 	return(1);
2631f6b84b08SMike Smith     }
2632f6b84b08SMike Smith     return(0);
2633f6b84b08SMike Smith }
2634f6b84b08SMike Smith 
2635f6b84b08SMike Smith /********************************************************************************
2636f6b84b08SMike Smith  * Enable/disable interrupts as requested.
2637f6b84b08SMike Smith  */
2638f6b84b08SMike Smith static void
2639f6b84b08SMike Smith mlx_v4_intaction(struct mlx_softc *sc, int action)
2640f6b84b08SMike Smith {
2641da8bb3a3SMike Smith     debug_called(1);
26420fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
2643f6b84b08SMike Smith 
2644f6b84b08SMike Smith     switch(action) {
2645f6b84b08SMike Smith     case MLX_INTACTION_DISABLE:
2646f6b84b08SMike Smith 	MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK | MLX_V4_IER_DISINT);
2647f6b84b08SMike Smith 	sc->mlx_state &= ~MLX_STATE_INTEN;
2648f6b84b08SMike Smith 	break;
2649f6b84b08SMike Smith     case MLX_INTACTION_ENABLE:
2650f6b84b08SMike Smith 	MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK & ~MLX_V4_IER_DISINT);
2651f6b84b08SMike Smith 	sc->mlx_state |= MLX_STATE_INTEN;
2652f6b84b08SMike Smith 	break;
2653f6b84b08SMike Smith     }
2654f6b84b08SMike Smith }
2655f6b84b08SMike Smith 
2656da8bb3a3SMike Smith /********************************************************************************
2657da8bb3a3SMike Smith  * Poll for firmware error codes during controller initialisation.
2658da8bb3a3SMike Smith  * Returns 0 if initialisation is complete, 1 if still in progress but no
2659da8bb3a3SMike Smith  * error has been fetched, 2 if an error has been retrieved.
2660da8bb3a3SMike Smith  */
2661da8bb3a3SMike Smith static int
26620fca6f8bSJohn Baldwin mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
26630fca6f8bSJohn Baldwin     int first)
2664da8bb3a3SMike Smith {
2665da8bb3a3SMike Smith     u_int8_t	fwerror;
2666da8bb3a3SMike Smith 
2667da8bb3a3SMike Smith     debug_called(2);
2668da8bb3a3SMike Smith 
2669da8bb3a3SMike Smith     /* first time around, clear any hardware completion status */
26700fca6f8bSJohn Baldwin     if (first) {
2671da8bb3a3SMike Smith 	MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2672da8bb3a3SMike Smith 	DELAY(1000);
2673da8bb3a3SMike Smith     }
2674da8bb3a3SMike Smith 
2675da8bb3a3SMike Smith     /* init in progress? */
2676da8bb3a3SMike Smith     if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_INIT_BUSY))
2677da8bb3a3SMike Smith 	return(0);
2678da8bb3a3SMike Smith 
2679da8bb3a3SMike Smith     /* test error value */
2680da8bb3a3SMike Smith     fwerror = MLX_V4_GET_FWERROR(sc);
2681da8bb3a3SMike Smith     if (!(fwerror & MLX_V4_FWERROR_PEND))
2682da8bb3a3SMike Smith 	return(1);
2683da8bb3a3SMike Smith 
2684da8bb3a3SMike Smith     /* mask status pending bit, fetch status */
2685da8bb3a3SMike Smith     *error = fwerror & ~MLX_V4_FWERROR_PEND;
2686da8bb3a3SMike Smith     *param1 = MLX_V4_GET_FWERROR_PARAM1(sc);
2687da8bb3a3SMike Smith     *param2 = MLX_V4_GET_FWERROR_PARAM2(sc);
2688da8bb3a3SMike Smith 
2689da8bb3a3SMike Smith     /* acknowledge */
2690da8bb3a3SMike Smith     MLX_V4_PUT_FWERROR(sc, 0);
2691da8bb3a3SMike Smith 
2692da8bb3a3SMike Smith     return(2);
2693da8bb3a3SMike Smith }
2694f6b84b08SMike Smith 
2695f6b84b08SMike Smith /********************************************************************************
2696f6b84b08SMike Smith  ********************************************************************************
26975792b7feSMike Smith                                                 Type 5 interface accessor methods
26985792b7feSMike Smith  ********************************************************************************
26995792b7feSMike Smith  ********************************************************************************/
27005792b7feSMike Smith 
27015792b7feSMike Smith /********************************************************************************
27025792b7feSMike Smith  * Try to give (mc) to the controller.  Returns 1 if successful, 0 on failure
27035792b7feSMike Smith  * (the controller is not ready to take a command).
27045792b7feSMike Smith  */
27055792b7feSMike Smith static int
27065792b7feSMike Smith mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
27075792b7feSMike Smith {
27085792b7feSMike Smith     int		i;
27095792b7feSMike Smith 
2710da8bb3a3SMike Smith     debug_called(2);
27110fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
27125792b7feSMike Smith 
27135792b7feSMike Smith     /* ready for our command? */
27145792b7feSMike Smith     if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_EMPTY) {
27155792b7feSMike Smith 	/* copy mailbox data to window */
27165792b7feSMike Smith 	for (i = 0; i < 13; i++)
27175792b7feSMike Smith 	    MLX_V5_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
27185792b7feSMike Smith 
27195792b7feSMike Smith 	/* post command */
27205792b7feSMike Smith 	MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_HWMBOX_CMD);
27215792b7feSMike Smith 	return(1);
27225792b7feSMike Smith     }
27235792b7feSMike Smith     return(0);
27245792b7feSMike Smith }
27255792b7feSMike Smith 
27265792b7feSMike Smith /********************************************************************************
27275792b7feSMike Smith  * See if a command has been completed, if so acknowledge its completion
27285792b7feSMike Smith  * and recover the slot number and status code.
27295792b7feSMike Smith  */
27305792b7feSMike Smith static int
27315792b7feSMike Smith mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
27325792b7feSMike Smith {
27335792b7feSMike Smith 
2734da8bb3a3SMike Smith     debug_called(2);
27350fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
27365792b7feSMike Smith 
27375792b7feSMike Smith     /* status available? */
27385792b7feSMike Smith     if (MLX_V5_GET_ODBR(sc) & MLX_V5_ODB_HWSAVAIL) {
27395792b7feSMike Smith 	*slot = MLX_V5_GET_STATUS_IDENT(sc);		/* get command identifier */
27405792b7feSMike Smith 	*status = MLX_V5_GET_STATUS(sc);		/* get status */
27415792b7feSMike Smith 
27425792b7feSMike Smith 	/* acknowledge completion */
27435792b7feSMike Smith 	MLX_V5_PUT_ODBR(sc, MLX_V5_ODB_HWMBOX_ACK);
27445792b7feSMike Smith 	MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
27455792b7feSMike Smith 	return(1);
27465792b7feSMike Smith     }
27475792b7feSMike Smith     return(0);
27485792b7feSMike Smith }
27495792b7feSMike Smith 
27505792b7feSMike Smith /********************************************************************************
27515792b7feSMike Smith  * Enable/disable interrupts as requested.
27525792b7feSMike Smith  */
27535792b7feSMike Smith static void
27545792b7feSMike Smith mlx_v5_intaction(struct mlx_softc *sc, int action)
27555792b7feSMike Smith {
2756da8bb3a3SMike Smith     debug_called(1);
27570fca6f8bSJohn Baldwin     MLX_IO_ASSERT_LOCKED(sc);
27585792b7feSMike Smith 
27595792b7feSMike Smith     switch(action) {
27605792b7feSMike Smith     case MLX_INTACTION_DISABLE:
2761da8bb3a3SMike Smith 	MLX_V5_PUT_IER(sc, 0xff & MLX_V5_IER_DISINT);
27625792b7feSMike Smith 	sc->mlx_state &= ~MLX_STATE_INTEN;
27635792b7feSMike Smith 	break;
27645792b7feSMike Smith     case MLX_INTACTION_ENABLE:
2765da8bb3a3SMike Smith 	MLX_V5_PUT_IER(sc, 0xff & ~MLX_V5_IER_DISINT);
27665792b7feSMike Smith 	sc->mlx_state |= MLX_STATE_INTEN;
27675792b7feSMike Smith 	break;
27685792b7feSMike Smith     }
27695792b7feSMike Smith }
27705792b7feSMike Smith 
2771da8bb3a3SMike Smith /********************************************************************************
2772da8bb3a3SMike Smith  * Poll for firmware error codes during controller initialisation.
2773da8bb3a3SMike Smith  * Returns 0 if initialisation is complete, 1 if still in progress but no
2774da8bb3a3SMike Smith  * error has been fetched, 2 if an error has been retrieved.
2775da8bb3a3SMike Smith  */
2776da8bb3a3SMike Smith static int
27770fca6f8bSJohn Baldwin mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
27780fca6f8bSJohn Baldwin     int first)
2779da8bb3a3SMike Smith {
2780da8bb3a3SMike Smith     u_int8_t	fwerror;
2781da8bb3a3SMike Smith 
2782da8bb3a3SMike Smith     debug_called(2);
2783da8bb3a3SMike Smith 
2784da8bb3a3SMike Smith     /* first time around, clear any hardware completion status */
27850fca6f8bSJohn Baldwin     if (first) {
2786da8bb3a3SMike Smith 	MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2787da8bb3a3SMike Smith 	DELAY(1000);
2788da8bb3a3SMike Smith     }
2789da8bb3a3SMike Smith 
2790da8bb3a3SMike Smith     /* init in progress? */
2791da8bb3a3SMike Smith     if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_INIT_DONE)
2792da8bb3a3SMike Smith 	return(0);
2793da8bb3a3SMike Smith 
2794da8bb3a3SMike Smith     /* test for error value */
2795da8bb3a3SMike Smith     fwerror = MLX_V5_GET_FWERROR(sc);
2796da8bb3a3SMike Smith     if (!(fwerror & MLX_V5_FWERROR_PEND))
2797da8bb3a3SMike Smith 	return(1);
2798da8bb3a3SMike Smith 
2799da8bb3a3SMike Smith     /* mask status pending bit, fetch status */
2800da8bb3a3SMike Smith     *error = fwerror & ~MLX_V5_FWERROR_PEND;
2801da8bb3a3SMike Smith     *param1 = MLX_V5_GET_FWERROR_PARAM1(sc);
2802da8bb3a3SMike Smith     *param2 = MLX_V5_GET_FWERROR_PARAM2(sc);
2803da8bb3a3SMike Smith 
2804da8bb3a3SMike Smith     /* acknowledge */
2805da8bb3a3SMike Smith     MLX_V5_PUT_FWERROR(sc, 0xff);
2806da8bb3a3SMike Smith 
2807da8bb3a3SMike Smith     return(2);
2808da8bb3a3SMike Smith }
28095792b7feSMike Smith 
28105792b7feSMike Smith /********************************************************************************
28115792b7feSMike Smith  ********************************************************************************
28121ac4b82bSMike Smith                                                                         Debugging
28131ac4b82bSMike Smith  ********************************************************************************
28141ac4b82bSMike Smith  ********************************************************************************/
28151ac4b82bSMike Smith 
28161ac4b82bSMike Smith /********************************************************************************
28171ac4b82bSMike Smith  * Return a status message describing (mc)
28181ac4b82bSMike Smith  */
28191ac4b82bSMike Smith static char *mlx_status_messages[] = {
28201ac4b82bSMike Smith     "normal completion",			/* 00 */
28211ac4b82bSMike Smith     "irrecoverable data error",			/* 01 */
28221ac4b82bSMike Smith     "drive does not exist, or is offline",	/* 02 */
28231ac4b82bSMike Smith     "attempt to write beyond end of drive",	/* 03 */
28241ac4b82bSMike Smith     "bad data encountered",			/* 04 */
28251ac4b82bSMike Smith     "invalid log entry request",		/* 05 */
28261ac4b82bSMike Smith     "attempt to rebuild online drive",		/* 06 */
28271ac4b82bSMike Smith     "new disk failed during rebuild",		/* 07 */
28281ac4b82bSMike Smith     "invalid channel/target",			/* 08 */
28291ac4b82bSMike Smith     "rebuild/check already in progress",	/* 09 */
28301ac4b82bSMike Smith     "one or more disks are dead",		/* 10 */
28311ac4b82bSMike Smith     "invalid or non-redundant drive",		/* 11 */
28321ac4b82bSMike Smith     "channel is busy",				/* 12 */
28331ac4b82bSMike Smith     "channel is not stopped",			/* 13 */
2834da8bb3a3SMike Smith     "rebuild successfully terminated",		/* 14 */
2835da8bb3a3SMike Smith     "unsupported command",			/* 15 */
2836da8bb3a3SMike Smith     "check condition received",			/* 16 */
2837da8bb3a3SMike Smith     "device is busy",				/* 17 */
2838da8bb3a3SMike Smith     "selection or command timeout",		/* 18 */
2839da8bb3a3SMike Smith     "command terminated abnormally",		/* 19 */
2840da8bb3a3SMike Smith     ""
28411ac4b82bSMike Smith };
28421ac4b82bSMike Smith 
28431ac4b82bSMike Smith static struct
28441ac4b82bSMike Smith {
28451ac4b82bSMike Smith     int		command;
28461ac4b82bSMike Smith     u_int16_t	status;
28471ac4b82bSMike Smith     int		msg;
28481ac4b82bSMike Smith } mlx_messages[] = {
2849da8bb3a3SMike Smith     {MLX_CMD_READSG,		0x0001,	 1},
2850da8bb3a3SMike Smith     {MLX_CMD_READSG,		0x0002,	 1},
2851da8bb3a3SMike Smith     {MLX_CMD_READSG,		0x0105,	 3},
2852da8bb3a3SMike Smith     {MLX_CMD_READSG,		0x010c,	 4},
2853da8bb3a3SMike Smith     {MLX_CMD_WRITESG,		0x0001,	 1},
2854da8bb3a3SMike Smith     {MLX_CMD_WRITESG,		0x0002,	 1},
2855da8bb3a3SMike Smith     {MLX_CMD_WRITESG,		0x0105,	 3},
2856da8bb3a3SMike Smith     {MLX_CMD_READSG_OLD,	0x0001,	 1},
2857da8bb3a3SMike Smith     {MLX_CMD_READSG_OLD,	0x0002,	 1},
2858da8bb3a3SMike Smith     {MLX_CMD_READSG_OLD,	0x0105,	 3},
2859da8bb3a3SMike Smith     {MLX_CMD_WRITESG_OLD,	0x0001,	 1},
2860da8bb3a3SMike Smith     {MLX_CMD_WRITESG_OLD,	0x0002,	 1},
2861da8bb3a3SMike Smith     {MLX_CMD_WRITESG_OLD,	0x0105,	 3},
28621ac4b82bSMike Smith     {MLX_CMD_LOGOP,		0x0105,	 5},
28631ac4b82bSMike Smith     {MLX_CMD_REBUILDASYNC,	0x0002,  6},
28641ac4b82bSMike Smith     {MLX_CMD_REBUILDASYNC,	0x0004,  7},
28651ac4b82bSMike Smith     {MLX_CMD_REBUILDASYNC,	0x0105,  8},
28661ac4b82bSMike Smith     {MLX_CMD_REBUILDASYNC,	0x0106,  9},
2867da8bb3a3SMike Smith     {MLX_CMD_REBUILDASYNC,	0x0107, 14},
28681ac4b82bSMike Smith     {MLX_CMD_CHECKASYNC,	0x0002, 10},
28691ac4b82bSMike Smith     {MLX_CMD_CHECKASYNC,	0x0105, 11},
28701ac4b82bSMike Smith     {MLX_CMD_CHECKASYNC,	0x0106,  9},
28711ac4b82bSMike Smith     {MLX_CMD_STOPCHANNEL,	0x0106, 12},
28721ac4b82bSMike Smith     {MLX_CMD_STOPCHANNEL,	0x0105,  8},
28731ac4b82bSMike Smith     {MLX_CMD_STARTCHANNEL,	0x0005, 13},
28741ac4b82bSMike Smith     {MLX_CMD_STARTCHANNEL,	0x0105,  8},
2875da8bb3a3SMike Smith     {MLX_CMD_DIRECT_CDB,	0x0002, 16},
2876da8bb3a3SMike Smith     {MLX_CMD_DIRECT_CDB,	0x0008, 17},
2877da8bb3a3SMike Smith     {MLX_CMD_DIRECT_CDB,	0x000e, 18},
2878da8bb3a3SMike Smith     {MLX_CMD_DIRECT_CDB,	0x000f, 19},
2879da8bb3a3SMike Smith     {MLX_CMD_DIRECT_CDB,	0x0105,  8},
2880da8bb3a3SMike Smith 
2881da8bb3a3SMike Smith     {0,				0x0104, 14},
28821ac4b82bSMike Smith     {-1, 0, 0}
28831ac4b82bSMike Smith };
28841ac4b82bSMike Smith 
28851ac4b82bSMike Smith static char *
28861ac4b82bSMike Smith mlx_diagnose_command(struct mlx_command *mc)
28871ac4b82bSMike Smith {
28881ac4b82bSMike Smith     static char	unkmsg[80];
28891ac4b82bSMike Smith     int		i;
28901ac4b82bSMike Smith 
28911ac4b82bSMike Smith     /* look up message in table */
28921ac4b82bSMike Smith     for (i = 0; mlx_messages[i].command != -1; i++)
2893da8bb3a3SMike Smith 	if (((mc->mc_mailbox[0] == mlx_messages[i].command) || (mlx_messages[i].command == 0)) &&
2894466454bdSMike Smith 	    (mc->mc_status == mlx_messages[i].status))
28951ac4b82bSMike Smith 	    return(mlx_status_messages[mlx_messages[i].msg]);
28961ac4b82bSMike Smith 
28971ac4b82bSMike Smith     sprintf(unkmsg, "unknown response 0x%x for command 0x%x", (int)mc->mc_status, (int)mc->mc_mailbox[0]);
28981ac4b82bSMike Smith     return(unkmsg);
28991ac4b82bSMike Smith }
29001ac4b82bSMike Smith 
29011ac4b82bSMike Smith /*******************************************************************************
2902da8bb3a3SMike Smith  * Print a string describing the controller (sc)
29031ac4b82bSMike Smith  */
29045792b7feSMike Smith static struct
29055792b7feSMike Smith {
29065792b7feSMike Smith     int		hwid;
29075792b7feSMike Smith     char	*name;
29085792b7feSMike Smith } mlx_controller_names[] = {
29095792b7feSMike Smith     {0x01,	"960P/PD"},
29105792b7feSMike Smith     {0x02,	"960PL"},
29115792b7feSMike Smith     {0x10,	"960PG"},
29125792b7feSMike Smith     {0x11,	"960PJ"},
29139eee27f1SMike Smith     {0x12,	"960PR"},
29149eee27f1SMike Smith     {0x13,	"960PT"},
29159eee27f1SMike Smith     {0x14,	"960PTL0"},
29169eee27f1SMike Smith     {0x15,	"960PRL"},
29179eee27f1SMike Smith     {0x16,	"960PTL1"},
29189eee27f1SMike Smith     {0x20,	"1164PVX"},
29195792b7feSMike Smith     {-1, NULL}
29205792b7feSMike Smith };
29215792b7feSMike Smith 
29229eee27f1SMike Smith static void
29239eee27f1SMike Smith mlx_describe_controller(struct mlx_softc *sc)
29241ac4b82bSMike Smith {
29251ac4b82bSMike Smith     static char		buf[80];
29265792b7feSMike Smith     char		*model;
29279eee27f1SMike Smith     int			i;
29281ac4b82bSMike Smith 
29295792b7feSMike Smith     for (i = 0, model = NULL; mlx_controller_names[i].name != NULL; i++) {
29309eee27f1SMike Smith 	if ((sc->mlx_enq2->me_hardware_id & 0xff) == mlx_controller_names[i].hwid) {
29315792b7feSMike Smith 	    model = mlx_controller_names[i].name;
29321ac4b82bSMike Smith 	    break;
29331ac4b82bSMike Smith 	}
29345792b7feSMike Smith     }
29355792b7feSMike Smith     if (model == NULL) {
29369eee27f1SMike Smith 	sprintf(buf, " model 0x%x", sc->mlx_enq2->me_hardware_id & 0xff);
29375792b7feSMike Smith 	model = buf;
29385792b7feSMike Smith     }
2939da8bb3a3SMike Smith     device_printf(sc->mlx_dev, "DAC%s, %d channel%s, firmware %d.%02d-%c-%02d, %dMB RAM\n",
29409eee27f1SMike Smith 		  model,
29419eee27f1SMike Smith 		  sc->mlx_enq2->me_actual_channels,
29429eee27f1SMike Smith 		  sc->mlx_enq2->me_actual_channels > 1 ? "s" : "",
29439eee27f1SMike Smith 		  sc->mlx_enq2->me_firmware_id & 0xff,
29449eee27f1SMike Smith 		  (sc->mlx_enq2->me_firmware_id >> 8) & 0xff,
29459eee27f1SMike Smith 		  (sc->mlx_enq2->me_firmware_id >> 24) & 0xff,
2946b9256fe3SMike Smith 		  (sc->mlx_enq2->me_firmware_id >> 16) & 0xff,
29479eee27f1SMike Smith 		  sc->mlx_enq2->me_mem_size / (1024 * 1024));
29489eee27f1SMike Smith 
29499eee27f1SMike Smith     if (bootverbose) {
29509eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Hardware ID                 0x%08x\n", sc->mlx_enq2->me_hardware_id);
29519eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Firmware ID                 0x%08x\n", sc->mlx_enq2->me_firmware_id);
29529eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Configured/Actual channels  %d/%d\n", sc->mlx_enq2->me_configured_channels,
29539eee27f1SMike Smith 		      sc->mlx_enq2->me_actual_channels);
29549eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Max Targets                 %d\n", sc->mlx_enq2->me_max_targets);
29559eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Max Tags                    %d\n", sc->mlx_enq2->me_max_tags);
29569eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Max System Drives           %d\n", sc->mlx_enq2->me_max_sys_drives);
29579eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Max Arms                    %d\n", sc->mlx_enq2->me_max_arms);
29589eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Max Spans                   %d\n", sc->mlx_enq2->me_max_spans);
29599eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  DRAM/cache/flash/NVRAM size %d/%d/%d/%d\n", sc->mlx_enq2->me_mem_size,
29609eee27f1SMike Smith 		      sc->mlx_enq2->me_cache_size, sc->mlx_enq2->me_flash_size, sc->mlx_enq2->me_nvram_size);
29619eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  DRAM type                   %d\n", sc->mlx_enq2->me_mem_type);
29629eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Clock Speed                 %dns\n", sc->mlx_enq2->me_clock_speed);
29639eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Hardware Speed              %dns\n", sc->mlx_enq2->me_hardware_speed);
29649eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Max Commands                %d\n", sc->mlx_enq2->me_max_commands);
29659eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Max SG Entries              %d\n", sc->mlx_enq2->me_max_sg);
29669eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Max DP                      %d\n", sc->mlx_enq2->me_max_dp);
29679eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Max IOD                     %d\n", sc->mlx_enq2->me_max_iod);
29689eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Max Comb                    %d\n", sc->mlx_enq2->me_max_comb);
29699eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Latency                     %ds\n", sc->mlx_enq2->me_latency);
29709eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  SCSI Timeout                %ds\n", sc->mlx_enq2->me_scsi_timeout);
29719eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Min Free Lines              %d\n", sc->mlx_enq2->me_min_freelines);
29729eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Rate Constant               %d\n", sc->mlx_enq2->me_rate_const);
29739eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  MAXBLK                      %d\n", sc->mlx_enq2->me_maxblk);
29749eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Blocking Factor             %d sectors\n", sc->mlx_enq2->me_blocking_factor);
29759eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Cache Line Size             %d blocks\n", sc->mlx_enq2->me_cacheline);
29769eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  SCSI Capability             %s%dMHz, %d bit\n",
29779eee27f1SMike Smith 		      sc->mlx_enq2->me_scsi_cap & (1<<4) ? "differential " : "",
29789eee27f1SMike Smith 		      (1 << ((sc->mlx_enq2->me_scsi_cap >> 2) & 3)) * 10,
29799eee27f1SMike Smith 		      8 << (sc->mlx_enq2->me_scsi_cap & 0x3));
29809eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Firmware Build Number       %d\n", sc->mlx_enq2->me_firmware_build);
29819eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Fault Management Type       %d\n", sc->mlx_enq2->me_fault_mgmt_type);
29829eee27f1SMike Smith 	device_printf(sc->mlx_dev, "  Features                    %b\n", sc->mlx_enq2->me_firmware_features,
29839eee27f1SMike Smith 		      "\20\4Background Init\3Read Ahead\2MORE\1Cluster\n");
29849eee27f1SMike Smith 
29859eee27f1SMike Smith     }
29861ac4b82bSMike Smith }
29871ac4b82bSMike Smith 
2988da8bb3a3SMike Smith /*******************************************************************************
2989da8bb3a3SMike Smith  * Emit a string describing the firmware handshake status code, and return a flag
2990da8bb3a3SMike Smith  * indicating whether the code represents a fatal error.
2991da8bb3a3SMike Smith  *
2992da8bb3a3SMike Smith  * Error code interpretations are from the Linux driver, and don't directly match
2993da8bb3a3SMike Smith  * the messages printed by Mylex's BIOS.  This may change if documentation on the
2994da8bb3a3SMike Smith  * codes is forthcoming.
2995da8bb3a3SMike Smith  */
2996da8bb3a3SMike Smith static int
2997da8bb3a3SMike Smith mlx_fw_message(struct mlx_softc *sc, int error, int param1, int param2)
2998da8bb3a3SMike Smith {
2999da8bb3a3SMike Smith     switch(error) {
3000da8bb3a3SMike Smith     case 0x00:
3001da8bb3a3SMike Smith 	device_printf(sc->mlx_dev, "physical drive %d:%d not responding\n", param2, param1);
3002da8bb3a3SMike Smith 	break;
3003da8bb3a3SMike Smith     case 0x08:
3004da8bb3a3SMike Smith 	/* we could be neater about this and give some indication when we receive more of them */
3005da8bb3a3SMike Smith 	if (!(sc->mlx_flags & MLX_SPINUP_REPORTED)) {
3006da8bb3a3SMike Smith 	    device_printf(sc->mlx_dev, "spinning up drives...\n");
3007da8bb3a3SMike Smith 	    sc->mlx_flags |= MLX_SPINUP_REPORTED;
3008da8bb3a3SMike Smith 	}
3009da8bb3a3SMike Smith 	break;
3010da8bb3a3SMike Smith     case 0x30:
3011da8bb3a3SMike Smith 	device_printf(sc->mlx_dev, "configuration checksum error\n");
3012da8bb3a3SMike Smith 	break;
3013da8bb3a3SMike Smith     case 0x60:
3014da8bb3a3SMike Smith 	device_printf(sc->mlx_dev, "mirror race recovery failed\n");
3015da8bb3a3SMike Smith 	break;
3016da8bb3a3SMike Smith     case 0x70:
3017da8bb3a3SMike Smith 	device_printf(sc->mlx_dev, "mirror race recovery in progress\n");
3018da8bb3a3SMike Smith 	break;
3019da8bb3a3SMike Smith     case 0x90:
3020da8bb3a3SMike Smith 	device_printf(sc->mlx_dev, "physical drive %d:%d COD mismatch\n", param2, param1);
3021da8bb3a3SMike Smith 	break;
3022da8bb3a3SMike Smith     case 0xa0:
3023da8bb3a3SMike Smith 	device_printf(sc->mlx_dev, "logical drive installation aborted\n");
3024da8bb3a3SMike Smith 	break;
3025da8bb3a3SMike Smith     case 0xb0:
3026da8bb3a3SMike Smith 	device_printf(sc->mlx_dev, "mirror race on a critical system drive\n");
3027da8bb3a3SMike Smith 	break;
3028da8bb3a3SMike Smith     case 0xd0:
3029da8bb3a3SMike Smith 	device_printf(sc->mlx_dev, "new controller configuration found\n");
3030da8bb3a3SMike Smith 	break;
3031da8bb3a3SMike Smith     case 0xf0:
3032da8bb3a3SMike Smith 	device_printf(sc->mlx_dev, "FATAL MEMORY PARITY ERROR\n");
3033da8bb3a3SMike Smith 	return(1);
3034da8bb3a3SMike Smith     default:
3035da8bb3a3SMike Smith 	device_printf(sc->mlx_dev, "unknown firmware initialisation error %02x:%02x:%02x\n", error, param1, param2);
3036da8bb3a3SMike Smith 	break;
3037da8bb3a3SMike Smith     }
3038da8bb3a3SMike Smith     return(0);
3039da8bb3a3SMike Smith }
3040da8bb3a3SMike Smith 
30411ac4b82bSMike Smith /********************************************************************************
30421ac4b82bSMike Smith  ********************************************************************************
30431ac4b82bSMike Smith                                                                 Utility Functions
30441ac4b82bSMike Smith  ********************************************************************************
30451ac4b82bSMike Smith  ********************************************************************************/
30461ac4b82bSMike Smith 
30471ac4b82bSMike Smith /********************************************************************************
30481ac4b82bSMike Smith  * Find the disk whose unit number is (unit) on this controller
30491ac4b82bSMike Smith  */
30501ac4b82bSMike Smith static struct mlx_sysdrive *
30511ac4b82bSMike Smith mlx_findunit(struct mlx_softc *sc, int unit)
30521ac4b82bSMike Smith {
30531ac4b82bSMike Smith     int		i;
30541ac4b82bSMike Smith 
30551ac4b82bSMike Smith     /* search system drives */
30560fca6f8bSJohn Baldwin     MLX_CONFIG_ASSERT_LOCKED(sc);
30571ac4b82bSMike Smith     for (i = 0; i < MLX_MAXDRIVES; i++) {
30581ac4b82bSMike Smith 	/* is this one attached? */
30591ac4b82bSMike Smith 	if (sc->mlx_sysdrive[i].ms_disk != 0) {
30601ac4b82bSMike Smith 	    /* is this the one? */
30611ac4b82bSMike Smith 	    if (unit == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
30621ac4b82bSMike Smith 		return(&sc->mlx_sysdrive[i]);
30631ac4b82bSMike Smith 	}
30641ac4b82bSMike Smith     }
30651ac4b82bSMike Smith     return(NULL);
30661ac4b82bSMike Smith }
3067