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.25 2008/06/10 17:20:52 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>
40 #include <sys/devicestat.h>
44 #include <sys/thread2.h>
46 #include <machine/clock.h>
48 #include "mlx_compat.h"
53 #define MLX_CDEV_MAJOR 130
55 static struct dev_ops mlx_ops = {
56 { "mlx", MLX_CDEV_MAJOR, 0 },
62 devclass_t mlx_devclass;
65 * Per-interface accessor methods
67 static int mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
68 static int mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
69 static void mlx_v3_intaction(struct mlx_softc *sc, int action);
70 static int mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2);
72 static int mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
73 static int mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
74 static void mlx_v4_intaction(struct mlx_softc *sc, int action);
75 static int mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2);
77 static int mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
78 static int mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
79 static void mlx_v5_intaction(struct mlx_softc *sc, int action);
80 static int mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2);
85 static void mlx_periodic(void *data);
86 static void mlx_periodic_enquiry(struct mlx_command *mc);
87 static void mlx_periodic_eventlog_poll(struct mlx_softc *sc);
88 static void mlx_periodic_eventlog_respond(struct mlx_command *mc);
89 static void mlx_periodic_rebuild(struct mlx_command *mc);
94 static void mlx_pause_action(struct mlx_softc *sc);
95 static void mlx_pause_done(struct mlx_command *mc);
100 static void *mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize,
101 void (*complete)(struct mlx_command *mc));
102 static int mlx_flush(struct mlx_softc *sc);
103 static int mlx_check(struct mlx_softc *sc, int drive);
104 static int mlx_rebuild(struct mlx_softc *sc, int channel, int target);
105 static int mlx_wait_command(struct mlx_command *mc);
106 static int mlx_poll_command(struct mlx_command *mc);
107 static void mlx_startio(struct mlx_softc *sc);
108 static void mlx_completeio(struct mlx_command *mc);
109 static int mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu);
112 * Command buffer allocation.
114 static struct mlx_command *mlx_alloccmd(struct mlx_softc *sc);
115 static void mlx_releasecmd(struct mlx_command *mc);
116 static void mlx_freecmd(struct mlx_command *mc);
119 * Command management.
121 static int mlx_getslot(struct mlx_command *mc);
122 static void mlx_mapcmd(struct mlx_command *mc);
123 static void mlx_unmapcmd(struct mlx_command *mc);
124 static int mlx_start(struct mlx_command *mc);
125 static int mlx_done(struct mlx_softc *sc);
126 static void mlx_complete(struct mlx_softc *sc);
131 static char *mlx_diagnose_command(struct mlx_command *mc);
132 static void mlx_describe_controller(struct mlx_softc *sc);
133 static int mlx_fw_message(struct mlx_softc *sc, int status, int param1, int param2);
138 static struct mlx_sysdrive *mlx_findunit(struct mlx_softc *sc, int unit);
140 /********************************************************************************
141 ********************************************************************************
143 ********************************************************************************
144 ********************************************************************************/
146 /********************************************************************************
147 * Free all of the resources associated with (sc)
149 * Should not be called if the controller is active.
152 mlx_free(struct mlx_softc *sc)
154 struct mlx_command *mc;
158 /* cancel status timeout */
159 callout_stop(&sc->mlx_timeout);
161 /* throw away any command buffers */
162 while ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL) {
163 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
167 /* destroy data-transfer DMA tag */
168 if (sc->mlx_buffer_dmat)
169 bus_dma_tag_destroy(sc->mlx_buffer_dmat);
171 /* free and destroy DMA memory and tag for s/g lists */
173 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
175 bus_dma_tag_destroy(sc->mlx_sg_dmat);
177 /* disconnect the interrupt handler */
179 bus_teardown_intr(sc->mlx_dev, sc->mlx_irq, sc->mlx_intr);
180 if (sc->mlx_irq != NULL)
181 bus_release_resource(sc->mlx_dev, SYS_RES_IRQ, 0, sc->mlx_irq);
183 /* destroy the parent DMA tag */
184 if (sc->mlx_parent_dmat)
185 bus_dma_tag_destroy(sc->mlx_parent_dmat);
187 /* release the register window mapping */
188 if (sc->mlx_mem != NULL)
189 bus_release_resource(sc->mlx_dev, sc->mlx_mem_type, sc->mlx_mem_rid, sc->mlx_mem);
191 /* free controller enquiry data */
192 if (sc->mlx_enq2 != NULL)
193 kfree(sc->mlx_enq2, M_DEVBUF);
195 dev_ops_remove_minor(&mlx_ops, device_get_unit(sc->mlx_dev));
198 /********************************************************************************
199 * Map the scatter/gather table into bus space
202 mlx_dma_map_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
204 struct mlx_softc *sc = (struct mlx_softc *)arg;
208 /* save base of s/g table's address in bus space */
209 sc->mlx_sgbusaddr = segs->ds_addr;
213 mlx_sglist_map(struct mlx_softc *sc)
220 /* destroy any existing mappings */
222 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
224 bus_dma_tag_destroy(sc->mlx_sg_dmat);
227 * Create a single tag describing a region large enough to hold all of
228 * the s/g lists we will need. If we're called early on, we don't know how
229 * many commands we're going to be asked to support, so only allocate enough
232 if (sc->mlx_enq2 == NULL) {
235 ncmd = sc->mlx_enq2->me_max_commands;
237 segsize = sizeof(struct mlx_sgentry) * MLX_NSEG * ncmd;
238 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */
239 1, 0, /* alignment, boundary */
240 BUS_SPACE_MAXADDR, /* lowaddr */
241 BUS_SPACE_MAXADDR, /* highaddr */
242 NULL, NULL, /* filter, filterarg */
243 segsize, 1, /* maxsize, nsegments */
244 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
248 device_printf(sc->mlx_dev, "can't allocate scatter/gather DMA tag\n");
253 * Allocate enough s/g maps for all commands and permanently map them into
254 * controller-visible space.
256 * XXX this assumes we can get enough space for all the s/g maps in one
257 * contiguous slab. We may need to switch to a more complex arrangement where
258 * we allocate in smaller chunks and keep a lookup table from slot to bus address.
260 error = bus_dmamem_alloc(sc->mlx_sg_dmat, (void *)&sc->mlx_sgtable, BUS_DMA_NOWAIT, &sc->mlx_sg_dmamap);
262 device_printf(sc->mlx_dev, "can't allocate s/g table\n");
265 bus_dmamap_load(sc->mlx_sg_dmat, sc->mlx_sg_dmamap, sc->mlx_sgtable, segsize, mlx_dma_map_sg, sc, 0);
269 /********************************************************************************
270 * Initialise the controller and softc
273 mlx_attach(struct mlx_softc *sc)
275 struct mlx_enquiry_old *meo;
276 int rid, error, fwminor, hscode, hserror, hsparam1, hsparam2, hsmsg;
279 callout_init(&sc->mlx_timeout);
282 * Initialise per-controller queues.
284 TAILQ_INIT(&sc->mlx_work);
285 TAILQ_INIT(&sc->mlx_freecmds);
286 bioq_init(&sc->mlx_bioq);
289 * Select accessor methods based on controller interface type.
291 switch(sc->mlx_iftype) {
294 sc->mlx_tryqueue = mlx_v3_tryqueue;
295 sc->mlx_findcomplete = mlx_v3_findcomplete;
296 sc->mlx_intaction = mlx_v3_intaction;
297 sc->mlx_fw_handshake = mlx_v3_fw_handshake;
300 sc->mlx_tryqueue = mlx_v4_tryqueue;
301 sc->mlx_findcomplete = mlx_v4_findcomplete;
302 sc->mlx_intaction = mlx_v4_intaction;
303 sc->mlx_fw_handshake = mlx_v4_fw_handshake;
306 sc->mlx_tryqueue = mlx_v5_tryqueue;
307 sc->mlx_findcomplete = mlx_v5_findcomplete;
308 sc->mlx_intaction = mlx_v5_intaction;
309 sc->mlx_fw_handshake = mlx_v5_fw_handshake;
313 return(ENXIO); /* should never happen */
316 /* disable interrupts before we start talking to the controller */
317 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
320 * Wait for the controller to come ready, handshake with the firmware if required.
321 * This is typically only necessary on platforms where the controller BIOS does not
326 while ((hscode = sc->mlx_fw_handshake(sc, &hserror, &hsparam1, &hsparam2)) != 0) {
327 /* report first time around... */
329 device_printf(sc->mlx_dev, "controller initialisation in progress...\n");
332 /* did we get a real message? */
334 hscode = mlx_fw_message(sc, hserror, hsparam1, hsparam2);
335 /* fatal initialisation error? */
343 device_printf(sc->mlx_dev, "initialisation complete.\n");
346 * Allocate and connect our interrupt.
349 sc->mlx_irq = bus_alloc_resource(sc->mlx_dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
350 if (sc->mlx_irq == NULL) {
351 device_printf(sc->mlx_dev, "can't allocate interrupt\n");
355 error = bus_setup_intr(sc->mlx_dev, sc->mlx_irq,
357 &sc->mlx_intr, NULL);
359 device_printf(sc->mlx_dev, "can't set up interrupt\n");
365 * Create DMA tag for mapping buffers into controller-addressable space.
367 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */
368 1, 0, /* alignment, boundary */
369 BUS_SPACE_MAXADDR, /* lowaddr */
370 BUS_SPACE_MAXADDR, /* highaddr */
371 NULL, NULL, /* filter, filterarg */
372 MAXBSIZE, MLX_NSEG, /* maxsize, nsegments */
373 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
375 &sc->mlx_buffer_dmat);
377 device_printf(sc->mlx_dev, "can't allocate buffer DMA tag\n");
383 * Create some initial scatter/gather mappings so we can run the probe commands.
385 error = mlx_sglist_map(sc);
387 device_printf(sc->mlx_dev, "can't make initial s/g list mapping\n");
393 * We don't (yet) know where the event log is up to.
395 sc->mlx_currevent = -1;
398 * Obtain controller feature information
400 if ((sc->mlx_enq2 = mlx_enquire(sc, MLX_CMD_ENQUIRY2, sizeof(struct mlx_enquiry2), NULL)) == NULL) {
401 device_printf(sc->mlx_dev, "ENQUIRY2 failed\n");
407 * Do quirk/feature related things.
409 fwminor = (sc->mlx_enq2->me_firmware_id >> 8) & 0xff;
410 switch(sc->mlx_iftype) {
412 /* These controllers don't report the firmware version in the ENQUIRY2 response */
413 if ((meo = mlx_enquire(sc, MLX_CMD_ENQUIRY_OLD, sizeof(struct mlx_enquiry_old), NULL)) == NULL) {
414 device_printf(sc->mlx_dev, "ENQUIRY_OLD failed\n");
418 sc->mlx_enq2->me_firmware_id = ('0' << 24) | (0 << 16) | (meo->me_fwminor << 8) | meo->me_fwmajor;
419 kfree(meo, M_DEVBUF);
421 /* XXX require 2.42 or better (PCI) or 2.14 or better (EISA) */
422 if (meo->me_fwminor < 42) {
423 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
424 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 2.42 or later\n");
428 /* XXX certify 3.52? */
430 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
431 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 3.51 or later\n");
435 /* XXX certify firmware versions? */
437 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
438 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 4.06 or later\n");
443 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
444 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 5.07 or later\n");
449 return(ENXIO); /* should never happen */
453 * Create the final scatter/gather mappings now that we have characterised the controller.
455 error = mlx_sglist_map(sc);
457 device_printf(sc->mlx_dev, "can't make final s/g list mapping\n");
463 * No user-requested background operation is in progress.
465 sc->mlx_background = 0;
466 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
469 * Create the control device.
471 make_dev(&mlx_ops, device_get_unit(sc->mlx_dev),
472 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
473 "mlx%d", device_get_unit(sc->mlx_dev));
476 * Start the timeout routine.
478 callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
480 /* print a little information about the controller */
481 mlx_describe_controller(sc);
486 /********************************************************************************
487 * Locate disk resources and attach children to them.
490 mlx_startup(struct mlx_softc *sc)
492 struct mlx_enq_sys_drive *mes;
493 struct mlx_sysdrive *dr;
499 * Scan all the system drives and attach children for those that
500 * don't currently have them.
502 mes = mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(*mes) * MLX_MAXDRIVES, NULL);
504 device_printf(sc->mlx_dev, "error fetching drive status\n");
508 /* iterate over drives returned */
509 for (i = 0, dr = &sc->mlx_sysdrive[0];
510 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
512 /* are we already attached to this drive? */
513 if (dr->ms_disk == 0) {
514 /* pick up drive information */
515 dr->ms_size = mes[i].sd_size;
516 dr->ms_raidlevel = mes[i].sd_raidlevel & 0xf;
517 dr->ms_state = mes[i].sd_state;
519 /* generate geometry information */
520 if (sc->mlx_geom == MLX_GEOM_128_32) {
523 dr->ms_cylinders = dr->ms_size / (128 * 32);
524 } else { /* MLX_GEOM_255/63 */
527 dr->ms_cylinders = dr->ms_size / (255 * 63);
529 dr->ms_disk = device_add_child(sc->mlx_dev, /*"mlxd"*/NULL, -1);
530 if (dr->ms_disk == 0)
531 device_printf(sc->mlx_dev, "device_add_child failed\n");
532 device_set_ivars(dr->ms_disk, dr);
535 kfree(mes, M_DEVBUF);
536 if ((error = bus_generic_attach(sc->mlx_dev)) != 0)
537 device_printf(sc->mlx_dev, "bus_generic_attach returned %d", error);
539 /* mark controller back up */
540 sc->mlx_state &= ~MLX_STATE_SHUTDOWN;
542 /* enable interrupts */
543 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
546 /********************************************************************************
547 * Disconnect from the controller completely, in preparation for unload.
550 mlx_detach(device_t dev)
552 struct mlx_softc *sc = device_get_softc(dev);
553 struct mlxd_softc *mlxd;
560 if (sc->mlx_state & MLX_STATE_OPEN)
563 for (i = 0; i < MLX_MAXDRIVES; i++) {
564 if (sc->mlx_sysdrive[i].ms_disk != 0) {
565 mlxd = device_get_softc(sc->mlx_sysdrive[i].ms_disk);
566 if (mlxd->mlxd_flags & MLXD_OPEN) { /* drive is mounted, abort detach */
567 device_printf(sc->mlx_sysdrive[i].ms_disk, "still open, can't detach\n");
572 if ((error = mlx_shutdown(dev)))
583 /********************************************************************************
584 * Bring the controller down to a dormant state and detach all child devices.
586 * This function is called before detach, system shutdown, or before performing
587 * an operation which may add or delete system disks. (Call mlx_startup to
588 * resume normal operation.)
590 * Note that we can assume that the bioq on the controller is empty, as we won't
591 * allow shutdown if any device is open.
594 mlx_shutdown(device_t dev)
596 struct mlx_softc *sc = device_get_softc(dev);
604 sc->mlx_state |= MLX_STATE_SHUTDOWN;
605 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
607 /* flush controller */
608 device_printf(sc->mlx_dev, "flushing cache...");
615 /* delete all our child devices */
616 for (i = 0; i < MLX_MAXDRIVES; i++) {
617 if (sc->mlx_sysdrive[i].ms_disk != 0) {
618 if ((error = device_delete_child(sc->mlx_dev, sc->mlx_sysdrive[i].ms_disk)) != 0)
620 sc->mlx_sysdrive[i].ms_disk = 0;
629 /********************************************************************************
630 * Bring the controller to a quiescent state, ready for system suspend.
633 mlx_suspend(device_t dev)
635 struct mlx_softc *sc = device_get_softc(dev);
640 sc->mlx_state |= MLX_STATE_SUSPEND;
642 /* flush controller */
643 device_printf(sc->mlx_dev, "flushing cache...");
644 kprintf("%s\n", mlx_flush(sc) ? "failed" : "done");
646 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
652 /********************************************************************************
653 * Bring the controller back to a state ready for operation.
656 mlx_resume(device_t dev)
658 struct mlx_softc *sc = device_get_softc(dev);
662 sc->mlx_state &= ~MLX_STATE_SUSPEND;
663 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
668 /*******************************************************************************
669 * Take an interrupt, or be poked by other code to look for interrupt-worthy
675 struct mlx_softc *sc = (struct mlx_softc *)arg;
679 /* collect finished commands, queue anything waiting */
683 /*******************************************************************************
684 * Receive a buf structure from a child device and queue it on a particular
685 * disk resource, then poke the disk resource to start as much work as it can.
688 mlx_submit_bio(struct mlx_softc *sc, struct bio *bio)
693 bioqdisksort(&sc->mlx_bioq, bio);
700 /********************************************************************************
701 * Accept an open operation on the control device.
704 mlx_open(struct dev_open_args *ap)
706 cdev_t dev = ap->a_head.a_dev;
707 int unit = minor(dev);
708 struct mlx_softc *sc = devclass_get_softc(mlx_devclass, unit);
710 sc->mlx_state |= MLX_STATE_OPEN;
714 /********************************************************************************
715 * Accept the last close on the control device.
718 mlx_close(struct dev_close_args *ap)
720 cdev_t dev = ap->a_head.a_dev;
721 int unit = minor(dev);
722 struct mlx_softc *sc = devclass_get_softc(mlx_devclass, unit);
724 sc->mlx_state &= ~MLX_STATE_OPEN;
728 /********************************************************************************
729 * Handle controller-specific control operations.
732 mlx_ioctl(struct dev_ioctl_args *ap)
734 cdev_t dev = ap->a_head.a_dev;
735 int unit = minor(dev);
736 struct mlx_softc *sc = devclass_get_softc(mlx_devclass, unit);
737 struct mlx_rebuild_request *rb = (struct mlx_rebuild_request *)ap->a_data;
738 struct mlx_rebuild_status *rs = (struct mlx_rebuild_status *)ap->a_data;
739 int *arg = (int *)ap->a_data;
740 struct mlx_pause *mp;
741 struct mlx_sysdrive *dr;
742 struct mlxd_softc *mlxd;
747 * Enumerate connected system drives; returns the first system drive's
748 * unit number if *arg is -1, or the next unit after *arg if it's
749 * a valid unit on this controller.
752 /* search system drives */
753 for (i = 0; i < MLX_MAXDRIVES; i++) {
754 /* is this one attached? */
755 if (sc->mlx_sysdrive[i].ms_disk != 0) {
756 /* looking for the next one we come across? */
758 *arg = device_get_unit(sc->mlx_sysdrive[0].ms_disk);
761 /* we want the one after this one */
762 if (*arg == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
769 * Scan the controller to see whether new drives have appeared.
771 case MLX_RESCAN_DRIVES:
776 * Disconnect from the specified drive; it may be about to go
779 case MLX_DETACH_DRIVE: /* detach one drive */
781 if (((dr = mlx_findunit(sc, *arg)) == NULL) ||
782 ((mlxd = device_get_softc(dr->ms_disk)) == NULL))
785 device_printf(dr->ms_disk, "detaching...");
787 if (mlxd->mlxd_flags & MLXD_OPEN) {
792 /* flush controller */
799 if ((error = device_delete_child(sc->mlx_dev, dr->ms_disk)) != 0)
812 * Pause one or more SCSI channels for a period of time, to assist
813 * in the process of hot-swapping devices.
815 * Note that at least the 3.51 firmware on the DAC960PL doesn't seem
818 case MLX_PAUSE_CHANNEL: /* schedule a channel pause */
819 /* Does this command work on this firmware? */
820 if (!(sc->mlx_feature & MLX_FEAT_PAUSEWORKS))
823 mp = (struct mlx_pause *)ap->a_data;
824 if ((mp->mp_which == MLX_PAUSE_CANCEL) && (sc->mlx_pause.mp_when != 0)) {
825 /* cancel a pending pause operation */
826 sc->mlx_pause.mp_which = 0;
828 /* fix for legal channels */
829 mp->mp_which &= ((1 << sc->mlx_enq2->me_actual_channels) -1);
830 /* check time values */
831 if ((mp->mp_when < 0) || (mp->mp_when > 3600))
833 if ((mp->mp_howlong < 1) || (mp->mp_howlong > (0xf * 30)))
836 /* check for a pause currently running */
837 if ((sc->mlx_pause.mp_which != 0) && (sc->mlx_pause.mp_when == 0))
840 /* looks ok, go with it */
841 sc->mlx_pause.mp_which = mp->mp_which;
842 sc->mlx_pause.mp_when = time_second + mp->mp_when;
843 sc->mlx_pause.mp_howlong = sc->mlx_pause.mp_when + mp->mp_howlong;
848 * Accept a command passthrough-style.
851 return(mlx_user_command(sc, (struct mlx_usercommand *)ap->a_data));
854 * Start a rebuild on a given SCSI disk
856 case MLX_REBUILDASYNC:
857 if (sc->mlx_background != 0) {
858 rb->rr_status = 0x0106;
861 rb->rr_status = mlx_rebuild(sc, rb->rr_channel, rb->rr_target);
862 switch (rb->rr_status) {
867 error = ENOMEM; /* couldn't set up the command */
886 sc->mlx_background = MLX_BACKGROUND_REBUILD;
890 * Get the status of the current rebuild or consistency check.
892 case MLX_REBUILDSTAT:
893 *rs = sc->mlx_rebuildstat;
897 * Return the per-controller system drive number matching the
898 * disk device number in (arg), if it happens to belong to us.
900 case MLX_GET_SYSDRIVE:
902 mlxd = (struct mlxd_softc *)devclass_get_softc(mlxd_devclass, *arg);
903 if ((mlxd != NULL) && (mlxd->mlxd_drive >= sc->mlx_sysdrive) &&
904 (mlxd->mlxd_drive < (sc->mlx_sysdrive + MLX_MAXDRIVES))) {
906 *arg = mlxd->mlxd_drive - sc->mlx_sysdrive;
915 /********************************************************************************
916 * Handle operations requested by a System Drive connected to this controller.
919 mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd,
920 caddr_t addr, int32_t flag)
922 int *arg = (int *)addr;
927 * Return the current status of this drive.
930 *arg = drive->ms_state;
934 * Start a background consistency check on this drive.
936 case MLXD_CHECKASYNC: /* start a background consistency check */
937 if (sc->mlx_background != 0) {
941 result = mlx_check(sc, drive - &sc->mlx_sysdrive[0]);
947 error = ENOMEM; /* couldn't set up the command */
963 sc->mlx_background = MLX_BACKGROUND_CHECK;
972 /********************************************************************************
973 ********************************************************************************
975 ********************************************************************************
976 ********************************************************************************/
978 /********************************************************************************
979 * Fire off commands to periodically check the status of connected drives.
982 mlx_periodic(void *data)
984 struct mlx_softc *sc = (struct mlx_softc *)data;
991 if ((sc->mlx_pause.mp_which != 0) &&
992 (sc->mlx_pause.mp_when > 0) &&
993 (time_second >= sc->mlx_pause.mp_when)){
995 mlx_pause_action(sc); /* pause is running */
996 sc->mlx_pause.mp_when = 0;
1000 * Bus pause still running?
1002 } else if ((sc->mlx_pause.mp_which != 0) &&
1003 (sc->mlx_pause.mp_when == 0)) {
1005 /* time to stop bus pause? */
1006 if (time_second >= sc->mlx_pause.mp_howlong) {
1007 mlx_pause_action(sc);
1008 sc->mlx_pause.mp_which = 0; /* pause is complete */
1011 sysbeep((time_second % 5) * 100 + 500, hz/8);
1015 * Run normal periodic activities?
1017 } else if (time_second > (sc->mlx_lastpoll + 10)) {
1018 sc->mlx_lastpoll = time_second;
1021 * Check controller status.
1023 * XXX Note that this may not actually launch a command in situations of high load.
1025 mlx_enquire(sc, (sc->mlx_iftype == MLX_IFTYPE_2) ? MLX_CMD_ENQUIRY_OLD : MLX_CMD_ENQUIRY,
1026 imax(sizeof(struct mlx_enquiry), sizeof(struct mlx_enquiry_old)), mlx_periodic_enquiry);
1029 * Check system drive status.
1031 * XXX This might be better left to event-driven detection, eg. I/O to an offline
1032 * drive will detect it's offline, rebuilds etc. should detect the drive is back
1035 mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(struct mlx_enq_sys_drive) * MLX_MAXDRIVES,
1036 mlx_periodic_enquiry);
1040 /* get drive rebuild/check status */
1041 /* XXX should check sc->mlx_background if this is only valid while in progress */
1042 mlx_enquire(sc, MLX_CMD_REBUILDSTAT, sizeof(struct mlx_rebuild_stat), mlx_periodic_rebuild);
1044 /* deal with possibly-missed interrupts and timed-out commands */
1047 /* reschedule another poll next second or so */
1048 callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
1051 /********************************************************************************
1052 * Handle the result of an ENQUIRY command instigated by periodic status polling.
1055 mlx_periodic_enquiry(struct mlx_command *mc)
1057 struct mlx_softc *sc = mc->mc_sc;
1061 /* Command completed OK? */
1062 if (mc->mc_status != 0) {
1063 device_printf(sc->mlx_dev, "periodic enquiry failed - %s\n", mlx_diagnose_command(mc));
1067 /* respond to command */
1068 switch(mc->mc_mailbox[0]) {
1070 * This is currently a bit fruitless, as we don't know how to extract the eventlog
1073 case MLX_CMD_ENQUIRY_OLD:
1075 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data;
1076 struct mlx_enquiry_old *meo = (struct mlx_enquiry_old *)mc->mc_data;
1079 /* convert data in-place to new format */
1080 for (i = (sizeof(me->me_dead) / sizeof(me->me_dead[0])) - 1; i >= 0; i--) {
1081 me->me_dead[i].dd_chan = meo->me_dead[i].dd_chan;
1082 me->me_dead[i].dd_targ = meo->me_dead[i].dd_targ;
1084 me->me_misc_flags = 0;
1085 me->me_rebuild_count = meo->me_rebuild_count;
1086 me->me_dead_count = meo->me_dead_count;
1087 me->me_critical_sd_count = meo->me_critical_sd_count;
1088 me->me_event_log_seq_num = 0;
1089 me->me_offline_sd_count = meo->me_offline_sd_count;
1090 me->me_max_commands = meo->me_max_commands;
1091 me->me_rebuild_flag = meo->me_rebuild_flag;
1092 me->me_fwmajor = meo->me_fwmajor;
1093 me->me_fwminor = meo->me_fwminor;
1094 me->me_status_flags = meo->me_status_flags;
1095 me->me_flash_age = meo->me_flash_age;
1096 for (i = (sizeof(me->me_drvsize) / sizeof(me->me_drvsize[0])) - 1; i >= 0; i--) {
1097 if (i > ((sizeof(meo->me_drvsize) / sizeof(meo->me_drvsize[0])) - 1)) {
1098 me->me_drvsize[i] = 0; /* drive beyond supported range */
1100 me->me_drvsize[i] = meo->me_drvsize[i];
1103 me->me_num_sys_drvs = meo->me_num_sys_drvs;
1108 * Generic controller status update. We could do more with this than just
1109 * checking the event log.
1111 case MLX_CMD_ENQUIRY:
1113 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data;
1115 if (sc->mlx_currevent == -1) {
1116 /* initialise our view of the event log */
1117 sc->mlx_currevent = sc->mlx_lastevent = me->me_event_log_seq_num;
1118 } else if ((me->me_event_log_seq_num != sc->mlx_lastevent) && !(sc->mlx_flags & MLX_EVENTLOG_BUSY)) {
1119 /* record where current events are up to */
1120 sc->mlx_currevent = me->me_event_log_seq_num;
1121 debug(1, "event log pointer was %d, now %d\n", sc->mlx_lastevent, sc->mlx_currevent);
1123 /* mark the event log as busy */
1124 atomic_set_int(&sc->mlx_flags, MLX_EVENTLOG_BUSY);
1126 /* drain new eventlog entries */
1127 mlx_periodic_eventlog_poll(sc);
1131 case MLX_CMD_ENQSYSDRIVE:
1133 struct mlx_enq_sys_drive *mes = (struct mlx_enq_sys_drive *)mc->mc_data;
1134 struct mlx_sysdrive *dr;
1137 for (i = 0, dr = &sc->mlx_sysdrive[0];
1138 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
1141 /* has state been changed by controller? */
1142 if (dr->ms_state != mes[i].sd_state) {
1143 switch(mes[i].sd_state) {
1144 case MLX_SYSD_OFFLINE:
1145 device_printf(dr->ms_disk, "drive offline\n");
1147 case MLX_SYSD_ONLINE:
1148 device_printf(dr->ms_disk, "drive online\n");
1150 case MLX_SYSD_CRITICAL:
1151 device_printf(dr->ms_disk, "drive critical\n");
1154 /* save new state */
1155 dr->ms_state = mes[i].sd_state;
1161 device_printf(sc->mlx_dev, "%s: unknown command 0x%x", __func__, mc->mc_mailbox[0]);
1166 kfree(mc->mc_data, M_DEVBUF);
1170 /********************************************************************************
1171 * Instigate a poll for one event log message on (sc).
1172 * We only poll for one message at a time, to keep our command usage down.
1175 mlx_periodic_eventlog_poll(struct mlx_softc *sc)
1177 struct mlx_command *mc;
1178 void *result = NULL;
1183 /* get ourselves a command buffer */
1185 if ((mc = mlx_alloccmd(sc)) == NULL)
1188 * allocate the response structure - sizeof(struct mlx_eventlog_entry)?
1189 * Called from timeout - use M_NOWAIT (repoll later on failure?)
1191 if ((result = kmalloc(1024, M_DEVBUF, M_NOWAIT)) == NULL)
1193 /* get a command slot */
1194 if (mlx_getslot(mc))
1197 /* map the command so the controller can see it */
1198 mc->mc_data = result;
1199 mc->mc_length = /*sizeof(struct mlx_eventlog_entry)*/1024;
1202 /* build the command to get one entry */
1203 mlx_make_type3(mc, MLX_CMD_LOGOP, MLX_LOGOP_GET, 1, sc->mlx_lastevent, 0, 0, mc->mc_dataphys, 0);
1204 mc->mc_complete = mlx_periodic_eventlog_respond;
1205 mc->mc_private = mc;
1207 /* start the command */
1208 if ((error = mlx_start(mc)) != 0)
1211 error = 0; /* success */
1217 kfree(result, M_DEVBUF);
1221 /********************************************************************************
1222 * Handle the result of polling for a log message, generate diagnostic output.
1223 * If this wasn't the last message waiting for us, we'll go collect another.
1225 static char *mlx_sense_messages[] = {
1226 "because write recovery failed",
1227 "because of SCSI bus reset failure",
1228 "because of double check condition",
1229 "because it was removed",
1230 "because of gross error on SCSI chip",
1231 "because of bad tag returned from drive",
1232 "because of timeout on SCSI command",
1233 "because of reset SCSI command issued from system",
1234 "because busy or parity error count exceeded limit",
1235 "because of 'kill drive' command from system",
1236 "because of selection timeout",
1237 "due to SCSI phase sequence error",
1238 "due to unknown status"
1242 mlx_periodic_eventlog_respond(struct mlx_command *mc)
1244 struct mlx_softc *sc = mc->mc_sc;
1245 struct mlx_eventlog_entry *el = (struct mlx_eventlog_entry *)mc->mc_data;
1250 sc->mlx_lastevent++; /* next message... */
1251 if (mc->mc_status == 0) {
1253 /* handle event log message */
1254 switch(el->el_type) {
1256 * This is the only sort of message we understand at the moment.
1257 * The tests here are probably incomplete.
1259 case MLX_LOGMSG_SENSE: /* sense data */
1260 /* Mylex vendor-specific message indicating a drive was killed? */
1261 if ((el->el_sensekey == 9) &&
1262 (el->el_asc == 0x80)) {
1263 if (el->el_asq < (sizeof(mlx_sense_messages) / sizeof(mlx_sense_messages[0]))) {
1264 reason = mlx_sense_messages[el->el_asq];
1266 reason = "for unknown reason";
1268 device_printf(sc->mlx_dev, "physical drive %d:%d killed %s\n",
1269 el->el_channel, el->el_target, reason);
1271 /* SCSI drive was reset? */
1272 if ((el->el_sensekey == 6) && (el->el_asc == 0x29)) {
1273 device_printf(sc->mlx_dev, "physical drive %d:%d reset\n",
1274 el->el_channel, el->el_target);
1276 /* SCSI drive error? */
1277 if (!((el->el_sensekey == 0) ||
1278 ((el->el_sensekey == 2) &&
1279 (el->el_asc == 0x04) &&
1280 ((el->el_asq == 0x01) ||
1281 (el->el_asq == 0x02))))) {
1282 device_printf(sc->mlx_dev, "physical drive %d:%d error log: sense = %d asc = %x asq = %x\n",
1283 el->el_channel, el->el_target, el->el_sensekey, el->el_asc, el->el_asq);
1284 device_printf(sc->mlx_dev, " info %4D csi %4D\n", el->el_information, ":", el->el_csi, ":");
1289 device_printf(sc->mlx_dev, "unknown log message type 0x%x\n", el->el_type);
1293 device_printf(sc->mlx_dev, "error reading message log - %s\n", mlx_diagnose_command(mc));
1294 /* give up on all the outstanding messages, as we may have come unsynched */
1295 sc->mlx_lastevent = sc->mlx_currevent;
1298 /* dispose of command and data */
1299 kfree(mc->mc_data, M_DEVBUF);
1302 /* is there another message to obtain? */
1303 if (sc->mlx_lastevent != sc->mlx_currevent) {
1304 mlx_periodic_eventlog_poll(sc);
1306 /* clear log-busy status */
1307 atomic_clear_int(&sc->mlx_flags, MLX_EVENTLOG_BUSY);
1311 /********************************************************************************
1312 * Handle check/rebuild operations in progress.
1315 mlx_periodic_rebuild(struct mlx_command *mc)
1317 struct mlx_softc *sc = mc->mc_sc;
1318 struct mlx_rebuild_status *mr = (struct mlx_rebuild_status *)mc->mc_data;
1320 switch(mc->mc_status) {
1321 case 0: /* operation running, update stats */
1322 sc->mlx_rebuildstat = *mr;
1324 /* spontaneous rebuild/check? */
1325 if (sc->mlx_background == 0) {
1326 sc->mlx_background = MLX_BACKGROUND_SPONTANEOUS;
1327 device_printf(sc->mlx_dev, "background check/rebuild operation started\n");
1331 case 0x0105: /* nothing running, finalise stats and report */
1332 switch(sc->mlx_background) {
1333 case MLX_BACKGROUND_CHECK:
1334 device_printf(sc->mlx_dev, "consistency check completed\n"); /* XXX print drive? */
1336 case MLX_BACKGROUND_REBUILD:
1337 device_printf(sc->mlx_dev, "drive rebuild completed\n"); /* XXX print channel/target? */
1339 case MLX_BACKGROUND_SPONTANEOUS:
1341 /* if we have previously been non-idle, report the transition */
1342 if (sc->mlx_rebuildstat.rs_code != MLX_REBUILDSTAT_IDLE) {
1343 device_printf(sc->mlx_dev, "background check/rebuild operation completed\n");
1346 sc->mlx_background = 0;
1347 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
1350 kfree(mc->mc_data, M_DEVBUF);
1354 /********************************************************************************
1355 ********************************************************************************
1357 ********************************************************************************
1358 ********************************************************************************/
1360 /********************************************************************************
1361 * It's time to perform a channel pause action for (sc), either start or stop
1365 mlx_pause_action(struct mlx_softc *sc)
1367 struct mlx_command *mc;
1368 int failsafe, i, command;
1370 /* What are we doing here? */
1371 if (sc->mlx_pause.mp_when == 0) {
1372 command = MLX_CMD_STARTCHANNEL;
1376 command = MLX_CMD_STOPCHANNEL;
1379 * Channels will always start again after the failsafe period,
1380 * which is specified in multiples of 30 seconds.
1381 * This constrains us to a maximum pause of 450 seconds.
1383 failsafe = ((sc->mlx_pause.mp_howlong - time_second) + 5) / 30;
1384 if (failsafe > 0xf) {
1386 sc->mlx_pause.mp_howlong = time_second + (0xf * 30) - 5;
1390 /* build commands for every channel requested */
1391 for (i = 0; i < sc->mlx_enq2->me_actual_channels; i++) {
1392 if ((1 << i) & sc->mlx_pause.mp_which) {
1394 /* get ourselves a command buffer */
1395 if ((mc = mlx_alloccmd(sc)) == NULL)
1397 /* get a command slot */
1398 mc->mc_flags |= MLX_CMD_PRIORITY;
1399 if (mlx_getslot(mc))
1402 /* build the command */
1403 mlx_make_type2(mc, command, (failsafe << 4) | i, 0, 0, 0, 0, 0, 0, 0);
1404 mc->mc_complete = mlx_pause_done;
1405 mc->mc_private = sc; /* XXX not needed */
1408 /* command submitted OK */
1412 device_printf(sc->mlx_dev, "%s failed for channel %d\n",
1413 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", i);
1421 mlx_pause_done(struct mlx_command *mc)
1423 struct mlx_softc *sc = mc->mc_sc;
1424 int command = mc->mc_mailbox[0];
1425 int channel = mc->mc_mailbox[2] & 0xf;
1427 if (mc->mc_status != 0) {
1428 device_printf(sc->mlx_dev, "%s command failed - %s\n",
1429 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", mlx_diagnose_command(mc));
1430 } else if (command == MLX_CMD_STOPCHANNEL) {
1431 device_printf(sc->mlx_dev, "channel %d pausing for %ld seconds\n",
1432 channel, (long)(sc->mlx_pause.mp_howlong - time_second));
1434 device_printf(sc->mlx_dev, "channel %d resuming\n", channel);
1439 /********************************************************************************
1440 ********************************************************************************
1442 ********************************************************************************
1443 ********************************************************************************/
1445 /********************************************************************************
1446 * Perform an Enquiry command using a type-3 command buffer and a return a single
1447 * linear result buffer. If the completion function is specified, it will
1448 * be called with the completed command (and the result response will not be
1449 * valid until that point). Otherwise, the command will either be busy-waited
1450 * for (interrupts not enabled), or slept for.
1453 mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize, void (* complete)(struct mlx_command *mc))
1455 struct mlx_command *mc;
1461 /* get ourselves a command buffer */
1464 if ((mc = mlx_alloccmd(sc)) == NULL)
1466 /* allocate the response structure */
1467 result = kmalloc(bufsize, M_DEVBUF, M_INTWAIT);
1468 /* get a command slot */
1469 mc->mc_flags |= MLX_CMD_PRIORITY | MLX_CMD_DATAOUT;
1470 if (mlx_getslot(mc))
1473 /* map the command so the controller can see it */
1474 mc->mc_data = result;
1475 mc->mc_length = bufsize;
1478 /* build an enquiry command */
1479 mlx_make_type2(mc, command, 0, 0, 0, 0, 0, 0, mc->mc_dataphys, 0);
1481 /* do we want a completion callback? */
1482 if (complete != NULL) {
1483 mc->mc_complete = complete;
1484 mc->mc_private = mc;
1485 if ((error = mlx_start(mc)) != 0)
1488 /* run the command in either polled or wait mode */
1489 if ((sc->mlx_state & MLX_STATE_INTEN) ? mlx_wait_command(mc) : mlx_poll_command(mc))
1492 /* command completed OK? */
1493 if (mc->mc_status != 0) {
1494 device_printf(sc->mlx_dev, "ENQUIRY failed - %s\n", mlx_diagnose_command(mc));
1498 error = 0; /* success */
1500 /* we got a command, but nobody else will free it */
1501 if ((complete == NULL) && (mc != NULL))
1503 /* we got an error, and we allocated a result */
1504 if ((error != 0) && (result != NULL)) {
1505 kfree(result, M_DEVBUF);
1512 /********************************************************************************
1513 * Perform a Flush command on the nominated controller.
1515 * May be called with interrupts enabled or disabled; will not return until
1516 * the flush operation completes or fails.
1519 mlx_flush(struct mlx_softc *sc)
1521 struct mlx_command *mc;
1526 /* get ourselves a command buffer */
1528 if ((mc = mlx_alloccmd(sc)) == NULL)
1530 /* get a command slot */
1531 if (mlx_getslot(mc))
1534 /* build a flush command */
1535 mlx_make_type2(mc, MLX_CMD_FLUSH, 0, 0, 0, 0, 0, 0, 0, 0);
1537 /* can't assume that interrupts are going to work here, so play it safe */
1538 if (mlx_poll_command(mc))
1541 /* command completed OK? */
1542 if (mc->mc_status != 0) {
1543 device_printf(sc->mlx_dev, "FLUSH failed - %s\n", mlx_diagnose_command(mc));
1547 error = 0; /* success */
1554 /********************************************************************************
1555 * Start a background consistency check on (drive).
1557 * May be called with interrupts enabled or disabled; will return as soon as the
1558 * operation has started or been refused.
1561 mlx_check(struct mlx_softc *sc, int drive)
1563 struct mlx_command *mc;
1568 /* get ourselves a command buffer */
1570 if ((mc = mlx_alloccmd(sc)) == NULL)
1572 /* get a command slot */
1573 if (mlx_getslot(mc))
1576 /* build a checkasync command, set the "fix it" flag */
1577 mlx_make_type2(mc, MLX_CMD_CHECKASYNC, 0, 0, 0, 0, 0, drive | 0x80, 0, 0);
1579 /* start the command and wait for it to be returned */
1580 if (mlx_wait_command(mc))
1583 /* command completed OK? */
1584 if (mc->mc_status != 0) {
1585 device_printf(sc->mlx_dev, "CHECK ASYNC failed - %s\n", mlx_diagnose_command(mc));
1587 device_printf(sc->mlx_sysdrive[drive].ms_disk, "consistency check started");
1589 error = mc->mc_status;
1597 /********************************************************************************
1598 * Start a background rebuild of the physical drive at (channel),(target).
1600 * May be called with interrupts enabled or disabled; will return as soon as the
1601 * operation has started or been refused.
1604 mlx_rebuild(struct mlx_softc *sc, int channel, int target)
1606 struct mlx_command *mc;
1611 /* get ourselves a command buffer */
1613 if ((mc = mlx_alloccmd(sc)) == NULL)
1615 /* get a command slot */
1616 if (mlx_getslot(mc))
1619 /* build a checkasync command, set the "fix it" flag */
1620 mlx_make_type2(mc, MLX_CMD_REBUILDASYNC, channel, target, 0, 0, 0, 0, 0, 0);
1622 /* start the command and wait for it to be returned */
1623 if (mlx_wait_command(mc))
1626 /* command completed OK? */
1627 if (mc->mc_status != 0) {
1628 device_printf(sc->mlx_dev, "REBUILD ASYNC failed - %s\n", mlx_diagnose_command(mc));
1630 device_printf(sc->mlx_dev, "drive rebuild started for %d:%d\n", channel, target);
1632 error = mc->mc_status;
1640 /********************************************************************************
1641 * Run the command (mc) and return when it completes.
1643 * Interrupts need to be enabled; returns nonzero on error.
1646 mlx_wait_command(struct mlx_command *mc)
1648 struct mlx_softc *sc = mc->mc_sc;
1653 mc->mc_complete = NULL;
1654 mc->mc_private = mc; /* wake us when you're done */
1655 if ((error = mlx_start(mc)) != 0)
1659 /* XXX better timeout? */
1660 while ((mc->mc_status == MLX_STATUS_BUSY) && (count < 30)) {
1661 tsleep(mc->mc_private, PCATCH, "mlxwcmd", hz);
1664 if (mc->mc_status != 0) {
1665 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1672 /********************************************************************************
1673 * Start the command (mc) and busy-wait for it to complete.
1675 * Should only be used when interrupts can't be relied upon. Returns 0 on
1676 * success, nonzero on error.
1677 * Successfully completed commands are dequeued.
1680 mlx_poll_command(struct mlx_command *mc)
1682 struct mlx_softc *sc = mc->mc_sc;
1687 mc->mc_complete = NULL;
1688 mc->mc_private = NULL; /* we will poll for it */
1689 if ((error = mlx_start(mc)) != 0)
1694 /* poll for completion */
1695 mlx_done(mc->mc_sc);
1697 } while ((mc->mc_status == MLX_STATUS_BUSY) && (count++ < 15000000));
1698 if (mc->mc_status != MLX_STATUS_BUSY) {
1700 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
1704 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1708 /********************************************************************************
1709 * Pull as much work off the softc's work queue as possible and give it to the
1710 * controller. Leave a couple of slots free for emergencies.
1712 * Must be called at splbio or in an equivalent fashion that prevents
1713 * reentry or activity on the bioq.
1716 mlx_startio(struct mlx_softc *sc)
1718 struct mlx_command *mc;
1719 struct mlxd_softc *mlxd;
1727 /* avoid reentrancy */
1728 if (mlx_lock_tas(sc, MLX_LOCK_STARTING))
1731 /* spin until something prevents us from doing any work */
1735 /* see if there's work to be done */
1736 if ((bio = bioq_first(&sc->mlx_bioq)) == NULL)
1739 if ((mc = mlx_alloccmd(sc)) == NULL)
1741 /* get a slot for the command */
1742 if (mlx_getslot(mc) != 0) {
1746 /* get the buf containing our work */
1747 bioq_remove(&sc->mlx_bioq, bio);
1752 /* connect the buf to the command */
1753 mc->mc_complete = mlx_completeio;
1754 mc->mc_private = bp;
1755 mc->mc_data = bp->b_data;
1756 mc->mc_length = bp->b_bcount;
1757 if (bp->b_cmd == BUF_CMD_READ) {
1758 mc->mc_flags |= MLX_CMD_DATAIN;
1759 cmd = MLX_CMD_READSG;
1761 mc->mc_flags |= MLX_CMD_DATAOUT;
1762 cmd = MLX_CMD_WRITESG;
1765 /* map the command so the controller can work with it */
1768 /* build a suitable I/O command (assumes 512-byte rounded transfers) */
1769 mlxd = (struct mlxd_softc *)bio->bio_driver_info;
1770 driveno = mlxd->mlxd_drive - sc->mlx_sysdrive;
1771 blkcount = (bp->b_bcount + MLX_BLKSIZE - 1) / MLX_BLKSIZE;
1772 blkno = bio->bio_offset / MLX_BLKSIZE;
1774 if ((blkno + blkcount) > sc->mlx_sysdrive[driveno].ms_size)
1775 device_printf(sc->mlx_dev, "I/O beyond end of unit (%u,%d > %u)\n",
1776 blkno, blkcount, sc->mlx_sysdrive[driveno].ms_size);
1779 * Build the I/O command. Note that the SG list type bits are set to zero,
1780 * denoting the format of SG list that we are using.
1782 if (sc->mlx_iftype == MLX_IFTYPE_2) {
1783 mlx_make_type1(mc, (cmd == MLX_CMD_WRITESG) ? MLX_CMD_WRITESG_OLD : MLX_CMD_READSG_OLD,
1784 blkcount & 0xff, /* xfer length low byte */
1785 blkno, /* physical block number */
1786 driveno, /* target drive number */
1787 mc->mc_sgphys, /* location of SG list */
1788 mc->mc_nsgent & 0x3f); /* size of SG list (top 3 bits clear) */
1790 mlx_make_type5(mc, cmd,
1791 blkcount & 0xff, /* xfer length low byte */
1792 (driveno << 3) | ((blkcount >> 8) & 0x07), /* target and length high 3 bits */
1793 blkno, /* physical block number */
1794 mc->mc_sgphys, /* location of SG list */
1795 mc->mc_nsgent & 0x3f); /* size of SG list (top 3 bits clear) */
1798 /* try to give command to controller */
1799 if (mlx_start(mc) != 0) {
1800 /* fail the command */
1801 mc->mc_status = MLX_STATUS_WEDGED;
1807 mlx_lock_clr(sc, MLX_LOCK_STARTING);
1810 /********************************************************************************
1811 * Handle completion of an I/O command.
1814 mlx_completeio(struct mlx_command *mc)
1816 struct mlx_softc *sc = mc->mc_sc;
1817 struct bio *bio = (mlx_bio *)mc->mc_private;
1818 struct mlxd_softc *mlxd = (struct mlxd_softc *)bio->bio_driver_info;
1819 struct buf *bp = bio->bio_buf;
1821 if (mc->mc_status != MLX_STATUS_OK) { /* could be more verbose here? */
1823 bp->b_flags |= B_ERROR;
1825 switch(mc->mc_status) {
1826 case MLX_STATUS_RDWROFFLINE: /* system drive has gone offline */
1827 device_printf(mlxd->mlxd_dev, "drive offline\n");
1828 /* should signal this with a return code */
1829 mlxd->mlxd_drive->ms_state = MLX_SYSD_OFFLINE;
1832 default: /* other I/O error */
1833 device_printf(sc->mlx_dev, "I/O error - %s\n", mlx_diagnose_command(mc));
1835 device_printf(sc->mlx_dev, " b_bcount %ld offset %lld\n",
1836 bp->b_bcount, bio->bio_offset);
1837 device_printf(sc->mlx_dev, " %13D\n", mc->mc_mailbox, " ");
1846 /********************************************************************************
1847 * Take a command from user-space and try to run it.
1849 * XXX Note that this can't perform very much in the way of error checking, and
1850 * as such, applications _must_ be considered trustworthy.
1851 * XXX Commands using S/G for data are not supported.
1854 mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu)
1856 struct mlx_command *mc;
1857 struct mlx_dcdb *dcdb;
1868 /* get ourselves a command and copy in from user space */
1869 if ((mc = mlx_alloccmd(sc)) == NULL)
1871 bcopy(mu->mu_command, mc->mc_mailbox, sizeof(mc->mc_mailbox));
1872 debug(0, "got command buffer");
1874 /* if we need a buffer for data transfer, allocate one and copy in its initial contents */
1875 if (mu->mu_datasize > 0) {
1876 if (mu->mu_datasize > MAXPHYS)
1878 if (((kbuf = kmalloc(mu->mu_datasize, M_DEVBUF, M_WAITOK)) == NULL) ||
1879 (error = copyin(mu->mu_buf, kbuf, mu->mu_datasize)))
1881 debug(0, "got kernel buffer");
1884 /* get a command slot */
1885 if (mlx_getslot(mc))
1887 debug(0, "got a slot");
1889 /* map the command so the controller can see it */
1891 mc->mc_length = mu->mu_datasize;
1896 * If this is a passthrough SCSI command, the DCDB is packed at the
1897 * beginning of the data area. Fix up the DCDB to point to the correct physical
1898 * address and override any bufptr supplied by the caller since we know
1899 * what it's meant to be.
1901 if (mc->mc_mailbox[0] == MLX_CMD_DIRECT_CDB) {
1902 dcdb = (struct mlx_dcdb *)kbuf;
1903 dcdb->dcdb_physaddr = mc->mc_dataphys + sizeof(*dcdb);
1908 * If there's a data buffer, fix up the command's buffer pointer.
1910 if (mu->mu_datasize > 0) {
1912 /* range check the pointer to physical buffer address */
1913 if ((mu->mu_bufptr < 0) || (mu->mu_bufptr > (sizeof(mu->mu_command) - sizeof(u_int32_t)))) {
1917 mc->mc_mailbox[mu->mu_bufptr ] = mc->mc_dataphys & 0xff;
1918 mc->mc_mailbox[mu->mu_bufptr + 1] = (mc->mc_dataphys >> 8) & 0xff;
1919 mc->mc_mailbox[mu->mu_bufptr + 2] = (mc->mc_dataphys >> 16) & 0xff;
1920 mc->mc_mailbox[mu->mu_bufptr + 3] = (mc->mc_dataphys >> 24) & 0xff;
1922 debug(0, "command fixup");
1924 /* submit the command and wait */
1925 if ((error = mlx_wait_command(mc)) != 0)
1928 /* copy out status and data */
1929 mu->mu_status = mc->mc_status;
1930 if ((mu->mu_datasize > 0) && ((error = copyout(kbuf, mu->mu_buf, mu->mu_datasize))))
1937 kfree(kbuf, M_DEVBUF);
1941 /********************************************************************************
1942 ********************************************************************************
1943 Command I/O to Controller
1944 ********************************************************************************
1945 ********************************************************************************/
1947 /********************************************************************************
1948 * Find a free command slot for (mc).
1950 * Don't hand out a slot to a normal-priority command unless there are at least
1951 * 4 slots free for priority commands.
1954 mlx_getslot(struct mlx_command *mc)
1956 struct mlx_softc *sc = mc->mc_sc;
1962 * Enforce slot-usage limit, if we have the required information.
1964 if (sc->mlx_enq2 != NULL) {
1965 limit = sc->mlx_enq2->me_max_commands;
1969 if (sc->mlx_busycmds >= ((mc->mc_flags & MLX_CMD_PRIORITY) ? limit : limit - 4))
1973 * Allocate an outstanding command slot
1975 * XXX linear search is slow
1978 for (slot = 0; slot < limit; slot++) {
1979 debug(2, "try slot %d", slot);
1980 if (sc->mlx_busycmd[slot] == NULL)
1984 sc->mlx_busycmd[slot] = mc;
1993 debug(2, "got slot %d", slot);
1998 /********************************************************************************
1999 * Map/unmap (mc)'s data in the controller's addressable space.
2002 mlx_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
2004 struct mlx_command *mc = (struct mlx_command *)arg;
2005 struct mlx_softc *sc = mc->mc_sc;
2006 struct mlx_sgentry *sg;
2011 /* XXX should be unnecessary */
2012 if (sc->mlx_enq2 && (nsegments > sc->mlx_enq2->me_max_sg))
2013 panic("MLX: too many s/g segments (%d, max %d)", nsegments, sc->mlx_enq2->me_max_sg);
2015 /* get base address of s/g table */
2016 sg = sc->mlx_sgtable + (mc->mc_slot * MLX_NSEG);
2018 /* save s/g table information in command */
2019 mc->mc_nsgent = nsegments;
2020 mc->mc_sgphys = sc->mlx_sgbusaddr + (mc->mc_slot * MLX_NSEG * sizeof(struct mlx_sgentry));
2021 mc->mc_dataphys = segs[0].ds_addr;
2023 /* populate s/g table */
2024 for (i = 0; i < nsegments; i++, sg++) {
2025 sg->sg_addr = segs[i].ds_addr;
2026 sg->sg_count = segs[i].ds_len;
2031 mlx_mapcmd(struct mlx_command *mc)
2033 struct mlx_softc *sc = mc->mc_sc;
2037 /* if the command involves data at all */
2038 if (mc->mc_data != NULL) {
2040 /* map the data buffer into bus space and build the s/g list */
2041 bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data, mc->mc_length,
2042 mlx_setup_dmamap, mc, 0);
2043 if (mc->mc_flags & MLX_CMD_DATAIN)
2044 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_PREREAD);
2045 if (mc->mc_flags & MLX_CMD_DATAOUT)
2046 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_PREWRITE);
2051 mlx_unmapcmd(struct mlx_command *mc)
2053 struct mlx_softc *sc = mc->mc_sc;
2057 /* if the command involved data at all */
2058 if (mc->mc_data != NULL) {
2060 if (mc->mc_flags & MLX_CMD_DATAIN)
2061 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTREAD);
2062 if (mc->mc_flags & MLX_CMD_DATAOUT)
2063 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTWRITE);
2065 bus_dmamap_unload(sc->mlx_buffer_dmat, mc->mc_dmamap);
2069 /********************************************************************************
2070 * Try to deliver (mc) to the controller.
2072 * Can be called at any interrupt level, with or without interrupts enabled.
2075 mlx_start(struct mlx_command *mc)
2077 struct mlx_softc *sc = mc->mc_sc;
2082 /* save the slot number as ident so we can handle this command when complete */
2083 mc->mc_mailbox[0x1] = mc->mc_slot;
2085 /* mark the command as currently being processed */
2086 mc->mc_status = MLX_STATUS_BUSY;
2088 /* set a default 60-second timeout XXX tunable? XXX not currently used */
2089 mc->mc_timeout = time_second + 60;
2091 /* spin waiting for the mailbox */
2092 for (i = 100000, done = 0; (i > 0) && !done; i--) {
2094 if (sc->mlx_tryqueue(sc, mc)) {
2096 /* move command to work queue */
2097 TAILQ_INSERT_TAIL(&sc->mlx_work, mc, mc_link);
2099 crit_exit(); /* drop spl to allow completion interrupts */
2102 /* command is enqueued */
2107 * We couldn't get the controller to take the command. Revoke the slot
2108 * that the command was given and return it with a bad status.
2110 sc->mlx_busycmd[mc->mc_slot] = NULL;
2111 device_printf(sc->mlx_dev, "controller wedged (not taking commands)\n");
2112 mc->mc_status = MLX_STATUS_WEDGED;
2117 /********************************************************************************
2118 * Poll the controller (sc) for completed commands.
2119 * Update command status and free slots for reuse. If any slots were freed,
2120 * new commands may be posted.
2122 * Returns nonzero if one or more commands were completed.
2125 mlx_done(struct mlx_softc *sc)
2127 struct mlx_command *mc;
2136 /* loop collecting completed commands */
2139 /* poll for a completed command's identifier and status */
2140 if (sc->mlx_findcomplete(sc, &slot, &status)) {
2142 mc = sc->mlx_busycmd[slot]; /* find command */
2143 if (mc != NULL) { /* paranoia */
2144 if (mc->mc_status == MLX_STATUS_BUSY) {
2145 mc->mc_status = status; /* save status */
2147 /* free slot for reuse */
2148 sc->mlx_busycmd[slot] = NULL;
2151 device_printf(sc->mlx_dev, "duplicate done event for slot %d\n", slot);
2154 device_printf(sc->mlx_dev, "done event for nonbusy slot %d\n", slot);
2162 /* if we've completed any commands, try posting some more */
2166 /* handle completion and timeouts */
2172 /********************************************************************************
2173 * Perform post-completion processing for commands on (sc).
2176 mlx_complete(struct mlx_softc *sc)
2178 struct mlx_command *mc, *nc;
2183 /* avoid reentrancy XXX might want to signal and request a restart */
2184 if (mlx_lock_tas(sc, MLX_LOCK_COMPLETING))
2190 /* scan the list of busy/done commands */
2191 mc = TAILQ_FIRST(&sc->mlx_work);
2192 while (mc != NULL) {
2193 nc = TAILQ_NEXT(mc, mc_link);
2195 /* Command has been completed in some fashion */
2196 if (mc->mc_status != MLX_STATUS_BUSY) {
2198 /* unmap the command's data buffer */
2201 * Does the command have a completion handler?
2203 if (mc->mc_complete != NULL) {
2204 /* remove from list and give to handler */
2205 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2206 mc->mc_complete(mc);
2209 * Is there a sleeper waiting on this command?
2211 } else if (mc->mc_private != NULL) { /* sleeping caller wants to know about it */
2213 /* remove from list and wake up sleeper */
2214 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2215 wakeup_one(mc->mc_private);
2218 * Leave the command for a caller that's polling for it.
2227 mlx_lock_clr(sc, MLX_LOCK_COMPLETING);
2230 /********************************************************************************
2231 ********************************************************************************
2232 Command Buffer Management
2233 ********************************************************************************
2234 ********************************************************************************/
2236 /********************************************************************************
2237 * Get a new command buffer.
2239 * This may return NULL in low-memory cases.
2241 * Note that using malloc() is expensive (the command buffer is << 1 page) but
2242 * necessary if we are to be a loadable module before the zone allocator is fixed.
2244 * If possible, we recycle a command buffer that's been used before.
2246 * XXX Note that command buffers are not cleaned out - it is the caller's
2247 * responsibility to ensure that all required fields are filled in before
2250 static struct mlx_command *
2251 mlx_alloccmd(struct mlx_softc *sc)
2253 struct mlx_command *mc;
2259 if ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL)
2260 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
2263 /* allocate a new command buffer? */
2265 mc = kmalloc(sizeof(*mc), M_DEVBUF, M_INTWAIT | M_ZERO);
2267 error = bus_dmamap_create(sc->mlx_buffer_dmat, 0, &mc->mc_dmamap);
2269 kfree(mc, M_DEVBUF);
2276 /********************************************************************************
2277 * Release a command buffer for recycling.
2279 * XXX It might be a good idea to limit the number of commands we save for reuse
2280 * if it's shown that this list bloats out massively.
2283 mlx_releasecmd(struct mlx_command *mc)
2288 TAILQ_INSERT_HEAD(&mc->mc_sc->mlx_freecmds, mc, mc_link);
2292 /********************************************************************************
2293 * Permanently discard a command buffer.
2296 mlx_freecmd(struct mlx_command *mc)
2298 struct mlx_softc *sc = mc->mc_sc;
2301 bus_dmamap_destroy(sc->mlx_buffer_dmat, mc->mc_dmamap);
2302 kfree(mc, M_DEVBUF);
2306 /********************************************************************************
2307 ********************************************************************************
2308 Type 3 interface accessor methods
2309 ********************************************************************************
2310 ********************************************************************************/
2312 /********************************************************************************
2313 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2314 * (the controller is not ready to take a command).
2316 * Must be called at splbio or in a fashion that prevents reentry.
2319 mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2325 /* ready for our command? */
2326 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_FULL)) {
2327 /* copy mailbox data to window */
2328 for (i = 0; i < 13; i++)
2329 MLX_V3_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2332 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_FULL);
2338 /********************************************************************************
2339 * See if a command has been completed, if so acknowledge its completion
2340 * and recover the slot number and status code.
2342 * Must be called at splbio or in a fashion that prevents reentry.
2345 mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2350 /* status available? */
2351 if (MLX_V3_GET_ODBR(sc) & MLX_V3_ODB_SAVAIL) {
2352 *slot = MLX_V3_GET_STATUS_IDENT(sc); /* get command identifier */
2353 *status = MLX_V3_GET_STATUS(sc); /* get status */
2355 /* acknowledge completion */
2356 MLX_V3_PUT_ODBR(sc, MLX_V3_ODB_SAVAIL);
2357 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2363 /********************************************************************************
2364 * Enable/disable interrupts as requested. (No acknowledge required)
2366 * Must be called at splbio or in a fashion that prevents reentry.
2369 mlx_v3_intaction(struct mlx_softc *sc, int action)
2374 case MLX_INTACTION_DISABLE:
2375 MLX_V3_PUT_IER(sc, 0);
2376 sc->mlx_state &= ~MLX_STATE_INTEN;
2378 case MLX_INTACTION_ENABLE:
2379 MLX_V3_PUT_IER(sc, 1);
2380 sc->mlx_state |= MLX_STATE_INTEN;
2385 /********************************************************************************
2386 * Poll for firmware error codes during controller initialisation.
2387 * Returns 0 if initialisation is complete, 1 if still in progress but no
2388 * error has been fetched, 2 if an error has been retrieved.
2391 mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2)
2394 static int initted = 0;
2398 /* first time around, clear any hardware completion status */
2400 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2405 /* init in progress? */
2406 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_INIT_BUSY))
2409 /* test error value */
2410 fwerror = MLX_V3_GET_FWERROR(sc);
2411 if (!(fwerror & MLX_V3_FWERROR_PEND))
2414 /* mask status pending bit, fetch status */
2415 *error = fwerror & ~MLX_V3_FWERROR_PEND;
2416 *param1 = MLX_V3_GET_FWERROR_PARAM1(sc);
2417 *param2 = MLX_V3_GET_FWERROR_PARAM2(sc);
2420 MLX_V3_PUT_FWERROR(sc, 0);
2425 /********************************************************************************
2426 ********************************************************************************
2427 Type 4 interface accessor methods
2428 ********************************************************************************
2429 ********************************************************************************/
2431 /********************************************************************************
2432 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2433 * (the controller is not ready to take a command).
2435 * Must be called at splbio or in a fashion that prevents reentry.
2438 mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2444 /* ready for our command? */
2445 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_FULL)) {
2446 /* copy mailbox data to window */
2447 for (i = 0; i < 13; i++)
2448 MLX_V4_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2450 /* memory-mapped controller, so issue a write barrier to ensure the mailbox is filled */
2451 bus_space_barrier(sc->mlx_btag, sc->mlx_bhandle, MLX_V4_MAILBOX, MLX_V4_MAILBOX_LENGTH,
2452 BUS_SPACE_BARRIER_WRITE);
2455 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_HWMBOX_CMD);
2461 /********************************************************************************
2462 * See if a command has been completed, if so acknowledge its completion
2463 * and recover the slot number and status code.
2465 * Must be called at splbio or in a fashion that prevents reentry.
2468 mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2473 /* status available? */
2474 if (MLX_V4_GET_ODBR(sc) & MLX_V4_ODB_HWSAVAIL) {
2475 *slot = MLX_V4_GET_STATUS_IDENT(sc); /* get command identifier */
2476 *status = MLX_V4_GET_STATUS(sc); /* get status */
2478 /* acknowledge completion */
2479 MLX_V4_PUT_ODBR(sc, MLX_V4_ODB_HWMBOX_ACK);
2480 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2486 /********************************************************************************
2487 * Enable/disable interrupts as requested.
2489 * Must be called at splbio or in a fashion that prevents reentry.
2492 mlx_v4_intaction(struct mlx_softc *sc, int action)
2497 case MLX_INTACTION_DISABLE:
2498 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK | MLX_V4_IER_DISINT);
2499 sc->mlx_state &= ~MLX_STATE_INTEN;
2501 case MLX_INTACTION_ENABLE:
2502 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK & ~MLX_V4_IER_DISINT);
2503 sc->mlx_state |= MLX_STATE_INTEN;
2508 /********************************************************************************
2509 * Poll for firmware error codes during controller initialisation.
2510 * Returns 0 if initialisation is complete, 1 if still in progress but no
2511 * error has been fetched, 2 if an error has been retrieved.
2514 mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2)
2517 static int initted = 0;
2521 /* first time around, clear any hardware completion status */
2523 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2528 /* init in progress? */
2529 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_INIT_BUSY))
2532 /* test error value */
2533 fwerror = MLX_V4_GET_FWERROR(sc);
2534 if (!(fwerror & MLX_V4_FWERROR_PEND))
2537 /* mask status pending bit, fetch status */
2538 *error = fwerror & ~MLX_V4_FWERROR_PEND;
2539 *param1 = MLX_V4_GET_FWERROR_PARAM1(sc);
2540 *param2 = MLX_V4_GET_FWERROR_PARAM2(sc);
2543 MLX_V4_PUT_FWERROR(sc, 0);
2548 /********************************************************************************
2549 ********************************************************************************
2550 Type 5 interface accessor methods
2551 ********************************************************************************
2552 ********************************************************************************/
2554 /********************************************************************************
2555 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2556 * (the controller is not ready to take a command).
2558 * Must be called at splbio or in a fashion that prevents reentry.
2561 mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2567 /* ready for our command? */
2568 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_EMPTY) {
2569 /* copy mailbox data to window */
2570 for (i = 0; i < 13; i++)
2571 MLX_V5_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2574 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_HWMBOX_CMD);
2580 /********************************************************************************
2581 * See if a command has been completed, if so acknowledge its completion
2582 * and recover the slot number and status code.
2584 * Must be called at splbio or in a fashion that prevents reentry.
2587 mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2592 /* status available? */
2593 if (MLX_V5_GET_ODBR(sc) & MLX_V5_ODB_HWSAVAIL) {
2594 *slot = MLX_V5_GET_STATUS_IDENT(sc); /* get command identifier */
2595 *status = MLX_V5_GET_STATUS(sc); /* get status */
2597 /* acknowledge completion */
2598 MLX_V5_PUT_ODBR(sc, MLX_V5_ODB_HWMBOX_ACK);
2599 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2605 /********************************************************************************
2606 * Enable/disable interrupts as requested.
2608 * Must be called at splbio or in a fashion that prevents reentry.
2611 mlx_v5_intaction(struct mlx_softc *sc, int action)
2616 case MLX_INTACTION_DISABLE:
2617 MLX_V5_PUT_IER(sc, 0xff & MLX_V5_IER_DISINT);
2618 sc->mlx_state &= ~MLX_STATE_INTEN;
2620 case MLX_INTACTION_ENABLE:
2621 MLX_V5_PUT_IER(sc, 0xff & ~MLX_V5_IER_DISINT);
2622 sc->mlx_state |= MLX_STATE_INTEN;
2627 /********************************************************************************
2628 * Poll for firmware error codes during controller initialisation.
2629 * Returns 0 if initialisation is complete, 1 if still in progress but no
2630 * error has been fetched, 2 if an error has been retrieved.
2633 mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2)
2636 static int initted = 0;
2640 /* first time around, clear any hardware completion status */
2642 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2647 /* init in progress? */
2648 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_INIT_DONE)
2651 /* test for error value */
2652 fwerror = MLX_V5_GET_FWERROR(sc);
2653 if (!(fwerror & MLX_V5_FWERROR_PEND))
2656 /* mask status pending bit, fetch status */
2657 *error = fwerror & ~MLX_V5_FWERROR_PEND;
2658 *param1 = MLX_V5_GET_FWERROR_PARAM1(sc);
2659 *param2 = MLX_V5_GET_FWERROR_PARAM2(sc);
2662 MLX_V5_PUT_FWERROR(sc, 0xff);
2667 /********************************************************************************
2668 ********************************************************************************
2670 ********************************************************************************
2671 ********************************************************************************/
2673 /********************************************************************************
2674 * Return a status message describing (mc)
2676 static char *mlx_status_messages[] = {
2677 "normal completion", /* 00 */
2678 "irrecoverable data error", /* 01 */
2679 "drive does not exist, or is offline", /* 02 */
2680 "attempt to write beyond end of drive", /* 03 */
2681 "bad data encountered", /* 04 */
2682 "invalid log entry request", /* 05 */
2683 "attempt to rebuild online drive", /* 06 */
2684 "new disk failed during rebuild", /* 07 */
2685 "invalid channel/target", /* 08 */
2686 "rebuild/check already in progress", /* 09 */
2687 "one or more disks are dead", /* 10 */
2688 "invalid or non-redundant drive", /* 11 */
2689 "channel is busy", /* 12 */
2690 "channel is not stopped", /* 13 */
2691 "rebuild successfully terminated", /* 14 */
2692 "unsupported command", /* 15 */
2693 "check condition received", /* 16 */
2694 "device is busy", /* 17 */
2695 "selection or command timeout", /* 18 */
2696 "command terminated abnormally", /* 19 */
2705 } mlx_messages[] = {
2706 {MLX_CMD_READSG, 0x0001, 1},
2707 {MLX_CMD_READSG, 0x0002, 1},
2708 {MLX_CMD_READSG, 0x0105, 3},
2709 {MLX_CMD_READSG, 0x010c, 4},
2710 {MLX_CMD_WRITESG, 0x0001, 1},
2711 {MLX_CMD_WRITESG, 0x0002, 1},
2712 {MLX_CMD_WRITESG, 0x0105, 3},
2713 {MLX_CMD_READSG_OLD, 0x0001, 1},
2714 {MLX_CMD_READSG_OLD, 0x0002, 1},
2715 {MLX_CMD_READSG_OLD, 0x0105, 3},
2716 {MLX_CMD_WRITESG_OLD, 0x0001, 1},
2717 {MLX_CMD_WRITESG_OLD, 0x0002, 1},
2718 {MLX_CMD_WRITESG_OLD, 0x0105, 3},
2719 {MLX_CMD_LOGOP, 0x0105, 5},
2720 {MLX_CMD_REBUILDASYNC, 0x0002, 6},
2721 {MLX_CMD_REBUILDASYNC, 0x0004, 7},
2722 {MLX_CMD_REBUILDASYNC, 0x0105, 8},
2723 {MLX_CMD_REBUILDASYNC, 0x0106, 9},
2724 {MLX_CMD_REBUILDASYNC, 0x0107, 14},
2725 {MLX_CMD_CHECKASYNC, 0x0002, 10},
2726 {MLX_CMD_CHECKASYNC, 0x0105, 11},
2727 {MLX_CMD_CHECKASYNC, 0x0106, 9},
2728 {MLX_CMD_STOPCHANNEL, 0x0106, 12},
2729 {MLX_CMD_STOPCHANNEL, 0x0105, 8},
2730 {MLX_CMD_STARTCHANNEL, 0x0005, 13},
2731 {MLX_CMD_STARTCHANNEL, 0x0105, 8},
2732 {MLX_CMD_DIRECT_CDB, 0x0002, 16},
2733 {MLX_CMD_DIRECT_CDB, 0x0008, 17},
2734 {MLX_CMD_DIRECT_CDB, 0x000e, 18},
2735 {MLX_CMD_DIRECT_CDB, 0x000f, 19},
2736 {MLX_CMD_DIRECT_CDB, 0x0105, 8},
2743 mlx_diagnose_command(struct mlx_command *mc)
2745 static char unkmsg[80];
2748 /* look up message in table */
2749 for (i = 0; mlx_messages[i].command != -1; i++)
2750 if (((mc->mc_mailbox[0] == mlx_messages[i].command) || (mlx_messages[i].command == 0)) &&
2751 (mc->mc_status == mlx_messages[i].status))
2752 return(mlx_status_messages[mlx_messages[i].msg]);
2754 ksprintf(unkmsg, "unknown response 0x%x for command 0x%x", (int)mc->mc_status, (int)mc->mc_mailbox[0]);
2758 /*******************************************************************************
2759 * Print a string describing the controller (sc)
2765 } mlx_controller_names[] = {
2780 mlx_describe_controller(struct mlx_softc *sc)
2782 static char buf[80];
2786 for (i = 0, model = NULL; mlx_controller_names[i].name != NULL; i++) {
2787 if ((sc->mlx_enq2->me_hardware_id & 0xff) == mlx_controller_names[i].hwid) {
2788 model = mlx_controller_names[i].name;
2792 if (model == NULL) {
2793 ksprintf(buf, " model 0x%x", sc->mlx_enq2->me_hardware_id & 0xff);
2796 device_printf(sc->mlx_dev, "DAC%s, %d channel%s, firmware %d.%02d-%c-%02d, %dMB RAM\n",
2798 sc->mlx_enq2->me_actual_channels,
2799 sc->mlx_enq2->me_actual_channels > 1 ? "s" : "",
2800 sc->mlx_enq2->me_firmware_id & 0xff,
2801 (sc->mlx_enq2->me_firmware_id >> 8) & 0xff,
2802 (sc->mlx_enq2->me_firmware_id >> 24) & 0xff,
2803 (sc->mlx_enq2->me_firmware_id >> 16) & 0xff,
2804 sc->mlx_enq2->me_mem_size / (1024 * 1024));
2807 device_printf(sc->mlx_dev, " Hardware ID 0x%08x\n", sc->mlx_enq2->me_hardware_id);
2808 device_printf(sc->mlx_dev, " Firmware ID 0x%08x\n", sc->mlx_enq2->me_firmware_id);
2809 device_printf(sc->mlx_dev, " Configured/Actual channels %d/%d\n", sc->mlx_enq2->me_configured_channels,
2810 sc->mlx_enq2->me_actual_channels);
2811 device_printf(sc->mlx_dev, " Max Targets %d\n", sc->mlx_enq2->me_max_targets);
2812 device_printf(sc->mlx_dev, " Max Tags %d\n", sc->mlx_enq2->me_max_tags);
2813 device_printf(sc->mlx_dev, " Max System Drives %d\n", sc->mlx_enq2->me_max_sys_drives);
2814 device_printf(sc->mlx_dev, " Max Arms %d\n", sc->mlx_enq2->me_max_arms);
2815 device_printf(sc->mlx_dev, " Max Spans %d\n", sc->mlx_enq2->me_max_spans);
2816 device_printf(sc->mlx_dev, " DRAM/cache/flash/NVRAM size %d/%d/%d/%d\n", sc->mlx_enq2->me_mem_size,
2817 sc->mlx_enq2->me_cache_size, sc->mlx_enq2->me_flash_size, sc->mlx_enq2->me_nvram_size);
2818 device_printf(sc->mlx_dev, " DRAM type %d\n", sc->mlx_enq2->me_mem_type);
2819 device_printf(sc->mlx_dev, " Clock Speed %dns\n", sc->mlx_enq2->me_clock_speed);
2820 device_printf(sc->mlx_dev, " Hardware Speed %dns\n", sc->mlx_enq2->me_hardware_speed);
2821 device_printf(sc->mlx_dev, " Max Commands %d\n", sc->mlx_enq2->me_max_commands);
2822 device_printf(sc->mlx_dev, " Max SG Entries %d\n", sc->mlx_enq2->me_max_sg);
2823 device_printf(sc->mlx_dev, " Max DP %d\n", sc->mlx_enq2->me_max_dp);
2824 device_printf(sc->mlx_dev, " Max IOD %d\n", sc->mlx_enq2->me_max_iod);
2825 device_printf(sc->mlx_dev, " Max Comb %d\n", sc->mlx_enq2->me_max_comb);
2826 device_printf(sc->mlx_dev, " Latency %ds\n", sc->mlx_enq2->me_latency);
2827 device_printf(sc->mlx_dev, " SCSI Timeout %ds\n", sc->mlx_enq2->me_scsi_timeout);
2828 device_printf(sc->mlx_dev, " Min Free Lines %d\n", sc->mlx_enq2->me_min_freelines);
2829 device_printf(sc->mlx_dev, " Rate Constant %d\n", sc->mlx_enq2->me_rate_const);
2830 device_printf(sc->mlx_dev, " MAXBLK %d\n", sc->mlx_enq2->me_maxblk);
2831 device_printf(sc->mlx_dev, " Blocking Factor %d sectors\n", sc->mlx_enq2->me_blocking_factor);
2832 device_printf(sc->mlx_dev, " Cache Line Size %d blocks\n", sc->mlx_enq2->me_cacheline);
2833 device_printf(sc->mlx_dev, " SCSI Capability %s%dMHz, %d bit\n",
2834 sc->mlx_enq2->me_scsi_cap & (1<<4) ? "differential " : "",
2835 (1 << ((sc->mlx_enq2->me_scsi_cap >> 2) & 3)) * 10,
2836 8 << (sc->mlx_enq2->me_scsi_cap & 0x3));
2837 device_printf(sc->mlx_dev, " Firmware Build Number %d\n", sc->mlx_enq2->me_firmware_build);
2838 device_printf(sc->mlx_dev, " Fault Management Type %d\n", sc->mlx_enq2->me_fault_mgmt_type);
2839 device_printf(sc->mlx_dev, " Features %b\n", sc->mlx_enq2->me_firmware_features,
2840 "\20\4Background Init\3Read Ahead\2MORE\1Cluster\n");
2845 /*******************************************************************************
2846 * Emit a string describing the firmware handshake status code, and return a flag
2847 * indicating whether the code represents a fatal error.
2849 * Error code interpretations are from the Linux driver, and don't directly match
2850 * the messages printed by Mylex's BIOS. This may change if documentation on the
2851 * codes is forthcoming.
2854 mlx_fw_message(struct mlx_softc *sc, int error, int param1, int param2)
2858 device_printf(sc->mlx_dev, "physical drive %d:%d not responding\n", param2, param1);
2861 /* we could be neater about this and give some indication when we receive more of them */
2862 if (!(sc->mlx_flags & MLX_SPINUP_REPORTED)) {
2863 device_printf(sc->mlx_dev, "spinning up drives...\n");
2864 sc->mlx_flags |= MLX_SPINUP_REPORTED;
2868 device_printf(sc->mlx_dev, "configuration checksum error\n");
2871 device_printf(sc->mlx_dev, "mirror race recovery failed\n");
2874 device_printf(sc->mlx_dev, "mirror race recovery in progress\n");
2877 device_printf(sc->mlx_dev, "physical drive %d:%d COD mismatch\n", param2, param1);
2880 device_printf(sc->mlx_dev, "logical drive installation aborted\n");
2883 device_printf(sc->mlx_dev, "mirror race on a critical system drive\n");
2886 device_printf(sc->mlx_dev, "new controller configuration found\n");
2889 device_printf(sc->mlx_dev, "FATAL MEMORY PARITY ERROR\n");
2892 device_printf(sc->mlx_dev, "unknown firmware initialisation error %02x:%02x:%02x\n", error, param1, param2);
2898 /********************************************************************************
2899 ********************************************************************************
2901 ********************************************************************************
2902 ********************************************************************************/
2904 /********************************************************************************
2905 * Find the disk whose unit number is (unit) on this controller
2907 static struct mlx_sysdrive *
2908 mlx_findunit(struct mlx_softc *sc, int unit)
2912 /* search system drives */
2913 for (i = 0; i < MLX_MAXDRIVES; i++) {
2914 /* is this one attached? */
2915 if (sc->mlx_sysdrive[i].ms_disk != 0) {
2916 /* is this the one? */
2917 if (unit == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
2918 return(&sc->mlx_sysdrive[i]);