2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions
6 * Copyright 1994-2009 The FreeBSD Project.
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY,OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * The views and conclusions contained in the software and documentation
28 * are those of the authors and should not be interpreted as representing
29 * official policies,either expressed or implied, of the FreeBSD Project.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/kernel.h>
41 #include <sys/selinfo.h>
45 #include <sys/ioccom.h>
46 #include <sys/eventhandler.h>
47 #include <sys/callout.h>
49 #include <machine/bus.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
52 #include <sys/malloc.h>
54 #include <dev/mfi/mfireg.h>
55 #include <dev/mfi/mfi_ioctl.h>
56 #include <dev/mfi/mfivar.h>
58 struct mfi_cmd_tbolt *mfi_tbolt_get_cmd(struct mfi_softc *sc);
59 union mfi_mpi2_request_descriptor *
60 mfi_tbolt_get_request_descriptor(struct mfi_softc *sc, uint16_t index);
61 void mfi_tbolt_complete_cmd(struct mfi_softc *sc);
62 int mfi_tbolt_build_io(struct mfi_softc *sc, struct mfi_command *mfi_cmd,
63 struct mfi_cmd_tbolt *cmd);
64 static inline void mfi_tbolt_return_cmd(struct mfi_softc *sc,
65 struct mfi_cmd_tbolt *cmd);
66 union mfi_mpi2_request_descriptor *mfi_tbolt_build_mpt_cmd(struct mfi_softc
67 *sc, struct mfi_command *cmd);
69 mfi_build_mpt_pass_thru(struct mfi_softc *sc, struct mfi_command *mfi_cmd);
70 union mfi_mpi2_request_descriptor *mfi_build_and_issue_cmd(struct mfi_softc
71 *sc, struct mfi_command *mfi_cmd);
72 int mfi_tbolt_is_ldio(struct mfi_command *mfi_cmd);
73 void mfi_tbolt_build_ldio(struct mfi_softc *sc, struct mfi_command *mfi_cmd,
74 struct mfi_cmd_tbolt *cmd);
75 static int mfi_tbolt_make_sgl(struct mfi_softc *sc, struct mfi_command
76 *mfi_cmd, pMpi25IeeeSgeChain64_t sgl_ptr, struct mfi_cmd_tbolt *cmd);
77 static int mfi_tbolt_build_cdb(struct mfi_softc *sc, struct mfi_command
78 *mfi_cmd, uint8_t *cdb);
80 map_tbolt_cmd_status(struct mfi_command *mfi_cmd, uint8_t status,
82 static void mfi_issue_pending_cmds_again (struct mfi_softc *sc);
83 static void mfi_kill_hba (struct mfi_softc *sc);
84 static void mfi_process_fw_state_chg_isr(void *arg);
85 static void mfi_sync_map_complete(struct mfi_command *);
86 static void mfi_queue_map_sync(struct mfi_softc *sc);
88 #define MFI_FUSION_ENABLE_INTERRUPT_MASK (0x00000008)
91 mfi_tbolt_enable_intr_ppc(struct mfi_softc *sc)
93 MFI_WRITE4(sc, MFI_OMSK, ~MFI_FUSION_ENABLE_INTERRUPT_MASK);
94 MFI_READ4(sc, MFI_OMSK);
98 mfi_tbolt_disable_intr_ppc(struct mfi_softc *sc)
100 MFI_WRITE4(sc, MFI_OMSK, 0xFFFFFFFF);
101 MFI_READ4(sc, MFI_OMSK);
105 mfi_tbolt_read_fw_status_ppc(struct mfi_softc *sc)
107 return MFI_READ4(sc, MFI_OSP0);
111 mfi_tbolt_check_clear_intr_ppc(struct mfi_softc *sc)
113 int32_t status, mfi_status = 0;
115 status = MFI_READ4(sc, MFI_OSTS);
118 MFI_WRITE4(sc, MFI_OSTS, status);
119 MFI_READ4(sc, MFI_OSTS);
120 if (status & MFI_STATE_CHANGE_INTERRUPT) {
121 mfi_status |= MFI_FIRMWARE_STATE_CHANGE;
126 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
129 MFI_READ4(sc, MFI_OSTS);
135 mfi_tbolt_issue_cmd_ppc(struct mfi_softc *sc, bus_addr_t bus_add,
138 bus_add |= (MFI_REQ_DESCRIPT_FLAGS_MFA
139 << MFI_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
140 MFI_WRITE4(sc, MFI_IQPL, (uint32_t)bus_add);
141 MFI_WRITE4(sc, MFI_IQPH, (uint32_t)((uint64_t)bus_add >> 32));
145 * mfi_tbolt_adp_reset - For controller reset
146 * @regs: MFI register set
149 mfi_tbolt_adp_reset(struct mfi_softc *sc)
151 int retry = 0, i = 0;
154 MFI_WRITE4(sc, MFI_WSR, 0xF);
155 MFI_WRITE4(sc, MFI_WSR, 4);
156 MFI_WRITE4(sc, MFI_WSR, 0xB);
157 MFI_WRITE4(sc, MFI_WSR, 2);
158 MFI_WRITE4(sc, MFI_WSR, 7);
159 MFI_WRITE4(sc, MFI_WSR, 0xD);
161 for (i = 0; i < 10000; i++) ;
163 HostDiag = (uint32_t)MFI_READ4(sc, MFI_HDR);
165 while (!( HostDiag & DIAG_WRITE_ENABLE)) {
166 for (i = 0; i < 1000; i++);
167 HostDiag = (uint32_t)MFI_READ4(sc, MFI_HDR);
168 device_printf(sc->mfi_dev, "ADP_RESET_TBOLT: retry time=%x, "
169 "hostdiag=%x\n", retry, HostDiag);
175 device_printf(sc->mfi_dev, "ADP_RESET_TBOLT: HostDiag=%x\n", HostDiag);
177 MFI_WRITE4(sc, MFI_HDR, (HostDiag | DIAG_RESET_ADAPTER));
179 for (i=0; i < 10; i++) {
180 for (i = 0; i < 10000; i++);
183 HostDiag = (uint32_t)MFI_READ4(sc, MFI_RSR);
184 while (HostDiag & DIAG_RESET_ADAPTER) {
185 for (i = 0; i < 1000; i++) ;
186 HostDiag = (uint32_t)MFI_READ4(sc, MFI_RSR);
187 device_printf(sc->mfi_dev, "ADP_RESET_TBOLT: retry time=%x, "
188 "hostdiag=%x\n", retry, HostDiag);
197 * This routine initialize Thunderbolt specific device information
200 mfi_tbolt_init_globals(struct mfi_softc *sc)
202 /* Initialize single reply size and Message size */
203 sc->reply_size = MEGASAS_THUNDERBOLT_REPLY_SIZE;
204 sc->raid_io_msg_size = MEGASAS_THUNDERBOLT_NEW_MSG_SIZE;
207 * Calculating how many SGEs allowed in a allocated main message
208 * (size of the Message - Raid SCSI IO message size(except SGE))
210 * (0x100 - (0x90 - 0x10)) / 0x10 = 8
212 sc->max_SGEs_in_main_message =
213 (uint8_t)((sc->raid_io_msg_size
214 - (sizeof(struct mfi_mpi2_request_raid_scsi_io)
215 - sizeof(MPI2_SGE_IO_UNION))) / sizeof(MPI2_SGE_IO_UNION));
217 * (Command frame size allocaed in SRB ext - Raid SCSI IO message size)
219 * (1280 - 256) / 16 = 64
221 sc->max_SGEs_in_chain_message = (MR_COMMAND_SIZE
222 - sc->raid_io_msg_size) / sizeof(MPI2_SGE_IO_UNION);
224 * (0x08-1) + 0x40 = 0x47 - 0x01 = 0x46 one is left for command
227 sc->mfi_max_sge = (sc->max_SGEs_in_main_message - 1)
228 + sc->max_SGEs_in_chain_message - 1;
230 * This is the offset in number of 4 * 32bit words to the next chain
231 * (0x100 - 0x10)/0x10 = 0xF(15)
233 sc->chain_offset_value_for_main_message = (sc->raid_io_msg_size
234 - sizeof(MPI2_SGE_IO_UNION))/16;
235 sc->chain_offset_value_for_mpt_ptmsg
236 = offsetof(struct mfi_mpi2_request_raid_scsi_io, SGL)/16;
237 sc->mfi_cmd_pool_tbolt = NULL;
238 sc->request_desc_pool = NULL;
242 * This function calculates the memory requirement for Thunderbolt
243 * controller, returns the total required memory in bytes
247 mfi_tbolt_get_memory_requirement(struct mfi_softc *sc)
250 size = MEGASAS_THUNDERBOLT_MSG_ALLIGNMENT; /* for Alignment */
251 size += sc->raid_io_msg_size * (sc->mfi_max_fw_cmds + 1);
252 size += sc->reply_size * sc->mfi_max_fw_cmds;
253 /* this is for SGL's */
254 size += MEGASAS_MAX_SZ_CHAIN_FRAME * sc->mfi_max_fw_cmds;
260 * This function will prepare message pools for the Thunderbolt controller
262 * DevExt - HBA miniport driver's adapter data storage structure
263 * pMemLocation - start of the memory allocated for Thunderbolt.
269 mfi_tbolt_init_desc_pool(struct mfi_softc *sc, uint8_t* mem_location,
270 uint32_t tbolt_contg_length)
273 uint8_t *addr = mem_location;
275 /* Request Descriptor Base physical Address */
277 /* For Request Decriptors Virtual Memory */
278 /* Initialise the aligned IO Frames Virtual Memory Pointer */
279 if (((uintptr_t)addr) & (0xFF)) {
280 addr = &addr[sc->raid_io_msg_size];
281 addr = (uint8_t *)((uintptr_t)addr & (~0xFF));
282 sc->request_message_pool_align = addr;
284 sc->request_message_pool_align = addr;
286 offset = sc->request_message_pool_align - sc->request_message_pool;
287 sc->request_msg_busaddr = sc->mfi_tb_busaddr + offset;
289 /* DJA XXX should this be bus dma ??? */
290 /* Skip request message pool */
291 addr = &addr[sc->raid_io_msg_size * (sc->mfi_max_fw_cmds + 1)];
292 /* Reply Frame Pool is initialized */
293 sc->reply_frame_pool = (struct mfi_mpi2_reply_header *) addr;
294 if (((uintptr_t)addr) & (0xFF)) {
295 addr = &addr[sc->reply_size];
296 addr = (uint8_t *)((uintptr_t)addr & (~0xFF));
298 sc->reply_frame_pool_align
299 = (struct mfi_mpi2_reply_header *)addr;
301 offset = (uintptr_t)sc->reply_frame_pool_align
302 - (uintptr_t)sc->request_message_pool;
303 sc->reply_frame_busaddr = sc->mfi_tb_busaddr + offset;
305 /* Skip Reply Frame Pool */
306 addr += sc->reply_size * sc->mfi_max_fw_cmds;
307 sc->reply_pool_limit = addr;
309 /* initializing reply address to 0xFFFFFFFF */
310 memset((uint8_t *)sc->reply_frame_pool, 0xFF,
311 (sc->reply_size * sc->mfi_max_fw_cmds));
313 offset = sc->reply_size * sc->mfi_max_fw_cmds;
314 sc->sg_frame_busaddr = sc->reply_frame_busaddr + offset;
315 /* initialize the last_reply_idx to 0 */
316 sc->last_reply_idx = 0;
317 offset = (sc->sg_frame_busaddr + (MEGASAS_MAX_SZ_CHAIN_FRAME *
318 sc->mfi_max_fw_cmds)) - sc->mfi_tb_busaddr;
319 if (offset > tbolt_contg_length)
320 device_printf(sc->mfi_dev, "Error:Initialized more than "
326 * This routine prepare and issue INIT2 frame to the Firmware
330 mfi_tbolt_init_MFI_queue(struct mfi_softc *sc)
332 struct MPI2_IOC_INIT_REQUEST *mpi2IocInit;
333 struct mfi_init_frame *mfi_init;
334 uintptr_t offset = 0;
335 bus_addr_t phyAddress;
336 MFI_ADDRESS *mfiAddressTemp;
337 struct mfi_command *cm;
340 mpi2IocInit = (struct MPI2_IOC_INIT_REQUEST *)sc->mfi_tb_ioc_init_desc;
341 /* Check if initialization is already completed */
342 if (sc->MFA_enabled) {
346 mtx_lock(&sc->mfi_io_lock);
347 if ((cm = mfi_dequeue_free(sc)) == NULL) {
348 mtx_unlock(&sc->mfi_io_lock);
351 cm->cm_frame = (union mfi_frame *)((uintptr_t)sc->mfi_tb_init);
352 cm->cm_frame_busaddr = sc->mfi_tb_init_busaddr;
353 cm->cm_dmamap = sc->mfi_tb_init_dmamap;
354 cm->cm_frame->header.context = 0;
359 * Abuse the SG list area of the frame to hold the init_qinfo
362 mfi_init = &cm->cm_frame->init;
364 bzero(mpi2IocInit, sizeof(struct MPI2_IOC_INIT_REQUEST));
365 mpi2IocInit->Function = MPI2_FUNCTION_IOC_INIT;
366 mpi2IocInit->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
368 /* set MsgVersion and HeaderVersion host driver was built with */
369 mpi2IocInit->MsgVersion = MPI2_VERSION;
370 mpi2IocInit->HeaderVersion = MPI2_HEADER_VERSION;
371 mpi2IocInit->SystemRequestFrameSize = sc->raid_io_msg_size/4;
372 mpi2IocInit->ReplyDescriptorPostQueueDepth
373 = (uint16_t)sc->mfi_max_fw_cmds;
374 mpi2IocInit->ReplyFreeQueueDepth = 0; /* Not supported by MR. */
376 /* Get physical address of reply frame pool */
377 offset = (uintptr_t) sc->reply_frame_pool_align
378 - (uintptr_t)sc->request_message_pool;
379 phyAddress = sc->mfi_tb_busaddr + offset;
381 (MFI_ADDRESS *)&mpi2IocInit->ReplyDescriptorPostQueueAddress;
382 mfiAddressTemp->u.addressLow = (uint32_t)phyAddress;
383 mfiAddressTemp->u.addressHigh = (uint32_t)((uint64_t)phyAddress >> 32);
385 /* Get physical address of request message pool */
386 offset = sc->request_message_pool_align - sc->request_message_pool;
387 phyAddress = sc->mfi_tb_busaddr + offset;
388 mfiAddressTemp = (MFI_ADDRESS *)&mpi2IocInit->SystemRequestFrameBaseAddress;
389 mfiAddressTemp->u.addressLow = (uint32_t)phyAddress;
390 mfiAddressTemp->u.addressHigh = (uint32_t)((uint64_t)phyAddress >> 32);
391 mpi2IocInit->ReplyFreeQueueAddress = 0; /* Not supported by MR. */
392 mpi2IocInit->TimeStamp = time_uptime;
395 snprintf((char *)sc->verbuf, strlen(MEGASAS_VERSION) + 2, "%s\n",
397 mfi_init->driver_ver_lo = (uint32_t)sc->verbuf_h_busaddr;
398 mfi_init->driver_ver_hi =
399 (uint32_t)((uint64_t)sc->verbuf_h_busaddr >> 32);
401 /* Get the physical address of the mpi2 ioc init command */
402 phyAddress = sc->mfi_tb_ioc_init_busaddr;
403 mfi_init->qinfo_new_addr_lo = (uint32_t)phyAddress;
404 mfi_init->qinfo_new_addr_hi = (uint32_t)((uint64_t)phyAddress >> 32);
405 mfi_init->header.flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
407 mfi_init->header.cmd = MFI_CMD_INIT;
408 mfi_init->header.data_len = sizeof(struct MPI2_IOC_INIT_REQUEST);
409 mfi_init->header.cmd_status = MFI_STAT_INVALID_STATUS;
412 cm->cm_flags |= MFI_CMD_POLLED;
413 cm->cm_timestamp = time_uptime;
414 if ((error = mfi_mapcmd(sc, cm)) != 0) {
415 device_printf(sc->mfi_dev, "failed to send IOC init2 "
416 "command %d at %lx\n", error, (long)cm->cm_frame_busaddr);
417 mfi_release_command(cm);
418 mtx_unlock(&sc->mfi_io_lock);
421 mfi_release_command(cm);
422 mtx_unlock(&sc->mfi_io_lock);
424 if (mfi_init->header.cmd_status == 0) {
428 device_printf(sc->mfi_dev, "Init command Failed %x\n",
429 mfi_init->header.cmd_status);
438 mfi_tbolt_alloc_cmd(struct mfi_softc *sc)
440 struct mfi_cmd_tbolt *cmd;
441 bus_addr_t io_req_base_phys;
442 uint8_t *io_req_base;
443 int i = 0, j = 0, offset = 0;
446 * sc->mfi_cmd_pool_tbolt is an array of struct mfi_cmd_tbolt pointers.
447 * Allocate the dynamic array first and then allocate individual
450 sc->request_desc_pool = malloc(sizeof(
451 union mfi_mpi2_request_descriptor) * sc->mfi_max_fw_cmds,
452 M_MFIBUF, M_NOWAIT|M_ZERO);
453 sc->mfi_cmd_pool_tbolt = malloc(sizeof(struct mfi_cmd_tbolt*)
454 * sc->mfi_max_fw_cmds, M_MFIBUF, M_NOWAIT|M_ZERO);
456 if (!sc->mfi_cmd_pool_tbolt) {
457 device_printf(sc->mfi_dev, "out of memory. Could not alloc "
458 "memory for cmd_list_fusion\n");
462 for (i = 0; i < sc->mfi_max_fw_cmds; i++) {
463 sc->mfi_cmd_pool_tbolt[i] = malloc(sizeof(
464 struct mfi_cmd_tbolt),M_MFIBUF, M_NOWAIT|M_ZERO);
466 if (!sc->mfi_cmd_pool_tbolt[i]) {
467 device_printf(sc->mfi_dev, "Could not alloc cmd list "
470 for (j = 0; j < i; j++)
471 free(sc->mfi_cmd_pool_tbolt[j], M_MFIBUF);
473 free(sc->mfi_cmd_pool_tbolt, M_MFIBUF);
474 sc->mfi_cmd_pool_tbolt = NULL;
479 * The first 256 bytes (SMID 0) is not used. Don't add to the cmd
482 io_req_base = sc->request_message_pool_align
483 + MEGASAS_THUNDERBOLT_NEW_MSG_SIZE;
484 io_req_base_phys = sc->request_msg_busaddr
485 + MEGASAS_THUNDERBOLT_NEW_MSG_SIZE;
488 * Add all the commands to command pool (instance->cmd_pool)
490 /* SMID 0 is reserved. Set SMID/index from 1 */
492 for (i = 0; i < sc->mfi_max_fw_cmds; i++) {
493 cmd = sc->mfi_cmd_pool_tbolt[i];
494 offset = MEGASAS_THUNDERBOLT_NEW_MSG_SIZE * i;
496 cmd->request_desc = (union mfi_mpi2_request_descriptor *)
497 (sc->request_desc_pool + i);
498 cmd->io_request = (struct mfi_mpi2_request_raid_scsi_io *)
499 (io_req_base + offset);
500 cmd->io_request_phys_addr = io_req_base_phys + offset;
501 cmd->sg_frame = (MPI2_SGE_IO_UNION *)(sc->reply_pool_limit
502 + i * MEGASAS_MAX_SZ_CHAIN_FRAME);
503 cmd->sg_frame_phys_addr = sc->sg_frame_busaddr + i
504 * MEGASAS_MAX_SZ_CHAIN_FRAME;
506 TAILQ_INSERT_TAIL(&(sc->mfi_cmd_tbolt_tqh), cmd, next);
512 mfi_tbolt_reset(struct mfi_softc *sc)
516 mtx_lock(&sc->mfi_io_lock);
517 if (sc->hw_crit_error) {
518 device_printf(sc->mfi_dev, "HW CRITICAL ERROR\n");
519 mtx_unlock(&sc->mfi_io_lock);
523 if (sc->mfi_flags & MFI_FLAGS_TBOLT) {
524 fw_state = sc->mfi_read_fw_status(sc);
525 if ((fw_state & MFI_FWSTATE_FAULT) == MFI_FWSTATE_FAULT) {
526 if ((sc->disableOnlineCtrlReset == 0)
527 && (sc->adpreset == 0)) {
528 device_printf(sc->mfi_dev, "Adapter RESET "
529 "condition is detected\n");
531 sc->issuepend_done = 0;
533 sc->last_reply_idx = 0;
534 mfi_process_fw_state_chg_isr((void *) sc);
536 mtx_unlock(&sc->mfi_io_lock);
540 mtx_unlock(&sc->mfi_io_lock);
545 * mfi_intr_tbolt - isr entry point
548 mfi_intr_tbolt(void *arg)
550 struct mfi_softc *sc = (struct mfi_softc *)arg;
552 if (sc->mfi_check_clear_intr(sc) == 1) {
555 if (sc->mfi_detaching)
557 mtx_lock(&sc->mfi_io_lock);
558 mfi_tbolt_complete_cmd(sc);
559 if (sc->mfi_flags & MFI_FLAGS_QFRZN)
560 sc->mfi_flags &= ~MFI_FLAGS_QFRZN;
562 mtx_unlock(&sc->mfi_io_lock);
567 * map_cmd_status - Maps FW cmd status to OS cmd status
568 * @cmd : Pointer to cmd
569 * @status : status of cmd returned by FW
570 * @ext_status : ext status of cmd returned by FW
574 map_tbolt_cmd_status(struct mfi_command *mfi_cmd, uint8_t status,
580 mfi_cmd->cm_frame->header.cmd_status = 0;
581 mfi_cmd->cm_frame->dcmd.header.cmd_status = 0;
584 case MFI_STAT_SCSI_IO_FAILED:
585 case MFI_STAT_LD_INIT_IN_PROGRESS:
586 mfi_cmd->cm_frame->header.cmd_status = status;
587 mfi_cmd->cm_frame->header.scsi_status = ext_status;
588 mfi_cmd->cm_frame->dcmd.header.cmd_status = status;
589 mfi_cmd->cm_frame->dcmd.header.scsi_status
593 case MFI_STAT_SCSI_DONE_WITH_ERROR:
594 mfi_cmd->cm_frame->header.cmd_status = ext_status;
595 mfi_cmd->cm_frame->dcmd.header.cmd_status = ext_status;
598 case MFI_STAT_LD_OFFLINE:
599 case MFI_STAT_DEVICE_NOT_FOUND:
600 mfi_cmd->cm_frame->header.cmd_status = status;
601 mfi_cmd->cm_frame->dcmd.header.cmd_status = status;
605 mfi_cmd->cm_frame->header.cmd_status = status;
606 mfi_cmd->cm_frame->dcmd.header.cmd_status = status;
612 * mfi_tbolt_return_cmd - Return a cmd to free command pool
613 * @instance: Adapter soft state
614 * @cmd: Command packet to be returned to free command pool
617 mfi_tbolt_return_cmd(struct mfi_softc *sc, struct mfi_cmd_tbolt *cmd)
619 mtx_assert(&sc->mfi_io_lock, MA_OWNED);
621 TAILQ_INSERT_TAIL(&sc->mfi_cmd_tbolt_tqh, cmd, next);
625 mfi_tbolt_complete_cmd(struct mfi_softc *sc)
627 struct mfi_mpi2_reply_header *desc, *reply_desc;
628 struct mfi_command *cmd_mfi, *cmd_mfi_check; /* For MFA Cmds */
629 struct mfi_cmd_tbolt *cmd_tbolt;
631 uint8_t reply_descript_type;
632 struct mfi_mpi2_request_raid_scsi_io *scsi_io_req;
633 uint32_t status, extStatus;
634 uint16_t num_completed;
635 union desc_value val;
637 desc = (struct mfi_mpi2_reply_header *)
638 ((uintptr_t)sc->reply_frame_pool_align
639 + sc->last_reply_idx * sc->reply_size);
643 device_printf(sc->mfi_dev, "reply desc is NULL!!\n");
645 reply_descript_type = reply_desc->ReplyFlags
646 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
647 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
651 val.word = ((union mfi_mpi2_reply_descriptor *)desc)->words;
653 /* Read Reply descriptor */
654 while ((val.u.low != 0xFFFFFFFF) && (val.u.high != 0xFFFFFFFF)) {
655 smid = reply_desc->SMID;
656 if (!smid || smid > sc->mfi_max_fw_cmds + 1) {
657 device_printf(sc->mfi_dev, "smid is %x. Cannot "
658 "proceed. Returning \n", smid);
662 cmd_tbolt = sc->mfi_cmd_pool_tbolt[smid - 1];
663 cmd_mfi = &sc->mfi_commands[cmd_tbolt->sync_cmd_idx];
664 scsi_io_req = cmd_tbolt->io_request;
666 status = cmd_mfi->cm_frame->dcmd.header.cmd_status;
667 extStatus = cmd_mfi->cm_frame->dcmd.header.scsi_status;
668 map_tbolt_cmd_status(cmd_mfi, status, extStatus);
670 /* remove command from busy queue if not polled */
671 TAILQ_FOREACH(cmd_mfi_check, &sc->mfi_busy, cm_link) {
672 if (cmd_mfi_check == cmd_mfi) {
673 mfi_remove_busy(cmd_mfi);
677 cmd_mfi->cm_error = 0;
678 mfi_complete(sc, cmd_mfi);
679 mfi_tbolt_return_cmd(sc, cmd_tbolt);
681 sc->last_reply_idx++;
682 if (sc->last_reply_idx >= sc->mfi_max_fw_cmds) {
683 MFI_WRITE4(sc, MFI_RPI, sc->last_reply_idx);
684 sc->last_reply_idx = 0;
686 /*set it back to all 0xfff.*/
687 ((union mfi_mpi2_reply_descriptor*)desc)->words =
692 /* Get the next reply descriptor */
693 desc = (struct mfi_mpi2_reply_header *)
694 ((uintptr_t)sc->reply_frame_pool_align
695 + sc->last_reply_idx * sc->reply_size);
697 val.word = ((union mfi_mpi2_reply_descriptor*)desc)->words;
698 reply_descript_type = reply_desc->ReplyFlags
699 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
700 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
707 /* update replyIndex to FW */
708 if (sc->last_reply_idx)
709 MFI_WRITE4(sc, MFI_RPI, sc->last_reply_idx);
715 * mfi_get_cmd - Get a command from the free pool
716 * @instance: Adapter soft state
718 * Returns a free command from the pool
721 struct mfi_cmd_tbolt *
722 mfi_tbolt_get_cmd(struct mfi_softc *sc)
724 struct mfi_cmd_tbolt *cmd = NULL;
726 mtx_assert(&sc->mfi_io_lock, MA_OWNED);
728 cmd = TAILQ_FIRST(&sc->mfi_cmd_tbolt_tqh);
729 TAILQ_REMOVE(&sc->mfi_cmd_tbolt_tqh, cmd, next);
730 memset((uint8_t *)cmd->sg_frame, 0, MEGASAS_MAX_SZ_CHAIN_FRAME);
731 memset((uint8_t *)cmd->io_request, 0,
732 MEGASAS_THUNDERBOLT_NEW_MSG_SIZE);
736 union mfi_mpi2_request_descriptor *
737 mfi_tbolt_get_request_descriptor(struct mfi_softc *sc, uint16_t index)
741 if (index >= sc->mfi_max_fw_cmds) {
742 device_printf(sc->mfi_dev, "Invalid SMID (0x%x)request "
743 "for descriptor\n", index);
746 p = sc->request_desc_pool + sizeof(union mfi_mpi2_request_descriptor)
748 memset(p, 0, sizeof(union mfi_mpi2_request_descriptor));
749 return (union mfi_mpi2_request_descriptor *)p;
753 /* Used to build IOCTL cmd */
755 mfi_build_mpt_pass_thru(struct mfi_softc *sc, struct mfi_command *mfi_cmd)
757 MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
758 struct mfi_mpi2_request_raid_scsi_io *io_req;
759 struct mfi_cmd_tbolt *cmd;
761 cmd = mfi_tbolt_get_cmd(sc);
764 mfi_cmd->cm_extra_frames = cmd->index; /* Frame count used as SMID */
765 cmd->sync_cmd_idx = mfi_cmd->cm_index;
766 io_req = cmd->io_request;
767 mpi25_ieee_chain = (MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
769 io_req->Function = MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
770 io_req->SGLOffset0 = offsetof(struct mfi_mpi2_request_raid_scsi_io,
772 io_req->ChainOffset = sc->chain_offset_value_for_mpt_ptmsg;
774 mpi25_ieee_chain->Address = mfi_cmd->cm_frame_busaddr;
777 In MFI pass thru, nextChainOffset will always be zero to
778 indicate the end of the chain.
780 mpi25_ieee_chain->Flags= MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT
781 | MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
783 /* setting the length to the maximum length */
784 mpi25_ieee_chain->Length = 1024;
790 mfi_tbolt_build_ldio(struct mfi_softc *sc, struct mfi_command *mfi_cmd,
791 struct mfi_cmd_tbolt *cmd)
793 uint32_t start_lba_lo = 0, start_lba_hi = 0, device_id;
794 struct mfi_mpi2_request_raid_scsi_io *io_request;
795 struct IO_REQUEST_INFO io_info;
797 device_id = mfi_cmd->cm_frame->io.header.target_id;
798 io_request = cmd->io_request;
799 io_request->RaidContext.TargetID = device_id;
800 io_request->RaidContext.Status = 0;
801 io_request->RaidContext.exStatus =0;
803 start_lba_lo = mfi_cmd->cm_frame->io.lba_lo;
804 start_lba_hi = mfi_cmd->cm_frame->io.lba_hi;
806 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
807 io_info.ldStartBlock = ((uint64_t)start_lba_hi << 32) | start_lba_lo;
808 io_info.numBlocks = mfi_cmd->cm_frame->io.header.data_len;
809 io_info.ldTgtId = device_id;
810 if ((mfi_cmd->cm_frame->header.flags & MFI_FRAME_DIR_READ) ==
814 io_request->RaidContext.timeoutValue
815 = MFI_FUSION_FP_DEFAULT_TIMEOUT;
816 io_request->Function = MPI2_FUNCTION_LD_IO_REQUEST;
817 io_request->DevHandle = device_id;
818 cmd->request_desc->header.RequestFlags
819 = (MFI_REQ_DESCRIPT_FLAGS_LD_IO
820 << MFI_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
821 if ((io_request->IoFlags == 6) && (io_info.numBlocks == 0))
822 io_request->RaidContext.RegLockLength = 0x100;
823 io_request->DataLength = mfi_cmd->cm_frame->io.header.data_len
828 mfi_tbolt_is_ldio(struct mfi_command *mfi_cmd)
830 if (mfi_cmd->cm_frame->header.cmd == MFI_CMD_LD_READ
831 || mfi_cmd->cm_frame->header.cmd == MFI_CMD_LD_WRITE)
838 mfi_tbolt_build_io(struct mfi_softc *sc, struct mfi_command *mfi_cmd,
839 struct mfi_cmd_tbolt *cmd)
843 uint8_t cdb[32], cdb_len;
846 struct mfi_mpi2_request_raid_scsi_io *io_request = cmd->io_request;
848 device_id = mfi_cmd->cm_frame->header.target_id;
850 /* Have to build CDB here for TB as BSD don't have a scsi layer */
851 if ((cdb_len = mfi_tbolt_build_cdb(sc, mfi_cmd, cdb)) == 1)
854 /* Just the CDB length,rest of the Flags are zero */
855 io_request->IoFlags = cdb_len;
856 memcpy(io_request->CDB.CDB32, cdb, 32);
858 if (mfi_tbolt_is_ldio(mfi_cmd))
859 mfi_tbolt_build_ldio(sc, mfi_cmd , cmd);
866 sge_count = mfi_tbolt_make_sgl(sc, mfi_cmd,
867 (pMpi25IeeeSgeChain64_t) &io_request->SGL, cmd);
868 if (sge_count > sc->mfi_max_sge) {
869 device_printf(sc->mfi_dev, "Error. sge_count (0x%x) exceeds "
870 "max (0x%x) allowed\n", sge_count, sc->mfi_max_sge);
873 io_request->RaidContext.numSGE = sge_count;
874 io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
876 if (mfi_cmd->cm_frame->header.cmd == MFI_CMD_LD_WRITE)
877 io_request->Control = MPI2_SCSIIO_CONTROL_WRITE;
879 io_request->Control = MPI2_SCSIIO_CONTROL_READ;
881 io_request->SGLOffset0 = offsetof(
882 struct mfi_mpi2_request_raid_scsi_io, SGL)/4;
884 io_request->SenseBufferLowAddress = mfi_cmd->cm_sense_busaddr;
885 io_request->SenseBufferLength = MFI_SENSE_LEN;
890 mfi_tbolt_build_cdb(struct mfi_softc *sc, struct mfi_command *mfi_cmd,
893 uint32_t lba_lo, lba_hi, num_lba;
896 if (mfi_cmd == NULL || cdb == NULL)
898 num_lba = mfi_cmd->cm_frame->io.header.data_len;
899 lba_lo = mfi_cmd->cm_frame->io.lba_lo;
900 lba_hi = mfi_cmd->cm_frame->io.lba_hi;
902 if (lba_hi == 0 && (num_lba <= 0xFF) && (lba_lo <= 0x1FFFFF)) {
903 if (mfi_cmd->cm_frame->header.cmd == MFI_CMD_LD_WRITE)
904 /* Read 6 or Write 6 */
905 cdb[0] = (uint8_t) (0x0A);
907 cdb[0] = (uint8_t) (0x08);
909 cdb[4] = (uint8_t) num_lba;
910 cdb[3] = (uint8_t) (lba_lo & 0xFF);
911 cdb[2] = (uint8_t) (lba_lo >> 8);
912 cdb[1] = (uint8_t) ((lba_lo >> 16) & 0x1F);
915 else if (lba_hi == 0 && (num_lba <= 0xFFFF) && (lba_lo <= 0xFFFFFFFF)) {
916 if (mfi_cmd->cm_frame->header.cmd == MFI_CMD_LD_WRITE)
917 /* Read 10 or Write 10 */
918 cdb[0] = (uint8_t) (0x2A);
920 cdb[0] = (uint8_t) (0x28);
921 cdb[8] = (uint8_t) (num_lba & 0xFF);
922 cdb[7] = (uint8_t) (num_lba >> 8);
923 cdb[5] = (uint8_t) (lba_lo & 0xFF);
924 cdb[4] = (uint8_t) (lba_lo >> 8);
925 cdb[3] = (uint8_t) (lba_lo >> 16);
926 cdb[2] = (uint8_t) (lba_lo >> 24);
928 } else if ((num_lba > 0xFFFF) && (lba_hi == 0)) {
929 if (mfi_cmd->cm_frame->header.cmd == MFI_CMD_LD_WRITE)
930 /* Read 12 or Write 12 */
931 cdb[0] = (uint8_t) (0xAA);
933 cdb[0] = (uint8_t) (0xA8);
934 cdb[9] = (uint8_t) (num_lba & 0xFF);
935 cdb[8] = (uint8_t) (num_lba >> 8);
936 cdb[7] = (uint8_t) (num_lba >> 16);
937 cdb[6] = (uint8_t) (num_lba >> 24);
938 cdb[5] = (uint8_t) (lba_lo & 0xFF);
939 cdb[4] = (uint8_t) (lba_lo >> 8);
940 cdb[3] = (uint8_t) (lba_lo >> 16);
941 cdb[2] = (uint8_t) (lba_lo >> 24);
944 if (mfi_cmd->cm_frame->header.cmd == MFI_CMD_LD_WRITE)
945 cdb[0] = (uint8_t) (0x8A);
947 cdb[0] = (uint8_t) (0x88);
948 cdb[13] = (uint8_t) (num_lba & 0xFF);
949 cdb[12] = (uint8_t) (num_lba >> 8);
950 cdb[11] = (uint8_t) (num_lba >> 16);
951 cdb[10] = (uint8_t) (num_lba >> 24);
952 cdb[9] = (uint8_t) (lba_lo & 0xFF);
953 cdb[8] = (uint8_t) (lba_lo >> 8);
954 cdb[7] = (uint8_t) (lba_lo >> 16);
955 cdb[6] = (uint8_t) (lba_lo >> 24);
956 cdb[5] = (uint8_t) (lba_hi & 0xFF);
957 cdb[4] = (uint8_t) (lba_hi >> 8);
958 cdb[3] = (uint8_t) (lba_hi >> 16);
959 cdb[2] = (uint8_t) (lba_hi >> 24);
966 mfi_tbolt_make_sgl(struct mfi_softc *sc, struct mfi_command *mfi_cmd,
967 pMpi25IeeeSgeChain64_t sgl_ptr, struct mfi_cmd_tbolt *cmd)
969 uint8_t i, sg_processed, sg_to_process;
970 uint8_t sge_count, sge_idx;
971 union mfi_sgl *os_sgl;
974 * Return 0 if there is no data transfer
976 if (!mfi_cmd->cm_sg || !mfi_cmd->cm_len) {
977 device_printf(sc->mfi_dev, "Buffer empty \n");
980 os_sgl = mfi_cmd->cm_sg;
981 sge_count = mfi_cmd->cm_frame->header.sg_count;
983 if (sge_count > sc->mfi_max_sge) {
984 device_printf(sc->mfi_dev, "sgl ptr %p sg_cnt %d \n",
989 if (sge_count > sc->max_SGEs_in_main_message)
990 /* One element to store the chain info */
991 sge_idx = sc->max_SGEs_in_main_message - 1;
995 for (i = 0; i < sge_idx; i++) {
997 * For 32bit BSD we are getting 32 bit SGL's from OS
998 * but FW only take 64 bit SGL's so copying from 32 bit
1001 if (sc->mfi_flags & MFI_FLAGS_SKINNY) {
1002 sgl_ptr->Length = os_sgl->sg_skinny[i].len;
1003 sgl_ptr->Address = os_sgl->sg_skinny[i].addr;
1005 sgl_ptr->Length = os_sgl->sg32[i].len;
1006 sgl_ptr->Address = os_sgl->sg32[i].addr;
1010 cmd->io_request->ChainOffset = 0;
1015 if (sg_processed < sge_count) {
1016 pMpi25IeeeSgeChain64_t sg_chain;
1017 sg_to_process = sge_count - sg_processed;
1018 cmd->io_request->ChainOffset =
1019 sc->chain_offset_value_for_main_message;
1021 /* Prepare chain element */
1022 sg_chain->NextChainOffset = 0;
1023 sg_chain->Flags = (MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1024 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1025 sg_chain->Length = (sizeof(MPI2_SGE_IO_UNION) *
1026 (sge_count - sg_processed));
1027 sg_chain->Address = cmd->sg_frame_phys_addr;
1028 sgl_ptr = (pMpi25IeeeSgeChain64_t)cmd->sg_frame;
1029 for (; i < sge_count; i++) {
1030 if (sc->mfi_flags & MFI_FLAGS_SKINNY) {
1031 sgl_ptr->Length = os_sgl->sg_skinny[i].len;
1032 sgl_ptr->Address = os_sgl->sg_skinny[i].addr;
1034 sgl_ptr->Length = os_sgl->sg32[i].len;
1035 sgl_ptr->Address = os_sgl->sg32[i].addr;
1044 union mfi_mpi2_request_descriptor *
1045 mfi_build_and_issue_cmd(struct mfi_softc *sc, struct mfi_command *mfi_cmd)
1047 struct mfi_cmd_tbolt *cmd;
1048 union mfi_mpi2_request_descriptor *req_desc = NULL;
1050 cmd = mfi_tbolt_get_cmd(sc);
1053 mfi_cmd->cm_extra_frames = cmd->index;
1054 cmd->sync_cmd_idx = mfi_cmd->cm_index;
1057 req_desc = mfi_tbolt_get_request_descriptor(sc, index-1);
1058 if (mfi_tbolt_build_io(sc, mfi_cmd, cmd))
1060 req_desc->header.SMID = index;
1064 union mfi_mpi2_request_descriptor *
1065 mfi_tbolt_build_mpt_cmd(struct mfi_softc *sc, struct mfi_command *cmd)
1067 union mfi_mpi2_request_descriptor *req_desc = NULL;
1069 if (mfi_build_mpt_pass_thru(sc, cmd)) {
1070 device_printf(sc->mfi_dev, "Couldn't build MFI pass thru "
1074 /* For fusion the frame_count variable is used for SMID */
1075 index = cmd->cm_extra_frames;
1077 req_desc = mfi_tbolt_get_request_descriptor(sc, index - 1);
1081 bzero(req_desc, sizeof(*req_desc));
1082 req_desc->header.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1083 MFI_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1084 req_desc->header.SMID = index;
1089 mfi_tbolt_send_frame(struct mfi_softc *sc, struct mfi_command *cm)
1091 struct mfi_frame_header *hdr;
1093 union mfi_mpi2_request_descriptor *req_desc = NULL;
1094 int tm = MFI_POLL_TIMEOUT_SECS * 1000;
1096 hdr = &cm->cm_frame->header;
1097 cdb = cm->cm_frame->pass.cdb;
1100 if ((cm->cm_flags & MFI_CMD_POLLED) == 0) {
1101 cm->cm_timestamp = time_uptime;
1102 mfi_enqueue_busy(cm);
1104 else { /* still get interrupts for it */
1105 hdr->cmd_status = MFI_STAT_INVALID_STATUS;
1106 hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
1109 if (hdr->cmd == MFI_CMD_PD_SCSI_IO) {
1110 /* check for inquiry commands coming from CLI */
1111 if (cdb[0] != 0x28 || cdb[0] != 0x2A) {
1112 if ((req_desc = mfi_tbolt_build_mpt_cmd(sc, cm)) ==
1114 device_printf(sc->mfi_dev, "Mapping from MFI "
1115 "to MPT Failed \n");
1120 device_printf(sc->mfi_dev, "DJA NA XXX SYSPDIO\n");
1122 else if (hdr->cmd == MFI_CMD_LD_SCSI_IO ||
1123 hdr->cmd == MFI_CMD_LD_READ || hdr->cmd == MFI_CMD_LD_WRITE) {
1124 if ((req_desc = mfi_build_and_issue_cmd(sc, cm)) == NULL) {
1125 device_printf(sc->mfi_dev, "LDIO Failed \n");
1129 if ((req_desc = mfi_tbolt_build_mpt_cmd(sc, cm)) == NULL) {
1130 device_printf(sc->mfi_dev, "Mapping from MFI to MPT "
1134 MFI_WRITE4(sc, MFI_ILQP, (req_desc->words & 0xFFFFFFFF));
1135 MFI_WRITE4(sc, MFI_IHQP, (req_desc->words >>0x20));
1137 if ((cm->cm_flags & MFI_CMD_POLLED) == 0)
1140 /* This is a polled command, so busy-wait for it to complete. */
1141 while (hdr->cmd_status == MFI_STAT_INVALID_STATUS) {
1148 if (hdr->cmd_status == MFI_STAT_INVALID_STATUS) {
1149 device_printf(sc->mfi_dev, "Frame %p timed out "
1150 "command 0x%X\n", hdr, cm->cm_frame->dcmd.opcode);
1157 mfi_issue_pending_cmds_again (struct mfi_softc *sc)
1159 struct mfi_command *cm, *tmp;
1161 mtx_assert(&sc->mfi_io_lock, MA_OWNED);
1162 TAILQ_FOREACH_REVERSE_SAFE(cm, &sc->mfi_busy, BUSYQ, cm_link, tmp) {
1164 cm->retry_for_fw_reset++;
1167 * If a command has continuously been tried multiple times
1168 * and causing a FW reset condition, no further recoveries
1169 * should be performed on the controller
1171 if (cm->retry_for_fw_reset == 3) {
1172 device_printf(sc->mfi_dev, "megaraid_sas: command %d "
1173 "was tried multiple times during adapter reset"
1174 "Shutting down the HBA\n", cm->cm_index);
1176 sc->hw_crit_error = 1;
1180 if ((cm->cm_flags & MFI_ON_MFIQ_BUSY) != 0) {
1181 struct mfi_cmd_tbolt *cmd;
1182 mfi_remove_busy(cm);
1183 cmd = sc->mfi_cmd_pool_tbolt[cm->cm_extra_frames -
1185 mfi_tbolt_return_cmd(sc, cmd);
1186 if ((cm->cm_flags & MFI_ON_MFIQ_MASK) == 0) {
1187 if (cm->cm_frame->dcmd.opcode !=
1188 MFI_DCMD_CTRL_EVENT_WAIT) {
1189 device_printf(sc->mfi_dev,
1190 "APJ ****requeue command %d \n",
1192 mfi_requeue_ready(cm);
1196 mfi_release_command(cm);
1203 mfi_kill_hba (struct mfi_softc *sc)
1205 if (sc->mfi_flags & MFI_FLAGS_TBOLT)
1206 MFI_WRITE4 (sc, 0x00,MFI_STOP_ADP);
1208 MFI_WRITE4 (sc, MFI_IDB,MFI_STOP_ADP);
1212 mfi_process_fw_state_chg_isr(void *arg)
1214 struct mfi_softc *sc= (struct mfi_softc *)arg;
1215 struct mfi_cmd_tbolt *cmd;
1218 if (sc->adpreset == 1) {
1219 device_printf(sc->mfi_dev, "First stage of FW reset "
1222 sc->mfi_adp_reset(sc);
1223 sc->mfi_enable_intr(sc);
1225 device_printf(sc->mfi_dev, "First stage of reset complete, "
1226 "second stage initiated...\n");
1230 /* waiting for about 20 second before start the second init */
1231 for (int wait = 0; wait < 20000; wait++)
1233 device_printf(sc->mfi_dev, "Second stage of FW reset "
1235 while ((status = MFI_READ4(sc, MFI_RSR)) & 0x04);
1237 sc->mfi_disable_intr(sc);
1239 /* We expect the FW state to be READY */
1240 if (mfi_transition_firmware(sc)) {
1241 device_printf(sc->mfi_dev, "controller is not in "
1244 sc->hw_crit_error= 1;
1247 if ((error = mfi_tbolt_init_MFI_queue(sc)) != 0)
1250 mtx_lock(&sc->mfi_io_lock);
1252 sc->mfi_enable_intr(sc);
1254 free(sc->mfi_aen_cm->cm_data, M_MFIBUF);
1255 mfi_remove_busy(sc->mfi_aen_cm);
1256 cmd = sc->mfi_cmd_pool_tbolt[sc->mfi_aen_cm->cm_extra_frames
1258 mfi_tbolt_return_cmd(sc, cmd);
1259 if (sc->mfi_aen_cm) {
1260 mfi_release_command(sc->mfi_aen_cm);
1261 sc->mfi_aen_cm = NULL;
1263 if (sc->mfi_map_sync_cm) {
1264 mfi_release_command(sc->mfi_map_sync_cm);
1265 sc->mfi_map_sync_cm = NULL;
1267 mfi_issue_pending_cmds_again(sc);
1270 * Issue pending command can result in adapter being marked
1271 * dead because of too many re-tries. Check for that
1272 * condition before clearing the reset condition on the FW
1274 if (!sc->hw_crit_error) {
1276 * Initiate AEN (Asynchronous Event Notification)
1278 mfi_aen_setup(sc, sc->last_seq_num);
1279 sc->issuepend_done = 1;
1280 device_printf(sc->mfi_dev, "second stage of reset "
1281 "complete, FW is ready now.\n");
1283 device_printf(sc->mfi_dev, "second stage of reset "
1284 "never completed, hba was marked offline.\n");
1287 device_printf(sc->mfi_dev, "mfi_process_fw_state_chg_isr "
1288 "called with unhandled value:%d\n", sc->adpreset);
1290 mtx_unlock(&sc->mfi_io_lock);
1294 * The ThunderBolt HW has an option for the driver to directly
1295 * access the underlying disks and operate on the RAID. To
1296 * do this there needs to be a capability to keep the RAID controller
1297 * and driver in sync. The FreeBSD driver does not take advantage
1298 * of this feature since it adds a lot of complexity and slows down
1299 * performance. Performance is gained by using the controller's
1302 * Even though this driver doesn't access the disks directly, an
1303 * AEN like command is used to inform the RAID firmware to "sync"
1304 * with all LD's via the MFI_DCMD_LD_MAP_GET_INFO command. This
1305 * command in write mode will return when the RAID firmware has
1306 * detected a change to the RAID state. Examples of this type
1307 * of change are removing a disk. Once the command returns then
1308 * the driver needs to acknowledge this and "sync" all LD's again.
1309 * This repeats until we shutdown. Then we need to cancel this
1312 * If this is not done right the RAID firmware will not remove a
1313 * pulled drive and the RAID won't go degraded etc. Effectively,
1314 * stopping any RAID mangement to functions.
1316 * Doing another LD sync, requires the use of an event since the
1317 * driver needs to do a mfi_wait_command and can't do that in an
1320 * The driver could get the RAID state via the MFI_DCMD_LD_MAP_GET_INFO
1321 * That requires a bunch of structure and it is simplier to just do
1322 * the MFI_DCMD_LD_GET_LIST versus walking the RAID map.
1326 mfi_tbolt_sync_map_info(struct mfi_softc *sc)
1329 struct mfi_command *cmd;
1330 struct mfi_dcmd_frame *dcmd;
1331 uint32_t context = 0;
1332 union mfi_ld_ref *ld_sync;
1334 struct mfi_frame_header *hdr;
1335 struct mfi_command *cm = NULL;
1336 struct mfi_ld_list *list = NULL;
1338 if (sc->mfi_map_sync_cm != NULL || sc->cm_map_abort)
1341 mtx_lock(&sc->mfi_io_lock);
1342 error = mfi_dcmd_command(sc, &cm, MFI_DCMD_LD_GET_LIST,
1343 (void **)&list, sizeof(*list));
1347 cm->cm_flags = MFI_CMD_POLLED | MFI_CMD_DATAIN;
1348 if (mfi_wait_command(sc, cm) != 0) {
1349 device_printf(sc->mfi_dev, "Failed to get device listing\n");
1353 hdr = &cm->cm_frame->header;
1354 if (hdr->cmd_status != MFI_STAT_OK) {
1355 device_printf(sc->mfi_dev, "MFI_DCMD_LD_GET_LIST failed %x\n",
1360 ld_size = sizeof(*ld_sync) * list->ld_count;
1361 mtx_unlock(&sc->mfi_io_lock);
1362 ld_sync = (union mfi_ld_ref *) malloc(ld_size, M_MFIBUF,
1364 if (ld_sync == NULL) {
1365 device_printf(sc->mfi_dev, "Failed to allocate sync\n");
1368 for (i = 0; i < list->ld_count; i++) {
1369 ld_sync[i].ref = list->ld_list[i].ld.ref;
1372 mtx_lock(&sc->mfi_io_lock);
1373 if ((cmd = mfi_dequeue_free(sc)) == NULL) {
1374 device_printf(sc->mfi_dev, "Failed to get command\n");
1375 free(ld_sync, M_MFIBUF);
1379 context = cmd->cm_frame->header.context;
1380 bzero(cmd->cm_frame, sizeof(union mfi_frame));
1381 cmd->cm_frame->header.context = context;
1383 dcmd = &cmd->cm_frame->dcmd;
1384 bzero(dcmd->mbox, MFI_MBOX_SIZE);
1385 dcmd->header.cmd = MFI_CMD_DCMD;
1386 dcmd->header.flags = MFI_FRAME_DIR_WRITE;
1387 dcmd->header.timeout = 0;
1388 dcmd->header.data_len = ld_size;
1389 dcmd->header.scsi_status = 0;
1390 dcmd->opcode = MFI_DCMD_LD_MAP_GET_INFO;
1391 cmd->cm_sg = &dcmd->sgl;
1392 cmd->cm_total_frame_size = MFI_DCMD_FRAME_SIZE;
1393 cmd->cm_data = ld_sync;
1394 cmd->cm_private = ld_sync;
1396 cmd->cm_len = ld_size;
1397 cmd->cm_complete = mfi_sync_map_complete;
1398 sc->mfi_map_sync_cm = cmd;
1400 cmd->cm_flags = MFI_CMD_DATAOUT;
1401 cmd->cm_frame->dcmd.mbox[0] = list->ld_count;
1402 cmd->cm_frame->dcmd.mbox[1] = MFI_DCMD_MBOX_PEND_FLAG;
1404 if ((error = mfi_mapcmd(sc, cmd)) != 0) {
1405 device_printf(sc->mfi_dev, "failed to send map sync\n");
1406 free(ld_sync, M_MFIBUF);
1407 sc->mfi_map_sync_cm = NULL;
1408 mfi_requeue_ready(cmd);
1414 free(list, M_MFIBUF);
1416 mfi_release_command(cm);
1417 mtx_unlock(&sc->mfi_io_lock);
1421 mfi_sync_map_complete(struct mfi_command *cm)
1423 struct mfi_frame_header *hdr;
1424 struct mfi_softc *sc;
1428 mtx_assert(&sc->mfi_io_lock, MA_OWNED);
1430 hdr = &cm->cm_frame->header;
1432 if (sc->mfi_map_sync_cm == NULL)
1435 if (sc->cm_map_abort ||
1436 hdr->cmd_status == MFI_STAT_INVALID_STATUS) {
1437 sc->cm_map_abort = 0;
1441 free(cm->cm_data, M_MFIBUF);
1442 sc->mfi_map_sync_cm = NULL;
1443 wakeup(&sc->mfi_map_sync_cm);
1444 mfi_release_command(cm);
1446 /* set it up again so the driver can catch more events */
1448 mfi_queue_map_sync(sc);
1453 mfi_queue_map_sync(struct mfi_softc *sc)
1455 mtx_assert(&sc->mfi_io_lock, MA_OWNED);
1456 taskqueue_enqueue(taskqueue_swi, &sc->mfi_map_sync_task);
1460 mfi_handle_map_sync(void *context, int pending)
1462 struct mfi_softc *sc;
1465 mfi_tbolt_sync_map_info(sc);