mly.c revision 126076
1/*-
2 * Copyright (c) 2000, 2001 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *	$FreeBSD: head/sys/dev/mly/mly.c 126076 2004-02-21 19:42:58Z phk $
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/malloc.h>
33#include <sys/kernel.h>
34#include <sys/bus.h>
35#include <sys/conf.h>
36#include <sys/ctype.h>
37#include <sys/ioccom.h>
38#include <sys/stat.h>
39
40#include <machine/bus_memio.h>
41#include <machine/bus.h>
42#include <machine/resource.h>
43#include <sys/rman.h>
44
45#include <cam/cam.h>
46#include <cam/cam_ccb.h>
47#include <cam/cam_periph.h>
48#include <cam/cam_sim.h>
49#include <cam/cam_xpt_sim.h>
50#include <cam/scsi/scsi_all.h>
51#include <cam/scsi/scsi_message.h>
52
53#include <dev/pci/pcireg.h>
54#include <dev/pci/pcivar.h>
55
56#include <dev/mly/mlyreg.h>
57#include <dev/mly/mlyio.h>
58#include <dev/mly/mlyvar.h>
59#include <dev/mly/mly_tables.h>
60
61static int	mly_probe(device_t dev);
62static int	mly_attach(device_t dev);
63static int	mly_pci_attach(struct mly_softc *sc);
64static int	mly_detach(device_t dev);
65static int	mly_shutdown(device_t dev);
66static void	mly_intr(void *arg);
67
68static int	mly_sg_map(struct mly_softc *sc);
69static void	mly_sg_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error);
70static int	mly_mmbox_map(struct mly_softc *sc);
71static void	mly_mmbox_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error);
72static void	mly_free(struct mly_softc *sc);
73
74static int	mly_get_controllerinfo(struct mly_softc *sc);
75static void	mly_scan_devices(struct mly_softc *sc);
76static void	mly_rescan_btl(struct mly_softc *sc, int bus, int target);
77static void	mly_complete_rescan(struct mly_command *mc);
78static int	mly_get_eventstatus(struct mly_softc *sc);
79static int	mly_enable_mmbox(struct mly_softc *sc);
80static int	mly_flush(struct mly_softc *sc);
81static int	mly_ioctl(struct mly_softc *sc, struct mly_command_ioctl *ioctl, void **data,
82			  size_t datasize, u_int8_t *status, void *sense_buffer, size_t *sense_length);
83static void	mly_check_event(struct mly_softc *sc);
84static void	mly_fetch_event(struct mly_softc *sc);
85static void	mly_complete_event(struct mly_command *mc);
86static void	mly_process_event(struct mly_softc *sc, struct mly_event *me);
87static void	mly_periodic(void *data);
88
89static int	mly_immediate_command(struct mly_command *mc);
90static int	mly_start(struct mly_command *mc);
91static void	mly_done(struct mly_softc *sc);
92static void	mly_complete(void *context, int pending);
93
94static int	mly_alloc_command(struct mly_softc *sc, struct mly_command **mcp);
95static void	mly_release_command(struct mly_command *mc);
96static void	mly_alloc_commands_map(void *arg, bus_dma_segment_t *segs, int nseg, int error);
97static int	mly_alloc_commands(struct mly_softc *sc);
98static void	mly_release_commands(struct mly_softc *sc);
99static void	mly_map_command(struct mly_command *mc);
100static void	mly_unmap_command(struct mly_command *mc);
101
102static int	mly_cam_attach(struct mly_softc *sc);
103static void	mly_cam_detach(struct mly_softc *sc);
104static void	mly_cam_rescan_btl(struct mly_softc *sc, int bus, int target);
105static void	mly_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb);
106static void	mly_cam_action(struct cam_sim *sim, union ccb *ccb);
107static int	mly_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
108static void	mly_cam_poll(struct cam_sim *sim);
109static void	mly_cam_complete(struct mly_command *mc);
110static struct cam_periph *mly_find_periph(struct mly_softc *sc, int bus, int target);
111static int	mly_name_device(struct mly_softc *sc, int bus, int target);
112
113static int	mly_fwhandshake(struct mly_softc *sc);
114
115static void	mly_describe_controller(struct mly_softc *sc);
116#ifdef MLY_DEBUG
117static void	mly_printstate(struct mly_softc *sc);
118static void	mly_print_command(struct mly_command *mc);
119static void	mly_print_packet(struct mly_command *mc);
120static void	mly_panic(struct mly_softc *sc, char *reason);
121#endif
122void		mly_print_controller(int controller);
123static int	mly_timeout(struct mly_softc *sc);
124
125
126static d_open_t		mly_user_open;
127static d_close_t	mly_user_close;
128static d_ioctl_t	mly_user_ioctl;
129static int	mly_user_command(struct mly_softc *sc, struct mly_user_command *uc);
130static int	mly_user_health(struct mly_softc *sc, struct mly_user_health *uh);
131
132#define MLY_CMD_TIMEOUT		20
133
134static device_method_t mly_methods[] = {
135    /* Device interface */
136    DEVMETHOD(device_probe,	mly_probe),
137    DEVMETHOD(device_attach,	mly_attach),
138    DEVMETHOD(device_detach,	mly_detach),
139    DEVMETHOD(device_shutdown,	mly_shutdown),
140    { 0, 0 }
141};
142
143static driver_t mly_pci_driver = {
144	"mly",
145	mly_methods,
146	sizeof(struct mly_softc)
147};
148
149static devclass_t	mly_devclass;
150DRIVER_MODULE(mly, pci, mly_pci_driver, mly_devclass, 0, 0);
151
152
153static struct cdevsw mly_cdevsw = {
154	.d_open =	mly_user_open,
155	.d_close =	mly_user_close,
156	.d_ioctl =	mly_user_ioctl,
157	.d_name =	"mly",
158};
159
160/********************************************************************************
161 ********************************************************************************
162                                                                 Device Interface
163 ********************************************************************************
164 ********************************************************************************/
165
166static struct mly_ident
167{
168    u_int16_t		vendor;
169    u_int16_t		device;
170    u_int16_t		subvendor;
171    u_int16_t		subdevice;
172    int			hwif;
173    char		*desc;
174} mly_identifiers[] = {
175    {0x1069, 0xba56, 0x1069, 0x0040, MLY_HWIF_STRONGARM, "Mylex eXtremeRAID 2000"},
176    {0x1069, 0xba56, 0x1069, 0x0030, MLY_HWIF_STRONGARM, "Mylex eXtremeRAID 3000"},
177    {0x1069, 0x0050, 0x1069, 0x0050, MLY_HWIF_I960RX,    "Mylex AcceleRAID 352"},
178    {0x1069, 0x0050, 0x1069, 0x0052, MLY_HWIF_I960RX,    "Mylex AcceleRAID 170"},
179    {0x1069, 0x0050, 0x1069, 0x0054, MLY_HWIF_I960RX,    "Mylex AcceleRAID 160"},
180    {0, 0, 0, 0, 0, 0}
181};
182
183/********************************************************************************
184 * Compare the provided PCI device with the list we support.
185 */
186static int
187mly_probe(device_t dev)
188{
189    struct mly_ident	*m;
190
191    debug_called(1);
192
193    for (m = mly_identifiers; m->vendor != 0; m++) {
194	if ((m->vendor == pci_get_vendor(dev)) &&
195	    (m->device == pci_get_device(dev)) &&
196	    ((m->subvendor == 0) || ((m->subvendor == pci_get_subvendor(dev)) &&
197				     (m->subdevice == pci_get_subdevice(dev))))) {
198
199	    device_set_desc(dev, m->desc);
200#ifdef MLY_MODULE
201	    return(-5);
202#else
203	    return(-10);	/* allow room to be overridden */
204#endif
205	}
206    }
207    return(ENXIO);
208}
209
210/********************************************************************************
211 * Initialise the controller and softc
212 */
213static int
214mly_attach(device_t dev)
215{
216    struct mly_softc	*sc = device_get_softc(dev);
217    int			error;
218
219    debug_called(1);
220
221    sc->mly_dev = dev;
222
223#ifdef MLY_DEBUG
224    if (device_get_unit(sc->mly_dev) == 0)
225	mly_softc0 = sc;
226#endif
227
228    /*
229     * Do PCI-specific initialisation.
230     */
231    if ((error = mly_pci_attach(sc)) != 0)
232	goto out;
233
234    /*
235     * Initialise per-controller queues.
236     */
237    mly_initq_free(sc);
238    mly_initq_busy(sc);
239    mly_initq_complete(sc);
240
241#if __FreeBSD_version >= 500005
242    /*
243     * Initialise command-completion task.
244     */
245    TASK_INIT(&sc->mly_task_complete, 0, mly_complete, sc);
246#endif
247
248    /* disable interrupts before we start talking to the controller */
249    MLY_MASK_INTERRUPTS(sc);
250
251    /*
252     * Wait for the controller to come ready, handshake with the firmware if required.
253     * This is typically only necessary on platforms where the controller BIOS does not
254     * run.
255     */
256    if ((error = mly_fwhandshake(sc)))
257	goto out;
258
259    /*
260     * Allocate initial command buffers.
261     */
262    if ((error = mly_alloc_commands(sc)))
263	goto out;
264
265    /*
266     * Obtain controller feature information
267     */
268    if ((error = mly_get_controllerinfo(sc)))
269	goto out;
270
271    /*
272     * Reallocate command buffers now we know how many we want.
273     */
274    mly_release_commands(sc);
275    if ((error = mly_alloc_commands(sc)))
276	goto out;
277
278    /*
279     * Get the current event counter for health purposes, populate the initial
280     * health status buffer.
281     */
282    if ((error = mly_get_eventstatus(sc)))
283	goto out;
284
285    /*
286     * Enable memory-mailbox mode.
287     */
288    if ((error = mly_enable_mmbox(sc)))
289	goto out;
290
291    /*
292     * Attach to CAM.
293     */
294    if ((error = mly_cam_attach(sc)))
295	goto out;
296
297    /*
298     * Print a little information about the controller
299     */
300    mly_describe_controller(sc);
301
302    /*
303     * Mark all attached devices for rescan.
304     */
305    mly_scan_devices(sc);
306
307    /*
308     * Instigate the first status poll immediately.  Rescan completions won't
309     * happen until interrupts are enabled, which should still be before
310     * the SCSI subsystem gets to us, courtesy of the "SCSI settling delay".
311     */
312    mly_periodic((void *)sc);
313
314    /*
315     * Create the control device.
316     */
317    sc->mly_dev_t = make_dev(&mly_cdevsw, device_get_unit(sc->mly_dev), UID_ROOT, GID_OPERATOR,
318			     S_IRUSR | S_IWUSR, "mly%d", device_get_unit(sc->mly_dev));
319    sc->mly_dev_t->si_drv1 = sc;
320
321    /* enable interrupts now */
322    MLY_UNMASK_INTERRUPTS(sc);
323
324#ifdef MLY_DEBUG
325    timeout((timeout_t *)mly_timeout, sc, MLY_CMD_TIMEOUT * hz);
326#endif
327
328 out:
329    if (error != 0)
330	mly_free(sc);
331    return(error);
332}
333
334/********************************************************************************
335 * Perform PCI-specific initialisation.
336 */
337static int
338mly_pci_attach(struct mly_softc *sc)
339{
340    int			i, error;
341    u_int32_t		command;
342
343    debug_called(1);
344
345    /* assume failure is 'not configured' */
346    error = ENXIO;
347
348    /*
349     * Verify that the adapter is correctly set up in PCI space.
350     *
351     * XXX we shouldn't do this; the PCI code should.
352     */
353    command = pci_read_config(sc->mly_dev, PCIR_COMMAND, 2);
354    command |= PCIM_CMD_BUSMASTEREN;
355    pci_write_config(sc->mly_dev, PCIR_COMMAND, command, 2);
356    command = pci_read_config(sc->mly_dev, PCIR_COMMAND, 2);
357    if (!(command & PCIM_CMD_BUSMASTEREN)) {
358	mly_printf(sc, "can't enable busmaster feature\n");
359	goto fail;
360    }
361    if ((command & PCIM_CMD_MEMEN) == 0) {
362	mly_printf(sc, "memory window not available\n");
363	goto fail;
364    }
365
366    /*
367     * Allocate the PCI register window.
368     */
369    sc->mly_regs_rid = PCIR_BAR(0);	/* first base address register */
370    if ((sc->mly_regs_resource = bus_alloc_resource(sc->mly_dev, SYS_RES_MEMORY, &sc->mly_regs_rid,
371						    0, ~0, 1, RF_ACTIVE)) == NULL) {
372	mly_printf(sc, "can't allocate register window\n");
373	goto fail;
374    }
375    sc->mly_btag = rman_get_bustag(sc->mly_regs_resource);
376    sc->mly_bhandle = rman_get_bushandle(sc->mly_regs_resource);
377
378    /*
379     * Allocate and connect our interrupt.
380     */
381    sc->mly_irq_rid = 0;
382    if ((sc->mly_irq = bus_alloc_resource(sc->mly_dev, SYS_RES_IRQ, &sc->mly_irq_rid,
383					  0, ~0, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
384	mly_printf(sc, "can't allocate interrupt\n");
385	goto fail;
386    }
387    if (bus_setup_intr(sc->mly_dev, sc->mly_irq, INTR_TYPE_CAM | INTR_ENTROPY,  mly_intr, sc, &sc->mly_intr)) {
388	mly_printf(sc, "can't set up interrupt\n");
389	goto fail;
390    }
391
392    /* assume failure is 'out of memory' */
393    error = ENOMEM;
394
395    /*
396     * Allocate the parent bus DMA tag appropriate for our PCI interface.
397     *
398     * Note that all of these controllers are 64-bit capable.
399     */
400    if (bus_dma_tag_create(NULL, 			/* parent */
401			   1, 0, 			/* alignment, boundary */
402			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
403			   BUS_SPACE_MAXADDR, 		/* highaddr */
404			   NULL, NULL, 			/* filter, filterarg */
405			   MAXBSIZE, MLY_MAX_SGENTRIES,	/* maxsize, nsegments */
406			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
407			   BUS_DMA_ALLOCNOW,		/* flags */
408			   NULL,			/* lockfunc */
409			   NULL,			/* lockarg */
410			   &sc->mly_parent_dmat)) {
411	mly_printf(sc, "can't allocate parent DMA tag\n");
412	goto fail;
413    }
414
415    /*
416     * Create DMA tag for mapping buffers into controller-addressable space.
417     */
418    if (bus_dma_tag_create(sc->mly_parent_dmat, 	/* parent */
419			   1, 0, 			/* alignment, boundary */
420			   BUS_SPACE_MAXADDR,		/* lowaddr */
421			   BUS_SPACE_MAXADDR, 		/* highaddr */
422			   NULL, NULL, 			/* filter, filterarg */
423			   MAXBSIZE, MLY_MAX_SGENTRIES,	/* maxsize, nsegments */
424			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
425			   0,				/* flags */
426			   busdma_lock_mutex,		/* lockfunc */
427			   &Giant,			/* lockarg */
428			   &sc->mly_buffer_dmat)) {
429	mly_printf(sc, "can't allocate buffer DMA tag\n");
430	goto fail;
431    }
432
433    /*
434     * Initialise the DMA tag for command packets.
435     */
436    if (bus_dma_tag_create(sc->mly_parent_dmat,		/* parent */
437			   1, 0, 			/* alignment, boundary */
438			   BUS_SPACE_MAXADDR,		/* lowaddr */
439			   BUS_SPACE_MAXADDR, 		/* highaddr */
440			   NULL, NULL, 			/* filter, filterarg */
441			   sizeof(union mly_command_packet) * MLY_MAX_COMMANDS, 1,	/* maxsize, nsegments */
442			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
443			   BUS_DMA_ALLOCNOW,		/* flags */
444			   NULL, NULL,			/* lockfunc, lockarg */
445			   &sc->mly_packet_dmat)) {
446	mly_printf(sc, "can't allocate command packet DMA tag\n");
447	goto fail;
448    }
449
450    /*
451     * Detect the hardware interface version
452     */
453    for (i = 0; mly_identifiers[i].vendor != 0; i++) {
454	if ((mly_identifiers[i].vendor == pci_get_vendor(sc->mly_dev)) &&
455	    (mly_identifiers[i].device == pci_get_device(sc->mly_dev))) {
456	    sc->mly_hwif = mly_identifiers[i].hwif;
457	    switch(sc->mly_hwif) {
458	    case MLY_HWIF_I960RX:
459		debug(1, "set hardware up for i960RX");
460		sc->mly_doorbell_true = 0x00;
461		sc->mly_command_mailbox =  MLY_I960RX_COMMAND_MAILBOX;
462		sc->mly_status_mailbox =   MLY_I960RX_STATUS_MAILBOX;
463		sc->mly_idbr =             MLY_I960RX_IDBR;
464		sc->mly_odbr =             MLY_I960RX_ODBR;
465		sc->mly_error_status =     MLY_I960RX_ERROR_STATUS;
466		sc->mly_interrupt_status = MLY_I960RX_INTERRUPT_STATUS;
467		sc->mly_interrupt_mask =   MLY_I960RX_INTERRUPT_MASK;
468		break;
469	    case MLY_HWIF_STRONGARM:
470		debug(1, "set hardware up for StrongARM");
471		sc->mly_doorbell_true = 0xff;		/* doorbell 'true' is 0 */
472		sc->mly_command_mailbox =  MLY_STRONGARM_COMMAND_MAILBOX;
473		sc->mly_status_mailbox =   MLY_STRONGARM_STATUS_MAILBOX;
474		sc->mly_idbr =             MLY_STRONGARM_IDBR;
475		sc->mly_odbr =             MLY_STRONGARM_ODBR;
476		sc->mly_error_status =     MLY_STRONGARM_ERROR_STATUS;
477		sc->mly_interrupt_status = MLY_STRONGARM_INTERRUPT_STATUS;
478		sc->mly_interrupt_mask =   MLY_STRONGARM_INTERRUPT_MASK;
479		break;
480	    }
481	    break;
482	}
483    }
484
485    /*
486     * Create the scatter/gather mappings.
487     */
488    if ((error = mly_sg_map(sc)))
489	goto fail;
490
491    /*
492     * Allocate and map the memory mailbox
493     */
494    if ((error = mly_mmbox_map(sc)))
495	goto fail;
496
497    error = 0;
498
499fail:
500    return(error);
501}
502
503/********************************************************************************
504 * Shut the controller down and detach all our resources.
505 */
506static int
507mly_detach(device_t dev)
508{
509    int			error;
510
511    if ((error = mly_shutdown(dev)) != 0)
512	return(error);
513
514    mly_free(device_get_softc(dev));
515    return(0);
516}
517
518/********************************************************************************
519 * Bring the controller to a state where it can be safely left alone.
520 *
521 * Note that it should not be necessary to wait for any outstanding commands,
522 * as they should be completed prior to calling here.
523 *
524 * XXX this applies for I/O, but not status polls; we should beware of
525 *     the case where a status command is running while we detach.
526 */
527static int
528mly_shutdown(device_t dev)
529{
530    struct mly_softc	*sc = device_get_softc(dev);
531
532    debug_called(1);
533
534    if (sc->mly_state & MLY_STATE_OPEN)
535	return(EBUSY);
536
537    /* kill the periodic event */
538    untimeout(mly_periodic, sc, sc->mly_periodic);
539
540    /* flush controller */
541    mly_printf(sc, "flushing cache...");
542    printf("%s\n", mly_flush(sc) ? "failed" : "done");
543
544    MLY_MASK_INTERRUPTS(sc);
545
546    return(0);
547}
548
549/*******************************************************************************
550 * Take an interrupt, or be poked by other code to look for interrupt-worthy
551 * status.
552 */
553static void
554mly_intr(void *arg)
555{
556    struct mly_softc	*sc = (struct mly_softc *)arg;
557
558    debug_called(2);
559
560    mly_done(sc);
561};
562
563/********************************************************************************
564 ********************************************************************************
565                                                Bus-dependant Resource Management
566 ********************************************************************************
567 ********************************************************************************/
568
569/********************************************************************************
570 * Allocate memory for the scatter/gather tables
571 */
572static int
573mly_sg_map(struct mly_softc *sc)
574{
575    size_t	segsize;
576
577    debug_called(1);
578
579    /*
580     * Create a single tag describing a region large enough to hold all of
581     * the s/g lists we will need.
582     */
583    segsize = sizeof(struct mly_sg_entry) * MLY_MAX_COMMANDS *MLY_MAX_SGENTRIES;
584    if (bus_dma_tag_create(sc->mly_parent_dmat,		/* parent */
585			   1, 0, 			/* alignment,boundary */
586			   BUS_SPACE_MAXADDR,		/* lowaddr */
587			   BUS_SPACE_MAXADDR, 		/* highaddr */
588			   NULL, NULL, 			/* filter, filterarg */
589			   segsize, 1,			/* maxsize, nsegments */
590			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
591			   BUS_DMA_ALLOCNOW,		/* flags */
592			   NULL, NULL,			/* lockfunc, lockarg */
593			   &sc->mly_sg_dmat)) {
594	mly_printf(sc, "can't allocate scatter/gather DMA tag\n");
595	return(ENOMEM);
596    }
597
598    /*
599     * Allocate enough s/g maps for all commands and permanently map them into
600     * controller-visible space.
601     *
602     * XXX this assumes we can get enough space for all the s/g maps in one
603     * contiguous slab.
604     */
605    if (bus_dmamem_alloc(sc->mly_sg_dmat, (void **)&sc->mly_sg_table,
606			 BUS_DMA_NOWAIT, &sc->mly_sg_dmamap)) {
607	mly_printf(sc, "can't allocate s/g table\n");
608	return(ENOMEM);
609    }
610    if (bus_dmamap_load(sc->mly_sg_dmat, sc->mly_sg_dmamap, sc->mly_sg_table,
611			segsize, mly_sg_map_helper, sc, BUS_DMA_NOWAIT) != 0)
612	return (ENOMEM);
613    return(0);
614}
615
616/********************************************************************************
617 * Save the physical address of the base of the s/g table.
618 */
619static void
620mly_sg_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
621{
622    struct mly_softc	*sc = (struct mly_softc *)arg;
623
624    debug_called(1);
625
626    /* save base of s/g table's address in bus space */
627    sc->mly_sg_busaddr = segs->ds_addr;
628}
629
630/********************************************************************************
631 * Allocate memory for the memory-mailbox interface
632 */
633static int
634mly_mmbox_map(struct mly_softc *sc)
635{
636
637    /*
638     * Create a DMA tag for a single contiguous region large enough for the
639     * memory mailbox structure.
640     */
641    if (bus_dma_tag_create(sc->mly_parent_dmat,		/* parent */
642			   1, 0, 			/* alignment,boundary */
643			   BUS_SPACE_MAXADDR,		/* lowaddr */
644			   BUS_SPACE_MAXADDR, 		/* highaddr */
645			   NULL, NULL, 			/* filter, filterarg */
646			   sizeof(struct mly_mmbox), 1,	/* maxsize, nsegments */
647			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
648			   BUS_DMA_ALLOCNOW,		/* flags */
649			   NULL, NULL,			/* lockfunc, lockarg */
650			   &sc->mly_mmbox_dmat)) {
651	mly_printf(sc, "can't allocate memory mailbox DMA tag\n");
652	return(ENOMEM);
653    }
654
655    /*
656     * Allocate the buffer
657     */
658    if (bus_dmamem_alloc(sc->mly_mmbox_dmat, (void **)&sc->mly_mmbox, BUS_DMA_NOWAIT, &sc->mly_mmbox_dmamap)) {
659	mly_printf(sc, "can't allocate memory mailbox\n");
660	return(ENOMEM);
661    }
662    if (bus_dmamap_load(sc->mly_mmbox_dmat, sc->mly_mmbox_dmamap, sc->mly_mmbox,
663			sizeof(struct mly_mmbox), mly_mmbox_map_helper, sc,
664			BUS_DMA_NOWAIT) != 0)
665	return (ENOMEM);
666    bzero(sc->mly_mmbox, sizeof(*sc->mly_mmbox));
667    return(0);
668
669}
670
671/********************************************************************************
672 * Save the physical address of the memory mailbox
673 */
674static void
675mly_mmbox_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
676{
677    struct mly_softc	*sc = (struct mly_softc *)arg;
678
679    debug_called(1);
680
681    sc->mly_mmbox_busaddr = segs->ds_addr;
682}
683
684/********************************************************************************
685 * Free all of the resources associated with (sc)
686 *
687 * Should not be called if the controller is active.
688 */
689static void
690mly_free(struct mly_softc *sc)
691{
692
693    debug_called(1);
694
695    /* Remove the management device */
696    destroy_dev(sc->mly_dev_t);
697
698    /* detach from CAM */
699    mly_cam_detach(sc);
700
701    /* release command memory */
702    mly_release_commands(sc);
703
704    /* throw away the controllerinfo structure */
705    if (sc->mly_controllerinfo != NULL)
706	free(sc->mly_controllerinfo, M_DEVBUF);
707
708    /* throw away the controllerparam structure */
709    if (sc->mly_controllerparam != NULL)
710	free(sc->mly_controllerparam, M_DEVBUF);
711
712    /* destroy data-transfer DMA tag */
713    if (sc->mly_buffer_dmat)
714	bus_dma_tag_destroy(sc->mly_buffer_dmat);
715
716    /* free and destroy DMA memory and tag for s/g lists */
717    if (sc->mly_sg_table) {
718	bus_dmamap_unload(sc->mly_sg_dmat, sc->mly_sg_dmamap);
719	bus_dmamem_free(sc->mly_sg_dmat, sc->mly_sg_table, sc->mly_sg_dmamap);
720    }
721    if (sc->mly_sg_dmat)
722	bus_dma_tag_destroy(sc->mly_sg_dmat);
723
724    /* free and destroy DMA memory and tag for memory mailbox */
725    if (sc->mly_mmbox) {
726	bus_dmamap_unload(sc->mly_mmbox_dmat, sc->mly_mmbox_dmamap);
727	bus_dmamem_free(sc->mly_mmbox_dmat, sc->mly_mmbox, sc->mly_mmbox_dmamap);
728    }
729    if (sc->mly_mmbox_dmat)
730	bus_dma_tag_destroy(sc->mly_mmbox_dmat);
731
732    /* disconnect the interrupt handler */
733    if (sc->mly_intr)
734	bus_teardown_intr(sc->mly_dev, sc->mly_irq, sc->mly_intr);
735    if (sc->mly_irq != NULL)
736	bus_release_resource(sc->mly_dev, SYS_RES_IRQ, sc->mly_irq_rid, sc->mly_irq);
737
738    /* destroy the parent DMA tag */
739    if (sc->mly_parent_dmat)
740	bus_dma_tag_destroy(sc->mly_parent_dmat);
741
742    /* release the register window mapping */
743    if (sc->mly_regs_resource != NULL)
744	bus_release_resource(sc->mly_dev, SYS_RES_MEMORY, sc->mly_regs_rid, sc->mly_regs_resource);
745}
746
747/********************************************************************************
748 ********************************************************************************
749                                                                 Command Wrappers
750 ********************************************************************************
751 ********************************************************************************/
752
753/********************************************************************************
754 * Fill in the mly_controllerinfo and mly_controllerparam fields in the softc.
755 */
756static int
757mly_get_controllerinfo(struct mly_softc *sc)
758{
759    struct mly_command_ioctl	mci;
760    u_int8_t			status;
761    int				error;
762
763    debug_called(1);
764
765    if (sc->mly_controllerinfo != NULL)
766	free(sc->mly_controllerinfo, M_DEVBUF);
767
768    /* build the getcontrollerinfo ioctl and send it */
769    bzero(&mci, sizeof(mci));
770    sc->mly_controllerinfo = NULL;
771    mci.sub_ioctl = MDACIOCTL_GETCONTROLLERINFO;
772    if ((error = mly_ioctl(sc, &mci, (void **)&sc->mly_controllerinfo, sizeof(*sc->mly_controllerinfo),
773			   &status, NULL, NULL)))
774	return(error);
775    if (status != 0)
776	return(EIO);
777
778    if (sc->mly_controllerparam != NULL)
779	free(sc->mly_controllerparam, M_DEVBUF);
780
781    /* build the getcontrollerparameter ioctl and send it */
782    bzero(&mci, sizeof(mci));
783    sc->mly_controllerparam = NULL;
784    mci.sub_ioctl = MDACIOCTL_GETCONTROLLERPARAMETER;
785    if ((error = mly_ioctl(sc, &mci, (void **)&sc->mly_controllerparam, sizeof(*sc->mly_controllerparam),
786			   &status, NULL, NULL)))
787	return(error);
788    if (status != 0)
789	return(EIO);
790
791    return(0);
792}
793
794/********************************************************************************
795 * Schedule all possible devices for a rescan.
796 *
797 */
798static void
799mly_scan_devices(struct mly_softc *sc)
800{
801    int		bus, target;
802
803    debug_called(1);
804
805    /*
806     * Clear any previous BTL information.
807     */
808    bzero(&sc->mly_btl, sizeof(sc->mly_btl));
809
810    /*
811     * Mark all devices as requiring a rescan, and let the next
812     * periodic scan collect them.
813     */
814    for (bus = 0; bus < sc->mly_cam_channels; bus++)
815	if (MLY_BUS_IS_VALID(sc, bus))
816	    for (target = 0; target < MLY_MAX_TARGETS; target++)
817		sc->mly_btl[bus][target].mb_flags = MLY_BTL_RESCAN;
818
819}
820
821/********************************************************************************
822 * Rescan a device, possibly as a consequence of getting an event which suggests
823 * that it may have changed.
824 *
825 * If we suffer resource starvation, we can abandon the rescan as we'll be
826 * retried.
827 */
828static void
829mly_rescan_btl(struct mly_softc *sc, int bus, int target)
830{
831    struct mly_command		*mc;
832    struct mly_command_ioctl	*mci;
833
834    debug_called(1);
835
836    /* check that this bus is valid */
837    if (!MLY_BUS_IS_VALID(sc, bus))
838	return;
839
840    /* get a command */
841    if (mly_alloc_command(sc, &mc))
842	return;
843
844    /* set up the data buffer */
845    if ((mc->mc_data = malloc(sizeof(union mly_devinfo), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) {
846	mly_release_command(mc);
847	return;
848    }
849    mc->mc_flags |= MLY_CMD_DATAIN;
850    mc->mc_complete = mly_complete_rescan;
851
852    /*
853     * Build the ioctl.
854     */
855    mci = (struct mly_command_ioctl *)&mc->mc_packet->ioctl;
856    mci->opcode = MDACMD_IOCTL;
857    mci->addr.phys.controller = 0;
858    mci->timeout.value = 30;
859    mci->timeout.scale = MLY_TIMEOUT_SECONDS;
860    if (MLY_BUS_IS_VIRTUAL(sc, bus)) {
861	mc->mc_length = mci->data_size = sizeof(struct mly_ioctl_getlogdevinfovalid);
862	mci->sub_ioctl = MDACIOCTL_GETLOGDEVINFOVALID;
863	mci->addr.log.logdev = MLY_LOGDEV_ID(sc, bus, target);
864	debug(1, "logical device %d", mci->addr.log.logdev);
865    } else {
866	mc->mc_length = mci->data_size = sizeof(struct mly_ioctl_getphysdevinfovalid);
867	mci->sub_ioctl = MDACIOCTL_GETPHYSDEVINFOVALID;
868	mci->addr.phys.lun = 0;
869	mci->addr.phys.target = target;
870	mci->addr.phys.channel = bus;
871	debug(1, "physical device %d:%d", mci->addr.phys.channel, mci->addr.phys.target);
872    }
873
874    /*
875     * Dispatch the command.  If we successfully send the command, clear the rescan
876     * bit.
877     */
878    if (mly_start(mc) != 0) {
879	mly_release_command(mc);
880    } else {
881	sc->mly_btl[bus][target].mb_flags &= ~MLY_BTL_RESCAN;	/* success */
882    }
883}
884
885/********************************************************************************
886 * Handle the completion of a rescan operation
887 */
888static void
889mly_complete_rescan(struct mly_command *mc)
890{
891    struct mly_softc				*sc = mc->mc_sc;
892    struct mly_ioctl_getlogdevinfovalid		*ldi;
893    struct mly_ioctl_getphysdevinfovalid	*pdi;
894    struct mly_command_ioctl			*mci;
895    struct mly_btl				btl, *btlp;
896    int						bus, target, rescan;
897
898    debug_called(1);
899
900    /*
901     * Recover the bus and target from the command.  We need these even in
902     * the case where we don't have a useful response.
903     */
904    mci = (struct mly_command_ioctl *)&mc->mc_packet->ioctl;
905    if (mci->sub_ioctl == MDACIOCTL_GETLOGDEVINFOVALID) {
906	bus = MLY_LOGDEV_BUS(sc, mci->addr.log.logdev);
907	target = MLY_LOGDEV_TARGET(sc, mci->addr.log.logdev);
908    } else {
909	bus = mci->addr.phys.channel;
910	target = mci->addr.phys.target;
911    }
912    /* XXX validate bus/target? */
913
914    /* the default result is 'no device' */
915    bzero(&btl, sizeof(btl));
916
917    /* if the rescan completed OK, we have possibly-new BTL data */
918    if (mc->mc_status == 0) {
919	if (mc->mc_length == sizeof(*ldi)) {
920	    ldi = (struct mly_ioctl_getlogdevinfovalid *)mc->mc_data;
921	    if ((MLY_LOGDEV_BUS(sc, ldi->logical_device_number) != bus) ||
922		(MLY_LOGDEV_TARGET(sc, ldi->logical_device_number) != target)) {
923		mly_printf(sc, "WARNING: BTL rescan for %d:%d returned data for %d:%d instead\n",
924			   bus, target, MLY_LOGDEV_BUS(sc, ldi->logical_device_number),
925			   MLY_LOGDEV_TARGET(sc, ldi->logical_device_number));
926		/* XXX what can we do about this? */
927	    }
928	    btl.mb_flags = MLY_BTL_LOGICAL;
929	    btl.mb_type = ldi->raid_level;
930	    btl.mb_state = ldi->state;
931	    debug(1, "BTL rescan for %d returns %s, %s", ldi->logical_device_number,
932		  mly_describe_code(mly_table_device_type, ldi->raid_level),
933		  mly_describe_code(mly_table_device_state, ldi->state));
934	} else if (mc->mc_length == sizeof(*pdi)) {
935	    pdi = (struct mly_ioctl_getphysdevinfovalid *)mc->mc_data;
936	    if ((pdi->channel != bus) || (pdi->target != target)) {
937		mly_printf(sc, "WARNING: BTL rescan for %d:%d returned data for %d:%d instead\n",
938			   bus, target, pdi->channel, pdi->target);
939		/* XXX what can we do about this? */
940	    }
941	    btl.mb_flags = MLY_BTL_PHYSICAL;
942	    btl.mb_type = MLY_DEVICE_TYPE_PHYSICAL;
943	    btl.mb_state = pdi->state;
944	    btl.mb_speed = pdi->speed;
945	    btl.mb_width = pdi->width;
946	    if (pdi->state != MLY_DEVICE_STATE_UNCONFIGURED)
947		sc->mly_btl[bus][target].mb_flags |= MLY_BTL_PROTECTED;
948	    debug(1, "BTL rescan for %d:%d returns %s", bus, target,
949		  mly_describe_code(mly_table_device_state, pdi->state));
950	} else {
951	    mly_printf(sc, "BTL rescan result invalid\n");
952	}
953    }
954
955    free(mc->mc_data, M_DEVBUF);
956    mly_release_command(mc);
957
958    /*
959     * Decide whether we need to rescan the device.
960     */
961    rescan = 0;
962
963    /* device type changes (usually between 'nothing' and 'something') */
964    btlp = &sc->mly_btl[bus][target];
965    if (btl.mb_flags != btlp->mb_flags) {
966	debug(1, "flags changed, rescanning");
967	rescan = 1;
968    }
969
970    /* XXX other reasons? */
971
972    /*
973     * Update BTL information.
974     */
975    *btlp = btl;
976
977    /*
978     * Perform CAM rescan if required.
979     */
980    if (rescan)
981	mly_cam_rescan_btl(sc, bus, target);
982}
983
984/********************************************************************************
985 * Get the current health status and set the 'next event' counter to suit.
986 */
987static int
988mly_get_eventstatus(struct mly_softc *sc)
989{
990    struct mly_command_ioctl	mci;
991    struct mly_health_status	*mh;
992    u_int8_t			status;
993    int				error;
994
995    /* build the gethealthstatus ioctl and send it */
996    bzero(&mci, sizeof(mci));
997    mh = NULL;
998    mci.sub_ioctl = MDACIOCTL_GETHEALTHSTATUS;
999
1000    if ((error = mly_ioctl(sc, &mci, (void **)&mh, sizeof(*mh), &status, NULL, NULL)))
1001	return(error);
1002    if (status != 0)
1003	return(EIO);
1004
1005    /* get the event counter */
1006    sc->mly_event_change = mh->change_counter;
1007    sc->mly_event_waiting = mh->next_event;
1008    sc->mly_event_counter = mh->next_event;
1009
1010    /* save the health status into the memory mailbox */
1011    bcopy(mh, &sc->mly_mmbox->mmm_health.status, sizeof(*mh));
1012
1013    debug(1, "initial change counter %d, event counter %d", mh->change_counter, mh->next_event);
1014
1015    free(mh, M_DEVBUF);
1016    return(0);
1017}
1018
1019/********************************************************************************
1020 * Enable the memory mailbox mode.
1021 */
1022static int
1023mly_enable_mmbox(struct mly_softc *sc)
1024{
1025    struct mly_command_ioctl	mci;
1026    u_int8_t			*sp, status;
1027    int				error;
1028
1029    debug_called(1);
1030
1031    /* build the ioctl and send it */
1032    bzero(&mci, sizeof(mci));
1033    mci.sub_ioctl = MDACIOCTL_SETMEMORYMAILBOX;
1034    /* set buffer addresses */
1035    mci.param.setmemorymailbox.command_mailbox_physaddr =
1036	sc->mly_mmbox_busaddr + offsetof(struct mly_mmbox, mmm_command);
1037    mci.param.setmemorymailbox.status_mailbox_physaddr =
1038	sc->mly_mmbox_busaddr + offsetof(struct mly_mmbox, mmm_status);
1039    mci.param.setmemorymailbox.health_buffer_physaddr =
1040	sc->mly_mmbox_busaddr + offsetof(struct mly_mmbox, mmm_health);
1041
1042    /* set buffer sizes - abuse of data_size field is revolting */
1043    sp = (u_int8_t *)&mci.data_size;
1044    sp[0] = ((sizeof(union mly_command_packet) * MLY_MMBOX_COMMANDS) / 1024);
1045    sp[1] = (sizeof(union mly_status_packet) * MLY_MMBOX_STATUS) / 1024;
1046    mci.param.setmemorymailbox.health_buffer_size = sizeof(union mly_health_region) / 1024;
1047
1048    debug(1, "memory mailbox at %p (0x%llx/%d 0x%llx/%d 0x%llx/%d", sc->mly_mmbox,
1049	  mci.param.setmemorymailbox.command_mailbox_physaddr, sp[0],
1050	  mci.param.setmemorymailbox.status_mailbox_physaddr, sp[1],
1051	  mci.param.setmemorymailbox.health_buffer_physaddr,
1052	  mci.param.setmemorymailbox.health_buffer_size);
1053
1054    if ((error = mly_ioctl(sc, &mci, NULL, 0, &status, NULL, NULL)))
1055	return(error);
1056    if (status != 0)
1057	return(EIO);
1058    sc->mly_state |= MLY_STATE_MMBOX_ACTIVE;
1059    debug(1, "memory mailbox active");
1060    return(0);
1061}
1062
1063/********************************************************************************
1064 * Flush all pending I/O from the controller.
1065 */
1066static int
1067mly_flush(struct mly_softc *sc)
1068{
1069    struct mly_command_ioctl	mci;
1070    u_int8_t			status;
1071    int				error;
1072
1073    debug_called(1);
1074
1075    /* build the ioctl */
1076    bzero(&mci, sizeof(mci));
1077    mci.sub_ioctl = MDACIOCTL_FLUSHDEVICEDATA;
1078    mci.param.deviceoperation.operation_device = MLY_OPDEVICE_PHYSICAL_CONTROLLER;
1079
1080    /* pass it off to the controller */
1081    if ((error = mly_ioctl(sc, &mci, NULL, 0, &status, NULL, NULL)))
1082	return(error);
1083
1084    return((status == 0) ? 0 : EIO);
1085}
1086
1087/********************************************************************************
1088 * Perform an ioctl command.
1089 *
1090 * If (data) is not NULL, the command requires data transfer.  If (*data) is NULL
1091 * the command requires data transfer from the controller, and we will allocate
1092 * a buffer for it.  If (*data) is not NULL, the command requires data transfer
1093 * to the controller.
1094 *
1095 * XXX passing in the whole ioctl structure is ugly.  Better ideas?
1096 *
1097 * XXX we don't even try to handle the case where datasize > 4k.  We should.
1098 */
1099static int
1100mly_ioctl(struct mly_softc *sc, struct mly_command_ioctl *ioctl, void **data, size_t datasize,
1101	  u_int8_t *status, void *sense_buffer, size_t *sense_length)
1102{
1103    struct mly_command		*mc;
1104    struct mly_command_ioctl	*mci;
1105    int				error;
1106
1107    debug_called(1);
1108
1109    mc = NULL;
1110    if (mly_alloc_command(sc, &mc)) {
1111	error = ENOMEM;
1112	goto out;
1113    }
1114
1115    /* copy the ioctl structure, but save some important fields and then fixup */
1116    mci = &mc->mc_packet->ioctl;
1117    ioctl->sense_buffer_address = mci->sense_buffer_address;
1118    ioctl->maximum_sense_size = mci->maximum_sense_size;
1119    *mci = *ioctl;
1120    mci->opcode = MDACMD_IOCTL;
1121    mci->timeout.value = 30;
1122    mci->timeout.scale = MLY_TIMEOUT_SECONDS;
1123
1124    /* handle the data buffer */
1125    if (data != NULL) {
1126	if (*data == NULL) {
1127	    /* allocate data buffer */
1128	    if ((mc->mc_data = malloc(datasize, M_DEVBUF, M_NOWAIT)) == NULL) {
1129		error = ENOMEM;
1130		goto out;
1131	    }
1132	    mc->mc_flags |= MLY_CMD_DATAIN;
1133	} else {
1134	    mc->mc_data = *data;
1135	    mc->mc_flags |= MLY_CMD_DATAOUT;
1136	}
1137	mc->mc_length = datasize;
1138	mc->mc_packet->generic.data_size = datasize;
1139    }
1140
1141    /* run the command */
1142    if ((error = mly_immediate_command(mc)))
1143	goto out;
1144
1145    /* clean up and return any data */
1146    *status = mc->mc_status;
1147    if ((mc->mc_sense > 0) && (sense_buffer != NULL)) {
1148	bcopy(mc->mc_packet, sense_buffer, mc->mc_sense);
1149	*sense_length = mc->mc_sense;
1150	goto out;
1151    }
1152
1153    /* should we return a data pointer? */
1154    if ((data != NULL) && (*data == NULL))
1155	*data = mc->mc_data;
1156
1157    /* command completed OK */
1158    error = 0;
1159
1160out:
1161    if (mc != NULL) {
1162	/* do we need to free a data buffer we allocated? */
1163	if (error && (mc->mc_data != NULL) && (*data == NULL))
1164	    free(mc->mc_data, M_DEVBUF);
1165	mly_release_command(mc);
1166    }
1167    return(error);
1168}
1169
1170/********************************************************************************
1171 * Check for event(s) outstanding in the controller.
1172 */
1173static void
1174mly_check_event(struct mly_softc *sc)
1175{
1176
1177    /*
1178     * The controller may have updated the health status information,
1179     * so check for it here.  Note that the counters are all in host memory,
1180     * so this check is very cheap.  Also note that we depend on checking on
1181     * completion
1182     */
1183    if (sc->mly_mmbox->mmm_health.status.change_counter != sc->mly_event_change) {
1184	sc->mly_event_change = sc->mly_mmbox->mmm_health.status.change_counter;
1185	debug(1, "event change %d, event status update, %d -> %d", sc->mly_event_change,
1186	      sc->mly_event_waiting, sc->mly_mmbox->mmm_health.status.next_event);
1187	sc->mly_event_waiting = sc->mly_mmbox->mmm_health.status.next_event;
1188
1189	/* wake up anyone that might be interested in this */
1190	wakeup(&sc->mly_event_change);
1191    }
1192    if (sc->mly_event_counter != sc->mly_event_waiting)
1193    mly_fetch_event(sc);
1194}
1195
1196/********************************************************************************
1197 * Fetch one event from the controller.
1198 *
1199 * If we fail due to resource starvation, we'll be retried the next time a
1200 * command completes.
1201 */
1202static void
1203mly_fetch_event(struct mly_softc *sc)
1204{
1205    struct mly_command		*mc;
1206    struct mly_command_ioctl	*mci;
1207    int				s;
1208    u_int32_t			event;
1209
1210    debug_called(1);
1211
1212    /* get a command */
1213    if (mly_alloc_command(sc, &mc))
1214	return;
1215
1216    /* set up the data buffer */
1217    if ((mc->mc_data = malloc(sizeof(struct mly_event), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) {
1218	mly_release_command(mc);
1219	return;
1220    }
1221    mc->mc_length = sizeof(struct mly_event);
1222    mc->mc_flags |= MLY_CMD_DATAIN;
1223    mc->mc_complete = mly_complete_event;
1224
1225    /*
1226     * Get an event number to fetch.  It's possible that we've raced with another
1227     * context for the last event, in which case there will be no more events.
1228     */
1229    s = splcam();
1230    if (sc->mly_event_counter == sc->mly_event_waiting) {
1231	mly_release_command(mc);
1232	splx(s);
1233	return;
1234    }
1235    event = sc->mly_event_counter++;
1236    splx(s);
1237
1238    /*
1239     * Build the ioctl.
1240     *
1241     * At this point we are committed to sending this request, as it
1242     * will be the only one constructed for this particular event number.
1243     */
1244    mci = (struct mly_command_ioctl *)&mc->mc_packet->ioctl;
1245    mci->opcode = MDACMD_IOCTL;
1246    mci->data_size = sizeof(struct mly_event);
1247    mci->addr.phys.lun = (event >> 16) & 0xff;
1248    mci->addr.phys.target = (event >> 24) & 0xff;
1249    mci->addr.phys.channel = 0;
1250    mci->addr.phys.controller = 0;
1251    mci->timeout.value = 30;
1252    mci->timeout.scale = MLY_TIMEOUT_SECONDS;
1253    mci->sub_ioctl = MDACIOCTL_GETEVENT;
1254    mci->param.getevent.sequence_number_low = event & 0xffff;
1255
1256    debug(1, "fetch event %u", event);
1257
1258    /*
1259     * Submit the command.
1260     *
1261     * Note that failure of mly_start() will result in this event never being
1262     * fetched.
1263     */
1264    if (mly_start(mc) != 0) {
1265	mly_printf(sc, "couldn't fetch event %u\n", event);
1266	mly_release_command(mc);
1267    }
1268}
1269
1270/********************************************************************************
1271 * Handle the completion of an event poll.
1272 */
1273static void
1274mly_complete_event(struct mly_command *mc)
1275{
1276    struct mly_softc	*sc = mc->mc_sc;
1277    struct mly_event	*me = (struct mly_event *)mc->mc_data;
1278
1279    debug_called(1);
1280
1281    /*
1282     * If the event was successfully fetched, process it.
1283     */
1284    if (mc->mc_status == SCSI_STATUS_OK) {
1285	mly_process_event(sc, me);
1286	free(me, M_DEVBUF);
1287    }
1288    mly_release_command(mc);
1289
1290    /*
1291     * Check for another event.
1292     */
1293    mly_check_event(sc);
1294}
1295
1296/********************************************************************************
1297 * Process a controller event.
1298 */
1299static void
1300mly_process_event(struct mly_softc *sc, struct mly_event *me)
1301{
1302    struct scsi_sense_data	*ssd = (struct scsi_sense_data *)&me->sense[0];
1303    char			*fp, *tp;
1304    int				bus, target, event, class, action;
1305
1306    /*
1307     * Errors can be reported using vendor-unique sense data.  In this case, the
1308     * event code will be 0x1c (Request sense data present), the sense key will
1309     * be 0x09 (vendor specific), the MSB of the ASC will be set, and the
1310     * actual event code will be a 16-bit value comprised of the ASCQ (low byte)
1311     * and low seven bits of the ASC (low seven bits of the high byte).
1312     */
1313    if ((me->code == 0x1c) &&
1314	((ssd->flags & SSD_KEY) == SSD_KEY_Vendor_Specific) &&
1315	(ssd->add_sense_code & 0x80)) {
1316	event = ((int)(ssd->add_sense_code & ~0x80) << 8) + ssd->add_sense_code_qual;
1317    } else {
1318	event = me->code;
1319    }
1320
1321    /* look up event, get codes */
1322    fp = mly_describe_code(mly_table_event, event);
1323
1324    debug(1, "Event %d  code 0x%x", me->sequence_number, me->code);
1325
1326    /* quiet event? */
1327    class = fp[0];
1328    if (isupper(class) && bootverbose)
1329	class = tolower(class);
1330
1331    /* get action code, text string */
1332    action = fp[1];
1333    tp = &fp[2];
1334
1335    /*
1336     * Print some information about the event.
1337     *
1338     * This code uses a table derived from the corresponding portion of the Linux
1339     * driver, and thus the parser is very similar.
1340     */
1341    switch(class) {
1342    case 'p':		/* error on physical device */
1343	mly_printf(sc, "physical device %d:%d %s\n", me->channel, me->target, tp);
1344	if (action == 'r')
1345	    sc->mly_btl[me->channel][me->target].mb_flags |= MLY_BTL_RESCAN;
1346	break;
1347    case 'l':		/* error on logical unit */
1348    case 'm':		/* message about logical unit */
1349	bus = MLY_LOGDEV_BUS(sc, me->lun);
1350	target = MLY_LOGDEV_TARGET(sc, me->lun);
1351	mly_name_device(sc, bus, target);
1352	mly_printf(sc, "logical device %d (%s) %s\n", me->lun, sc->mly_btl[bus][target].mb_name, tp);
1353	if (action == 'r')
1354	    sc->mly_btl[bus][target].mb_flags |= MLY_BTL_RESCAN;
1355	break;
1356      break;
1357    case 's':		/* report of sense data */
1358	if (((ssd->flags & SSD_KEY) == SSD_KEY_NO_SENSE) ||
1359	    (((ssd->flags & SSD_KEY) == SSD_KEY_NOT_READY) &&
1360	     (ssd->add_sense_code == 0x04) &&
1361	     ((ssd->add_sense_code_qual == 0x01) || (ssd->add_sense_code_qual == 0x02))))
1362	    break;	/* ignore NO_SENSE or NOT_READY in one case */
1363
1364	mly_printf(sc, "physical device %d:%d %s\n", me->channel, me->target, tp);
1365	mly_printf(sc, "  sense key %d  asc %02x  ascq %02x\n",
1366		      ssd->flags & SSD_KEY, ssd->add_sense_code, ssd->add_sense_code_qual);
1367	mly_printf(sc, "  info %4D  csi %4D\n", ssd->info, "", ssd->cmd_spec_info, "");
1368	if (action == 'r')
1369	    sc->mly_btl[me->channel][me->target].mb_flags |= MLY_BTL_RESCAN;
1370	break;
1371    case 'e':
1372	mly_printf(sc, tp, me->target, me->lun);
1373	printf("\n");
1374	break;
1375    case 'c':
1376	mly_printf(sc, "controller %s\n", tp);
1377	break;
1378    case '?':
1379	mly_printf(sc, "%s - %d\n", tp, me->code);
1380	break;
1381    default:	/* probably a 'noisy' event being ignored */
1382	break;
1383    }
1384}
1385
1386/********************************************************************************
1387 * Perform periodic activities.
1388 */
1389static void
1390mly_periodic(void *data)
1391{
1392    struct mly_softc	*sc = (struct mly_softc *)data;
1393    int			bus, target;
1394
1395    debug_called(2);
1396
1397    /*
1398     * Scan devices.
1399     */
1400    for (bus = 0; bus < sc->mly_cam_channels; bus++) {
1401	if (MLY_BUS_IS_VALID(sc, bus)) {
1402	    for (target = 0; target < MLY_MAX_TARGETS; target++) {
1403
1404		/* ignore the controller in this scan */
1405		if (target == sc->mly_controllerparam->initiator_id)
1406		    continue;
1407
1408		/* perform device rescan? */
1409		if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_RESCAN)
1410		    mly_rescan_btl(sc, bus, target);
1411	    }
1412	}
1413    }
1414
1415    /* check for controller events */
1416    mly_check_event(sc);
1417
1418    /* reschedule ourselves */
1419    sc->mly_periodic = timeout(mly_periodic, sc, MLY_PERIODIC_INTERVAL * hz);
1420}
1421
1422/********************************************************************************
1423 ********************************************************************************
1424                                                               Command Processing
1425 ********************************************************************************
1426 ********************************************************************************/
1427
1428/********************************************************************************
1429 * Run a command and wait for it to complete.
1430 *
1431 */
1432static int
1433mly_immediate_command(struct mly_command *mc)
1434{
1435    struct mly_softc	*sc = mc->mc_sc;
1436    int			error, s;
1437
1438    debug_called(1);
1439
1440    /* spinning at splcam is ugly, but we're only used during controller init */
1441    s = splcam();
1442    if ((error = mly_start(mc))) {
1443	splx(s);
1444	return(error);
1445    }
1446
1447    if (sc->mly_state & MLY_STATE_INTERRUPTS_ON) {
1448	/* sleep on the command */
1449	while(!(mc->mc_flags & MLY_CMD_COMPLETE)) {
1450	    tsleep(mc, PRIBIO, "mlywait", 0);
1451	}
1452    } else {
1453	/* spin and collect status while we do */
1454	while(!(mc->mc_flags & MLY_CMD_COMPLETE)) {
1455	    mly_done(mc->mc_sc);
1456	}
1457    }
1458    splx(s);
1459    return(0);
1460}
1461
1462/********************************************************************************
1463 * Deliver a command to the controller.
1464 *
1465 * XXX it would be good to just queue commands that we can't submit immediately
1466 *     and send them later, but we probably want a wrapper for that so that
1467 *     we don't hang on a failed submission for an immediate command.
1468 */
1469static int
1470mly_start(struct mly_command *mc)
1471{
1472    struct mly_softc		*sc = mc->mc_sc;
1473    union mly_command_packet	*pkt;
1474    int				s;
1475
1476    debug_called(2);
1477
1478    /*
1479     * Set the command up for delivery to the controller.
1480     */
1481    mly_map_command(mc);
1482    mc->mc_packet->generic.command_id = mc->mc_slot;
1483
1484#ifdef MLY_DEBUG
1485    mc->mc_timestamp = time_second;
1486#endif
1487
1488    s = splcam();
1489
1490    /*
1491     * Do we have to use the hardware mailbox?
1492     */
1493    if (!(sc->mly_state & MLY_STATE_MMBOX_ACTIVE)) {
1494	/*
1495	 * Check to see if the controller is ready for us.
1496	 */
1497	if (MLY_IDBR_TRUE(sc, MLY_HM_CMDSENT)) {
1498	    splx(s);
1499	    return(EBUSY);
1500	}
1501	mc->mc_flags |= MLY_CMD_BUSY;
1502
1503	/*
1504	 * It's ready, send the command.
1505	 */
1506	MLY_SET_MBOX(sc, sc->mly_command_mailbox, &mc->mc_packetphys);
1507	MLY_SET_REG(sc, sc->mly_idbr, MLY_HM_CMDSENT);
1508
1509    } else {	/* use memory-mailbox mode */
1510
1511	pkt = &sc->mly_mmbox->mmm_command[sc->mly_mmbox_command_index];
1512
1513	/* check to see if the next index is free yet */
1514	if (pkt->mmbox.flag != 0) {
1515	    splx(s);
1516	    return(EBUSY);
1517	}
1518	mc->mc_flags |= MLY_CMD_BUSY;
1519
1520	/* copy in new command */
1521	bcopy(mc->mc_packet->mmbox.data, pkt->mmbox.data, sizeof(pkt->mmbox.data));
1522	/* barrier to ensure completion of previous write before we write the flag */
1523	bus_space_barrier(sc->mly_btag, sc->mly_bhandle, 0, 0,
1524	    BUS_SPACE_BARRIER_WRITE);
1525	/* copy flag last */
1526	pkt->mmbox.flag = mc->mc_packet->mmbox.flag;
1527	/* barrier to ensure completion of previous write before we notify the controller */
1528	bus_space_barrier(sc->mly_btag, sc->mly_bhandle, 0, 0,
1529	    BUS_SPACE_BARRIER_WRITE);
1530
1531	/* signal controller, update index */
1532	MLY_SET_REG(sc, sc->mly_idbr, MLY_AM_CMDSENT);
1533	sc->mly_mmbox_command_index = (sc->mly_mmbox_command_index + 1) % MLY_MMBOX_COMMANDS;
1534    }
1535
1536    mly_enqueue_busy(mc);
1537    splx(s);
1538    return(0);
1539}
1540
1541/********************************************************************************
1542 * Pick up command status from the controller, schedule a completion event
1543 */
1544static void
1545mly_done(struct mly_softc *sc)
1546{
1547    struct mly_command		*mc;
1548    union mly_status_packet	*sp;
1549    u_int16_t			slot;
1550    int				s, worked;
1551
1552    s = splcam();
1553    worked = 0;
1554
1555    /* pick up hardware-mailbox commands */
1556    if (MLY_ODBR_TRUE(sc, MLY_HM_STSREADY)) {
1557	slot = MLY_GET_REG2(sc, sc->mly_status_mailbox);
1558	if (slot < MLY_SLOT_MAX) {
1559	    mc = &sc->mly_command[slot - MLY_SLOT_START];
1560	    mc->mc_status = MLY_GET_REG(sc, sc->mly_status_mailbox + 2);
1561	    mc->mc_sense = MLY_GET_REG(sc, sc->mly_status_mailbox + 3);
1562	    mc->mc_resid = MLY_GET_REG4(sc, sc->mly_status_mailbox + 4);
1563	    mly_remove_busy(mc);
1564	    mc->mc_flags &= ~MLY_CMD_BUSY;
1565	    mly_enqueue_complete(mc);
1566	    worked = 1;
1567	} else {
1568	    /* slot 0xffff may mean "extremely bogus command" */
1569	    mly_printf(sc, "got HM completion for illegal slot %u\n", slot);
1570	}
1571	/* unconditionally acknowledge status */
1572	MLY_SET_REG(sc, sc->mly_odbr, MLY_HM_STSREADY);
1573	MLY_SET_REG(sc, sc->mly_idbr, MLY_HM_STSACK);
1574    }
1575
1576    /* pick up memory-mailbox commands */
1577    if (MLY_ODBR_TRUE(sc, MLY_AM_STSREADY)) {
1578	for (;;) {
1579	    sp = &sc->mly_mmbox->mmm_status[sc->mly_mmbox_status_index];
1580
1581	    /* check for more status */
1582	    if (sp->mmbox.flag == 0)
1583		break;
1584
1585	    /* get slot number */
1586	    slot = sp->status.command_id;
1587	    if (slot < MLY_SLOT_MAX) {
1588		mc = &sc->mly_command[slot - MLY_SLOT_START];
1589		mc->mc_status = sp->status.status;
1590		mc->mc_sense = sp->status.sense_length;
1591		mc->mc_resid = sp->status.residue;
1592		mly_remove_busy(mc);
1593		mc->mc_flags &= ~MLY_CMD_BUSY;
1594		mly_enqueue_complete(mc);
1595		worked = 1;
1596	    } else {
1597		/* slot 0xffff may mean "extremely bogus command" */
1598		mly_printf(sc, "got AM completion for illegal slot %u at %d\n",
1599			   slot, sc->mly_mmbox_status_index);
1600	    }
1601
1602	    /* clear and move to next index */
1603	    sp->mmbox.flag = 0;
1604	    sc->mly_mmbox_status_index = (sc->mly_mmbox_status_index + 1) % MLY_MMBOX_STATUS;
1605	}
1606	/* acknowledge that we have collected status value(s) */
1607	MLY_SET_REG(sc, sc->mly_odbr, MLY_AM_STSREADY);
1608    }
1609
1610    splx(s);
1611    if (worked) {
1612#if __FreeBSD_version >= 500005
1613	if (sc->mly_state & MLY_STATE_INTERRUPTS_ON)
1614	    taskqueue_enqueue(taskqueue_swi_giant, &sc->mly_task_complete);
1615	else
1616#endif
1617	    mly_complete(sc, 0);
1618    }
1619}
1620
1621/********************************************************************************
1622 * Process completed commands
1623 */
1624static void
1625mly_complete(void *context, int pending)
1626{
1627    struct mly_softc	*sc = (struct mly_softc *)context;
1628    struct mly_command	*mc;
1629    void	        (* mc_complete)(struct mly_command *mc);
1630
1631
1632    debug_called(2);
1633
1634    /*
1635     * Spin pulling commands off the completed queue and processing them.
1636     */
1637    while ((mc = mly_dequeue_complete(sc)) != NULL) {
1638
1639	/*
1640	 * Free controller resources, mark command complete.
1641	 *
1642	 * Note that as soon as we mark the command complete, it may be freed
1643	 * out from under us, so we need to save the mc_complete field in
1644	 * order to later avoid dereferencing mc.  (We would not expect to
1645	 * have a polling/sleeping consumer with mc_complete != NULL).
1646	 */
1647	mly_unmap_command(mc);
1648	mc_complete = mc->mc_complete;
1649	mc->mc_flags |= MLY_CMD_COMPLETE;
1650
1651	/*
1652	 * Call completion handler or wake up sleeping consumer.
1653	 */
1654	if (mc_complete != NULL) {
1655	    mc_complete(mc);
1656	} else {
1657	    wakeup(mc);
1658	}
1659    }
1660
1661    /*
1662     * XXX if we are deferring commands due to controller-busy status, we should
1663     *     retry submitting them here.
1664     */
1665}
1666
1667/********************************************************************************
1668 ********************************************************************************
1669                                                        Command Buffer Management
1670 ********************************************************************************
1671 ********************************************************************************/
1672
1673/********************************************************************************
1674 * Allocate a command.
1675 */
1676static int
1677mly_alloc_command(struct mly_softc *sc, struct mly_command **mcp)
1678{
1679    struct mly_command	*mc;
1680
1681    debug_called(3);
1682
1683    if ((mc = mly_dequeue_free(sc)) == NULL)
1684	return(ENOMEM);
1685
1686    *mcp = mc;
1687    return(0);
1688}
1689
1690/********************************************************************************
1691 * Release a command back to the freelist.
1692 */
1693static void
1694mly_release_command(struct mly_command *mc)
1695{
1696    debug_called(3);
1697
1698    /*
1699     * Fill in parts of the command that may cause confusion if
1700     * a consumer doesn't when we are later allocated.
1701     */
1702    mc->mc_data = NULL;
1703    mc->mc_flags = 0;
1704    mc->mc_complete = NULL;
1705    mc->mc_private = NULL;
1706
1707    /*
1708     * By default, we set up to overwrite the command packet with
1709     * sense information.
1710     */
1711    mc->mc_packet->generic.sense_buffer_address = mc->mc_packetphys;
1712    mc->mc_packet->generic.maximum_sense_size = sizeof(union mly_command_packet);
1713
1714    mly_enqueue_free(mc);
1715}
1716
1717/********************************************************************************
1718 * Map helper for command allocation.
1719 */
1720static void
1721mly_alloc_commands_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1722{
1723    struct mly_softc	*sc = (struct mly_softc *)arg;
1724
1725    debug_called(1);
1726
1727    sc->mly_packetphys = segs[0].ds_addr;
1728}
1729
1730/********************************************************************************
1731 * Allocate and initialise command and packet structures.
1732 *
1733 * If the controller supports fewer than MLY_MAX_COMMANDS commands, limit our
1734 * allocation to that number.  If we don't yet know how many commands the
1735 * controller supports, allocate a very small set (suitable for initialisation
1736 * purposes only).
1737 */
1738static int
1739mly_alloc_commands(struct mly_softc *sc)
1740{
1741    struct mly_command		*mc;
1742    int				i, ncmd;
1743
1744    if (sc->mly_controllerinfo == NULL) {
1745	ncmd = 4;
1746    } else {
1747	ncmd = min(MLY_MAX_COMMANDS, sc->mly_controllerinfo->maximum_parallel_commands);
1748    }
1749
1750    /*
1751     * Allocate enough space for all the command packets in one chunk and
1752     * map them permanently into controller-visible space.
1753     */
1754    if (bus_dmamem_alloc(sc->mly_packet_dmat, (void **)&sc->mly_packet,
1755			 BUS_DMA_NOWAIT, &sc->mly_packetmap)) {
1756	return(ENOMEM);
1757    }
1758    if (bus_dmamap_load(sc->mly_packet_dmat, sc->mly_packetmap, sc->mly_packet,
1759			ncmd * sizeof(union mly_command_packet),
1760			mly_alloc_commands_map, sc, BUS_DMA_NOWAIT) != 0)
1761	return (ENOMEM);
1762
1763    for (i = 0; i < ncmd; i++) {
1764	mc = &sc->mly_command[i];
1765	bzero(mc, sizeof(*mc));
1766	mc->mc_sc = sc;
1767	mc->mc_slot = MLY_SLOT_START + i;
1768	mc->mc_packet = sc->mly_packet + i;
1769	mc->mc_packetphys = sc->mly_packetphys + (i * sizeof(union mly_command_packet));
1770	if (!bus_dmamap_create(sc->mly_buffer_dmat, 0, &mc->mc_datamap))
1771	    mly_release_command(mc);
1772    }
1773    return(0);
1774}
1775
1776/********************************************************************************
1777 * Free all the storage held by commands.
1778 *
1779 * Must be called with all commands on the free list.
1780 */
1781static void
1782mly_release_commands(struct mly_softc *sc)
1783{
1784    struct mly_command	*mc;
1785
1786    /* throw away command buffer DMA maps */
1787    while (mly_alloc_command(sc, &mc) == 0)
1788	bus_dmamap_destroy(sc->mly_buffer_dmat, mc->mc_datamap);
1789
1790    /* release the packet storage */
1791    if (sc->mly_packet != NULL) {
1792	bus_dmamap_unload(sc->mly_packet_dmat, sc->mly_packetmap);
1793	bus_dmamem_free(sc->mly_packet_dmat, sc->mly_packet, sc->mly_packetmap);
1794	sc->mly_packet = NULL;
1795    }
1796}
1797
1798
1799/********************************************************************************
1800 * Command-mapping helper function - populate this command's s/g table
1801 * with the s/g entries for its data.
1802 */
1803static void
1804mly_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1805{
1806    struct mly_command		*mc = (struct mly_command *)arg;
1807    struct mly_softc		*sc = mc->mc_sc;
1808    struct mly_command_generic	*gen = &(mc->mc_packet->generic);
1809    struct mly_sg_entry		*sg;
1810    int				i, tabofs;
1811
1812    debug_called(2);
1813
1814    /* can we use the transfer structure directly? */
1815    if (nseg <= 2) {
1816	sg = &gen->transfer.direct.sg[0];
1817	gen->command_control.extended_sg_table = 0;
1818    } else {
1819	tabofs = ((mc->mc_slot - MLY_SLOT_START) * MLY_MAX_SGENTRIES);
1820	sg = sc->mly_sg_table + tabofs;
1821	gen->transfer.indirect.entries[0] = nseg;
1822	gen->transfer.indirect.table_physaddr[0] = sc->mly_sg_busaddr + (tabofs * sizeof(struct mly_sg_entry));
1823	gen->command_control.extended_sg_table = 1;
1824    }
1825
1826    /* copy the s/g table */
1827    for (i = 0; i < nseg; i++) {
1828	sg[i].physaddr = segs[i].ds_addr;
1829	sg[i].length = segs[i].ds_len;
1830    }
1831
1832}
1833
1834#if 0
1835/********************************************************************************
1836 * Command-mapping helper function - save the cdb's physical address.
1837 *
1838 * We don't support 'large' SCSI commands at this time, so this is unused.
1839 */
1840static void
1841mly_map_command_cdb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1842{
1843    struct mly_command			*mc = (struct mly_command *)arg;
1844
1845    debug_called(2);
1846
1847    /* XXX can we safely assume that a CDB will never cross a page boundary? */
1848    if ((segs[0].ds_addr % PAGE_SIZE) >
1849	((segs[0].ds_addr + mc->mc_packet->scsi_large.cdb_length) % PAGE_SIZE))
1850	panic("cdb crosses page boundary");
1851
1852    /* fix up fields in the command packet */
1853    mc->mc_packet->scsi_large.cdb_physaddr = segs[0].ds_addr;
1854}
1855#endif
1856
1857/********************************************************************************
1858 * Map a command into controller-visible space
1859 */
1860static void
1861mly_map_command(struct mly_command *mc)
1862{
1863    struct mly_softc	*sc = mc->mc_sc;
1864
1865    debug_called(2);
1866
1867    /* don't map more than once */
1868    if (mc->mc_flags & MLY_CMD_MAPPED)
1869	return;
1870
1871    /* does the command have a data buffer? */
1872    if (mc->mc_data != NULL) {
1873	bus_dmamap_load(sc->mly_buffer_dmat, mc->mc_datamap, mc->mc_data, mc->mc_length,
1874			mly_map_command_sg, mc, 0);
1875
1876	if (mc->mc_flags & MLY_CMD_DATAIN)
1877	    bus_dmamap_sync(sc->mly_buffer_dmat, mc->mc_datamap, BUS_DMASYNC_PREREAD);
1878	if (mc->mc_flags & MLY_CMD_DATAOUT)
1879	    bus_dmamap_sync(sc->mly_buffer_dmat, mc->mc_datamap, BUS_DMASYNC_PREWRITE);
1880    }
1881    mc->mc_flags |= MLY_CMD_MAPPED;
1882}
1883
1884/********************************************************************************
1885 * Unmap a command from controller-visible space
1886 */
1887static void
1888mly_unmap_command(struct mly_command *mc)
1889{
1890    struct mly_softc	*sc = mc->mc_sc;
1891
1892    debug_called(2);
1893
1894    if (!(mc->mc_flags & MLY_CMD_MAPPED))
1895	return;
1896
1897    /* does the command have a data buffer? */
1898    if (mc->mc_data != NULL) {
1899	if (mc->mc_flags & MLY_CMD_DATAIN)
1900	    bus_dmamap_sync(sc->mly_buffer_dmat, mc->mc_datamap, BUS_DMASYNC_POSTREAD);
1901	if (mc->mc_flags & MLY_CMD_DATAOUT)
1902	    bus_dmamap_sync(sc->mly_buffer_dmat, mc->mc_datamap, BUS_DMASYNC_POSTWRITE);
1903
1904	bus_dmamap_unload(sc->mly_buffer_dmat, mc->mc_datamap);
1905    }
1906    mc->mc_flags &= ~MLY_CMD_MAPPED;
1907}
1908
1909
1910/********************************************************************************
1911 ********************************************************************************
1912                                                                    CAM interface
1913 ********************************************************************************
1914 ********************************************************************************/
1915
1916/********************************************************************************
1917 * Attach the physical and virtual SCSI busses to CAM.
1918 *
1919 * Physical bus numbering starts from 0, virtual bus numbering from one greater
1920 * than the highest physical bus.  Physical busses are only registered if
1921 * the kernel environment variable "hw.mly.register_physical_channels" is set.
1922 *
1923 * When we refer to a "bus", we are referring to the bus number registered with
1924 * the SIM, wheras a "channel" is a channel number given to the adapter.  In order
1925 * to keep things simple, we map these 1:1, so "bus" and "channel" may be used
1926 * interchangeably.
1927 */
1928static int
1929mly_cam_attach(struct mly_softc *sc)
1930{
1931    struct cam_devq	*devq;
1932    int			chn, i;
1933
1934    debug_called(1);
1935
1936    /*
1937     * Allocate a devq for all our channels combined.
1938     */
1939    if ((devq = cam_simq_alloc(sc->mly_controllerinfo->maximum_parallel_commands)) == NULL) {
1940	mly_printf(sc, "can't allocate CAM SIM queue\n");
1941	return(ENOMEM);
1942    }
1943
1944    /*
1945     * If physical channel registration has been requested, register these first.
1946     * Note that we enable tagged command queueing for physical channels.
1947     */
1948    if (testenv("hw.mly.register_physical_channels")) {
1949	chn = 0;
1950	for (i = 0; i < sc->mly_controllerinfo->physical_channels_present; i++, chn++) {
1951
1952	    if ((sc->mly_cam_sim[chn] = cam_sim_alloc(mly_cam_action, mly_cam_poll, "mly", sc,
1953						      device_get_unit(sc->mly_dev),
1954						      sc->mly_controllerinfo->maximum_parallel_commands,
1955						      1, devq)) == NULL) {
1956		return(ENOMEM);
1957	    }
1958	    if (xpt_bus_register(sc->mly_cam_sim[chn], chn)) {
1959		mly_printf(sc, "CAM XPT phsyical channel registration failed\n");
1960		return(ENXIO);
1961	    }
1962	    debug(1, "registered physical channel %d", chn);
1963	}
1964    }
1965
1966    /*
1967     * Register our virtual channels, with bus numbers matching channel numbers.
1968     */
1969    chn = sc->mly_controllerinfo->physical_channels_present;
1970    for (i = 0; i < sc->mly_controllerinfo->virtual_channels_present; i++, chn++) {
1971	if ((sc->mly_cam_sim[chn] = cam_sim_alloc(mly_cam_action, mly_cam_poll, "mly", sc,
1972						  device_get_unit(sc->mly_dev),
1973						  sc->mly_controllerinfo->maximum_parallel_commands,
1974						  0, devq)) == NULL) {
1975	    return(ENOMEM);
1976	}
1977	if (xpt_bus_register(sc->mly_cam_sim[chn], chn)) {
1978	    mly_printf(sc, "CAM XPT virtual channel registration failed\n");
1979	    return(ENXIO);
1980	}
1981	debug(1, "registered virtual channel %d", chn);
1982    }
1983
1984    /*
1985     * This is the total number of channels that (might have been) registered with
1986     * CAM.  Some may not have been; check the mly_cam_sim array to be certain.
1987     */
1988    sc->mly_cam_channels = sc->mly_controllerinfo->physical_channels_present +
1989	sc->mly_controllerinfo->virtual_channels_present;
1990
1991    return(0);
1992}
1993
1994/********************************************************************************
1995 * Detach from CAM
1996 */
1997static void
1998mly_cam_detach(struct mly_softc *sc)
1999{
2000    int		i;
2001
2002    debug_called(1);
2003
2004    for (i = 0; i < sc->mly_cam_channels; i++) {
2005	if (sc->mly_cam_sim[i] != NULL) {
2006	    xpt_bus_deregister(cam_sim_path(sc->mly_cam_sim[i]));
2007	    cam_sim_free(sc->mly_cam_sim[i], 0);
2008	}
2009    }
2010    if (sc->mly_cam_devq != NULL)
2011	cam_simq_free(sc->mly_cam_devq);
2012}
2013
2014/************************************************************************
2015 * Rescan a device.
2016 */
2017static void
2018mly_cam_rescan_btl(struct mly_softc *sc, int bus, int target)
2019{
2020    union ccb	*ccb;
2021
2022    debug_called(1);
2023
2024    if ((ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO)) == NULL) {
2025	mly_printf(sc, "rescan failed (can't allocate CCB)\n");
2026	return;
2027    }
2028
2029    if (xpt_create_path(&sc->mly_cam_path, xpt_periph,
2030			cam_sim_path(sc->mly_cam_sim[bus]), target, 0) != CAM_REQ_CMP) {
2031	mly_printf(sc, "rescan failed (can't create path)\n");
2032	free(ccb, M_TEMP);
2033	return;
2034    }
2035    xpt_setup_ccb(&ccb->ccb_h, sc->mly_cam_path, 5/*priority (low)*/);
2036    ccb->ccb_h.func_code = XPT_SCAN_LUN;
2037    ccb->ccb_h.cbfcnp = mly_cam_rescan_callback;
2038    ccb->crcn.flags = CAM_FLAG_NONE;
2039    debug(1, "rescan target %d:%d", bus, target);
2040    xpt_action(ccb);
2041}
2042
2043static void
2044mly_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2045{
2046    free(ccb, M_TEMP);
2047}
2048
2049/********************************************************************************
2050 * Handle an action requested by CAM
2051 */
2052static void
2053mly_cam_action(struct cam_sim *sim, union ccb *ccb)
2054{
2055    struct mly_softc	*sc = cam_sim_softc(sim);
2056
2057    debug_called(2);
2058
2059    switch (ccb->ccb_h.func_code) {
2060
2061	/* perform SCSI I/O */
2062    case XPT_SCSI_IO:
2063	if (!mly_cam_action_io(sim, (struct ccb_scsiio *)&ccb->csio))
2064	    return;
2065	break;
2066
2067	/* perform geometry calculations */
2068    case XPT_CALC_GEOMETRY:
2069    {
2070	struct ccb_calc_geometry	*ccg = &ccb->ccg;
2071        u_int32_t			secs_per_cylinder;
2072
2073	debug(2, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2074
2075	if (sc->mly_controllerparam->bios_geometry == MLY_BIOSGEOM_8G) {
2076	    ccg->heads = 255;
2077            ccg->secs_per_track = 63;
2078	} else {				/* MLY_BIOSGEOM_2G */
2079	    ccg->heads = 128;
2080            ccg->secs_per_track = 32;
2081	}
2082	secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2083        ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2084        ccb->ccb_h.status = CAM_REQ_CMP;
2085        break;
2086    }
2087
2088	/* handle path attribute inquiry */
2089    case XPT_PATH_INQ:
2090    {
2091	struct ccb_pathinq	*cpi = &ccb->cpi;
2092
2093	debug(2, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2094
2095	cpi->version_num = 1;
2096	cpi->hba_inquiry = PI_TAG_ABLE;		/* XXX extra flags for physical channels? */
2097	cpi->target_sprt = 0;
2098	cpi->hba_misc = 0;
2099	cpi->max_target = MLY_MAX_TARGETS - 1;
2100	cpi->max_lun = MLY_MAX_LUNS - 1;
2101	cpi->initiator_id = sc->mly_controllerparam->initiator_id;
2102	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2103        strncpy(cpi->hba_vid, "FreeBSD", HBA_IDLEN);
2104        strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2105        cpi->unit_number = cam_sim_unit(sim);
2106        cpi->bus_id = cam_sim_bus(sim);
2107	cpi->base_transfer_speed = 132 * 1024;	/* XXX what to set this to? */
2108	ccb->ccb_h.status = CAM_REQ_CMP;
2109	break;
2110    }
2111
2112    case XPT_GET_TRAN_SETTINGS:
2113    {
2114	struct ccb_trans_settings	*cts = &ccb->cts;
2115	int				bus, target;
2116
2117	bus = cam_sim_bus(sim);
2118	target = cts->ccb_h.target_id;
2119	/* XXX validate bus/target? */
2120
2121	debug(2, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
2122	cts->valid = 0;
2123
2124	/* logical device? */
2125	if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_LOGICAL) {
2126	    /* nothing special for these */
2127
2128	/* physical device? */
2129	} else if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_PHYSICAL) {
2130	    /* allow CAM to try tagged transactions */
2131	    cts->flags |= CCB_TRANS_TAG_ENB;
2132	    cts->valid |= CCB_TRANS_TQ_VALID;
2133
2134	    /* convert speed (MHz) to usec */
2135	    if (sc->mly_btl[bus][target].mb_speed == 0) {
2136		cts->sync_period = 1000000 / 5;
2137	    } else {
2138		cts->sync_period = 1000000 / sc->mly_btl[bus][target].mb_speed;
2139	    }
2140
2141	    /* convert bus width to CAM internal encoding */
2142	    switch (sc->mly_btl[bus][target].mb_width) {
2143	    case 32:
2144		cts->bus_width = MSG_EXT_WDTR_BUS_32_BIT;
2145		break;
2146	    case 16:
2147		cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2148		break;
2149	    case 8:
2150	    default:
2151		cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2152		break;
2153	    }
2154	    cts->valid |= CCB_TRANS_SYNC_RATE_VALID | CCB_TRANS_BUS_WIDTH_VALID;
2155
2156	    /* not a device, bail out */
2157	} else {
2158	    cts->ccb_h.status = CAM_REQ_CMP_ERR;
2159	    break;
2160	}
2161
2162	/* disconnect always OK */
2163	cts->flags |= CCB_TRANS_DISC_ENB;
2164	cts->valid |= CCB_TRANS_DISC_VALID;
2165
2166	cts->ccb_h.status = CAM_REQ_CMP;
2167	break;
2168    }
2169
2170    default:		/* we can't do this */
2171	debug(2, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
2172	ccb->ccb_h.status = CAM_REQ_INVALID;
2173	break;
2174    }
2175
2176    xpt_done(ccb);
2177}
2178
2179/********************************************************************************
2180 * Handle an I/O operation requested by CAM
2181 */
2182static int
2183mly_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
2184{
2185    struct mly_softc			*sc = cam_sim_softc(sim);
2186    struct mly_command			*mc;
2187    struct mly_command_scsi_small	*ss;
2188    int					bus, target;
2189    int					error;
2190    int					s;
2191
2192    bus = cam_sim_bus(sim);
2193    target = csio->ccb_h.target_id;
2194
2195    debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
2196
2197    /* validate bus number */
2198    if (!MLY_BUS_IS_VALID(sc, bus)) {
2199	debug(0, " invalid bus %d", bus);
2200	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2201    }
2202
2203    /*  check for I/O attempt to a protected device */
2204    if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_PROTECTED) {
2205	debug(2, "  device protected");
2206	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2207    }
2208
2209    /* check for I/O attempt to nonexistent device */
2210    if (!(sc->mly_btl[bus][target].mb_flags & (MLY_BTL_LOGICAL | MLY_BTL_PHYSICAL))) {
2211	debug(2, "  device %d:%d does not exist", bus, target);
2212	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2213    }
2214
2215    /* XXX increase if/when we support large SCSI commands */
2216    if (csio->cdb_len > MLY_CMD_SCSI_SMALL_CDB) {
2217	debug(0, "  command too large (%d > %d)", csio->cdb_len, MLY_CMD_SCSI_SMALL_CDB);
2218	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2219    }
2220
2221    /* check that the CDB pointer is not to a physical address */
2222    if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
2223	debug(0, "  CDB pointer is to physical address");
2224	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2225    }
2226
2227    /* if there is data transfer, it must be to/from a virtual address */
2228    if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
2229	if (csio->ccb_h.flags & CAM_DATA_PHYS) {		/* we can't map it */
2230	    debug(0, "  data pointer is to physical address");
2231	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
2232	}
2233	if (csio->ccb_h.flags & CAM_SCATTER_VALID) {	/* we want to do the s/g setup */
2234	    debug(0, "  data has premature s/g setup");
2235	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
2236	}
2237    }
2238
2239    /* abandon aborted ccbs or those that have failed validation */
2240    if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
2241	debug(2, "abandoning CCB due to abort/validation failure");
2242	return(EINVAL);
2243    }
2244
2245    /*
2246     * Get a command, or push the ccb back to CAM and freeze the queue.
2247     */
2248    if ((error = mly_alloc_command(sc, &mc))) {
2249	s = splcam();
2250	xpt_freeze_simq(sim, 1);
2251	csio->ccb_h.status |= CAM_REQUEUE_REQ;
2252	sc->mly_qfrzn_cnt++;
2253	splx(s);
2254	return(error);
2255    }
2256
2257    /* build the command */
2258    mc->mc_data = csio->data_ptr;
2259    mc->mc_length = csio->dxfer_len;
2260    mc->mc_complete = mly_cam_complete;
2261    mc->mc_private = csio;
2262
2263    /* save the bus number in the ccb for later recovery XXX should be a better way */
2264     csio->ccb_h.sim_priv.entries[0].field = bus;
2265
2266    /* build the packet for the controller */
2267    ss = &mc->mc_packet->scsi_small;
2268    ss->opcode = MDACMD_SCSI;
2269    if (csio->ccb_h.flags & CAM_DIS_DISCONNECT)
2270	ss->command_control.disable_disconnect = 1;
2271    if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
2272	ss->command_control.data_direction = MLY_CCB_WRITE;
2273    ss->data_size = csio->dxfer_len;
2274    ss->addr.phys.lun = csio->ccb_h.target_lun;
2275    ss->addr.phys.target = csio->ccb_h.target_id;
2276    ss->addr.phys.channel = bus;
2277    if (csio->ccb_h.timeout < (60 * 1000)) {
2278	ss->timeout.value = csio->ccb_h.timeout / 1000;
2279	ss->timeout.scale = MLY_TIMEOUT_SECONDS;
2280    } else if (csio->ccb_h.timeout < (60 * 60 * 1000)) {
2281	ss->timeout.value = csio->ccb_h.timeout / (60 * 1000);
2282	ss->timeout.scale = MLY_TIMEOUT_MINUTES;
2283    } else {
2284	ss->timeout.value = csio->ccb_h.timeout / (60 * 60 * 1000);	/* overflow? */
2285	ss->timeout.scale = MLY_TIMEOUT_HOURS;
2286    }
2287    ss->maximum_sense_size = csio->sense_len;
2288    ss->cdb_length = csio->cdb_len;
2289    if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2290	bcopy(csio->cdb_io.cdb_ptr, ss->cdb, csio->cdb_len);
2291    } else {
2292	bcopy(csio->cdb_io.cdb_bytes, ss->cdb, csio->cdb_len);
2293    }
2294
2295    /* give the command to the controller */
2296    if ((error = mly_start(mc))) {
2297	s = splcam();
2298	xpt_freeze_simq(sim, 1);
2299	csio->ccb_h.status |= CAM_REQUEUE_REQ;
2300	sc->mly_qfrzn_cnt++;
2301	splx(s);
2302	return(error);
2303    }
2304
2305    return(0);
2306}
2307
2308/********************************************************************************
2309 * Check for possibly-completed commands.
2310 */
2311static void
2312mly_cam_poll(struct cam_sim *sim)
2313{
2314    struct mly_softc	*sc = cam_sim_softc(sim);
2315
2316    debug_called(2);
2317
2318    mly_done(sc);
2319}
2320
2321/********************************************************************************
2322 * Handle completion of a command - pass results back through the CCB
2323 */
2324static void
2325mly_cam_complete(struct mly_command *mc)
2326{
2327    struct mly_softc		*sc = mc->mc_sc;
2328    struct ccb_scsiio		*csio = (struct ccb_scsiio *)mc->mc_private;
2329    struct scsi_inquiry_data	*inq = (struct scsi_inquiry_data *)csio->data_ptr;
2330    struct mly_btl		*btl;
2331    u_int8_t			cmd;
2332    int				bus, target;
2333    int				s;
2334
2335    debug_called(2);
2336
2337    csio->scsi_status = mc->mc_status;
2338    switch(mc->mc_status) {
2339    case SCSI_STATUS_OK:
2340	/*
2341	 * In order to report logical device type and status, we overwrite
2342	 * the result of the INQUIRY command to logical devices.
2343	 */
2344	bus = csio->ccb_h.sim_priv.entries[0].field;
2345	target = csio->ccb_h.target_id;
2346	/* XXX validate bus/target? */
2347	if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_LOGICAL) {
2348	    if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2349		cmd = *csio->cdb_io.cdb_ptr;
2350	    } else {
2351		cmd = csio->cdb_io.cdb_bytes[0];
2352	    }
2353	    if (cmd == INQUIRY) {
2354		btl = &sc->mly_btl[bus][target];
2355		padstr(inq->vendor, mly_describe_code(mly_table_device_type, btl->mb_type), 8);
2356		padstr(inq->product, mly_describe_code(mly_table_device_state, btl->mb_state), 16);
2357		padstr(inq->revision, "", 4);
2358	    }
2359	}
2360
2361	debug(2, "SCSI_STATUS_OK");
2362	csio->ccb_h.status = CAM_REQ_CMP;
2363	break;
2364
2365    case SCSI_STATUS_CHECK_COND:
2366	debug(1, "SCSI_STATUS_CHECK_COND  sense %d  resid %d", mc->mc_sense, mc->mc_resid);
2367	csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
2368	bzero(&csio->sense_data, SSD_FULL_SIZE);
2369	bcopy(mc->mc_packet, &csio->sense_data, mc->mc_sense);
2370	csio->sense_len = mc->mc_sense;
2371	csio->ccb_h.status |= CAM_AUTOSNS_VALID;
2372	csio->resid = mc->mc_resid;	/* XXX this is a signed value... */
2373	break;
2374
2375    case SCSI_STATUS_BUSY:
2376	debug(1, "SCSI_STATUS_BUSY");
2377	csio->ccb_h.status = CAM_SCSI_BUSY;
2378	break;
2379
2380    default:
2381	debug(1, "unknown status 0x%x", csio->scsi_status);
2382	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2383	break;
2384    }
2385
2386    s = splcam();
2387    if (sc->mly_qfrzn_cnt) {
2388	csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2389	sc->mly_qfrzn_cnt--;
2390    }
2391    splx(s);
2392
2393    xpt_done((union ccb *)csio);
2394    mly_release_command(mc);
2395}
2396
2397/********************************************************************************
2398 * Find a peripheral attahed at (bus),(target)
2399 */
2400static struct cam_periph *
2401mly_find_periph(struct mly_softc *sc, int bus, int target)
2402{
2403    struct cam_periph	*periph;
2404    struct cam_path	*path;
2405    int			status;
2406
2407    status = xpt_create_path(&path, NULL, cam_sim_path(sc->mly_cam_sim[bus]), target, 0);
2408    if (status == CAM_REQ_CMP) {
2409	periph = cam_periph_find(path, NULL);
2410	xpt_free_path(path);
2411    } else {
2412	periph = NULL;
2413    }
2414    return(periph);
2415}
2416
2417/********************************************************************************
2418 * Name the device at (bus)(target)
2419 */
2420static int
2421mly_name_device(struct mly_softc *sc, int bus, int target)
2422{
2423    struct cam_periph	*periph;
2424
2425    if ((periph = mly_find_periph(sc, bus, target)) != NULL) {
2426	sprintf(sc->mly_btl[bus][target].mb_name, "%s%d", periph->periph_name, periph->unit_number);
2427	return(0);
2428    }
2429    sc->mly_btl[bus][target].mb_name[0] = 0;
2430    return(ENOENT);
2431}
2432
2433/********************************************************************************
2434 ********************************************************************************
2435                                                                 Hardware Control
2436 ********************************************************************************
2437 ********************************************************************************/
2438
2439/********************************************************************************
2440 * Handshake with the firmware while the card is being initialised.
2441 */
2442static int
2443mly_fwhandshake(struct mly_softc *sc)
2444{
2445    u_int8_t	error, param0, param1;
2446    int		spinup = 0;
2447
2448    debug_called(1);
2449
2450    /* set HM_STSACK and let the firmware initialise */
2451    MLY_SET_REG(sc, sc->mly_idbr, MLY_HM_STSACK);
2452    DELAY(1000);	/* too short? */
2453
2454    /* if HM_STSACK is still true, the controller is initialising */
2455    if (!MLY_IDBR_TRUE(sc, MLY_HM_STSACK))
2456	return(0);
2457    mly_printf(sc, "controller initialisation started\n");
2458
2459    /* spin waiting for initialisation to finish, or for a message to be delivered */
2460    while (MLY_IDBR_TRUE(sc, MLY_HM_STSACK)) {
2461	/* check for a message */
2462	if (MLY_ERROR_VALID(sc)) {
2463	    error = MLY_GET_REG(sc, sc->mly_error_status) & ~MLY_MSG_EMPTY;
2464	    param0 = MLY_GET_REG(sc, sc->mly_command_mailbox);
2465	    param1 = MLY_GET_REG(sc, sc->mly_command_mailbox + 1);
2466
2467	    switch(error) {
2468	    case MLY_MSG_SPINUP:
2469		if (!spinup) {
2470		    mly_printf(sc, "drive spinup in progress\n");
2471		    spinup = 1;			/* only print this once (should print drive being spun?) */
2472		}
2473		break;
2474	    case MLY_MSG_RACE_RECOVERY_FAIL:
2475		mly_printf(sc, "mirror race recovery failed, one or more drives offline\n");
2476		break;
2477	    case MLY_MSG_RACE_IN_PROGRESS:
2478		mly_printf(sc, "mirror race recovery in progress\n");
2479		break;
2480	    case MLY_MSG_RACE_ON_CRITICAL:
2481		mly_printf(sc, "mirror race recovery on a critical drive\n");
2482		break;
2483	    case MLY_MSG_PARITY_ERROR:
2484		mly_printf(sc, "FATAL MEMORY PARITY ERROR\n");
2485		return(ENXIO);
2486	    default:
2487		mly_printf(sc, "unknown initialisation code 0x%x\n", error);
2488	    }
2489	}
2490    }
2491    return(0);
2492}
2493
2494/********************************************************************************
2495 ********************************************************************************
2496                                                        Debugging and Diagnostics
2497 ********************************************************************************
2498 ********************************************************************************/
2499
2500/********************************************************************************
2501 * Print some information about the controller.
2502 */
2503static void
2504mly_describe_controller(struct mly_softc *sc)
2505{
2506    struct mly_ioctl_getcontrollerinfo	*mi = sc->mly_controllerinfo;
2507
2508    mly_printf(sc, "%16s, %d channel%s, firmware %d.%02d-%d-%02d (%02d%02d%02d%02d), %dMB RAM\n",
2509	       mi->controller_name, mi->physical_channels_present, (mi->physical_channels_present) > 1 ? "s" : "",
2510	       mi->fw_major, mi->fw_minor, mi->fw_turn, mi->fw_build,	/* XXX turn encoding? */
2511	       mi->fw_century, mi->fw_year, mi->fw_month, mi->fw_day,
2512	       mi->memory_size);
2513
2514    if (bootverbose) {
2515	mly_printf(sc, "%s %s (%x), %dMHz %d-bit %.16s\n",
2516		   mly_describe_code(mly_table_oemname, mi->oem_information),
2517		   mly_describe_code(mly_table_controllertype, mi->controller_type), mi->controller_type,
2518		   mi->interface_speed, mi->interface_width, mi->interface_name);
2519	mly_printf(sc, "%dMB %dMHz %d-bit %s%s%s, cache %dMB\n",
2520		   mi->memory_size, mi->memory_speed, mi->memory_width,
2521		   mly_describe_code(mly_table_memorytype, mi->memory_type),
2522		   mi->memory_parity ? "+parity": "",mi->memory_ecc ? "+ECC": "",
2523		   mi->cache_size);
2524	mly_printf(sc, "CPU: %s @ %dMHZ\n",
2525		   mly_describe_code(mly_table_cputype, mi->cpu[0].type), mi->cpu[0].speed);
2526	if (mi->l2cache_size != 0)
2527	    mly_printf(sc, "%dKB L2 cache\n", mi->l2cache_size);
2528	if (mi->exmemory_size != 0)
2529	    mly_printf(sc, "%dMB %dMHz %d-bit private %s%s%s\n",
2530		       mi->exmemory_size, mi->exmemory_speed, mi->exmemory_width,
2531		       mly_describe_code(mly_table_memorytype, mi->exmemory_type),
2532		       mi->exmemory_parity ? "+parity": "",mi->exmemory_ecc ? "+ECC": "");
2533	mly_printf(sc, "battery backup %s\n", mi->bbu_present ? "present" : "not installed");
2534	mly_printf(sc, "maximum data transfer %d blocks, maximum sg entries/command %d\n",
2535		   mi->maximum_block_count, mi->maximum_sg_entries);
2536	mly_printf(sc, "logical devices present/critical/offline %d/%d/%d\n",
2537		   mi->logical_devices_present, mi->logical_devices_critical, mi->logical_devices_offline);
2538	mly_printf(sc, "physical devices present %d\n",
2539		   mi->physical_devices_present);
2540	mly_printf(sc, "physical disks present/offline %d/%d\n",
2541		   mi->physical_disks_present, mi->physical_disks_offline);
2542	mly_printf(sc, "%d physical channel%s, %d virtual channel%s of %d possible\n",
2543		   mi->physical_channels_present, mi->physical_channels_present == 1 ? "" : "s",
2544		   mi->virtual_channels_present, mi->virtual_channels_present == 1 ? "" : "s",
2545		   mi->virtual_channels_possible);
2546	mly_printf(sc, "%d parallel commands supported\n", mi->maximum_parallel_commands);
2547	mly_printf(sc, "%dMB flash ROM, %d of %d maximum cycles\n",
2548		   mi->flash_size, mi->flash_age, mi->flash_maximum_age);
2549    }
2550}
2551
2552#ifdef MLY_DEBUG
2553/********************************************************************************
2554 * Print some controller state
2555 */
2556static void
2557mly_printstate(struct mly_softc *sc)
2558{
2559    mly_printf(sc, "IDBR %02x  ODBR %02x  ERROR %02x  (%x %x %x)\n",
2560		  MLY_GET_REG(sc, sc->mly_idbr),
2561		  MLY_GET_REG(sc, sc->mly_odbr),
2562		  MLY_GET_REG(sc, sc->mly_error_status),
2563		  sc->mly_idbr,
2564		  sc->mly_odbr,
2565		  sc->mly_error_status);
2566    mly_printf(sc, "IMASK %02x  ISTATUS %02x\n",
2567		  MLY_GET_REG(sc, sc->mly_interrupt_mask),
2568		  MLY_GET_REG(sc, sc->mly_interrupt_status));
2569    mly_printf(sc, "COMMAND %02x %02x %02x %02x %02x %02x %02x %02x\n",
2570		  MLY_GET_REG(sc, sc->mly_command_mailbox),
2571		  MLY_GET_REG(sc, sc->mly_command_mailbox + 1),
2572		  MLY_GET_REG(sc, sc->mly_command_mailbox + 2),
2573		  MLY_GET_REG(sc, sc->mly_command_mailbox + 3),
2574		  MLY_GET_REG(sc, sc->mly_command_mailbox + 4),
2575		  MLY_GET_REG(sc, sc->mly_command_mailbox + 5),
2576		  MLY_GET_REG(sc, sc->mly_command_mailbox + 6),
2577		  MLY_GET_REG(sc, sc->mly_command_mailbox + 7));
2578    mly_printf(sc, "STATUS  %02x %02x %02x %02x %02x %02x %02x %02x\n",
2579		  MLY_GET_REG(sc, sc->mly_status_mailbox),
2580		  MLY_GET_REG(sc, sc->mly_status_mailbox + 1),
2581		  MLY_GET_REG(sc, sc->mly_status_mailbox + 2),
2582		  MLY_GET_REG(sc, sc->mly_status_mailbox + 3),
2583		  MLY_GET_REG(sc, sc->mly_status_mailbox + 4),
2584		  MLY_GET_REG(sc, sc->mly_status_mailbox + 5),
2585		  MLY_GET_REG(sc, sc->mly_status_mailbox + 6),
2586		  MLY_GET_REG(sc, sc->mly_status_mailbox + 7));
2587    mly_printf(sc, "        %04x        %08x\n",
2588		  MLY_GET_REG2(sc, sc->mly_status_mailbox),
2589		  MLY_GET_REG4(sc, sc->mly_status_mailbox + 4));
2590}
2591
2592struct mly_softc	*mly_softc0 = NULL;
2593void
2594mly_printstate0(void)
2595{
2596    if (mly_softc0 != NULL)
2597	mly_printstate(mly_softc0);
2598}
2599
2600/********************************************************************************
2601 * Print a command
2602 */
2603static void
2604mly_print_command(struct mly_command *mc)
2605{
2606    struct mly_softc	*sc = mc->mc_sc;
2607
2608    mly_printf(sc, "COMMAND @ %p\n", mc);
2609    mly_printf(sc, "  slot      %d\n", mc->mc_slot);
2610    mly_printf(sc, "  status    0x%x\n", mc->mc_status);
2611    mly_printf(sc, "  sense len %d\n", mc->mc_sense);
2612    mly_printf(sc, "  resid     %d\n", mc->mc_resid);
2613    mly_printf(sc, "  packet    %p/0x%llx\n", mc->mc_packet, mc->mc_packetphys);
2614    if (mc->mc_packet != NULL)
2615	mly_print_packet(mc);
2616    mly_printf(sc, "  data      %p/%d\n", mc->mc_data, mc->mc_length);
2617    mly_printf(sc, "  flags     %b\n", mc->mc_flags, "\20\1busy\2complete\3slotted\4mapped\5datain\6dataout\n");
2618    mly_printf(sc, "  complete  %p\n", mc->mc_complete);
2619    mly_printf(sc, "  private   %p\n", mc->mc_private);
2620}
2621
2622/********************************************************************************
2623 * Print a command packet
2624 */
2625static void
2626mly_print_packet(struct mly_command *mc)
2627{
2628    struct mly_softc			*sc = mc->mc_sc;
2629    struct mly_command_generic		*ge = (struct mly_command_generic *)mc->mc_packet;
2630    struct mly_command_scsi_small	*ss = (struct mly_command_scsi_small *)mc->mc_packet;
2631    struct mly_command_scsi_large	*sl = (struct mly_command_scsi_large *)mc->mc_packet;
2632    struct mly_command_ioctl		*io = (struct mly_command_ioctl *)mc->mc_packet;
2633    int					transfer;
2634
2635    mly_printf(sc, "   command_id           %d\n", ge->command_id);
2636    mly_printf(sc, "   opcode               %d\n", ge->opcode);
2637    mly_printf(sc, "   command_control      fua %d  dpo %d  est %d  dd %s  nas %d ddis %d\n",
2638		  ge->command_control.force_unit_access,
2639		  ge->command_control.disable_page_out,
2640		  ge->command_control.extended_sg_table,
2641		  (ge->command_control.data_direction == MLY_CCB_WRITE) ? "WRITE" : "READ",
2642		  ge->command_control.no_auto_sense,
2643		  ge->command_control.disable_disconnect);
2644    mly_printf(sc, "   data_size            %d\n", ge->data_size);
2645    mly_printf(sc, "   sense_buffer_address 0x%llx\n", ge->sense_buffer_address);
2646    mly_printf(sc, "   lun                  %d\n", ge->addr.phys.lun);
2647    mly_printf(sc, "   target               %d\n", ge->addr.phys.target);
2648    mly_printf(sc, "   channel              %d\n", ge->addr.phys.channel);
2649    mly_printf(sc, "   logical device       %d\n", ge->addr.log.logdev);
2650    mly_printf(sc, "   controller           %d\n", ge->addr.phys.controller);
2651    mly_printf(sc, "   timeout              %d %s\n",
2652		  ge->timeout.value,
2653		  (ge->timeout.scale == MLY_TIMEOUT_SECONDS) ? "seconds" :
2654		  ((ge->timeout.scale == MLY_TIMEOUT_MINUTES) ? "minutes" : "hours"));
2655    mly_printf(sc, "   maximum_sense_size   %d\n", ge->maximum_sense_size);
2656    switch(ge->opcode) {
2657    case MDACMD_SCSIPT:
2658    case MDACMD_SCSI:
2659	mly_printf(sc, "   cdb length           %d\n", ss->cdb_length);
2660	mly_printf(sc, "   cdb                  %*D\n", ss->cdb_length, ss->cdb, " ");
2661	transfer = 1;
2662	break;
2663    case MDACMD_SCSILC:
2664    case MDACMD_SCSILCPT:
2665	mly_printf(sc, "   cdb length           %d\n", sl->cdb_length);
2666	mly_printf(sc, "   cdb                  0x%llx\n", sl->cdb_physaddr);
2667	transfer = 1;
2668	break;
2669    case MDACMD_IOCTL:
2670	mly_printf(sc, "   sub_ioctl            0x%x\n", io->sub_ioctl);
2671	switch(io->sub_ioctl) {
2672	case MDACIOCTL_SETMEMORYMAILBOX:
2673	    mly_printf(sc, "   health_buffer_size   %d\n",
2674			  io->param.setmemorymailbox.health_buffer_size);
2675	    mly_printf(sc, "   health_buffer_phys   0x%llx\n",
2676			  io->param.setmemorymailbox.health_buffer_physaddr);
2677	    mly_printf(sc, "   command_mailbox      0x%llx\n",
2678			  io->param.setmemorymailbox.command_mailbox_physaddr);
2679	    mly_printf(sc, "   status_mailbox       0x%llx\n",
2680			  io->param.setmemorymailbox.status_mailbox_physaddr);
2681	    transfer = 0;
2682	    break;
2683
2684	case MDACIOCTL_SETREALTIMECLOCK:
2685	case MDACIOCTL_GETHEALTHSTATUS:
2686	case MDACIOCTL_GETCONTROLLERINFO:
2687	case MDACIOCTL_GETLOGDEVINFOVALID:
2688	case MDACIOCTL_GETPHYSDEVINFOVALID:
2689	case MDACIOCTL_GETPHYSDEVSTATISTICS:
2690	case MDACIOCTL_GETLOGDEVSTATISTICS:
2691	case MDACIOCTL_GETCONTROLLERSTATISTICS:
2692	case MDACIOCTL_GETBDT_FOR_SYSDRIVE:
2693	case MDACIOCTL_CREATENEWCONF:
2694	case MDACIOCTL_ADDNEWCONF:
2695	case MDACIOCTL_GETDEVCONFINFO:
2696	case MDACIOCTL_GETFREESPACELIST:
2697	case MDACIOCTL_MORE:
2698	case MDACIOCTL_SETPHYSDEVPARAMETER:
2699	case MDACIOCTL_GETPHYSDEVPARAMETER:
2700	case MDACIOCTL_GETLOGDEVPARAMETER:
2701	case MDACIOCTL_SETLOGDEVPARAMETER:
2702	    mly_printf(sc, "   param                %10D\n", io->param.data.param, " ");
2703	    transfer = 1;
2704	    break;
2705
2706	case MDACIOCTL_GETEVENT:
2707	    mly_printf(sc, "   event                %d\n",
2708		       io->param.getevent.sequence_number_low + ((u_int32_t)io->addr.log.logdev << 16));
2709	    transfer = 1;
2710	    break;
2711
2712	case MDACIOCTL_SETRAIDDEVSTATE:
2713	    mly_printf(sc, "   state                %d\n", io->param.setraiddevstate.state);
2714	    transfer = 0;
2715	    break;
2716
2717	case MDACIOCTL_XLATEPHYSDEVTORAIDDEV:
2718	    mly_printf(sc, "   raid_device          %d\n", io->param.xlatephysdevtoraiddev.raid_device);
2719	    mly_printf(sc, "   controller           %d\n", io->param.xlatephysdevtoraiddev.controller);
2720	    mly_printf(sc, "   channel              %d\n", io->param.xlatephysdevtoraiddev.channel);
2721	    mly_printf(sc, "   target               %d\n", io->param.xlatephysdevtoraiddev.target);
2722	    mly_printf(sc, "   lun                  %d\n", io->param.xlatephysdevtoraiddev.lun);
2723	    transfer = 0;
2724	    break;
2725
2726	case MDACIOCTL_GETGROUPCONFINFO:
2727	    mly_printf(sc, "   group                %d\n", io->param.getgroupconfinfo.group);
2728	    transfer = 1;
2729	    break;
2730
2731	case MDACIOCTL_GET_SUBSYSTEM_DATA:
2732	case MDACIOCTL_SET_SUBSYSTEM_DATA:
2733	case MDACIOCTL_STARTDISOCVERY:
2734	case MDACIOCTL_INITPHYSDEVSTART:
2735	case MDACIOCTL_INITPHYSDEVSTOP:
2736	case MDACIOCTL_INITRAIDDEVSTART:
2737	case MDACIOCTL_INITRAIDDEVSTOP:
2738	case MDACIOCTL_REBUILDRAIDDEVSTART:
2739	case MDACIOCTL_REBUILDRAIDDEVSTOP:
2740	case MDACIOCTL_MAKECONSISTENTDATASTART:
2741	case MDACIOCTL_MAKECONSISTENTDATASTOP:
2742	case MDACIOCTL_CONSISTENCYCHECKSTART:
2743	case MDACIOCTL_CONSISTENCYCHECKSTOP:
2744	case MDACIOCTL_RESETDEVICE:
2745	case MDACIOCTL_FLUSHDEVICEDATA:
2746	case MDACIOCTL_PAUSEDEVICE:
2747	case MDACIOCTL_UNPAUSEDEVICE:
2748	case MDACIOCTL_LOCATEDEVICE:
2749	case MDACIOCTL_SETMASTERSLAVEMODE:
2750	case MDACIOCTL_DELETERAIDDEV:
2751	case MDACIOCTL_REPLACEINTERNALDEV:
2752	case MDACIOCTL_CLEARCONF:
2753	case MDACIOCTL_GETCONTROLLERPARAMETER:
2754	case MDACIOCTL_SETCONTRLLERPARAMETER:
2755	case MDACIOCTL_CLEARCONFSUSPMODE:
2756	case MDACIOCTL_STOREIMAGE:
2757	case MDACIOCTL_READIMAGE:
2758	case MDACIOCTL_FLASHIMAGES:
2759	case MDACIOCTL_RENAMERAIDDEV:
2760	default:			/* no idea what to print */
2761	    transfer = 0;
2762	    break;
2763	}
2764	break;
2765
2766    case MDACMD_IOCTLCHECK:
2767    case MDACMD_MEMCOPY:
2768    default:
2769	transfer = 0;
2770	break;	/* print nothing */
2771    }
2772    if (transfer) {
2773	if (ge->command_control.extended_sg_table) {
2774	    mly_printf(sc, "   sg table             0x%llx/%d\n",
2775			  ge->transfer.indirect.table_physaddr[0], ge->transfer.indirect.entries[0]);
2776	} else {
2777	    mly_printf(sc, "   0000                 0x%llx/%lld\n",
2778			  ge->transfer.direct.sg[0].physaddr, ge->transfer.direct.sg[0].length);
2779	    mly_printf(sc, "   0001                 0x%llx/%lld\n",
2780			  ge->transfer.direct.sg[1].physaddr, ge->transfer.direct.sg[1].length);
2781	}
2782    }
2783}
2784
2785/********************************************************************************
2786 * Panic in a slightly informative fashion
2787 */
2788static void
2789mly_panic(struct mly_softc *sc, char *reason)
2790{
2791    mly_printstate(sc);
2792    panic(reason);
2793}
2794
2795/********************************************************************************
2796 * Print queue statistics, callable from DDB.
2797 */
2798void
2799mly_print_controller(int controller)
2800{
2801    struct mly_softc	*sc;
2802
2803    if ((sc = devclass_get_softc(devclass_find("mly"), controller)) == NULL) {
2804	printf("mly: controller %d invalid\n", controller);
2805    } else {
2806	device_printf(sc->mly_dev, "queue    curr max\n");
2807	device_printf(sc->mly_dev, "free     %04d/%04d\n",
2808		      sc->mly_qstat[MLYQ_FREE].q_length, sc->mly_qstat[MLYQ_FREE].q_max);
2809	device_printf(sc->mly_dev, "busy     %04d/%04d\n",
2810		      sc->mly_qstat[MLYQ_BUSY].q_length, sc->mly_qstat[MLYQ_BUSY].q_max);
2811	device_printf(sc->mly_dev, "complete %04d/%04d\n",
2812		      sc->mly_qstat[MLYQ_COMPLETE].q_length, sc->mly_qstat[MLYQ_COMPLETE].q_max);
2813    }
2814}
2815#endif
2816
2817
2818/********************************************************************************
2819 ********************************************************************************
2820                                                         Control device interface
2821 ********************************************************************************
2822 ********************************************************************************/
2823
2824/********************************************************************************
2825 * Accept an open operation on the control device.
2826 */
2827static int
2828mly_user_open(dev_t dev, int flags, int fmt, struct thread *td)
2829{
2830    int			unit = minor(dev);
2831    struct mly_softc	*sc = devclass_get_softc(devclass_find("mly"), unit);
2832
2833    sc->mly_state |= MLY_STATE_OPEN;
2834    return(0);
2835}
2836
2837/********************************************************************************
2838 * Accept the last close on the control device.
2839 */
2840static int
2841mly_user_close(dev_t dev, int flags, int fmt, struct thread *td)
2842{
2843    int			unit = minor(dev);
2844    struct mly_softc	*sc = devclass_get_softc(devclass_find("mly"), unit);
2845
2846    sc->mly_state &= ~MLY_STATE_OPEN;
2847    return (0);
2848}
2849
2850/********************************************************************************
2851 * Handle controller-specific control operations.
2852 */
2853static int
2854mly_user_ioctl(dev_t dev, u_long cmd, caddr_t addr,
2855				int32_t flag, struct thread *td)
2856{
2857    struct mly_softc		*sc = (struct mly_softc *)dev->si_drv1;
2858    struct mly_user_command	*uc = (struct mly_user_command *)addr;
2859    struct mly_user_health	*uh = (struct mly_user_health *)addr;
2860
2861    switch(cmd) {
2862    case MLYIO_COMMAND:
2863	return(mly_user_command(sc, uc));
2864    case MLYIO_HEALTH:
2865	return(mly_user_health(sc, uh));
2866    default:
2867	return(ENOIOCTL);
2868    }
2869}
2870
2871/********************************************************************************
2872 * Execute a command passed in from userspace.
2873 *
2874 * The control structure contains the actual command for the controller, as well
2875 * as the user-space data pointer and data size, and an optional sense buffer
2876 * size/pointer.  On completion, the data size is adjusted to the command
2877 * residual, and the sense buffer size to the size of the returned sense data.
2878 *
2879 */
2880static int
2881mly_user_command(struct mly_softc *sc, struct mly_user_command *uc)
2882{
2883    struct mly_command	*mc;
2884    int			error, s;
2885
2886    /* allocate a command */
2887    if (mly_alloc_command(sc, &mc)) {
2888	error = ENOMEM;
2889	goto out;		/* XXX Linux version will wait for a command */
2890    }
2891
2892    /* handle data size/direction */
2893    mc->mc_length = (uc->DataTransferLength >= 0) ? uc->DataTransferLength : -uc->DataTransferLength;
2894    if (mc->mc_length > 0) {
2895	if ((mc->mc_data = malloc(mc->mc_length, M_DEVBUF, M_NOWAIT)) == NULL) {
2896	    error = ENOMEM;
2897	    goto out;
2898	}
2899    }
2900    if (uc->DataTransferLength > 0) {
2901	mc->mc_flags |= MLY_CMD_DATAIN;
2902	bzero(mc->mc_data, mc->mc_length);
2903    }
2904    if (uc->DataTransferLength < 0) {
2905	mc->mc_flags |= MLY_CMD_DATAOUT;
2906	if ((error = copyin(uc->DataTransferBuffer, mc->mc_data, mc->mc_length)) != 0)
2907	    goto out;
2908    }
2909
2910    /* copy the controller command */
2911    bcopy(&uc->CommandMailbox, mc->mc_packet, sizeof(uc->CommandMailbox));
2912
2913    /* clear command completion handler so that we get woken up */
2914    mc->mc_complete = NULL;
2915
2916    /* execute the command */
2917    if ((error = mly_start(mc)) != 0)
2918	goto out;
2919    s = splcam();
2920    while (!(mc->mc_flags & MLY_CMD_COMPLETE))
2921	tsleep(mc, PRIBIO, "mlyioctl", 0);
2922    splx(s);
2923
2924    /* return the data to userspace */
2925    if (uc->DataTransferLength > 0)
2926	if ((error = copyout(mc->mc_data, uc->DataTransferBuffer, mc->mc_length)) != 0)
2927	    goto out;
2928
2929    /* return the sense buffer to userspace */
2930    if ((uc->RequestSenseLength > 0) && (mc->mc_sense > 0)) {
2931	if ((error = copyout(mc->mc_packet, uc->RequestSenseBuffer,
2932			     min(uc->RequestSenseLength, mc->mc_sense))) != 0)
2933	    goto out;
2934    }
2935
2936    /* return command results to userspace (caller will copy out) */
2937    uc->DataTransferLength = mc->mc_resid;
2938    uc->RequestSenseLength = min(uc->RequestSenseLength, mc->mc_sense);
2939    uc->CommandStatus = mc->mc_status;
2940    error = 0;
2941
2942 out:
2943    if (mc->mc_data != NULL)
2944	free(mc->mc_data, M_DEVBUF);
2945    if (mc != NULL)
2946	mly_release_command(mc);
2947    return(error);
2948}
2949
2950/********************************************************************************
2951 * Return health status to userspace.  If the health change index in the user
2952 * structure does not match that currently exported by the controller, we
2953 * return the current status immediately.  Otherwise, we block until either
2954 * interrupted or new status is delivered.
2955 */
2956static int
2957mly_user_health(struct mly_softc *sc, struct mly_user_health *uh)
2958{
2959    struct mly_health_status		mh;
2960    int					error, s;
2961
2962    /* fetch the current health status from userspace */
2963    if ((error = copyin(uh->HealthStatusBuffer, &mh, sizeof(mh))) != 0)
2964	return(error);
2965
2966    /* spin waiting for a status update */
2967    s = splcam();
2968    error = EWOULDBLOCK;
2969    while ((error != 0) && (sc->mly_event_change == mh.change_counter))
2970	error = tsleep(&sc->mly_event_change, PRIBIO | PCATCH, "mlyhealth", 0);
2971    splx(s);
2972
2973    /* copy the controller's health status buffer out (there is a race here if it changes again) */
2974    error = copyout(&sc->mly_mmbox->mmm_health.status, uh->HealthStatusBuffer,
2975		    sizeof(uh->HealthStatusBuffer));
2976    return(error);
2977}
2978
2979static int
2980mly_timeout(struct mly_softc *sc)
2981{
2982	struct mly_command *mc;
2983	int deadline;
2984
2985	deadline = time_second - MLY_CMD_TIMEOUT;
2986	TAILQ_FOREACH(mc, &sc->mly_busy, mc_link) {
2987		if ((mc->mc_timestamp < deadline)) {
2988			device_printf(sc->mly_dev,
2989			    "COMMAND %p TIMEOUT AFTER %d SECONDS\n", mc,
2990			    (int)(time_second - mc->mc_timestamp));
2991		}
2992	}
2993
2994	timeout((timeout_t *)mly_timeout, sc, MLY_CMD_TIMEOUT * hz);
2995
2996	return (0);
2997}
2998