aacvar.h revision 82527
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 82527 2001-08-29 23:34:05Z 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	20		/* 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#if __FreeBSD_version >= 500005
102# include <sys/taskqueue.h>
103#endif
104
105/*
106 * Per-container data structure
107 */
108struct aac_container
109{
110    struct aac_mntobj		co_mntobj;
111    device_t			co_disk;
112    int				co_found;
113    TAILQ_ENTRY(aac_container)	co_link;
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    int				unit;
135};
136
137/*
138 * Per-command control structure.
139 */
140struct aac_command
141{
142    TAILQ_ENTRY(aac_command)	cm_link;	/* list linkage */
143
144    struct aac_softc		*cm_sc;		/* controller that owns us */
145
146    struct aac_fib		*cm_fib;	/* FIB associated with this
147						 * command */
148    u_int32_t			cm_fibphys;	/* bus address of the FIB */
149    struct bio			*cm_data;	/* pointer to data in kernel
150						 * space */
151    u_int32_t			cm_datalen;	/* data length */
152    bus_dmamap_t		cm_datamap;	/* DMA map for bio data */
153    struct aac_sg_table		*cm_sgtable;	/* pointer to s/g table in
154						 * command */
155
156    int				cm_flags;
157#define AAC_CMD_MAPPED		(1<<0)		/* command has had its data
158						 * mapped */
159#define AAC_CMD_DATAIN		(1<<1)		/* command involves data moving
160						 * from controller to host */
161#define AAC_CMD_DATAOUT		(1<<2)		/* command involves data moving
162						 * from host to controller */
163#define AAC_CMD_COMPLETED	(1<<3)		/* command has been completed */
164#define AAC_CMD_TIMEDOUT	(1<<4)		/* command taken too long */
165#define AAC_ON_AACQ_FREE	(1<<5)
166#define AAC_ON_AACQ_READY	(1<<6)
167#define AAC_ON_AACQ_BUSY	(1<<7)
168#define AAC_ON_AACQ_COMPLETE	(1<<8)
169#define AAC_ON_AACQ_MASK	((1<<5)|(1<<6)|(1<<7)|(1<<8))
170
171    void			(* cm_complete)(struct aac_command *cm);
172    void			*cm_private;
173    time_t			cm_timestamp;	/* command creation time */
174    int				cm_queue;
175};
176
177/*
178 * We gather a number of adapter-visible items into a single structure.
179 *
180 * The ordering of this strucure may be important; we copy the Linux driver:
181 *
182 * Adapter FIBs
183 * Init struct
184 * Queue headers (Comm Area)
185 * Printf buffer
186 *
187 * In addition, we add:
188 * Sync Fib
189 */
190struct aac_common {
191    /* fibs for the controller to send us messages */
192    struct aac_fib		ac_fibs[AAC_ADAPTER_FIBS];
193
194    /* the init structure */
195    struct aac_adapter_init	ac_init;
196
197    /* arena within which the queue structures are kept */
198    u_int8_t			ac_qbuf[sizeof(struct aac_queue_table) +
199					AAC_QUEUE_ALIGN];
200
201    /* buffer for text messages from the controller */
202    char		       	ac_printf[AAC_PRINTF_BUFSIZE];
203
204    /* fib for synchronous commands */
205    struct aac_fib		ac_sync_fib;
206};
207
208/*
209 * Interface operations
210 */
211struct aac_interface
212{
213    int		(* aif_get_fwstatus)(struct aac_softc *sc);
214    void	(* aif_qnotify)(struct aac_softc *sc, int qbit);
215    int		(* aif_get_istatus)(struct aac_softc *sc);
216    void	(* aif_set_istatus)(struct aac_softc *sc, int mask);
217    void	(* aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
218				    u_int32_t arg0, u_int32_t arg1,
219				    u_int32_t arg2, u_int32_t arg3);
220    int		(* aif_get_mailboxstatus)(struct aac_softc *sc);
221    void	(* aif_set_interrupts)(struct aac_softc *sc, int enable);
222};
223extern struct aac_interface	aac_rx_interface;
224extern struct aac_interface	aac_sa_interface;
225
226#define AAC_GET_FWSTATUS(sc)		((sc)->aac_if.aif_get_fwstatus((sc)))
227#define AAC_QNOTIFY(sc, qbit)		((sc)->aac_if.aif_qnotify((sc), (qbit)))
228#define AAC_GET_ISTATUS(sc)		((sc)->aac_if.aif_get_istatus((sc)))
229#define AAC_CLEAR_ISTATUS(sc, mask)	((sc)->aac_if.aif_set_istatus((sc), \
230					(mask)))
231#define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
232	((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
233	(arg3)))
234#define AAC_GET_MAILBOXSTATUS(sc)	((sc)->aac_if.aif_get_mailboxstatus((sc)))
235#define	AAC_MASK_INTERRUPTS(sc)		((sc)->aac_if.aif_set_interrupts((sc), 0))
236#define AAC_UNMASK_INTERRUPTS(sc)	((sc)->aac_if.aif_set_interrupts((sc), 1))
237
238#define AAC_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag, \
239					sc->aac_bhandle, reg, val)
240#define AAC_GETREG4(sc, reg)		bus_space_read_4 (sc->aac_btag, \
241					sc->aac_bhandle, reg)
242#define AAC_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag, \
243					sc->aac_bhandle, reg, val)
244#define AAC_GETREG2(sc, reg)		bus_space_read_2 (sc->aac_btag, \
245					sc->aac_bhandle, reg)
246#define AAC_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag, \
247					sc->aac_bhandle, reg, val)
248#define AAC_GETREG1(sc, reg)		bus_space_read_1 (sc->aac_btag, \
249					sc->aac_bhandle, reg)
250
251TAILQ_HEAD(aac_container_tq, aac_container);
252
253/* Define the OS version specific locks */
254#if __FreeBSD_version >= 500005
255#include <sys/lock.h>
256#include <sys/mutex.h>
257typedef struct mtx aac_lock_t;
258#define AAC_LOCK_INIT(l)	mtx_init(l, "AAC Container Mutex", MTX_DEF)
259#define AAC_LOCK_AQUIRE(l)	mtx_lock(l)
260#define AAC_LOCK_RELEASE(l)	mtx_unlock(l)
261#else
262typedef struct simplelock aac_lock_t;
263#define AAC_LOCK_INIT(l)	simple_lock_init(l)
264#define AAC_LOCK_AQUIRE(l)	simple_lock(l)
265#define AAC_LOCK_RELEASE(l)	simple_unlock(l)
266#endif
267
268/*
269 * Per-controller structure.
270 */
271struct aac_softc
272{
273    /* bus connections */
274    device_t			aac_dev;
275    struct resource		*aac_regs_resource;	/* register interface
276							 * window */
277    int				aac_regs_rid;		/* resource ID */
278    bus_space_handle_t		aac_bhandle;		/* bus space handle */
279    bus_space_tag_t		aac_btag;		/* bus space tag */
280    bus_dma_tag_t		aac_parent_dmat;	/* parent DMA tag */
281    bus_dma_tag_t		aac_buffer_dmat;	/* data buffer/command
282							 * DMA tag */
283    struct resource		*aac_irq;		/* interrupt */
284    int				aac_irq_rid;
285    void			*aac_intr;		/* interrupt handle */
286
287    /* controller features, limits and status */
288    int				aac_state;
289#define AAC_STATE_SUSPEND	(1<<0)
290#define	AAC_STATE_OPEN		(1<<1)
291#define AAC_STATE_INTERRUPTS_ON	(1<<2)
292#define AAC_STATE_AIF_SLEEPER	(1<<3)
293    struct FsaRevision		aac_revision;
294
295    /* controller hardware interface */
296    int				aac_hwif;
297#define AAC_HWIF_I960RX		0
298#define AAC_HWIF_STRONGARM	1
299#define AAC_HWIF_UNKNOWN	-1
300    bus_dma_tag_t		aac_common_dmat;	/* common structure
301							 * DMA tag */
302    bus_dmamap_t		aac_common_dmamap;	/* common structure
303							 * DMA map */
304    struct aac_common		*aac_common;
305    u_int32_t			aac_common_busaddr;
306    struct aac_interface	aac_if;
307
308    /* command/fib resources */
309    bus_dma_tag_t		aac_fib_dmat;	/* DMA tag for allocing FIBs */
310    struct aac_fib		*aac_fibs;
311    bus_dmamap_t		aac_fibmap;
312    u_int32_t			aac_fibphys;
313    struct aac_command		aac_command[AAC_FIB_COUNT];
314
315    /* command management */
316    TAILQ_HEAD(,aac_command)	aac_free;	/* command structures
317						 * available for reuse */
318    TAILQ_HEAD(,aac_command)	aac_ready;	/* commands on hold for
319						 * controller resources */
320    TAILQ_HEAD(,aac_command)	aac_busy;
321    TAILQ_HEAD(,aac_command)	aac_complete;	/* commands which have been
322						 * returned by the controller */
323    struct bio_queue_head	aac_bioq;
324    struct aac_queue_table	*aac_queues;
325    struct aac_queue_entry	*aac_qentries[AAC_QUEUE_COUNT];
326
327    struct aac_qstat		aac_qstat[AACQ_COUNT];	/* queue statistics */
328
329    /* connected containters */
330    struct aac_container_tq	aac_container_tqh;
331    aac_lock_t			aac_container_lock;
332
333    /* delayed activity infrastructure */
334#if __FreeBSD_version >= 500005
335    struct task			aac_task_complete;	/* deferred-completion
336							 * task */
337#endif
338    struct intr_config_hook	aac_ich;
339
340    /* management interface */
341    dev_t			aac_dev_t;
342    struct aac_aif_command	aac_aifq[AAC_AIFQ_LENGTH];
343    int				aac_aifq_head;
344    int				aac_aifq_tail;
345    struct proc			*aifthread;
346    int				aifflags;
347#define AAC_AIFFLAGS_RUNNING	(1 << 0)
348#define AAC_AIFFLAGS_PENDING	(1 << 1)
349#define	AAC_AIFFLAGS_EXIT	(1 << 2)
350#define AAC_AIFFLAGS_EXITED	(1 << 3)
351};
352
353
354/*
355 * Public functions
356 */
357extern void		aac_free(struct aac_softc *sc);
358extern int		aac_attach(struct aac_softc *sc);
359extern int		aac_detach(device_t dev);
360extern int		aac_shutdown(device_t dev);
361extern int		aac_suspend(device_t dev);
362extern int		aac_resume(device_t dev);
363extern void		aac_intr(void *arg);
364extern devclass_t	aac_devclass;
365extern void		aac_submit_bio(struct bio *bp);
366extern void		aac_biodone(struct bio *bp);
367extern int		aac_dump_enqueue(struct aac_disk *ad, u_int32_t lba,
368					 void *data, int nblks);
369extern void		aac_dump_complete(struct aac_softc *sc);
370
371/*
372 * Debugging levels:
373 *  0 - quiet, only emit warnings
374 *  1 - noisy, emit major function points and things done
375 *  2 - extremely noisy, emit trace items in loops, etc.
376 */
377#ifdef AAC_DEBUG
378# define debug(level, fmt, args...)					\
379    do {								\
380	if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __FUNCTION__ , ##args); \
381    } while(0)
382# define debug_called(level)						\
383    do {								\
384	if (level <= AAC_DEBUG) printf(__FUNCTION__ ": called\n");	\
385    } while(0)
386
387extern void	aac_print_queues(struct aac_softc *sc);
388extern void	aac_panic(struct aac_softc *sc, char *reason);
389extern void	aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
390			      char *caller);
391extern void	aac_print_aif(struct aac_softc *sc,
392			      struct aac_aif_command *aif);
393
394# define AAC_PRINT_FIB(sc, fib)	aac_print_fib(sc, fib, __FUNCTION__)
395
396#else
397# define debug(level, fmt, args...)
398# define debug_called(level)
399
400# define aac_print_queues(sc)
401# define aac_panic(sc, reason)
402
403# define AAC_PRINT_FIB(sc, fib)
404# define aac_print_aif(sc, aac_aif_command)
405#endif
406
407struct aac_code_lookup {
408    char	*string;
409    u_int32_t	code;
410};
411
412/******************************************************************************
413 * Queue primitives for driver queues.
414 */
415#define AACQ_ADD(sc, qname)					\
416	do {							\
417	    struct aac_qstat *qs = &(sc)->aac_qstat[qname];	\
418								\
419	    qs->q_length++;					\
420	    if (qs->q_length > qs->q_max)			\
421		qs->q_max = qs->q_length;			\
422	} while(0)
423
424#define AACQ_REMOVE(sc, qname)    (sc)->aac_qstat[qname].q_length--
425#define AACQ_INIT(sc, qname)			\
426	do {					\
427	    sc->aac_qstat[qname].q_length = 0;	\
428	    sc->aac_qstat[qname].q_max = 0;	\
429	} while(0)
430
431
432#define AACQ_COMMAND_QUEUE(name, index)					\
433static __inline void							\
434aac_initq_ ## name (struct aac_softc *sc)				\
435{									\
436    TAILQ_INIT(&sc->aac_ ## name);					\
437    AACQ_INIT(sc, index);						\
438}									\
439static __inline void							\
440aac_enqueue_ ## name (struct aac_command *cm)				\
441{									\
442    int		s;							\
443									\
444    s = splbio();							\
445    if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
446	printf("command %p is on another queue, flags = %#x\n",		\
447	       cm, cm->cm_flags);					\
448	panic("command is on another queue");				\
449    }									\
450    TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
451    cm->cm_flags |= AAC_ON_ ## index;					\
452    AACQ_ADD(cm->cm_sc, index);						\
453    splx(s);								\
454}									\
455static __inline void							\
456aac_requeue_ ## name (struct aac_command *cm)				\
457{									\
458    int		s;							\
459									\
460    s = splbio();							\
461    if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
462	printf("command %p is on another queue, flags = %#x\n",		\
463	       cm, cm->cm_flags);					\
464	panic("command is on another queue");				\
465    }									\
466    TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
467    cm->cm_flags |= AAC_ON_ ## index;					\
468    AACQ_ADD(cm->cm_sc, index);						\
469    splx(s);								\
470}									\
471static __inline struct aac_command *					\
472aac_dequeue_ ## name (struct aac_softc *sc)				\
473{									\
474    struct aac_command	*cm;						\
475    int			s;						\
476									\
477    s = splbio();							\
478    if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) {		\
479	if ((cm->cm_flags & AAC_ON_ ## index) == 0) {			\
480		printf("command %p not in queue, flags = %#x, bit = %#x\n",\
481		       cm, cm->cm_flags, AAC_ON_ ## index);		\
482		panic("command not in queue");				\
483	}								\
484	TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link);			\
485	cm->cm_flags &= ~AAC_ON_ ## index;				\
486	AACQ_REMOVE(sc, index);						\
487    }									\
488    splx(s);								\
489    return(cm);								\
490}									\
491static __inline void							\
492aac_remove_ ## name (struct aac_command *cm)				\
493{									\
494    int			s;						\
495									\
496    s = splbio();							\
497    if ((cm->cm_flags & AAC_ON_ ## index) == 0) {			\
498	printf("command %p not in queue, flags = %#x, bit = %#x\n",	\
499	       cm, cm->cm_flags, AAC_ON_ ## index);			\
500	panic("command not in queue");					\
501    }									\
502    TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
503    cm->cm_flags &= ~AAC_ON_ ## index;					\
504    AACQ_REMOVE(cm->cm_sc, index);					\
505    splx(s);								\
506}									\
507struct hack
508
509AACQ_COMMAND_QUEUE(free, AACQ_FREE);
510AACQ_COMMAND_QUEUE(ready, AACQ_READY);
511AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
512AACQ_COMMAND_QUEUE(complete, AACQ_COMPLETE);
513
514/*
515 * outstanding bio queue
516 */
517static __inline void
518aac_initq_bio(struct aac_softc *sc)
519{
520    bioq_init(&sc->aac_bioq);
521    AACQ_INIT(sc, AACQ_BIO);
522}
523
524static __inline void
525aac_enqueue_bio(struct aac_softc *sc, struct bio *bp)
526{
527    int		s;
528
529    s = splbio();
530    bioq_insert_tail(&sc->aac_bioq, bp);
531    AACQ_ADD(sc, AACQ_BIO);
532    splx(s);
533}
534
535static __inline struct bio *
536aac_dequeue_bio(struct aac_softc *sc)
537{
538    int		s;
539    struct bio	*bp;
540
541    s = splbio();
542    if ((bp = bioq_first(&sc->aac_bioq)) != NULL) {
543	bioq_remove(&sc->aac_bioq, bp);
544	AACQ_REMOVE(sc, AACQ_BIO);
545    }
546    splx(s);
547    return(bp);
548}
549
550static __inline void
551aac_print_printf(struct aac_softc *sc)
552{
553    if (sc->aac_common->ac_printf[0]) {
554	device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
555		      sc->aac_common->ac_printf);
556	sc->aac_common->ac_printf[0] = 0;
557	AAC_QNOTIFY(sc, AAC_DB_PRINTF);
558    }
559}
560