xref: /dragonfly/sys/dev/raid/twe/twevar.h (revision bcb3e04d)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2003 Paul Saab
4  * Copyright (c) 2003 Vinod Kashyap
5  * Copyright (c) 2000 BSDi
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$FreeBSD: src/sys/dev/twe/twevar.h,v 1.1.2.8 2004/06/11 18:57:32 vkashyap Exp $
30  */
31 
32 #define TWE_DRIVER_VERSION_STRING	"1.40.01.002"
33 
34 #ifdef TWE_DEBUG
35 #define debug(level, fmt, args...)							\
36 	do {										\
37 	    if (level <= TWE_DEBUG) kprintf("%s: " fmt "\n", __func__ , ##args);	\
38 	} while(0)
39 #define debug_called(level)						\
40 	do {								\
41 	    if (level <= TWE_DEBUG) kprintf("%s: called\n", __func__);	\
42 	} while(0)
43 #else
44 #define debug(level, fmt, args...)
45 #define debug_called(level)
46 #endif
47 
48 /*
49  * Structure describing a logical unit as attached to the controller
50  */
51 struct twe_drive
52 {
53     /* unit properties */
54     u_int32_t		td_size;
55     int			td_cylinders;
56     int			td_heads;
57     int			td_sectors;
58     int			td_sys_unit; /* Unit #, as seen by the system
59 				     (the 0 in twed0, the 1 in twed1 etc.) */
60     int			td_twe_unit; /* Unit #, as seen by us, and our ctlr
61 				     (index into sc->twe_drive[]) */
62 
63     /* unit state and type */
64     u_int8_t		td_state;
65     u_int8_t		td_type;
66 
67     /* handle for attached driver */
68     device_t		td_disk;
69 };
70 
71 /*
72  * Disk device softc
73  */
74 struct twed_softc
75 {
76     device_t		twed_dev;
77     cdev_t		twed_dev_t;
78     struct twe_softc	*twed_controller;	/* parent device softc */
79     struct twe_drive	*twed_drive;		/* drive data in parent softc */
80     struct disk		twed_disk;		/* generic disk handle */
81     struct devstat	twed_stats;		/* accounting */
82     int			twed_flags;
83 #define TWED_OPEN	(1<<0)			/* drive is open (can't shut down) */
84 };
85 
86 /*
87  * Per-command control structure.
88  *
89  * Note that due to alignment constraints on the tc_command field, this *must* be 64-byte aligned.
90  * We achieve this by placing the command at the beginning of the structure, and using malloc()
91  * to allocate each structure.
92  */
93 struct twe_request
94 {
95     /* controller command */
96     TWE_Command			tr_command;	/* command as submitted to controller */
97 
98     /* command payload */
99     void			*tr_data;	/* data buffer */
100     void			*tr_realdata;	/* copy of real data buffer pointer for alignment fixup */
101     size_t			tr_length;
102 
103     TAILQ_ENTRY(twe_request)	tr_link;	/* list linkage */
104     struct twe_softc		*tr_sc;		/* controller that owns us */
105     int				tr_status;	/* command status */
106 #define TWE_CMD_SETUP		0	/* being assembled */
107 #define TWE_CMD_BUSY		1	/* submitted to controller */
108 #define TWE_CMD_COMPLETE	2	/* completed by controller (maybe with error) */
109 #define TWE_CMD_ERROR		3	/* encountered error, even before submission to controller */
110     int				tr_flags;
111 #define TWE_CMD_DATAIN		(1<<0)
112 #define TWE_CMD_DATAOUT		(1<<1)
113 #define TWE_CMD_ALIGNBUF	(1<<2)	/* data in bio is misaligned, have to copy to/from private buffer */
114 #define TWE_CMD_SLEEPER		(1<<3)	/* owner is sleeping on this command */
115 #define TWE_CMD_MAPPED		(1<<4)	/* cmd has been mapped */
116 #define TWE_CMD_IN_PROGRESS	(1<<5)	/* bus_dmamap_load returned EINPROGRESS */
117     void			(* tr_complete)(struct twe_request *tr);	/* completion handler */
118     void			*tr_private;	/* submitter-private data or wait channel */
119 
120     TWE_PLATFORM_REQUEST	/* platform-specific request elements */
121 };
122 
123 /*
124  * Per-controller state.
125  */
126 struct twe_softc
127 {
128     /* controller queues and arrays */
129     TAILQ_HEAD(, twe_request)	twe_free;			/* command structures available for reuse */
130     twe_bioq 			twe_bioq;			/* outstanding I/O operations */
131     TAILQ_HEAD(, twe_request)	twe_ready;			/* requests ready for the controller */
132     TAILQ_HEAD(, twe_request)	twe_busy;			/* requests busy in the controller */
133     TAILQ_HEAD(, twe_request)	twe_complete;			/* active commands (busy or waiting for completion) */
134     struct twe_request		*twe_lookup[TWE_Q_LENGTH];	/* commands indexed by request ID */
135     struct twe_drive	twe_drive[TWE_MAX_UNITS];		/* attached drives */
136 
137     /* asynchronous event handling */
138     u_int16_t		twe_aen_queue[TWE_Q_LENGTH];	/* AENs queued for userland tool(s) */
139     int			twe_aen_head, twe_aen_tail;	/* ringbuffer pointers for AEN queue */
140     int			twe_wait_aen;    		/* wait-for-aen notification */
141 
142     /* controller status */
143     int			twe_state;
144 #define TWE_STATE_INTEN		(1<<0)	/* interrupts have been enabled */
145 #define TWE_STATE_SHUTDOWN	(1<<1)	/* controller is shut down */
146 #define TWE_STATE_OPEN		(1<<2)	/* control device is open */
147 #define TWE_STATE_SUSPEND	(1<<3)	/* controller is suspended */
148 #define TWE_STATE_FRZN		(1<<4)	/* got EINPROGRESS */
149 #define TWE_STATE_CTLR_BUSY	(1<<5)	/* controller cmd queue full */
150     int			twe_host_id;
151     struct twe_qstat	twe_qstat[TWEQ_COUNT];	/* queue statistics */
152 
153     TWE_PLATFORM_SOFTC		/* platform-specific softc elements */
154 };
155 
156 /*
157  * Interface betwen driver core and platform-dependant code.
158  */
159 extern int	twe_setup(struct twe_softc *sc);		/* do early driver/controller setup */
160 extern void	twe_init(struct twe_softc *sc);			/* init controller */
161 extern void	twe_deinit(struct twe_softc *sc);		/* stop controller */
162 extern void	twe_intr(struct twe_softc *sc);			/* hardware interrupt signalled */
163 extern void	twe_startio(struct twe_softc *sc);
164 extern int	twe_start(struct twe_request *tr);
165 extern int	twe_dump_blocks(struct twe_softc *sc, int unit,	/* crashdump block write */
166 				u_int64_t lba, void *data, int nblks);
167 extern int	twe_ioctl(struct twe_softc *sc, int cmd,
168 				  void *addr);			/* handle user request */
169 extern void	twe_describe_controller(struct twe_softc *sc);	/* print controller info */
170 extern char	*twe_describe_code(struct twe_code_lookup *table, u_int32_t code);
171 extern void	twe_print_controller(struct twe_softc *sc);
172 extern void	twe_enable_interrupts(struct twe_softc *sc);	/* enable controller interrupts */
173 extern void	twe_disable_interrupts(struct twe_softc *sc);	/* disable controller interrupts */
174 
175 extern int	twe_attach_drive(struct twe_softc *sc,
176 					 struct twe_drive *dr); /* attach drive when found in twe_init */
177 extern int	twe_detach_drive(struct twe_softc *sc,
178 					 int unit);		/* detach drive */
179 extern void	twe_clear_pci_parity_error(struct twe_softc *sc);
180 extern void	twe_clear_pci_abort(struct twe_softc *sc);
181 extern void	twed_intr(twe_bio *bp);				/* return bio from core */
182 extern struct	twe_request *twe_allocate_request(struct twe_softc *sc);/* allocate request structure */
183 extern int	twe_map_request(struct twe_request *tr);	/* make request visible to controller, do s/g */
184 extern void	twe_unmap_request(struct twe_request *tr);	/* cleanup after transfer, unmap */
185 
186 /********************************************************************************
187  * Queue primitives
188  */
189 #define TWEQ_ADD(sc, qname)					\
190 	do {							\
191 	    struct twe_qstat *qs = &(sc)->twe_qstat[qname];	\
192 								\
193 	    qs->q_length++;					\
194 	    if (qs->q_length > qs->q_max)			\
195 		qs->q_max = qs->q_length;			\
196 	} while(0)
197 
198 #define TWEQ_REMOVE(sc, qname)					\
199 	do {							\
200 	    struct twe_qstat *qs = &(sc)->twe_qstat[qname];	\
201 								\
202 	    qs->q_length--;					\
203 	    if (qs->q_length < qs->q_min)			\
204 		qs->q_min = qs->q_length;			\
205 	} while(0)
206 
207 #define TWEQ_INIT(sc, qname)				\
208 	do {						\
209 	    sc->twe_qstat[qname].q_length = 0;		\
210 	    sc->twe_qstat[qname].q_max = 0;		\
211 	    sc->twe_qstat[qname].q_min = 0xFFFFFFFF;	\
212 	} while(0)
213 
214 
215 #define TWEQ_REQUEST_QUEUE(name, index)					\
216 static __inline void							\
217 twe_initq_ ## name (struct twe_softc *sc)				\
218 {									\
219     TAILQ_INIT(&sc->twe_ ## name);					\
220     TWEQ_INIT(sc, index);						\
221 }									\
222 static __inline void							\
223 twe_enqueue_ ## name (struct twe_request *tr)				\
224 {									\
225     crit_enter();							\
226     TAILQ_INSERT_TAIL(&tr->tr_sc->twe_ ## name, tr, tr_link);		\
227     TWEQ_ADD(tr->tr_sc, index);						\
228     crit_exit();							\
229 }									\
230 static __inline void							\
231 twe_requeue_ ## name (struct twe_request *tr)				\
232 {									\
233     crit_enter();							\
234     TAILQ_INSERT_HEAD(&tr->tr_sc->twe_ ## name, tr, tr_link);		\
235     TWEQ_ADD(tr->tr_sc, index);						\
236     crit_exit();							\
237 }									\
238 static __inline struct twe_request *					\
239 twe_dequeue_ ## name (struct twe_softc *sc)				\
240 {									\
241     struct twe_request	*tr;						\
242 									\
243     crit_enter();							\
244     if ((tr = TAILQ_FIRST(&sc->twe_ ## name)) != NULL) {		\
245 	TAILQ_REMOVE(&sc->twe_ ## name, tr, tr_link);			\
246 	TWEQ_REMOVE(sc, index);						\
247     }									\
248     crit_exit();							\
249     return(tr);								\
250 }									\
251 static __inline void							\
252 twe_remove_ ## name (struct twe_request *tr)				\
253 {									\
254     crit_enter();							\
255     TAILQ_REMOVE(&tr->tr_sc->twe_ ## name, tr, tr_link);		\
256     TWEQ_REMOVE(tr->tr_sc, index);					\
257     crit_exit();							\
258 }
259 
260 TWEQ_REQUEST_QUEUE(free, TWEQ_FREE)
261 TWEQ_REQUEST_QUEUE(ready, TWEQ_READY)
262 TWEQ_REQUEST_QUEUE(busy, TWEQ_BUSY)
263 TWEQ_REQUEST_QUEUE(complete, TWEQ_COMPLETE)
264 
265 
266 /*
267  * outstanding bio queue
268  */
269 static __inline void
270 twe_initq_bio(struct twe_softc *sc)
271 {
272     bioq_init(&sc->twe_bioq);
273     TWEQ_INIT(sc, TWEQ_BIO);
274 }
275 
276 static __inline void
277 twe_enqueue_bio(struct twe_softc *sc, struct bio *bio)
278 {
279     crit_enter();
280     bioqdisksort(&sc->twe_bioq, bio);
281     /* bioq_insert_tail(&sc->twe_bioq, bio); */
282     TWEQ_ADD(sc, TWEQ_BIO);
283     crit_exit();
284 }
285 
286 static __inline
287 struct bio *
288 twe_dequeue_bio(struct twe_softc *sc)
289 {
290     struct bio *bio;
291 
292     crit_enter();
293     if ((bio = bioq_first(&sc->twe_bioq)) != NULL) {
294 	bioq_remove(&sc->twe_bioq, bio);
295 	TWEQ_REMOVE(sc, TWEQ_BIO);
296     }
297     crit_exit();
298     return(bio);
299 }
300