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 megasas_instance *instance,
78 struct IO_REQUEST_INFO *io_info,
79 struct RAID_CONTEXT *pRAID_Context,
80 struct MR_FW_RAID_MAP_ALL *map);
81 u16 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_FW_RAID_MAP_ALL *map);
82 struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
84 u16 MR_GetLDTgtId(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
87 megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
89 u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
90 struct LD_LOAD_BALANCE_INFO *lbInfo);
91 u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
92 struct IO_REQUEST_INFO *in_info);
93 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
94 void megaraid_sas_kill_hba(struct megasas_instance *instance);
96 extern u32 megasas_dbg_lvl;
99 * megasas_enable_intr_fusion - Enables interrupts
100 * @regs: MFI register set
103 megasas_enable_intr_fusion(struct megasas_register_set __iomem *regs)
105 /* For Thunderbolt/Invader also clear intr on enable */
106 writel(~0, ®s->outbound_intr_status);
107 readl(®s->outbound_intr_status);
109 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
111 /* Dummy readl to force pci flush */
112 readl(®s->outbound_intr_mask);
116 * megasas_disable_intr_fusion - Disables interrupt
117 * @regs: MFI register set
120 megasas_disable_intr_fusion(struct megasas_register_set __iomem *regs)
122 u32 mask = 0xFFFFFFFF;
125 writel(mask, ®s->outbound_intr_mask);
126 /* Dummy readl to force pci flush */
127 status = readl(®s->outbound_intr_mask);
131 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
135 * Check if it is our interrupt
137 status = readl(®s->outbound_intr_status);
140 writel(status, ®s->outbound_intr_status);
141 readl(®s->outbound_intr_status);
144 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
151 * megasas_get_cmd_fusion - Get a command from the free pool
152 * @instance: Adapter soft state
154 * Returns a free command from the pool
156 struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
160 struct fusion_context *fusion =
161 (struct fusion_context *)instance->ctrl_context;
162 struct megasas_cmd_fusion *cmd = NULL;
164 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
166 if (!list_empty(&fusion->cmd_pool)) {
167 cmd = list_entry((&fusion->cmd_pool)->next,
168 struct megasas_cmd_fusion, list);
169 list_del_init(&cmd->list);
171 printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
174 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
179 * megasas_return_cmd_fusion - Return a cmd to free command pool
180 * @instance: Adapter soft state
181 * @cmd: Command packet to be returned to free command pool
184 megasas_return_cmd_fusion(struct megasas_instance *instance,
185 struct megasas_cmd_fusion *cmd)
188 struct fusion_context *fusion =
189 (struct fusion_context *)instance->ctrl_context;
191 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
194 cmd->sync_cmd_idx = (u32)ULONG_MAX;
195 list_add_tail(&cmd->list, &fusion->cmd_pool);
197 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
201 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
202 * @instance: Adapter soft state
204 static void megasas_teardown_frame_pool_fusion(
205 struct megasas_instance *instance)
208 struct fusion_context *fusion = instance->ctrl_context;
210 u16 max_cmd = instance->max_fw_cmds;
212 struct megasas_cmd_fusion *cmd;
214 if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
215 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
216 "sense pool : %p\n", fusion->sg_dma_pool,
217 fusion->sense_dma_pool);
222 * Return all frames to pool
224 for (i = 0; i < max_cmd; i++) {
226 cmd = fusion->cmd_list[i];
229 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
230 cmd->sg_frame_phys_addr);
233 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
234 cmd->sense_phys_addr);
238 * Now destroy the pool itself
240 pci_pool_destroy(fusion->sg_dma_pool);
241 pci_pool_destroy(fusion->sense_dma_pool);
243 fusion->sg_dma_pool = NULL;
244 fusion->sense_dma_pool = NULL;
248 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
249 * @instance: Adapter soft state
252 megasas_free_cmds_fusion(struct megasas_instance *instance)
255 struct fusion_context *fusion = instance->ctrl_context;
257 u32 max_cmds, req_sz, reply_sz, io_frames_sz;
260 req_sz = fusion->request_alloc_sz;
261 reply_sz = fusion->reply_alloc_sz;
262 io_frames_sz = fusion->io_frames_alloc_sz;
264 max_cmds = instance->max_fw_cmds;
266 /* Free descriptors and request Frames memory */
267 if (fusion->req_frames_desc)
268 dma_free_coherent(&instance->pdev->dev, req_sz,
269 fusion->req_frames_desc,
270 fusion->req_frames_desc_phys);
272 if (fusion->reply_frames_desc) {
273 pci_pool_free(fusion->reply_frames_desc_pool,
274 fusion->reply_frames_desc,
275 fusion->reply_frames_desc_phys);
276 pci_pool_destroy(fusion->reply_frames_desc_pool);
279 if (fusion->io_request_frames) {
280 pci_pool_free(fusion->io_request_frames_pool,
281 fusion->io_request_frames,
282 fusion->io_request_frames_phys);
283 pci_pool_destroy(fusion->io_request_frames_pool);
286 /* Free the Fusion frame pool */
287 megasas_teardown_frame_pool_fusion(instance);
289 /* Free all the commands in the cmd_list */
290 for (i = 0; i < max_cmds; i++)
291 kfree(fusion->cmd_list[i]);
293 /* Free the cmd_list buffer itself */
294 kfree(fusion->cmd_list);
295 fusion->cmd_list = NULL;
297 INIT_LIST_HEAD(&fusion->cmd_pool);
301 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
302 * @instance: Adapter soft state
305 static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
309 struct fusion_context *fusion;
310 struct megasas_cmd_fusion *cmd;
311 u32 total_sz_chain_frame;
313 fusion = instance->ctrl_context;
314 max_cmd = instance->max_fw_cmds;
316 total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
319 * Use DMA pool facility provided by PCI layer
322 fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
324 total_sz_chain_frame, 4,
326 if (!fusion->sg_dma_pool) {
327 printk(KERN_DEBUG "megasas: failed to setup request pool "
331 fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
333 SCSI_SENSE_BUFFERSIZE, 64, 0);
335 if (!fusion->sense_dma_pool) {
336 printk(KERN_DEBUG "megasas: failed to setup sense pool "
338 pci_pool_destroy(fusion->sg_dma_pool);
339 fusion->sg_dma_pool = NULL;
344 * Allocate and attach a frame to each of the commands in cmd_list
346 for (i = 0; i < max_cmd; i++) {
348 cmd = fusion->cmd_list[i];
350 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
352 &cmd->sg_frame_phys_addr);
354 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
355 GFP_KERNEL, &cmd->sense_phys_addr);
357 * megasas_teardown_frame_pool_fusion() takes care of freeing
358 * whatever has been allocated
360 if (!cmd->sg_frame || !cmd->sense) {
361 printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
362 megasas_teardown_frame_pool_fusion(instance);
370 * megasas_alloc_cmds_fusion - Allocates the command packets
371 * @instance: Adapter soft state
374 * Each frame has a 32-bit field called context. This context is used to get
375 * back the megasas_cmd_fusion from the frame when a frame gets completed
376 * In this driver, the 32 bit values are the indices into an array cmd_list.
377 * This array is used only to look up the megasas_cmd_fusion given the context.
378 * The free commands themselves are maintained in a linked list called cmd_pool.
380 * cmds are formed in the io_request and sg_frame members of the
381 * megasas_cmd_fusion. The context field is used to get a request descriptor
382 * and is used as SMID of the cmd.
383 * SMID value range is from 1 to max_fw_cmds.
386 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
389 u32 max_cmd, io_frames_sz;
390 struct fusion_context *fusion;
391 struct megasas_cmd_fusion *cmd;
392 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
394 dma_addr_t io_req_base_phys;
397 fusion = instance->ctrl_context;
399 max_cmd = instance->max_fw_cmds;
401 fusion->req_frames_desc =
402 dma_alloc_coherent(&instance->pdev->dev,
403 fusion->request_alloc_sz,
404 &fusion->req_frames_desc_phys, GFP_KERNEL);
406 if (!fusion->req_frames_desc) {
407 printk(KERN_ERR "megasas; Could not allocate memory for "
412 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
413 fusion->reply_frames_desc_pool =
414 pci_pool_create("reply_frames pool", instance->pdev,
415 fusion->reply_alloc_sz * count, 16, 0);
417 if (!fusion->reply_frames_desc_pool) {
418 printk(KERN_ERR "megasas; Could not allocate memory for "
419 "reply_frame pool\n");
420 goto fail_reply_desc;
423 fusion->reply_frames_desc =
424 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
425 &fusion->reply_frames_desc_phys);
426 if (!fusion->reply_frames_desc) {
427 printk(KERN_ERR "megasas; Could not allocate memory for "
428 "reply_frame pool\n");
429 pci_pool_destroy(fusion->reply_frames_desc_pool);
430 goto fail_reply_desc;
433 reply_desc = fusion->reply_frames_desc;
434 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
435 reply_desc->Words = ULLONG_MAX;
437 io_frames_sz = fusion->io_frames_alloc_sz;
439 fusion->io_request_frames_pool =
440 pci_pool_create("io_request_frames pool", instance->pdev,
441 fusion->io_frames_alloc_sz, 16, 0);
443 if (!fusion->io_request_frames_pool) {
444 printk(KERN_ERR "megasas: Could not allocate memory for "
445 "io_request_frame pool\n");
449 fusion->io_request_frames =
450 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
451 &fusion->io_request_frames_phys);
452 if (!fusion->io_request_frames) {
453 printk(KERN_ERR "megasas: Could not allocate memory for "
454 "io_request_frames frames\n");
455 pci_pool_destroy(fusion->io_request_frames_pool);
460 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
461 * Allocate the dynamic array first and then allocate individual
464 fusion->cmd_list = kmalloc(sizeof(struct megasas_cmd_fusion *)
465 *max_cmd, GFP_KERNEL);
467 if (!fusion->cmd_list) {
468 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
469 "memory for cmd_list_fusion\n");
473 memset(fusion->cmd_list, 0, sizeof(struct megasas_cmd_fusion *)
476 max_cmd = instance->max_fw_cmds;
477 for (i = 0; i < max_cmd; i++) {
478 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
480 if (!fusion->cmd_list[i]) {
481 printk(KERN_ERR "Could not alloc cmd list fusion\n");
483 for (j = 0; j < i; j++)
484 kfree(fusion->cmd_list[j]);
486 kfree(fusion->cmd_list);
487 fusion->cmd_list = NULL;
492 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
493 io_req_base = fusion->io_request_frames +
494 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
495 io_req_base_phys = fusion->io_request_frames_phys +
496 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
499 * Add all the commands to command pool (fusion->cmd_pool)
502 /* SMID 0 is reserved. Set SMID/index from 1 */
503 for (i = 0; i < max_cmd; i++) {
504 cmd = fusion->cmd_list[i];
505 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
506 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
509 cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */
510 cmd->instance = instance;
512 (struct MPI2_RAID_SCSI_IO_REQUEST *)
513 (io_req_base + offset);
514 memset(cmd->io_request, 0,
515 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
516 cmd->io_request_phys_addr = io_req_base_phys + offset;
518 list_add_tail(&cmd->list, &fusion->cmd_pool);
522 * Create a frame pool and assign one frame to each cmd
524 if (megasas_create_frame_pool_fusion(instance)) {
525 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
526 megasas_free_cmds_fusion(instance);
533 pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
534 fusion->io_request_frames_phys);
535 pci_pool_destroy(fusion->io_request_frames_pool);
537 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
538 fusion->reply_frames_desc,
539 fusion->reply_frames_desc_phys);
540 pci_pool_free(fusion->reply_frames_desc_pool,
541 fusion->reply_frames_desc,
542 fusion->reply_frames_desc_phys);
543 pci_pool_destroy(fusion->reply_frames_desc_pool);
546 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
547 fusion->req_frames_desc,
548 fusion->req_frames_desc_phys);
554 * wait_and_poll - Issues a polling command
555 * @instance: Adapter soft state
556 * @cmd: Command packet to be issued
558 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
561 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd)
564 struct megasas_header *frame_hdr = &cmd->frame->hdr;
566 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
569 * Wait for cmd_status to change
571 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
576 if (frame_hdr->cmd_status == 0xff)
583 * megasas_ioc_init_fusion - Initializes the FW
584 * @instance: Adapter soft state
586 * Issues the IOC Init cmd
589 megasas_ioc_init_fusion(struct megasas_instance *instance)
591 struct megasas_init_frame *init_frame;
592 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
593 dma_addr_t ioc_init_handle;
594 struct megasas_cmd *cmd;
596 struct fusion_context *fusion;
597 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
599 struct megasas_header *frame_hdr;
601 fusion = instance->ctrl_context;
603 cmd = megasas_get_cmd(instance);
606 printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
612 dma_alloc_coherent(&instance->pdev->dev,
613 sizeof(struct MPI2_IOC_INIT_REQUEST),
614 &ioc_init_handle, GFP_KERNEL);
616 if (!IOCInitMessage) {
617 printk(KERN_ERR "Could not allocate memory for "
623 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
625 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
626 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
627 IOCInitMessage->MsgVersion = MPI2_VERSION;
628 IOCInitMessage->HeaderVersion = MPI2_HEADER_VERSION;
629 IOCInitMessage->SystemRequestFrameSize =
630 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4;
632 IOCInitMessage->ReplyDescriptorPostQueueDepth = fusion->reply_q_depth;
633 IOCInitMessage->ReplyDescriptorPostQueueAddress =
634 fusion->reply_frames_desc_phys;
635 IOCInitMessage->SystemRequestFrameBaseAddress =
636 fusion->io_request_frames_phys;
637 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
638 init_frame = (struct megasas_init_frame *)cmd->frame;
639 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
641 frame_hdr = &cmd->frame->hdr;
642 frame_hdr->cmd_status = 0xFF;
643 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
645 init_frame->cmd = MFI_CMD_INIT;
646 init_frame->cmd_status = 0xFF;
648 init_frame->queue_info_new_phys_addr_lo = ioc_init_handle;
649 init_frame->data_xfer_len = sizeof(struct MPI2_IOC_INIT_REQUEST);
652 (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)fusion->req_frames_desc;
654 req_desc->Words = cmd->frame_phys_addr;
655 req_desc->MFAIo.RequestFlags =
656 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
657 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
660 * disable the intr before firing the init frame
662 instance->instancet->disable_intr(instance->reg_set);
664 for (i = 0; i < (10 * 1000); i += 20) {
665 if (readl(&instance->reg_set->doorbell) & 1)
671 instance->instancet->fire_cmd(instance, req_desc->u.low,
672 req_desc->u.high, instance->reg_set);
674 wait_and_poll(instance, cmd);
676 frame_hdr = &cmd->frame->hdr;
677 if (frame_hdr->cmd_status != 0) {
681 printk(KERN_ERR "megasas:IOC Init cmd success\n");
686 megasas_return_cmd(instance, cmd);
688 dma_free_coherent(&instance->pdev->dev,
689 sizeof(struct MPI2_IOC_INIT_REQUEST),
690 IOCInitMessage, ioc_init_handle);
696 * megasas_get_ld_map_info - Returns FW's ld_map structure
697 * @instance: Adapter soft state
698 * @pend: Pend the command or not
699 * Issues an internal command (DCMD) to get the FW's controller PD
700 * list structure. This information is mainly used to find out SYSTEM
701 * supported by the FW.
704 megasas_get_ld_map_info(struct megasas_instance *instance)
707 struct megasas_cmd *cmd;
708 struct megasas_dcmd_frame *dcmd;
709 struct MR_FW_RAID_MAP_ALL *ci;
712 struct fusion_context *fusion;
714 cmd = megasas_get_cmd(instance);
717 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
721 fusion = instance->ctrl_context;
724 megasas_return_cmd(instance, cmd);
728 dcmd = &cmd->frame->dcmd;
730 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
731 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
733 ci = fusion->ld_map[(instance->map_id & 1)];
734 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
737 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
738 megasas_return_cmd(instance, cmd);
742 memset(ci, 0, sizeof(*ci));
743 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
745 dcmd->cmd = MFI_CMD_DCMD;
746 dcmd->cmd_status = 0xFF;
748 dcmd->flags = MFI_FRAME_DIR_READ;
751 dcmd->data_xfer_len = size_map_info;
752 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
753 dcmd->sgl.sge32[0].phys_addr = ci_h;
754 dcmd->sgl.sge32[0].length = size_map_info;
756 if (!megasas_issue_polled(instance, cmd))
759 printk(KERN_ERR "megasas: Get LD Map Info Failed\n");
763 megasas_return_cmd(instance, cmd);
769 megasas_get_map_info(struct megasas_instance *instance)
771 struct fusion_context *fusion = instance->ctrl_context;
773 fusion->fast_path_io = 0;
774 if (!megasas_get_ld_map_info(instance)) {
775 if (MR_ValidateMapInfo(fusion->ld_map[(instance->map_id & 1)],
776 fusion->load_balance_info)) {
777 fusion->fast_path_io = 1;
785 * megasas_sync_map_info - Returns FW's ld_map structure
786 * @instance: Adapter soft state
788 * Issues an internal command (DCMD) to get the FW's controller PD
789 * list structure. This information is mainly used to find out SYSTEM
790 * supported by the FW.
793 megasas_sync_map_info(struct megasas_instance *instance)
796 struct megasas_cmd *cmd;
797 struct megasas_dcmd_frame *dcmd;
798 u32 size_sync_info, num_lds;
799 struct fusion_context *fusion;
800 struct MR_LD_TARGET_SYNC *ci = NULL;
801 struct MR_FW_RAID_MAP_ALL *map;
802 struct MR_LD_RAID *raid;
803 struct MR_LD_TARGET_SYNC *ld_sync;
807 cmd = megasas_get_cmd(instance);
810 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
815 fusion = instance->ctrl_context;
818 megasas_return_cmd(instance, cmd);
822 map = fusion->ld_map[instance->map_id & 1];
824 num_lds = map->raidMap.ldCount;
826 dcmd = &cmd->frame->dcmd;
828 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
830 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
832 ci = (struct MR_LD_TARGET_SYNC *)
833 fusion->ld_map[(instance->map_id - 1) & 1];
834 memset(ci, 0, sizeof(struct MR_FW_RAID_MAP_ALL));
836 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
838 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
840 for (i = 0; i < num_lds; i++, ld_sync++) {
841 raid = MR_LdRaidGet(i, map);
842 ld_sync->targetId = MR_GetLDTgtId(i, map);
843 ld_sync->seqNum = raid->seqNum;
846 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
847 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
849 dcmd->cmd = MFI_CMD_DCMD;
850 dcmd->cmd_status = 0xFF;
852 dcmd->flags = MFI_FRAME_DIR_WRITE;
855 dcmd->data_xfer_len = size_map_info;
856 dcmd->mbox.b[0] = num_lds;
857 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
858 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
859 dcmd->sgl.sge32[0].phys_addr = ci_h;
860 dcmd->sgl.sge32[0].length = size_map_info;
862 instance->map_update_cmd = cmd;
864 instance->instancet->issue_dcmd(instance, cmd);
870 * megasas_init_adapter_fusion - Initializes the FW
871 * @instance: Adapter soft state
873 * This is the main function for initializing firmware.
876 megasas_init_adapter_fusion(struct megasas_instance *instance)
878 struct megasas_register_set __iomem *reg_set;
879 struct fusion_context *fusion;
883 fusion = instance->ctrl_context;
885 reg_set = instance->reg_set;
888 * Get various operational parameters from status register
890 instance->max_fw_cmds =
891 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
892 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
895 * Reduce the max supported cmds by 1. This is to ensure that the
896 * reply_q_sz (1 more than the max cmd that driver may send)
897 * does not exceed max cmds that the FW can support
899 instance->max_fw_cmds = instance->max_fw_cmds-1;
900 /* Only internal cmds (DCMD) need to have MFI frames */
901 instance->max_mfi_cmds = MEGASAS_INT_CMDS;
903 max_cmd = instance->max_fw_cmds;
905 fusion->reply_q_depth = ((max_cmd + 1 + 15)/16)*16;
907 fusion->request_alloc_sz =
908 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
909 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
910 *(fusion->reply_q_depth);
911 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
912 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
913 (max_cmd + 1)); /* Extra 1 for SMID 0 */
915 fusion->max_sge_in_main_msg =
916 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
917 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
919 fusion->max_sge_in_chain =
920 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
922 instance->max_num_sge = fusion->max_sge_in_main_msg +
923 fusion->max_sge_in_chain - 2;
925 /* Used for pass thru MFI frame (DCMD) */
926 fusion->chain_offset_mfi_pthru =
927 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
929 fusion->chain_offset_io_request =
930 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
931 sizeof(union MPI2_SGE_IO_UNION))/16;
933 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
934 for (i = 0 ; i < count; i++)
935 fusion->last_reply_idx[i] = 0;
938 * Allocate memory for descriptors
939 * Create a pool of commands
941 if (megasas_alloc_cmds(instance))
942 goto fail_alloc_mfi_cmds;
943 if (megasas_alloc_cmds_fusion(instance))
944 goto fail_alloc_cmds;
946 if (megasas_ioc_init_fusion(instance))
949 instance->flag_ieee = 1;
951 fusion->map_sz = sizeof(struct MR_FW_RAID_MAP) +
952 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
954 fusion->fast_path_io = 0;
956 for (i = 0; i < 2; i++) {
957 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
959 &fusion->ld_map_phys[i],
961 if (!fusion->ld_map[i]) {
962 printk(KERN_ERR "megasas: Could not allocate memory "
968 if (!megasas_get_map_info(instance))
969 megasas_sync_map_info(instance);
975 dma_free_coherent(&instance->pdev->dev, fusion->map_sz,
976 fusion->ld_map[0], fusion->ld_map_phys[0]);
978 megasas_free_cmds_fusion(instance);
980 megasas_free_cmds(instance);
986 * megasas_fire_cmd_fusion - Sends command to the FW
987 * @frame_phys_addr : Physical address of cmd
988 * @frame_count : Number of frames for the command
989 * @regs : MFI register set
992 megasas_fire_cmd_fusion(struct megasas_instance *instance,
993 dma_addr_t req_desc_lo,
995 struct megasas_register_set __iomem *regs)
999 spin_lock_irqsave(&instance->hba_lock, flags);
1002 &(regs)->inbound_low_queue_port);
1003 writel(req_desc_hi, &(regs)->inbound_high_queue_port);
1004 spin_unlock_irqrestore(&instance->hba_lock, flags);
1008 * map_cmd_status - Maps FW cmd status to OS cmd status
1009 * @cmd : Pointer to cmd
1010 * @status : status of cmd returned by FW
1011 * @ext_status : ext status of cmd returned by FW
1015 map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1021 cmd->scmd->result = DID_OK << 16;
1024 case MFI_STAT_SCSI_IO_FAILED:
1025 case MFI_STAT_LD_INIT_IN_PROGRESS:
1026 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1029 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1031 cmd->scmd->result = (DID_OK << 16) | ext_status;
1032 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1033 memset(cmd->scmd->sense_buffer, 0,
1034 SCSI_SENSE_BUFFERSIZE);
1035 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1036 SCSI_SENSE_BUFFERSIZE);
1037 cmd->scmd->result |= DRIVER_SENSE << 24;
1041 case MFI_STAT_LD_OFFLINE:
1042 case MFI_STAT_DEVICE_NOT_FOUND:
1043 cmd->scmd->result = DID_BAD_TARGET << 16;
1045 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1046 cmd->scmd->result = DID_IMM_RETRY << 16;
1049 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1050 cmd->scmd->result = DID_ERROR << 16;
1056 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1057 * @instance: Adapter soft state
1058 * @scp: SCSI command from the mid-layer
1059 * @sgl_ptr: SGL to be filled in
1060 * @cmd: cmd we are working on
1062 * If successful, this function returns the number of SG elements.
1065 megasas_make_sgl_fusion(struct megasas_instance *instance,
1066 struct scsi_cmnd *scp,
1067 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1068 struct megasas_cmd_fusion *cmd)
1070 int i, sg_processed, sge_count;
1071 struct scatterlist *os_sgl;
1072 struct fusion_context *fusion;
1074 fusion = instance->ctrl_context;
1076 if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1077 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1078 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1079 sgl_ptr_end->Flags = 0;
1082 sge_count = scsi_dma_map(scp);
1084 BUG_ON(sge_count < 0);
1086 if (sge_count > instance->max_num_sge || !sge_count)
1089 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1090 sgl_ptr->Length = sg_dma_len(os_sgl);
1091 sgl_ptr->Address = sg_dma_address(os_sgl);
1093 if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1094 if (i == sge_count - 1)
1095 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1099 sg_processed = i + 1;
1101 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1102 (sge_count > fusion->max_sge_in_main_msg)) {
1104 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1105 if (instance->pdev->device ==
1106 PCI_DEVICE_ID_LSI_INVADER) {
1107 if ((cmd->io_request->IoFlags &
1108 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1109 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1110 cmd->io_request->ChainOffset =
1112 chain_offset_io_request;
1114 cmd->io_request->ChainOffset = 0;
1116 cmd->io_request->ChainOffset =
1117 fusion->chain_offset_io_request;
1120 /* Prepare chain element */
1121 sg_chain->NextChainOffset = 0;
1122 if (instance->pdev->device ==
1123 PCI_DEVICE_ID_LSI_INVADER)
1124 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1127 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1128 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1129 sg_chain->Length = (sizeof(union MPI2_SGE_IO_UNION)
1130 *(sge_count - sg_processed));
1131 sg_chain->Address = cmd->sg_frame_phys_addr;
1134 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1142 * megasas_set_pd_lba - Sets PD LBA
1144 * @cdb_len: cdb length
1145 * @start_blk: Start block of IO
1147 * Used to set the PD LBA in CDB for FP IOs
1150 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1151 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1152 struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1154 struct MR_LD_RAID *raid;
1156 u64 start_blk = io_info->pdBlock;
1157 u8 *cdb = io_request->CDB.CDB32;
1158 u32 num_blocks = io_info->numBlocks;
1159 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1161 /* Check if T10 PI (DIF) is enabled for this LD */
1162 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1163 raid = MR_LdRaidGet(ld, local_map_ptr);
1164 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1165 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1166 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1167 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1169 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1170 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1172 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1173 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1176 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1177 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1178 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1179 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1180 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1181 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1182 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1183 cdb[19] = (u8)(start_blk & 0xff);
1185 /* Logical block reference tag */
1186 io_request->CDB.EEDP32.PrimaryReferenceTag =
1187 cpu_to_be32(ref_tag);
1188 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
1190 io_request->DataLength = num_blocks * 512;
1191 io_request->IoFlags = 32; /* Specify 32-byte cdb */
1193 /* Transfer length */
1194 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1195 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1196 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1197 cdb[31] = (u8)(num_blocks & 0xff);
1199 /* set SCSI IO EEDPFlags */
1200 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1201 io_request->EEDPFlags =
1202 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1203 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1204 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1205 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1206 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
1208 io_request->EEDPFlags =
1209 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1210 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
1212 io_request->Control |= (0x4 << 26);
1213 io_request->EEDPBlockSize = MEGASAS_EEDPBLOCKSIZE;
1215 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1216 if (((cdb_len == 12) || (cdb_len == 16)) &&
1217 (start_blk <= 0xffffffff)) {
1218 if (cdb_len == 16) {
1219 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1224 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1230 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1237 /* Transfer length */
1238 cdb[8] = (u8)(num_blocks & 0xff);
1239 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1241 io_request->IoFlags = 10; /* Specify 10-byte cdb */
1243 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1244 /* Convert to 16 byte CDB for large LBA's */
1247 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1252 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1259 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1266 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1273 /* Transfer length */
1274 cdb[13] = (u8)(num_blocks & 0xff);
1275 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1276 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1277 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1279 io_request->IoFlags = 16; /* Specify 16-byte cdb */
1283 /* Normal case, just load LBA here */
1287 u8 val = cdb[1] & 0xE0;
1288 cdb[3] = (u8)(start_blk & 0xff);
1289 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1290 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1294 cdb[5] = (u8)(start_blk & 0xff);
1295 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1296 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1297 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1300 cdb[5] = (u8)(start_blk & 0xff);
1301 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1302 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1303 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1306 cdb[9] = (u8)(start_blk & 0xff);
1307 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1308 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1309 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1310 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1311 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1312 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1313 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1320 * megasas_build_ldio_fusion - Prepares IOs to devices
1321 * @instance: Adapter soft state
1322 * @scp: SCSI command
1323 * @cmd: Command to be prepared
1325 * Prepares the io_request and chain elements (sg_frame) for IO
1326 * The IO can be for PD (Fast Path) or LD
1329 megasas_build_ldio_fusion(struct megasas_instance *instance,
1330 struct scsi_cmnd *scp,
1331 struct megasas_cmd_fusion *cmd)
1334 u32 start_lba_lo, start_lba_hi, device_id;
1335 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1336 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1337 struct IO_REQUEST_INFO io_info;
1338 struct fusion_context *fusion;
1339 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1341 device_id = MEGASAS_DEV_INDEX(instance, scp);
1343 fusion = instance->ctrl_context;
1345 io_request = cmd->io_request;
1346 io_request->RaidContext.VirtualDiskTgtId = device_id;
1347 io_request->RaidContext.status = 0;
1348 io_request->RaidContext.exStatus = 0;
1350 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1357 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1359 if (scp->cmd_len == 6) {
1360 io_request->DataLength = (u32) scp->cmnd[4];
1361 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1362 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1364 start_lba_lo &= 0x1FFFFF;
1368 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1370 else if (scp->cmd_len == 10) {
1371 io_request->DataLength = (u32) scp->cmnd[8] |
1372 ((u32) scp->cmnd[7] << 8);
1373 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1374 ((u32) scp->cmnd[3] << 16) |
1375 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1379 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1381 else if (scp->cmd_len == 12) {
1382 io_request->DataLength = ((u32) scp->cmnd[6] << 24) |
1383 ((u32) scp->cmnd[7] << 16) |
1384 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1385 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1386 ((u32) scp->cmnd[3] << 16) |
1387 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1391 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1393 else if (scp->cmd_len == 16) {
1394 io_request->DataLength = ((u32) scp->cmnd[10] << 24) |
1395 ((u32) scp->cmnd[11] << 16) |
1396 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1397 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1398 ((u32) scp->cmnd[7] << 16) |
1399 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1401 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1402 ((u32) scp->cmnd[3] << 16) |
1403 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1406 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1407 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
1408 io_info.numBlocks = io_request->DataLength;
1409 io_info.ldTgtId = device_id;
1411 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1414 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1416 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1417 MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) {
1418 io_request->RaidContext.regLockFlags = 0;
1421 if (MR_BuildRaidContext(instance, &io_info,
1422 &io_request->RaidContext,
1424 fp_possible = io_info.fpOkForIo;
1427 /* Use smp_processor_id() for now until cmd->request->cpu is CPU
1428 id by default, not CPU group id, otherwise all MSI-X queues won't
1430 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1431 smp_processor_id() % instance->msix_vectors : 0;
1434 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1435 local_map_ptr, start_lba_lo);
1436 io_request->DataLength = scsi_bufflen(scp);
1437 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1438 cmd->request_desc->SCSIIO.RequestFlags =
1439 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1440 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1441 if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1442 if (io_request->RaidContext.regLockFlags ==
1444 cmd->request_desc->SCSIIO.RequestFlags =
1445 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1446 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1447 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1448 io_request->RaidContext.nseg = 0x1;
1449 io_request->IoFlags |=
1450 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
1451 io_request->RaidContext.regLockFlags |=
1452 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1453 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1455 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1458 get_updated_dev_handle(
1459 &fusion->load_balance_info[device_id],
1461 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1463 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1464 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1465 io_request->DevHandle = io_info.devHandle;
1467 io_request->RaidContext.timeoutValue =
1468 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1469 cmd->request_desc->SCSIIO.RequestFlags =
1470 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1471 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1472 if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1473 if (io_request->RaidContext.regLockFlags ==
1475 cmd->request_desc->SCSIIO.RequestFlags =
1476 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1477 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1478 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1479 io_request->RaidContext.regLockFlags |=
1480 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1481 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1482 io_request->RaidContext.nseg = 0x1;
1484 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1485 io_request->DevHandle = device_id;
1490 * megasas_build_dcdb_fusion - Prepares IOs to devices
1491 * @instance: Adapter soft state
1492 * @scp: SCSI command
1493 * @cmd: Command to be prepared
1495 * Prepares the io_request frame for non-io cmds
1498 megasas_build_dcdb_fusion(struct megasas_instance *instance,
1499 struct scsi_cmnd *scmd,
1500 struct megasas_cmd_fusion *cmd)
1503 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1505 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1506 struct fusion_context *fusion = instance->ctrl_context;
1508 io_request = cmd->io_request;
1509 device_id = MEGASAS_DEV_INDEX(instance, scmd);
1510 pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1512 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1514 /* Check if this is a system PD I/O */
1515 if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1516 io_request->Function = 0;
1517 io_request->DevHandle =
1518 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1519 io_request->RaidContext.timeoutValue =
1520 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1521 io_request->RaidContext.regLockFlags = 0;
1522 io_request->RaidContext.regLockRowLBA = 0;
1523 io_request->RaidContext.regLockLength = 0;
1524 io_request->RaidContext.RAIDFlags =
1525 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1526 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1527 cmd->request_desc->SCSIIO.RequestFlags =
1528 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1529 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1531 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1532 io_request->DevHandle = device_id;
1533 cmd->request_desc->SCSIIO.RequestFlags =
1534 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1535 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1537 io_request->RaidContext.VirtualDiskTgtId = device_id;
1538 io_request->LUN[1] = scmd->device->lun;
1539 io_request->DataLength = scsi_bufflen(scmd);
1543 * megasas_build_io_fusion - Prepares IOs to devices
1544 * @instance: Adapter soft state
1545 * @scp: SCSI command
1546 * @cmd: Command to be prepared
1548 * Invokes helper functions to prepare request frames
1549 * and sets flags appropriate for IO/Non-IO cmd
1552 megasas_build_io_fusion(struct megasas_instance *instance,
1553 struct scsi_cmnd *scp,
1554 struct megasas_cmd_fusion *cmd)
1556 u32 device_id, sge_count;
1557 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1559 device_id = MEGASAS_DEV_INDEX(instance, scp);
1561 /* Zero out some fields so they don't get reused */
1562 io_request->LUN[1] = 0;
1563 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1564 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1565 io_request->EEDPFlags = 0;
1566 io_request->Control = 0;
1567 io_request->EEDPBlockSize = 0;
1568 io_request->ChainOffset = 0;
1569 io_request->RaidContext.RAIDFlags = 0;
1570 io_request->RaidContext.Type = 0;
1571 io_request->RaidContext.nseg = 0;
1573 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1575 * Just the CDB length,rest of the Flags are zero
1576 * This will be modified for FP in build_ldio_fusion
1578 io_request->IoFlags = scp->cmd_len;
1580 if (megasas_is_ldio(scp))
1581 megasas_build_ldio_fusion(instance, scp, cmd);
1583 megasas_build_dcdb_fusion(instance, scp, cmd);
1590 megasas_make_sgl_fusion(instance, scp,
1591 (struct MPI25_IEEE_SGE_CHAIN64 *)
1592 &io_request->SGL, cmd);
1594 if (sge_count > instance->max_num_sge) {
1595 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1596 "max (0x%x) allowed\n", sge_count,
1597 instance->max_num_sge);
1601 io_request->RaidContext.numSGE = sge_count;
1603 io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
1605 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1606 io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
1607 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1608 io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
1610 io_request->SGLOffset0 =
1611 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1613 io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
1614 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1617 scp->SCp.ptr = (char *)cmd;
1622 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1623 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1626 struct fusion_context *fusion;
1628 if (index >= instance->max_fw_cmds) {
1629 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
1630 "descriptor\n", index);
1633 fusion = instance->ctrl_context;
1634 p = fusion->req_frames_desc
1635 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1637 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1641 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1642 * issuing non IOCTL cmd
1643 * @instance: Adapter soft state
1644 * @scmd: pointer to scsi cmd from OS
1647 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1648 struct scsi_cmnd *scmd)
1650 struct megasas_cmd_fusion *cmd;
1651 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1653 struct fusion_context *fusion;
1655 fusion = instance->ctrl_context;
1657 cmd = megasas_get_cmd_fusion(instance);
1659 return SCSI_MLQUEUE_HOST_BUSY;
1663 req_desc = megasas_get_request_descriptor(instance, index-1);
1667 req_desc->Words = 0;
1668 cmd->request_desc = req_desc;
1670 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1671 megasas_return_cmd_fusion(instance, cmd);
1672 printk(KERN_ERR "megasas: Error building command.\n");
1673 cmd->request_desc = NULL;
1677 req_desc = cmd->request_desc;
1678 req_desc->SCSIIO.SMID = index;
1680 if (cmd->io_request->ChainOffset != 0 &&
1681 cmd->io_request->ChainOffset != 0xF)
1682 printk(KERN_ERR "megasas: The chain offset value is not "
1683 "correct : %x\n", cmd->io_request->ChainOffset);
1686 * Issue the command to the FW
1688 atomic_inc(&instance->fw_outstanding);
1690 instance->instancet->fire_cmd(instance,
1691 req_desc->u.low, req_desc->u.high,
1698 * complete_cmd_fusion - Completes command
1699 * @instance: Adapter soft state
1700 * Completes all commands that is in reply descriptor queue
1703 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
1705 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1706 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1707 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1708 struct fusion_context *fusion;
1709 struct megasas_cmd *cmd_mfi;
1710 struct megasas_cmd_fusion *cmd_fusion;
1711 u16 smid, num_completed;
1712 u8 reply_descript_type, arm;
1713 u32 status, extStatus, device_id;
1714 union desc_value d_val;
1715 struct LD_LOAD_BALANCE_INFO *lbinfo;
1717 fusion = instance->ctrl_context;
1719 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1722 desc = fusion->reply_frames_desc;
1723 desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1724 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1725 fusion->last_reply_idx[MSIxIndex];
1727 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1729 d_val.word = desc->Words;
1731 reply_descript_type = reply_desc->ReplyFlags &
1732 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1734 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1737 d_val.word = desc->Words;
1741 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
1742 smid = reply_desc->SMID;
1744 cmd_fusion = fusion->cmd_list[smid - 1];
1747 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1748 cmd_fusion->io_request;
1750 if (cmd_fusion->scmd)
1751 cmd_fusion->scmd->SCp.ptr = NULL;
1753 status = scsi_io_req->RaidContext.status;
1754 extStatus = scsi_io_req->RaidContext.exStatus;
1756 switch (scsi_io_req->Function) {
1757 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
1758 /* Update load balancing info */
1759 device_id = MEGASAS_DEV_INDEX(instance,
1761 lbinfo = &fusion->load_balance_info[device_id];
1762 if (cmd_fusion->scmd->SCp.Status &
1763 MEGASAS_LOAD_BALANCE_FLAG) {
1764 arm = lbinfo->raid1DevHandle[0] ==
1765 cmd_fusion->io_request->DevHandle ? 0 :
1767 atomic_dec(&lbinfo->scsi_pending_cmds[arm]);
1768 cmd_fusion->scmd->SCp.Status &=
1769 ~MEGASAS_LOAD_BALANCE_FLAG;
1771 if (reply_descript_type ==
1772 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
1773 if (megasas_dbg_lvl == 5)
1774 printk(KERN_ERR "\nmegasas: FAST Path "
1777 /* Fall thru and complete IO */
1778 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
1779 /* Map the FW Cmd Status */
1780 map_cmd_status(cmd_fusion, status, extStatus);
1781 scsi_dma_unmap(cmd_fusion->scmd);
1782 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
1783 scsi_io_req->RaidContext.status = 0;
1784 scsi_io_req->RaidContext.exStatus = 0;
1785 megasas_return_cmd_fusion(instance, cmd_fusion);
1786 atomic_dec(&instance->fw_outstanding);
1789 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
1790 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1791 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1792 cmd_fusion->flags = 0;
1793 megasas_return_cmd_fusion(instance, cmd_fusion);
1798 fusion->last_reply_idx[MSIxIndex]++;
1799 if (fusion->last_reply_idx[MSIxIndex] >=
1800 fusion->reply_q_depth)
1801 fusion->last_reply_idx[MSIxIndex] = 0;
1803 desc->Words = ULLONG_MAX;
1806 /* Get the next reply descriptor */
1807 if (!fusion->last_reply_idx[MSIxIndex])
1808 desc = fusion->reply_frames_desc +
1809 ((MSIxIndex * fusion->reply_alloc_sz)/
1810 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
1815 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1817 d_val.word = desc->Words;
1819 reply_descript_type = reply_desc->ReplyFlags &
1820 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1822 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1830 writel((MSIxIndex << 24) | fusion->last_reply_idx[MSIxIndex],
1831 &instance->reg_set->reply_post_host_index);
1832 megasas_check_and_restore_queue_depth(instance);
1837 * megasas_complete_cmd_dpc_fusion - Completes command
1838 * @instance: Adapter soft state
1840 * Tasklet to complete cmds
1843 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
1845 struct megasas_instance *instance =
1846 (struct megasas_instance *)instance_addr;
1847 unsigned long flags;
1848 u32 count, MSIxIndex;
1850 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1852 /* If we have already declared adapter dead, donot complete cmds */
1853 spin_lock_irqsave(&instance->hba_lock, flags);
1854 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1855 spin_unlock_irqrestore(&instance->hba_lock, flags);
1858 spin_unlock_irqrestore(&instance->hba_lock, flags);
1860 spin_lock_irqsave(&instance->completion_lock, flags);
1861 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
1862 complete_cmd_fusion(instance, MSIxIndex);
1863 spin_unlock_irqrestore(&instance->completion_lock, flags);
1867 * megasas_isr_fusion - isr entry point
1869 irqreturn_t megasas_isr_fusion(int irq, void *devp)
1871 struct megasas_irq_context *irq_context = devp;
1872 struct megasas_instance *instance = irq_context->instance;
1873 u32 mfiStatus, fw_state;
1875 if (!instance->msix_vectors) {
1876 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
1881 /* If we are resetting, bail */
1882 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
1883 instance->instancet->clear_intr(instance->reg_set);
1887 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
1888 instance->instancet->clear_intr(instance->reg_set);
1889 /* If we didn't complete any commands, check for FW fault */
1890 fw_state = instance->instancet->read_fw_status_reg(
1891 instance->reg_set) & MFI_STATE_MASK;
1892 if (fw_state == MFI_STATE_FAULT)
1893 schedule_work(&instance->work_init);
1900 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
1901 * @instance: Adapter soft state
1902 * mfi_cmd: megasas_cmd pointer
1906 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
1907 struct megasas_cmd *mfi_cmd)
1909 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
1910 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
1911 struct megasas_cmd_fusion *cmd;
1912 struct fusion_context *fusion;
1913 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
1915 cmd = megasas_get_cmd_fusion(instance);
1919 /* Save the smid. To be used for returning the cmd */
1920 mfi_cmd->context.smid = cmd->index;
1922 cmd->sync_cmd_idx = mfi_cmd->index;
1925 * For cmds where the flag is set, store the flag and check
1926 * on completion. For cmds with this flag, don't call
1927 * megasas_complete_cmd
1930 if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)
1931 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
1933 fusion = instance->ctrl_context;
1934 io_req = cmd->io_request;
1936 if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1937 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
1938 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
1939 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1940 sgl_ptr_end->Flags = 0;
1944 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
1946 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
1947 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
1949 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
1951 mpi25_ieee_chain->Address = mfi_cmd->frame_phys_addr;
1953 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1954 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
1956 mpi25_ieee_chain->Length = MEGASAS_MAX_SZ_CHAIN_FRAME;
1962 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
1963 * @instance: Adapter soft state
1964 * @cmd: mfi cmd to build
1967 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1968 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
1970 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1973 if (build_mpt_mfi_pass_thru(instance, cmd)) {
1974 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
1978 index = cmd->context.smid;
1980 req_desc = megasas_get_request_descriptor(instance, index - 1);
1985 req_desc->Words = 0;
1986 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1987 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1989 req_desc->SCSIIO.SMID = index;
1995 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
1996 * @instance: Adapter soft state
1997 * @cmd: mfi cmd pointer
2001 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2002 struct megasas_cmd *cmd)
2004 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2006 req_desc = build_mpt_cmd(instance, cmd);
2008 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2011 instance->instancet->fire_cmd(instance, req_desc->u.low,
2012 req_desc->u.high, instance->reg_set);
2016 * megasas_release_fusion - Reverses the FW initialization
2017 * @intance: Adapter soft state
2020 megasas_release_fusion(struct megasas_instance *instance)
2022 megasas_free_cmds(instance);
2023 megasas_free_cmds_fusion(instance);
2025 iounmap(instance->reg_set);
2027 pci_release_selected_regions(instance->pdev, instance->bar);
2031 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2032 * @regs: MFI register set
2035 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2037 return readl(&(regs)->outbound_scratch_pad);
2041 * megasas_adp_reset_fusion - For controller reset
2042 * @regs: MFI register set
2045 megasas_adp_reset_fusion(struct megasas_instance *instance,
2046 struct megasas_register_set __iomem *regs)
2052 * megasas_check_reset_fusion - For controller reset check
2053 * @regs: MFI register set
2056 megasas_check_reset_fusion(struct megasas_instance *instance,
2057 struct megasas_register_set __iomem *regs)
2062 /* This function waits for outstanding commands on fusion to complete */
2063 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance)
2065 int i, outstanding, retval = 0;
2066 u32 fw_state, wait_time = MEGASAS_RESET_WAIT_TIME;
2068 for (i = 0; i < wait_time; i++) {
2069 /* Check if firmware is in fault state */
2070 fw_state = instance->instancet->read_fw_status_reg(
2071 instance->reg_set) & MFI_STATE_MASK;
2072 if (fw_state == MFI_STATE_FAULT) {
2073 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
2074 " will reset adapter.\n");
2079 outstanding = atomic_read(&instance->fw_outstanding);
2083 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2084 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2085 "commands to complete\n", i, outstanding);
2086 megasas_complete_cmd_dpc_fusion(
2087 (unsigned long)instance);
2092 if (atomic_read(&instance->fw_outstanding)) {
2093 printk("megaraid_sas: pending commands remain after waiting, "
2094 "will reset adapter.\n");
2101 void megasas_reset_reply_desc(struct megasas_instance *instance)
2104 struct fusion_context *fusion;
2105 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2107 fusion = instance->ctrl_context;
2108 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2109 for (i = 0 ; i < count ; i++)
2110 fusion->last_reply_idx[i] = 0;
2111 reply_desc = fusion->reply_frames_desc;
2112 for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
2113 reply_desc->Words = ULLONG_MAX;
2116 /* Core fusion reset function */
2117 int megasas_reset_fusion(struct Scsi_Host *shost)
2119 int retval = SUCCESS, i, j, retry = 0;
2120 struct megasas_instance *instance;
2121 struct megasas_cmd_fusion *cmd_fusion;
2122 struct fusion_context *fusion;
2123 struct megasas_cmd *cmd_mfi;
2124 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2125 u32 host_diag, abs_state, status_reg, reset_adapter;
2127 instance = (struct megasas_instance *)shost->hostdata;
2128 fusion = instance->ctrl_context;
2130 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2131 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
2132 "returning FAILED.\n");
2136 mutex_lock(&instance->reset_mutex);
2137 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2138 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2139 instance->instancet->disable_intr(instance->reg_set);
2142 /* First try waiting for commands to complete */
2143 if (megasas_wait_for_outstanding_fusion(instance)) {
2144 printk(KERN_WARNING "megaraid_sas: resetting fusion "
2146 /* Now return commands back to the OS */
2147 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2148 cmd_fusion = fusion->cmd_list[i];
2149 if (cmd_fusion->scmd) {
2150 scsi_dma_unmap(cmd_fusion->scmd);
2151 cmd_fusion->scmd->result = (DID_RESET << 16);
2152 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2153 megasas_return_cmd_fusion(instance, cmd_fusion);
2154 atomic_dec(&instance->fw_outstanding);
2158 status_reg = instance->instancet->read_fw_status_reg(
2160 abs_state = status_reg & MFI_STATE_MASK;
2161 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2162 if (instance->disableOnlineCtrlReset ||
2163 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
2164 /* Reset not supported, kill adapter */
2165 printk(KERN_WARNING "megaraid_sas: Reset not supported"
2166 ", killing adapter.\n");
2167 megaraid_sas_kill_hba(instance);
2168 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2173 /* Now try to reset the chip */
2174 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2175 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2176 &instance->reg_set->fusion_seq_offset);
2177 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2178 &instance->reg_set->fusion_seq_offset);
2179 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2180 &instance->reg_set->fusion_seq_offset);
2181 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2182 &instance->reg_set->fusion_seq_offset);
2183 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2184 &instance->reg_set->fusion_seq_offset);
2185 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2186 &instance->reg_set->fusion_seq_offset);
2187 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2188 &instance->reg_set->fusion_seq_offset);
2190 /* Check that the diag write enable (DRWE) bit is on */
2191 host_diag = readl(&instance->reg_set->fusion_host_diag);
2193 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2196 readl(&instance->reg_set->fusion_host_diag);
2197 if (retry++ == 100) {
2198 printk(KERN_WARNING "megaraid_sas: "
2199 "Host diag unlock failed!\n");
2203 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2206 /* Send chip reset command */
2207 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2208 &instance->reg_set->fusion_host_diag);
2211 /* Make sure reset adapter bit is cleared */
2212 host_diag = readl(&instance->reg_set->fusion_host_diag);
2214 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2217 readl(&instance->reg_set->fusion_host_diag);
2218 if (retry++ == 1000) {
2219 printk(KERN_WARNING "megaraid_sas: "
2220 "Diag reset adapter never "
2225 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2229 instance->instancet->read_fw_status_reg(
2230 instance->reg_set) & MFI_STATE_MASK;
2233 while ((abs_state <= MFI_STATE_FW_INIT) &&
2237 instance->instancet->read_fw_status_reg(
2238 instance->reg_set) & MFI_STATE_MASK;
2240 if (abs_state <= MFI_STATE_FW_INIT) {
2241 printk(KERN_WARNING "megaraid_sas: firmware "
2242 "state < MFI_STATE_FW_INIT, state = "
2243 "0x%x\n", abs_state);
2247 /* Wait for FW to become ready */
2248 if (megasas_transition_to_ready(instance, 1)) {
2249 printk(KERN_WARNING "megaraid_sas: Failed to "
2250 "transition controller to ready.\n");
2254 megasas_reset_reply_desc(instance);
2255 if (megasas_ioc_init_fusion(instance)) {
2256 printk(KERN_WARNING "megaraid_sas: "
2257 "megasas_ioc_init_fusion() failed!\n");
2261 clear_bit(MEGASAS_FUSION_IN_RESET,
2262 &instance->reset_flags);
2263 instance->instancet->enable_intr(instance->reg_set);
2264 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2266 /* Re-fire management commands */
2267 for (j = 0 ; j < instance->max_fw_cmds; j++) {
2268 cmd_fusion = fusion->cmd_list[j];
2269 if (cmd_fusion->sync_cmd_idx !=
2273 cmd_list[cmd_fusion->sync_cmd_idx];
2274 if (cmd_mfi->frame->dcmd.opcode ==
2275 MR_DCMD_LD_MAP_GET_INFO) {
2276 megasas_return_cmd(instance,
2278 megasas_return_cmd_fusion(
2279 instance, cmd_fusion);
2282 megasas_get_request_descriptor(
2284 cmd_mfi->context.smid
2291 instance->instancet->
2304 /* Reset load balance info */
2305 memset(fusion->load_balance_info, 0,
2306 sizeof(struct LD_LOAD_BALANCE_INFO)
2307 *MAX_LOGICAL_DRIVES);
2309 if (!megasas_get_map_info(instance))
2310 megasas_sync_map_info(instance);
2312 /* Adapter reset completed successfully */
2313 printk(KERN_WARNING "megaraid_sas: Reset "
2318 /* Reset failed, kill the adapter */
2319 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
2321 megaraid_sas_kill_hba(instance);
2324 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2325 instance->instancet->enable_intr(instance->reg_set);
2326 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2329 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2330 mutex_unlock(&instance->reset_mutex);
2334 /* Fusion OCR work queue */
2335 void megasas_fusion_ocr_wq(struct work_struct *work)
2337 struct megasas_instance *instance =
2338 container_of(work, struct megasas_instance, work_init);
2340 megasas_reset_fusion(instance->host);
2343 struct megasas_instance_template megasas_instance_template_fusion = {
2344 .fire_cmd = megasas_fire_cmd_fusion,
2345 .enable_intr = megasas_enable_intr_fusion,
2346 .disable_intr = megasas_disable_intr_fusion,
2347 .clear_intr = megasas_clear_intr_fusion,
2348 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2349 .adp_reset = megasas_adp_reset_fusion,
2350 .check_reset = megasas_check_reset_fusion,
2351 .service_isr = megasas_isr_fusion,
2352 .tasklet = megasas_complete_cmd_dpc_fusion,
2353 .init_adapter = megasas_init_adapter_fusion,
2354 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2355 .issue_dcmd = megasas_issue_dcmd_fusion,