xref: /freebsd/sys/dev/mlx/mlxvar.h (revision da8bb3a3bf71a0579594e2faa0dd92b821689e12)
1 /*-
2  * Copyright (c) 1999 Michael Smith
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *	$FreeBSD$
27  */
28 
29 /*
30  * Debugging levels:
31  *  0 - quiet, only emit warnings
32  *  1 - noisy, emit major function points and things done
33  *  2 - extremely noisy, emit trace items in loops, etc.
34  */
35 #ifdef MLX_DEBUG
36 #define debug(level, fmt, args...)	do { if (level <= MLX_DEBUG) printf("%s: " fmt "\n", __FUNCTION__ , ##args); } while(0)
37 #define debug_called(level)		do { if (level <= MLX_DEBUG) printf(__FUNCTION__ ": called\n"); } while(0)
38 #else
39 #define debug(level, fmt, args...)
40 #define debug_called(level)
41 #endif
42 
43 
44 /*
45  * We could actually use all 17/33 segments, but using only 16/32 means that
46  * each scatter/gather map is 128/256 bytes in size, and thus we don't have to worry about
47  * maps crossing page boundaries.
48  */
49 #define MLX_NSEG_OLD	16		/* max scatter/gather segments we use 3.x and earlier */
50 #define	MLX_NSEG_NEW	32		/* max scatter/gather segments we use 4.x and later */
51 #define MLX_NSLOTS	256		/* max number of command slots */
52 
53 #define MLX_MAXDRIVES	32		/* max number of system drives */
54 
55 /*
56  * Structure describing a System Drive as attached to the controller.
57  */
58 struct mlx_sysdrive
59 {
60     /* from MLX_CMD_ENQSYSDRIVE */
61     u_int32_t		ms_size;
62     int			ms_state;
63     int			ms_raidlevel;
64 
65     /* synthetic geometry */
66     int			ms_cylinders;
67     int			ms_heads;
68     int			ms_sectors;
69 
70     /* handle for attached driver */
71     device_t		ms_disk;
72 };
73 
74 /*
75  * Per-command control structure.
76  */
77 struct mlx_command
78 {
79     TAILQ_ENTRY(mlx_command)	mc_link;	/* list linkage */
80 
81     struct mlx_softc		*mc_sc;		/* controller that owns us */
82     u_int8_t			mc_slot;	/* command slot we occupy */
83     u_int16_t			mc_status;	/* command completion status */
84     time_t			mc_timeout;	/* when this command expires */
85     u_int8_t			mc_mailbox[16];	/* command mailbox */
86     u_int32_t			mc_sgphys;	/* physical address of s/g array in controller space */
87     int				mc_nsgent;	/* number of entries in s/g map */
88     int				mc_flags;
89 #define MLX_CMD_DATAIN		(1<<0)
90 #define MLX_CMD_DATAOUT		(1<<1)
91 #define MLX_CMD_PRIORITY	(1<<2)		/* high-priority command */
92 
93     void			*mc_data;	/* data buffer */
94     size_t			mc_length;
95     bus_dmamap_t		mc_dmamap;	/* DMA map for data */
96     u_int32_t			mc_dataphys;	/* data buffer base address controller space */
97 
98     void			(* mc_complete)(struct mlx_command *mc);	/* completion handler */
99     void			*mc_private;	/* submitter-private data or wait channel */
100 };
101 
102 /*
103  * Per-controller structure.
104  */
105 struct mlx_softc
106 {
107     /* bus connections */
108     device_t		mlx_dev;
109     dev_t		mlx_dev_t;
110     struct resource	*mlx_mem;	/* mailbox interface window */
111     bus_space_handle_t	mlx_bhandle;	/* bus space handle */
112     bus_space_tag_t	mlx_btag;	/* bus space tag */
113     bus_dma_tag_t	mlx_parent_dmat;/* parent DMA tag */
114     bus_dma_tag_t	mlx_buffer_dmat;/* data buffer DMA tag */
115     struct resource	*mlx_irq;	/* interrupt */
116     void		*mlx_intr;	/* interrupt handle */
117 
118     /* scatter/gather lists and their controller-visible mappings */
119     struct mlx_sgentry	*mlx_sgtable;	/* s/g lists */
120     u_int32_t		mlx_sgbusaddr;	/* s/g table base address in bus space */
121     bus_dma_tag_t	mlx_sg_dmat;	/* s/g buffer DMA tag */
122     bus_dmamap_t	mlx_sg_dmamap;	/* map for s/g buffers */
123     int			mlx_sg_nseg;	/* max number of s/g entries */
124 
125     /* controller limits and features */
126     struct mlx_enquiry2	*mlx_enq2;
127     int			mlx_maxiop;	/* hard maximum number of commands */
128     int			mlx_feature;	/* controller features/quirks */
129 #define MLX_FEAT_PAUSEWORKS	(1<<0)	/* channel pause works as expected */
130 
131     /* controller queues and arrays */
132     TAILQ_HEAD(, mlx_command)	mlx_freecmds;		/* command structures available for reuse */
133     TAILQ_HEAD(, mlx_command)	mlx_work;		/* active commands */
134     struct mlx_command	*mlx_busycmd[MLX_NSLOTS];	/* busy commands */
135     int			mlx_busycmds;			/* count of busy commands */
136     struct mlx_sysdrive	mlx_sysdrive[MLX_MAXDRIVES];	/* system drives */
137     struct buf_queue_head mlx_bufq;			/* outstanding I/O operations */
138     int			mlx_waitbufs;			/* number of bufs awaiting commands */
139 
140     /* controller status */
141     int			mlx_geom;
142 #define MLX_GEOM_128_32		0	/* geoemetry translation modes */
143 #define MLX_GEOM_256_63		1
144     int			mlx_state;
145 #define MLX_STATE_INTEN		(1<<0)	/* interrupts have been enabled */
146 #define MLX_STATE_SHUTDOWN	(1<<1)	/* controller is shut down */
147 #define MLX_STATE_OPEN		(1<<2)	/* control device is open */
148 #define MLX_STATE_SUSPEND	(1<<3)	/* controller is suspended */
149     struct callout_handle mlx_timeout;	/* periodic status monitor */
150     time_t		mlx_lastpoll;	/* last time_second we polled for status */
151     int			mlx_lastevent;	/* sequence number of the last event we recorded */
152     int			mlx_currevent;	/* sequence number last time we looked */
153     int			mlx_rebuild;	/* if >= 0, drive is being rebuilt */
154     u_int32_t		mlx_rebuildstat;/* blocks left to rebuild if active */
155     int			mlx_check;	/* if >= 0, drive is being checked */
156     struct mlx_pause	mlx_pause;	/* pending pause operation details */
157 
158     int			mlx_locks;	/* reentrancy avoidance */
159     int			mlx_flags;
160 #define MLX_SPINUP_REPORTED	(1<<0)	/* "spinning up drives" message displayed */
161 
162     /* interface-specific accessor functions */
163     int			mlx_iftype;	/* interface protocol */
164 #define MLX_IFTYPE_2	2
165 #define MLX_IFTYPE_3	3
166 #define MLX_IFTYPE_4	4
167 #define MLX_IFTYPE_5	5
168     int			(* mlx_tryqueue)(struct mlx_softc *sc, struct mlx_command *mc);
169     int			(* mlx_findcomplete)(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
170     void		(* mlx_intaction)(struct mlx_softc *sc, int action);
171     int			(* mlx_fw_handshake)(struct mlx_softc *sc, int *error, int *param1, int *param2);
172 #define MLX_INTACTION_DISABLE		0
173 #define MLX_INTACTION_ENABLE		1
174 };
175 
176 /*
177  * Simple (stupid) locks.
178  *
179  * Note that these are designed to avoid reentrancy, not concurrency, and will
180  * need to be replaced with something better.
181  */
182 #define MLX_LOCK_COMPLETING	(1<<0)
183 #define MLX_LOCK_STARTING	(1<<1)
184 
185 static __inline int
186 mlx_lock_tas(struct mlx_softc *sc, int lock)
187 {
188     if ((sc)->mlx_locks & (lock))
189 	return(1);
190     atomic_set_int(&sc->mlx_locks, lock);
191     return(0);
192 }
193 
194 static __inline void
195 mlx_lock_clr(struct mlx_softc *sc, int lock)
196 {
197     atomic_clear_int(&sc->mlx_locks, lock);
198 }
199 
200 /*
201  * Interface between bus connections and driver core.
202  */
203 extern void		mlx_free(struct mlx_softc *sc);
204 extern int		mlx_attach(struct mlx_softc *sc);
205 extern void		mlx_startup(struct mlx_softc *sc);
206 extern void		mlx_intr(void *data);
207 extern int		mlx_detach(device_t dev);
208 extern int		mlx_shutdown(device_t dev);
209 extern int		mlx_suspend(device_t dev);
210 extern int		mlx_resume(device_t dev);
211 extern d_open_t		mlx_open;
212 extern d_close_t	mlx_close;
213 extern d_ioctl_t	mlx_ioctl;
214 
215 extern devclass_t	mlx_devclass;
216 
217 /*
218  * Mylex System Disk driver
219  */
220 struct mlxd_softc
221 {
222     device_t		mlxd_dev;
223     struct mlx_softc	*mlxd_controller;
224     struct mlx_sysdrive	*mlxd_drive;
225     struct disk		mlxd_disk;
226     struct devstat	mlxd_stats;
227     struct disklabel	mlxd_label;
228     int			mlxd_unit;
229     int			mlxd_flags;
230 #define MLXD_OPEN	(1<<0)		/* drive is open (can't shut down) */
231 };
232 
233 /*
234  * Interface between driver core and disk driver (should be using a bus?)
235  */
236 extern int	mlx_submit_buf(struct mlx_softc *sc, struct buf *bp);
237 extern int	mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd,
238 				 caddr_t addr, int32_t flag, struct proc *p);
239 extern void	mlxd_intr(void *data);
240 
241 
242