xref: /freebsd/sys/dev/ciss/cissvar.h (revision 830940567b49bb0c08dfaed40418999e76616909)
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 /* XXX Prep for increasing max i/o */
145 #define CISS_MAX_SG_ELEMENTS   17
146 
147 /*
148  * Per-logical-drive data.
149  */
150 struct ciss_ldrive
151 {
152     union ciss_device_address	cl_address;
153     union ciss_device_address	*cl_controller;
154     int				cl_status;
155 #define CISS_LD_NONEXISTENT	0
156 #define CISS_LD_ONLINE		1
157 #define CISS_LD_OFFLINE		2
158 
159     int				cl_update;
160 
161     struct ciss_bmic_id_ldrive	*cl_ldrive;
162     struct ciss_bmic_id_lstatus	*cl_lstatus;
163     struct ciss_ldrive_geometry	cl_geometry;
164 
165     char			cl_name[16];		/* device name */
166 };
167 
168 /*
169  * Per-physical-drive data
170  */
171 struct ciss_pdrive
172 {
173     union ciss_device_address	cp_address;
174     int				cp_online;
175 };
176 
177 #define CISS_PHYSICAL_SHIFT	5
178 #define CISS_PHYSICAL_BASE	(1 << CISS_PHYSICAL_SHIFT)
179 #define CISS_MAX_PHYSTGT	15
180 
181 #define CISS_IS_PHYSICAL(bus)	(bus >= CISS_PHYSICAL_BASE)
182 #define CISS_CAM_TO_PBUS(bus)	(bus - CISS_PHYSICAL_BASE)
183 
184 /*
185  * Per-adapter data
186  */
187 struct ciss_softc
188 {
189     /* bus connections */
190     device_t			ciss_dev;		/* bus attachment */
191     struct cdev *ciss_dev_t;		/* control device */
192 
193     struct resource		*ciss_regs_resource;	/* register interface window */
194     int				ciss_regs_rid;		/* resource ID */
195     bus_space_handle_t		ciss_regs_bhandle;	/* bus space handle */
196     bus_space_tag_t		ciss_regs_btag;		/* bus space tag */
197 
198     struct resource		*ciss_cfg_resource;	/* config struct interface window */
199     int				ciss_cfg_rid;		/* resource ID */
200     struct ciss_config_table	*ciss_cfg;		/* config table in adapter memory */
201     struct ciss_perf_config	*ciss_perf;		/* config table for the performant */
202     struct ciss_bmic_id_table	*ciss_id;		/* ID table in host memory */
203     u_int32_t			ciss_heartbeat;		/* last heartbeat value */
204     int				ciss_heart_attack;	/* number of times we have seen this value */
205 
206     int				ciss_msi;
207     struct resource		*ciss_irq_resource;	/* interrupt */
208     int				ciss_irq_rid[CISS_MSI_COUNT];		/* resource ID */
209     void			*ciss_intr;		/* interrupt handle */
210 
211     bus_dma_tag_t		ciss_parent_dmat;	/* parent DMA tag */
212     bus_dma_tag_t		ciss_buffer_dmat;	/* data buffer/command DMA tag */
213 
214     u_int32_t			ciss_interrupt_mask;	/* controller interrupt mask bits */
215 
216     uint64_t			*ciss_reply;
217     int				ciss_cycle;
218     int				ciss_rqidx;
219     bus_dma_tag_t		ciss_reply_dmat;
220     bus_dmamap_t		ciss_reply_map;
221     uint32_t			ciss_reply_phys;
222 
223     int				ciss_max_requests;
224     struct ciss_request		ciss_request[CISS_MAX_REQUESTS];	/* requests */
225     void			*ciss_command;		/* command structures */
226     bus_dma_tag_t		ciss_command_dmat;	/* command DMA tag */
227     bus_dmamap_t		ciss_command_map;	/* command DMA map */
228     u_int32_t			ciss_command_phys;	/* command array base address */
229     cr_qhead_t			ciss_free;		/* requests available for reuse */
230     cr_qhead_t			ciss_notify;		/* requests which are defered for processing */
231     struct proc			*ciss_notify_thread;
232 
233     struct callout		ciss_periodic;		/* periodic event handling */
234     struct ciss_request		*ciss_periodic_notify;	/* notify callback request */
235 
236     struct mtx			ciss_mtx;
237     struct ciss_ldrive		**ciss_logical;
238     struct ciss_pdrive		**ciss_physical;
239     union ciss_device_address	*ciss_controllers;	/* controller address */
240     int				ciss_max_bus_number;	/* maximum bus number */
241     int				ciss_max_logical_bus;
242     int				ciss_max_physical_bus;
243 
244     struct cam_devq		*ciss_cam_devq;
245     struct cam_sim		**ciss_cam_sim;
246 
247     int				ciss_soft_reset;
248 
249     int				ciss_flags;
250 #define CISS_FLAG_NOTIFY_OK	(1<<0)		/* notify command running OK */
251 #define CISS_FLAG_CONTROL_OPEN	(1<<1)		/* control device is open */
252 #define CISS_FLAG_ABORTING	(1<<2)		/* driver is going away */
253 #define CISS_FLAG_RUNNING	(1<<3)		/* driver is running (interrupts usable) */
254 
255 #define CISS_FLAG_FAKE_SYNCH	(1<<16)		/* needs SYNCHRONISE_CACHE faked */
256 #define CISS_FLAG_BMIC_ABORT	(1<<17)		/* use BMIC command to abort Notify on Event */
257 #define CISS_FLAG_THREAD_SHUT	(1<<20)		/* shutdown the kthread */
258 
259     struct ciss_qstat		ciss_qstat[CISSQ_COUNT];	/* queue statistics */
260 };
261 
262 /*
263  * Given a request tag, find the corresponding command in virtual or
264  * physical space.
265  *
266  * The arithmetic here is due to the allocation of ciss_command structures
267  * inside CISS_COMMAND_ALLOC_SIZE blocks.  See the comment at the definition
268  * of CISS_COMMAND_ALLOC_SIZE above.
269  */
270 #define CISS_FIND_COMMAND(cr)							\
271 	(struct ciss_command *)((u_int8_t *)(cr)->cr_sc->ciss_command +		\
272 				((cr)->cr_tag * CISS_COMMAND_ALLOC_SIZE))
273 #define CISS_FIND_COMMANDPHYS(cr)	((cr)->cr_sc->ciss_command_phys + \
274 					 ((cr)->cr_tag * CISS_COMMAND_ALLOC_SIZE))
275 
276 /************************************************************************
277  * Debugging/diagnostic output.
278  */
279 
280 /*
281  * Debugging levels:
282  *  0 - quiet, only emit warnings
283  *  1 - talkative, log major events, but nothing on the I/O path
284  *  2 - noisy, log events on the I/O path
285  *  3 - extremely noisy, log items in loops
286  */
287 #ifdef CISS_DEBUG
288 # define debug(level, fmt, args...)							\
289 	do {										\
290 	    if (level <= CISS_DEBUG) printf("%s: " fmt "\n", __func__ , ##args);	\
291 	} while(0)
292 # define debug_called(level)						\
293 	do {								\
294 	    if (level <= CISS_DEBUG) printf("%s: called\n", __func__);	\
295 	} while(0)
296 # define debug_struct(s)		printf("  SIZE %s: %zu\n", #s, sizeof(struct s))
297 # define debug_union(s)			printf("  SIZE %s: %zu\n", #s, sizeof(union s))
298 # define debug_type(s)			printf("  SIZE %s: %zu\n", #s, sizeof(s))
299 # define debug_field(s, f)		printf("  OFFSET %s.%s: %d\n", #s, #f, ((int)&(((struct s *)0)->f)))
300 # define debug_const(c)			printf("  CONST %s %jd/0x%jx\n", #c, (intmax_t)c, (intmax_t)c);
301 #else
302 # define debug(level, fmt, args...)
303 # define debug_called(level)
304 # define debug_struct(s)
305 # define debug_union(s)
306 # define debug_type(s)
307 # define debug_field(s, f)
308 # define debug_const(c)
309 #endif
310 
311 #define ciss_printf(sc, fmt, args...)	device_printf(sc->ciss_dev, fmt , ##args)
312 
313 /************************************************************************
314  * Queue primitives
315  */
316 
317 #define CISSQ_ADD(sc, qname)					\
318 	do {							\
319 	    struct ciss_qstat *qs = &(sc)->ciss_qstat[qname];	\
320 								\
321 	    qs->q_length++;					\
322 	    if (qs->q_length > qs->q_max)			\
323 		qs->q_max = qs->q_length;			\
324 	} while(0)
325 
326 #define CISSQ_REMOVE(sc, qname)    (sc)->ciss_qstat[qname].q_length--
327 #define CISSQ_INIT(sc, qname)			\
328 	do {					\
329 	    sc->ciss_qstat[qname].q_length = 0;	\
330 	    sc->ciss_qstat[qname].q_max = 0;	\
331 	} while(0)
332 
333 
334 #define CISSQ_REQUEST_QUEUE(name, index)				\
335 static __inline void							\
336 ciss_initq_ ## name (struct ciss_softc *sc)				\
337 {									\
338     STAILQ_INIT(&sc->ciss_ ## name);					\
339     CISSQ_INIT(sc, index);						\
340 }									\
341 static __inline void							\
342 ciss_enqueue_ ## name (struct ciss_request *cr)				\
343 {									\
344 									\
345     STAILQ_INSERT_TAIL(&cr->cr_sc->ciss_ ## name, cr, cr_link);		\
346     CISSQ_ADD(cr->cr_sc, index);					\
347     cr->cr_onq = index;							\
348 }									\
349 static __inline void							\
350 ciss_requeue_ ## name (struct ciss_request *cr)				\
351 {									\
352 									\
353     STAILQ_INSERT_HEAD(&cr->cr_sc->ciss_ ## name, cr, cr_link);		\
354     CISSQ_ADD(cr->cr_sc, index);					\
355     cr->cr_onq = index;							\
356 }									\
357 static __inline struct ciss_request *					\
358 ciss_dequeue_ ## name (struct ciss_softc *sc)				\
359 {									\
360     struct ciss_request	*cr;						\
361 									\
362     if ((cr = STAILQ_FIRST(&sc->ciss_ ## name)) != NULL) {		\
363 	STAILQ_REMOVE_HEAD(&sc->ciss_ ## name, cr_link);		\
364 	CISSQ_REMOVE(sc, index);					\
365 	cr->cr_onq = -1;						\
366     }									\
367     return(cr);								\
368 }									\
369 struct hack
370 
371 CISSQ_REQUEST_QUEUE(free, CISSQ_FREE);
372 CISSQ_REQUEST_QUEUE(notify, CISSQ_NOTIFY);
373 
374 static __inline void
375 ciss_enqueue_complete(struct ciss_request *ac, cr_qhead_t *head)
376 {
377 
378     STAILQ_INSERT_TAIL(head, ac, cr_link);
379 }
380 
381 static __inline struct ciss_request *
382 ciss_dequeue_complete(struct ciss_softc *sc, cr_qhead_t *head)
383 {
384     struct ciss_request  *ac;
385 
386     if ((ac = STAILQ_FIRST(head)) != NULL)
387         STAILQ_REMOVE_HEAD(head, cr_link);
388     return(ac);
389 }
390 
391 /********************************************************************************
392  * space-fill a character string
393  */
394 static __inline void
395 padstr(char *targ, const char *src, int len)
396 {
397     while (len-- > 0) {
398 	if (*src != 0) {
399 	    *targ++ = *src++;
400 	} else {
401 	    *targ++ = ' ';
402 	}
403     }
404 }
405 
406 #define ciss_report_request(a, b, c)	\
407 	_ciss_report_request(a, b, c, __FUNCTION__)
408