2 * Copyright (c) 2000, 2001 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: src/sys/dev/mly/mly.c,v 1.3.2.3 2001/03/05 20:17:24 msmith Exp $
28 * $DragonFly: src/sys/dev/raid/mly/mly.c,v 1.11 2004/09/15 14:24:33 joerg Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
37 #include <sys/ctype.h>
38 #include <sys/ioccom.h>
41 #include <machine/bus_memio.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
46 #include <bus/cam/scsi/scsi_all.h>
51 #define MLY_DEFINE_TABLES
52 #include "mly_tables.h"
54 static int mly_get_controllerinfo(struct mly_softc *sc);
55 static void mly_scan_devices(struct mly_softc *sc);
56 static void mly_rescan_btl(struct mly_softc *sc, int bus, int target);
57 static void mly_complete_rescan(struct mly_command *mc);
58 static int mly_get_eventstatus(struct mly_softc *sc);
59 static int mly_enable_mmbox(struct mly_softc *sc);
60 static int mly_flush(struct mly_softc *sc);
61 static int mly_ioctl(struct mly_softc *sc, struct mly_command_ioctl *ioctl, void **data,
62 size_t datasize, u_int8_t *status, void *sense_buffer, size_t *sense_length);
63 static void mly_fetch_event(struct mly_softc *sc);
64 static void mly_complete_event(struct mly_command *mc);
65 static void mly_process_event(struct mly_softc *sc, struct mly_event *me);
66 static void mly_periodic(void *data);
68 static int mly_immediate_command(struct mly_command *mc);
69 static int mly_start(struct mly_command *mc);
70 static void mly_complete(void *context, int pending);
72 static void mly_alloc_commands_map(void *arg, bus_dma_segment_t *segs, int nseg, int error);
73 static int mly_alloc_commands(struct mly_softc *sc);
74 static void mly_map_command(struct mly_command *mc);
75 static void mly_unmap_command(struct mly_command *mc);
77 static int mly_fwhandshake(struct mly_softc *sc);
79 static void mly_describe_controller(struct mly_softc *sc);
81 static void mly_printstate(struct mly_softc *sc);
82 static void mly_print_command(struct mly_command *mc);
83 static void mly_print_packet(struct mly_command *mc);
84 static void mly_panic(struct mly_softc *sc, char *reason);
86 void mly_print_controller(int controller);
88 static d_open_t mly_user_open;
89 static d_close_t mly_user_close;
90 static d_ioctl_t mly_user_ioctl;
91 static int mly_user_command(struct mly_softc *sc, struct mly_user_command *uc);
92 static int mly_user_health(struct mly_softc *sc, struct mly_user_health *uh);
94 #define MLY_CDEV_MAJOR 158
96 static struct cdevsw mly_cdevsw = {
98 /* cmaj */ MLY_CDEV_MAJOR,
115 /********************************************************************************
116 ********************************************************************************
118 ********************************************************************************
119 ********************************************************************************/
121 /********************************************************************************
122 * Initialise the controller and softc
125 mly_attach(struct mly_softc *sc)
131 callout_init(&sc->mly_periodic);
134 * Initialise per-controller queues.
139 mly_initq_complete(sc);
141 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
143 * Initialise command-completion task.
145 TASK_INIT(&sc->mly_task_complete, 0, mly_complete, sc);
148 /* disable interrupts before we start talking to the controller */
149 MLY_MASK_INTERRUPTS(sc);
152 * Wait for the controller to come ready, handshake with the firmware if required.
153 * This is typically only necessary on platforms where the controller BIOS does not
156 if ((error = mly_fwhandshake(sc)))
160 * Allocate command buffers
162 if ((error = mly_alloc_commands(sc)))
166 * Obtain controller feature information
168 if ((error = mly_get_controllerinfo(sc)))
172 * Get the current event counter for health purposes, populate the initial
173 * health status buffer.
175 if ((error = mly_get_eventstatus(sc)))
179 * Enable memory-mailbox mode
181 if ((error = mly_enable_mmbox(sc)))
187 if ((error = mly_cam_attach(sc)))
191 * Print a little information about the controller
193 mly_describe_controller(sc);
196 * Mark all attached devices for rescan
198 mly_scan_devices(sc);
201 * Instigate the first status poll immediately. Rescan completions won't
202 * happen until interrupts are enabled, which should still be before
203 * the SCSI subsystem gets to us. (XXX assuming CAM and interrupt-driven
206 mly_periodic((void *)sc);
209 * Create the control device.
211 cdevsw_add(&mly_cdevsw, -1, device_get_unit(sc->mly_dev));
212 sc->mly_dev_t = make_dev(&mly_cdevsw, device_get_unit(sc->mly_dev),
213 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
214 "mly%d", device_get_unit(sc->mly_dev));
215 sc->mly_dev_t->si_drv1 = sc;
217 /* enable interrupts now */
218 MLY_UNMASK_INTERRUPTS(sc);
223 /********************************************************************************
224 * Bring the controller to a state where it can be safely left alone.
227 mly_detach(struct mly_softc *sc)
232 /* kill the periodic event */
233 callout_stop(&sc->mly_periodic);
235 sc->mly_state |= MLY_STATE_SUSPEND;
237 /* flush controller */
238 mly_printf(sc, "flushing cache...");
239 printf("%s\n", mly_flush(sc) ? "failed" : "done");
241 MLY_MASK_INTERRUPTS(sc);
244 /********************************************************************************
245 ********************************************************************************
247 ********************************************************************************
248 ********************************************************************************/
250 /********************************************************************************
251 * Fill in the mly_controllerinfo and mly_controllerparam fields in the softc.
254 mly_get_controllerinfo(struct mly_softc *sc)
256 struct mly_command_ioctl mci;
262 if (sc->mly_controllerinfo != NULL)
263 free(sc->mly_controllerinfo, M_DEVBUF);
265 /* build the getcontrollerinfo ioctl and send it */
266 bzero(&mci, sizeof(mci));
267 sc->mly_controllerinfo = NULL;
268 mci.sub_ioctl = MDACIOCTL_GETCONTROLLERINFO;
269 if ((error = mly_ioctl(sc, &mci, (void **)&sc->mly_controllerinfo, sizeof(*sc->mly_controllerinfo),
270 &status, NULL, NULL)))
275 if (sc->mly_controllerparam != NULL)
276 free(sc->mly_controllerparam, M_DEVBUF);
278 /* build the getcontrollerparameter ioctl and send it */
279 bzero(&mci, sizeof(mci));
280 sc->mly_controllerparam = NULL;
281 mci.sub_ioctl = MDACIOCTL_GETCONTROLLERPARAMETER;
282 if ((error = mly_ioctl(sc, &mci, (void **)&sc->mly_controllerparam, sizeof(*sc->mly_controllerparam),
283 &status, NULL, NULL)))
291 /********************************************************************************
292 * Schedule all possible devices for a rescan.
296 mly_scan_devices(struct mly_softc *sc)
298 int bus, target, nchn;
303 * Clear any previous BTL information.
305 bzero(&sc->mly_btl, sizeof(sc->mly_btl));
308 * Mark all devices as requiring a rescan, and let the early periodic scan collect them.
310 nchn = sc->mly_controllerinfo->physical_channels_present +
311 sc->mly_controllerinfo->virtual_channels_present;
312 for (bus = 0; bus < nchn; bus++)
313 for (target = 0; target < MLY_MAX_TARGETS; target++)
314 sc->mly_btl[bus][target].mb_flags = MLY_BTL_RESCAN;
318 /********************************************************************************
319 * Rescan a device, possibly as a consequence of getting an event which suggests
320 * that it may have changed.
323 mly_rescan_btl(struct mly_softc *sc, int bus, int target)
325 struct mly_command *mc;
326 struct mly_command_ioctl *mci;
332 if (mly_alloc_command(sc, &mc))
333 return; /* we'll be retried soon */
335 /* set up the data buffer */
336 mc->mc_data = malloc(sizeof(union mly_devinfo), M_DEVBUF, M_INTWAIT | M_ZERO);
337 mc->mc_flags |= MLY_CMD_DATAIN;
338 mc->mc_complete = mly_complete_rescan;
340 sc->mly_btl[bus][target].mb_flags &= ~MLY_BTL_RESCAN;
345 * At this point we are committed to sending this request, as it
346 * will be the only one constructed for this particular update.
348 mci = (struct mly_command_ioctl *)&mc->mc_packet->ioctl;
349 mci->opcode = MDACMD_IOCTL;
350 mci->addr.phys.controller = 0;
351 mci->timeout.value = 30;
352 mci->timeout.scale = MLY_TIMEOUT_SECONDS;
353 if (bus >= sc->mly_controllerinfo->physical_channels_present) {
354 mc->mc_length = mci->data_size = sizeof(struct mly_ioctl_getlogdevinfovalid);
355 mci->sub_ioctl = MDACIOCTL_GETLOGDEVINFOVALID;
356 mci->addr.log.logdev = ((bus - sc->mly_controllerinfo->physical_channels_present) * MLY_MAX_TARGETS)
358 debug(2, "logical device %d", mci->addr.log.logdev);
360 mc->mc_length = mci->data_size = sizeof(struct mly_ioctl_getphysdevinfovalid);
361 mci->sub_ioctl = MDACIOCTL_GETPHYSDEVINFOVALID;
362 mci->addr.phys.lun = 0;
363 mci->addr.phys.target = target;
364 mci->addr.phys.channel = bus;
365 debug(2, "physical device %d:%d", mci->addr.phys.channel, mci->addr.phys.target);
369 * Use the ready queue to get this command dispatched.
371 mly_enqueue_ready(mc);
375 /********************************************************************************
376 * Handle the completion of a rescan operation
379 mly_complete_rescan(struct mly_command *mc)
381 struct mly_softc *sc = mc->mc_sc;
382 struct mly_ioctl_getlogdevinfovalid *ldi;
383 struct mly_ioctl_getphysdevinfovalid *pdi;
388 /* iff the command completed OK, we should use the result to update our data */
389 if (mc->mc_status == 0) {
390 if (mc->mc_length == sizeof(*ldi)) {
391 ldi = (struct mly_ioctl_getlogdevinfovalid *)mc->mc_data;
392 bus = MLY_LOGDEV_BUS(sc, ldi->logical_device_number);
393 target = MLY_LOGDEV_TARGET(ldi->logical_device_number);
394 sc->mly_btl[bus][target].mb_flags = MLY_BTL_LOGICAL; /* clears all other flags */
395 sc->mly_btl[bus][target].mb_type = ldi->raid_level;
396 sc->mly_btl[bus][target].mb_state = ldi->state;
397 debug(2, "BTL rescan for %d returns %s, %s", ldi->logical_device_number,
398 mly_describe_code(mly_table_device_type, ldi->raid_level),
399 mly_describe_code(mly_table_device_state, ldi->state));
400 } else if (mc->mc_length == sizeof(*pdi)) {
401 pdi = (struct mly_ioctl_getphysdevinfovalid *)mc->mc_data;
403 target = pdi->target;
404 sc->mly_btl[bus][target].mb_flags = MLY_BTL_PHYSICAL; /* clears all other flags */
405 sc->mly_btl[bus][target].mb_type = MLY_DEVICE_TYPE_PHYSICAL;
406 sc->mly_btl[bus][target].mb_state = pdi->state;
407 sc->mly_btl[bus][target].mb_speed = pdi->speed;
408 sc->mly_btl[bus][target].mb_width = pdi->width;
409 if (pdi->state != MLY_DEVICE_STATE_UNCONFIGURED)
410 sc->mly_btl[bus][target].mb_flags |= MLY_BTL_PROTECTED;
411 debug(2, "BTL rescan for %d:%d returns %s", bus, target,
412 mly_describe_code(mly_table_device_state, pdi->state));
414 mly_printf(sc, "BTL rescan result corrupted\n");
418 * A request sent for a device beyond the last device present will fail.
419 * We don't care about this, so we do nothing about it.
422 free(mc->mc_data, M_DEVBUF);
423 mly_release_command(mc);
426 /********************************************************************************
427 * Get the current health status and set the 'next event' counter to suit.
430 mly_get_eventstatus(struct mly_softc *sc)
432 struct mly_command_ioctl mci;
433 struct mly_health_status *mh;
437 /* build the gethealthstatus ioctl and send it */
438 bzero(&mci, sizeof(mci));
440 mci.sub_ioctl = MDACIOCTL_GETHEALTHSTATUS;
442 if ((error = mly_ioctl(sc, &mci, (void **)&mh, sizeof(*mh), &status, NULL, NULL)))
447 /* get the event counter */
448 sc->mly_event_change = mh->change_counter;
449 sc->mly_event_waiting = mh->next_event;
450 sc->mly_event_counter = mh->next_event;
452 /* save the health status into the memory mailbox */
453 bcopy(mh, &sc->mly_mmbox->mmm_health.status, sizeof(*mh));
455 debug(1, "initial change counter %d, event counter %d", mh->change_counter, mh->next_event);
461 /********************************************************************************
462 * Enable the memory mailbox mode.
465 mly_enable_mmbox(struct mly_softc *sc)
467 struct mly_command_ioctl mci;
468 u_int8_t *sp, status;
473 /* build the ioctl and send it */
474 bzero(&mci, sizeof(mci));
475 mci.sub_ioctl = MDACIOCTL_SETMEMORYMAILBOX;
476 /* set buffer addresses */
477 mci.param.setmemorymailbox.command_mailbox_physaddr =
478 sc->mly_mmbox_busaddr + offsetof(struct mly_mmbox, mmm_command);
479 mci.param.setmemorymailbox.status_mailbox_physaddr =
480 sc->mly_mmbox_busaddr + offsetof(struct mly_mmbox, mmm_status);
481 mci.param.setmemorymailbox.health_buffer_physaddr =
482 sc->mly_mmbox_busaddr + offsetof(struct mly_mmbox, mmm_health);
484 /* set buffer sizes - abuse of data_size field is revolting */
485 sp = (u_int8_t *)&mci.data_size;
486 sp[0] = ((sizeof(union mly_command_packet) * MLY_MMBOX_COMMANDS) / 1024);
487 sp[1] = (sizeof(union mly_status_packet) * MLY_MMBOX_STATUS) / 1024;
488 mci.param.setmemorymailbox.health_buffer_size = sizeof(union mly_health_region) / 1024;
490 debug(1, "memory mailbox at %p (0x%llx/%d 0x%llx/%d 0x%llx/%d", sc->mly_mmbox,
491 mci.param.setmemorymailbox.command_mailbox_physaddr, sp[0],
492 mci.param.setmemorymailbox.status_mailbox_physaddr, sp[1],
493 mci.param.setmemorymailbox.health_buffer_physaddr,
494 mci.param.setmemorymailbox.health_buffer_size);
496 if ((error = mly_ioctl(sc, &mci, NULL, 0, &status, NULL, NULL)))
500 sc->mly_state |= MLY_STATE_MMBOX_ACTIVE;
501 debug(1, "memory mailbox active");
505 /********************************************************************************
506 * Flush all pending I/O from the controller.
509 mly_flush(struct mly_softc *sc)
511 struct mly_command_ioctl mci;
517 /* build the ioctl */
518 bzero(&mci, sizeof(mci));
519 mci.sub_ioctl = MDACIOCTL_FLUSHDEVICEDATA;
520 mci.param.deviceoperation.operation_device = MLY_OPDEVICE_PHYSICAL_CONTROLLER;
522 /* pass it off to the controller */
523 if ((error = mly_ioctl(sc, &mci, NULL, 0, &status, NULL, NULL)))
526 return((status == 0) ? 0 : EIO);
529 /********************************************************************************
530 * Perform an ioctl command.
532 * If (data) is not NULL, the command requires data transfer. If (*data) is NULL
533 * the command requires data transfer from the controller, and we will allocate
534 * a buffer for it. If (*data) is not NULL, the command requires data transfer
537 * XXX passing in the whole ioctl structure is ugly. Better ideas?
539 * XXX we don't even try to handle the case where datasize > 4k. We should.
542 mly_ioctl(struct mly_softc *sc, struct mly_command_ioctl *ioctl, void **data, size_t datasize,
543 u_int8_t *status, void *sense_buffer, size_t *sense_length)
545 struct mly_command *mc;
546 struct mly_command_ioctl *mci;
552 if (mly_alloc_command(sc, &mc)) {
557 /* copy the ioctl structure, but save some important fields and then fixup */
558 mci = &mc->mc_packet->ioctl;
559 ioctl->sense_buffer_address = mci->sense_buffer_address;
560 ioctl->maximum_sense_size = mci->maximum_sense_size;
562 mci->opcode = MDACMD_IOCTL;
563 mci->timeout.value = 30;
564 mci->timeout.scale = MLY_TIMEOUT_SECONDS;
566 /* handle the data buffer */
569 /* allocate data buffer */
570 mc->mc_data = malloc(datasize, M_DEVBUF, M_INTWAIT);
571 mc->mc_flags |= MLY_CMD_DATAIN;
574 mc->mc_flags |= MLY_CMD_DATAOUT;
576 mc->mc_length = datasize;
577 mc->mc_packet->generic.data_size = datasize;
580 /* run the command */
581 if ((error = mly_immediate_command(mc)))
584 /* clean up and return any data */
585 *status = mc->mc_status;
586 if ((mc->mc_sense > 0) && (sense_buffer != NULL)) {
587 bcopy(mc->mc_packet, sense_buffer, mc->mc_sense);
588 *sense_length = mc->mc_sense;
592 /* should we return a data pointer? */
593 if ((data != NULL) && (*data == NULL))
596 /* command completed OK */
601 /* do we need to free a data buffer we allocated? */
602 if (error && (mc->mc_data != NULL) && (*data == NULL))
603 free(mc->mc_data, M_DEVBUF);
604 mly_release_command(mc);
609 /********************************************************************************
610 * Fetch one event from the controller.
613 mly_fetch_event(struct mly_softc *sc)
615 struct mly_command *mc;
616 struct mly_command_ioctl *mci;
624 if (mly_alloc_command(sc, &mc))
625 return; /* we'll get retried the next time a command completes */
627 /* set up the data buffer */
628 mc->mc_data = malloc(sizeof(struct mly_event), M_DEVBUF, M_INTWAIT|M_ZERO);
629 mc->mc_length = sizeof(struct mly_event);
630 mc->mc_flags |= MLY_CMD_DATAIN;
631 mc->mc_complete = mly_complete_event;
634 * Get an event number to fetch. It's possible that we've raced with another
635 * context for the last event, in which case there will be no more events.
638 if (sc->mly_event_counter == sc->mly_event_waiting) {
639 mly_release_command(mc);
643 event = sc->mly_event_counter++;
649 * At this point we are committed to sending this request, as it
650 * will be the only one constructed for this particular event number.
652 mci = (struct mly_command_ioctl *)&mc->mc_packet->ioctl;
653 mci->opcode = MDACMD_IOCTL;
654 mci->data_size = sizeof(struct mly_event);
655 mci->addr.phys.lun = (event >> 16) & 0xff;
656 mci->addr.phys.target = (event >> 24) & 0xff;
657 mci->addr.phys.channel = 0;
658 mci->addr.phys.controller = 0;
659 mci->timeout.value = 30;
660 mci->timeout.scale = MLY_TIMEOUT_SECONDS;
661 mci->sub_ioctl = MDACIOCTL_GETEVENT;
662 mci->param.getevent.sequence_number_low = event & 0xffff;
664 debug(2, "fetch event %u", event);
667 * Use the ready queue to get this command dispatched.
669 mly_enqueue_ready(mc);
673 /********************************************************************************
674 * Handle the completion of an event poll.
676 * Note that we don't actually have to instigate another poll; the completion of
677 * this command will trigger that if there are any more events to poll for.
680 mly_complete_event(struct mly_command *mc)
682 struct mly_softc *sc = mc->mc_sc;
683 struct mly_event *me = (struct mly_event *)mc->mc_data;
688 * If the event was successfully fetched, process it.
690 if (mc->mc_status == SCSI_STATUS_OK) {
691 mly_process_event(sc, me);
694 mly_release_command(mc);
697 /********************************************************************************
698 * Process a controller event.
701 mly_process_event(struct mly_softc *sc, struct mly_event *me)
703 struct scsi_sense_data *ssd = (struct scsi_sense_data *)&me->sense[0];
705 int bus, target, event, class, action;
708 * Errors can be reported using vendor-unique sense data. In this case, the
709 * event code will be 0x1c (Request sense data present), the sense key will
710 * be 0x09 (vendor specific), the MSB of the ASC will be set, and the
711 * actual event code will be a 16-bit value comprised of the ASCQ (low byte)
712 * and low seven bits of the ASC (low seven bits of the high byte).
714 if ((me->code == 0x1c) &&
715 ((ssd->flags & SSD_KEY) == SSD_KEY_Vendor_Specific) &&
716 (ssd->add_sense_code & 0x80)) {
717 event = ((int)(ssd->add_sense_code & ~0x80) << 8) + ssd->add_sense_code_qual;
722 /* look up event, get codes */
723 fp = mly_describe_code(mly_table_event, event);
725 debug(2, "Event %d code 0x%x", me->sequence_number, me->code);
729 if (isupper(class) && bootverbose)
730 class = tolower(class);
732 /* get action code, text string */
737 * Print some information about the event.
739 * This code uses a table derived from the corresponding portion of the Linux
740 * driver, and thus the parser is very similar.
743 case 'p': /* error on physical device */
744 mly_printf(sc, "physical device %d:%d %s\n", me->channel, me->target, tp);
746 sc->mly_btl[me->channel][me->target].mb_flags |= MLY_BTL_RESCAN;
748 case 'l': /* error on logical unit */
749 case 'm': /* message about logical unit */
750 bus = MLY_LOGDEV_BUS(sc, me->lun);
751 target = MLY_LOGDEV_TARGET(me->lun);
752 mly_name_device(sc, bus, target);
753 mly_printf(sc, "logical device %d (%s) %s\n", me->lun, sc->mly_btl[bus][target].mb_name, tp);
755 sc->mly_btl[bus][target].mb_flags |= MLY_BTL_RESCAN;
758 case 's': /* report of sense data */
759 if (((ssd->flags & SSD_KEY) == SSD_KEY_NO_SENSE) ||
760 (((ssd->flags & SSD_KEY) == SSD_KEY_NOT_READY) &&
761 (ssd->add_sense_code == 0x04) &&
762 ((ssd->add_sense_code_qual == 0x01) || (ssd->add_sense_code_qual == 0x02))))
763 break; /* ignore NO_SENSE or NOT_READY in one case */
765 mly_printf(sc, "physical device %d:%d %s\n", me->channel, me->target, tp);
766 mly_printf(sc, " sense key %d asc %02x ascq %02x\n",
767 ssd->flags & SSD_KEY, ssd->add_sense_code, ssd->add_sense_code_qual);
768 mly_printf(sc, " info %4D csi %4D\n", ssd->info, "", ssd->cmd_spec_info, "");
770 sc->mly_btl[me->channel][me->target].mb_flags |= MLY_BTL_RESCAN;
773 mly_printf(sc, tp, me->target, me->lun);
776 mly_printf(sc, "controller %s\n", tp);
779 mly_printf(sc, "%s - %d\n", tp, me->code);
781 default: /* probably a 'noisy' event being ignored */
786 /********************************************************************************
787 * Perform periodic activities.
790 mly_periodic(void *data)
792 struct mly_softc *sc = (struct mly_softc *)data;
793 int nchn, bus, target;
800 nchn = sc->mly_controllerinfo->physical_channels_present +
801 sc->mly_controllerinfo->virtual_channels_present;
802 for (bus = 0; bus < nchn; bus++) {
803 for (target = 0; target < MLY_MAX_TARGETS; target++) {
805 /* ignore the controller in this scan */
806 if (target == sc->mly_controllerparam->initiator_id)
809 /* perform device rescan? */
810 if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_RESCAN)
811 mly_rescan_btl(sc, bus, target);
815 callout_reset(&sc->mly_periodic, hz, mly_periodic, sc);
818 /********************************************************************************
819 ********************************************************************************
821 ********************************************************************************
822 ********************************************************************************/
824 /********************************************************************************
825 * Run a command and wait for it to complete.
829 mly_immediate_command(struct mly_command *mc)
831 struct mly_softc *sc = mc->mc_sc;
836 /* spinning at splcam is ugly, but we're only used during controller init */
838 if ((error = mly_start(mc)))
841 if (sc->mly_state & MLY_STATE_INTERRUPTS_ON) {
842 /* sleep on the command */
843 while(!(mc->mc_flags & MLY_CMD_COMPLETE)) {
844 tsleep(mc, 0, "mlywait", 0);
847 /* spin and collect status while we do */
848 while(!(mc->mc_flags & MLY_CMD_COMPLETE)) {
856 /********************************************************************************
857 * Start as much queued I/O as possible on the controller
860 mly_startio(struct mly_softc *sc)
862 struct mly_command *mc;
868 /* try for a ready command */
869 mc = mly_dequeue_ready(sc);
871 /* try to build a command from a queued ccb */
873 mly_cam_command(sc, &mc);
875 /* no command == nothing to do */
879 /* try to post the command */
881 /* controller busy, or no resources - defer for later */
882 mly_requeue_ready(mc);
888 /********************************************************************************
889 * Deliver a command to the controller; allocate controller resources at the
893 mly_start(struct mly_command *mc)
895 struct mly_softc *sc = mc->mc_sc;
896 union mly_command_packet *pkt;
902 * Set the command up for delivery to the controller.
905 mc->mc_packet->generic.command_id = mc->mc_slot;
910 * Do we have to use the hardware mailbox?
912 if (!(sc->mly_state & MLY_STATE_MMBOX_ACTIVE)) {
914 * Check to see if the controller is ready for us.
916 if (MLY_IDBR_TRUE(sc, MLY_HM_CMDSENT)) {
920 mc->mc_flags |= MLY_CMD_BUSY;
923 * It's ready, send the command.
925 MLY_SET_MBOX(sc, sc->mly_command_mailbox, &mc->mc_packetphys);
926 MLY_SET_REG(sc, sc->mly_idbr, MLY_HM_CMDSENT);
928 } else { /* use memory-mailbox mode */
930 pkt = &sc->mly_mmbox->mmm_command[sc->mly_mmbox_command_index];
932 /* check to see if the next index is free yet */
933 if (pkt->mmbox.flag != 0) {
937 mc->mc_flags |= MLY_CMD_BUSY;
939 /* copy in new command */
940 bcopy(mc->mc_packet->mmbox.data, pkt->mmbox.data, sizeof(pkt->mmbox.data));
941 /* barrier to ensure completion of previous write before we write the flag */
942 bus_space_barrier(NULL, NULL, 0, 0, BUS_SPACE_BARRIER_WRITE); /* tag/handle? */
944 pkt->mmbox.flag = mc->mc_packet->mmbox.flag;
945 /* barrier to ensure completion of previous write before we notify the controller */
946 bus_space_barrier(NULL, NULL, 0, 0, BUS_SPACE_BARRIER_WRITE); /* tag/handle */
948 /* signal controller, update index */
949 MLY_SET_REG(sc, sc->mly_idbr, MLY_AM_CMDSENT);
950 sc->mly_mmbox_command_index = (sc->mly_mmbox_command_index + 1) % MLY_MMBOX_COMMANDS;
953 mly_enqueue_busy(mc);
958 /********************************************************************************
959 * Pick up command status from the controller, schedule a completion event
962 mly_done(struct mly_softc *sc)
964 struct mly_command *mc;
965 union mly_status_packet *sp;
972 /* pick up hardware-mailbox commands */
973 if (MLY_ODBR_TRUE(sc, MLY_HM_STSREADY)) {
974 slot = MLY_GET_REG2(sc, sc->mly_status_mailbox);
975 if (slot < MLY_SLOT_MAX) {
976 mc = &sc->mly_command[slot - MLY_SLOT_START];
977 mc->mc_status = MLY_GET_REG(sc, sc->mly_status_mailbox + 2);
978 mc->mc_sense = MLY_GET_REG(sc, sc->mly_status_mailbox + 3);
979 mc->mc_resid = MLY_GET_REG4(sc, sc->mly_status_mailbox + 4);
981 mc->mc_flags &= ~MLY_CMD_BUSY;
982 mly_enqueue_complete(mc);
985 /* slot 0xffff may mean "extremely bogus command" */
986 mly_printf(sc, "got HM completion for illegal slot %u\n", slot);
988 /* unconditionally acknowledge status */
989 MLY_SET_REG(sc, sc->mly_odbr, MLY_HM_STSREADY);
990 MLY_SET_REG(sc, sc->mly_idbr, MLY_HM_STSACK);
993 /* pick up memory-mailbox commands */
994 if (MLY_ODBR_TRUE(sc, MLY_AM_STSREADY)) {
996 sp = &sc->mly_mmbox->mmm_status[sc->mly_mmbox_status_index];
998 /* check for more status */
999 if (sp->mmbox.flag == 0)
1002 /* get slot number */
1003 slot = sp->status.command_id;
1004 if (slot < MLY_SLOT_MAX) {
1005 mc = &sc->mly_command[slot - MLY_SLOT_START];
1006 mc->mc_status = sp->status.status;
1007 mc->mc_sense = sp->status.sense_length;
1008 mc->mc_resid = sp->status.residue;
1009 mly_remove_busy(mc);
1010 mc->mc_flags &= ~MLY_CMD_BUSY;
1011 mly_enqueue_complete(mc);
1014 /* slot 0xffff may mean "extremely bogus command" */
1015 mly_printf(sc, "got AM completion for illegal slot %u at %d\n",
1016 slot, sc->mly_mmbox_status_index);
1019 /* clear and move to next index */
1021 sc->mly_mmbox_status_index = (sc->mly_mmbox_status_index + 1) % MLY_MMBOX_STATUS;
1023 /* acknowledge that we have collected status value(s) */
1024 MLY_SET_REG(sc, sc->mly_odbr, MLY_AM_STSREADY);
1029 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
1030 if (sc->mly_state & MLY_STATE_INTERRUPTS_ON)
1031 taskqueue_enqueue(taskqueue_swi, &sc->mly_task_complete);
1034 mly_complete(sc, 0);
1038 /********************************************************************************
1039 * Process completed commands
1042 mly_complete(void *context, int pending)
1044 struct mly_softc *sc = (struct mly_softc *)context;
1045 struct mly_command *mc;
1046 void (* mc_complete)(struct mly_command *mc);
1052 * Spin pulling commands off the completed queue and processing them.
1054 while ((mc = mly_dequeue_complete(sc)) != NULL) {
1057 * Free controller resources, mark command complete.
1059 * Note that as soon as we mark the command complete, it may be freed
1060 * out from under us, so we need to save the mc_complete field in
1061 * order to later avoid dereferencing mc. (We would not expect to
1062 * have a polling/sleeping consumer with mc_complete != NULL).
1064 mly_unmap_command(mc);
1065 mc_complete = mc->mc_complete;
1066 mc->mc_flags |= MLY_CMD_COMPLETE;
1069 * Call completion handler or wake up sleeping consumer.
1071 if (mc_complete != NULL) {
1079 * We may have freed up controller resources which would allow us
1080 * to push more commands onto the controller, so we check here.
1085 * The controller may have updated the health status information,
1086 * so check for it here.
1088 * Note that we only check for health status after a completed command. It
1089 * might be wise to ping the controller occasionally if it's been idle for
1090 * a while just to check up on it. While a filesystem is mounted, or I/O is
1091 * active this isn't really an issue.
1093 if (sc->mly_mmbox->mmm_health.status.change_counter != sc->mly_event_change) {
1094 sc->mly_event_change = sc->mly_mmbox->mmm_health.status.change_counter;
1095 debug(1, "event change %d, event status update, %d -> %d", sc->mly_event_change,
1096 sc->mly_event_waiting, sc->mly_mmbox->mmm_health.status.next_event);
1097 sc->mly_event_waiting = sc->mly_mmbox->mmm_health.status.next_event;
1099 /* wake up anyone that might be interested in this */
1100 wakeup(&sc->mly_event_change);
1102 if (sc->mly_event_counter != sc->mly_event_waiting)
1103 mly_fetch_event(sc);
1106 /********************************************************************************
1107 ********************************************************************************
1108 Command Buffer Management
1109 ********************************************************************************
1110 ********************************************************************************/
1112 /********************************************************************************
1113 * Allocate a command.
1116 mly_alloc_command(struct mly_softc *sc, struct mly_command **mcp)
1118 struct mly_command *mc;
1122 if ((mc = mly_dequeue_free(sc)) == NULL)
1129 /********************************************************************************
1130 * Release a command back to the freelist.
1133 mly_release_command(struct mly_command *mc)
1138 * Fill in parts of the command that may cause confusion if
1139 * a consumer doesn't when we are later allocated.
1143 mc->mc_complete = NULL;
1144 mc->mc_private = NULL;
1147 * By default, we set up to overwrite the command packet with
1148 * sense information.
1150 mc->mc_packet->generic.sense_buffer_address = mc->mc_packetphys;
1151 mc->mc_packet->generic.maximum_sense_size = sizeof(union mly_command_packet);
1153 mly_enqueue_free(mc);
1156 /********************************************************************************
1157 * Map helper for command allocation.
1160 mly_alloc_commands_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1162 struct mly_softc *sc = (struct mly_softc *)arg
1166 sc->mly_packetphys = segs[0].ds_addr;
1169 /********************************************************************************
1170 * Allocate and initialise command and packet structures.
1173 mly_alloc_commands(struct mly_softc *sc)
1175 struct mly_command *mc;
1179 * Allocate enough space for all the command packets in one chunk and
1180 * map them permanently into controller-visible space.
1182 if (bus_dmamem_alloc(sc->mly_packet_dmat, (void **)&sc->mly_packet,
1183 BUS_DMA_NOWAIT, &sc->mly_packetmap)) {
1186 bus_dmamap_load(sc->mly_packet_dmat, sc->mly_packetmap, sc->mly_packet,
1187 MLY_MAXCOMMANDS * sizeof(union mly_command_packet),
1188 mly_alloc_commands_map, sc, 0);
1190 for (i = 0; i < MLY_MAXCOMMANDS; i++) {
1191 mc = &sc->mly_command[i];
1192 bzero(mc, sizeof(*mc));
1194 mc->mc_slot = MLY_SLOT_START + i;
1195 mc->mc_packet = sc->mly_packet + i;
1196 mc->mc_packetphys = sc->mly_packetphys + (i * sizeof(union mly_command_packet));
1197 if (!bus_dmamap_create(sc->mly_buffer_dmat, 0, &mc->mc_datamap))
1198 mly_release_command(mc);
1203 /********************************************************************************
1204 * Command-mapping helper function - populate this command's s/g table
1205 * with the s/g entries for its data.
1208 mly_map_command_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1210 struct mly_command *mc = (struct mly_command *)arg;
1211 struct mly_softc *sc = mc->mc_sc;
1212 struct mly_command_generic *gen = &(mc->mc_packet->generic);
1213 struct mly_sg_entry *sg;
1218 /* can we use the transfer structure directly? */
1220 sg = &gen->transfer.direct.sg[0];
1221 gen->command_control.extended_sg_table = 0;
1223 tabofs = ((mc->mc_slot - MLY_SLOT_START) * MLY_MAXSGENTRIES);
1224 sg = sc->mly_sg_table + tabofs;
1225 gen->transfer.indirect.entries[0] = nseg;
1226 gen->transfer.indirect.table_physaddr[0] = sc->mly_sg_busaddr + (tabofs * sizeof(struct mly_sg_entry));
1227 gen->command_control.extended_sg_table = 1;
1230 /* copy the s/g table */
1231 for (i = 0; i < nseg; i++) {
1232 sg[i].physaddr = segs[i].ds_addr;
1233 sg[i].length = segs[i].ds_len;
1239 /********************************************************************************
1240 * Command-mapping helper function - save the cdb's physical address.
1242 * We don't support 'large' SCSI commands at this time, so this is unused.
1245 mly_map_command_cdb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1247 struct mly_command *mc = (struct mly_command *)arg;
1251 /* XXX can we safely assume that a CDB will never cross a page boundary? */
1252 if ((segs[0].ds_addr % PAGE_SIZE) >
1253 ((segs[0].ds_addr + mc->mc_packet->scsi_large.cdb_length) % PAGE_SIZE))
1254 panic("cdb crosses page boundary");
1256 /* fix up fields in the command packet */
1257 mc->mc_packet->scsi_large.cdb_physaddr = segs[0].ds_addr;
1261 /********************************************************************************
1262 * Map a command into controller-visible space
1265 mly_map_command(struct mly_command *mc)
1267 struct mly_softc *sc = mc->mc_sc;
1271 /* don't map more than once */
1272 if (mc->mc_flags & MLY_CMD_MAPPED)
1275 /* does the command have a data buffer? */
1276 if (mc->mc_data != NULL)
1277 bus_dmamap_load(sc->mly_buffer_dmat, mc->mc_datamap, mc->mc_data, mc->mc_length,
1278 mly_map_command_sg, mc, 0);
1280 if (mc->mc_flags & MLY_CMD_DATAIN)
1281 bus_dmamap_sync(sc->mly_buffer_dmat, mc->mc_datamap, BUS_DMASYNC_PREREAD);
1282 if (mc->mc_flags & MLY_CMD_DATAOUT)
1283 bus_dmamap_sync(sc->mly_buffer_dmat, mc->mc_datamap, BUS_DMASYNC_PREWRITE);
1285 mc->mc_flags |= MLY_CMD_MAPPED;
1288 /********************************************************************************
1289 * Unmap a command from controller-visible space
1292 mly_unmap_command(struct mly_command *mc)
1294 struct mly_softc *sc = mc->mc_sc;
1298 if (!(mc->mc_flags & MLY_CMD_MAPPED))
1301 if (mc->mc_flags & MLY_CMD_DATAIN)
1302 bus_dmamap_sync(sc->mly_buffer_dmat, mc->mc_datamap, BUS_DMASYNC_POSTREAD);
1303 if (mc->mc_flags & MLY_CMD_DATAOUT)
1304 bus_dmamap_sync(sc->mly_buffer_dmat, mc->mc_datamap, BUS_DMASYNC_POSTWRITE);
1306 /* does the command have a data buffer? */
1307 if (mc->mc_data != NULL)
1308 bus_dmamap_unload(sc->mly_buffer_dmat, mc->mc_datamap);
1310 mc->mc_flags &= ~MLY_CMD_MAPPED;
1313 /********************************************************************************
1314 ********************************************************************************
1316 ********************************************************************************
1317 ********************************************************************************/
1319 /********************************************************************************
1320 * Handshake with the firmware while the card is being initialised.
1323 mly_fwhandshake(struct mly_softc *sc)
1325 u_int8_t error, param0, param1;
1330 /* set HM_STSACK and let the firmware initialise */
1331 MLY_SET_REG(sc, sc->mly_idbr, MLY_HM_STSACK);
1332 DELAY(1000); /* too short? */
1334 /* if HM_STSACK is still true, the controller is initialising */
1335 if (!MLY_IDBR_TRUE(sc, MLY_HM_STSACK))
1337 mly_printf(sc, "controller initialisation started\n");
1339 /* spin waiting for initialisation to finish, or for a message to be delivered */
1340 while (MLY_IDBR_TRUE(sc, MLY_HM_STSACK)) {
1341 /* check for a message */
1342 if (MLY_ERROR_VALID(sc)) {
1343 error = MLY_GET_REG(sc, sc->mly_error_status) & ~MLY_MSG_EMPTY;
1344 param0 = MLY_GET_REG(sc, sc->mly_command_mailbox);
1345 param1 = MLY_GET_REG(sc, sc->mly_command_mailbox + 1);
1348 case MLY_MSG_SPINUP:
1350 mly_printf(sc, "drive spinup in progress\n");
1351 spinup = 1; /* only print this once (should print drive being spun?) */
1354 case MLY_MSG_RACE_RECOVERY_FAIL:
1355 mly_printf(sc, "mirror race recovery failed, one or more drives offline\n");
1357 case MLY_MSG_RACE_IN_PROGRESS:
1358 mly_printf(sc, "mirror race recovery in progress\n");
1360 case MLY_MSG_RACE_ON_CRITICAL:
1361 mly_printf(sc, "mirror race recovery on a critical drive\n");
1363 case MLY_MSG_PARITY_ERROR:
1364 mly_printf(sc, "FATAL MEMORY PARITY ERROR\n");
1367 mly_printf(sc, "unknown initialisation code 0x%x\n", error);
1374 /********************************************************************************
1375 ********************************************************************************
1376 Debugging and Diagnostics
1377 ********************************************************************************
1378 ********************************************************************************/
1380 /********************************************************************************
1381 * Print some information about the controller.
1384 mly_describe_controller(struct mly_softc *sc)
1386 struct mly_ioctl_getcontrollerinfo *mi = sc->mly_controllerinfo;
1388 mly_printf(sc, "%16s, %d channel%s, firmware %d.%02d-%d-%02d (%02d%02d%02d%02d), %dMB RAM\n",
1389 mi->controller_name, mi->physical_channels_present, (mi->physical_channels_present) > 1 ? "s" : "",
1390 mi->fw_major, mi->fw_minor, mi->fw_turn, mi->fw_build, /* XXX turn encoding? */
1391 mi->fw_century, mi->fw_year, mi->fw_month, mi->fw_day,
1395 mly_printf(sc, "%s %s (%x), %dMHz %d-bit %.16s\n",
1396 mly_describe_code(mly_table_oemname, mi->oem_information),
1397 mly_describe_code(mly_table_controllertype, mi->controller_type), mi->controller_type,
1398 mi->interface_speed, mi->interface_width, mi->interface_name);
1399 mly_printf(sc, "%dMB %dMHz %d-bit %s%s%s, cache %dMB\n",
1400 mi->memory_size, mi->memory_speed, mi->memory_width,
1401 mly_describe_code(mly_table_memorytype, mi->memory_type),
1402 mi->memory_parity ? "+parity": "",mi->memory_ecc ? "+ECC": "",
1404 mly_printf(sc, "CPU: %s @ %dMHZ\n",
1405 mly_describe_code(mly_table_cputype, mi->cpu[0].type), mi->cpu[0].speed);
1406 if (mi->l2cache_size != 0)
1407 mly_printf(sc, "%dKB L2 cache\n", mi->l2cache_size);
1408 if (mi->exmemory_size != 0)
1409 mly_printf(sc, "%dMB %dMHz %d-bit private %s%s%s\n",
1410 mi->exmemory_size, mi->exmemory_speed, mi->exmemory_width,
1411 mly_describe_code(mly_table_memorytype, mi->exmemory_type),
1412 mi->exmemory_parity ? "+parity": "",mi->exmemory_ecc ? "+ECC": "");
1413 mly_printf(sc, "battery backup %s\n", mi->bbu_present ? "present" : "not installed");
1414 mly_printf(sc, "maximum data transfer %d blocks, maximum sg entries/command %d\n",
1415 mi->maximum_block_count, mi->maximum_sg_entries);
1416 mly_printf(sc, "logical devices present/critical/offline %d/%d/%d\n",
1417 mi->logical_devices_present, mi->logical_devices_critical, mi->logical_devices_offline);
1418 mly_printf(sc, "physical devices present %d\n",
1419 mi->physical_devices_present);
1420 mly_printf(sc, "physical disks present/offline %d/%d\n",
1421 mi->physical_disks_present, mi->physical_disks_offline);
1422 mly_printf(sc, "%d physical channel%s, %d virtual channel%s of %d possible\n",
1423 mi->physical_channels_present, mi->physical_channels_present == 1 ? "" : "s",
1424 mi->virtual_channels_present, mi->virtual_channels_present == 1 ? "" : "s",
1425 mi->virtual_channels_possible);
1426 mly_printf(sc, "%d parallel commands supported\n", mi->maximum_parallel_commands);
1427 mly_printf(sc, "%dMB flash ROM, %d of %d maximum cycles\n",
1428 mi->flash_size, mi->flash_age, mi->flash_maximum_age);
1433 /********************************************************************************
1434 * Print some controller state
1437 mly_printstate(struct mly_softc *sc)
1439 mly_printf(sc, "IDBR %02x ODBR %02x ERROR %02x (%x %x %x)\n",
1440 MLY_GET_REG(sc, sc->mly_idbr),
1441 MLY_GET_REG(sc, sc->mly_odbr),
1442 MLY_GET_REG(sc, sc->mly_error_status),
1445 sc->mly_error_status);
1446 mly_printf(sc, "IMASK %02x ISTATUS %02x\n",
1447 MLY_GET_REG(sc, sc->mly_interrupt_mask),
1448 MLY_GET_REG(sc, sc->mly_interrupt_status));
1449 mly_printf(sc, "COMMAND %02x %02x %02x %02x %02x %02x %02x %02x\n",
1450 MLY_GET_REG(sc, sc->mly_command_mailbox),
1451 MLY_GET_REG(sc, sc->mly_command_mailbox + 1),
1452 MLY_GET_REG(sc, sc->mly_command_mailbox + 2),
1453 MLY_GET_REG(sc, sc->mly_command_mailbox + 3),
1454 MLY_GET_REG(sc, sc->mly_command_mailbox + 4),
1455 MLY_GET_REG(sc, sc->mly_command_mailbox + 5),
1456 MLY_GET_REG(sc, sc->mly_command_mailbox + 6),
1457 MLY_GET_REG(sc, sc->mly_command_mailbox + 7));
1458 mly_printf(sc, "STATUS %02x %02x %02x %02x %02x %02x %02x %02x\n",
1459 MLY_GET_REG(sc, sc->mly_status_mailbox),
1460 MLY_GET_REG(sc, sc->mly_status_mailbox + 1),
1461 MLY_GET_REG(sc, sc->mly_status_mailbox + 2),
1462 MLY_GET_REG(sc, sc->mly_status_mailbox + 3),
1463 MLY_GET_REG(sc, sc->mly_status_mailbox + 4),
1464 MLY_GET_REG(sc, sc->mly_status_mailbox + 5),
1465 MLY_GET_REG(sc, sc->mly_status_mailbox + 6),
1466 MLY_GET_REG(sc, sc->mly_status_mailbox + 7));
1467 mly_printf(sc, " %04x %08x\n",
1468 MLY_GET_REG2(sc, sc->mly_status_mailbox),
1469 MLY_GET_REG4(sc, sc->mly_status_mailbox + 4));
1472 struct mly_softc *mly_softc0 = NULL;
1474 mly_printstate0(void)
1476 if (mly_softc0 != NULL)
1477 mly_printstate(mly_softc0);
1480 /********************************************************************************
1484 mly_print_command(struct mly_command *mc)
1486 struct mly_softc *sc = mc->mc_sc;
1488 mly_printf(sc, "COMMAND @ %p\n", mc);
1489 mly_printf(sc, " slot %d\n", mc->mc_slot);
1490 mly_printf(sc, " status 0x%x\n", mc->mc_status);
1491 mly_printf(sc, " sense len %d\n", mc->mc_sense);
1492 mly_printf(sc, " resid %d\n", mc->mc_resid);
1493 mly_printf(sc, " packet %p/0x%llx\n", mc->mc_packet, mc->mc_packetphys);
1494 if (mc->mc_packet != NULL)
1495 mly_print_packet(mc);
1496 mly_printf(sc, " data %p/%d\n", mc->mc_data, mc->mc_length);
1497 mly_printf(sc, " flags %b\n", mc->mc_flags, "\20\1busy\2complete\3slotted\4mapped\5datain\6dataout\n");
1498 mly_printf(sc, " complete %p\n", mc->mc_complete);
1499 mly_printf(sc, " private %p\n", mc->mc_private);
1502 /********************************************************************************
1503 * Print a command packet
1506 mly_print_packet(struct mly_command *mc)
1508 struct mly_softc *sc = mc->mc_sc;
1509 struct mly_command_generic *ge = (struct mly_command_generic *)mc->mc_packet;
1510 struct mly_command_scsi_small *ss = (struct mly_command_scsi_small *)mc->mc_packet;
1511 struct mly_command_scsi_large *sl = (struct mly_command_scsi_large *)mc->mc_packet;
1512 struct mly_command_ioctl *io = (struct mly_command_ioctl *)mc->mc_packet;
1515 mly_printf(sc, " command_id %d\n", ge->command_id);
1516 mly_printf(sc, " opcode %d\n", ge->opcode);
1517 mly_printf(sc, " command_control fua %d dpo %d est %d dd %s nas %d ddis %d\n",
1518 ge->command_control.force_unit_access,
1519 ge->command_control.disable_page_out,
1520 ge->command_control.extended_sg_table,
1521 (ge->command_control.data_direction == MLY_CCB_WRITE) ? "WRITE" : "READ",
1522 ge->command_control.no_auto_sense,
1523 ge->command_control.disable_disconnect);
1524 mly_printf(sc, " data_size %d\n", ge->data_size);
1525 mly_printf(sc, " sense_buffer_address 0x%llx\n", ge->sense_buffer_address);
1526 mly_printf(sc, " lun %d\n", ge->addr.phys.lun);
1527 mly_printf(sc, " target %d\n", ge->addr.phys.target);
1528 mly_printf(sc, " channel %d\n", ge->addr.phys.channel);
1529 mly_printf(sc, " logical device %d\n", ge->addr.log.logdev);
1530 mly_printf(sc, " controller %d\n", ge->addr.phys.controller);
1531 mly_printf(sc, " timeout %d %s\n",
1533 (ge->timeout.scale == MLY_TIMEOUT_SECONDS) ? "seconds" :
1534 ((ge->timeout.scale == MLY_TIMEOUT_MINUTES) ? "minutes" : "hours"));
1535 mly_printf(sc, " maximum_sense_size %d\n", ge->maximum_sense_size);
1536 switch(ge->opcode) {
1539 mly_printf(sc, " cdb length %d\n", ss->cdb_length);
1540 mly_printf(sc, " cdb %*D\n", ss->cdb_length, ss->cdb, " ");
1544 case MDACMD_SCSILCPT:
1545 mly_printf(sc, " cdb length %d\n", sl->cdb_length);
1546 mly_printf(sc, " cdb 0x%llx\n", sl->cdb_physaddr);
1550 mly_printf(sc, " sub_ioctl 0x%x\n", io->sub_ioctl);
1551 switch(io->sub_ioctl) {
1552 case MDACIOCTL_SETMEMORYMAILBOX:
1553 mly_printf(sc, " health_buffer_size %d\n",
1554 io->param.setmemorymailbox.health_buffer_size);
1555 mly_printf(sc, " health_buffer_phys 0x%llx\n",
1556 io->param.setmemorymailbox.health_buffer_physaddr);
1557 mly_printf(sc, " command_mailbox 0x%llx\n",
1558 io->param.setmemorymailbox.command_mailbox_physaddr);
1559 mly_printf(sc, " status_mailbox 0x%llx\n",
1560 io->param.setmemorymailbox.status_mailbox_physaddr);
1564 case MDACIOCTL_SETREALTIMECLOCK:
1565 case MDACIOCTL_GETHEALTHSTATUS:
1566 case MDACIOCTL_GETCONTROLLERINFO:
1567 case MDACIOCTL_GETLOGDEVINFOVALID:
1568 case MDACIOCTL_GETPHYSDEVINFOVALID:
1569 case MDACIOCTL_GETPHYSDEVSTATISTICS:
1570 case MDACIOCTL_GETLOGDEVSTATISTICS:
1571 case MDACIOCTL_GETCONTROLLERSTATISTICS:
1572 case MDACIOCTL_GETBDT_FOR_SYSDRIVE:
1573 case MDACIOCTL_CREATENEWCONF:
1574 case MDACIOCTL_ADDNEWCONF:
1575 case MDACIOCTL_GETDEVCONFINFO:
1576 case MDACIOCTL_GETFREESPACELIST:
1577 case MDACIOCTL_MORE:
1578 case MDACIOCTL_SETPHYSDEVPARAMETER:
1579 case MDACIOCTL_GETPHYSDEVPARAMETER:
1580 case MDACIOCTL_GETLOGDEVPARAMETER:
1581 case MDACIOCTL_SETLOGDEVPARAMETER:
1582 mly_printf(sc, " param %10D\n", io->param.data.param, " ");
1586 case MDACIOCTL_GETEVENT:
1587 mly_printf(sc, " event %d\n",
1588 io->param.getevent.sequence_number_low + ((u_int32_t)io->addr.log.logdev << 16));
1592 case MDACIOCTL_SETRAIDDEVSTATE:
1593 mly_printf(sc, " state %d\n", io->param.setraiddevstate.state);
1597 case MDACIOCTL_XLATEPHYSDEVTORAIDDEV:
1598 mly_printf(sc, " raid_device %d\n", io->param.xlatephysdevtoraiddev.raid_device);
1599 mly_printf(sc, " controller %d\n", io->param.xlatephysdevtoraiddev.controller);
1600 mly_printf(sc, " channel %d\n", io->param.xlatephysdevtoraiddev.channel);
1601 mly_printf(sc, " target %d\n", io->param.xlatephysdevtoraiddev.target);
1602 mly_printf(sc, " lun %d\n", io->param.xlatephysdevtoraiddev.lun);
1606 case MDACIOCTL_GETGROUPCONFINFO:
1607 mly_printf(sc, " group %d\n", io->param.getgroupconfinfo.group);
1611 case MDACIOCTL_GET_SUBSYSTEM_DATA:
1612 case MDACIOCTL_SET_SUBSYSTEM_DATA:
1613 case MDACIOCTL_STARTDISOCVERY:
1614 case MDACIOCTL_INITPHYSDEVSTART:
1615 case MDACIOCTL_INITPHYSDEVSTOP:
1616 case MDACIOCTL_INITRAIDDEVSTART:
1617 case MDACIOCTL_INITRAIDDEVSTOP:
1618 case MDACIOCTL_REBUILDRAIDDEVSTART:
1619 case MDACIOCTL_REBUILDRAIDDEVSTOP:
1620 case MDACIOCTL_MAKECONSISTENTDATASTART:
1621 case MDACIOCTL_MAKECONSISTENTDATASTOP:
1622 case MDACIOCTL_CONSISTENCYCHECKSTART:
1623 case MDACIOCTL_CONSISTENCYCHECKSTOP:
1624 case MDACIOCTL_RESETDEVICE:
1625 case MDACIOCTL_FLUSHDEVICEDATA:
1626 case MDACIOCTL_PAUSEDEVICE:
1627 case MDACIOCTL_UNPAUSEDEVICE:
1628 case MDACIOCTL_LOCATEDEVICE:
1629 case MDACIOCTL_SETMASTERSLAVEMODE:
1630 case MDACIOCTL_DELETERAIDDEV:
1631 case MDACIOCTL_REPLACEINTERNALDEV:
1632 case MDACIOCTL_CLEARCONF:
1633 case MDACIOCTL_GETCONTROLLERPARAMETER:
1634 case MDACIOCTL_SETCONTRLLERPARAMETER:
1635 case MDACIOCTL_CLEARCONFSUSPMODE:
1636 case MDACIOCTL_STOREIMAGE:
1637 case MDACIOCTL_READIMAGE:
1638 case MDACIOCTL_FLASHIMAGES:
1639 case MDACIOCTL_RENAMERAIDDEV:
1640 default: /* no idea what to print */
1646 case MDACMD_IOCTLCHECK:
1647 case MDACMD_MEMCOPY:
1650 break; /* print nothing */
1653 if (ge->command_control.extended_sg_table) {
1654 mly_printf(sc, " sg table 0x%llx/%d\n",
1655 ge->transfer.indirect.table_physaddr[0], ge->transfer.indirect.entries[0]);
1657 mly_printf(sc, " 0000 0x%llx/%lld\n",
1658 ge->transfer.direct.sg[0].physaddr, ge->transfer.direct.sg[0].length);
1659 mly_printf(sc, " 0001 0x%llx/%lld\n",
1660 ge->transfer.direct.sg[1].physaddr, ge->transfer.direct.sg[1].length);
1665 /********************************************************************************
1666 * Panic in a slightly informative fashion
1669 mly_panic(struct mly_softc *sc, char *reason)
1676 /********************************************************************************
1677 * Print queue statistics, callable from DDB.
1680 mly_print_controller(int controller)
1682 struct mly_softc *sc;
1684 if ((sc = devclass_get_softc(devclass_find("mly"), controller)) == NULL) {
1685 printf("mly: controller %d invalid\n", controller);
1687 device_printf(sc->mly_dev, "queue curr max\n");
1688 device_printf(sc->mly_dev, "free %04d/%04d\n",
1689 sc->mly_qstat[MLYQ_FREE].q_length, sc->mly_qstat[MLYQ_FREE].q_max);
1690 device_printf(sc->mly_dev, "ready %04d/%04d\n",
1691 sc->mly_qstat[MLYQ_READY].q_length, sc->mly_qstat[MLYQ_READY].q_max);
1692 device_printf(sc->mly_dev, "busy %04d/%04d\n",
1693 sc->mly_qstat[MLYQ_BUSY].q_length, sc->mly_qstat[MLYQ_BUSY].q_max);
1694 device_printf(sc->mly_dev, "complete %04d/%04d\n",
1695 sc->mly_qstat[MLYQ_COMPLETE].q_length, sc->mly_qstat[MLYQ_COMPLETE].q_max);
1700 /********************************************************************************
1701 ********************************************************************************
1702 Control device interface
1703 ********************************************************************************
1704 ********************************************************************************/
1706 /********************************************************************************
1707 * Accept an open operation on the control device.
1710 mly_user_open(dev_t dev, int flags, int fmt, d_thread_t *td)
1712 int unit = minor(dev);
1713 struct mly_softc *sc = devclass_get_softc(devclass_find("mly"), unit);
1715 sc->mly_state |= MLY_STATE_OPEN;
1719 /********************************************************************************
1720 * Accept the last close on the control device.
1723 mly_user_close(dev_t dev, int flags, int fmt, d_thread_t *td)
1725 int unit = minor(dev);
1726 struct mly_softc *sc = devclass_get_softc(devclass_find("mly"), unit);
1728 sc->mly_state &= ~MLY_STATE_OPEN;
1732 /********************************************************************************
1733 * Handle controller-specific control operations.
1736 mly_user_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *td)
1738 struct mly_softc *sc = (struct mly_softc *)dev->si_drv1;
1739 struct mly_user_command *uc = (struct mly_user_command *)addr;
1740 struct mly_user_health *uh = (struct mly_user_health *)addr;
1744 return(mly_user_command(sc, uc));
1746 return(mly_user_health(sc, uh));
1752 /********************************************************************************
1753 * Execute a command passed in from userspace.
1755 * The control structure contains the actual command for the controller, as well
1756 * as the user-space data pointer and data size, and an optional sense buffer
1757 * size/pointer. On completion, the data size is adjusted to the command
1758 * residual, and the sense buffer size to the size of the returned sense data.
1762 mly_user_command(struct mly_softc *sc, struct mly_user_command *uc)
1764 struct mly_command *mc;
1767 /* allocate a command */
1768 if (mly_alloc_command(sc, &mc)) {
1770 goto out; /* XXX Linux version will wait for a command */
1773 /* handle data size/direction */
1774 mc->mc_length = (uc->DataTransferLength >= 0) ? uc->DataTransferLength : -uc->DataTransferLength;
1775 if (mc->mc_length > 0)
1776 mc->mc_data = malloc(mc->mc_length, M_DEVBUF, M_INTWAIT);
1777 if (uc->DataTransferLength > 0) {
1778 mc->mc_flags |= MLY_CMD_DATAIN;
1779 bzero(mc->mc_data, mc->mc_length);
1781 if (uc->DataTransferLength < 0) {
1782 mc->mc_flags |= MLY_CMD_DATAOUT;
1783 if ((error = copyin(uc->DataTransferBuffer, mc->mc_data, mc->mc_length)) != 0)
1787 /* copy the controller command */
1788 bcopy(&uc->CommandMailbox, mc->mc_packet, sizeof(uc->CommandMailbox));
1790 /* clear command completion handler so that we get woken up */
1791 mc->mc_complete = NULL;
1793 /* execute the command */
1795 mly_requeue_ready(mc);
1797 while (!(mc->mc_flags & MLY_CMD_COMPLETE))
1798 tsleep(mc, 0, "mlyioctl", 0);
1801 /* return the data to userspace */
1802 if (uc->DataTransferLength > 0)
1803 if ((error = copyout(mc->mc_data, uc->DataTransferBuffer, mc->mc_length)) != 0)
1806 /* return the sense buffer to userspace */
1807 if ((uc->RequestSenseLength > 0) && (mc->mc_sense > 0)) {
1808 if ((error = copyout(mc->mc_packet, uc->RequestSenseBuffer,
1809 min(uc->RequestSenseLength, mc->mc_sense))) != 0)
1813 /* return command results to userspace (caller will copy out) */
1814 uc->DataTransferLength = mc->mc_resid;
1815 uc->RequestSenseLength = min(uc->RequestSenseLength, mc->mc_sense);
1816 uc->CommandStatus = mc->mc_status;
1820 if (mc->mc_data != NULL)
1821 free(mc->mc_data, M_DEVBUF);
1823 mly_release_command(mc);
1827 /********************************************************************************
1828 * Return health status to userspace. If the health change index in the user
1829 * structure does not match that currently exported by the controller, we
1830 * return the current status immediately. Otherwise, we block until either
1831 * interrupted or new status is delivered.
1834 mly_user_health(struct mly_softc *sc, struct mly_user_health *uh)
1836 struct mly_health_status mh;
1839 /* fetch the current health status from userspace */
1840 if ((error = copyin(uh->HealthStatusBuffer, &mh, sizeof(mh))) != 0)
1843 /* spin waiting for a status update */
1845 error = EWOULDBLOCK;
1846 while ((error != 0) && (sc->mly_event_change == mh.change_counter))
1847 error = tsleep(&sc->mly_event_change, PCATCH, "mlyhealth", 0);
1850 /* copy the controller's health status buffer out (there is a race here if it changes again) */
1851 error = copyout(&sc->mly_mmbox->mmm_health.status, uh->HealthStatusBuffer,
1852 sizeof(uh->HealthStatusBuffer));