Merge branch 'vendor/LIBARCHIVE'
[dragonfly.git] / sys / dev / raid / mfi / mfivar.h
1 /*-
2  * Copyright (c) 2006 IronPort Systems
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 /*-
27  * Copyright (c) 2007 LSI Corp.
28  * Copyright (c) 2007 Rajesh Prabhakaran.
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  *
40  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  */
52 /*-
53  * Redistribution and use in source and binary forms, with or without
54  * modification, are permitted provided that the following conditions
55  * are met:
56  *
57  *            Copyright 1994-2009 The FreeBSD Project.
58  *            All rights reserved.
59  *
60  * 1. Redistributions of source code must retain the above copyright
61  *    notice, this list of conditions and the following disclaimer.
62  * 2. Redistributions in binary form must reproduce the above copyright
63  *    notice, this list of conditions and the following disclaimer in the
64  *    documentation and/or other materials provided with the distribution.
65  *
66  *    THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT``AS IS'' AND
67  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
68  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
69  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FREEBSD PROJECT OR
70  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
71  * EXEMPLARY,OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
72  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
73  * PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY THEORY
74  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
75  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
76  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
77  *
78  * The views and conclusions contained in the software and documentation
79  * are those of the authors and should not be interpreted as representing
80  * official policies,either expressed or implied, of the FreeBSD Project.
81  *
82  * $FreeBSD: src/sys/dev/mfi/mfivar.h,v 1.14 2011/09/29 08:37:53 mav Exp $
83  */
84
85 #ifndef _MFIVAR_H
86 #define _MFIVAR_H
87
88 #include <sys/devicestat.h>
89 #include <sys/disk.h>
90 #include <sys/eventhandler.h>
91 #include <sys/lock.h>
92 #include <sys/sysctl.h>
93
94 /*
95  * SCSI structures and definitions are used from here, but no linking
96  * requirements are made to CAM.
97  */
98 #include <bus/cam/scsi/scsi_all.h>
99
100 struct mfi_hwcomms {
101         uint32_t                hw_pi;
102         uint32_t                hw_ci;
103         uint32_t                hw_reply_q[1];
104 };
105
106 struct mfi_softc;
107 struct disk;
108 struct ccb_hdr;
109
110 struct mfi_command {
111         TAILQ_ENTRY(mfi_command) cm_link;
112         time_t                  cm_timestamp;
113         struct mfi_softc        *cm_sc;
114         union mfi_frame         *cm_frame;
115 #if defined(__x86_64__)
116         uint64_t                cm_frame_busaddr;
117 #else
118         uint32_t                cm_frame_busaddr;
119 #endif
120         struct mfi_sense        *cm_sense;
121 #if defined(__x86_64__)
122         uint64_t                cm_sense_busaddr;
123 #else
124         uint32_t                cm_sense_busaddr;
125 #endif
126         bus_dmamap_t            cm_dmamap;
127         union mfi_sgl           *cm_sg;
128         void                    *cm_data;
129         int                     cm_len;
130         int                     cm_stp_len;
131         int                     cm_total_frame_size;
132         int                     cm_extra_frames;
133         int                     cm_flags;
134 #define MFI_CMD_MAPPED          (1<<0)
135 #define MFI_CMD_DATAIN          (1<<1)
136 #define MFI_CMD_DATAOUT         (1<<2)
137 #define MFI_CMD_COMPLETED       (1<<3)
138 #define MFI_CMD_POLLED          (1<<4)
139 #define MFI_ON_MFIQ_FREE        (1<<5)
140 #define MFI_ON_MFIQ_READY       (1<<6)
141 #define MFI_ON_MFIQ_BUSY        (1<<7)
142 #define MFI_ON_MFIQ_MASK        ((1<<5)|(1<<6)|(1<<7))
143         int                     cm_aen_abort;
144         void                    (* cm_complete)(struct mfi_command *cm);
145         void                    *cm_private;
146         int                     cm_index;
147         int                     cm_error;
148 };
149
150 /*
151  * XXX swildner
152  *
153  * At the moment we rely on mfi_disk and mfi_system_pd having the same size.
154  * A future cleanup should merge them.
155  */
156 struct mfi_disk {
157         TAILQ_ENTRY(mfi_disk)   ld_link;
158         device_t        ld_dev;
159         cdev_t          ld_dev_t;
160         int             ld_id;
161         int             ld_unit;
162         struct mfi_softc *ld_controller;
163         struct mfi_ld_info      *ld_info;
164         struct disk     ld_disk;
165         struct devstat  ld_devstat;
166         int             ld_flags;
167 #define MFI_DISK_FLAGS_OPEN     0x01
168 #define MFI_DISK_FLAGS_DISABLED 0x02
169 #define MFI_DISK_FLAGS_SYSPD    0x04
170 };
171
172 struct mfi_system_pd {
173         TAILQ_ENTRY(mfi_system_pd)      pd_link;
174         device_t        pd_dev;
175         cdev_t          pd_dev_t;
176         int             pd_id;
177         int             pd_unit;
178         struct mfi_softc *pd_controller;
179         struct mfi_pd_info      *pd_info;
180         struct disk     pd_disk;
181         struct devstat  pd_devstat;
182         int             pd_flags;
183 };
184
185 struct mfi_aen {
186         TAILQ_ENTRY(mfi_aen) aen_link;
187         struct proc                     *p;
188 };
189
190 struct mfi_softc {
191         device_t                        mfi_dev;
192         int                             mfi_flags;
193 #define MFI_FLAGS_SG64          (1<<0)
194 #define MFI_FLAGS_QFRZN         (1<<1)
195 #define MFI_FLAGS_OPEN          (1<<2)
196 #define MFI_FLAGS_STOP          (1<<3)
197 #define MFI_FLAGS_1064R         (1<<4)
198 #define MFI_FLAGS_1078          (1<<5)
199 #define MFI_FLAGS_GEN2          (1<<6)
200 #define MFI_FLAGS_SKINNY        (1<<7)
201
202         struct mfi_hwcomms              *mfi_comms;
203         TAILQ_HEAD(,mfi_command)        mfi_free;
204         TAILQ_HEAD(,mfi_command)        mfi_ready;
205         TAILQ_HEAD(,mfi_command)        mfi_busy;
206         struct bio_queue_head           mfi_bioq;
207         struct mfi_qstat                mfi_qstat[MFIQ_COUNT];
208
209         struct resource                 *mfi_regs_resource;
210         bus_space_handle_t              mfi_bhandle;
211         bus_space_tag_t                 mfi_btag;
212         int                             mfi_regs_rid;
213
214         bus_dma_tag_t                   mfi_parent_dmat;
215         bus_dma_tag_t                   mfi_buffer_dmat;
216
217         bus_dma_tag_t                   mfi_comms_dmat;
218         bus_dmamap_t                    mfi_comms_dmamap;
219         uint32_t                        mfi_comms_busaddr;
220
221         bus_dma_tag_t                   mfi_frames_dmat;
222         bus_dmamap_t                    mfi_frames_dmamap;
223         uint32_t                        mfi_frames_busaddr;
224         union mfi_frame                 *mfi_frames;
225
226         TAILQ_HEAD(,mfi_aen)            mfi_aen_pids;
227         struct mfi_command              *mfi_aen_cm;
228         struct mfi_command              *mfi_skinny_cm;
229         uint32_t                        mfi_aen_triggered;
230         uint32_t                        mfi_poll_waiting;
231         struct kqinfo                   mfi_kq;
232         int                             mfi_delete_busy_volumes;
233         int                             mfi_keep_deleted_volumes;
234         int                             mfi_detaching;
235
236         bus_dma_tag_t                   mfi_sense_dmat;
237         bus_dmamap_t                    mfi_sense_dmamap;
238         uint32_t                        mfi_sense_busaddr;
239         struct mfi_sense                *mfi_sense;
240
241         struct resource                 *mfi_irq;
242         void                            *mfi_intr;
243         int                             mfi_irq_rid;
244         int                             mfi_irq_type;
245
246         struct intr_config_hook         mfi_ich;
247         eventhandler_tag                eh;
248
249         /*
250          * Allocation for the command array.  Used as an indexable array to
251          * recover completed commands.
252          */
253         struct mfi_command              *mfi_commands;
254         /*
255          * How many commands were actually allocated
256          */
257         int                             mfi_total_cmds;
258         /*
259          * How many commands the firmware can handle.  Also how big the reply
260          * queue is, minus 1.
261          */
262         int                             mfi_max_fw_cmds;
263         /*
264          * How many S/G elements we'll ever actually use
265          */
266         int                             mfi_max_sge;
267         /*
268          * How many bytes a compound frame is, including all of the extra frames
269          * that are used for S/G elements.
270          */
271         int                             mfi_cmd_size;
272         /*
273          * How large an S/G element is.  Used to calculate the number of single
274          * frames in a command.
275          */
276         int                             mfi_sge_size;
277         /*
278          * Max number of sectors that the firmware allows
279          */
280         uint32_t                        mfi_max_io;
281
282         TAILQ_HEAD(,mfi_disk)           mfi_ld_tqh;
283         TAILQ_HEAD(,mfi_system_pd)      mfi_syspd_tqh;
284         eventhandler_tag                mfi_eh;
285         struct cdev                     *mfi_cdev;
286
287         TAILQ_HEAD(, ccb_hdr)           mfi_cam_ccbq;
288         struct mfi_command *            (* mfi_cam_start)(void *);
289         struct callout                  mfi_watchdog_callout;
290         struct lock                     mfi_io_lock;
291         struct lock                     mfi_config_lock;
292
293         struct sysctl_ctx_list          mfi_sysctl_ctx;
294         struct sysctl_oid               *mfi_sysctl_tree;
295
296         /* Controller type specific interfaces */
297         void    (*mfi_enable_intr)(struct mfi_softc *sc);
298         int32_t (*mfi_read_fw_status)(struct mfi_softc *sc);
299         int     (*mfi_check_clear_intr)(struct mfi_softc *sc);
300         void    (*mfi_issue_cmd)(struct mfi_softc *sc,uint32_t bus_add,uint32_t frame_cnt);
301 };
302
303 extern int mfi_attach(struct mfi_softc *);
304 extern void mfi_free(struct mfi_softc *);
305 extern int mfi_shutdown(struct mfi_softc *);
306 extern void mfi_startio(struct mfi_softc *);
307 extern void mfi_disk_complete(struct bio *);
308 extern int mfi_disk_disable(struct mfi_disk *);
309 extern void mfi_disk_enable(struct mfi_disk *);
310 extern int mfi_dump_blocks(struct mfi_softc *, int id, uint64_t, void *, int);
311 extern int mfi_syspd_disable(struct mfi_system_pd *);
312 extern void mfi_syspd_enable(struct mfi_system_pd *);
313 extern int mfi_dump_syspd_blocks(struct mfi_softc *, int id, uint64_t, void *, int);
314
315 #define MFIQ_ADD(sc, qname)                                     \
316         do {                                                    \
317                 struct mfi_qstat *qs;                           \
318                                                                 \
319                 qs = &(sc)->mfi_qstat[qname];                   \
320                 qs->q_length++;                                 \
321                 if (qs->q_length > qs->q_max)                   \
322                         qs->q_max = qs->q_length;               \
323         } while (0)
324
325 #define MFIQ_REMOVE(sc, qname)  (sc)->mfi_qstat[qname].q_length--
326
327 #define MFIQ_INIT(sc, qname)                                    \
328         do {                                                    \
329                 sc->mfi_qstat[qname].q_length = 0;              \
330                 sc->mfi_qstat[qname].q_max = 0;                 \
331         } while (0)
332
333 #define MFIQ_COMMAND_QUEUE(name, index)                                 \
334         static __inline void                                            \
335         mfi_initq_ ## name (struct mfi_softc *sc)                       \
336         {                                                               \
337                 TAILQ_INIT(&sc->mfi_ ## name);                          \
338                 MFIQ_INIT(sc, index);                                   \
339         }                                                               \
340         static __inline void                                            \
341         mfi_enqueue_ ## name (struct mfi_command *cm)                   \
342         {                                                               \
343                 if ((cm->cm_flags & MFI_ON_MFIQ_MASK) != 0) {           \
344                         kprintf("command %p is on another queue, "      \
345                             "flags = %#x\n", cm, cm->cm_flags);         \
346                         panic("command is on another queue");           \
347                 }                                                       \
348                 TAILQ_INSERT_TAIL(&cm->cm_sc->mfi_ ## name, cm, cm_link); \
349                 cm->cm_flags |= MFI_ON_ ## index;                       \
350                 MFIQ_ADD(cm->cm_sc, index);                             \
351         }                                                               \
352         static __inline void                                            \
353         mfi_requeue_ ## name (struct mfi_command *cm)                   \
354         {                                                               \
355                 if ((cm->cm_flags & MFI_ON_MFIQ_MASK) != 0) {           \
356                         kprintf("command %p is on another queue, "      \
357                             "flags = %#x\n", cm, cm->cm_flags);         \
358                         panic("command is on another queue");           \
359                 }                                                       \
360                 TAILQ_INSERT_HEAD(&cm->cm_sc->mfi_ ## name, cm, cm_link); \
361                 cm->cm_flags |= MFI_ON_ ## index;                       \
362                 MFIQ_ADD(cm->cm_sc, index);                             \
363         }                                                               \
364         static __inline struct mfi_command *                            \
365         mfi_dequeue_ ## name (struct mfi_softc *sc)                     \
366         {                                                               \
367                 struct mfi_command *cm;                                 \
368                                                                         \
369                 if ((cm = TAILQ_FIRST(&sc->mfi_ ## name)) != NULL) {    \
370                         if ((cm->cm_flags & MFI_ON_ ## index) == 0) {   \
371                                 kprintf("command %p not in queue, "     \
372                                     "flags = %#x, bit = %#x\n", cm,     \
373                                     cm->cm_flags, MFI_ON_ ## index);    \
374                                 panic("command not in queue");          \
375                         }                                               \
376                         TAILQ_REMOVE(&sc->mfi_ ## name, cm, cm_link);   \
377                         cm->cm_flags &= ~MFI_ON_ ## index;              \
378                         MFIQ_REMOVE(sc, index);                         \
379                 }                                                       \
380                 return (cm);                                            \
381         }                                                               \
382         static __inline void                                            \
383         mfi_remove_ ## name (struct mfi_command *cm)                    \
384         {                                                               \
385                 if ((cm->cm_flags & MFI_ON_ ## index) == 0) {           \
386                         kprintf("command %p not in queue, flags = %#x, " \
387                             "bit = %#x\n", cm, cm->cm_flags,            \
388                             MFI_ON_ ## index);                          \
389                         panic("command not in queue");                  \
390                 }                                                       \
391                 TAILQ_REMOVE(&cm->cm_sc->mfi_ ## name, cm, cm_link);    \
392                 cm->cm_flags &= ~MFI_ON_ ## index;                      \
393                 MFIQ_REMOVE(cm->cm_sc, index);                          \
394         }                                                               \
395 struct hack
396
397 MFIQ_COMMAND_QUEUE(free, MFIQ_FREE);
398 MFIQ_COMMAND_QUEUE(ready, MFIQ_READY);
399 MFIQ_COMMAND_QUEUE(busy, MFIQ_BUSY);
400
401 static __inline void
402 mfi_initq_bio(struct mfi_softc *sc)
403 {
404         bioq_init(&sc->mfi_bioq);
405         MFIQ_INIT(sc, MFIQ_BIO);
406 }
407
408 static __inline void
409 mfi_enqueue_bio(struct mfi_softc *sc, struct bio *bp)
410 {
411         bioq_insert_tail(&sc->mfi_bioq, bp);
412         MFIQ_ADD(sc, MFIQ_BIO);
413 }
414
415 static __inline struct bio *
416 mfi_dequeue_bio(struct mfi_softc *sc)
417 {
418         struct bio *bp;
419
420         if ((bp = bioq_first(&sc->mfi_bioq)) != NULL) {
421                 bioq_remove(&sc->mfi_bioq, bp);
422                 MFIQ_REMOVE(sc, MFIQ_BIO);
423         }
424         return (bp);
425 }
426
427 static __inline void
428 mfi_print_sense(struct mfi_softc *sc, void *sense)
429 {
430         int error, key, asc, ascq;
431
432         scsi_extract_sense((struct scsi_sense_data *)sense,
433             &error, &key, &asc, &ascq);
434         device_printf(sc->mfi_dev, "sense error %d, sense_key %d, "
435             "asc %d, ascq %d\n", error, key, asc, ascq);
436 }
437
438
439 #define MFI_WRITE4(sc, reg, val)        bus_space_write_4((sc)->mfi_btag, \
440         sc->mfi_bhandle, (reg), (val))
441 #define MFI_READ4(sc, reg)              bus_space_read_4((sc)->mfi_btag, \
442         (sc)->mfi_bhandle, (reg))
443 #define MFI_WRITE2(sc, reg, val)        bus_space_write_2((sc)->mfi_btag, \
444         sc->mfi_bhandle, (reg), (val))
445 #define MFI_READ2(sc, reg)              bus_space_read_2((sc)->mfi_btag, \
446         (sc)->mfi_bhandle, (reg))
447 #define MFI_WRITE1(sc, reg, val)        bus_space_write_1((sc)->mfi_btag, \
448         sc->mfi_bhandle, (reg), (val))
449 #define MFI_READ1(sc, reg)              bus_space_read_1((sc)->mfi_btag, \
450         (sc)->mfi_bhandle, (reg))
451
452 MALLOC_DECLARE(M_MFIBUF);
453
454 #define MFI_CMD_TIMEOUT 30
455 #define MFI_MAXPHYS (128 * 1024)
456 #define SKINNY_MEMORY 0x02000000
457
458 #ifdef MFI_DEBUG
459 extern void mfi_print_cmd(struct mfi_command *cm);
460 extern void mfi_dump_cmds(struct mfi_softc *sc);
461 extern void mfi_validate_sg(struct mfi_softc *, struct mfi_command *, const char *, int );
462 #define MFI_PRINT_CMD(cm)       mfi_print_cmd(cm)
463 #define MFI_DUMP_CMDS(sc)       mfi_dump_cmds(sc)
464 #define MFI_VALIDATE_CMD(sc, cm) mfi_validate_sg(sc, cm, __FUNCTION__, __LINE__)
465 #else
466 #define MFI_PRINT_CMD(cm)
467 #define MFI_DUMP_CMDS(sc)
468 #define MFI_VALIDATE_CMD(sc, cm)
469 #endif
470
471 extern void mfi_release_command(struct mfi_command *cm);
472
473 #endif /* _MFIVAR_H */