aacvar.h revision 95350
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 95350 2002-04-24 05:12:50Z 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_clr_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;
220extern struct aac_interface	aac_fa_interface;
221
222#define AAC_GET_FWSTATUS(sc)		((sc)->aac_if.aif_get_fwstatus((sc)))
223#define AAC_QNOTIFY(sc, qbit)		((sc)->aac_if.aif_qnotify((sc), (qbit)))
224#define AAC_GET_ISTATUS(sc)		((sc)->aac_if.aif_get_istatus((sc)))
225#define AAC_CLEAR_ISTATUS(sc, mask)	((sc)->aac_if.aif_clr_istatus((sc), \
226					(mask)))
227#define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
228	((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
229	(arg3)))
230#define AAC_GET_MAILBOXSTATUS(sc)	((sc)->aac_if.aif_get_mailboxstatus(  \
231					(sc)))
232#define	AAC_MASK_INTERRUPTS(sc)		((sc)->aac_if.aif_set_interrupts((sc), \
233					0))
234#define AAC_UNMASK_INTERRUPTS(sc)	((sc)->aac_if.aif_set_interrupts((sc), \
235					1))
236
237#define AAC_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag, \
238					sc->aac_bhandle, reg, val)
239#define AAC_GETREG4(sc, reg)		bus_space_read_4 (sc->aac_btag, \
240					sc->aac_bhandle, reg)
241#define AAC_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag, \
242					sc->aac_bhandle, reg, val)
243#define AAC_GETREG2(sc, reg)		bus_space_read_2 (sc->aac_btag, \
244					sc->aac_bhandle, reg)
245#define AAC_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag, \
246					sc->aac_bhandle, reg, val)
247#define AAC_GETREG1(sc, reg)		bus_space_read_1 (sc->aac_btag, \
248					sc->aac_bhandle, reg)
249
250TAILQ_HEAD(aac_container_tq, aac_container);
251
252/* Define the OS version specific locks */
253#if __FreeBSD_version >= 500005
254#include <sys/lock.h>
255#include <sys/mutex.h>
256typedef struct mtx aac_lock_t;
257#define AAC_LOCK_INIT(l, s)	mtx_init(l, s, NULL, MTX_DEF)
258#define AAC_LOCK_ACQUIRE(l)	mtx_lock(l)
259#define AAC_LOCK_RELEASE(l)	mtx_unlock(l)
260#else
261typedef struct simplelock aac_lock_t;
262#define AAC_LOCK_INIT(l, s)	simple_lock_init(l)
263#define AAC_LOCK_ACQUIRE(l)	simple_lock(l)
264#define AAC_LOCK_RELEASE(l)	simple_unlock(l)
265#endif
266
267#if __FreeBSD_version >= 500005
268#include <sys/selinfo.h>
269#else
270#include <sys/select.h>
271#endif
272
273/*
274 * Per-controller structure.
275 */
276struct aac_softc
277{
278	/* bus connections */
279	device_t		aac_dev;
280	struct resource		*aac_regs_resource;	/* register interface
281							 * window */
282	int			aac_regs_rid;		/* resource ID */
283	bus_space_handle_t	aac_bhandle;		/* bus space handle */
284	bus_space_tag_t		aac_btag;		/* bus space tag */
285	bus_dma_tag_t		aac_parent_dmat;	/* parent DMA tag */
286	bus_dma_tag_t		aac_buffer_dmat;	/* data buffer/command
287							 * DMA tag */
288	struct resource		*aac_irq;		/* interrupt */
289	int			aac_irq_rid;
290	void			*aac_intr;		/* interrupt handle */
291
292	/* controller features, limits and status */
293	int			aac_state;
294#define AAC_STATE_SUSPEND	(1<<0)
295#define	AAC_STATE_OPEN		(1<<1)
296#define AAC_STATE_INTERRUPTS_ON	(1<<2)
297#define AAC_STATE_AIF_SLEEPER	(1<<3)
298	struct FsaRevision		aac_revision;
299
300	/* controller hardware interface */
301	int			aac_hwif;
302#define AAC_HWIF_I960RX		0
303#define AAC_HWIF_STRONGARM	1
304#define	AAC_HWIF_FALCON		2
305#define AAC_HWIF_UNKNOWN	-1
306	bus_dma_tag_t		aac_common_dmat;	/* common structure
307							 * DMA tag */
308	bus_dmamap_t		aac_common_dmamap;	/* common structure
309							 * DMA map */
310	struct aac_common	*aac_common;
311	u_int32_t		aac_common_busaddr;
312	struct aac_interface	aac_if;
313
314	/* command/fib resources */
315	bus_dma_tag_t		aac_fib_dmat;	/* DMA tag for allocing FIBs */
316	struct aac_fib		*aac_fibs;
317	bus_dmamap_t		aac_fibmap;
318	u_int32_t		aac_fibphys;
319	struct aac_command	aac_command[AAC_FIB_COUNT];
320
321	/* command management */
322	TAILQ_HEAD(,aac_command) aac_free;	/* command structures
323						 * available for reuse */
324	TAILQ_HEAD(,aac_command) aac_ready;	/* commands on hold for
325						 * controller resources */
326	TAILQ_HEAD(,aac_command) aac_busy;
327	TAILQ_HEAD(,aac_command) aac_complete;	/* commands which have been
328						 * returned by the controller */
329	struct bio_queue_head	aac_bioq;
330	struct aac_queue_table	*aac_queues;
331	struct aac_queue_entry	*aac_qentries[AAC_QUEUE_COUNT];
332
333	struct aac_qstat	aac_qstat[AACQ_COUNT];	/* queue statistics */
334
335	/* connected containters */
336	struct aac_container_tq	aac_container_tqh;
337	aac_lock_t		aac_container_lock;
338
339	/* Protect the sync fib */
340#define AAC_SYNC_LOCK_FORCE	(1 << 0)
341	aac_lock_t		aac_sync_lock;
342
343	/* delayed activity infrastructure */
344#if __FreeBSD_version >= 500005
345	struct task		aac_task_complete;	/* deferred-completion
346							 * task */
347#endif
348	struct intr_config_hook	aac_ich;
349
350	/* management interface */
351	dev_t			aac_dev_t;
352	aac_lock_t		aac_aifq_lock;
353	struct aac_aif_command	aac_aifq[AAC_AIFQ_LENGTH];
354	int			aac_aifq_head;
355	int			aac_aifq_tail;
356	struct selinfo		rcv_select;
357	struct proc		*aifthread;
358	int			aifflags;
359#define AAC_AIFFLAGS_RUNNING	(1 << 0)
360#define AAC_AIFFLAGS_PENDING	(1 << 1)
361#define	AAC_AIFFLAGS_EXIT	(1 << 2)
362#define AAC_AIFFLAGS_EXITED	(1 << 3)
363	u_int32_t		quirks;
364#define AAC_QUIRK_PERC2QC	(1 << 0)
365};
366
367
368/*
369 * Public functions
370 */
371extern void		aac_free(struct aac_softc *sc);
372extern int		aac_attach(struct aac_softc *sc);
373extern int		aac_detach(device_t dev);
374extern int		aac_shutdown(device_t dev);
375extern int		aac_suspend(device_t dev);
376extern int		aac_resume(device_t dev);
377extern void		aac_intr(void *arg);
378extern void		aac_submit_bio(struct bio *bp);
379extern void		aac_biodone(struct bio *bp);
380extern int		aac_get_sync_fib(struct aac_softc *sc,
381					 struct aac_fib **fib, int flags);
382extern void		aac_release_sync_fib(struct aac_softc *sc);
383extern int		aac_sync_fib(struct aac_softc *sc, u_int32_t command,
384				     u_int32_t xferstate, struct aac_fib *fib,
385				     u_int16_t datasize);
386
387/*
388 * Debugging levels:
389 *  0 - quiet, only emit warnings
390 *  1 - noisy, emit major function points and things done
391 *  2 - extremely noisy, emit trace items in loops, etc.
392 */
393#ifdef AAC_DEBUG
394# define debug(level, fmt, args...)					\
395	do {								\
396	if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \
397	} while (0)
398# define debug_called(level)						\
399	do {								\
400	if (level <= AAC_DEBUG) printf("%s: called\n", __func__);	\
401	} while (0)
402
403extern void	aac_print_queues(struct aac_softc *sc);
404extern void	aac_panic(struct aac_softc *sc, char *reason);
405extern void	aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
406			      char *caller);
407extern void	aac_print_aif(struct aac_softc *sc,
408			      struct aac_aif_command *aif);
409
410#define AAC_PRINT_FIB(sc, fib)	aac_print_fib(sc, fib, __func__)
411
412#else
413# define debug(level, fmt, args...)
414# define debug_called(level)
415
416# define aac_print_queues(sc)
417# define aac_panic(sc, reason)
418
419# define AAC_PRINT_FIB(sc, fib)
420# define aac_print_aif(sc, aac_aif_command)
421#endif
422
423struct aac_code_lookup {
424	char	*string;
425	u_int32_t	code;
426};
427
428/*
429 * Queue primitives for driver queues.
430 */
431#define AACQ_ADD(sc, qname)					\
432	do {							\
433		struct aac_qstat *qs;				\
434								\
435		qs = &(sc)->aac_qstat[qname];			\
436								\
437		qs->q_length++;					\
438		if (qs->q_length > qs->q_max)			\
439			qs->q_max = qs->q_length;		\
440	} while (0)
441
442#define AACQ_REMOVE(sc, qname)    (sc)->aac_qstat[qname].q_length--
443#define AACQ_INIT(sc, qname)				\
444	do {						\
445		sc->aac_qstat[qname].q_length = 0;	\
446		sc->aac_qstat[qname].q_max = 0;		\
447	} while (0)
448
449
450#define AACQ_COMMAND_QUEUE(name, index)					\
451static __inline void							\
452aac_initq_ ## name (struct aac_softc *sc)				\
453{									\
454	TAILQ_INIT(&sc->aac_ ## name);					\
455	AACQ_INIT(sc, index);						\
456}									\
457static __inline void							\
458aac_enqueue_ ## name (struct aac_command *cm)				\
459{									\
460	int s;								\
461									\
462	s = splbio();							\
463	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
464		printf("command %p is on another queue, flags = %#x\n",	\
465		       cm, cm->cm_flags);				\
466		panic("command is on another queue");			\
467	}								\
468	TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
469	cm->cm_flags |= AAC_ON_ ## index;				\
470	AACQ_ADD(cm->cm_sc, index);					\
471	splx(s);							\
472}									\
473static __inline void							\
474aac_requeue_ ## name (struct aac_command *cm)				\
475{									\
476	int s;								\
477									\
478	s = splbio();							\
479	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
480		printf("command %p is on another queue, flags = %#x\n",	\
481		       cm, cm->cm_flags);				\
482		panic("command is on another queue");			\
483	}								\
484	TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
485	cm->cm_flags |= AAC_ON_ ## index;				\
486	AACQ_ADD(cm->cm_sc, index);					\
487	splx(s);							\
488}									\
489static __inline struct aac_command *					\
490aac_dequeue_ ## name (struct aac_softc *sc)				\
491{									\
492	struct aac_command *cm;						\
493	int s;								\
494									\
495	s = splbio();							\
496	if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) {		\
497		if ((cm->cm_flags & AAC_ON_ ## index) == 0) {		\
498			printf("command %p not in queue, flags = %#x, "	\
499		       	       "bit = %#x\n", cm, cm->cm_flags,		\
500			       AAC_ON_ ## index);			\
501			panic("command not in queue");			\
502		}							\
503		TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link);		\
504		cm->cm_flags &= ~AAC_ON_ ## index;			\
505		AACQ_REMOVE(sc, index);					\
506	}								\
507	splx(s);							\
508	return(cm);							\
509}									\
510static __inline void							\
511aac_remove_ ## name (struct aac_command *cm)				\
512{									\
513	int s;								\
514									\
515	s = splbio();							\
516	if ((cm->cm_flags & AAC_ON_ ## index) == 0) {			\
517		printf("command %p not in queue, flags = %#x, "		\
518		       "bit = %#x\n", cm, cm->cm_flags, 		\
519		       AAC_ON_ ## index);				\
520		panic("command not in queue");				\
521	}								\
522	TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
523	cm->cm_flags &= ~AAC_ON_ ## index;				\
524	AACQ_REMOVE(cm->cm_sc, index);					\
525	splx(s);							\
526}									\
527struct hack
528
529AACQ_COMMAND_QUEUE(free, AACQ_FREE);
530AACQ_COMMAND_QUEUE(ready, AACQ_READY);
531AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
532AACQ_COMMAND_QUEUE(complete, AACQ_COMPLETE);
533
534/*
535 * outstanding bio queue
536 */
537static __inline void
538aac_initq_bio(struct aac_softc *sc)
539{
540	bioq_init(&sc->aac_bioq);
541	AACQ_INIT(sc, AACQ_BIO);
542}
543
544static __inline void
545aac_enqueue_bio(struct aac_softc *sc, struct bio *bp)
546{
547	int s;
548
549	s = splbio();
550	bioq_insert_tail(&sc->aac_bioq, bp);
551	AACQ_ADD(sc, AACQ_BIO);
552	splx(s);
553}
554
555static __inline struct bio *
556aac_dequeue_bio(struct aac_softc *sc)
557{
558	int s;
559	struct bio *bp;
560
561	s = splbio();
562	if ((bp = bioq_first(&sc->aac_bioq)) != NULL) {
563		bioq_remove(&sc->aac_bioq, bp);
564		AACQ_REMOVE(sc, AACQ_BIO);
565	}
566	splx(s);
567	return(bp);
568}
569
570static __inline void
571aac_print_printf(struct aac_softc *sc)
572{
573	/*
574	 * XXX We have the ability to read the length of the printf string
575	 * from out of the mailboxes.
576	 */
577	device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
578		      sc->aac_common->ac_printf);
579	AAC_QNOTIFY(sc, AAC_DB_PRINTF);
580}
581