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