2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2009-2012 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 u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
90 struct IO_REQUEST_INFO *in_info);
91 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
92 void megaraid_sas_kill_hba(struct megasas_instance *instance);
94 extern u32 megasas_dbg_lvl;
95 extern int resetwaittime;
98 * megasas_enable_intr_fusion - Enables interrupts
99 * @regs: MFI register set
102 megasas_enable_intr_fusion(struct megasas_instance *instance)
104 struct megasas_register_set __iomem *regs;
105 regs = instance->reg_set;
106 /* For Thunderbolt/Invader also clear intr on enable */
107 writel(~0, ®s->outbound_intr_status);
108 readl(®s->outbound_intr_status);
110 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
112 /* Dummy readl to force pci flush */
113 readl(®s->outbound_intr_mask);
114 instance->mask_interrupts = 0;
118 * megasas_disable_intr_fusion - Disables interrupt
119 * @regs: MFI register set
122 megasas_disable_intr_fusion(struct megasas_instance *instance)
124 u32 mask = 0xFFFFFFFF;
126 struct megasas_register_set __iomem *regs;
127 regs = instance->reg_set;
128 instance->mask_interrupts = 1;
130 writel(mask, ®s->outbound_intr_mask);
131 /* Dummy readl to force pci flush */
132 status = readl(®s->outbound_intr_mask);
136 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
140 * Check if it is our interrupt
142 status = readl(®s->outbound_intr_status);
145 writel(status, ®s->outbound_intr_status);
146 readl(®s->outbound_intr_status);
149 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
156 * megasas_get_cmd_fusion - Get a command from the free pool
157 * @instance: Adapter soft state
159 * Returns a free command from the pool
161 struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
165 struct fusion_context *fusion =
166 (struct fusion_context *)instance->ctrl_context;
167 struct megasas_cmd_fusion *cmd = NULL;
169 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
171 if (!list_empty(&fusion->cmd_pool)) {
172 cmd = list_entry((&fusion->cmd_pool)->next,
173 struct megasas_cmd_fusion, list);
174 list_del_init(&cmd->list);
176 printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
179 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
184 * megasas_return_cmd_fusion - Return a cmd to free command pool
185 * @instance: Adapter soft state
186 * @cmd: Command packet to be returned to free command pool
189 megasas_return_cmd_fusion(struct megasas_instance *instance,
190 struct megasas_cmd_fusion *cmd)
193 struct fusion_context *fusion =
194 (struct fusion_context *)instance->ctrl_context;
196 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
199 cmd->sync_cmd_idx = (u32)ULONG_MAX;
200 list_add_tail(&cmd->list, &fusion->cmd_pool);
202 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
206 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
207 * @instance: Adapter soft state
209 static void megasas_teardown_frame_pool_fusion(
210 struct megasas_instance *instance)
213 struct fusion_context *fusion = instance->ctrl_context;
215 u16 max_cmd = instance->max_fw_cmds;
217 struct megasas_cmd_fusion *cmd;
219 if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
220 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
221 "sense pool : %p\n", fusion->sg_dma_pool,
222 fusion->sense_dma_pool);
227 * Return all frames to pool
229 for (i = 0; i < max_cmd; i++) {
231 cmd = fusion->cmd_list[i];
234 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
235 cmd->sg_frame_phys_addr);
238 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
239 cmd->sense_phys_addr);
243 * Now destroy the pool itself
245 pci_pool_destroy(fusion->sg_dma_pool);
246 pci_pool_destroy(fusion->sense_dma_pool);
248 fusion->sg_dma_pool = NULL;
249 fusion->sense_dma_pool = NULL;
253 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
254 * @instance: Adapter soft state
257 megasas_free_cmds_fusion(struct megasas_instance *instance)
260 struct fusion_context *fusion = instance->ctrl_context;
262 u32 max_cmds, req_sz, reply_sz, io_frames_sz;
265 req_sz = fusion->request_alloc_sz;
266 reply_sz = fusion->reply_alloc_sz;
267 io_frames_sz = fusion->io_frames_alloc_sz;
269 max_cmds = instance->max_fw_cmds;
271 /* Free descriptors and request Frames memory */
272 if (fusion->req_frames_desc)
273 dma_free_coherent(&instance->pdev->dev, req_sz,
274 fusion->req_frames_desc,
275 fusion->req_frames_desc_phys);
277 if (fusion->reply_frames_desc) {
278 pci_pool_free(fusion->reply_frames_desc_pool,
279 fusion->reply_frames_desc,
280 fusion->reply_frames_desc_phys);
281 pci_pool_destroy(fusion->reply_frames_desc_pool);
284 if (fusion->io_request_frames) {
285 pci_pool_free(fusion->io_request_frames_pool,
286 fusion->io_request_frames,
287 fusion->io_request_frames_phys);
288 pci_pool_destroy(fusion->io_request_frames_pool);
291 /* Free the Fusion frame pool */
292 megasas_teardown_frame_pool_fusion(instance);
294 /* Free all the commands in the cmd_list */
295 for (i = 0; i < max_cmds; i++)
296 kfree(fusion->cmd_list[i]);
298 /* Free the cmd_list buffer itself */
299 kfree(fusion->cmd_list);
300 fusion->cmd_list = NULL;
302 INIT_LIST_HEAD(&fusion->cmd_pool);
306 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
307 * @instance: Adapter soft state
310 static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
314 struct fusion_context *fusion;
315 struct megasas_cmd_fusion *cmd;
316 u32 total_sz_chain_frame;
318 fusion = instance->ctrl_context;
319 max_cmd = instance->max_fw_cmds;
321 total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
324 * Use DMA pool facility provided by PCI layer
327 fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
329 total_sz_chain_frame, 4,
331 if (!fusion->sg_dma_pool) {
332 printk(KERN_DEBUG "megasas: failed to setup request pool "
336 fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
338 SCSI_SENSE_BUFFERSIZE, 64, 0);
340 if (!fusion->sense_dma_pool) {
341 printk(KERN_DEBUG "megasas: failed to setup sense pool "
343 pci_pool_destroy(fusion->sg_dma_pool);
344 fusion->sg_dma_pool = NULL;
349 * Allocate and attach a frame to each of the commands in cmd_list
351 for (i = 0; i < max_cmd; i++) {
353 cmd = fusion->cmd_list[i];
355 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
357 &cmd->sg_frame_phys_addr);
359 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
360 GFP_KERNEL, &cmd->sense_phys_addr);
362 * megasas_teardown_frame_pool_fusion() takes care of freeing
363 * whatever has been allocated
365 if (!cmd->sg_frame || !cmd->sense) {
366 printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
367 megasas_teardown_frame_pool_fusion(instance);
375 * megasas_alloc_cmds_fusion - Allocates the command packets
376 * @instance: Adapter soft state
379 * Each frame has a 32-bit field called context. This context is used to get
380 * back the megasas_cmd_fusion from the frame when a frame gets completed
381 * In this driver, the 32 bit values are the indices into an array cmd_list.
382 * This array is used only to look up the megasas_cmd_fusion given the context.
383 * The free commands themselves are maintained in a linked list called cmd_pool.
385 * cmds are formed in the io_request and sg_frame members of the
386 * megasas_cmd_fusion. The context field is used to get a request descriptor
387 * and is used as SMID of the cmd.
388 * SMID value range is from 1 to max_fw_cmds.
391 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
394 u32 max_cmd, io_frames_sz;
395 struct fusion_context *fusion;
396 struct megasas_cmd_fusion *cmd;
397 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
399 dma_addr_t io_req_base_phys;
402 fusion = instance->ctrl_context;
404 max_cmd = instance->max_fw_cmds;
406 fusion->req_frames_desc =
407 dma_alloc_coherent(&instance->pdev->dev,
408 fusion->request_alloc_sz,
409 &fusion->req_frames_desc_phys, GFP_KERNEL);
411 if (!fusion->req_frames_desc) {
412 printk(KERN_ERR "megasas; Could not allocate memory for "
417 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
418 fusion->reply_frames_desc_pool =
419 pci_pool_create("reply_frames pool", instance->pdev,
420 fusion->reply_alloc_sz * count, 16, 0);
422 if (!fusion->reply_frames_desc_pool) {
423 printk(KERN_ERR "megasas; Could not allocate memory for "
424 "reply_frame pool\n");
425 goto fail_reply_desc;
428 fusion->reply_frames_desc =
429 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
430 &fusion->reply_frames_desc_phys);
431 if (!fusion->reply_frames_desc) {
432 printk(KERN_ERR "megasas; Could not allocate memory for "
433 "reply_frame pool\n");
434 pci_pool_destroy(fusion->reply_frames_desc_pool);
435 goto fail_reply_desc;
438 reply_desc = fusion->reply_frames_desc;
439 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
440 reply_desc->Words = ULLONG_MAX;
442 io_frames_sz = fusion->io_frames_alloc_sz;
444 fusion->io_request_frames_pool =
445 pci_pool_create("io_request_frames pool", instance->pdev,
446 fusion->io_frames_alloc_sz, 16, 0);
448 if (!fusion->io_request_frames_pool) {
449 printk(KERN_ERR "megasas: Could not allocate memory for "
450 "io_request_frame pool\n");
454 fusion->io_request_frames =
455 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
456 &fusion->io_request_frames_phys);
457 if (!fusion->io_request_frames) {
458 printk(KERN_ERR "megasas: Could not allocate memory for "
459 "io_request_frames frames\n");
460 pci_pool_destroy(fusion->io_request_frames_pool);
465 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
466 * Allocate the dynamic array first and then allocate individual
469 fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *)
470 * max_cmd, GFP_KERNEL);
472 if (!fusion->cmd_list) {
473 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
474 "memory for cmd_list_fusion\n");
478 max_cmd = instance->max_fw_cmds;
479 for (i = 0; i < max_cmd; i++) {
480 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
482 if (!fusion->cmd_list[i]) {
483 printk(KERN_ERR "Could not alloc cmd list fusion\n");
485 for (j = 0; j < i; j++)
486 kfree(fusion->cmd_list[j]);
488 kfree(fusion->cmd_list);
489 fusion->cmd_list = NULL;
494 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
495 io_req_base = fusion->io_request_frames +
496 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
497 io_req_base_phys = fusion->io_request_frames_phys +
498 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
501 * Add all the commands to command pool (fusion->cmd_pool)
504 /* SMID 0 is reserved. Set SMID/index from 1 */
505 for (i = 0; i < max_cmd; i++) {
506 cmd = fusion->cmd_list[i];
507 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
508 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
511 cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */
512 cmd->instance = instance;
514 (struct MPI2_RAID_SCSI_IO_REQUEST *)
515 (io_req_base + offset);
516 memset(cmd->io_request, 0,
517 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
518 cmd->io_request_phys_addr = io_req_base_phys + offset;
520 list_add_tail(&cmd->list, &fusion->cmd_pool);
524 * Create a frame pool and assign one frame to each cmd
526 if (megasas_create_frame_pool_fusion(instance)) {
527 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
528 megasas_free_cmds_fusion(instance);
535 pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
536 fusion->io_request_frames_phys);
537 pci_pool_destroy(fusion->io_request_frames_pool);
539 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
540 fusion->reply_frames_desc,
541 fusion->reply_frames_desc_phys);
542 pci_pool_free(fusion->reply_frames_desc_pool,
543 fusion->reply_frames_desc,
544 fusion->reply_frames_desc_phys);
545 pci_pool_destroy(fusion->reply_frames_desc_pool);
548 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
549 fusion->req_frames_desc,
550 fusion->req_frames_desc_phys);
556 * wait_and_poll - Issues a polling command
557 * @instance: Adapter soft state
558 * @cmd: Command packet to be issued
560 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
563 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd)
566 struct megasas_header *frame_hdr = &cmd->frame->hdr;
568 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
571 * Wait for cmd_status to change
573 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
578 if (frame_hdr->cmd_status == 0xff)
585 * megasas_ioc_init_fusion - Initializes the FW
586 * @instance: Adapter soft state
588 * Issues the IOC Init cmd
591 megasas_ioc_init_fusion(struct megasas_instance *instance)
593 struct megasas_init_frame *init_frame;
594 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
595 dma_addr_t ioc_init_handle;
596 struct megasas_cmd *cmd;
598 struct fusion_context *fusion;
599 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
601 struct megasas_header *frame_hdr;
603 fusion = instance->ctrl_context;
605 cmd = megasas_get_cmd(instance);
608 printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
614 dma_alloc_coherent(&instance->pdev->dev,
615 sizeof(struct MPI2_IOC_INIT_REQUEST),
616 &ioc_init_handle, GFP_KERNEL);
618 if (!IOCInitMessage) {
619 printk(KERN_ERR "Could not allocate memory for "
625 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
627 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
628 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
629 IOCInitMessage->MsgVersion = MPI2_VERSION;
630 IOCInitMessage->HeaderVersion = MPI2_HEADER_VERSION;
631 IOCInitMessage->SystemRequestFrameSize =
632 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4;
634 IOCInitMessage->ReplyDescriptorPostQueueDepth = fusion->reply_q_depth;
635 IOCInitMessage->ReplyDescriptorPostQueueAddress =
636 fusion->reply_frames_desc_phys;
637 IOCInitMessage->SystemRequestFrameBaseAddress =
638 fusion->io_request_frames_phys;
639 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
640 init_frame = (struct megasas_init_frame *)cmd->frame;
641 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
643 frame_hdr = &cmd->frame->hdr;
644 frame_hdr->cmd_status = 0xFF;
645 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
647 init_frame->cmd = MFI_CMD_INIT;
648 init_frame->cmd_status = 0xFF;
650 /* driver support Extended MSIX */
651 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
652 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
653 init_frame->driver_operations.
654 mfi_capabilities.support_additional_msix = 1;
656 init_frame->queue_info_new_phys_addr_lo = ioc_init_handle;
657 init_frame->data_xfer_len = sizeof(struct MPI2_IOC_INIT_REQUEST);
660 (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)fusion->req_frames_desc;
662 req_desc->Words = cmd->frame_phys_addr;
663 req_desc->MFAIo.RequestFlags =
664 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
665 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
668 * disable the intr before firing the init frame
670 instance->instancet->disable_intr(instance);
672 for (i = 0; i < (10 * 1000); i += 20) {
673 if (readl(&instance->reg_set->doorbell) & 1)
679 instance->instancet->fire_cmd(instance, req_desc->u.low,
680 req_desc->u.high, instance->reg_set);
682 wait_and_poll(instance, cmd);
684 frame_hdr = &cmd->frame->hdr;
685 if (frame_hdr->cmd_status != 0) {
689 printk(KERN_ERR "megasas:IOC Init cmd success\n");
694 megasas_return_cmd(instance, cmd);
696 dma_free_coherent(&instance->pdev->dev,
697 sizeof(struct MPI2_IOC_INIT_REQUEST),
698 IOCInitMessage, ioc_init_handle);
704 * megasas_get_ld_map_info - Returns FW's ld_map structure
705 * @instance: Adapter soft state
706 * @pend: Pend the command or not
707 * Issues an internal command (DCMD) to get the FW's controller PD
708 * list structure. This information is mainly used to find out SYSTEM
709 * supported by the FW.
712 megasas_get_ld_map_info(struct megasas_instance *instance)
715 struct megasas_cmd *cmd;
716 struct megasas_dcmd_frame *dcmd;
717 struct MR_FW_RAID_MAP_ALL *ci;
720 struct fusion_context *fusion;
722 cmd = megasas_get_cmd(instance);
725 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
729 fusion = instance->ctrl_context;
732 megasas_return_cmd(instance, cmd);
736 dcmd = &cmd->frame->dcmd;
738 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
739 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
741 ci = fusion->ld_map[(instance->map_id & 1)];
742 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
745 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
746 megasas_return_cmd(instance, cmd);
750 memset(ci, 0, sizeof(*ci));
751 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
753 dcmd->cmd = MFI_CMD_DCMD;
754 dcmd->cmd_status = 0xFF;
756 dcmd->flags = MFI_FRAME_DIR_READ;
759 dcmd->data_xfer_len = size_map_info;
760 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
761 dcmd->sgl.sge32[0].phys_addr = ci_h;
762 dcmd->sgl.sge32[0].length = size_map_info;
764 if (!megasas_issue_polled(instance, cmd))
767 printk(KERN_ERR "megasas: Get LD Map Info Failed\n");
771 megasas_return_cmd(instance, cmd);
777 megasas_get_map_info(struct megasas_instance *instance)
779 struct fusion_context *fusion = instance->ctrl_context;
781 fusion->fast_path_io = 0;
782 if (!megasas_get_ld_map_info(instance)) {
783 if (MR_ValidateMapInfo(instance)) {
784 fusion->fast_path_io = 1;
792 * megasas_sync_map_info - Returns FW's ld_map structure
793 * @instance: Adapter soft state
795 * Issues an internal command (DCMD) to get the FW's controller PD
796 * list structure. This information is mainly used to find out SYSTEM
797 * supported by the FW.
800 megasas_sync_map_info(struct megasas_instance *instance)
803 struct megasas_cmd *cmd;
804 struct megasas_dcmd_frame *dcmd;
805 u32 size_sync_info, num_lds;
806 struct fusion_context *fusion;
807 struct MR_LD_TARGET_SYNC *ci = NULL;
808 struct MR_FW_RAID_MAP_ALL *map;
809 struct MR_LD_RAID *raid;
810 struct MR_LD_TARGET_SYNC *ld_sync;
814 cmd = megasas_get_cmd(instance);
817 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
822 fusion = instance->ctrl_context;
825 megasas_return_cmd(instance, cmd);
829 map = fusion->ld_map[instance->map_id & 1];
831 num_lds = map->raidMap.ldCount;
833 dcmd = &cmd->frame->dcmd;
835 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
837 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
839 ci = (struct MR_LD_TARGET_SYNC *)
840 fusion->ld_map[(instance->map_id - 1) & 1];
841 memset(ci, 0, sizeof(struct MR_FW_RAID_MAP_ALL));
843 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
845 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
847 for (i = 0; i < num_lds; i++, ld_sync++) {
848 raid = MR_LdRaidGet(i, map);
849 ld_sync->targetId = MR_GetLDTgtId(i, map);
850 ld_sync->seqNum = raid->seqNum;
853 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
854 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
856 dcmd->cmd = MFI_CMD_DCMD;
857 dcmd->cmd_status = 0xFF;
859 dcmd->flags = MFI_FRAME_DIR_WRITE;
862 dcmd->data_xfer_len = size_map_info;
863 dcmd->mbox.b[0] = num_lds;
864 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
865 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
866 dcmd->sgl.sge32[0].phys_addr = ci_h;
867 dcmd->sgl.sge32[0].length = size_map_info;
869 instance->map_update_cmd = cmd;
871 instance->instancet->issue_dcmd(instance, cmd);
877 * meagasas_display_intel_branding - Display branding string
878 * @instance: per adapter object
883 megasas_display_intel_branding(struct megasas_instance *instance)
885 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
888 switch (instance->pdev->device) {
889 case PCI_DEVICE_ID_LSI_INVADER:
890 switch (instance->pdev->subsystem_device) {
891 case MEGARAID_INTEL_RS3DC080_SSDID:
892 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
893 instance->host->host_no,
894 MEGARAID_INTEL_RS3DC080_BRANDING);
896 case MEGARAID_INTEL_RS3DC040_SSDID:
897 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
898 instance->host->host_no,
899 MEGARAID_INTEL_RS3DC040_BRANDING);
901 case MEGARAID_INTEL_RS3SC008_SSDID:
902 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
903 instance->host->host_no,
904 MEGARAID_INTEL_RS3SC008_BRANDING);
906 case MEGARAID_INTEL_RS3MC044_SSDID:
907 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
908 instance->host->host_no,
909 MEGARAID_INTEL_RS3MC044_BRANDING);
915 case PCI_DEVICE_ID_LSI_FURY:
916 switch (instance->pdev->subsystem_device) {
917 case MEGARAID_INTEL_RS3WC080_SSDID:
918 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
919 instance->host->host_no,
920 MEGARAID_INTEL_RS3WC080_BRANDING);
922 case MEGARAID_INTEL_RS3WC040_SSDID:
923 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
924 instance->host->host_no,
925 MEGARAID_INTEL_RS3WC040_BRANDING);
937 * megasas_init_adapter_fusion - Initializes the FW
938 * @instance: Adapter soft state
940 * This is the main function for initializing firmware.
943 megasas_init_adapter_fusion(struct megasas_instance *instance)
945 struct megasas_register_set __iomem *reg_set;
946 struct fusion_context *fusion;
950 fusion = instance->ctrl_context;
952 reg_set = instance->reg_set;
955 * Get various operational parameters from status register
957 instance->max_fw_cmds =
958 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
959 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
962 * Reduce the max supported cmds by 1. This is to ensure that the
963 * reply_q_sz (1 more than the max cmd that driver may send)
964 * does not exceed max cmds that the FW can support
966 instance->max_fw_cmds = instance->max_fw_cmds-1;
967 /* Only internal cmds (DCMD) need to have MFI frames */
968 instance->max_mfi_cmds = MEGASAS_INT_CMDS;
970 max_cmd = instance->max_fw_cmds;
972 fusion->reply_q_depth = ((max_cmd + 1 + 15)/16)*16;
974 fusion->request_alloc_sz =
975 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
976 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
977 *(fusion->reply_q_depth);
978 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
979 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
980 (max_cmd + 1)); /* Extra 1 for SMID 0 */
982 fusion->max_sge_in_main_msg =
983 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
984 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
986 fusion->max_sge_in_chain =
987 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
989 instance->max_num_sge = fusion->max_sge_in_main_msg +
990 fusion->max_sge_in_chain - 2;
992 /* Used for pass thru MFI frame (DCMD) */
993 fusion->chain_offset_mfi_pthru =
994 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
996 fusion->chain_offset_io_request =
997 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
998 sizeof(union MPI2_SGE_IO_UNION))/16;
1000 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1001 for (i = 0 ; i < count; i++)
1002 fusion->last_reply_idx[i] = 0;
1005 * Allocate memory for descriptors
1006 * Create a pool of commands
1008 if (megasas_alloc_cmds(instance))
1009 goto fail_alloc_mfi_cmds;
1010 if (megasas_alloc_cmds_fusion(instance))
1011 goto fail_alloc_cmds;
1013 if (megasas_ioc_init_fusion(instance))
1016 megasas_display_intel_branding(instance);
1018 instance->flag_ieee = 1;
1020 fusion->map_sz = sizeof(struct MR_FW_RAID_MAP) +
1021 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
1023 fusion->fast_path_io = 0;
1025 for (i = 0; i < 2; i++) {
1026 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1028 &fusion->ld_map_phys[i],
1030 if (!fusion->ld_map[i]) {
1031 printk(KERN_ERR "megasas: Could not allocate memory "
1037 if (!megasas_get_map_info(instance))
1038 megasas_sync_map_info(instance);
1044 dma_free_coherent(&instance->pdev->dev, fusion->map_sz,
1045 fusion->ld_map[0], fusion->ld_map_phys[0]);
1047 megasas_free_cmds_fusion(instance);
1049 megasas_free_cmds(instance);
1050 fail_alloc_mfi_cmds:
1055 * megasas_fire_cmd_fusion - Sends command to the FW
1056 * @frame_phys_addr : Physical address of cmd
1057 * @frame_count : Number of frames for the command
1058 * @regs : MFI register set
1061 megasas_fire_cmd_fusion(struct megasas_instance *instance,
1062 dma_addr_t req_desc_lo,
1064 struct megasas_register_set __iomem *regs)
1066 unsigned long flags;
1068 spin_lock_irqsave(&instance->hba_lock, flags);
1071 &(regs)->inbound_low_queue_port);
1072 writel(req_desc_hi, &(regs)->inbound_high_queue_port);
1073 spin_unlock_irqrestore(&instance->hba_lock, flags);
1077 * map_cmd_status - Maps FW cmd status to OS cmd status
1078 * @cmd : Pointer to cmd
1079 * @status : status of cmd returned by FW
1080 * @ext_status : ext status of cmd returned by FW
1084 map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1090 cmd->scmd->result = DID_OK << 16;
1093 case MFI_STAT_SCSI_IO_FAILED:
1094 case MFI_STAT_LD_INIT_IN_PROGRESS:
1095 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1098 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1100 cmd->scmd->result = (DID_OK << 16) | ext_status;
1101 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1102 memset(cmd->scmd->sense_buffer, 0,
1103 SCSI_SENSE_BUFFERSIZE);
1104 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1105 SCSI_SENSE_BUFFERSIZE);
1106 cmd->scmd->result |= DRIVER_SENSE << 24;
1110 case MFI_STAT_LD_OFFLINE:
1111 case MFI_STAT_DEVICE_NOT_FOUND:
1112 cmd->scmd->result = DID_BAD_TARGET << 16;
1114 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1115 cmd->scmd->result = DID_IMM_RETRY << 16;
1118 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1119 cmd->scmd->result = DID_ERROR << 16;
1125 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1126 * @instance: Adapter soft state
1127 * @scp: SCSI command from the mid-layer
1128 * @sgl_ptr: SGL to be filled in
1129 * @cmd: cmd we are working on
1131 * If successful, this function returns the number of SG elements.
1134 megasas_make_sgl_fusion(struct megasas_instance *instance,
1135 struct scsi_cmnd *scp,
1136 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1137 struct megasas_cmd_fusion *cmd)
1139 int i, sg_processed, sge_count;
1140 struct scatterlist *os_sgl;
1141 struct fusion_context *fusion;
1143 fusion = instance->ctrl_context;
1145 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1146 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1147 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1148 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1149 sgl_ptr_end->Flags = 0;
1152 sge_count = scsi_dma_map(scp);
1154 BUG_ON(sge_count < 0);
1156 if (sge_count > instance->max_num_sge || !sge_count)
1159 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1160 sgl_ptr->Length = sg_dma_len(os_sgl);
1161 sgl_ptr->Address = sg_dma_address(os_sgl);
1163 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1164 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1165 if (i == sge_count - 1)
1166 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1170 sg_processed = i + 1;
1172 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1173 (sge_count > fusion->max_sge_in_main_msg)) {
1175 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1176 if ((instance->pdev->device ==
1177 PCI_DEVICE_ID_LSI_INVADER) ||
1178 (instance->pdev->device ==
1179 PCI_DEVICE_ID_LSI_FURY)) {
1180 if ((cmd->io_request->IoFlags &
1181 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1182 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1183 cmd->io_request->ChainOffset =
1185 chain_offset_io_request;
1187 cmd->io_request->ChainOffset = 0;
1189 cmd->io_request->ChainOffset =
1190 fusion->chain_offset_io_request;
1193 /* Prepare chain element */
1194 sg_chain->NextChainOffset = 0;
1195 if ((instance->pdev->device ==
1196 PCI_DEVICE_ID_LSI_INVADER) ||
1197 (instance->pdev->device ==
1198 PCI_DEVICE_ID_LSI_FURY))
1199 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1202 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1203 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1204 sg_chain->Length = (sizeof(union MPI2_SGE_IO_UNION)
1205 *(sge_count - sg_processed));
1206 sg_chain->Address = cmd->sg_frame_phys_addr;
1209 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1217 * megasas_set_pd_lba - Sets PD LBA
1219 * @cdb_len: cdb length
1220 * @start_blk: Start block of IO
1222 * Used to set the PD LBA in CDB for FP IOs
1225 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1226 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1227 struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1229 struct MR_LD_RAID *raid;
1231 u64 start_blk = io_info->pdBlock;
1232 u8 *cdb = io_request->CDB.CDB32;
1233 u32 num_blocks = io_info->numBlocks;
1234 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1236 /* Check if T10 PI (DIF) is enabled for this LD */
1237 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1238 raid = MR_LdRaidGet(ld, local_map_ptr);
1239 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1240 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1241 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1242 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1244 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1245 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1247 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1248 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1251 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1252 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1253 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1254 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1255 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1256 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1257 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1258 cdb[19] = (u8)(start_blk & 0xff);
1260 /* Logical block reference tag */
1261 io_request->CDB.EEDP32.PrimaryReferenceTag =
1262 cpu_to_be32(ref_tag);
1263 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
1264 io_request->IoFlags = 32; /* Specify 32-byte cdb */
1266 /* Transfer length */
1267 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1268 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1269 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1270 cdb[31] = (u8)(num_blocks & 0xff);
1272 /* set SCSI IO EEDPFlags */
1273 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1274 io_request->EEDPFlags =
1275 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1276 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1277 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1278 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1279 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
1281 io_request->EEDPFlags =
1282 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1283 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
1285 io_request->Control |= (0x4 << 26);
1286 io_request->EEDPBlockSize = scp->device->sector_size;
1288 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1289 if (((cdb_len == 12) || (cdb_len == 16)) &&
1290 (start_blk <= 0xffffffff)) {
1291 if (cdb_len == 16) {
1292 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1297 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1303 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1310 /* Transfer length */
1311 cdb[8] = (u8)(num_blocks & 0xff);
1312 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1314 io_request->IoFlags = 10; /* Specify 10-byte cdb */
1316 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1317 /* Convert to 16 byte CDB for large LBA's */
1320 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1325 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1332 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1339 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1346 /* Transfer length */
1347 cdb[13] = (u8)(num_blocks & 0xff);
1348 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1349 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1350 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1352 io_request->IoFlags = 16; /* Specify 16-byte cdb */
1356 /* Normal case, just load LBA here */
1360 u8 val = cdb[1] & 0xE0;
1361 cdb[3] = (u8)(start_blk & 0xff);
1362 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1363 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1367 cdb[5] = (u8)(start_blk & 0xff);
1368 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1369 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1370 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1373 cdb[5] = (u8)(start_blk & 0xff);
1374 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1375 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1376 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1379 cdb[9] = (u8)(start_blk & 0xff);
1380 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1381 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1382 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1383 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1384 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1385 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1386 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1393 * megasas_build_ldio_fusion - Prepares IOs to devices
1394 * @instance: Adapter soft state
1395 * @scp: SCSI command
1396 * @cmd: Command to be prepared
1398 * Prepares the io_request and chain elements (sg_frame) for IO
1399 * The IO can be for PD (Fast Path) or LD
1402 megasas_build_ldio_fusion(struct megasas_instance *instance,
1403 struct scsi_cmnd *scp,
1404 struct megasas_cmd_fusion *cmd)
1407 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
1408 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1409 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1410 struct IO_REQUEST_INFO io_info;
1411 struct fusion_context *fusion;
1412 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1414 device_id = MEGASAS_DEV_INDEX(instance, scp);
1416 fusion = instance->ctrl_context;
1418 io_request = cmd->io_request;
1419 io_request->RaidContext.VirtualDiskTgtId = device_id;
1420 io_request->RaidContext.status = 0;
1421 io_request->RaidContext.exStatus = 0;
1423 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1430 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1432 if (scp->cmd_len == 6) {
1433 datalength = (u32) scp->cmnd[4];
1434 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1435 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1437 start_lba_lo &= 0x1FFFFF;
1441 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1443 else if (scp->cmd_len == 10) {
1444 datalength = (u32) scp->cmnd[8] |
1445 ((u32) scp->cmnd[7] << 8);
1446 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1447 ((u32) scp->cmnd[3] << 16) |
1448 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1452 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1454 else if (scp->cmd_len == 12) {
1455 datalength = ((u32) scp->cmnd[6] << 24) |
1456 ((u32) scp->cmnd[7] << 16) |
1457 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1458 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1459 ((u32) scp->cmnd[3] << 16) |
1460 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1464 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1466 else if (scp->cmd_len == 16) {
1467 datalength = ((u32) scp->cmnd[10] << 24) |
1468 ((u32) scp->cmnd[11] << 16) |
1469 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1470 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1471 ((u32) scp->cmnd[7] << 16) |
1472 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1474 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1475 ((u32) scp->cmnd[3] << 16) |
1476 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1479 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1480 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
1481 io_info.numBlocks = datalength;
1482 io_info.ldTgtId = device_id;
1483 io_request->DataLength = scsi_bufflen(scp);
1485 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1488 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1490 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1491 MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) {
1492 io_request->RaidContext.regLockFlags = 0;
1495 if (MR_BuildRaidContext(instance, &io_info,
1496 &io_request->RaidContext,
1498 fp_possible = io_info.fpOkForIo;
1501 /* Use smp_processor_id() for now until cmd->request->cpu is CPU
1502 id by default, not CPU group id, otherwise all MSI-X queues won't
1504 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1505 smp_processor_id() % instance->msix_vectors : 0;
1508 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1509 local_map_ptr, start_lba_lo);
1510 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1511 cmd->request_desc->SCSIIO.RequestFlags =
1512 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1513 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1514 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1515 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1516 if (io_request->RaidContext.regLockFlags ==
1518 cmd->request_desc->SCSIIO.RequestFlags =
1519 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1520 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1521 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1522 io_request->RaidContext.nseg = 0x1;
1523 io_request->IoFlags |=
1524 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
1525 io_request->RaidContext.regLockFlags |=
1526 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1527 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1529 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1532 get_updated_dev_handle(
1533 &fusion->load_balance_info[device_id],
1535 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1537 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1538 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1539 io_request->DevHandle = io_info.devHandle;
1541 io_request->RaidContext.timeoutValue =
1542 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1543 cmd->request_desc->SCSIIO.RequestFlags =
1544 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1545 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1546 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1547 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1548 if (io_request->RaidContext.regLockFlags ==
1550 cmd->request_desc->SCSIIO.RequestFlags =
1551 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1552 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1553 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1554 io_request->RaidContext.regLockFlags |=
1555 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1556 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1557 io_request->RaidContext.nseg = 0x1;
1559 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1560 io_request->DevHandle = device_id;
1565 * megasas_build_dcdb_fusion - Prepares IOs to devices
1566 * @instance: Adapter soft state
1567 * @scp: SCSI command
1568 * @cmd: Command to be prepared
1570 * Prepares the io_request frame for non-io cmds
1573 megasas_build_dcdb_fusion(struct megasas_instance *instance,
1574 struct scsi_cmnd *scmd,
1575 struct megasas_cmd_fusion *cmd)
1578 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1580 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1581 struct fusion_context *fusion = instance->ctrl_context;
1583 io_request = cmd->io_request;
1584 device_id = MEGASAS_DEV_INDEX(instance, scmd);
1585 pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1587 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1589 /* Check if this is a system PD I/O */
1590 if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS &&
1591 instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1592 io_request->Function = 0;
1593 if (fusion->fast_path_io)
1594 io_request->DevHandle =
1595 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1596 io_request->RaidContext.timeoutValue =
1597 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1598 io_request->RaidContext.regLockFlags = 0;
1599 io_request->RaidContext.regLockRowLBA = 0;
1600 io_request->RaidContext.regLockLength = 0;
1601 io_request->RaidContext.RAIDFlags =
1602 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1603 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1604 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1605 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
1606 io_request->IoFlags |=
1607 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
1608 cmd->request_desc->SCSIIO.RequestFlags =
1609 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1610 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1611 cmd->request_desc->SCSIIO.DevHandle =
1612 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1614 * If the command is for the tape device, set the
1615 * FP timeout to the os layer timeout value.
1617 if (scmd->device->type == TYPE_TAPE) {
1618 if ((scmd->request->timeout / HZ) > 0xFFFF)
1619 io_request->RaidContext.timeoutValue =
1622 io_request->RaidContext.timeoutValue =
1623 scmd->request->timeout / HZ;
1626 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1627 io_request->DevHandle = device_id;
1628 cmd->request_desc->SCSIIO.RequestFlags =
1629 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1630 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1632 io_request->RaidContext.VirtualDiskTgtId = device_id;
1633 io_request->LUN[1] = scmd->device->lun;
1634 io_request->DataLength = scsi_bufflen(scmd);
1638 * megasas_build_io_fusion - Prepares IOs to devices
1639 * @instance: Adapter soft state
1640 * @scp: SCSI command
1641 * @cmd: Command to be prepared
1643 * Invokes helper functions to prepare request frames
1644 * and sets flags appropriate for IO/Non-IO cmd
1647 megasas_build_io_fusion(struct megasas_instance *instance,
1648 struct scsi_cmnd *scp,
1649 struct megasas_cmd_fusion *cmd)
1651 u32 device_id, sge_count;
1652 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1654 device_id = MEGASAS_DEV_INDEX(instance, scp);
1656 /* Zero out some fields so they don't get reused */
1657 io_request->LUN[1] = 0;
1658 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1659 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1660 io_request->EEDPFlags = 0;
1661 io_request->Control = 0;
1662 io_request->EEDPBlockSize = 0;
1663 io_request->ChainOffset = 0;
1664 io_request->RaidContext.RAIDFlags = 0;
1665 io_request->RaidContext.Type = 0;
1666 io_request->RaidContext.nseg = 0;
1668 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1670 * Just the CDB length,rest of the Flags are zero
1671 * This will be modified for FP in build_ldio_fusion
1673 io_request->IoFlags = scp->cmd_len;
1675 if (megasas_is_ldio(scp))
1676 megasas_build_ldio_fusion(instance, scp, cmd);
1678 megasas_build_dcdb_fusion(instance, scp, cmd);
1685 megasas_make_sgl_fusion(instance, scp,
1686 (struct MPI25_IEEE_SGE_CHAIN64 *)
1687 &io_request->SGL, cmd);
1689 if (sge_count > instance->max_num_sge) {
1690 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1691 "max (0x%x) allowed\n", sge_count,
1692 instance->max_num_sge);
1696 io_request->RaidContext.numSGE = sge_count;
1698 io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
1700 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1701 io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
1702 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1703 io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
1705 io_request->SGLOffset0 =
1706 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1708 io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
1709 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1712 scp->SCp.ptr = (char *)cmd;
1717 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1718 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1721 struct fusion_context *fusion;
1723 if (index >= instance->max_fw_cmds) {
1724 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
1725 "descriptor\n", index);
1728 fusion = instance->ctrl_context;
1729 p = fusion->req_frames_desc
1730 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1732 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1736 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1737 * issuing non IOCTL cmd
1738 * @instance: Adapter soft state
1739 * @scmd: pointer to scsi cmd from OS
1742 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1743 struct scsi_cmnd *scmd)
1745 struct megasas_cmd_fusion *cmd;
1746 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1748 struct fusion_context *fusion;
1750 fusion = instance->ctrl_context;
1752 cmd = megasas_get_cmd_fusion(instance);
1754 return SCSI_MLQUEUE_HOST_BUSY;
1758 req_desc = megasas_get_request_descriptor(instance, index-1);
1762 req_desc->Words = 0;
1763 cmd->request_desc = req_desc;
1765 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1766 megasas_return_cmd_fusion(instance, cmd);
1767 printk(KERN_ERR "megasas: Error building command.\n");
1768 cmd->request_desc = NULL;
1772 req_desc = cmd->request_desc;
1773 req_desc->SCSIIO.SMID = index;
1775 if (cmd->io_request->ChainOffset != 0 &&
1776 cmd->io_request->ChainOffset != 0xF)
1777 printk(KERN_ERR "megasas: The chain offset value is not "
1778 "correct : %x\n", cmd->io_request->ChainOffset);
1781 * Issue the command to the FW
1783 atomic_inc(&instance->fw_outstanding);
1785 instance->instancet->fire_cmd(instance,
1786 req_desc->u.low, req_desc->u.high,
1793 * complete_cmd_fusion - Completes command
1794 * @instance: Adapter soft state
1795 * Completes all commands that is in reply descriptor queue
1798 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
1800 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1801 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1802 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1803 struct fusion_context *fusion;
1804 struct megasas_cmd *cmd_mfi;
1805 struct megasas_cmd_fusion *cmd_fusion;
1806 u16 smid, num_completed;
1807 u8 reply_descript_type, arm;
1808 u32 status, extStatus, device_id;
1809 union desc_value d_val;
1810 struct LD_LOAD_BALANCE_INFO *lbinfo;
1812 fusion = instance->ctrl_context;
1814 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1817 desc = fusion->reply_frames_desc;
1818 desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1819 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1820 fusion->last_reply_idx[MSIxIndex];
1822 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1824 d_val.word = desc->Words;
1826 reply_descript_type = reply_desc->ReplyFlags &
1827 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1829 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1834 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
1835 smid = reply_desc->SMID;
1837 cmd_fusion = fusion->cmd_list[smid - 1];
1840 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1841 cmd_fusion->io_request;
1843 if (cmd_fusion->scmd)
1844 cmd_fusion->scmd->SCp.ptr = NULL;
1846 status = scsi_io_req->RaidContext.status;
1847 extStatus = scsi_io_req->RaidContext.exStatus;
1849 switch (scsi_io_req->Function) {
1850 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
1851 /* Update load balancing info */
1852 device_id = MEGASAS_DEV_INDEX(instance,
1854 lbinfo = &fusion->load_balance_info[device_id];
1855 if (cmd_fusion->scmd->SCp.Status &
1856 MEGASAS_LOAD_BALANCE_FLAG) {
1857 arm = lbinfo->raid1DevHandle[0] ==
1858 cmd_fusion->io_request->DevHandle ? 0 :
1860 atomic_dec(&lbinfo->scsi_pending_cmds[arm]);
1861 cmd_fusion->scmd->SCp.Status &=
1862 ~MEGASAS_LOAD_BALANCE_FLAG;
1864 if (reply_descript_type ==
1865 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
1866 if (megasas_dbg_lvl == 5)
1867 printk(KERN_ERR "\nmegasas: FAST Path "
1870 /* Fall thru and complete IO */
1871 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
1872 /* Map the FW Cmd Status */
1873 map_cmd_status(cmd_fusion, status, extStatus);
1874 scsi_dma_unmap(cmd_fusion->scmd);
1875 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
1876 scsi_io_req->RaidContext.status = 0;
1877 scsi_io_req->RaidContext.exStatus = 0;
1878 megasas_return_cmd_fusion(instance, cmd_fusion);
1879 atomic_dec(&instance->fw_outstanding);
1882 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
1883 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1884 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1885 cmd_fusion->flags = 0;
1886 megasas_return_cmd_fusion(instance, cmd_fusion);
1891 fusion->last_reply_idx[MSIxIndex]++;
1892 if (fusion->last_reply_idx[MSIxIndex] >=
1893 fusion->reply_q_depth)
1894 fusion->last_reply_idx[MSIxIndex] = 0;
1896 desc->Words = ULLONG_MAX;
1899 /* Get the next reply descriptor */
1900 if (!fusion->last_reply_idx[MSIxIndex])
1901 desc = fusion->reply_frames_desc +
1902 ((MSIxIndex * fusion->reply_alloc_sz)/
1903 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
1908 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1910 d_val.word = desc->Words;
1912 reply_descript_type = reply_desc->ReplyFlags &
1913 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1915 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1923 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1924 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
1925 writel(((MSIxIndex & 0x7) << 24) |
1926 fusion->last_reply_idx[MSIxIndex],
1927 instance->reply_post_host_index_addr[MSIxIndex/8]);
1929 writel((MSIxIndex << 24) |
1930 fusion->last_reply_idx[MSIxIndex],
1931 instance->reply_post_host_index_addr[0]);
1932 megasas_check_and_restore_queue_depth(instance);
1937 * megasas_complete_cmd_dpc_fusion - Completes command
1938 * @instance: Adapter soft state
1940 * Tasklet to complete cmds
1943 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
1945 struct megasas_instance *instance =
1946 (struct megasas_instance *)instance_addr;
1947 unsigned long flags;
1948 u32 count, MSIxIndex;
1950 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1952 /* If we have already declared adapter dead, donot complete cmds */
1953 spin_lock_irqsave(&instance->hba_lock, flags);
1954 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1955 spin_unlock_irqrestore(&instance->hba_lock, flags);
1958 spin_unlock_irqrestore(&instance->hba_lock, flags);
1960 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
1961 complete_cmd_fusion(instance, MSIxIndex);
1965 * megasas_isr_fusion - isr entry point
1967 irqreturn_t megasas_isr_fusion(int irq, void *devp)
1969 struct megasas_irq_context *irq_context = devp;
1970 struct megasas_instance *instance = irq_context->instance;
1971 u32 mfiStatus, fw_state;
1973 if (instance->mask_interrupts)
1976 if (!instance->msix_vectors) {
1977 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
1982 /* If we are resetting, bail */
1983 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
1984 instance->instancet->clear_intr(instance->reg_set);
1988 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
1989 instance->instancet->clear_intr(instance->reg_set);
1990 /* If we didn't complete any commands, check for FW fault */
1991 fw_state = instance->instancet->read_fw_status_reg(
1992 instance->reg_set) & MFI_STATE_MASK;
1993 if (fw_state == MFI_STATE_FAULT)
1994 schedule_work(&instance->work_init);
2001 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2002 * @instance: Adapter soft state
2003 * mfi_cmd: megasas_cmd pointer
2007 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
2008 struct megasas_cmd *mfi_cmd)
2010 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
2011 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
2012 struct megasas_cmd_fusion *cmd;
2013 struct fusion_context *fusion;
2014 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
2016 cmd = megasas_get_cmd_fusion(instance);
2020 /* Save the smid. To be used for returning the cmd */
2021 mfi_cmd->context.smid = cmd->index;
2023 cmd->sync_cmd_idx = mfi_cmd->index;
2026 * For cmds where the flag is set, store the flag and check
2027 * on completion. For cmds with this flag, don't call
2028 * megasas_complete_cmd
2031 if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)
2032 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
2034 fusion = instance->ctrl_context;
2035 io_req = cmd->io_request;
2037 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2038 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
2039 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2040 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2041 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2042 sgl_ptr_end->Flags = 0;
2046 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2048 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2049 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2051 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2053 mpi25_ieee_chain->Address = mfi_cmd->frame_phys_addr;
2055 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2056 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2058 mpi25_ieee_chain->Length = MEGASAS_MAX_SZ_CHAIN_FRAME;
2064 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2065 * @instance: Adapter soft state
2066 * @cmd: mfi cmd to build
2069 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2070 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2072 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2075 if (build_mpt_mfi_pass_thru(instance, cmd)) {
2076 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
2080 index = cmd->context.smid;
2082 req_desc = megasas_get_request_descriptor(instance, index - 1);
2087 req_desc->Words = 0;
2088 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2089 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2091 req_desc->SCSIIO.SMID = index;
2097 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2098 * @instance: Adapter soft state
2099 * @cmd: mfi cmd pointer
2103 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2104 struct megasas_cmd *cmd)
2106 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2108 req_desc = build_mpt_cmd(instance, cmd);
2110 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2113 instance->instancet->fire_cmd(instance, req_desc->u.low,
2114 req_desc->u.high, instance->reg_set);
2118 * megasas_release_fusion - Reverses the FW initialization
2119 * @intance: Adapter soft state
2122 megasas_release_fusion(struct megasas_instance *instance)
2124 megasas_free_cmds(instance);
2125 megasas_free_cmds_fusion(instance);
2127 iounmap(instance->reg_set);
2129 pci_release_selected_regions(instance->pdev, instance->bar);
2133 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2134 * @regs: MFI register set
2137 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2139 return readl(&(regs)->outbound_scratch_pad);
2143 * megasas_adp_reset_fusion - For controller reset
2144 * @regs: MFI register set
2147 megasas_adp_reset_fusion(struct megasas_instance *instance,
2148 struct megasas_register_set __iomem *regs)
2154 * megasas_check_reset_fusion - For controller reset check
2155 * @regs: MFI register set
2158 megasas_check_reset_fusion(struct megasas_instance *instance,
2159 struct megasas_register_set __iomem *regs)
2164 /* This function waits for outstanding commands on fusion to complete */
2165 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance)
2167 int i, outstanding, retval = 0;
2170 for (i = 0; i < resetwaittime; i++) {
2171 /* Check if firmware is in fault state */
2172 fw_state = instance->instancet->read_fw_status_reg(
2173 instance->reg_set) & MFI_STATE_MASK;
2174 if (fw_state == MFI_STATE_FAULT) {
2175 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
2176 " will reset adapter.\n");
2181 outstanding = atomic_read(&instance->fw_outstanding);
2185 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2186 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2187 "commands to complete\n", i, outstanding);
2188 megasas_complete_cmd_dpc_fusion(
2189 (unsigned long)instance);
2194 if (atomic_read(&instance->fw_outstanding)) {
2195 printk("megaraid_sas: pending commands remain after waiting, "
2196 "will reset adapter.\n");
2203 void megasas_reset_reply_desc(struct megasas_instance *instance)
2206 struct fusion_context *fusion;
2207 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2209 fusion = instance->ctrl_context;
2210 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2211 for (i = 0 ; i < count ; i++)
2212 fusion->last_reply_idx[i] = 0;
2213 reply_desc = fusion->reply_frames_desc;
2214 for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
2215 reply_desc->Words = ULLONG_MAX;
2218 /* Core fusion reset function */
2219 int megasas_reset_fusion(struct Scsi_Host *shost)
2221 int retval = SUCCESS, i, j, retry = 0;
2222 struct megasas_instance *instance;
2223 struct megasas_cmd_fusion *cmd_fusion;
2224 struct fusion_context *fusion;
2225 struct megasas_cmd *cmd_mfi;
2226 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2227 u32 host_diag, abs_state, status_reg, reset_adapter;
2229 instance = (struct megasas_instance *)shost->hostdata;
2230 fusion = instance->ctrl_context;
2232 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2233 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
2234 "returning FAILED.\n");
2238 mutex_lock(&instance->reset_mutex);
2239 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2240 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2241 instance->instancet->disable_intr(instance);
2244 /* First try waiting for commands to complete */
2245 if (megasas_wait_for_outstanding_fusion(instance)) {
2246 printk(KERN_WARNING "megaraid_sas: resetting fusion "
2248 /* Now return commands back to the OS */
2249 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2250 cmd_fusion = fusion->cmd_list[i];
2251 if (cmd_fusion->scmd) {
2252 scsi_dma_unmap(cmd_fusion->scmd);
2253 cmd_fusion->scmd->result = (DID_RESET << 16);
2254 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2255 megasas_return_cmd_fusion(instance, cmd_fusion);
2256 atomic_dec(&instance->fw_outstanding);
2260 status_reg = instance->instancet->read_fw_status_reg(
2262 abs_state = status_reg & MFI_STATE_MASK;
2263 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2264 if (instance->disableOnlineCtrlReset ||
2265 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
2266 /* Reset not supported, kill adapter */
2267 printk(KERN_WARNING "megaraid_sas: Reset not supported"
2268 ", killing adapter.\n");
2269 megaraid_sas_kill_hba(instance);
2270 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2275 /* Now try to reset the chip */
2276 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2277 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2278 &instance->reg_set->fusion_seq_offset);
2279 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2280 &instance->reg_set->fusion_seq_offset);
2281 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2282 &instance->reg_set->fusion_seq_offset);
2283 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2284 &instance->reg_set->fusion_seq_offset);
2285 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2286 &instance->reg_set->fusion_seq_offset);
2287 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2288 &instance->reg_set->fusion_seq_offset);
2289 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2290 &instance->reg_set->fusion_seq_offset);
2292 /* Check that the diag write enable (DRWE) bit is on */
2293 host_diag = readl(&instance->reg_set->fusion_host_diag);
2295 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2298 readl(&instance->reg_set->fusion_host_diag);
2299 if (retry++ == 100) {
2300 printk(KERN_WARNING "megaraid_sas: "
2301 "Host diag unlock failed!\n");
2305 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2308 /* Send chip reset command */
2309 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2310 &instance->reg_set->fusion_host_diag);
2313 /* Make sure reset adapter bit is cleared */
2314 host_diag = readl(&instance->reg_set->fusion_host_diag);
2316 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2319 readl(&instance->reg_set->fusion_host_diag);
2320 if (retry++ == 1000) {
2321 printk(KERN_WARNING "megaraid_sas: "
2322 "Diag reset adapter never "
2327 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2331 instance->instancet->read_fw_status_reg(
2332 instance->reg_set) & MFI_STATE_MASK;
2335 while ((abs_state <= MFI_STATE_FW_INIT) &&
2339 instance->instancet->read_fw_status_reg(
2340 instance->reg_set) & MFI_STATE_MASK;
2342 if (abs_state <= MFI_STATE_FW_INIT) {
2343 printk(KERN_WARNING "megaraid_sas: firmware "
2344 "state < MFI_STATE_FW_INIT, state = "
2345 "0x%x\n", abs_state);
2349 /* Wait for FW to become ready */
2350 if (megasas_transition_to_ready(instance, 1)) {
2351 printk(KERN_WARNING "megaraid_sas: Failed to "
2352 "transition controller to ready.\n");
2356 megasas_reset_reply_desc(instance);
2357 if (megasas_ioc_init_fusion(instance)) {
2358 printk(KERN_WARNING "megaraid_sas: "
2359 "megasas_ioc_init_fusion() failed!\n");
2363 clear_bit(MEGASAS_FUSION_IN_RESET,
2364 &instance->reset_flags);
2365 instance->instancet->enable_intr(instance);
2366 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2368 /* Re-fire management commands */
2369 for (j = 0 ; j < instance->max_fw_cmds; j++) {
2370 cmd_fusion = fusion->cmd_list[j];
2371 if (cmd_fusion->sync_cmd_idx !=
2375 cmd_list[cmd_fusion->sync_cmd_idx];
2376 if (cmd_mfi->frame->dcmd.opcode ==
2377 MR_DCMD_LD_MAP_GET_INFO) {
2378 megasas_return_cmd(instance,
2380 megasas_return_cmd_fusion(
2381 instance, cmd_fusion);
2384 megasas_get_request_descriptor(
2386 cmd_mfi->context.smid
2393 instance->instancet->
2406 /* Reset load balance info */
2407 memset(fusion->load_balance_info, 0,
2408 sizeof(struct LD_LOAD_BALANCE_INFO)
2409 *MAX_LOGICAL_DRIVES);
2411 if (!megasas_get_map_info(instance))
2412 megasas_sync_map_info(instance);
2414 /* Adapter reset completed successfully */
2415 printk(KERN_WARNING "megaraid_sas: Reset "
2420 /* Reset failed, kill the adapter */
2421 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
2423 megaraid_sas_kill_hba(instance);
2426 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2427 instance->instancet->enable_intr(instance);
2428 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2431 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2432 mutex_unlock(&instance->reset_mutex);
2436 /* Fusion OCR work queue */
2437 void megasas_fusion_ocr_wq(struct work_struct *work)
2439 struct megasas_instance *instance =
2440 container_of(work, struct megasas_instance, work_init);
2442 megasas_reset_fusion(instance->host);
2445 struct megasas_instance_template megasas_instance_template_fusion = {
2446 .fire_cmd = megasas_fire_cmd_fusion,
2447 .enable_intr = megasas_enable_intr_fusion,
2448 .disable_intr = megasas_disable_intr_fusion,
2449 .clear_intr = megasas_clear_intr_fusion,
2450 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2451 .adp_reset = megasas_adp_reset_fusion,
2452 .check_reset = megasas_check_reset_fusion,
2453 .service_isr = megasas_isr_fusion,
2454 .tasklet = megasas_complete_cmd_dpc_fusion,
2455 .init_adapter = megasas_init_adapter_fusion,
2456 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2457 .issue_dcmd = megasas_issue_dcmd_fusion,