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 $
30 * Driver for the Mylex DAC960 family of RAID controllers.
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
39 #include <sys/devicestat.h>
43 #include <sys/thread2.h>
45 #include <machine/clock.h>
47 #include "mlx_compat.h"
52 static struct dev_ops mlx_ops = {
59 devclass_t mlx_devclass;
62 * Per-interface accessor methods
64 static int mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
65 static int mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
66 static void mlx_v3_intaction(struct mlx_softc *sc, int action);
67 static int mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2);
69 static int mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
70 static int mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
71 static void mlx_v4_intaction(struct mlx_softc *sc, int action);
72 static int mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2);
74 static int mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
75 static int mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
76 static void mlx_v5_intaction(struct mlx_softc *sc, int action);
77 static int mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2);
82 static void mlx_periodic(void *data);
83 static void mlx_periodic_enquiry(struct mlx_command *mc);
84 static void mlx_periodic_eventlog_poll(struct mlx_softc *sc);
85 static void mlx_periodic_eventlog_respond(struct mlx_command *mc);
86 static void mlx_periodic_rebuild(struct mlx_command *mc);
91 static void mlx_pause_action(struct mlx_softc *sc);
92 static void mlx_pause_done(struct mlx_command *mc);
97 static void *mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize,
98 void (*complete)(struct mlx_command *mc));
99 static int mlx_flush(struct mlx_softc *sc);
100 static int mlx_check(struct mlx_softc *sc, int drive);
101 static int mlx_rebuild(struct mlx_softc *sc, int channel, int target);
102 static int mlx_wait_command(struct mlx_command *mc);
103 static int mlx_poll_command(struct mlx_command *mc);
104 static void mlx_startio(struct mlx_softc *sc);
105 static void mlx_completeio(struct mlx_command *mc);
106 static int mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu);
109 * Command buffer allocation.
111 static struct mlx_command *mlx_alloccmd(struct mlx_softc *sc);
112 static void mlx_releasecmd(struct mlx_command *mc);
113 static void mlx_freecmd(struct mlx_command *mc);
116 * Command management.
118 static int mlx_getslot(struct mlx_command *mc);
119 static void mlx_mapcmd(struct mlx_command *mc);
120 static void mlx_unmapcmd(struct mlx_command *mc);
121 static int mlx_start(struct mlx_command *mc);
122 static int mlx_done(struct mlx_softc *sc);
123 static void mlx_complete(struct mlx_softc *sc);
128 static char *mlx_diagnose_command(struct mlx_command *mc);
129 static void mlx_describe_controller(struct mlx_softc *sc);
130 static int mlx_fw_message(struct mlx_softc *sc, int status, int param1, int param2);
135 static struct mlx_sysdrive *mlx_findunit(struct mlx_softc *sc, int unit);
137 /********************************************************************************
138 ********************************************************************************
140 ********************************************************************************
141 ********************************************************************************/
143 /********************************************************************************
144 * Free all of the resources associated with (sc)
146 * Should not be called if the controller is active.
149 mlx_free(struct mlx_softc *sc)
151 struct mlx_command *mc;
155 /* cancel status timeout */
156 callout_stop(&sc->mlx_timeout);
158 /* throw away any command buffers */
159 while ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL) {
160 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
164 /* destroy data-transfer DMA tag */
165 if (sc->mlx_buffer_dmat)
166 bus_dma_tag_destroy(sc->mlx_buffer_dmat);
168 /* free and destroy DMA memory and tag for s/g lists */
170 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
172 bus_dma_tag_destroy(sc->mlx_sg_dmat);
174 /* disconnect the interrupt handler */
176 bus_teardown_intr(sc->mlx_dev, sc->mlx_irq, sc->mlx_intr);
177 if (sc->mlx_irq != NULL)
178 bus_release_resource(sc->mlx_dev, SYS_RES_IRQ, 0, sc->mlx_irq);
180 /* destroy the parent DMA tag */
181 if (sc->mlx_parent_dmat)
182 bus_dma_tag_destroy(sc->mlx_parent_dmat);
184 /* release the register window mapping */
185 if (sc->mlx_mem != NULL)
186 bus_release_resource(sc->mlx_dev, sc->mlx_mem_type, sc->mlx_mem_rid, sc->mlx_mem);
188 /* free controller enquiry data */
189 if (sc->mlx_enq2 != NULL)
190 kfree(sc->mlx_enq2, M_DEVBUF);
192 dev_ops_remove_minor(&mlx_ops, device_get_unit(sc->mlx_dev));
195 /********************************************************************************
196 * Map the scatter/gather table into bus space
199 mlx_dma_map_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
201 struct mlx_softc *sc = (struct mlx_softc *)arg;
205 /* save base of s/g table's address in bus space */
206 sc->mlx_sgbusaddr = segs->ds_addr;
210 mlx_sglist_map(struct mlx_softc *sc)
217 /* destroy any existing mappings */
219 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
221 bus_dma_tag_destroy(sc->mlx_sg_dmat);
224 * Create a single tag describing a region large enough to hold all of
225 * the s/g lists we will need. If we're called early on, we don't know how
226 * many commands we're going to be asked to support, so only allocate enough
229 if (sc->mlx_enq2 == NULL) {
232 ncmd = sc->mlx_enq2->me_max_commands;
234 segsize = sizeof(struct mlx_sgentry) * MLX_NSEG * ncmd;
235 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */
236 1, 0, /* alignment, boundary */
237 BUS_SPACE_MAXADDR, /* lowaddr */
238 BUS_SPACE_MAXADDR, /* highaddr */
239 NULL, NULL, /* filter, filterarg */
240 segsize, 1, /* maxsize, nsegments */
241 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
245 device_printf(sc->mlx_dev, "can't allocate scatter/gather DMA tag\n");
250 * Allocate enough s/g maps for all commands and permanently map them into
251 * controller-visible space.
253 * XXX this assumes we can get enough space for all the s/g maps in one
254 * contiguous slab. We may need to switch to a more complex arrangement where
255 * we allocate in smaller chunks and keep a lookup table from slot to bus address.
257 error = bus_dmamem_alloc(sc->mlx_sg_dmat, (void *)&sc->mlx_sgtable, BUS_DMA_NOWAIT, &sc->mlx_sg_dmamap);
259 device_printf(sc->mlx_dev, "can't allocate s/g table\n");
262 bus_dmamap_load(sc->mlx_sg_dmat, sc->mlx_sg_dmamap, sc->mlx_sgtable, segsize, mlx_dma_map_sg, sc, 0);
266 /********************************************************************************
267 * Initialise the controller and softc
270 mlx_attach(struct mlx_softc *sc)
272 struct mlx_enquiry_old *meo;
273 int rid, error, fwminor, hscode, hserror, hsparam1, hsparam2, hsmsg;
276 callout_init(&sc->mlx_timeout);
279 * Initialise per-controller queues.
281 TAILQ_INIT(&sc->mlx_work);
282 TAILQ_INIT(&sc->mlx_freecmds);
283 bioq_init(&sc->mlx_bioq);
286 * Select accessor methods based on controller interface type.
288 switch(sc->mlx_iftype) {
291 sc->mlx_tryqueue = mlx_v3_tryqueue;
292 sc->mlx_findcomplete = mlx_v3_findcomplete;
293 sc->mlx_intaction = mlx_v3_intaction;
294 sc->mlx_fw_handshake = mlx_v3_fw_handshake;
297 sc->mlx_tryqueue = mlx_v4_tryqueue;
298 sc->mlx_findcomplete = mlx_v4_findcomplete;
299 sc->mlx_intaction = mlx_v4_intaction;
300 sc->mlx_fw_handshake = mlx_v4_fw_handshake;
303 sc->mlx_tryqueue = mlx_v5_tryqueue;
304 sc->mlx_findcomplete = mlx_v5_findcomplete;
305 sc->mlx_intaction = mlx_v5_intaction;
306 sc->mlx_fw_handshake = mlx_v5_fw_handshake;
310 return(ENXIO); /* should never happen */
313 /* disable interrupts before we start talking to the controller */
314 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
317 * Wait for the controller to come ready, handshake with the firmware if required.
318 * This is typically only necessary on platforms where the controller BIOS does not
323 while ((hscode = sc->mlx_fw_handshake(sc, &hserror, &hsparam1, &hsparam2)) != 0) {
324 /* report first time around... */
326 device_printf(sc->mlx_dev, "controller initialisation in progress...\n");
329 /* did we get a real message? */
331 hscode = mlx_fw_message(sc, hserror, hsparam1, hsparam2);
332 /* fatal initialisation error? */
340 device_printf(sc->mlx_dev, "initialisation complete.\n");
343 * Allocate and connect our interrupt.
346 sc->mlx_irq = bus_alloc_resource(sc->mlx_dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
347 if (sc->mlx_irq == NULL) {
348 device_printf(sc->mlx_dev, "can't allocate interrupt\n");
352 error = bus_setup_intr(sc->mlx_dev, sc->mlx_irq,
354 &sc->mlx_intr, NULL);
356 device_printf(sc->mlx_dev, "can't set up interrupt\n");
362 * Create DMA tag for mapping buffers into controller-addressable space.
364 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */
365 1, 0, /* alignment, boundary */
366 BUS_SPACE_MAXADDR, /* lowaddr */
367 BUS_SPACE_MAXADDR, /* highaddr */
368 NULL, NULL, /* filter, filterarg */
369 MAXBSIZE, MLX_NSEG, /* maxsize, nsegments */
370 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
372 &sc->mlx_buffer_dmat);
374 device_printf(sc->mlx_dev, "can't allocate buffer DMA tag\n");
380 * Create some initial scatter/gather mappings so we can run the probe commands.
382 error = mlx_sglist_map(sc);
384 device_printf(sc->mlx_dev, "can't make initial s/g list mapping\n");
390 * We don't (yet) know where the event log is up to.
392 sc->mlx_currevent = -1;
395 * Obtain controller feature information
397 if ((sc->mlx_enq2 = mlx_enquire(sc, MLX_CMD_ENQUIRY2, sizeof(struct mlx_enquiry2), NULL)) == NULL) {
398 device_printf(sc->mlx_dev, "ENQUIRY2 failed\n");
404 * Do quirk/feature related things.
406 fwminor = (sc->mlx_enq2->me_firmware_id >> 8) & 0xff;
407 switch(sc->mlx_iftype) {
409 /* These controllers don't report the firmware version in the ENQUIRY2 response */
410 if ((meo = mlx_enquire(sc, MLX_CMD_ENQUIRY_OLD, sizeof(struct mlx_enquiry_old), NULL)) == NULL) {
411 device_printf(sc->mlx_dev, "ENQUIRY_OLD failed\n");
415 sc->mlx_enq2->me_firmware_id = ('0' << 24) | (0 << 16) | (meo->me_fwminor << 8) | meo->me_fwmajor;
416 kfree(meo, M_DEVBUF);
418 /* XXX require 2.42 or better (PCI) or 2.14 or better (EISA) */
419 if (meo->me_fwminor < 42) {
420 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
421 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 2.42 or later\n");
425 /* XXX certify 3.52? */
427 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
428 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 3.51 or later\n");
432 /* XXX certify firmware versions? */
434 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
435 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 4.06 or later\n");
440 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
441 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 5.07 or later\n");
446 return(ENXIO); /* should never happen */
450 * Create the final scatter/gather mappings now that we have characterised the controller.
452 error = mlx_sglist_map(sc);
454 device_printf(sc->mlx_dev, "can't make final s/g list mapping\n");
460 * No user-requested background operation is in progress.
462 sc->mlx_background = 0;
463 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
466 * Create the control device.
468 make_dev(&mlx_ops, device_get_unit(sc->mlx_dev),
469 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
470 "mlx%d", device_get_unit(sc->mlx_dev));
473 * Start the timeout routine.
475 callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
477 /* print a little information about the controller */
478 mlx_describe_controller(sc);
483 /********************************************************************************
484 * Locate disk resources and attach children to them.
487 mlx_startup(struct mlx_softc *sc)
489 struct mlx_enq_sys_drive *mes;
490 struct mlx_sysdrive *dr;
496 * Scan all the system drives and attach children for those that
497 * don't currently have them.
499 mes = mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(*mes) * MLX_MAXDRIVES, NULL);
501 device_printf(sc->mlx_dev, "error fetching drive status\n");
505 /* iterate over drives returned */
506 for (i = 0, dr = &sc->mlx_sysdrive[0];
507 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
509 /* are we already attached to this drive? */
510 if (dr->ms_disk == 0) {
511 /* pick up drive information */
512 dr->ms_size = mes[i].sd_size;
513 dr->ms_raidlevel = mes[i].sd_raidlevel & 0xf;
514 dr->ms_state = mes[i].sd_state;
516 /* generate geometry information */
517 if (sc->mlx_geom == MLX_GEOM_128_32) {
520 dr->ms_cylinders = dr->ms_size / (128 * 32);
521 } else { /* MLX_GEOM_255/63 */
524 dr->ms_cylinders = dr->ms_size / (255 * 63);
526 dr->ms_disk = device_add_child(sc->mlx_dev, /*"mlxd"*/NULL, -1);
527 if (dr->ms_disk == 0)
528 device_printf(sc->mlx_dev, "device_add_child failed\n");
529 device_set_ivars(dr->ms_disk, dr);
532 kfree(mes, M_DEVBUF);
533 if ((error = bus_generic_attach(sc->mlx_dev)) != 0)
534 device_printf(sc->mlx_dev, "bus_generic_attach returned %d", error);
536 /* mark controller back up */
537 sc->mlx_state &= ~MLX_STATE_SHUTDOWN;
539 /* enable interrupts */
540 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
543 /********************************************************************************
544 * Disconnect from the controller completely, in preparation for unload.
547 mlx_detach(device_t dev)
549 struct mlx_softc *sc = device_get_softc(dev);
550 struct mlxd_softc *mlxd;
557 if (sc->mlx_state & MLX_STATE_OPEN)
560 for (i = 0; i < MLX_MAXDRIVES; i++) {
561 if (sc->mlx_sysdrive[i].ms_disk != 0) {
562 mlxd = device_get_softc(sc->mlx_sysdrive[i].ms_disk);
563 if (mlxd->mlxd_flags & MLXD_OPEN) { /* drive is mounted, abort detach */
564 device_printf(sc->mlx_sysdrive[i].ms_disk, "still open, can't detach\n");
569 if ((error = mlx_shutdown(dev)))
580 /********************************************************************************
581 * Bring the controller down to a dormant state and detach all child devices.
583 * This function is called before detach, system shutdown, or before performing
584 * an operation which may add or delete system disks. (Call mlx_startup to
585 * resume normal operation.)
587 * Note that we can assume that the bioq on the controller is empty, as we won't
588 * allow shutdown if any device is open.
591 mlx_shutdown(device_t dev)
593 struct mlx_softc *sc = device_get_softc(dev);
601 sc->mlx_state |= MLX_STATE_SHUTDOWN;
602 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
604 /* flush controller */
605 device_printf(sc->mlx_dev, "flushing cache...");
612 /* delete all our child devices */
613 for (i = 0; i < MLX_MAXDRIVES; i++) {
614 if (sc->mlx_sysdrive[i].ms_disk != 0) {
615 if ((error = device_delete_child(sc->mlx_dev, sc->mlx_sysdrive[i].ms_disk)) != 0)
617 sc->mlx_sysdrive[i].ms_disk = 0;
626 /********************************************************************************
627 * Bring the controller to a quiescent state, ready for system suspend.
630 mlx_suspend(device_t dev)
632 struct mlx_softc *sc = device_get_softc(dev);
637 sc->mlx_state |= MLX_STATE_SUSPEND;
639 /* flush controller */
640 device_printf(sc->mlx_dev, "flushing cache...");
641 kprintf("%s\n", mlx_flush(sc) ? "failed" : "done");
643 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
649 /********************************************************************************
650 * Bring the controller back to a state ready for operation.
653 mlx_resume(device_t dev)
655 struct mlx_softc *sc = device_get_softc(dev);
659 sc->mlx_state &= ~MLX_STATE_SUSPEND;
660 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
665 /*******************************************************************************
666 * Take an interrupt, or be poked by other code to look for interrupt-worthy
672 struct mlx_softc *sc = (struct mlx_softc *)arg;
676 /* collect finished commands, queue anything waiting */
680 /*******************************************************************************
681 * Receive a buf structure from a child device and queue it on a particular
682 * disk resource, then poke the disk resource to start as much work as it can.
685 mlx_submit_bio(struct mlx_softc *sc, struct bio *bio)
690 bioqdisksort(&sc->mlx_bioq, bio);
697 /********************************************************************************
698 * Accept an open operation on the control device.
701 mlx_open(struct dev_open_args *ap)
703 cdev_t dev = ap->a_head.a_dev;
704 int unit = minor(dev);
705 struct mlx_softc *sc = devclass_get_softc(mlx_devclass, unit);
707 sc->mlx_state |= MLX_STATE_OPEN;
711 /********************************************************************************
712 * Accept the last close on the control device.
715 mlx_close(struct dev_close_args *ap)
717 cdev_t dev = ap->a_head.a_dev;
718 int unit = minor(dev);
719 struct mlx_softc *sc = devclass_get_softc(mlx_devclass, unit);
721 sc->mlx_state &= ~MLX_STATE_OPEN;
725 /********************************************************************************
726 * Handle controller-specific control operations.
729 mlx_ioctl(struct dev_ioctl_args *ap)
731 cdev_t dev = ap->a_head.a_dev;
732 int unit = minor(dev);
733 struct mlx_softc *sc = devclass_get_softc(mlx_devclass, unit);
734 struct mlx_rebuild_request *rb = (struct mlx_rebuild_request *)ap->a_data;
735 struct mlx_rebuild_status *rs = (struct mlx_rebuild_status *)ap->a_data;
736 int *arg = (int *)ap->a_data;
737 struct mlx_pause *mp;
738 struct mlx_sysdrive *dr;
739 struct mlxd_softc *mlxd;
744 * Enumerate connected system drives; returns the first system drive's
745 * unit number if *arg is -1, or the next unit after *arg if it's
746 * a valid unit on this controller.
749 /* search system drives */
750 for (i = 0; i < MLX_MAXDRIVES; i++) {
751 /* is this one attached? */
752 if (sc->mlx_sysdrive[i].ms_disk != 0) {
753 /* looking for the next one we come across? */
755 *arg = device_get_unit(sc->mlx_sysdrive[0].ms_disk);
758 /* we want the one after this one */
759 if (*arg == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
766 * Scan the controller to see whether new drives have appeared.
768 case MLX_RESCAN_DRIVES:
773 * Disconnect from the specified drive; it may be about to go
776 case MLX_DETACH_DRIVE: /* detach one drive */
778 if (((dr = mlx_findunit(sc, *arg)) == NULL) ||
779 ((mlxd = device_get_softc(dr->ms_disk)) == NULL))
782 device_printf(dr->ms_disk, "detaching...");
784 if (mlxd->mlxd_flags & MLXD_OPEN) {
789 /* flush controller */
796 if ((error = device_delete_child(sc->mlx_dev, dr->ms_disk)) != 0)
809 * Pause one or more SCSI channels for a period of time, to assist
810 * in the process of hot-swapping devices.
812 * Note that at least the 3.51 firmware on the DAC960PL doesn't seem
815 case MLX_PAUSE_CHANNEL: /* schedule a channel pause */
816 /* Does this command work on this firmware? */
817 if (!(sc->mlx_feature & MLX_FEAT_PAUSEWORKS))
820 mp = (struct mlx_pause *)ap->a_data;
821 if ((mp->mp_which == MLX_PAUSE_CANCEL) && (sc->mlx_pause.mp_when != 0)) {
822 /* cancel a pending pause operation */
823 sc->mlx_pause.mp_which = 0;
825 /* fix for legal channels */
826 mp->mp_which &= ((1 << sc->mlx_enq2->me_actual_channels) -1);
827 /* check time values */
828 if ((mp->mp_when < 0) || (mp->mp_when > 3600))
830 if ((mp->mp_howlong < 1) || (mp->mp_howlong > (0xf * 30)))
833 /* check for a pause currently running */
834 if ((sc->mlx_pause.mp_which != 0) && (sc->mlx_pause.mp_when == 0))
837 /* looks ok, go with it */
838 sc->mlx_pause.mp_which = mp->mp_which;
839 sc->mlx_pause.mp_when = time_second + mp->mp_when;
840 sc->mlx_pause.mp_howlong = sc->mlx_pause.mp_when + mp->mp_howlong;
845 * Accept a command passthrough-style.
848 return(mlx_user_command(sc, (struct mlx_usercommand *)ap->a_data));
851 * Start a rebuild on a given SCSI disk
853 case MLX_REBUILDASYNC:
854 if (sc->mlx_background != 0) {
855 rb->rr_status = 0x0106;
858 rb->rr_status = mlx_rebuild(sc, rb->rr_channel, rb->rr_target);
859 switch (rb->rr_status) {
864 error = ENOMEM; /* couldn't set up the command */
883 sc->mlx_background = MLX_BACKGROUND_REBUILD;
887 * Get the status of the current rebuild or consistency check.
889 case MLX_REBUILDSTAT:
890 *rs = sc->mlx_rebuildstat;
894 * Return the per-controller system drive number matching the
895 * disk device number in (arg), if it happens to belong to us.
897 case MLX_GET_SYSDRIVE:
899 mlxd = (struct mlxd_softc *)devclass_get_softc(mlxd_devclass, *arg);
900 if ((mlxd != NULL) && (mlxd->mlxd_drive >= sc->mlx_sysdrive) &&
901 (mlxd->mlxd_drive < (sc->mlx_sysdrive + MLX_MAXDRIVES))) {
903 *arg = mlxd->mlxd_drive - sc->mlx_sysdrive;
912 /********************************************************************************
913 * Handle operations requested by a System Drive connected to this controller.
916 mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd,
917 caddr_t addr, int32_t flag)
919 int *arg = (int *)addr;
924 * Return the current status of this drive.
927 *arg = drive->ms_state;
931 * Start a background consistency check on this drive.
933 case MLXD_CHECKASYNC: /* start a background consistency check */
934 if (sc->mlx_background != 0) {
938 result = mlx_check(sc, drive - &sc->mlx_sysdrive[0]);
944 error = ENOMEM; /* couldn't set up the command */
960 sc->mlx_background = MLX_BACKGROUND_CHECK;
969 /********************************************************************************
970 ********************************************************************************
972 ********************************************************************************
973 ********************************************************************************/
975 /********************************************************************************
976 * Fire off commands to periodically check the status of connected drives.
979 mlx_periodic(void *data)
981 struct mlx_softc *sc = (struct mlx_softc *)data;
988 if ((sc->mlx_pause.mp_which != 0) &&
989 (sc->mlx_pause.mp_when > 0) &&
990 (time_second >= sc->mlx_pause.mp_when)){
992 mlx_pause_action(sc); /* pause is running */
993 sc->mlx_pause.mp_when = 0;
997 * Bus pause still running?
999 } else if ((sc->mlx_pause.mp_which != 0) &&
1000 (sc->mlx_pause.mp_when == 0)) {
1002 /* time to stop bus pause? */
1003 if (time_second >= sc->mlx_pause.mp_howlong) {
1004 mlx_pause_action(sc);
1005 sc->mlx_pause.mp_which = 0; /* pause is complete */
1008 sysbeep((time_second % 5) * 100 + 500, hz/8);
1012 * Run normal periodic activities?
1014 } else if (time_second > (sc->mlx_lastpoll + 10)) {
1015 sc->mlx_lastpoll = time_second;
1018 * Check controller status.
1020 * XXX Note that this may not actually launch a command in situations of high load.
1022 mlx_enquire(sc, (sc->mlx_iftype == MLX_IFTYPE_2) ? MLX_CMD_ENQUIRY_OLD : MLX_CMD_ENQUIRY,
1023 imax(sizeof(struct mlx_enquiry), sizeof(struct mlx_enquiry_old)), mlx_periodic_enquiry);
1026 * Check system drive status.
1028 * XXX This might be better left to event-driven detection, eg. I/O to an offline
1029 * drive will detect it's offline, rebuilds etc. should detect the drive is back
1032 mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(struct mlx_enq_sys_drive) * MLX_MAXDRIVES,
1033 mlx_periodic_enquiry);
1037 /* get drive rebuild/check status */
1038 /* XXX should check sc->mlx_background if this is only valid while in progress */
1039 mlx_enquire(sc, MLX_CMD_REBUILDSTAT, sizeof(struct mlx_rebuild_stat), mlx_periodic_rebuild);
1041 /* deal with possibly-missed interrupts and timed-out commands */
1044 /* reschedule another poll next second or so */
1045 callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
1048 /********************************************************************************
1049 * Handle the result of an ENQUIRY command instigated by periodic status polling.
1052 mlx_periodic_enquiry(struct mlx_command *mc)
1054 struct mlx_softc *sc = mc->mc_sc;
1058 /* Command completed OK? */
1059 if (mc->mc_status != 0) {
1060 device_printf(sc->mlx_dev, "periodic enquiry failed - %s\n", mlx_diagnose_command(mc));
1064 /* respond to command */
1065 switch(mc->mc_mailbox[0]) {
1067 * This is currently a bit fruitless, as we don't know how to extract the eventlog
1070 case MLX_CMD_ENQUIRY_OLD:
1072 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data;
1073 struct mlx_enquiry_old *meo = (struct mlx_enquiry_old *)mc->mc_data;
1076 /* convert data in-place to new format */
1077 for (i = (sizeof(me->me_dead) / sizeof(me->me_dead[0])) - 1; i >= 0; i--) {
1078 me->me_dead[i].dd_chan = meo->me_dead[i].dd_chan;
1079 me->me_dead[i].dd_targ = meo->me_dead[i].dd_targ;
1081 me->me_misc_flags = 0;
1082 me->me_rebuild_count = meo->me_rebuild_count;
1083 me->me_dead_count = meo->me_dead_count;
1084 me->me_critical_sd_count = meo->me_critical_sd_count;
1085 me->me_event_log_seq_num = 0;
1086 me->me_offline_sd_count = meo->me_offline_sd_count;
1087 me->me_max_commands = meo->me_max_commands;
1088 me->me_rebuild_flag = meo->me_rebuild_flag;
1089 me->me_fwmajor = meo->me_fwmajor;
1090 me->me_fwminor = meo->me_fwminor;
1091 me->me_status_flags = meo->me_status_flags;
1092 me->me_flash_age = meo->me_flash_age;
1093 for (i = (sizeof(me->me_drvsize) / sizeof(me->me_drvsize[0])) - 1; i >= 0; i--) {
1094 if (i > ((sizeof(meo->me_drvsize) / sizeof(meo->me_drvsize[0])) - 1)) {
1095 me->me_drvsize[i] = 0; /* drive beyond supported range */
1097 me->me_drvsize[i] = meo->me_drvsize[i];
1100 me->me_num_sys_drvs = meo->me_num_sys_drvs;
1105 * Generic controller status update. We could do more with this than just
1106 * checking the event log.
1108 case MLX_CMD_ENQUIRY:
1110 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data;
1112 if (sc->mlx_currevent == -1) {
1113 /* initialise our view of the event log */
1114 sc->mlx_currevent = sc->mlx_lastevent = me->me_event_log_seq_num;
1115 } else if ((me->me_event_log_seq_num != sc->mlx_lastevent) && !(sc->mlx_flags & MLX_EVENTLOG_BUSY)) {
1116 /* record where current events are up to */
1117 sc->mlx_currevent = me->me_event_log_seq_num;
1118 debug(1, "event log pointer was %d, now %d\n", sc->mlx_lastevent, sc->mlx_currevent);
1120 /* mark the event log as busy */
1121 atomic_set_int(&sc->mlx_flags, MLX_EVENTLOG_BUSY);
1123 /* drain new eventlog entries */
1124 mlx_periodic_eventlog_poll(sc);
1128 case MLX_CMD_ENQSYSDRIVE:
1130 struct mlx_enq_sys_drive *mes = (struct mlx_enq_sys_drive *)mc->mc_data;
1131 struct mlx_sysdrive *dr;
1134 for (i = 0, dr = &sc->mlx_sysdrive[0];
1135 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
1138 /* has state been changed by controller? */
1139 if (dr->ms_state != mes[i].sd_state) {
1140 switch(mes[i].sd_state) {
1141 case MLX_SYSD_OFFLINE:
1142 device_printf(dr->ms_disk, "drive offline\n");
1144 case MLX_SYSD_ONLINE:
1145 device_printf(dr->ms_disk, "drive online\n");
1147 case MLX_SYSD_CRITICAL:
1148 device_printf(dr->ms_disk, "drive critical\n");
1151 /* save new state */
1152 dr->ms_state = mes[i].sd_state;
1158 device_printf(sc->mlx_dev, "%s: unknown command 0x%x", __func__, mc->mc_mailbox[0]);
1163 kfree(mc->mc_data, M_DEVBUF);
1167 /********************************************************************************
1168 * Instigate a poll for one event log message on (sc).
1169 * We only poll for one message at a time, to keep our command usage down.
1172 mlx_periodic_eventlog_poll(struct mlx_softc *sc)
1174 struct mlx_command *mc;
1175 void *result = NULL;
1180 /* get ourselves a command buffer */
1182 if ((mc = mlx_alloccmd(sc)) == NULL)
1185 * allocate the response structure - sizeof(struct mlx_eventlog_entry)?
1186 * Called from timeout - use M_NOWAIT (repoll later on failure?)
1188 if ((result = kmalloc(1024, M_DEVBUF, M_NOWAIT)) == NULL)
1190 /* get a command slot */
1191 if (mlx_getslot(mc))
1194 /* map the command so the controller can see it */
1195 mc->mc_data = result;
1196 mc->mc_length = /*sizeof(struct mlx_eventlog_entry)*/1024;
1199 /* build the command to get one entry */
1200 mlx_make_type3(mc, MLX_CMD_LOGOP, MLX_LOGOP_GET, 1, sc->mlx_lastevent, 0, 0, mc->mc_dataphys, 0);
1201 mc->mc_complete = mlx_periodic_eventlog_respond;
1202 mc->mc_private = mc;
1204 /* start the command */
1205 if ((error = mlx_start(mc)) != 0)
1208 error = 0; /* success */
1214 kfree(result, M_DEVBUF);
1218 /********************************************************************************
1219 * Handle the result of polling for a log message, generate diagnostic output.
1220 * If this wasn't the last message waiting for us, we'll go collect another.
1222 static char *mlx_sense_messages[] = {
1223 "because write recovery failed",
1224 "because of SCSI bus reset failure",
1225 "because of double check condition",
1226 "because it was removed",
1227 "because of gross error on SCSI chip",
1228 "because of bad tag returned from drive",
1229 "because of timeout on SCSI command",
1230 "because of reset SCSI command issued from system",
1231 "because busy or parity error count exceeded limit",
1232 "because of 'kill drive' command from system",
1233 "because of selection timeout",
1234 "due to SCSI phase sequence error",
1235 "due to unknown status"
1239 mlx_periodic_eventlog_respond(struct mlx_command *mc)
1241 struct mlx_softc *sc = mc->mc_sc;
1242 struct mlx_eventlog_entry *el = (struct mlx_eventlog_entry *)mc->mc_data;
1247 sc->mlx_lastevent++; /* next message... */
1248 if (mc->mc_status == 0) {
1250 /* handle event log message */
1251 switch(el->el_type) {
1253 * This is the only sort of message we understand at the moment.
1254 * The tests here are probably incomplete.
1256 case MLX_LOGMSG_SENSE: /* sense data */
1257 /* Mylex vendor-specific message indicating a drive was killed? */
1258 if ((el->el_sensekey == 9) &&
1259 (el->el_asc == 0x80)) {
1260 if (el->el_asq < (sizeof(mlx_sense_messages) / sizeof(mlx_sense_messages[0]))) {
1261 reason = mlx_sense_messages[el->el_asq];
1263 reason = "for unknown reason";
1265 device_printf(sc->mlx_dev, "physical drive %d:%d killed %s\n",
1266 el->el_channel, el->el_target, reason);
1268 /* SCSI drive was reset? */
1269 if ((el->el_sensekey == 6) && (el->el_asc == 0x29)) {
1270 device_printf(sc->mlx_dev, "physical drive %d:%d reset\n",
1271 el->el_channel, el->el_target);
1273 /* SCSI drive error? */
1274 if (!((el->el_sensekey == 0) ||
1275 ((el->el_sensekey == 2) &&
1276 (el->el_asc == 0x04) &&
1277 ((el->el_asq == 0x01) ||
1278 (el->el_asq == 0x02))))) {
1279 device_printf(sc->mlx_dev, "physical drive %d:%d error log: sense = %d asc = %x asq = %x\n",
1280 el->el_channel, el->el_target, el->el_sensekey, el->el_asc, el->el_asq);
1281 device_printf(sc->mlx_dev, " info %4D csi %4D\n", el->el_information, ":", el->el_csi, ":");
1286 device_printf(sc->mlx_dev, "unknown log message type 0x%x\n", el->el_type);
1290 device_printf(sc->mlx_dev, "error reading message log - %s\n", mlx_diagnose_command(mc));
1291 /* give up on all the outstanding messages, as we may have come unsynched */
1292 sc->mlx_lastevent = sc->mlx_currevent;
1295 /* dispose of command and data */
1296 kfree(mc->mc_data, M_DEVBUF);
1299 /* is there another message to obtain? */
1300 if (sc->mlx_lastevent != sc->mlx_currevent) {
1301 mlx_periodic_eventlog_poll(sc);
1303 /* clear log-busy status */
1304 atomic_clear_int(&sc->mlx_flags, MLX_EVENTLOG_BUSY);
1308 /********************************************************************************
1309 * Handle check/rebuild operations in progress.
1312 mlx_periodic_rebuild(struct mlx_command *mc)
1314 struct mlx_softc *sc = mc->mc_sc;
1315 struct mlx_rebuild_status *mr = (struct mlx_rebuild_status *)mc->mc_data;
1317 switch(mc->mc_status) {
1318 case 0: /* operation running, update stats */
1319 sc->mlx_rebuildstat = *mr;
1321 /* spontaneous rebuild/check? */
1322 if (sc->mlx_background == 0) {
1323 sc->mlx_background = MLX_BACKGROUND_SPONTANEOUS;
1324 device_printf(sc->mlx_dev, "background check/rebuild operation started\n");
1328 case 0x0105: /* nothing running, finalise stats and report */
1329 switch(sc->mlx_background) {
1330 case MLX_BACKGROUND_CHECK:
1331 device_printf(sc->mlx_dev, "consistency check completed\n"); /* XXX print drive? */
1333 case MLX_BACKGROUND_REBUILD:
1334 device_printf(sc->mlx_dev, "drive rebuild completed\n"); /* XXX print channel/target? */
1336 case MLX_BACKGROUND_SPONTANEOUS:
1338 /* if we have previously been non-idle, report the transition */
1339 if (sc->mlx_rebuildstat.rs_code != MLX_REBUILDSTAT_IDLE) {
1340 device_printf(sc->mlx_dev, "background check/rebuild operation completed\n");
1343 sc->mlx_background = 0;
1344 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
1347 kfree(mc->mc_data, M_DEVBUF);
1351 /********************************************************************************
1352 ********************************************************************************
1354 ********************************************************************************
1355 ********************************************************************************/
1357 /********************************************************************************
1358 * It's time to perform a channel pause action for (sc), either start or stop
1362 mlx_pause_action(struct mlx_softc *sc)
1364 struct mlx_command *mc;
1365 int failsafe, i, command;
1367 /* What are we doing here? */
1368 if (sc->mlx_pause.mp_when == 0) {
1369 command = MLX_CMD_STARTCHANNEL;
1373 command = MLX_CMD_STOPCHANNEL;
1376 * Channels will always start again after the failsafe period,
1377 * which is specified in multiples of 30 seconds.
1378 * This constrains us to a maximum pause of 450 seconds.
1380 failsafe = ((sc->mlx_pause.mp_howlong - time_second) + 5) / 30;
1381 if (failsafe > 0xf) {
1383 sc->mlx_pause.mp_howlong = time_second + (0xf * 30) - 5;
1387 /* build commands for every channel requested */
1388 for (i = 0; i < sc->mlx_enq2->me_actual_channels; i++) {
1389 if ((1 << i) & sc->mlx_pause.mp_which) {
1391 /* get ourselves a command buffer */
1392 if ((mc = mlx_alloccmd(sc)) == NULL)
1394 /* get a command slot */
1395 mc->mc_flags |= MLX_CMD_PRIORITY;
1396 if (mlx_getslot(mc))
1399 /* build the command */
1400 mlx_make_type2(mc, command, (failsafe << 4) | i, 0, 0, 0, 0, 0, 0, 0);
1401 mc->mc_complete = mlx_pause_done;
1402 mc->mc_private = sc; /* XXX not needed */
1405 /* command submitted OK */
1409 device_printf(sc->mlx_dev, "%s failed for channel %d\n",
1410 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", i);
1418 mlx_pause_done(struct mlx_command *mc)
1420 struct mlx_softc *sc = mc->mc_sc;
1421 int command = mc->mc_mailbox[0];
1422 int channel = mc->mc_mailbox[2] & 0xf;
1424 if (mc->mc_status != 0) {
1425 device_printf(sc->mlx_dev, "%s command failed - %s\n",
1426 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", mlx_diagnose_command(mc));
1427 } else if (command == MLX_CMD_STOPCHANNEL) {
1428 device_printf(sc->mlx_dev, "channel %d pausing for %ld seconds\n",
1429 channel, (long)(sc->mlx_pause.mp_howlong - time_second));
1431 device_printf(sc->mlx_dev, "channel %d resuming\n", channel);
1436 /********************************************************************************
1437 ********************************************************************************
1439 ********************************************************************************
1440 ********************************************************************************/
1442 /********************************************************************************
1443 * Perform an Enquiry command using a type-3 command buffer and a return a single
1444 * linear result buffer. If the completion function is specified, it will
1445 * be called with the completed command (and the result response will not be
1446 * valid until that point). Otherwise, the command will either be busy-waited
1447 * for (interrupts not enabled), or slept for.
1450 mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize, void (* complete)(struct mlx_command *mc))
1452 struct mlx_command *mc;
1458 /* get ourselves a command buffer */
1461 if ((mc = mlx_alloccmd(sc)) == NULL)
1463 /* allocate the response structure */
1464 result = kmalloc(bufsize, M_DEVBUF, M_INTWAIT);
1465 /* get a command slot */
1466 mc->mc_flags |= MLX_CMD_PRIORITY | MLX_CMD_DATAOUT;
1467 if (mlx_getslot(mc))
1470 /* map the command so the controller can see it */
1471 mc->mc_data = result;
1472 mc->mc_length = bufsize;
1475 /* build an enquiry command */
1476 mlx_make_type2(mc, command, 0, 0, 0, 0, 0, 0, mc->mc_dataphys, 0);
1478 /* do we want a completion callback? */
1479 if (complete != NULL) {
1480 mc->mc_complete = complete;
1481 mc->mc_private = mc;
1482 if ((error = mlx_start(mc)) != 0)
1485 /* run the command in either polled or wait mode */
1486 if ((sc->mlx_state & MLX_STATE_INTEN) ? mlx_wait_command(mc) : mlx_poll_command(mc))
1489 /* command completed OK? */
1490 if (mc->mc_status != 0) {
1491 device_printf(sc->mlx_dev, "ENQUIRY failed - %s\n", mlx_diagnose_command(mc));
1495 error = 0; /* success */
1497 /* we got a command, but nobody else will free it */
1498 if ((complete == NULL) && (mc != NULL))
1500 /* we got an error, and we allocated a result */
1501 if ((error != 0) && (result != NULL)) {
1502 kfree(result, M_DEVBUF);
1509 /********************************************************************************
1510 * Perform a Flush command on the nominated controller.
1512 * May be called with interrupts enabled or disabled; will not return until
1513 * the flush operation completes or fails.
1516 mlx_flush(struct mlx_softc *sc)
1518 struct mlx_command *mc;
1523 /* get ourselves a command buffer */
1525 if ((mc = mlx_alloccmd(sc)) == NULL)
1527 /* get a command slot */
1528 if (mlx_getslot(mc))
1531 /* build a flush command */
1532 mlx_make_type2(mc, MLX_CMD_FLUSH, 0, 0, 0, 0, 0, 0, 0, 0);
1534 /* can't assume that interrupts are going to work here, so play it safe */
1535 if (mlx_poll_command(mc))
1538 /* command completed OK? */
1539 if (mc->mc_status != 0) {
1540 device_printf(sc->mlx_dev, "FLUSH failed - %s\n", mlx_diagnose_command(mc));
1544 error = 0; /* success */
1551 /********************************************************************************
1552 * Start a background consistency check on (drive).
1554 * May be called with interrupts enabled or disabled; will return as soon as the
1555 * operation has started or been refused.
1558 mlx_check(struct mlx_softc *sc, int drive)
1560 struct mlx_command *mc;
1565 /* get ourselves a command buffer */
1567 if ((mc = mlx_alloccmd(sc)) == NULL)
1569 /* get a command slot */
1570 if (mlx_getslot(mc))
1573 /* build a checkasync command, set the "fix it" flag */
1574 mlx_make_type2(mc, MLX_CMD_CHECKASYNC, 0, 0, 0, 0, 0, drive | 0x80, 0, 0);
1576 /* start the command and wait for it to be returned */
1577 if (mlx_wait_command(mc))
1580 /* command completed OK? */
1581 if (mc->mc_status != 0) {
1582 device_printf(sc->mlx_dev, "CHECK ASYNC failed - %s\n", mlx_diagnose_command(mc));
1584 device_printf(sc->mlx_sysdrive[drive].ms_disk, "consistency check started");
1586 error = mc->mc_status;
1594 /********************************************************************************
1595 * Start a background rebuild of the physical drive at (channel),(target).
1597 * May be called with interrupts enabled or disabled; will return as soon as the
1598 * operation has started or been refused.
1601 mlx_rebuild(struct mlx_softc *sc, int channel, int target)
1603 struct mlx_command *mc;
1608 /* get ourselves a command buffer */
1610 if ((mc = mlx_alloccmd(sc)) == NULL)
1612 /* get a command slot */
1613 if (mlx_getslot(mc))
1616 /* build a checkasync command, set the "fix it" flag */
1617 mlx_make_type2(mc, MLX_CMD_REBUILDASYNC, channel, target, 0, 0, 0, 0, 0, 0);
1619 /* start the command and wait for it to be returned */
1620 if (mlx_wait_command(mc))
1623 /* command completed OK? */
1624 if (mc->mc_status != 0) {
1625 device_printf(sc->mlx_dev, "REBUILD ASYNC failed - %s\n", mlx_diagnose_command(mc));
1627 device_printf(sc->mlx_dev, "drive rebuild started for %d:%d\n", channel, target);
1629 error = mc->mc_status;
1637 /********************************************************************************
1638 * Run the command (mc) and return when it completes.
1640 * Interrupts need to be enabled; returns nonzero on error.
1643 mlx_wait_command(struct mlx_command *mc)
1645 struct mlx_softc *sc = mc->mc_sc;
1650 mc->mc_complete = NULL;
1651 mc->mc_private = mc; /* wake us when you're done */
1652 if ((error = mlx_start(mc)) != 0)
1656 /* XXX better timeout? */
1657 while ((mc->mc_status == MLX_STATUS_BUSY) && (count < 30)) {
1658 tsleep(mc->mc_private, PCATCH, "mlxwcmd", hz);
1661 if (mc->mc_status != 0) {
1662 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1669 /********************************************************************************
1670 * Start the command (mc) and busy-wait for it to complete.
1672 * Should only be used when interrupts can't be relied upon. Returns 0 on
1673 * success, nonzero on error.
1674 * Successfully completed commands are dequeued.
1677 mlx_poll_command(struct mlx_command *mc)
1679 struct mlx_softc *sc = mc->mc_sc;
1684 mc->mc_complete = NULL;
1685 mc->mc_private = NULL; /* we will poll for it */
1686 if ((error = mlx_start(mc)) != 0)
1691 /* poll for completion */
1692 mlx_done(mc->mc_sc);
1694 } while ((mc->mc_status == MLX_STATUS_BUSY) && (count++ < 15000000));
1695 if (mc->mc_status != MLX_STATUS_BUSY) {
1697 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
1701 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1705 /********************************************************************************
1706 * Pull as much work off the softc's work queue as possible and give it to the
1707 * controller. Leave a couple of slots free for emergencies.
1709 * Must be called at splbio or in an equivalent fashion that prevents
1710 * reentry or activity on the bioq.
1713 mlx_startio(struct mlx_softc *sc)
1715 struct mlx_command *mc;
1716 struct mlxd_softc *mlxd;
1724 /* avoid reentrancy */
1725 if (mlx_lock_tas(sc, MLX_LOCK_STARTING))
1728 /* spin until something prevents us from doing any work */
1732 /* see if there's work to be done */
1733 if ((bio = bioq_first(&sc->mlx_bioq)) == NULL)
1736 if ((mc = mlx_alloccmd(sc)) == NULL)
1738 /* get a slot for the command */
1739 if (mlx_getslot(mc) != 0) {
1743 /* get the buf containing our work */
1744 bioq_remove(&sc->mlx_bioq, bio);
1749 /* connect the buf to the command */
1750 mc->mc_complete = mlx_completeio;
1751 mc->mc_private = bp;
1752 mc->mc_data = bp->b_data;
1753 mc->mc_length = bp->b_bcount;
1754 if (bp->b_cmd == BUF_CMD_READ) {
1755 mc->mc_flags |= MLX_CMD_DATAIN;
1756 cmd = MLX_CMD_READSG;
1758 mc->mc_flags |= MLX_CMD_DATAOUT;
1759 cmd = MLX_CMD_WRITESG;
1762 /* map the command so the controller can work with it */
1765 /* build a suitable I/O command (assumes 512-byte rounded transfers) */
1766 mlxd = (struct mlxd_softc *)bio->bio_driver_info;
1767 driveno = mlxd->mlxd_drive - sc->mlx_sysdrive;
1768 blkcount = (bp->b_bcount + MLX_BLKSIZE - 1) / MLX_BLKSIZE;
1769 blkno = bio->bio_offset / MLX_BLKSIZE;
1771 if ((blkno + blkcount) > sc->mlx_sysdrive[driveno].ms_size)
1772 device_printf(sc->mlx_dev, "I/O beyond end of unit (%u,%d > %u)\n",
1773 blkno, blkcount, sc->mlx_sysdrive[driveno].ms_size);
1776 * Build the I/O command. Note that the SG list type bits are set to zero,
1777 * denoting the format of SG list that we are using.
1779 if (sc->mlx_iftype == MLX_IFTYPE_2) {
1780 mlx_make_type1(mc, (cmd == MLX_CMD_WRITESG) ? MLX_CMD_WRITESG_OLD : MLX_CMD_READSG_OLD,
1781 blkcount & 0xff, /* xfer length low byte */
1782 blkno, /* physical block number */
1783 driveno, /* target drive number */
1784 mc->mc_sgphys, /* location of SG list */
1785 mc->mc_nsgent & 0x3f); /* size of SG list (top 3 bits clear) */
1787 mlx_make_type5(mc, cmd,
1788 blkcount & 0xff, /* xfer length low byte */
1789 (driveno << 3) | ((blkcount >> 8) & 0x07), /* target and length high 3 bits */
1790 blkno, /* physical block number */
1791 mc->mc_sgphys, /* location of SG list */
1792 mc->mc_nsgent & 0x3f); /* size of SG list (top 3 bits clear) */
1795 /* try to give command to controller */
1796 if (mlx_start(mc) != 0) {
1797 /* fail the command */
1798 mc->mc_status = MLX_STATUS_WEDGED;
1804 mlx_lock_clr(sc, MLX_LOCK_STARTING);
1807 /********************************************************************************
1808 * Handle completion of an I/O command.
1811 mlx_completeio(struct mlx_command *mc)
1813 struct mlx_softc *sc = mc->mc_sc;
1814 struct bio *bio = (mlx_bio *)mc->mc_private;
1815 struct mlxd_softc *mlxd = (struct mlxd_softc *)bio->bio_driver_info;
1816 struct buf *bp = bio->bio_buf;
1818 if (mc->mc_status != MLX_STATUS_OK) { /* could be more verbose here? */
1820 bp->b_flags |= B_ERROR;
1822 switch(mc->mc_status) {
1823 case MLX_STATUS_RDWROFFLINE: /* system drive has gone offline */
1824 device_printf(mlxd->mlxd_dev, "drive offline\n");
1825 /* should signal this with a return code */
1826 mlxd->mlxd_drive->ms_state = MLX_SYSD_OFFLINE;
1829 default: /* other I/O error */
1830 device_printf(sc->mlx_dev, "I/O error - %s\n", mlx_diagnose_command(mc));
1832 device_printf(sc->mlx_dev, " b_bcount %ld offset %lld\n",
1833 bp->b_bcount, bio->bio_offset);
1834 device_printf(sc->mlx_dev, " %13D\n", mc->mc_mailbox, " ");
1843 /********************************************************************************
1844 * Take a command from user-space and try to run it.
1846 * XXX Note that this can't perform very much in the way of error checking, and
1847 * as such, applications _must_ be considered trustworthy.
1848 * XXX Commands using S/G for data are not supported.
1851 mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu)
1853 struct mlx_command *mc;
1854 struct mlx_dcdb *dcdb;
1865 /* get ourselves a command and copy in from user space */
1866 if ((mc = mlx_alloccmd(sc)) == NULL)
1868 bcopy(mu->mu_command, mc->mc_mailbox, sizeof(mc->mc_mailbox));
1869 debug(0, "got command buffer");
1871 /* if we need a buffer for data transfer, allocate one and copy in its initial contents */
1872 if (mu->mu_datasize > 0) {
1873 if (mu->mu_datasize > MAXPHYS)
1875 if (((kbuf = kmalloc(mu->mu_datasize, M_DEVBUF, M_WAITOK)) == NULL) ||
1876 (error = copyin(mu->mu_buf, kbuf, mu->mu_datasize)))
1878 debug(0, "got kernel buffer");
1881 /* get a command slot */
1882 if (mlx_getslot(mc))
1884 debug(0, "got a slot");
1886 /* map the command so the controller can see it */
1888 mc->mc_length = mu->mu_datasize;
1893 * If this is a passthrough SCSI command, the DCDB is packed at the
1894 * beginning of the data area. Fix up the DCDB to point to the correct physical
1895 * address and override any bufptr supplied by the caller since we know
1896 * what it's meant to be.
1898 if (mc->mc_mailbox[0] == MLX_CMD_DIRECT_CDB) {
1899 dcdb = (struct mlx_dcdb *)kbuf;
1900 dcdb->dcdb_physaddr = mc->mc_dataphys + sizeof(*dcdb);
1905 * If there's a data buffer, fix up the command's buffer pointer.
1907 if (mu->mu_datasize > 0) {
1909 /* range check the pointer to physical buffer address */
1910 if ((mu->mu_bufptr < 0) || (mu->mu_bufptr > (sizeof(mu->mu_command) - sizeof(u_int32_t)))) {
1914 mc->mc_mailbox[mu->mu_bufptr ] = mc->mc_dataphys & 0xff;
1915 mc->mc_mailbox[mu->mu_bufptr + 1] = (mc->mc_dataphys >> 8) & 0xff;
1916 mc->mc_mailbox[mu->mu_bufptr + 2] = (mc->mc_dataphys >> 16) & 0xff;
1917 mc->mc_mailbox[mu->mu_bufptr + 3] = (mc->mc_dataphys >> 24) & 0xff;
1919 debug(0, "command fixup");
1921 /* submit the command and wait */
1922 if ((error = mlx_wait_command(mc)) != 0)
1925 /* copy out status and data */
1926 mu->mu_status = mc->mc_status;
1927 if ((mu->mu_datasize > 0) && ((error = copyout(kbuf, mu->mu_buf, mu->mu_datasize))))
1934 kfree(kbuf, M_DEVBUF);
1938 /********************************************************************************
1939 ********************************************************************************
1940 Command I/O to Controller
1941 ********************************************************************************
1942 ********************************************************************************/
1944 /********************************************************************************
1945 * Find a free command slot for (mc).
1947 * Don't hand out a slot to a normal-priority command unless there are at least
1948 * 4 slots free for priority commands.
1951 mlx_getslot(struct mlx_command *mc)
1953 struct mlx_softc *sc = mc->mc_sc;
1959 * Enforce slot-usage limit, if we have the required information.
1961 if (sc->mlx_enq2 != NULL) {
1962 limit = sc->mlx_enq2->me_max_commands;
1966 if (sc->mlx_busycmds >= ((mc->mc_flags & MLX_CMD_PRIORITY) ? limit : limit - 4))
1970 * Allocate an outstanding command slot
1972 * XXX linear search is slow
1975 for (slot = 0; slot < limit; slot++) {
1976 debug(2, "try slot %d", slot);
1977 if (sc->mlx_busycmd[slot] == NULL)
1981 sc->mlx_busycmd[slot] = mc;
1990 debug(2, "got slot %d", slot);
1995 /********************************************************************************
1996 * Map/unmap (mc)'s data in the controller's addressable space.
1999 mlx_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
2001 struct mlx_command *mc = (struct mlx_command *)arg;
2002 struct mlx_softc *sc = mc->mc_sc;
2003 struct mlx_sgentry *sg;
2008 /* XXX should be unnecessary */
2009 if (sc->mlx_enq2 && (nsegments > sc->mlx_enq2->me_max_sg))
2010 panic("MLX: too many s/g segments (%d, max %d)", nsegments, sc->mlx_enq2->me_max_sg);
2012 /* get base address of s/g table */
2013 sg = sc->mlx_sgtable + (mc->mc_slot * MLX_NSEG);
2015 /* save s/g table information in command */
2016 mc->mc_nsgent = nsegments;
2017 mc->mc_sgphys = sc->mlx_sgbusaddr + (mc->mc_slot * MLX_NSEG * sizeof(struct mlx_sgentry));
2018 mc->mc_dataphys = segs[0].ds_addr;
2020 /* populate s/g table */
2021 for (i = 0; i < nsegments; i++, sg++) {
2022 sg->sg_addr = segs[i].ds_addr;
2023 sg->sg_count = segs[i].ds_len;
2028 mlx_mapcmd(struct mlx_command *mc)
2030 struct mlx_softc *sc = mc->mc_sc;
2034 /* if the command involves data at all */
2035 if (mc->mc_data != NULL) {
2037 /* map the data buffer into bus space and build the s/g list */
2038 bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data, mc->mc_length,
2039 mlx_setup_dmamap, mc, 0);
2040 if (mc->mc_flags & MLX_CMD_DATAIN)
2041 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_PREREAD);
2042 if (mc->mc_flags & MLX_CMD_DATAOUT)
2043 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_PREWRITE);
2048 mlx_unmapcmd(struct mlx_command *mc)
2050 struct mlx_softc *sc = mc->mc_sc;
2054 /* if the command involved data at all */
2055 if (mc->mc_data != NULL) {
2057 if (mc->mc_flags & MLX_CMD_DATAIN)
2058 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTREAD);
2059 if (mc->mc_flags & MLX_CMD_DATAOUT)
2060 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTWRITE);
2062 bus_dmamap_unload(sc->mlx_buffer_dmat, mc->mc_dmamap);
2066 /********************************************************************************
2067 * Try to deliver (mc) to the controller.
2069 * Can be called at any interrupt level, with or without interrupts enabled.
2072 mlx_start(struct mlx_command *mc)
2074 struct mlx_softc *sc = mc->mc_sc;
2079 /* save the slot number as ident so we can handle this command when complete */
2080 mc->mc_mailbox[0x1] = mc->mc_slot;
2082 /* mark the command as currently being processed */
2083 mc->mc_status = MLX_STATUS_BUSY;
2085 /* set a default 60-second timeout XXX tunable? XXX not currently used */
2086 mc->mc_timeout = time_second + 60;
2088 /* spin waiting for the mailbox */
2089 for (i = 100000, done = 0; (i > 0) && !done; i--) {
2091 if (sc->mlx_tryqueue(sc, mc)) {
2093 /* move command to work queue */
2094 TAILQ_INSERT_TAIL(&sc->mlx_work, mc, mc_link);
2096 crit_exit(); /* drop spl to allow completion interrupts */
2099 /* command is enqueued */
2104 * We couldn't get the controller to take the command. Revoke the slot
2105 * that the command was given and return it with a bad status.
2107 sc->mlx_busycmd[mc->mc_slot] = NULL;
2108 device_printf(sc->mlx_dev, "controller wedged (not taking commands)\n");
2109 mc->mc_status = MLX_STATUS_WEDGED;
2114 /********************************************************************************
2115 * Poll the controller (sc) for completed commands.
2116 * Update command status and free slots for reuse. If any slots were freed,
2117 * new commands may be posted.
2119 * Returns nonzero if one or more commands were completed.
2122 mlx_done(struct mlx_softc *sc)
2124 struct mlx_command *mc;
2133 /* loop collecting completed commands */
2136 /* poll for a completed command's identifier and status */
2137 if (sc->mlx_findcomplete(sc, &slot, &status)) {
2139 mc = sc->mlx_busycmd[slot]; /* find command */
2140 if (mc != NULL) { /* paranoia */
2141 if (mc->mc_status == MLX_STATUS_BUSY) {
2142 mc->mc_status = status; /* save status */
2144 /* free slot for reuse */
2145 sc->mlx_busycmd[slot] = NULL;
2148 device_printf(sc->mlx_dev, "duplicate done event for slot %d\n", slot);
2151 device_printf(sc->mlx_dev, "done event for nonbusy slot %d\n", slot);
2159 /* if we've completed any commands, try posting some more */
2163 /* handle completion and timeouts */
2169 /********************************************************************************
2170 * Perform post-completion processing for commands on (sc).
2173 mlx_complete(struct mlx_softc *sc)
2175 struct mlx_command *mc, *nc;
2180 /* avoid reentrancy XXX might want to signal and request a restart */
2181 if (mlx_lock_tas(sc, MLX_LOCK_COMPLETING))
2187 /* scan the list of busy/done commands */
2188 mc = TAILQ_FIRST(&sc->mlx_work);
2189 while (mc != NULL) {
2190 nc = TAILQ_NEXT(mc, mc_link);
2192 /* Command has been completed in some fashion */
2193 if (mc->mc_status != MLX_STATUS_BUSY) {
2195 /* unmap the command's data buffer */
2198 * Does the command have a completion handler?
2200 if (mc->mc_complete != NULL) {
2201 /* remove from list and give to handler */
2202 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2203 mc->mc_complete(mc);
2206 * Is there a sleeper waiting on this command?
2208 } else if (mc->mc_private != NULL) { /* sleeping caller wants to know about it */
2210 /* remove from list and wake up sleeper */
2211 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2212 wakeup_one(mc->mc_private);
2215 * Leave the command for a caller that's polling for it.
2224 mlx_lock_clr(sc, MLX_LOCK_COMPLETING);
2227 /********************************************************************************
2228 ********************************************************************************
2229 Command Buffer Management
2230 ********************************************************************************
2231 ********************************************************************************/
2233 /********************************************************************************
2234 * Get a new command buffer.
2236 * This may return NULL in low-memory cases.
2238 * Note that using malloc() is expensive (the command buffer is << 1 page) but
2239 * necessary if we are to be a loadable module before the zone allocator is fixed.
2241 * If possible, we recycle a command buffer that's been used before.
2243 * XXX Note that command buffers are not cleaned out - it is the caller's
2244 * responsibility to ensure that all required fields are filled in before
2247 static struct mlx_command *
2248 mlx_alloccmd(struct mlx_softc *sc)
2250 struct mlx_command *mc;
2256 if ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL)
2257 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
2260 /* allocate a new command buffer? */
2262 mc = kmalloc(sizeof(*mc), M_DEVBUF, M_INTWAIT | M_ZERO);
2264 error = bus_dmamap_create(sc->mlx_buffer_dmat, 0, &mc->mc_dmamap);
2266 kfree(mc, M_DEVBUF);
2273 /********************************************************************************
2274 * Release a command buffer for recycling.
2276 * XXX It might be a good idea to limit the number of commands we save for reuse
2277 * if it's shown that this list bloats out massively.
2280 mlx_releasecmd(struct mlx_command *mc)
2285 TAILQ_INSERT_HEAD(&mc->mc_sc->mlx_freecmds, mc, mc_link);
2289 /********************************************************************************
2290 * Permanently discard a command buffer.
2293 mlx_freecmd(struct mlx_command *mc)
2295 struct mlx_softc *sc = mc->mc_sc;
2298 bus_dmamap_destroy(sc->mlx_buffer_dmat, mc->mc_dmamap);
2299 kfree(mc, M_DEVBUF);
2303 /********************************************************************************
2304 ********************************************************************************
2305 Type 3 interface accessor methods
2306 ********************************************************************************
2307 ********************************************************************************/
2309 /********************************************************************************
2310 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2311 * (the controller is not ready to take a command).
2313 * Must be called at splbio or in a fashion that prevents reentry.
2316 mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2322 /* ready for our command? */
2323 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_FULL)) {
2324 /* copy mailbox data to window */
2325 for (i = 0; i < 13; i++)
2326 MLX_V3_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2329 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_FULL);
2335 /********************************************************************************
2336 * See if a command has been completed, if so acknowledge its completion
2337 * and recover the slot number and status code.
2339 * Must be called at splbio or in a fashion that prevents reentry.
2342 mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2347 /* status available? */
2348 if (MLX_V3_GET_ODBR(sc) & MLX_V3_ODB_SAVAIL) {
2349 *slot = MLX_V3_GET_STATUS_IDENT(sc); /* get command identifier */
2350 *status = MLX_V3_GET_STATUS(sc); /* get status */
2352 /* acknowledge completion */
2353 MLX_V3_PUT_ODBR(sc, MLX_V3_ODB_SAVAIL);
2354 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2360 /********************************************************************************
2361 * Enable/disable interrupts as requested. (No acknowledge required)
2363 * Must be called at splbio or in a fashion that prevents reentry.
2366 mlx_v3_intaction(struct mlx_softc *sc, int action)
2371 case MLX_INTACTION_DISABLE:
2372 MLX_V3_PUT_IER(sc, 0);
2373 sc->mlx_state &= ~MLX_STATE_INTEN;
2375 case MLX_INTACTION_ENABLE:
2376 MLX_V3_PUT_IER(sc, 1);
2377 sc->mlx_state |= MLX_STATE_INTEN;
2382 /********************************************************************************
2383 * Poll for firmware error codes during controller initialisation.
2384 * Returns 0 if initialisation is complete, 1 if still in progress but no
2385 * error has been fetched, 2 if an error has been retrieved.
2388 mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2)
2391 static int initted = 0;
2395 /* first time around, clear any hardware completion status */
2397 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2402 /* init in progress? */
2403 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_INIT_BUSY))
2406 /* test error value */
2407 fwerror = MLX_V3_GET_FWERROR(sc);
2408 if (!(fwerror & MLX_V3_FWERROR_PEND))
2411 /* mask status pending bit, fetch status */
2412 *error = fwerror & ~MLX_V3_FWERROR_PEND;
2413 *param1 = MLX_V3_GET_FWERROR_PARAM1(sc);
2414 *param2 = MLX_V3_GET_FWERROR_PARAM2(sc);
2417 MLX_V3_PUT_FWERROR(sc, 0);
2422 /********************************************************************************
2423 ********************************************************************************
2424 Type 4 interface accessor methods
2425 ********************************************************************************
2426 ********************************************************************************/
2428 /********************************************************************************
2429 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2430 * (the controller is not ready to take a command).
2432 * Must be called at splbio or in a fashion that prevents reentry.
2435 mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2441 /* ready for our command? */
2442 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_FULL)) {
2443 /* copy mailbox data to window */
2444 for (i = 0; i < 13; i++)
2445 MLX_V4_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2447 /* memory-mapped controller, so issue a write barrier to ensure the mailbox is filled */
2448 bus_space_barrier(sc->mlx_btag, sc->mlx_bhandle, MLX_V4_MAILBOX, MLX_V4_MAILBOX_LENGTH,
2449 BUS_SPACE_BARRIER_WRITE);
2452 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_HWMBOX_CMD);
2458 /********************************************************************************
2459 * See if a command has been completed, if so acknowledge its completion
2460 * and recover the slot number and status code.
2462 * Must be called at splbio or in a fashion that prevents reentry.
2465 mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2470 /* status available? */
2471 if (MLX_V4_GET_ODBR(sc) & MLX_V4_ODB_HWSAVAIL) {
2472 *slot = MLX_V4_GET_STATUS_IDENT(sc); /* get command identifier */
2473 *status = MLX_V4_GET_STATUS(sc); /* get status */
2475 /* acknowledge completion */
2476 MLX_V4_PUT_ODBR(sc, MLX_V4_ODB_HWMBOX_ACK);
2477 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2483 /********************************************************************************
2484 * Enable/disable interrupts as requested.
2486 * Must be called at splbio or in a fashion that prevents reentry.
2489 mlx_v4_intaction(struct mlx_softc *sc, int action)
2494 case MLX_INTACTION_DISABLE:
2495 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK | MLX_V4_IER_DISINT);
2496 sc->mlx_state &= ~MLX_STATE_INTEN;
2498 case MLX_INTACTION_ENABLE:
2499 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK & ~MLX_V4_IER_DISINT);
2500 sc->mlx_state |= MLX_STATE_INTEN;
2505 /********************************************************************************
2506 * Poll for firmware error codes during controller initialisation.
2507 * Returns 0 if initialisation is complete, 1 if still in progress but no
2508 * error has been fetched, 2 if an error has been retrieved.
2511 mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2)
2514 static int initted = 0;
2518 /* first time around, clear any hardware completion status */
2520 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2525 /* init in progress? */
2526 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_INIT_BUSY))
2529 /* test error value */
2530 fwerror = MLX_V4_GET_FWERROR(sc);
2531 if (!(fwerror & MLX_V4_FWERROR_PEND))
2534 /* mask status pending bit, fetch status */
2535 *error = fwerror & ~MLX_V4_FWERROR_PEND;
2536 *param1 = MLX_V4_GET_FWERROR_PARAM1(sc);
2537 *param2 = MLX_V4_GET_FWERROR_PARAM2(sc);
2540 MLX_V4_PUT_FWERROR(sc, 0);
2545 /********************************************************************************
2546 ********************************************************************************
2547 Type 5 interface accessor methods
2548 ********************************************************************************
2549 ********************************************************************************/
2551 /********************************************************************************
2552 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2553 * (the controller is not ready to take a command).
2555 * Must be called at splbio or in a fashion that prevents reentry.
2558 mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2564 /* ready for our command? */
2565 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_EMPTY) {
2566 /* copy mailbox data to window */
2567 for (i = 0; i < 13; i++)
2568 MLX_V5_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2571 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_HWMBOX_CMD);
2577 /********************************************************************************
2578 * See if a command has been completed, if so acknowledge its completion
2579 * and recover the slot number and status code.
2581 * Must be called at splbio or in a fashion that prevents reentry.
2584 mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2589 /* status available? */
2590 if (MLX_V5_GET_ODBR(sc) & MLX_V5_ODB_HWSAVAIL) {
2591 *slot = MLX_V5_GET_STATUS_IDENT(sc); /* get command identifier */
2592 *status = MLX_V5_GET_STATUS(sc); /* get status */
2594 /* acknowledge completion */
2595 MLX_V5_PUT_ODBR(sc, MLX_V5_ODB_HWMBOX_ACK);
2596 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2602 /********************************************************************************
2603 * Enable/disable interrupts as requested.
2605 * Must be called at splbio or in a fashion that prevents reentry.
2608 mlx_v5_intaction(struct mlx_softc *sc, int action)
2613 case MLX_INTACTION_DISABLE:
2614 MLX_V5_PUT_IER(sc, 0xff & MLX_V5_IER_DISINT);
2615 sc->mlx_state &= ~MLX_STATE_INTEN;
2617 case MLX_INTACTION_ENABLE:
2618 MLX_V5_PUT_IER(sc, 0xff & ~MLX_V5_IER_DISINT);
2619 sc->mlx_state |= MLX_STATE_INTEN;
2624 /********************************************************************************
2625 * Poll for firmware error codes during controller initialisation.
2626 * Returns 0 if initialisation is complete, 1 if still in progress but no
2627 * error has been fetched, 2 if an error has been retrieved.
2630 mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2)
2633 static int initted = 0;
2637 /* first time around, clear any hardware completion status */
2639 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2644 /* init in progress? */
2645 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_INIT_DONE)
2648 /* test for error value */
2649 fwerror = MLX_V5_GET_FWERROR(sc);
2650 if (!(fwerror & MLX_V5_FWERROR_PEND))
2653 /* mask status pending bit, fetch status */
2654 *error = fwerror & ~MLX_V5_FWERROR_PEND;
2655 *param1 = MLX_V5_GET_FWERROR_PARAM1(sc);
2656 *param2 = MLX_V5_GET_FWERROR_PARAM2(sc);
2659 MLX_V5_PUT_FWERROR(sc, 0xff);
2664 /********************************************************************************
2665 ********************************************************************************
2667 ********************************************************************************
2668 ********************************************************************************/
2670 /********************************************************************************
2671 * Return a status message describing (mc)
2673 static char *mlx_status_messages[] = {
2674 "normal completion", /* 00 */
2675 "irrecoverable data error", /* 01 */
2676 "drive does not exist, or is offline", /* 02 */
2677 "attempt to write beyond end of drive", /* 03 */
2678 "bad data encountered", /* 04 */
2679 "invalid log entry request", /* 05 */
2680 "attempt to rebuild online drive", /* 06 */
2681 "new disk failed during rebuild", /* 07 */
2682 "invalid channel/target", /* 08 */
2683 "rebuild/check already in progress", /* 09 */
2684 "one or more disks are dead", /* 10 */
2685 "invalid or non-redundant drive", /* 11 */
2686 "channel is busy", /* 12 */
2687 "channel is not stopped", /* 13 */
2688 "rebuild successfully terminated", /* 14 */
2689 "unsupported command", /* 15 */
2690 "check condition received", /* 16 */
2691 "device is busy", /* 17 */
2692 "selection or command timeout", /* 18 */
2693 "command terminated abnormally", /* 19 */
2702 } mlx_messages[] = {
2703 {MLX_CMD_READSG, 0x0001, 1},
2704 {MLX_CMD_READSG, 0x0002, 1},
2705 {MLX_CMD_READSG, 0x0105, 3},
2706 {MLX_CMD_READSG, 0x010c, 4},
2707 {MLX_CMD_WRITESG, 0x0001, 1},
2708 {MLX_CMD_WRITESG, 0x0002, 1},
2709 {MLX_CMD_WRITESG, 0x0105, 3},
2710 {MLX_CMD_READSG_OLD, 0x0001, 1},
2711 {MLX_CMD_READSG_OLD, 0x0002, 1},
2712 {MLX_CMD_READSG_OLD, 0x0105, 3},
2713 {MLX_CMD_WRITESG_OLD, 0x0001, 1},
2714 {MLX_CMD_WRITESG_OLD, 0x0002, 1},
2715 {MLX_CMD_WRITESG_OLD, 0x0105, 3},
2716 {MLX_CMD_LOGOP, 0x0105, 5},
2717 {MLX_CMD_REBUILDASYNC, 0x0002, 6},
2718 {MLX_CMD_REBUILDASYNC, 0x0004, 7},
2719 {MLX_CMD_REBUILDASYNC, 0x0105, 8},
2720 {MLX_CMD_REBUILDASYNC, 0x0106, 9},
2721 {MLX_CMD_REBUILDASYNC, 0x0107, 14},
2722 {MLX_CMD_CHECKASYNC, 0x0002, 10},
2723 {MLX_CMD_CHECKASYNC, 0x0105, 11},
2724 {MLX_CMD_CHECKASYNC, 0x0106, 9},
2725 {MLX_CMD_STOPCHANNEL, 0x0106, 12},
2726 {MLX_CMD_STOPCHANNEL, 0x0105, 8},
2727 {MLX_CMD_STARTCHANNEL, 0x0005, 13},
2728 {MLX_CMD_STARTCHANNEL, 0x0105, 8},
2729 {MLX_CMD_DIRECT_CDB, 0x0002, 16},
2730 {MLX_CMD_DIRECT_CDB, 0x0008, 17},
2731 {MLX_CMD_DIRECT_CDB, 0x000e, 18},
2732 {MLX_CMD_DIRECT_CDB, 0x000f, 19},
2733 {MLX_CMD_DIRECT_CDB, 0x0105, 8},
2740 mlx_diagnose_command(struct mlx_command *mc)
2742 static char unkmsg[80];
2745 /* look up message in table */
2746 for (i = 0; mlx_messages[i].command != -1; i++)
2747 if (((mc->mc_mailbox[0] == mlx_messages[i].command) || (mlx_messages[i].command == 0)) &&
2748 (mc->mc_status == mlx_messages[i].status))
2749 return(mlx_status_messages[mlx_messages[i].msg]);
2751 ksprintf(unkmsg, "unknown response 0x%x for command 0x%x", (int)mc->mc_status, (int)mc->mc_mailbox[0]);
2755 /*******************************************************************************
2756 * Print a string describing the controller (sc)
2762 } mlx_controller_names[] = {
2777 mlx_describe_controller(struct mlx_softc *sc)
2779 static char buf[80];
2783 for (i = 0, model = NULL; mlx_controller_names[i].name != NULL; i++) {
2784 if ((sc->mlx_enq2->me_hardware_id & 0xff) == mlx_controller_names[i].hwid) {
2785 model = mlx_controller_names[i].name;
2789 if (model == NULL) {
2790 ksprintf(buf, " model 0x%x", sc->mlx_enq2->me_hardware_id & 0xff);
2793 device_printf(sc->mlx_dev, "DAC%s, %d channel%s, firmware %d.%02d-%c-%02d, %dMB RAM\n",
2795 sc->mlx_enq2->me_actual_channels,
2796 sc->mlx_enq2->me_actual_channels > 1 ? "s" : "",
2797 sc->mlx_enq2->me_firmware_id & 0xff,
2798 (sc->mlx_enq2->me_firmware_id >> 8) & 0xff,
2799 (sc->mlx_enq2->me_firmware_id >> 24) & 0xff,
2800 (sc->mlx_enq2->me_firmware_id >> 16) & 0xff,
2801 sc->mlx_enq2->me_mem_size / (1024 * 1024));
2804 device_printf(sc->mlx_dev, " Hardware ID 0x%08x\n", sc->mlx_enq2->me_hardware_id);
2805 device_printf(sc->mlx_dev, " Firmware ID 0x%08x\n", sc->mlx_enq2->me_firmware_id);
2806 device_printf(sc->mlx_dev, " Configured/Actual channels %d/%d\n", sc->mlx_enq2->me_configured_channels,
2807 sc->mlx_enq2->me_actual_channels);
2808 device_printf(sc->mlx_dev, " Max Targets %d\n", sc->mlx_enq2->me_max_targets);
2809 device_printf(sc->mlx_dev, " Max Tags %d\n", sc->mlx_enq2->me_max_tags);
2810 device_printf(sc->mlx_dev, " Max System Drives %d\n", sc->mlx_enq2->me_max_sys_drives);
2811 device_printf(sc->mlx_dev, " Max Arms %d\n", sc->mlx_enq2->me_max_arms);
2812 device_printf(sc->mlx_dev, " Max Spans %d\n", sc->mlx_enq2->me_max_spans);
2813 device_printf(sc->mlx_dev, " DRAM/cache/flash/NVRAM size %d/%d/%d/%d\n", sc->mlx_enq2->me_mem_size,
2814 sc->mlx_enq2->me_cache_size, sc->mlx_enq2->me_flash_size, sc->mlx_enq2->me_nvram_size);
2815 device_printf(sc->mlx_dev, " DRAM type %d\n", sc->mlx_enq2->me_mem_type);
2816 device_printf(sc->mlx_dev, " Clock Speed %dns\n", sc->mlx_enq2->me_clock_speed);
2817 device_printf(sc->mlx_dev, " Hardware Speed %dns\n", sc->mlx_enq2->me_hardware_speed);
2818 device_printf(sc->mlx_dev, " Max Commands %d\n", sc->mlx_enq2->me_max_commands);
2819 device_printf(sc->mlx_dev, " Max SG Entries %d\n", sc->mlx_enq2->me_max_sg);
2820 device_printf(sc->mlx_dev, " Max DP %d\n", sc->mlx_enq2->me_max_dp);
2821 device_printf(sc->mlx_dev, " Max IOD %d\n", sc->mlx_enq2->me_max_iod);
2822 device_printf(sc->mlx_dev, " Max Comb %d\n", sc->mlx_enq2->me_max_comb);
2823 device_printf(sc->mlx_dev, " Latency %ds\n", sc->mlx_enq2->me_latency);
2824 device_printf(sc->mlx_dev, " SCSI Timeout %ds\n", sc->mlx_enq2->me_scsi_timeout);
2825 device_printf(sc->mlx_dev, " Min Free Lines %d\n", sc->mlx_enq2->me_min_freelines);
2826 device_printf(sc->mlx_dev, " Rate Constant %d\n", sc->mlx_enq2->me_rate_const);
2827 device_printf(sc->mlx_dev, " MAXBLK %d\n", sc->mlx_enq2->me_maxblk);
2828 device_printf(sc->mlx_dev, " Blocking Factor %d sectors\n", sc->mlx_enq2->me_blocking_factor);
2829 device_printf(sc->mlx_dev, " Cache Line Size %d blocks\n", sc->mlx_enq2->me_cacheline);
2830 device_printf(sc->mlx_dev, " SCSI Capability %s%dMHz, %d bit\n",
2831 sc->mlx_enq2->me_scsi_cap & (1<<4) ? "differential " : "",
2832 (1 << ((sc->mlx_enq2->me_scsi_cap >> 2) & 3)) * 10,
2833 8 << (sc->mlx_enq2->me_scsi_cap & 0x3));
2834 device_printf(sc->mlx_dev, " Firmware Build Number %d\n", sc->mlx_enq2->me_firmware_build);
2835 device_printf(sc->mlx_dev, " Fault Management Type %d\n", sc->mlx_enq2->me_fault_mgmt_type);
2836 device_printf(sc->mlx_dev, " Features %b\n", sc->mlx_enq2->me_firmware_features,
2837 "\20\4Background Init\3Read Ahead\2MORE\1Cluster\n");
2842 /*******************************************************************************
2843 * Emit a string describing the firmware handshake status code, and return a flag
2844 * indicating whether the code represents a fatal error.
2846 * Error code interpretations are from the Linux driver, and don't directly match
2847 * the messages printed by Mylex's BIOS. This may change if documentation on the
2848 * codes is forthcoming.
2851 mlx_fw_message(struct mlx_softc *sc, int error, int param1, int param2)
2855 device_printf(sc->mlx_dev, "physical drive %d:%d not responding\n", param2, param1);
2858 /* we could be neater about this and give some indication when we receive more of them */
2859 if (!(sc->mlx_flags & MLX_SPINUP_REPORTED)) {
2860 device_printf(sc->mlx_dev, "spinning up drives...\n");
2861 sc->mlx_flags |= MLX_SPINUP_REPORTED;
2865 device_printf(sc->mlx_dev, "configuration checksum error\n");
2868 device_printf(sc->mlx_dev, "mirror race recovery failed\n");
2871 device_printf(sc->mlx_dev, "mirror race recovery in progress\n");
2874 device_printf(sc->mlx_dev, "physical drive %d:%d COD mismatch\n", param2, param1);
2877 device_printf(sc->mlx_dev, "logical drive installation aborted\n");
2880 device_printf(sc->mlx_dev, "mirror race on a critical system drive\n");
2883 device_printf(sc->mlx_dev, "new controller configuration found\n");
2886 device_printf(sc->mlx_dev, "FATAL MEMORY PARITY ERROR\n");
2889 device_printf(sc->mlx_dev, "unknown firmware initialisation error %02x:%02x:%02x\n", error, param1, param2);
2895 /********************************************************************************
2896 ********************************************************************************
2898 ********************************************************************************
2899 ********************************************************************************/
2901 /********************************************************************************
2902 * Find the disk whose unit number is (unit) on this controller
2904 static struct mlx_sysdrive *
2905 mlx_findunit(struct mlx_softc *sc, int unit)
2909 /* search system drives */
2910 for (i = 0; i < MLX_MAXDRIVES; i++) {
2911 /* is this one attached? */
2912 if (sc->mlx_sysdrive[i].ms_disk != 0) {
2913 /* is this the one? */
2914 if (unit == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
2915 return(&sc->mlx_sysdrive[i]);