aacvar.h revision 81082
1/*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2001 Scott Long
4 * Copyright (c) 2000 BSDi
5 * Copyright (c) 2001 Adaptec, Inc.
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: head/sys/dev/aac/aacvar.h 81082 2001-08-03 00:50:30Z scottl $
30 */
31
32/******************************************************************************
33 ******************************************************************************
34			Driver Parameter Definitions
35 ******************************************************************************
36 ******************************************************************************/
37
38/*
39 * The firmware interface allows for a 16-bit s/g list length.  We limit
40 * ourselves to a reasonable maximum and ensure alignment.
41 */
42#define AAC_MAXSGENTRIES	64	/* max S/G entries, limit 65535 */
43
44/*
45 * We allocate a small set of FIBs for the adapter to use to send us messages.
46 */
47#define AAC_ADAPTER_FIBS	8
48
49/*
50 * FIBs are allocated up-front, and the pool isn't grown.  We should allocate
51 * enough here to let us keep the adapter busy without wasting large amounts
52 * of kernel memory.  The current interface implementation limits us to 512
53 * FIBs queued for the adapter at any one time.
54 */
55#define AAC_FIB_COUNT		128
56
57/*
58 * The controller reports status events in AIFs.  We hang on to a number of
59 * these in order to pass them out to user-space management tools.
60 */
61#define AAC_AIFQ_LENGTH		64
62
63/*
64 * Firmware messages are passed in the printf buffer.
65 */
66#define AAC_PRINTF_BUFSIZE	256
67
68/*
69 * We wait this many seconds for the adapter to come ready if it is still
70 * booting
71 */
72#define AAC_BOOT_TIMEOUT	(3 * 60)
73
74/*
75 * Timeout for immediate commands.
76 */
77#define AAC_IMMEDIATE_TIMEOUT	30		/* seconds */
78
79/*
80 * Timeout for normal commands
81 */
82#define AAC_CMD_TIMEOUT		30		/* seconds */
83
84/*
85 * Rate at which we periodically check for timed out commands and kick the
86 * controller.
87 */
88#define AAC_PERIODIC_INTERVAL	10		/* seconds */
89
90/*
91 * Character device major numbers.
92 */
93#define AAC_DISK_MAJOR	200
94
95/******************************************************************************
96 ******************************************************************************
97			Driver Variable Definitions
98 ******************************************************************************
99 ******************************************************************************/
100
101#include "opt_aac.h"
102
103#if __FreeBSD_version >= 500005
104# include <sys/taskqueue.h>
105#endif
106
107/*
108 * Per-container data structure
109 */
110struct aac_container
111{
112    struct aac_mntobj	co_mntobj;
113    device_t		co_disk;
114};
115
116/*
117 * Per-disk structure
118 */
119struct aac_disk
120{
121    device_t			ad_dev;
122    dev_t			ad_dev_t;
123    struct aac_softc		*ad_controller;
124    struct aac_container	*ad_container;
125    struct disk			ad_disk;
126    struct devstat		ad_stats;
127    struct disklabel		ad_label;
128    int				ad_flags;
129#define AAC_DISK_OPEN	(1<<0)
130    int				ad_cylinders;
131    int				ad_heads;
132    int				ad_sectors;
133    u_int32_t			ad_size;
134};
135
136/*
137 * Per-command control structure.
138 */
139struct aac_command
140{
141    TAILQ_ENTRY(aac_command)	cm_link;	/* list linkage */
142
143    struct aac_softc		*cm_sc;		/* controller that owns us */
144
145    struct aac_fib		*cm_fib;	/* FIB associated with this
146						 * command */
147    u_int32_t			cm_fibphys;	/* bus address of the FIB */
148    struct bio			*cm_data;	/* pointer to data in kernel
149						 * space */
150    u_int32_t			cm_datalen;	/* data length */
151    bus_dmamap_t		cm_datamap;	/* DMA map for bio data */
152    struct aac_sg_table		*cm_sgtable;	/* pointer to s/g table in
153						 * command */
154
155    int				cm_flags;
156#define AAC_CMD_MAPPED		(1<<0)		/* command has had its data
157						 * mapped */
158#define AAC_CMD_DATAIN		(1<<1)		/* command involves data moving
159						 * from controller to host */
160#define AAC_CMD_DATAOUT		(1<<2)		/* command involves data moving
161						 * from host to controller */
162#define AAC_CMD_COMPLETED	(1<<3)		/* command has been completed */
163#define AAC_CMD_TIMEDOUT	(1<<4)		/* command taken too long */
164
165    void			(* cm_complete)(struct aac_command *cm);
166    void			*cm_private;
167    time_t			cm_timestamp;	/* command creation time */
168};
169
170/*
171 * We gather a number of adapter-visible items into a single structure.
172 *
173 * The ordering of this strucure may be important; we copy the Linux driver:
174 *
175 * Adapter FIBs
176 * Init struct
177 * Queue headers (Comm Area)
178 * Printf buffer
179 *
180 * In addition, we add:
181 * Sync Fib
182 */
183struct aac_common {
184    /* fibs for the controller to send us messages */
185    struct aac_fib		ac_fibs[AAC_ADAPTER_FIBS];
186
187    /* the init structure */
188    struct aac_adapter_init	ac_init;
189
190    /* arena within which the queue structures are kept */
191    u_int8_t			ac_qbuf[sizeof(struct aac_queue_table) +
192					AAC_QUEUE_ALIGN];
193
194    /* buffer for text messages from the controller */
195    char		       	ac_printf[AAC_PRINTF_BUFSIZE];
196
197    /* fib for synchronous commands */
198    struct aac_fib		ac_sync_fib;
199};
200
201/*
202 * Interface operations
203 */
204struct aac_interface
205{
206    int		(* aif_get_fwstatus)(struct aac_softc *sc);
207    void	(* aif_qnotify)(struct aac_softc *sc, int qbit);
208    int		(* aif_get_istatus)(struct aac_softc *sc);
209    void	(* aif_set_istatus)(struct aac_softc *sc, int mask);
210    void	(* aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
211				    u_int32_t arg0, u_int32_t arg1,
212				    u_int32_t arg2, u_int32_t arg3);
213    int		(* aif_get_mailboxstatus)(struct aac_softc *sc);
214    void	(* aif_set_interrupts)(struct aac_softc *sc, int enable);
215};
216extern struct aac_interface	aac_rx_interface;
217extern struct aac_interface	aac_sa_interface;
218
219#define AAC_GET_FWSTATUS(sc)		((sc)->aac_if.aif_get_fwstatus((sc)))
220#define AAC_QNOTIFY(sc, qbit)		((sc)->aac_if.aif_qnotify((sc), (qbit)))
221#define AAC_GET_ISTATUS(sc)		((sc)->aac_if.aif_get_istatus((sc)))
222#define AAC_CLEAR_ISTATUS(sc, mask)	((sc)->aac_if.aif_set_istatus((sc), \
223					(mask)))
224#define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
225	((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
226	(arg3)))
227#define AAC_GET_MAILBOXSTATUS(sc)	((sc)->aac_if.aif_get_mailboxstatus((sc)))
228#define	AAC_MASK_INTERRUPTS(sc)		((sc)->aac_if.aif_set_interrupts((sc), 0))
229#define AAC_UNMASK_INTERRUPTS(sc)	((sc)->aac_if.aif_set_interrupts((sc), 1))
230
231#define AAC_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag, \
232					sc->aac_bhandle, reg, val)
233#define AAC_GETREG4(sc, reg)		bus_space_read_4 (sc->aac_btag, \
234					sc->aac_bhandle, reg)
235#define AAC_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag, \
236					sc->aac_bhandle, reg, val)
237#define AAC_GETREG2(sc, reg)		bus_space_read_2 (sc->aac_btag, \
238					sc->aac_bhandle, reg)
239#define AAC_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag, \
240					sc->aac_bhandle, reg, val)
241#define AAC_GETREG1(sc, reg)		bus_space_read_1 (sc->aac_btag, \
242					sc->aac_bhandle, reg)
243
244/*
245 * Per-controller structure.
246 */
247struct aac_softc
248{
249    /* bus connections */
250    device_t			aac_dev;
251    struct resource		*aac_regs_resource;	/* register interface
252							 * window */
253    int				aac_regs_rid;		/* resource ID */
254    bus_space_handle_t		aac_bhandle;		/* bus space handle */
255    bus_space_tag_t		aac_btag;		/* bus space tag */
256    bus_dma_tag_t		aac_parent_dmat;	/* parent DMA tag */
257    bus_dma_tag_t		aac_buffer_dmat;	/* data buffer/command
258							 * DMA tag */
259    struct resource		*aac_irq;		/* interrupt */
260    int				aac_irq_rid;
261    void			*aac_intr;		/* interrupt handle */
262
263    /* controller features, limits and status */
264    int				aac_state;
265#define AAC_STATE_SUSPEND	(1<<0)
266#define	AAC_STATE_OPEN		(1<<1)
267#define AAC_STATE_INTERRUPTS_ON	(1<<2)
268#define AAC_STATE_AIF_SLEEPER	(1<<3)
269    struct FsaRevision		aac_revision;
270
271    /* controller hardware interface */
272    int				aac_hwif;
273#define AAC_HWIF_I960RX		0
274#define AAC_HWIF_STRONGARM	1
275#define AAC_HWIF_UNKNOWN	-1
276    bus_dma_tag_t		aac_common_dmat;	/* common structure
277							 * DMA tag */
278    bus_dmamap_t		aac_common_dmamap;	/* common structure
279							 * DMA map */
280    struct aac_common		*aac_common;
281    u_int32_t			aac_common_busaddr;
282    struct aac_interface	aac_if;
283
284    /* command/fib resources */
285    bus_dma_tag_t		aac_fib_dmat;	/* DMA tag for allocing FIBs */
286    struct aac_fib		*aac_fibs;
287    bus_dmamap_t		aac_fibmap;
288    u_int32_t			aac_fibphys;
289    struct aac_command		aac_command[AAC_FIB_COUNT];
290
291    /* command management */
292    TAILQ_HEAD(,aac_command)	aac_free;	/* command structures
293						 * available for reuse */
294    TAILQ_HEAD(,aac_command)	aac_ready;	/* commands on hold for
295						 * controller resources */
296    TAILQ_HEAD(,aac_command)	aac_busy;
297    TAILQ_HEAD(,aac_command)	aac_complete;	/* commands which have been
298						 * returned by the controller */
299    struct bio_queue_head	aac_bioq;
300    struct aac_queue_table	*aac_queues;
301    struct aac_queue_entry	*aac_qentries[AAC_QUEUE_COUNT];
302
303    struct aac_qstat		aac_qstat[AACQ_COUNT];	/* queue statistics */
304
305    /* connected containters */
306    struct aac_container	aac_container[AAC_MAX_CONTAINERS];
307
308    /* delayed activity infrastructure */
309#if __FreeBSD_version >= 500005
310    struct task			aac_task_complete;	/* deferred-completion
311							 * task */
312#endif
313    struct intr_config_hook	aac_ich;
314
315    /* management interface */
316    dev_t			aac_dev_t;
317    struct aac_aif_command	aac_aifq[AAC_AIFQ_LENGTH];
318    int				aac_aifq_head;
319    int				aac_aifq_tail;
320};
321
322
323/*
324 * Public functions
325 */
326extern void		aac_free(struct aac_softc *sc);
327extern int		aac_attach(struct aac_softc *sc);
328extern int		aac_detach(device_t dev);
329extern int		aac_shutdown(device_t dev);
330extern int		aac_suspend(device_t dev);
331extern int		aac_resume(device_t dev);
332extern void		aac_intr(void *arg);
333extern devclass_t	aac_devclass;
334extern void		aac_submit_bio(struct bio *bp);
335extern void		aac_biodone(struct bio *bp);
336
337/*
338 * Debugging levels:
339 *  0 - quiet, only emit warnings
340 *  1 - noisy, emit major function points and things done
341 *  2 - extremely noisy, emit trace items in loops, etc.
342 */
343#ifdef AAC_DEBUG
344# define debug(level, fmt, args...)					\
345    do {								\
346	if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __FUNCTION__ , ##args); \
347    } while(0)
348# define debug_called(level)						\
349    do {								\
350	if (level <= AAC_DEBUG) printf(__FUNCTION__ ": called\n");	\
351    } while(0)
352
353extern void	aac_print_queues(struct aac_softc *sc);
354extern void	aac_panic(struct aac_softc *sc, char *reason);
355extern void	aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
356			      char *caller);
357extern void	aac_print_aif(struct aac_softc *sc,
358			      struct aac_aif_command *aif);
359
360# define AAC_PRINT_FIB(sc, fib)	aac_print_fib(sc, fib, __FUNCTION__)
361
362#else
363# define debug(level, fmt, args...)
364# define debug_called(level)
365
366# define aac_print_queues(sc)
367# define aac_panic(sc, reason)
368# define aac_print_aif(sc, aif)
369
370# define AAC_PRINT_FIB(sc, fib)
371#endif
372
373struct aac_code_lookup {
374    char	*string;
375    u_int32_t	code;
376};
377
378/******************************************************************************
379 * Queue primitives for driver queues.
380 */
381#define AACQ_ADD(sc, qname)					\
382	do {							\
383	    struct aac_qstat *qs = &(sc)->aac_qstat[qname];	\
384								\
385	    qs->q_length++;					\
386	    if (qs->q_length > qs->q_max)			\
387		qs->q_max = qs->q_length;			\
388	} while(0)
389
390#define AACQ_REMOVE(sc, qname)    (sc)->aac_qstat[qname].q_length--
391#define AACQ_INIT(sc, qname)			\
392	do {					\
393	    sc->aac_qstat[qname].q_length = 0;	\
394	    sc->aac_qstat[qname].q_max = 0;	\
395	} while(0)
396
397
398#define AACQ_COMMAND_QUEUE(name, index)					\
399static __inline void							\
400aac_initq_ ## name (struct aac_softc *sc)				\
401{									\
402    TAILQ_INIT(&sc->aac_ ## name);					\
403    AACQ_INIT(sc, index);						\
404}									\
405static __inline void							\
406aac_enqueue_ ## name (struct aac_command *cm)				\
407{									\
408    int		s;							\
409									\
410    s = splbio();							\
411    TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
412    AACQ_ADD(cm->cm_sc, index);						\
413    splx(s);								\
414}									\
415static __inline void							\
416aac_requeue_ ## name (struct aac_command *cm)				\
417{									\
418    int		s;							\
419									\
420    s = splbio();							\
421    TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
422    AACQ_ADD(cm->cm_sc, index);						\
423    splx(s);								\
424}									\
425static __inline struct aac_command *					\
426aac_dequeue_ ## name (struct aac_softc *sc)				\
427{									\
428    struct aac_command	*cm;						\
429    int			s;						\
430									\
431    s = splbio();							\
432    if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) {		\
433	TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link);			\
434	AACQ_REMOVE(sc, index);						\
435    }									\
436    splx(s);								\
437    return(cm);								\
438}									\
439static __inline void							\
440aac_remove_ ## name (struct aac_command *cm)				\
441{									\
442    int			s;						\
443									\
444    s = splbio();							\
445    TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
446    AACQ_REMOVE(cm->cm_sc, index);					\
447    splx(s);								\
448}									\
449struct hack
450
451AACQ_COMMAND_QUEUE(free, AACQ_FREE);
452AACQ_COMMAND_QUEUE(ready, AACQ_READY);
453AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
454AACQ_COMMAND_QUEUE(complete, AACQ_COMPLETE);
455
456/*
457 * outstanding bio queue
458 */
459static __inline void
460aac_initq_bio(struct aac_softc *sc)
461{
462    bioq_init(&sc->aac_bioq);
463    AACQ_INIT(sc, AACQ_BIO);
464}
465
466static __inline void
467aac_enqueue_bio(struct aac_softc *sc, struct bio *bp)
468{
469    int		s;
470
471    s = splbio();
472    bioq_insert_tail(&sc->aac_bioq, bp);
473    AACQ_ADD(sc, AACQ_BIO);
474    splx(s);
475}
476
477static __inline struct bio *
478aac_dequeue_bio(struct aac_softc *sc)
479{
480    int		s;
481    struct bio	*bp;
482
483    s = splbio();
484    if ((bp = bioq_first(&sc->aac_bioq)) != NULL) {
485	bioq_remove(&sc->aac_bioq, bp);
486	AACQ_REMOVE(sc, AACQ_BIO);
487    }
488    splx(s);
489    return(bp);
490}
491