xref: /dragonfly/sys/dev/raid/ciss/cissvar.h (revision b40e316c)
1 /*-
2  * Copyright (c) 2001 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: src/sys/dev/ciss/cissvar.h,v 1.3.2.2 2003/02/06 21:42:59 ps Exp $
27  *	$DragonFly: src/sys/dev/raid/ciss/cissvar.h,v 1.4 2004/09/15 16:02:41 joerg Exp $
28  */
29 
30 /*
31  * CISS adapter driver datastructures
32  */
33 
34 /************************************************************************
35  * Tunable parameters
36  */
37 
38 /*
39  * There is no guaranteed upper bound on the number of concurrent
40  * commands an adapter may claim to support.  Cap it at a reasonable
41  * value.
42  */
43 #define CISS_MAX_REQUESTS	256
44 
45 /*
46  * Maximum number of logical drives we support.
47  */
48 #define CISS_MAX_LOGICAL	15
49 
50 /*
51  * Interrupt reduction can be controlled by tuning the interrupt
52  * coalesce delay and count paramters.  The delay (in microseconds)
53  * defers delivery of interrupts to increase the chance of there being
54  * more than one completed command ready when the interrupt is
55  * delivered.  The count expedites the delivery of the interrupt when
56  * the given number of commands are ready.
57  *
58  * If the delay is set to 0, interrupts are delivered immediately.
59  */
60 #define CISS_INTERRUPT_COALESCE_DELAY	1000
61 #define CISS_INTERRUPT_COALESCE_COUNT	16
62 
63 /*
64  * Heartbeat routine timeout in seconds.  Note that since event
65  * handling is performed on a callback basis, we don't need this to
66  * run very often.
67  */
68 #define CISS_HEARTBEAT_RATE		10
69 
70 /*
71  * Maximum number of events we will queue for a monitoring utility.
72  */
73 #define CISS_MAX_EVENTS		32
74 
75 /************************************************************************
76  * Compatibility with older versions of FreeBSD
77  */
78 #if defined(__FreeBSD__) && __FreeBSD_version < 440001
79 #warning testing old-FreeBSD compat
80 typedef struct proc	d_thread_t;
81 #endif
82 
83 /************************************************************************
84  * Command queue statistics
85  */
86 
87 #define CISSQ_FREE	0
88 #define CISSQ_BUSY	1
89 #define CISSQ_COMPLETE	2
90 #define CISSQ_COUNT	3
91 
92 struct ciss_qstat
93 {
94     u_int32_t	q_length;
95     u_int32_t	q_max;
96 };
97 
98 /************************************************************************
99  * Driver version.  Only really significant to the ACU interface.
100  */
101 #define CISS_DRIVER_VERSION	20011201
102 
103 /************************************************************************
104  * Driver data structures
105  */
106 
107 /*
108  * Each command issued to the adapter is managed by a request
109  * structure.
110  */
111 struct ciss_request
112 {
113     TAILQ_ENTRY(ciss_request)	cr_link;
114     int				cr_onq;		/* which queue we are on */
115 
116     struct ciss_softc		*cr_sc;		/* controller softc */
117     void			*cr_data;	/* data buffer */
118     u_int32_t			cr_length;	/* data length */
119     bus_dmamap_t		cr_datamap;	/* DMA map for data */
120     int				cr_tag;
121     int				cr_flags;
122 #define CISS_REQ_MAPPED		(1<<0)		/* data mapped */
123 #define CISS_REQ_SLEEP		(1<<1)		/* submitter sleeping */
124 #define CISS_REQ_POLL		(1<<2)		/* submitter polling */
125 #define CISS_REQ_DATAOUT	(1<<3)		/* data host->adapter */
126 #define CISS_REQ_DATAIN		(1<<4)		/* data adapter->host */
127 
128     void			(* cr_complete)(struct ciss_request *);
129     void			*cr_private;
130 };
131 
132 /*
133  * The adapter command structure is defined with a zero-length
134  * scatter/gather list size.  In practise, we want space for a
135  * scatter-gather list, and we also want to avoid having commands
136  * cross page boundaries.
137  *
138  * Note that 512 bytes yields 28 scatter/gather entries, or the
139  * ability to map (26 * PAGE_SIZE) + 2 bytes of data.  On x86, this is
140  * 104kB.  256 bytes would only yield 12 entries, giving a mere 40kB,
141  * too small.
142  */
143 
144 #define CISS_COMMAND_ALLOC_SIZE		512	/* XXX tune to get sensible s/g list length */
145 #define CISS_COMMAND_SG_LENGTH	((CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command)) \
146 				 / sizeof(struct ciss_sg_entry))
147 
148 /*
149  * Per-logical-drive data.
150  */
151 struct ciss_ldrive
152 {
153     union ciss_device_address	cl_address;
154 
155     int				cl_status;
156 #define CISS_LD_NONEXISTENT	0
157 #define CISS_LD_ONLINE		1
158 #define CISS_LD_OFFLINE		2
159 
160     struct ciss_bmic_id_ldrive	*cl_ldrive;
161     struct ciss_bmic_id_lstatus	*cl_lstatus;
162     struct ciss_ldrive_geometry	cl_geometry;
163 
164     char			cl_name[16];		/* device name */
165 };
166 
167 /*
168  * Per-adapter data
169  */
170 struct ciss_softc
171 {
172     /* bus connections */
173     device_t			ciss_dev;		/* bus attachment */
174     dev_t			ciss_dev_t;		/* control device */
175 
176     struct resource		*ciss_regs_resource;	/* register interface window */
177     int				ciss_regs_rid;		/* resource ID */
178     bus_space_handle_t		ciss_regs_bhandle;	/* bus space handle */
179     bus_space_tag_t		ciss_regs_btag;		/* bus space tag */
180 
181     struct resource		*ciss_cfg_resource;	/* config struct interface window */
182     int				ciss_cfg_rid;		/* resource ID */
183     struct ciss_config_table	*ciss_cfg;		/* config table in adapter memory */
184     struct ciss_bmic_id_table	*ciss_id;		/* ID table in host memory */
185     u_int32_t			ciss_heartbeat;		/* last heartbeat value */
186     int				ciss_heart_attack;	/* number of times we have seen this value */
187 
188     struct resource		*ciss_irq_resource;	/* interrupt */
189     int				ciss_irq_rid;		/* resource ID */
190     void			*ciss_intr;		/* interrupt handle */
191 
192     bus_dma_tag_t		ciss_parent_dmat;	/* parent DMA tag */
193     bus_dma_tag_t		ciss_buffer_dmat;	/* data buffer/command DMA tag */
194 
195     u_int32_t			ciss_interrupt_mask;	/* controller interrupt mask bits */
196 
197     int				ciss_max_requests;
198     struct ciss_request		ciss_request[CISS_MAX_REQUESTS];	/* requests */
199     void			*ciss_command;		/* command structures */
200     bus_dma_tag_t		ciss_command_dmat;	/* command DMA tag */
201     bus_dmamap_t		ciss_command_map;	/* command DMA map */
202     u_int32_t			ciss_command_phys;	/* command array base address */
203     TAILQ_HEAD(,ciss_request)	ciss_free;		/* requests available for reuse */
204     TAILQ_HEAD(,ciss_request)	ciss_busy;		/* requests in the adapter */
205     TAILQ_HEAD(,ciss_request)	ciss_complete;		/* requests which have been returned by the adapter */
206 
207     struct callout		ciss_periodic;		/* periodic event handling */
208     struct ciss_request		*ciss_periodic_notify;	/* notify callback request */
209 
210     struct ciss_notify		ciss_notify[CISS_MAX_EVENTS];
211     int				ciss_notify_head;	/* saved-event ringbuffer */
212     int				ciss_notify_tail;
213 
214     struct ciss_ldrive		ciss_logical[CISS_MAX_LOGICAL];
215 
216     struct cam_devq		*ciss_cam_devq;
217     struct cam_sim		*ciss_cam_sim;
218     struct cam_path		*ciss_cam_path;
219 
220     int				ciss_flags;
221 #define CISS_FLAG_NOTIFY_OK	(1<<0)		/* notify command running OK */
222 #define CISS_FLAG_CONTROL_OPEN	(1<<1)		/* control device is open */
223 #define CISS_FLAG_ABORTING	(1<<2)		/* driver is going away */
224 #define CISS_FLAG_RUNNING	(1<<3)		/* driver is running (interrupts usable) */
225 
226 #define CISS_FLAG_FAKE_SYNCH	(1<<16)		/* needs SYNCHRONISE_CACHE faked */
227 #define CISS_FLAG_BMIC_ABORT	(1<<17)		/* use BMIC command to abort Notify on Event */
228 
229     struct ciss_qstat		ciss_qstat[CISSQ_COUNT];	/* queue statistics */
230 };
231 
232 /*
233  * Given a request tag, find the corresponding command in virtual or
234  * physical space.
235  *
236  * The arithmetic here is due to the allocation of ciss_command structures
237  * inside CISS_COMMAND_ALLOC_SIZE blocks.  See the comment at the definition
238  * of CISS_COMMAND_ALLOC_SIZE above.
239  */
240 #define CISS_FIND_COMMAND(cr)							\
241 	(struct ciss_command *)((u_int8_t *)(cr)->cr_sc->ciss_command +		\
242 				((cr)->cr_tag * CISS_COMMAND_ALLOC_SIZE))
243 #define CISS_FIND_COMMANDPHYS(cr)	((cr)->cr_sc->ciss_command_phys + \
244 					 ((cr)->cr_tag * CISS_COMMAND_ALLOC_SIZE))
245 
246 /************************************************************************
247  * Debugging/diagnostic output.
248  */
249 
250 /*
251  * Debugging levels:
252  *  0 - quiet, only emit warnings
253  *  1 - talkative, log major events, but nothing on the I/O path
254  *  2 - noisy, log events on the I/O path
255  *  3 - extremely noisy, log items in loops
256  */
257 #ifdef CISS_DEBUG
258 # define debug(level, fmt, args...)							\
259 	do {										\
260 	    if (level <= CISS_DEBUG) printf("%s: " fmt "\n", __func__ , ##args);	\
261 	} while(0)
262 # define debug_called(level)						\
263 	do {								\
264 	    if (level <= CISS_DEBUG) printf("%s: called\n", __func__);	\
265 	} while(0)
266 # define debug_struct(s)		printf("  SIZE %s: %d\n", #s, sizeof(struct s))
267 # define debug_union(s)			printf("  SIZE %s: %d\n", #s, sizeof(union s))
268 # define debug_type(s)			printf("  SIZE %s: %d\n", #s, sizeof(s))
269 # define debug_field(s, f)		printf("  OFFSET %s.%s: %d\n", #s, #f, ((int)&(((struct s *)0)->f)))
270 # define debug_const(c)			printf("  CONST %s %d/0x%x\n", #c, c, c);
271 #else
272 # define debug(level, fmt, args...)
273 # define debug_called(level)
274 # define debug_struct(s)
275 # define debug_union(s)
276 # define debug_type(s)
277 # define debug_field(s, f)
278 # define debug_const(c)
279 #endif
280 
281 #define ciss_printf(sc, fmt, args...)	device_printf(sc->ciss_dev, fmt , ##args)
282 
283 /************************************************************************
284  * Queue primitives
285  */
286 
287 #define CISSQ_ADD(sc, qname)					\
288 	do {							\
289 	    struct ciss_qstat *qs = &(sc)->ciss_qstat[qname];	\
290 								\
291 	    qs->q_length++;					\
292 	    if (qs->q_length > qs->q_max)			\
293 		qs->q_max = qs->q_length;			\
294 	} while(0)
295 
296 #define CISSQ_REMOVE(sc, qname)    (sc)->ciss_qstat[qname].q_length--
297 #define CISSQ_INIT(sc, qname)			\
298 	do {					\
299 	    sc->ciss_qstat[qname].q_length = 0;	\
300 	    sc->ciss_qstat[qname].q_max = 0;	\
301 	} while(0)
302 
303 
304 #define CISSQ_REQUEST_QUEUE(name, index)				\
305 static __inline void							\
306 ciss_initq_ ## name (struct ciss_softc *sc)				\
307 {									\
308     TAILQ_INIT(&sc->ciss_ ## name);					\
309     CISSQ_INIT(sc, index);						\
310 }									\
311 static __inline void							\
312 ciss_enqueue_ ## name (struct ciss_request *cr)				\
313 {									\
314     int		s;							\
315 									\
316     s = splcam();							\
317     TAILQ_INSERT_TAIL(&cr->cr_sc->ciss_ ## name, cr, cr_link);		\
318     CISSQ_ADD(cr->cr_sc, index);					\
319     cr->cr_onq = index;							\
320     splx(s);								\
321 }									\
322 static __inline void							\
323 ciss_requeue_ ## name (struct ciss_request *cr)				\
324 {									\
325     int		s;							\
326 									\
327     s = splcam();							\
328     TAILQ_INSERT_HEAD(&cr->cr_sc->ciss_ ## name, cr, cr_link);		\
329     CISSQ_ADD(cr->cr_sc, index);					\
330     cr->cr_onq = index;							\
331     splx(s);								\
332 }									\
333 static __inline struct ciss_request *					\
334 ciss_dequeue_ ## name (struct ciss_softc *sc)				\
335 {									\
336     struct ciss_request	*cr;						\
337     int			s;						\
338 									\
339     s = splcam();							\
340     if ((cr = TAILQ_FIRST(&sc->ciss_ ## name)) != NULL) {		\
341 	TAILQ_REMOVE(&sc->ciss_ ## name, cr, cr_link);			\
342 	CISSQ_REMOVE(sc, index);					\
343 	cr->cr_onq = -1;						\
344     }									\
345     splx(s);								\
346     return(cr);								\
347 }									\
348 static __inline int							\
349 ciss_remove_ ## name (struct ciss_request *cr)				\
350 {									\
351     int			s, error;					\
352 									\
353     s = splcam();							\
354     if (cr->cr_onq != index) {						\
355 	printf("request on queue %d (expected %d)\n", cr->cr_onq, index);\
356 	error = 1;							\
357     } else {								\
358 	TAILQ_REMOVE(&cr->cr_sc->ciss_ ## name, cr, cr_link);		\
359 	CISSQ_REMOVE(cr->cr_sc, index);					\
360 	cr->cr_onq = -1;						\
361 	error = 0;							\
362     }									\
363     splx(s);								\
364     return(error);							\
365 }									\
366 struct hack
367 
368 CISSQ_REQUEST_QUEUE(free, CISSQ_FREE);
369 CISSQ_REQUEST_QUEUE(busy, CISSQ_BUSY);
370 CISSQ_REQUEST_QUEUE(complete, CISSQ_COMPLETE);
371 
372 /********************************************************************************
373  * space-fill a character string
374  */
375 static __inline void
376 padstr(char *targ, const char *src, int len)
377 {
378     while (len-- > 0) {
379 	if (*src != 0) {
380 	    *targ++ = *src++;
381 	} else {
382 	    *targ++ = ' ';
383 	}
384     }
385 }
386