2 * Copyright (c) 2006 IronPort Systems
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Copyright (c) 2007 LSI Corp.
28 * Copyright (c) 2007 Rajesh Prabhakaran.
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
40 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/sysctl.h>
61 #include <sys/malloc.h>
62 #include <sys/kernel.h>
64 #include <sys/selinfo.h>
67 #include <sys/eventhandler.h>
69 #include <sys/bus_dma.h>
71 #include <sys/ioccom.h>
74 #include <sys/signalvar.h>
76 #include <machine/bus.h>
77 #include <machine/resource.h>
79 #include <dev/mfi/mfireg.h>
80 #include <dev/mfi/mfi_ioctl.h>
81 #include <dev/mfi/mfivar.h>
83 static int mfi_alloc_commands(struct mfi_softc *);
84 static int mfi_comms_init(struct mfi_softc *);
85 static int mfi_wait_command(struct mfi_softc *, struct mfi_command *);
86 static int mfi_get_controller_info(struct mfi_softc *);
87 static int mfi_get_log_state(struct mfi_softc *,
88 struct mfi_evt_log_state **);
89 static int mfi_parse_entries(struct mfi_softc *, int, int);
90 static int mfi_dcmd_command(struct mfi_softc *, struct mfi_command **,
91 uint32_t, void **, size_t);
92 static void mfi_data_cb(void *, bus_dma_segment_t *, int, int);
93 static void mfi_startup(void *arg);
94 static void mfi_intr(void *arg);
95 static void mfi_ldprobe(struct mfi_softc *sc);
96 static int mfi_aen_register(struct mfi_softc *sc, int seq, int locale);
97 static void mfi_aen_complete(struct mfi_command *);
98 static int mfi_aen_setup(struct mfi_softc *, uint32_t);
99 static int mfi_add_ld(struct mfi_softc *sc, int);
100 static void mfi_add_ld_complete(struct mfi_command *);
101 static struct mfi_command * mfi_bio_command(struct mfi_softc *);
102 static void mfi_bio_complete(struct mfi_command *);
103 static int mfi_mapcmd(struct mfi_softc *, struct mfi_command *);
104 static int mfi_send_frame(struct mfi_softc *, struct mfi_command *);
105 static void mfi_complete(struct mfi_softc *, struct mfi_command *);
106 static int mfi_abort(struct mfi_softc *, struct mfi_command *);
107 static int mfi_linux_ioctl_int(struct cdev *, u_long, caddr_t, int, struct thread *);
108 static void mfi_timeout(void *);
109 static int mfi_user_command(struct mfi_softc *,
110 struct mfi_ioc_passthru *);
111 static void mfi_enable_intr_xscale(struct mfi_softc *sc);
112 static void mfi_enable_intr_ppc(struct mfi_softc *sc);
113 static int32_t mfi_read_fw_status_xscale(struct mfi_softc *sc);
114 static int32_t mfi_read_fw_status_ppc(struct mfi_softc *sc);
115 static int mfi_check_clear_intr_xscale(struct mfi_softc *sc);
116 static int mfi_check_clear_intr_ppc(struct mfi_softc *sc);
117 static void mfi_issue_cmd_xscale(struct mfi_softc *sc,uint32_t bus_add,uint32_t frame_cnt);
118 static void mfi_issue_cmd_ppc(struct mfi_softc *sc,uint32_t bus_add,uint32_t frame_cnt);
120 SYSCTL_NODE(_hw, OID_AUTO, mfi, CTLFLAG_RD, 0, "MFI driver parameters");
121 static int mfi_event_locale = MFI_EVT_LOCALE_ALL;
122 TUNABLE_INT("hw.mfi.event_locale", &mfi_event_locale);
123 SYSCTL_INT(_hw_mfi, OID_AUTO, event_locale, CTLFLAG_RW, &mfi_event_locale,
124 0, "event message locale");
126 static int mfi_event_class = MFI_EVT_CLASS_INFO;
127 TUNABLE_INT("hw.mfi.event_class", &mfi_event_class);
128 SYSCTL_INT(_hw_mfi, OID_AUTO, event_class, CTLFLAG_RW, &mfi_event_class,
129 0, "event message class");
131 static int mfi_max_cmds = 128;
132 TUNABLE_INT("hw.mfi.max_cmds", &mfi_max_cmds);
133 SYSCTL_INT(_hw_mfi, OID_AUTO, max_cmds, CTLFLAG_RD, &mfi_max_cmds,
136 /* Management interface */
137 static d_open_t mfi_open;
138 static d_close_t mfi_close;
139 static d_ioctl_t mfi_ioctl;
140 static d_poll_t mfi_poll;
142 static struct cdevsw mfi_cdevsw = {
143 .d_version = D_VERSION,
146 .d_close = mfi_close,
147 .d_ioctl = mfi_ioctl,
152 MALLOC_DEFINE(M_MFIBUF, "mfibuf", "Buffers for the MFI driver");
154 #define MFI_INQ_LENGTH SHORT_INQUIRY_LENGTH
157 mfi_enable_intr_xscale(struct mfi_softc *sc)
159 MFI_WRITE4(sc, MFI_OMSK, 0x01);
163 mfi_enable_intr_ppc(struct mfi_softc *sc)
165 MFI_WRITE4(sc, MFI_ODCR0, 0xFFFFFFFF);
166 if (sc->mfi_flags & MFI_FLAGS_1078) {
167 MFI_WRITE4(sc, MFI_OMSK, ~MFI_1078_EIM);
168 } else if (sc->mfi_flags & MFI_FLAGS_GEN2) {
169 MFI_WRITE4(sc, MFI_OMSK, ~MFI_GEN2_EIM);
174 mfi_read_fw_status_xscale(struct mfi_softc *sc)
176 return MFI_READ4(sc, MFI_OMSG0);
180 mfi_read_fw_status_ppc(struct mfi_softc *sc)
182 return MFI_READ4(sc, MFI_OSP0);
186 mfi_check_clear_intr_xscale(struct mfi_softc *sc)
190 status = MFI_READ4(sc, MFI_OSTS);
191 if ((status & MFI_OSTS_INTR_VALID) == 0)
194 MFI_WRITE4(sc, MFI_OSTS, status);
199 mfi_check_clear_intr_ppc(struct mfi_softc *sc)
203 status = MFI_READ4(sc, MFI_OSTS);
204 if (sc->mfi_flags & MFI_FLAGS_1078) {
205 if (!(status & MFI_1078_RM)) {
208 } else if (sc->mfi_flags & MFI_FLAGS_GEN2) {
209 if (!(status & MFI_GEN2_RM)) {
214 MFI_WRITE4(sc, MFI_ODCR0, status);
219 mfi_issue_cmd_xscale(struct mfi_softc *sc,uint32_t bus_add,uint32_t frame_cnt)
221 MFI_WRITE4(sc, MFI_IQP,(bus_add >>3)|frame_cnt);
225 mfi_issue_cmd_ppc(struct mfi_softc *sc,uint32_t bus_add,uint32_t frame_cnt)
227 MFI_WRITE4(sc, MFI_IQP, (bus_add |frame_cnt <<1)|1 );
231 mfi_transition_firmware(struct mfi_softc *sc)
233 uint32_t fw_state, cur_state;
236 fw_state = sc->mfi_read_fw_status(sc)& MFI_FWSTATE_MASK;
237 while (fw_state != MFI_FWSTATE_READY) {
239 device_printf(sc->mfi_dev, "Waiting for firmware to "
241 cur_state = fw_state;
243 case MFI_FWSTATE_FAULT:
244 device_printf(sc->mfi_dev, "Firmware fault\n");
246 case MFI_FWSTATE_WAIT_HANDSHAKE:
247 MFI_WRITE4(sc, MFI_IDB, MFI_FWINIT_CLEAR_HANDSHAKE);
250 case MFI_FWSTATE_OPERATIONAL:
251 MFI_WRITE4(sc, MFI_IDB, MFI_FWINIT_READY);
254 case MFI_FWSTATE_UNDEFINED:
255 case MFI_FWSTATE_BB_INIT:
258 case MFI_FWSTATE_FW_INIT:
259 case MFI_FWSTATE_DEVICE_SCAN:
260 case MFI_FWSTATE_FLUSH_CACHE:
264 device_printf(sc->mfi_dev,"Unknown firmware state %d\n",
268 for (i = 0; i < (max_wait * 10); i++) {
269 fw_state = sc->mfi_read_fw_status(sc) & MFI_FWSTATE_MASK;
270 if (fw_state == cur_state)
275 if (fw_state == cur_state) {
276 device_printf(sc->mfi_dev, "firmware stuck in state "
285 mfi_addr32_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
290 *addr = segs[0].ds_addr;
294 mfi_attach(struct mfi_softc *sc)
297 int error, commsz, framessz, sensesz;
298 int frames, unit, max_fw_sge;
300 device_printf(sc->mfi_dev, "Megaraid SAS driver Ver 3.00 \n");
302 mtx_init(&sc->mfi_io_lock, "MFI I/O lock", NULL, MTX_DEF);
303 sx_init(&sc->mfi_config_lock, "MFI config");
304 TAILQ_INIT(&sc->mfi_ld_tqh);
305 TAILQ_INIT(&sc->mfi_aen_pids);
306 TAILQ_INIT(&sc->mfi_cam_ccbq);
313 if (sc->mfi_flags & MFI_FLAGS_1064R) {
314 sc->mfi_enable_intr = mfi_enable_intr_xscale;
315 sc->mfi_read_fw_status = mfi_read_fw_status_xscale;
316 sc->mfi_check_clear_intr = mfi_check_clear_intr_xscale;
317 sc->mfi_issue_cmd = mfi_issue_cmd_xscale;
320 sc->mfi_enable_intr = mfi_enable_intr_ppc;
321 sc->mfi_read_fw_status = mfi_read_fw_status_ppc;
322 sc->mfi_check_clear_intr = mfi_check_clear_intr_ppc;
323 sc->mfi_issue_cmd = mfi_issue_cmd_ppc;
327 /* Before we get too far, see if the firmware is working */
328 if ((error = mfi_transition_firmware(sc)) != 0) {
329 device_printf(sc->mfi_dev, "Firmware not in READY state, "
330 "error %d\n", error);
335 * Get information needed for sizing the contiguous memory for the
336 * frame pool. Size down the sgl parameter since we know that
337 * we will never need more than what's required for MAXPHYS.
338 * It would be nice if these constants were available at runtime
339 * instead of compile time.
341 status = sc->mfi_read_fw_status(sc);
342 sc->mfi_max_fw_cmds = status & MFI_FWSTATE_MAXCMD_MASK;
343 max_fw_sge = (status & MFI_FWSTATE_MAXSGL_MASK) >> 16;
344 sc->mfi_max_sge = min(max_fw_sge, ((MFI_MAXPHYS / PAGE_SIZE) + 1));
347 * Create the dma tag for data buffers. Used both for block I/O
348 * and for various internal data queries.
350 if (bus_dma_tag_create( sc->mfi_parent_dmat, /* parent */
351 1, 0, /* algnmnt, boundary */
352 BUS_SPACE_MAXADDR, /* lowaddr */
353 BUS_SPACE_MAXADDR, /* highaddr */
354 NULL, NULL, /* filter, filterarg */
355 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
356 sc->mfi_max_sge, /* nsegments */
357 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
358 BUS_DMA_ALLOCNOW, /* flags */
359 busdma_lock_mutex, /* lockfunc */
360 &sc->mfi_io_lock, /* lockfuncarg */
361 &sc->mfi_buffer_dmat)) {
362 device_printf(sc->mfi_dev, "Cannot allocate buffer DMA tag\n");
367 * Allocate DMA memory for the comms queues. Keep it under 4GB for
368 * efficiency. The mfi_hwcomms struct includes space for 1 reply queue
369 * entry, so the calculated size here will be will be 1 more than
370 * mfi_max_fw_cmds. This is apparently a requirement of the hardware.
372 commsz = (sizeof(uint32_t) * sc->mfi_max_fw_cmds) +
373 sizeof(struct mfi_hwcomms);
374 if (bus_dma_tag_create( sc->mfi_parent_dmat, /* parent */
375 1, 0, /* algnmnt, boundary */
376 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
377 BUS_SPACE_MAXADDR, /* highaddr */
378 NULL, NULL, /* filter, filterarg */
379 commsz, /* maxsize */
381 commsz, /* maxsegsize */
383 NULL, NULL, /* lockfunc, lockarg */
384 &sc->mfi_comms_dmat)) {
385 device_printf(sc->mfi_dev, "Cannot allocate comms DMA tag\n");
388 if (bus_dmamem_alloc(sc->mfi_comms_dmat, (void **)&sc->mfi_comms,
389 BUS_DMA_NOWAIT, &sc->mfi_comms_dmamap)) {
390 device_printf(sc->mfi_dev, "Cannot allocate comms memory\n");
393 bzero(sc->mfi_comms, commsz);
394 bus_dmamap_load(sc->mfi_comms_dmat, sc->mfi_comms_dmamap,
395 sc->mfi_comms, commsz, mfi_addr32_cb, &sc->mfi_comms_busaddr, 0);
398 * Allocate DMA memory for the command frames. Keep them in the
399 * lower 4GB for efficiency. Calculate the size of the commands at
400 * the same time; each command is one 64 byte frame plus a set of
401 * additional frames for holding sg lists or other data.
402 * The assumption here is that the SG list will start at the second
403 * frame and not use the unused bytes in the first frame. While this
404 * isn't technically correct, it simplifies the calculation and allows
405 * for command frames that might be larger than an mfi_io_frame.
407 if (sizeof(bus_addr_t) == 8) {
408 sc->mfi_sge_size = sizeof(struct mfi_sg64);
409 sc->mfi_flags |= MFI_FLAGS_SG64;
411 sc->mfi_sge_size = sizeof(struct mfi_sg32);
413 frames = (sc->mfi_sge_size * sc->mfi_max_sge - 1) / MFI_FRAME_SIZE + 2;
414 sc->mfi_cmd_size = frames * MFI_FRAME_SIZE;
415 framessz = sc->mfi_cmd_size * sc->mfi_max_fw_cmds;
416 if (bus_dma_tag_create( sc->mfi_parent_dmat, /* parent */
417 64, 0, /* algnmnt, boundary */
418 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
419 BUS_SPACE_MAXADDR, /* highaddr */
420 NULL, NULL, /* filter, filterarg */
421 framessz, /* maxsize */
423 framessz, /* maxsegsize */
425 NULL, NULL, /* lockfunc, lockarg */
426 &sc->mfi_frames_dmat)) {
427 device_printf(sc->mfi_dev, "Cannot allocate frame DMA tag\n");
430 if (bus_dmamem_alloc(sc->mfi_frames_dmat, (void **)&sc->mfi_frames,
431 BUS_DMA_NOWAIT, &sc->mfi_frames_dmamap)) {
432 device_printf(sc->mfi_dev, "Cannot allocate frames memory\n");
435 bzero(sc->mfi_frames, framessz);
436 bus_dmamap_load(sc->mfi_frames_dmat, sc->mfi_frames_dmamap,
437 sc->mfi_frames, framessz, mfi_addr32_cb, &sc->mfi_frames_busaddr,0);
440 * Allocate DMA memory for the frame sense data. Keep them in the
441 * lower 4GB for efficiency
443 sensesz = sc->mfi_max_fw_cmds * MFI_SENSE_LEN;
444 if (bus_dma_tag_create( sc->mfi_parent_dmat, /* parent */
445 4, 0, /* algnmnt, boundary */
446 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
447 BUS_SPACE_MAXADDR, /* highaddr */
448 NULL, NULL, /* filter, filterarg */
449 sensesz, /* maxsize */
451 sensesz, /* maxsegsize */
453 NULL, NULL, /* lockfunc, lockarg */
454 &sc->mfi_sense_dmat)) {
455 device_printf(sc->mfi_dev, "Cannot allocate sense DMA tag\n");
458 if (bus_dmamem_alloc(sc->mfi_sense_dmat, (void **)&sc->mfi_sense,
459 BUS_DMA_NOWAIT, &sc->mfi_sense_dmamap)) {
460 device_printf(sc->mfi_dev, "Cannot allocate sense memory\n");
463 bus_dmamap_load(sc->mfi_sense_dmat, sc->mfi_sense_dmamap,
464 sc->mfi_sense, sensesz, mfi_addr32_cb, &sc->mfi_sense_busaddr, 0);
466 if ((error = mfi_alloc_commands(sc)) != 0)
469 if ((error = mfi_comms_init(sc)) != 0)
472 if ((error = mfi_get_controller_info(sc)) != 0)
475 mtx_lock(&sc->mfi_io_lock);
476 if ((error = mfi_aen_setup(sc, 0), 0) != 0) {
477 mtx_unlock(&sc->mfi_io_lock);
480 mtx_unlock(&sc->mfi_io_lock);
483 * Set up the interrupt handler. XXX This should happen in
487 if ((sc->mfi_irq = bus_alloc_resource_any(sc->mfi_dev, SYS_RES_IRQ,
488 &sc->mfi_irq_rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
489 device_printf(sc->mfi_dev, "Cannot allocate interrupt\n");
492 if (bus_setup_intr(sc->mfi_dev, sc->mfi_irq, INTR_MPSAFE|INTR_TYPE_BIO,
493 NULL, mfi_intr, sc, &sc->mfi_intr)) {
494 device_printf(sc->mfi_dev, "Cannot set up interrupt\n");
498 /* Register a config hook to probe the bus for arrays */
499 sc->mfi_ich.ich_func = mfi_startup;
500 sc->mfi_ich.ich_arg = sc;
501 if (config_intrhook_establish(&sc->mfi_ich) != 0) {
502 device_printf(sc->mfi_dev, "Cannot establish configuration "
508 * Register a shutdown handler.
510 if ((sc->mfi_eh = EVENTHANDLER_REGISTER(shutdown_final, mfi_shutdown,
511 sc, SHUTDOWN_PRI_DEFAULT)) == NULL) {
512 device_printf(sc->mfi_dev, "Warning: shutdown event "
513 "registration failed\n");
517 * Create the control device for doing management
519 unit = device_get_unit(sc->mfi_dev);
520 sc->mfi_cdev = make_dev(&mfi_cdevsw, unit, UID_ROOT, GID_OPERATOR,
521 0640, "mfi%d", unit);
523 make_dev_alias(sc->mfi_cdev, "megaraid_sas_ioctl_node");
524 if (sc->mfi_cdev != NULL)
525 sc->mfi_cdev->si_drv1 = sc;
526 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->mfi_dev),
527 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->mfi_dev)),
528 OID_AUTO, "delete_busy_volumes", CTLFLAG_RW,
529 &sc->mfi_delete_busy_volumes, 0, "Allow removal of busy volumes");
530 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->mfi_dev),
531 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->mfi_dev)),
532 OID_AUTO, "keep_deleted_volumes", CTLFLAG_RW,
533 &sc->mfi_keep_deleted_volumes, 0,
534 "Don't detach the mfid device for a busy volume that is deleted");
536 device_add_child(sc->mfi_dev, "mfip", -1);
537 bus_generic_attach(sc->mfi_dev);
539 /* Start the timeout watchdog */
540 callout_init(&sc->mfi_watchdog_callout, CALLOUT_MPSAFE);
541 callout_reset(&sc->mfi_watchdog_callout, MFI_CMD_TIMEOUT * hz,
548 mfi_alloc_commands(struct mfi_softc *sc)
550 struct mfi_command *cm;
554 * XXX Should we allocate all the commands up front, or allocate on
555 * demand later like 'aac' does?
557 ncmds = MIN(mfi_max_cmds, sc->mfi_max_fw_cmds);
559 device_printf(sc->mfi_dev, "Max fw cmds= %d, sizing driver "
560 "pool to %d\n", sc->mfi_max_fw_cmds, ncmds);
562 sc->mfi_commands = malloc(sizeof(struct mfi_command) * ncmds, M_MFIBUF,
565 for (i = 0; i < ncmds; i++) {
566 cm = &sc->mfi_commands[i];
567 cm->cm_frame = (union mfi_frame *)((uintptr_t)sc->mfi_frames +
568 sc->mfi_cmd_size * i);
569 cm->cm_frame_busaddr = sc->mfi_frames_busaddr +
570 sc->mfi_cmd_size * i;
571 cm->cm_frame->header.context = i;
572 cm->cm_sense = &sc->mfi_sense[i];
573 cm->cm_sense_busaddr= sc->mfi_sense_busaddr + MFI_SENSE_LEN * i;
576 if (bus_dmamap_create(sc->mfi_buffer_dmat, 0,
577 &cm->cm_dmamap) == 0)
578 mfi_release_command(cm);
581 sc->mfi_total_cmds++;
588 mfi_release_command(struct mfi_command *cm)
590 struct mfi_frame_header *hdr;
594 * Zero out the important fields of the frame, but make sure the
595 * context field is preserved. For efficiency, handle the fields
596 * as 32 bit words. Clear out the first S/G entry too for safety.
598 hdr = &cm->cm_frame->header;
599 if (cm->cm_data != NULL && hdr->sg_count) {
600 cm->cm_sg->sg32[0].len = 0;
601 cm->cm_sg->sg32[0].addr = 0;
604 hdr_data = (uint32_t *)cm->cm_frame;
605 hdr_data[0] = 0; /* cmd, sense_len, cmd_status, scsi_status */
606 hdr_data[1] = 0; /* target_id, lun_id, cdb_len, sg_count */
607 hdr_data[4] = 0; /* flags, timeout */
608 hdr_data[5] = 0; /* data_len */
610 cm->cm_extra_frames = 0;
612 cm->cm_complete = NULL;
613 cm->cm_private = NULL;
616 cm->cm_total_frame_size = 0;
618 mfi_enqueue_free(cm);
622 mfi_dcmd_command(struct mfi_softc *sc, struct mfi_command **cmp, uint32_t opcode,
623 void **bufp, size_t bufsize)
625 struct mfi_command *cm;
626 struct mfi_dcmd_frame *dcmd;
629 mtx_assert(&sc->mfi_io_lock, MA_OWNED);
631 cm = mfi_dequeue_free(sc);
635 if ((bufsize > 0) && (bufp != NULL)) {
637 buf = malloc(bufsize, M_MFIBUF, M_NOWAIT|M_ZERO);
639 mfi_release_command(cm);
648 dcmd = &cm->cm_frame->dcmd;
649 bzero(dcmd->mbox, MFI_MBOX_SIZE);
650 dcmd->header.cmd = MFI_CMD_DCMD;
651 dcmd->header.timeout = 0;
652 dcmd->header.flags = 0;
653 dcmd->header.data_len = bufsize;
654 dcmd->opcode = opcode;
655 cm->cm_sg = &dcmd->sgl;
656 cm->cm_total_frame_size = MFI_DCMD_FRAME_SIZE;
659 cm->cm_private = buf;
660 cm->cm_len = bufsize;
663 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
669 mfi_comms_init(struct mfi_softc *sc)
671 struct mfi_command *cm;
672 struct mfi_init_frame *init;
673 struct mfi_init_qinfo *qinfo;
676 mtx_lock(&sc->mfi_io_lock);
677 if ((cm = mfi_dequeue_free(sc)) == NULL)
681 * Abuse the SG list area of the frame to hold the init_qinfo
684 init = &cm->cm_frame->init;
685 qinfo = (struct mfi_init_qinfo *)((uintptr_t)init + MFI_FRAME_SIZE);
687 bzero(qinfo, sizeof(struct mfi_init_qinfo));
688 qinfo->rq_entries = sc->mfi_max_fw_cmds + 1;
689 qinfo->rq_addr_lo = sc->mfi_comms_busaddr +
690 offsetof(struct mfi_hwcomms, hw_reply_q);
691 qinfo->pi_addr_lo = sc->mfi_comms_busaddr +
692 offsetof(struct mfi_hwcomms, hw_pi);
693 qinfo->ci_addr_lo = sc->mfi_comms_busaddr +
694 offsetof(struct mfi_hwcomms, hw_ci);
696 init->header.cmd = MFI_CMD_INIT;
697 init->header.data_len = sizeof(struct mfi_init_qinfo);
698 init->qinfo_new_addr_lo = cm->cm_frame_busaddr + MFI_FRAME_SIZE;
700 cm->cm_flags = MFI_CMD_POLLED;
702 if ((error = mfi_mapcmd(sc, cm)) != 0) {
703 device_printf(sc->mfi_dev, "failed to send init command\n");
704 mtx_unlock(&sc->mfi_io_lock);
707 mfi_release_command(cm);
708 mtx_unlock(&sc->mfi_io_lock);
714 mfi_get_controller_info(struct mfi_softc *sc)
716 struct mfi_command *cm = NULL;
717 struct mfi_ctrl_info *ci = NULL;
718 uint32_t max_sectors_1, max_sectors_2;
721 mtx_lock(&sc->mfi_io_lock);
722 error = mfi_dcmd_command(sc, &cm, MFI_DCMD_CTRL_GETINFO,
723 (void **)&ci, sizeof(*ci));
726 cm->cm_flags = MFI_CMD_DATAIN | MFI_CMD_POLLED;
728 if ((error = mfi_mapcmd(sc, cm)) != 0) {
729 device_printf(sc->mfi_dev, "Failed to get controller info\n");
730 sc->mfi_max_io = (sc->mfi_max_sge - 1) * PAGE_SIZE /
736 bus_dmamap_sync(sc->mfi_buffer_dmat, cm->cm_dmamap,
737 BUS_DMASYNC_POSTREAD);
738 bus_dmamap_unload(sc->mfi_buffer_dmat, cm->cm_dmamap);
740 max_sectors_1 = (1 << ci->stripe_sz_ops.min) * ci->max_strips_per_io;
741 max_sectors_2 = ci->max_request_size;
742 sc->mfi_max_io = min(max_sectors_1, max_sectors_2);
748 mfi_release_command(cm);
749 mtx_unlock(&sc->mfi_io_lock);
754 mfi_get_log_state(struct mfi_softc *sc, struct mfi_evt_log_state **log_state)
756 struct mfi_command *cm = NULL;
759 error = mfi_dcmd_command(sc, &cm, MFI_DCMD_CTRL_EVENT_GETINFO,
760 (void **)log_state, sizeof(**log_state));
763 cm->cm_flags = MFI_CMD_DATAIN | MFI_CMD_POLLED;
765 if ((error = mfi_mapcmd(sc, cm)) != 0) {
766 device_printf(sc->mfi_dev, "Failed to get log state\n");
770 bus_dmamap_sync(sc->mfi_buffer_dmat, cm->cm_dmamap,
771 BUS_DMASYNC_POSTREAD);
772 bus_dmamap_unload(sc->mfi_buffer_dmat, cm->cm_dmamap);
776 mfi_release_command(cm);
782 mfi_aen_setup(struct mfi_softc *sc, uint32_t seq_start)
784 struct mfi_evt_log_state *log_state = NULL;
785 union mfi_evt class_locale;
789 class_locale.members.reserved = 0;
790 class_locale.members.locale = mfi_event_locale;
791 class_locale.members.class = mfi_event_class;
793 if (seq_start == 0) {
794 error = mfi_get_log_state(sc, &log_state);
797 free(log_state, M_MFIBUF);
802 * Walk through any events that fired since the last
805 mfi_parse_entries(sc, log_state->shutdown_seq_num,
806 log_state->newest_seq_num);
807 seq = log_state->newest_seq_num;
810 mfi_aen_register(sc, seq, class_locale.word);
811 free(log_state, M_MFIBUF);
817 mfi_wait_command(struct mfi_softc *sc, struct mfi_command *cm)
820 mtx_assert(&sc->mfi_io_lock, MA_OWNED);
821 cm->cm_complete = NULL;
825 * MegaCli can issue a DCMD of 0. In this case do nothing
826 * and return 0 to it as status
828 if (cm->cm_frame->dcmd.opcode == 0) {
829 cm->cm_frame->header.cmd_status = MFI_STAT_OK;
831 return (cm->cm_error);
833 mfi_enqueue_ready(cm);
835 if ((cm->cm_flags & MFI_CMD_COMPLETED) == 0)
836 msleep(cm, &sc->mfi_io_lock, PRIBIO, "mfiwait", 0);
837 return (cm->cm_error);
841 mfi_free(struct mfi_softc *sc)
843 struct mfi_command *cm;
846 callout_drain(&sc->mfi_watchdog_callout);
848 if (sc->mfi_cdev != NULL)
849 destroy_dev(sc->mfi_cdev);
851 if (sc->mfi_total_cmds != 0) {
852 for (i = 0; i < sc->mfi_total_cmds; i++) {
853 cm = &sc->mfi_commands[i];
854 bus_dmamap_destroy(sc->mfi_buffer_dmat, cm->cm_dmamap);
856 free(sc->mfi_commands, M_MFIBUF);
860 bus_teardown_intr(sc->mfi_dev, sc->mfi_irq, sc->mfi_intr);
861 if (sc->mfi_irq != NULL)
862 bus_release_resource(sc->mfi_dev, SYS_RES_IRQ, sc->mfi_irq_rid,
865 if (sc->mfi_sense_busaddr != 0)
866 bus_dmamap_unload(sc->mfi_sense_dmat, sc->mfi_sense_dmamap);
867 if (sc->mfi_sense != NULL)
868 bus_dmamem_free(sc->mfi_sense_dmat, sc->mfi_sense,
869 sc->mfi_sense_dmamap);
870 if (sc->mfi_sense_dmat != NULL)
871 bus_dma_tag_destroy(sc->mfi_sense_dmat);
873 if (sc->mfi_frames_busaddr != 0)
874 bus_dmamap_unload(sc->mfi_frames_dmat, sc->mfi_frames_dmamap);
875 if (sc->mfi_frames != NULL)
876 bus_dmamem_free(sc->mfi_frames_dmat, sc->mfi_frames,
877 sc->mfi_frames_dmamap);
878 if (sc->mfi_frames_dmat != NULL)
879 bus_dma_tag_destroy(sc->mfi_frames_dmat);
881 if (sc->mfi_comms_busaddr != 0)
882 bus_dmamap_unload(sc->mfi_comms_dmat, sc->mfi_comms_dmamap);
883 if (sc->mfi_comms != NULL)
884 bus_dmamem_free(sc->mfi_comms_dmat, sc->mfi_comms,
885 sc->mfi_comms_dmamap);
886 if (sc->mfi_comms_dmat != NULL)
887 bus_dma_tag_destroy(sc->mfi_comms_dmat);
889 if (sc->mfi_buffer_dmat != NULL)
890 bus_dma_tag_destroy(sc->mfi_buffer_dmat);
891 if (sc->mfi_parent_dmat != NULL)
892 bus_dma_tag_destroy(sc->mfi_parent_dmat);
894 if (mtx_initialized(&sc->mfi_io_lock)) {
895 mtx_destroy(&sc->mfi_io_lock);
896 sx_destroy(&sc->mfi_config_lock);
903 mfi_startup(void *arg)
905 struct mfi_softc *sc;
907 sc = (struct mfi_softc *)arg;
909 config_intrhook_disestablish(&sc->mfi_ich);
911 sc->mfi_enable_intr(sc);
912 sx_xlock(&sc->mfi_config_lock);
913 mtx_lock(&sc->mfi_io_lock);
915 mtx_unlock(&sc->mfi_io_lock);
916 sx_xunlock(&sc->mfi_config_lock);
922 struct mfi_softc *sc;
923 struct mfi_command *cm;
924 uint32_t pi, ci, context;
926 sc = (struct mfi_softc *)arg;
928 if (sc->mfi_check_clear_intr(sc))
931 pi = sc->mfi_comms->hw_pi;
932 ci = sc->mfi_comms->hw_ci;
933 mtx_lock(&sc->mfi_io_lock);
935 context = sc->mfi_comms->hw_reply_q[ci];
936 if (context < sc->mfi_max_fw_cmds) {
937 cm = &sc->mfi_commands[context];
940 mfi_complete(sc, cm);
942 if (++ci == (sc->mfi_max_fw_cmds + 1)) {
947 sc->mfi_comms->hw_ci = ci;
949 /* Give defered I/O a chance to run */
950 if (sc->mfi_flags & MFI_FLAGS_QFRZN)
951 sc->mfi_flags &= ~MFI_FLAGS_QFRZN;
953 mtx_unlock(&sc->mfi_io_lock);
959 mfi_shutdown(struct mfi_softc *sc)
961 struct mfi_dcmd_frame *dcmd;
962 struct mfi_command *cm;
965 mtx_lock(&sc->mfi_io_lock);
966 error = mfi_dcmd_command(sc, &cm, MFI_DCMD_CTRL_SHUTDOWN, NULL, 0);
968 mtx_unlock(&sc->mfi_io_lock);
972 if (sc->mfi_aen_cm != NULL)
973 mfi_abort(sc, sc->mfi_aen_cm);
975 dcmd = &cm->cm_frame->dcmd;
976 dcmd->header.flags = MFI_FRAME_DIR_NONE;
977 cm->cm_flags = MFI_CMD_POLLED;
980 if ((error = mfi_mapcmd(sc, cm)) != 0) {
981 device_printf(sc->mfi_dev, "Failed to shutdown controller\n");
984 mfi_release_command(cm);
985 mtx_unlock(&sc->mfi_io_lock);
990 mfi_ldprobe(struct mfi_softc *sc)
992 struct mfi_frame_header *hdr;
993 struct mfi_command *cm = NULL;
994 struct mfi_ld_list *list = NULL;
998 sx_assert(&sc->mfi_config_lock, SA_XLOCKED);
999 mtx_assert(&sc->mfi_io_lock, MA_OWNED);
1001 error = mfi_dcmd_command(sc, &cm, MFI_DCMD_LD_GET_LIST,
1002 (void **)&list, sizeof(*list));
1006 cm->cm_flags = MFI_CMD_DATAIN;
1007 if (mfi_wait_command(sc, cm) != 0) {
1008 device_printf(sc->mfi_dev, "Failed to get device listing\n");
1012 hdr = &cm->cm_frame->header;
1013 if (hdr->cmd_status != MFI_STAT_OK) {
1014 device_printf(sc->mfi_dev, "MFI_DCMD_LD_GET_LIST failed %x\n",
1019 for (i = 0; i < list->ld_count; i++) {
1020 TAILQ_FOREACH(ld, &sc->mfi_ld_tqh, ld_link) {
1021 if (ld->ld_id == list->ld_list[i].ld.v.target_id)
1024 mfi_add_ld(sc, list->ld_list[i].ld.v.target_id);
1029 free(list, M_MFIBUF);
1031 mfi_release_command(cm);
1037 * The timestamp is the number of seconds since 00:00 Jan 1, 2000. If
1038 * the bits in 24-31 are all set, then it is the number of seconds since
1042 format_timestamp(uint32_t timestamp)
1044 static char buffer[32];
1046 if ((timestamp & 0xff000000) == 0xff000000)
1047 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
1050 snprintf(buffer, sizeof(buffer), "%us", timestamp);
1055 format_class(int8_t class)
1057 static char buffer[6];
1060 case MFI_EVT_CLASS_DEBUG:
1062 case MFI_EVT_CLASS_PROGRESS:
1063 return ("progress");
1064 case MFI_EVT_CLASS_INFO:
1066 case MFI_EVT_CLASS_WARNING:
1068 case MFI_EVT_CLASS_CRITICAL:
1070 case MFI_EVT_CLASS_FATAL:
1072 case MFI_EVT_CLASS_DEAD:
1075 snprintf(buffer, sizeof(buffer), "%d", class);
1081 mfi_decode_evt(struct mfi_softc *sc, struct mfi_evt_detail *detail)
1084 device_printf(sc->mfi_dev, "%d (%s/0x%04x/%s) - %s\n", detail->seq,
1085 format_timestamp(detail->time), detail->class.members.locale,
1086 format_class(detail->class.members.class), detail->description);
1090 mfi_aen_register(struct mfi_softc *sc, int seq, int locale)
1092 struct mfi_command *cm;
1093 struct mfi_dcmd_frame *dcmd;
1094 union mfi_evt current_aen, prior_aen;
1095 struct mfi_evt_detail *ed = NULL;
1098 current_aen.word = locale;
1099 if (sc->mfi_aen_cm != NULL) {
1101 ((uint32_t *)&sc->mfi_aen_cm->cm_frame->dcmd.mbox)[1];
1102 if (prior_aen.members.class <= current_aen.members.class &&
1103 !((prior_aen.members.locale & current_aen.members.locale)
1104 ^current_aen.members.locale)) {
1107 prior_aen.members.locale |= current_aen.members.locale;
1108 if (prior_aen.members.class
1109 < current_aen.members.class)
1110 current_aen.members.class =
1111 prior_aen.members.class;
1112 mfi_abort(sc, sc->mfi_aen_cm);
1116 error = mfi_dcmd_command(sc, &cm, MFI_DCMD_CTRL_EVENT_WAIT,
1117 (void **)&ed, sizeof(*ed));
1122 dcmd = &cm->cm_frame->dcmd;
1123 ((uint32_t *)&dcmd->mbox)[0] = seq;
1124 ((uint32_t *)&dcmd->mbox)[1] = locale;
1125 cm->cm_flags = MFI_CMD_DATAIN;
1126 cm->cm_complete = mfi_aen_complete;
1128 sc->mfi_aen_cm = cm;
1130 mfi_enqueue_ready(cm);
1138 mfi_aen_complete(struct mfi_command *cm)
1140 struct mfi_frame_header *hdr;
1141 struct mfi_softc *sc;
1142 struct mfi_evt_detail *detail;
1143 struct mfi_aen *mfi_aen_entry, *tmp;
1144 int seq = 0, aborted = 0;
1147 hdr = &cm->cm_frame->header;
1149 if (sc->mfi_aen_cm == NULL)
1152 if (sc->mfi_aen_cm->cm_aen_abort || hdr->cmd_status == 0xff) {
1153 sc->mfi_aen_cm->cm_aen_abort = 0;
1156 sc->mfi_aen_triggered = 1;
1157 if (sc->mfi_poll_waiting) {
1158 sc->mfi_poll_waiting = 0;
1159 selwakeup(&sc->mfi_select);
1161 detail = cm->cm_data;
1163 * XXX If this function is too expensive or is recursive, then
1164 * events should be put onto a queue and processed later.
1166 mfi_decode_evt(sc, detail);
1167 seq = detail->seq + 1;
1168 TAILQ_FOREACH_SAFE(mfi_aen_entry, &sc->mfi_aen_pids, aen_link, tmp) {
1169 TAILQ_REMOVE(&sc->mfi_aen_pids, mfi_aen_entry,
1171 PROC_LOCK(mfi_aen_entry->p);
1172 psignal(mfi_aen_entry->p, SIGIO);
1173 PROC_UNLOCK(mfi_aen_entry->p);
1174 free(mfi_aen_entry, M_MFIBUF);
1178 free(cm->cm_data, M_MFIBUF);
1179 sc->mfi_aen_cm = NULL;
1180 wakeup(&sc->mfi_aen_cm);
1181 mfi_release_command(cm);
1183 /* set it up again so the driver can catch more events */
1185 mfi_aen_setup(sc, seq);
1189 #define MAX_EVENTS 15
1192 mfi_parse_entries(struct mfi_softc *sc, int start_seq, int stop_seq)
1194 struct mfi_command *cm;
1195 struct mfi_dcmd_frame *dcmd;
1196 struct mfi_evt_list *el;
1197 union mfi_evt class_locale;
1198 int error, i, seq, size;
1200 class_locale.members.reserved = 0;
1201 class_locale.members.locale = mfi_event_locale;
1202 class_locale.members.class = mfi_event_class;
1204 size = sizeof(struct mfi_evt_list) + sizeof(struct mfi_evt_detail)
1206 el = malloc(size, M_MFIBUF, M_NOWAIT | M_ZERO);
1210 for (seq = start_seq;;) {
1211 if ((cm = mfi_dequeue_free(sc)) == NULL) {
1216 dcmd = &cm->cm_frame->dcmd;
1217 bzero(dcmd->mbox, MFI_MBOX_SIZE);
1218 dcmd->header.cmd = MFI_CMD_DCMD;
1219 dcmd->header.timeout = 0;
1220 dcmd->header.data_len = size;
1221 dcmd->opcode = MFI_DCMD_CTRL_EVENT_GET;
1222 ((uint32_t *)&dcmd->mbox)[0] = seq;
1223 ((uint32_t *)&dcmd->mbox)[1] = class_locale.word;
1224 cm->cm_sg = &dcmd->sgl;
1225 cm->cm_total_frame_size = MFI_DCMD_FRAME_SIZE;
1226 cm->cm_flags = MFI_CMD_DATAIN | MFI_CMD_POLLED;
1230 if ((error = mfi_mapcmd(sc, cm)) != 0) {
1231 device_printf(sc->mfi_dev,
1232 "Failed to get controller entries\n");
1233 mfi_release_command(cm);
1237 bus_dmamap_sync(sc->mfi_buffer_dmat, cm->cm_dmamap,
1238 BUS_DMASYNC_POSTREAD);
1239 bus_dmamap_unload(sc->mfi_buffer_dmat, cm->cm_dmamap);
1241 if (dcmd->header.cmd_status == MFI_STAT_NOT_FOUND) {
1242 mfi_release_command(cm);
1245 if (dcmd->header.cmd_status != MFI_STAT_OK) {
1246 device_printf(sc->mfi_dev,
1247 "Error %d fetching controller entries\n",
1248 dcmd->header.cmd_status);
1249 mfi_release_command(cm);
1252 mfi_release_command(cm);
1254 for (i = 0; i < el->count; i++) {
1256 * If this event is newer than 'stop_seq' then
1257 * break out of the loop. Note that the log
1258 * is a circular buffer so we have to handle
1259 * the case that our stop point is earlier in
1260 * the buffer than our start point.
1262 if (el->event[i].seq >= stop_seq) {
1263 if (start_seq <= stop_seq)
1265 else if (el->event[i].seq < start_seq)
1268 mfi_decode_evt(sc, &el->event[i]);
1270 seq = el->event[el->count - 1].seq + 1;
1278 mfi_add_ld(struct mfi_softc *sc, int id)
1280 struct mfi_command *cm;
1281 struct mfi_dcmd_frame *dcmd = NULL;
1282 struct mfi_ld_info *ld_info = NULL;
1285 mtx_assert(&sc->mfi_io_lock, MA_OWNED);
1287 error = mfi_dcmd_command(sc, &cm, MFI_DCMD_LD_GET_INFO,
1288 (void **)&ld_info, sizeof(*ld_info));
1290 device_printf(sc->mfi_dev,
1291 "Failed to allocate for MFI_DCMD_LD_GET_INFO %d\n", error);
1293 free(ld_info, M_MFIBUF);
1296 cm->cm_flags = MFI_CMD_DATAIN;
1297 dcmd = &cm->cm_frame->dcmd;
1299 if (mfi_wait_command(sc, cm) != 0) {
1300 device_printf(sc->mfi_dev,
1301 "Failed to get logical drive: %d\n", id);
1302 free(ld_info, M_MFIBUF);
1306 mfi_add_ld_complete(cm);
1311 mfi_add_ld_complete(struct mfi_command *cm)
1313 struct mfi_frame_header *hdr;
1314 struct mfi_ld_info *ld_info;
1315 struct mfi_softc *sc;
1319 hdr = &cm->cm_frame->header;
1320 ld_info = cm->cm_private;
1322 if (hdr->cmd_status != MFI_STAT_OK) {
1323 free(ld_info, M_MFIBUF);
1324 mfi_release_command(cm);
1327 mfi_release_command(cm);
1329 mtx_unlock(&sc->mfi_io_lock);
1331 if ((child = device_add_child(sc->mfi_dev, "mfid", -1)) == NULL) {
1332 device_printf(sc->mfi_dev, "Failed to add logical disk\n");
1333 free(ld_info, M_MFIBUF);
1335 mtx_lock(&sc->mfi_io_lock);
1339 device_set_ivars(child, ld_info);
1340 device_set_desc(child, "MFI Logical Disk");
1341 bus_generic_attach(sc->mfi_dev);
1343 mtx_lock(&sc->mfi_io_lock);
1346 static struct mfi_command *
1347 mfi_bio_command(struct mfi_softc *sc)
1349 struct mfi_io_frame *io;
1350 struct mfi_command *cm;
1352 int flags, blkcount;
1354 if ((cm = mfi_dequeue_free(sc)) == NULL)
1357 if ((bio = mfi_dequeue_bio(sc)) == NULL) {
1358 mfi_release_command(cm);
1362 io = &cm->cm_frame->io;
1363 switch (bio->bio_cmd & 0x03) {
1365 io->header.cmd = MFI_CMD_LD_READ;
1366 flags = MFI_CMD_DATAIN;
1369 io->header.cmd = MFI_CMD_LD_WRITE;
1370 flags = MFI_CMD_DATAOUT;
1373 panic("Invalid bio command");
1376 /* Cheat with the sector length to avoid a non-constant division */
1377 blkcount = (bio->bio_bcount + MFI_SECTOR_LEN - 1) / MFI_SECTOR_LEN;
1378 io->header.target_id = (uintptr_t)bio->bio_driver1;
1379 io->header.timeout = 0;
1380 io->header.flags = 0;
1381 io->header.sense_len = MFI_SENSE_LEN;
1382 io->header.data_len = blkcount;
1383 io->sense_addr_lo = cm->cm_sense_busaddr;
1384 io->sense_addr_hi = 0;
1385 io->lba_hi = (bio->bio_pblkno & 0xffffffff00000000) >> 32;
1386 io->lba_lo = bio->bio_pblkno & 0xffffffff;
1387 cm->cm_complete = mfi_bio_complete;
1388 cm->cm_private = bio;
1389 cm->cm_data = bio->bio_data;
1390 cm->cm_len = bio->bio_bcount;
1391 cm->cm_sg = &io->sgl;
1392 cm->cm_total_frame_size = MFI_IO_FRAME_SIZE;
1393 cm->cm_flags = flags;
1398 mfi_bio_complete(struct mfi_command *cm)
1401 struct mfi_frame_header *hdr;
1402 struct mfi_softc *sc;
1404 bio = cm->cm_private;
1405 hdr = &cm->cm_frame->header;
1408 if ((hdr->cmd_status != 0) || (hdr->scsi_status != 0)) {
1409 bio->bio_flags |= BIO_ERROR;
1410 bio->bio_error = EIO;
1411 device_printf(sc->mfi_dev, "I/O error, status= %d "
1412 "scsi_status= %d\n", hdr->cmd_status, hdr->scsi_status);
1413 mfi_print_sense(cm->cm_sc, cm->cm_sense);
1414 } else if (cm->cm_error != 0) {
1415 bio->bio_flags |= BIO_ERROR;
1418 mfi_release_command(cm);
1419 mfi_disk_complete(bio);
1423 mfi_startio(struct mfi_softc *sc)
1425 struct mfi_command *cm;
1426 struct ccb_hdr *ccbh;
1429 /* Don't bother if we're short on resources */
1430 if (sc->mfi_flags & MFI_FLAGS_QFRZN)
1433 /* Try a command that has already been prepared */
1434 cm = mfi_dequeue_ready(sc);
1437 if ((ccbh = TAILQ_FIRST(&sc->mfi_cam_ccbq)) != NULL)
1438 cm = sc->mfi_cam_start(ccbh);
1441 /* Nope, so look for work on the bioq */
1443 cm = mfi_bio_command(sc);
1445 /* No work available, so exit */
1449 /* Send the command to the controller */
1450 if (mfi_mapcmd(sc, cm) != 0) {
1451 mfi_requeue_ready(cm);
1458 mfi_mapcmd(struct mfi_softc *sc, struct mfi_command *cm)
1462 mtx_assert(&sc->mfi_io_lock, MA_OWNED);
1464 if (cm->cm_data != NULL) {
1465 polled = (cm->cm_flags & MFI_CMD_POLLED) ? BUS_DMA_NOWAIT : 0;
1466 error = bus_dmamap_load(sc->mfi_buffer_dmat, cm->cm_dmamap,
1467 cm->cm_data, cm->cm_len, mfi_data_cb, cm, polled);
1468 if (error == EINPROGRESS) {
1469 sc->mfi_flags |= MFI_FLAGS_QFRZN;
1473 error = mfi_send_frame(sc, cm);
1480 mfi_data_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1482 struct mfi_frame_header *hdr;
1483 struct mfi_command *cm;
1485 struct mfi_softc *sc;
1488 cm = (struct mfi_command *)arg;
1490 hdr = &cm->cm_frame->header;
1494 printf("error %d in callback\n", error);
1495 cm->cm_error = error;
1496 mfi_complete(sc, cm);
1500 if ((sc->mfi_flags & MFI_FLAGS_SG64) == 0) {
1501 for (i = 0; i < nsegs; i++) {
1502 sgl->sg32[i].addr = segs[i].ds_addr;
1503 sgl->sg32[i].len = segs[i].ds_len;
1506 for (i = 0; i < nsegs; i++) {
1507 sgl->sg64[i].addr = segs[i].ds_addr;
1508 sgl->sg64[i].len = segs[i].ds_len;
1510 hdr->flags |= MFI_FRAME_SGL64;
1512 hdr->sg_count = nsegs;
1515 if (cm->cm_flags & MFI_CMD_DATAIN) {
1516 dir |= BUS_DMASYNC_PREREAD;
1517 hdr->flags |= MFI_FRAME_DIR_READ;
1519 if (cm->cm_flags & MFI_CMD_DATAOUT) {
1520 dir |= BUS_DMASYNC_PREWRITE;
1521 hdr->flags |= MFI_FRAME_DIR_WRITE;
1523 bus_dmamap_sync(sc->mfi_buffer_dmat, cm->cm_dmamap, dir);
1524 cm->cm_flags |= MFI_CMD_MAPPED;
1527 * Instead of calculating the total number of frames in the
1528 * compound frame, it's already assumed that there will be at
1529 * least 1 frame, so don't compensate for the modulo of the
1530 * following division.
1532 cm->cm_total_frame_size += (sc->mfi_sge_size * nsegs);
1533 cm->cm_extra_frames = (cm->cm_total_frame_size - 1) / MFI_FRAME_SIZE;
1535 mfi_send_frame(sc, cm);
1541 mfi_send_frame(struct mfi_softc *sc, struct mfi_command *cm)
1543 struct mfi_frame_header *hdr;
1544 int tm = MFI_POLL_TIMEOUT_SECS * 1000;
1546 hdr = &cm->cm_frame->header;
1548 if ((cm->cm_flags & MFI_CMD_POLLED) == 0) {
1549 cm->cm_timestamp = time_uptime;
1550 mfi_enqueue_busy(cm);
1552 hdr->cmd_status = 0xff;
1553 hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
1557 * The bus address of the command is aligned on a 64 byte boundary,
1558 * leaving the least 6 bits as zero. For whatever reason, the
1559 * hardware wants the address shifted right by three, leaving just
1560 * 3 zero bits. These three bits are then used as a prefetching
1561 * hint for the hardware to predict how many frames need to be
1562 * fetched across the bus. If a command has more than 8 frames
1563 * then the 3 bits are set to 0x7 and the firmware uses other
1564 * information in the command to determine the total amount to fetch.
1565 * However, FreeBSD doesn't support I/O larger than 128K, so 8 frames
1566 * is enough for both 32bit and 64bit systems.
1568 if (cm->cm_extra_frames > 7)
1569 cm->cm_extra_frames = 7;
1571 sc->mfi_issue_cmd(sc,cm->cm_frame_busaddr,cm->cm_extra_frames);
1573 if ((cm->cm_flags & MFI_CMD_POLLED) == 0)
1576 /* This is a polled command, so busy-wait for it to complete. */
1577 while (hdr->cmd_status == 0xff) {
1584 if (hdr->cmd_status == 0xff) {
1585 device_printf(sc->mfi_dev, "Frame %p timed out "
1586 "command 0x%X\n", hdr, cm->cm_frame->dcmd.opcode);
1594 mfi_complete(struct mfi_softc *sc, struct mfi_command *cm)
1598 if ((cm->cm_flags & MFI_CMD_MAPPED) != 0) {
1600 if (cm->cm_flags & MFI_CMD_DATAIN)
1601 dir |= BUS_DMASYNC_POSTREAD;
1602 if (cm->cm_flags & MFI_CMD_DATAOUT)
1603 dir |= BUS_DMASYNC_POSTWRITE;
1605 bus_dmamap_sync(sc->mfi_buffer_dmat, cm->cm_dmamap, dir);
1606 bus_dmamap_unload(sc->mfi_buffer_dmat, cm->cm_dmamap);
1607 cm->cm_flags &= ~MFI_CMD_MAPPED;
1610 cm->cm_flags |= MFI_CMD_COMPLETED;
1612 if (cm->cm_complete != NULL)
1613 cm->cm_complete(cm);
1619 mfi_abort(struct mfi_softc *sc, struct mfi_command *cm_abort)
1621 struct mfi_command *cm;
1622 struct mfi_abort_frame *abort;
1625 mtx_assert(&sc->mfi_io_lock, MA_OWNED);
1627 if ((cm = mfi_dequeue_free(sc)) == NULL) {
1631 abort = &cm->cm_frame->abort;
1632 abort->header.cmd = MFI_CMD_ABORT;
1633 abort->header.flags = 0;
1634 abort->abort_context = cm_abort->cm_frame->header.context;
1635 abort->abort_mfi_addr_lo = cm_abort->cm_frame_busaddr;
1636 abort->abort_mfi_addr_hi = 0;
1638 cm->cm_flags = MFI_CMD_POLLED;
1640 sc->mfi_aen_cm->cm_aen_abort = 1;
1642 mfi_release_command(cm);
1644 while (i < 5 && sc->mfi_aen_cm != NULL) {
1645 msleep(&sc->mfi_aen_cm, &sc->mfi_io_lock, 0, "mfiabort", 5 * hz);
1653 mfi_dump_blocks(struct mfi_softc *sc, int id, uint64_t lba, void *virt, int len)
1655 struct mfi_command *cm;
1656 struct mfi_io_frame *io;
1659 if ((cm = mfi_dequeue_free(sc)) == NULL)
1662 io = &cm->cm_frame->io;
1663 io->header.cmd = MFI_CMD_LD_WRITE;
1664 io->header.target_id = id;
1665 io->header.timeout = 0;
1666 io->header.flags = 0;
1667 io->header.sense_len = MFI_SENSE_LEN;
1668 io->header.data_len = (len + MFI_SECTOR_LEN - 1) / MFI_SECTOR_LEN;
1669 io->sense_addr_lo = cm->cm_sense_busaddr;
1670 io->sense_addr_hi = 0;
1671 io->lba_hi = (lba & 0xffffffff00000000) >> 32;
1672 io->lba_lo = lba & 0xffffffff;
1675 cm->cm_sg = &io->sgl;
1676 cm->cm_total_frame_size = MFI_IO_FRAME_SIZE;
1677 cm->cm_flags = MFI_CMD_POLLED | MFI_CMD_DATAOUT;
1679 error = mfi_mapcmd(sc, cm);
1680 bus_dmamap_sync(sc->mfi_buffer_dmat, cm->cm_dmamap,
1681 BUS_DMASYNC_POSTWRITE);
1682 bus_dmamap_unload(sc->mfi_buffer_dmat, cm->cm_dmamap);
1683 mfi_release_command(cm);
1689 mfi_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1691 struct mfi_softc *sc;
1696 mtx_lock(&sc->mfi_io_lock);
1697 if (sc->mfi_detaching)
1700 sc->mfi_flags |= MFI_FLAGS_OPEN;
1703 mtx_unlock(&sc->mfi_io_lock);
1709 mfi_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1711 struct mfi_softc *sc;
1712 struct mfi_aen *mfi_aen_entry, *tmp;
1716 mtx_lock(&sc->mfi_io_lock);
1717 sc->mfi_flags &= ~MFI_FLAGS_OPEN;
1719 TAILQ_FOREACH_SAFE(mfi_aen_entry, &sc->mfi_aen_pids, aen_link, tmp) {
1720 if (mfi_aen_entry->p == curproc) {
1721 TAILQ_REMOVE(&sc->mfi_aen_pids, mfi_aen_entry,
1723 free(mfi_aen_entry, M_MFIBUF);
1726 mtx_unlock(&sc->mfi_io_lock);
1731 mfi_config_lock(struct mfi_softc *sc, uint32_t opcode)
1735 case MFI_DCMD_LD_DELETE:
1736 case MFI_DCMD_CFG_ADD:
1737 case MFI_DCMD_CFG_CLEAR:
1738 sx_xlock(&sc->mfi_config_lock);
1746 mfi_config_unlock(struct mfi_softc *sc, int locked)
1750 sx_xunlock(&sc->mfi_config_lock);
1753 /* Perform pre-issue checks on commands from userland and possibly veto them. */
1755 mfi_check_command_pre(struct mfi_softc *sc, struct mfi_command *cm)
1757 struct mfi_disk *ld, *ld2;
1760 mtx_assert(&sc->mfi_io_lock, MA_OWNED);
1762 switch (cm->cm_frame->dcmd.opcode) {
1763 case MFI_DCMD_LD_DELETE:
1764 TAILQ_FOREACH(ld, &sc->mfi_ld_tqh, ld_link) {
1765 if (ld->ld_id == cm->cm_frame->dcmd.mbox[0])
1771 error = mfi_disk_disable(ld);
1773 case MFI_DCMD_CFG_CLEAR:
1774 TAILQ_FOREACH(ld, &sc->mfi_ld_tqh, ld_link) {
1775 error = mfi_disk_disable(ld);
1780 TAILQ_FOREACH(ld2, &sc->mfi_ld_tqh, ld_link) {
1783 mfi_disk_enable(ld2);
1793 /* Perform post-issue checks on commands from userland. */
1795 mfi_check_command_post(struct mfi_softc *sc, struct mfi_command *cm)
1797 struct mfi_disk *ld, *ldn;
1799 switch (cm->cm_frame->dcmd.opcode) {
1800 case MFI_DCMD_LD_DELETE:
1801 TAILQ_FOREACH(ld, &sc->mfi_ld_tqh, ld_link) {
1802 if (ld->ld_id == cm->cm_frame->dcmd.mbox[0])
1805 KASSERT(ld != NULL, ("volume dissappeared"));
1806 if (cm->cm_frame->header.cmd_status == MFI_STAT_OK) {
1807 mtx_unlock(&sc->mfi_io_lock);
1809 device_delete_child(sc->mfi_dev, ld->ld_dev);
1811 mtx_lock(&sc->mfi_io_lock);
1813 mfi_disk_enable(ld);
1815 case MFI_DCMD_CFG_CLEAR:
1816 if (cm->cm_frame->header.cmd_status == MFI_STAT_OK) {
1817 mtx_unlock(&sc->mfi_io_lock);
1819 TAILQ_FOREACH_SAFE(ld, &sc->mfi_ld_tqh, ld_link, ldn) {
1820 device_delete_child(sc->mfi_dev, ld->ld_dev);
1823 mtx_lock(&sc->mfi_io_lock);
1825 TAILQ_FOREACH(ld, &sc->mfi_ld_tqh, ld_link)
1826 mfi_disk_enable(ld);
1829 case MFI_DCMD_CFG_ADD:
1832 case MFI_DCMD_CFG_FOREIGN_IMPORT:
1839 mfi_user_command(struct mfi_softc *sc, struct mfi_ioc_passthru *ioc)
1841 struct mfi_command *cm;
1842 struct mfi_dcmd_frame *dcmd;
1843 void *ioc_buf = NULL;
1845 int error = 0, locked;
1848 if (ioc->buf_size > 0) {
1849 ioc_buf = malloc(ioc->buf_size, M_MFIBUF, M_WAITOK);
1850 if (ioc_buf == NULL) {
1853 error = copyin(ioc->buf, ioc_buf, ioc->buf_size);
1855 device_printf(sc->mfi_dev, "failed to copyin\n");
1856 free(ioc_buf, M_MFIBUF);
1861 locked = mfi_config_lock(sc, ioc->ioc_frame.opcode);
1863 mtx_lock(&sc->mfi_io_lock);
1864 while ((cm = mfi_dequeue_free(sc)) == NULL)
1865 msleep(mfi_user_command, &sc->mfi_io_lock, 0, "mfiioc", hz);
1867 /* Save context for later */
1868 context = cm->cm_frame->header.context;
1870 dcmd = &cm->cm_frame->dcmd;
1871 bcopy(&ioc->ioc_frame, dcmd, sizeof(struct mfi_dcmd_frame));
1873 cm->cm_sg = &dcmd->sgl;
1874 cm->cm_total_frame_size = MFI_DCMD_FRAME_SIZE;
1875 cm->cm_data = ioc_buf;
1876 cm->cm_len = ioc->buf_size;
1878 /* restore context */
1879 cm->cm_frame->header.context = context;
1881 /* Cheat since we don't know if we're writing or reading */
1882 cm->cm_flags = MFI_CMD_DATAIN | MFI_CMD_DATAOUT;
1884 error = mfi_check_command_pre(sc, cm);
1888 error = mfi_wait_command(sc, cm);
1890 device_printf(sc->mfi_dev, "ioctl failed %d\n", error);
1893 bcopy(dcmd, &ioc->ioc_frame, sizeof(struct mfi_dcmd_frame));
1894 mfi_check_command_post(sc, cm);
1896 mfi_release_command(cm);
1897 mtx_unlock(&sc->mfi_io_lock);
1898 mfi_config_unlock(sc, locked);
1899 if (ioc->buf_size > 0)
1900 error = copyout(ioc_buf, ioc->buf, ioc->buf_size);
1902 free(ioc_buf, M_MFIBUF);
1907 #define PTRIN(p) ((void *)(uintptr_t)(p))
1909 #define PTRIN(p) (p)
1913 mfi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
1915 struct mfi_softc *sc;
1916 union mfi_statrequest *ms;
1917 struct mfi_ioc_packet *ioc;
1919 struct mfi_ioc_packet32 *ioc32;
1921 struct mfi_ioc_aen *aen;
1922 struct mfi_command *cm = NULL;
1924 union mfi_sense_ptr sense_ptr;
1925 uint8_t *data = NULL, *temp;
1927 struct mfi_ioc_passthru *iop = (struct mfi_ioc_passthru *)arg;
1929 struct mfi_ioc_passthru32 *iop32 = (struct mfi_ioc_passthru32 *)arg;
1930 struct mfi_ioc_passthru iop_swab;
1939 ms = (union mfi_statrequest *)arg;
1940 switch (ms->ms_item) {
1945 bcopy(&sc->mfi_qstat[ms->ms_item], &ms->ms_qstat,
1946 sizeof(struct mfi_qstat));
1953 case MFIIO_QUERY_DISK:
1955 struct mfi_query_disk *qd;
1956 struct mfi_disk *ld;
1958 qd = (struct mfi_query_disk *)arg;
1959 mtx_lock(&sc->mfi_io_lock);
1960 TAILQ_FOREACH(ld, &sc->mfi_ld_tqh, ld_link) {
1961 if (ld->ld_id == qd->array_id)
1966 mtx_unlock(&sc->mfi_io_lock);
1970 if (ld->ld_flags & MFI_DISK_FLAGS_OPEN)
1972 bzero(qd->devname, SPECNAMELEN + 1);
1973 snprintf(qd->devname, SPECNAMELEN, "mfid%d", ld->ld_unit);
1974 mtx_unlock(&sc->mfi_io_lock);
1982 devclass_t devclass;
1983 ioc = (struct mfi_ioc_packet *)arg;
1986 adapter = ioc->mfi_adapter_no;
1987 if (device_get_unit(sc->mfi_dev) == 0 && adapter != 0) {
1988 devclass = devclass_find("mfi");
1989 sc = devclass_get_softc(devclass, adapter);
1991 mtx_lock(&sc->mfi_io_lock);
1992 if ((cm = mfi_dequeue_free(sc)) == NULL) {
1993 mtx_unlock(&sc->mfi_io_lock);
1996 mtx_unlock(&sc->mfi_io_lock);
2000 * save off original context since copying from user
2001 * will clobber some data
2003 context = cm->cm_frame->header.context;
2005 bcopy(ioc->mfi_frame.raw, cm->cm_frame,
2006 2 * MFI_DCMD_FRAME_SIZE); /* this isn't quite right */
2007 cm->cm_total_frame_size = (sizeof(union mfi_sgl)
2008 * ioc->mfi_sge_count) + ioc->mfi_sgl_off;
2009 if (ioc->mfi_sge_count) {
2011 (union mfi_sgl *)&cm->cm_frame->bytes[ioc->mfi_sgl_off];
2014 if (cm->cm_frame->header.flags & MFI_FRAME_DATAIN)
2015 cm->cm_flags |= MFI_CMD_DATAIN;
2016 if (cm->cm_frame->header.flags & MFI_FRAME_DATAOUT)
2017 cm->cm_flags |= MFI_CMD_DATAOUT;
2018 /* Legacy app shim */
2019 if (cm->cm_flags == 0)
2020 cm->cm_flags |= MFI_CMD_DATAIN | MFI_CMD_DATAOUT;
2021 cm->cm_len = cm->cm_frame->header.data_len;
2023 (cm->cm_flags & (MFI_CMD_DATAIN | MFI_CMD_DATAOUT))) {
2024 cm->cm_data = data = malloc(cm->cm_len, M_MFIBUF,
2026 if (cm->cm_data == NULL) {
2027 device_printf(sc->mfi_dev, "Malloc failed\n");
2034 /* restore header context */
2035 cm->cm_frame->header.context = context;
2038 if (cm->cm_flags & MFI_CMD_DATAOUT) {
2039 for (i = 0; i < ioc->mfi_sge_count; i++) {
2041 if (cmd == MFI_CMD) {
2043 error = copyin(ioc->mfi_sgl[i].iov_base,
2045 ioc->mfi_sgl[i].iov_len);
2049 ioc32 = (struct mfi_ioc_packet32 *)ioc;
2051 PTRIN(ioc32->mfi_sgl[i].iov_base);
2052 error = copyin(temp_convert,
2054 ioc32->mfi_sgl[i].iov_len);
2057 error = copyin(ioc->mfi_sgl[i].iov_base,
2059 ioc->mfi_sgl[i].iov_len);
2062 device_printf(sc->mfi_dev,
2063 "Copy in failed\n");
2066 temp = &temp[ioc->mfi_sgl[i].iov_len];
2070 if (cm->cm_frame->header.cmd == MFI_CMD_DCMD)
2071 locked = mfi_config_lock(sc, cm->cm_frame->dcmd.opcode);
2073 if (cm->cm_frame->header.cmd == MFI_CMD_PD_SCSI_IO) {
2074 cm->cm_frame->pass.sense_addr_lo = cm->cm_sense_busaddr;
2075 cm->cm_frame->pass.sense_addr_hi = 0;
2078 mtx_lock(&sc->mfi_io_lock);
2079 error = mfi_check_command_pre(sc, cm);
2081 mtx_unlock(&sc->mfi_io_lock);
2085 if ((error = mfi_wait_command(sc, cm)) != 0) {
2086 device_printf(sc->mfi_dev,
2087 "Controller polled failed\n");
2088 mtx_unlock(&sc->mfi_io_lock);
2092 mfi_check_command_post(sc, cm);
2093 mtx_unlock(&sc->mfi_io_lock);
2096 if (cm->cm_flags & MFI_CMD_DATAIN) {
2097 for (i = 0; i < ioc->mfi_sge_count; i++) {
2099 if (cmd == MFI_CMD) {
2101 error = copyout(temp,
2102 ioc->mfi_sgl[i].iov_base,
2103 ioc->mfi_sgl[i].iov_len);
2107 ioc32 = (struct mfi_ioc_packet32 *)ioc;
2109 PTRIN(ioc32->mfi_sgl[i].iov_base);
2110 error = copyout(temp,
2112 ioc32->mfi_sgl[i].iov_len);
2115 error = copyout(temp,
2116 ioc->mfi_sgl[i].iov_base,
2117 ioc->mfi_sgl[i].iov_len);
2120 device_printf(sc->mfi_dev,
2121 "Copy out failed\n");
2124 temp = &temp[ioc->mfi_sgl[i].iov_len];
2128 if (ioc->mfi_sense_len) {
2129 /* get user-space sense ptr then copy out sense */
2130 bcopy(&((struct mfi_ioc_packet*)arg)
2131 ->mfi_frame.raw[ioc->mfi_sense_off],
2132 &sense_ptr.sense_ptr_data[0],
2133 sizeof(sense_ptr.sense_ptr_data));
2135 if (cmd != MFI_CMD) {
2137 * not 64bit native so zero out any address
2139 sense_ptr.addr.high = 0;
2142 error = copyout(cm->cm_sense, sense_ptr.user_space,
2143 ioc->mfi_sense_len);
2145 device_printf(sc->mfi_dev,
2146 "Copy out failed\n");
2151 ioc->mfi_frame.hdr.cmd_status = cm->cm_frame->header.cmd_status;
2153 mfi_config_unlock(sc, locked);
2155 free(data, M_MFIBUF);
2157 mtx_lock(&sc->mfi_io_lock);
2158 mfi_release_command(cm);
2159 mtx_unlock(&sc->mfi_io_lock);
2165 aen = (struct mfi_ioc_aen *)arg;
2166 error = mfi_aen_register(sc, aen->aen_seq_num,
2167 aen->aen_class_locale);
2170 case MFI_LINUX_CMD_2: /* Firmware Linux ioctl shim */
2172 devclass_t devclass;
2173 struct mfi_linux_ioc_packet l_ioc;
2176 devclass = devclass_find("mfi");
2177 if (devclass == NULL)
2180 error = copyin(arg, &l_ioc, sizeof(l_ioc));
2183 adapter = l_ioc.lioc_adapter_no;
2184 sc = devclass_get_softc(devclass, adapter);
2187 return (mfi_linux_ioctl_int(sc->mfi_cdev,
2188 cmd, arg, flag, td));
2191 case MFI_LINUX_SET_AEN_2: /* AEN Linux ioctl shim */
2193 devclass_t devclass;
2194 struct mfi_linux_ioc_aen l_aen;
2197 devclass = devclass_find("mfi");
2198 if (devclass == NULL)
2201 error = copyin(arg, &l_aen, sizeof(l_aen));
2204 adapter = l_aen.laen_adapter_no;
2205 sc = devclass_get_softc(devclass, adapter);
2208 return (mfi_linux_ioctl_int(sc->mfi_cdev,
2209 cmd, arg, flag, td));
2213 case MFIIO_PASSTHRU32:
2214 iop_swab.ioc_frame = iop32->ioc_frame;
2215 iop_swab.buf_size = iop32->buf_size;
2216 iop_swab.buf = PTRIN(iop32->buf);
2220 case MFIIO_PASSTHRU:
2221 error = mfi_user_command(sc, iop);
2223 if (cmd == MFIIO_PASSTHRU32)
2224 iop32->ioc_frame = iop_swab.ioc_frame;
2228 device_printf(sc->mfi_dev, "IOCTL 0x%lx not handled\n", cmd);
2237 mfi_linux_ioctl_int(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
2239 struct mfi_softc *sc;
2240 struct mfi_linux_ioc_packet l_ioc;
2241 struct mfi_linux_ioc_aen l_aen;
2242 struct mfi_command *cm = NULL;
2243 struct mfi_aen *mfi_aen_entry;
2244 union mfi_sense_ptr sense_ptr;
2246 uint8_t *data = NULL, *temp;
2253 case MFI_LINUX_CMD_2: /* Firmware Linux ioctl shim */
2254 error = copyin(arg, &l_ioc, sizeof(l_ioc));
2258 if (l_ioc.lioc_sge_count > MAX_LINUX_IOCTL_SGE) {
2262 mtx_lock(&sc->mfi_io_lock);
2263 if ((cm = mfi_dequeue_free(sc)) == NULL) {
2264 mtx_unlock(&sc->mfi_io_lock);
2267 mtx_unlock(&sc->mfi_io_lock);
2271 * save off original context since copying from user
2272 * will clobber some data
2274 context = cm->cm_frame->header.context;
2276 bcopy(l_ioc.lioc_frame.raw, cm->cm_frame,
2277 2 * MFI_DCMD_FRAME_SIZE); /* this isn't quite right */
2278 cm->cm_total_frame_size = (sizeof(union mfi_sgl)
2279 * l_ioc.lioc_sge_count) + l_ioc.lioc_sgl_off;
2280 if (l_ioc.lioc_sge_count)
2282 (union mfi_sgl *)&cm->cm_frame->bytes[l_ioc.lioc_sgl_off];
2284 if (cm->cm_frame->header.flags & MFI_FRAME_DATAIN)
2285 cm->cm_flags |= MFI_CMD_DATAIN;
2286 if (cm->cm_frame->header.flags & MFI_FRAME_DATAOUT)
2287 cm->cm_flags |= MFI_CMD_DATAOUT;
2288 cm->cm_len = cm->cm_frame->header.data_len;
2290 (cm->cm_flags & (MFI_CMD_DATAIN | MFI_CMD_DATAOUT))) {
2291 cm->cm_data = data = malloc(cm->cm_len, M_MFIBUF,
2293 if (cm->cm_data == NULL) {
2294 device_printf(sc->mfi_dev, "Malloc failed\n");
2301 /* restore header context */
2302 cm->cm_frame->header.context = context;
2305 if (cm->cm_flags & MFI_CMD_DATAOUT) {
2306 for (i = 0; i < l_ioc.lioc_sge_count; i++) {
2307 error = copyin(PTRIN(l_ioc.lioc_sgl[i].iov_base),
2309 l_ioc.lioc_sgl[i].iov_len);
2311 device_printf(sc->mfi_dev,
2312 "Copy in failed\n");
2315 temp = &temp[l_ioc.lioc_sgl[i].iov_len];
2319 if (cm->cm_frame->header.cmd == MFI_CMD_DCMD)
2320 locked = mfi_config_lock(sc, cm->cm_frame->dcmd.opcode);
2322 if (cm->cm_frame->header.cmd == MFI_CMD_PD_SCSI_IO) {
2323 cm->cm_frame->pass.sense_addr_lo = cm->cm_sense_busaddr;
2324 cm->cm_frame->pass.sense_addr_hi = 0;
2327 mtx_lock(&sc->mfi_io_lock);
2328 error = mfi_check_command_pre(sc, cm);
2330 mtx_unlock(&sc->mfi_io_lock);
2334 if ((error = mfi_wait_command(sc, cm)) != 0) {
2335 device_printf(sc->mfi_dev,
2336 "Controller polled failed\n");
2337 mtx_unlock(&sc->mfi_io_lock);
2341 mfi_check_command_post(sc, cm);
2342 mtx_unlock(&sc->mfi_io_lock);
2345 if (cm->cm_flags & MFI_CMD_DATAIN) {
2346 for (i = 0; i < l_ioc.lioc_sge_count; i++) {
2347 error = copyout(temp,
2348 PTRIN(l_ioc.lioc_sgl[i].iov_base),
2349 l_ioc.lioc_sgl[i].iov_len);
2351 device_printf(sc->mfi_dev,
2352 "Copy out failed\n");
2355 temp = &temp[l_ioc.lioc_sgl[i].iov_len];
2359 if (l_ioc.lioc_sense_len) {
2360 /* get user-space sense ptr then copy out sense */
2361 bcopy(&((struct mfi_linux_ioc_packet*)arg)
2362 ->lioc_frame.raw[l_ioc.lioc_sense_off],
2363 &sense_ptr.sense_ptr_data[0],
2364 sizeof(sense_ptr.sense_ptr_data));
2367 * only 32bit Linux support so zero out any
2368 * address over 32bit
2370 sense_ptr.addr.high = 0;
2372 error = copyout(cm->cm_sense, sense_ptr.user_space,
2373 l_ioc.lioc_sense_len);
2375 device_printf(sc->mfi_dev,
2376 "Copy out failed\n");
2381 error = copyout(&cm->cm_frame->header.cmd_status,
2382 &((struct mfi_linux_ioc_packet*)arg)
2383 ->lioc_frame.hdr.cmd_status,
2386 device_printf(sc->mfi_dev,
2387 "Copy out failed\n");
2392 mfi_config_unlock(sc, locked);
2394 free(data, M_MFIBUF);
2396 mtx_lock(&sc->mfi_io_lock);
2397 mfi_release_command(cm);
2398 mtx_unlock(&sc->mfi_io_lock);
2402 case MFI_LINUX_SET_AEN_2: /* AEN Linux ioctl shim */
2403 error = copyin(arg, &l_aen, sizeof(l_aen));
2406 printf("AEN IMPLEMENTED for pid %d\n", curproc->p_pid);
2407 mfi_aen_entry = malloc(sizeof(struct mfi_aen), M_MFIBUF,
2409 mtx_lock(&sc->mfi_io_lock);
2410 if (mfi_aen_entry != NULL) {
2411 mfi_aen_entry->p = curproc;
2412 TAILQ_INSERT_TAIL(&sc->mfi_aen_pids, mfi_aen_entry,
2415 error = mfi_aen_register(sc, l_aen.laen_seq_num,
2416 l_aen.laen_class_locale);
2419 TAILQ_REMOVE(&sc->mfi_aen_pids, mfi_aen_entry,
2421 free(mfi_aen_entry, M_MFIBUF);
2423 mtx_unlock(&sc->mfi_io_lock);
2427 device_printf(sc->mfi_dev, "IOCTL 0x%lx not handled\n", cmd);
2436 mfi_poll(struct cdev *dev, int poll_events, struct thread *td)
2438 struct mfi_softc *sc;
2443 if (poll_events & (POLLIN | POLLRDNORM)) {
2444 if (sc->mfi_aen_triggered != 0) {
2445 revents |= poll_events & (POLLIN | POLLRDNORM);
2446 sc->mfi_aen_triggered = 0;
2448 if (sc->mfi_aen_triggered == 0 && sc->mfi_aen_cm == NULL) {
2454 if (poll_events & (POLLIN | POLLRDNORM)) {
2455 sc->mfi_poll_waiting = 1;
2456 selrecord(td, &sc->mfi_select);
2467 struct mfi_softc *sc;
2468 struct mfi_command *cm;
2474 dc = devclass_find("mfi");
2476 printf("No mfi dev class\n");
2480 for (i = 0; ; i++) {
2481 sc = devclass_get_softc(dc, i);
2484 device_printf(sc->mfi_dev, "Dumping\n\n");
2486 deadline = time_uptime - MFI_CMD_TIMEOUT;
2487 mtx_lock(&sc->mfi_io_lock);
2488 TAILQ_FOREACH(cm, &sc->mfi_busy, cm_link) {
2489 if (cm->cm_timestamp < deadline) {
2490 device_printf(sc->mfi_dev,
2491 "COMMAND %p TIMEOUT AFTER %d SECONDS\n", cm,
2492 (int)(time_uptime - cm->cm_timestamp));
2503 mtx_unlock(&sc->mfi_io_lock);
2510 mfi_timeout(void *data)
2512 struct mfi_softc *sc = (struct mfi_softc *)data;
2513 struct mfi_command *cm;
2517 deadline = time_uptime - MFI_CMD_TIMEOUT;
2518 mtx_lock(&sc->mfi_io_lock);
2519 TAILQ_FOREACH(cm, &sc->mfi_busy, cm_link) {
2520 if (sc->mfi_aen_cm == cm)
2522 if ((sc->mfi_aen_cm != cm) && (cm->cm_timestamp < deadline)) {
2523 device_printf(sc->mfi_dev,
2524 "COMMAND %p TIMEOUT AFTER %d SECONDS\n", cm,
2525 (int)(time_uptime - cm->cm_timestamp));
2527 MFI_VALIDATE_CMD(sc, cm);
2537 mtx_unlock(&sc->mfi_io_lock);
2539 callout_reset(&sc->mfi_watchdog_callout, MFI_CMD_TIMEOUT * hz,