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