aacvar.h revision 111979
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 111979 2003-03-08 08:01:31Z phk $
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_mailboxstatus)(struct aac_softc *sc);
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_MAILBOXSTATUS(sc)	((sc)->aac_if.aif_get_mailboxstatus(  \
242					(sc)))
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		quirks;
361#define AAC_QUIRK_PERC2QC	(1 << 0)
362#define	AAC_QUIRK_NOCAM		(1 << 1)	/* No SCSI passthrough */
363#define	AAC_QUIRK_CAM_NORESET	(1 << 2)	/* Fake SCSI resets */
364#define	AAC_QUIRK_CAM_PASSONLY	(1 << 3)	/* Only create pass devices */
365
366	u_int32_t		scsi_method_id;
367	TAILQ_HEAD(,aac_sim)	aac_sim_tqh;
368};
369
370
371/*
372 * Public functions
373 */
374extern void		aac_free(struct aac_softc *sc);
375extern int		aac_attach(struct aac_softc *sc);
376extern int		aac_detach(device_t dev);
377extern int		aac_shutdown(device_t dev);
378extern int		aac_suspend(device_t dev);
379extern int		aac_resume(device_t dev);
380extern void		aac_intr(void *arg);
381extern void		aac_submit_bio(struct bio *bp);
382extern void		aac_biodone(struct bio *bp);
383extern void		aac_startio(struct aac_softc *sc);
384extern int		aac_alloc_command(struct aac_softc *sc,
385					  struct aac_command **cmp);
386extern void		aac_release_command(struct aac_command *cm);
387extern int		aac_alloc_sync_fib(struct aac_softc *sc,
388					 struct aac_fib **fib, int flags);
389extern void		aac_release_sync_fib(struct aac_softc *sc);
390extern int		aac_sync_fib(struct aac_softc *sc, u_int32_t command,
391				     u_int32_t xferstate, struct aac_fib *fib,
392				     u_int16_t datasize);
393
394/*
395 * Debugging levels:
396 *  0 - quiet, only emit warnings
397 *  1 - noisy, emit major function points and things done
398 *  2 - extremely noisy, emit trace items in loops, etc.
399 */
400#ifdef AAC_DEBUG
401# define debug(level, fmt, args...)					\
402	do {								\
403	if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \
404	} while (0)
405# define debug_called(level)						\
406	do {								\
407	if (level <= AAC_DEBUG) printf("%s: called\n", __func__);	\
408	} while (0)
409
410extern void	aac_print_queues(struct aac_softc *sc);
411extern void	aac_panic(struct aac_softc *sc, char *reason);
412extern void	aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
413			      const char *caller);
414extern void	aac_print_aif(struct aac_softc *sc,
415			      struct aac_aif_command *aif);
416
417#define AAC_PRINT_FIB(sc, fib)	aac_print_fib(sc, fib, __func__)
418
419#else
420# define debug(level, fmt, args...)
421# define debug_called(level)
422
423# define aac_print_queues(sc)
424# define aac_panic(sc, reason)
425
426# define AAC_PRINT_FIB(sc, fib)
427# define aac_print_aif(sc, aac_aif_command)
428#endif
429
430struct aac_code_lookup {
431	char	*string;
432	u_int32_t	code;
433};
434
435/*
436 * Queue primitives for driver queues.
437 */
438#define AACQ_ADD(sc, qname)					\
439	do {							\
440		struct aac_qstat *qs;				\
441								\
442		qs = &(sc)->aac_qstat[qname];			\
443								\
444		qs->q_length++;					\
445		if (qs->q_length > qs->q_max)			\
446			qs->q_max = qs->q_length;		\
447	} while (0)
448
449#define AACQ_REMOVE(sc, qname)    (sc)->aac_qstat[qname].q_length--
450#define AACQ_INIT(sc, qname)				\
451	do {						\
452		sc->aac_qstat[qname].q_length = 0;	\
453		sc->aac_qstat[qname].q_max = 0;		\
454	} while (0)
455
456
457#define AACQ_COMMAND_QUEUE(name, index)					\
458static __inline void							\
459aac_initq_ ## name (struct aac_softc *sc)				\
460{									\
461	TAILQ_INIT(&sc->aac_ ## name);					\
462	AACQ_INIT(sc, index);						\
463}									\
464static __inline void							\
465aac_enqueue_ ## name (struct aac_command *cm)				\
466{									\
467	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
468		printf("command %p is on another queue, flags = %#x\n",	\
469		       cm, cm->cm_flags);				\
470		panic("command is on another queue");			\
471	}								\
472	TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
473	cm->cm_flags |= AAC_ON_ ## index;				\
474	AACQ_ADD(cm->cm_sc, index);					\
475}									\
476static __inline void							\
477aac_requeue_ ## name (struct aac_command *cm)				\
478{									\
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}									\
488static __inline struct aac_command *					\
489aac_dequeue_ ## name (struct aac_softc *sc)				\
490{									\
491	struct aac_command *cm;						\
492									\
493	if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) {		\
494		if ((cm->cm_flags & AAC_ON_ ## index) == 0) {		\
495			printf("command %p not in queue, flags = %#x, "	\
496		       	       "bit = %#x\n", cm, cm->cm_flags,		\
497			       AAC_ON_ ## index);			\
498			panic("command not in queue");			\
499		}							\
500		TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link);		\
501		cm->cm_flags &= ~AAC_ON_ ## index;			\
502		AACQ_REMOVE(sc, index);					\
503	}								\
504	return(cm);							\
505}									\
506static __inline void							\
507aac_remove_ ## name (struct aac_command *cm)				\
508{									\
509	if ((cm->cm_flags & AAC_ON_ ## index) == 0) {			\
510		printf("command %p not in queue, flags = %#x, "		\
511		       "bit = %#x\n", cm, cm->cm_flags, 		\
512		       AAC_ON_ ## index);				\
513		panic("command not in queue");				\
514	}								\
515	TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
516	cm->cm_flags &= ~AAC_ON_ ## index;				\
517	AACQ_REMOVE(cm->cm_sc, index);					\
518}									\
519struct hack
520
521AACQ_COMMAND_QUEUE(free, AACQ_FREE);
522AACQ_COMMAND_QUEUE(ready, AACQ_READY);
523AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
524
525/*
526 * outstanding bio queue
527 */
528static __inline void
529aac_initq_bio(struct aac_softc *sc)
530{
531	bioq_init(&sc->aac_bioq);
532	AACQ_INIT(sc, AACQ_BIO);
533}
534
535static __inline void
536aac_enqueue_bio(struct aac_softc *sc, struct bio *bp)
537{
538	bioq_insert_tail(&sc->aac_bioq, bp);
539	AACQ_ADD(sc, AACQ_BIO);
540}
541
542static __inline struct bio *
543aac_dequeue_bio(struct aac_softc *sc)
544{
545	struct bio *bp;
546
547	if ((bp = bioq_first(&sc->aac_bioq)) != NULL) {
548		bioq_remove(&sc->aac_bioq, bp);
549		AACQ_REMOVE(sc, AACQ_BIO);
550	}
551	return(bp);
552}
553
554static __inline void
555aac_print_printf(struct aac_softc *sc)
556{
557	/*
558	 * XXX We have the ability to read the length of the printf string
559	 * from out of the mailboxes.
560	 */
561	device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
562		      sc->aac_common->ac_printf);
563	AAC_QNOTIFY(sc, AAC_DB_PRINTF);
564}
565