aacvar.h revision 83114
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 83114 2001-09-05 20:43:02Z scottl $
30 */
31
32/*
33 * Driver Parameter Definitions
34 */
35
36/*
37 * The firmware interface allows for a 16-bit s/g list length.  We limit
38 * ourselves to a reasonable maximum and ensure alignment.
39 */
40#define AAC_MAXSGENTRIES	64	/* max S/G entries, limit 65535 */
41
42/*
43 * We allocate a small set of FIBs for the adapter to use to send us messages.
44 */
45#define AAC_ADAPTER_FIBS	8
46
47/*
48 * FIBs are allocated up-front, and the pool isn't grown.  We should allocate
49 * enough here to let us keep the adapter busy without wasting large amounts
50 * of kernel memory.  The current interface implementation limits us to 512
51 * FIBs queued for the adapter at any one time.
52 */
53#define AAC_FIB_COUNT		128
54
55/*
56 * The controller reports status events in AIFs.  We hang on to a number of
57 * these in order to pass them out to user-space management tools.
58 */
59#define AAC_AIFQ_LENGTH		64
60
61/*
62 * Firmware messages are passed in the printf buffer.
63 */
64#define AAC_PRINTF_BUFSIZE	256
65
66/*
67 * We wait this many seconds for the adapter to come ready if it is still
68 * booting
69 */
70#define AAC_BOOT_TIMEOUT	(3 * 60)
71
72/*
73 * Timeout for immediate commands.
74 */
75#define AAC_IMMEDIATE_TIMEOUT	30		/* seconds */
76
77/*
78 * Timeout for normal commands
79 */
80#define AAC_CMD_TIMEOUT		30		/* seconds */
81
82/*
83 * Rate at which we periodically check for timed out commands and kick the
84 * controller.
85 */
86#define AAC_PERIODIC_INTERVAL	20		/* seconds */
87
88/*
89 * Character device major numbers.
90 */
91#define AAC_DISK_MAJOR	200
92
93/*
94 * Driver Variable Definitions
95 */
96
97#if __FreeBSD_version >= 500005
98# include <sys/taskqueue.h>
99#endif
100
101/*
102 * Per-container data structure
103 */
104struct aac_container
105{
106	struct aac_mntobj		co_mntobj;
107	device_t			co_disk;
108	int				co_found;
109	TAILQ_ENTRY(aac_container)	co_link;
110};
111
112/*
113 * Per-disk structure
114 */
115struct aac_disk
116{
117	device_t			ad_dev;
118	dev_t				ad_dev_t;
119	struct aac_softc		*ad_controller;
120	struct aac_container		*ad_container;
121	struct disk			ad_disk;
122	struct devstat			ad_stats;
123	struct disklabel		ad_label;
124	int				ad_flags;
125#define AAC_DISK_OPEN	(1<<0)
126	int				ad_cylinders;
127	int				ad_heads;
128	int				ad_sectors;
129	u_int32_t			ad_size;
130	int				unit;
131};
132
133/*
134 * Per-command control structure.
135 */
136struct aac_command
137{
138	TAILQ_ENTRY(aac_command) cm_link;	/* list linkage */
139
140	struct aac_softc	*cm_sc;		/* controller that owns us */
141
142	struct aac_fib		*cm_fib;	/* FIB associated with this
143						 * command */
144	u_int32_t		cm_fibphys;	/* bus address of the FIB */
145	struct bio		*cm_data;	/* pointer to data in kernel
146						 * space */
147	u_int32_t		cm_datalen;	/* data length */
148	bus_dmamap_t		cm_datamap;	/* DMA map for bio data */
149	struct aac_sg_table	*cm_sgtable;	/* pointer to s/g table in
150						 * command */
151	int			cm_flags;
152#define AAC_CMD_MAPPED		(1<<0)		/* command has had its data
153						 * mapped */
154#define AAC_CMD_DATAIN		(1<<1)		/* command involves data moving
155						 * from controller to host */
156#define AAC_CMD_DATAOUT		(1<<2)		/* command involves data moving
157						 * from host to controller */
158#define AAC_CMD_COMPLETED	(1<<3)		/* command has been completed */
159#define AAC_CMD_TIMEDOUT	(1<<4)		/* command taken too long */
160#define AAC_ON_AACQ_FREE	(1<<5)
161#define AAC_ON_AACQ_READY	(1<<6)
162#define AAC_ON_AACQ_BUSY	(1<<7)
163#define AAC_ON_AACQ_COMPLETE	(1<<8)
164#define AAC_ON_AACQ_MASK	((1<<5)|(1<<6)|(1<<7)|(1<<8))
165
166	void			(* cm_complete)(struct aac_command *cm);
167	void			*cm_private;
168	time_t			cm_timestamp;	/* command creation time */
169	int			cm_queue;
170};
171
172/*
173 * We gather a number of adapter-visible items into a single structure.
174 *
175 * The ordering of this strucure may be important; we copy the Linux driver:
176 *
177 * Adapter FIBs
178 * Init struct
179 * Queue headers (Comm Area)
180 * Printf buffer
181 *
182 * In addition, we add:
183 * Sync Fib
184 */
185struct aac_common {
186	/* fibs for the controller to send us messages */
187	struct aac_fib		ac_fibs[AAC_ADAPTER_FIBS];
188
189	/* the init structure */
190	struct aac_adapter_init	ac_init;
191
192	/* arena within which the queue structures are kept */
193	u_int8_t		ac_qbuf[sizeof(struct aac_queue_table) +
194				AAC_QUEUE_ALIGN];
195
196	/* buffer for text messages from the controller */
197	char		       	ac_printf[AAC_PRINTF_BUFSIZE];
198
199	/* fib for synchronous commands */
200	struct aac_fib		ac_sync_fib;
201};
202
203/*
204 * Interface operations
205 */
206struct aac_interface
207{
208	int	(*aif_get_fwstatus)(struct aac_softc *sc);
209	void	(*aif_qnotify)(struct aac_softc *sc, int qbit);
210	int	(*aif_get_istatus)(struct aac_softc *sc);
211	void	(*aif_set_istatus)(struct aac_softc *sc, int mask);
212	void	(*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
213				   u_int32_t arg0, u_int32_t arg1,
214				   u_int32_t arg2, u_int32_t arg3);
215	int	(*aif_get_mailboxstatus)(struct aac_softc *sc);
216	void	(*aif_set_interrupts)(struct aac_softc *sc, int enable);
217};
218extern struct aac_interface	aac_rx_interface;
219extern struct aac_interface	aac_sa_interface;
220
221#define AAC_GET_FWSTATUS(sc)		((sc)->aac_if.aif_get_fwstatus((sc)))
222#define AAC_QNOTIFY(sc, qbit)		((sc)->aac_if.aif_qnotify((sc), (qbit)))
223#define AAC_GET_ISTATUS(sc)		((sc)->aac_if.aif_get_istatus((sc)))
224#define AAC_CLEAR_ISTATUS(sc, mask)	((sc)->aac_if.aif_set_istatus((sc), \
225					(mask)))
226#define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
227	((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
228	(arg3)))
229#define AAC_GET_MAILBOXSTATUS(sc)	((sc)->aac_if.aif_get_mailboxstatus(  \
230					(sc)))
231#define	AAC_MASK_INTERRUPTS(sc)		((sc)->aac_if.aif_set_interrupts((sc), \
232					0))
233#define AAC_UNMASK_INTERRUPTS(sc)	((sc)->aac_if.aif_set_interrupts((sc), \
234					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
249TAILQ_HEAD(aac_container_tq, aac_container);
250
251/* Define the OS version specific locks */
252#if __FreeBSD_version >= 500005
253#include <sys/lock.h>
254#include <sys/mutex.h>
255typedef struct mtx aac_lock_t;
256#define AAC_LOCK_INIT(l)	mtx_init(l, "AAC Container Mutex", MTX_DEF)
257#define AAC_LOCK_AQUIRE(l)	mtx_lock(l)
258#define AAC_LOCK_RELEASE(l)	mtx_unlock(l)
259#else
260typedef struct simplelock aac_lock_t;
261#define AAC_LOCK_INIT(l)	simple_lock_init(l)
262#define AAC_LOCK_AQUIRE(l)	simple_lock(l)
263#define AAC_LOCK_RELEASE(l)	simple_unlock(l)
264#endif
265
266/*
267 * Per-controller structure.
268 */
269struct aac_softc
270{
271	/* bus connections */
272	device_t		aac_dev;
273	struct resource		*aac_regs_resource;	/* register interface
274							 * window */
275	int			aac_regs_rid;		/* resource ID */
276	bus_space_handle_t	aac_bhandle;		/* bus space handle */
277	bus_space_tag_t		aac_btag;		/* bus space tag */
278	bus_dma_tag_t		aac_parent_dmat;	/* parent DMA tag */
279	bus_dma_tag_t		aac_buffer_dmat;	/* data buffer/command
280							 * DMA tag */
281	struct resource		*aac_irq;		/* interrupt */
282	int			aac_irq_rid;
283	void			*aac_intr;		/* interrupt handle */
284
285	/* controller features, limits and status */
286	int			aac_state;
287#define AAC_STATE_SUSPEND	(1<<0)
288#define	AAC_STATE_OPEN		(1<<1)
289#define AAC_STATE_INTERRUPTS_ON	(1<<2)
290#define AAC_STATE_AIF_SLEEPER	(1<<3)
291	struct FsaRevision		aac_revision;
292
293	/* controller hardware interface */
294	int			aac_hwif;
295#define AAC_HWIF_I960RX		0
296#define AAC_HWIF_STRONGARM	1
297#define AAC_HWIF_UNKNOWN	-1
298	bus_dma_tag_t		aac_common_dmat;	/* common structure
299							 * DMA tag */
300	bus_dmamap_t		aac_common_dmamap;	/* common structure
301							 * DMA map */
302	struct aac_common	*aac_common;
303	u_int32_t		aac_common_busaddr;
304	struct aac_interface	aac_if;
305
306	/* command/fib resources */
307	bus_dma_tag_t		aac_fib_dmat;	/* DMA tag for allocing FIBs */
308	struct aac_fib		*aac_fibs;
309	bus_dmamap_t		aac_fibmap;
310	u_int32_t		aac_fibphys;
311	struct aac_command	aac_command[AAC_FIB_COUNT];
312
313	/* command management */
314	TAILQ_HEAD(,aac_command) aac_free;	/* command structures
315						 * available for reuse */
316	TAILQ_HEAD(,aac_command) aac_ready;	/* commands on hold for
317						 * controller resources */
318	TAILQ_HEAD(,aac_command) aac_busy;
319	TAILQ_HEAD(,aac_command) aac_complete;	/* commands which have been
320						 * returned by the controller */
321	struct bio_queue_head	aac_bioq;
322	struct aac_queue_table	*aac_queues;
323	struct aac_queue_entry	*aac_qentries[AAC_QUEUE_COUNT];
324
325	struct aac_qstat	aac_qstat[AACQ_COUNT];	/* queue statistics */
326
327	/* connected containters */
328	struct aac_container_tq	aac_container_tqh;
329	aac_lock_t		aac_container_lock;
330
331	/* delayed activity infrastructure */
332#if __FreeBSD_version >= 500005
333	struct task		aac_task_complete;	/* deferred-completion
334							 * task */
335#endif
336	struct intr_config_hook	aac_ich;
337
338	/* management interface */
339	dev_t			aac_dev_t;
340	struct aac_aif_command	aac_aifq[AAC_AIFQ_LENGTH];
341	int			aac_aifq_head;
342	int			aac_aifq_tail;
343	struct proc		*aifthread;
344	int			aifflags;
345#define AAC_AIFFLAGS_RUNNING	(1 << 0)
346#define AAC_AIFFLAGS_PENDING	(1 << 1)
347#define	AAC_AIFFLAGS_EXIT	(1 << 2)
348#define AAC_AIFFLAGS_EXITED	(1 << 3)
349};
350
351
352/*
353 * Public functions
354 */
355extern void		aac_free(struct aac_softc *sc);
356extern int		aac_attach(struct aac_softc *sc);
357extern int		aac_detach(device_t dev);
358extern int		aac_shutdown(device_t dev);
359extern int		aac_suspend(device_t dev);
360extern int		aac_resume(device_t dev);
361extern void		aac_intr(void *arg);
362extern devclass_t	aac_devclass;
363extern void		aac_submit_bio(struct bio *bp);
364extern void		aac_biodone(struct bio *bp);
365extern int		aac_dump_enqueue(struct aac_disk *ad, u_int32_t lba,
366					 void *data, int nblks);
367extern void		aac_dump_complete(struct aac_softc *sc);
368
369/*
370 * Debugging levels:
371 *  0 - quiet, only emit warnings
372 *  1 - noisy, emit major function points and things done
373 *  2 - extremely noisy, emit trace items in loops, etc.
374 */
375#ifdef AAC_DEBUG
376# define debug(level, fmt, args...)					\
377	do {								\
378	if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __FUNCTION__ , ##args); \
379	} while (0)
380# define debug_called(level)						\
381	do {								\
382	if (level <= AAC_DEBUG) printf(__FUNCTION__ ": called\n");	\
383	} while (0)
384
385extern void	aac_print_queues(struct aac_softc *sc);
386extern void	aac_panic(struct aac_softc *sc, char *reason);
387extern void	aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
388			      char *caller);
389extern void	aac_print_aif(struct aac_softc *sc,
390			      struct aac_aif_command *aif);
391
392# define AAC_PRINT_FIB(sc, fib)	aac_print_fib(sc, fib, __FUNCTION__)
393
394#else
395# define debug(level, fmt, args...)
396# define debug_called(level)
397
398# define aac_print_queues(sc)
399# define aac_panic(sc, reason)
400
401# define AAC_PRINT_FIB(sc, fib)
402# define aac_print_aif(sc, aac_aif_command)
403#endif
404
405struct aac_code_lookup {
406	char	*string;
407	u_int32_t	code;
408};
409
410/*
411 * Queue primitives for driver queues.
412 */
413#define AACQ_ADD(sc, qname)					\
414	do {							\
415		struct aac_qstat *qs;				\
416								\
417		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, "	\
481		       	       "bit = %#x\n", cm, cm->cm_flags,		\
482			       AAC_ON_ ## index);			\
483			panic("command not in queue");			\
484		}							\
485		TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link);		\
486		cm->cm_flags &= ~AAC_ON_ ## index;			\
487		AACQ_REMOVE(sc, index);					\
488	}								\
489	splx(s);							\
490	return(cm);							\
491}									\
492static __inline void							\
493aac_remove_ ## name (struct aac_command *cm)				\
494{									\
495	int s;								\
496									\
497	s = splbio();							\
498	if ((cm->cm_flags & AAC_ON_ ## index) == 0) {			\
499		printf("command %p not in queue, flags = %#x, "		\
500		       "bit = %#x\n", cm, cm->cm_flags, 		\
501		       AAC_ON_ ## index);				\
502		panic("command not in queue");				\
503	}								\
504	TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
505	cm->cm_flags &= ~AAC_ON_ ## index;				\
506	AACQ_REMOVE(cm->cm_sc, index);					\
507	splx(s);							\
508}									\
509struct hack
510
511AACQ_COMMAND_QUEUE(free, AACQ_FREE);
512AACQ_COMMAND_QUEUE(ready, AACQ_READY);
513AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
514AACQ_COMMAND_QUEUE(complete, AACQ_COMPLETE);
515
516/*
517 * outstanding bio queue
518 */
519static __inline void
520aac_initq_bio(struct aac_softc *sc)
521{
522	bioq_init(&sc->aac_bioq);
523	AACQ_INIT(sc, AACQ_BIO);
524}
525
526static __inline void
527aac_enqueue_bio(struct aac_softc *sc, struct bio *bp)
528{
529	int s;
530
531	s = splbio();
532	bioq_insert_tail(&sc->aac_bioq, bp);
533	AACQ_ADD(sc, AACQ_BIO);
534	splx(s);
535}
536
537static __inline struct bio *
538aac_dequeue_bio(struct aac_softc *sc)
539{
540	int s;
541	struct bio *bp;
542
543	s = splbio();
544	if ((bp = bioq_first(&sc->aac_bioq)) != NULL) {
545	bioq_remove(&sc->aac_bioq, bp);
546	AACQ_REMOVE(sc, AACQ_BIO);
547	}
548	splx(s);
549	return(bp);
550}
551
552static __inline void
553aac_print_printf(struct aac_softc *sc)
554{
555	if (sc->aac_common->ac_printf[0]) {
556	device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
557		      sc->aac_common->ac_printf);
558	sc->aac_common->ac_printf[0] = 0;
559	AAC_QNOTIFY(sc, AAC_DB_PRINTF);
560	}
561}
562