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