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