2 * Copyright (c) 1999,2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Copyright (c) 2002 Eric Moore
28 * Copyright (c) 2002 LSI Logic Corporation
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. The party using or redistributing the source code and binary forms
40 * agrees to the disclaimer below and the terms and conditions set forth
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * $FreeBSD: src/sys/dev/amr/amr.c,v 1.7.2.13 2003/01/15 13:41:18 emoore Exp $
56 * $DragonFly: src/sys/dev/raid/amr/amr.c,v 1.3 2003/07/19 21:14:17 dillon Exp $
60 * Driver for the AMI MegaRaid family of controllers.
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/malloc.h>
66 #include <sys/kernel.h>
68 #include <dev/amr/amr_compat.h>
71 #include <sys/devicestat.h>
75 #include <machine/bus_memio.h>
76 #include <machine/bus_pio.h>
77 #include <machine/bus.h>
78 #include <machine/resource.h>
81 #include <pci/pcireg.h>
82 #include <pci/pcivar.h>
84 #include <dev/amr/amrio.h>
85 #include <dev/amr/amrreg.h>
86 #include <dev/amr/amrvar.h>
87 #define AMR_DEFINE_TABLES
88 #include <dev/amr/amr_tables.h>
90 #define AMR_CDEV_MAJOR 132
92 static d_open_t amr_open;
93 static d_close_t amr_close;
94 static d_ioctl_t amr_ioctl;
96 static struct cdevsw amr_cdevsw = {
98 /* close */ amr_close,
101 /* ioctl */ amr_ioctl,
104 /* strategy */ nostrategy,
106 /* maj */ AMR_CDEV_MAJOR,
113 * Initialisation, bus interface.
115 static void amr_startup(void *arg);
120 static int amr_query_controller(struct amr_softc *sc);
121 static void *amr_enquiry(struct amr_softc *sc, size_t bufsize,
122 u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual);
123 static void amr_completeio(struct amr_command *ac);
124 static int amr_support_ext_cdb(struct amr_softc *sc);
127 * Command buffer allocation.
129 static void amr_alloccmd_cluster(struct amr_softc *sc);
130 static void amr_freecmd_cluster(struct amr_command_cluster *acc);
133 * Command processing.
135 static int amr_bio_command(struct amr_softc *sc, struct amr_command **acp);
136 static int amr_wait_command(struct amr_command *ac);
137 static int amr_getslot(struct amr_command *ac);
138 static void amr_mapcmd(struct amr_command *ac);
139 static void amr_unmapcmd(struct amr_command *ac);
140 static int amr_start(struct amr_command *ac);
141 static void amr_complete(void *context, int pending);
146 static void amr_periodic(void *data);
149 * Interface-specific shims
151 static int amr_quartz_submit_command(struct amr_softc *sc);
152 static int amr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave);
153 static int amr_quartz_poll_command(struct amr_command *ac);
155 static int amr_std_submit_command(struct amr_softc *sc);
156 static int amr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave);
157 static int amr_std_poll_command(struct amr_command *ac);
158 static void amr_std_attach_mailbox(struct amr_softc *sc);
160 #ifdef AMR_BOARD_INIT
161 static int amr_quartz_init(struct amr_softc *sc);
162 static int amr_std_init(struct amr_softc *sc);
168 static void amr_describe_controller(struct amr_softc *sc);
171 static void amr_printcommand(struct amr_command *ac);
175 /********************************************************************************
176 ********************************************************************************
178 ********************************************************************************
179 ********************************************************************************/
181 /********************************************************************************
182 ********************************************************************************
184 ********************************************************************************
185 ********************************************************************************/
187 /********************************************************************************
188 * Initialise the controller and softc.
191 amr_attach(struct amr_softc *sc)
197 * Initialise per-controller queues.
199 TAILQ_INIT(&sc->amr_completed);
200 TAILQ_INIT(&sc->amr_freecmds);
201 TAILQ_INIT(&sc->amr_cmd_clusters);
202 TAILQ_INIT(&sc->amr_ready);
203 bioq_init(&sc->amr_bioq);
205 #if __FreeBSD_version >= 500005
207 * Initialise command-completion task.
209 TASK_INIT(&sc->amr_task_complete, 0, amr_complete, sc);
212 debug(2, "queue init done");
215 * Configure for this controller type.
217 if (AMR_IS_QUARTZ(sc)) {
218 sc->amr_submit_command = amr_quartz_submit_command;
219 sc->amr_get_work = amr_quartz_get_work;
220 sc->amr_poll_command = amr_quartz_poll_command;
222 sc->amr_submit_command = amr_std_submit_command;
223 sc->amr_get_work = amr_std_get_work;
224 sc->amr_poll_command = amr_std_poll_command;
225 amr_std_attach_mailbox(sc);;
228 #ifdef AMR_BOARD_INIT
229 if ((AMR_IS_QUARTZ(sc) ? amr_quartz_init(sc) : amr_std_init(sc))))
234 * Quiz controller for features and limits.
236 if (amr_query_controller(sc))
239 debug(2, "controller query complete");
242 * Attach our 'real' SCSI channels to CAM.
244 if (amr_cam_attach(sc))
246 debug(2, "CAM attach done");
249 * Create the control device.
251 sc->amr_dev_t = make_dev(&amr_cdevsw, device_get_unit(sc->amr_dev), UID_ROOT, GID_OPERATOR,
252 S_IRUSR | S_IWUSR, "amr%d", device_get_unit(sc->amr_dev));
253 sc->amr_dev_t->si_drv1 = sc;
256 * Schedule ourselves to bring the controller up once interrupts are
259 bzero(&sc->amr_ich, sizeof(struct intr_config_hook));
260 sc->amr_ich.ich_func = amr_startup;
261 sc->amr_ich.ich_arg = sc;
262 if (config_intrhook_establish(&sc->amr_ich) != 0) {
263 device_printf(sc->amr_dev, "can't establish configuration hook\n");
268 * Print a little information about the controller.
270 amr_describe_controller(sc);
272 debug(2, "attach complete");
276 /********************************************************************************
277 * Locate disk resources and attach children to them.
280 amr_startup(void *arg)
282 struct amr_softc *sc = (struct amr_softc *)arg;
283 struct amr_logdrive *dr;
288 /* pull ourselves off the intrhook chain */
289 config_intrhook_disestablish(&sc->amr_ich);
291 /* get up-to-date drive information */
292 if (amr_query_controller(sc)) {
293 device_printf(sc->amr_dev, "can't scan controller for drives\n");
297 /* iterate over available drives */
298 for (i = 0, dr = &sc->amr_drive[0]; (i < AMR_MAXLD) && (dr->al_size != 0xffffffff); i++, dr++) {
299 /* are we already attached to this drive? */
300 if (dr->al_disk == 0) {
301 /* generate geometry information */
302 if (dr->al_size > 0x200000) { /* extended translation? */
309 dr->al_cylinders = dr->al_size / (dr->al_heads * dr->al_sectors);
311 dr->al_disk = device_add_child(sc->amr_dev, NULL, -1);
312 if (dr->al_disk == 0)
313 device_printf(sc->amr_dev, "device_add_child failed\n");
314 device_set_ivars(dr->al_disk, dr);
318 if ((error = bus_generic_attach(sc->amr_dev)) != 0)
319 device_printf(sc->amr_dev, "bus_generic_attach returned %d\n", error);
321 /* mark controller back up */
322 sc->amr_state &= ~AMR_STATE_SHUTDOWN;
324 /* interrupts will be enabled before we do anything more */
325 sc->amr_state |= AMR_STATE_INTEN;
328 * Start the timeout routine.
330 /* sc->amr_timeout = timeout(amr_periodic, sc, hz);*/
335 /*******************************************************************************
336 * Free resources associated with a controller instance
339 amr_free(struct amr_softc *sc)
341 struct amr_command_cluster *acc;
343 /* detach from CAM */
346 /* cancel status timeout */
347 untimeout(amr_periodic, sc, sc->amr_timeout);
349 /* throw away any command buffers */
350 while ((acc = TAILQ_FIRST(&sc->amr_cmd_clusters)) != NULL) {
351 TAILQ_REMOVE(&sc->amr_cmd_clusters, acc, acc_link);
352 amr_freecmd_cluster(acc);
355 /* destroy control device */
356 if( sc->amr_dev_t != (dev_t)NULL)
357 destroy_dev(sc->amr_dev_t);
360 /*******************************************************************************
361 * Receive a bio structure from a child device and queue it on a particular
362 * disk resource, then poke the disk resource to start as much work as it can.
365 amr_submit_bio(struct amr_softc *sc, struct bio *bio)
369 amr_enqueue_bio(sc, bio);
374 /********************************************************************************
375 * Accept an open operation on the control device.
378 amr_open(dev_t dev, int flags, int fmt, d_thread_t *td)
380 int unit = minor(dev);
381 struct amr_softc *sc = devclass_get_softc(devclass_find("amr"), unit);
385 sc->amr_state |= AMR_STATE_OPEN;
389 /********************************************************************************
390 * Accept the last close on the control device.
393 amr_close(dev_t dev, int flags, int fmt, d_thread_t *td)
395 int unit = minor(dev);
396 struct amr_softc *sc = devclass_get_softc(devclass_find("amr"), unit);
400 sc->amr_state &= ~AMR_STATE_OPEN;
404 /********************************************************************************
405 * Handle controller-specific control operations.
408 amr_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *td)
410 struct amr_softc *sc = (struct amr_softc *)dev->si_drv1;
411 int *arg = (int *)addr;
412 struct amr_user_ioctl *au = (struct amr_user_ioctl *)addr;
413 struct amr_command *ac;
414 struct amr_mailbox_ioctl *mbi;
415 struct amr_passthrough *ap;
428 debug(1, "AMR_IO_VERSION");
429 *arg = AMR_IO_VERSION_NUMBER;
433 debug(1, "AMR_IO_COMMAND 0x%x", au->au_cmd[0]);
434 /* handle inbound data buffer */
435 if (au->au_length != 0) {
436 if ((dp = malloc(au->au_length, M_DEVBUF, M_WAITOK)) == NULL) {
440 if ((error = copyin(au->au_buffer, dp, au->au_length)) != 0)
442 debug(2, "copyin %ld bytes from %p -> %p", au->au_length, au->au_buffer, dp);
445 if ((ac = amr_alloccmd(sc)) == NULL) {
450 /* handle SCSI passthrough command */
451 if (au->au_cmd[0] == AMR_CMD_PASS) {
452 if ((ap = malloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO)) == NULL) {
458 ap->ap_cdb_length = au->au_cmd[2];
459 bcopy(&au->au_cmd[3], &ap->ap_cdb[0], ap->ap_cdb_length);
461 /* build passthrough */
462 ap->ap_timeout = au->au_cmd[ap->ap_cdb_length + 3] & 0x07;
463 ap->ap_ars = (au->au_cmd[ap->ap_cdb_length + 3] & 0x08) ? 1 : 0;
464 ap->ap_islogical = (au->au_cmd[ap->ap_cdb_length + 3] & 0x80) ? 1 : 0;
465 ap->ap_logical_drive_no = au->au_cmd[ap->ap_cdb_length + 4];
466 ap->ap_channel = au->au_cmd[ap->ap_cdb_length + 5];
467 ap->ap_scsi_id = au->au_cmd[ap->ap_cdb_length + 6];
468 ap->ap_request_sense_length = 14;
469 ap->ap_data_transfer_length = au->au_length;
470 /* XXX what about the request-sense area? does the caller want it? */
474 ac->ac_length = sizeof(*ap);
475 ac->ac_flags |= AMR_CMD_DATAOUT;
476 ac->ac_ccb_data = dp;
477 ac->ac_ccb_length = au->au_length;
478 if (au->au_direction & AMR_IO_READ)
479 ac->ac_flags |= AMR_CMD_CCB_DATAIN;
480 if (au->au_direction & AMR_IO_WRITE)
481 ac->ac_flags |= AMR_CMD_CCB_DATAOUT;
483 ac->ac_mailbox.mb_command = AMR_CMD_PASS;
486 /* direct command to controller */
487 mbi = (struct amr_mailbox_ioctl *)&ac->ac_mailbox;
489 /* copy pertinent mailbox items */
490 mbi->mb_command = au->au_cmd[0];
491 mbi->mb_channel = au->au_cmd[1];
492 mbi->mb_param = au->au_cmd[2];
493 mbi->mb_pad[0] = au->au_cmd[3];
494 mbi->mb_drive = au->au_cmd[4];
496 /* build the command */
498 ac->ac_length = au->au_length;
499 if (au->au_direction & AMR_IO_READ)
500 ac->ac_flags |= AMR_CMD_DATAIN;
501 if (au->au_direction & AMR_IO_WRITE)
502 ac->ac_flags |= AMR_CMD_DATAOUT;
505 /* run the command */
506 if ((error = amr_wait_command(ac)) != 0)
509 /* copy out data and set status */
510 if (au->au_length != 0)
511 error = copyout(dp, au->au_buffer, au->au_length);
512 debug(2, "copyout %ld bytes from %p -> %p", au->au_length, dp, au->au_buffer);
514 debug(2, "%16d", (int)dp);
515 au->au_status = ac->ac_status;
519 debug(1, "unknown ioctl 0x%lx", cmd);
533 /********************************************************************************
534 ********************************************************************************
536 ********************************************************************************
537 ********************************************************************************/
539 /********************************************************************************
540 * Perform a periodic check of the controller status
543 amr_periodic(void *data)
545 struct amr_softc *sc = (struct amr_softc *)data;
549 /* XXX perform periodic status checks here */
551 /* compensate for missed interrupts */
555 sc->amr_timeout = timeout(amr_periodic, sc, hz);
558 /********************************************************************************
559 ********************************************************************************
561 ********************************************************************************
562 ********************************************************************************/
564 /********************************************************************************
565 * Interrogate the controller for the operational parameters we require.
568 amr_query_controller(struct amr_softc *sc)
570 struct amr_enquiry3 *aex;
571 struct amr_prodinfo *ap;
572 struct amr_enquiry *ae;
576 * If we haven't found the real limit yet, let us have a couple of commands in
577 * order to be able to probe.
579 if (sc->amr_maxio == 0)
583 * Greater than 10 byte cdb support
585 sc->support_ext_cdb = amr_support_ext_cdb(sc);
587 if(sc->support_ext_cdb) {
588 debug(2,"supports extended CDBs.");
592 * Try to issue an ENQUIRY3 command
594 if ((aex = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_ENQ3,
595 AMR_CONFIG_ENQ3_SOLICITED_FULL)) != NULL) {
598 * Fetch current state of logical drives.
600 for (ldrv = 0; ldrv < aex->ae_numldrives; ldrv++) {
601 sc->amr_drive[ldrv].al_size = aex->ae_drivesize[ldrv];
602 sc->amr_drive[ldrv].al_state = aex->ae_drivestate[ldrv];
603 sc->amr_drive[ldrv].al_properties = aex->ae_driveprop[ldrv];
604 debug(2, " drive %d: %d state %x properties %x\n", ldrv, sc->amr_drive[ldrv].al_size,
605 sc->amr_drive[ldrv].al_state, sc->amr_drive[ldrv].al_properties);
610 * Get product info for channel count.
612 if ((ap = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0)) == NULL) {
613 device_printf(sc->amr_dev, "can't obtain product data from controller\n");
616 sc->amr_maxdrives = 40;
617 sc->amr_maxchan = ap->ap_nschan;
618 sc->amr_maxio = ap->ap_maxio;
619 sc->amr_type |= AMR_TYPE_40LD;
624 /* failed, try the 8LD ENQUIRY commands */
625 if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_EXT_ENQUIRY2, 0, 0)) == NULL) {
626 if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0)) == NULL) {
627 device_printf(sc->amr_dev, "can't obtain configuration data from controller\n");
630 ae->ae_signature = 0;
634 * Fetch current state of logical drives.
636 for (ldrv = 0; ldrv < ae->ae_ldrv.al_numdrives; ldrv++) {
637 sc->amr_drive[ldrv].al_size = ae->ae_ldrv.al_size[ldrv];
638 sc->amr_drive[ldrv].al_state = ae->ae_ldrv.al_state[ldrv];
639 sc->amr_drive[ldrv].al_properties = ae->ae_ldrv.al_properties[ldrv];
640 debug(2, " drive %d: %d state %x properties %x\n", ldrv, sc->amr_drive[ldrv].al_size,
641 sc->amr_drive[ldrv].al_state, sc->amr_drive[ldrv].al_properties);
644 sc->amr_maxdrives = 8;
645 sc->amr_maxchan = ae->ae_adapter.aa_channels;
646 sc->amr_maxio = ae->ae_adapter.aa_maxio;
651 * Mark remaining drives as unused.
653 for (; ldrv < AMR_MAXLD; ldrv++)
654 sc->amr_drive[ldrv].al_size = 0xffffffff;
657 * Cap the maximum number of outstanding I/Os. AMI's Linux driver doesn't trust
658 * the controller's reported value, and lockups have been seen when we do.
660 sc->amr_maxio = imin(sc->amr_maxio, AMR_LIMITCMD);
665 /********************************************************************************
666 * Run a generic enquiry-style command.
669 amr_enquiry(struct amr_softc *sc, size_t bufsize, u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual)
671 struct amr_command *ac;
681 /* get ourselves a command buffer */
682 if ((ac = amr_alloccmd(sc)) == NULL)
684 /* allocate the response structure */
685 if ((result = malloc(bufsize, M_DEVBUF, M_NOWAIT)) == NULL)
687 /* set command flags */
688 ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
690 /* point the command at our data */
691 ac->ac_data = result;
692 ac->ac_length = bufsize;
694 /* build the command proper */
695 mbox = (u_int8_t *)&ac->ac_mailbox; /* XXX want a real structure for this? */
700 /* can't assume that interrupts are going to work here, so play it safe */
701 if (sc->amr_poll_command(ac))
703 error = ac->ac_status;
708 if ((error != 0) && (result != NULL)) {
709 free(result, M_DEVBUF);
715 /********************************************************************************
716 * Flush the controller's internal cache, return status.
719 amr_flush(struct amr_softc *sc)
721 struct amr_command *ac;
724 /* get ourselves a command buffer */
726 if ((ac = amr_alloccmd(sc)) == NULL)
728 /* set command flags */
729 ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
731 /* build the command proper */
732 ac->ac_mailbox.mb_command = AMR_CMD_FLUSH;
734 /* we have to poll, as the system may be going down or otherwise damaged */
735 if (sc->amr_poll_command(ac))
737 error = ac->ac_status;
745 /********************************************************************************
746 * Detect extented cdb >> greater than 10 byte cdb support
747 * returns '1' means this support exist
748 * returns '0' means this support doesn't exist
751 amr_support_ext_cdb(struct amr_softc *sc)
753 struct amr_command *ac;
757 /* get ourselves a command buffer */
759 if ((ac = amr_alloccmd(sc)) == NULL)
761 /* set command flags */
762 ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
764 /* build the command proper */
765 mbox = (u_int8_t *)&ac->ac_mailbox; /* XXX want a real structure for this? */
770 /* we have to poll, as the system may be going down or otherwise damaged */
771 if (sc->amr_poll_command(ac))
773 if( ac->ac_status == AMR_STATUS_SUCCESS ) {
783 /********************************************************************************
784 * Try to find I/O work for the controller from one or more of the work queues.
786 * We make the assumption that if the controller is not ready to take a command
787 * at some given time, it will generate an interrupt at some later time when
791 amr_startio(struct amr_softc *sc)
793 struct amr_command *ac;
795 /* spin until something prevents us from doing any work */
798 /* try to get a ready command */
799 ac = amr_dequeue_ready(sc);
801 /* if that failed, build a command from a bio */
803 (void)amr_bio_command(sc, &ac);
805 /* if that failed, build a command from a ccb */
807 (void)amr_cam_command(sc, &ac);
809 /* if we don't have anything to do, give up */
813 /* try to give the command to the controller; if this fails save it for later and give up */
815 debug(2, "controller busy, command deferred");
816 amr_requeue_ready(ac); /* XXX schedule retry very soon? */
822 /********************************************************************************
823 * Handle completion of an I/O command.
826 amr_completeio(struct amr_command *ac)
828 struct amr_softc *sc = ac->ac_sc;
830 if (ac->ac_status != AMR_STATUS_SUCCESS) { /* could be more verbose here? */
831 ac->ac_bio->bio_error = EIO;
832 ac->ac_bio->bio_flags |= BIO_ERROR;
834 device_printf(sc->amr_dev, "I/O error - 0x%x\n", ac->ac_status);
835 /* amr_printcommand(ac);*/
837 amrd_intr(ac->ac_bio);
841 /********************************************************************************
842 ********************************************************************************
844 ********************************************************************************
845 ********************************************************************************/
847 /********************************************************************************
848 * Convert a bio off the top of the bio queue into a command.
851 amr_bio_command(struct amr_softc *sc, struct amr_command **acp)
853 struct amr_command *ac;
854 struct amrd_softc *amrd;
864 /* get a bio to work on */
865 if ((bio = amr_dequeue_bio(sc)) == NULL)
869 if ((ac = amr_alloccmd(sc)) == NULL) {
874 /* connect the bio to the command */
875 ac->ac_complete = amr_completeio;
877 ac->ac_data = bio->bio_data;
878 ac->ac_length = bio->bio_bcount;
879 if (BIO_IS_READ(bio)) {
880 ac->ac_flags |= AMR_CMD_DATAIN;
883 ac->ac_flags |= AMR_CMD_DATAOUT;
884 cmd = AMR_CMD_LWRITE;
886 amrd = (struct amrd_softc *)bio->bio_dev->si_drv1;
887 driveno = amrd->amrd_drive - sc->amr_drive;
888 blkcount = (bio->bio_bcount + AMR_BLKSIZE - 1) / AMR_BLKSIZE;
890 ac->ac_mailbox.mb_command = cmd;
891 ac->ac_mailbox.mb_blkcount = blkcount;
892 ac->ac_mailbox.mb_lba = bio->bio_pblkno;
893 ac->ac_mailbox.mb_drive = driveno;
894 /* we fill in the s/g related data when the command is mapped */
896 if ((bio->bio_pblkno + blkcount) > sc->amr_drive[driveno].al_size)
897 device_printf(sc->amr_dev, "I/O beyond end of unit (%lld,%d > %lu)\n",
898 (long long)bio->bio_pblkno, blkcount,
899 (u_long)sc->amr_drive[driveno].al_size);
905 if (bio != NULL) /* this breaks ordering... */
906 amr_enqueue_bio(sc, bio);
912 /********************************************************************************
913 * Take a command, submit it to the controller and sleep until it completes
914 * or fails. Interrupts must be enabled, returns nonzero on error.
917 amr_wait_command(struct amr_command *ac)
923 ac->ac_complete = NULL;
924 ac->ac_flags |= AMR_CMD_SLEEP;
925 if ((error = amr_start(ac)) != 0)
929 /* XXX better timeout? */
930 while ((ac->ac_flags & AMR_CMD_BUSY) && (count < 30)) {
931 tsleep(ac, PCATCH, "amrwcmd", hz);
936 /********************************************************************************
937 * Take a command, submit it to the controller and busy-wait for it to return.
938 * Returns nonzero on error. Can be safely called with interrupts enabled.
941 amr_std_poll_command(struct amr_command *ac)
943 struct amr_softc *sc = ac->ac_sc;
948 ac->ac_complete = NULL;
949 if ((error = amr_start(ac)) != 0)
955 * Poll for completion, although the interrupt handler may beat us to it.
956 * Note that the timeout here is somewhat arbitrary.
960 } while ((ac->ac_flags & AMR_CMD_BUSY) && (count++ < 1000));
961 if (!(ac->ac_flags & AMR_CMD_BUSY)) {
964 /* XXX the slot is now marked permanently busy */
966 device_printf(sc->amr_dev, "polled command timeout\n");
971 /********************************************************************************
972 * Take a command, submit it to the controller and busy-wait for it to return.
973 * Returns nonzero on error. Can be safely called with interrupts enabled.
976 amr_quartz_poll_command(struct amr_command *ac)
978 struct amr_softc *sc = ac->ac_sc;
984 /* now we have a slot, we can map the command (unmapped in amr_complete) */
990 while (sc->amr_busyslots){
991 tsleep(sc, PCATCH, "amrpoll", hz);
997 if(sc->amr_busyslots) {
998 device_printf(sc->amr_dev, "adapter is busy\n");
1005 bcopy(&ac->ac_mailbox, (void *)(uintptr_t)(volatile void *)sc->amr_mailbox, AMR_MBOX_CMDSIZE);
1007 /* clear the poll/ack fields in the mailbox */
1008 sc->amr_mailbox->mb_ident = 0xFE;
1009 sc->amr_mailbox->mb_nstatus = 0xFF;
1010 sc->amr_mailbox->mb_status = 0xFF;
1011 sc->amr_mailbox->mb_poll = 0;
1012 sc->amr_mailbox->mb_ack = 0;
1013 sc->amr_mailbox->mb_busy = 1;
1015 AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_SUBMIT);
1017 while(sc->amr_mailbox->mb_nstatus == 0xFF);
1018 while(sc->amr_mailbox->mb_status == 0xFF);
1019 ac->ac_status=sc->amr_mailbox->mb_status;
1020 error = (ac->ac_status !=AMR_STATUS_SUCCESS) ? 1:0;
1021 while(sc->amr_mailbox->mb_poll != 0x77);
1022 sc->amr_mailbox->mb_poll = 0;
1023 sc->amr_mailbox->mb_ack = 0x77;
1025 /* acknowledge that we have the commands */
1026 AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_ACK);
1027 while(AMR_QGET_IDB(sc) & AMR_QIDB_ACK);
1031 /* unmap the command's data buffer */
1037 /********************************************************************************
1038 * Get a free command slot for a command if it doesn't already have one.
1040 * May be safely called multiple times for a given command.
1043 amr_getslot(struct amr_command *ac)
1045 struct amr_softc *sc = ac->ac_sc;
1046 int s, slot, limit, error;
1050 /* if the command already has a slot, don't try to give it another one */
1051 if (ac->ac_slot != 0)
1054 /* enforce slot usage limit */
1055 limit = (ac->ac_flags & AMR_CMD_PRIORITY) ? sc->amr_maxio : sc->amr_maxio - 4;
1056 if (sc->amr_busyslots > limit)
1060 * Allocate a slot. XXX linear scan is slow
1064 for (slot = 0; slot < sc->amr_maxio; slot++) {
1065 if (sc->amr_busycmd[slot] == NULL) {
1066 sc->amr_busycmd[slot] = ac;
1067 sc->amr_busyslots++;
1078 /********************************************************************************
1079 * Map/unmap (ac)'s data in the controller's addressable space as required.
1081 * These functions may be safely called multiple times on a given command.
1084 amr_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1086 struct amr_command *ac = (struct amr_command *)arg;
1087 struct amr_softc *sc = ac->ac_sc;
1088 struct amr_sgentry *sg;
1094 /* get base address of s/g table */
1095 sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
1097 /* save data physical address */
1098 ac->ac_dataphys = segs[0].ds_addr;
1100 /* for AMR_CMD_CONFIG the s/g count goes elsewhere */
1101 if (ac->ac_mailbox.mb_command == AMR_CMD_CONFIG) {
1102 sgc = &(((struct amr_mailbox_ioctl *)&ac->ac_mailbox)->mb_param);
1104 sgc = &ac->ac_mailbox.mb_nsgelem;
1107 /* decide whether we need to populate the s/g table */
1108 if (nsegments < 2) {
1110 ac->ac_mailbox.mb_nsgelem = 0;
1111 ac->ac_mailbox.mb_physaddr = ac->ac_dataphys;
1113 ac->ac_mailbox.mb_nsgelem = nsegments;
1115 ac->ac_mailbox.mb_physaddr = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry));
1116 for (i = 0; i < nsegments; i++, sg++) {
1117 sg->sg_addr = segs[i].ds_addr;
1118 sg->sg_count = segs[i].ds_len;
1124 amr_setup_ccbmap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1126 struct amr_command *ac = (struct amr_command *)arg;
1127 struct amr_softc *sc = ac->ac_sc;
1128 struct amr_sgentry *sg;
1129 struct amr_passthrough *ap = (struct amr_passthrough *)ac->ac_data;
1130 struct amr_ext_passthrough *aep = (struct amr_ext_passthrough *)ac->ac_data;
1133 /* get base address of s/g table */
1134 sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
1136 /* decide whether we need to populate the s/g table */
1137 if( ac->ac_mailbox.mb_command == AMR_CMD_EXTPASS ) {
1138 if (nsegments < 2) {
1139 aep->ap_no_sg_elements = 0;
1140 aep->ap_data_transfer_address = segs[0].ds_addr;
1142 /* save s/g table information in passthrough */
1143 aep->ap_no_sg_elements = nsegments;
1144 aep->ap_data_transfer_address = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry));
1145 /* populate s/g table (overwrites previous call which mapped the passthrough) */
1146 for (i = 0; i < nsegments; i++, sg++) {
1147 sg->sg_addr = segs[i].ds_addr;
1148 sg->sg_count = segs[i].ds_len;
1149 debug(3, " %d: 0x%x/%d", i, sg->sg_addr, sg->sg_count);
1152 debug(3, "slot %d %d segments at 0x%x, passthrough at 0x%x", ac->ac_slot,
1153 aep->ap_no_sg_elements, aep->ap_data_transfer_address, ac->ac_dataphys);
1155 if (nsegments < 2) {
1156 ap->ap_no_sg_elements = 0;
1157 ap->ap_data_transfer_address = segs[0].ds_addr;
1159 /* save s/g table information in passthrough */
1160 ap->ap_no_sg_elements = nsegments;
1161 ap->ap_data_transfer_address = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry));
1162 /* populate s/g table (overwrites previous call which mapped the passthrough) */
1163 for (i = 0; i < nsegments; i++, sg++) {
1164 sg->sg_addr = segs[i].ds_addr;
1165 sg->sg_count = segs[i].ds_len;
1166 debug(3, " %d: 0x%x/%d", i, sg->sg_addr, sg->sg_count);
1169 debug(3, "slot %d %d segments at 0x%x, passthrough at 0x%x", ac->ac_slot,
1170 ap->ap_no_sg_elements, ap->ap_data_transfer_address, ac->ac_dataphys);
1175 amr_mapcmd(struct amr_command *ac)
1177 struct amr_softc *sc = ac->ac_sc;
1181 /* if the command involves data at all, and hasn't been mapped */
1182 if (!(ac->ac_flags & AMR_CMD_MAPPED)) {
1184 if (ac->ac_data != NULL) {
1185 /* map the data buffers into bus space and build the s/g list */
1186 bus_dmamap_load(sc->amr_buffer_dmat, ac->ac_dmamap, ac->ac_data, ac->ac_length,
1187 amr_setup_dmamap, ac, 0);
1188 if (ac->ac_flags & AMR_CMD_DATAIN)
1189 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_PREREAD);
1190 if (ac->ac_flags & AMR_CMD_DATAOUT)
1191 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_PREWRITE);
1194 if (ac->ac_ccb_data != NULL) {
1195 bus_dmamap_load(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, ac->ac_ccb_data, ac->ac_ccb_length,
1196 amr_setup_ccbmap, ac, 0);
1197 if (ac->ac_flags & AMR_CMD_CCB_DATAIN)
1198 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_PREREAD);
1199 if (ac->ac_flags & AMR_CMD_CCB_DATAOUT)
1200 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_PREWRITE);
1202 ac->ac_flags |= AMR_CMD_MAPPED;
1207 amr_unmapcmd(struct amr_command *ac)
1209 struct amr_softc *sc = ac->ac_sc;
1213 /* if the command involved data at all and was mapped */
1214 if (ac->ac_flags & AMR_CMD_MAPPED) {
1216 if (ac->ac_data != NULL) {
1217 if (ac->ac_flags & AMR_CMD_DATAIN)
1218 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_POSTREAD);
1219 if (ac->ac_flags & AMR_CMD_DATAOUT)
1220 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_POSTWRITE);
1221 bus_dmamap_unload(sc->amr_buffer_dmat, ac->ac_dmamap);
1224 if (ac->ac_ccb_data != NULL) {
1225 if (ac->ac_flags & AMR_CMD_CCB_DATAIN)
1226 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_POSTREAD);
1227 if (ac->ac_flags & AMR_CMD_CCB_DATAOUT)
1228 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_POSTWRITE);
1229 bus_dmamap_unload(sc->amr_buffer_dmat, ac->ac_ccb_dmamap);
1231 ac->ac_flags &= ~AMR_CMD_MAPPED;
1235 /********************************************************************************
1236 * Take a command and give it to the controller, returns 0 if successful, or
1237 * EBUSY if the command should be retried later.
1240 amr_start(struct amr_command *ac)
1242 struct amr_softc *sc = ac->ac_sc;
1247 /* mark command as busy so that polling consumer can tell */
1248 ac->ac_flags |= AMR_CMD_BUSY;
1250 /* get a command slot (freed in amr_done) */
1251 if (amr_getslot(ac))
1254 /* now we have a slot, we can map the command (unmapped in amr_complete) */
1257 /* mark the new mailbox we are going to copy in as busy */
1258 ac->ac_mailbox.mb_busy = 1;
1260 /* clear the poll/ack fields in the mailbox */
1261 sc->amr_mailbox->mb_poll = 0;
1262 sc->amr_mailbox->mb_ack = 0;
1265 * Save the slot number so that we can locate this command when complete.
1266 * Note that ident = 0 seems to be special, so we don't use it.
1268 ac->ac_mailbox.mb_ident = ac->ac_slot + 1;
1271 * Spin waiting for the mailbox, give up after ~1 second. We expect the
1272 * controller to be able to handle our I/O.
1274 * XXX perhaps we should wait for less time, and count on the deferred command
1275 * handling to deal with retries?
1277 debug(4, "wait for mailbox");
1278 for (i = 10000, done = 0; (i > 0) && !done; i--) {
1281 /* is the mailbox free? */
1282 if (sc->amr_mailbox->mb_busy == 0) {
1283 debug(4, "got mailbox");
1284 sc->amr_mailbox64->mb64_segment = 0;
1285 bcopy(&ac->ac_mailbox, (void *)(uintptr_t)(volatile void *)sc->amr_mailbox, AMR_MBOX_CMDSIZE);
1288 /* not free, spin waiting */
1290 debug(4, "busy flag %x\n", sc->amr_mailbox->mb_busy);
1291 /* this is somewhat ugly */
1294 splx(s); /* drop spl to allow completion interrupts */
1298 * Now give the command to the controller
1301 if (sc->amr_submit_command(sc)) {
1302 /* the controller wasn't ready to take the command, forget that we tried to post it */
1303 sc->amr_mailbox->mb_busy = 0;
1306 debug(3, "posted command");
1311 * The controller wouldn't take the command. Return the command as busy
1312 * so that it is retried later.
1317 /********************************************************************************
1318 * Extract one or more completed commands from the controller (sc)
1320 * Returns nonzero if any commands on the work queue were marked as completed.
1323 amr_done(struct amr_softc *sc)
1325 struct amr_command *ac;
1326 struct amr_mailbox mbox;
1331 /* See if there's anything for us to do */
1334 /* loop collecting completed commands */
1336 /* poll for a completed command's identifier and status */
1337 if (sc->amr_get_work(sc, &mbox)) {
1340 /* iterate over completed commands in this result */
1341 for (i = 0; i < mbox.mb_nstatus; i++) {
1342 /* get pointer to busy command */
1343 idx = mbox.mb_completed[i] - 1;
1344 ac = sc->amr_busycmd[idx];
1346 /* really a busy command? */
1349 /* pull the command from the busy index */
1350 sc->amr_busycmd[idx] = NULL;
1351 sc->amr_busyslots--;
1353 /* save status for later use */
1354 ac->ac_status = mbox.mb_status;
1355 amr_enqueue_completed(ac);
1356 debug(3, "completed command with status %x", mbox.mb_status);
1358 device_printf(sc->amr_dev, "bad slot %d completed\n", idx);
1362 break; /* no work */
1366 /* if we've completed any commands, try posting some more */
1370 /* handle completion and timeouts */
1371 #if __FreeBSD_version >= 500005
1372 if (sc->amr_state & AMR_STATE_INTEN)
1373 taskqueue_enqueue(taskqueue_swi, &sc->amr_task_complete);
1376 amr_complete(sc, 0);
1381 /********************************************************************************
1382 * Do completion processing on done commands on (sc)
1385 amr_complete(void *context, int pending)
1387 struct amr_softc *sc = (struct amr_softc *)context;
1388 struct amr_command *ac;
1392 /* pull completed commands off the queue */
1394 ac = amr_dequeue_completed(sc);
1398 /* unmap the command's data buffer */
1401 /* unbusy the command */
1402 ac->ac_flags &= ~AMR_CMD_BUSY;
1405 * Is there a completion handler?
1407 if (ac->ac_complete != NULL) {
1408 ac->ac_complete(ac);
1411 * Is someone sleeping on this one?
1413 } else if (ac->ac_flags & AMR_CMD_SLEEP) {
1417 if(!sc->amr_busyslots) {
1423 /********************************************************************************
1424 ********************************************************************************
1425 Command Buffer Management
1426 ********************************************************************************
1427 ********************************************************************************/
1429 /********************************************************************************
1430 * Get a new command buffer.
1432 * This may return NULL in low-memory cases.
1434 * If possible, we recycle a command buffer that's been used before.
1436 struct amr_command *
1437 amr_alloccmd(struct amr_softc *sc)
1439 struct amr_command *ac;
1443 ac = amr_dequeue_free(sc);
1445 amr_alloccmd_cluster(sc);
1446 ac = amr_dequeue_free(sc);
1451 /* clear out significant fields */
1454 bzero(&ac->ac_mailbox, sizeof(struct amr_mailbox));
1458 ac->ac_ccb_data = NULL;
1459 ac->ac_complete = NULL;
1463 /********************************************************************************
1464 * Release a command buffer for recycling.
1467 amr_releasecmd(struct amr_command *ac)
1471 amr_enqueue_free(ac);
1474 /********************************************************************************
1475 * Allocate a new command cluster and initialise it.
1478 amr_alloccmd_cluster(struct amr_softc *sc)
1480 struct amr_command_cluster *acc;
1481 struct amr_command *ac;
1484 acc = malloc(AMR_CMD_CLUSTERSIZE, M_DEVBUF, M_NOWAIT);
1487 TAILQ_INSERT_TAIL(&sc->amr_cmd_clusters, acc, acc_link);
1489 for (i = 0; i < AMR_CMD_CLUSTERCOUNT; i++) {
1490 ac = &acc->acc_command[i];
1491 bzero(ac, sizeof(*ac));
1493 if (!bus_dmamap_create(sc->amr_buffer_dmat, 0, &ac->ac_dmamap) &&
1494 !bus_dmamap_create(sc->amr_buffer_dmat, 0, &ac->ac_ccb_dmamap))
1500 /********************************************************************************
1501 * Free a command cluster
1504 amr_freecmd_cluster(struct amr_command_cluster *acc)
1506 struct amr_softc *sc = acc->acc_command[0].ac_sc;
1509 for (i = 0; i < AMR_CMD_CLUSTERCOUNT; i++)
1510 bus_dmamap_destroy(sc->amr_buffer_dmat, acc->acc_command[i].ac_dmamap);
1511 free(acc, M_DEVBUF);
1514 /********************************************************************************
1515 ********************************************************************************
1516 Interface-specific Shims
1517 ********************************************************************************
1518 ********************************************************************************/
1520 /********************************************************************************
1521 * Tell the controller that the mailbox contains a valid command
1524 amr_quartz_submit_command(struct amr_softc *sc)
1528 if (AMR_QGET_IDB(sc) & AMR_QIDB_SUBMIT)
1530 AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_SUBMIT);
1535 amr_std_submit_command(struct amr_softc *sc)
1539 if (AMR_SGET_MBSTAT(sc) & AMR_SMBOX_BUSYFLAG)
1541 AMR_SPOST_COMMAND(sc);
1545 /********************************************************************************
1546 * Claim any work that the controller has completed; acknowledge completion,
1547 * save details of the completion in (mbsave)
1550 amr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave)
1560 /* work waiting for us? */
1561 if ((outd = AMR_QGET_ODB(sc)) == AMR_QODB_READY) {
1563 /* save mailbox, which contains a list of completed commands */
1564 bcopy((void *)(uintptr_t)(volatile void *)sc->amr_mailbox, mbsave, sizeof(*mbsave));
1566 /* acknowledge interrupt */
1567 AMR_QPUT_ODB(sc, AMR_QODB_READY);
1569 /* acknowledge that we have the commands */
1570 AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_ACK);
1572 #ifndef AMR_QUARTZ_GOFASTER
1574 * This waits for the controller to notice that we've taken the
1575 * command from it. It's very inefficient, and we shouldn't do it,
1576 * but if we remove this code, we stop completing commands under
1579 * Peter J says we shouldn't do this. The documentation says we
1580 * should. Who is right?
1582 while(AMR_QGET_IDB(sc) & AMR_QIDB_ACK)
1583 ; /* XXX aiee! what if it dies? */
1586 worked = 1; /* got some work */
1594 amr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave)
1604 /* check for valid interrupt status */
1605 istat = AMR_SGET_ISTAT(sc);
1606 if ((istat & AMR_SINTR_VALID) != 0) {
1607 AMR_SPUT_ISTAT(sc, istat); /* ack interrupt status */
1609 /* save mailbox, which contains a list of completed commands */
1610 bcopy((void *)(uintptr_t)(volatile void *)sc->amr_mailbox, mbsave, sizeof(*mbsave));
1612 AMR_SACK_INTERRUPT(sc); /* acknowledge we have the mailbox */
1620 /********************************************************************************
1621 * Notify the controller of the mailbox location.
1624 amr_std_attach_mailbox(struct amr_softc *sc)
1627 /* program the mailbox physical address */
1628 AMR_SBYTE_SET(sc, AMR_SMBOX_0, sc->amr_mailboxphys & 0xff);
1629 AMR_SBYTE_SET(sc, AMR_SMBOX_1, (sc->amr_mailboxphys >> 8) & 0xff);
1630 AMR_SBYTE_SET(sc, AMR_SMBOX_2, (sc->amr_mailboxphys >> 16) & 0xff);
1631 AMR_SBYTE_SET(sc, AMR_SMBOX_3, (sc->amr_mailboxphys >> 24) & 0xff);
1632 AMR_SBYTE_SET(sc, AMR_SMBOX_ENABLE, AMR_SMBOX_ADDR);
1634 /* clear any outstanding interrupt and enable interrupts proper */
1635 AMR_SACK_INTERRUPT(sc);
1636 AMR_SENABLE_INTR(sc);
1639 #ifdef AMR_BOARD_INIT
1640 /********************************************************************************
1641 * Initialise the controller
1644 amr_quartz_init(struct amr_softc *sc)
1646 int status, ostatus;
1648 device_printf(sc->amr_dev, "initial init status %x\n", AMR_QGET_INITSTATUS(sc));
1653 while ((status = AMR_QGET_INITSTATUS(sc)) != AMR_QINIT_DONE) {
1654 if (status != ostatus) {
1655 device_printf(sc->amr_dev, "(%x) %s\n", status, amr_describe_code(amr_table_qinit, status));
1659 case AMR_QINIT_NOMEM:
1662 case AMR_QINIT_SCAN:
1663 /* XXX we could print channel/target here */
1671 amr_std_init(struct amr_softc *sc)
1673 int status, ostatus;
1675 device_printf(sc->amr_dev, "initial init status %x\n", AMR_SGET_INITSTATUS(sc));
1680 while ((status = AMR_SGET_INITSTATUS(sc)) != AMR_SINIT_DONE) {
1681 if (status != ostatus) {
1682 device_printf(sc->amr_dev, "(%x) %s\n", status, amr_describe_code(amr_table_sinit, status));
1686 case AMR_SINIT_NOMEM:
1689 case AMR_SINIT_INPROG:
1690 /* XXX we could print channel/target here? */
1698 /********************************************************************************
1699 ********************************************************************************
1701 ********************************************************************************
1702 ********************************************************************************/
1704 /********************************************************************************
1705 * Identify the controller and print some information about it.
1708 amr_describe_controller(struct amr_softc *sc)
1710 struct amr_prodinfo *ap;
1711 struct amr_enquiry *ae;
1715 * Try to get 40LD product info, which tells us what the card is labelled as.
1717 if ((ap = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0)) != NULL) {
1718 device_printf(sc->amr_dev, "<LSILogic %.80s> Firmware %.16s, BIOS %.16s, %dMB RAM\n",
1719 ap->ap_product, ap->ap_firmware, ap->ap_bios,
1727 * Try 8LD extended ENQUIRY to get controller signature, and use lookup table.
1729 if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_EXT_ENQUIRY2, 0, 0)) != NULL) {
1730 prod = amr_describe_code(amr_table_adaptertype, ae->ae_signature);
1732 } else if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0)) != NULL) {
1735 * Try to work it out based on the PCI signatures.
1737 switch (pci_get_device(sc->amr_dev)) {
1739 prod = "Series 428";
1742 prod = "Series 434";
1745 prod = "unknown controller";
1749 prod = "unsupported controller";
1753 * HP NetRaid controllers have a special encoding of the firmware and
1754 * BIOS versions. The AMI version seems to have it as strings whereas
1755 * the HP version does it with a leading uppercase character and two
1759 if(ae->ae_adapter.aa_firmware[2] >= 'A' &&
1760 ae->ae_adapter.aa_firmware[2] <= 'Z' &&
1761 ae->ae_adapter.aa_firmware[1] < ' ' &&
1762 ae->ae_adapter.aa_firmware[0] < ' ' &&
1763 ae->ae_adapter.aa_bios[2] >= 'A' &&
1764 ae->ae_adapter.aa_bios[2] <= 'Z' &&
1765 ae->ae_adapter.aa_bios[1] < ' ' &&
1766 ae->ae_adapter.aa_bios[0] < ' ') {
1768 /* this looks like we have an HP NetRaid version of the MegaRaid */
1770 if(ae->ae_signature == AMR_SIG_438) {
1771 /* the AMI 438 is a NetRaid 3si in HP-land */
1772 prod = "HP NetRaid 3si";
1775 device_printf(sc->amr_dev, "<%s> Firmware %c.%02d.%02d, BIOS %c.%02d.%02d, %dMB RAM\n",
1776 prod, ae->ae_adapter.aa_firmware[2],
1777 ae->ae_adapter.aa_firmware[1],
1778 ae->ae_adapter.aa_firmware[0],
1779 ae->ae_adapter.aa_bios[2],
1780 ae->ae_adapter.aa_bios[1],
1781 ae->ae_adapter.aa_bios[0],
1782 ae->ae_adapter.aa_memorysize);
1784 device_printf(sc->amr_dev, "<%s> Firmware %.4s, BIOS %.4s, %dMB RAM\n",
1785 prod, ae->ae_adapter.aa_firmware, ae->ae_adapter.aa_bios,
1786 ae->ae_adapter.aa_memorysize);
1792 /********************************************************************************
1793 * Print the command (ac) in human-readable format
1797 amr_printcommand(struct amr_command *ac)
1799 struct amr_softc *sc = ac->ac_sc;
1800 struct amr_sgentry *sg;
1803 device_printf(sc->amr_dev, "cmd %x ident %d drive %d\n",
1804 ac->ac_mailbox.mb_command, ac->ac_mailbox.mb_ident, ac->ac_mailbox.mb_drive);
1805 device_printf(sc->amr_dev, "blkcount %d lba %d\n",
1806 ac->ac_mailbox.mb_blkcount, ac->ac_mailbox.mb_lba);
1807 device_printf(sc->amr_dev, "virtaddr %p length %lu\n", ac->ac_data, (unsigned long)ac->ac_length);
1808 device_printf(sc->amr_dev, "sg physaddr %08x nsg %d\n",
1809 ac->ac_mailbox.mb_physaddr, ac->ac_mailbox.mb_nsgelem);
1810 device_printf(sc->amr_dev, "ccb %p bio %p\n", ac->ac_ccb_data, ac->ac_bio);
1812 /* get base address of s/g table */
1813 sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
1814 for (i = 0; i < ac->ac_mailbox.mb_nsgelem; i++, sg++)
1815 device_printf(sc->amr_dev, " %x/%d\n", sg->sg_addr, sg->sg_count);