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