xref: /freebsd/sys/dev/aac/aac.c (revision c6eafcf2)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2001 Scott Long
4  * Copyright (c) 2000 BSDi
5  * Copyright (c) 2001 Adaptec, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$FreeBSD$
30  */
31 
32 /*
33  * Driver for the Adaptec 'FSA' family of PCI/SCSI RAID adapters.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
40 
41 #include <dev/aac/aac_compat.h>
42 
43 #include <sys/bus.h>
44 #include <sys/conf.h>
45 #include <sys/devicestat.h>
46 #include <sys/disk.h>
47 #include <sys/file.h>
48 #include <sys/signalvar.h>
49 #include <sys/time.h>
50 
51 #include <machine/bus_memio.h>
52 #include <machine/bus.h>
53 #include <machine/resource.h>
54 
55 #include <dev/aac/aacreg.h>
56 #include <dev/aac/aac_ioctl.h>
57 #include <dev/aac/aacvar.h>
58 #include <dev/aac/aac_tables.h>
59 
60 devclass_t	aac_devclass;
61 
62 static void	aac_startup(void *arg);
63 
64 /* Command Processing */
65 static void	aac_startio(struct aac_softc *sc);
66 static void	aac_timeout(struct aac_softc *sc);
67 static int	aac_start(struct aac_command *cm);
68 static void	aac_complete(void *context, int pending);
69 static int	aac_bio_command(struct aac_softc *sc, struct aac_command **cmp);
70 static void	aac_bio_complete(struct aac_command *cm);
71 static int	aac_wait_command(struct aac_command *cm, int timeout);
72 static void	aac_host_command(struct aac_softc *sc);
73 static void	aac_host_response(struct aac_softc *sc);
74 
75 /* Command Buffer Management */
76 static int	aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp);
77 static void	aac_release_command(struct aac_command *cm);
78 static void	aac_map_command_helper(void *arg, bus_dma_segment_t *segs,
79 				       int nseg, int error);
80 static int	aac_alloc_commands(struct aac_softc *sc);
81 static void	aac_free_commands(struct aac_softc *sc);
82 static void	aac_map_command(struct aac_command *cm);
83 static void	aac_unmap_command(struct aac_command *cm);
84 
85 /* Hardware Interface */
86 static void	aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg,
87 			       int error);
88 static int	aac_init(struct aac_softc *sc);
89 static int	aac_sync_command(struct aac_softc *sc, u_int32_t command,
90 				 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2,
91 				 u_int32_t arg3, u_int32_t *sp);
92 static int	aac_sync_fib(struct aac_softc *sc, u_int32_t command,
93 			     u_int32_t xferstate, void *data,
94 			     u_int16_t datasize, void *result,
95 			     u_int16_t *resultsize);
96 static int	aac_enqueue_fib(struct aac_softc *sc, int queue,
97 				u_int32_t fib_size, u_int32_t fib_addr);
98 static int	aac_dequeue_fib(struct aac_softc *sc, int queue,
99 				u_int32_t *fib_size, struct aac_fib **fib_addr);
100 
101 /* StrongARM interface */
102 static int	aac_sa_get_fwstatus(struct aac_softc *sc);
103 static void	aac_sa_qnotify(struct aac_softc *sc, int qbit);
104 static int	aac_sa_get_istatus(struct aac_softc *sc);
105 static void	aac_sa_clear_istatus(struct aac_softc *sc, int mask);
106 static void	aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
107 				   u_int32_t arg0, u_int32_t arg1,
108 				   u_int32_t arg2, u_int32_t arg3);
109 static int	aac_sa_get_mailboxstatus(struct aac_softc *sc);
110 static void	aac_sa_set_interrupts(struct aac_softc *sc, int enable);
111 
112 struct aac_interface aac_sa_interface = {
113     aac_sa_get_fwstatus,
114     aac_sa_qnotify,
115     aac_sa_get_istatus,
116     aac_sa_clear_istatus,
117     aac_sa_set_mailbox,
118     aac_sa_get_mailboxstatus,
119     aac_sa_set_interrupts
120 };
121 
122 /* i960Rx interface */
123 static int	aac_rx_get_fwstatus(struct aac_softc *sc);
124 static void	aac_rx_qnotify(struct aac_softc *sc, int qbit);
125 static int	aac_rx_get_istatus(struct aac_softc *sc);
126 static void	aac_rx_clear_istatus(struct aac_softc *sc, int mask);
127 static void	aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
128 				   u_int32_t arg0, u_int32_t arg1,
129 				   u_int32_t arg2, u_int32_t arg3);
130 static int	aac_rx_get_mailboxstatus(struct aac_softc *sc);
131 static void	aac_rx_set_interrupts(struct aac_softc *sc, int enable);
132 
133 struct aac_interface aac_rx_interface = {
134     aac_rx_get_fwstatus,
135     aac_rx_qnotify,
136     aac_rx_get_istatus,
137     aac_rx_clear_istatus,
138     aac_rx_set_mailbox,
139     aac_rx_get_mailboxstatus,
140     aac_rx_set_interrupts
141 };
142 
143 /* Debugging and Diagnostics */
144 static void	aac_describe_controller(struct aac_softc *sc);
145 static char	*aac_describe_code(struct aac_code_lookup *table,
146 				   u_int32_t code);
147 
148 /* Management Interface */
149 static d_open_t		aac_open;
150 static d_close_t	aac_close;
151 static d_ioctl_t	aac_ioctl;
152 static int		aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib);
153 static void		aac_handle_aif(struct aac_softc *sc,
154 				       struct aac_aif_command *aif);
155 #ifdef AAC_COMPAT_LINUX
156 static int		aac_linux_rev_check(struct aac_softc *sc,
157 					    caddr_t udata);
158 static int		aac_linux_getnext_aif(struct aac_softc *sc,
159 					    caddr_t arg);
160 static int		aac_linux_return_aif(struct aac_softc *sc,
161 					    caddr_t uptr);
162 #endif
163 
164 #define AAC_CDEV_MAJOR	150
165 
166 static struct cdevsw aac_cdevsw = {
167     aac_open,		/* open */
168     aac_close,		/* close */
169     noread,		/* read */
170     nowrite,		/* write */
171     aac_ioctl,		/* ioctl */
172     nopoll,		/* poll */
173     nommap,		/* mmap */
174     nostrategy,		/* strategy */
175     "aac",		/* name */
176     AAC_CDEV_MAJOR,	/* major */
177     nodump,		/* dump */
178     nopsize,		/* psize */
179     0,			/* flags */
180 };
181 
182 /******************************************************************************
183  ******************************************************************************
184 				Device Interface
185  ******************************************************************************
186  ******************************************************************************/
187 
188 /******************************************************************************
189  * Initialise the controller and softc
190  */
191 int
192 aac_attach(struct aac_softc *sc)
193 {
194     int		error, unit;
195 
196     debug_called(1);
197 
198     /*
199      * Initialise per-controller queues.
200      */
201     aac_initq_free(sc);
202     aac_initq_ready(sc);
203     aac_initq_busy(sc);
204     aac_initq_complete(sc);
205     aac_initq_bio(sc);
206 
207 #if __FreeBSD_version >= 500005
208     /*
209      * Initialise command-completion task.
210      */
211     TASK_INIT(&sc->aac_task_complete, 0, aac_complete, sc);
212 #endif
213 
214     /* disable interrupts before we enable anything */
215     AAC_MASK_INTERRUPTS(sc);
216 
217     /* mark controller as suspended until we get ourselves organised */
218     sc->aac_state |= AAC_STATE_SUSPEND;
219 
220     /*
221      * Allocate command structures.
222      */
223     if ((error = aac_alloc_commands(sc)) != 0)
224 	return(error);
225 
226     /*
227      * Initialise the adapter.
228      */
229     if ((error = aac_init(sc)) != 0)
230 	return(error);
231 
232     /*
233      * Print a little information about the controller.
234      */
235     aac_describe_controller(sc);
236 
237     /*
238      * Register to probe our containers later.
239      */
240     sc->aac_ich.ich_func = aac_startup;
241     sc->aac_ich.ich_arg = sc;
242     if (config_intrhook_establish(&sc->aac_ich) != 0) {
243         device_printf(sc->aac_dev, "can't establish configuration hook\n");
244         return(ENXIO);
245     }
246 
247     /*
248      * Make the control device.
249      */
250     unit = device_get_unit(sc->aac_dev);
251     sc->aac_dev_t = make_dev(&aac_cdevsw, unit, UID_ROOT, GID_WHEEL, 0644,
252 			     "aac%d", unit);
253     (void)make_dev_alias(sc->aac_dev_t, "afa%d", unit);
254     (void)make_dev_alias(sc->aac_dev_t, "hpn%d", unit);
255 
256     sc->aac_dev_t->si_drv1 = sc;
257 
258     return(0);
259 }
260 
261 /******************************************************************************
262  * Probe for containers, create disks.
263  */
264 static void
265 aac_startup(void *arg)
266 {
267     struct aac_softc		*sc = (struct aac_softc *)arg;
268     struct aac_mntinfo		mi;
269     struct aac_mntinforesponse	mir;
270     device_t			child;
271     u_int16_t			rsize;
272     int				i;
273 
274     debug_called(1);
275 
276     /* disconnect ourselves from the intrhook chain */
277     config_intrhook_disestablish(&sc->aac_ich);
278 
279     /* loop over possible containers */
280     mi.Command = VM_NameServe;
281     mi.MntType = FT_FILESYS;
282     for (i = 0; i < AAC_MAX_CONTAINERS; i++) {
283 	/* request information on this container */
284 	mi.MntCount = i;
285 	if (aac_sync_fib(sc, ContainerCommand, 0, &mi,
286 			 sizeof(struct aac_mntinfo), &mir, &rsize)) {
287 	    debug(2, "error probing container %d", i);
288 	    continue;
289 	}
290 	/* check response size */
291 	if (rsize != sizeof(mir)) {
292 	    debug(2, "container info response wrong size (%d should be %d)",
293 		  rsize, sizeof(mir));
294 	    continue;
295 	}
296 	/*
297 	 * Check container volume type for validity.  Note that many of the
298 	 * possible types may never show up.
299 	 */
300 	if ((mir.Status == ST_OK) && (mir.MntTable[0].VolType != CT_NONE)) {
301 	    debug(1, "%d: id %x  name '%.16s'  size %u  type %d",
302 		  i, mir.MntTable[0].ObjectId,
303 		  mir.MntTable[0].FileSystemName, mir.MntTable[0].Capacity,
304 		  mir.MntTable[0].VolType);
305 
306 	    if ((child = device_add_child(sc->aac_dev, NULL, -1)) == NULL) {
307 		device_printf(sc->aac_dev, "device_add_child failed\n");
308 	    } else {
309 		device_set_ivars(child, &sc->aac_container[i]);
310 	    }
311 	    device_set_desc(child, aac_describe_code(aac_container_types,
312 			    mir.MntTable[0].VolType));
313 	    sc->aac_container[i].co_disk = child;
314 	    sc->aac_container[i].co_mntobj = mir.MntTable[0];
315 	}
316     }
317 
318     /* poke the bus to actually attach the child devices */
319     if (bus_generic_attach(sc->aac_dev))
320 	device_printf(sc->aac_dev, "bus_generic_attach failed\n");
321 
322     /* mark the controller up */
323     sc->aac_state &= ~AAC_STATE_SUSPEND;
324 
325     /* enable interrupts now */
326     AAC_UNMASK_INTERRUPTS(sc);
327 
328     /* enable the timeout watchdog */
329     timeout((timeout_t*)aac_timeout, sc, AAC_PERIODIC_INTERVAL * hz);
330 }
331 
332 /******************************************************************************
333  * Free all of the resources associated with (sc)
334  *
335  * Should not be called if the controller is active.
336  */
337 void
338 aac_free(struct aac_softc *sc)
339 {
340     debug_called(1);
341 
342     /* remove the control device */
343     if (sc->aac_dev_t != NULL)
344 	destroy_dev(sc->aac_dev_t);
345 
346     /* throw away any FIB buffers, discard the FIB DMA tag */
347     if (sc->aac_fibs != NULL)
348 	aac_free_commands(sc);
349     if (sc->aac_fib_dmat)
350 	bus_dma_tag_destroy(sc->aac_fib_dmat);
351 
352     /* destroy the common area */
353     if (sc->aac_common) {
354 	bus_dmamap_unload(sc->aac_common_dmat, sc->aac_common_dmamap);
355 	bus_dmamem_free(sc->aac_common_dmat, sc->aac_common,
356 			sc->aac_common_dmamap);
357     }
358     if (sc->aac_common_dmat)
359 	bus_dma_tag_destroy(sc->aac_common_dmat);
360 
361     /* disconnect the interrupt handler */
362     if (sc->aac_intr)
363 	bus_teardown_intr(sc->aac_dev, sc->aac_irq, sc->aac_intr);
364     if (sc->aac_irq != NULL)
365 	bus_release_resource(sc->aac_dev, SYS_RES_IRQ, sc->aac_irq_rid,
366 			     sc->aac_irq);
367 
368     /* destroy data-transfer DMA tag */
369     if (sc->aac_buffer_dmat)
370 	bus_dma_tag_destroy(sc->aac_buffer_dmat);
371 
372     /* destroy the parent DMA tag */
373     if (sc->aac_parent_dmat)
374 	bus_dma_tag_destroy(sc->aac_parent_dmat);
375 
376     /* release the register window mapping */
377     if (sc->aac_regs_resource != NULL)
378 	bus_release_resource(sc->aac_dev, SYS_RES_MEMORY, sc->aac_regs_rid,
379 			     sc->aac_regs_resource);
380 }
381 
382 /******************************************************************************
383  * Disconnect from the controller completely, in preparation for unload.
384  */
385 int
386 aac_detach(device_t dev)
387 {
388     struct aac_softc	*sc = device_get_softc(dev);
389     int			error;
390 
391     debug_called(1);
392 
393     if (sc->aac_state & AAC_STATE_OPEN)
394 	return(EBUSY);
395 
396     if ((error = aac_shutdown(dev)))
397 	return(error);
398 
399     aac_free(sc);
400 
401     return(0);
402 }
403 
404 /******************************************************************************
405  * Bring the controller down to a dormant state and detach all child devices.
406  *
407  * This function is called before detach or system shutdown.
408  *
409  * Note that we can assume that the bioq on the controller is empty, as we won't
410  * allow shutdown if any device is open.
411  */
412 int
413 aac_shutdown(device_t dev)
414 {
415     struct aac_softc		*sc = device_get_softc(dev);
416     struct aac_close_command	cc;
417     int				s, i;
418 
419     debug_called(1);
420 
421     s = splbio();
422 
423     sc->aac_state |= AAC_STATE_SUSPEND;
424 
425     /*
426      * Send a Container shutdown followed by a HostShutdown FIB to the
427      * controller to convince it that we don't want to talk to it anymore.
428      * We've been closed and all I/O completed already
429      */
430     device_printf(sc->aac_dev, "shutting down controller...");
431 
432     cc.Command = VM_CloseAll;
433     cc.ContainerId = 0xffffffff;
434     if (aac_sync_fib(sc, ContainerCommand, 0, &cc, sizeof(cc), NULL, NULL)) {
435 	printf("FAILED.\n");
436     } else {
437 	i = 0;
438 	if (aac_sync_fib(sc, FsaHostShutdown, AAC_FIBSTATE_SHUTDOWN, &i,
439 			 sizeof(i), NULL, NULL)) {
440 	    printf("FAILED.\n");
441 	} else {
442 	    printf("done.\n");
443 	}
444     }
445 
446     AAC_MASK_INTERRUPTS(sc);
447 
448     splx(s);
449     return(0);
450 }
451 
452 /******************************************************************************
453  * Bring the controller to a quiescent state, ready for system suspend.
454  */
455 int
456 aac_suspend(device_t dev)
457 {
458     struct aac_softc	*sc = device_get_softc(dev);
459     int			s;
460 
461     debug_called(1);
462     s = splbio();
463 
464     sc->aac_state |= AAC_STATE_SUSPEND;
465 
466     AAC_MASK_INTERRUPTS(sc);
467     splx(s);
468     return(0);
469 }
470 
471 /******************************************************************************
472  * Bring the controller back to a state ready for operation.
473  */
474 int
475 aac_resume(device_t dev)
476 {
477     struct aac_softc	*sc = device_get_softc(dev);
478 
479     debug_called(1);
480     sc->aac_state &= ~AAC_STATE_SUSPEND;
481     AAC_UNMASK_INTERRUPTS(sc);
482     return(0);
483 }
484 
485 /******************************************************************************
486  * Take an interrupt.
487  */
488 void
489 aac_intr(void *arg)
490 {
491     struct aac_softc	*sc = (struct aac_softc *)arg;
492     u_int16_t		reason;
493 
494     debug_called(2);
495 
496     reason = AAC_GET_ISTATUS(sc);
497 
498     /* controller wants to talk to the log?  XXX should we defer this? */
499     if (reason & AAC_DB_PRINTF) {
500 	if (sc->aac_common->ac_printf[0]) {
501 	    device_printf(sc->aac_dev, "** %.*s", AAC_PRINTF_BUFSIZE,
502 			  sc->aac_common->ac_printf);
503 	    sc->aac_common->ac_printf[0] = 0;
504 	}
505 	AAC_CLEAR_ISTATUS(sc, AAC_DB_PRINTF);
506 	AAC_QNOTIFY(sc, AAC_DB_PRINTF);
507     }
508 
509     /* controller has a message for us? */
510     if (reason & AAC_DB_COMMAND_READY) {
511 	AAC_CLEAR_ISTATUS(sc, AAC_DB_COMMAND_READY);
512 	aac_host_command(sc);
513     }
514 
515     /* controller has a response for us? */
516     if (reason & AAC_DB_RESPONSE_READY) {
517 	AAC_CLEAR_ISTATUS(sc, AAC_DB_RESPONSE_READY);
518 	aac_host_response(sc);
519     }
520 
521     /*
522      * spurious interrupts that we don't use - reset the mask and clear the
523      * interrupts
524      */
525     if (reason & (AAC_DB_COMMAND_NOT_FULL | AAC_DB_RESPONSE_NOT_FULL)) {
526 	AAC_UNMASK_INTERRUPTS(sc);
527 	AAC_CLEAR_ISTATUS(sc, AAC_DB_COMMAND_NOT_FULL |
528 			  AAC_DB_RESPONSE_NOT_FULL);
529     }
530 };
531 
532 /******************************************************************************
533  ******************************************************************************
534 				Command Processing
535  ******************************************************************************
536  ******************************************************************************/
537 
538 /******************************************************************************
539  * Start as much queued I/O as possible on the controller
540  */
541 static void
542 aac_startio(struct aac_softc *sc)
543 {
544     struct aac_command	*cm;
545 
546     debug_called(2);
547 
548     for(;;) {
549 	/* try to get a command that's been put off for lack of resources */
550 	cm = aac_dequeue_ready(sc);
551 
552 	/* try to build a command off the bio queue (ignore error return) */
553 	if (cm == NULL)
554 	    aac_bio_command(sc, &cm);
555 
556 	/* nothing to do? */
557 	if (cm == NULL)
558 	    break;
559 
560 	/* try to give the command to the controller */
561 	if (aac_start(cm) == EBUSY) {
562 	    /* put it on the ready queue for later */
563 	    aac_requeue_ready(cm);
564 	    break;
565 	}
566     }
567 }
568 
569 /******************************************************************************
570  * Deliver a command to the controller; allocate controller resources at the
571  * last moment when possible.
572  */
573 static int
574 aac_start(struct aac_command *cm)
575 {
576     struct aac_softc	*sc = cm->cm_sc;
577     int			error;
578 
579     debug_called(2);
580 
581     /* get the command mapped */
582     aac_map_command(cm);
583 
584     /* fix up the address values in the FIB */
585     cm->cm_fib->Header.SenderFibAddress = (u_int32_t)cm->cm_fib;
586     cm->cm_fib->Header.ReceiverFibAddress = cm->cm_fibphys;
587 
588     /* save a pointer to the command for speedy reverse-lookup */
589     cm->cm_fib->Header.SenderData = (u_int32_t)cm;	/* XXX 64-bit physical
590 							 * address issue */
591 
592     /* put the FIB on the outbound queue */
593     if (aac_enqueue_fib(sc, AAC_ADAP_NORM_CMD_QUEUE, cm->cm_fib->Header.Size,
594 			cm->cm_fib->Header.ReceiverFibAddress)) {
595 	error = EBUSY;
596     } else {
597 	aac_enqueue_busy(cm);
598 	error = 0;
599     }
600     return(error);
601 }
602 
603 /******************************************************************************
604  * Handle notification of one or more FIBs coming from the controller.
605  */
606 static void
607 aac_host_command(struct aac_softc *sc)
608 {
609     struct aac_fib	*fib;
610     u_int32_t		fib_size;
611 
612     debug_called(1);
613 
614     for (;;) {
615 	if (aac_dequeue_fib(sc, AAC_HOST_NORM_CMD_QUEUE, &fib_size, &fib))
616 	    break;	/* nothing to do */
617 
618 	switch(fib->Header.Command) {
619 	case AifRequest:
620 	    aac_handle_aif(sc, (struct aac_aif_command *)&fib->data[0]);
621 	    break;
622 	default:
623 	    device_printf(sc->aac_dev, "unknown command from controller\n");
624 	    AAC_PRINT_FIB(sc, fib);
625 	    break;
626 	}
627 
628 	/* XXX reply to FIBs requesting responses ?? */
629 	/* XXX how do we return these FIBs to the controller? */
630     }
631 }
632 
633 /******************************************************************************
634  * Handle notification of one or more FIBs completed by the controller
635  */
636 static void
637 aac_host_response(struct aac_softc *sc)
638 {
639     struct aac_command	*cm;
640     struct aac_fib	*fib;
641     u_int32_t		fib_size;
642 
643     debug_called(2);
644 
645     for (;;) {
646 	/* look for completed FIBs on our queue */
647 	if (aac_dequeue_fib(sc, AAC_HOST_NORM_RESP_QUEUE, &fib_size, &fib))
648 	    break;	/* nothing to do */
649 
650 	/* get the command, unmap and queue for later processing */
651 	cm = (struct aac_command *)fib->Header.SenderData;
652 	if (cm == NULL) {
653 	    AAC_PRINT_FIB(sc, fib);
654 	} else {
655 	    aac_remove_busy(cm);
656 	    aac_unmap_command(cm);		/* XXX defer? */
657 	    aac_enqueue_complete(cm);
658 	}
659     }
660 
661     /* handle completion processing */
662 #if __FreeBSD_version >= 500005
663     taskqueue_enqueue(taskqueue_swi, &sc->aac_task_complete);
664 #else
665     aac_complete(sc, 0);
666 #endif
667 }
668 
669 /******************************************************************************
670  * Process completed commands.
671  */
672 static void
673 aac_complete(void *context, int pending)
674 {
675     struct aac_softc	*sc = (struct aac_softc *)context;
676     struct aac_command	*cm;
677 
678     debug_called(2);
679 
680     /* pull completed commands off the queue */
681     for (;;) {
682 	cm = aac_dequeue_complete(sc);
683 	if (cm == NULL)
684 	    break;
685 	cm->cm_flags |= AAC_CMD_COMPLETED;
686 
687 	/* is there a completion handler? */
688 	if (cm->cm_complete != NULL) {
689 	    cm->cm_complete(cm);
690 	} else {
691 	    /* assume that someone is sleeping on this command */
692 	    wakeup(cm);
693 	}
694     }
695 
696     /* see if we can start some more I/O */
697     aac_startio(sc);
698 }
699 
700 /******************************************************************************
701  * Handle a bio submitted from a disk device.
702  */
703 void
704 aac_submit_bio(struct bio *bp)
705 {
706     struct aac_disk	*ad = (struct aac_disk *)bp->bio_dev->si_drv1;
707     struct aac_softc	*sc = ad->ad_controller;
708 
709     debug_called(2);
710 
711     /* queue the BIO and try to get some work done */
712     aac_enqueue_bio(sc, bp);
713     aac_startio(sc);
714 }
715 
716 /******************************************************************************
717  * Get a bio and build a command to go with it.
718  */
719 static int
720 aac_bio_command(struct aac_softc *sc, struct aac_command **cmp)
721 {
722     struct aac_command		*cm;
723     struct aac_fib		*fib;
724     struct aac_blockread	*br;
725     struct aac_blockwrite	*bw;
726     struct aac_disk		*ad;
727     struct bio			*bp;
728 
729     debug_called(2);
730 
731     /* get the resources we will need */
732     cm = NULL;
733     if ((bp = aac_dequeue_bio(sc)) == NULL)
734 	goto fail;
735     if (aac_alloc_command(sc, &cm))	/* get a command */
736 	goto fail;
737 
738     /* fill out the command */
739     cm->cm_data = (void *)bp->bio_data;
740     cm->cm_datalen = bp->bio_bcount;
741     cm->cm_complete = aac_bio_complete;
742     cm->cm_private = bp;
743     cm->cm_timestamp = time_second;
744 
745     /* build the FIB */
746     fib = cm->cm_fib;
747     fib->Header.XferState =
748 	AAC_FIBSTATE_HOSTOWNED   |
749 	AAC_FIBSTATE_INITIALISED |
750 	AAC_FIBSTATE_FROMHOST    |
751 	AAC_FIBSTATE_REXPECTED   |
752 	AAC_FIBSTATE_NORM;
753     fib->Header.Command = ContainerCommand;
754     fib->Header.Size = sizeof(struct aac_fib_header);
755 
756     /* build the read/write request */
757     ad = (struct aac_disk *)bp->bio_dev->si_drv1;
758     if (BIO_IS_READ(bp)) {
759 	br = (struct aac_blockread *)&fib->data[0];
760 	br->Command = VM_CtBlockRead;
761 	br->ContainerId = ad->ad_container->co_mntobj.ObjectId;
762 	br->BlockNumber = bp->bio_pblkno;
763 	br->ByteCount = bp->bio_bcount;
764 	fib->Header.Size += sizeof(struct aac_blockread);
765 	cm->cm_sgtable = &br->SgMap;
766 	cm->cm_flags |= AAC_CMD_DATAIN;
767     } else {
768 	bw = (struct aac_blockwrite *)&fib->data[0];
769 	bw->Command = VM_CtBlockWrite;
770 	bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
771 	bw->BlockNumber = bp->bio_pblkno;
772 	bw->ByteCount = bp->bio_bcount;
773 	bw->Stable = CUNSTABLE;		/* XXX what's appropriate here? */
774 	fib->Header.Size += sizeof(struct aac_blockwrite);
775 	cm->cm_flags |= AAC_CMD_DATAOUT;
776 	cm->cm_sgtable = &bw->SgMap;
777     }
778 
779     *cmp = cm;
780     return(0);
781 
782 fail:
783     if (bp != NULL)
784 	aac_enqueue_bio(sc, bp);
785     if (cm != NULL)
786 	aac_release_command(cm);
787     return(ENOMEM);
788 }
789 
790 /******************************************************************************
791  * Handle a bio-instigated command that has been completed.
792  */
793 static void
794 aac_bio_complete(struct aac_command *cm)
795 {
796     struct aac_blockread_response	*brr;
797     struct aac_blockwrite_response	*bwr;
798     struct bio				*bp;
799     AAC_FSAStatus			status;
800 
801     /* fetch relevant status and then release the command */
802     bp = (struct bio *)cm->cm_private;
803     if (BIO_IS_READ(bp)) {
804 	brr = (struct aac_blockread_response *)&cm->cm_fib->data[0];
805 	status = brr->Status;
806     } else {
807 	bwr = (struct aac_blockwrite_response *)&cm->cm_fib->data[0];
808 	status = bwr->Status;
809     }
810     aac_release_command(cm);
811 
812     /* fix up the bio based on status */
813     if (status == ST_OK) {
814 	bp->bio_resid = 0;
815     } else {
816 	bp->bio_error = EIO;
817 	bp->bio_flags |= BIO_ERROR;
818 	/* pass an error string out to the disk layer */
819 	bp->bio_driver1 = aac_describe_code(aac_command_status_table, status);
820     }
821     aac_biodone(bp);
822 }
823 
824 /******************************************************************************
825  * Submit a command to the controller, return when it completes.
826  */
827 static int
828 aac_wait_command(struct aac_command *cm, int timeout)
829 {
830     int s, error = 0;
831 
832     debug_called(2);
833 
834     /* Put the command on the ready queue and get things going */
835     aac_enqueue_ready(cm);
836     aac_startio(cm->cm_sc);
837     s = splbio();
838     while(!(cm->cm_flags & AAC_CMD_COMPLETED) && (error != EWOULDBLOCK)) {
839         error = tsleep(cm, PRIBIO, "aacwait", timeout * hz);
840     }
841     splx(s);
842     return(error);
843 }
844 
845 /******************************************************************************
846  ******************************************************************************
847 			Command Buffer Management
848  ******************************************************************************
849  ******************************************************************************/
850 
851 /******************************************************************************
852  * Allocate a command.
853  */
854 static int
855 aac_alloc_command(struct aac_softc *sc, struct aac_command **cmp)
856 {
857     struct aac_command	*cm;
858 
859     debug_called(3);
860 
861     if ((cm = aac_dequeue_free(sc)) == NULL)
862 	return(ENOMEM);
863 
864     *cmp = cm;
865     return(0);
866 }
867 
868 /******************************************************************************
869  * Release a command back to the freelist.
870  */
871 static void
872 aac_release_command(struct aac_command *cm)
873 {
874     debug_called(3);
875 
876     /* (re)initialise the command/FIB */
877     cm->cm_sgtable = NULL;
878     cm->cm_flags = 0;
879     cm->cm_complete = NULL;
880     cm->cm_private = NULL;
881     cm->cm_fib->Header.XferState = AAC_FIBSTATE_EMPTY;
882     cm->cm_fib->Header.StructType = AAC_FIBTYPE_TFIB;
883     cm->cm_fib->Header.Flags = 0;
884     cm->cm_fib->Header.SenderSize = sizeof(struct aac_fib);
885 
886     /*
887      * These are duplicated in aac_start to cover the case where an
888      * intermediate stage may have destroyed them.  They're left
889      * initialised here for debugging purposes only.
890      */
891     cm->cm_fib->Header.SenderFibAddress = (u_int32_t)cm->cm_fib;
892     cm->cm_fib->Header.ReceiverFibAddress = cm->cm_fibphys;
893 
894     aac_enqueue_free(cm);
895 }
896 
897 /******************************************************************************
898  * Map helper for command/FIB allocation.
899  */
900 static void
901 aac_map_command_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
902 {
903     struct aac_softc	*sc = (struct aac_softc *)arg;
904 
905     debug_called(3);
906 
907     sc->aac_fibphys = segs[0].ds_addr;
908 }
909 
910 /******************************************************************************
911  * Allocate and initialise commands/FIBs for this adapter.
912  */
913 static int
914 aac_alloc_commands(struct aac_softc *sc)
915 {
916     struct aac_command		*cm;
917     int				i;
918 
919     debug_called(1);
920 
921     /* allocate the FIBs in DMAable memory and load them */
922     if (bus_dmamem_alloc(sc->aac_fib_dmat, (void **)&sc->aac_fibs,
923 			 BUS_DMA_NOWAIT, &sc->aac_fibmap)) {
924 	return(ENOMEM);
925     }
926     bus_dmamap_load(sc->aac_fib_dmat, sc->aac_fibmap, sc->aac_fibs,
927 		    AAC_FIB_COUNT * sizeof(struct aac_fib),
928 		    aac_map_command_helper, sc, 0);
929 
930     /* initialise constant fields in the command structure */
931     for (i = 0; i < AAC_FIB_COUNT; i++) {
932 	cm = &sc->aac_command[i];
933 	cm->cm_sc = sc;
934 	cm->cm_fib = sc->aac_fibs + i;
935 	cm->cm_fibphys = sc->aac_fibphys + (i * sizeof(struct aac_fib));
936 
937 	if (!bus_dmamap_create(sc->aac_buffer_dmat, 0, &cm->cm_datamap))
938 	    aac_release_command(cm);
939     }
940     return(0);
941 }
942 
943 /******************************************************************************
944  * Free FIBs owned by this adapter.
945  */
946 static void
947 aac_free_commands(struct aac_softc *sc)
948 {
949     int			i;
950 
951     debug_called(1);
952 
953     for (i = 0; i < AAC_FIB_COUNT; i++)
954 	bus_dmamap_destroy(sc->aac_buffer_dmat, sc->aac_command[i].cm_datamap);
955     bus_dmamap_unload(sc->aac_fib_dmat, sc->aac_fibmap);
956     bus_dmamem_free(sc->aac_fib_dmat, sc->aac_fibs, sc->aac_fibmap);
957 }
958 
959 /******************************************************************************
960  * Command-mapping helper function - populate this command's s/g table.
961  */
962 static void
963 aac_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
964 {
965     struct aac_command		*cm = (struct aac_command *)arg;
966     struct aac_fib		*fib = cm->cm_fib;
967     struct aac_sg_table		*sg;
968     int				i;
969 
970     debug_called(3);
971 
972     /* find the s/g table */
973     sg = cm->cm_sgtable;
974 
975     /* copy into the FIB */
976     if (sg != NULL) {
977 	sg->SgCount = nseg;
978 	for (i = 0; i < nseg; i++) {
979 	    sg->SgEntry[i].SgAddress = segs[i].ds_addr;
980 	    sg->SgEntry[i].SgByteCount = segs[i].ds_len;
981 	}
982 	/* update the FIB size for the s/g count */
983 	fib->Header.Size += nseg * sizeof(struct aac_sg_entry);
984     }
985 
986 }
987 
988 /******************************************************************************
989  * Map a command into controller-visible space.
990  */
991 static void
992 aac_map_command(struct aac_command *cm)
993 {
994     struct aac_softc	*sc = cm->cm_sc;
995 
996     debug_called(2);
997 
998     /* don't map more than once */
999     if (cm->cm_flags & AAC_CMD_MAPPED)
1000 	return;
1001 
1002     if (cm->cm_datalen != 0) {
1003 	bus_dmamap_load(sc->aac_buffer_dmat, cm->cm_datamap, cm->cm_data,
1004 			cm->cm_datalen, aac_map_command_sg, cm, 0);
1005 
1006 	if (cm->cm_flags & AAC_CMD_DATAIN)
1007 	    bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1008 			    BUS_DMASYNC_PREREAD);
1009 	if (cm->cm_flags & AAC_CMD_DATAOUT)
1010 	    bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1011 			    BUS_DMASYNC_PREWRITE);
1012     }
1013     cm->cm_flags |= AAC_CMD_MAPPED;
1014 }
1015 
1016 /******************************************************************************
1017  * Unmap a command from controller-visible space.
1018  */
1019 static void
1020 aac_unmap_command(struct aac_command *cm)
1021 {
1022     struct aac_softc	*sc = cm->cm_sc;
1023 
1024     debug_called(2);
1025 
1026     if (!(cm->cm_flags & AAC_CMD_MAPPED))
1027 	return;
1028 
1029     if (cm->cm_datalen != 0) {
1030 	if (cm->cm_flags & AAC_CMD_DATAIN)
1031 	    bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1032 			    BUS_DMASYNC_POSTREAD);
1033 	if (cm->cm_flags & AAC_CMD_DATAOUT)
1034 	    bus_dmamap_sync(sc->aac_buffer_dmat, cm->cm_datamap,
1035 			    BUS_DMASYNC_POSTWRITE);
1036 
1037 	bus_dmamap_unload(sc->aac_buffer_dmat, cm->cm_datamap);
1038     }
1039     cm->cm_flags &= ~AAC_CMD_MAPPED;
1040 }
1041 
1042 /******************************************************************************
1043  ******************************************************************************
1044 				Hardware Interface
1045  ******************************************************************************
1046  ******************************************************************************/
1047 
1048 /******************************************************************************
1049  * Initialise the adapter.
1050  */
1051 static void
1052 aac_common_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1053 {
1054     struct aac_softc	*sc = (struct aac_softc *)arg;
1055 
1056     debug_called(1);
1057 
1058     sc->aac_common_busaddr = segs[0].ds_addr;
1059 }
1060 
1061 static int
1062 aac_init(struct aac_softc *sc)
1063 {
1064     struct aac_adapter_init	*ip;
1065     time_t			then;
1066     u_int32_t			code;
1067     u_int8_t			*qaddr;
1068 
1069     debug_called(1);
1070 
1071     /*
1072      * First wait for the adapter to come ready.
1073      */
1074     then = time_second;
1075     do {
1076 	code = AAC_GET_FWSTATUS(sc);
1077 	if (code & AAC_SELF_TEST_FAILED) {
1078 	    device_printf(sc->aac_dev, "FATAL: selftest failed\n");
1079 	    return(ENXIO);
1080 	}
1081 	if (code & AAC_KERNEL_PANIC) {
1082 	    device_printf(sc->aac_dev, "FATAL: controller kernel panic\n");
1083 	    return(ENXIO);
1084 	}
1085 	if (time_second > (then + AAC_BOOT_TIMEOUT)) {
1086 	    device_printf(sc->aac_dev, "FATAL: controller not coming ready, "
1087 			  "status %x\n", code);
1088 	    return(ENXIO);
1089 	}
1090     } while (!(code & AAC_UP_AND_RUNNING));
1091 
1092     /*
1093      * Create DMA tag for the common structure and allocate it.
1094      */
1095     if (bus_dma_tag_create(sc->aac_parent_dmat, 	/* parent */
1096 			   1, 0, 			/* algnmnt, boundary */
1097 			   BUS_SPACE_MAXADDR,		/* lowaddr */
1098 			   BUS_SPACE_MAXADDR, 		/* highaddr */
1099 			   NULL, NULL, 			/* filter, filterarg */
1100 			   sizeof(struct aac_common), 1,/* maxsize, nsegments */
1101 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1102 			   0,				/* flags */
1103 			   &sc->aac_common_dmat)) {
1104 	device_printf(sc->aac_dev, "can't allocate common structure DMA tag\n");
1105 	return(ENOMEM);
1106     }
1107     if (bus_dmamem_alloc(sc->aac_common_dmat, (void **)&sc->aac_common,
1108 			 BUS_DMA_NOWAIT, &sc->aac_common_dmamap)) {
1109 	device_printf(sc->aac_dev, "can't allocate common structure\n");
1110 	return(ENOMEM);
1111     }
1112     bus_dmamap_load(sc->aac_common_dmat, sc->aac_common_dmamap, sc->aac_common,
1113 		    sizeof(*sc->aac_common), aac_common_map, sc, 0);
1114     bzero(sc->aac_common, sizeof(*sc->aac_common));
1115 
1116     /*
1117      * Fill in the init structure.  This tells the adapter about the physical
1118      * location of various important shared data structures.
1119      */
1120     ip = &sc->aac_common->ac_init;
1121     ip->InitStructRevision = AAC_INIT_STRUCT_REVISION;
1122 
1123     ip->AdapterFibsPhysicalAddress = sc->aac_common_busaddr +
1124 				     offsetof(struct aac_common, ac_fibs);
1125     ip->AdapterFibsVirtualAddress = &sc->aac_common->ac_fibs[0];
1126     ip->AdapterFibsSize = AAC_ADAPTER_FIBS * sizeof(struct aac_fib);
1127     ip->AdapterFibAlign = sizeof(struct aac_fib);
1128 
1129     ip->PrintfBufferAddress = sc->aac_common_busaddr +
1130 			      offsetof(struct aac_common, ac_printf);
1131     ip->PrintfBufferSize = AAC_PRINTF_BUFSIZE;
1132 
1133     ip->HostPhysMemPages = 0;			/* not used? */
1134     ip->HostElapsedSeconds = time_second;	/* reset later if invalid */
1135 
1136     /*
1137      * Initialise FIB queues.  Note that it appears that the layout of the
1138      * indexes and the segmentation of the entries may be mandated by the
1139      * adapter, which is only told about the base of the queue index fields.
1140      *
1141      * The initial values of the indices are assumed to inform the adapter
1142      * of the sizes of the respective queues, and theoretically it could work
1143      * out the entire layout of the queue structures from this.  We take the
1144      * easy route and just lay this area out like everyone else does.
1145      *
1146      * The Linux driver uses a much more complex scheme whereby several header
1147      * records are kept for each queue.  We use a couple of generic list
1148      * manipulation functions which 'know' the size of each list by virtue of a
1149      * table.
1150      */
1151     qaddr = &sc->aac_common->ac_qbuf[0] + AAC_QUEUE_ALIGN;
1152     qaddr -= (u_int32_t)qaddr % AAC_QUEUE_ALIGN;
1153     sc->aac_queues = (struct aac_queue_table *)qaddr;
1154     ip->CommHeaderAddress = sc->aac_common_busaddr + ((u_int32_t)sc->aac_queues
1155 			    - (u_int32_t)sc->aac_common);
1156     bzero(sc->aac_queues, sizeof(struct aac_queue_table));
1157 
1158     sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1159 		AAC_HOST_NORM_CMD_ENTRIES;
1160     sc->aac_queues->qt_qindex[AAC_HOST_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1161 		AAC_HOST_NORM_CMD_ENTRIES;
1162     sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1163 		AAC_HOST_HIGH_CMD_ENTRIES;
1164     sc->aac_queues->qt_qindex[AAC_HOST_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1165 		AAC_HOST_HIGH_CMD_ENTRIES;
1166     sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1167 		AAC_ADAP_NORM_CMD_ENTRIES;
1168     sc->aac_queues->qt_qindex[AAC_ADAP_NORM_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1169 		AAC_ADAP_NORM_CMD_ENTRIES;
1170     sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_PRODUCER_INDEX] =
1171 		AAC_ADAP_HIGH_CMD_ENTRIES;
1172     sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_CMD_QUEUE][AAC_CONSUMER_INDEX] =
1173 		AAC_ADAP_HIGH_CMD_ENTRIES;
1174     sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX] =
1175 		AAC_HOST_NORM_RESP_ENTRIES;
1176     sc->aac_queues->qt_qindex[AAC_HOST_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX] =
1177 		AAC_HOST_NORM_RESP_ENTRIES;
1178     sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX] =
1179 		AAC_HOST_HIGH_RESP_ENTRIES;
1180     sc->aac_queues->qt_qindex[AAC_HOST_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX] =
1181 		AAC_HOST_HIGH_RESP_ENTRIES;
1182     sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_PRODUCER_INDEX] =
1183 		AAC_ADAP_NORM_RESP_ENTRIES;
1184     sc->aac_queues->qt_qindex[AAC_ADAP_NORM_RESP_QUEUE][AAC_CONSUMER_INDEX] =
1185 		AAC_ADAP_NORM_RESP_ENTRIES;
1186     sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_PRODUCER_INDEX] =
1187 		AAC_ADAP_HIGH_RESP_ENTRIES;
1188     sc->aac_queues->qt_qindex[AAC_ADAP_HIGH_RESP_QUEUE][AAC_CONSUMER_INDEX] =
1189 		AAC_ADAP_HIGH_RESP_ENTRIES;
1190     sc->aac_qentries[AAC_HOST_NORM_CMD_QUEUE] =
1191 		&sc->aac_queues->qt_HostNormCmdQueue[0];
1192     sc->aac_qentries[AAC_HOST_HIGH_CMD_QUEUE] =
1193 		&sc->aac_queues->qt_HostHighCmdQueue[0];
1194     sc->aac_qentries[AAC_ADAP_NORM_CMD_QUEUE] =
1195 		&sc->aac_queues->qt_AdapNormCmdQueue[0];
1196     sc->aac_qentries[AAC_ADAP_HIGH_CMD_QUEUE] =
1197 		&sc->aac_queues->qt_AdapHighCmdQueue[0];
1198     sc->aac_qentries[AAC_HOST_NORM_RESP_QUEUE] =
1199 		&sc->aac_queues->qt_HostNormRespQueue[0];
1200     sc->aac_qentries[AAC_HOST_HIGH_RESP_QUEUE] =
1201 		&sc->aac_queues->qt_HostHighRespQueue[0];
1202     sc->aac_qentries[AAC_ADAP_NORM_RESP_QUEUE] =
1203 		&sc->aac_queues->qt_AdapNormRespQueue[0];
1204     sc->aac_qentries[AAC_ADAP_HIGH_RESP_QUEUE] =
1205 		&sc->aac_queues->qt_AdapHighRespQueue[0];
1206 
1207     /*
1208      * Do controller-type-specific initialisation
1209      */
1210     switch (sc->aac_hwif) {
1211     case AAC_HWIF_I960RX:
1212 	AAC_SETREG4(sc, AAC_RX_ODBR, ~0);
1213 	break;
1214     }
1215 
1216     /*
1217      * Give the init structure to the controller.
1218      */
1219     if (aac_sync_command(sc, AAC_MONKER_INITSTRUCT,
1220 			 sc->aac_common_busaddr + offsetof(struct aac_common,
1221 			 ac_init), 0, 0, 0, NULL)) {
1222 	device_printf(sc->aac_dev, "error establishing init structure\n");
1223 	return(EIO);
1224     }
1225 
1226     return(0);
1227 }
1228 
1229 /******************************************************************************
1230  * Send a synchronous command to the controller and wait for a result.
1231  */
1232 static int
1233 aac_sync_command(struct aac_softc *sc, u_int32_t command,
1234 		 u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3,
1235 		 u_int32_t *sp)
1236 {
1237     time_t	then;
1238     u_int32_t	status;
1239 
1240     debug_called(3);
1241 
1242     /* populate the mailbox */
1243     AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3);
1244 
1245     /* ensure the sync command doorbell flag is cleared */
1246     AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
1247 
1248     /* then set it to signal the adapter */
1249     AAC_QNOTIFY(sc, AAC_DB_SYNC_COMMAND);
1250 
1251     /* spin waiting for the command to complete */
1252     then = time_second;
1253     do {
1254 	if (time_second > (then + AAC_IMMEDIATE_TIMEOUT)) {
1255 	    debug(2, "timed out");
1256 	    return(EIO);
1257 	}
1258     } while (!(AAC_GET_ISTATUS(sc) & AAC_DB_SYNC_COMMAND));
1259 
1260     /* clear the completion flag */
1261     AAC_CLEAR_ISTATUS(sc, AAC_DB_SYNC_COMMAND);
1262 
1263     /* get the command status */
1264     status = AAC_GET_MAILBOXSTATUS(sc);
1265     if (sp != NULL)
1266 	*sp = status;
1267     return(0);
1268 }
1269 
1270 /******************************************************************************
1271  * Send a synchronous FIB to the controller and wait for a result.
1272  */
1273 static int
1274 aac_sync_fib(struct aac_softc *sc, u_int32_t command, u_int32_t xferstate,
1275 	     void *data, u_int16_t datasize,
1276 	     void *result, u_int16_t *resultsize)
1277 {
1278     struct aac_fib	*fib = &sc->aac_common->ac_sync_fib;
1279 
1280     debug_called(3);
1281 
1282     if (datasize > AAC_FIB_DATASIZE)
1283 	return(EINVAL);
1284 
1285     /*
1286      * Set up the sync FIB
1287      */
1288     fib->Header.XferState = AAC_FIBSTATE_HOSTOWNED | AAC_FIBSTATE_INITIALISED |
1289 			    AAC_FIBSTATE_EMPTY;
1290     fib->Header.XferState |= xferstate;
1291     fib->Header.Command = command;
1292     fib->Header.StructType = AAC_FIBTYPE_TFIB;
1293     fib->Header.Size = sizeof(struct aac_fib) + datasize;
1294     fib->Header.SenderSize = sizeof(struct aac_fib);
1295     fib->Header.SenderFibAddress = (u_int32_t)fib;
1296     fib->Header.ReceiverFibAddress = sc->aac_common_busaddr +
1297 				     offsetof(struct aac_common, ac_sync_fib);
1298 
1299     /*
1300      * Copy in data.
1301      */
1302     if (data != NULL) {
1303 	bcopy(data, fib->data, datasize);
1304 	fib->Header.XferState |= AAC_FIBSTATE_FROMHOST | AAC_FIBSTATE_NORM;
1305     }
1306 
1307     /*
1308      * Give the FIB to the controller, wait for a response.
1309      */
1310     if (aac_sync_command(sc, AAC_MONKER_SYNCFIB, fib->Header.ReceiverFibAddress,
1311 			 0, 0, 0, NULL)) {
1312 	debug(2, "IO error");
1313 	return(EIO);
1314     }
1315 
1316     /*
1317      * Copy out the result
1318      */
1319     if (result != NULL) {
1320 	*resultsize = fib->Header.Size - sizeof(struct aac_fib_header);
1321 	bcopy(fib->data, result, *resultsize);
1322     }
1323     return(0);
1324 }
1325 
1326 /********************************************************************************
1327  * Adapter-space FIB queue manipulation
1328  *
1329  * Note that the queue implementation here is a little funky; neither the PI or
1330  * CI will ever be zero.  This behaviour is a controller feature.
1331  */
1332 static struct {
1333     int		size;
1334     int		notify;
1335 } aac_qinfo[] = {
1336     {AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL},
1337     {AAC_HOST_HIGH_CMD_ENTRIES, 0},
1338     {AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY},
1339     {AAC_ADAP_HIGH_CMD_ENTRIES, 0},
1340     {AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL},
1341     {AAC_HOST_HIGH_RESP_ENTRIES, 0},
1342     {AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY},
1343     {AAC_ADAP_HIGH_RESP_ENTRIES, 0}
1344 };
1345 
1346 /*
1347  * Atomically insert an entry into the nominated queue, returns 0 on success or
1348  * EBUSY if the queue is full.
1349  *
1350  * Note: it would be more efficient to defer notifying the controller in
1351  *       the case where we may be inserting several entries in rapid succession,
1352  *       but implementing this usefully may be difficult (it would involve a
1353  *       separate queue/notify interface).
1354  */
1355 static int
1356 aac_enqueue_fib(struct aac_softc *sc, int queue, u_int32_t fib_size,
1357 		u_int32_t fib_addr)
1358 {
1359     u_int32_t	pi, ci;
1360     int		s, error;
1361 
1362     debug_called(3);
1363 
1364     s = splbio();
1365 
1366     /* get the producer/consumer indices */
1367     pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1368     ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1369 
1370     /* wrap the queue? */
1371     if (pi >= aac_qinfo[queue].size)
1372 	pi = 0;
1373 
1374     /* check for queue full */
1375     if ((pi + 1) == ci) {
1376 	error = EBUSY;
1377 	goto out;
1378     }
1379 
1380     /* populate queue entry */
1381     (sc->aac_qentries[queue] + pi)->aq_fib_size = fib_size;
1382     (sc->aac_qentries[queue] + pi)->aq_fib_addr = fib_addr;
1383 
1384     /* update producer index */
1385     sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX] = pi + 1;
1386 
1387     /* notify the adapter if we know how */
1388     if (aac_qinfo[queue].notify != 0)
1389 	AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1390 
1391     error = 0;
1392 
1393 out:
1394     splx(s);
1395     return(error);
1396 }
1397 
1398 /*
1399  * Atomically remove one entry from the nominated queue, returns 0 on success or
1400  * ENOENT if the queue is empty.
1401  */
1402 static int
1403 aac_dequeue_fib(struct aac_softc *sc, int queue, u_int32_t *fib_size,
1404 		struct aac_fib **fib_addr)
1405 {
1406     u_int32_t	pi, ci;
1407     int		s, error;
1408 
1409     debug_called(3);
1410 
1411     s = splbio();
1412 
1413     /* get the producer/consumer indices */
1414     pi = sc->aac_queues->qt_qindex[queue][AAC_PRODUCER_INDEX];
1415     ci = sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX];
1416 
1417     /* check for queue empty */
1418     if (ci == pi) {
1419 	error = ENOENT;
1420 	goto out;
1421     }
1422 
1423     /* wrap the queue? */
1424     if (ci >= aac_qinfo[queue].size)
1425 	ci = 0;
1426 
1427     /* fetch the entry */
1428     *fib_size = (sc->aac_qentries[queue] + ci)->aq_fib_size;
1429     *fib_addr = (struct aac_fib *)(sc->aac_qentries[queue] + ci)->aq_fib_addr;
1430 
1431     /* update consumer index */
1432     sc->aac_queues->qt_qindex[queue][AAC_CONSUMER_INDEX] = ci + 1;
1433 
1434     /* if we have made the queue un-full, notify the adapter */
1435     if (((pi + 1) == ci) && (aac_qinfo[queue].notify != 0))
1436 	AAC_QNOTIFY(sc, aac_qinfo[queue].notify);
1437     error = 0;
1438 
1439 out:
1440     splx(s);
1441     return(error);
1442 }
1443 
1444 /******************************************************************************
1445  * Check for commands that have been outstanding for a suspiciously long time,
1446  * and complain about them.
1447  */
1448 static void
1449 aac_timeout(struct aac_softc *sc)
1450 {
1451     int		s;
1452     struct	aac_command *cm;
1453     time_t	deadline;
1454 
1455     /* simulate an interrupt to handle possibly-missed interrupts */
1456     aac_intr(sc);
1457 
1458     /* kick the I/O queue to restart it in the case of deadlock */
1459     aac_startio(sc);
1460 
1461     /* traverse the busy command list, bitch about late commands once only */
1462     deadline = time_second - AAC_CMD_TIMEOUT;
1463     s = splbio();
1464     TAILQ_FOREACH(cm, &sc->aac_busy, cm_link) {
1465 	if ((cm->cm_timestamp < deadline) &&
1466 	    !(cm->cm_flags & AAC_CMD_TIMEDOUT)) {
1467 	    cm->cm_flags |= AAC_CMD_TIMEDOUT;
1468 	    device_printf(sc->aac_dev, "COMMAND TIMED OUT AFTER %d SECONDS\n",
1469 			  (int)(time_second - cm->cm_timestamp));
1470 	    AAC_PRINT_FIB(sc, cm->cm_fib);
1471 	}
1472     }
1473     splx(s);
1474 
1475     /* reset the timer for next time */
1476     timeout((timeout_t*)aac_timeout, sc, AAC_PERIODIC_INTERVAL * hz);
1477     return;
1478 }
1479 
1480 /******************************************************************************
1481  ******************************************************************************
1482 			Interface Function Vectors
1483  ******************************************************************************
1484  ******************************************************************************/
1485 
1486 /******************************************************************************
1487  * Read the current firmware status word.
1488  */
1489 static int
1490 aac_sa_get_fwstatus(struct aac_softc *sc)
1491 {
1492     debug_called(3);
1493 
1494     return(AAC_GETREG4(sc, AAC_SA_FWSTATUS));
1495 }
1496 
1497 static int
1498 aac_rx_get_fwstatus(struct aac_softc *sc)
1499 {
1500     debug_called(3);
1501 
1502     return(AAC_GETREG4(sc, AAC_RX_FWSTATUS));
1503 }
1504 
1505 /******************************************************************************
1506  * Notify the controller of a change in a given queue
1507  */
1508 
1509 static void
1510 aac_sa_qnotify(struct aac_softc *sc, int qbit)
1511 {
1512     debug_called(3);
1513 
1514     AAC_SETREG2(sc, AAC_SA_DOORBELL1_SET, qbit);
1515 }
1516 
1517 static void
1518 aac_rx_qnotify(struct aac_softc *sc, int qbit)
1519 {
1520     debug_called(3);
1521 
1522     AAC_SETREG4(sc, AAC_RX_IDBR, qbit);
1523 }
1524 
1525 /******************************************************************************
1526  * Get the interrupt reason bits
1527  */
1528 static int
1529 aac_sa_get_istatus(struct aac_softc *sc)
1530 {
1531     debug_called(3);
1532 
1533     return(AAC_GETREG2(sc, AAC_SA_DOORBELL0));
1534 }
1535 
1536 static int
1537 aac_rx_get_istatus(struct aac_softc *sc)
1538 {
1539     debug_called(3);
1540 
1541     return(AAC_GETREG4(sc, AAC_RX_ODBR));
1542 }
1543 
1544 /******************************************************************************
1545  * Clear some interrupt reason bits
1546  */
1547 static void
1548 aac_sa_clear_istatus(struct aac_softc *sc, int mask)
1549 {
1550     debug_called(3);
1551 
1552     AAC_SETREG2(sc, AAC_SA_DOORBELL0_CLEAR, mask);
1553 }
1554 
1555 static void
1556 aac_rx_clear_istatus(struct aac_softc *sc, int mask)
1557 {
1558     debug_called(3);
1559 
1560     AAC_SETREG4(sc, AAC_RX_ODBR, mask);
1561 }
1562 
1563 /******************************************************************************
1564  * Populate the mailbox and set the command word
1565  */
1566 static void
1567 aac_sa_set_mailbox(struct aac_softc *sc, u_int32_t command,
1568 		u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
1569 {
1570     debug_called(4);
1571 
1572     AAC_SETREG4(sc, AAC_SA_MAILBOX, command);
1573     AAC_SETREG4(sc, AAC_SA_MAILBOX + 4, arg0);
1574     AAC_SETREG4(sc, AAC_SA_MAILBOX + 8, arg1);
1575     AAC_SETREG4(sc, AAC_SA_MAILBOX + 12, arg2);
1576     AAC_SETREG4(sc, AAC_SA_MAILBOX + 16, arg3);
1577 }
1578 
1579 static void
1580 aac_rx_set_mailbox(struct aac_softc *sc, u_int32_t command,
1581 		u_int32_t arg0, u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
1582 {
1583     debug_called(4);
1584 
1585     AAC_SETREG4(sc, AAC_RX_MAILBOX, command);
1586     AAC_SETREG4(sc, AAC_RX_MAILBOX + 4, arg0);
1587     AAC_SETREG4(sc, AAC_RX_MAILBOX + 8, arg1);
1588     AAC_SETREG4(sc, AAC_RX_MAILBOX + 12, arg2);
1589     AAC_SETREG4(sc, AAC_RX_MAILBOX + 16, arg3);
1590 }
1591 
1592 /******************************************************************************
1593  * Fetch the immediate command status word
1594  */
1595 static int
1596 aac_sa_get_mailboxstatus(struct aac_softc *sc)
1597 {
1598     debug_called(4);
1599 
1600     return(AAC_GETREG4(sc, AAC_SA_MAILBOX));
1601 }
1602 
1603 static int
1604 aac_rx_get_mailboxstatus(struct aac_softc *sc)
1605 {
1606     debug_called(4);
1607 
1608     return(AAC_GETREG4(sc, AAC_RX_MAILBOX));
1609 }
1610 
1611 /******************************************************************************
1612  * Set/clear interrupt masks
1613  */
1614 static void
1615 aac_sa_set_interrupts(struct aac_softc *sc, int enable)
1616 {
1617     debug(2, "%sable interrupts", enable ? "en" : "dis");
1618 
1619     if (enable) {
1620 	AAC_SETREG2((sc), AAC_SA_MASK0_CLEAR, AAC_DB_INTERRUPTS);
1621     } else {
1622 	AAC_SETREG2((sc), AAC_SA_MASK0_SET, ~0);
1623     }
1624 }
1625 
1626 static void
1627 aac_rx_set_interrupts(struct aac_softc *sc, int enable)
1628 {
1629     debug(2, "%sable interrupts", enable ? "en" : "dis");
1630 
1631     if (enable) {
1632 	AAC_SETREG4(sc, AAC_RX_OIMR, ~AAC_DB_INTERRUPTS);
1633     } else {
1634 	AAC_SETREG4(sc, AAC_RX_OIMR, ~0);
1635     }
1636 }
1637 
1638 /******************************************************************************
1639  ******************************************************************************
1640 			Debugging and Diagnostics
1641  ******************************************************************************
1642  ******************************************************************************/
1643 
1644 /******************************************************************************
1645  * Print some information about the controller.
1646  */
1647 static void
1648 aac_describe_controller(struct aac_softc *sc)
1649 {
1650     u_int8_t			buf[AAC_FIB_DATASIZE];	/* XXX really a bit big
1651 							 * for the stack */
1652     u_int16_t			bufsize;
1653     struct aac_adapter_info	*info;
1654     u_int8_t			arg;
1655 
1656     debug_called(2);
1657 
1658     arg = 0;
1659     if (aac_sync_fib(sc, RequestAdapterInfo, 0, &arg, sizeof(arg), &buf,
1660 		     &bufsize)) {
1661 	device_printf(sc->aac_dev, "RequestAdapterInfo failed\n");
1662 	return;
1663     }
1664     if (bufsize != sizeof(*info)) {
1665 	device_printf(sc->aac_dev, "RequestAdapterInfo returned wrong data "
1666 		      "size (%d != %d)\n", bufsize, sizeof(*info));
1667 	/*return;*/
1668     }
1669     info = (struct aac_adapter_info *)&buf[0];
1670 
1671     device_printf(sc->aac_dev, "%s %dMHz, %dMB total memory, %s (%d)\n",
1672 		  aac_describe_code(aac_cpu_variant, info->CpuVariant),
1673 		  info->ClockSpeed, info->TotalMem / (1024 * 1024),
1674 		  aac_describe_code(aac_battery_platform,
1675 		  info->batteryPlatform), info->batteryPlatform);
1676 
1677     /* save the kernel revision structure for later use */
1678     sc->aac_revision = info->KernelRevision;
1679     device_printf(sc->aac_dev, "Kernel %d.%d-%d, S/N %llx\n",
1680 		  info->KernelRevision.external.comp.major,
1681 		  info->KernelRevision.external.comp.minor,
1682 		  info->KernelRevision.external.comp.dash,
1683 		  info->SerialNumber);	/* XXX format? */
1684 }
1685 
1686 /******************************************************************************
1687  * Look up a text description of a numeric error code and return a pointer to
1688  * same.
1689  */
1690 static char *
1691 aac_describe_code(struct aac_code_lookup *table, u_int32_t code)
1692 {
1693     int		i;
1694 
1695     for (i = 0; table[i].string != NULL; i++)
1696 	if (table[i].code == code)
1697 	    return(table[i].string);
1698     return(table[i + 1].string);
1699 }
1700 
1701 /*****************************************************************************
1702  *****************************************************************************
1703 				Management Interface
1704  *****************************************************************************
1705  *****************************************************************************/
1706 
1707 static int
1708 aac_open(dev_t dev, int flags, int fmt, struct proc *p)
1709 {
1710     struct aac_softc	*sc = dev->si_drv1;
1711 
1712     debug_called(2);
1713 
1714     /* Check to make sure the device isn't already open */
1715     if (sc->aac_state & AAC_STATE_OPEN) {
1716         return EBUSY;
1717     }
1718     sc->aac_state |= AAC_STATE_OPEN;
1719 
1720     return 0;
1721 }
1722 
1723 static int
1724 aac_close(dev_t dev, int flags, int fmt, struct proc *p)
1725 {
1726     struct aac_softc	*sc = dev->si_drv1;
1727 
1728     debug_called(2);
1729 
1730     /* Mark this unit as no longer open  */
1731     sc->aac_state &= ~AAC_STATE_OPEN;
1732 
1733     return 0;
1734 }
1735 
1736 static int
1737 aac_ioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p)
1738 {
1739     union aac_statrequest	*as = (union aac_statrequest *)arg;
1740     struct aac_softc		*sc = dev->si_drv1;
1741     int				error = 0;
1742 #ifdef AAC_COMPAT_LINUX
1743     int				i;
1744 #endif
1745 
1746     debug_called(2);
1747 
1748     switch (cmd) {
1749     case AACIO_STATS:
1750 	switch (as->as_item) {
1751 	case AACQ_FREE:
1752 	case AACQ_BIO:
1753 	case AACQ_READY:
1754 	case AACQ_BUSY:
1755 	case AACQ_COMPLETE:
1756 	    bcopy(&sc->aac_qstat[as->as_item], &as->as_qstat,
1757 		  sizeof(struct aac_qstat));
1758 	    break;
1759 	default:
1760 	    error = ENOENT;
1761 	    break;
1762 	}
1763 	break;
1764 
1765 #ifdef AAC_COMPAT_LINUX
1766     case FSACTL_SENDFIB:
1767 	debug(1, "FSACTL_SENDFIB");
1768 	error = aac_ioctl_sendfib(sc, arg);
1769 	break;
1770     case FSACTL_AIF_THREAD:
1771 	debug(1, "FSACTL_AIF_THREAD");
1772 	error = EINVAL;
1773 	break;
1774     case FSACTL_OPEN_GET_ADAPTER_FIB:
1775 	debug(1, "FSACTL_OPEN_GET_ADAPTER_FIB");
1776 	/*
1777 	 * Pass the caller out an AdapterFibContext.
1778 	 *
1779 	 * Note that because we only support one opener, we
1780 	 * basically ignore this.  Set the caller's context to a magic
1781 	 * number just in case.
1782 	 *
1783 	 * The Linux code hands the driver a pointer into kernel space,
1784 	 * and then trusts it when the caller hands it back.  Aiee!
1785 	 */
1786 	i = AAC_AIF_SILLYMAGIC;
1787 	error = copyout(&i, arg, sizeof(i));
1788 	break;
1789     case FSACTL_GET_NEXT_ADAPTER_FIB:
1790 	debug(1, "FSACTL_GET_NEXT_ADAPTER_FIB");
1791 	error = aac_linux_getnext_aif(sc, arg);
1792 	break;
1793     case FSACTL_CLOSE_GET_ADAPTER_FIB:
1794 	debug(1, "FSACTL_CLOSE_GET_ADAPTER_FIB");
1795 	/* don't do anything here */
1796 	break;
1797     case FSACTL_MINIPORT_REV_CHECK:
1798 	debug(1, "FSACTL_MINIPORT_REV_CHECK");
1799 	error = aac_linux_rev_check(sc, arg);
1800 	break;
1801 #endif
1802     default:
1803 	device_printf(sc->aac_dev, "unsupported cmd 0x%lx\n", cmd);
1804 	error = EINVAL;
1805 	break;
1806     }
1807     return(error);
1808 }
1809 
1810 /******************************************************************************
1811  * Send a FIB supplied from userspace
1812  */
1813 static int
1814 aac_ioctl_sendfib(struct aac_softc *sc, caddr_t ufib)
1815 {
1816     struct aac_command 	*cm;
1817     int			size, error;
1818 
1819     debug_called(2);
1820 
1821     cm = NULL;
1822 
1823     /*
1824      * Get a command
1825      */
1826     if (aac_alloc_command(sc, &cm)) {
1827 	error = EBUSY;
1828 	goto out;
1829     }
1830 
1831     /*
1832      * Fetch the FIB header, then re-copy to get data as well.
1833      */
1834     if ((error = copyin(ufib, cm->cm_fib, sizeof(struct aac_fib_header))) != 0)
1835 	goto out;
1836     size = cm->cm_fib->Header.Size + sizeof(struct aac_fib_header);
1837     if (size > sizeof(struct aac_fib)) {
1838 	device_printf(sc->aac_dev, "incoming FIB oversized (%d > %d)\n", size,
1839 		      sizeof(struct aac_fib));
1840 	size = sizeof(struct aac_fib);
1841     }
1842     if ((error = copyin(ufib, cm->cm_fib, size)) != 0)
1843 	goto out;
1844     cm->cm_fib->Header.Size = size;
1845 
1846     /*
1847      * Pass the FIB to the controller, wait for it to complete.
1848      */
1849     if ((error = aac_wait_command(cm, 30)) != 0)	/* XXX user timeout? */
1850 	goto out;
1851 
1852     /*
1853      * Copy the FIB and data back out to the caller.
1854      */
1855     size = cm->cm_fib->Header.Size;
1856     if (size > sizeof(struct aac_fib)) {
1857 	device_printf(sc->aac_dev, "outbound FIB oversized (%d > %d)\n", size,
1858 		      sizeof(struct aac_fib));
1859 	size = sizeof(struct aac_fib);
1860     }
1861     error = copyout(cm->cm_fib, ufib, size);
1862 
1863 out:
1864     if (cm != NULL)
1865 	aac_release_command(cm);
1866     return(error);
1867 }
1868 
1869 /******************************************************************************
1870  * Handle an AIF sent to us by the controller; queue it for later reference.
1871  *
1872  * XXX what's the right thing to do here when the queue is full?  Drop the older
1873  * or newer entries?
1874  */
1875 static void
1876 aac_handle_aif(struct aac_softc *sc, struct aac_aif_command *aif)
1877 {
1878     int		next, s;
1879 
1880     debug_called(2);
1881 
1882     s = splbio();
1883     next = (sc->aac_aifq_head + 1) % AAC_AIFQ_LENGTH;
1884     if (next != sc->aac_aifq_tail) {
1885 	bcopy(aif, &sc->aac_aifq[next], sizeof(struct aac_aif_command));
1886 	sc->aac_aifq_head = next;
1887 	if (sc->aac_state & AAC_STATE_AIF_SLEEPER)
1888 	    wakeup(sc->aac_aifq);
1889     }
1890     splx(s);
1891     aac_print_aif(sc, aif);
1892 }
1893 
1894 /******************************************************************************
1895  ******************************************************************************
1896 			Linux Management Interface
1897  ******************************************************************************
1898  ******************************************************************************/
1899 
1900 #ifdef AAC_COMPAT_LINUX
1901 
1902 #include <sys/proc.h>
1903 #include <machine/../linux/linux.h>
1904 #include <machine/../linux/linux_proto.h>
1905 #include <compat/linux/linux_ioctl.h>
1906 
1907 #define AAC_LINUX_IOCTL_MIN  0x2000
1908 #define AAC_LINUX_IOCTL_MAX  0x21ff
1909 
1910 static linux_ioctl_function_t aac_linux_ioctl;
1911 static struct linux_ioctl_handler aac_handler = {aac_linux_ioctl,
1912 						AAC_LINUX_IOCTL_MIN,
1913 						AAC_LINUX_IOCTL_MAX};
1914 
1915 SYSINIT  (aac_register,   SI_SUB_KLD, SI_ORDER_MIDDLE,
1916 	  linux_ioctl_register_handler, &aac_handler);
1917 SYSUNINIT(aac_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE,
1918 	  linux_ioctl_unregister_handler, &aac_handler);
1919 
1920 MODULE_DEPEND(aac, linux, 1, 1, 1);
1921 
1922 static int
1923 aac_linux_ioctl(struct proc *p, struct linux_ioctl_args *args)
1924 {
1925     struct file		*fp = p->p_fd->fd_ofiles[args->fd];
1926     u_long		cmd = args->cmd;
1927 
1928     /*
1929      * Pass the ioctl off to our standard handler.
1930      */
1931     return(fo_ioctl(fp, cmd, (caddr_t)args->arg, p));
1932 }
1933 
1934 /******************************************************************************
1935  * Return the Revision of the driver to userspace and check to see if the
1936  * userspace app is possibly compatible.  This is extremely bogus right now
1937  * because I have no idea how to handle the versioning of this driver.  It is
1938  * needed, though, to get aaccli working.
1939  */
1940 static int
1941 aac_linux_rev_check(struct aac_softc *sc, caddr_t udata)
1942 {
1943     struct aac_rev_check	rev_check;
1944     struct aac_rev_check_resp	rev_check_resp;
1945     int				error = 0;
1946 
1947     debug_called(2);
1948 
1949     /*
1950      * Copyin the revision struct from userspace
1951      */
1952     if ((error = copyin(udata, (caddr_t)&rev_check,
1953 			sizeof(struct aac_rev_check))) != 0) {
1954 	return error;
1955     }
1956 
1957     debug(2, "Userland revision= %d\n", rev_check.callingRevision.buildNumber);
1958 
1959     /*
1960      * Doctor up the response struct.
1961      */
1962     rev_check_resp.possiblyCompatible = 1;
1963     rev_check_resp.adapterSWRevision.external.ul = sc->aac_revision.external.ul;
1964     rev_check_resp.adapterSWRevision.buildNumber = sc->aac_revision.buildNumber;
1965 
1966     return(copyout((caddr_t)&rev_check_resp, udata,
1967 		   sizeof(struct aac_rev_check_resp)));
1968 }
1969 
1970 /******************************************************************************
1971  * Pass the caller the next AIF in their queue
1972  */
1973 static int
1974 aac_linux_getnext_aif(struct aac_softc *sc, caddr_t arg)
1975 {
1976     struct get_adapter_fib_ioctl	agf;
1977     int					error, s;
1978 
1979     debug_called(2);
1980 
1981     if ((error = copyin(arg, &agf, sizeof(agf))) == 0) {
1982 
1983 	/*
1984 	 * Check the magic number that we gave the caller.
1985 	 */
1986 	if (agf.AdapterFibContext != AAC_AIF_SILLYMAGIC) {
1987 	    error = EFAULT;
1988 	} else {
1989 
1990 	    s = splbio();
1991 	    error = aac_linux_return_aif(sc, agf.AifFib);
1992 
1993 	    if ((error == EAGAIN) && (agf.Wait)) {
1994 		sc->aac_state |= AAC_STATE_AIF_SLEEPER;
1995 		while (error == EAGAIN) {
1996 		    error = tsleep(sc->aac_aifq, PRIBIO | PCATCH, "aacaif", 0);
1997 		    if (error == 0)
1998 			error = aac_linux_return_aif(sc, agf.AifFib);
1999 		}
2000 		sc->aac_state &= ~AAC_STATE_AIF_SLEEPER;
2001 	    }
2002 	    splx(s);
2003 	}
2004     }
2005     return(error);
2006 }
2007 
2008 /******************************************************************************
2009  * Hand the next AIF off the top of the queue out to userspace.
2010  */
2011 static int
2012 aac_linux_return_aif(struct aac_softc *sc, caddr_t uptr)
2013 {
2014     int		error, s;
2015 
2016     debug_called(2);
2017 
2018     s = splbio();
2019     if (sc->aac_aifq_tail == sc->aac_aifq_head) {
2020 	error = EAGAIN;
2021     } else {
2022 	error = copyout(&sc->aac_aifq[sc->aac_aifq_tail], uptr,
2023 			sizeof(struct aac_aif_command));
2024 	if (!error)
2025 	    sc->aac_aifq_tail = (sc->aac_aifq_tail + 1) % AAC_AIFQ_LENGTH;
2026     }
2027     splx(s);
2028     return(error);
2029 }
2030 
2031 
2032 #endif /* AAC_COMPAT_LINUX */
2033