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
52 * $FreeBSD: src/sys/dev/mfi/mfi.c,v 1.54 2009/12/07 21:24:07 jkim Exp $
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/sysctl.h>
60 #include <sys/malloc.h>
61 #include <sys/kernel.h>
63 #include <sys/eventhandler.h>
65 #include <sys/bus_dma.h>
67 #include <sys/ioccom.h>
70 #include <sys/signalvar.h>
71 #include <sys/device.h>
72 #include <sys/mplock2.h>
74 #include <dev/raid/mfi/mfireg.h>
75 #include <dev/raid/mfi/mfi_ioctl.h>
76 #include <dev/raid/mfi/mfivar.h>
78 static int mfi_alloc_commands(struct mfi_softc *);
79 static int mfi_comms_init(struct mfi_softc *);
80 static int mfi_wait_command(struct mfi_softc *, struct mfi_command *);
81 static int mfi_get_controller_info(struct mfi_softc *);
82 static int mfi_get_log_state(struct mfi_softc *,
83 struct mfi_evt_log_state **);
84 static int mfi_parse_entries(struct mfi_softc *, int, int);
85 static int mfi_dcmd_command(struct mfi_softc *, struct mfi_command **,
86 uint32_t, void **, size_t);
87 static void mfi_data_cb(void *, bus_dma_segment_t *, int, int);
88 static void mfi_startup(void *arg);
89 static void mfi_intr(void *arg);
90 static void mfi_ldprobe(struct mfi_softc *sc);
91 static int mfi_aen_register(struct mfi_softc *sc, int seq, int locale);
92 static void mfi_aen_complete(struct mfi_command *);
93 static int mfi_aen_setup(struct mfi_softc *, uint32_t);
94 static int mfi_add_ld(struct mfi_softc *sc, int);
95 static void mfi_add_ld_complete(struct mfi_command *);
96 static struct mfi_command * mfi_bio_command(struct mfi_softc *);
97 static void mfi_bio_complete(struct mfi_command *);
98 static int mfi_mapcmd(struct mfi_softc *, struct mfi_command *);
99 static int mfi_send_frame(struct mfi_softc *, struct mfi_command *);
100 static void mfi_complete(struct mfi_softc *, struct mfi_command *);
101 static int mfi_abort(struct mfi_softc *, struct mfi_command *);
102 static int mfi_linux_ioctl_int(struct cdev *, u_long, caddr_t, int);
103 static void mfi_timeout(void *);
104 static int mfi_user_command(struct mfi_softc *,
105 struct mfi_ioc_passthru *);
106 static void mfi_enable_intr_xscale(struct mfi_softc *sc);
107 static void mfi_enable_intr_ppc(struct mfi_softc *sc);
108 static int32_t mfi_read_fw_status_xscale(struct mfi_softc *sc);
109 static int32_t mfi_read_fw_status_ppc(struct mfi_softc *sc);
110 static int mfi_check_clear_intr_xscale(struct mfi_softc *sc);
111 static int mfi_check_clear_intr_ppc(struct mfi_softc *sc);
112 static void mfi_issue_cmd_xscale(struct mfi_softc *sc,uint32_t bus_add,uint32_t frame_cnt);
113 static void mfi_issue_cmd_ppc(struct mfi_softc *sc,uint32_t bus_add,uint32_t frame_cnt);
114 static void mfi_filter_detach(struct knote *);
115 static int mfi_filter_read(struct knote *, long);
116 static int mfi_filter_write(struct knote *, long);
118 SYSCTL_NODE(_hw, OID_AUTO, mfi, CTLFLAG_RD, 0, "MFI driver parameters");
119 static int mfi_event_locale = MFI_EVT_LOCALE_ALL;
120 TUNABLE_INT("hw.mfi.event_locale", &mfi_event_locale);
121 SYSCTL_INT(_hw_mfi, OID_AUTO, event_locale, CTLFLAG_RW, &mfi_event_locale,
122 0, "event message locale");
124 static int mfi_event_class = MFI_EVT_CLASS_INFO;
125 TUNABLE_INT("hw.mfi.event_class", &mfi_event_class);
126 SYSCTL_INT(_hw_mfi, OID_AUTO, event_class, CTLFLAG_RW, &mfi_event_class,
127 0, "event message class");
129 static int mfi_max_cmds = 128;
130 TUNABLE_INT("hw.mfi.max_cmds", &mfi_max_cmds);
131 SYSCTL_INT(_hw_mfi, OID_AUTO, max_cmds, CTLFLAG_RD, &mfi_max_cmds,
134 /* Management interface */
135 static d_open_t mfi_open;
136 static d_close_t mfi_close;
137 static d_ioctl_t mfi_ioctl;
138 static d_kqfilter_t mfi_kqfilter;
140 static struct dev_ops mfi_ops = {
143 .d_close = mfi_close,
144 .d_ioctl = mfi_ioctl,
145 .d_kqfilter = mfi_kqfilter,
148 static struct filterops mfi_read_filterops =
149 { FILTEROP_ISFD, NULL, mfi_filter_detach, mfi_filter_read };
150 static struct filterops mfi_write_filterops =
151 { FILTEROP_ISFD, NULL, mfi_filter_detach, mfi_filter_write };
153 MALLOC_DEFINE(M_MFIBUF, "mfibuf", "Buffers for the MFI driver");
155 #define MFI_INQ_LENGTH SHORT_INQUIRY_LENGTH
158 mfi_enable_intr_xscale(struct mfi_softc *sc)
160 MFI_WRITE4(sc, MFI_OMSK, 0x01);
164 mfi_enable_intr_ppc(struct mfi_softc *sc)
166 MFI_WRITE4(sc, MFI_ODCR0, 0xFFFFFFFF);
167 if (sc->mfi_flags & MFI_FLAGS_1078) {
168 MFI_WRITE4(sc, MFI_OMSK, ~MFI_1078_EIM);
169 } else if (sc->mfi_flags & MFI_FLAGS_GEN2) {
170 MFI_WRITE4(sc, MFI_OMSK, ~MFI_GEN2_EIM);
175 mfi_read_fw_status_xscale(struct mfi_softc *sc)
177 return MFI_READ4(sc, MFI_OMSG0);
181 mfi_read_fw_status_ppc(struct mfi_softc *sc)
183 return MFI_READ4(sc, MFI_OSP0);
187 mfi_check_clear_intr_xscale(struct mfi_softc *sc)
191 status = MFI_READ4(sc, MFI_OSTS);
192 if ((status & MFI_OSTS_INTR_VALID) == 0)
195 MFI_WRITE4(sc, MFI_OSTS, status);
200 mfi_check_clear_intr_ppc(struct mfi_softc *sc)
204 status = MFI_READ4(sc, MFI_OSTS);
205 if (sc->mfi_flags & MFI_FLAGS_1078) {
206 if (!(status & MFI_1078_RM)) {
209 } else if (sc->mfi_flags & MFI_FLAGS_GEN2) {
210 if (!(status & MFI_GEN2_RM)) {
215 MFI_WRITE4(sc, MFI_ODCR0, status);
220 mfi_issue_cmd_xscale(struct mfi_softc *sc,uint32_t bus_add,uint32_t frame_cnt)
222 MFI_WRITE4(sc, MFI_IQP,(bus_add >>3)|frame_cnt);
226 mfi_issue_cmd_ppc(struct mfi_softc *sc,uint32_t bus_add,uint32_t frame_cnt)
228 MFI_WRITE4(sc, MFI_IQP, (bus_add |frame_cnt <<1)|1 );
232 mfi_transition_firmware(struct mfi_softc *sc)
234 uint32_t fw_state, cur_state;
237 fw_state = sc->mfi_read_fw_status(sc)& MFI_FWSTATE_MASK;
238 while (fw_state != MFI_FWSTATE_READY) {
240 device_printf(sc->mfi_dev, "Waiting for firmware to "
242 cur_state = fw_state;
244 case MFI_FWSTATE_FAULT:
245 device_printf(sc->mfi_dev, "Firmware fault\n");
247 case MFI_FWSTATE_WAIT_HANDSHAKE:
248 MFI_WRITE4(sc, MFI_IDB, MFI_FWINIT_CLEAR_HANDSHAKE);
251 case MFI_FWSTATE_OPERATIONAL:
252 MFI_WRITE4(sc, MFI_IDB, MFI_FWINIT_READY);
255 case MFI_FWSTATE_UNDEFINED:
256 case MFI_FWSTATE_BB_INIT:
259 case MFI_FWSTATE_FW_INIT:
260 case MFI_FWSTATE_DEVICE_SCAN:
261 case MFI_FWSTATE_FLUSH_CACHE:
265 device_printf(sc->mfi_dev,"Unknown firmware state %d\n",
269 for (i = 0; i < (max_wait * 10); i++) {
270 fw_state = sc->mfi_read_fw_status(sc) & MFI_FWSTATE_MASK;
271 if (fw_state == cur_state)
276 if (fw_state == cur_state) {
277 device_printf(sc->mfi_dev, "firmware stuck in state "
286 mfi_addr32_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
291 *addr = segs[0].ds_addr;
295 mfi_attach(struct mfi_softc *sc)
298 int error, commsz, framessz, sensesz;
299 int frames, unit, max_fw_sge;
301 device_printf(sc->mfi_dev, "Megaraid SAS driver Ver 3.00 \n");
303 lockinit(&sc->mfi_io_lock, "MFI I/O lock", 0, LK_CANRECURSE);
304 lockinit(&sc->mfi_config_lock, "MFI config", 0, LK_CANRECURSE);
305 TAILQ_INIT(&sc->mfi_ld_tqh);
306 TAILQ_INIT(&sc->mfi_aen_pids);
307 TAILQ_INIT(&sc->mfi_cam_ccbq);
314 if (sc->mfi_flags & MFI_FLAGS_1064R) {
315 sc->mfi_enable_intr = mfi_enable_intr_xscale;
316 sc->mfi_read_fw_status = mfi_read_fw_status_xscale;
317 sc->mfi_check_clear_intr = mfi_check_clear_intr_xscale;
318 sc->mfi_issue_cmd = mfi_issue_cmd_xscale;
321 sc->mfi_enable_intr = mfi_enable_intr_ppc;
322 sc->mfi_read_fw_status = mfi_read_fw_status_ppc;
323 sc->mfi_check_clear_intr = mfi_check_clear_intr_ppc;
324 sc->mfi_issue_cmd = mfi_issue_cmd_ppc;
328 /* Before we get too far, see if the firmware is working */
329 if ((error = mfi_transition_firmware(sc)) != 0) {
330 device_printf(sc->mfi_dev, "Firmware not in READY state, "
331 "error %d\n", error);
336 * Get information needed for sizing the contiguous memory for the
337 * frame pool. Size down the sgl parameter since we know that
338 * we will never need more than what's required for MAXPHYS.
339 * It would be nice if these constants were available at runtime
340 * instead of compile time.
342 status = sc->mfi_read_fw_status(sc);
343 sc->mfi_max_fw_cmds = status & MFI_FWSTATE_MAXCMD_MASK;
344 max_fw_sge = (status & MFI_FWSTATE_MAXSGL_MASK) >> 16;
345 sc->mfi_max_sge = min(max_fw_sge, ((MFI_MAXPHYS / PAGE_SIZE) + 1));
348 * Create the dma tag for data buffers. Used both for block I/O
349 * and for various internal data queries.
351 if (bus_dma_tag_create( sc->mfi_parent_dmat, /* parent */
352 1, 0, /* algnmnt, boundary */
353 BUS_SPACE_MAXADDR, /* lowaddr */
354 BUS_SPACE_MAXADDR, /* highaddr */
355 NULL, NULL, /* filter, filterarg */
356 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
357 sc->mfi_max_sge, /* nsegments */
358 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
359 BUS_DMA_ALLOCNOW, /* flags */
360 &sc->mfi_buffer_dmat)) {
361 device_printf(sc->mfi_dev, "Cannot allocate buffer DMA tag\n");
366 * Allocate DMA memory for the comms queues. Keep it under 4GB for
367 * efficiency. The mfi_hwcomms struct includes space for 1 reply queue
368 * entry, so the calculated size here will be will be 1 more than
369 * mfi_max_fw_cmds. This is apparently a requirement of the hardware.
371 commsz = (sizeof(uint32_t) * sc->mfi_max_fw_cmds) +
372 sizeof(struct mfi_hwcomms);
373 if (bus_dma_tag_create( sc->mfi_parent_dmat, /* parent */
374 1, 0, /* algnmnt, boundary */
375 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
376 BUS_SPACE_MAXADDR, /* highaddr */
377 NULL, NULL, /* filter, filterarg */
378 commsz, /* maxsize */
380 commsz, /* maxsegsize */
382 &sc->mfi_comms_dmat)) {
383 device_printf(sc->mfi_dev, "Cannot allocate comms DMA tag\n");
386 if (bus_dmamem_alloc(sc->mfi_comms_dmat, (void **)&sc->mfi_comms,
387 BUS_DMA_NOWAIT, &sc->mfi_comms_dmamap)) {
388 device_printf(sc->mfi_dev, "Cannot allocate comms memory\n");
391 bzero(sc->mfi_comms, commsz);
392 bus_dmamap_load(sc->mfi_comms_dmat, sc->mfi_comms_dmamap,
393 sc->mfi_comms, commsz, mfi_addr32_cb, &sc->mfi_comms_busaddr, 0);
396 * Allocate DMA memory for the command frames. Keep them in the
397 * lower 4GB for efficiency. Calculate the size of the commands at
398 * the same time; each command is one 64 byte frame plus a set of
399 * additional frames for holding sg lists or other data.
400 * The assumption here is that the SG list will start at the second
401 * frame and not use the unused bytes in the first frame. While this
402 * isn't technically correct, it simplifies the calculation and allows
403 * for command frames that might be larger than an mfi_io_frame.
405 if (sizeof(bus_addr_t) == 8) {
406 sc->mfi_sge_size = sizeof(struct mfi_sg64);
407 sc->mfi_flags |= MFI_FLAGS_SG64;
409 sc->mfi_sge_size = sizeof(struct mfi_sg32);
411 frames = (sc->mfi_sge_size * sc->mfi_max_sge - 1) / MFI_FRAME_SIZE + 2;
412 sc->mfi_cmd_size = frames * MFI_FRAME_SIZE;
413 framessz = sc->mfi_cmd_size * sc->mfi_max_fw_cmds;
414 if (bus_dma_tag_create( sc->mfi_parent_dmat, /* parent */
415 64, 0, /* algnmnt, boundary */
416 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
417 BUS_SPACE_MAXADDR, /* highaddr */
418 NULL, NULL, /* filter, filterarg */
419 framessz, /* maxsize */
421 framessz, /* maxsegsize */
423 &sc->mfi_frames_dmat)) {
424 device_printf(sc->mfi_dev, "Cannot allocate frame DMA tag\n");
427 if (bus_dmamem_alloc(sc->mfi_frames_dmat, (void **)&sc->mfi_frames,
428 BUS_DMA_NOWAIT, &sc->mfi_frames_dmamap)) {
429 device_printf(sc->mfi_dev, "Cannot allocate frames memory\n");
432 bzero(sc->mfi_frames, framessz);
433 bus_dmamap_load(sc->mfi_frames_dmat, sc->mfi_frames_dmamap,
434 sc->mfi_frames, framessz, mfi_addr32_cb, &sc->mfi_frames_busaddr,0);
437 * Allocate DMA memory for the frame sense data. Keep them in the
438 * lower 4GB for efficiency
440 sensesz = sc->mfi_max_fw_cmds * MFI_SENSE_LEN;
441 if (bus_dma_tag_create( sc->mfi_parent_dmat, /* parent */
442 4, 0, /* algnmnt, boundary */
443 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
444 BUS_SPACE_MAXADDR, /* highaddr */
445 NULL, NULL, /* filter, filterarg */
446 sensesz, /* maxsize */
448 sensesz, /* maxsegsize */
450 &sc->mfi_sense_dmat)) {
451 device_printf(sc->mfi_dev, "Cannot allocate sense DMA tag\n");
454 if (bus_dmamem_alloc(sc->mfi_sense_dmat, (void **)&sc->mfi_sense,
455 BUS_DMA_NOWAIT, &sc->mfi_sense_dmamap)) {
456 device_printf(sc->mfi_dev, "Cannot allocate sense memory\n");
459 bus_dmamap_load(sc->mfi_sense_dmat, sc->mfi_sense_dmamap,
460 sc->mfi_sense, sensesz, mfi_addr32_cb, &sc->mfi_sense_busaddr, 0);
462 if ((error = mfi_alloc_commands(sc)) != 0)
465 if ((error = mfi_comms_init(sc)) != 0)
468 if ((error = mfi_get_controller_info(sc)) != 0)
471 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
472 if ((error = mfi_aen_setup(sc, 0), 0) != 0) {
473 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
476 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
479 * Set up the interrupt handler. XXX This should happen in
483 if ((sc->mfi_irq = bus_alloc_resource_any(sc->mfi_dev, SYS_RES_IRQ,
484 &sc->mfi_irq_rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
485 device_printf(sc->mfi_dev, "Cannot allocate interrupt\n");
488 if (bus_setup_intr(sc->mfi_dev, sc->mfi_irq, 0,
489 mfi_intr, sc, &sc->mfi_intr, NULL)) {
490 device_printf(sc->mfi_dev, "Cannot set up interrupt\n");
494 /* Register a config hook to probe the bus for arrays */
495 sc->mfi_ich.ich_func = mfi_startup;
496 sc->mfi_ich.ich_arg = sc;
497 if (config_intrhook_establish(&sc->mfi_ich) != 0) {
498 device_printf(sc->mfi_dev, "Cannot establish configuration "
504 * Register a shutdown handler.
506 if ((sc->mfi_eh = EVENTHANDLER_REGISTER(shutdown_final, mfi_shutdown,
507 sc, SHUTDOWN_PRI_DEFAULT)) == NULL) {
508 device_printf(sc->mfi_dev, "Warning: shutdown event "
509 "registration failed\n");
513 * Create the control device for doing management
515 unit = device_get_unit(sc->mfi_dev);
516 sc->mfi_cdev = make_dev(&mfi_ops, unit, UID_ROOT, GID_OPERATOR,
517 0640, "mfi%d", unit);
519 make_dev_alias(sc->mfi_cdev, "megaraid_sas_ioctl_node");
520 if (sc->mfi_cdev != NULL)
521 sc->mfi_cdev->si_drv1 = sc;
522 sysctl_ctx_init(&sc->mfi_sysctl_ctx);
523 sc->mfi_sysctl_tree = SYSCTL_ADD_NODE(&sc->mfi_sysctl_ctx,
524 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
525 device_get_nameunit(sc->mfi_dev), CTLFLAG_RD, 0, "");
526 if (sc->mfi_sysctl_tree == NULL) {
527 device_printf(sc->mfi_dev, "can't add sysctl node\n");
530 SYSCTL_ADD_INT(&sc->mfi_sysctl_ctx,
531 SYSCTL_CHILDREN(sc->mfi_sysctl_tree),
532 OID_AUTO, "delete_busy_volumes", CTLFLAG_RW,
533 &sc->mfi_delete_busy_volumes, 0, "Allow removal of busy volumes");
534 SYSCTL_ADD_INT(&sc->mfi_sysctl_ctx,
535 SYSCTL_CHILDREN(sc->mfi_sysctl_tree),
536 OID_AUTO, "keep_deleted_volumes", CTLFLAG_RW,
537 &sc->mfi_keep_deleted_volumes, 0,
538 "Don't detach the mfid device for a busy volume that is deleted");
540 device_add_child(sc->mfi_dev, "mfip", -1);
541 bus_generic_attach(sc->mfi_dev);
543 /* Start the timeout watchdog */
544 callout_init(&sc->mfi_watchdog_callout);
545 callout_reset(&sc->mfi_watchdog_callout, MFI_CMD_TIMEOUT * hz,
552 mfi_alloc_commands(struct mfi_softc *sc)
554 struct mfi_command *cm;
558 * XXX Should we allocate all the commands up front, or allocate on
559 * demand later like 'aac' does?
561 ncmds = MIN(mfi_max_cmds, sc->mfi_max_fw_cmds);
563 device_printf(sc->mfi_dev, "Max fw cmds= %d, sizing driver "
564 "pool to %d\n", sc->mfi_max_fw_cmds, ncmds);
566 sc->mfi_commands = kmalloc(sizeof(struct mfi_command) * ncmds, M_MFIBUF,
569 for (i = 0; i < ncmds; i++) {
570 cm = &sc->mfi_commands[i];
571 cm->cm_frame = (union mfi_frame *)((uintptr_t)sc->mfi_frames +
572 sc->mfi_cmd_size * i);
573 cm->cm_frame_busaddr = sc->mfi_frames_busaddr +
574 sc->mfi_cmd_size * i;
575 cm->cm_frame->header.context = i;
576 cm->cm_sense = &sc->mfi_sense[i];
577 cm->cm_sense_busaddr= sc->mfi_sense_busaddr + MFI_SENSE_LEN * i;
580 if (bus_dmamap_create(sc->mfi_buffer_dmat, 0,
581 &cm->cm_dmamap) == 0)
582 mfi_release_command(cm);
585 sc->mfi_total_cmds++;
592 mfi_release_command(struct mfi_command *cm)
594 struct mfi_frame_header *hdr;
598 * Zero out the important fields of the frame, but make sure the
599 * context field is preserved. For efficiency, handle the fields
600 * as 32 bit words. Clear out the first S/G entry too for safety.
602 hdr = &cm->cm_frame->header;
603 if (cm->cm_data != NULL && hdr->sg_count) {
604 cm->cm_sg->sg32[0].len = 0;
605 cm->cm_sg->sg32[0].addr = 0;
608 hdr_data = (uint32_t *)cm->cm_frame;
609 hdr_data[0] = 0; /* cmd, sense_len, cmd_status, scsi_status */
610 hdr_data[1] = 0; /* target_id, lun_id, cdb_len, sg_count */
611 hdr_data[4] = 0; /* flags, timeout */
612 hdr_data[5] = 0; /* data_len */
614 cm->cm_extra_frames = 0;
616 cm->cm_complete = NULL;
617 cm->cm_private = NULL;
620 cm->cm_total_frame_size = 0;
622 mfi_enqueue_free(cm);
626 mfi_dcmd_command(struct mfi_softc *sc, struct mfi_command **cmp, uint32_t opcode,
627 void **bufp, size_t bufsize)
629 struct mfi_command *cm;
630 struct mfi_dcmd_frame *dcmd;
633 KKASSERT(lockstatus(&sc->mfi_io_lock, curthread) != 0);
635 cm = mfi_dequeue_free(sc);
639 if ((bufsize > 0) && (bufp != NULL)) {
641 buf = kmalloc(bufsize, M_MFIBUF, M_NOWAIT|M_ZERO);
643 mfi_release_command(cm);
652 dcmd = &cm->cm_frame->dcmd;
653 bzero(dcmd->mbox, MFI_MBOX_SIZE);
654 dcmd->header.cmd = MFI_CMD_DCMD;
655 dcmd->header.timeout = 0;
656 dcmd->header.flags = 0;
657 dcmd->header.data_len = bufsize;
658 dcmd->opcode = opcode;
659 cm->cm_sg = &dcmd->sgl;
660 cm->cm_total_frame_size = MFI_DCMD_FRAME_SIZE;
663 cm->cm_private = buf;
664 cm->cm_len = bufsize;
667 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
673 mfi_comms_init(struct mfi_softc *sc)
675 struct mfi_command *cm;
676 struct mfi_init_frame *init;
677 struct mfi_init_qinfo *qinfo;
680 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
681 if ((cm = mfi_dequeue_free(sc)) == NULL)
685 * Abuse the SG list area of the frame to hold the init_qinfo
688 init = &cm->cm_frame->init;
689 qinfo = (struct mfi_init_qinfo *)((uintptr_t)init + MFI_FRAME_SIZE);
691 bzero(qinfo, sizeof(struct mfi_init_qinfo));
692 qinfo->rq_entries = sc->mfi_max_fw_cmds + 1;
693 qinfo->rq_addr_lo = sc->mfi_comms_busaddr +
694 offsetof(struct mfi_hwcomms, hw_reply_q);
695 qinfo->pi_addr_lo = sc->mfi_comms_busaddr +
696 offsetof(struct mfi_hwcomms, hw_pi);
697 qinfo->ci_addr_lo = sc->mfi_comms_busaddr +
698 offsetof(struct mfi_hwcomms, hw_ci);
700 init->header.cmd = MFI_CMD_INIT;
701 init->header.data_len = sizeof(struct mfi_init_qinfo);
702 init->qinfo_new_addr_lo = cm->cm_frame_busaddr + MFI_FRAME_SIZE;
704 cm->cm_flags = MFI_CMD_POLLED;
706 if ((error = mfi_mapcmd(sc, cm)) != 0) {
707 device_printf(sc->mfi_dev, "failed to send init command\n");
708 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
711 mfi_release_command(cm);
712 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
718 mfi_get_controller_info(struct mfi_softc *sc)
720 struct mfi_command *cm = NULL;
721 struct mfi_ctrl_info *ci = NULL;
722 uint32_t max_sectors_1, max_sectors_2;
725 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
726 error = mfi_dcmd_command(sc, &cm, MFI_DCMD_CTRL_GETINFO,
727 (void **)&ci, sizeof(*ci));
730 cm->cm_flags = MFI_CMD_DATAIN | MFI_CMD_POLLED;
732 if ((error = mfi_mapcmd(sc, cm)) != 0) {
733 device_printf(sc->mfi_dev, "Failed to get controller info\n");
734 sc->mfi_max_io = (sc->mfi_max_sge - 1) * PAGE_SIZE /
740 bus_dmamap_sync(sc->mfi_buffer_dmat, cm->cm_dmamap,
741 BUS_DMASYNC_POSTREAD);
742 bus_dmamap_unload(sc->mfi_buffer_dmat, cm->cm_dmamap);
744 max_sectors_1 = (1 << ci->stripe_sz_ops.min) * ci->max_strips_per_io;
745 max_sectors_2 = ci->max_request_size;
746 sc->mfi_max_io = min(max_sectors_1, max_sectors_2);
752 mfi_release_command(cm);
753 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
758 mfi_get_log_state(struct mfi_softc *sc, struct mfi_evt_log_state **log_state)
760 struct mfi_command *cm = NULL;
763 error = mfi_dcmd_command(sc, &cm, MFI_DCMD_CTRL_EVENT_GETINFO,
764 (void **)log_state, sizeof(**log_state));
767 cm->cm_flags = MFI_CMD_DATAIN | MFI_CMD_POLLED;
769 if ((error = mfi_mapcmd(sc, cm)) != 0) {
770 device_printf(sc->mfi_dev, "Failed to get log state\n");
774 bus_dmamap_sync(sc->mfi_buffer_dmat, cm->cm_dmamap,
775 BUS_DMASYNC_POSTREAD);
776 bus_dmamap_unload(sc->mfi_buffer_dmat, cm->cm_dmamap);
780 mfi_release_command(cm);
786 mfi_aen_setup(struct mfi_softc *sc, uint32_t seq_start)
788 struct mfi_evt_log_state *log_state = NULL;
789 union mfi_evt class_locale;
793 class_locale.members.reserved = 0;
794 class_locale.members.locale = mfi_event_locale;
795 class_locale.members.class = mfi_event_class;
797 if (seq_start == 0) {
798 error = mfi_get_log_state(sc, &log_state);
801 kfree(log_state, M_MFIBUF);
806 * Walk through any events that fired since the last
809 mfi_parse_entries(sc, log_state->shutdown_seq_num,
810 log_state->newest_seq_num);
811 seq = log_state->newest_seq_num;
814 mfi_aen_register(sc, seq, class_locale.word);
815 if (log_state != NULL)
816 kfree(log_state, M_MFIBUF);
822 mfi_wait_command(struct mfi_softc *sc, struct mfi_command *cm)
825 KKASSERT(lockstatus(&sc->mfi_io_lock, curthread) != 0);
826 cm->cm_complete = NULL;
830 * MegaCli can issue a DCMD of 0. In this case do nothing
831 * and return 0 to it as status
833 if (cm->cm_frame->dcmd.opcode == 0) {
834 cm->cm_frame->header.cmd_status = MFI_STAT_OK;
836 return (cm->cm_error);
838 mfi_enqueue_ready(cm);
840 if ((cm->cm_flags & MFI_CMD_COMPLETED) == 0)
841 lksleep(cm, &sc->mfi_io_lock, 0, "mfiwait", 0);
842 return (cm->cm_error);
846 mfi_free(struct mfi_softc *sc)
848 struct mfi_command *cm;
851 #if 0 /* XXX swildner */
852 callout_drain(&sc->mfi_watchdog_callout);
855 if (sc->mfi_cdev != NULL)
856 destroy_dev(sc->mfi_cdev);
858 if (sc->mfi_total_cmds != 0) {
859 for (i = 0; i < sc->mfi_total_cmds; i++) {
860 cm = &sc->mfi_commands[i];
861 bus_dmamap_destroy(sc->mfi_buffer_dmat, cm->cm_dmamap);
863 kfree(sc->mfi_commands, M_MFIBUF);
867 bus_teardown_intr(sc->mfi_dev, sc->mfi_irq, sc->mfi_intr);
868 if (sc->mfi_irq != NULL)
869 bus_release_resource(sc->mfi_dev, SYS_RES_IRQ, sc->mfi_irq_rid,
872 if (sc->mfi_sense_busaddr != 0)
873 bus_dmamap_unload(sc->mfi_sense_dmat, sc->mfi_sense_dmamap);
874 if (sc->mfi_sense != NULL)
875 bus_dmamem_free(sc->mfi_sense_dmat, sc->mfi_sense,
876 sc->mfi_sense_dmamap);
877 if (sc->mfi_sense_dmat != NULL)
878 bus_dma_tag_destroy(sc->mfi_sense_dmat);
880 if (sc->mfi_frames_busaddr != 0)
881 bus_dmamap_unload(sc->mfi_frames_dmat, sc->mfi_frames_dmamap);
882 if (sc->mfi_frames != NULL)
883 bus_dmamem_free(sc->mfi_frames_dmat, sc->mfi_frames,
884 sc->mfi_frames_dmamap);
885 if (sc->mfi_frames_dmat != NULL)
886 bus_dma_tag_destroy(sc->mfi_frames_dmat);
888 if (sc->mfi_comms_busaddr != 0)
889 bus_dmamap_unload(sc->mfi_comms_dmat, sc->mfi_comms_dmamap);
890 if (sc->mfi_comms != NULL)
891 bus_dmamem_free(sc->mfi_comms_dmat, sc->mfi_comms,
892 sc->mfi_comms_dmamap);
893 if (sc->mfi_comms_dmat != NULL)
894 bus_dma_tag_destroy(sc->mfi_comms_dmat);
896 if (sc->mfi_buffer_dmat != NULL)
897 bus_dma_tag_destroy(sc->mfi_buffer_dmat);
898 if (sc->mfi_parent_dmat != NULL)
899 bus_dma_tag_destroy(sc->mfi_parent_dmat);
901 if (sc->mfi_sysctl_tree != NULL)
902 sysctl_ctx_free(&sc->mfi_sysctl_ctx);
904 #if 0 /* XXX swildner: not sure if we need something like mtx_initialized() */
906 if (mtx_initialized(&sc->mfi_io_lock)) {
907 lockuninit(&sc->mfi_io_lock);
908 sx_destroy(&sc->mfi_config_lock);
912 lockuninit(&sc->mfi_io_lock);
913 lockuninit(&sc->mfi_config_lock);
919 mfi_startup(void *arg)
921 struct mfi_softc *sc;
923 sc = (struct mfi_softc *)arg;
925 config_intrhook_disestablish(&sc->mfi_ich);
927 sc->mfi_enable_intr(sc);
928 lockmgr(&sc->mfi_config_lock, LK_EXCLUSIVE);
929 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
931 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
932 lockmgr(&sc->mfi_config_lock, LK_RELEASE);
938 struct mfi_softc *sc;
939 struct mfi_command *cm;
940 uint32_t pi, ci, context;
942 sc = (struct mfi_softc *)arg;
944 if (sc->mfi_check_clear_intr(sc))
947 pi = sc->mfi_comms->hw_pi;
948 ci = sc->mfi_comms->hw_ci;
949 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
951 context = sc->mfi_comms->hw_reply_q[ci];
952 if (context < sc->mfi_max_fw_cmds) {
953 cm = &sc->mfi_commands[context];
956 mfi_complete(sc, cm);
958 if (++ci == (sc->mfi_max_fw_cmds + 1)) {
963 sc->mfi_comms->hw_ci = ci;
965 /* Give defered I/O a chance to run */
966 if (sc->mfi_flags & MFI_FLAGS_QFRZN)
967 sc->mfi_flags &= ~MFI_FLAGS_QFRZN;
969 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
975 mfi_shutdown(struct mfi_softc *sc)
977 struct mfi_dcmd_frame *dcmd;
978 struct mfi_command *cm;
981 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
982 error = mfi_dcmd_command(sc, &cm, MFI_DCMD_CTRL_SHUTDOWN, NULL, 0);
984 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
988 if (sc->mfi_aen_cm != NULL)
989 mfi_abort(sc, sc->mfi_aen_cm);
991 dcmd = &cm->cm_frame->dcmd;
992 dcmd->header.flags = MFI_FRAME_DIR_NONE;
993 cm->cm_flags = MFI_CMD_POLLED;
996 if ((error = mfi_mapcmd(sc, cm)) != 0) {
997 device_printf(sc->mfi_dev, "Failed to shutdown controller\n");
1000 mfi_release_command(cm);
1001 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
1006 mfi_ldprobe(struct mfi_softc *sc)
1008 struct mfi_frame_header *hdr;
1009 struct mfi_command *cm = NULL;
1010 struct mfi_ld_list *list = NULL;
1011 struct mfi_disk *ld;
1014 KKASSERT(lockstatus(&sc->mfi_config_lock, curthread) != 0);
1015 KKASSERT(lockstatus(&sc->mfi_io_lock, curthread) != 0);
1017 error = mfi_dcmd_command(sc, &cm, MFI_DCMD_LD_GET_LIST,
1018 (void **)&list, sizeof(*list));
1022 cm->cm_flags = MFI_CMD_DATAIN;
1023 if (mfi_wait_command(sc, cm) != 0) {
1024 device_printf(sc->mfi_dev, "Failed to get device listing\n");
1028 hdr = &cm->cm_frame->header;
1029 if (hdr->cmd_status != MFI_STAT_OK) {
1030 device_printf(sc->mfi_dev, "MFI_DCMD_LD_GET_LIST failed %x\n",
1035 for (i = 0; i < list->ld_count; i++) {
1036 TAILQ_FOREACH(ld, &sc->mfi_ld_tqh, ld_link) {
1037 if (ld->ld_id == list->ld_list[i].ld.v.target_id)
1040 mfi_add_ld(sc, list->ld_list[i].ld.v.target_id);
1045 kfree(list, M_MFIBUF);
1047 mfi_release_command(cm);
1053 * The timestamp is the number of seconds since 00:00 Jan 1, 2000. If
1054 * the bits in 24-31 are all set, then it is the number of seconds since
1058 format_timestamp(uint32_t timestamp)
1060 static char buffer[32];
1062 if ((timestamp & 0xff000000) == 0xff000000)
1063 ksnprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
1066 ksnprintf(buffer, sizeof(buffer), "%us", timestamp);
1071 format_class(int8_t class)
1073 static char buffer[6];
1076 case MFI_EVT_CLASS_DEBUG:
1078 case MFI_EVT_CLASS_PROGRESS:
1079 return ("progress");
1080 case MFI_EVT_CLASS_INFO:
1082 case MFI_EVT_CLASS_WARNING:
1084 case MFI_EVT_CLASS_CRITICAL:
1086 case MFI_EVT_CLASS_FATAL:
1088 case MFI_EVT_CLASS_DEAD:
1091 ksnprintf(buffer, sizeof(buffer), "%d", class);
1097 mfi_decode_evt(struct mfi_softc *sc, struct mfi_evt_detail *detail)
1100 device_printf(sc->mfi_dev, "%d (%s/0x%04x/%s) - %s\n", detail->seq,
1101 format_timestamp(detail->time), detail->class.members.locale,
1102 format_class(detail->class.members.class), detail->description);
1106 mfi_aen_register(struct mfi_softc *sc, int seq, int locale)
1108 struct mfi_command *cm;
1109 struct mfi_dcmd_frame *dcmd;
1110 union mfi_evt current_aen, prior_aen;
1111 struct mfi_evt_detail *ed = NULL;
1114 current_aen.word = locale;
1115 if (sc->mfi_aen_cm != NULL) {
1117 ((uint32_t *)&sc->mfi_aen_cm->cm_frame->dcmd.mbox)[1];
1118 if (prior_aen.members.class <= current_aen.members.class &&
1119 !((prior_aen.members.locale & current_aen.members.locale)
1120 ^current_aen.members.locale)) {
1123 prior_aen.members.locale |= current_aen.members.locale;
1124 if (prior_aen.members.class
1125 < current_aen.members.class)
1126 current_aen.members.class =
1127 prior_aen.members.class;
1128 mfi_abort(sc, sc->mfi_aen_cm);
1132 error = mfi_dcmd_command(sc, &cm, MFI_DCMD_CTRL_EVENT_WAIT,
1133 (void **)&ed, sizeof(*ed));
1138 dcmd = &cm->cm_frame->dcmd;
1139 ((uint32_t *)&dcmd->mbox)[0] = seq;
1140 ((uint32_t *)&dcmd->mbox)[1] = locale;
1141 cm->cm_flags = MFI_CMD_DATAIN;
1142 cm->cm_complete = mfi_aen_complete;
1144 sc->mfi_aen_cm = cm;
1146 mfi_enqueue_ready(cm);
1154 mfi_aen_complete(struct mfi_command *cm)
1156 struct mfi_frame_header *hdr;
1157 struct mfi_softc *sc;
1158 struct mfi_evt_detail *detail;
1159 struct mfi_aen *mfi_aen_entry, *tmp;
1160 int seq = 0, aborted = 0;
1163 hdr = &cm->cm_frame->header;
1165 if (sc->mfi_aen_cm == NULL)
1168 if (sc->mfi_aen_cm->cm_aen_abort || hdr->cmd_status == 0xff) {
1169 sc->mfi_aen_cm->cm_aen_abort = 0;
1172 sc->mfi_aen_triggered = 1;
1173 if (sc->mfi_poll_waiting) {
1174 sc->mfi_poll_waiting = 0;
1175 KNOTE(&sc->mfi_kq.ki_note, 0);
1177 detail = cm->cm_data;
1179 * XXX If this function is too expensive or is recursive, then
1180 * events should be put onto a queue and processed later.
1182 mfi_decode_evt(sc, detail);
1183 seq = detail->seq + 1;
1184 TAILQ_FOREACH_MUTABLE(mfi_aen_entry, &sc->mfi_aen_pids, aen_link, tmp) {
1185 TAILQ_REMOVE(&sc->mfi_aen_pids, mfi_aen_entry,
1187 lwkt_gettoken(&proc_token);
1188 ksignal(mfi_aen_entry->p, SIGIO);
1189 lwkt_reltoken(&proc_token);
1190 kfree(mfi_aen_entry, M_MFIBUF);
1194 kfree(cm->cm_data, M_MFIBUF);
1195 sc->mfi_aen_cm = NULL;
1196 wakeup(&sc->mfi_aen_cm);
1197 mfi_release_command(cm);
1199 /* set it up again so the driver can catch more events */
1201 mfi_aen_setup(sc, seq);
1205 #define MAX_EVENTS 15
1208 mfi_parse_entries(struct mfi_softc *sc, int start_seq, int stop_seq)
1210 struct mfi_command *cm;
1211 struct mfi_dcmd_frame *dcmd;
1212 struct mfi_evt_list *el;
1213 union mfi_evt class_locale;
1214 int error, i, seq, size;
1216 class_locale.members.reserved = 0;
1217 class_locale.members.locale = mfi_event_locale;
1218 class_locale.members.class = mfi_event_class;
1220 size = sizeof(struct mfi_evt_list) + sizeof(struct mfi_evt_detail)
1222 el = kmalloc(size, M_MFIBUF, M_NOWAIT | M_ZERO);
1226 for (seq = start_seq;;) {
1227 if ((cm = mfi_dequeue_free(sc)) == NULL) {
1228 kfree(el, M_MFIBUF);
1232 dcmd = &cm->cm_frame->dcmd;
1233 bzero(dcmd->mbox, MFI_MBOX_SIZE);
1234 dcmd->header.cmd = MFI_CMD_DCMD;
1235 dcmd->header.timeout = 0;
1236 dcmd->header.data_len = size;
1237 dcmd->opcode = MFI_DCMD_CTRL_EVENT_GET;
1238 ((uint32_t *)&dcmd->mbox)[0] = seq;
1239 ((uint32_t *)&dcmd->mbox)[1] = class_locale.word;
1240 cm->cm_sg = &dcmd->sgl;
1241 cm->cm_total_frame_size = MFI_DCMD_FRAME_SIZE;
1242 cm->cm_flags = MFI_CMD_DATAIN | MFI_CMD_POLLED;
1246 if ((error = mfi_mapcmd(sc, cm)) != 0) {
1247 device_printf(sc->mfi_dev,
1248 "Failed to get controller entries\n");
1249 mfi_release_command(cm);
1253 bus_dmamap_sync(sc->mfi_buffer_dmat, cm->cm_dmamap,
1254 BUS_DMASYNC_POSTREAD);
1255 bus_dmamap_unload(sc->mfi_buffer_dmat, cm->cm_dmamap);
1257 if (dcmd->header.cmd_status == MFI_STAT_NOT_FOUND) {
1258 mfi_release_command(cm);
1261 if (dcmd->header.cmd_status != MFI_STAT_OK) {
1262 device_printf(sc->mfi_dev,
1263 "Error %d fetching controller entries\n",
1264 dcmd->header.cmd_status);
1265 mfi_release_command(cm);
1268 mfi_release_command(cm);
1270 for (i = 0; i < el->count; i++) {
1272 * If this event is newer than 'stop_seq' then
1273 * break out of the loop. Note that the log
1274 * is a circular buffer so we have to handle
1275 * the case that our stop point is earlier in
1276 * the buffer than our start point.
1278 if (el->event[i].seq >= stop_seq) {
1279 if (start_seq <= stop_seq)
1281 else if (el->event[i].seq < start_seq)
1284 mfi_decode_evt(sc, &el->event[i]);
1286 seq = el->event[el->count - 1].seq + 1;
1289 kfree(el, M_MFIBUF);
1294 mfi_add_ld(struct mfi_softc *sc, int id)
1296 struct mfi_command *cm;
1297 struct mfi_dcmd_frame *dcmd = NULL;
1298 struct mfi_ld_info *ld_info = NULL;
1301 KKASSERT(lockstatus(&sc->mfi_io_lock, curthread) != 0);
1303 error = mfi_dcmd_command(sc, &cm, MFI_DCMD_LD_GET_INFO,
1304 (void **)&ld_info, sizeof(*ld_info));
1306 device_printf(sc->mfi_dev,
1307 "Failed to allocate for MFI_DCMD_LD_GET_INFO %d\n", error);
1309 kfree(ld_info, M_MFIBUF);
1312 cm->cm_flags = MFI_CMD_DATAIN;
1313 dcmd = &cm->cm_frame->dcmd;
1315 if (mfi_wait_command(sc, cm) != 0) {
1316 device_printf(sc->mfi_dev,
1317 "Failed to get logical drive: %d\n", id);
1318 kfree(ld_info, M_MFIBUF);
1322 mfi_add_ld_complete(cm);
1327 mfi_add_ld_complete(struct mfi_command *cm)
1329 struct mfi_frame_header *hdr;
1330 struct mfi_ld_info *ld_info;
1331 struct mfi_softc *sc;
1335 hdr = &cm->cm_frame->header;
1336 ld_info = cm->cm_private;
1338 if (hdr->cmd_status != MFI_STAT_OK) {
1339 kfree(ld_info, M_MFIBUF);
1340 mfi_release_command(cm);
1343 mfi_release_command(cm);
1345 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
1347 if ((child = device_add_child(sc->mfi_dev, "mfid", -1)) == NULL) {
1348 device_printf(sc->mfi_dev, "Failed to add logical disk\n");
1349 kfree(ld_info, M_MFIBUF);
1351 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
1355 device_set_ivars(child, ld_info);
1356 device_set_desc(child, "MFI Logical Disk");
1357 bus_generic_attach(sc->mfi_dev);
1359 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
1362 static struct mfi_command *
1363 mfi_bio_command(struct mfi_softc *sc)
1365 struct mfi_io_frame *io;
1366 struct mfi_command *cm;
1369 struct mfi_disk *disk;
1370 int flags, blkcount;
1372 if ((cm = mfi_dequeue_free(sc)) == NULL)
1375 if ((bio = mfi_dequeue_bio(sc)) == NULL) {
1376 mfi_release_command(cm);
1381 io = &cm->cm_frame->io;
1382 switch (bp->b_cmd & 0x03) {
1384 io->header.cmd = MFI_CMD_LD_READ;
1385 flags = MFI_CMD_DATAIN;
1388 io->header.cmd = MFI_CMD_LD_WRITE;
1389 flags = MFI_CMD_DATAOUT;
1392 panic("Invalid bio command");
1395 /* Cheat with the sector length to avoid a non-constant division */
1396 blkcount = (bp->b_bcount + MFI_SECTOR_LEN - 1) / MFI_SECTOR_LEN;
1397 disk = bio->bio_driver_info;
1398 io->header.target_id = disk->ld_id;
1399 io->header.timeout = 0;
1400 io->header.flags = 0;
1401 io->header.sense_len = MFI_SENSE_LEN;
1402 io->header.data_len = blkcount;
1403 io->sense_addr_lo = cm->cm_sense_busaddr;
1404 io->sense_addr_hi = 0;
1405 io->lba_hi = ((bio->bio_offset / MFI_SECTOR_LEN) & 0xffffffff00000000) >> 32;
1406 io->lba_lo = (bio->bio_offset / MFI_SECTOR_LEN) & 0xffffffff;
1407 cm->cm_complete = mfi_bio_complete;
1408 cm->cm_private = bio;
1409 cm->cm_data = bp->b_data;
1410 cm->cm_len = bp->b_bcount;
1411 cm->cm_sg = &io->sgl;
1412 cm->cm_total_frame_size = MFI_IO_FRAME_SIZE;
1413 cm->cm_flags = flags;
1418 mfi_bio_complete(struct mfi_command *cm)
1422 struct mfi_frame_header *hdr;
1423 struct mfi_softc *sc;
1425 bio = cm->cm_private;
1427 hdr = &cm->cm_frame->header;
1430 if ((hdr->cmd_status != 0) || (hdr->scsi_status != 0)) {
1431 bp->b_flags |= B_ERROR;
1433 device_printf(sc->mfi_dev, "I/O error, status= %d "
1434 "scsi_status= %d\n", hdr->cmd_status, hdr->scsi_status);
1435 mfi_print_sense(cm->cm_sc, cm->cm_sense);
1436 } else if (cm->cm_error != 0) {
1437 bp->b_flags |= B_ERROR;
1440 mfi_release_command(cm);
1441 mfi_disk_complete(bio);
1445 mfi_startio(struct mfi_softc *sc)
1447 struct mfi_command *cm;
1448 struct ccb_hdr *ccbh;
1451 /* Don't bother if we're short on resources */
1452 if (sc->mfi_flags & MFI_FLAGS_QFRZN)
1455 /* Try a command that has already been prepared */
1456 cm = mfi_dequeue_ready(sc);
1459 if ((ccbh = TAILQ_FIRST(&sc->mfi_cam_ccbq)) != NULL)
1460 cm = sc->mfi_cam_start(ccbh);
1463 /* Nope, so look for work on the bioq */
1465 cm = mfi_bio_command(sc);
1467 /* No work available, so exit */
1471 /* Send the command to the controller */
1472 if (mfi_mapcmd(sc, cm) != 0) {
1473 mfi_requeue_ready(cm);
1480 mfi_mapcmd(struct mfi_softc *sc, struct mfi_command *cm)
1484 KKASSERT(lockstatus(&sc->mfi_io_lock, curthread) != 0);
1486 if (cm->cm_data != NULL) {
1487 polled = (cm->cm_flags & MFI_CMD_POLLED) ? BUS_DMA_NOWAIT : 0;
1488 error = bus_dmamap_load(sc->mfi_buffer_dmat, cm->cm_dmamap,
1489 cm->cm_data, cm->cm_len, mfi_data_cb, cm, polled);
1490 if (error == EINPROGRESS) {
1491 sc->mfi_flags |= MFI_FLAGS_QFRZN;
1495 error = mfi_send_frame(sc, cm);
1502 mfi_data_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1504 struct mfi_frame_header *hdr;
1505 struct mfi_command *cm;
1507 struct mfi_softc *sc;
1510 cm = (struct mfi_command *)arg;
1512 hdr = &cm->cm_frame->header;
1516 kprintf("error %d in callback\n", error);
1517 cm->cm_error = error;
1518 mfi_complete(sc, cm);
1522 if ((sc->mfi_flags & MFI_FLAGS_SG64) == 0) {
1523 for (i = 0; i < nsegs; i++) {
1524 sgl->sg32[i].addr = segs[i].ds_addr;
1525 sgl->sg32[i].len = segs[i].ds_len;
1528 for (i = 0; i < nsegs; i++) {
1529 sgl->sg64[i].addr = segs[i].ds_addr;
1530 sgl->sg64[i].len = segs[i].ds_len;
1532 hdr->flags |= MFI_FRAME_SGL64;
1534 hdr->sg_count = nsegs;
1537 if (cm->cm_flags & MFI_CMD_DATAIN) {
1538 dir |= BUS_DMASYNC_PREREAD;
1539 hdr->flags |= MFI_FRAME_DIR_READ;
1541 if (cm->cm_flags & MFI_CMD_DATAOUT) {
1542 dir |= BUS_DMASYNC_PREWRITE;
1543 hdr->flags |= MFI_FRAME_DIR_WRITE;
1545 bus_dmamap_sync(sc->mfi_buffer_dmat, cm->cm_dmamap, dir);
1546 cm->cm_flags |= MFI_CMD_MAPPED;
1549 * Instead of calculating the total number of frames in the
1550 * compound frame, it's already assumed that there will be at
1551 * least 1 frame, so don't compensate for the modulo of the
1552 * following division.
1554 cm->cm_total_frame_size += (sc->mfi_sge_size * nsegs);
1555 cm->cm_extra_frames = (cm->cm_total_frame_size - 1) / MFI_FRAME_SIZE;
1557 mfi_send_frame(sc, cm);
1563 mfi_send_frame(struct mfi_softc *sc, struct mfi_command *cm)
1565 struct mfi_frame_header *hdr;
1566 int tm = MFI_POLL_TIMEOUT_SECS * 1000;
1568 hdr = &cm->cm_frame->header;
1570 if ((cm->cm_flags & MFI_CMD_POLLED) == 0) {
1571 cm->cm_timestamp = time_second;
1572 mfi_enqueue_busy(cm);
1574 hdr->cmd_status = 0xff;
1575 hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
1579 * The bus address of the command is aligned on a 64 byte boundary,
1580 * leaving the least 6 bits as zero. For whatever reason, the
1581 * hardware wants the address shifted right by three, leaving just
1582 * 3 zero bits. These three bits are then used as a prefetching
1583 * hint for the hardware to predict how many frames need to be
1584 * fetched across the bus. If a command has more than 8 frames
1585 * then the 3 bits are set to 0x7 and the firmware uses other
1586 * information in the command to determine the total amount to fetch.
1587 * However, FreeBSD doesn't support I/O larger than 128K, so 8 frames
1588 * is enough for both 32bit and 64bit systems.
1590 if (cm->cm_extra_frames > 7)
1591 cm->cm_extra_frames = 7;
1593 sc->mfi_issue_cmd(sc,cm->cm_frame_busaddr,cm->cm_extra_frames);
1595 if ((cm->cm_flags & MFI_CMD_POLLED) == 0)
1598 /* This is a polled command, so busy-wait for it to complete. */
1599 while (hdr->cmd_status == 0xff) {
1606 if (hdr->cmd_status == 0xff) {
1607 device_printf(sc->mfi_dev, "Frame %p timed out "
1608 "command 0x%X\n", hdr, cm->cm_frame->dcmd.opcode);
1616 mfi_complete(struct mfi_softc *sc, struct mfi_command *cm)
1620 if ((cm->cm_flags & MFI_CMD_MAPPED) != 0) {
1622 if (cm->cm_flags & MFI_CMD_DATAIN)
1623 dir |= BUS_DMASYNC_POSTREAD;
1624 if (cm->cm_flags & MFI_CMD_DATAOUT)
1625 dir |= BUS_DMASYNC_POSTWRITE;
1627 bus_dmamap_sync(sc->mfi_buffer_dmat, cm->cm_dmamap, dir);
1628 bus_dmamap_unload(sc->mfi_buffer_dmat, cm->cm_dmamap);
1629 cm->cm_flags &= ~MFI_CMD_MAPPED;
1632 cm->cm_flags |= MFI_CMD_COMPLETED;
1634 if (cm->cm_complete != NULL)
1635 cm->cm_complete(cm);
1641 mfi_abort(struct mfi_softc *sc, struct mfi_command *cm_abort)
1643 struct mfi_command *cm;
1644 struct mfi_abort_frame *abort;
1647 KKASSERT(lockstatus(&sc->mfi_io_lock, curthread) != 0);
1649 if ((cm = mfi_dequeue_free(sc)) == NULL) {
1653 abort = &cm->cm_frame->abort;
1654 abort->header.cmd = MFI_CMD_ABORT;
1655 abort->header.flags = 0;
1656 abort->abort_context = cm_abort->cm_frame->header.context;
1657 abort->abort_mfi_addr_lo = cm_abort->cm_frame_busaddr;
1658 abort->abort_mfi_addr_hi = 0;
1660 cm->cm_flags = MFI_CMD_POLLED;
1662 sc->mfi_aen_cm->cm_aen_abort = 1;
1664 mfi_release_command(cm);
1666 while (i < 5 && sc->mfi_aen_cm != NULL) {
1667 lksleep(&sc->mfi_aen_cm, &sc->mfi_io_lock, 0, "mfiabort", 5 * hz);
1675 mfi_dump_blocks(struct mfi_softc *sc, int id, uint64_t lba, void *virt, int len)
1677 struct mfi_command *cm;
1678 struct mfi_io_frame *io;
1681 if ((cm = mfi_dequeue_free(sc)) == NULL)
1684 io = &cm->cm_frame->io;
1685 io->header.cmd = MFI_CMD_LD_WRITE;
1686 io->header.target_id = id;
1687 io->header.timeout = 0;
1688 io->header.flags = 0;
1689 io->header.sense_len = MFI_SENSE_LEN;
1690 io->header.data_len = (len + MFI_SECTOR_LEN - 1) / MFI_SECTOR_LEN;
1691 io->sense_addr_lo = cm->cm_sense_busaddr;
1692 io->sense_addr_hi = 0;
1693 io->lba_hi = (lba & 0xffffffff00000000) >> 32;
1694 io->lba_lo = lba & 0xffffffff;
1697 cm->cm_sg = &io->sgl;
1698 cm->cm_total_frame_size = MFI_IO_FRAME_SIZE;
1699 cm->cm_flags = MFI_CMD_POLLED | MFI_CMD_DATAOUT;
1701 error = mfi_mapcmd(sc, cm);
1702 bus_dmamap_sync(sc->mfi_buffer_dmat, cm->cm_dmamap,
1703 BUS_DMASYNC_POSTWRITE);
1704 bus_dmamap_unload(sc->mfi_buffer_dmat, cm->cm_dmamap);
1705 mfi_release_command(cm);
1711 mfi_open(struct dev_open_args *ap)
1713 cdev_t dev = ap->a_head.a_dev;
1714 struct mfi_softc *sc;
1719 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
1720 if (sc->mfi_detaching)
1723 sc->mfi_flags |= MFI_FLAGS_OPEN;
1726 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
1732 mfi_close(struct dev_close_args *ap)
1734 cdev_t dev = ap->a_head.a_dev;
1735 struct mfi_softc *sc;
1736 struct mfi_aen *mfi_aen_entry, *tmp;
1740 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
1741 sc->mfi_flags &= ~MFI_FLAGS_OPEN;
1743 TAILQ_FOREACH_MUTABLE(mfi_aen_entry, &sc->mfi_aen_pids, aen_link, tmp) {
1744 if (mfi_aen_entry->p == curproc) {
1745 TAILQ_REMOVE(&sc->mfi_aen_pids, mfi_aen_entry,
1747 kfree(mfi_aen_entry, M_MFIBUF);
1750 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
1755 mfi_config_lock(struct mfi_softc *sc, uint32_t opcode)
1759 case MFI_DCMD_LD_DELETE:
1760 case MFI_DCMD_CFG_ADD:
1761 case MFI_DCMD_CFG_CLEAR:
1762 lockmgr(&sc->mfi_config_lock, LK_EXCLUSIVE);
1770 mfi_config_unlock(struct mfi_softc *sc, int locked)
1774 lockmgr(&sc->mfi_config_lock, LK_RELEASE);
1777 /* Perform pre-issue checks on commands from userland and possibly veto them. */
1779 mfi_check_command_pre(struct mfi_softc *sc, struct mfi_command *cm)
1781 struct mfi_disk *ld, *ld2;
1784 KKASSERT(lockstatus(&sc->mfi_io_lock, curthread) != 0);
1786 switch (cm->cm_frame->dcmd.opcode) {
1787 case MFI_DCMD_LD_DELETE:
1788 TAILQ_FOREACH(ld, &sc->mfi_ld_tqh, ld_link) {
1789 if (ld->ld_id == cm->cm_frame->dcmd.mbox[0])
1795 error = mfi_disk_disable(ld);
1797 case MFI_DCMD_CFG_CLEAR:
1798 TAILQ_FOREACH(ld, &sc->mfi_ld_tqh, ld_link) {
1799 error = mfi_disk_disable(ld);
1804 TAILQ_FOREACH(ld2, &sc->mfi_ld_tqh, ld_link) {
1807 mfi_disk_enable(ld2);
1817 /* Perform post-issue checks on commands from userland. */
1819 mfi_check_command_post(struct mfi_softc *sc, struct mfi_command *cm)
1821 struct mfi_disk *ld, *ldn;
1823 switch (cm->cm_frame->dcmd.opcode) {
1824 case MFI_DCMD_LD_DELETE:
1825 TAILQ_FOREACH(ld, &sc->mfi_ld_tqh, ld_link) {
1826 if (ld->ld_id == cm->cm_frame->dcmd.mbox[0])
1829 KASSERT(ld != NULL, ("volume dissappeared"));
1830 if (cm->cm_frame->header.cmd_status == MFI_STAT_OK) {
1831 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
1833 device_delete_child(sc->mfi_dev, ld->ld_dev);
1835 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
1837 mfi_disk_enable(ld);
1839 case MFI_DCMD_CFG_CLEAR:
1840 if (cm->cm_frame->header.cmd_status == MFI_STAT_OK) {
1841 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
1843 TAILQ_FOREACH_MUTABLE(ld, &sc->mfi_ld_tqh, ld_link, ldn) {
1844 device_delete_child(sc->mfi_dev, ld->ld_dev);
1847 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
1849 TAILQ_FOREACH(ld, &sc->mfi_ld_tqh, ld_link)
1850 mfi_disk_enable(ld);
1853 case MFI_DCMD_CFG_ADD:
1856 case MFI_DCMD_CFG_FOREIGN_IMPORT:
1863 mfi_user_command(struct mfi_softc *sc, struct mfi_ioc_passthru *ioc)
1865 struct mfi_command *cm;
1866 struct mfi_dcmd_frame *dcmd;
1867 void *ioc_buf = NULL;
1869 int error = 0, locked;
1872 if (ioc->buf_size > 0) {
1873 ioc_buf = kmalloc(ioc->buf_size, M_MFIBUF, M_WAITOK);
1874 if (ioc_buf == NULL) {
1877 error = copyin(ioc->buf, ioc_buf, ioc->buf_size);
1879 device_printf(sc->mfi_dev, "failed to copyin\n");
1880 kfree(ioc_buf, M_MFIBUF);
1885 locked = mfi_config_lock(sc, ioc->ioc_frame.opcode);
1887 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
1888 while ((cm = mfi_dequeue_free(sc)) == NULL)
1889 lksleep(mfi_user_command, &sc->mfi_io_lock, 0, "mfiioc", hz);
1891 /* Save context for later */
1892 context = cm->cm_frame->header.context;
1894 dcmd = &cm->cm_frame->dcmd;
1895 bcopy(&ioc->ioc_frame, dcmd, sizeof(struct mfi_dcmd_frame));
1897 cm->cm_sg = &dcmd->sgl;
1898 cm->cm_total_frame_size = MFI_DCMD_FRAME_SIZE;
1899 cm->cm_data = ioc_buf;
1900 cm->cm_len = ioc->buf_size;
1902 /* restore context */
1903 cm->cm_frame->header.context = context;
1905 /* Cheat since we don't know if we're writing or reading */
1906 cm->cm_flags = MFI_CMD_DATAIN | MFI_CMD_DATAOUT;
1908 error = mfi_check_command_pre(sc, cm);
1912 error = mfi_wait_command(sc, cm);
1914 device_printf(sc->mfi_dev, "ioctl failed %d\n", error);
1917 bcopy(dcmd, &ioc->ioc_frame, sizeof(struct mfi_dcmd_frame));
1918 mfi_check_command_post(sc, cm);
1920 mfi_release_command(cm);
1921 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
1922 mfi_config_unlock(sc, locked);
1923 if (ioc->buf_size > 0)
1924 error = copyout(ioc_buf, ioc->buf, ioc->buf_size);
1926 kfree(ioc_buf, M_MFIBUF);
1931 #define PTRIN(p) ((void *)(uintptr_t)(p))
1933 #define PTRIN(p) (p)
1937 mfi_ioctl(struct dev_ioctl_args *ap)
1939 cdev_t dev = ap->a_head.a_dev;
1940 u_long cmd = ap->a_cmd;
1941 int flag = ap->a_fflag;
1942 caddr_t arg = ap->a_data;
1943 struct mfi_softc *sc;
1944 union mfi_statrequest *ms;
1945 struct mfi_ioc_packet *ioc;
1947 struct mfi_ioc_packet32 *ioc32;
1949 struct mfi_ioc_aen *aen;
1950 struct mfi_command *cm = NULL;
1952 union mfi_sense_ptr sense_ptr;
1953 uint8_t *data = NULL, *temp;
1955 struct mfi_ioc_passthru *iop = (struct mfi_ioc_passthru *)arg;
1957 struct mfi_ioc_passthru32 *iop32 = (struct mfi_ioc_passthru32 *)arg;
1958 struct mfi_ioc_passthru iop_swab;
1967 ms = (union mfi_statrequest *)arg;
1968 switch (ms->ms_item) {
1973 bcopy(&sc->mfi_qstat[ms->ms_item], &ms->ms_qstat,
1974 sizeof(struct mfi_qstat));
1981 case MFIIO_QUERY_DISK:
1983 struct mfi_query_disk *qd;
1984 struct mfi_disk *ld;
1986 qd = (struct mfi_query_disk *)arg;
1987 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
1988 TAILQ_FOREACH(ld, &sc->mfi_ld_tqh, ld_link) {
1989 if (ld->ld_id == qd->array_id)
1994 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
1998 if (ld->ld_flags & MFI_DISK_FLAGS_OPEN)
2000 bzero(qd->devname, SPECNAMELEN + 1);
2001 ksnprintf(qd->devname, SPECNAMELEN, "mfid%d", ld->ld_unit);
2002 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2010 devclass_t devclass;
2011 ioc = (struct mfi_ioc_packet *)arg;
2014 adapter = ioc->mfi_adapter_no;
2015 if (device_get_unit(sc->mfi_dev) == 0 && adapter != 0) {
2016 devclass = devclass_find("mfi");
2017 sc = devclass_get_softc(devclass, adapter);
2019 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
2020 if ((cm = mfi_dequeue_free(sc)) == NULL) {
2021 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2024 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2028 * save off original context since copying from user
2029 * will clobber some data
2031 context = cm->cm_frame->header.context;
2033 bcopy(ioc->mfi_frame.raw, cm->cm_frame,
2034 2 * MFI_DCMD_FRAME_SIZE); /* this isn't quite right */
2035 cm->cm_total_frame_size = (sizeof(union mfi_sgl)
2036 * ioc->mfi_sge_count) + ioc->mfi_sgl_off;
2037 if (ioc->mfi_sge_count) {
2039 (union mfi_sgl *)&cm->cm_frame->bytes[ioc->mfi_sgl_off];
2042 if (cm->cm_frame->header.flags & MFI_FRAME_DATAIN)
2043 cm->cm_flags |= MFI_CMD_DATAIN;
2044 if (cm->cm_frame->header.flags & MFI_FRAME_DATAOUT)
2045 cm->cm_flags |= MFI_CMD_DATAOUT;
2046 /* Legacy app shim */
2047 if (cm->cm_flags == 0)
2048 cm->cm_flags |= MFI_CMD_DATAIN | MFI_CMD_DATAOUT;
2049 cm->cm_len = cm->cm_frame->header.data_len;
2051 (cm->cm_flags & (MFI_CMD_DATAIN | MFI_CMD_DATAOUT))) {
2052 cm->cm_data = data = kmalloc(cm->cm_len, M_MFIBUF,
2054 if (cm->cm_data == NULL) {
2055 device_printf(sc->mfi_dev, "Malloc failed\n");
2062 /* restore header context */
2063 cm->cm_frame->header.context = context;
2066 if (cm->cm_flags & MFI_CMD_DATAOUT) {
2067 for (i = 0; i < ioc->mfi_sge_count; i++) {
2069 if (cmd == MFI_CMD) {
2071 error = copyin(ioc->mfi_sgl[i].iov_base,
2073 ioc->mfi_sgl[i].iov_len);
2077 ioc32 = (struct mfi_ioc_packet32 *)ioc;
2079 PTRIN(ioc32->mfi_sgl[i].iov_base);
2080 error = copyin(temp_convert,
2082 ioc32->mfi_sgl[i].iov_len);
2085 error = copyin(ioc->mfi_sgl[i].iov_base,
2087 ioc->mfi_sgl[i].iov_len);
2090 device_printf(sc->mfi_dev,
2091 "Copy in failed\n");
2094 temp = &temp[ioc->mfi_sgl[i].iov_len];
2098 if (cm->cm_frame->header.cmd == MFI_CMD_DCMD)
2099 locked = mfi_config_lock(sc, cm->cm_frame->dcmd.opcode);
2101 if (cm->cm_frame->header.cmd == MFI_CMD_PD_SCSI_IO) {
2102 cm->cm_frame->pass.sense_addr_lo = cm->cm_sense_busaddr;
2103 cm->cm_frame->pass.sense_addr_hi = 0;
2106 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
2107 error = mfi_check_command_pre(sc, cm);
2109 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2113 if ((error = mfi_wait_command(sc, cm)) != 0) {
2114 device_printf(sc->mfi_dev,
2115 "Controller polled failed\n");
2116 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2120 mfi_check_command_post(sc, cm);
2121 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2124 if (cm->cm_flags & MFI_CMD_DATAIN) {
2125 for (i = 0; i < ioc->mfi_sge_count; i++) {
2127 if (cmd == MFI_CMD) {
2129 error = copyout(temp,
2130 ioc->mfi_sgl[i].iov_base,
2131 ioc->mfi_sgl[i].iov_len);
2135 ioc32 = (struct mfi_ioc_packet32 *)ioc;
2137 PTRIN(ioc32->mfi_sgl[i].iov_base);
2138 error = copyout(temp,
2140 ioc32->mfi_sgl[i].iov_len);
2143 error = copyout(temp,
2144 ioc->mfi_sgl[i].iov_base,
2145 ioc->mfi_sgl[i].iov_len);
2148 device_printf(sc->mfi_dev,
2149 "Copy out failed\n");
2152 temp = &temp[ioc->mfi_sgl[i].iov_len];
2156 if (ioc->mfi_sense_len) {
2157 /* get user-space sense ptr then copy out sense */
2158 bcopy(&((struct mfi_ioc_packet*)arg)
2159 ->mfi_frame.raw[ioc->mfi_sense_off],
2160 &sense_ptr.sense_ptr_data[0],
2161 sizeof(sense_ptr.sense_ptr_data));
2163 if (cmd != MFI_CMD) {
2165 * not 64bit native so zero out any address
2167 sense_ptr.addr.high = 0;
2170 error = copyout(cm->cm_sense, sense_ptr.user_space,
2171 ioc->mfi_sense_len);
2173 device_printf(sc->mfi_dev,
2174 "Copy out failed\n");
2179 ioc->mfi_frame.hdr.cmd_status = cm->cm_frame->header.cmd_status;
2181 mfi_config_unlock(sc, locked);
2183 kfree(data, M_MFIBUF);
2185 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
2186 mfi_release_command(cm);
2187 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2193 aen = (struct mfi_ioc_aen *)arg;
2194 error = mfi_aen_register(sc, aen->aen_seq_num,
2195 aen->aen_class_locale);
2198 case MFI_LINUX_CMD_2: /* Firmware Linux ioctl shim */
2200 devclass_t devclass;
2201 struct mfi_linux_ioc_packet l_ioc;
2204 devclass = devclass_find("mfi");
2205 if (devclass == NULL)
2208 error = copyin(arg, &l_ioc, sizeof(l_ioc));
2211 adapter = l_ioc.lioc_adapter_no;
2212 sc = devclass_get_softc(devclass, adapter);
2215 return (mfi_linux_ioctl_int(sc->mfi_cdev,
2219 case MFI_LINUX_SET_AEN_2: /* AEN Linux ioctl shim */
2221 devclass_t devclass;
2222 struct mfi_linux_ioc_aen l_aen;
2225 devclass = devclass_find("mfi");
2226 if (devclass == NULL)
2229 error = copyin(arg, &l_aen, sizeof(l_aen));
2232 adapter = l_aen.laen_adapter_no;
2233 sc = devclass_get_softc(devclass, adapter);
2236 return (mfi_linux_ioctl_int(sc->mfi_cdev,
2241 case MFIIO_PASSTHRU32:
2242 iop_swab.ioc_frame = iop32->ioc_frame;
2243 iop_swab.buf_size = iop32->buf_size;
2244 iop_swab.buf = PTRIN(iop32->buf);
2248 case MFIIO_PASSTHRU:
2249 error = mfi_user_command(sc, iop);
2251 if (cmd == MFIIO_PASSTHRU32)
2252 iop32->ioc_frame = iop_swab.ioc_frame;
2256 device_printf(sc->mfi_dev, "IOCTL 0x%lx not handled\n", cmd);
2265 mfi_linux_ioctl_int(struct cdev *dev, u_long cmd, caddr_t arg, int flag)
2267 struct mfi_softc *sc;
2268 struct mfi_linux_ioc_packet l_ioc;
2269 struct mfi_linux_ioc_aen l_aen;
2270 struct mfi_command *cm = NULL;
2271 struct mfi_aen *mfi_aen_entry;
2272 union mfi_sense_ptr sense_ptr;
2274 uint8_t *data = NULL, *temp;
2281 case MFI_LINUX_CMD_2: /* Firmware Linux ioctl shim */
2282 error = copyin(arg, &l_ioc, sizeof(l_ioc));
2286 if (l_ioc.lioc_sge_count > MAX_LINUX_IOCTL_SGE) {
2290 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
2291 if ((cm = mfi_dequeue_free(sc)) == NULL) {
2292 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2295 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2299 * save off original context since copying from user
2300 * will clobber some data
2302 context = cm->cm_frame->header.context;
2304 bcopy(l_ioc.lioc_frame.raw, cm->cm_frame,
2305 2 * MFI_DCMD_FRAME_SIZE); /* this isn't quite right */
2306 cm->cm_total_frame_size = (sizeof(union mfi_sgl)
2307 * l_ioc.lioc_sge_count) + l_ioc.lioc_sgl_off;
2308 if (l_ioc.lioc_sge_count)
2310 (union mfi_sgl *)&cm->cm_frame->bytes[l_ioc.lioc_sgl_off];
2312 if (cm->cm_frame->header.flags & MFI_FRAME_DATAIN)
2313 cm->cm_flags |= MFI_CMD_DATAIN;
2314 if (cm->cm_frame->header.flags & MFI_FRAME_DATAOUT)
2315 cm->cm_flags |= MFI_CMD_DATAOUT;
2316 cm->cm_len = cm->cm_frame->header.data_len;
2318 (cm->cm_flags & (MFI_CMD_DATAIN | MFI_CMD_DATAOUT))) {
2319 cm->cm_data = data = kmalloc(cm->cm_len, M_MFIBUF,
2321 if (cm->cm_data == NULL) {
2322 device_printf(sc->mfi_dev, "Malloc failed\n");
2329 /* restore header context */
2330 cm->cm_frame->header.context = context;
2333 if (cm->cm_flags & MFI_CMD_DATAOUT) {
2334 for (i = 0; i < l_ioc.lioc_sge_count; i++) {
2335 error = copyin(PTRIN(l_ioc.lioc_sgl[i].iov_base),
2337 l_ioc.lioc_sgl[i].iov_len);
2339 device_printf(sc->mfi_dev,
2340 "Copy in failed\n");
2343 temp = &temp[l_ioc.lioc_sgl[i].iov_len];
2347 if (cm->cm_frame->header.cmd == MFI_CMD_DCMD)
2348 locked = mfi_config_lock(sc, cm->cm_frame->dcmd.opcode);
2350 if (cm->cm_frame->header.cmd == MFI_CMD_PD_SCSI_IO) {
2351 cm->cm_frame->pass.sense_addr_lo = cm->cm_sense_busaddr;
2352 cm->cm_frame->pass.sense_addr_hi = 0;
2355 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
2356 error = mfi_check_command_pre(sc, cm);
2358 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2362 if ((error = mfi_wait_command(sc, cm)) != 0) {
2363 device_printf(sc->mfi_dev,
2364 "Controller polled failed\n");
2365 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2369 mfi_check_command_post(sc, cm);
2370 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2373 if (cm->cm_flags & MFI_CMD_DATAIN) {
2374 for (i = 0; i < l_ioc.lioc_sge_count; i++) {
2375 error = copyout(temp,
2376 PTRIN(l_ioc.lioc_sgl[i].iov_base),
2377 l_ioc.lioc_sgl[i].iov_len);
2379 device_printf(sc->mfi_dev,
2380 "Copy out failed\n");
2383 temp = &temp[l_ioc.lioc_sgl[i].iov_len];
2387 if (l_ioc.lioc_sense_len) {
2388 /* get user-space sense ptr then copy out sense */
2389 bcopy(&((struct mfi_linux_ioc_packet*)arg)
2390 ->lioc_frame.raw[l_ioc.lioc_sense_off],
2391 &sense_ptr.sense_ptr_data[0],
2392 sizeof(sense_ptr.sense_ptr_data));
2395 * only 32bit Linux support so zero out any
2396 * address over 32bit
2398 sense_ptr.addr.high = 0;
2400 error = copyout(cm->cm_sense, sense_ptr.user_space,
2401 l_ioc.lioc_sense_len);
2403 device_printf(sc->mfi_dev,
2404 "Copy out failed\n");
2409 error = copyout(&cm->cm_frame->header.cmd_status,
2410 &((struct mfi_linux_ioc_packet*)arg)
2411 ->lioc_frame.hdr.cmd_status,
2414 device_printf(sc->mfi_dev,
2415 "Copy out failed\n");
2420 mfi_config_unlock(sc, locked);
2422 kfree(data, M_MFIBUF);
2424 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
2425 mfi_release_command(cm);
2426 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2430 case MFI_LINUX_SET_AEN_2: /* AEN Linux ioctl shim */
2431 error = copyin(arg, &l_aen, sizeof(l_aen));
2434 kprintf("AEN IMPLEMENTED for pid %d\n", curproc->p_pid);
2435 mfi_aen_entry = kmalloc(sizeof(struct mfi_aen), M_MFIBUF,
2437 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
2438 if (mfi_aen_entry != NULL) {
2439 mfi_aen_entry->p = curproc;
2440 TAILQ_INSERT_TAIL(&sc->mfi_aen_pids, mfi_aen_entry,
2443 error = mfi_aen_register(sc, l_aen.laen_seq_num,
2444 l_aen.laen_class_locale);
2447 TAILQ_REMOVE(&sc->mfi_aen_pids, mfi_aen_entry,
2449 kfree(mfi_aen_entry, M_MFIBUF);
2451 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2455 device_printf(sc->mfi_dev, "IOCTL 0x%lx not handled\n", cmd);
2464 mfi_kqfilter(struct dev_kqfilter_args *ap)
2466 cdev_t dev = ap->a_head.a_dev;
2467 struct knote *kn = ap->a_kn;
2468 struct mfi_softc *sc;
2469 struct klist *klist;
2474 switch (kn->kn_filter) {
2476 kn->kn_fop = &mfi_read_filterops;
2477 kn->kn_hook = (caddr_t)sc;
2480 kn->kn_fop = &mfi_write_filterops;
2481 kn->kn_hook = (caddr_t)sc;
2484 ap->a_result = EOPNOTSUPP;
2488 klist = &sc->mfi_kq.ki_note;
2489 knote_insert(klist, kn);
2495 mfi_filter_detach(struct knote *kn)
2497 struct mfi_softc *sc = (struct mfi_softc *)kn->kn_hook;
2498 struct klist *klist = &sc->mfi_kq.ki_note;
2500 knote_remove(klist, kn);
2504 mfi_filter_read(struct knote *kn, long hint)
2506 struct mfi_softc *sc = (struct mfi_softc *)kn->kn_hook;
2509 if (sc->mfi_aen_triggered != 0) {
2511 sc->mfi_aen_triggered = 0;
2513 if (sc->mfi_aen_triggered == 0 && sc->mfi_aen_cm == NULL)
2514 kn->kn_flags |= EV_ERROR;
2517 sc->mfi_poll_waiting = 1;
2523 mfi_filter_write(struct knote *kn, long hint)
2531 struct mfi_softc *sc;
2532 struct mfi_command *cm;
2538 dc = devclass_find("mfi");
2540 kprintf("No mfi dev class\n");
2544 for (i = 0; ; i++) {
2545 sc = devclass_get_softc(dc, i);
2548 device_printf(sc->mfi_dev, "Dumping\n\n");
2550 deadline = time_second - MFI_CMD_TIMEOUT;
2551 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
2552 TAILQ_FOREACH(cm, &sc->mfi_busy, cm_link) {
2553 if (cm->cm_timestamp < deadline) {
2554 device_printf(sc->mfi_dev,
2555 "COMMAND %p TIMEOUT AFTER %d SECONDS\n", cm,
2556 (int)(time_second - cm->cm_timestamp));
2567 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2574 mfi_timeout(void *data)
2576 struct mfi_softc *sc = (struct mfi_softc *)data;
2577 struct mfi_command *cm;
2581 deadline = time_second - MFI_CMD_TIMEOUT;
2582 lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
2583 TAILQ_FOREACH(cm, &sc->mfi_busy, cm_link) {
2584 if (sc->mfi_aen_cm == cm)
2586 if ((sc->mfi_aen_cm != cm) && (cm->cm_timestamp < deadline)) {
2587 device_printf(sc->mfi_dev,
2588 "COMMAND %p TIMEOUT AFTER %d SECONDS\n", cm,
2589 (int)(time_second - cm->cm_timestamp));
2591 MFI_VALIDATE_CMD(sc, cm);
2601 lockmgr(&sc->mfi_io_lock, LK_RELEASE);
2603 callout_reset(&sc->mfi_watchdog_callout, MFI_CMD_TIMEOUT * hz,