2 * Copyright (c) 1999 Michael Smith
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/dev/mlx/mlx.c,v 1.14.2.5 2001/09/11 09:49:53 kris Exp $
27 * $DragonFly: src/sys/dev/raid/mlx/mlx.c,v 1.9 2004/06/21 15:39:31 dillon Exp $
31 * Driver for the Mylex DAC960 family of RAID controllers.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
41 #include <sys/devicestat.h>
45 #include <machine/resource.h>
46 #include <machine/bus_memio.h>
47 #include <machine/bus_pio.h>
48 #include <machine/bus.h>
49 #include <machine/clock.h>
52 #include "mlx_compat.h"
57 #define MLX_CDEV_MAJOR 130
59 static struct cdevsw mlx_cdevsw = {
61 /* maj */ MLX_CDEV_MAJOR,
67 /* close */ mlx_close,
70 /* ioctl */ mlx_ioctl,
73 /* strategy */ nostrategy,
78 devclass_t mlx_devclass;
81 * Per-interface accessor methods
83 static int mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
84 static int mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
85 static void mlx_v3_intaction(struct mlx_softc *sc, int action);
86 static int mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2);
88 static int mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
89 static int mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
90 static void mlx_v4_intaction(struct mlx_softc *sc, int action);
91 static int mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2);
93 static int mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
94 static int mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
95 static void mlx_v5_intaction(struct mlx_softc *sc, int action);
96 static int mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2);
101 static void mlx_periodic(void *data);
102 static void mlx_periodic_enquiry(struct mlx_command *mc);
103 static void mlx_periodic_eventlog_poll(struct mlx_softc *sc);
104 static void mlx_periodic_eventlog_respond(struct mlx_command *mc);
105 static void mlx_periodic_rebuild(struct mlx_command *mc);
110 static void mlx_pause_action(struct mlx_softc *sc);
111 static void mlx_pause_done(struct mlx_command *mc);
114 * Command submission.
116 static void *mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize,
117 void (*complete)(struct mlx_command *mc));
118 static int mlx_flush(struct mlx_softc *sc);
119 static int mlx_check(struct mlx_softc *sc, int drive);
120 static int mlx_rebuild(struct mlx_softc *sc, int channel, int target);
121 static int mlx_wait_command(struct mlx_command *mc);
122 static int mlx_poll_command(struct mlx_command *mc);
123 static void mlx_startio(struct mlx_softc *sc);
124 static void mlx_completeio(struct mlx_command *mc);
125 static int mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu);
128 * Command buffer allocation.
130 static struct mlx_command *mlx_alloccmd(struct mlx_softc *sc);
131 static void mlx_releasecmd(struct mlx_command *mc);
132 static void mlx_freecmd(struct mlx_command *mc);
135 * Command management.
137 static int mlx_getslot(struct mlx_command *mc);
138 static void mlx_mapcmd(struct mlx_command *mc);
139 static void mlx_unmapcmd(struct mlx_command *mc);
140 static int mlx_start(struct mlx_command *mc);
141 static int mlx_done(struct mlx_softc *sc);
142 static void mlx_complete(struct mlx_softc *sc);
147 static char *mlx_diagnose_command(struct mlx_command *mc);
148 static void mlx_describe_controller(struct mlx_softc *sc);
149 static int mlx_fw_message(struct mlx_softc *sc, int status, int param1, int param2);
154 static struct mlx_sysdrive *mlx_findunit(struct mlx_softc *sc, int unit);
156 /********************************************************************************
157 ********************************************************************************
159 ********************************************************************************
160 ********************************************************************************/
162 /********************************************************************************
163 * Free all of the resources associated with (sc)
165 * Should not be called if the controller is active.
168 mlx_free(struct mlx_softc *sc)
170 struct mlx_command *mc;
174 /* cancel status timeout */
175 untimeout(mlx_periodic, sc, sc->mlx_timeout);
177 /* throw away any command buffers */
178 while ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL) {
179 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
183 /* destroy data-transfer DMA tag */
184 if (sc->mlx_buffer_dmat)
185 bus_dma_tag_destroy(sc->mlx_buffer_dmat);
187 /* free and destroy DMA memory and tag for s/g lists */
189 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
191 bus_dma_tag_destroy(sc->mlx_sg_dmat);
193 /* disconnect the interrupt handler */
195 bus_teardown_intr(sc->mlx_dev, sc->mlx_irq, sc->mlx_intr);
196 if (sc->mlx_irq != NULL)
197 bus_release_resource(sc->mlx_dev, SYS_RES_IRQ, 0, sc->mlx_irq);
199 /* destroy the parent DMA tag */
200 if (sc->mlx_parent_dmat)
201 bus_dma_tag_destroy(sc->mlx_parent_dmat);
203 /* release the register window mapping */
204 if (sc->mlx_mem != NULL)
205 bus_release_resource(sc->mlx_dev, sc->mlx_mem_type, sc->mlx_mem_rid, sc->mlx_mem);
207 /* free controller enquiry data */
208 if (sc->mlx_enq2 != NULL)
209 free(sc->mlx_enq2, M_DEVBUF);
211 cdevsw_remove(&mlx_cdevsw, -1, device_get_unit(sc->mlx_dev));
214 /********************************************************************************
215 * Map the scatter/gather table into bus space
218 mlx_dma_map_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
220 struct mlx_softc *sc = (struct mlx_softc *)arg;
224 /* save base of s/g table's address in bus space */
225 sc->mlx_sgbusaddr = segs->ds_addr;
229 mlx_sglist_map(struct mlx_softc *sc)
236 /* destroy any existing mappings */
238 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
240 bus_dma_tag_destroy(sc->mlx_sg_dmat);
243 * Create a single tag describing a region large enough to hold all of
244 * the s/g lists we will need. If we're called early on, we don't know how
245 * many commands we're going to be asked to support, so only allocate enough
248 if (sc->mlx_enq2 == NULL) {
251 ncmd = sc->mlx_enq2->me_max_commands;
253 segsize = sizeof(struct mlx_sgentry) * MLX_NSEG * ncmd;
254 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */
255 1, 0, /* alignment, boundary */
256 BUS_SPACE_MAXADDR, /* lowaddr */
257 BUS_SPACE_MAXADDR, /* highaddr */
258 NULL, NULL, /* filter, filterarg */
259 segsize, 1, /* maxsize, nsegments */
260 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
264 device_printf(sc->mlx_dev, "can't allocate scatter/gather DMA tag\n");
269 * Allocate enough s/g maps for all commands and permanently map them into
270 * controller-visible space.
272 * XXX this assumes we can get enough space for all the s/g maps in one
273 * contiguous slab. We may need to switch to a more complex arrangement where
274 * we allocate in smaller chunks and keep a lookup table from slot to bus address.
276 error = bus_dmamem_alloc(sc->mlx_sg_dmat, (void **)&sc->mlx_sgtable, BUS_DMA_NOWAIT, &sc->mlx_sg_dmamap);
278 device_printf(sc->mlx_dev, "can't allocate s/g table\n");
281 bus_dmamap_load(sc->mlx_sg_dmat, sc->mlx_sg_dmamap, sc->mlx_sgtable, segsize, mlx_dma_map_sg, sc, 0);
285 /********************************************************************************
286 * Initialise the controller and softc
289 mlx_attach(struct mlx_softc *sc)
291 struct mlx_enquiry_old *meo;
292 int rid, error, fwminor, hscode, hserror, hsparam1, hsparam2, hsmsg;
297 * Initialise per-controller queues.
299 TAILQ_INIT(&sc->mlx_work);
300 TAILQ_INIT(&sc->mlx_freecmds);
301 MLX_BIO_QINIT(sc->mlx_bioq);
304 * Select accessor methods based on controller interface type.
306 switch(sc->mlx_iftype) {
309 sc->mlx_tryqueue = mlx_v3_tryqueue;
310 sc->mlx_findcomplete = mlx_v3_findcomplete;
311 sc->mlx_intaction = mlx_v3_intaction;
312 sc->mlx_fw_handshake = mlx_v3_fw_handshake;
315 sc->mlx_tryqueue = mlx_v4_tryqueue;
316 sc->mlx_findcomplete = mlx_v4_findcomplete;
317 sc->mlx_intaction = mlx_v4_intaction;
318 sc->mlx_fw_handshake = mlx_v4_fw_handshake;
321 sc->mlx_tryqueue = mlx_v5_tryqueue;
322 sc->mlx_findcomplete = mlx_v5_findcomplete;
323 sc->mlx_intaction = mlx_v5_intaction;
324 sc->mlx_fw_handshake = mlx_v5_fw_handshake;
328 return(ENXIO); /* should never happen */
331 /* disable interrupts before we start talking to the controller */
332 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
335 * Wait for the controller to come ready, handshake with the firmware if required.
336 * This is typically only necessary on platforms where the controller BIOS does not
341 while ((hscode = sc->mlx_fw_handshake(sc, &hserror, &hsparam1, &hsparam2)) != 0) {
342 /* report first time around... */
344 device_printf(sc->mlx_dev, "controller initialisation in progress...\n");
347 /* did we get a real message? */
349 hscode = mlx_fw_message(sc, hserror, hsparam1, hsparam2);
350 /* fatal initialisation error? */
358 device_printf(sc->mlx_dev, "initialisation complete.\n");
361 * Allocate and connect our interrupt.
364 sc->mlx_irq = bus_alloc_resource(sc->mlx_dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
365 if (sc->mlx_irq == NULL) {
366 device_printf(sc->mlx_dev, "can't allocate interrupt\n");
370 error = bus_setup_intr(sc->mlx_dev, sc->mlx_irq, INTR_TYPE_BIO | INTR_ENTROPY, mlx_intr, sc, &sc->mlx_intr);
372 device_printf(sc->mlx_dev, "can't set up interrupt\n");
378 * Create DMA tag for mapping buffers into controller-addressable space.
380 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */
381 1, 0, /* alignment, boundary */
382 BUS_SPACE_MAXADDR, /* lowaddr */
383 BUS_SPACE_MAXADDR, /* highaddr */
384 NULL, NULL, /* filter, filterarg */
385 MAXBSIZE, MLX_NSEG, /* maxsize, nsegments */
386 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
388 &sc->mlx_buffer_dmat);
390 device_printf(sc->mlx_dev, "can't allocate buffer DMA tag\n");
396 * Create some initial scatter/gather mappings so we can run the probe commands.
398 error = mlx_sglist_map(sc);
400 device_printf(sc->mlx_dev, "can't make initial s/g list mapping\n");
406 * We don't (yet) know where the event log is up to.
408 sc->mlx_currevent = -1;
411 * Obtain controller feature information
413 if ((sc->mlx_enq2 = mlx_enquire(sc, MLX_CMD_ENQUIRY2, sizeof(struct mlx_enquiry2), NULL)) == NULL) {
414 device_printf(sc->mlx_dev, "ENQUIRY2 failed\n");
420 * Do quirk/feature related things.
422 fwminor = (sc->mlx_enq2->me_firmware_id >> 8) & 0xff;
423 switch(sc->mlx_iftype) {
425 /* These controllers don't report the firmware version in the ENQUIRY2 response */
426 if ((meo = mlx_enquire(sc, MLX_CMD_ENQUIRY_OLD, sizeof(struct mlx_enquiry_old), NULL)) == NULL) {
427 device_printf(sc->mlx_dev, "ENQUIRY_OLD failed\n");
431 sc->mlx_enq2->me_firmware_id = ('0' << 24) | (0 << 16) | (meo->me_fwminor << 8) | meo->me_fwmajor;
434 /* XXX require 2.42 or better (PCI) or 2.14 or better (EISA) */
435 if (meo->me_fwminor < 42) {
436 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
437 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 2.42 or later\n");
441 /* XXX certify 3.52? */
443 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
444 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 3.51 or later\n");
448 /* XXX certify firmware versions? */
450 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
451 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 4.06 or later\n");
456 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
457 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 5.07 or later\n");
462 return(ENXIO); /* should never happen */
466 * Create the final scatter/gather mappings now that we have characterised the controller.
468 error = mlx_sglist_map(sc);
470 device_printf(sc->mlx_dev, "can't make final s/g list mapping\n");
476 * No user-requested background operation is in progress.
478 sc->mlx_background = 0;
479 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
482 * Create the control device.
484 cdevsw_add(&mlx_cdevsw, -1, device_get_unit(sc->mlx_dev));
485 make_dev(&mlx_cdevsw, device_get_unit(sc->mlx_dev),
486 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
487 "mlx%d", device_get_unit(sc->mlx_dev));
490 * Start the timeout routine.
492 sc->mlx_timeout = timeout(mlx_periodic, sc, hz);
494 /* print a little information about the controller */
495 mlx_describe_controller(sc);
500 /********************************************************************************
501 * Locate disk resources and attach children to them.
504 mlx_startup(struct mlx_softc *sc)
506 struct mlx_enq_sys_drive *mes;
507 struct mlx_sysdrive *dr;
513 * Scan all the system drives and attach children for those that
514 * don't currently have them.
516 mes = mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(*mes) * MLX_MAXDRIVES, NULL);
518 device_printf(sc->mlx_dev, "error fetching drive status\n");
522 /* iterate over drives returned */
523 for (i = 0, dr = &sc->mlx_sysdrive[0];
524 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
526 /* are we already attached to this drive? */
527 if (dr->ms_disk == 0) {
528 /* pick up drive information */
529 dr->ms_size = mes[i].sd_size;
530 dr->ms_raidlevel = mes[i].sd_raidlevel & 0xf;
531 dr->ms_state = mes[i].sd_state;
533 /* generate geometry information */
534 if (sc->mlx_geom == MLX_GEOM_128_32) {
537 dr->ms_cylinders = dr->ms_size / (128 * 32);
538 } else { /* MLX_GEOM_255/63 */
541 dr->ms_cylinders = dr->ms_size / (255 * 63);
543 dr->ms_disk = device_add_child(sc->mlx_dev, /*"mlxd"*/NULL, -1);
544 if (dr->ms_disk == 0)
545 device_printf(sc->mlx_dev, "device_add_child failed\n");
546 device_set_ivars(dr->ms_disk, dr);
550 if ((error = bus_generic_attach(sc->mlx_dev)) != 0)
551 device_printf(sc->mlx_dev, "bus_generic_attach returned %d", error);
553 /* mark controller back up */
554 sc->mlx_state &= ~MLX_STATE_SHUTDOWN;
556 /* enable interrupts */
557 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
560 /********************************************************************************
561 * Disconnect from the controller completely, in preparation for unload.
564 mlx_detach(device_t dev)
566 struct mlx_softc *sc = device_get_softc(dev);
567 struct mlxd_softc *mlxd;
574 if (sc->mlx_state & MLX_STATE_OPEN)
577 for (i = 0; i < MLX_MAXDRIVES; i++) {
578 if (sc->mlx_sysdrive[i].ms_disk != 0) {
579 mlxd = device_get_softc(sc->mlx_sysdrive[i].ms_disk);
580 if (mlxd->mlxd_flags & MLXD_OPEN) { /* drive is mounted, abort detach */
581 device_printf(sc->mlx_sysdrive[i].ms_disk, "still open, can't detach\n");
586 if ((error = mlx_shutdown(dev)))
597 /********************************************************************************
598 * Bring the controller down to a dormant state and detach all child devices.
600 * This function is called before detach, system shutdown, or before performing
601 * an operation which may add or delete system disks. (Call mlx_startup to
602 * resume normal operation.)
604 * Note that we can assume that the bioq on the controller is empty, as we won't
605 * allow shutdown if any device is open.
608 mlx_shutdown(device_t dev)
610 struct mlx_softc *sc = device_get_softc(dev);
618 sc->mlx_state |= MLX_STATE_SHUTDOWN;
619 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
621 /* flush controller */
622 device_printf(sc->mlx_dev, "flushing cache...");
629 /* delete all our child devices */
630 for (i = 0; i < MLX_MAXDRIVES; i++) {
631 if (sc->mlx_sysdrive[i].ms_disk != 0) {
632 if ((error = device_delete_child(sc->mlx_dev, sc->mlx_sysdrive[i].ms_disk)) != 0)
634 sc->mlx_sysdrive[i].ms_disk = 0;
643 /********************************************************************************
644 * Bring the controller to a quiescent state, ready for system suspend.
647 mlx_suspend(device_t dev)
649 struct mlx_softc *sc = device_get_softc(dev);
655 sc->mlx_state |= MLX_STATE_SUSPEND;
657 /* flush controller */
658 device_printf(sc->mlx_dev, "flushing cache...");
659 printf("%s\n", mlx_flush(sc) ? "failed" : "done");
661 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
667 /********************************************************************************
668 * Bring the controller back to a state ready for operation.
671 mlx_resume(device_t dev)
673 struct mlx_softc *sc = device_get_softc(dev);
677 sc->mlx_state &= ~MLX_STATE_SUSPEND;
678 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
683 /*******************************************************************************
684 * Take an interrupt, or be poked by other code to look for interrupt-worthy
690 struct mlx_softc *sc = (struct mlx_softc *)arg;
694 /* collect finished commands, queue anything waiting */
698 /*******************************************************************************
699 * Receive a buf structure from a child device and queue it on a particular
700 * disk resource, then poke the disk resource to start as much work as it can.
703 mlx_submit_buf(struct mlx_softc *sc, mlx_bio *bp)
710 MLX_BIO_QINSERT(sc->mlx_bioq, bp);
717 /********************************************************************************
718 * Accept an open operation on the control device.
721 mlx_open(dev_t dev, int flags, int fmt, d_thread_t *td)
723 int unit = minor(dev);
724 struct mlx_softc *sc = devclass_get_softc(mlx_devclass, unit);
726 sc->mlx_state |= MLX_STATE_OPEN;
730 /********************************************************************************
731 * Accept the last close on the control device.
734 mlx_close(dev_t dev, int flags, int fmt, d_thread_t *td)
736 int unit = minor(dev);
737 struct mlx_softc *sc = devclass_get_softc(mlx_devclass, unit);
739 sc->mlx_state &= ~MLX_STATE_OPEN;
743 /********************************************************************************
744 * Handle controller-specific control operations.
747 mlx_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *td)
749 int unit = minor(dev);
750 struct mlx_softc *sc = devclass_get_softc(mlx_devclass, unit);
751 struct mlx_rebuild_request *rb = (struct mlx_rebuild_request *)addr;
752 struct mlx_rebuild_status *rs = (struct mlx_rebuild_status *)addr;
753 int *arg = (int *)addr;
754 struct mlx_pause *mp;
755 struct mlx_sysdrive *dr;
756 struct mlxd_softc *mlxd;
761 * Enumerate connected system drives; returns the first system drive's
762 * unit number if *arg is -1, or the next unit after *arg if it's
763 * a valid unit on this controller.
766 /* search system drives */
767 for (i = 0; i < MLX_MAXDRIVES; i++) {
768 /* is this one attached? */
769 if (sc->mlx_sysdrive[i].ms_disk != 0) {
770 /* looking for the next one we come across? */
772 *arg = device_get_unit(sc->mlx_sysdrive[0].ms_disk);
775 /* we want the one after this one */
776 if (*arg == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
783 * Scan the controller to see whether new drives have appeared.
785 case MLX_RESCAN_DRIVES:
790 * Disconnect from the specified drive; it may be about to go
793 case MLX_DETACH_DRIVE: /* detach one drive */
795 if (((dr = mlx_findunit(sc, *arg)) == NULL) ||
796 ((mlxd = device_get_softc(dr->ms_disk)) == NULL))
799 device_printf(dr->ms_disk, "detaching...");
801 if (mlxd->mlxd_flags & MLXD_OPEN) {
806 /* flush controller */
813 if ((error = device_delete_child(sc->mlx_dev, dr->ms_disk)) != 0)
826 * Pause one or more SCSI channels for a period of time, to assist
827 * in the process of hot-swapping devices.
829 * Note that at least the 3.51 firmware on the DAC960PL doesn't seem
832 case MLX_PAUSE_CHANNEL: /* schedule a channel pause */
833 /* Does this command work on this firmware? */
834 if (!(sc->mlx_feature & MLX_FEAT_PAUSEWORKS))
837 mp = (struct mlx_pause *)addr;
838 if ((mp->mp_which == MLX_PAUSE_CANCEL) && (sc->mlx_pause.mp_when != 0)) {
839 /* cancel a pending pause operation */
840 sc->mlx_pause.mp_which = 0;
842 /* fix for legal channels */
843 mp->mp_which &= ((1 << sc->mlx_enq2->me_actual_channels) -1);
844 /* check time values */
845 if ((mp->mp_when < 0) || (mp->mp_when > 3600))
847 if ((mp->mp_howlong < 1) || (mp->mp_howlong > (0xf * 30)))
850 /* check for a pause currently running */
851 if ((sc->mlx_pause.mp_which != 0) && (sc->mlx_pause.mp_when == 0))
854 /* looks ok, go with it */
855 sc->mlx_pause.mp_which = mp->mp_which;
856 sc->mlx_pause.mp_when = time_second + mp->mp_when;
857 sc->mlx_pause.mp_howlong = sc->mlx_pause.mp_when + mp->mp_howlong;
862 * Accept a command passthrough-style.
865 return(mlx_user_command(sc, (struct mlx_usercommand *)addr));
868 * Start a rebuild on a given SCSI disk
870 case MLX_REBUILDASYNC:
871 if (sc->mlx_background != 0) {
872 rb->rr_status = 0x0106;
875 rb->rr_status = mlx_rebuild(sc, rb->rr_channel, rb->rr_target);
876 switch (rb->rr_status) {
881 error = ENOMEM; /* couldn't set up the command */
900 sc->mlx_background = MLX_BACKGROUND_REBUILD;
904 * Get the status of the current rebuild or consistency check.
906 case MLX_REBUILDSTAT:
907 *rs = sc->mlx_rebuildstat;
911 * Return the per-controller system drive number matching the
912 * disk device number in (arg), if it happens to belong to us.
914 case MLX_GET_SYSDRIVE:
916 mlxd = (struct mlxd_softc *)devclass_get_softc(mlxd_devclass, *arg);
917 if ((mlxd != NULL) && (mlxd->mlxd_drive >= sc->mlx_sysdrive) &&
918 (mlxd->mlxd_drive < (sc->mlx_sysdrive + MLX_MAXDRIVES))) {
920 *arg = mlxd->mlxd_drive - sc->mlx_sysdrive;
929 /********************************************************************************
930 * Handle operations requested by a System Drive connected to this controller.
933 mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd,
934 caddr_t addr, int32_t flag, d_thread_t *td)
936 int *arg = (int *)addr;
941 * Return the current status of this drive.
944 *arg = drive->ms_state;
948 * Start a background consistency check on this drive.
950 case MLXD_CHECKASYNC: /* start a background consistency check */
951 if (sc->mlx_background != 0) {
955 result = mlx_check(sc, drive - &sc->mlx_sysdrive[0]);
961 error = ENOMEM; /* couldn't set up the command */
977 sc->mlx_background = MLX_BACKGROUND_CHECK;
986 /********************************************************************************
987 ********************************************************************************
989 ********************************************************************************
990 ********************************************************************************/
992 /********************************************************************************
993 * Fire off commands to periodically check the status of connected drives.
996 mlx_periodic(void *data)
998 struct mlx_softc *sc = (struct mlx_softc *)data;
1005 if ((sc->mlx_pause.mp_which != 0) &&
1006 (sc->mlx_pause.mp_when > 0) &&
1007 (time_second >= sc->mlx_pause.mp_when)){
1009 mlx_pause_action(sc); /* pause is running */
1010 sc->mlx_pause.mp_when = 0;
1014 * Bus pause still running?
1016 } else if ((sc->mlx_pause.mp_which != 0) &&
1017 (sc->mlx_pause.mp_when == 0)) {
1019 /* time to stop bus pause? */
1020 if (time_second >= sc->mlx_pause.mp_howlong) {
1021 mlx_pause_action(sc);
1022 sc->mlx_pause.mp_which = 0; /* pause is complete */
1025 sysbeep((time_second % 5) * 100 + 500, hz/8);
1029 * Run normal periodic activities?
1031 } else if (time_second > (sc->mlx_lastpoll + 10)) {
1032 sc->mlx_lastpoll = time_second;
1035 * Check controller status.
1037 * XXX Note that this may not actually launch a command in situations of high load.
1039 mlx_enquire(sc, (sc->mlx_iftype == MLX_IFTYPE_2) ? MLX_CMD_ENQUIRY_OLD : MLX_CMD_ENQUIRY,
1040 imax(sizeof(struct mlx_enquiry), sizeof(struct mlx_enquiry_old)), mlx_periodic_enquiry);
1043 * Check system drive status.
1045 * XXX This might be better left to event-driven detection, eg. I/O to an offline
1046 * drive will detect it's offline, rebuilds etc. should detect the drive is back
1049 mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(struct mlx_enq_sys_drive) * MLX_MAXDRIVES,
1050 mlx_periodic_enquiry);
1054 /* get drive rebuild/check status */
1055 /* XXX should check sc->mlx_background if this is only valid while in progress */
1056 mlx_enquire(sc, MLX_CMD_REBUILDSTAT, sizeof(struct mlx_rebuild_stat), mlx_periodic_rebuild);
1058 /* deal with possibly-missed interrupts and timed-out commands */
1061 /* reschedule another poll next second or so */
1062 sc->mlx_timeout = timeout(mlx_periodic, sc, hz);
1065 /********************************************************************************
1066 * Handle the result of an ENQUIRY command instigated by periodic status polling.
1069 mlx_periodic_enquiry(struct mlx_command *mc)
1071 struct mlx_softc *sc = mc->mc_sc;
1075 /* Command completed OK? */
1076 if (mc->mc_status != 0) {
1077 device_printf(sc->mlx_dev, "periodic enquiry failed - %s\n", mlx_diagnose_command(mc));
1081 /* respond to command */
1082 switch(mc->mc_mailbox[0]) {
1084 * This is currently a bit fruitless, as we don't know how to extract the eventlog
1087 case MLX_CMD_ENQUIRY_OLD:
1089 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data;
1090 struct mlx_enquiry_old *meo = (struct mlx_enquiry_old *)mc->mc_data;
1093 /* convert data in-place to new format */
1094 for (i = (sizeof(me->me_dead) / sizeof(me->me_dead[0])) - 1; i >= 0; i--) {
1095 me->me_dead[i].dd_chan = meo->me_dead[i].dd_chan;
1096 me->me_dead[i].dd_targ = meo->me_dead[i].dd_targ;
1098 me->me_misc_flags = 0;
1099 me->me_rebuild_count = meo->me_rebuild_count;
1100 me->me_dead_count = meo->me_dead_count;
1101 me->me_critical_sd_count = meo->me_critical_sd_count;
1102 me->me_event_log_seq_num = 0;
1103 me->me_offline_sd_count = meo->me_offline_sd_count;
1104 me->me_max_commands = meo->me_max_commands;
1105 me->me_rebuild_flag = meo->me_rebuild_flag;
1106 me->me_fwmajor = meo->me_fwmajor;
1107 me->me_fwminor = meo->me_fwminor;
1108 me->me_status_flags = meo->me_status_flags;
1109 me->me_flash_age = meo->me_flash_age;
1110 for (i = (sizeof(me->me_drvsize) / sizeof(me->me_drvsize[0])) - 1; i >= 0; i--) {
1111 if (i > ((sizeof(meo->me_drvsize) / sizeof(meo->me_drvsize[0])) - 1)) {
1112 me->me_drvsize[i] = 0; /* drive beyond supported range */
1114 me->me_drvsize[i] = meo->me_drvsize[i];
1117 me->me_num_sys_drvs = meo->me_num_sys_drvs;
1122 * Generic controller status update. We could do more with this than just
1123 * checking the event log.
1125 case MLX_CMD_ENQUIRY:
1127 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data;
1129 if (sc->mlx_currevent == -1) {
1130 /* initialise our view of the event log */
1131 sc->mlx_currevent = sc->mlx_lastevent = me->me_event_log_seq_num;
1132 } else if ((me->me_event_log_seq_num != sc->mlx_lastevent) && !(sc->mlx_flags & MLX_EVENTLOG_BUSY)) {
1133 /* record where current events are up to */
1134 sc->mlx_currevent = me->me_event_log_seq_num;
1135 debug(1, "event log pointer was %d, now %d\n", sc->mlx_lastevent, sc->mlx_currevent);
1137 /* mark the event log as busy */
1138 atomic_set_int(&sc->mlx_flags, MLX_EVENTLOG_BUSY);
1140 /* drain new eventlog entries */
1141 mlx_periodic_eventlog_poll(sc);
1145 case MLX_CMD_ENQSYSDRIVE:
1147 struct mlx_enq_sys_drive *mes = (struct mlx_enq_sys_drive *)mc->mc_data;
1148 struct mlx_sysdrive *dr;
1151 for (i = 0, dr = &sc->mlx_sysdrive[0];
1152 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
1155 /* has state been changed by controller? */
1156 if (dr->ms_state != mes[i].sd_state) {
1157 switch(mes[i].sd_state) {
1158 case MLX_SYSD_OFFLINE:
1159 device_printf(dr->ms_disk, "drive offline\n");
1161 case MLX_SYSD_ONLINE:
1162 device_printf(dr->ms_disk, "drive online\n");
1164 case MLX_SYSD_CRITICAL:
1165 device_printf(dr->ms_disk, "drive critical\n");
1168 /* save new state */
1169 dr->ms_state = mes[i].sd_state;
1175 device_printf(sc->mlx_dev, "%s: unknown command 0x%x", __FUNCTION__, mc->mc_mailbox[0]);
1180 free(mc->mc_data, M_DEVBUF);
1184 /********************************************************************************
1185 * Instigate a poll for one event log message on (sc).
1186 * We only poll for one message at a time, to keep our command usage down.
1189 mlx_periodic_eventlog_poll(struct mlx_softc *sc)
1191 struct mlx_command *mc;
1192 void *result = NULL;
1197 /* get ourselves a command buffer */
1199 if ((mc = mlx_alloccmd(sc)) == NULL)
1202 * allocate the response structure - sizeof(struct mlx_eventlog_entry)?
1203 * Called from timeout - use M_NOWAIT (repoll later on failure?)
1205 if ((result = malloc(1024, M_DEVBUF, M_NOWAIT)) == NULL)
1207 /* get a command slot */
1208 if (mlx_getslot(mc))
1211 /* map the command so the controller can see it */
1212 mc->mc_data = result;
1213 mc->mc_length = /*sizeof(struct mlx_eventlog_entry)*/1024;
1216 /* build the command to get one entry */
1217 mlx_make_type3(mc, MLX_CMD_LOGOP, MLX_LOGOP_GET, 1, sc->mlx_lastevent, 0, 0, mc->mc_dataphys, 0);
1218 mc->mc_complete = mlx_periodic_eventlog_respond;
1219 mc->mc_private = mc;
1221 /* start the command */
1222 if ((error = mlx_start(mc)) != 0)
1225 error = 0; /* success */
1231 free(result, M_DEVBUF);
1235 /********************************************************************************
1236 * Handle the result of polling for a log message, generate diagnostic output.
1237 * If this wasn't the last message waiting for us, we'll go collect another.
1239 static char *mlx_sense_messages[] = {
1240 "because write recovery failed",
1241 "because of SCSI bus reset failure",
1242 "because of double check condition",
1243 "because it was removed",
1244 "because of gross error on SCSI chip",
1245 "because of bad tag returned from drive",
1246 "because of timeout on SCSI command",
1247 "because of reset SCSI command issued from system",
1248 "because busy or parity error count exceeded limit",
1249 "because of 'kill drive' command from system",
1250 "because of selection timeout",
1251 "due to SCSI phase sequence error",
1252 "due to unknown status"
1256 mlx_periodic_eventlog_respond(struct mlx_command *mc)
1258 struct mlx_softc *sc = mc->mc_sc;
1259 struct mlx_eventlog_entry *el = (struct mlx_eventlog_entry *)mc->mc_data;
1264 sc->mlx_lastevent++; /* next message... */
1265 if (mc->mc_status == 0) {
1267 /* handle event log message */
1268 switch(el->el_type) {
1270 * This is the only sort of message we understand at the moment.
1271 * The tests here are probably incomplete.
1273 case MLX_LOGMSG_SENSE: /* sense data */
1274 /* Mylex vendor-specific message indicating a drive was killed? */
1275 if ((el->el_sensekey == 9) &&
1276 (el->el_asc == 0x80)) {
1277 if (el->el_asq < (sizeof(mlx_sense_messages) / sizeof(mlx_sense_messages[0]))) {
1278 reason = mlx_sense_messages[el->el_asq];
1280 reason = "for unknown reason";
1282 device_printf(sc->mlx_dev, "physical drive %d:%d killed %s\n",
1283 el->el_channel, el->el_target, reason);
1285 /* SCSI drive was reset? */
1286 if ((el->el_sensekey == 6) && (el->el_asc == 0x29)) {
1287 device_printf(sc->mlx_dev, "physical drive %d:%d reset\n",
1288 el->el_channel, el->el_target);
1290 /* SCSI drive error? */
1291 if (!((el->el_sensekey == 0) ||
1292 ((el->el_sensekey == 2) &&
1293 (el->el_asc == 0x04) &&
1294 ((el->el_asq == 0x01) ||
1295 (el->el_asq == 0x02))))) {
1296 device_printf(sc->mlx_dev, "physical drive %d:%d error log: sense = %d asc = %x asq = %x\n",
1297 el->el_channel, el->el_target, el->el_sensekey, el->el_asc, el->el_asq);
1298 device_printf(sc->mlx_dev, " info %4D csi %4D\n", el->el_information, ":", el->el_csi, ":");
1303 device_printf(sc->mlx_dev, "unknown log message type 0x%x\n", el->el_type);
1307 device_printf(sc->mlx_dev, "error reading message log - %s\n", mlx_diagnose_command(mc));
1308 /* give up on all the outstanding messages, as we may have come unsynched */
1309 sc->mlx_lastevent = sc->mlx_currevent;
1312 /* dispose of command and data */
1313 free(mc->mc_data, M_DEVBUF);
1316 /* is there another message to obtain? */
1317 if (sc->mlx_lastevent != sc->mlx_currevent) {
1318 mlx_periodic_eventlog_poll(sc);
1320 /* clear log-busy status */
1321 atomic_clear_int(&sc->mlx_flags, MLX_EVENTLOG_BUSY);
1325 /********************************************************************************
1326 * Handle check/rebuild operations in progress.
1329 mlx_periodic_rebuild(struct mlx_command *mc)
1331 struct mlx_softc *sc = mc->mc_sc;
1332 struct mlx_rebuild_status *mr = (struct mlx_rebuild_status *)mc->mc_data;
1334 switch(mc->mc_status) {
1335 case 0: /* operation running, update stats */
1336 sc->mlx_rebuildstat = *mr;
1338 /* spontaneous rebuild/check? */
1339 if (sc->mlx_background == 0) {
1340 sc->mlx_background = MLX_BACKGROUND_SPONTANEOUS;
1341 device_printf(sc->mlx_dev, "background check/rebuild operation started\n");
1345 case 0x0105: /* nothing running, finalise stats and report */
1346 switch(sc->mlx_background) {
1347 case MLX_BACKGROUND_CHECK:
1348 device_printf(sc->mlx_dev, "consistency check completed\n"); /* XXX print drive? */
1350 case MLX_BACKGROUND_REBUILD:
1351 device_printf(sc->mlx_dev, "drive rebuild completed\n"); /* XXX print channel/target? */
1353 case MLX_BACKGROUND_SPONTANEOUS:
1355 /* if we have previously been non-idle, report the transition */
1356 if (sc->mlx_rebuildstat.rs_code != MLX_REBUILDSTAT_IDLE) {
1357 device_printf(sc->mlx_dev, "background check/rebuild operation completed\n");
1360 sc->mlx_background = 0;
1361 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
1364 free(mc->mc_data, M_DEVBUF);
1368 /********************************************************************************
1369 ********************************************************************************
1371 ********************************************************************************
1372 ********************************************************************************/
1374 /********************************************************************************
1375 * It's time to perform a channel pause action for (sc), either start or stop
1379 mlx_pause_action(struct mlx_softc *sc)
1381 struct mlx_command *mc;
1382 int failsafe, i, command;
1384 /* What are we doing here? */
1385 if (sc->mlx_pause.mp_when == 0) {
1386 command = MLX_CMD_STARTCHANNEL;
1390 command = MLX_CMD_STOPCHANNEL;
1393 * Channels will always start again after the failsafe period,
1394 * which is specified in multiples of 30 seconds.
1395 * This constrains us to a maximum pause of 450 seconds.
1397 failsafe = ((sc->mlx_pause.mp_howlong - time_second) + 5) / 30;
1398 if (failsafe > 0xf) {
1400 sc->mlx_pause.mp_howlong = time_second + (0xf * 30) - 5;
1404 /* build commands for every channel requested */
1405 for (i = 0; i < sc->mlx_enq2->me_actual_channels; i++) {
1406 if ((1 << i) & sc->mlx_pause.mp_which) {
1408 /* get ourselves a command buffer */
1409 if ((mc = mlx_alloccmd(sc)) == NULL)
1411 /* get a command slot */
1412 mc->mc_flags |= MLX_CMD_PRIORITY;
1413 if (mlx_getslot(mc))
1416 /* build the command */
1417 mlx_make_type2(mc, command, (failsafe << 4) | i, 0, 0, 0, 0, 0, 0, 0);
1418 mc->mc_complete = mlx_pause_done;
1419 mc->mc_private = sc; /* XXX not needed */
1422 /* command submitted OK */
1426 device_printf(sc->mlx_dev, "%s failed for channel %d\n",
1427 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", i);
1435 mlx_pause_done(struct mlx_command *mc)
1437 struct mlx_softc *sc = mc->mc_sc;
1438 int command = mc->mc_mailbox[0];
1439 int channel = mc->mc_mailbox[2] & 0xf;
1441 if (mc->mc_status != 0) {
1442 device_printf(sc->mlx_dev, "%s command failed - %s\n",
1443 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", mlx_diagnose_command(mc));
1444 } else if (command == MLX_CMD_STOPCHANNEL) {
1445 device_printf(sc->mlx_dev, "channel %d pausing for %ld seconds\n",
1446 channel, (long)(sc->mlx_pause.mp_howlong - time_second));
1448 device_printf(sc->mlx_dev, "channel %d resuming\n", channel);
1453 /********************************************************************************
1454 ********************************************************************************
1456 ********************************************************************************
1457 ********************************************************************************/
1459 /********************************************************************************
1460 * Perform an Enquiry command using a type-3 command buffer and a return a single
1461 * linear result buffer. If the completion function is specified, it will
1462 * be called with the completed command (and the result response will not be
1463 * valid until that point). Otherwise, the command will either be busy-waited
1464 * for (interrupts not enabled), or slept for.
1467 mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize, void (* complete)(struct mlx_command *mc))
1469 struct mlx_command *mc;
1475 /* get ourselves a command buffer */
1478 if ((mc = mlx_alloccmd(sc)) == NULL)
1480 /* allocate the response structure */
1481 result = malloc(bufsize, M_DEVBUF, M_INTWAIT);
1482 /* get a command slot */
1483 mc->mc_flags |= MLX_CMD_PRIORITY | MLX_CMD_DATAOUT;
1484 if (mlx_getslot(mc))
1487 /* map the command so the controller can see it */
1488 mc->mc_data = result;
1489 mc->mc_length = bufsize;
1492 /* build an enquiry command */
1493 mlx_make_type2(mc, command, 0, 0, 0, 0, 0, 0, mc->mc_dataphys, 0);
1495 /* do we want a completion callback? */
1496 if (complete != NULL) {
1497 mc->mc_complete = complete;
1498 mc->mc_private = mc;
1499 if ((error = mlx_start(mc)) != 0)
1502 /* run the command in either polled or wait mode */
1503 if ((sc->mlx_state & MLX_STATE_INTEN) ? mlx_wait_command(mc) : mlx_poll_command(mc))
1506 /* command completed OK? */
1507 if (mc->mc_status != 0) {
1508 device_printf(sc->mlx_dev, "ENQUIRY failed - %s\n", mlx_diagnose_command(mc));
1512 error = 0; /* success */
1514 /* we got a command, but nobody else will free it */
1515 if ((complete == NULL) && (mc != NULL))
1517 /* we got an error, and we allocated a result */
1518 if ((error != 0) && (result != NULL)) {
1519 free(result, M_DEVBUF);
1526 /********************************************************************************
1527 * Perform a Flush command on the nominated controller.
1529 * May be called with interrupts enabled or disabled; will not return until
1530 * the flush operation completes or fails.
1533 mlx_flush(struct mlx_softc *sc)
1535 struct mlx_command *mc;
1540 /* get ourselves a command buffer */
1542 if ((mc = mlx_alloccmd(sc)) == NULL)
1544 /* get a command slot */
1545 if (mlx_getslot(mc))
1548 /* build a flush command */
1549 mlx_make_type2(mc, MLX_CMD_FLUSH, 0, 0, 0, 0, 0, 0, 0, 0);
1551 /* can't assume that interrupts are going to work here, so play it safe */
1552 if (mlx_poll_command(mc))
1555 /* command completed OK? */
1556 if (mc->mc_status != 0) {
1557 device_printf(sc->mlx_dev, "FLUSH failed - %s\n", mlx_diagnose_command(mc));
1561 error = 0; /* success */
1568 /********************************************************************************
1569 * Start a background consistency check on (drive).
1571 * May be called with interrupts enabled or disabled; will return as soon as the
1572 * operation has started or been refused.
1575 mlx_check(struct mlx_softc *sc, int drive)
1577 struct mlx_command *mc;
1582 /* get ourselves a command buffer */
1584 if ((mc = mlx_alloccmd(sc)) == NULL)
1586 /* get a command slot */
1587 if (mlx_getslot(mc))
1590 /* build a checkasync command, set the "fix it" flag */
1591 mlx_make_type2(mc, MLX_CMD_CHECKASYNC, 0, 0, 0, 0, 0, drive | 0x80, 0, 0);
1593 /* start the command and wait for it to be returned */
1594 if (mlx_wait_command(mc))
1597 /* command completed OK? */
1598 if (mc->mc_status != 0) {
1599 device_printf(sc->mlx_dev, "CHECK ASYNC failed - %s\n", mlx_diagnose_command(mc));
1601 device_printf(sc->mlx_sysdrive[drive].ms_disk, "consistency check started");
1603 error = mc->mc_status;
1611 /********************************************************************************
1612 * Start a background rebuild of the physical drive at (channel),(target).
1614 * May be called with interrupts enabled or disabled; will return as soon as the
1615 * operation has started or been refused.
1618 mlx_rebuild(struct mlx_softc *sc, int channel, int target)
1620 struct mlx_command *mc;
1625 /* get ourselves a command buffer */
1627 if ((mc = mlx_alloccmd(sc)) == NULL)
1629 /* get a command slot */
1630 if (mlx_getslot(mc))
1633 /* build a checkasync command, set the "fix it" flag */
1634 mlx_make_type2(mc, MLX_CMD_REBUILDASYNC, channel, target, 0, 0, 0, 0, 0, 0);
1636 /* start the command and wait for it to be returned */
1637 if (mlx_wait_command(mc))
1640 /* command completed OK? */
1641 if (mc->mc_status != 0) {
1642 device_printf(sc->mlx_dev, "REBUILD ASYNC failed - %s\n", mlx_diagnose_command(mc));
1644 device_printf(sc->mlx_dev, "drive rebuild started for %d:%d\n", channel, target);
1646 error = mc->mc_status;
1654 /********************************************************************************
1655 * Run the command (mc) and return when it completes.
1657 * Interrupts need to be enabled; returns nonzero on error.
1660 mlx_wait_command(struct mlx_command *mc)
1662 struct mlx_softc *sc = mc->mc_sc;
1667 mc->mc_complete = NULL;
1668 mc->mc_private = mc; /* wake us when you're done */
1669 if ((error = mlx_start(mc)) != 0)
1673 /* XXX better timeout? */
1674 while ((mc->mc_status == MLX_STATUS_BUSY) && (count < 30)) {
1675 tsleep(mc->mc_private, PCATCH, "mlxwcmd", hz);
1678 if (mc->mc_status != 0) {
1679 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1686 /********************************************************************************
1687 * Start the command (mc) and busy-wait for it to complete.
1689 * Should only be used when interrupts can't be relied upon. Returns 0 on
1690 * success, nonzero on error.
1691 * Successfully completed commands are dequeued.
1694 mlx_poll_command(struct mlx_command *mc)
1696 struct mlx_softc *sc = mc->mc_sc;
1697 int error, count, s;
1701 mc->mc_complete = NULL;
1702 mc->mc_private = NULL; /* we will poll for it */
1703 if ((error = mlx_start(mc)) != 0)
1708 /* poll for completion */
1709 mlx_done(mc->mc_sc);
1711 } while ((mc->mc_status == MLX_STATUS_BUSY) && (count++ < 15000000));
1712 if (mc->mc_status != MLX_STATUS_BUSY) {
1714 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
1718 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1722 /********************************************************************************
1723 * Pull as much work off the softc's work queue as possible and give it to the
1724 * controller. Leave a couple of slots free for emergencies.
1726 * Must be called at splbio or in an equivalent fashion that prevents
1727 * reentry or activity on the bioq.
1730 mlx_startio(struct mlx_softc *sc)
1732 struct mlx_command *mc;
1733 struct mlxd_softc *mlxd;
1740 /* avoid reentrancy */
1741 if (mlx_lock_tas(sc, MLX_LOCK_STARTING))
1744 /* spin until something prevents us from doing any work */
1748 /* see if there's work to be done */
1749 if ((bp = MLX_BIO_QFIRST(sc->mlx_bioq)) == NULL)
1752 if ((mc = mlx_alloccmd(sc)) == NULL)
1754 /* get a slot for the command */
1755 if (mlx_getslot(mc) != 0) {
1759 /* get the buf containing our work */
1760 MLX_BIO_QREMOVE(sc->mlx_bioq, bp);
1764 /* connect the buf to the command */
1765 mc->mc_complete = mlx_completeio;
1766 mc->mc_private = bp;
1767 mc->mc_data = MLX_BIO_DATA(bp);
1768 mc->mc_length = MLX_BIO_LENGTH(bp);
1769 if (MLX_BIO_IS_READ(bp)) {
1770 mc->mc_flags |= MLX_CMD_DATAIN;
1771 cmd = MLX_CMD_READSG;
1773 mc->mc_flags |= MLX_CMD_DATAOUT;
1774 cmd = MLX_CMD_WRITESG;
1777 /* map the command so the controller can work with it */
1780 /* build a suitable I/O command (assumes 512-byte rounded transfers) */
1781 mlxd = (struct mlxd_softc *)MLX_BIO_SOFTC(bp);
1782 driveno = mlxd->mlxd_drive - sc->mlx_sysdrive;
1783 blkcount = (MLX_BIO_LENGTH(bp) + MLX_BLKSIZE - 1) / MLX_BLKSIZE;
1785 if ((MLX_BIO_LBA(bp) + blkcount) > sc->mlx_sysdrive[driveno].ms_size)
1786 device_printf(sc->mlx_dev, "I/O beyond end of unit (%u,%d > %u)\n",
1787 MLX_BIO_LBA(bp), blkcount, sc->mlx_sysdrive[driveno].ms_size);
1790 * Build the I/O command. Note that the SG list type bits are set to zero,
1791 * denoting the format of SG list that we are using.
1793 if (sc->mlx_iftype == MLX_IFTYPE_2) {
1794 mlx_make_type1(mc, (cmd == MLX_CMD_WRITESG) ? MLX_CMD_WRITESG_OLD : MLX_CMD_READSG_OLD,
1795 blkcount & 0xff, /* xfer length low byte */
1796 MLX_BIO_LBA(bp), /* physical block number */
1797 driveno, /* target drive number */
1798 mc->mc_sgphys, /* location of SG list */
1799 mc->mc_nsgent & 0x3f); /* size of SG list (top 3 bits clear) */
1801 mlx_make_type5(mc, cmd,
1802 blkcount & 0xff, /* xfer length low byte */
1803 (driveno << 3) | ((blkcount >> 8) & 0x07), /* target and length high 3 bits */
1804 MLX_BIO_LBA(bp), /* physical block number */
1805 mc->mc_sgphys, /* location of SG list */
1806 mc->mc_nsgent & 0x3f); /* size of SG list (top 3 bits clear) */
1809 /* try to give command to controller */
1810 if (mlx_start(mc) != 0) {
1811 /* fail the command */
1812 mc->mc_status = MLX_STATUS_WEDGED;
1818 mlx_lock_clr(sc, MLX_LOCK_STARTING);
1821 /********************************************************************************
1822 * Handle completion of an I/O command.
1825 mlx_completeio(struct mlx_command *mc)
1827 struct mlx_softc *sc = mc->mc_sc;
1828 mlx_bio *bp = (mlx_bio *)mc->mc_private;
1829 struct mlxd_softc *mlxd = (struct mlxd_softc *)MLX_BIO_SOFTC(bp);
1831 if (mc->mc_status != MLX_STATUS_OK) { /* could be more verbose here? */
1832 MLX_BIO_SET_ERROR(bp, EIO);
1834 switch(mc->mc_status) {
1835 case MLX_STATUS_RDWROFFLINE: /* system drive has gone offline */
1836 device_printf(mlxd->mlxd_dev, "drive offline\n");
1837 /* should signal this with a return code */
1838 mlxd->mlxd_drive->ms_state = MLX_SYSD_OFFLINE;
1841 default: /* other I/O error */
1842 device_printf(sc->mlx_dev, "I/O error - %s\n", mlx_diagnose_command(mc));
1844 device_printf(sc->mlx_dev, " b_bcount %ld blkcount %ld b_pblkno %d\n",
1845 MLX_BIO_LENGTH(bp), MLX_BIO_LENGTH(bp) / MLX_BLKSIZE, MLX_BIO_LBA(bp));
1846 device_printf(sc->mlx_dev, " %13D\n", mc->mc_mailbox, " ");
1855 /********************************************************************************
1856 * Take a command from user-space and try to run it.
1858 * XXX Note that this can't perform very much in the way of error checking, and
1859 * as such, applications _must_ be considered trustworthy.
1860 * XXX Commands using S/G for data are not supported.
1863 mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu)
1865 struct mlx_command *mc;
1866 struct mlx_dcdb *dcdb;
1877 /* get ourselves a command and copy in from user space */
1878 if ((mc = mlx_alloccmd(sc)) == NULL)
1880 bcopy(mu->mu_command, mc->mc_mailbox, sizeof(mc->mc_mailbox));
1881 debug(0, "got command buffer");
1883 /* if we need a buffer for data transfer, allocate one and copy in its initial contents */
1884 if (mu->mu_datasize > 0) {
1885 if (mu->mu_datasize > MAXPHYS)
1887 if (((kbuf = malloc(mu->mu_datasize, M_DEVBUF, M_WAITOK)) == NULL) ||
1888 (error = copyin(mu->mu_buf, kbuf, mu->mu_datasize)))
1890 debug(0, "got kernel buffer");
1893 /* get a command slot */
1894 if (mlx_getslot(mc))
1896 debug(0, "got a slot");
1898 /* map the command so the controller can see it */
1900 mc->mc_length = mu->mu_datasize;
1905 * If this is a passthrough SCSI command, the DCDB is packed at the
1906 * beginning of the data area. Fix up the DCDB to point to the correct physical
1907 * address and override any bufptr supplied by the caller since we know
1908 * what it's meant to be.
1910 if (mc->mc_mailbox[0] == MLX_CMD_DIRECT_CDB) {
1911 dcdb = (struct mlx_dcdb *)kbuf;
1912 dcdb->dcdb_physaddr = mc->mc_dataphys + sizeof(*dcdb);
1917 * If there's a data buffer, fix up the command's buffer pointer.
1919 if (mu->mu_datasize > 0) {
1921 /* range check the pointer to physical buffer address */
1922 if ((mu->mu_bufptr < 0) || (mu->mu_bufptr > (sizeof(mu->mu_command) - sizeof(u_int32_t)))) {
1926 mc->mc_mailbox[mu->mu_bufptr ] = mc->mc_dataphys & 0xff;
1927 mc->mc_mailbox[mu->mu_bufptr + 1] = (mc->mc_dataphys >> 8) & 0xff;
1928 mc->mc_mailbox[mu->mu_bufptr + 2] = (mc->mc_dataphys >> 16) & 0xff;
1929 mc->mc_mailbox[mu->mu_bufptr + 3] = (mc->mc_dataphys >> 24) & 0xff;
1931 debug(0, "command fixup");
1933 /* submit the command and wait */
1934 if ((error = mlx_wait_command(mc)) != 0)
1937 /* copy out status and data */
1938 mu->mu_status = mc->mc_status;
1939 if ((mu->mu_datasize > 0) && ((error = copyout(kbuf, mu->mu_buf, mu->mu_datasize))))
1946 free(kbuf, M_DEVBUF);
1950 /********************************************************************************
1951 ********************************************************************************
1952 Command I/O to Controller
1953 ********************************************************************************
1954 ********************************************************************************/
1956 /********************************************************************************
1957 * Find a free command slot for (mc).
1959 * Don't hand out a slot to a normal-priority command unless there are at least
1960 * 4 slots free for priority commands.
1963 mlx_getslot(struct mlx_command *mc)
1965 struct mlx_softc *sc = mc->mc_sc;
1971 * Enforce slot-usage limit, if we have the required information.
1973 if (sc->mlx_enq2 != NULL) {
1974 limit = sc->mlx_enq2->me_max_commands;
1978 if (sc->mlx_busycmds >= ((mc->mc_flags & MLX_CMD_PRIORITY) ? limit : limit - 4))
1982 * Allocate an outstanding command slot
1984 * XXX linear search is slow
1987 for (slot = 0; slot < limit; slot++) {
1988 debug(2, "try slot %d", slot);
1989 if (sc->mlx_busycmd[slot] == NULL)
1993 sc->mlx_busycmd[slot] = mc;
2002 debug(2, "got slot %d", slot);
2007 /********************************************************************************
2008 * Map/unmap (mc)'s data in the controller's addressable space.
2011 mlx_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
2013 struct mlx_command *mc = (struct mlx_command *)arg;
2014 struct mlx_softc *sc = mc->mc_sc;
2015 struct mlx_sgentry *sg;
2020 /* XXX should be unnecessary */
2021 if (sc->mlx_enq2 && (nsegments > sc->mlx_enq2->me_max_sg))
2022 panic("MLX: too many s/g segments (%d, max %d)", nsegments, sc->mlx_enq2->me_max_sg);
2024 /* get base address of s/g table */
2025 sg = sc->mlx_sgtable + (mc->mc_slot * MLX_NSEG);
2027 /* save s/g table information in command */
2028 mc->mc_nsgent = nsegments;
2029 mc->mc_sgphys = sc->mlx_sgbusaddr + (mc->mc_slot * MLX_NSEG * sizeof(struct mlx_sgentry));
2030 mc->mc_dataphys = segs[0].ds_addr;
2032 /* populate s/g table */
2033 for (i = 0; i < nsegments; i++, sg++) {
2034 sg->sg_addr = segs[i].ds_addr;
2035 sg->sg_count = segs[i].ds_len;
2040 mlx_mapcmd(struct mlx_command *mc)
2042 struct mlx_softc *sc = mc->mc_sc;
2046 /* if the command involves data at all */
2047 if (mc->mc_data != NULL) {
2049 /* map the data buffer into bus space and build the s/g list */
2050 bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data, mc->mc_length,
2051 mlx_setup_dmamap, mc, 0);
2052 if (mc->mc_flags & MLX_CMD_DATAIN)
2053 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_PREREAD);
2054 if (mc->mc_flags & MLX_CMD_DATAOUT)
2055 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_PREWRITE);
2060 mlx_unmapcmd(struct mlx_command *mc)
2062 struct mlx_softc *sc = mc->mc_sc;
2066 /* if the command involved data at all */
2067 if (mc->mc_data != NULL) {
2069 if (mc->mc_flags & MLX_CMD_DATAIN)
2070 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTREAD);
2071 if (mc->mc_flags & MLX_CMD_DATAOUT)
2072 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTWRITE);
2074 bus_dmamap_unload(sc->mlx_buffer_dmat, mc->mc_dmamap);
2078 /********************************************************************************
2079 * Try to deliver (mc) to the controller.
2081 * Can be called at any interrupt level, with or without interrupts enabled.
2084 mlx_start(struct mlx_command *mc)
2086 struct mlx_softc *sc = mc->mc_sc;
2091 /* save the slot number as ident so we can handle this command when complete */
2092 mc->mc_mailbox[0x1] = mc->mc_slot;
2094 /* mark the command as currently being processed */
2095 mc->mc_status = MLX_STATUS_BUSY;
2097 /* set a default 60-second timeout XXX tunable? XXX not currently used */
2098 mc->mc_timeout = time_second + 60;
2100 /* spin waiting for the mailbox */
2101 for (i = 100000, done = 0; (i > 0) && !done; i--) {
2103 if (sc->mlx_tryqueue(sc, mc)) {
2105 /* move command to work queue */
2106 TAILQ_INSERT_TAIL(&sc->mlx_work, mc, mc_link);
2108 splx(s); /* drop spl to allow completion interrupts */
2111 /* command is enqueued */
2116 * We couldn't get the controller to take the command. Revoke the slot
2117 * that the command was given and return it with a bad status.
2119 sc->mlx_busycmd[mc->mc_slot] = NULL;
2120 device_printf(sc->mlx_dev, "controller wedged (not taking commands)\n");
2121 mc->mc_status = MLX_STATUS_WEDGED;
2126 /********************************************************************************
2127 * Poll the controller (sc) for completed commands.
2128 * Update command status and free slots for reuse. If any slots were freed,
2129 * new commands may be posted.
2131 * Returns nonzero if one or more commands were completed.
2134 mlx_done(struct mlx_softc *sc)
2136 struct mlx_command *mc;
2145 /* loop collecting completed commands */
2148 /* poll for a completed command's identifier and status */
2149 if (sc->mlx_findcomplete(sc, &slot, &status)) {
2151 mc = sc->mlx_busycmd[slot]; /* find command */
2152 if (mc != NULL) { /* paranoia */
2153 if (mc->mc_status == MLX_STATUS_BUSY) {
2154 mc->mc_status = status; /* save status */
2156 /* free slot for reuse */
2157 sc->mlx_busycmd[slot] = NULL;
2160 device_printf(sc->mlx_dev, "duplicate done event for slot %d\n", slot);
2163 device_printf(sc->mlx_dev, "done event for nonbusy slot %d\n", slot);
2171 /* if we've completed any commands, try posting some more */
2175 /* handle completion and timeouts */
2181 /********************************************************************************
2182 * Perform post-completion processing for commands on (sc).
2185 mlx_complete(struct mlx_softc *sc)
2187 struct mlx_command *mc, *nc;
2192 /* avoid reentrancy XXX might want to signal and request a restart */
2193 if (mlx_lock_tas(sc, MLX_LOCK_COMPLETING))
2199 /* scan the list of busy/done commands */
2200 mc = TAILQ_FIRST(&sc->mlx_work);
2201 while (mc != NULL) {
2202 nc = TAILQ_NEXT(mc, mc_link);
2204 /* Command has been completed in some fashion */
2205 if (mc->mc_status != MLX_STATUS_BUSY) {
2207 /* unmap the command's data buffer */
2210 * Does the command have a completion handler?
2212 if (mc->mc_complete != NULL) {
2213 /* remove from list and give to handler */
2214 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2215 mc->mc_complete(mc);
2218 * Is there a sleeper waiting on this command?
2220 } else if (mc->mc_private != NULL) { /* sleeping caller wants to know about it */
2222 /* remove from list and wake up sleeper */
2223 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2224 wakeup_one(mc->mc_private);
2227 * Leave the command for a caller that's polling for it.
2236 mlx_lock_clr(sc, MLX_LOCK_COMPLETING);
2239 /********************************************************************************
2240 ********************************************************************************
2241 Command Buffer Management
2242 ********************************************************************************
2243 ********************************************************************************/
2245 /********************************************************************************
2246 * Get a new command buffer.
2248 * This may return NULL in low-memory cases.
2250 * Note that using malloc() is expensive (the command buffer is << 1 page) but
2251 * necessary if we are to be a loadable module before the zone allocator is fixed.
2253 * If possible, we recycle a command buffer that's been used before.
2255 * XXX Note that command buffers are not cleaned out - it is the caller's
2256 * responsibility to ensure that all required fields are filled in before
2259 static struct mlx_command *
2260 mlx_alloccmd(struct mlx_softc *sc)
2262 struct mlx_command *mc;
2269 if ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL)
2270 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
2273 /* allocate a new command buffer? */
2275 mc = malloc(sizeof(*mc), M_DEVBUF, M_INTWAIT | M_ZERO);
2277 error = bus_dmamap_create(sc->mlx_buffer_dmat, 0, &mc->mc_dmamap);
2286 /********************************************************************************
2287 * Release a command buffer for recycling.
2289 * XXX It might be a good idea to limit the number of commands we save for reuse
2290 * if it's shown that this list bloats out massively.
2293 mlx_releasecmd(struct mlx_command *mc)
2300 TAILQ_INSERT_HEAD(&mc->mc_sc->mlx_freecmds, mc, mc_link);
2304 /********************************************************************************
2305 * Permanently discard a command buffer.
2308 mlx_freecmd(struct mlx_command *mc)
2310 struct mlx_softc *sc = mc->mc_sc;
2313 bus_dmamap_destroy(sc->mlx_buffer_dmat, mc->mc_dmamap);
2318 /********************************************************************************
2319 ********************************************************************************
2320 Type 3 interface accessor methods
2321 ********************************************************************************
2322 ********************************************************************************/
2324 /********************************************************************************
2325 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2326 * (the controller is not ready to take a command).
2328 * Must be called at splbio or in a fashion that prevents reentry.
2331 mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2337 /* ready for our command? */
2338 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_FULL)) {
2339 /* copy mailbox data to window */
2340 for (i = 0; i < 13; i++)
2341 MLX_V3_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2344 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_FULL);
2350 /********************************************************************************
2351 * See if a command has been completed, if so acknowledge its completion
2352 * and recover the slot number and status code.
2354 * Must be called at splbio or in a fashion that prevents reentry.
2357 mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2362 /* status available? */
2363 if (MLX_V3_GET_ODBR(sc) & MLX_V3_ODB_SAVAIL) {
2364 *slot = MLX_V3_GET_STATUS_IDENT(sc); /* get command identifier */
2365 *status = MLX_V3_GET_STATUS(sc); /* get status */
2367 /* acknowledge completion */
2368 MLX_V3_PUT_ODBR(sc, MLX_V3_ODB_SAVAIL);
2369 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2375 /********************************************************************************
2376 * Enable/disable interrupts as requested. (No acknowledge required)
2378 * Must be called at splbio or in a fashion that prevents reentry.
2381 mlx_v3_intaction(struct mlx_softc *sc, int action)
2386 case MLX_INTACTION_DISABLE:
2387 MLX_V3_PUT_IER(sc, 0);
2388 sc->mlx_state &= ~MLX_STATE_INTEN;
2390 case MLX_INTACTION_ENABLE:
2391 MLX_V3_PUT_IER(sc, 1);
2392 sc->mlx_state |= MLX_STATE_INTEN;
2397 /********************************************************************************
2398 * Poll for firmware error codes during controller initialisation.
2399 * Returns 0 if initialisation is complete, 1 if still in progress but no
2400 * error has been fetched, 2 if an error has been retrieved.
2403 mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2)
2406 static int initted = 0;
2410 /* first time around, clear any hardware completion status */
2412 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2417 /* init in progress? */
2418 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_INIT_BUSY))
2421 /* test error value */
2422 fwerror = MLX_V3_GET_FWERROR(sc);
2423 if (!(fwerror & MLX_V3_FWERROR_PEND))
2426 /* mask status pending bit, fetch status */
2427 *error = fwerror & ~MLX_V3_FWERROR_PEND;
2428 *param1 = MLX_V3_GET_FWERROR_PARAM1(sc);
2429 *param2 = MLX_V3_GET_FWERROR_PARAM2(sc);
2432 MLX_V3_PUT_FWERROR(sc, 0);
2437 /********************************************************************************
2438 ********************************************************************************
2439 Type 4 interface accessor methods
2440 ********************************************************************************
2441 ********************************************************************************/
2443 /********************************************************************************
2444 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2445 * (the controller is not ready to take a command).
2447 * Must be called at splbio or in a fashion that prevents reentry.
2450 mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2456 /* ready for our command? */
2457 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_FULL)) {
2458 /* copy mailbox data to window */
2459 for (i = 0; i < 13; i++)
2460 MLX_V4_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2462 /* memory-mapped controller, so issue a write barrier to ensure the mailbox is filled */
2463 bus_space_barrier(sc->mlx_btag, sc->mlx_bhandle, MLX_V4_MAILBOX, MLX_V4_MAILBOX_LENGTH,
2464 BUS_SPACE_BARRIER_WRITE);
2467 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_HWMBOX_CMD);
2473 /********************************************************************************
2474 * See if a command has been completed, if so acknowledge its completion
2475 * and recover the slot number and status code.
2477 * Must be called at splbio or in a fashion that prevents reentry.
2480 mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2485 /* status available? */
2486 if (MLX_V4_GET_ODBR(sc) & MLX_V4_ODB_HWSAVAIL) {
2487 *slot = MLX_V4_GET_STATUS_IDENT(sc); /* get command identifier */
2488 *status = MLX_V4_GET_STATUS(sc); /* get status */
2490 /* acknowledge completion */
2491 MLX_V4_PUT_ODBR(sc, MLX_V4_ODB_HWMBOX_ACK);
2492 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2498 /********************************************************************************
2499 * Enable/disable interrupts as requested.
2501 * Must be called at splbio or in a fashion that prevents reentry.
2504 mlx_v4_intaction(struct mlx_softc *sc, int action)
2509 case MLX_INTACTION_DISABLE:
2510 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK | MLX_V4_IER_DISINT);
2511 sc->mlx_state &= ~MLX_STATE_INTEN;
2513 case MLX_INTACTION_ENABLE:
2514 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK & ~MLX_V4_IER_DISINT);
2515 sc->mlx_state |= MLX_STATE_INTEN;
2520 /********************************************************************************
2521 * Poll for firmware error codes during controller initialisation.
2522 * Returns 0 if initialisation is complete, 1 if still in progress but no
2523 * error has been fetched, 2 if an error has been retrieved.
2526 mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2)
2529 static int initted = 0;
2533 /* first time around, clear any hardware completion status */
2535 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2540 /* init in progress? */
2541 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_INIT_BUSY))
2544 /* test error value */
2545 fwerror = MLX_V4_GET_FWERROR(sc);
2546 if (!(fwerror & MLX_V4_FWERROR_PEND))
2549 /* mask status pending bit, fetch status */
2550 *error = fwerror & ~MLX_V4_FWERROR_PEND;
2551 *param1 = MLX_V4_GET_FWERROR_PARAM1(sc);
2552 *param2 = MLX_V4_GET_FWERROR_PARAM2(sc);
2555 MLX_V4_PUT_FWERROR(sc, 0);
2560 /********************************************************************************
2561 ********************************************************************************
2562 Type 5 interface accessor methods
2563 ********************************************************************************
2564 ********************************************************************************/
2566 /********************************************************************************
2567 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2568 * (the controller is not ready to take a command).
2570 * Must be called at splbio or in a fashion that prevents reentry.
2573 mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2579 /* ready for our command? */
2580 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_EMPTY) {
2581 /* copy mailbox data to window */
2582 for (i = 0; i < 13; i++)
2583 MLX_V5_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2586 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_HWMBOX_CMD);
2592 /********************************************************************************
2593 * See if a command has been completed, if so acknowledge its completion
2594 * and recover the slot number and status code.
2596 * Must be called at splbio or in a fashion that prevents reentry.
2599 mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2604 /* status available? */
2605 if (MLX_V5_GET_ODBR(sc) & MLX_V5_ODB_HWSAVAIL) {
2606 *slot = MLX_V5_GET_STATUS_IDENT(sc); /* get command identifier */
2607 *status = MLX_V5_GET_STATUS(sc); /* get status */
2609 /* acknowledge completion */
2610 MLX_V5_PUT_ODBR(sc, MLX_V5_ODB_HWMBOX_ACK);
2611 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2617 /********************************************************************************
2618 * Enable/disable interrupts as requested.
2620 * Must be called at splbio or in a fashion that prevents reentry.
2623 mlx_v5_intaction(struct mlx_softc *sc, int action)
2628 case MLX_INTACTION_DISABLE:
2629 MLX_V5_PUT_IER(sc, 0xff & MLX_V5_IER_DISINT);
2630 sc->mlx_state &= ~MLX_STATE_INTEN;
2632 case MLX_INTACTION_ENABLE:
2633 MLX_V5_PUT_IER(sc, 0xff & ~MLX_V5_IER_DISINT);
2634 sc->mlx_state |= MLX_STATE_INTEN;
2639 /********************************************************************************
2640 * Poll for firmware error codes during controller initialisation.
2641 * Returns 0 if initialisation is complete, 1 if still in progress but no
2642 * error has been fetched, 2 if an error has been retrieved.
2645 mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2)
2648 static int initted = 0;
2652 /* first time around, clear any hardware completion status */
2654 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2659 /* init in progress? */
2660 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_INIT_DONE)
2663 /* test for error value */
2664 fwerror = MLX_V5_GET_FWERROR(sc);
2665 if (!(fwerror & MLX_V5_FWERROR_PEND))
2668 /* mask status pending bit, fetch status */
2669 *error = fwerror & ~MLX_V5_FWERROR_PEND;
2670 *param1 = MLX_V5_GET_FWERROR_PARAM1(sc);
2671 *param2 = MLX_V5_GET_FWERROR_PARAM2(sc);
2674 MLX_V5_PUT_FWERROR(sc, 0xff);
2679 /********************************************************************************
2680 ********************************************************************************
2682 ********************************************************************************
2683 ********************************************************************************/
2685 /********************************************************************************
2686 * Return a status message describing (mc)
2688 static char *mlx_status_messages[] = {
2689 "normal completion", /* 00 */
2690 "irrecoverable data error", /* 01 */
2691 "drive does not exist, or is offline", /* 02 */
2692 "attempt to write beyond end of drive", /* 03 */
2693 "bad data encountered", /* 04 */
2694 "invalid log entry request", /* 05 */
2695 "attempt to rebuild online drive", /* 06 */
2696 "new disk failed during rebuild", /* 07 */
2697 "invalid channel/target", /* 08 */
2698 "rebuild/check already in progress", /* 09 */
2699 "one or more disks are dead", /* 10 */
2700 "invalid or non-redundant drive", /* 11 */
2701 "channel is busy", /* 12 */
2702 "channel is not stopped", /* 13 */
2703 "rebuild successfully terminated", /* 14 */
2704 "unsupported command", /* 15 */
2705 "check condition received", /* 16 */
2706 "device is busy", /* 17 */
2707 "selection or command timeout", /* 18 */
2708 "command terminated abnormally", /* 19 */
2717 } mlx_messages[] = {
2718 {MLX_CMD_READSG, 0x0001, 1},
2719 {MLX_CMD_READSG, 0x0002, 1},
2720 {MLX_CMD_READSG, 0x0105, 3},
2721 {MLX_CMD_READSG, 0x010c, 4},
2722 {MLX_CMD_WRITESG, 0x0001, 1},
2723 {MLX_CMD_WRITESG, 0x0002, 1},
2724 {MLX_CMD_WRITESG, 0x0105, 3},
2725 {MLX_CMD_READSG_OLD, 0x0001, 1},
2726 {MLX_CMD_READSG_OLD, 0x0002, 1},
2727 {MLX_CMD_READSG_OLD, 0x0105, 3},
2728 {MLX_CMD_WRITESG_OLD, 0x0001, 1},
2729 {MLX_CMD_WRITESG_OLD, 0x0002, 1},
2730 {MLX_CMD_WRITESG_OLD, 0x0105, 3},
2731 {MLX_CMD_LOGOP, 0x0105, 5},
2732 {MLX_CMD_REBUILDASYNC, 0x0002, 6},
2733 {MLX_CMD_REBUILDASYNC, 0x0004, 7},
2734 {MLX_CMD_REBUILDASYNC, 0x0105, 8},
2735 {MLX_CMD_REBUILDASYNC, 0x0106, 9},
2736 {MLX_CMD_REBUILDASYNC, 0x0107, 14},
2737 {MLX_CMD_CHECKASYNC, 0x0002, 10},
2738 {MLX_CMD_CHECKASYNC, 0x0105, 11},
2739 {MLX_CMD_CHECKASYNC, 0x0106, 9},
2740 {MLX_CMD_STOPCHANNEL, 0x0106, 12},
2741 {MLX_CMD_STOPCHANNEL, 0x0105, 8},
2742 {MLX_CMD_STARTCHANNEL, 0x0005, 13},
2743 {MLX_CMD_STARTCHANNEL, 0x0105, 8},
2744 {MLX_CMD_DIRECT_CDB, 0x0002, 16},
2745 {MLX_CMD_DIRECT_CDB, 0x0008, 17},
2746 {MLX_CMD_DIRECT_CDB, 0x000e, 18},
2747 {MLX_CMD_DIRECT_CDB, 0x000f, 19},
2748 {MLX_CMD_DIRECT_CDB, 0x0105, 8},
2755 mlx_diagnose_command(struct mlx_command *mc)
2757 static char unkmsg[80];
2760 /* look up message in table */
2761 for (i = 0; mlx_messages[i].command != -1; i++)
2762 if (((mc->mc_mailbox[0] == mlx_messages[i].command) || (mlx_messages[i].command == 0)) &&
2763 (mc->mc_status == mlx_messages[i].status))
2764 return(mlx_status_messages[mlx_messages[i].msg]);
2766 sprintf(unkmsg, "unknown response 0x%x for command 0x%x", (int)mc->mc_status, (int)mc->mc_mailbox[0]);
2770 /*******************************************************************************
2771 * Print a string describing the controller (sc)
2777 } mlx_controller_names[] = {
2792 mlx_describe_controller(struct mlx_softc *sc)
2794 static char buf[80];
2798 for (i = 0, model = NULL; mlx_controller_names[i].name != NULL; i++) {
2799 if ((sc->mlx_enq2->me_hardware_id & 0xff) == mlx_controller_names[i].hwid) {
2800 model = mlx_controller_names[i].name;
2804 if (model == NULL) {
2805 sprintf(buf, " model 0x%x", sc->mlx_enq2->me_hardware_id & 0xff);
2808 device_printf(sc->mlx_dev, "DAC%s, %d channel%s, firmware %d.%02d-%c-%02d, %dMB RAM\n",
2810 sc->mlx_enq2->me_actual_channels,
2811 sc->mlx_enq2->me_actual_channels > 1 ? "s" : "",
2812 sc->mlx_enq2->me_firmware_id & 0xff,
2813 (sc->mlx_enq2->me_firmware_id >> 8) & 0xff,
2814 (sc->mlx_enq2->me_firmware_id >> 24) & 0xff,
2815 (sc->mlx_enq2->me_firmware_id >> 16) & 0xff,
2816 sc->mlx_enq2->me_mem_size / (1024 * 1024));
2819 device_printf(sc->mlx_dev, " Hardware ID 0x%08x\n", sc->mlx_enq2->me_hardware_id);
2820 device_printf(sc->mlx_dev, " Firmware ID 0x%08x\n", sc->mlx_enq2->me_firmware_id);
2821 device_printf(sc->mlx_dev, " Configured/Actual channels %d/%d\n", sc->mlx_enq2->me_configured_channels,
2822 sc->mlx_enq2->me_actual_channels);
2823 device_printf(sc->mlx_dev, " Max Targets %d\n", sc->mlx_enq2->me_max_targets);
2824 device_printf(sc->mlx_dev, " Max Tags %d\n", sc->mlx_enq2->me_max_tags);
2825 device_printf(sc->mlx_dev, " Max System Drives %d\n", sc->mlx_enq2->me_max_sys_drives);
2826 device_printf(sc->mlx_dev, " Max Arms %d\n", sc->mlx_enq2->me_max_arms);
2827 device_printf(sc->mlx_dev, " Max Spans %d\n", sc->mlx_enq2->me_max_spans);
2828 device_printf(sc->mlx_dev, " DRAM/cache/flash/NVRAM size %d/%d/%d/%d\n", sc->mlx_enq2->me_mem_size,
2829 sc->mlx_enq2->me_cache_size, sc->mlx_enq2->me_flash_size, sc->mlx_enq2->me_nvram_size);
2830 device_printf(sc->mlx_dev, " DRAM type %d\n", sc->mlx_enq2->me_mem_type);
2831 device_printf(sc->mlx_dev, " Clock Speed %dns\n", sc->mlx_enq2->me_clock_speed);
2832 device_printf(sc->mlx_dev, " Hardware Speed %dns\n", sc->mlx_enq2->me_hardware_speed);
2833 device_printf(sc->mlx_dev, " Max Commands %d\n", sc->mlx_enq2->me_max_commands);
2834 device_printf(sc->mlx_dev, " Max SG Entries %d\n", sc->mlx_enq2->me_max_sg);
2835 device_printf(sc->mlx_dev, " Max DP %d\n", sc->mlx_enq2->me_max_dp);
2836 device_printf(sc->mlx_dev, " Max IOD %d\n", sc->mlx_enq2->me_max_iod);
2837 device_printf(sc->mlx_dev, " Max Comb %d\n", sc->mlx_enq2->me_max_comb);
2838 device_printf(sc->mlx_dev, " Latency %ds\n", sc->mlx_enq2->me_latency);
2839 device_printf(sc->mlx_dev, " SCSI Timeout %ds\n", sc->mlx_enq2->me_scsi_timeout);
2840 device_printf(sc->mlx_dev, " Min Free Lines %d\n", sc->mlx_enq2->me_min_freelines);
2841 device_printf(sc->mlx_dev, " Rate Constant %d\n", sc->mlx_enq2->me_rate_const);
2842 device_printf(sc->mlx_dev, " MAXBLK %d\n", sc->mlx_enq2->me_maxblk);
2843 device_printf(sc->mlx_dev, " Blocking Factor %d sectors\n", sc->mlx_enq2->me_blocking_factor);
2844 device_printf(sc->mlx_dev, " Cache Line Size %d blocks\n", sc->mlx_enq2->me_cacheline);
2845 device_printf(sc->mlx_dev, " SCSI Capability %s%dMHz, %d bit\n",
2846 sc->mlx_enq2->me_scsi_cap & (1<<4) ? "differential " : "",
2847 (1 << ((sc->mlx_enq2->me_scsi_cap >> 2) & 3)) * 10,
2848 8 << (sc->mlx_enq2->me_scsi_cap & 0x3));
2849 device_printf(sc->mlx_dev, " Firmware Build Number %d\n", sc->mlx_enq2->me_firmware_build);
2850 device_printf(sc->mlx_dev, " Fault Management Type %d\n", sc->mlx_enq2->me_fault_mgmt_type);
2851 device_printf(sc->mlx_dev, " Features %b\n", sc->mlx_enq2->me_firmware_features,
2852 "\20\4Background Init\3Read Ahead\2MORE\1Cluster\n");
2857 /*******************************************************************************
2858 * Emit a string describing the firmware handshake status code, and return a flag
2859 * indicating whether the code represents a fatal error.
2861 * Error code interpretations are from the Linux driver, and don't directly match
2862 * the messages printed by Mylex's BIOS. This may change if documentation on the
2863 * codes is forthcoming.
2866 mlx_fw_message(struct mlx_softc *sc, int error, int param1, int param2)
2870 device_printf(sc->mlx_dev, "physical drive %d:%d not responding\n", param2, param1);
2873 /* we could be neater about this and give some indication when we receive more of them */
2874 if (!(sc->mlx_flags & MLX_SPINUP_REPORTED)) {
2875 device_printf(sc->mlx_dev, "spinning up drives...\n");
2876 sc->mlx_flags |= MLX_SPINUP_REPORTED;
2880 device_printf(sc->mlx_dev, "configuration checksum error\n");
2883 device_printf(sc->mlx_dev, "mirror race recovery failed\n");
2886 device_printf(sc->mlx_dev, "mirror race recovery in progress\n");
2889 device_printf(sc->mlx_dev, "physical drive %d:%d COD mismatch\n", param2, param1);
2892 device_printf(sc->mlx_dev, "logical drive installation aborted\n");
2895 device_printf(sc->mlx_dev, "mirror race on a critical system drive\n");
2898 device_printf(sc->mlx_dev, "new controller configuration found\n");
2901 device_printf(sc->mlx_dev, "FATAL MEMORY PARITY ERROR\n");
2904 device_printf(sc->mlx_dev, "unknown firmware initialisation error %02x:%02x:%02x\n", error, param1, param2);
2910 /********************************************************************************
2911 ********************************************************************************
2913 ********************************************************************************
2914 ********************************************************************************/
2916 /********************************************************************************
2917 * Find the disk whose unit number is (unit) on this controller
2919 static struct mlx_sysdrive *
2920 mlx_findunit(struct mlx_softc *sc, int unit)
2924 /* search system drives */
2925 for (i = 0; i < MLX_MAXDRIVES; i++) {
2926 /* is this one attached? */
2927 if (sc->mlx_sysdrive[i].ms_disk != 0) {
2928 /* is this the one? */
2929 if (unit == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
2930 return(&sc->mlx_sysdrive[i]);