2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: src/sys/dev/twe/twevar.h,v 1.1.2.4 2002/03/07 09:57:02 msmith Exp $
28 * $DragonFly: src/sys/dev/raid/twe/twevar.h,v 1.4 2005/06/10 17:10:26 swildner Exp $
32 #define debug(level, fmt, args...) \
34 if (level <= TWE_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \
36 #define debug_called(level) \
38 if (level <= TWE_DEBUG) printf("%s: called\n", __func__); \
41 #define debug(level, fmt, args...)
42 #define debug_called(level)
46 * Structure describing a logical unit as attached to the controller
57 /* unit state and type */
61 /* handle for attached driver */
66 * Per-command control structure.
68 * Note that due to alignment constraints on the tc_command field, this *must* be 64-byte aligned.
69 * We achieve this by placing the command at the beginning of the structure, and using malloc()
70 * to allocate each structure.
74 /* controller command */
75 TWE_Command tr_command; /* command as submitted to controller */
78 void *tr_data; /* data buffer */
79 void *tr_realdata; /* copy of real data buffer pointer for alignment fixup */
82 TAILQ_ENTRY(twe_request) tr_link; /* list linkage */
83 struct twe_softc *tr_sc; /* controller that owns us */
84 int tr_status; /* command status */
85 #define TWE_CMD_SETUP 0 /* being assembled */
86 #define TWE_CMD_BUSY 1 /* submitted to controller */
87 #define TWE_CMD_COMPLETE 2 /* completed by controller (maybe with error) */
89 #define TWE_CMD_DATAIN (1<<0)
90 #define TWE_CMD_DATAOUT (1<<1)
91 #define TWE_CMD_ALIGNBUF (1<<2) /* data in bio is misaligned, have to copy to/from private buffer */
92 #define TWE_CMD_SLEEPER (1<<3) /* owner is sleeping on this command */
93 void (* tr_complete)(struct twe_request *tr); /* completion handler */
94 void *tr_private; /* submitter-private data or wait channel */
96 TWE_PLATFORM_REQUEST /* platform-specific request elements */
100 * Per-controller state.
104 /* controller queues and arrays */
105 TAILQ_HEAD(, twe_request) twe_free; /* command structures available for reuse */
106 twe_bioq twe_bioq; /* outstanding I/O operations */
107 TAILQ_HEAD(, twe_request) twe_ready; /* requests ready for the controller */
108 TAILQ_HEAD(, twe_request) twe_busy; /* requests busy in the controller */
109 TAILQ_HEAD(, twe_request) twe_complete; /* active commands (busy or waiting for completion) */
110 struct twe_request *twe_lookup[TWE_Q_LENGTH]; /* commands indexed by request ID */
111 struct twe_drive twe_drive[TWE_MAX_UNITS]; /* attached drives */
113 /* asynchronous event handling */
114 u_int16_t twe_aen_queue[TWE_Q_LENGTH]; /* AENs queued for userland tool(s) */
115 int twe_aen_head, twe_aen_tail; /* ringbuffer pointers for AEN queue */
116 int twe_wait_aen; /* wait-for-aen notification */
118 /* controller status */
120 #define TWE_STATE_INTEN (1<<0) /* interrupts have been enabled */
121 #define TWE_STATE_SHUTDOWN (1<<1) /* controller is shut down */
122 #define TWE_STATE_OPEN (1<<2) /* control device is open */
123 #define TWE_STATE_SUSPEND (1<<3) /* controller is suspended */
125 struct twe_qstat twe_qstat[TWEQ_COUNT]; /* queue statistics */
127 TWE_PLATFORM_SOFTC /* platform-specific softc elements */
131 * Interface betwen driver core and platform-dependant code.
133 extern int twe_setup(struct twe_softc *sc); /* do early driver/controller setup */
134 extern void twe_init(struct twe_softc *sc); /* init controller */
135 extern void twe_deinit(struct twe_softc *sc); /* stop controller */
136 extern void twe_intr(struct twe_softc *sc); /* hardware interrupt signalled */
137 extern void twe_startio(struct twe_softc *sc);
138 extern int twe_dump_blocks(struct twe_softc *sc, int unit, /* crashdump block write */
139 u_int32_t lba, void *data, int nblks);
140 extern int twe_ioctl(struct twe_softc *sc, int cmd,
141 void *addr); /* handle user request */
142 extern void twe_describe_controller(struct twe_softc *sc); /* print controller info */
143 extern void twe_print_controller(struct twe_softc *sc);
144 extern void twe_enable_interrupts(struct twe_softc *sc); /* enable controller interrupts */
145 extern void twe_disable_interrupts(struct twe_softc *sc); /* disable controller interrupts */
147 extern void twe_attach_drive(struct twe_softc *sc,
148 struct twe_drive *dr); /* attach drive when found in twe_add_unit */
149 extern void twe_detach_drive(struct twe_softc *sc,
150 int unit); /* detach drive */
151 extern void twe_clear_pci_parity_error(struct twe_softc *sc);
152 extern void twe_clear_pci_abort(struct twe_softc *sc);
153 extern void twed_intr(twe_bio *bp); /* return bio from core */
154 extern struct twe_request *twe_allocate_request(struct twe_softc *sc); /* allocate request structure */
155 extern void twe_free_request(struct twe_request *tr); /* free request structure */
156 extern void twe_map_request(struct twe_request *tr); /* make request visible to controller, do s/g */
157 extern void twe_unmap_request(struct twe_request *tr); /* cleanup after transfer, unmap */
159 /********************************************************************************
162 #define TWEQ_ADD(sc, qname) \
164 struct twe_qstat *qs = &(sc)->twe_qstat[qname]; \
167 if (qs->q_length > qs->q_max) \
168 qs->q_max = qs->q_length; \
171 #define TWEQ_REMOVE(sc, qname) (sc)->twe_qstat[qname].q_length--
172 #define TWEQ_INIT(sc, qname) \
174 sc->twe_qstat[qname].q_length = 0; \
175 sc->twe_qstat[qname].q_max = 0; \
179 #define TWEQ_REQUEST_QUEUE(name, index) \
180 static __inline void \
181 twe_initq_ ## name (struct twe_softc *sc) \
183 TAILQ_INIT(&sc->twe_ ## name); \
184 TWEQ_INIT(sc, index); \
186 static __inline void \
187 twe_enqueue_ ## name (struct twe_request *tr) \
190 TAILQ_INSERT_TAIL(&tr->tr_sc->twe_ ## name, tr, tr_link); \
191 TWEQ_ADD(tr->tr_sc, index); \
194 static __inline void \
195 twe_requeue_ ## name (struct twe_request *tr) \
198 TAILQ_INSERT_HEAD(&tr->tr_sc->twe_ ## name, tr, tr_link); \
199 TWEQ_ADD(tr->tr_sc, index); \
202 static __inline struct twe_request * \
203 twe_dequeue_ ## name (struct twe_softc *sc) \
205 struct twe_request *tr; \
208 if ((tr = TAILQ_FIRST(&sc->twe_ ## name)) != NULL) { \
209 TAILQ_REMOVE(&sc->twe_ ## name, tr, tr_link); \
210 TWEQ_REMOVE(sc, index); \
215 static __inline void \
216 twe_remove_ ## name (struct twe_request *tr) \
219 TAILQ_REMOVE(&tr->tr_sc->twe_ ## name, tr, tr_link); \
220 TWEQ_REMOVE(tr->tr_sc, index); \
224 TWEQ_REQUEST_QUEUE(free, TWEQ_FREE)
225 TWEQ_REQUEST_QUEUE(ready, TWEQ_READY)
226 TWEQ_REQUEST_QUEUE(busy, TWEQ_BUSY)
227 TWEQ_REQUEST_QUEUE(complete, TWEQ_COMPLETE)
231 * outstanding bio queue
234 twe_initq_bio(struct twe_softc *sc)
236 TWE_BIO_QINIT(sc->twe_bioq);
237 TWEQ_INIT(sc, TWEQ_BIO);
241 twe_enqueue_bio(struct twe_softc *sc, twe_bio *bp)
244 TWE_BIO_QINSERT(sc->twe_bioq, bp);
245 TWEQ_ADD(sc, TWEQ_BIO);
249 static __inline twe_bio *
250 twe_dequeue_bio(struct twe_softc *sc)
255 if ((bp = TWE_BIO_QFIRST(sc->twe_bioq)) != NULL) {
256 TWE_BIO_QREMOVE(sc->twe_bioq, bp);
257 TWEQ_REMOVE(sc, TWEQ_BIO);