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
30 * Driver for the Mylex DAC960 family of RAID controllers.
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/kernel.h>
46 #include <machine/resource.h>
47 #include <machine/bus.h>
48 #include <machine/clock.h>
51 #include <geom/geom_disk.h>
53 #include <dev/mlx/mlxio.h>
54 #include <dev/mlx/mlxvar.h>
55 #include <dev/mlx/mlxreg.h>
57 static struct cdevsw mlx_cdevsw = {
58 .d_version = D_VERSION,
65 devclass_t mlx_devclass;
68 * Per-interface accessor methods
70 static int mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
71 static int mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
72 static void mlx_v3_intaction(struct mlx_softc *sc, int action);
73 static int mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first);
75 static int mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
76 static int mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
77 static void mlx_v4_intaction(struct mlx_softc *sc, int action);
78 static int mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first);
80 static int mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
81 static int mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
82 static void mlx_v5_intaction(struct mlx_softc *sc, int action);
83 static int mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first);
88 static void mlx_periodic(void *data);
89 static void mlx_periodic_enquiry(struct mlx_command *mc);
90 static void mlx_periodic_eventlog_poll(struct mlx_softc *sc);
91 static void mlx_periodic_eventlog_respond(struct mlx_command *mc);
92 static void mlx_periodic_rebuild(struct mlx_command *mc);
97 static void mlx_pause_action(struct mlx_softc *sc);
98 static void mlx_pause_done(struct mlx_command *mc);
101 * Command submission.
103 static void *mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize,
104 void (*complete)(struct mlx_command *mc));
105 static int mlx_flush(struct mlx_softc *sc);
106 static int mlx_check(struct mlx_softc *sc, int drive);
107 static int mlx_rebuild(struct mlx_softc *sc, int channel, int target);
108 static int mlx_wait_command(struct mlx_command *mc);
109 static int mlx_poll_command(struct mlx_command *mc);
110 void mlx_startio_cb(void *arg,
111 bus_dma_segment_t *segs,
112 int nsegments, int error);
113 static void mlx_startio(struct mlx_softc *sc);
114 static void mlx_completeio(struct mlx_command *mc);
115 static int mlx_user_command(struct mlx_softc *sc,
116 struct mlx_usercommand *mu);
117 void mlx_user_cb(void *arg, bus_dma_segment_t *segs,
118 int nsegments, int error);
121 * Command buffer allocation.
123 static struct mlx_command *mlx_alloccmd(struct mlx_softc *sc);
124 static void mlx_releasecmd(struct mlx_command *mc);
125 static void mlx_freecmd(struct mlx_command *mc);
128 * Command management.
130 static int mlx_getslot(struct mlx_command *mc);
131 static void mlx_setup_dmamap(struct mlx_command *mc,
132 bus_dma_segment_t *segs,
133 int nsegments, int error);
134 static void mlx_unmapcmd(struct mlx_command *mc);
135 static int mlx_shutdown_locked(struct mlx_softc *sc);
136 static int mlx_start(struct mlx_command *mc);
137 static int mlx_done(struct mlx_softc *sc, int startio);
138 static void mlx_complete(struct mlx_softc *sc);
143 static char *mlx_diagnose_command(struct mlx_command *mc);
144 static void mlx_describe_controller(struct mlx_softc *sc);
145 static int mlx_fw_message(struct mlx_softc *sc, int status, int param1, int param2);
150 static struct mlx_sysdrive *mlx_findunit(struct mlx_softc *sc, int unit);
152 /********************************************************************************
153 ********************************************************************************
155 ********************************************************************************
156 ********************************************************************************/
158 /********************************************************************************
159 * Free all of the resources associated with (sc)
161 * Should not be called if the controller is active.
164 mlx_free(struct mlx_softc *sc)
166 struct mlx_command *mc;
170 /* destroy control device */
171 if (sc->mlx_dev_t != NULL)
172 destroy_dev(sc->mlx_dev_t);
175 bus_teardown_intr(sc->mlx_dev, sc->mlx_irq, sc->mlx_intr);
177 /* cancel status timeout */
179 callout_stop(&sc->mlx_timeout);
181 /* throw away any command buffers */
182 while ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL) {
183 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
187 callout_drain(&sc->mlx_timeout);
189 /* destroy data-transfer DMA tag */
190 if (sc->mlx_buffer_dmat)
191 bus_dma_tag_destroy(sc->mlx_buffer_dmat);
193 /* free and destroy DMA memory and tag for s/g lists */
195 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
197 bus_dma_tag_destroy(sc->mlx_sg_dmat);
199 /* disconnect the interrupt handler */
200 if (sc->mlx_irq != NULL)
201 bus_release_resource(sc->mlx_dev, SYS_RES_IRQ, 0, sc->mlx_irq);
203 /* destroy the parent DMA tag */
204 if (sc->mlx_parent_dmat)
205 bus_dma_tag_destroy(sc->mlx_parent_dmat);
207 /* release the register window mapping */
208 if (sc->mlx_mem != NULL)
209 bus_release_resource(sc->mlx_dev, sc->mlx_mem_type, sc->mlx_mem_rid, sc->mlx_mem);
211 /* free controller enquiry data */
212 if (sc->mlx_enq2 != NULL)
213 free(sc->mlx_enq2, M_DEVBUF);
215 sx_destroy(&sc->mlx_config_lock);
216 mtx_destroy(&sc->mlx_io_lock);
219 /********************************************************************************
220 * Map the scatter/gather table into bus space
223 mlx_dma_map_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
225 struct mlx_softc *sc = (struct mlx_softc *)arg;
229 /* save base of s/g table's address in bus space */
230 sc->mlx_sgbusaddr = segs->ds_addr;
234 mlx_sglist_map(struct mlx_softc *sc)
241 /* destroy any existing mappings */
243 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
245 bus_dma_tag_destroy(sc->mlx_sg_dmat);
248 * Create a single tag describing a region large enough to hold all of
249 * the s/g lists we will need. If we're called early on, we don't know how
250 * many commands we're going to be asked to support, so only allocate enough
253 if (sc->mlx_enq2 == NULL) {
256 ncmd = sc->mlx_enq2->me_max_commands;
258 segsize = sizeof(struct mlx_sgentry) * MLX_NSEG * ncmd;
259 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */
260 1, 0, /* alignment,boundary */
261 BUS_SPACE_MAXADDR, /* lowaddr */
262 BUS_SPACE_MAXADDR, /* highaddr */
263 NULL, NULL, /* filter, filterarg */
264 segsize, 1, /* maxsize, nsegments */
265 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
267 NULL, NULL, /* lockfunc, lockarg */
270 device_printf(sc->mlx_dev, "can't allocate scatter/gather DMA tag\n");
275 * Allocate enough s/g maps for all commands and permanently map them into
276 * controller-visible space.
278 * XXX this assumes we can get enough space for all the s/g maps in one
279 * contiguous slab. We may need to switch to a more complex arrangement
280 * where we allocate in smaller chunks and keep a lookup table from slot
283 error = bus_dmamem_alloc(sc->mlx_sg_dmat, (void **)&sc->mlx_sgtable,
284 BUS_DMA_NOWAIT, &sc->mlx_sg_dmamap);
286 device_printf(sc->mlx_dev, "can't allocate s/g table\n");
289 (void)bus_dmamap_load(sc->mlx_sg_dmat, sc->mlx_sg_dmamap, sc->mlx_sgtable,
290 segsize, mlx_dma_map_sg, sc, 0);
294 /********************************************************************************
295 * Initialise the controller and softc
298 mlx_attach(struct mlx_softc *sc)
300 struct mlx_enquiry_old *meo;
301 int rid, error, fwminor, hscode, hserror, hsparam1, hsparam2, hsmsg;
306 * Initialise per-controller queues.
308 TAILQ_INIT(&sc->mlx_work);
309 TAILQ_INIT(&sc->mlx_freecmds);
310 bioq_init(&sc->mlx_bioq);
313 * Select accessor methods based on controller interface type.
315 switch(sc->mlx_iftype) {
318 sc->mlx_tryqueue = mlx_v3_tryqueue;
319 sc->mlx_findcomplete = mlx_v3_findcomplete;
320 sc->mlx_intaction = mlx_v3_intaction;
321 sc->mlx_fw_handshake = mlx_v3_fw_handshake;
324 sc->mlx_tryqueue = mlx_v4_tryqueue;
325 sc->mlx_findcomplete = mlx_v4_findcomplete;
326 sc->mlx_intaction = mlx_v4_intaction;
327 sc->mlx_fw_handshake = mlx_v4_fw_handshake;
330 sc->mlx_tryqueue = mlx_v5_tryqueue;
331 sc->mlx_findcomplete = mlx_v5_findcomplete;
332 sc->mlx_intaction = mlx_v5_intaction;
333 sc->mlx_fw_handshake = mlx_v5_fw_handshake;
336 return(ENXIO); /* should never happen */
339 /* disable interrupts before we start talking to the controller */
341 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
345 * Wait for the controller to come ready, handshake with the firmware if required.
346 * This is typically only necessary on platforms where the controller BIOS does not
351 while ((hscode = sc->mlx_fw_handshake(sc, &hserror, &hsparam1, &hsparam2,
353 /* report first time around... */
355 device_printf(sc->mlx_dev, "controller initialisation in progress...\n");
358 /* did we get a real message? */
360 hscode = mlx_fw_message(sc, hserror, hsparam1, hsparam2);
361 /* fatal initialisation error? */
368 device_printf(sc->mlx_dev, "initialisation complete.\n");
371 * Allocate and connect our interrupt.
374 sc->mlx_irq = bus_alloc_resource_any(sc->mlx_dev, SYS_RES_IRQ, &rid,
375 RF_SHAREABLE | RF_ACTIVE);
376 if (sc->mlx_irq == NULL) {
377 device_printf(sc->mlx_dev, "can't allocate interrupt\n");
380 error = bus_setup_intr(sc->mlx_dev, sc->mlx_irq, INTR_TYPE_BIO |
381 INTR_ENTROPY | INTR_MPSAFE, NULL, mlx_intr, sc, &sc->mlx_intr);
383 device_printf(sc->mlx_dev, "can't set up interrupt\n");
388 * Create DMA tag for mapping buffers into controller-addressable space.
390 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */
391 1, 0, /* align, boundary */
392 BUS_SPACE_MAXADDR, /* lowaddr */
393 BUS_SPACE_MAXADDR, /* highaddr */
394 NULL, NULL, /* filter, filterarg */
395 MLX_MAXPHYS, /* maxsize */
396 MLX_NSEG, /* nsegments */
397 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
399 busdma_lock_mutex, /* lockfunc */
400 &sc->mlx_io_lock, /* lockarg */
401 &sc->mlx_buffer_dmat);
403 device_printf(sc->mlx_dev, "can't allocate buffer DMA tag\n");
408 * Create some initial scatter/gather mappings so we can run the probe
411 error = mlx_sglist_map(sc);
413 device_printf(sc->mlx_dev, "can't make initial s/g list mapping\n");
418 * We don't (yet) know where the event log is up to.
420 sc->mlx_currevent = -1;
423 * Obtain controller feature information
426 if ((sc->mlx_enq2 = mlx_enquire(sc, MLX_CMD_ENQUIRY2, sizeof(struct mlx_enquiry2), NULL)) == NULL) {
428 device_printf(sc->mlx_dev, "ENQUIRY2 failed\n");
433 * Do quirk/feature related things.
435 fwminor = (sc->mlx_enq2->me_firmware_id >> 8) & 0xff;
436 switch(sc->mlx_iftype) {
438 /* These controllers don't report the firmware version in the ENQUIRY2 response */
439 if ((meo = mlx_enquire(sc, MLX_CMD_ENQUIRY_OLD, sizeof(struct mlx_enquiry_old), NULL)) == NULL) {
441 device_printf(sc->mlx_dev, "ENQUIRY_OLD failed\n");
444 sc->mlx_enq2->me_firmware_id = ('0' << 24) | (0 << 16) | (meo->me_fwminor << 8) | meo->me_fwmajor;
446 /* XXX require 2.42 or better (PCI) or 2.14 or better (EISA) */
447 if (meo->me_fwminor < 42) {
448 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
449 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 2.42 or later\n");
454 /* XXX certify 3.52? */
456 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
457 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 3.51 or later\n");
461 /* XXX certify firmware versions? */
463 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
464 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 4.06 or later\n");
469 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
470 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 5.07 or later\n");
475 return(ENXIO); /* should never happen */
480 * Create the final scatter/gather mappings now that we have characterised the controller.
482 error = mlx_sglist_map(sc);
484 device_printf(sc->mlx_dev, "can't make final s/g list mapping\n");
489 * No user-requested background operation is in progress.
491 sc->mlx_background = 0;
492 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
495 * Create the control device.
497 sc->mlx_dev_t = make_dev(&mlx_cdevsw, 0, UID_ROOT, GID_OPERATOR,
498 S_IRUSR | S_IWUSR, "mlx%d", device_get_unit(sc->mlx_dev));
499 sc->mlx_dev_t->si_drv1 = sc;
502 * Start the timeout routine.
504 callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
506 /* print a little information about the controller */
507 mlx_describe_controller(sc);
512 /********************************************************************************
513 * Locate disk resources and attach children to them.
516 mlx_startup(struct mlx_softc *sc)
518 struct mlx_enq_sys_drive *mes;
519 struct mlx_sysdrive *dr;
525 * Scan all the system drives and attach children for those that
526 * don't currently have them.
529 mes = mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(*mes) * MLX_MAXDRIVES, NULL);
532 device_printf(sc->mlx_dev, "error fetching drive status\n");
536 /* iterate over drives returned */
538 for (i = 0, dr = &sc->mlx_sysdrive[0];
539 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
541 /* are we already attached to this drive? */
542 if (dr->ms_disk == 0) {
543 /* pick up drive information */
544 dr->ms_size = mes[i].sd_size;
545 dr->ms_raidlevel = mes[i].sd_raidlevel & 0xf;
546 dr->ms_state = mes[i].sd_state;
548 /* generate geometry information */
549 if (sc->mlx_geom == MLX_GEOM_128_32) {
552 dr->ms_cylinders = dr->ms_size / (128 * 32);
553 } else { /* MLX_GEOM_255/63 */
556 dr->ms_cylinders = dr->ms_size / (255 * 63);
558 dr->ms_disk = device_add_child(sc->mlx_dev, /*"mlxd"*/NULL, -1);
559 if (dr->ms_disk == 0)
560 device_printf(sc->mlx_dev, "device_add_child failed\n");
561 device_set_ivars(dr->ms_disk, dr);
565 if ((error = bus_generic_attach(sc->mlx_dev)) != 0)
566 device_printf(sc->mlx_dev, "bus_generic_attach returned %d", error);
568 /* mark controller back up */
570 sc->mlx_state &= ~MLX_STATE_SHUTDOWN;
572 /* enable interrupts */
573 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
575 MLX_CONFIG_UNLOCK(sc);
578 /********************************************************************************
579 * Disconnect from the controller completely, in preparation for unload.
582 mlx_detach(device_t dev)
584 struct mlx_softc *sc = device_get_softc(dev);
585 struct mlxd_softc *mlxd;
592 if (sc->mlx_state & MLX_STATE_OPEN)
595 for (i = 0; i < MLX_MAXDRIVES; i++) {
596 if (sc->mlx_sysdrive[i].ms_disk != 0) {
597 mlxd = device_get_softc(sc->mlx_sysdrive[i].ms_disk);
598 if (mlxd->mlxd_flags & MLXD_OPEN) { /* drive is mounted, abort detach */
599 device_printf(sc->mlx_sysdrive[i].ms_disk, "still open, can't detach\n");
604 if ((error = mlx_shutdown(dev)))
606 MLX_CONFIG_UNLOCK(sc);
612 MLX_CONFIG_UNLOCK(sc);
616 /********************************************************************************
617 * Bring the controller down to a dormant state and detach all child devices.
619 * This function is called before detach, system shutdown, or before performing
620 * an operation which may add or delete system disks. (Call mlx_startup to
621 * resume normal operation.)
623 * Note that we can assume that the bioq on the controller is empty, as we won't
624 * allow shutdown if any device is open.
627 mlx_shutdown(device_t dev)
629 struct mlx_softc *sc = device_get_softc(dev);
633 error = mlx_shutdown_locked(sc);
634 MLX_CONFIG_UNLOCK(sc);
639 mlx_shutdown_locked(struct mlx_softc *sc)
645 MLX_CONFIG_ASSERT_LOCKED(sc);
648 sc->mlx_state |= MLX_STATE_SHUTDOWN;
649 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
651 /* flush controller */
652 device_printf(sc->mlx_dev, "flushing cache...");
660 /* delete all our child devices */
661 for (i = 0; i < MLX_MAXDRIVES; i++) {
662 if (sc->mlx_sysdrive[i].ms_disk != 0) {
663 if ((error = device_delete_child(sc->mlx_dev, sc->mlx_sysdrive[i].ms_disk)) != 0)
665 sc->mlx_sysdrive[i].ms_disk = 0;
672 /********************************************************************************
673 * Bring the controller to a quiescent state, ready for system suspend.
676 mlx_suspend(device_t dev)
678 struct mlx_softc *sc = device_get_softc(dev);
683 sc->mlx_state |= MLX_STATE_SUSPEND;
685 /* flush controller */
686 device_printf(sc->mlx_dev, "flushing cache...");
687 printf("%s\n", mlx_flush(sc) ? "failed" : "done");
689 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
695 /********************************************************************************
696 * Bring the controller back to a state ready for operation.
699 mlx_resume(device_t dev)
701 struct mlx_softc *sc = device_get_softc(dev);
706 sc->mlx_state &= ~MLX_STATE_SUSPEND;
707 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
713 /*******************************************************************************
714 * Take an interrupt, or be poked by other code to look for interrupt-worthy
720 struct mlx_softc *sc = (struct mlx_softc *)arg;
724 /* collect finished commands, queue anything waiting */
730 /*******************************************************************************
731 * Receive a buf structure from a child device and queue it on a particular
732 * disk resource, then poke the disk resource to start as much work as it can.
735 mlx_submit_buf(struct mlx_softc *sc, struct bio *bp)
740 MLX_IO_ASSERT_LOCKED(sc);
741 bioq_insert_tail(&sc->mlx_bioq, bp);
747 /********************************************************************************
748 * Accept an open operation on the control device.
751 mlx_open(struct cdev *dev, int flags, int fmt, struct thread *td)
753 struct mlx_softc *sc = dev->si_drv1;
757 sc->mlx_state |= MLX_STATE_OPEN;
759 MLX_CONFIG_UNLOCK(sc);
763 /********************************************************************************
764 * Accept the last close on the control device.
767 mlx_close(struct cdev *dev, int flags, int fmt, struct thread *td)
769 struct mlx_softc *sc = dev->si_drv1;
773 sc->mlx_state &= ~MLX_STATE_OPEN;
775 MLX_CONFIG_UNLOCK(sc);
779 /********************************************************************************
780 * Handle controller-specific control operations.
783 mlx_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
785 struct mlx_softc *sc = dev->si_drv1;
786 struct mlx_rebuild_request *rb = (struct mlx_rebuild_request *)addr;
787 struct mlx_rebuild_status *rs = (struct mlx_rebuild_status *)addr;
788 int *arg = (int *)addr;
789 struct mlx_pause *mp;
790 struct mlx_sysdrive *dr;
791 struct mlxd_softc *mlxd;
796 * Enumerate connected system drives; returns the first system drive's
797 * unit number if *arg is -1, or the next unit after *arg if it's
798 * a valid unit on this controller.
801 /* search system drives */
803 for (i = 0; i < MLX_MAXDRIVES; i++) {
804 /* is this one attached? */
805 if (sc->mlx_sysdrive[i].ms_disk != 0) {
806 /* looking for the next one we come across? */
808 *arg = device_get_unit(sc->mlx_sysdrive[i].ms_disk);
809 MLX_CONFIG_UNLOCK(sc);
812 /* we want the one after this one */
813 if (*arg == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
817 MLX_CONFIG_UNLOCK(sc);
821 * Scan the controller to see whether new drives have appeared.
823 case MLX_RESCAN_DRIVES:
830 * Disconnect from the specified drive; it may be about to go
833 case MLX_DETACH_DRIVE: /* detach one drive */
835 if (((dr = mlx_findunit(sc, *arg)) == NULL) ||
836 ((mlxd = device_get_softc(dr->ms_disk)) == NULL)) {
837 MLX_CONFIG_UNLOCK(sc);
841 device_printf(dr->ms_disk, "detaching...");
843 if (mlxd->mlxd_flags & MLXD_OPEN) {
848 /* flush controller */
858 if ((error = device_delete_child(sc->mlx_dev, dr->ms_disk)) != 0)
863 MLX_CONFIG_UNLOCK(sc);
872 * Pause one or more SCSI channels for a period of time, to assist
873 * in the process of hot-swapping devices.
875 * Note that at least the 3.51 firmware on the DAC960PL doesn't seem
878 case MLX_PAUSE_CHANNEL: /* schedule a channel pause */
879 /* Does this command work on this firmware? */
880 if (!(sc->mlx_feature & MLX_FEAT_PAUSEWORKS))
883 /* check time values */
884 mp = (struct mlx_pause *)addr;
885 if ((mp->mp_when < 0) || (mp->mp_when > 3600))
887 if ((mp->mp_howlong < 1) || (mp->mp_howlong > (0xf * 30)))
891 if ((mp->mp_which == MLX_PAUSE_CANCEL) && (sc->mlx_pause.mp_when != 0)) {
892 /* cancel a pending pause operation */
893 sc->mlx_pause.mp_which = 0;
895 /* fix for legal channels */
896 mp->mp_which &= ((1 << sc->mlx_enq2->me_actual_channels) -1);
898 /* check for a pause currently running */
899 if ((sc->mlx_pause.mp_which != 0) && (sc->mlx_pause.mp_when == 0)) {
904 /* looks ok, go with it */
905 sc->mlx_pause.mp_which = mp->mp_which;
906 sc->mlx_pause.mp_when = time_second + mp->mp_when;
907 sc->mlx_pause.mp_howlong = sc->mlx_pause.mp_when + mp->mp_howlong;
913 * Accept a command passthrough-style.
916 return(mlx_user_command(sc, (struct mlx_usercommand *)addr));
919 * Start a rebuild on a given SCSI disk
921 case MLX_REBUILDASYNC:
923 if (sc->mlx_background != 0) {
925 rb->rr_status = 0x0106;
928 rb->rr_status = mlx_rebuild(sc, rb->rr_channel, rb->rr_target);
929 switch (rb->rr_status) {
934 error = ENOMEM; /* couldn't set up the command */
953 sc->mlx_background = MLX_BACKGROUND_REBUILD;
958 * Get the status of the current rebuild or consistency check.
960 case MLX_REBUILDSTAT:
962 *rs = sc->mlx_rebuildstat;
967 * Return the per-controller system drive number matching the
968 * disk device number in (arg), if it happens to belong to us.
970 case MLX_GET_SYSDRIVE:
974 mlxd = (struct mlxd_softc *)devclass_get_softc(mlxd_devclass, *arg);
976 if ((mlxd != NULL) && (mlxd->mlxd_drive >= sc->mlx_sysdrive) &&
977 (mlxd->mlxd_drive < (sc->mlx_sysdrive + MLX_MAXDRIVES))) {
979 *arg = mlxd->mlxd_drive - sc->mlx_sysdrive;
981 MLX_CONFIG_UNLOCK(sc);
989 /********************************************************************************
990 * Handle operations requested by a System Drive connected to this controller.
993 mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd,
994 caddr_t addr, int32_t flag, struct thread *td)
996 int *arg = (int *)addr;
1001 * Return the current status of this drive.
1005 *arg = drive->ms_state;
1010 * Start a background consistency check on this drive.
1012 case MLXD_CHECKASYNC: /* start a background consistency check */
1014 if (sc->mlx_background != 0) {
1019 result = mlx_check(sc, drive - &sc->mlx_sysdrive[0]);
1025 error = ENOMEM; /* couldn't set up the command */
1041 sc->mlx_background = MLX_BACKGROUND_CHECK;
1051 /********************************************************************************
1052 ********************************************************************************
1054 ********************************************************************************
1055 ********************************************************************************/
1057 /********************************************************************************
1058 * Fire off commands to periodically check the status of connected drives.
1061 mlx_periodic(void *data)
1063 struct mlx_softc *sc = (struct mlx_softc *)data;
1066 MLX_IO_ASSERT_LOCKED(sc);
1071 if ((sc->mlx_pause.mp_which != 0) &&
1072 (sc->mlx_pause.mp_when > 0) &&
1073 (time_second >= sc->mlx_pause.mp_when)){
1075 mlx_pause_action(sc); /* pause is running */
1076 sc->mlx_pause.mp_when = 0;
1080 * Bus pause still running?
1082 } else if ((sc->mlx_pause.mp_which != 0) &&
1083 (sc->mlx_pause.mp_when == 0)) {
1085 /* time to stop bus pause? */
1086 if (time_second >= sc->mlx_pause.mp_howlong) {
1087 mlx_pause_action(sc);
1088 sc->mlx_pause.mp_which = 0; /* pause is complete */
1091 sysbeep((time_second % 5) * 100 + 500, hz/8);
1095 * Run normal periodic activities?
1097 } else if (time_second > (sc->mlx_lastpoll + 10)) {
1098 sc->mlx_lastpoll = time_second;
1101 * Check controller status.
1103 * XXX Note that this may not actually launch a command in situations of high load.
1105 mlx_enquire(sc, (sc->mlx_iftype == MLX_IFTYPE_2) ? MLX_CMD_ENQUIRY_OLD : MLX_CMD_ENQUIRY,
1106 imax(sizeof(struct mlx_enquiry), sizeof(struct mlx_enquiry_old)), mlx_periodic_enquiry);
1109 * Check system drive status.
1111 * XXX This might be better left to event-driven detection, eg. I/O to an offline
1112 * drive will detect it's offline, rebuilds etc. should detect the drive is back
1115 mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(struct mlx_enq_sys_drive) * MLX_MAXDRIVES,
1116 mlx_periodic_enquiry);
1120 /* get drive rebuild/check status */
1121 /* XXX should check sc->mlx_background if this is only valid while in progress */
1122 mlx_enquire(sc, MLX_CMD_REBUILDSTAT, sizeof(struct mlx_rebuild_stat), mlx_periodic_rebuild);
1124 /* deal with possibly-missed interrupts and timed-out commands */
1127 /* reschedule another poll next second or so */
1128 callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
1131 /********************************************************************************
1132 * Handle the result of an ENQUIRY command instigated by periodic status polling.
1135 mlx_periodic_enquiry(struct mlx_command *mc)
1137 struct mlx_softc *sc = mc->mc_sc;
1140 MLX_IO_ASSERT_LOCKED(sc);
1142 /* Command completed OK? */
1143 if (mc->mc_status != 0) {
1144 device_printf(sc->mlx_dev, "periodic enquiry failed - %s\n", mlx_diagnose_command(mc));
1148 /* respond to command */
1149 switch(mc->mc_mailbox[0]) {
1151 * This is currently a bit fruitless, as we don't know how to extract the eventlog
1154 case MLX_CMD_ENQUIRY_OLD:
1156 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data;
1157 struct mlx_enquiry_old *meo = (struct mlx_enquiry_old *)mc->mc_data;
1160 /* convert data in-place to new format */
1161 for (i = (sizeof(me->me_dead) / sizeof(me->me_dead[0])) - 1; i >= 0; i--) {
1162 me->me_dead[i].dd_chan = meo->me_dead[i].dd_chan;
1163 me->me_dead[i].dd_targ = meo->me_dead[i].dd_targ;
1165 me->me_misc_flags = 0;
1166 me->me_rebuild_count = meo->me_rebuild_count;
1167 me->me_dead_count = meo->me_dead_count;
1168 me->me_critical_sd_count = meo->me_critical_sd_count;
1169 me->me_event_log_seq_num = 0;
1170 me->me_offline_sd_count = meo->me_offline_sd_count;
1171 me->me_max_commands = meo->me_max_commands;
1172 me->me_rebuild_flag = meo->me_rebuild_flag;
1173 me->me_fwmajor = meo->me_fwmajor;
1174 me->me_fwminor = meo->me_fwminor;
1175 me->me_status_flags = meo->me_status_flags;
1176 me->me_flash_age = meo->me_flash_age;
1177 for (i = (sizeof(me->me_drvsize) / sizeof(me->me_drvsize[0])) - 1; i >= 0; i--) {
1178 if (i > ((sizeof(meo->me_drvsize) / sizeof(meo->me_drvsize[0])) - 1)) {
1179 me->me_drvsize[i] = 0; /* drive beyond supported range */
1181 me->me_drvsize[i] = meo->me_drvsize[i];
1184 me->me_num_sys_drvs = meo->me_num_sys_drvs;
1189 * Generic controller status update. We could do more with this than just
1190 * checking the event log.
1192 case MLX_CMD_ENQUIRY:
1194 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data;
1196 if (sc->mlx_currevent == -1) {
1197 /* initialise our view of the event log */
1198 sc->mlx_currevent = sc->mlx_lastevent = me->me_event_log_seq_num;
1199 } else if ((me->me_event_log_seq_num != sc->mlx_lastevent) && !(sc->mlx_flags & MLX_EVENTLOG_BUSY)) {
1200 /* record where current events are up to */
1201 sc->mlx_currevent = me->me_event_log_seq_num;
1202 debug(1, "event log pointer was %d, now %d\n", sc->mlx_lastevent, sc->mlx_currevent);
1204 /* mark the event log as busy */
1205 sc->mlx_flags |= MLX_EVENTLOG_BUSY;
1207 /* drain new eventlog entries */
1208 mlx_periodic_eventlog_poll(sc);
1212 case MLX_CMD_ENQSYSDRIVE:
1214 struct mlx_enq_sys_drive *mes = (struct mlx_enq_sys_drive *)mc->mc_data;
1215 struct mlx_sysdrive *dr;
1218 for (i = 0, dr = &sc->mlx_sysdrive[0];
1219 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
1222 /* has state been changed by controller? */
1223 if (dr->ms_state != mes[i].sd_state) {
1224 switch(mes[i].sd_state) {
1225 case MLX_SYSD_OFFLINE:
1226 device_printf(dr->ms_disk, "drive offline\n");
1228 case MLX_SYSD_ONLINE:
1229 device_printf(dr->ms_disk, "drive online\n");
1231 case MLX_SYSD_CRITICAL:
1232 device_printf(dr->ms_disk, "drive critical\n");
1235 /* save new state */
1236 dr->ms_state = mes[i].sd_state;
1242 device_printf(sc->mlx_dev, "%s: unknown command 0x%x", __func__, mc->mc_mailbox[0]);
1247 free(mc->mc_data, M_DEVBUF);
1252 mlx_eventlog_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1254 struct mlx_command *mc;
1256 mc = (struct mlx_command *)arg;
1257 mlx_setup_dmamap(mc, segs, nsegments, error);
1259 /* build the command to get one entry */
1260 mlx_make_type3(mc, MLX_CMD_LOGOP, MLX_LOGOP_GET, 1,
1261 mc->mc_sc->mlx_lastevent, 0, 0, mc->mc_dataphys, 0);
1262 mc->mc_complete = mlx_periodic_eventlog_respond;
1263 mc->mc_private = mc;
1265 /* start the command */
1266 if (mlx_start(mc) != 0) {
1268 free(mc->mc_data, M_DEVBUF);
1274 /********************************************************************************
1275 * Instigate a poll for one event log message on (sc).
1276 * We only poll for one message at a time, to keep our command usage down.
1279 mlx_periodic_eventlog_poll(struct mlx_softc *sc)
1281 struct mlx_command *mc;
1282 void *result = NULL;
1286 MLX_IO_ASSERT_LOCKED(sc);
1288 /* get ourselves a command buffer */
1290 if ((mc = mlx_alloccmd(sc)) == NULL)
1293 /* allocate the response structure */
1294 if ((result = malloc(/*sizeof(struct mlx_eventlog_entry)*/1024, M_DEVBUF,
1298 /* get a command slot */
1299 if (mlx_getslot(mc))
1302 /* map the command so the controller can see it */
1303 mc->mc_data = result;
1304 mc->mc_length = /*sizeof(struct mlx_eventlog_entry)*/1024;
1305 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
1306 mc->mc_length, mlx_eventlog_cb, mc, BUS_DMA_NOWAIT);
1312 if ((result != NULL) && (mc->mc_data != NULL))
1313 free(result, M_DEVBUF);
1317 /********************************************************************************
1318 * Handle the result of polling for a log message, generate diagnostic output.
1319 * If this wasn't the last message waiting for us, we'll go collect another.
1321 static char *mlx_sense_messages[] = {
1322 "because write recovery failed",
1323 "because of SCSI bus reset failure",
1324 "because of double check condition",
1325 "because it was removed",
1326 "because of gross error on SCSI chip",
1327 "because of bad tag returned from drive",
1328 "because of timeout on SCSI command",
1329 "because of reset SCSI command issued from system",
1330 "because busy or parity error count exceeded limit",
1331 "because of 'kill drive' command from system",
1332 "because of selection timeout",
1333 "due to SCSI phase sequence error",
1334 "due to unknown status"
1338 mlx_periodic_eventlog_respond(struct mlx_command *mc)
1340 struct mlx_softc *sc = mc->mc_sc;
1341 struct mlx_eventlog_entry *el = (struct mlx_eventlog_entry *)mc->mc_data;
1345 MLX_IO_ASSERT_LOCKED(sc);
1347 sc->mlx_lastevent++; /* next message... */
1348 if (mc->mc_status == 0) {
1350 /* handle event log message */
1351 switch(el->el_type) {
1353 * This is the only sort of message we understand at the moment.
1354 * The tests here are probably incomplete.
1356 case MLX_LOGMSG_SENSE: /* sense data */
1357 /* Mylex vendor-specific message indicating a drive was killed? */
1358 if ((el->el_sensekey == 9) &&
1359 (el->el_asc == 0x80)) {
1360 if (el->el_asq < (sizeof(mlx_sense_messages) / sizeof(mlx_sense_messages[0]))) {
1361 reason = mlx_sense_messages[el->el_asq];
1363 reason = "for unknown reason";
1365 device_printf(sc->mlx_dev, "physical drive %d:%d killed %s\n",
1366 el->el_channel, el->el_target, reason);
1368 /* SCSI drive was reset? */
1369 if ((el->el_sensekey == 6) && (el->el_asc == 0x29)) {
1370 device_printf(sc->mlx_dev, "physical drive %d:%d reset\n",
1371 el->el_channel, el->el_target);
1373 /* SCSI drive error? */
1374 if (!((el->el_sensekey == 0) ||
1375 ((el->el_sensekey == 2) &&
1376 (el->el_asc == 0x04) &&
1377 ((el->el_asq == 0x01) ||
1378 (el->el_asq == 0x02))))) {
1379 device_printf(sc->mlx_dev, "physical drive %d:%d error log: sense = %d asc = %x asq = %x\n",
1380 el->el_channel, el->el_target, el->el_sensekey, el->el_asc, el->el_asq);
1381 device_printf(sc->mlx_dev, " info %4D csi %4D\n", el->el_information, ":", el->el_csi, ":");
1386 device_printf(sc->mlx_dev, "unknown log message type 0x%x\n", el->el_type);
1390 device_printf(sc->mlx_dev, "error reading message log - %s\n", mlx_diagnose_command(mc));
1391 /* give up on all the outstanding messages, as we may have come unsynched */
1392 sc->mlx_lastevent = sc->mlx_currevent;
1395 /* dispose of command and data */
1396 free(mc->mc_data, M_DEVBUF);
1399 /* is there another message to obtain? */
1400 if (sc->mlx_lastevent != sc->mlx_currevent) {
1401 mlx_periodic_eventlog_poll(sc);
1403 /* clear log-busy status */
1404 sc->mlx_flags &= ~MLX_EVENTLOG_BUSY;
1408 /********************************************************************************
1409 * Handle check/rebuild operations in progress.
1412 mlx_periodic_rebuild(struct mlx_command *mc)
1414 struct mlx_softc *sc = mc->mc_sc;
1415 struct mlx_rebuild_status *mr = (struct mlx_rebuild_status *)mc->mc_data;
1417 MLX_IO_ASSERT_LOCKED(sc);
1418 switch(mc->mc_status) {
1419 case 0: /* operation running, update stats */
1420 sc->mlx_rebuildstat = *mr;
1422 /* spontaneous rebuild/check? */
1423 if (sc->mlx_background == 0) {
1424 sc->mlx_background = MLX_BACKGROUND_SPONTANEOUS;
1425 device_printf(sc->mlx_dev, "background check/rebuild operation started\n");
1429 case 0x0105: /* nothing running, finalise stats and report */
1430 switch(sc->mlx_background) {
1431 case MLX_BACKGROUND_CHECK:
1432 device_printf(sc->mlx_dev, "consistency check completed\n"); /* XXX print drive? */
1434 case MLX_BACKGROUND_REBUILD:
1435 device_printf(sc->mlx_dev, "drive rebuild completed\n"); /* XXX print channel/target? */
1437 case MLX_BACKGROUND_SPONTANEOUS:
1439 /* if we have previously been non-idle, report the transition */
1440 if (sc->mlx_rebuildstat.rs_code != MLX_REBUILDSTAT_IDLE) {
1441 device_printf(sc->mlx_dev, "background check/rebuild operation completed\n");
1444 sc->mlx_background = 0;
1445 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
1448 free(mc->mc_data, M_DEVBUF);
1452 /********************************************************************************
1453 ********************************************************************************
1455 ********************************************************************************
1456 ********************************************************************************/
1458 /********************************************************************************
1459 * It's time to perform a channel pause action for (sc), either start or stop
1463 mlx_pause_action(struct mlx_softc *sc)
1465 struct mlx_command *mc;
1466 int failsafe, i, command;
1468 MLX_IO_ASSERT_LOCKED(sc);
1470 /* What are we doing here? */
1471 if (sc->mlx_pause.mp_when == 0) {
1472 command = MLX_CMD_STARTCHANNEL;
1476 command = MLX_CMD_STOPCHANNEL;
1479 * Channels will always start again after the failsafe period,
1480 * which is specified in multiples of 30 seconds.
1481 * This constrains us to a maximum pause of 450 seconds.
1483 failsafe = ((sc->mlx_pause.mp_howlong - time_second) + 5) / 30;
1484 if (failsafe > 0xf) {
1486 sc->mlx_pause.mp_howlong = time_second + (0xf * 30) - 5;
1490 /* build commands for every channel requested */
1491 for (i = 0; i < sc->mlx_enq2->me_actual_channels; i++) {
1492 if ((1 << i) & sc->mlx_pause.mp_which) {
1494 /* get ourselves a command buffer */
1495 if ((mc = mlx_alloccmd(sc)) == NULL)
1497 /* get a command slot */
1498 mc->mc_flags |= MLX_CMD_PRIORITY;
1499 if (mlx_getslot(mc))
1502 /* build the command */
1503 mlx_make_type2(mc, command, (failsafe << 4) | i, 0, 0, 0, 0, 0, 0, 0);
1504 mc->mc_complete = mlx_pause_done;
1505 mc->mc_private = sc; /* XXX not needed */
1508 /* command submitted OK */
1512 device_printf(sc->mlx_dev, "%s failed for channel %d\n",
1513 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", i);
1521 mlx_pause_done(struct mlx_command *mc)
1523 struct mlx_softc *sc = mc->mc_sc;
1524 int command = mc->mc_mailbox[0];
1525 int channel = mc->mc_mailbox[2] & 0xf;
1527 MLX_IO_ASSERT_LOCKED(sc);
1528 if (mc->mc_status != 0) {
1529 device_printf(sc->mlx_dev, "%s command failed - %s\n",
1530 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", mlx_diagnose_command(mc));
1531 } else if (command == MLX_CMD_STOPCHANNEL) {
1532 device_printf(sc->mlx_dev, "channel %d pausing for %ld seconds\n",
1533 channel, (long)(sc->mlx_pause.mp_howlong - time_second));
1535 device_printf(sc->mlx_dev, "channel %d resuming\n", channel);
1540 /********************************************************************************
1541 ********************************************************************************
1543 ********************************************************************************
1544 ********************************************************************************/
1547 mlx_enquire_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1549 struct mlx_softc *sc;
1550 struct mlx_command *mc;
1552 mc = (struct mlx_command *)arg;
1556 mlx_setup_dmamap(mc, segs, nsegments, error);
1558 /* build an enquiry command */
1560 mlx_make_type2(mc, mc->mc_command, 0, 0, 0, 0, 0, 0, mc->mc_dataphys, 0);
1562 /* do we want a completion callback? */
1563 if (mc->mc_complete != NULL) {
1564 if ((error = mlx_start(mc)) != 0)
1567 /* run the command in either polled or wait mode */
1568 if ((sc->mlx_state & MLX_STATE_INTEN) ? mlx_wait_command(mc) :
1569 mlx_poll_command(mc))
1572 /* command completed OK? */
1573 if (mc->mc_status != 0) {
1574 device_printf(sc->mlx_dev, "ENQUIRY failed - %s\n",
1575 mlx_diagnose_command(mc));
1581 /********************************************************************************
1582 * Perform an Enquiry command using a type-3 command buffer and a return a single
1583 * linear result buffer. If the completion function is specified, it will
1584 * be called with the completed command (and the result response will not be
1585 * valid until that point). Otherwise, the command will either be busy-waited
1586 * for (interrupts not enabled), or slept for.
1589 mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize, void (* complete)(struct mlx_command *mc))
1591 struct mlx_command *mc;
1596 MLX_IO_ASSERT_LOCKED(sc);
1598 /* get ourselves a command buffer */
1601 if ((mc = mlx_alloccmd(sc)) == NULL)
1603 /* allocate the response structure */
1604 if ((result = malloc(bufsize, M_DEVBUF, M_NOWAIT)) == NULL)
1606 /* get a command slot */
1607 mc->mc_flags |= MLX_CMD_PRIORITY | MLX_CMD_DATAOUT;
1608 if (mlx_getslot(mc))
1611 /* map the command so the controller can see it */
1612 mc->mc_data = result;
1613 mc->mc_length = bufsize;
1614 mc->mc_command = command;
1616 if (complete != NULL) {
1617 mc->mc_complete = complete;
1618 mc->mc_private = mc;
1621 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
1622 mc->mc_length, mlx_enquire_cb, mc, BUS_DMA_NOWAIT);
1625 /* we got a command, but nobody else will free it */
1626 if ((mc != NULL) && (mc->mc_complete == NULL))
1628 /* we got an error, and we allocated a result */
1629 if ((error != 0) && (result != NULL)) {
1630 free(result, M_DEVBUF);
1637 /********************************************************************************
1638 * Perform a Flush command on the nominated controller.
1640 * May be called with interrupts enabled or disabled; will not return until
1641 * the flush operation completes or fails.
1644 mlx_flush(struct mlx_softc *sc)
1646 struct mlx_command *mc;
1650 MLX_IO_ASSERT_LOCKED(sc);
1652 /* get ourselves a command buffer */
1654 if ((mc = mlx_alloccmd(sc)) == NULL)
1656 /* get a command slot */
1657 if (mlx_getslot(mc))
1660 /* build a flush command */
1661 mlx_make_type2(mc, MLX_CMD_FLUSH, 0, 0, 0, 0, 0, 0, 0, 0);
1663 /* can't assume that interrupts are going to work here, so play it safe */
1664 if (mlx_poll_command(mc))
1667 /* command completed OK? */
1668 if (mc->mc_status != 0) {
1669 device_printf(sc->mlx_dev, "FLUSH failed - %s\n", mlx_diagnose_command(mc));
1673 error = 0; /* success */
1680 /********************************************************************************
1681 * Start a background consistency check on (drive).
1683 * May be called with interrupts enabled or disabled; will return as soon as the
1684 * operation has started or been refused.
1687 mlx_check(struct mlx_softc *sc, int drive)
1689 struct mlx_command *mc;
1693 MLX_IO_ASSERT_LOCKED(sc);
1695 /* get ourselves a command buffer */
1697 if ((mc = mlx_alloccmd(sc)) == NULL)
1699 /* get a command slot */
1700 if (mlx_getslot(mc))
1703 /* build a checkasync command, set the "fix it" flag */
1704 mlx_make_type2(mc, MLX_CMD_CHECKASYNC, 0, 0, 0, 0, 0, drive | 0x80, 0, 0);
1706 /* start the command and wait for it to be returned */
1707 if (mlx_wait_command(mc))
1710 /* command completed OK? */
1711 if (mc->mc_status != 0) {
1712 device_printf(sc->mlx_dev, "CHECK ASYNC failed - %s\n", mlx_diagnose_command(mc));
1714 device_printf(sc->mlx_sysdrive[drive].ms_disk, "consistency check started");
1716 error = mc->mc_status;
1724 /********************************************************************************
1725 * Start a background rebuild of the physical drive at (channel),(target).
1727 * May be called with interrupts enabled or disabled; will return as soon as the
1728 * operation has started or been refused.
1731 mlx_rebuild(struct mlx_softc *sc, int channel, int target)
1733 struct mlx_command *mc;
1737 MLX_IO_ASSERT_LOCKED(sc);
1739 /* get ourselves a command buffer */
1741 if ((mc = mlx_alloccmd(sc)) == NULL)
1743 /* get a command slot */
1744 if (mlx_getslot(mc))
1747 /* build a checkasync command, set the "fix it" flag */
1748 mlx_make_type2(mc, MLX_CMD_REBUILDASYNC, channel, target, 0, 0, 0, 0, 0, 0);
1750 /* start the command and wait for it to be returned */
1751 if (mlx_wait_command(mc))
1754 /* command completed OK? */
1755 if (mc->mc_status != 0) {
1756 device_printf(sc->mlx_dev, "REBUILD ASYNC failed - %s\n", mlx_diagnose_command(mc));
1758 device_printf(sc->mlx_dev, "drive rebuild started for %d:%d\n", channel, target);
1760 error = mc->mc_status;
1768 /********************************************************************************
1769 * Run the command (mc) and return when it completes.
1771 * Interrupts need to be enabled; returns nonzero on error.
1774 mlx_wait_command(struct mlx_command *mc)
1776 struct mlx_softc *sc = mc->mc_sc;
1780 MLX_IO_ASSERT_LOCKED(sc);
1782 mc->mc_complete = NULL;
1783 mc->mc_private = mc; /* wake us when you're done */
1784 if ((error = mlx_start(mc)) != 0)
1788 /* XXX better timeout? */
1789 while ((mc->mc_status == MLX_STATUS_BUSY) && (count < 30)) {
1790 mtx_sleep(mc->mc_private, &sc->mlx_io_lock, PRIBIO | PCATCH, "mlxwcmd", hz);
1793 if (mc->mc_status != 0) {
1794 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1801 /********************************************************************************
1802 * Start the command (mc) and busy-wait for it to complete.
1804 * Should only be used when interrupts can't be relied upon. Returns 0 on
1805 * success, nonzero on error.
1806 * Successfully completed commands are dequeued.
1809 mlx_poll_command(struct mlx_command *mc)
1811 struct mlx_softc *sc = mc->mc_sc;
1815 MLX_IO_ASSERT_LOCKED(sc);
1817 mc->mc_complete = NULL;
1818 mc->mc_private = NULL; /* we will poll for it */
1819 if ((error = mlx_start(mc)) != 0)
1824 /* poll for completion */
1825 mlx_done(mc->mc_sc, 1);
1827 } while ((mc->mc_status == MLX_STATUS_BUSY) && (count++ < 15000000));
1828 if (mc->mc_status != MLX_STATUS_BUSY) {
1829 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
1832 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1837 mlx_startio_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1839 struct mlx_command *mc;
1840 struct mlxd_softc *mlxd;
1841 struct mlx_softc *sc;
1847 mc = (struct mlx_command *)arg;
1848 mlx_setup_dmamap(mc, segs, nsegments, error);
1851 bp = mc->mc_private;
1853 if (bp->bio_cmd == BIO_READ) {
1854 mc->mc_flags |= MLX_CMD_DATAIN;
1855 cmd = MLX_CMD_READSG;
1857 mc->mc_flags |= MLX_CMD_DATAOUT;
1858 cmd = MLX_CMD_WRITESG;
1861 /* build a suitable I/O command (assumes 512-byte rounded transfers) */
1862 mlxd = bp->bio_disk->d_drv1;
1863 driveno = mlxd->mlxd_drive - sc->mlx_sysdrive;
1864 blkcount = (bp->bio_bcount + MLX_BLKSIZE - 1) / MLX_BLKSIZE;
1866 if ((bp->bio_pblkno + blkcount) > sc->mlx_sysdrive[driveno].ms_size)
1867 device_printf(sc->mlx_dev,
1868 "I/O beyond end of unit (%lld,%d > %lu)\n",
1869 (long long)bp->bio_pblkno, blkcount,
1870 (u_long)sc->mlx_sysdrive[driveno].ms_size);
1873 * Build the I/O command. Note that the SG list type bits are set to zero,
1874 * denoting the format of SG list that we are using.
1876 if (sc->mlx_iftype == MLX_IFTYPE_2) {
1877 mlx_make_type1(mc, (cmd == MLX_CMD_WRITESG) ? MLX_CMD_WRITESG_OLD :
1879 blkcount & 0xff, /* xfer length low byte */
1880 bp->bio_pblkno, /* physical block number */
1881 driveno, /* target drive number */
1882 mc->mc_sgphys, /* location of SG list */
1883 mc->mc_nsgent & 0x3f); /* size of SG list */
1885 mlx_make_type5(mc, cmd,
1886 blkcount & 0xff, /* xfer length low byte */
1887 (driveno << 3) | ((blkcount >> 8) & 0x07),
1888 /* target+length high 3 bits */
1889 bp->bio_pblkno, /* physical block number */
1890 mc->mc_sgphys, /* location of SG list */
1891 mc->mc_nsgent & 0x3f); /* size of SG list */
1894 /* try to give command to controller */
1895 if (mlx_start(mc) != 0) {
1896 /* fail the command */
1897 mc->mc_status = MLX_STATUS_WEDGED;
1901 sc->mlx_state &= ~MLX_STATE_QFROZEN;
1904 /********************************************************************************
1905 * Pull as much work off the softc's work queue as possible and give it to the
1906 * controller. Leave a couple of slots free for emergencies.
1909 mlx_startio(struct mlx_softc *sc)
1911 struct mlx_command *mc;
1915 MLX_IO_ASSERT_LOCKED(sc);
1917 /* spin until something prevents us from doing any work */
1919 if (sc->mlx_state & MLX_STATE_QFROZEN)
1922 /* see if there's work to be done */
1923 if ((bp = bioq_first(&sc->mlx_bioq)) == NULL)
1926 if ((mc = mlx_alloccmd(sc)) == NULL)
1928 /* get a slot for the command */
1929 if (mlx_getslot(mc) != 0) {
1933 /* get the buf containing our work */
1934 bioq_remove(&sc->mlx_bioq, bp);
1937 /* connect the buf to the command */
1938 mc->mc_complete = mlx_completeio;
1939 mc->mc_private = bp;
1940 mc->mc_data = bp->bio_data;
1941 mc->mc_length = bp->bio_bcount;
1943 /* map the command so the controller can work with it */
1944 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
1945 mc->mc_length, mlx_startio_cb, mc, 0);
1946 if (error == EINPROGRESS) {
1947 sc->mlx_state |= MLX_STATE_QFROZEN;
1953 /********************************************************************************
1954 * Handle completion of an I/O command.
1957 mlx_completeio(struct mlx_command *mc)
1959 struct mlx_softc *sc = mc->mc_sc;
1960 struct bio *bp = mc->mc_private;
1961 struct mlxd_softc *mlxd = bp->bio_disk->d_drv1;
1963 MLX_IO_ASSERT_LOCKED(sc);
1964 if (mc->mc_status != MLX_STATUS_OK) { /* could be more verbose here? */
1965 bp->bio_error = EIO;
1966 bp->bio_flags |= BIO_ERROR;
1968 switch(mc->mc_status) {
1969 case MLX_STATUS_RDWROFFLINE: /* system drive has gone offline */
1970 device_printf(mlxd->mlxd_dev, "drive offline\n");
1971 /* should signal this with a return code */
1972 mlxd->mlxd_drive->ms_state = MLX_SYSD_OFFLINE;
1975 default: /* other I/O error */
1976 device_printf(sc->mlx_dev, "I/O error - %s\n", mlx_diagnose_command(mc));
1978 device_printf(sc->mlx_dev, " b_bcount %ld blkcount %ld b_pblkno %d\n",
1979 bp->bio_bcount, bp->bio_bcount / MLX_BLKSIZE, bp->bio_pblkno);
1980 device_printf(sc->mlx_dev, " %13D\n", mc->mc_mailbox, " ");
1990 mlx_user_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1992 struct mlx_usercommand *mu;
1993 struct mlx_command *mc;
1994 struct mlx_dcdb *dcdb;
1996 mc = (struct mlx_command *)arg;
2000 mlx_setup_dmamap(mc, segs, nsegments, error);
2002 mu = (struct mlx_usercommand *)mc->mc_private;
2006 * If this is a passthrough SCSI command, the DCDB is packed at the
2007 * beginning of the data area. Fix up the DCDB to point to the correct
2008 * physical address and override any bufptr supplied by the caller since
2009 * we know what it's meant to be.
2011 if (mc->mc_mailbox[0] == MLX_CMD_DIRECT_CDB) {
2012 dcdb = (struct mlx_dcdb *)mc->mc_data;
2013 dcdb->dcdb_physaddr = mc->mc_dataphys + sizeof(*dcdb);
2018 * If there's a data buffer, fix up the command's buffer pointer.
2020 if (mu->mu_datasize > 0) {
2021 mc->mc_mailbox[mu->mu_bufptr ] = mc->mc_dataphys & 0xff;
2022 mc->mc_mailbox[mu->mu_bufptr + 1] = (mc->mc_dataphys >> 8) & 0xff;
2023 mc->mc_mailbox[mu->mu_bufptr + 2] = (mc->mc_dataphys >> 16) & 0xff;
2024 mc->mc_mailbox[mu->mu_bufptr + 3] = (mc->mc_dataphys >> 24) & 0xff;
2026 debug(0, "command fixup");
2028 /* submit the command and wait */
2029 if (mlx_wait_command(mc) != 0)
2034 /********************************************************************************
2035 * Take a command from user-space and try to run it.
2037 * XXX Note that this can't perform very much in the way of error checking, and
2038 * as such, applications _must_ be considered trustworthy.
2039 * XXX Commands using S/G for data are not supported.
2042 mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu)
2044 struct mlx_command *mc;
2054 /* get ourselves a command and copy in from user space */
2056 if ((mc = mlx_alloccmd(sc)) == NULL) {
2060 bcopy(mu->mu_command, mc->mc_mailbox, sizeof(mc->mc_mailbox));
2061 debug(0, "got command buffer");
2064 * if we need a buffer for data transfer, allocate one and copy in its
2067 if (mu->mu_datasize > 0) {
2068 if (mu->mu_datasize > MLX_MAXPHYS) {
2073 if (((kbuf = malloc(mu->mu_datasize, M_DEVBUF, M_WAITOK)) == NULL) ||
2074 (error = copyin(mu->mu_buf, kbuf, mu->mu_datasize))) {
2079 debug(0, "got kernel buffer");
2082 /* get a command slot */
2083 if (mlx_getslot(mc))
2085 debug(0, "got a slot");
2087 if (mu->mu_datasize > 0) {
2089 /* range check the pointer to physical buffer address */
2090 if ((mu->mu_bufptr < 0) || (mu->mu_bufptr > (sizeof(mu->mu_command) -
2091 sizeof(u_int32_t)))) {
2097 /* map the command so the controller can see it */
2099 mc->mc_length = mu->mu_datasize;
2100 mc->mc_private = mu;
2101 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
2102 mc->mc_length, mlx_user_cb, mc, BUS_DMA_NOWAIT);
2106 /* copy out status and data */
2107 mu->mu_status = mc->mc_status;
2108 if (mu->mu_datasize > 0) {
2110 error = copyout(kbuf, mu->mu_buf, mu->mu_datasize);
2118 free(kbuf, M_DEVBUF);
2122 /********************************************************************************
2123 ********************************************************************************
2124 Command I/O to Controller
2125 ********************************************************************************
2126 ********************************************************************************/
2128 /********************************************************************************
2129 * Find a free command slot for (mc).
2131 * Don't hand out a slot to a normal-priority command unless there are at least
2132 * 4 slots free for priority commands.
2135 mlx_getslot(struct mlx_command *mc)
2137 struct mlx_softc *sc = mc->mc_sc;
2142 MLX_IO_ASSERT_LOCKED(sc);
2145 * Enforce slot-usage limit, if we have the required information.
2147 if (sc->mlx_enq2 != NULL) {
2148 limit = sc->mlx_enq2->me_max_commands;
2152 if (sc->mlx_busycmds >= ((mc->mc_flags & MLX_CMD_PRIORITY) ? limit : limit - 4))
2156 * Allocate an outstanding command slot
2158 * XXX linear search is slow
2160 for (slot = 0; slot < limit; slot++) {
2161 debug(2, "try slot %d", slot);
2162 if (sc->mlx_busycmd[slot] == NULL)
2166 sc->mlx_busycmd[slot] = mc;
2174 debug(2, "got slot %d", slot);
2179 /********************************************************************************
2180 * Map/unmap (mc)'s data in the controller's addressable space.
2183 mlx_setup_dmamap(struct mlx_command *mc, bus_dma_segment_t *segs, int nsegments,
2186 struct mlx_softc *sc = mc->mc_sc;
2187 struct mlx_sgentry *sg;
2192 /* XXX should be unnecessary */
2193 if (sc->mlx_enq2 && (nsegments > sc->mlx_enq2->me_max_sg))
2194 panic("MLX: too many s/g segments (%d, max %d)", nsegments,
2195 sc->mlx_enq2->me_max_sg);
2197 /* get base address of s/g table */
2198 sg = sc->mlx_sgtable + (mc->mc_slot * MLX_NSEG);
2200 /* save s/g table information in command */
2201 mc->mc_nsgent = nsegments;
2202 mc->mc_sgphys = sc->mlx_sgbusaddr +
2203 (mc->mc_slot * MLX_NSEG * sizeof(struct mlx_sgentry));
2204 mc->mc_dataphys = segs[0].ds_addr;
2206 /* populate s/g table */
2207 for (i = 0; i < nsegments; i++, sg++) {
2208 sg->sg_addr = segs[i].ds_addr;
2209 sg->sg_count = segs[i].ds_len;
2212 /* Make sure the buffers are visible on the bus. */
2213 if (mc->mc_flags & MLX_CMD_DATAIN)
2214 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap,
2215 BUS_DMASYNC_PREREAD);
2216 if (mc->mc_flags & MLX_CMD_DATAOUT)
2217 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap,
2218 BUS_DMASYNC_PREWRITE);
2222 mlx_unmapcmd(struct mlx_command *mc)
2224 struct mlx_softc *sc = mc->mc_sc;
2228 /* if the command involved data at all */
2229 if (mc->mc_data != NULL) {
2231 if (mc->mc_flags & MLX_CMD_DATAIN)
2232 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTREAD);
2233 if (mc->mc_flags & MLX_CMD_DATAOUT)
2234 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTWRITE);
2236 bus_dmamap_unload(sc->mlx_buffer_dmat, mc->mc_dmamap);
2240 /********************************************************************************
2241 * Try to deliver (mc) to the controller.
2243 * Can be called at any interrupt level, with or without interrupts enabled.
2246 mlx_start(struct mlx_command *mc)
2248 struct mlx_softc *sc = mc->mc_sc;
2253 /* save the slot number as ident so we can handle this command when complete */
2254 mc->mc_mailbox[0x1] = mc->mc_slot;
2256 /* mark the command as currently being processed */
2257 mc->mc_status = MLX_STATUS_BUSY;
2259 /* set a default 60-second timeout XXX tunable? XXX not currently used */
2260 mc->mc_timeout = time_second + 60;
2262 /* spin waiting for the mailbox */
2263 for (i = 100000; i > 0; i--) {
2264 if (sc->mlx_tryqueue(sc, mc)) {
2265 /* move command to work queue */
2266 TAILQ_INSERT_TAIL(&sc->mlx_work, mc, mc_link);
2273 * We couldn't get the controller to take the command. Revoke the slot
2274 * that the command was given and return it with a bad status.
2276 sc->mlx_busycmd[mc->mc_slot] = NULL;
2277 device_printf(sc->mlx_dev, "controller wedged (not taking commands)\n");
2278 mc->mc_status = MLX_STATUS_WEDGED;
2283 /********************************************************************************
2284 * Poll the controller (sc) for completed commands.
2285 * Update command status and free slots for reuse. If any slots were freed,
2286 * new commands may be posted.
2288 * Returns nonzero if one or more commands were completed.
2291 mlx_done(struct mlx_softc *sc, int startio)
2293 struct mlx_command *mc;
2299 MLX_IO_ASSERT_LOCKED(sc);
2303 /* loop collecting completed commands */
2305 /* poll for a completed command's identifier and status */
2306 if (sc->mlx_findcomplete(sc, &slot, &status)) {
2308 mc = sc->mlx_busycmd[slot]; /* find command */
2309 if (mc != NULL) { /* paranoia */
2310 if (mc->mc_status == MLX_STATUS_BUSY) {
2311 mc->mc_status = status; /* save status */
2313 /* free slot for reuse */
2314 sc->mlx_busycmd[slot] = NULL;
2317 device_printf(sc->mlx_dev, "duplicate done event for slot %d\n", slot);
2320 device_printf(sc->mlx_dev, "done event for nonbusy slot %d\n", slot);
2327 /* if we've completed any commands, try posting some more */
2328 if (result && startio)
2331 /* handle completion and timeouts */
2337 /********************************************************************************
2338 * Perform post-completion processing for commands on (sc).
2341 mlx_complete(struct mlx_softc *sc)
2343 struct mlx_command *mc, *nc;
2346 MLX_IO_ASSERT_LOCKED(sc);
2348 /* scan the list of busy/done commands */
2349 mc = TAILQ_FIRST(&sc->mlx_work);
2350 while (mc != NULL) {
2351 nc = TAILQ_NEXT(mc, mc_link);
2353 /* Command has been completed in some fashion */
2354 if (mc->mc_status != MLX_STATUS_BUSY) {
2356 /* unmap the command's data buffer */
2359 * Does the command have a completion handler?
2361 if (mc->mc_complete != NULL) {
2362 /* remove from list and give to handler */
2363 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2364 mc->mc_complete(mc);
2367 * Is there a sleeper waiting on this command?
2369 } else if (mc->mc_private != NULL) { /* sleeping caller wants to know about it */
2371 /* remove from list and wake up sleeper */
2372 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2373 wakeup_one(mc->mc_private);
2376 * Leave the command for a caller that's polling for it.
2385 /********************************************************************************
2386 ********************************************************************************
2387 Command Buffer Management
2388 ********************************************************************************
2389 ********************************************************************************/
2391 /********************************************************************************
2392 * Get a new command buffer.
2394 * This may return NULL in low-memory cases.
2396 * Note that using malloc() is expensive (the command buffer is << 1 page) but
2397 * necessary if we are to be a loadable module before the zone allocator is fixed.
2399 * If possible, we recycle a command buffer that's been used before.
2401 * XXX Note that command buffers are not cleaned out - it is the caller's
2402 * responsibility to ensure that all required fields are filled in before
2405 static struct mlx_command *
2406 mlx_alloccmd(struct mlx_softc *sc)
2408 struct mlx_command *mc;
2413 MLX_IO_ASSERT_LOCKED(sc);
2414 if ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL)
2415 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
2417 /* allocate a new command buffer? */
2419 mc = (struct mlx_command *)malloc(sizeof(*mc), M_DEVBUF, M_NOWAIT | M_ZERO);
2422 error = bus_dmamap_create(sc->mlx_buffer_dmat, 0, &mc->mc_dmamap);
2432 /********************************************************************************
2433 * Release a command buffer for recycling.
2435 * XXX It might be a good idea to limit the number of commands we save for reuse
2436 * if it's shown that this list bloats out massively.
2439 mlx_releasecmd(struct mlx_command *mc)
2444 MLX_IO_ASSERT_LOCKED(mc->mc_sc);
2445 TAILQ_INSERT_HEAD(&mc->mc_sc->mlx_freecmds, mc, mc_link);
2448 /********************************************************************************
2449 * Permanently discard a command buffer.
2452 mlx_freecmd(struct mlx_command *mc)
2454 struct mlx_softc *sc = mc->mc_sc;
2457 bus_dmamap_destroy(sc->mlx_buffer_dmat, mc->mc_dmamap);
2462 /********************************************************************************
2463 ********************************************************************************
2464 Type 3 interface accessor methods
2465 ********************************************************************************
2466 ********************************************************************************/
2468 /********************************************************************************
2469 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2470 * (the controller is not ready to take a command).
2473 mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2478 MLX_IO_ASSERT_LOCKED(sc);
2480 /* ready for our command? */
2481 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_FULL)) {
2482 /* copy mailbox data to window */
2483 for (i = 0; i < 13; i++)
2484 MLX_V3_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2487 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_FULL);
2493 /********************************************************************************
2494 * See if a command has been completed, if so acknowledge its completion
2495 * and recover the slot number and status code.
2498 mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2502 MLX_IO_ASSERT_LOCKED(sc);
2504 /* status available? */
2505 if (MLX_V3_GET_ODBR(sc) & MLX_V3_ODB_SAVAIL) {
2506 *slot = MLX_V3_GET_STATUS_IDENT(sc); /* get command identifier */
2507 *status = MLX_V3_GET_STATUS(sc); /* get status */
2509 /* acknowledge completion */
2510 MLX_V3_PUT_ODBR(sc, MLX_V3_ODB_SAVAIL);
2511 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2517 /********************************************************************************
2518 * Enable/disable interrupts as requested. (No acknowledge required)
2521 mlx_v3_intaction(struct mlx_softc *sc, int action)
2524 MLX_IO_ASSERT_LOCKED(sc);
2527 case MLX_INTACTION_DISABLE:
2528 MLX_V3_PUT_IER(sc, 0);
2529 sc->mlx_state &= ~MLX_STATE_INTEN;
2531 case MLX_INTACTION_ENABLE:
2532 MLX_V3_PUT_IER(sc, 1);
2533 sc->mlx_state |= MLX_STATE_INTEN;
2538 /********************************************************************************
2539 * Poll for firmware error codes during controller initialisation.
2540 * Returns 0 if initialisation is complete, 1 if still in progress but no
2541 * error has been fetched, 2 if an error has been retrieved.
2544 mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
2551 /* first time around, clear any hardware completion status */
2553 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2557 /* init in progress? */
2558 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_INIT_BUSY))
2561 /* test error value */
2562 fwerror = MLX_V3_GET_FWERROR(sc);
2563 if (!(fwerror & MLX_V3_FWERROR_PEND))
2566 /* mask status pending bit, fetch status */
2567 *error = fwerror & ~MLX_V3_FWERROR_PEND;
2568 *param1 = MLX_V3_GET_FWERROR_PARAM1(sc);
2569 *param2 = MLX_V3_GET_FWERROR_PARAM2(sc);
2572 MLX_V3_PUT_FWERROR(sc, 0);
2577 /********************************************************************************
2578 ********************************************************************************
2579 Type 4 interface accessor methods
2580 ********************************************************************************
2581 ********************************************************************************/
2583 /********************************************************************************
2584 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2585 * (the controller is not ready to take a command).
2588 mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2593 MLX_IO_ASSERT_LOCKED(sc);
2595 /* ready for our command? */
2596 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_FULL)) {
2597 /* copy mailbox data to window */
2598 for (i = 0; i < 13; i++)
2599 MLX_V4_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2601 /* memory-mapped controller, so issue a write barrier to ensure the mailbox is filled */
2602 bus_barrier(sc->mlx_mem, MLX_V4_MAILBOX, MLX_V4_MAILBOX_LENGTH,
2603 BUS_SPACE_BARRIER_WRITE);
2606 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_HWMBOX_CMD);
2612 /********************************************************************************
2613 * See if a command has been completed, if so acknowledge its completion
2614 * and recover the slot number and status code.
2617 mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2621 MLX_IO_ASSERT_LOCKED(sc);
2623 /* status available? */
2624 if (MLX_V4_GET_ODBR(sc) & MLX_V4_ODB_HWSAVAIL) {
2625 *slot = MLX_V4_GET_STATUS_IDENT(sc); /* get command identifier */
2626 *status = MLX_V4_GET_STATUS(sc); /* get status */
2628 /* acknowledge completion */
2629 MLX_V4_PUT_ODBR(sc, MLX_V4_ODB_HWMBOX_ACK);
2630 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2636 /********************************************************************************
2637 * Enable/disable interrupts as requested.
2640 mlx_v4_intaction(struct mlx_softc *sc, int action)
2643 MLX_IO_ASSERT_LOCKED(sc);
2646 case MLX_INTACTION_DISABLE:
2647 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK | MLX_V4_IER_DISINT);
2648 sc->mlx_state &= ~MLX_STATE_INTEN;
2650 case MLX_INTACTION_ENABLE:
2651 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK & ~MLX_V4_IER_DISINT);
2652 sc->mlx_state |= MLX_STATE_INTEN;
2657 /********************************************************************************
2658 * Poll for firmware error codes during controller initialisation.
2659 * Returns 0 if initialisation is complete, 1 if still in progress but no
2660 * error has been fetched, 2 if an error has been retrieved.
2663 mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
2670 /* first time around, clear any hardware completion status */
2672 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2676 /* init in progress? */
2677 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_INIT_BUSY))
2680 /* test error value */
2681 fwerror = MLX_V4_GET_FWERROR(sc);
2682 if (!(fwerror & MLX_V4_FWERROR_PEND))
2685 /* mask status pending bit, fetch status */
2686 *error = fwerror & ~MLX_V4_FWERROR_PEND;
2687 *param1 = MLX_V4_GET_FWERROR_PARAM1(sc);
2688 *param2 = MLX_V4_GET_FWERROR_PARAM2(sc);
2691 MLX_V4_PUT_FWERROR(sc, 0);
2696 /********************************************************************************
2697 ********************************************************************************
2698 Type 5 interface accessor methods
2699 ********************************************************************************
2700 ********************************************************************************/
2702 /********************************************************************************
2703 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2704 * (the controller is not ready to take a command).
2707 mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2712 MLX_IO_ASSERT_LOCKED(sc);
2714 /* ready for our command? */
2715 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_EMPTY) {
2716 /* copy mailbox data to window */
2717 for (i = 0; i < 13; i++)
2718 MLX_V5_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2721 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_HWMBOX_CMD);
2727 /********************************************************************************
2728 * See if a command has been completed, if so acknowledge its completion
2729 * and recover the slot number and status code.
2732 mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2736 MLX_IO_ASSERT_LOCKED(sc);
2738 /* status available? */
2739 if (MLX_V5_GET_ODBR(sc) & MLX_V5_ODB_HWSAVAIL) {
2740 *slot = MLX_V5_GET_STATUS_IDENT(sc); /* get command identifier */
2741 *status = MLX_V5_GET_STATUS(sc); /* get status */
2743 /* acknowledge completion */
2744 MLX_V5_PUT_ODBR(sc, MLX_V5_ODB_HWMBOX_ACK);
2745 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2751 /********************************************************************************
2752 * Enable/disable interrupts as requested.
2755 mlx_v5_intaction(struct mlx_softc *sc, int action)
2758 MLX_IO_ASSERT_LOCKED(sc);
2761 case MLX_INTACTION_DISABLE:
2762 MLX_V5_PUT_IER(sc, 0xff & MLX_V5_IER_DISINT);
2763 sc->mlx_state &= ~MLX_STATE_INTEN;
2765 case MLX_INTACTION_ENABLE:
2766 MLX_V5_PUT_IER(sc, 0xff & ~MLX_V5_IER_DISINT);
2767 sc->mlx_state |= MLX_STATE_INTEN;
2772 /********************************************************************************
2773 * Poll for firmware error codes during controller initialisation.
2774 * Returns 0 if initialisation is complete, 1 if still in progress but no
2775 * error has been fetched, 2 if an error has been retrieved.
2778 mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
2785 /* first time around, clear any hardware completion status */
2787 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2791 /* init in progress? */
2792 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_INIT_DONE)
2795 /* test for error value */
2796 fwerror = MLX_V5_GET_FWERROR(sc);
2797 if (!(fwerror & MLX_V5_FWERROR_PEND))
2800 /* mask status pending bit, fetch status */
2801 *error = fwerror & ~MLX_V5_FWERROR_PEND;
2802 *param1 = MLX_V5_GET_FWERROR_PARAM1(sc);
2803 *param2 = MLX_V5_GET_FWERROR_PARAM2(sc);
2806 MLX_V5_PUT_FWERROR(sc, 0xff);
2811 /********************************************************************************
2812 ********************************************************************************
2814 ********************************************************************************
2815 ********************************************************************************/
2817 /********************************************************************************
2818 * Return a status message describing (mc)
2820 static char *mlx_status_messages[] = {
2821 "normal completion", /* 00 */
2822 "irrecoverable data error", /* 01 */
2823 "drive does not exist, or is offline", /* 02 */
2824 "attempt to write beyond end of drive", /* 03 */
2825 "bad data encountered", /* 04 */
2826 "invalid log entry request", /* 05 */
2827 "attempt to rebuild online drive", /* 06 */
2828 "new disk failed during rebuild", /* 07 */
2829 "invalid channel/target", /* 08 */
2830 "rebuild/check already in progress", /* 09 */
2831 "one or more disks are dead", /* 10 */
2832 "invalid or non-redundant drive", /* 11 */
2833 "channel is busy", /* 12 */
2834 "channel is not stopped", /* 13 */
2835 "rebuild successfully terminated", /* 14 */
2836 "unsupported command", /* 15 */
2837 "check condition received", /* 16 */
2838 "device is busy", /* 17 */
2839 "selection or command timeout", /* 18 */
2840 "command terminated abnormally", /* 19 */
2849 } mlx_messages[] = {
2850 {MLX_CMD_READSG, 0x0001, 1},
2851 {MLX_CMD_READSG, 0x0002, 1},
2852 {MLX_CMD_READSG, 0x0105, 3},
2853 {MLX_CMD_READSG, 0x010c, 4},
2854 {MLX_CMD_WRITESG, 0x0001, 1},
2855 {MLX_CMD_WRITESG, 0x0002, 1},
2856 {MLX_CMD_WRITESG, 0x0105, 3},
2857 {MLX_CMD_READSG_OLD, 0x0001, 1},
2858 {MLX_CMD_READSG_OLD, 0x0002, 1},
2859 {MLX_CMD_READSG_OLD, 0x0105, 3},
2860 {MLX_CMD_WRITESG_OLD, 0x0001, 1},
2861 {MLX_CMD_WRITESG_OLD, 0x0002, 1},
2862 {MLX_CMD_WRITESG_OLD, 0x0105, 3},
2863 {MLX_CMD_LOGOP, 0x0105, 5},
2864 {MLX_CMD_REBUILDASYNC, 0x0002, 6},
2865 {MLX_CMD_REBUILDASYNC, 0x0004, 7},
2866 {MLX_CMD_REBUILDASYNC, 0x0105, 8},
2867 {MLX_CMD_REBUILDASYNC, 0x0106, 9},
2868 {MLX_CMD_REBUILDASYNC, 0x0107, 14},
2869 {MLX_CMD_CHECKASYNC, 0x0002, 10},
2870 {MLX_CMD_CHECKASYNC, 0x0105, 11},
2871 {MLX_CMD_CHECKASYNC, 0x0106, 9},
2872 {MLX_CMD_STOPCHANNEL, 0x0106, 12},
2873 {MLX_CMD_STOPCHANNEL, 0x0105, 8},
2874 {MLX_CMD_STARTCHANNEL, 0x0005, 13},
2875 {MLX_CMD_STARTCHANNEL, 0x0105, 8},
2876 {MLX_CMD_DIRECT_CDB, 0x0002, 16},
2877 {MLX_CMD_DIRECT_CDB, 0x0008, 17},
2878 {MLX_CMD_DIRECT_CDB, 0x000e, 18},
2879 {MLX_CMD_DIRECT_CDB, 0x000f, 19},
2880 {MLX_CMD_DIRECT_CDB, 0x0105, 8},
2887 mlx_diagnose_command(struct mlx_command *mc)
2889 static char unkmsg[80];
2892 /* look up message in table */
2893 for (i = 0; mlx_messages[i].command != -1; i++)
2894 if (((mc->mc_mailbox[0] == mlx_messages[i].command) || (mlx_messages[i].command == 0)) &&
2895 (mc->mc_status == mlx_messages[i].status))
2896 return(mlx_status_messages[mlx_messages[i].msg]);
2898 sprintf(unkmsg, "unknown response 0x%x for command 0x%x", (int)mc->mc_status, (int)mc->mc_mailbox[0]);
2902 /*******************************************************************************
2903 * Print a string describing the controller (sc)
2909 } mlx_controller_names[] = {
2924 mlx_describe_controller(struct mlx_softc *sc)
2926 static char buf[80];
2930 for (i = 0, model = NULL; mlx_controller_names[i].name != NULL; i++) {
2931 if ((sc->mlx_enq2->me_hardware_id & 0xff) == mlx_controller_names[i].hwid) {
2932 model = mlx_controller_names[i].name;
2936 if (model == NULL) {
2937 sprintf(buf, " model 0x%x", sc->mlx_enq2->me_hardware_id & 0xff);
2940 device_printf(sc->mlx_dev, "DAC%s, %d channel%s, firmware %d.%02d-%c-%02d, %dMB RAM\n",
2942 sc->mlx_enq2->me_actual_channels,
2943 sc->mlx_enq2->me_actual_channels > 1 ? "s" : "",
2944 sc->mlx_enq2->me_firmware_id & 0xff,
2945 (sc->mlx_enq2->me_firmware_id >> 8) & 0xff,
2946 (sc->mlx_enq2->me_firmware_id >> 24) & 0xff,
2947 (sc->mlx_enq2->me_firmware_id >> 16) & 0xff,
2948 sc->mlx_enq2->me_mem_size / (1024 * 1024));
2951 device_printf(sc->mlx_dev, " Hardware ID 0x%08x\n", sc->mlx_enq2->me_hardware_id);
2952 device_printf(sc->mlx_dev, " Firmware ID 0x%08x\n", sc->mlx_enq2->me_firmware_id);
2953 device_printf(sc->mlx_dev, " Configured/Actual channels %d/%d\n", sc->mlx_enq2->me_configured_channels,
2954 sc->mlx_enq2->me_actual_channels);
2955 device_printf(sc->mlx_dev, " Max Targets %d\n", sc->mlx_enq2->me_max_targets);
2956 device_printf(sc->mlx_dev, " Max Tags %d\n", sc->mlx_enq2->me_max_tags);
2957 device_printf(sc->mlx_dev, " Max System Drives %d\n", sc->mlx_enq2->me_max_sys_drives);
2958 device_printf(sc->mlx_dev, " Max Arms %d\n", sc->mlx_enq2->me_max_arms);
2959 device_printf(sc->mlx_dev, " Max Spans %d\n", sc->mlx_enq2->me_max_spans);
2960 device_printf(sc->mlx_dev, " DRAM/cache/flash/NVRAM size %d/%d/%d/%d\n", sc->mlx_enq2->me_mem_size,
2961 sc->mlx_enq2->me_cache_size, sc->mlx_enq2->me_flash_size, sc->mlx_enq2->me_nvram_size);
2962 device_printf(sc->mlx_dev, " DRAM type %d\n", sc->mlx_enq2->me_mem_type);
2963 device_printf(sc->mlx_dev, " Clock Speed %dns\n", sc->mlx_enq2->me_clock_speed);
2964 device_printf(sc->mlx_dev, " Hardware Speed %dns\n", sc->mlx_enq2->me_hardware_speed);
2965 device_printf(sc->mlx_dev, " Max Commands %d\n", sc->mlx_enq2->me_max_commands);
2966 device_printf(sc->mlx_dev, " Max SG Entries %d\n", sc->mlx_enq2->me_max_sg);
2967 device_printf(sc->mlx_dev, " Max DP %d\n", sc->mlx_enq2->me_max_dp);
2968 device_printf(sc->mlx_dev, " Max IOD %d\n", sc->mlx_enq2->me_max_iod);
2969 device_printf(sc->mlx_dev, " Max Comb %d\n", sc->mlx_enq2->me_max_comb);
2970 device_printf(sc->mlx_dev, " Latency %ds\n", sc->mlx_enq2->me_latency);
2971 device_printf(sc->mlx_dev, " SCSI Timeout %ds\n", sc->mlx_enq2->me_scsi_timeout);
2972 device_printf(sc->mlx_dev, " Min Free Lines %d\n", sc->mlx_enq2->me_min_freelines);
2973 device_printf(sc->mlx_dev, " Rate Constant %d\n", sc->mlx_enq2->me_rate_const);
2974 device_printf(sc->mlx_dev, " MAXBLK %d\n", sc->mlx_enq2->me_maxblk);
2975 device_printf(sc->mlx_dev, " Blocking Factor %d sectors\n", sc->mlx_enq2->me_blocking_factor);
2976 device_printf(sc->mlx_dev, " Cache Line Size %d blocks\n", sc->mlx_enq2->me_cacheline);
2977 device_printf(sc->mlx_dev, " SCSI Capability %s%dMHz, %d bit\n",
2978 sc->mlx_enq2->me_scsi_cap & (1<<4) ? "differential " : "",
2979 (1 << ((sc->mlx_enq2->me_scsi_cap >> 2) & 3)) * 10,
2980 8 << (sc->mlx_enq2->me_scsi_cap & 0x3));
2981 device_printf(sc->mlx_dev, " Firmware Build Number %d\n", sc->mlx_enq2->me_firmware_build);
2982 device_printf(sc->mlx_dev, " Fault Management Type %d\n", sc->mlx_enq2->me_fault_mgmt_type);
2983 device_printf(sc->mlx_dev, " Features %b\n", sc->mlx_enq2->me_firmware_features,
2984 "\20\4Background Init\3Read Ahead\2MORE\1Cluster\n");
2989 /*******************************************************************************
2990 * Emit a string describing the firmware handshake status code, and return a flag
2991 * indicating whether the code represents a fatal error.
2993 * Error code interpretations are from the Linux driver, and don't directly match
2994 * the messages printed by Mylex's BIOS. This may change if documentation on the
2995 * codes is forthcoming.
2998 mlx_fw_message(struct mlx_softc *sc, int error, int param1, int param2)
3002 device_printf(sc->mlx_dev, "physical drive %d:%d not responding\n", param2, param1);
3005 /* we could be neater about this and give some indication when we receive more of them */
3006 if (!(sc->mlx_flags & MLX_SPINUP_REPORTED)) {
3007 device_printf(sc->mlx_dev, "spinning up drives...\n");
3008 sc->mlx_flags |= MLX_SPINUP_REPORTED;
3012 device_printf(sc->mlx_dev, "configuration checksum error\n");
3015 device_printf(sc->mlx_dev, "mirror race recovery failed\n");
3018 device_printf(sc->mlx_dev, "mirror race recovery in progress\n");
3021 device_printf(sc->mlx_dev, "physical drive %d:%d COD mismatch\n", param2, param1);
3024 device_printf(sc->mlx_dev, "logical drive installation aborted\n");
3027 device_printf(sc->mlx_dev, "mirror race on a critical system drive\n");
3030 device_printf(sc->mlx_dev, "new controller configuration found\n");
3033 device_printf(sc->mlx_dev, "FATAL MEMORY PARITY ERROR\n");
3036 device_printf(sc->mlx_dev, "unknown firmware initialisation error %02x:%02x:%02x\n", error, param1, param2);
3042 /********************************************************************************
3043 ********************************************************************************
3045 ********************************************************************************
3046 ********************************************************************************/
3048 /********************************************************************************
3049 * Find the disk whose unit number is (unit) on this controller
3051 static struct mlx_sysdrive *
3052 mlx_findunit(struct mlx_softc *sc, int unit)
3056 /* search system drives */
3057 MLX_CONFIG_ASSERT_LOCKED(sc);
3058 for (i = 0; i < MLX_MAXDRIVES; i++) {
3059 /* is this one attached? */
3060 if (sc->mlx_sysdrive[i].ms_disk != 0) {
3061 /* is this the one? */
3062 if (unit == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
3063 return(&sc->mlx_sysdrive[i]);