xref: /freebsd/sys/dev/mlx/mlxvar.h (revision b40ce4165d5eb3a5de1515245055350ae3dbab8e)
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 /*
30da8bb3a3SMike Smith  * Debugging levels:
31da8bb3a3SMike Smith  *  0 - quiet, only emit warnings
32da8bb3a3SMike Smith  *  1 - noisy, emit major function points and things done
33da8bb3a3SMike Smith  *  2 - extremely noisy, emit trace items in loops, etc.
34da8bb3a3SMike Smith  */
35da8bb3a3SMike Smith #ifdef MLX_DEBUG
36da8bb3a3SMike Smith #define debug(level, fmt, args...)	do { if (level <= MLX_DEBUG) printf("%s: " fmt "\n", __FUNCTION__ , ##args); } while(0)
37da8bb3a3SMike Smith #define debug_called(level)		do { if (level <= MLX_DEBUG) printf(__FUNCTION__ ": called\n"); } while(0)
38da8bb3a3SMike Smith #else
39da8bb3a3SMike Smith #define debug(level, fmt, args...)
40da8bb3a3SMike Smith #define debug_called(level)
41da8bb3a3SMike Smith #endif
42da8bb3a3SMike Smith 
43da8bb3a3SMike Smith /*
44baff09dbSMike Smith  * Regardless of the actual capacity of the controller, we will allocate space
45baff09dbSMike Smith  * for 64 s/g entries.  Typically controllers support 17 or 33 entries (64k or
46baff09dbSMike Smith  * 128k maximum transfer assuming 4k page size and non-optimal alignment), but
47baff09dbSMike Smith  * making that fit cleanly without crossing page boundaries requires rounding up
48baff09dbSMike Smith  * to the next power of two.
491ac4b82bSMike Smith  */
50baff09dbSMike Smith #define MLX_NSEG	64
51baff09dbSMike Smith 
521ac4b82bSMike Smith #define MLX_NSLOTS	256		/* max number of command slots */
531ac4b82bSMike Smith 
54da8bb3a3SMike Smith #define MLX_MAXDRIVES	32		/* max number of system drives */
551ac4b82bSMike Smith 
561ac4b82bSMike Smith /*
571ac4b82bSMike Smith  * Structure describing a System Drive as attached to the controller.
581ac4b82bSMike Smith  */
591ac4b82bSMike Smith struct mlx_sysdrive
601ac4b82bSMike Smith {
611ac4b82bSMike Smith     /* from MLX_CMD_ENQSYSDRIVE */
621ac4b82bSMike Smith     u_int32_t		ms_size;
631ac4b82bSMike Smith     int			ms_state;
641ac4b82bSMike Smith     int			ms_raidlevel;
651ac4b82bSMike Smith 
661ac4b82bSMike Smith     /* synthetic geometry */
671ac4b82bSMike Smith     int			ms_cylinders;
681ac4b82bSMike Smith     int			ms_heads;
691ac4b82bSMike Smith     int			ms_sectors;
701ac4b82bSMike Smith 
711ac4b82bSMike Smith     /* handle for attached driver */
721ac4b82bSMike Smith     device_t		ms_disk;
731ac4b82bSMike Smith };
741ac4b82bSMike Smith 
751ac4b82bSMike Smith /*
761ac4b82bSMike Smith  * Per-command control structure.
771ac4b82bSMike Smith  */
781ac4b82bSMike Smith struct mlx_command
791ac4b82bSMike Smith {
80e3975643SJake Burkholder     TAILQ_ENTRY(mlx_command)	mc_link;	/* list linkage */
811ac4b82bSMike Smith 
821ac4b82bSMike Smith     struct mlx_softc		*mc_sc;		/* controller that owns us */
831ac4b82bSMike Smith     u_int8_t			mc_slot;	/* command slot we occupy */
841ac4b82bSMike Smith     u_int16_t			mc_status;	/* command completion status */
855792b7feSMike Smith     time_t			mc_timeout;	/* when this command expires */
861ac4b82bSMike Smith     u_int8_t			mc_mailbox[16];	/* command mailbox */
871ac4b82bSMike Smith     u_int32_t			mc_sgphys;	/* physical address of s/g array in controller space */
881ac4b82bSMike Smith     int				mc_nsgent;	/* number of entries in s/g map */
891ac4b82bSMike Smith     int				mc_flags;
901ac4b82bSMike Smith #define MLX_CMD_DATAIN		(1<<0)
911ac4b82bSMike Smith #define MLX_CMD_DATAOUT		(1<<1)
921ac4b82bSMike Smith #define MLX_CMD_PRIORITY	(1<<2)		/* high-priority command */
931ac4b82bSMike Smith 
941ac4b82bSMike Smith     void			*mc_data;	/* data buffer */
951ac4b82bSMike Smith     size_t			mc_length;
961ac4b82bSMike Smith     bus_dmamap_t		mc_dmamap;	/* DMA map for data */
971ac4b82bSMike Smith     u_int32_t			mc_dataphys;	/* data buffer base address controller space */
981ac4b82bSMike Smith 
991ac4b82bSMike Smith     void			(* mc_complete)(struct mlx_command *mc);	/* completion handler */
1001ac4b82bSMike Smith     void			*mc_private;	/* submitter-private data or wait channel */
1011ac4b82bSMike Smith };
1021ac4b82bSMike Smith 
1031ac4b82bSMike Smith /*
1041ac4b82bSMike Smith  * Per-controller structure.
1051ac4b82bSMike Smith  */
1061ac4b82bSMike Smith struct mlx_softc
1071ac4b82bSMike Smith {
1081ac4b82bSMike Smith     /* bus connections */
1091ac4b82bSMike Smith     device_t		mlx_dev;
110da8bb3a3SMike Smith     dev_t		mlx_dev_t;
1111ac4b82bSMike Smith     struct resource	*mlx_mem;	/* mailbox interface window */
1129b11c7baSMatthew N. Dodd     int			mlx_mem_rid;
1139b11c7baSMatthew N. Dodd     int			mlx_mem_type;
1141ac4b82bSMike Smith     bus_space_handle_t	mlx_bhandle;	/* bus space handle */
1151ac4b82bSMike Smith     bus_space_tag_t	mlx_btag;	/* bus space tag */
1161ac4b82bSMike Smith     bus_dma_tag_t	mlx_parent_dmat;/* parent DMA tag */
1171ac4b82bSMike Smith     bus_dma_tag_t	mlx_buffer_dmat;/* data buffer DMA tag */
1181ac4b82bSMike Smith     struct resource	*mlx_irq;	/* interrupt */
1191ac4b82bSMike Smith     void		*mlx_intr;	/* interrupt handle */
1201ac4b82bSMike Smith 
1211ac4b82bSMike Smith     /* scatter/gather lists and their controller-visible mappings */
1221ac4b82bSMike Smith     struct mlx_sgentry	*mlx_sgtable;	/* s/g lists */
1231ac4b82bSMike Smith     u_int32_t		mlx_sgbusaddr;	/* s/g table base address in bus space */
1241ac4b82bSMike Smith     bus_dma_tag_t	mlx_sg_dmat;	/* s/g buffer DMA tag */
1251ac4b82bSMike Smith     bus_dmamap_t	mlx_sg_dmamap;	/* map for s/g buffers */
1261ac4b82bSMike Smith 
1271ac4b82bSMike Smith     /* controller limits and features */
1289eee27f1SMike Smith     struct mlx_enquiry2	*mlx_enq2;
1291ac4b82bSMike Smith     int			mlx_feature;	/* controller features/quirks */
1301ac4b82bSMike Smith #define MLX_FEAT_PAUSEWORKS	(1<<0)	/* channel pause works as expected */
1311ac4b82bSMike Smith 
1321ac4b82bSMike Smith     /* controller queues and arrays */
133e3975643SJake Burkholder     TAILQ_HEAD(, mlx_command)	mlx_freecmds;		/* command structures available for reuse */
134e3975643SJake Burkholder     TAILQ_HEAD(, mlx_command)	mlx_work;		/* active commands */
1351ac4b82bSMike Smith     struct mlx_command	*mlx_busycmd[MLX_NSLOTS];	/* busy commands */
1361ac4b82bSMike Smith     int			mlx_busycmds;			/* count of busy commands */
1371ac4b82bSMike Smith     struct mlx_sysdrive	mlx_sysdrive[MLX_MAXDRIVES];	/* system drives */
13815fd5d22SMike Smith     mlx_bioq		mlx_bioq;			/* outstanding I/O operations */
1391ac4b82bSMike Smith     int			mlx_waitbufs;			/* number of bufs awaiting commands */
1401ac4b82bSMike Smith 
1411ac4b82bSMike Smith     /* controller status */
1421ac4b82bSMike Smith     int			mlx_geom;
1431ac4b82bSMike Smith #define MLX_GEOM_128_32		0	/* geoemetry translation modes */
1441ac4b82bSMike Smith #define MLX_GEOM_256_63		1
1451ac4b82bSMike Smith     int			mlx_state;
1461ac4b82bSMike Smith #define MLX_STATE_INTEN		(1<<0)	/* interrupts have been enabled */
1471ac4b82bSMike Smith #define MLX_STATE_SHUTDOWN	(1<<1)	/* controller is shut down */
1481ac4b82bSMike Smith #define MLX_STATE_OPEN		(1<<2)	/* control device is open */
1491ac4b82bSMike Smith #define MLX_STATE_SUSPEND	(1<<3)	/* controller is suspended */
1501ac4b82bSMike Smith     struct callout_handle mlx_timeout;	/* periodic status monitor */
1511ac4b82bSMike Smith     time_t		mlx_lastpoll;	/* last time_second we polled for status */
152421f2f7dSMike Smith     u_int16_t		mlx_lastevent;	/* sequence number of the last event we recorded */
1539eee27f1SMike Smith     int			mlx_currevent;	/* sequence number last time we looked */
154421f2f7dSMike Smith     int			mlx_background;	/* if != 0 rebuild or check is in progress */
155421f2f7dSMike Smith #define MLX_BACKGROUND_CHECK		1	/* we started a check */
156421f2f7dSMike Smith #define MLX_BACKGROUND_REBUILD		2	/* we started a rebuild */
157421f2f7dSMike Smith #define MLX_BACKGROUND_SPONTANEOUS	3	/* it just happened somehow */
158421f2f7dSMike Smith     struct mlx_rebuild_status mlx_rebuildstat;	/* last rebuild status */
1591ac4b82bSMike Smith     struct mlx_pause	mlx_pause;	/* pending pause operation details */
1601ac4b82bSMike Smith 
1615792b7feSMike Smith     int			mlx_locks;	/* reentrancy avoidance */
162da8bb3a3SMike Smith     int			mlx_flags;
163da8bb3a3SMike Smith #define MLX_SPINUP_REPORTED	(1<<0)	/* "spinning up drives" message displayed */
1645d278f5cSMike Smith #define MLX_EVENTLOG_BUSY	(1<<1)	/* currently reading event log */
1655792b7feSMike Smith 
1661ac4b82bSMike Smith     /* interface-specific accessor functions */
1671ac4b82bSMike Smith     int			mlx_iftype;	/* interface protocol */
168da8bb3a3SMike Smith #define MLX_IFTYPE_2	2
1691ac4b82bSMike Smith #define MLX_IFTYPE_3	3
1701ac4b82bSMike Smith #define MLX_IFTYPE_4	4
1711ac4b82bSMike Smith #define MLX_IFTYPE_5	5
1721ac4b82bSMike Smith     int			(* mlx_tryqueue)(struct mlx_softc *sc, struct mlx_command *mc);
1731ac4b82bSMike Smith     int			(* mlx_findcomplete)(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
1741ac4b82bSMike Smith     void		(* mlx_intaction)(struct mlx_softc *sc, int action);
175da8bb3a3SMike Smith     int			(* mlx_fw_handshake)(struct mlx_softc *sc, int *error, int *param1, int *param2);
1761ac4b82bSMike Smith #define MLX_INTACTION_DISABLE		0
1771ac4b82bSMike Smith #define MLX_INTACTION_ENABLE		1
1781ac4b82bSMike Smith };
1791ac4b82bSMike Smith 
1801ac4b82bSMike Smith /*
1815792b7feSMike Smith  * Simple (stupid) locks.
1825792b7feSMike Smith  *
1835792b7feSMike Smith  * Note that these are designed to avoid reentrancy, not concurrency, and will
1845792b7feSMike Smith  * need to be replaced with something better.
1855792b7feSMike Smith  */
1865792b7feSMike Smith #define MLX_LOCK_COMPLETING	(1<<0)
1875792b7feSMike Smith #define MLX_LOCK_STARTING	(1<<1)
1885792b7feSMike Smith 
1895792b7feSMike Smith static __inline int
1905792b7feSMike Smith mlx_lock_tas(struct mlx_softc *sc, int lock)
1915792b7feSMike Smith {
1925792b7feSMike Smith     if ((sc)->mlx_locks & (lock))
1935792b7feSMike Smith 	return(1);
1945792b7feSMike Smith     atomic_set_int(&sc->mlx_locks, lock);
1955792b7feSMike Smith     return(0);
1965792b7feSMike Smith }
1975792b7feSMike Smith 
1985792b7feSMike Smith static __inline void
1995792b7feSMike Smith mlx_lock_clr(struct mlx_softc *sc, int lock)
2005792b7feSMike Smith {
2015792b7feSMike Smith     atomic_clear_int(&sc->mlx_locks, lock);
2025792b7feSMike Smith }
2035792b7feSMike Smith 
2045792b7feSMike Smith /*
2051ac4b82bSMike Smith  * Interface between bus connections and driver core.
2061ac4b82bSMike Smith  */
2071ac4b82bSMike Smith extern void		mlx_free(struct mlx_softc *sc);
2081ac4b82bSMike Smith extern int		mlx_attach(struct mlx_softc *sc);
2091ac4b82bSMike Smith extern void		mlx_startup(struct mlx_softc *sc);
2101ac4b82bSMike Smith extern void		mlx_intr(void *data);
2111ac4b82bSMike Smith extern int		mlx_detach(device_t dev);
2121ac4b82bSMike Smith extern int		mlx_shutdown(device_t dev);
2131ac4b82bSMike Smith extern int		mlx_suspend(device_t dev);
2141ac4b82bSMike Smith extern int		mlx_resume(device_t dev);
2151ac4b82bSMike Smith extern d_open_t		mlx_open;
2161ac4b82bSMike Smith extern d_close_t	mlx_close;
2171ac4b82bSMike Smith extern d_ioctl_t	mlx_ioctl;
2181ac4b82bSMike Smith 
2191ac4b82bSMike Smith extern devclass_t	mlx_devclass;
220421f2f7dSMike Smith extern devclass_t	mlxd_devclass;
2211ac4b82bSMike Smith 
2221ac4b82bSMike Smith /*
2231ac4b82bSMike Smith  * Mylex System Disk driver
2241ac4b82bSMike Smith  */
2251ac4b82bSMike Smith struct mlxd_softc
2261ac4b82bSMike Smith {
2271ac4b82bSMike Smith     device_t		mlxd_dev;
228421f2f7dSMike Smith     dev_t		mlxd_dev_t;
2291ac4b82bSMike Smith     struct mlx_softc	*mlxd_controller;
2301ac4b82bSMike Smith     struct mlx_sysdrive	*mlxd_drive;
2311ac4b82bSMike Smith     struct disk		mlxd_disk;
2321ac4b82bSMike Smith     struct devstat	mlxd_stats;
2331ac4b82bSMike Smith     struct disklabel	mlxd_label;
2341ac4b82bSMike Smith     int			mlxd_unit;
2351ac4b82bSMike Smith     int			mlxd_flags;
2361ac4b82bSMike Smith #define MLXD_OPEN	(1<<0)		/* drive is open (can't shut down) */
2371ac4b82bSMike Smith };
2381ac4b82bSMike Smith 
2391ac4b82bSMike Smith /*
2401ac4b82bSMike Smith  * Interface between driver core and disk driver (should be using a bus?)
2411ac4b82bSMike Smith  */
24215fd5d22SMike Smith extern int	mlx_submit_buf(struct mlx_softc *sc, mlx_bio *bp);
243b40ce416SJulian Elischer extern int	mlx_submit_ioctl(struct mlx_softc *sc,
244b40ce416SJulian Elischer 			struct mlx_sysdrive *drive, u_long cmd,
245b40ce416SJulian Elischer 			caddr_t addr, int32_t flag, struct thread *td);
2461ac4b82bSMike Smith extern void	mlxd_intr(void *data);
2471ac4b82bSMike Smith 
2481ac4b82bSMike Smith 
249