xref: /freebsd/sys/dev/aacraid/aacraid_var.h (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2000 Michael Smith
5  * Copyright (c) 2001 Scott Long
6  * Copyright (c) 2000 BSDi
7  * Copyright (c) 2001-2010 Adaptec, Inc.
8  * Copyright (c) 2010-2012 PMC-Sierra, Inc.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/bio.h>
34 #include <sys/callout.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/taskqueue.h>
38 #include <sys/selinfo.h>
39 #include <geom/geom_disk.h>
40 
41 #define	AAC_TYPE_DEVO			1
42 #define	AAC_TYPE_ALPHA			2
43 #define	AAC_TYPE_BETA			3
44 #define	AAC_TYPE_RELEASE		4
45 
46 #define	AAC_DRIVER_MAJOR_VERSION	3
47 #define	AAC_DRIVER_MINOR_VERSION	2
48 #define	AAC_DRIVER_BUGFIX_LEVEL		10
49 #define	AAC_DRIVER_TYPE			AAC_TYPE_RELEASE
50 
51 #ifndef AAC_DRIVER_BUILD
52 # define AAC_DRIVER_BUILD 1
53 #endif
54 
55 /* **************************** NewBUS interrupt Crock ************************/
56 #define	aac_bus_setup_intr	bus_setup_intr
57 
58 /* **************************** NewBUS CAM Support ****************************/
59 #define aac_xpt_bus_register	xpt_bus_register
60 
61 /**************************** Kernel Thread Support ***************************/
62 #define aac_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
63 	kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
64 #define	aac_kthread_exit(status)	\
65 	kproc_exit(status)
66 
67 /*
68  * Driver Parameter Definitions
69  */
70 
71 /*
72  * We allocate a small set of FIBs for the adapter to use to send us messages.
73  */
74 #define AAC_ADAPTER_FIBS	8
75 
76 /*
77  * The controller reports status events in AIFs.  We hang on to a number of
78  * these in order to pass them out to user-space management tools.
79  */
80 #define AAC_AIFQ_LENGTH		64
81 
82 /*
83  * Firmware messages are passed in the printf buffer.
84  */
85 #define AAC_PRINTF_BUFSIZE	256
86 
87 /*
88  * We wait this many seconds for the adapter to come ready if it is still
89  * booting
90  */
91 #define AAC_BOOT_TIMEOUT	(3 * 60)
92 
93 /*
94  * We wait this many seconds for the adapter to come ready
95  * after flash update
96  */
97 #define AAC_FWUPD_TIMEOUT	(5 * 60)
98 
99 /*
100  * Timeout for sync. commands.
101  */
102 #define AAC_SYNC_TIMEOUT	180		/* seconds */
103 
104 /*
105  * Timeout for normal commands
106  */
107 #define AAC_CMD_TIMEOUT		180		/* seconds */
108 
109 /*
110  * Rate at which we periodically check for timed out commands and kick the
111  * controller.
112  */
113 #define AAC_PERIODIC_INTERVAL	20		/* seconds */
114 
115 #define PASSTHROUGH_BUS		0
116 #define CONTAINER_BUS		1
117 /*
118  * Per-container data structure
119  */
120 struct aac_container
121 {
122 	struct aac_mntobj		co_mntobj;
123 	int				co_found;
124 	u_int32_t		co_uid;
125 	TAILQ_ENTRY(aac_container)	co_link;
126 };
127 
128 /*
129  * Per-SIM data structure
130  */
131 struct aac_cam;
132 struct aac_sim
133 {
134 	device_t		sim_dev;
135 	int			TargetsPerBus;
136 	int			BusNumber;
137 	int			BusType;
138 	int			InitiatorBusId;
139 	struct aac_softc	*aac_sc;
140 	struct aac_cam		*aac_cam;
141 	TAILQ_ENTRY(aac_sim)	sim_link;
142 };
143 
144 /*
145  * Per-disk structure
146  */
147 struct aac_disk
148 {
149 	device_t			ad_dev;
150 	struct aac_softc		*ad_controller;
151 	struct aac_container		*ad_container;
152 	struct disk			*ad_disk;
153 	int				ad_flags;
154 #define AAC_DISK_OPEN	(1<<0)
155 	int				ad_cylinders;
156 	int				ad_heads;
157 	int				ad_sectors;
158 	u_int64_t			ad_size;
159 	int				unit;
160 };
161 
162 /*
163  * Per-command control structure.
164  */
165 struct aac_command
166 {
167 	TAILQ_ENTRY(aac_command) cm_link;	/* list linkage */
168 
169 	struct aac_softc	*cm_sc;		/* controller that owns us */
170 
171 	struct aac_fib		*cm_fib;	/* FIB associated with this
172 						 * command */
173 	u_int64_t		cm_fibphys;	/* bus address of the FIB */
174 	struct bio		*cm_data;	/* pointer to data in kernel
175 						 * space */
176 	u_int32_t		cm_datalen;	/* data length */
177 	bus_dmamap_t		cm_datamap;	/* DMA map for bio data */
178 	struct aac_sg_table	*cm_sgtable;	/* pointer to s/g table in
179 						 * command */
180 	int			cm_flags;
181 #define AAC_CMD_MAPPED		(1<<0)		/* command has had its data
182 						 * mapped */
183 #define AAC_CMD_DATAIN		(1<<1)		/* command involves data moving
184 						 * from controller to host */
185 #define AAC_CMD_DATAOUT		(1<<2)		/* command involves data moving
186 						 * from host to controller */
187 #define AAC_CMD_COMPLETED	(1<<3)		/* command has been completed */
188 #define AAC_CMD_TIMEDOUT	(1<<4)		/* command taken too long */
189 #define AAC_ON_AACQ_FREE	(1<<5)
190 #define AAC_ON_AACQ_READY	(1<<6)
191 #define AAC_ON_AACQ_BUSY	(1<<7)
192 #define AAC_ON_AACQ_AIF		(1<<8)
193 #define AAC_ON_AACQ_NORM	(1<<10)
194 #define AAC_ON_AACQ_MASK	((1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<10))
195 #define AAC_CMD_RESET		(1<<9)
196 #define AAC_CMD_FASTRESP	(1<<11)
197 #define AAC_CMD_WAIT		(1<<12)
198 
199 	void			(* cm_complete)(struct aac_command *cm);
200 	union ccb 		*cm_ccb;
201 	time_t			cm_timestamp;	/* command creation time */
202 	int			cm_index;
203 	bus_dma_tag_t		cm_passthr_dmat;	/* passthrough buffer/command
204 							 * DMA tag */
205 };
206 
207 struct aac_fibmap {
208 	TAILQ_ENTRY(aac_fibmap) fm_link;	/* list linkage */
209 	struct aac_fib		*aac_fibs;
210 	bus_dmamap_t		aac_fibmap;
211 	struct aac_command	*aac_commands;
212 };
213 
214 /*
215  * We gather a number of adapter-visible items into a single structure.
216  *
217  * The ordering of this strucure may be important; we copy the Linux driver:
218  *
219  * Adapter FIBs
220  * Init struct
221  * Queue headers (Comm Area)
222  * Printf buffer
223  *
224  * In addition, we add:
225  * Sync Fib
226  */
227 struct aac_common {
228 	/* fibs for the controller to send us messages */
229 	struct aac_fib		ac_fibs[AAC_ADAPTER_FIBS];
230 
231 	/* the init structure */
232 	struct aac_adapter_init	ac_init;
233 
234 	/* buffer for text messages from the controller */
235 	char		       	ac_printf[AAC_PRINTF_BUFSIZE];
236 
237 	/* fib for synchronous commands */
238 	struct aac_fib		ac_sync_fib;
239 
240 	/* response buffer for SRC (new comm. type1) - must be last element */
241 	u_int32_t   ac_host_rrq[0];
242 };
243 
244 /*
245  * Interface operations
246  */
247 struct aac_interface
248 {
249 	int	(*aif_get_fwstatus)(struct aac_softc *sc);
250 	void	(*aif_qnotify)(struct aac_softc *sc, int qbit);
251 	int	(*aif_get_istatus)(struct aac_softc *sc);
252 	void	(*aif_clr_istatus)(struct aac_softc *sc, int mask);
253 	void	(*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
254 				   u_int32_t arg0, u_int32_t arg1,
255 				   u_int32_t arg2, u_int32_t arg3);
256 	int	(*aif_get_mailbox)(struct aac_softc *sc, int mb);
257 	void	(*aif_access_devreg)(struct aac_softc *sc, int enable);
258 	int (*aif_send_command)(struct aac_softc *sc, struct aac_command *cm);
259 	int (*aif_get_outb_queue)(struct aac_softc *sc);
260 	void (*aif_set_outb_queue)(struct aac_softc *sc, int index);
261 };
262 extern struct aac_interface	aacraid_src_interface;
263 extern struct aac_interface	aacraid_srcv_interface;
264 
265 #define AAC_GET_FWSTATUS(sc)		((sc)->aac_if.aif_get_fwstatus((sc)))
266 #define AAC_QNOTIFY(sc, qbit)		((sc)->aac_if.aif_qnotify((sc), (qbit)))
267 #define AAC_GET_ISTATUS(sc)		((sc)->aac_if.aif_get_istatus((sc)))
268 #define AAC_CLEAR_ISTATUS(sc, mask)	((sc)->aac_if.aif_clr_istatus((sc), \
269 					(mask)))
270 #define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
271 	((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
272 	(arg3)))
273 #define AAC_GET_MAILBOX(sc, mb)		((sc)->aac_if.aif_get_mailbox((sc), \
274 					(mb)))
275 #define	AAC_ACCESS_DEVREG(sc, mode)	((sc)->aac_if.aif_access_devreg((sc), \
276 					mode))
277 #define AAC_SEND_COMMAND(sc, cm)	((sc)->aac_if.aif_send_command((sc), (cm)))
278 #define AAC_GET_OUTB_QUEUE(sc)		((sc)->aac_if.aif_get_outb_queue((sc)))
279 #define AAC_SET_OUTB_QUEUE(sc, idx)	((sc)->aac_if.aif_set_outb_queue((sc), (idx)))
280 
281 #define AAC_MEM0_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag0, \
282 					sc->aac_bhandle0, reg, val)
283 #define AAC_MEM0_GETREG4(sc, reg)	bus_space_read_4(sc->aac_btag0, \
284 					sc->aac_bhandle0, reg)
285 #define AAC_MEM0_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag0, \
286 					sc->aac_bhandle0, reg, val)
287 #define AAC_MEM0_GETREG2(sc, reg)	bus_space_read_2(sc->aac_btag0, \
288 					sc->aac_bhandle0, reg)
289 #define AAC_MEM0_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag0, \
290 					sc->aac_bhandle0, reg, val)
291 #define AAC_MEM0_GETREG1(sc, reg)	bus_space_read_1(sc->aac_btag0, \
292 					sc->aac_bhandle0, reg)
293 
294 #define AAC_MEM1_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag1, \
295 					sc->aac_bhandle1, reg, val)
296 #define AAC_MEM1_GETREG4(sc, reg)	bus_space_read_4(sc->aac_btag1, \
297 					sc->aac_bhandle1, reg)
298 #define AAC_MEM1_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag1, \
299 					sc->aac_bhandle1, reg, val)
300 #define AAC_MEM1_GETREG2(sc, reg)	bus_space_read_2(sc->aac_btag1, \
301 					sc->aac_bhandle1, reg)
302 #define AAC_MEM1_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag1, \
303 					sc->aac_bhandle1, reg, val)
304 #define AAC_MEM1_GETREG1(sc, reg)	bus_space_read_1(sc->aac_btag1, \
305 					sc->aac_bhandle1, reg)
306 
307 /* fib context (IOCTL) */
308 struct aac_fib_context {
309 	u_int32_t		unique;
310 	int			ctx_idx;
311 	int			ctx_wrap;
312 	struct aac_fib_context *next, *prev;
313 };
314 
315 /* MSIX context */
316 struct aac_msix_ctx {
317 	int			vector_no;
318 	struct aac_softc  	*sc;
319 };
320 
321 /*
322  * Per-controller structure.
323  */
324 struct aac_softc
325 {
326 	/* bus connections */
327 	device_t		aac_dev;
328 	struct resource		*aac_regs_res0, *aac_regs_res1; /* reg. if. window */
329 	int			aac_regs_rid0, aac_regs_rid1;		/* resource ID */
330 	bus_space_handle_t	aac_bhandle0, aac_bhandle1;		/* bus space handle */
331 	bus_space_tag_t		aac_btag0, aac_btag1;		/* bus space tag */
332 	bus_dma_tag_t		aac_parent_dmat;	/* parent DMA tag */
333 	bus_dma_tag_t		aac_buffer_dmat;	/* data buffer/command
334 							 * DMA tag */
335 	struct resource		*aac_irq[AAC_MAX_MSIX];	 /* interrupt */
336 	int			aac_irq_rid[AAC_MAX_MSIX];
337 	void			*aac_intr[AAC_MAX_MSIX]; /* interrupt handle */
338 	struct aac_msix_ctx	aac_msix[AAC_MAX_MSIX]; /* context */
339 	eventhandler_tag	eh;
340 	struct callout	aac_daemontime;		/* clock daemon callout */
341 
342 	/* controller features, limits and status */
343 	int			aac_state;
344 #define AAC_STATE_SUSPEND	(1<<0)
345 #define	AAC_STATE_UNUSED0	(1<<1)
346 #define AAC_STATE_INTERRUPTS_ON	(1<<2)
347 #define AAC_STATE_AIF_SLEEPER	(1<<3)
348 #define AAC_STATE_RESET		(1<<4)
349 	struct FsaRevision		aac_revision;
350 
351 	/* controller hardware interface */
352 	int			aac_hwif;
353 #define AAC_HWIF_SRC		5
354 #define AAC_HWIF_SRCV		6
355 #define AAC_HWIF_UNKNOWN	-1
356 	bus_dma_tag_t		aac_common_dmat;	/* common structure
357 							 * DMA tag */
358 	bus_dmamap_t		aac_common_dmamap;	/* common structure
359 							 * DMA map */
360 	struct aac_common	*aac_common;
361 	u_int32_t		aac_common_busaddr;
362 	u_int32_t		aac_host_rrq_idx[AAC_MAX_MSIX];
363 	u_int32_t		aac_rrq_outstanding[AAC_MAX_MSIX];
364 	u_int32_t		aac_fibs_pushed_no;
365 	struct aac_interface	aac_if;
366 
367 	/* command/fib resources */
368 	bus_dma_tag_t		aac_fib_dmat;	/* DMA tag for allocing FIBs */
369 	TAILQ_HEAD(,aac_fibmap)	aac_fibmap_tqh;
370 	u_int			total_fibs;
371 	struct aac_command	*aac_commands;
372 
373 	/* command management */
374 	TAILQ_HEAD(,aac_command) aac_free;	/* command structures
375 						 * available for reuse */
376 	TAILQ_HEAD(,aac_command) aac_ready;	/* commands on hold for
377 						 * controller resources */
378 	TAILQ_HEAD(,aac_command) aac_busy;
379 	TAILQ_HEAD(,aac_event)	aac_ev_cmfree;
380 	struct bio_queue_head	aac_bioq;
381 
382 	struct aac_qstat	aac_qstat[AACQ_COUNT];	/* queue statistics */
383 
384 	/* connected containters */
385 	TAILQ_HEAD(,aac_container)	aac_container_tqh;
386 	struct mtx		aac_container_lock;
387 
388 	/*
389 	 * The general I/O lock.  This protects the sync fib, the lists, the
390 	 * queues, and the registers.
391 	 */
392 	struct mtx		aac_io_lock;
393 
394 	struct intr_config_hook	aac_ich;
395 
396 	/* sync. transfer mode */
397 	struct aac_command *aac_sync_cm;
398 
399 	/* management interface */
400 	struct cdev *aac_dev_t;
401 	struct mtx		aac_aifq_lock;
402 	struct aac_fib		aac_aifq[AAC_AIFQ_LENGTH];
403 	int			aifq_idx;
404 	int			aifq_filled;
405 	int 			aif_pending;
406 	struct aac_fib_context *fibctx;
407 	struct selinfo		rcv_select;
408 	struct proc		*aifthread;
409 	int			aifflags;
410 #define AAC_AIFFLAGS_RUNNING	(1 << 0)
411 #define AAC_AIFFLAGS_AIF	(1 << 1)
412 #define	AAC_AIFFLAGS_EXIT	(1 << 2)
413 #define AAC_AIFFLAGS_EXITED	(1 << 3)
414 #define AAC_AIFFLAGS_PRINTF	(1 << 4)
415 #define	AAC_AIFFLAGS_ALLOCFIBS	(1 << 5)
416 #define AAC_AIFFLAGS_PENDING	(AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \
417 				 AAC_AIFFLAGS_ALLOCFIBS)
418 	u_int32_t		flags;
419 #define AAC_FLAGS_PERC2QC	(1 << 0)
420 #define	AAC_FLAGS_ENABLE_CAM	(1 << 1)	/* No SCSI passthrough */
421 #define	AAC_FLAGS_CAM_NORESET	(1 << 2)	/* Fake SCSI resets */
422 #define	AAC_FLAGS_CAM_PASSONLY	(1 << 3)	/* Only create pass devices */
423 #define	AAC_FLAGS_SG_64BIT	(1 << 4)	/* Use 64-bit S/G addresses */
424 #define	AAC_FLAGS_4GB_WINDOW	(1 << 5)	/* Device can access host mem
425 						 * 2GB-4GB range */
426 #define	AAC_FLAGS_NO4GB		(1 << 6)	/* Can't access host mem >2GB */
427 #define	AAC_FLAGS_256FIBS	(1 << 7)	/* Can only do 256 commands */
428 #define	AAC_FLAGS_BROKEN_MEMMAP (1 << 8)	/* Broken HostPhysMemPages */
429 #define AAC_FLAGS_SLAVE	(1 << 9)
430 #define AAC_FLAGS_MASTER	(1 << 10)
431 #define AAC_FLAGS_NEW_COMM	(1 << 11)	/* New comm. interface supported */
432 #define AAC_FLAGS_RAW_IO	(1 << 12)	/* Raw I/O interface */
433 #define AAC_FLAGS_ARRAY_64BIT	(1 << 13)	/* 64-bit array size */
434 #define AAC_FLAGS_LBA_64BIT	(1 << 14)	/* 64-bit LBA support */
435 #define AAC_QUEUE_FRZN		(1 << 15)	/* Freeze the processing of
436 						 * commands on the queue. */
437 #define AAC_FLAGS_NEW_COMM_TYPE1 (1 << 16)	/* New comm. type1 supported */
438 #define AAC_FLAGS_NEW_COMM_TYPE2 (1 << 17)	/* New comm. type2 supported */
439 #define AAC_FLAGS_NEW_COMM_TYPE34 (1 << 18)	/* New comm. type3/4 */
440 #define AAC_FLAGS_SYNC_MODE (1 << 18)	/* Sync. transfer mode */
441 	u_int32_t		hint_flags;		/* driver parameters */
442 	int	sim_freezed;				/* flag for sim_freeze/release */
443 	u_int32_t		supported_options;
444 	u_int32_t		scsi_method_id;
445 	TAILQ_HEAD(,aac_sim)	aac_sim_tqh;
446 
447 	u_int32_t	aac_max_fibs;           /* max. FIB count */
448 	u_int32_t	aac_max_fibs_alloc;		/* max. alloc. per alloc_commands() */
449 	u_int32_t	aac_max_fib_size;		/* max. FIB size */
450 	u_int32_t	aac_sg_tablesize;		/* max. sg count from host */
451 	u_int32_t	aac_max_sectors;		/* max. I/O size from host (blocks) */
452 	u_int32_t	aac_feature_bits;		/* feature bits from suppl. info */
453 	u_int32_t	aac_support_opt2;		/* supp. options from suppl. info */
454 	u_int32_t	aac_max_aif;			/* max. AIF count */
455 	u_int32_t	doorbell_mask;			/* for IOP reset */
456 	u_int32_t	aac_max_msix;			/* max. MSI-X vectors */
457 	u_int32_t	aac_vector_cap;			/* MSI-X vector capab.*/
458 	int		msi_enabled;			/* MSI/MSI-X enabled */
459 	int		msi_tupelo;		/* Series 6 support for */
460 						/* single MSI interrupt */
461 #define AAC_CAM_TARGET_WILDCARD ~0
462 	void			(*cam_rescan_cb)(struct aac_softc *, uint32_t,
463 				    uint32_t);
464 	u_int32_t	DebugFlags;		/* Debug print flags bitmap */
465 	u_int32_t	DebugOffset;		/* Offset from DPMEM start */
466 	u_int32_t	DebugHeaderSize;	/* Size of debug header */
467 	u_int32_t	FwDebugFlags;		/* FW Debug Flags */
468 	u_int32_t	FwDebugBufferSize;	/* FW Debug Buffer size */
469 };
470 
471 /*
472  * Max. I/O size in bytes.
473  * Reserve one page for the DMA subsystem, that may need it when the
474  * I/O buffer is not page aligned.
475  */
476 #define AAC_MAXIO_SIZE(sc)	MIN(((sc)->aac_max_sectors << 9) - PAGE_SIZE, \
477 					maxphys)
478 
479 /*
480  * Event callback mechanism for the driver
481  */
482 #define AAC_EVENT_NONE		0x00
483 #define AAC_EVENT_CMFREE	0x01
484 #define	AAC_EVENT_MASK		0xff
485 #define AAC_EVENT_REPEAT	0x100
486 
487 typedef void aac_event_cb_t(struct aac_softc *sc, struct aac_event *event,
488     void *arg);
489 struct aac_event {
490 	TAILQ_ENTRY(aac_event)	ev_links;
491 	int			ev_type;
492 	aac_event_cb_t		*ev_callback;
493 	void			*ev_arg;
494 };
495 
496 /*
497  * Public functions
498  */
499 extern void		aacraid_free(struct aac_softc *sc);
500 extern int		aacraid_attach(struct aac_softc *sc);
501 extern int		aacraid_detach(device_t dev);
502 extern int		aacraid_shutdown(device_t dev);
503 extern int		aacraid_suspend(device_t dev);
504 extern int		aacraid_resume(device_t dev);
505 extern void		aacraid_new_intr_type1(void *arg);
506 extern void		aacraid_submit_bio(struct bio *bp);
507 extern void		aacraid_biodone(struct bio *bp);
508 extern void		aacraid_startio(struct aac_softc *sc);
509 extern int		aacraid_alloc_command(struct aac_softc *sc,
510 					  struct aac_command **cmp);
511 extern void		aacraid_release_command(struct aac_command *cm);
512 extern void		aacraid_add_event(struct aac_softc *sc, struct aac_event
513 				      *event);
514 extern void		aacraid_map_command_sg(void *arg, bus_dma_segment_t *segs,
515 				int nseg, int error);
516 extern int		aacraid_wait_command(struct aac_command *cmp);
517 
518 #ifdef AACRAID_DEBUG
519 # define fwprintf(sc, flags, fmt, args...)				\
520 	aacraid_fw_printf(sc, flags, "%s: " fmt, __func__, ##args);
521 
522 extern void	aacraid_print_queues(struct aac_softc *sc);
523 extern void	aacraid_print_fib(struct aac_softc *sc, struct aac_fib *fib,
524 			      const char *caller);
525 extern void	aacraid_print_aif(struct aac_softc *sc,
526 			      struct aac_aif_command *aif);
527 
528 #define AAC_PRINT_FIB(sc, fib)	aacraid_print_fib(sc, fib, __func__)
529 
530 #else
531 # define fwprintf(sc, flags, fmt, args...)
532 
533 # define aacraid_print_queues(sc)
534 
535 # define AAC_PRINT_FIB(sc, fib)
536 # define aacraid_print_aif(sc, aac_aif_command)
537 #endif
538 
539 struct aac_code_lookup {
540 	char	*string;
541 	u_int32_t	code;
542 };
543 
544 /*
545  * Queue primitives for driver queues.
546  */
547 #define AACQ_ADD(sc, qname)					\
548 	do {							\
549 		struct aac_qstat *qs;				\
550 								\
551 		qs = &(sc)->aac_qstat[qname];			\
552 								\
553 		qs->q_length++;					\
554 		if (qs->q_length > qs->q_max)			\
555 			qs->q_max = qs->q_length;		\
556 	} while (0)
557 
558 #define AACQ_REMOVE(sc, qname)    (sc)->aac_qstat[qname].q_length--
559 #define AACQ_INIT(sc, qname)				\
560 	do {						\
561 		sc->aac_qstat[qname].q_length = 0;	\
562 		sc->aac_qstat[qname].q_max = 0;		\
563 	} while (0)
564 
565 #define AACQ_COMMAND_QUEUE(name, index)					\
566 static __inline void							\
567 aac_initq_ ## name (struct aac_softc *sc)				\
568 {									\
569 	TAILQ_INIT(&sc->aac_ ## name);					\
570 	AACQ_INIT(sc, index);						\
571 }									\
572 static __inline void							\
573 aac_enqueue_ ## name (struct aac_command *cm)				\
574 {									\
575 	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
576 		printf("command %p is on another queue, flags = %#x\n",	\
577 		       cm, cm->cm_flags);				\
578 		panic("command is on another queue");			\
579 	}								\
580 	TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
581 	cm->cm_flags |= AAC_ON_ ## index;				\
582 	AACQ_ADD(cm->cm_sc, index);					\
583 }									\
584 static __inline void							\
585 aac_requeue_ ## name (struct aac_command *cm)				\
586 {									\
587 	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
588 		printf("command %p is on another queue, flags = %#x\n",	\
589 		       cm, cm->cm_flags);				\
590 		panic("command is on another queue");			\
591 	}								\
592 	TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
593 	cm->cm_flags |= AAC_ON_ ## index;				\
594 	AACQ_ADD(cm->cm_sc, index);					\
595 }									\
596 static __inline struct aac_command *					\
597 aac_dequeue_ ## name (struct aac_softc *sc)				\
598 {									\
599 	struct aac_command *cm;						\
600 									\
601 	if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) {		\
602 		if ((cm->cm_flags & AAC_ON_ ## index) == 0) {		\
603 			printf("command %p not in queue, flags = %#x, "	\
604 		       	       "bit = %#x\n", cm, cm->cm_flags,		\
605 			       AAC_ON_ ## index);			\
606 			panic("command not in queue");			\
607 		}							\
608 		TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link);		\
609 		cm->cm_flags &= ~AAC_ON_ ## index;			\
610 		AACQ_REMOVE(sc, index);					\
611 	}								\
612 	return(cm);							\
613 }									\
614 static __inline void							\
615 aac_remove_ ## name (struct aac_command *cm)				\
616 {									\
617 	if ((cm->cm_flags & AAC_ON_ ## index) == 0) {			\
618 		printf("command %p not in queue, flags = %#x, "		\
619 		       "bit = %#x\n", cm, cm->cm_flags, 		\
620 		       AAC_ON_ ## index);				\
621 		panic("command not in queue");				\
622 	}								\
623 	TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
624 	cm->cm_flags &= ~AAC_ON_ ## index;				\
625 	AACQ_REMOVE(cm->cm_sc, index);					\
626 }									\
627 struct hack
628 
629 AACQ_COMMAND_QUEUE(free, AACQ_FREE);
630 AACQ_COMMAND_QUEUE(ready, AACQ_READY);
631 AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
632 
633 static __inline void
634 aac_print_printf(struct aac_softc *sc)
635 {
636 	/*
637 	 * XXX We have the ability to read the length of the printf string
638 	 * from out of the mailboxes.
639 	 */
640 	device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
641 		      sc->aac_common->ac_printf);
642 	sc->aac_common->ac_printf[0] = 0;
643 	AAC_QNOTIFY(sc, AAC_DB_PRINTF);
644 }
645 
646 static __inline int
647 aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib)
648 {
649 
650 	mtx_assert(&sc->aac_io_lock, MA_OWNED);
651 	*fib = &sc->aac_common->ac_sync_fib;
652 	return (0);
653 }
654 
655 static __inline void
656 aac_release_sync_fib(struct aac_softc *sc)
657 {
658 
659 	mtx_assert(&sc->aac_io_lock, MA_OWNED);
660 }
661