]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/dev/mfi/mfivar.h
MFC r319972:
[FreeBSD/stable/9.git] / sys / dev / 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 #ifndef _MFIVAR_H
54 #define _MFIVAR_H
55
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
58
59 #include <sys/lock.h>
60 #include <sys/sx.h>
61
62 #include <sys/types.h>
63 #include <sys/taskqueue.h>
64 #include "opt_mfi.h"
65
66 /*
67  * SCSI structures and definitions are used from here, but no linking
68  * requirements are made to CAM.
69  */
70 #include <cam/scsi/scsi_all.h>
71
72 struct mfi_hwcomms {
73         uint32_t                hw_pi;
74         uint32_t                hw_ci;
75         uint32_t                hw_reply_q[1];
76 };
77 #define MEGASAS_MAX_NAME        32
78 #define MEGASAS_VERSION         "4.23"
79
80 struct mfi_softc;
81 struct disk;
82 struct ccb_hdr;
83
84 struct mfi_command {
85         TAILQ_ENTRY(mfi_command) cm_link;
86         time_t                  cm_timestamp;
87         struct mfi_softc        *cm_sc;
88         union mfi_frame         *cm_frame;
89         bus_addr_t              cm_frame_busaddr;
90         struct mfi_sense        *cm_sense;
91         bus_addr_t              cm_sense_busaddr;
92         bus_dmamap_t            cm_dmamap;
93         union mfi_sgl           *cm_sg;
94         void                    *cm_data;
95         int                     cm_len;
96         int                     cm_stp_len;
97         int                     cm_total_frame_size;
98         int                     cm_extra_frames;
99         int                     cm_flags;
100 #define MFI_CMD_MAPPED          (1<<0)
101 #define MFI_CMD_DATAIN          (1<<1)
102 #define MFI_CMD_DATAOUT         (1<<2)
103 #define MFI_CMD_COMPLETED       (1<<3)
104 #define MFI_CMD_POLLED          (1<<4)
105 #define MFI_CMD_SCSI            (1<<5)
106 #define MFI_CMD_CCB             (1<<6)
107 #define MFI_CMD_TBOLT           (1<<7)
108 #define MFI_ON_MFIQ_FREE        (1<<8)
109 #define MFI_ON_MFIQ_READY       (1<<9)
110 #define MFI_ON_MFIQ_BUSY        (1<<10)
111 #define MFI_ON_MFIQ_MASK        (MFI_ON_MFIQ_FREE | MFI_ON_MFIQ_READY| \
112     MFI_ON_MFIQ_BUSY)
113 #define MFI_CMD_FLAGS_FMT       "\20" \
114     "\1MAPPED" \
115     "\2DATAIN" \
116     "\3DATAOUT" \
117     "\4COMPLETED" \
118     "\5POLLED" \
119     "\6SCSI" \
120     "\7TBOLT" \
121     "\10Q_FREE" \
122     "\11Q_READY" \
123     "\12Q_BUSY"
124         uint8_t                 retry_for_fw_reset;
125         void                    (* cm_complete)(struct mfi_command *cm);
126         void                    *cm_private;
127         int                     cm_index;
128         int                     cm_error;
129 };
130
131 struct mfi_disk {
132         TAILQ_ENTRY(mfi_disk)   ld_link;
133         device_t        ld_dev;
134         int             ld_id;
135         int             ld_unit;
136         struct mfi_softc *ld_controller;
137         struct mfi_ld_info      *ld_info;
138         struct disk     *ld_disk;
139         int             ld_flags;
140 #define MFI_DISK_FLAGS_OPEN     0x01
141 #define MFI_DISK_FLAGS_DISABLED 0x02
142 };
143
144 struct mfi_disk_pending {
145         TAILQ_ENTRY(mfi_disk_pending)   ld_link;
146         int             ld_id;
147 };
148
149 struct mfi_system_pd {
150         TAILQ_ENTRY(mfi_system_pd) pd_link;
151         device_t        pd_dev;
152         int             pd_id;
153         int             pd_unit;
154         struct mfi_softc *pd_controller;
155         struct mfi_pd_info *pd_info;
156         struct disk     *pd_disk;
157         int             pd_flags;
158 };
159
160 struct mfi_system_pending {
161         TAILQ_ENTRY(mfi_system_pending) pd_link;
162         int             pd_id;
163 };
164
165 struct mfi_evt_queue_elm {
166         TAILQ_ENTRY(mfi_evt_queue_elm)  link;
167         struct mfi_evt_detail           detail;
168 };
169
170 struct mfi_aen {
171         TAILQ_ENTRY(mfi_aen) aen_link;
172         struct proc                     *p;
173 };
174
175 struct mfi_skinny_dma_info {
176         bus_dma_tag_t                   dmat[514];
177         bus_dmamap_t                    dmamap[514];
178         uint32_t                        mem[514];
179         int                             noofmaps;
180 };
181
182 struct megasas_sge
183 {
184         bus_addr_t                      phys_addr;
185         uint32_t                        length;
186 };
187
188 struct mfi_cmd_tbolt;
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 #define MFI_FLAGS_TBOLT         (1<<8)
202 #define MFI_FLAGS_MRSAS         (1<<9)
203 #define MFI_FLAGS_INVADER       (1<<10)
204 #define MFI_FLAGS_FURY          (1<<11)
205         // Start: LSIP200113393
206         bus_dma_tag_t                   verbuf_h_dmat;
207         bus_dmamap_t                    verbuf_h_dmamap;
208         bus_addr_t                      verbuf_h_busaddr;
209         uint32_t                        *verbuf;
210         void                            *kbuff_arr[MAX_IOCTL_SGE];
211         bus_dma_tag_t                   mfi_kbuff_arr_dmat[2];
212         bus_dmamap_t                    mfi_kbuff_arr_dmamap[2];
213         bus_addr_t                      mfi_kbuff_arr_busaddr[2];
214
215         struct mfi_hwcomms              *mfi_comms;
216         TAILQ_HEAD(,mfi_command)        mfi_free;
217         TAILQ_HEAD(,mfi_command)        mfi_ready;
218         TAILQ_HEAD(BUSYQ,mfi_command)   mfi_busy;
219         struct bio_queue_head           mfi_bioq;
220         struct mfi_qstat                mfi_qstat[MFIQ_COUNT];
221
222         struct resource                 *mfi_regs_resource;
223         bus_space_handle_t              mfi_bhandle;
224         bus_space_tag_t                 mfi_btag;
225         int                             mfi_regs_rid;
226
227         bus_dma_tag_t                   mfi_parent_dmat;
228         bus_dma_tag_t                   mfi_buffer_dmat;
229
230         bus_dma_tag_t                   mfi_comms_dmat;
231         bus_dmamap_t                    mfi_comms_dmamap;
232         bus_addr_t                      mfi_comms_busaddr;
233
234         bus_dma_tag_t                   mfi_frames_dmat;
235         bus_dmamap_t                    mfi_frames_dmamap;
236         bus_addr_t                      mfi_frames_busaddr;
237         union mfi_frame                 *mfi_frames;
238
239         bus_dma_tag_t                   mfi_tb_init_dmat;
240         bus_dmamap_t                    mfi_tb_init_dmamap;
241         bus_addr_t                      mfi_tb_init_busaddr;
242         bus_addr_t                      mfi_tb_ioc_init_busaddr;
243         union mfi_frame                 *mfi_tb_init;
244
245         TAILQ_HEAD(,mfi_evt_queue_elm)  mfi_evt_queue;
246         struct task                     mfi_evt_task;
247         struct task                     mfi_map_sync_task;
248         TAILQ_HEAD(,mfi_aen)            mfi_aen_pids;
249         struct mfi_command              *mfi_aen_cm;
250         struct mfi_command              *mfi_skinny_cm;
251         struct mfi_command              *mfi_map_sync_cm;
252         int                             cm_aen_abort;
253         int                             cm_map_abort;
254         uint32_t                        mfi_aen_triggered;
255         uint32_t                        mfi_poll_waiting;
256         uint32_t                        mfi_boot_seq_num;
257         struct selinfo                  mfi_select;
258         int                             mfi_delete_busy_volumes;
259         int                             mfi_keep_deleted_volumes;
260         int                             mfi_detaching;
261
262         bus_dma_tag_t                   mfi_sense_dmat;
263         bus_dmamap_t                    mfi_sense_dmamap;
264         bus_addr_t                      mfi_sense_busaddr;
265         struct mfi_sense                *mfi_sense;
266
267         struct resource                 *mfi_irq;
268         void                            *mfi_intr;
269         int                             mfi_irq_rid;
270
271         struct intr_config_hook         mfi_ich;
272         eventhandler_tag                eh;
273         /* OCR flags */
274         uint8_t adpreset;
275         uint8_t issuepend_done;
276         uint8_t disableOnlineCtrlReset;
277         uint32_t mfiStatus;
278         uint32_t last_seq_num;
279         uint32_t volatile hw_crit_error;
280
281         /*
282          * Allocation for the command array.  Used as an indexable array to
283          * recover completed commands.
284          */
285         struct mfi_command              *mfi_commands;
286         /*
287          * How many commands the firmware can handle.  Also how big the reply
288          * queue is, minus 1.
289          */
290         int                             mfi_max_fw_cmds;
291         /*
292          * How many S/G elements we'll ever actually use
293          */
294         int                             mfi_max_sge;
295         /*
296          * How many bytes a compound frame is, including all of the extra frames
297          * that are used for S/G elements.
298          */
299         int                             mfi_cmd_size;
300         /*
301          * How large an S/G element is.  Used to calculate the number of single
302          * frames in a command.
303          */
304         int                             mfi_sge_size;
305         /*
306          * Max number of sectors that the firmware allows
307          */
308         uint32_t                        mfi_max_io;
309
310         TAILQ_HEAD(,mfi_disk)           mfi_ld_tqh;
311         TAILQ_HEAD(,mfi_system_pd)      mfi_syspd_tqh;
312         TAILQ_HEAD(,mfi_disk_pending)   mfi_ld_pend_tqh;
313         TAILQ_HEAD(,mfi_system_pending) mfi_syspd_pend_tqh;
314         eventhandler_tag                mfi_eh;
315         struct cdev                     *mfi_cdev;
316
317         TAILQ_HEAD(, ccb_hdr)           mfi_cam_ccbq;
318         struct mfi_command *            (* mfi_cam_start)(void *);
319         void                            (*mfi_cam_rescan_cb)(struct mfi_softc *,
320                                             uint32_t);
321         struct callout                  mfi_watchdog_callout;
322         struct mtx                      mfi_io_lock;
323         struct sx                       mfi_config_lock;
324
325         /* Controller type specific interfaces */
326         void    (*mfi_enable_intr)(struct mfi_softc *sc);
327         void    (*mfi_disable_intr)(struct mfi_softc *sc);
328         int32_t (*mfi_read_fw_status)(struct mfi_softc *sc);
329         int     (*mfi_check_clear_intr)(struct mfi_softc *sc);
330         void    (*mfi_issue_cmd)(struct mfi_softc *sc, bus_addr_t bus_add,
331                     uint32_t frame_cnt);
332         int     (*mfi_adp_reset)(struct mfi_softc *sc);
333         int     (*mfi_adp_check_reset)(struct mfi_softc *sc);
334         void                            (*mfi_intr_ptr)(void *sc);
335
336         /* ThunderBolt */
337         uint32_t                        mfi_tbolt;
338         uint32_t                        MFA_enabled;
339         /* Single Reply structure size */
340         uint16_t                        reply_size;
341         /* Singler message size. */
342         uint16_t                        raid_io_msg_size;
343         TAILQ_HEAD(TB, mfi_cmd_tbolt)   mfi_cmd_tbolt_tqh;
344         /* ThunderBolt base contiguous memory mapping. */
345         bus_dma_tag_t                   mfi_tb_dmat;
346         bus_dmamap_t                    mfi_tb_dmamap;
347         bus_addr_t                      mfi_tb_busaddr;
348         /* ThunderBolt Contiguous DMA memory Mapping */
349         uint8_t *                       request_message_pool;
350         uint8_t *                       request_message_pool_align;
351         uint8_t *                       request_desc_pool;
352         bus_addr_t                      request_msg_busaddr;
353         bus_addr_t                      reply_frame_busaddr;
354         bus_addr_t                      sg_frame_busaddr;
355         /* ThunderBolt IOC Init Descriptor */
356         bus_dma_tag_t                   mfi_tb_ioc_init_dmat;
357         bus_dmamap_t                    mfi_tb_ioc_init_dmamap;
358         uint8_t *                       mfi_tb_ioc_init_desc;
359         struct mfi_cmd_tbolt            **mfi_cmd_pool_tbolt;
360         /* Virtual address of reply Frame Pool */
361         struct mfi_mpi2_reply_header*   reply_frame_pool;
362         struct mfi_mpi2_reply_header*   reply_frame_pool_align;
363
364         /* Last reply frame address */
365         uint8_t *                       reply_pool_limit;
366         uint16_t                        last_reply_idx;
367         uint8_t                         max_SGEs_in_chain_message;
368         uint8_t                         max_SGEs_in_main_message;
369         uint8_t                         chain_offset_value_for_main_message;
370         uint8_t                         chain_offset_value_for_mpt_ptmsg;
371 };
372
373 union desc_value {
374         uint64_t        word;
375         struct {
376                 uint32_t        low;
377                 uint32_t        high;
378         }u;
379 };
380
381 // TODO find the right definition
382 #define XXX_MFI_CMD_OP_INIT2                    0x9
383 /*
384  * Request descriptor types
385  */
386 #define MFI_REQ_DESCRIPT_FLAGS_LD_IO           0x7
387 #define MFI_REQ_DESCRIPT_FLAGS_MFA             0x1
388 #define MFI_REQ_DESCRIPT_FLAGS_TYPE_SHIFT       0x1
389 #define MFI_FUSION_FP_DEFAULT_TIMEOUT           0x14
390 #define MFI_LOAD_BALANCE_FLAG                   0x1
391 #define MFI_DCMD_MBOX_PEND_FLAG                 0x1
392
393 //#define MR_PROT_INFO_TYPE_CONTROLLER  0x08
394 #define MEGASAS_SCSI_VARIABLE_LENGTH_CMD        0x7f
395 #define MEGASAS_SCSI_SERVICE_ACTION_READ32      0x9
396 #define MEGASAS_SCSI_SERVICE_ACTION_WRITE32     0xB
397 #define MEGASAS_SCSI_ADDL_CDB_LEN               0x18
398 #define MEGASAS_RD_WR_PROTECT_CHECK_ALL         0x20
399 #define MEGASAS_RD_WR_PROTECT_CHECK_NONE        0x60
400 #define MEGASAS_EEDPBLOCKSIZE                   512
401 struct mfi_cmd_tbolt {
402         union mfi_mpi2_request_descriptor *request_desc;
403         struct mfi_mpi2_request_raid_scsi_io *io_request;
404         bus_addr_t              io_request_phys_addr;
405         bus_addr_t              sg_frame_phys_addr;
406         bus_addr_t              sense_phys_addr;
407         MPI2_SGE_IO_UNION       *sg_frame;
408         uint8_t                 *sense;
409         TAILQ_ENTRY(mfi_cmd_tbolt) next;
410         /*
411          * Context for a MFI frame.
412          * Used to get the mfi cmd from list when a MFI cmd is completed
413          */
414         uint32_t                sync_cmd_idx;
415         uint16_t                index;
416         uint8_t                 status;
417 };
418
419 extern int mfi_attach(struct mfi_softc *);
420 extern void mfi_free(struct mfi_softc *);
421 extern int mfi_shutdown(struct mfi_softc *);
422 extern void mfi_startio(struct mfi_softc *);
423 extern void mfi_disk_complete(struct bio *);
424 extern int mfi_disk_disable(struct mfi_disk *);
425 extern void mfi_disk_enable(struct mfi_disk *);
426 extern int mfi_dump_blocks(struct mfi_softc *, int id, uint64_t, void *, int);
427 extern int mfi_syspd_disable(struct mfi_system_pd *);
428 extern void mfi_syspd_enable(struct mfi_system_pd *);
429 extern int mfi_dump_syspd_blocks(struct mfi_softc *, int id, uint64_t, void *,
430     int);
431 extern int mfi_transition_firmware(struct mfi_softc *sc);
432 extern int mfi_aen_setup(struct mfi_softc *sc, uint32_t seq_start);
433 extern void mfi_complete(struct mfi_softc *sc, struct mfi_command *cm);
434 extern int mfi_mapcmd(struct mfi_softc *sc,struct mfi_command *cm);
435 extern int mfi_wait_command(struct mfi_softc *sc, struct mfi_command *cm);
436 extern void mfi_tbolt_enable_intr_ppc(struct mfi_softc *);
437 extern void mfi_tbolt_disable_intr_ppc(struct mfi_softc *);
438 extern int32_t mfi_tbolt_read_fw_status_ppc(struct mfi_softc *);
439 extern int32_t mfi_tbolt_check_clear_intr_ppc(struct mfi_softc *);
440 extern void mfi_tbolt_issue_cmd_ppc(struct mfi_softc *, bus_addr_t, uint32_t);
441 extern void mfi_tbolt_init_globals(struct mfi_softc*);
442 extern uint32_t mfi_tbolt_get_memory_requirement(struct mfi_softc *);
443 extern int mfi_tbolt_init_desc_pool(struct mfi_softc *, uint8_t *, uint32_t);
444 extern int mfi_tbolt_init_MFI_queue(struct mfi_softc *);
445 extern void mfi_intr_tbolt(void *arg);
446 extern int mfi_tbolt_alloc_cmd(struct mfi_softc *sc);
447 extern int mfi_tbolt_send_frame(struct mfi_softc *sc, struct mfi_command *cm);
448 extern int mfi_tbolt_adp_reset(struct mfi_softc *sc);
449 extern int mfi_tbolt_reset(struct mfi_softc *sc);
450 extern void mfi_tbolt_sync_map_info(struct mfi_softc *sc);
451 extern void mfi_handle_map_sync(void *context, int pending);
452 extern int mfi_dcmd_command(struct mfi_softc *, struct mfi_command **,
453      uint32_t, void **, size_t);
454 extern int mfi_build_cdb(int, uint8_t, u_int64_t, u_int32_t, uint8_t *);
455
456 #define MFIQ_ADD(sc, qname)                                     \
457         do {                                                    \
458                 struct mfi_qstat *qs;                           \
459                                                                 \
460                 qs = &(sc)->mfi_qstat[qname];                   \
461                 qs->q_length++;                                 \
462                 if (qs->q_length > qs->q_max)                   \
463                         qs->q_max = qs->q_length;               \
464         } while (0)
465
466 #define MFIQ_REMOVE(sc, qname)  (sc)->mfi_qstat[qname].q_length--
467
468 #define MFIQ_INIT(sc, qname)                                    \
469         do {                                                    \
470                 sc->mfi_qstat[qname].q_length = 0;              \
471                 sc->mfi_qstat[qname].q_max = 0;                 \
472         } while (0)
473
474 #define MFIQ_COMMAND_QUEUE(name, index)                                 \
475         static __inline void                                            \
476         mfi_initq_ ## name (struct mfi_softc *sc)                       \
477         {                                                               \
478                 TAILQ_INIT(&sc->mfi_ ## name);                          \
479                 MFIQ_INIT(sc, index);                                   \
480         }                                                               \
481         static __inline void                                            \
482         mfi_enqueue_ ## name (struct mfi_command *cm)                   \
483         {                                                               \
484                 if ((cm->cm_flags & MFI_ON_MFIQ_MASK) != 0) {           \
485                         panic("command %p is on another queue, "        \
486                             "flags = %#x\n", cm, cm->cm_flags);         \
487                 }                                                       \
488                 TAILQ_INSERT_TAIL(&cm->cm_sc->mfi_ ## name, cm, cm_link); \
489                 cm->cm_flags |= MFI_ON_ ## index;                       \
490                 MFIQ_ADD(cm->cm_sc, index);                             \
491         }                                                               \
492         static __inline void                                            \
493         mfi_requeue_ ## name (struct mfi_command *cm)                   \
494         {                                                               \
495                 if ((cm->cm_flags & MFI_ON_MFIQ_MASK) != 0) {           \
496                         panic("command %p is on another queue, "        \
497                             "flags = %#x\n", cm, cm->cm_flags);         \
498                 }                                                       \
499                 TAILQ_INSERT_HEAD(&cm->cm_sc->mfi_ ## name, cm, cm_link); \
500                 cm->cm_flags |= MFI_ON_ ## index;                       \
501                 MFIQ_ADD(cm->cm_sc, index);                             \
502         }                                                               \
503         static __inline struct mfi_command *                            \
504         mfi_dequeue_ ## name (struct mfi_softc *sc)                     \
505         {                                                               \
506                 struct mfi_command *cm;                                 \
507                                                                         \
508                 if ((cm = TAILQ_FIRST(&sc->mfi_ ## name)) != NULL) {    \
509                         if ((cm->cm_flags & MFI_ON_ ## index) == 0) {   \
510                                 panic("command %p not in queue, "       \
511                                     "flags = %#x, bit = %#x\n", cm,     \
512                                     cm->cm_flags, MFI_ON_ ## index);    \
513                         }                                               \
514                         TAILQ_REMOVE(&sc->mfi_ ## name, cm, cm_link);   \
515                         cm->cm_flags &= ~MFI_ON_ ## index;              \
516                         MFIQ_REMOVE(sc, index);                         \
517                 }                                                       \
518                 return (cm);                                            \
519         }                                                               \
520         static __inline void                                            \
521         mfi_remove_ ## name (struct mfi_command *cm)                    \
522         {                                                               \
523                 if ((cm->cm_flags & MFI_ON_ ## index) == 0) {           \
524                         panic("command %p not in queue, flags = %#x, " \
525                             "bit = %#x\n", cm, cm->cm_flags,            \
526                             MFI_ON_ ## index);                          \
527                 }                                                       \
528                 TAILQ_REMOVE(&cm->cm_sc->mfi_ ## name, cm, cm_link);    \
529                 cm->cm_flags &= ~MFI_ON_ ## index;                      \
530                 MFIQ_REMOVE(cm->cm_sc, index);                          \
531         }                                                               \
532 struct hack
533
534 MFIQ_COMMAND_QUEUE(free, MFIQ_FREE);
535 MFIQ_COMMAND_QUEUE(ready, MFIQ_READY);
536 MFIQ_COMMAND_QUEUE(busy, MFIQ_BUSY);
537
538 static __inline void
539 mfi_initq_bio(struct mfi_softc *sc)
540 {
541         bioq_init(&sc->mfi_bioq);
542         MFIQ_INIT(sc, MFIQ_BIO);
543 }
544
545 static __inline void
546 mfi_enqueue_bio(struct mfi_softc *sc, struct bio *bp)
547 {
548         bioq_insert_tail(&sc->mfi_bioq, bp);
549         MFIQ_ADD(sc, MFIQ_BIO);
550 }
551
552 static __inline struct bio *
553 mfi_dequeue_bio(struct mfi_softc *sc)
554 {
555         struct bio *bp;
556
557         if ((bp = bioq_first(&sc->mfi_bioq)) != NULL) {
558                 bioq_remove(&sc->mfi_bioq, bp);
559                 MFIQ_REMOVE(sc, MFIQ_BIO);
560         }
561         return (bp);
562 }
563
564 /*
565  * This is from the original scsi_extract_sense() in CAM.  It's copied
566  * here because CAM now uses a non-inline version that follows more complex
567  * additions to the SPC spec, and we don't want to force a dependency on
568  * the CAM module for such a trivial action.
569  */
570 static __inline void
571 mfi_extract_sense(struct scsi_sense_data_fixed *sense,
572     int *error_code, int *sense_key, int *asc, int *ascq)
573 {
574
575         *error_code = sense->error_code & SSD_ERRCODE;
576         *sense_key = sense->flags & SSD_KEY;
577         *asc = (sense->extra_len >= 5) ? sense->add_sense_code : 0;
578         *ascq = (sense->extra_len >= 6) ? sense->add_sense_code_qual : 0;
579 }
580
581 static __inline void
582 mfi_print_sense(struct mfi_softc *sc, void *sense)
583 {
584         int error, key, asc, ascq;
585
586         mfi_extract_sense((struct scsi_sense_data_fixed *)sense,
587             &error, &key, &asc, &ascq);
588         device_printf(sc->mfi_dev, "sense error %d, sense_key %d, "
589             "asc %d, ascq %d\n", error, key, asc, ascq);
590 }
591
592
593 #define MFI_WRITE4(sc, reg, val)        bus_space_write_4((sc)->mfi_btag, \
594         sc->mfi_bhandle, (reg), (val))
595 #define MFI_READ4(sc, reg)              bus_space_read_4((sc)->mfi_btag, \
596         (sc)->mfi_bhandle, (reg))
597 #define MFI_WRITE2(sc, reg, val)        bus_space_write_2((sc)->mfi_btag, \
598         sc->mfi_bhandle, (reg), (val))
599 #define MFI_READ2(sc, reg)              bus_space_read_2((sc)->mfi_btag, \
600         (sc)->mfi_bhandle, (reg))
601 #define MFI_WRITE1(sc, reg, val)        bus_space_write_1((sc)->mfi_btag, \
602         sc->mfi_bhandle, (reg), (val))
603 #define MFI_READ1(sc, reg)              bus_space_read_1((sc)->mfi_btag, \
604         (sc)->mfi_bhandle, (reg))
605
606 MALLOC_DECLARE(M_MFIBUF);
607 SYSCTL_DECL(_hw_mfi);
608
609 #define MFI_RESET_WAIT_TIME 180
610 #define MFI_CMD_TIMEOUT 30
611 #define MFI_SYS_PD_IO   0
612 #define MFI_LD_IO       1
613 #define MFI_SKINNY_MEMORY 0x02000000
614 #define MFI_MAXPHYS (128 * 1024)
615
616 #ifdef MFI_DEBUG
617 extern void mfi_print_cmd(struct mfi_command *cm);
618 extern void mfi_dump_cmds(struct mfi_softc *sc);
619 extern void mfi_validate_sg(struct mfi_softc *, struct mfi_command *,
620     const char *, int);
621 #define MFI_PRINT_CMD(cm)       mfi_print_cmd(cm)
622 #define MFI_DUMP_CMDS(sc)       mfi_dump_cmds(sc)
623 #define MFI_VALIDATE_CMD(sc, cm) mfi_validate_sg(sc, cm, __FUNCTION__, __LINE__)
624 #else
625 #define MFI_PRINT_CMD(cm)
626 #define MFI_DUMP_CMDS(sc)
627 #define MFI_VALIDATE_CMD(sc, cm)
628 #endif
629
630 extern void mfi_release_command(struct mfi_command *);
631 extern void mfi_tbolt_return_cmd(struct mfi_softc *,
632     struct mfi_cmd_tbolt *, struct mfi_command *);
633
634 #endif /* _MFIVAR_H */