aacvar.h revision 103675
1251875Speter/*-
2251875Speter * Copyright (c) 2000 Michael Smith
3251875Speter * Copyright (c) 2001 Scott Long
4251875Speter * Copyright (c) 2000 BSDi
5251875Speter * Copyright (c) 2001 Adaptec, Inc.
6251875Speter * All rights reserved.
7251875Speter *
8251875Speter * Redistribution and use in source and binary forms, with or without
9251875Speter * modification, are permitted provided that the following conditions
10251875Speter * are met:
11251875Speter * 1. Redistributions of source code must retain the above copyright
12251875Speter *    notice, this list of conditions and the following disclaimer.
13251875Speter * 2. Redistributions in binary form must reproduce the above copyright
14251875Speter *    notice, this list of conditions and the following disclaimer in the
15251875Speter *    documentation and/or other materials provided with the distribution.
16251875Speter *
17251875Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18251875Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19251875Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20251875Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21251875Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22251875Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23251875Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24251875Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25251875Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26251875Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27251875Speter * SUCH DAMAGE.
28251875Speter *
29251875Speter *	$FreeBSD: head/sys/dev/aac/aacvar.h 103675 2002-09-20 12:52:03Z phk $
30251875Speter */
31251875Speter
32251875Speter/*
33251875Speter * Driver Parameter Definitions
34251875Speter */
35251875Speter
36251875Speter/*
37251875Speter * The firmware interface allows for a 16-bit s/g list length.  We limit
38251875Speter * ourselves to a reasonable maximum and ensure alignment.
39251875Speter */
40251875Speter#define AAC_MAXSGENTRIES	64	/* max S/G entries, limit 65535 */
41251875Speter
42251875Speter/*
43251875Speter * We allocate a small set of FIBs for the adapter to use to send us messages.
44251875Speter */
45251875Speter#define AAC_ADAPTER_FIBS	8
46251875Speter
47251875Speter/*
48251875Speter * FIBs are allocated up-front, and the pool isn't grown.  We should allocate
49251875Speter * enough here to let us keep the adapter busy without wasting large amounts
50251875Speter * of kernel memory.  The current interface implementation limits us to 512
51251875Speter * FIBs queued for the adapter at any one time.
52251875Speter */
53251875Speter#define AAC_FIB_COUNT		128
54251875Speter
55251875Speter/*
56251875Speter * The controller reports status events in AIFs.  We hang on to a number of
57251875Speter * these in order to pass them out to user-space management tools.
58251875Speter */
59251875Speter#define AAC_AIFQ_LENGTH		64
60251875Speter
61251875Speter/*
62251875Speter * Firmware messages are passed in the printf buffer.
63251875Speter */
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	int				ad_flags;
124#define AAC_DISK_OPEN	(1<<0)
125	int				ad_cylinders;
126	int				ad_heads;
127	int				ad_sectors;
128	u_int32_t			ad_size;
129	int				unit;
130};
131
132/*
133 * Per-command control structure.
134 */
135struct aac_command
136{
137	TAILQ_ENTRY(aac_command) cm_link;	/* list linkage */
138
139	struct aac_softc	*cm_sc;		/* controller that owns us */
140
141	struct aac_fib		*cm_fib;	/* FIB associated with this
142						 * command */
143	u_int32_t		cm_fibphys;	/* bus address of the FIB */
144	struct bio		*cm_data;	/* pointer to data in kernel
145						 * space */
146	u_int32_t		cm_datalen;	/* data length */
147	bus_dmamap_t		cm_datamap;	/* DMA map for bio data */
148	struct aac_sg_table	*cm_sgtable;	/* pointer to s/g table in
149						 * command */
150	int			cm_flags;
151#define AAC_CMD_MAPPED		(1<<0)		/* command has had its data
152						 * mapped */
153#define AAC_CMD_DATAIN		(1<<1)		/* command involves data moving
154						 * from controller to host */
155#define AAC_CMD_DATAOUT		(1<<2)		/* command involves data moving
156						 * from host to controller */
157#define AAC_CMD_COMPLETED	(1<<3)		/* command has been completed */
158#define AAC_CMD_TIMEDOUT	(1<<4)		/* command taken too long */
159#define AAC_ON_AACQ_FREE	(1<<5)
160#define AAC_ON_AACQ_READY	(1<<6)
161#define AAC_ON_AACQ_BUSY	(1<<7)
162#define AAC_ON_AACQ_COMPLETE	(1<<8)
163#define AAC_ON_AACQ_MASK	((1<<5)|(1<<6)|(1<<7)|(1<<8))
164
165	void			(* cm_complete)(struct aac_command *cm);
166	void			*cm_private;
167	time_t			cm_timestamp;	/* command creation time */
168	int			cm_queue;
169};
170
171/*
172 * We gather a number of adapter-visible items into a single structure.
173 *
174 * The ordering of this strucure may be important; we copy the Linux driver:
175 *
176 * Adapter FIBs
177 * Init struct
178 * Queue headers (Comm Area)
179 * Printf buffer
180 *
181 * In addition, we add:
182 * Sync Fib
183 */
184struct aac_common {
185	/* fibs for the controller to send us messages */
186	struct aac_fib		ac_fibs[AAC_ADAPTER_FIBS];
187
188	/* the init structure */
189	struct aac_adapter_init	ac_init;
190
191	/* arena within which the queue structures are kept */
192	u_int8_t		ac_qbuf[sizeof(struct aac_queue_table) +
193				AAC_QUEUE_ALIGN];
194
195	/* buffer for text messages from the controller */
196	char		       	ac_printf[AAC_PRINTF_BUFSIZE];
197
198	/* fib for synchronous commands */
199	struct aac_fib		ac_sync_fib;
200};
201
202/*
203 * Interface operations
204 */
205struct aac_interface
206{
207	int	(*aif_get_fwstatus)(struct aac_softc *sc);
208	void	(*aif_qnotify)(struct aac_softc *sc, int qbit);
209	int	(*aif_get_istatus)(struct aac_softc *sc);
210	void	(*aif_clr_istatus)(struct aac_softc *sc, int mask);
211	void	(*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
212				   u_int32_t arg0, u_int32_t arg1,
213				   u_int32_t arg2, u_int32_t arg3);
214	int	(*aif_get_mailboxstatus)(struct aac_softc *sc);
215	void	(*aif_set_interrupts)(struct aac_softc *sc, int enable);
216};
217extern struct aac_interface	aac_rx_interface;
218extern struct aac_interface	aac_sa_interface;
219extern struct aac_interface	aac_fa_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_clr_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, s)	mtx_init(l, s, NULL, MTX_DEF)
257#define AAC_LOCK_ACQUIRE(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, s)	simple_lock_init(l)
262#define AAC_LOCK_ACQUIRE(l)	simple_lock(l)
263#define AAC_LOCK_RELEASE(l)	simple_unlock(l)
264#endif
265
266#if __FreeBSD_version >= 500005
267#include <sys/selinfo.h>
268#else
269#include <sys/select.h>
270#endif
271
272/*
273 * Per-controller structure.
274 */
275struct aac_softc
276{
277	/* bus connections */
278	device_t		aac_dev;
279	struct resource		*aac_regs_resource;	/* register interface
280							 * window */
281	int			aac_regs_rid;		/* resource ID */
282	bus_space_handle_t	aac_bhandle;		/* bus space handle */
283	bus_space_tag_t		aac_btag;		/* bus space tag */
284	bus_dma_tag_t		aac_parent_dmat;	/* parent DMA tag */
285	bus_dma_tag_t		aac_buffer_dmat;	/* data buffer/command
286							 * DMA tag */
287	struct resource		*aac_irq;		/* interrupt */
288	int			aac_irq_rid;
289	void			*aac_intr;		/* interrupt handle */
290
291	/* controller features, limits and status */
292	int			aac_state;
293#define AAC_STATE_SUSPEND	(1<<0)
294#define	AAC_STATE_OPEN		(1<<1)
295#define AAC_STATE_INTERRUPTS_ON	(1<<2)
296#define AAC_STATE_AIF_SLEEPER	(1<<3)
297	struct FsaRevision		aac_revision;
298
299	/* controller hardware interface */
300	int			aac_hwif;
301#define AAC_HWIF_I960RX		0
302#define AAC_HWIF_STRONGARM	1
303#define	AAC_HWIF_FALCON		2
304#define AAC_HWIF_UNKNOWN	-1
305	bus_dma_tag_t		aac_common_dmat;	/* common structure
306							 * DMA tag */
307	bus_dmamap_t		aac_common_dmamap;	/* common structure
308							 * DMA map */
309	struct aac_common	*aac_common;
310	u_int32_t		aac_common_busaddr;
311	struct aac_interface	aac_if;
312
313	/* command/fib resources */
314	bus_dma_tag_t		aac_fib_dmat;	/* DMA tag for allocing FIBs */
315	struct aac_fib		*aac_fibs;
316	bus_dmamap_t		aac_fibmap;
317	u_int32_t		aac_fibphys;
318	struct aac_command	aac_command[AAC_FIB_COUNT];
319
320	/* command management */
321	TAILQ_HEAD(,aac_command) aac_free;	/* command structures
322						 * available for reuse */
323	TAILQ_HEAD(,aac_command) aac_ready;	/* commands on hold for
324						 * controller resources */
325	TAILQ_HEAD(,aac_command) aac_busy;
326	TAILQ_HEAD(,aac_command) aac_complete;	/* commands which have been
327						 * returned by the controller */
328	struct bio_queue_head	aac_bioq;
329	struct aac_queue_table	*aac_queues;
330	struct aac_queue_entry	*aac_qentries[AAC_QUEUE_COUNT];
331
332	struct aac_qstat	aac_qstat[AACQ_COUNT];	/* queue statistics */
333
334	/* connected containters */
335	struct aac_container_tq	aac_container_tqh;
336	aac_lock_t		aac_container_lock;
337
338	/* Protect the sync fib */
339#define AAC_SYNC_LOCK_FORCE	(1 << 0)
340	aac_lock_t		aac_sync_lock;
341
342	/* delayed activity infrastructure */
343#if __FreeBSD_version >= 500005
344	struct task		aac_task_complete;	/* deferred-completion
345							 * task */
346#endif
347	struct intr_config_hook	aac_ich;
348
349	/* management interface */
350	dev_t			aac_dev_t;
351	aac_lock_t		aac_aifq_lock;
352	struct aac_aif_command	aac_aifq[AAC_AIFQ_LENGTH];
353	int			aac_aifq_head;
354	int			aac_aifq_tail;
355	struct selinfo		rcv_select;
356	struct proc		*aifthread;
357	int			aifflags;
358#define AAC_AIFFLAGS_RUNNING	(1 << 0)
359#define AAC_AIFFLAGS_PENDING	(1 << 1)
360#define	AAC_AIFFLAGS_EXIT	(1 << 2)
361#define AAC_AIFFLAGS_EXITED	(1 << 3)
362	u_int32_t		quirks;
363#define AAC_QUIRK_PERC2QC	(1 << 0)
364#define	AAC_QUIRK_NOCAM		(1 << 1)	/* No SCSI passthrough */
365#define	AAC_QUIRK_CAM_NORESET	(1 << 2)	/* Fake SCSI resets */
366#define	AAC_QUIRK_CAM_PASSONLY	(1 << 3)	/* Only create pass devices */
367
368	u_int32_t		scsi_method_id;
369};
370
371
372/*
373 * Public functions
374 */
375extern void		aac_free(struct aac_softc *sc);
376extern int		aac_attach(struct aac_softc *sc);
377extern int		aac_detach(device_t dev);
378extern int		aac_shutdown(device_t dev);
379extern int		aac_suspend(device_t dev);
380extern int		aac_resume(device_t dev);
381extern void		aac_intr(void *arg);
382extern void		aac_submit_bio(struct bio *bp);
383extern void		aac_biodone(struct bio *bp);
384extern void		aac_startio(struct aac_softc *sc);
385extern int		aac_alloc_command(struct aac_softc *sc,
386					  struct aac_command **cmp);
387extern void		aac_release_command(struct aac_command *cm);
388extern int		aac_alloc_sync_fib(struct aac_softc *sc,
389					 struct aac_fib **fib, int flags);
390extern void		aac_release_sync_fib(struct aac_softc *sc);
391extern int		aac_sync_fib(struct aac_softc *sc, u_int32_t command,
392				     u_int32_t xferstate, struct aac_fib *fib,
393				     u_int16_t datasize);
394
395/*
396 * Debugging levels:
397 *  0 - quiet, only emit warnings
398 *  1 - noisy, emit major function points and things done
399 *  2 - extremely noisy, emit trace items in loops, etc.
400 */
401#ifdef AAC_DEBUG
402# define debug(level, fmt, args...)					\
403	do {								\
404	if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \
405	} while (0)
406# define debug_called(level)						\
407	do {								\
408	if (level <= AAC_DEBUG) printf("%s: called\n", __func__);	\
409	} while (0)
410
411extern void	aac_print_queues(struct aac_softc *sc);
412extern void	aac_panic(struct aac_softc *sc, char *reason);
413extern void	aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
414			      const char *caller);
415extern void	aac_print_aif(struct aac_softc *sc,
416			      struct aac_aif_command *aif);
417
418#define AAC_PRINT_FIB(sc, fib)	aac_print_fib(sc, fib, __func__)
419
420#else
421# define debug(level, fmt, args...)
422# define debug_called(level)
423
424# define aac_print_queues(sc)
425# define aac_panic(sc, reason)
426
427# define AAC_PRINT_FIB(sc, fib)
428# define aac_print_aif(sc, aac_aif_command)
429#endif
430
431struct aac_code_lookup {
432	char	*string;
433	u_int32_t	code;
434};
435
436/*
437 * Queue primitives for driver queues.
438 */
439#define AACQ_ADD(sc, qname)					\
440	do {							\
441		struct aac_qstat *qs;				\
442								\
443		qs = &(sc)->aac_qstat[qname];			\
444								\
445		qs->q_length++;					\
446		if (qs->q_length > qs->q_max)			\
447			qs->q_max = qs->q_length;		\
448	} while (0)
449
450#define AACQ_REMOVE(sc, qname)    (sc)->aac_qstat[qname].q_length--
451#define AACQ_INIT(sc, qname)				\
452	do {						\
453		sc->aac_qstat[qname].q_length = 0;	\
454		sc->aac_qstat[qname].q_max = 0;		\
455	} while (0)
456
457
458#define AACQ_COMMAND_QUEUE(name, index)					\
459static __inline void							\
460aac_initq_ ## name (struct aac_softc *sc)				\
461{									\
462	TAILQ_INIT(&sc->aac_ ## name);					\
463	AACQ_INIT(sc, index);						\
464}									\
465static __inline void							\
466aac_enqueue_ ## name (struct aac_command *cm)				\
467{									\
468	int s;								\
469									\
470	s = splbio();							\
471	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
472		printf("command %p is on another queue, flags = %#x\n",	\
473		       cm, cm->cm_flags);				\
474		panic("command is on another queue");			\
475	}								\
476	TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
477	cm->cm_flags |= AAC_ON_ ## index;				\
478	AACQ_ADD(cm->cm_sc, index);					\
479	splx(s);							\
480}									\
481static __inline void							\
482aac_requeue_ ## name (struct aac_command *cm)				\
483{									\
484	int s;								\
485									\
486	s = splbio();							\
487	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
488		printf("command %p is on another queue, flags = %#x\n",	\
489		       cm, cm->cm_flags);				\
490		panic("command is on another queue");			\
491	}								\
492	TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
493	cm->cm_flags |= AAC_ON_ ## index;				\
494	AACQ_ADD(cm->cm_sc, index);					\
495	splx(s);							\
496}									\
497static __inline struct aac_command *					\
498aac_dequeue_ ## name (struct aac_softc *sc)				\
499{									\
500	struct aac_command *cm;						\
501	int s;								\
502									\
503	s = splbio();							\
504	if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) {		\
505		if ((cm->cm_flags & AAC_ON_ ## index) == 0) {		\
506			printf("command %p not in queue, flags = %#x, "	\
507		       	       "bit = %#x\n", cm, cm->cm_flags,		\
508			       AAC_ON_ ## index);			\
509			panic("command not in queue");			\
510		}							\
511		TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link);		\
512		cm->cm_flags &= ~AAC_ON_ ## index;			\
513		AACQ_REMOVE(sc, index);					\
514	}								\
515	splx(s);							\
516	return(cm);							\
517}									\
518static __inline void							\
519aac_remove_ ## name (struct aac_command *cm)				\
520{									\
521	int s;								\
522									\
523	s = splbio();							\
524	if ((cm->cm_flags & AAC_ON_ ## index) == 0) {			\
525		printf("command %p not in queue, flags = %#x, "		\
526		       "bit = %#x\n", cm, cm->cm_flags, 		\
527		       AAC_ON_ ## index);				\
528		panic("command not in queue");				\
529	}								\
530	TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
531	cm->cm_flags &= ~AAC_ON_ ## index;				\
532	AACQ_REMOVE(cm->cm_sc, index);					\
533	splx(s);							\
534}									\
535struct hack
536
537AACQ_COMMAND_QUEUE(free, AACQ_FREE);
538AACQ_COMMAND_QUEUE(ready, AACQ_READY);
539AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
540AACQ_COMMAND_QUEUE(complete, AACQ_COMPLETE);
541
542/*
543 * outstanding bio queue
544 */
545static __inline void
546aac_initq_bio(struct aac_softc *sc)
547{
548	bioq_init(&sc->aac_bioq);
549	AACQ_INIT(sc, AACQ_BIO);
550}
551
552static __inline void
553aac_enqueue_bio(struct aac_softc *sc, struct bio *bp)
554{
555	int s;
556
557	s = splbio();
558	bioq_insert_tail(&sc->aac_bioq, bp);
559	AACQ_ADD(sc, AACQ_BIO);
560	splx(s);
561}
562
563static __inline struct bio *
564aac_dequeue_bio(struct aac_softc *sc)
565{
566	int s;
567	struct bio *bp;
568
569	s = splbio();
570	if ((bp = bioq_first(&sc->aac_bioq)) != NULL) {
571		bioq_remove(&sc->aac_bioq, bp);
572		AACQ_REMOVE(sc, AACQ_BIO);
573	}
574	splx(s);
575	return(bp);
576}
577
578static __inline void
579aac_print_printf(struct aac_softc *sc)
580{
581	/*
582	 * XXX We have the ability to read the length of the printf string
583	 * from out of the mailboxes.
584	 */
585	device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
586		      sc->aac_common->ac_printf);
587	AAC_QNOTIFY(sc, AAC_DB_PRINTF);
588}
589