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