2 * Copyright (c) 1999,2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * Copyright (c) 2005 Scott Long
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Copyright (c) 2002 Eric Moore
30 * Copyright (c) 2002, 2004 LSI Logic Corporation
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. The party using or redistributing the source code and binary forms
42 * agrees to the disclaimer below and the terms and conditions set forth
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * $FreeBSD: src/sys/dev/amr/amr.c,v 1.99 2012/08/31 09:42:46 scottl Exp $
61 * Driver for the AMI MegaRaid family of controllers.
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/malloc.h>
67 #include <sys/kernel.h>
69 #include <sys/sysctl.h>
70 #include <sys/sysmsg.h>
77 #include <machine/cpu.h>
80 #include <bus/pci/pcireg.h>
81 #include <bus/pci/pcivar.h>
83 #include <dev/raid/amr/amrio.h>
84 #include <dev/raid/amr/amrreg.h>
85 #include <dev/raid/amr/amrvar.h>
86 #define AMR_DEFINE_TABLES
87 #include <dev/raid/amr/amr_tables.h>
89 SYSCTL_NODE(_hw, OID_AUTO, amr, CTLFLAG_RD, 0, "AMR driver parameters");
91 static d_open_t amr_open;
92 static d_close_t amr_close;
93 static d_ioctl_t amr_ioctl;
95 static struct dev_ops amr_ops = {
102 int linux_no_adapter = 0;
104 * Initialisation, bus interface.
106 static void amr_startup(void *arg);
111 static int amr_query_controller(struct amr_softc *sc);
112 static void *amr_enquiry(struct amr_softc *sc, size_t bufsize,
113 u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual, int *status);
114 static void amr_completeio(struct amr_command *ac);
115 static int amr_support_ext_cdb(struct amr_softc *sc);
118 * Command buffer allocation.
120 static void amr_alloccmd_cluster(struct amr_softc *sc);
121 static void amr_freecmd_cluster(struct amr_command_cluster *acc);
124 * Command processing.
126 static int amr_bio_command(struct amr_softc *sc, struct amr_command **acp);
127 static int amr_wait_command(struct amr_command *ac);
128 static int amr_mapcmd(struct amr_command *ac);
129 static void amr_unmapcmd(struct amr_command *ac);
130 static int amr_start(struct amr_command *ac);
131 static void amr_complete(void *context, ac_qhead_t *head);
132 static void amr_setup_sg(void *arg, bus_dma_segment_t *segs, int nsegments, int error);
133 static void amr_setup_data(void *arg, bus_dma_segment_t *segs, int nsegments, int error);
134 static void amr_setup_ccb(void *arg, bus_dma_segment_t *segs, int nsegments, int error);
135 static void amr_abort_load(struct amr_command *ac);
138 * Interface-specific shims
140 static int amr_quartz_submit_command(struct amr_command *ac);
141 static int amr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave);
142 static int amr_quartz_poll_command(struct amr_command *ac);
143 static int amr_quartz_poll_command1(struct amr_softc *sc, struct amr_command *ac);
145 static int amr_std_submit_command(struct amr_command *ac);
146 static int amr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave);
147 static int amr_std_poll_command(struct amr_command *ac);
148 static void amr_std_attach_mailbox(struct amr_softc *sc);
150 #ifdef AMR_BOARD_INIT
151 static int amr_quartz_init(struct amr_softc *sc);
152 static int amr_std_init(struct amr_softc *sc);
158 static void amr_describe_controller(struct amr_softc *sc);
161 static void amr_printcommand(struct amr_command *ac);
165 static void amr_init_sysctl(struct amr_softc *sc);
166 static int amr_linux_ioctl_int(struct cdev *dev, u_long cmd, caddr_t addr,
167 int32_t flag, struct sysmsg *sm);
169 static MALLOC_DEFINE(M_AMR, "amr", "AMR memory");
171 /********************************************************************************
172 ********************************************************************************
174 ********************************************************************************
175 ********************************************************************************/
177 /********************************************************************************
178 ********************************************************************************
180 ********************************************************************************
181 ********************************************************************************/
183 /********************************************************************************
184 * Initialise the controller and softc.
187 amr_attach(struct amr_softc *sc)
194 * Initialise per-controller queues.
196 amr_init_qhead(&sc->amr_freecmds);
197 amr_init_qhead(&sc->amr_ready);
198 TAILQ_INIT(&sc->amr_cmd_clusters);
199 bioq_init(&sc->amr_bioq);
201 debug(2, "queue init done");
204 * Configure for this controller type.
206 if (AMR_IS_QUARTZ(sc)) {
207 sc->amr_submit_command = amr_quartz_submit_command;
208 sc->amr_get_work = amr_quartz_get_work;
209 sc->amr_poll_command = amr_quartz_poll_command;
210 sc->amr_poll_command1 = amr_quartz_poll_command1;
212 sc->amr_submit_command = amr_std_submit_command;
213 sc->amr_get_work = amr_std_get_work;
214 sc->amr_poll_command = amr_std_poll_command;
215 amr_std_attach_mailbox(sc);
218 #ifdef AMR_BOARD_INIT
219 if ((AMR_IS_QUARTZ(sc) ? amr_quartz_init(sc) : amr_std_init(sc)))
224 * Allocate initial commands.
226 amr_alloccmd_cluster(sc);
229 * Quiz controller for features and limits.
231 if (amr_query_controller(sc))
234 debug(2, "controller query complete");
237 * preallocate the remaining commands.
239 while (sc->amr_nextslot < sc->amr_maxio)
240 amr_alloccmd_cluster(sc);
245 sysctl_ctx_init(&sc->amr_sysctl_ctx);
246 sc->amr_sysctl_tree = SYSCTL_ADD_NODE(&sc->amr_sysctl_ctx,
247 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
248 device_get_nameunit(sc->amr_dev), CTLFLAG_RD, 0, "");
249 if (sc->amr_sysctl_tree == NULL) {
250 device_printf(sc->amr_dev, "can't add sysctl node\n");
256 * Attach our 'real' SCSI channels to CAM.
258 child = device_add_child(sc->amr_dev, "amrp", -1);
259 sc->amr_pass = child;
261 device_set_softc(child, sc);
262 device_set_desc(child, "SCSI Passthrough Bus");
263 bus_generic_attach(sc->amr_dev);
267 * Create the control device.
269 sc->amr_dev_t = make_dev(&amr_ops, device_get_unit(sc->amr_dev), UID_ROOT, GID_OPERATOR,
270 S_IRUSR | S_IWUSR, "amr%d", device_get_unit(sc->amr_dev));
271 sc->amr_dev_t->si_drv1 = sc;
273 if (device_get_unit(sc->amr_dev) == 0)
274 make_dev_alias(sc->amr_dev_t, "megadev0");
277 * Schedule ourselves to bring the controller up once interrupts are
280 bzero(&sc->amr_ich, sizeof(struct intr_config_hook));
281 sc->amr_ich.ich_func = amr_startup;
282 sc->amr_ich.ich_arg = sc;
283 sc->amr_ich.ich_desc = "amr";
284 if (config_intrhook_establish(&sc->amr_ich) != 0) {
285 device_printf(sc->amr_dev, "can't establish configuration hook\n");
290 * Print a little information about the controller.
292 amr_describe_controller(sc);
294 debug(2, "attach complete");
298 /********************************************************************************
299 * Locate disk resources and attach children to them.
302 amr_startup(void *arg)
304 struct amr_softc *sc = (struct amr_softc *)arg;
305 struct amr_logdrive *dr;
310 /* pull ourselves off the intrhook chain */
311 if (sc->amr_ich.ich_func)
312 config_intrhook_disestablish(&sc->amr_ich);
313 sc->amr_ich.ich_func = NULL;
315 /* get up-to-date drive information */
316 if (amr_query_controller(sc)) {
317 device_printf(sc->amr_dev, "can't scan controller for drives\n");
321 /* iterate over available drives */
322 for (i = 0, dr = &sc->amr_drive[0]; (i < AMR_MAXLD) && (dr->al_size != 0xffffffff); i++, dr++) {
323 /* are we already attached to this drive? */
324 if (dr->al_disk == 0) {
325 /* generate geometry information */
326 if (dr->al_size > 0x200000) { /* extended translation? */
333 dr->al_cylinders = dr->al_size / (dr->al_heads * dr->al_sectors);
335 dr->al_disk = device_add_child(sc->amr_dev, NULL, -1);
336 if (dr->al_disk == 0)
337 device_printf(sc->amr_dev, "device_add_child failed\n");
338 device_set_ivars(dr->al_disk, dr);
342 if ((error = bus_generic_attach(sc->amr_dev)) != 0)
343 device_printf(sc->amr_dev, "bus_generic_attach returned %d\n", error);
345 /* mark controller back up */
346 sc->amr_state &= ~AMR_STATE_SHUTDOWN;
348 /* interrupts will be enabled before we do anything more */
349 sc->amr_state |= AMR_STATE_INTEN;
355 amr_init_sysctl(struct amr_softc *sc)
358 SYSCTL_ADD_INT(&sc->amr_sysctl_ctx,
359 SYSCTL_CHILDREN(sc->amr_sysctl_tree),
360 OID_AUTO, "allow_volume_configure", CTLFLAG_RW, &sc->amr_allow_vol_config, 0,
362 SYSCTL_ADD_INT(&sc->amr_sysctl_ctx,
363 SYSCTL_CHILDREN(sc->amr_sysctl_tree),
364 OID_AUTO, "nextslot", CTLFLAG_RD, &sc->amr_nextslot, 0,
366 SYSCTL_ADD_INT(&sc->amr_sysctl_ctx,
367 SYSCTL_CHILDREN(sc->amr_sysctl_tree),
368 OID_AUTO, "busyslots", CTLFLAG_RD, &sc->amr_busyslots, 0,
370 SYSCTL_ADD_INT(&sc->amr_sysctl_ctx,
371 SYSCTL_CHILDREN(sc->amr_sysctl_tree),
372 OID_AUTO, "maxio", CTLFLAG_RD, &sc->amr_maxio, 0,
377 /*******************************************************************************
378 * Free resources associated with a controller instance
381 amr_free(struct amr_softc *sc)
383 struct amr_command_cluster *acc;
385 /* detach from CAM */
386 if (sc->amr_pass != NULL)
387 device_delete_child(sc->amr_dev, sc->amr_pass);
389 /* throw away any command buffers */
390 while ((acc = TAILQ_FIRST(&sc->amr_cmd_clusters)) != NULL) {
391 TAILQ_REMOVE(&sc->amr_cmd_clusters, acc, acc_link);
392 amr_freecmd_cluster(acc);
395 /* destroy control device */
396 if(sc->amr_dev_t != NULL)
397 destroy_dev(sc->amr_dev_t);
398 dev_ops_remove_minor(&amr_ops, device_get_unit(sc->amr_dev));
400 #if 0 /* XXX swildner */
401 if (mtx_initialized(&sc->amr_hw_lock))
402 mtx_destroy(&sc->amr_hw_lock);
404 if (mtx_initialized(&sc->amr_list_lock))
405 mtx_destroy(&sc->amr_list_lock);
408 if (sc->amr_sysctl_tree != NULL)
409 sysctl_ctx_free(&sc->amr_sysctl_ctx);
411 lockuninit(&sc->amr_hw_lock);
412 lockuninit(&sc->amr_list_lock);
415 /*******************************************************************************
416 * Receive a bio structure from a child device and queue it on a particular
417 * disk resource, then poke the disk resource to start as much work as it can.
420 amr_submit_bio(struct amr_softc *sc, struct bio *bio)
424 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
425 amr_enqueue_bio(sc, bio);
427 lockmgr(&sc->amr_list_lock, LK_RELEASE);
431 /********************************************************************************
432 * Accept an open operation on the control device.
435 amr_open(struct dev_open_args *ap)
437 cdev_t dev = ap->a_head.a_dev;
438 int unit = minor(dev);
439 struct amr_softc *sc = devclass_get_softc(devclass_find("amr"), unit);
443 sc->amr_state |= AMR_STATE_OPEN;
447 /********************************************************************************
448 * Accept the last close on the control device.
451 amr_close(struct dev_close_args *ap)
453 cdev_t dev = ap->a_head.a_dev;
454 int unit = minor(dev);
455 struct amr_softc *sc = devclass_get_softc(devclass_find("amr"), unit);
459 sc->amr_state &= ~AMR_STATE_OPEN;
463 /********************************************************************************
464 * Handle controller-specific control operations.
467 amr_rescan_drives(struct cdev *dev)
469 struct amr_softc *sc = (struct amr_softc *)dev->si_drv1;
472 sc->amr_state |= AMR_STATE_REMAP_LD;
473 while (sc->amr_busyslots) {
474 device_printf(sc->amr_dev, "idle controller\n");
478 /* mark ourselves as in-shutdown */
479 sc->amr_state |= AMR_STATE_SHUTDOWN;
481 /* flush controller */
482 device_printf(sc->amr_dev, "flushing cache...");
483 kprintf("%s\n", amr_flush(sc) ? "failed" : "done");
485 /* delete all our child devices */
486 for(i = 0 ; i < AMR_MAXLD; i++) {
487 if(sc->amr_drive[i].al_disk != 0) {
488 if((error = device_delete_child(sc->amr_dev,
489 sc->amr_drive[i].al_disk)) != 0)
492 sc->amr_drive[i].al_disk = 0;
501 * Bug-for-bug compatibility with Linux!
502 * Some apps will send commands with inlen and outlen set to 0,
503 * even though they expect data to be transfered to them from the
504 * card. Linux accidentally allows this by allocating a 4KB
505 * buffer for the transfer anyways, but it then throws it away
506 * without copying it back to the app.
508 * The amr(4) firmware relies on this feature. In fact, it assumes
509 * the buffer is always a power of 2 up to a max of 64k. There is
510 * also at least one case where it assumes a buffer less than 16k is
511 * greater than 16k. Force a minimum buffer size of 32k and round
512 * sizes between 32k and 64k up to 64k as a workaround.
515 amr_ioctl_buffer_length(unsigned long len)
518 if (len <= 32 * 1024)
520 if (len <= 64 * 1024)
526 amr_linux_ioctl_int(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag,
529 struct amr_softc *sc = (struct amr_softc *)dev->si_drv1;
530 struct amr_command *ac;
531 struct amr_mailbox *mb;
532 struct amr_linux_ioctl ali;
535 int len, ac_flags = 0;
536 int logical_drives_changed = 0;
537 u_int32_t linux_version = 0x02100000;
539 struct amr_passthrough *ap; /* 60 bytes */
546 if ((error = copyin(addr, &ali, sizeof(ali))) != 0)
548 switch (ali.ui.fcs.opcode) {
550 switch(ali.ui.fcs.subopcode) {
552 copyout(&linux_version, (void *)(uintptr_t)ali.data,
553 sizeof(linux_version));
558 copyout(&linux_no_adapter, (void *)(uintptr_t)ali.data,
559 sizeof(linux_no_adapter));
560 sm->sm_result.iresult = linux_no_adapter;
565 kprintf("Unknown subopcode\n");
573 if (ali.ui.fcs.opcode == 0x80)
574 len = max(ali.outlen, ali.inlen);
576 len = ali.ui.fcs.length;
578 mb = (void *)&ali.mbox[0];
580 if ((ali.mbox[0] == FC_DEL_LOGDRV && ali.mbox[2] == OP_DEL_LOGDRV) || /* delete */
581 (ali.mbox[0] == AMR_CMD_CONFIG && ali.mbox[2] == 0x0d)) { /* create */
582 if (sc->amr_allow_vol_config == 0) {
586 logical_drives_changed = 1;
589 if (ali.mbox[0] == AMR_CMD_PASS) {
590 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
591 while ((ac = amr_alloccmd(sc)) == NULL)
592 lksleep(sc, &sc->amr_list_lock, 0, "amrioc", hz);
593 lockmgr(&sc->amr_list_lock, LK_RELEASE);
594 ap = &ac->ac_ccb->ccb_pthru;
596 error = copyin((void *)(uintptr_t)mb->mb_physaddr, ap,
597 sizeof(struct amr_passthrough));
601 if (ap->ap_data_transfer_length)
602 dp = kmalloc(ap->ap_data_transfer_length, M_AMR,
606 error = copyin((void *)(uintptr_t)ap->ap_data_transfer_address,
607 dp, ap->ap_data_transfer_length);
612 ac_flags = AMR_CMD_DATAIN|AMR_CMD_DATAOUT|AMR_CMD_CCB;
613 bzero(&ac->ac_mailbox, sizeof(ac->ac_mailbox));
614 ac->ac_mailbox.mb_command = AMR_CMD_PASS;
615 ac->ac_flags = ac_flags;
618 ac->ac_length = ap->ap_data_transfer_length;
619 temp = (void *)(uintptr_t)ap->ap_data_transfer_address;
621 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
622 error = amr_wait_command(ac);
623 lockmgr(&sc->amr_list_lock, LK_RELEASE);
627 status = ac->ac_status;
628 error = copyout(&status, &((struct amr_passthrough *)(uintptr_t)mb->mb_physaddr)->ap_scsi_status, sizeof(status));
633 error = copyout(dp, temp, ap->ap_data_transfer_length);
637 error = copyout(ap->ap_request_sense_area, ((struct amr_passthrough *)(uintptr_t)mb->mb_physaddr)->ap_request_sense_area, ap->ap_request_sense_length);
643 } else if (ali.mbox[0] == AMR_CMD_PASS_64) {
644 kprintf("No AMR_CMD_PASS_64\n");
647 } else if (ali.mbox[0] == AMR_CMD_EXTPASS) {
648 kprintf("No AMR_CMD_EXTPASS\n");
652 len = amr_ioctl_buffer_length(imax(ali.inlen, ali.outlen));
654 dp = kmalloc(len, M_AMR, M_WAITOK | M_ZERO);
657 error = copyin((void *)(uintptr_t)mb->mb_physaddr, dp, len);
662 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
663 while ((ac = amr_alloccmd(sc)) == NULL)
664 lksleep(sc, &sc->amr_list_lock, 0, "amrioc", hz);
666 ac_flags = AMR_CMD_DATAIN|AMR_CMD_DATAOUT;
667 bzero(&ac->ac_mailbox, sizeof(ac->ac_mailbox));
668 bcopy(&ali.mbox[0], &ac->ac_mailbox, sizeof(ali.mbox));
672 ac->ac_flags = ac_flags;
674 error = amr_wait_command(ac);
675 lockmgr(&sc->amr_list_lock, LK_RELEASE);
679 status = ac->ac_status;
680 error = copyout(&status, &((struct amr_mailbox *)&((struct amr_linux_ioctl *)addr)->mbox[0])->mb_status, sizeof(status));
682 error = copyout(dp, (void *)(uintptr_t)mb->mb_physaddr, ali.outlen);
688 if (logical_drives_changed)
689 amr_rescan_drives(dev);
695 debug(1, "unknown linux ioctl 0x%lx", cmd);
696 kprintf("unknown linux ioctl 0x%lx\n", cmd);
702 * At this point, we know that there is a lock held and that these
703 * objects have been allocated.
705 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
708 lockmgr(&sc->amr_list_lock, LK_RELEASE);
715 amr_ioctl(struct dev_ioctl_args *ap)
717 cdev_t dev = ap->a_head.a_dev;
718 caddr_t addr = ap->a_data;
719 u_long cmd = ap->a_cmd;
720 struct amr_softc *sc = (struct amr_softc *)dev->si_drv1;
723 struct amr_user_ioctl *au;
724 #ifdef AMR_IO_COMMAND32
725 struct amr_user_ioctl32 *au32;
729 struct amr_command *ac;
730 struct amr_mailbox_ioctl *mbi;
731 void *dp, *au_buffer;
732 unsigned long au_length, real_length;
733 unsigned char *au_cmd;
736 struct amr_passthrough *_ap; /* 60 bytes */
737 int logical_drives_changed = 0;
741 arg._p = (void *)addr;
751 debug(1, "AMR_IO_VERSION");
752 *arg.result = AMR_IO_VERSION_NUMBER;
755 #ifdef AMR_IO_COMMAND32
757 * Accept ioctl-s from 32-bit binaries on non-32-bit
758 * platforms, such as AMD. LSI's MEGAMGR utility is
759 * the only example known today... -mi
761 case AMR_IO_COMMAND32:
762 debug(1, "AMR_IO_COMMAND32 0x%x", arg.au32->au_cmd[0]);
763 au_cmd = arg.au32->au_cmd;
764 au_buffer = (void *)(u_int64_t)arg.au32->au_buffer;
765 au_length = arg.au32->au_length;
766 au_statusp = &arg.au32->au_status;
771 debug(1, "AMR_IO_COMMAND 0x%x", arg.au->au_cmd[0]);
772 au_cmd = arg.au->au_cmd;
773 au_buffer = (void *)arg.au->au_buffer;
774 au_length = arg.au->au_length;
775 au_statusp = &arg.au->au_status;
779 case 0xc06e6d00: /* Linux emulation */
782 struct amr_linux_ioctl ali;
785 devclass = devclass_find("amr");
786 if (devclass == NULL)
789 error = copyin(addr, &ali, sizeof(ali));
792 if (ali.ui.fcs.opcode == 0x82)
795 adapter = (ali.ui.fcs.adapno) ^ 'm' << 8;
797 sc = devclass_get_softc(devclass, adapter);
801 return (amr_linux_ioctl_int(sc->amr_dev_t, cmd, addr, 0, ap->a_sysmsg));
804 debug(1, "unknown ioctl 0x%lx", cmd);
808 if ((au_cmd[0] == FC_DEL_LOGDRV && au_cmd[1] == OP_DEL_LOGDRV) || /* delete */
809 (au_cmd[0] == AMR_CMD_CONFIG && au_cmd[1] == 0x0d)) { /* create */
810 if (sc->amr_allow_vol_config == 0) {
814 logical_drives_changed = 1;
817 /* handle inbound data buffer */
818 real_length = amr_ioctl_buffer_length(au_length);
819 if (au_length != 0 && au_cmd[0] != 0x06) {
820 if ((dp = kmalloc(real_length, M_AMR, M_WAITOK|M_ZERO)) == NULL) {
824 if ((error = copyin(au_buffer, dp, au_length)) != 0) {
828 debug(2, "copyin %ld bytes from %p -> %p", au_length, au_buffer, dp);
831 /* Allocate this now before the mutex gets held */
833 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
834 while ((ac = amr_alloccmd(sc)) == NULL)
835 lksleep(sc, &sc->amr_list_lock, 0, "amrioc", hz);
837 /* handle SCSI passthrough command */
838 if (au_cmd[0] == AMR_CMD_PASS) {
841 _ap = &ac->ac_ccb->ccb_pthru;
842 bzero(_ap, sizeof(struct amr_passthrough));
846 _ap->ap_cdb_length = len;
847 bcopy(au_cmd + 3, _ap->ap_cdb, len);
849 /* build passthrough */
850 _ap->ap_timeout = au_cmd[len + 3] & 0x07;
851 _ap->ap_ars = (au_cmd[len + 3] & 0x08) ? 1 : 0;
852 _ap->ap_islogical = (au_cmd[len + 3] & 0x80) ? 1 : 0;
853 _ap->ap_logical_drive_no = au_cmd[len + 4];
854 _ap->ap_channel = au_cmd[len + 5];
855 _ap->ap_scsi_id = au_cmd[len + 6];
856 _ap->ap_request_sense_length = 14;
857 _ap->ap_data_transfer_length = au_length;
858 /* XXX what about the request-sense area? does the caller want it? */
861 ac->ac_mailbox.mb_command = AMR_CMD_PASS;
862 ac->ac_flags = AMR_CMD_CCB;
865 /* direct command to controller */
866 mbi = (struct amr_mailbox_ioctl *)&ac->ac_mailbox;
868 /* copy pertinent mailbox items */
869 mbi->mb_command = au_cmd[0];
870 mbi->mb_channel = au_cmd[1];
871 mbi->mb_param = au_cmd[2];
872 mbi->mb_pad[0] = au_cmd[3];
873 mbi->mb_drive = au_cmd[4];
877 /* build the command */
879 ac->ac_length = real_length;
880 ac->ac_flags |= AMR_CMD_DATAIN|AMR_CMD_DATAOUT;
882 /* run the command */
883 error = amr_wait_command(ac);
884 lockmgr(&sc->amr_list_lock, LK_RELEASE);
888 /* copy out data and set status */
889 if (au_length != 0) {
890 error = copyout(dp, au_buffer, au_length);
892 debug(2, "copyout %ld bytes from %p -> %p", au_length, dp, au_buffer);
894 debug(2, "%p status 0x%x", dp, ac->ac_status);
895 *au_statusp = ac->ac_status;
899 * At this point, we know that there is a lock held and that these
900 * objects have been allocated.
902 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
905 lockmgr(&sc->amr_list_lock, LK_RELEASE);
909 if (logical_drives_changed)
910 amr_rescan_drives(dev);
915 /********************************************************************************
916 ********************************************************************************
918 ********************************************************************************
919 ********************************************************************************/
921 /********************************************************************************
922 * Interrogate the controller for the operational parameters we require.
925 amr_query_controller(struct amr_softc *sc)
927 struct amr_enquiry3 *aex;
928 struct amr_prodinfo *ap;
929 struct amr_enquiry *ae;
934 * Greater than 10 byte cdb support
936 sc->support_ext_cdb = amr_support_ext_cdb(sc);
938 if(sc->support_ext_cdb) {
939 debug(2,"supports extended CDBs.");
943 * Try to issue an ENQUIRY3 command
945 if ((aex = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_ENQ3,
946 AMR_CONFIG_ENQ3_SOLICITED_FULL, &status)) != NULL) {
949 * Fetch current state of logical drives.
951 for (ldrv = 0; ldrv < aex->ae_numldrives; ldrv++) {
952 sc->amr_drive[ldrv].al_size = aex->ae_drivesize[ldrv];
953 sc->amr_drive[ldrv].al_state = aex->ae_drivestate[ldrv];
954 sc->amr_drive[ldrv].al_properties = aex->ae_driveprop[ldrv];
955 debug(2, " drive %d: %d state %x properties %x", ldrv, sc->amr_drive[ldrv].al_size,
956 sc->amr_drive[ldrv].al_state, sc->amr_drive[ldrv].al_properties);
961 * Get product info for channel count.
963 if ((ap = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0, &status)) == NULL) {
964 device_printf(sc->amr_dev, "can't obtain product data from controller\n");
967 sc->amr_maxdrives = 40;
968 sc->amr_maxchan = ap->ap_nschan;
969 sc->amr_maxio = ap->ap_maxio;
970 sc->amr_type |= AMR_TYPE_40LD;
973 ap = amr_enquiry(sc, 0, FC_DEL_LOGDRV, OP_SUP_DEL_LOGDRV, 0, &status);
977 sc->amr_ld_del_supported = 1;
978 device_printf(sc->amr_dev, "delete logical drives supported by controller\n");
982 /* failed, try the 8LD ENQUIRY commands */
983 if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_EXT_ENQUIRY2, 0, 0, &status)) == NULL) {
984 if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0, &status)) == NULL) {
985 device_printf(sc->amr_dev, "can't obtain configuration data from controller\n");
988 ae->ae_signature = 0;
992 * Fetch current state of logical drives.
994 for (ldrv = 0; ldrv < ae->ae_ldrv.al_numdrives; ldrv++) {
995 sc->amr_drive[ldrv].al_size = ae->ae_ldrv.al_size[ldrv];
996 sc->amr_drive[ldrv].al_state = ae->ae_ldrv.al_state[ldrv];
997 sc->amr_drive[ldrv].al_properties = ae->ae_ldrv.al_properties[ldrv];
998 debug(2, " drive %d: %d state %x properties %x\n", ldrv, sc->amr_drive[ldrv].al_size,
999 sc->amr_drive[ldrv].al_state, sc->amr_drive[ldrv].al_properties);
1002 sc->amr_maxdrives = 8;
1003 sc->amr_maxchan = ae->ae_adapter.aa_channels;
1004 sc->amr_maxio = ae->ae_adapter.aa_maxio;
1009 * Mark remaining drives as unused.
1011 for (; ldrv < AMR_MAXLD; ldrv++)
1012 sc->amr_drive[ldrv].al_size = 0xffffffff;
1015 * Cap the maximum number of outstanding I/Os. AMI's Linux driver doesn't trust
1016 * the controller's reported value, and lockups have been seen when we do.
1018 sc->amr_maxio = imin(sc->amr_maxio, AMR_LIMITCMD);
1023 /********************************************************************************
1024 * Run a generic enquiry-style command.
1027 amr_enquiry(struct amr_softc *sc, size_t bufsize, u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual, int *status)
1029 struct amr_command *ac;
1039 /* get ourselves a command buffer */
1040 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
1041 ac = amr_alloccmd(sc);
1042 lockmgr(&sc->amr_list_lock, LK_RELEASE);
1045 /* allocate the response structure */
1046 if ((result = kmalloc(bufsize, M_AMR, M_ZERO|M_NOWAIT)) == NULL)
1048 /* set command flags */
1050 ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAIN;
1052 /* point the command at our data */
1053 ac->ac_data = result;
1054 ac->ac_length = bufsize;
1056 /* build the command proper */
1057 mbox = (u_int8_t *)&ac->ac_mailbox; /* XXX want a real structure for this? */
1063 /* can't assume that interrupts are going to work here, so play it safe */
1064 if (sc->amr_poll_command(ac))
1066 error = ac->ac_status;
1067 *status = ac->ac_status;
1070 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
1073 lockmgr(&sc->amr_list_lock, LK_RELEASE);
1074 if ((error != 0) && (result != NULL)) {
1075 kfree(result, M_AMR);
1081 /********************************************************************************
1082 * Flush the controller's internal cache, return status.
1085 amr_flush(struct amr_softc *sc)
1087 struct amr_command *ac;
1090 /* get ourselves a command buffer */
1092 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
1093 ac = amr_alloccmd(sc);
1094 lockmgr(&sc->amr_list_lock, LK_RELEASE);
1097 /* set command flags */
1098 ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
1100 /* build the command proper */
1101 ac->ac_mailbox.mb_command = AMR_CMD_FLUSH;
1103 /* we have to poll, as the system may be going down or otherwise damaged */
1104 if (sc->amr_poll_command(ac))
1106 error = ac->ac_status;
1109 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
1112 lockmgr(&sc->amr_list_lock, LK_RELEASE);
1116 /********************************************************************************
1117 * Detect extented cdb >> greater than 10 byte cdb support
1118 * returns '1' means this support exist
1119 * returns '0' means this support doesn't exist
1122 amr_support_ext_cdb(struct amr_softc *sc)
1124 struct amr_command *ac;
1128 /* get ourselves a command buffer */
1130 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
1131 ac = amr_alloccmd(sc);
1132 lockmgr(&sc->amr_list_lock, LK_RELEASE);
1135 /* set command flags */
1136 ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
1138 /* build the command proper */
1139 mbox = (u_int8_t *)&ac->ac_mailbox; /* XXX want a real structure for this? */
1144 /* we have to poll, as the system may be going down or otherwise damaged */
1145 if (sc->amr_poll_command(ac))
1147 if( ac->ac_status == AMR_STATUS_SUCCESS ) {
1152 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
1155 lockmgr(&sc->amr_list_lock, LK_RELEASE);
1159 /********************************************************************************
1160 * Try to find I/O work for the controller from one or more of the work queues.
1162 * We make the assumption that if the controller is not ready to take a command
1163 * at some given time, it will generate an interrupt at some later time when
1167 amr_startio(struct amr_softc *sc)
1169 struct amr_command *ac;
1171 /* spin until something prevents us from doing any work */
1174 /* Don't bother to queue commands no bounce buffers are available. */
1175 if (sc->amr_state & AMR_STATE_QUEUE_FRZN)
1178 /* try to get a ready command */
1179 ac = amr_dequeue_ready(sc);
1181 /* if that failed, build a command from a bio */
1183 (void)amr_bio_command(sc, &ac);
1185 /* if that failed, build a command from a ccb */
1186 if ((ac == NULL) && (sc->amr_cam_command != NULL))
1187 sc->amr_cam_command(sc, &ac);
1189 /* if we don't have anything to do, give up */
1193 /* try to give the command to the controller; if this fails save it for later and give up */
1194 if (amr_start(ac)) {
1195 debug(2, "controller busy, command deferred");
1196 amr_requeue_ready(ac); /* XXX schedule retry very soon? */
1202 /********************************************************************************
1203 * Handle completion of an I/O command.
1206 amr_completeio(struct amr_command *ac)
1208 struct amr_softc *sc = ac->ac_sc;
1209 static struct timeval lastfail;
1211 struct buf *bp = ac->ac_bio->bio_buf;
1213 if (ac->ac_status != AMR_STATUS_SUCCESS) { /* could be more verbose here? */
1215 bp->b_flags |= B_ERROR;
1217 if (ppsratecheck(&lastfail, &curfail, 1))
1218 device_printf(sc->amr_dev, "I/O error - 0x%x\n", ac->ac_status);
1219 /* amr_printcommand(ac);*/
1221 amrd_intr(ac->ac_bio);
1222 lockmgr(&ac->ac_sc->amr_list_lock, LK_EXCLUSIVE);
1224 lockmgr(&ac->ac_sc->amr_list_lock, LK_RELEASE);
1227 /********************************************************************************
1228 ********************************************************************************
1230 ********************************************************************************
1231 ********************************************************************************/
1233 /********************************************************************************
1234 * Convert a bio off the top of the bio queue into a command.
1237 amr_bio_command(struct amr_softc *sc, struct amr_command **acp)
1239 struct amr_command *ac;
1240 struct amrd_softc *amrd;
1252 if ((ac = amr_alloccmd(sc)) == NULL)
1255 /* get a bio to work on */
1256 if ((bio = amr_dequeue_bio(sc)) == NULL) {
1261 /* connect the bio to the command */
1263 ac->ac_complete = amr_completeio;
1265 ac->ac_data = bp->b_data;
1266 ac->ac_length = bp->b_bcount;
1268 switch (bp->b_cmd) {
1270 ac->ac_flags |= AMR_CMD_DATAIN;
1271 if (AMR_IS_SG64(sc)) {
1272 cmd = AMR_CMD_LREAD64;
1273 ac->ac_flags |= AMR_CMD_SG64;
1275 cmd = AMR_CMD_LREAD;
1278 ac->ac_flags |= AMR_CMD_DATAOUT;
1279 if (AMR_IS_SG64(sc)) {
1280 cmd = AMR_CMD_LWRITE64;
1281 ac->ac_flags |= AMR_CMD_SG64;
1283 cmd = AMR_CMD_LWRITE;
1286 ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
1287 cmd = AMR_CMD_FLUSH;
1290 panic("Invalid bio command");
1292 amrd = (struct amrd_softc *)bio->bio_driver_info;
1293 driveno = amrd->amrd_drive - sc->amr_drive;
1294 blkcount = (bp->b_bcount + AMR_BLKSIZE - 1) / AMR_BLKSIZE;
1296 ac->ac_mailbox.mb_command = cmd;
1297 if (bp->b_cmd & (BUF_CMD_READ|BUF_CMD_WRITE)) {
1298 ac->ac_mailbox.mb_blkcount = blkcount;
1299 ac->ac_mailbox.mb_lba = bio->bio_offset / AMR_BLKSIZE;
1300 if (((bio->bio_offset / AMR_BLKSIZE) + blkcount) > sc->amr_drive[driveno].al_size) {
1301 device_printf(sc->amr_dev,
1302 "I/O beyond end of unit (%lld,%d > %lu)\n",
1303 (long long)(bio->bio_offset / AMR_BLKSIZE), blkcount,
1304 (u_long)sc->amr_drive[driveno].al_size);
1307 ac->ac_mailbox.mb_drive = driveno;
1308 if (sc->amr_state & AMR_STATE_REMAP_LD)
1309 ac->ac_mailbox.mb_drive |= 0x80;
1311 /* we fill in the s/g related data when the command is mapped */
1318 /********************************************************************************
1319 * Take a command, submit it to the controller and sleep until it completes
1320 * or fails. Interrupts must be enabled, returns nonzero on error.
1323 amr_wait_command(struct amr_command *ac)
1326 struct amr_softc *sc = ac->ac_sc;
1330 ac->ac_complete = NULL;
1331 ac->ac_flags |= AMR_CMD_SLEEP;
1332 if ((error = amr_start(ac)) != 0) {
1336 while ((ac->ac_flags & AMR_CMD_BUSY) && (error != EWOULDBLOCK)) {
1337 error = lksleep(ac,&sc->amr_list_lock, 0, "amrwcmd", 0);
1343 /********************************************************************************
1344 * Take a command, submit it to the controller and busy-wait for it to return.
1345 * Returns nonzero on error. Can be safely called with interrupts enabled.
1348 amr_std_poll_command(struct amr_command *ac)
1350 struct amr_softc *sc = ac->ac_sc;
1355 ac->ac_complete = NULL;
1356 if ((error = amr_start(ac)) != 0)
1362 * Poll for completion, although the interrupt handler may beat us to it.
1363 * Note that the timeout here is somewhat arbitrary.
1367 } while ((ac->ac_flags & AMR_CMD_BUSY) && (count++ < 1000));
1368 if (!(ac->ac_flags & AMR_CMD_BUSY)) {
1371 /* XXX the slot is now marked permanently busy */
1373 device_printf(sc->amr_dev, "polled command timeout\n");
1379 amr_setup_polled_dmamap(void *arg, bus_dma_segment_t *segs, int nsegs, int err)
1381 struct amr_command *ac = arg;
1382 struct amr_softc *sc = ac->ac_sc;
1386 device_printf(sc->amr_dev, "error %d in %s", err, __func__);
1387 ac->ac_status = AMR_STATUS_ABORTED;
1391 amr_setup_sg(arg, segs, nsegs, err);
1393 /* for AMR_CMD_CONFIG Read/Write the s/g count goes elsewhere */
1394 mb_channel = ((struct amr_mailbox_ioctl *)&ac->ac_mailbox)->mb_channel;
1395 if (ac->ac_mailbox.mb_command == AMR_CMD_CONFIG &&
1396 ((mb_channel == AMR_CONFIG_READ_NVRAM_CONFIG) ||
1397 (mb_channel == AMR_CONFIG_WRITE_NVRAM_CONFIG)))
1398 ((struct amr_mailbox_ioctl *)&ac->ac_mailbox)->mb_param = ac->ac_nsegments;
1400 ac->ac_mailbox.mb_nsgelem = ac->ac_nsegments;
1401 ac->ac_mailbox.mb_physaddr = ac->ac_mb_physaddr;
1402 if (AC_IS_SG64(ac)) {
1404 ac->ac_sg64_lo = ac->ac_sgbusaddr;
1407 sc->amr_poll_command1(sc, ac);
1410 /********************************************************************************
1411 * Take a command, submit it to the controller and busy-wait for it to return.
1412 * Returns nonzero on error. Can be safely called with interrupts enabled.
1415 amr_quartz_poll_command(struct amr_command *ac)
1417 struct amr_softc *sc = ac->ac_sc;
1424 if (AC_IS_SG64(ac)) {
1425 ac->ac_tag = sc->amr_buffer64_dmat;
1426 ac->ac_datamap = ac->ac_dma64map;
1428 ac->ac_tag = sc->amr_buffer_dmat;
1429 ac->ac_datamap = ac->ac_dmamap;
1432 /* now we have a slot, we can map the command (unmapped in amr_complete) */
1433 if (ac->ac_data != NULL && ac->ac_length != 0) {
1434 if (bus_dmamap_load(ac->ac_tag, ac->ac_datamap, ac->ac_data,
1435 ac->ac_length, amr_setup_polled_dmamap, ac, BUS_DMA_NOWAIT) != 0) {
1439 error = amr_quartz_poll_command1(sc, ac);
1446 amr_quartz_poll_command1(struct amr_softc *sc, struct amr_command *ac)
1450 lockmgr(&sc->amr_hw_lock, LK_EXCLUSIVE);
1451 if ((sc->amr_state & AMR_STATE_INTEN) == 0) {
1453 while (sc->amr_busyslots) {
1454 lksleep(sc, &sc->amr_hw_lock, PCATCH, "amrpoll", hz);
1460 if(sc->amr_busyslots) {
1461 device_printf(sc->amr_dev, "adapter is busy\n");
1462 lockmgr(&sc->amr_hw_lock, LK_RELEASE);
1463 if (ac->ac_data != NULL) {
1464 bus_dmamap_unload(ac->ac_tag, ac->ac_datamap);
1471 bcopy(&ac->ac_mailbox, (void *)(uintptr_t)(volatile void *)sc->amr_mailbox, AMR_MBOX_CMDSIZE);
1473 /* clear the poll/ack fields in the mailbox */
1474 sc->amr_mailbox->mb_ident = 0xFE;
1475 sc->amr_mailbox->mb_nstatus = 0xFF;
1476 sc->amr_mailbox->mb_status = 0xFF;
1477 sc->amr_mailbox->mb_poll = 0;
1478 sc->amr_mailbox->mb_ack = 0;
1479 sc->amr_mailbox->mb_busy = 1;
1481 AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_SUBMIT);
1483 while(sc->amr_mailbox->mb_nstatus == 0xFF)
1485 while(sc->amr_mailbox->mb_status == 0xFF)
1487 ac->ac_status=sc->amr_mailbox->mb_status;
1488 error = (ac->ac_status !=AMR_STATUS_SUCCESS) ? 1:0;
1489 while(sc->amr_mailbox->mb_poll != 0x77)
1491 sc->amr_mailbox->mb_poll = 0;
1492 sc->amr_mailbox->mb_ack = 0x77;
1494 /* acknowledge that we have the commands */
1495 AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_ACK);
1496 while(AMR_QGET_IDB(sc) & AMR_QIDB_ACK)
1498 lockmgr(&sc->amr_hw_lock, LK_RELEASE);
1500 /* unmap the command's data buffer */
1501 if (ac->ac_flags & AMR_CMD_DATAIN) {
1502 bus_dmamap_sync(ac->ac_tag, ac->ac_datamap, BUS_DMASYNC_POSTREAD);
1504 if (ac->ac_flags & AMR_CMD_DATAOUT) {
1505 bus_dmamap_sync(ac->ac_tag, ac->ac_datamap, BUS_DMASYNC_POSTWRITE);
1507 bus_dmamap_unload(ac->ac_tag, ac->ac_datamap);
1513 amr_freeslot(struct amr_command *ac)
1515 struct amr_softc *sc = ac->ac_sc;
1521 if (sc->amr_busycmd[slot] == NULL)
1522 panic("amr: slot %d not busy?", slot);
1524 sc->amr_busycmd[slot] = NULL;
1525 atomic_subtract_int(&sc->amr_busyslots, 1);
1530 /********************************************************************************
1531 * Map/unmap (ac)'s data in the controller's addressable space as required.
1533 * These functions may be safely called multiple times on a given command.
1536 amr_setup_sg(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1538 struct amr_command *ac = (struct amr_command *)arg;
1539 struct amr_sgentry *sg;
1540 struct amr_sg64entry *sg64;
1545 /* get base address of s/g table */
1546 sg = ac->ac_sg.sg32;
1547 sg64 = ac->ac_sg.sg64;
1549 if (AC_IS_SG64(ac)) {
1550 ac->ac_nsegments = nsegments;
1551 ac->ac_mb_physaddr = 0xffffffff;
1552 for (i = 0; i < nsegments; i++, sg64++) {
1553 sg64->sg_addr = segs[i].ds_addr;
1554 sg64->sg_count = segs[i].ds_len;
1557 /* decide whether we need to populate the s/g table */
1558 if (nsegments < 2) {
1559 ac->ac_nsegments = 0;
1560 ac->ac_mb_physaddr = segs[0].ds_addr;
1562 ac->ac_nsegments = nsegments;
1563 ac->ac_mb_physaddr = ac->ac_sgbusaddr;
1564 for (i = 0; i < nsegments; i++, sg++) {
1565 sg->sg_addr = segs[i].ds_addr;
1566 sg->sg_count = segs[i].ds_len;
1572 if (ac->ac_flags & AMR_CMD_DATAIN)
1573 flags |= BUS_DMASYNC_PREREAD;
1574 if (ac->ac_flags & AMR_CMD_DATAOUT)
1575 flags |= BUS_DMASYNC_PREWRITE;
1576 bus_dmamap_sync(ac->ac_tag, ac->ac_datamap, flags);
1577 ac->ac_flags |= AMR_CMD_MAPPED;
1581 amr_setup_data(void *arg, bus_dma_segment_t *segs, int nsegs, int err)
1583 struct amr_command *ac = arg;
1584 struct amr_softc *sc = ac->ac_sc;
1588 device_printf(sc->amr_dev, "error %d in %s", err, __func__);
1593 amr_setup_sg(arg, segs, nsegs, err);
1595 /* for AMR_CMD_CONFIG Read/Write the s/g count goes elsewhere */
1596 mb_channel = ((struct amr_mailbox_ioctl *)&ac->ac_mailbox)->mb_channel;
1597 if (ac->ac_mailbox.mb_command == AMR_CMD_CONFIG &&
1598 ((mb_channel == AMR_CONFIG_READ_NVRAM_CONFIG) ||
1599 (mb_channel == AMR_CONFIG_WRITE_NVRAM_CONFIG)))
1600 ((struct amr_mailbox_ioctl *)&ac->ac_mailbox)->mb_param = ac->ac_nsegments;
1602 ac->ac_mailbox.mb_nsgelem = ac->ac_nsegments;
1603 ac->ac_mailbox.mb_physaddr = ac->ac_mb_physaddr;
1604 if (AC_IS_SG64(ac)) {
1606 ac->ac_sg64_lo = ac->ac_sgbusaddr;
1609 if (sc->amr_submit_command(ac) == EBUSY) {
1611 amr_requeue_ready(ac);
1616 amr_setup_ccb(void *arg, bus_dma_segment_t *segs, int nsegs, int err)
1618 struct amr_command *ac = arg;
1619 struct amr_softc *sc = ac->ac_sc;
1620 struct amr_passthrough *ap = &ac->ac_ccb->ccb_pthru;
1621 struct amr_ext_passthrough *aep = &ac->ac_ccb->ccb_epthru;
1624 device_printf(sc->amr_dev, "error %d in %s", err, __func__);
1629 /* Set up the mailbox portion of the command to point at the ccb */
1630 ac->ac_mailbox.mb_nsgelem = 0;
1631 ac->ac_mailbox.mb_physaddr = ac->ac_ccb_busaddr;
1633 amr_setup_sg(arg, segs, nsegs, err);
1635 switch (ac->ac_mailbox.mb_command) {
1636 case AMR_CMD_EXTPASS:
1637 aep->ap_no_sg_elements = ac->ac_nsegments;
1638 aep->ap_data_transfer_address = ac->ac_mb_physaddr;
1641 ap->ap_no_sg_elements = ac->ac_nsegments;
1642 ap->ap_data_transfer_address = ac->ac_mb_physaddr;
1645 panic("Unknown ccb command");
1648 if (sc->amr_submit_command(ac) == EBUSY) {
1650 amr_requeue_ready(ac);
1655 amr_mapcmd(struct amr_command *ac)
1657 bus_dmamap_callback_t *cb;
1658 struct amr_softc *sc = ac->ac_sc;
1662 if (AC_IS_SG64(ac)) {
1663 ac->ac_tag = sc->amr_buffer64_dmat;
1664 ac->ac_datamap = ac->ac_dma64map;
1666 ac->ac_tag = sc->amr_buffer_dmat;
1667 ac->ac_datamap = ac->ac_dmamap;
1670 if (ac->ac_flags & AMR_CMD_CCB)
1673 cb = amr_setup_data;
1675 /* if the command involves data at all, and hasn't been mapped */
1676 if ((ac->ac_flags & AMR_CMD_MAPPED) == 0 && (ac->ac_data != NULL)) {
1677 /* map the data buffers into bus space and build the s/g list */
1678 if (bus_dmamap_load(ac->ac_tag, ac->ac_datamap, ac->ac_data,
1679 ac->ac_length, cb, ac, 0) == EINPROGRESS) {
1680 sc->amr_state |= AMR_STATE_QUEUE_FRZN;
1683 if (sc->amr_submit_command(ac) == EBUSY) {
1685 amr_requeue_ready(ac);
1693 amr_unmapcmd(struct amr_command *ac)
1699 /* if the command involved data at all and was mapped */
1700 if (ac->ac_flags & AMR_CMD_MAPPED) {
1702 if (ac->ac_data != NULL) {
1705 if (ac->ac_flags & AMR_CMD_DATAIN)
1706 flag |= BUS_DMASYNC_POSTREAD;
1707 if (ac->ac_flags & AMR_CMD_DATAOUT)
1708 flag |= BUS_DMASYNC_POSTWRITE;
1710 bus_dmamap_sync(ac->ac_tag, ac->ac_datamap, flag);
1711 bus_dmamap_unload(ac->ac_tag, ac->ac_datamap);
1714 ac->ac_flags &= ~AMR_CMD_MAPPED;
1719 amr_abort_load(struct amr_command *ac)
1722 struct amr_softc *sc = ac->ac_sc;
1724 KKASSERT(lockstatus(&sc->amr_list_lock, curthread) != 0);
1726 ac->ac_status = AMR_STATUS_ABORTED;
1727 amr_init_qhead(&head);
1728 amr_enqueue_completed(ac, &head);
1730 lockmgr(&sc->amr_list_lock, LK_RELEASE);
1731 amr_complete(sc, &head);
1732 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
1735 /********************************************************************************
1736 * Take a command and give it to the controller, returns 0 if successful, or
1737 * EBUSY if the command should be retried later.
1740 amr_start(struct amr_command *ac)
1742 struct amr_softc *sc;
1748 /* mark command as busy so that polling consumer can tell */
1750 ac->ac_flags |= AMR_CMD_BUSY;
1752 /* get a command slot (freed in amr_done) */
1754 if (sc->amr_busycmd[slot] != NULL)
1755 panic("amr: slot %d busy?", slot);
1756 sc->amr_busycmd[slot] = ac;
1757 atomic_add_int(&sc->amr_busyslots, 1);
1759 /* Now we have a slot, we can map the command (unmapped in amr_complete). */
1760 if ((error = amr_mapcmd(ac)) == ENOMEM) {
1762 * Memroy resources are short, so free the slot and let this be tried
1771 /********************************************************************************
1772 * Extract one or more completed commands from the controller (sc)
1774 * Returns nonzero if any commands on the work queue were marked as completed.
1778 amr_done(struct amr_softc *sc)
1781 struct amr_command *ac;
1782 struct amr_mailbox mbox;
1787 /* See if there's anything for us to do */
1789 amr_init_qhead(&head);
1791 /* loop collecting completed commands */
1793 /* poll for a completed command's identifier and status */
1794 if (sc->amr_get_work(sc, &mbox)) {
1797 /* iterate over completed commands in this result */
1798 for (i = 0; i < mbox.mb_nstatus; i++) {
1799 /* get pointer to busy command */
1800 idx = mbox.mb_completed[i] - 1;
1801 ac = sc->amr_busycmd[idx];
1803 /* really a busy command? */
1806 /* pull the command from the busy index */
1809 /* save status for later use */
1810 ac->ac_status = mbox.mb_status;
1811 amr_enqueue_completed(ac, &head);
1812 debug(3, "completed command with status %x", mbox.mb_status);
1814 device_printf(sc->amr_dev, "bad slot %d completed\n", idx);
1818 break; /* no work */
1821 /* handle completion and timeouts */
1822 amr_complete(sc, &head);
1827 /********************************************************************************
1828 * Do completion processing on done commands on (sc)
1832 amr_complete(void *context, ac_qhead_t *head)
1834 struct amr_softc *sc = (struct amr_softc *)context;
1835 struct amr_command *ac;
1839 /* pull completed commands off the queue */
1841 ac = amr_dequeue_completed(sc, head);
1845 /* unmap the command's data buffer */
1849 * Is there a completion handler?
1851 if (ac->ac_complete != NULL) {
1852 /* unbusy the command */
1853 ac->ac_flags &= ~AMR_CMD_BUSY;
1854 ac->ac_complete(ac);
1857 * Is someone sleeping on this one?
1860 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
1861 ac->ac_flags &= ~AMR_CMD_BUSY;
1862 if (ac->ac_flags & AMR_CMD_SLEEP) {
1863 /* unbusy the command */
1866 lockmgr(&sc->amr_list_lock, LK_RELEASE);
1869 if(!sc->amr_busyslots) {
1874 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
1875 sc->amr_state &= ~AMR_STATE_QUEUE_FRZN;
1877 lockmgr(&sc->amr_list_lock, LK_RELEASE);
1880 /********************************************************************************
1881 ********************************************************************************
1882 Command Buffer Management
1883 ********************************************************************************
1884 ********************************************************************************/
1886 /********************************************************************************
1887 * Get a new command buffer.
1889 * This may return NULL in low-memory cases.
1891 * If possible, we recycle a command buffer that's been used before.
1893 struct amr_command *
1894 amr_alloccmd(struct amr_softc *sc)
1896 struct amr_command *ac;
1900 ac = amr_dequeue_free(sc);
1902 sc->amr_state |= AMR_STATE_QUEUE_FRZN;
1906 /* clear out significant fields */
1908 bzero(&ac->ac_mailbox, sizeof(struct amr_mailbox));
1912 ac->ac_complete = NULL;
1915 ac->ac_datamap = NULL;
1919 /********************************************************************************
1920 * Release a command buffer for recycling.
1923 amr_releasecmd(struct amr_command *ac)
1927 amr_enqueue_free(ac);
1930 /********************************************************************************
1931 * Allocate a new command cluster and initialise it.
1934 amr_alloccmd_cluster(struct amr_softc *sc)
1936 struct amr_command_cluster *acc;
1937 struct amr_command *ac;
1941 * If we haven't found the real limit yet, let us have a couple of
1942 * commands in order to be able to probe.
1944 if (sc->amr_maxio == 0)
1947 if (sc->amr_nextslot > sc->amr_maxio)
1949 acc = kmalloc(AMR_CMD_CLUSTERSIZE, M_AMR, M_NOWAIT | M_ZERO);
1951 nextslot = sc->amr_nextslot;
1952 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
1953 TAILQ_INSERT_TAIL(&sc->amr_cmd_clusters, acc, acc_link);
1954 lockmgr(&sc->amr_list_lock, LK_RELEASE);
1955 for (i = 0; i < AMR_CMD_CLUSTERCOUNT; i++) {
1956 ac = &acc->acc_command[i];
1958 ac->ac_slot = nextslot;
1961 * The SG table for each slot is a fixed size and is assumed to
1962 * to hold 64-bit s/g objects when the driver is configured to do
1963 * 64-bit DMA. 32-bit DMA commands still use the same table, but
1964 * cast down to 32-bit objects.
1966 if (AMR_IS_SG64(sc)) {
1967 ac->ac_sgbusaddr = sc->amr_sgbusaddr +
1968 (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sg64entry));
1969 ac->ac_sg.sg64 = sc->amr_sg64table + (ac->ac_slot * AMR_NSEG);
1971 ac->ac_sgbusaddr = sc->amr_sgbusaddr +
1972 (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry));
1973 ac->ac_sg.sg32 = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
1976 ac->ac_ccb = sc->amr_ccb + ac->ac_slot;
1977 ac->ac_ccb_busaddr = sc->amr_ccb_busaddr +
1978 (ac->ac_slot * sizeof(union amr_ccb));
1980 if (bus_dmamap_create(sc->amr_buffer_dmat, 0, &ac->ac_dmamap))
1982 if (AMR_IS_SG64(sc) &&
1983 (bus_dmamap_create(sc->amr_buffer64_dmat, 0,&ac->ac_dma64map)))
1986 if (++nextslot > sc->amr_maxio)
1989 sc->amr_nextslot = nextslot;
1993 /********************************************************************************
1994 * Free a command cluster
1997 amr_freecmd_cluster(struct amr_command_cluster *acc)
1999 struct amr_softc *sc = acc->acc_command[0].ac_sc;
2002 for (i = 0; i < AMR_CMD_CLUSTERCOUNT; i++) {
2003 if (acc->acc_command[i].ac_sc == NULL)
2005 bus_dmamap_destroy(sc->amr_buffer_dmat, acc->acc_command[i].ac_dmamap);
2006 if (AMR_IS_SG64(sc))
2007 bus_dmamap_destroy(sc->amr_buffer64_dmat, acc->acc_command[i].ac_dma64map);
2012 /********************************************************************************
2013 ********************************************************************************
2014 Interface-specific Shims
2015 ********************************************************************************
2016 ********************************************************************************/
2018 /********************************************************************************
2019 * Tell the controller that the mailbox contains a valid command
2022 amr_quartz_submit_command(struct amr_command *ac)
2024 struct amr_softc *sc = ac->ac_sc;
2025 static struct timeval lastfail;
2029 lockmgr(&sc->amr_hw_lock, LK_EXCLUSIVE);
2030 while (sc->amr_mailbox->mb_busy && (i++ < 10)) {
2032 /* This is a no-op read that flushes pending mailbox updates */
2035 if (sc->amr_mailbox->mb_busy) {
2036 lockmgr(&sc->amr_hw_lock, LK_RELEASE);
2037 if (ac->ac_retries++ > 1000) {
2038 if (ppsratecheck(&lastfail, &curfail, 1))
2039 device_printf(sc->amr_dev, "Too many retries on command %p. "
2040 "Controller is likely dead\n", ac);
2047 * Save the slot number so that we can locate this command when complete.
2048 * Note that ident = 0 seems to be special, so we don't use it.
2050 ac->ac_mailbox.mb_ident = ac->ac_slot + 1; /* will be coppied into mbox */
2051 bcopy(&ac->ac_mailbox, (void *)(uintptr_t)(volatile void *)sc->amr_mailbox, 14);
2052 sc->amr_mailbox->mb_busy = 1;
2053 sc->amr_mailbox->mb_poll = 0;
2054 sc->amr_mailbox->mb_ack = 0;
2055 sc->amr_mailbox64->sg64_hi = ac->ac_sg64_hi;
2056 sc->amr_mailbox64->sg64_lo = ac->ac_sg64_lo;
2058 AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_SUBMIT);
2059 lockmgr(&sc->amr_hw_lock, LK_RELEASE);
2064 amr_std_submit_command(struct amr_command *ac)
2066 struct amr_softc *sc = ac->ac_sc;
2067 static struct timeval lastfail;
2070 lockmgr(&sc->amr_hw_lock, LK_EXCLUSIVE);
2071 if (AMR_SGET_MBSTAT(sc) & AMR_SMBOX_BUSYFLAG) {
2072 lockmgr(&sc->amr_hw_lock, LK_RELEASE);
2073 if (ac->ac_retries++ > 1000) {
2074 if (ppsratecheck(&lastfail, &curfail, 1))
2075 device_printf(sc->amr_dev, "Too many retries on command %p. "
2076 "Controller is likely dead\n", ac);
2083 * Save the slot number so that we can locate this command when complete.
2084 * Note that ident = 0 seems to be special, so we don't use it.
2086 ac->ac_mailbox.mb_ident = ac->ac_slot + 1; /* will be coppied into mbox */
2087 bcopy(&ac->ac_mailbox, (void *)(uintptr_t)(volatile void *)sc->amr_mailbox, 14);
2088 sc->amr_mailbox->mb_busy = 1;
2089 sc->amr_mailbox->mb_poll = 0;
2090 sc->amr_mailbox->mb_ack = 0;
2092 AMR_SPOST_COMMAND(sc);
2093 lockmgr(&sc->amr_hw_lock, LK_RELEASE);
2097 /********************************************************************************
2098 * Claim any work that the controller has completed; acknowledge completion,
2099 * save details of the completion in (mbsave)
2102 amr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave)
2107 u_int8_t completed[46];
2113 /* work waiting for us? */
2114 if ((outd = AMR_QGET_ODB(sc)) == AMR_QODB_READY) {
2116 /* acknowledge interrupt */
2117 AMR_QPUT_ODB(sc, AMR_QODB_READY);
2119 while ((nstatus = sc->amr_mailbox->mb_nstatus) == 0xff)
2121 sc->amr_mailbox->mb_nstatus = 0xff;
2123 /* wait until fw wrote out all completions */
2124 for (i = 0; i < nstatus; i++) {
2125 while ((completed[i] = sc->amr_mailbox->mb_completed[i]) == 0xff)
2127 sc->amr_mailbox->mb_completed[i] = 0xff;
2130 /* Save information for later processing */
2131 mbsave->mb_nstatus = nstatus;
2132 mbsave->mb_status = sc->amr_mailbox->mb_status;
2133 sc->amr_mailbox->mb_status = 0xff;
2135 for (i = 0; i < nstatus; i++)
2136 mbsave->mb_completed[i] = completed[i];
2138 /* acknowledge that we have the commands */
2139 AMR_QPUT_IDB(sc, AMR_QIDB_ACK);
2142 #ifndef AMR_QUARTZ_GOFASTER
2144 * This waits for the controller to notice that we've taken the
2145 * command from it. It's very inefficient, and we shouldn't do it,
2146 * but if we remove this code, we stop completing commands under
2149 * Peter J says we shouldn't do this. The documentation says we
2150 * should. Who is right?
2152 while(AMR_QGET_IDB(sc) & AMR_QIDB_ACK)
2153 ; /* XXX aiee! what if it dies? */
2157 worked = 1; /* got some work */
2164 amr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave)
2173 /* check for valid interrupt status */
2174 istat = AMR_SGET_ISTAT(sc);
2175 if ((istat & AMR_SINTR_VALID) != 0) {
2176 AMR_SPUT_ISTAT(sc, istat); /* ack interrupt status */
2178 /* save mailbox, which contains a list of completed commands */
2179 bcopy((void *)(uintptr_t)(volatile void *)sc->amr_mailbox, mbsave, sizeof(*mbsave));
2181 AMR_SACK_INTERRUPT(sc); /* acknowledge we have the mailbox */
2188 /********************************************************************************
2189 * Notify the controller of the mailbox location.
2192 amr_std_attach_mailbox(struct amr_softc *sc)
2195 /* program the mailbox physical address */
2196 AMR_SBYTE_SET(sc, AMR_SMBOX_0, sc->amr_mailboxphys & 0xff);
2197 AMR_SBYTE_SET(sc, AMR_SMBOX_1, (sc->amr_mailboxphys >> 8) & 0xff);
2198 AMR_SBYTE_SET(sc, AMR_SMBOX_2, (sc->amr_mailboxphys >> 16) & 0xff);
2199 AMR_SBYTE_SET(sc, AMR_SMBOX_3, (sc->amr_mailboxphys >> 24) & 0xff);
2200 AMR_SBYTE_SET(sc, AMR_SMBOX_ENABLE, AMR_SMBOX_ADDR);
2202 /* clear any outstanding interrupt and enable interrupts proper */
2203 AMR_SACK_INTERRUPT(sc);
2204 AMR_SENABLE_INTR(sc);
2207 #ifdef AMR_BOARD_INIT
2208 /********************************************************************************
2209 * Initialise the controller
2212 amr_quartz_init(struct amr_softc *sc)
2214 int status, ostatus;
2216 device_printf(sc->amr_dev, "initial init status %x\n", AMR_QGET_INITSTATUS(sc));
2221 while ((status = AMR_QGET_INITSTATUS(sc)) != AMR_QINIT_DONE) {
2222 if (status != ostatus) {
2223 device_printf(sc->amr_dev, "(%x) %s\n", status, amr_describe_code(amr_table_qinit, status));
2227 case AMR_QINIT_NOMEM:
2230 case AMR_QINIT_SCAN:
2231 /* XXX we could print channel/target here */
2239 amr_std_init(struct amr_softc *sc)
2241 int status, ostatus;
2243 device_printf(sc->amr_dev, "initial init status %x\n", AMR_SGET_INITSTATUS(sc));
2248 while ((status = AMR_SGET_INITSTATUS(sc)) != AMR_SINIT_DONE) {
2249 if (status != ostatus) {
2250 device_printf(sc->amr_dev, "(%x) %s\n", status, amr_describe_code(amr_table_sinit, status));
2254 case AMR_SINIT_NOMEM:
2257 case AMR_SINIT_INPROG:
2258 /* XXX we could print channel/target here? */
2266 /********************************************************************************
2267 ********************************************************************************
2269 ********************************************************************************
2270 ********************************************************************************/
2272 /********************************************************************************
2273 * Identify the controller and print some information about it.
2276 amr_describe_controller(struct amr_softc *sc)
2278 struct amr_prodinfo *ap;
2279 struct amr_enquiry *ae;
2284 * Try to get 40LD product info, which tells us what the card is labelled as.
2286 if ((ap = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0, &status)) != NULL) {
2287 device_printf(sc->amr_dev, "<LSILogic %.80s> Firmware %.16s, BIOS %.16s, %dMB RAM\n",
2288 ap->ap_product, ap->ap_firmware, ap->ap_bios,
2296 * Try 8LD extended ENQUIRY to get controller signature, and use lookup table.
2298 if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_EXT_ENQUIRY2, 0, 0, &status)) != NULL) {
2299 prod = amr_describe_code(amr_table_adaptertype, ae->ae_signature);
2301 } else if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0, &status)) != NULL) {
2304 * Try to work it out based on the PCI signatures.
2306 switch (pci_get_device(sc->amr_dev)) {
2308 prod = "Series 428";
2311 prod = "Series 434";
2314 prod = "unknown controller";
2318 device_printf(sc->amr_dev, "<unsupported controller>\n");
2323 * HP NetRaid controllers have a special encoding of the firmware and
2324 * BIOS versions. The AMI version seems to have it as strings whereas
2325 * the HP version does it with a leading uppercase character and two
2329 if(ae->ae_adapter.aa_firmware[2] >= 'A' &&
2330 ae->ae_adapter.aa_firmware[2] <= 'Z' &&
2331 ae->ae_adapter.aa_firmware[1] < ' ' &&
2332 ae->ae_adapter.aa_firmware[0] < ' ' &&
2333 ae->ae_adapter.aa_bios[2] >= 'A' &&
2334 ae->ae_adapter.aa_bios[2] <= 'Z' &&
2335 ae->ae_adapter.aa_bios[1] < ' ' &&
2336 ae->ae_adapter.aa_bios[0] < ' ') {
2338 /* this looks like we have an HP NetRaid version of the MegaRaid */
2340 if(ae->ae_signature == AMR_SIG_438) {
2341 /* the AMI 438 is a NetRaid 3si in HP-land */
2342 prod = "HP NetRaid 3si";
2345 device_printf(sc->amr_dev, "<%s> Firmware %c.%02d.%02d, BIOS %c.%02d.%02d, %dMB RAM\n",
2346 prod, ae->ae_adapter.aa_firmware[2],
2347 ae->ae_adapter.aa_firmware[1],
2348 ae->ae_adapter.aa_firmware[0],
2349 ae->ae_adapter.aa_bios[2],
2350 ae->ae_adapter.aa_bios[1],
2351 ae->ae_adapter.aa_bios[0],
2352 ae->ae_adapter.aa_memorysize);
2354 device_printf(sc->amr_dev, "<%s> Firmware %.4s, BIOS %.4s, %dMB RAM\n",
2355 prod, ae->ae_adapter.aa_firmware, ae->ae_adapter.aa_bios,
2356 ae->ae_adapter.aa_memorysize);
2362 amr_dump_blocks(struct amr_softc *sc, int unit, u_int32_t lba, void *data, int blks)
2364 struct amr_command *ac;
2369 sc->amr_state |= AMR_STATE_INTEN;
2371 /* get ourselves a command buffer */
2372 if ((ac = amr_alloccmd(sc)) == NULL)
2374 /* set command flags */
2375 ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
2377 /* point the command at our data */
2379 ac->ac_length = blks * AMR_BLKSIZE;
2381 /* build the command proper */
2382 ac->ac_mailbox.mb_command = AMR_CMD_LWRITE;
2383 ac->ac_mailbox.mb_blkcount = blks;
2384 ac->ac_mailbox.mb_lba = lba;
2385 ac->ac_mailbox.mb_drive = unit;
2387 /* can't assume that interrupts are going to work here, so play it safe */
2388 if (sc->amr_poll_command(ac))
2390 error = ac->ac_status;
2396 sc->amr_state &= ~AMR_STATE_INTEN;
2403 /********************************************************************************
2404 * Print the command (ac) in human-readable format
2408 amr_printcommand(struct amr_command *ac)
2410 struct amr_softc *sc = ac->ac_sc;
2411 struct amr_sgentry *sg;
2414 device_printf(sc->amr_dev, "cmd %x ident %d drive %d\n",
2415 ac->ac_mailbox.mb_command, ac->ac_mailbox.mb_ident, ac->ac_mailbox.mb_drive);
2416 device_printf(sc->amr_dev, "blkcount %d lba %d\n",
2417 ac->ac_mailbox.mb_blkcount, ac->ac_mailbox.mb_lba);
2418 device_printf(sc->amr_dev, "virtaddr %p length %lu\n", ac->ac_data, (unsigned long)ac->ac_length);
2419 device_printf(sc->amr_dev, "sg physaddr %08x nsg %d\n",
2420 ac->ac_mailbox.mb_physaddr, ac->ac_mailbox.mb_nsgelem);
2421 device_printf(sc->amr_dev, "ccb %p bio %p\n", ac->ac_ccb_data, ac->ac_bio);
2423 /* get base address of s/g table */
2424 sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
2425 for (i = 0; i < ac->ac_mailbox.mb_nsgelem; i++, sg++)
2426 device_printf(sc->amr_dev, " %x/%d\n", sg->sg_addr, sg->sg_count);