xref: /freebsd/sys/dev/mlx/mlx.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1999 Michael Smith
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Driver for the Mylex DAC960 family of RAID controllers.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bio.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/kernel.h>
40 #include <sys/sx.h>
41 
42 #include <sys/bus.h>
43 #include <sys/conf.h>
44 #include <sys/stat.h>
45 
46 #include <machine/resource.h>
47 #include <machine/bus.h>
48 #include <machine/clock.h>
49 #include <sys/rman.h>
50 
51 #include <geom/geom_disk.h>
52 
53 #include <dev/mlx/mlxio.h>
54 #include <dev/mlx/mlxvar.h>
55 #include <dev/mlx/mlxreg.h>
56 
57 static struct cdevsw mlx_cdevsw = {
58 	.d_version =	D_VERSION,
59 	.d_open =	mlx_open,
60 	.d_close =	mlx_close,
61 	.d_ioctl =	mlx_ioctl,
62 	.d_name =	"mlx",
63 };
64 
65 /*
66  * Per-interface accessor methods
67  */
68 static int			mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
69 static int			mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
70 static void			mlx_v3_intaction(struct mlx_softc *sc, int action);
71 static int			mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first);
72 
73 static int			mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
74 static int			mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
75 static void			mlx_v4_intaction(struct mlx_softc *sc, int action);
76 static int			mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first);
77 
78 static int			mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
79 static int			mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
80 static void			mlx_v5_intaction(struct mlx_softc *sc, int action);
81 static int			mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first);
82 
83 /*
84  * Status monitoring
85  */
86 static void			mlx_periodic(void *data);
87 static void			mlx_periodic_enquiry(struct mlx_command *mc);
88 static void			mlx_periodic_eventlog_poll(struct mlx_softc *sc);
89 static void			mlx_periodic_eventlog_respond(struct mlx_command *mc);
90 static void			mlx_periodic_rebuild(struct mlx_command *mc);
91 
92 /*
93  * Channel Pause
94  */
95 static void			mlx_pause_action(struct mlx_softc *sc);
96 static void			mlx_pause_done(struct mlx_command *mc);
97 
98 /*
99  * Command submission.
100  */
101 static void			*mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize,
102 					     void (*complete)(struct mlx_command *mc));
103 static int			mlx_flush(struct mlx_softc *sc);
104 static int			mlx_check(struct mlx_softc *sc, int drive);
105 static int			mlx_rebuild(struct mlx_softc *sc, int channel, int target);
106 static int			mlx_wait_command(struct mlx_command *mc);
107 static int			mlx_poll_command(struct mlx_command *mc);
108 void				mlx_startio_cb(void *arg,
109 					       bus_dma_segment_t *segs,
110 					       int nsegments, int error);
111 static void			mlx_startio(struct mlx_softc *sc);
112 static void			mlx_completeio(struct mlx_command *mc);
113 static int			mlx_user_command(struct mlx_softc *sc,
114 						 struct mlx_usercommand *mu);
115 void				mlx_user_cb(void *arg, bus_dma_segment_t *segs,
116 					    int nsegments, int error);
117 
118 /*
119  * Command buffer allocation.
120  */
121 static struct mlx_command	*mlx_alloccmd(struct mlx_softc *sc);
122 static void			mlx_releasecmd(struct mlx_command *mc);
123 static void			mlx_freecmd(struct mlx_command *mc);
124 
125 /*
126  * Command management.
127  */
128 static int			mlx_getslot(struct mlx_command *mc);
129 static void			mlx_setup_dmamap(struct mlx_command *mc,
130 						 bus_dma_segment_t *segs,
131 						 int nsegments, int error);
132 static void			mlx_unmapcmd(struct mlx_command *mc);
133 static int			mlx_shutdown_locked(struct mlx_softc *sc);
134 static int			mlx_start(struct mlx_command *mc);
135 static int			mlx_done(struct mlx_softc *sc, int startio);
136 static void			mlx_complete(struct mlx_softc *sc);
137 
138 /*
139  * Debugging.
140  */
141 static char			*mlx_diagnose_command(struct mlx_command *mc);
142 static void			mlx_describe_controller(struct mlx_softc *sc);
143 static int			mlx_fw_message(struct mlx_softc *sc, int status, int param1, int param2);
144 
145 /*
146  * Utility functions.
147  */
148 static struct mlx_sysdrive	*mlx_findunit(struct mlx_softc *sc, int unit);
149 
150 /********************************************************************************
151  ********************************************************************************
152                                                                 Public Interfaces
153  ********************************************************************************
154  ********************************************************************************/
155 
156 /********************************************************************************
157  * Free all of the resources associated with (sc)
158  *
159  * Should not be called if the controller is active.
160  */
161 void
162 mlx_free(struct mlx_softc *sc)
163 {
164     struct mlx_command	*mc;
165 
166     debug_called(1);
167 
168     /* destroy control device */
169     if (sc->mlx_dev_t != NULL)
170 	destroy_dev(sc->mlx_dev_t);
171 
172     if (sc->mlx_intr)
173 	bus_teardown_intr(sc->mlx_dev, sc->mlx_irq, sc->mlx_intr);
174 
175     /* cancel status timeout */
176     MLX_IO_LOCK(sc);
177     callout_stop(&sc->mlx_timeout);
178 
179     /* throw away any command buffers */
180     while ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL) {
181 	TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
182 	mlx_freecmd(mc);
183     }
184     MLX_IO_UNLOCK(sc);
185     callout_drain(&sc->mlx_timeout);
186 
187     /* destroy data-transfer DMA tag */
188     if (sc->mlx_buffer_dmat)
189 	bus_dma_tag_destroy(sc->mlx_buffer_dmat);
190 
191     /* free and destroy DMA memory and tag for s/g lists */
192     if (sc->mlx_sgbusaddr)
193 	bus_dmamap_unload(sc->mlx_sg_dmat, sc->mlx_sg_dmamap);
194     if (sc->mlx_sgtable)
195 	bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
196     if (sc->mlx_sg_dmat)
197 	bus_dma_tag_destroy(sc->mlx_sg_dmat);
198 
199     /* disconnect the interrupt handler */
200     if (sc->mlx_irq != NULL)
201 	bus_release_resource(sc->mlx_dev, SYS_RES_IRQ, 0, sc->mlx_irq);
202 
203     /* destroy the parent DMA tag */
204     if (sc->mlx_parent_dmat)
205 	bus_dma_tag_destroy(sc->mlx_parent_dmat);
206 
207     /* release the register window mapping */
208     if (sc->mlx_mem != NULL)
209 	bus_release_resource(sc->mlx_dev, sc->mlx_mem_type, sc->mlx_mem_rid, sc->mlx_mem);
210 
211     /* free controller enquiry data */
212     if (sc->mlx_enq2 != NULL)
213 	free(sc->mlx_enq2, M_DEVBUF);
214 
215     sx_destroy(&sc->mlx_config_lock);
216     mtx_destroy(&sc->mlx_io_lock);
217 }
218 
219 /********************************************************************************
220  * Map the scatter/gather table into bus space
221  */
222 static void
223 mlx_dma_map_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
224 {
225     struct mlx_softc	*sc = (struct mlx_softc *)arg;
226 
227     debug_called(1);
228 
229     /* save base of s/g table's address in bus space */
230     sc->mlx_sgbusaddr = segs->ds_addr;
231 }
232 
233 static int
234 mlx_sglist_map(struct mlx_softc *sc)
235 {
236     size_t	segsize;
237     int		error, ncmd;
238 
239     debug_called(1);
240 
241     /* destroy any existing mappings */
242     if (sc->mlx_sgbusaddr)
243 	bus_dmamap_unload(sc->mlx_sg_dmat, sc->mlx_sg_dmamap);
244     if (sc->mlx_sgtable)
245 	bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
246     if (sc->mlx_sg_dmat)
247 	bus_dma_tag_destroy(sc->mlx_sg_dmat);
248     sc->mlx_sgbusaddr = 0;
249     sc->mlx_sgtable = NULL;
250     sc->mlx_sg_dmat = NULL;
251 
252     /*
253      * Create a single tag describing a region large enough to hold all of
254      * the s/g lists we will need.  If we're called early on, we don't know how
255      * many commands we're going to be asked to support, so only allocate enough
256      * for a couple.
257      */
258     if (sc->mlx_enq2 == NULL) {
259 	ncmd = 2;
260     } else {
261 	ncmd = sc->mlx_enq2->me_max_commands;
262     }
263     segsize = sizeof(struct mlx_sgentry) * MLX_NSEG * ncmd;
264     error = bus_dma_tag_create(sc->mlx_parent_dmat, 	/* parent */
265 			       1, 0, 			/* alignment,boundary */
266 			       BUS_SPACE_MAXADDR,	/* lowaddr */
267 			       BUS_SPACE_MAXADDR, 	/* highaddr */
268 			       NULL, NULL, 		/* filter, filterarg */
269 			       segsize, 1,		/* maxsize, nsegments */
270 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
271 			       0,			/* flags */
272 			       NULL, NULL,		/* lockfunc, lockarg */
273 			       &sc->mlx_sg_dmat);
274     if (error != 0) {
275 	device_printf(sc->mlx_dev, "can't allocate scatter/gather DMA tag\n");
276 	return(ENOMEM);
277     }
278 
279     /*
280      * Allocate enough s/g maps for all commands and permanently map them into
281      * controller-visible space.
282      *
283      * XXX this assumes we can get enough space for all the s/g maps in one
284      * contiguous slab.  We may need to switch to a more complex arrangement
285      * where we allocate in smaller chunks and keep a lookup table from slot
286      * to bus address.
287      */
288     error = bus_dmamem_alloc(sc->mlx_sg_dmat, (void **)&sc->mlx_sgtable,
289 			     BUS_DMA_NOWAIT, &sc->mlx_sg_dmamap);
290     if (error) {
291 	device_printf(sc->mlx_dev, "can't allocate s/g table\n");
292 	return(ENOMEM);
293     }
294     (void)bus_dmamap_load(sc->mlx_sg_dmat, sc->mlx_sg_dmamap, sc->mlx_sgtable,
295 			  segsize, mlx_dma_map_sg, sc, 0);
296     return(0);
297 }
298 
299 /********************************************************************************
300  * Initialise the controller and softc
301  */
302 int
303 mlx_attach(struct mlx_softc *sc)
304 {
305     struct mlx_enquiry_old	*meo;
306     int				rid, error, fwminor, hscode, hserror, hsparam1, hsparam2, hsmsg;
307 
308     debug_called(1);
309 
310     /*
311      * Initialise per-controller queues.
312      */
313     TAILQ_INIT(&sc->mlx_work);
314     TAILQ_INIT(&sc->mlx_freecmds);
315     bioq_init(&sc->mlx_bioq);
316 
317     /*
318      * Select accessor methods based on controller interface type.
319      */
320     switch(sc->mlx_iftype) {
321     case MLX_IFTYPE_2:
322     case MLX_IFTYPE_3:
323 	sc->mlx_tryqueue	= mlx_v3_tryqueue;
324 	sc->mlx_findcomplete	= mlx_v3_findcomplete;
325 	sc->mlx_intaction	= mlx_v3_intaction;
326 	sc->mlx_fw_handshake	= mlx_v3_fw_handshake;
327 	break;
328     case MLX_IFTYPE_4:
329 	sc->mlx_tryqueue	= mlx_v4_tryqueue;
330 	sc->mlx_findcomplete	= mlx_v4_findcomplete;
331 	sc->mlx_intaction	= mlx_v4_intaction;
332 	sc->mlx_fw_handshake	= mlx_v4_fw_handshake;
333 	break;
334     case MLX_IFTYPE_5:
335 	sc->mlx_tryqueue	= mlx_v5_tryqueue;
336 	sc->mlx_findcomplete	= mlx_v5_findcomplete;
337 	sc->mlx_intaction	= mlx_v5_intaction;
338 	sc->mlx_fw_handshake	= mlx_v5_fw_handshake;
339 	break;
340     default:
341 	return(ENXIO);		/* should never happen */
342     }
343 
344     /* disable interrupts before we start talking to the controller */
345     MLX_IO_LOCK(sc);
346     sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
347     MLX_IO_UNLOCK(sc);
348 
349     /*
350      * Wait for the controller to come ready, handshake with the firmware if required.
351      * This is typically only necessary on platforms where the controller BIOS does not
352      * run.
353      */
354     hsmsg = 0;
355     DELAY(1000);
356     while ((hscode = sc->mlx_fw_handshake(sc, &hserror, &hsparam1, &hsparam2,
357 	hsmsg == 0)) != 0) {
358 	/* report first time around... */
359 	if (hsmsg == 0) {
360 	    device_printf(sc->mlx_dev, "controller initialisation in progress...\n");
361 	    hsmsg = 1;
362 	}
363 	/* did we get a real message? */
364 	if (hscode == 2) {
365 	    hscode = mlx_fw_message(sc, hserror, hsparam1, hsparam2);
366 	    /* fatal initialisation error? */
367 	    if (hscode != 0) {
368 		return(ENXIO);
369 	    }
370 	}
371     }
372     if (hsmsg == 1)
373 	device_printf(sc->mlx_dev, "initialisation complete.\n");
374 
375     /*
376      * Allocate and connect our interrupt.
377      */
378     rid = 0;
379     sc->mlx_irq = bus_alloc_resource_any(sc->mlx_dev, SYS_RES_IRQ, &rid,
380         RF_SHAREABLE | RF_ACTIVE);
381     if (sc->mlx_irq == NULL) {
382 	device_printf(sc->mlx_dev, "can't allocate interrupt\n");
383 	return(ENXIO);
384     }
385     error = bus_setup_intr(sc->mlx_dev, sc->mlx_irq, INTR_TYPE_BIO |
386 	INTR_ENTROPY | INTR_MPSAFE, NULL, mlx_intr, sc, &sc->mlx_intr);
387     if (error) {
388 	device_printf(sc->mlx_dev, "can't set up interrupt\n");
389 	return(ENXIO);
390     }
391 
392     /*
393      * Create DMA tag for mapping buffers into controller-addressable space.
394      */
395     error = bus_dma_tag_create(sc->mlx_parent_dmat, 	/* parent */
396 			       1, 0, 			/* align, boundary */
397 			       BUS_SPACE_MAXADDR,	/* lowaddr */
398 			       BUS_SPACE_MAXADDR, 	/* highaddr */
399 			       NULL, NULL, 		/* filter, filterarg */
400 			       MLX_MAXPHYS,		/* maxsize */
401 			       MLX_NSEG,		/* nsegments */
402 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
403 			       0,			/* flags */
404 			       busdma_lock_mutex,	/* lockfunc */
405 			       &sc->mlx_io_lock,	/* lockarg */
406 			       &sc->mlx_buffer_dmat);
407     if (error != 0) {
408 	device_printf(sc->mlx_dev, "can't allocate buffer DMA tag\n");
409 	return(ENOMEM);
410     }
411 
412     /*
413      * Create some initial scatter/gather mappings so we can run the probe
414      * commands.
415      */
416     error = mlx_sglist_map(sc);
417     if (error != 0) {
418 	device_printf(sc->mlx_dev, "can't make initial s/g list mapping\n");
419 	return(error);
420     }
421 
422     /*
423      * We don't (yet) know where the event log is up to.
424      */
425     sc->mlx_currevent = -1;
426 
427     /*
428      * Obtain controller feature information
429      */
430     MLX_IO_LOCK(sc);
431     if ((sc->mlx_enq2 = mlx_enquire(sc, MLX_CMD_ENQUIRY2, sizeof(struct mlx_enquiry2), NULL)) == NULL) {
432 	MLX_IO_UNLOCK(sc);
433 	device_printf(sc->mlx_dev, "ENQUIRY2 failed\n");
434 	return(ENXIO);
435     }
436 
437     /*
438      * Do quirk/feature related things.
439      */
440     fwminor = (sc->mlx_enq2->me_firmware_id >> 8) & 0xff;
441     switch(sc->mlx_iftype) {
442     case MLX_IFTYPE_2:
443 	/* These controllers don't report the firmware version in the ENQUIRY2 response */
444 	if ((meo = mlx_enquire(sc, MLX_CMD_ENQUIRY_OLD, sizeof(struct mlx_enquiry_old), NULL)) == NULL) {
445 	    MLX_IO_UNLOCK(sc);
446 	    device_printf(sc->mlx_dev, "ENQUIRY_OLD failed\n");
447 	    return(ENXIO);
448 	}
449 	sc->mlx_enq2->me_firmware_id = ('0' << 24) | (0 << 16) | (meo->me_fwminor << 8) | meo->me_fwmajor;
450 
451 	/* XXX require 2.42 or better (PCI) */
452 	if (meo->me_fwminor < 42) {
453 	    device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
454 	    device_printf(sc->mlx_dev, " *** WARNING *** Use revision 2.42 or later\n");
455 	}
456 	free(meo, M_DEVBUF);
457 	break;
458     case MLX_IFTYPE_3:
459 	/* XXX certify 3.52? */
460 	if (fwminor < 51) {
461 	    device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
462 	    device_printf(sc->mlx_dev, " *** WARNING *** Use revision 3.51 or later\n");
463 	}
464 	break;
465     case MLX_IFTYPE_4:
466 	/* XXX certify firmware versions? */
467 	if (fwminor < 6) {
468 	    device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
469 	    device_printf(sc->mlx_dev, " *** WARNING *** Use revision 4.06 or later\n");
470 	}
471 	break;
472     case MLX_IFTYPE_5:
473 	if (fwminor < 7) {
474 	    device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
475 	    device_printf(sc->mlx_dev, " *** WARNING *** Use revision 5.07 or later\n");
476 	}
477 	break;
478     default:
479 	MLX_IO_UNLOCK(sc);
480 	return(ENXIO);		/* should never happen */
481     }
482     MLX_IO_UNLOCK(sc);
483 
484     /*
485      * Create the final scatter/gather mappings now that we have characterised the controller.
486      */
487     error = mlx_sglist_map(sc);
488     if (error != 0) {
489 	device_printf(sc->mlx_dev, "can't make final s/g list mapping\n");
490 	return(error);
491     }
492 
493     /*
494      * No user-requested background operation is in progress.
495      */
496     sc->mlx_background = 0;
497     sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
498 
499     /*
500      * Create the control device.
501      */
502     sc->mlx_dev_t = make_dev(&mlx_cdevsw, 0, UID_ROOT, GID_OPERATOR,
503 			     S_IRUSR | S_IWUSR, "mlx%d", device_get_unit(sc->mlx_dev));
504     sc->mlx_dev_t->si_drv1 = sc;
505 
506     /*
507      * Start the timeout routine.
508      */
509     callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
510 
511     /* print a little information about the controller */
512     mlx_describe_controller(sc);
513 
514     return(0);
515 }
516 
517 /********************************************************************************
518  * Locate disk resources and attach children to them.
519  */
520 void
521 mlx_startup(struct mlx_softc *sc)
522 {
523     struct mlx_enq_sys_drive	*mes;
524     struct mlx_sysdrive		*dr;
525     int				i, error;
526 
527     debug_called(1);
528 
529     /*
530      * Scan all the system drives and attach children for those that
531      * don't currently have them.
532      */
533     MLX_IO_LOCK(sc);
534     mes = mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(*mes) * MLX_MAXDRIVES, NULL);
535     MLX_IO_UNLOCK(sc);
536     if (mes == NULL) {
537 	device_printf(sc->mlx_dev, "error fetching drive status\n");
538 	return;
539     }
540 
541     /* iterate over drives returned */
542     MLX_CONFIG_LOCK(sc);
543     for (i = 0, dr = &sc->mlx_sysdrive[0];
544 	 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
545 	 i++, dr++) {
546 	/* are we already attached to this drive? */
547     	if (dr->ms_disk == 0) {
548 	    /* pick up drive information */
549 	    dr->ms_size = mes[i].sd_size;
550 	    dr->ms_raidlevel = mes[i].sd_raidlevel & 0xf;
551 	    dr->ms_state = mes[i].sd_state;
552 
553 	    /* generate geometry information */
554 	    if (sc->mlx_geom == MLX_GEOM_128_32) {
555 		dr->ms_heads = 128;
556 		dr->ms_sectors = 32;
557 		dr->ms_cylinders = dr->ms_size / (128 * 32);
558 	    } else {        /* MLX_GEOM_255/63 */
559 		dr->ms_heads = 255;
560 		dr->ms_sectors = 63;
561 		dr->ms_cylinders = dr->ms_size / (255 * 63);
562 	    }
563 	    dr->ms_disk =  device_add_child(sc->mlx_dev, /*"mlxd"*/NULL, -1);
564 	    if (dr->ms_disk == 0)
565 		device_printf(sc->mlx_dev, "device_add_child failed\n");
566 	    device_set_ivars(dr->ms_disk, dr);
567 	}
568     }
569     free(mes, M_DEVBUF);
570     if ((error = bus_generic_attach(sc->mlx_dev)) != 0)
571 	device_printf(sc->mlx_dev, "bus_generic_attach returned %d", error);
572 
573     /* mark controller back up */
574     MLX_IO_LOCK(sc);
575     sc->mlx_state &= ~MLX_STATE_SHUTDOWN;
576 
577     /* enable interrupts */
578     sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
579     MLX_IO_UNLOCK(sc);
580     MLX_CONFIG_UNLOCK(sc);
581 }
582 
583 /********************************************************************************
584  * Disconnect from the controller completely, in preparation for unload.
585  */
586 int
587 mlx_detach(device_t dev)
588 {
589     struct mlx_softc	*sc = device_get_softc(dev);
590     struct mlxd_softc	*mlxd;
591     int			i, error;
592 
593     debug_called(1);
594 
595     error = EBUSY;
596     MLX_CONFIG_LOCK(sc);
597     if (sc->mlx_state & MLX_STATE_OPEN)
598 	goto out;
599 
600     for (i = 0; i < MLX_MAXDRIVES; i++) {
601 	if (sc->mlx_sysdrive[i].ms_disk != 0) {
602 	    mlxd = device_get_softc(sc->mlx_sysdrive[i].ms_disk);
603 	    if (mlxd->mlxd_flags & MLXD_OPEN) {		/* drive is mounted, abort detach */
604 		device_printf(sc->mlx_sysdrive[i].ms_disk, "still open, can't detach\n");
605 		goto out;
606 	    }
607 	}
608     }
609     if ((error = mlx_shutdown(dev)))
610 	goto out;
611     MLX_CONFIG_UNLOCK(sc);
612 
613     mlx_free(sc);
614 
615     return (0);
616  out:
617     MLX_CONFIG_UNLOCK(sc);
618     return(error);
619 }
620 
621 /********************************************************************************
622  * Bring the controller down to a dormant state and detach all child devices.
623  *
624  * This function is called before detach, system shutdown, or before performing
625  * an operation which may add or delete system disks.  (Call mlx_startup to
626  * resume normal operation.)
627  *
628  * Note that we can assume that the bioq on the controller is empty, as we won't
629  * allow shutdown if any device is open.
630  */
631 int
632 mlx_shutdown(device_t dev)
633 {
634     struct mlx_softc	*sc = device_get_softc(dev);
635     int			error;
636 
637     MLX_CONFIG_LOCK(sc);
638     error = mlx_shutdown_locked(sc);
639     MLX_CONFIG_UNLOCK(sc);
640     return (error);
641 }
642 
643 static int
644 mlx_shutdown_locked(struct mlx_softc *sc)
645 {
646     int			i, error;
647 
648     debug_called(1);
649 
650     MLX_CONFIG_ASSERT_LOCKED(sc);
651 
652     MLX_IO_LOCK(sc);
653     sc->mlx_state |= MLX_STATE_SHUTDOWN;
654     sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
655 
656     /* flush controller */
657     device_printf(sc->mlx_dev, "flushing cache...");
658     if (mlx_flush(sc)) {
659 	printf("failed\n");
660     } else {
661 	printf("done\n");
662     }
663     MLX_IO_UNLOCK(sc);
664 
665     /* delete all our child devices */
666     for (i = 0; i < MLX_MAXDRIVES; i++) {
667 	if (sc->mlx_sysdrive[i].ms_disk != 0) {
668 	    if ((error = device_delete_child(sc->mlx_dev, sc->mlx_sysdrive[i].ms_disk)) != 0)
669 		return (error);
670 	    sc->mlx_sysdrive[i].ms_disk = 0;
671 	}
672     }
673 
674     return (0);
675 }
676 
677 /********************************************************************************
678  * Bring the controller to a quiescent state, ready for system suspend.
679  */
680 int
681 mlx_suspend(device_t dev)
682 {
683     struct mlx_softc	*sc = device_get_softc(dev);
684 
685     debug_called(1);
686 
687     MLX_IO_LOCK(sc);
688     sc->mlx_state |= MLX_STATE_SUSPEND;
689 
690     /* flush controller */
691     device_printf(sc->mlx_dev, "flushing cache...");
692     printf("%s\n", mlx_flush(sc) ? "failed" : "done");
693 
694     sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
695     MLX_IO_UNLOCK(sc);
696 
697     return(0);
698 }
699 
700 /********************************************************************************
701  * Bring the controller back to a state ready for operation.
702  */
703 int
704 mlx_resume(device_t dev)
705 {
706     struct mlx_softc	*sc = device_get_softc(dev);
707 
708     debug_called(1);
709 
710     MLX_IO_LOCK(sc);
711     sc->mlx_state &= ~MLX_STATE_SUSPEND;
712     sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
713     MLX_IO_UNLOCK(sc);
714 
715     return(0);
716 }
717 
718 /*******************************************************************************
719  * Take an interrupt, or be poked by other code to look for interrupt-worthy
720  * status.
721  */
722 void
723 mlx_intr(void *arg)
724 {
725     struct mlx_softc	*sc = (struct mlx_softc *)arg;
726 
727     debug_called(1);
728 
729     /* collect finished commands, queue anything waiting */
730     MLX_IO_LOCK(sc);
731     mlx_done(sc, 1);
732     MLX_IO_UNLOCK(sc);
733 };
734 
735 /*******************************************************************************
736  * Receive a buf structure from a child device and queue it on a particular
737  * disk resource, then poke the disk resource to start as much work as it can.
738  */
739 int
740 mlx_submit_buf(struct mlx_softc *sc, struct bio *bp)
741 {
742 
743     debug_called(1);
744 
745     MLX_IO_ASSERT_LOCKED(sc);
746     bioq_insert_tail(&sc->mlx_bioq, bp);
747     sc->mlx_waitbufs++;
748     mlx_startio(sc);
749     return(0);
750 }
751 
752 /********************************************************************************
753  * Accept an open operation on the control device.
754  */
755 int
756 mlx_open(struct cdev *dev, int flags, int fmt, struct thread *td)
757 {
758     struct mlx_softc	*sc = dev->si_drv1;
759 
760     MLX_CONFIG_LOCK(sc);
761     MLX_IO_LOCK(sc);
762     sc->mlx_state |= MLX_STATE_OPEN;
763     MLX_IO_UNLOCK(sc);
764     MLX_CONFIG_UNLOCK(sc);
765     return(0);
766 }
767 
768 /********************************************************************************
769  * Accept the last close on the control device.
770  */
771 int
772 mlx_close(struct cdev *dev, int flags, int fmt, struct thread *td)
773 {
774     struct mlx_softc	*sc = dev->si_drv1;
775 
776     MLX_CONFIG_LOCK(sc);
777     MLX_IO_LOCK(sc);
778     sc->mlx_state &= ~MLX_STATE_OPEN;
779     MLX_IO_UNLOCK(sc);
780     MLX_CONFIG_UNLOCK(sc);
781     return (0);
782 }
783 
784 /********************************************************************************
785  * Handle controller-specific control operations.
786  */
787 int
788 mlx_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
789 {
790     struct mlx_softc		*sc = dev->si_drv1;
791     struct mlx_rebuild_request	*rb = (struct mlx_rebuild_request *)addr;
792     struct mlx_rebuild_status	*rs = (struct mlx_rebuild_status *)addr;
793     int				*arg = (int *)addr;
794     struct mlx_pause		*mp;
795     struct mlx_sysdrive		*dr;
796     struct mlxd_softc		*mlxd;
797     int				i, error;
798 
799     switch(cmd) {
800 	/*
801 	 * Enumerate connected system drives; returns the first system drive's
802 	 * unit number if *arg is -1, or the next unit after *arg if it's
803 	 * a valid unit on this controller.
804 	 */
805     case MLX_NEXT_CHILD:
806 	/* search system drives */
807 	MLX_CONFIG_LOCK(sc);
808 	for (i = 0; i < MLX_MAXDRIVES; i++) {
809 	    /* is this one attached? */
810 	    if (sc->mlx_sysdrive[i].ms_disk != 0) {
811 		/* looking for the next one we come across? */
812 		if (*arg == -1) {
813 		    *arg = device_get_unit(sc->mlx_sysdrive[i].ms_disk);
814 		    MLX_CONFIG_UNLOCK(sc);
815 		    return(0);
816 		}
817 		/* we want the one after this one */
818 		if (*arg == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
819 		    *arg = -1;
820 	    }
821 	}
822 	MLX_CONFIG_UNLOCK(sc);
823 	return(ENOENT);
824 
825 	/*
826 	 * Scan the controller to see whether new drives have appeared.
827 	 */
828     case MLX_RESCAN_DRIVES:
829 	bus_topo_lock();
830 	mlx_startup(sc);
831 	bus_topo_unlock();
832 	return(0);
833 
834 	/*
835 	 * Disconnect from the specified drive; it may be about to go
836 	 * away.
837 	 */
838     case MLX_DETACH_DRIVE:			/* detach one drive */
839 	MLX_CONFIG_LOCK(sc);
840 	if (((dr = mlx_findunit(sc, *arg)) == NULL) ||
841 	    ((mlxd = device_get_softc(dr->ms_disk)) == NULL)) {
842 	    MLX_CONFIG_UNLOCK(sc);
843 	    return(ENOENT);
844 	}
845 
846 	device_printf(dr->ms_disk, "detaching...");
847 	error = 0;
848 	if (mlxd->mlxd_flags & MLXD_OPEN) {
849 	    error = EBUSY;
850 	    goto detach_out;
851 	}
852 
853 	/* flush controller */
854 	MLX_IO_LOCK(sc);
855 	if (mlx_flush(sc)) {
856 	    MLX_IO_UNLOCK(sc);
857 	    error = EBUSY;
858 	    goto detach_out;
859 	}
860 	MLX_IO_UNLOCK(sc);
861 
862 	/* nuke drive */
863 	if ((error = device_delete_child(sc->mlx_dev, dr->ms_disk)) != 0)
864 	    goto detach_out;
865 	dr->ms_disk = 0;
866 
867     detach_out:
868 	MLX_CONFIG_UNLOCK(sc);
869 	if (error) {
870 	    printf("failed\n");
871 	} else {
872 	    printf("done\n");
873 	}
874 	return(error);
875 
876 	/*
877 	 * Pause one or more SCSI channels for a period of time, to assist
878 	 * in the process of hot-swapping devices.
879 	 *
880 	 * Note that at least the 3.51 firmware on the DAC960PL doesn't seem
881 	 * to do this right.
882 	 */
883     case MLX_PAUSE_CHANNEL:			/* schedule a channel pause */
884 	/* Does this command work on this firmware? */
885 	if (!(sc->mlx_feature & MLX_FEAT_PAUSEWORKS))
886 	    return(EOPNOTSUPP);
887 
888 	/* check time values */
889 	mp = (struct mlx_pause *)addr;
890 	if ((mp->mp_when < 0) || (mp->mp_when > 3600))
891 	    return(EINVAL);
892 	if ((mp->mp_howlong < 1) || (mp->mp_howlong > (0xf * 30)))
893 	    return(EINVAL);
894 
895 	MLX_IO_LOCK(sc);
896 	if ((mp->mp_which == MLX_PAUSE_CANCEL) && (sc->mlx_pause.mp_when != 0)) {
897 	    /* cancel a pending pause operation */
898 	    sc->mlx_pause.mp_which = 0;
899 	} else {
900 	    /* fix for legal channels */
901 	    mp->mp_which &= ((1 << sc->mlx_enq2->me_actual_channels) -1);
902 
903 	    /* check for a pause currently running */
904 	    if ((sc->mlx_pause.mp_which != 0) && (sc->mlx_pause.mp_when == 0)) {
905 		MLX_IO_UNLOCK(sc);
906 		return(EBUSY);
907 	    }
908 
909 	    /* looks ok, go with it */
910 	    sc->mlx_pause.mp_which = mp->mp_which;
911 	    sc->mlx_pause.mp_when = time_second + mp->mp_when;
912 	    sc->mlx_pause.mp_howlong = sc->mlx_pause.mp_when + mp->mp_howlong;
913 	}
914 	MLX_IO_UNLOCK(sc);
915 	return(0);
916 
917 	/*
918 	 * Accept a command passthrough-style.
919 	 */
920     case MLX_COMMAND:
921 	return(mlx_user_command(sc, (struct mlx_usercommand *)addr));
922 
923 	/*
924 	 * Start a rebuild on a given SCSI disk
925 	 */
926     case MLX_REBUILDASYNC:
927 	MLX_IO_LOCK(sc);
928 	if (sc->mlx_background != 0) {
929 	    MLX_IO_UNLOCK(sc);
930 	    rb->rr_status = 0x0106;
931 	    return(EBUSY);
932 	}
933 	rb->rr_status = mlx_rebuild(sc, rb->rr_channel, rb->rr_target);
934 	switch (rb->rr_status) {
935 	case 0:
936 	    error = 0;
937 	    break;
938 	case 0x10000:
939 	    error = ENOMEM;		/* couldn't set up the command */
940 	    break;
941 	case 0x0002:
942 	    error = EBUSY;
943 	    break;
944 	case 0x0104:
945 	    error = EIO;
946 	    break;
947 	case 0x0105:
948 	    error = ERANGE;
949 	    break;
950 	case 0x0106:
951 	    error = EBUSY;
952 	    break;
953 	default:
954 	    error = EINVAL;
955 	    break;
956 	}
957 	if (error == 0)
958 	    sc->mlx_background = MLX_BACKGROUND_REBUILD;
959 	MLX_IO_UNLOCK(sc);
960 	return(error);
961 
962 	/*
963 	 * Get the status of the current rebuild or consistency check.
964 	 */
965     case MLX_REBUILDSTAT:
966 	MLX_IO_LOCK(sc);
967 	*rs = sc->mlx_rebuildstat;
968 	MLX_IO_UNLOCK(sc);
969 	return(0);
970 
971 	/*
972 	 * Return the per-controller system drive number matching the
973 	 * disk device number in (arg), if it happens to belong to us.
974 	 */
975     case MLX_GET_SYSDRIVE:
976 	error = ENOENT;
977 	MLX_CONFIG_LOCK(sc);
978 	bus_topo_lock();
979 	mlxd = devclass_get_softc(devclass_find("mlxd"), *arg);
980 	bus_topo_unlock();
981 	if ((mlxd != NULL) && (mlxd->mlxd_drive >= sc->mlx_sysdrive) &&
982 	    (mlxd->mlxd_drive < (sc->mlx_sysdrive + MLX_MAXDRIVES))) {
983 	    error = 0;
984 	    *arg = mlxd->mlxd_drive - sc->mlx_sysdrive;
985 	}
986 	MLX_CONFIG_UNLOCK(sc);
987 	return(error);
988 
989     default:
990 	return(ENOTTY);
991     }
992 }
993 
994 /********************************************************************************
995  * Handle operations requested by a System Drive connected to this controller.
996  */
997 int
998 mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd,
999 		caddr_t addr, int32_t flag, struct thread *td)
1000 {
1001     int				*arg = (int *)addr;
1002     int				error, result;
1003 
1004     switch(cmd) {
1005 	/*
1006 	 * Return the current status of this drive.
1007 	 */
1008     case MLXD_STATUS:
1009 	MLX_IO_LOCK(sc);
1010 	*arg = drive->ms_state;
1011 	MLX_IO_UNLOCK(sc);
1012 	return(0);
1013 
1014 	/*
1015 	 * Start a background consistency check on this drive.
1016 	 */
1017     case MLXD_CHECKASYNC:		/* start a background consistency check */
1018 	MLX_IO_LOCK(sc);
1019 	if (sc->mlx_background != 0) {
1020 	    MLX_IO_UNLOCK(sc);
1021 	    *arg = 0x0106;
1022 	    return(EBUSY);
1023 	}
1024 	result = mlx_check(sc, drive - &sc->mlx_sysdrive[0]);
1025 	switch (result) {
1026 	case 0:
1027 	    error = 0;
1028 	    break;
1029 	case 0x10000:
1030 	    error = ENOMEM;		/* couldn't set up the command */
1031 	    break;
1032 	case 0x0002:
1033 	    error = EIO;
1034 	    break;
1035 	case 0x0105:
1036 	    error = ERANGE;
1037 	    break;
1038 	case 0x0106:
1039 	    error = EBUSY;
1040 	    break;
1041 	default:
1042 	    error = EINVAL;
1043 	    break;
1044 	}
1045 	if (error == 0)
1046 	    sc->mlx_background = MLX_BACKGROUND_CHECK;
1047 	MLX_IO_UNLOCK(sc);
1048 	*arg = result;
1049 	return(error);
1050 
1051     }
1052     return(ENOIOCTL);
1053 }
1054 
1055 
1056 /********************************************************************************
1057  ********************************************************************************
1058                                                                 Status Monitoring
1059  ********************************************************************************
1060  ********************************************************************************/
1061 
1062 /********************************************************************************
1063  * Fire off commands to periodically check the status of connected drives.
1064  */
1065 static void
1066 mlx_periodic(void *data)
1067 {
1068     struct mlx_softc *sc = (struct mlx_softc *)data;
1069 
1070     debug_called(1);
1071     MLX_IO_ASSERT_LOCKED(sc);
1072 
1073     /*
1074      * Run a bus pause?
1075      */
1076     if ((sc->mlx_pause.mp_which != 0) &&
1077 	(sc->mlx_pause.mp_when > 0) &&
1078 	(time_second >= sc->mlx_pause.mp_when)){
1079 
1080 	mlx_pause_action(sc);		/* pause is running */
1081 	sc->mlx_pause.mp_when = 0;
1082 	sysbeep(500, SBT_1S);
1083 
1084 	/*
1085 	 * Bus pause still running?
1086 	 */
1087     } else if ((sc->mlx_pause.mp_which != 0) &&
1088 	       (sc->mlx_pause.mp_when == 0)) {
1089 
1090 	/* time to stop bus pause? */
1091 	if (time_second >= sc->mlx_pause.mp_howlong) {
1092 	    mlx_pause_action(sc);
1093 	    sc->mlx_pause.mp_which = 0;	/* pause is complete */
1094 	    sysbeep(500, SBT_1S);
1095 	} else {
1096 	    sysbeep((time_second % 5) * 100 + 500, SBT_1S / 8);
1097 	}
1098 
1099 	/*
1100 	 * Run normal periodic activities?
1101 	 */
1102     } else if (time_second > (sc->mlx_lastpoll + 10)) {
1103 	sc->mlx_lastpoll = time_second;
1104 
1105 	/*
1106 	 * Check controller status.
1107 	 *
1108 	 * XXX Note that this may not actually launch a command in situations of high load.
1109 	 */
1110 	mlx_enquire(sc, (sc->mlx_iftype == MLX_IFTYPE_2) ? MLX_CMD_ENQUIRY_OLD : MLX_CMD_ENQUIRY,
1111 		    imax(sizeof(struct mlx_enquiry), sizeof(struct mlx_enquiry_old)), mlx_periodic_enquiry);
1112 
1113 	/*
1114 	 * Check system drive status.
1115 	 *
1116 	 * XXX This might be better left to event-driven detection, eg. I/O to an offline
1117 	 *     drive will detect it's offline, rebuilds etc. should detect the drive is back
1118 	 *     online.
1119 	 */
1120 	mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(struct mlx_enq_sys_drive) * MLX_MAXDRIVES,
1121 			mlx_periodic_enquiry);
1122 
1123     }
1124 
1125     /* get drive rebuild/check status */
1126     /* XXX should check sc->mlx_background if this is only valid while in progress */
1127     mlx_enquire(sc, MLX_CMD_REBUILDSTAT, sizeof(struct mlx_rebuild_stat), mlx_periodic_rebuild);
1128 
1129     /* deal with possibly-missed interrupts and timed-out commands */
1130     mlx_done(sc, 1);
1131 
1132     /* reschedule another poll next second or so */
1133     callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
1134 }
1135 
1136 /********************************************************************************
1137  * Handle the result of an ENQUIRY command instigated by periodic status polling.
1138  */
1139 static void
1140 mlx_periodic_enquiry(struct mlx_command *mc)
1141 {
1142     struct mlx_softc		*sc = mc->mc_sc;
1143 
1144     debug_called(1);
1145     MLX_IO_ASSERT_LOCKED(sc);
1146 
1147     /* Command completed OK? */
1148     if (mc->mc_status != 0) {
1149 	device_printf(sc->mlx_dev, "periodic enquiry failed - %s\n", mlx_diagnose_command(mc));
1150 	goto out;
1151     }
1152 
1153     /* respond to command */
1154     switch(mc->mc_mailbox[0]) {
1155 	/*
1156 	 * This is currently a bit fruitless, as we don't know how to extract the eventlog
1157 	 * pointer yet.
1158 	 */
1159     case MLX_CMD_ENQUIRY_OLD:
1160     {
1161 	struct mlx_enquiry		*me = (struct mlx_enquiry *)mc->mc_data;
1162 	struct mlx_enquiry_old		*meo = (struct mlx_enquiry_old *)mc->mc_data;
1163 	int				i;
1164 
1165 	/* convert data in-place to new format */
1166 	for (i = (sizeof(me->me_dead) / sizeof(me->me_dead[0])) - 1; i >= 0; i--) {
1167 	    me->me_dead[i].dd_chan = meo->me_dead[i].dd_chan;
1168 	    me->me_dead[i].dd_targ = meo->me_dead[i].dd_targ;
1169 	}
1170 	me->me_misc_flags        = 0;
1171 	me->me_rebuild_count     = meo->me_rebuild_count;
1172 	me->me_dead_count        = meo->me_dead_count;
1173 	me->me_critical_sd_count = meo->me_critical_sd_count;
1174 	me->me_event_log_seq_num = 0;
1175 	me->me_offline_sd_count  = meo->me_offline_sd_count;
1176 	me->me_max_commands      = meo->me_max_commands;
1177 	me->me_rebuild_flag      = meo->me_rebuild_flag;
1178 	me->me_fwmajor           = meo->me_fwmajor;
1179 	me->me_fwminor           = meo->me_fwminor;
1180 	me->me_status_flags      = meo->me_status_flags;
1181 	me->me_flash_age         = meo->me_flash_age;
1182 	for (i = (sizeof(me->me_drvsize) / sizeof(me->me_drvsize[0])) - 1; i >= 0; i--) {
1183 	    if (i > ((sizeof(meo->me_drvsize) / sizeof(meo->me_drvsize[0])) - 1)) {
1184 		me->me_drvsize[i] = 0;		/* drive beyond supported range */
1185 	    } else {
1186 		me->me_drvsize[i] = meo->me_drvsize[i];
1187 	    }
1188 	}
1189 	me->me_num_sys_drvs = meo->me_num_sys_drvs;
1190     }
1191     /* FALLTHROUGH */
1192 
1193 	/*
1194 	 * Generic controller status update.  We could do more with this than just
1195 	 * checking the event log.
1196 	 */
1197     case MLX_CMD_ENQUIRY:
1198     {
1199 	struct mlx_enquiry		*me = (struct mlx_enquiry *)mc->mc_data;
1200 
1201 	if (sc->mlx_currevent == -1) {
1202 	    /* initialise our view of the event log */
1203 	    sc->mlx_currevent = sc->mlx_lastevent = me->me_event_log_seq_num;
1204 	} else if ((me->me_event_log_seq_num != sc->mlx_lastevent) && !(sc->mlx_flags & MLX_EVENTLOG_BUSY)) {
1205 	    /* record where current events are up to */
1206 	    sc->mlx_currevent = me->me_event_log_seq_num;
1207 	    debug(1, "event log pointer was %d, now %d\n", sc->mlx_lastevent, sc->mlx_currevent);
1208 
1209 	    /* mark the event log as busy */
1210 	    sc->mlx_flags |= MLX_EVENTLOG_BUSY;
1211 
1212 	    /* drain new eventlog entries */
1213 	    mlx_periodic_eventlog_poll(sc);
1214 	}
1215 	break;
1216     }
1217     case MLX_CMD_ENQSYSDRIVE:
1218     {
1219 	struct mlx_enq_sys_drive	*mes = (struct mlx_enq_sys_drive *)mc->mc_data;
1220 	struct mlx_sysdrive		*dr;
1221 	int				i;
1222 
1223 	for (i = 0, dr = &sc->mlx_sysdrive[0];
1224 	     (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
1225 	     i++) {
1226 
1227 	    /* has state been changed by controller? */
1228 	    if (dr->ms_state != mes[i].sd_state) {
1229 		switch(mes[i].sd_state) {
1230 		case MLX_SYSD_OFFLINE:
1231 		    device_printf(dr->ms_disk, "drive offline\n");
1232 		    break;
1233 		case MLX_SYSD_ONLINE:
1234 		    device_printf(dr->ms_disk, "drive online\n");
1235 		    break;
1236 		case MLX_SYSD_CRITICAL:
1237 		    device_printf(dr->ms_disk, "drive critical\n");
1238 		    break;
1239 		}
1240 		/* save new state */
1241 		dr->ms_state = mes[i].sd_state;
1242 	    }
1243 	}
1244 	break;
1245     }
1246     default:
1247 	device_printf(sc->mlx_dev, "%s: unknown command 0x%x", __func__, mc->mc_mailbox[0]);
1248 	break;
1249     }
1250 
1251  out:
1252     free(mc->mc_data, M_DEVBUF);
1253     mlx_releasecmd(mc);
1254 }
1255 
1256 static void
1257 mlx_eventlog_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1258 {
1259     struct mlx_command *mc;
1260 
1261     mc = (struct mlx_command *)arg;
1262     mlx_setup_dmamap(mc, segs, nsegments, error);
1263 
1264     /* build the command to get one entry */
1265     mlx_make_type3(mc, MLX_CMD_LOGOP, MLX_LOGOP_GET, 1,
1266 		   mc->mc_sc->mlx_lastevent, 0, 0, mc->mc_dataphys, 0);
1267     mc->mc_complete = mlx_periodic_eventlog_respond;
1268     mc->mc_private = mc;
1269 
1270     /* start the command */
1271     if (mlx_start(mc) != 0) {
1272 	mlx_releasecmd(mc);
1273 	free(mc->mc_data, M_DEVBUF);
1274 	mc->mc_data = NULL;
1275     }
1276 
1277 }
1278 
1279 /********************************************************************************
1280  * Instigate a poll for one event log message on (sc).
1281  * We only poll for one message at a time, to keep our command usage down.
1282  */
1283 static void
1284 mlx_periodic_eventlog_poll(struct mlx_softc *sc)
1285 {
1286     struct mlx_command	*mc;
1287     void		*result = NULL;
1288     int			error = 0;
1289 
1290     debug_called(1);
1291     MLX_IO_ASSERT_LOCKED(sc);
1292 
1293     /* get ourselves a command buffer */
1294     error = 1;
1295     if ((mc = mlx_alloccmd(sc)) == NULL)
1296 	goto out;
1297 
1298     /* allocate the response structure */
1299     if ((result = malloc(/*sizeof(struct mlx_eventlog_entry)*/1024, M_DEVBUF,
1300 			 M_NOWAIT)) == NULL)
1301 	goto out;
1302 
1303     /* get a command slot */
1304     if (mlx_getslot(mc))
1305 	goto out;
1306 
1307     /* map the command so the controller can see it */
1308     mc->mc_data = result;
1309     mc->mc_length = /*sizeof(struct mlx_eventlog_entry)*/1024;
1310     error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
1311 			    mc->mc_length, mlx_eventlog_cb, mc, BUS_DMA_NOWAIT);
1312 
1313  out:
1314     if (error != 0) {
1315 	if (mc != NULL)
1316 	    mlx_releasecmd(mc);
1317 	if ((result != NULL) && (mc->mc_data != NULL))
1318 	    free(result, M_DEVBUF);
1319     }
1320 }
1321 
1322 /********************************************************************************
1323  * Handle the result of polling for a log message, generate diagnostic output.
1324  * If this wasn't the last message waiting for us, we'll go collect another.
1325  */
1326 static char *mlx_sense_messages[] = {
1327     "because write recovery failed",
1328     "because of SCSI bus reset failure",
1329     "because of double check condition",
1330     "because it was removed",
1331     "because of gross error on SCSI chip",
1332     "because of bad tag returned from drive",
1333     "because of timeout on SCSI command",
1334     "because of reset SCSI command issued from system",
1335     "because busy or parity error count exceeded limit",
1336     "because of 'kill drive' command from system",
1337     "because of selection timeout",
1338     "due to SCSI phase sequence error",
1339     "due to unknown status"
1340 };
1341 
1342 static void
1343 mlx_periodic_eventlog_respond(struct mlx_command *mc)
1344 {
1345     struct mlx_softc		*sc = mc->mc_sc;
1346     struct mlx_eventlog_entry	*el = (struct mlx_eventlog_entry *)mc->mc_data;
1347     char			*reason;
1348 
1349     debug_called(1);
1350     MLX_IO_ASSERT_LOCKED(sc);
1351 
1352     sc->mlx_lastevent++;		/* next message... */
1353     if (mc->mc_status == 0) {
1354 
1355 	/* handle event log message */
1356 	switch(el->el_type) {
1357 	    /*
1358 	     * This is the only sort of message we understand at the moment.
1359 	     * The tests here are probably incomplete.
1360 	     */
1361 	case MLX_LOGMSG_SENSE:	/* sense data */
1362 	    /* Mylex vendor-specific message indicating a drive was killed? */
1363 	    if ((el->el_sensekey == 9) &&
1364 		(el->el_asc == 0x80)) {
1365 		if (el->el_asq < nitems(mlx_sense_messages)) {
1366 		    reason = mlx_sense_messages[el->el_asq];
1367 		} else {
1368 		    reason = "for unknown reason";
1369 		}
1370 		device_printf(sc->mlx_dev, "physical drive %d:%d killed %s\n",
1371 			      el->el_channel, el->el_target, reason);
1372 	    }
1373 	    /* SCSI drive was reset? */
1374 	    if ((el->el_sensekey == 6) && (el->el_asc == 0x29)) {
1375 		device_printf(sc->mlx_dev, "physical drive %d:%d reset\n",
1376 			      el->el_channel, el->el_target);
1377 	    }
1378 	    /* SCSI drive error? */
1379 	    if (!((el->el_sensekey == 0) ||
1380 		  ((el->el_sensekey == 2) &&
1381 		   (el->el_asc == 0x04) &&
1382 		   ((el->el_asq == 0x01) ||
1383 		    (el->el_asq == 0x02))))) {
1384 		device_printf(sc->mlx_dev, "physical drive %d:%d error log: sense = %d asc = %x asq = %x\n",
1385 			      el->el_channel, el->el_target, el->el_sensekey, el->el_asc, el->el_asq);
1386 		device_printf(sc->mlx_dev, "  info %4D csi %4D\n", el->el_information, ":", el->el_csi, ":");
1387 	    }
1388 	    break;
1389 
1390 	default:
1391 	    device_printf(sc->mlx_dev, "unknown log message type 0x%x\n", el->el_type);
1392 	    break;
1393 	}
1394     } else {
1395 	device_printf(sc->mlx_dev, "error reading message log - %s\n", mlx_diagnose_command(mc));
1396 	/* give up on all the outstanding messages, as we may have come unsynched */
1397 	sc->mlx_lastevent = sc->mlx_currevent;
1398     }
1399 
1400     /* dispose of command and data */
1401     free(mc->mc_data, M_DEVBUF);
1402     mlx_releasecmd(mc);
1403 
1404     /* is there another message to obtain? */
1405     if (sc->mlx_lastevent != sc->mlx_currevent) {
1406 	mlx_periodic_eventlog_poll(sc);
1407     } else {
1408 	/* clear log-busy status */
1409 	sc->mlx_flags &= ~MLX_EVENTLOG_BUSY;
1410     }
1411 }
1412 
1413 /********************************************************************************
1414  * Handle check/rebuild operations in progress.
1415  */
1416 static void
1417 mlx_periodic_rebuild(struct mlx_command *mc)
1418 {
1419     struct mlx_softc		*sc = mc->mc_sc;
1420     struct mlx_rebuild_status	*mr = (struct mlx_rebuild_status *)mc->mc_data;
1421 
1422     MLX_IO_ASSERT_LOCKED(sc);
1423     switch(mc->mc_status) {
1424     case 0:				/* operation running, update stats */
1425 	sc->mlx_rebuildstat = *mr;
1426 
1427 	/* spontaneous rebuild/check? */
1428 	if (sc->mlx_background == 0) {
1429 	    sc->mlx_background = MLX_BACKGROUND_SPONTANEOUS;
1430 	    device_printf(sc->mlx_dev, "background check/rebuild operation started\n");
1431 	}
1432 	break;
1433 
1434     case 0x0105:			/* nothing running, finalise stats and report */
1435 	switch(sc->mlx_background) {
1436 	case MLX_BACKGROUND_CHECK:
1437 	    device_printf(sc->mlx_dev, "consistency check completed\n");	/* XXX print drive? */
1438 	    break;
1439 	case MLX_BACKGROUND_REBUILD:
1440 	    device_printf(sc->mlx_dev, "drive rebuild completed\n");	/* XXX print channel/target? */
1441 	    break;
1442 	case MLX_BACKGROUND_SPONTANEOUS:
1443 	default:
1444 	    /* if we have previously been non-idle, report the transition */
1445 	    if (sc->mlx_rebuildstat.rs_code != MLX_REBUILDSTAT_IDLE) {
1446 		device_printf(sc->mlx_dev, "background check/rebuild operation completed\n");
1447 	    }
1448 	}
1449 	sc->mlx_background = 0;
1450 	sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
1451 	break;
1452     }
1453     free(mc->mc_data, M_DEVBUF);
1454     mlx_releasecmd(mc);
1455 }
1456 
1457 /********************************************************************************
1458  ********************************************************************************
1459                                                                     Channel Pause
1460  ********************************************************************************
1461  ********************************************************************************/
1462 
1463 /********************************************************************************
1464  * It's time to perform a channel pause action for (sc), either start or stop
1465  * the pause.
1466  */
1467 static void
1468 mlx_pause_action(struct mlx_softc *sc)
1469 {
1470     struct mlx_command	*mc;
1471     int			failsafe, i, command;
1472 
1473     MLX_IO_ASSERT_LOCKED(sc);
1474 
1475     /* What are we doing here? */
1476     if (sc->mlx_pause.mp_when == 0) {
1477 	command = MLX_CMD_STARTCHANNEL;
1478 	failsafe = 0;
1479 
1480     } else {
1481 	command = MLX_CMD_STOPCHANNEL;
1482 
1483 	/*
1484 	 * Channels will always start again after the failsafe period,
1485 	 * which is specified in multiples of 30 seconds.
1486 	 * This constrains us to a maximum pause of 450 seconds.
1487 	 */
1488 	failsafe = ((sc->mlx_pause.mp_howlong - time_second) + 5) / 30;
1489 	if (failsafe > 0xf) {
1490 	    failsafe = 0xf;
1491 	    sc->mlx_pause.mp_howlong = time_second + (0xf * 30) - 5;
1492 	}
1493     }
1494 
1495     /* build commands for every channel requested */
1496     for (i = 0; i < sc->mlx_enq2->me_actual_channels; i++) {
1497 	if ((1 << i) & sc->mlx_pause.mp_which) {
1498 
1499 	    /* get ourselves a command buffer */
1500 	    if ((mc = mlx_alloccmd(sc)) == NULL)
1501 		goto fail;
1502 	    /* get a command slot */
1503 	    mc->mc_flags |= MLX_CMD_PRIORITY;
1504 	    if (mlx_getslot(mc))
1505 		goto fail;
1506 
1507 	    /* build the command */
1508 	    mlx_make_type2(mc, command, (failsafe << 4) | i, 0, 0, 0, 0, 0, 0, 0);
1509 	    mc->mc_complete = mlx_pause_done;
1510 	    mc->mc_private = sc;		/* XXX not needed */
1511 	    if (mlx_start(mc))
1512 		goto fail;
1513 	    /* command submitted OK */
1514 	    return;
1515 
1516 	fail:
1517 	    device_printf(sc->mlx_dev, "%s failed for channel %d\n",
1518 			  command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", i);
1519 	    if (mc != NULL)
1520 		mlx_releasecmd(mc);
1521 	}
1522     }
1523 }
1524 
1525 static void
1526 mlx_pause_done(struct mlx_command *mc)
1527 {
1528     struct mlx_softc	*sc = mc->mc_sc;
1529     int			command = mc->mc_mailbox[0];
1530     int			channel = mc->mc_mailbox[2] & 0xf;
1531 
1532     MLX_IO_ASSERT_LOCKED(sc);
1533     if (mc->mc_status != 0) {
1534 	device_printf(sc->mlx_dev, "%s command failed - %s\n",
1535 		      command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", mlx_diagnose_command(mc));
1536     } else if (command == MLX_CMD_STOPCHANNEL) {
1537 	device_printf(sc->mlx_dev, "channel %d pausing for %ld seconds\n",
1538 		      channel, (long)(sc->mlx_pause.mp_howlong - time_second));
1539     } else {
1540 	device_printf(sc->mlx_dev, "channel %d resuming\n", channel);
1541     }
1542     mlx_releasecmd(mc);
1543 }
1544 
1545 /********************************************************************************
1546  ********************************************************************************
1547                                                                Command Submission
1548  ********************************************************************************
1549  ********************************************************************************/
1550 
1551 static void
1552 mlx_enquire_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1553 {
1554     struct mlx_softc *sc;
1555     struct mlx_command *mc;
1556 
1557     mc = (struct mlx_command *)arg;
1558     if (error)
1559 	return;
1560 
1561     mlx_setup_dmamap(mc, segs, nsegments, error);
1562 
1563     /* build an enquiry command */
1564     sc = mc->mc_sc;
1565     mlx_make_type2(mc, mc->mc_command, 0, 0, 0, 0, 0, 0, mc->mc_dataphys, 0);
1566 
1567     /* do we want a completion callback? */
1568     if (mc->mc_complete != NULL) {
1569 	if ((error = mlx_start(mc)) != 0)
1570 	    return;
1571     } else {
1572 	/* run the command in either polled or wait mode */
1573 	if ((sc->mlx_state & MLX_STATE_INTEN) ? mlx_wait_command(mc) :
1574 						mlx_poll_command(mc))
1575 	    return;
1576 
1577 	/* command completed OK? */
1578 	if (mc->mc_status != 0) {
1579 	    device_printf(sc->mlx_dev, "ENQUIRY failed - %s\n",
1580 			  mlx_diagnose_command(mc));
1581 	    return;
1582 	}
1583     }
1584 }
1585 
1586 /********************************************************************************
1587  * Perform an Enquiry command using a type-3 command buffer and a return a single
1588  * linear result buffer.  If the completion function is specified, it will
1589  * be called with the completed command (and the result response will not be
1590  * valid until that point).  Otherwise, the command will either be busy-waited
1591  * for (interrupts not enabled), or slept for.
1592  */
1593 static void *
1594 mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize, void (* complete)(struct mlx_command *mc))
1595 {
1596     struct mlx_command	*mc;
1597     void		*result;
1598     int			error;
1599 
1600     debug_called(1);
1601     MLX_IO_ASSERT_LOCKED(sc);
1602 
1603     /* get ourselves a command buffer */
1604     error = 1;
1605     result = NULL;
1606     if ((mc = mlx_alloccmd(sc)) == NULL)
1607 	goto out;
1608     /* allocate the response structure */
1609     if ((result = malloc(bufsize, M_DEVBUF, M_NOWAIT)) == NULL)
1610 	goto out;
1611     /* get a command slot */
1612     mc->mc_flags |= MLX_CMD_PRIORITY | MLX_CMD_DATAOUT;
1613     if (mlx_getslot(mc))
1614 	goto out;
1615 
1616     /* map the command so the controller can see it */
1617     mc->mc_data = result;
1618     mc->mc_length = bufsize;
1619     mc->mc_command = command;
1620 
1621     if (complete != NULL) {
1622 	mc->mc_complete = complete;
1623 	mc->mc_private = mc;
1624     }
1625 
1626     error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
1627 			    mc->mc_length, mlx_enquire_cb, mc, BUS_DMA_NOWAIT);
1628 
1629  out:
1630     /* we got a command, but nobody else will free it */
1631     if ((mc != NULL) && (mc->mc_complete == NULL))
1632 	mlx_releasecmd(mc);
1633     /* we got an error, and we allocated a result */
1634     if ((error != 0) && (result != NULL)) {
1635 	free(result, M_DEVBUF);
1636 	result = NULL;
1637     }
1638     return(result);
1639 }
1640 
1641 
1642 /********************************************************************************
1643  * Perform a Flush command on the nominated controller.
1644  *
1645  * May be called with interrupts enabled or disabled; will not return until
1646  * the flush operation completes or fails.
1647  */
1648 static int
1649 mlx_flush(struct mlx_softc *sc)
1650 {
1651     struct mlx_command	*mc;
1652     int			error;
1653 
1654     debug_called(1);
1655     MLX_IO_ASSERT_LOCKED(sc);
1656 
1657     /* get ourselves a command buffer */
1658     error = 1;
1659     if ((mc = mlx_alloccmd(sc)) == NULL)
1660 	goto out;
1661     /* get a command slot */
1662     if (mlx_getslot(mc))
1663 	goto out;
1664 
1665     /* build a flush command */
1666     mlx_make_type2(mc, MLX_CMD_FLUSH, 0, 0, 0, 0, 0, 0, 0, 0);
1667 
1668     /* can't assume that interrupts are going to work here, so play it safe */
1669     if (mlx_poll_command(mc))
1670 	goto out;
1671 
1672     /* command completed OK? */
1673     if (mc->mc_status != 0) {
1674 	device_printf(sc->mlx_dev, "FLUSH failed - %s\n", mlx_diagnose_command(mc));
1675 	goto out;
1676     }
1677 
1678     error = 0;			/* success */
1679  out:
1680     if (mc != NULL)
1681 	mlx_releasecmd(mc);
1682     return(error);
1683 }
1684 
1685 /********************************************************************************
1686  * Start a background consistency check on (drive).
1687  *
1688  * May be called with interrupts enabled or disabled; will return as soon as the
1689  * operation has started or been refused.
1690  */
1691 static int
1692 mlx_check(struct mlx_softc *sc, int drive)
1693 {
1694     struct mlx_command	*mc;
1695     int			error;
1696 
1697     debug_called(1);
1698     MLX_IO_ASSERT_LOCKED(sc);
1699 
1700     /* get ourselves a command buffer */
1701     error = 0x10000;
1702     if ((mc = mlx_alloccmd(sc)) == NULL)
1703 	goto out;
1704     /* get a command slot */
1705     if (mlx_getslot(mc))
1706 	goto out;
1707 
1708     /* build a checkasync command, set the "fix it" flag */
1709     mlx_make_type2(mc, MLX_CMD_CHECKASYNC, 0, 0, 0, 0, 0, drive | 0x80, 0, 0);
1710 
1711     /* start the command and wait for it to be returned */
1712     if (mlx_wait_command(mc))
1713 	goto out;
1714 
1715     /* command completed OK? */
1716     if (mc->mc_status != 0) {
1717 	device_printf(sc->mlx_dev, "CHECK ASYNC failed - %s\n", mlx_diagnose_command(mc));
1718     } else {
1719 	device_printf(sc->mlx_sysdrive[drive].ms_disk, "consistency check started");
1720     }
1721     error = mc->mc_status;
1722 
1723  out:
1724     if (mc != NULL)
1725 	mlx_releasecmd(mc);
1726     return(error);
1727 }
1728 
1729 /********************************************************************************
1730  * Start a background rebuild of the physical drive at (channel),(target).
1731  *
1732  * May be called with interrupts enabled or disabled; will return as soon as the
1733  * operation has started or been refused.
1734  */
1735 static int
1736 mlx_rebuild(struct mlx_softc *sc, int channel, int target)
1737 {
1738     struct mlx_command	*mc;
1739     int			error;
1740 
1741     debug_called(1);
1742     MLX_IO_ASSERT_LOCKED(sc);
1743 
1744     /* get ourselves a command buffer */
1745     error = 0x10000;
1746     if ((mc = mlx_alloccmd(sc)) == NULL)
1747 	goto out;
1748     /* get a command slot */
1749     if (mlx_getslot(mc))
1750 	goto out;
1751 
1752     /* build a checkasync command, set the "fix it" flag */
1753     mlx_make_type2(mc, MLX_CMD_REBUILDASYNC, channel, target, 0, 0, 0, 0, 0, 0);
1754 
1755     /* start the command and wait for it to be returned */
1756     if (mlx_wait_command(mc))
1757 	goto out;
1758 
1759     /* command completed OK? */
1760     if (mc->mc_status != 0) {
1761 	device_printf(sc->mlx_dev, "REBUILD ASYNC failed - %s\n", mlx_diagnose_command(mc));
1762     } else {
1763 	device_printf(sc->mlx_dev, "drive rebuild started for %d:%d\n", channel, target);
1764     }
1765     error = mc->mc_status;
1766 
1767  out:
1768     if (mc != NULL)
1769 	mlx_releasecmd(mc);
1770     return(error);
1771 }
1772 
1773 /********************************************************************************
1774  * Run the command (mc) and return when it completes.
1775  *
1776  * Interrupts need to be enabled; returns nonzero on error.
1777  */
1778 static int
1779 mlx_wait_command(struct mlx_command *mc)
1780 {
1781     struct mlx_softc	*sc = mc->mc_sc;
1782     int			error, count;
1783 
1784     debug_called(1);
1785     MLX_IO_ASSERT_LOCKED(sc);
1786 
1787     mc->mc_complete = NULL;
1788     mc->mc_private = mc;		/* wake us when you're done */
1789     if ((error = mlx_start(mc)) != 0)
1790 	return(error);
1791 
1792     count = 0;
1793     /* XXX better timeout? */
1794     while ((mc->mc_status == MLX_STATUS_BUSY) && (count < 30)) {
1795 	mtx_sleep(mc->mc_private, &sc->mlx_io_lock, PRIBIO | PCATCH, "mlxwcmd", hz);
1796     }
1797 
1798     if (mc->mc_status != 0) {
1799 	device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1800 	return(EIO);
1801     }
1802     return(0);
1803 }
1804 
1805 
1806 /********************************************************************************
1807  * Start the command (mc) and busy-wait for it to complete.
1808  *
1809  * Should only be used when interrupts can't be relied upon. Returns 0 on
1810  * success, nonzero on error.
1811  * Successfully completed commands are dequeued.
1812  */
1813 static int
1814 mlx_poll_command(struct mlx_command *mc)
1815 {
1816     struct mlx_softc	*sc = mc->mc_sc;
1817     int			error, count;
1818 
1819     debug_called(1);
1820     MLX_IO_ASSERT_LOCKED(sc);
1821 
1822     mc->mc_complete = NULL;
1823     mc->mc_private = NULL;	/* we will poll for it */
1824     if ((error = mlx_start(mc)) != 0)
1825 	return(error);
1826 
1827     count = 0;
1828     do {
1829 	/* poll for completion */
1830 	mlx_done(mc->mc_sc, 1);
1831 
1832     } while ((mc->mc_status == MLX_STATUS_BUSY) && (count++ < 15000000));
1833     if (mc->mc_status != MLX_STATUS_BUSY) {
1834 	TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
1835 	return(0);
1836     }
1837     device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1838     return(EIO);
1839 }
1840 
1841 void
1842 mlx_startio_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1843 {
1844     struct mlx_command	*mc;
1845     struct mlxd_softc	*mlxd;
1846     struct mlx_softc	*sc;
1847     struct bio		*bp;
1848     int			blkcount;
1849     int			driveno;
1850     int			cmd;
1851 
1852     mc = (struct mlx_command *)arg;
1853     mlx_setup_dmamap(mc, segs, nsegments, error);
1854 
1855     sc = mc->mc_sc;
1856     bp = mc->mc_private;
1857 
1858     if (bp->bio_cmd == BIO_READ) {
1859 	mc->mc_flags |= MLX_CMD_DATAIN;
1860 	cmd = MLX_CMD_READSG;
1861     } else {
1862 	mc->mc_flags |= MLX_CMD_DATAOUT;
1863 	cmd = MLX_CMD_WRITESG;
1864     }
1865 
1866     /* build a suitable I/O command (assumes 512-byte rounded transfers) */
1867     mlxd = bp->bio_disk->d_drv1;
1868     driveno = mlxd->mlxd_drive - sc->mlx_sysdrive;
1869     blkcount = howmany(bp->bio_bcount, MLX_BLKSIZE);
1870 
1871     if ((bp->bio_pblkno + blkcount) > sc->mlx_sysdrive[driveno].ms_size)
1872 	device_printf(sc->mlx_dev,
1873 		      "I/O beyond end of unit (%lld,%d > %lu)\n",
1874 		      (long long)bp->bio_pblkno, blkcount,
1875 		      (u_long)sc->mlx_sysdrive[driveno].ms_size);
1876 
1877     /*
1878      * Build the I/O command.  Note that the SG list type bits are set to zero,
1879      * denoting the format of SG list that we are using.
1880      */
1881     if (sc->mlx_iftype == MLX_IFTYPE_2) {
1882 	mlx_make_type1(mc, (cmd == MLX_CMD_WRITESG) ? MLX_CMD_WRITESG_OLD :
1883 						      MLX_CMD_READSG_OLD,
1884 		       blkcount & 0xff, 	/* xfer length low byte */
1885 		       bp->bio_pblkno,		/* physical block number */
1886 		       driveno,			/* target drive number */
1887 		       mc->mc_sgphys,		/* location of SG list */
1888 		       mc->mc_nsgent & 0x3f);	/* size of SG list */
1889 	} else {
1890 	mlx_make_type5(mc, cmd,
1891 		       blkcount & 0xff, 	/* xfer length low byte */
1892 		       (driveno << 3) | ((blkcount >> 8) & 0x07),
1893 						/* target+length high 3 bits */
1894 		       bp->bio_pblkno,		/* physical block number */
1895 		       mc->mc_sgphys,		/* location of SG list */
1896 		       mc->mc_nsgent & 0x3f);	/* size of SG list */
1897     }
1898 
1899     /* try to give command to controller */
1900     if (mlx_start(mc) != 0) {
1901 	/* fail the command */
1902 	mc->mc_status = MLX_STATUS_WEDGED;
1903 	mlx_completeio(mc);
1904     }
1905 
1906     sc->mlx_state &= ~MLX_STATE_QFROZEN;
1907 }
1908 
1909 /********************************************************************************
1910  * Pull as much work off the softc's work queue as possible and give it to the
1911  * controller.  Leave a couple of slots free for emergencies.
1912  */
1913 static void
1914 mlx_startio(struct mlx_softc *sc)
1915 {
1916     struct mlx_command	*mc;
1917     struct bio		*bp;
1918     int			error;
1919 
1920     MLX_IO_ASSERT_LOCKED(sc);
1921 
1922     /* spin until something prevents us from doing any work */
1923     for (;;) {
1924 	if (sc->mlx_state & MLX_STATE_QFROZEN)
1925 	    break;
1926 
1927 	/* see if there's work to be done */
1928 	if ((bp = bioq_first(&sc->mlx_bioq)) == NULL)
1929 	    break;
1930 	/* get a command */
1931 	if ((mc = mlx_alloccmd(sc)) == NULL)
1932 	    break;
1933 	/* get a slot for the command */
1934 	if (mlx_getslot(mc) != 0) {
1935 	    mlx_releasecmd(mc);
1936 	    break;
1937 	}
1938 	/* get the buf containing our work */
1939 	bioq_remove(&sc->mlx_bioq, bp);
1940 	sc->mlx_waitbufs--;
1941 
1942 	/* connect the buf to the command */
1943 	mc->mc_complete = mlx_completeio;
1944 	mc->mc_private = bp;
1945 	mc->mc_data = bp->bio_data;
1946 	mc->mc_length = bp->bio_bcount;
1947 
1948 	/* map the command so the controller can work with it */
1949 	error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
1950 				mc->mc_length, mlx_startio_cb, mc, 0);
1951 	if (error == EINPROGRESS) {
1952 	    sc->mlx_state |= MLX_STATE_QFROZEN;
1953 	    break;
1954 	}
1955     }
1956 }
1957 
1958 /********************************************************************************
1959  * Handle completion of an I/O command.
1960  */
1961 static void
1962 mlx_completeio(struct mlx_command *mc)
1963 {
1964     struct mlx_softc	*sc = mc->mc_sc;
1965     struct bio		*bp = mc->mc_private;
1966     struct mlxd_softc	*mlxd = bp->bio_disk->d_drv1;
1967 
1968     MLX_IO_ASSERT_LOCKED(sc);
1969     if (mc->mc_status != MLX_STATUS_OK) {	/* could be more verbose here? */
1970 	bp->bio_error = EIO;
1971 	bp->bio_flags |= BIO_ERROR;
1972 
1973 	switch(mc->mc_status) {
1974 	case MLX_STATUS_RDWROFFLINE:		/* system drive has gone offline */
1975 	    device_printf(mlxd->mlxd_dev, "drive offline\n");
1976 	    /* should signal this with a return code */
1977 	    mlxd->mlxd_drive->ms_state = MLX_SYSD_OFFLINE;
1978 	    break;
1979 
1980 	default:				/* other I/O error */
1981 	    device_printf(sc->mlx_dev, "I/O error - %s\n", mlx_diagnose_command(mc));
1982 #if 0
1983 	    device_printf(sc->mlx_dev, "  b_bcount %ld  blkcount %ld  b_pblkno %d\n",
1984 			  bp->bio_bcount, bp->bio_bcount / MLX_BLKSIZE, bp->bio_pblkno);
1985 	    device_printf(sc->mlx_dev, "  %13D\n", mc->mc_mailbox, " ");
1986 #endif
1987 	    break;
1988 	}
1989     }
1990     mlx_releasecmd(mc);
1991     mlxd_intr(bp);
1992 }
1993 
1994 void
1995 mlx_user_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1996 {
1997     struct mlx_usercommand *mu;
1998     struct mlx_command *mc;
1999     struct mlx_dcdb	*dcdb;
2000 
2001     mc = (struct mlx_command *)arg;
2002     if (error)
2003 	return;
2004 
2005     mlx_setup_dmamap(mc, segs, nsegments, error);
2006 
2007     mu = (struct mlx_usercommand *)mc->mc_private;
2008     dcdb = NULL;
2009 
2010     /*
2011      * If this is a passthrough SCSI command, the DCDB is packed at the
2012      * beginning of the data area.  Fix up the DCDB to point to the correct
2013      * physical address and override any bufptr supplied by the caller since
2014      * we know what it's meant to be.
2015      */
2016     if (mc->mc_mailbox[0] == MLX_CMD_DIRECT_CDB) {
2017 	dcdb = (struct mlx_dcdb *)mc->mc_data;
2018 	dcdb->dcdb_physaddr = mc->mc_dataphys + sizeof(*dcdb);
2019 	mu->mu_bufptr = 8;
2020     }
2021 
2022     /*
2023      * If there's a data buffer, fix up the command's buffer pointer.
2024      */
2025     if (mu->mu_datasize > 0) {
2026 	mc->mc_mailbox[mu->mu_bufptr    ] =  mc->mc_dataphys        & 0xff;
2027 	mc->mc_mailbox[mu->mu_bufptr + 1] = (mc->mc_dataphys >> 8)  & 0xff;
2028 	mc->mc_mailbox[mu->mu_bufptr + 2] = (mc->mc_dataphys >> 16) & 0xff;
2029 	mc->mc_mailbox[mu->mu_bufptr + 3] = (mc->mc_dataphys >> 24) & 0xff;
2030     }
2031     debug(0, "command fixup");
2032 
2033     /* submit the command and wait */
2034     if (mlx_wait_command(mc) != 0)
2035 	return;
2036 
2037 }
2038 
2039 /********************************************************************************
2040  * Take a command from user-space and try to run it.
2041  *
2042  * XXX Note that this can't perform very much in the way of error checking, and
2043  *     as such, applications _must_ be considered trustworthy.
2044  * XXX Commands using S/G for data are not supported.
2045  */
2046 static int
2047 mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu)
2048 {
2049     struct mlx_command	*mc;
2050     void		*kbuf;
2051     int			error;
2052 
2053     debug_called(0);
2054 
2055     kbuf = NULL;
2056     mc = NULL;
2057     error = ENOMEM;
2058 
2059     /* get ourselves a command and copy in from user space */
2060     MLX_IO_LOCK(sc);
2061     if ((mc = mlx_alloccmd(sc)) == NULL) {
2062 	MLX_IO_UNLOCK(sc);
2063 	return(error);
2064     }
2065     bcopy(mu->mu_command, mc->mc_mailbox, sizeof(mc->mc_mailbox));
2066     debug(0, "got command buffer");
2067 
2068     /*
2069      * if we need a buffer for data transfer, allocate one and copy in its
2070      * initial contents
2071      */
2072     if (mu->mu_datasize > 0) {
2073 	if (mu->mu_datasize > MLX_MAXPHYS) {
2074 	    error = EINVAL;
2075 	    goto out;
2076 	}
2077 	MLX_IO_UNLOCK(sc);
2078 	if (((kbuf = malloc(mu->mu_datasize, M_DEVBUF, M_WAITOK)) == NULL) ||
2079 	    (error = copyin(mu->mu_buf, kbuf, mu->mu_datasize))) {
2080 	    MLX_IO_LOCK(sc);
2081 	    goto out;
2082 	}
2083 	MLX_IO_LOCK(sc);
2084 	debug(0, "got kernel buffer");
2085     }
2086 
2087     /* get a command slot */
2088     if (mlx_getslot(mc))
2089 	goto out;
2090     debug(0, "got a slot");
2091 
2092     if (mu->mu_datasize > 0) {
2093 
2094 	/* range check the pointer to physical buffer address */
2095 	if ((mu->mu_bufptr < 0) || (mu->mu_bufptr > (sizeof(mu->mu_command) -
2096 						     sizeof(u_int32_t)))) {
2097 	    error = EINVAL;
2098 	    goto out;
2099 	}
2100     }
2101 
2102     /* map the command so the controller can see it */
2103     mc->mc_data = kbuf;
2104     mc->mc_length = mu->mu_datasize;
2105     mc->mc_private = mu;
2106     error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
2107 			    mc->mc_length, mlx_user_cb, mc, BUS_DMA_NOWAIT);
2108     if (error)
2109 	goto out;
2110 
2111     /* copy out status and data */
2112     mu->mu_status = mc->mc_status;
2113     if (mu->mu_datasize > 0) {
2114 	MLX_IO_UNLOCK(sc);
2115 	error = copyout(kbuf, mu->mu_buf, mu->mu_datasize);
2116 	MLX_IO_LOCK(sc);
2117     }
2118 
2119  out:
2120     mlx_releasecmd(mc);
2121     MLX_IO_UNLOCK(sc);
2122     if (kbuf != NULL)
2123 	free(kbuf, M_DEVBUF);
2124     return(error);
2125 }
2126 
2127 /********************************************************************************
2128  ********************************************************************************
2129                                                         Command I/O to Controller
2130  ********************************************************************************
2131  ********************************************************************************/
2132 
2133 /********************************************************************************
2134  * Find a free command slot for (mc).
2135  *
2136  * Don't hand out a slot to a normal-priority command unless there are at least
2137  * 4 slots free for priority commands.
2138  */
2139 static int
2140 mlx_getslot(struct mlx_command *mc)
2141 {
2142     struct mlx_softc	*sc = mc->mc_sc;
2143     int			slot, limit;
2144 
2145     debug_called(1);
2146 
2147     MLX_IO_ASSERT_LOCKED(sc);
2148 
2149     /*
2150      * Enforce slot-usage limit, if we have the required information.
2151      */
2152     if (sc->mlx_enq2 != NULL) {
2153 	limit = sc->mlx_enq2->me_max_commands;
2154     } else {
2155 	limit = 2;
2156     }
2157     if (sc->mlx_busycmds >= ((mc->mc_flags & MLX_CMD_PRIORITY) ? limit : limit - 4))
2158 	return(EBUSY);
2159 
2160     /*
2161      * Allocate an outstanding command slot
2162      *
2163      * XXX linear search is slow
2164      */
2165     for (slot = 0; slot < limit; slot++) {
2166 	debug(2, "try slot %d", slot);
2167 	if (sc->mlx_busycmd[slot] == NULL)
2168 	    break;
2169     }
2170     if (slot < limit) {
2171 	sc->mlx_busycmd[slot] = mc;
2172 	sc->mlx_busycmds++;
2173     }
2174 
2175     /* out of slots? */
2176     if (slot >= limit)
2177 	return(EBUSY);
2178 
2179     debug(2, "got slot %d", slot);
2180     mc->mc_slot = slot;
2181     return(0);
2182 }
2183 
2184 /********************************************************************************
2185  * Map/unmap (mc)'s data in the controller's addressable space.
2186  */
2187 static void
2188 mlx_setup_dmamap(struct mlx_command *mc, bus_dma_segment_t *segs, int nsegments,
2189 		 int error)
2190 {
2191     struct mlx_softc	*sc = mc->mc_sc;
2192     struct mlx_sgentry	*sg;
2193     int			i;
2194 
2195     debug_called(1);
2196 
2197     /* XXX should be unnecessary */
2198     if (sc->mlx_enq2 && (nsegments > sc->mlx_enq2->me_max_sg))
2199 	panic("MLX: too many s/g segments (%d, max %d)", nsegments,
2200 	      sc->mlx_enq2->me_max_sg);
2201 
2202     /* get base address of s/g table */
2203     sg = sc->mlx_sgtable + (mc->mc_slot * MLX_NSEG);
2204 
2205     /* save s/g table information in command */
2206     mc->mc_nsgent = nsegments;
2207     mc->mc_sgphys = sc->mlx_sgbusaddr +
2208 		   (mc->mc_slot * MLX_NSEG * sizeof(struct mlx_sgentry));
2209     mc->mc_dataphys = segs[0].ds_addr;
2210 
2211     /* populate s/g table */
2212     for (i = 0; i < nsegments; i++, sg++) {
2213 	sg->sg_addr = segs[i].ds_addr;
2214 	sg->sg_count = segs[i].ds_len;
2215     }
2216 
2217     /* Make sure the buffers are visible on the bus. */
2218     if (mc->mc_flags & MLX_CMD_DATAIN)
2219 	bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap,
2220 			BUS_DMASYNC_PREREAD);
2221     if (mc->mc_flags & MLX_CMD_DATAOUT)
2222 	bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap,
2223 			BUS_DMASYNC_PREWRITE);
2224 }
2225 
2226 static void
2227 mlx_unmapcmd(struct mlx_command *mc)
2228 {
2229     struct mlx_softc	*sc = mc->mc_sc;
2230 
2231     debug_called(1);
2232 
2233     /* if the command involved data at all */
2234     if (mc->mc_data != NULL) {
2235 
2236 	if (mc->mc_flags & MLX_CMD_DATAIN)
2237 	    bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTREAD);
2238 	if (mc->mc_flags & MLX_CMD_DATAOUT)
2239 	    bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTWRITE);
2240 
2241 	bus_dmamap_unload(sc->mlx_buffer_dmat, mc->mc_dmamap);
2242     }
2243 }
2244 
2245 /********************************************************************************
2246  * Try to deliver (mc) to the controller.
2247  *
2248  * Can be called at any interrupt level, with or without interrupts enabled.
2249  */
2250 static int
2251 mlx_start(struct mlx_command *mc)
2252 {
2253     struct mlx_softc	*sc = mc->mc_sc;
2254     int			i;
2255 
2256     debug_called(1);
2257 
2258     /* save the slot number as ident so we can handle this command when complete */
2259     mc->mc_mailbox[0x1] = mc->mc_slot;
2260 
2261     /* mark the command as currently being processed */
2262     mc->mc_status = MLX_STATUS_BUSY;
2263 
2264     /* set a default 60-second timeout  XXX tunable?  XXX not currently used */
2265     mc->mc_timeout = time_second + 60;
2266 
2267     /* spin waiting for the mailbox */
2268     for (i = 100000; i > 0; i--) {
2269 	if (sc->mlx_tryqueue(sc, mc)) {
2270 	    /* move command to work queue */
2271 	    TAILQ_INSERT_TAIL(&sc->mlx_work, mc, mc_link);
2272 	    return (0);
2273 	} else if (i > 1)
2274 	    mlx_done(sc, 0);
2275     }
2276 
2277     /*
2278      * We couldn't get the controller to take the command.  Revoke the slot
2279      * that the command was given and return it with a bad status.
2280      */
2281     sc->mlx_busycmd[mc->mc_slot] = NULL;
2282     device_printf(sc->mlx_dev, "controller wedged (not taking commands)\n");
2283     mc->mc_status = MLX_STATUS_WEDGED;
2284     mlx_complete(sc);
2285     return(EIO);
2286 }
2287 
2288 /********************************************************************************
2289  * Poll the controller (sc) for completed commands.
2290  * Update command status and free slots for reuse.  If any slots were freed,
2291  * new commands may be posted.
2292  *
2293  * Returns nonzero if one or more commands were completed.
2294  */
2295 static int
2296 mlx_done(struct mlx_softc *sc, int startio)
2297 {
2298     struct mlx_command	*mc;
2299     int			result;
2300     u_int8_t		slot;
2301     u_int16_t		status;
2302 
2303     debug_called(2);
2304     MLX_IO_ASSERT_LOCKED(sc);
2305 
2306     result = 0;
2307 
2308     /* loop collecting completed commands */
2309     for (;;) {
2310 	/* poll for a completed command's identifier and status */
2311 	if (sc->mlx_findcomplete(sc, &slot, &status)) {
2312 	    result = 1;
2313 	    mc = sc->mlx_busycmd[slot];			/* find command */
2314 	    if (mc != NULL) {				/* paranoia */
2315 		if (mc->mc_status == MLX_STATUS_BUSY) {
2316 		    mc->mc_status = status;		/* save status */
2317 
2318 		    /* free slot for reuse */
2319 		    sc->mlx_busycmd[slot] = NULL;
2320 		    sc->mlx_busycmds--;
2321 		} else {
2322 		    device_printf(sc->mlx_dev, "duplicate done event for slot %d\n", slot);
2323 		}
2324 	    } else {
2325 		device_printf(sc->mlx_dev, "done event for nonbusy slot %d\n", slot);
2326 	    }
2327 	} else {
2328 	    break;
2329 	}
2330     }
2331 
2332     /* if we've completed any commands, try posting some more */
2333     if (result && startio)
2334 	mlx_startio(sc);
2335 
2336     /* handle completion and timeouts */
2337     mlx_complete(sc);
2338 
2339     return(result);
2340 }
2341 
2342 /********************************************************************************
2343  * Perform post-completion processing for commands on (sc).
2344  */
2345 static void
2346 mlx_complete(struct mlx_softc *sc)
2347 {
2348     struct mlx_command	*mc, *nc;
2349 
2350     debug_called(2);
2351     MLX_IO_ASSERT_LOCKED(sc);
2352 
2353     /* scan the list of busy/done commands */
2354     mc = TAILQ_FIRST(&sc->mlx_work);
2355     while (mc != NULL) {
2356 	nc = TAILQ_NEXT(mc, mc_link);
2357 
2358 	/* Command has been completed in some fashion */
2359 	if (mc->mc_status != MLX_STATUS_BUSY) {
2360 
2361 	    /* unmap the command's data buffer */
2362 	    mlx_unmapcmd(mc);
2363 	    /*
2364 	     * Does the command have a completion handler?
2365 	     */
2366 	    if (mc->mc_complete != NULL) {
2367 		/* remove from list and give to handler */
2368 		TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2369 		mc->mc_complete(mc);
2370 
2371 		/*
2372 		 * Is there a sleeper waiting on this command?
2373 		 */
2374 	    } else if (mc->mc_private != NULL) {	/* sleeping caller wants to know about it */
2375 
2376 		/* remove from list and wake up sleeper */
2377 		TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2378 		wakeup_one(mc->mc_private);
2379 
2380 		/*
2381 		 * Leave the command for a caller that's polling for it.
2382 		 */
2383 	    } else {
2384 	    }
2385 	}
2386 	mc = nc;
2387     }
2388 }
2389 
2390 /********************************************************************************
2391  ********************************************************************************
2392                                                         Command Buffer Management
2393  ********************************************************************************
2394  ********************************************************************************/
2395 
2396 /********************************************************************************
2397  * Get a new command buffer.
2398  *
2399  * This may return NULL in low-memory cases.
2400  *
2401  * Note that using malloc() is expensive (the command buffer is << 1 page) but
2402  * necessary if we are to be a loadable module before the zone allocator is fixed.
2403  *
2404  * If possible, we recycle a command buffer that's been used before.
2405  *
2406  * XXX Note that command buffers are not cleaned out - it is the caller's
2407  *     responsibility to ensure that all required fields are filled in before
2408  *     using a buffer.
2409  */
2410 static struct mlx_command *
2411 mlx_alloccmd(struct mlx_softc *sc)
2412 {
2413     struct mlx_command	*mc;
2414     int			error;
2415 
2416     debug_called(1);
2417 
2418     MLX_IO_ASSERT_LOCKED(sc);
2419     if ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL)
2420 	TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
2421 
2422     /* allocate a new command buffer? */
2423     if (mc == NULL) {
2424 	mc = (struct mlx_command *)malloc(sizeof(*mc), M_DEVBUF, M_NOWAIT | M_ZERO);
2425 	if (mc != NULL) {
2426 	    mc->mc_sc = sc;
2427 	    error = bus_dmamap_create(sc->mlx_buffer_dmat, 0, &mc->mc_dmamap);
2428 	    if (error) {
2429 		free(mc, M_DEVBUF);
2430 		return(NULL);
2431 	    }
2432 	}
2433     }
2434     return(mc);
2435 }
2436 
2437 /********************************************************************************
2438  * Release a command buffer for recycling.
2439  *
2440  * XXX It might be a good idea to limit the number of commands we save for reuse
2441  *     if it's shown that this list bloats out massively.
2442  */
2443 static void
2444 mlx_releasecmd(struct mlx_command *mc)
2445 {
2446 
2447     debug_called(1);
2448 
2449     MLX_IO_ASSERT_LOCKED(mc->mc_sc);
2450     TAILQ_INSERT_HEAD(&mc->mc_sc->mlx_freecmds, mc, mc_link);
2451 }
2452 
2453 /********************************************************************************
2454  * Permanently discard a command buffer.
2455  */
2456 static void
2457 mlx_freecmd(struct mlx_command *mc)
2458 {
2459     struct mlx_softc	*sc = mc->mc_sc;
2460 
2461     debug_called(1);
2462     bus_dmamap_destroy(sc->mlx_buffer_dmat, mc->mc_dmamap);
2463     free(mc, M_DEVBUF);
2464 }
2465 
2466 
2467 /********************************************************************************
2468  ********************************************************************************
2469                                                 Type 3 interface accessor methods
2470  ********************************************************************************
2471  ********************************************************************************/
2472 
2473 /********************************************************************************
2474  * Try to give (mc) to the controller.  Returns 1 if successful, 0 on failure
2475  * (the controller is not ready to take a command).
2476  */
2477 static int
2478 mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2479 {
2480     int		i;
2481 
2482     debug_called(2);
2483     MLX_IO_ASSERT_LOCKED(sc);
2484 
2485     /* ready for our command? */
2486     if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_FULL)) {
2487 	/* copy mailbox data to window */
2488 	for (i = 0; i < 13; i++)
2489 	    MLX_V3_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2490 
2491 	/* post command */
2492 	MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_FULL);
2493 	return(1);
2494     }
2495     return(0);
2496 }
2497 
2498 /********************************************************************************
2499  * See if a command has been completed, if so acknowledge its completion
2500  * and recover the slot number and status code.
2501  */
2502 static int
2503 mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2504 {
2505 
2506     debug_called(2);
2507     MLX_IO_ASSERT_LOCKED(sc);
2508 
2509     /* status available? */
2510     if (MLX_V3_GET_ODBR(sc) & MLX_V3_ODB_SAVAIL) {
2511 	*slot = MLX_V3_GET_STATUS_IDENT(sc);		/* get command identifier */
2512 	*status = MLX_V3_GET_STATUS(sc);		/* get status */
2513 
2514 	/* acknowledge completion */
2515 	MLX_V3_PUT_ODBR(sc, MLX_V3_ODB_SAVAIL);
2516 	MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2517 	return(1);
2518     }
2519     return(0);
2520 }
2521 
2522 /********************************************************************************
2523  * Enable/disable interrupts as requested. (No acknowledge required)
2524  */
2525 static void
2526 mlx_v3_intaction(struct mlx_softc *sc, int action)
2527 {
2528     debug_called(1);
2529     MLX_IO_ASSERT_LOCKED(sc);
2530 
2531     switch(action) {
2532     case MLX_INTACTION_DISABLE:
2533 	MLX_V3_PUT_IER(sc, 0);
2534 	sc->mlx_state &= ~MLX_STATE_INTEN;
2535 	break;
2536     case MLX_INTACTION_ENABLE:
2537 	MLX_V3_PUT_IER(sc, 1);
2538 	sc->mlx_state |= MLX_STATE_INTEN;
2539 	break;
2540     }
2541 }
2542 
2543 /********************************************************************************
2544  * Poll for firmware error codes during controller initialisation.
2545  * Returns 0 if initialisation is complete, 1 if still in progress but no
2546  * error has been fetched, 2 if an error has been retrieved.
2547  */
2548 static int
2549 mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
2550     int first)
2551 {
2552     u_int8_t	fwerror;
2553 
2554     debug_called(2);
2555 
2556     /* first time around, clear any hardware completion status */
2557     if (first) {
2558 	MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2559 	DELAY(1000);
2560     }
2561 
2562     /* init in progress? */
2563     if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_INIT_BUSY))
2564 	return(0);
2565 
2566     /* test error value */
2567     fwerror = MLX_V3_GET_FWERROR(sc);
2568     if (!(fwerror & MLX_V3_FWERROR_PEND))
2569 	return(1);
2570 
2571     /* mask status pending bit, fetch status */
2572     *error = fwerror & ~MLX_V3_FWERROR_PEND;
2573     *param1 = MLX_V3_GET_FWERROR_PARAM1(sc);
2574     *param2 = MLX_V3_GET_FWERROR_PARAM2(sc);
2575 
2576     /* acknowledge */
2577     MLX_V3_PUT_FWERROR(sc, 0);
2578 
2579     return(2);
2580 }
2581 
2582 /********************************************************************************
2583  ********************************************************************************
2584                                                 Type 4 interface accessor methods
2585  ********************************************************************************
2586  ********************************************************************************/
2587 
2588 /********************************************************************************
2589  * Try to give (mc) to the controller.  Returns 1 if successful, 0 on failure
2590  * (the controller is not ready to take a command).
2591  */
2592 static int
2593 mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2594 {
2595     int		i;
2596 
2597     debug_called(2);
2598     MLX_IO_ASSERT_LOCKED(sc);
2599 
2600     /* ready for our command? */
2601     if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_FULL)) {
2602 	/* copy mailbox data to window */
2603 	for (i = 0; i < 13; i++)
2604 	    MLX_V4_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2605 
2606 	/* memory-mapped controller, so issue a write barrier to ensure the mailbox is filled */
2607 	bus_barrier(sc->mlx_mem, MLX_V4_MAILBOX, MLX_V4_MAILBOX_LENGTH,
2608 			  BUS_SPACE_BARRIER_WRITE);
2609 
2610 	/* post command */
2611 	MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_HWMBOX_CMD);
2612 	return(1);
2613     }
2614     return(0);
2615 }
2616 
2617 /********************************************************************************
2618  * See if a command has been completed, if so acknowledge its completion
2619  * and recover the slot number and status code.
2620  */
2621 static int
2622 mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2623 {
2624 
2625     debug_called(2);
2626     MLX_IO_ASSERT_LOCKED(sc);
2627 
2628     /* status available? */
2629     if (MLX_V4_GET_ODBR(sc) & MLX_V4_ODB_HWSAVAIL) {
2630 	*slot = MLX_V4_GET_STATUS_IDENT(sc);		/* get command identifier */
2631 	*status = MLX_V4_GET_STATUS(sc);		/* get status */
2632 
2633 	/* acknowledge completion */
2634 	MLX_V4_PUT_ODBR(sc, MLX_V4_ODB_HWMBOX_ACK);
2635 	MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2636 	return(1);
2637     }
2638     return(0);
2639 }
2640 
2641 /********************************************************************************
2642  * Enable/disable interrupts as requested.
2643  */
2644 static void
2645 mlx_v4_intaction(struct mlx_softc *sc, int action)
2646 {
2647     debug_called(1);
2648     MLX_IO_ASSERT_LOCKED(sc);
2649 
2650     switch(action) {
2651     case MLX_INTACTION_DISABLE:
2652 	MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK | MLX_V4_IER_DISINT);
2653 	sc->mlx_state &= ~MLX_STATE_INTEN;
2654 	break;
2655     case MLX_INTACTION_ENABLE:
2656 	MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK & ~MLX_V4_IER_DISINT);
2657 	sc->mlx_state |= MLX_STATE_INTEN;
2658 	break;
2659     }
2660 }
2661 
2662 /********************************************************************************
2663  * Poll for firmware error codes during controller initialisation.
2664  * Returns 0 if initialisation is complete, 1 if still in progress but no
2665  * error has been fetched, 2 if an error has been retrieved.
2666  */
2667 static int
2668 mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
2669     int first)
2670 {
2671     u_int8_t	fwerror;
2672 
2673     debug_called(2);
2674 
2675     /* first time around, clear any hardware completion status */
2676     if (first) {
2677 	MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2678 	DELAY(1000);
2679     }
2680 
2681     /* init in progress? */
2682     if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_INIT_BUSY))
2683 	return(0);
2684 
2685     /* test error value */
2686     fwerror = MLX_V4_GET_FWERROR(sc);
2687     if (!(fwerror & MLX_V4_FWERROR_PEND))
2688 	return(1);
2689 
2690     /* mask status pending bit, fetch status */
2691     *error = fwerror & ~MLX_V4_FWERROR_PEND;
2692     *param1 = MLX_V4_GET_FWERROR_PARAM1(sc);
2693     *param2 = MLX_V4_GET_FWERROR_PARAM2(sc);
2694 
2695     /* acknowledge */
2696     MLX_V4_PUT_FWERROR(sc, 0);
2697 
2698     return(2);
2699 }
2700 
2701 /********************************************************************************
2702  ********************************************************************************
2703                                                 Type 5 interface accessor methods
2704  ********************************************************************************
2705  ********************************************************************************/
2706 
2707 /********************************************************************************
2708  * Try to give (mc) to the controller.  Returns 1 if successful, 0 on failure
2709  * (the controller is not ready to take a command).
2710  */
2711 static int
2712 mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2713 {
2714     int		i;
2715 
2716     debug_called(2);
2717     MLX_IO_ASSERT_LOCKED(sc);
2718 
2719     /* ready for our command? */
2720     if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_EMPTY) {
2721 	/* copy mailbox data to window */
2722 	for (i = 0; i < 13; i++)
2723 	    MLX_V5_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2724 
2725 	/* post command */
2726 	MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_HWMBOX_CMD);
2727 	return(1);
2728     }
2729     return(0);
2730 }
2731 
2732 /********************************************************************************
2733  * See if a command has been completed, if so acknowledge its completion
2734  * and recover the slot number and status code.
2735  */
2736 static int
2737 mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2738 {
2739 
2740     debug_called(2);
2741     MLX_IO_ASSERT_LOCKED(sc);
2742 
2743     /* status available? */
2744     if (MLX_V5_GET_ODBR(sc) & MLX_V5_ODB_HWSAVAIL) {
2745 	*slot = MLX_V5_GET_STATUS_IDENT(sc);		/* get command identifier */
2746 	*status = MLX_V5_GET_STATUS(sc);		/* get status */
2747 
2748 	/* acknowledge completion */
2749 	MLX_V5_PUT_ODBR(sc, MLX_V5_ODB_HWMBOX_ACK);
2750 	MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2751 	return(1);
2752     }
2753     return(0);
2754 }
2755 
2756 /********************************************************************************
2757  * Enable/disable interrupts as requested.
2758  */
2759 static void
2760 mlx_v5_intaction(struct mlx_softc *sc, int action)
2761 {
2762     debug_called(1);
2763     MLX_IO_ASSERT_LOCKED(sc);
2764 
2765     switch(action) {
2766     case MLX_INTACTION_DISABLE:
2767 	MLX_V5_PUT_IER(sc, 0xff & MLX_V5_IER_DISINT);
2768 	sc->mlx_state &= ~MLX_STATE_INTEN;
2769 	break;
2770     case MLX_INTACTION_ENABLE:
2771 	MLX_V5_PUT_IER(sc, 0xff & ~MLX_V5_IER_DISINT);
2772 	sc->mlx_state |= MLX_STATE_INTEN;
2773 	break;
2774     }
2775 }
2776 
2777 /********************************************************************************
2778  * Poll for firmware error codes during controller initialisation.
2779  * Returns 0 if initialisation is complete, 1 if still in progress but no
2780  * error has been fetched, 2 if an error has been retrieved.
2781  */
2782 static int
2783 mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
2784     int first)
2785 {
2786     u_int8_t	fwerror;
2787 
2788     debug_called(2);
2789 
2790     /* first time around, clear any hardware completion status */
2791     if (first) {
2792 	MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2793 	DELAY(1000);
2794     }
2795 
2796     /* init in progress? */
2797     if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_INIT_DONE)
2798 	return(0);
2799 
2800     /* test for error value */
2801     fwerror = MLX_V5_GET_FWERROR(sc);
2802     if (!(fwerror & MLX_V5_FWERROR_PEND))
2803 	return(1);
2804 
2805     /* mask status pending bit, fetch status */
2806     *error = fwerror & ~MLX_V5_FWERROR_PEND;
2807     *param1 = MLX_V5_GET_FWERROR_PARAM1(sc);
2808     *param2 = MLX_V5_GET_FWERROR_PARAM2(sc);
2809 
2810     /* acknowledge */
2811     MLX_V5_PUT_FWERROR(sc, 0xff);
2812 
2813     return(2);
2814 }
2815 
2816 /********************************************************************************
2817  ********************************************************************************
2818                                                                         Debugging
2819  ********************************************************************************
2820  ********************************************************************************/
2821 
2822 /********************************************************************************
2823  * Return a status message describing (mc)
2824  */
2825 static char *mlx_status_messages[] = {
2826     "normal completion",			/* 00 */
2827     "irrecoverable data error",			/* 01 */
2828     "drive does not exist, or is offline",	/* 02 */
2829     "attempt to write beyond end of drive",	/* 03 */
2830     "bad data encountered",			/* 04 */
2831     "invalid log entry request",		/* 05 */
2832     "attempt to rebuild online drive",		/* 06 */
2833     "new disk failed during rebuild",		/* 07 */
2834     "invalid channel/target",			/* 08 */
2835     "rebuild/check already in progress",	/* 09 */
2836     "one or more disks are dead",		/* 10 */
2837     "invalid or non-redundant drive",		/* 11 */
2838     "channel is busy",				/* 12 */
2839     "channel is not stopped",			/* 13 */
2840     "rebuild successfully terminated",		/* 14 */
2841     "unsupported command",			/* 15 */
2842     "check condition received",			/* 16 */
2843     "device is busy",				/* 17 */
2844     "selection or command timeout",		/* 18 */
2845     "command terminated abnormally",		/* 19 */
2846     ""
2847 };
2848 
2849 static struct
2850 {
2851     int		command;
2852     u_int16_t	status;
2853     int		msg;
2854 } mlx_messages[] = {
2855     {MLX_CMD_READSG,		0x0001,	 1},
2856     {MLX_CMD_READSG,		0x0002,	 1},
2857     {MLX_CMD_READSG,		0x0105,	 3},
2858     {MLX_CMD_READSG,		0x010c,	 4},
2859     {MLX_CMD_WRITESG,		0x0001,	 1},
2860     {MLX_CMD_WRITESG,		0x0002,	 1},
2861     {MLX_CMD_WRITESG,		0x0105,	 3},
2862     {MLX_CMD_READSG_OLD,	0x0001,	 1},
2863     {MLX_CMD_READSG_OLD,	0x0002,	 1},
2864     {MLX_CMD_READSG_OLD,	0x0105,	 3},
2865     {MLX_CMD_WRITESG_OLD,	0x0001,	 1},
2866     {MLX_CMD_WRITESG_OLD,	0x0002,	 1},
2867     {MLX_CMD_WRITESG_OLD,	0x0105,	 3},
2868     {MLX_CMD_LOGOP,		0x0105,	 5},
2869     {MLX_CMD_REBUILDASYNC,	0x0002,  6},
2870     {MLX_CMD_REBUILDASYNC,	0x0004,  7},
2871     {MLX_CMD_REBUILDASYNC,	0x0105,  8},
2872     {MLX_CMD_REBUILDASYNC,	0x0106,  9},
2873     {MLX_CMD_REBUILDASYNC,	0x0107, 14},
2874     {MLX_CMD_CHECKASYNC,	0x0002, 10},
2875     {MLX_CMD_CHECKASYNC,	0x0105, 11},
2876     {MLX_CMD_CHECKASYNC,	0x0106,  9},
2877     {MLX_CMD_STOPCHANNEL,	0x0106, 12},
2878     {MLX_CMD_STOPCHANNEL,	0x0105,  8},
2879     {MLX_CMD_STARTCHANNEL,	0x0005, 13},
2880     {MLX_CMD_STARTCHANNEL,	0x0105,  8},
2881     {MLX_CMD_DIRECT_CDB,	0x0002, 16},
2882     {MLX_CMD_DIRECT_CDB,	0x0008, 17},
2883     {MLX_CMD_DIRECT_CDB,	0x000e, 18},
2884     {MLX_CMD_DIRECT_CDB,	0x000f, 19},
2885     {MLX_CMD_DIRECT_CDB,	0x0105,  8},
2886 
2887     {0,				0x0104, 14},
2888     {-1, 0, 0}
2889 };
2890 
2891 static char *
2892 mlx_diagnose_command(struct mlx_command *mc)
2893 {
2894     static char	unkmsg[80];
2895     int		i;
2896 
2897     /* look up message in table */
2898     for (i = 0; mlx_messages[i].command != -1; i++)
2899 	if (((mc->mc_mailbox[0] == mlx_messages[i].command) || (mlx_messages[i].command == 0)) &&
2900 	    (mc->mc_status == mlx_messages[i].status))
2901 	    return(mlx_status_messages[mlx_messages[i].msg]);
2902 
2903     sprintf(unkmsg, "unknown response 0x%x for command 0x%x", (int)mc->mc_status, (int)mc->mc_mailbox[0]);
2904     return(unkmsg);
2905 }
2906 
2907 /*******************************************************************************
2908  * Print a string describing the controller (sc)
2909  */
2910 static struct
2911 {
2912     int		hwid;
2913     char	*name;
2914 } mlx_controller_names[] = {
2915     {0x01,	"960P/PD"},
2916     {0x02,	"960PL"},
2917     {0x10,	"960PG"},
2918     {0x11,	"960PJ"},
2919     {0x12,	"960PR"},
2920     {0x13,	"960PT"},
2921     {0x14,	"960PTL0"},
2922     {0x15,	"960PRL"},
2923     {0x16,	"960PTL1"},
2924     {0x20,	"1164PVX"},
2925     {-1, NULL}
2926 };
2927 
2928 static void
2929 mlx_describe_controller(struct mlx_softc *sc)
2930 {
2931     static char		buf[80];
2932     char		*model;
2933     int			i;
2934 
2935     for (i = 0, model = NULL; mlx_controller_names[i].name != NULL; i++) {
2936 	if ((sc->mlx_enq2->me_hardware_id & 0xff) == mlx_controller_names[i].hwid) {
2937 	    model = mlx_controller_names[i].name;
2938 	    break;
2939 	}
2940     }
2941     if (model == NULL) {
2942 	sprintf(buf, " model 0x%x", sc->mlx_enq2->me_hardware_id & 0xff);
2943 	model = buf;
2944     }
2945     device_printf(sc->mlx_dev, "DAC%s, %d channel%s, firmware %d.%02d-%c-%02d, %dMB RAM\n",
2946 		  model,
2947 		  sc->mlx_enq2->me_actual_channels,
2948 		  sc->mlx_enq2->me_actual_channels > 1 ? "s" : "",
2949 		  sc->mlx_enq2->me_firmware_id & 0xff,
2950 		  (sc->mlx_enq2->me_firmware_id >> 8) & 0xff,
2951 		  (sc->mlx_enq2->me_firmware_id >> 24) & 0xff,
2952 		  (sc->mlx_enq2->me_firmware_id >> 16) & 0xff,
2953 		  sc->mlx_enq2->me_mem_size / (1024 * 1024));
2954 
2955     if (bootverbose) {
2956 	device_printf(sc->mlx_dev, "  Hardware ID                 0x%08x\n", sc->mlx_enq2->me_hardware_id);
2957 	device_printf(sc->mlx_dev, "  Firmware ID                 0x%08x\n", sc->mlx_enq2->me_firmware_id);
2958 	device_printf(sc->mlx_dev, "  Configured/Actual channels  %d/%d\n", sc->mlx_enq2->me_configured_channels,
2959 		      sc->mlx_enq2->me_actual_channels);
2960 	device_printf(sc->mlx_dev, "  Max Targets                 %d\n", sc->mlx_enq2->me_max_targets);
2961 	device_printf(sc->mlx_dev, "  Max Tags                    %d\n", sc->mlx_enq2->me_max_tags);
2962 	device_printf(sc->mlx_dev, "  Max System Drives           %d\n", sc->mlx_enq2->me_max_sys_drives);
2963 	device_printf(sc->mlx_dev, "  Max Arms                    %d\n", sc->mlx_enq2->me_max_arms);
2964 	device_printf(sc->mlx_dev, "  Max Spans                   %d\n", sc->mlx_enq2->me_max_spans);
2965 	device_printf(sc->mlx_dev, "  DRAM/cache/flash/NVRAM size %d/%d/%d/%d\n", sc->mlx_enq2->me_mem_size,
2966 		      sc->mlx_enq2->me_cache_size, sc->mlx_enq2->me_flash_size, sc->mlx_enq2->me_nvram_size);
2967 	device_printf(sc->mlx_dev, "  DRAM type                   %d\n", sc->mlx_enq2->me_mem_type);
2968 	device_printf(sc->mlx_dev, "  Clock Speed                 %dns\n", sc->mlx_enq2->me_clock_speed);
2969 	device_printf(sc->mlx_dev, "  Hardware Speed              %dns\n", sc->mlx_enq2->me_hardware_speed);
2970 	device_printf(sc->mlx_dev, "  Max Commands                %d\n", sc->mlx_enq2->me_max_commands);
2971 	device_printf(sc->mlx_dev, "  Max SG Entries              %d\n", sc->mlx_enq2->me_max_sg);
2972 	device_printf(sc->mlx_dev, "  Max DP                      %d\n", sc->mlx_enq2->me_max_dp);
2973 	device_printf(sc->mlx_dev, "  Max IOD                     %d\n", sc->mlx_enq2->me_max_iod);
2974 	device_printf(sc->mlx_dev, "  Max Comb                    %d\n", sc->mlx_enq2->me_max_comb);
2975 	device_printf(sc->mlx_dev, "  Latency                     %ds\n", sc->mlx_enq2->me_latency);
2976 	device_printf(sc->mlx_dev, "  SCSI Timeout                %ds\n", sc->mlx_enq2->me_scsi_timeout);
2977 	device_printf(sc->mlx_dev, "  Min Free Lines              %d\n", sc->mlx_enq2->me_min_freelines);
2978 	device_printf(sc->mlx_dev, "  Rate Constant               %d\n", sc->mlx_enq2->me_rate_const);
2979 	device_printf(sc->mlx_dev, "  MAXBLK                      %d\n", sc->mlx_enq2->me_maxblk);
2980 	device_printf(sc->mlx_dev, "  Blocking Factor             %d sectors\n", sc->mlx_enq2->me_blocking_factor);
2981 	device_printf(sc->mlx_dev, "  Cache Line Size             %d blocks\n", sc->mlx_enq2->me_cacheline);
2982 	device_printf(sc->mlx_dev, "  SCSI Capability             %s%dMHz, %d bit\n",
2983 		      sc->mlx_enq2->me_scsi_cap & (1<<4) ? "differential " : "",
2984 		      (1 << ((sc->mlx_enq2->me_scsi_cap >> 2) & 3)) * 10,
2985 		      8 << (sc->mlx_enq2->me_scsi_cap & 0x3));
2986 	device_printf(sc->mlx_dev, "  Firmware Build Number       %d\n", sc->mlx_enq2->me_firmware_build);
2987 	device_printf(sc->mlx_dev, "  Fault Management Type       %d\n", sc->mlx_enq2->me_fault_mgmt_type);
2988 	device_printf(sc->mlx_dev, "  Features                    %b\n", sc->mlx_enq2->me_firmware_features,
2989 		      "\20\4Background Init\3Read Ahead\2MORE\1Cluster\n");
2990 
2991     }
2992 }
2993 
2994 /*******************************************************************************
2995  * Emit a string describing the firmware handshake status code, and return a flag
2996  * indicating whether the code represents a fatal error.
2997  *
2998  * Error code interpretations are from the Linux driver, and don't directly match
2999  * the messages printed by Mylex's BIOS.  This may change if documentation on the
3000  * codes is forthcoming.
3001  */
3002 static int
3003 mlx_fw_message(struct mlx_softc *sc, int error, int param1, int param2)
3004 {
3005     switch(error) {
3006     case 0x00:
3007 	device_printf(sc->mlx_dev, "physical drive %d:%d not responding\n", param2, param1);
3008 	break;
3009     case 0x08:
3010 	/* we could be neater about this and give some indication when we receive more of them */
3011 	if (!(sc->mlx_flags & MLX_SPINUP_REPORTED)) {
3012 	    device_printf(sc->mlx_dev, "spinning up drives...\n");
3013 	    sc->mlx_flags |= MLX_SPINUP_REPORTED;
3014 	}
3015 	break;
3016     case 0x30:
3017 	device_printf(sc->mlx_dev, "configuration checksum error\n");
3018 	break;
3019     case 0x60:
3020 	device_printf(sc->mlx_dev, "mirror race recovery failed\n");
3021 	break;
3022     case 0x70:
3023 	device_printf(sc->mlx_dev, "mirror race recovery in progress\n");
3024 	break;
3025     case 0x90:
3026 	device_printf(sc->mlx_dev, "physical drive %d:%d COD mismatch\n", param2, param1);
3027 	break;
3028     case 0xa0:
3029 	device_printf(sc->mlx_dev, "logical drive installation aborted\n");
3030 	break;
3031     case 0xb0:
3032 	device_printf(sc->mlx_dev, "mirror race on a critical system drive\n");
3033 	break;
3034     case 0xd0:
3035 	device_printf(sc->mlx_dev, "new controller configuration found\n");
3036 	break;
3037     case 0xf0:
3038 	device_printf(sc->mlx_dev, "FATAL MEMORY PARITY ERROR\n");
3039 	return(1);
3040     default:
3041 	device_printf(sc->mlx_dev, "unknown firmware initialisation error %02x:%02x:%02x\n", error, param1, param2);
3042 	break;
3043     }
3044     return(0);
3045 }
3046 
3047 /********************************************************************************
3048  ********************************************************************************
3049                                                                 Utility Functions
3050  ********************************************************************************
3051  ********************************************************************************/
3052 
3053 /********************************************************************************
3054  * Find the disk whose unit number is (unit) on this controller
3055  */
3056 static struct mlx_sysdrive *
3057 mlx_findunit(struct mlx_softc *sc, int unit)
3058 {
3059     int		i;
3060 
3061     /* search system drives */
3062     MLX_CONFIG_ASSERT_LOCKED(sc);
3063     for (i = 0; i < MLX_MAXDRIVES; i++) {
3064 	/* is this one attached? */
3065 	if (sc->mlx_sysdrive[i].ms_disk != 0) {
3066 	    /* is this the one? */
3067 	    if (unit == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
3068 		return(&sc->mlx_sysdrive[i]);
3069 	}
3070     }
3071     return(NULL);
3072 }
3073