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 */
194 if (sc->mlx_sgbusaddr)
195 bus_dmamap_unload(sc->mlx_sg_dmat, sc->mlx_sg_dmamap);
197 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
199 bus_dma_tag_destroy(sc->mlx_sg_dmat);
201 /* disconnect the interrupt handler */
202 if (sc->mlx_irq != NULL)
203 bus_release_resource(sc->mlx_dev, SYS_RES_IRQ, 0, sc->mlx_irq);
205 /* destroy the parent DMA tag */
206 if (sc->mlx_parent_dmat)
207 bus_dma_tag_destroy(sc->mlx_parent_dmat);
209 /* release the register window mapping */
210 if (sc->mlx_mem != NULL)
211 bus_release_resource(sc->mlx_dev, sc->mlx_mem_type, sc->mlx_mem_rid, sc->mlx_mem);
213 /* free controller enquiry data */
214 if (sc->mlx_enq2 != NULL)
215 free(sc->mlx_enq2, M_DEVBUF);
217 sx_destroy(&sc->mlx_config_lock);
218 mtx_destroy(&sc->mlx_io_lock);
221 /********************************************************************************
222 * Map the scatter/gather table into bus space
225 mlx_dma_map_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
227 struct mlx_softc *sc = (struct mlx_softc *)arg;
231 /* save base of s/g table's address in bus space */
232 sc->mlx_sgbusaddr = segs->ds_addr;
236 mlx_sglist_map(struct mlx_softc *sc)
243 /* destroy any existing mappings */
244 if (sc->mlx_sgbusaddr)
245 bus_dmamap_unload(sc->mlx_sg_dmat, sc->mlx_sg_dmamap);
247 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
249 bus_dma_tag_destroy(sc->mlx_sg_dmat);
250 sc->mlx_sgbusaddr = 0;
251 sc->mlx_sgtable = NULL;
252 sc->mlx_sg_dmat = NULL;
255 * Create a single tag describing a region large enough to hold all of
256 * the s/g lists we will need. If we're called early on, we don't know how
257 * many commands we're going to be asked to support, so only allocate enough
260 if (sc->mlx_enq2 == NULL) {
263 ncmd = sc->mlx_enq2->me_max_commands;
265 segsize = sizeof(struct mlx_sgentry) * MLX_NSEG * ncmd;
266 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */
267 1, 0, /* alignment,boundary */
268 BUS_SPACE_MAXADDR, /* lowaddr */
269 BUS_SPACE_MAXADDR, /* highaddr */
270 NULL, NULL, /* filter, filterarg */
271 segsize, 1, /* maxsize, nsegments */
272 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
274 NULL, NULL, /* lockfunc, lockarg */
277 device_printf(sc->mlx_dev, "can't allocate scatter/gather DMA tag\n");
282 * Allocate enough s/g maps for all commands and permanently map them into
283 * controller-visible space.
285 * XXX this assumes we can get enough space for all the s/g maps in one
286 * contiguous slab. We may need to switch to a more complex arrangement
287 * where we allocate in smaller chunks and keep a lookup table from slot
290 error = bus_dmamem_alloc(sc->mlx_sg_dmat, (void **)&sc->mlx_sgtable,
291 BUS_DMA_NOWAIT, &sc->mlx_sg_dmamap);
293 device_printf(sc->mlx_dev, "can't allocate s/g table\n");
296 (void)bus_dmamap_load(sc->mlx_sg_dmat, sc->mlx_sg_dmamap, sc->mlx_sgtable,
297 segsize, mlx_dma_map_sg, sc, 0);
301 /********************************************************************************
302 * Initialise the controller and softc
305 mlx_attach(struct mlx_softc *sc)
307 struct mlx_enquiry_old *meo;
308 int rid, error, fwminor, hscode, hserror, hsparam1, hsparam2, hsmsg;
313 * Initialise per-controller queues.
315 TAILQ_INIT(&sc->mlx_work);
316 TAILQ_INIT(&sc->mlx_freecmds);
317 bioq_init(&sc->mlx_bioq);
320 * Select accessor methods based on controller interface type.
322 switch(sc->mlx_iftype) {
325 sc->mlx_tryqueue = mlx_v3_tryqueue;
326 sc->mlx_findcomplete = mlx_v3_findcomplete;
327 sc->mlx_intaction = mlx_v3_intaction;
328 sc->mlx_fw_handshake = mlx_v3_fw_handshake;
331 sc->mlx_tryqueue = mlx_v4_tryqueue;
332 sc->mlx_findcomplete = mlx_v4_findcomplete;
333 sc->mlx_intaction = mlx_v4_intaction;
334 sc->mlx_fw_handshake = mlx_v4_fw_handshake;
337 sc->mlx_tryqueue = mlx_v5_tryqueue;
338 sc->mlx_findcomplete = mlx_v5_findcomplete;
339 sc->mlx_intaction = mlx_v5_intaction;
340 sc->mlx_fw_handshake = mlx_v5_fw_handshake;
343 return(ENXIO); /* should never happen */
346 /* disable interrupts before we start talking to the controller */
348 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
352 * Wait for the controller to come ready, handshake with the firmware if required.
353 * This is typically only necessary on platforms where the controller BIOS does not
358 while ((hscode = sc->mlx_fw_handshake(sc, &hserror, &hsparam1, &hsparam2,
360 /* report first time around... */
362 device_printf(sc->mlx_dev, "controller initialisation in progress...\n");
365 /* did we get a real message? */
367 hscode = mlx_fw_message(sc, hserror, hsparam1, hsparam2);
368 /* fatal initialisation error? */
375 device_printf(sc->mlx_dev, "initialisation complete.\n");
378 * Allocate and connect our interrupt.
381 sc->mlx_irq = bus_alloc_resource_any(sc->mlx_dev, SYS_RES_IRQ, &rid,
382 RF_SHAREABLE | RF_ACTIVE);
383 if (sc->mlx_irq == NULL) {
384 device_printf(sc->mlx_dev, "can't allocate interrupt\n");
387 error = bus_setup_intr(sc->mlx_dev, sc->mlx_irq, INTR_TYPE_BIO |
388 INTR_ENTROPY | INTR_MPSAFE, NULL, mlx_intr, sc, &sc->mlx_intr);
390 device_printf(sc->mlx_dev, "can't set up interrupt\n");
395 * Create DMA tag for mapping buffers into controller-addressable space.
397 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */
398 1, 0, /* align, boundary */
399 BUS_SPACE_MAXADDR, /* lowaddr */
400 BUS_SPACE_MAXADDR, /* highaddr */
401 NULL, NULL, /* filter, filterarg */
402 MLX_MAXPHYS, /* maxsize */
403 MLX_NSEG, /* nsegments */
404 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
406 busdma_lock_mutex, /* lockfunc */
407 &sc->mlx_io_lock, /* lockarg */
408 &sc->mlx_buffer_dmat);
410 device_printf(sc->mlx_dev, "can't allocate buffer DMA tag\n");
415 * Create some initial scatter/gather mappings so we can run the probe
418 error = mlx_sglist_map(sc);
420 device_printf(sc->mlx_dev, "can't make initial s/g list mapping\n");
425 * We don't (yet) know where the event log is up to.
427 sc->mlx_currevent = -1;
430 * Obtain controller feature information
433 if ((sc->mlx_enq2 = mlx_enquire(sc, MLX_CMD_ENQUIRY2, sizeof(struct mlx_enquiry2), NULL)) == NULL) {
435 device_printf(sc->mlx_dev, "ENQUIRY2 failed\n");
440 * Do quirk/feature related things.
442 fwminor = (sc->mlx_enq2->me_firmware_id >> 8) & 0xff;
443 switch(sc->mlx_iftype) {
445 /* These controllers don't report the firmware version in the ENQUIRY2 response */
446 if ((meo = mlx_enquire(sc, MLX_CMD_ENQUIRY_OLD, sizeof(struct mlx_enquiry_old), NULL)) == NULL) {
448 device_printf(sc->mlx_dev, "ENQUIRY_OLD failed\n");
451 sc->mlx_enq2->me_firmware_id = ('0' << 24) | (0 << 16) | (meo->me_fwminor << 8) | meo->me_fwmajor;
453 /* XXX require 2.42 or better (PCI) */
454 if (meo->me_fwminor < 42) {
455 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
456 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 2.42 or later\n");
461 /* XXX certify 3.52? */
463 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
464 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 3.51 or later\n");
468 /* XXX certify firmware versions? */
470 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
471 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 4.06 or later\n");
476 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
477 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 5.07 or later\n");
482 return(ENXIO); /* should never happen */
487 * Create the final scatter/gather mappings now that we have characterised the controller.
489 error = mlx_sglist_map(sc);
491 device_printf(sc->mlx_dev, "can't make final s/g list mapping\n");
496 * No user-requested background operation is in progress.
498 sc->mlx_background = 0;
499 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
502 * Create the control device.
504 sc->mlx_dev_t = make_dev(&mlx_cdevsw, 0, UID_ROOT, GID_OPERATOR,
505 S_IRUSR | S_IWUSR, "mlx%d", device_get_unit(sc->mlx_dev));
506 sc->mlx_dev_t->si_drv1 = sc;
509 * Start the timeout routine.
511 callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
513 /* print a little information about the controller */
514 mlx_describe_controller(sc);
519 /********************************************************************************
520 * Locate disk resources and attach children to them.
523 mlx_startup(struct mlx_softc *sc)
525 struct mlx_enq_sys_drive *mes;
526 struct mlx_sysdrive *dr;
532 * Scan all the system drives and attach children for those that
533 * don't currently have them.
536 mes = mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(*mes) * MLX_MAXDRIVES, NULL);
539 device_printf(sc->mlx_dev, "error fetching drive status\n");
543 /* iterate over drives returned */
545 for (i = 0, dr = &sc->mlx_sysdrive[0];
546 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
548 /* are we already attached to this drive? */
549 if (dr->ms_disk == 0) {
550 /* pick up drive information */
551 dr->ms_size = mes[i].sd_size;
552 dr->ms_raidlevel = mes[i].sd_raidlevel & 0xf;
553 dr->ms_state = mes[i].sd_state;
555 /* generate geometry information */
556 if (sc->mlx_geom == MLX_GEOM_128_32) {
559 dr->ms_cylinders = dr->ms_size / (128 * 32);
560 } else { /* MLX_GEOM_255/63 */
563 dr->ms_cylinders = dr->ms_size / (255 * 63);
565 dr->ms_disk = device_add_child(sc->mlx_dev, /*"mlxd"*/NULL, -1);
566 if (dr->ms_disk == 0)
567 device_printf(sc->mlx_dev, "device_add_child failed\n");
568 device_set_ivars(dr->ms_disk, dr);
572 if ((error = bus_generic_attach(sc->mlx_dev)) != 0)
573 device_printf(sc->mlx_dev, "bus_generic_attach returned %d", error);
575 /* mark controller back up */
577 sc->mlx_state &= ~MLX_STATE_SHUTDOWN;
579 /* enable interrupts */
580 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
582 MLX_CONFIG_UNLOCK(sc);
585 /********************************************************************************
586 * Disconnect from the controller completely, in preparation for unload.
589 mlx_detach(device_t dev)
591 struct mlx_softc *sc = device_get_softc(dev);
592 struct mlxd_softc *mlxd;
599 if (sc->mlx_state & MLX_STATE_OPEN)
602 for (i = 0; i < MLX_MAXDRIVES; i++) {
603 if (sc->mlx_sysdrive[i].ms_disk != 0) {
604 mlxd = device_get_softc(sc->mlx_sysdrive[i].ms_disk);
605 if (mlxd->mlxd_flags & MLXD_OPEN) { /* drive is mounted, abort detach */
606 device_printf(sc->mlx_sysdrive[i].ms_disk, "still open, can't detach\n");
611 if ((error = mlx_shutdown(dev)))
613 MLX_CONFIG_UNLOCK(sc);
619 MLX_CONFIG_UNLOCK(sc);
623 /********************************************************************************
624 * Bring the controller down to a dormant state and detach all child devices.
626 * This function is called before detach, system shutdown, or before performing
627 * an operation which may add or delete system disks. (Call mlx_startup to
628 * resume normal operation.)
630 * Note that we can assume that the bioq on the controller is empty, as we won't
631 * allow shutdown if any device is open.
634 mlx_shutdown(device_t dev)
636 struct mlx_softc *sc = device_get_softc(dev);
640 error = mlx_shutdown_locked(sc);
641 MLX_CONFIG_UNLOCK(sc);
646 mlx_shutdown_locked(struct mlx_softc *sc)
652 MLX_CONFIG_ASSERT_LOCKED(sc);
655 sc->mlx_state |= MLX_STATE_SHUTDOWN;
656 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
658 /* flush controller */
659 device_printf(sc->mlx_dev, "flushing cache...");
667 /* delete all our child devices */
668 for (i = 0; i < MLX_MAXDRIVES; i++) {
669 if (sc->mlx_sysdrive[i].ms_disk != 0) {
670 if ((error = device_delete_child(sc->mlx_dev, sc->mlx_sysdrive[i].ms_disk)) != 0)
672 sc->mlx_sysdrive[i].ms_disk = 0;
679 /********************************************************************************
680 * Bring the controller to a quiescent state, ready for system suspend.
683 mlx_suspend(device_t dev)
685 struct mlx_softc *sc = device_get_softc(dev);
690 sc->mlx_state |= MLX_STATE_SUSPEND;
692 /* flush controller */
693 device_printf(sc->mlx_dev, "flushing cache...");
694 printf("%s\n", mlx_flush(sc) ? "failed" : "done");
696 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
702 /********************************************************************************
703 * Bring the controller back to a state ready for operation.
706 mlx_resume(device_t dev)
708 struct mlx_softc *sc = device_get_softc(dev);
713 sc->mlx_state &= ~MLX_STATE_SUSPEND;
714 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
720 /*******************************************************************************
721 * Take an interrupt, or be poked by other code to look for interrupt-worthy
727 struct mlx_softc *sc = (struct mlx_softc *)arg;
731 /* collect finished commands, queue anything waiting */
737 /*******************************************************************************
738 * Receive a buf structure from a child device and queue it on a particular
739 * disk resource, then poke the disk resource to start as much work as it can.
742 mlx_submit_buf(struct mlx_softc *sc, struct bio *bp)
747 MLX_IO_ASSERT_LOCKED(sc);
748 bioq_insert_tail(&sc->mlx_bioq, bp);
754 /********************************************************************************
755 * Accept an open operation on the control device.
758 mlx_open(struct cdev *dev, int flags, int fmt, struct thread *td)
760 struct mlx_softc *sc = dev->si_drv1;
764 sc->mlx_state |= MLX_STATE_OPEN;
766 MLX_CONFIG_UNLOCK(sc);
770 /********************************************************************************
771 * Accept the last close on the control device.
774 mlx_close(struct cdev *dev, int flags, int fmt, struct thread *td)
776 struct mlx_softc *sc = dev->si_drv1;
780 sc->mlx_state &= ~MLX_STATE_OPEN;
782 MLX_CONFIG_UNLOCK(sc);
786 /********************************************************************************
787 * Handle controller-specific control operations.
790 mlx_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
792 struct mlx_softc *sc = dev->si_drv1;
793 struct mlx_rebuild_request *rb = (struct mlx_rebuild_request *)addr;
794 struct mlx_rebuild_status *rs = (struct mlx_rebuild_status *)addr;
795 int *arg = (int *)addr;
796 struct mlx_pause *mp;
797 struct mlx_sysdrive *dr;
798 struct mlxd_softc *mlxd;
803 * Enumerate connected system drives; returns the first system drive's
804 * unit number if *arg is -1, or the next unit after *arg if it's
805 * a valid unit on this controller.
808 /* search system drives */
810 for (i = 0; i < MLX_MAXDRIVES; i++) {
811 /* is this one attached? */
812 if (sc->mlx_sysdrive[i].ms_disk != 0) {
813 /* looking for the next one we come across? */
815 *arg = device_get_unit(sc->mlx_sysdrive[i].ms_disk);
816 MLX_CONFIG_UNLOCK(sc);
819 /* we want the one after this one */
820 if (*arg == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
824 MLX_CONFIG_UNLOCK(sc);
828 * Scan the controller to see whether new drives have appeared.
830 case MLX_RESCAN_DRIVES:
837 * Disconnect from the specified drive; it may be about to go
840 case MLX_DETACH_DRIVE: /* detach one drive */
842 if (((dr = mlx_findunit(sc, *arg)) == NULL) ||
843 ((mlxd = device_get_softc(dr->ms_disk)) == NULL)) {
844 MLX_CONFIG_UNLOCK(sc);
848 device_printf(dr->ms_disk, "detaching...");
850 if (mlxd->mlxd_flags & MLXD_OPEN) {
855 /* flush controller */
865 if ((error = device_delete_child(sc->mlx_dev, dr->ms_disk)) != 0)
870 MLX_CONFIG_UNLOCK(sc);
879 * Pause one or more SCSI channels for a period of time, to assist
880 * in the process of hot-swapping devices.
882 * Note that at least the 3.51 firmware on the DAC960PL doesn't seem
885 case MLX_PAUSE_CHANNEL: /* schedule a channel pause */
886 /* Does this command work on this firmware? */
887 if (!(sc->mlx_feature & MLX_FEAT_PAUSEWORKS))
890 /* check time values */
891 mp = (struct mlx_pause *)addr;
892 if ((mp->mp_when < 0) || (mp->mp_when > 3600))
894 if ((mp->mp_howlong < 1) || (mp->mp_howlong > (0xf * 30)))
898 if ((mp->mp_which == MLX_PAUSE_CANCEL) && (sc->mlx_pause.mp_when != 0)) {
899 /* cancel a pending pause operation */
900 sc->mlx_pause.mp_which = 0;
902 /* fix for legal channels */
903 mp->mp_which &= ((1 << sc->mlx_enq2->me_actual_channels) -1);
905 /* check for a pause currently running */
906 if ((sc->mlx_pause.mp_which != 0) && (sc->mlx_pause.mp_when == 0)) {
911 /* looks ok, go with it */
912 sc->mlx_pause.mp_which = mp->mp_which;
913 sc->mlx_pause.mp_when = time_second + mp->mp_when;
914 sc->mlx_pause.mp_howlong = sc->mlx_pause.mp_when + mp->mp_howlong;
920 * Accept a command passthrough-style.
923 return(mlx_user_command(sc, (struct mlx_usercommand *)addr));
926 * Start a rebuild on a given SCSI disk
928 case MLX_REBUILDASYNC:
930 if (sc->mlx_background != 0) {
932 rb->rr_status = 0x0106;
935 rb->rr_status = mlx_rebuild(sc, rb->rr_channel, rb->rr_target);
936 switch (rb->rr_status) {
941 error = ENOMEM; /* couldn't set up the command */
960 sc->mlx_background = MLX_BACKGROUND_REBUILD;
965 * Get the status of the current rebuild or consistency check.
967 case MLX_REBUILDSTAT:
969 *rs = sc->mlx_rebuildstat;
974 * Return the per-controller system drive number matching the
975 * disk device number in (arg), if it happens to belong to us.
977 case MLX_GET_SYSDRIVE:
981 mlxd = (struct mlxd_softc *)devclass_get_softc(mlxd_devclass, *arg);
983 if ((mlxd != NULL) && (mlxd->mlxd_drive >= sc->mlx_sysdrive) &&
984 (mlxd->mlxd_drive < (sc->mlx_sysdrive + MLX_MAXDRIVES))) {
986 *arg = mlxd->mlxd_drive - sc->mlx_sysdrive;
988 MLX_CONFIG_UNLOCK(sc);
996 /********************************************************************************
997 * Handle operations requested by a System Drive connected to this controller.
1000 mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd,
1001 caddr_t addr, int32_t flag, struct thread *td)
1003 int *arg = (int *)addr;
1008 * Return the current status of this drive.
1012 *arg = drive->ms_state;
1017 * Start a background consistency check on this drive.
1019 case MLXD_CHECKASYNC: /* start a background consistency check */
1021 if (sc->mlx_background != 0) {
1026 result = mlx_check(sc, drive - &sc->mlx_sysdrive[0]);
1032 error = ENOMEM; /* couldn't set up the command */
1048 sc->mlx_background = MLX_BACKGROUND_CHECK;
1058 /********************************************************************************
1059 ********************************************************************************
1061 ********************************************************************************
1062 ********************************************************************************/
1064 /********************************************************************************
1065 * Fire off commands to periodically check the status of connected drives.
1068 mlx_periodic(void *data)
1070 struct mlx_softc *sc = (struct mlx_softc *)data;
1073 MLX_IO_ASSERT_LOCKED(sc);
1078 if ((sc->mlx_pause.mp_which != 0) &&
1079 (sc->mlx_pause.mp_when > 0) &&
1080 (time_second >= sc->mlx_pause.mp_when)){
1082 mlx_pause_action(sc); /* pause is running */
1083 sc->mlx_pause.mp_when = 0;
1087 * Bus pause still running?
1089 } else if ((sc->mlx_pause.mp_which != 0) &&
1090 (sc->mlx_pause.mp_when == 0)) {
1092 /* time to stop bus pause? */
1093 if (time_second >= sc->mlx_pause.mp_howlong) {
1094 mlx_pause_action(sc);
1095 sc->mlx_pause.mp_which = 0; /* pause is complete */
1098 sysbeep((time_second % 5) * 100 + 500, hz/8);
1102 * Run normal periodic activities?
1104 } else if (time_second > (sc->mlx_lastpoll + 10)) {
1105 sc->mlx_lastpoll = time_second;
1108 * Check controller status.
1110 * XXX Note that this may not actually launch a command in situations of high load.
1112 mlx_enquire(sc, (sc->mlx_iftype == MLX_IFTYPE_2) ? MLX_CMD_ENQUIRY_OLD : MLX_CMD_ENQUIRY,
1113 imax(sizeof(struct mlx_enquiry), sizeof(struct mlx_enquiry_old)), mlx_periodic_enquiry);
1116 * Check system drive status.
1118 * XXX This might be better left to event-driven detection, eg. I/O to an offline
1119 * drive will detect it's offline, rebuilds etc. should detect the drive is back
1122 mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(struct mlx_enq_sys_drive) * MLX_MAXDRIVES,
1123 mlx_periodic_enquiry);
1127 /* get drive rebuild/check status */
1128 /* XXX should check sc->mlx_background if this is only valid while in progress */
1129 mlx_enquire(sc, MLX_CMD_REBUILDSTAT, sizeof(struct mlx_rebuild_stat), mlx_periodic_rebuild);
1131 /* deal with possibly-missed interrupts and timed-out commands */
1134 /* reschedule another poll next second or so */
1135 callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
1138 /********************************************************************************
1139 * Handle the result of an ENQUIRY command instigated by periodic status polling.
1142 mlx_periodic_enquiry(struct mlx_command *mc)
1144 struct mlx_softc *sc = mc->mc_sc;
1147 MLX_IO_ASSERT_LOCKED(sc);
1149 /* Command completed OK? */
1150 if (mc->mc_status != 0) {
1151 device_printf(sc->mlx_dev, "periodic enquiry failed - %s\n", mlx_diagnose_command(mc));
1155 /* respond to command */
1156 switch(mc->mc_mailbox[0]) {
1158 * This is currently a bit fruitless, as we don't know how to extract the eventlog
1161 case MLX_CMD_ENQUIRY_OLD:
1163 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data;
1164 struct mlx_enquiry_old *meo = (struct mlx_enquiry_old *)mc->mc_data;
1167 /* convert data in-place to new format */
1168 for (i = (sizeof(me->me_dead) / sizeof(me->me_dead[0])) - 1; i >= 0; i--) {
1169 me->me_dead[i].dd_chan = meo->me_dead[i].dd_chan;
1170 me->me_dead[i].dd_targ = meo->me_dead[i].dd_targ;
1172 me->me_misc_flags = 0;
1173 me->me_rebuild_count = meo->me_rebuild_count;
1174 me->me_dead_count = meo->me_dead_count;
1175 me->me_critical_sd_count = meo->me_critical_sd_count;
1176 me->me_event_log_seq_num = 0;
1177 me->me_offline_sd_count = meo->me_offline_sd_count;
1178 me->me_max_commands = meo->me_max_commands;
1179 me->me_rebuild_flag = meo->me_rebuild_flag;
1180 me->me_fwmajor = meo->me_fwmajor;
1181 me->me_fwminor = meo->me_fwminor;
1182 me->me_status_flags = meo->me_status_flags;
1183 me->me_flash_age = meo->me_flash_age;
1184 for (i = (sizeof(me->me_drvsize) / sizeof(me->me_drvsize[0])) - 1; i >= 0; i--) {
1185 if (i > ((sizeof(meo->me_drvsize) / sizeof(meo->me_drvsize[0])) - 1)) {
1186 me->me_drvsize[i] = 0; /* drive beyond supported range */
1188 me->me_drvsize[i] = meo->me_drvsize[i];
1191 me->me_num_sys_drvs = meo->me_num_sys_drvs;
1196 * Generic controller status update. We could do more with this than just
1197 * checking the event log.
1199 case MLX_CMD_ENQUIRY:
1201 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data;
1203 if (sc->mlx_currevent == -1) {
1204 /* initialise our view of the event log */
1205 sc->mlx_currevent = sc->mlx_lastevent = me->me_event_log_seq_num;
1206 } else if ((me->me_event_log_seq_num != sc->mlx_lastevent) && !(sc->mlx_flags & MLX_EVENTLOG_BUSY)) {
1207 /* record where current events are up to */
1208 sc->mlx_currevent = me->me_event_log_seq_num;
1209 debug(1, "event log pointer was %d, now %d\n", sc->mlx_lastevent, sc->mlx_currevent);
1211 /* mark the event log as busy */
1212 sc->mlx_flags |= MLX_EVENTLOG_BUSY;
1214 /* drain new eventlog entries */
1215 mlx_periodic_eventlog_poll(sc);
1219 case MLX_CMD_ENQSYSDRIVE:
1221 struct mlx_enq_sys_drive *mes = (struct mlx_enq_sys_drive *)mc->mc_data;
1222 struct mlx_sysdrive *dr;
1225 for (i = 0, dr = &sc->mlx_sysdrive[0];
1226 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
1229 /* has state been changed by controller? */
1230 if (dr->ms_state != mes[i].sd_state) {
1231 switch(mes[i].sd_state) {
1232 case MLX_SYSD_OFFLINE:
1233 device_printf(dr->ms_disk, "drive offline\n");
1235 case MLX_SYSD_ONLINE:
1236 device_printf(dr->ms_disk, "drive online\n");
1238 case MLX_SYSD_CRITICAL:
1239 device_printf(dr->ms_disk, "drive critical\n");
1242 /* save new state */
1243 dr->ms_state = mes[i].sd_state;
1249 device_printf(sc->mlx_dev, "%s: unknown command 0x%x", __func__, mc->mc_mailbox[0]);
1254 free(mc->mc_data, M_DEVBUF);
1259 mlx_eventlog_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1261 struct mlx_command *mc;
1263 mc = (struct mlx_command *)arg;
1264 mlx_setup_dmamap(mc, segs, nsegments, error);
1266 /* build the command to get one entry */
1267 mlx_make_type3(mc, MLX_CMD_LOGOP, MLX_LOGOP_GET, 1,
1268 mc->mc_sc->mlx_lastevent, 0, 0, mc->mc_dataphys, 0);
1269 mc->mc_complete = mlx_periodic_eventlog_respond;
1270 mc->mc_private = mc;
1272 /* start the command */
1273 if (mlx_start(mc) != 0) {
1275 free(mc->mc_data, M_DEVBUF);
1281 /********************************************************************************
1282 * Instigate a poll for one event log message on (sc).
1283 * We only poll for one message at a time, to keep our command usage down.
1286 mlx_periodic_eventlog_poll(struct mlx_softc *sc)
1288 struct mlx_command *mc;
1289 void *result = NULL;
1293 MLX_IO_ASSERT_LOCKED(sc);
1295 /* get ourselves a command buffer */
1297 if ((mc = mlx_alloccmd(sc)) == NULL)
1300 /* allocate the response structure */
1301 if ((result = malloc(/*sizeof(struct mlx_eventlog_entry)*/1024, M_DEVBUF,
1305 /* get a command slot */
1306 if (mlx_getslot(mc))
1309 /* map the command so the controller can see it */
1310 mc->mc_data = result;
1311 mc->mc_length = /*sizeof(struct mlx_eventlog_entry)*/1024;
1312 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
1313 mc->mc_length, mlx_eventlog_cb, mc, BUS_DMA_NOWAIT);
1319 if ((result != NULL) && (mc->mc_data != NULL))
1320 free(result, M_DEVBUF);
1324 /********************************************************************************
1325 * Handle the result of polling for a log message, generate diagnostic output.
1326 * If this wasn't the last message waiting for us, we'll go collect another.
1328 static char *mlx_sense_messages[] = {
1329 "because write recovery failed",
1330 "because of SCSI bus reset failure",
1331 "because of double check condition",
1332 "because it was removed",
1333 "because of gross error on SCSI chip",
1334 "because of bad tag returned from drive",
1335 "because of timeout on SCSI command",
1336 "because of reset SCSI command issued from system",
1337 "because busy or parity error count exceeded limit",
1338 "because of 'kill drive' command from system",
1339 "because of selection timeout",
1340 "due to SCSI phase sequence error",
1341 "due to unknown status"
1345 mlx_periodic_eventlog_respond(struct mlx_command *mc)
1347 struct mlx_softc *sc = mc->mc_sc;
1348 struct mlx_eventlog_entry *el = (struct mlx_eventlog_entry *)mc->mc_data;
1352 MLX_IO_ASSERT_LOCKED(sc);
1354 sc->mlx_lastevent++; /* next message... */
1355 if (mc->mc_status == 0) {
1357 /* handle event log message */
1358 switch(el->el_type) {
1360 * This is the only sort of message we understand at the moment.
1361 * The tests here are probably incomplete.
1363 case MLX_LOGMSG_SENSE: /* sense data */
1364 /* Mylex vendor-specific message indicating a drive was killed? */
1365 if ((el->el_sensekey == 9) &&
1366 (el->el_asc == 0x80)) {
1367 if (el->el_asq < nitems(mlx_sense_messages)) {
1368 reason = mlx_sense_messages[el->el_asq];
1370 reason = "for unknown reason";
1372 device_printf(sc->mlx_dev, "physical drive %d:%d killed %s\n",
1373 el->el_channel, el->el_target, reason);
1375 /* SCSI drive was reset? */
1376 if ((el->el_sensekey == 6) && (el->el_asc == 0x29)) {
1377 device_printf(sc->mlx_dev, "physical drive %d:%d reset\n",
1378 el->el_channel, el->el_target);
1380 /* SCSI drive error? */
1381 if (!((el->el_sensekey == 0) ||
1382 ((el->el_sensekey == 2) &&
1383 (el->el_asc == 0x04) &&
1384 ((el->el_asq == 0x01) ||
1385 (el->el_asq == 0x02))))) {
1386 device_printf(sc->mlx_dev, "physical drive %d:%d error log: sense = %d asc = %x asq = %x\n",
1387 el->el_channel, el->el_target, el->el_sensekey, el->el_asc, el->el_asq);
1388 device_printf(sc->mlx_dev, " info %4D csi %4D\n", el->el_information, ":", el->el_csi, ":");
1393 device_printf(sc->mlx_dev, "unknown log message type 0x%x\n", el->el_type);
1397 device_printf(sc->mlx_dev, "error reading message log - %s\n", mlx_diagnose_command(mc));
1398 /* give up on all the outstanding messages, as we may have come unsynched */
1399 sc->mlx_lastevent = sc->mlx_currevent;
1402 /* dispose of command and data */
1403 free(mc->mc_data, M_DEVBUF);
1406 /* is there another message to obtain? */
1407 if (sc->mlx_lastevent != sc->mlx_currevent) {
1408 mlx_periodic_eventlog_poll(sc);
1410 /* clear log-busy status */
1411 sc->mlx_flags &= ~MLX_EVENTLOG_BUSY;
1415 /********************************************************************************
1416 * Handle check/rebuild operations in progress.
1419 mlx_periodic_rebuild(struct mlx_command *mc)
1421 struct mlx_softc *sc = mc->mc_sc;
1422 struct mlx_rebuild_status *mr = (struct mlx_rebuild_status *)mc->mc_data;
1424 MLX_IO_ASSERT_LOCKED(sc);
1425 switch(mc->mc_status) {
1426 case 0: /* operation running, update stats */
1427 sc->mlx_rebuildstat = *mr;
1429 /* spontaneous rebuild/check? */
1430 if (sc->mlx_background == 0) {
1431 sc->mlx_background = MLX_BACKGROUND_SPONTANEOUS;
1432 device_printf(sc->mlx_dev, "background check/rebuild operation started\n");
1436 case 0x0105: /* nothing running, finalise stats and report */
1437 switch(sc->mlx_background) {
1438 case MLX_BACKGROUND_CHECK:
1439 device_printf(sc->mlx_dev, "consistency check completed\n"); /* XXX print drive? */
1441 case MLX_BACKGROUND_REBUILD:
1442 device_printf(sc->mlx_dev, "drive rebuild completed\n"); /* XXX print channel/target? */
1444 case MLX_BACKGROUND_SPONTANEOUS:
1446 /* if we have previously been non-idle, report the transition */
1447 if (sc->mlx_rebuildstat.rs_code != MLX_REBUILDSTAT_IDLE) {
1448 device_printf(sc->mlx_dev, "background check/rebuild operation completed\n");
1451 sc->mlx_background = 0;
1452 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
1455 free(mc->mc_data, M_DEVBUF);
1459 /********************************************************************************
1460 ********************************************************************************
1462 ********************************************************************************
1463 ********************************************************************************/
1465 /********************************************************************************
1466 * It's time to perform a channel pause action for (sc), either start or stop
1470 mlx_pause_action(struct mlx_softc *sc)
1472 struct mlx_command *mc;
1473 int failsafe, i, command;
1475 MLX_IO_ASSERT_LOCKED(sc);
1477 /* What are we doing here? */
1478 if (sc->mlx_pause.mp_when == 0) {
1479 command = MLX_CMD_STARTCHANNEL;
1483 command = MLX_CMD_STOPCHANNEL;
1486 * Channels will always start again after the failsafe period,
1487 * which is specified in multiples of 30 seconds.
1488 * This constrains us to a maximum pause of 450 seconds.
1490 failsafe = ((sc->mlx_pause.mp_howlong - time_second) + 5) / 30;
1491 if (failsafe > 0xf) {
1493 sc->mlx_pause.mp_howlong = time_second + (0xf * 30) - 5;
1497 /* build commands for every channel requested */
1498 for (i = 0; i < sc->mlx_enq2->me_actual_channels; i++) {
1499 if ((1 << i) & sc->mlx_pause.mp_which) {
1501 /* get ourselves a command buffer */
1502 if ((mc = mlx_alloccmd(sc)) == NULL)
1504 /* get a command slot */
1505 mc->mc_flags |= MLX_CMD_PRIORITY;
1506 if (mlx_getslot(mc))
1509 /* build the command */
1510 mlx_make_type2(mc, command, (failsafe << 4) | i, 0, 0, 0, 0, 0, 0, 0);
1511 mc->mc_complete = mlx_pause_done;
1512 mc->mc_private = sc; /* XXX not needed */
1515 /* command submitted OK */
1519 device_printf(sc->mlx_dev, "%s failed for channel %d\n",
1520 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", i);
1528 mlx_pause_done(struct mlx_command *mc)
1530 struct mlx_softc *sc = mc->mc_sc;
1531 int command = mc->mc_mailbox[0];
1532 int channel = mc->mc_mailbox[2] & 0xf;
1534 MLX_IO_ASSERT_LOCKED(sc);
1535 if (mc->mc_status != 0) {
1536 device_printf(sc->mlx_dev, "%s command failed - %s\n",
1537 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", mlx_diagnose_command(mc));
1538 } else if (command == MLX_CMD_STOPCHANNEL) {
1539 device_printf(sc->mlx_dev, "channel %d pausing for %ld seconds\n",
1540 channel, (long)(sc->mlx_pause.mp_howlong - time_second));
1542 device_printf(sc->mlx_dev, "channel %d resuming\n", channel);
1547 /********************************************************************************
1548 ********************************************************************************
1550 ********************************************************************************
1551 ********************************************************************************/
1554 mlx_enquire_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1556 struct mlx_softc *sc;
1557 struct mlx_command *mc;
1559 mc = (struct mlx_command *)arg;
1563 mlx_setup_dmamap(mc, segs, nsegments, error);
1565 /* build an enquiry command */
1567 mlx_make_type2(mc, mc->mc_command, 0, 0, 0, 0, 0, 0, mc->mc_dataphys, 0);
1569 /* do we want a completion callback? */
1570 if (mc->mc_complete != NULL) {
1571 if ((error = mlx_start(mc)) != 0)
1574 /* run the command in either polled or wait mode */
1575 if ((sc->mlx_state & MLX_STATE_INTEN) ? mlx_wait_command(mc) :
1576 mlx_poll_command(mc))
1579 /* command completed OK? */
1580 if (mc->mc_status != 0) {
1581 device_printf(sc->mlx_dev, "ENQUIRY failed - %s\n",
1582 mlx_diagnose_command(mc));
1588 /********************************************************************************
1589 * Perform an Enquiry command using a type-3 command buffer and a return a single
1590 * linear result buffer. If the completion function is specified, it will
1591 * be called with the completed command (and the result response will not be
1592 * valid until that point). Otherwise, the command will either be busy-waited
1593 * for (interrupts not enabled), or slept for.
1596 mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize, void (* complete)(struct mlx_command *mc))
1598 struct mlx_command *mc;
1603 MLX_IO_ASSERT_LOCKED(sc);
1605 /* get ourselves a command buffer */
1608 if ((mc = mlx_alloccmd(sc)) == NULL)
1610 /* allocate the response structure */
1611 if ((result = malloc(bufsize, M_DEVBUF, M_NOWAIT)) == NULL)
1613 /* get a command slot */
1614 mc->mc_flags |= MLX_CMD_PRIORITY | MLX_CMD_DATAOUT;
1615 if (mlx_getslot(mc))
1618 /* map the command so the controller can see it */
1619 mc->mc_data = result;
1620 mc->mc_length = bufsize;
1621 mc->mc_command = command;
1623 if (complete != NULL) {
1624 mc->mc_complete = complete;
1625 mc->mc_private = mc;
1628 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
1629 mc->mc_length, mlx_enquire_cb, mc, BUS_DMA_NOWAIT);
1632 /* we got a command, but nobody else will free it */
1633 if ((mc != NULL) && (mc->mc_complete == NULL))
1635 /* we got an error, and we allocated a result */
1636 if ((error != 0) && (result != NULL)) {
1637 free(result, M_DEVBUF);
1644 /********************************************************************************
1645 * Perform a Flush command on the nominated controller.
1647 * May be called with interrupts enabled or disabled; will not return until
1648 * the flush operation completes or fails.
1651 mlx_flush(struct mlx_softc *sc)
1653 struct mlx_command *mc;
1657 MLX_IO_ASSERT_LOCKED(sc);
1659 /* get ourselves a command buffer */
1661 if ((mc = mlx_alloccmd(sc)) == NULL)
1663 /* get a command slot */
1664 if (mlx_getslot(mc))
1667 /* build a flush command */
1668 mlx_make_type2(mc, MLX_CMD_FLUSH, 0, 0, 0, 0, 0, 0, 0, 0);
1670 /* can't assume that interrupts are going to work here, so play it safe */
1671 if (mlx_poll_command(mc))
1674 /* command completed OK? */
1675 if (mc->mc_status != 0) {
1676 device_printf(sc->mlx_dev, "FLUSH failed - %s\n", mlx_diagnose_command(mc));
1680 error = 0; /* success */
1687 /********************************************************************************
1688 * Start a background consistency check on (drive).
1690 * May be called with interrupts enabled or disabled; will return as soon as the
1691 * operation has started or been refused.
1694 mlx_check(struct mlx_softc *sc, int drive)
1696 struct mlx_command *mc;
1700 MLX_IO_ASSERT_LOCKED(sc);
1702 /* get ourselves a command buffer */
1704 if ((mc = mlx_alloccmd(sc)) == NULL)
1706 /* get a command slot */
1707 if (mlx_getslot(mc))
1710 /* build a checkasync command, set the "fix it" flag */
1711 mlx_make_type2(mc, MLX_CMD_CHECKASYNC, 0, 0, 0, 0, 0, drive | 0x80, 0, 0);
1713 /* start the command and wait for it to be returned */
1714 if (mlx_wait_command(mc))
1717 /* command completed OK? */
1718 if (mc->mc_status != 0) {
1719 device_printf(sc->mlx_dev, "CHECK ASYNC failed - %s\n", mlx_diagnose_command(mc));
1721 device_printf(sc->mlx_sysdrive[drive].ms_disk, "consistency check started");
1723 error = mc->mc_status;
1731 /********************************************************************************
1732 * Start a background rebuild of the physical drive at (channel),(target).
1734 * May be called with interrupts enabled or disabled; will return as soon as the
1735 * operation has started or been refused.
1738 mlx_rebuild(struct mlx_softc *sc, int channel, int target)
1740 struct mlx_command *mc;
1744 MLX_IO_ASSERT_LOCKED(sc);
1746 /* get ourselves a command buffer */
1748 if ((mc = mlx_alloccmd(sc)) == NULL)
1750 /* get a command slot */
1751 if (mlx_getslot(mc))
1754 /* build a checkasync command, set the "fix it" flag */
1755 mlx_make_type2(mc, MLX_CMD_REBUILDASYNC, channel, target, 0, 0, 0, 0, 0, 0);
1757 /* start the command and wait for it to be returned */
1758 if (mlx_wait_command(mc))
1761 /* command completed OK? */
1762 if (mc->mc_status != 0) {
1763 device_printf(sc->mlx_dev, "REBUILD ASYNC failed - %s\n", mlx_diagnose_command(mc));
1765 device_printf(sc->mlx_dev, "drive rebuild started for %d:%d\n", channel, target);
1767 error = mc->mc_status;
1775 /********************************************************************************
1776 * Run the command (mc) and return when it completes.
1778 * Interrupts need to be enabled; returns nonzero on error.
1781 mlx_wait_command(struct mlx_command *mc)
1783 struct mlx_softc *sc = mc->mc_sc;
1787 MLX_IO_ASSERT_LOCKED(sc);
1789 mc->mc_complete = NULL;
1790 mc->mc_private = mc; /* wake us when you're done */
1791 if ((error = mlx_start(mc)) != 0)
1795 /* XXX better timeout? */
1796 while ((mc->mc_status == MLX_STATUS_BUSY) && (count < 30)) {
1797 mtx_sleep(mc->mc_private, &sc->mlx_io_lock, PRIBIO | PCATCH, "mlxwcmd", hz);
1800 if (mc->mc_status != 0) {
1801 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1808 /********************************************************************************
1809 * Start the command (mc) and busy-wait for it to complete.
1811 * Should only be used when interrupts can't be relied upon. Returns 0 on
1812 * success, nonzero on error.
1813 * Successfully completed commands are dequeued.
1816 mlx_poll_command(struct mlx_command *mc)
1818 struct mlx_softc *sc = mc->mc_sc;
1822 MLX_IO_ASSERT_LOCKED(sc);
1824 mc->mc_complete = NULL;
1825 mc->mc_private = NULL; /* we will poll for it */
1826 if ((error = mlx_start(mc)) != 0)
1831 /* poll for completion */
1832 mlx_done(mc->mc_sc, 1);
1834 } while ((mc->mc_status == MLX_STATUS_BUSY) && (count++ < 15000000));
1835 if (mc->mc_status != MLX_STATUS_BUSY) {
1836 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
1839 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1844 mlx_startio_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1846 struct mlx_command *mc;
1847 struct mlxd_softc *mlxd;
1848 struct mlx_softc *sc;
1854 mc = (struct mlx_command *)arg;
1855 mlx_setup_dmamap(mc, segs, nsegments, error);
1858 bp = mc->mc_private;
1860 if (bp->bio_cmd == BIO_READ) {
1861 mc->mc_flags |= MLX_CMD_DATAIN;
1862 cmd = MLX_CMD_READSG;
1864 mc->mc_flags |= MLX_CMD_DATAOUT;
1865 cmd = MLX_CMD_WRITESG;
1868 /* build a suitable I/O command (assumes 512-byte rounded transfers) */
1869 mlxd = bp->bio_disk->d_drv1;
1870 driveno = mlxd->mlxd_drive - sc->mlx_sysdrive;
1871 blkcount = howmany(bp->bio_bcount, MLX_BLKSIZE);
1873 if ((bp->bio_pblkno + blkcount) > sc->mlx_sysdrive[driveno].ms_size)
1874 device_printf(sc->mlx_dev,
1875 "I/O beyond end of unit (%lld,%d > %lu)\n",
1876 (long long)bp->bio_pblkno, blkcount,
1877 (u_long)sc->mlx_sysdrive[driveno].ms_size);
1880 * Build the I/O command. Note that the SG list type bits are set to zero,
1881 * denoting the format of SG list that we are using.
1883 if (sc->mlx_iftype == MLX_IFTYPE_2) {
1884 mlx_make_type1(mc, (cmd == MLX_CMD_WRITESG) ? MLX_CMD_WRITESG_OLD :
1886 blkcount & 0xff, /* xfer length low byte */
1887 bp->bio_pblkno, /* physical block number */
1888 driveno, /* target drive number */
1889 mc->mc_sgphys, /* location of SG list */
1890 mc->mc_nsgent & 0x3f); /* size of SG list */
1892 mlx_make_type5(mc, cmd,
1893 blkcount & 0xff, /* xfer length low byte */
1894 (driveno << 3) | ((blkcount >> 8) & 0x07),
1895 /* target+length high 3 bits */
1896 bp->bio_pblkno, /* physical block number */
1897 mc->mc_sgphys, /* location of SG list */
1898 mc->mc_nsgent & 0x3f); /* size of SG list */
1901 /* try to give command to controller */
1902 if (mlx_start(mc) != 0) {
1903 /* fail the command */
1904 mc->mc_status = MLX_STATUS_WEDGED;
1908 sc->mlx_state &= ~MLX_STATE_QFROZEN;
1911 /********************************************************************************
1912 * Pull as much work off the softc's work queue as possible and give it to the
1913 * controller. Leave a couple of slots free for emergencies.
1916 mlx_startio(struct mlx_softc *sc)
1918 struct mlx_command *mc;
1922 MLX_IO_ASSERT_LOCKED(sc);
1924 /* spin until something prevents us from doing any work */
1926 if (sc->mlx_state & MLX_STATE_QFROZEN)
1929 /* see if there's work to be done */
1930 if ((bp = bioq_first(&sc->mlx_bioq)) == NULL)
1933 if ((mc = mlx_alloccmd(sc)) == NULL)
1935 /* get a slot for the command */
1936 if (mlx_getslot(mc) != 0) {
1940 /* get the buf containing our work */
1941 bioq_remove(&sc->mlx_bioq, bp);
1944 /* connect the buf to the command */
1945 mc->mc_complete = mlx_completeio;
1946 mc->mc_private = bp;
1947 mc->mc_data = bp->bio_data;
1948 mc->mc_length = bp->bio_bcount;
1950 /* map the command so the controller can work with it */
1951 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
1952 mc->mc_length, mlx_startio_cb, mc, 0);
1953 if (error == EINPROGRESS) {
1954 sc->mlx_state |= MLX_STATE_QFROZEN;
1960 /********************************************************************************
1961 * Handle completion of an I/O command.
1964 mlx_completeio(struct mlx_command *mc)
1966 struct mlx_softc *sc = mc->mc_sc;
1967 struct bio *bp = mc->mc_private;
1968 struct mlxd_softc *mlxd = bp->bio_disk->d_drv1;
1970 MLX_IO_ASSERT_LOCKED(sc);
1971 if (mc->mc_status != MLX_STATUS_OK) { /* could be more verbose here? */
1972 bp->bio_error = EIO;
1973 bp->bio_flags |= BIO_ERROR;
1975 switch(mc->mc_status) {
1976 case MLX_STATUS_RDWROFFLINE: /* system drive has gone offline */
1977 device_printf(mlxd->mlxd_dev, "drive offline\n");
1978 /* should signal this with a return code */
1979 mlxd->mlxd_drive->ms_state = MLX_SYSD_OFFLINE;
1982 default: /* other I/O error */
1983 device_printf(sc->mlx_dev, "I/O error - %s\n", mlx_diagnose_command(mc));
1985 device_printf(sc->mlx_dev, " b_bcount %ld blkcount %ld b_pblkno %d\n",
1986 bp->bio_bcount, bp->bio_bcount / MLX_BLKSIZE, bp->bio_pblkno);
1987 device_printf(sc->mlx_dev, " %13D\n", mc->mc_mailbox, " ");
1997 mlx_user_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1999 struct mlx_usercommand *mu;
2000 struct mlx_command *mc;
2001 struct mlx_dcdb *dcdb;
2003 mc = (struct mlx_command *)arg;
2007 mlx_setup_dmamap(mc, segs, nsegments, error);
2009 mu = (struct mlx_usercommand *)mc->mc_private;
2013 * If this is a passthrough SCSI command, the DCDB is packed at the
2014 * beginning of the data area. Fix up the DCDB to point to the correct
2015 * physical address and override any bufptr supplied by the caller since
2016 * we know what it's meant to be.
2018 if (mc->mc_mailbox[0] == MLX_CMD_DIRECT_CDB) {
2019 dcdb = (struct mlx_dcdb *)mc->mc_data;
2020 dcdb->dcdb_physaddr = mc->mc_dataphys + sizeof(*dcdb);
2025 * If there's a data buffer, fix up the command's buffer pointer.
2027 if (mu->mu_datasize > 0) {
2028 mc->mc_mailbox[mu->mu_bufptr ] = mc->mc_dataphys & 0xff;
2029 mc->mc_mailbox[mu->mu_bufptr + 1] = (mc->mc_dataphys >> 8) & 0xff;
2030 mc->mc_mailbox[mu->mu_bufptr + 2] = (mc->mc_dataphys >> 16) & 0xff;
2031 mc->mc_mailbox[mu->mu_bufptr + 3] = (mc->mc_dataphys >> 24) & 0xff;
2033 debug(0, "command fixup");
2035 /* submit the command and wait */
2036 if (mlx_wait_command(mc) != 0)
2041 /********************************************************************************
2042 * Take a command from user-space and try to run it.
2044 * XXX Note that this can't perform very much in the way of error checking, and
2045 * as such, applications _must_ be considered trustworthy.
2046 * XXX Commands using S/G for data are not supported.
2049 mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu)
2051 struct mlx_command *mc;
2061 /* get ourselves a command and copy in from user space */
2063 if ((mc = mlx_alloccmd(sc)) == NULL) {
2067 bcopy(mu->mu_command, mc->mc_mailbox, sizeof(mc->mc_mailbox));
2068 debug(0, "got command buffer");
2071 * if we need a buffer for data transfer, allocate one and copy in its
2074 if (mu->mu_datasize > 0) {
2075 if (mu->mu_datasize > MLX_MAXPHYS) {
2080 if (((kbuf = malloc(mu->mu_datasize, M_DEVBUF, M_WAITOK)) == NULL) ||
2081 (error = copyin(mu->mu_buf, kbuf, mu->mu_datasize))) {
2086 debug(0, "got kernel buffer");
2089 /* get a command slot */
2090 if (mlx_getslot(mc))
2092 debug(0, "got a slot");
2094 if (mu->mu_datasize > 0) {
2096 /* range check the pointer to physical buffer address */
2097 if ((mu->mu_bufptr < 0) || (mu->mu_bufptr > (sizeof(mu->mu_command) -
2098 sizeof(u_int32_t)))) {
2104 /* map the command so the controller can see it */
2106 mc->mc_length = mu->mu_datasize;
2107 mc->mc_private = mu;
2108 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
2109 mc->mc_length, mlx_user_cb, mc, BUS_DMA_NOWAIT);
2113 /* copy out status and data */
2114 mu->mu_status = mc->mc_status;
2115 if (mu->mu_datasize > 0) {
2117 error = copyout(kbuf, mu->mu_buf, mu->mu_datasize);
2125 free(kbuf, M_DEVBUF);
2129 /********************************************************************************
2130 ********************************************************************************
2131 Command I/O to Controller
2132 ********************************************************************************
2133 ********************************************************************************/
2135 /********************************************************************************
2136 * Find a free command slot for (mc).
2138 * Don't hand out a slot to a normal-priority command unless there are at least
2139 * 4 slots free for priority commands.
2142 mlx_getslot(struct mlx_command *mc)
2144 struct mlx_softc *sc = mc->mc_sc;
2149 MLX_IO_ASSERT_LOCKED(sc);
2152 * Enforce slot-usage limit, if we have the required information.
2154 if (sc->mlx_enq2 != NULL) {
2155 limit = sc->mlx_enq2->me_max_commands;
2159 if (sc->mlx_busycmds >= ((mc->mc_flags & MLX_CMD_PRIORITY) ? limit : limit - 4))
2163 * Allocate an outstanding command slot
2165 * XXX linear search is slow
2167 for (slot = 0; slot < limit; slot++) {
2168 debug(2, "try slot %d", slot);
2169 if (sc->mlx_busycmd[slot] == NULL)
2173 sc->mlx_busycmd[slot] = mc;
2181 debug(2, "got slot %d", slot);
2186 /********************************************************************************
2187 * Map/unmap (mc)'s data in the controller's addressable space.
2190 mlx_setup_dmamap(struct mlx_command *mc, bus_dma_segment_t *segs, int nsegments,
2193 struct mlx_softc *sc = mc->mc_sc;
2194 struct mlx_sgentry *sg;
2199 /* XXX should be unnecessary */
2200 if (sc->mlx_enq2 && (nsegments > sc->mlx_enq2->me_max_sg))
2201 panic("MLX: too many s/g segments (%d, max %d)", nsegments,
2202 sc->mlx_enq2->me_max_sg);
2204 /* get base address of s/g table */
2205 sg = sc->mlx_sgtable + (mc->mc_slot * MLX_NSEG);
2207 /* save s/g table information in command */
2208 mc->mc_nsgent = nsegments;
2209 mc->mc_sgphys = sc->mlx_sgbusaddr +
2210 (mc->mc_slot * MLX_NSEG * sizeof(struct mlx_sgentry));
2211 mc->mc_dataphys = segs[0].ds_addr;
2213 /* populate s/g table */
2214 for (i = 0; i < nsegments; i++, sg++) {
2215 sg->sg_addr = segs[i].ds_addr;
2216 sg->sg_count = segs[i].ds_len;
2219 /* Make sure the buffers are visible on the bus. */
2220 if (mc->mc_flags & MLX_CMD_DATAIN)
2221 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap,
2222 BUS_DMASYNC_PREREAD);
2223 if (mc->mc_flags & MLX_CMD_DATAOUT)
2224 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap,
2225 BUS_DMASYNC_PREWRITE);
2229 mlx_unmapcmd(struct mlx_command *mc)
2231 struct mlx_softc *sc = mc->mc_sc;
2235 /* if the command involved data at all */
2236 if (mc->mc_data != NULL) {
2238 if (mc->mc_flags & MLX_CMD_DATAIN)
2239 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTREAD);
2240 if (mc->mc_flags & MLX_CMD_DATAOUT)
2241 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTWRITE);
2243 bus_dmamap_unload(sc->mlx_buffer_dmat, mc->mc_dmamap);
2247 /********************************************************************************
2248 * Try to deliver (mc) to the controller.
2250 * Can be called at any interrupt level, with or without interrupts enabled.
2253 mlx_start(struct mlx_command *mc)
2255 struct mlx_softc *sc = mc->mc_sc;
2260 /* save the slot number as ident so we can handle this command when complete */
2261 mc->mc_mailbox[0x1] = mc->mc_slot;
2263 /* mark the command as currently being processed */
2264 mc->mc_status = MLX_STATUS_BUSY;
2266 /* set a default 60-second timeout XXX tunable? XXX not currently used */
2267 mc->mc_timeout = time_second + 60;
2269 /* spin waiting for the mailbox */
2270 for (i = 100000; i > 0; i--) {
2271 if (sc->mlx_tryqueue(sc, mc)) {
2272 /* move command to work queue */
2273 TAILQ_INSERT_TAIL(&sc->mlx_work, mc, mc_link);
2280 * We couldn't get the controller to take the command. Revoke the slot
2281 * that the command was given and return it with a bad status.
2283 sc->mlx_busycmd[mc->mc_slot] = NULL;
2284 device_printf(sc->mlx_dev, "controller wedged (not taking commands)\n");
2285 mc->mc_status = MLX_STATUS_WEDGED;
2290 /********************************************************************************
2291 * Poll the controller (sc) for completed commands.
2292 * Update command status and free slots for reuse. If any slots were freed,
2293 * new commands may be posted.
2295 * Returns nonzero if one or more commands were completed.
2298 mlx_done(struct mlx_softc *sc, int startio)
2300 struct mlx_command *mc;
2306 MLX_IO_ASSERT_LOCKED(sc);
2310 /* loop collecting completed commands */
2312 /* poll for a completed command's identifier and status */
2313 if (sc->mlx_findcomplete(sc, &slot, &status)) {
2315 mc = sc->mlx_busycmd[slot]; /* find command */
2316 if (mc != NULL) { /* paranoia */
2317 if (mc->mc_status == MLX_STATUS_BUSY) {
2318 mc->mc_status = status; /* save status */
2320 /* free slot for reuse */
2321 sc->mlx_busycmd[slot] = NULL;
2324 device_printf(sc->mlx_dev, "duplicate done event for slot %d\n", slot);
2327 device_printf(sc->mlx_dev, "done event for nonbusy slot %d\n", slot);
2334 /* if we've completed any commands, try posting some more */
2335 if (result && startio)
2338 /* handle completion and timeouts */
2344 /********************************************************************************
2345 * Perform post-completion processing for commands on (sc).
2348 mlx_complete(struct mlx_softc *sc)
2350 struct mlx_command *mc, *nc;
2353 MLX_IO_ASSERT_LOCKED(sc);
2355 /* scan the list of busy/done commands */
2356 mc = TAILQ_FIRST(&sc->mlx_work);
2357 while (mc != NULL) {
2358 nc = TAILQ_NEXT(mc, mc_link);
2360 /* Command has been completed in some fashion */
2361 if (mc->mc_status != MLX_STATUS_BUSY) {
2363 /* unmap the command's data buffer */
2366 * Does the command have a completion handler?
2368 if (mc->mc_complete != NULL) {
2369 /* remove from list and give to handler */
2370 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2371 mc->mc_complete(mc);
2374 * Is there a sleeper waiting on this command?
2376 } else if (mc->mc_private != NULL) { /* sleeping caller wants to know about it */
2378 /* remove from list and wake up sleeper */
2379 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2380 wakeup_one(mc->mc_private);
2383 * Leave the command for a caller that's polling for it.
2392 /********************************************************************************
2393 ********************************************************************************
2394 Command Buffer Management
2395 ********************************************************************************
2396 ********************************************************************************/
2398 /********************************************************************************
2399 * Get a new command buffer.
2401 * This may return NULL in low-memory cases.
2403 * Note that using malloc() is expensive (the command buffer is << 1 page) but
2404 * necessary if we are to be a loadable module before the zone allocator is fixed.
2406 * If possible, we recycle a command buffer that's been used before.
2408 * XXX Note that command buffers are not cleaned out - it is the caller's
2409 * responsibility to ensure that all required fields are filled in before
2412 static struct mlx_command *
2413 mlx_alloccmd(struct mlx_softc *sc)
2415 struct mlx_command *mc;
2420 MLX_IO_ASSERT_LOCKED(sc);
2421 if ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL)
2422 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
2424 /* allocate a new command buffer? */
2426 mc = (struct mlx_command *)malloc(sizeof(*mc), M_DEVBUF, M_NOWAIT | M_ZERO);
2429 error = bus_dmamap_create(sc->mlx_buffer_dmat, 0, &mc->mc_dmamap);
2439 /********************************************************************************
2440 * Release a command buffer for recycling.
2442 * XXX It might be a good idea to limit the number of commands we save for reuse
2443 * if it's shown that this list bloats out massively.
2446 mlx_releasecmd(struct mlx_command *mc)
2451 MLX_IO_ASSERT_LOCKED(mc->mc_sc);
2452 TAILQ_INSERT_HEAD(&mc->mc_sc->mlx_freecmds, mc, mc_link);
2455 /********************************************************************************
2456 * Permanently discard a command buffer.
2459 mlx_freecmd(struct mlx_command *mc)
2461 struct mlx_softc *sc = mc->mc_sc;
2464 bus_dmamap_destroy(sc->mlx_buffer_dmat, mc->mc_dmamap);
2469 /********************************************************************************
2470 ********************************************************************************
2471 Type 3 interface accessor methods
2472 ********************************************************************************
2473 ********************************************************************************/
2475 /********************************************************************************
2476 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2477 * (the controller is not ready to take a command).
2480 mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2485 MLX_IO_ASSERT_LOCKED(sc);
2487 /* ready for our command? */
2488 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_FULL)) {
2489 /* copy mailbox data to window */
2490 for (i = 0; i < 13; i++)
2491 MLX_V3_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2494 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_FULL);
2500 /********************************************************************************
2501 * See if a command has been completed, if so acknowledge its completion
2502 * and recover the slot number and status code.
2505 mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2509 MLX_IO_ASSERT_LOCKED(sc);
2511 /* status available? */
2512 if (MLX_V3_GET_ODBR(sc) & MLX_V3_ODB_SAVAIL) {
2513 *slot = MLX_V3_GET_STATUS_IDENT(sc); /* get command identifier */
2514 *status = MLX_V3_GET_STATUS(sc); /* get status */
2516 /* acknowledge completion */
2517 MLX_V3_PUT_ODBR(sc, MLX_V3_ODB_SAVAIL);
2518 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2524 /********************************************************************************
2525 * Enable/disable interrupts as requested. (No acknowledge required)
2528 mlx_v3_intaction(struct mlx_softc *sc, int action)
2531 MLX_IO_ASSERT_LOCKED(sc);
2534 case MLX_INTACTION_DISABLE:
2535 MLX_V3_PUT_IER(sc, 0);
2536 sc->mlx_state &= ~MLX_STATE_INTEN;
2538 case MLX_INTACTION_ENABLE:
2539 MLX_V3_PUT_IER(sc, 1);
2540 sc->mlx_state |= MLX_STATE_INTEN;
2545 /********************************************************************************
2546 * Poll for firmware error codes during controller initialisation.
2547 * Returns 0 if initialisation is complete, 1 if still in progress but no
2548 * error has been fetched, 2 if an error has been retrieved.
2551 mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
2558 /* first time around, clear any hardware completion status */
2560 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2564 /* init in progress? */
2565 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_INIT_BUSY))
2568 /* test error value */
2569 fwerror = MLX_V3_GET_FWERROR(sc);
2570 if (!(fwerror & MLX_V3_FWERROR_PEND))
2573 /* mask status pending bit, fetch status */
2574 *error = fwerror & ~MLX_V3_FWERROR_PEND;
2575 *param1 = MLX_V3_GET_FWERROR_PARAM1(sc);
2576 *param2 = MLX_V3_GET_FWERROR_PARAM2(sc);
2579 MLX_V3_PUT_FWERROR(sc, 0);
2584 /********************************************************************************
2585 ********************************************************************************
2586 Type 4 interface accessor methods
2587 ********************************************************************************
2588 ********************************************************************************/
2590 /********************************************************************************
2591 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2592 * (the controller is not ready to take a command).
2595 mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2600 MLX_IO_ASSERT_LOCKED(sc);
2602 /* ready for our command? */
2603 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_FULL)) {
2604 /* copy mailbox data to window */
2605 for (i = 0; i < 13; i++)
2606 MLX_V4_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2608 /* memory-mapped controller, so issue a write barrier to ensure the mailbox is filled */
2609 bus_barrier(sc->mlx_mem, MLX_V4_MAILBOX, MLX_V4_MAILBOX_LENGTH,
2610 BUS_SPACE_BARRIER_WRITE);
2613 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_HWMBOX_CMD);
2619 /********************************************************************************
2620 * See if a command has been completed, if so acknowledge its completion
2621 * and recover the slot number and status code.
2624 mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2628 MLX_IO_ASSERT_LOCKED(sc);
2630 /* status available? */
2631 if (MLX_V4_GET_ODBR(sc) & MLX_V4_ODB_HWSAVAIL) {
2632 *slot = MLX_V4_GET_STATUS_IDENT(sc); /* get command identifier */
2633 *status = MLX_V4_GET_STATUS(sc); /* get status */
2635 /* acknowledge completion */
2636 MLX_V4_PUT_ODBR(sc, MLX_V4_ODB_HWMBOX_ACK);
2637 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2643 /********************************************************************************
2644 * Enable/disable interrupts as requested.
2647 mlx_v4_intaction(struct mlx_softc *sc, int action)
2650 MLX_IO_ASSERT_LOCKED(sc);
2653 case MLX_INTACTION_DISABLE:
2654 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK | MLX_V4_IER_DISINT);
2655 sc->mlx_state &= ~MLX_STATE_INTEN;
2657 case MLX_INTACTION_ENABLE:
2658 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK & ~MLX_V4_IER_DISINT);
2659 sc->mlx_state |= MLX_STATE_INTEN;
2664 /********************************************************************************
2665 * Poll for firmware error codes during controller initialisation.
2666 * Returns 0 if initialisation is complete, 1 if still in progress but no
2667 * error has been fetched, 2 if an error has been retrieved.
2670 mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
2677 /* first time around, clear any hardware completion status */
2679 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2683 /* init in progress? */
2684 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_INIT_BUSY))
2687 /* test error value */
2688 fwerror = MLX_V4_GET_FWERROR(sc);
2689 if (!(fwerror & MLX_V4_FWERROR_PEND))
2692 /* mask status pending bit, fetch status */
2693 *error = fwerror & ~MLX_V4_FWERROR_PEND;
2694 *param1 = MLX_V4_GET_FWERROR_PARAM1(sc);
2695 *param2 = MLX_V4_GET_FWERROR_PARAM2(sc);
2698 MLX_V4_PUT_FWERROR(sc, 0);
2703 /********************************************************************************
2704 ********************************************************************************
2705 Type 5 interface accessor methods
2706 ********************************************************************************
2707 ********************************************************************************/
2709 /********************************************************************************
2710 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2711 * (the controller is not ready to take a command).
2714 mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2719 MLX_IO_ASSERT_LOCKED(sc);
2721 /* ready for our command? */
2722 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_EMPTY) {
2723 /* copy mailbox data to window */
2724 for (i = 0; i < 13; i++)
2725 MLX_V5_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2728 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_HWMBOX_CMD);
2734 /********************************************************************************
2735 * See if a command has been completed, if so acknowledge its completion
2736 * and recover the slot number and status code.
2739 mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2743 MLX_IO_ASSERT_LOCKED(sc);
2745 /* status available? */
2746 if (MLX_V5_GET_ODBR(sc) & MLX_V5_ODB_HWSAVAIL) {
2747 *slot = MLX_V5_GET_STATUS_IDENT(sc); /* get command identifier */
2748 *status = MLX_V5_GET_STATUS(sc); /* get status */
2750 /* acknowledge completion */
2751 MLX_V5_PUT_ODBR(sc, MLX_V5_ODB_HWMBOX_ACK);
2752 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2758 /********************************************************************************
2759 * Enable/disable interrupts as requested.
2762 mlx_v5_intaction(struct mlx_softc *sc, int action)
2765 MLX_IO_ASSERT_LOCKED(sc);
2768 case MLX_INTACTION_DISABLE:
2769 MLX_V5_PUT_IER(sc, 0xff & MLX_V5_IER_DISINT);
2770 sc->mlx_state &= ~MLX_STATE_INTEN;
2772 case MLX_INTACTION_ENABLE:
2773 MLX_V5_PUT_IER(sc, 0xff & ~MLX_V5_IER_DISINT);
2774 sc->mlx_state |= MLX_STATE_INTEN;
2779 /********************************************************************************
2780 * Poll for firmware error codes during controller initialisation.
2781 * Returns 0 if initialisation is complete, 1 if still in progress but no
2782 * error has been fetched, 2 if an error has been retrieved.
2785 mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
2792 /* first time around, clear any hardware completion status */
2794 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2798 /* init in progress? */
2799 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_INIT_DONE)
2802 /* test for error value */
2803 fwerror = MLX_V5_GET_FWERROR(sc);
2804 if (!(fwerror & MLX_V5_FWERROR_PEND))
2807 /* mask status pending bit, fetch status */
2808 *error = fwerror & ~MLX_V5_FWERROR_PEND;
2809 *param1 = MLX_V5_GET_FWERROR_PARAM1(sc);
2810 *param2 = MLX_V5_GET_FWERROR_PARAM2(sc);
2813 MLX_V5_PUT_FWERROR(sc, 0xff);
2818 /********************************************************************************
2819 ********************************************************************************
2821 ********************************************************************************
2822 ********************************************************************************/
2824 /********************************************************************************
2825 * Return a status message describing (mc)
2827 static char *mlx_status_messages[] = {
2828 "normal completion", /* 00 */
2829 "irrecoverable data error", /* 01 */
2830 "drive does not exist, or is offline", /* 02 */
2831 "attempt to write beyond end of drive", /* 03 */
2832 "bad data encountered", /* 04 */
2833 "invalid log entry request", /* 05 */
2834 "attempt to rebuild online drive", /* 06 */
2835 "new disk failed during rebuild", /* 07 */
2836 "invalid channel/target", /* 08 */
2837 "rebuild/check already in progress", /* 09 */
2838 "one or more disks are dead", /* 10 */
2839 "invalid or non-redundant drive", /* 11 */
2840 "channel is busy", /* 12 */
2841 "channel is not stopped", /* 13 */
2842 "rebuild successfully terminated", /* 14 */
2843 "unsupported command", /* 15 */
2844 "check condition received", /* 16 */
2845 "device is busy", /* 17 */
2846 "selection or command timeout", /* 18 */
2847 "command terminated abnormally", /* 19 */
2856 } mlx_messages[] = {
2857 {MLX_CMD_READSG, 0x0001, 1},
2858 {MLX_CMD_READSG, 0x0002, 1},
2859 {MLX_CMD_READSG, 0x0105, 3},
2860 {MLX_CMD_READSG, 0x010c, 4},
2861 {MLX_CMD_WRITESG, 0x0001, 1},
2862 {MLX_CMD_WRITESG, 0x0002, 1},
2863 {MLX_CMD_WRITESG, 0x0105, 3},
2864 {MLX_CMD_READSG_OLD, 0x0001, 1},
2865 {MLX_CMD_READSG_OLD, 0x0002, 1},
2866 {MLX_CMD_READSG_OLD, 0x0105, 3},
2867 {MLX_CMD_WRITESG_OLD, 0x0001, 1},
2868 {MLX_CMD_WRITESG_OLD, 0x0002, 1},
2869 {MLX_CMD_WRITESG_OLD, 0x0105, 3},
2870 {MLX_CMD_LOGOP, 0x0105, 5},
2871 {MLX_CMD_REBUILDASYNC, 0x0002, 6},
2872 {MLX_CMD_REBUILDASYNC, 0x0004, 7},
2873 {MLX_CMD_REBUILDASYNC, 0x0105, 8},
2874 {MLX_CMD_REBUILDASYNC, 0x0106, 9},
2875 {MLX_CMD_REBUILDASYNC, 0x0107, 14},
2876 {MLX_CMD_CHECKASYNC, 0x0002, 10},
2877 {MLX_CMD_CHECKASYNC, 0x0105, 11},
2878 {MLX_CMD_CHECKASYNC, 0x0106, 9},
2879 {MLX_CMD_STOPCHANNEL, 0x0106, 12},
2880 {MLX_CMD_STOPCHANNEL, 0x0105, 8},
2881 {MLX_CMD_STARTCHANNEL, 0x0005, 13},
2882 {MLX_CMD_STARTCHANNEL, 0x0105, 8},
2883 {MLX_CMD_DIRECT_CDB, 0x0002, 16},
2884 {MLX_CMD_DIRECT_CDB, 0x0008, 17},
2885 {MLX_CMD_DIRECT_CDB, 0x000e, 18},
2886 {MLX_CMD_DIRECT_CDB, 0x000f, 19},
2887 {MLX_CMD_DIRECT_CDB, 0x0105, 8},
2894 mlx_diagnose_command(struct mlx_command *mc)
2896 static char unkmsg[80];
2899 /* look up message in table */
2900 for (i = 0; mlx_messages[i].command != -1; i++)
2901 if (((mc->mc_mailbox[0] == mlx_messages[i].command) || (mlx_messages[i].command == 0)) &&
2902 (mc->mc_status == mlx_messages[i].status))
2903 return(mlx_status_messages[mlx_messages[i].msg]);
2905 sprintf(unkmsg, "unknown response 0x%x for command 0x%x", (int)mc->mc_status, (int)mc->mc_mailbox[0]);
2909 /*******************************************************************************
2910 * Print a string describing the controller (sc)
2916 } mlx_controller_names[] = {
2931 mlx_describe_controller(struct mlx_softc *sc)
2933 static char buf[80];
2937 for (i = 0, model = NULL; mlx_controller_names[i].name != NULL; i++) {
2938 if ((sc->mlx_enq2->me_hardware_id & 0xff) == mlx_controller_names[i].hwid) {
2939 model = mlx_controller_names[i].name;
2943 if (model == NULL) {
2944 sprintf(buf, " model 0x%x", sc->mlx_enq2->me_hardware_id & 0xff);
2947 device_printf(sc->mlx_dev, "DAC%s, %d channel%s, firmware %d.%02d-%c-%02d, %dMB RAM\n",
2949 sc->mlx_enq2->me_actual_channels,
2950 sc->mlx_enq2->me_actual_channels > 1 ? "s" : "",
2951 sc->mlx_enq2->me_firmware_id & 0xff,
2952 (sc->mlx_enq2->me_firmware_id >> 8) & 0xff,
2953 (sc->mlx_enq2->me_firmware_id >> 24) & 0xff,
2954 (sc->mlx_enq2->me_firmware_id >> 16) & 0xff,
2955 sc->mlx_enq2->me_mem_size / (1024 * 1024));
2958 device_printf(sc->mlx_dev, " Hardware ID 0x%08x\n", sc->mlx_enq2->me_hardware_id);
2959 device_printf(sc->mlx_dev, " Firmware ID 0x%08x\n", sc->mlx_enq2->me_firmware_id);
2960 device_printf(sc->mlx_dev, " Configured/Actual channels %d/%d\n", sc->mlx_enq2->me_configured_channels,
2961 sc->mlx_enq2->me_actual_channels);
2962 device_printf(sc->mlx_dev, " Max Targets %d\n", sc->mlx_enq2->me_max_targets);
2963 device_printf(sc->mlx_dev, " Max Tags %d\n", sc->mlx_enq2->me_max_tags);
2964 device_printf(sc->mlx_dev, " Max System Drives %d\n", sc->mlx_enq2->me_max_sys_drives);
2965 device_printf(sc->mlx_dev, " Max Arms %d\n", sc->mlx_enq2->me_max_arms);
2966 device_printf(sc->mlx_dev, " Max Spans %d\n", sc->mlx_enq2->me_max_spans);
2967 device_printf(sc->mlx_dev, " DRAM/cache/flash/NVRAM size %d/%d/%d/%d\n", sc->mlx_enq2->me_mem_size,
2968 sc->mlx_enq2->me_cache_size, sc->mlx_enq2->me_flash_size, sc->mlx_enq2->me_nvram_size);
2969 device_printf(sc->mlx_dev, " DRAM type %d\n", sc->mlx_enq2->me_mem_type);
2970 device_printf(sc->mlx_dev, " Clock Speed %dns\n", sc->mlx_enq2->me_clock_speed);
2971 device_printf(sc->mlx_dev, " Hardware Speed %dns\n", sc->mlx_enq2->me_hardware_speed);
2972 device_printf(sc->mlx_dev, " Max Commands %d\n", sc->mlx_enq2->me_max_commands);
2973 device_printf(sc->mlx_dev, " Max SG Entries %d\n", sc->mlx_enq2->me_max_sg);
2974 device_printf(sc->mlx_dev, " Max DP %d\n", sc->mlx_enq2->me_max_dp);
2975 device_printf(sc->mlx_dev, " Max IOD %d\n", sc->mlx_enq2->me_max_iod);
2976 device_printf(sc->mlx_dev, " Max Comb %d\n", sc->mlx_enq2->me_max_comb);
2977 device_printf(sc->mlx_dev, " Latency %ds\n", sc->mlx_enq2->me_latency);
2978 device_printf(sc->mlx_dev, " SCSI Timeout %ds\n", sc->mlx_enq2->me_scsi_timeout);
2979 device_printf(sc->mlx_dev, " Min Free Lines %d\n", sc->mlx_enq2->me_min_freelines);
2980 device_printf(sc->mlx_dev, " Rate Constant %d\n", sc->mlx_enq2->me_rate_const);
2981 device_printf(sc->mlx_dev, " MAXBLK %d\n", sc->mlx_enq2->me_maxblk);
2982 device_printf(sc->mlx_dev, " Blocking Factor %d sectors\n", sc->mlx_enq2->me_blocking_factor);
2983 device_printf(sc->mlx_dev, " Cache Line Size %d blocks\n", sc->mlx_enq2->me_cacheline);
2984 device_printf(sc->mlx_dev, " SCSI Capability %s%dMHz, %d bit\n",
2985 sc->mlx_enq2->me_scsi_cap & (1<<4) ? "differential " : "",
2986 (1 << ((sc->mlx_enq2->me_scsi_cap >> 2) & 3)) * 10,
2987 8 << (sc->mlx_enq2->me_scsi_cap & 0x3));
2988 device_printf(sc->mlx_dev, " Firmware Build Number %d\n", sc->mlx_enq2->me_firmware_build);
2989 device_printf(sc->mlx_dev, " Fault Management Type %d\n", sc->mlx_enq2->me_fault_mgmt_type);
2990 device_printf(sc->mlx_dev, " Features %b\n", sc->mlx_enq2->me_firmware_features,
2991 "\20\4Background Init\3Read Ahead\2MORE\1Cluster\n");
2996 /*******************************************************************************
2997 * Emit a string describing the firmware handshake status code, and return a flag
2998 * indicating whether the code represents a fatal error.
3000 * Error code interpretations are from the Linux driver, and don't directly match
3001 * the messages printed by Mylex's BIOS. This may change if documentation on the
3002 * codes is forthcoming.
3005 mlx_fw_message(struct mlx_softc *sc, int error, int param1, int param2)
3009 device_printf(sc->mlx_dev, "physical drive %d:%d not responding\n", param2, param1);
3012 /* we could be neater about this and give some indication when we receive more of them */
3013 if (!(sc->mlx_flags & MLX_SPINUP_REPORTED)) {
3014 device_printf(sc->mlx_dev, "spinning up drives...\n");
3015 sc->mlx_flags |= MLX_SPINUP_REPORTED;
3019 device_printf(sc->mlx_dev, "configuration checksum error\n");
3022 device_printf(sc->mlx_dev, "mirror race recovery failed\n");
3025 device_printf(sc->mlx_dev, "mirror race recovery in progress\n");
3028 device_printf(sc->mlx_dev, "physical drive %d:%d COD mismatch\n", param2, param1);
3031 device_printf(sc->mlx_dev, "logical drive installation aborted\n");
3034 device_printf(sc->mlx_dev, "mirror race on a critical system drive\n");
3037 device_printf(sc->mlx_dev, "new controller configuration found\n");
3040 device_printf(sc->mlx_dev, "FATAL MEMORY PARITY ERROR\n");
3043 device_printf(sc->mlx_dev, "unknown firmware initialisation error %02x:%02x:%02x\n", error, param1, param2);
3049 /********************************************************************************
3050 ********************************************************************************
3052 ********************************************************************************
3053 ********************************************************************************/
3055 /********************************************************************************
3056 * Find the disk whose unit number is (unit) on this controller
3058 static struct mlx_sysdrive *
3059 mlx_findunit(struct mlx_softc *sc, int unit)
3063 /* search system drives */
3064 MLX_CONFIG_ASSERT_LOCKED(sc);
3065 for (i = 0; i < MLX_MAXDRIVES; i++) {
3066 /* is this one attached? */
3067 if (sc->mlx_sysdrive[i].ms_disk != 0) {
3068 /* is this the one? */
3069 if (unit == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
3070 return(&sc->mlx_sysdrive[i]);