xref: /netbsd-src/sys/dev/ic/mlxvar.h (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: mlxvar.h,v 1.7 2002/09/22 18:59:00 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*-
40  * Copyright (c) 1999 Michael Smith
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  * from FreeBSD: mlxvar.h,v 1.5.2.2 2000/04/24 19:40:50 msmith Exp
65  */
66 
67 #ifndef _IC_MLXVAR_H_
68 #define	_IC_MLXVAR_H_
69 
70 #include "locators.h"
71 
72 /* Older boards allow up to 17 segments and 64kB transfers. */
73 #define	MLX_MAX_SEGS		17
74 #define	MLX_MAX_XFER		65536
75 #define MLX_SGL_SIZE		(sizeof(struct mlx_sgentry) * MLX_MAX_SEGS)
76 
77 /* This shouldn't be ajusted lightly... */
78 #define MLX_MAX_DRIVES		32
79 
80 /* Maximum queue depth, matching the older controllers. */
81 #define	MLX_MAX_QUEUECNT	63
82 
83 /* Number of CCBs to reserve for control operations. */
84 #define	MLX_NCCBS_CONTROL	7
85 
86 /* Structure describing a system drive as attached to the controller. */
87 struct mlx_sysdrive {
88 	u_int32_t	ms_size;
89 	u_short		ms_state;
90 	u_short		ms_raidlevel;
91 	struct device	*ms_dv;
92 };
93 
94 /* Optional per-CCB context. */
95 struct mlx_ccb;
96 struct mlx_context {
97 	void	(*mx_handler)(struct mlx_ccb *);
98 	void 	*mx_context;
99 	struct	device	*mx_dv;
100 };
101 
102 /* Command control block. */
103 struct mlx_ccb {
104 	union {
105 		SIMPLEQ_ENTRY(mlx_ccb) simpleq;
106 		SLIST_ENTRY(mlx_ccb) slist;
107 		TAILQ_ENTRY(mlx_ccb) tailq;
108 	} mc_chain;
109 	u_int		mc_flags;
110 	u_int		mc_status;
111 	u_int		mc_ident;
112 	time_t		mc_expiry;
113 	u_int		mc_nsgent;
114 	u_int		mc_xfer_size;
115 	bus_addr_t	mc_xfer_phys;
116 	bus_dmamap_t	mc_xfer_map;
117 	struct mlx_context mc_mx;
118 	u_int8_t	mc_mbox[16];
119 };
120 #define	MC_XFER_IN	MU_XFER_IN	/* Map describes inbound xfer */
121 #define	MC_XFER_OUT	MU_XFER_OUT	/* Map describes outbound xfer */
122 #define	MC_WAITING	0x0400		/* We have waiters */
123 #define	MC_CONTROL	0x0800		/* Control operation */
124 
125 /*
126  * Per-controller state.
127  */
128 struct mlx_softc {
129 	struct device		mlx_dv;
130 	bus_space_tag_t		mlx_iot;
131 	bus_space_handle_t	mlx_ioh;
132 	bus_dma_tag_t		mlx_dmat;
133 	bus_dmamap_t		mlx_dmamap;
134 	void			*mlx_ih;
135 
136 	SLIST_HEAD(, mlx_ccb)	mlx_ccb_freelist;
137 	TAILQ_HEAD(, mlx_ccb)	mlx_ccb_worklist;
138 	SIMPLEQ_HEAD(, mlx_ccb)	mlx_ccb_queue;
139 	struct mlx_ccb		*mlx_ccbs;
140 	int			mlx_nccbs;
141 	int			mlx_nccbs_ctrl;
142 
143 	caddr_t			mlx_sgls;
144 	bus_addr_t		mlx_sgls_paddr;
145 
146 	int	(*mlx_submit)(struct mlx_softc *, struct mlx_ccb *);
147 	int	(*mlx_findcomplete)(struct mlx_softc *, u_int *, u_int *);
148 	void	(*mlx_intaction)(struct mlx_softc *, int);
149 	int	(*mlx_fw_handshake)(struct mlx_softc *, int *, int *, int *);
150 	int	(*mlx_reset)(struct mlx_softc *);
151 
152 	int			mlx_max_queuecnt;
153 	struct mlx_cinfo	mlx_ci;
154 
155 	time_t			mlx_lastpoll;
156 	u_int			mlx_lastevent;
157 	u_int			mlx_currevent;
158 	u_int			mlx_bg;
159 	struct mlx_rebuild_status mlx_rebuildstat;
160 	struct mlx_pause	mlx_pause;
161 	int			mlx_flags;
162 
163 	struct mlx_sysdrive	mlx_sysdrive[MLX_MAX_DRIVES];
164 	int			mlx_numsysdrives;
165 };
166 
167 #define MLX_BG_CHECK		1	/* we started a check */
168 #define MLX_BG_REBUILD		2	/* we started a rebuild */
169 #define MLX_BG_SPONTANEOUS	3	/* it just happened somehow */
170 
171 #define MLXF_SPINUP_REPORTED	0x0001	/* "spinning up drives" displayed */
172 #define MLXF_EVENTLOG_BUSY	0x0002	/* currently reading event log */
173 #define MLXF_FW_INITTED		0x0004	/* firmware init crap done */
174 #define MLXF_PAUSEWORKS		0x0008	/* channel pause works as expected */
175 #define MLXF_OPEN		0x0010	/* control device is open */
176 #define	MLXF_INITOK		0x0020	/* controller initialised OK */
177 #define	MLXF_PERIODIC_CTLR	0x0040	/* periodic check running */
178 #define	MLXF_PERIODIC_DRIVE	0x0080	/* periodic check running */
179 #define	MLXF_PERIODIC_REBUILD	0x0100	/* periodic check running */
180 #define	MLXF_RESCANNING		0x0400	/* rescanning drive table */
181 
182 struct mlx_attach_args {
183 	int		mlxa_unit;
184 };
185 
186 #define	mlxacf_unit	cf_loc[MLXCF_UNIT]
187 
188 int	mlx_flush(struct mlx_softc *, int);
189 void	mlx_init(struct mlx_softc *, const char *);
190 int	mlx_intr(void *);
191 
192 int	mlx_ccb_alloc(struct mlx_softc *, struct mlx_ccb **, int);
193 const char *mlx_ccb_diagnose(struct mlx_ccb *);
194 void	mlx_ccb_enqueue(struct mlx_softc *, struct mlx_ccb *);
195 void	mlx_ccb_free(struct mlx_softc *, struct mlx_ccb *);
196 int	mlx_ccb_map(struct mlx_softc *, struct mlx_ccb *, void *, int, int);
197 int	mlx_ccb_poll(struct mlx_softc *, struct mlx_ccb *, int);
198 void	mlx_ccb_unmap(struct mlx_softc *, struct mlx_ccb *);
199 int	mlx_ccb_wait(struct mlx_softc *, struct mlx_ccb *);
200 
201 static __inline__ void	mlx_make_type1(struct mlx_ccb *, u_int8_t, u_int16_t,
202 				       u_int32_t, u_int8_t, u_int32_t,
203 				       u_int8_t);
204 static __inline__ void	mlx_make_type2(struct mlx_ccb *, u_int8_t, u_int8_t,
205 				       u_int8_t, u_int8_t, u_int8_t,
206 				       u_int8_t, u_int8_t, u_int32_t,
207 				       u_int8_t);
208 static __inline__ void	mlx_make_type3(struct mlx_ccb *, u_int8_t, u_int8_t,
209 				       u_int8_t, u_int16_t, u_int8_t,
210 				       u_int8_t, u_int32_t, u_int8_t);
211 static __inline__ void	mlx_make_type4(struct mlx_ccb *, u_int8_t, u_int16_t,
212 				       u_int32_t, u_int32_t, u_int8_t);
213 static __inline__ void	mlx_make_type5(struct mlx_ccb *, u_int8_t,  u_int8_t,
214 				       u_int8_t, u_int32_t, u_int32_t,
215 				       u_int8_t);
216 
217 static __inline__ u_int8_t	mlx_inb(struct mlx_softc *, int);
218 static __inline__ u_int16_t	mlx_inw(struct mlx_softc *, int);
219 static __inline__ u_int32_t	mlx_inl(struct mlx_softc *, int);
220 static __inline__ void		mlx_outb(struct mlx_softc *, int, u_int8_t);
221 static __inline__ void		mlx_outw(struct mlx_softc *, int, u_int16_t);
222 static __inline__ void		mlx_outl(struct mlx_softc *, int, u_int32_t);
223 
224 static __inline__ void
225 mlx_make_type1(struct mlx_ccb *mc, u_int8_t code, u_int16_t f1, u_int32_t f2,
226 	       u_int8_t f3, u_int32_t f4, u_int8_t f5)
227 {
228 
229 	mc->mc_mbox[0x0] = code;
230 	mc->mc_mbox[0x2] = f1;
231 	mc->mc_mbox[0x3] = ((f2 >> 18) & 0xc0) | ((f1 >> 8) & 0x3f);
232 	mc->mc_mbox[0x4] = f2;
233 	mc->mc_mbox[0x5] = (f2 >> 8);
234 	mc->mc_mbox[0x6] = (f2 >> 16);
235 	mc->mc_mbox[0x7] = f3;
236 	mc->mc_mbox[0x8] = f4;
237 	mc->mc_mbox[0x9] = (f4 >> 8);
238 	mc->mc_mbox[0xa] = (f4 >> 16);
239 	mc->mc_mbox[0xb] = (f4 >> 24);
240 	mc->mc_mbox[0xc] = f5;
241 }
242 
243 static __inline__ void
244 mlx_make_type2(struct mlx_ccb *mc, u_int8_t code, u_int8_t f1, u_int8_t f2,
245 	       u_int8_t f3, u_int8_t f4, u_int8_t f5, u_int8_t f6,
246 	       u_int32_t f7, u_int8_t f8)
247 {
248 
249 	mc->mc_mbox[0x0] = code;
250 	mc->mc_mbox[0x2] = f1;
251 	mc->mc_mbox[0x3] = f2;
252 	mc->mc_mbox[0x4] = f3;
253 	mc->mc_mbox[0x5] = f4;
254 	mc->mc_mbox[0x6] = f5;
255 	mc->mc_mbox[0x7] = f6;
256 	mc->mc_mbox[0x8] = f7;
257 	mc->mc_mbox[0x9] = (f7 >> 8);
258 	mc->mc_mbox[0xa] = (f7 >> 16);
259 	mc->mc_mbox[0xb] = (f7 >> 24);
260 	mc->mc_mbox[0xc] = f8;
261 }
262 
263 static __inline__ void
264 mlx_make_type3(struct mlx_ccb *mc, u_int8_t code, u_int8_t f1, u_int8_t f2,
265 	       u_int16_t f3, u_int8_t f4, u_int8_t f5, u_int32_t f6,
266 	       u_int8_t f7)
267 {
268 
269 	mc->mc_mbox[0x0] = code;
270 	mc->mc_mbox[0x2] = f1;
271 	mc->mc_mbox[0x3] = f2;
272 	mc->mc_mbox[0x4] = f3;
273 	mc->mc_mbox[0x5] = (f3 >> 8);
274 	mc->mc_mbox[0x6] = f4;
275 	mc->mc_mbox[0x7] = f5;
276 	mc->mc_mbox[0x8] = f6;
277 	mc->mc_mbox[0x9] = (f6 >> 8);
278 	mc->mc_mbox[0xa] = (f6 >> 16);
279 	mc->mc_mbox[0xb] = (f6 >> 24);
280 	mc->mc_mbox[0xc] = f7;
281 }
282 
283 static __inline__ void
284 mlx_make_type4(struct mlx_ccb *mc, u_int8_t code,  u_int16_t f1, u_int32_t f2,
285 	       u_int32_t f3, u_int8_t f4)
286 {
287 
288 	mc->mc_mbox[0x0] = code;
289 	mc->mc_mbox[0x2] = f1;
290 	mc->mc_mbox[0x3] = (f1 >> 8);
291 	mc->mc_mbox[0x4] = f2;
292 	mc->mc_mbox[0x5] = (f2 >> 8);
293 	mc->mc_mbox[0x6] = (f2 >> 16);
294 	mc->mc_mbox[0x7] = (f2 >> 24);
295 	mc->mc_mbox[0x8] = f3;
296 	mc->mc_mbox[0x9] = (f3 >> 8);
297 	mc->mc_mbox[0xa] = (f3 >> 16);
298 	mc->mc_mbox[0xb] = (f3 >> 24);
299 	mc->mc_mbox[0xc] = f4;
300 }
301 
302 static __inline__ void
303 mlx_make_type5(struct mlx_ccb *mc, u_int8_t code, u_int8_t f1, u_int8_t f2,
304 	       u_int32_t f3, u_int32_t f4, u_int8_t f5)
305 {
306 
307 	mc->mc_mbox[0x0] = code;
308 	mc->mc_mbox[0x2] = f1;
309 	mc->mc_mbox[0x3] = f2;
310 	mc->mc_mbox[0x4] = f3;
311 	mc->mc_mbox[0x5] = (f3 >> 8);
312 	mc->mc_mbox[0x6] = (f3 >> 16);
313 	mc->mc_mbox[0x7] = (f3 >> 24);
314 	mc->mc_mbox[0x8] = f4;
315 	mc->mc_mbox[0x9] = (f4 >> 8);
316 	mc->mc_mbox[0xa] = (f4 >> 16);
317 	mc->mc_mbox[0xb] = (f4 >> 24);
318 	mc->mc_mbox[0xc] = f5;
319 }
320 
321 static __inline__ u_int8_t
322 mlx_inb(struct mlx_softc *mlx, int off)
323 {
324 
325 	bus_space_barrier(mlx->mlx_iot, mlx->mlx_ioh, off, 1,
326 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
327 	return (bus_space_read_1(mlx->mlx_iot, mlx->mlx_ioh, off));
328 }
329 
330 static __inline__ u_int16_t
331 mlx_inw(struct mlx_softc *mlx, int off)
332 {
333 
334 	bus_space_barrier(mlx->mlx_iot, mlx->mlx_ioh, off, 2,
335 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
336 	return (bus_space_read_2(mlx->mlx_iot, mlx->mlx_ioh, off));
337 }
338 
339 static __inline__ u_int32_t
340 mlx_inl(struct mlx_softc *mlx, int off)
341 {
342 
343 	bus_space_barrier(mlx->mlx_iot, mlx->mlx_ioh, off, 4,
344 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
345 	return (bus_space_read_4(mlx->mlx_iot, mlx->mlx_ioh, off));
346 }
347 
348 static __inline__ void
349 mlx_outb(struct mlx_softc *mlx, int off, u_int8_t val)
350 {
351 
352 	bus_space_write_1(mlx->mlx_iot, mlx->mlx_ioh, off, val);
353 	bus_space_barrier(mlx->mlx_iot, mlx->mlx_ioh, off, 1,
354 	    BUS_SPACE_BARRIER_WRITE);
355 }
356 
357 static __inline__ void
358 mlx_outw(struct mlx_softc *mlx, int off, u_int16_t val)
359 {
360 
361 	bus_space_write_2(mlx->mlx_iot, mlx->mlx_ioh, off, val);
362 	bus_space_barrier(mlx->mlx_iot, mlx->mlx_ioh, off, 2,
363 	    BUS_SPACE_BARRIER_WRITE);
364 }
365 
366 static __inline__ void
367 mlx_outl(struct mlx_softc *mlx, int off, u_int32_t val)
368 {
369 
370 	bus_space_write_4(mlx->mlx_iot, mlx->mlx_ioh, off, val);
371 	bus_space_barrier(mlx->mlx_iot, mlx->mlx_ioh, off, 4,
372 	    BUS_SPACE_BARRIER_WRITE);
373 }
374 
375 #endif	/* !_IC_MLXVAR_H_ */
376