Merge branch 'vendor/TNFTP'
[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  *
56  *      $FreeBSD: src/sys/dev/amr/amrvar.h,v 1.39 2012/08/31 09:42:46 scottl Exp $
57  */
58
59 #include "opt_amr.h"
60
61 #include <sys/buf2.h>
62 #include <sys/devicestat.h>
63 #include <sys/disk.h>
64 #include <sys/lock.h>
65 #include <sys/sysctl.h>
66
67 #define LSI_DESC_PCI "LSILogic MegaRAID 1.53"
68
69 #ifdef AMR_DEBUG
70 # define debug(level, fmt, args...)     do {if (level <= AMR_DEBUG) kprintf("%s: " fmt "\n", __func__ , ##args);} while(0)
71 # define debug_called(level)            do {if (level <= AMR_DEBUG) kprintf("%s: called\n", __func__);} while(0)
72 #else
73 # define debug(level, fmt, args...)     do {} while (0)
74 # define debug_called(level)            do {} while (0)
75 #endif
76 #define xdebug(fmt, args...)    printf("%s: " fmt "\n", __func__ , ##args)
77
78 /*
79  * Per-logical-drive datastructure
80  */
81 struct amr_logdrive
82 {
83     u_int32_t   al_size;
84     int         al_state;
85     int         al_properties;
86     
87     /* synthetic geometry */
88     int         al_cylinders;
89     int         al_heads;
90     int         al_sectors;
91     
92     /* driver */
93     device_t    al_disk;
94 };
95
96 /*
97  * Due to the difficulty of using the zone allocator to create a new
98  * zone from within a module, we use our own clustering to reduce 
99  * memory wastage due to allocating lots of these small structures.
100  *
101  * 16k gives us a little under 200 command structures, which should
102  * normally be plenty.  We will grab more if we need them.
103  */
104
105 #define AMR_CMD_CLUSTERSIZE     (16 * 1024)
106
107 typedef STAILQ_HEAD(, amr_command)      ac_qhead_t;
108 typedef STAILQ_ENTRY(amr_command)       ac_link_t;
109
110 union amr_ccb {
111     struct amr_passthrough      ccb_pthru;
112     struct amr_ext_passthrough  ccb_epthru;
113     uint8_t                     bytes[128];
114 };
115
116 /*
117  * Per-command control structure.
118  */
119 struct amr_command
120 {
121     ac_link_t                   ac_link;
122
123     struct amr_softc            *ac_sc;
124     u_int8_t                    ac_slot;
125     int                         ac_status;      /* command completion status */
126     union {
127         struct amr_sgentry      *sg32;
128         struct amr_sg64entry    *sg64;
129     } ac_sg;
130     u_int32_t                   ac_sgbusaddr;
131     u_int32_t                   ac_sg64_lo;
132     u_int32_t                   ac_sg64_hi;
133     struct amr_mailbox          ac_mailbox;
134     int                         ac_flags;
135 #define AMR_CMD_DATAIN          (1<<0)
136 #define AMR_CMD_DATAOUT         (1<<1)
137 #define AMR_CMD_CCB             (1<<2)
138 #define AMR_CMD_PRIORITY        (1<<4)
139 #define AMR_CMD_MAPPED          (1<<5)
140 #define AMR_CMD_SLEEP           (1<<6)
141 #define AMR_CMD_BUSY            (1<<7)
142 #define AMR_CMD_SG64            (1<<8)
143 #define AC_IS_SG64(ac)          ((ac)->ac_flags & AMR_CMD_SG64)
144     u_int                       ac_retries;
145
146     struct bio                  *ac_bio;
147     void                        (* ac_complete)(struct amr_command *ac);
148     void                        *ac_private;
149
150     void                        *ac_data;
151     size_t                      ac_length;
152     bus_dmamap_t                ac_dmamap;
153     bus_dmamap_t                ac_dma64map;
154
155     bus_dma_tag_t               ac_tag;
156     bus_dmamap_t                ac_datamap;
157     int                         ac_nsegments;
158     uint32_t                    ac_mb_physaddr;
159
160     union amr_ccb               *ac_ccb;
161     uint32_t                    ac_ccb_busaddr;
162 };
163
164 struct amr_command_cluster
165 {
166     TAILQ_ENTRY(amr_command_cluster)    acc_link;
167     struct amr_command          acc_command[0];
168 };
169
170 #define AMR_CMD_CLUSTERCOUNT    ((AMR_CMD_CLUSTERSIZE - sizeof(struct amr_command_cluster)) /   \
171                                  sizeof(struct amr_command))
172
173 /*
174  * Per-controller-instance data
175  */
176 struct amr_softc 
177 {
178     /* bus attachments */
179     device_t                    amr_dev;
180     struct resource             *amr_reg;               /* control registers */
181     bus_space_handle_t          amr_bhandle;
182     bus_space_tag_t             amr_btag;
183     bus_dma_tag_t               amr_parent_dmat;        /* parent DMA tag */
184     bus_dma_tag_t               amr_buffer_dmat;        /* data buffer DMA tag */
185     bus_dma_tag_t               amr_buffer64_dmat;
186     struct resource             *amr_irq;               /* interrupt */
187     void                        *amr_intr;
188
189     /* mailbox */
190     volatile struct amr_mailbox         *amr_mailbox;
191     volatile struct amr_mailbox64       *amr_mailbox64;
192     u_int32_t                   amr_mailboxphys;
193     bus_dma_tag_t               amr_mailbox_dmat;
194     bus_dmamap_t                amr_mailbox_dmamap;
195
196     /* scatter/gather lists and their controller-visible mappings */
197     struct amr_sgentry          *amr_sgtable;           /* s/g lists */
198     struct amr_sg64entry        *amr_sg64table;         /* 64bit s/g lists */
199     u_int32_t                   amr_sgbusaddr;          /* s/g table base address in bus space */
200     bus_dma_tag_t               amr_sg_dmat;            /* s/g buffer DMA tag */
201     bus_dmamap_t                amr_sg_dmamap;          /* map for s/g buffers */
202
203     union amr_ccb               *amr_ccb;
204     uint32_t                    amr_ccb_busaddr;
205     bus_dma_tag_t               amr_ccb_dmat;
206     bus_dmamap_t                amr_ccb_dmamap;
207
208     /* controller limits and features */
209     int                         amr_nextslot;           /* Next slot to use for newly allocated commands */
210     int                         amr_maxio;              /* maximum number of I/O transactions */
211     int                         amr_maxdrives;          /* max number of logical drives */
212     int                         amr_maxchan;            /* count of SCSI channels */
213     
214     /* connected logical drives */
215     struct amr_logdrive         amr_drive[AMR_MAXLD];
216
217     /* controller state */
218     int                         amr_state;
219 #define AMR_STATE_OPEN          (1<<0)
220 #define AMR_STATE_SUSPEND       (1<<1)
221 #define AMR_STATE_INTEN         (1<<2)
222 #define AMR_STATE_SHUTDOWN      (1<<3)
223 #define AMR_STATE_CRASHDUMP     (1<<4)
224 #define AMR_STATE_QUEUE_FRZN    (1<<5)
225 #define AMR_STATE_LD_DELETE     (1<<6)
226 #define AMR_STATE_REMAP_LD      (1<<7)
227
228     /* per-controller queues */
229     struct bio_queue_head       amr_bioq;               /* pending I/O with no commands */
230     ac_qhead_t                  amr_ready;              /* commands ready to be submitted */
231     struct amr_command          *amr_busycmd[AMR_MAXCMD];
232     int                         amr_busyslots;
233     ac_qhead_t                  amr_freecmds;
234     TAILQ_HEAD(,amr_command_cluster)    amr_cmd_clusters;
235
236     /* CAM attachments for passthrough */
237     struct cam_sim              *amr_cam_sim[AMR_MAX_CHANNELS];
238     TAILQ_HEAD(, ccb_hdr)       amr_cam_ccbq;
239     struct cam_devq             *amr_cam_devq;
240
241     /* control device */
242     struct cdev                 *amr_dev_t;
243     struct lock                 amr_list_lock;
244
245     struct sysctl_ctx_list      amr_sysctl_ctx;
246     struct sysctl_oid           *amr_sysctl_tree;
247
248     /* controller type-specific support */
249     int                         amr_type;
250 #define AMR_TYPE_QUARTZ         (1<<0)
251 #define AMR_IS_QUARTZ(sc)       ((sc)->amr_type & AMR_TYPE_QUARTZ)
252 #define AMR_TYPE_40LD           (1<<1)
253 #define AMR_IS_40LD(sc)         ((sc)->amr_type & AMR_TYPE_40LD)
254 #define AMR_TYPE_SG64           (1<<2)
255 #define AMR_IS_SG64(sc)         ((sc)->amr_type & AMR_TYPE_SG64)
256     int                         (* amr_submit_command)(struct amr_command *ac);
257     int                         (* amr_get_work)(struct amr_softc *sc, struct amr_mailbox *mbsave);
258     int                         (*amr_poll_command)(struct amr_command *ac);
259     int                         (*amr_poll_command1)(struct amr_softc *sc, struct amr_command *ac);
260     int                         support_ext_cdb;        /* greater than 10 byte cdb support */
261
262     /* misc glue */
263     device_t                    amr_pass;
264     int                         (*amr_cam_command)(struct amr_softc *sc, struct amr_command **acp);
265     struct intr_config_hook     amr_ich;                /* wait-for-interrupts probe hook */
266     int                         amr_allow_vol_config;
267     int                         amr_linux_no_adapters;
268     int                         amr_ld_del_supported;
269     struct lock                 amr_hw_lock;
270 };
271
272 /*
273  * Interface between bus connections and driver core.
274  */
275 extern int              amr_attach(struct amr_softc *sc);
276 extern void             amr_free(struct amr_softc *sc);
277 extern int              amr_flush(struct amr_softc *sc);
278 extern int              amr_done(struct amr_softc *sc);
279 extern void             amr_startio(struct amr_softc *sc);
280
281 /*
282  * Command buffer allocation.
283  */
284 extern struct amr_command       *amr_alloccmd(struct amr_softc *sc);
285 extern void                     amr_releasecmd(struct amr_command *ac);
286
287 /*
288  * MegaRAID logical disk driver
289  */
290 struct amrd_softc 
291 {
292     device_t            amrd_dev;
293     cdev_t              amrd_dev_t;
294     struct amr_softc    *amrd_controller;
295     struct amr_logdrive *amrd_drive;
296     struct disk         amrd_disk;
297     struct devstat      amrd_stats;
298     int                 amrd_unit;
299 };
300
301 /*
302  * Interface between driver core and disk driver (should be using a bus?)
303  */
304 extern int      amr_submit_bio(struct amr_softc *sc, struct bio *bio);
305 extern int      amr_dump_blocks(struct amr_softc *sc, int unit, u_int32_t lba, void *data, int blks);
306 extern void     amrd_intr(void *data);
307
308 /********************************************************************************
309  * Enqueue/dequeue functions
310  */
311 static __inline void
312 amr_enqueue_bio(struct amr_softc *sc, struct bio *bio)
313 {
314
315     bioq_insert_tail(&sc->amr_bioq, bio);
316 }
317
318 static __inline struct bio *
319 amr_dequeue_bio(struct amr_softc *sc)
320 {
321     struct bio  *bio;
322
323     if ((bio = bioq_first(&sc->amr_bioq)) != NULL)
324         bioq_remove(&sc->amr_bioq, bio);
325     return(bio);
326 }
327
328 static __inline void
329 amr_init_qhead(ac_qhead_t *head)
330 {
331
332         STAILQ_INIT(head);
333 }
334
335 static __inline void
336 amr_enqueue_ready(struct amr_command *ac)
337 {
338
339     STAILQ_INSERT_TAIL(&ac->ac_sc->amr_ready, ac, ac_link);
340 }
341
342 static __inline void
343 amr_requeue_ready(struct amr_command *ac)
344 {
345
346     STAILQ_INSERT_HEAD(&ac->ac_sc->amr_ready, ac, ac_link);
347 }
348
349 static __inline struct amr_command *
350 amr_dequeue_ready(struct amr_softc *sc)
351 {
352     struct amr_command  *ac;
353
354     if ((ac = STAILQ_FIRST(&sc->amr_ready)) != NULL)
355         STAILQ_REMOVE_HEAD(&sc->amr_ready, ac_link);
356     return(ac);
357 }
358
359 static __inline void
360 amr_enqueue_completed(struct amr_command *ac, ac_qhead_t *head)
361 {
362
363     STAILQ_INSERT_TAIL(head, ac, ac_link);
364 }
365
366 static __inline struct amr_command *
367 amr_dequeue_completed(struct amr_softc *sc, ac_qhead_t *head)
368 {
369     struct amr_command  *ac;
370
371     if ((ac = STAILQ_FIRST(head)) != NULL)
372         STAILQ_REMOVE_HEAD(head, ac_link);
373     return(ac);
374 }
375
376 static __inline void
377 amr_enqueue_free(struct amr_command *ac)
378 {
379
380     STAILQ_INSERT_HEAD(&ac->ac_sc->amr_freecmds, ac, ac_link);
381 }
382
383 static __inline struct amr_command *
384 amr_dequeue_free(struct amr_softc *sc)
385 {
386     struct amr_command  *ac;
387
388     if ((ac = STAILQ_FIRST(&sc->amr_freecmds)) != NULL)
389         STAILQ_REMOVE_HEAD(&sc->amr_freecmds, ac_link);
390     return(ac);
391 }