aacvar.h revision 112679
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 112679 2003-03-26 17:50:11Z scottl $
30 */
31
32#include <sys/bio.h>
33#include <sys/lock.h>
34#include <sys/mutex.h>
35#include <sys/taskqueue.h>
36#include <sys/selinfo.h>
37
38/*
39 * Driver Parameter Definitions
40 */
41
42/*
43 * The firmware interface allows for a 16-bit s/g list length.  We limit
44 * ourselves to a reasonable maximum and ensure alignment.
45 */
46#define AAC_MAXSGENTRIES	64	/* max S/G entries, limit 65535 */
47
48/*
49 * We allocate a small set of FIBs for the adapter to use to send us messages.
50 */
51#define AAC_ADAPTER_FIBS	8
52
53/*
54 * FIBs are allocated in page-size chunks and can grow up to the 512
55 * limit imposed by the hardware.
56 */
57#define AAC_FIB_COUNT		(PAGE_SIZE/sizeof(struct aac_fib))
58#define AAC_PREALLOCATE_FIBS	128
59#define AAC_MAX_FIBS		512
60
61/*
62 * The controller reports status events in AIFs.  We hang on to a number of
63 * these in order to pass them out to user-space management tools.
64 */
65#define AAC_AIFQ_LENGTH		64
66
67/*
68 * Firmware messages are passed in the printf buffer.
69 */
70#define AAC_PRINTF_BUFSIZE	256
71
72/*
73 * We wait this many seconds for the adapter to come ready if it is still
74 * booting
75 */
76#define AAC_BOOT_TIMEOUT	(3 * 60)
77
78/*
79 * Timeout for immediate commands.
80 */
81#define AAC_IMMEDIATE_TIMEOUT	30		/* seconds */
82
83/*
84 * Timeout for normal commands
85 */
86#define AAC_CMD_TIMEOUT		30		/* seconds */
87
88/*
89 * Rate at which we periodically check for timed out commands and kick the
90 * controller.
91 */
92#define AAC_PERIODIC_INTERVAL	20		/* seconds */
93
94/*
95 * Per-container data structure
96 */
97struct aac_container
98{
99	struct aac_mntobj		co_mntobj;
100	device_t			co_disk;
101	int				co_found;
102	TAILQ_ENTRY(aac_container)	co_link;
103};
104
105/*
106 * Per-SIM data structure
107 */
108struct aac_sim
109{
110	device_t		sim_dev;
111	int			TargetsPerBus;
112	int			BusNumber;
113	int			InitiatorBusId;
114	struct aac_softc	*aac_sc;
115	TAILQ_ENTRY(aac_sim)	sim_link;
116};
117
118/*
119 * Per-disk structure
120 */
121struct aac_disk
122{
123	device_t			ad_dev;
124	struct aac_softc		*ad_controller;
125	struct aac_container		*ad_container;
126	struct disk			ad_disk;
127	int				ad_flags;
128#define AAC_DISK_OPEN	(1<<0)
129	int				ad_cylinders;
130	int				ad_heads;
131	int				ad_sectors;
132	u_int32_t			ad_size;
133	int				unit;
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	int			cm_flags;
155#define AAC_CMD_MAPPED		(1<<0)		/* command has had its data
156						 * mapped */
157#define AAC_CMD_DATAIN		(1<<1)		/* command involves data moving
158						 * from controller to host */
159#define AAC_CMD_DATAOUT		(1<<2)		/* command involves data moving
160						 * from host to controller */
161#define AAC_CMD_COMPLETED	(1<<3)		/* command has been completed */
162#define AAC_CMD_TIMEDOUT	(1<<4)		/* command taken too long */
163#define AAC_ON_AACQ_FREE	(1<<5)
164#define AAC_ON_AACQ_READY	(1<<6)
165#define AAC_ON_AACQ_BUSY	(1<<7)
166#define AAC_ON_AACQ_COMPLETE	(1<<8)
167#define AAC_ON_AACQ_MASK	((1<<5)|(1<<6)|(1<<7)|(1<<8))
168
169	void			(* cm_complete)(struct aac_command *cm);
170	void			*cm_private;
171	time_t			cm_timestamp;	/* command creation time */
172	int			cm_queue;
173	int			cm_index;
174};
175
176struct aac_fibmap {
177	TAILQ_ENTRY(aac_fibmap) fm_link;	/* list linkage */
178	struct aac_fib		*aac_fibs;
179	bus_dmamap_t		aac_fibmap;
180	struct aac_command	*aac_commands;
181};
182
183/*
184 * We gather a number of adapter-visible items into a single structure.
185 *
186 * The ordering of this strucure may be important; we copy the Linux driver:
187 *
188 * Adapter FIBs
189 * Init struct
190 * Queue headers (Comm Area)
191 * Printf buffer
192 *
193 * In addition, we add:
194 * Sync Fib
195 */
196struct aac_common {
197	/* fibs for the controller to send us messages */
198	struct aac_fib		ac_fibs[AAC_ADAPTER_FIBS];
199
200	/* the init structure */
201	struct aac_adapter_init	ac_init;
202
203	/* arena within which the queue structures are kept */
204	u_int8_t		ac_qbuf[sizeof(struct aac_queue_table) +
205				AAC_QUEUE_ALIGN];
206
207	/* buffer for text messages from the controller */
208	char		       	ac_printf[AAC_PRINTF_BUFSIZE];
209
210	/* fib for synchronous commands */
211	struct aac_fib		ac_sync_fib;
212};
213
214/*
215 * Interface operations
216 */
217struct aac_interface
218{
219	int	(*aif_get_fwstatus)(struct aac_softc *sc);
220	void	(*aif_qnotify)(struct aac_softc *sc, int qbit);
221	int	(*aif_get_istatus)(struct aac_softc *sc);
222	void	(*aif_clr_istatus)(struct aac_softc *sc, int mask);
223	void	(*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
224				   u_int32_t arg0, u_int32_t arg1,
225				   u_int32_t arg2, u_int32_t arg3);
226	int	(*aif_get_mailbox)(struct aac_softc *sc, int mb);
227	void	(*aif_set_interrupts)(struct aac_softc *sc, int enable);
228};
229extern struct aac_interface	aac_rx_interface;
230extern struct aac_interface	aac_sa_interface;
231extern struct aac_interface	aac_fa_interface;
232
233#define AAC_GET_FWSTATUS(sc)		((sc)->aac_if.aif_get_fwstatus((sc)))
234#define AAC_QNOTIFY(sc, qbit)		((sc)->aac_if.aif_qnotify((sc), (qbit)))
235#define AAC_GET_ISTATUS(sc)		((sc)->aac_if.aif_get_istatus((sc)))
236#define AAC_CLEAR_ISTATUS(sc, mask)	((sc)->aac_if.aif_clr_istatus((sc), \
237					(mask)))
238#define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
239	((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
240	(arg3)))
241#define AAC_GET_MAILBOX(sc, mb)		((sc)->aac_if.aif_get_mailbox((sc), \
242					(mb)))
243#define	AAC_MASK_INTERRUPTS(sc)		((sc)->aac_if.aif_set_interrupts((sc), \
244					0))
245#define AAC_UNMASK_INTERRUPTS(sc)	((sc)->aac_if.aif_set_interrupts((sc), \
246					1))
247
248#define AAC_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag, \
249					sc->aac_bhandle, reg, val)
250#define AAC_GETREG4(sc, reg)		bus_space_read_4 (sc->aac_btag, \
251					sc->aac_bhandle, reg)
252#define AAC_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag, \
253					sc->aac_bhandle, reg, val)
254#define AAC_GETREG2(sc, reg)		bus_space_read_2 (sc->aac_btag, \
255					sc->aac_bhandle, reg)
256#define AAC_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag, \
257					sc->aac_bhandle, reg, val)
258#define AAC_GETREG1(sc, reg)		bus_space_read_1 (sc->aac_btag, \
259					sc->aac_bhandle, reg)
260
261/* Define the OS version specific locks */
262typedef struct mtx aac_lock_t;
263#define AAC_LOCK_INIT(l, s)	mtx_init(l, s, NULL, MTX_DEF)
264#define AAC_LOCK_ACQUIRE(l)	mtx_lock(l)
265#define AAC_LOCK_RELEASE(l)	mtx_unlock(l)
266
267
268/*
269 * Per-controller structure.
270 */
271struct aac_softc
272{
273	/* bus connections */
274	device_t		aac_dev;
275	struct resource		*aac_regs_resource;	/* register interface
276							 * window */
277	int			aac_regs_rid;		/* resource ID */
278	bus_space_handle_t	aac_bhandle;		/* bus space handle */
279	bus_space_tag_t		aac_btag;		/* bus space tag */
280	bus_dma_tag_t		aac_parent_dmat;	/* parent DMA tag */
281	bus_dma_tag_t		aac_buffer_dmat;	/* data buffer/command
282							 * DMA tag */
283	struct resource		*aac_irq;		/* interrupt */
284	int			aac_irq_rid;
285	void			*aac_intr;		/* interrupt handle */
286	eventhandler_tag	eh;
287
288	/* controller features, limits and status */
289	int			aac_state;
290#define AAC_STATE_SUSPEND	(1<<0)
291#define	AAC_STATE_OPEN		(1<<1)
292#define AAC_STATE_INTERRUPTS_ON	(1<<2)
293#define AAC_STATE_AIF_SLEEPER	(1<<3)
294	struct FsaRevision		aac_revision;
295
296	/* controller hardware interface */
297	int			aac_hwif;
298#define AAC_HWIF_I960RX		0
299#define AAC_HWIF_STRONGARM	1
300#define	AAC_HWIF_FALCON		2
301#define AAC_HWIF_UNKNOWN	-1
302	bus_dma_tag_t		aac_common_dmat;	/* common structure
303							 * DMA tag */
304	bus_dmamap_t		aac_common_dmamap;	/* common structure
305							 * DMA map */
306	struct aac_common	*aac_common;
307	u_int32_t		aac_common_busaddr;
308	struct aac_interface	aac_if;
309
310	/* command/fib resources */
311	bus_dma_tag_t		aac_fib_dmat;	/* DMA tag for allocing FIBs */
312	TAILQ_HEAD(,aac_fibmap)	aac_fibmap_tqh;
313	uint			total_fibs;
314	struct aac_command	*aac_commands;
315
316	/* command management */
317	TAILQ_HEAD(,aac_command) aac_free;	/* command structures
318						 * available for reuse */
319	TAILQ_HEAD(,aac_command) aac_ready;	/* commands on hold for
320						 * controller resources */
321	TAILQ_HEAD(,aac_command) aac_busy;
322	struct bio_queue_head	aac_bioq;
323	struct aac_queue_table	*aac_queues;
324	struct aac_queue_entry	*aac_qentries[AAC_QUEUE_COUNT];
325
326	struct aac_qstat	aac_qstat[AACQ_COUNT];	/* queue statistics */
327
328	/* connected containters */
329	TAILQ_HEAD(,aac_container)	aac_container_tqh;
330	aac_lock_t		aac_container_lock;
331
332	/* Protect the sync fib */
333#define AAC_SYNC_LOCK_FORCE	(1 << 0)
334	aac_lock_t		aac_sync_lock;
335
336	aac_lock_t		aac_io_lock;
337
338	/* delayed activity infrastructure */
339	struct task		aac_task_complete;	/* deferred-completion
340							 * task */
341	struct intr_config_hook	aac_ich;
342
343	/* management interface */
344	dev_t			aac_dev_t;
345	aac_lock_t		aac_aifq_lock;
346	struct aac_aif_command	aac_aifq[AAC_AIFQ_LENGTH];
347	int			aac_aifq_head;
348	int			aac_aifq_tail;
349	struct selinfo		rcv_select;
350	struct proc		*aifthread;
351	int			aifflags;
352#define AAC_AIFFLAGS_RUNNING	(1 << 0)
353#define AAC_AIFFLAGS_AIF	(1 << 1)
354#define	AAC_AIFFLAGS_EXIT	(1 << 2)
355#define AAC_AIFFLAGS_EXITED	(1 << 3)
356#define AAC_AIFFLAGS_PRINTF	(1 << 4)
357#define	AAC_AIFFLAGS_ALLOCFIBS	(1 << 5)
358#define AAC_AIFFLAGS_PENDING	(AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \
359				 AAC_AIFFLAGS_ALLOCFIBS)
360	u_int32_t		flags;
361#define AAC_FLAGS_PERC2QC	(1 << 0)
362#define	AAC_FLAGS_ENABLE_CAM	(1 << 1)	/* No SCSI passthrough */
363#define	AAC_FLAGS_CAM_NORESET	(1 << 2)	/* Fake SCSI resets */
364#define	AAC_FLAGS_CAM_PASSONLY	(1 << 3)	/* Only create pass devices */
365#define	AAC_FLAGS_SG_64BIT	(1 << 4)	/* Use 64-bit S/G addresses */
366#define	AAC_FLAGS_4GB_WINDOW	(1 << 5)	/* Device can access host mem
367						 * 2GB-4GB range */
368#define	AAC_FLAGS_NO4GB		(1 << 6)	/* Can't access host mem >2GB */
369#define	AAC_FLAGS_256FIBS	(1 << 7)	/* Can only do 256 commands */
370
371	u_int32_t		supported_options;
372	int			aac_max_fibs;
373	u_int32_t		scsi_method_id;
374	TAILQ_HEAD(,aac_sim)	aac_sim_tqh;
375};
376
377
378/*
379 * Public functions
380 */
381extern void		aac_free(struct aac_softc *sc);
382extern int		aac_attach(struct aac_softc *sc);
383extern int		aac_detach(device_t dev);
384extern int		aac_shutdown(device_t dev);
385extern int		aac_suspend(device_t dev);
386extern int		aac_resume(device_t dev);
387extern void		aac_intr(void *arg);
388extern void		aac_submit_bio(struct bio *bp);
389extern void		aac_biodone(struct bio *bp);
390extern void		aac_startio(struct aac_softc *sc);
391extern int		aac_alloc_command(struct aac_softc *sc,
392					  struct aac_command **cmp);
393extern void		aac_release_command(struct aac_command *cm);
394extern int		aac_alloc_sync_fib(struct aac_softc *sc,
395					 struct aac_fib **fib, int flags);
396extern void		aac_release_sync_fib(struct aac_softc *sc);
397extern int		aac_sync_fib(struct aac_softc *sc, u_int32_t command,
398				     u_int32_t xferstate, struct aac_fib *fib,
399				     u_int16_t datasize);
400
401/*
402 * Debugging levels:
403 *  0 - quiet, only emit warnings
404 *  1 - noisy, emit major function points and things done
405 *  2 - extremely noisy, emit trace items in loops, etc.
406 */
407#ifdef AAC_DEBUG
408# define debug(level, fmt, args...)					\
409	do {								\
410	if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \
411	} while (0)
412# define debug_called(level)						\
413	do {								\
414	if (level <= AAC_DEBUG) printf("%s: called\n", __func__);	\
415	} while (0)
416
417extern void	aac_print_queues(struct aac_softc *sc);
418extern void	aac_panic(struct aac_softc *sc, char *reason);
419extern void	aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
420			      const char *caller);
421extern void	aac_print_aif(struct aac_softc *sc,
422			      struct aac_aif_command *aif);
423
424#define AAC_PRINT_FIB(sc, fib)	aac_print_fib(sc, fib, __func__)
425
426#else
427# define debug(level, fmt, args...)
428# define debug_called(level)
429
430# define aac_print_queues(sc)
431# define aac_panic(sc, reason)
432
433# define AAC_PRINT_FIB(sc, fib)
434# define aac_print_aif(sc, aac_aif_command)
435#endif
436
437struct aac_code_lookup {
438	char	*string;
439	u_int32_t	code;
440};
441
442/*
443 * Queue primitives for driver queues.
444 */
445#define AACQ_ADD(sc, qname)					\
446	do {							\
447		struct aac_qstat *qs;				\
448								\
449		qs = &(sc)->aac_qstat[qname];			\
450								\
451		qs->q_length++;					\
452		if (qs->q_length > qs->q_max)			\
453			qs->q_max = qs->q_length;		\
454	} while (0)
455
456#define AACQ_REMOVE(sc, qname)    (sc)->aac_qstat[qname].q_length--
457#define AACQ_INIT(sc, qname)				\
458	do {						\
459		sc->aac_qstat[qname].q_length = 0;	\
460		sc->aac_qstat[qname].q_max = 0;		\
461	} while (0)
462
463
464#define AACQ_COMMAND_QUEUE(name, index)					\
465static __inline void							\
466aac_initq_ ## name (struct aac_softc *sc)				\
467{									\
468	TAILQ_INIT(&sc->aac_ ## name);					\
469	AACQ_INIT(sc, index);						\
470}									\
471static __inline void							\
472aac_enqueue_ ## name (struct aac_command *cm)				\
473{									\
474	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
475		printf("command %p is on another queue, flags = %#x\n",	\
476		       cm, cm->cm_flags);				\
477		panic("command is on another queue");			\
478	}								\
479	TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
480	cm->cm_flags |= AAC_ON_ ## index;				\
481	AACQ_ADD(cm->cm_sc, index);					\
482}									\
483static __inline void							\
484aac_requeue_ ## name (struct aac_command *cm)				\
485{									\
486	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
487		printf("command %p is on another queue, flags = %#x\n",	\
488		       cm, cm->cm_flags);				\
489		panic("command is on another queue");			\
490	}								\
491	TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
492	cm->cm_flags |= AAC_ON_ ## index;				\
493	AACQ_ADD(cm->cm_sc, index);					\
494}									\
495static __inline struct aac_command *					\
496aac_dequeue_ ## name (struct aac_softc *sc)				\
497{									\
498	struct aac_command *cm;						\
499									\
500	if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) {		\
501		if ((cm->cm_flags & AAC_ON_ ## index) == 0) {		\
502			printf("command %p not in queue, flags = %#x, "	\
503		       	       "bit = %#x\n", cm, cm->cm_flags,		\
504			       AAC_ON_ ## index);			\
505			panic("command not in queue");			\
506		}							\
507		TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link);		\
508		cm->cm_flags &= ~AAC_ON_ ## index;			\
509		AACQ_REMOVE(sc, index);					\
510	}								\
511	return(cm);							\
512}									\
513static __inline void							\
514aac_remove_ ## name (struct aac_command *cm)				\
515{									\
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}									\
526struct hack
527
528AACQ_COMMAND_QUEUE(free, AACQ_FREE);
529AACQ_COMMAND_QUEUE(ready, AACQ_READY);
530AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
531
532/*
533 * outstanding bio queue
534 */
535static __inline void
536aac_initq_bio(struct aac_softc *sc)
537{
538	bioq_init(&sc->aac_bioq);
539	AACQ_INIT(sc, AACQ_BIO);
540}
541
542static __inline void
543aac_enqueue_bio(struct aac_softc *sc, struct bio *bp)
544{
545	bioq_insert_tail(&sc->aac_bioq, bp);
546	AACQ_ADD(sc, AACQ_BIO);
547}
548
549static __inline struct bio *
550aac_dequeue_bio(struct aac_softc *sc)
551{
552	struct bio *bp;
553
554	if ((bp = bioq_first(&sc->aac_bioq)) != NULL) {
555		bioq_remove(&sc->aac_bioq, bp);
556		AACQ_REMOVE(sc, AACQ_BIO);
557	}
558	return(bp);
559}
560
561static __inline void
562aac_print_printf(struct aac_softc *sc)
563{
564	/*
565	 * XXX We have the ability to read the length of the printf string
566	 * from out of the mailboxes.
567	 */
568	device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
569		      sc->aac_common->ac_printf);
570	AAC_QNOTIFY(sc, AAC_DB_PRINTF);
571}
572