aac.c revision 120576
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: head/sys/dev/aac/aac.c 120576 2003-09-29 14:10:04Z scottl $");
32
33/*
34 * Driver for the Adaptec 'FSA' family of PCI/SCSI RAID adapters.
35 */
36
37#include "opt_aac.h"
38
39/* #include <stddef.h> */
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/malloc.h>
43#include <sys/kernel.h>
44#include <sys/kthread.h>
45#include <sys/sysctl.h>
46#include <sys/poll.h>
47#include <sys/ioccom.h>
48
49#include <sys/bus.h>
50#include <sys/conf.h>
51#include <sys/signalvar.h>
52#include <sys/time.h>
53#include <sys/eventhandler.h>
54
55#include <machine/bus_memio.h>
56#include <machine/bus.h>
57#include <machine/resource.h>
58
59#include <dev/aac/aacreg.h>
60#include <dev/aac/aac_ioctl.h>
61#include <dev/aac/aacvar.h>
62#include <dev/aac/aac_tables.h>
63
64static void	aac_startup(void *arg);
65static void	aac_add_container(struct aac_softc *sc,
66				  struct aac_mntinforesp *mir, int f);
67static void	aac_get_bus_info(struct aac_softc *sc);
68
69/* Command Processing */
70static void	aac_timeout(struct aac_softc *sc);
71static int	aac_map_command(struct aac_command *cm);
72static void	aac_complete(void *context, int pending);
73static int	aac_bio_command(struct aac_softc *sc, struct aac_command **cmp);
74static void	aac_bio_complete(struct aac_command *cm);
75static int	aac_wait_command(struct aac_command *cm, int timeout);
76static void	aac_command_thread(struct aac_softc *sc);
77
78/* Command Buffer Management */
79static void	aac_map_command_sg(void *arg, bus_dma_segment_t *segs,
80				   int nseg, int error);
81static void	aac_map_command_helper(void *arg, bus_dma_segment_t *segs,
82				       int nseg, int error);
83static int	aac_alloc_commands(struct aac_softc *sc);
84static void	aac_free_commands(struct aac_softc *sc);
85static void	aac_unmap_command(struct aac_command *cm);
86
87/* Hardware Interface */
88static void	aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg,
89			       int error);
90static int	aac_check_firmware(struct aac_softc *sc);
91static int	aac_init(struct aac_softc *sc);
92static int	aac_sync_command(struct aac_softc *sc, u_int32_t command,
93				 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2,
94				 u_int32_t arg3, u_int32_t *sp);
95static int	aac_enqueue_fib(struct aac_softc *sc, int queue,
96				struct aac_command *cm);
97static int	aac_dequeue_fib(struct aac_softc *sc, int queue,
98				u_int32_t *fib_size, struct aac_fib **fib_addr);
99static int	aac_enqueue_response(struct aac_softc *sc, int queue,
100				     struct aac_fib *fib);
101
102/* Falcon/PPC interface */
103static int	aac_fa_get_fwstatus(struct aac_softc *sc);
104static void	aac_fa_qnotify(struct aac_softc *sc, int qbit);
105static int	aac_fa_get_istatus(struct aac_softc *sc);
106static void	aac_fa_clear_istatus(struct aac_softc *sc, int mask);
107static void	aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
108				   u_int32_t arg0, u_int32_t arg1,
109				   u_int32_t arg2, u_int32_t arg3);
110static int	aac_fa_get_mailbox(struct aac_softc *sc, int mb);
111static void	aac_fa_set_interrupts(struct aac_softc *sc, int enable);
112
113struct aac_interface aac_fa_interface = {
114	aac_fa_get_fwstatus,
115	aac_fa_qnotify,
116	aac_fa_get_istatus,
117	aac_fa_clear_istatus,
118	aac_fa_set_mailbox,
119	aac_fa_get_mailbox,
120	aac_fa_set_interrupts
121};
122
123/* StrongARM interface */
124static int	aac_sa_get_fwstatus(struct aac_softc *sc);
125static void	aac_sa_qnotify(struct aac_softc *sc, int qbit);
126static int	aac_sa_get_istatus(struct aac_softc *sc);
127static void	aac_sa_clear_istatus(struct aac_softc *sc, int mask);
128static void	aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
129				   u_int32_t arg0, u_int32_t arg1,
130				   u_int32_t arg2, u_int32_t arg3);
131static int	aac_sa_get_mailbox(struct aac_softc *sc, int mb);
132static void	aac_sa_set_interrupts(struct aac_softc *sc, int enable);
133
134struct aac_interface aac_sa_interface = {
135	aac_sa_get_fwstatus,
136	aac_sa_qnotify,
137	aac_sa_get_istatus,
138	aac_sa_clear_istatus,
139	aac_sa_set_mailbox,
140	aac_sa_get_mailbox,
141	aac_sa_set_interrupts
142};
143
144/* i960Rx interface */
145static int	aac_rx_get_fwstatus(struct aac_softc *sc);
146static void	aac_rx_qnotify(struct aac_softc *sc, int qbit);
147static int	aac_rx_get_istatus(struct aac_softc *sc);
148static void	aac_rx_clear_istatus(struct aac_softc *sc, int mask);
149static void	aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
150				   u_int32_t arg0, u_int32_t arg1,
151				   u_int32_t arg2, u_int32_t arg3);
152static int	aac_rx_get_mailbox(struct aac_softc *sc, int mb);
153static void	aac_rx_set_interrupts(struct aac_softc *sc, int enable);
154
155struct aac_interface aac_rx_interface = {
156	aac_rx_get_fwstatus,
157	aac_rx_qnotify,
158	aac_rx_get_istatus,
159	aac_rx_clear_istatus,
160	aac_rx_set_mailbox,
161	aac_rx_get_mailbox,
162	aac_rx_set_interrupts
163};
164
165/* Debugging and Diagnostics */
166static void	aac_describe_controller(struct aac_softc *sc);
167static char	*aac_describe_code(struct aac_code_lookup *table,
168				   u_int32_t code);
169
170/* Management Interface */
171static d_open_t		aac_open;
172static d_close_t	aac_close;
173static d_ioctl_t	aac_ioctl;
174static d_poll_t		aac_poll;
175static int		aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib);
176static void		aac_handle_aif(struct aac_softc *sc,
177					   struct aac_fib *fib);
178static int		aac_rev_check(struct aac_softc *sc, caddr_t udata);
179static int		aac_getnext_aif(struct aac_softc *sc, caddr_t arg);
180static int		aac_return_aif(struct aac_softc *sc, caddr_t uptr);
181static int		aac_query_disk(struct aac_softc *sc, caddr_t uptr);
182
183static struct cdevsw aac_cdevsw = {
184	.d_open =	aac_open,
185	.d_close =	aac_close,
186	.d_ioctl =	aac_ioctl,
187	.d_poll =	aac_poll,
188	.d_name =	"aac",
189};
190
191MALLOC_DEFINE(M_AACBUF, "aacbuf", "Buffers for the AAC driver");
192
193/* sysctl node */
194SYSCTL_NODE(_hw, OID_AUTO, aac, CTLFLAG_RD, 0, "AAC driver parameters");
195
196/*
197 * Device Interface
198 */
199
200/*
201 * Initialise the controller and softc
202 */
203int
204aac_attach(struct aac_softc *sc)
205{
206	int error, unit;
207
208	debug_called(1);
209
210	/*
211	 * Initialise per-controller queues.
212	 */
213	aac_initq_free(sc);
214	aac_initq_ready(sc);
215	aac_initq_busy(sc);
216	aac_initq_bio(sc);
217
218	/*
219	 * Initialise command-completion task.
220	 */
221	TASK_INIT(&sc->aac_task_complete, 0, aac_complete, sc);
222
223	/* disable interrupts before we enable anything */
224	AAC_MASK_INTERRUPTS(sc);
225
226	/* mark controller as suspended until we get ourselves organised */
227	sc->aac_state |= AAC_STATE_SUSPEND;
228
229	/*
230	 * Check that the firmware on the card is supported.
231	 */
232	if ((error = aac_check_firmware(sc)) != 0)
233		return(error);
234
235	/*
236	 * Initialize locks
237	 */
238	AAC_LOCK_INIT(&sc->aac_sync_lock, "AAC sync FIB lock");
239	AAC_LOCK_INIT(&sc->aac_aifq_lock, "AAC AIF lock");
240	AAC_LOCK_INIT(&sc->aac_io_lock, "AAC I/O lock");
241	AAC_LOCK_INIT(&sc->aac_container_lock, "AAC container lock");
242	TAILQ_INIT(&sc->aac_container_tqh);
243
244
245	/*
246	 * Initialise the adapter.
247	 */
248	if ((error = aac_init(sc)) != 0)
249		return(error);
250
251	/*
252	 * Print a little information about the controller.
253	 */
254	aac_describe_controller(sc);
255
256	/*
257	 * Register to probe our containers later.
258	 */
259	sc->aac_ich.ich_func = aac_startup;
260	sc->aac_ich.ich_arg = sc;
261	if (config_intrhook_establish(&sc->aac_ich) != 0) {
262		device_printf(sc->aac_dev,
263			      "can't establish configuration hook\n");
264		return(ENXIO);
265	}
266
267	/*
268	 * Make the control device.
269	 */
270	unit = device_get_unit(sc->aac_dev);
271	sc->aac_dev_t = make_dev(&aac_cdevsw, unit, UID_ROOT, GID_OPERATOR,
272				 0640, "aac%d", unit);
273	(void)make_dev_alias(sc->aac_dev_t, "afa%d", unit);
274	(void)make_dev_alias(sc->aac_dev_t, "hpn%d", unit);
275	sc->aac_dev_t->si_drv1 = sc;
276
277	/* Create the AIF thread */
278	if (kthread_create((void(*)(void *))aac_command_thread, sc,
279			   &sc->aifthread, 0, 0, "aac%daif", unit))
280		panic("Could not create AIF thread\n");
281
282	/* Register the shutdown method to only be called post-dump */
283	if ((sc->eh = EVENTHANDLER_REGISTER(shutdown_final, aac_shutdown,
284	    sc->aac_dev, SHUTDOWN_PRI_DEFAULT)) == NULL)
285		device_printf(sc->aac_dev,
286			      "shutdown event registration failed\n");
287
288	/* Register with CAM for the non-DASD devices */
289	if ((sc->flags & AAC_FLAGS_ENABLE_CAM) != 0) {
290		TAILQ_INIT(&sc->aac_sim_tqh);
291		aac_get_bus_info(sc);
292	}
293
294	return(0);
295}
296
297/*
298 * Probe for containers, create disks.
299 */
300static void
301aac_startup(void *arg)
302{
303	struct aac_softc *sc;
304	struct aac_fib *fib;
305	struct aac_mntinfo *mi;
306	struct aac_mntinforesp *mir = NULL;
307	int count = 0, i = 0;
308
309	debug_called(1);
310
311	sc = (struct aac_softc *)arg;
312
313	/* disconnect ourselves from the intrhook chain */
314	config_intrhook_disestablish(&sc->aac_ich);
315
316	aac_alloc_sync_fib(sc, &fib, 0);
317	mi = (struct aac_mntinfo *)&fib->data[0];
318
319	/* loop over possible containers */
320	do {
321		/* request information on this container */
322		bzero(mi, sizeof(struct aac_mntinfo));
323		mi->Command = VM_NameServe;
324		mi->MntType = FT_FILESYS;
325		mi->MntCount = i;
326		if (aac_sync_fib(sc, ContainerCommand, 0, fib,
327				 sizeof(struct aac_mntinfo))) {
328			printf("error probing container %d", i);
329			continue;
330		}
331
332		mir = (struct aac_mntinforesp *)&fib->data[0];
333		/* XXX Need to check if count changed */
334		count = mir->MntRespCount;
335		aac_add_container(sc, mir, 0);
336		i++;
337	} while ((i < count) && (i < AAC_MAX_CONTAINERS));
338
339	aac_release_sync_fib(sc);
340
341	/* poke the bus to actually attach the child devices */
342	if (bus_generic_attach(sc->aac_dev))
343		device_printf(sc->aac_dev, "bus_generic_attach failed\n");
344
345	/* mark the controller up */
346	sc->aac_state &= ~AAC_STATE_SUSPEND;
347
348	/* enable interrupts now */
349	AAC_UNMASK_INTERRUPTS(sc);
350}
351
352/*
353 * Create a device to respresent a new container
354 */
355static void
356aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f)
357{
358	struct aac_container *co;
359	device_t child;
360
361	/*
362	 * Check container volume type for validity.  Note that many of
363	 * the possible types may never show up.
364	 */
365	if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) {
366		co = (struct aac_container *)malloc(sizeof *co, M_AACBUF,
367		       M_NOWAIT | M_ZERO);
368		if (co == NULL)
369			panic("Out of memory?!\n");
370		debug(1, "id %x  name '%.16s'  size %u  type %d",
371		      mir->MntTable[0].ObjectId,
372		      mir->MntTable[0].FileSystemName,
373		      mir->MntTable[0].Capacity, mir->MntTable[0].VolType);
374
375		if ((child = device_add_child(sc->aac_dev, "aacd", -1)) == NULL)
376			device_printf(sc->aac_dev, "device_add_child failed\n");
377		else
378			device_set_ivars(child, co);
379		device_set_desc(child, aac_describe_code(aac_container_types,
380				mir->MntTable[0].VolType));
381		co->co_disk = child;
382		co->co_found = f;
383		bcopy(&mir->MntTable[0], &co->co_mntobj,
384		      sizeof(struct aac_mntobj));
385		AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
386		TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link);
387		AAC_LOCK_RELEASE(&sc->aac_container_lock);
388	}
389}
390
391/*
392 * Free all of the resources associated with (sc)
393 *
394 * Should not be called if the controller is active.
395 */
396void
397aac_free(struct aac_softc *sc)
398{
399
400	debug_called(1);
401
402	/* remove the control device */
403	if (sc->aac_dev_t != NULL)
404		destroy_dev(sc->aac_dev_t);
405
406	/* throw away any FIB buffers, discard the FIB DMA tag */
407	aac_free_commands(sc);
408	if (sc->aac_fib_dmat)
409		bus_dma_tag_destroy(sc->aac_fib_dmat);
410
411	free(sc->aac_commands, M_AACBUF);
412
413	/* destroy the common area */
414	if (sc->aac_common) {
415		bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap);
416		bus_dmamem_free(sc->aac_common_dmat, sc->aac_common,
417				sc->aac_common_dmamap);
418	}
419	if (sc->aac_common_dmat)
420		bus_dma_tag_destroy(sc->aac_common_dmat);
421
422	/* disconnect the interrupt handler */
423	if (sc->aac_intr)
424		bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr);
425	if (sc->aac_irq != NULL)
426		bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid,
427				     sc->aac_irq);
428
429	/* destroy data-transfer DMA tag */
430	if (sc->aac_buffer_dmat)
431		bus_dma_tag_destroy(sc->aac_buffer_dmat);
432
433	/* destroy the parent DMA tag */
434	if (sc->aac_parent_dmat)
435		bus_dma_tag_destroy(sc->aac_parent_dmat);
436
437	/* release the register window mapping */
438	if (sc->aac_regs_resource != NULL)
439		bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
440				     sc->aac_regs_rid, sc->aac_regs_resource);
441}
442
443/*
444 * Disconnect from the controller completely, in preparation for unload.
445 */
446int
447aac_detach(device_t dev)
448{
449	struct aac_softc *sc;
450	struct aac_container *co;
451	struct aac_sim	*sim;
452	int error;
453
454	debug_called(1);
455
456	sc = device_get_softc(dev);
457
458	if (sc->aac_state & AAC_STATE_OPEN)
459		return(EBUSY);
460
461	/* Remove the child containers */
462	while ((co = TAILQ_FIRST(&sc->aac_container_tqh)) != NULL) {
463		error = device_delete_child(dev, co->co_disk);
464		if (error)
465			return (error);
466		TAILQ_REMOVE(&sc->aac_container_tqh, co, co_link);
467		free(co, M_AACBUF);
468	}
469
470	/* Remove the CAM SIMs */
471	while ((sim = TAILQ_FIRST(&sc->aac_sim_tqh)) != NULL) {
472		TAILQ_REMOVE(&sc->aac_sim_tqh, sim, sim_link);
473		error = device_delete_child(dev, sim->sim_dev);
474		if (error)
475			return (error);
476		free(sim, M_AACBUF);
477	}
478
479	if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
480		sc->aifflags |= AAC_AIFFLAGS_EXIT;
481		wakeup(sc->aifthread);
482		tsleep(sc->aac_dev, PUSER | PCATCH, "aacdch", 30 * hz);
483	}
484
485	if (sc->aifflags & AAC_AIFFLAGS_RUNNING)
486		panic("Cannot shutdown AIF thread\n");
487
488	if ((error = aac_shutdown(dev)))
489		return(error);
490
491	EVENTHANDLER_DEREGISTER(shutdown_final, sc->eh);
492
493	aac_free(sc);
494
495	return(0);
496}
497
498/*
499 * Bring the controller down to a dormant state and detach all child devices.
500 *
501 * This function is called before detach or system shutdown.
502 *
503 * Note that we can assume that the bioq on the controller is empty, as we won't
504 * allow shutdown if any device is open.
505 */
506int
507aac_shutdown(device_t dev)
508{
509	struct aac_softc *sc;
510	struct aac_fib *fib;
511	struct aac_close_command *cc;
512
513	debug_called(1);
514
515	sc = device_get_softc(dev);
516
517	sc->aac_state |= AAC_STATE_SUSPEND;
518
519	/*
520	 * Send a Container shutdown followed by a HostShutdown FIB to the
521	 * controller to convince it that we don't want to talk to it anymore.
522	 * We've been closed and all I/O completed already
523	 */
524	device_printf(sc->aac_dev, "shutting down controller...");
525
526	aac_alloc_sync_fib(sc, &fib, AAC_SYNC_LOCK_FORCE);
527	cc = (struct aac_close_command *)&fib->data[0];
528
529	bzero(cc, sizeof(struct aac_close_command));
530	cc->Command = VM_CloseAll;
531	cc->ContainerId = 0xffffffff;
532	if (aac_sync_fib(sc, ContainerCommand, 0, fib,
533	    sizeof(struct aac_close_command)))
534		printf("FAILED.\n");
535	else
536		printf("done\n");
537#if 0
538	else {
539		fib->data[0] = 0;
540		/*
541		 * XXX Issuing this command to the controller makes it shut down
542		 * but also keeps it from coming back up without a reset of the
543		 * PCI bus.  This is not desirable if you are just unloading the
544		 * driver module with the intent to reload it later.
545		 */
546		if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN,
547		    fib, 1)) {
548			printf("FAILED.\n");
549		} else {
550			printf("done.\n");
551		}
552	}
553#endif
554
555	AAC_MASK_INTERRUPTS(sc);
556
557	return(0);
558}
559
560/*
561 * Bring the controller to a quiescent state, ready for system suspend.
562 */
563int
564aac_suspend(device_t dev)
565{
566	struct aac_softc *sc;
567
568	debug_called(1);
569
570	sc = device_get_softc(dev);
571
572	sc->aac_state |= AAC_STATE_SUSPEND;
573
574	AAC_MASK_INTERRUPTS(sc);
575	return(0);
576}
577
578/*
579 * Bring the controller back to a state ready for operation.
580 */
581int
582aac_resume(device_t dev)
583{
584	struct aac_softc *sc;
585
586	debug_called(1);
587
588	sc = device_get_softc(dev);
589
590	sc->aac_state &= ~AAC_STATE_SUSPEND;
591	AAC_UNMASK_INTERRUPTS(sc);
592	return(0);
593}
594
595/*
596 * Take an interrupt.
597 */
598void
599aac_intr(void *arg)
600{
601	struct aac_softc *sc;
602	u_int32_t *resp_queue;
603	u_int16_t reason;
604
605	debug_called(2);
606
607	sc = (struct aac_softc *)arg;
608
609	/*
610	 * Optimize the common case of adapter response interrupts.
611	 * We must read from the card prior to processing the responses
612	 * to ensure the clear is flushed prior to accessing the queues.
613	 * Reading the queues from local memory might save us a PCI read.
614	 */
615	resp_queue = sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE];
616	if (resp_queue[AAC_PRODUCER_INDEX] != resp_queue[AAC_CONSUMER_INDEX])
617		reason = AAC_DB_RESPONSE_READY;
618	else
619		reason = AAC_GET_ISTATUS(sc);
620	AAC_CLEAR_ISTATUS(sc, reason);
621	(void)AAC_GET_ISTATUS(sc);
622
623	/* It's not ok to return here because of races with the previous step */
624	if (reason & AAC_DB_RESPONSE_READY)
625		/* handle completion processing */
626		taskqueue_enqueue(taskqueue_swi, &sc->aac_task_complete);
627
628	/* controller wants to talk to the log */
629	if (reason & AAC_DB_PRINTF) {
630		if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
631			sc->aifflags |= AAC_AIFFLAGS_PRINTF;
632		} else
633			aac_print_printf(sc);
634	}
635
636	/* controller has a message for us? */
637	if (reason & AAC_DB_COMMAND_READY) {
638		if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
639			sc->aifflags |= AAC_AIFFLAGS_AIF;
640		} else {
641			/*
642			 * XXX If the kthread is dead and we're at this point,
643			 * there are bigger problems than just figuring out
644			 * what to do with an AIF.
645			 */
646		}
647
648	}
649
650	if ((sc->aifflags & AAC_AIFFLAGS_PENDING) != 0)
651		/* XXX Should this be done with cv_signal? */
652		wakeup(sc->aifthread);
653}
654
655/*
656 * Command Processing
657 */
658
659/*
660 * Start as much queued I/O as possible on the controller
661 */
662void
663aac_startio(struct aac_softc *sc)
664{
665	struct aac_command *cm;
666
667	debug_called(2);
668
669	if (sc->flags & AAC_QUEUE_FRZN)
670		return;
671
672	for (;;) {
673		/*
674		 * Try to get a command that's been put off for lack of
675		 * resources
676		 */
677		cm = aac_dequeue_ready(sc);
678
679		/*
680		 * Try to build a command off the bio queue (ignore error
681		 * return)
682		 */
683		if (cm == NULL)
684			aac_bio_command(sc, &cm);
685
686		/* nothing to do? */
687		if (cm == NULL)
688			break;
689
690		/* try to give the command to the controller */
691		if (aac_map_command(cm) == EBUSY) {
692			/* put it on the ready queue for later */
693			aac_requeue_ready(cm);
694			break;
695		}
696	}
697}
698
699/*
700 * Deliver a command to the controller; allocate controller resources at the
701 * last moment when possible.
702 */
703static int
704aac_map_command(struct aac_command *cm)
705{
706	struct aac_softc *sc;
707	int error;
708
709	debug_called(2);
710
711	sc = cm->cm_sc;
712	error = 0;
713
714	/* don't map more than once */
715	if (cm->cm_flags & AAC_CMD_MAPPED)
716		return (0);
717
718	if (cm->cm_datalen != 0) {
719		error = bus_dmamap_load(sc->aac_buffer_dmat, cm->cm_datamap,
720					cm->cm_data, cm->cm_datalen,
721					aac_map_command_sg, cm, 0);
722		if (error == EINPROGRESS) {
723			debug(1, "freezing queue\n");
724			sc->flags |= AAC_QUEUE_FRZN;
725			error = 0;
726		}
727	} else {
728		aac_map_command_sg(cm, NULL, 0, 0);
729	}
730	return (error);
731}
732
733/*
734 * Handle notification of one or more FIBs coming from the controller.
735 */
736static void
737aac_command_thread(struct aac_softc *sc)
738{
739	struct aac_fib *fib;
740	u_int32_t fib_size;
741	int size;
742
743	debug_called(2);
744
745	sc->aifflags |= AAC_AIFFLAGS_RUNNING;
746
747	while (!(sc->aifflags & AAC_AIFFLAGS_EXIT)) {
748		if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0)
749			tsleep(sc->aifthread, PRIBIO, "aifthd",
750			       AAC_PERIODIC_INTERVAL * hz);
751
752		if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0)
753			aac_timeout(sc);
754
755		/* Check the hardware printf message buffer */
756		if ((sc->aifflags & AAC_AIFFLAGS_PRINTF) != 0) {
757			sc->aifflags &= ~AAC_AIFFLAGS_PRINTF;
758			aac_print_printf(sc);
759		}
760
761		/* See if any FIBs need to be allocated */
762		if ((sc->aifflags & AAC_AIFFLAGS_ALLOCFIBS) != 0) {
763			AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
764			aac_alloc_commands(sc);
765			sc->aifflags &= ~AAC_AIFFLAGS_ALLOCFIBS;
766			AAC_LOCK_RELEASE(&sc->aac_io_lock);
767		}
768
769		/* While we're here, check to see if any commands are stuck */
770		while (sc->aifflags & AAC_AIFFLAGS_AIF) {
771			if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE,
772					    &fib_size, &fib)) {
773				sc->aifflags &= ~AAC_AIFFLAGS_AIF;
774				break;	/* nothing to do */
775			}
776
777			AAC_PRINT_FIB(sc, fib);
778
779			switch (fib->Header.Command) {
780			case AifRequest:
781				aac_handle_aif(sc, fib);
782				break;
783			default:
784				device_printf(sc->aac_dev, "unknown command "
785					      "from controller\n");
786				break;
787			}
788
789			if ((fib->Header.XferState == 0) ||
790			    (fib->Header.StructType != AAC_FIBTYPE_TFIB))
791				break;
792
793			/* Return the AIF to the controller. */
794			if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) {
795				fib->Header.XferState |= AAC_FIBSTATE_DONEHOST;
796				*(AAC_FSAStatus*)fib->data = ST_OK;
797
798				/* XXX Compute the Size field? */
799				size = fib->Header.Size;
800				if (size > sizeof(struct aac_fib)) {
801					size = sizeof(struct aac_fib);
802					fib->Header.Size = size;
803				}
804				/*
805				 * Since we did not generate this command, it
806				 * cannot go through the normal
807				 * enqueue->startio chain.
808				 */
809				aac_enqueue_response(sc,
810						     AAC_ADAP_NORM_RESP_QUEUE,
811						     fib);
812			}
813		}
814	}
815	sc->aifflags &= ~AAC_AIFFLAGS_RUNNING;
816	wakeup(sc->aac_dev);
817
818	mtx_lock(&Giant);
819	kthread_exit(0);
820}
821
822/*
823 * Process completed commands.
824 */
825static void
826aac_complete(void *context, int pending)
827{
828	struct aac_softc *sc;
829	struct aac_command *cm;
830	struct aac_fib *fib;
831	u_int32_t fib_size;
832
833	debug_called(2);
834
835	sc = (struct aac_softc *)context;
836
837	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
838
839	/* pull completed commands off the queue */
840	for (;;) {
841		/* look for completed FIBs on our queue */
842		if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size,
843				    &fib))
844			break;	/* nothing to do */
845
846		/* get the command, unmap and queue for later processing */
847		cm = sc->aac_commands + fib->Header.SenderData;
848		if (cm == NULL) {
849			AAC_PRINT_FIB(sc, fib);
850			break;
851		}
852
853		aac_remove_busy(cm);
854		aac_unmap_command(cm);		/* XXX defer? */
855		cm->cm_flags |= AAC_CMD_COMPLETED;
856
857		/* is there a completion handler? */
858		if (cm->cm_complete != NULL) {
859			cm->cm_complete(cm);
860		} else {
861			/* assume that someone is sleeping on this command */
862			wakeup(cm);
863		}
864	}
865
866	/* see if we can start some more I/O */
867	sc->flags &= ~AAC_QUEUE_FRZN;
868	aac_startio(sc);
869
870	AAC_LOCK_RELEASE(&sc->aac_io_lock);
871}
872
873/*
874 * Handle a bio submitted from a disk device.
875 */
876void
877aac_submit_bio(struct bio *bp)
878{
879	struct aac_disk *ad;
880	struct aac_softc *sc;
881
882	debug_called(2);
883
884	ad = (struct aac_disk *)bp->bio_disk->d_drv1;
885	sc = ad->ad_controller;
886
887	/* queue the BIO and try to get some work done */
888	aac_enqueue_bio(sc, bp);
889	aac_startio(sc);
890}
891
892/*
893 * Get a bio and build a command to go with it.
894 */
895static int
896aac_bio_command(struct aac_softc *sc, struct aac_command **cmp)
897{
898	struct aac_command *cm;
899	struct aac_fib *fib;
900	struct aac_disk *ad;
901	struct bio *bp;
902
903	debug_called(2);
904
905	/* get the resources we will need */
906	cm = NULL;
907	if ((bp = aac_dequeue_bio(sc)) == NULL)
908		goto fail;
909	if (aac_alloc_command(sc, &cm))	/* get a command */
910		goto fail;
911
912	/* fill out the command */
913	cm->cm_data = (void *)bp->bio_data;
914	cm->cm_datalen = bp->bio_bcount;
915	cm->cm_complete = aac_bio_complete;
916	cm->cm_private = bp;
917	cm->cm_timestamp = time_second;
918	cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
919
920	/* build the FIB */
921	fib = cm->cm_fib;
922	fib->Header.Size = sizeof(struct aac_fib_header);
923	fib->Header.XferState =
924		AAC_FIBSTATE_HOSTOWNED   |
925		AAC_FIBSTATE_INITIALISED |
926		AAC_FIBSTATE_EMPTY	 |
927		AAC_FIBSTATE_FROMHOST	 |
928		AAC_FIBSTATE_REXPECTED   |
929		AAC_FIBSTATE_NORM	 |
930		AAC_FIBSTATE_ASYNC	 |
931		AAC_FIBSTATE_FAST_RESPONSE;
932
933	/* build the read/write request */
934	ad = (struct aac_disk *)bp->bio_disk->d_drv1;
935
936	if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
937		fib->Header.Command = ContainerCommand;
938		if (bp->bio_cmd == BIO_READ) {
939			struct aac_blockread *br;
940			br = (struct aac_blockread *)&fib->data[0];
941			br->Command = VM_CtBlockRead;
942			br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
943			br->BlockNumber = bp->bio_pblkno;
944			br->ByteCount = bp->bio_bcount;
945			fib->Header.Size += sizeof(struct aac_blockread);
946			cm->cm_sgtable = &br->SgMap;
947			cm->cm_flags |= AAC_CMD_DATAIN;
948		} else {
949			struct aac_blockwrite *bw;
950			bw = (struct aac_blockwrite *)&fib->data[0];
951			bw->Command = VM_CtBlockWrite;
952			bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
953			bw->BlockNumber = bp->bio_pblkno;
954			bw->ByteCount = bp->bio_bcount;
955			bw->Stable = CUNSTABLE;
956			fib->Header.Size += sizeof(struct aac_blockwrite);
957			cm->cm_flags |= AAC_CMD_DATAOUT;
958			cm->cm_sgtable = &bw->SgMap;
959		}
960	} else {
961		fib->Header.Command = ContainerCommand64;
962		if (bp->bio_cmd == BIO_READ) {
963			struct aac_blockread64 *br;
964			br = (struct aac_blockread64 *)&fib->data[0];
965			br->Command = VM_CtHostRead64;
966			br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
967			br->SectorCount = bp->bio_bcount / AAC_BLOCK_SIZE;
968			br->BlockNumber = bp->bio_pblkno;
969			br->Pad = 0;
970			br->Flags = 0;
971			fib->Header.Size += sizeof(struct aac_blockread64);
972			cm->cm_flags |= AAC_CMD_DATAOUT;
973			(struct aac_sg_table64 *)cm->cm_sgtable = &br->SgMap64;
974		} else {
975			struct aac_blockwrite64 *bw;
976			bw = (struct aac_blockwrite64 *)&fib->data[0];
977			bw->Command = VM_CtHostWrite64;
978			bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
979			bw->SectorCount = bp->bio_bcount / AAC_BLOCK_SIZE;
980			bw->BlockNumber = bp->bio_pblkno;
981			bw->Pad = 0;
982			bw->Flags = 0;
983			fib->Header.Size += sizeof(struct aac_blockwrite64);
984			cm->cm_flags |= AAC_CMD_DATAIN;
985			(struct aac_sg_table64 *)cm->cm_sgtable = &bw->SgMap64;
986		}
987	}
988
989	*cmp = cm;
990	return(0);
991
992fail:
993	if (bp != NULL)
994		aac_enqueue_bio(sc, bp);
995	if (cm != NULL)
996		aac_release_command(cm);
997	return(ENOMEM);
998}
999
1000/*
1001 * Handle a bio-instigated command that has been completed.
1002 */
1003static void
1004aac_bio_complete(struct aac_command *cm)
1005{
1006	struct aac_blockread_response *brr;
1007	struct aac_blockwrite_response *bwr;
1008	struct bio *bp;
1009	AAC_FSAStatus status;
1010
1011	/* fetch relevant status and then release the command */
1012	bp = (struct bio *)cm->cm_private;
1013	if (bp->bio_cmd == BIO_READ) {
1014		brr = (struct aac_blockread_response *)&cm->cm_fib->data[0];
1015		status = brr->Status;
1016	} else {
1017		bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0];
1018		status = bwr->Status;
1019	}
1020	aac_release_command(cm);
1021
1022	/* fix up the bio based on status */
1023	if (status == ST_OK) {
1024		bp->bio_resid = 0;
1025	} else {
1026		bp->bio_error = EIO;
1027		bp->bio_flags |= BIO_ERROR;
1028		/* pass an error string out to the disk layer */
1029		bp->bio_driver1 = aac_describe_code(aac_command_status_table,
1030						    status);
1031	}
1032	aac_biodone(bp);
1033}
1034
1035/*
1036 * Submit a command to the controller, return when it completes.
1037 * XXX This is very dangerous!  If the card has gone out to lunch, we could
1038 *     be stuck here forever.  At the same time, signals are not caught
1039 *     because there is a risk that a signal could wakeup the tsleep before
1040 *     the card has a chance to complete the command.  The passed in timeout
1041 *     is ignored for the same reason.  Since there is no way to cancel a
1042 *     command in progress, we should probably create a 'dead' queue where
1043 *     commands go that have been interrupted/timed-out/etc, that keeps them
1044 *     out of the free pool.  That way, if the card is just slow, it won't
1045 *     spam the memory of a command that has been recycled.
1046 */
1047static int
1048aac_wait_command(struct aac_command *cm, int timeout)
1049{
1050	struct aac_softc *sc;
1051	int error = 0;
1052
1053	debug_called(2);
1054
1055	sc = cm->cm_sc;
1056
1057	/* Put the command on the ready queue and get things going */
1058	cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1059	aac_enqueue_ready(cm);
1060	aac_startio(sc);
1061	while (!(cm->cm_flags & AAC_CMD_COMPLETED) && (error != EWOULDBLOCK)) {
1062		error = msleep(cm, &sc->aac_io_lock, PRIBIO, "aacwait", 0);
1063	}
1064	return(error);
1065}
1066
1067/*
1068 *Command Buffer Management
1069 */
1070
1071/*
1072 * Allocate a command.
1073 */
1074int
1075aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
1076{
1077	struct aac_command *cm;
1078
1079	debug_called(3);
1080
1081	if ((cm = aac_dequeue_free(sc)) == NULL) {
1082		if (sc->total_fibs < sc->aac_max_fibs) {
1083			sc->aifflags |= AAC_AIFFLAGS_ALLOCFIBS;
1084			wakeup(sc->aifthread);
1085		}
1086		return (EBUSY);
1087	}
1088
1089	*cmp = cm;
1090	return(0);
1091}
1092
1093/*
1094 * Release a command back to the freelist.
1095 */
1096void
1097aac_release_command(struct aac_command *cm)
1098{
1099	debug_called(3);
1100
1101	/* (re)initialise the command/FIB */
1102	cm->cm_sgtable = NULL;
1103	cm->cm_flags = 0;
1104	cm->cm_complete = NULL;
1105	cm->cm_private = NULL;
1106	cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
1107	cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
1108	cm->cm_fib->Header.Flags = 0;
1109	cm->cm_fib->Header.SenderSize = sizeof(struct aac_fib);
1110
1111	/*
1112	 * These are duplicated in aac_start to cover the case where an
1113	 * intermediate stage may have destroyed them.  They're left
1114	 * initialised here for debugging purposes only.
1115	 */
1116	cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1117	cm->cm_fib->Header.SenderData = 0;
1118
1119	aac_enqueue_free(cm);
1120}
1121
1122/*
1123 * Map helper for command/FIB allocation.
1124 */
1125static void
1126aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1127{
1128	uint32_t	*fibphys;
1129
1130	fibphys = (uint32_t *)arg;
1131
1132	debug_called(3);
1133
1134	*fibphys = segs[0].ds_addr;
1135}
1136
1137/*
1138 * Allocate and initialise commands/FIBs for this adapter.
1139 */
1140static int
1141aac_alloc_commands(struct aac_softc *sc)
1142{
1143	struct aac_command *cm;
1144	struct aac_fibmap *fm;
1145	uint32_t fibphys;
1146	int i, error;
1147
1148	debug_called(2);
1149
1150	if (sc->total_fibs + AAC_FIB_COUNT > sc->aac_max_fibs)
1151		return (ENOMEM);
1152
1153	fm = malloc(sizeof(struct aac_fibmap), M_AACBUF, M_NOWAIT|M_ZERO);
1154	if (fm == NULL)
1155		return (ENOMEM);
1156
1157	/* allocate the FIBs in DMAable memory and load them */
1158	if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&fm->aac_fibs,
1159			     BUS_DMA_NOWAIT, &fm->aac_fibmap)) {
1160		device_printf(sc->aac_dev,
1161			      "Not enough contiguous memory available.\n");
1162		free(fm, M_AACBUF);
1163		return (ENOMEM);
1164	}
1165
1166	/* Ignore errors since this doesn't bounce */
1167	(void)bus_dmamap_load(sc->aac_fib_dmat, fm->aac_fibmap, fm->aac_fibs,
1168			      AAC_FIB_COUNT * sizeof(struct aac_fib),
1169			      aac_map_command_helper, &fibphys, 0);
1170
1171	/* initialise constant fields in the command structure */
1172	bzero(fm->aac_fibs, AAC_FIB_COUNT * sizeof(struct aac_fib));
1173	for (i = 0; i < AAC_FIB_COUNT; i++) {
1174		cm = sc->aac_commands + sc->total_fibs;
1175		fm->aac_commands = cm;
1176		cm->cm_sc = sc;
1177		cm->cm_fib = fm->aac_fibs + i;
1178		cm->cm_fibphys = fibphys + (i * sizeof(struct aac_fib));
1179		cm->cm_index = sc->total_fibs;
1180
1181		if ((error = bus_dmamap_create(sc->aac_buffer_dmat, 0,
1182					       &cm->cm_datamap)) == 0)
1183			aac_release_command(cm);
1184		else
1185			break;
1186		sc->total_fibs++;
1187	}
1188
1189	if (i > 0) {
1190		TAILQ_INSERT_TAIL(&sc->aac_fibmap_tqh, fm, fm_link);
1191		debug(1, "total_fibs= %d\n", sc->total_fibs);
1192		return (0);
1193	}
1194
1195	bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1196	bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1197	free(fm, M_AACBUF);
1198	return (ENOMEM);
1199}
1200
1201/*
1202 * Free FIBs owned by this adapter.
1203 */
1204static void
1205aac_free_commands(struct aac_softc *sc)
1206{
1207	struct aac_fibmap *fm;
1208	struct aac_command *cm;
1209	int i;
1210
1211	debug_called(1);
1212
1213	while ((fm = TAILQ_FIRST(&sc->aac_fibmap_tqh)) != NULL) {
1214
1215		TAILQ_REMOVE(&sc->aac_fibmap_tqh, fm, fm_link);
1216		/*
1217		 * We check against total_fibs to handle partially
1218		 * allocated blocks.
1219		 */
1220		for (i = 0; i < AAC_FIB_COUNT && sc->total_fibs--; i++) {
1221			cm = fm->aac_commands + i;
1222			bus_dmamap_destroy(sc->aac_buffer_dmat, cm->cm_datamap);
1223		}
1224		bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1225		bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1226		free(fm, M_AACBUF);
1227	}
1228}
1229
1230/*
1231 * Command-mapping helper function - populate this command's s/g table.
1232 */
1233static void
1234aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1235{
1236	struct aac_softc *sc;
1237	struct aac_command *cm;
1238	struct aac_fib *fib;
1239	int i;
1240
1241	debug_called(3);
1242
1243	cm = (struct aac_command *)arg;
1244	sc = cm->cm_sc;
1245	fib = cm->cm_fib;
1246
1247	/* copy into the FIB */
1248	if (cm->cm_sgtable != NULL) {
1249		if ((cm->cm_sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1250			struct aac_sg_table *sg;
1251			sg = cm->cm_sgtable;
1252			sg->SgCount = nseg;
1253			for (i = 0; i < nseg; i++) {
1254				sg->SgEntry[i].SgAddress = segs[i].ds_addr;
1255				sg->SgEntry[i].SgByteCount = segs[i].ds_len;
1256			}
1257			/* update the FIB size for the s/g count */
1258			fib->Header.Size += nseg * sizeof(struct aac_sg_entry);
1259		} else {
1260			struct aac_sg_table64 *sg;
1261			sg = (struct aac_sg_table64 *)cm->cm_sgtable;
1262			sg->SgCount = nseg;
1263			for (i = 0; i < nseg; i++) {
1264				sg->SgEntry64[i].SgAddress = segs[i].ds_addr;
1265				sg->SgEntry64[i].SgByteCount = segs[i].ds_len;
1266			}
1267			/* update the FIB size for the s/g count */
1268			fib->Header.Size += nseg*sizeof(struct aac_sg_entry64);
1269		}
1270	}
1271
1272	/* Fix up the address values in the FIB.  Use the command array index
1273	 * instead of a pointer since these fields are only 32 bits.  Shift
1274	 * the SenderFibAddress over to make room for the fast response bit.
1275	 */
1276	cm->cm_fib->Header.SenderFibAddress = (cm->cm_index << 1);
1277	cm->cm_fib->Header.ReceiverFibAddress = cm->cm_fibphys;
1278
1279	/* save a pointer to the command for speedy reverse-lookup */
1280	cm->cm_fib->Header.SenderData = cm->cm_index;
1281
1282	if (cm->cm_flags & AAC_CMD_DATAIN)
1283		bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1284				BUS_DMASYNC_PREREAD);
1285	if (cm->cm_flags & AAC_CMD_DATAOUT)
1286		bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1287				BUS_DMASYNC_PREWRITE);
1288	cm->cm_flags |= AAC_CMD_MAPPED;
1289
1290	/* put the FIB on the outbound queue */
1291	if (aac_enqueue_fib(sc, cm->cm_queue, cm) == EBUSY)
1292		aac_requeue_ready(cm);
1293
1294	return;
1295}
1296
1297/*
1298 * Unmap a command from controller-visible space.
1299 */
1300static void
1301aac_unmap_command(struct aac_command *cm)
1302{
1303	struct aac_softc *sc;
1304
1305	debug_called(2);
1306
1307	sc = cm->cm_sc;
1308
1309	if (!(cm->cm_flags & AAC_CMD_MAPPED))
1310		return;
1311
1312	if (cm->cm_datalen != 0) {
1313		if (cm->cm_flags & AAC_CMD_DATAIN)
1314			bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1315					BUS_DMASYNC_POSTREAD);
1316		if (cm->cm_flags & AAC_CMD_DATAOUT)
1317			bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1318					BUS_DMASYNC_POSTWRITE);
1319
1320		bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap);
1321	}
1322	cm->cm_flags &= ~AAC_CMD_MAPPED;
1323}
1324
1325/*
1326 * Hardware Interface
1327 */
1328
1329/*
1330 * Initialise the adapter.
1331 */
1332static void
1333aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1334{
1335	struct aac_softc *sc;
1336
1337	debug_called(1);
1338
1339	sc = (struct aac_softc *)arg;
1340
1341	sc->aac_common_busaddr = segs[0].ds_addr;
1342}
1343
1344static int
1345aac_check_firmware(struct aac_softc *sc)
1346{
1347	u_int32_t major, minor, options;
1348
1349	debug_called(1);
1350
1351	/*
1352	 * Retrieve the firmware version numbers.  Dell PERC2/QC cards with
1353	 * firmware version 1.x are not compatible with this driver.
1354	 */
1355	if (sc->flags & AAC_FLAGS_PERC2QC) {
1356		if (aac_sync_command(sc, AAC_MONKER_GETKERNVER, 0, 0, 0, 0,
1357				     NULL)) {
1358			device_printf(sc->aac_dev,
1359				      "Error reading firmware version\n");
1360			return (EIO);
1361		}
1362
1363		/* These numbers are stored as ASCII! */
1364		major = (AAC_GET_MAILBOX(sc, 1) & 0xff) - 0x30;
1365		minor = (AAC_GET_MAILBOX(sc, 2) & 0xff) - 0x30;
1366		if (major == 1) {
1367			device_printf(sc->aac_dev,
1368			    "Firmware version %d.%d is not supported.\n",
1369			    major, minor);
1370			return (EINVAL);
1371		}
1372	}
1373
1374	/*
1375	 * Retrieve the capabilities/supported options word so we know what
1376	 * work-arounds to enable.
1377	 */
1378	if (aac_sync_command(sc, AAC_MONKER_GETINFO, 0, 0, 0, 0, NULL)) {
1379		device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
1380		return (EIO);
1381	}
1382	options = AAC_GET_MAILBOX(sc, 1);
1383	sc->supported_options = options;
1384
1385	if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 &&
1386	    (sc->flags & AAC_FLAGS_NO4GB) == 0)
1387		sc->flags |= AAC_FLAGS_4GB_WINDOW;
1388	if (options & AAC_SUPPORTED_NONDASD)
1389		sc->flags |= AAC_FLAGS_ENABLE_CAM;
1390	if ((options & AAC_SUPPORTED_SGMAP_HOST64) != 0
1391	     && (sizeof(bus_addr_t) > 4)) {
1392		device_printf(sc->aac_dev, "Enabling 64-bit address support\n");
1393		sc->flags |= AAC_FLAGS_SG_64BIT;
1394	}
1395
1396	/* Check for broken hardware that does a lower number of commands */
1397	if ((sc->flags & AAC_FLAGS_256FIBS) == 0)
1398		sc->aac_max_fibs = AAC_MAX_FIBS;
1399	else
1400		sc->aac_max_fibs = 256;
1401
1402	return (0);
1403}
1404
1405static int
1406aac_init(struct aac_softc *sc)
1407{
1408	struct aac_adapter_init	*ip;
1409	time_t then;
1410	u_int32_t code, qoffset;
1411	int error;
1412
1413	debug_called(1);
1414
1415	/*
1416	 * First wait for the adapter to come ready.
1417	 */
1418	then = time_second;
1419	do {
1420		code = AAC_GET_FWSTATUS(sc);
1421		if (code & AAC_SELF_TEST_FAILED) {
1422			device_printf(sc->aac_dev, "FATAL: selftest failed\n");
1423			return(ENXIO);
1424		}
1425		if (code & AAC_KERNEL_PANIC) {
1426			device_printf(sc->aac_dev,
1427				      "FATAL: controller kernel panic\n");
1428			return(ENXIO);
1429		}
1430		if (time_second > (then + AAC_BOOT_TIMEOUT)) {
1431			device_printf(sc->aac_dev,
1432				      "FATAL: controller not coming ready, "
1433					   "status %x\n", code);
1434			return(ENXIO);
1435		}
1436	} while (!(code & AAC_UP_AND_RUNNING));
1437
1438	error = ENOMEM;
1439	/*
1440	 * Create DMA tag for mapping buffers into controller-addressable space.
1441	 */
1442	if (bus_dma_tag_create(sc->aac_parent_dmat, 	/* parent */
1443			       1, 0, 			/* algnmnt, boundary */
1444			       (sc->flags & AAC_FLAGS_SG_64BIT) ?
1445			       BUS_SPACE_MAXADDR :
1446			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1447			       BUS_SPACE_MAXADDR, 	/* highaddr */
1448			       NULL, NULL, 		/* filter, filterarg */
1449			       MAXBSIZE,		/* maxsize */
1450			       AAC_MAXSGENTRIES,	/* nsegments */
1451			       MAXBSIZE,		/* maxsegsize */
1452			       BUS_DMA_ALLOCNOW,	/* flags */
1453			       busdma_lock_mutex,	/* lockfunc */
1454			       &sc->aac_io_lock,	/* lockfuncarg */
1455			       &sc->aac_buffer_dmat)) {
1456		device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
1457		goto out;
1458	}
1459
1460	/*
1461	 * Create DMA tag for mapping FIBs into controller-addressable space..
1462	 */
1463	if (bus_dma_tag_create(sc->aac_parent_dmat,	/* parent */
1464			       1, 0, 			/* algnmnt, boundary */
1465			       (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
1466			       BUS_SPACE_MAXADDR_32BIT :
1467			       0x7fffffff,		/* lowaddr */
1468			       BUS_SPACE_MAXADDR, 	/* highaddr */
1469			       NULL, NULL, 		/* filter, filterarg */
1470			       AAC_FIB_COUNT *
1471			       sizeof(struct aac_fib),  /* maxsize */
1472			       1,			/* nsegments */
1473			       AAC_FIB_COUNT *
1474			       sizeof(struct aac_fib),	/* maxsegsize */
1475			       BUS_DMA_ALLOCNOW,	/* flags */
1476			       NULL, NULL,		/* No locking needed */
1477			       &sc->aac_fib_dmat)) {
1478		device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");;
1479		goto out;
1480	}
1481
1482	/*
1483	 * Create DMA tag for the common structure and allocate it.
1484	 */
1485	if (bus_dma_tag_create(sc->aac_parent_dmat, 	/* parent */
1486			       1, 0,			/* algnmnt, boundary */
1487			       (sc->flags & AAC_FLAGS_4GB_WINDOW) ?
1488			       BUS_SPACE_MAXADDR_32BIT :
1489			       0x7fffffff,		/* lowaddr */
1490			       BUS_SPACE_MAXADDR, 	/* highaddr */
1491			       NULL, NULL, 		/* filter, filterarg */
1492			       8192 + sizeof(struct aac_common), /* maxsize */
1493			       1,			/* nsegments */
1494			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1495			       BUS_DMA_ALLOCNOW,	/* flags */
1496			       NULL, NULL,		/* No locking needed */
1497			       &sc->aac_common_dmat)) {
1498		device_printf(sc->aac_dev,
1499			      "can't allocate common structure DMA tag\n");
1500		goto out;
1501	}
1502	if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common,
1503			     BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) {
1504		device_printf(sc->aac_dev, "can't allocate common structure\n");
1505		goto out;
1506	}
1507
1508	/*
1509	 * Work around a bug in the 2120 and 2200 that cannot DMA commands
1510	 * below address 8192 in physical memory.
1511	 * XXX If the padding is not needed, can it be put to use instead
1512	 * of ignored?
1513	 */
1514	(void)bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap,
1515			sc->aac_common, 8192 + sizeof(*sc->aac_common),
1516			aac_common_map, sc, 0);
1517
1518	if (sc->aac_common_busaddr < 8192) {
1519		(uint8_t *)sc->aac_common += 8192;
1520		sc->aac_common_busaddr += 8192;
1521	}
1522	bzero(sc->aac_common, sizeof(*sc->aac_common));
1523
1524	/* Allocate some FIBs and associated command structs */
1525	TAILQ_INIT(&sc->aac_fibmap_tqh);
1526	sc->aac_commands = malloc(AAC_MAX_FIBS * sizeof(struct aac_command),
1527				  M_AACBUF, M_WAITOK|M_ZERO);
1528	while (sc->total_fibs < AAC_PREALLOCATE_FIBS) {
1529		if (aac_alloc_commands(sc) != 0)
1530			break;
1531	}
1532	if (sc->total_fibs == 0)
1533		goto out;
1534
1535	/*
1536	 * Fill in the init structure.  This tells the adapter about the
1537	 * physical location of various important shared data structures.
1538	 */
1539	ip = &sc->aac_common->ac_init;
1540	ip->InitStructRevision = AAC_INIT_STRUCT_REVISION;
1541	ip->MiniPortRevision = AAC_INIT_STRUCT_MINIPORT_REVISION;
1542
1543	ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr +
1544					 offsetof(struct aac_common, ac_fibs);
1545	ip->AdapterFibsVirtualAddress = 0;
1546	ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib);
1547	ip->AdapterFibAlign = sizeof(struct aac_fib);
1548
1549	ip->PrintfBufferAddress = sc->aac_common_busaddr +
1550				  offsetof(struct aac_common, ac_printf);
1551	ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE;
1552
1553	/*
1554	 * The adapter assumes that pages are 4K in size, except on some
1555 	 * broken firmware versions that do the page->byte conversion twice,
1556	 * therefore 'assuming' that this value is in 16MB units (2^24).
1557	 * Round up since the granularity is so high.
1558	 */
1559	ip->HostPhysMemPages = ctob(physmem) / AAC_PAGE_SIZE;
1560	if (sc->flags & AAC_FLAGS_BROKEN_MEMMAP) {
1561		ip->HostPhysMemPages =
1562		    (ip->HostPhysMemPages + AAC_PAGE_SIZE) / AAC_PAGE_SIZE;
1563	}
1564	ip->HostElapsedSeconds = time_second;	/* reset later if invalid */
1565
1566	/*
1567	 * Initialise FIB queues.  Note that it appears that the layout of the
1568	 * indexes and the segmentation of the entries may be mandated by the
1569	 * adapter, which is only told about the base of the queue index fields.
1570	 *
1571	 * The initial values of the indices are assumed to inform the adapter
1572	 * of the sizes of the respective queues, and theoretically it could
1573	 * work out the entire layout of the queue structures from this.  We
1574	 * take the easy route and just lay this area out like everyone else
1575	 * does.
1576	 *
1577	 * The Linux driver uses a much more complex scheme whereby several
1578	 * header records are kept for each queue.  We use a couple of generic
1579	 * list manipulation functions which 'know' the size of each list by
1580	 * virtue of a table.
1581	 */
1582	qoffset = offsetof(struct aac_common, ac_qbuf) + AAC_QUEUE_ALIGN;
1583	qoffset &= ~(AAC_QUEUE_ALIGN - 1);
1584	sc->aac_queues =
1585	    (struct aac_queue_table *)((uintptr_t)sc->aac_common + qoffset);
1586	ip->CommHeaderAddress = sc->aac_common_busaddr + qoffset;
1587
1588	sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1589		AAC_HOST_NORM_CMD_ENTRIES;
1590	sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1591		AAC_HOST_NORM_CMD_ENTRIES;
1592	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1593		AAC_HOST_HIGH_CMD_ENTRIES;
1594	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1595		AAC_HOST_HIGH_CMD_ENTRIES;
1596	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1597		AAC_ADAP_NORM_CMD_ENTRIES;
1598	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1599		AAC_ADAP_NORM_CMD_ENTRIES;
1600	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1601		AAC_ADAP_HIGH_CMD_ENTRIES;
1602	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1603		AAC_ADAP_HIGH_CMD_ENTRIES;
1604	sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1605		AAC_HOST_NORM_RESP_ENTRIES;
1606	sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1607		AAC_HOST_NORM_RESP_ENTRIES;
1608	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1609		AAC_HOST_HIGH_RESP_ENTRIES;
1610	sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1611		AAC_HOST_HIGH_RESP_ENTRIES;
1612	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1613		AAC_ADAP_NORM_RESP_ENTRIES;
1614	sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1615		AAC_ADAP_NORM_RESP_ENTRIES;
1616	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX]=
1617		AAC_ADAP_HIGH_RESP_ENTRIES;
1618	sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX]=
1619		AAC_ADAP_HIGH_RESP_ENTRIES;
1620	sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] =
1621		&sc->aac_queues->qt_HostNormCmdQueue[0];
1622	sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] =
1623		&sc->aac_queues->qt_HostHighCmdQueue[0];
1624	sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] =
1625		&sc->aac_queues->qt_AdapNormCmdQueue[0];
1626	sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] =
1627		&sc->aac_queues->qt_AdapHighCmdQueue[0];
1628	sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] =
1629		&sc->aac_queues->qt_HostNormRespQueue[0];
1630	sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] =
1631		&sc->aac_queues->qt_HostHighRespQueue[0];
1632	sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] =
1633		&sc->aac_queues->qt_AdapNormRespQueue[0];
1634	sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] =
1635		&sc->aac_queues->qt_AdapHighRespQueue[0];
1636
1637	/*
1638	 * Do controller-type-specific initialisation
1639	 */
1640	switch (sc->aac_hwif) {
1641	case AAC_HWIF_I960RX:
1642		AAC_SETREG4(sc, AAC_RX_ODBR, ~0);
1643		break;
1644	}
1645
1646	/*
1647	 * Give the init structure to the controller.
1648	 */
1649	if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT,
1650			     sc->aac_common_busaddr +
1651			     offsetof(struct aac_common, ac_init), 0, 0, 0,
1652			     NULL)) {
1653		device_printf(sc->aac_dev,
1654			      "error establishing init structure\n");
1655		error = EIO;
1656		goto out;
1657	}
1658
1659	error = 0;
1660out:
1661	return(error);
1662}
1663
1664/*
1665 * Send a synchronous command to the controller and wait for a result.
1666 */
1667static int
1668aac_sync_command(struct aac_softc *sc, u_int32_t command,
1669		 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3,
1670		 u_int32_t *sp)
1671{
1672	time_t then;
1673	u_int32_t status;
1674
1675	debug_called(3);
1676
1677	/* populate the mailbox */
1678	AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3);
1679
1680	/* ensure the sync command doorbell flag is cleared */
1681	AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
1682
1683	/* then set it to signal the adapter */
1684	AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND);
1685
1686	/* spin waiting for the command to complete */
1687	then = time_second;
1688	do {
1689		if (time_second > (then + AAC_IMMEDIATE_TIMEOUT)) {
1690			debug(1, "timed out");
1691			return(EIO);
1692		}
1693	} while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND));
1694
1695	/* clear the completion flag */
1696	AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
1697
1698	/* get the command status */
1699	status = AAC_GET_MAILBOX(sc, 0);
1700	if (sp != NULL)
1701		*sp = status;
1702	return(0);
1703}
1704
1705/*
1706 * Grab the sync fib area.
1707 */
1708int
1709aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib, int flags)
1710{
1711
1712	/*
1713	 * If the force flag is set, the system is shutting down, or in
1714	 * trouble.  Ignore the mutex.
1715	 */
1716	if (!(flags & AAC_SYNC_LOCK_FORCE))
1717		AAC_LOCK_ACQUIRE(&sc->aac_sync_lock);
1718
1719	*fib = &sc->aac_common->ac_sync_fib;
1720
1721	return (1);
1722}
1723
1724/*
1725 * Release the sync fib area.
1726 */
1727void
1728aac_release_sync_fib(struct aac_softc *sc)
1729{
1730
1731	AAC_LOCK_RELEASE(&sc->aac_sync_lock);
1732}
1733
1734/*
1735 * Send a synchronous FIB to the controller and wait for a result.
1736 */
1737int
1738aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate,
1739		 struct aac_fib *fib, u_int16_t datasize)
1740{
1741	debug_called(3);
1742
1743	if (datasize > AAC_FIB_DATASIZE)
1744		return(EINVAL);
1745
1746	/*
1747	 * Set up the sync FIB
1748	 */
1749	fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED |
1750				AAC_FIBSTATE_INITIALISED |
1751				AAC_FIBSTATE_EMPTY;
1752	fib->Header.XferState |= xferstate;
1753	fib->Header.Command = command;
1754	fib->Header.StructType = AAC_FIBTYPE_TFIB;
1755	fib->Header.Size = sizeof(struct aac_fib) + datasize;
1756	fib->Header.SenderSize = sizeof(struct aac_fib);
1757	fib->Header.SenderFibAddress = 0;	/* Not needed */
1758	fib->Header.ReceiverFibAddress = sc->aac_common_busaddr +
1759					 offsetof(struct aac_common,
1760						  ac_sync_fib);
1761
1762	/*
1763	 * Give the FIB to the controller, wait for a response.
1764	 */
1765	if (aac_sync_command(sc, AAC_MONKER_SYNCFIB,
1766			     fib->Header.ReceiverFibAddress, 0, 0, 0, NULL)) {
1767		debug(2, "IO error");
1768		return(EIO);
1769	}
1770
1771	return (0);
1772}
1773
1774/*
1775 * Adapter-space FIB queue manipulation
1776 *
1777 * Note that the queue implementation here is a little funky; neither the PI or
1778 * CI will ever be zero.  This behaviour is a controller feature.
1779 */
1780static struct {
1781	int		size;
1782	int		notify;
1783} aac_qinfo[] = {
1784	{AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL},
1785	{AAC_HOST_HIGH_CMD_ENTRIES, 0},
1786	{AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY},
1787	{AAC_ADAP_HIGH_CMD_ENTRIES, 0},
1788	{AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL},
1789	{AAC_HOST_HIGH_RESP_ENTRIES, 0},
1790	{AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY},
1791	{AAC_ADAP_HIGH_RESP_ENTRIES, 0}
1792};
1793
1794/*
1795 * Atomically insert an entry into the nominated queue, returns 0 on success or
1796 * EBUSY if the queue is full.
1797 *
1798 * Note: it would be more efficient to defer notifying the controller in
1799 *	 the case where we may be inserting several entries in rapid succession,
1800 *	 but implementing this usefully may be difficult (it would involve a
1801 *	 separate queue/notify interface).
1802 */
1803static int
1804aac_enqueue_fib(struct aac_softc *sc, int queue, struct aac_command *cm)
1805{
1806	u_int32_t pi, ci;
1807	int error;
1808	u_int32_t fib_size;
1809	u_int32_t fib_addr;
1810
1811	debug_called(3);
1812
1813	fib_size = cm->cm_fib->Header.Size;
1814	fib_addr = cm->cm_fib->Header.ReceiverFibAddress;
1815
1816	/* get the producer/consumer indices */
1817	pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1818	ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1819
1820	/* wrap the queue? */
1821	if (pi >= aac_qinfo[queue].size)
1822		pi = 0;
1823
1824	/* check for queue full */
1825	if ((pi + 1) == ci) {
1826		error = EBUSY;
1827		goto out;
1828	}
1829
1830	/* populate queue entry */
1831	(sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
1832	(sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
1833
1834	/* update producer index */
1835	sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
1836
1837	/*
1838	 * To avoid a race with its completion interrupt, place this command on
1839	 * the busy queue prior to advertising it to the controller.
1840	 */
1841	aac_enqueue_busy(cm);
1842
1843	/* notify the adapter if we know how */
1844	if (aac_qinfo[queue].notify != 0)
1845		AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1846
1847	error = 0;
1848
1849out:
1850	return(error);
1851}
1852
1853/*
1854 * Atomically remove one entry from the nominated queue, returns 0 on
1855 * success or ENOENT if the queue is empty.
1856 */
1857static int
1858aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size,
1859		struct aac_fib **fib_addr)
1860{
1861	u_int32_t pi, ci;
1862	u_int32_t fib_index;
1863	int error;
1864	int notify;
1865
1866	debug_called(3);
1867
1868	/* get the producer/consumer indices */
1869	pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1870	ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1871
1872	/* check for queue empty */
1873	if (ci == pi) {
1874		error = ENOENT;
1875		goto out;
1876	}
1877
1878	/* wrap the pi so the following test works */
1879	if (pi >= aac_qinfo[queue].size)
1880		pi = 0;
1881
1882	notify = 0;
1883	if (ci == pi + 1)
1884		notify++;
1885
1886	/* wrap the queue? */
1887	if (ci >= aac_qinfo[queue].size)
1888		ci = 0;
1889
1890	/* fetch the entry */
1891	*fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size;
1892
1893	switch (queue) {
1894	case AAC_HOST_NORM_CMD_QUEUE:
1895	case AAC_HOST_HIGH_CMD_QUEUE:
1896		/*
1897		 * The aq_fib_addr is only 32 bits wide so it can't be counted
1898		 * on to hold an address.  For AIF's, the adapter assumes
1899		 * that it's giving us an address into the array of AIF fibs.
1900		 * Therefore, we have to convert it to an index.
1901		 */
1902		fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr /
1903			sizeof(struct aac_fib);
1904		*fib_addr = &sc->aac_common->ac_fibs[fib_index];
1905		break;
1906
1907	case AAC_HOST_NORM_RESP_QUEUE:
1908	case AAC_HOST_HIGH_RESP_QUEUE:
1909	{
1910		struct aac_command *cm;
1911
1912		/*
1913		 * As above, an index is used instead of an actual address.
1914		 * Gotta shift the index to account for the fast response
1915		 * bit.  No other correction is needed since this value was
1916		 * originally provided by the driver via the SenderFibAddress
1917		 * field.
1918		 */
1919		fib_index = (sc->aac_qentries[queue] + ci)->aq_fib_addr;
1920		cm = sc->aac_commands + (fib_index >> 1);
1921		*fib_addr = cm->cm_fib;
1922
1923		/*
1924		 * Is this a fast response? If it is, update the fib fields in
1925		 * local memory since the whole fib isn't DMA'd back up.
1926		 */
1927		if (fib_index & 0x01) {
1928			(*fib_addr)->Header.XferState |= AAC_FIBSTATE_DONEADAP;
1929			*((u_int32_t*)((*fib_addr)->data)) = AAC_ERROR_NORMAL;
1930		}
1931		break;
1932	}
1933	default:
1934		panic("Invalid queue in aac_dequeue_fib()");
1935		break;
1936	}
1937
1938	/* update consumer index */
1939	sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1;
1940
1941	/* if we have made the queue un-full, notify the adapter */
1942	if (notify && (aac_qinfo[queue].notify != 0))
1943		AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1944	error = 0;
1945
1946out:
1947	return(error);
1948}
1949
1950/*
1951 * Put our response to an Adapter Initialed Fib on the response queue
1952 */
1953static int
1954aac_enqueue_response(struct aac_softc *sc, int queue, struct aac_fib *fib)
1955{
1956	u_int32_t pi, ci;
1957	int error;
1958	u_int32_t fib_size;
1959	u_int32_t fib_addr;
1960
1961	debug_called(1);
1962
1963	/* Tell the adapter where the FIB is */
1964	fib_size = fib->Header.Size;
1965	fib_addr = fib->Header.SenderFibAddress;
1966	fib->Header.ReceiverFibAddress = fib_addr;
1967
1968	/* get the producer/consumer indices */
1969	pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1970	ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1971
1972	/* wrap the queue? */
1973	if (pi >= aac_qinfo[queue].size)
1974		pi = 0;
1975
1976	/* check for queue full */
1977	if ((pi + 1) == ci) {
1978		error = EBUSY;
1979		goto out;
1980	}
1981
1982	/* populate queue entry */
1983	(sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
1984	(sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
1985
1986	/* update producer index */
1987	sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
1988
1989	/* notify the adapter if we know how */
1990	if (aac_qinfo[queue].notify != 0)
1991		AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1992
1993	error = 0;
1994
1995out:
1996	return(error);
1997}
1998
1999/*
2000 * Check for commands that have been outstanding for a suspiciously long time,
2001 * and complain about them.
2002 */
2003static void
2004aac_timeout(struct aac_softc *sc)
2005{
2006	struct aac_command *cm;
2007	time_t deadline;
2008
2009	/*
2010	 * Traverse the busy command list, bitch about late commands once
2011	 * only.
2012	 */
2013	deadline = time_second - AAC_CMD_TIMEOUT;
2014	TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) {
2015		if ((cm->cm_timestamp  < deadline)
2016			/* && !(cm->cm_flags & AAC_CMD_TIMEDOUT) */) {
2017			cm->cm_flags |= AAC_CMD_TIMEDOUT;
2018			device_printf(sc->aac_dev,
2019				      "COMMAND %p TIMEOUT AFTER %d SECONDS\n",
2020				      cm, (int)(time_second-cm->cm_timestamp));
2021			AAC_PRINT_FIB(sc, cm->cm_fib);
2022		}
2023	}
2024
2025	return;
2026}
2027
2028/*
2029 * Interface Function Vectors
2030 */
2031
2032/*
2033 * Read the current firmware status word.
2034 */
2035static int
2036aac_sa_get_fwstatus(struct aac_softc *sc)
2037{
2038	debug_called(3);
2039
2040	return(AAC_GETREG4(sc, AAC_SA_FWSTATUS));
2041}
2042
2043static int
2044aac_rx_get_fwstatus(struct aac_softc *sc)
2045{
2046	debug_called(3);
2047
2048	return(AAC_GETREG4(sc, AAC_RX_FWSTATUS));
2049}
2050
2051static int
2052aac_fa_get_fwstatus(struct aac_softc *sc)
2053{
2054	int val;
2055
2056	debug_called(3);
2057
2058	val = AAC_GETREG4(sc, AAC_FA_FWSTATUS);
2059	return (val);
2060}
2061
2062/*
2063 * Notify the controller of a change in a given queue
2064 */
2065
2066static void
2067aac_sa_qnotify(struct aac_softc *sc, int qbit)
2068{
2069	debug_called(3);
2070
2071	AAC_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit);
2072}
2073
2074static void
2075aac_rx_qnotify(struct aac_softc *sc, int qbit)
2076{
2077	debug_called(3);
2078
2079	AAC_SETREG4(sc, AAC_RX_IDBR, qbit);
2080}
2081
2082static void
2083aac_fa_qnotify(struct aac_softc *sc, int qbit)
2084{
2085	debug_called(3);
2086
2087	AAC_SETREG2(sc, AAC_FA_DOORBELL1, qbit);
2088	AAC_FA_HACK(sc);
2089}
2090
2091/*
2092 * Get the interrupt reason bits
2093 */
2094static int
2095aac_sa_get_istatus(struct aac_softc *sc)
2096{
2097	debug_called(3);
2098
2099	return(AAC_GETREG2(sc, AAC_SA_DOORBELL0));
2100}
2101
2102static int
2103aac_rx_get_istatus(struct aac_softc *sc)
2104{
2105	debug_called(3);
2106
2107	return(AAC_GETREG4(sc, AAC_RX_ODBR));
2108}
2109
2110static int
2111aac_fa_get_istatus(struct aac_softc *sc)
2112{
2113	int val;
2114
2115	debug_called(3);
2116
2117	val = AAC_GETREG2(sc, AAC_FA_DOORBELL0);
2118	return (val);
2119}
2120
2121/*
2122 * Clear some interrupt reason bits
2123 */
2124static void
2125aac_sa_clear_istatus(struct aac_softc *sc, int mask)
2126{
2127	debug_called(3);
2128
2129	AAC_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask);
2130}
2131
2132static void
2133aac_rx_clear_istatus(struct aac_softc *sc, int mask)
2134{
2135	debug_called(3);
2136
2137	AAC_SETREG4(sc, AAC_RX_ODBR, mask);
2138}
2139
2140static void
2141aac_fa_clear_istatus(struct aac_softc *sc, int mask)
2142{
2143	debug_called(3);
2144
2145	AAC_SETREG2(sc, AAC_FA_DOORBELL0_CLEAR, mask);
2146	AAC_FA_HACK(sc);
2147}
2148
2149/*
2150 * Populate the mailbox and set the command word
2151 */
2152static void
2153aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2154		u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2155{
2156	debug_called(4);
2157
2158	AAC_SETREG4(sc, AAC_SA_MAILBOX, command);
2159	AAC_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0);
2160	AAC_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1);
2161	AAC_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2);
2162	AAC_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3);
2163}
2164
2165static void
2166aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
2167		u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2168{
2169	debug_called(4);
2170
2171	AAC_SETREG4(sc, AAC_RX_MAILBOX, command);
2172	AAC_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0);
2173	AAC_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1);
2174	AAC_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2);
2175	AAC_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3);
2176}
2177
2178static void
2179aac_fa_set_mailbox(struct aac_softc *sc, u_int32_t command,
2180		u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
2181{
2182	debug_called(4);
2183
2184	AAC_SETREG4(sc, AAC_FA_MAILBOX, command);
2185	AAC_FA_HACK(sc);
2186	AAC_SETREG4(sc, AAC_FA_MAILBOX + 4, arg0);
2187	AAC_FA_HACK(sc);
2188	AAC_SETREG4(sc, AAC_FA_MAILBOX + 8, arg1);
2189	AAC_FA_HACK(sc);
2190	AAC_SETREG4(sc, AAC_FA_MAILBOX + 12, arg2);
2191	AAC_FA_HACK(sc);
2192	AAC_SETREG4(sc, AAC_FA_MAILBOX + 16, arg3);
2193	AAC_FA_HACK(sc);
2194}
2195
2196/*
2197 * Fetch the immediate command status word
2198 */
2199static int
2200aac_sa_get_mailbox(struct aac_softc *sc, int mb)
2201{
2202	debug_called(4);
2203
2204	return(AAC_GETREG4(sc, AAC_SA_MAILBOX + (mb * 4)));
2205}
2206
2207static int
2208aac_rx_get_mailbox(struct aac_softc *sc, int mb)
2209{
2210	debug_called(4);
2211
2212	return(AAC_GETREG4(sc, AAC_RX_MAILBOX + (mb * 4)));
2213}
2214
2215static int
2216aac_fa_get_mailbox(struct aac_softc *sc, int mb)
2217{
2218	int val;
2219
2220	debug_called(4);
2221
2222	val = AAC_GETREG4(sc, AAC_FA_MAILBOX + (mb * 4));
2223	return (val);
2224}
2225
2226/*
2227 * Set/clear interrupt masks
2228 */
2229static void
2230aac_sa_set_interrupts(struct aac_softc *sc, int enable)
2231{
2232	debug(2, "%sable interrupts", enable ? "en" : "dis");
2233
2234	if (enable) {
2235		AAC_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2236	} else {
2237		AAC_SETREG2((sc), AAC_SA_MASK0_SET, ~0);
2238	}
2239}
2240
2241static void
2242aac_rx_set_interrupts(struct aac_softc *sc, int enable)
2243{
2244	debug(2, "%sable interrupts", enable ? "en" : "dis");
2245
2246	if (enable) {
2247		AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS);
2248	} else {
2249		AAC_SETREG4(sc, AAC_RX_OIMR, ~0);
2250	}
2251}
2252
2253static void
2254aac_fa_set_interrupts(struct aac_softc *sc, int enable)
2255{
2256	debug(2, "%sable interrupts", enable ? "en" : "dis");
2257
2258	if (enable) {
2259		AAC_SETREG2((sc), AAC_FA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
2260		AAC_FA_HACK(sc);
2261	} else {
2262		AAC_SETREG2((sc), AAC_FA_MASK0, ~0);
2263		AAC_FA_HACK(sc);
2264	}
2265}
2266
2267/*
2268 * Debugging and Diagnostics
2269 */
2270
2271/*
2272 * Print some information about the controller.
2273 */
2274static void
2275aac_describe_controller(struct aac_softc *sc)
2276{
2277	struct aac_fib *fib;
2278	struct aac_adapter_info	*info;
2279
2280	debug_called(2);
2281
2282	aac_alloc_sync_fib(sc, &fib, 0);
2283
2284	fib->data[0] = 0;
2285	if (aac_sync_fib(sc, RequestAdapterInfo, 0, fib, 1)) {
2286		device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
2287		aac_release_sync_fib(sc);
2288		return;
2289	}
2290	info = (struct aac_adapter_info *)&fib->data[0];
2291
2292	device_printf(sc->aac_dev, "%s %dMHz, %dMB cache memory, %s\n",
2293		      aac_describe_code(aac_cpu_variant, info->CpuVariant),
2294		      info->ClockSpeed, info->BufferMem / (1024 * 1024),
2295		      aac_describe_code(aac_battery_platform,
2296					info->batteryPlatform));
2297
2298	/* save the kernel revision structure for later use */
2299	sc->aac_revision = info->KernelRevision;
2300	device_printf(sc->aac_dev, "Kernel %d.%d-%d, Build %d, S/N %6X\n",
2301		      info->KernelRevision.external.comp.major,
2302		      info->KernelRevision.external.comp.minor,
2303		      info->KernelRevision.external.comp.dash,
2304		      info->KernelRevision.buildNumber,
2305		      (u_int32_t)(info->SerialNumber & 0xffffff));
2306
2307	aac_release_sync_fib(sc);
2308
2309	if (1 || bootverbose) {
2310		device_printf(sc->aac_dev, "Supported Options=%b\n",
2311			      sc->supported_options,
2312			      "\20"
2313			      "\1SNAPSHOT"
2314			      "\2CLUSTERS"
2315			      "\3WCACHE"
2316			      "\4DATA64"
2317			      "\5HOSTTIME"
2318			      "\6RAID50"
2319			      "\7WINDOW4GB"
2320			      "\10SCSIUPGD"
2321			      "\11SOFTERR"
2322			      "\12NORECOND"
2323			      "\13SGMAP64"
2324			      "\14ALARM"
2325			      "\15NONDASD");
2326	}
2327}
2328
2329/*
2330 * Look up a text description of a numeric error code and return a pointer to
2331 * same.
2332 */
2333static char *
2334aac_describe_code(struct aac_code_lookup *table, u_int32_t code)
2335{
2336	int i;
2337
2338	for (i = 0; table[i].string != NULL; i++)
2339		if (table[i].code == code)
2340			return(table[i].string);
2341	return(table[i + 1].string);
2342}
2343
2344/*
2345 * Management Interface
2346 */
2347
2348static int
2349aac_open(dev_t dev, int flags, int fmt, d_thread_t *td)
2350{
2351	struct aac_softc *sc;
2352
2353	debug_called(2);
2354
2355	sc = dev->si_drv1;
2356
2357	/* Check to make sure the device isn't already open */
2358	if (sc->aac_state & AAC_STATE_OPEN) {
2359		return EBUSY;
2360	}
2361	sc->aac_state |= AAC_STATE_OPEN;
2362
2363	return 0;
2364}
2365
2366static int
2367aac_close(dev_t dev, int flags, int fmt, d_thread_t *td)
2368{
2369	struct aac_softc *sc;
2370
2371	debug_called(2);
2372
2373	sc = dev->si_drv1;
2374
2375	/* Mark this unit as no longer open  */
2376	sc->aac_state &= ~AAC_STATE_OPEN;
2377
2378	return 0;
2379}
2380
2381static int
2382aac_ioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, d_thread_t *td)
2383{
2384	union aac_statrequest *as;
2385	struct aac_softc *sc;
2386	int error = 0;
2387	uint32_t cookie;
2388
2389	debug_called(2);
2390
2391	as = (union aac_statrequest *)arg;
2392	sc = dev->si_drv1;
2393
2394	switch (cmd) {
2395	case AACIO_STATS:
2396		switch (as->as_item) {
2397		case AACQ_FREE:
2398		case AACQ_BIO:
2399		case AACQ_READY:
2400		case AACQ_BUSY:
2401		case AACQ_COMPLETE:
2402			bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
2403			      sizeof(struct aac_qstat));
2404			break;
2405		default:
2406			error = ENOENT;
2407			break;
2408		}
2409	break;
2410
2411	case FSACTL_SENDFIB:
2412		arg = *(caddr_t*)arg;
2413	case FSACTL_LNX_SENDFIB:
2414		debug(1, "FSACTL_SENDFIB");
2415		error = aac_ioctl_sendfib(sc, arg);
2416		break;
2417	case FSACTL_AIF_THREAD:
2418	case FSACTL_LNX_AIF_THREAD:
2419		debug(1, "FSACTL_AIF_THREAD");
2420		error = EINVAL;
2421		break;
2422	case FSACTL_OPEN_GET_ADAPTER_FIB:
2423		arg = *(caddr_t*)arg;
2424	case FSACTL_LNX_OPEN_GET_ADAPTER_FIB:
2425		debug(1, "FSACTL_OPEN_GET_ADAPTER_FIB");
2426		/*
2427		 * Pass the caller out an AdapterFibContext.
2428		 *
2429		 * Note that because we only support one opener, we
2430		 * basically ignore this.  Set the caller's context to a magic
2431		 * number just in case.
2432		 *
2433		 * The Linux code hands the driver a pointer into kernel space,
2434		 * and then trusts it when the caller hands it back.  Aiee!
2435		 * Here, we give it the proc pointer of the per-adapter aif
2436		 * thread. It's only used as a sanity check in other calls.
2437		 */
2438		cookie = (uint32_t)(uintptr_t)sc->aifthread;
2439		error = copyout(&cookie, arg, sizeof(cookie));
2440		break;
2441	case FSACTL_GET_NEXT_ADAPTER_FIB:
2442		arg = *(caddr_t*)arg;
2443	case FSACTL_LNX_GET_NEXT_ADAPTER_FIB:
2444		debug(1, "FSACTL_GET_NEXT_ADAPTER_FIB");
2445		error = aac_getnext_aif(sc, arg);
2446		break;
2447	case FSACTL_CLOSE_GET_ADAPTER_FIB:
2448	case FSACTL_LNX_CLOSE_GET_ADAPTER_FIB:
2449		debug(1, "FSACTL_CLOSE_GET_ADAPTER_FIB");
2450		/* don't do anything here */
2451		break;
2452	case FSACTL_MINIPORT_REV_CHECK:
2453		arg = *(caddr_t*)arg;
2454	case FSACTL_LNX_MINIPORT_REV_CHECK:
2455		debug(1, "FSACTL_MINIPORT_REV_CHECK");
2456		error = aac_rev_check(sc, arg);
2457		break;
2458	case FSACTL_QUERY_DISK:
2459		arg = *(caddr_t*)arg;
2460	case FSACTL_LNX_QUERY_DISK:
2461		debug(1, "FSACTL_QUERY_DISK");
2462		error = aac_query_disk(sc, arg);
2463			break;
2464	case FSACTL_DELETE_DISK:
2465	case FSACTL_LNX_DELETE_DISK:
2466		/*
2467		 * We don't trust the underland to tell us when to delete a
2468		 * container, rather we rely on an AIF coming from the
2469		 * controller
2470		 */
2471		error = 0;
2472		break;
2473	default:
2474		debug(1, "unsupported cmd 0x%lx\n", cmd);
2475		error = EINVAL;
2476		break;
2477	}
2478	return(error);
2479}
2480
2481static int
2482aac_poll(dev_t dev, int poll_events, d_thread_t *td)
2483{
2484	struct aac_softc *sc;
2485	int revents;
2486
2487	sc = dev->si_drv1;
2488	revents = 0;
2489
2490	AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2491	if ((poll_events & (POLLRDNORM | POLLIN)) != 0) {
2492		if (sc->aac_aifq_tail != sc->aac_aifq_head)
2493			revents |= poll_events & (POLLIN | POLLRDNORM);
2494	}
2495	AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2496
2497	if (revents == 0) {
2498		if (poll_events & (POLLIN | POLLRDNORM))
2499			selrecord(td, &sc->rcv_select);
2500	}
2501
2502	return (revents);
2503}
2504
2505/*
2506 * Send a FIB supplied from userspace
2507 */
2508static int
2509aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
2510{
2511	struct aac_command *cm;
2512	int size, error;
2513
2514	debug_called(2);
2515
2516	cm = NULL;
2517
2518	/*
2519	 * Get a command
2520	 */
2521	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
2522	if (aac_alloc_command(sc, &cm)) {
2523		error = EBUSY;
2524		goto out;
2525	}
2526
2527	/*
2528	 * Fetch the FIB header, then re-copy to get data as well.
2529	 */
2530	if ((error = copyin(ufib, cm->cm_fib,
2531			    sizeof(struct aac_fib_header))) != 0)
2532		goto out;
2533	size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
2534	if (size > sizeof(struct aac_fib)) {
2535		device_printf(sc->aac_dev, "incoming FIB oversized (%d > %zd)\n",
2536			      size, sizeof(struct aac_fib));
2537		size = sizeof(struct aac_fib);
2538	}
2539	if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
2540		goto out;
2541	cm->cm_fib->Header.Size = size;
2542	cm->cm_timestamp = time_second;
2543
2544	/*
2545	 * Pass the FIB to the controller, wait for it to complete.
2546	 */
2547	if ((error = aac_wait_command(cm, 30)) != 0) {	/* XXX user timeout? */
2548		device_printf(sc->aac_dev,
2549			      "aac_wait_command return %d\n", error);
2550		goto out;
2551	}
2552
2553	/*
2554	 * Copy the FIB and data back out to the caller.
2555	 */
2556	size = cm->cm_fib->Header.Size;
2557	if (size > sizeof(struct aac_fib)) {
2558		device_printf(sc->aac_dev, "outbound FIB oversized (%d > %zd)\n",
2559			      size, sizeof(struct aac_fib));
2560		size = sizeof(struct aac_fib);
2561	}
2562	error = copyout(cm->cm_fib, ufib, size);
2563
2564out:
2565	if (cm != NULL) {
2566		aac_release_command(cm);
2567	}
2568
2569	AAC_LOCK_RELEASE(&sc->aac_io_lock);
2570	return(error);
2571}
2572
2573/*
2574 * Handle an AIF sent to us by the controller; queue it for later reference.
2575 * If the queue fills up, then drop the older entries.
2576 */
2577static void
2578aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib)
2579{
2580	struct aac_aif_command *aif;
2581	struct aac_container *co, *co_next;
2582	struct aac_mntinfo *mi;
2583	struct aac_mntinforesp *mir = NULL;
2584	u_int16_t rsize;
2585	int next, found;
2586	int count = 0, added = 0, i = 0;
2587
2588	debug_called(2);
2589
2590	aif = (struct aac_aif_command*)&fib->data[0];
2591	aac_print_aif(sc, aif);
2592
2593	/* Is it an event that we should care about? */
2594	switch (aif->command) {
2595	case AifCmdEventNotify:
2596		switch (aif->data.EN.type) {
2597		case AifEnAddContainer:
2598		case AifEnDeleteContainer:
2599			/*
2600			 * A container was added or deleted, but the message
2601			 * doesn't tell us anything else!  Re-enumerate the
2602			 * containers and sort things out.
2603			 */
2604			aac_alloc_sync_fib(sc, &fib, 0);
2605			mi = (struct aac_mntinfo *)&fib->data[0];
2606			do {
2607				/*
2608				 * Ask the controller for its containers one at
2609				 * a time.
2610				 * XXX What if the controller's list changes
2611				 * midway through this enumaration?
2612				 * XXX This should be done async.
2613				 */
2614				bzero(mi, sizeof(struct aac_mntinfo));
2615				mi->Command = VM_NameServe;
2616				mi->MntType = FT_FILESYS;
2617				mi->MntCount = i;
2618				rsize = sizeof(mir);
2619				if (aac_sync_fib(sc, ContainerCommand, 0, fib,
2620						 sizeof(struct aac_mntinfo))) {
2621					printf("Error probing container %d\n",
2622					      i);
2623					continue;
2624				}
2625				mir = (struct aac_mntinforesp *)&fib->data[0];
2626				/* XXX Need to check if count changed */
2627				count = mir->MntRespCount;
2628				/*
2629				 * Check the container against our list.
2630				 * co->co_found was already set to 0 in a
2631				 * previous run.
2632				 */
2633				if ((mir->Status == ST_OK) &&
2634				    (mir->MntTable[0].VolType != CT_NONE)) {
2635					found = 0;
2636					TAILQ_FOREACH(co,
2637						      &sc->aac_container_tqh,
2638						      co_link) {
2639						if (co->co_mntobj.ObjectId ==
2640						    mir->MntTable[0].ObjectId) {
2641							co->co_found = 1;
2642							found = 1;
2643							break;
2644						}
2645					}
2646					/*
2647					 * If the container matched, continue
2648					 * in the list.
2649					 */
2650					if (found) {
2651						i++;
2652						continue;
2653					}
2654
2655					/*
2656					 * This is a new container.  Do all the
2657					 * appropriate things to set it up.
2658					 */
2659					aac_add_container(sc, mir, 1);
2660					added = 1;
2661				}
2662				i++;
2663			} while ((i < count) && (i < AAC_MAX_CONTAINERS));
2664			aac_release_sync_fib(sc);
2665
2666			/*
2667			 * Go through our list of containers and see which ones
2668			 * were not marked 'found'.  Since the controller didn't
2669			 * list them they must have been deleted.  Do the
2670			 * appropriate steps to destroy the device.  Also reset
2671			 * the co->co_found field.
2672			 */
2673			co = TAILQ_FIRST(&sc->aac_container_tqh);
2674			while (co != NULL) {
2675				if (co->co_found == 0) {
2676					device_delete_child(sc->aac_dev,
2677							    co->co_disk);
2678					co_next = TAILQ_NEXT(co, co_link);
2679					AAC_LOCK_ACQUIRE(&sc->
2680							aac_container_lock);
2681					TAILQ_REMOVE(&sc->aac_container_tqh, co,
2682						     co_link);
2683					AAC_LOCK_RELEASE(&sc->
2684							 aac_container_lock);
2685					FREE(co, M_AACBUF);
2686					co = co_next;
2687				} else {
2688					co->co_found = 0;
2689					co = TAILQ_NEXT(co, co_link);
2690				}
2691			}
2692
2693			/* Attach the newly created containers */
2694			if (added)
2695				bus_generic_attach(sc->aac_dev);
2696
2697			break;
2698
2699		default:
2700			break;
2701		}
2702
2703	default:
2704		break;
2705	}
2706
2707	/* Copy the AIF data to the AIF queue for ioctl retrieval */
2708	AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2709	next = (sc->aac_aifq_head + 1) % AAC_AIFQ_LENGTH;
2710	if (next != sc->aac_aifq_tail) {
2711		bcopy(aif, &sc->aac_aifq[next], sizeof(struct aac_aif_command));
2712		sc->aac_aifq_head = next;
2713
2714		/* On the off chance that someone is sleeping for an aif... */
2715		if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
2716			wakeup(sc->aac_aifq);
2717		/* Wakeup any poll()ers */
2718		selwakeup(&sc->rcv_select);
2719	}
2720	AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2721
2722	return;
2723}
2724
2725/*
2726 * Return the Revision of the driver to userspace and check to see if the
2727 * userspace app is possibly compatible.  This is extremely bogus since
2728 * our driver doesn't follow Adaptec's versioning system.  Cheat by just
2729 * returning what the card reported.
2730 */
2731static int
2732aac_rev_check(struct aac_softc *sc, caddr_t udata)
2733{
2734	struct aac_rev_check rev_check;
2735	struct aac_rev_check_resp rev_check_resp;
2736	int error = 0;
2737
2738	debug_called(2);
2739
2740	/*
2741	 * Copyin the revision struct from userspace
2742	 */
2743	if ((error = copyin(udata, (caddr_t)&rev_check,
2744			sizeof(struct aac_rev_check))) != 0) {
2745		return error;
2746	}
2747
2748	debug(2, "Userland revision= %d\n",
2749	      rev_check.callingRevision.buildNumber);
2750
2751	/*
2752	 * Doctor up the response struct.
2753	 */
2754	rev_check_resp.possiblyCompatible = 1;
2755	rev_check_resp.adapterSWRevision.external.ul =
2756	    sc->aac_revision.external.ul;
2757	rev_check_resp.adapterSWRevision.buildNumber =
2758	    sc->aac_revision.buildNumber;
2759
2760	return(copyout((caddr_t)&rev_check_resp, udata,
2761			sizeof(struct aac_rev_check_resp)));
2762}
2763
2764/*
2765 * Pass the caller the next AIF in their queue
2766 */
2767static int
2768aac_getnext_aif(struct aac_softc *sc, caddr_t arg)
2769{
2770	struct get_adapter_fib_ioctl agf;
2771	int error;
2772
2773	debug_called(2);
2774
2775	if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
2776
2777		/*
2778		 * Check the magic number that we gave the caller.
2779		 */
2780		if (agf.AdapterFibContext != (int)(uintptr_t)sc->aifthread) {
2781			error = EFAULT;
2782		} else {
2783			error = aac_return_aif(sc, agf.AifFib);
2784			if ((error == EAGAIN) && (agf.Wait)) {
2785				sc->aac_state |= AAC_STATE_AIF_SLEEPER;
2786				while (error == EAGAIN) {
2787					error = tsleep(sc->aac_aifq, PRIBIO |
2788						       PCATCH, "aacaif", 0);
2789					if (error == 0)
2790						error = aac_return_aif(sc,
2791						    agf.AifFib);
2792				}
2793				sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
2794			}
2795		}
2796	}
2797	return(error);
2798}
2799
2800/*
2801 * Hand the next AIF off the top of the queue out to userspace.
2802 */
2803static int
2804aac_return_aif(struct aac_softc *sc, caddr_t uptr)
2805{
2806	int error;
2807
2808	debug_called(2);
2809
2810	AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2811	if (sc->aac_aifq_tail == sc->aac_aifq_head) {
2812		error = EAGAIN;
2813	} else {
2814		error = copyout(&sc->aac_aifq[sc->aac_aifq_tail], uptr,
2815				sizeof(struct aac_aif_command));
2816		if (error)
2817			device_printf(sc->aac_dev,
2818			    "aac_return_aif: copyout returned %d\n", error);
2819		if (!error)
2820			sc->aac_aifq_tail = (sc->aac_aifq_tail + 1) %
2821					    AAC_AIFQ_LENGTH;
2822	}
2823	AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2824	return(error);
2825}
2826
2827/*
2828 * Give the userland some information about the container.  The AAC arch
2829 * expects the driver to be a SCSI passthrough type driver, so it expects
2830 * the containers to have b:t:l numbers.  Fake it.
2831 */
2832static int
2833aac_query_disk(struct aac_softc *sc, caddr_t uptr)
2834{
2835	struct aac_query_disk query_disk;
2836	struct aac_container *co;
2837	struct aac_disk	*disk;
2838	int error, id;
2839
2840	debug_called(2);
2841
2842	disk = NULL;
2843
2844	error = copyin(uptr, (caddr_t)&query_disk,
2845		       sizeof(struct aac_query_disk));
2846	if (error)
2847		return (error);
2848
2849	id = query_disk.ContainerNumber;
2850	if (id == -1)
2851		return (EINVAL);
2852
2853	AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
2854	TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
2855		if (co->co_mntobj.ObjectId == id)
2856			break;
2857		}
2858
2859	if (co == NULL) {
2860			query_disk.Valid = 0;
2861			query_disk.Locked = 0;
2862			query_disk.Deleted = 1;		/* XXX is this right? */
2863	} else {
2864		disk = device_get_softc(co->co_disk);
2865		query_disk.Valid = 1;
2866		query_disk.Locked =
2867		    (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0;
2868		query_disk.Deleted = 0;
2869		query_disk.Bus = device_get_unit(sc->aac_dev);
2870		query_disk.Target = disk->unit;
2871		query_disk.Lun = 0;
2872		query_disk.UnMapped = 0;
2873		sprintf(&query_disk.diskDeviceName[0], "%s%d",
2874		        disk->ad_disk.d_name, disk->ad_disk.d_unit);
2875	}
2876	AAC_LOCK_RELEASE(&sc->aac_container_lock);
2877
2878	error = copyout((caddr_t)&query_disk, uptr,
2879			sizeof(struct aac_query_disk));
2880
2881	return (error);
2882}
2883
2884static void
2885aac_get_bus_info(struct aac_softc *sc)
2886{
2887	struct aac_fib *fib;
2888	struct aac_ctcfg *c_cmd;
2889	struct aac_ctcfg_resp *c_resp;
2890	struct aac_vmioctl *vmi;
2891	struct aac_vmi_businf_resp *vmi_resp;
2892	struct aac_getbusinf businfo;
2893	struct aac_sim *caminf;
2894	device_t child;
2895	int i, found, error;
2896
2897	aac_alloc_sync_fib(sc, &fib, 0);
2898	c_cmd = (struct aac_ctcfg *)&fib->data[0];
2899	bzero(c_cmd, sizeof(struct aac_ctcfg));
2900
2901	c_cmd->Command = VM_ContainerConfig;
2902	c_cmd->cmd = CT_GET_SCSI_METHOD;
2903	c_cmd->param = 0;
2904
2905	error = aac_sync_fib(sc, ContainerCommand, 0, fib,
2906	    sizeof(struct aac_ctcfg));
2907	if (error) {
2908		device_printf(sc->aac_dev, "Error %d sending "
2909		    "VM_ContainerConfig command\n", error);
2910		aac_release_sync_fib(sc);
2911		return;
2912	}
2913
2914	c_resp = (struct aac_ctcfg_resp *)&fib->data[0];
2915	if (c_resp->Status != ST_OK) {
2916		device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n",
2917		    c_resp->Status);
2918		aac_release_sync_fib(sc);
2919		return;
2920	}
2921
2922	sc->scsi_method_id = c_resp->param;
2923
2924	vmi = (struct aac_vmioctl *)&fib->data[0];
2925	bzero(vmi, sizeof(struct aac_vmioctl));
2926
2927	vmi->Command = VM_Ioctl;
2928	vmi->ObjType = FT_DRIVE;
2929	vmi->MethId = sc->scsi_method_id;
2930	vmi->ObjId = 0;
2931	vmi->IoctlCmd = GetBusInfo;
2932
2933	error = aac_sync_fib(sc, ContainerCommand, 0, fib,
2934	    sizeof(struct aac_vmioctl));
2935	if (error) {
2936		device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
2937		    error);
2938		aac_release_sync_fib(sc);
2939		return;
2940	}
2941
2942	vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0];
2943	if (vmi_resp->Status != ST_OK) {
2944		device_printf(sc->aac_dev, "VM_Ioctl returned %d\n",
2945		    vmi_resp->Status);
2946		aac_release_sync_fib(sc);
2947		return;
2948	}
2949
2950	bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf));
2951	aac_release_sync_fib(sc);
2952
2953	found = 0;
2954	for (i = 0; i < businfo.BusCount; i++) {
2955		if (businfo.BusValid[i] != AAC_BUS_VALID)
2956			continue;
2957
2958		caminf = (struct aac_sim *)malloc( sizeof(struct aac_sim),
2959		    M_AACBUF, M_NOWAIT | M_ZERO);
2960		if (caminf == NULL)
2961			continue;
2962
2963		child = device_add_child(sc->aac_dev, "aacp", -1);
2964		if (child == NULL) {
2965			device_printf(sc->aac_dev, "device_add_child failed\n");
2966			continue;
2967		}
2968
2969		caminf->TargetsPerBus = businfo.TargetsPerBus;
2970		caminf->BusNumber = i;
2971		caminf->InitiatorBusId = businfo.InitiatorBusId[i];
2972		caminf->aac_sc = sc;
2973		caminf->sim_dev = child;
2974
2975		device_set_ivars(child, caminf);
2976		device_set_desc(child, "SCSI Passthrough Bus");
2977		TAILQ_INSERT_TAIL(&sc->aac_sim_tqh, caminf, sim_link);
2978
2979		found = 1;
2980	}
2981
2982	if (found)
2983		bus_generic_attach(sc->aac_dev);
2984
2985	return;
2986}
2987