Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / raid / mly / mlyvar.h
1 /*-
2  * Copyright (c) 2000, 2001 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/mly/mlyvar.h,v 1.1.2.2 2001/03/05 20:17:24 msmith Exp $
28  */
29
30 /********************************************************************************
31  ********************************************************************************
32                                                      Driver Parameter Definitions
33  ********************************************************************************
34  ********************************************************************************/
35
36 /*
37  * The firmware interface allows for a 16-bit command identifier.  A lookup
38  * table this size (256k) would be too expensive, so we cap ourselves at a
39  * reasonable limit.
40  */
41 #define MLY_MAXCOMMANDS         256     /* max commands per controller */
42
43 /*
44  * The firmware interface allows for a 16-bit s/g list length.  We limit 
45  * ourselves to a reasonable maximum and ensure alignment.
46  */
47 #define MLY_MAXSGENTRIES        64      /* max S/G entries, limit 65535 */              
48
49 /********************************************************************************
50  ********************************************************************************
51                                                       Cross-version Compatibility
52  ********************************************************************************
53  ********************************************************************************/
54
55 #if __FreeBSD_version >= 500005
56 # include <sys/taskqueue.h>
57 #endif
58
59 #if __FreeBSD_version <= 500014
60 # include <machine/clock.h>
61 # undef offsetof
62 # define offsetof(type, field) ((size_t)(&((type *)0)->field))
63 #endif
64
65 /********************************************************************************
66  ********************************************************************************
67                                                       Driver Variable Definitions
68  ********************************************************************************
69  ********************************************************************************/
70
71 /*
72  * Debugging levels:
73  *  0 - quiet, only emit warnings
74  *  1 - noisy, emit major function points and things done
75  *  2 - extremely noisy, emit trace items in loops, etc.
76  */
77 #ifdef MLY_DEBUG
78 # define debug(level, fmt, args...)     do { if (level <= MLY_DEBUG) printf("%s: " fmt "\n", __FUNCTION__ , ##args); } while(0)
79 # define debug_called(level)            do { if (level <= MLY_DEBUG) printf(__FUNCTION__ ": called\n"); } while(0)
80 # define debug_struct(s)                printf("  SIZE %s: %d\n", #s, sizeof(struct s))
81 # define debug_union(s)                 printf("  SIZE %s: %d\n", #s, sizeof(union s))
82 # define debug_field(s, f)              printf("  OFFSET %s.%s: %d\n", #s, #f, ((int)&(((struct s *)0)->f)))
83 extern void             mly_printstate0(void);
84 extern struct mly_softc *mly_softc0;
85 #else
86 # define debug(level, fmt, args...)
87 # define debug_called(level)
88 # define debug_struct(s)
89 #endif
90
91 #define mly_printf(sc, fmt, args...)    device_printf(sc->mly_dev, fmt , ##args)
92
93 /*
94  * Per-device structure, used to save persistent state on devices.
95  *
96  * Note that this isn't really Bus/Target/Lun since we don't support
97  * lun != 0 at this time.
98  */
99 struct mly_btl {
100     int                 mb_flags;
101 #define MLY_BTL_PHYSICAL        (1<<0)          /* physical device */
102 #define MLY_BTL_LOGICAL         (1<<1)          /* logical device */
103 #define MLY_BTL_PROTECTED       (1<<2)          /* device is protected - I/O not allowed */
104 #define MLY_BTL_RESCAN          (1<<3)          /* device needs to be rescanned */
105     char                mb_name[16];            /* peripheral attached to this device */
106     int                 mb_state;               /* see 8.1 */
107     int                 mb_type;                /* see 8.2 */
108
109     /* physical devices only */
110     int                 mb_speed;               /* interface transfer rate */
111     int                 mb_width;               /* interface width */
112 };
113
114 /*
115  * Per-command control structure.
116  */
117 struct mly_command {
118     TAILQ_ENTRY(mly_command)    mc_link;        /* list linkage */
119
120     struct mly_softc            *mc_sc;         /* controller that owns us */
121     u_int16_t                   mc_slot;        /* command slot we occupy */
122     int                         mc_flags;
123 #define MLY_CMD_BUSY            (1<<0)          /* command is being run, or ready to run, or not completed */
124 #define MLY_CMD_COMPLETE        (1<<1)          /* command has been completed */
125 #define MLY_CMD_MAPPED          (1<<3)          /* command has had its data mapped */
126 #define MLY_CMD_DATAIN          (1<<4)          /* data moves controller->system */
127 #define MLY_CMD_DATAOUT         (1<<5)          /* data moves system->controller */
128     u_int16_t                   mc_status;      /* command completion status */
129     u_int8_t                    mc_sense;       /* sense data length */
130     int32_t                     mc_resid;       /* I/O residual count */
131
132     union mly_command_packet    *mc_packet;     /* our controller command */
133     u_int64_t                   mc_packetphys;  /* physical address of the mapped packet */
134
135     void                        *mc_data;       /* data buffer */
136     size_t                      mc_length;      /* data length */
137     bus_dmamap_t                mc_datamap;     /* DMA map for data */
138
139     void        (* mc_complete)(struct mly_command *mc);        /* completion handler */
140     void        *mc_private;                                    /* caller-private data */
141
142 };
143
144 /*
145  * Command slot regulation.
146  *
147  * We can't use slot 0 due to the memory mailbox implementation.
148  */
149 #define MLY_SLOT_START          1
150 #define MLY_SLOT_MAX            (MLY_SLOT_START + MLY_MAXCOMMANDS)
151
152 /*
153  * Per-controller structure.
154  */
155 struct mly_softc {
156     /* bus connections */
157     device_t            mly_dev;
158     dev_t               mly_dev_t;
159     struct resource     *mly_regs_resource;     /* register interface window */
160     int                 mly_regs_rid;           /* resource ID */
161     bus_space_handle_t  mly_bhandle;            /* bus space handle */
162     bus_space_tag_t     mly_btag;               /* bus space tag */
163     bus_dma_tag_t       mly_parent_dmat;        /* parent DMA tag */
164     bus_dma_tag_t       mly_buffer_dmat;        /* data buffer/command DMA tag */
165     struct resource     *mly_irq;               /* interrupt */
166     int                 mly_irq_rid;
167     void                *mly_intr;              /* interrupt handle */
168
169     /* scatter/gather lists and their controller-visible mappings */
170     struct mly_sg_entry *mly_sg_table;          /* s/g lists */
171     u_int32_t           mly_sg_busaddr;         /* s/g table base address in bus space */
172     bus_dma_tag_t       mly_sg_dmat;            /* s/g buffer DMA tag */
173     bus_dmamap_t        mly_sg_dmamap;          /* map for s/g buffers */
174
175     /* controller hardware interface */
176     int                 mly_hwif;
177 #define MLY_HWIF_I960RX         0
178 #define MLY_HWIF_STRONGARM      1
179     u_int8_t            mly_doorbell_true;      /* xor map to make hardware doorbell 'true' bits into 1s */
180     u_int8_t            mly_command_mailbox;    /* register offsets */
181     u_int8_t            mly_status_mailbox;
182     u_int8_t            mly_idbr;
183     u_int8_t            mly_odbr;
184     u_int8_t            mly_error_status;
185     u_int8_t            mly_interrupt_status;
186     u_int8_t            mly_interrupt_mask;
187     struct mly_mmbox    *mly_mmbox;                     /* kernel-space address of memory mailbox */
188     u_int64_t           mly_mmbox_busaddr;              /* bus-space address of memory mailbox */
189     bus_dma_tag_t       mly_mmbox_dmat;                 /* memory mailbox DMA tag */
190     bus_dmamap_t        mly_mmbox_dmamap;               /* memory mailbox DMA map */
191     u_int32_t           mly_mmbox_command_index;        /* next index to use */
192     u_int32_t           mly_mmbox_status_index;         /* index we next expect status at */
193
194     /* controller features, limits and status */
195     int                 mly_state;
196 #define MLY_STATE_SUSPEND       (1<<0)
197 #define MLY_STATE_OPEN          (1<<1)
198 #define MLY_STATE_INTERRUPTS_ON (1<<2)
199 #define MLY_STATE_MMBOX_ACTIVE  (1<<3)
200     struct mly_ioctl_getcontrollerinfo  *mly_controllerinfo;
201     struct mly_param_controller         *mly_controllerparam;
202     struct mly_btl                      mly_btl[MLY_MAX_CHANNELS][MLY_MAX_TARGETS];
203
204     /* command management */
205     struct mly_command          mly_command[MLY_MAXCOMMANDS];   /* commands */
206     union mly_command_packet    *mly_packet;            /* command packets */
207     bus_dma_tag_t               mly_packet_dmat;        /* packet DMA tag */
208     bus_dmamap_t                mly_packetmap;          /* packet DMA map */
209     u_int64_t                   mly_packetphys;         /* packet array base address */
210     TAILQ_HEAD(,mly_command)    mly_free;               /* commands available for reuse */
211     TAILQ_HEAD(,mly_command)    mly_ready;              /* commands ready to be submitted */
212     TAILQ_HEAD(,mly_command)    mly_busy;
213     TAILQ_HEAD(,mly_command)    mly_complete;           /* commands which have been returned by the controller */
214     struct mly_qstat            mly_qstat[MLYQ_COUNT];  /* queue statistics */
215
216     /* health monitoring */
217     u_int32_t                   mly_event_change;       /* event status change indicator */
218     u_int32_t                   mly_event_counter;      /* next event for which we anticpiate status */
219     u_int32_t                   mly_event_waiting;      /* next event the controller will post status for */
220     struct callout_handle       mly_periodic;           /* periodic event handling */
221
222     /* CAM connection */
223     TAILQ_HEAD(,ccb_hdr)        mly_cam_ccbq;                   /* outstanding I/O from CAM */
224     struct cam_sim              *mly_cam_sim[MLY_MAX_CHANNELS];
225     int                         mly_cam_lowbus;
226
227 #if __FreeBSD_version >= 500005
228     /* command-completion task */
229     struct task         mly_task_complete;      /* deferred-completion task */
230 #endif
231 };
232
233 /*
234  * Register access helpers.
235  */
236 #define MLY_SET_REG(sc, reg, val)       bus_space_write_1(sc->mly_btag, sc->mly_bhandle, reg, val)
237 #define MLY_GET_REG(sc, reg)            bus_space_read_1 (sc->mly_btag, sc->mly_bhandle, reg)
238 #define MLY_GET_REG2(sc, reg)           bus_space_read_2 (sc->mly_btag, sc->mly_bhandle, reg)
239 #define MLY_GET_REG4(sc, reg)           bus_space_read_4 (sc->mly_btag, sc->mly_bhandle, reg)
240
241 #define MLY_SET_MBOX(sc, mbox, ptr)                                                                     \
242         do {                                                                                            \
243             bus_space_write_4(sc->mly_btag, sc->mly_bhandle, mbox,      *((u_int32_t *)ptr));           \
244             bus_space_write_4(sc->mly_btag, sc->mly_bhandle, mbox +  4, *((u_int32_t *)ptr + 1));       \
245             bus_space_write_4(sc->mly_btag, sc->mly_bhandle, mbox +  8, *((u_int32_t *)ptr + 2));       \
246             bus_space_write_4(sc->mly_btag, sc->mly_bhandle, mbox + 12, *((u_int32_t *)ptr + 3));       \
247         } while(0);
248 #define MLY_GET_MBOX(sc, mbox, ptr)                                                                     \
249         do {                                                                                            \
250             *((u_int32_t *)ptr) = bus_space_read_4(sc->mly_btag, sc->mly_bhandle, mbox);                \
251             *((u_int32_t *)ptr + 1) = bus_space_read_4(sc->mly_btag, sc->mly_bhandle, mbox + 4);        \
252             *((u_int32_t *)ptr + 2) = bus_space_read_4(sc->mly_btag, sc->mly_bhandle, mbox + 8);        \
253             *((u_int32_t *)ptr + 3) = bus_space_read_4(sc->mly_btag, sc->mly_bhandle, mbox + 12);       \
254         } while(0);
255
256 #define MLY_IDBR_TRUE(sc, mask)                                                         \
257         ((((MLY_GET_REG((sc), (sc)->mly_idbr)) ^ (sc)->mly_doorbell_true) & (mask)) == (mask))
258 #define MLY_ODBR_TRUE(sc, mask)                                                         \
259         ((MLY_GET_REG((sc), (sc)->mly_odbr) & (mask)) == (mask))
260 #define MLY_ERROR_VALID(sc)                                                             \
261         ((((MLY_GET_REG((sc), (sc)->mly_error_status)) ^ (sc)->mly_doorbell_true) & (MLY_MSG_EMPTY)) == 0)
262
263 #define MLY_MASK_INTERRUPTS(sc)                                                         \
264         do {                                                                            \
265             MLY_SET_REG((sc), (sc)->mly_interrupt_mask, MLY_INTERRUPT_MASK_DISABLE);    \
266             sc->mly_state &= ~MLY_STATE_INTERRUPTS_ON;                                  \
267         } while(0);
268 #define MLY_UNMASK_INTERRUPTS(sc)                                                       \
269         do {                                                                            \
270             MLY_SET_REG((sc), (sc)->mly_interrupt_mask, MLY_INTERRUPT_MASK_ENABLE);     \
271             sc->mly_state |= MLY_STATE_INTERRUPTS_ON;                                   \
272         } while(0);
273
274 /*
275  * Logical device number -> bus/target translation
276  */
277 #define MLY_LOGDEV_BUS(sc, x)   (((x) / MLY_MAX_TARGETS) + (sc)->mly_controllerinfo->physical_channels_present)
278 #define MLY_LOGDEV_TARGET(x)    ((x) % MLY_MAX_TARGETS)
279
280 /*
281  * Public functions/variables
282  */
283 /* mly.c */
284 extern int      mly_attach(struct mly_softc *sc);
285 extern void     mly_detach(struct mly_softc *sc);
286 extern void     mly_free(struct mly_softc *sc);
287 extern void     mly_startio(struct mly_softc *sc);
288 extern void     mly_done(struct mly_softc *sc);
289 extern int      mly_alloc_command(struct mly_softc *sc, struct mly_command **mcp);
290 extern void     mly_release_command(struct mly_command *mc);
291
292 /* mly_cam.c */
293 extern int      mly_cam_attach(struct mly_softc *sc);
294 extern void     mly_cam_detach(struct mly_softc *sc);
295 extern int      mly_cam_command(struct mly_softc *sc, struct mly_command **mcp);
296 extern int      mly_name_device(struct mly_softc *sc, int bus, int target);
297
298 /********************************************************************************
299  * Queue primitives
300  */
301
302 #define MLYQ_ADD(sc, qname)                                     \
303         do {                                                    \
304             struct mly_qstat *qs = &(sc)->mly_qstat[qname];     \
305                                                                 \
306             qs->q_length++;                                     \
307             if (qs->q_length > qs->q_max)                       \
308                 qs->q_max = qs->q_length;                       \
309         } while(0)
310
311 #define MLYQ_REMOVE(sc, qname)    (sc)->mly_qstat[qname].q_length--
312 #define MLYQ_INIT(sc, qname)                    \
313         do {                                    \
314             sc->mly_qstat[qname].q_length = 0;  \
315             sc->mly_qstat[qname].q_max = 0;     \
316         } while(0)
317
318
319 #define MLYQ_COMMAND_QUEUE(name, index)                                 \
320 static __inline void                                                    \
321 mly_initq_ ## name (struct mly_softc *sc)                               \
322 {                                                                       \
323     TAILQ_INIT(&sc->mly_ ## name);                                      \
324     MLYQ_INIT(sc, index);                                               \
325 }                                                                       \
326 static __inline void                                                    \
327 mly_enqueue_ ## name (struct mly_command *mc)                           \
328 {                                                                       \
329     int         s;                                                      \
330                                                                         \
331     s = splcam();                                                       \
332     TAILQ_INSERT_TAIL(&mc->mc_sc->mly_ ## name, mc, mc_link);           \
333     MLYQ_ADD(mc->mc_sc, index);                                         \
334     splx(s);                                                            \
335 }                                                                       \
336 static __inline void                                                    \
337 mly_requeue_ ## name (struct mly_command *mc)                           \
338 {                                                                       \
339     int         s;                                                      \
340                                                                         \
341     s = splcam();                                                       \
342     TAILQ_INSERT_HEAD(&mc->mc_sc->mly_ ## name, mc, mc_link);           \
343     MLYQ_ADD(mc->mc_sc, index);                                         \
344     splx(s);                                                            \
345 }                                                                       \
346 static __inline struct mly_command *                                    \
347 mly_dequeue_ ## name (struct mly_softc *sc)                             \
348 {                                                                       \
349     struct mly_command  *mc;                                            \
350     int                 s;                                              \
351                                                                         \
352     s = splcam();                                                       \
353     if ((mc = TAILQ_FIRST(&sc->mly_ ## name)) != NULL) {                \
354         TAILQ_REMOVE(&sc->mly_ ## name, mc, mc_link);                   \
355         MLYQ_REMOVE(sc, index);                                         \
356     }                                                                   \
357     splx(s);                                                            \
358     return(mc);                                                         \
359 }                                                                       \
360 static __inline void                                                    \
361 mly_remove_ ## name (struct mly_command *mc)                            \
362 {                                                                       \
363     int                 s;                                              \
364                                                                         \
365     s = splcam();                                                       \
366     TAILQ_REMOVE(&mc->mc_sc->mly_ ## name, mc, mc_link);                \
367     MLYQ_REMOVE(mc->mc_sc, index);                                      \
368     splx(s);                                                            \
369 }                                                                       \
370 struct hack
371
372 MLYQ_COMMAND_QUEUE(free, MLYQ_FREE);
373 MLYQ_COMMAND_QUEUE(ready, MLYQ_READY);
374 MLYQ_COMMAND_QUEUE(busy, MLYQ_BUSY);
375 MLYQ_COMMAND_QUEUE(complete, MLYQ_COMPLETE);
376