xref: /freebsd/sys/dev/aac/aac.c (revision aa0a1e58)
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 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*
34  * Driver for the Adaptec 'FSA' family of PCI/SCSI RAID adapters.
35  */
36 #define AAC_DRIVERNAME			"aac"
37 
38 #include "opt_aac.h"
39 
40 /* #include <stddef.h> */
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
45 #include <sys/kthread.h>
46 #include <sys/sysctl.h>
47 #include <sys/poll.h>
48 #include <sys/ioccom.h>
49 
50 #include <sys/bus.h>
51 #include <sys/conf.h>
52 #include <sys/signalvar.h>
53 #include <sys/time.h>
54 #include <sys/eventhandler.h>
55 #include <sys/rman.h>
56 
57 #include <machine/bus.h>
58 #include <sys/bus_dma.h>
59 #include <machine/resource.h>
60 
61 #include <dev/pci/pcireg.h>
62 #include <dev/pci/pcivar.h>
63 
64 #include <dev/aac/aacreg.h>
65 #include <sys/aac_ioctl.h>
66 #include <dev/aac/aacvar.h>
67 #include <dev/aac/aac_tables.h>
68 
69 static void	aac_startup(void *arg);
70 static void	aac_add_container(struct aac_softc *sc,
71 				  struct aac_mntinforesp *mir, int f);
72 static void	aac_get_bus_info(struct aac_softc *sc);
73 static void	aac_daemon(void *arg);
74 
75 /* Command Processing */
76 static void	aac_timeout(struct aac_softc *sc);
77 static void	aac_complete(void *context, int pending);
78 static int	aac_bio_command(struct aac_softc *sc, struct aac_command **cmp);
79 static void	aac_bio_complete(struct aac_command *cm);
80 static int	aac_wait_command(struct aac_command *cm);
81 static void	aac_command_thread(struct aac_softc *sc);
82 
83 /* Command Buffer Management */
84 static void	aac_map_command_sg(void *arg, bus_dma_segment_t *segs,
85 				   int nseg, int error);
86 static void	aac_map_command_helper(void *arg, bus_dma_segment_t *segs,
87 				       int nseg, int error);
88 static int	aac_alloc_commands(struct aac_softc *sc);
89 static void	aac_free_commands(struct aac_softc *sc);
90 static void	aac_unmap_command(struct aac_command *cm);
91 
92 /* Hardware Interface */
93 static int	aac_alloc(struct aac_softc *sc);
94 static void	aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg,
95 			       int error);
96 static int	aac_check_firmware(struct aac_softc *sc);
97 static int	aac_init(struct aac_softc *sc);
98 static int	aac_sync_command(struct aac_softc *sc, u_int32_t command,
99 				 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2,
100 				 u_int32_t arg3, u_int32_t *sp);
101 static int	aac_setup_intr(struct aac_softc *sc);
102 static int	aac_enqueue_fib(struct aac_softc *sc, int queue,
103 				struct aac_command *cm);
104 static int	aac_dequeue_fib(struct aac_softc *sc, int queue,
105 				u_int32_t *fib_size, struct aac_fib **fib_addr);
106 static int	aac_enqueue_response(struct aac_softc *sc, int queue,
107 				     struct aac_fib *fib);
108 
109 /* StrongARM interface */
110 static int	aac_sa_get_fwstatus(struct aac_softc *sc);
111 static void	aac_sa_qnotify(struct aac_softc *sc, int qbit);
112 static int	aac_sa_get_istatus(struct aac_softc *sc);
113 static void	aac_sa_clear_istatus(struct aac_softc *sc, int mask);
114 static void	aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
115 				   u_int32_t arg0, u_int32_t arg1,
116 				   u_int32_t arg2, u_int32_t arg3);
117 static int	aac_sa_get_mailbox(struct aac_softc *sc, int mb);
118 static void	aac_sa_set_interrupts(struct aac_softc *sc, int enable);
119 
120 struct aac_interface aac_sa_interface = {
121 	aac_sa_get_fwstatus,
122 	aac_sa_qnotify,
123 	aac_sa_get_istatus,
124 	aac_sa_clear_istatus,
125 	aac_sa_set_mailbox,
126 	aac_sa_get_mailbox,
127 	aac_sa_set_interrupts,
128 	NULL, NULL, NULL
129 };
130 
131 /* i960Rx interface */
132 static int	aac_rx_get_fwstatus(struct aac_softc *sc);
133 static void	aac_rx_qnotify(struct aac_softc *sc, int qbit);
134 static int	aac_rx_get_istatus(struct aac_softc *sc);
135 static void	aac_rx_clear_istatus(struct aac_softc *sc, int mask);
136 static void	aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
137 				   u_int32_t arg0, u_int32_t arg1,
138 				   u_int32_t arg2, u_int32_t arg3);
139 static int	aac_rx_get_mailbox(struct aac_softc *sc, int mb);
140 static void	aac_rx_set_interrupts(struct aac_softc *sc, int enable);
141 static int aac_rx_send_command(struct aac_softc *sc, struct aac_command *cm);
142 static int aac_rx_get_outb_queue(struct aac_softc *sc);
143 static void aac_rx_set_outb_queue(struct aac_softc *sc, int index);
144 
145 struct aac_interface aac_rx_interface = {
146 	aac_rx_get_fwstatus,
147 	aac_rx_qnotify,
148 	aac_rx_get_istatus,
149 	aac_rx_clear_istatus,
150 	aac_rx_set_mailbox,
151 	aac_rx_get_mailbox,
152 	aac_rx_set_interrupts,
153 	aac_rx_send_command,
154 	aac_rx_get_outb_queue,
155 	aac_rx_set_outb_queue
156 };
157 
158 /* Rocket/MIPS interface */
159 static int	aac_rkt_get_fwstatus(struct aac_softc *sc);
160 static void	aac_rkt_qnotify(struct aac_softc *sc, int qbit);
161 static int	aac_rkt_get_istatus(struct aac_softc *sc);
162 static void	aac_rkt_clear_istatus(struct aac_softc *sc, int mask);
163 static void	aac_rkt_set_mailbox(struct aac_softc *sc, u_int32_t command,
164 				    u_int32_t arg0, u_int32_t arg1,
165 				    u_int32_t arg2, u_int32_t arg3);
166 static int	aac_rkt_get_mailbox(struct aac_softc *sc, int mb);
167 static void	aac_rkt_set_interrupts(struct aac_softc *sc, int enable);
168 static int aac_rkt_send_command(struct aac_softc *sc, struct aac_command *cm);
169 static int aac_rkt_get_outb_queue(struct aac_softc *sc);
170 static void aac_rkt_set_outb_queue(struct aac_softc *sc, int index);
171 
172 struct aac_interface aac_rkt_interface = {
173 	aac_rkt_get_fwstatus,
174 	aac_rkt_qnotify,
175 	aac_rkt_get_istatus,
176 	aac_rkt_clear_istatus,
177 	aac_rkt_set_mailbox,
178 	aac_rkt_get_mailbox,
179 	aac_rkt_set_interrupts,
180 	aac_rkt_send_command,
181 	aac_rkt_get_outb_queue,
182 	aac_rkt_set_outb_queue
183 };
184 
185 /* Debugging and Diagnostics */
186 static void	aac_describe_controller(struct aac_softc *sc);
187 static char	*aac_describe_code(struct aac_code_lookup *table,
188 				   u_int32_t code);
189 
190 /* Management Interface */
191 static d_open_t		aac_open;
192 static d_ioctl_t	aac_ioctl;
193 static d_poll_t		aac_poll;
194 static void		aac_cdevpriv_dtor(void *arg);
195 static int		aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib);
196 static int		aac_ioctl_send_raw_srb(struct aac_softc *sc, caddr_t arg);
197 static void		aac_handle_aif(struct aac_softc *sc,
198 					   struct aac_fib *fib);
199 static int		aac_rev_check(struct aac_softc *sc, caddr_t udata);
200 static int		aac_open_aif(struct aac_softc *sc, caddr_t arg);
201 static int		aac_close_aif(struct aac_softc *sc, caddr_t arg);
202 static int		aac_getnext_aif(struct aac_softc *sc, caddr_t arg);
203 static int		aac_return_aif(struct aac_softc *sc,
204 					struct aac_fib_context *ctx, caddr_t uptr);
205 static int		aac_query_disk(struct aac_softc *sc, caddr_t uptr);
206 static int		aac_get_pci_info(struct aac_softc *sc, caddr_t uptr);
207 static int		aac_supported_features(struct aac_softc *sc, caddr_t uptr);
208 static void		aac_ioctl_event(struct aac_softc *sc,
209 					struct aac_event *event, void *arg);
210 static struct aac_mntinforesp *
211 	aac_get_container_info(struct aac_softc *sc, struct aac_fib *fib, int cid);
212 
213 static struct cdevsw aac_cdevsw = {
214 	.d_version =	D_VERSION,
215 	.d_flags =	D_NEEDGIANT,
216 	.d_open =	aac_open,
217 	.d_ioctl =	aac_ioctl,
218 	.d_poll =	aac_poll,
219 	.d_name =	"aac",
220 };
221 
222 MALLOC_DEFINE(M_AACBUF, "aacbuf", "Buffers for the AAC driver");
223 
224 /* sysctl node */
225 SYSCTL_NODE(_hw, OID_AUTO, aac, CTLFLAG_RD, 0, "AAC driver parameters");
226 
227 /*
228  * Device Interface
229  */
230 
231 /*
232  * Initialize the controller and softc
233  */
234 int
235 aac_attach(struct aac_softc *sc)
236 {
237 	int error, unit;
238 
239 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
240 
241 	/*
242 	 * Initialize per-controller queues.
243 	 */
244 	aac_initq_free(sc);
245 	aac_initq_ready(sc);
246 	aac_initq_busy(sc);
247 	aac_initq_bio(sc);
248 
249 	/*
250 	 * Initialize command-completion task.
251 	 */
252 	TASK_INIT(&sc->aac_task_complete, 0, aac_complete, sc);
253 
254 	/* mark controller as suspended until we get ourselves organised */
255 	sc->aac_state |= AAC_STATE_SUSPEND;
256 
257 	/*
258 	 * Check that the firmware on the card is supported.
259 	 */
260 	if ((error = aac_check_firmware(sc)) != 0)
261 		return(error);
262 
263 	/*
264 	 * Initialize locks
265 	 */
266 	mtx_init(&sc->aac_aifq_lock, "AAC AIF lock", NULL, MTX_DEF);
267 	mtx_init(&sc->aac_io_lock, "AAC I/O lock", NULL, MTX_DEF);
268 	mtx_init(&sc->aac_container_lock, "AAC container lock", NULL, MTX_DEF);
269 	TAILQ_INIT(&sc->aac_container_tqh);
270 	TAILQ_INIT(&sc->aac_ev_cmfree);
271 
272 	/* Initialize the clock daemon callout. */
273 	callout_init_mtx(&sc->aac_daemontime, &sc->aac_io_lock, 0);
274 
275 	/*
276 	 * Initialize the adapter.
277 	 */
278 	if ((error = aac_alloc(sc)) != 0)
279 		return(error);
280 	if ((error = aac_init(sc)) != 0)
281 		return(error);
282 
283 	/*
284 	 * Allocate and connect our interrupt.
285 	 */
286 	if ((error = aac_setup_intr(sc)) != 0)
287 		return(error);
288 
289 	/*
290 	 * Print a little information about the controller.
291 	 */
292 	aac_describe_controller(sc);
293 
294 	/*
295 	 * Register to probe our containers later.
296 	 */
297 	sc->aac_ich.ich_func = aac_startup;
298 	sc->aac_ich.ich_arg = sc;
299 	if (config_intrhook_establish(&sc->aac_ich) != 0) {
300 		device_printf(sc->aac_dev,
301 			      "can't establish configuration hook\n");
302 		return(ENXIO);
303 	}
304 
305 	/*
306 	 * Make the control device.
307 	 */
308 	unit = device_get_unit(sc->aac_dev);
309 	sc->aac_dev_t = make_dev(&aac_cdevsw, unit, UID_ROOT, GID_OPERATOR,
310 				 0640, "aac%d", unit);
311 	(void)make_dev_alias(sc->aac_dev_t, "afa%d", unit);
312 	(void)make_dev_alias(sc->aac_dev_t, "hpn%d", unit);
313 	sc->aac_dev_t->si_drv1 = sc;
314 
315 	/* Create the AIF thread */
316 	if (kproc_create((void(*)(void *))aac_command_thread, sc,
317 		   &sc->aifthread, 0, 0, "aac%daif", unit))
318 		panic("Could not create AIF thread");
319 
320 	/* Register the shutdown method to only be called post-dump */
321 	if ((sc->eh = EVENTHANDLER_REGISTER(shutdown_final, aac_shutdown,
322 	    sc->aac_dev, SHUTDOWN_PRI_DEFAULT)) == NULL)
323 		device_printf(sc->aac_dev,
324 			      "shutdown event registration failed\n");
325 
326 	/* Register with CAM for the non-DASD devices */
327 	if ((sc->flags & AAC_FLAGS_ENABLE_CAM) != 0) {
328 		TAILQ_INIT(&sc->aac_sim_tqh);
329 		aac_get_bus_info(sc);
330 	}
331 
332 	mtx_lock(&sc->aac_io_lock);
333 	callout_reset(&sc->aac_daemontime, 60 * hz, aac_daemon, sc);
334 	mtx_unlock(&sc->aac_io_lock);
335 
336 	return(0);
337 }
338 
339 static void
340 aac_daemon(void *arg)
341 {
342 	struct timeval tv;
343 	struct aac_softc *sc;
344 	struct aac_fib *fib;
345 
346 	sc = arg;
347 	mtx_assert(&sc->aac_io_lock, MA_OWNED);
348 
349 	if (callout_pending(&sc->aac_daemontime) ||
350 	    callout_active(&sc->aac_daemontime) == 0)
351 		return;
352 	getmicrotime(&tv);
353 	aac_alloc_sync_fib(sc, &fib);
354 	*(uint32_t *)fib->data = tv.tv_sec;
355 	aac_sync_fib(sc, SendHostTime, 0, fib, sizeof(uint32_t));
356 	aac_release_sync_fib(sc);
357 	callout_schedule(&sc->aac_daemontime, 30 * 60 * hz);
358 }
359 
360 void
361 aac_add_event(struct aac_softc *sc, struct aac_event *event)
362 {
363 
364 	switch (event->ev_type & AAC_EVENT_MASK) {
365 	case AAC_EVENT_CMFREE:
366 		TAILQ_INSERT_TAIL(&sc->aac_ev_cmfree, event, ev_links);
367 		break;
368 	default:
369 		device_printf(sc->aac_dev, "aac_add event: unknown event %d\n",
370 		    event->ev_type);
371 		break;
372 	}
373 
374 	return;
375 }
376 
377 /*
378  * Request information of container #cid
379  */
380 static struct aac_mntinforesp *
381 aac_get_container_info(struct aac_softc *sc, struct aac_fib *fib, int cid)
382 {
383 	struct aac_mntinfo *mi;
384 
385 	mi = (struct aac_mntinfo *)&fib->data[0];
386 	/* use 64-bit LBA if enabled */
387 	mi->Command = (sc->flags & AAC_FLAGS_LBA_64BIT) ?
388 	    VM_NameServe64 : VM_NameServe;
389 	mi->MntType = FT_FILESYS;
390 	mi->MntCount = cid;
391 
392 	if (aac_sync_fib(sc, ContainerCommand, 0, fib,
393 			 sizeof(struct aac_mntinfo))) {
394 		device_printf(sc->aac_dev, "Error probing container %d\n", cid);
395 		return (NULL);
396 	}
397 
398 	return ((struct aac_mntinforesp *)&fib->data[0]);
399 }
400 
401 /*
402  * Probe for containers, create disks.
403  */
404 static void
405 aac_startup(void *arg)
406 {
407 	struct aac_softc *sc;
408 	struct aac_fib *fib;
409 	struct aac_mntinforesp *mir;
410 	int count = 0, i = 0;
411 
412 	sc = (struct aac_softc *)arg;
413 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
414 
415 	/* disconnect ourselves from the intrhook chain */
416 	config_intrhook_disestablish(&sc->aac_ich);
417 
418 	mtx_lock(&sc->aac_io_lock);
419 	aac_alloc_sync_fib(sc, &fib);
420 
421 	/* loop over possible containers */
422 	do {
423 		if ((mir = aac_get_container_info(sc, fib, i)) == NULL)
424 			continue;
425 		if (i == 0)
426 			count = mir->MntRespCount;
427 		aac_add_container(sc, mir, 0);
428 		i++;
429 	} while ((i < count) && (i < AAC_MAX_CONTAINERS));
430 
431 	aac_release_sync_fib(sc);
432 	mtx_unlock(&sc->aac_io_lock);
433 
434 	/* poke the bus to actually attach the child devices */
435 	if (bus_generic_attach(sc->aac_dev))
436 		device_printf(sc->aac_dev, "bus_generic_attach failed\n");
437 
438 	/* mark the controller up */
439 	sc->aac_state &= ~AAC_STATE_SUSPEND;
440 
441 	/* enable interrupts now */
442 	AAC_UNMASK_INTERRUPTS(sc);
443 }
444 
445 /*
446  * Create a device to represent a new container
447  */
448 static void
449 aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f)
450 {
451 	struct aac_container *co;
452 	device_t child;
453 
454 	/*
455 	 * Check container volume type for validity.  Note that many of
456 	 * the possible types may never show up.
457 	 */
458 	if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) {
459 		co = (struct aac_container *)malloc(sizeof *co, M_AACBUF,
460 		       M_NOWAIT | M_ZERO);
461 		if (co == NULL)
462 			panic("Out of memory?!");
463 		fwprintf(sc, HBA_FLAGS_DBG_INIT_B, "id %x  name '%.16s'  size %u  type %d",
464 		      mir->MntTable[0].ObjectId,
465 		      mir->MntTable[0].FileSystemName,
466 		      mir->MntTable[0].Capacity, mir->MntTable[0].VolType);
467 
468 		if ((child = device_add_child(sc->aac_dev, "aacd", -1)) == NULL)
469 			device_printf(sc->aac_dev, "device_add_child failed\n");
470 		else
471 			device_set_ivars(child, co);
472 		device_set_desc(child, aac_describe_code(aac_container_types,
473 				mir->MntTable[0].VolType));
474 		co->co_disk = child;
475 		co->co_found = f;
476 		bcopy(&mir->MntTable[0], &co->co_mntobj,
477 		      sizeof(struct aac_mntobj));
478 		mtx_lock(&sc->aac_container_lock);
479 		TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link);
480 		mtx_unlock(&sc->aac_container_lock);
481 	}
482 }
483 
484 /*
485  * Allocate resources associated with (sc)
486  */
487 static int
488 aac_alloc(struct aac_softc *sc)
489 {
490 
491 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
492 
493 	/*
494 	 * Create DMA tag for mapping buffers into controller-addressable space.
495 	 */
496 	if (bus_dma_tag_create(sc->aac_parent_dmat, 	/* parent */
497 			       1, 0, 			/* algnmnt, boundary */
498 			       (sc->flags & AAC_FLAGS_SG_64BIT) ?
499 			       BUS_SPACE_MAXADDR :
500 			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
501 			       BUS_SPACE_MAXADDR, 	/* highaddr */
502 			       NULL, NULL, 		/* filter, filterarg */
503 			       MAXBSIZE,		/* maxsize */
504 			       sc->aac_sg_tablesize,	/* nsegments */
505 			       MAXBSIZE,		/* maxsegsize */
506 			       BUS_DMA_ALLOCNOW,	/* flags */
507 			       busdma_lock_mutex,	/* lockfunc */
508 			       &sc->aac_io_lock,	/* lockfuncarg */
509 			       &sc->aac_buffer_dmat)) {
510 		device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
511 		return (ENOMEM);
512 	}
513 
514 	/*
515 	 * Create DMA tag for mapping FIBs into controller-addressable space..
516 	 */
517 	if (bus_dma_tag_create(sc->aac_parent_dmat,	/* parent */
518 			       1, 0, 			/* algnmnt, boundary */
519 			       (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
520 			       BUS_SPACE_MAXADDR_32BIT :
521 			       0x7fffffff,		/* lowaddr */
522 			       BUS_SPACE_MAXADDR, 	/* highaddr */
523 			       NULL, NULL, 		/* filter, filterarg */
524 			       sc->aac_max_fibs_alloc *
525 			       sc->aac_max_fib_size,  /* maxsize */
526 			       1,			/* nsegments */
527 			       sc->aac_max_fibs_alloc *
528 			       sc->aac_max_fib_size,	/* maxsize */
529 			       0,			/* flags */
530 			       NULL, NULL,		/* No locking needed */
531 			       &sc->aac_fib_dmat)) {
532 		device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");
533 		return (ENOMEM);
534 	}
535 
536 	/*
537 	 * Create DMA tag for the common structure and allocate it.
538 	 */
539 	if (bus_dma_tag_create(sc->aac_parent_dmat, 	/* parent */
540 			       1, 0,			/* algnmnt, boundary */
541 			       (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
542 			       BUS_SPACE_MAXADDR_32BIT :
543 			       0x7fffffff,		/* lowaddr */
544 			       BUS_SPACE_MAXADDR, 	/* highaddr */
545 			       NULL, NULL, 		/* filter, filterarg */
546 			       8192 + sizeof(struct aac_common), /* maxsize */
547 			       1,			/* nsegments */
548 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
549 			       0,			/* flags */
550 			       NULL, NULL,		/* No locking needed */
551 			       &sc->aac_common_dmat)) {
552 		device_printf(sc->aac_dev,
553 			      "can't allocate common structure DMA tag\n");
554 		return (ENOMEM);
555 	}
556 	if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common,
557 			     BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) {
558 		device_printf(sc->aac_dev, "can't allocate common structure\n");
559 		return (ENOMEM);
560 	}
561 
562 	/*
563 	 * Work around a bug in the 2120 and 2200 that cannot DMA commands
564 	 * below address 8192 in physical memory.
565 	 * XXX If the padding is not needed, can it be put to use instead
566 	 * of ignored?
567 	 */
568 	(void)bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap,
569 			sc->aac_common, 8192 + sizeof(*sc->aac_common),
570 			aac_common_map, sc, 0);
571 
572 	if (sc->aac_common_busaddr < 8192) {
573 		sc->aac_common = (struct aac_common *)
574 		    ((uint8_t *)sc->aac_common + 8192);
575 		sc->aac_common_busaddr += 8192;
576 	}
577 	bzero(sc->aac_common, sizeof(*sc->aac_common));
578 
579 	/* Allocate some FIBs and associated command structs */
580 	TAILQ_INIT(&sc->aac_fibmap_tqh);
581 	sc->aac_commands = malloc(sc->aac_max_fibs * sizeof(struct aac_command),
582 				  M_AACBUF, M_WAITOK|M_ZERO);
583 	while (sc->total_fibs < sc->aac_max_fibs) {
584 		if (aac_alloc_commands(sc) != 0)
585 			break;
586 	}
587 	if (sc->total_fibs == 0)
588 		return (ENOMEM);
589 
590 	return (0);
591 }
592 
593 /*
594  * Free all of the resources associated with (sc)
595  *
596  * Should not be called if the controller is active.
597  */
598 void
599 aac_free(struct aac_softc *sc)
600 {
601 
602 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
603 
604 	/* remove the control device */
605 	if (sc->aac_dev_t != NULL)
606 		destroy_dev(sc->aac_dev_t);
607 
608 	/* throw away any FIB buffers, discard the FIB DMA tag */
609 	aac_free_commands(sc);
610 	if (sc->aac_fib_dmat)
611 		bus_dma_tag_destroy(sc->aac_fib_dmat);
612 
613 	free(sc->aac_commands, M_AACBUF);
614 
615 	/* destroy the common area */
616 	if (sc->aac_common) {
617 		bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap);
618 		bus_dmamem_free(sc->aac_common_dmat, sc->aac_common,
619 				sc->aac_common_dmamap);
620 	}
621 	if (sc->aac_common_dmat)
622 		bus_dma_tag_destroy(sc->aac_common_dmat);
623 
624 	/* disconnect the interrupt handler */
625 	if (sc->aac_intr)
626 		bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr);
627 	if (sc->aac_irq != NULL)
628 		bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid,
629 				     sc->aac_irq);
630 
631 	/* destroy data-transfer DMA tag */
632 	if (sc->aac_buffer_dmat)
633 		bus_dma_tag_destroy(sc->aac_buffer_dmat);
634 
635 	/* destroy the parent DMA tag */
636 	if (sc->aac_parent_dmat)
637 		bus_dma_tag_destroy(sc->aac_parent_dmat);
638 
639 	/* release the register window mapping */
640 	if (sc->aac_regs_res0 != NULL)
641 		bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
642 				     sc->aac_regs_rid0, sc->aac_regs_res0);
643 	if (sc->aac_hwif == AAC_HWIF_NARK && sc->aac_regs_res1 != NULL)
644 		bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
645 				     sc->aac_regs_rid1, sc->aac_regs_res1);
646 }
647 
648 /*
649  * Disconnect from the controller completely, in preparation for unload.
650  */
651 int
652 aac_detach(device_t dev)
653 {
654 	struct aac_softc *sc;
655 	struct aac_container *co;
656 	struct aac_sim	*sim;
657 	int error;
658 
659 	sc = device_get_softc(dev);
660 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
661 
662 	callout_drain(&sc->aac_daemontime);
663 
664 	/* Remove the child containers */
665 	while ((co = TAILQ_FIRST(&sc->aac_container_tqh)) != NULL) {
666 		error = device_delete_child(dev, co->co_disk);
667 		if (error)
668 			return (error);
669 		TAILQ_REMOVE(&sc->aac_container_tqh, co, co_link);
670 		free(co, M_AACBUF);
671 	}
672 
673 	/* Remove the CAM SIMs */
674 	while ((sim = TAILQ_FIRST(&sc->aac_sim_tqh)) != NULL) {
675 		TAILQ_REMOVE(&sc->aac_sim_tqh, sim, sim_link);
676 		error = device_delete_child(dev, sim->sim_dev);
677 		if (error)
678 			return (error);
679 		free(sim, M_AACBUF);
680 	}
681 
682 	if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
683 		sc->aifflags |= AAC_AIFFLAGS_EXIT;
684 		wakeup(sc->aifthread);
685 		tsleep(sc->aac_dev, PUSER | PCATCH, "aacdch", 30 * hz);
686 	}
687 
688 	if (sc->aifflags & AAC_AIFFLAGS_RUNNING)
689 		panic("Cannot shutdown AIF thread");
690 
691 	if ((error = aac_shutdown(dev)))
692 		return(error);
693 
694 	EVENTHANDLER_DEREGISTER(shutdown_final, sc->eh);
695 
696 	aac_free(sc);
697 
698 	mtx_destroy(&sc->aac_aifq_lock);
699 	mtx_destroy(&sc->aac_io_lock);
700 	mtx_destroy(&sc->aac_container_lock);
701 
702 	return(0);
703 }
704 
705 /*
706  * Bring the controller down to a dormant state and detach all child devices.
707  *
708  * This function is called before detach or system shutdown.
709  *
710  * Note that we can assume that the bioq on the controller is empty, as we won't
711  * allow shutdown if any device is open.
712  */
713 int
714 aac_shutdown(device_t dev)
715 {
716 	struct aac_softc *sc;
717 	struct aac_fib *fib;
718 	struct aac_close_command *cc;
719 
720 	sc = device_get_softc(dev);
721 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
722 
723 	sc->aac_state |= AAC_STATE_SUSPEND;
724 
725 	/*
726 	 * Send a Container shutdown followed by a HostShutdown FIB to the
727 	 * controller to convince it that we don't want to talk to it anymore.
728 	 * We've been closed and all I/O completed already
729 	 */
730 	device_printf(sc->aac_dev, "shutting down controller...");
731 
732 	mtx_lock(&sc->aac_io_lock);
733 	aac_alloc_sync_fib(sc, &fib);
734 	cc = (struct aac_close_command *)&fib->data[0];
735 
736 	bzero(cc, sizeof(struct aac_close_command));
737 	cc->Command = VM_CloseAll;
738 	cc->ContainerId = 0xffffffff;
739 	if (aac_sync_fib(sc, ContainerCommand, 0, fib,
740 	    sizeof(struct aac_close_command)))
741 		printf("FAILED.\n");
742 	else
743 		printf("done\n");
744 #if 0
745 	else {
746 		fib->data[0] = 0;
747 		/*
748 		 * XXX Issuing this command to the controller makes it shut down
749 		 * but also keeps it from coming back up without a reset of the
750 		 * PCI bus.  This is not desirable if you are just unloading the
751 		 * driver module with the intent to reload it later.
752 		 */
753 		if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN,
754 		    fib, 1)) {
755 			printf("FAILED.\n");
756 		} else {
757 			printf("done.\n");
758 		}
759 	}
760 #endif
761 
762 	AAC_MASK_INTERRUPTS(sc);
763 	aac_release_sync_fib(sc);
764 	mtx_unlock(&sc->aac_io_lock);
765 
766 	return(0);
767 }
768 
769 /*
770  * Bring the controller to a quiescent state, ready for system suspend.
771  */
772 int
773 aac_suspend(device_t dev)
774 {
775 	struct aac_softc *sc;
776 
777 	sc = device_get_softc(dev);
778 
779 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
780 	sc->aac_state |= AAC_STATE_SUSPEND;
781 
782 	AAC_MASK_INTERRUPTS(sc);
783 	return(0);
784 }
785 
786 /*
787  * Bring the controller back to a state ready for operation.
788  */
789 int
790 aac_resume(device_t dev)
791 {
792 	struct aac_softc *sc;
793 
794 	sc = device_get_softc(dev);
795 
796 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
797 	sc->aac_state &= ~AAC_STATE_SUSPEND;
798 	AAC_UNMASK_INTERRUPTS(sc);
799 	return(0);
800 }
801 
802 /*
803  * Interrupt handler for NEW_COMM interface.
804  */
805 void
806 aac_new_intr(void *arg)
807 {
808 	struct aac_softc *sc;
809 	u_int32_t index, fast;
810 	struct aac_command *cm;
811 	struct aac_fib *fib;
812 	int i;
813 
814 	sc = (struct aac_softc *)arg;
815 
816 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
817 	mtx_lock(&sc->aac_io_lock);
818 	while (1) {
819 		index = AAC_GET_OUTB_QUEUE(sc);
820 		if (index == 0xffffffff)
821 			index = AAC_GET_OUTB_QUEUE(sc);
822 		if (index == 0xffffffff)
823 			break;
824 		if (index & 2) {
825 			if (index == 0xfffffffe) {
826 				/* XXX This means that the controller wants
827 				 * more work.  Ignore it for now.
828 				 */
829 				continue;
830 			}
831 			/* AIF */
832 			fib = (struct aac_fib *)malloc(sizeof *fib, M_AACBUF,
833 				   M_NOWAIT | M_ZERO);
834 			if (fib == NULL) {
835 				/* If we're really this short on memory,
836 				 * hopefully breaking out of the handler will
837 				 * allow something to get freed.  This
838 				 * actually sucks a whole lot.
839 				 */
840 				break;
841 			}
842 			index &= ~2;
843 			for (i = 0; i < sizeof(struct aac_fib)/4; ++i)
844 				((u_int32_t *)fib)[i] = AAC_MEM1_GETREG4(sc, index + i*4);
845 			aac_handle_aif(sc, fib);
846 			free(fib, M_AACBUF);
847 
848 			/*
849 			 * AIF memory is owned by the adapter, so let it
850 			 * know that we are done with it.
851 			 */
852 			AAC_SET_OUTB_QUEUE(sc, index);
853 			AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY);
854 		} else {
855 			fast = index & 1;
856 			cm = sc->aac_commands + (index >> 2);
857 			fib = cm->cm_fib;
858 			if (fast) {
859 				fib->Header.XferState |= AAC_FIBSTATE_DONEADAP;
860 				*((u_int32_t *)(fib->data)) = AAC_ERROR_NORMAL;
861 			}
862 			aac_remove_busy(cm);
863  			aac_unmap_command(cm);
864 			cm->cm_flags |= AAC_CMD_COMPLETED;
865 
866 			/* is there a completion handler? */
867 			if (cm->cm_complete != NULL) {
868 				cm->cm_complete(cm);
869 			} else {
870 				/* assume that someone is sleeping on this
871 				 * command
872 				 */
873 				wakeup(cm);
874 			}
875 			sc->flags &= ~AAC_QUEUE_FRZN;
876 		}
877 	}
878 	/* see if we can start some more I/O */
879 	if ((sc->flags & AAC_QUEUE_FRZN) == 0)
880 		aac_startio(sc);
881 
882 	mtx_unlock(&sc->aac_io_lock);
883 }
884 
885 /*
886  * Interrupt filter for !NEW_COMM interface.
887  */
888 int
889 aac_filter(void *arg)
890 {
891 	struct aac_softc *sc;
892 	u_int16_t reason;
893 
894 	sc = (struct aac_softc *)arg;
895 
896 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
897 	/*
898 	 * Read the status register directly.  This is faster than taking the
899 	 * driver lock and reading the queues directly.  It also saves having
900 	 * to turn parts of the driver lock into a spin mutex, which would be
901 	 * ugly.
902 	 */
903 	reason = AAC_GET_ISTATUS(sc);
904 	AAC_CLEAR_ISTATUS(sc, reason);
905 
906 	/* handle completion processing */
907 	if (reason & AAC_DB_RESPONSE_READY)
908 		taskqueue_enqueue_fast(taskqueue_fast, &sc->aac_task_complete);
909 
910 	/* controller wants to talk to us */
911 	if (reason & (AAC_DB_PRINTF | AAC_DB_COMMAND_READY)) {
912 		/*
913 		 * XXX Make sure that we don't get fooled by strange messages
914 		 * that start with a NULL.
915 		 */
916 		if ((reason & AAC_DB_PRINTF) &&
917 			(sc->aac_common->ac_printf[0] == 0))
918 			sc->aac_common->ac_printf[0] = 32;
919 
920 		/*
921 		 * This might miss doing the actual wakeup.  However, the
922 		 * msleep that this is waking up has a timeout, so it will
923 		 * wake up eventually.  AIFs and printfs are low enough
924 		 * priority that they can handle hanging out for a few seconds
925 		 * if needed.
926 		 */
927 		wakeup(sc->aifthread);
928 	}
929 	return (FILTER_HANDLED);
930 }
931 
932 /*
933  * Command Processing
934  */
935 
936 /*
937  * Start as much queued I/O as possible on the controller
938  */
939 void
940 aac_startio(struct aac_softc *sc)
941 {
942 	struct aac_command *cm;
943 	int error;
944 
945 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
946 
947 	for (;;) {
948 		/*
949 		 * This flag might be set if the card is out of resources.
950 		 * Checking it here prevents an infinite loop of deferrals.
951 		 */
952 		if (sc->flags & AAC_QUEUE_FRZN)
953 			break;
954 
955 		/*
956 		 * Try to get a command that's been put off for lack of
957 		 * resources
958 		 */
959 		cm = aac_dequeue_ready(sc);
960 
961 		/*
962 		 * Try to build a command off the bio queue (ignore error
963 		 * return)
964 		 */
965 		if (cm == NULL)
966 			aac_bio_command(sc, &cm);
967 
968 		/* nothing to do? */
969 		if (cm == NULL)
970 			break;
971 
972 		/* don't map more than once */
973 		if (cm->cm_flags & AAC_CMD_MAPPED)
974 			panic("aac: command %p already mapped", cm);
975 
976 		/*
977 		 * Set up the command to go to the controller.  If there are no
978 		 * data buffers associated with the command then it can bypass
979 		 * busdma.
980 		 */
981 		if (cm->cm_datalen != 0) {
982 			error = bus_dmamap_load(sc->aac_buffer_dmat,
983 						cm->cm_datamap, cm->cm_data,
984 						cm->cm_datalen,
985 						aac_map_command_sg, cm, 0);
986 			if (error == EINPROGRESS) {
987 				fwprintf(sc, HBA_FLAGS_DBG_COMM_B, "freezing queue\n");
988 				sc->flags |= AAC_QUEUE_FRZN;
989 				error = 0;
990 			} else if (error != 0)
991 				panic("aac_startio: unexpected error %d from "
992 				      "busdma", error);
993 		} else
994 			aac_map_command_sg(cm, NULL, 0, 0);
995 	}
996 }
997 
998 /*
999  * Handle notification of one or more FIBs coming from the controller.
1000  */
1001 static void
1002 aac_command_thread(struct aac_softc *sc)
1003 {
1004 	struct aac_fib *fib;
1005 	u_int32_t fib_size;
1006 	int size, retval;
1007 
1008 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1009 
1010 	mtx_lock(&sc->aac_io_lock);
1011 	sc->aifflags = AAC_AIFFLAGS_RUNNING;
1012 
1013 	while ((sc->aifflags & AAC_AIFFLAGS_EXIT) == 0) {
1014 
1015 		retval = 0;
1016 		if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0)
1017 			retval = msleep(sc->aifthread, &sc->aac_io_lock, PRIBIO,
1018 					"aifthd", AAC_PERIODIC_INTERVAL * hz);
1019 
1020 		/*
1021 		 * First see if any FIBs need to be allocated.  This needs
1022 		 * to be called without the driver lock because contigmalloc
1023 		 * will grab Giant, and would result in an LOR.
1024 		 */
1025 		if ((sc->aifflags & AAC_AIFFLAGS_ALLOCFIBS) != 0) {
1026 			mtx_unlock(&sc->aac_io_lock);
1027 			aac_alloc_commands(sc);
1028 			mtx_lock(&sc->aac_io_lock);
1029 			sc->aifflags &= ~AAC_AIFFLAGS_ALLOCFIBS;
1030 			aac_startio(sc);
1031 		}
1032 
1033 		/*
1034 		 * While we're here, check to see if any commands are stuck.
1035 		 * This is pretty low-priority, so it's ok if it doesn't
1036 		 * always fire.
1037 		 */
1038 		if (retval == EWOULDBLOCK)
1039 			aac_timeout(sc);
1040 
1041 		/* Check the hardware printf message buffer */
1042 		if (sc->aac_common->ac_printf[0] != 0)
1043 			aac_print_printf(sc);
1044 
1045 		/* Also check to see if the adapter has a command for us. */
1046 		if (sc->flags & AAC_FLAGS_NEW_COMM)
1047 			continue;
1048 		for (;;) {
1049 			if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE,
1050 					   &fib_size, &fib))
1051 				break;
1052 
1053 			AAC_PRINT_FIB(sc, fib);
1054 
1055 			switch (fib->Header.Command) {
1056 			case AifRequest:
1057 				aac_handle_aif(sc, fib);
1058 				break;
1059 			default:
1060 				device_printf(sc->aac_dev, "unknown command "
1061 					      "from controller\n");
1062 				break;
1063 			}
1064 
1065 			if ((fib->Header.XferState == 0) ||
1066 			    (fib->Header.StructType != AAC_FIBTYPE_TFIB)) {
1067 				break;
1068 			}
1069 
1070 			/* Return the AIF to the controller. */
1071 			if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) {
1072 				fib->Header.XferState |= AAC_FIBSTATE_DONEHOST;
1073 				*(AAC_FSAStatus*)fib->data = ST_OK;
1074 
1075 				/* XXX Compute the Size field? */
1076 				size = fib->Header.Size;
1077 				if (size > sizeof(struct aac_fib)) {
1078 					size = sizeof(struct aac_fib);
1079 					fib->Header.Size = size;
1080 				}
1081 				/*
1082 				 * Since we did not generate this command, it
1083 				 * cannot go through the normal
1084 				 * enqueue->startio chain.
1085 				 */
1086 				aac_enqueue_response(sc,
1087 						 AAC_ADAP_NORM_RESP_QUEUE,
1088 						 fib);
1089 			}
1090 		}
1091 	}
1092 	sc->aifflags &= ~AAC_AIFFLAGS_RUNNING;
1093 	mtx_unlock(&sc->aac_io_lock);
1094 	wakeup(sc->aac_dev);
1095 
1096 	kproc_exit(0);
1097 }
1098 
1099 /*
1100  * Process completed commands.
1101  */
1102 static void
1103 aac_complete(void *context, int pending)
1104 {
1105 	struct aac_softc *sc;
1106 	struct aac_command *cm;
1107 	struct aac_fib *fib;
1108 	u_int32_t fib_size;
1109 
1110 	sc = (struct aac_softc *)context;
1111 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1112 
1113 	mtx_lock(&sc->aac_io_lock);
1114 
1115 	/* pull completed commands off the queue */
1116 	for (;;) {
1117 		/* look for completed FIBs on our queue */
1118 		if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size,
1119 							&fib))
1120 			break;	/* nothing to do */
1121 
1122 		/* get the command, unmap and hand off for processing */
1123 		cm = sc->aac_commands + fib->Header.SenderData;
1124 		if (cm == NULL) {
1125 			AAC_PRINT_FIB(sc, fib);
1126 			break;
1127 		}
1128 		if ((cm->cm_flags & AAC_CMD_TIMEDOUT) != 0)
1129 			device_printf(sc->aac_dev,
1130 			    "COMMAND %p COMPLETED AFTER %d SECONDS\n",
1131 			    cm, (int)(time_uptime-cm->cm_timestamp));
1132 
1133 		aac_remove_busy(cm);
1134 
1135  		aac_unmap_command(cm);
1136 		cm->cm_flags |= AAC_CMD_COMPLETED;
1137 
1138 		/* is there a completion handler? */
1139 		if (cm->cm_complete != NULL) {
1140 			cm->cm_complete(cm);
1141 		} else {
1142 			/* assume that someone is sleeping on this command */
1143 			wakeup(cm);
1144 		}
1145 	}
1146 
1147 	/* see if we can start some more I/O */
1148 	sc->flags &= ~AAC_QUEUE_FRZN;
1149 	aac_startio(sc);
1150 
1151 	mtx_unlock(&sc->aac_io_lock);
1152 }
1153 
1154 /*
1155  * Handle a bio submitted from a disk device.
1156  */
1157 void
1158 aac_submit_bio(struct bio *bp)
1159 {
1160 	struct aac_disk *ad;
1161 	struct aac_softc *sc;
1162 
1163 	ad = (struct aac_disk *)bp->bio_disk->d_drv1;
1164 	sc = ad->ad_controller;
1165 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1166 
1167 	/* queue the BIO and try to get some work done */
1168 	aac_enqueue_bio(sc, bp);
1169 	aac_startio(sc);
1170 }
1171 
1172 /*
1173  * Get a bio and build a command to go with it.
1174  */
1175 static int
1176 aac_bio_command(struct aac_softc *sc, struct aac_command **cmp)
1177 {
1178 	struct aac_command *cm;
1179 	struct aac_fib *fib;
1180 	struct aac_disk *ad;
1181 	struct bio *bp;
1182 
1183 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1184 
1185 	/* get the resources we will need */
1186 	cm = NULL;
1187 	bp = NULL;
1188 	if (aac_alloc_command(sc, &cm))	/* get a command */
1189 		goto fail;
1190 	if ((bp = aac_dequeue_bio(sc)) == NULL)
1191 		goto fail;
1192 
1193 	/* fill out the command */
1194 	cm->cm_data = (void *)bp->bio_data;
1195 	cm->cm_datalen = bp->bio_bcount;
1196 	cm->cm_complete = aac_bio_complete;
1197 	cm->cm_private = bp;
1198 	cm->cm_timestamp = time_uptime;
1199 
1200 	/* build the FIB */
1201 	fib = cm->cm_fib;
1202 	fib->Header.Size = sizeof(struct aac_fib_header);
1203 	fib->Header.XferState =
1204 		AAC_FIBSTATE_HOSTOWNED   |
1205 		AAC_FIBSTATE_INITIALISED |
1206 		AAC_FIBSTATE_EMPTY	 |
1207 		AAC_FIBSTATE_FROMHOST	 |
1208 		AAC_FIBSTATE_REXPECTED   |
1209 		AAC_FIBSTATE_NORM	 |
1210 		AAC_FIBSTATE_ASYNC	 |
1211 		AAC_FIBSTATE_FAST_RESPONSE;
1212 
1213 	/* build the read/write request */
1214 	ad = (struct aac_disk *)bp->bio_disk->d_drv1;
1215 
1216 	if (sc->flags & AAC_FLAGS_RAW_IO) {
1217 		struct aac_raw_io *raw;
1218 		raw = (struct aac_raw_io *)&fib->data[0];
1219 		fib->Header.Command = RawIo;
1220 		raw->BlockNumber = (u_int64_t)bp->bio_pblkno;
1221 		raw->ByteCount = bp->bio_bcount;
1222 		raw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1223 		raw->BpTotal = 0;
1224 		raw->BpComplete = 0;
1225 		fib->Header.Size += sizeof(struct aac_raw_io);
1226 		cm->cm_sgtable = (struct aac_sg_table *)&raw->SgMapRaw;
1227 		if (bp->bio_cmd == BIO_READ) {
1228 			raw->Flags = 1;
1229 			cm->cm_flags |= AAC_CMD_DATAIN;
1230 		} else {
1231 			raw->Flags = 0;
1232 			cm->cm_flags |= AAC_CMD_DATAOUT;
1233 		}
1234 	} else if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1235 		fib->Header.Command = ContainerCommand;
1236 		if (bp->bio_cmd == BIO_READ) {
1237 			struct aac_blockread *br;
1238 			br = (struct aac_blockread *)&fib->data[0];
1239 			br->Command = VM_CtBlockRead;
1240 			br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1241 			br->BlockNumber = bp->bio_pblkno;
1242 			br->ByteCount = bp->bio_bcount;
1243 			fib->Header.Size += sizeof(struct aac_blockread);
1244 			cm->cm_sgtable = &br->SgMap;
1245 			cm->cm_flags |= AAC_CMD_DATAIN;
1246 		} else {
1247 			struct aac_blockwrite *bw;
1248 			bw = (struct aac_blockwrite *)&fib->data[0];
1249 			bw->Command = VM_CtBlockWrite;
1250 			bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1251 			bw->BlockNumber = bp->bio_pblkno;
1252 			bw->ByteCount = bp->bio_bcount;
1253 			bw->Stable = CUNSTABLE;
1254 			fib->Header.Size += sizeof(struct aac_blockwrite);
1255 			cm->cm_flags |= AAC_CMD_DATAOUT;
1256 			cm->cm_sgtable = &bw->SgMap;
1257 		}
1258 	} else {
1259 		fib->Header.Command = ContainerCommand64;
1260 		if (bp->bio_cmd == BIO_READ) {
1261 			struct aac_blockread64 *br;
1262 			br = (struct aac_blockread64 *)&fib->data[0];
1263 			br->Command = VM_CtHostRead64;
1264 			br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1265 			br->SectorCount = bp->bio_bcount / AAC_BLOCK_SIZE;
1266 			br->BlockNumber = bp->bio_pblkno;
1267 			br->Pad = 0;
1268 			br->Flags = 0;
1269 			fib->Header.Size += sizeof(struct aac_blockread64);
1270 			cm->cm_flags |= AAC_CMD_DATAIN;
1271 			cm->cm_sgtable = (struct aac_sg_table *)&br->SgMap64;
1272 		} else {
1273 			struct aac_blockwrite64 *bw;
1274 			bw = (struct aac_blockwrite64 *)&fib->data[0];
1275 			bw->Command = VM_CtHostWrite64;
1276 			bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
1277 			bw->SectorCount = bp->bio_bcount / AAC_BLOCK_SIZE;
1278 			bw->BlockNumber = bp->bio_pblkno;
1279 			bw->Pad = 0;
1280 			bw->Flags = 0;
1281 			fib->Header.Size += sizeof(struct aac_blockwrite64);
1282 			cm->cm_flags |= AAC_CMD_DATAOUT;
1283 			cm->cm_sgtable = (struct aac_sg_table *)&bw->SgMap64;
1284 		}
1285 	}
1286 
1287 	*cmp = cm;
1288 	return(0);
1289 
1290 fail:
1291 	if (bp != NULL)
1292 		aac_enqueue_bio(sc, bp);
1293 	if (cm != NULL)
1294 		aac_release_command(cm);
1295 	return(ENOMEM);
1296 }
1297 
1298 /*
1299  * Handle a bio-instigated command that has been completed.
1300  */
1301 static void
1302 aac_bio_complete(struct aac_command *cm)
1303 {
1304 	struct aac_blockread_response *brr;
1305 	struct aac_blockwrite_response *bwr;
1306 	struct bio *bp;
1307 	AAC_FSAStatus status;
1308 
1309 	/* fetch relevant status and then release the command */
1310 	bp = (struct bio *)cm->cm_private;
1311 	if (bp->bio_cmd == BIO_READ) {
1312 		brr = (struct aac_blockread_response *)&cm->cm_fib->data[0];
1313 		status = brr->Status;
1314 	} else {
1315 		bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0];
1316 		status = bwr->Status;
1317 	}
1318 	aac_release_command(cm);
1319 
1320 	/* fix up the bio based on status */
1321 	if (status == ST_OK) {
1322 		bp->bio_resid = 0;
1323 	} else {
1324 		bp->bio_error = EIO;
1325 		bp->bio_flags |= BIO_ERROR;
1326 		/* pass an error string out to the disk layer */
1327 		bp->bio_driver1 = aac_describe_code(aac_command_status_table,
1328 						    status);
1329 	}
1330 	aac_biodone(bp);
1331 }
1332 
1333 /*
1334  * Submit a command to the controller, return when it completes.
1335  * XXX This is very dangerous!  If the card has gone out to lunch, we could
1336  *     be stuck here forever.  At the same time, signals are not caught
1337  *     because there is a risk that a signal could wakeup the sleep before
1338  *     the card has a chance to complete the command.  Since there is no way
1339  *     to cancel a command that is in progress, we can't protect against the
1340  *     card completing a command late and spamming the command and data
1341  *     memory.  So, we are held hostage until the command completes.
1342  */
1343 static int
1344 aac_wait_command(struct aac_command *cm)
1345 {
1346 	struct aac_softc *sc;
1347 	int error;
1348 
1349 	sc = cm->cm_sc;
1350 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1351 
1352 	/* Put the command on the ready queue and get things going */
1353 	aac_enqueue_ready(cm);
1354 	aac_startio(sc);
1355 	error = msleep(cm, &sc->aac_io_lock, PRIBIO, "aacwait", 0);
1356 	return(error);
1357 }
1358 
1359 /*
1360  *Command Buffer Management
1361  */
1362 
1363 /*
1364  * Allocate a command.
1365  */
1366 int
1367 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
1368 {
1369 	struct aac_command *cm;
1370 
1371 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1372 
1373 	if ((cm = aac_dequeue_free(sc)) == NULL) {
1374 		if (sc->total_fibs < sc->aac_max_fibs) {
1375 			sc->aifflags |= AAC_AIFFLAGS_ALLOCFIBS;
1376 			wakeup(sc->aifthread);
1377 		}
1378 		return (EBUSY);
1379 	}
1380 
1381 	*cmp = cm;
1382 	return(0);
1383 }
1384 
1385 /*
1386  * Release a command back to the freelist.
1387  */
1388 void
1389 aac_release_command(struct aac_command *cm)
1390 {
1391 	struct aac_event *event;
1392 	struct aac_softc *sc;
1393 
1394 	sc = cm->cm_sc;
1395 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1396 
1397 	/* (re)initialize the command/FIB */
1398 	cm->cm_sgtable = NULL;
1399 	cm->cm_flags = 0;
1400 	cm->cm_complete = NULL;
1401 	cm->cm_private = NULL;
1402 	cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1403 	cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
1404 	cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
1405 	cm->cm_fib->Header.Flags = 0;
1406 	cm->cm_fib->Header.SenderSize = cm->cm_sc->aac_max_fib_size;
1407 
1408 	/*
1409 	 * These are duplicated in aac_start to cover the case where an
1410 	 * intermediate stage may have destroyed them.  They're left
1411 	 * initialized here for debugging purposes only.
1412 	 */
1413 	cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1414 	cm->cm_fib->Header.SenderData = 0;
1415 
1416 	aac_enqueue_free(cm);
1417 
1418 	if ((event = TAILQ_FIRST(&sc->aac_ev_cmfree)) != NULL) {
1419 		TAILQ_REMOVE(&sc->aac_ev_cmfree, event, ev_links);
1420 		event->ev_callback(sc, event, event->ev_arg);
1421 	}
1422 }
1423 
1424 /*
1425  * Map helper for command/FIB allocation.
1426  */
1427 static void
1428 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1429 {
1430 	uint64_t	*fibphys;
1431 
1432 	fibphys = (uint64_t *)arg;
1433 
1434 	*fibphys = segs[0].ds_addr;
1435 }
1436 
1437 /*
1438  * Allocate and initialize commands/FIBs for this adapter.
1439  */
1440 static int
1441 aac_alloc_commands(struct aac_softc *sc)
1442 {
1443 	struct aac_command *cm;
1444 	struct aac_fibmap *fm;
1445 	uint64_t fibphys;
1446 	int i, error;
1447 
1448 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1449 
1450 	if (sc->total_fibs + sc->aac_max_fibs_alloc > sc->aac_max_fibs)
1451 		return (ENOMEM);
1452 
1453 	fm = malloc(sizeof(struct aac_fibmap), M_AACBUF, M_NOWAIT|M_ZERO);
1454 	if (fm == NULL)
1455 		return (ENOMEM);
1456 
1457 	/* allocate the FIBs in DMAable memory and load them */
1458 	if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&fm->aac_fibs,
1459 			     BUS_DMA_NOWAIT, &fm->aac_fibmap)) {
1460 		device_printf(sc->aac_dev,
1461 			      "Not enough contiguous memory available.\n");
1462 		free(fm, M_AACBUF);
1463 		return (ENOMEM);
1464 	}
1465 
1466 	/* Ignore errors since this doesn't bounce */
1467 	(void)bus_dmamap_load(sc->aac_fib_dmat, fm->aac_fibmap, fm->aac_fibs,
1468 			      sc->aac_max_fibs_alloc * sc->aac_max_fib_size,
1469 			      aac_map_command_helper, &fibphys, 0);
1470 
1471 	/* initialize constant fields in the command structure */
1472 	bzero(fm->aac_fibs, sc->aac_max_fibs_alloc * sc->aac_max_fib_size);
1473 	for (i = 0; i < sc->aac_max_fibs_alloc; i++) {
1474 		cm = sc->aac_commands + sc->total_fibs;
1475 		fm->aac_commands = cm;
1476 		cm->cm_sc = sc;
1477 		cm->cm_fib = (struct aac_fib *)
1478 			((u_int8_t *)fm->aac_fibs + i*sc->aac_max_fib_size);
1479 		cm->cm_fibphys = fibphys + i*sc->aac_max_fib_size;
1480 		cm->cm_index = sc->total_fibs;
1481 
1482 		if ((error = bus_dmamap_create(sc->aac_buffer_dmat, 0,
1483 					       &cm->cm_datamap)) != 0)
1484 			break;
1485 		mtx_lock(&sc->aac_io_lock);
1486 		aac_release_command(cm);
1487 		sc->total_fibs++;
1488 		mtx_unlock(&sc->aac_io_lock);
1489 	}
1490 
1491 	if (i > 0) {
1492 		mtx_lock(&sc->aac_io_lock);
1493 		TAILQ_INSERT_TAIL(&sc->aac_fibmap_tqh, fm, fm_link);
1494 		fwprintf(sc, HBA_FLAGS_DBG_COMM_B, "total_fibs= %d\n", sc->total_fibs);
1495 		mtx_unlock(&sc->aac_io_lock);
1496 		return (0);
1497 	}
1498 
1499 	bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1500 	bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1501 	free(fm, M_AACBUF);
1502 	return (ENOMEM);
1503 }
1504 
1505 /*
1506  * Free FIBs owned by this adapter.
1507  */
1508 static void
1509 aac_free_commands(struct aac_softc *sc)
1510 {
1511 	struct aac_fibmap *fm;
1512 	struct aac_command *cm;
1513 	int i;
1514 
1515 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1516 
1517 	while ((fm = TAILQ_FIRST(&sc->aac_fibmap_tqh)) != NULL) {
1518 
1519 		TAILQ_REMOVE(&sc->aac_fibmap_tqh, fm, fm_link);
1520 		/*
1521 		 * We check against total_fibs to handle partially
1522 		 * allocated blocks.
1523 		 */
1524 		for (i = 0; i < sc->aac_max_fibs_alloc && sc->total_fibs--; i++) {
1525 			cm = fm->aac_commands + i;
1526 			bus_dmamap_destroy(sc->aac_buffer_dmat, cm->cm_datamap);
1527 		}
1528 		bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1529 		bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1530 		free(fm, M_AACBUF);
1531 	}
1532 }
1533 
1534 /*
1535  * Command-mapping helper function - populate this command's s/g table.
1536  */
1537 static void
1538 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1539 {
1540 	struct aac_softc *sc;
1541 	struct aac_command *cm;
1542 	struct aac_fib *fib;
1543 	int i;
1544 
1545 	cm = (struct aac_command *)arg;
1546 	sc = cm->cm_sc;
1547 	fib = cm->cm_fib;
1548 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1549 
1550 	/* copy into the FIB */
1551 	if (cm->cm_sgtable != NULL) {
1552 		if (fib->Header.Command == RawIo) {
1553 			struct aac_sg_tableraw *sg;
1554 			sg = (struct aac_sg_tableraw *)cm->cm_sgtable;
1555 			sg->SgCount = nseg;
1556 			for (i = 0; i < nseg; i++) {
1557 				sg->SgEntryRaw[i].SgAddress = segs[i].ds_addr;
1558 				sg->SgEntryRaw[i].SgByteCount = segs[i].ds_len;
1559 				sg->SgEntryRaw[i].Next = 0;
1560 				sg->SgEntryRaw[i].Prev = 0;
1561 				sg->SgEntryRaw[i].Flags = 0;
1562 			}
1563 			/* update the FIB size for the s/g count */
1564 			fib->Header.Size += nseg*sizeof(struct aac_sg_entryraw);
1565 		} else if ((cm->cm_sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1566 			struct aac_sg_table *sg;
1567 			sg = cm->cm_sgtable;
1568 			sg->SgCount = nseg;
1569 			for (i = 0; i < nseg; i++) {
1570 				sg->SgEntry[i].SgAddress = segs[i].ds_addr;
1571 				sg->SgEntry[i].SgByteCount = segs[i].ds_len;
1572 			}
1573 			/* update the FIB size for the s/g count */
1574 			fib->Header.Size += nseg*sizeof(struct aac_sg_entry);
1575 		} else {
1576 			struct aac_sg_table64 *sg;
1577 			sg = (struct aac_sg_table64 *)cm->cm_sgtable;
1578 			sg->SgCount = nseg;
1579 			for (i = 0; i < nseg; i++) {
1580 				sg->SgEntry64[i].SgAddress = segs[i].ds_addr;
1581 				sg->SgEntry64[i].SgByteCount = segs[i].ds_len;
1582 			}
1583 			/* update the FIB size for the s/g count */
1584 			fib->Header.Size += nseg*sizeof(struct aac_sg_entry64);
1585 		}
1586 	}
1587 
1588 	/* Fix up the address values in the FIB.  Use the command array index
1589 	 * instead of a pointer since these fields are only 32 bits.  Shift
1590 	 * the SenderFibAddress over to make room for the fast response bit
1591 	 * and for the AIF bit
1592 	 */
1593 	cm->cm_fib->Header.SenderFibAddress = (cm->cm_index << 2);
1594 	cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1595 
1596 	/* save a pointer to the command for speedy reverse-lookup */
1597 	cm->cm_fib->Header.SenderData = cm->cm_index;
1598 
1599 	if (cm->cm_flags & AAC_CMD_DATAIN)
1600 		bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1601 				BUS_DMASYNC_PREREAD);
1602 	if (cm->cm_flags & AAC_CMD_DATAOUT)
1603 		bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1604 				BUS_DMASYNC_PREWRITE);
1605 	cm->cm_flags |= AAC_CMD_MAPPED;
1606 
1607 	if (sc->flags & AAC_FLAGS_NEW_COMM) {
1608 		int count = 10000000L;
1609 		while (AAC_SEND_COMMAND(sc, cm) != 0) {
1610 			if (--count == 0) {
1611 				aac_unmap_command(cm);
1612 				sc->flags |= AAC_QUEUE_FRZN;
1613 				aac_requeue_ready(cm);
1614 			}
1615 			DELAY(5);			/* wait 5 usec. */
1616 		}
1617 	} else {
1618 		/* Put the FIB on the outbound queue */
1619 		if (aac_enqueue_fib(sc, cm->cm_queue, cm) == EBUSY) {
1620 			aac_unmap_command(cm);
1621 			sc->flags |= AAC_QUEUE_FRZN;
1622 			aac_requeue_ready(cm);
1623 		}
1624 	}
1625 
1626 	return;
1627 }
1628 
1629 /*
1630  * Unmap a command from controller-visible space.
1631  */
1632 static void
1633 aac_unmap_command(struct aac_command *cm)
1634 {
1635 	struct aac_softc *sc;
1636 
1637 	sc = cm->cm_sc;
1638 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1639 
1640 	if (!(cm->cm_flags & AAC_CMD_MAPPED))
1641 		return;
1642 
1643 	if (cm->cm_datalen != 0) {
1644 		if (cm->cm_flags & AAC_CMD_DATAIN)
1645 			bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1646 					BUS_DMASYNC_POSTREAD);
1647 		if (cm->cm_flags & AAC_CMD_DATAOUT)
1648 			bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1649 					BUS_DMASYNC_POSTWRITE);
1650 
1651 		bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap);
1652 	}
1653 	cm->cm_flags &= ~AAC_CMD_MAPPED;
1654 }
1655 
1656 /*
1657  * Hardware Interface
1658  */
1659 
1660 /*
1661  * Initialize the adapter.
1662  */
1663 static void
1664 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1665 {
1666 	struct aac_softc *sc;
1667 
1668 	sc = (struct aac_softc *)arg;
1669 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1670 
1671 	sc->aac_common_busaddr = segs[0].ds_addr;
1672 }
1673 
1674 static int
1675 aac_check_firmware(struct aac_softc *sc)
1676 {
1677 	u_int32_t code, major, minor, options = 0, atu_size = 0;
1678 	int status;
1679 	time_t then;
1680 
1681 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1682 	/*
1683 	 * Wait for the adapter to come ready.
1684 	 */
1685 	then = time_uptime;
1686 	do {
1687 		code = AAC_GET_FWSTATUS(sc);
1688 		if (code & AAC_SELF_TEST_FAILED) {
1689 			device_printf(sc->aac_dev, "FATAL: selftest failed\n");
1690 			return(ENXIO);
1691 		}
1692 		if (code & AAC_KERNEL_PANIC) {
1693 			device_printf(sc->aac_dev,
1694 				      "FATAL: controller kernel panic");
1695 			return(ENXIO);
1696 		}
1697 		if (time_uptime > (then + AAC_BOOT_TIMEOUT)) {
1698 			device_printf(sc->aac_dev,
1699 				      "FATAL: controller not coming ready, "
1700 					   "status %x\n", code);
1701 			return(ENXIO);
1702 		}
1703 	} while (!(code & AAC_UP_AND_RUNNING));
1704 
1705 	/*
1706 	 * Retrieve the firmware version numbers.  Dell PERC2/QC cards with
1707 	 * firmware version 1.x are not compatible with this driver.
1708 	 */
1709 	if (sc->flags & AAC_FLAGS_PERC2QC) {
1710 		if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0,
1711 				     NULL)) {
1712 			device_printf(sc->aac_dev,
1713 				      "Error reading firmware version\n");
1714 			return (EIO);
1715 		}
1716 
1717 		/* These numbers are stored as ASCII! */
1718 		major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30;
1719 		minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30;
1720 		if (major == 1) {
1721 			device_printf(sc->aac_dev,
1722 			    "Firmware version %d.%d is not supported.\n",
1723 			    major, minor);
1724 			return (EINVAL);
1725 		}
1726 	}
1727 
1728 	/*
1729 	 * Retrieve the capabilities/supported options word so we know what
1730 	 * work-arounds to enable.  Some firmware revs don't support this
1731 	 * command.
1732 	 */
1733 	if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, &status)) {
1734 		if (status != AAC_SRB_STS_INVALID_REQUEST) {
1735 			device_printf(sc->aac_dev,
1736 			     "RequestAdapterInfo failed\n");
1737 			return (EIO);
1738 		}
1739 	} else {
1740 		options = AAC_GET_MAILBOX(sc, 1);
1741 		atu_size = AAC_GET_MAILBOX(sc, 2);
1742 		sc->supported_options = options;
1743 
1744 		if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 &&
1745 		    (sc->flags & AAC_FLAGS_NO4GB) == 0)
1746 			sc->flags |= AAC_FLAGS_4GB_WINDOW;
1747 		if (options & AAC_SUPPORTED_NONDASD)
1748 			sc->flags |= AAC_FLAGS_ENABLE_CAM;
1749 		if ((options & AAC_SUPPORTED_SGMAP_HOST64) != 0
1750 		     && (sizeof(bus_addr_t) > 4)) {
1751 			device_printf(sc->aac_dev,
1752 			    "Enabling 64-bit address support\n");
1753 			sc->flags |= AAC_FLAGS_SG_64BIT;
1754 		}
1755 		if ((options & AAC_SUPPORTED_NEW_COMM)
1756 		 && sc->aac_if.aif_send_command)
1757 			sc->flags |= AAC_FLAGS_NEW_COMM;
1758 		if (options & AAC_SUPPORTED_64BIT_ARRAYSIZE)
1759 			sc->flags |= AAC_FLAGS_ARRAY_64BIT;
1760 	}
1761 
1762 	/* Check for broken hardware that does a lower number of commands */
1763 	sc->aac_max_fibs = (sc->flags & AAC_FLAGS_256FIBS ? 256:512);
1764 
1765 	/* Remap mem. resource, if required */
1766 	if ((sc->flags & AAC_FLAGS_NEW_COMM) &&
1767 		atu_size > rman_get_size(sc->aac_regs_res1)) {
1768 		bus_release_resource(
1769 			sc->aac_dev, SYS_RES_MEMORY,
1770 			sc->aac_regs_rid1, sc->aac_regs_res1);
1771 		sc->aac_regs_res1 = bus_alloc_resource(
1772 			sc->aac_dev, SYS_RES_MEMORY, &sc->aac_regs_rid1,
1773 			0ul, ~0ul, atu_size, RF_ACTIVE);
1774 		if (sc->aac_regs_res1 == NULL) {
1775 			sc->aac_regs_res1 = bus_alloc_resource_any(
1776 				sc->aac_dev, SYS_RES_MEMORY,
1777 				&sc->aac_regs_rid1, RF_ACTIVE);
1778 			if (sc->aac_regs_res1 == NULL) {
1779 				device_printf(sc->aac_dev,
1780 				    "couldn't allocate register window\n");
1781 				return (ENXIO);
1782 			}
1783 			sc->flags &= ~AAC_FLAGS_NEW_COMM;
1784 		}
1785 		sc->aac_btag1 = rman_get_bustag(sc->aac_regs_res1);
1786 		sc->aac_bhandle1 = rman_get_bushandle(sc->aac_regs_res1);
1787 
1788 		if (sc->aac_hwif == AAC_HWIF_NARK) {
1789 			sc->aac_regs_res0 = sc->aac_regs_res1;
1790 			sc->aac_regs_rid0 = sc->aac_regs_rid1;
1791 			sc->aac_btag0 = sc->aac_btag1;
1792 			sc->aac_bhandle0 = sc->aac_bhandle1;
1793 		}
1794 	}
1795 
1796 	/* Read preferred settings */
1797 	sc->aac_max_fib_size = sizeof(struct aac_fib);
1798 	sc->aac_max_sectors = 128;				/* 64KB */
1799 	if (sc->flags & AAC_FLAGS_SG_64BIT)
1800 		sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1801 		 - sizeof(struct aac_blockwrite64))
1802 		 / sizeof(struct aac_sg_entry64);
1803 	else
1804 		sc->aac_sg_tablesize = (AAC_FIB_DATASIZE
1805 		 - sizeof(struct aac_blockwrite))
1806 		 / sizeof(struct aac_sg_entry);
1807 
1808 	if (!aac_sync_command(sc, AAC_MONKER_GETCOMMPREF, 0, 0, 0, 0, NULL)) {
1809 		options = AAC_GET_MAILBOX(sc, 1);
1810 		sc->aac_max_fib_size = (options & 0xFFFF);
1811 		sc->aac_max_sectors = (options >> 16) << 1;
1812 		options = AAC_GET_MAILBOX(sc, 2);
1813 		sc->aac_sg_tablesize = (options >> 16);
1814 		options = AAC_GET_MAILBOX(sc, 3);
1815 		sc->aac_max_fibs = (options & 0xFFFF);
1816 	}
1817 	if (sc->aac_max_fib_size > PAGE_SIZE)
1818 		sc->aac_max_fib_size = PAGE_SIZE;
1819 	sc->aac_max_fibs_alloc = PAGE_SIZE / sc->aac_max_fib_size;
1820 
1821 	if (sc->aac_max_fib_size > sizeof(struct aac_fib)) {
1822 		sc->flags |= AAC_FLAGS_RAW_IO;
1823 		device_printf(sc->aac_dev, "Enable Raw I/O\n");
1824 	}
1825 	if ((sc->flags & AAC_FLAGS_RAW_IO) &&
1826 	    (sc->flags & AAC_FLAGS_ARRAY_64BIT)) {
1827 		sc->flags |= AAC_FLAGS_LBA_64BIT;
1828 		device_printf(sc->aac_dev, "Enable 64-bit array\n");
1829 	}
1830 
1831 	return (0);
1832 }
1833 
1834 static int
1835 aac_init(struct aac_softc *sc)
1836 {
1837 	struct aac_adapter_init	*ip;
1838 	u_int32_t qoffset;
1839 	int error;
1840 
1841 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
1842 
1843 	/*
1844 	 * Fill in the init structure.  This tells the adapter about the
1845 	 * physical location of various important shared data structures.
1846 	 */
1847 	ip = &sc->aac_common->ac_init;
1848 	ip->InitStructRevision = AAC_INIT_STRUCT_REVISION;
1849 	if (sc->aac_max_fib_size > sizeof(struct aac_fib)) {
1850 		ip->InitStructRevision = AAC_INIT_STRUCT_REVISION_4;
1851 		sc->flags |= AAC_FLAGS_RAW_IO;
1852 	}
1853 	ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION;
1854 
1855 	ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr +
1856 					 offsetof(struct aac_common, ac_fibs);
1857 	ip->AdapterFibsVirtualAddress = 0;
1858 	ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib);
1859 	ip->AdapterFibAlign = sizeof(struct aac_fib);
1860 
1861 	ip->PrintfBufferAddress = sc->aac_common_busaddr +
1862 				  offsetof(struct aac_common, ac_printf);
1863 	ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE;
1864 
1865 	/*
1866 	 * The adapter assumes that pages are 4K in size, except on some
1867  	 * broken firmware versions that do the page->byte conversion twice,
1868 	 * therefore 'assuming' that this value is in 16MB units (2^24).
1869 	 * Round up since the granularity is so high.
1870 	 */
1871 	ip->HostPhysMemPages = ctob(physmem) / AAC_PAGE_SIZE;
1872 	if (sc->flags & AAC_FLAGS_BROKEN_MEMMAP) {
1873 		ip->HostPhysMemPages =
1874 		    (ip->HostPhysMemPages + AAC_PAGE_SIZE) / AAC_PAGE_SIZE;
1875 	}
1876 	ip->HostElapsedSeconds = time_uptime;	/* reset later if invalid */
1877 
1878 	ip->InitFlags = 0;
1879 	if (sc->flags & AAC_FLAGS_NEW_COMM) {
1880 		ip->InitFlags |= AAC_INITFLAGS_NEW_COMM_SUPPORTED;
1881 		device_printf(sc->aac_dev, "New comm. interface enabled\n");
1882 	}
1883 
1884 	ip->MaxIoCommands = sc->aac_max_fibs;
1885 	ip->MaxIoSize = sc->aac_max_sectors << 9;
1886 	ip->MaxFibSize = sc->aac_max_fib_size;
1887 
1888 	/*
1889 	 * Initialize FIB queues.  Note that it appears that the layout of the
1890 	 * indexes and the segmentation of the entries may be mandated by the
1891 	 * adapter, which is only told about the base of the queue index fields.
1892 	 *
1893 	 * The initial values of the indices are assumed to inform the adapter
1894 	 * of the sizes of the respective queues, and theoretically it could
1895 	 * work out the entire layout of the queue structures from this.  We
1896 	 * take the easy route and just lay this area out like everyone else
1897 	 * does.
1898 	 *
1899 	 * The Linux driver uses a much more complex scheme whereby several
1900 	 * header records are kept for each queue.  We use a couple of generic
1901 	 * list manipulation functions which 'know' the size of each list by
1902 	 * virtue of a table.
1903 	 */
1904 	qoffset = offsetof(struct aac_common, ac_qbuf) + AAC_QUEUE_ALIGN;
1905 	qoffset &= ~(AAC_QUEUE_ALIGN - 1);
1906 	sc->aac_queues =
1907 	    (struct aac_queue_table *)((uintptr_t)sc->aac_common + qoffset);
1908 	ip->CommHeaderAddress = sc->aac_common_busaddr + qoffset;
1909 
1910 	sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1911 		AAC_HOST_NORM_CMD_ENTRIES;
1912 	sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1913 		AAC_HOST_NORM_CMD_ENTRIES;
1914 	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1915 		AAC_HOST_HIGH_CMD_ENTRIES;
1916 	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1917 		AAC_HOST_HIGH_CMD_ENTRIES;
1918 	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1919 		AAC_ADAP_NORM_CMD_ENTRIES;
1920 	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1921 		AAC_ADAP_NORM_CMD_ENTRIES;
1922 	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1923 		AAC_ADAP_HIGH_CMD_ENTRIES;
1924 	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1925 		AAC_ADAP_HIGH_CMD_ENTRIES;
1926 	sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1927 		AAC_HOST_NORM_RESP_ENTRIES;
1928 	sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1929 		AAC_HOST_NORM_RESP_ENTRIES;
1930 	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1931 		AAC_HOST_HIGH_RESP_ENTRIES;
1932 	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1933 		AAC_HOST_HIGH_RESP_ENTRIES;
1934 	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1935 		AAC_ADAP_NORM_RESP_ENTRIES;
1936 	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1937 		AAC_ADAP_NORM_RESP_ENTRIES;
1938 	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1939 		AAC_ADAP_HIGH_RESP_ENTRIES;
1940 	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1941 		AAC_ADAP_HIGH_RESP_ENTRIES;
1942 	sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] =
1943 		&sc->aac_queues->qt_HostNormCmdQueue[0];
1944 	sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] =
1945 		&sc->aac_queues->qt_HostHighCmdQueue[0];
1946 	sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] =
1947 		&sc->aac_queues->qt_AdapNormCmdQueue[0];
1948 	sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] =
1949 		&sc->aac_queues->qt_AdapHighCmdQueue[0];
1950 	sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] =
1951 		&sc->aac_queues->qt_HostNormRespQueue[0];
1952 	sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] =
1953 		&sc->aac_queues->qt_HostHighRespQueue[0];
1954 	sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] =
1955 		&sc->aac_queues->qt_AdapNormRespQueue[0];
1956 	sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] =
1957 		&sc->aac_queues->qt_AdapHighRespQueue[0];
1958 
1959 	/*
1960 	 * Do controller-type-specific initialisation
1961 	 */
1962 	switch (sc->aac_hwif) {
1963 	case AAC_HWIF_I960RX:
1964 		AAC_MEM0_SETREG4(sc, AAC_RX_ODBR, ~0);
1965 		break;
1966 	case AAC_HWIF_RKT:
1967 		AAC_MEM0_SETREG4(sc, AAC_RKT_ODBR, ~0);
1968 		break;
1969 	default:
1970 		break;
1971 	}
1972 
1973 	/*
1974 	 * Give the init structure to the controller.
1975 	 */
1976 	if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT,
1977 			     sc->aac_common_busaddr +
1978 			     offsetof(struct aac_common, ac_init), 0, 0, 0,
1979 			     NULL)) {
1980 		device_printf(sc->aac_dev,
1981 			      "error establishing init structure\n");
1982 		error = EIO;
1983 		goto out;
1984 	}
1985 
1986 	error = 0;
1987 out:
1988 	return(error);
1989 }
1990 
1991 static int
1992 aac_setup_intr(struct aac_softc *sc)
1993 {
1994 	sc->aac_irq_rid = 0;
1995 	if ((sc->aac_irq = bus_alloc_resource_any(sc->aac_dev, SYS_RES_IRQ,
1996 			   			  &sc->aac_irq_rid,
1997 			   			  RF_SHAREABLE |
1998 						  RF_ACTIVE)) == NULL) {
1999 		device_printf(sc->aac_dev, "can't allocate interrupt\n");
2000 		return (EINVAL);
2001 	}
2002 	if (sc->flags & AAC_FLAGS_NEW_COMM) {
2003 		if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
2004 				   INTR_MPSAFE|INTR_TYPE_BIO, NULL,
2005 				   aac_new_intr, sc, &sc->aac_intr)) {
2006 			device_printf(sc->aac_dev, "can't set up interrupt\n");
2007 			return (EINVAL);
2008 		}
2009 	} else {
2010 		if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
2011 				   INTR_TYPE_BIO, aac_filter, NULL,
2012 				   sc, &sc->aac_intr)) {
2013 			device_printf(sc->aac_dev,
2014 				      "can't set up interrupt filter\n");
2015 			return (EINVAL);
2016 		}
2017 	}
2018 	return (0);
2019 }
2020 
2021 /*
2022  * Send a synchronous command to the controller and wait for a result.
2023  * Indicate if the controller completed the command with an error status.
2024  */
2025 static int
2026 aac_sync_command(struct aac_softc *sc, u_int32_t command,
2027 		 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3,
2028 		 u_int32_t *sp)
2029 {
2030 	time_t then;
2031 	u_int32_t status;
2032 
2033 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2034 
2035 	/* populate the mailbox */
2036 	AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3);
2037 
2038 	/* ensure the sync command doorbell flag is cleared */
2039 	AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2040 
2041 	/* then set it to signal the adapter */
2042 	AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND);
2043 
2044 	/* spin waiting for the command to complete */
2045 	then = time_uptime;
2046 	do {
2047 		if (time_uptime > (then + AAC_IMMEDIATE_TIMEOUT)) {
2048 			fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "timed out");
2049 			return(EIO);
2050 		}
2051 	} while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND));
2052 
2053 	/* clear the completion flag */
2054 	AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
2055 
2056 	/* get the command status */
2057 	status = AAC_GET_MAILBOX(sc, 0);
2058 	if (sp != NULL)
2059 		*sp = status;
2060 
2061 	if (status != AAC_SRB_STS_SUCCESS)
2062 		return (-1);
2063 	return(0);
2064 }
2065 
2066 int
2067 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate,
2068 		 struct aac_fib *fib, u_int16_t datasize)
2069 {
2070 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2071 	mtx_assert(&sc->aac_io_lock, MA_OWNED);
2072 
2073 	if (datasize > AAC_FIB_DATASIZE)
2074 		return(EINVAL);
2075 
2076 	/*
2077 	 * Set up the sync FIB
2078 	 */
2079 	fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED |
2080 				AAC_FIBSTATE_INITIALISED |
2081 				AAC_FIBSTATE_EMPTY;
2082 	fib->Header.XferState |= xferstate;
2083 	fib->Header.Command = command;
2084 	fib->Header.StructType = AAC_FIBTYPE_TFIB;
2085 	fib->Header.Size = sizeof(struct aac_fib_header) + datasize;
2086 	fib->Header.SenderSize = sizeof(struct aac_fib);
2087 	fib->Header.SenderFibAddress = 0;	/* Not needed */
2088 	fib->Header.ReceiverFibAddress = sc->aac_common_busaddr +
2089 					 offsetof(struct aac_common,
2090 						  ac_sync_fib);
2091 
2092 	/*
2093 	 * Give the FIB to the controller, wait for a response.
2094 	 */
2095 	if (aac_sync_command(sc, AAC_MONKER_SYNCFIB,
2096 			     fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) {
2097 		fwprintf(sc, HBA_FLAGS_DBG_ERROR_B, "IO error");
2098 		return(EIO);
2099 	}
2100 
2101 	return (0);
2102 }
2103 
2104 /*
2105  * Adapter-space FIB queue manipulation
2106  *
2107  * Note that the queue implementation here is a little funky; neither the PI or
2108  * CI will ever be zero.  This behaviour is a controller feature.
2109  */
2110 static struct {
2111 	int		size;
2112 	int		notify;
2113 } aac_qinfo[] = {
2114 	{AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL},
2115 	{AAC_HOST_HIGH_CMD_ENTRIES, 0},
2116 	{AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY},
2117 	{AAC_ADAP_HIGH_CMD_ENTRIES, 0},
2118 	{AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL},
2119 	{AAC_HOST_HIGH_RESP_ENTRIES, 0},
2120 	{AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY},
2121 	{AAC_ADAP_HIGH_RESP_ENTRIES, 0}
2122 };
2123 
2124 /*
2125  * Atomically insert an entry into the nominated queue, returns 0 on success or
2126  * EBUSY if the queue is full.
2127  *
2128  * Note: it would be more efficient to defer notifying the controller in
2129  *	 the case where we may be inserting several entries in rapid succession,
2130  *	 but implementing this usefully may be difficult (it would involve a
2131  *	 separate queue/notify interface).
2132  */
2133 static int
2134 aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm)
2135 {
2136 	u_int32_t pi, ci;
2137 	int error;
2138 	u_int32_t fib_size;
2139 	u_int32_t fib_addr;
2140 
2141 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2142 
2143 	fib_size = cm->cm_fib->Header.Size;
2144 	fib_addr = cm->cm_fib->Header.ReceiverFibAddress;
2145 
2146 	/* get the producer/consumer indices */
2147 	pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2148 	ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2149 
2150 	/* wrap the queue? */
2151 	if (pi >= aac_qinfo[queue].size)
2152 		pi = 0;
2153 
2154 	/* check for queue full */
2155 	if ((pi + 1) == ci) {
2156 		error = EBUSY;
2157 		goto out;
2158 	}
2159 
2160 	/*
2161 	 * To avoid a race with its completion interrupt, place this command on
2162 	 * the busy queue prior to advertising it to the controller.
2163 	 */
2164 	aac_enqueue_busy(cm);
2165 
2166 	/* populate queue entry */
2167 	(sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
2168 	(sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
2169 
2170 	/* update producer index */
2171 	sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
2172 
2173 	/* notify the adapter if we know how */
2174 	if (aac_qinfo[queue].notify != 0)
2175 		AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2176 
2177 	error = 0;
2178 
2179 out:
2180 	return(error);
2181 }
2182 
2183 /*
2184  * Atomically remove one entry from the nominated queue, returns 0 on
2185  * success or ENOENT if the queue is empty.
2186  */
2187 static int
2188 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size,
2189 		struct aac_fib **fib_addr)
2190 {
2191 	u_int32_t pi, ci;
2192 	u_int32_t fib_index;
2193 	int error;
2194 	int notify;
2195 
2196 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2197 
2198 	/* get the producer/consumer indices */
2199 	pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2200 	ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2201 
2202 	/* check for queue empty */
2203 	if (ci == pi) {
2204 		error = ENOENT;
2205 		goto out;
2206 	}
2207 
2208 	/* wrap the pi so the following test works */
2209 	if (pi >= aac_qinfo[queue].size)
2210 		pi = 0;
2211 
2212 	notify = 0;
2213 	if (ci == pi + 1)
2214 		notify++;
2215 
2216 	/* wrap the queue? */
2217 	if (ci >= aac_qinfo[queue].size)
2218 		ci = 0;
2219 
2220 	/* fetch the entry */
2221 	*fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size;
2222 
2223 	switch (queue) {
2224 	case AAC_HOST_NORM_CMD_QUEUE:
2225 	case AAC_HOST_HIGH_CMD_QUEUE:
2226 		/*
2227 		 * The aq_fib_addr is only 32 bits wide so it can't be counted
2228 		 * on to hold an address.  For AIF's, the adapter assumes
2229 		 * that it's giving us an address into the array of AIF fibs.
2230 		 * Therefore, we have to convert it to an index.
2231 		 */
2232 		fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr /
2233 			sizeof(struct aac_fib);
2234 		*fib_addr = &sc->aac_common->ac_fibs[fib_index];
2235 		break;
2236 
2237 	case AAC_HOST_NORM_RESP_QUEUE:
2238 	case AAC_HOST_HIGH_RESP_QUEUE:
2239 	{
2240 		struct aac_command *cm;
2241 
2242 		/*
2243 		 * As above, an index is used instead of an actual address.
2244 		 * Gotta shift the index to account for the fast response
2245 		 * bit.  No other correction is needed since this value was
2246 		 * originally provided by the driver via the SenderFibAddress
2247 		 * field.
2248 		 */
2249 		fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr;
2250 		cm = sc->aac_commands + (fib_index >> 2);
2251 		*fib_addr = cm->cm_fib;
2252 
2253 		/*
2254 		 * Is this a fast response? If it is, update the fib fields in
2255 		 * local memory since the whole fib isn't DMA'd back up.
2256 		 */
2257 		if (fib_index & 0x01) {
2258 			(*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP;
2259 			*((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL;
2260 		}
2261 		break;
2262 	}
2263 	default:
2264 		panic("Invalid queue in aac_dequeue_fib()");
2265 		break;
2266 	}
2267 
2268 	/* update consumer index */
2269 	sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1;
2270 
2271 	/* if we have made the queue un-full, notify the adapter */
2272 	if (notify && (aac_qinfo[queue].notify != 0))
2273 		AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2274 	error = 0;
2275 
2276 out:
2277 	return(error);
2278 }
2279 
2280 /*
2281  * Put our response to an Adapter Initialed Fib on the response queue
2282  */
2283 static int
2284 aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib)
2285 {
2286 	u_int32_t pi, ci;
2287 	int error;
2288 	u_int32_t fib_size;
2289 	u_int32_t fib_addr;
2290 
2291 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2292 
2293 	/* Tell the adapter where the FIB is */
2294 	fib_size = fib->Header.Size;
2295 	fib_addr = fib->Header.SenderFibAddress;
2296 	fib->Header.ReceiverFibAddress = fib_addr;
2297 
2298 	/* get the producer/consumer indices */
2299 	pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
2300 	ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
2301 
2302 	/* wrap the queue? */
2303 	if (pi >= aac_qinfo[queue].size)
2304 		pi = 0;
2305 
2306 	/* check for queue full */
2307 	if ((pi + 1) == ci) {
2308 		error = EBUSY;
2309 		goto out;
2310 	}
2311 
2312 	/* populate queue entry */
2313 	(sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
2314 	(sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
2315 
2316 	/* update producer index */
2317 	sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
2318 
2319 	/* notify the adapter if we know how */
2320 	if (aac_qinfo[queue].notify != 0)
2321 		AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
2322 
2323 	error = 0;
2324 
2325 out:
2326 	return(error);
2327 }
2328 
2329 /*
2330  * Check for commands that have been outstanding for a suspiciously long time,
2331  * and complain about them.
2332  */
2333 static void
2334 aac_timeout(struct aac_softc *sc)
2335 {
2336 	struct aac_command *cm;
2337 	time_t deadline;
2338 	int timedout, code;
2339 
2340 	/*
2341 	 * Traverse the busy command list, bitch about late commands once
2342 	 * only.
2343 	 */
2344 	timedout = 0;
2345 	deadline = time_uptime - AAC_CMD_TIMEOUT;
2346 	TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) {
2347 		if ((cm->cm_timestamp  < deadline)
2348 		    && !(cm->cm_flags & AAC_CMD_TIMEDOUT)) {
2349 			cm->cm_flags |= AAC_CMD_TIMEDOUT;
2350 			device_printf(sc->aac_dev,
2351 			    "COMMAND %p (TYPE %d) TIMEOUT AFTER %d SECONDS\n",
2352 			    cm, cm->cm_fib->Header.Command,
2353 			    (int)(time_uptime-cm->cm_timestamp));
2354 			AAC_PRINT_FIB(sc, cm->cm_fib);
2355 			timedout++;
2356 		}
2357 	}
2358 
2359 	if (timedout) {
2360 		code = AAC_GET_FWSTATUS(sc);
2361 		if (code != AAC_UP_AND_RUNNING) {
2362 			device_printf(sc->aac_dev, "WARNING! Controller is no "
2363 				      "longer running! code= 0x%x\n", code);
2364 		}
2365 	}
2366 	return;
2367 }
2368 
2369 /*
2370  * Interface Function Vectors
2371  */
2372 
2373 /*
2374  * Read the current firmware status word.
2375  */
2376 static int
2377 aac_sa_get_fwstatus(struct aac_softc *sc)
2378 {
2379 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2380 
2381 	return(AAC_MEM0_GETREG4(sc, AAC_SA_FWSTATUS));
2382 }
2383 
2384 static int
2385 aac_rx_get_fwstatus(struct aac_softc *sc)
2386 {
2387 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2388 
2389 	return(AAC_MEM0_GETREG4(sc, sc->flags & AAC_FLAGS_NEW_COMM ?
2390 	    AAC_RX_OMR0 : AAC_RX_FWSTATUS));
2391 }
2392 
2393 static int
2394 aac_rkt_get_fwstatus(struct aac_softc *sc)
2395 {
2396 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2397 
2398 	return(AAC_MEM0_GETREG4(sc, sc->flags & AAC_FLAGS_NEW_COMM ?
2399 	    AAC_RKT_OMR0 : AAC_RKT_FWSTATUS));
2400 }
2401 
2402 /*
2403  * Notify the controller of a change in a given queue
2404  */
2405 
2406 static void
2407 aac_sa_qnotify(struct aac_softc *sc, int qbit)
2408 {
2409 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2410 
2411 	AAC_MEM0_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit);
2412 }
2413 
2414 static void
2415 aac_rx_qnotify(struct aac_softc *sc, int qbit)
2416 {
2417 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2418 
2419 	AAC_MEM0_SETREG4(sc, AAC_RX_IDBR, qbit);
2420 }
2421 
2422 static void
2423 aac_rkt_qnotify(struct aac_softc *sc, int qbit)
2424 {
2425 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2426 
2427 	AAC_MEM0_SETREG4(sc, AAC_RKT_IDBR, qbit);
2428 }
2429 
2430 /*
2431  * Get the interrupt reason bits
2432  */
2433 static int
2434 aac_sa_get_istatus(struct aac_softc *sc)
2435 {
2436 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2437 
2438 	return(AAC_MEM0_GETREG2(sc, AAC_SA_DOORBELL0));
2439 }
2440 
2441 static int
2442 aac_rx_get_istatus(struct aac_softc *sc)
2443 {
2444 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2445 
2446 	return(AAC_MEM0_GETREG4(sc, AAC_RX_ODBR));
2447 }
2448 
2449 static int
2450 aac_rkt_get_istatus(struct aac_softc *sc)
2451 {
2452 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2453 
2454 	return(AAC_MEM0_GETREG4(sc, AAC_RKT_ODBR));
2455 }
2456 
2457 /*
2458  * Clear some interrupt reason bits
2459  */
2460 static void
2461 aac_sa_clear_istatus(struct aac_softc *sc, int mask)
2462 {
2463 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2464 
2465 	AAC_MEM0_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask);
2466 }
2467 
2468 static void
2469 aac_rx_clear_istatus(struct aac_softc *sc, int mask)
2470 {
2471 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2472 
2473 	AAC_MEM0_SETREG4(sc, AAC_RX_ODBR, mask);
2474 }
2475 
2476 static void
2477 aac_rkt_clear_istatus(struct aac_softc *sc, int mask)
2478 {
2479 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2480 
2481 	AAC_MEM0_SETREG4(sc, AAC_RKT_ODBR, mask);
2482 }
2483 
2484 /*
2485  * Populate the mailbox and set the command word
2486  */
2487 static void
2488 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2489 		u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2490 {
2491 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2492 
2493 	AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX, command);
2494 	AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0);
2495 	AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1);
2496 	AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2);
2497 	AAC_MEM1_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3);
2498 }
2499 
2500 static void
2501 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
2502 		u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2503 {
2504 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2505 
2506 	AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX, command);
2507 	AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0);
2508 	AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1);
2509 	AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2);
2510 	AAC_MEM1_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3);
2511 }
2512 
2513 static void
2514 aac_rkt_set_mailbox(struct aac_softc *sc, u_int32_t command, u_int32_t arg0,
2515 		    u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2516 {
2517 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2518 
2519 	AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX, command);
2520 	AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 4, arg0);
2521 	AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 8, arg1);
2522 	AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 12, arg2);
2523 	AAC_MEM1_SETREG4(sc, AAC_RKT_MAILBOX + 16, arg3);
2524 }
2525 
2526 /*
2527  * Fetch the immediate command status word
2528  */
2529 static int
2530 aac_sa_get_mailbox(struct aac_softc *sc, int mb)
2531 {
2532 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2533 
2534 	return(AAC_MEM1_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4)));
2535 }
2536 
2537 static int
2538 aac_rx_get_mailbox(struct aac_softc *sc, int mb)
2539 {
2540 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2541 
2542 	return(AAC_MEM1_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4)));
2543 }
2544 
2545 static int
2546 aac_rkt_get_mailbox(struct aac_softc *sc, int mb)
2547 {
2548 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2549 
2550 	return(AAC_MEM1_GETREG4(sc, AAC_RKT_MAILBOX + (mb * 4)));
2551 }
2552 
2553 /*
2554  * Set/clear interrupt masks
2555  */
2556 static void
2557 aac_sa_set_interrupts(struct aac_softc *sc, int enable)
2558 {
2559 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2560 
2561 	if (enable) {
2562 		AAC_MEM0_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2563 	} else {
2564 		AAC_MEM0_SETREG2((sc), AAC_SA_MASK0_SET, ~0);
2565 	}
2566 }
2567 
2568 static void
2569 aac_rx_set_interrupts(struct aac_softc *sc, int enable)
2570 {
2571 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2572 
2573 	if (enable) {
2574 		if (sc->flags & AAC_FLAGS_NEW_COMM)
2575 			AAC_MEM0_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INT_NEW_COMM);
2576 		else
2577 			AAC_MEM0_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS);
2578 	} else {
2579 		AAC_MEM0_SETREG4(sc, AAC_RX_OIMR, ~0);
2580 	}
2581 }
2582 
2583 static void
2584 aac_rkt_set_interrupts(struct aac_softc *sc, int enable)
2585 {
2586 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "%sable interrupts", enable ? "en" : "dis");
2587 
2588 	if (enable) {
2589 		if (sc->flags & AAC_FLAGS_NEW_COMM)
2590 			AAC_MEM0_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INT_NEW_COMM);
2591 		else
2592 			AAC_MEM0_SETREG4(sc, AAC_RKT_OIMR, ~AAC_DB_INTERRUPTS);
2593 	} else {
2594 		AAC_MEM0_SETREG4(sc, AAC_RKT_OIMR, ~0);
2595 	}
2596 }
2597 
2598 /*
2599  * New comm. interface: Send command functions
2600  */
2601 static int
2602 aac_rx_send_command(struct aac_softc *sc, struct aac_command *cm)
2603 {
2604 	u_int32_t index, device;
2605 
2606 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "send command (new comm.)");
2607 
2608 	index = AAC_MEM0_GETREG4(sc, AAC_RX_IQUE);
2609 	if (index == 0xffffffffL)
2610 		index = AAC_MEM0_GETREG4(sc, AAC_RX_IQUE);
2611 	if (index == 0xffffffffL)
2612 		return index;
2613 	aac_enqueue_busy(cm);
2614 	device = index;
2615 	AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys & 0xffffffffUL));
2616 	device += 4;
2617 	AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys >> 32));
2618 	device += 4;
2619 	AAC_MEM1_SETREG4(sc, device, cm->cm_fib->Header.Size);
2620 	AAC_MEM0_SETREG4(sc, AAC_RX_IQUE, index);
2621 	return 0;
2622 }
2623 
2624 static int
2625 aac_rkt_send_command(struct aac_softc *sc, struct aac_command *cm)
2626 {
2627 	u_int32_t index, device;
2628 
2629 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "send command (new comm.)");
2630 
2631 	index = AAC_MEM0_GETREG4(sc, AAC_RKT_IQUE);
2632 	if (index == 0xffffffffL)
2633 		index = AAC_MEM0_GETREG4(sc, AAC_RKT_IQUE);
2634 	if (index == 0xffffffffL)
2635 		return index;
2636 	aac_enqueue_busy(cm);
2637 	device = index;
2638 	AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys & 0xffffffffUL));
2639 	device += 4;
2640 	AAC_MEM1_SETREG4(sc, device, (u_int32_t)(cm->cm_fibphys >> 32));
2641 	device += 4;
2642 	AAC_MEM1_SETREG4(sc, device, cm->cm_fib->Header.Size);
2643 	AAC_MEM0_SETREG4(sc, AAC_RKT_IQUE, index);
2644 	return 0;
2645 }
2646 
2647 /*
2648  * New comm. interface: get, set outbound queue index
2649  */
2650 static int
2651 aac_rx_get_outb_queue(struct aac_softc *sc)
2652 {
2653 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2654 
2655 	return(AAC_MEM0_GETREG4(sc, AAC_RX_OQUE));
2656 }
2657 
2658 static int
2659 aac_rkt_get_outb_queue(struct aac_softc *sc)
2660 {
2661 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2662 
2663 	return(AAC_MEM0_GETREG4(sc, AAC_RKT_OQUE));
2664 }
2665 
2666 static void
2667 aac_rx_set_outb_queue(struct aac_softc *sc, int index)
2668 {
2669 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2670 
2671 	AAC_MEM0_SETREG4(sc, AAC_RX_OQUE, index);
2672 }
2673 
2674 static void
2675 aac_rkt_set_outb_queue(struct aac_softc *sc, int index)
2676 {
2677 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2678 
2679 	AAC_MEM0_SETREG4(sc, AAC_RKT_OQUE, index);
2680 }
2681 
2682 /*
2683  * Debugging and Diagnostics
2684  */
2685 
2686 /*
2687  * Print some information about the controller.
2688  */
2689 static void
2690 aac_describe_controller(struct aac_softc *sc)
2691 {
2692 	struct aac_fib *fib;
2693 	struct aac_adapter_info	*info;
2694 	char *adapter_type = "Adaptec RAID controller";
2695 
2696 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2697 
2698 	mtx_lock(&sc->aac_io_lock);
2699 	aac_alloc_sync_fib(sc, &fib);
2700 
2701 	fib->data[0] = 0;
2702 	if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) {
2703 		device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
2704 		aac_release_sync_fib(sc);
2705 		mtx_unlock(&sc->aac_io_lock);
2706 		return;
2707 	}
2708 
2709 	/* save the kernel revision structure for later use */
2710 	info = (struct aac_adapter_info *)&fib->data[0];
2711 	sc->aac_revision = info->KernelRevision;
2712 
2713 	if (bootverbose) {
2714 		device_printf(sc->aac_dev, "%s %dMHz, %dMB memory "
2715 		    "(%dMB cache, %dMB execution), %s\n",
2716 		    aac_describe_code(aac_cpu_variant, info->CpuVariant),
2717 		    info->ClockSpeed, info->TotalMem / (1024 * 1024),
2718 		    info->BufferMem / (1024 * 1024),
2719 		    info->ExecutionMem / (1024 * 1024),
2720 		    aac_describe_code(aac_battery_platform,
2721 		    info->batteryPlatform));
2722 
2723 		device_printf(sc->aac_dev,
2724 		    "Kernel %d.%d-%d, Build %d, S/N %6X\n",
2725 		    info->KernelRevision.external.comp.major,
2726 		    info->KernelRevision.external.comp.minor,
2727 		    info->KernelRevision.external.comp.dash,
2728 		    info->KernelRevision.buildNumber,
2729 		    (u_int32_t)(info->SerialNumber & 0xffffff));
2730 
2731 		device_printf(sc->aac_dev, "Supported Options=%b\n",
2732 			      sc->supported_options,
2733 			      "\20"
2734 			      "\1SNAPSHOT"
2735 			      "\2CLUSTERS"
2736 			      "\3WCACHE"
2737 			      "\4DATA64"
2738 			      "\5HOSTTIME"
2739 			      "\6RAID50"
2740 			      "\7WINDOW4GB"
2741 			      "\10SCSIUPGD"
2742 			      "\11SOFTERR"
2743 			      "\12NORECOND"
2744 			      "\13SGMAP64"
2745 			      "\14ALARM"
2746 			      "\15NONDASD"
2747 			      "\16SCSIMGT"
2748 			      "\17RAIDSCSI"
2749 			      "\21ADPTINFO"
2750 			      "\22NEWCOMM"
2751 			      "\23ARRAY64BIT"
2752 			      "\24HEATSENSOR");
2753 	}
2754 
2755 	if (sc->supported_options & AAC_SUPPORTED_SUPPLEMENT_ADAPTER_INFO) {
2756 		fib->data[0] = 0;
2757 		if (aac_sync_fib(sc, RequestSupplementAdapterInfo, 0, fib, 1))
2758 			device_printf(sc->aac_dev,
2759 			    "RequestSupplementAdapterInfo failed\n");
2760 		else
2761 			adapter_type = ((struct aac_supplement_adapter_info *)
2762 			    &fib->data[0])->AdapterTypeText;
2763 	}
2764 	device_printf(sc->aac_dev, "%s, aac driver %d.%d.%d-%d\n",
2765 		adapter_type,
2766 		AAC_DRIVER_MAJOR_VERSION, AAC_DRIVER_MINOR_VERSION,
2767 		AAC_DRIVER_BUGFIX_LEVEL, AAC_DRIVER_BUILD);
2768 
2769 	aac_release_sync_fib(sc);
2770 	mtx_unlock(&sc->aac_io_lock);
2771 }
2772 
2773 /*
2774  * Look up a text description of a numeric error code and return a pointer to
2775  * same.
2776  */
2777 static char *
2778 aac_describe_code(struct aac_code_lookup *table, u_int32_t code)
2779 {
2780 	int i;
2781 
2782 	for (i = 0; table[i].string != NULL; i++)
2783 		if (table[i].code == code)
2784 			return(table[i].string);
2785 	return(table[i + 1].string);
2786 }
2787 
2788 /*
2789  * Management Interface
2790  */
2791 
2792 static int
2793 aac_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2794 {
2795 	struct aac_softc *sc;
2796 
2797 	sc = dev->si_drv1;
2798 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2799 	device_busy(sc->aac_dev);
2800 	devfs_set_cdevpriv(sc, aac_cdevpriv_dtor);
2801 
2802 	return 0;
2803 }
2804 
2805 static int
2806 aac_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
2807 {
2808 	union aac_statrequest *as;
2809 	struct aac_softc *sc;
2810 	int error = 0;
2811 
2812 	as = (union aac_statrequest *)arg;
2813 	sc = dev->si_drv1;
2814 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2815 
2816 	switch (cmd) {
2817 	case AACIO_STATS:
2818 		switch (as->as_item) {
2819 		case AACQ_FREE:
2820 		case AACQ_BIO:
2821 		case AACQ_READY:
2822 		case AACQ_BUSY:
2823 			bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
2824 			      sizeof(struct aac_qstat));
2825 			break;
2826 		default:
2827 			error = ENOENT;
2828 			break;
2829 		}
2830 	break;
2831 
2832 	case FSACTL_SENDFIB:
2833 	case FSACTL_SEND_LARGE_FIB:
2834 		arg = *(caddr_t*)arg;
2835 	case FSACTL_LNX_SENDFIB:
2836 	case FSACTL_LNX_SEND_LARGE_FIB:
2837 		fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_SENDFIB");
2838 		error = aac_ioctl_sendfib(sc, arg);
2839 		break;
2840 	case FSACTL_SEND_RAW_SRB:
2841 		arg = *(caddr_t*)arg;
2842 	case FSACTL_LNX_SEND_RAW_SRB:
2843 		fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_SEND_RAW_SRB");
2844 		error = aac_ioctl_send_raw_srb(sc, arg);
2845 		break;
2846 	case FSACTL_AIF_THREAD:
2847 	case FSACTL_LNX_AIF_THREAD:
2848 		fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_AIF_THREAD");
2849 		error = EINVAL;
2850 		break;
2851 	case FSACTL_OPEN_GET_ADAPTER_FIB:
2852 		arg = *(caddr_t*)arg;
2853 	case FSACTL_LNX_OPEN_GET_ADAPTER_FIB:
2854 		fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_OPEN_GET_ADAPTER_FIB");
2855 		error = aac_open_aif(sc, arg);
2856 		break;
2857 	case FSACTL_GET_NEXT_ADAPTER_FIB:
2858 		arg = *(caddr_t*)arg;
2859 	case FSACTL_LNX_GET_NEXT_ADAPTER_FIB:
2860 		fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_NEXT_ADAPTER_FIB");
2861 		error = aac_getnext_aif(sc, arg);
2862 		break;
2863 	case FSACTL_CLOSE_GET_ADAPTER_FIB:
2864 		arg = *(caddr_t*)arg;
2865 	case FSACTL_LNX_CLOSE_GET_ADAPTER_FIB:
2866 		fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_CLOSE_GET_ADAPTER_FIB");
2867 		error = aac_close_aif(sc, arg);
2868 		break;
2869 	case FSACTL_MINIPORT_REV_CHECK:
2870 		arg = *(caddr_t*)arg;
2871 	case FSACTL_LNX_MINIPORT_REV_CHECK:
2872 		fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_MINIPORT_REV_CHECK");
2873 		error = aac_rev_check(sc, arg);
2874 		break;
2875 	case FSACTL_QUERY_DISK:
2876 		arg = *(caddr_t*)arg;
2877 	case FSACTL_LNX_QUERY_DISK:
2878 		fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_QUERY_DISK");
2879 		error = aac_query_disk(sc, arg);
2880 		break;
2881 	case FSACTL_DELETE_DISK:
2882 	case FSACTL_LNX_DELETE_DISK:
2883 		/*
2884 		 * We don't trust the underland to tell us when to delete a
2885 		 * container, rather we rely on an AIF coming from the
2886 		 * controller
2887 		 */
2888 		error = 0;
2889 		break;
2890 	case FSACTL_GET_PCI_INFO:
2891 		arg = *(caddr_t*)arg;
2892 	case FSACTL_LNX_GET_PCI_INFO:
2893 		fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_PCI_INFO");
2894 		error = aac_get_pci_info(sc, arg);
2895 		break;
2896 	case FSACTL_GET_FEATURES:
2897 		arg = *(caddr_t*)arg;
2898 	case FSACTL_LNX_GET_FEATURES:
2899 		fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "FSACTL_GET_FEATURES");
2900 		error = aac_supported_features(sc, arg);
2901 		break;
2902 	default:
2903 		fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "unsupported cmd 0x%lx\n", cmd);
2904 		error = EINVAL;
2905 		break;
2906 	}
2907 	return(error);
2908 }
2909 
2910 static int
2911 aac_poll(struct cdev *dev, int poll_events, struct thread *td)
2912 {
2913 	struct aac_softc *sc;
2914 	struct aac_fib_context *ctx;
2915 	int revents;
2916 
2917 	sc = dev->si_drv1;
2918 	revents = 0;
2919 
2920 	mtx_lock(&sc->aac_aifq_lock);
2921 	if ((poll_events & (POLLRDNORM | POLLIN)) != 0) {
2922 		for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
2923 			if (ctx->ctx_idx != sc->aifq_idx || ctx->ctx_wrap) {
2924 				revents |= poll_events & (POLLIN | POLLRDNORM);
2925 				break;
2926 			}
2927 		}
2928 	}
2929 	mtx_unlock(&sc->aac_aifq_lock);
2930 
2931 	if (revents == 0) {
2932 		if (poll_events & (POLLIN | POLLRDNORM))
2933 			selrecord(td, &sc->rcv_select);
2934 	}
2935 
2936 	return (revents);
2937 }
2938 
2939 static void
2940 aac_ioctl_event(struct aac_softc *sc, struct aac_event *event, void *arg)
2941 {
2942 
2943 	switch (event->ev_type) {
2944 	case AAC_EVENT_CMFREE:
2945 		mtx_assert(&sc->aac_io_lock, MA_OWNED);
2946 		if (aac_alloc_command(sc, (struct aac_command **)arg)) {
2947 			aac_add_event(sc, event);
2948 			return;
2949 		}
2950 		free(event, M_AACBUF);
2951 		wakeup(arg);
2952 		break;
2953 	default:
2954 		break;
2955 	}
2956 }
2957 
2958 /*
2959  * Send a FIB supplied from userspace
2960  */
2961 static int
2962 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
2963 {
2964 	struct aac_command *cm;
2965 	int size, error;
2966 
2967 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
2968 
2969 	cm = NULL;
2970 
2971 	/*
2972 	 * Get a command
2973 	 */
2974 	mtx_lock(&sc->aac_io_lock);
2975 	if (aac_alloc_command(sc, &cm)) {
2976 		struct aac_event *event;
2977 
2978 		event = malloc(sizeof(struct aac_event), M_AACBUF,
2979 		    M_NOWAIT | M_ZERO);
2980 		if (event == NULL) {
2981 			error = EBUSY;
2982 			mtx_unlock(&sc->aac_io_lock);
2983 			goto out;
2984 		}
2985 		event->ev_type = AAC_EVENT_CMFREE;
2986 		event->ev_callback = aac_ioctl_event;
2987 		event->ev_arg = &cm;
2988 		aac_add_event(sc, event);
2989 		msleep(&cm, &sc->aac_io_lock, 0, "sendfib", 0);
2990 	}
2991 	mtx_unlock(&sc->aac_io_lock);
2992 
2993 	/*
2994 	 * Fetch the FIB header, then re-copy to get data as well.
2995 	 */
2996 	if ((error = copyin(ufib, cm->cm_fib,
2997 			    sizeof(struct aac_fib_header))) != 0)
2998 		goto out;
2999 	size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
3000 	if (size > sc->aac_max_fib_size) {
3001 		device_printf(sc->aac_dev, "incoming FIB oversized (%d > %d)\n",
3002 			      size, sc->aac_max_fib_size);
3003 		size = sc->aac_max_fib_size;
3004 	}
3005 	if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
3006 		goto out;
3007 	cm->cm_fib->Header.Size = size;
3008 	cm->cm_timestamp = time_uptime;
3009 
3010 	/*
3011 	 * Pass the FIB to the controller, wait for it to complete.
3012 	 */
3013 	mtx_lock(&sc->aac_io_lock);
3014 	error = aac_wait_command(cm);
3015 	mtx_unlock(&sc->aac_io_lock);
3016 	if (error != 0) {
3017 		device_printf(sc->aac_dev,
3018 			      "aac_wait_command return %d\n", error);
3019 		goto out;
3020 	}
3021 
3022 	/*
3023 	 * Copy the FIB and data back out to the caller.
3024 	 */
3025 	size = cm->cm_fib->Header.Size;
3026 	if (size > sc->aac_max_fib_size) {
3027 		device_printf(sc->aac_dev, "outbound FIB oversized (%d > %d)\n",
3028 			      size, sc->aac_max_fib_size);
3029 		size = sc->aac_max_fib_size;
3030 	}
3031 	error = copyout(cm->cm_fib, ufib, size);
3032 
3033 out:
3034 	if (cm != NULL) {
3035 		mtx_lock(&sc->aac_io_lock);
3036 		aac_release_command(cm);
3037 		mtx_unlock(&sc->aac_io_lock);
3038 	}
3039 	return(error);
3040 }
3041 
3042 /*
3043  * Send a passthrough FIB supplied from userspace
3044  */
3045 static int
3046 aac_ioctl_send_raw_srb(struct aac_softc *sc, caddr_t arg)
3047 {
3048 	struct aac_command *cm;
3049 	struct aac_event *event;
3050 	struct aac_fib *fib;
3051 	struct aac_srb *srbcmd, *user_srb;
3052 	struct aac_sg_entry *sge;
3053 	struct aac_sg_entry64 *sge64;
3054 	void *srb_sg_address, *ureply;
3055 	uint32_t fibsize, srb_sg_bytecount;
3056 	int error, transfer_data;
3057 
3058 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3059 
3060 	cm = NULL;
3061 	transfer_data = 0;
3062 	fibsize = 0;
3063 	user_srb = (struct aac_srb *)arg;
3064 
3065 	mtx_lock(&sc->aac_io_lock);
3066 	if (aac_alloc_command(sc, &cm)) {
3067 		 event = malloc(sizeof(struct aac_event), M_AACBUF,
3068 		    M_NOWAIT | M_ZERO);
3069 		if (event == NULL) {
3070 			error = EBUSY;
3071 			mtx_unlock(&sc->aac_io_lock);
3072 			goto out;
3073 		}
3074 		event->ev_type = AAC_EVENT_CMFREE;
3075 		event->ev_callback = aac_ioctl_event;
3076 		event->ev_arg = &cm;
3077 		aac_add_event(sc, event);
3078 		msleep(cm, &sc->aac_io_lock, 0, "aacraw", 0);
3079 	}
3080 	mtx_unlock(&sc->aac_io_lock);
3081 
3082 	cm->cm_data = NULL;
3083 	fib = cm->cm_fib;
3084 	srbcmd = (struct aac_srb *)fib->data;
3085 	error = copyin(&user_srb->data_len, &fibsize, sizeof(uint32_t));
3086 	if (error != 0)
3087 		goto out;
3088 	if (fibsize > (sc->aac_max_fib_size - sizeof(struct aac_fib_header))) {
3089 		error = EINVAL;
3090 		goto out;
3091 	}
3092 	error = copyin(user_srb, srbcmd, fibsize);
3093 	if (error != 0)
3094 		goto out;
3095 	srbcmd->function = 0;
3096 	srbcmd->retry_limit = 0;
3097 	if (srbcmd->sg_map.SgCount > 1) {
3098 		error = EINVAL;
3099 		goto out;
3100 	}
3101 
3102 	/* Retrieve correct SG entries. */
3103 	if (fibsize == (sizeof(struct aac_srb) +
3104 	    srbcmd->sg_map.SgCount * sizeof(struct aac_sg_entry))) {
3105 		sge = srbcmd->sg_map.SgEntry;
3106 		sge64 = NULL;
3107 		srb_sg_bytecount = sge->SgByteCount;
3108 		srb_sg_address = (void *)(uintptr_t)sge->SgAddress;
3109 	}
3110 #ifdef __amd64__
3111 	else if (fibsize == (sizeof(struct aac_srb) +
3112 	    srbcmd->sg_map.SgCount * sizeof(struct aac_sg_entry64))) {
3113 		sge = NULL;
3114 		sge64 = (struct aac_sg_entry64 *)srbcmd->sg_map.SgEntry;
3115 		srb_sg_bytecount = sge64->SgByteCount;
3116 		srb_sg_address = (void *)sge64->SgAddress;
3117 		if (sge64->SgAddress > 0xffffffffull &&
3118 		    (sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
3119 			error = EINVAL;
3120 			goto out;
3121 		}
3122 	}
3123 #endif
3124 	else {
3125 		error = EINVAL;
3126 		goto out;
3127 	}
3128 	ureply = (char *)arg + fibsize;
3129 	srbcmd->data_len = srb_sg_bytecount;
3130 	if (srbcmd->sg_map.SgCount == 1)
3131 		transfer_data = 1;
3132 
3133 	cm->cm_sgtable = (struct aac_sg_table *)&srbcmd->sg_map;
3134 	if (transfer_data) {
3135 		cm->cm_datalen = srb_sg_bytecount;
3136 		cm->cm_data = malloc(cm->cm_datalen, M_AACBUF, M_NOWAIT);
3137 		if (cm->cm_data == NULL) {
3138 			error = ENOMEM;
3139 			goto out;
3140 		}
3141 		if (srbcmd->flags & AAC_SRB_FLAGS_DATA_IN)
3142 			cm->cm_flags |= AAC_CMD_DATAIN;
3143 		if (srbcmd->flags & AAC_SRB_FLAGS_DATA_OUT) {
3144 			cm->cm_flags |= AAC_CMD_DATAOUT;
3145 			error = copyin(srb_sg_address, cm->cm_data,
3146 			    cm->cm_datalen);
3147 			if (error != 0)
3148 				goto out;
3149 		}
3150 	}
3151 
3152 	fib->Header.Size = sizeof(struct aac_fib_header) +
3153 	    sizeof(struct aac_srb);
3154 	fib->Header.XferState =
3155 	    AAC_FIBSTATE_HOSTOWNED   |
3156 	    AAC_FIBSTATE_INITIALISED |
3157 	    AAC_FIBSTATE_EMPTY       |
3158 	    AAC_FIBSTATE_FROMHOST    |
3159 	    AAC_FIBSTATE_REXPECTED   |
3160 	    AAC_FIBSTATE_NORM        |
3161 	    AAC_FIBSTATE_ASYNC       |
3162 	    AAC_FIBSTATE_FAST_RESPONSE;
3163 	fib->Header.Command = (sc->flags & AAC_FLAGS_SG_64BIT) != 0 ?
3164 	    ScsiPortCommandU64 : ScsiPortCommand;
3165 
3166 	mtx_lock(&sc->aac_io_lock);
3167 	aac_wait_command(cm);
3168 	mtx_unlock(&sc->aac_io_lock);
3169 
3170 	if (transfer_data && (srbcmd->flags & AAC_SRB_FLAGS_DATA_IN) != 0) {
3171 		error = copyout(cm->cm_data, srb_sg_address, cm->cm_datalen);
3172 		if (error != 0)
3173 			goto out;
3174 	}
3175 	error = copyout(fib->data, ureply, sizeof(struct aac_srb_response));
3176 out:
3177 	if (cm != NULL) {
3178 		if (cm->cm_data != NULL)
3179 			free(cm->cm_data, M_AACBUF);
3180 		mtx_lock(&sc->aac_io_lock);
3181 		aac_release_command(cm);
3182 		mtx_unlock(&sc->aac_io_lock);
3183 	}
3184 	return(error);
3185 }
3186 
3187 /*
3188  * cdevpriv interface private destructor.
3189  */
3190 static void
3191 aac_cdevpriv_dtor(void *arg)
3192 {
3193 	struct aac_softc *sc;
3194 
3195 	sc = arg;
3196 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3197 	mtx_lock(&Giant);
3198 	device_unbusy(sc->aac_dev);
3199 	mtx_unlock(&Giant);
3200 }
3201 
3202 /*
3203  * Handle an AIF sent to us by the controller; queue it for later reference.
3204  * If the queue fills up, then drop the older entries.
3205  */
3206 static void
3207 aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib)
3208 {
3209 	struct aac_aif_command *aif;
3210 	struct aac_container *co, *co_next;
3211 	struct aac_fib_context *ctx;
3212 	struct aac_mntinforesp *mir;
3213 	int next, current, found;
3214 	int count = 0, added = 0, i = 0;
3215 	uint32_t channel;
3216 
3217 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3218 
3219 	aif = (struct aac_aif_command*)&fib->data[0];
3220 	aac_print_aif(sc, aif);
3221 
3222 	/* Is it an event that we should care about? */
3223 	switch (aif->command) {
3224 	case AifCmdEventNotify:
3225 		switch (aif->data.EN.type) {
3226 		case AifEnAddContainer:
3227 		case AifEnDeleteContainer:
3228 			/*
3229 			 * A container was added or deleted, but the message
3230 			 * doesn't tell us anything else!  Re-enumerate the
3231 			 * containers and sort things out.
3232 			 */
3233 			aac_alloc_sync_fib(sc, &fib);
3234 			do {
3235 				/*
3236 				 * Ask the controller for its containers one at
3237 				 * a time.
3238 				 * XXX What if the controller's list changes
3239 				 * midway through this enumaration?
3240 				 * XXX This should be done async.
3241 				 */
3242 				if ((mir = aac_get_container_info(sc, fib, i)) == NULL)
3243 					continue;
3244 				if (i == 0)
3245 					count = mir->MntRespCount;
3246 				/*
3247 				 * Check the container against our list.
3248 				 * co->co_found was already set to 0 in a
3249 				 * previous run.
3250 				 */
3251 				if ((mir->Status == ST_OK) &&
3252 				    (mir->MntTable[0].VolType != CT_NONE)) {
3253 					found = 0;
3254 					TAILQ_FOREACH(co,
3255 						      &sc->aac_container_tqh,
3256 						      co_link) {
3257 						if (co->co_mntobj.ObjectId ==
3258 						    mir->MntTable[0].ObjectId) {
3259 							co->co_found = 1;
3260 							found = 1;
3261 							break;
3262 						}
3263 					}
3264 					/*
3265 					 * If the container matched, continue
3266 					 * in the list.
3267 					 */
3268 					if (found) {
3269 						i++;
3270 						continue;
3271 					}
3272 
3273 					/*
3274 					 * This is a new container.  Do all the
3275 					 * appropriate things to set it up.
3276 					 */
3277 					aac_add_container(sc, mir, 1);
3278 					added = 1;
3279 				}
3280 				i++;
3281 			} while ((i < count) && (i < AAC_MAX_CONTAINERS));
3282 			aac_release_sync_fib(sc);
3283 
3284 			/*
3285 			 * Go through our list of containers and see which ones
3286 			 * were not marked 'found'.  Since the controller didn't
3287 			 * list them they must have been deleted.  Do the
3288 			 * appropriate steps to destroy the device.  Also reset
3289 			 * the co->co_found field.
3290 			 */
3291 			co = TAILQ_FIRST(&sc->aac_container_tqh);
3292 			while (co != NULL) {
3293 				if (co->co_found == 0) {
3294 					mtx_unlock(&sc->aac_io_lock);
3295 					mtx_lock(&Giant);
3296 					device_delete_child(sc->aac_dev,
3297 							    co->co_disk);
3298 					mtx_unlock(&Giant);
3299 					mtx_lock(&sc->aac_io_lock);
3300 					co_next = TAILQ_NEXT(co, co_link);
3301 					mtx_lock(&sc->aac_container_lock);
3302 					TAILQ_REMOVE(&sc->aac_container_tqh, co,
3303 						     co_link);
3304 					mtx_unlock(&sc->aac_container_lock);
3305 					free(co, M_AACBUF);
3306 					co = co_next;
3307 				} else {
3308 					co->co_found = 0;
3309 					co = TAILQ_NEXT(co, co_link);
3310 				}
3311 			}
3312 
3313 			/* Attach the newly created containers */
3314 			if (added) {
3315 				mtx_unlock(&sc->aac_io_lock);
3316 				mtx_lock(&Giant);
3317 				bus_generic_attach(sc->aac_dev);
3318 				mtx_unlock(&Giant);
3319 				mtx_lock(&sc->aac_io_lock);
3320 			}
3321 
3322 			break;
3323 
3324 		case AifEnEnclosureManagement:
3325 			switch (aif->data.EN.data.EEE.eventType) {
3326 			case AIF_EM_DRIVE_INSERTION:
3327 			case AIF_EM_DRIVE_REMOVAL:
3328 				channel = aif->data.EN.data.EEE.unitID;
3329 				if (sc->cam_rescan_cb != NULL)
3330 					sc->cam_rescan_cb(sc,
3331 					    (channel >> 24) & 0xF,
3332 					    (channel & 0xFFFF));
3333 				break;
3334 			}
3335 			break;
3336 
3337 		case AifEnAddJBOD:
3338 		case AifEnDeleteJBOD:
3339 			channel = aif->data.EN.data.ECE.container;
3340 			if (sc->cam_rescan_cb != NULL)
3341 				sc->cam_rescan_cb(sc, (channel >> 24) & 0xF,
3342 				    AAC_CAM_TARGET_WILDCARD);
3343 			break;
3344 
3345 		default:
3346 			break;
3347 		}
3348 
3349 	default:
3350 		break;
3351 	}
3352 
3353 	/* Copy the AIF data to the AIF queue for ioctl retrieval */
3354 	mtx_lock(&sc->aac_aifq_lock);
3355 	current = sc->aifq_idx;
3356 	next = (current + 1) % AAC_AIFQ_LENGTH;
3357 	if (next == 0)
3358 		sc->aifq_filled = 1;
3359 	bcopy(fib, &sc->aac_aifq[current], sizeof(struct aac_fib));
3360 	/* modify AIF contexts */
3361 	if (sc->aifq_filled) {
3362 		for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3363 			if (next == ctx->ctx_idx)
3364 				ctx->ctx_wrap = 1;
3365 			else if (current == ctx->ctx_idx && ctx->ctx_wrap)
3366 				ctx->ctx_idx = next;
3367 		}
3368 	}
3369 	sc->aifq_idx = next;
3370 	/* On the off chance that someone is sleeping for an aif... */
3371 	if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
3372 		wakeup(sc->aac_aifq);
3373 	/* Wakeup any poll()ers */
3374 	selwakeuppri(&sc->rcv_select, PRIBIO);
3375 	mtx_unlock(&sc->aac_aifq_lock);
3376 
3377 	return;
3378 }
3379 
3380 /*
3381  * Return the Revision of the driver to userspace and check to see if the
3382  * userspace app is possibly compatible.  This is extremely bogus since
3383  * our driver doesn't follow Adaptec's versioning system.  Cheat by just
3384  * returning what the card reported.
3385  */
3386 static int
3387 aac_rev_check(struct aac_softc *sc, caddr_t udata)
3388 {
3389 	struct aac_rev_check rev_check;
3390 	struct aac_rev_check_resp rev_check_resp;
3391 	int error = 0;
3392 
3393 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3394 
3395 	/*
3396 	 * Copyin the revision struct from userspace
3397 	 */
3398 	if ((error = copyin(udata, (caddr_t)&rev_check,
3399 			sizeof(struct aac_rev_check))) != 0) {
3400 		return error;
3401 	}
3402 
3403 	fwprintf(sc, HBA_FLAGS_DBG_IOCTL_COMMANDS_B, "Userland revision= %d\n",
3404 	      rev_check.callingRevision.buildNumber);
3405 
3406 	/*
3407 	 * Doctor up the response struct.
3408 	 */
3409 	rev_check_resp.possiblyCompatible = 1;
3410 	rev_check_resp.adapterSWRevision.external.comp.major =
3411 	    AAC_DRIVER_MAJOR_VERSION;
3412 	rev_check_resp.adapterSWRevision.external.comp.minor =
3413 	    AAC_DRIVER_MINOR_VERSION;
3414 	rev_check_resp.adapterSWRevision.external.comp.type =
3415 	    AAC_DRIVER_TYPE;
3416 	rev_check_resp.adapterSWRevision.external.comp.dash =
3417 	    AAC_DRIVER_BUGFIX_LEVEL;
3418 	rev_check_resp.adapterSWRevision.buildNumber =
3419 	    AAC_DRIVER_BUILD;
3420 
3421 	return(copyout((caddr_t)&rev_check_resp, udata,
3422 			sizeof(struct aac_rev_check_resp)));
3423 }
3424 
3425 /*
3426  * Pass the fib context to the caller
3427  */
3428 static int
3429 aac_open_aif(struct aac_softc *sc, caddr_t arg)
3430 {
3431 	struct aac_fib_context *fibctx, *ctx;
3432 	int error = 0;
3433 
3434 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3435 
3436 	fibctx = malloc(sizeof(struct aac_fib_context), M_AACBUF, M_NOWAIT|M_ZERO);
3437 	if (fibctx == NULL)
3438 		return (ENOMEM);
3439 
3440 	mtx_lock(&sc->aac_aifq_lock);
3441 	/* all elements are already 0, add to queue */
3442 	if (sc->fibctx == NULL)
3443 		sc->fibctx = fibctx;
3444 	else {
3445 		for (ctx = sc->fibctx; ctx->next; ctx = ctx->next)
3446 			;
3447 		ctx->next = fibctx;
3448 		fibctx->prev = ctx;
3449 	}
3450 
3451 	/* evaluate unique value */
3452 	fibctx->unique = (*(u_int32_t *)&fibctx & 0xffffffff);
3453 	ctx = sc->fibctx;
3454 	while (ctx != fibctx) {
3455 		if (ctx->unique == fibctx->unique) {
3456 			fibctx->unique++;
3457 			ctx = sc->fibctx;
3458 		} else {
3459 			ctx = ctx->next;
3460 		}
3461 	}
3462 	mtx_unlock(&sc->aac_aifq_lock);
3463 
3464 	error = copyout(&fibctx->unique, (void *)arg, sizeof(u_int32_t));
3465 	if (error)
3466 		aac_close_aif(sc, (caddr_t)ctx);
3467 	return error;
3468 }
3469 
3470 /*
3471  * Close the caller's fib context
3472  */
3473 static int
3474 aac_close_aif(struct aac_softc *sc, caddr_t arg)
3475 {
3476 	struct aac_fib_context *ctx;
3477 
3478 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3479 
3480 	mtx_lock(&sc->aac_aifq_lock);
3481 	for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3482 		if (ctx->unique == *(uint32_t *)&arg) {
3483 			if (ctx == sc->fibctx)
3484 				sc->fibctx = NULL;
3485 			else {
3486 				ctx->prev->next = ctx->next;
3487 				if (ctx->next)
3488 					ctx->next->prev = ctx->prev;
3489 			}
3490 			break;
3491 		}
3492 	}
3493 	mtx_unlock(&sc->aac_aifq_lock);
3494 	if (ctx)
3495 		free(ctx, M_AACBUF);
3496 
3497 	return 0;
3498 }
3499 
3500 /*
3501  * Pass the caller the next AIF in their queue
3502  */
3503 static int
3504 aac_getnext_aif(struct aac_softc *sc, caddr_t arg)
3505 {
3506 	struct get_adapter_fib_ioctl agf;
3507 	struct aac_fib_context *ctx;
3508 	int error;
3509 
3510 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3511 
3512 	if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
3513 		for (ctx = sc->fibctx; ctx; ctx = ctx->next) {
3514 			if (agf.AdapterFibContext == ctx->unique)
3515 				break;
3516 		}
3517 		if (!ctx)
3518 			return (EFAULT);
3519 
3520 		error = aac_return_aif(sc, ctx, agf.AifFib);
3521 		if (error == EAGAIN && agf.Wait) {
3522 			fwprintf(sc, HBA_FLAGS_DBG_AIF_B, "aac_getnext_aif(): waiting for AIF");
3523 			sc->aac_state |= AAC_STATE_AIF_SLEEPER;
3524 			while (error == EAGAIN) {
3525 				error = tsleep(sc->aac_aifq, PRIBIO |
3526 					       PCATCH, "aacaif", 0);
3527 				if (error == 0)
3528 					error = aac_return_aif(sc, ctx, agf.AifFib);
3529 			}
3530 			sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
3531 		}
3532 	}
3533 	return(error);
3534 }
3535 
3536 /*
3537  * Hand the next AIF off the top of the queue out to userspace.
3538  */
3539 static int
3540 aac_return_aif(struct aac_softc *sc, struct aac_fib_context *ctx, caddr_t uptr)
3541 {
3542 	int current, error;
3543 
3544 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3545 
3546 	mtx_lock(&sc->aac_aifq_lock);
3547 	current = ctx->ctx_idx;
3548 	if (current == sc->aifq_idx && !ctx->ctx_wrap) {
3549 		/* empty */
3550 		mtx_unlock(&sc->aac_aifq_lock);
3551 		return (EAGAIN);
3552 	}
3553 	error =
3554 		copyout(&sc->aac_aifq[current], (void *)uptr, sizeof(struct aac_fib));
3555 	if (error)
3556 		device_printf(sc->aac_dev,
3557 		    "aac_return_aif: copyout returned %d\n", error);
3558 	else {
3559 		ctx->ctx_wrap = 0;
3560 		ctx->ctx_idx = (current + 1) % AAC_AIFQ_LENGTH;
3561 	}
3562 	mtx_unlock(&sc->aac_aifq_lock);
3563 	return(error);
3564 }
3565 
3566 static int
3567 aac_get_pci_info(struct aac_softc *sc, caddr_t uptr)
3568 {
3569 	struct aac_pci_info {
3570 		u_int32_t bus;
3571 		u_int32_t slot;
3572 	} pciinf;
3573 	int error;
3574 
3575 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3576 
3577 	pciinf.bus = pci_get_bus(sc->aac_dev);
3578 	pciinf.slot = pci_get_slot(sc->aac_dev);
3579 
3580 	error = copyout((caddr_t)&pciinf, uptr,
3581 			sizeof(struct aac_pci_info));
3582 
3583 	return (error);
3584 }
3585 
3586 static int
3587 aac_supported_features(struct aac_softc *sc, caddr_t uptr)
3588 {
3589 	struct aac_features f;
3590 	int error;
3591 
3592 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3593 
3594 	if ((error = copyin(uptr, &f, sizeof (f))) != 0)
3595 		return (error);
3596 
3597 	/*
3598 	 * When the management driver receives FSACTL_GET_FEATURES ioctl with
3599 	 * ALL zero in the featuresState, the driver will return the current
3600 	 * state of all the supported features, the data field will not be
3601 	 * valid.
3602 	 * When the management driver receives FSACTL_GET_FEATURES ioctl with
3603 	 * a specific bit set in the featuresState, the driver will return the
3604 	 * current state of this specific feature and whatever data that are
3605 	 * associated with the feature in the data field or perform whatever
3606 	 * action needed indicates in the data field.
3607 	 */
3608 	if (f.feat.fValue == 0) {
3609 		f.feat.fBits.largeLBA =
3610 		    (sc->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
3611 		/* TODO: In the future, add other features state here as well */
3612 	} else {
3613 		if (f.feat.fBits.largeLBA)
3614 			f.feat.fBits.largeLBA =
3615 			    (sc->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
3616 		/* TODO: Add other features state and data in the future */
3617 	}
3618 
3619 	error = copyout(&f, uptr, sizeof (f));
3620 	return (error);
3621 }
3622 
3623 /*
3624  * Give the userland some information about the container.  The AAC arch
3625  * expects the driver to be a SCSI passthrough type driver, so it expects
3626  * the containers to have b:t:l numbers.  Fake it.
3627  */
3628 static int
3629 aac_query_disk(struct aac_softc *sc, caddr_t uptr)
3630 {
3631 	struct aac_query_disk query_disk;
3632 	struct aac_container *co;
3633 	struct aac_disk	*disk;
3634 	int error, id;
3635 
3636 	fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
3637 
3638 	disk = NULL;
3639 
3640 	error = copyin(uptr, (caddr_t)&query_disk,
3641 		       sizeof(struct aac_query_disk));
3642 	if (error)
3643 		return (error);
3644 
3645 	id = query_disk.ContainerNumber;
3646 	if (id == -1)
3647 		return (EINVAL);
3648 
3649 	mtx_lock(&sc->aac_container_lock);
3650 	TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
3651 		if (co->co_mntobj.ObjectId == id)
3652 			break;
3653 		}
3654 
3655 	if (co == NULL) {
3656 			query_disk.Valid = 0;
3657 			query_disk.Locked = 0;
3658 			query_disk.Deleted = 1;		/* XXX is this right? */
3659 	} else {
3660 		disk = device_get_softc(co->co_disk);
3661 		query_disk.Valid = 1;
3662 		query_disk.Locked =
3663 		    (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0;
3664 		query_disk.Deleted = 0;
3665 		query_disk.Bus = device_get_unit(sc->aac_dev);
3666 		query_disk.Target = disk->unit;
3667 		query_disk.Lun = 0;
3668 		query_disk.UnMapped = 0;
3669 		sprintf(&query_disk.diskDeviceName[0], "%s%d",
3670 			disk->ad_disk->d_name, disk->ad_disk->d_unit);
3671 	}
3672 	mtx_unlock(&sc->aac_container_lock);
3673 
3674 	error = copyout((caddr_t)&query_disk, uptr,
3675 			sizeof(struct aac_query_disk));
3676 
3677 	return (error);
3678 }
3679 
3680 static void
3681 aac_get_bus_info(struct aac_softc *sc)
3682 {
3683 	struct aac_fib *fib;
3684 	struct aac_ctcfg *c_cmd;
3685 	struct aac_ctcfg_resp *c_resp;
3686 	struct aac_vmioctl *vmi;
3687 	struct aac_vmi_businf_resp *vmi_resp;
3688 	struct aac_getbusinf businfo;
3689 	struct aac_sim *caminf;
3690 	device_t child;
3691 	int i, found, error;
3692 
3693 	mtx_lock(&sc->aac_io_lock);
3694 	aac_alloc_sync_fib(sc, &fib);
3695 	c_cmd = (struct aac_ctcfg *)&fib->data[0];
3696 	bzero(c_cmd, sizeof(struct aac_ctcfg));
3697 
3698 	c_cmd->Command = VM_ContainerConfig;
3699 	c_cmd->cmd = CT_GET_SCSI_METHOD;
3700 	c_cmd->param = 0;
3701 
3702 	error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3703 	    sizeof(struct aac_ctcfg));
3704 	if (error) {
3705 		device_printf(sc->aac_dev, "Error %d sending "
3706 		    "VM_ContainerConfig command\n", error);
3707 		aac_release_sync_fib(sc);
3708 		mtx_unlock(&sc->aac_io_lock);
3709 		return;
3710 	}
3711 
3712 	c_resp = (struct aac_ctcfg_resp *)&fib->data[0];
3713 	if (c_resp->Status != ST_OK) {
3714 		device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n",
3715 		    c_resp->Status);
3716 		aac_release_sync_fib(sc);
3717 		mtx_unlock(&sc->aac_io_lock);
3718 		return;
3719 	}
3720 
3721 	sc->scsi_method_id = c_resp->param;
3722 
3723 	vmi = (struct aac_vmioctl *)&fib->data[0];
3724 	bzero(vmi, sizeof(struct aac_vmioctl));
3725 
3726 	vmi->Command = VM_Ioctl;
3727 	vmi->ObjType = FT_DRIVE;
3728 	vmi->MethId = sc->scsi_method_id;
3729 	vmi->ObjId = 0;
3730 	vmi->IoctlCmd = GetBusInfo;
3731 
3732 	error = aac_sync_fib(sc, ContainerCommand, 0, fib,
3733 	    sizeof(struct aac_vmi_businf_resp));
3734 	if (error) {
3735 		device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
3736 		    error);
3737 		aac_release_sync_fib(sc);
3738 		mtx_unlock(&sc->aac_io_lock);
3739 		return;
3740 	}
3741 
3742 	vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0];
3743 	if (vmi_resp->Status != ST_OK) {
3744 		device_printf(sc->aac_dev, "VM_Ioctl returned %d\n",
3745 		    vmi_resp->Status);
3746 		aac_release_sync_fib(sc);
3747 		mtx_unlock(&sc->aac_io_lock);
3748 		return;
3749 	}
3750 
3751 	bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf));
3752 	aac_release_sync_fib(sc);
3753 	mtx_unlock(&sc->aac_io_lock);
3754 
3755 	found = 0;
3756 	for (i = 0; i < businfo.BusCount; i++) {
3757 		if (businfo.BusValid[i] != AAC_BUS_VALID)
3758 			continue;
3759 
3760 		caminf = (struct aac_sim *)malloc( sizeof(struct aac_sim),
3761 		    M_AACBUF, M_NOWAIT | M_ZERO);
3762 		if (caminf == NULL) {
3763 			device_printf(sc->aac_dev,
3764 			    "No memory to add passthrough bus %d\n", i);
3765 			break;
3766 		};
3767 
3768 		child = device_add_child(sc->aac_dev, "aacp", -1);
3769 		if (child == NULL) {
3770 			device_printf(sc->aac_dev,
3771 			    "device_add_child failed for passthrough bus %d\n",
3772 			    i);
3773 			free(caminf, M_AACBUF);
3774 			break;
3775 		}
3776 
3777 		caminf->TargetsPerBus = businfo.TargetsPerBus;
3778 		caminf->BusNumber = i;
3779 		caminf->InitiatorBusId = businfo.InitiatorBusId[i];
3780 		caminf->aac_sc = sc;
3781 		caminf->sim_dev = child;
3782 
3783 		device_set_ivars(child, caminf);
3784 		device_set_desc(child, "SCSI Passthrough Bus");
3785 		TAILQ_INSERT_TAIL(&sc->aac_sim_tqh, caminf, sim_link);
3786 
3787 		found = 1;
3788 	}
3789 
3790 	if (found)
3791 		bus_generic_attach(sc->aac_dev);
3792 
3793 	return;
3794 }
3795