2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2009-2011 LSI Corporation.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * FILE: megaraid_sas_fusion.c
22 * Authors: LSI Corporation
24 * Adam Radford <linuxraid@lsi.com>
26 * Send feedback to: <megaraidlinux@lsi.com>
28 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/pci.h>
35 #include <linux/list.h>
36 #include <linux/moduleparam.h>
37 #include <linux/module.h>
38 #include <linux/spinlock.h>
39 #include <linux/interrupt.h>
40 #include <linux/delay.h>
41 #include <linux/uio.h>
42 #include <linux/uaccess.h>
44 #include <linux/compat.h>
45 #include <linux/blkdev.h>
46 #include <linux/mutex.h>
47 #include <linux/poll.h>
49 #include <scsi/scsi.h>
50 #include <scsi/scsi_cmnd.h>
51 #include <scsi/scsi_device.h>
52 #include <scsi/scsi_host.h>
54 #include "megaraid_sas_fusion.h"
55 #include "megaraid_sas.h"
57 extern void megasas_free_cmds(struct megasas_instance *instance);
58 extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
61 megasas_complete_cmd(struct megasas_instance *instance,
62 struct megasas_cmd *cmd, u8 alt_status);
63 int megasas_is_ldio(struct scsi_cmnd *cmd);
65 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
68 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
69 int megasas_alloc_cmds(struct megasas_instance *instance);
71 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
73 megasas_issue_polled(struct megasas_instance *instance,
74 struct megasas_cmd *cmd);
77 MR_BuildRaidContext(struct IO_REQUEST_INFO *io_info,
78 struct RAID_CONTEXT *pRAID_Context,
79 struct MR_FW_RAID_MAP_ALL *map);
80 u16 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_FW_RAID_MAP_ALL *map);
81 struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
83 u16 MR_GetLDTgtId(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
86 megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
88 u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
89 struct LD_LOAD_BALANCE_INFO *lbInfo);
90 u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
91 struct IO_REQUEST_INFO *in_info);
92 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
93 void megaraid_sas_kill_hba(struct megasas_instance *instance);
95 extern u32 megasas_dbg_lvl;
98 * megasas_enable_intr_fusion - Enables interrupts
99 * @regs: MFI register set
102 megasas_enable_intr_fusion(struct megasas_register_set __iomem *regs)
104 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
106 /* Dummy readl to force pci flush */
107 readl(®s->outbound_intr_mask);
111 * megasas_disable_intr_fusion - Disables interrupt
112 * @regs: MFI register set
115 megasas_disable_intr_fusion(struct megasas_register_set __iomem *regs)
117 u32 mask = 0xFFFFFFFF;
120 writel(mask, ®s->outbound_intr_mask);
121 /* Dummy readl to force pci flush */
122 status = readl(®s->outbound_intr_mask);
126 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
130 * Check if it is our interrupt
132 status = readl(®s->outbound_intr_status);
135 writel(status, ®s->outbound_intr_status);
136 readl(®s->outbound_intr_status);
139 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
146 * megasas_get_cmd_fusion - Get a command from the free pool
147 * @instance: Adapter soft state
149 * Returns a free command from the pool
151 struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
155 struct fusion_context *fusion =
156 (struct fusion_context *)instance->ctrl_context;
157 struct megasas_cmd_fusion *cmd = NULL;
159 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
161 if (!list_empty(&fusion->cmd_pool)) {
162 cmd = list_entry((&fusion->cmd_pool)->next,
163 struct megasas_cmd_fusion, list);
164 list_del_init(&cmd->list);
166 printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
169 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
174 * megasas_return_cmd_fusion - Return a cmd to free command pool
175 * @instance: Adapter soft state
176 * @cmd: Command packet to be returned to free command pool
179 megasas_return_cmd_fusion(struct megasas_instance *instance,
180 struct megasas_cmd_fusion *cmd)
183 struct fusion_context *fusion =
184 (struct fusion_context *)instance->ctrl_context;
186 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
189 cmd->sync_cmd_idx = (u32)ULONG_MAX;
190 list_add_tail(&cmd->list, &fusion->cmd_pool);
192 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
196 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
197 * @instance: Adapter soft state
199 static void megasas_teardown_frame_pool_fusion(
200 struct megasas_instance *instance)
203 struct fusion_context *fusion = instance->ctrl_context;
205 u16 max_cmd = instance->max_fw_cmds;
207 struct megasas_cmd_fusion *cmd;
209 if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
210 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
211 "sense pool : %p\n", fusion->sg_dma_pool,
212 fusion->sense_dma_pool);
217 * Return all frames to pool
219 for (i = 0; i < max_cmd; i++) {
221 cmd = fusion->cmd_list[i];
224 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
225 cmd->sg_frame_phys_addr);
228 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
229 cmd->sense_phys_addr);
233 * Now destroy the pool itself
235 pci_pool_destroy(fusion->sg_dma_pool);
236 pci_pool_destroy(fusion->sense_dma_pool);
238 fusion->sg_dma_pool = NULL;
239 fusion->sense_dma_pool = NULL;
243 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
244 * @instance: Adapter soft state
247 megasas_free_cmds_fusion(struct megasas_instance *instance)
250 struct fusion_context *fusion = instance->ctrl_context;
252 u32 max_cmds, req_sz, reply_sz, io_frames_sz;
255 req_sz = fusion->request_alloc_sz;
256 reply_sz = fusion->reply_alloc_sz;
257 io_frames_sz = fusion->io_frames_alloc_sz;
259 max_cmds = instance->max_fw_cmds;
261 /* Free descriptors and request Frames memory */
262 if (fusion->req_frames_desc)
263 dma_free_coherent(&instance->pdev->dev, req_sz,
264 fusion->req_frames_desc,
265 fusion->req_frames_desc_phys);
267 if (fusion->reply_frames_desc) {
268 pci_pool_free(fusion->reply_frames_desc_pool,
269 fusion->reply_frames_desc,
270 fusion->reply_frames_desc_phys);
271 pci_pool_destroy(fusion->reply_frames_desc_pool);
274 if (fusion->io_request_frames) {
275 pci_pool_free(fusion->io_request_frames_pool,
276 fusion->io_request_frames,
277 fusion->io_request_frames_phys);
278 pci_pool_destroy(fusion->io_request_frames_pool);
281 /* Free the Fusion frame pool */
282 megasas_teardown_frame_pool_fusion(instance);
284 /* Free all the commands in the cmd_list */
285 for (i = 0; i < max_cmds; i++)
286 kfree(fusion->cmd_list[i]);
288 /* Free the cmd_list buffer itself */
289 kfree(fusion->cmd_list);
290 fusion->cmd_list = NULL;
292 INIT_LIST_HEAD(&fusion->cmd_pool);
296 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
297 * @instance: Adapter soft state
300 static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
304 struct fusion_context *fusion;
305 struct megasas_cmd_fusion *cmd;
306 u32 total_sz_chain_frame;
308 fusion = instance->ctrl_context;
309 max_cmd = instance->max_fw_cmds;
311 total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
314 * Use DMA pool facility provided by PCI layer
317 fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
319 total_sz_chain_frame, 4,
321 if (!fusion->sg_dma_pool) {
322 printk(KERN_DEBUG "megasas: failed to setup request pool "
326 fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
328 SCSI_SENSE_BUFFERSIZE, 64, 0);
330 if (!fusion->sense_dma_pool) {
331 printk(KERN_DEBUG "megasas: failed to setup sense pool "
333 pci_pool_destroy(fusion->sg_dma_pool);
334 fusion->sg_dma_pool = NULL;
339 * Allocate and attach a frame to each of the commands in cmd_list
341 for (i = 0; i < max_cmd; i++) {
343 cmd = fusion->cmd_list[i];
345 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
347 &cmd->sg_frame_phys_addr);
349 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
350 GFP_KERNEL, &cmd->sense_phys_addr);
352 * megasas_teardown_frame_pool_fusion() takes care of freeing
353 * whatever has been allocated
355 if (!cmd->sg_frame || !cmd->sense) {
356 printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
357 megasas_teardown_frame_pool_fusion(instance);
365 * megasas_alloc_cmds_fusion - Allocates the command packets
366 * @instance: Adapter soft state
369 * Each frame has a 32-bit field called context. This context is used to get
370 * back the megasas_cmd_fusion from the frame when a frame gets completed
371 * In this driver, the 32 bit values are the indices into an array cmd_list.
372 * This array is used only to look up the megasas_cmd_fusion given the context.
373 * The free commands themselves are maintained in a linked list called cmd_pool.
375 * cmds are formed in the io_request and sg_frame members of the
376 * megasas_cmd_fusion. The context field is used to get a request descriptor
377 * and is used as SMID of the cmd.
378 * SMID value range is from 1 to max_fw_cmds.
381 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
384 u32 max_cmd, io_frames_sz;
385 struct fusion_context *fusion;
386 struct megasas_cmd_fusion *cmd;
387 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
389 dma_addr_t io_req_base_phys;
392 fusion = instance->ctrl_context;
394 max_cmd = instance->max_fw_cmds;
396 fusion->req_frames_desc =
397 dma_alloc_coherent(&instance->pdev->dev,
398 fusion->request_alloc_sz,
399 &fusion->req_frames_desc_phys, GFP_KERNEL);
401 if (!fusion->req_frames_desc) {
402 printk(KERN_ERR "megasas; Could not allocate memory for "
407 fusion->reply_frames_desc_pool =
408 pci_pool_create("reply_frames pool", instance->pdev,
409 fusion->reply_alloc_sz, 16, 0);
411 if (!fusion->reply_frames_desc_pool) {
412 printk(KERN_ERR "megasas; Could not allocate memory for "
413 "reply_frame pool\n");
414 goto fail_reply_desc;
417 fusion->reply_frames_desc =
418 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
419 &fusion->reply_frames_desc_phys);
420 if (!fusion->reply_frames_desc) {
421 printk(KERN_ERR "megasas; Could not allocate memory for "
422 "reply_frame pool\n");
423 pci_pool_destroy(fusion->reply_frames_desc_pool);
424 goto fail_reply_desc;
427 reply_desc = fusion->reply_frames_desc;
428 for (i = 0; i < fusion->reply_q_depth; i++, reply_desc++)
429 reply_desc->Words = ULLONG_MAX;
431 io_frames_sz = fusion->io_frames_alloc_sz;
433 fusion->io_request_frames_pool =
434 pci_pool_create("io_request_frames pool", instance->pdev,
435 fusion->io_frames_alloc_sz, 16, 0);
437 if (!fusion->io_request_frames_pool) {
438 printk(KERN_ERR "megasas: Could not allocate memory for "
439 "io_request_frame pool\n");
443 fusion->io_request_frames =
444 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
445 &fusion->io_request_frames_phys);
446 if (!fusion->io_request_frames) {
447 printk(KERN_ERR "megasas: Could not allocate memory for "
448 "io_request_frames frames\n");
449 pci_pool_destroy(fusion->io_request_frames_pool);
454 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
455 * Allocate the dynamic array first and then allocate individual
458 fusion->cmd_list = kmalloc(sizeof(struct megasas_cmd_fusion *)
459 *max_cmd, GFP_KERNEL);
461 if (!fusion->cmd_list) {
462 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
463 "memory for cmd_list_fusion\n");
467 memset(fusion->cmd_list, 0, sizeof(struct megasas_cmd_fusion *)
470 max_cmd = instance->max_fw_cmds;
471 for (i = 0; i < max_cmd; i++) {
472 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
474 if (!fusion->cmd_list[i]) {
475 printk(KERN_ERR "Could not alloc cmd list fusion\n");
477 for (j = 0; j < i; j++)
478 kfree(fusion->cmd_list[j]);
480 kfree(fusion->cmd_list);
481 fusion->cmd_list = NULL;
486 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
487 io_req_base = fusion->io_request_frames +
488 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
489 io_req_base_phys = fusion->io_request_frames_phys +
490 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
493 * Add all the commands to command pool (fusion->cmd_pool)
496 /* SMID 0 is reserved. Set SMID/index from 1 */
497 for (i = 0; i < max_cmd; i++) {
498 cmd = fusion->cmd_list[i];
499 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
500 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
503 cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */
504 cmd->instance = instance;
506 (struct MPI2_RAID_SCSI_IO_REQUEST *)
507 (io_req_base + offset);
508 memset(cmd->io_request, 0,
509 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
510 cmd->io_request_phys_addr = io_req_base_phys + offset;
512 list_add_tail(&cmd->list, &fusion->cmd_pool);
516 * Create a frame pool and assign one frame to each cmd
518 if (megasas_create_frame_pool_fusion(instance)) {
519 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
520 megasas_free_cmds_fusion(instance);
527 pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
528 fusion->io_request_frames_phys);
529 pci_pool_destroy(fusion->io_request_frames_pool);
531 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
532 fusion->reply_frames_desc,
533 fusion->reply_frames_desc_phys);
534 pci_pool_free(fusion->reply_frames_desc_pool,
535 fusion->reply_frames_desc,
536 fusion->reply_frames_desc_phys);
537 pci_pool_destroy(fusion->reply_frames_desc_pool);
540 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
541 fusion->req_frames_desc,
542 fusion->req_frames_desc_phys);
548 * wait_and_poll - Issues a polling command
549 * @instance: Adapter soft state
550 * @cmd: Command packet to be issued
552 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
555 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd)
558 struct megasas_header *frame_hdr = &cmd->frame->hdr;
560 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
563 * Wait for cmd_status to change
565 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
570 if (frame_hdr->cmd_status == 0xff)
577 * megasas_ioc_init_fusion - Initializes the FW
578 * @instance: Adapter soft state
580 * Issues the IOC Init cmd
583 megasas_ioc_init_fusion(struct megasas_instance *instance)
585 struct megasas_init_frame *init_frame;
586 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
587 dma_addr_t ioc_init_handle;
588 struct megasas_cmd *cmd;
590 struct fusion_context *fusion;
591 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
593 struct megasas_header *frame_hdr;
595 fusion = instance->ctrl_context;
597 cmd = megasas_get_cmd(instance);
600 printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
606 dma_alloc_coherent(&instance->pdev->dev,
607 sizeof(struct MPI2_IOC_INIT_REQUEST),
608 &ioc_init_handle, GFP_KERNEL);
610 if (!IOCInitMessage) {
611 printk(KERN_ERR "Could not allocate memory for "
617 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
619 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
620 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
621 IOCInitMessage->MsgVersion = MPI2_VERSION;
622 IOCInitMessage->HeaderVersion = MPI2_HEADER_VERSION;
623 IOCInitMessage->SystemRequestFrameSize =
624 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4;
626 IOCInitMessage->ReplyDescriptorPostQueueDepth = fusion->reply_q_depth;
627 IOCInitMessage->ReplyDescriptorPostQueueAddress =
628 fusion->reply_frames_desc_phys;
629 IOCInitMessage->SystemRequestFrameBaseAddress =
630 fusion->io_request_frames_phys;
632 init_frame = (struct megasas_init_frame *)cmd->frame;
633 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
635 frame_hdr = &cmd->frame->hdr;
636 frame_hdr->cmd_status = 0xFF;
637 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
639 init_frame->cmd = MFI_CMD_INIT;
640 init_frame->cmd_status = 0xFF;
642 init_frame->queue_info_new_phys_addr_lo = ioc_init_handle;
643 init_frame->data_xfer_len = sizeof(struct MPI2_IOC_INIT_REQUEST);
646 (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)fusion->req_frames_desc;
648 req_desc->Words = cmd->frame_phys_addr;
649 req_desc->MFAIo.RequestFlags =
650 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
651 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
654 * disable the intr before firing the init frame
656 instance->instancet->disable_intr(instance->reg_set);
658 for (i = 0; i < (10 * 1000); i += 20) {
659 if (readl(&instance->reg_set->doorbell) & 1)
665 instance->instancet->fire_cmd(instance, req_desc->u.low,
666 req_desc->u.high, instance->reg_set);
668 wait_and_poll(instance, cmd);
670 frame_hdr = &cmd->frame->hdr;
671 if (frame_hdr->cmd_status != 0) {
675 printk(KERN_ERR "megasas:IOC Init cmd success\n");
680 megasas_return_cmd(instance, cmd);
682 dma_free_coherent(&instance->pdev->dev,
683 sizeof(struct MPI2_IOC_INIT_REQUEST),
684 IOCInitMessage, ioc_init_handle);
690 * megasas_get_ld_map_info - Returns FW's ld_map structure
691 * @instance: Adapter soft state
692 * @pend: Pend the command or not
693 * Issues an internal command (DCMD) to get the FW's controller PD
694 * list structure. This information is mainly used to find out SYSTEM
695 * supported by the FW.
698 megasas_get_ld_map_info(struct megasas_instance *instance)
701 struct megasas_cmd *cmd;
702 struct megasas_dcmd_frame *dcmd;
703 struct MR_FW_RAID_MAP_ALL *ci;
706 struct fusion_context *fusion;
708 cmd = megasas_get_cmd(instance);
711 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
715 fusion = instance->ctrl_context;
718 megasas_return_cmd(instance, cmd);
722 dcmd = &cmd->frame->dcmd;
724 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
725 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
727 ci = fusion->ld_map[(instance->map_id & 1)];
728 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
731 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
732 megasas_return_cmd(instance, cmd);
736 memset(ci, 0, sizeof(*ci));
737 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
739 dcmd->cmd = MFI_CMD_DCMD;
740 dcmd->cmd_status = 0xFF;
742 dcmd->flags = MFI_FRAME_DIR_READ;
745 dcmd->data_xfer_len = size_map_info;
746 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
747 dcmd->sgl.sge32[0].phys_addr = ci_h;
748 dcmd->sgl.sge32[0].length = size_map_info;
750 if (!megasas_issue_polled(instance, cmd))
753 printk(KERN_ERR "megasas: Get LD Map Info Failed\n");
757 megasas_return_cmd(instance, cmd);
763 megasas_get_map_info(struct megasas_instance *instance)
765 struct fusion_context *fusion = instance->ctrl_context;
767 fusion->fast_path_io = 0;
768 if (!megasas_get_ld_map_info(instance)) {
769 if (MR_ValidateMapInfo(fusion->ld_map[(instance->map_id & 1)],
770 fusion->load_balance_info)) {
771 fusion->fast_path_io = 1;
779 * megasas_sync_map_info - Returns FW's ld_map structure
780 * @instance: Adapter soft state
782 * Issues an internal command (DCMD) to get the FW's controller PD
783 * list structure. This information is mainly used to find out SYSTEM
784 * supported by the FW.
787 megasas_sync_map_info(struct megasas_instance *instance)
790 struct megasas_cmd *cmd;
791 struct megasas_dcmd_frame *dcmd;
792 u32 size_sync_info, num_lds;
793 struct fusion_context *fusion;
794 struct MR_LD_TARGET_SYNC *ci = NULL;
795 struct MR_FW_RAID_MAP_ALL *map;
796 struct MR_LD_RAID *raid;
797 struct MR_LD_TARGET_SYNC *ld_sync;
801 cmd = megasas_get_cmd(instance);
804 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
809 fusion = instance->ctrl_context;
812 megasas_return_cmd(instance, cmd);
816 map = fusion->ld_map[instance->map_id & 1];
818 num_lds = map->raidMap.ldCount;
820 dcmd = &cmd->frame->dcmd;
822 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
824 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
826 ci = (struct MR_LD_TARGET_SYNC *)
827 fusion->ld_map[(instance->map_id - 1) & 1];
828 memset(ci, 0, sizeof(struct MR_FW_RAID_MAP_ALL));
830 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
832 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
834 for (i = 0; i < num_lds; i++, ld_sync++) {
835 raid = MR_LdRaidGet(i, map);
836 ld_sync->targetId = MR_GetLDTgtId(i, map);
837 ld_sync->seqNum = raid->seqNum;
840 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
841 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
843 dcmd->cmd = MFI_CMD_DCMD;
844 dcmd->cmd_status = 0xFF;
846 dcmd->flags = MFI_FRAME_DIR_WRITE;
849 dcmd->data_xfer_len = size_map_info;
850 dcmd->mbox.b[0] = num_lds;
851 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
852 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
853 dcmd->sgl.sge32[0].phys_addr = ci_h;
854 dcmd->sgl.sge32[0].length = size_map_info;
856 instance->map_update_cmd = cmd;
858 instance->instancet->issue_dcmd(instance, cmd);
864 * megasas_init_adapter_fusion - Initializes the FW
865 * @instance: Adapter soft state
867 * This is the main function for initializing firmware.
870 megasas_init_adapter_fusion(struct megasas_instance *instance)
872 struct megasas_register_set __iomem *reg_set;
873 struct fusion_context *fusion;
877 fusion = instance->ctrl_context;
879 reg_set = instance->reg_set;
882 * Get various operational parameters from status register
884 instance->max_fw_cmds =
885 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
886 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
889 * Reduce the max supported cmds by 1. This is to ensure that the
890 * reply_q_sz (1 more than the max cmd that driver may send)
891 * does not exceed max cmds that the FW can support
893 instance->max_fw_cmds = instance->max_fw_cmds-1;
894 /* Only internal cmds (DCMD) need to have MFI frames */
895 instance->max_mfi_cmds = MEGASAS_INT_CMDS;
897 max_cmd = instance->max_fw_cmds;
899 fusion->reply_q_depth = ((max_cmd + 1 + 15)/16)*16;
901 fusion->request_alloc_sz =
902 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
903 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
904 *(fusion->reply_q_depth);
905 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
906 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
907 (max_cmd + 1)); /* Extra 1 for SMID 0 */
909 fusion->max_sge_in_main_msg =
910 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
911 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
913 fusion->max_sge_in_chain =
914 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
916 instance->max_num_sge = fusion->max_sge_in_main_msg +
917 fusion->max_sge_in_chain - 2;
919 /* Used for pass thru MFI frame (DCMD) */
920 fusion->chain_offset_mfi_pthru =
921 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
923 fusion->chain_offset_io_request =
924 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
925 sizeof(union MPI2_SGE_IO_UNION))/16;
927 fusion->last_reply_idx = 0;
930 * Allocate memory for descriptors
931 * Create a pool of commands
933 if (megasas_alloc_cmds(instance))
934 goto fail_alloc_mfi_cmds;
935 if (megasas_alloc_cmds_fusion(instance))
936 goto fail_alloc_cmds;
938 if (megasas_ioc_init_fusion(instance))
941 instance->flag_ieee = 1;
943 fusion->map_sz = sizeof(struct MR_FW_RAID_MAP) +
944 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
946 fusion->fast_path_io = 0;
948 for (i = 0; i < 2; i++) {
949 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
951 &fusion->ld_map_phys[i],
953 if (!fusion->ld_map[i]) {
954 printk(KERN_ERR "megasas: Could not allocate memory "
960 if (!megasas_get_map_info(instance))
961 megasas_sync_map_info(instance);
967 dma_free_coherent(&instance->pdev->dev, fusion->map_sz,
968 fusion->ld_map[0], fusion->ld_map_phys[0]);
970 megasas_free_cmds_fusion(instance);
972 megasas_free_cmds(instance);
978 * megasas_fire_cmd_fusion - Sends command to the FW
979 * @frame_phys_addr : Physical address of cmd
980 * @frame_count : Number of frames for the command
981 * @regs : MFI register set
984 megasas_fire_cmd_fusion(struct megasas_instance *instance,
985 dma_addr_t req_desc_lo,
987 struct megasas_register_set __iomem *regs)
991 spin_lock_irqsave(&instance->hba_lock, flags);
994 &(regs)->inbound_low_queue_port);
995 writel(req_desc_hi, &(regs)->inbound_high_queue_port);
996 spin_unlock_irqrestore(&instance->hba_lock, flags);
1000 * map_cmd_status - Maps FW cmd status to OS cmd status
1001 * @cmd : Pointer to cmd
1002 * @status : status of cmd returned by FW
1003 * @ext_status : ext status of cmd returned by FW
1007 map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1013 cmd->scmd->result = DID_OK << 16;
1016 case MFI_STAT_SCSI_IO_FAILED:
1017 case MFI_STAT_LD_INIT_IN_PROGRESS:
1018 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1021 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1023 cmd->scmd->result = (DID_OK << 16) | ext_status;
1024 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1025 memset(cmd->scmd->sense_buffer, 0,
1026 SCSI_SENSE_BUFFERSIZE);
1027 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1028 SCSI_SENSE_BUFFERSIZE);
1029 cmd->scmd->result |= DRIVER_SENSE << 24;
1033 case MFI_STAT_LD_OFFLINE:
1034 case MFI_STAT_DEVICE_NOT_FOUND:
1035 cmd->scmd->result = DID_BAD_TARGET << 16;
1039 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1040 cmd->scmd->result = DID_ERROR << 16;
1046 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1047 * @instance: Adapter soft state
1048 * @scp: SCSI command from the mid-layer
1049 * @sgl_ptr: SGL to be filled in
1050 * @cmd: cmd we are working on
1052 * If successful, this function returns the number of SG elements.
1055 megasas_make_sgl_fusion(struct megasas_instance *instance,
1056 struct scsi_cmnd *scp,
1057 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1058 struct megasas_cmd_fusion *cmd)
1060 int i, sg_processed;
1061 int sge_count, sge_idx;
1062 struct scatterlist *os_sgl;
1063 struct fusion_context *fusion;
1065 fusion = instance->ctrl_context;
1067 cmd->io_request->ChainOffset = 0;
1069 sge_count = scsi_dma_map(scp);
1071 BUG_ON(sge_count < 0);
1073 if (sge_count > instance->max_num_sge || !sge_count)
1076 if (sge_count > fusion->max_sge_in_main_msg) {
1077 /* One element to store the chain info */
1078 sge_idx = fusion->max_sge_in_main_msg - 1;
1080 sge_idx = sge_count;
1082 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1083 sgl_ptr->Length = sg_dma_len(os_sgl);
1084 sgl_ptr->Address = sg_dma_address(os_sgl);
1088 sg_processed = i + 1;
1090 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1091 (sge_count > fusion->max_sge_in_main_msg)) {
1093 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1094 cmd->io_request->ChainOffset =
1095 fusion->chain_offset_io_request;
1097 /* Prepare chain element */
1098 sg_chain->NextChainOffset = 0;
1099 sg_chain->Flags = (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1100 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1101 sg_chain->Length = (sizeof(union MPI2_SGE_IO_UNION)
1102 *(sge_count - sg_processed));
1103 sg_chain->Address = cmd->sg_frame_phys_addr;
1106 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1114 * megasas_set_pd_lba - Sets PD LBA
1116 * @cdb_len: cdb length
1117 * @start_blk: Start block of IO
1119 * Used to set the PD LBA in CDB for FP IOs
1122 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1123 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1124 struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1126 struct MR_LD_RAID *raid;
1128 u64 start_blk = io_info->pdBlock;
1129 u8 *cdb = io_request->CDB.CDB32;
1130 u32 num_blocks = io_info->numBlocks;
1131 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1133 /* Check if T10 PI (DIF) is enabled for this LD */
1134 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1135 raid = MR_LdRaidGet(ld, local_map_ptr);
1136 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1137 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1138 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1139 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1141 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1142 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1144 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1145 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1148 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1149 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1150 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1151 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1152 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1153 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1154 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1155 cdb[19] = (u8)(start_blk & 0xff);
1157 /* Logical block reference tag */
1158 io_request->CDB.EEDP32.PrimaryReferenceTag =
1159 cpu_to_be32(ref_tag);
1160 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
1162 io_request->DataLength = num_blocks * 512;
1163 io_request->IoFlags = 32; /* Specify 32-byte cdb */
1165 /* Transfer length */
1166 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1167 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1168 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1169 cdb[31] = (u8)(num_blocks & 0xff);
1171 /* set SCSI IO EEDPFlags */
1172 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1173 io_request->EEDPFlags =
1174 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1175 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1176 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1177 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1178 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
1180 io_request->EEDPFlags =
1181 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1182 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
1184 io_request->Control |= (0x4 << 26);
1185 io_request->EEDPBlockSize = MEGASAS_EEDPBLOCKSIZE;
1187 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1188 if (((cdb_len == 12) || (cdb_len == 16)) &&
1189 (start_blk <= 0xffffffff)) {
1190 if (cdb_len == 16) {
1191 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1196 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1202 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1209 /* Transfer length */
1210 cdb[8] = (u8)(num_blocks & 0xff);
1211 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1213 io_request->IoFlags = 10; /* Specify 10-byte cdb */
1215 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1216 /* Convert to 16 byte CDB for large LBA's */
1219 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1224 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1231 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1238 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1245 /* Transfer length */
1246 cdb[13] = (u8)(num_blocks & 0xff);
1247 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1248 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1249 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1251 io_request->IoFlags = 16; /* Specify 16-byte cdb */
1255 /* Normal case, just load LBA here */
1259 u8 val = cdb[1] & 0xE0;
1260 cdb[3] = (u8)(start_blk & 0xff);
1261 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1262 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1266 cdb[5] = (u8)(start_blk & 0xff);
1267 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1268 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1269 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1272 cdb[5] = (u8)(start_blk & 0xff);
1273 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1274 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1275 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1278 cdb[9] = (u8)(start_blk & 0xff);
1279 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1280 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1281 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1282 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1283 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1284 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1285 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1292 * megasas_build_ldio_fusion - Prepares IOs to devices
1293 * @instance: Adapter soft state
1294 * @scp: SCSI command
1295 * @cmd: Command to be prepared
1297 * Prepares the io_request and chain elements (sg_frame) for IO
1298 * The IO can be for PD (Fast Path) or LD
1301 megasas_build_ldio_fusion(struct megasas_instance *instance,
1302 struct scsi_cmnd *scp,
1303 struct megasas_cmd_fusion *cmd)
1306 u32 start_lba_lo, start_lba_hi, device_id;
1307 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1308 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1309 struct IO_REQUEST_INFO io_info;
1310 struct fusion_context *fusion;
1311 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1313 device_id = MEGASAS_DEV_INDEX(instance, scp);
1315 fusion = instance->ctrl_context;
1317 io_request = cmd->io_request;
1318 io_request->RaidContext.VirtualDiskTgtId = device_id;
1319 io_request->RaidContext.status = 0;
1320 io_request->RaidContext.exStatus = 0;
1322 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1329 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1331 if (scp->cmd_len == 6) {
1332 io_request->DataLength = (u32) scp->cmnd[4];
1333 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1334 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1336 start_lba_lo &= 0x1FFFFF;
1340 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1342 else if (scp->cmd_len == 10) {
1343 io_request->DataLength = (u32) scp->cmnd[8] |
1344 ((u32) scp->cmnd[7] << 8);
1345 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1346 ((u32) scp->cmnd[3] << 16) |
1347 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1351 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1353 else if (scp->cmd_len == 12) {
1354 io_request->DataLength = ((u32) scp->cmnd[6] << 24) |
1355 ((u32) scp->cmnd[7] << 16) |
1356 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1357 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1358 ((u32) scp->cmnd[3] << 16) |
1359 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1363 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1365 else if (scp->cmd_len == 16) {
1366 io_request->DataLength = ((u32) scp->cmnd[10] << 24) |
1367 ((u32) scp->cmnd[11] << 16) |
1368 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1369 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1370 ((u32) scp->cmnd[7] << 16) |
1371 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1373 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1374 ((u32) scp->cmnd[3] << 16) |
1375 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1378 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1379 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
1380 io_info.numBlocks = io_request->DataLength;
1381 io_info.ldTgtId = device_id;
1383 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1386 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1388 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1389 MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) {
1390 io_request->RaidContext.regLockFlags = 0;
1393 if (MR_BuildRaidContext(&io_info, &io_request->RaidContext,
1395 fp_possible = io_info.fpOkForIo;
1399 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1400 local_map_ptr, start_lba_lo);
1401 io_request->DataLength = scsi_bufflen(scp);
1402 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1403 cmd->request_desc->SCSIIO.RequestFlags =
1404 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1405 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1406 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1409 get_updated_dev_handle(
1410 &fusion->load_balance_info[device_id],
1412 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1414 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1415 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1416 io_request->DevHandle = io_info.devHandle;
1418 io_request->RaidContext.timeoutValue =
1419 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1420 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1421 io_request->DevHandle = device_id;
1422 cmd->request_desc->SCSIIO.RequestFlags =
1423 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1424 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1429 * megasas_build_dcdb_fusion - Prepares IOs to devices
1430 * @instance: Adapter soft state
1431 * @scp: SCSI command
1432 * @cmd: Command to be prepared
1434 * Prepares the io_request frame for non-io cmds
1437 megasas_build_dcdb_fusion(struct megasas_instance *instance,
1438 struct scsi_cmnd *scmd,
1439 struct megasas_cmd_fusion *cmd)
1442 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1444 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1445 struct fusion_context *fusion = instance->ctrl_context;
1447 io_request = cmd->io_request;
1448 device_id = MEGASAS_DEV_INDEX(instance, scmd);
1449 pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1451 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1453 /* Check if this is a system PD I/O */
1454 if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1455 io_request->Function = 0;
1456 io_request->DevHandle =
1457 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1458 io_request->RaidContext.timeoutValue =
1459 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1460 io_request->RaidContext.regLockFlags = 0;
1461 io_request->RaidContext.regLockRowLBA = 0;
1462 io_request->RaidContext.regLockLength = 0;
1463 io_request->RaidContext.RAIDFlags =
1464 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1465 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1466 cmd->request_desc->SCSIIO.RequestFlags =
1467 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1468 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1470 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1471 io_request->DevHandle = device_id;
1472 cmd->request_desc->SCSIIO.RequestFlags =
1473 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1474 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1476 io_request->RaidContext.VirtualDiskTgtId = device_id;
1477 io_request->LUN[1] = scmd->device->lun;
1478 io_request->DataLength = scsi_bufflen(scmd);
1482 * megasas_build_io_fusion - Prepares IOs to devices
1483 * @instance: Adapter soft state
1484 * @scp: SCSI command
1485 * @cmd: Command to be prepared
1487 * Invokes helper functions to prepare request frames
1488 * and sets flags appropriate for IO/Non-IO cmd
1491 megasas_build_io_fusion(struct megasas_instance *instance,
1492 struct scsi_cmnd *scp,
1493 struct megasas_cmd_fusion *cmd)
1495 u32 device_id, sge_count;
1496 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1498 device_id = MEGASAS_DEV_INDEX(instance, scp);
1500 /* Zero out some fields so they don't get reused */
1501 io_request->LUN[1] = 0;
1502 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1503 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1504 io_request->EEDPFlags = 0;
1505 io_request->Control = 0;
1506 io_request->EEDPBlockSize = 0;
1507 io_request->IoFlags = 0;
1508 io_request->RaidContext.RAIDFlags = 0;
1510 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1512 * Just the CDB length,rest of the Flags are zero
1513 * This will be modified for FP in build_ldio_fusion
1515 io_request->IoFlags = scp->cmd_len;
1517 if (megasas_is_ldio(scp))
1518 megasas_build_ldio_fusion(instance, scp, cmd);
1520 megasas_build_dcdb_fusion(instance, scp, cmd);
1527 megasas_make_sgl_fusion(instance, scp,
1528 (struct MPI25_IEEE_SGE_CHAIN64 *)
1529 &io_request->SGL, cmd);
1531 if (sge_count > instance->max_num_sge) {
1532 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1533 "max (0x%x) allowed\n", sge_count,
1534 instance->max_num_sge);
1538 io_request->RaidContext.numSGE = sge_count;
1540 io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
1542 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1543 io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
1544 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1545 io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
1547 io_request->SGLOffset0 =
1548 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1550 io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
1551 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1554 scp->SCp.ptr = (char *)cmd;
1559 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1560 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1563 struct fusion_context *fusion;
1565 if (index >= instance->max_fw_cmds) {
1566 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
1567 "descriptor\n", index);
1570 fusion = instance->ctrl_context;
1571 p = fusion->req_frames_desc
1572 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1574 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1578 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1579 * issuing non IOCTL cmd
1580 * @instance: Adapter soft state
1581 * @scmd: pointer to scsi cmd from OS
1584 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1585 struct scsi_cmnd *scmd)
1587 struct megasas_cmd_fusion *cmd;
1588 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1590 struct fusion_context *fusion;
1592 fusion = instance->ctrl_context;
1594 cmd = megasas_get_cmd_fusion(instance);
1596 return SCSI_MLQUEUE_HOST_BUSY;
1600 req_desc = megasas_get_request_descriptor(instance, index-1);
1604 req_desc->Words = 0;
1605 cmd->request_desc = req_desc;
1607 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1608 megasas_return_cmd_fusion(instance, cmd);
1609 printk(KERN_ERR "megasas: Error building command.\n");
1610 cmd->request_desc = NULL;
1614 req_desc = cmd->request_desc;
1615 req_desc->SCSIIO.SMID = index;
1617 if (cmd->io_request->ChainOffset != 0 &&
1618 cmd->io_request->ChainOffset != 0xF)
1619 printk(KERN_ERR "megasas: The chain offset value is not "
1620 "correct : %x\n", cmd->io_request->ChainOffset);
1623 * Issue the command to the FW
1625 atomic_inc(&instance->fw_outstanding);
1627 instance->instancet->fire_cmd(instance,
1628 req_desc->u.low, req_desc->u.high,
1635 * complete_cmd_fusion - Completes command
1636 * @instance: Adapter soft state
1637 * Completes all commands that is in reply descriptor queue
1640 complete_cmd_fusion(struct megasas_instance *instance)
1642 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1643 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1644 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1645 struct fusion_context *fusion;
1646 struct megasas_cmd *cmd_mfi;
1647 struct megasas_cmd_fusion *cmd_fusion;
1648 u16 smid, num_completed;
1649 u8 reply_descript_type, arm;
1650 u32 status, extStatus, device_id;
1651 union desc_value d_val;
1652 struct LD_LOAD_BALANCE_INFO *lbinfo;
1654 fusion = instance->ctrl_context;
1656 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1659 desc = fusion->reply_frames_desc;
1660 desc += fusion->last_reply_idx;
1662 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1664 d_val.word = desc->Words;
1666 reply_descript_type = reply_desc->ReplyFlags &
1667 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1669 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1672 d_val.word = desc->Words;
1676 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
1677 smid = reply_desc->SMID;
1679 cmd_fusion = fusion->cmd_list[smid - 1];
1682 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1683 cmd_fusion->io_request;
1685 if (cmd_fusion->scmd)
1686 cmd_fusion->scmd->SCp.ptr = NULL;
1688 status = scsi_io_req->RaidContext.status;
1689 extStatus = scsi_io_req->RaidContext.exStatus;
1691 switch (scsi_io_req->Function) {
1692 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
1693 /* Update load balancing info */
1694 device_id = MEGASAS_DEV_INDEX(instance,
1696 lbinfo = &fusion->load_balance_info[device_id];
1697 if (cmd_fusion->scmd->SCp.Status &
1698 MEGASAS_LOAD_BALANCE_FLAG) {
1699 arm = lbinfo->raid1DevHandle[0] ==
1700 cmd_fusion->io_request->DevHandle ? 0 :
1702 atomic_dec(&lbinfo->scsi_pending_cmds[arm]);
1703 cmd_fusion->scmd->SCp.Status &=
1704 ~MEGASAS_LOAD_BALANCE_FLAG;
1706 if (reply_descript_type ==
1707 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
1708 if (megasas_dbg_lvl == 5)
1709 printk(KERN_ERR "\nmegasas: FAST Path "
1712 /* Fall thru and complete IO */
1713 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
1714 /* Map the FW Cmd Status */
1715 map_cmd_status(cmd_fusion, status, extStatus);
1716 scsi_dma_unmap(cmd_fusion->scmd);
1717 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
1718 scsi_io_req->RaidContext.status = 0;
1719 scsi_io_req->RaidContext.exStatus = 0;
1720 megasas_return_cmd_fusion(instance, cmd_fusion);
1721 atomic_dec(&instance->fw_outstanding);
1724 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
1725 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1726 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1727 cmd_fusion->flags = 0;
1728 megasas_return_cmd_fusion(instance, cmd_fusion);
1733 fusion->last_reply_idx++;
1734 if (fusion->last_reply_idx >= fusion->reply_q_depth)
1735 fusion->last_reply_idx = 0;
1737 desc->Words = ULLONG_MAX;
1740 /* Get the next reply descriptor */
1741 if (!fusion->last_reply_idx)
1742 desc = fusion->reply_frames_desc;
1747 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1749 d_val.word = desc->Words;
1751 reply_descript_type = reply_desc->ReplyFlags &
1752 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1754 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1762 writel(fusion->last_reply_idx,
1763 &instance->reg_set->reply_post_host_index);
1764 megasas_check_and_restore_queue_depth(instance);
1769 * megasas_complete_cmd_dpc_fusion - Completes command
1770 * @instance: Adapter soft state
1772 * Tasklet to complete cmds
1775 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
1777 struct megasas_instance *instance =
1778 (struct megasas_instance *)instance_addr;
1779 unsigned long flags;
1781 /* If we have already declared adapter dead, donot complete cmds */
1782 spin_lock_irqsave(&instance->hba_lock, flags);
1783 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1784 spin_unlock_irqrestore(&instance->hba_lock, flags);
1787 spin_unlock_irqrestore(&instance->hba_lock, flags);
1789 spin_lock_irqsave(&instance->completion_lock, flags);
1790 complete_cmd_fusion(instance);
1791 spin_unlock_irqrestore(&instance->completion_lock, flags);
1795 * megasas_isr_fusion - isr entry point
1797 irqreturn_t megasas_isr_fusion(int irq, void *devp)
1799 struct megasas_instance *instance = (struct megasas_instance *)devp;
1800 u32 mfiStatus, fw_state;
1802 if (!instance->msi_flag) {
1803 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
1808 /* If we are resetting, bail */
1809 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
1812 if (!complete_cmd_fusion(instance)) {
1813 /* If we didn't complete any commands, check for FW fault */
1814 fw_state = instance->instancet->read_fw_status_reg(
1815 instance->reg_set) & MFI_STATE_MASK;
1816 if (fw_state == MFI_STATE_FAULT)
1817 schedule_work(&instance->work_init);
1824 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
1825 * @instance: Adapter soft state
1826 * mfi_cmd: megasas_cmd pointer
1830 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
1831 struct megasas_cmd *mfi_cmd)
1833 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
1834 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
1835 struct megasas_cmd_fusion *cmd;
1836 struct fusion_context *fusion;
1837 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
1839 cmd = megasas_get_cmd_fusion(instance);
1843 /* Save the smid. To be used for returning the cmd */
1844 mfi_cmd->context.smid = cmd->index;
1846 cmd->sync_cmd_idx = mfi_cmd->index;
1849 * For cmds where the flag is set, store the flag and check
1850 * on completion. For cmds with this flag, don't call
1851 * megasas_complete_cmd
1854 if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)
1855 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
1857 fusion = instance->ctrl_context;
1858 io_req = cmd->io_request;
1860 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
1862 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
1863 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
1865 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
1867 mpi25_ieee_chain->Address = mfi_cmd->frame_phys_addr;
1869 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1870 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
1872 mpi25_ieee_chain->Length = MEGASAS_MAX_SZ_CHAIN_FRAME;
1878 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
1879 * @instance: Adapter soft state
1880 * @cmd: mfi cmd to build
1883 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1884 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
1886 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1889 if (build_mpt_mfi_pass_thru(instance, cmd)) {
1890 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
1894 index = cmd->context.smid;
1896 req_desc = megasas_get_request_descriptor(instance, index - 1);
1901 req_desc->Words = 0;
1902 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1903 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1905 req_desc->SCSIIO.SMID = index;
1911 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
1912 * @instance: Adapter soft state
1913 * @cmd: mfi cmd pointer
1917 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
1918 struct megasas_cmd *cmd)
1920 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1922 req_desc = build_mpt_cmd(instance, cmd);
1924 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
1927 instance->instancet->fire_cmd(instance, req_desc->u.low,
1928 req_desc->u.high, instance->reg_set);
1932 * megasas_release_fusion - Reverses the FW initialization
1933 * @intance: Adapter soft state
1936 megasas_release_fusion(struct megasas_instance *instance)
1938 megasas_free_cmds(instance);
1939 megasas_free_cmds_fusion(instance);
1941 iounmap(instance->reg_set);
1943 pci_release_selected_regions(instance->pdev, instance->bar);
1947 * megasas_read_fw_status_reg_fusion - returns the current FW status value
1948 * @regs: MFI register set
1951 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
1953 return readl(&(regs)->outbound_scratch_pad);
1957 * megasas_adp_reset_fusion - For controller reset
1958 * @regs: MFI register set
1961 megasas_adp_reset_fusion(struct megasas_instance *instance,
1962 struct megasas_register_set __iomem *regs)
1968 * megasas_check_reset_fusion - For controller reset check
1969 * @regs: MFI register set
1972 megasas_check_reset_fusion(struct megasas_instance *instance,
1973 struct megasas_register_set __iomem *regs)
1978 /* This function waits for outstanding commands on fusion to complete */
1979 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance)
1981 int i, outstanding, retval = 0;
1982 u32 fw_state, wait_time = MEGASAS_RESET_WAIT_TIME;
1984 for (i = 0; i < wait_time; i++) {
1985 /* Check if firmware is in fault state */
1986 fw_state = instance->instancet->read_fw_status_reg(
1987 instance->reg_set) & MFI_STATE_MASK;
1988 if (fw_state == MFI_STATE_FAULT) {
1989 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
1990 " will reset adapter.\n");
1995 outstanding = atomic_read(&instance->fw_outstanding);
1999 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2000 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2001 "commands to complete\n", i, outstanding);
2002 megasas_complete_cmd_dpc_fusion(
2003 (unsigned long)instance);
2008 if (atomic_read(&instance->fw_outstanding)) {
2009 printk("megaraid_sas: pending commands remain after waiting, "
2010 "will reset adapter.\n");
2017 void megasas_reset_reply_desc(struct megasas_instance *instance)
2020 struct fusion_context *fusion;
2021 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2023 fusion = instance->ctrl_context;
2024 fusion->last_reply_idx = 0;
2025 reply_desc = fusion->reply_frames_desc;
2026 for (i = 0 ; i < fusion->reply_q_depth; i++, reply_desc++)
2027 reply_desc->Words = ULLONG_MAX;
2030 /* Core fusion reset function */
2031 int megasas_reset_fusion(struct Scsi_Host *shost)
2033 int retval = SUCCESS, i, j, retry = 0;
2034 struct megasas_instance *instance;
2035 struct megasas_cmd_fusion *cmd_fusion;
2036 struct fusion_context *fusion;
2037 struct megasas_cmd *cmd_mfi;
2038 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2039 u32 host_diag, abs_state, status_reg, reset_adapter;
2041 instance = (struct megasas_instance *)shost->hostdata;
2042 fusion = instance->ctrl_context;
2044 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2045 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
2046 "returning FAILED.\n");
2050 mutex_lock(&instance->reset_mutex);
2051 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2052 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2053 instance->instancet->disable_intr(instance->reg_set);
2056 /* First try waiting for commands to complete */
2057 if (megasas_wait_for_outstanding_fusion(instance)) {
2058 printk(KERN_WARNING "megaraid_sas: resetting fusion "
2060 /* Now return commands back to the OS */
2061 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2062 cmd_fusion = fusion->cmd_list[i];
2063 if (cmd_fusion->scmd) {
2064 scsi_dma_unmap(cmd_fusion->scmd);
2065 cmd_fusion->scmd->result = (DID_RESET << 16);
2066 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2067 megasas_return_cmd_fusion(instance, cmd_fusion);
2068 atomic_dec(&instance->fw_outstanding);
2072 status_reg = instance->instancet->read_fw_status_reg(
2074 abs_state = status_reg & MFI_STATE_MASK;
2075 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2076 if (instance->disableOnlineCtrlReset ||
2077 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
2078 /* Reset not supported, kill adapter */
2079 printk(KERN_WARNING "megaraid_sas: Reset not supported"
2080 ", killing adapter.\n");
2081 megaraid_sas_kill_hba(instance);
2082 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2087 /* Now try to reset the chip */
2088 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2089 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2090 &instance->reg_set->fusion_seq_offset);
2091 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2092 &instance->reg_set->fusion_seq_offset);
2093 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2094 &instance->reg_set->fusion_seq_offset);
2095 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2096 &instance->reg_set->fusion_seq_offset);
2097 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2098 &instance->reg_set->fusion_seq_offset);
2099 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2100 &instance->reg_set->fusion_seq_offset);
2101 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2102 &instance->reg_set->fusion_seq_offset);
2104 /* Check that the diag write enable (DRWE) bit is on */
2105 host_diag = readl(&instance->reg_set->fusion_host_diag);
2107 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2110 readl(&instance->reg_set->fusion_host_diag);
2111 if (retry++ == 100) {
2112 printk(KERN_WARNING "megaraid_sas: "
2113 "Host diag unlock failed!\n");
2117 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2120 /* Send chip reset command */
2121 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2122 &instance->reg_set->fusion_host_diag);
2125 /* Make sure reset adapter bit is cleared */
2126 host_diag = readl(&instance->reg_set->fusion_host_diag);
2128 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2131 readl(&instance->reg_set->fusion_host_diag);
2132 if (retry++ == 1000) {
2133 printk(KERN_WARNING "megaraid_sas: "
2134 "Diag reset adapter never "
2139 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2143 instance->instancet->read_fw_status_reg(
2144 instance->reg_set) & MFI_STATE_MASK;
2147 while ((abs_state <= MFI_STATE_FW_INIT) &&
2151 instance->instancet->read_fw_status_reg(
2152 instance->reg_set) & MFI_STATE_MASK;
2154 if (abs_state <= MFI_STATE_FW_INIT) {
2155 printk(KERN_WARNING "megaraid_sas: firmware "
2156 "state < MFI_STATE_FW_INIT, state = "
2157 "0x%x\n", abs_state);
2161 /* Wait for FW to become ready */
2162 if (megasas_transition_to_ready(instance, 1)) {
2163 printk(KERN_WARNING "megaraid_sas: Failed to "
2164 "transition controller to ready.\n");
2168 megasas_reset_reply_desc(instance);
2169 if (megasas_ioc_init_fusion(instance)) {
2170 printk(KERN_WARNING "megaraid_sas: "
2171 "megasas_ioc_init_fusion() failed!\n");
2175 instance->instancet->enable_intr(instance->reg_set);
2176 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2178 /* Re-fire management commands */
2179 for (j = 0 ; j < instance->max_fw_cmds; j++) {
2180 cmd_fusion = fusion->cmd_list[j];
2181 if (cmd_fusion->sync_cmd_idx !=
2185 cmd_list[cmd_fusion->sync_cmd_idx];
2186 if (cmd_mfi->frame->dcmd.opcode ==
2187 MR_DCMD_LD_MAP_GET_INFO) {
2188 megasas_return_cmd(instance,
2190 megasas_return_cmd_fusion(
2191 instance, cmd_fusion);
2194 megasas_get_request_descriptor(
2196 cmd_mfi->context.smid
2203 instance->instancet->
2216 /* Reset load balance info */
2217 memset(fusion->load_balance_info, 0,
2218 sizeof(struct LD_LOAD_BALANCE_INFO)
2219 *MAX_LOGICAL_DRIVES);
2221 if (!megasas_get_map_info(instance))
2222 megasas_sync_map_info(instance);
2224 /* Adapter reset completed successfully */
2225 printk(KERN_WARNING "megaraid_sas: Reset "
2230 /* Reset failed, kill the adapter */
2231 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
2233 megaraid_sas_kill_hba(instance);
2236 instance->instancet->enable_intr(instance->reg_set);
2237 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2240 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2241 mutex_unlock(&instance->reset_mutex);
2245 /* Fusion OCR work queue */
2246 void megasas_fusion_ocr_wq(struct work_struct *work)
2248 struct megasas_instance *instance =
2249 container_of(work, struct megasas_instance, work_init);
2251 megasas_reset_fusion(instance->host);
2254 struct megasas_instance_template megasas_instance_template_fusion = {
2255 .fire_cmd = megasas_fire_cmd_fusion,
2256 .enable_intr = megasas_enable_intr_fusion,
2257 .disable_intr = megasas_disable_intr_fusion,
2258 .clear_intr = megasas_clear_intr_fusion,
2259 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2260 .adp_reset = megasas_adp_reset_fusion,
2261 .check_reset = megasas_check_reset_fusion,
2262 .service_isr = megasas_isr_fusion,
2263 .tasklet = megasas_complete_cmd_dpc_fusion,
2264 .init_adapter = megasas_init_adapter_fusion,
2265 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2266 .issue_dcmd = megasas_issue_dcmd_fusion,