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.18 2006/04/30 17:22:16 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>
44 #include <sys/thread2.h>
46 #include <machine/resource.h>
47 #include <machine/bus_memio.h>
48 #include <machine/bus_pio.h>
49 #include <machine/bus.h>
50 #include <machine/clock.h>
53 #include "mlx_compat.h"
58 #define MLX_CDEV_MAJOR 130
60 static struct cdevsw mlx_cdevsw = {
62 /* maj */ MLX_CDEV_MAJOR,
68 /* close */ mlx_close,
71 /* ioctl */ mlx_ioctl,
74 /* strategy */ nostrategy,
79 devclass_t mlx_devclass;
82 * Per-interface accessor methods
84 static int mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
85 static int mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
86 static void mlx_v3_intaction(struct mlx_softc *sc, int action);
87 static int mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2);
89 static int mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
90 static int mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
91 static void mlx_v4_intaction(struct mlx_softc *sc, int action);
92 static int mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2);
94 static int mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
95 static int mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
96 static void mlx_v5_intaction(struct mlx_softc *sc, int action);
97 static int mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2);
102 static void mlx_periodic(void *data);
103 static void mlx_periodic_enquiry(struct mlx_command *mc);
104 static void mlx_periodic_eventlog_poll(struct mlx_softc *sc);
105 static void mlx_periodic_eventlog_respond(struct mlx_command *mc);
106 static void mlx_periodic_rebuild(struct mlx_command *mc);
111 static void mlx_pause_action(struct mlx_softc *sc);
112 static void mlx_pause_done(struct mlx_command *mc);
115 * Command submission.
117 static void *mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize,
118 void (*complete)(struct mlx_command *mc));
119 static int mlx_flush(struct mlx_softc *sc);
120 static int mlx_check(struct mlx_softc *sc, int drive);
121 static int mlx_rebuild(struct mlx_softc *sc, int channel, int target);
122 static int mlx_wait_command(struct mlx_command *mc);
123 static int mlx_poll_command(struct mlx_command *mc);
124 static void mlx_startio(struct mlx_softc *sc);
125 static void mlx_completeio(struct mlx_command *mc);
126 static int mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu);
129 * Command buffer allocation.
131 static struct mlx_command *mlx_alloccmd(struct mlx_softc *sc);
132 static void mlx_releasecmd(struct mlx_command *mc);
133 static void mlx_freecmd(struct mlx_command *mc);
136 * Command management.
138 static int mlx_getslot(struct mlx_command *mc);
139 static void mlx_mapcmd(struct mlx_command *mc);
140 static void mlx_unmapcmd(struct mlx_command *mc);
141 static int mlx_start(struct mlx_command *mc);
142 static int mlx_done(struct mlx_softc *sc);
143 static void mlx_complete(struct mlx_softc *sc);
148 static char *mlx_diagnose_command(struct mlx_command *mc);
149 static void mlx_describe_controller(struct mlx_softc *sc);
150 static int mlx_fw_message(struct mlx_softc *sc, int status, int param1, int param2);
155 static struct mlx_sysdrive *mlx_findunit(struct mlx_softc *sc, int unit);
157 /********************************************************************************
158 ********************************************************************************
160 ********************************************************************************
161 ********************************************************************************/
163 /********************************************************************************
164 * Free all of the resources associated with (sc)
166 * Should not be called if the controller is active.
169 mlx_free(struct mlx_softc *sc)
171 struct mlx_command *mc;
175 /* cancel status timeout */
176 callout_stop(&sc->mlx_timeout);
178 /* throw away any command buffers */
179 while ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL) {
180 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
184 /* destroy data-transfer DMA tag */
185 if (sc->mlx_buffer_dmat)
186 bus_dma_tag_destroy(sc->mlx_buffer_dmat);
188 /* free and destroy DMA memory and tag for s/g lists */
190 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
192 bus_dma_tag_destroy(sc->mlx_sg_dmat);
194 /* disconnect the interrupt handler */
196 bus_teardown_intr(sc->mlx_dev, sc->mlx_irq, sc->mlx_intr);
197 if (sc->mlx_irq != NULL)
198 bus_release_resource(sc->mlx_dev, SYS_RES_IRQ, 0, sc->mlx_irq);
200 /* destroy the parent DMA tag */
201 if (sc->mlx_parent_dmat)
202 bus_dma_tag_destroy(sc->mlx_parent_dmat);
204 /* release the register window mapping */
205 if (sc->mlx_mem != NULL)
206 bus_release_resource(sc->mlx_dev, sc->mlx_mem_type, sc->mlx_mem_rid, sc->mlx_mem);
208 /* free controller enquiry data */
209 if (sc->mlx_enq2 != NULL)
210 free(sc->mlx_enq2, M_DEVBUF);
212 cdevsw_remove(&mlx_cdevsw, -1, device_get_unit(sc->mlx_dev));
215 /********************************************************************************
216 * Map the scatter/gather table into bus space
219 mlx_dma_map_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
221 struct mlx_softc *sc = (struct mlx_softc *)arg;
225 /* save base of s/g table's address in bus space */
226 sc->mlx_sgbusaddr = segs->ds_addr;
230 mlx_sglist_map(struct mlx_softc *sc)
237 /* destroy any existing mappings */
239 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
241 bus_dma_tag_destroy(sc->mlx_sg_dmat);
244 * Create a single tag describing a region large enough to hold all of
245 * the s/g lists we will need. If we're called early on, we don't know how
246 * many commands we're going to be asked to support, so only allocate enough
249 if (sc->mlx_enq2 == NULL) {
252 ncmd = sc->mlx_enq2->me_max_commands;
254 segsize = sizeof(struct mlx_sgentry) * MLX_NSEG * ncmd;
255 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */
256 1, 0, /* alignment, boundary */
257 BUS_SPACE_MAXADDR, /* lowaddr */
258 BUS_SPACE_MAXADDR, /* highaddr */
259 NULL, NULL, /* filter, filterarg */
260 segsize, 1, /* maxsize, nsegments */
261 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
265 device_printf(sc->mlx_dev, "can't allocate scatter/gather DMA tag\n");
270 * Allocate enough s/g maps for all commands and permanently map them into
271 * controller-visible space.
273 * XXX this assumes we can get enough space for all the s/g maps in one
274 * contiguous slab. We may need to switch to a more complex arrangement where
275 * we allocate in smaller chunks and keep a lookup table from slot to bus address.
277 error = bus_dmamem_alloc(sc->mlx_sg_dmat, (void **)&sc->mlx_sgtable, BUS_DMA_NOWAIT, &sc->mlx_sg_dmamap);
279 device_printf(sc->mlx_dev, "can't allocate s/g table\n");
282 bus_dmamap_load(sc->mlx_sg_dmat, sc->mlx_sg_dmamap, sc->mlx_sgtable, segsize, mlx_dma_map_sg, sc, 0);
286 /********************************************************************************
287 * Initialise the controller and softc
290 mlx_attach(struct mlx_softc *sc)
292 struct mlx_enquiry_old *meo;
293 int rid, error, fwminor, hscode, hserror, hsparam1, hsparam2, hsmsg;
296 callout_init(&sc->mlx_timeout);
299 * Initialise per-controller queues.
301 TAILQ_INIT(&sc->mlx_work);
302 TAILQ_INIT(&sc->mlx_freecmds);
303 bioq_init(&sc->mlx_bioq);
306 * Select accessor methods based on controller interface type.
308 switch(sc->mlx_iftype) {
311 sc->mlx_tryqueue = mlx_v3_tryqueue;
312 sc->mlx_findcomplete = mlx_v3_findcomplete;
313 sc->mlx_intaction = mlx_v3_intaction;
314 sc->mlx_fw_handshake = mlx_v3_fw_handshake;
317 sc->mlx_tryqueue = mlx_v4_tryqueue;
318 sc->mlx_findcomplete = mlx_v4_findcomplete;
319 sc->mlx_intaction = mlx_v4_intaction;
320 sc->mlx_fw_handshake = mlx_v4_fw_handshake;
323 sc->mlx_tryqueue = mlx_v5_tryqueue;
324 sc->mlx_findcomplete = mlx_v5_findcomplete;
325 sc->mlx_intaction = mlx_v5_intaction;
326 sc->mlx_fw_handshake = mlx_v5_fw_handshake;
330 return(ENXIO); /* should never happen */
333 /* disable interrupts before we start talking to the controller */
334 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
337 * Wait for the controller to come ready, handshake with the firmware if required.
338 * This is typically only necessary on platforms where the controller BIOS does not
343 while ((hscode = sc->mlx_fw_handshake(sc, &hserror, &hsparam1, &hsparam2)) != 0) {
344 /* report first time around... */
346 device_printf(sc->mlx_dev, "controller initialisation in progress...\n");
349 /* did we get a real message? */
351 hscode = mlx_fw_message(sc, hserror, hsparam1, hsparam2);
352 /* fatal initialisation error? */
360 device_printf(sc->mlx_dev, "initialisation complete.\n");
363 * Allocate and connect our interrupt.
366 sc->mlx_irq = bus_alloc_resource(sc->mlx_dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
367 if (sc->mlx_irq == NULL) {
368 device_printf(sc->mlx_dev, "can't allocate interrupt\n");
372 error = bus_setup_intr(sc->mlx_dev, sc->mlx_irq,
374 &sc->mlx_intr, NULL);
376 device_printf(sc->mlx_dev, "can't set up interrupt\n");
382 * Create DMA tag for mapping buffers into controller-addressable space.
384 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */
385 1, 0, /* alignment, boundary */
386 BUS_SPACE_MAXADDR, /* lowaddr */
387 BUS_SPACE_MAXADDR, /* highaddr */
388 NULL, NULL, /* filter, filterarg */
389 MAXBSIZE, MLX_NSEG, /* maxsize, nsegments */
390 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
392 &sc->mlx_buffer_dmat);
394 device_printf(sc->mlx_dev, "can't allocate buffer DMA tag\n");
400 * Create some initial scatter/gather mappings so we can run the probe commands.
402 error = mlx_sglist_map(sc);
404 device_printf(sc->mlx_dev, "can't make initial s/g list mapping\n");
410 * We don't (yet) know where the event log is up to.
412 sc->mlx_currevent = -1;
415 * Obtain controller feature information
417 if ((sc->mlx_enq2 = mlx_enquire(sc, MLX_CMD_ENQUIRY2, sizeof(struct mlx_enquiry2), NULL)) == NULL) {
418 device_printf(sc->mlx_dev, "ENQUIRY2 failed\n");
424 * Do quirk/feature related things.
426 fwminor = (sc->mlx_enq2->me_firmware_id >> 8) & 0xff;
427 switch(sc->mlx_iftype) {
429 /* These controllers don't report the firmware version in the ENQUIRY2 response */
430 if ((meo = mlx_enquire(sc, MLX_CMD_ENQUIRY_OLD, sizeof(struct mlx_enquiry_old), NULL)) == NULL) {
431 device_printf(sc->mlx_dev, "ENQUIRY_OLD failed\n");
435 sc->mlx_enq2->me_firmware_id = ('0' << 24) | (0 << 16) | (meo->me_fwminor << 8) | meo->me_fwmajor;
438 /* XXX require 2.42 or better (PCI) or 2.14 or better (EISA) */
439 if (meo->me_fwminor < 42) {
440 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
441 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 2.42 or later\n");
445 /* XXX certify 3.52? */
447 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
448 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 3.51 or later\n");
452 /* XXX certify firmware versions? */
454 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
455 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 4.06 or later\n");
460 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
461 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 5.07 or later\n");
466 return(ENXIO); /* should never happen */
470 * Create the final scatter/gather mappings now that we have characterised the controller.
472 error = mlx_sglist_map(sc);
474 device_printf(sc->mlx_dev, "can't make final s/g list mapping\n");
480 * No user-requested background operation is in progress.
482 sc->mlx_background = 0;
483 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
486 * Create the control device.
488 cdevsw_add(&mlx_cdevsw, -1, device_get_unit(sc->mlx_dev));
489 make_dev(&mlx_cdevsw, device_get_unit(sc->mlx_dev),
490 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
491 "mlx%d", device_get_unit(sc->mlx_dev));
494 * Start the timeout routine.
496 callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
498 /* print a little information about the controller */
499 mlx_describe_controller(sc);
504 /********************************************************************************
505 * Locate disk resources and attach children to them.
508 mlx_startup(struct mlx_softc *sc)
510 struct mlx_enq_sys_drive *mes;
511 struct mlx_sysdrive *dr;
517 * Scan all the system drives and attach children for those that
518 * don't currently have them.
520 mes = mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(*mes) * MLX_MAXDRIVES, NULL);
522 device_printf(sc->mlx_dev, "error fetching drive status\n");
526 /* iterate over drives returned */
527 for (i = 0, dr = &sc->mlx_sysdrive[0];
528 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
530 /* are we already attached to this drive? */
531 if (dr->ms_disk == 0) {
532 /* pick up drive information */
533 dr->ms_size = mes[i].sd_size;
534 dr->ms_raidlevel = mes[i].sd_raidlevel & 0xf;
535 dr->ms_state = mes[i].sd_state;
537 /* generate geometry information */
538 if (sc->mlx_geom == MLX_GEOM_128_32) {
541 dr->ms_cylinders = dr->ms_size / (128 * 32);
542 } else { /* MLX_GEOM_255/63 */
545 dr->ms_cylinders = dr->ms_size / (255 * 63);
547 dr->ms_disk = device_add_child(sc->mlx_dev, /*"mlxd"*/NULL, -1);
548 if (dr->ms_disk == 0)
549 device_printf(sc->mlx_dev, "device_add_child failed\n");
550 device_set_ivars(dr->ms_disk, dr);
554 if ((error = bus_generic_attach(sc->mlx_dev)) != 0)
555 device_printf(sc->mlx_dev, "bus_generic_attach returned %d", error);
557 /* mark controller back up */
558 sc->mlx_state &= ~MLX_STATE_SHUTDOWN;
560 /* enable interrupts */
561 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
564 /********************************************************************************
565 * Disconnect from the controller completely, in preparation for unload.
568 mlx_detach(device_t dev)
570 struct mlx_softc *sc = device_get_softc(dev);
571 struct mlxd_softc *mlxd;
578 if (sc->mlx_state & MLX_STATE_OPEN)
581 for (i = 0; i < MLX_MAXDRIVES; i++) {
582 if (sc->mlx_sysdrive[i].ms_disk != 0) {
583 mlxd = device_get_softc(sc->mlx_sysdrive[i].ms_disk);
584 if (mlxd->mlxd_flags & MLXD_OPEN) { /* drive is mounted, abort detach */
585 device_printf(sc->mlx_sysdrive[i].ms_disk, "still open, can't detach\n");
590 if ((error = mlx_shutdown(dev)))
601 /********************************************************************************
602 * Bring the controller down to a dormant state and detach all child devices.
604 * This function is called before detach, system shutdown, or before performing
605 * an operation which may add or delete system disks. (Call mlx_startup to
606 * resume normal operation.)
608 * Note that we can assume that the bioq on the controller is empty, as we won't
609 * allow shutdown if any device is open.
612 mlx_shutdown(device_t dev)
614 struct mlx_softc *sc = device_get_softc(dev);
622 sc->mlx_state |= MLX_STATE_SHUTDOWN;
623 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
625 /* flush controller */
626 device_printf(sc->mlx_dev, "flushing cache...");
633 /* delete all our child devices */
634 for (i = 0; i < MLX_MAXDRIVES; i++) {
635 if (sc->mlx_sysdrive[i].ms_disk != 0) {
636 if ((error = device_delete_child(sc->mlx_dev, sc->mlx_sysdrive[i].ms_disk)) != 0)
638 sc->mlx_sysdrive[i].ms_disk = 0;
647 /********************************************************************************
648 * Bring the controller to a quiescent state, ready for system suspend.
651 mlx_suspend(device_t dev)
653 struct mlx_softc *sc = device_get_softc(dev);
658 sc->mlx_state |= MLX_STATE_SUSPEND;
660 /* flush controller */
661 device_printf(sc->mlx_dev, "flushing cache...");
662 printf("%s\n", mlx_flush(sc) ? "failed" : "done");
664 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
670 /********************************************************************************
671 * Bring the controller back to a state ready for operation.
674 mlx_resume(device_t dev)
676 struct mlx_softc *sc = device_get_softc(dev);
680 sc->mlx_state &= ~MLX_STATE_SUSPEND;
681 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
686 /*******************************************************************************
687 * Take an interrupt, or be poked by other code to look for interrupt-worthy
693 struct mlx_softc *sc = (struct mlx_softc *)arg;
697 /* collect finished commands, queue anything waiting */
701 /*******************************************************************************
702 * Receive a buf structure from a child device and queue it on a particular
703 * disk resource, then poke the disk resource to start as much work as it can.
706 mlx_submit_bio(struct mlx_softc *sc, struct bio *bio)
711 bioq_insert_tail(&sc->mlx_bioq, bio);
718 /********************************************************************************
719 * Accept an open operation on the control device.
722 mlx_open(dev_t dev, int flags, int fmt, d_thread_t *td)
724 int unit = minor(dev);
725 struct mlx_softc *sc = devclass_get_softc(mlx_devclass, unit);
727 sc->mlx_state |= MLX_STATE_OPEN;
731 /********************************************************************************
732 * Accept the last close on the control device.
735 mlx_close(dev_t dev, int flags, int fmt, d_thread_t *td)
737 int unit = minor(dev);
738 struct mlx_softc *sc = devclass_get_softc(mlx_devclass, unit);
740 sc->mlx_state &= ~MLX_STATE_OPEN;
744 /********************************************************************************
745 * Handle controller-specific control operations.
748 mlx_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *td)
750 int unit = minor(dev);
751 struct mlx_softc *sc = devclass_get_softc(mlx_devclass, unit);
752 struct mlx_rebuild_request *rb = (struct mlx_rebuild_request *)addr;
753 struct mlx_rebuild_status *rs = (struct mlx_rebuild_status *)addr;
754 int *arg = (int *)addr;
755 struct mlx_pause *mp;
756 struct mlx_sysdrive *dr;
757 struct mlxd_softc *mlxd;
762 * Enumerate connected system drives; returns the first system drive's
763 * unit number if *arg is -1, or the next unit after *arg if it's
764 * a valid unit on this controller.
767 /* search system drives */
768 for (i = 0; i < MLX_MAXDRIVES; i++) {
769 /* is this one attached? */
770 if (sc->mlx_sysdrive[i].ms_disk != 0) {
771 /* looking for the next one we come across? */
773 *arg = device_get_unit(sc->mlx_sysdrive[0].ms_disk);
776 /* we want the one after this one */
777 if (*arg == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
784 * Scan the controller to see whether new drives have appeared.
786 case MLX_RESCAN_DRIVES:
791 * Disconnect from the specified drive; it may be about to go
794 case MLX_DETACH_DRIVE: /* detach one drive */
796 if (((dr = mlx_findunit(sc, *arg)) == NULL) ||
797 ((mlxd = device_get_softc(dr->ms_disk)) == NULL))
800 device_printf(dr->ms_disk, "detaching...");
802 if (mlxd->mlxd_flags & MLXD_OPEN) {
807 /* flush controller */
814 if ((error = device_delete_child(sc->mlx_dev, dr->ms_disk)) != 0)
827 * Pause one or more SCSI channels for a period of time, to assist
828 * in the process of hot-swapping devices.
830 * Note that at least the 3.51 firmware on the DAC960PL doesn't seem
833 case MLX_PAUSE_CHANNEL: /* schedule a channel pause */
834 /* Does this command work on this firmware? */
835 if (!(sc->mlx_feature & MLX_FEAT_PAUSEWORKS))
838 mp = (struct mlx_pause *)addr;
839 if ((mp->mp_which == MLX_PAUSE_CANCEL) && (sc->mlx_pause.mp_when != 0)) {
840 /* cancel a pending pause operation */
841 sc->mlx_pause.mp_which = 0;
843 /* fix for legal channels */
844 mp->mp_which &= ((1 << sc->mlx_enq2->me_actual_channels) -1);
845 /* check time values */
846 if ((mp->mp_when < 0) || (mp->mp_when > 3600))
848 if ((mp->mp_howlong < 1) || (mp->mp_howlong > (0xf * 30)))
851 /* check for a pause currently running */
852 if ((sc->mlx_pause.mp_which != 0) && (sc->mlx_pause.mp_when == 0))
855 /* looks ok, go with it */
856 sc->mlx_pause.mp_which = mp->mp_which;
857 sc->mlx_pause.mp_when = time_second + mp->mp_when;
858 sc->mlx_pause.mp_howlong = sc->mlx_pause.mp_when + mp->mp_howlong;
863 * Accept a command passthrough-style.
866 return(mlx_user_command(sc, (struct mlx_usercommand *)addr));
869 * Start a rebuild on a given SCSI disk
871 case MLX_REBUILDASYNC:
872 if (sc->mlx_background != 0) {
873 rb->rr_status = 0x0106;
876 rb->rr_status = mlx_rebuild(sc, rb->rr_channel, rb->rr_target);
877 switch (rb->rr_status) {
882 error = ENOMEM; /* couldn't set up the command */
901 sc->mlx_background = MLX_BACKGROUND_REBUILD;
905 * Get the status of the current rebuild or consistency check.
907 case MLX_REBUILDSTAT:
908 *rs = sc->mlx_rebuildstat;
912 * Return the per-controller system drive number matching the
913 * disk device number in (arg), if it happens to belong to us.
915 case MLX_GET_SYSDRIVE:
917 mlxd = (struct mlxd_softc *)devclass_get_softc(mlxd_devclass, *arg);
918 if ((mlxd != NULL) && (mlxd->mlxd_drive >= sc->mlx_sysdrive) &&
919 (mlxd->mlxd_drive < (sc->mlx_sysdrive + MLX_MAXDRIVES))) {
921 *arg = mlxd->mlxd_drive - sc->mlx_sysdrive;
930 /********************************************************************************
931 * Handle operations requested by a System Drive connected to this controller.
934 mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd,
935 caddr_t addr, int32_t flag, d_thread_t *td)
937 int *arg = (int *)addr;
942 * Return the current status of this drive.
945 *arg = drive->ms_state;
949 * Start a background consistency check on this drive.
951 case MLXD_CHECKASYNC: /* start a background consistency check */
952 if (sc->mlx_background != 0) {
956 result = mlx_check(sc, drive - &sc->mlx_sysdrive[0]);
962 error = ENOMEM; /* couldn't set up the command */
978 sc->mlx_background = MLX_BACKGROUND_CHECK;
987 /********************************************************************************
988 ********************************************************************************
990 ********************************************************************************
991 ********************************************************************************/
993 /********************************************************************************
994 * Fire off commands to periodically check the status of connected drives.
997 mlx_periodic(void *data)
999 struct mlx_softc *sc = (struct mlx_softc *)data;
1006 if ((sc->mlx_pause.mp_which != 0) &&
1007 (sc->mlx_pause.mp_when > 0) &&
1008 (time_second >= sc->mlx_pause.mp_when)){
1010 mlx_pause_action(sc); /* pause is running */
1011 sc->mlx_pause.mp_when = 0;
1015 * Bus pause still running?
1017 } else if ((sc->mlx_pause.mp_which != 0) &&
1018 (sc->mlx_pause.mp_when == 0)) {
1020 /* time to stop bus pause? */
1021 if (time_second >= sc->mlx_pause.mp_howlong) {
1022 mlx_pause_action(sc);
1023 sc->mlx_pause.mp_which = 0; /* pause is complete */
1026 sysbeep((time_second % 5) * 100 + 500, hz/8);
1030 * Run normal periodic activities?
1032 } else if (time_second > (sc->mlx_lastpoll + 10)) {
1033 sc->mlx_lastpoll = time_second;
1036 * Check controller status.
1038 * XXX Note that this may not actually launch a command in situations of high load.
1040 mlx_enquire(sc, (sc->mlx_iftype == MLX_IFTYPE_2) ? MLX_CMD_ENQUIRY_OLD : MLX_CMD_ENQUIRY,
1041 imax(sizeof(struct mlx_enquiry), sizeof(struct mlx_enquiry_old)), mlx_periodic_enquiry);
1044 * Check system drive status.
1046 * XXX This might be better left to event-driven detection, eg. I/O to an offline
1047 * drive will detect it's offline, rebuilds etc. should detect the drive is back
1050 mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(struct mlx_enq_sys_drive) * MLX_MAXDRIVES,
1051 mlx_periodic_enquiry);
1055 /* get drive rebuild/check status */
1056 /* XXX should check sc->mlx_background if this is only valid while in progress */
1057 mlx_enquire(sc, MLX_CMD_REBUILDSTAT, sizeof(struct mlx_rebuild_stat), mlx_periodic_rebuild);
1059 /* deal with possibly-missed interrupts and timed-out commands */
1062 /* reschedule another poll next second or so */
1063 callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
1066 /********************************************************************************
1067 * Handle the result of an ENQUIRY command instigated by periodic status polling.
1070 mlx_periodic_enquiry(struct mlx_command *mc)
1072 struct mlx_softc *sc = mc->mc_sc;
1076 /* Command completed OK? */
1077 if (mc->mc_status != 0) {
1078 device_printf(sc->mlx_dev, "periodic enquiry failed - %s\n", mlx_diagnose_command(mc));
1082 /* respond to command */
1083 switch(mc->mc_mailbox[0]) {
1085 * This is currently a bit fruitless, as we don't know how to extract the eventlog
1088 case MLX_CMD_ENQUIRY_OLD:
1090 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data;
1091 struct mlx_enquiry_old *meo = (struct mlx_enquiry_old *)mc->mc_data;
1094 /* convert data in-place to new format */
1095 for (i = (sizeof(me->me_dead) / sizeof(me->me_dead[0])) - 1; i >= 0; i--) {
1096 me->me_dead[i].dd_chan = meo->me_dead[i].dd_chan;
1097 me->me_dead[i].dd_targ = meo->me_dead[i].dd_targ;
1099 me->me_misc_flags = 0;
1100 me->me_rebuild_count = meo->me_rebuild_count;
1101 me->me_dead_count = meo->me_dead_count;
1102 me->me_critical_sd_count = meo->me_critical_sd_count;
1103 me->me_event_log_seq_num = 0;
1104 me->me_offline_sd_count = meo->me_offline_sd_count;
1105 me->me_max_commands = meo->me_max_commands;
1106 me->me_rebuild_flag = meo->me_rebuild_flag;
1107 me->me_fwmajor = meo->me_fwmajor;
1108 me->me_fwminor = meo->me_fwminor;
1109 me->me_status_flags = meo->me_status_flags;
1110 me->me_flash_age = meo->me_flash_age;
1111 for (i = (sizeof(me->me_drvsize) / sizeof(me->me_drvsize[0])) - 1; i >= 0; i--) {
1112 if (i > ((sizeof(meo->me_drvsize) / sizeof(meo->me_drvsize[0])) - 1)) {
1113 me->me_drvsize[i] = 0; /* drive beyond supported range */
1115 me->me_drvsize[i] = meo->me_drvsize[i];
1118 me->me_num_sys_drvs = meo->me_num_sys_drvs;
1123 * Generic controller status update. We could do more with this than just
1124 * checking the event log.
1126 case MLX_CMD_ENQUIRY:
1128 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data;
1130 if (sc->mlx_currevent == -1) {
1131 /* initialise our view of the event log */
1132 sc->mlx_currevent = sc->mlx_lastevent = me->me_event_log_seq_num;
1133 } else if ((me->me_event_log_seq_num != sc->mlx_lastevent) && !(sc->mlx_flags & MLX_EVENTLOG_BUSY)) {
1134 /* record where current events are up to */
1135 sc->mlx_currevent = me->me_event_log_seq_num;
1136 debug(1, "event log pointer was %d, now %d\n", sc->mlx_lastevent, sc->mlx_currevent);
1138 /* mark the event log as busy */
1139 atomic_set_int(&sc->mlx_flags, MLX_EVENTLOG_BUSY);
1141 /* drain new eventlog entries */
1142 mlx_periodic_eventlog_poll(sc);
1146 case MLX_CMD_ENQSYSDRIVE:
1148 struct mlx_enq_sys_drive *mes = (struct mlx_enq_sys_drive *)mc->mc_data;
1149 struct mlx_sysdrive *dr;
1152 for (i = 0, dr = &sc->mlx_sysdrive[0];
1153 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
1156 /* has state been changed by controller? */
1157 if (dr->ms_state != mes[i].sd_state) {
1158 switch(mes[i].sd_state) {
1159 case MLX_SYSD_OFFLINE:
1160 device_printf(dr->ms_disk, "drive offline\n");
1162 case MLX_SYSD_ONLINE:
1163 device_printf(dr->ms_disk, "drive online\n");
1165 case MLX_SYSD_CRITICAL:
1166 device_printf(dr->ms_disk, "drive critical\n");
1169 /* save new state */
1170 dr->ms_state = mes[i].sd_state;
1176 device_printf(sc->mlx_dev, "%s: unknown command 0x%x", __func__, mc->mc_mailbox[0]);
1181 free(mc->mc_data, M_DEVBUF);
1185 /********************************************************************************
1186 * Instigate a poll for one event log message on (sc).
1187 * We only poll for one message at a time, to keep our command usage down.
1190 mlx_periodic_eventlog_poll(struct mlx_softc *sc)
1192 struct mlx_command *mc;
1193 void *result = NULL;
1198 /* get ourselves a command buffer */
1200 if ((mc = mlx_alloccmd(sc)) == NULL)
1203 * allocate the response structure - sizeof(struct mlx_eventlog_entry)?
1204 * Called from timeout - use M_NOWAIT (repoll later on failure?)
1206 if ((result = malloc(1024, M_DEVBUF, M_NOWAIT)) == NULL)
1208 /* get a command slot */
1209 if (mlx_getslot(mc))
1212 /* map the command so the controller can see it */
1213 mc->mc_data = result;
1214 mc->mc_length = /*sizeof(struct mlx_eventlog_entry)*/1024;
1217 /* build the command to get one entry */
1218 mlx_make_type3(mc, MLX_CMD_LOGOP, MLX_LOGOP_GET, 1, sc->mlx_lastevent, 0, 0, mc->mc_dataphys, 0);
1219 mc->mc_complete = mlx_periodic_eventlog_respond;
1220 mc->mc_private = mc;
1222 /* start the command */
1223 if ((error = mlx_start(mc)) != 0)
1226 error = 0; /* success */
1232 free(result, M_DEVBUF);
1236 /********************************************************************************
1237 * Handle the result of polling for a log message, generate diagnostic output.
1238 * If this wasn't the last message waiting for us, we'll go collect another.
1240 static char *mlx_sense_messages[] = {
1241 "because write recovery failed",
1242 "because of SCSI bus reset failure",
1243 "because of double check condition",
1244 "because it was removed",
1245 "because of gross error on SCSI chip",
1246 "because of bad tag returned from drive",
1247 "because of timeout on SCSI command",
1248 "because of reset SCSI command issued from system",
1249 "because busy or parity error count exceeded limit",
1250 "because of 'kill drive' command from system",
1251 "because of selection timeout",
1252 "due to SCSI phase sequence error",
1253 "due to unknown status"
1257 mlx_periodic_eventlog_respond(struct mlx_command *mc)
1259 struct mlx_softc *sc = mc->mc_sc;
1260 struct mlx_eventlog_entry *el = (struct mlx_eventlog_entry *)mc->mc_data;
1265 sc->mlx_lastevent++; /* next message... */
1266 if (mc->mc_status == 0) {
1268 /* handle event log message */
1269 switch(el->el_type) {
1271 * This is the only sort of message we understand at the moment.
1272 * The tests here are probably incomplete.
1274 case MLX_LOGMSG_SENSE: /* sense data */
1275 /* Mylex vendor-specific message indicating a drive was killed? */
1276 if ((el->el_sensekey == 9) &&
1277 (el->el_asc == 0x80)) {
1278 if (el->el_asq < (sizeof(mlx_sense_messages) / sizeof(mlx_sense_messages[0]))) {
1279 reason = mlx_sense_messages[el->el_asq];
1281 reason = "for unknown reason";
1283 device_printf(sc->mlx_dev, "physical drive %d:%d killed %s\n",
1284 el->el_channel, el->el_target, reason);
1286 /* SCSI drive was reset? */
1287 if ((el->el_sensekey == 6) && (el->el_asc == 0x29)) {
1288 device_printf(sc->mlx_dev, "physical drive %d:%d reset\n",
1289 el->el_channel, el->el_target);
1291 /* SCSI drive error? */
1292 if (!((el->el_sensekey == 0) ||
1293 ((el->el_sensekey == 2) &&
1294 (el->el_asc == 0x04) &&
1295 ((el->el_asq == 0x01) ||
1296 (el->el_asq == 0x02))))) {
1297 device_printf(sc->mlx_dev, "physical drive %d:%d error log: sense = %d asc = %x asq = %x\n",
1298 el->el_channel, el->el_target, el->el_sensekey, el->el_asc, el->el_asq);
1299 device_printf(sc->mlx_dev, " info %4D csi %4D\n", el->el_information, ":", el->el_csi, ":");
1304 device_printf(sc->mlx_dev, "unknown log message type 0x%x\n", el->el_type);
1308 device_printf(sc->mlx_dev, "error reading message log - %s\n", mlx_diagnose_command(mc));
1309 /* give up on all the outstanding messages, as we may have come unsynched */
1310 sc->mlx_lastevent = sc->mlx_currevent;
1313 /* dispose of command and data */
1314 free(mc->mc_data, M_DEVBUF);
1317 /* is there another message to obtain? */
1318 if (sc->mlx_lastevent != sc->mlx_currevent) {
1319 mlx_periodic_eventlog_poll(sc);
1321 /* clear log-busy status */
1322 atomic_clear_int(&sc->mlx_flags, MLX_EVENTLOG_BUSY);
1326 /********************************************************************************
1327 * Handle check/rebuild operations in progress.
1330 mlx_periodic_rebuild(struct mlx_command *mc)
1332 struct mlx_softc *sc = mc->mc_sc;
1333 struct mlx_rebuild_status *mr = (struct mlx_rebuild_status *)mc->mc_data;
1335 switch(mc->mc_status) {
1336 case 0: /* operation running, update stats */
1337 sc->mlx_rebuildstat = *mr;
1339 /* spontaneous rebuild/check? */
1340 if (sc->mlx_background == 0) {
1341 sc->mlx_background = MLX_BACKGROUND_SPONTANEOUS;
1342 device_printf(sc->mlx_dev, "background check/rebuild operation started\n");
1346 case 0x0105: /* nothing running, finalise stats and report */
1347 switch(sc->mlx_background) {
1348 case MLX_BACKGROUND_CHECK:
1349 device_printf(sc->mlx_dev, "consistency check completed\n"); /* XXX print drive? */
1351 case MLX_BACKGROUND_REBUILD:
1352 device_printf(sc->mlx_dev, "drive rebuild completed\n"); /* XXX print channel/target? */
1354 case MLX_BACKGROUND_SPONTANEOUS:
1356 /* if we have previously been non-idle, report the transition */
1357 if (sc->mlx_rebuildstat.rs_code != MLX_REBUILDSTAT_IDLE) {
1358 device_printf(sc->mlx_dev, "background check/rebuild operation completed\n");
1361 sc->mlx_background = 0;
1362 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
1365 free(mc->mc_data, M_DEVBUF);
1369 /********************************************************************************
1370 ********************************************************************************
1372 ********************************************************************************
1373 ********************************************************************************/
1375 /********************************************************************************
1376 * It's time to perform a channel pause action for (sc), either start or stop
1380 mlx_pause_action(struct mlx_softc *sc)
1382 struct mlx_command *mc;
1383 int failsafe, i, command;
1385 /* What are we doing here? */
1386 if (sc->mlx_pause.mp_when == 0) {
1387 command = MLX_CMD_STARTCHANNEL;
1391 command = MLX_CMD_STOPCHANNEL;
1394 * Channels will always start again after the failsafe period,
1395 * which is specified in multiples of 30 seconds.
1396 * This constrains us to a maximum pause of 450 seconds.
1398 failsafe = ((sc->mlx_pause.mp_howlong - time_second) + 5) / 30;
1399 if (failsafe > 0xf) {
1401 sc->mlx_pause.mp_howlong = time_second + (0xf * 30) - 5;
1405 /* build commands for every channel requested */
1406 for (i = 0; i < sc->mlx_enq2->me_actual_channels; i++) {
1407 if ((1 << i) & sc->mlx_pause.mp_which) {
1409 /* get ourselves a command buffer */
1410 if ((mc = mlx_alloccmd(sc)) == NULL)
1412 /* get a command slot */
1413 mc->mc_flags |= MLX_CMD_PRIORITY;
1414 if (mlx_getslot(mc))
1417 /* build the command */
1418 mlx_make_type2(mc, command, (failsafe << 4) | i, 0, 0, 0, 0, 0, 0, 0);
1419 mc->mc_complete = mlx_pause_done;
1420 mc->mc_private = sc; /* XXX not needed */
1423 /* command submitted OK */
1427 device_printf(sc->mlx_dev, "%s failed for channel %d\n",
1428 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", i);
1436 mlx_pause_done(struct mlx_command *mc)
1438 struct mlx_softc *sc = mc->mc_sc;
1439 int command = mc->mc_mailbox[0];
1440 int channel = mc->mc_mailbox[2] & 0xf;
1442 if (mc->mc_status != 0) {
1443 device_printf(sc->mlx_dev, "%s command failed - %s\n",
1444 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", mlx_diagnose_command(mc));
1445 } else if (command == MLX_CMD_STOPCHANNEL) {
1446 device_printf(sc->mlx_dev, "channel %d pausing for %ld seconds\n",
1447 channel, (long)(sc->mlx_pause.mp_howlong - time_second));
1449 device_printf(sc->mlx_dev, "channel %d resuming\n", channel);
1454 /********************************************************************************
1455 ********************************************************************************
1457 ********************************************************************************
1458 ********************************************************************************/
1460 /********************************************************************************
1461 * Perform an Enquiry command using a type-3 command buffer and a return a single
1462 * linear result buffer. If the completion function is specified, it will
1463 * be called with the completed command (and the result response will not be
1464 * valid until that point). Otherwise, the command will either be busy-waited
1465 * for (interrupts not enabled), or slept for.
1468 mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize, void (* complete)(struct mlx_command *mc))
1470 struct mlx_command *mc;
1476 /* get ourselves a command buffer */
1479 if ((mc = mlx_alloccmd(sc)) == NULL)
1481 /* allocate the response structure */
1482 result = malloc(bufsize, M_DEVBUF, M_INTWAIT);
1483 /* get a command slot */
1484 mc->mc_flags |= MLX_CMD_PRIORITY | MLX_CMD_DATAOUT;
1485 if (mlx_getslot(mc))
1488 /* map the command so the controller can see it */
1489 mc->mc_data = result;
1490 mc->mc_length = bufsize;
1493 /* build an enquiry command */
1494 mlx_make_type2(mc, command, 0, 0, 0, 0, 0, 0, mc->mc_dataphys, 0);
1496 /* do we want a completion callback? */
1497 if (complete != NULL) {
1498 mc->mc_complete = complete;
1499 mc->mc_private = mc;
1500 if ((error = mlx_start(mc)) != 0)
1503 /* run the command in either polled or wait mode */
1504 if ((sc->mlx_state & MLX_STATE_INTEN) ? mlx_wait_command(mc) : mlx_poll_command(mc))
1507 /* command completed OK? */
1508 if (mc->mc_status != 0) {
1509 device_printf(sc->mlx_dev, "ENQUIRY failed - %s\n", mlx_diagnose_command(mc));
1513 error = 0; /* success */
1515 /* we got a command, but nobody else will free it */
1516 if ((complete == NULL) && (mc != NULL))
1518 /* we got an error, and we allocated a result */
1519 if ((error != 0) && (result != NULL)) {
1520 free(result, M_DEVBUF);
1527 /********************************************************************************
1528 * Perform a Flush command on the nominated controller.
1530 * May be called with interrupts enabled or disabled; will not return until
1531 * the flush operation completes or fails.
1534 mlx_flush(struct mlx_softc *sc)
1536 struct mlx_command *mc;
1541 /* get ourselves a command buffer */
1543 if ((mc = mlx_alloccmd(sc)) == NULL)
1545 /* get a command slot */
1546 if (mlx_getslot(mc))
1549 /* build a flush command */
1550 mlx_make_type2(mc, MLX_CMD_FLUSH, 0, 0, 0, 0, 0, 0, 0, 0);
1552 /* can't assume that interrupts are going to work here, so play it safe */
1553 if (mlx_poll_command(mc))
1556 /* command completed OK? */
1557 if (mc->mc_status != 0) {
1558 device_printf(sc->mlx_dev, "FLUSH failed - %s\n", mlx_diagnose_command(mc));
1562 error = 0; /* success */
1569 /********************************************************************************
1570 * Start a background consistency check on (drive).
1572 * May be called with interrupts enabled or disabled; will return as soon as the
1573 * operation has started or been refused.
1576 mlx_check(struct mlx_softc *sc, int drive)
1578 struct mlx_command *mc;
1583 /* get ourselves a command buffer */
1585 if ((mc = mlx_alloccmd(sc)) == NULL)
1587 /* get a command slot */
1588 if (mlx_getslot(mc))
1591 /* build a checkasync command, set the "fix it" flag */
1592 mlx_make_type2(mc, MLX_CMD_CHECKASYNC, 0, 0, 0, 0, 0, drive | 0x80, 0, 0);
1594 /* start the command and wait for it to be returned */
1595 if (mlx_wait_command(mc))
1598 /* command completed OK? */
1599 if (mc->mc_status != 0) {
1600 device_printf(sc->mlx_dev, "CHECK ASYNC failed - %s\n", mlx_diagnose_command(mc));
1602 device_printf(sc->mlx_sysdrive[drive].ms_disk, "consistency check started");
1604 error = mc->mc_status;
1612 /********************************************************************************
1613 * Start a background rebuild of the physical drive at (channel),(target).
1615 * May be called with interrupts enabled or disabled; will return as soon as the
1616 * operation has started or been refused.
1619 mlx_rebuild(struct mlx_softc *sc, int channel, int target)
1621 struct mlx_command *mc;
1626 /* get ourselves a command buffer */
1628 if ((mc = mlx_alloccmd(sc)) == NULL)
1630 /* get a command slot */
1631 if (mlx_getslot(mc))
1634 /* build a checkasync command, set the "fix it" flag */
1635 mlx_make_type2(mc, MLX_CMD_REBUILDASYNC, channel, target, 0, 0, 0, 0, 0, 0);
1637 /* start the command and wait for it to be returned */
1638 if (mlx_wait_command(mc))
1641 /* command completed OK? */
1642 if (mc->mc_status != 0) {
1643 device_printf(sc->mlx_dev, "REBUILD ASYNC failed - %s\n", mlx_diagnose_command(mc));
1645 device_printf(sc->mlx_dev, "drive rebuild started for %d:%d\n", channel, target);
1647 error = mc->mc_status;
1655 /********************************************************************************
1656 * Run the command (mc) and return when it completes.
1658 * Interrupts need to be enabled; returns nonzero on error.
1661 mlx_wait_command(struct mlx_command *mc)
1663 struct mlx_softc *sc = mc->mc_sc;
1668 mc->mc_complete = NULL;
1669 mc->mc_private = mc; /* wake us when you're done */
1670 if ((error = mlx_start(mc)) != 0)
1674 /* XXX better timeout? */
1675 while ((mc->mc_status == MLX_STATUS_BUSY) && (count < 30)) {
1676 tsleep(mc->mc_private, PCATCH, "mlxwcmd", hz);
1679 if (mc->mc_status != 0) {
1680 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1687 /********************************************************************************
1688 * Start the command (mc) and busy-wait for it to complete.
1690 * Should only be used when interrupts can't be relied upon. Returns 0 on
1691 * success, nonzero on error.
1692 * Successfully completed commands are dequeued.
1695 mlx_poll_command(struct mlx_command *mc)
1697 struct mlx_softc *sc = mc->mc_sc;
1702 mc->mc_complete = NULL;
1703 mc->mc_private = NULL; /* we will poll for it */
1704 if ((error = mlx_start(mc)) != 0)
1709 /* poll for completion */
1710 mlx_done(mc->mc_sc);
1712 } while ((mc->mc_status == MLX_STATUS_BUSY) && (count++ < 15000000));
1713 if (mc->mc_status != MLX_STATUS_BUSY) {
1715 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
1719 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1723 /********************************************************************************
1724 * Pull as much work off the softc's work queue as possible and give it to the
1725 * controller. Leave a couple of slots free for emergencies.
1727 * Must be called at splbio or in an equivalent fashion that prevents
1728 * reentry or activity on the bioq.
1731 mlx_startio(struct mlx_softc *sc)
1733 struct mlx_command *mc;
1734 struct mlxd_softc *mlxd;
1742 /* avoid reentrancy */
1743 if (mlx_lock_tas(sc, MLX_LOCK_STARTING))
1746 /* spin until something prevents us from doing any work */
1750 /* see if there's work to be done */
1751 if ((bio = bioq_first(&sc->mlx_bioq)) == NULL)
1754 if ((mc = mlx_alloccmd(sc)) == NULL)
1756 /* get a slot for the command */
1757 if (mlx_getslot(mc) != 0) {
1761 /* get the buf containing our work */
1762 bioq_remove(&sc->mlx_bioq, bio);
1767 /* connect the buf to the command */
1768 mc->mc_complete = mlx_completeio;
1769 mc->mc_private = bp;
1770 mc->mc_data = bp->b_data;
1771 mc->mc_length = bp->b_bcount;
1772 if (bp->b_cmd == BUF_CMD_READ) {
1773 mc->mc_flags |= MLX_CMD_DATAIN;
1774 cmd = MLX_CMD_READSG;
1776 mc->mc_flags |= MLX_CMD_DATAOUT;
1777 cmd = MLX_CMD_WRITESG;
1780 /* map the command so the controller can work with it */
1783 /* build a suitable I/O command (assumes 512-byte rounded transfers) */
1784 mlxd = (struct mlxd_softc *)bio->bio_driver_info;
1785 driveno = mlxd->mlxd_drive - sc->mlx_sysdrive;
1786 blkcount = (bp->b_bcount + MLX_BLKSIZE - 1) / MLX_BLKSIZE;
1787 blkno = bio->bio_offset / MLX_BLKSIZE;
1789 if ((blkno + blkcount) > sc->mlx_sysdrive[driveno].ms_size)
1790 device_printf(sc->mlx_dev, "I/O beyond end of unit (%u,%d > %u)\n",
1791 blkno, blkcount, sc->mlx_sysdrive[driveno].ms_size);
1794 * Build the I/O command. Note that the SG list type bits are set to zero,
1795 * denoting the format of SG list that we are using.
1797 if (sc->mlx_iftype == MLX_IFTYPE_2) {
1798 mlx_make_type1(mc, (cmd == MLX_CMD_WRITESG) ? MLX_CMD_WRITESG_OLD : MLX_CMD_READSG_OLD,
1799 blkcount & 0xff, /* xfer length low byte */
1800 blkno, /* physical block number */
1801 driveno, /* target drive number */
1802 mc->mc_sgphys, /* location of SG list */
1803 mc->mc_nsgent & 0x3f); /* size of SG list (top 3 bits clear) */
1805 mlx_make_type5(mc, cmd,
1806 blkcount & 0xff, /* xfer length low byte */
1807 (driveno << 3) | ((blkcount >> 8) & 0x07), /* target and length high 3 bits */
1808 blkno, /* physical block number */
1809 mc->mc_sgphys, /* location of SG list */
1810 mc->mc_nsgent & 0x3f); /* size of SG list (top 3 bits clear) */
1813 /* try to give command to controller */
1814 if (mlx_start(mc) != 0) {
1815 /* fail the command */
1816 mc->mc_status = MLX_STATUS_WEDGED;
1822 mlx_lock_clr(sc, MLX_LOCK_STARTING);
1825 /********************************************************************************
1826 * Handle completion of an I/O command.
1829 mlx_completeio(struct mlx_command *mc)
1831 struct mlx_softc *sc = mc->mc_sc;
1832 struct bio *bio = (mlx_bio *)mc->mc_private;
1833 struct mlxd_softc *mlxd = (struct mlxd_softc *)bio->bio_driver_info;
1834 struct buf *bp = bio->bio_buf;
1836 if (mc->mc_status != MLX_STATUS_OK) { /* could be more verbose here? */
1838 bp->b_flags |= B_ERROR;
1840 switch(mc->mc_status) {
1841 case MLX_STATUS_RDWROFFLINE: /* system drive has gone offline */
1842 device_printf(mlxd->mlxd_dev, "drive offline\n");
1843 /* should signal this with a return code */
1844 mlxd->mlxd_drive->ms_state = MLX_SYSD_OFFLINE;
1847 default: /* other I/O error */
1848 device_printf(sc->mlx_dev, "I/O error - %s\n", mlx_diagnose_command(mc));
1850 device_printf(sc->mlx_dev, " b_bcount %ld offset %lld\n",
1851 bp->b_bcount, bio->bio_offset);
1852 device_printf(sc->mlx_dev, " %13D\n", mc->mc_mailbox, " ");
1861 /********************************************************************************
1862 * Take a command from user-space and try to run it.
1864 * XXX Note that this can't perform very much in the way of error checking, and
1865 * as such, applications _must_ be considered trustworthy.
1866 * XXX Commands using S/G for data are not supported.
1869 mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu)
1871 struct mlx_command *mc;
1872 struct mlx_dcdb *dcdb;
1883 /* get ourselves a command and copy in from user space */
1884 if ((mc = mlx_alloccmd(sc)) == NULL)
1886 bcopy(mu->mu_command, mc->mc_mailbox, sizeof(mc->mc_mailbox));
1887 debug(0, "got command buffer");
1889 /* if we need a buffer for data transfer, allocate one and copy in its initial contents */
1890 if (mu->mu_datasize > 0) {
1891 if (mu->mu_datasize > MAXPHYS)
1893 if (((kbuf = malloc(mu->mu_datasize, M_DEVBUF, M_WAITOK)) == NULL) ||
1894 (error = copyin(mu->mu_buf, kbuf, mu->mu_datasize)))
1896 debug(0, "got kernel buffer");
1899 /* get a command slot */
1900 if (mlx_getslot(mc))
1902 debug(0, "got a slot");
1904 /* map the command so the controller can see it */
1906 mc->mc_length = mu->mu_datasize;
1911 * If this is a passthrough SCSI command, the DCDB is packed at the
1912 * beginning of the data area. Fix up the DCDB to point to the correct physical
1913 * address and override any bufptr supplied by the caller since we know
1914 * what it's meant to be.
1916 if (mc->mc_mailbox[0] == MLX_CMD_DIRECT_CDB) {
1917 dcdb = (struct mlx_dcdb *)kbuf;
1918 dcdb->dcdb_physaddr = mc->mc_dataphys + sizeof(*dcdb);
1923 * If there's a data buffer, fix up the command's buffer pointer.
1925 if (mu->mu_datasize > 0) {
1927 /* range check the pointer to physical buffer address */
1928 if ((mu->mu_bufptr < 0) || (mu->mu_bufptr > (sizeof(mu->mu_command) - sizeof(u_int32_t)))) {
1932 mc->mc_mailbox[mu->mu_bufptr ] = mc->mc_dataphys & 0xff;
1933 mc->mc_mailbox[mu->mu_bufptr + 1] = (mc->mc_dataphys >> 8) & 0xff;
1934 mc->mc_mailbox[mu->mu_bufptr + 2] = (mc->mc_dataphys >> 16) & 0xff;
1935 mc->mc_mailbox[mu->mu_bufptr + 3] = (mc->mc_dataphys >> 24) & 0xff;
1937 debug(0, "command fixup");
1939 /* submit the command and wait */
1940 if ((error = mlx_wait_command(mc)) != 0)
1943 /* copy out status and data */
1944 mu->mu_status = mc->mc_status;
1945 if ((mu->mu_datasize > 0) && ((error = copyout(kbuf, mu->mu_buf, mu->mu_datasize))))
1952 free(kbuf, M_DEVBUF);
1956 /********************************************************************************
1957 ********************************************************************************
1958 Command I/O to Controller
1959 ********************************************************************************
1960 ********************************************************************************/
1962 /********************************************************************************
1963 * Find a free command slot for (mc).
1965 * Don't hand out a slot to a normal-priority command unless there are at least
1966 * 4 slots free for priority commands.
1969 mlx_getslot(struct mlx_command *mc)
1971 struct mlx_softc *sc = mc->mc_sc;
1977 * Enforce slot-usage limit, if we have the required information.
1979 if (sc->mlx_enq2 != NULL) {
1980 limit = sc->mlx_enq2->me_max_commands;
1984 if (sc->mlx_busycmds >= ((mc->mc_flags & MLX_CMD_PRIORITY) ? limit : limit - 4))
1988 * Allocate an outstanding command slot
1990 * XXX linear search is slow
1993 for (slot = 0; slot < limit; slot++) {
1994 debug(2, "try slot %d", slot);
1995 if (sc->mlx_busycmd[slot] == NULL)
1999 sc->mlx_busycmd[slot] = mc;
2008 debug(2, "got slot %d", slot);
2013 /********************************************************************************
2014 * Map/unmap (mc)'s data in the controller's addressable space.
2017 mlx_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
2019 struct mlx_command *mc = (struct mlx_command *)arg;
2020 struct mlx_softc *sc = mc->mc_sc;
2021 struct mlx_sgentry *sg;
2026 /* XXX should be unnecessary */
2027 if (sc->mlx_enq2 && (nsegments > sc->mlx_enq2->me_max_sg))
2028 panic("MLX: too many s/g segments (%d, max %d)", nsegments, sc->mlx_enq2->me_max_sg);
2030 /* get base address of s/g table */
2031 sg = sc->mlx_sgtable + (mc->mc_slot * MLX_NSEG);
2033 /* save s/g table information in command */
2034 mc->mc_nsgent = nsegments;
2035 mc->mc_sgphys = sc->mlx_sgbusaddr + (mc->mc_slot * MLX_NSEG * sizeof(struct mlx_sgentry));
2036 mc->mc_dataphys = segs[0].ds_addr;
2038 /* populate s/g table */
2039 for (i = 0; i < nsegments; i++, sg++) {
2040 sg->sg_addr = segs[i].ds_addr;
2041 sg->sg_count = segs[i].ds_len;
2046 mlx_mapcmd(struct mlx_command *mc)
2048 struct mlx_softc *sc = mc->mc_sc;
2052 /* if the command involves data at all */
2053 if (mc->mc_data != NULL) {
2055 /* map the data buffer into bus space and build the s/g list */
2056 bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data, mc->mc_length,
2057 mlx_setup_dmamap, mc, 0);
2058 if (mc->mc_flags & MLX_CMD_DATAIN)
2059 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_PREREAD);
2060 if (mc->mc_flags & MLX_CMD_DATAOUT)
2061 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_PREWRITE);
2066 mlx_unmapcmd(struct mlx_command *mc)
2068 struct mlx_softc *sc = mc->mc_sc;
2072 /* if the command involved data at all */
2073 if (mc->mc_data != NULL) {
2075 if (mc->mc_flags & MLX_CMD_DATAIN)
2076 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTREAD);
2077 if (mc->mc_flags & MLX_CMD_DATAOUT)
2078 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTWRITE);
2080 bus_dmamap_unload(sc->mlx_buffer_dmat, mc->mc_dmamap);
2084 /********************************************************************************
2085 * Try to deliver (mc) to the controller.
2087 * Can be called at any interrupt level, with or without interrupts enabled.
2090 mlx_start(struct mlx_command *mc)
2092 struct mlx_softc *sc = mc->mc_sc;
2097 /* save the slot number as ident so we can handle this command when complete */
2098 mc->mc_mailbox[0x1] = mc->mc_slot;
2100 /* mark the command as currently being processed */
2101 mc->mc_status = MLX_STATUS_BUSY;
2103 /* set a default 60-second timeout XXX tunable? XXX not currently used */
2104 mc->mc_timeout = time_second + 60;
2106 /* spin waiting for the mailbox */
2107 for (i = 100000, done = 0; (i > 0) && !done; i--) {
2109 if (sc->mlx_tryqueue(sc, mc)) {
2111 /* move command to work queue */
2112 TAILQ_INSERT_TAIL(&sc->mlx_work, mc, mc_link);
2114 crit_exit(); /* drop spl to allow completion interrupts */
2117 /* command is enqueued */
2122 * We couldn't get the controller to take the command. Revoke the slot
2123 * that the command was given and return it with a bad status.
2125 sc->mlx_busycmd[mc->mc_slot] = NULL;
2126 device_printf(sc->mlx_dev, "controller wedged (not taking commands)\n");
2127 mc->mc_status = MLX_STATUS_WEDGED;
2132 /********************************************************************************
2133 * Poll the controller (sc) for completed commands.
2134 * Update command status and free slots for reuse. If any slots were freed,
2135 * new commands may be posted.
2137 * Returns nonzero if one or more commands were completed.
2140 mlx_done(struct mlx_softc *sc)
2142 struct mlx_command *mc;
2151 /* loop collecting completed commands */
2154 /* poll for a completed command's identifier and status */
2155 if (sc->mlx_findcomplete(sc, &slot, &status)) {
2157 mc = sc->mlx_busycmd[slot]; /* find command */
2158 if (mc != NULL) { /* paranoia */
2159 if (mc->mc_status == MLX_STATUS_BUSY) {
2160 mc->mc_status = status; /* save status */
2162 /* free slot for reuse */
2163 sc->mlx_busycmd[slot] = NULL;
2166 device_printf(sc->mlx_dev, "duplicate done event for slot %d\n", slot);
2169 device_printf(sc->mlx_dev, "done event for nonbusy slot %d\n", slot);
2177 /* if we've completed any commands, try posting some more */
2181 /* handle completion and timeouts */
2187 /********************************************************************************
2188 * Perform post-completion processing for commands on (sc).
2191 mlx_complete(struct mlx_softc *sc)
2193 struct mlx_command *mc, *nc;
2198 /* avoid reentrancy XXX might want to signal and request a restart */
2199 if (mlx_lock_tas(sc, MLX_LOCK_COMPLETING))
2205 /* scan the list of busy/done commands */
2206 mc = TAILQ_FIRST(&sc->mlx_work);
2207 while (mc != NULL) {
2208 nc = TAILQ_NEXT(mc, mc_link);
2210 /* Command has been completed in some fashion */
2211 if (mc->mc_status != MLX_STATUS_BUSY) {
2213 /* unmap the command's data buffer */
2216 * Does the command have a completion handler?
2218 if (mc->mc_complete != NULL) {
2219 /* remove from list and give to handler */
2220 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2221 mc->mc_complete(mc);
2224 * Is there a sleeper waiting on this command?
2226 } else if (mc->mc_private != NULL) { /* sleeping caller wants to know about it */
2228 /* remove from list and wake up sleeper */
2229 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2230 wakeup_one(mc->mc_private);
2233 * Leave the command for a caller that's polling for it.
2242 mlx_lock_clr(sc, MLX_LOCK_COMPLETING);
2245 /********************************************************************************
2246 ********************************************************************************
2247 Command Buffer Management
2248 ********************************************************************************
2249 ********************************************************************************/
2251 /********************************************************************************
2252 * Get a new command buffer.
2254 * This may return NULL in low-memory cases.
2256 * Note that using malloc() is expensive (the command buffer is << 1 page) but
2257 * necessary if we are to be a loadable module before the zone allocator is fixed.
2259 * If possible, we recycle a command buffer that's been used before.
2261 * XXX Note that command buffers are not cleaned out - it is the caller's
2262 * responsibility to ensure that all required fields are filled in before
2265 static struct mlx_command *
2266 mlx_alloccmd(struct mlx_softc *sc)
2268 struct mlx_command *mc;
2274 if ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL)
2275 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
2278 /* allocate a new command buffer? */
2280 mc = malloc(sizeof(*mc), M_DEVBUF, M_INTWAIT | M_ZERO);
2282 error = bus_dmamap_create(sc->mlx_buffer_dmat, 0, &mc->mc_dmamap);
2291 /********************************************************************************
2292 * Release a command buffer for recycling.
2294 * XXX It might be a good idea to limit the number of commands we save for reuse
2295 * if it's shown that this list bloats out massively.
2298 mlx_releasecmd(struct mlx_command *mc)
2303 TAILQ_INSERT_HEAD(&mc->mc_sc->mlx_freecmds, mc, mc_link);
2307 /********************************************************************************
2308 * Permanently discard a command buffer.
2311 mlx_freecmd(struct mlx_command *mc)
2313 struct mlx_softc *sc = mc->mc_sc;
2316 bus_dmamap_destroy(sc->mlx_buffer_dmat, mc->mc_dmamap);
2321 /********************************************************************************
2322 ********************************************************************************
2323 Type 3 interface accessor methods
2324 ********************************************************************************
2325 ********************************************************************************/
2327 /********************************************************************************
2328 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2329 * (the controller is not ready to take a command).
2331 * Must be called at splbio or in a fashion that prevents reentry.
2334 mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2340 /* ready for our command? */
2341 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_FULL)) {
2342 /* copy mailbox data to window */
2343 for (i = 0; i < 13; i++)
2344 MLX_V3_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2347 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_FULL);
2353 /********************************************************************************
2354 * See if a command has been completed, if so acknowledge its completion
2355 * and recover the slot number and status code.
2357 * Must be called at splbio or in a fashion that prevents reentry.
2360 mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2365 /* status available? */
2366 if (MLX_V3_GET_ODBR(sc) & MLX_V3_ODB_SAVAIL) {
2367 *slot = MLX_V3_GET_STATUS_IDENT(sc); /* get command identifier */
2368 *status = MLX_V3_GET_STATUS(sc); /* get status */
2370 /* acknowledge completion */
2371 MLX_V3_PUT_ODBR(sc, MLX_V3_ODB_SAVAIL);
2372 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2378 /********************************************************************************
2379 * Enable/disable interrupts as requested. (No acknowledge required)
2381 * Must be called at splbio or in a fashion that prevents reentry.
2384 mlx_v3_intaction(struct mlx_softc *sc, int action)
2389 case MLX_INTACTION_DISABLE:
2390 MLX_V3_PUT_IER(sc, 0);
2391 sc->mlx_state &= ~MLX_STATE_INTEN;
2393 case MLX_INTACTION_ENABLE:
2394 MLX_V3_PUT_IER(sc, 1);
2395 sc->mlx_state |= MLX_STATE_INTEN;
2400 /********************************************************************************
2401 * Poll for firmware error codes during controller initialisation.
2402 * Returns 0 if initialisation is complete, 1 if still in progress but no
2403 * error has been fetched, 2 if an error has been retrieved.
2406 mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2)
2409 static int initted = 0;
2413 /* first time around, clear any hardware completion status */
2415 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2420 /* init in progress? */
2421 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_INIT_BUSY))
2424 /* test error value */
2425 fwerror = MLX_V3_GET_FWERROR(sc);
2426 if (!(fwerror & MLX_V3_FWERROR_PEND))
2429 /* mask status pending bit, fetch status */
2430 *error = fwerror & ~MLX_V3_FWERROR_PEND;
2431 *param1 = MLX_V3_GET_FWERROR_PARAM1(sc);
2432 *param2 = MLX_V3_GET_FWERROR_PARAM2(sc);
2435 MLX_V3_PUT_FWERROR(sc, 0);
2440 /********************************************************************************
2441 ********************************************************************************
2442 Type 4 interface accessor methods
2443 ********************************************************************************
2444 ********************************************************************************/
2446 /********************************************************************************
2447 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2448 * (the controller is not ready to take a command).
2450 * Must be called at splbio or in a fashion that prevents reentry.
2453 mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2459 /* ready for our command? */
2460 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_FULL)) {
2461 /* copy mailbox data to window */
2462 for (i = 0; i < 13; i++)
2463 MLX_V4_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2465 /* memory-mapped controller, so issue a write barrier to ensure the mailbox is filled */
2466 bus_space_barrier(sc->mlx_btag, sc->mlx_bhandle, MLX_V4_MAILBOX, MLX_V4_MAILBOX_LENGTH,
2467 BUS_SPACE_BARRIER_WRITE);
2470 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_HWMBOX_CMD);
2476 /********************************************************************************
2477 * See if a command has been completed, if so acknowledge its completion
2478 * and recover the slot number and status code.
2480 * Must be called at splbio or in a fashion that prevents reentry.
2483 mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2488 /* status available? */
2489 if (MLX_V4_GET_ODBR(sc) & MLX_V4_ODB_HWSAVAIL) {
2490 *slot = MLX_V4_GET_STATUS_IDENT(sc); /* get command identifier */
2491 *status = MLX_V4_GET_STATUS(sc); /* get status */
2493 /* acknowledge completion */
2494 MLX_V4_PUT_ODBR(sc, MLX_V4_ODB_HWMBOX_ACK);
2495 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2501 /********************************************************************************
2502 * Enable/disable interrupts as requested.
2504 * Must be called at splbio or in a fashion that prevents reentry.
2507 mlx_v4_intaction(struct mlx_softc *sc, int action)
2512 case MLX_INTACTION_DISABLE:
2513 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK | MLX_V4_IER_DISINT);
2514 sc->mlx_state &= ~MLX_STATE_INTEN;
2516 case MLX_INTACTION_ENABLE:
2517 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK & ~MLX_V4_IER_DISINT);
2518 sc->mlx_state |= MLX_STATE_INTEN;
2523 /********************************************************************************
2524 * Poll for firmware error codes during controller initialisation.
2525 * Returns 0 if initialisation is complete, 1 if still in progress but no
2526 * error has been fetched, 2 if an error has been retrieved.
2529 mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2)
2532 static int initted = 0;
2536 /* first time around, clear any hardware completion status */
2538 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2543 /* init in progress? */
2544 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_INIT_BUSY))
2547 /* test error value */
2548 fwerror = MLX_V4_GET_FWERROR(sc);
2549 if (!(fwerror & MLX_V4_FWERROR_PEND))
2552 /* mask status pending bit, fetch status */
2553 *error = fwerror & ~MLX_V4_FWERROR_PEND;
2554 *param1 = MLX_V4_GET_FWERROR_PARAM1(sc);
2555 *param2 = MLX_V4_GET_FWERROR_PARAM2(sc);
2558 MLX_V4_PUT_FWERROR(sc, 0);
2563 /********************************************************************************
2564 ********************************************************************************
2565 Type 5 interface accessor methods
2566 ********************************************************************************
2567 ********************************************************************************/
2569 /********************************************************************************
2570 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2571 * (the controller is not ready to take a command).
2573 * Must be called at splbio or in a fashion that prevents reentry.
2576 mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2582 /* ready for our command? */
2583 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_EMPTY) {
2584 /* copy mailbox data to window */
2585 for (i = 0; i < 13; i++)
2586 MLX_V5_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2589 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_HWMBOX_CMD);
2595 /********************************************************************************
2596 * See if a command has been completed, if so acknowledge its completion
2597 * and recover the slot number and status code.
2599 * Must be called at splbio or in a fashion that prevents reentry.
2602 mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2607 /* status available? */
2608 if (MLX_V5_GET_ODBR(sc) & MLX_V5_ODB_HWSAVAIL) {
2609 *slot = MLX_V5_GET_STATUS_IDENT(sc); /* get command identifier */
2610 *status = MLX_V5_GET_STATUS(sc); /* get status */
2612 /* acknowledge completion */
2613 MLX_V5_PUT_ODBR(sc, MLX_V5_ODB_HWMBOX_ACK);
2614 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2620 /********************************************************************************
2621 * Enable/disable interrupts as requested.
2623 * Must be called at splbio or in a fashion that prevents reentry.
2626 mlx_v5_intaction(struct mlx_softc *sc, int action)
2631 case MLX_INTACTION_DISABLE:
2632 MLX_V5_PUT_IER(sc, 0xff & MLX_V5_IER_DISINT);
2633 sc->mlx_state &= ~MLX_STATE_INTEN;
2635 case MLX_INTACTION_ENABLE:
2636 MLX_V5_PUT_IER(sc, 0xff & ~MLX_V5_IER_DISINT);
2637 sc->mlx_state |= MLX_STATE_INTEN;
2642 /********************************************************************************
2643 * Poll for firmware error codes during controller initialisation.
2644 * Returns 0 if initialisation is complete, 1 if still in progress but no
2645 * error has been fetched, 2 if an error has been retrieved.
2648 mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2)
2651 static int initted = 0;
2655 /* first time around, clear any hardware completion status */
2657 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2662 /* init in progress? */
2663 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_INIT_DONE)
2666 /* test for error value */
2667 fwerror = MLX_V5_GET_FWERROR(sc);
2668 if (!(fwerror & MLX_V5_FWERROR_PEND))
2671 /* mask status pending bit, fetch status */
2672 *error = fwerror & ~MLX_V5_FWERROR_PEND;
2673 *param1 = MLX_V5_GET_FWERROR_PARAM1(sc);
2674 *param2 = MLX_V5_GET_FWERROR_PARAM2(sc);
2677 MLX_V5_PUT_FWERROR(sc, 0xff);
2682 /********************************************************************************
2683 ********************************************************************************
2685 ********************************************************************************
2686 ********************************************************************************/
2688 /********************************************************************************
2689 * Return a status message describing (mc)
2691 static char *mlx_status_messages[] = {
2692 "normal completion", /* 00 */
2693 "irrecoverable data error", /* 01 */
2694 "drive does not exist, or is offline", /* 02 */
2695 "attempt to write beyond end of drive", /* 03 */
2696 "bad data encountered", /* 04 */
2697 "invalid log entry request", /* 05 */
2698 "attempt to rebuild online drive", /* 06 */
2699 "new disk failed during rebuild", /* 07 */
2700 "invalid channel/target", /* 08 */
2701 "rebuild/check already in progress", /* 09 */
2702 "one or more disks are dead", /* 10 */
2703 "invalid or non-redundant drive", /* 11 */
2704 "channel is busy", /* 12 */
2705 "channel is not stopped", /* 13 */
2706 "rebuild successfully terminated", /* 14 */
2707 "unsupported command", /* 15 */
2708 "check condition received", /* 16 */
2709 "device is busy", /* 17 */
2710 "selection or command timeout", /* 18 */
2711 "command terminated abnormally", /* 19 */
2720 } mlx_messages[] = {
2721 {MLX_CMD_READSG, 0x0001, 1},
2722 {MLX_CMD_READSG, 0x0002, 1},
2723 {MLX_CMD_READSG, 0x0105, 3},
2724 {MLX_CMD_READSG, 0x010c, 4},
2725 {MLX_CMD_WRITESG, 0x0001, 1},
2726 {MLX_CMD_WRITESG, 0x0002, 1},
2727 {MLX_CMD_WRITESG, 0x0105, 3},
2728 {MLX_CMD_READSG_OLD, 0x0001, 1},
2729 {MLX_CMD_READSG_OLD, 0x0002, 1},
2730 {MLX_CMD_READSG_OLD, 0x0105, 3},
2731 {MLX_CMD_WRITESG_OLD, 0x0001, 1},
2732 {MLX_CMD_WRITESG_OLD, 0x0002, 1},
2733 {MLX_CMD_WRITESG_OLD, 0x0105, 3},
2734 {MLX_CMD_LOGOP, 0x0105, 5},
2735 {MLX_CMD_REBUILDASYNC, 0x0002, 6},
2736 {MLX_CMD_REBUILDASYNC, 0x0004, 7},
2737 {MLX_CMD_REBUILDASYNC, 0x0105, 8},
2738 {MLX_CMD_REBUILDASYNC, 0x0106, 9},
2739 {MLX_CMD_REBUILDASYNC, 0x0107, 14},
2740 {MLX_CMD_CHECKASYNC, 0x0002, 10},
2741 {MLX_CMD_CHECKASYNC, 0x0105, 11},
2742 {MLX_CMD_CHECKASYNC, 0x0106, 9},
2743 {MLX_CMD_STOPCHANNEL, 0x0106, 12},
2744 {MLX_CMD_STOPCHANNEL, 0x0105, 8},
2745 {MLX_CMD_STARTCHANNEL, 0x0005, 13},
2746 {MLX_CMD_STARTCHANNEL, 0x0105, 8},
2747 {MLX_CMD_DIRECT_CDB, 0x0002, 16},
2748 {MLX_CMD_DIRECT_CDB, 0x0008, 17},
2749 {MLX_CMD_DIRECT_CDB, 0x000e, 18},
2750 {MLX_CMD_DIRECT_CDB, 0x000f, 19},
2751 {MLX_CMD_DIRECT_CDB, 0x0105, 8},
2758 mlx_diagnose_command(struct mlx_command *mc)
2760 static char unkmsg[80];
2763 /* look up message in table */
2764 for (i = 0; mlx_messages[i].command != -1; i++)
2765 if (((mc->mc_mailbox[0] == mlx_messages[i].command) || (mlx_messages[i].command == 0)) &&
2766 (mc->mc_status == mlx_messages[i].status))
2767 return(mlx_status_messages[mlx_messages[i].msg]);
2769 sprintf(unkmsg, "unknown response 0x%x for command 0x%x", (int)mc->mc_status, (int)mc->mc_mailbox[0]);
2773 /*******************************************************************************
2774 * Print a string describing the controller (sc)
2780 } mlx_controller_names[] = {
2795 mlx_describe_controller(struct mlx_softc *sc)
2797 static char buf[80];
2801 for (i = 0, model = NULL; mlx_controller_names[i].name != NULL; i++) {
2802 if ((sc->mlx_enq2->me_hardware_id & 0xff) == mlx_controller_names[i].hwid) {
2803 model = mlx_controller_names[i].name;
2807 if (model == NULL) {
2808 sprintf(buf, " model 0x%x", sc->mlx_enq2->me_hardware_id & 0xff);
2811 device_printf(sc->mlx_dev, "DAC%s, %d channel%s, firmware %d.%02d-%c-%02d, %dMB RAM\n",
2813 sc->mlx_enq2->me_actual_channels,
2814 sc->mlx_enq2->me_actual_channels > 1 ? "s" : "",
2815 sc->mlx_enq2->me_firmware_id & 0xff,
2816 (sc->mlx_enq2->me_firmware_id >> 8) & 0xff,
2817 (sc->mlx_enq2->me_firmware_id >> 24) & 0xff,
2818 (sc->mlx_enq2->me_firmware_id >> 16) & 0xff,
2819 sc->mlx_enq2->me_mem_size / (1024 * 1024));
2822 device_printf(sc->mlx_dev, " Hardware ID 0x%08x\n", sc->mlx_enq2->me_hardware_id);
2823 device_printf(sc->mlx_dev, " Firmware ID 0x%08x\n", sc->mlx_enq2->me_firmware_id);
2824 device_printf(sc->mlx_dev, " Configured/Actual channels %d/%d\n", sc->mlx_enq2->me_configured_channels,
2825 sc->mlx_enq2->me_actual_channels);
2826 device_printf(sc->mlx_dev, " Max Targets %d\n", sc->mlx_enq2->me_max_targets);
2827 device_printf(sc->mlx_dev, " Max Tags %d\n", sc->mlx_enq2->me_max_tags);
2828 device_printf(sc->mlx_dev, " Max System Drives %d\n", sc->mlx_enq2->me_max_sys_drives);
2829 device_printf(sc->mlx_dev, " Max Arms %d\n", sc->mlx_enq2->me_max_arms);
2830 device_printf(sc->mlx_dev, " Max Spans %d\n", sc->mlx_enq2->me_max_spans);
2831 device_printf(sc->mlx_dev, " DRAM/cache/flash/NVRAM size %d/%d/%d/%d\n", sc->mlx_enq2->me_mem_size,
2832 sc->mlx_enq2->me_cache_size, sc->mlx_enq2->me_flash_size, sc->mlx_enq2->me_nvram_size);
2833 device_printf(sc->mlx_dev, " DRAM type %d\n", sc->mlx_enq2->me_mem_type);
2834 device_printf(sc->mlx_dev, " Clock Speed %dns\n", sc->mlx_enq2->me_clock_speed);
2835 device_printf(sc->mlx_dev, " Hardware Speed %dns\n", sc->mlx_enq2->me_hardware_speed);
2836 device_printf(sc->mlx_dev, " Max Commands %d\n", sc->mlx_enq2->me_max_commands);
2837 device_printf(sc->mlx_dev, " Max SG Entries %d\n", sc->mlx_enq2->me_max_sg);
2838 device_printf(sc->mlx_dev, " Max DP %d\n", sc->mlx_enq2->me_max_dp);
2839 device_printf(sc->mlx_dev, " Max IOD %d\n", sc->mlx_enq2->me_max_iod);
2840 device_printf(sc->mlx_dev, " Max Comb %d\n", sc->mlx_enq2->me_max_comb);
2841 device_printf(sc->mlx_dev, " Latency %ds\n", sc->mlx_enq2->me_latency);
2842 device_printf(sc->mlx_dev, " SCSI Timeout %ds\n", sc->mlx_enq2->me_scsi_timeout);
2843 device_printf(sc->mlx_dev, " Min Free Lines %d\n", sc->mlx_enq2->me_min_freelines);
2844 device_printf(sc->mlx_dev, " Rate Constant %d\n", sc->mlx_enq2->me_rate_const);
2845 device_printf(sc->mlx_dev, " MAXBLK %d\n", sc->mlx_enq2->me_maxblk);
2846 device_printf(sc->mlx_dev, " Blocking Factor %d sectors\n", sc->mlx_enq2->me_blocking_factor);
2847 device_printf(sc->mlx_dev, " Cache Line Size %d blocks\n", sc->mlx_enq2->me_cacheline);
2848 device_printf(sc->mlx_dev, " SCSI Capability %s%dMHz, %d bit\n",
2849 sc->mlx_enq2->me_scsi_cap & (1<<4) ? "differential " : "",
2850 (1 << ((sc->mlx_enq2->me_scsi_cap >> 2) & 3)) * 10,
2851 8 << (sc->mlx_enq2->me_scsi_cap & 0x3));
2852 device_printf(sc->mlx_dev, " Firmware Build Number %d\n", sc->mlx_enq2->me_firmware_build);
2853 device_printf(sc->mlx_dev, " Fault Management Type %d\n", sc->mlx_enq2->me_fault_mgmt_type);
2854 device_printf(sc->mlx_dev, " Features %b\n", sc->mlx_enq2->me_firmware_features,
2855 "\20\4Background Init\3Read Ahead\2MORE\1Cluster\n");
2860 /*******************************************************************************
2861 * Emit a string describing the firmware handshake status code, and return a flag
2862 * indicating whether the code represents a fatal error.
2864 * Error code interpretations are from the Linux driver, and don't directly match
2865 * the messages printed by Mylex's BIOS. This may change if documentation on the
2866 * codes is forthcoming.
2869 mlx_fw_message(struct mlx_softc *sc, int error, int param1, int param2)
2873 device_printf(sc->mlx_dev, "physical drive %d:%d not responding\n", param2, param1);
2876 /* we could be neater about this and give some indication when we receive more of them */
2877 if (!(sc->mlx_flags & MLX_SPINUP_REPORTED)) {
2878 device_printf(sc->mlx_dev, "spinning up drives...\n");
2879 sc->mlx_flags |= MLX_SPINUP_REPORTED;
2883 device_printf(sc->mlx_dev, "configuration checksum error\n");
2886 device_printf(sc->mlx_dev, "mirror race recovery failed\n");
2889 device_printf(sc->mlx_dev, "mirror race recovery in progress\n");
2892 device_printf(sc->mlx_dev, "physical drive %d:%d COD mismatch\n", param2, param1);
2895 device_printf(sc->mlx_dev, "logical drive installation aborted\n");
2898 device_printf(sc->mlx_dev, "mirror race on a critical system drive\n");
2901 device_printf(sc->mlx_dev, "new controller configuration found\n");
2904 device_printf(sc->mlx_dev, "FATAL MEMORY PARITY ERROR\n");
2907 device_printf(sc->mlx_dev, "unknown firmware initialisation error %02x:%02x:%02x\n", error, param1, param2);
2913 /********************************************************************************
2914 ********************************************************************************
2916 ********************************************************************************
2917 ********************************************************************************/
2919 /********************************************************************************
2920 * Find the disk whose unit number is (unit) on this controller
2922 static struct mlx_sysdrive *
2923 mlx_findunit(struct mlx_softc *sc, int unit)
2927 /* search system drives */
2928 for (i = 0; i < MLX_MAXDRIVES; i++) {
2929 /* is this one attached? */
2930 if (sc->mlx_sysdrive[i].ms_disk != 0) {
2931 /* is this the one? */
2932 if (unit == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
2933 return(&sc->mlx_sysdrive[i]);