xref: /freebsd/sys/dev/mlx/mlxvar.h (revision 0f3fae6159ce49ac3f56630bffda78b1bab090b3)
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
366e551fb6SDavid E. O'Brien #define debug(level, fmt, args...)	do { if (level <= MLX_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); } while(0)
376e551fb6SDavid E. O'Brien #define debug_called(level)		do { if (level <= MLX_DEBUG) printf(__func__ ": 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  */
50*0f3fae61SXin LI #define MLX_MAXPHYS	(128 * 1024)
51baff09dbSMike Smith #define MLX_NSEG	64
52baff09dbSMike Smith 
531ac4b82bSMike Smith #define MLX_NSLOTS	256		/* max number of command slots */
541ac4b82bSMike Smith 
55da8bb3a3SMike Smith #define MLX_MAXDRIVES	32		/* max number of system drives */
561ac4b82bSMike Smith 
571ac4b82bSMike Smith /*
581ac4b82bSMike Smith  * Structure describing a System Drive as attached to the controller.
591ac4b82bSMike Smith  */
601ac4b82bSMike Smith struct mlx_sysdrive
611ac4b82bSMike Smith {
621ac4b82bSMike Smith     /* from MLX_CMD_ENQSYSDRIVE */
631ac4b82bSMike Smith     u_int32_t		ms_size;
641ac4b82bSMike Smith     int			ms_state;
651ac4b82bSMike Smith     int			ms_raidlevel;
661ac4b82bSMike Smith 
671ac4b82bSMike Smith     /* synthetic geometry */
681ac4b82bSMike Smith     int			ms_cylinders;
691ac4b82bSMike Smith     int			ms_heads;
701ac4b82bSMike Smith     int			ms_sectors;
711ac4b82bSMike Smith 
721ac4b82bSMike Smith     /* handle for attached driver */
731ac4b82bSMike Smith     device_t		ms_disk;
741ac4b82bSMike Smith };
751ac4b82bSMike Smith 
761ac4b82bSMike Smith /*
771ac4b82bSMike Smith  * Per-command control structure.
781ac4b82bSMike Smith  */
791ac4b82bSMike Smith struct mlx_command
801ac4b82bSMike Smith {
81e3975643SJake Burkholder     TAILQ_ENTRY(mlx_command)	mc_link;	/* list linkage */
821ac4b82bSMike Smith 
831ac4b82bSMike Smith     struct mlx_softc		*mc_sc;		/* controller that owns us */
841ac4b82bSMike Smith     u_int8_t			mc_slot;	/* command slot we occupy */
851ac4b82bSMike Smith     u_int16_t			mc_status;	/* command completion status */
865792b7feSMike Smith     time_t			mc_timeout;	/* when this command expires */
871ac4b82bSMike Smith     u_int8_t			mc_mailbox[16];	/* command mailbox */
881ac4b82bSMike Smith     u_int32_t			mc_sgphys;	/* physical address of s/g array in controller space */
891ac4b82bSMike Smith     int				mc_nsgent;	/* number of entries in s/g map */
901ac4b82bSMike Smith     int				mc_flags;
911ac4b82bSMike Smith #define MLX_CMD_DATAIN		(1<<0)
921ac4b82bSMike Smith #define MLX_CMD_DATAOUT		(1<<1)
931ac4b82bSMike Smith #define MLX_CMD_PRIORITY	(1<<2)		/* high-priority command */
941ac4b82bSMike Smith 
951ac4b82bSMike Smith     void			*mc_data;	/* data buffer */
961ac4b82bSMike Smith     size_t			mc_length;
971ac4b82bSMike Smith     bus_dmamap_t		mc_dmamap;	/* DMA map for data */
981ac4b82bSMike Smith     u_int32_t			mc_dataphys;	/* data buffer base address controller space */
991ac4b82bSMike Smith 
1001ac4b82bSMike Smith     void			(* mc_complete)(struct mlx_command *mc);	/* completion handler */
1011ac4b82bSMike Smith     void			*mc_private;	/* submitter-private data or wait channel */
1021b4404f9SScott Long     int				mc_command;
1031ac4b82bSMike Smith };
1041ac4b82bSMike Smith 
1051ac4b82bSMike Smith /*
1061ac4b82bSMike Smith  * Per-controller structure.
1071ac4b82bSMike Smith  */
1081ac4b82bSMike Smith struct mlx_softc
1091ac4b82bSMike Smith {
1101ac4b82bSMike Smith     /* bus connections */
1111ac4b82bSMike Smith     device_t		mlx_dev;
11289c9c53dSPoul-Henning Kamp     struct cdev *mlx_dev_t;
1131ac4b82bSMike Smith     struct resource	*mlx_mem;	/* mailbox interface window */
1149b11c7baSMatthew N. Dodd     int			mlx_mem_rid;
1159b11c7baSMatthew N. Dodd     int			mlx_mem_type;
1161ac4b82bSMike Smith     bus_space_handle_t	mlx_bhandle;	/* bus space handle */
1171ac4b82bSMike Smith     bus_space_tag_t	mlx_btag;	/* bus space tag */
1181ac4b82bSMike Smith     bus_dma_tag_t	mlx_parent_dmat;/* parent DMA tag */
1191ac4b82bSMike Smith     bus_dma_tag_t	mlx_buffer_dmat;/* data buffer DMA tag */
1201ac4b82bSMike Smith     struct resource	*mlx_irq;	/* interrupt */
1211ac4b82bSMike Smith     void		*mlx_intr;	/* interrupt handle */
1221ac4b82bSMike Smith 
1231ac4b82bSMike Smith     /* scatter/gather lists and their controller-visible mappings */
1241ac4b82bSMike Smith     struct mlx_sgentry	*mlx_sgtable;	/* s/g lists */
1251ac4b82bSMike Smith     u_int32_t		mlx_sgbusaddr;	/* s/g table base address in bus space */
1261ac4b82bSMike Smith     bus_dma_tag_t	mlx_sg_dmat;	/* s/g buffer DMA tag */
1271ac4b82bSMike Smith     bus_dmamap_t	mlx_sg_dmamap;	/* map for s/g buffers */
1281ac4b82bSMike Smith 
1291ac4b82bSMike Smith     /* controller limits and features */
1309eee27f1SMike Smith     struct mlx_enquiry2	*mlx_enq2;
1311ac4b82bSMike Smith     int			mlx_feature;	/* controller features/quirks */
1321ac4b82bSMike Smith #define MLX_FEAT_PAUSEWORKS	(1<<0)	/* channel pause works as expected */
1331ac4b82bSMike Smith 
1341ac4b82bSMike Smith     /* controller queues and arrays */
135e3975643SJake Burkholder     TAILQ_HEAD(, mlx_command)	mlx_freecmds;		/* command structures available for reuse */
136e3975643SJake Burkholder     TAILQ_HEAD(, mlx_command)	mlx_work;		/* active commands */
1371ac4b82bSMike Smith     struct mlx_command	*mlx_busycmd[MLX_NSLOTS];	/* busy commands */
1381ac4b82bSMike Smith     int			mlx_busycmds;			/* count of busy commands */
1391ac4b82bSMike Smith     struct mlx_sysdrive	mlx_sysdrive[MLX_MAXDRIVES];	/* system drives */
14015fd5d22SMike Smith     mlx_bioq		mlx_bioq;			/* outstanding I/O operations */
1411ac4b82bSMike Smith     int			mlx_waitbufs;			/* number of bufs awaiting commands */
1421ac4b82bSMike Smith 
1431ac4b82bSMike Smith     /* controller status */
1441ac4b82bSMike Smith     int			mlx_geom;
1451ac4b82bSMike Smith #define MLX_GEOM_128_32		0	/* geoemetry translation modes */
1461ac4b82bSMike Smith #define MLX_GEOM_256_63		1
1471ac4b82bSMike Smith     int			mlx_state;
1481ac4b82bSMike Smith #define MLX_STATE_INTEN		(1<<0)	/* interrupts have been enabled */
1491ac4b82bSMike Smith #define MLX_STATE_SHUTDOWN	(1<<1)	/* controller is shut down */
1501ac4b82bSMike Smith #define MLX_STATE_OPEN		(1<<2)	/* control device is open */
1511ac4b82bSMike Smith #define MLX_STATE_SUSPEND	(1<<3)	/* controller is suspended */
1521ac4b82bSMike Smith     struct callout_handle mlx_timeout;	/* periodic status monitor */
1531ac4b82bSMike Smith     time_t		mlx_lastpoll;	/* last time_second we polled for status */
154421f2f7dSMike Smith     u_int16_t		mlx_lastevent;	/* sequence number of the last event we recorded */
1559eee27f1SMike Smith     int			mlx_currevent;	/* sequence number last time we looked */
156421f2f7dSMike Smith     int			mlx_background;	/* if != 0 rebuild or check is in progress */
157421f2f7dSMike Smith #define MLX_BACKGROUND_CHECK		1	/* we started a check */
158421f2f7dSMike Smith #define MLX_BACKGROUND_REBUILD		2	/* we started a rebuild */
159421f2f7dSMike Smith #define MLX_BACKGROUND_SPONTANEOUS	3	/* it just happened somehow */
160421f2f7dSMike Smith     struct mlx_rebuild_status mlx_rebuildstat;	/* last rebuild status */
1611ac4b82bSMike Smith     struct mlx_pause	mlx_pause;	/* pending pause operation details */
1621ac4b82bSMike Smith 
1635792b7feSMike Smith     int			mlx_locks;	/* reentrancy avoidance */
164da8bb3a3SMike Smith     int			mlx_flags;
165da8bb3a3SMike Smith #define MLX_SPINUP_REPORTED	(1<<0)	/* "spinning up drives" message displayed */
1665d278f5cSMike Smith #define MLX_EVENTLOG_BUSY	(1<<1)	/* currently reading event log */
1675792b7feSMike Smith 
1681ac4b82bSMike Smith     /* interface-specific accessor functions */
1691ac4b82bSMike Smith     int			mlx_iftype;	/* interface protocol */
170da8bb3a3SMike Smith #define MLX_IFTYPE_2	2
1711ac4b82bSMike Smith #define MLX_IFTYPE_3	3
1721ac4b82bSMike Smith #define MLX_IFTYPE_4	4
1731ac4b82bSMike Smith #define MLX_IFTYPE_5	5
1741ac4b82bSMike Smith     int			(* mlx_tryqueue)(struct mlx_softc *sc, struct mlx_command *mc);
1751ac4b82bSMike Smith     int			(* mlx_findcomplete)(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
1761ac4b82bSMike Smith     void		(* mlx_intaction)(struct mlx_softc *sc, int action);
177da8bb3a3SMike Smith     int			(* mlx_fw_handshake)(struct mlx_softc *sc, int *error, int *param1, int *param2);
1781ac4b82bSMike Smith #define MLX_INTACTION_DISABLE		0
1791ac4b82bSMike Smith #define MLX_INTACTION_ENABLE		1
1801ac4b82bSMike Smith };
1811ac4b82bSMike Smith 
1821ac4b82bSMike Smith /*
1835792b7feSMike Smith  * Simple (stupid) locks.
1845792b7feSMike Smith  *
1855792b7feSMike Smith  * Note that these are designed to avoid reentrancy, not concurrency, and will
1865792b7feSMike Smith  * need to be replaced with something better.
1875792b7feSMike Smith  */
1885792b7feSMike Smith #define MLX_LOCK_COMPLETING	(1<<0)
1895792b7feSMike Smith #define MLX_LOCK_STARTING	(1<<1)
1905792b7feSMike Smith 
1915792b7feSMike Smith static __inline int
1925792b7feSMike Smith mlx_lock_tas(struct mlx_softc *sc, int lock)
1935792b7feSMike Smith {
1945792b7feSMike Smith     if ((sc)->mlx_locks & (lock))
1955792b7feSMike Smith 	return(1);
1965792b7feSMike Smith     atomic_set_int(&sc->mlx_locks, lock);
1975792b7feSMike Smith     return(0);
1985792b7feSMike Smith }
1995792b7feSMike Smith 
2005792b7feSMike Smith static __inline void
2015792b7feSMike Smith mlx_lock_clr(struct mlx_softc *sc, int lock)
2025792b7feSMike Smith {
2035792b7feSMike Smith     atomic_clear_int(&sc->mlx_locks, lock);
2045792b7feSMike Smith }
2055792b7feSMike Smith 
2065792b7feSMike Smith /*
2071ac4b82bSMike Smith  * Interface between bus connections and driver core.
2081ac4b82bSMike Smith  */
2091ac4b82bSMike Smith extern void		mlx_free(struct mlx_softc *sc);
2101ac4b82bSMike Smith extern int		mlx_attach(struct mlx_softc *sc);
2111ac4b82bSMike Smith extern void		mlx_startup(struct mlx_softc *sc);
2121ac4b82bSMike Smith extern void		mlx_intr(void *data);
2131ac4b82bSMike Smith extern int		mlx_detach(device_t dev);
2141ac4b82bSMike Smith extern int		mlx_shutdown(device_t dev);
2151ac4b82bSMike Smith extern int		mlx_suspend(device_t dev);
2161ac4b82bSMike Smith extern int		mlx_resume(device_t dev);
2171ac4b82bSMike Smith extern d_open_t		mlx_open;
2181ac4b82bSMike Smith extern d_close_t	mlx_close;
2191ac4b82bSMike Smith extern d_ioctl_t	mlx_ioctl;
2201ac4b82bSMike Smith 
2211ac4b82bSMike Smith extern devclass_t	mlx_devclass;
222421f2f7dSMike Smith extern devclass_t	mlxd_devclass;
2231ac4b82bSMike Smith 
2241ac4b82bSMike Smith /*
2251ac4b82bSMike Smith  * Mylex System Disk driver
2261ac4b82bSMike Smith  */
2271ac4b82bSMike Smith struct mlxd_softc
2281ac4b82bSMike Smith {
2291ac4b82bSMike Smith     device_t		mlxd_dev;
2301ac4b82bSMike Smith     struct mlx_softc	*mlxd_controller;
2311ac4b82bSMike Smith     struct mlx_sysdrive	*mlxd_drive;
2320b7ed341SPoul-Henning Kamp     struct disk		*mlxd_disk;
2331ac4b82bSMike Smith     int			mlxd_unit;
2341ac4b82bSMike Smith     int			mlxd_flags;
2351ac4b82bSMike Smith #define MLXD_OPEN	(1<<0)		/* drive is open (can't shut down) */
2361ac4b82bSMike Smith };
2371ac4b82bSMike Smith 
2381ac4b82bSMike Smith /*
2391ac4b82bSMike Smith  * Interface between driver core and disk driver (should be using a bus?)
2401ac4b82bSMike Smith  */
24115fd5d22SMike Smith extern int	mlx_submit_buf(struct mlx_softc *sc, mlx_bio *bp);
242b40ce416SJulian Elischer extern int	mlx_submit_ioctl(struct mlx_softc *sc,
243b40ce416SJulian Elischer 			struct mlx_sysdrive *drive, u_long cmd,
244b40ce416SJulian Elischer 			caddr_t addr, int32_t flag, struct thread *td);
2451ac4b82bSMike Smith extern void	mlxd_intr(void *data);
2461ac4b82bSMike Smith 
2471ac4b82bSMike Smith 
248