Merge from vendor branch OPENPAM:
[dragonfly.git] / sys / dev / raid / twe / twevar.h
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  *      $FreeBSD: src/sys/dev/twe/twevar.h,v 1.1.2.4 2002/03/07 09:57:02 msmith Exp $
28  *      $DragonFly: src/sys/dev/raid/twe/twevar.h,v 1.4 2005/06/10 17:10:26 swildner Exp $
29  */
30
31 #ifdef TWE_DEBUG
32 #define debug(level, fmt, args...)                                                      \
33         do {                                                                            \
34             if (level <= TWE_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \
35         } while(0)
36 #define debug_called(level)                                             \
37         do {                                                            \
38             if (level <= TWE_DEBUG) printf("%s: called\n", __func__);   \
39         } while(0)
40 #else
41 #define debug(level, fmt, args...)
42 #define debug_called(level)
43 #endif
44
45 /*
46  * Structure describing a logical unit as attached to the controller
47  */
48 struct twe_drive
49 {
50     /* unit properties */
51     u_int32_t           td_size;
52     int                 td_cylinders;
53     int                 td_heads;
54     int                 td_sectors;
55     int                 td_unit;
56
57     /* unit state and type */
58     u_int8_t            td_state;
59     u_int8_t            td_type;
60
61     /* handle for attached driver */
62     device_t            td_disk;
63 };
64
65 /*
66  * Per-command control structure.
67  *
68  * Note that due to alignment constraints on the tc_command field, this *must* be 64-byte aligned.
69  * We achieve this by placing the command at the beginning of the structure, and using malloc()
70  * to allocate each structure.
71  */
72 struct twe_request
73 {
74     /* controller command */
75     TWE_Command                 tr_command;     /* command as submitted to controller */
76
77     /* command payload */
78     void                        *tr_data;       /* data buffer */
79     void                        *tr_realdata;   /* copy of real data buffer pointer for alignment fixup */
80     size_t                      tr_length;
81
82     TAILQ_ENTRY(twe_request)    tr_link;        /* list linkage */
83     struct twe_softc            *tr_sc;         /* controller that owns us */
84     int                         tr_status;      /* command status */
85 #define TWE_CMD_SETUP           0       /* being assembled */
86 #define TWE_CMD_BUSY            1       /* submitted to controller */
87 #define TWE_CMD_COMPLETE        2       /* completed by controller (maybe with error) */
88     int                         tr_flags;
89 #define TWE_CMD_DATAIN          (1<<0)
90 #define TWE_CMD_DATAOUT         (1<<1)
91 #define TWE_CMD_ALIGNBUF        (1<<2)  /* data in bio is misaligned, have to copy to/from private buffer */
92 #define TWE_CMD_SLEEPER         (1<<3)  /* owner is sleeping on this command */
93     void                        (* tr_complete)(struct twe_request *tr);        /* completion handler */
94     void                        *tr_private;    /* submitter-private data or wait channel */
95
96     TWE_PLATFORM_REQUEST        /* platform-specific request elements */
97 };
98
99 /*
100  * Per-controller state.
101  */
102 struct twe_softc 
103 {
104     /* controller queues and arrays */
105     TAILQ_HEAD(, twe_request)   twe_free;                       /* command structures available for reuse */
106     twe_bioq                    twe_bioq;                       /* outstanding I/O operations */
107     TAILQ_HEAD(, twe_request)   twe_ready;                      /* requests ready for the controller */
108     TAILQ_HEAD(, twe_request)   twe_busy;                       /* requests busy in the controller */
109     TAILQ_HEAD(, twe_request)   twe_complete;                   /* active commands (busy or waiting for completion) */
110     struct twe_request          *twe_lookup[TWE_Q_LENGTH];      /* commands indexed by request ID */
111     struct twe_drive    twe_drive[TWE_MAX_UNITS];               /* attached drives */
112
113     /* asynchronous event handling */
114     u_int16_t           twe_aen_queue[TWE_Q_LENGTH];    /* AENs queued for userland tool(s) */
115     int                 twe_aen_head, twe_aen_tail;     /* ringbuffer pointers for AEN queue */
116     int                 twe_wait_aen;                   /* wait-for-aen notification */
117
118     /* controller status */
119     int                 twe_state;
120 #define TWE_STATE_INTEN         (1<<0)  /* interrupts have been enabled */
121 #define TWE_STATE_SHUTDOWN      (1<<1)  /* controller is shut down */
122 #define TWE_STATE_OPEN          (1<<2)  /* control device is open */
123 #define TWE_STATE_SUSPEND       (1<<3)  /* controller is suspended */
124     int                 twe_host_id;
125     struct twe_qstat    twe_qstat[TWEQ_COUNT];  /* queue statistics */
126
127     TWE_PLATFORM_SOFTC          /* platform-specific softc elements */
128 };
129
130 /*
131  * Interface betwen driver core and platform-dependant code.
132  */
133 extern int      twe_setup(struct twe_softc *sc);                /* do early driver/controller setup */
134 extern void     twe_init(struct twe_softc *sc);                 /* init controller */
135 extern void     twe_deinit(struct twe_softc *sc);               /* stop controller */
136 extern void     twe_intr(struct twe_softc *sc);                 /* hardware interrupt signalled */
137 extern void     twe_startio(struct twe_softc *sc);
138 extern int      twe_dump_blocks(struct twe_softc *sc, int unit, /* crashdump block write */
139                                 u_int32_t lba, void *data, int nblks);
140 extern int      twe_ioctl(struct twe_softc *sc, int cmd,
141                                   void *addr);                  /* handle user request */
142 extern void     twe_describe_controller(struct twe_softc *sc);  /* print controller info */
143 extern void     twe_print_controller(struct twe_softc *sc);
144 extern void     twe_enable_interrupts(struct twe_softc *sc);    /* enable controller interrupts */
145 extern void     twe_disable_interrupts(struct twe_softc *sc);   /* disable controller interrupts */
146
147 extern void     twe_attach_drive(struct twe_softc *sc,
148                                          struct twe_drive *dr); /* attach drive when found in twe_add_unit */
149 extern void     twe_detach_drive(struct twe_softc *sc,
150                                          int unit);             /* detach drive */
151 extern void     twe_clear_pci_parity_error(struct twe_softc *sc);
152 extern void     twe_clear_pci_abort(struct twe_softc *sc);
153 extern void     twed_intr(twe_bio *bp);                         /* return bio from core */
154 extern struct twe_request *twe_allocate_request(struct twe_softc *sc);  /* allocate request structure */
155 extern void     twe_free_request(struct twe_request *tr);       /* free request structure */
156 extern void     twe_map_request(struct twe_request *tr);        /* make request visible to controller, do s/g */
157 extern void     twe_unmap_request(struct twe_request *tr);      /* cleanup after transfer, unmap */
158
159 /********************************************************************************
160  * Queue primitives
161  */
162 #define TWEQ_ADD(sc, qname)                                     \
163         do {                                                    \
164             struct twe_qstat *qs = &(sc)->twe_qstat[qname];     \
165                                                                 \
166             qs->q_length++;                                     \
167             if (qs->q_length > qs->q_max)                       \
168                 qs->q_max = qs->q_length;                       \
169         } while(0)
170
171 #define TWEQ_REMOVE(sc, qname)    (sc)->twe_qstat[qname].q_length--
172 #define TWEQ_INIT(sc, qname)                    \
173         do {                                    \
174             sc->twe_qstat[qname].q_length = 0;  \
175             sc->twe_qstat[qname].q_max = 0;     \
176         } while(0)
177
178
179 #define TWEQ_REQUEST_QUEUE(name, index)                                 \
180 static __inline void                                                    \
181 twe_initq_ ## name (struct twe_softc *sc)                               \
182 {                                                                       \
183     TAILQ_INIT(&sc->twe_ ## name);                                      \
184     TWEQ_INIT(sc, index);                                               \
185 }                                                                       \
186 static __inline void                                                    \
187 twe_enqueue_ ## name (struct twe_request *tr)                           \
188 {                                                                       \
189     crit_enter();                                                       \
190     TAILQ_INSERT_TAIL(&tr->tr_sc->twe_ ## name, tr, tr_link);           \
191     TWEQ_ADD(tr->tr_sc, index);                                         \
192     crit_exit();                                                        \
193 }                                                                       \
194 static __inline void                                                    \
195 twe_requeue_ ## name (struct twe_request *tr)                           \
196 {                                                                       \
197     crit_enter();                                                       \
198     TAILQ_INSERT_HEAD(&tr->tr_sc->twe_ ## name, tr, tr_link);           \
199     TWEQ_ADD(tr->tr_sc, index);                                         \
200     crit_exit();                                                        \
201 }                                                                       \
202 static __inline struct twe_request *                                    \
203 twe_dequeue_ ## name (struct twe_softc *sc)                             \
204 {                                                                       \
205     struct twe_request  *tr;                                            \
206                                                                         \
207     crit_enter();                                                       \
208     if ((tr = TAILQ_FIRST(&sc->twe_ ## name)) != NULL) {                \
209         TAILQ_REMOVE(&sc->twe_ ## name, tr, tr_link);                   \
210         TWEQ_REMOVE(sc, index);                                         \
211     }                                                                   \
212     crit_exit();                                                        \
213     return(tr);                                                         \
214 }                                                                       \
215 static __inline void                                                    \
216 twe_remove_ ## name (struct twe_request *tr)                            \
217 {                                                                       \
218     crit_enter();                                                       \
219     TAILQ_REMOVE(&tr->tr_sc->twe_ ## name, tr, tr_link);                \
220     TWEQ_REMOVE(tr->tr_sc, index);                                      \
221     crit_exit();                                                        \
222 }
223
224 TWEQ_REQUEST_QUEUE(free, TWEQ_FREE)
225 TWEQ_REQUEST_QUEUE(ready, TWEQ_READY)
226 TWEQ_REQUEST_QUEUE(busy, TWEQ_BUSY)
227 TWEQ_REQUEST_QUEUE(complete, TWEQ_COMPLETE)
228
229
230 /*
231  * outstanding bio queue
232  */
233 static __inline void
234 twe_initq_bio(struct twe_softc *sc)
235 {
236     TWE_BIO_QINIT(sc->twe_bioq);
237     TWEQ_INIT(sc, TWEQ_BIO);
238 }
239
240 static __inline void
241 twe_enqueue_bio(struct twe_softc *sc, twe_bio *bp)
242 {
243     crit_enter();
244     TWE_BIO_QINSERT(sc->twe_bioq, bp);
245     TWEQ_ADD(sc, TWEQ_BIO);
246     crit_exit();
247 }
248
249 static __inline twe_bio *
250 twe_dequeue_bio(struct twe_softc *sc)
251 {
252     twe_bio     *bp;
253
254     crit_enter();
255     if ((bp = TWE_BIO_QFIRST(sc->twe_bioq)) != NULL) {
256         TWE_BIO_QREMOVE(sc->twe_bioq, bp);
257         TWEQ_REMOVE(sc, TWEQ_BIO);
258     }
259     crit_exit();
260     return(bp);
261 }