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