Merge branch 'vendor/BIND' into bind_vendor2
[dragonfly.git] / sys / dev / raid / amr / amrvar.h
1 /*-
2  * Copyright (c) 1999,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  * Copyright (c) 2002 Eric Moore
28  * Copyright (c) 2002 LSI Logic Corporation
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  * 3. The party using or redistributing the source code and binary forms
40  *    agrees to the disclaimer below and the terms and conditions set forth
41  *    herein.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  *
55  *      $FreeBSD: src/sys/dev/amr/amrvar.h,v 1.2.2.5 2002/12/20 15:12:04 emoore Exp $
56  *      $DragonFly: src/sys/dev/raid/amr/amrvar.h,v 1.11 2008/06/10 17:20:49 dillon Exp $
57  */
58
59 #include <sys/thread2.h>
60
61 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
62 # include <sys/taskqueue.h>
63 #endif
64
65 #ifdef AMR_DEBUG
66 # define debug(level, fmt, args...)     do {if (level <= AMR_DEBUG) kprintf("%s: " fmt "\n", __func__ , ##args);} while(0)
67 # define debug_called(level)            do {if (level <= AMR_DEBUG) kprintf("%s: called\n", __func__);} while(0)
68 #else
69 # define debug(level, fmt, args...)
70 # define debug_called(level)
71 #endif
72 #define xdebug(fmt, args...)    kprintf("%s: " fmt "\n", __func__ , ##args)
73
74 /*
75  * Per-logical-drive datastructure
76  */
77 struct amr_logdrive
78 {
79     u_int32_t   al_size;
80     int         al_state;
81     int         al_properties;
82     
83     /* synthetic geometry */
84     int         al_cylinders;
85     int         al_heads;
86     int         al_sectors;
87     
88     /* driver */
89     device_t    al_disk;
90 };
91
92 /*
93  * Due to the difficulty of using the zone allocator to create a new
94  * zone from within a module, we use our own clustering to reduce 
95  * memory wastage due to allocating lots of these small structures.
96  *
97  * 16k gives us a little under 200 command structures, which should
98  * normally be plenty.  We will grab more if we need them.
99  */
100
101 #define AMR_CMD_CLUSTERSIZE     (16 * 1024)
102
103 /*
104  * Per-command control structure.
105  */
106 struct amr_command
107 {
108     TAILQ_ENTRY(amr_command)    ac_link;
109
110     struct amr_softc            *ac_sc;
111     u_int8_t                    ac_slot;
112     int                         ac_status;      /* command completion status */
113     struct amr_mailbox          ac_mailbox;
114     int                         ac_flags;
115 #define AMR_CMD_DATAIN          (1<<0)
116 #define AMR_CMD_DATAOUT         (1<<1)
117 #define AMR_CMD_CCB_DATAIN      (1<<2)
118 #define AMR_CMD_CCB_DATAOUT     (1<<3)
119 #define AMR_CMD_PRIORITY        (1<<4)
120 #define AMR_CMD_MAPPED          (1<<5)
121 #define AMR_CMD_SLEEP           (1<<6)
122 #define AMR_CMD_BUSY            (1<<7)
123
124     struct bio                  *ac_bio;
125
126     void                        *ac_data;
127     size_t                      ac_length;
128     bus_dmamap_t                ac_dmamap;
129     u_int32_t                   ac_dataphys;
130
131     void                        *ac_ccb_data;
132     size_t                      ac_ccb_length;
133     bus_dmamap_t                ac_ccb_dmamap;
134     u_int32_t                   ac_ccb_dataphys;
135
136     void                        (* ac_complete)(struct amr_command *ac);
137     void                        *ac_private;
138 };
139
140 struct amr_command_cluster
141 {
142     TAILQ_ENTRY(amr_command_cluster)    acc_link;
143     struct amr_command          acc_command[0];
144 };
145
146 #define AMR_CMD_CLUSTERCOUNT    ((AMR_CMD_CLUSTERSIZE - sizeof(struct amr_command_cluster)) /   \
147                                  sizeof(struct amr_command))
148
149 /*
150  * Per-controller-instance data
151  */
152 struct amr_softc 
153 {
154     /* bus attachments */
155     device_t                    amr_dev;
156     struct resource             *amr_reg;               /* control registers */
157     bus_space_handle_t          amr_bhandle;
158     bus_space_tag_t             amr_btag;
159     bus_dma_tag_t               amr_parent_dmat;        /* parent DMA tag */
160     bus_dma_tag_t               amr_buffer_dmat;        /* data buffer DMA tag */
161     struct resource             *amr_irq;               /* interrupt */
162     void                        *amr_intr;
163
164     /* mailbox */
165     volatile struct amr_mailbox         *amr_mailbox;
166     volatile struct amr_mailbox64       *amr_mailbox64;
167     u_int32_t                   amr_mailboxphys;
168     bus_dma_tag_t               amr_mailbox_dmat;
169     bus_dmamap_t                amr_mailbox_dmamap;
170
171     /* scatter/gather lists and their controller-visible mappings */
172     struct amr_sgentry          *amr_sgtable;           /* s/g lists */
173     u_int32_t                   amr_sgbusaddr;          /* s/g table base address in bus space */
174     bus_dma_tag_t               amr_sg_dmat;            /* s/g buffer DMA tag */
175     bus_dmamap_t                amr_sg_dmamap;          /* map for s/g buffers */
176
177     /* controller limits and features */
178     int                         amr_maxio;              /* maximum number of I/O transactions */
179     int                         amr_maxdrives;          /* max number of logical drives */
180     int                         amr_maxchan;            /* count of SCSI channels */
181     
182     /* connected logical drives */
183     struct amr_logdrive         amr_drive[AMR_MAXLD];
184
185     /* controller state */
186     int                         amr_state;
187 #define AMR_STATE_OPEN          (1<<0)
188 #define AMR_STATE_SUSPEND       (1<<1)
189 #define AMR_STATE_INTEN         (1<<2)
190 #define AMR_STATE_SHUTDOWN      (1<<3)
191
192     /* per-controller queues */
193     struct bio_queue_head       amr_bioq;               /* pending I/O with no commands */
194     TAILQ_HEAD(,amr_command)    amr_ready;              /* commands ready to be submitted */
195     struct amr_command          *amr_busycmd[AMR_MAXCMD];
196     int                         amr_busyslots;
197     TAILQ_HEAD(,amr_command)    amr_completed;
198     TAILQ_HEAD(,amr_command)    amr_freecmds;
199     TAILQ_HEAD(,amr_command_cluster)    amr_cmd_clusters;
200
201     /* CAM attachments for passthrough */
202     struct cam_sim              *amr_cam_sim[AMR_MAX_CHANNELS];
203     TAILQ_HEAD(, ccb_hdr)       amr_cam_ccbq;
204
205     /* control device */
206     cdev_t                      amr_dev_t;
207
208     /* controller type-specific support */
209     int                         amr_type;
210 #define AMR_TYPE_QUARTZ         (1<<0)
211 #define AMR_IS_QUARTZ(sc)       ((sc)->amr_type & AMR_TYPE_QUARTZ)
212 #define AMR_TYPE_40LD           (1<<1)
213 #define AMR_IS_40LD(sc)         ((sc)->amr_type & AMR_TYPE_40LD)
214     int                         (* amr_submit_command)(struct amr_softc *sc);
215     int                         (* amr_get_work)(struct amr_softc *sc, struct amr_mailbox *mbsave);
216     int                         (*amr_poll_command)(struct amr_command *ac);
217     int                         support_ext_cdb;        /* greater than 10 byte cdb support */
218
219     /* misc glue */
220     struct intr_config_hook     amr_ich;                /* wait-for-interrupts probe hook */
221     struct callout              amr_timeout;            /* periodic status check */
222 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
223     struct task                 amr_task_complete;      /* deferred-completion task */
224 #endif
225 };
226
227 /*
228  * Interface between bus connections and driver core.
229  */
230 extern int              amr_attach(struct amr_softc *sc);
231 extern void             amr_free(struct amr_softc *sc);
232 extern int              amr_flush(struct amr_softc *sc);
233 extern int              amr_done(struct amr_softc *sc);
234 extern void             amr_startio(struct amr_softc *sc);
235
236 /*
237  * Command buffer allocation.
238  */
239 extern struct amr_command       *amr_alloccmd(struct amr_softc *sc);
240 extern void                     amr_releasecmd(struct amr_command *ac);
241
242 /*
243  * CAM interface
244  */
245 extern int              amr_cam_attach(struct amr_softc *sc);
246 extern void             amr_cam_detach(struct amr_softc *sc);
247 extern int              amr_cam_command(struct amr_softc *sc, struct amr_command **acp);
248
249 /*
250  * MegaRAID logical disk driver
251  */
252 struct amrd_softc 
253 {
254     device_t            amrd_dev;
255     cdev_t              amrd_dev_t;
256     struct amr_softc    *amrd_controller;
257     struct amr_logdrive *amrd_drive;
258     struct disk         amrd_disk;
259     struct devstat      amrd_stats;
260     int                 amrd_unit;
261     int                 amrd_flags;
262 #define AMRD_OPEN       (1<<0)          /* drive is open (can't detach) */
263 };
264
265 /*
266  * Interface between driver core and disk driver (should be using a bus?)
267  */
268 extern int      amr_submit_bio(struct amr_softc *sc, struct bio *bio);
269 extern void     amrd_intr(struct bio *bio);
270 extern int      amr_dump_blocks(struct amr_softc *sc, int unit, u_int64_t lba, void *data, int blks);
271
272 /********************************************************************************
273  * Enqueue/dequeue functions
274  */
275 static __inline void
276 amr_enqueue_bio(struct amr_softc *sc, struct bio *bio)
277 {
278     crit_enter();
279     bioqdisksort(&sc->amr_bioq, bio);
280     crit_exit();
281 }
282
283 static __inline struct bio *
284 amr_dequeue_bio(struct amr_softc *sc)
285 {
286     struct bio  *bio;
287
288     crit_enter();
289     if ((bio = bioq_first(&sc->amr_bioq)) != NULL)
290         bioq_remove(&sc->amr_bioq, bio);
291     crit_exit();
292     return(bio);
293 }
294
295 static __inline void
296 amr_enqueue_ready(struct amr_command *ac)
297 {
298     crit_enter();
299     TAILQ_INSERT_TAIL(&ac->ac_sc->amr_ready, ac, ac_link);
300     crit_exit();
301 }
302
303 static __inline void
304 amr_requeue_ready(struct amr_command *ac)
305 {
306     crit_enter();
307     TAILQ_INSERT_HEAD(&ac->ac_sc->amr_ready, ac, ac_link);
308     crit_exit();
309 }
310
311 static __inline struct amr_command *
312 amr_dequeue_ready(struct amr_softc *sc)
313 {
314     struct amr_command  *ac;
315
316     crit_enter();
317     if ((ac = TAILQ_FIRST(&sc->amr_ready)) != NULL)
318         TAILQ_REMOVE(&sc->amr_ready, ac, ac_link);
319     crit_exit();
320     return(ac);
321 }
322
323 static __inline void
324 amr_enqueue_completed(struct amr_command *ac)
325 {
326     crit_enter();
327     TAILQ_INSERT_TAIL(&ac->ac_sc->amr_completed, ac, ac_link);
328     crit_exit();
329 }
330
331 static __inline struct amr_command *
332 amr_dequeue_completed(struct amr_softc *sc)
333 {
334     struct amr_command  *ac;
335
336     crit_enter();
337     if ((ac = TAILQ_FIRST(&sc->amr_completed)) != NULL)
338         TAILQ_REMOVE(&sc->amr_completed, ac, ac_link);
339     crit_exit();
340     return(ac);
341 }
342
343 static __inline void
344 amr_enqueue_free(struct amr_command *ac)
345 {
346     crit_enter();
347     TAILQ_INSERT_TAIL(&ac->ac_sc->amr_freecmds, ac, ac_link);
348     crit_exit();
349 }
350
351 static __inline struct amr_command *
352 amr_dequeue_free(struct amr_softc *sc)
353 {
354     struct amr_command  *ac;
355
356     crit_enter();
357     if ((ac = TAILQ_FIRST(&sc->amr_freecmds)) != NULL)
358         TAILQ_REMOVE(&sc->amr_freecmds, ac, ac_link);
359     crit_exit();
360     return(ac);
361 }