aac.c revision 128258
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 128258 2004-04-14 19:11:29Z 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);
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_version =	D_VERSION,
185	.d_flags =	D_NEEDGIANT,
186	.d_open =	aac_open,
187	.d_close =	aac_close,
188	.d_ioctl =	aac_ioctl,
189	.d_poll =	aac_poll,
190	.d_name =	"aac",
191};
192
193MALLOC_DEFINE(M_AACBUF, "aacbuf", "Buffers for the AAC driver");
194
195/* sysctl node */
196SYSCTL_NODE(_hw, OID_AUTO, aac, CTLFLAG_RD, 0, "AAC driver parameters");
197
198/*
199 * Device Interface
200 */
201
202/*
203 * Initialise the controller and softc
204 */
205int
206aac_attach(struct aac_softc *sc)
207{
208	int error, unit;
209
210	debug_called(1);
211
212	/*
213	 * Initialise per-controller queues.
214	 */
215	aac_initq_free(sc);
216	aac_initq_ready(sc);
217	aac_initq_busy(sc);
218	aac_initq_bio(sc);
219
220	/*
221	 * Initialise command-completion task.
222	 */
223	TASK_INIT(&sc->aac_task_complete, 0, aac_complete, sc);
224
225	/* disable interrupts before we enable anything */
226	AAC_MASK_INTERRUPTS(sc);
227
228	/* mark controller as suspended until we get ourselves organised */
229	sc->aac_state |= AAC_STATE_SUSPEND;
230
231	/*
232	 * Check that the firmware on the card is supported.
233	 */
234	if ((error = aac_check_firmware(sc)) != 0)
235		return(error);
236
237	/*
238	 * Initialize locks
239	 */
240	AAC_LOCK_INIT(&sc->aac_sync_lock, "AAC sync FIB lock");
241	AAC_LOCK_INIT(&sc->aac_aifq_lock, "AAC AIF lock");
242	AAC_LOCK_INIT(&sc->aac_io_lock, "AAC I/O lock");
243	AAC_LOCK_INIT(&sc->aac_container_lock, "AAC container lock");
244	TAILQ_INIT(&sc->aac_container_tqh);
245
246	/* Initialize the local AIF queue pointers */
247	sc->aac_aifq_head = sc->aac_aifq_tail = AAC_AIFQ_LENGTH;
248
249	/*
250	 * Initialise the adapter.
251	 */
252	if ((error = aac_init(sc)) != 0)
253		return(error);
254
255	/*
256	 * Print a little information about the controller.
257	 */
258	aac_describe_controller(sc);
259
260	/*
261	 * Register to probe our containers later.
262	 */
263	sc->aac_ich.ich_func = aac_startup;
264	sc->aac_ich.ich_arg = sc;
265	if (config_intrhook_establish(&sc->aac_ich) != 0) {
266		device_printf(sc->aac_dev,
267			      "can't establish configuration hook\n");
268		return(ENXIO);
269	}
270
271	/*
272	 * Make the control device.
273	 */
274	unit = device_get_unit(sc->aac_dev);
275	sc->aac_dev_t = make_dev(&aac_cdevsw, unit, UID_ROOT, GID_OPERATOR,
276				 0640, "aac%d", unit);
277	(void)make_dev_alias(sc->aac_dev_t, "afa%d", unit);
278	(void)make_dev_alias(sc->aac_dev_t, "hpn%d", unit);
279	sc->aac_dev_t->si_drv1 = sc;
280
281	/* Create the AIF thread */
282	if (kthread_create((void(*)(void *))aac_command_thread, sc,
283			   &sc->aifthread, 0, 0, "aac%daif", unit))
284		panic("Could not create AIF thread\n");
285
286	/* Register the shutdown method to only be called post-dump */
287	if ((sc->eh = EVENTHANDLER_REGISTER(shutdown_final, aac_shutdown,
288	    sc->aac_dev, SHUTDOWN_PRI_DEFAULT)) == NULL)
289		device_printf(sc->aac_dev,
290			      "shutdown event registration failed\n");
291
292	/* Register with CAM for the non-DASD devices */
293	if ((sc->flags & AAC_FLAGS_ENABLE_CAM) != 0) {
294		TAILQ_INIT(&sc->aac_sim_tqh);
295		aac_get_bus_info(sc);
296	}
297
298	return(0);
299}
300
301/*
302 * Probe for containers, create disks.
303 */
304static void
305aac_startup(void *arg)
306{
307	struct aac_softc *sc;
308	struct aac_fib *fib;
309	struct aac_mntinfo *mi;
310	struct aac_mntinforesp *mir = NULL;
311	int count = 0, i = 0;
312
313	debug_called(1);
314
315	sc = (struct aac_softc *)arg;
316
317	/* disconnect ourselves from the intrhook chain */
318	config_intrhook_disestablish(&sc->aac_ich);
319
320	aac_alloc_sync_fib(sc, &fib, 0);
321	mi = (struct aac_mntinfo *)&fib->data[0];
322
323	/* loop over possible containers */
324	do {
325		/* request information on this container */
326		bzero(mi, sizeof(struct aac_mntinfo));
327		mi->Command = VM_NameServe;
328		mi->MntType = FT_FILESYS;
329		mi->MntCount = i;
330		if (aac_sync_fib(sc, ContainerCommand, 0, fib,
331				 sizeof(struct aac_mntinfo))) {
332			printf("error probing container %d", i);
333			continue;
334		}
335
336		mir = (struct aac_mntinforesp *)&fib->data[0];
337		/* XXX Need to check if count changed */
338		count = mir->MntRespCount;
339		aac_add_container(sc, mir, 0);
340		i++;
341	} while ((i < count) && (i < AAC_MAX_CONTAINERS));
342
343	aac_release_sync_fib(sc);
344
345	/* poke the bus to actually attach the child devices */
346	if (bus_generic_attach(sc->aac_dev))
347		device_printf(sc->aac_dev, "bus_generic_attach failed\n");
348
349	/* mark the controller up */
350	sc->aac_state &= ~AAC_STATE_SUSPEND;
351
352	/* enable interrupts now */
353	AAC_UNMASK_INTERRUPTS(sc);
354}
355
356/*
357 * Create a device to respresent a new container
358 */
359static void
360aac_add_container(struct aac_softc *sc, struct aac_mntinforesp *mir, int f)
361{
362	struct aac_container *co;
363	device_t child;
364
365	/*
366	 * Check container volume type for validity.  Note that many of
367	 * the possible types may never show up.
368	 */
369	if ((mir->Status == ST_OK) && (mir->MntTable[0].VolType != CT_NONE)) {
370		co = (struct aac_container *)malloc(sizeof *co, M_AACBUF,
371		       M_NOWAIT | M_ZERO);
372		if (co == NULL)
373			panic("Out of memory?!\n");
374		debug(1, "id %x  name '%.16s'  size %u  type %d",
375		      mir->MntTable[0].ObjectId,
376		      mir->MntTable[0].FileSystemName,
377		      mir->MntTable[0].Capacity, mir->MntTable[0].VolType);
378
379		if ((child = device_add_child(sc->aac_dev, "aacd", -1)) == NULL)
380			device_printf(sc->aac_dev, "device_add_child failed\n");
381		else
382			device_set_ivars(child, co);
383		device_set_desc(child, aac_describe_code(aac_container_types,
384				mir->MntTable[0].VolType));
385		co->co_disk = child;
386		co->co_found = f;
387		bcopy(&mir->MntTable[0], &co->co_mntobj,
388		      sizeof(struct aac_mntobj));
389		AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
390		TAILQ_INSERT_TAIL(&sc->aac_container_tqh, co, co_link);
391		AAC_LOCK_RELEASE(&sc->aac_container_lock);
392	}
393}
394
395/*
396 * Free all of the resources associated with (sc)
397 *
398 * Should not be called if the controller is active.
399 */
400void
401aac_free(struct aac_softc *sc)
402{
403
404	debug_called(1);
405
406	/* remove the control device */
407	if (sc->aac_dev_t != NULL)
408		destroy_dev(sc->aac_dev_t);
409
410	/* throw away any FIB buffers, discard the FIB DMA tag */
411	aac_free_commands(sc);
412	if (sc->aac_fib_dmat)
413		bus_dma_tag_destroy(sc->aac_fib_dmat);
414
415	free(sc->aac_commands, M_AACBUF);
416
417	/* destroy the common area */
418	if (sc->aac_common) {
419		bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap);
420		bus_dmamem_free(sc->aac_common_dmat, sc->aac_common,
421				sc->aac_common_dmamap);
422	}
423	if (sc->aac_common_dmat)
424		bus_dma_tag_destroy(sc->aac_common_dmat);
425
426	/* disconnect the interrupt handler */
427	if (sc->aac_intr)
428		bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr);
429	if (sc->aac_irq != NULL)
430		bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid,
431				     sc->aac_irq);
432
433	/* destroy data-transfer DMA tag */
434	if (sc->aac_buffer_dmat)
435		bus_dma_tag_destroy(sc->aac_buffer_dmat);
436
437	/* destroy the parent DMA tag */
438	if (sc->aac_parent_dmat)
439		bus_dma_tag_destroy(sc->aac_parent_dmat);
440
441	/* release the register window mapping */
442	if (sc->aac_regs_resource != NULL)
443		bus_release_resource(sc->aac_dev, SYS_RES_MEMORY,
444				     sc->aac_regs_rid, sc->aac_regs_resource);
445}
446
447/*
448 * Disconnect from the controller completely, in preparation for unload.
449 */
450int
451aac_detach(device_t dev)
452{
453	struct aac_softc *sc;
454	struct aac_container *co;
455	struct aac_sim	*sim;
456	int error;
457
458	debug_called(1);
459
460	sc = device_get_softc(dev);
461
462	if (sc->aac_state & AAC_STATE_OPEN)
463		return(EBUSY);
464
465	/* Remove the child containers */
466	while ((co = TAILQ_FIRST(&sc->aac_container_tqh)) != NULL) {
467		error = device_delete_child(dev, co->co_disk);
468		if (error)
469			return (error);
470		TAILQ_REMOVE(&sc->aac_container_tqh, co, co_link);
471		free(co, M_AACBUF);
472	}
473
474	/* Remove the CAM SIMs */
475	while ((sim = TAILQ_FIRST(&sc->aac_sim_tqh)) != NULL) {
476		TAILQ_REMOVE(&sc->aac_sim_tqh, sim, sim_link);
477		error = device_delete_child(dev, sim->sim_dev);
478		if (error)
479			return (error);
480		free(sim, M_AACBUF);
481	}
482
483	if (sc->aifflags & AAC_AIFFLAGS_RUNNING) {
484		sc->aifflags |= AAC_AIFFLAGS_EXIT;
485		wakeup(sc->aifthread);
486		tsleep(sc->aac_dev, PUSER | PCATCH, "aacdch", 30 * hz);
487	}
488
489	if (sc->aifflags & AAC_AIFFLAGS_RUNNING)
490		panic("Cannot shutdown AIF thread\n");
491
492	if ((error = aac_shutdown(dev)))
493		return(error);
494
495	EVENTHANDLER_DEREGISTER(shutdown_final, sc->eh);
496
497	aac_free(sc);
498
499	return(0);
500}
501
502/*
503 * Bring the controller down to a dormant state and detach all child devices.
504 *
505 * This function is called before detach or system shutdown.
506 *
507 * Note that we can assume that the bioq on the controller is empty, as we won't
508 * allow shutdown if any device is open.
509 */
510int
511aac_shutdown(device_t dev)
512{
513	struct aac_softc *sc;
514	struct aac_fib *fib;
515	struct aac_close_command *cc;
516
517	debug_called(1);
518
519	sc = device_get_softc(dev);
520
521	sc->aac_state |= AAC_STATE_SUSPEND;
522
523	/*
524	 * Send a Container shutdown followed by a HostShutdown FIB to the
525	 * controller to convince it that we don't want to talk to it anymore.
526	 * We've been closed and all I/O completed already
527	 */
528	device_printf(sc->aac_dev, "shutting down controller...");
529
530	aac_alloc_sync_fib(sc, &fib, AAC_SYNC_LOCK_FORCE);
531	cc = (struct aac_close_command *)&fib->data[0];
532
533	bzero(cc, sizeof(struct aac_close_command));
534	cc->Command = VM_CloseAll;
535	cc->ContainerId = 0xffffffff;
536	if (aac_sync_fib(sc, ContainerCommand, 0, fib,
537	    sizeof(struct aac_close_command)))
538		printf("FAILED.\n");
539	else
540		printf("done\n");
541#if 0
542	else {
543		fib->data[0] = 0;
544		/*
545		 * XXX Issuing this command to the controller makes it shut down
546		 * but also keeps it from coming back up without a reset of the
547		 * PCI bus.  This is not desirable if you are just unloading the
548		 * driver module with the intent to reload it later.
549		 */
550		if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN,
551		    fib, 1)) {
552			printf("FAILED.\n");
553		} else {
554			printf("done.\n");
555		}
556	}
557#endif
558
559	AAC_MASK_INTERRUPTS(sc);
560
561	return(0);
562}
563
564/*
565 * Bring the controller to a quiescent state, ready for system suspend.
566 */
567int
568aac_suspend(device_t dev)
569{
570	struct aac_softc *sc;
571
572	debug_called(1);
573
574	sc = device_get_softc(dev);
575
576	sc->aac_state |= AAC_STATE_SUSPEND;
577
578	AAC_MASK_INTERRUPTS(sc);
579	return(0);
580}
581
582/*
583 * Bring the controller back to a state ready for operation.
584 */
585int
586aac_resume(device_t dev)
587{
588	struct aac_softc *sc;
589
590	debug_called(1);
591
592	sc = device_get_softc(dev);
593
594	sc->aac_state &= ~AAC_STATE_SUSPEND;
595	AAC_UNMASK_INTERRUPTS(sc);
596	return(0);
597}
598
599/*
600 * Take an interrupt.
601 */
602void
603aac_intr(void *arg)
604{
605	struct aac_softc *sc;
606	u_int16_t reason;
607
608	debug_called(2);
609
610	sc = (struct aac_softc *)arg;
611
612	/*
613	 * Read the status register directly.  This is faster than taking the
614	 * driver lock and reading the queues directly.  It also saves having
615	 * to turn parts of the driver lock into a spin mutex, which would be
616	 * ugly.
617	 */
618	reason = AAC_GET_ISTATUS(sc);
619	AAC_CLEAR_ISTATUS(sc, reason);
620
621	/* handle completion processing */
622	if (reason & AAC_DB_RESPONSE_READY)
623		taskqueue_enqueue_fast(taskqueue_fast, &sc->aac_task_complete);
624
625	/* controller wants to talk to us */
626	if (reason & (AAC_DB_PRINTF | AAC_DB_COMMAND_READY)) {
627		/*
628		 * XXX Make sure that we don't get fooled by strange messages
629		 * that start with a NULL.
630		 */
631		if ((reason & AAC_DB_PRINTF) &&
632		    (sc->aac_common->ac_printf[0] == 0))
633			sc->aac_common->ac_printf[0] = 32;
634
635		/*
636		 * This might miss doing the actual wakeup.  However, the
637		 * msleep that this is waking up has a timeout, so it will
638		 * wake up eventually.  AIFs and printfs are low enough
639		 * priority that they can handle hanging out for a few seconds
640		 * if needed.
641		 */
642		wakeup(sc->aifthread);
643	}
644}
645
646/*
647 * Command Processing
648 */
649
650/*
651 * Start as much queued I/O as possible on the controller
652 */
653void
654aac_startio(struct aac_softc *sc)
655{
656	struct aac_command *cm;
657
658	debug_called(2);
659
660	if (sc->flags & AAC_QUEUE_FRZN)
661		return;
662
663	for (;;) {
664		/*
665		 * Try to get a command that's been put off for lack of
666		 * resources
667		 */
668		cm = aac_dequeue_ready(sc);
669
670		/*
671		 * Try to build a command off the bio queue (ignore error
672		 * return)
673		 */
674		if (cm == NULL)
675			aac_bio_command(sc, &cm);
676
677		/* nothing to do? */
678		if (cm == NULL)
679			break;
680
681		/*
682		 * Try to give the command to the controller.  Any error is
683		 * catastrophic since it means that bus_dmamap_load() failed.
684		 */
685		if (aac_map_command(cm) != 0)
686			panic("aac: error mapping command %p\n", cm);
687	}
688}
689
690/*
691 * Deliver a command to the controller; allocate controller resources at the
692 * last moment when possible.
693 */
694static int
695aac_map_command(struct aac_command *cm)
696{
697	struct aac_softc *sc;
698	int error;
699
700	debug_called(2);
701
702	sc = cm->cm_sc;
703	error = 0;
704
705	/* don't map more than once */
706	if (cm->cm_flags & AAC_CMD_MAPPED)
707		panic("aac: command %p already mapped", cm);
708
709	if (cm->cm_datalen != 0) {
710		error = bus_dmamap_load(sc->aac_buffer_dmat, cm->cm_datamap,
711					cm->cm_data, cm->cm_datalen,
712					aac_map_command_sg, cm, 0);
713		if (error == EINPROGRESS) {
714			debug(1, "freezing queue\n");
715			sc->flags |= AAC_QUEUE_FRZN;
716			error = 0;
717		}
718	} else {
719		aac_map_command_sg(cm, NULL, 0, 0);
720	}
721	return (error);
722}
723
724/*
725 * Handle notification of one or more FIBs coming from the controller.
726 */
727static void
728aac_command_thread(struct aac_softc *sc)
729{
730	struct aac_fib *fib;
731	u_int32_t fib_size;
732	int size, retval;
733
734	debug_called(2);
735
736	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
737	sc->aifflags = AAC_AIFFLAGS_RUNNING;
738
739	while ((sc->aifflags & AAC_AIFFLAGS_EXIT) == 0) {
740
741		retval = 0;
742		if ((sc->aifflags & AAC_AIFFLAGS_PENDING) == 0)
743			retval = msleep(sc->aifthread, &sc->aac_io_lock, PRIBIO,
744					"aifthd", AAC_PERIODIC_INTERVAL * hz);
745
746		/*
747		 * First see if any FIBs need to be allocated.  This needs
748		 * to be called without the driver lock because contigmalloc
749		 * will grab Giant, and would result in an LOR.
750		 */
751		if ((sc->aifflags & AAC_AIFFLAGS_ALLOCFIBS) != 0) {
752			AAC_LOCK_RELEASE(&sc->aac_io_lock);
753			aac_alloc_commands(sc);
754			AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
755			sc->aifflags &= ~AAC_AIFFLAGS_ALLOCFIBS;
756			aac_startio(sc);
757		}
758
759		/*
760		 * While we're here, check to see if any commands are stuck.
761		 * This is pretty low-priority, so it's ok if it doesn't
762		 * always fire.
763		 */
764		if (retval == EWOULDBLOCK)
765			aac_timeout(sc);
766
767		/* Check the hardware printf message buffer */
768		if (sc->aac_common->ac_printf[0] != 0)
769			aac_print_printf(sc);
770
771		/* Also check to see if the adapter has a command for us. */
772		while (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE,
773				       &fib_size, &fib) == 0) {
774
775			AAC_PRINT_FIB(sc, fib);
776
777			switch (fib->Header.Command) {
778			case AifRequest:
779				aac_handle_aif(sc, fib);
780				break;
781			default:
782				device_printf(sc->aac_dev, "unknown command "
783					      "from controller\n");
784				break;
785			}
786
787			if ((fib->Header.XferState == 0) ||
788			    (fib->Header.StructType != AAC_FIBTYPE_TFIB))
789				break;
790
791			/* Return the AIF to the controller. */
792			if (fib->Header.XferState & AAC_FIBSTATE_FROMADAP) {
793				fib->Header.XferState |= AAC_FIBSTATE_DONEHOST;
794				*(AAC_FSAStatus*)fib->data = ST_OK;
795
796				/* XXX Compute the Size field? */
797				size = fib->Header.Size;
798				if (size > sizeof(struct aac_fib)) {
799					size = sizeof(struct aac_fib);
800					fib->Header.Size = size;
801				}
802				/*
803				 * Since we did not generate this command, it
804				 * cannot go through the normal
805				 * enqueue->startio chain.
806				 */
807				aac_enqueue_response(sc,
808						     AAC_ADAP_NORM_RESP_QUEUE,
809						     fib);
810			}
811		}
812	}
813	sc->aifflags &= ~AAC_AIFFLAGS_RUNNING;
814	AAC_LOCK_RELEASE(&sc->aac_io_lock);
815	wakeup(sc->aac_dev);
816
817	kthread_exit(0);
818}
819
820/*
821 * Process completed commands.
822 */
823static void
824aac_complete(void *context, int pending)
825{
826	struct aac_softc *sc;
827	struct aac_command *cm;
828	struct aac_fib *fib;
829	u_int32_t fib_size;
830
831	debug_called(2);
832
833	sc = (struct aac_softc *)context;
834
835	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
836
837	/* pull completed commands off the queue */
838	for (;;) {
839		/* look for completed FIBs on our queue */
840		if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size,
841				    &fib))
842			break;	/* nothing to do */
843
844		/* get the command, unmap and hand off for processing */
845		cm = sc->aac_commands + fib->Header.SenderData;
846		if (cm == NULL) {
847			AAC_PRINT_FIB(sc, fib);
848			break;
849		}
850
851		aac_remove_busy(cm);
852		aac_unmap_command(cm);
853		cm->cm_flags |= AAC_CMD_COMPLETED;
854
855		/* is there a completion handler? */
856		if (cm->cm_complete != NULL) {
857			cm->cm_complete(cm);
858		} else {
859			/* assume that someone is sleeping on this command */
860			wakeup(cm);
861		}
862	}
863
864	/* see if we can start some more I/O */
865	sc->flags &= ~AAC_QUEUE_FRZN;
866	aac_startio(sc);
867
868	AAC_LOCK_RELEASE(&sc->aac_io_lock);
869}
870
871/*
872 * Handle a bio submitted from a disk device.
873 */
874void
875aac_submit_bio(struct bio *bp)
876{
877	struct aac_disk *ad;
878	struct aac_softc *sc;
879
880	debug_called(2);
881
882	ad = (struct aac_disk *)bp->bio_disk->d_drv1;
883	sc = ad->ad_controller;
884
885	/* queue the BIO and try to get some work done */
886	aac_enqueue_bio(sc, bp);
887	aac_startio(sc);
888}
889
890/*
891 * Get a bio and build a command to go with it.
892 */
893static int
894aac_bio_command(struct aac_softc *sc, struct aac_command **cmp)
895{
896	struct aac_command *cm;
897	struct aac_fib *fib;
898	struct aac_disk *ad;
899	struct bio *bp;
900
901	debug_called(2);
902
903	/* get the resources we will need */
904	cm = NULL;
905	bp = NULL;
906	if (aac_alloc_command(sc, &cm))	/* get a command */
907		goto fail;
908	if ((bp = aac_dequeue_bio(sc)) == NULL)
909		goto fail;
910
911	/* fill out the command */
912	cm->cm_data = (void *)bp->bio_data;
913	cm->cm_datalen = bp->bio_bcount;
914	cm->cm_complete = aac_bio_complete;
915	cm->cm_private = bp;
916	cm->cm_timestamp = time_second;
917	cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
918
919	/* build the FIB */
920	fib = cm->cm_fib;
921	fib->Header.Size = sizeof(struct aac_fib_header);
922	fib->Header.XferState =
923		AAC_FIBSTATE_HOSTOWNED   |
924		AAC_FIBSTATE_INITIALISED |
925		AAC_FIBSTATE_EMPTY	 |
926		AAC_FIBSTATE_FROMHOST	 |
927		AAC_FIBSTATE_REXPECTED   |
928		AAC_FIBSTATE_NORM	 |
929		AAC_FIBSTATE_ASYNC	 |
930		AAC_FIBSTATE_FAST_RESPONSE;
931
932	/* build the read/write request */
933	ad = (struct aac_disk *)bp->bio_disk->d_drv1;
934
935	if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
936		fib->Header.Command = ContainerCommand;
937		if (bp->bio_cmd == BIO_READ) {
938			struct aac_blockread *br;
939			br = (struct aac_blockread *)&fib->data[0];
940			br->Command = VM_CtBlockRead;
941			br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
942			br->BlockNumber = bp->bio_pblkno;
943			br->ByteCount = bp->bio_bcount;
944			fib->Header.Size += sizeof(struct aac_blockread);
945			cm->cm_sgtable = &br->SgMap;
946			cm->cm_flags |= AAC_CMD_DATAIN;
947		} else {
948			struct aac_blockwrite *bw;
949			bw = (struct aac_blockwrite *)&fib->data[0];
950			bw->Command = VM_CtBlockWrite;
951			bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
952			bw->BlockNumber = bp->bio_pblkno;
953			bw->ByteCount = bp->bio_bcount;
954			bw->Stable = CUNSTABLE;
955			fib->Header.Size += sizeof(struct aac_blockwrite);
956			cm->cm_flags |= AAC_CMD_DATAOUT;
957			cm->cm_sgtable = &bw->SgMap;
958		}
959	} else {
960		fib->Header.Command = ContainerCommand64;
961		if (bp->bio_cmd == BIO_READ) {
962			struct aac_blockread64 *br;
963			br = (struct aac_blockread64 *)&fib->data[0];
964			br->Command = VM_CtHostRead64;
965			br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
966			br->SectorCount = bp->bio_bcount / AAC_BLOCK_SIZE;
967			br->BlockNumber = bp->bio_pblkno;
968			br->Pad = 0;
969			br->Flags = 0;
970			fib->Header.Size += sizeof(struct aac_blockread64);
971			cm->cm_flags |= AAC_CMD_DATAOUT;
972			(struct aac_sg_table64 *)cm->cm_sgtable = &br->SgMap64;
973		} else {
974			struct aac_blockwrite64 *bw;
975			bw = (struct aac_blockwrite64 *)&fib->data[0];
976			bw->Command = VM_CtHostWrite64;
977			bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
978			bw->SectorCount = bp->bio_bcount / AAC_BLOCK_SIZE;
979			bw->BlockNumber = bp->bio_pblkno;
980			bw->Pad = 0;
981			bw->Flags = 0;
982			fib->Header.Size += sizeof(struct aac_blockwrite64);
983			cm->cm_flags |= AAC_CMD_DATAIN;
984			(struct aac_sg_table64 *)cm->cm_sgtable = &bw->SgMap64;
985		}
986	}
987
988	*cmp = cm;
989	return(0);
990
991fail:
992	if (bp != NULL)
993		aac_enqueue_bio(sc, bp);
994	if (cm != NULL)
995		aac_release_command(cm);
996	return(ENOMEM);
997}
998
999/*
1000 * Handle a bio-instigated command that has been completed.
1001 */
1002static void
1003aac_bio_complete(struct aac_command *cm)
1004{
1005	struct aac_blockread_response *brr;
1006	struct aac_blockwrite_response *bwr;
1007	struct bio *bp;
1008	AAC_FSAStatus status;
1009
1010	/* fetch relevant status and then release the command */
1011	bp = (struct bio *)cm->cm_private;
1012	if (bp->bio_cmd == BIO_READ) {
1013		brr = (struct aac_blockread_response *)&cm->cm_fib->data[0];
1014		status = brr->Status;
1015	} else {
1016		bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0];
1017		status = bwr->Status;
1018	}
1019	aac_release_command(cm);
1020
1021	/* fix up the bio based on status */
1022	if (status == ST_OK) {
1023		bp->bio_resid = 0;
1024	} else {
1025		bp->bio_error = EIO;
1026		bp->bio_flags |= BIO_ERROR;
1027		/* pass an error string out to the disk layer */
1028		bp->bio_driver1 = aac_describe_code(aac_command_status_table,
1029						    status);
1030	}
1031	aac_biodone(bp);
1032}
1033
1034/*
1035 * Submit a command to the controller, return when it completes.
1036 * XXX This is very dangerous!  If the card has gone out to lunch, we could
1037 *     be stuck here forever.  At the same time, signals are not caught
1038 *     because there is a risk that a signal could wakeup the sleep before
1039 *     the card has a chance to complete the command.  Since there is no way
1040 *     to cancel a command that is in progress, we can't protect against the
1041 *     card completing a command late and spamming the command and data
1042 *     memory.  So, we are held hostage until the command completes.
1043 */
1044static int
1045aac_wait_command(struct aac_command *cm)
1046{
1047	struct aac_softc *sc;
1048	int error;
1049
1050	debug_called(2);
1051
1052	sc = cm->cm_sc;
1053
1054	/* Put the command on the ready queue and get things going */
1055	cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
1056	aac_enqueue_ready(cm);
1057	aac_startio(sc);
1058	error = msleep(cm, &sc->aac_io_lock, PRIBIO, "aacwait", 0);
1059	return(error);
1060}
1061
1062/*
1063 *Command Buffer Management
1064 */
1065
1066/*
1067 * Allocate a command.
1068 */
1069int
1070aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
1071{
1072	struct aac_command *cm;
1073
1074	debug_called(3);
1075
1076	if ((cm = aac_dequeue_free(sc)) == NULL) {
1077		if (sc->total_fibs < sc->aac_max_fibs) {
1078			sc->aifflags |= AAC_AIFFLAGS_ALLOCFIBS;
1079			wakeup(sc->aifthread);
1080		}
1081		return (EBUSY);
1082	}
1083
1084	*cmp = cm;
1085	return(0);
1086}
1087
1088/*
1089 * Release a command back to the freelist.
1090 */
1091void
1092aac_release_command(struct aac_command *cm)
1093{
1094	debug_called(3);
1095
1096	/* (re)initialise the command/FIB */
1097	cm->cm_sgtable = NULL;
1098	cm->cm_flags = 0;
1099	cm->cm_complete = NULL;
1100	cm->cm_private = NULL;
1101	cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
1102	cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
1103	cm->cm_fib->Header.Flags = 0;
1104	cm->cm_fib->Header.SenderSize = sizeof(struct aac_fib);
1105
1106	/*
1107	 * These are duplicated in aac_start to cover the case where an
1108	 * intermediate stage may have destroyed them.  They're left
1109	 * initialised here for debugging purposes only.
1110	 */
1111	cm->cm_fib->Header.ReceiverFibAddress = (u_int32_t)cm->cm_fibphys;
1112	cm->cm_fib->Header.SenderData = 0;
1113
1114	aac_enqueue_free(cm);
1115}
1116
1117/*
1118 * Map helper for command/FIB allocation.
1119 */
1120static void
1121aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1122{
1123	uint32_t	*fibphys;
1124
1125	fibphys = (uint32_t *)arg;
1126
1127	debug_called(3);
1128
1129	*fibphys = segs[0].ds_addr;
1130}
1131
1132/*
1133 * Allocate and initialise commands/FIBs for this adapter.
1134 */
1135static int
1136aac_alloc_commands(struct aac_softc *sc)
1137{
1138	struct aac_command *cm;
1139	struct aac_fibmap *fm;
1140	uint32_t fibphys;
1141	int i, error;
1142
1143	debug_called(2);
1144
1145	if (sc->total_fibs + AAC_FIB_COUNT > sc->aac_max_fibs)
1146		return (ENOMEM);
1147
1148	fm = malloc(sizeof(struct aac_fibmap), M_AACBUF, M_NOWAIT|M_ZERO);
1149	if (fm == NULL)
1150		return (ENOMEM);
1151
1152	/* allocate the FIBs in DMAable memory and load them */
1153	if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&fm->aac_fibs,
1154			     BUS_DMA_NOWAIT, &fm->aac_fibmap)) {
1155		device_printf(sc->aac_dev,
1156			      "Not enough contiguous memory available.\n");
1157		free(fm, M_AACBUF);
1158		return (ENOMEM);
1159	}
1160
1161	/* Ignore errors since this doesn't bounce */
1162	(void)bus_dmamap_load(sc->aac_fib_dmat, fm->aac_fibmap, fm->aac_fibs,
1163			      AAC_FIB_COUNT * sizeof(struct aac_fib),
1164			      aac_map_command_helper, &fibphys, 0);
1165
1166	/* initialise constant fields in the command structure */
1167	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
1168	bzero(fm->aac_fibs, AAC_FIB_COUNT * sizeof(struct aac_fib));
1169	for (i = 0; i < AAC_FIB_COUNT; i++) {
1170		cm = sc->aac_commands + sc->total_fibs;
1171		fm->aac_commands = cm;
1172		cm->cm_sc = sc;
1173		cm->cm_fib = fm->aac_fibs + i;
1174		cm->cm_fibphys = fibphys + (i * sizeof(struct aac_fib));
1175		cm->cm_index = sc->total_fibs;
1176
1177		if ((error = bus_dmamap_create(sc->aac_buffer_dmat, 0,
1178					       &cm->cm_datamap)) == 0)
1179			aac_release_command(cm);
1180		else
1181			break;
1182		sc->total_fibs++;
1183	}
1184
1185	if (i > 0) {
1186		TAILQ_INSERT_TAIL(&sc->aac_fibmap_tqh, fm, fm_link);
1187		debug(1, "total_fibs= %d\n", sc->total_fibs);
1188		AAC_LOCK_RELEASE(&sc->aac_io_lock);
1189		return (0);
1190	}
1191
1192	AAC_LOCK_RELEASE(&sc->aac_io_lock);
1193	bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1194	bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1195	free(fm, M_AACBUF);
1196	return (ENOMEM);
1197}
1198
1199/*
1200 * Free FIBs owned by this adapter.
1201 */
1202static void
1203aac_free_commands(struct aac_softc *sc)
1204{
1205	struct aac_fibmap *fm;
1206	struct aac_command *cm;
1207	int i;
1208
1209	debug_called(1);
1210
1211	while ((fm = TAILQ_FIRST(&sc->aac_fibmap_tqh)) != NULL) {
1212
1213		TAILQ_REMOVE(&sc->aac_fibmap_tqh, fm, fm_link);
1214		/*
1215		 * We check against total_fibs to handle partially
1216		 * allocated blocks.
1217		 */
1218		for (i = 0; i < AAC_FIB_COUNT && sc->total_fibs--; i++) {
1219			cm = fm->aac_commands + i;
1220			bus_dmamap_destroy(sc->aac_buffer_dmat, cm->cm_datamap);
1221		}
1222		bus_dmamap_unload(sc->aac_fib_dmat, fm->aac_fibmap);
1223		bus_dmamem_free(sc->aac_fib_dmat, fm->aac_fibs, fm->aac_fibmap);
1224		free(fm, M_AACBUF);
1225	}
1226}
1227
1228/*
1229 * Command-mapping helper function - populate this command's s/g table.
1230 */
1231static void
1232aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1233{
1234	struct aac_softc *sc;
1235	struct aac_command *cm;
1236	struct aac_fib *fib;
1237	int i;
1238
1239	debug_called(3);
1240
1241	cm = (struct aac_command *)arg;
1242	sc = cm->cm_sc;
1243	fib = cm->cm_fib;
1244
1245	/* copy into the FIB */
1246	if (cm->cm_sgtable != NULL) {
1247		if ((cm->cm_sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
1248			struct aac_sg_table *sg;
1249			sg = cm->cm_sgtable;
1250			sg->SgCount = nseg;
1251			for (i = 0; i < nseg; i++) {
1252				sg->SgEntry[i].SgAddress = segs[i].ds_addr;
1253				sg->SgEntry[i].SgByteCount = segs[i].ds_len;
1254			}
1255			/* update the FIB size for the s/g count */
1256			fib->Header.Size += nseg * sizeof(struct aac_sg_entry);
1257		} else {
1258			struct aac_sg_table64 *sg;
1259			sg = (struct aac_sg_table64 *)cm->cm_sgtable;
1260			sg->SgCount = nseg;
1261			for (i = 0; i < nseg; i++) {
1262				sg->SgEntry64[i].SgAddress = segs[i].ds_addr;
1263				sg->SgEntry64[i].SgByteCount = segs[i].ds_len;
1264			}
1265			/* update the FIB size for the s/g count */
1266			fib->Header.Size += nseg*sizeof(struct aac_sg_entry64);
1267		}
1268	}
1269
1270	/* Fix up the address values in the FIB.  Use the command array index
1271	 * instead of a pointer since these fields are only 32 bits.  Shift
1272	 * the SenderFibAddress over to make room for the fast response bit.
1273	 */
1274	cm->cm_fib->Header.SenderFibAddress = (cm->cm_index << 1);
1275	cm->cm_fib->Header.ReceiverFibAddress = cm->cm_fibphys;
1276
1277	/* save a pointer to the command for speedy reverse-lookup */
1278	cm->cm_fib->Header.SenderData = cm->cm_index;
1279
1280	if (cm->cm_flags & AAC_CMD_DATAIN)
1281		bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1282				BUS_DMASYNC_PREREAD);
1283	if (cm->cm_flags & AAC_CMD_DATAOUT)
1284		bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1285				BUS_DMASYNC_PREWRITE);
1286	cm->cm_flags |= AAC_CMD_MAPPED;
1287
1288	/* put the FIB on the outbound queue */
1289	if (aac_enqueue_fib(sc, cm->cm_queue, cm) == EBUSY) {
1290		aac_unmap_command(cm);
1291		aac_requeue_ready(cm);
1292	}
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			bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
2402			      sizeof(struct aac_qstat));
2403			break;
2404		default:
2405			error = ENOENT;
2406			break;
2407		}
2408	break;
2409
2410	case FSACTL_SENDFIB:
2411		arg = *(caddr_t*)arg;
2412	case FSACTL_LNX_SENDFIB:
2413		debug(1, "FSACTL_SENDFIB");
2414		error = aac_ioctl_sendfib(sc, arg);
2415		break;
2416	case FSACTL_AIF_THREAD:
2417	case FSACTL_LNX_AIF_THREAD:
2418		debug(1, "FSACTL_AIF_THREAD");
2419		error = EINVAL;
2420		break;
2421	case FSACTL_OPEN_GET_ADAPTER_FIB:
2422		arg = *(caddr_t*)arg;
2423	case FSACTL_LNX_OPEN_GET_ADAPTER_FIB:
2424		debug(1, "FSACTL_OPEN_GET_ADAPTER_FIB");
2425		/*
2426		 * Pass the caller out an AdapterFibContext.
2427		 *
2428		 * Note that because we only support one opener, we
2429		 * basically ignore this.  Set the caller's context to a magic
2430		 * number just in case.
2431		 *
2432		 * The Linux code hands the driver a pointer into kernel space,
2433		 * and then trusts it when the caller hands it back.  Aiee!
2434		 * Here, we give it the proc pointer of the per-adapter aif
2435		 * thread. It's only used as a sanity check in other calls.
2436		 */
2437		cookie = (uint32_t)(uintptr_t)sc->aifthread;
2438		error = copyout(&cookie, arg, sizeof(cookie));
2439		break;
2440	case FSACTL_GET_NEXT_ADAPTER_FIB:
2441		arg = *(caddr_t*)arg;
2442	case FSACTL_LNX_GET_NEXT_ADAPTER_FIB:
2443		debug(1, "FSACTL_GET_NEXT_ADAPTER_FIB");
2444		error = aac_getnext_aif(sc, arg);
2445		break;
2446	case FSACTL_CLOSE_GET_ADAPTER_FIB:
2447	case FSACTL_LNX_CLOSE_GET_ADAPTER_FIB:
2448		debug(1, "FSACTL_CLOSE_GET_ADAPTER_FIB");
2449		/* don't do anything here */
2450		break;
2451	case FSACTL_MINIPORT_REV_CHECK:
2452		arg = *(caddr_t*)arg;
2453	case FSACTL_LNX_MINIPORT_REV_CHECK:
2454		debug(1, "FSACTL_MINIPORT_REV_CHECK");
2455		error = aac_rev_check(sc, arg);
2456		break;
2457	case FSACTL_QUERY_DISK:
2458		arg = *(caddr_t*)arg;
2459	case FSACTL_LNX_QUERY_DISK:
2460		debug(1, "FSACTL_QUERY_DISK");
2461		error = aac_query_disk(sc, arg);
2462			break;
2463	case FSACTL_DELETE_DISK:
2464	case FSACTL_LNX_DELETE_DISK:
2465		/*
2466		 * We don't trust the underland to tell us when to delete a
2467		 * container, rather we rely on an AIF coming from the
2468		 * controller
2469		 */
2470		error = 0;
2471		break;
2472	default:
2473		debug(1, "unsupported cmd 0x%lx\n", cmd);
2474		error = EINVAL;
2475		break;
2476	}
2477	return(error);
2478}
2479
2480static int
2481aac_poll(dev_t dev, int poll_events, d_thread_t *td)
2482{
2483	struct aac_softc *sc;
2484	int revents;
2485
2486	sc = dev->si_drv1;
2487	revents = 0;
2488
2489	AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2490	if ((poll_events & (POLLRDNORM | POLLIN)) != 0) {
2491		if (sc->aac_aifq_tail != sc->aac_aifq_head)
2492			revents |= poll_events & (POLLIN | POLLRDNORM);
2493	}
2494	AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2495
2496	if (revents == 0) {
2497		if (poll_events & (POLLIN | POLLRDNORM))
2498			selrecord(td, &sc->rcv_select);
2499	}
2500
2501	return (revents);
2502}
2503
2504/*
2505 * Send a FIB supplied from userspace
2506 */
2507static int
2508aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
2509{
2510	struct aac_command *cm;
2511	int size, error;
2512
2513	debug_called(2);
2514
2515	cm = NULL;
2516
2517	/*
2518	 * Get a command
2519	 */
2520	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
2521	if (aac_alloc_command(sc, &cm)) {
2522		error = EBUSY;
2523		goto out;
2524	}
2525
2526	/*
2527	 * Fetch the FIB header, then re-copy to get data as well.
2528	 */
2529	if ((error = copyin(ufib, cm->cm_fib,
2530			    sizeof(struct aac_fib_header))) != 0)
2531		goto out;
2532	size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
2533	if (size > sizeof(struct aac_fib)) {
2534		device_printf(sc->aac_dev, "incoming FIB oversized (%d > %zd)\n",
2535			      size, sizeof(struct aac_fib));
2536		size = sizeof(struct aac_fib);
2537	}
2538	if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
2539		goto out;
2540	cm->cm_fib->Header.Size = size;
2541	cm->cm_timestamp = time_second;
2542
2543	/*
2544	 * Pass the FIB to the controller, wait for it to complete.
2545	 */
2546	if ((error = aac_wait_command(cm)) != 0) {
2547		device_printf(sc->aac_dev,
2548			      "aac_wait_command return %d\n", error);
2549		goto out;
2550	}
2551
2552	/*
2553	 * Copy the FIB and data back out to the caller.
2554	 */
2555	size = cm->cm_fib->Header.Size;
2556	if (size > sizeof(struct aac_fib)) {
2557		device_printf(sc->aac_dev, "outbound FIB oversized (%d > %zd)\n",
2558			      size, sizeof(struct aac_fib));
2559		size = sizeof(struct aac_fib);
2560	}
2561	error = copyout(cm->cm_fib, ufib, size);
2562
2563out:
2564	if (cm != NULL) {
2565		aac_release_command(cm);
2566	}
2567
2568	AAC_LOCK_RELEASE(&sc->aac_io_lock);
2569	return(error);
2570}
2571
2572/*
2573 * Handle an AIF sent to us by the controller; queue it for later reference.
2574 * If the queue fills up, then drop the older entries.
2575 */
2576static void
2577aac_handle_aif(struct aac_softc *sc, struct aac_fib *fib)
2578{
2579	struct aac_aif_command *aif;
2580	struct aac_container *co, *co_next;
2581	struct aac_mntinfo *mi;
2582	struct aac_mntinforesp *mir = NULL;
2583	u_int16_t rsize;
2584	int next, found;
2585	int count = 0, added = 0, i = 0;
2586
2587	debug_called(2);
2588
2589	aif = (struct aac_aif_command*)&fib->data[0];
2590	aac_print_aif(sc, aif);
2591
2592	/* Is it an event that we should care about? */
2593	switch (aif->command) {
2594	case AifCmdEventNotify:
2595		switch (aif->data.EN.type) {
2596		case AifEnAddContainer:
2597		case AifEnDeleteContainer:
2598			/*
2599			 * A container was added or deleted, but the message
2600			 * doesn't tell us anything else!  Re-enumerate the
2601			 * containers and sort things out.
2602			 */
2603			aac_alloc_sync_fib(sc, &fib, 0);
2604			mi = (struct aac_mntinfo *)&fib->data[0];
2605			do {
2606				/*
2607				 * Ask the controller for its containers one at
2608				 * a time.
2609				 * XXX What if the controller's list changes
2610				 * midway through this enumaration?
2611				 * XXX This should be done async.
2612				 */
2613				bzero(mi, sizeof(struct aac_mntinfo));
2614				mi->Command = VM_NameServe;
2615				mi->MntType = FT_FILESYS;
2616				mi->MntCount = i;
2617				rsize = sizeof(mir);
2618				if (aac_sync_fib(sc, ContainerCommand, 0, fib,
2619						 sizeof(struct aac_mntinfo))) {
2620					printf("Error probing container %d\n",
2621					      i);
2622					continue;
2623				}
2624				mir = (struct aac_mntinforesp *)&fib->data[0];
2625				/* XXX Need to check if count changed */
2626				count = mir->MntRespCount;
2627				/*
2628				 * Check the container against our list.
2629				 * co->co_found was already set to 0 in a
2630				 * previous run.
2631				 */
2632				if ((mir->Status == ST_OK) &&
2633				    (mir->MntTable[0].VolType != CT_NONE)) {
2634					found = 0;
2635					TAILQ_FOREACH(co,
2636						      &sc->aac_container_tqh,
2637						      co_link) {
2638						if (co->co_mntobj.ObjectId ==
2639						    mir->MntTable[0].ObjectId) {
2640							co->co_found = 1;
2641							found = 1;
2642							break;
2643						}
2644					}
2645					/*
2646					 * If the container matched, continue
2647					 * in the list.
2648					 */
2649					if (found) {
2650						i++;
2651						continue;
2652					}
2653
2654					/*
2655					 * This is a new container.  Do all the
2656					 * appropriate things to set it up.
2657					 */
2658					aac_add_container(sc, mir, 1);
2659					added = 1;
2660				}
2661				i++;
2662			} while ((i < count) && (i < AAC_MAX_CONTAINERS));
2663			aac_release_sync_fib(sc);
2664
2665			/*
2666			 * Go through our list of containers and see which ones
2667			 * were not marked 'found'.  Since the controller didn't
2668			 * list them they must have been deleted.  Do the
2669			 * appropriate steps to destroy the device.  Also reset
2670			 * the co->co_found field.
2671			 */
2672			co = TAILQ_FIRST(&sc->aac_container_tqh);
2673			while (co != NULL) {
2674				if (co->co_found == 0) {
2675					device_delete_child(sc->aac_dev,
2676							    co->co_disk);
2677					co_next = TAILQ_NEXT(co, co_link);
2678					AAC_LOCK_ACQUIRE(&sc->
2679							aac_container_lock);
2680					TAILQ_REMOVE(&sc->aac_container_tqh, co,
2681						     co_link);
2682					AAC_LOCK_RELEASE(&sc->
2683							 aac_container_lock);
2684					FREE(co, M_AACBUF);
2685					co = co_next;
2686				} else {
2687					co->co_found = 0;
2688					co = TAILQ_NEXT(co, co_link);
2689				}
2690			}
2691
2692			/* Attach the newly created containers */
2693			if (added)
2694				bus_generic_attach(sc->aac_dev);
2695
2696			break;
2697
2698		default:
2699			break;
2700		}
2701
2702	default:
2703		break;
2704	}
2705
2706	/* Copy the AIF data to the AIF queue for ioctl retrieval */
2707	AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2708	next = (sc->aac_aifq_head + 1) % AAC_AIFQ_LENGTH;
2709	if (next != sc->aac_aifq_tail) {
2710		bcopy(aif, &sc->aac_aifq[next], sizeof(struct aac_aif_command));
2711		sc->aac_aifq_head = next;
2712
2713		/* On the off chance that someone is sleeping for an aif... */
2714		if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
2715			wakeup(sc->aac_aifq);
2716		/* Wakeup any poll()ers */
2717		selwakeuppri(&sc->rcv_select, PRIBIO);
2718	}
2719	AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2720
2721	return;
2722}
2723
2724/*
2725 * Return the Revision of the driver to userspace and check to see if the
2726 * userspace app is possibly compatible.  This is extremely bogus since
2727 * our driver doesn't follow Adaptec's versioning system.  Cheat by just
2728 * returning what the card reported.
2729 */
2730static int
2731aac_rev_check(struct aac_softc *sc, caddr_t udata)
2732{
2733	struct aac_rev_check rev_check;
2734	struct aac_rev_check_resp rev_check_resp;
2735	int error = 0;
2736
2737	debug_called(2);
2738
2739	/*
2740	 * Copyin the revision struct from userspace
2741	 */
2742	if ((error = copyin(udata, (caddr_t)&rev_check,
2743			sizeof(struct aac_rev_check))) != 0) {
2744		return error;
2745	}
2746
2747	debug(2, "Userland revision= %d\n",
2748	      rev_check.callingRevision.buildNumber);
2749
2750	/*
2751	 * Doctor up the response struct.
2752	 */
2753	rev_check_resp.possiblyCompatible = 1;
2754	rev_check_resp.adapterSWRevision.external.ul =
2755	    sc->aac_revision.external.ul;
2756	rev_check_resp.adapterSWRevision.buildNumber =
2757	    sc->aac_revision.buildNumber;
2758
2759	return(copyout((caddr_t)&rev_check_resp, udata,
2760			sizeof(struct aac_rev_check_resp)));
2761}
2762
2763/*
2764 * Pass the caller the next AIF in their queue
2765 */
2766static int
2767aac_getnext_aif(struct aac_softc *sc, caddr_t arg)
2768{
2769	struct get_adapter_fib_ioctl agf;
2770	int error;
2771
2772	debug_called(2);
2773
2774	if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
2775
2776		/*
2777		 * Check the magic number that we gave the caller.
2778		 */
2779		if (agf.AdapterFibContext != (int)(uintptr_t)sc->aifthread) {
2780			error = EFAULT;
2781		} else {
2782			error = aac_return_aif(sc, agf.AifFib);
2783			if ((error == EAGAIN) && (agf.Wait)) {
2784				sc->aac_state |= AAC_STATE_AIF_SLEEPER;
2785				while (error == EAGAIN) {
2786					error = tsleep(sc->aac_aifq, PRIBIO |
2787						       PCATCH, "aacaif", 0);
2788					if (error == 0)
2789						error = aac_return_aif(sc,
2790						    agf.AifFib);
2791				}
2792				sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
2793			}
2794		}
2795	}
2796	return(error);
2797}
2798
2799/*
2800 * Hand the next AIF off the top of the queue out to userspace.
2801 */
2802static int
2803aac_return_aif(struct aac_softc *sc, caddr_t uptr)
2804{
2805	int next, error;
2806
2807	debug_called(2);
2808
2809	AAC_LOCK_ACQUIRE(&sc->aac_aifq_lock);
2810	if (sc->aac_aifq_tail == sc->aac_aifq_head) {
2811		AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2812		return (EAGAIN);
2813	}
2814
2815	next = (sc->aac_aifq_tail + 1) % AAC_AIFQ_LENGTH;
2816	error = copyout(&sc->aac_aifq[next], uptr,
2817			sizeof(struct aac_aif_command));
2818	if (error)
2819		device_printf(sc->aac_dev,
2820		    "aac_return_aif: copyout returned %d\n", error);
2821	else
2822		sc->aac_aifq_tail = next;
2823
2824	AAC_LOCK_RELEASE(&sc->aac_aifq_lock);
2825	return(error);
2826}
2827
2828/*
2829 * Give the userland some information about the container.  The AAC arch
2830 * expects the driver to be a SCSI passthrough type driver, so it expects
2831 * the containers to have b:t:l numbers.  Fake it.
2832 */
2833static int
2834aac_query_disk(struct aac_softc *sc, caddr_t uptr)
2835{
2836	struct aac_query_disk query_disk;
2837	struct aac_container *co;
2838	struct aac_disk	*disk;
2839	int error, id;
2840
2841	debug_called(2);
2842
2843	disk = NULL;
2844
2845	error = copyin(uptr, (caddr_t)&query_disk,
2846		       sizeof(struct aac_query_disk));
2847	if (error)
2848		return (error);
2849
2850	id = query_disk.ContainerNumber;
2851	if (id == -1)
2852		return (EINVAL);
2853
2854	AAC_LOCK_ACQUIRE(&sc->aac_container_lock);
2855	TAILQ_FOREACH(co, &sc->aac_container_tqh, co_link) {
2856		if (co->co_mntobj.ObjectId == id)
2857			break;
2858		}
2859
2860	if (co == NULL) {
2861			query_disk.Valid = 0;
2862			query_disk.Locked = 0;
2863			query_disk.Deleted = 1;		/* XXX is this right? */
2864	} else {
2865		disk = device_get_softc(co->co_disk);
2866		query_disk.Valid = 1;
2867		query_disk.Locked =
2868		    (disk->ad_flags & AAC_DISK_OPEN) ? 1 : 0;
2869		query_disk.Deleted = 0;
2870		query_disk.Bus = device_get_unit(sc->aac_dev);
2871		query_disk.Target = disk->unit;
2872		query_disk.Lun = 0;
2873		query_disk.UnMapped = 0;
2874		sprintf(&query_disk.diskDeviceName[0], "%s%d",
2875		        disk->ad_disk->d_name, disk->ad_disk->d_unit);
2876	}
2877	AAC_LOCK_RELEASE(&sc->aac_container_lock);
2878
2879	error = copyout((caddr_t)&query_disk, uptr,
2880			sizeof(struct aac_query_disk));
2881
2882	return (error);
2883}
2884
2885static void
2886aac_get_bus_info(struct aac_softc *sc)
2887{
2888	struct aac_fib *fib;
2889	struct aac_ctcfg *c_cmd;
2890	struct aac_ctcfg_resp *c_resp;
2891	struct aac_vmioctl *vmi;
2892	struct aac_vmi_businf_resp *vmi_resp;
2893	struct aac_getbusinf businfo;
2894	struct aac_sim *caminf;
2895	device_t child;
2896	int i, found, error;
2897
2898	aac_alloc_sync_fib(sc, &fib, 0);
2899	c_cmd = (struct aac_ctcfg *)&fib->data[0];
2900	bzero(c_cmd, sizeof(struct aac_ctcfg));
2901
2902	c_cmd->Command = VM_ContainerConfig;
2903	c_cmd->cmd = CT_GET_SCSI_METHOD;
2904	c_cmd->param = 0;
2905
2906	error = aac_sync_fib(sc, ContainerCommand, 0, fib,
2907	    sizeof(struct aac_ctcfg));
2908	if (error) {
2909		device_printf(sc->aac_dev, "Error %d sending "
2910		    "VM_ContainerConfig command\n", error);
2911		aac_release_sync_fib(sc);
2912		return;
2913	}
2914
2915	c_resp = (struct aac_ctcfg_resp *)&fib->data[0];
2916	if (c_resp->Status != ST_OK) {
2917		device_printf(sc->aac_dev, "VM_ContainerConfig returned 0x%x\n",
2918		    c_resp->Status);
2919		aac_release_sync_fib(sc);
2920		return;
2921	}
2922
2923	sc->scsi_method_id = c_resp->param;
2924
2925	vmi = (struct aac_vmioctl *)&fib->data[0];
2926	bzero(vmi, sizeof(struct aac_vmioctl));
2927
2928	vmi->Command = VM_Ioctl;
2929	vmi->ObjType = FT_DRIVE;
2930	vmi->MethId = sc->scsi_method_id;
2931	vmi->ObjId = 0;
2932	vmi->IoctlCmd = GetBusInfo;
2933
2934	error = aac_sync_fib(sc, ContainerCommand, 0, fib,
2935	    sizeof(struct aac_vmioctl));
2936	if (error) {
2937		device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
2938		    error);
2939		aac_release_sync_fib(sc);
2940		return;
2941	}
2942
2943	vmi_resp = (struct aac_vmi_businf_resp *)&fib->data[0];
2944	if (vmi_resp->Status != ST_OK) {
2945		device_printf(sc->aac_dev, "VM_Ioctl returned %d\n",
2946		    vmi_resp->Status);
2947		aac_release_sync_fib(sc);
2948		return;
2949	}
2950
2951	bcopy(&vmi_resp->BusInf, &businfo, sizeof(struct aac_getbusinf));
2952	aac_release_sync_fib(sc);
2953
2954	found = 0;
2955	for (i = 0; i < businfo.BusCount; i++) {
2956		if (businfo.BusValid[i] != AAC_BUS_VALID)
2957			continue;
2958
2959		caminf = (struct aac_sim *)malloc( sizeof(struct aac_sim),
2960		    M_AACBUF, M_NOWAIT | M_ZERO);
2961		if (caminf == NULL)
2962			continue;
2963
2964		child = device_add_child(sc->aac_dev, "aacp", -1);
2965		if (child == NULL) {
2966			device_printf(sc->aac_dev, "device_add_child failed\n");
2967			continue;
2968		}
2969
2970		caminf->TargetsPerBus = businfo.TargetsPerBus;
2971		caminf->BusNumber = i;
2972		caminf->InitiatorBusId = businfo.InitiatorBusId[i];
2973		caminf->aac_sc = sc;
2974		caminf->sim_dev = child;
2975
2976		device_set_ivars(child, caminf);
2977		device_set_desc(child, "SCSI Passthrough Bus");
2978		TAILQ_INSERT_TAIL(&sc->aac_sim_tqh, caminf, sim_link);
2979
2980		found = 1;
2981	}
2982
2983	if (found)
2984		bus_generic_attach(sc->aac_dev);
2985
2986	return;
2987}
2988