xref: /freebsd/sys/dev/ciss/cissvar.h (revision 076b94438c7d42c1b4661ed1e12e3b12ca69361a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2001 Michael Smith
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *	$FreeBSD$
29  */
30 
31 /*
32  * CISS adapter driver datastructures
33  */
34 
35 typedef STAILQ_HEAD(, ciss_request)	cr_qhead_t;
36 
37 /************************************************************************
38  * Tunable parameters
39  */
40 
41 /*
42  * There is no guaranteed upper bound on the number of concurrent
43  * commands an adapter may claim to support.  Cap it at a reasonable
44  * value.
45  */
46 #define CISS_MAX_REQUESTS	1024
47 
48 /*
49  * Maximum number of logical drives we support.
50  * If the controller does not indicate a maximum
51  * value.  This is a compatibiliy value to support
52  * older ciss controllers (e.g. model 6i)
53  */
54 #define CISS_MAX_LOGICAL	16
55 
56 /*
57  * Maximum number of physical devices we support.
58  */
59 #define CISS_MAX_PHYSICAL	1024
60 
61 /*
62  * Interrupt reduction can be controlled by tuning the interrupt
63  * coalesce delay and count parameters.  The delay (in microseconds)
64  * defers delivery of interrupts to increase the chance of there being
65  * more than one completed command ready when the interrupt is
66  * delivered.  The count expedites the delivery of the interrupt when
67  * the given number of commands are ready.
68  *
69  * If the delay is set to 0, interrupts are delivered immediately.
70  */
71 #define CISS_INTERRUPT_COALESCE_DELAY	0
72 #define CISS_INTERRUPT_COALESCE_COUNT	16
73 
74 /*
75  * Heartbeat routine timeout in seconds.  Note that since event
76  * handling is performed on a callback basis, we don't need this to
77  * run very often.
78  */
79 #define CISS_HEARTBEAT_RATE		10
80 
81 /************************************************************************
82  * Driver version.  Only really significant to the ACU interface.
83  */
84 #define CISS_DRIVER_VERSION	20011201
85 
86 /************************************************************************
87  * Driver data structures
88  */
89 
90 /*
91  * Each command issued to the adapter is managed by a request
92  * structure.
93  */
94 struct ciss_request
95 {
96     STAILQ_ENTRY(ciss_request)	cr_link;
97     int				cr_onq;		/* which queue we are on */
98 
99     struct ciss_softc		*cr_sc;		/* controller softc */
100     void			*cr_data;	/* data buffer */
101     u_int32_t			cr_length;	/* data length */
102     bus_dmamap_t		cr_datamap;	/* DMA map for data */
103     struct ciss_command		*cr_cc;
104     uint32_t			cr_ccphys;
105     int				cr_tag;
106     int				cr_flags;
107 #define CISS_REQ_MAPPED		(1<<0)		/* data mapped */
108 #define CISS_REQ_SLEEP		(1<<1)		/* submitter sleeping */
109 #define CISS_REQ_POLL		(1<<2)		/* submitter polling */
110 #define CISS_REQ_DATAOUT	(1<<3)		/* data host->adapter */
111 #define CISS_REQ_DATAIN		(1<<4)		/* data adapter->host */
112 #define CISS_REQ_BUSY		(1<<5)		/* controller has req */
113 #define CISS_REQ_CCB		(1<<6)		/* data is ccb */
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  * The size of the ciss_command is 52 bytes.  65 s/g elements are reserved
135  * to allow a max i/o size of 256k.  This gives a total command size of
136  * 1120 bytes, including the 32 byte alignment padding.  Modern controllers
137  * seem to saturate nicely at this value.
138  */
139 
140 #define CISS_MAX_SG_ELEMENTS	65
141 #define CISS_COMMAND_ALIGN	32
142 #define CISS_COMMAND_SG_LENGTH	(sizeof(struct ciss_sg_entry) * CISS_MAX_SG_ELEMENTS)
143 #define CISS_COMMAND_ALLOC_SIZE		(roundup2(sizeof(struct ciss_command) + CISS_COMMAND_SG_LENGTH, CISS_COMMAND_ALIGN))
144 
145 /*
146  * Per-logical-drive data.
147  */
148 struct ciss_ldrive
149 {
150     union ciss_device_address	cl_address;
151     union ciss_device_address	*cl_controller;
152     int				cl_status;
153 #define CISS_LD_NONEXISTENT	0
154 #define CISS_LD_ONLINE		1
155 #define CISS_LD_OFFLINE		2
156 
157     int				cl_update;
158 
159     struct ciss_bmic_id_ldrive	*cl_ldrive;
160     struct ciss_bmic_id_lstatus	*cl_lstatus;
161     struct ciss_ldrive_geometry	cl_geometry;
162 
163     char			cl_name[16];		/* device name */
164 };
165 
166 /*
167  * Per-physical-drive data
168  */
169 struct ciss_pdrive
170 {
171     union ciss_device_address	cp_address;
172     int				cp_online;
173 };
174 
175 #define CISS_PHYSICAL_SHIFT	5
176 #define CISS_PHYSICAL_BASE	(1 << CISS_PHYSICAL_SHIFT)
177 #define CISS_MAX_PHYSTGT	256
178 
179 #define CISS_IS_PHYSICAL(bus)	(bus >= CISS_PHYSICAL_BASE)
180 #define CISS_CAM_TO_PBUS(bus)	(bus - CISS_PHYSICAL_BASE)
181 
182 /*
183  * Per-adapter data
184  */
185 struct ciss_softc
186 {
187     /* bus connections */
188     device_t			ciss_dev;		/* bus attachment */
189     struct cdev *ciss_dev_t;		/* control device */
190 
191     struct resource		*ciss_regs_resource;	/* register interface window */
192     int				ciss_regs_rid;		/* resource ID */
193     bus_space_handle_t		ciss_regs_bhandle;	/* bus space handle */
194     bus_space_tag_t		ciss_regs_btag;		/* bus space tag */
195 
196     struct resource		*ciss_cfg_resource;	/* config struct interface window */
197     int				ciss_cfg_rid;		/* resource ID */
198     struct ciss_config_table	*ciss_cfg;		/* config table in adapter memory */
199     struct ciss_perf_config	*ciss_perf;		/* config table for the performant */
200     struct ciss_bmic_id_table	*ciss_id;		/* ID table in host memory */
201     u_int32_t			ciss_heartbeat;		/* last heartbeat value */
202     int				ciss_heart_attack;	/* number of times we have seen this value */
203 
204     int				ciss_msi;
205     struct resource		*ciss_irq_resource;	/* interrupt */
206     int				ciss_irq_rid[CISS_MSI_COUNT];		/* resource ID */
207     void			*ciss_intr;		/* interrupt handle */
208 
209     bus_dma_tag_t		ciss_parent_dmat;	/* parent DMA tag */
210     bus_dma_tag_t		ciss_buffer_dmat;	/* data buffer/command DMA tag */
211 
212     u_int32_t			ciss_interrupt_mask;	/* controller interrupt mask bits */
213 
214     uint64_t			*ciss_reply;
215     int				ciss_cycle;
216     int				ciss_rqidx;
217     bus_dma_tag_t		ciss_reply_dmat;
218     bus_dmamap_t		ciss_reply_map;
219     uint32_t			ciss_reply_phys;
220 
221     int				ciss_max_requests;
222     struct ciss_request		ciss_request[CISS_MAX_REQUESTS];	/* requests */
223     void			*ciss_command;		/* command structures */
224     bus_dma_tag_t		ciss_command_dmat;	/* command DMA tag */
225     bus_dmamap_t		ciss_command_map;	/* command DMA map */
226     u_int32_t			ciss_command_phys;	/* command array base address */
227     cr_qhead_t			ciss_free;		/* requests available for reuse */
228     cr_qhead_t			ciss_notify;		/* requests which are defered for processing */
229     struct proc			*ciss_notify_thread;
230 
231     struct callout		ciss_periodic;		/* periodic event handling */
232     struct ciss_request		*ciss_periodic_notify;	/* notify callback request */
233 
234     struct mtx			ciss_mtx;
235     struct ciss_ldrive		**ciss_logical;
236     struct ciss_pdrive		**ciss_physical;
237     union ciss_device_address	*ciss_controllers;	/* controller address */
238     int				ciss_max_bus_number;	/* maximum bus number */
239     int				ciss_max_logical_bus;
240     int				ciss_max_physical_bus;
241 
242     struct cam_devq		*ciss_cam_devq;
243     struct cam_sim		**ciss_cam_sim;
244 
245     int				ciss_soft_reset;
246 
247     int				ciss_flags;
248 #define CISS_FLAG_NOTIFY_OK	(1<<0)		/* notify command running OK */
249 #define CISS_FLAG_CONTROL_OPEN	(1<<1)		/* control device is open */
250 #define CISS_FLAG_ABORTING	(1<<2)		/* driver is going away */
251 #define CISS_FLAG_RUNNING	(1<<3)		/* driver is running (interrupts usable) */
252 #define CISS_FLAG_BUSY		(1<<4)		/* no free commands */
253 
254 #define CISS_FLAG_FAKE_SYNCH	(1<<16)		/* needs SYNCHRONISE_CACHE faked */
255 #define CISS_FLAG_BMIC_ABORT	(1<<17)		/* use BMIC command to abort Notify on Event */
256 #define CISS_FLAG_THREAD_SHUT	(1<<20)		/* shutdown the kthread */
257 
258     struct ciss_qstat		ciss_qstat[CISSQ_COUNT];	/* queue statistics */
259 };
260 
261 /************************************************************************
262  * Debugging/diagnostic output.
263  */
264 
265 /*
266  * Debugging levels:
267  *  0 - quiet, only emit warnings
268  *  1 - talkative, log major events, but nothing on the I/O path
269  *  2 - noisy, log events on the I/O path
270  *  3 - extremely noisy, log items in loops
271  */
272 #ifdef CISS_DEBUG
273 # define debug(level, fmt, args...)							\
274 	do {										\
275 	    if (level <= CISS_DEBUG) printf("%s: " fmt "\n", __func__ , ##args);	\
276 	} while(0)
277 # define debug_called(level)						\
278 	do {								\
279 	    if (level <= CISS_DEBUG) printf("%s: called\n", __func__);	\
280 	} while(0)
281 # define debug_struct(s)		printf("  SIZE %s: %zu\n", #s, sizeof(struct s))
282 # define debug_union(s)			printf("  SIZE %s: %zu\n", #s, sizeof(union s))
283 # define debug_type(s)			printf("  SIZE %s: %zu\n", #s, sizeof(s))
284 # define debug_field(s, f)		printf("  OFFSET %s.%s: %d\n", #s, #f, ((int)&(((struct s *)0)->f)))
285 # define debug_const(c)			printf("  CONST %s %jd/0x%jx\n", #c, (intmax_t)c, (intmax_t)c);
286 #else
287 # define debug(level, fmt, args...)
288 # define debug_called(level)
289 # define debug_struct(s)
290 # define debug_union(s)
291 # define debug_type(s)
292 # define debug_field(s, f)
293 # define debug_const(c)
294 #endif
295 
296 #define ciss_printf(sc, fmt, args...)	device_printf(sc->ciss_dev, fmt , ##args)
297 
298 /************************************************************************
299  * Queue primitives
300  */
301 
302 #define CISSQ_ADD(sc, qname)					\
303 	do {							\
304 	    struct ciss_qstat *qs = &(sc)->ciss_qstat[qname];	\
305 								\
306 	    qs->q_length++;					\
307 	    if (qs->q_length > qs->q_max)			\
308 		qs->q_max = qs->q_length;			\
309 	} while(0)
310 
311 #define CISSQ_REMOVE(sc, qname)    (sc)->ciss_qstat[qname].q_length--
312 #define CISSQ_INIT(sc, qname)			\
313 	do {					\
314 	    sc->ciss_qstat[qname].q_length = 0;	\
315 	    sc->ciss_qstat[qname].q_max = 0;	\
316 	} while(0)
317 
318 
319 #define CISSQ_REQUEST_QUEUE(name, index)				\
320 static __inline void							\
321 ciss_initq_ ## name (struct ciss_softc *sc)				\
322 {									\
323     STAILQ_INIT(&sc->ciss_ ## name);					\
324     CISSQ_INIT(sc, index);						\
325 }									\
326 static __inline void							\
327 ciss_enqueue_ ## name (struct ciss_request *cr)				\
328 {									\
329 									\
330     STAILQ_INSERT_TAIL(&cr->cr_sc->ciss_ ## name, cr, cr_link);		\
331     CISSQ_ADD(cr->cr_sc, index);					\
332     cr->cr_onq = index;							\
333 }									\
334 static __inline void							\
335 ciss_requeue_ ## name (struct ciss_request *cr)				\
336 {									\
337 									\
338     STAILQ_INSERT_HEAD(&cr->cr_sc->ciss_ ## name, cr, cr_link);		\
339     CISSQ_ADD(cr->cr_sc, index);					\
340     cr->cr_onq = index;							\
341 }									\
342 static __inline struct ciss_request *					\
343 ciss_dequeue_ ## name (struct ciss_softc *sc)				\
344 {									\
345     struct ciss_request	*cr;						\
346 									\
347     if ((cr = STAILQ_FIRST(&sc->ciss_ ## name)) != NULL) {		\
348 	STAILQ_REMOVE_HEAD(&sc->ciss_ ## name, cr_link);		\
349 	CISSQ_REMOVE(sc, index);					\
350 	cr->cr_onq = -1;						\
351     }									\
352     return(cr);								\
353 }									\
354 struct hack
355 
356 CISSQ_REQUEST_QUEUE(free, CISSQ_FREE);
357 CISSQ_REQUEST_QUEUE(notify, CISSQ_NOTIFY);
358 
359 static __inline void
360 ciss_enqueue_complete(struct ciss_request *ac, cr_qhead_t *head)
361 {
362 
363     STAILQ_INSERT_TAIL(head, ac, cr_link);
364 }
365 
366 static __inline struct ciss_request *
367 ciss_dequeue_complete(struct ciss_softc *sc, cr_qhead_t *head)
368 {
369     struct ciss_request  *ac;
370 
371     if ((ac = STAILQ_FIRST(head)) != NULL)
372         STAILQ_REMOVE_HEAD(head, cr_link);
373     return(ac);
374 }
375 
376 /********************************************************************************
377  * space-fill a character string
378  */
379 static __inline void
380 padstr(char *targ, const char *src, int len)
381 {
382     while (len-- > 0) {
383 	if (*src != 0) {
384 	    *targ++ = *src++;
385 	} else {
386 	    *targ++ = ' ';
387 	}
388     }
389 }
390 
391 #define ciss_report_request(a, b, c)	\
392 	_ciss_report_request(a, b, c, __FUNCTION__)
393