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
31 * Driver for the AMI MegaRaid family of controllers.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
39 #include <dev/amr/amr_compat.h>
42 #include <sys/devicestat.h>
46 #include <machine/bus_memio.h>
47 #include <machine/bus_pio.h>
48 #include <machine/bus.h>
49 #include <machine/resource.h>
52 #include <pci/pcireg.h>
53 #include <pci/pcivar.h>
55 #include <dev/amr/amrio.h>
56 #include <dev/amr/amrreg.h>
57 #include <dev/amr/amrvar.h>
58 #define AMR_DEFINE_TABLES
59 #include <dev/amr/amr_tables.h>
61 #define AMR_CDEV_MAJOR 132
63 static d_open_t amr_open;
64 static d_close_t amr_close;
65 static d_ioctl_t amr_ioctl;
67 static struct cdevsw amr_cdevsw = {
69 /* close */ amr_close,
72 /* ioctl */ amr_ioctl,
75 /* strategy */ nostrategy,
77 /* maj */ AMR_CDEV_MAJOR,
84 * Initialisation, bus interface.
86 static void amr_startup(void *arg);
91 static int amr_query_controller(struct amr_softc *sc);
92 static void *amr_enquiry(struct amr_softc *sc, size_t bufsize,
93 u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual);
94 static void amr_completeio(struct amr_command *ac);
97 * Command buffer allocation.
99 static void amr_alloccmd_cluster(struct amr_softc *sc);
100 static void amr_freecmd_cluster(struct amr_command_cluster *acc);
103 * Command processing.
105 static int amr_bio_command(struct amr_softc *sc, struct amr_command **acp);
106 static int amr_wait_command(struct amr_command *ac);
107 static int amr_poll_command(struct amr_command *ac);
108 static int amr_getslot(struct amr_command *ac);
109 static void amr_mapcmd(struct amr_command *ac);
110 static void amr_unmapcmd(struct amr_command *ac);
111 static int amr_start(struct amr_command *ac);
112 static void amr_complete(void *context, int pending);
117 static void amr_periodic(void *data);
120 * Interface-specific shims
122 static int amr_quartz_submit_command(struct amr_softc *sc);
123 static int amr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave);
125 static int amr_std_submit_command(struct amr_softc *sc);
126 static int amr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave);
127 static void amr_std_attach_mailbox(struct amr_softc *sc);
129 #ifdef AMR_BOARD_INIT
130 static int amr_quartz_init(struct amr_softc *sc);
131 static int amr_std_init(struct amr_softc *sc);
137 static void amr_describe_controller(struct amr_softc *sc);
139 static void amr_printcommand(struct amr_command *ac);
142 /********************************************************************************
143 ********************************************************************************
145 ********************************************************************************
146 ********************************************************************************/
148 /********************************************************************************
149 ********************************************************************************
151 ********************************************************************************
152 ********************************************************************************/
154 /********************************************************************************
155 * Initialise the controller and softc.
158 amr_attach(struct amr_softc *sc)
164 * Initialise per-controller queues.
166 TAILQ_INIT(&sc->amr_completed);
167 TAILQ_INIT(&sc->amr_freecmds);
168 TAILQ_INIT(&sc->amr_cmd_clusters);
169 TAILQ_INIT(&sc->amr_ready);
170 bioq_init(&sc->amr_bioq);
172 #if __FreeBSD_version >= 500005
174 * Initialise command-completion task.
176 TASK_INIT(&sc->amr_task_complete, 0, amr_complete, sc);
179 debug(2, "queue init done");
182 * Configure for this controller type.
184 if (AMR_IS_QUARTZ(sc)) {
185 sc->amr_submit_command = amr_quartz_submit_command;
186 sc->amr_get_work = amr_quartz_get_work;
188 sc->amr_submit_command = amr_std_submit_command;
189 sc->amr_get_work = amr_std_get_work;
190 amr_std_attach_mailbox(sc);;
193 #ifdef AMR_BOARD_INIT
194 if ((AMR_IS_QUARTZ(sc) ? amr_quartz_init(sc) : amr_std_init(sc))))
199 * Quiz controller for features and limits.
201 if (amr_query_controller(sc))
204 debug(2, "controller query complete");
206 #ifdef AMR_SCSI_PASSTHROUGH
208 * Attach our 'real' SCSI channels to CAM.
210 if (amr_cam_attach(sc))
212 debug(2, "CAM attach done");
216 * Create the control device.
218 sc->amr_dev_t = make_dev(&amr_cdevsw, device_get_unit(sc->amr_dev), UID_ROOT, GID_OPERATOR,
219 S_IRUSR | S_IWUSR, "amr%d", device_get_unit(sc->amr_dev));
220 sc->amr_dev_t->si_drv1 = sc;
223 * Schedule ourselves to bring the controller up once interrupts are
226 bzero(&sc->amr_ich, sizeof(struct intr_config_hook));
227 sc->amr_ich.ich_func = amr_startup;
228 sc->amr_ich.ich_arg = sc;
229 if (config_intrhook_establish(&sc->amr_ich) != 0) {
230 device_printf(sc->amr_dev, "can't establish configuration hook\n");
235 * Print a little information about the controller.
237 amr_describe_controller(sc);
239 debug(2, "attach complete");
243 /********************************************************************************
244 * Locate disk resources and attach children to them.
247 amr_startup(void *arg)
249 struct amr_softc *sc = (struct amr_softc *)arg;
250 struct amr_logdrive *dr;
255 /* pull ourselves off the intrhook chain */
256 config_intrhook_disestablish(&sc->amr_ich);
258 /* get up-to-date drive information */
259 if (amr_query_controller(sc)) {
260 device_printf(sc->amr_dev, "can't scan controller for drives\n");
264 /* iterate over available drives */
265 for (i = 0, dr = &sc->amr_drive[0]; (i < AMR_MAXLD) && (dr->al_size != 0xffffffff); i++, dr++) {
266 /* are we already attached to this drive? */
267 if (dr->al_disk == 0) {
268 /* generate geometry information */
269 if (dr->al_size > 0x200000) { /* extended translation? */
276 dr->al_cylinders = dr->al_size / (dr->al_heads * dr->al_sectors);
278 dr->al_disk = device_add_child(sc->amr_dev, NULL, -1);
279 if (dr->al_disk == 0)
280 device_printf(sc->amr_dev, "device_add_child failed\n");
281 device_set_ivars(dr->al_disk, dr);
285 if ((error = bus_generic_attach(sc->amr_dev)) != 0)
286 device_printf(sc->amr_dev, "bus_generic_attach returned %d\n", error);
288 /* mark controller back up */
289 sc->amr_state &= ~AMR_STATE_SHUTDOWN;
291 /* interrupts will be enabled before we do anything more */
292 sc->amr_state |= AMR_STATE_INTEN;
295 * Start the timeout routine.
297 /* sc->amr_timeout = timeout(amr_periodic, sc, hz);*/
302 /*******************************************************************************
303 * Free resources associated with a controller instance
306 amr_free(struct amr_softc *sc)
308 struct amr_command_cluster *acc;
310 #ifdef AMR_SCSI_PASSTHROUGH
311 /* detach from CAM */
315 /* cancel status timeout */
316 untimeout(amr_periodic, sc, sc->amr_timeout);
318 /* throw away any command buffers */
319 while ((acc = TAILQ_FIRST(&sc->amr_cmd_clusters)) != NULL) {
320 TAILQ_REMOVE(&sc->amr_cmd_clusters, acc, acc_link);
321 amr_freecmd_cluster(acc);
325 /*******************************************************************************
326 * Receive a bio structure from a child device and queue it on a particular
327 * disk resource, then poke the disk resource to start as much work as it can.
330 amr_submit_bio(struct amr_softc *sc, struct bio *bio)
334 amr_enqueue_bio(sc, bio);
339 /********************************************************************************
340 * Accept an open operation on the control device.
343 amr_open(dev_t dev, int flags, int fmt, struct thread *td)
345 int unit = minor(dev);
346 struct amr_softc *sc = devclass_get_softc(devclass_find("amr"), unit);
350 sc->amr_state |= AMR_STATE_OPEN;
354 /********************************************************************************
355 * Accept the last close on the control device.
358 amr_close(dev_t dev, int flags, int fmt, struct thread *td)
360 int unit = minor(dev);
361 struct amr_softc *sc = devclass_get_softc(devclass_find("amr"), unit);
365 sc->amr_state &= ~AMR_STATE_OPEN;
369 /********************************************************************************
370 * Handle controller-specific control operations.
373 amr_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
375 struct amr_softc *sc = (struct amr_softc *)dev->si_drv1;
376 int *arg = (int *)addr;
377 struct amr_user_ioctl *au = (struct amr_user_ioctl *)addr;
378 struct amr_command *ac;
379 struct amr_mailbox_ioctl *mbi;
380 struct amr_passthrough *ap;
393 debug(1, "AMR_IO_VERSION");
394 *arg = AMR_IO_VERSION_NUMBER;
398 debug(1, "AMR_IO_COMMAND 0x%x", au->au_cmd[0]);
399 /* handle inbound data buffer */
400 if (au->au_length != 0) {
401 if ((dp = malloc(au->au_length, M_DEVBUF, M_WAITOK)) == NULL) {
405 if ((error = copyin(au->au_buffer, dp, au->au_length)) != 0)
407 debug(2, "copyin %ld bytes from %p -> %p", au->au_length, au->au_buffer, dp);
410 if ((ac = amr_alloccmd(sc)) == NULL) {
415 /* handle SCSI passthrough command */
416 if (au->au_cmd[0] == AMR_CMD_PASS) {
417 if ((ap = malloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO)) == NULL) {
423 ap->ap_cdb_length = au->au_cmd[2];
424 bcopy(&au->au_cmd[3], &ap->ap_cdb[0], ap->ap_cdb_length);
426 /* build passthrough */
427 ap->ap_timeout = au->au_cmd[ap->ap_cdb_length + 3] & 0x07;
428 ap->ap_ars = (au->au_cmd[ap->ap_cdb_length + 3] & 0x08) ? 1 : 0;
429 ap->ap_islogical = (au->au_cmd[ap->ap_cdb_length + 3] & 0x80) ? 1 : 0;
430 ap->ap_logical_drive_no = au->au_cmd[ap->ap_cdb_length + 4];
431 ap->ap_channel = au->au_cmd[ap->ap_cdb_length + 5];
432 ap->ap_scsi_id = au->au_cmd[ap->ap_cdb_length + 6];
433 ap->ap_request_sense_length = 14;
434 /* XXX what about the request-sense area? does the caller want it? */
438 ac->ac_length = sizeof(*ap);
439 ac->ac_flags |= AMR_CMD_DATAOUT;
440 ac->ac_ccb_data = dp;
441 ac->ac_ccb_length = au->au_length;
442 if (au->au_direction & AMR_IO_READ)
443 ac->ac_flags |= AMR_CMD_CCB_DATAIN;
444 if (au->au_direction & AMR_IO_WRITE)
445 ac->ac_flags |= AMR_CMD_CCB_DATAOUT;
447 ac->ac_mailbox.mb_command = AMR_CMD_PASS;
450 /* direct command to controller */
451 mbi = (struct amr_mailbox_ioctl *)&ac->ac_mailbox;
453 /* copy pertinent mailbox items */
454 mbi->mb_command = au->au_cmd[0];
455 mbi->mb_channel = au->au_cmd[1];
456 mbi->mb_param = au->au_cmd[2];
457 mbi->mb_pad[0] = au->au_cmd[3];
458 mbi->mb_drive = au->au_cmd[4];
460 /* build the command */
462 ac->ac_length = au->au_length;
463 if (au->au_direction & AMR_IO_READ)
464 ac->ac_flags |= AMR_CMD_DATAIN;
465 if (au->au_direction & AMR_IO_WRITE)
466 ac->ac_flags |= AMR_CMD_DATAOUT;
469 /* run the command */
470 if ((error = amr_wait_command(ac)) != 0)
473 /* copy out data and set status */
474 if (au->au_length != 0)
475 error = copyout(dp, au->au_buffer, au->au_length);
476 debug(2, "copyout %ld bytes from %p -> %p", au->au_length, dp, au->au_buffer);
478 debug(2, "%16D", dp, " ");
479 au->au_status = ac->ac_status;
483 debug(1, "unknown ioctl 0x%lx", cmd);
497 /********************************************************************************
498 ********************************************************************************
500 ********************************************************************************
501 ********************************************************************************/
503 /********************************************************************************
504 * Perform a periodic check of the controller status
507 amr_periodic(void *data)
509 struct amr_softc *sc = (struct amr_softc *)data;
513 /* XXX perform periodic status checks here */
515 /* compensate for missed interrupts */
519 sc->amr_timeout = timeout(amr_periodic, sc, hz);
522 /********************************************************************************
523 ********************************************************************************
525 ********************************************************************************
526 ********************************************************************************/
528 /********************************************************************************
529 * Interrogate the controller for the operational parameters we require.
532 amr_query_controller(struct amr_softc *sc)
534 struct amr_enquiry3 *aex;
535 struct amr_prodinfo *ap;
536 struct amr_enquiry *ae;
540 * If we haven't found the real limit yet, let us have a couple of commands in
541 * order to be able to probe.
543 if (sc->amr_maxio == 0)
547 * Try to issue an ENQUIRY3 command
549 if ((aex = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_ENQ3,
550 AMR_CONFIG_ENQ3_SOLICITED_FULL)) != NULL) {
553 * Fetch current state of logical drives.
555 for (ldrv = 0; ldrv < aex->ae_numldrives; ldrv++) {
556 sc->amr_drive[ldrv].al_size = aex->ae_drivesize[ldrv];
557 sc->amr_drive[ldrv].al_state = aex->ae_drivestate[ldrv];
558 sc->amr_drive[ldrv].al_properties = aex->ae_driveprop[ldrv];
559 debug(2, " drive %d: %d state %x properties %x\n", ldrv, sc->amr_drive[ldrv].al_size,
560 sc->amr_drive[ldrv].al_state, sc->amr_drive[ldrv].al_properties);
565 * Get product info for channel count.
567 if ((ap = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0)) == NULL) {
568 device_printf(sc->amr_dev, "can't obtain product data from controller\n");
571 sc->amr_maxdrives = 40;
572 sc->amr_maxchan = ap->ap_nschan;
573 sc->amr_maxio = ap->ap_maxio;
574 sc->amr_type |= AMR_TYPE_40LD;
579 /* failed, try the 8LD ENQUIRY commands */
580 if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_EXT_ENQUIRY2, 0, 0)) == NULL) {
581 if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0)) == NULL) {
582 device_printf(sc->amr_dev, "can't obtain configuration data from controller\n");
585 ae->ae_signature = 0;
589 * Fetch current state of logical drives.
591 for (ldrv = 0; ldrv < ae->ae_ldrv.al_numdrives; ldrv++) {
592 sc->amr_drive[ldrv].al_size = ae->ae_ldrv.al_size[ldrv];
593 sc->amr_drive[ldrv].al_state = ae->ae_ldrv.al_state[ldrv];
594 sc->amr_drive[ldrv].al_properties = ae->ae_ldrv.al_properties[ldrv];
595 debug(2, " drive %d: %d state %x properties %x\n", ldrv, sc->amr_drive[ldrv].al_size,
596 sc->amr_drive[ldrv].al_state, sc->amr_drive[ldrv].al_properties);
599 sc->amr_maxdrives = 8;
600 sc->amr_maxchan = ae->ae_adapter.aa_channels;
601 sc->amr_maxio = ae->ae_adapter.aa_maxio;
606 * Mark remaining drives as unused.
608 for (; ldrv < AMR_MAXLD; ldrv++)
609 sc->amr_drive[ldrv].al_size = 0xffffffff;
612 * Cap the maximum number of outstanding I/Os. AMI's Linux driver doesn't trust
613 * the controller's reported value, and lockups have been seen when we do.
615 sc->amr_maxio = imin(sc->amr_maxio, AMR_LIMITCMD);
620 /********************************************************************************
621 * Run a generic enquiry-style command.
624 amr_enquiry(struct amr_softc *sc, size_t bufsize, u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual)
626 struct amr_command *ac;
636 /* get ourselves a command buffer */
637 if ((ac = amr_alloccmd(sc)) == NULL)
639 /* allocate the response structure */
640 if ((result = malloc(bufsize, M_DEVBUF, M_NOWAIT)) == NULL)
642 /* set command flags */
643 ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
645 /* point the command at our data */
646 ac->ac_data = result;
647 ac->ac_length = bufsize;
649 /* build the command proper */
650 mbox = (u_int8_t *)&ac->ac_mailbox; /* XXX want a real structure for this? */
655 /* can't assume that interrupts are going to work here, so play it safe */
656 if (amr_poll_command(ac))
658 error = ac->ac_status;
663 if ((error != 0) && (result != NULL)) {
664 free(result, M_DEVBUF);
670 /********************************************************************************
671 * Flush the controller's internal cache, return status.
674 amr_flush(struct amr_softc *sc)
676 struct amr_command *ac;
679 /* get ourselves a command buffer */
681 if ((ac = amr_alloccmd(sc)) == NULL)
683 /* set command flags */
684 ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
686 /* build the command proper */
687 ac->ac_mailbox.mb_command = AMR_CMD_FLUSH;
689 /* we have to poll, as the system may be going down or otherwise damaged */
690 if (amr_poll_command(ac))
692 error = ac->ac_status;
700 /********************************************************************************
701 * Try to find I/O work for the controller from one or more of the work queues.
703 * We make the assumption that if the controller is not ready to take a command
704 * at some given time, it will generate an interrupt at some later time when
708 amr_startio(struct amr_softc *sc)
710 struct amr_command *ac;
712 /* spin until something prevents us from doing any work */
715 /* try to get a ready command */
716 ac = amr_dequeue_ready(sc);
718 /* if that failed, build a command from a bio */
720 (void)amr_bio_command(sc, &ac);
722 #ifdef AMR_SCSI_PASSTHROUGH
723 /* if that failed, build a command from a ccb */
725 (void)amr_cam_command(sc, &ac);
728 /* if we don't have anything to do, give up */
732 /* try to give the command to the controller; if this fails save it for later and give up */
734 debug(2, "controller busy, command deferred");
735 amr_requeue_ready(ac); /* XXX schedule retry very soon? */
741 /********************************************************************************
742 * Handle completion of an I/O command.
745 amr_completeio(struct amr_command *ac)
747 struct amr_softc *sc = ac->ac_sc;
749 if (ac->ac_status != AMR_STATUS_SUCCESS) { /* could be more verbose here? */
750 ac->ac_bio->bio_error = EIO;
751 ac->ac_bio->bio_flags |= BIO_ERROR;
753 device_printf(sc->amr_dev, "I/O error - 0x%x\n", ac->ac_status);
754 /* amr_printcommand(ac);*/
756 amrd_intr(ac->ac_bio);
760 /********************************************************************************
761 ********************************************************************************
763 ********************************************************************************
764 ********************************************************************************/
766 /********************************************************************************
767 * Convert a bio off the top of the bio queue into a command.
770 amr_bio_command(struct amr_softc *sc, struct amr_command **acp)
772 struct amr_command *ac;
773 struct amrd_softc *amrd;
783 /* get a bio to work on */
784 if ((bio = amr_dequeue_bio(sc)) == NULL)
788 if ((ac = amr_alloccmd(sc)) == NULL) {
793 /* connect the bio to the command */
794 ac->ac_complete = amr_completeio;
796 ac->ac_data = bio->bio_data;
797 ac->ac_length = bio->bio_bcount;
798 if (BIO_IS_READ(bio)) {
799 ac->ac_flags |= AMR_CMD_DATAIN;
802 ac->ac_flags |= AMR_CMD_DATAOUT;
803 cmd = AMR_CMD_LWRITE;
805 amrd = (struct amrd_softc *)bio->bio_dev->si_drv1;
806 driveno = amrd->amrd_drive - sc->amr_drive;
807 blkcount = (bio->bio_bcount + AMR_BLKSIZE - 1) / AMR_BLKSIZE;
809 ac->ac_mailbox.mb_command = cmd;
810 ac->ac_mailbox.mb_blkcount = blkcount;
811 ac->ac_mailbox.mb_lba = bio->bio_pblkno;
812 ac->ac_mailbox.mb_drive = driveno;
813 /* we fill in the s/g related data when the command is mapped */
815 if ((bio->bio_pblkno + blkcount) > sc->amr_drive[driveno].al_size)
816 device_printf(sc->amr_dev, "I/O beyond end of unit (%lld,%d > %lu)\n",
817 (long long)bio->bio_pblkno, blkcount,
818 (u_long)sc->amr_drive[driveno].al_size);
824 if (bio != NULL) /* this breaks ordering... */
825 amr_enqueue_bio(sc, bio);
831 /********************************************************************************
832 * Take a command, submit it to the controller and sleep until it completes
833 * or fails. Interrupts must be enabled, returns nonzero on error.
836 amr_wait_command(struct amr_command *ac)
842 ac->ac_complete = NULL;
843 ac->ac_flags |= AMR_CMD_SLEEP;
844 if ((error = amr_start(ac)) != 0)
848 /* XXX better timeout? */
849 while ((ac->ac_flags & AMR_CMD_BUSY) && (count < 30)) {
850 tsleep(ac, PRIBIO | PCATCH, "amrwcmd", hz);
855 /********************************************************************************
856 * Take a command, submit it to the controller and busy-wait for it to return.
857 * Returns nonzero on error. Can be safely called with interrupts enabled.
860 amr_poll_command(struct amr_command *ac)
862 struct amr_softc *sc = ac->ac_sc;
867 ac->ac_complete = NULL;
868 if ((error = amr_start(ac)) != 0)
874 * Poll for completion, although the interrupt handler may beat us to it.
875 * Note that the timeout here is somewhat arbitrary.
879 } while ((ac->ac_flags & AMR_CMD_BUSY) && (count++ < 1000));
880 if (!(ac->ac_flags & AMR_CMD_BUSY)) {
883 /* XXX the slot is now marked permanently busy */
885 device_printf(sc->amr_dev, "polled command timeout\n");
890 /********************************************************************************
891 * Get a free command slot for a command if it doesn't already have one.
893 * May be safely called multiple times for a given command.
896 amr_getslot(struct amr_command *ac)
898 struct amr_softc *sc = ac->ac_sc;
899 int s, slot, limit, error;
903 /* if the command already has a slot, don't try to give it another one */
904 if (ac->ac_slot != 0)
907 /* enforce slot usage limit */
908 limit = (ac->ac_flags & AMR_CMD_PRIORITY) ? sc->amr_maxio : sc->amr_maxio - 4;
909 if (sc->amr_busyslots > limit)
913 * Allocate a slot. XXX linear scan is slow
917 for (slot = 0; slot < sc->amr_maxio; slot++) {
918 if (sc->amr_busycmd[slot] == NULL) {
919 sc->amr_busycmd[slot] = ac;
931 /********************************************************************************
932 * Map/unmap (ac)'s data in the controller's addressable space as required.
934 * These functions may be safely called multiple times on a given command.
937 amr_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
939 struct amr_command *ac = (struct amr_command *)arg;
940 struct amr_softc *sc = ac->ac_sc;
941 struct amr_sgentry *sg;
947 /* get base address of s/g table */
948 sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
950 /* save data physical address */
951 ac->ac_dataphys = segs[0].ds_addr;
953 /* for AMR_CMD_CONFIG the s/g count goes elsewhere */
954 if (ac->ac_mailbox.mb_command == AMR_CMD_CONFIG) {
955 sgc = &(((struct amr_mailbox_ioctl *)&ac->ac_mailbox)->mb_param);
957 sgc = &ac->ac_mailbox.mb_nsgelem;
960 /* decide whether we need to populate the s/g table */
963 ac->ac_mailbox.mb_physaddr = ac->ac_dataphys;
966 ac->ac_mailbox.mb_physaddr = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry));
967 for (i = 0; i < nsegments; i++, sg++) {
968 sg->sg_addr = segs[i].ds_addr;
969 sg->sg_count = segs[i].ds_len;
975 amr_setup_ccbmap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
977 struct amr_command *ac = (struct amr_command *)arg;
978 struct amr_softc *sc = ac->ac_sc;
979 struct amr_sgentry *sg;
980 struct amr_passthrough *ap = (struct amr_passthrough *)ac->ac_data;
983 /* get base address of s/g table */
984 sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
986 /* save s/g table information in passthrough */
987 ap->ap_no_sg_elements = nsegments;
988 ap->ap_data_transfer_address = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry));
990 /* save pointer to passthrough in command XXX is this already done above? */
991 ac->ac_mailbox.mb_physaddr = ac->ac_dataphys;
993 debug(3, "slot %d %d segments at 0x%x, passthrough at 0x%x", ac->ac_slot,
994 ap->ap_no_sg_elements, ap->ap_data_transfer_address, ac->ac_dataphys);
996 /* populate s/g table (overwrites previous call which mapped the passthrough) */
997 for (i = 0; i < nsegments; i++, sg++) {
998 sg->sg_addr = segs[i].ds_addr;
999 sg->sg_count = segs[i].ds_len;
1000 debug(3, " %d: 0x%x/%d", i, sg->sg_addr, sg->sg_count);
1005 amr_mapcmd(struct amr_command *ac)
1007 struct amr_softc *sc = ac->ac_sc;
1011 /* if the command involves data at all, and hasn't been mapped */
1012 if (!(ac->ac_flags & AMR_CMD_MAPPED)) {
1014 if (ac->ac_data != NULL) {
1015 /* map the data buffers into bus space and build the s/g list */
1016 bus_dmamap_load(sc->amr_buffer_dmat, ac->ac_dmamap, ac->ac_data, ac->ac_length,
1017 amr_setup_dmamap, ac, 0);
1018 if (ac->ac_flags & AMR_CMD_DATAIN)
1019 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_PREREAD);
1020 if (ac->ac_flags & AMR_CMD_DATAOUT)
1021 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_PREWRITE);
1024 if (ac->ac_ccb_data != NULL) {
1025 bus_dmamap_load(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, ac->ac_ccb_data, ac->ac_ccb_length,
1026 amr_setup_ccbmap, ac, 0);
1027 if (ac->ac_flags & AMR_CMD_CCB_DATAIN)
1028 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_PREREAD);
1029 if (ac->ac_flags & AMR_CMD_CCB_DATAOUT)
1030 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_PREWRITE);
1032 ac->ac_flags |= AMR_CMD_MAPPED;
1037 amr_unmapcmd(struct amr_command *ac)
1039 struct amr_softc *sc = ac->ac_sc;
1043 /* if the command involved data at all and was mapped */
1044 if (ac->ac_flags & AMR_CMD_MAPPED) {
1046 if (ac->ac_data != NULL) {
1047 if (ac->ac_flags & AMR_CMD_DATAIN)
1048 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_POSTREAD);
1049 if (ac->ac_flags & AMR_CMD_DATAOUT)
1050 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_POSTWRITE);
1051 bus_dmamap_unload(sc->amr_buffer_dmat, ac->ac_dmamap);
1054 if (ac->ac_ccb_data != NULL) {
1055 if (ac->ac_flags & AMR_CMD_CCB_DATAIN)
1056 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_POSTREAD);
1057 if (ac->ac_flags & AMR_CMD_CCB_DATAOUT)
1058 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_POSTWRITE);
1059 bus_dmamap_unload(sc->amr_buffer_dmat, ac->ac_ccb_dmamap);
1061 ac->ac_flags &= ~AMR_CMD_MAPPED;
1065 /********************************************************************************
1066 * Take a command and give it to the controller, returns 0 if successful, or
1067 * EBUSY if the command should be retried later.
1070 amr_start(struct amr_command *ac)
1072 struct amr_softc *sc = ac->ac_sc;
1077 /* mark command as busy so that polling consumer can tell */
1078 ac->ac_flags |= AMR_CMD_BUSY;
1080 /* get a command slot (freed in amr_done) */
1081 if (amr_getslot(ac))
1084 /* now we have a slot, we can map the command (unmapped in amr_complete) */
1087 /* mark the new mailbox we are going to copy in as busy */
1088 ac->ac_mailbox.mb_busy = 1;
1090 /* clear the poll/ack fields in the mailbox */
1091 sc->amr_mailbox->mb_poll = 0;
1092 sc->amr_mailbox->mb_ack = 0;
1095 * Save the slot number so that we can locate this command when complete.
1096 * Note that ident = 0 seems to be special, so we don't use it.
1098 ac->ac_mailbox.mb_ident = ac->ac_slot + 1;
1101 * Spin waiting for the mailbox, give up after ~1 second. We expect the
1102 * controller to be able to handle our I/O.
1104 * XXX perhaps we should wait for less time, and count on the deferred command
1105 * handling to deal with retries?
1107 debug(4, "wait for mailbox");
1108 for (i = 10000, done = 0; (i > 0) && !done; i--) {
1111 /* is the mailbox free? */
1112 if (sc->amr_mailbox->mb_busy == 0) {
1113 debug(4, "got mailbox");
1114 sc->amr_mailbox64->mb64_segment = 0;
1115 bcopy(&ac->ac_mailbox, (void *)(uintptr_t)(volatile void *)sc->amr_mailbox, AMR_MBOX_CMDSIZE);
1118 /* not free, spin waiting */
1120 debug(4, "busy flag %x\n", sc->amr_mailbox->mb_busy);
1121 /* this is somewhat ugly */
1124 splx(s); /* drop spl to allow completion interrupts */
1128 * Now give the command to the controller
1131 if (sc->amr_submit_command(sc)) {
1132 /* the controller wasn't ready to take the command, forget that we tried to post it */
1133 sc->amr_mailbox->mb_busy = 0;
1136 debug(3, "posted command");
1141 * The controller wouldn't take the command. Return the command as busy
1142 * so that it is retried later.
1147 /********************************************************************************
1148 * Extract one or more completed commands from the controller (sc)
1150 * Returns nonzero if any commands on the work queue were marked as completed.
1153 amr_done(struct amr_softc *sc)
1155 struct amr_command *ac;
1156 struct amr_mailbox mbox;
1161 /* See if there's anything for us to do */
1164 /* loop collecting completed commands */
1166 /* poll for a completed command's identifier and status */
1167 if (sc->amr_get_work(sc, &mbox)) {
1170 /* iterate over completed commands in this result */
1171 for (i = 0; i < mbox.mb_nstatus; i++) {
1172 /* get pointer to busy command */
1173 idx = mbox.mb_completed[i] - 1;
1174 ac = sc->amr_busycmd[idx];
1176 /* really a busy command? */
1179 /* pull the command from the busy index */
1180 sc->amr_busycmd[idx] = NULL;
1181 sc->amr_busyslots--;
1183 /* save status for later use */
1184 ac->ac_status = mbox.mb_status;
1185 amr_enqueue_completed(ac);
1186 debug(3, "completed command with status %x", mbox.mb_status);
1188 device_printf(sc->amr_dev, "bad slot %d completed\n", idx);
1192 break; /* no work */
1196 /* if we've completed any commands, try posting some more */
1200 /* handle completion and timeouts */
1201 #if __FreeBSD_version >= 500005
1202 if (sc->amr_state & AMR_STATE_INTEN)
1203 taskqueue_enqueue(taskqueue_swi, &sc->amr_task_complete);
1206 amr_complete(sc, 0);
1211 /********************************************************************************
1212 * Do completion processing on done commands on (sc)
1215 amr_complete(void *context, int pending)
1217 struct amr_softc *sc = (struct amr_softc *)context;
1218 struct amr_command *ac;
1222 /* pull completed commands off the queue */
1224 ac = amr_dequeue_completed(sc);
1228 /* unmap the command's data buffer */
1231 /* unbusy the command */
1232 ac->ac_flags &= ~AMR_CMD_BUSY;
1235 * Is there a completion handler?
1237 if (ac->ac_complete != NULL) {
1238 ac->ac_complete(ac);
1241 * Is someone sleeping on this one?
1243 } else if (ac->ac_flags & AMR_CMD_SLEEP) {
1249 /********************************************************************************
1250 ********************************************************************************
1251 Command Buffer Management
1252 ********************************************************************************
1253 ********************************************************************************/
1255 /********************************************************************************
1256 * Get a new command buffer.
1258 * This may return NULL in low-memory cases.
1260 * If possible, we recycle a command buffer that's been used before.
1262 struct amr_command *
1263 amr_alloccmd(struct amr_softc *sc)
1265 struct amr_command *ac;
1269 ac = amr_dequeue_free(sc);
1271 amr_alloccmd_cluster(sc);
1272 ac = amr_dequeue_free(sc);
1277 /* clear out significant fields */
1280 bzero(&ac->ac_mailbox, sizeof(struct amr_mailbox));
1284 ac->ac_ccb_data = NULL;
1285 ac->ac_complete = NULL;
1289 /********************************************************************************
1290 * Release a command buffer for recycling.
1293 amr_releasecmd(struct amr_command *ac)
1297 amr_enqueue_free(ac);
1300 /********************************************************************************
1301 * Allocate a new command cluster and initialise it.
1304 amr_alloccmd_cluster(struct amr_softc *sc)
1306 struct amr_command_cluster *acc;
1307 struct amr_command *ac;
1310 acc = malloc(AMR_CMD_CLUSTERSIZE, M_DEVBUF, M_NOWAIT);
1313 TAILQ_INSERT_TAIL(&sc->amr_cmd_clusters, acc, acc_link);
1315 for (i = 0; i < AMR_CMD_CLUSTERCOUNT; i++) {
1316 ac = &acc->acc_command[i];
1317 bzero(ac, sizeof(*ac));
1319 if (!bus_dmamap_create(sc->amr_buffer_dmat, 0, &ac->ac_dmamap) &&
1320 !bus_dmamap_create(sc->amr_buffer_dmat, 0, &ac->ac_ccb_dmamap))
1326 /********************************************************************************
1327 * Free a command cluster
1330 amr_freecmd_cluster(struct amr_command_cluster *acc)
1332 struct amr_softc *sc = acc->acc_command[0].ac_sc;
1335 for (i = 0; i < AMR_CMD_CLUSTERCOUNT; i++)
1336 bus_dmamap_destroy(sc->amr_buffer_dmat, acc->acc_command[i].ac_dmamap);
1337 free(acc, M_DEVBUF);
1340 /********************************************************************************
1341 ********************************************************************************
1342 Interface-specific Shims
1343 ********************************************************************************
1344 ********************************************************************************/
1346 /********************************************************************************
1347 * Tell the controller that the mailbox contains a valid command
1350 amr_quartz_submit_command(struct amr_softc *sc)
1354 if (AMR_QGET_IDB(sc) & AMR_QIDB_SUBMIT)
1356 AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_SUBMIT);
1361 amr_std_submit_command(struct amr_softc *sc)
1365 if (AMR_SGET_MBSTAT(sc) & AMR_SMBOX_BUSYFLAG)
1367 AMR_SPOST_COMMAND(sc);
1371 /********************************************************************************
1372 * Claim any work that the controller has completed; acknowledge completion,
1373 * save details of the completion in (mbsave)
1376 amr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave)
1386 /* work waiting for us? */
1387 if ((outd = AMR_QGET_ODB(sc)) == AMR_QODB_READY) {
1389 /* save mailbox, which contains a list of completed commands */
1390 bcopy((void *)(uintptr_t)(volatile void *)sc->amr_mailbox, mbsave, sizeof(*mbsave));
1392 /* acknowledge interrupt */
1393 AMR_QPUT_ODB(sc, AMR_QODB_READY);
1395 /* acknowledge that we have the commands */
1396 AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_ACK);
1398 #ifndef AMR_QUARTZ_GOFASTER
1400 * This waits for the controller to notice that we've taken the
1401 * command from it. It's very inefficient, and we shouldn't do it,
1402 * but if we remove this code, we stop completing commands under
1405 * Peter J says we shouldn't do this. The documentation says we
1406 * should. Who is right?
1408 while(AMR_QGET_IDB(sc) & AMR_QIDB_ACK)
1409 ; /* XXX aiee! what if it dies? */
1412 worked = 1; /* got some work */
1420 amr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave)
1430 /* check for valid interrupt status */
1431 istat = AMR_SGET_ISTAT(sc);
1432 if ((istat & AMR_SINTR_VALID) != 0) {
1433 AMR_SPUT_ISTAT(sc, istat); /* ack interrupt status */
1435 /* save mailbox, which contains a list of completed commands */
1436 bcopy((void *)(uintptr_t)(volatile void *)sc->amr_mailbox, mbsave, sizeof(*mbsave));
1438 AMR_SACK_INTERRUPT(sc); /* acknowledge we have the mailbox */
1446 /********************************************************************************
1447 * Notify the controller of the mailbox location.
1450 amr_std_attach_mailbox(struct amr_softc *sc)
1453 /* program the mailbox physical address */
1454 AMR_SBYTE_SET(sc, AMR_SMBOX_0, sc->amr_mailboxphys & 0xff);
1455 AMR_SBYTE_SET(sc, AMR_SMBOX_1, (sc->amr_mailboxphys >> 8) & 0xff);
1456 AMR_SBYTE_SET(sc, AMR_SMBOX_2, (sc->amr_mailboxphys >> 16) & 0xff);
1457 AMR_SBYTE_SET(sc, AMR_SMBOX_3, (sc->amr_mailboxphys >> 24) & 0xff);
1458 AMR_SBYTE_SET(sc, AMR_SMBOX_ENABLE, AMR_SMBOX_ADDR);
1460 /* clear any outstanding interrupt and enable interrupts proper */
1461 AMR_SACK_INTERRUPT(sc);
1462 AMR_SENABLE_INTR(sc);
1465 #ifdef AMR_BOARD_INIT
1466 /********************************************************************************
1467 * Initialise the controller
1470 amr_quartz_init(struct amr_softc *sc)
1472 int status, ostatus;
1474 device_printf(sc->amr_dev, "initial init status %x\n", AMR_QGET_INITSTATUS(sc));
1479 while ((status = AMR_QGET_INITSTATUS(sc)) != AMR_QINIT_DONE) {
1480 if (status != ostatus) {
1481 device_printf(sc->amr_dev, "(%x) %s\n", status, amr_describe_code(amr_table_qinit, status));
1485 case AMR_QINIT_NOMEM:
1488 case AMR_QINIT_SCAN:
1489 /* XXX we could print channel/target here */
1497 amr_std_init(struct amr_softc *sc)
1499 int status, ostatus;
1501 device_printf(sc->amr_dev, "initial init status %x\n", AMR_SGET_INITSTATUS(sc));
1506 while ((status = AMR_SGET_INITSTATUS(sc)) != AMR_SINIT_DONE) {
1507 if (status != ostatus) {
1508 device_printf(sc->amr_dev, "(%x) %s\n", status, amr_describe_code(amr_table_sinit, status));
1512 case AMR_SINIT_NOMEM:
1515 case AMR_SINIT_INPROG:
1516 /* XXX we could print channel/target here? */
1524 /********************************************************************************
1525 ********************************************************************************
1527 ********************************************************************************
1528 ********************************************************************************/
1530 /********************************************************************************
1531 * Identify the controller and print some information about it.
1534 amr_describe_controller(struct amr_softc *sc)
1536 struct amr_prodinfo *ap;
1537 struct amr_enquiry *ae;
1541 * Try to get 40LD product info, which tells us what the card is labelled as.
1543 if ((ap = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0)) != NULL) {
1544 device_printf(sc->amr_dev, "<%.80s> Firmware %.16s, BIOS %.16s, %dMB RAM\n",
1545 ap->ap_product, ap->ap_firmware, ap->ap_bios,
1553 * Try 8LD extended ENQUIRY to get controller signature, and use lookup table.
1555 if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_EXT_ENQUIRY2, 0, 0)) != NULL) {
1556 prod = amr_describe_code(amr_table_adaptertype, ae->ae_signature);
1558 } else if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0)) != NULL) {
1561 * Try to work it out based on the PCI signatures.
1563 switch (pci_get_device(sc->amr_dev)) {
1565 prod = "Series 428";
1568 prod = "Series 434";
1571 prod = "unknown controller";
1575 prod = "unsupported controller";
1579 * HP NetRaid controllers have a special encoding of the firmware and
1580 * BIOS versions. The AMI version seems to have it as strings whereas
1581 * the HP version does it with a leading uppercase character and two
1585 if(ae->ae_adapter.aa_firmware[2] >= 'A' &&
1586 ae->ae_adapter.aa_firmware[2] <= 'Z' &&
1587 ae->ae_adapter.aa_firmware[1] < ' ' &&
1588 ae->ae_adapter.aa_firmware[0] < ' ' &&
1589 ae->ae_adapter.aa_bios[2] >= 'A' &&
1590 ae->ae_adapter.aa_bios[2] <= 'Z' &&
1591 ae->ae_adapter.aa_bios[1] < ' ' &&
1592 ae->ae_adapter.aa_bios[0] < ' ') {
1594 /* this looks like we have an HP NetRaid version of the MegaRaid */
1596 if(ae->ae_signature == AMR_SIG_438) {
1597 /* the AMI 438 is an NetRaid 3si in HP-land */
1598 prod = "HP NetRaid 3si";
1601 device_printf(sc->amr_dev, "<%s> Firmware %c.%02d.%02d, BIOS %c.%02d.%02d, %dMB RAM\n",
1602 prod, ae->ae_adapter.aa_firmware[2],
1603 ae->ae_adapter.aa_firmware[1],
1604 ae->ae_adapter.aa_firmware[0],
1605 ae->ae_adapter.aa_bios[2],
1606 ae->ae_adapter.aa_bios[1],
1607 ae->ae_adapter.aa_bios[0],
1608 ae->ae_adapter.aa_memorysize);
1610 device_printf(sc->amr_dev, "<%s> Firmware %.4s, BIOS %.4s, %dMB RAM\n",
1611 prod, ae->ae_adapter.aa_firmware, ae->ae_adapter.aa_bios,
1612 ae->ae_adapter.aa_memorysize);
1618 /********************************************************************************
1619 * Print the command (ac) in human-readable format
1622 amr_printcommand(struct amr_command *ac)
1624 struct amr_softc *sc = ac->ac_sc;
1625 struct amr_sgentry *sg;
1628 device_printf(sc->amr_dev, "cmd %x ident %d drive %d\n",
1629 ac->ac_mailbox.mb_command, ac->ac_mailbox.mb_ident, ac->ac_mailbox.mb_drive);
1630 device_printf(sc->amr_dev, "blkcount %d lba %d\n",
1631 ac->ac_mailbox.mb_blkcount, ac->ac_mailbox.mb_lba);
1632 device_printf(sc->amr_dev, "virtaddr %p length %lu\n", ac->ac_data, (unsigned long)ac->ac_length);
1633 device_printf(sc->amr_dev, "sg physaddr %08x nsg %d\n",
1634 ac->ac_mailbox.mb_physaddr, ac->ac_mailbox.mb_nsgelem);
1635 device_printf(sc->amr_dev, "ccb %p bio %p\n", ac->ac_ccb_data, ac->ac_bio);
1637 /* get base address of s/g table */
1638 sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
1639 for (i = 0; i < ac->ac_mailbox.mb_nsgelem; i++, sg++)
1640 device_printf(sc->amr_dev, " %x/%d\n", sg->sg_addr, sg->sg_count);