xref: /dragonfly/sys/dev/raid/twe/twevar.h (revision 1de703da)
1 /*-
2  * Copyright (c) 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  *	$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.2 2003/06/17 04:28:32 dillon Exp $
29  */
30 
31 #ifdef TWE_DEBUG
32 #define debug(level, fmt, args...)							\
33 	do {										\
34 	    if (level <= TWE_DEBUG) printf("%s: " fmt "\n", __func__ , ##args);	\
35 	} while(0)
36 #define debug_called(level)						\
37 	do {								\
38 	    if (level <= TWE_DEBUG) printf("%s: called\n", __func__);	\
39 	} while(0)
40 #else
41 #define debug(level, fmt, args...)
42 #define debug_called(level)
43 #endif
44 
45 /*
46  * Structure describing a logical unit as attached to the controller
47  */
48 struct twe_drive
49 {
50     /* unit properties */
51     u_int32_t		td_size;
52     int			td_cylinders;
53     int			td_heads;
54     int			td_sectors;
55     int			td_unit;
56 
57     /* unit state and type */
58     u_int8_t		td_state;
59     u_int8_t		td_type;
60 
61     /* handle for attached driver */
62     device_t		td_disk;
63 };
64 
65 /*
66  * Per-command control structure.
67  *
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.
71  */
72 struct twe_request
73 {
74     /* controller command */
75     TWE_Command			tr_command;	/* command as submitted to controller */
76 
77     /* command payload */
78     void			*tr_data;	/* data buffer */
79     void			*tr_realdata;	/* copy of real data buffer pointer for alignment fixup */
80     size_t			tr_length;
81 
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) */
88     int				tr_flags;
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 */
95 
96     TWE_PLATFORM_REQUEST	/* platform-specific request elements */
97 };
98 
99 /*
100  * Per-controller state.
101  */
102 struct twe_softc
103 {
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 */
112 
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 */
117 
118     /* controller status */
119     int			twe_state;
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 */
124     int			twe_host_id;
125     struct twe_qstat	twe_qstat[TWEQ_COUNT];	/* queue statistics */
126 
127     TWE_PLATFORM_SOFTC		/* platform-specific softc elements */
128 };
129 
130 /*
131  * Interface betwen driver core and platform-dependant code.
132  */
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 */
146 
147 extern void	twe_attach_drive(struct twe_softc *sc,
148 					 struct twe_drive *dr); /* attach drive when found in twe_init */
149 extern void	twe_clear_pci_parity_error(struct twe_softc *sc);
150 extern void	twe_clear_pci_abort(struct twe_softc *sc);
151 extern void	twed_intr(twe_bio *bp);				/* return bio from core */
152 extern struct twe_request *twe_allocate_request(struct twe_softc *sc);	/* allocate request structure */
153 extern void	twe_free_request(struct twe_request *tr);	/* free request structure */
154 extern void	twe_map_request(struct twe_request *tr);	/* make request visible to controller, do s/g */
155 extern void	twe_unmap_request(struct twe_request *tr);	/* cleanup after transfer, unmap */
156 
157 /********************************************************************************
158  * Queue primitives
159  */
160 #define TWEQ_ADD(sc, qname)					\
161 	do {							\
162 	    struct twe_qstat *qs = &(sc)->twe_qstat[qname];	\
163 								\
164 	    qs->q_length++;					\
165 	    if (qs->q_length > qs->q_max)			\
166 		qs->q_max = qs->q_length;			\
167 	} while(0)
168 
169 #define TWEQ_REMOVE(sc, qname)    (sc)->twe_qstat[qname].q_length--
170 #define TWEQ_INIT(sc, qname)			\
171 	do {					\
172 	    sc->twe_qstat[qname].q_length = 0;	\
173 	    sc->twe_qstat[qname].q_max = 0;	\
174 	} while(0)
175 
176 
177 #define TWEQ_REQUEST_QUEUE(name, index)					\
178 static __inline void							\
179 twe_initq_ ## name (struct twe_softc *sc)				\
180 {									\
181     TAILQ_INIT(&sc->twe_ ## name);					\
182     TWEQ_INIT(sc, index);						\
183 }									\
184 static __inline void							\
185 twe_enqueue_ ## name (struct twe_request *tr)				\
186 {									\
187     int		s;							\
188 									\
189     s = splbio();							\
190     TAILQ_INSERT_TAIL(&tr->tr_sc->twe_ ## name, tr, tr_link);		\
191     TWEQ_ADD(tr->tr_sc, index);						\
192     splx(s);								\
193 }									\
194 static __inline void							\
195 twe_requeue_ ## name (struct twe_request *tr)				\
196 {									\
197     int		s;							\
198 									\
199     s = splbio();							\
200     TAILQ_INSERT_HEAD(&tr->tr_sc->twe_ ## name, tr, tr_link);		\
201     TWEQ_ADD(tr->tr_sc, index);						\
202     splx(s);								\
203 }									\
204 static __inline struct twe_request *					\
205 twe_dequeue_ ## name (struct twe_softc *sc)				\
206 {									\
207     struct twe_request	*tr;						\
208     int			s;						\
209 									\
210     s = splbio();							\
211     if ((tr = TAILQ_FIRST(&sc->twe_ ## name)) != NULL) {		\
212 	TAILQ_REMOVE(&sc->twe_ ## name, tr, tr_link);			\
213 	TWEQ_REMOVE(sc, index);						\
214     }									\
215     splx(s);								\
216     return(tr);								\
217 }									\
218 static __inline void							\
219 twe_remove_ ## name (struct twe_request *tr)				\
220 {									\
221     int			s;						\
222 									\
223     s = splbio();							\
224     TAILQ_REMOVE(&tr->tr_sc->twe_ ## name, tr, tr_link);		\
225     TWEQ_REMOVE(tr->tr_sc, index);					\
226     splx(s);								\
227 }
228 
229 TWEQ_REQUEST_QUEUE(free, TWEQ_FREE)
230 TWEQ_REQUEST_QUEUE(ready, TWEQ_READY)
231 TWEQ_REQUEST_QUEUE(busy, TWEQ_BUSY)
232 TWEQ_REQUEST_QUEUE(complete, TWEQ_COMPLETE)
233 
234 
235 /*
236  * outstanding bio queue
237  */
238 static __inline void
239 twe_initq_bio(struct twe_softc *sc)
240 {
241     TWE_BIO_QINIT(sc->twe_bioq);
242     TWEQ_INIT(sc, TWEQ_BIO);
243 }
244 
245 static __inline void
246 twe_enqueue_bio(struct twe_softc *sc, twe_bio *bp)
247 {
248     int		s;
249 
250     s = splbio();
251     TWE_BIO_QINSERT(sc->twe_bioq, bp);
252     TWEQ_ADD(sc, TWEQ_BIO);
253     splx(s);
254 }
255 
256 static __inline twe_bio *
257 twe_dequeue_bio(struct twe_softc *sc)
258 {
259     int		s;
260     twe_bio	*bp;
261 
262     s = splbio();
263     if ((bp = TWE_BIO_QFIRST(sc->twe_bioq)) != NULL) {
264 	TWE_BIO_QREMOVE(sc->twe_bioq, bp);
265 	TWEQ_REMOVE(sc, TWEQ_BIO);
266     }
267     splx(s);
268     return(bp);
269 }
270